Return reloc_class_ifunc for reloc against IFUNC
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets in custom GDB protocol
8926118c 2
32d0add0 3 Copyright (C) 1988-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b
JM
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b
JM
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c5aa993b 19
23860348 20/* See the GDB User Guide for details of the GDB remote protocol. */
c5aa993b 21
c906108c 22#include "defs.h"
c906108c
SS
23#include <ctype.h>
24#include <fcntl.h>
c906108c 25#include "inferior.h"
45741a9c 26#include "infrun.h"
c906108c
SS
27#include "bfd.h"
28#include "symfile.h"
29#include "target.h"
c5aa993b 30/*#include "terminal.h" */
c906108c
SS
31#include "gdbcmd.h"
32#include "objfiles.h"
33#include "gdb-stabs.h"
34#include "gdbthread.h"
c2c6d25f 35#include "remote.h"
722247f1 36#include "remote-notif.h"
4e052eda 37#include "regcache.h"
fd0407d6 38#include "value.h"
6867ae3e 39#include "observer.h"
a77053c2 40#include "solib.h"
37a105a1
DJ
41#include "cli/cli-decode.h"
42#include "cli/cli-setshow.h"
424163ea 43#include "target-descriptions.h"
a4453b7e 44#include "gdb_bfd.h"
614c279d 45#include "filestuff.h"
9c3d6531 46#include "rsp-low.h"
6b940e6a 47#include "disasm.h"
f00aae0f 48#include "location.h"
c906108c 49
9846de1b 50#include <sys/time.h>
c906108c 51
43ff13b4 52#include "event-loop.h"
c2c6d25f 53#include "event-top.h"
2acceee2 54#include "inf-loop.h"
43ff13b4 55
c906108c
SS
56#include <signal.h>
57#include "serial.h"
58
6240bebf
MS
59#include "gdbcore.h" /* for exec_bfd */
60
449092f6 61#include "remote-fileio.h"
a6b151f1 62#include "gdb/fileio.h"
53ce3c39 63#include <sys/stat.h>
dc146f7c 64#include "xml-support.h"
449092f6 65
fd79ecee
DJ
66#include "memory-map.h"
67
35b1e5cc
SS
68#include "tracepoint.h"
69#include "ax.h"
70#include "ax-gdb.h"
d1feda86 71#include "agent.h"
9accd112 72#include "btrace.h"
35b1e5cc 73
0df8b418 74/* Temp hacks for tracepoint encoding migration. */
35b1e5cc
SS
75static char *target_buf;
76static long target_buf_size;
35b1e5cc 77
6765f3e5
DJ
78/* The size to align memory write packets, when practical. The protocol
79 does not guarantee any alignment, and gdb will generate short
80 writes and unaligned writes, but even as a best-effort attempt this
81 can improve bulk transfers. For instance, if a write is misaligned
82 relative to the target's data bus, the stub may need to make an extra
83 round trip fetching data from the target. This doesn't make a
84 huge difference, but it's easy to do, so we try to be helpful.
85
86 The alignment chosen is arbitrary; usually data bus width is
87 important here, not the possibly larger cache line size. */
88enum { REMOTE_ALIGN_WRITES = 16 };
89
23860348 90/* Prototypes for local functions. */
934b9bac 91static void async_cleanup_sigint_signal_handler (void *dummy);
6d820c5c 92static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
74531fed 93static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
fee9eda9 94 int forever, int *is_notif);
6426a772 95
934b9bac
JK
96static void async_handle_remote_sigint (int);
97static void async_handle_remote_sigint_twice (int);
43ff13b4 98
a14ed312 99static void remote_files_info (struct target_ops *ignore);
c906108c 100
f32dbf8c
MM
101static void remote_prepare_to_store (struct target_ops *self,
102 struct regcache *regcache);
c906108c 103
014f9477
TT
104static void remote_open_1 (const char *, int, struct target_ops *,
105 int extended_p);
c906108c 106
de90e03d 107static void remote_close (struct target_ops *self);
c906108c 108
cbb8991c
DB
109struct remote_state;
110
111static int remote_vkill (int pid, struct remote_state *rs);
112
136d6dae 113static void remote_mourn (struct target_ops *ops);
c906108c 114
a14ed312 115static void extended_remote_restart (void);
c906108c 116
136d6dae 117static void extended_remote_mourn (struct target_ops *);
c906108c 118
6d820c5c 119static void remote_send (char **buf, long *sizeof_buf_p);
c906108c 120
a14ed312 121static int readchar (int timeout);
c906108c 122
c33e31fd
PA
123static void remote_serial_write (const char *str, int len);
124
7d85a9c0 125static void remote_kill (struct target_ops *ops);
c906108c 126
6a109b6b 127static int remote_can_async_p (struct target_ops *);
75c99385 128
6a109b6b 129static int remote_is_async_p (struct target_ops *);
75c99385 130
6a3753b3 131static void remote_async (struct target_ops *ops, int enable);
75c99385 132
934b9bac 133static void sync_remote_interrupt_twice (int signo);
7a292a7a 134
a14ed312 135static void interrupt_query (void);
c906108c 136
79d7f229
PA
137static void set_general_thread (struct ptid ptid);
138static void set_continue_thread (struct ptid ptid);
c906108c 139
a14ed312 140static void get_offsets (void);
c906108c 141
6d820c5c
DJ
142static void skip_frame (void);
143
144static long read_frame (char **buf_p, long *sizeof_buf);
c906108c 145
a14ed312 146static int hexnumlen (ULONGEST num);
c906108c 147
a14ed312 148static void init_remote_ops (void);
c906108c 149
a14ed312 150static void init_extended_remote_ops (void);
c906108c 151
1eab8a48 152static void remote_stop (struct target_ops *self, ptid_t);
c906108c 153
a14ed312 154static int stubhex (int ch);
c906108c 155
a14ed312 156static int hexnumstr (char *, ULONGEST);
c906108c 157
a14ed312 158static int hexnumnstr (char *, ULONGEST, int);
2df3850c 159
a14ed312 160static CORE_ADDR remote_address_masked (CORE_ADDR);
c906108c 161
baa336ce 162static void print_packet (const char *);
c906108c 163
a14ed312 164static void compare_sections_command (char *, int);
c906108c 165
a14ed312 166static void packet_command (char *, int);
c906108c 167
a14ed312 168static int stub_unpack_int (char *buff, int fieldlength);
c906108c 169
39f77062 170static ptid_t remote_current_thread (ptid_t oldptid);
c906108c 171
baa336ce 172static int putpkt_binary (const char *buf, int cnt);
c906108c 173
a14ed312 174static void check_binary_download (CORE_ADDR addr);
c906108c 175
5a2468f5 176struct packet_config;
5a2468f5 177
a14ed312 178static void show_packet_config_cmd (struct packet_config *config);
5a2468f5 179
bb572ddd
DJ
180static void show_remote_protocol_packet_cmd (struct ui_file *file,
181 int from_tty,
182 struct cmd_list_element *c,
183 const char *value);
184
82f73884
PA
185static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
186static ptid_t read_ptid (char *buf, char **obuf);
187
c378d69d 188static void remote_set_permissions (struct target_ops *self);
d914c394 189
8bd200f1
TT
190static int remote_get_trace_status (struct target_ops *self,
191 struct trace_status *ts);
d5551862 192
ab6617cc
TT
193static int remote_upload_tracepoints (struct target_ops *self,
194 struct uploaded_tp **utpp);
00bf0b85 195
181e3713
TT
196static int remote_upload_trace_state_variables (struct target_ops *self,
197 struct uploaded_tsv **utsvp);
00bf0b85 198
c8d104ad
PA
199static void remote_query_supported (void);
200
36d25514 201static void remote_check_symbols (void);
c8d104ad 202
a14ed312 203void _initialize_remote (void);
c906108c 204
74531fed 205struct stop_reply;
74531fed 206static void stop_reply_xfree (struct stop_reply *);
722247f1 207static void remote_parse_stop_reply (char *, struct stop_reply *);
74531fed 208static void push_stop_reply (struct stop_reply *);
bcc75809 209static void discard_pending_stop_replies_in_queue (struct remote_state *);
74531fed
PA
210static int peek_stop_reply (ptid_t ptid);
211
cbb8991c
DB
212struct threads_listing_context;
213static void remove_new_fork_children (struct threads_listing_context *);
214
74531fed 215static void remote_async_inferior_event_handler (gdb_client_data);
74531fed 216
e3594fd1 217static void remote_terminal_ours (struct target_ops *self);
d3fd5342 218
d962ef82
DJ
219static int remote_read_description_p (struct target_ops *target);
220
176a6961 221static void remote_console_output (char *msg);
dde08ee1 222
efcc2da7 223static int remote_supports_cond_breakpoints (struct target_ops *self);
b775012e 224
78eff0ec 225static int remote_can_run_breakpoint_commands (struct target_ops *self);
d3ce09f5 226
f4abbc16
MM
227static void remote_btrace_reset (void);
228
a6b151f1
DJ
229/* For "remote". */
230
231static struct cmd_list_element *remote_cmdlist;
232
bb572ddd
DJ
233/* For "set remote" and "show remote". */
234
235static struct cmd_list_element *remote_set_cmdlist;
236static struct cmd_list_element *remote_show_cmdlist;
237
d458bd84
PA
238/* Stub vCont actions support.
239
240 Each field is a boolean flag indicating whether the stub reports
241 support for the corresponding action. */
242
243struct vCont_action_support
244{
245 /* vCont;t */
246 int t;
c1e36e3e
PA
247
248 /* vCont;r */
249 int r;
d458bd84
PA
250};
251
c1e36e3e
PA
252/* Controls whether GDB is willing to use range stepping. */
253
254static int use_range_stepping = 1;
255
0d031856
TT
256#define OPAQUETHREADBYTES 8
257
258/* a 64 bit opaque identifier */
259typedef unsigned char threadref[OPAQUETHREADBYTES];
260
261/* About this many threadisds fit in a packet. */
262
263#define MAXTHREADLISTRESULTS 32
264
ea9c271d
DJ
265/* Description of the remote protocol state for the currently
266 connected target. This is per-target state, and independent of the
267 selected architecture. */
268
269struct remote_state
270{
271 /* A buffer to use for incoming packets, and its current size. The
272 buffer is grown dynamically for larger incoming packets.
273 Outgoing packets may also be constructed in this buffer.
274 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
275 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
276 packets. */
277 char *buf;
278 long buf_size;
be2a5f71 279
1e51243a
PA
280 /* True if we're going through initial connection setup (finding out
281 about the remote side's threads, relocating symbols, etc.). */
282 int starting_up;
283
be2a5f71
DJ
284 /* If we negotiated packet size explicitly (and thus can bypass
285 heuristics for the largest packet size that will not overflow
286 a buffer in the stub), this will be set to that packet size.
287 Otherwise zero, meaning to use the guessed size. */
288 long explicit_packet_size;
2d717e4f
DJ
289
290 /* remote_wait is normally called when the target is running and
291 waits for a stop reply packet. But sometimes we need to call it
292 when the target is already stopped. We can send a "?" packet
293 and have remote_wait read the response. Or, if we already have
294 the response, we can stash it in BUF and tell remote_wait to
295 skip calling getpkt. This flag is set when BUF contains a
296 stop reply packet and the target is not waiting. */
297 int cached_wait_status;
a6f3e723
SL
298
299 /* True, if in no ack mode. That is, neither GDB nor the stub will
300 expect acks from each other. The connection is assumed to be
301 reliable. */
302 int noack_mode;
82f73884
PA
303
304 /* True if we're connected in extended remote mode. */
305 int extended;
306
e24a49d8
PA
307 /* True if we resumed the target and we're waiting for the target to
308 stop. In the mean time, we can't start another command/query.
309 The remote server wouldn't be ready to process it, so we'd
310 timeout waiting for a reply that would never come and eventually
311 we'd close the connection. This can happen in asynchronous mode
312 because we allow GDB commands while the target is running. */
313 int waiting_for_stop_reply;
74531fed 314
d458bd84
PA
315 /* The status of the stub support for the various vCont actions. */
316 struct vCont_action_support supports_vCont;
782b2b07 317
3a29589a
DJ
318 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
319 responded to that. */
320 int ctrlc_pending_p;
5d93a237
TT
321
322 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
323 remote_open knows that we don't have a file open when the program
324 starts. */
325 struct serial *remote_desc;
47f8a51d
TT
326
327 /* These are the threads which we last sent to the remote system. The
328 TID member will be -1 for all or -2 for not sent yet. */
329 ptid_t general_thread;
330 ptid_t continue_thread;
262e1174
TT
331
332 /* This is the traceframe which we last selected on the remote system.
333 It will be -1 if no traceframe is selected. */
334 int remote_traceframe_number;
747dc59d
TT
335
336 char *last_pass_packet;
5e4a05c4
TT
337
338 /* The last QProgramSignals packet sent to the target. We bypass
339 sending a new program signals list down to the target if the new
340 packet is exactly the same as the last we sent. IOW, we only let
341 the target know about program signals list changes. */
342 char *last_program_signals_packet;
b73be471
TT
343
344 enum gdb_signal last_sent_signal;
280ceea3
TT
345
346 int last_sent_step;
8e88304f
TT
347
348 char *finished_object;
349 char *finished_annex;
350 ULONGEST finished_offset;
b80fafe3
TT
351
352 /* Should we try the 'ThreadInfo' query packet?
353
354 This variable (NOT available to the user: auto-detect only!)
355 determines whether GDB will use the new, simpler "ThreadInfo"
356 query or the older, more complex syntax for thread queries.
357 This is an auto-detect variable (set to true at each connect,
358 and set to false when the target fails to recognize it). */
359 int use_threadinfo_query;
360 int use_threadextra_query;
88b496c3 361
ee154bee
TT
362 /* This is set to the data address of the access causing the target
363 to stop for a watchpoint. */
364 CORE_ADDR remote_watch_data_address;
365
f7e6eed5
PA
366 /* Whether the target stopped for a breakpoint/watchpoint. */
367 enum target_stop_reason stop_reason;
0d031856
TT
368
369 threadref echo_nextthread;
370 threadref nextthread;
371 threadref resultthreadlist[MAXTHREADLISTRESULTS];
5965e028
YQ
372
373 /* The state of remote notification. */
374 struct remote_notif_state *notif_state;
f4abbc16
MM
375
376 /* The branch trace configuration. */
377 struct btrace_config btrace_config;
15a201c8
GB
378
379 /* The argument to the last "vFile:setfs:" packet we sent, used
380 to avoid sending repeated unnecessary "vFile:setfs:" packets.
381 Initialized to -1 to indicate that no "vFile:setfs:" packet
382 has yet been sent. */
383 int fs_pid;
ea9c271d
DJ
384};
385
dc146f7c
VP
386/* Private data that we'll store in (struct thread_info)->private. */
387struct private_thread_info
388{
389 char *extra;
390 int core;
391};
392
393static void
394free_private_thread_info (struct private_thread_info *info)
395{
396 xfree (info->extra);
397 xfree (info);
398}
399
ea9c271d
DJ
400/* This data could be associated with a target, but we do not always
401 have access to the current target when we need it, so for now it is
402 static. This will be fine for as long as only one target is in use
403 at a time. */
cf792862 404static struct remote_state *remote_state;
ea9c271d
DJ
405
406static struct remote_state *
0b83947e 407get_remote_state_raw (void)
ea9c271d 408{
cf792862
TT
409 return remote_state;
410}
411
412/* Allocate a new struct remote_state with xmalloc, initialize it, and
413 return it. */
414
415static struct remote_state *
416new_remote_state (void)
417{
418 struct remote_state *result = XCNEW (struct remote_state);
419
420 /* The default buffer size is unimportant; it will be expanded
421 whenever a larger buffer is needed. */
422 result->buf_size = 400;
423 result->buf = xmalloc (result->buf_size);
262e1174 424 result->remote_traceframe_number = -1;
b73be471 425 result->last_sent_signal = GDB_SIGNAL_0;
15a201c8 426 result->fs_pid = -1;
cf792862
TT
427
428 return result;
ea9c271d
DJ
429}
430
431/* Description of the remote protocol for a given architecture. */
d01949b6 432
ad10f812
AC
433struct packet_reg
434{
435 long offset; /* Offset into G packet. */
436 long regnum; /* GDB's internal register number. */
437 LONGEST pnum; /* Remote protocol register number. */
b323314b 438 int in_g_packet; /* Always part of G packet. */
f5656ead 439 /* long size in bytes; == register_size (target_gdbarch (), regnum);
23860348 440 at present. */
f5656ead 441 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
c9f4d572 442 at present. */
ad10f812
AC
443};
444
ea9c271d 445struct remote_arch_state
d01949b6 446{
ad10f812
AC
447 /* Description of the remote protocol registers. */
448 long sizeof_g_packet;
b323314b
AC
449
450 /* Description of the remote protocol registers indexed by REGNUM
f57d151a 451 (making an array gdbarch_num_regs in size). */
b323314b 452 struct packet_reg *regs;
ad10f812 453
d01949b6
AC
454 /* This is the size (in chars) of the first response to the ``g''
455 packet. It is used as a heuristic when determining the maximum
456 size of memory-read and memory-write packets. A target will
457 typically only reserve a buffer large enough to hold the ``g''
458 packet. The size does not include packet overhead (headers and
23860348 459 trailers). */
d01949b6
AC
460 long actual_register_packet_size;
461
462 /* This is the maximum size (in chars) of a non read/write packet.
23860348 463 It is also used as a cap on the size of read/write packets. */
d01949b6
AC
464 long remote_packet_size;
465};
466
35b1e5cc
SS
467/* Utility: generate error from an incoming stub packet. */
468static void
469trace_error (char *buf)
470{
471 if (*buf++ != 'E')
472 return; /* not an error msg */
473 switch (*buf)
474 {
475 case '1': /* malformed packet error */
476 if (*++buf == '0') /* general case: */
477 error (_("remote.c: error in outgoing packet."));
478 else
479 error (_("remote.c: error in outgoing packet at field #%ld."),
480 strtol (buf, NULL, 16));
35b1e5cc
SS
481 default:
482 error (_("Target returns error code '%s'."), buf);
483 }
484}
485
486/* Utility: wait for reply from stub, while accepting "O" packets. */
487static char *
488remote_get_noisy_reply (char **buf_p,
489 long *sizeof_buf)
490{
491 do /* Loop on reply from remote stub. */
492 {
493 char *buf;
a744cf53 494
0df8b418 495 QUIT; /* Allow user to bail out with ^C. */
35b1e5cc
SS
496 getpkt (buf_p, sizeof_buf, 0);
497 buf = *buf_p;
ad91cd99 498 if (buf[0] == 'E')
35b1e5cc 499 trace_error (buf);
61012eef 500 else if (startswith (buf, "qRelocInsn:"))
dde08ee1
PA
501 {
502 ULONGEST ul;
503 CORE_ADDR from, to, org_to;
504 char *p, *pp;
505 int adjusted_size = 0;
7556d4a4 506 int relocated = 0;
dde08ee1
PA
507
508 p = buf + strlen ("qRelocInsn:");
509 pp = unpack_varlen_hex (p, &ul);
510 if (*pp != ';')
cb91c06a 511 error (_("invalid qRelocInsn packet: %s"), buf);
dde08ee1
PA
512 from = ul;
513
514 p = pp + 1;
a9cbf802 515 unpack_varlen_hex (p, &ul);
dde08ee1
PA
516 to = ul;
517
518 org_to = to;
519
492d29ea 520 TRY
dde08ee1 521 {
f5656ead 522 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
7556d4a4 523 relocated = 1;
dde08ee1 524 }
492d29ea 525 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
526 {
527 if (ex.error == MEMORY_ERROR)
528 {
529 /* Propagate memory errors silently back to the
530 target. The stub may have limited the range of
531 addresses we can write to, for example. */
532 }
533 else
534 {
535 /* Something unexpectedly bad happened. Be verbose
536 so we can tell what, and propagate the error back
537 to the stub, so it doesn't get stuck waiting for
538 a response. */
539 exception_fprintf (gdb_stderr, ex,
540 _("warning: relocating instruction: "));
541 }
542 putpkt ("E01");
543 }
492d29ea 544 END_CATCH
7556d4a4
PA
545
546 if (relocated)
dde08ee1
PA
547 {
548 adjusted_size = to - org_to;
549
bba74b36 550 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
dde08ee1
PA
551 putpkt (buf);
552 }
dde08ee1 553 }
ad91cd99 554 else if (buf[0] == 'O' && buf[1] != 'K')
35b1e5cc
SS
555 remote_console_output (buf + 1); /* 'O' message from stub */
556 else
0df8b418 557 return buf; /* Here's the actual reply. */
35b1e5cc
SS
558 }
559 while (1);
560}
3c3bea1c 561
d01949b6
AC
562/* Handle for retreving the remote protocol data from gdbarch. */
563static struct gdbarch_data *remote_gdbarch_data_handle;
564
ea9c271d
DJ
565static struct remote_arch_state *
566get_remote_arch_state (void)
d01949b6 567{
f5656ead 568 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
d01949b6
AC
569}
570
0b83947e
DJ
571/* Fetch the global remote target state. */
572
573static struct remote_state *
574get_remote_state (void)
575{
576 /* Make sure that the remote architecture state has been
577 initialized, because doing so might reallocate rs->buf. Any
578 function which calls getpkt also needs to be mindful of changes
579 to rs->buf, but this call limits the number of places which run
580 into trouble. */
581 get_remote_arch_state ();
582
583 return get_remote_state_raw ();
584}
585
74ca34ce
DJ
586static int
587compare_pnums (const void *lhs_, const void *rhs_)
588{
589 const struct packet_reg * const *lhs = lhs_;
590 const struct packet_reg * const *rhs = rhs_;
591
592 if ((*lhs)->pnum < (*rhs)->pnum)
593 return -1;
594 else if ((*lhs)->pnum == (*rhs)->pnum)
595 return 0;
596 else
597 return 1;
598}
599
c21236dc
PA
600static int
601map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
d01949b6 602{
74ca34ce 603 int regnum, num_remote_regs, offset;
74ca34ce 604 struct packet_reg **remote_regs;
ea9c271d 605
4a22f64d 606 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
ad10f812 607 {
c21236dc 608 struct packet_reg *r = &regs[regnum];
baef701f 609
4a22f64d 610 if (register_size (gdbarch, regnum) == 0)
baef701f
DJ
611 /* Do not try to fetch zero-sized (placeholder) registers. */
612 r->pnum = -1;
613 else
614 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
615
b323314b 616 r->regnum = regnum;
74ca34ce
DJ
617 }
618
619 /* Define the g/G packet format as the contents of each register
620 with a remote protocol number, in order of ascending protocol
621 number. */
622
4a22f64d 623 remote_regs = alloca (gdbarch_num_regs (gdbarch)
c21236dc 624 * sizeof (struct packet_reg *));
f57d151a 625 for (num_remote_regs = 0, regnum = 0;
4a22f64d 626 regnum < gdbarch_num_regs (gdbarch);
f57d151a 627 regnum++)
c21236dc
PA
628 if (regs[regnum].pnum != -1)
629 remote_regs[num_remote_regs++] = &regs[regnum];
7d58c67d 630
74ca34ce
DJ
631 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
632 compare_pnums);
633
634 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
635 {
636 remote_regs[regnum]->in_g_packet = 1;
637 remote_regs[regnum]->offset = offset;
4a22f64d 638 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
ad10f812
AC
639 }
640
c21236dc
PA
641 return offset;
642}
643
644/* Given the architecture described by GDBARCH, return the remote
645 protocol register's number and the register's offset in the g/G
646 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
647 If the target does not have a mapping for REGNUM, return false,
648 otherwise, return true. */
649
650int
651remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
652 int *pnum, int *poffset)
653{
654 int sizeof_g_packet;
655 struct packet_reg *regs;
656 struct cleanup *old_chain;
657
658 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
659
660 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
661 old_chain = make_cleanup (xfree, regs);
662
663 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
664
665 *pnum = regs[regnum].pnum;
666 *poffset = regs[regnum].offset;
667
668 do_cleanups (old_chain);
669
670 return *pnum != -1;
671}
672
673static void *
674init_remote_state (struct gdbarch *gdbarch)
675{
676 struct remote_state *rs = get_remote_state_raw ();
677 struct remote_arch_state *rsa;
678
679 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
680
681 /* Use the architecture to build a regnum<->pnum table, which will be
682 1:1 unless a feature set specifies otherwise. */
683 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
684 gdbarch_num_regs (gdbarch),
685 struct packet_reg);
686
74ca34ce
DJ
687 /* Record the maximum possible size of the g packet - it may turn out
688 to be smaller. */
c21236dc 689 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
74ca34ce 690
0df8b418 691 /* Default maximum number of characters in a packet body. Many
d01949b6
AC
692 remote stubs have a hardwired buffer size of 400 bytes
693 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
694 as the maximum packet-size to ensure that the packet and an extra
695 NUL character can always fit in the buffer. This stops GDB
696 trashing stubs that try to squeeze an extra NUL into what is
ea9c271d
DJ
697 already a full buffer (As of 1999-12-04 that was most stubs). */
698 rsa->remote_packet_size = 400 - 1;
d01949b6 699
ea9c271d
DJ
700 /* This one is filled in when a ``g'' packet is received. */
701 rsa->actual_register_packet_size = 0;
702
703 /* Should rsa->sizeof_g_packet needs more space than the
0df8b418
MS
704 default, adjust the size accordingly. Remember that each byte is
705 encoded as two characters. 32 is the overhead for the packet
706 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 707 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 708 little. */
ea9c271d
DJ
709 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
710 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
802188a7 711
ea9c271d
DJ
712 /* Make sure that the packet buffer is plenty big enough for
713 this architecture. */
714 if (rs->buf_size < rsa->remote_packet_size)
715 {
716 rs->buf_size = 2 * rsa->remote_packet_size;
7fca722e 717 rs->buf = xrealloc (rs->buf, rs->buf_size);
ea9c271d 718 }
6d820c5c 719
ea9c271d
DJ
720 return rsa;
721}
722
723/* Return the current allowed size of a remote packet. This is
724 inferred from the current architecture, and should be used to
725 limit the length of outgoing packets. */
726static long
727get_remote_packet_size (void)
728{
be2a5f71 729 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
730 struct remote_arch_state *rsa = get_remote_arch_state ();
731
be2a5f71
DJ
732 if (rs->explicit_packet_size)
733 return rs->explicit_packet_size;
734
ea9c271d 735 return rsa->remote_packet_size;
d01949b6
AC
736}
737
ad10f812 738static struct packet_reg *
ea9c271d 739packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
ad10f812 740{
f5656ead 741 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
b323314b
AC
742 return NULL;
743 else
ad10f812 744 {
ea9c271d 745 struct packet_reg *r = &rsa->regs[regnum];
a744cf53 746
b323314b
AC
747 gdb_assert (r->regnum == regnum);
748 return r;
ad10f812 749 }
ad10f812
AC
750}
751
752static struct packet_reg *
ea9c271d 753packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
ad10f812 754{
b323314b 755 int i;
a744cf53 756
f5656ead 757 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
ad10f812 758 {
ea9c271d 759 struct packet_reg *r = &rsa->regs[i];
a744cf53 760
b323314b
AC
761 if (r->pnum == pnum)
762 return r;
ad10f812
AC
763 }
764 return NULL;
d01949b6
AC
765}
766
c906108c
SS
767static struct target_ops remote_ops;
768
769static struct target_ops extended_remote_ops;
770
6426a772
JM
771/* FIXME: cagney/1999-09-23: Even though getpkt was called with
772 ``forever'' still use the normal timeout mechanism. This is
773 currently used by the ASYNC code to guarentee that target reads
774 during the initial connect always time-out. Once getpkt has been
775 modified to return a timeout indication and, in turn
776 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 777 this can go away. */
6426a772
JM
778static int wait_forever_enabled_p = 1;
779
9a7071a8
JB
780/* Allow the user to specify what sequence to send to the remote
781 when he requests a program interruption: Although ^C is usually
782 what remote systems expect (this is the default, here), it is
783 sometimes preferable to send a break. On other systems such
784 as the Linux kernel, a break followed by g, which is Magic SysRq g
785 is required in order to interrupt the execution. */
786const char interrupt_sequence_control_c[] = "Ctrl-C";
787const char interrupt_sequence_break[] = "BREAK";
788const char interrupt_sequence_break_g[] = "BREAK-g";
40478521 789static const char *const interrupt_sequence_modes[] =
9a7071a8
JB
790 {
791 interrupt_sequence_control_c,
792 interrupt_sequence_break,
793 interrupt_sequence_break_g,
794 NULL
795 };
796static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
797
798static void
799show_interrupt_sequence (struct ui_file *file, int from_tty,
800 struct cmd_list_element *c,
801 const char *value)
802{
803 if (interrupt_sequence_mode == interrupt_sequence_control_c)
804 fprintf_filtered (file,
805 _("Send the ASCII ETX character (Ctrl-c) "
806 "to the remote target to interrupt the "
807 "execution of the program.\n"));
808 else if (interrupt_sequence_mode == interrupt_sequence_break)
809 fprintf_filtered (file,
810 _("send a break signal to the remote target "
811 "to interrupt the execution of the program.\n"));
812 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
813 fprintf_filtered (file,
814 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
815 "the remote target to interrupt the execution "
816 "of Linux kernel.\n"));
817 else
818 internal_error (__FILE__, __LINE__,
819 _("Invalid value for interrupt_sequence_mode: %s."),
820 interrupt_sequence_mode);
821}
6426a772 822
9a7071a8
JB
823/* This boolean variable specifies whether interrupt_sequence is sent
824 to the remote target when gdb connects to it.
825 This is mostly needed when you debug the Linux kernel: The Linux kernel
826 expects BREAK g which is Magic SysRq g for connecting gdb. */
827static int interrupt_on_connect = 0;
c906108c 828
9a7071a8
JB
829/* This variable is used to implement the "set/show remotebreak" commands.
830 Since these commands are now deprecated in favor of "set/show remote
831 interrupt-sequence", it no longer has any effect on the code. */
c906108c
SS
832static int remote_break;
833
9a7071a8
JB
834static void
835set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
836{
837 if (remote_break)
838 interrupt_sequence_mode = interrupt_sequence_break;
839 else
840 interrupt_sequence_mode = interrupt_sequence_control_c;
841}
842
843static void
844show_remotebreak (struct ui_file *file, int from_tty,
845 struct cmd_list_element *c,
846 const char *value)
847{
848}
849
c906108c
SS
850/* This variable sets the number of bits in an address that are to be
851 sent in a memory ("M" or "m") packet. Normally, after stripping
0df8b418 852 leading zeros, the entire address would be sent. This variable
c906108c
SS
853 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
854 initial implementation of remote.c restricted the address sent in
855 memory packets to ``host::sizeof long'' bytes - (typically 32
856 bits). Consequently, for 64 bit targets, the upper 32 bits of an
857 address was never sent. Since fixing this bug may cause a break in
858 some remote targets this variable is principly provided to
23860348 859 facilitate backward compatibility. */
c906108c 860
883b9c6c 861static unsigned int remote_address_size;
c906108c 862
75c99385
PA
863/* Temporary to track who currently owns the terminal. See
864 remote_terminal_* for more details. */
6426a772
JM
865
866static int remote_async_terminal_ours_p;
867
2d717e4f
DJ
868/* The executable file to use for "run" on the remote side. */
869
870static char *remote_exec_file = "";
871
11cf8741 872\f
11cf8741 873/* User configurable variables for the number of characters in a
ea9c271d
DJ
874 memory read/write packet. MIN (rsa->remote_packet_size,
875 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 876 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
877 (speed up transfers). The variables ``preferred_*'' (the user
878 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 879 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
880
881struct memory_packet_config
882{
883 char *name;
884 long size;
885 int fixed_p;
886};
887
888/* Compute the current size of a read/write packet. Since this makes
889 use of ``actual_register_packet_size'' the computation is dynamic. */
890
891static long
892get_memory_packet_size (struct memory_packet_config *config)
893{
d01949b6 894 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
895 struct remote_arch_state *rsa = get_remote_arch_state ();
896
11cf8741
JM
897 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
898 law?) that some hosts don't cope very well with large alloca()
899 calls. Eventually the alloca() code will be replaced by calls to
900 xmalloc() and make_cleanups() allowing this restriction to either
23860348 901 be lifted or removed. */
11cf8741
JM
902#ifndef MAX_REMOTE_PACKET_SIZE
903#define MAX_REMOTE_PACKET_SIZE 16384
904#endif
3de11b2e 905 /* NOTE: 20 ensures we can write at least one byte. */
11cf8741 906#ifndef MIN_REMOTE_PACKET_SIZE
3de11b2e 907#define MIN_REMOTE_PACKET_SIZE 20
11cf8741
JM
908#endif
909 long what_they_get;
910 if (config->fixed_p)
911 {
912 if (config->size <= 0)
913 what_they_get = MAX_REMOTE_PACKET_SIZE;
914 else
915 what_they_get = config->size;
916 }
917 else
918 {
ea9c271d 919 what_they_get = get_remote_packet_size ();
23860348 920 /* Limit the packet to the size specified by the user. */
11cf8741
JM
921 if (config->size > 0
922 && what_they_get > config->size)
923 what_they_get = config->size;
be2a5f71
DJ
924
925 /* Limit it to the size of the targets ``g'' response unless we have
926 permission from the stub to use a larger packet size. */
927 if (rs->explicit_packet_size == 0
928 && rsa->actual_register_packet_size > 0
929 && what_they_get > rsa->actual_register_packet_size)
930 what_they_get = rsa->actual_register_packet_size;
11cf8741
JM
931 }
932 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
933 what_they_get = MAX_REMOTE_PACKET_SIZE;
934 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
935 what_they_get = MIN_REMOTE_PACKET_SIZE;
6d820c5c
DJ
936
937 /* Make sure there is room in the global buffer for this packet
938 (including its trailing NUL byte). */
939 if (rs->buf_size < what_they_get + 1)
940 {
941 rs->buf_size = 2 * what_they_get;
942 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
943 }
944
11cf8741
JM
945 return what_they_get;
946}
947
0df8b418 948/* Update the size of a read/write packet. If they user wants
23860348 949 something really big then do a sanity check. */
11cf8741
JM
950
951static void
952set_memory_packet_size (char *args, struct memory_packet_config *config)
953{
954 int fixed_p = config->fixed_p;
955 long size = config->size;
a744cf53 956
11cf8741 957 if (args == NULL)
8a3fe4f8 958 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
959 else if (strcmp (args, "hard") == 0
960 || strcmp (args, "fixed") == 0)
961 fixed_p = 1;
962 else if (strcmp (args, "soft") == 0
963 || strcmp (args, "limit") == 0)
964 fixed_p = 0;
965 else
966 {
967 char *end;
a744cf53 968
11cf8741
JM
969 size = strtoul (args, &end, 0);
970 if (args == end)
8a3fe4f8 971 error (_("Invalid %s (bad syntax)."), config->name);
11cf8741
JM
972#if 0
973 /* Instead of explicitly capping the size of a packet to
974 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
975 instead allowed to set the size to something arbitrarily
23860348 976 large. */
11cf8741 977 if (size > MAX_REMOTE_PACKET_SIZE)
8a3fe4f8 978 error (_("Invalid %s (too large)."), config->name);
11cf8741
JM
979#endif
980 }
23860348 981 /* Extra checks? */
11cf8741
JM
982 if (fixed_p && !config->fixed_p)
983 {
e2e0b3e5
AC
984 if (! query (_("The target may not be able to correctly handle a %s\n"
985 "of %ld bytes. Change the packet size? "),
11cf8741 986 config->name, size))
8a3fe4f8 987 error (_("Packet size not changed."));
11cf8741 988 }
23860348 989 /* Update the config. */
11cf8741
JM
990 config->fixed_p = fixed_p;
991 config->size = size;
992}
993
994static void
995show_memory_packet_size (struct memory_packet_config *config)
996{
a3f17187 997 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 998 if (config->fixed_p)
a3f17187 999 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
1000 get_memory_packet_size (config));
1001 else
a3f17187 1002 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
1003 get_memory_packet_size (config));
1004}
1005
1006static struct memory_packet_config memory_write_packet_config =
1007{
1008 "memory-write-packet-size",
1009};
1010
1011static void
1012set_memory_write_packet_size (char *args, int from_tty)
1013{
1014 set_memory_packet_size (args, &memory_write_packet_config);
1015}
1016
1017static void
1018show_memory_write_packet_size (char *args, int from_tty)
1019{
1020 show_memory_packet_size (&memory_write_packet_config);
1021}
1022
1023static long
1024get_memory_write_packet_size (void)
1025{
1026 return get_memory_packet_size (&memory_write_packet_config);
1027}
1028
1029static struct memory_packet_config memory_read_packet_config =
1030{
1031 "memory-read-packet-size",
1032};
1033
1034static void
1035set_memory_read_packet_size (char *args, int from_tty)
1036{
1037 set_memory_packet_size (args, &memory_read_packet_config);
1038}
1039
1040static void
1041show_memory_read_packet_size (char *args, int from_tty)
1042{
1043 show_memory_packet_size (&memory_read_packet_config);
1044}
1045
1046static long
1047get_memory_read_packet_size (void)
1048{
1049 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1050
11cf8741
JM
1051 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1052 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1053 increased beyond this. */
1054 if (size > get_remote_packet_size ())
1055 size = get_remote_packet_size ();
11cf8741
JM
1056 return size;
1057}
1058
11cf8741 1059\f
5a2468f5 1060/* Generic configuration support for packets the stub optionally
0df8b418 1061 supports. Allows the user to specify the use of the packet as well
23860348 1062 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1063
1064enum packet_support
1065 {
1066 PACKET_SUPPORT_UNKNOWN = 0,
1067 PACKET_ENABLE,
1068 PACKET_DISABLE
1069 };
1070
5a2468f5
JM
1071struct packet_config
1072 {
bb572ddd
DJ
1073 const char *name;
1074 const char *title;
4082afcc
PA
1075
1076 /* If auto, GDB auto-detects support for this packet or feature,
1077 either through qSupported, or by trying the packet and looking
1078 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1079 packet. If false, the packet is disabled. Configs that don't
1080 have an associated command always have this set to auto. */
7f19b9a2 1081 enum auto_boolean detect;
4082afcc
PA
1082
1083 /* Does the target support this packet? */
5a2468f5
JM
1084 enum packet_support support;
1085 };
1086
d471ea57 1087/* Analyze a packet's return value and update the packet config
23860348 1088 accordingly. */
d471ea57
AC
1089
1090enum packet_result
1091{
1092 PACKET_ERROR,
1093 PACKET_OK,
1094 PACKET_UNKNOWN
1095};
1096
4082afcc
PA
1097static enum packet_support packet_config_support (struct packet_config *config);
1098static enum packet_support packet_support (int packet);
5a2468f5
JM
1099
1100static void
fba45db2 1101show_packet_config_cmd (struct packet_config *config)
5a2468f5
JM
1102{
1103 char *support = "internal-error";
a744cf53 1104
4082afcc 1105 switch (packet_config_support (config))
5a2468f5
JM
1106 {
1107 case PACKET_ENABLE:
1108 support = "enabled";
1109 break;
1110 case PACKET_DISABLE:
1111 support = "disabled";
1112 break;
1113 case PACKET_SUPPORT_UNKNOWN:
1114 support = "unknown";
1115 break;
1116 }
1117 switch (config->detect)
1118 {
7f19b9a2 1119 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1120 printf_filtered (_("Support for the `%s' packet "
1121 "is auto-detected, currently %s.\n"),
37a105a1 1122 config->name, support);
5a2468f5 1123 break;
7f19b9a2
AC
1124 case AUTO_BOOLEAN_TRUE:
1125 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1126 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1127 config->name, support);
8e248173 1128 break;
5a2468f5
JM
1129 }
1130}
1131
1132static void
bb572ddd
DJ
1133add_packet_config_cmd (struct packet_config *config, const char *name,
1134 const char *title, int legacy)
d471ea57 1135{
5a2468f5
JM
1136 char *set_doc;
1137 char *show_doc;
d471ea57 1138 char *cmd_name;
3ed07be4 1139
5a2468f5
JM
1140 config->name = name;
1141 config->title = title;
b435e160
AC
1142 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1143 name, title);
3e43a32a
MS
1144 show_doc = xstrprintf ("Show current use of remote "
1145 "protocol `%s' (%s) packet",
b435e160 1146 name, title);
d471ea57 1147 /* set/show TITLE-packet {auto,on,off} */
b435e160 1148 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1149 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1150 &config->detect, set_doc,
1151 show_doc, NULL, /* help_doc */
4082afcc 1152 NULL,
bb572ddd
DJ
1153 show_remote_protocol_packet_cmd,
1154 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1155 /* The command code copies the documentation strings. */
1156 xfree (set_doc);
1157 xfree (show_doc);
23860348 1158 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1159 if (legacy)
1160 {
1161 char *legacy_name;
a744cf53 1162
b435e160 1163 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1164 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1165 &remote_set_cmdlist);
d471ea57 1166 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1167 &remote_show_cmdlist);
d471ea57 1168 }
5a2468f5
JM
1169}
1170
d471ea57 1171static enum packet_result
a76d924d 1172packet_check_result (const char *buf)
5a2468f5 1173{
d471ea57 1174 if (buf[0] != '\0')
5a2468f5 1175 {
d471ea57 1176 /* The stub recognized the packet request. Check that the
23860348 1177 operation succeeded. */
a76d924d
DJ
1178 if (buf[0] == 'E'
1179 && isxdigit (buf[1]) && isxdigit (buf[2])
1180 && buf[3] == '\0')
1181 /* "Enn" - definitly an error. */
1182 return PACKET_ERROR;
1183
1184 /* Always treat "E." as an error. This will be used for
1185 more verbose error messages, such as E.memtypes. */
1186 if (buf[0] == 'E' && buf[1] == '.')
1187 return PACKET_ERROR;
1188
1189 /* The packet may or may not be OK. Just assume it is. */
1190 return PACKET_OK;
1191 }
1192 else
1193 /* The stub does not support the packet. */
1194 return PACKET_UNKNOWN;
1195}
1196
1197static enum packet_result
1198packet_ok (const char *buf, struct packet_config *config)
1199{
1200 enum packet_result result;
1201
4082afcc
PA
1202 if (config->detect != AUTO_BOOLEAN_TRUE
1203 && config->support == PACKET_DISABLE)
1204 internal_error (__FILE__, __LINE__,
1205 _("packet_ok: attempt to use a disabled packet"));
1206
a76d924d
DJ
1207 result = packet_check_result (buf);
1208 switch (result)
1209 {
1210 case PACKET_OK:
1211 case PACKET_ERROR:
1212 /* The stub recognized the packet request. */
4082afcc 1213 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1214 {
d471ea57
AC
1215 if (remote_debug)
1216 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1217 "Packet %s (%s) is supported\n",
1218 config->name, config->title);
d471ea57 1219 config->support = PACKET_ENABLE;
d471ea57 1220 }
a76d924d
DJ
1221 break;
1222 case PACKET_UNKNOWN:
23860348 1223 /* The stub does not support the packet. */
4082afcc
PA
1224 if (config->detect == AUTO_BOOLEAN_AUTO
1225 && config->support == PACKET_ENABLE)
d471ea57 1226 {
4082afcc
PA
1227 /* If the stub previously indicated that the packet was
1228 supported then there is a protocol error. */
1229 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1230 config->name, config->title);
1231 }
1232 else if (config->detect == AUTO_BOOLEAN_TRUE)
1233 {
1234 /* The user set it wrong. */
1235 error (_("Enabled packet %s (%s) not recognized by stub"),
1236 config->name, config->title);
d471ea57 1237 }
4082afcc
PA
1238
1239 if (remote_debug)
1240 fprintf_unfiltered (gdb_stdlog,
1241 "Packet %s (%s) is NOT supported\n",
1242 config->name, config->title);
1243 config->support = PACKET_DISABLE;
a76d924d 1244 break;
5a2468f5 1245 }
a76d924d
DJ
1246
1247 return result;
5a2468f5
JM
1248}
1249
444abaca
DJ
1250enum {
1251 PACKET_vCont = 0,
1252 PACKET_X,
1253 PACKET_qSymbol,
1254 PACKET_P,
1255 PACKET_p,
1256 PACKET_Z0,
1257 PACKET_Z1,
1258 PACKET_Z2,
1259 PACKET_Z3,
1260 PACKET_Z4,
15a201c8 1261 PACKET_vFile_setfs,
a6b151f1
DJ
1262 PACKET_vFile_open,
1263 PACKET_vFile_pread,
1264 PACKET_vFile_pwrite,
1265 PACKET_vFile_close,
1266 PACKET_vFile_unlink,
b9e7b9c3 1267 PACKET_vFile_readlink,
0a93529c 1268 PACKET_vFile_fstat,
0876f84a 1269 PACKET_qXfer_auxv,
23181151 1270 PACKET_qXfer_features,
c78fa86a 1271 PACKET_qXfer_exec_file,
cfa9d6d9 1272 PACKET_qXfer_libraries,
2268b414 1273 PACKET_qXfer_libraries_svr4,
fd79ecee 1274 PACKET_qXfer_memory_map,
0e7f50da
UW
1275 PACKET_qXfer_spu_read,
1276 PACKET_qXfer_spu_write,
07e059b5 1277 PACKET_qXfer_osdata,
dc146f7c 1278 PACKET_qXfer_threads,
0fb4aa4b 1279 PACKET_qXfer_statictrace_read,
b3b9301e 1280 PACKET_qXfer_traceframe_info,
169081d0 1281 PACKET_qXfer_uib,
711e434b 1282 PACKET_qGetTIBAddr,
444abaca 1283 PACKET_qGetTLSAddr,
be2a5f71 1284 PACKET_qSupported,
bd3eecc3 1285 PACKET_qTStatus,
89be2091 1286 PACKET_QPassSignals,
9b224c5e 1287 PACKET_QProgramSignals,
936d2992 1288 PACKET_qCRC,
08388c79 1289 PACKET_qSearch_memory,
2d717e4f
DJ
1290 PACKET_vAttach,
1291 PACKET_vRun,
a6f3e723 1292 PACKET_QStartNoAckMode,
82f73884 1293 PACKET_vKill,
4aa995e1
PA
1294 PACKET_qXfer_siginfo_read,
1295 PACKET_qXfer_siginfo_write,
0b16c5cf 1296 PACKET_qAttached,
4082afcc
PA
1297
1298 /* Support for conditional tracepoints. */
782b2b07 1299 PACKET_ConditionalTracepoints,
4082afcc
PA
1300
1301 /* Support for target-side breakpoint conditions. */
3788aec7 1302 PACKET_ConditionalBreakpoints,
4082afcc
PA
1303
1304 /* Support for target-side breakpoint commands. */
d3ce09f5 1305 PACKET_BreakpointCommands,
4082afcc
PA
1306
1307 /* Support for fast tracepoints. */
7a697b8d 1308 PACKET_FastTracepoints,
4082afcc
PA
1309
1310 /* Support for static tracepoints. */
0fb4aa4b 1311 PACKET_StaticTracepoints,
4082afcc
PA
1312
1313 /* Support for installing tracepoints while a trace experiment is
1314 running. */
1e4d1764 1315 PACKET_InstallInTrace,
4082afcc 1316
40ab02ce
MS
1317 PACKET_bc,
1318 PACKET_bs,
409873ef 1319 PACKET_TracepointSource,
d914c394 1320 PACKET_QAllow,
78d85199 1321 PACKET_qXfer_fdpic,
03583c20 1322 PACKET_QDisableRandomization,
d1feda86 1323 PACKET_QAgent,
f6f899bf 1324 PACKET_QTBuffer_size,
9accd112
MM
1325 PACKET_Qbtrace_off,
1326 PACKET_Qbtrace_bts,
b20a6524 1327 PACKET_Qbtrace_pt,
9accd112 1328 PACKET_qXfer_btrace,
4082afcc
PA
1329
1330 /* Support for the QNonStop packet. */
1331 PACKET_QNonStop,
1332
1333 /* Support for multi-process extensions. */
1334 PACKET_multiprocess_feature,
1335
1336 /* Support for enabling and disabling tracepoints while a trace
1337 experiment is running. */
1338 PACKET_EnableDisableTracepoints_feature,
1339
1340 /* Support for collecting strings using the tracenz bytecode. */
1341 PACKET_tracenz_feature,
1342
1343 /* Support for continuing to run a trace experiment while GDB is
1344 disconnected. */
1345 PACKET_DisconnectedTracing_feature,
1346
1347 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1348 PACKET_augmented_libraries_svr4_read_feature,
1349
f4abbc16
MM
1350 /* Support for the qXfer:btrace-conf:read packet. */
1351 PACKET_qXfer_btrace_conf,
1352
d33501a5
MM
1353 /* Support for the Qbtrace-conf:bts:size packet. */
1354 PACKET_Qbtrace_conf_bts_size,
1355
f7e6eed5
PA
1356 /* Support for swbreak+ feature. */
1357 PACKET_swbreak_feature,
1358
1359 /* Support for hwbreak+ feature. */
1360 PACKET_hwbreak_feature,
1361
89245bc0
DB
1362 /* Support for fork events. */
1363 PACKET_fork_event_feature,
1364
1365 /* Support for vfork events. */
1366 PACKET_vfork_event_feature,
1367
b20a6524
MM
1368 /* Support for the Qbtrace-conf:pt:size packet. */
1369 PACKET_Qbtrace_conf_pt_size,
1370
444abaca
DJ
1371 PACKET_MAX
1372};
506fb367 1373
444abaca 1374static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1375
f7e6eed5
PA
1376/* Returns the packet's corresponding "set remote foo-packet" command
1377 state. See struct packet_config for more details. */
1378
1379static enum auto_boolean
1380packet_set_cmd_state (int packet)
1381{
1382 return remote_protocol_packets[packet].detect;
1383}
1384
4082afcc
PA
1385/* Returns whether a given packet or feature is supported. This takes
1386 into account the state of the corresponding "set remote foo-packet"
1387 command, which may be used to bypass auto-detection. */
dc8acb97 1388
4082afcc
PA
1389static enum packet_support
1390packet_config_support (struct packet_config *config)
1391{
1392 switch (config->detect)
444abaca 1393 {
4082afcc
PA
1394 case AUTO_BOOLEAN_TRUE:
1395 return PACKET_ENABLE;
1396 case AUTO_BOOLEAN_FALSE:
1397 return PACKET_DISABLE;
1398 case AUTO_BOOLEAN_AUTO:
1399 return config->support;
1400 default:
1401 gdb_assert_not_reached (_("bad switch"));
444abaca 1402 }
4082afcc
PA
1403}
1404
1405/* Same as packet_config_support, but takes the packet's enum value as
1406 argument. */
1407
1408static enum packet_support
1409packet_support (int packet)
1410{
1411 struct packet_config *config = &remote_protocol_packets[packet];
1412
1413 return packet_config_support (config);
dc8acb97
MS
1414}
1415
5a2468f5 1416static void
444abaca
DJ
1417show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1418 struct cmd_list_element *c,
1419 const char *value)
5a2468f5 1420{
444abaca 1421 struct packet_config *packet;
5a2468f5 1422
444abaca
DJ
1423 for (packet = remote_protocol_packets;
1424 packet < &remote_protocol_packets[PACKET_MAX];
1425 packet++)
1426 {
1427 if (&packet->detect == c->var)
1428 {
1429 show_packet_config_cmd (packet);
1430 return;
1431 }
1432 }
9b20d036 1433 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1434 c->name);
5a2468f5
JM
1435}
1436
d471ea57
AC
1437/* Should we try one of the 'Z' requests? */
1438
1439enum Z_packet_type
1440{
1441 Z_PACKET_SOFTWARE_BP,
1442 Z_PACKET_HARDWARE_BP,
1443 Z_PACKET_WRITE_WP,
1444 Z_PACKET_READ_WP,
1445 Z_PACKET_ACCESS_WP,
1446 NR_Z_PACKET_TYPES
1447};
96baa820 1448
d471ea57 1449/* For compatibility with older distributions. Provide a ``set remote
23860348 1450 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1451
7f19b9a2 1452static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1453
1454static void
fba45db2
KB
1455set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1456 struct cmd_list_element *c)
96baa820 1457{
d471ea57 1458 int i;
a744cf53 1459
d471ea57 1460 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1461 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1462}
1463
1464static void
08546159
AC
1465show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1466 struct cmd_list_element *c,
1467 const char *value)
96baa820 1468{
d471ea57 1469 int i;
a744cf53 1470
d471ea57
AC
1471 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1472 {
444abaca 1473 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1474 }
96baa820
JM
1475}
1476
4082afcc
PA
1477/* Returns true if the multi-process extensions are in effect. */
1478
1479static int
1480remote_multi_process_p (struct remote_state *rs)
1481{
1482 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1483}
1484
de0d863e
DB
1485/* Returns true if fork events are supported. */
1486
1487static int
1488remote_fork_event_p (struct remote_state *rs)
1489{
1490 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1491}
1492
c269dbdb
DB
1493/* Returns true if vfork events are supported. */
1494
1495static int
1496remote_vfork_event_p (struct remote_state *rs)
1497{
1498 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1499}
1500
cbb8991c
DB
1501/* Insert fork catchpoint target routine. If fork events are enabled
1502 then return success, nothing more to do. */
1503
1504static int
1505remote_insert_fork_catchpoint (struct target_ops *ops, int pid)
1506{
1507 struct remote_state *rs = get_remote_state ();
1508
1509 return !remote_fork_event_p (rs);
1510}
1511
1512/* Remove fork catchpoint target routine. Nothing to do, just
1513 return success. */
1514
1515static int
1516remote_remove_fork_catchpoint (struct target_ops *ops, int pid)
1517{
1518 return 0;
1519}
1520
1521/* Insert vfork catchpoint target routine. If vfork events are enabled
1522 then return success, nothing more to do. */
1523
1524static int
1525remote_insert_vfork_catchpoint (struct target_ops *ops, int pid)
1526{
1527 struct remote_state *rs = get_remote_state ();
1528
1529 return !remote_vfork_event_p (rs);
1530}
1531
1532/* Remove vfork catchpoint target routine. Nothing to do, just
1533 return success. */
1534
1535static int
1536remote_remove_vfork_catchpoint (struct target_ops *ops, int pid)
1537{
1538 return 0;
1539}
1540
23860348 1541/* Tokens for use by the asynchronous signal handlers for SIGINT. */
934b9bac
JK
1542static struct async_signal_handler *async_sigint_remote_twice_token;
1543static struct async_signal_handler *async_sigint_remote_token;
43ff13b4 1544
74531fed
PA
1545\f
1546/* Asynchronous signal handle registered as event loop source for
1547 when we have pending events ready to be passed to the core. */
1548
1549static struct async_event_handler *remote_async_inferior_event_token;
1550
c906108c
SS
1551\f
1552
79d7f229
PA
1553static ptid_t magic_null_ptid;
1554static ptid_t not_sent_ptid;
1555static ptid_t any_thread_ptid;
1556
0b16c5cf
PA
1557/* Find out if the stub attached to PID (and hence GDB should offer to
1558 detach instead of killing it when bailing out). */
1559
1560static int
1561remote_query_attached (int pid)
1562{
1563 struct remote_state *rs = get_remote_state ();
bba74b36 1564 size_t size = get_remote_packet_size ();
0b16c5cf 1565
4082afcc 1566 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
1567 return 0;
1568
1569 if (remote_multi_process_p (rs))
bba74b36 1570 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 1571 else
bba74b36 1572 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
1573
1574 putpkt (rs->buf);
1575 getpkt (&rs->buf, &rs->buf_size, 0);
1576
1577 switch (packet_ok (rs->buf,
1554e9be 1578 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
1579 {
1580 case PACKET_OK:
1581 if (strcmp (rs->buf, "1") == 0)
1582 return 1;
1583 break;
1584 case PACKET_ERROR:
1585 warning (_("Remote failure reply: %s"), rs->buf);
1586 break;
1587 case PACKET_UNKNOWN:
1588 break;
1589 }
1590
1591 return 0;
1592}
1593
49c62f2e
PA
1594/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1595 has been invented by GDB, instead of reported by the target. Since
1596 we can be connected to a remote system before before knowing about
1597 any inferior, mark the target with execution when we find the first
1598 inferior. If ATTACHED is 1, then we had just attached to this
1599 inferior. If it is 0, then we just created this inferior. If it
1600 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
1601 attached to the inferior or not. If TRY_OPEN_EXEC is true then
1602 attempt to open this inferior's executable as the main executable
1603 if no main executable is open already. */
1941c569
PA
1604
1605static struct inferior *
1b6e6f5c
GB
1606remote_add_inferior (int fake_pid_p, int pid, int attached,
1607 int try_open_exec)
1941c569 1608{
1941c569
PA
1609 struct inferior *inf;
1610
0b16c5cf
PA
1611 /* Check whether this process we're learning about is to be
1612 considered attached, or if is to be considered to have been
1613 spawned by the stub. */
1614 if (attached == -1)
1615 attached = remote_query_attached (pid);
1616
f5656ead 1617 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
1618 {
1619 /* If the target shares code across all inferiors, then every
1620 attach adds a new inferior. */
1621 inf = add_inferior (pid);
1622
1623 /* ... and every inferior is bound to the same program space.
1624 However, each inferior may still have its own address
1625 space. */
1626 inf->aspace = maybe_new_address_space ();
1627 inf->pspace = current_program_space;
1628 }
1629 else
1630 {
1631 /* In the traditional debugging scenario, there's a 1-1 match
1632 between program/address spaces. We simply bind the inferior
1633 to the program space's address space. */
1634 inf = current_inferior ();
1635 inferior_appeared (inf, pid);
1636 }
1941c569 1637
0b16c5cf 1638 inf->attach_flag = attached;
49c62f2e 1639 inf->fake_pid_p = fake_pid_p;
0b16c5cf 1640
1b6e6f5c
GB
1641 /* If no main executable is currently open then attempt to
1642 open the file that was executed to create this inferior. */
835205d0 1643 if (try_open_exec && get_exec_file (0) == NULL)
1b6e6f5c
GB
1644 exec_file_locate_attach (pid, 1);
1645
1941c569
PA
1646 return inf;
1647}
1648
1649/* Add thread PTID to GDB's thread list. Tag it as executing/running
1650 according to RUNNING. */
1651
c906108c 1652static void
1941c569 1653remote_add_thread (ptid_t ptid, int running)
c906108c 1654{
b7ea362b
PA
1655 struct remote_state *rs = get_remote_state ();
1656
1657 /* GDB historically didn't pull threads in the initial connection
1658 setup. If the remote target doesn't even have a concept of
1659 threads (e.g., a bare-metal target), even if internally we
1660 consider that a single-threaded target, mentioning a new thread
1661 might be confusing to the user. Be silent then, preserving the
1662 age old behavior. */
1663 if (rs->starting_up)
1664 add_thread_silent (ptid);
1665 else
1666 add_thread (ptid);
1941c569
PA
1667
1668 set_executing (ptid, running);
1669 set_running (ptid, running);
1670}
1671
1672/* Come here when we learn about a thread id from the remote target.
1673 It may be the first time we hear about such thread, so take the
1674 opportunity to add it to GDB's thread list. In case this is the
1675 first time we're noticing its corresponding inferior, add it to
1676 GDB's inferior list as well. */
1677
1678static void
1679remote_notice_new_inferior (ptid_t currthread, int running)
1680{
c906108c
SS
1681 /* If this is a new thread, add it to GDB's thread list.
1682 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
1683
1684 if (in_thread_list (currthread) && is_exited (currthread))
1685 {
1686 /* We're seeing an event on a thread id we knew had exited.
1687 This has to be a new thread reusing the old id. Add it. */
1941c569 1688 remote_add_thread (currthread, running);
82f73884
PA
1689 return;
1690 }
1691
79d7f229 1692 if (!in_thread_list (currthread))
c0a2216e 1693 {
1941c569 1694 struct inferior *inf = NULL;
bad34192 1695 int pid = ptid_get_pid (currthread);
1941c569 1696
bad34192
PA
1697 if (ptid_is_pid (inferior_ptid)
1698 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
1699 {
1700 /* inferior_ptid has no thread member yet. This can happen
1701 with the vAttach -> remote_wait,"TAAthread:" path if the
1702 stub doesn't support qC. This is the first stop reported
1703 after an attach, so this is the main thread. Update the
1704 ptid in the thread list. */
bad34192
PA
1705 if (in_thread_list (pid_to_ptid (pid)))
1706 thread_change_ptid (inferior_ptid, currthread);
1707 else
1708 {
1709 remote_add_thread (currthread, running);
1710 inferior_ptid = currthread;
1711 }
dc146f7c 1712 return;
c0a2216e 1713 }
82f73884
PA
1714
1715 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
1716 {
1717 /* inferior_ptid is not set yet. This can happen with the
1718 vRun -> remote_wait,"TAAthread:" path if the stub
1719 doesn't support qC. This is the first stop reported
1720 after an attach, so this is the main thread. Update the
1721 ptid in the thread list. */
dc146f7c 1722 thread_change_ptid (inferior_ptid, currthread);
82f73884 1723 return;
c0a2216e 1724 }
82f73884 1725
29c87f7f
PA
1726 /* When connecting to a target remote, or to a target
1727 extended-remote which already was debugging an inferior, we
1728 may not know about it yet. Add it before adding its child
1729 thread, so notifications are emitted in a sensible order. */
1730 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
1731 {
1732 struct remote_state *rs = get_remote_state ();
1733 int fake_pid_p = !remote_multi_process_p (rs);
1734
1735 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 1736 ptid_get_pid (currthread), -1, 1);
49c62f2e 1737 }
29c87f7f 1738
82f73884 1739 /* This is really a new thread. Add it. */
1941c569
PA
1740 remote_add_thread (currthread, running);
1741
1742 /* If we found a new inferior, let the common code do whatever
1743 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
1744 breakpoints), unless we're just setting up an all-stop
1745 connection. */
1941c569 1746 if (inf != NULL)
b7ea362b
PA
1747 {
1748 struct remote_state *rs = get_remote_state ();
1749
1750 if (non_stop || !rs->starting_up)
1751 notice_new_inferior (currthread, running, 0);
1752 }
c0a2216e 1753 }
c906108c
SS
1754}
1755
dc146f7c
VP
1756/* Return the private thread data, creating it if necessary. */
1757
70221824 1758static struct private_thread_info *
dc146f7c
VP
1759demand_private_info (ptid_t ptid)
1760{
1761 struct thread_info *info = find_thread_ptid (ptid);
1762
1763 gdb_assert (info);
1764
fe978cb0 1765 if (!info->priv)
dc146f7c 1766 {
fe978cb0 1767 info->priv = xmalloc (sizeof (*(info->priv)));
dc146f7c 1768 info->private_dtor = free_private_thread_info;
fe978cb0
PA
1769 info->priv->core = -1;
1770 info->priv->extra = 0;
dc146f7c
VP
1771 }
1772
fe978cb0 1773 return info->priv;
dc146f7c
VP
1774}
1775
74531fed
PA
1776/* Call this function as a result of
1777 1) A halt indication (T packet) containing a thread id
1778 2) A direct query of currthread
0df8b418 1779 3) Successful execution of set thread */
74531fed
PA
1780
1781static void
47f8a51d 1782record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 1783{
47f8a51d 1784 rs->general_thread = currthread;
74531fed
PA
1785}
1786
89be2091
DJ
1787/* If 'QPassSignals' is supported, tell the remote stub what signals
1788 it can simply pass through to the inferior without reporting. */
1789
1790static void
94bedb42
TT
1791remote_pass_signals (struct target_ops *self,
1792 int numsigs, unsigned char *pass_signals)
89be2091 1793{
4082afcc 1794 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
1795 {
1796 char *pass_packet, *p;
89be2091 1797 int count = 0, i;
747dc59d 1798 struct remote_state *rs = get_remote_state ();
89be2091
DJ
1799
1800 gdb_assert (numsigs < 256);
1801 for (i = 0; i < numsigs; i++)
1802 {
2455069d 1803 if (pass_signals[i])
89be2091
DJ
1804 count++;
1805 }
1806 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1807 strcpy (pass_packet, "QPassSignals:");
1808 p = pass_packet + strlen (pass_packet);
1809 for (i = 0; i < numsigs; i++)
1810 {
2455069d 1811 if (pass_signals[i])
89be2091
DJ
1812 {
1813 if (i >= 16)
1814 *p++ = tohex (i >> 4);
1815 *p++ = tohex (i & 15);
1816 if (count)
1817 *p++ = ';';
1818 else
1819 break;
1820 count--;
1821 }
1822 }
1823 *p = 0;
747dc59d 1824 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 1825 {
89be2091
DJ
1826 putpkt (pass_packet);
1827 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1828 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
1829 if (rs->last_pass_packet)
1830 xfree (rs->last_pass_packet);
1831 rs->last_pass_packet = pass_packet;
89be2091
DJ
1832 }
1833 else
1834 xfree (pass_packet);
1835 }
1836}
1837
9b224c5e
PA
1838/* If 'QProgramSignals' is supported, tell the remote stub what
1839 signals it should pass through to the inferior when detaching. */
1840
1841static void
daf5e9b6
TT
1842remote_program_signals (struct target_ops *self,
1843 int numsigs, unsigned char *signals)
9b224c5e 1844{
4082afcc 1845 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
1846 {
1847 char *packet, *p;
1848 int count = 0, i;
5e4a05c4 1849 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
1850
1851 gdb_assert (numsigs < 256);
1852 for (i = 0; i < numsigs; i++)
1853 {
1854 if (signals[i])
1855 count++;
1856 }
1857 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1858 strcpy (packet, "QProgramSignals:");
1859 p = packet + strlen (packet);
1860 for (i = 0; i < numsigs; i++)
1861 {
1862 if (signal_pass_state (i))
1863 {
1864 if (i >= 16)
1865 *p++ = tohex (i >> 4);
1866 *p++ = tohex (i & 15);
1867 if (count)
1868 *p++ = ';';
1869 else
1870 break;
1871 count--;
1872 }
1873 }
1874 *p = 0;
5e4a05c4
TT
1875 if (!rs->last_program_signals_packet
1876 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 1877 {
9b224c5e
PA
1878 putpkt (packet);
1879 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1880 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
1881 xfree (rs->last_program_signals_packet);
1882 rs->last_program_signals_packet = packet;
9b224c5e
PA
1883 }
1884 else
1885 xfree (packet);
1886 }
1887}
1888
79d7f229
PA
1889/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1890 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1891 thread. If GEN is set, set the general thread, if not, then set
1892 the step/continue thread. */
c906108c 1893static void
79d7f229 1894set_thread (struct ptid ptid, int gen)
c906108c 1895{
d01949b6 1896 struct remote_state *rs = get_remote_state ();
47f8a51d 1897 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 1898 char *buf = rs->buf;
79d7f229 1899 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 1900
79d7f229 1901 if (ptid_equal (state, ptid))
c906108c
SS
1902 return;
1903
79d7f229
PA
1904 *buf++ = 'H';
1905 *buf++ = gen ? 'g' : 'c';
1906 if (ptid_equal (ptid, magic_null_ptid))
1907 xsnprintf (buf, endbuf - buf, "0");
1908 else if (ptid_equal (ptid, any_thread_ptid))
1909 xsnprintf (buf, endbuf - buf, "0");
1910 else if (ptid_equal (ptid, minus_one_ptid))
1911 xsnprintf (buf, endbuf - buf, "-1");
1912 else
82f73884 1913 write_ptid (buf, endbuf, ptid);
79d7f229 1914 putpkt (rs->buf);
6d820c5c 1915 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 1916 if (gen)
47f8a51d 1917 rs->general_thread = ptid;
c906108c 1918 else
47f8a51d 1919 rs->continue_thread = ptid;
c906108c 1920}
79d7f229
PA
1921
1922static void
1923set_general_thread (struct ptid ptid)
1924{
1925 set_thread (ptid, 1);
1926}
1927
1928static void
1929set_continue_thread (struct ptid ptid)
1930{
1931 set_thread (ptid, 0);
1932}
1933
3c9c4b83
PA
1934/* Change the remote current process. Which thread within the process
1935 ends up selected isn't important, as long as it is the same process
1936 as what INFERIOR_PTID points to.
1937
1938 This comes from that fact that there is no explicit notion of
1939 "selected process" in the protocol. The selected process for
1940 general operations is the process the selected general thread
1941 belongs to. */
1942
1943static void
1944set_general_process (void)
1945{
1946 struct remote_state *rs = get_remote_state ();
1947
1948 /* If the remote can't handle multiple processes, don't bother. */
901f9912 1949 if (!rs->extended || !remote_multi_process_p (rs))
3c9c4b83
PA
1950 return;
1951
1952 /* We only need to change the remote current thread if it's pointing
1953 at some other process. */
47f8a51d 1954 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
1955 set_general_thread (inferior_ptid);
1956}
1957
c906108c 1958\f
7d1a114c
PA
1959/* Return nonzero if this is the main thread that we made up ourselves
1960 to model non-threaded targets as single-threaded. */
c906108c
SS
1961
1962static int
7d1a114c 1963remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
c906108c 1964{
6d820c5c 1965 struct remote_state *rs = get_remote_state ();
82f73884 1966 char *p, *endp;
c906108c 1967
c0a2216e
PA
1968 if (ptid_equal (ptid, magic_null_ptid))
1969 /* The main thread is always alive. */
1970 return 1;
1971
ba348170 1972 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
1973 /* The main thread is always alive. This can happen after a
1974 vAttach, if the remote side doesn't support
1975 multi-threading. */
1976 return 1;
1977
7d1a114c
PA
1978 return 0;
1979}
1980
1981/* Return nonzero if the thread PTID is still alive on the remote
1982 system. */
1983
1984static int
1985remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1986{
1987 struct remote_state *rs = get_remote_state ();
1988 char *p, *endp;
1989
1990 /* Check if this is a thread that we made up ourselves to model
1991 non-threaded targets as single-threaded. */
1992 if (remote_thread_always_alive (ops, ptid))
1993 return 1;
1994
82f73884
PA
1995 p = rs->buf;
1996 endp = rs->buf + get_remote_packet_size ();
1997
1998 *p++ = 'T';
1999 write_ptid (p, endp, ptid);
2000
2e9f7625 2001 putpkt (rs->buf);
6d820c5c 2002 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2003 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
2004}
2005
2006/* About these extended threadlist and threadinfo packets. They are
2007 variable length packets but, the fields within them are often fixed
2008 length. They are redundent enough to send over UDP as is the
2009 remote protocol in general. There is a matching unit test module
2010 in libstub. */
2011
23860348 2012/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 2013 libstub protocol encoding, and remote.c. It is not particularly
23860348 2014 changable. */
cce74817
JM
2015
2016/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 2017 Plan to fix this. */
cce74817 2018
23860348 2019typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 2020
9d1f7ab2 2021/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 2022 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
2023
2024struct gdb_ext_thread_info
c5aa993b 2025 {
23860348 2026 threadref threadid; /* External form of thread reference. */
2bc416ba 2027 int active; /* Has state interesting to GDB?
23860348 2028 regs, stack. */
2bc416ba 2029 char display[256]; /* Brief state display, name,
cedea757 2030 blocked/suspended. */
23860348 2031 char shortname[32]; /* To be used to name threads. */
2bc416ba 2032 char more_display[256]; /* Long info, statistics, queue depth,
23860348 2033 whatever. */
c5aa993b 2034 };
cce74817
JM
2035
2036/* The volume of remote transfers can be limited by submitting
2037 a mask containing bits specifying the desired information.
2038 Use a union of these values as the 'selection' parameter to
0df8b418 2039 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
2040
2041#define TAG_THREADID 1
2042#define TAG_EXISTS 2
2043#define TAG_DISPLAY 4
2044#define TAG_THREADNAME 8
c5aa993b 2045#define TAG_MOREDISPLAY 16
cce74817 2046
23860348 2047#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 2048
a14ed312 2049static char *unpack_nibble (char *buf, int *val);
cce74817 2050
a14ed312 2051static char *unpack_byte (char *buf, int *value);
cce74817 2052
a14ed312 2053static char *pack_int (char *buf, int value);
cce74817 2054
a14ed312 2055static char *unpack_int (char *buf, int *value);
cce74817 2056
a14ed312 2057static char *unpack_string (char *src, char *dest, int length);
cce74817 2058
23860348 2059static char *pack_threadid (char *pkt, threadref *id);
cce74817 2060
23860348 2061static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 2062
23860348 2063void int_to_threadref (threadref *id, int value);
cce74817 2064
23860348 2065static int threadref_to_int (threadref *ref);
cce74817 2066
23860348 2067static void copy_threadref (threadref *dest, threadref *src);
cce74817 2068
23860348 2069static int threadmatch (threadref *dest, threadref *src);
cce74817 2070
2bc416ba 2071static char *pack_threadinfo_request (char *pkt, int mode,
23860348 2072 threadref *id);
cce74817 2073
a14ed312 2074static int remote_unpack_thread_info_response (char *pkt,
23860348 2075 threadref *expectedref,
a14ed312
KB
2076 struct gdb_ext_thread_info
2077 *info);
cce74817
JM
2078
2079
2bc416ba 2080static int remote_get_threadinfo (threadref *threadid,
23860348 2081 int fieldset, /*TAG mask */
a14ed312 2082 struct gdb_ext_thread_info *info);
cce74817 2083
a14ed312
KB
2084static char *pack_threadlist_request (char *pkt, int startflag,
2085 int threadcount,
23860348 2086 threadref *nextthread);
cce74817 2087
a14ed312
KB
2088static int parse_threadlist_response (char *pkt,
2089 int result_limit,
23860348 2090 threadref *original_echo,
2bc416ba 2091 threadref *resultlist,
23860348 2092 int *doneflag);
cce74817 2093
a14ed312 2094static int remote_get_threadlist (int startflag,
23860348 2095 threadref *nextthread,
a14ed312
KB
2096 int result_limit,
2097 int *done,
2bc416ba 2098 int *result_count,
23860348 2099 threadref *threadlist);
cce74817 2100
23860348 2101typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2102
a14ed312
KB
2103static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2104 void *context, int looplimit);
cce74817 2105
23860348 2106static int remote_newthread_step (threadref *ref, void *context);
cce74817 2107
82f73884
PA
2108
2109/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2110 buffer we're allowed to write to. Returns
2111 BUF+CHARACTERS_WRITTEN. */
2112
2113static char *
2114write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2115{
2116 int pid, tid;
2117 struct remote_state *rs = get_remote_state ();
2118
2119 if (remote_multi_process_p (rs))
2120 {
2121 pid = ptid_get_pid (ptid);
2122 if (pid < 0)
2123 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2124 else
2125 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2126 }
ba348170 2127 tid = ptid_get_lwp (ptid);
82f73884
PA
2128 if (tid < 0)
2129 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2130 else
2131 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2132
2133 return buf;
2134}
2135
2136/* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2137 passed the last parsed char. Returns null_ptid on error. */
2138
2139static ptid_t
2140read_ptid (char *buf, char **obuf)
2141{
2142 char *p = buf;
2143 char *pp;
2144 ULONGEST pid = 0, tid = 0;
82f73884
PA
2145
2146 if (*p == 'p')
2147 {
2148 /* Multi-process ptid. */
2149 pp = unpack_varlen_hex (p + 1, &pid);
2150 if (*pp != '.')
b37520b6 2151 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2152
2153 p = pp;
2154 pp = unpack_varlen_hex (p + 1, &tid);
2155 if (obuf)
2156 *obuf = pp;
ba348170 2157 return ptid_build (pid, tid, 0);
82f73884
PA
2158 }
2159
2160 /* No multi-process. Just a tid. */
2161 pp = unpack_varlen_hex (p, &tid);
2162
c9f35b34
KB
2163 /* Return null_ptid when no thread id is found. */
2164 if (p == pp)
2165 {
2166 if (obuf)
2167 *obuf = pp;
2168 return null_ptid;
2169 }
2170
82f73884 2171 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2172 what's in inferior_ptid, unless it's null at this point. If so,
2173 then since there's no way to know the pid of the reported
2174 threads, use the magic number. */
2175 if (ptid_equal (inferior_ptid, null_ptid))
2176 pid = ptid_get_pid (magic_null_ptid);
2177 else
2178 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2179
2180 if (obuf)
2181 *obuf = pp;
ba348170 2182 return ptid_build (pid, tid, 0);
82f73884
PA
2183}
2184
c906108c 2185static int
fba45db2 2186stubhex (int ch)
c906108c
SS
2187{
2188 if (ch >= 'a' && ch <= 'f')
2189 return ch - 'a' + 10;
2190 if (ch >= '0' && ch <= '9')
2191 return ch - '0';
2192 if (ch >= 'A' && ch <= 'F')
2193 return ch - 'A' + 10;
2194 return -1;
2195}
2196
2197static int
fba45db2 2198stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2199{
2200 int nibble;
2201 int retval = 0;
2202
2203 while (fieldlength)
2204 {
2205 nibble = stubhex (*buff++);
2206 retval |= nibble;
2207 fieldlength--;
2208 if (fieldlength)
2209 retval = retval << 4;
2210 }
2211 return retval;
2212}
2213
c906108c 2214static char *
fba45db2 2215unpack_nibble (char *buf, int *val)
c906108c 2216{
b7589f7d 2217 *val = fromhex (*buf++);
c906108c
SS
2218 return buf;
2219}
2220
c906108c 2221static char *
fba45db2 2222unpack_byte (char *buf, int *value)
c906108c
SS
2223{
2224 *value = stub_unpack_int (buf, 2);
2225 return buf + 2;
2226}
2227
2228static char *
fba45db2 2229pack_int (char *buf, int value)
c906108c
SS
2230{
2231 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2232 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2233 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2234 buf = pack_hex_byte (buf, (value & 0xff));
2235 return buf;
2236}
2237
2238static char *
fba45db2 2239unpack_int (char *buf, int *value)
c906108c
SS
2240{
2241 *value = stub_unpack_int (buf, 8);
2242 return buf + 8;
2243}
2244
23860348 2245#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2246static char *pack_string (char *pkt, char *string);
c906108c
SS
2247
2248static char *
fba45db2 2249pack_string (char *pkt, char *string)
c906108c
SS
2250{
2251 char ch;
2252 int len;
2253
2254 len = strlen (string);
2255 if (len > 200)
23860348 2256 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2257 pkt = pack_hex_byte (pkt, len);
2258 while (len-- > 0)
2259 {
2260 ch = *string++;
2261 if ((ch == '\0') || (ch == '#'))
23860348 2262 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2263 *pkt++ = ch;
2264 }
2265 return pkt;
2266}
2267#endif /* 0 (unused) */
2268
2269static char *
fba45db2 2270unpack_string (char *src, char *dest, int length)
c906108c
SS
2271{
2272 while (length--)
2273 *dest++ = *src++;
2274 *dest = '\0';
2275 return src;
2276}
2277
2278static char *
fba45db2 2279pack_threadid (char *pkt, threadref *id)
c906108c
SS
2280{
2281 char *limit;
2282 unsigned char *altid;
2283
2284 altid = (unsigned char *) id;
2285 limit = pkt + BUF_THREAD_ID_SIZE;
2286 while (pkt < limit)
2287 pkt = pack_hex_byte (pkt, *altid++);
2288 return pkt;
2289}
2290
2291
2292static char *
fba45db2 2293unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2294{
2295 char *altref;
2296 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2297 int x, y;
2298
2299 altref = (char *) id;
2300
2301 while (inbuf < limit)
2302 {
2303 x = stubhex (*inbuf++);
2304 y = stubhex (*inbuf++);
2305 *altref++ = (x << 4) | y;
2306 }
2307 return inbuf;
2308}
2309
2310/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2311 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2312 to use 64bit thread references internally. This is an adapter
2313 function. */
2314
2315void
fba45db2 2316int_to_threadref (threadref *id, int value)
c906108c
SS
2317{
2318 unsigned char *scan;
2319
2320 scan = (unsigned char *) id;
2321 {
2322 int i = 4;
2323 while (i--)
2324 *scan++ = 0;
2325 }
2326 *scan++ = (value >> 24) & 0xff;
2327 *scan++ = (value >> 16) & 0xff;
2328 *scan++ = (value >> 8) & 0xff;
2329 *scan++ = (value & 0xff);
2330}
2331
2332static int
fba45db2 2333threadref_to_int (threadref *ref)
c906108c
SS
2334{
2335 int i, value = 0;
2336 unsigned char *scan;
2337
cfd77fa1 2338 scan = *ref;
c906108c
SS
2339 scan += 4;
2340 i = 4;
2341 while (i-- > 0)
2342 value = (value << 8) | ((*scan++) & 0xff);
2343 return value;
2344}
2345
2346static void
fba45db2 2347copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2348{
2349 int i;
2350 unsigned char *csrc, *cdest;
2351
2352 csrc = (unsigned char *) src;
2353 cdest = (unsigned char *) dest;
2354 i = 8;
2355 while (i--)
2356 *cdest++ = *csrc++;
2357}
2358
2359static int
fba45db2 2360threadmatch (threadref *dest, threadref *src)
c906108c 2361{
23860348 2362 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2363#if 0
2364 unsigned char *srcp, *destp;
2365 int i, result;
2366 srcp = (char *) src;
2367 destp = (char *) dest;
2368
2369 result = 1;
2370 while (i-- > 0)
2371 result &= (*srcp++ == *destp++) ? 1 : 0;
2372 return result;
2373#endif
2374 return 1;
2375}
2376
2377/*
c5aa993b
JM
2378 threadid:1, # always request threadid
2379 context_exists:2,
2380 display:4,
2381 unique_name:8,
2382 more_display:16
2383 */
c906108c
SS
2384
2385/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2386
2387static char *
fba45db2 2388pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2389{
23860348
MS
2390 *pkt++ = 'q'; /* Info Query */
2391 *pkt++ = 'P'; /* process or thread info */
2392 pkt = pack_int (pkt, mode); /* mode */
c906108c 2393 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2394 *pkt = '\0'; /* terminate */
c906108c
SS
2395 return pkt;
2396}
2397
23860348 2398/* These values tag the fields in a thread info response packet. */
c906108c 2399/* Tagging the fields allows us to request specific fields and to
23860348 2400 add more fields as time goes by. */
c906108c 2401
23860348 2402#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2403#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2404 fetch registers and its stack? */
c5aa993b 2405#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2406#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2407#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2408 the process. */
c906108c
SS
2409
2410static int
fba45db2
KB
2411remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2412 struct gdb_ext_thread_info *info)
c906108c 2413{
d01949b6 2414 struct remote_state *rs = get_remote_state ();
c906108c 2415 int mask, length;
cfd77fa1 2416 int tag;
c906108c 2417 threadref ref;
6d820c5c 2418 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
2419 int retval = 1;
2420
23860348 2421 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
2422 info->active = 0;
2423 info->display[0] = '\0';
2424 info->shortname[0] = '\0';
2425 info->more_display[0] = '\0';
2426
23860348
MS
2427 /* Assume the characters indicating the packet type have been
2428 stripped. */
c906108c
SS
2429 pkt = unpack_int (pkt, &mask); /* arg mask */
2430 pkt = unpack_threadid (pkt, &ref);
2431
2432 if (mask == 0)
8a3fe4f8 2433 warning (_("Incomplete response to threadinfo request."));
c906108c 2434 if (!threadmatch (&ref, expectedref))
23860348 2435 { /* This is an answer to a different request. */
8a3fe4f8 2436 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
2437 return 0;
2438 }
2439 copy_threadref (&info->threadid, &ref);
2440
23860348 2441 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 2442
23860348
MS
2443 /* Packets are terminated with nulls. */
2444 while ((pkt < limit) && mask && *pkt)
c906108c
SS
2445 {
2446 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
2447 pkt = unpack_byte (pkt, &length); /* length */
2448 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 2449 {
8a3fe4f8 2450 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
2451 retval = 0;
2452 break;
2453 }
2454 if (tag == TAG_THREADID)
2455 {
2456 if (length != 16)
2457 {
8a3fe4f8 2458 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
2459 retval = 0;
2460 break;
2461 }
2462 pkt = unpack_threadid (pkt, &ref);
2463 mask = mask & ~TAG_THREADID;
2464 continue;
2465 }
2466 if (tag == TAG_EXISTS)
2467 {
2468 info->active = stub_unpack_int (pkt, length);
2469 pkt += length;
2470 mask = mask & ~(TAG_EXISTS);
2471 if (length > 8)
2472 {
8a3fe4f8 2473 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
2474 retval = 0;
2475 break;
2476 }
2477 continue;
2478 }
2479 if (tag == TAG_THREADNAME)
2480 {
2481 pkt = unpack_string (pkt, &info->shortname[0], length);
2482 mask = mask & ~TAG_THREADNAME;
2483 continue;
2484 }
2485 if (tag == TAG_DISPLAY)
2486 {
2487 pkt = unpack_string (pkt, &info->display[0], length);
2488 mask = mask & ~TAG_DISPLAY;
2489 continue;
2490 }
2491 if (tag == TAG_MOREDISPLAY)
2492 {
2493 pkt = unpack_string (pkt, &info->more_display[0], length);
2494 mask = mask & ~TAG_MOREDISPLAY;
2495 continue;
2496 }
8a3fe4f8 2497 warning (_("ERROR RMT: unknown thread info tag."));
23860348 2498 break; /* Not a tag we know about. */
c906108c
SS
2499 }
2500 return retval;
2501}
2502
2503static int
fba45db2
KB
2504remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2505 struct gdb_ext_thread_info *info)
c906108c 2506{
d01949b6 2507 struct remote_state *rs = get_remote_state ();
c906108c 2508 int result;
c906108c 2509
2e9f7625
DJ
2510 pack_threadinfo_request (rs->buf, fieldset, threadid);
2511 putpkt (rs->buf);
6d820c5c 2512 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
2513
2514 if (rs->buf[0] == '\0')
2515 return 0;
2516
2e9f7625 2517 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 2518 threadid, info);
c906108c
SS
2519 return result;
2520}
2521
c906108c
SS
2522/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2523
2524static char *
fba45db2
KB
2525pack_threadlist_request (char *pkt, int startflag, int threadcount,
2526 threadref *nextthread)
c906108c
SS
2527{
2528 *pkt++ = 'q'; /* info query packet */
2529 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 2530 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
2531 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2532 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2533 *pkt = '\0';
2534 return pkt;
2535}
2536
2537/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2538
2539static int
fba45db2
KB
2540parse_threadlist_response (char *pkt, int result_limit,
2541 threadref *original_echo, threadref *resultlist,
2542 int *doneflag)
c906108c 2543{
d01949b6 2544 struct remote_state *rs = get_remote_state ();
c906108c
SS
2545 char *limit;
2546 int count, resultcount, done;
2547
2548 resultcount = 0;
2549 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 2550 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 2551 /* done parse past here */
c906108c
SS
2552 pkt = unpack_byte (pkt, &count); /* count field */
2553 pkt = unpack_nibble (pkt, &done);
2554 /* The first threadid is the argument threadid. */
2555 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2556 while ((count-- > 0) && (pkt < limit))
2557 {
2558 pkt = unpack_threadid (pkt, resultlist++);
2559 if (resultcount++ >= result_limit)
2560 break;
2561 }
2562 if (doneflag)
2563 *doneflag = done;
2564 return resultcount;
2565}
2566
6dc54d91
PA
2567/* Fetch the next batch of threads from the remote. Returns -1 if the
2568 qL packet is not supported, 0 on error and 1 on success. */
2569
c906108c 2570static int
fba45db2
KB
2571remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2572 int *done, int *result_count, threadref *threadlist)
c906108c 2573{
d01949b6 2574 struct remote_state *rs = get_remote_state ();
c906108c
SS
2575 int result = 1;
2576
23860348 2577 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
2578 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2579 >= get_remote_packet_size ())
ea9c271d 2580 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 2581
6d820c5c
DJ
2582 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2583 putpkt (rs->buf);
2584 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 2585 if (*rs->buf == '\0')
6dc54d91
PA
2586 {
2587 /* Packet not supported. */
2588 return -1;
2589 }
2590
2591 *result_count =
2592 parse_threadlist_response (rs->buf + 2, result_limit,
2593 &rs->echo_nextthread, threadlist, done);
c906108c 2594
0d031856 2595 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 2596 {
23860348
MS
2597 /* FIXME: This is a good reason to drop the packet. */
2598 /* Possably, there is a duplicate response. */
c906108c
SS
2599 /* Possabilities :
2600 retransmit immediatly - race conditions
2601 retransmit after timeout - yes
2602 exit
2603 wait for packet, then exit
2604 */
8a3fe4f8 2605 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 2606 return 0; /* I choose simply exiting. */
c906108c
SS
2607 }
2608 if (*result_count <= 0)
2609 {
2610 if (*done != 1)
2611 {
8a3fe4f8 2612 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
2613 result = 0;
2614 }
2615 return result; /* break; */
2616 }
2617 if (*result_count > result_limit)
2618 {
2619 *result_count = 0;
8a3fe4f8 2620 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
2621 return 0;
2622 }
2623 return result;
2624}
2625
6dc54d91
PA
2626/* Fetch the list of remote threads, with the qL packet, and call
2627 STEPFUNCTION for each thread found. Stops iterating and returns 1
2628 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2629 STEPFUNCTION returns false. If the packet is not supported,
2630 returns -1. */
c906108c 2631
c906108c 2632static int
fba45db2
KB
2633remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2634 int looplimit)
c906108c 2635{
0d031856 2636 struct remote_state *rs = get_remote_state ();
c906108c
SS
2637 int done, i, result_count;
2638 int startflag = 1;
2639 int result = 1;
2640 int loopcount = 0;
c906108c
SS
2641
2642 done = 0;
2643 while (!done)
2644 {
2645 if (loopcount++ > looplimit)
2646 {
2647 result = 0;
8a3fe4f8 2648 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
2649 break;
2650 }
6dc54d91
PA
2651 result = remote_get_threadlist (startflag, &rs->nextthread,
2652 MAXTHREADLISTRESULTS,
2653 &done, &result_count,
2654 rs->resultthreadlist);
2655 if (result <= 0)
2656 break;
23860348 2657 /* Clear for later iterations. */
c906108c
SS
2658 startflag = 0;
2659 /* Setup to resume next batch of thread references, set nextthread. */
2660 if (result_count >= 1)
0d031856
TT
2661 copy_threadref (&rs->nextthread,
2662 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
2663 i = 0;
2664 while (result_count--)
6dc54d91
PA
2665 {
2666 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2667 {
2668 result = 0;
2669 break;
2670 }
2671 }
c906108c
SS
2672 }
2673 return result;
2674}
2675
6dc54d91
PA
2676/* A thread found on the remote target. */
2677
2678typedef struct thread_item
2679{
2680 /* The thread's PTID. */
2681 ptid_t ptid;
2682
2683 /* The thread's extra info. May be NULL. */
2684 char *extra;
2685
2686 /* The core the thread was running on. -1 if not known. */
2687 int core;
2688} thread_item_t;
2689DEF_VEC_O(thread_item_t);
2690
2691/* Context passed around to the various methods listing remote
2692 threads. As new threads are found, they're added to the ITEMS
2693 vector. */
2694
2695struct threads_listing_context
2696{
2697 /* The threads found on the remote target. */
2698 VEC (thread_item_t) *items;
2699};
2700
80134cf5
PA
2701/* Discard the contents of the constructed thread listing context. */
2702
2703static void
2704clear_threads_listing_context (void *p)
2705{
2706 struct threads_listing_context *context = p;
2707 int i;
2708 struct thread_item *item;
2709
2710 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2711 xfree (item->extra);
2712
2713 VEC_free (thread_item_t, context->items);
2714}
2715
cbb8991c
DB
2716/* Remove the thread specified as the related_pid field of WS
2717 from the CONTEXT list. */
2718
2719static void
2720threads_listing_context_remove (struct target_waitstatus *ws,
2721 struct threads_listing_context *context)
2722{
2723 struct thread_item *item;
2724 int i;
2725 ptid_t child_ptid = ws->value.related_pid;
2726
2727 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2728 {
2729 if (ptid_equal (item->ptid, child_ptid))
2730 {
2731 VEC_ordered_remove (thread_item_t, context->items, i);
2732 break;
2733 }
2734 }
2735}
2736
c906108c 2737static int
6dc54d91 2738remote_newthread_step (threadref *ref, void *data)
c906108c 2739{
6dc54d91
PA
2740 struct threads_listing_context *context = data;
2741 struct thread_item item;
79d7f229 2742 int pid = ptid_get_pid (inferior_ptid);
39f77062 2743
6dc54d91
PA
2744 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
2745 item.core = -1;
2746 item.extra = NULL;
2747
2748 VEC_safe_push (thread_item_t, context->items, &item);
2749
c906108c
SS
2750 return 1; /* continue iterator */
2751}
2752
2753#define CRAZY_MAX_THREADS 1000
2754
39f77062
KB
2755static ptid_t
2756remote_current_thread (ptid_t oldpid)
c906108c 2757{
d01949b6 2758 struct remote_state *rs = get_remote_state ();
c906108c
SS
2759
2760 putpkt ("qC");
6d820c5c 2761 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2762 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
c9f35b34
KB
2763 {
2764 char *obuf;
2765 ptid_t result;
2766
2767 result = read_ptid (&rs->buf[2], &obuf);
2768 if (*obuf != '\0' && remote_debug)
2769 fprintf_unfiltered (gdb_stdlog,
2770 "warning: garbage in qC reply\n");
2771
2772 return result;
2773 }
c906108c
SS
2774 else
2775 return oldpid;
2776}
2777
6dc54d91 2778/* List remote threads using the deprecated qL packet. */
cce74817 2779
6dc54d91
PA
2780static int
2781remote_get_threads_with_ql (struct target_ops *ops,
2782 struct threads_listing_context *context)
c906108c 2783{
6dc54d91
PA
2784 if (remote_threadlist_iterator (remote_newthread_step, context,
2785 CRAZY_MAX_THREADS) >= 0)
2786 return 1;
2787
2788 return 0;
c906108c
SS
2789}
2790
dc146f7c
VP
2791#if defined(HAVE_LIBEXPAT)
2792
dc146f7c
VP
2793static void
2794start_thread (struct gdb_xml_parser *parser,
2795 const struct gdb_xml_element *element,
2796 void *user_data, VEC(gdb_xml_value_s) *attributes)
2797{
6dc54d91 2798 struct threads_listing_context *data = user_data;
dc146f7c
VP
2799
2800 struct thread_item item;
2801 char *id;
3d2c1d41 2802 struct gdb_xml_value *attr;
dc146f7c 2803
3d2c1d41 2804 id = xml_find_attribute (attributes, "id")->value;
dc146f7c
VP
2805 item.ptid = read_ptid (id, NULL);
2806
3d2c1d41
PA
2807 attr = xml_find_attribute (attributes, "core");
2808 if (attr != NULL)
2809 item.core = *(ULONGEST *) attr->value;
dc146f7c
VP
2810 else
2811 item.core = -1;
2812
2813 item.extra = 0;
2814
2815 VEC_safe_push (thread_item_t, data->items, &item);
2816}
2817
2818static void
2819end_thread (struct gdb_xml_parser *parser,
2820 const struct gdb_xml_element *element,
2821 void *user_data, const char *body_text)
2822{
6dc54d91 2823 struct threads_listing_context *data = user_data;
dc146f7c
VP
2824
2825 if (body_text && *body_text)
2ae2a0b7 2826 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
dc146f7c
VP
2827}
2828
2829const struct gdb_xml_attribute thread_attributes[] = {
2830 { "id", GDB_XML_AF_NONE, NULL, NULL },
2831 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2832 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2833};
2834
2835const struct gdb_xml_element thread_children[] = {
2836 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2837};
2838
2839const struct gdb_xml_element threads_children[] = {
2840 { "thread", thread_attributes, thread_children,
2841 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2842 start_thread, end_thread },
2843 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2844};
2845
2846const struct gdb_xml_element threads_elements[] = {
2847 { "threads", NULL, threads_children,
2848 GDB_XML_EF_NONE, NULL, NULL },
2849 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2850};
2851
2852#endif
2853
6dc54d91 2854/* List remote threads using qXfer:threads:read. */
9d1f7ab2 2855
6dc54d91
PA
2856static int
2857remote_get_threads_with_qxfer (struct target_ops *ops,
2858 struct threads_listing_context *context)
0f71a2f6 2859{
dc146f7c 2860#if defined(HAVE_LIBEXPAT)
4082afcc 2861 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 2862 {
6dc54d91 2863 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
dc146f7c 2864 struct cleanup *back_to = make_cleanup (xfree, xml);
efc0eabd 2865
6dc54d91 2866 if (xml != NULL && *xml != '\0')
dc146f7c 2867 {
6dc54d91
PA
2868 gdb_xml_parse_quick (_("threads"), "threads.dtd",
2869 threads_elements, xml, context);
dc146f7c
VP
2870 }
2871
2872 do_cleanups (back_to);
6dc54d91 2873 return 1;
dc146f7c
VP
2874 }
2875#endif
2876
6dc54d91
PA
2877 return 0;
2878}
2879
2880/* List remote threads using qfThreadInfo/qsThreadInfo. */
2881
2882static int
2883remote_get_threads_with_qthreadinfo (struct target_ops *ops,
2884 struct threads_listing_context *context)
2885{
2886 struct remote_state *rs = get_remote_state ();
2887
b80fafe3 2888 if (rs->use_threadinfo_query)
9d1f7ab2 2889 {
6dc54d91
PA
2890 char *bufp;
2891
9d1f7ab2 2892 putpkt ("qfThreadInfo");
6d820c5c 2893 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2894 bufp = rs->buf;
9d1f7ab2 2895 if (bufp[0] != '\0') /* q packet recognized */
802188a7 2896 {
9d1f7ab2
MS
2897 while (*bufp++ == 'm') /* reply contains one or more TID */
2898 {
2899 do
2900 {
6dc54d91
PA
2901 struct thread_item item;
2902
2903 item.ptid = read_ptid (bufp, &bufp);
2904 item.core = -1;
2905 item.extra = NULL;
2906
2907 VEC_safe_push (thread_item_t, context->items, &item);
9d1f7ab2
MS
2908 }
2909 while (*bufp++ == ','); /* comma-separated list */
2910 putpkt ("qsThreadInfo");
6d820c5c 2911 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 2912 bufp = rs->buf;
9d1f7ab2 2913 }
6dc54d91
PA
2914 return 1;
2915 }
2916 else
2917 {
2918 /* Packet not recognized. */
2919 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
2920 }
2921 }
2922
6dc54d91
PA
2923 return 0;
2924}
2925
e8032dde 2926/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
2927 targets. */
2928
2929static void
e8032dde 2930remote_update_thread_list (struct target_ops *ops)
6dc54d91
PA
2931{
2932 struct remote_state *rs = get_remote_state ();
2933 struct threads_listing_context context;
2934 struct cleanup *old_chain;
ab970af1 2935 int got_list = 0;
e8032dde 2936
6dc54d91
PA
2937 context.items = NULL;
2938 old_chain = make_cleanup (clear_threads_listing_context, &context);
2939
2940 /* We have a few different mechanisms to fetch the thread list. Try
2941 them all, starting with the most preferred one first, falling
2942 back to older methods. */
2943 if (remote_get_threads_with_qxfer (ops, &context)
2944 || remote_get_threads_with_qthreadinfo (ops, &context)
2945 || remote_get_threads_with_ql (ops, &context))
2946 {
2947 int i;
2948 struct thread_item *item;
ab970af1
PA
2949 struct thread_info *tp, *tmp;
2950
2951 got_list = 1;
2952
7d1a114c
PA
2953 if (VEC_empty (thread_item_t, context.items)
2954 && remote_thread_always_alive (ops, inferior_ptid))
2955 {
2956 /* Some targets don't really support threads, but still
2957 reply an (empty) thread list in response to the thread
2958 listing packets, instead of replying "packet not
2959 supported". Exit early so we don't delete the main
2960 thread. */
2961 do_cleanups (old_chain);
2962 return;
2963 }
2964
ab970af1
PA
2965 /* CONTEXT now holds the current thread list on the remote
2966 target end. Delete GDB-side threads no longer found on the
2967 target. */
8a06aea7 2968 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 2969 {
ab970af1
PA
2970 for (i = 0;
2971 VEC_iterate (thread_item_t, context.items, i, item);
2972 ++i)
2973 {
2974 if (ptid_equal (item->ptid, tp->ptid))
2975 break;
2976 }
2977
2978 if (i == VEC_length (thread_item_t, context.items))
2979 {
2980 /* Not found. */
2981 delete_thread (tp->ptid);
2982 }
cbb8991c
DB
2983 }
2984
2985 /* Remove any unreported fork child threads from CONTEXT so
2986 that we don't interfere with follow fork, which is where
2987 creation of such threads is handled. */
2988 remove_new_fork_children (&context);
74531fed 2989
ab970af1 2990 /* And now add threads we don't know about yet to our list. */
6dc54d91
PA
2991 for (i = 0;
2992 VEC_iterate (thread_item_t, context.items, i, item);
2993 ++i)
2994 {
2995 if (!ptid_equal (item->ptid, null_ptid))
2996 {
2997 struct private_thread_info *info;
2998 /* In non-stop mode, we assume new found threads are
2999 running until proven otherwise with a stop reply. In
3000 all-stop, we can only get here if all threads are
3001 stopped. */
3002 int running = non_stop ? 1 : 0;
3003
3004 remote_notice_new_inferior (item->ptid, running);
3005
3006 info = demand_private_info (item->ptid);
3007 info->core = item->core;
3008 info->extra = item->extra;
3009 item->extra = NULL;
3010 }
3011 }
3012 }
3013
ab970af1
PA
3014 if (!got_list)
3015 {
3016 /* If no thread listing method is supported, then query whether
3017 each known thread is alive, one by one, with the T packet.
3018 If the target doesn't support threads at all, then this is a
3019 no-op. See remote_thread_alive. */
3020 prune_threads ();
3021 }
3022
6dc54d91 3023 do_cleanups (old_chain);
9d1f7ab2
MS
3024}
3025
802188a7 3026/*
9d1f7ab2
MS
3027 * Collect a descriptive string about the given thread.
3028 * The target may say anything it wants to about the thread
3029 * (typically info about its blocked / runnable state, name, etc.).
3030 * This string will appear in the info threads display.
802188a7 3031 *
9d1f7ab2
MS
3032 * Optional: targets are not required to implement this function.
3033 */
3034
3035static char *
c15906d8 3036remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
9d1f7ab2 3037{
d01949b6 3038 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3039 int result;
3040 int set;
3041 threadref id;
3042 struct gdb_ext_thread_info threadinfo;
23860348 3043 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3044 int n = 0; /* position in display_buf */
3045
5d93a237 3046 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3047 internal_error (__FILE__, __LINE__,
e2e0b3e5 3048 _("remote_threads_extra_info"));
9d1f7ab2 3049
60e569b9 3050 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3051 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3052 /* This is the main thread which was added by GDB. The remote
3053 server doesn't know about it. */
3054 return NULL;
3055
4082afcc 3056 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3057 {
3058 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3059
fe978cb0
PA
3060 if (info && info->priv)
3061 return info->priv->extra;
dc146f7c
VP
3062 else
3063 return NULL;
3064 }
3065
b80fafe3 3066 if (rs->use_threadextra_query)
9d1f7ab2 3067 {
82f73884
PA
3068 char *b = rs->buf;
3069 char *endb = rs->buf + get_remote_packet_size ();
3070
3071 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3072 b += strlen (b);
3073 write_ptid (b, endb, tp->ptid);
3074
2e9f7625 3075 putpkt (rs->buf);
6d820c5c 3076 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3077 if (rs->buf[0] != 0)
9d1f7ab2 3078 {
2e9f7625
DJ
3079 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
3080 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3081 display_buf [result] = '\0';
9d1f7ab2
MS
3082 return display_buf;
3083 }
0f71a2f6 3084 }
9d1f7ab2
MS
3085
3086 /* If the above query fails, fall back to the old method. */
b80fafe3 3087 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3088 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3089 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3090 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3091 if (remote_get_threadinfo (&id, set, &threadinfo))
3092 if (threadinfo.active)
0f71a2f6 3093 {
9d1f7ab2 3094 if (*threadinfo.shortname)
2bc416ba 3095 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3096 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3097 if (*threadinfo.display)
2bc416ba 3098 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3099 " State: %s,", threadinfo.display);
9d1f7ab2 3100 if (*threadinfo.more_display)
2bc416ba 3101 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3102 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3103
3104 if (n > 0)
c5aa993b 3105 {
23860348 3106 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3107 if (',' == display_buf[n-1])
3108 display_buf[n-1] = ' ';
3109 return display_buf;
c5aa993b 3110 }
0f71a2f6 3111 }
9d1f7ab2 3112 return NULL;
0f71a2f6 3113}
c906108c 3114\f
c5aa993b 3115
0fb4aa4b 3116static int
61fc905d 3117remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
0fb4aa4b
PA
3118 struct static_tracepoint_marker *marker)
3119{
3120 struct remote_state *rs = get_remote_state ();
3121 char *p = rs->buf;
3122
bba74b36 3123 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3124 p += strlen (p);
3125 p += hexnumstr (p, addr);
3126 putpkt (rs->buf);
3127 getpkt (&rs->buf, &rs->buf_size, 0);
3128 p = rs->buf;
3129
3130 if (*p == 'E')
3131 error (_("Remote failure reply: %s"), p);
3132
3133 if (*p++ == 'm')
3134 {
3135 parse_static_tracepoint_marker_definition (p, &p, marker);
3136 return 1;
3137 }
3138
3139 return 0;
3140}
3141
0fb4aa4b 3142static VEC(static_tracepoint_marker_p) *
c686c57f
TT
3143remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3144 const char *strid)
0fb4aa4b
PA
3145{
3146 struct remote_state *rs = get_remote_state ();
3147 VEC(static_tracepoint_marker_p) *markers = NULL;
3148 struct static_tracepoint_marker *marker = NULL;
3149 struct cleanup *old_chain;
3150 char *p;
3151
3152 /* Ask for a first packet of static tracepoint marker
3153 definition. */
3154 putpkt ("qTfSTM");
3155 getpkt (&rs->buf, &rs->buf_size, 0);
3156 p = rs->buf;
3157 if (*p == 'E')
3158 error (_("Remote failure reply: %s"), p);
3159
3160 old_chain = make_cleanup (free_current_marker, &marker);
3161
3162 while (*p++ == 'm')
3163 {
3164 if (marker == NULL)
3165 marker = XCNEW (struct static_tracepoint_marker);
3166
3167 do
3168 {
3169 parse_static_tracepoint_marker_definition (p, &p, marker);
3170
3171 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3172 {
3173 VEC_safe_push (static_tracepoint_marker_p,
3174 markers, marker);
3175 marker = NULL;
3176 }
3177 else
3178 {
3179 release_static_tracepoint_marker (marker);
3180 memset (marker, 0, sizeof (*marker));
3181 }
3182 }
3183 while (*p++ == ','); /* comma-separated list */
3184 /* Ask for another packet of static tracepoint definition. */
3185 putpkt ("qTsSTM");
3186 getpkt (&rs->buf, &rs->buf_size, 0);
3187 p = rs->buf;
3188 }
3189
3190 do_cleanups (old_chain);
3191 return markers;
3192}
3193
3194\f
10760264
JB
3195/* Implement the to_get_ada_task_ptid function for the remote targets. */
3196
3197static ptid_t
1e6b91a4 3198remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
10760264 3199{
ba348170 3200 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3201}
3202\f
3203
24b06219 3204/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3205
3206static void
fba45db2 3207extended_remote_restart (void)
c906108c 3208{
d01949b6 3209 struct remote_state *rs = get_remote_state ();
c906108c
SS
3210
3211 /* Send the restart command; for reasons I don't understand the
3212 remote side really expects a number after the "R". */
ea9c271d 3213 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3214 putpkt (rs->buf);
c906108c 3215
ad9a8f3f 3216 remote_fileio_reset ();
c906108c
SS
3217}
3218\f
3219/* Clean up connection to a remote debugger. */
3220
c906108c 3221static void
de90e03d 3222remote_close (struct target_ops *self)
c906108c 3223{
5d93a237
TT
3224 struct remote_state *rs = get_remote_state ();
3225
3226 if (rs->remote_desc == NULL)
d3fd5342
PA
3227 return; /* already closed */
3228
3229 /* Make sure we leave stdin registered in the event loop, and we
3230 don't leave the async SIGINT signal handler installed. */
e3594fd1 3231 remote_terminal_ours (self);
ce5ce7ed 3232
5d93a237
TT
3233 serial_close (rs->remote_desc);
3234 rs->remote_desc = NULL;
ce5ce7ed
PA
3235
3236 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3237 of all the inferiors and their threads we were controlling.
3238 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3239 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3240 inferior_ptid = null_ptid;
f67fd822 3241 discard_all_inferiors ();
ce5ce7ed 3242
f48ff2a7
YQ
3243 /* We are closing the remote target, so we should discard
3244 everything of this target. */
bcc75809 3245 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3246
3247 if (remote_async_inferior_event_token)
3248 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3249
5965e028 3250 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3251
3252 trace_reset_local_state ();
c906108c
SS
3253}
3254
23860348 3255/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3256
3257static void
fba45db2 3258get_offsets (void)
c906108c 3259{
d01949b6 3260 struct remote_state *rs = get_remote_state ();
2e9f7625 3261 char *buf;
085dd6e6 3262 char *ptr;
31d99776
DJ
3263 int lose, num_segments = 0, do_sections, do_segments;
3264 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3265 struct section_offsets *offs;
31d99776
DJ
3266 struct symfile_segment_data *data;
3267
3268 if (symfile_objfile == NULL)
3269 return;
c906108c
SS
3270
3271 putpkt ("qOffsets");
6d820c5c 3272 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3273 buf = rs->buf;
c906108c
SS
3274
3275 if (buf[0] == '\000')
3276 return; /* Return silently. Stub doesn't support
23860348 3277 this command. */
c906108c
SS
3278 if (buf[0] == 'E')
3279 {
8a3fe4f8 3280 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3281 return;
3282 }
3283
3284 /* Pick up each field in turn. This used to be done with scanf, but
3285 scanf will make trouble if CORE_ADDR size doesn't match
3286 conversion directives correctly. The following code will work
3287 with any size of CORE_ADDR. */
3288 text_addr = data_addr = bss_addr = 0;
3289 ptr = buf;
3290 lose = 0;
3291
61012eef 3292 if (startswith (ptr, "Text="))
c906108c
SS
3293 {
3294 ptr += 5;
3295 /* Don't use strtol, could lose on big values. */
3296 while (*ptr && *ptr != ';')
3297 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3298
61012eef 3299 if (startswith (ptr, ";Data="))
31d99776
DJ
3300 {
3301 ptr += 6;
3302 while (*ptr && *ptr != ';')
3303 data_addr = (data_addr << 4) + fromhex (*ptr++);
3304 }
3305 else
3306 lose = 1;
3307
61012eef 3308 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3309 {
3310 ptr += 5;
3311 while (*ptr && *ptr != ';')
3312 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3313
31d99776
DJ
3314 if (bss_addr != data_addr)
3315 warning (_("Target reported unsupported offsets: %s"), buf);
3316 }
3317 else
3318 lose = 1;
3319 }
61012eef 3320 else if (startswith (ptr, "TextSeg="))
c906108c 3321 {
31d99776
DJ
3322 ptr += 8;
3323 /* Don't use strtol, could lose on big values. */
c906108c 3324 while (*ptr && *ptr != ';')
31d99776
DJ
3325 text_addr = (text_addr << 4) + fromhex (*ptr++);
3326 num_segments = 1;
3327
61012eef 3328 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3329 {
3330 ptr += 9;
3331 while (*ptr && *ptr != ';')
3332 data_addr = (data_addr << 4) + fromhex (*ptr++);
3333 num_segments++;
3334 }
c906108c
SS
3335 }
3336 else
3337 lose = 1;
3338
3339 if (lose)
8a3fe4f8 3340 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3341 else if (*ptr != '\0')
3342 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3343
802188a7 3344 offs = ((struct section_offsets *)
a39a16c4 3345 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3346 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3347 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3348
31d99776
DJ
3349 data = get_symfile_segment_data (symfile_objfile->obfd);
3350 do_segments = (data != NULL);
3351 do_sections = num_segments == 0;
c906108c 3352
28c32713 3353 if (num_segments > 0)
31d99776 3354 {
31d99776
DJ
3355 segments[0] = text_addr;
3356 segments[1] = data_addr;
3357 }
28c32713
JB
3358 /* If we have two segments, we can still try to relocate everything
3359 by assuming that the .text and .data offsets apply to the whole
3360 text and data segments. Convert the offsets given in the packet
3361 to base addresses for symfile_map_offsets_to_segments. */
3362 else if (data && data->num_segments == 2)
3363 {
3364 segments[0] = data->segment_bases[0] + text_addr;
3365 segments[1] = data->segment_bases[1] + data_addr;
3366 num_segments = 2;
3367 }
8d385431
DJ
3368 /* If the object file has only one segment, assume that it is text
3369 rather than data; main programs with no writable data are rare,
3370 but programs with no code are useless. Of course the code might
3371 have ended up in the data segment... to detect that we would need
3372 the permissions here. */
3373 else if (data && data->num_segments == 1)
3374 {
3375 segments[0] = data->segment_bases[0] + text_addr;
3376 num_segments = 1;
3377 }
28c32713
JB
3378 /* There's no way to relocate by segment. */
3379 else
3380 do_segments = 0;
31d99776
DJ
3381
3382 if (do_segments)
3383 {
3384 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3385 offs, num_segments, segments);
3386
3387 if (ret == 0 && !do_sections)
3e43a32a
MS
3388 error (_("Can not handle qOffsets TextSeg "
3389 "response with this symbol file"));
31d99776
DJ
3390
3391 if (ret > 0)
3392 do_sections = 0;
3393 }
c906108c 3394
9ef895d6
DJ
3395 if (data)
3396 free_symfile_segment_data (data);
31d99776
DJ
3397
3398 if (do_sections)
3399 {
3400 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3401
3e43a32a
MS
3402 /* This is a temporary kludge to force data and bss to use the
3403 same offsets because that's what nlmconv does now. The real
3404 solution requires changes to the stub and remote.c that I
3405 don't have time to do right now. */
31d99776
DJ
3406
3407 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3408 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3409 }
c906108c
SS
3410
3411 objfile_relocate (symfile_objfile, offs);
3412}
3413
74531fed
PA
3414/* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3415 threads we know are stopped already. This is used during the
3416 initial remote connection in non-stop mode --- threads that are
3417 reported as already being stopped are left stopped. */
3418
3419static int
3420set_stop_requested_callback (struct thread_info *thread, void *data)
3421{
3422 /* If we have a stop reply for this thread, it must be stopped. */
3423 if (peek_stop_reply (thread->ptid))
3424 set_stop_requested (thread->ptid, 1);
3425
3426 return 0;
3427}
3428
9a7071a8
JB
3429/* Send interrupt_sequence to remote target. */
3430static void
eeae04df 3431send_interrupt_sequence (void)
9a7071a8 3432{
5d93a237
TT
3433 struct remote_state *rs = get_remote_state ();
3434
9a7071a8 3435 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3436 remote_serial_write ("\x03", 1);
9a7071a8 3437 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3438 serial_send_break (rs->remote_desc);
9a7071a8
JB
3439 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3440 {
5d93a237 3441 serial_send_break (rs->remote_desc);
c33e31fd 3442 remote_serial_write ("g", 1);
9a7071a8
JB
3443 }
3444 else
3445 internal_error (__FILE__, __LINE__,
3446 _("Invalid value for interrupt_sequence_mode: %s."),
3447 interrupt_sequence_mode);
3448}
3449
3405876a
PA
3450
3451/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3452 and extract the PTID. Returns NULL_PTID if not found. */
3453
3454static ptid_t
3455stop_reply_extract_thread (char *stop_reply)
3456{
3457 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3458 {
3459 char *p;
3460
3461 /* Txx r:val ; r:val (...) */
3462 p = &stop_reply[3];
3463
3464 /* Look for "register" named "thread". */
3465 while (*p != '\0')
3466 {
3467 char *p1;
3468
3469 p1 = strchr (p, ':');
3470 if (p1 == NULL)
3471 return null_ptid;
3472
3473 if (strncmp (p, "thread", p1 - p) == 0)
3474 return read_ptid (++p1, &p);
3475
3476 p1 = strchr (p, ';');
3477 if (p1 == NULL)
3478 return null_ptid;
3479 p1++;
3480
3481 p = p1;
3482 }
3483 }
3484
3485 return null_ptid;
3486}
3487
b7ea362b
PA
3488/* Determine the remote side's current thread. If we have a stop
3489 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3490 "thread" register we can extract the current thread from. If not,
3491 ask the remote which is the current thread with qC. The former
3492 method avoids a roundtrip. */
3493
3494static ptid_t
3495get_current_thread (char *wait_status)
3496{
6a49a997 3497 ptid_t ptid = null_ptid;
b7ea362b
PA
3498
3499 /* Note we don't use remote_parse_stop_reply as that makes use of
3500 the target architecture, which we haven't yet fully determined at
3501 this point. */
3502 if (wait_status != NULL)
3503 ptid = stop_reply_extract_thread (wait_status);
3504 if (ptid_equal (ptid, null_ptid))
3505 ptid = remote_current_thread (inferior_ptid);
3506
3507 return ptid;
3508}
3509
49c62f2e
PA
3510/* Query the remote target for which is the current thread/process,
3511 add it to our tables, and update INFERIOR_PTID. The caller is
3512 responsible for setting the state such that the remote end is ready
3405876a
PA
3513 to return the current thread.
3514
3515 This function is called after handling the '?' or 'vRun' packets,
3516 whose response is a stop reply from which we can also try
3517 extracting the thread. If the target doesn't support the explicit
3518 qC query, we infer the current thread from that stop reply, passed
3519 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
3520
3521static void
3405876a 3522add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
3523{
3524 struct remote_state *rs = get_remote_state ();
3525 int fake_pid_p = 0;
6a49a997 3526 ptid_t ptid;
49c62f2e
PA
3527
3528 inferior_ptid = null_ptid;
3529
b7ea362b
PA
3530 /* Now, if we have thread information, update inferior_ptid. */
3531 ptid = get_current_thread (wait_status);
3405876a 3532
49c62f2e
PA
3533 if (!ptid_equal (ptid, null_ptid))
3534 {
3535 if (!remote_multi_process_p (rs))
3536 fake_pid_p = 1;
3537
3538 inferior_ptid = ptid;
3539 }
3540 else
3541 {
3542 /* Without this, some commands which require an active target
3543 (such as kill) won't work. This variable serves (at least)
3544 double duty as both the pid of the target process (if it has
3545 such), and as a flag indicating that a target is active. */
3546 inferior_ptid = magic_null_ptid;
3547 fake_pid_p = 1;
3548 }
3549
1b6e6f5c 3550 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1, 1);
49c62f2e
PA
3551
3552 /* Add the main thread. */
3553 add_thread_silent (inferior_ptid);
3554}
3555
9cbc821d 3556static void
04bd08de 3557remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
c906108c 3558{
c8d104ad
PA
3559 struct remote_state *rs = get_remote_state ();
3560 struct packet_config *noack_config;
2d717e4f 3561 char *wait_status = NULL;
8621d6a9 3562
23860348 3563 immediate_quit++; /* Allow user to interrupt it. */
522002f9 3564 QUIT;
c906108c 3565
9a7071a8
JB
3566 if (interrupt_on_connect)
3567 send_interrupt_sequence ();
3568
57e12211 3569 /* Ack any packet which the remote side has already sent. */
5d93a237 3570 serial_write (rs->remote_desc, "+", 1);
57e12211 3571
1e51243a
PA
3572 /* Signal other parts that we're going through the initial setup,
3573 and so things may not be stable yet. */
3574 rs->starting_up = 1;
3575
c8d104ad
PA
3576 /* The first packet we send to the target is the optional "supported
3577 packets" request. If the target can answer this, it will tell us
3578 which later probes to skip. */
3579 remote_query_supported ();
3580
d914c394 3581 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 3582 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
c378d69d 3583 remote_set_permissions (target);
d914c394 3584
c8d104ad
PA
3585 /* Next, we possibly activate noack mode.
3586
3587 If the QStartNoAckMode packet configuration is set to AUTO,
3588 enable noack mode if the stub reported a wish for it with
3589 qSupported.
3590
3591 If set to TRUE, then enable noack mode even if the stub didn't
3592 report it in qSupported. If the stub doesn't reply OK, the
3593 session ends with an error.
3594
3595 If FALSE, then don't activate noack mode, regardless of what the
3596 stub claimed should be the default with qSupported. */
3597
3598 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 3599 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
3600 {
3601 putpkt ("QStartNoAckMode");
3602 getpkt (&rs->buf, &rs->buf_size, 0);
3603 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3604 rs->noack_mode = 1;
3605 }
3606
04bd08de 3607 if (extended_p)
5fe04517
PA
3608 {
3609 /* Tell the remote that we are using the extended protocol. */
3610 putpkt ("!");
3611 getpkt (&rs->buf, &rs->buf_size, 0);
3612 }
3613
9b224c5e
PA
3614 /* Let the target know which signals it is allowed to pass down to
3615 the program. */
3616 update_signals_program_target ();
3617
d962ef82
DJ
3618 /* Next, if the target can specify a description, read it. We do
3619 this before anything involving memory or registers. */
3620 target_find_description ();
3621
6c95b8df
PA
3622 /* Next, now that we know something about the target, update the
3623 address spaces in the program spaces. */
3624 update_address_spaces ();
3625
50c71eaf
PA
3626 /* On OSs where the list of libraries is global to all
3627 processes, we fetch them early. */
f5656ead 3628 if (gdbarch_has_global_solist (target_gdbarch ()))
04bd08de 3629 solib_add (NULL, from_tty, target, auto_solib_add);
50c71eaf 3630
74531fed
PA
3631 if (non_stop)
3632 {
4082afcc 3633 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
3634 error (_("Non-stop mode requested, but remote "
3635 "does not support non-stop"));
74531fed
PA
3636
3637 putpkt ("QNonStop:1");
3638 getpkt (&rs->buf, &rs->buf_size, 0);
3639
3640 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3641 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
3642
3643 /* Find about threads and processes the stub is already
3644 controlling. We default to adding them in the running state.
3645 The '?' query below will then tell us about which threads are
3646 stopped. */
e8032dde 3647 remote_update_thread_list (target);
74531fed 3648 }
4082afcc 3649 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
3650 {
3651 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 3652 Request it explicitly. */
74531fed
PA
3653 putpkt ("QNonStop:0");
3654 getpkt (&rs->buf, &rs->buf_size, 0);
3655
3656 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3657 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
3658 }
3659
a0743c90
YQ
3660 /* Upload TSVs regardless of whether the target is running or not. The
3661 remote stub, such as GDBserver, may have some predefined or builtin
3662 TSVs, even if the target is not running. */
8bd200f1 3663 if (remote_get_trace_status (target, current_trace_status ()) != -1)
a0743c90
YQ
3664 {
3665 struct uploaded_tsv *uploaded_tsvs = NULL;
3666
181e3713 3667 remote_upload_trace_state_variables (target, &uploaded_tsvs);
a0743c90
YQ
3668 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3669 }
3670
2d717e4f
DJ
3671 /* Check whether the target is running now. */
3672 putpkt ("?");
3673 getpkt (&rs->buf, &rs->buf_size, 0);
3674
74531fed 3675 if (!non_stop)
2d717e4f 3676 {
e714e1bf
UW
3677 ptid_t ptid;
3678 int fake_pid_p = 0;
3679 struct inferior *inf;
3680
74531fed 3681 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 3682 {
04bd08de 3683 if (!extended_p)
74531fed 3684 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
3685
3686 /* We're connected, but not running. Drop out before we
3687 call start_remote. */
e278ad5b 3688 rs->starting_up = 0;
c35b1492 3689 return;
2d717e4f
DJ
3690 }
3691 else
74531fed 3692 {
74531fed
PA
3693 /* Save the reply for later. */
3694 wait_status = alloca (strlen (rs->buf) + 1);
3695 strcpy (wait_status, rs->buf);
3696 }
3697
b7ea362b 3698 /* Fetch thread list. */
e8032dde 3699 target_update_thread_list ();
b7ea362b 3700
74531fed
PA
3701 /* Let the stub know that we want it to return the thread. */
3702 set_continue_thread (minus_one_ptid);
3703
b7ea362b
PA
3704 if (thread_count () == 0)
3705 {
3706 /* Target has no concept of threads at all. GDB treats
3707 non-threaded target as single-threaded; add a main
3708 thread. */
3709 add_current_inferior_and_thread (wait_status);
3710 }
3711 else
3712 {
3713 /* We have thread information; select the thread the target
3714 says should be current. If we're reconnecting to a
3715 multi-threaded program, this will ideally be the thread
3716 that last reported an event before GDB disconnected. */
3717 inferior_ptid = get_current_thread (wait_status);
3718 if (ptid_equal (inferior_ptid, null_ptid))
3719 {
3720 /* Odd... The target was able to list threads, but not
3721 tell us which thread was current (no "thread"
3722 register in T stop reply?). Just pick the first
3723 thread in the thread list then. */
c9f35b34
KB
3724
3725 if (remote_debug)
3726 fprintf_unfiltered (gdb_stdlog,
3727 "warning: couldn't determine remote "
3728 "current thread; picking first in list.\n");
3729
b7ea362b
PA
3730 inferior_ptid = thread_list->ptid;
3731 }
3732 }
74531fed 3733
6e586cc5
YQ
3734 /* init_wait_for_inferior should be called before get_offsets in order
3735 to manage `inserted' flag in bp loc in a correct state.
3736 breakpoint_init_inferior, called from init_wait_for_inferior, set
3737 `inserted' flag to 0, while before breakpoint_re_set, called from
3738 start_remote, set `inserted' flag to 1. In the initialization of
3739 inferior, breakpoint_init_inferior should be called first, and then
3740 breakpoint_re_set can be called. If this order is broken, state of
3741 `inserted' flag is wrong, and cause some problems on breakpoint
3742 manipulation. */
3743 init_wait_for_inferior ();
3744
74531fed
PA
3745 get_offsets (); /* Get text, data & bss offsets. */
3746
d962ef82
DJ
3747 /* If we could not find a description using qXfer, and we know
3748 how to do it some other way, try again. This is not
3749 supported for non-stop; it could be, but it is tricky if
3750 there are no stopped threads when we connect. */
04bd08de 3751 if (remote_read_description_p (target)
f5656ead 3752 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
3753 {
3754 target_clear_description ();
3755 target_find_description ();
3756 }
3757
74531fed
PA
3758 /* Use the previously fetched status. */
3759 gdb_assert (wait_status != NULL);
3760 strcpy (rs->buf, wait_status);
3761 rs->cached_wait_status = 1;
3762
3763 immediate_quit--;
04bd08de 3764 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
3765 }
3766 else
3767 {
68c97600
PA
3768 /* Clear WFI global state. Do this before finding about new
3769 threads and inferiors, and setting the current inferior.
3770 Otherwise we would clear the proceed status of the current
3771 inferior when we want its stop_soon state to be preserved
3772 (see notice_new_inferior). */
3773 init_wait_for_inferior ();
3774
74531fed
PA
3775 /* In non-stop, we will either get an "OK", meaning that there
3776 are no stopped threads at this time; or, a regular stop
3777 reply. In the latter case, there may be more than one thread
3778 stopped --- we pull them all out using the vStopped
3779 mechanism. */
3780 if (strcmp (rs->buf, "OK") != 0)
3781 {
722247f1 3782 struct notif_client *notif = &notif_client_stop;
2d717e4f 3783
722247f1
YQ
3784 /* remote_notif_get_pending_replies acks this one, and gets
3785 the rest out. */
f48ff2a7 3786 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
3787 = remote_notif_parse (notif, rs->buf);
3788 remote_notif_get_pending_events (notif);
c906108c 3789
74531fed
PA
3790 /* Make sure that threads that were stopped remain
3791 stopped. */
3792 iterate_over_threads (set_stop_requested_callback, NULL);
3793 }
2d717e4f 3794
74531fed 3795 if (target_can_async_p ())
6a3753b3 3796 target_async (1);
c906108c 3797
74531fed
PA
3798 if (thread_count () == 0)
3799 {
04bd08de 3800 if (!extended_p)
74531fed 3801 error (_("The target is not running (try extended-remote?)"));
82f73884 3802
c35b1492
PA
3803 /* We're connected, but not running. Drop out before we
3804 call start_remote. */
e278ad5b 3805 rs->starting_up = 0;
c35b1492
PA
3806 return;
3807 }
74531fed
PA
3808
3809 /* Let the stub know that we want it to return the thread. */
c0a2216e 3810
74531fed
PA
3811 /* Force the stub to choose a thread. */
3812 set_general_thread (null_ptid);
c906108c 3813
74531fed
PA
3814 /* Query it. */
3815 inferior_ptid = remote_current_thread (minus_one_ptid);
3816 if (ptid_equal (inferior_ptid, minus_one_ptid))
3817 error (_("remote didn't report the current thread in non-stop mode"));
c906108c 3818
74531fed
PA
3819 get_offsets (); /* Get text, data & bss offsets. */
3820
3821 /* In non-stop mode, any cached wait status will be stored in
3822 the stop reply queue. */
3823 gdb_assert (wait_status == NULL);
f0223081 3824
2455069d 3825 /* Report all signals during attach/startup. */
94bedb42 3826 remote_pass_signals (target, 0, NULL);
74531fed 3827 }
c8d104ad 3828
c8d104ad
PA
3829 /* If we connected to a live target, do some additional setup. */
3830 if (target_has_execution)
3831 {
f4ccffad 3832 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 3833 remote_check_symbols ();
c8d104ad 3834 }
50c71eaf 3835
d5551862
SS
3836 /* Possibly the target has been engaged in a trace run started
3837 previously; find out where things are at. */
8bd200f1 3838 if (remote_get_trace_status (target, current_trace_status ()) != -1)
d5551862 3839 {
00bf0b85 3840 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 3841
00bf0b85
SS
3842 if (current_trace_status ()->running)
3843 printf_filtered (_("Trace is already running on the target.\n"));
3844
ab6617cc 3845 remote_upload_tracepoints (target, &uploaded_tps);
00bf0b85
SS
3846
3847 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
3848 }
3849
1e51243a
PA
3850 /* The thread and inferior lists are now synchronized with the
3851 target, our symbols have been relocated, and we're merged the
3852 target's tracepoints with ours. We're done with basic start
3853 up. */
3854 rs->starting_up = 0;
3855
a25a5a45
PA
3856 /* Maybe breakpoints are global and need to be inserted now. */
3857 if (breakpoints_should_be_inserted_now ())
50c71eaf 3858 insert_breakpoints ();
c906108c
SS
3859}
3860
3861/* Open a connection to a remote debugger.
3862 NAME is the filename used for communication. */
3863
3864static void
014f9477 3865remote_open (const char *name, int from_tty)
c906108c 3866{
75c99385 3867 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
3868}
3869
c906108c
SS
3870/* Open a connection to a remote debugger using the extended
3871 remote gdb protocol. NAME is the filename used for communication. */
3872
3873static void
014f9477 3874extended_remote_open (const char *name, int from_tty)
c906108c 3875{
75c99385 3876 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
3877}
3878
ca4f7f8b
PA
3879/* Reset all packets back to "unknown support". Called when opening a
3880 new connection to a remote target. */
c906108c 3881
d471ea57 3882static void
ca4f7f8b 3883reset_all_packet_configs_support (void)
d471ea57
AC
3884{
3885 int i;
a744cf53 3886
444abaca 3887 for (i = 0; i < PACKET_MAX; i++)
4082afcc 3888 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
3889}
3890
ca4f7f8b
PA
3891/* Initialize all packet configs. */
3892
3893static void
3894init_all_packet_configs (void)
3895{
3896 int i;
3897
3898 for (i = 0; i < PACKET_MAX; i++)
3899 {
3900 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
3901 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3902 }
3903}
3904
23860348 3905/* Symbol look-up. */
dc8acb97
MS
3906
3907static void
36d25514 3908remote_check_symbols (void)
dc8acb97 3909{
d01949b6 3910 struct remote_state *rs = get_remote_state ();
dc8acb97 3911 char *msg, *reply, *tmp;
3b7344d5 3912 struct bound_minimal_symbol sym;
dc8acb97
MS
3913 int end;
3914
63154eca
PA
3915 /* The remote side has no concept of inferiors that aren't running
3916 yet, it only knows about running processes. If we're connected
3917 but our current inferior is not running, we should not invite the
3918 remote target to request symbol lookups related to its
3919 (unrelated) current process. */
3920 if (!target_has_execution)
3921 return;
3922
4082afcc 3923 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
3924 return;
3925
63154eca
PA
3926 /* Make sure the remote is pointing at the right process. Note
3927 there's no way to select "no process". */
3c9c4b83
PA
3928 set_general_process ();
3929
6d820c5c
DJ
3930 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3931 because we need both at the same time. */
ea9c271d 3932 msg = alloca (get_remote_packet_size ());
6d820c5c 3933
23860348 3934 /* Invite target to request symbol lookups. */
dc8acb97
MS
3935
3936 putpkt ("qSymbol::");
6d820c5c
DJ
3937 getpkt (&rs->buf, &rs->buf_size, 0);
3938 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
2e9f7625 3939 reply = rs->buf;
dc8acb97 3940
61012eef 3941 while (startswith (reply, "qSymbol:"))
dc8acb97 3942 {
77e371c0
TT
3943 struct bound_minimal_symbol sym;
3944
dc8acb97 3945 tmp = &reply[8];
cfd77fa1 3946 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
3947 msg[end] = '\0';
3948 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 3949 if (sym.minsym == NULL)
ea9c271d 3950 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 3951 else
2bbe3cc1 3952 {
f5656ead 3953 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 3954 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
3955
3956 /* If this is a function address, return the start of code
3957 instead of any data function descriptor. */
f5656ead 3958 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
3959 sym_addr,
3960 &current_target);
3961
3962 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 3963 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
3964 }
3965
dc8acb97 3966 putpkt (msg);
6d820c5c 3967 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3968 reply = rs->buf;
dc8acb97
MS
3969 }
3970}
3971
9db8d71f 3972static struct serial *
baa336ce 3973remote_serial_open (const char *name)
9db8d71f
DJ
3974{
3975 static int udp_warning = 0;
3976
3977 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3978 of in ser-tcp.c, because it is the remote protocol assuming that the
3979 serial connection is reliable and not the serial connection promising
3980 to be. */
61012eef 3981 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 3982 {
3e43a32a
MS
3983 warning (_("The remote protocol may be unreliable over UDP.\n"
3984 "Some events may be lost, rendering further debugging "
3985 "impossible."));
9db8d71f
DJ
3986 udp_warning = 1;
3987 }
3988
3989 return serial_open (name);
3990}
3991
d914c394
SS
3992/* Inform the target of our permission settings. The permission flags
3993 work without this, but if the target knows the settings, it can do
3994 a couple things. First, it can add its own check, to catch cases
3995 that somehow manage to get by the permissions checks in target
3996 methods. Second, if the target is wired to disallow particular
3997 settings (for instance, a system in the field that is not set up to
3998 be able to stop at a breakpoint), it can object to any unavailable
3999 permissions. */
4000
4001void
c378d69d 4002remote_set_permissions (struct target_ops *self)
d914c394
SS
4003{
4004 struct remote_state *rs = get_remote_state ();
4005
bba74b36
YQ
4006 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
4007 "WriteReg:%x;WriteMem:%x;"
4008 "InsertBreak:%x;InsertTrace:%x;"
4009 "InsertFastTrace:%x;Stop:%x",
4010 may_write_registers, may_write_memory,
4011 may_insert_breakpoints, may_insert_tracepoints,
4012 may_insert_fast_tracepoints, may_stop);
d914c394
SS
4013 putpkt (rs->buf);
4014 getpkt (&rs->buf, &rs->buf_size, 0);
4015
4016 /* If the target didn't like the packet, warn the user. Do not try
4017 to undo the user's settings, that would just be maddening. */
4018 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 4019 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
4020}
4021
be2a5f71
DJ
4022/* This type describes each known response to the qSupported
4023 packet. */
4024struct protocol_feature
4025{
4026 /* The name of this protocol feature. */
4027 const char *name;
4028
4029 /* The default for this protocol feature. */
4030 enum packet_support default_support;
4031
4032 /* The function to call when this feature is reported, or after
4033 qSupported processing if the feature is not supported.
4034 The first argument points to this structure. The second
4035 argument indicates whether the packet requested support be
4036 enabled, disabled, or probed (or the default, if this function
4037 is being called at the end of processing and this feature was
4038 not reported). The third argument may be NULL; if not NULL, it
4039 is a NUL-terminated string taken from the packet following
4040 this feature's name and an equals sign. */
4041 void (*func) (const struct protocol_feature *, enum packet_support,
4042 const char *);
4043
4044 /* The corresponding packet for this feature. Only used if
4045 FUNC is remote_supported_packet. */
4046 int packet;
4047};
4048
be2a5f71
DJ
4049static void
4050remote_supported_packet (const struct protocol_feature *feature,
4051 enum packet_support support,
4052 const char *argument)
4053{
4054 if (argument)
4055 {
4056 warning (_("Remote qSupported response supplied an unexpected value for"
4057 " \"%s\"."), feature->name);
4058 return;
4059 }
4060
4082afcc 4061 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4062}
be2a5f71
DJ
4063
4064static void
4065remote_packet_size (const struct protocol_feature *feature,
4066 enum packet_support support, const char *value)
4067{
4068 struct remote_state *rs = get_remote_state ();
4069
4070 int packet_size;
4071 char *value_end;
4072
4073 if (support != PACKET_ENABLE)
4074 return;
4075
4076 if (value == NULL || *value == '\0')
4077 {
4078 warning (_("Remote target reported \"%s\" without a size."),
4079 feature->name);
4080 return;
4081 }
4082
4083 errno = 0;
4084 packet_size = strtol (value, &value_end, 16);
4085 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4086 {
4087 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4088 feature->name, value);
4089 return;
4090 }
4091
4092 if (packet_size > MAX_REMOTE_PACKET_SIZE)
4093 {
4094 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
4095 packet_size, MAX_REMOTE_PACKET_SIZE);
4096 packet_size = MAX_REMOTE_PACKET_SIZE;
4097 }
4098
4099 /* Record the new maximum packet size. */
4100 rs->explicit_packet_size = packet_size;
4101}
4102
dc473cfb 4103static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4104 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4105 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4106 PACKET_qXfer_auxv },
c78fa86a
GB
4107 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4108 PACKET_qXfer_exec_file },
23181151
DJ
4109 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4110 PACKET_qXfer_features },
cfa9d6d9
DJ
4111 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4112 PACKET_qXfer_libraries },
2268b414
JK
4113 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4114 PACKET_qXfer_libraries_svr4 },
ced63ec0 4115 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4116 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4117 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4118 PACKET_qXfer_memory_map },
4de6483e
UW
4119 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4120 PACKET_qXfer_spu_read },
4121 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4122 PACKET_qXfer_spu_write },
07e059b5
VP
4123 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4124 PACKET_qXfer_osdata },
dc146f7c
VP
4125 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4126 PACKET_qXfer_threads },
b3b9301e
PA
4127 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4128 PACKET_qXfer_traceframe_info },
89be2091
DJ
4129 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4130 PACKET_QPassSignals },
9b224c5e
PA
4131 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4132 PACKET_QProgramSignals },
a6f3e723
SL
4133 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4134 PACKET_QStartNoAckMode },
4082afcc
PA
4135 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4136 PACKET_multiprocess_feature },
4137 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4138 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4139 PACKET_qXfer_siginfo_read },
4140 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4141 PACKET_qXfer_siginfo_write },
4082afcc 4142 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4143 PACKET_ConditionalTracepoints },
4082afcc 4144 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4145 PACKET_ConditionalBreakpoints },
4082afcc 4146 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4147 PACKET_BreakpointCommands },
4082afcc 4148 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4149 PACKET_FastTracepoints },
4082afcc 4150 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4151 PACKET_StaticTracepoints },
4082afcc 4152 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4153 PACKET_InstallInTrace},
4082afcc
PA
4154 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4155 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4156 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4157 PACKET_bc },
4158 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4159 PACKET_bs },
409873ef
SS
4160 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4161 PACKET_TracepointSource },
d914c394
SS
4162 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4163 PACKET_QAllow },
4082afcc
PA
4164 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4165 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4166 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4167 PACKET_qXfer_fdpic },
169081d0
TG
4168 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4169 PACKET_qXfer_uib },
03583c20
UW
4170 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4171 PACKET_QDisableRandomization },
d1feda86 4172 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4173 { "QTBuffer:size", PACKET_DISABLE,
4174 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4175 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4176 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4177 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
b20a6524 4178 { "Qbtrace:pt", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_pt },
9accd112 4179 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4180 PACKET_qXfer_btrace },
4181 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4182 PACKET_qXfer_btrace_conf },
4183 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4184 PACKET_Qbtrace_conf_bts_size },
4185 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4186 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4187 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4188 PACKET_fork_event_feature },
4189 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4190 PACKET_vfork_event_feature },
b20a6524
MM
4191 { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
4192 PACKET_Qbtrace_conf_pt_size }
be2a5f71
DJ
4193};
4194
c8d5aac9
L
4195static char *remote_support_xml;
4196
4197/* Register string appended to "xmlRegisters=" in qSupported query. */
4198
4199void
6e39997a 4200register_remote_support_xml (const char *xml)
c8d5aac9
L
4201{
4202#if defined(HAVE_LIBEXPAT)
4203 if (remote_support_xml == NULL)
c4f7c687 4204 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4205 else
4206 {
4207 char *copy = xstrdup (remote_support_xml + 13);
4208 char *p = strtok (copy, ",");
4209
4210 do
4211 {
4212 if (strcmp (p, xml) == 0)
4213 {
4214 /* already there */
4215 xfree (copy);
4216 return;
4217 }
4218 }
4219 while ((p = strtok (NULL, ",")) != NULL);
4220 xfree (copy);
4221
94b0dee1
PA
4222 remote_support_xml = reconcat (remote_support_xml,
4223 remote_support_xml, ",", xml,
4224 (char *) NULL);
c8d5aac9
L
4225 }
4226#endif
4227}
4228
4229static char *
4230remote_query_supported_append (char *msg, const char *append)
4231{
4232 if (msg)
94b0dee1 4233 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
4234 else
4235 return xstrdup (append);
4236}
4237
be2a5f71
DJ
4238static void
4239remote_query_supported (void)
4240{
4241 struct remote_state *rs = get_remote_state ();
4242 char *next;
4243 int i;
4244 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4245
4246 /* The packet support flags are handled differently for this packet
4247 than for most others. We treat an error, a disabled packet, and
4248 an empty response identically: any features which must be reported
4249 to be used will be automatically disabled. An empty buffer
4250 accomplishes this, since that is also the representation for a list
4251 containing no features. */
4252
4253 rs->buf[0] = 0;
4082afcc 4254 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 4255 {
c8d5aac9 4256 char *q = NULL;
94b0dee1 4257 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 4258
901f9912 4259 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9 4260
f7e6eed5
PA
4261 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4262 q = remote_query_supported_append (q, "swbreak+");
4263 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4264 q = remote_query_supported_append (q, "hwbreak+");
4265
c8d5aac9
L
4266 if (remote_support_xml)
4267 q = remote_query_supported_append (q, remote_support_xml);
4268
dde08ee1
PA
4269 q = remote_query_supported_append (q, "qRelocInsn+");
4270
89245bc0
DB
4271 if (rs->extended)
4272 {
4273 if (packet_set_cmd_state (PACKET_fork_event_feature)
4274 != AUTO_BOOLEAN_FALSE)
4275 q = remote_query_supported_append (q, "fork-events+");
4276 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4277 != AUTO_BOOLEAN_FALSE)
4278 q = remote_query_supported_append (q, "vfork-events+");
4279 }
4280
dde08ee1
PA
4281 q = reconcat (q, "qSupported:", q, (char *) NULL);
4282 putpkt (q);
82f73884 4283
94b0dee1
PA
4284 do_cleanups (old_chain);
4285
be2a5f71
DJ
4286 getpkt (&rs->buf, &rs->buf_size, 0);
4287
4288 /* If an error occured, warn, but do not return - just reset the
4289 buffer to empty and go on to disable features. */
4290 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4291 == PACKET_ERROR)
4292 {
4293 warning (_("Remote failure reply: %s"), rs->buf);
4294 rs->buf[0] = 0;
4295 }
4296 }
4297
4298 memset (seen, 0, sizeof (seen));
4299
4300 next = rs->buf;
4301 while (*next)
4302 {
4303 enum packet_support is_supported;
4304 char *p, *end, *name_end, *value;
4305
4306 /* First separate out this item from the rest of the packet. If
4307 there's another item after this, we overwrite the separator
4308 (terminated strings are much easier to work with). */
4309 p = next;
4310 end = strchr (p, ';');
4311 if (end == NULL)
4312 {
4313 end = p + strlen (p);
4314 next = end;
4315 }
4316 else
4317 {
89be2091
DJ
4318 *end = '\0';
4319 next = end + 1;
4320
be2a5f71
DJ
4321 if (end == p)
4322 {
4323 warning (_("empty item in \"qSupported\" response"));
4324 continue;
4325 }
be2a5f71
DJ
4326 }
4327
4328 name_end = strchr (p, '=');
4329 if (name_end)
4330 {
4331 /* This is a name=value entry. */
4332 is_supported = PACKET_ENABLE;
4333 value = name_end + 1;
4334 *name_end = '\0';
4335 }
4336 else
4337 {
4338 value = NULL;
4339 switch (end[-1])
4340 {
4341 case '+':
4342 is_supported = PACKET_ENABLE;
4343 break;
4344
4345 case '-':
4346 is_supported = PACKET_DISABLE;
4347 break;
4348
4349 case '?':
4350 is_supported = PACKET_SUPPORT_UNKNOWN;
4351 break;
4352
4353 default:
3e43a32a
MS
4354 warning (_("unrecognized item \"%s\" "
4355 "in \"qSupported\" response"), p);
be2a5f71
DJ
4356 continue;
4357 }
4358 end[-1] = '\0';
4359 }
4360
4361 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4362 if (strcmp (remote_protocol_features[i].name, p) == 0)
4363 {
4364 const struct protocol_feature *feature;
4365
4366 seen[i] = 1;
4367 feature = &remote_protocol_features[i];
4368 feature->func (feature, is_supported, value);
4369 break;
4370 }
4371 }
4372
4373 /* If we increased the packet size, make sure to increase the global
4374 buffer size also. We delay this until after parsing the entire
4375 qSupported packet, because this is the same buffer we were
4376 parsing. */
4377 if (rs->buf_size < rs->explicit_packet_size)
4378 {
4379 rs->buf_size = rs->explicit_packet_size;
4380 rs->buf = xrealloc (rs->buf, rs->buf_size);
4381 }
4382
4383 /* Handle the defaults for unmentioned features. */
4384 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4385 if (!seen[i])
4386 {
4387 const struct protocol_feature *feature;
4388
4389 feature = &remote_protocol_features[i];
4390 feature->func (feature, feature->default_support, NULL);
4391 }
4392}
4393
78a095c3
JK
4394/* Remove any of the remote.c targets from target stack. Upper targets depend
4395 on it so remove them first. */
4396
4397static void
4398remote_unpush_target (void)
4399{
4400 pop_all_targets_above (process_stratum - 1);
4401}
be2a5f71 4402
c906108c 4403static void
014f9477 4404remote_open_1 (const char *name, int from_tty,
3e43a32a 4405 struct target_ops *target, int extended_p)
c906108c 4406{
d01949b6 4407 struct remote_state *rs = get_remote_state ();
a6f3e723 4408
c906108c 4409 if (name == 0)
8a3fe4f8 4410 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 4411 "serial device is attached to the remote system\n"
8a3fe4f8 4412 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 4413
23860348 4414 /* See FIXME above. */
c6ebd6cf 4415 if (!target_async_permitted)
92d1e331 4416 wait_forever_enabled_p = 1;
6426a772 4417
2d717e4f 4418 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
4419 Ask this question first, before target_preopen has a chance to kill
4420 anything. */
5d93a237 4421 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 4422 {
78a095c3
JK
4423 if (from_tty
4424 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
4425 error (_("Still connected."));
4426 }
4427
78a095c3 4428 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
4429 target_preopen (from_tty);
4430
89be2091 4431 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
4432 xfree (rs->last_pass_packet);
4433 rs->last_pass_packet = NULL;
89be2091 4434
9b224c5e
PA
4435 /* Make sure we send the program signals list the next time we
4436 resume. */
5e4a05c4
TT
4437 xfree (rs->last_program_signals_packet);
4438 rs->last_program_signals_packet = NULL;
9b224c5e 4439
ad9a8f3f 4440 remote_fileio_reset ();
1dd41f16
NS
4441 reopen_exec_file ();
4442 reread_symbols ();
4443
5d93a237
TT
4444 rs->remote_desc = remote_serial_open (name);
4445 if (!rs->remote_desc)
c906108c
SS
4446 perror_with_name (name);
4447
4448 if (baud_rate != -1)
4449 {
5d93a237 4450 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 4451 {
9b74d5d3
KB
4452 /* The requested speed could not be set. Error out to
4453 top level after closing remote_desc. Take care to
4454 set remote_desc to NULL to avoid closing remote_desc
4455 more than once. */
5d93a237
TT
4456 serial_close (rs->remote_desc);
4457 rs->remote_desc = NULL;
c906108c
SS
4458 perror_with_name (name);
4459 }
4460 }
4461
236af5e3 4462 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 4463 serial_raw (rs->remote_desc);
c906108c
SS
4464
4465 /* If there is something sitting in the buffer we might take it as a
4466 response to a command, which would be bad. */
5d93a237 4467 serial_flush_input (rs->remote_desc);
c906108c
SS
4468
4469 if (from_tty)
4470 {
4471 puts_filtered ("Remote debugging using ");
4472 puts_filtered (name);
4473 puts_filtered ("\n");
4474 }
23860348 4475 push_target (target); /* Switch to using remote target now. */
c906108c 4476
74531fed
PA
4477 /* Register extra event sources in the event loop. */
4478 remote_async_inferior_event_token
4479 = create_async_event_handler (remote_async_inferior_event_handler,
4480 NULL);
5965e028 4481 rs->notif_state = remote_notif_state_allocate ();
74531fed 4482
be2a5f71
DJ
4483 /* Reset the target state; these things will be queried either by
4484 remote_query_supported or as they are needed. */
ca4f7f8b 4485 reset_all_packet_configs_support ();
74531fed 4486 rs->cached_wait_status = 0;
be2a5f71 4487 rs->explicit_packet_size = 0;
a6f3e723 4488 rs->noack_mode = 0;
82f73884 4489 rs->extended = extended_p;
e24a49d8 4490 rs->waiting_for_stop_reply = 0;
3a29589a 4491 rs->ctrlc_pending_p = 0;
802188a7 4492
47f8a51d
TT
4493 rs->general_thread = not_sent_ptid;
4494 rs->continue_thread = not_sent_ptid;
262e1174 4495 rs->remote_traceframe_number = -1;
c906108c 4496
9d1f7ab2 4497 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
4498 rs->use_threadinfo_query = 1;
4499 rs->use_threadextra_query = 1;
9d1f7ab2 4500
c6ebd6cf 4501 if (target_async_permitted)
92d1e331 4502 {
23860348 4503 /* With this target we start out by owning the terminal. */
92d1e331
DJ
4504 remote_async_terminal_ours_p = 1;
4505
4506 /* FIXME: cagney/1999-09-23: During the initial connection it is
4507 assumed that the target is already ready and able to respond to
0df8b418 4508 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 4509 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 4510 around this. Eventually a mechanism that allows
92d1e331 4511 wait_for_inferior() to expect/get timeouts will be
23860348 4512 implemented. */
92d1e331
DJ
4513 wait_forever_enabled_p = 0;
4514 }
4515
23860348 4516 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 4517 no_shared_libraries (NULL, 0);
f78f6cf1 4518
74531fed
PA
4519 /* Start afresh. */
4520 init_thread_list ();
4521
36918e70 4522 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
4523 target (we'd otherwise be in an inconsistent state) and then
4524 propogate the error on up the exception chain. This ensures that
4525 the caller doesn't stumble along blindly assuming that the
4526 function succeeded. The CLI doesn't have this problem but other
4527 UI's, such as MI do.
36918e70
AC
4528
4529 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4530 this function should return an error indication letting the
ce2826aa 4531 caller restore the previous state. Unfortunately the command
36918e70
AC
4532 ``target remote'' is directly wired to this function making that
4533 impossible. On a positive note, the CLI side of this problem has
4534 been fixed - the function set_cmd_context() makes it possible for
4535 all the ``target ....'' commands to share a common callback
4536 function. See cli-dump.c. */
109c3e39 4537 {
2d717e4f 4538
492d29ea 4539 TRY
04bd08de
TT
4540 {
4541 remote_start_remote (from_tty, target, extended_p);
4542 }
492d29ea 4543 CATCH (ex, RETURN_MASK_ALL)
109c3e39 4544 {
c8d104ad
PA
4545 /* Pop the partially set up target - unless something else did
4546 already before throwing the exception. */
5d93a237 4547 if (rs->remote_desc != NULL)
78a095c3 4548 remote_unpush_target ();
c6ebd6cf 4549 if (target_async_permitted)
109c3e39
AC
4550 wait_forever_enabled_p = 1;
4551 throw_exception (ex);
4552 }
492d29ea 4553 END_CATCH
109c3e39 4554 }
c906108c 4555
f4abbc16
MM
4556 remote_btrace_reset ();
4557
c6ebd6cf 4558 if (target_async_permitted)
92d1e331 4559 wait_forever_enabled_p = 1;
43ff13b4
JM
4560}
4561
de0d863e
DB
4562/* Detach the specified process. */
4563
4564static void
4565remote_detach_pid (int pid)
4566{
4567 struct remote_state *rs = get_remote_state ();
4568
4569 if (remote_multi_process_p (rs))
4570 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4571 else
4572 strcpy (rs->buf, "D");
4573
4574 putpkt (rs->buf);
4575 getpkt (&rs->buf, &rs->buf_size, 0);
4576
4577 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4578 ;
4579 else if (rs->buf[0] == '\0')
4580 error (_("Remote doesn't know how to detach"));
4581 else
4582 error (_("Can't detach process."));
4583}
4584
4585/* This detaches a program to which we previously attached, using
4586 inferior_ptid to identify the process. After this is done, GDB
4587 can be used to debug some other program. We better not have left
4588 any breakpoints in the target program or it'll die when it hits
4589 one. */
c906108c
SS
4590
4591static void
de0d863e 4592remote_detach_1 (const char *args, int from_tty)
c906108c 4593{
82f73884 4594 int pid = ptid_get_pid (inferior_ptid);
d01949b6 4595 struct remote_state *rs = get_remote_state ();
de0d863e
DB
4596 struct thread_info *tp = find_thread_ptid (inferior_ptid);
4597 int is_fork_parent;
c906108c
SS
4598
4599 if (args)
8a3fe4f8 4600 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 4601
2d717e4f
DJ
4602 if (!target_has_execution)
4603 error (_("No process to detach from."));
4604
7cee1e54
PA
4605 if (from_tty)
4606 {
4607 char *exec_file = get_exec_file (0);
4608 if (exec_file == NULL)
4609 exec_file = "";
4610 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4611 target_pid_to_str (pid_to_ptid (pid)));
4612 gdb_flush (gdb_stdout);
4613 }
4614
c906108c 4615 /* Tell the remote target to detach. */
de0d863e 4616 remote_detach_pid (pid);
82f73884 4617
de0d863e 4618 if (from_tty && !rs->extended)
7cee1e54 4619 puts_filtered (_("Ending remote debugging.\n"));
82f73884 4620
de0d863e
DB
4621 /* Check to see if we are detaching a fork parent. Note that if we
4622 are detaching a fork child, tp == NULL. */
4623 is_fork_parent = (tp != NULL
4624 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
4625
4626 /* If doing detach-on-fork, we don't mourn, because that will delete
4627 breakpoints that should be available for the followed inferior. */
4628 if (!is_fork_parent)
4629 target_mourn_inferior ();
4630 else
4631 {
4632 inferior_ptid = null_ptid;
4633 detach_inferior (pid);
4634 }
2d717e4f
DJ
4635}
4636
4637static void
52554a0e 4638remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4639{
de0d863e 4640 remote_detach_1 (args, from_tty);
2d717e4f
DJ
4641}
4642
4643static void
52554a0e 4644extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4645{
de0d863e
DB
4646 remote_detach_1 (args, from_tty);
4647}
4648
4649/* Target follow-fork function for remote targets. On entry, and
4650 at return, the current inferior is the fork parent.
4651
4652 Note that although this is currently only used for extended-remote,
4653 it is named remote_follow_fork in anticipation of using it for the
4654 remote target as well. */
4655
4656static int
4657remote_follow_fork (struct target_ops *ops, int follow_child,
4658 int detach_fork)
4659{
4660 struct remote_state *rs = get_remote_state ();
c269dbdb 4661 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 4662
c269dbdb
DB
4663 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
4664 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
4665 {
4666 /* When following the parent and detaching the child, we detach
4667 the child here. For the case of following the child and
4668 detaching the parent, the detach is done in the target-
4669 independent follow fork code in infrun.c. We can't use
4670 target_detach when detaching an unfollowed child because
4671 the client side doesn't know anything about the child. */
4672 if (detach_fork && !follow_child)
4673 {
4674 /* Detach the fork child. */
4675 ptid_t child_ptid;
4676 pid_t child_pid;
4677
4678 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
4679 child_pid = ptid_get_pid (child_ptid);
4680
4681 remote_detach_pid (child_pid);
4682 detach_inferior (child_pid);
4683 }
4684 }
4685 return 0;
c906108c
SS
4686}
4687
6ad8ae5c
DJ
4688/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4689
43ff13b4 4690static void
fee354ee 4691remote_disconnect (struct target_ops *target, const char *args, int from_tty)
43ff13b4 4692{
43ff13b4 4693 if (args)
2d717e4f 4694 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 4695
2d717e4f 4696 /* Make sure we unpush even the extended remote targets; mourn
20f796c9 4697 won't do it. So call remote_mourn directly instead of
2d717e4f 4698 target_mourn_inferior. */
20f796c9 4699 remote_mourn (target);
2d717e4f 4700
43ff13b4
JM
4701 if (from_tty)
4702 puts_filtered ("Ending remote debugging.\n");
4703}
4704
2d717e4f
DJ
4705/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4706 be chatty about it. */
4707
4708static void
20f796c9
GB
4709extended_remote_attach (struct target_ops *target, const char *args,
4710 int from_tty)
2d717e4f
DJ
4711{
4712 struct remote_state *rs = get_remote_state ();
be86555c 4713 int pid;
96ef3384 4714 char *wait_status = NULL;
2d717e4f 4715
74164c56 4716 pid = parse_pid_to_attach (args);
2d717e4f 4717
74164c56
JK
4718 /* Remote PID can be freely equal to getpid, do not check it here the same
4719 way as in other targets. */
2d717e4f 4720
4082afcc 4721 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
4722 error (_("This target does not support attaching to a process"));
4723
7cee1e54
PA
4724 if (from_tty)
4725 {
4726 char *exec_file = get_exec_file (0);
4727
4728 if (exec_file)
4729 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4730 target_pid_to_str (pid_to_ptid (pid)));
4731 else
4732 printf_unfiltered (_("Attaching to %s\n"),
4733 target_pid_to_str (pid_to_ptid (pid)));
4734
4735 gdb_flush (gdb_stdout);
4736 }
4737
bba74b36 4738 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
4739 putpkt (rs->buf);
4740 getpkt (&rs->buf, &rs->buf_size, 0);
4741
4082afcc
PA
4742 switch (packet_ok (rs->buf,
4743 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 4744 {
4082afcc 4745 case PACKET_OK:
74531fed
PA
4746 if (!non_stop)
4747 {
4748 /* Save the reply for later. */
4749 wait_status = alloca (strlen (rs->buf) + 1);
4750 strcpy (wait_status, rs->buf);
4751 }
4752 else if (strcmp (rs->buf, "OK") != 0)
4753 error (_("Attaching to %s failed with: %s"),
4754 target_pid_to_str (pid_to_ptid (pid)),
4755 rs->buf);
4082afcc
PA
4756 break;
4757 case PACKET_UNKNOWN:
4758 error (_("This target does not support attaching to a process"));
4759 default:
4760 error (_("Attaching to %s failed"),
4761 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 4762 }
2d717e4f 4763
1b6e6f5c 4764 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 4765
2d717e4f 4766 inferior_ptid = pid_to_ptid (pid);
79d7f229 4767
bad34192
PA
4768 if (non_stop)
4769 {
4770 struct thread_info *thread;
79d7f229 4771
bad34192 4772 /* Get list of threads. */
e8032dde 4773 remote_update_thread_list (target);
82f73884 4774
bad34192
PA
4775 thread = first_thread_of_process (pid);
4776 if (thread)
4777 inferior_ptid = thread->ptid;
4778 else
4779 inferior_ptid = pid_to_ptid (pid);
4780
4781 /* Invalidate our notion of the remote current thread. */
47f8a51d 4782 record_currthread (rs, minus_one_ptid);
bad34192 4783 }
74531fed 4784 else
bad34192
PA
4785 {
4786 /* Now, if we have thread information, update inferior_ptid. */
4787 inferior_ptid = remote_current_thread (inferior_ptid);
4788
4789 /* Add the main thread to the thread list. */
4790 add_thread_silent (inferior_ptid);
4791 }
c0a2216e 4792
96ef3384
UW
4793 /* Next, if the target can specify a description, read it. We do
4794 this before anything involving memory or registers. */
4795 target_find_description ();
4796
74531fed
PA
4797 if (!non_stop)
4798 {
4799 /* Use the previously fetched status. */
4800 gdb_assert (wait_status != NULL);
4801
4802 if (target_can_async_p ())
4803 {
722247f1
YQ
4804 struct notif_event *reply
4805 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 4806
722247f1 4807 push_stop_reply ((struct stop_reply *) reply);
74531fed 4808
6a3753b3 4809 target_async (1);
74531fed
PA
4810 }
4811 else
4812 {
4813 gdb_assert (wait_status != NULL);
4814 strcpy (rs->buf, wait_status);
4815 rs->cached_wait_status = 1;
4816 }
4817 }
4818 else
4819 gdb_assert (wait_status == NULL);
2d717e4f
DJ
4820}
4821
b9c1d481
AS
4822/* Implementation of the to_post_attach method. */
4823
4824static void
4825extended_remote_post_attach (struct target_ops *ops, int pid)
4826{
4827 /* In certain cases GDB might not have had the chance to start
4828 symbol lookup up until now. This could happen if the debugged
4829 binary is not using shared libraries, the vsyscall page is not
4830 present (on Linux) and the binary itself hadn't changed since the
4831 debugging process was started. */
4832 if (symfile_objfile != NULL)
4833 remote_check_symbols();
4834}
4835
c906108c 4836\f
506fb367
DJ
4837/* Check for the availability of vCont. This function should also check
4838 the response. */
c906108c
SS
4839
4840static void
6d820c5c 4841remote_vcont_probe (struct remote_state *rs)
c906108c 4842{
2e9f7625 4843 char *buf;
6d820c5c 4844
2e9f7625
DJ
4845 strcpy (rs->buf, "vCont?");
4846 putpkt (rs->buf);
6d820c5c 4847 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 4848 buf = rs->buf;
c906108c 4849
506fb367 4850 /* Make sure that the features we assume are supported. */
61012eef 4851 if (startswith (buf, "vCont"))
506fb367
DJ
4852 {
4853 char *p = &buf[5];
4854 int support_s, support_S, support_c, support_C;
4855
4856 support_s = 0;
4857 support_S = 0;
4858 support_c = 0;
4859 support_C = 0;
d458bd84 4860 rs->supports_vCont.t = 0;
c1e36e3e 4861 rs->supports_vCont.r = 0;
506fb367
DJ
4862 while (p && *p == ';')
4863 {
4864 p++;
4865 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4866 support_s = 1;
4867 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4868 support_S = 1;
4869 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4870 support_c = 1;
4871 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4872 support_C = 1;
74531fed 4873 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 4874 rs->supports_vCont.t = 1;
c1e36e3e
PA
4875 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
4876 rs->supports_vCont.r = 1;
506fb367
DJ
4877
4878 p = strchr (p, ';');
4879 }
c906108c 4880
506fb367
DJ
4881 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4882 BUF will make packet_ok disable the packet. */
4883 if (!support_s || !support_S || !support_c || !support_C)
4884 buf[0] = 0;
4885 }
c906108c 4886
444abaca 4887 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 4888}
c906108c 4889
0d8f58ca
PA
4890/* Helper function for building "vCont" resumptions. Write a
4891 resumption to P. ENDP points to one-passed-the-end of the buffer
4892 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4893 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4894 resumed thread should be single-stepped and/or signalled. If PTID
4895 equals minus_one_ptid, then all threads are resumed; if PTID
4896 represents a process, then all threads of the process are resumed;
4897 the thread to be stepped and/or signalled is given in the global
4898 INFERIOR_PTID. */
4899
4900static char *
4901append_resumption (char *p, char *endp,
2ea28649 4902 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
4903{
4904 struct remote_state *rs = get_remote_state ();
4905
a493e3e2 4906 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 4907 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
4908 else if (step
4909 /* GDB is willing to range step. */
4910 && use_range_stepping
4911 /* Target supports range stepping. */
4912 && rs->supports_vCont.r
4913 /* We don't currently support range stepping multiple
4914 threads with a wildcard (though the protocol allows it,
4915 so stubs shouldn't make an active effort to forbid
4916 it). */
4917 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4918 {
4919 struct thread_info *tp;
4920
4921 if (ptid_equal (ptid, minus_one_ptid))
4922 {
4923 /* If we don't know about the target thread's tid, then
4924 we're resuming magic_null_ptid (see caller). */
4925 tp = find_thread_ptid (magic_null_ptid);
4926 }
4927 else
4928 tp = find_thread_ptid (ptid);
4929 gdb_assert (tp != NULL);
4930
4931 if (tp->control.may_range_step)
4932 {
4933 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
4934
4935 p += xsnprintf (p, endp - p, ";r%s,%s",
4936 phex_nz (tp->control.step_range_start,
4937 addr_size),
4938 phex_nz (tp->control.step_range_end,
4939 addr_size));
4940 }
4941 else
4942 p += xsnprintf (p, endp - p, ";s");
4943 }
0d8f58ca
PA
4944 else if (step)
4945 p += xsnprintf (p, endp - p, ";s");
a493e3e2 4946 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
4947 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4948 else
4949 p += xsnprintf (p, endp - p, ";c");
4950
4951 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4952 {
4953 ptid_t nptid;
4954
4955 /* All (-1) threads of process. */
ba348170 4956 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
4957
4958 p += xsnprintf (p, endp - p, ":");
4959 p = write_ptid (p, endp, nptid);
4960 }
4961 else if (!ptid_equal (ptid, minus_one_ptid))
4962 {
4963 p += xsnprintf (p, endp - p, ":");
4964 p = write_ptid (p, endp, ptid);
4965 }
4966
4967 return p;
4968}
4969
e5ef252a
PA
4970/* Append a vCont continue-with-signal action for threads that have a
4971 non-zero stop signal. */
4972
4973static char *
4974append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4975{
4976 struct thread_info *thread;
4977
034f788c 4978 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
4979 if (ptid_match (thread->ptid, ptid)
4980 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 4981 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
4982 {
4983 p = append_resumption (p, endp, thread->ptid,
4984 0, thread->suspend.stop_signal);
4985 thread->suspend.stop_signal = GDB_SIGNAL_0;
4986 }
4987
4988 return p;
4989}
4990
506fb367
DJ
4991/* Resume the remote inferior by using a "vCont" packet. The thread
4992 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
4993 resumed thread should be single-stepped and/or signalled. If PTID
4994 equals minus_one_ptid, then all threads are resumed; the thread to
4995 be stepped and/or signalled is given in the global INFERIOR_PTID.
4996 This function returns non-zero iff it resumes the inferior.
44eaed12 4997
506fb367
DJ
4998 This function issues a strict subset of all possible vCont commands at the
4999 moment. */
44eaed12 5000
506fb367 5001static int
2ea28649 5002remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
5003{
5004 struct remote_state *rs = get_remote_state ();
82f73884
PA
5005 char *p;
5006 char *endp;
44eaed12 5007
4082afcc 5008 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 5009 remote_vcont_probe (rs);
44eaed12 5010
4082afcc 5011 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 5012 return 0;
44eaed12 5013
82f73884
PA
5014 p = rs->buf;
5015 endp = rs->buf + get_remote_packet_size ();
5016
506fb367
DJ
5017 /* If we could generate a wider range of packets, we'd have to worry
5018 about overflowing BUF. Should there be a generic
5019 "multi-part-packet" packet? */
5020
0d8f58ca
PA
5021 p += xsnprintf (p, endp - p, "vCont");
5022
79d7f229 5023 if (ptid_equal (ptid, magic_null_ptid))
c906108c 5024 {
79d7f229
PA
5025 /* MAGIC_NULL_PTID means that we don't have any active threads,
5026 so we don't have any TID numbers the inferior will
5027 understand. Make sure to only send forms that do not specify
5028 a TID. */
a9cbf802 5029 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 5030 }
0d8f58ca 5031 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 5032 {
0d8f58ca
PA
5033 /* Resume all threads (of all processes, or of a single
5034 process), with preference for INFERIOR_PTID. This assumes
5035 inferior_ptid belongs to the set of all threads we are about
5036 to resume. */
a493e3e2 5037 if (step || siggnal != GDB_SIGNAL_0)
82f73884 5038 {
0d8f58ca
PA
5039 /* Step inferior_ptid, with or without signal. */
5040 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5041 }
0d8f58ca 5042
e5ef252a
PA
5043 /* Also pass down any pending signaled resumption for other
5044 threads not the current. */
5045 p = append_pending_thread_resumptions (p, endp, ptid);
5046
0d8f58ca 5047 /* And continue others without a signal. */
a493e3e2 5048 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5049 }
5050 else
506fb367
DJ
5051 {
5052 /* Scheduler locking; resume only PTID. */
a9cbf802 5053 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5054 }
c906108c 5055
82f73884
PA
5056 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5057 putpkt (rs->buf);
506fb367 5058
74531fed
PA
5059 if (non_stop)
5060 {
5061 /* In non-stop, the stub replies to vCont with "OK". The stop
5062 reply will be reported asynchronously by means of a `%Stop'
5063 notification. */
5064 getpkt (&rs->buf, &rs->buf_size, 0);
5065 if (strcmp (rs->buf, "OK") != 0)
5066 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5067 }
5068
506fb367 5069 return 1;
c906108c 5070}
43ff13b4 5071
506fb367
DJ
5072/* Tell the remote machine to resume. */
5073
43ff13b4 5074static void
28439f5e 5075remote_resume (struct target_ops *ops,
2ea28649 5076 ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 5077{
d01949b6 5078 struct remote_state *rs = get_remote_state ();
2e9f7625 5079 char *buf;
43ff13b4 5080
722247f1
YQ
5081 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
5082 (explained in remote-notif.c:handle_notification) so
5083 remote_notif_process is not called. We need find a place where
5084 it is safe to start a 'vNotif' sequence. It is good to do it
5085 before resuming inferior, because inferior was stopped and no RSP
5086 traffic at that moment. */
5087 if (!non_stop)
5965e028 5088 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 5089
b73be471 5090 rs->last_sent_signal = siggnal;
280ceea3 5091 rs->last_sent_step = step;
43ff13b4 5092
506fb367 5093 /* The vCont packet doesn't need to specify threads via Hc. */
40ab02ce
MS
5094 /* No reverse support (yet) for vCont. */
5095 if (execution_direction != EXEC_REVERSE)
5096 if (remote_vcont_resume (ptid, step, siggnal))
5097 goto done;
506fb367 5098
79d7f229
PA
5099 /* All other supported resume packets do use Hc, so set the continue
5100 thread. */
5101 if (ptid_equal (ptid, minus_one_ptid))
5102 set_continue_thread (any_thread_ptid);
506fb367 5103 else
79d7f229 5104 set_continue_thread (ptid);
506fb367 5105
2e9f7625 5106 buf = rs->buf;
b2175913
MS
5107 if (execution_direction == EXEC_REVERSE)
5108 {
5109 /* We don't pass signals to the target in reverse exec mode. */
a493e3e2 5110 if (info_verbose && siggnal != GDB_SIGNAL_0)
7ea6d463 5111 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
b2175913 5112 siggnal);
40ab02ce 5113
4082afcc 5114 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
40ab02ce 5115 error (_("Remote reverse-step not supported."));
4082afcc 5116 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
08c93ed9 5117 error (_("Remote reverse-continue not supported."));
40ab02ce 5118
b2175913
MS
5119 strcpy (buf, step ? "bs" : "bc");
5120 }
a493e3e2 5121 else if (siggnal != GDB_SIGNAL_0)
43ff13b4
JM
5122 {
5123 buf[0] = step ? 'S' : 'C';
c5aa993b 5124 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 5125 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
5126 buf[3] = '\0';
5127 }
5128 else
c5aa993b 5129 strcpy (buf, step ? "s" : "c");
506fb367 5130
44eaed12 5131 putpkt (buf);
43ff13b4 5132
75c99385 5133 done:
2acceee2 5134 /* We are about to start executing the inferior, let's register it
0df8b418
MS
5135 with the event loop. NOTE: this is the one place where all the
5136 execution commands end up. We could alternatively do this in each
23860348 5137 of the execution commands in infcmd.c. */
2acceee2
JM
5138 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
5139 into infcmd.c in order to allow inferior function calls to work
23860348 5140 NOT asynchronously. */
362646f5 5141 if (target_can_async_p ())
6a3753b3 5142 target_async (1);
e24a49d8
PA
5143
5144 /* We've just told the target to resume. The remote server will
5145 wait for the inferior to stop, and then send a stop reply. In
5146 the mean time, we can't start another command/query ourselves
74531fed
PA
5147 because the stub wouldn't be ready to process it. This applies
5148 only to the base all-stop protocol, however. In non-stop (which
5149 only supports vCont), the stub replies with an "OK", and is
5150 immediate able to process further serial input. */
5151 if (!non_stop)
5152 rs->waiting_for_stop_reply = 1;
43ff13b4 5153}
c906108c 5154\f
43ff13b4
JM
5155
5156/* Set up the signal handler for SIGINT, while the target is
23860348 5157 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 5158static void
934b9bac 5159async_initialize_sigint_signal_handler (void)
43ff13b4 5160{
934b9bac 5161 signal (SIGINT, async_handle_remote_sigint);
43ff13b4
JM
5162}
5163
23860348 5164/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 5165static void
934b9bac 5166async_handle_remote_sigint (int sig)
43ff13b4 5167{
934b9bac 5168 signal (sig, async_handle_remote_sigint_twice);
b2ee242b
PA
5169 /* Note we need to go through gdb_call_async_signal_handler in order
5170 to wake up the event loop on Windows. */
5171 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
43ff13b4
JM
5172}
5173
5174/* Signal handler for SIGINT, installed after SIGINT has already been
5175 sent once. It will take effect the second time that the user sends
23860348 5176 a ^C. */
43ff13b4 5177static void
934b9bac 5178async_handle_remote_sigint_twice (int sig)
43ff13b4 5179{
934b9bac 5180 signal (sig, async_handle_remote_sigint);
b2ee242b
PA
5181 /* See note in async_handle_remote_sigint. */
5182 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
43ff13b4
JM
5183}
5184
6426a772 5185/* Perform the real interruption of the target execution, in response
23860348 5186 to a ^C. */
c5aa993b 5187static void
fba45db2 5188async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
5189{
5190 if (remote_debug)
248fd3bf 5191 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
43ff13b4 5192
94cc34af 5193 target_stop (inferior_ptid);
43ff13b4
JM
5194}
5195
0df8b418 5196/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 5197 up on the target alltogether. */
47e1ce27 5198static void
fba45db2 5199async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 5200{
2df3850c 5201 if (remote_debug)
248fd3bf 5202 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
b803fb0f
DJ
5203
5204 interrupt_query ();
43ff13b4
JM
5205}
5206
5207/* Reinstall the usual SIGINT handlers, after the target has
23860348 5208 stopped. */
6426a772 5209static void
934b9bac 5210async_cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
5211{
5212 signal (SIGINT, handle_sigint);
43ff13b4
JM
5213}
5214
c906108c
SS
5215/* Send ^C to target to halt it. Target will respond, and send us a
5216 packet. */
507f3c78 5217static void (*ofunc) (int);
c906108c 5218
bfedc46a
PA
5219/* The command line interface's interrupt routine. This function is installed
5220 as a signal handler for SIGINT. The first time a user requests an
5221 interrupt, we call remote_interrupt to send a break or ^C. If there is no
7a292a7a 5222 response from the target (it didn't stop when the user requested it),
23860348 5223 we ask the user if he'd like to detach from the target. */
bfedc46a 5224
c906108c 5225static void
934b9bac 5226sync_remote_interrupt (int signo)
c906108c 5227{
23860348 5228 /* If this doesn't work, try more severe steps. */
934b9bac 5229 signal (signo, sync_remote_interrupt_twice);
7a292a7a 5230
934b9bac 5231 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
7a292a7a
SS
5232}
5233
5234/* The user typed ^C twice. */
5235
5236static void
934b9bac 5237sync_remote_interrupt_twice (int signo)
7a292a7a
SS
5238{
5239 signal (signo, ofunc);
934b9bac
JK
5240 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
5241 signal (signo, sync_remote_interrupt);
c906108c 5242}
7a292a7a 5243
74531fed
PA
5244/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5245 thread, all threads of a remote process, or all threads of all
5246 processes. */
5247
5248static void
5249remote_stop_ns (ptid_t ptid)
5250{
5251 struct remote_state *rs = get_remote_state ();
5252 char *p = rs->buf;
5253 char *endp = rs->buf + get_remote_packet_size ();
74531fed 5254
4082afcc 5255 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
5256 remote_vcont_probe (rs);
5257
d458bd84 5258 if (!rs->supports_vCont.t)
74531fed
PA
5259 error (_("Remote server does not support stopping threads"));
5260
f91d3df5
PA
5261 if (ptid_equal (ptid, minus_one_ptid)
5262 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
5263 p += xsnprintf (p, endp - p, "vCont;t");
5264 else
5265 {
5266 ptid_t nptid;
5267
74531fed
PA
5268 p += xsnprintf (p, endp - p, "vCont;t:");
5269
5270 if (ptid_is_pid (ptid))
5271 /* All (-1) threads of process. */
ba348170 5272 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
5273 else
5274 {
5275 /* Small optimization: if we already have a stop reply for
5276 this thread, no use in telling the stub we want this
5277 stopped. */
5278 if (peek_stop_reply (ptid))
5279 return;
5280
5281 nptid = ptid;
5282 }
5283
a9cbf802 5284 write_ptid (p, endp, nptid);
74531fed
PA
5285 }
5286
5287 /* In non-stop, we get an immediate OK reply. The stop reply will
5288 come in asynchronously by notification. */
5289 putpkt (rs->buf);
5290 getpkt (&rs->buf, &rs->buf_size, 0);
5291 if (strcmp (rs->buf, "OK") != 0)
5292 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
5293}
5294
bfedc46a
PA
5295/* All-stop version of target_interrupt. Sends a break or a ^C to
5296 interrupt the remote target. It is undefined which thread of which
5297 process reports the interrupt. */
74531fed
PA
5298
5299static void
bfedc46a 5300remote_interrupt_as (ptid_t ptid)
74531fed
PA
5301{
5302 struct remote_state *rs = get_remote_state ();
5303
3a29589a
DJ
5304 rs->ctrlc_pending_p = 1;
5305
74531fed
PA
5306 /* If the inferior is stopped already, but the core didn't know
5307 about it yet, just ignore the request. The cached wait status
5308 will be collected in remote_wait. */
5309 if (rs->cached_wait_status)
5310 return;
5311
9a7071a8
JB
5312 /* Send interrupt_sequence to remote target. */
5313 send_interrupt_sequence ();
74531fed
PA
5314}
5315
bfedc46a 5316/* Implement the to_stop function for the remote targets. */
74531fed 5317
c906108c 5318static void
1eab8a48 5319remote_stop (struct target_ops *self, ptid_t ptid)
c906108c 5320{
7a292a7a 5321 if (remote_debug)
0f71a2f6 5322 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 5323
74531fed
PA
5324 if (non_stop)
5325 remote_stop_ns (ptid);
c906108c 5326 else
bfedc46a
PA
5327 {
5328 /* We don't currently have a way to transparently pause the
5329 remote target in all-stop mode. Interrupt it instead. */
5330 remote_interrupt_as (ptid);
5331 }
5332}
5333
5334/* Implement the to_interrupt function for the remote targets. */
5335
5336static void
5337remote_interrupt (struct target_ops *self, ptid_t ptid)
5338{
5339 if (remote_debug)
5340 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
5341
5342 if (non_stop)
5343 {
5344 /* We don't currently have a way to ^C the remote target in
5345 non-stop mode. Stop it (with no signal) instead. */
5346 remote_stop_ns (ptid);
5347 }
5348 else
5349 remote_interrupt_as (ptid);
c906108c
SS
5350}
5351
5352/* Ask the user what to do when an interrupt is received. */
5353
5354static void
fba45db2 5355interrupt_query (void)
c906108c
SS
5356{
5357 target_terminal_ours ();
5358
d9d41e78 5359 if (target_is_async_p ())
c906108c 5360 {
74531fed 5361 signal (SIGINT, handle_sigint);
039e3c22 5362 quit ();
c906108c 5363 }
74531fed
PA
5364 else
5365 {
9e2f0ad4
HZ
5366 if (query (_("Interrupted while waiting for the program.\n\
5367Give up (and stop debugging it)? ")))
74531fed 5368 {
78a095c3 5369 remote_unpush_target ();
039e3c22 5370 quit ();
74531fed
PA
5371 }
5372 }
c906108c
SS
5373
5374 target_terminal_inferior ();
5375}
5376
6426a772
JM
5377/* Enable/disable target terminal ownership. Most targets can use
5378 terminal groups to control terminal ownership. Remote targets are
5379 different in that explicit transfer of ownership to/from GDB/target
23860348 5380 is required. */
6426a772
JM
5381
5382static void
d2f640d4 5383remote_terminal_inferior (struct target_ops *self)
6426a772 5384{
c6ebd6cf 5385 if (!target_async_permitted)
75c99385
PA
5386 /* Nothing to do. */
5387 return;
5388
d9d2d8b6
PA
5389 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5390 idempotent. The event-loop GDB talking to an asynchronous target
5391 with a synchronous command calls this function from both
5392 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5393 transfer the terminal to the target when it shouldn't this guard
5394 can go away. */
6426a772
JM
5395 if (!remote_async_terminal_ours_p)
5396 return;
5397 delete_file_handler (input_fd);
5398 remote_async_terminal_ours_p = 0;
934b9bac 5399 async_initialize_sigint_signal_handler ();
6426a772
JM
5400 /* NOTE: At this point we could also register our selves as the
5401 recipient of all input. Any characters typed could then be
23860348 5402 passed on down to the target. */
6426a772
JM
5403}
5404
5405static void
e3594fd1 5406remote_terminal_ours (struct target_ops *self)
6426a772 5407{
c6ebd6cf 5408 if (!target_async_permitted)
75c99385
PA
5409 /* Nothing to do. */
5410 return;
5411
5412 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
5413 if (remote_async_terminal_ours_p)
5414 return;
934b9bac 5415 async_cleanup_sigint_signal_handler (NULL);
6426a772
JM
5416 add_file_handler (input_fd, stdin_event_handler, 0);
5417 remote_async_terminal_ours_p = 1;
5418}
5419
176a6961 5420static void
917317f4 5421remote_console_output (char *msg)
c906108c
SS
5422{
5423 char *p;
5424
c5aa993b 5425 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
5426 {
5427 char tb[2];
5428 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 5429
c906108c
SS
5430 tb[0] = c;
5431 tb[1] = 0;
43ff13b4 5432 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 5433 }
00db5b94
PA
5434 gdb_flush (gdb_stdtarg);
5435}
74531fed
PA
5436
5437typedef struct cached_reg
5438{
5439 int num;
5440 gdb_byte data[MAX_REGISTER_SIZE];
5441} cached_reg_t;
5442
5443DEF_VEC_O(cached_reg_t);
5444
722247f1 5445typedef struct stop_reply
74531fed 5446{
722247f1 5447 struct notif_event base;
74531fed 5448
722247f1 5449 /* The identifier of the thread about this event */
74531fed
PA
5450 ptid_t ptid;
5451
340e3c99 5452 /* The remote state this event is associated with. When the remote
bcc75809
YQ
5453 connection, represented by a remote_state object, is closed,
5454 all the associated stop_reply events should be released. */
5455 struct remote_state *rs;
5456
74531fed
PA
5457 struct target_waitstatus ws;
5458
15148d6a
PA
5459 /* Expedited registers. This makes remote debugging a bit more
5460 efficient for those targets that provide critical registers as
5461 part of their normal status mechanism (as another roundtrip to
5462 fetch them is avoided). */
74531fed
PA
5463 VEC(cached_reg_t) *regcache;
5464
f7e6eed5
PA
5465 enum target_stop_reason stop_reason;
5466
74531fed
PA
5467 CORE_ADDR watch_data_address;
5468
dc146f7c 5469 int core;
722247f1 5470} *stop_reply_p;
a744cf53 5471
722247f1
YQ
5472DECLARE_QUEUE_P (stop_reply_p);
5473DEFINE_QUEUE_P (stop_reply_p);
5474/* The list of already fetched and acknowledged stop events. This
5475 queue is used for notification Stop, and other notifications
5476 don't need queue for their events, because the notification events
5477 of Stop can't be consumed immediately, so that events should be
5478 queued first, and be consumed by remote_wait_{ns,as} one per
5479 time. Other notifications can consume their events immediately,
5480 so queue is not needed for them. */
5481static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
5482
5483static void
5484stop_reply_xfree (struct stop_reply *r)
5485{
f48ff2a7 5486 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
5487}
5488
722247f1
YQ
5489static void
5490remote_notif_stop_parse (struct notif_client *self, char *buf,
5491 struct notif_event *event)
5492{
5493 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5494}
5495
5496static void
5497remote_notif_stop_ack (struct notif_client *self, char *buf,
5498 struct notif_event *event)
5499{
5500 struct stop_reply *stop_reply = (struct stop_reply *) event;
5501
5502 /* acknowledge */
5503 putpkt ((char *) self->ack_command);
5504
5505 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5506 /* We got an unknown stop reply. */
5507 error (_("Unknown stop reply"));
5508
5509 push_stop_reply (stop_reply);
5510}
5511
5512static int
5513remote_notif_stop_can_get_pending_events (struct notif_client *self)
5514{
5515 /* We can't get pending events in remote_notif_process for
5516 notification stop, and we have to do this in remote_wait_ns
5517 instead. If we fetch all queued events from stub, remote stub
5518 may exit and we have no chance to process them back in
5519 remote_wait_ns. */
5520 mark_async_event_handler (remote_async_inferior_event_token);
5521 return 0;
5522}
5523
5524static void
5525stop_reply_dtr (struct notif_event *event)
5526{
5527 struct stop_reply *r = (struct stop_reply *) event;
5528
5529 VEC_free (cached_reg_t, r->regcache);
5530}
5531
5532static struct notif_event *
5533remote_notif_stop_alloc_reply (void)
5534{
5535 struct notif_event *r
70ba0933 5536 = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
5537
5538 r->dtr = stop_reply_dtr;
5539
5540 return r;
5541}
5542
5543/* A client of notification Stop. */
5544
5545struct notif_client notif_client_stop =
5546{
5547 "Stop",
5548 "vStopped",
5549 remote_notif_stop_parse,
5550 remote_notif_stop_ack,
5551 remote_notif_stop_can_get_pending_events,
5552 remote_notif_stop_alloc_reply,
f48ff2a7 5553 REMOTE_NOTIF_STOP,
722247f1
YQ
5554};
5555
5556/* A parameter to pass data in and out. */
5557
5558struct queue_iter_param
5559{
5560 void *input;
5561 struct stop_reply *output;
5562};
5563
cbb8991c
DB
5564/* Determine if THREAD is a pending fork parent thread. ARG contains
5565 the pid of the process that owns the threads we want to check, or
5566 -1 if we want to check all threads. */
5567
5568static int
5569is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
5570 ptid_t thread_ptid)
5571{
5572 if (ws->kind == TARGET_WAITKIND_FORKED
5573 || ws->kind == TARGET_WAITKIND_VFORKED)
5574 {
5575 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
5576 return 1;
5577 }
5578
5579 return 0;
5580}
5581
5582/* Check whether EVENT is a fork event, and if it is, remove the
5583 fork child from the context list passed in DATA. */
5584
5585static int
5586remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
5587 QUEUE_ITER (stop_reply_p) *iter,
5588 stop_reply_p event,
5589 void *data)
5590{
5591 struct queue_iter_param *param = data;
5592 struct threads_listing_context *context = param->input;
5593
5594 if (event->ws.kind == TARGET_WAITKIND_FORKED
5595 || event->ws.kind == TARGET_WAITKIND_VFORKED)
5596 {
5597 threads_listing_context_remove (&event->ws, context);
5598 }
5599
5600 return 1;
5601}
5602
5603/* If CONTEXT contains any fork child threads that have not been
5604 reported yet, remove them from the CONTEXT list. If such a
5605 thread exists it is because we are stopped at a fork catchpoint
5606 and have not yet called follow_fork, which will set up the
5607 host-side data structures for the new process. */
5608
5609static void
5610remove_new_fork_children (struct threads_listing_context *context)
5611{
5612 struct thread_info * thread;
5613 int pid = -1;
5614 struct notif_client *notif = &notif_client_stop;
5615 struct queue_iter_param param;
5616
5617 /* For any threads stopped at a fork event, remove the corresponding
5618 fork child threads from the CONTEXT list. */
5619 ALL_NON_EXITED_THREADS (thread)
5620 {
5621 struct target_waitstatus *ws = &thread->pending_follow;
5622
5623 if (is_pending_fork_parent (ws, pid, thread->ptid))
5624 {
5625 threads_listing_context_remove (ws, context);
5626 }
5627 }
5628
5629 /* Check for any pending fork events (not reported or processed yet)
5630 in process PID and remove those fork child threads from the
5631 CONTEXT list as well. */
5632 remote_notif_get_pending_events (notif);
5633 param.input = context;
5634 param.output = NULL;
5635 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5636 remove_child_of_pending_fork, &param);
5637}
5638
f48ff2a7
YQ
5639/* Remove stop replies in the queue if its pid is equal to the given
5640 inferior's pid. */
722247f1
YQ
5641
5642static int
f48ff2a7
YQ
5643remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5644 QUEUE_ITER (stop_reply_p) *iter,
5645 stop_reply_p event,
5646 void *data)
722247f1
YQ
5647{
5648 struct queue_iter_param *param = data;
5649 struct inferior *inf = param->input;
5650
f48ff2a7 5651 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
5652 {
5653 stop_reply_xfree (event);
5654 QUEUE_remove_elem (stop_reply_p, q, iter);
5655 }
5656
5657 return 1;
5658}
5659
f48ff2a7 5660/* Discard all pending stop replies of inferior INF. */
c906108c 5661
74531fed 5662static void
5f4cf0bb 5663discard_pending_stop_replies (struct inferior *inf)
c906108c 5664{
722247f1
YQ
5665 int i;
5666 struct queue_iter_param param;
f48ff2a7
YQ
5667 struct stop_reply *reply;
5668 struct remote_state *rs = get_remote_state ();
5669 struct remote_notif_state *rns = rs->notif_state;
5670
5671 /* This function can be notified when an inferior exists. When the
5672 target is not remote, the notification state is NULL. */
5673 if (rs->remote_desc == NULL)
5674 return;
5675
5676 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 5677
74531fed 5678 /* Discard the in-flight notification. */
f48ff2a7 5679 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 5680 {
722247f1 5681 stop_reply_xfree (reply);
f48ff2a7 5682 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 5683 }
c906108c 5684
722247f1
YQ
5685 param.input = inf;
5686 param.output = NULL;
74531fed
PA
5687 /* Discard the stop replies we have already pulled with
5688 vStopped. */
722247f1 5689 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
5690 remove_stop_reply_for_inferior, &param);
5691}
5692
bcc75809
YQ
5693/* If its remote state is equal to the given remote state,
5694 remove EVENT from the stop reply queue. */
5695
5696static int
5697remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5698 QUEUE_ITER (stop_reply_p) *iter,
5699 stop_reply_p event,
5700 void *data)
5701{
5702 struct queue_iter_param *param = data;
5703 struct remote_state *rs = param->input;
5704
5705 if (event->rs == rs)
5706 {
5707 stop_reply_xfree (event);
5708 QUEUE_remove_elem (stop_reply_p, q, iter);
5709 }
5710
5711 return 1;
5712}
5713
5714/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
5715
5716static void
bcc75809 5717discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
5718{
5719 struct queue_iter_param param;
5720
bcc75809 5721 param.input = rs;
f48ff2a7
YQ
5722 param.output = NULL;
5723 /* Discard the stop replies we have already pulled with
5724 vStopped. */
5725 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 5726 remove_stop_reply_of_remote_state, &param);
722247f1 5727}
74531fed 5728
722247f1
YQ
5729/* A parameter to pass data in and out. */
5730
5731static int
5732remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5733 QUEUE_ITER (stop_reply_p) *iter,
5734 stop_reply_p event,
5735 void *data)
5736{
5737 struct queue_iter_param *param = data;
5738 ptid_t *ptid = param->input;
5739
5740 if (ptid_match (event->ptid, *ptid))
5741 {
5742 param->output = event;
5743 QUEUE_remove_elem (stop_reply_p, q, iter);
5744 return 0;
c8e38a49 5745 }
722247f1
YQ
5746
5747 return 1;
74531fed 5748}
43ff13b4 5749
722247f1
YQ
5750/* Remove the first reply in 'stop_reply_queue' which matches
5751 PTID. */
2e9f7625 5752
722247f1
YQ
5753static struct stop_reply *
5754remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 5755{
722247f1
YQ
5756 struct queue_iter_param param;
5757
5758 param.input = &ptid;
5759 param.output = NULL;
5760
5761 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5762 remote_notif_remove_once_on_match, &param);
5763 if (notif_debug)
5764 fprintf_unfiltered (gdb_stdlog,
5765 "notif: discard queued event: 'Stop' in %s\n",
5766 target_pid_to_str (ptid));
a744cf53 5767
722247f1 5768 return param.output;
74531fed 5769}
75c99385 5770
74531fed
PA
5771/* Look for a queued stop reply belonging to PTID. If one is found,
5772 remove it from the queue, and return it. Returns NULL if none is
5773 found. If there are still queued events left to process, tell the
5774 event loop to get back to target_wait soon. */
e24a49d8 5775
74531fed
PA
5776static struct stop_reply *
5777queued_stop_reply (ptid_t ptid)
5778{
722247f1 5779 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 5780
722247f1 5781 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
5782 /* There's still at least an event left. */
5783 mark_async_event_handler (remote_async_inferior_event_token);
5784
722247f1 5785 return r;
74531fed
PA
5786}
5787
5788/* Push a fully parsed stop reply in the stop reply queue. Since we
5789 know that we now have at least one queued event left to pass to the
5790 core side, tell the event loop to get back to target_wait soon. */
5791
5792static void
5793push_stop_reply (struct stop_reply *new_event)
5794{
722247f1 5795 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 5796
722247f1
YQ
5797 if (notif_debug)
5798 fprintf_unfiltered (gdb_stdlog,
5799 "notif: push 'Stop' %s to queue %d\n",
5800 target_pid_to_str (new_event->ptid),
5801 QUEUE_length (stop_reply_p,
5802 stop_reply_queue));
74531fed
PA
5803
5804 mark_async_event_handler (remote_async_inferior_event_token);
5805}
5806
722247f1
YQ
5807static int
5808stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5809 QUEUE_ITER (stop_reply_p) *iter,
5810 struct stop_reply *event,
5811 void *data)
5812{
5813 ptid_t *ptid = data;
5814
5815 return !(ptid_equal (*ptid, event->ptid)
5816 && event->ws.kind == TARGET_WAITKIND_STOPPED);
5817}
5818
74531fed
PA
5819/* Returns true if we have a stop reply for PTID. */
5820
5821static int
5822peek_stop_reply (ptid_t ptid)
5823{
722247f1
YQ
5824 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5825 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
5826}
5827
1f10ba14
PA
5828/* Skip PACKET until the next semi-colon (or end of string). */
5829
5830static char *
5831skip_to_semicolon (char *p)
5832{
5833 while (*p != '\0' && *p != ';')
5834 p++;
5835 return p;
5836}
5837
74531fed
PA
5838/* Parse the stop reply in BUF. Either the function succeeds, and the
5839 result is stored in EVENT, or throws an error. */
5840
5841static void
5842remote_parse_stop_reply (char *buf, struct stop_reply *event)
5843{
5844 struct remote_arch_state *rsa = get_remote_arch_state ();
5845 ULONGEST addr;
5846 char *p;
5847
5848 event->ptid = null_ptid;
bcc75809 5849 event->rs = get_remote_state ();
74531fed
PA
5850 event->ws.kind = TARGET_WAITKIND_IGNORE;
5851 event->ws.value.integer = 0;
f7e6eed5 5852 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 5853 event->regcache = NULL;
dc146f7c 5854 event->core = -1;
74531fed
PA
5855
5856 switch (buf[0])
5857 {
5858 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
5859 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5860 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5861 ss = signal number
5862 n... = register number
5863 r... = register contents
5864 */
5865
5866 p = &buf[3]; /* after Txx */
5867 while (*p)
5868 {
5869 char *p1;
cea39f65 5870 int fieldsize;
43ff13b4 5871
1f10ba14
PA
5872 p1 = strchr (p, ':');
5873 if (p1 == NULL)
5874 error (_("Malformed packet(a) (missing colon): %s\n\
5875Packet: '%s'\n"),
5876 p, buf);
5877 if (p == p1)
5878 error (_("Malformed packet(a) (missing register number): %s\n\
5879Packet: '%s'\n"),
5880 p, buf);
3c3bea1c 5881
1f10ba14
PA
5882 /* Some "registers" are actually extended stop information.
5883 Note if you're adding a new entry here: GDB 7.9 and
5884 earlier assume that all register "numbers" that start
5885 with an hex digit are real register numbers. Make sure
5886 the server only sends such a packet if it knows the
5887 client understands it. */
c8e38a49 5888
1f10ba14
PA
5889 if (strncmp (p, "thread", p1 - p) == 0)
5890 event->ptid = read_ptid (++p1, &p);
5891 else if ((strncmp (p, "watch", p1 - p) == 0)
5892 || (strncmp (p, "rwatch", p1 - p) == 0)
5893 || (strncmp (p, "awatch", p1 - p) == 0))
cea39f65 5894 {
f7e6eed5 5895 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
5896 p = unpack_varlen_hex (++p1, &addr);
5897 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 5898 }
f7e6eed5
PA
5899 else if (strncmp (p, "swbreak", p1 - p) == 0)
5900 {
5901 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
5902
5903 /* Make sure the stub doesn't forget to indicate support
5904 with qSupported. */
5905 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
5906 error (_("Unexpected swbreak stop reason"));
5907
5908 /* The value part is documented as "must be empty",
5909 though we ignore it, in case we ever decide to make
5910 use of it in a backward compatible way. */
5911 p = skip_to_semicolon (p1 + 1);
5912 }
5913 else if (strncmp (p, "hwbreak", p1 - p) == 0)
5914 {
5915 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
5916
5917 /* Make sure the stub doesn't forget to indicate support
5918 with qSupported. */
5919 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
5920 error (_("Unexpected hwbreak stop reason"));
5921
5922 /* See above. */
5923 p = skip_to_semicolon (p1 + 1);
5924 }
1f10ba14 5925 else if (strncmp (p, "library", p1 - p) == 0)
cea39f65 5926 {
1f10ba14
PA
5927 event->ws.kind = TARGET_WAITKIND_LOADED;
5928 p = skip_to_semicolon (p1 + 1);
5929 }
5930 else if (strncmp (p, "replaylog", p1 - p) == 0)
5931 {
5932 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5933 /* p1 will indicate "begin" or "end", but it makes
5934 no difference for now, so ignore it. */
5935 p = skip_to_semicolon (p1 + 1);
5936 }
5937 else if (strncmp (p, "core", p1 - p) == 0)
5938 {
5939 ULONGEST c;
a744cf53 5940
1f10ba14
PA
5941 p = unpack_varlen_hex (++p1, &c);
5942 event->core = c;
cea39f65 5943 }
de0d863e
DB
5944 else if (strncmp (p, "fork", p1 - p) == 0)
5945 {
5946 event->ws.value.related_pid = read_ptid (++p1, &p);
5947 event->ws.kind = TARGET_WAITKIND_FORKED;
5948 }
c269dbdb
DB
5949 else if (strncmp (p, "vfork", p1 - p) == 0)
5950 {
5951 event->ws.value.related_pid = read_ptid (++p1, &p);
5952 event->ws.kind = TARGET_WAITKIND_VFORKED;
5953 }
5954 else if (strncmp (p, "vforkdone", p1 - p) == 0)
5955 {
5956 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
5957 p = skip_to_semicolon (p1 + 1);
5958 }
cea39f65
MS
5959 else
5960 {
1f10ba14
PA
5961 ULONGEST pnum;
5962 char *p_temp;
5963
5964 /* Maybe a real ``P'' register number. */
5965 p_temp = unpack_varlen_hex (p, &pnum);
5966 /* If the first invalid character is the colon, we got a
5967 register number. Otherwise, it's an unknown stop
5968 reason. */
5969 if (p_temp == p1)
5970 {
5971 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5972 cached_reg_t cached_reg;
43ff13b4 5973
1f10ba14
PA
5974 if (reg == NULL)
5975 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 5976Packet: '%s'\n"),
1f10ba14 5977 hex_string (pnum), p, buf);
c8e38a49 5978
1f10ba14 5979 cached_reg.num = reg->regnum;
4100683b 5980
1f10ba14
PA
5981 p = p1 + 1;
5982 fieldsize = hex2bin (p, cached_reg.data,
5983 register_size (target_gdbarch (),
5984 reg->regnum));
5985 p += 2 * fieldsize;
5986 if (fieldsize < register_size (target_gdbarch (),
5987 reg->regnum))
5988 warning (_("Remote reply is too short: %s"), buf);
74531fed 5989
1f10ba14
PA
5990 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5991 }
5992 else
5993 {
5994 /* Not a number. Silently skip unknown optional
5995 info. */
5996 p = skip_to_semicolon (p1 + 1);
5997 }
cea39f65 5998 }
c8e38a49 5999
cea39f65
MS
6000 if (*p != ';')
6001 error (_("Remote register badly formatted: %s\nhere: %s"),
6002 buf, p);
6003 ++p;
6004 }
5b5596ff
PA
6005
6006 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
6007 break;
6008
c8e38a49
PA
6009 /* fall through */
6010 case 'S': /* Old style status, just signal only. */
3a09da41
PA
6011 {
6012 int sig;
6013
6014 event->ws.kind = TARGET_WAITKIND_STOPPED;
6015 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
6016 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
6017 event->ws.value.sig = (enum gdb_signal) sig;
6018 else
6019 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
6020 }
c8e38a49
PA
6021 break;
6022 case 'W': /* Target exited. */
6023 case 'X':
6024 {
6025 char *p;
6026 int pid;
6027 ULONGEST value;
82f73884 6028
c8e38a49
PA
6029 /* GDB used to accept only 2 hex chars here. Stubs should
6030 only send more if they detect GDB supports multi-process
6031 support. */
6032 p = unpack_varlen_hex (&buf[1], &value);
82f73884 6033
c8e38a49
PA
6034 if (buf[0] == 'W')
6035 {
6036 /* The remote process exited. */
74531fed
PA
6037 event->ws.kind = TARGET_WAITKIND_EXITED;
6038 event->ws.value.integer = value;
c8e38a49
PA
6039 }
6040 else
6041 {
6042 /* The remote process exited with a signal. */
74531fed 6043 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
6044 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
6045 event->ws.value.sig = (enum gdb_signal) value;
6046 else
6047 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 6048 }
82f73884 6049
c8e38a49
PA
6050 /* If no process is specified, assume inferior_ptid. */
6051 pid = ptid_get_pid (inferior_ptid);
6052 if (*p == '\0')
6053 ;
6054 else if (*p == ';')
6055 {
6056 p++;
6057
0b24eb2d 6058 if (*p == '\0')
82f73884 6059 ;
61012eef 6060 else if (startswith (p, "process:"))
82f73884 6061 {
c8e38a49 6062 ULONGEST upid;
a744cf53 6063
c8e38a49
PA
6064 p += sizeof ("process:") - 1;
6065 unpack_varlen_hex (p, &upid);
6066 pid = upid;
82f73884
PA
6067 }
6068 else
6069 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 6070 }
c8e38a49
PA
6071 else
6072 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
6073 event->ptid = pid_to_ptid (pid);
6074 }
6075 break;
6076 }
6077
6078 if (non_stop && ptid_equal (event->ptid, null_ptid))
6079 error (_("No process or thread specified in stop reply: %s"), buf);
6080}
6081
722247f1
YQ
6082/* When the stub wants to tell GDB about a new notification reply, it
6083 sends a notification (%Stop, for example). Those can come it at
6084 any time, hence, we have to make sure that any pending
6085 putpkt/getpkt sequence we're making is finished, before querying
6086 the stub for more events with the corresponding ack command
6087 (vStopped, for example). E.g., if we started a vStopped sequence
6088 immediately upon receiving the notification, something like this
6089 could happen:
74531fed
PA
6090
6091 1.1) --> Hg 1
6092 1.2) <-- OK
6093 1.3) --> g
6094 1.4) <-- %Stop
6095 1.5) --> vStopped
6096 1.6) <-- (registers reply to step #1.3)
6097
6098 Obviously, the reply in step #1.6 would be unexpected to a vStopped
6099 query.
6100
796cb314 6101 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
6102 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
6103 doing whatever we were doing:
6104
6105 2.1) --> Hg 1
6106 2.2) <-- OK
6107 2.3) --> g
6108 2.4) <-- %Stop
6109 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
6110 2.5) <-- (registers reply to step #2.3)
6111
6112 Eventualy after step #2.5, we return to the event loop, which
6113 notices there's an event on the
6114 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
6115 associated callback --- the function below. At this point, we're
6116 always safe to start a vStopped sequence. :
6117
6118 2.6) --> vStopped
6119 2.7) <-- T05 thread:2
6120 2.8) --> vStopped
6121 2.9) --> OK
6122*/
6123
722247f1
YQ
6124void
6125remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
6126{
6127 struct remote_state *rs = get_remote_state ();
74531fed 6128
f48ff2a7 6129 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 6130 {
722247f1
YQ
6131 if (notif_debug)
6132 fprintf_unfiltered (gdb_stdlog,
6133 "notif: process: '%s' ack pending event\n",
6134 nc->name);
74531fed 6135
722247f1 6136 /* acknowledge */
f48ff2a7
YQ
6137 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
6138 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
6139
6140 while (1)
6141 {
6142 getpkt (&rs->buf, &rs->buf_size, 0);
6143 if (strcmp (rs->buf, "OK") == 0)
6144 break;
6145 else
722247f1 6146 remote_notif_ack (nc, rs->buf);
74531fed
PA
6147 }
6148 }
722247f1
YQ
6149 else
6150 {
6151 if (notif_debug)
6152 fprintf_unfiltered (gdb_stdlog,
6153 "notif: process: '%s' no pending reply\n",
6154 nc->name);
6155 }
74531fed
PA
6156}
6157
74531fed
PA
6158/* Called when it is decided that STOP_REPLY holds the info of the
6159 event that is to be returned to the core. This function always
6160 destroys STOP_REPLY. */
6161
6162static ptid_t
6163process_stop_reply (struct stop_reply *stop_reply,
6164 struct target_waitstatus *status)
6165{
6166 ptid_t ptid;
6167
6168 *status = stop_reply->ws;
6169 ptid = stop_reply->ptid;
6170
6171 /* If no thread/process was reported by the stub, assume the current
6172 inferior. */
6173 if (ptid_equal (ptid, null_ptid))
6174 ptid = inferior_ptid;
6175
5f3563ea
PA
6176 if (status->kind != TARGET_WAITKIND_EXITED
6177 && status->kind != TARGET_WAITKIND_SIGNALLED)
74531fed 6178 {
ee154bee
TT
6179 struct remote_state *rs = get_remote_state ();
6180
5f3563ea
PA
6181 /* Expedited registers. */
6182 if (stop_reply->regcache)
6183 {
217f1f79 6184 struct regcache *regcache
f5656ead 6185 = get_thread_arch_regcache (ptid, target_gdbarch ());
5f3563ea
PA
6186 cached_reg_t *reg;
6187 int ix;
6188
6189 for (ix = 0;
6190 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
6191 ix++)
217f1f79 6192 regcache_raw_supply (regcache, reg->num, reg->data);
5f3563ea
PA
6193 VEC_free (cached_reg_t, stop_reply->regcache);
6194 }
74531fed 6195
f7e6eed5 6196 rs->stop_reason = stop_reply->stop_reason;
ee154bee 6197 rs->remote_watch_data_address = stop_reply->watch_data_address;
1941c569
PA
6198
6199 remote_notice_new_inferior (ptid, 0);
dc146f7c 6200 demand_private_info (ptid)->core = stop_reply->core;
74531fed
PA
6201 }
6202
74531fed
PA
6203 stop_reply_xfree (stop_reply);
6204 return ptid;
6205}
6206
6207/* The non-stop mode version of target_wait. */
6208
6209static ptid_t
47608cb1 6210remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6211{
6212 struct remote_state *rs = get_remote_state ();
74531fed
PA
6213 struct stop_reply *stop_reply;
6214 int ret;
fee9eda9 6215 int is_notif = 0;
74531fed
PA
6216
6217 /* If in non-stop mode, get out of getpkt even if a
6218 notification is received. */
6219
6220 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6221 0 /* forever */, &is_notif);
74531fed
PA
6222 while (1)
6223 {
fee9eda9 6224 if (ret != -1 && !is_notif)
74531fed
PA
6225 switch (rs->buf[0])
6226 {
6227 case 'E': /* Error of some sort. */
6228 /* We're out of sync with the target now. Did it continue
6229 or not? We can't tell which thread it was in non-stop,
6230 so just ignore this. */
6231 warning (_("Remote failure reply: %s"), rs->buf);
6232 break;
6233 case 'O': /* Console output. */
6234 remote_console_output (rs->buf + 1);
6235 break;
6236 default:
6237 warning (_("Invalid remote reply: %s"), rs->buf);
6238 break;
6239 }
6240
6241 /* Acknowledge a pending stop reply that may have arrived in the
6242 mean time. */
f48ff2a7 6243 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 6244 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
6245
6246 /* If indeed we noticed a stop reply, we're done. */
6247 stop_reply = queued_stop_reply (ptid);
6248 if (stop_reply != NULL)
6249 return process_stop_reply (stop_reply, status);
6250
47608cb1 6251 /* Still no event. If we're just polling for an event, then
74531fed 6252 return to the event loop. */
47608cb1 6253 if (options & TARGET_WNOHANG)
74531fed
PA
6254 {
6255 status->kind = TARGET_WAITKIND_IGNORE;
6256 return minus_one_ptid;
6257 }
6258
47608cb1 6259 /* Otherwise do a blocking wait. */
74531fed 6260 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6261 1 /* forever */, &is_notif);
74531fed
PA
6262 }
6263}
6264
6265/* Wait until the remote machine stops, then return, storing status in
6266 STATUS just as `wait' would. */
6267
6268static ptid_t
47608cb1 6269remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6270{
6271 struct remote_state *rs = get_remote_state ();
74531fed 6272 ptid_t event_ptid = null_ptid;
cea39f65 6273 char *buf;
74531fed
PA
6274 struct stop_reply *stop_reply;
6275
47608cb1
PA
6276 again:
6277
74531fed
PA
6278 status->kind = TARGET_WAITKIND_IGNORE;
6279 status->value.integer = 0;
6280
6281 stop_reply = queued_stop_reply (ptid);
6282 if (stop_reply != NULL)
6283 return process_stop_reply (stop_reply, status);
6284
6285 if (rs->cached_wait_status)
6286 /* Use the cached wait status, but only once. */
6287 rs->cached_wait_status = 0;
6288 else
6289 {
6290 int ret;
722247f1 6291 int is_notif;
567420d1
PA
6292 int forever = ((options & TARGET_WNOHANG) == 0
6293 && wait_forever_enabled_p);
6294
6295 if (!rs->waiting_for_stop_reply)
6296 {
6297 status->kind = TARGET_WAITKIND_NO_RESUMED;
6298 return minus_one_ptid;
6299 }
74531fed
PA
6300
6301 if (!target_is_async_p ())
6302 {
934b9bac 6303 ofunc = signal (SIGINT, sync_remote_interrupt);
74531fed
PA
6304 /* If the user hit C-c before this packet, or between packets,
6305 pretend that it was hit right here. */
522002f9 6306 if (check_quit_flag ())
74531fed 6307 {
522002f9 6308 clear_quit_flag ();
934b9bac 6309 sync_remote_interrupt (SIGINT);
74531fed
PA
6310 }
6311 }
6312
6313 /* FIXME: cagney/1999-09-27: If we're in async mode we should
6314 _never_ wait for ever -> test on target_is_async_p().
6315 However, before we do that we need to ensure that the caller
6316 knows how to take the target into/out of async mode. */
722247f1 6317 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
567420d1 6318 forever, &is_notif);
722247f1 6319
5e1b953b
SDJ
6320 if (!target_is_async_p ())
6321 signal (SIGINT, ofunc);
6322
722247f1
YQ
6323 /* GDB gets a notification. Return to core as this event is
6324 not interesting. */
6325 if (ret != -1 && is_notif)
6326 return minus_one_ptid;
567420d1
PA
6327
6328 if (ret == -1 && (options & TARGET_WNOHANG) != 0)
6329 return minus_one_ptid;
74531fed
PA
6330 }
6331
6332 buf = rs->buf;
6333
f7e6eed5 6334 rs->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed
PA
6335
6336 /* We got something. */
6337 rs->waiting_for_stop_reply = 0;
6338
3a29589a
DJ
6339 /* Assume that the target has acknowledged Ctrl-C unless we receive
6340 an 'F' or 'O' packet. */
6341 if (buf[0] != 'F' && buf[0] != 'O')
6342 rs->ctrlc_pending_p = 0;
6343
74531fed
PA
6344 switch (buf[0])
6345 {
6346 case 'E': /* Error of some sort. */
6347 /* We're out of sync with the target now. Did it continue or
6348 not? Not is more likely, so report a stop. */
6349 warning (_("Remote failure reply: %s"), buf);
6350 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 6351 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
6352 break;
6353 case 'F': /* File-I/O request. */
3a29589a
DJ
6354 remote_fileio_request (buf, rs->ctrlc_pending_p);
6355 rs->ctrlc_pending_p = 0;
74531fed
PA
6356 break;
6357 case 'T': case 'S': case 'X': case 'W':
6358 {
722247f1
YQ
6359 struct stop_reply *stop_reply
6360 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
6361 rs->buf);
74531fed 6362
74531fed 6363 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
6364 break;
6365 }
6366 case 'O': /* Console output. */
6367 remote_console_output (buf + 1);
e24a49d8 6368
c8e38a49
PA
6369 /* The target didn't really stop; keep waiting. */
6370 rs->waiting_for_stop_reply = 1;
e24a49d8 6371
c8e38a49
PA
6372 break;
6373 case '\0':
b73be471 6374 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
6375 {
6376 /* Zero length reply means that we tried 'S' or 'C' and the
6377 remote system doesn't support it. */
6378 target_terminal_ours_for_output ();
6379 printf_filtered
6380 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
6381 gdb_signal_to_name (rs->last_sent_signal));
6382 rs->last_sent_signal = GDB_SIGNAL_0;
c8e38a49
PA
6383 target_terminal_inferior ();
6384
280ceea3 6385 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
c8e38a49
PA
6386 putpkt ((char *) buf);
6387
6388 /* We just told the target to resume, so a stop reply is in
6389 order. */
e24a49d8 6390 rs->waiting_for_stop_reply = 1;
c8e38a49 6391 break;
43ff13b4 6392 }
c8e38a49
PA
6393 /* else fallthrough */
6394 default:
6395 warning (_("Invalid remote reply: %s"), buf);
6396 /* Keep waiting. */
6397 rs->waiting_for_stop_reply = 1;
6398 break;
43ff13b4 6399 }
c8e38a49 6400
c8e38a49 6401 if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
6402 {
6403 /* Nothing interesting happened. If we're doing a non-blocking
6404 poll, we're done. Otherwise, go back to waiting. */
6405 if (options & TARGET_WNOHANG)
6406 return minus_one_ptid;
6407 else
6408 goto again;
6409 }
74531fed
PA
6410 else if (status->kind != TARGET_WAITKIND_EXITED
6411 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
6412 {
6413 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 6414 record_currthread (rs, event_ptid);
82f73884
PA
6415 else
6416 event_ptid = inferior_ptid;
43ff13b4 6417 }
74531fed
PA
6418 else
6419 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 6420 record_currthread (rs, minus_one_ptid);
79d7f229 6421
82f73884 6422 return event_ptid;
43ff13b4
JM
6423}
6424
74531fed
PA
6425/* Wait until the remote machine stops, then return, storing status in
6426 STATUS just as `wait' would. */
6427
c8e38a49 6428static ptid_t
117de6a9 6429remote_wait (struct target_ops *ops,
47608cb1 6430 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
6431{
6432 ptid_t event_ptid;
6433
74531fed 6434 if (non_stop)
47608cb1 6435 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 6436 else
47608cb1 6437 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 6438
d9d41e78 6439 if (target_is_async_p ())
c8e38a49 6440 {
74531fed
PA
6441 /* If there are are events left in the queue tell the event loop
6442 to return here. */
722247f1 6443 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 6444 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 6445 }
c8e38a49
PA
6446
6447 return event_ptid;
6448}
6449
74ca34ce 6450/* Fetch a single register using a 'p' packet. */
c906108c 6451
b96ec7ac 6452static int
56be3814 6453fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac
AC
6454{
6455 struct remote_state *rs = get_remote_state ();
2e9f7625 6456 char *buf, *p;
b96ec7ac
AC
6457 char regp[MAX_REGISTER_SIZE];
6458 int i;
6459
4082afcc 6460 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
6461 return 0;
6462
6463 if (reg->pnum == -1)
6464 return 0;
6465
2e9f7625 6466 p = rs->buf;
fcad0fa4 6467 *p++ = 'p';
74ca34ce 6468 p += hexnumstr (p, reg->pnum);
fcad0fa4 6469 *p++ = '\0';
1f4437a4
MS
6470 putpkt (rs->buf);
6471 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 6472
2e9f7625
DJ
6473 buf = rs->buf;
6474
74ca34ce
DJ
6475 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
6476 {
6477 case PACKET_OK:
6478 break;
6479 case PACKET_UNKNOWN:
6480 return 0;
6481 case PACKET_ERROR:
27a9c0bf
MS
6482 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6483 gdbarch_register_name (get_regcache_arch (regcache),
6484 reg->regnum),
6485 buf);
74ca34ce 6486 }
3f9a994c
JB
6487
6488 /* If this register is unfetchable, tell the regcache. */
6489 if (buf[0] == 'x')
8480adf2 6490 {
56be3814 6491 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 6492 return 1;
b96ec7ac 6493 }
b96ec7ac 6494
3f9a994c
JB
6495 /* Otherwise, parse and supply the value. */
6496 p = buf;
6497 i = 0;
6498 while (p[0] != 0)
6499 {
6500 if (p[1] == 0)
74ca34ce 6501 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
6502
6503 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6504 p += 2;
6505 }
56be3814 6506 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 6507 return 1;
b96ec7ac
AC
6508}
6509
74ca34ce
DJ
6510/* Fetch the registers included in the target's 'g' packet. */
6511
29709017
DJ
6512static int
6513send_g_packet (void)
c906108c 6514{
d01949b6 6515 struct remote_state *rs = get_remote_state ();
cea39f65 6516 int buf_len;
c906108c 6517
bba74b36 6518 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 6519 remote_send (&rs->buf, &rs->buf_size);
c906108c 6520
29709017
DJ
6521 /* We can get out of synch in various cases. If the first character
6522 in the buffer is not a hex character, assume that has happened
6523 and try to fetch another packet to read. */
6524 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6525 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6526 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6527 && rs->buf[0] != 'x') /* New: unavailable register value. */
6528 {
6529 if (remote_debug)
6530 fprintf_unfiltered (gdb_stdlog,
6531 "Bad register packet; fetching a new packet\n");
6532 getpkt (&rs->buf, &rs->buf_size, 0);
6533 }
6534
74ca34ce
DJ
6535 buf_len = strlen (rs->buf);
6536
6537 /* Sanity check the received packet. */
6538 if (buf_len % 2 != 0)
6539 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
6540
6541 return buf_len / 2;
6542}
6543
6544static void
56be3814 6545process_g_packet (struct regcache *regcache)
29709017 6546{
4a22f64d 6547 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
6548 struct remote_state *rs = get_remote_state ();
6549 struct remote_arch_state *rsa = get_remote_arch_state ();
6550 int i, buf_len;
6551 char *p;
6552 char *regs;
6553
6554 buf_len = strlen (rs->buf);
6555
6556 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce
DJ
6557 if (buf_len > 2 * rsa->sizeof_g_packet)
6558 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6559
6560 /* Save the size of the packet sent to us by the target. It is used
6561 as a heuristic when determining the max size of packets that the
6562 target can safely receive. */
6563 if (rsa->actual_register_packet_size == 0)
6564 rsa->actual_register_packet_size = buf_len;
6565
6566 /* If this is smaller than we guessed the 'g' packet would be,
6567 update our records. A 'g' reply that doesn't include a register's
6568 value implies either that the register is not available, or that
6569 the 'p' packet must be used. */
6570 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 6571 {
74ca34ce
DJ
6572 rsa->sizeof_g_packet = buf_len / 2;
6573
4a22f64d 6574 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 6575 {
74ca34ce
DJ
6576 if (rsa->regs[i].pnum == -1)
6577 continue;
6578
6579 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6580 rsa->regs[i].in_g_packet = 0;
b96ec7ac 6581 else
74ca34ce 6582 rsa->regs[i].in_g_packet = 1;
b96ec7ac 6583 }
74ca34ce 6584 }
b323314b 6585
74ca34ce 6586 regs = alloca (rsa->sizeof_g_packet);
c906108c
SS
6587
6588 /* Unimplemented registers read as all bits zero. */
ea9c271d 6589 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 6590
c906108c
SS
6591 /* Reply describes registers byte by byte, each byte encoded as two
6592 hex characters. Suck them all up, then supply them to the
6593 register cacheing/storage mechanism. */
6594
74ca34ce 6595 p = rs->buf;
ea9c271d 6596 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 6597 {
74ca34ce
DJ
6598 if (p[0] == 0 || p[1] == 0)
6599 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6600 internal_error (__FILE__, __LINE__,
9b20d036 6601 _("unexpected end of 'g' packet reply"));
74ca34ce 6602
c906108c 6603 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 6604 regs[i] = 0; /* 'x' */
c906108c
SS
6605 else
6606 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6607 p += 2;
6608 }
6609
a744cf53
MS
6610 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6611 {
6612 struct packet_reg *r = &rsa->regs[i];
6613
6614 if (r->in_g_packet)
6615 {
6616 if (r->offset * 2 >= strlen (rs->buf))
6617 /* This shouldn't happen - we adjusted in_g_packet above. */
6618 internal_error (__FILE__, __LINE__,
9b20d036 6619 _("unexpected end of 'g' packet reply"));
a744cf53
MS
6620 else if (rs->buf[r->offset * 2] == 'x')
6621 {
6622 gdb_assert (r->offset * 2 < strlen (rs->buf));
6623 /* The register isn't available, mark it as such (at
6624 the same time setting the value to zero). */
6625 regcache_raw_supply (regcache, r->regnum, NULL);
6626 }
6627 else
6628 regcache_raw_supply (regcache, r->regnum,
6629 regs + r->offset);
6630 }
6631 }
c906108c
SS
6632}
6633
29709017 6634static void
56be3814 6635fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
6636{
6637 send_g_packet ();
56be3814 6638 process_g_packet (regcache);
29709017
DJ
6639}
6640
e6e4e701
PA
6641/* Make the remote selected traceframe match GDB's selected
6642 traceframe. */
6643
6644static void
6645set_remote_traceframe (void)
6646{
6647 int newnum;
262e1174 6648 struct remote_state *rs = get_remote_state ();
e6e4e701 6649
262e1174 6650 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
6651 return;
6652
6653 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 6654 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
6655
6656 newnum = target_trace_find (tfind_number,
6657 get_traceframe_number (), 0, 0, NULL);
6658
6659 /* Should not happen. If it does, all bets are off. */
6660 if (newnum != get_traceframe_number ())
6661 warning (_("could not set remote traceframe"));
6662}
6663
74ca34ce 6664static void
28439f5e
PA
6665remote_fetch_registers (struct target_ops *ops,
6666 struct regcache *regcache, int regnum)
74ca34ce 6667{
74ca34ce
DJ
6668 struct remote_arch_state *rsa = get_remote_arch_state ();
6669 int i;
6670
e6e4e701 6671 set_remote_traceframe ();
79d7f229 6672 set_general_thread (inferior_ptid);
74ca34ce
DJ
6673
6674 if (regnum >= 0)
6675 {
6676 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6677
74ca34ce
DJ
6678 gdb_assert (reg != NULL);
6679
6680 /* If this register might be in the 'g' packet, try that first -
6681 we are likely to read more than one register. If this is the
6682 first 'g' packet, we might be overly optimistic about its
6683 contents, so fall back to 'p'. */
6684 if (reg->in_g_packet)
6685 {
56be3814 6686 fetch_registers_using_g (regcache);
74ca34ce
DJ
6687 if (reg->in_g_packet)
6688 return;
6689 }
6690
56be3814 6691 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
6692 return;
6693
6694 /* This register is not available. */
56be3814 6695 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
6696
6697 return;
6698 }
6699
56be3814 6700 fetch_registers_using_g (regcache);
74ca34ce 6701
4a22f64d 6702 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6703 if (!rsa->regs[i].in_g_packet)
56be3814 6704 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
6705 {
6706 /* This register is not available. */
56be3814 6707 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
6708 }
6709}
6710
c906108c
SS
6711/* Prepare to store registers. Since we may send them all (using a
6712 'G' request), we have to read out the ones we don't want to change
6713 first. */
6714
c5aa993b 6715static void
f32dbf8c 6716remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 6717{
ea9c271d 6718 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 6719 int i;
cfd77fa1 6720 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 6721
c906108c 6722 /* Make sure the entire registers array is valid. */
4082afcc 6723 switch (packet_support (PACKET_P))
5a2468f5
JM
6724 {
6725 case PACKET_DISABLE:
6726 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 6727 /* Make sure all the necessary registers are cached. */
4a22f64d 6728 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 6729 if (rsa->regs[i].in_g_packet)
316f2060 6730 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
6731 break;
6732 case PACKET_ENABLE:
6733 break;
6734 }
6735}
6736
ad10f812 6737/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 6738 packet was not recognized. */
5a2468f5
JM
6739
6740static int
1f4437a4
MS
6741store_register_using_P (const struct regcache *regcache,
6742 struct packet_reg *reg)
5a2468f5 6743{
4a22f64d 6744 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 6745 struct remote_state *rs = get_remote_state ();
5a2468f5 6746 /* Try storing a single register. */
6d820c5c 6747 char *buf = rs->buf;
cfd77fa1 6748 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 6749 char *p;
5a2468f5 6750
4082afcc 6751 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
6752 return 0;
6753
6754 if (reg->pnum == -1)
6755 return 0;
6756
ea9c271d 6757 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 6758 p = buf + strlen (buf);
56be3814 6759 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 6760 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
6761 putpkt (rs->buf);
6762 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 6763
74ca34ce
DJ
6764 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6765 {
6766 case PACKET_OK:
6767 return 1;
6768 case PACKET_ERROR:
27a9c0bf
MS
6769 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6770 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
6771 case PACKET_UNKNOWN:
6772 return 0;
6773 default:
6774 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6775 }
c906108c
SS
6776}
6777
23860348
MS
6778/* Store register REGNUM, or all registers if REGNUM == -1, from the
6779 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
6780
6781static void
56be3814 6782store_registers_using_G (const struct regcache *regcache)
c906108c 6783{
d01949b6 6784 struct remote_state *rs = get_remote_state ();
ea9c271d 6785 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 6786 gdb_byte *regs;
c906108c
SS
6787 char *p;
6788
193cb69f
AC
6789 /* Extract all the registers in the regcache copying them into a
6790 local buffer. */
6791 {
b323314b 6792 int i;
a744cf53 6793
ea9c271d
DJ
6794 regs = alloca (rsa->sizeof_g_packet);
6795 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 6796 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 6797 {
ea9c271d 6798 struct packet_reg *r = &rsa->regs[i];
a744cf53 6799
b323314b 6800 if (r->in_g_packet)
56be3814 6801 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
6802 }
6803 }
c906108c
SS
6804
6805 /* Command describes registers byte by byte,
6806 each byte encoded as two hex characters. */
6d820c5c 6807 p = rs->buf;
193cb69f 6808 *p++ = 'G';
74ca34ce
DJ
6809 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6810 updated. */
6811 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
6812 putpkt (rs->buf);
6813 getpkt (&rs->buf, &rs->buf_size, 0);
6814 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
6815 error (_("Could not write registers; remote failure reply '%s'"),
6816 rs->buf);
c906108c 6817}
74ca34ce
DJ
6818
6819/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6820 of the register cache buffer. FIXME: ignores errors. */
6821
6822static void
28439f5e
PA
6823remote_store_registers (struct target_ops *ops,
6824 struct regcache *regcache, int regnum)
74ca34ce 6825{
74ca34ce
DJ
6826 struct remote_arch_state *rsa = get_remote_arch_state ();
6827 int i;
6828
e6e4e701 6829 set_remote_traceframe ();
79d7f229 6830 set_general_thread (inferior_ptid);
74ca34ce
DJ
6831
6832 if (regnum >= 0)
6833 {
6834 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6835
74ca34ce
DJ
6836 gdb_assert (reg != NULL);
6837
6838 /* Always prefer to store registers using the 'P' packet if
6839 possible; we often change only a small number of registers.
6840 Sometimes we change a larger number; we'd need help from a
6841 higher layer to know to use 'G'. */
56be3814 6842 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
6843 return;
6844
6845 /* For now, don't complain if we have no way to write the
6846 register. GDB loses track of unavailable registers too
6847 easily. Some day, this may be an error. We don't have
0df8b418 6848 any way to read the register, either... */
74ca34ce
DJ
6849 if (!reg->in_g_packet)
6850 return;
6851
56be3814 6852 store_registers_using_G (regcache);
74ca34ce
DJ
6853 return;
6854 }
6855
56be3814 6856 store_registers_using_G (regcache);
74ca34ce 6857
4a22f64d 6858 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6859 if (!rsa->regs[i].in_g_packet)
56be3814 6860 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
6861 /* See above for why we do not issue an error here. */
6862 continue;
6863}
c906108c
SS
6864\f
6865
6866/* Return the number of hex digits in num. */
6867
6868static int
fba45db2 6869hexnumlen (ULONGEST num)
c906108c
SS
6870{
6871 int i;
6872
6873 for (i = 0; num != 0; i++)
6874 num >>= 4;
6875
6876 return max (i, 1);
6877}
6878
2df3850c 6879/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
6880
6881static int
fba45db2 6882hexnumstr (char *buf, ULONGEST num)
c906108c 6883{
c906108c 6884 int len = hexnumlen (num);
a744cf53 6885
2df3850c
JM
6886 return hexnumnstr (buf, num, len);
6887}
6888
c906108c 6889
2df3850c 6890/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 6891
2df3850c 6892static int
fba45db2 6893hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
6894{
6895 int i;
6896
6897 buf[width] = '\0';
6898
6899 for (i = width - 1; i >= 0; i--)
c906108c 6900 {
c5aa993b 6901 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
6902 num >>= 4;
6903 }
6904
2df3850c 6905 return width;
c906108c
SS
6906}
6907
23860348 6908/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
6909
6910static CORE_ADDR
fba45db2 6911remote_address_masked (CORE_ADDR addr)
c906108c 6912{
883b9c6c 6913 unsigned int address_size = remote_address_size;
a744cf53 6914
911c95a5
UW
6915 /* If "remoteaddresssize" was not set, default to target address size. */
6916 if (!address_size)
f5656ead 6917 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
6918
6919 if (address_size > 0
6920 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
6921 {
6922 /* Only create a mask when that mask can safely be constructed
23860348 6923 in a ULONGEST variable. */
c906108c 6924 ULONGEST mask = 1;
a744cf53 6925
911c95a5 6926 mask = (mask << address_size) - 1;
c906108c
SS
6927 addr &= mask;
6928 }
6929 return addr;
6930}
6931
6932/* Determine whether the remote target supports binary downloading.
6933 This is accomplished by sending a no-op memory write of zero length
6934 to the target at the specified address. It does not suffice to send
23860348
MS
6935 the whole packet, since many stubs strip the eighth bit and
6936 subsequently compute a wrong checksum, which causes real havoc with
6937 remote_write_bytes.
7a292a7a 6938
96baa820 6939 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 6940 clean. In cases like this, the user should clear "remote
23860348 6941 X-packet". */
96baa820 6942
c906108c 6943static void
fba45db2 6944check_binary_download (CORE_ADDR addr)
c906108c 6945{
d01949b6 6946 struct remote_state *rs = get_remote_state ();
24b06219 6947
4082afcc 6948 switch (packet_support (PACKET_X))
c906108c 6949 {
96baa820
JM
6950 case PACKET_DISABLE:
6951 break;
6952 case PACKET_ENABLE:
6953 break;
6954 case PACKET_SUPPORT_UNKNOWN:
6955 {
96baa820 6956 char *p;
802188a7 6957
2e9f7625 6958 p = rs->buf;
96baa820
JM
6959 *p++ = 'X';
6960 p += hexnumstr (p, (ULONGEST) addr);
6961 *p++ = ',';
6962 p += hexnumstr (p, (ULONGEST) 0);
6963 *p++ = ':';
6964 *p = '\0';
802188a7 6965
2e9f7625 6966 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 6967 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 6968
2e9f7625 6969 if (rs->buf[0] == '\0')
96baa820
JM
6970 {
6971 if (remote_debug)
6972 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
6973 "binary downloading NOT "
6974 "supported by target\n");
444abaca 6975 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
6976 }
6977 else
6978 {
6979 if (remote_debug)
6980 fprintf_unfiltered (gdb_stdlog,
64b9b334 6981 "binary downloading supported by target\n");
444abaca 6982 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
6983 }
6984 break;
6985 }
c906108c
SS
6986 }
6987}
6988
124e13d9
SM
6989/* Helper function to resize the payload in order to try to get a good
6990 alignment. We try to write an amount of data such that the next write will
6991 start on an address aligned on REMOTE_ALIGN_WRITES. */
6992
6993static int
6994align_for_efficient_write (int todo, CORE_ADDR memaddr)
6995{
6996 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6997}
6998
c906108c
SS
6999/* Write memory data directly to the remote machine.
7000 This does not inform the data cache; the data cache uses this.
a76d924d 7001 HEADER is the starting part of the packet.
c906108c
SS
7002 MEMADDR is the address in the remote memory space.
7003 MYADDR is the address of the buffer in our space.
124e13d9
SM
7004 LEN_UNITS is the number of addressable units to write.
7005 UNIT_SIZE is the length in bytes of an addressable unit.
a76d924d
DJ
7006 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
7007 should send data as binary ('X'), or hex-encoded ('M').
7008
7009 The function creates packet of the form
7010 <HEADER><ADDRESS>,<LENGTH>:<DATA>
7011
124e13d9 7012 where encoding of <DATA> is terminated by PACKET_FORMAT.
a76d924d
DJ
7013
7014 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
7015 are omitted.
7016
9b409511 7017 Return the transferred status, error or OK (an
124e13d9
SM
7018 'enum target_xfer_status' value). Save the number of addressable units
7019 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
7020
7021 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
7022 exchange between gdb and the stub could look like (?? in place of the
7023 checksum):
7024
7025 -> $m1000,4#??
7026 <- aaaabbbbccccdddd
7027
7028 -> $M1000,3:eeeeffffeeee#??
7029 <- OK
7030
7031 -> $m1000,4#??
7032 <- eeeeffffeeeedddd */
c906108c 7033
9b409511 7034static enum target_xfer_status
a76d924d 7035remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
124e13d9
SM
7036 const gdb_byte *myaddr, ULONGEST len_units,
7037 int unit_size, ULONGEST *xfered_len_units,
7038 char packet_format, int use_length)
c906108c 7039{
6d820c5c 7040 struct remote_state *rs = get_remote_state ();
cfd77fa1 7041 char *p;
a76d924d
DJ
7042 char *plen = NULL;
7043 int plenlen = 0;
124e13d9
SM
7044 int todo_units;
7045 int units_written;
7046 int payload_capacity_bytes;
7047 int payload_length_bytes;
a76d924d
DJ
7048
7049 if (packet_format != 'X' && packet_format != 'M')
7050 internal_error (__FILE__, __LINE__,
9b20d036 7051 _("remote_write_bytes_aux: bad packet format"));
c906108c 7052
124e13d9 7053 if (len_units == 0)
9b409511 7054 return TARGET_XFER_EOF;
b2182ed2 7055
124e13d9 7056 payload_capacity_bytes = get_memory_write_packet_size ();
2bc416ba 7057
6d820c5c
DJ
7058 /* The packet buffer will be large enough for the payload;
7059 get_memory_packet_size ensures this. */
a76d924d 7060 rs->buf[0] = '\0';
c906108c 7061
a257b5bb 7062 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
7063 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
7064
124e13d9 7065 payload_capacity_bytes -= strlen ("$,:#NN");
a76d924d 7066 if (!use_length)
0df8b418 7067 /* The comma won't be used. */
124e13d9
SM
7068 payload_capacity_bytes += 1;
7069 payload_capacity_bytes -= strlen (header);
7070 payload_capacity_bytes -= hexnumlen (memaddr);
c906108c 7071
a76d924d 7072 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 7073
a76d924d
DJ
7074 strcat (rs->buf, header);
7075 p = rs->buf + strlen (header);
7076
7077 /* Compute a best guess of the number of bytes actually transfered. */
7078 if (packet_format == 'X')
c906108c 7079 {
23860348 7080 /* Best guess at number of bytes that will fit. */
124e13d9 7081 todo_units = min (len_units, payload_capacity_bytes / unit_size);
a76d924d 7082 if (use_length)
124e13d9
SM
7083 payload_capacity_bytes -= hexnumlen (todo_units);
7084 todo_units = min (todo_units, payload_capacity_bytes / unit_size);
a76d924d
DJ
7085 }
7086 else
7087 {
124e13d9
SM
7088 /* Number of bytes that will fit. */
7089 todo_units = min (len_units, (payload_capacity_bytes / unit_size) / 2);
a76d924d 7090 if (use_length)
124e13d9
SM
7091 payload_capacity_bytes -= hexnumlen (todo_units);
7092 todo_units = min (todo_units, (payload_capacity_bytes / unit_size) / 2);
917317f4 7093 }
a76d924d 7094
124e13d9 7095 if (todo_units <= 0)
3de11b2e 7096 internal_error (__FILE__, __LINE__,
405f8e94 7097 _("minimum packet size too small to write data"));
802188a7 7098
6765f3e5
DJ
7099 /* If we already need another packet, then try to align the end
7100 of this packet to a useful boundary. */
124e13d9
SM
7101 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
7102 todo_units = align_for_efficient_write (todo_units, memaddr);
6765f3e5 7103
a257b5bb 7104 /* Append "<memaddr>". */
917317f4
JM
7105 memaddr = remote_address_masked (memaddr);
7106 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 7107
a76d924d
DJ
7108 if (use_length)
7109 {
7110 /* Append ",". */
7111 *p++ = ',';
802188a7 7112
124e13d9
SM
7113 /* Append the length and retain its location and size. It may need to be
7114 adjusted once the packet body has been created. */
a76d924d 7115 plen = p;
124e13d9 7116 plenlen = hexnumstr (p, (ULONGEST) todo_units);
a76d924d
DJ
7117 p += plenlen;
7118 }
a257b5bb
AC
7119
7120 /* Append ":". */
917317f4
JM
7121 *p++ = ':';
7122 *p = '\0';
802188a7 7123
a257b5bb 7124 /* Append the packet body. */
a76d924d 7125 if (packet_format == 'X')
917317f4 7126 {
917317f4
JM
7127 /* Binary mode. Send target system values byte by byte, in
7128 increasing byte addresses. Only escape certain critical
7129 characters. */
124e13d9
SM
7130 payload_length_bytes =
7131 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
7132 &units_written, payload_capacity_bytes);
6765f3e5 7133
124e13d9 7134 /* If not all TODO units fit, then we'll need another packet. Make
9b7194bc
DJ
7135 a second try to keep the end of the packet aligned. Don't do
7136 this if the packet is tiny. */
124e13d9 7137 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
6765f3e5 7138 {
124e13d9
SM
7139 int new_todo_units;
7140
7141 new_todo_units = align_for_efficient_write (units_written, memaddr);
7142
7143 if (new_todo_units != units_written)
7144 payload_length_bytes =
7145 remote_escape_output (myaddr, new_todo_units, unit_size,
7146 (gdb_byte *) p, &units_written,
7147 payload_capacity_bytes);
6765f3e5
DJ
7148 }
7149
124e13d9
SM
7150 p += payload_length_bytes;
7151 if (use_length && units_written < todo_units)
c906108c 7152 {
802188a7 7153 /* Escape chars have filled up the buffer prematurely,
124e13d9 7154 and we have actually sent fewer units than planned.
917317f4
JM
7155 Fix-up the length field of the packet. Use the same
7156 number of characters as before. */
124e13d9
SM
7157 plen += hexnumnstr (plen, (ULONGEST) units_written,
7158 plenlen);
917317f4 7159 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 7160 }
a76d924d
DJ
7161 }
7162 else
7163 {
917317f4
JM
7164 /* Normal mode: Send target system values byte by byte, in
7165 increasing byte addresses. Each byte is encoded as a two hex
7166 value. */
124e13d9
SM
7167 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
7168 units_written = todo_units;
c906108c 7169 }
802188a7 7170
2e9f7625 7171 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 7172 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 7173
2e9f7625 7174 if (rs->buf[0] == 'E')
00d84524 7175 return TARGET_XFER_E_IO;
802188a7 7176
124e13d9
SM
7177 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
7178 send fewer units than we'd planned. */
7179 *xfered_len_units = (ULONGEST) units_written;
9b409511 7180 return TARGET_XFER_OK;
c906108c
SS
7181}
7182
a76d924d
DJ
7183/* Write memory data directly to the remote machine.
7184 This does not inform the data cache; the data cache uses this.
7185 MEMADDR is the address in the remote memory space.
7186 MYADDR is the address of the buffer in our space.
7187 LEN is the number of bytes.
7188
9b409511
YQ
7189 Return the transferred status, error or OK (an
7190 'enum target_xfer_status' value). Save the number of bytes
7191 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 7192
9b409511
YQ
7193static enum target_xfer_status
7194remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
124e13d9 7195 int unit_size, ULONGEST *xfered_len)
a76d924d
DJ
7196{
7197 char *packet_format = 0;
7198
7199 /* Check whether the target supports binary download. */
7200 check_binary_download (memaddr);
7201
4082afcc 7202 switch (packet_support (PACKET_X))
a76d924d
DJ
7203 {
7204 case PACKET_ENABLE:
7205 packet_format = "X";
7206 break;
7207 case PACKET_DISABLE:
7208 packet_format = "M";
7209 break;
7210 case PACKET_SUPPORT_UNKNOWN:
7211 internal_error (__FILE__, __LINE__,
7212 _("remote_write_bytes: bad internal state"));
7213 default:
7214 internal_error (__FILE__, __LINE__, _("bad switch"));
7215 }
7216
7217 return remote_write_bytes_aux (packet_format,
124e13d9 7218 memaddr, myaddr, len, unit_size, xfered_len,
9b409511 7219 packet_format[0], 1);
a76d924d
DJ
7220}
7221
9217e74e
YQ
7222/* Read memory data directly from the remote machine.
7223 This does not use the data cache; the data cache uses this.
7224 MEMADDR is the address in the remote memory space.
7225 MYADDR is the address of the buffer in our space.
124e13d9
SM
7226 LEN_UNITS is the number of addressable memory units to read..
7227 UNIT_SIZE is the length in bytes of an addressable unit.
9217e74e
YQ
7228
7229 Return the transferred status, error or OK (an
7230 'enum target_xfer_status' value). Save the number of bytes
124e13d9
SM
7231 transferred in *XFERED_LEN_UNITS.
7232
7233 See the comment of remote_write_bytes_aux for an example of
7234 memory read/write exchange between gdb and the stub. */
9217e74e
YQ
7235
7236static enum target_xfer_status
124e13d9
SM
7237remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
7238 int unit_size, ULONGEST *xfered_len_units)
9217e74e
YQ
7239{
7240 struct remote_state *rs = get_remote_state ();
124e13d9 7241 int buf_size_bytes; /* Max size of packet output buffer. */
9217e74e 7242 char *p;
124e13d9
SM
7243 int todo_units;
7244 int decoded_bytes;
9217e74e 7245
124e13d9 7246 buf_size_bytes = get_memory_read_packet_size ();
9217e74e
YQ
7247 /* The packet buffer will be large enough for the payload;
7248 get_memory_packet_size ensures this. */
7249
124e13d9
SM
7250 /* Number of units that will fit. */
7251 todo_units = min (len_units, (buf_size_bytes / unit_size) / 2);
9217e74e
YQ
7252
7253 /* Construct "m"<memaddr>","<len>". */
7254 memaddr = remote_address_masked (memaddr);
7255 p = rs->buf;
7256 *p++ = 'm';
7257 p += hexnumstr (p, (ULONGEST) memaddr);
7258 *p++ = ',';
124e13d9 7259 p += hexnumstr (p, (ULONGEST) todo_units);
9217e74e
YQ
7260 *p = '\0';
7261 putpkt (rs->buf);
7262 getpkt (&rs->buf, &rs->buf_size, 0);
7263 if (rs->buf[0] == 'E'
7264 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
7265 && rs->buf[3] == '\0')
7266 return TARGET_XFER_E_IO;
7267 /* Reply describes memory byte by byte, each byte encoded as two hex
7268 characters. */
7269 p = rs->buf;
124e13d9 7270 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
9217e74e 7271 /* Return what we have. Let higher layers handle partial reads. */
124e13d9 7272 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
9217e74e
YQ
7273 return TARGET_XFER_OK;
7274}
7275
b55fbac4
YQ
7276/* Using the set of read-only target sections of remote, read live
7277 read-only memory.
8acf9577
YQ
7278
7279 For interface/parameters/return description see target.h,
7280 to_xfer_partial. */
7281
7282static enum target_xfer_status
b55fbac4
YQ
7283remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
7284 ULONGEST memaddr, ULONGEST len,
124e13d9 7285 int unit_size, ULONGEST *xfered_len)
8acf9577
YQ
7286{
7287 struct target_section *secp;
7288 struct target_section_table *table;
7289
7290 secp = target_section_by_addr (ops, memaddr);
7291 if (secp != NULL
7292 && (bfd_get_section_flags (secp->the_bfd_section->owner,
7293 secp->the_bfd_section)
7294 & SEC_READONLY))
7295 {
7296 struct target_section *p;
7297 ULONGEST memend = memaddr + len;
7298
7299 table = target_get_section_table (ops);
7300
7301 for (p = table->sections; p < table->sections_end; p++)
7302 {
7303 if (memaddr >= p->addr)
7304 {
7305 if (memend <= p->endaddr)
7306 {
7307 /* Entire transfer is within this section. */
124e13d9 7308 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7309 xfered_len);
8acf9577
YQ
7310 }
7311 else if (memaddr >= p->endaddr)
7312 {
7313 /* This section ends before the transfer starts. */
7314 continue;
7315 }
7316 else
7317 {
7318 /* This section overlaps the transfer. Just do half. */
7319 len = p->endaddr - memaddr;
124e13d9 7320 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7321 xfered_len);
8acf9577
YQ
7322 }
7323 }
7324 }
7325 }
7326
7327 return TARGET_XFER_EOF;
7328}
7329
9217e74e
YQ
7330/* Similar to remote_read_bytes_1, but it reads from the remote stub
7331 first if the requested memory is unavailable in traceframe.
7332 Otherwise, fall back to remote_read_bytes_1. */
c906108c 7333
9b409511 7334static enum target_xfer_status
8acf9577 7335remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
124e13d9
SM
7336 gdb_byte *myaddr, ULONGEST len, int unit_size,
7337 ULONGEST *xfered_len)
c906108c 7338{
6b6aa828 7339 if (len == 0)
96c4f946 7340 return TARGET_XFER_EOF;
b2182ed2 7341
8acf9577
YQ
7342 if (get_traceframe_number () != -1)
7343 {
7344 VEC(mem_range_s) *available;
7345
7346 /* If we fail to get the set of available memory, then the
7347 target does not support querying traceframe info, and so we
7348 attempt reading from the traceframe anyway (assuming the
7349 target implements the old QTro packet then). */
7350 if (traceframe_available_memory (&available, memaddr, len))
7351 {
7352 struct cleanup *old_chain;
7353
7354 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
7355
7356 if (VEC_empty (mem_range_s, available)
7357 || VEC_index (mem_range_s, available, 0)->start != memaddr)
7358 {
7359 enum target_xfer_status res;
7360
7361 /* Don't read into the traceframe's available
7362 memory. */
7363 if (!VEC_empty (mem_range_s, available))
7364 {
7365 LONGEST oldlen = len;
7366
7367 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
7368 gdb_assert (len <= oldlen);
7369 }
7370
7371 do_cleanups (old_chain);
7372
7373 /* This goes through the topmost target again. */
b55fbac4 7374 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
124e13d9 7375 len, unit_size, xfered_len);
8acf9577
YQ
7376 if (res == TARGET_XFER_OK)
7377 return TARGET_XFER_OK;
7378 else
7379 {
7380 /* No use trying further, we know some memory starting
7381 at MEMADDR isn't available. */
7382 *xfered_len = len;
7383 return TARGET_XFER_UNAVAILABLE;
7384 }
7385 }
7386
7387 /* Don't try to read more than how much is available, in
7388 case the target implements the deprecated QTro packet to
7389 cater for older GDBs (the target's knowledge of read-only
7390 sections may be outdated by now). */
7391 len = VEC_index (mem_range_s, available, 0)->length;
7392
7393 do_cleanups (old_chain);
7394 }
7395 }
7396
124e13d9 7397 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
c906108c 7398}
74531fed 7399
c906108c 7400\f
c906108c 7401
a76d924d
DJ
7402/* Sends a packet with content determined by the printf format string
7403 FORMAT and the remaining arguments, then gets the reply. Returns
7404 whether the packet was a success, a failure, or unknown. */
7405
77b64a49
PA
7406static enum packet_result remote_send_printf (const char *format, ...)
7407 ATTRIBUTE_PRINTF (1, 2);
7408
2c0b251b 7409static enum packet_result
a76d924d
DJ
7410remote_send_printf (const char *format, ...)
7411{
7412 struct remote_state *rs = get_remote_state ();
7413 int max_size = get_remote_packet_size ();
a76d924d 7414 va_list ap;
a744cf53 7415
a76d924d
DJ
7416 va_start (ap, format);
7417
7418 rs->buf[0] = '\0';
7419 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 7420 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
7421
7422 if (putpkt (rs->buf) < 0)
7423 error (_("Communication problem with target."));
7424
7425 rs->buf[0] = '\0';
7426 getpkt (&rs->buf, &rs->buf_size, 0);
7427
7428 return packet_check_result (rs->buf);
7429}
7430
7431static void
7432restore_remote_timeout (void *p)
7433{
7434 int value = *(int *)p;
a744cf53 7435
a76d924d
DJ
7436 remote_timeout = value;
7437}
7438
7439/* Flash writing can take quite some time. We'll set
7440 effectively infinite timeout for flash operations.
7441 In future, we'll need to decide on a better approach. */
7442static const int remote_flash_timeout = 1000;
7443
7444static void
7445remote_flash_erase (struct target_ops *ops,
7446 ULONGEST address, LONGEST length)
7447{
f5656ead 7448 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d
DJ
7449 int saved_remote_timeout = remote_timeout;
7450 enum packet_result ret;
a76d924d
DJ
7451 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7452 &saved_remote_timeout);
a744cf53 7453
a76d924d
DJ
7454 remote_timeout = remote_flash_timeout;
7455
7456 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 7457 phex (address, addr_size),
a76d924d
DJ
7458 phex (length, 4));
7459 switch (ret)
7460 {
7461 case PACKET_UNKNOWN:
7462 error (_("Remote target does not support flash erase"));
7463 case PACKET_ERROR:
7464 error (_("Error erasing flash with vFlashErase packet"));
7465 default:
7466 break;
7467 }
7468
7469 do_cleanups (back_to);
7470}
7471
9b409511
YQ
7472static enum target_xfer_status
7473remote_flash_write (struct target_ops *ops, ULONGEST address,
7474 ULONGEST length, ULONGEST *xfered_len,
7475 const gdb_byte *data)
a76d924d
DJ
7476{
7477 int saved_remote_timeout = remote_timeout;
9b409511 7478 enum target_xfer_status ret;
a76d924d 7479 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
9b409511 7480 &saved_remote_timeout);
a76d924d
DJ
7481
7482 remote_timeout = remote_flash_timeout;
124e13d9 7483 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
9b409511 7484 xfered_len,'X', 0);
a76d924d
DJ
7485 do_cleanups (back_to);
7486
7487 return ret;
7488}
7489
7490static void
7491remote_flash_done (struct target_ops *ops)
7492{
7493 int saved_remote_timeout = remote_timeout;
7494 int ret;
7495 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7496 &saved_remote_timeout);
7497
7498 remote_timeout = remote_flash_timeout;
7499 ret = remote_send_printf ("vFlashDone");
7500 do_cleanups (back_to);
7501
7502 switch (ret)
7503 {
7504 case PACKET_UNKNOWN:
7505 error (_("Remote target does not support vFlashDone"));
7506 case PACKET_ERROR:
7507 error (_("Error finishing flash operation"));
7508 default:
7509 break;
7510 }
7511}
7512
c906108c 7513static void
fba45db2 7514remote_files_info (struct target_ops *ignore)
c906108c
SS
7515{
7516 puts_filtered ("Debugging a target over a serial line.\n");
7517}
7518\f
7519/* Stuff for dealing with the packets which are part of this protocol.
7520 See comment at top of file for details. */
7521
1927e618
PA
7522/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7523 error to higher layers. Called when a serial error is detected.
7524 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
7525 the system error message for errno at function entry and final dot
7526 for output compatibility with throw_perror_with_name. */
1927e618
PA
7527
7528static void
7529unpush_and_perror (const char *string)
7530{
d6cb50a2 7531 int saved_errno = errno;
1927e618
PA
7532
7533 remote_unpush_target ();
d6cb50a2
JK
7534 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7535 safe_strerror (saved_errno));
1927e618
PA
7536}
7537
0876f84a 7538/* Read a single character from the remote end. */
c906108c
SS
7539
7540static int
fba45db2 7541readchar (int timeout)
c906108c
SS
7542{
7543 int ch;
5d93a237 7544 struct remote_state *rs = get_remote_state ();
c906108c 7545
5d93a237 7546 ch = serial_readchar (rs->remote_desc, timeout);
c906108c 7547
2acceee2 7548 if (ch >= 0)
0876f84a 7549 return ch;
2acceee2
JM
7550
7551 switch ((enum serial_rc) ch)
c906108c
SS
7552 {
7553 case SERIAL_EOF:
78a095c3 7554 remote_unpush_target ();
598d3636 7555 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 7556 /* no return */
c906108c 7557 case SERIAL_ERROR:
1927e618
PA
7558 unpush_and_perror (_("Remote communication error. "
7559 "Target disconnected."));
2acceee2 7560 /* no return */
c906108c 7561 case SERIAL_TIMEOUT:
2acceee2 7562 break;
c906108c 7563 }
2acceee2 7564 return ch;
c906108c
SS
7565}
7566
c33e31fd
PA
7567/* Wrapper for serial_write that closes the target and throws if
7568 writing fails. */
7569
7570static void
7571remote_serial_write (const char *str, int len)
7572{
5d93a237
TT
7573 struct remote_state *rs = get_remote_state ();
7574
7575 if (serial_write (rs->remote_desc, str, len))
c33e31fd 7576 {
1927e618
PA
7577 unpush_and_perror (_("Remote communication error. "
7578 "Target disconnected."));
c33e31fd
PA
7579 }
7580}
7581
6d820c5c
DJ
7582/* Send the command in *BUF to the remote machine, and read the reply
7583 into *BUF. Report an error if we get an error reply. Resize
7584 *BUF using xrealloc if necessary to hold the result, and update
7585 *SIZEOF_BUF. */
c906108c
SS
7586
7587static void
6d820c5c
DJ
7588remote_send (char **buf,
7589 long *sizeof_buf)
c906108c 7590{
6d820c5c 7591 putpkt (*buf);
c2d11a7d 7592 getpkt (buf, sizeof_buf, 0);
c906108c 7593
6d820c5c
DJ
7594 if ((*buf)[0] == 'E')
7595 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
7596}
7597
6e5abd65
PA
7598/* Return a pointer to an xmalloc'ed string representing an escaped
7599 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7600 etc. The caller is responsible for releasing the returned
7601 memory. */
7602
7603static char *
7604escape_buffer (const char *buf, int n)
7605{
7606 struct cleanup *old_chain;
7607 struct ui_file *stb;
7608 char *str;
6e5abd65
PA
7609
7610 stb = mem_fileopen ();
7611 old_chain = make_cleanup_ui_file_delete (stb);
7612
6ef284bd 7613 fputstrn_unfiltered (buf, n, '\\', stb);
759ef836 7614 str = ui_file_xstrdup (stb, NULL);
6e5abd65
PA
7615 do_cleanups (old_chain);
7616 return str;
7617}
7618
c906108c
SS
7619/* Display a null-terminated packet on stdout, for debugging, using C
7620 string notation. */
7621
7622static void
baa336ce 7623print_packet (const char *buf)
c906108c
SS
7624{
7625 puts_filtered ("\"");
43e526b9 7626 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
7627 puts_filtered ("\"");
7628}
7629
7630int
baa336ce 7631putpkt (const char *buf)
c906108c
SS
7632{
7633 return putpkt_binary (buf, strlen (buf));
7634}
7635
7636/* Send a packet to the remote machine, with error checking. The data
23860348 7637 of the packet is in BUF. The string in BUF can be at most
ea9c271d 7638 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
7639 and for a possible /0 if we are debugging (remote_debug) and want
7640 to print the sent packet as a string. */
c906108c
SS
7641
7642static int
baa336ce 7643putpkt_binary (const char *buf, int cnt)
c906108c 7644{
2d717e4f 7645 struct remote_state *rs = get_remote_state ();
c906108c
SS
7646 int i;
7647 unsigned char csum = 0;
11cf8741 7648 char *buf2 = alloca (cnt + 6);
085dd6e6 7649
c906108c
SS
7650 int ch;
7651 int tcount = 0;
7652 char *p;
dd61ec5c 7653 char *message;
c906108c 7654
e24a49d8
PA
7655 /* Catch cases like trying to read memory or listing threads while
7656 we're waiting for a stop reply. The remote server wouldn't be
7657 ready to handle this request, so we'd hang and timeout. We don't
7658 have to worry about this in synchronous mode, because in that
7659 case it's not possible to issue a command while the target is
74531fed
PA
7660 running. This is not a problem in non-stop mode, because in that
7661 case, the stub is always ready to process serial input. */
d9d41e78 7662 if (!non_stop && target_is_async_p () && rs->waiting_for_stop_reply)
9597b22a
DE
7663 {
7664 error (_("Cannot execute this command while the target is running.\n"
7665 "Use the \"interrupt\" command to stop the target\n"
7666 "and then try again."));
7667 }
e24a49d8 7668
2d717e4f
DJ
7669 /* We're sending out a new packet. Make sure we don't look at a
7670 stale cached response. */
7671 rs->cached_wait_status = 0;
7672
c906108c
SS
7673 /* Copy the packet into buffer BUF2, encapsulating it
7674 and giving it a checksum. */
7675
c906108c
SS
7676 p = buf2;
7677 *p++ = '$';
7678
7679 for (i = 0; i < cnt; i++)
7680 {
7681 csum += buf[i];
7682 *p++ = buf[i];
7683 }
7684 *p++ = '#';
7685 *p++ = tohex ((csum >> 4) & 0xf);
7686 *p++ = tohex (csum & 0xf);
7687
7688 /* Send it over and over until we get a positive ack. */
7689
7690 while (1)
7691 {
7692 int started_error_output = 0;
7693
7694 if (remote_debug)
7695 {
6e5abd65
PA
7696 struct cleanup *old_chain;
7697 char *str;
7698
c906108c 7699 *p = '\0';
6e5abd65
PA
7700 str = escape_buffer (buf2, p - buf2);
7701 old_chain = make_cleanup (xfree, str);
7702 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
0f71a2f6 7703 gdb_flush (gdb_stdlog);
6e5abd65 7704 do_cleanups (old_chain);
c906108c 7705 }
c33e31fd 7706 remote_serial_write (buf2, p - buf2);
c906108c 7707
a6f3e723
SL
7708 /* If this is a no acks version of the remote protocol, send the
7709 packet and move on. */
7710 if (rs->noack_mode)
7711 break;
7712
74531fed
PA
7713 /* Read until either a timeout occurs (-2) or '+' is read.
7714 Handle any notification that arrives in the mean time. */
c906108c
SS
7715 while (1)
7716 {
7717 ch = readchar (remote_timeout);
7718
c5aa993b 7719 if (remote_debug)
c906108c
SS
7720 {
7721 switch (ch)
7722 {
7723 case '+':
1216fa2c 7724 case '-':
c906108c
SS
7725 case SERIAL_TIMEOUT:
7726 case '$':
74531fed 7727 case '%':
c906108c
SS
7728 if (started_error_output)
7729 {
7730 putchar_unfiltered ('\n');
7731 started_error_output = 0;
7732 }
7733 }
7734 }
7735
7736 switch (ch)
7737 {
7738 case '+':
7739 if (remote_debug)
0f71a2f6 7740 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 7741 return 1;
1216fa2c
AC
7742 case '-':
7743 if (remote_debug)
7744 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 7745 /* FALLTHROUGH */
c906108c 7746 case SERIAL_TIMEOUT:
c5aa993b 7747 tcount++;
c906108c
SS
7748 if (tcount > 3)
7749 return 0;
23860348 7750 break; /* Retransmit buffer. */
c906108c
SS
7751 case '$':
7752 {
40e3f985 7753 if (remote_debug)
2bc416ba 7754 fprintf_unfiltered (gdb_stdlog,
23860348 7755 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
7756 /* It's probably an old response sent because an ACK
7757 was lost. Gobble up the packet and ack it so it
7758 doesn't get retransmitted when we resend this
7759 packet. */
6d820c5c 7760 skip_frame ();
c33e31fd 7761 remote_serial_write ("+", 1);
23860348 7762 continue; /* Now, go look for +. */
c906108c 7763 }
74531fed
PA
7764
7765 case '%':
7766 {
7767 int val;
7768
7769 /* If we got a notification, handle it, and go back to looking
7770 for an ack. */
7771 /* We've found the start of a notification. Now
7772 collect the data. */
7773 val = read_frame (&rs->buf, &rs->buf_size);
7774 if (val >= 0)
7775 {
7776 if (remote_debug)
7777 {
6e5abd65
PA
7778 struct cleanup *old_chain;
7779 char *str;
7780
7781 str = escape_buffer (rs->buf, val);
7782 old_chain = make_cleanup (xfree, str);
7783 fprintf_unfiltered (gdb_stdlog,
7784 " Notification received: %s\n",
7785 str);
7786 do_cleanups (old_chain);
74531fed 7787 }
5965e028 7788 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
7789 /* We're in sync now, rewait for the ack. */
7790 tcount = 0;
7791 }
7792 else
7793 {
7794 if (remote_debug)
7795 {
7796 if (!started_error_output)
7797 {
7798 started_error_output = 1;
7799 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7800 }
7801 fputc_unfiltered (ch & 0177, gdb_stdlog);
7802 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7803 }
7804 }
7805 continue;
7806 }
7807 /* fall-through */
c906108c
SS
7808 default:
7809 if (remote_debug)
7810 {
7811 if (!started_error_output)
7812 {
7813 started_error_output = 1;
0f71a2f6 7814 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 7815 }
0f71a2f6 7816 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
7817 }
7818 continue;
7819 }
23860348 7820 break; /* Here to retransmit. */
c906108c
SS
7821 }
7822
7823#if 0
7824 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
7825 able to get out next time we call QUIT, without anything as
7826 violent as interrupt_query. If we want to provide a way out of
7827 here without getting to the next QUIT, it should be based on
7828 hitting ^C twice as in remote_wait. */
c906108c
SS
7829 if (quit_flag)
7830 {
7831 quit_flag = 0;
7832 interrupt_query ();
7833 }
7834#endif
7835 }
a6f3e723 7836 return 0;
c906108c
SS
7837}
7838
6d820c5c
DJ
7839/* Come here after finding the start of a frame when we expected an
7840 ack. Do our best to discard the rest of this packet. */
7841
7842static void
7843skip_frame (void)
7844{
7845 int c;
7846
7847 while (1)
7848 {
7849 c = readchar (remote_timeout);
7850 switch (c)
7851 {
7852 case SERIAL_TIMEOUT:
7853 /* Nothing we can do. */
7854 return;
7855 case '#':
7856 /* Discard the two bytes of checksum and stop. */
7857 c = readchar (remote_timeout);
7858 if (c >= 0)
7859 c = readchar (remote_timeout);
7860
7861 return;
7862 case '*': /* Run length encoding. */
7863 /* Discard the repeat count. */
7864 c = readchar (remote_timeout);
7865 if (c < 0)
7866 return;
7867 break;
7868 default:
7869 /* A regular character. */
7870 break;
7871 }
7872 }
7873}
7874
c906108c 7875/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
7876 into *BUF, verifying the checksum, length, and handling run-length
7877 compression. NUL terminate the buffer. If there is not enough room,
7878 expand *BUF using xrealloc.
c906108c 7879
c2d11a7d
JM
7880 Returns -1 on error, number of characters in buffer (ignoring the
7881 trailing NULL) on success. (could be extended to return one of the
23860348 7882 SERIAL status indications). */
c2d11a7d
JM
7883
7884static long
6d820c5c
DJ
7885read_frame (char **buf_p,
7886 long *sizeof_buf)
c906108c
SS
7887{
7888 unsigned char csum;
c2d11a7d 7889 long bc;
c906108c 7890 int c;
6d820c5c 7891 char *buf = *buf_p;
a6f3e723 7892 struct remote_state *rs = get_remote_state ();
c906108c
SS
7893
7894 csum = 0;
c2d11a7d 7895 bc = 0;
c906108c
SS
7896
7897 while (1)
7898 {
7899 c = readchar (remote_timeout);
c906108c
SS
7900 switch (c)
7901 {
7902 case SERIAL_TIMEOUT:
7903 if (remote_debug)
0f71a2f6 7904 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 7905 return -1;
c906108c
SS
7906 case '$':
7907 if (remote_debug)
0f71a2f6
JM
7908 fputs_filtered ("Saw new packet start in middle of old one\n",
7909 gdb_stdlog);
23860348 7910 return -1; /* Start a new packet, count retries. */
c906108c
SS
7911 case '#':
7912 {
7913 unsigned char pktcsum;
e1b09194
AC
7914 int check_0 = 0;
7915 int check_1 = 0;
c906108c 7916
c2d11a7d 7917 buf[bc] = '\0';
c906108c 7918
e1b09194
AC
7919 check_0 = readchar (remote_timeout);
7920 if (check_0 >= 0)
7921 check_1 = readchar (remote_timeout);
802188a7 7922
e1b09194
AC
7923 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7924 {
7925 if (remote_debug)
2bc416ba 7926 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 7927 gdb_stdlog);
e1b09194
AC
7928 return -1;
7929 }
7930 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
7931 {
7932 if (remote_debug)
2bc416ba 7933 fputs_filtered ("Communication error in checksum\n",
23860348 7934 gdb_stdlog);
40e3f985
FN
7935 return -1;
7936 }
c906108c 7937
a6f3e723
SL
7938 /* Don't recompute the checksum; with no ack packets we
7939 don't have any way to indicate a packet retransmission
7940 is necessary. */
7941 if (rs->noack_mode)
7942 return bc;
7943
e1b09194 7944 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 7945 if (csum == pktcsum)
c2d11a7d 7946 return bc;
c906108c 7947
c5aa993b 7948 if (remote_debug)
c906108c 7949 {
6e5abd65
PA
7950 struct cleanup *old_chain;
7951 char *str;
7952
7953 str = escape_buffer (buf, bc);
7954 old_chain = make_cleanup (xfree, str);
7955 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
7956 "Bad checksum, sentsum=0x%x, "
7957 "csum=0x%x, buf=%s\n",
6e5abd65
PA
7958 pktcsum, csum, str);
7959 do_cleanups (old_chain);
c906108c 7960 }
c2d11a7d 7961 /* Number of characters in buffer ignoring trailing
23860348 7962 NULL. */
c2d11a7d 7963 return -1;
c906108c 7964 }
23860348 7965 case '*': /* Run length encoding. */
c2c6d25f
JM
7966 {
7967 int repeat;
c906108c 7968
a744cf53 7969 csum += c;
b4501125
AC
7970 c = readchar (remote_timeout);
7971 csum += c;
23860348 7972 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 7973
23860348 7974 /* The character before ``*'' is repeated. */
c2d11a7d 7975
6d820c5c 7976 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 7977 {
6d820c5c
DJ
7978 if (bc + repeat - 1 >= *sizeof_buf - 1)
7979 {
7980 /* Make some more room in the buffer. */
7981 *sizeof_buf += repeat;
7982 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7983 buf = *buf_p;
7984 }
7985
c2d11a7d
JM
7986 memset (&buf[bc], buf[bc - 1], repeat);
7987 bc += repeat;
c2c6d25f
JM
7988 continue;
7989 }
7990
c2d11a7d 7991 buf[bc] = '\0';
6d820c5c 7992 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 7993 return -1;
c2c6d25f 7994 }
c906108c 7995 default:
6d820c5c 7996 if (bc >= *sizeof_buf - 1)
c906108c 7997 {
6d820c5c
DJ
7998 /* Make some more room in the buffer. */
7999 *sizeof_buf *= 2;
8000 *buf_p = xrealloc (*buf_p, *sizeof_buf);
8001 buf = *buf_p;
c906108c
SS
8002 }
8003
6d820c5c
DJ
8004 buf[bc++] = c;
8005 csum += c;
8006 continue;
c906108c
SS
8007 }
8008 }
8009}
8010
8011/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
8012 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
8013 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
8014 rather than timing out; this is used (in synchronous mode) to wait
8015 for a target that is is executing user code to stop. */
d9fcf2fb
JM
8016/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
8017 don't have to change all the calls to getpkt to deal with the
8018 return value, because at the moment I don't know what the right
23860348 8019 thing to do it for those. */
c906108c 8020void
6d820c5c
DJ
8021getpkt (char **buf,
8022 long *sizeof_buf,
c2d11a7d 8023 int forever)
d9fcf2fb
JM
8024{
8025 int timed_out;
8026
8027 timed_out = getpkt_sane (buf, sizeof_buf, forever);
8028}
8029
8030
8031/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
8032 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
8033 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
8034 rather than timing out; this is used (in synchronous mode) to wait
8035 for a target that is is executing user code to stop. If FOREVER ==
8036 0, this function is allowed to time out gracefully and return an
74531fed
PA
8037 indication of this to the caller. Otherwise return the number of
8038 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
8039 enough reason to return to the caller. *IS_NOTIF is an output
8040 boolean that indicates whether *BUF holds a notification or not
8041 (a regular packet). */
74531fed 8042
3172dc30 8043static int
74531fed 8044getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 8045 int expecting_notif, int *is_notif)
c906108c 8046{
2d717e4f 8047 struct remote_state *rs = get_remote_state ();
c906108c
SS
8048 int c;
8049 int tries;
8050 int timeout;
df4b58fe 8051 int val = -1;
c906108c 8052
2d717e4f
DJ
8053 /* We're reading a new response. Make sure we don't look at a
8054 previously cached response. */
8055 rs->cached_wait_status = 0;
8056
6d820c5c 8057 strcpy (*buf, "timeout");
c906108c
SS
8058
8059 if (forever)
74531fed
PA
8060 timeout = watchdog > 0 ? watchdog : -1;
8061 else if (expecting_notif)
8062 timeout = 0; /* There should already be a char in the buffer. If
8063 not, bail out. */
c906108c
SS
8064 else
8065 timeout = remote_timeout;
8066
8067#define MAX_TRIES 3
8068
74531fed
PA
8069 /* Process any number of notifications, and then return when
8070 we get a packet. */
8071 for (;;)
c906108c 8072 {
d9c43928 8073 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
8074 times. */
8075 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 8076 {
74531fed
PA
8077 /* This can loop forever if the remote side sends us
8078 characters continuously, but if it pauses, we'll get
8079 SERIAL_TIMEOUT from readchar because of timeout. Then
8080 we'll count that as a retry.
8081
8082 Note that even when forever is set, we will only wait
8083 forever prior to the start of a packet. After that, we
8084 expect characters to arrive at a brisk pace. They should
8085 show up within remote_timeout intervals. */
8086 do
8087 c = readchar (timeout);
8088 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
8089
8090 if (c == SERIAL_TIMEOUT)
8091 {
74531fed
PA
8092 if (expecting_notif)
8093 return -1; /* Don't complain, it's normal to not get
8094 anything in this case. */
8095
23860348 8096 if (forever) /* Watchdog went off? Kill the target. */
c906108c 8097 {
2acceee2 8098 QUIT;
78a095c3 8099 remote_unpush_target ();
598d3636
JK
8100 throw_error (TARGET_CLOSE_ERROR,
8101 _("Watchdog timeout has expired. "
8102 "Target detached."));
c906108c 8103 }
c906108c 8104 if (remote_debug)
0f71a2f6 8105 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 8106 }
74531fed
PA
8107 else
8108 {
8109 /* We've found the start of a packet or notification.
8110 Now collect the data. */
8111 val = read_frame (buf, sizeof_buf);
8112 if (val >= 0)
8113 break;
8114 }
8115
c33e31fd 8116 remote_serial_write ("-", 1);
c906108c 8117 }
c906108c 8118
74531fed
PA
8119 if (tries > MAX_TRIES)
8120 {
8121 /* We have tried hard enough, and just can't receive the
8122 packet/notification. Give up. */
8123 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 8124
74531fed
PA
8125 /* Skip the ack char if we're in no-ack mode. */
8126 if (!rs->noack_mode)
c33e31fd 8127 remote_serial_write ("+", 1);
74531fed
PA
8128 return -1;
8129 }
c906108c 8130
74531fed
PA
8131 /* If we got an ordinary packet, return that to our caller. */
8132 if (c == '$')
c906108c
SS
8133 {
8134 if (remote_debug)
43e526b9 8135 {
6e5abd65
PA
8136 struct cleanup *old_chain;
8137 char *str;
8138
8139 str = escape_buffer (*buf, val);
8140 old_chain = make_cleanup (xfree, str);
8141 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
8142 do_cleanups (old_chain);
43e526b9 8143 }
a6f3e723
SL
8144
8145 /* Skip the ack char if we're in no-ack mode. */
8146 if (!rs->noack_mode)
c33e31fd 8147 remote_serial_write ("+", 1);
fee9eda9
YQ
8148 if (is_notif != NULL)
8149 *is_notif = 0;
0876f84a 8150 return val;
c906108c
SS
8151 }
8152
74531fed
PA
8153 /* If we got a notification, handle it, and go back to looking
8154 for a packet. */
8155 else
8156 {
8157 gdb_assert (c == '%');
8158
8159 if (remote_debug)
8160 {
6e5abd65
PA
8161 struct cleanup *old_chain;
8162 char *str;
8163
8164 str = escape_buffer (*buf, val);
8165 old_chain = make_cleanup (xfree, str);
8166 fprintf_unfiltered (gdb_stdlog,
8167 " Notification received: %s\n",
8168 str);
8169 do_cleanups (old_chain);
74531fed 8170 }
fee9eda9
YQ
8171 if (is_notif != NULL)
8172 *is_notif = 1;
c906108c 8173
5965e028 8174 handle_notification (rs->notif_state, *buf);
c906108c 8175
74531fed 8176 /* Notifications require no acknowledgement. */
a6f3e723 8177
74531fed 8178 if (expecting_notif)
fee9eda9 8179 return val;
74531fed
PA
8180 }
8181 }
8182}
8183
8184static int
8185getpkt_sane (char **buf, long *sizeof_buf, int forever)
8186{
fee9eda9 8187 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
8188}
8189
8190static int
fee9eda9
YQ
8191getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
8192 int *is_notif)
74531fed 8193{
fee9eda9
YQ
8194 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
8195 is_notif);
c906108c 8196}
74531fed 8197
cbb8991c
DB
8198/* Check whether EVENT is a fork event for the process specified
8199 by the pid passed in DATA, and if it is, kill the fork child. */
8200
8201static int
8202kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
8203 QUEUE_ITER (stop_reply_p) *iter,
8204 stop_reply_p event,
8205 void *data)
8206{
8207 struct queue_iter_param *param = data;
8208 int parent_pid = *(int *) param->input;
8209
8210 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
8211 {
8212 struct remote_state *rs = get_remote_state ();
8213 int child_pid = ptid_get_pid (event->ws.value.related_pid);
8214 int res;
8215
8216 res = remote_vkill (child_pid, rs);
8217 if (res != 0)
8218 error (_("Can't kill fork child process %d"), child_pid);
8219 }
8220
8221 return 1;
8222}
8223
8224/* Kill any new fork children of process PID that haven't been
8225 processed by follow_fork. */
8226
8227static void
8228kill_new_fork_children (int pid, struct remote_state *rs)
8229{
8230 struct thread_info *thread;
8231 struct notif_client *notif = &notif_client_stop;
8232 struct queue_iter_param param;
8233
8234 /* Kill the fork child threads of any threads in process PID
8235 that are stopped at a fork event. */
8236 ALL_NON_EXITED_THREADS (thread)
8237 {
8238 struct target_waitstatus *ws = &thread->pending_follow;
8239
8240 if (is_pending_fork_parent (ws, pid, thread->ptid))
8241 {
8242 struct remote_state *rs = get_remote_state ();
8243 int child_pid = ptid_get_pid (ws->value.related_pid);
8244 int res;
8245
8246 res = remote_vkill (child_pid, rs);
8247 if (res != 0)
8248 error (_("Can't kill fork child process %d"), child_pid);
8249 }
8250 }
8251
8252 /* Check for any pending fork events (not reported or processed yet)
8253 in process PID and kill those fork child threads as well. */
8254 remote_notif_get_pending_events (notif);
8255 param.input = &pid;
8256 param.output = NULL;
8257 QUEUE_iterate (stop_reply_p, stop_reply_queue,
8258 kill_child_of_pending_fork, &param);
8259}
8260
c906108c
SS
8261\f
8262static void
7d85a9c0 8263remote_kill (struct target_ops *ops)
43ff13b4 8264{
0fdf84ca
PA
8265
8266 /* Catch errors so the user can quit from gdb even when we
23860348 8267 aren't on speaking terms with the remote system. */
492d29ea 8268 TRY
0fdf84ca
PA
8269 {
8270 putpkt ("k");
8271 }
492d29ea 8272 CATCH (ex, RETURN_MASK_ERROR)
0fdf84ca
PA
8273 {
8274 if (ex.error == TARGET_CLOSE_ERROR)
8275 {
8276 /* If we got an (EOF) error that caused the target
8277 to go away, then we're done, that's what we wanted.
8278 "k" is susceptible to cause a premature EOF, given
8279 that the remote server isn't actually required to
8280 reply to "k", and it can happen that it doesn't
8281 even get to reply ACK to the "k". */
8282 return;
8283 }
8284
8285 /* Otherwise, something went wrong. We didn't actually kill
8286 the target. Just propagate the exception, and let the
8287 user or higher layers decide what to do. */
8288 throw_exception (ex);
8289 }
492d29ea 8290 END_CATCH
43ff13b4 8291
0fdf84ca
PA
8292 /* We've killed the remote end, we get to mourn it. Since this is
8293 target remote, single-process, mourning the inferior also
8294 unpushes remote_ops. */
43ff13b4
JM
8295 target_mourn_inferior ();
8296}
8297
82f73884
PA
8298static int
8299remote_vkill (int pid, struct remote_state *rs)
8300{
4082afcc 8301 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
8302 return -1;
8303
8304 /* Tell the remote target to detach. */
bba74b36 8305 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
8306 putpkt (rs->buf);
8307 getpkt (&rs->buf, &rs->buf_size, 0);
8308
4082afcc
PA
8309 switch (packet_ok (rs->buf,
8310 &remote_protocol_packets[PACKET_vKill]))
8311 {
8312 case PACKET_OK:
8313 return 0;
8314 case PACKET_ERROR:
8315 return 1;
8316 case PACKET_UNKNOWN:
8317 return -1;
8318 default:
8319 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
8320 }
82f73884
PA
8321}
8322
8323static void
7d85a9c0 8324extended_remote_kill (struct target_ops *ops)
82f73884
PA
8325{
8326 int res;
8327 int pid = ptid_get_pid (inferior_ptid);
8328 struct remote_state *rs = get_remote_state ();
8329
cbb8991c
DB
8330 /* If we're stopped while forking and we haven't followed yet, kill the
8331 child task. We need to do this before killing the parent task
8332 because if this is a vfork then the parent will be sleeping. */
8333 kill_new_fork_children (pid, rs);
8334
82f73884 8335 res = remote_vkill (pid, rs);
901f9912 8336 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
82f73884
PA
8337 {
8338 /* Don't try 'k' on a multi-process aware stub -- it has no way
8339 to specify the pid. */
8340
8341 putpkt ("k");
8342#if 0
8343 getpkt (&rs->buf, &rs->buf_size, 0);
8344 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
8345 res = 1;
8346#else
8347 /* Don't wait for it to die. I'm not really sure it matters whether
8348 we do or not. For the existing stubs, kill is a noop. */
8349 res = 0;
8350#endif
8351 }
8352
8353 if (res != 0)
8354 error (_("Can't kill process"));
8355
82f73884
PA
8356 target_mourn_inferior ();
8357}
8358
c906108c 8359static void
20f796c9 8360remote_mourn (struct target_ops *target)
c906108c
SS
8361{
8362 unpush_target (target);
ce5ce7ed 8363
8a2492ee
PA
8364 /* remote_close takes care of doing most of the clean up. */
8365 generic_mourn_inferior ();
c906108c
SS
8366}
8367
2d717e4f 8368static void
20f796c9 8369extended_remote_mourn (struct target_ops *target)
2d717e4f
DJ
8370{
8371 struct remote_state *rs = get_remote_state ();
c906108c 8372
e24a49d8
PA
8373 /* In case we got here due to an error, but we're going to stay
8374 connected. */
8375 rs->waiting_for_stop_reply = 0;
8376
dc1981d7
PA
8377 /* If the current general thread belonged to the process we just
8378 detached from or has exited, the remote side current general
8379 thread becomes undefined. Considering a case like this:
8380
8381 - We just got here due to a detach.
8382 - The process that we're detaching from happens to immediately
8383 report a global breakpoint being hit in non-stop mode, in the
8384 same thread we had selected before.
8385 - GDB attaches to this process again.
8386 - This event happens to be the next event we handle.
8387
8388 GDB would consider that the current general thread didn't need to
8389 be set on the stub side (with Hg), since for all it knew,
8390 GENERAL_THREAD hadn't changed.
8391
8392 Notice that although in all-stop mode, the remote server always
8393 sets the current thread to the thread reporting the stop event,
8394 that doesn't happen in non-stop mode; in non-stop, the stub *must
8395 not* change the current thread when reporting a breakpoint hit,
8396 due to the decoupling of event reporting and event handling.
8397
8398 To keep things simple, we always invalidate our notion of the
8399 current thread. */
47f8a51d 8400 record_currthread (rs, minus_one_ptid);
dc1981d7 8401
2d717e4f
DJ
8402 /* Unlike "target remote", we do not want to unpush the target; then
8403 the next time the user says "run", we won't be connected. */
8404
48aa3c27
PA
8405 /* Call common code to mark the inferior as not running. */
8406 generic_mourn_inferior ();
8407
d729566a 8408 if (!have_inferiors ())
2d717e4f 8409 {
82f73884
PA
8410 if (!remote_multi_process_p (rs))
8411 {
8412 /* Check whether the target is running now - some remote stubs
8413 automatically restart after kill. */
8414 putpkt ("?");
8415 getpkt (&rs->buf, &rs->buf_size, 0);
8416
8417 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
8418 {
3e43a32a
MS
8419 /* Assume that the target has been restarted. Set
8420 inferior_ptid so that bits of core GDB realizes
8421 there's something here, e.g., so that the user can
8422 say "kill" again. */
82f73884
PA
8423 inferior_ptid = magic_null_ptid;
8424 }
82f73884 8425 }
2d717e4f
DJ
8426 }
8427}
c906108c 8428
03583c20 8429static int
2bfc0540 8430extended_remote_supports_disable_randomization (struct target_ops *self)
03583c20 8431{
4082afcc 8432 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
8433}
8434
8435static void
8436extended_remote_disable_randomization (int val)
8437{
8438 struct remote_state *rs = get_remote_state ();
8439 char *reply;
8440
bba74b36
YQ
8441 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
8442 val);
03583c20
UW
8443 putpkt (rs->buf);
8444 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
8445 if (*reply == '\0')
8446 error (_("Target does not support QDisableRandomization."));
8447 if (strcmp (reply, "OK") != 0)
8448 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
8449}
8450
2d717e4f
DJ
8451static int
8452extended_remote_run (char *args)
8453{
8454 struct remote_state *rs = get_remote_state ();
2d717e4f 8455 int len;
c906108c 8456
2d717e4f
DJ
8457 /* If the user has disabled vRun support, or we have detected that
8458 support is not available, do not try it. */
4082afcc 8459 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 8460 return -1;
424163ea 8461
2d717e4f
DJ
8462 strcpy (rs->buf, "vRun;");
8463 len = strlen (rs->buf);
c906108c 8464
2d717e4f
DJ
8465 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
8466 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
8467 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
8468 strlen (remote_exec_file));
2d717e4f 8469
d1a41061 8470 gdb_assert (args != NULL);
2d717e4f
DJ
8471 if (*args)
8472 {
8473 struct cleanup *back_to;
8474 int i;
8475 char **argv;
8476
d1a41061 8477 argv = gdb_buildargv (args);
6e366df1 8478 back_to = make_cleanup_freeargv (argv);
2d717e4f
DJ
8479 for (i = 0; argv[i] != NULL; i++)
8480 {
8481 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
8482 error (_("Argument list too long for run packet"));
8483 rs->buf[len++] = ';';
9f1b45b0
TT
8484 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
8485 strlen (argv[i]));
2d717e4f
DJ
8486 }
8487 do_cleanups (back_to);
8488 }
8489
8490 rs->buf[len++] = '\0';
8491
8492 putpkt (rs->buf);
8493 getpkt (&rs->buf, &rs->buf_size, 0);
8494
4082afcc 8495 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 8496 {
4082afcc 8497 case PACKET_OK:
3405876a 8498 /* We have a wait response. All is well. */
2d717e4f 8499 return 0;
4082afcc
PA
8500 case PACKET_UNKNOWN:
8501 return -1;
8502 case PACKET_ERROR:
2d717e4f
DJ
8503 if (remote_exec_file[0] == '\0')
8504 error (_("Running the default executable on the remote target failed; "
8505 "try \"set remote exec-file\"?"));
8506 else
8507 error (_("Running \"%s\" on the remote target failed"),
8508 remote_exec_file);
4082afcc
PA
8509 default:
8510 gdb_assert_not_reached (_("bad switch"));
2d717e4f 8511 }
c906108c
SS
8512}
8513
2d717e4f
DJ
8514/* In the extended protocol we want to be able to do things like
8515 "run" and have them basically work as expected. So we need
8516 a special create_inferior function. We support changing the
8517 executable file and the command line arguments, but not the
8518 environment. */
8519
43ff13b4 8520static void
77a19445
TT
8521extended_remote_create_inferior (struct target_ops *ops,
8522 char *exec_file, char *args,
8523 char **env, int from_tty)
43ff13b4 8524{
3405876a
PA
8525 int run_worked;
8526 char *stop_reply;
8527 struct remote_state *rs = get_remote_state ();
8528
43ff13b4 8529 /* If running asynchronously, register the target file descriptor
23860348 8530 with the event loop. */
75c99385 8531 if (target_can_async_p ())
6a3753b3 8532 target_async (1);
43ff13b4 8533
03583c20 8534 /* Disable address space randomization if requested (and supported). */
2bfc0540 8535 if (extended_remote_supports_disable_randomization (ops))
03583c20
UW
8536 extended_remote_disable_randomization (disable_randomization);
8537
43ff13b4 8538 /* Now restart the remote server. */
3405876a
PA
8539 run_worked = extended_remote_run (args) != -1;
8540 if (!run_worked)
2d717e4f
DJ
8541 {
8542 /* vRun was not supported. Fail if we need it to do what the
8543 user requested. */
8544 if (remote_exec_file[0])
8545 error (_("Remote target does not support \"set remote exec-file\""));
8546 if (args[0])
8547 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 8548
2d717e4f
DJ
8549 /* Fall back to "R". */
8550 extended_remote_restart ();
8551 }
424163ea 8552
6c95b8df
PA
8553 if (!have_inferiors ())
8554 {
8555 /* Clean up from the last time we ran, before we mark the target
8556 running again. This will mark breakpoints uninserted, and
8557 get_offsets may insert breakpoints. */
8558 init_thread_list ();
8559 init_wait_for_inferior ();
8560 }
45280a52 8561
3405876a
PA
8562 /* vRun's success return is a stop reply. */
8563 stop_reply = run_worked ? rs->buf : NULL;
8564 add_current_inferior_and_thread (stop_reply);
c0a2216e 8565
2d717e4f
DJ
8566 /* Get updated offsets, if the stub uses qOffsets. */
8567 get_offsets ();
2d717e4f 8568}
c906108c 8569\f
c5aa993b 8570
b775012e
LM
8571/* Given a location's target info BP_TGT and the packet buffer BUF, output
8572 the list of conditions (in agent expression bytecode format), if any, the
8573 target needs to evaluate. The output is placed into the packet buffer
bba74b36 8574 started from BUF and ended at BUF_END. */
b775012e
LM
8575
8576static int
8577remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
8578 struct bp_target_info *bp_tgt, char *buf,
8579 char *buf_end)
b775012e
LM
8580{
8581 struct agent_expr *aexpr = NULL;
8582 int i, ix;
8583 char *pkt;
8584 char *buf_start = buf;
8585
8586 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8587 return 0;
8588
8589 buf += strlen (buf);
bba74b36 8590 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
8591 buf++;
8592
8593 /* Send conditions to the target and free the vector. */
8594 for (ix = 0;
8595 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8596 ix++)
8597 {
bba74b36 8598 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e
LM
8599 buf += strlen (buf);
8600 for (i = 0; i < aexpr->len; ++i)
8601 buf = pack_hex_byte (buf, aexpr->buf[i]);
8602 *buf = '\0';
8603 }
b775012e
LM
8604 return 0;
8605}
8606
d3ce09f5
SS
8607static void
8608remote_add_target_side_commands (struct gdbarch *gdbarch,
8609 struct bp_target_info *bp_tgt, char *buf)
8610{
8611 struct agent_expr *aexpr = NULL;
8612 int i, ix;
8613
8614 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8615 return;
8616
8617 buf += strlen (buf);
8618
8619 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8620 buf += strlen (buf);
8621
8622 /* Concatenate all the agent expressions that are commands into the
8623 cmds parameter. */
8624 for (ix = 0;
8625 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8626 ix++)
8627 {
8628 sprintf (buf, "X%x,", aexpr->len);
8629 buf += strlen (buf);
8630 for (i = 0; i < aexpr->len; ++i)
8631 buf = pack_hex_byte (buf, aexpr->buf[i]);
8632 *buf = '\0';
8633 }
d3ce09f5
SS
8634}
8635
8181d85f
DJ
8636/* Insert a breakpoint. On targets that have software breakpoint
8637 support, we ask the remote target to do the work; on targets
8638 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
8639
8640static int
3db08215
MM
8641remote_insert_breakpoint (struct target_ops *ops,
8642 struct gdbarch *gdbarch,
a6d9a66e 8643 struct bp_target_info *bp_tgt)
c906108c 8644{
d471ea57
AC
8645 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8646 If it succeeds, then set the support to PACKET_ENABLE. If it
8647 fails, and the user has explicitly requested the Z support then
23860348 8648 report an error, otherwise, mark it disabled and go on. */
802188a7 8649
4082afcc 8650 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8651 {
0d5ed153 8652 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8653 struct remote_state *rs;
bba74b36 8654 char *p, *endbuf;
7c0f6dcc 8655 int bpsize;
b775012e 8656 struct condition_list *cond = NULL;
4fff2411 8657
28439a30
PA
8658 /* Make sure the remote is pointing at the right process, if
8659 necessary. */
8660 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8661 set_general_process ();
8662
a1dcb23a 8663 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
4fff2411
JZ
8664
8665 rs = get_remote_state ();
8666 p = rs->buf;
bba74b36 8667 endbuf = rs->buf + get_remote_packet_size ();
802188a7 8668
96baa820
JM
8669 *(p++) = 'Z';
8670 *(p++) = '0';
8671 *(p++) = ',';
7c0f6dcc 8672 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 8673 p += hexnumstr (p, addr);
bba74b36 8674 xsnprintf (p, endbuf - p, ",%d", bpsize);
802188a7 8675
efcc2da7 8676 if (remote_supports_cond_breakpoints (ops))
bba74b36 8677 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8678
78eff0ec 8679 if (remote_can_run_breakpoint_commands (ops))
d3ce09f5
SS
8680 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8681
6d820c5c
DJ
8682 putpkt (rs->buf);
8683 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8684
6d820c5c 8685 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 8686 {
d471ea57
AC
8687 case PACKET_ERROR:
8688 return -1;
8689 case PACKET_OK:
7c0f6dcc
JL
8690 bp_tgt->placed_address = addr;
8691 bp_tgt->placed_size = bpsize;
d471ea57
AC
8692 return 0;
8693 case PACKET_UNKNOWN:
8694 break;
96baa820
JM
8695 }
8696 }
c906108c 8697
0000e5cc
PA
8698 /* If this breakpoint has target-side commands but this stub doesn't
8699 support Z0 packets, throw error. */
8700 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8701 throw_error (NOT_SUPPORTED_ERROR, _("\
8702Target doesn't support breakpoints that have target side commands."));
8703
3db08215 8704 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8705}
8706
8707static int
3db08215
MM
8708remote_remove_breakpoint (struct target_ops *ops,
8709 struct gdbarch *gdbarch,
a6d9a66e 8710 struct bp_target_info *bp_tgt)
c906108c 8711{
8181d85f 8712 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 8713 struct remote_state *rs = get_remote_state ();
96baa820 8714
4082afcc 8715 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8716 {
6d820c5c 8717 char *p = rs->buf;
bba74b36 8718 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 8719
28439a30
PA
8720 /* Make sure the remote is pointing at the right process, if
8721 necessary. */
8722 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8723 set_general_process ();
8724
96baa820
JM
8725 *(p++) = 'z';
8726 *(p++) = '0';
8727 *(p++) = ',';
8728
8181d85f
DJ
8729 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8730 p += hexnumstr (p, addr);
bba74b36 8731 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
802188a7 8732
6d820c5c
DJ
8733 putpkt (rs->buf);
8734 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8735
6d820c5c 8736 return (rs->buf[0] == 'E');
96baa820
JM
8737 }
8738
3db08215 8739 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8740}
8741
f486487f 8742static enum Z_packet_type
d471ea57
AC
8743watchpoint_to_Z_packet (int type)
8744{
8745 switch (type)
8746 {
8747 case hw_write:
bb858e6a 8748 return Z_PACKET_WRITE_WP;
d471ea57
AC
8749 break;
8750 case hw_read:
bb858e6a 8751 return Z_PACKET_READ_WP;
d471ea57
AC
8752 break;
8753 case hw_access:
bb858e6a 8754 return Z_PACKET_ACCESS_WP;
d471ea57
AC
8755 break;
8756 default:
8e65ff28 8757 internal_error (__FILE__, __LINE__,
e2e0b3e5 8758 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
8759 }
8760}
8761
3c3bea1c 8762static int
f486487f
SM
8763remote_insert_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
8764 enum target_hw_bp_type type, struct expression *cond)
96baa820 8765{
d01949b6 8766 struct remote_state *rs = get_remote_state ();
bba74b36 8767 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8768 char *p;
d471ea57 8769 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 8770
4082afcc 8771 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 8772 return 1;
802188a7 8773
28439a30
PA
8774 /* Make sure the remote is pointing at the right process, if
8775 necessary. */
8776 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8777 set_general_process ();
8778
bba74b36 8779 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 8780 p = strchr (rs->buf, '\0');
96baa820
JM
8781 addr = remote_address_masked (addr);
8782 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8783 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 8784
6d820c5c
DJ
8785 putpkt (rs->buf);
8786 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8787
6d820c5c 8788 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8789 {
8790 case PACKET_ERROR:
d471ea57 8791 return -1;
85d721b8
PA
8792 case PACKET_UNKNOWN:
8793 return 1;
d471ea57
AC
8794 case PACKET_OK:
8795 return 0;
8796 }
8e65ff28 8797 internal_error (__FILE__, __LINE__,
e2e0b3e5 8798 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
8799}
8800
283002cf
MR
8801static int
8802remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8803 CORE_ADDR start, int length)
8804{
8805 CORE_ADDR diff = remote_address_masked (addr - start);
8806
8807 return diff < length;
8808}
8809
d471ea57 8810
3c3bea1c 8811static int
f486487f
SM
8812remote_remove_watchpoint (struct target_ops *self, CORE_ADDR addr, int len,
8813 enum target_hw_bp_type type, struct expression *cond)
96baa820 8814{
d01949b6 8815 struct remote_state *rs = get_remote_state ();
bba74b36 8816 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8817 char *p;
d471ea57
AC
8818 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8819
4082afcc 8820 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 8821 return -1;
802188a7 8822
28439a30
PA
8823 /* Make sure the remote is pointing at the right process, if
8824 necessary. */
8825 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8826 set_general_process ();
8827
bba74b36 8828 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 8829 p = strchr (rs->buf, '\0');
96baa820
JM
8830 addr = remote_address_masked (addr);
8831 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8832 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
8833 putpkt (rs->buf);
8834 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8835
6d820c5c 8836 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8837 {
8838 case PACKET_ERROR:
8839 case PACKET_UNKNOWN:
8840 return -1;
8841 case PACKET_OK:
8842 return 0;
8843 }
8e65ff28 8844 internal_error (__FILE__, __LINE__,
e2e0b3e5 8845 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
8846}
8847
3c3bea1c 8848
501eef12 8849int remote_hw_watchpoint_limit = -1;
480a3f21 8850int remote_hw_watchpoint_length_limit = -1;
501eef12 8851int remote_hw_breakpoint_limit = -1;
d471ea57 8852
480a3f21 8853static int
31568a15
TT
8854remote_region_ok_for_hw_watchpoint (struct target_ops *self,
8855 CORE_ADDR addr, int len)
480a3f21
PW
8856{
8857 if (remote_hw_watchpoint_length_limit == 0)
8858 return 0;
8859 else if (remote_hw_watchpoint_length_limit < 0)
8860 return 1;
8861 else if (len <= remote_hw_watchpoint_length_limit)
8862 return 1;
8863 else
8864 return 0;
8865}
8866
b9362cc7 8867static int
5461485a 8868remote_check_watch_resources (struct target_ops *self,
f486487f 8869 enum bptype type, int cnt, int ot)
96baa820 8870{
3c3bea1c
GS
8871 if (type == bp_hardware_breakpoint)
8872 {
8873 if (remote_hw_breakpoint_limit == 0)
8874 return 0;
501eef12
AC
8875 else if (remote_hw_breakpoint_limit < 0)
8876 return 1;
3c3bea1c
GS
8877 else if (cnt <= remote_hw_breakpoint_limit)
8878 return 1;
8879 }
8880 else
8881 {
8882 if (remote_hw_watchpoint_limit == 0)
8883 return 0;
501eef12
AC
8884 else if (remote_hw_watchpoint_limit < 0)
8885 return 1;
3c3bea1c
GS
8886 else if (ot)
8887 return -1;
8888 else if (cnt <= remote_hw_watchpoint_limit)
8889 return 1;
8890 }
8891 return -1;
8892}
8893
f7e6eed5
PA
8894/* The to_stopped_by_sw_breakpoint method of target remote. */
8895
8896static int
8897remote_stopped_by_sw_breakpoint (struct target_ops *ops)
8898{
8899 struct remote_state *rs = get_remote_state ();
8900
8901 return rs->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
8902}
8903
8904/* The to_supports_stopped_by_sw_breakpoint method of target
8905 remote. */
8906
8907static int
8908remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
8909{
8910 struct remote_state *rs = get_remote_state ();
8911
8912 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
8913}
8914
8915/* The to_stopped_by_hw_breakpoint method of target remote. */
8916
8917static int
8918remote_stopped_by_hw_breakpoint (struct target_ops *ops)
8919{
8920 struct remote_state *rs = get_remote_state ();
8921
8922 return rs->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
8923}
8924
8925/* The to_supports_stopped_by_hw_breakpoint method of target
8926 remote. */
8927
8928static int
8929remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
8930{
8931 struct remote_state *rs = get_remote_state ();
8932
8933 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
8934}
8935
b9362cc7 8936static int
6a109b6b 8937remote_stopped_by_watchpoint (struct target_ops *ops)
3c3bea1c 8938{
ee154bee
TT
8939 struct remote_state *rs = get_remote_state ();
8940
f7e6eed5 8941 return rs->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
3c3bea1c
GS
8942}
8943
4aa7a7f5
JJ
8944static int
8945remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 8946{
ee154bee 8947 struct remote_state *rs = get_remote_state ();
4aa7a7f5 8948 int rc = 0;
a744cf53 8949
6a109b6b 8950 if (remote_stopped_by_watchpoint (target))
4aa7a7f5 8951 {
ee154bee 8952 *addr_p = rs->remote_watch_data_address;
4aa7a7f5
JJ
8953 rc = 1;
8954 }
8955
8956 return rc;
3c3bea1c
GS
8957}
8958
8959
8960static int
23a26771 8961remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8962 struct bp_target_info *bp_tgt)
3c3bea1c 8963{
0d5ed153 8964 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8965 struct remote_state *rs;
bba74b36 8966 char *p, *endbuf;
dd61ec5c 8967 char *message;
0d5ed153 8968 int bpsize;
802188a7 8969
c8189ed1 8970 /* The length field should be set to the size of a breakpoint
8181d85f 8971 instruction, even though we aren't inserting one ourselves. */
c8189ed1 8972
0d5ed153 8973 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
3c3bea1c 8974
4082afcc 8975 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 8976 return -1;
2bc416ba 8977
28439a30
PA
8978 /* Make sure the remote is pointing at the right process, if
8979 necessary. */
8980 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8981 set_general_process ();
8982
4fff2411
JZ
8983 rs = get_remote_state ();
8984 p = rs->buf;
bba74b36 8985 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 8986
96baa820
JM
8987 *(p++) = 'Z';
8988 *(p++) = '1';
8989 *(p++) = ',';
802188a7 8990
0d5ed153 8991 addr = remote_address_masked (addr);
96baa820 8992 p += hexnumstr (p, (ULONGEST) addr);
0d5ed153 8993 xsnprintf (p, endbuf - p, ",%x", bpsize);
96baa820 8994
efcc2da7 8995 if (remote_supports_cond_breakpoints (self))
bba74b36 8996 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8997
78eff0ec 8998 if (remote_can_run_breakpoint_commands (self))
d3ce09f5
SS
8999 remote_add_target_side_commands (gdbarch, bp_tgt, p);
9000
6d820c5c
DJ
9001 putpkt (rs->buf);
9002 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 9003
6d820c5c 9004 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
9005 {
9006 case PACKET_ERROR:
dd61ec5c
MW
9007 if (rs->buf[1] == '.')
9008 {
9009 message = strchr (rs->buf + 2, '.');
9010 if (message)
0316657e 9011 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
9012 }
9013 return -1;
d471ea57
AC
9014 case PACKET_UNKNOWN:
9015 return -1;
9016 case PACKET_OK:
0d5ed153
MR
9017 bp_tgt->placed_address = addr;
9018 bp_tgt->placed_size = bpsize;
d471ea57
AC
9019 return 0;
9020 }
8e65ff28 9021 internal_error (__FILE__, __LINE__,
e2e0b3e5 9022 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
9023}
9024
d471ea57 9025
802188a7 9026static int
a64dc96c 9027remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 9028 struct bp_target_info *bp_tgt)
96baa820 9029{
8181d85f 9030 CORE_ADDR addr;
d01949b6 9031 struct remote_state *rs = get_remote_state ();
6d820c5c 9032 char *p = rs->buf;
bba74b36 9033 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 9034
4082afcc 9035 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 9036 return -1;
802188a7 9037
28439a30
PA
9038 /* Make sure the remote is pointing at the right process, if
9039 necessary. */
9040 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9041 set_general_process ();
9042
96baa820
JM
9043 *(p++) = 'z';
9044 *(p++) = '1';
9045 *(p++) = ',';
802188a7 9046
8181d85f 9047 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 9048 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 9049 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
96baa820 9050
6d820c5c
DJ
9051 putpkt (rs->buf);
9052 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 9053
6d820c5c 9054 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
9055 {
9056 case PACKET_ERROR:
9057 case PACKET_UNKNOWN:
9058 return -1;
9059 case PACKET_OK:
9060 return 0;
9061 }
8e65ff28 9062 internal_error (__FILE__, __LINE__,
e2e0b3e5 9063 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 9064}
96baa820 9065
4a5e7a5b
PA
9066/* Verify memory using the "qCRC:" request. */
9067
9068static int
9069remote_verify_memory (struct target_ops *ops,
9070 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
9071{
9072 struct remote_state *rs = get_remote_state ();
9073 unsigned long host_crc, target_crc;
9074 char *tmp;
9075
936d2992
PA
9076 /* It doesn't make sense to use qCRC if the remote target is
9077 connected but not running. */
9078 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
9079 {
9080 enum packet_result result;
28439a30 9081
936d2992
PA
9082 /* Make sure the remote is pointing at the right process. */
9083 set_general_process ();
4a5e7a5b 9084
936d2992
PA
9085 /* FIXME: assumes lma can fit into long. */
9086 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
9087 (long) lma, (long) size);
9088 putpkt (rs->buf);
4a5e7a5b 9089
936d2992
PA
9090 /* Be clever; compute the host_crc before waiting for target
9091 reply. */
9092 host_crc = xcrc32 (data, size, 0xffffffff);
9093
9094 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 9095
936d2992
PA
9096 result = packet_ok (rs->buf,
9097 &remote_protocol_packets[PACKET_qCRC]);
9098 if (result == PACKET_ERROR)
9099 return -1;
9100 else if (result == PACKET_OK)
9101 {
9102 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
9103 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 9104
936d2992
PA
9105 return (host_crc == target_crc);
9106 }
9107 }
4a5e7a5b 9108
936d2992 9109 return simple_verify_memory (ops, data, lma, size);
4a5e7a5b
PA
9110}
9111
c906108c
SS
9112/* compare-sections command
9113
9114 With no arguments, compares each loadable section in the exec bfd
9115 with the same memory range on the target, and reports mismatches.
4a5e7a5b 9116 Useful for verifying the image on the target against the exec file. */
e514a9d6 9117
c906108c 9118static void
fba45db2 9119compare_sections_command (char *args, int from_tty)
c906108c
SS
9120{
9121 asection *s;
c906108c 9122 struct cleanup *old_chain;
948f8e3d 9123 gdb_byte *sectdata;
ce359b09 9124 const char *sectname;
c906108c
SS
9125 bfd_size_type size;
9126 bfd_vma lma;
9127 int matched = 0;
9128 int mismatched = 0;
4a5e7a5b 9129 int res;
95cf3b38 9130 int read_only = 0;
c906108c
SS
9131
9132 if (!exec_bfd)
8a3fe4f8 9133 error (_("command cannot be used without an exec file"));
c906108c 9134
28439a30
PA
9135 /* Make sure the remote is pointing at the right process. */
9136 set_general_process ();
9137
95cf3b38
DT
9138 if (args != NULL && strcmp (args, "-r") == 0)
9139 {
9140 read_only = 1;
9141 args = NULL;
9142 }
9143
c5aa993b 9144 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
9145 {
9146 if (!(s->flags & SEC_LOAD))
0df8b418 9147 continue; /* Skip non-loadable section. */
c906108c 9148
95cf3b38
DT
9149 if (read_only && (s->flags & SEC_READONLY) == 0)
9150 continue; /* Skip writeable sections */
9151
2c500098 9152 size = bfd_get_section_size (s);
c906108c 9153 if (size == 0)
0df8b418 9154 continue; /* Skip zero-length section. */
c906108c 9155
ce359b09 9156 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 9157 if (args && strcmp (args, sectname) != 0)
0df8b418 9158 continue; /* Not the section selected by user. */
c906108c 9159
0df8b418 9160 matched = 1; /* Do this section. */
c906108c 9161 lma = s->lma;
c906108c 9162
c906108c 9163 sectdata = xmalloc (size);
b8c9b27d 9164 old_chain = make_cleanup (xfree, sectdata);
c906108c 9165 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 9166
4a5e7a5b
PA
9167 res = target_verify_memory (sectdata, lma, size);
9168
9169 if (res == -1)
5af949e3 9170 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
9171 paddress (target_gdbarch (), lma),
9172 paddress (target_gdbarch (), lma + size));
c906108c 9173
5af949e3 9174 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
9175 paddress (target_gdbarch (), lma),
9176 paddress (target_gdbarch (), lma + size));
4a5e7a5b 9177 if (res)
c906108c
SS
9178 printf_filtered ("matched.\n");
9179 else
c5aa993b
JM
9180 {
9181 printf_filtered ("MIS-MATCHED!\n");
9182 mismatched++;
9183 }
c906108c
SS
9184
9185 do_cleanups (old_chain);
9186 }
9187 if (mismatched > 0)
936d2992 9188 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 9189the loaded file\n"));
c906108c 9190 if (args && !matched)
a3f17187 9191 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
9192}
9193
0e7f50da
UW
9194/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
9195 into remote target. The number of bytes written to the remote
9196 target is returned, or -1 for error. */
9197
9b409511 9198static enum target_xfer_status
0e7f50da
UW
9199remote_write_qxfer (struct target_ops *ops, const char *object_name,
9200 const char *annex, const gdb_byte *writebuf,
9b409511 9201 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
9202 struct packet_config *packet)
9203{
9204 int i, buf_len;
9205 ULONGEST n;
0e7f50da
UW
9206 struct remote_state *rs = get_remote_state ();
9207 int max_size = get_memory_write_packet_size ();
9208
9209 if (packet->support == PACKET_DISABLE)
2ed4b548 9210 return TARGET_XFER_E_IO;
0e7f50da
UW
9211
9212 /* Insert header. */
9213 i = snprintf (rs->buf, max_size,
9214 "qXfer:%s:write:%s:%s:",
9215 object_name, annex ? annex : "",
9216 phex_nz (offset, sizeof offset));
9217 max_size -= (i + 1);
9218
9219 /* Escape as much data as fits into rs->buf. */
9220 buf_len = remote_escape_output
124e13d9 9221 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
9222
9223 if (putpkt_binary (rs->buf, i + buf_len) < 0
9224 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9225 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9226 return TARGET_XFER_E_IO;
0e7f50da
UW
9227
9228 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
9229
9230 *xfered_len = n;
9231 return TARGET_XFER_OK;
0e7f50da
UW
9232}
9233
0876f84a
DJ
9234/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
9235 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
9236 number of bytes read is returned, or 0 for EOF, or -1 for error.
9237 The number of bytes read may be less than LEN without indicating an
9238 EOF. PACKET is checked and updated to indicate whether the remote
9239 target supports this object. */
9240
9b409511 9241static enum target_xfer_status
0876f84a
DJ
9242remote_read_qxfer (struct target_ops *ops, const char *object_name,
9243 const char *annex,
9244 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 9245 ULONGEST *xfered_len,
0876f84a
DJ
9246 struct packet_config *packet)
9247{
0876f84a 9248 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
9249 LONGEST i, n, packet_len;
9250
9251 if (packet->support == PACKET_DISABLE)
2ed4b548 9252 return TARGET_XFER_E_IO;
0876f84a
DJ
9253
9254 /* Check whether we've cached an end-of-object packet that matches
9255 this request. */
8e88304f 9256 if (rs->finished_object)
0876f84a 9257 {
8e88304f
TT
9258 if (strcmp (object_name, rs->finished_object) == 0
9259 && strcmp (annex ? annex : "", rs->finished_annex) == 0
9260 && offset == rs->finished_offset)
9b409511
YQ
9261 return TARGET_XFER_EOF;
9262
0876f84a
DJ
9263
9264 /* Otherwise, we're now reading something different. Discard
9265 the cache. */
8e88304f
TT
9266 xfree (rs->finished_object);
9267 xfree (rs->finished_annex);
9268 rs->finished_object = NULL;
9269 rs->finished_annex = NULL;
0876f84a
DJ
9270 }
9271
9272 /* Request only enough to fit in a single packet. The actual data
9273 may not, since we don't know how much of it will need to be escaped;
9274 the target is free to respond with slightly less data. We subtract
9275 five to account for the response type and the protocol frame. */
9276 n = min (get_remote_packet_size () - 5, len);
9277 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
9278 object_name, annex ? annex : "",
9279 phex_nz (offset, sizeof offset),
9280 phex_nz (n, sizeof n));
9281 i = putpkt (rs->buf);
9282 if (i < 0)
2ed4b548 9283 return TARGET_XFER_E_IO;
0876f84a
DJ
9284
9285 rs->buf[0] = '\0';
9286 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9287 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9288 return TARGET_XFER_E_IO;
0876f84a
DJ
9289
9290 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
9291 error (_("Unknown remote qXfer reply: %s"), rs->buf);
9292
9293 /* 'm' means there is (or at least might be) more data after this
9294 batch. That does not make sense unless there's at least one byte
9295 of data in this reply. */
9296 if (rs->buf[0] == 'm' && packet_len == 1)
9297 error (_("Remote qXfer reply contained no data."));
9298
9299 /* Got some data. */
bc20a4af
PA
9300 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
9301 packet_len - 1, readbuf, n);
0876f84a
DJ
9302
9303 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
9304 or possibly empty. If we have the final block of a non-empty
9305 object, record this fact to bypass a subsequent partial read. */
9306 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 9307 {
8e88304f
TT
9308 rs->finished_object = xstrdup (object_name);
9309 rs->finished_annex = xstrdup (annex ? annex : "");
9310 rs->finished_offset = offset + i;
0876f84a
DJ
9311 }
9312
9b409511
YQ
9313 if (i == 0)
9314 return TARGET_XFER_EOF;
9315 else
9316 {
9317 *xfered_len = i;
9318 return TARGET_XFER_OK;
9319 }
0876f84a
DJ
9320}
9321
9b409511 9322static enum target_xfer_status
4b8a223f 9323remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5 9324 const char *annex, gdb_byte *readbuf,
9b409511
YQ
9325 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
9326 ULONGEST *xfered_len)
c906108c 9327{
82f73884 9328 struct remote_state *rs;
c906108c 9329 int i;
6d820c5c 9330 char *p2;
1e3ff5ad 9331 char query_type;
124e13d9 9332 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
c906108c 9333
e6e4e701 9334 set_remote_traceframe ();
82f73884
PA
9335 set_general_thread (inferior_ptid);
9336
9337 rs = get_remote_state ();
9338
b2182ed2 9339 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
9340 if (object == TARGET_OBJECT_MEMORY)
9341 {
2d717e4f
DJ
9342 /* If the remote target is connected but not running, we should
9343 pass this request down to a lower stratum (e.g. the executable
9344 file). */
9345 if (!target_has_execution)
9b409511 9346 return TARGET_XFER_EOF;
2d717e4f 9347
21e3b9b9 9348 if (writebuf != NULL)
124e13d9
SM
9349 return remote_write_bytes (offset, writebuf, len, unit_size,
9350 xfered_len);
21e3b9b9 9351 else
124e13d9
SM
9352 return remote_read_bytes (ops, offset, readbuf, len, unit_size,
9353 xfered_len);
21e3b9b9
DJ
9354 }
9355
0df8b418 9356 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
9357 if (object == TARGET_OBJECT_SPU)
9358 {
9359 if (readbuf)
9360 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9b409511
YQ
9361 xfered_len, &remote_protocol_packets
9362 [PACKET_qXfer_spu_read]);
0e7f50da
UW
9363 else
9364 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9b409511
YQ
9365 xfered_len, &remote_protocol_packets
9366 [PACKET_qXfer_spu_write]);
0e7f50da
UW
9367 }
9368
4aa995e1
PA
9369 /* Handle extra signal info using qxfer packets. */
9370 if (object == TARGET_OBJECT_SIGNAL_INFO)
9371 {
9372 if (readbuf)
9373 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9b409511 9374 xfered_len, &remote_protocol_packets
4aa995e1
PA
9375 [PACKET_qXfer_siginfo_read]);
9376 else
3e43a32a 9377 return remote_write_qxfer (ops, "siginfo", annex,
9b409511 9378 writebuf, offset, len, xfered_len,
4aa995e1
PA
9379 &remote_protocol_packets
9380 [PACKET_qXfer_siginfo_write]);
9381 }
9382
0fb4aa4b
PA
9383 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
9384 {
9385 if (readbuf)
3e43a32a 9386 return remote_read_qxfer (ops, "statictrace", annex,
9b409511 9387 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
9388 &remote_protocol_packets
9389 [PACKET_qXfer_statictrace_read]);
9390 else
2ed4b548 9391 return TARGET_XFER_E_IO;
0fb4aa4b
PA
9392 }
9393
a76d924d
DJ
9394 /* Only handle flash writes. */
9395 if (writebuf != NULL)
9396 {
9397 LONGEST xfered;
9398
9399 switch (object)
9400 {
9401 case TARGET_OBJECT_FLASH:
9b409511
YQ
9402 return remote_flash_write (ops, offset, len, xfered_len,
9403 writebuf);
a76d924d
DJ
9404
9405 default:
2ed4b548 9406 return TARGET_XFER_E_IO;
a76d924d
DJ
9407 }
9408 }
4b8a223f 9409
1e3ff5ad
AC
9410 /* Map pre-existing objects onto letters. DO NOT do this for new
9411 objects!!! Instead specify new query packets. */
9412 switch (object)
c906108c 9413 {
1e3ff5ad
AC
9414 case TARGET_OBJECT_AVR:
9415 query_type = 'R';
9416 break;
802188a7
RM
9417
9418 case TARGET_OBJECT_AUXV:
0876f84a
DJ
9419 gdb_assert (annex == NULL);
9420 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9b409511 9421 xfered_len,
0876f84a 9422 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 9423
23181151
DJ
9424 case TARGET_OBJECT_AVAILABLE_FEATURES:
9425 return remote_read_qxfer
9b409511 9426 (ops, "features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
9427 &remote_protocol_packets[PACKET_qXfer_features]);
9428
cfa9d6d9
DJ
9429 case TARGET_OBJECT_LIBRARIES:
9430 return remote_read_qxfer
9b409511 9431 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
9432 &remote_protocol_packets[PACKET_qXfer_libraries]);
9433
2268b414
JK
9434 case TARGET_OBJECT_LIBRARIES_SVR4:
9435 return remote_read_qxfer
9b409511 9436 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
9437 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
9438
fd79ecee
DJ
9439 case TARGET_OBJECT_MEMORY_MAP:
9440 gdb_assert (annex == NULL);
9441 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9b409511 9442 xfered_len,
fd79ecee
DJ
9443 &remote_protocol_packets[PACKET_qXfer_memory_map]);
9444
07e059b5
VP
9445 case TARGET_OBJECT_OSDATA:
9446 /* Should only get here if we're connected. */
5d93a237 9447 gdb_assert (rs->remote_desc);
07e059b5 9448 return remote_read_qxfer
9b409511 9449 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
9450 &remote_protocol_packets[PACKET_qXfer_osdata]);
9451
dc146f7c
VP
9452 case TARGET_OBJECT_THREADS:
9453 gdb_assert (annex == NULL);
9454 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9b409511 9455 xfered_len,
dc146f7c
VP
9456 &remote_protocol_packets[PACKET_qXfer_threads]);
9457
b3b9301e
PA
9458 case TARGET_OBJECT_TRACEFRAME_INFO:
9459 gdb_assert (annex == NULL);
9460 return remote_read_qxfer
9b409511 9461 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 9462 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
9463
9464 case TARGET_OBJECT_FDPIC:
9465 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9b409511 9466 xfered_len,
78d85199 9467 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
9468
9469 case TARGET_OBJECT_OPENVMS_UIB:
9470 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9b409511 9471 xfered_len,
169081d0
TG
9472 &remote_protocol_packets[PACKET_qXfer_uib]);
9473
9accd112
MM
9474 case TARGET_OBJECT_BTRACE:
9475 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9b409511 9476 xfered_len,
9accd112
MM
9477 &remote_protocol_packets[PACKET_qXfer_btrace]);
9478
f4abbc16
MM
9479 case TARGET_OBJECT_BTRACE_CONF:
9480 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
9481 len, xfered_len,
9482 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
9483
c78fa86a
GB
9484 case TARGET_OBJECT_EXEC_FILE:
9485 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
9486 len, xfered_len,
9487 &remote_protocol_packets[PACKET_qXfer_exec_file]);
9488
1e3ff5ad 9489 default:
2ed4b548 9490 return TARGET_XFER_E_IO;
c906108c
SS
9491 }
9492
0df8b418 9493 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 9494 large enough let the caller deal with it. */
ea9c271d 9495 if (len < get_remote_packet_size ())
2ed4b548 9496 return TARGET_XFER_E_IO;
ea9c271d 9497 len = get_remote_packet_size ();
1e3ff5ad 9498
23860348 9499 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 9500 if (!rs->remote_desc)
8a3fe4f8 9501 error (_("remote query is only available after target open"));
c906108c 9502
1e3ff5ad 9503 gdb_assert (annex != NULL);
4b8a223f 9504 gdb_assert (readbuf != NULL);
c906108c 9505
6d820c5c 9506 p2 = rs->buf;
c906108c
SS
9507 *p2++ = 'q';
9508 *p2++ = query_type;
9509
23860348
MS
9510 /* We used one buffer char for the remote protocol q command and
9511 another for the query type. As the remote protocol encapsulation
9512 uses 4 chars plus one extra in case we are debugging
9513 (remote_debug), we have PBUFZIZ - 7 left to pack the query
9514 string. */
c906108c 9515 i = 0;
ea9c271d 9516 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 9517 {
1e3ff5ad
AC
9518 /* Bad caller may have sent forbidden characters. */
9519 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
9520 *p2++ = annex[i];
c906108c
SS
9521 i++;
9522 }
1e3ff5ad
AC
9523 *p2 = '\0';
9524 gdb_assert (annex[i] == '\0');
c906108c 9525
6d820c5c 9526 i = putpkt (rs->buf);
c5aa993b 9527 if (i < 0)
2ed4b548 9528 return TARGET_XFER_E_IO;
c906108c 9529
6d820c5c
DJ
9530 getpkt (&rs->buf, &rs->buf_size, 0);
9531 strcpy ((char *) readbuf, rs->buf);
c906108c 9532
9b409511
YQ
9533 *xfered_len = strlen ((char *) readbuf);
9534 return TARGET_XFER_OK;
c906108c
SS
9535}
9536
08388c79
DE
9537static int
9538remote_search_memory (struct target_ops* ops,
9539 CORE_ADDR start_addr, ULONGEST search_space_len,
9540 const gdb_byte *pattern, ULONGEST pattern_len,
9541 CORE_ADDR *found_addrp)
9542{
f5656ead 9543 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
9544 struct remote_state *rs = get_remote_state ();
9545 int max_size = get_memory_write_packet_size ();
9546 struct packet_config *packet =
9547 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
9548 /* Number of packet bytes used to encode the pattern;
9549 this could be more than PATTERN_LEN due to escape characters. */
08388c79 9550 int escaped_pattern_len;
0df8b418 9551 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
9552 int used_pattern_len;
9553 int i;
9554 int found;
9555 ULONGEST found_addr;
9556
9557 /* Don't go to the target if we don't have to.
9558 This is done before checking packet->support to avoid the possibility that
9559 a success for this edge case means the facility works in general. */
9560 if (pattern_len > search_space_len)
9561 return 0;
9562 if (pattern_len == 0)
9563 {
9564 *found_addrp = start_addr;
9565 return 1;
9566 }
9567
9568 /* If we already know the packet isn't supported, fall back to the simple
9569 way of searching memory. */
9570
4082afcc 9571 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
9572 {
9573 /* Target doesn't provided special support, fall back and use the
9574 standard support (copy memory and do the search here). */
9575 return simple_search_memory (ops, start_addr, search_space_len,
9576 pattern, pattern_len, found_addrp);
9577 }
9578
28439a30
PA
9579 /* Make sure the remote is pointing at the right process. */
9580 set_general_process ();
9581
08388c79
DE
9582 /* Insert header. */
9583 i = snprintf (rs->buf, max_size,
9584 "qSearch:memory:%s;%s;",
5af949e3 9585 phex_nz (start_addr, addr_size),
08388c79
DE
9586 phex_nz (search_space_len, sizeof (search_space_len)));
9587 max_size -= (i + 1);
9588
9589 /* Escape as much data as fits into rs->buf. */
9590 escaped_pattern_len =
124e13d9 9591 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
08388c79
DE
9592 &used_pattern_len, max_size);
9593
9594 /* Bail if the pattern is too large. */
9595 if (used_pattern_len != pattern_len)
9b20d036 9596 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
9597
9598 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
9599 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9600 || packet_ok (rs->buf, packet) != PACKET_OK)
9601 {
9602 /* The request may not have worked because the command is not
9603 supported. If so, fall back to the simple way. */
9604 if (packet->support == PACKET_DISABLE)
9605 {
9606 return simple_search_memory (ops, start_addr, search_space_len,
9607 pattern, pattern_len, found_addrp);
9608 }
9609 return -1;
9610 }
9611
9612 if (rs->buf[0] == '0')
9613 found = 0;
9614 else if (rs->buf[0] == '1')
9615 {
9616 found = 1;
9617 if (rs->buf[1] != ',')
10e0fa18 9618 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9619 unpack_varlen_hex (rs->buf + 2, &found_addr);
9620 *found_addrp = found_addr;
9621 }
9622 else
10e0fa18 9623 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9624
9625 return found;
9626}
9627
96baa820 9628static void
a30bf1f1 9629remote_rcmd (struct target_ops *self, const char *command,
d9fcf2fb 9630 struct ui_file *outbuf)
96baa820 9631{
d01949b6 9632 struct remote_state *rs = get_remote_state ();
2e9f7625 9633 char *p = rs->buf;
96baa820 9634
5d93a237 9635 if (!rs->remote_desc)
8a3fe4f8 9636 error (_("remote rcmd is only available after target open"));
96baa820 9637
23860348 9638 /* Send a NULL command across as an empty command. */
7be570e7
JM
9639 if (command == NULL)
9640 command = "";
9641
23860348 9642 /* The query prefix. */
2e9f7625
DJ
9643 strcpy (rs->buf, "qRcmd,");
9644 p = strchr (rs->buf, '\0');
96baa820 9645
3e43a32a
MS
9646 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9647 > get_remote_packet_size ())
8a3fe4f8 9648 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 9649
23860348 9650 /* Encode the actual command. */
a30bf1f1 9651 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 9652
6d820c5c 9653 if (putpkt (rs->buf) < 0)
8a3fe4f8 9654 error (_("Communication problem with target."));
96baa820
JM
9655
9656 /* get/display the response */
9657 while (1)
9658 {
2e9f7625
DJ
9659 char *buf;
9660
00bf0b85 9661 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 9662 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 9663 rs->buf[0] = '\0';
5b37825d
PW
9664 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9665 {
9666 /* Timeout. Continue to (try to) read responses.
9667 This is better than stopping with an error, assuming the stub
9668 is still executing the (long) monitor command.
9669 If needed, the user can interrupt gdb using C-c, obtaining
9670 an effect similar to stop on timeout. */
9671 continue;
9672 }
2e9f7625 9673 buf = rs->buf;
96baa820 9674 if (buf[0] == '\0')
8a3fe4f8 9675 error (_("Target does not support this command."));
96baa820
JM
9676 if (buf[0] == 'O' && buf[1] != 'K')
9677 {
23860348 9678 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
9679 continue;
9680 }
9681 if (strcmp (buf, "OK") == 0)
9682 break;
7be570e7
JM
9683 if (strlen (buf) == 3 && buf[0] == 'E'
9684 && isdigit (buf[1]) && isdigit (buf[2]))
9685 {
8a3fe4f8 9686 error (_("Protocol error with Rcmd"));
7be570e7 9687 }
96baa820
JM
9688 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9689 {
9690 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 9691
96baa820
JM
9692 fputc_unfiltered (c, outbuf);
9693 }
9694 break;
9695 }
9696}
9697
fd79ecee
DJ
9698static VEC(mem_region_s) *
9699remote_memory_map (struct target_ops *ops)
9700{
9701 VEC(mem_region_s) *result = NULL;
9702 char *text = target_read_stralloc (&current_target,
9703 TARGET_OBJECT_MEMORY_MAP, NULL);
9704
9705 if (text)
9706 {
9707 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 9708
fd79ecee
DJ
9709 result = parse_memory_map (text);
9710 do_cleanups (back_to);
9711 }
9712
9713 return result;
9714}
9715
c906108c 9716static void
fba45db2 9717packet_command (char *args, int from_tty)
c906108c 9718{
d01949b6 9719 struct remote_state *rs = get_remote_state ();
c906108c 9720
5d93a237 9721 if (!rs->remote_desc)
8a3fe4f8 9722 error (_("command can only be used with remote target"));
c906108c 9723
c5aa993b 9724 if (!args)
8a3fe4f8 9725 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
9726
9727 puts_filtered ("sending: ");
9728 print_packet (args);
9729 puts_filtered ("\n");
9730 putpkt (args);
9731
6d820c5c 9732 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 9733 puts_filtered ("received: ");
6d820c5c 9734 print_packet (rs->buf);
c906108c
SS
9735 puts_filtered ("\n");
9736}
9737
9738#if 0
23860348 9739/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 9740
a14ed312 9741static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 9742
a14ed312 9743static void threadset_test_cmd (char *cmd, int tty);
c906108c 9744
a14ed312 9745static void threadalive_test (char *cmd, int tty);
c906108c 9746
a14ed312 9747static void threadlist_test_cmd (char *cmd, int tty);
c906108c 9748
23860348 9749int get_and_display_threadinfo (threadref *ref);
c906108c 9750
a14ed312 9751static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 9752
23860348 9753static int thread_display_step (threadref *ref, void *context);
c906108c 9754
a14ed312 9755static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 9756
a14ed312 9757static void init_remote_threadtests (void);
c906108c 9758
23860348 9759#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
9760
9761static void
fba45db2 9762threadset_test_cmd (char *cmd, int tty)
c906108c
SS
9763{
9764 int sample_thread = SAMPLE_THREAD;
9765
a3f17187 9766 printf_filtered (_("Remote threadset test\n"));
79d7f229 9767 set_general_thread (sample_thread);
c906108c
SS
9768}
9769
9770
9771static void
fba45db2 9772threadalive_test (char *cmd, int tty)
c906108c
SS
9773{
9774 int sample_thread = SAMPLE_THREAD;
79d7f229 9775 int pid = ptid_get_pid (inferior_ptid);
ba348170 9776 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 9777
79d7f229 9778 if (remote_thread_alive (ptid))
c906108c
SS
9779 printf_filtered ("PASS: Thread alive test\n");
9780 else
9781 printf_filtered ("FAIL: Thread alive test\n");
9782}
9783
23860348 9784void output_threadid (char *title, threadref *ref);
c906108c
SS
9785
9786void
fba45db2 9787output_threadid (char *title, threadref *ref)
c906108c
SS
9788{
9789 char hexid[20];
9790
23860348 9791 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
9792 hexid[16] = 0;
9793 printf_filtered ("%s %s\n", title, (&hexid[0]));
9794}
9795
9796static void
fba45db2 9797threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
9798{
9799 int startflag = 1;
9800 threadref nextthread;
9801 int done, result_count;
9802 threadref threadlist[3];
9803
9804 printf_filtered ("Remote Threadlist test\n");
9805 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
9806 &result_count, &threadlist[0]))
9807 printf_filtered ("FAIL: threadlist test\n");
9808 else
9809 {
9810 threadref *scan = threadlist;
9811 threadref *limit = scan + result_count;
9812
9813 while (scan < limit)
9814 output_threadid (" thread ", scan++);
9815 }
9816}
9817
9818void
fba45db2 9819display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
9820{
9821 output_threadid ("Threadid: ", &info->threadid);
9822 printf_filtered ("Name: %s\n ", info->shortname);
9823 printf_filtered ("State: %s\n", info->display);
9824 printf_filtered ("other: %s\n\n", info->more_display);
9825}
9826
9827int
fba45db2 9828get_and_display_threadinfo (threadref *ref)
c906108c
SS
9829{
9830 int result;
9831 int set;
9832 struct gdb_ext_thread_info threadinfo;
9833
9834 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9835 | TAG_MOREDISPLAY | TAG_DISPLAY;
9836 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9837 display_thread_info (&threadinfo);
9838 return result;
9839}
9840
9841static void
fba45db2 9842threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
9843{
9844 int athread = SAMPLE_THREAD;
9845 threadref thread;
9846 int set;
9847
9848 int_to_threadref (&thread, athread);
9849 printf_filtered ("Remote Threadinfo test\n");
9850 if (!get_and_display_threadinfo (&thread))
9851 printf_filtered ("FAIL cannot get thread info\n");
9852}
9853
9854static int
fba45db2 9855thread_display_step (threadref *ref, void *context)
c906108c
SS
9856{
9857 /* output_threadid(" threadstep ",ref); *//* simple test */
9858 return get_and_display_threadinfo (ref);
9859}
9860
9861static void
fba45db2 9862threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
9863{
9864 printf_filtered ("Remote Threadlist update test\n");
9865 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9866}
9867
9868static void
9869init_remote_threadtests (void)
9870{
3e43a32a
MS
9871 add_com ("tlist", class_obscure, threadlist_test_cmd,
9872 _("Fetch and print the remote list of "
9873 "thread identifiers, one pkt only"));
c906108c 9874 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 9875 _("Fetch and display info about one thread"));
c906108c 9876 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 9877 _("Test setting to a different thread"));
c906108c 9878 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 9879 _("Iterate through updating all remote thread info"));
c906108c 9880 add_com ("talive", class_obscure, threadalive_test,
1bedd215 9881 _(" Remote thread alive test "));
c906108c
SS
9882}
9883
9884#endif /* 0 */
9885
f3fb8c85
MS
9886/* Convert a thread ID to a string. Returns the string in a static
9887 buffer. */
9888
9889static char *
117de6a9 9890remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 9891{
79d7f229 9892 static char buf[64];
82f73884 9893 struct remote_state *rs = get_remote_state ();
f3fb8c85 9894
7cee1e54
PA
9895 if (ptid_equal (ptid, null_ptid))
9896 return normal_pid_to_str (ptid);
9897 else if (ptid_is_pid (ptid))
ecd0ada5
PA
9898 {
9899 /* Printing an inferior target id. */
9900
9901 /* When multi-process extensions are off, there's no way in the
9902 remote protocol to know the remote process id, if there's any
9903 at all. There's one exception --- when we're connected with
9904 target extended-remote, and we manually attached to a process
9905 with "attach PID". We don't record anywhere a flag that
9906 allows us to distinguish that case from the case of
9907 connecting with extended-remote and the stub already being
9908 attached to a process, and reporting yes to qAttached, hence
9909 no smart special casing here. */
9910 if (!remote_multi_process_p (rs))
9911 {
9912 xsnprintf (buf, sizeof buf, "Remote target");
9913 return buf;
9914 }
9915
9916 return normal_pid_to_str (ptid);
82f73884 9917 }
ecd0ada5 9918 else
79d7f229 9919 {
ecd0ada5
PA
9920 if (ptid_equal (magic_null_ptid, ptid))
9921 xsnprintf (buf, sizeof buf, "Thread <main>");
901f9912 9922 else if (rs->extended && remote_multi_process_p (rs))
de0d863e
DB
9923 if (ptid_get_lwp (ptid) == 0)
9924 return normal_pid_to_str (ptid);
9925 else
9926 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9927 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
9928 else
9929 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 9930 ptid_get_lwp (ptid));
79d7f229
PA
9931 return buf;
9932 }
f3fb8c85
MS
9933}
9934
38691318
KB
9935/* Get the address of the thread local variable in OBJFILE which is
9936 stored at OFFSET within the thread local storage for thread PTID. */
9937
9938static CORE_ADDR
117de6a9
PA
9939remote_get_thread_local_address (struct target_ops *ops,
9940 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 9941{
4082afcc 9942 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
9943 {
9944 struct remote_state *rs = get_remote_state ();
6d820c5c 9945 char *p = rs->buf;
82f73884 9946 char *endp = rs->buf + get_remote_packet_size ();
571dd617 9947 enum packet_result result;
38691318
KB
9948
9949 strcpy (p, "qGetTLSAddr:");
9950 p += strlen (p);
82f73884 9951 p = write_ptid (p, endp, ptid);
38691318
KB
9952 *p++ = ',';
9953 p += hexnumstr (p, offset);
9954 *p++ = ',';
9955 p += hexnumstr (p, lm);
9956 *p++ = '\0';
9957
6d820c5c
DJ
9958 putpkt (rs->buf);
9959 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
9960 result = packet_ok (rs->buf,
9961 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 9962 if (result == PACKET_OK)
38691318
KB
9963 {
9964 ULONGEST result;
9965
6d820c5c 9966 unpack_varlen_hex (rs->buf, &result);
38691318
KB
9967 return result;
9968 }
571dd617 9969 else if (result == PACKET_UNKNOWN)
109c3e39
AC
9970 throw_error (TLS_GENERIC_ERROR,
9971 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 9972 else
109c3e39
AC
9973 throw_error (TLS_GENERIC_ERROR,
9974 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
9975 }
9976 else
109c3e39
AC
9977 throw_error (TLS_GENERIC_ERROR,
9978 _("TLS not supported or disabled on this target"));
38691318
KB
9979 /* Not reached. */
9980 return 0;
9981}
9982
711e434b
PM
9983/* Provide thread local base, i.e. Thread Information Block address.
9984 Returns 1 if ptid is found and thread_local_base is non zero. */
9985
70221824 9986static int
bd7ae0f5 9987remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
711e434b 9988{
4082afcc 9989 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
9990 {
9991 struct remote_state *rs = get_remote_state ();
9992 char *p = rs->buf;
9993 char *endp = rs->buf + get_remote_packet_size ();
9994 enum packet_result result;
9995
9996 strcpy (p, "qGetTIBAddr:");
9997 p += strlen (p);
9998 p = write_ptid (p, endp, ptid);
9999 *p++ = '\0';
10000
10001 putpkt (rs->buf);
10002 getpkt (&rs->buf, &rs->buf_size, 0);
10003 result = packet_ok (rs->buf,
10004 &remote_protocol_packets[PACKET_qGetTIBAddr]);
10005 if (result == PACKET_OK)
10006 {
10007 ULONGEST result;
10008
10009 unpack_varlen_hex (rs->buf, &result);
10010 if (addr)
10011 *addr = (CORE_ADDR) result;
10012 return 1;
10013 }
10014 else if (result == PACKET_UNKNOWN)
10015 error (_("Remote target doesn't support qGetTIBAddr packet"));
10016 else
10017 error (_("Remote target failed to process qGetTIBAddr request"));
10018 }
10019 else
10020 error (_("qGetTIBAddr not supported or disabled on this target"));
10021 /* Not reached. */
10022 return 0;
10023}
10024
29709017
DJ
10025/* Support for inferring a target description based on the current
10026 architecture and the size of a 'g' packet. While the 'g' packet
10027 can have any size (since optional registers can be left off the
10028 end), some sizes are easily recognizable given knowledge of the
10029 approximate architecture. */
10030
10031struct remote_g_packet_guess
10032{
10033 int bytes;
10034 const struct target_desc *tdesc;
10035};
10036typedef struct remote_g_packet_guess remote_g_packet_guess_s;
10037DEF_VEC_O(remote_g_packet_guess_s);
10038
10039struct remote_g_packet_data
10040{
10041 VEC(remote_g_packet_guess_s) *guesses;
10042};
10043
10044static struct gdbarch_data *remote_g_packet_data_handle;
10045
10046static void *
10047remote_g_packet_data_init (struct obstack *obstack)
10048{
10049 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
10050}
10051
10052void
10053register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
10054 const struct target_desc *tdesc)
10055{
10056 struct remote_g_packet_data *data
10057 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
10058 struct remote_g_packet_guess new_guess, *guess;
10059 int ix;
10060
10061 gdb_assert (tdesc != NULL);
10062
10063 for (ix = 0;
10064 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10065 ix++)
10066 if (guess->bytes == bytes)
10067 internal_error (__FILE__, __LINE__,
9b20d036 10068 _("Duplicate g packet description added for size %d"),
29709017
DJ
10069 bytes);
10070
10071 new_guess.bytes = bytes;
10072 new_guess.tdesc = tdesc;
10073 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
10074}
10075
d962ef82
DJ
10076/* Return 1 if remote_read_description would do anything on this target
10077 and architecture, 0 otherwise. */
10078
10079static int
10080remote_read_description_p (struct target_ops *target)
10081{
10082 struct remote_g_packet_data *data
f5656ead 10083 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
d962ef82
DJ
10084
10085 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10086 return 1;
10087
10088 return 0;
10089}
10090
29709017
DJ
10091static const struct target_desc *
10092remote_read_description (struct target_ops *target)
10093{
10094 struct remote_g_packet_data *data
f5656ead 10095 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
29709017 10096
d962ef82
DJ
10097 /* Do not try this during initial connection, when we do not know
10098 whether there is a running but stopped thread. */
10099 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
2117c711 10100 return target->beneath->to_read_description (target->beneath);
d962ef82 10101
29709017
DJ
10102 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10103 {
10104 struct remote_g_packet_guess *guess;
10105 int ix;
10106 int bytes = send_g_packet ();
10107
10108 for (ix = 0;
10109 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10110 ix++)
10111 if (guess->bytes == bytes)
10112 return guess->tdesc;
10113
10114 /* We discard the g packet. A minor optimization would be to
10115 hold on to it, and fill the register cache once we have selected
10116 an architecture, but it's too tricky to do safely. */
10117 }
10118
2117c711 10119 return target->beneath->to_read_description (target->beneath);
29709017
DJ
10120}
10121
a6b151f1
DJ
10122/* Remote file transfer support. This is host-initiated I/O, not
10123 target-initiated; for target-initiated, see remote-fileio.c. */
10124
10125/* If *LEFT is at least the length of STRING, copy STRING to
10126 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10127 decrease *LEFT. Otherwise raise an error. */
10128
10129static void
10130remote_buffer_add_string (char **buffer, int *left, char *string)
10131{
10132 int len = strlen (string);
10133
10134 if (len > *left)
10135 error (_("Packet too long for target."));
10136
10137 memcpy (*buffer, string, len);
10138 *buffer += len;
10139 *left -= len;
10140
10141 /* NUL-terminate the buffer as a convenience, if there is
10142 room. */
10143 if (*left)
10144 **buffer = '\0';
10145}
10146
10147/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
10148 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10149 decrease *LEFT. Otherwise raise an error. */
10150
10151static void
10152remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
10153 int len)
10154{
10155 if (2 * len > *left)
10156 error (_("Packet too long for target."));
10157
10158 bin2hex (bytes, *buffer, len);
10159 *buffer += 2 * len;
10160 *left -= 2 * len;
10161
10162 /* NUL-terminate the buffer as a convenience, if there is
10163 room. */
10164 if (*left)
10165 **buffer = '\0';
10166}
10167
10168/* If *LEFT is large enough, convert VALUE to hex and add it to
10169 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10170 decrease *LEFT. Otherwise raise an error. */
10171
10172static void
10173remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
10174{
10175 int len = hexnumlen (value);
10176
10177 if (len > *left)
10178 error (_("Packet too long for target."));
10179
10180 hexnumstr (*buffer, value);
10181 *buffer += len;
10182 *left -= len;
10183
10184 /* NUL-terminate the buffer as a convenience, if there is
10185 room. */
10186 if (*left)
10187 **buffer = '\0';
10188}
10189
10190/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
10191 value, *REMOTE_ERRNO to the remote error number or zero if none
10192 was included, and *ATTACHMENT to point to the start of the annex
10193 if any. The length of the packet isn't needed here; there may
10194 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
10195
10196 Return 0 if the packet could be parsed, -1 if it could not. If
10197 -1 is returned, the other variables may not be initialized. */
10198
10199static int
10200remote_hostio_parse_result (char *buffer, int *retcode,
10201 int *remote_errno, char **attachment)
10202{
10203 char *p, *p2;
10204
10205 *remote_errno = 0;
10206 *attachment = NULL;
10207
10208 if (buffer[0] != 'F')
10209 return -1;
10210
10211 errno = 0;
10212 *retcode = strtol (&buffer[1], &p, 16);
10213 if (errno != 0 || p == &buffer[1])
10214 return -1;
10215
10216 /* Check for ",errno". */
10217 if (*p == ',')
10218 {
10219 errno = 0;
10220 *remote_errno = strtol (p + 1, &p2, 16);
10221 if (errno != 0 || p + 1 == p2)
10222 return -1;
10223 p = p2;
10224 }
10225
10226 /* Check for ";attachment". If there is no attachment, the
10227 packet should end here. */
10228 if (*p == ';')
10229 {
10230 *attachment = p + 1;
10231 return 0;
10232 }
10233 else if (*p == '\0')
10234 return 0;
10235 else
10236 return -1;
10237}
10238
10239/* Send a prepared I/O packet to the target and read its response.
10240 The prepared packet is in the global RS->BUF before this function
10241 is called, and the answer is there when we return.
10242
10243 COMMAND_BYTES is the length of the request to send, which may include
10244 binary data. WHICH_PACKET is the packet configuration to check
10245 before attempting a packet. If an error occurs, *REMOTE_ERRNO
10246 is set to the error number and -1 is returned. Otherwise the value
10247 returned by the function is returned.
10248
10249 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
10250 attachment is expected; an error will be reported if there's a
10251 mismatch. If one is found, *ATTACHMENT will be set to point into
10252 the packet buffer and *ATTACHMENT_LEN will be set to the
10253 attachment's length. */
10254
10255static int
10256remote_hostio_send_command (int command_bytes, int which_packet,
10257 int *remote_errno, char **attachment,
10258 int *attachment_len)
10259{
10260 struct remote_state *rs = get_remote_state ();
10261 int ret, bytes_read;
10262 char *attachment_tmp;
10263
5d93a237 10264 if (!rs->remote_desc
4082afcc 10265 || packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
10266 {
10267 *remote_errno = FILEIO_ENOSYS;
10268 return -1;
10269 }
10270
10271 putpkt_binary (rs->buf, command_bytes);
10272 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10273
10274 /* If it timed out, something is wrong. Don't try to parse the
10275 buffer. */
10276 if (bytes_read < 0)
10277 {
10278 *remote_errno = FILEIO_EINVAL;
10279 return -1;
10280 }
10281
10282 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
10283 {
10284 case PACKET_ERROR:
10285 *remote_errno = FILEIO_EINVAL;
10286 return -1;
10287 case PACKET_UNKNOWN:
10288 *remote_errno = FILEIO_ENOSYS;
10289 return -1;
10290 case PACKET_OK:
10291 break;
10292 }
10293
10294 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
10295 &attachment_tmp))
10296 {
10297 *remote_errno = FILEIO_EINVAL;
10298 return -1;
10299 }
10300
10301 /* Make sure we saw an attachment if and only if we expected one. */
10302 if ((attachment_tmp == NULL && attachment != NULL)
10303 || (attachment_tmp != NULL && attachment == NULL))
10304 {
10305 *remote_errno = FILEIO_EINVAL;
10306 return -1;
10307 }
10308
10309 /* If an attachment was found, it must point into the packet buffer;
10310 work out how many bytes there were. */
10311 if (attachment_tmp != NULL)
10312 {
10313 *attachment = attachment_tmp;
10314 *attachment_len = bytes_read - (*attachment - rs->buf);
10315 }
10316
10317 return ret;
10318}
10319
15a201c8
GB
10320/* Set the filesystem remote_hostio functions that take FILENAME
10321 arguments will use. Return 0 on success, or -1 if an error
10322 occurs (and set *REMOTE_ERRNO). */
10323
10324static int
10325remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
10326{
10327 struct remote_state *rs = get_remote_state ();
10328 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
10329 char *p = rs->buf;
10330 int left = get_remote_packet_size () - 1;
10331 char arg[9];
10332 int ret;
10333
10334 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10335 return 0;
10336
10337 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
10338 return 0;
10339
10340 remote_buffer_add_string (&p, &left, "vFile:setfs:");
10341
10342 xsnprintf (arg, sizeof (arg), "%x", required_pid);
10343 remote_buffer_add_string (&p, &left, arg);
10344
10345 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
10346 remote_errno, NULL, NULL);
10347
10348 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10349 return 0;
10350
10351 if (ret == 0)
10352 rs->fs_pid = required_pid;
10353
10354 return ret;
10355}
10356
12e2a5fd 10357/* Implementation of to_fileio_open. */
a6b151f1
DJ
10358
10359static int
cd897586 10360remote_hostio_open (struct target_ops *self,
07c138c8
GB
10361 struct inferior *inf, const char *filename,
10362 int flags, int mode, int *remote_errno)
a6b151f1
DJ
10363{
10364 struct remote_state *rs = get_remote_state ();
10365 char *p = rs->buf;
10366 int left = get_remote_packet_size () - 1;
10367
15a201c8
GB
10368 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10369 return -1;
10370
a6b151f1
DJ
10371 remote_buffer_add_string (&p, &left, "vFile:open:");
10372
10373 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10374 strlen (filename));
10375 remote_buffer_add_string (&p, &left, ",");
10376
10377 remote_buffer_add_int (&p, &left, flags);
10378 remote_buffer_add_string (&p, &left, ",");
10379
10380 remote_buffer_add_int (&p, &left, mode);
10381
10382 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
10383 remote_errno, NULL, NULL);
10384}
10385
12e2a5fd 10386/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
10387
10388static int
0d866f62
TT
10389remote_hostio_pwrite (struct target_ops *self,
10390 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
10391 ULONGEST offset, int *remote_errno)
10392{
10393 struct remote_state *rs = get_remote_state ();
10394 char *p = rs->buf;
10395 int left = get_remote_packet_size ();
10396 int out_len;
10397
10398 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
10399
10400 remote_buffer_add_int (&p, &left, fd);
10401 remote_buffer_add_string (&p, &left, ",");
10402
10403 remote_buffer_add_int (&p, &left, offset);
10404 remote_buffer_add_string (&p, &left, ",");
10405
124e13d9 10406 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
a6b151f1
DJ
10407 get_remote_packet_size () - (p - rs->buf));
10408
10409 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
10410 remote_errno, NULL, NULL);
10411}
10412
12e2a5fd 10413/* Implementation of to_fileio_pread. */
a6b151f1
DJ
10414
10415static int
a3be983c
TT
10416remote_hostio_pread (struct target_ops *self,
10417 int fd, gdb_byte *read_buf, int len,
a6b151f1
DJ
10418 ULONGEST offset, int *remote_errno)
10419{
10420 struct remote_state *rs = get_remote_state ();
10421 char *p = rs->buf;
10422 char *attachment;
10423 int left = get_remote_packet_size ();
10424 int ret, attachment_len;
10425 int read_len;
10426
10427 remote_buffer_add_string (&p, &left, "vFile:pread:");
10428
10429 remote_buffer_add_int (&p, &left, fd);
10430 remote_buffer_add_string (&p, &left, ",");
10431
10432 remote_buffer_add_int (&p, &left, len);
10433 remote_buffer_add_string (&p, &left, ",");
10434
10435 remote_buffer_add_int (&p, &left, offset);
10436
10437 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
10438 remote_errno, &attachment,
10439 &attachment_len);
10440
10441 if (ret < 0)
10442 return ret;
10443
bc20a4af 10444 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
10445 read_buf, len);
10446 if (read_len != ret)
10447 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
10448
10449 return ret;
10450}
10451
12e2a5fd 10452/* Implementation of to_fileio_close. */
a6b151f1
DJ
10453
10454static int
df39ea25 10455remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
10456{
10457 struct remote_state *rs = get_remote_state ();
10458 char *p = rs->buf;
10459 int left = get_remote_packet_size () - 1;
10460
10461 remote_buffer_add_string (&p, &left, "vFile:close:");
10462
10463 remote_buffer_add_int (&p, &left, fd);
10464
10465 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
10466 remote_errno, NULL, NULL);
10467}
10468
12e2a5fd 10469/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
10470
10471static int
dbbca37d 10472remote_hostio_unlink (struct target_ops *self,
07c138c8
GB
10473 struct inferior *inf, const char *filename,
10474 int *remote_errno)
a6b151f1
DJ
10475{
10476 struct remote_state *rs = get_remote_state ();
10477 char *p = rs->buf;
10478 int left = get_remote_packet_size () - 1;
10479
15a201c8
GB
10480 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10481 return -1;
10482
a6b151f1
DJ
10483 remote_buffer_add_string (&p, &left, "vFile:unlink:");
10484
10485 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10486 strlen (filename));
10487
10488 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
10489 remote_errno, NULL, NULL);
10490}
10491
12e2a5fd 10492/* Implementation of to_fileio_readlink. */
b9e7b9c3
UW
10493
10494static char *
fab5aa7c 10495remote_hostio_readlink (struct target_ops *self,
07c138c8
GB
10496 struct inferior *inf, const char *filename,
10497 int *remote_errno)
b9e7b9c3
UW
10498{
10499 struct remote_state *rs = get_remote_state ();
10500 char *p = rs->buf;
10501 char *attachment;
10502 int left = get_remote_packet_size ();
10503 int len, attachment_len;
10504 int read_len;
10505 char *ret;
10506
15a201c8
GB
10507 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10508 return NULL;
10509
b9e7b9c3
UW
10510 remote_buffer_add_string (&p, &left, "vFile:readlink:");
10511
10512 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10513 strlen (filename));
10514
10515 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
10516 remote_errno, &attachment,
10517 &attachment_len);
10518
10519 if (len < 0)
10520 return NULL;
10521
10522 ret = xmalloc (len + 1);
10523
bc20a4af
PA
10524 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10525 (gdb_byte *) ret, len);
b9e7b9c3
UW
10526 if (read_len != len)
10527 error (_("Readlink returned %d, but %d bytes."), len, read_len);
10528
10529 ret[len] = '\0';
10530 return ret;
10531}
10532
12e2a5fd 10533/* Implementation of to_fileio_fstat. */
0a93529c
GB
10534
10535static int
10536remote_hostio_fstat (struct target_ops *self,
10537 int fd, struct stat *st,
10538 int *remote_errno)
10539{
10540 struct remote_state *rs = get_remote_state ();
10541 char *p = rs->buf;
10542 int left = get_remote_packet_size ();
10543 int attachment_len, ret;
10544 char *attachment;
10545 struct fio_stat fst;
10546 int read_len;
10547
464b0089
GB
10548 remote_buffer_add_string (&p, &left, "vFile:fstat:");
10549
10550 remote_buffer_add_int (&p, &left, fd);
10551
10552 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
10553 remote_errno, &attachment,
10554 &attachment_len);
10555 if (ret < 0)
0a93529c 10556 {
464b0089
GB
10557 if (*remote_errno != FILEIO_ENOSYS)
10558 return ret;
10559
0a93529c
GB
10560 /* Strictly we should return -1, ENOSYS here, but when
10561 "set sysroot remote:" was implemented in August 2008
10562 BFD's need for a stat function was sidestepped with
10563 this hack. This was not remedied until March 2015
10564 so we retain the previous behavior to avoid breaking
10565 compatibility.
10566
10567 Note that the memset is a March 2015 addition; older
10568 GDBs set st_size *and nothing else* so the structure
10569 would have garbage in all other fields. This might
10570 break something but retaining the previous behavior
10571 here would be just too wrong. */
10572
10573 memset (st, 0, sizeof (struct stat));
10574 st->st_size = INT_MAX;
10575 return 0;
10576 }
10577
0a93529c
GB
10578 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10579 (gdb_byte *) &fst, sizeof (fst));
10580
10581 if (read_len != ret)
10582 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
10583
10584 if (read_len != sizeof (fst))
10585 error (_("vFile:fstat returned %d bytes, but expecting %d."),
10586 read_len, (int) sizeof (fst));
10587
10588 remote_fileio_to_host_stat (&fst, st);
10589
10590 return 0;
10591}
10592
12e2a5fd 10593/* Implementation of to_filesystem_is_local. */
e3dd7556
GB
10594
10595static int
10596remote_filesystem_is_local (struct target_ops *self)
10597{
10598 /* Valgrind GDB presents itself as a remote target but works
10599 on the local filesystem: it does not implement remote get
10600 and users are not expected to set a sysroot. To handle
10601 this case we treat the remote filesystem as local if the
10602 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
10603 does not support vFile:open. */
a3be80c3 10604 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
e3dd7556
GB
10605 {
10606 enum packet_support ps = packet_support (PACKET_vFile_open);
10607
10608 if (ps == PACKET_SUPPORT_UNKNOWN)
10609 {
10610 int fd, remote_errno;
10611
10612 /* Try opening a file to probe support. The supplied
10613 filename is irrelevant, we only care about whether
10614 the stub recognizes the packet or not. */
07c138c8 10615 fd = remote_hostio_open (self, NULL, "just probing",
e3dd7556
GB
10616 FILEIO_O_RDONLY, 0700,
10617 &remote_errno);
10618
10619 if (fd >= 0)
10620 remote_hostio_close (self, fd, &remote_errno);
10621
10622 ps = packet_support (PACKET_vFile_open);
10623 }
10624
10625 if (ps == PACKET_DISABLE)
10626 {
10627 static int warning_issued = 0;
10628
10629 if (!warning_issued)
10630 {
10631 warning (_("remote target does not support file"
10632 " transfer, attempting to access files"
10633 " from local filesystem."));
10634 warning_issued = 1;
10635 }
10636
10637 return 1;
10638 }
10639 }
10640
10641 return 0;
10642}
10643
a6b151f1
DJ
10644static int
10645remote_fileio_errno_to_host (int errnum)
10646{
10647 switch (errnum)
10648 {
10649 case FILEIO_EPERM:
10650 return EPERM;
10651 case FILEIO_ENOENT:
10652 return ENOENT;
10653 case FILEIO_EINTR:
10654 return EINTR;
10655 case FILEIO_EIO:
10656 return EIO;
10657 case FILEIO_EBADF:
10658 return EBADF;
10659 case FILEIO_EACCES:
10660 return EACCES;
10661 case FILEIO_EFAULT:
10662 return EFAULT;
10663 case FILEIO_EBUSY:
10664 return EBUSY;
10665 case FILEIO_EEXIST:
10666 return EEXIST;
10667 case FILEIO_ENODEV:
10668 return ENODEV;
10669 case FILEIO_ENOTDIR:
10670 return ENOTDIR;
10671 case FILEIO_EISDIR:
10672 return EISDIR;
10673 case FILEIO_EINVAL:
10674 return EINVAL;
10675 case FILEIO_ENFILE:
10676 return ENFILE;
10677 case FILEIO_EMFILE:
10678 return EMFILE;
10679 case FILEIO_EFBIG:
10680 return EFBIG;
10681 case FILEIO_ENOSPC:
10682 return ENOSPC;
10683 case FILEIO_ESPIPE:
10684 return ESPIPE;
10685 case FILEIO_EROFS:
10686 return EROFS;
10687 case FILEIO_ENOSYS:
10688 return ENOSYS;
10689 case FILEIO_ENAMETOOLONG:
10690 return ENAMETOOLONG;
10691 }
10692 return -1;
10693}
10694
10695static char *
10696remote_hostio_error (int errnum)
10697{
10698 int host_error = remote_fileio_errno_to_host (errnum);
10699
10700 if (host_error == -1)
10701 error (_("Unknown remote I/O error %d"), errnum);
10702 else
10703 error (_("Remote I/O error: %s"), safe_strerror (host_error));
10704}
10705
a6b151f1
DJ
10706static void
10707remote_hostio_close_cleanup (void *opaque)
10708{
10709 int fd = *(int *) opaque;
10710 int remote_errno;
10711
df39ea25 10712 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
a6b151f1
DJ
10713}
10714
10715void
10716remote_file_put (const char *local_file, const char *remote_file, int from_tty)
10717{
10718 struct cleanup *back_to, *close_cleanup;
10719 int retcode, fd, remote_errno, bytes, io_size;
10720 FILE *file;
10721 gdb_byte *buffer;
10722 int bytes_in_buffer;
10723 int saw_eof;
10724 ULONGEST offset;
5d93a237 10725 struct remote_state *rs = get_remote_state ();
a6b151f1 10726
5d93a237 10727 if (!rs->remote_desc)
a6b151f1
DJ
10728 error (_("command can only be used with remote target"));
10729
614c279d 10730 file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
10731 if (file == NULL)
10732 perror_with_name (local_file);
7c8a8b04 10733 back_to = make_cleanup_fclose (file);
a6b151f1 10734
07c138c8 10735 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 10736 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
a6b151f1
DJ
10737 | FILEIO_O_TRUNC),
10738 0700, &remote_errno);
10739 if (fd == -1)
10740 remote_hostio_error (remote_errno);
10741
10742 /* Send up to this many bytes at once. They won't all fit in the
10743 remote packet limit, so we'll transfer slightly fewer. */
10744 io_size = get_remote_packet_size ();
10745 buffer = xmalloc (io_size);
10746 make_cleanup (xfree, buffer);
10747
10748 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10749
10750 bytes_in_buffer = 0;
10751 saw_eof = 0;
10752 offset = 0;
10753 while (bytes_in_buffer || !saw_eof)
10754 {
10755 if (!saw_eof)
10756 {
3e43a32a
MS
10757 bytes = fread (buffer + bytes_in_buffer, 1,
10758 io_size - bytes_in_buffer,
a6b151f1
DJ
10759 file);
10760 if (bytes == 0)
10761 {
10762 if (ferror (file))
10763 error (_("Error reading %s."), local_file);
10764 else
10765 {
10766 /* EOF. Unless there is something still in the
10767 buffer from the last iteration, we are done. */
10768 saw_eof = 1;
10769 if (bytes_in_buffer == 0)
10770 break;
10771 }
10772 }
10773 }
10774 else
10775 bytes = 0;
10776
10777 bytes += bytes_in_buffer;
10778 bytes_in_buffer = 0;
10779
0d866f62
TT
10780 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
10781 fd, buffer, bytes,
3e43a32a 10782 offset, &remote_errno);
a6b151f1
DJ
10783
10784 if (retcode < 0)
10785 remote_hostio_error (remote_errno);
10786 else if (retcode == 0)
10787 error (_("Remote write of %d bytes returned 0!"), bytes);
10788 else if (retcode < bytes)
10789 {
10790 /* Short write. Save the rest of the read data for the next
10791 write. */
10792 bytes_in_buffer = bytes - retcode;
10793 memmove (buffer, buffer + retcode, bytes_in_buffer);
10794 }
10795
10796 offset += retcode;
10797 }
10798
10799 discard_cleanups (close_cleanup);
df39ea25 10800 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10801 remote_hostio_error (remote_errno);
10802
10803 if (from_tty)
10804 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
10805 do_cleanups (back_to);
10806}
10807
10808void
10809remote_file_get (const char *remote_file, const char *local_file, int from_tty)
10810{
10811 struct cleanup *back_to, *close_cleanup;
cea39f65 10812 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
10813 FILE *file;
10814 gdb_byte *buffer;
10815 ULONGEST offset;
5d93a237 10816 struct remote_state *rs = get_remote_state ();
a6b151f1 10817
5d93a237 10818 if (!rs->remote_desc)
a6b151f1
DJ
10819 error (_("command can only be used with remote target"));
10820
07c138c8 10821 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 10822 remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
a6b151f1
DJ
10823 if (fd == -1)
10824 remote_hostio_error (remote_errno);
10825
614c279d 10826 file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
10827 if (file == NULL)
10828 perror_with_name (local_file);
7c8a8b04 10829 back_to = make_cleanup_fclose (file);
a6b151f1
DJ
10830
10831 /* Send up to this many bytes at once. They won't all fit in the
10832 remote packet limit, so we'll transfer slightly fewer. */
10833 io_size = get_remote_packet_size ();
10834 buffer = xmalloc (io_size);
10835 make_cleanup (xfree, buffer);
10836
10837 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10838
10839 offset = 0;
10840 while (1)
10841 {
a3be983c
TT
10842 bytes = remote_hostio_pread (find_target_at (process_stratum),
10843 fd, buffer, io_size, offset, &remote_errno);
a6b151f1
DJ
10844 if (bytes == 0)
10845 /* Success, but no bytes, means end-of-file. */
10846 break;
10847 if (bytes == -1)
10848 remote_hostio_error (remote_errno);
10849
10850 offset += bytes;
10851
10852 bytes = fwrite (buffer, 1, bytes, file);
10853 if (bytes == 0)
10854 perror_with_name (local_file);
10855 }
10856
10857 discard_cleanups (close_cleanup);
df39ea25 10858 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10859 remote_hostio_error (remote_errno);
10860
10861 if (from_tty)
10862 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
10863 do_cleanups (back_to);
10864}
10865
10866void
10867remote_file_delete (const char *remote_file, int from_tty)
10868{
10869 int retcode, remote_errno;
5d93a237 10870 struct remote_state *rs = get_remote_state ();
a6b151f1 10871
5d93a237 10872 if (!rs->remote_desc)
a6b151f1
DJ
10873 error (_("command can only be used with remote target"));
10874
dbbca37d 10875 retcode = remote_hostio_unlink (find_target_at (process_stratum),
07c138c8 10876 NULL, remote_file, &remote_errno);
a6b151f1
DJ
10877 if (retcode == -1)
10878 remote_hostio_error (remote_errno);
10879
10880 if (from_tty)
10881 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
10882}
10883
10884static void
10885remote_put_command (char *args, int from_tty)
10886{
10887 struct cleanup *back_to;
10888 char **argv;
10889
d1a41061
PP
10890 if (args == NULL)
10891 error_no_arg (_("file to put"));
10892
10893 argv = gdb_buildargv (args);
a6b151f1
DJ
10894 back_to = make_cleanup_freeargv (argv);
10895 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10896 error (_("Invalid parameters to remote put"));
10897
10898 remote_file_put (argv[0], argv[1], from_tty);
10899
10900 do_cleanups (back_to);
10901}
10902
10903static void
10904remote_get_command (char *args, int from_tty)
10905{
10906 struct cleanup *back_to;
10907 char **argv;
10908
d1a41061
PP
10909 if (args == NULL)
10910 error_no_arg (_("file to get"));
10911
10912 argv = gdb_buildargv (args);
a6b151f1
DJ
10913 back_to = make_cleanup_freeargv (argv);
10914 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10915 error (_("Invalid parameters to remote get"));
10916
10917 remote_file_get (argv[0], argv[1], from_tty);
10918
10919 do_cleanups (back_to);
10920}
10921
10922static void
10923remote_delete_command (char *args, int from_tty)
10924{
10925 struct cleanup *back_to;
10926 char **argv;
10927
d1a41061
PP
10928 if (args == NULL)
10929 error_no_arg (_("file to delete"));
10930
10931 argv = gdb_buildargv (args);
a6b151f1
DJ
10932 back_to = make_cleanup_freeargv (argv);
10933 if (argv[0] == NULL || argv[1] != NULL)
10934 error (_("Invalid parameters to remote delete"));
10935
10936 remote_file_delete (argv[0], from_tty);
10937
10938 do_cleanups (back_to);
10939}
10940
10941static void
10942remote_command (char *args, int from_tty)
10943{
635c7e8a 10944 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
10945}
10946
b2175913 10947static int
19db3e69 10948remote_can_execute_reverse (struct target_ops *self)
b2175913 10949{
4082afcc
PA
10950 if (packet_support (PACKET_bs) == PACKET_ENABLE
10951 || packet_support (PACKET_bc) == PACKET_ENABLE)
40ab02ce
MS
10952 return 1;
10953 else
10954 return 0;
b2175913
MS
10955}
10956
74531fed 10957static int
2a9a2795 10958remote_supports_non_stop (struct target_ops *self)
74531fed
PA
10959{
10960 return 1;
10961}
10962
03583c20 10963static int
2bfc0540 10964remote_supports_disable_randomization (struct target_ops *self)
03583c20
UW
10965{
10966 /* Only supported in extended mode. */
10967 return 0;
10968}
10969
8a305172 10970static int
86ce2668 10971remote_supports_multi_process (struct target_ops *self)
8a305172
PA
10972{
10973 struct remote_state *rs = get_remote_state ();
a744cf53 10974
901f9912
UW
10975 /* Only extended-remote handles being attached to multiple
10976 processes, even though plain remote can use the multi-process
10977 thread id extensions, so that GDB knows the target process's
10978 PID. */
10979 return rs->extended && remote_multi_process_p (rs);
8a305172
PA
10980}
10981
70221824 10982static int
782b2b07
SS
10983remote_supports_cond_tracepoints (void)
10984{
4082afcc 10985 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
10986}
10987
3788aec7 10988static int
efcc2da7 10989remote_supports_cond_breakpoints (struct target_ops *self)
3788aec7 10990{
4082afcc 10991 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
10992}
10993
70221824 10994static int
7a697b8d
SS
10995remote_supports_fast_tracepoints (void)
10996{
4082afcc 10997 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
10998}
10999
0fb4aa4b
PA
11000static int
11001remote_supports_static_tracepoints (void)
11002{
4082afcc 11003 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
11004}
11005
1e4d1764
YQ
11006static int
11007remote_supports_install_in_trace (void)
11008{
4082afcc 11009 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
11010}
11011
d248b706 11012static int
7d178d6a 11013remote_supports_enable_disable_tracepoint (struct target_ops *self)
d248b706 11014{
4082afcc
PA
11015 return (packet_support (PACKET_EnableDisableTracepoints_feature)
11016 == PACKET_ENABLE);
d248b706
KY
11017}
11018
3065dfb6 11019static int
6de37a3a 11020remote_supports_string_tracing (struct target_ops *self)
3065dfb6 11021{
4082afcc 11022 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
11023}
11024
d3ce09f5 11025static int
78eff0ec 11026remote_can_run_breakpoint_commands (struct target_ops *self)
d3ce09f5 11027{
4082afcc 11028 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
11029}
11030
35b1e5cc 11031static void
ecae04e1 11032remote_trace_init (struct target_ops *self)
35b1e5cc
SS
11033{
11034 putpkt ("QTinit");
11035 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99 11036 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11037 error (_("Target does not support this command."));
11038}
11039
11040static void free_actions_list (char **actions_list);
11041static void free_actions_list_cleanup_wrapper (void *);
11042static void
11043free_actions_list_cleanup_wrapper (void *al)
11044{
11045 free_actions_list (al);
11046}
11047
11048static void
11049free_actions_list (char **actions_list)
11050{
11051 int ndx;
11052
11053 if (actions_list == 0)
11054 return;
11055
11056 for (ndx = 0; actions_list[ndx]; ndx++)
11057 xfree (actions_list[ndx]);
11058
11059 xfree (actions_list);
11060}
11061
409873ef
SS
11062/* Recursive routine to walk through command list including loops, and
11063 download packets for each command. */
11064
11065static void
11066remote_download_command_source (int num, ULONGEST addr,
11067 struct command_line *cmds)
11068{
11069 struct remote_state *rs = get_remote_state ();
11070 struct command_line *cmd;
11071
11072 for (cmd = cmds; cmd; cmd = cmd->next)
11073 {
0df8b418 11074 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11075 strcpy (rs->buf, "QTDPsrc:");
11076 encode_source_string (num, addr, "cmd", cmd->line,
11077 rs->buf + strlen (rs->buf),
11078 rs->buf_size - strlen (rs->buf));
11079 putpkt (rs->buf);
11080 remote_get_noisy_reply (&target_buf, &target_buf_size);
11081 if (strcmp (target_buf, "OK"))
11082 warning (_("Target does not support source download."));
11083
11084 if (cmd->control_type == while_control
11085 || cmd->control_type == while_stepping_control)
11086 {
11087 remote_download_command_source (num, addr, *cmd->body_list);
11088
0df8b418 11089 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11090 strcpy (rs->buf, "QTDPsrc:");
11091 encode_source_string (num, addr, "cmd", "end",
11092 rs->buf + strlen (rs->buf),
11093 rs->buf_size - strlen (rs->buf));
11094 putpkt (rs->buf);
11095 remote_get_noisy_reply (&target_buf, &target_buf_size);
11096 if (strcmp (target_buf, "OK"))
11097 warning (_("Target does not support source download."));
11098 }
11099 }
11100}
11101
35b1e5cc 11102static void
548f7808 11103remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
35b1e5cc 11104{
bba74b36 11105#define BUF_SIZE 2048
e8ba3115 11106
35b1e5cc 11107 CORE_ADDR tpaddr;
409873ef 11108 char addrbuf[40];
bba74b36 11109 char buf[BUF_SIZE];
35b1e5cc
SS
11110 char **tdp_actions;
11111 char **stepping_actions;
11112 int ndx;
11113 struct cleanup *old_chain = NULL;
11114 struct agent_expr *aexpr;
11115 struct cleanup *aexpr_chain = NULL;
11116 char *pkt;
e8ba3115 11117 struct breakpoint *b = loc->owner;
d9b3f62e 11118 struct tracepoint *t = (struct tracepoint *) b;
35b1e5cc 11119
dc673c81 11120 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
11121 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
11122 tdp_actions);
11123 (void) make_cleanup (free_actions_list_cleanup_wrapper,
11124 stepping_actions);
11125
11126 tpaddr = loc->address;
11127 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
11128 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
11129 addrbuf, /* address */
11130 (b->enable_state == bp_enabled ? 'E' : 'D'),
11131 t->step_count, t->pass_count);
e8ba3115
YQ
11132 /* Fast tracepoints are mostly handled by the target, but we can
11133 tell the target how big of an instruction block should be moved
11134 around. */
11135 if (b->type == bp_fast_tracepoint)
11136 {
11137 /* Only test for support at download time; we may not know
11138 target capabilities at definition time. */
11139 if (remote_supports_fast_tracepoints ())
35b1e5cc 11140 {
6b940e6a
PL
11141 if (gdbarch_fast_tracepoint_valid_at (loc->gdbarch, tpaddr,
11142 NULL))
bba74b36 11143 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
6b940e6a 11144 gdb_insn_length (loc->gdbarch, tpaddr));
35b1e5cc 11145 else
e8ba3115
YQ
11146 /* If it passed validation at definition but fails now,
11147 something is very wrong. */
11148 internal_error (__FILE__, __LINE__,
11149 _("Fast tracepoint not "
11150 "valid during download"));
35b1e5cc 11151 }
e8ba3115
YQ
11152 else
11153 /* Fast tracepoints are functionally identical to regular
11154 tracepoints, so don't take lack of support as a reason to
11155 give up on the trace run. */
11156 warning (_("Target does not support fast tracepoints, "
11157 "downloading %d as regular tracepoint"), b->number);
11158 }
11159 else if (b->type == bp_static_tracepoint)
11160 {
11161 /* Only test for support at download time; we may not know
11162 target capabilities at definition time. */
11163 if (remote_supports_static_tracepoints ())
0fb4aa4b 11164 {
e8ba3115 11165 struct static_tracepoint_marker marker;
0fb4aa4b 11166
e8ba3115
YQ
11167 if (target_static_tracepoint_marker_at (tpaddr, &marker))
11168 strcat (buf, ":S");
0fb4aa4b 11169 else
e8ba3115 11170 error (_("Static tracepoint not valid during download"));
0fb4aa4b 11171 }
e8ba3115
YQ
11172 else
11173 /* Fast tracepoints are functionally identical to regular
11174 tracepoints, so don't take lack of support as a reason
11175 to give up on the trace run. */
11176 error (_("Target does not support static tracepoints"));
11177 }
11178 /* If the tracepoint has a conditional, make it into an agent
11179 expression and append to the definition. */
11180 if (loc->cond)
11181 {
11182 /* Only test support at download time, we may not know target
11183 capabilities at definition time. */
11184 if (remote_supports_cond_tracepoints ())
35b1e5cc 11185 {
e8ba3115
YQ
11186 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
11187 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
bba74b36
YQ
11188 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
11189 aexpr->len);
e8ba3115
YQ
11190 pkt = buf + strlen (buf);
11191 for (ndx = 0; ndx < aexpr->len; ++ndx)
11192 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
11193 *pkt = '\0';
11194 do_cleanups (aexpr_chain);
35b1e5cc 11195 }
e8ba3115
YQ
11196 else
11197 warning (_("Target does not support conditional tracepoints, "
11198 "ignoring tp %d cond"), b->number);
11199 }
35b1e5cc 11200
d9b3f62e 11201 if (b->commands || *default_collect)
e8ba3115
YQ
11202 strcat (buf, "-");
11203 putpkt (buf);
11204 remote_get_noisy_reply (&target_buf, &target_buf_size);
11205 if (strcmp (target_buf, "OK"))
11206 error (_("Target does not support tracepoints."));
35b1e5cc 11207
e8ba3115
YQ
11208 /* do_single_steps (t); */
11209 if (tdp_actions)
11210 {
11211 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 11212 {
e8ba3115 11213 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11214 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
11215 b->number, addrbuf, /* address */
11216 tdp_actions[ndx],
11217 ((tdp_actions[ndx + 1] || stepping_actions)
11218 ? '-' : 0));
e8ba3115
YQ
11219 putpkt (buf);
11220 remote_get_noisy_reply (&target_buf,
11221 &target_buf_size);
11222 if (strcmp (target_buf, "OK"))
11223 error (_("Error on target while setting tracepoints."));
35b1e5cc 11224 }
e8ba3115
YQ
11225 }
11226 if (stepping_actions)
11227 {
11228 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 11229 {
e8ba3115 11230 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11231 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
11232 b->number, addrbuf, /* address */
11233 ((ndx == 0) ? "S" : ""),
11234 stepping_actions[ndx],
11235 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115
YQ
11236 putpkt (buf);
11237 remote_get_noisy_reply (&target_buf,
11238 &target_buf_size);
11239 if (strcmp (target_buf, "OK"))
11240 error (_("Error on target while setting tracepoints."));
35b1e5cc 11241 }
e8ba3115 11242 }
409873ef 11243
4082afcc 11244 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115 11245 {
f00aae0f 11246 if (b->location != NULL)
409873ef 11247 {
e8ba3115 11248 strcpy (buf, "QTDPsrc:");
f00aae0f
KS
11249 encode_source_string (b->number, loc->address, "at",
11250 event_location_to_string (b->location),
11251 buf + strlen (buf), 2048 - strlen (buf));
e8ba3115
YQ
11252 putpkt (buf);
11253 remote_get_noisy_reply (&target_buf, &target_buf_size);
11254 if (strcmp (target_buf, "OK"))
11255 warning (_("Target does not support source download."));
409873ef 11256 }
e8ba3115
YQ
11257 if (b->cond_string)
11258 {
11259 strcpy (buf, "QTDPsrc:");
11260 encode_source_string (b->number, loc->address,
11261 "cond", b->cond_string, buf + strlen (buf),
11262 2048 - strlen (buf));
11263 putpkt (buf);
11264 remote_get_noisy_reply (&target_buf, &target_buf_size);
11265 if (strcmp (target_buf, "OK"))
11266 warning (_("Target does not support source download."));
11267 }
11268 remote_download_command_source (b->number, loc->address,
11269 breakpoint_commands (b));
35b1e5cc 11270 }
e8ba3115
YQ
11271
11272 do_cleanups (old_chain);
35b1e5cc
SS
11273}
11274
1e4d1764 11275static int
a52a8357 11276remote_can_download_tracepoint (struct target_ops *self)
1e4d1764 11277{
1e51243a
PA
11278 struct remote_state *rs = get_remote_state ();
11279 struct trace_status *ts;
11280 int status;
11281
11282 /* Don't try to install tracepoints until we've relocated our
11283 symbols, and fetched and merged the target's tracepoint list with
11284 ours. */
11285 if (rs->starting_up)
11286 return 0;
11287
11288 ts = current_trace_status ();
8bd200f1 11289 status = remote_get_trace_status (self, ts);
1e4d1764
YQ
11290
11291 if (status == -1 || !ts->running_known || !ts->running)
11292 return 0;
11293
11294 /* If we are in a tracing experiment, but remote stub doesn't support
11295 installing tracepoint in trace, we have to return. */
11296 if (!remote_supports_install_in_trace ())
11297 return 0;
11298
11299 return 1;
11300}
11301
11302
35b1e5cc 11303static void
559d2b81
TT
11304remote_download_trace_state_variable (struct target_ops *self,
11305 struct trace_state_variable *tsv)
35b1e5cc
SS
11306{
11307 struct remote_state *rs = get_remote_state ();
00bf0b85 11308 char *p;
35b1e5cc 11309
bba74b36
YQ
11310 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
11311 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
11312 tsv->builtin);
00bf0b85
SS
11313 p = rs->buf + strlen (rs->buf);
11314 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
11315 error (_("Trace state variable name too long for tsv definition packet"));
9f1b45b0 11316 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
00bf0b85 11317 *p++ = '\0';
35b1e5cc
SS
11318 putpkt (rs->buf);
11319 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11320 if (*target_buf == '\0')
11321 error (_("Target does not support this command."));
11322 if (strcmp (target_buf, "OK") != 0)
11323 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
11324}
11325
d248b706 11326static void
46670d57
TT
11327remote_enable_tracepoint (struct target_ops *self,
11328 struct bp_location *location)
d248b706
KY
11329{
11330 struct remote_state *rs = get_remote_state ();
11331 char addr_buf[40];
11332
11333 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11334 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
11335 location->owner->number, addr_buf);
d248b706
KY
11336 putpkt (rs->buf);
11337 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11338 if (*rs->buf == '\0')
11339 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
11340 if (strcmp (rs->buf, "OK") != 0)
11341 error (_("Error on target while enabling tracepoint."));
11342}
11343
11344static void
780b049c
TT
11345remote_disable_tracepoint (struct target_ops *self,
11346 struct bp_location *location)
d248b706
KY
11347{
11348 struct remote_state *rs = get_remote_state ();
11349 char addr_buf[40];
11350
11351 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11352 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
11353 location->owner->number, addr_buf);
d248b706
KY
11354 putpkt (rs->buf);
11355 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11356 if (*rs->buf == '\0')
11357 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
11358 if (strcmp (rs->buf, "OK") != 0)
11359 error (_("Error on target while disabling tracepoint."));
11360}
11361
35b1e5cc 11362static void
583f9a86 11363remote_trace_set_readonly_regions (struct target_ops *self)
35b1e5cc
SS
11364{
11365 asection *s;
81b9b86e 11366 bfd *abfd = NULL;
35b1e5cc 11367 bfd_size_type size;
608bcef2 11368 bfd_vma vma;
35b1e5cc 11369 int anysecs = 0;
c2fa21f1 11370 int offset = 0;
35b1e5cc
SS
11371
11372 if (!exec_bfd)
11373 return; /* No information to give. */
11374
11375 strcpy (target_buf, "QTro");
9779ab84 11376 offset = strlen (target_buf);
35b1e5cc
SS
11377 for (s = exec_bfd->sections; s; s = s->next)
11378 {
11379 char tmp1[40], tmp2[40];
c2fa21f1 11380 int sec_length;
35b1e5cc
SS
11381
11382 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 11383 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
11384 (s->flags & SEC_READONLY) == 0)
11385 continue;
11386
11387 anysecs = 1;
81b9b86e 11388 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 11389 size = bfd_get_section_size (s);
608bcef2
HZ
11390 sprintf_vma (tmp1, vma);
11391 sprintf_vma (tmp2, vma + size);
c2fa21f1
HZ
11392 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
11393 if (offset + sec_length + 1 > target_buf_size)
11394 {
4082afcc 11395 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 11396 warning (_("\
c2fa21f1
HZ
11397Too many sections for read-only sections definition packet."));
11398 break;
11399 }
bba74b36
YQ
11400 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
11401 tmp1, tmp2);
c2fa21f1 11402 offset += sec_length;
35b1e5cc
SS
11403 }
11404 if (anysecs)
11405 {
11406 putpkt (target_buf);
11407 getpkt (&target_buf, &target_buf_size, 0);
11408 }
11409}
11410
11411static void
e2d1aae3 11412remote_trace_start (struct target_ops *self)
35b1e5cc
SS
11413{
11414 putpkt ("QTStart");
11415 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11416 if (*target_buf == '\0')
11417 error (_("Target does not support this command."));
11418 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11419 error (_("Bogus reply from target: %s"), target_buf);
11420}
11421
11422static int
8bd200f1 11423remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
35b1e5cc 11424{
953b98d1 11425 /* Initialize it just to avoid a GCC false warning. */
f652de6f 11426 char *p = NULL;
0df8b418 11427 /* FIXME we need to get register block size some other way. */
00bf0b85 11428 extern int trace_regblock_size;
bd3eecc3
PA
11429 enum packet_result result;
11430
4082afcc 11431 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 11432 return -1;
a744cf53 11433
00bf0b85
SS
11434 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
11435
049dc89b
JK
11436 putpkt ("qTStatus");
11437
492d29ea 11438 TRY
67f41397
JK
11439 {
11440 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
11441 }
492d29ea 11442 CATCH (ex, RETURN_MASK_ERROR)
67f41397 11443 {
598d3636
JK
11444 if (ex.error != TARGET_CLOSE_ERROR)
11445 {
11446 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
11447 return -1;
11448 }
11449 throw_exception (ex);
67f41397 11450 }
492d29ea 11451 END_CATCH
00bf0b85 11452
bd3eecc3
PA
11453 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
11454
00bf0b85 11455 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 11456 if (result == PACKET_UNKNOWN)
00bf0b85 11457 return -1;
35b1e5cc 11458
00bf0b85 11459 /* We're working with a live target. */
f5911ea1 11460 ts->filename = NULL;
00bf0b85 11461
00bf0b85 11462 if (*p++ != 'T')
35b1e5cc
SS
11463 error (_("Bogus trace status reply from target: %s"), target_buf);
11464
84cebc4a
YQ
11465 /* Function 'parse_trace_status' sets default value of each field of
11466 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
11467 parse_trace_status (p, ts);
11468
11469 return ts->running;
35b1e5cc
SS
11470}
11471
70221824 11472static void
db90e85c 11473remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
f196051f
SS
11474 struct uploaded_tp *utp)
11475{
11476 struct remote_state *rs = get_remote_state ();
f196051f
SS
11477 char *reply;
11478 struct bp_location *loc;
11479 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 11480 size_t size = get_remote_packet_size ();
f196051f
SS
11481
11482 if (tp)
11483 {
11484 tp->base.hit_count = 0;
11485 tp->traceframe_usage = 0;
11486 for (loc = tp->base.loc; loc; loc = loc->next)
11487 {
11488 /* If the tracepoint was never downloaded, don't go asking for
11489 any status. */
11490 if (tp->number_on_target == 0)
11491 continue;
bba74b36
YQ
11492 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
11493 phex_nz (loc->address, 0));
f196051f
SS
11494 putpkt (rs->buf);
11495 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11496 if (reply && *reply)
11497 {
11498 if (*reply == 'V')
11499 parse_tracepoint_status (reply + 1, bp, utp);
11500 }
11501 }
11502 }
11503 else if (utp)
11504 {
11505 utp->hit_count = 0;
11506 utp->traceframe_usage = 0;
bba74b36
YQ
11507 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
11508 phex_nz (utp->addr, 0));
f196051f
SS
11509 putpkt (rs->buf);
11510 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11511 if (reply && *reply)
11512 {
11513 if (*reply == 'V')
11514 parse_tracepoint_status (reply + 1, bp, utp);
11515 }
11516 }
11517}
11518
35b1e5cc 11519static void
74499f1b 11520remote_trace_stop (struct target_ops *self)
35b1e5cc
SS
11521{
11522 putpkt ("QTStop");
11523 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11524 if (*target_buf == '\0')
11525 error (_("Target does not support this command."));
11526 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11527 error (_("Bogus reply from target: %s"), target_buf);
11528}
11529
11530static int
bd4c6793
TT
11531remote_trace_find (struct target_ops *self,
11532 enum trace_find_type type, int num,
cc5925ad 11533 CORE_ADDR addr1, CORE_ADDR addr2,
35b1e5cc
SS
11534 int *tpp)
11535{
11536 struct remote_state *rs = get_remote_state ();
bba74b36 11537 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
11538 char *p, *reply;
11539 int target_frameno = -1, target_tracept = -1;
11540
e6e4e701
PA
11541 /* Lookups other than by absolute frame number depend on the current
11542 trace selected, so make sure it is correct on the remote end
11543 first. */
11544 if (type != tfind_number)
11545 set_remote_traceframe ();
11546
35b1e5cc
SS
11547 p = rs->buf;
11548 strcpy (p, "QTFrame:");
11549 p = strchr (p, '\0');
11550 switch (type)
11551 {
11552 case tfind_number:
bba74b36 11553 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
11554 break;
11555 case tfind_pc:
bba74b36 11556 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
11557 break;
11558 case tfind_tp:
bba74b36 11559 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
11560 break;
11561 case tfind_range:
bba74b36
YQ
11562 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
11563 phex_nz (addr2, 0));
35b1e5cc
SS
11564 break;
11565 case tfind_outside:
bba74b36
YQ
11566 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
11567 phex_nz (addr2, 0));
35b1e5cc
SS
11568 break;
11569 default:
9b20d036 11570 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
11571 }
11572
11573 putpkt (rs->buf);
2f65bcb7 11574 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
ad91cd99
PA
11575 if (*reply == '\0')
11576 error (_("Target does not support this command."));
35b1e5cc
SS
11577
11578 while (reply && *reply)
11579 switch (*reply)
11580 {
11581 case 'F':
f197e0f1
VP
11582 p = ++reply;
11583 target_frameno = (int) strtol (p, &reply, 16);
11584 if (reply == p)
11585 error (_("Unable to parse trace frame number"));
e6e4e701
PA
11586 /* Don't update our remote traceframe number cache on failure
11587 to select a remote traceframe. */
f197e0f1
VP
11588 if (target_frameno == -1)
11589 return -1;
35b1e5cc
SS
11590 break;
11591 case 'T':
f197e0f1
VP
11592 p = ++reply;
11593 target_tracept = (int) strtol (p, &reply, 16);
11594 if (reply == p)
11595 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
11596 break;
11597 case 'O': /* "OK"? */
11598 if (reply[1] == 'K' && reply[2] == '\0')
11599 reply += 2;
11600 else
11601 error (_("Bogus reply from target: %s"), reply);
11602 break;
11603 default:
11604 error (_("Bogus reply from target: %s"), reply);
11605 }
11606 if (tpp)
11607 *tpp = target_tracept;
e6e4e701 11608
262e1174 11609 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
11610 return target_frameno;
11611}
11612
11613static int
4011015b
TT
11614remote_get_trace_state_variable_value (struct target_ops *self,
11615 int tsvnum, LONGEST *val)
35b1e5cc
SS
11616{
11617 struct remote_state *rs = get_remote_state ();
11618 char *reply;
11619 ULONGEST uval;
11620
e6e4e701
PA
11621 set_remote_traceframe ();
11622
bba74b36 11623 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc
SS
11624 putpkt (rs->buf);
11625 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11626 if (reply && *reply)
11627 {
11628 if (*reply == 'V')
11629 {
11630 unpack_varlen_hex (reply + 1, &uval);
11631 *val = (LONGEST) uval;
11632 return 1;
11633 }
11634 }
11635 return 0;
11636}
11637
00bf0b85 11638static int
dc3decaf 11639remote_save_trace_data (struct target_ops *self, const char *filename)
00bf0b85
SS
11640{
11641 struct remote_state *rs = get_remote_state ();
11642 char *p, *reply;
11643
11644 p = rs->buf;
11645 strcpy (p, "QTSave:");
11646 p += strlen (p);
11647 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
11648 error (_("Remote file name too long for trace save packet"));
9f1b45b0 11649 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
11650 *p++ = '\0';
11651 putpkt (rs->buf);
ad91cd99 11652 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
d6c5869f 11653 if (*reply == '\0')
ad91cd99
PA
11654 error (_("Target does not support this command."));
11655 if (strcmp (reply, "OK") != 0)
11656 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
11657 return 0;
11658}
11659
11660/* This is basically a memory transfer, but needs to be its own packet
11661 because we don't know how the target actually organizes its trace
11662 memory, plus we want to be able to ask for as much as possible, but
11663 not be unhappy if we don't get as much as we ask for. */
11664
11665static LONGEST
88ee6f45
TT
11666remote_get_raw_trace_data (struct target_ops *self,
11667 gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
11668{
11669 struct remote_state *rs = get_remote_state ();
11670 char *reply;
11671 char *p;
11672 int rslt;
11673
11674 p = rs->buf;
11675 strcpy (p, "qTBuffer:");
11676 p += strlen (p);
11677 p += hexnumstr (p, offset);
11678 *p++ = ',';
11679 p += hexnumstr (p, len);
11680 *p++ = '\0';
11681
11682 putpkt (rs->buf);
11683 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11684 if (reply && *reply)
11685 {
11686 /* 'l' by itself means we're at the end of the buffer and
11687 there is nothing more to get. */
11688 if (*reply == 'l')
11689 return 0;
11690
11691 /* Convert the reply into binary. Limit the number of bytes to
11692 convert according to our passed-in buffer size, rather than
11693 what was returned in the packet; if the target is
11694 unexpectedly generous and gives us a bigger reply than we
11695 asked for, we don't want to crash. */
11696 rslt = hex2bin (target_buf, buf, len);
11697 return rslt;
11698 }
11699
11700 /* Something went wrong, flag as an error. */
11701 return -1;
11702}
11703
35b1e5cc 11704static void
37b25738 11705remote_set_disconnected_tracing (struct target_ops *self, int val)
35b1e5cc
SS
11706{
11707 struct remote_state *rs = get_remote_state ();
11708
4082afcc 11709 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 11710 {
ad91cd99
PA
11711 char *reply;
11712
bba74b36 11713 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 11714 putpkt (rs->buf);
ad91cd99
PA
11715 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11716 if (*reply == '\0')
33da3f1c 11717 error (_("Target does not support this command."));
ad91cd99
PA
11718 if (strcmp (reply, "OK") != 0)
11719 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
11720 }
11721 else if (val)
11722 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
11723}
11724
dc146f7c
VP
11725static int
11726remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
11727{
11728 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 11729
fe978cb0
PA
11730 if (info && info->priv)
11731 return info->priv->core;
dc146f7c
VP
11732 return -1;
11733}
11734
4daf5ac0 11735static void
736d5b1f 11736remote_set_circular_trace_buffer (struct target_ops *self, int val)
4daf5ac0
SS
11737{
11738 struct remote_state *rs = get_remote_state ();
ad91cd99 11739 char *reply;
4daf5ac0 11740
bba74b36 11741 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 11742 putpkt (rs->buf);
ad91cd99
PA
11743 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11744 if (*reply == '\0')
4daf5ac0 11745 error (_("Target does not support this command."));
ad91cd99
PA
11746 if (strcmp (reply, "OK") != 0)
11747 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
11748}
11749
b3b9301e 11750static struct traceframe_info *
a893e81f 11751remote_traceframe_info (struct target_ops *self)
b3b9301e
PA
11752{
11753 char *text;
11754
11755 text = target_read_stralloc (&current_target,
11756 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11757 if (text != NULL)
11758 {
11759 struct traceframe_info *info;
11760 struct cleanup *back_to = make_cleanup (xfree, text);
11761
11762 info = parse_traceframe_info (text);
11763 do_cleanups (back_to);
11764 return info;
11765 }
11766
11767 return NULL;
11768}
11769
405f8e94
SS
11770/* Handle the qTMinFTPILen packet. Returns the minimum length of
11771 instruction on which a fast tracepoint may be placed. Returns -1
11772 if the packet is not supported, and 0 if the minimum instruction
11773 length is unknown. */
11774
11775static int
0e67620a 11776remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
405f8e94
SS
11777{
11778 struct remote_state *rs = get_remote_state ();
11779 char *reply;
11780
e886a173
PA
11781 /* If we're not debugging a process yet, the IPA can't be
11782 loaded. */
11783 if (!target_has_execution)
11784 return 0;
11785
11786 /* Make sure the remote is pointing at the right process. */
11787 set_general_process ();
11788
bba74b36 11789 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94
SS
11790 putpkt (rs->buf);
11791 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11792 if (*reply == '\0')
11793 return -1;
11794 else
11795 {
11796 ULONGEST min_insn_len;
11797
11798 unpack_varlen_hex (reply, &min_insn_len);
11799
11800 return (int) min_insn_len;
11801 }
11802}
11803
f6f899bf 11804static void
4da384be 11805remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
f6f899bf 11806{
4082afcc 11807 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
11808 {
11809 struct remote_state *rs = get_remote_state ();
11810 char *buf = rs->buf;
11811 char *endbuf = rs->buf + get_remote_packet_size ();
11812 enum packet_result result;
11813
11814 gdb_assert (val >= 0 || val == -1);
11815 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11816 /* Send -1 as literal "-1" to avoid host size dependency. */
11817 if (val < 0)
11818 {
11819 *buf++ = '-';
11820 buf += hexnumstr (buf, (ULONGEST) -val);
11821 }
11822 else
11823 buf += hexnumstr (buf, (ULONGEST) val);
11824
11825 putpkt (rs->buf);
11826 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11827 result = packet_ok (rs->buf,
11828 &remote_protocol_packets[PACKET_QTBuffer_size]);
11829
11830 if (result != PACKET_OK)
11831 warning (_("Bogus reply from target: %s"), rs->buf);
11832 }
11833}
11834
f196051f 11835static int
d9e68a2c
TT
11836remote_set_trace_notes (struct target_ops *self,
11837 const char *user, const char *notes,
ca623f82 11838 const char *stop_notes)
f196051f
SS
11839{
11840 struct remote_state *rs = get_remote_state ();
11841 char *reply;
11842 char *buf = rs->buf;
11843 char *endbuf = rs->buf + get_remote_packet_size ();
11844 int nbytes;
11845
11846 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11847 if (user)
11848 {
11849 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 11850 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
11851 buf += 2 * nbytes;
11852 *buf++ = ';';
11853 }
11854 if (notes)
11855 {
11856 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 11857 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
11858 buf += 2 * nbytes;
11859 *buf++ = ';';
11860 }
11861 if (stop_notes)
11862 {
11863 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 11864 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
11865 buf += 2 * nbytes;
11866 *buf++ = ';';
11867 }
11868 /* Ensure the buffer is terminated. */
11869 *buf = '\0';
11870
11871 putpkt (rs->buf);
11872 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11873 if (*reply == '\0')
11874 return 0;
11875
11876 if (strcmp (reply, "OK") != 0)
11877 error (_("Bogus reply from target: %s"), reply);
11878
11879 return 1;
11880}
11881
d1feda86 11882static int
2c152180 11883remote_use_agent (struct target_ops *self, int use)
d1feda86 11884{
4082afcc 11885 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
11886 {
11887 struct remote_state *rs = get_remote_state ();
11888
11889 /* If the stub supports QAgent. */
bba74b36 11890 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
11891 putpkt (rs->buf);
11892 getpkt (&rs->buf, &rs->buf_size, 0);
11893
11894 if (strcmp (rs->buf, "OK") == 0)
11895 {
11896 use_agent = use;
11897 return 1;
11898 }
11899 }
11900
11901 return 0;
11902}
11903
11904static int
fe38f897 11905remote_can_use_agent (struct target_ops *self)
d1feda86 11906{
4082afcc 11907 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
11908}
11909
9accd112
MM
11910struct btrace_target_info
11911{
11912 /* The ptid of the traced thread. */
11913 ptid_t ptid;
f4abbc16
MM
11914
11915 /* The obtained branch trace configuration. */
11916 struct btrace_config conf;
9accd112
MM
11917};
11918
f4abbc16
MM
11919/* Reset our idea of our target's btrace configuration. */
11920
11921static void
11922remote_btrace_reset (void)
11923{
11924 struct remote_state *rs = get_remote_state ();
11925
11926 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
11927}
11928
9accd112
MM
11929/* Check whether the target supports branch tracing. */
11930
11931static int
043c3577 11932remote_supports_btrace (struct target_ops *self, enum btrace_format format)
9accd112 11933{
4082afcc 11934 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
9accd112 11935 return 0;
4082afcc 11936 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
9accd112
MM
11937 return 0;
11938
043c3577
MM
11939 switch (format)
11940 {
11941 case BTRACE_FORMAT_NONE:
11942 return 0;
11943
11944 case BTRACE_FORMAT_BTS:
11945 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
b20a6524
MM
11946
11947 case BTRACE_FORMAT_PT:
11948 /* The trace is decoded on the host. Even if our target supports it,
11949 we still need to have libipt to decode the trace. */
11950#if defined (HAVE_LIBIPT)
11951 return (packet_support (PACKET_Qbtrace_pt) == PACKET_ENABLE);
11952#else /* !defined (HAVE_LIBIPT) */
11953 return 0;
11954#endif /* !defined (HAVE_LIBIPT) */
043c3577
MM
11955 }
11956
11957 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
9accd112
MM
11958}
11959
f4abbc16
MM
11960/* Synchronize the configuration with the target. */
11961
11962static void
11963btrace_sync_conf (const struct btrace_config *conf)
11964{
d33501a5
MM
11965 struct packet_config *packet;
11966 struct remote_state *rs;
11967 char *buf, *pos, *endbuf;
11968
11969 rs = get_remote_state ();
11970 buf = rs->buf;
11971 endbuf = buf + get_remote_packet_size ();
11972
11973 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
11974 if (packet_config_support (packet) == PACKET_ENABLE
11975 && conf->bts.size != rs->btrace_config.bts.size)
11976 {
11977 pos = buf;
11978 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
11979 conf->bts.size);
11980
11981 putpkt (buf);
11982 getpkt (&buf, &rs->buf_size, 0);
11983
11984 if (packet_ok (buf, packet) == PACKET_ERROR)
11985 {
11986 if (buf[0] == 'E' && buf[1] == '.')
11987 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
11988 else
11989 error (_("Failed to configure the BTS buffer size."));
11990 }
11991
11992 rs->btrace_config.bts.size = conf->bts.size;
11993 }
b20a6524
MM
11994
11995 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_pt_size];
11996 if (packet_config_support (packet) == PACKET_ENABLE
11997 && conf->pt.size != rs->btrace_config.pt.size)
11998 {
11999 pos = buf;
12000 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
12001 conf->pt.size);
12002
12003 putpkt (buf);
12004 getpkt (&buf, &rs->buf_size, 0);
12005
12006 if (packet_ok (buf, packet) == PACKET_ERROR)
12007 {
12008 if (buf[0] == 'E' && buf[1] == '.')
12009 error (_("Failed to configure the trace buffer size: %s"), buf + 2);
12010 else
12011 error (_("Failed to configure the trace buffer size."));
12012 }
12013
12014 rs->btrace_config.pt.size = conf->pt.size;
12015 }
f4abbc16
MM
12016}
12017
12018/* Read the current thread's btrace configuration from the target and
12019 store it into CONF. */
12020
12021static void
12022btrace_read_config (struct btrace_config *conf)
12023{
12024 char *xml;
12025
12026 xml = target_read_stralloc (&current_target,
b20a6524 12027 TARGET_OBJECT_BTRACE_CONF, "");
f4abbc16
MM
12028 if (xml != NULL)
12029 {
12030 struct cleanup *cleanup;
12031
12032 cleanup = make_cleanup (xfree, xml);
12033 parse_xml_btrace_conf (conf, xml);
12034 do_cleanups (cleanup);
12035 }
12036}
12037
9accd112
MM
12038/* Enable branch tracing. */
12039
12040static struct btrace_target_info *
f4abbc16
MM
12041remote_enable_btrace (struct target_ops *self, ptid_t ptid,
12042 const struct btrace_config *conf)
9accd112
MM
12043{
12044 struct btrace_target_info *tinfo = NULL;
b20a6524 12045 struct packet_config *packet = NULL;
9accd112
MM
12046 struct remote_state *rs = get_remote_state ();
12047 char *buf = rs->buf;
12048 char *endbuf = rs->buf + get_remote_packet_size ();
12049
b20a6524
MM
12050 switch (conf->format)
12051 {
12052 case BTRACE_FORMAT_BTS:
12053 packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
12054 break;
12055
12056 case BTRACE_FORMAT_PT:
12057 packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
12058 break;
12059 }
12060
12061 if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12062 error (_("Target does not support branch tracing."));
12063
f4abbc16
MM
12064 btrace_sync_conf (conf);
12065
9accd112
MM
12066 set_general_thread (ptid);
12067
12068 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12069 putpkt (rs->buf);
12070 getpkt (&rs->buf, &rs->buf_size, 0);
12071
12072 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12073 {
12074 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12075 error (_("Could not enable branch tracing for %s: %s"),
12076 target_pid_to_str (ptid), rs->buf + 2);
12077 else
12078 error (_("Could not enable branch tracing for %s."),
12079 target_pid_to_str (ptid));
12080 }
12081
12082 tinfo = xzalloc (sizeof (*tinfo));
12083 tinfo->ptid = ptid;
12084
f4abbc16
MM
12085 /* If we fail to read the configuration, we lose some information, but the
12086 tracing itself is not impacted. */
492d29ea
PA
12087 TRY
12088 {
12089 btrace_read_config (&tinfo->conf);
12090 }
12091 CATCH (err, RETURN_MASK_ERROR)
12092 {
12093 if (err.message != NULL)
12094 warning ("%s", err.message);
12095 }
12096 END_CATCH
f4abbc16 12097
9accd112
MM
12098 return tinfo;
12099}
12100
12101/* Disable branch tracing. */
12102
12103static void
25e95349
TT
12104remote_disable_btrace (struct target_ops *self,
12105 struct btrace_target_info *tinfo)
9accd112
MM
12106{
12107 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
12108 struct remote_state *rs = get_remote_state ();
12109 char *buf = rs->buf;
12110 char *endbuf = rs->buf + get_remote_packet_size ();
12111
4082afcc 12112 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12113 error (_("Target does not support branch tracing."));
12114
12115 set_general_thread (tinfo->ptid);
12116
12117 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12118 putpkt (rs->buf);
12119 getpkt (&rs->buf, &rs->buf_size, 0);
12120
12121 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12122 {
12123 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12124 error (_("Could not disable branch tracing for %s: %s"),
12125 target_pid_to_str (tinfo->ptid), rs->buf + 2);
12126 else
12127 error (_("Could not disable branch tracing for %s."),
12128 target_pid_to_str (tinfo->ptid));
12129 }
12130
12131 xfree (tinfo);
12132}
12133
12134/* Teardown branch tracing. */
12135
12136static void
1777056d
TT
12137remote_teardown_btrace (struct target_ops *self,
12138 struct btrace_target_info *tinfo)
9accd112
MM
12139{
12140 /* We must not talk to the target during teardown. */
12141 xfree (tinfo);
12142}
12143
12144/* Read the branch trace. */
12145
969c39fb 12146static enum btrace_error
39c49f83 12147remote_read_btrace (struct target_ops *self,
734b0e4b 12148 struct btrace_data *btrace,
969c39fb 12149 struct btrace_target_info *tinfo,
9accd112
MM
12150 enum btrace_read_type type)
12151{
12152 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
12153 struct remote_state *rs = get_remote_state ();
969c39fb 12154 struct cleanup *cleanup;
9accd112
MM
12155 const char *annex;
12156 char *xml;
12157
4082afcc 12158 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12159 error (_("Target does not support branch tracing."));
12160
12161#if !defined(HAVE_LIBEXPAT)
12162 error (_("Cannot process branch tracing result. XML parsing not supported."));
12163#endif
12164
12165 switch (type)
12166 {
864089d2 12167 case BTRACE_READ_ALL:
9accd112
MM
12168 annex = "all";
12169 break;
864089d2 12170 case BTRACE_READ_NEW:
9accd112
MM
12171 annex = "new";
12172 break;
969c39fb
MM
12173 case BTRACE_READ_DELTA:
12174 annex = "delta";
12175 break;
9accd112
MM
12176 default:
12177 internal_error (__FILE__, __LINE__,
12178 _("Bad branch tracing read type: %u."),
12179 (unsigned int) type);
12180 }
12181
12182 xml = target_read_stralloc (&current_target,
b20a6524 12183 TARGET_OBJECT_BTRACE, annex);
969c39fb
MM
12184 if (xml == NULL)
12185 return BTRACE_ERR_UNKNOWN;
9accd112 12186
969c39fb 12187 cleanup = make_cleanup (xfree, xml);
734b0e4b 12188 parse_xml_btrace (btrace, xml);
969c39fb 12189 do_cleanups (cleanup);
9accd112 12190
969c39fb 12191 return BTRACE_ERR_NONE;
9accd112
MM
12192}
12193
f4abbc16
MM
12194static const struct btrace_config *
12195remote_btrace_conf (struct target_ops *self,
12196 const struct btrace_target_info *tinfo)
12197{
12198 return &tinfo->conf;
12199}
12200
ced63ec0 12201static int
5436ff03 12202remote_augmented_libraries_svr4_read (struct target_ops *self)
ced63ec0 12203{
4082afcc
PA
12204 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
12205 == PACKET_ENABLE);
ced63ec0
GB
12206}
12207
9dd130a0
TT
12208/* Implementation of to_load. */
12209
12210static void
9cbe5fff 12211remote_load (struct target_ops *self, const char *name, int from_tty)
9dd130a0
TT
12212{
12213 generic_load (name, from_tty);
12214}
12215
c78fa86a
GB
12216/* Accepts an integer PID; returns a string representing a file that
12217 can be opened on the remote side to get the symbols for the child
12218 process. Returns NULL if the operation is not supported. */
12219
12220static char *
12221remote_pid_to_exec_file (struct target_ops *self, int pid)
12222{
12223 static char *filename = NULL;
835205d0
GB
12224 struct inferior *inf;
12225 char *annex = NULL;
c78fa86a
GB
12226
12227 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
12228 return NULL;
12229
12230 if (filename != NULL)
12231 xfree (filename);
12232
835205d0
GB
12233 inf = find_inferior_pid (pid);
12234 if (inf == NULL)
12235 internal_error (__FILE__, __LINE__,
12236 _("not currently attached to process %d"), pid);
12237
12238 if (!inf->fake_pid_p)
12239 {
12240 const int annex_size = 9;
12241
12242 annex = alloca (annex_size);
12243 xsnprintf (annex, annex_size, "%x", pid);
12244 }
12245
c78fa86a
GB
12246 filename = target_read_stralloc (&current_target,
12247 TARGET_OBJECT_EXEC_FILE, annex);
12248
12249 return filename;
12250}
12251
c906108c 12252static void
fba45db2 12253init_remote_ops (void)
c906108c 12254{
c5aa993b 12255 remote_ops.to_shortname = "remote";
c906108c 12256 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 12257 remote_ops.to_doc =
c906108c 12258 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
12259Specify the serial device it is connected to\n\
12260(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
12261 remote_ops.to_open = remote_open;
12262 remote_ops.to_close = remote_close;
c906108c 12263 remote_ops.to_detach = remote_detach;
6ad8ae5c 12264 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 12265 remote_ops.to_resume = remote_resume;
c906108c
SS
12266 remote_ops.to_wait = remote_wait;
12267 remote_ops.to_fetch_registers = remote_fetch_registers;
12268 remote_ops.to_store_registers = remote_store_registers;
12269 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b 12270 remote_ops.to_files_info = remote_files_info;
c906108c
SS
12271 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
12272 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
f7e6eed5
PA
12273 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
12274 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
12275 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
12276 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
3c3bea1c
GS
12277 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
12278 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
12279 remote_ops.to_watchpoint_addr_within_range =
12280 remote_watchpoint_addr_within_range;
3c3bea1c
GS
12281 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
12282 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
12283 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
12284 remote_ops.to_region_ok_for_hw_watchpoint
12285 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
12286 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
12287 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b 12288 remote_ops.to_kill = remote_kill;
9dd130a0 12289 remote_ops.to_load = remote_load;
c906108c 12290 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 12291 remote_ops.to_pass_signals = remote_pass_signals;
9b224c5e 12292 remote_ops.to_program_signals = remote_program_signals;
c906108c 12293 remote_ops.to_thread_alive = remote_thread_alive;
e8032dde 12294 remote_ops.to_update_thread_list = remote_update_thread_list;
0caabb7e 12295 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 12296 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 12297 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 12298 remote_ops.to_stop = remote_stop;
bfedc46a 12299 remote_ops.to_interrupt = remote_interrupt;
4b8a223f 12300 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 12301 remote_ops.to_rcmd = remote_rcmd;
c78fa86a 12302 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
49d03eab 12303 remote_ops.to_log_command = serial_log_command;
38691318 12304 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 12305 remote_ops.to_stratum = process_stratum;
c35b1492
PA
12306 remote_ops.to_has_all_memory = default_child_has_all_memory;
12307 remote_ops.to_has_memory = default_child_has_memory;
12308 remote_ops.to_has_stack = default_child_has_stack;
12309 remote_ops.to_has_registers = default_child_has_registers;
12310 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 12311 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 12312 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 12313 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 12314 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
12315 remote_ops.to_flash_erase = remote_flash_erase;
12316 remote_ops.to_flash_done = remote_flash_done;
29709017 12317 remote_ops.to_read_description = remote_read_description;
08388c79 12318 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
12319 remote_ops.to_can_async_p = remote_can_async_p;
12320 remote_ops.to_is_async_p = remote_is_async_p;
12321 remote_ops.to_async = remote_async;
75c99385
PA
12322 remote_ops.to_terminal_inferior = remote_terminal_inferior;
12323 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 12324 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 12325 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
12326 remote_ops.to_supports_disable_randomization
12327 = remote_supports_disable_randomization;
4bd7dc42 12328 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
7313baad
UW
12329 remote_ops.to_fileio_open = remote_hostio_open;
12330 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
12331 remote_ops.to_fileio_pread = remote_hostio_pread;
9b15c1f0 12332 remote_ops.to_fileio_fstat = remote_hostio_fstat;
7313baad
UW
12333 remote_ops.to_fileio_close = remote_hostio_close;
12334 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 12335 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 12336 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 12337 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 12338 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
d3ce09f5 12339 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
35b1e5cc
SS
12340 remote_ops.to_trace_init = remote_trace_init;
12341 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 12342 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
12343 remote_ops.to_download_trace_state_variable
12344 = remote_download_trace_state_variable;
d248b706
KY
12345 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
12346 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
12347 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
12348 remote_ops.to_trace_start = remote_trace_start;
12349 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 12350 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
12351 remote_ops.to_trace_stop = remote_trace_stop;
12352 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
12353 remote_ops.to_get_trace_state_variable_value
12354 = remote_get_trace_state_variable_value;
00bf0b85
SS
12355 remote_ops.to_save_trace_data = remote_save_trace_data;
12356 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
12357 remote_ops.to_upload_trace_state_variables
12358 = remote_upload_trace_state_variables;
00bf0b85 12359 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 12360 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 12361 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 12362 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f6f899bf 12363 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
f196051f 12364 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 12365 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 12366 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 12367 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 12368 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
12369 remote_ops.to_static_tracepoint_marker_at
12370 = remote_static_tracepoint_marker_at;
12371 remote_ops.to_static_tracepoint_markers_by_strid
12372 = remote_static_tracepoint_markers_by_strid;
b3b9301e 12373 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
12374 remote_ops.to_use_agent = remote_use_agent;
12375 remote_ops.to_can_use_agent = remote_can_use_agent;
9accd112
MM
12376 remote_ops.to_supports_btrace = remote_supports_btrace;
12377 remote_ops.to_enable_btrace = remote_enable_btrace;
12378 remote_ops.to_disable_btrace = remote_disable_btrace;
12379 remote_ops.to_teardown_btrace = remote_teardown_btrace;
12380 remote_ops.to_read_btrace = remote_read_btrace;
f4abbc16 12381 remote_ops.to_btrace_conf = remote_btrace_conf;
ced63ec0
GB
12382 remote_ops.to_augmented_libraries_svr4_read =
12383 remote_augmented_libraries_svr4_read;
c906108c
SS
12384}
12385
12386/* Set up the extended remote vector by making a copy of the standard
12387 remote vector and adding to it. */
12388
12389static void
fba45db2 12390init_extended_remote_ops (void)
c906108c
SS
12391{
12392 extended_remote_ops = remote_ops;
12393
0f71a2f6 12394 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 12395 extended_remote_ops.to_longname =
c906108c 12396 "Extended remote serial target in gdb-specific protocol";
c5aa993b 12397 extended_remote_ops.to_doc =
c906108c 12398 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
12399Specify the serial device it is connected to (e.g. /dev/ttya).";
12400 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
12401 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
12402 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2d717e4f
DJ
12403 extended_remote_ops.to_detach = extended_remote_detach;
12404 extended_remote_ops.to_attach = extended_remote_attach;
b9c1d481 12405 extended_remote_ops.to_post_attach = extended_remote_post_attach;
82f73884 12406 extended_remote_ops.to_kill = extended_remote_kill;
03583c20
UW
12407 extended_remote_ops.to_supports_disable_randomization
12408 = extended_remote_supports_disable_randomization;
de0d863e 12409 extended_remote_ops.to_follow_fork = remote_follow_fork;
cbb8991c
DB
12410 extended_remote_ops.to_insert_fork_catchpoint
12411 = remote_insert_fork_catchpoint;
12412 extended_remote_ops.to_remove_fork_catchpoint
12413 = remote_remove_fork_catchpoint;
12414 extended_remote_ops.to_insert_vfork_catchpoint
12415 = remote_insert_vfork_catchpoint;
12416 extended_remote_ops.to_remove_vfork_catchpoint
12417 = remote_remove_vfork_catchpoint;
0f71a2f6
JM
12418}
12419
6426a772 12420static int
6a109b6b 12421remote_can_async_p (struct target_ops *ops)
6426a772 12422{
5d93a237
TT
12423 struct remote_state *rs = get_remote_state ();
12424
c6ebd6cf 12425 if (!target_async_permitted)
75c99385
PA
12426 /* We only enable async when the user specifically asks for it. */
12427 return 0;
12428
23860348 12429 /* We're async whenever the serial device is. */
5d93a237 12430 return serial_can_async_p (rs->remote_desc);
6426a772
JM
12431}
12432
12433static int
6a109b6b 12434remote_is_async_p (struct target_ops *ops)
6426a772 12435{
5d93a237
TT
12436 struct remote_state *rs = get_remote_state ();
12437
c6ebd6cf 12438 if (!target_async_permitted)
75c99385
PA
12439 /* We only enable async when the user specifically asks for it. */
12440 return 0;
12441
23860348 12442 /* We're async whenever the serial device is. */
5d93a237 12443 return serial_is_async_p (rs->remote_desc);
6426a772
JM
12444}
12445
2acceee2
JM
12446/* Pass the SERIAL event on and up to the client. One day this code
12447 will be able to delay notifying the client of an event until the
23860348 12448 point where an entire packet has been received. */
2acceee2 12449
2acceee2
JM
12450static serial_event_ftype remote_async_serial_handler;
12451
6426a772 12452static void
819cc324 12453remote_async_serial_handler (struct serial *scb, void *context)
6426a772 12454{
88b496c3
TT
12455 struct remote_state *rs = context;
12456
2acceee2
JM
12457 /* Don't propogate error information up to the client. Instead let
12458 the client find out about the error by querying the target. */
6a3753b3 12459 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
12460}
12461
74531fed
PA
12462static void
12463remote_async_inferior_event_handler (gdb_client_data data)
12464{
12465 inferior_event_handler (INF_REG_EVENT, NULL);
12466}
12467
2acceee2 12468static void
6a3753b3 12469remote_async (struct target_ops *ops, int enable)
2acceee2 12470{
5d93a237
TT
12471 struct remote_state *rs = get_remote_state ();
12472
6a3753b3 12473 if (enable)
2acceee2 12474 {
88b496c3 12475 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
12476
12477 /* If there are pending events in the stop reply queue tell the
12478 event loop to process them. */
12479 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
12480 mark_async_event_handler (remote_async_inferior_event_token);
2acceee2
JM
12481 }
12482 else
b7d2e916
PA
12483 {
12484 serial_async (rs->remote_desc, NULL, NULL);
12485 clear_async_event_handler (remote_async_inferior_event_token);
12486 }
6426a772
JM
12487}
12488
5a2468f5 12489static void
c2d11a7d 12490set_remote_cmd (char *args, int from_tty)
5a2468f5 12491{
635c7e8a 12492 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
12493}
12494
d471ea57
AC
12495static void
12496show_remote_cmd (char *args, int from_tty)
12497{
37a105a1 12498 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 12499 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1
DJ
12500 struct cleanup *showlist_chain;
12501 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 12502 struct ui_out *uiout = current_uiout;
37a105a1
DJ
12503
12504 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
12505 for (; list != NULL; list = list->next)
12506 if (strcmp (list->name, "Z-packet") == 0)
12507 continue;
427c3a89
DJ
12508 else if (list->type == not_set_cmd)
12509 /* Alias commands are exactly like the original, except they
12510 don't have the normal type. */
12511 continue;
12512 else
37a105a1
DJ
12513 {
12514 struct cleanup *option_chain
12515 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
a744cf53 12516
37a105a1
DJ
12517 ui_out_field_string (uiout, "name", list->name);
12518 ui_out_text (uiout, ": ");
427c3a89 12519 if (list->type == show_cmd)
5b9afe8a 12520 do_show_command ((char *) NULL, from_tty, list);
427c3a89
DJ
12521 else
12522 cmd_func (list, NULL, from_tty);
37a105a1
DJ
12523 /* Close the tuple. */
12524 do_cleanups (option_chain);
12525 }
427c3a89
DJ
12526
12527 /* Close the tuple. */
12528 do_cleanups (showlist_chain);
d471ea57 12529}
5a2468f5 12530
0f71a2f6 12531
23860348 12532/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
12533static void
12534remote_new_objfile (struct objfile *objfile)
12535{
5d93a237
TT
12536 struct remote_state *rs = get_remote_state ();
12537
12538 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 12539 remote_check_symbols ();
dc8acb97
MS
12540}
12541
00bf0b85
SS
12542/* Pull all the tracepoints defined on the target and create local
12543 data structures representing them. We don't want to create real
12544 tracepoints yet, we don't want to mess up the user's existing
12545 collection. */
12546
12547static int
ab6617cc 12548remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
d5551862 12549{
00bf0b85
SS
12550 struct remote_state *rs = get_remote_state ();
12551 char *p;
d5551862 12552
00bf0b85
SS
12553 /* Ask for a first packet of tracepoint definition. */
12554 putpkt ("qTfP");
12555 getpkt (&rs->buf, &rs->buf_size, 0);
12556 p = rs->buf;
12557 while (*p && *p != 'l')
d5551862 12558 {
00bf0b85
SS
12559 parse_tracepoint_definition (p, utpp);
12560 /* Ask for another packet of tracepoint definition. */
12561 putpkt ("qTsP");
12562 getpkt (&rs->buf, &rs->buf_size, 0);
12563 p = rs->buf;
d5551862 12564 }
00bf0b85 12565 return 0;
d5551862
SS
12566}
12567
00bf0b85 12568static int
181e3713
TT
12569remote_upload_trace_state_variables (struct target_ops *self,
12570 struct uploaded_tsv **utsvp)
d5551862 12571{
00bf0b85 12572 struct remote_state *rs = get_remote_state ();
d5551862 12573 char *p;
d5551862 12574
00bf0b85
SS
12575 /* Ask for a first packet of variable definition. */
12576 putpkt ("qTfV");
d5551862
SS
12577 getpkt (&rs->buf, &rs->buf_size, 0);
12578 p = rs->buf;
00bf0b85 12579 while (*p && *p != 'l')
d5551862 12580 {
00bf0b85
SS
12581 parse_tsv_definition (p, utsvp);
12582 /* Ask for another packet of variable definition. */
12583 putpkt ("qTsV");
d5551862
SS
12584 getpkt (&rs->buf, &rs->buf_size, 0);
12585 p = rs->buf;
12586 }
00bf0b85 12587 return 0;
d5551862
SS
12588}
12589
c1e36e3e
PA
12590/* The "set/show range-stepping" show hook. */
12591
12592static void
12593show_range_stepping (struct ui_file *file, int from_tty,
12594 struct cmd_list_element *c,
12595 const char *value)
12596{
12597 fprintf_filtered (file,
12598 _("Debugger's willingness to use range stepping "
12599 "is %s.\n"), value);
12600}
12601
12602/* The "set/show range-stepping" set hook. */
12603
12604static void
12605set_range_stepping (char *ignore_args, int from_tty,
12606 struct cmd_list_element *c)
12607{
5d93a237
TT
12608 struct remote_state *rs = get_remote_state ();
12609
c1e36e3e
PA
12610 /* Whene enabling, check whether range stepping is actually
12611 supported by the target, and warn if not. */
12612 if (use_range_stepping)
12613 {
5d93a237 12614 if (rs->remote_desc != NULL)
c1e36e3e 12615 {
4082afcc 12616 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
12617 remote_vcont_probe (rs);
12618
4082afcc 12619 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
12620 && rs->supports_vCont.r)
12621 return;
12622 }
12623
12624 warning (_("Range stepping is not supported by the current target"));
12625 }
12626}
12627
c906108c 12628void
fba45db2 12629_initialize_remote (void)
c906108c 12630{
ea9c271d 12631 struct remote_state *rs;
9a7071a8 12632 struct cmd_list_element *cmd;
6f937416 12633 const char *cmd_name;
ea9c271d 12634
0f71a2f6 12635 /* architecture specific data */
2bc416ba 12636 remote_gdbarch_data_handle =
23860348 12637 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
12638 remote_g_packet_data_handle =
12639 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 12640
ea9c271d
DJ
12641 /* Initialize the per-target state. At the moment there is only one
12642 of these, not one per target. Only one target is active at a
cf792862
TT
12643 time. */
12644 remote_state = new_remote_state ();
ea9c271d 12645
c906108c
SS
12646 init_remote_ops ();
12647 add_target (&remote_ops);
12648
12649 init_extended_remote_ops ();
12650 add_target (&extended_remote_ops);
cce74817 12651
dc8acb97 12652 /* Hook into new objfile notification. */
06d3b283 12653 observer_attach_new_objfile (remote_new_objfile);
5f4cf0bb
YQ
12654 /* We're no longer interested in notification events of an inferior
12655 when it exits. */
12656 observer_attach_inferior_exit (discard_pending_stop_replies);
dc8acb97 12657
b803fb0f 12658 /* Set up signal handlers. */
934b9bac 12659 async_sigint_remote_token =
b803fb0f 12660 create_async_signal_handler (async_remote_interrupt, NULL);
934b9bac 12661 async_sigint_remote_twice_token =
6d549500 12662 create_async_signal_handler (async_remote_interrupt_twice, NULL);
b803fb0f 12663
c906108c
SS
12664#if 0
12665 init_remote_threadtests ();
12666#endif
12667
722247f1 12668 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 12669 /* set/show remote ... */
d471ea57 12670
1bedd215 12671 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
12672Remote protocol specific variables\n\
12673Configure various remote-protocol specific variables such as\n\
1bedd215 12674the packets being used"),
cff3e48b 12675 &remote_set_cmdlist, "set remote ",
23860348 12676 0 /* allow-unknown */, &setlist);
1bedd215 12677 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
12678Remote protocol specific variables\n\
12679Configure various remote-protocol specific variables such as\n\
1bedd215 12680the packets being used"),
cff3e48b 12681 &remote_show_cmdlist, "show remote ",
23860348 12682 0 /* allow-unknown */, &showlist);
5a2468f5 12683
1a966eab
AC
12684 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
12685Compare section data on target to the exec file.\n\
95cf3b38
DT
12686Argument is a single section name (default: all loaded sections).\n\
12687To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
12688 &cmdlist);
12689
1a966eab
AC
12690 add_cmd ("packet", class_maintenance, packet_command, _("\
12691Send an arbitrary packet to a remote target.\n\
c906108c
SS
12692 maintenance packet TEXT\n\
12693If GDB is talking to an inferior via the GDB serial protocol, then\n\
12694this command sends the string TEXT to the inferior, and displays the\n\
12695response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 12696terminating `#' character and checksum."),
c906108c
SS
12697 &maintenancelist);
12698
7915a72c
AC
12699 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
12700Set whether to send break if interrupted."), _("\
12701Show whether to send break if interrupted."), _("\
12702If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 12703 set_remotebreak, show_remotebreak,
e707bbc2 12704 &setlist, &showlist);
9a7071a8
JB
12705 cmd_name = "remotebreak";
12706 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
12707 deprecate_cmd (cmd, "set remote interrupt-sequence");
12708 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
12709 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
12710 deprecate_cmd (cmd, "show remote interrupt-sequence");
12711
12712 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
12713 interrupt_sequence_modes, &interrupt_sequence_mode,
12714 _("\
9a7071a8
JB
12715Set interrupt sequence to remote target."), _("\
12716Show interrupt sequence to remote target."), _("\
12717Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
12718 NULL, show_interrupt_sequence,
12719 &remote_set_cmdlist,
12720 &remote_show_cmdlist);
12721
12722 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
12723 &interrupt_on_connect, _("\
12724Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12725Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12726If set, interrupt sequence is sent to remote target."),
12727 NULL, NULL,
12728 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 12729
23860348 12730 /* Install commands for configuring memory read/write packets. */
11cf8741 12731
1a966eab
AC
12732 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
12733Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 12734 &setlist);
1a966eab
AC
12735 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
12736Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
12737 &showlist);
12738 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
12739 set_memory_write_packet_size, _("\
12740Set the maximum number of bytes per memory-write packet.\n\
12741Specify the number of bytes in a packet or 0 (zero) for the\n\
12742default packet size. The actual limit is further reduced\n\
12743dependent on the target. Specify ``fixed'' to disable the\n\
12744further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12745 &remote_set_cmdlist);
12746 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
12747 set_memory_read_packet_size, _("\
12748Set the maximum number of bytes per memory-read packet.\n\
12749Specify the number of bytes in a packet or 0 (zero) for the\n\
12750default packet size. The actual limit is further reduced\n\
12751dependent on the target. Specify ``fixed'' to disable the\n\
12752further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12753 &remote_set_cmdlist);
12754 add_cmd ("memory-write-packet-size", no_class,
12755 show_memory_write_packet_size,
1a966eab 12756 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
12757 &remote_show_cmdlist);
12758 add_cmd ("memory-read-packet-size", no_class,
12759 show_memory_read_packet_size,
1a966eab 12760 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 12761 &remote_show_cmdlist);
c906108c 12762
b3f42336 12763 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
12764 &remote_hw_watchpoint_limit, _("\
12765Set the maximum number of target hardware watchpoints."), _("\
12766Show the maximum number of target hardware watchpoints."), _("\
12767Specify a negative limit for unlimited."),
3e43a32a
MS
12768 NULL, NULL, /* FIXME: i18n: The maximum
12769 number of target hardware
12770 watchpoints is %s. */
b3f42336 12771 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
12772 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
12773 &remote_hw_watchpoint_length_limit, _("\
12774Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
12775Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
12776Specify a negative limit for unlimited."),
12777 NULL, NULL, /* FIXME: i18n: The maximum
12778 length (in bytes) of a target
12779 hardware watchpoint is %s. */
12780 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 12781 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
12782 &remote_hw_breakpoint_limit, _("\
12783Set the maximum number of target hardware breakpoints."), _("\
12784Show the maximum number of target hardware breakpoints."), _("\
12785Specify a negative limit for unlimited."),
3e43a32a
MS
12786 NULL, NULL, /* FIXME: i18n: The maximum
12787 number of target hardware
12788 breakpoints is %s. */
b3f42336 12789 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 12790
1b493192
PA
12791 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
12792 &remote_address_size, _("\
4d28ad1e
AC
12793Set the maximum size of the address (in bits) in a memory packet."), _("\
12794Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
12795 NULL,
12796 NULL, /* FIXME: i18n: */
12797 &setlist, &showlist);
c906108c 12798
ca4f7f8b
PA
12799 init_all_packet_configs ();
12800
444abaca 12801 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 12802 "X", "binary-download", 1);
0f71a2f6 12803
444abaca 12804 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 12805 "vCont", "verbose-resume", 0);
506fb367 12806
89be2091
DJ
12807 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
12808 "QPassSignals", "pass-signals", 0);
12809
9b224c5e
PA
12810 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
12811 "QProgramSignals", "program-signals", 0);
12812
444abaca 12813 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 12814 "qSymbol", "symbol-lookup", 0);
dc8acb97 12815
444abaca 12816 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 12817 "P", "set-register", 1);
d471ea57 12818
444abaca 12819 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 12820 "p", "fetch-register", 1);
b96ec7ac 12821
444abaca 12822 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 12823 "Z0", "software-breakpoint", 0);
d471ea57 12824
444abaca 12825 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 12826 "Z1", "hardware-breakpoint", 0);
d471ea57 12827
444abaca 12828 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 12829 "Z2", "write-watchpoint", 0);
d471ea57 12830
444abaca 12831 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 12832 "Z3", "read-watchpoint", 0);
d471ea57 12833
444abaca 12834 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 12835 "Z4", "access-watchpoint", 0);
d471ea57 12836
0876f84a
DJ
12837 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
12838 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 12839
c78fa86a
GB
12840 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
12841 "qXfer:exec-file:read", "pid-to-exec-file", 0);
12842
23181151
DJ
12843 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
12844 "qXfer:features:read", "target-features", 0);
12845
cfa9d6d9
DJ
12846 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
12847 "qXfer:libraries:read", "library-info", 0);
12848
2268b414
JK
12849 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
12850 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
12851
fd79ecee
DJ
12852 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
12853 "qXfer:memory-map:read", "memory-map", 0);
12854
0e7f50da
UW
12855 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
12856 "qXfer:spu:read", "read-spu-object", 0);
12857
12858 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
12859 "qXfer:spu:write", "write-spu-object", 0);
12860
07e059b5
VP
12861 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
12862 "qXfer:osdata:read", "osdata", 0);
12863
dc146f7c
VP
12864 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
12865 "qXfer:threads:read", "threads", 0);
12866
4aa995e1
PA
12867 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
12868 "qXfer:siginfo:read", "read-siginfo-object", 0);
12869
12870 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
12871 "qXfer:siginfo:write", "write-siginfo-object", 0);
12872
b3b9301e
PA
12873 add_packet_config_cmd
12874 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 12875 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 12876
169081d0
TG
12877 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
12878 "qXfer:uib:read", "unwind-info-block", 0);
12879
444abaca 12880 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 12881 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
12882 0);
12883
711e434b
PM
12884 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
12885 "qGetTIBAddr", "get-thread-information-block-address",
12886 0);
12887
40ab02ce
MS
12888 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
12889 "bc", "reverse-continue", 0);
12890
12891 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
12892 "bs", "reverse-step", 0);
12893
be2a5f71
DJ
12894 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
12895 "qSupported", "supported-packets", 0);
12896
08388c79
DE
12897 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
12898 "qSearch:memory", "search-memory", 0);
12899
bd3eecc3
PA
12900 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
12901 "qTStatus", "trace-status", 0);
12902
15a201c8
GB
12903 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
12904 "vFile:setfs", "hostio-setfs", 0);
12905
a6b151f1
DJ
12906 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
12907 "vFile:open", "hostio-open", 0);
12908
12909 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
12910 "vFile:pread", "hostio-pread", 0);
12911
12912 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
12913 "vFile:pwrite", "hostio-pwrite", 0);
12914
12915 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
12916 "vFile:close", "hostio-close", 0);
12917
12918 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
12919 "vFile:unlink", "hostio-unlink", 0);
12920
b9e7b9c3
UW
12921 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
12922 "vFile:readlink", "hostio-readlink", 0);
12923
0a93529c
GB
12924 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
12925 "vFile:fstat", "hostio-fstat", 0);
12926
2d717e4f
DJ
12927 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
12928 "vAttach", "attach", 0);
12929
12930 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
12931 "vRun", "run", 0);
12932
a6f3e723
SL
12933 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
12934 "QStartNoAckMode", "noack", 0);
12935
82f73884
PA
12936 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
12937 "vKill", "kill", 0);
12938
0b16c5cf
PA
12939 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
12940 "qAttached", "query-attached", 0);
12941
782b2b07 12942 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
12943 "ConditionalTracepoints",
12944 "conditional-tracepoints", 0);
3788aec7
LM
12945
12946 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
12947 "ConditionalBreakpoints",
12948 "conditional-breakpoints", 0);
12949
d3ce09f5
SS
12950 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
12951 "BreakpointCommands",
12952 "breakpoint-commands", 0);
12953
7a697b8d
SS
12954 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
12955 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 12956
409873ef
SS
12957 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
12958 "TracepointSource", "TracepointSource", 0);
12959
d914c394
SS
12960 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
12961 "QAllow", "allow", 0);
12962
0fb4aa4b
PA
12963 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
12964 "StaticTracepoints", "static-tracepoints", 0);
12965
1e4d1764
YQ
12966 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
12967 "InstallInTrace", "install-in-trace", 0);
12968
0fb4aa4b
PA
12969 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
12970 "qXfer:statictrace:read", "read-sdata-object", 0);
12971
78d85199
YQ
12972 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
12973 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12974
03583c20
UW
12975 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
12976 "QDisableRandomization", "disable-randomization", 0);
12977
d1feda86
YQ
12978 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
12979 "QAgent", "agent", 0);
12980
f6f899bf
HAQ
12981 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
12982 "QTBuffer:size", "trace-buffer-size", 0);
12983
9accd112
MM
12984 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
12985 "Qbtrace:off", "disable-btrace", 0);
12986
12987 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
b20a6524
MM
12988 "Qbtrace:bts", "enable-btrace-bts", 0);
12989
12990 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_pt],
12991 "Qbtrace:pt", "enable-btrace-pt", 0);
9accd112
MM
12992
12993 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
12994 "qXfer:btrace", "read-btrace", 0);
12995
f4abbc16
MM
12996 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
12997 "qXfer:btrace-conf", "read-btrace-conf", 0);
12998
d33501a5
MM
12999 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
13000 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
13001
f7e6eed5
PA
13002 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
13003 "swbreak-feature", "swbreak-feature", 0);
13004
13005 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
13006 "hwbreak-feature", "hwbreak-feature", 0);
13007
89245bc0
DB
13008 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
13009 "fork-event-feature", "fork-event-feature", 0);
13010
13011 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
13012 "vfork-event-feature", "vfork-event-feature", 0);
13013
b20a6524
MM
13014 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_pt_size],
13015 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
13016
0b736949
DB
13017 /* Assert that we've registered "set remote foo-packet" commands
13018 for all packet configs. */
ca4f7f8b
PA
13019 {
13020 int i;
13021
13022 for (i = 0; i < PACKET_MAX; i++)
13023 {
13024 /* Ideally all configs would have a command associated. Some
13025 still don't though. */
13026 int excepted;
13027
13028 switch (i)
13029 {
13030 case PACKET_QNonStop:
13031 case PACKET_multiprocess_feature:
13032 case PACKET_EnableDisableTracepoints_feature:
13033 case PACKET_tracenz_feature:
13034 case PACKET_DisconnectedTracing_feature:
13035 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
13036 case PACKET_qCRC:
13037 /* Additions to this list need to be well justified:
13038 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
13039 excepted = 1;
13040 break;
13041 default:
13042 excepted = 0;
13043 break;
13044 }
13045
13046 /* This catches both forgetting to add a config command, and
13047 forgetting to remove a packet from the exception list. */
13048 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
13049 }
13050 }
13051
37a105a1
DJ
13052 /* Keep the old ``set remote Z-packet ...'' working. Each individual
13053 Z sub-packet has its own set and show commands, but users may
13054 have sets to this variable in their .gdbinit files (or in their
13055 documentation). */
e9e68a56 13056 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
13057 &remote_Z_packet_detect, _("\
13058Set use of remote protocol `Z' packets"), _("\
13059Show use of remote protocol `Z' packets "), _("\
3b64bf98 13060When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 13061packets."),
e9e68a56 13062 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
13063 show_remote_protocol_Z_packet_cmd,
13064 /* FIXME: i18n: Use of remote protocol
13065 `Z' packets is %s. */
e9e68a56 13066 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 13067
a6b151f1
DJ
13068 add_prefix_cmd ("remote", class_files, remote_command, _("\
13069Manipulate files on the remote system\n\
13070Transfer files to and from the remote target system."),
13071 &remote_cmdlist, "remote ",
13072 0 /* allow-unknown */, &cmdlist);
13073
13074 add_cmd ("put", class_files, remote_put_command,
13075 _("Copy a local file to the remote system."),
13076 &remote_cmdlist);
13077
13078 add_cmd ("get", class_files, remote_get_command,
13079 _("Copy a remote file to the local system."),
13080 &remote_cmdlist);
13081
13082 add_cmd ("delete", class_files, remote_delete_command,
13083 _("Delete a remote file."),
13084 &remote_cmdlist);
13085
2d717e4f
DJ
13086 remote_exec_file = xstrdup ("");
13087 add_setshow_string_noescape_cmd ("exec-file", class_files,
13088 &remote_exec_file, _("\
13089Set the remote pathname for \"run\""), _("\
13090Show the remote pathname for \"run\""), NULL, NULL, NULL,
13091 &remote_set_cmdlist, &remote_show_cmdlist);
13092
c1e36e3e
PA
13093 add_setshow_boolean_cmd ("range-stepping", class_run,
13094 &use_range_stepping, _("\
13095Enable or disable range stepping."), _("\
13096Show whether target-assisted range stepping is enabled."), _("\
13097If on, and the target supports it, when stepping a source line, GDB\n\
13098tells the target to step the corresponding range of addresses itself instead\n\
13099of issuing multiple single-steps. This speeds up source level\n\
13100stepping. If off, GDB always issues single-steps, even if range\n\
13101stepping is supported by the target. The default is on."),
13102 set_range_stepping,
13103 show_range_stepping,
13104 &setlist,
13105 &showlist);
13106
449092f6
CV
13107 /* Eventually initialize fileio. See fileio.c */
13108 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 13109
ba348170 13110 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 13111 special ptids with it set to != 0. */
ba348170
PA
13112 magic_null_ptid = ptid_build (42000, -1, 1);
13113 not_sent_ptid = ptid_build (42000, -2, 1);
13114 any_thread_ptid = ptid_build (42000, 0, 1);
35b1e5cc
SS
13115
13116 target_buf_size = 2048;
13117 target_buf = xmalloc (target_buf_size);
c906108c 13118}
10760264 13119
This page took 2.850908 seconds and 4 git commands to generate.