Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets in custom GDB protocol
8926118c 2
32d0add0 3 Copyright (C) 1988-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b
JM
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b
JM
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c5aa993b 19
23860348 20/* See the GDB User Guide for details of the GDB remote protocol. */
c5aa993b 21
c906108c 22#include "defs.h"
c906108c
SS
23#include <ctype.h>
24#include <fcntl.h>
c906108c 25#include "inferior.h"
45741a9c 26#include "infrun.h"
c906108c
SS
27#include "bfd.h"
28#include "symfile.h"
29#include "target.h"
c5aa993b 30/*#include "terminal.h" */
c906108c
SS
31#include "gdbcmd.h"
32#include "objfiles.h"
33#include "gdb-stabs.h"
34#include "gdbthread.h"
c2c6d25f 35#include "remote.h"
722247f1 36#include "remote-notif.h"
4e052eda 37#include "regcache.h"
fd0407d6 38#include "value.h"
6867ae3e 39#include "observer.h"
a77053c2 40#include "solib.h"
37a105a1
DJ
41#include "cli/cli-decode.h"
42#include "cli/cli-setshow.h"
424163ea 43#include "target-descriptions.h"
a4453b7e 44#include "gdb_bfd.h"
614c279d 45#include "filestuff.h"
9c3d6531 46#include "rsp-low.h"
c906108c 47
9846de1b 48#include <sys/time.h>
c906108c 49
43ff13b4 50#include "event-loop.h"
c2c6d25f 51#include "event-top.h"
2acceee2 52#include "inf-loop.h"
43ff13b4 53
c906108c
SS
54#include <signal.h>
55#include "serial.h"
56
6240bebf
MS
57#include "gdbcore.h" /* for exec_bfd */
58
449092f6 59#include "remote-fileio.h"
a6b151f1 60#include "gdb/fileio.h"
53ce3c39 61#include <sys/stat.h>
dc146f7c 62#include "xml-support.h"
449092f6 63
fd79ecee
DJ
64#include "memory-map.h"
65
35b1e5cc
SS
66#include "tracepoint.h"
67#include "ax.h"
68#include "ax-gdb.h"
d1feda86 69#include "agent.h"
9accd112 70#include "btrace.h"
35b1e5cc 71
0df8b418 72/* Temp hacks for tracepoint encoding migration. */
35b1e5cc
SS
73static char *target_buf;
74static long target_buf_size;
35b1e5cc 75
6765f3e5
DJ
76/* The size to align memory write packets, when practical. The protocol
77 does not guarantee any alignment, and gdb will generate short
78 writes and unaligned writes, but even as a best-effort attempt this
79 can improve bulk transfers. For instance, if a write is misaligned
80 relative to the target's data bus, the stub may need to make an extra
81 round trip fetching data from the target. This doesn't make a
82 huge difference, but it's easy to do, so we try to be helpful.
83
84 The alignment chosen is arbitrary; usually data bus width is
85 important here, not the possibly larger cache line size. */
86enum { REMOTE_ALIGN_WRITES = 16 };
87
23860348 88/* Prototypes for local functions. */
934b9bac 89static void async_cleanup_sigint_signal_handler (void *dummy);
6d820c5c 90static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
74531fed 91static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
fee9eda9 92 int forever, int *is_notif);
6426a772 93
934b9bac
JK
94static void async_handle_remote_sigint (int);
95static void async_handle_remote_sigint_twice (int);
43ff13b4 96
a14ed312 97static void remote_files_info (struct target_ops *ignore);
c906108c 98
f32dbf8c
MM
99static void remote_prepare_to_store (struct target_ops *self,
100 struct regcache *regcache);
c906108c 101
014f9477
TT
102static void remote_open_1 (const char *, int, struct target_ops *,
103 int extended_p);
c906108c 104
de90e03d 105static void remote_close (struct target_ops *self);
c906108c 106
cbb8991c
DB
107struct remote_state;
108
109static int remote_vkill (int pid, struct remote_state *rs);
110
136d6dae 111static void remote_mourn (struct target_ops *ops);
c906108c 112
a14ed312 113static void extended_remote_restart (void);
c906108c 114
136d6dae 115static void extended_remote_mourn (struct target_ops *);
c906108c 116
6d820c5c 117static void remote_send (char **buf, long *sizeof_buf_p);
c906108c 118
a14ed312 119static int readchar (int timeout);
c906108c 120
c33e31fd
PA
121static void remote_serial_write (const char *str, int len);
122
7d85a9c0 123static void remote_kill (struct target_ops *ops);
c906108c 124
6a109b6b 125static int remote_can_async_p (struct target_ops *);
75c99385 126
6a109b6b 127static int remote_is_async_p (struct target_ops *);
75c99385 128
6a3753b3 129static void remote_async (struct target_ops *ops, int enable);
75c99385 130
934b9bac 131static void sync_remote_interrupt_twice (int signo);
7a292a7a 132
a14ed312 133static void interrupt_query (void);
c906108c 134
79d7f229
PA
135static void set_general_thread (struct ptid ptid);
136static void set_continue_thread (struct ptid ptid);
c906108c 137
a14ed312 138static void get_offsets (void);
c906108c 139
6d820c5c
DJ
140static void skip_frame (void);
141
142static long read_frame (char **buf_p, long *sizeof_buf);
c906108c 143
a14ed312 144static int hexnumlen (ULONGEST num);
c906108c 145
a14ed312 146static void init_remote_ops (void);
c906108c 147
a14ed312 148static void init_extended_remote_ops (void);
c906108c 149
1eab8a48 150static void remote_stop (struct target_ops *self, ptid_t);
c906108c 151
a14ed312 152static int stubhex (int ch);
c906108c 153
a14ed312 154static int hexnumstr (char *, ULONGEST);
c906108c 155
a14ed312 156static int hexnumnstr (char *, ULONGEST, int);
2df3850c 157
a14ed312 158static CORE_ADDR remote_address_masked (CORE_ADDR);
c906108c 159
baa336ce 160static void print_packet (const char *);
c906108c 161
a14ed312 162static void compare_sections_command (char *, int);
c906108c 163
a14ed312 164static void packet_command (char *, int);
c906108c 165
a14ed312 166static int stub_unpack_int (char *buff, int fieldlength);
c906108c 167
39f77062 168static ptid_t remote_current_thread (ptid_t oldptid);
c906108c 169
baa336ce 170static int putpkt_binary (const char *buf, int cnt);
c906108c 171
a14ed312 172static void check_binary_download (CORE_ADDR addr);
c906108c 173
5a2468f5 174struct packet_config;
5a2468f5 175
a14ed312 176static void show_packet_config_cmd (struct packet_config *config);
5a2468f5 177
bb572ddd
DJ
178static void show_remote_protocol_packet_cmd (struct ui_file *file,
179 int from_tty,
180 struct cmd_list_element *c,
181 const char *value);
182
82f73884
PA
183static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
184static ptid_t read_ptid (char *buf, char **obuf);
185
c378d69d 186static void remote_set_permissions (struct target_ops *self);
d914c394 187
8bd200f1
TT
188static int remote_get_trace_status (struct target_ops *self,
189 struct trace_status *ts);
d5551862 190
ab6617cc
TT
191static int remote_upload_tracepoints (struct target_ops *self,
192 struct uploaded_tp **utpp);
00bf0b85 193
181e3713
TT
194static int remote_upload_trace_state_variables (struct target_ops *self,
195 struct uploaded_tsv **utsvp);
00bf0b85 196
c8d104ad
PA
197static void remote_query_supported (void);
198
36d25514 199static void remote_check_symbols (void);
c8d104ad 200
a14ed312 201void _initialize_remote (void);
c906108c 202
74531fed 203struct stop_reply;
74531fed 204static void stop_reply_xfree (struct stop_reply *);
722247f1 205static void remote_parse_stop_reply (char *, struct stop_reply *);
74531fed 206static void push_stop_reply (struct stop_reply *);
bcc75809 207static void discard_pending_stop_replies_in_queue (struct remote_state *);
74531fed
PA
208static int peek_stop_reply (ptid_t ptid);
209
cbb8991c
DB
210struct threads_listing_context;
211static void remove_new_fork_children (struct threads_listing_context *);
212
74531fed 213static void remote_async_inferior_event_handler (gdb_client_data);
74531fed 214
e3594fd1 215static void remote_terminal_ours (struct target_ops *self);
d3fd5342 216
d962ef82
DJ
217static int remote_read_description_p (struct target_ops *target);
218
176a6961 219static void remote_console_output (char *msg);
dde08ee1 220
efcc2da7 221static int remote_supports_cond_breakpoints (struct target_ops *self);
b775012e 222
78eff0ec 223static int remote_can_run_breakpoint_commands (struct target_ops *self);
d3ce09f5 224
f4abbc16
MM
225static void remote_btrace_reset (void);
226
a6b151f1
DJ
227/* For "remote". */
228
229static struct cmd_list_element *remote_cmdlist;
230
bb572ddd
DJ
231/* For "set remote" and "show remote". */
232
233static struct cmd_list_element *remote_set_cmdlist;
234static struct cmd_list_element *remote_show_cmdlist;
235
d458bd84
PA
236/* Stub vCont actions support.
237
238 Each field is a boolean flag indicating whether the stub reports
239 support for the corresponding action. */
240
241struct vCont_action_support
242{
243 /* vCont;t */
244 int t;
c1e36e3e
PA
245
246 /* vCont;r */
247 int r;
d458bd84
PA
248};
249
c1e36e3e
PA
250/* Controls whether GDB is willing to use range stepping. */
251
252static int use_range_stepping = 1;
253
0d031856
TT
254#define OPAQUETHREADBYTES 8
255
256/* a 64 bit opaque identifier */
257typedef unsigned char threadref[OPAQUETHREADBYTES];
258
259/* About this many threadisds fit in a packet. */
260
261#define MAXTHREADLISTRESULTS 32
262
ea9c271d
DJ
263/* Description of the remote protocol state for the currently
264 connected target. This is per-target state, and independent of the
265 selected architecture. */
266
267struct remote_state
268{
269 /* A buffer to use for incoming packets, and its current size. The
270 buffer is grown dynamically for larger incoming packets.
271 Outgoing packets may also be constructed in this buffer.
272 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
273 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
274 packets. */
275 char *buf;
276 long buf_size;
be2a5f71 277
1e51243a
PA
278 /* True if we're going through initial connection setup (finding out
279 about the remote side's threads, relocating symbols, etc.). */
280 int starting_up;
281
be2a5f71
DJ
282 /* If we negotiated packet size explicitly (and thus can bypass
283 heuristics for the largest packet size that will not overflow
284 a buffer in the stub), this will be set to that packet size.
285 Otherwise zero, meaning to use the guessed size. */
286 long explicit_packet_size;
2d717e4f
DJ
287
288 /* remote_wait is normally called when the target is running and
289 waits for a stop reply packet. But sometimes we need to call it
290 when the target is already stopped. We can send a "?" packet
291 and have remote_wait read the response. Or, if we already have
292 the response, we can stash it in BUF and tell remote_wait to
293 skip calling getpkt. This flag is set when BUF contains a
294 stop reply packet and the target is not waiting. */
295 int cached_wait_status;
a6f3e723
SL
296
297 /* True, if in no ack mode. That is, neither GDB nor the stub will
298 expect acks from each other. The connection is assumed to be
299 reliable. */
300 int noack_mode;
82f73884
PA
301
302 /* True if we're connected in extended remote mode. */
303 int extended;
304
e24a49d8
PA
305 /* True if we resumed the target and we're waiting for the target to
306 stop. In the mean time, we can't start another command/query.
307 The remote server wouldn't be ready to process it, so we'd
308 timeout waiting for a reply that would never come and eventually
309 we'd close the connection. This can happen in asynchronous mode
310 because we allow GDB commands while the target is running. */
311 int waiting_for_stop_reply;
74531fed 312
d458bd84
PA
313 /* The status of the stub support for the various vCont actions. */
314 struct vCont_action_support supports_vCont;
782b2b07 315
3a29589a
DJ
316 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
317 responded to that. */
318 int ctrlc_pending_p;
5d93a237
TT
319
320 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
321 remote_open knows that we don't have a file open when the program
322 starts. */
323 struct serial *remote_desc;
47f8a51d
TT
324
325 /* These are the threads which we last sent to the remote system. The
326 TID member will be -1 for all or -2 for not sent yet. */
327 ptid_t general_thread;
328 ptid_t continue_thread;
262e1174
TT
329
330 /* This is the traceframe which we last selected on the remote system.
331 It will be -1 if no traceframe is selected. */
332 int remote_traceframe_number;
747dc59d
TT
333
334 char *last_pass_packet;
5e4a05c4
TT
335
336 /* The last QProgramSignals packet sent to the target. We bypass
337 sending a new program signals list down to the target if the new
338 packet is exactly the same as the last we sent. IOW, we only let
339 the target know about program signals list changes. */
340 char *last_program_signals_packet;
b73be471
TT
341
342 enum gdb_signal last_sent_signal;
280ceea3
TT
343
344 int last_sent_step;
8e88304f
TT
345
346 char *finished_object;
347 char *finished_annex;
348 ULONGEST finished_offset;
b80fafe3
TT
349
350 /* Should we try the 'ThreadInfo' query packet?
351
352 This variable (NOT available to the user: auto-detect only!)
353 determines whether GDB will use the new, simpler "ThreadInfo"
354 query or the older, more complex syntax for thread queries.
355 This is an auto-detect variable (set to true at each connect,
356 and set to false when the target fails to recognize it). */
357 int use_threadinfo_query;
358 int use_threadextra_query;
88b496c3 359
ee154bee
TT
360 /* This is set to the data address of the access causing the target
361 to stop for a watchpoint. */
362 CORE_ADDR remote_watch_data_address;
363
f7e6eed5
PA
364 /* Whether the target stopped for a breakpoint/watchpoint. */
365 enum target_stop_reason stop_reason;
0d031856
TT
366
367 threadref echo_nextthread;
368 threadref nextthread;
369 threadref resultthreadlist[MAXTHREADLISTRESULTS];
5965e028
YQ
370
371 /* The state of remote notification. */
372 struct remote_notif_state *notif_state;
f4abbc16
MM
373
374 /* The branch trace configuration. */
375 struct btrace_config btrace_config;
15a201c8
GB
376
377 /* The argument to the last "vFile:setfs:" packet we sent, used
378 to avoid sending repeated unnecessary "vFile:setfs:" packets.
379 Initialized to -1 to indicate that no "vFile:setfs:" packet
380 has yet been sent. */
381 int fs_pid;
ea9c271d
DJ
382};
383
dc146f7c
VP
384/* Private data that we'll store in (struct thread_info)->private. */
385struct private_thread_info
386{
387 char *extra;
388 int core;
389};
390
391static void
392free_private_thread_info (struct private_thread_info *info)
393{
394 xfree (info->extra);
395 xfree (info);
396}
397
ea9c271d
DJ
398/* This data could be associated with a target, but we do not always
399 have access to the current target when we need it, so for now it is
400 static. This will be fine for as long as only one target is in use
401 at a time. */
cf792862 402static struct remote_state *remote_state;
ea9c271d
DJ
403
404static struct remote_state *
0b83947e 405get_remote_state_raw (void)
ea9c271d 406{
cf792862
TT
407 return remote_state;
408}
409
410/* Allocate a new struct remote_state with xmalloc, initialize it, and
411 return it. */
412
413static struct remote_state *
414new_remote_state (void)
415{
416 struct remote_state *result = XCNEW (struct remote_state);
417
418 /* The default buffer size is unimportant; it will be expanded
419 whenever a larger buffer is needed. */
420 result->buf_size = 400;
421 result->buf = xmalloc (result->buf_size);
262e1174 422 result->remote_traceframe_number = -1;
b73be471 423 result->last_sent_signal = GDB_SIGNAL_0;
15a201c8 424 result->fs_pid = -1;
cf792862
TT
425
426 return result;
ea9c271d
DJ
427}
428
429/* Description of the remote protocol for a given architecture. */
d01949b6 430
ad10f812
AC
431struct packet_reg
432{
433 long offset; /* Offset into G packet. */
434 long regnum; /* GDB's internal register number. */
435 LONGEST pnum; /* Remote protocol register number. */
b323314b 436 int in_g_packet; /* Always part of G packet. */
f5656ead 437 /* long size in bytes; == register_size (target_gdbarch (), regnum);
23860348 438 at present. */
f5656ead 439 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
c9f4d572 440 at present. */
ad10f812
AC
441};
442
ea9c271d 443struct remote_arch_state
d01949b6 444{
ad10f812
AC
445 /* Description of the remote protocol registers. */
446 long sizeof_g_packet;
b323314b
AC
447
448 /* Description of the remote protocol registers indexed by REGNUM
f57d151a 449 (making an array gdbarch_num_regs in size). */
b323314b 450 struct packet_reg *regs;
ad10f812 451
d01949b6
AC
452 /* This is the size (in chars) of the first response to the ``g''
453 packet. It is used as a heuristic when determining the maximum
454 size of memory-read and memory-write packets. A target will
455 typically only reserve a buffer large enough to hold the ``g''
456 packet. The size does not include packet overhead (headers and
23860348 457 trailers). */
d01949b6
AC
458 long actual_register_packet_size;
459
460 /* This is the maximum size (in chars) of a non read/write packet.
23860348 461 It is also used as a cap on the size of read/write packets. */
d01949b6
AC
462 long remote_packet_size;
463};
464
35b1e5cc
SS
465/* Utility: generate error from an incoming stub packet. */
466static void
467trace_error (char *buf)
468{
469 if (*buf++ != 'E')
470 return; /* not an error msg */
471 switch (*buf)
472 {
473 case '1': /* malformed packet error */
474 if (*++buf == '0') /* general case: */
475 error (_("remote.c: error in outgoing packet."));
476 else
477 error (_("remote.c: error in outgoing packet at field #%ld."),
478 strtol (buf, NULL, 16));
35b1e5cc
SS
479 default:
480 error (_("Target returns error code '%s'."), buf);
481 }
482}
483
484/* Utility: wait for reply from stub, while accepting "O" packets. */
485static char *
486remote_get_noisy_reply (char **buf_p,
487 long *sizeof_buf)
488{
489 do /* Loop on reply from remote stub. */
490 {
491 char *buf;
a744cf53 492
0df8b418 493 QUIT; /* Allow user to bail out with ^C. */
35b1e5cc
SS
494 getpkt (buf_p, sizeof_buf, 0);
495 buf = *buf_p;
ad91cd99 496 if (buf[0] == 'E')
35b1e5cc 497 trace_error (buf);
61012eef 498 else if (startswith (buf, "qRelocInsn:"))
dde08ee1
PA
499 {
500 ULONGEST ul;
501 CORE_ADDR from, to, org_to;
502 char *p, *pp;
503 int adjusted_size = 0;
7556d4a4 504 int relocated = 0;
dde08ee1
PA
505
506 p = buf + strlen ("qRelocInsn:");
507 pp = unpack_varlen_hex (p, &ul);
508 if (*pp != ';')
cb91c06a 509 error (_("invalid qRelocInsn packet: %s"), buf);
dde08ee1
PA
510 from = ul;
511
512 p = pp + 1;
a9cbf802 513 unpack_varlen_hex (p, &ul);
dde08ee1
PA
514 to = ul;
515
516 org_to = to;
517
492d29ea 518 TRY
dde08ee1 519 {
f5656ead 520 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
7556d4a4 521 relocated = 1;
dde08ee1 522 }
492d29ea 523 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
524 {
525 if (ex.error == MEMORY_ERROR)
526 {
527 /* Propagate memory errors silently back to the
528 target. The stub may have limited the range of
529 addresses we can write to, for example. */
530 }
531 else
532 {
533 /* Something unexpectedly bad happened. Be verbose
534 so we can tell what, and propagate the error back
535 to the stub, so it doesn't get stuck waiting for
536 a response. */
537 exception_fprintf (gdb_stderr, ex,
538 _("warning: relocating instruction: "));
539 }
540 putpkt ("E01");
541 }
492d29ea 542 END_CATCH
7556d4a4
PA
543
544 if (relocated)
dde08ee1
PA
545 {
546 adjusted_size = to - org_to;
547
bba74b36 548 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
dde08ee1
PA
549 putpkt (buf);
550 }
dde08ee1 551 }
ad91cd99 552 else if (buf[0] == 'O' && buf[1] != 'K')
35b1e5cc
SS
553 remote_console_output (buf + 1); /* 'O' message from stub */
554 else
0df8b418 555 return buf; /* Here's the actual reply. */
35b1e5cc
SS
556 }
557 while (1);
558}
3c3bea1c 559
d01949b6
AC
560/* Handle for retreving the remote protocol data from gdbarch. */
561static struct gdbarch_data *remote_gdbarch_data_handle;
562
ea9c271d
DJ
563static struct remote_arch_state *
564get_remote_arch_state (void)
d01949b6 565{
f5656ead 566 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
d01949b6
AC
567}
568
0b83947e
DJ
569/* Fetch the global remote target state. */
570
571static struct remote_state *
572get_remote_state (void)
573{
574 /* Make sure that the remote architecture state has been
575 initialized, because doing so might reallocate rs->buf. Any
576 function which calls getpkt also needs to be mindful of changes
577 to rs->buf, but this call limits the number of places which run
578 into trouble. */
579 get_remote_arch_state ();
580
581 return get_remote_state_raw ();
582}
583
74ca34ce
DJ
584static int
585compare_pnums (const void *lhs_, const void *rhs_)
586{
587 const struct packet_reg * const *lhs = lhs_;
588 const struct packet_reg * const *rhs = rhs_;
589
590 if ((*lhs)->pnum < (*rhs)->pnum)
591 return -1;
592 else if ((*lhs)->pnum == (*rhs)->pnum)
593 return 0;
594 else
595 return 1;
596}
597
c21236dc
PA
598static int
599map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
d01949b6 600{
74ca34ce 601 int regnum, num_remote_regs, offset;
74ca34ce 602 struct packet_reg **remote_regs;
ea9c271d 603
4a22f64d 604 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
ad10f812 605 {
c21236dc 606 struct packet_reg *r = &regs[regnum];
baef701f 607
4a22f64d 608 if (register_size (gdbarch, regnum) == 0)
baef701f
DJ
609 /* Do not try to fetch zero-sized (placeholder) registers. */
610 r->pnum = -1;
611 else
612 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
613
b323314b 614 r->regnum = regnum;
74ca34ce
DJ
615 }
616
617 /* Define the g/G packet format as the contents of each register
618 with a remote protocol number, in order of ascending protocol
619 number. */
620
4a22f64d 621 remote_regs = alloca (gdbarch_num_regs (gdbarch)
c21236dc 622 * sizeof (struct packet_reg *));
f57d151a 623 for (num_remote_regs = 0, regnum = 0;
4a22f64d 624 regnum < gdbarch_num_regs (gdbarch);
f57d151a 625 regnum++)
c21236dc
PA
626 if (regs[regnum].pnum != -1)
627 remote_regs[num_remote_regs++] = &regs[regnum];
7d58c67d 628
74ca34ce
DJ
629 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
630 compare_pnums);
631
632 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
633 {
634 remote_regs[regnum]->in_g_packet = 1;
635 remote_regs[regnum]->offset = offset;
4a22f64d 636 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
ad10f812
AC
637 }
638
c21236dc
PA
639 return offset;
640}
641
642/* Given the architecture described by GDBARCH, return the remote
643 protocol register's number and the register's offset in the g/G
644 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
645 If the target does not have a mapping for REGNUM, return false,
646 otherwise, return true. */
647
648int
649remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
650 int *pnum, int *poffset)
651{
652 int sizeof_g_packet;
653 struct packet_reg *regs;
654 struct cleanup *old_chain;
655
656 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
657
658 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
659 old_chain = make_cleanup (xfree, regs);
660
661 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
662
663 *pnum = regs[regnum].pnum;
664 *poffset = regs[regnum].offset;
665
666 do_cleanups (old_chain);
667
668 return *pnum != -1;
669}
670
671static void *
672init_remote_state (struct gdbarch *gdbarch)
673{
674 struct remote_state *rs = get_remote_state_raw ();
675 struct remote_arch_state *rsa;
676
677 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
678
679 /* Use the architecture to build a regnum<->pnum table, which will be
680 1:1 unless a feature set specifies otherwise. */
681 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
682 gdbarch_num_regs (gdbarch),
683 struct packet_reg);
684
74ca34ce
DJ
685 /* Record the maximum possible size of the g packet - it may turn out
686 to be smaller. */
c21236dc 687 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
74ca34ce 688
0df8b418 689 /* Default maximum number of characters in a packet body. Many
d01949b6
AC
690 remote stubs have a hardwired buffer size of 400 bytes
691 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
692 as the maximum packet-size to ensure that the packet and an extra
693 NUL character can always fit in the buffer. This stops GDB
694 trashing stubs that try to squeeze an extra NUL into what is
ea9c271d
DJ
695 already a full buffer (As of 1999-12-04 that was most stubs). */
696 rsa->remote_packet_size = 400 - 1;
d01949b6 697
ea9c271d
DJ
698 /* This one is filled in when a ``g'' packet is received. */
699 rsa->actual_register_packet_size = 0;
700
701 /* Should rsa->sizeof_g_packet needs more space than the
0df8b418
MS
702 default, adjust the size accordingly. Remember that each byte is
703 encoded as two characters. 32 is the overhead for the packet
704 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 705 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 706 little. */
ea9c271d
DJ
707 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
708 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
802188a7 709
ea9c271d
DJ
710 /* Make sure that the packet buffer is plenty big enough for
711 this architecture. */
712 if (rs->buf_size < rsa->remote_packet_size)
713 {
714 rs->buf_size = 2 * rsa->remote_packet_size;
7fca722e 715 rs->buf = xrealloc (rs->buf, rs->buf_size);
ea9c271d 716 }
6d820c5c 717
ea9c271d
DJ
718 return rsa;
719}
720
721/* Return the current allowed size of a remote packet. This is
722 inferred from the current architecture, and should be used to
723 limit the length of outgoing packets. */
724static long
725get_remote_packet_size (void)
726{
be2a5f71 727 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
728 struct remote_arch_state *rsa = get_remote_arch_state ();
729
be2a5f71
DJ
730 if (rs->explicit_packet_size)
731 return rs->explicit_packet_size;
732
ea9c271d 733 return rsa->remote_packet_size;
d01949b6
AC
734}
735
ad10f812 736static struct packet_reg *
ea9c271d 737packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
ad10f812 738{
f5656ead 739 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
b323314b
AC
740 return NULL;
741 else
ad10f812 742 {
ea9c271d 743 struct packet_reg *r = &rsa->regs[regnum];
a744cf53 744
b323314b
AC
745 gdb_assert (r->regnum == regnum);
746 return r;
ad10f812 747 }
ad10f812
AC
748}
749
750static struct packet_reg *
ea9c271d 751packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
ad10f812 752{
b323314b 753 int i;
a744cf53 754
f5656ead 755 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
ad10f812 756 {
ea9c271d 757 struct packet_reg *r = &rsa->regs[i];
a744cf53 758
b323314b
AC
759 if (r->pnum == pnum)
760 return r;
ad10f812
AC
761 }
762 return NULL;
d01949b6
AC
763}
764
c906108c
SS
765static struct target_ops remote_ops;
766
767static struct target_ops extended_remote_ops;
768
6426a772
JM
769/* FIXME: cagney/1999-09-23: Even though getpkt was called with
770 ``forever'' still use the normal timeout mechanism. This is
771 currently used by the ASYNC code to guarentee that target reads
772 during the initial connect always time-out. Once getpkt has been
773 modified to return a timeout indication and, in turn
774 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 775 this can go away. */
6426a772
JM
776static int wait_forever_enabled_p = 1;
777
9a7071a8
JB
778/* Allow the user to specify what sequence to send to the remote
779 when he requests a program interruption: Although ^C is usually
780 what remote systems expect (this is the default, here), it is
781 sometimes preferable to send a break. On other systems such
782 as the Linux kernel, a break followed by g, which is Magic SysRq g
783 is required in order to interrupt the execution. */
784const char interrupt_sequence_control_c[] = "Ctrl-C";
785const char interrupt_sequence_break[] = "BREAK";
786const char interrupt_sequence_break_g[] = "BREAK-g";
40478521 787static const char *const interrupt_sequence_modes[] =
9a7071a8
JB
788 {
789 interrupt_sequence_control_c,
790 interrupt_sequence_break,
791 interrupt_sequence_break_g,
792 NULL
793 };
794static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
795
796static void
797show_interrupt_sequence (struct ui_file *file, int from_tty,
798 struct cmd_list_element *c,
799 const char *value)
800{
801 if (interrupt_sequence_mode == interrupt_sequence_control_c)
802 fprintf_filtered (file,
803 _("Send the ASCII ETX character (Ctrl-c) "
804 "to the remote target to interrupt the "
805 "execution of the program.\n"));
806 else if (interrupt_sequence_mode == interrupt_sequence_break)
807 fprintf_filtered (file,
808 _("send a break signal to the remote target "
809 "to interrupt the execution of the program.\n"));
810 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
811 fprintf_filtered (file,
812 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
813 "the remote target to interrupt the execution "
814 "of Linux kernel.\n"));
815 else
816 internal_error (__FILE__, __LINE__,
817 _("Invalid value for interrupt_sequence_mode: %s."),
818 interrupt_sequence_mode);
819}
6426a772 820
9a7071a8
JB
821/* This boolean variable specifies whether interrupt_sequence is sent
822 to the remote target when gdb connects to it.
823 This is mostly needed when you debug the Linux kernel: The Linux kernel
824 expects BREAK g which is Magic SysRq g for connecting gdb. */
825static int interrupt_on_connect = 0;
c906108c 826
9a7071a8
JB
827/* This variable is used to implement the "set/show remotebreak" commands.
828 Since these commands are now deprecated in favor of "set/show remote
829 interrupt-sequence", it no longer has any effect on the code. */
c906108c
SS
830static int remote_break;
831
9a7071a8
JB
832static void
833set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
834{
835 if (remote_break)
836 interrupt_sequence_mode = interrupt_sequence_break;
837 else
838 interrupt_sequence_mode = interrupt_sequence_control_c;
839}
840
841static void
842show_remotebreak (struct ui_file *file, int from_tty,
843 struct cmd_list_element *c,
844 const char *value)
845{
846}
847
c906108c
SS
848/* This variable sets the number of bits in an address that are to be
849 sent in a memory ("M" or "m") packet. Normally, after stripping
0df8b418 850 leading zeros, the entire address would be sent. This variable
c906108c
SS
851 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
852 initial implementation of remote.c restricted the address sent in
853 memory packets to ``host::sizeof long'' bytes - (typically 32
854 bits). Consequently, for 64 bit targets, the upper 32 bits of an
855 address was never sent. Since fixing this bug may cause a break in
856 some remote targets this variable is principly provided to
23860348 857 facilitate backward compatibility. */
c906108c 858
883b9c6c 859static unsigned int remote_address_size;
c906108c 860
75c99385
PA
861/* Temporary to track who currently owns the terminal. See
862 remote_terminal_* for more details. */
6426a772
JM
863
864static int remote_async_terminal_ours_p;
865
2d717e4f
DJ
866/* The executable file to use for "run" on the remote side. */
867
868static char *remote_exec_file = "";
869
11cf8741 870\f
11cf8741 871/* User configurable variables for the number of characters in a
ea9c271d
DJ
872 memory read/write packet. MIN (rsa->remote_packet_size,
873 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 874 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
875 (speed up transfers). The variables ``preferred_*'' (the user
876 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 877 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
878
879struct memory_packet_config
880{
881 char *name;
882 long size;
883 int fixed_p;
884};
885
886/* Compute the current size of a read/write packet. Since this makes
887 use of ``actual_register_packet_size'' the computation is dynamic. */
888
889static long
890get_memory_packet_size (struct memory_packet_config *config)
891{
d01949b6 892 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
893 struct remote_arch_state *rsa = get_remote_arch_state ();
894
11cf8741
JM
895 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
896 law?) that some hosts don't cope very well with large alloca()
897 calls. Eventually the alloca() code will be replaced by calls to
898 xmalloc() and make_cleanups() allowing this restriction to either
23860348 899 be lifted or removed. */
11cf8741
JM
900#ifndef MAX_REMOTE_PACKET_SIZE
901#define MAX_REMOTE_PACKET_SIZE 16384
902#endif
3de11b2e 903 /* NOTE: 20 ensures we can write at least one byte. */
11cf8741 904#ifndef MIN_REMOTE_PACKET_SIZE
3de11b2e 905#define MIN_REMOTE_PACKET_SIZE 20
11cf8741
JM
906#endif
907 long what_they_get;
908 if (config->fixed_p)
909 {
910 if (config->size <= 0)
911 what_they_get = MAX_REMOTE_PACKET_SIZE;
912 else
913 what_they_get = config->size;
914 }
915 else
916 {
ea9c271d 917 what_they_get = get_remote_packet_size ();
23860348 918 /* Limit the packet to the size specified by the user. */
11cf8741
JM
919 if (config->size > 0
920 && what_they_get > config->size)
921 what_they_get = config->size;
be2a5f71
DJ
922
923 /* Limit it to the size of the targets ``g'' response unless we have
924 permission from the stub to use a larger packet size. */
925 if (rs->explicit_packet_size == 0
926 && rsa->actual_register_packet_size > 0
927 && what_they_get > rsa->actual_register_packet_size)
928 what_they_get = rsa->actual_register_packet_size;
11cf8741
JM
929 }
930 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
931 what_they_get = MAX_REMOTE_PACKET_SIZE;
932 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
933 what_they_get = MIN_REMOTE_PACKET_SIZE;
6d820c5c
DJ
934
935 /* Make sure there is room in the global buffer for this packet
936 (including its trailing NUL byte). */
937 if (rs->buf_size < what_they_get + 1)
938 {
939 rs->buf_size = 2 * what_they_get;
940 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
941 }
942
11cf8741
JM
943 return what_they_get;
944}
945
0df8b418 946/* Update the size of a read/write packet. If they user wants
23860348 947 something really big then do a sanity check. */
11cf8741
JM
948
949static void
950set_memory_packet_size (char *args, struct memory_packet_config *config)
951{
952 int fixed_p = config->fixed_p;
953 long size = config->size;
a744cf53 954
11cf8741 955 if (args == NULL)
8a3fe4f8 956 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
957 else if (strcmp (args, "hard") == 0
958 || strcmp (args, "fixed") == 0)
959 fixed_p = 1;
960 else if (strcmp (args, "soft") == 0
961 || strcmp (args, "limit") == 0)
962 fixed_p = 0;
963 else
964 {
965 char *end;
a744cf53 966
11cf8741
JM
967 size = strtoul (args, &end, 0);
968 if (args == end)
8a3fe4f8 969 error (_("Invalid %s (bad syntax)."), config->name);
11cf8741
JM
970#if 0
971 /* Instead of explicitly capping the size of a packet to
972 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
973 instead allowed to set the size to something arbitrarily
23860348 974 large. */
11cf8741 975 if (size > MAX_REMOTE_PACKET_SIZE)
8a3fe4f8 976 error (_("Invalid %s (too large)."), config->name);
11cf8741
JM
977#endif
978 }
23860348 979 /* Extra checks? */
11cf8741
JM
980 if (fixed_p && !config->fixed_p)
981 {
e2e0b3e5
AC
982 if (! query (_("The target may not be able to correctly handle a %s\n"
983 "of %ld bytes. Change the packet size? "),
11cf8741 984 config->name, size))
8a3fe4f8 985 error (_("Packet size not changed."));
11cf8741 986 }
23860348 987 /* Update the config. */
11cf8741
JM
988 config->fixed_p = fixed_p;
989 config->size = size;
990}
991
992static void
993show_memory_packet_size (struct memory_packet_config *config)
994{
a3f17187 995 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 996 if (config->fixed_p)
a3f17187 997 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
998 get_memory_packet_size (config));
999 else
a3f17187 1000 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
1001 get_memory_packet_size (config));
1002}
1003
1004static struct memory_packet_config memory_write_packet_config =
1005{
1006 "memory-write-packet-size",
1007};
1008
1009static void
1010set_memory_write_packet_size (char *args, int from_tty)
1011{
1012 set_memory_packet_size (args, &memory_write_packet_config);
1013}
1014
1015static void
1016show_memory_write_packet_size (char *args, int from_tty)
1017{
1018 show_memory_packet_size (&memory_write_packet_config);
1019}
1020
1021static long
1022get_memory_write_packet_size (void)
1023{
1024 return get_memory_packet_size (&memory_write_packet_config);
1025}
1026
1027static struct memory_packet_config memory_read_packet_config =
1028{
1029 "memory-read-packet-size",
1030};
1031
1032static void
1033set_memory_read_packet_size (char *args, int from_tty)
1034{
1035 set_memory_packet_size (args, &memory_read_packet_config);
1036}
1037
1038static void
1039show_memory_read_packet_size (char *args, int from_tty)
1040{
1041 show_memory_packet_size (&memory_read_packet_config);
1042}
1043
1044static long
1045get_memory_read_packet_size (void)
1046{
1047 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1048
11cf8741
JM
1049 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1050 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1051 increased beyond this. */
1052 if (size > get_remote_packet_size ())
1053 size = get_remote_packet_size ();
11cf8741
JM
1054 return size;
1055}
1056
11cf8741 1057\f
5a2468f5 1058/* Generic configuration support for packets the stub optionally
0df8b418 1059 supports. Allows the user to specify the use of the packet as well
23860348 1060 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1061
1062enum packet_support
1063 {
1064 PACKET_SUPPORT_UNKNOWN = 0,
1065 PACKET_ENABLE,
1066 PACKET_DISABLE
1067 };
1068
5a2468f5
JM
1069struct packet_config
1070 {
bb572ddd
DJ
1071 const char *name;
1072 const char *title;
4082afcc
PA
1073
1074 /* If auto, GDB auto-detects support for this packet or feature,
1075 either through qSupported, or by trying the packet and looking
1076 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1077 packet. If false, the packet is disabled. Configs that don't
1078 have an associated command always have this set to auto. */
7f19b9a2 1079 enum auto_boolean detect;
4082afcc
PA
1080
1081 /* Does the target support this packet? */
5a2468f5
JM
1082 enum packet_support support;
1083 };
1084
d471ea57 1085/* Analyze a packet's return value and update the packet config
23860348 1086 accordingly. */
d471ea57
AC
1087
1088enum packet_result
1089{
1090 PACKET_ERROR,
1091 PACKET_OK,
1092 PACKET_UNKNOWN
1093};
1094
4082afcc
PA
1095static enum packet_support packet_config_support (struct packet_config *config);
1096static enum packet_support packet_support (int packet);
5a2468f5
JM
1097
1098static void
fba45db2 1099show_packet_config_cmd (struct packet_config *config)
5a2468f5
JM
1100{
1101 char *support = "internal-error";
a744cf53 1102
4082afcc 1103 switch (packet_config_support (config))
5a2468f5
JM
1104 {
1105 case PACKET_ENABLE:
1106 support = "enabled";
1107 break;
1108 case PACKET_DISABLE:
1109 support = "disabled";
1110 break;
1111 case PACKET_SUPPORT_UNKNOWN:
1112 support = "unknown";
1113 break;
1114 }
1115 switch (config->detect)
1116 {
7f19b9a2 1117 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1118 printf_filtered (_("Support for the `%s' packet "
1119 "is auto-detected, currently %s.\n"),
37a105a1 1120 config->name, support);
5a2468f5 1121 break;
7f19b9a2
AC
1122 case AUTO_BOOLEAN_TRUE:
1123 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1124 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1125 config->name, support);
8e248173 1126 break;
5a2468f5
JM
1127 }
1128}
1129
1130static void
bb572ddd
DJ
1131add_packet_config_cmd (struct packet_config *config, const char *name,
1132 const char *title, int legacy)
d471ea57 1133{
5a2468f5
JM
1134 char *set_doc;
1135 char *show_doc;
d471ea57 1136 char *cmd_name;
3ed07be4 1137
5a2468f5
JM
1138 config->name = name;
1139 config->title = title;
b435e160
AC
1140 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1141 name, title);
3e43a32a
MS
1142 show_doc = xstrprintf ("Show current use of remote "
1143 "protocol `%s' (%s) packet",
b435e160 1144 name, title);
d471ea57 1145 /* set/show TITLE-packet {auto,on,off} */
b435e160 1146 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1147 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1148 &config->detect, set_doc,
1149 show_doc, NULL, /* help_doc */
4082afcc 1150 NULL,
bb572ddd
DJ
1151 show_remote_protocol_packet_cmd,
1152 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1153 /* The command code copies the documentation strings. */
1154 xfree (set_doc);
1155 xfree (show_doc);
23860348 1156 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1157 if (legacy)
1158 {
1159 char *legacy_name;
a744cf53 1160
b435e160 1161 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1162 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1163 &remote_set_cmdlist);
d471ea57 1164 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1165 &remote_show_cmdlist);
d471ea57 1166 }
5a2468f5
JM
1167}
1168
d471ea57 1169static enum packet_result
a76d924d 1170packet_check_result (const char *buf)
5a2468f5 1171{
d471ea57 1172 if (buf[0] != '\0')
5a2468f5 1173 {
d471ea57 1174 /* The stub recognized the packet request. Check that the
23860348 1175 operation succeeded. */
a76d924d
DJ
1176 if (buf[0] == 'E'
1177 && isxdigit (buf[1]) && isxdigit (buf[2])
1178 && buf[3] == '\0')
1179 /* "Enn" - definitly an error. */
1180 return PACKET_ERROR;
1181
1182 /* Always treat "E." as an error. This will be used for
1183 more verbose error messages, such as E.memtypes. */
1184 if (buf[0] == 'E' && buf[1] == '.')
1185 return PACKET_ERROR;
1186
1187 /* The packet may or may not be OK. Just assume it is. */
1188 return PACKET_OK;
1189 }
1190 else
1191 /* The stub does not support the packet. */
1192 return PACKET_UNKNOWN;
1193}
1194
1195static enum packet_result
1196packet_ok (const char *buf, struct packet_config *config)
1197{
1198 enum packet_result result;
1199
4082afcc
PA
1200 if (config->detect != AUTO_BOOLEAN_TRUE
1201 && config->support == PACKET_DISABLE)
1202 internal_error (__FILE__, __LINE__,
1203 _("packet_ok: attempt to use a disabled packet"));
1204
a76d924d
DJ
1205 result = packet_check_result (buf);
1206 switch (result)
1207 {
1208 case PACKET_OK:
1209 case PACKET_ERROR:
1210 /* The stub recognized the packet request. */
4082afcc 1211 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1212 {
d471ea57
AC
1213 if (remote_debug)
1214 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1215 "Packet %s (%s) is supported\n",
1216 config->name, config->title);
d471ea57 1217 config->support = PACKET_ENABLE;
d471ea57 1218 }
a76d924d
DJ
1219 break;
1220 case PACKET_UNKNOWN:
23860348 1221 /* The stub does not support the packet. */
4082afcc
PA
1222 if (config->detect == AUTO_BOOLEAN_AUTO
1223 && config->support == PACKET_ENABLE)
d471ea57 1224 {
4082afcc
PA
1225 /* If the stub previously indicated that the packet was
1226 supported then there is a protocol error. */
1227 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1228 config->name, config->title);
1229 }
1230 else if (config->detect == AUTO_BOOLEAN_TRUE)
1231 {
1232 /* The user set it wrong. */
1233 error (_("Enabled packet %s (%s) not recognized by stub"),
1234 config->name, config->title);
d471ea57 1235 }
4082afcc
PA
1236
1237 if (remote_debug)
1238 fprintf_unfiltered (gdb_stdlog,
1239 "Packet %s (%s) is NOT supported\n",
1240 config->name, config->title);
1241 config->support = PACKET_DISABLE;
a76d924d 1242 break;
5a2468f5 1243 }
a76d924d
DJ
1244
1245 return result;
5a2468f5
JM
1246}
1247
444abaca
DJ
1248enum {
1249 PACKET_vCont = 0,
1250 PACKET_X,
1251 PACKET_qSymbol,
1252 PACKET_P,
1253 PACKET_p,
1254 PACKET_Z0,
1255 PACKET_Z1,
1256 PACKET_Z2,
1257 PACKET_Z3,
1258 PACKET_Z4,
15a201c8 1259 PACKET_vFile_setfs,
a6b151f1
DJ
1260 PACKET_vFile_open,
1261 PACKET_vFile_pread,
1262 PACKET_vFile_pwrite,
1263 PACKET_vFile_close,
1264 PACKET_vFile_unlink,
b9e7b9c3 1265 PACKET_vFile_readlink,
0a93529c 1266 PACKET_vFile_fstat,
0876f84a 1267 PACKET_qXfer_auxv,
23181151 1268 PACKET_qXfer_features,
c78fa86a 1269 PACKET_qXfer_exec_file,
cfa9d6d9 1270 PACKET_qXfer_libraries,
2268b414 1271 PACKET_qXfer_libraries_svr4,
fd79ecee 1272 PACKET_qXfer_memory_map,
0e7f50da
UW
1273 PACKET_qXfer_spu_read,
1274 PACKET_qXfer_spu_write,
07e059b5 1275 PACKET_qXfer_osdata,
dc146f7c 1276 PACKET_qXfer_threads,
0fb4aa4b 1277 PACKET_qXfer_statictrace_read,
b3b9301e 1278 PACKET_qXfer_traceframe_info,
169081d0 1279 PACKET_qXfer_uib,
711e434b 1280 PACKET_qGetTIBAddr,
444abaca 1281 PACKET_qGetTLSAddr,
be2a5f71 1282 PACKET_qSupported,
bd3eecc3 1283 PACKET_qTStatus,
89be2091 1284 PACKET_QPassSignals,
9b224c5e 1285 PACKET_QProgramSignals,
936d2992 1286 PACKET_qCRC,
08388c79 1287 PACKET_qSearch_memory,
2d717e4f
DJ
1288 PACKET_vAttach,
1289 PACKET_vRun,
a6f3e723 1290 PACKET_QStartNoAckMode,
82f73884 1291 PACKET_vKill,
4aa995e1
PA
1292 PACKET_qXfer_siginfo_read,
1293 PACKET_qXfer_siginfo_write,
0b16c5cf 1294 PACKET_qAttached,
4082afcc
PA
1295
1296 /* Support for conditional tracepoints. */
782b2b07 1297 PACKET_ConditionalTracepoints,
4082afcc
PA
1298
1299 /* Support for target-side breakpoint conditions. */
3788aec7 1300 PACKET_ConditionalBreakpoints,
4082afcc
PA
1301
1302 /* Support for target-side breakpoint commands. */
d3ce09f5 1303 PACKET_BreakpointCommands,
4082afcc
PA
1304
1305 /* Support for fast tracepoints. */
7a697b8d 1306 PACKET_FastTracepoints,
4082afcc
PA
1307
1308 /* Support for static tracepoints. */
0fb4aa4b 1309 PACKET_StaticTracepoints,
4082afcc
PA
1310
1311 /* Support for installing tracepoints while a trace experiment is
1312 running. */
1e4d1764 1313 PACKET_InstallInTrace,
4082afcc 1314
40ab02ce
MS
1315 PACKET_bc,
1316 PACKET_bs,
409873ef 1317 PACKET_TracepointSource,
d914c394 1318 PACKET_QAllow,
78d85199 1319 PACKET_qXfer_fdpic,
03583c20 1320 PACKET_QDisableRandomization,
d1feda86 1321 PACKET_QAgent,
f6f899bf 1322 PACKET_QTBuffer_size,
9accd112
MM
1323 PACKET_Qbtrace_off,
1324 PACKET_Qbtrace_bts,
b20a6524 1325 PACKET_Qbtrace_pt,
9accd112 1326 PACKET_qXfer_btrace,
4082afcc
PA
1327
1328 /* Support for the QNonStop packet. */
1329 PACKET_QNonStop,
1330
1331 /* Support for multi-process extensions. */
1332 PACKET_multiprocess_feature,
1333
1334 /* Support for enabling and disabling tracepoints while a trace
1335 experiment is running. */
1336 PACKET_EnableDisableTracepoints_feature,
1337
1338 /* Support for collecting strings using the tracenz bytecode. */
1339 PACKET_tracenz_feature,
1340
1341 /* Support for continuing to run a trace experiment while GDB is
1342 disconnected. */
1343 PACKET_DisconnectedTracing_feature,
1344
1345 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1346 PACKET_augmented_libraries_svr4_read_feature,
1347
f4abbc16
MM
1348 /* Support for the qXfer:btrace-conf:read packet. */
1349 PACKET_qXfer_btrace_conf,
1350
d33501a5
MM
1351 /* Support for the Qbtrace-conf:bts:size packet. */
1352 PACKET_Qbtrace_conf_bts_size,
1353
f7e6eed5
PA
1354 /* Support for swbreak+ feature. */
1355 PACKET_swbreak_feature,
1356
1357 /* Support for hwbreak+ feature. */
1358 PACKET_hwbreak_feature,
1359
89245bc0
DB
1360 /* Support for fork events. */
1361 PACKET_fork_event_feature,
1362
1363 /* Support for vfork events. */
1364 PACKET_vfork_event_feature,
1365
b20a6524
MM
1366 /* Support for the Qbtrace-conf:pt:size packet. */
1367 PACKET_Qbtrace_conf_pt_size,
1368
444abaca
DJ
1369 PACKET_MAX
1370};
506fb367 1371
444abaca 1372static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1373
f7e6eed5
PA
1374/* Returns the packet's corresponding "set remote foo-packet" command
1375 state. See struct packet_config for more details. */
1376
1377static enum auto_boolean
1378packet_set_cmd_state (int packet)
1379{
1380 return remote_protocol_packets[packet].detect;
1381}
1382
4082afcc
PA
1383/* Returns whether a given packet or feature is supported. This takes
1384 into account the state of the corresponding "set remote foo-packet"
1385 command, which may be used to bypass auto-detection. */
dc8acb97 1386
4082afcc
PA
1387static enum packet_support
1388packet_config_support (struct packet_config *config)
1389{
1390 switch (config->detect)
444abaca 1391 {
4082afcc
PA
1392 case AUTO_BOOLEAN_TRUE:
1393 return PACKET_ENABLE;
1394 case AUTO_BOOLEAN_FALSE:
1395 return PACKET_DISABLE;
1396 case AUTO_BOOLEAN_AUTO:
1397 return config->support;
1398 default:
1399 gdb_assert_not_reached (_("bad switch"));
444abaca 1400 }
4082afcc
PA
1401}
1402
1403/* Same as packet_config_support, but takes the packet's enum value as
1404 argument. */
1405
1406static enum packet_support
1407packet_support (int packet)
1408{
1409 struct packet_config *config = &remote_protocol_packets[packet];
1410
1411 return packet_config_support (config);
dc8acb97
MS
1412}
1413
5a2468f5 1414static void
444abaca
DJ
1415show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1416 struct cmd_list_element *c,
1417 const char *value)
5a2468f5 1418{
444abaca 1419 struct packet_config *packet;
5a2468f5 1420
444abaca
DJ
1421 for (packet = remote_protocol_packets;
1422 packet < &remote_protocol_packets[PACKET_MAX];
1423 packet++)
1424 {
1425 if (&packet->detect == c->var)
1426 {
1427 show_packet_config_cmd (packet);
1428 return;
1429 }
1430 }
9b20d036 1431 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1432 c->name);
5a2468f5
JM
1433}
1434
d471ea57
AC
1435/* Should we try one of the 'Z' requests? */
1436
1437enum Z_packet_type
1438{
1439 Z_PACKET_SOFTWARE_BP,
1440 Z_PACKET_HARDWARE_BP,
1441 Z_PACKET_WRITE_WP,
1442 Z_PACKET_READ_WP,
1443 Z_PACKET_ACCESS_WP,
1444 NR_Z_PACKET_TYPES
1445};
96baa820 1446
d471ea57 1447/* For compatibility with older distributions. Provide a ``set remote
23860348 1448 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1449
7f19b9a2 1450static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1451
1452static void
fba45db2
KB
1453set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1454 struct cmd_list_element *c)
96baa820 1455{
d471ea57 1456 int i;
a744cf53 1457
d471ea57 1458 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1459 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1460}
1461
1462static void
08546159
AC
1463show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1464 struct cmd_list_element *c,
1465 const char *value)
96baa820 1466{
d471ea57 1467 int i;
a744cf53 1468
d471ea57
AC
1469 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1470 {
444abaca 1471 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1472 }
96baa820
JM
1473}
1474
4082afcc
PA
1475/* Returns true if the multi-process extensions are in effect. */
1476
1477static int
1478remote_multi_process_p (struct remote_state *rs)
1479{
1480 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1481}
1482
de0d863e
DB
1483/* Returns true if fork events are supported. */
1484
1485static int
1486remote_fork_event_p (struct remote_state *rs)
1487{
1488 return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
1489}
1490
c269dbdb
DB
1491/* Returns true if vfork events are supported. */
1492
1493static int
1494remote_vfork_event_p (struct remote_state *rs)
1495{
1496 return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
1497}
1498
cbb8991c
DB
1499/* Insert fork catchpoint target routine. If fork events are enabled
1500 then return success, nothing more to do. */
1501
1502static int
1503remote_insert_fork_catchpoint (struct target_ops *ops, int pid)
1504{
1505 struct remote_state *rs = get_remote_state ();
1506
1507 return !remote_fork_event_p (rs);
1508}
1509
1510/* Remove fork catchpoint target routine. Nothing to do, just
1511 return success. */
1512
1513static int
1514remote_remove_fork_catchpoint (struct target_ops *ops, int pid)
1515{
1516 return 0;
1517}
1518
1519/* Insert vfork catchpoint target routine. If vfork events are enabled
1520 then return success, nothing more to do. */
1521
1522static int
1523remote_insert_vfork_catchpoint (struct target_ops *ops, int pid)
1524{
1525 struct remote_state *rs = get_remote_state ();
1526
1527 return !remote_vfork_event_p (rs);
1528}
1529
1530/* Remove vfork catchpoint target routine. Nothing to do, just
1531 return success. */
1532
1533static int
1534remote_remove_vfork_catchpoint (struct target_ops *ops, int pid)
1535{
1536 return 0;
1537}
1538
23860348 1539/* Tokens for use by the asynchronous signal handlers for SIGINT. */
934b9bac
JK
1540static struct async_signal_handler *async_sigint_remote_twice_token;
1541static struct async_signal_handler *async_sigint_remote_token;
43ff13b4 1542
74531fed
PA
1543\f
1544/* Asynchronous signal handle registered as event loop source for
1545 when we have pending events ready to be passed to the core. */
1546
1547static struct async_event_handler *remote_async_inferior_event_token;
1548
c906108c
SS
1549\f
1550
79d7f229
PA
1551static ptid_t magic_null_ptid;
1552static ptid_t not_sent_ptid;
1553static ptid_t any_thread_ptid;
1554
0b16c5cf
PA
1555/* Find out if the stub attached to PID (and hence GDB should offer to
1556 detach instead of killing it when bailing out). */
1557
1558static int
1559remote_query_attached (int pid)
1560{
1561 struct remote_state *rs = get_remote_state ();
bba74b36 1562 size_t size = get_remote_packet_size ();
0b16c5cf 1563
4082afcc 1564 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
1565 return 0;
1566
1567 if (remote_multi_process_p (rs))
bba74b36 1568 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 1569 else
bba74b36 1570 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
1571
1572 putpkt (rs->buf);
1573 getpkt (&rs->buf, &rs->buf_size, 0);
1574
1575 switch (packet_ok (rs->buf,
1554e9be 1576 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
1577 {
1578 case PACKET_OK:
1579 if (strcmp (rs->buf, "1") == 0)
1580 return 1;
1581 break;
1582 case PACKET_ERROR:
1583 warning (_("Remote failure reply: %s"), rs->buf);
1584 break;
1585 case PACKET_UNKNOWN:
1586 break;
1587 }
1588
1589 return 0;
1590}
1591
49c62f2e
PA
1592/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1593 has been invented by GDB, instead of reported by the target. Since
1594 we can be connected to a remote system before before knowing about
1595 any inferior, mark the target with execution when we find the first
1596 inferior. If ATTACHED is 1, then we had just attached to this
1597 inferior. If it is 0, then we just created this inferior. If it
1598 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
1599 attached to the inferior or not. If TRY_OPEN_EXEC is true then
1600 attempt to open this inferior's executable as the main executable
1601 if no main executable is open already. */
1941c569
PA
1602
1603static struct inferior *
1b6e6f5c
GB
1604remote_add_inferior (int fake_pid_p, int pid, int attached,
1605 int try_open_exec)
1941c569 1606{
1941c569
PA
1607 struct inferior *inf;
1608
0b16c5cf
PA
1609 /* Check whether this process we're learning about is to be
1610 considered attached, or if is to be considered to have been
1611 spawned by the stub. */
1612 if (attached == -1)
1613 attached = remote_query_attached (pid);
1614
f5656ead 1615 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
1616 {
1617 /* If the target shares code across all inferiors, then every
1618 attach adds a new inferior. */
1619 inf = add_inferior (pid);
1620
1621 /* ... and every inferior is bound to the same program space.
1622 However, each inferior may still have its own address
1623 space. */
1624 inf->aspace = maybe_new_address_space ();
1625 inf->pspace = current_program_space;
1626 }
1627 else
1628 {
1629 /* In the traditional debugging scenario, there's a 1-1 match
1630 between program/address spaces. We simply bind the inferior
1631 to the program space's address space. */
1632 inf = current_inferior ();
1633 inferior_appeared (inf, pid);
1634 }
1941c569 1635
0b16c5cf 1636 inf->attach_flag = attached;
49c62f2e 1637 inf->fake_pid_p = fake_pid_p;
0b16c5cf 1638
1b6e6f5c
GB
1639 /* If no main executable is currently open then attempt to
1640 open the file that was executed to create this inferior. */
835205d0 1641 if (try_open_exec && get_exec_file (0) == NULL)
1b6e6f5c
GB
1642 exec_file_locate_attach (pid, 1);
1643
1941c569
PA
1644 return inf;
1645}
1646
1647/* Add thread PTID to GDB's thread list. Tag it as executing/running
1648 according to RUNNING. */
1649
c906108c 1650static void
1941c569 1651remote_add_thread (ptid_t ptid, int running)
c906108c 1652{
b7ea362b
PA
1653 struct remote_state *rs = get_remote_state ();
1654
1655 /* GDB historically didn't pull threads in the initial connection
1656 setup. If the remote target doesn't even have a concept of
1657 threads (e.g., a bare-metal target), even if internally we
1658 consider that a single-threaded target, mentioning a new thread
1659 might be confusing to the user. Be silent then, preserving the
1660 age old behavior. */
1661 if (rs->starting_up)
1662 add_thread_silent (ptid);
1663 else
1664 add_thread (ptid);
1941c569
PA
1665
1666 set_executing (ptid, running);
1667 set_running (ptid, running);
1668}
1669
1670/* Come here when we learn about a thread id from the remote target.
1671 It may be the first time we hear about such thread, so take the
1672 opportunity to add it to GDB's thread list. In case this is the
1673 first time we're noticing its corresponding inferior, add it to
1674 GDB's inferior list as well. */
1675
1676static void
1677remote_notice_new_inferior (ptid_t currthread, int running)
1678{
c906108c
SS
1679 /* If this is a new thread, add it to GDB's thread list.
1680 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
1681
1682 if (in_thread_list (currthread) && is_exited (currthread))
1683 {
1684 /* We're seeing an event on a thread id we knew had exited.
1685 This has to be a new thread reusing the old id. Add it. */
1941c569 1686 remote_add_thread (currthread, running);
82f73884
PA
1687 return;
1688 }
1689
79d7f229 1690 if (!in_thread_list (currthread))
c0a2216e 1691 {
1941c569 1692 struct inferior *inf = NULL;
bad34192 1693 int pid = ptid_get_pid (currthread);
1941c569 1694
bad34192
PA
1695 if (ptid_is_pid (inferior_ptid)
1696 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
1697 {
1698 /* inferior_ptid has no thread member yet. This can happen
1699 with the vAttach -> remote_wait,"TAAthread:" path if the
1700 stub doesn't support qC. This is the first stop reported
1701 after an attach, so this is the main thread. Update the
1702 ptid in the thread list. */
bad34192
PA
1703 if (in_thread_list (pid_to_ptid (pid)))
1704 thread_change_ptid (inferior_ptid, currthread);
1705 else
1706 {
1707 remote_add_thread (currthread, running);
1708 inferior_ptid = currthread;
1709 }
dc146f7c 1710 return;
c0a2216e 1711 }
82f73884
PA
1712
1713 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
1714 {
1715 /* inferior_ptid is not set yet. This can happen with the
1716 vRun -> remote_wait,"TAAthread:" path if the stub
1717 doesn't support qC. This is the first stop reported
1718 after an attach, so this is the main thread. Update the
1719 ptid in the thread list. */
dc146f7c 1720 thread_change_ptid (inferior_ptid, currthread);
82f73884 1721 return;
c0a2216e 1722 }
82f73884 1723
29c87f7f
PA
1724 /* When connecting to a target remote, or to a target
1725 extended-remote which already was debugging an inferior, we
1726 may not know about it yet. Add it before adding its child
1727 thread, so notifications are emitted in a sensible order. */
1728 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
1729 {
1730 struct remote_state *rs = get_remote_state ();
1731 int fake_pid_p = !remote_multi_process_p (rs);
1732
1733 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 1734 ptid_get_pid (currthread), -1, 1);
49c62f2e 1735 }
29c87f7f 1736
82f73884 1737 /* This is really a new thread. Add it. */
1941c569
PA
1738 remote_add_thread (currthread, running);
1739
1740 /* If we found a new inferior, let the common code do whatever
1741 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
1742 breakpoints), unless we're just setting up an all-stop
1743 connection. */
1941c569 1744 if (inf != NULL)
b7ea362b
PA
1745 {
1746 struct remote_state *rs = get_remote_state ();
1747
1748 if (non_stop || !rs->starting_up)
1749 notice_new_inferior (currthread, running, 0);
1750 }
c0a2216e 1751 }
c906108c
SS
1752}
1753
dc146f7c
VP
1754/* Return the private thread data, creating it if necessary. */
1755
70221824 1756static struct private_thread_info *
dc146f7c
VP
1757demand_private_info (ptid_t ptid)
1758{
1759 struct thread_info *info = find_thread_ptid (ptid);
1760
1761 gdb_assert (info);
1762
fe978cb0 1763 if (!info->priv)
dc146f7c 1764 {
fe978cb0 1765 info->priv = xmalloc (sizeof (*(info->priv)));
dc146f7c 1766 info->private_dtor = free_private_thread_info;
fe978cb0
PA
1767 info->priv->core = -1;
1768 info->priv->extra = 0;
dc146f7c
VP
1769 }
1770
fe978cb0 1771 return info->priv;
dc146f7c
VP
1772}
1773
74531fed
PA
1774/* Call this function as a result of
1775 1) A halt indication (T packet) containing a thread id
1776 2) A direct query of currthread
0df8b418 1777 3) Successful execution of set thread */
74531fed
PA
1778
1779static void
47f8a51d 1780record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 1781{
47f8a51d 1782 rs->general_thread = currthread;
74531fed
PA
1783}
1784
89be2091
DJ
1785/* If 'QPassSignals' is supported, tell the remote stub what signals
1786 it can simply pass through to the inferior without reporting. */
1787
1788static void
94bedb42
TT
1789remote_pass_signals (struct target_ops *self,
1790 int numsigs, unsigned char *pass_signals)
89be2091 1791{
4082afcc 1792 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
1793 {
1794 char *pass_packet, *p;
89be2091 1795 int count = 0, i;
747dc59d 1796 struct remote_state *rs = get_remote_state ();
89be2091
DJ
1797
1798 gdb_assert (numsigs < 256);
1799 for (i = 0; i < numsigs; i++)
1800 {
2455069d 1801 if (pass_signals[i])
89be2091
DJ
1802 count++;
1803 }
1804 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1805 strcpy (pass_packet, "QPassSignals:");
1806 p = pass_packet + strlen (pass_packet);
1807 for (i = 0; i < numsigs; i++)
1808 {
2455069d 1809 if (pass_signals[i])
89be2091
DJ
1810 {
1811 if (i >= 16)
1812 *p++ = tohex (i >> 4);
1813 *p++ = tohex (i & 15);
1814 if (count)
1815 *p++ = ';';
1816 else
1817 break;
1818 count--;
1819 }
1820 }
1821 *p = 0;
747dc59d 1822 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 1823 {
89be2091
DJ
1824 putpkt (pass_packet);
1825 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1826 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
1827 if (rs->last_pass_packet)
1828 xfree (rs->last_pass_packet);
1829 rs->last_pass_packet = pass_packet;
89be2091
DJ
1830 }
1831 else
1832 xfree (pass_packet);
1833 }
1834}
1835
9b224c5e
PA
1836/* If 'QProgramSignals' is supported, tell the remote stub what
1837 signals it should pass through to the inferior when detaching. */
1838
1839static void
daf5e9b6
TT
1840remote_program_signals (struct target_ops *self,
1841 int numsigs, unsigned char *signals)
9b224c5e 1842{
4082afcc 1843 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
1844 {
1845 char *packet, *p;
1846 int count = 0, i;
5e4a05c4 1847 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
1848
1849 gdb_assert (numsigs < 256);
1850 for (i = 0; i < numsigs; i++)
1851 {
1852 if (signals[i])
1853 count++;
1854 }
1855 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1856 strcpy (packet, "QProgramSignals:");
1857 p = packet + strlen (packet);
1858 for (i = 0; i < numsigs; i++)
1859 {
1860 if (signal_pass_state (i))
1861 {
1862 if (i >= 16)
1863 *p++ = tohex (i >> 4);
1864 *p++ = tohex (i & 15);
1865 if (count)
1866 *p++ = ';';
1867 else
1868 break;
1869 count--;
1870 }
1871 }
1872 *p = 0;
5e4a05c4
TT
1873 if (!rs->last_program_signals_packet
1874 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 1875 {
9b224c5e
PA
1876 putpkt (packet);
1877 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1878 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
1879 xfree (rs->last_program_signals_packet);
1880 rs->last_program_signals_packet = packet;
9b224c5e
PA
1881 }
1882 else
1883 xfree (packet);
1884 }
1885}
1886
79d7f229
PA
1887/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1888 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1889 thread. If GEN is set, set the general thread, if not, then set
1890 the step/continue thread. */
c906108c 1891static void
79d7f229 1892set_thread (struct ptid ptid, int gen)
c906108c 1893{
d01949b6 1894 struct remote_state *rs = get_remote_state ();
47f8a51d 1895 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 1896 char *buf = rs->buf;
79d7f229 1897 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 1898
79d7f229 1899 if (ptid_equal (state, ptid))
c906108c
SS
1900 return;
1901
79d7f229
PA
1902 *buf++ = 'H';
1903 *buf++ = gen ? 'g' : 'c';
1904 if (ptid_equal (ptid, magic_null_ptid))
1905 xsnprintf (buf, endbuf - buf, "0");
1906 else if (ptid_equal (ptid, any_thread_ptid))
1907 xsnprintf (buf, endbuf - buf, "0");
1908 else if (ptid_equal (ptid, minus_one_ptid))
1909 xsnprintf (buf, endbuf - buf, "-1");
1910 else
82f73884 1911 write_ptid (buf, endbuf, ptid);
79d7f229 1912 putpkt (rs->buf);
6d820c5c 1913 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 1914 if (gen)
47f8a51d 1915 rs->general_thread = ptid;
c906108c 1916 else
47f8a51d 1917 rs->continue_thread = ptid;
c906108c 1918}
79d7f229
PA
1919
1920static void
1921set_general_thread (struct ptid ptid)
1922{
1923 set_thread (ptid, 1);
1924}
1925
1926static void
1927set_continue_thread (struct ptid ptid)
1928{
1929 set_thread (ptid, 0);
1930}
1931
3c9c4b83
PA
1932/* Change the remote current process. Which thread within the process
1933 ends up selected isn't important, as long as it is the same process
1934 as what INFERIOR_PTID points to.
1935
1936 This comes from that fact that there is no explicit notion of
1937 "selected process" in the protocol. The selected process for
1938 general operations is the process the selected general thread
1939 belongs to. */
1940
1941static void
1942set_general_process (void)
1943{
1944 struct remote_state *rs = get_remote_state ();
1945
1946 /* If the remote can't handle multiple processes, don't bother. */
901f9912 1947 if (!rs->extended || !remote_multi_process_p (rs))
3c9c4b83
PA
1948 return;
1949
1950 /* We only need to change the remote current thread if it's pointing
1951 at some other process. */
47f8a51d 1952 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
1953 set_general_thread (inferior_ptid);
1954}
1955
c906108c 1956\f
7d1a114c
PA
1957/* Return nonzero if this is the main thread that we made up ourselves
1958 to model non-threaded targets as single-threaded. */
c906108c
SS
1959
1960static int
7d1a114c 1961remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
c906108c 1962{
6d820c5c 1963 struct remote_state *rs = get_remote_state ();
82f73884 1964 char *p, *endp;
c906108c 1965
c0a2216e
PA
1966 if (ptid_equal (ptid, magic_null_ptid))
1967 /* The main thread is always alive. */
1968 return 1;
1969
ba348170 1970 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
1971 /* The main thread is always alive. This can happen after a
1972 vAttach, if the remote side doesn't support
1973 multi-threading. */
1974 return 1;
1975
7d1a114c
PA
1976 return 0;
1977}
1978
1979/* Return nonzero if the thread PTID is still alive on the remote
1980 system. */
1981
1982static int
1983remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1984{
1985 struct remote_state *rs = get_remote_state ();
1986 char *p, *endp;
1987
1988 /* Check if this is a thread that we made up ourselves to model
1989 non-threaded targets as single-threaded. */
1990 if (remote_thread_always_alive (ops, ptid))
1991 return 1;
1992
82f73884
PA
1993 p = rs->buf;
1994 endp = rs->buf + get_remote_packet_size ();
1995
1996 *p++ = 'T';
1997 write_ptid (p, endp, ptid);
1998
2e9f7625 1999 putpkt (rs->buf);
6d820c5c 2000 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2001 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
2002}
2003
2004/* About these extended threadlist and threadinfo packets. They are
2005 variable length packets but, the fields within them are often fixed
2006 length. They are redundent enough to send over UDP as is the
2007 remote protocol in general. There is a matching unit test module
2008 in libstub. */
2009
23860348 2010/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 2011 libstub protocol encoding, and remote.c. It is not particularly
23860348 2012 changable. */
cce74817
JM
2013
2014/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 2015 Plan to fix this. */
cce74817 2016
23860348 2017typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 2018
9d1f7ab2 2019/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 2020 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
2021
2022struct gdb_ext_thread_info
c5aa993b 2023 {
23860348 2024 threadref threadid; /* External form of thread reference. */
2bc416ba 2025 int active; /* Has state interesting to GDB?
23860348 2026 regs, stack. */
2bc416ba 2027 char display[256]; /* Brief state display, name,
cedea757 2028 blocked/suspended. */
23860348 2029 char shortname[32]; /* To be used to name threads. */
2bc416ba 2030 char more_display[256]; /* Long info, statistics, queue depth,
23860348 2031 whatever. */
c5aa993b 2032 };
cce74817
JM
2033
2034/* The volume of remote transfers can be limited by submitting
2035 a mask containing bits specifying the desired information.
2036 Use a union of these values as the 'selection' parameter to
0df8b418 2037 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
2038
2039#define TAG_THREADID 1
2040#define TAG_EXISTS 2
2041#define TAG_DISPLAY 4
2042#define TAG_THREADNAME 8
c5aa993b 2043#define TAG_MOREDISPLAY 16
cce74817 2044
23860348 2045#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 2046
a14ed312 2047static char *unpack_nibble (char *buf, int *val);
cce74817 2048
a14ed312 2049static char *unpack_byte (char *buf, int *value);
cce74817 2050
a14ed312 2051static char *pack_int (char *buf, int value);
cce74817 2052
a14ed312 2053static char *unpack_int (char *buf, int *value);
cce74817 2054
a14ed312 2055static char *unpack_string (char *src, char *dest, int length);
cce74817 2056
23860348 2057static char *pack_threadid (char *pkt, threadref *id);
cce74817 2058
23860348 2059static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 2060
23860348 2061void int_to_threadref (threadref *id, int value);
cce74817 2062
23860348 2063static int threadref_to_int (threadref *ref);
cce74817 2064
23860348 2065static void copy_threadref (threadref *dest, threadref *src);
cce74817 2066
23860348 2067static int threadmatch (threadref *dest, threadref *src);
cce74817 2068
2bc416ba 2069static char *pack_threadinfo_request (char *pkt, int mode,
23860348 2070 threadref *id);
cce74817 2071
a14ed312 2072static int remote_unpack_thread_info_response (char *pkt,
23860348 2073 threadref *expectedref,
a14ed312
KB
2074 struct gdb_ext_thread_info
2075 *info);
cce74817
JM
2076
2077
2bc416ba 2078static int remote_get_threadinfo (threadref *threadid,
23860348 2079 int fieldset, /*TAG mask */
a14ed312 2080 struct gdb_ext_thread_info *info);
cce74817 2081
a14ed312
KB
2082static char *pack_threadlist_request (char *pkt, int startflag,
2083 int threadcount,
23860348 2084 threadref *nextthread);
cce74817 2085
a14ed312
KB
2086static int parse_threadlist_response (char *pkt,
2087 int result_limit,
23860348 2088 threadref *original_echo,
2bc416ba 2089 threadref *resultlist,
23860348 2090 int *doneflag);
cce74817 2091
a14ed312 2092static int remote_get_threadlist (int startflag,
23860348 2093 threadref *nextthread,
a14ed312
KB
2094 int result_limit,
2095 int *done,
2bc416ba 2096 int *result_count,
23860348 2097 threadref *threadlist);
cce74817 2098
23860348 2099typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2100
a14ed312
KB
2101static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2102 void *context, int looplimit);
cce74817 2103
23860348 2104static int remote_newthread_step (threadref *ref, void *context);
cce74817 2105
82f73884
PA
2106
2107/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2108 buffer we're allowed to write to. Returns
2109 BUF+CHARACTERS_WRITTEN. */
2110
2111static char *
2112write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2113{
2114 int pid, tid;
2115 struct remote_state *rs = get_remote_state ();
2116
2117 if (remote_multi_process_p (rs))
2118 {
2119 pid = ptid_get_pid (ptid);
2120 if (pid < 0)
2121 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2122 else
2123 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2124 }
ba348170 2125 tid = ptid_get_lwp (ptid);
82f73884
PA
2126 if (tid < 0)
2127 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2128 else
2129 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2130
2131 return buf;
2132}
2133
2134/* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2135 passed the last parsed char. Returns null_ptid on error. */
2136
2137static ptid_t
2138read_ptid (char *buf, char **obuf)
2139{
2140 char *p = buf;
2141 char *pp;
2142 ULONGEST pid = 0, tid = 0;
82f73884
PA
2143
2144 if (*p == 'p')
2145 {
2146 /* Multi-process ptid. */
2147 pp = unpack_varlen_hex (p + 1, &pid);
2148 if (*pp != '.')
b37520b6 2149 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2150
2151 p = pp;
2152 pp = unpack_varlen_hex (p + 1, &tid);
2153 if (obuf)
2154 *obuf = pp;
ba348170 2155 return ptid_build (pid, tid, 0);
82f73884
PA
2156 }
2157
2158 /* No multi-process. Just a tid. */
2159 pp = unpack_varlen_hex (p, &tid);
2160
2161 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2162 what's in inferior_ptid, unless it's null at this point. If so,
2163 then since there's no way to know the pid of the reported
2164 threads, use the magic number. */
2165 if (ptid_equal (inferior_ptid, null_ptid))
2166 pid = ptid_get_pid (magic_null_ptid);
2167 else
2168 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2169
2170 if (obuf)
2171 *obuf = pp;
ba348170 2172 return ptid_build (pid, tid, 0);
82f73884
PA
2173}
2174
c906108c 2175static int
fba45db2 2176stubhex (int ch)
c906108c
SS
2177{
2178 if (ch >= 'a' && ch <= 'f')
2179 return ch - 'a' + 10;
2180 if (ch >= '0' && ch <= '9')
2181 return ch - '0';
2182 if (ch >= 'A' && ch <= 'F')
2183 return ch - 'A' + 10;
2184 return -1;
2185}
2186
2187static int
fba45db2 2188stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2189{
2190 int nibble;
2191 int retval = 0;
2192
2193 while (fieldlength)
2194 {
2195 nibble = stubhex (*buff++);
2196 retval |= nibble;
2197 fieldlength--;
2198 if (fieldlength)
2199 retval = retval << 4;
2200 }
2201 return retval;
2202}
2203
c906108c 2204static char *
fba45db2 2205unpack_nibble (char *buf, int *val)
c906108c 2206{
b7589f7d 2207 *val = fromhex (*buf++);
c906108c
SS
2208 return buf;
2209}
2210
c906108c 2211static char *
fba45db2 2212unpack_byte (char *buf, int *value)
c906108c
SS
2213{
2214 *value = stub_unpack_int (buf, 2);
2215 return buf + 2;
2216}
2217
2218static char *
fba45db2 2219pack_int (char *buf, int value)
c906108c
SS
2220{
2221 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2222 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2223 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2224 buf = pack_hex_byte (buf, (value & 0xff));
2225 return buf;
2226}
2227
2228static char *
fba45db2 2229unpack_int (char *buf, int *value)
c906108c
SS
2230{
2231 *value = stub_unpack_int (buf, 8);
2232 return buf + 8;
2233}
2234
23860348 2235#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2236static char *pack_string (char *pkt, char *string);
c906108c
SS
2237
2238static char *
fba45db2 2239pack_string (char *pkt, char *string)
c906108c
SS
2240{
2241 char ch;
2242 int len;
2243
2244 len = strlen (string);
2245 if (len > 200)
23860348 2246 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2247 pkt = pack_hex_byte (pkt, len);
2248 while (len-- > 0)
2249 {
2250 ch = *string++;
2251 if ((ch == '\0') || (ch == '#'))
23860348 2252 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2253 *pkt++ = ch;
2254 }
2255 return pkt;
2256}
2257#endif /* 0 (unused) */
2258
2259static char *
fba45db2 2260unpack_string (char *src, char *dest, int length)
c906108c
SS
2261{
2262 while (length--)
2263 *dest++ = *src++;
2264 *dest = '\0';
2265 return src;
2266}
2267
2268static char *
fba45db2 2269pack_threadid (char *pkt, threadref *id)
c906108c
SS
2270{
2271 char *limit;
2272 unsigned char *altid;
2273
2274 altid = (unsigned char *) id;
2275 limit = pkt + BUF_THREAD_ID_SIZE;
2276 while (pkt < limit)
2277 pkt = pack_hex_byte (pkt, *altid++);
2278 return pkt;
2279}
2280
2281
2282static char *
fba45db2 2283unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2284{
2285 char *altref;
2286 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2287 int x, y;
2288
2289 altref = (char *) id;
2290
2291 while (inbuf < limit)
2292 {
2293 x = stubhex (*inbuf++);
2294 y = stubhex (*inbuf++);
2295 *altref++ = (x << 4) | y;
2296 }
2297 return inbuf;
2298}
2299
2300/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2301 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2302 to use 64bit thread references internally. This is an adapter
2303 function. */
2304
2305void
fba45db2 2306int_to_threadref (threadref *id, int value)
c906108c
SS
2307{
2308 unsigned char *scan;
2309
2310 scan = (unsigned char *) id;
2311 {
2312 int i = 4;
2313 while (i--)
2314 *scan++ = 0;
2315 }
2316 *scan++ = (value >> 24) & 0xff;
2317 *scan++ = (value >> 16) & 0xff;
2318 *scan++ = (value >> 8) & 0xff;
2319 *scan++ = (value & 0xff);
2320}
2321
2322static int
fba45db2 2323threadref_to_int (threadref *ref)
c906108c
SS
2324{
2325 int i, value = 0;
2326 unsigned char *scan;
2327
cfd77fa1 2328 scan = *ref;
c906108c
SS
2329 scan += 4;
2330 i = 4;
2331 while (i-- > 0)
2332 value = (value << 8) | ((*scan++) & 0xff);
2333 return value;
2334}
2335
2336static void
fba45db2 2337copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2338{
2339 int i;
2340 unsigned char *csrc, *cdest;
2341
2342 csrc = (unsigned char *) src;
2343 cdest = (unsigned char *) dest;
2344 i = 8;
2345 while (i--)
2346 *cdest++ = *csrc++;
2347}
2348
2349static int
fba45db2 2350threadmatch (threadref *dest, threadref *src)
c906108c 2351{
23860348 2352 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2353#if 0
2354 unsigned char *srcp, *destp;
2355 int i, result;
2356 srcp = (char *) src;
2357 destp = (char *) dest;
2358
2359 result = 1;
2360 while (i-- > 0)
2361 result &= (*srcp++ == *destp++) ? 1 : 0;
2362 return result;
2363#endif
2364 return 1;
2365}
2366
2367/*
c5aa993b
JM
2368 threadid:1, # always request threadid
2369 context_exists:2,
2370 display:4,
2371 unique_name:8,
2372 more_display:16
2373 */
c906108c
SS
2374
2375/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2376
2377static char *
fba45db2 2378pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2379{
23860348
MS
2380 *pkt++ = 'q'; /* Info Query */
2381 *pkt++ = 'P'; /* process or thread info */
2382 pkt = pack_int (pkt, mode); /* mode */
c906108c 2383 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2384 *pkt = '\0'; /* terminate */
c906108c
SS
2385 return pkt;
2386}
2387
23860348 2388/* These values tag the fields in a thread info response packet. */
c906108c 2389/* Tagging the fields allows us to request specific fields and to
23860348 2390 add more fields as time goes by. */
c906108c 2391
23860348 2392#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2393#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2394 fetch registers and its stack? */
c5aa993b 2395#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2396#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2397#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2398 the process. */
c906108c
SS
2399
2400static int
fba45db2
KB
2401remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2402 struct gdb_ext_thread_info *info)
c906108c 2403{
d01949b6 2404 struct remote_state *rs = get_remote_state ();
c906108c 2405 int mask, length;
cfd77fa1 2406 int tag;
c906108c 2407 threadref ref;
6d820c5c 2408 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
2409 int retval = 1;
2410
23860348 2411 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
2412 info->active = 0;
2413 info->display[0] = '\0';
2414 info->shortname[0] = '\0';
2415 info->more_display[0] = '\0';
2416
23860348
MS
2417 /* Assume the characters indicating the packet type have been
2418 stripped. */
c906108c
SS
2419 pkt = unpack_int (pkt, &mask); /* arg mask */
2420 pkt = unpack_threadid (pkt, &ref);
2421
2422 if (mask == 0)
8a3fe4f8 2423 warning (_("Incomplete response to threadinfo request."));
c906108c 2424 if (!threadmatch (&ref, expectedref))
23860348 2425 { /* This is an answer to a different request. */
8a3fe4f8 2426 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
2427 return 0;
2428 }
2429 copy_threadref (&info->threadid, &ref);
2430
23860348 2431 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 2432
23860348
MS
2433 /* Packets are terminated with nulls. */
2434 while ((pkt < limit) && mask && *pkt)
c906108c
SS
2435 {
2436 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
2437 pkt = unpack_byte (pkt, &length); /* length */
2438 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 2439 {
8a3fe4f8 2440 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
2441 retval = 0;
2442 break;
2443 }
2444 if (tag == TAG_THREADID)
2445 {
2446 if (length != 16)
2447 {
8a3fe4f8 2448 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
2449 retval = 0;
2450 break;
2451 }
2452 pkt = unpack_threadid (pkt, &ref);
2453 mask = mask & ~TAG_THREADID;
2454 continue;
2455 }
2456 if (tag == TAG_EXISTS)
2457 {
2458 info->active = stub_unpack_int (pkt, length);
2459 pkt += length;
2460 mask = mask & ~(TAG_EXISTS);
2461 if (length > 8)
2462 {
8a3fe4f8 2463 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
2464 retval = 0;
2465 break;
2466 }
2467 continue;
2468 }
2469 if (tag == TAG_THREADNAME)
2470 {
2471 pkt = unpack_string (pkt, &info->shortname[0], length);
2472 mask = mask & ~TAG_THREADNAME;
2473 continue;
2474 }
2475 if (tag == TAG_DISPLAY)
2476 {
2477 pkt = unpack_string (pkt, &info->display[0], length);
2478 mask = mask & ~TAG_DISPLAY;
2479 continue;
2480 }
2481 if (tag == TAG_MOREDISPLAY)
2482 {
2483 pkt = unpack_string (pkt, &info->more_display[0], length);
2484 mask = mask & ~TAG_MOREDISPLAY;
2485 continue;
2486 }
8a3fe4f8 2487 warning (_("ERROR RMT: unknown thread info tag."));
23860348 2488 break; /* Not a tag we know about. */
c906108c
SS
2489 }
2490 return retval;
2491}
2492
2493static int
fba45db2
KB
2494remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2495 struct gdb_ext_thread_info *info)
c906108c 2496{
d01949b6 2497 struct remote_state *rs = get_remote_state ();
c906108c 2498 int result;
c906108c 2499
2e9f7625
DJ
2500 pack_threadinfo_request (rs->buf, fieldset, threadid);
2501 putpkt (rs->buf);
6d820c5c 2502 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
2503
2504 if (rs->buf[0] == '\0')
2505 return 0;
2506
2e9f7625 2507 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 2508 threadid, info);
c906108c
SS
2509 return result;
2510}
2511
c906108c
SS
2512/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2513
2514static char *
fba45db2
KB
2515pack_threadlist_request (char *pkt, int startflag, int threadcount,
2516 threadref *nextthread)
c906108c
SS
2517{
2518 *pkt++ = 'q'; /* info query packet */
2519 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 2520 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
2521 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2522 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2523 *pkt = '\0';
2524 return pkt;
2525}
2526
2527/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2528
2529static int
fba45db2
KB
2530parse_threadlist_response (char *pkt, int result_limit,
2531 threadref *original_echo, threadref *resultlist,
2532 int *doneflag)
c906108c 2533{
d01949b6 2534 struct remote_state *rs = get_remote_state ();
c906108c
SS
2535 char *limit;
2536 int count, resultcount, done;
2537
2538 resultcount = 0;
2539 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 2540 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 2541 /* done parse past here */
c906108c
SS
2542 pkt = unpack_byte (pkt, &count); /* count field */
2543 pkt = unpack_nibble (pkt, &done);
2544 /* The first threadid is the argument threadid. */
2545 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2546 while ((count-- > 0) && (pkt < limit))
2547 {
2548 pkt = unpack_threadid (pkt, resultlist++);
2549 if (resultcount++ >= result_limit)
2550 break;
2551 }
2552 if (doneflag)
2553 *doneflag = done;
2554 return resultcount;
2555}
2556
6dc54d91
PA
2557/* Fetch the next batch of threads from the remote. Returns -1 if the
2558 qL packet is not supported, 0 on error and 1 on success. */
2559
c906108c 2560static int
fba45db2
KB
2561remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2562 int *done, int *result_count, threadref *threadlist)
c906108c 2563{
d01949b6 2564 struct remote_state *rs = get_remote_state ();
c906108c
SS
2565 int result = 1;
2566
23860348 2567 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
2568 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2569 >= get_remote_packet_size ())
ea9c271d 2570 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 2571
6d820c5c
DJ
2572 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2573 putpkt (rs->buf);
2574 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 2575 if (*rs->buf == '\0')
6dc54d91
PA
2576 {
2577 /* Packet not supported. */
2578 return -1;
2579 }
2580
2581 *result_count =
2582 parse_threadlist_response (rs->buf + 2, result_limit,
2583 &rs->echo_nextthread, threadlist, done);
c906108c 2584
0d031856 2585 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 2586 {
23860348
MS
2587 /* FIXME: This is a good reason to drop the packet. */
2588 /* Possably, there is a duplicate response. */
c906108c
SS
2589 /* Possabilities :
2590 retransmit immediatly - race conditions
2591 retransmit after timeout - yes
2592 exit
2593 wait for packet, then exit
2594 */
8a3fe4f8 2595 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 2596 return 0; /* I choose simply exiting. */
c906108c
SS
2597 }
2598 if (*result_count <= 0)
2599 {
2600 if (*done != 1)
2601 {
8a3fe4f8 2602 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
2603 result = 0;
2604 }
2605 return result; /* break; */
2606 }
2607 if (*result_count > result_limit)
2608 {
2609 *result_count = 0;
8a3fe4f8 2610 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
2611 return 0;
2612 }
2613 return result;
2614}
2615
6dc54d91
PA
2616/* Fetch the list of remote threads, with the qL packet, and call
2617 STEPFUNCTION for each thread found. Stops iterating and returns 1
2618 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2619 STEPFUNCTION returns false. If the packet is not supported,
2620 returns -1. */
c906108c 2621
c906108c 2622static int
fba45db2
KB
2623remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2624 int looplimit)
c906108c 2625{
0d031856 2626 struct remote_state *rs = get_remote_state ();
c906108c
SS
2627 int done, i, result_count;
2628 int startflag = 1;
2629 int result = 1;
2630 int loopcount = 0;
c906108c
SS
2631
2632 done = 0;
2633 while (!done)
2634 {
2635 if (loopcount++ > looplimit)
2636 {
2637 result = 0;
8a3fe4f8 2638 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
2639 break;
2640 }
6dc54d91
PA
2641 result = remote_get_threadlist (startflag, &rs->nextthread,
2642 MAXTHREADLISTRESULTS,
2643 &done, &result_count,
2644 rs->resultthreadlist);
2645 if (result <= 0)
2646 break;
23860348 2647 /* Clear for later iterations. */
c906108c
SS
2648 startflag = 0;
2649 /* Setup to resume next batch of thread references, set nextthread. */
2650 if (result_count >= 1)
0d031856
TT
2651 copy_threadref (&rs->nextthread,
2652 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
2653 i = 0;
2654 while (result_count--)
6dc54d91
PA
2655 {
2656 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2657 {
2658 result = 0;
2659 break;
2660 }
2661 }
c906108c
SS
2662 }
2663 return result;
2664}
2665
6dc54d91
PA
2666/* A thread found on the remote target. */
2667
2668typedef struct thread_item
2669{
2670 /* The thread's PTID. */
2671 ptid_t ptid;
2672
2673 /* The thread's extra info. May be NULL. */
2674 char *extra;
2675
2676 /* The core the thread was running on. -1 if not known. */
2677 int core;
2678} thread_item_t;
2679DEF_VEC_O(thread_item_t);
2680
2681/* Context passed around to the various methods listing remote
2682 threads. As new threads are found, they're added to the ITEMS
2683 vector. */
2684
2685struct threads_listing_context
2686{
2687 /* The threads found on the remote target. */
2688 VEC (thread_item_t) *items;
2689};
2690
80134cf5
PA
2691/* Discard the contents of the constructed thread listing context. */
2692
2693static void
2694clear_threads_listing_context (void *p)
2695{
2696 struct threads_listing_context *context = p;
2697 int i;
2698 struct thread_item *item;
2699
2700 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2701 xfree (item->extra);
2702
2703 VEC_free (thread_item_t, context->items);
2704}
2705
cbb8991c
DB
2706/* Remove the thread specified as the related_pid field of WS
2707 from the CONTEXT list. */
2708
2709static void
2710threads_listing_context_remove (struct target_waitstatus *ws,
2711 struct threads_listing_context *context)
2712{
2713 struct thread_item *item;
2714 int i;
2715 ptid_t child_ptid = ws->value.related_pid;
2716
2717 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2718 {
2719 if (ptid_equal (item->ptid, child_ptid))
2720 {
2721 VEC_ordered_remove (thread_item_t, context->items, i);
2722 break;
2723 }
2724 }
2725}
2726
c906108c 2727static int
6dc54d91 2728remote_newthread_step (threadref *ref, void *data)
c906108c 2729{
6dc54d91
PA
2730 struct threads_listing_context *context = data;
2731 struct thread_item item;
79d7f229 2732 int pid = ptid_get_pid (inferior_ptid);
39f77062 2733
6dc54d91
PA
2734 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
2735 item.core = -1;
2736 item.extra = NULL;
2737
2738 VEC_safe_push (thread_item_t, context->items, &item);
2739
c906108c
SS
2740 return 1; /* continue iterator */
2741}
2742
2743#define CRAZY_MAX_THREADS 1000
2744
39f77062
KB
2745static ptid_t
2746remote_current_thread (ptid_t oldpid)
c906108c 2747{
d01949b6 2748 struct remote_state *rs = get_remote_state ();
c906108c
SS
2749
2750 putpkt ("qC");
6d820c5c 2751 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2752 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
82f73884 2753 return read_ptid (&rs->buf[2], NULL);
c906108c
SS
2754 else
2755 return oldpid;
2756}
2757
6dc54d91 2758/* List remote threads using the deprecated qL packet. */
cce74817 2759
6dc54d91
PA
2760static int
2761remote_get_threads_with_ql (struct target_ops *ops,
2762 struct threads_listing_context *context)
c906108c 2763{
6dc54d91
PA
2764 if (remote_threadlist_iterator (remote_newthread_step, context,
2765 CRAZY_MAX_THREADS) >= 0)
2766 return 1;
2767
2768 return 0;
c906108c
SS
2769}
2770
dc146f7c
VP
2771#if defined(HAVE_LIBEXPAT)
2772
dc146f7c
VP
2773static void
2774start_thread (struct gdb_xml_parser *parser,
2775 const struct gdb_xml_element *element,
2776 void *user_data, VEC(gdb_xml_value_s) *attributes)
2777{
6dc54d91 2778 struct threads_listing_context *data = user_data;
dc146f7c
VP
2779
2780 struct thread_item item;
2781 char *id;
3d2c1d41 2782 struct gdb_xml_value *attr;
dc146f7c 2783
3d2c1d41 2784 id = xml_find_attribute (attributes, "id")->value;
dc146f7c
VP
2785 item.ptid = read_ptid (id, NULL);
2786
3d2c1d41
PA
2787 attr = xml_find_attribute (attributes, "core");
2788 if (attr != NULL)
2789 item.core = *(ULONGEST *) attr->value;
dc146f7c
VP
2790 else
2791 item.core = -1;
2792
2793 item.extra = 0;
2794
2795 VEC_safe_push (thread_item_t, data->items, &item);
2796}
2797
2798static void
2799end_thread (struct gdb_xml_parser *parser,
2800 const struct gdb_xml_element *element,
2801 void *user_data, const char *body_text)
2802{
6dc54d91 2803 struct threads_listing_context *data = user_data;
dc146f7c
VP
2804
2805 if (body_text && *body_text)
2ae2a0b7 2806 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
dc146f7c
VP
2807}
2808
2809const struct gdb_xml_attribute thread_attributes[] = {
2810 { "id", GDB_XML_AF_NONE, NULL, NULL },
2811 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2812 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2813};
2814
2815const struct gdb_xml_element thread_children[] = {
2816 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2817};
2818
2819const struct gdb_xml_element threads_children[] = {
2820 { "thread", thread_attributes, thread_children,
2821 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2822 start_thread, end_thread },
2823 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2824};
2825
2826const struct gdb_xml_element threads_elements[] = {
2827 { "threads", NULL, threads_children,
2828 GDB_XML_EF_NONE, NULL, NULL },
2829 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2830};
2831
2832#endif
2833
6dc54d91 2834/* List remote threads using qXfer:threads:read. */
9d1f7ab2 2835
6dc54d91
PA
2836static int
2837remote_get_threads_with_qxfer (struct target_ops *ops,
2838 struct threads_listing_context *context)
0f71a2f6 2839{
dc146f7c 2840#if defined(HAVE_LIBEXPAT)
4082afcc 2841 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 2842 {
6dc54d91 2843 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
dc146f7c 2844 struct cleanup *back_to = make_cleanup (xfree, xml);
efc0eabd 2845
6dc54d91 2846 if (xml != NULL && *xml != '\0')
dc146f7c 2847 {
6dc54d91
PA
2848 gdb_xml_parse_quick (_("threads"), "threads.dtd",
2849 threads_elements, xml, context);
dc146f7c
VP
2850 }
2851
2852 do_cleanups (back_to);
6dc54d91 2853 return 1;
dc146f7c
VP
2854 }
2855#endif
2856
6dc54d91
PA
2857 return 0;
2858}
2859
2860/* List remote threads using qfThreadInfo/qsThreadInfo. */
2861
2862static int
2863remote_get_threads_with_qthreadinfo (struct target_ops *ops,
2864 struct threads_listing_context *context)
2865{
2866 struct remote_state *rs = get_remote_state ();
2867
b80fafe3 2868 if (rs->use_threadinfo_query)
9d1f7ab2 2869 {
6dc54d91
PA
2870 char *bufp;
2871
9d1f7ab2 2872 putpkt ("qfThreadInfo");
6d820c5c 2873 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2874 bufp = rs->buf;
9d1f7ab2 2875 if (bufp[0] != '\0') /* q packet recognized */
802188a7 2876 {
9d1f7ab2
MS
2877 while (*bufp++ == 'm') /* reply contains one or more TID */
2878 {
2879 do
2880 {
6dc54d91
PA
2881 struct thread_item item;
2882
2883 item.ptid = read_ptid (bufp, &bufp);
2884 item.core = -1;
2885 item.extra = NULL;
2886
2887 VEC_safe_push (thread_item_t, context->items, &item);
9d1f7ab2
MS
2888 }
2889 while (*bufp++ == ','); /* comma-separated list */
2890 putpkt ("qsThreadInfo");
6d820c5c 2891 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 2892 bufp = rs->buf;
9d1f7ab2 2893 }
6dc54d91
PA
2894 return 1;
2895 }
2896 else
2897 {
2898 /* Packet not recognized. */
2899 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
2900 }
2901 }
2902
6dc54d91
PA
2903 return 0;
2904}
2905
e8032dde 2906/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
2907 targets. */
2908
2909static void
e8032dde 2910remote_update_thread_list (struct target_ops *ops)
6dc54d91
PA
2911{
2912 struct remote_state *rs = get_remote_state ();
2913 struct threads_listing_context context;
2914 struct cleanup *old_chain;
ab970af1 2915 int got_list = 0;
e8032dde 2916
6dc54d91
PA
2917 context.items = NULL;
2918 old_chain = make_cleanup (clear_threads_listing_context, &context);
2919
2920 /* We have a few different mechanisms to fetch the thread list. Try
2921 them all, starting with the most preferred one first, falling
2922 back to older methods. */
2923 if (remote_get_threads_with_qxfer (ops, &context)
2924 || remote_get_threads_with_qthreadinfo (ops, &context)
2925 || remote_get_threads_with_ql (ops, &context))
2926 {
2927 int i;
2928 struct thread_item *item;
ab970af1
PA
2929 struct thread_info *tp, *tmp;
2930
2931 got_list = 1;
2932
7d1a114c
PA
2933 if (VEC_empty (thread_item_t, context.items)
2934 && remote_thread_always_alive (ops, inferior_ptid))
2935 {
2936 /* Some targets don't really support threads, but still
2937 reply an (empty) thread list in response to the thread
2938 listing packets, instead of replying "packet not
2939 supported". Exit early so we don't delete the main
2940 thread. */
2941 do_cleanups (old_chain);
2942 return;
2943 }
2944
ab970af1
PA
2945 /* CONTEXT now holds the current thread list on the remote
2946 target end. Delete GDB-side threads no longer found on the
2947 target. */
8a06aea7 2948 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 2949 {
ab970af1
PA
2950 for (i = 0;
2951 VEC_iterate (thread_item_t, context.items, i, item);
2952 ++i)
2953 {
2954 if (ptid_equal (item->ptid, tp->ptid))
2955 break;
2956 }
2957
2958 if (i == VEC_length (thread_item_t, context.items))
2959 {
2960 /* Not found. */
2961 delete_thread (tp->ptid);
2962 }
cbb8991c
DB
2963 }
2964
2965 /* Remove any unreported fork child threads from CONTEXT so
2966 that we don't interfere with follow fork, which is where
2967 creation of such threads is handled. */
2968 remove_new_fork_children (&context);
74531fed 2969
ab970af1 2970 /* And now add threads we don't know about yet to our list. */
6dc54d91
PA
2971 for (i = 0;
2972 VEC_iterate (thread_item_t, context.items, i, item);
2973 ++i)
2974 {
2975 if (!ptid_equal (item->ptid, null_ptid))
2976 {
2977 struct private_thread_info *info;
2978 /* In non-stop mode, we assume new found threads are
2979 running until proven otherwise with a stop reply. In
2980 all-stop, we can only get here if all threads are
2981 stopped. */
2982 int running = non_stop ? 1 : 0;
2983
2984 remote_notice_new_inferior (item->ptid, running);
2985
2986 info = demand_private_info (item->ptid);
2987 info->core = item->core;
2988 info->extra = item->extra;
2989 item->extra = NULL;
2990 }
2991 }
2992 }
2993
ab970af1
PA
2994 if (!got_list)
2995 {
2996 /* If no thread listing method is supported, then query whether
2997 each known thread is alive, one by one, with the T packet.
2998 If the target doesn't support threads at all, then this is a
2999 no-op. See remote_thread_alive. */
3000 prune_threads ();
3001 }
3002
6dc54d91 3003 do_cleanups (old_chain);
9d1f7ab2
MS
3004}
3005
802188a7 3006/*
9d1f7ab2
MS
3007 * Collect a descriptive string about the given thread.
3008 * The target may say anything it wants to about the thread
3009 * (typically info about its blocked / runnable state, name, etc.).
3010 * This string will appear in the info threads display.
802188a7 3011 *
9d1f7ab2
MS
3012 * Optional: targets are not required to implement this function.
3013 */
3014
3015static char *
c15906d8 3016remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
9d1f7ab2 3017{
d01949b6 3018 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3019 int result;
3020 int set;
3021 threadref id;
3022 struct gdb_ext_thread_info threadinfo;
23860348 3023 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3024 int n = 0; /* position in display_buf */
3025
5d93a237 3026 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3027 internal_error (__FILE__, __LINE__,
e2e0b3e5 3028 _("remote_threads_extra_info"));
9d1f7ab2 3029
60e569b9 3030 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3031 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3032 /* This is the main thread which was added by GDB. The remote
3033 server doesn't know about it. */
3034 return NULL;
3035
4082afcc 3036 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3037 {
3038 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3039
fe978cb0
PA
3040 if (info && info->priv)
3041 return info->priv->extra;
dc146f7c
VP
3042 else
3043 return NULL;
3044 }
3045
b80fafe3 3046 if (rs->use_threadextra_query)
9d1f7ab2 3047 {
82f73884
PA
3048 char *b = rs->buf;
3049 char *endb = rs->buf + get_remote_packet_size ();
3050
3051 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3052 b += strlen (b);
3053 write_ptid (b, endb, tp->ptid);
3054
2e9f7625 3055 putpkt (rs->buf);
6d820c5c 3056 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3057 if (rs->buf[0] != 0)
9d1f7ab2 3058 {
2e9f7625
DJ
3059 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
3060 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3061 display_buf [result] = '\0';
9d1f7ab2
MS
3062 return display_buf;
3063 }
0f71a2f6 3064 }
9d1f7ab2
MS
3065
3066 /* If the above query fails, fall back to the old method. */
b80fafe3 3067 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3068 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3069 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3070 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3071 if (remote_get_threadinfo (&id, set, &threadinfo))
3072 if (threadinfo.active)
0f71a2f6 3073 {
9d1f7ab2 3074 if (*threadinfo.shortname)
2bc416ba 3075 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3076 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3077 if (*threadinfo.display)
2bc416ba 3078 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3079 " State: %s,", threadinfo.display);
9d1f7ab2 3080 if (*threadinfo.more_display)
2bc416ba 3081 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3082 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3083
3084 if (n > 0)
c5aa993b 3085 {
23860348 3086 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3087 if (',' == display_buf[n-1])
3088 display_buf[n-1] = ' ';
3089 return display_buf;
c5aa993b 3090 }
0f71a2f6 3091 }
9d1f7ab2 3092 return NULL;
0f71a2f6 3093}
c906108c 3094\f
c5aa993b 3095
0fb4aa4b 3096static int
61fc905d 3097remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
0fb4aa4b
PA
3098 struct static_tracepoint_marker *marker)
3099{
3100 struct remote_state *rs = get_remote_state ();
3101 char *p = rs->buf;
3102
bba74b36 3103 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3104 p += strlen (p);
3105 p += hexnumstr (p, addr);
3106 putpkt (rs->buf);
3107 getpkt (&rs->buf, &rs->buf_size, 0);
3108 p = rs->buf;
3109
3110 if (*p == 'E')
3111 error (_("Remote failure reply: %s"), p);
3112
3113 if (*p++ == 'm')
3114 {
3115 parse_static_tracepoint_marker_definition (p, &p, marker);
3116 return 1;
3117 }
3118
3119 return 0;
3120}
3121
0fb4aa4b 3122static VEC(static_tracepoint_marker_p) *
c686c57f
TT
3123remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3124 const char *strid)
0fb4aa4b
PA
3125{
3126 struct remote_state *rs = get_remote_state ();
3127 VEC(static_tracepoint_marker_p) *markers = NULL;
3128 struct static_tracepoint_marker *marker = NULL;
3129 struct cleanup *old_chain;
3130 char *p;
3131
3132 /* Ask for a first packet of static tracepoint marker
3133 definition. */
3134 putpkt ("qTfSTM");
3135 getpkt (&rs->buf, &rs->buf_size, 0);
3136 p = rs->buf;
3137 if (*p == 'E')
3138 error (_("Remote failure reply: %s"), p);
3139
3140 old_chain = make_cleanup (free_current_marker, &marker);
3141
3142 while (*p++ == 'm')
3143 {
3144 if (marker == NULL)
3145 marker = XCNEW (struct static_tracepoint_marker);
3146
3147 do
3148 {
3149 parse_static_tracepoint_marker_definition (p, &p, marker);
3150
3151 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3152 {
3153 VEC_safe_push (static_tracepoint_marker_p,
3154 markers, marker);
3155 marker = NULL;
3156 }
3157 else
3158 {
3159 release_static_tracepoint_marker (marker);
3160 memset (marker, 0, sizeof (*marker));
3161 }
3162 }
3163 while (*p++ == ','); /* comma-separated list */
3164 /* Ask for another packet of static tracepoint definition. */
3165 putpkt ("qTsSTM");
3166 getpkt (&rs->buf, &rs->buf_size, 0);
3167 p = rs->buf;
3168 }
3169
3170 do_cleanups (old_chain);
3171 return markers;
3172}
3173
3174\f
10760264
JB
3175/* Implement the to_get_ada_task_ptid function for the remote targets. */
3176
3177static ptid_t
1e6b91a4 3178remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
10760264 3179{
ba348170 3180 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3181}
3182\f
3183
24b06219 3184/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3185
3186static void
fba45db2 3187extended_remote_restart (void)
c906108c 3188{
d01949b6 3189 struct remote_state *rs = get_remote_state ();
c906108c
SS
3190
3191 /* Send the restart command; for reasons I don't understand the
3192 remote side really expects a number after the "R". */
ea9c271d 3193 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3194 putpkt (rs->buf);
c906108c 3195
ad9a8f3f 3196 remote_fileio_reset ();
c906108c
SS
3197}
3198\f
3199/* Clean up connection to a remote debugger. */
3200
c906108c 3201static void
de90e03d 3202remote_close (struct target_ops *self)
c906108c 3203{
5d93a237
TT
3204 struct remote_state *rs = get_remote_state ();
3205
3206 if (rs->remote_desc == NULL)
d3fd5342
PA
3207 return; /* already closed */
3208
3209 /* Make sure we leave stdin registered in the event loop, and we
3210 don't leave the async SIGINT signal handler installed. */
e3594fd1 3211 remote_terminal_ours (self);
ce5ce7ed 3212
5d93a237
TT
3213 serial_close (rs->remote_desc);
3214 rs->remote_desc = NULL;
ce5ce7ed
PA
3215
3216 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3217 of all the inferiors and their threads we were controlling.
3218 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3219 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3220 inferior_ptid = null_ptid;
f67fd822 3221 discard_all_inferiors ();
ce5ce7ed 3222
f48ff2a7
YQ
3223 /* We are closing the remote target, so we should discard
3224 everything of this target. */
bcc75809 3225 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3226
3227 if (remote_async_inferior_event_token)
3228 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3229
5965e028 3230 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3231
3232 trace_reset_local_state ();
c906108c
SS
3233}
3234
23860348 3235/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3236
3237static void
fba45db2 3238get_offsets (void)
c906108c 3239{
d01949b6 3240 struct remote_state *rs = get_remote_state ();
2e9f7625 3241 char *buf;
085dd6e6 3242 char *ptr;
31d99776
DJ
3243 int lose, num_segments = 0, do_sections, do_segments;
3244 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3245 struct section_offsets *offs;
31d99776
DJ
3246 struct symfile_segment_data *data;
3247
3248 if (symfile_objfile == NULL)
3249 return;
c906108c
SS
3250
3251 putpkt ("qOffsets");
6d820c5c 3252 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3253 buf = rs->buf;
c906108c
SS
3254
3255 if (buf[0] == '\000')
3256 return; /* Return silently. Stub doesn't support
23860348 3257 this command. */
c906108c
SS
3258 if (buf[0] == 'E')
3259 {
8a3fe4f8 3260 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3261 return;
3262 }
3263
3264 /* Pick up each field in turn. This used to be done with scanf, but
3265 scanf will make trouble if CORE_ADDR size doesn't match
3266 conversion directives correctly. The following code will work
3267 with any size of CORE_ADDR. */
3268 text_addr = data_addr = bss_addr = 0;
3269 ptr = buf;
3270 lose = 0;
3271
61012eef 3272 if (startswith (ptr, "Text="))
c906108c
SS
3273 {
3274 ptr += 5;
3275 /* Don't use strtol, could lose on big values. */
3276 while (*ptr && *ptr != ';')
3277 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3278
61012eef 3279 if (startswith (ptr, ";Data="))
31d99776
DJ
3280 {
3281 ptr += 6;
3282 while (*ptr && *ptr != ';')
3283 data_addr = (data_addr << 4) + fromhex (*ptr++);
3284 }
3285 else
3286 lose = 1;
3287
61012eef 3288 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3289 {
3290 ptr += 5;
3291 while (*ptr && *ptr != ';')
3292 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3293
31d99776
DJ
3294 if (bss_addr != data_addr)
3295 warning (_("Target reported unsupported offsets: %s"), buf);
3296 }
3297 else
3298 lose = 1;
3299 }
61012eef 3300 else if (startswith (ptr, "TextSeg="))
c906108c 3301 {
31d99776
DJ
3302 ptr += 8;
3303 /* Don't use strtol, could lose on big values. */
c906108c 3304 while (*ptr && *ptr != ';')
31d99776
DJ
3305 text_addr = (text_addr << 4) + fromhex (*ptr++);
3306 num_segments = 1;
3307
61012eef 3308 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3309 {
3310 ptr += 9;
3311 while (*ptr && *ptr != ';')
3312 data_addr = (data_addr << 4) + fromhex (*ptr++);
3313 num_segments++;
3314 }
c906108c
SS
3315 }
3316 else
3317 lose = 1;
3318
3319 if (lose)
8a3fe4f8 3320 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3321 else if (*ptr != '\0')
3322 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3323
802188a7 3324 offs = ((struct section_offsets *)
a39a16c4 3325 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3326 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3327 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3328
31d99776
DJ
3329 data = get_symfile_segment_data (symfile_objfile->obfd);
3330 do_segments = (data != NULL);
3331 do_sections = num_segments == 0;
c906108c 3332
28c32713 3333 if (num_segments > 0)
31d99776 3334 {
31d99776
DJ
3335 segments[0] = text_addr;
3336 segments[1] = data_addr;
3337 }
28c32713
JB
3338 /* If we have two segments, we can still try to relocate everything
3339 by assuming that the .text and .data offsets apply to the whole
3340 text and data segments. Convert the offsets given in the packet
3341 to base addresses for symfile_map_offsets_to_segments. */
3342 else if (data && data->num_segments == 2)
3343 {
3344 segments[0] = data->segment_bases[0] + text_addr;
3345 segments[1] = data->segment_bases[1] + data_addr;
3346 num_segments = 2;
3347 }
8d385431
DJ
3348 /* If the object file has only one segment, assume that it is text
3349 rather than data; main programs with no writable data are rare,
3350 but programs with no code are useless. Of course the code might
3351 have ended up in the data segment... to detect that we would need
3352 the permissions here. */
3353 else if (data && data->num_segments == 1)
3354 {
3355 segments[0] = data->segment_bases[0] + text_addr;
3356 num_segments = 1;
3357 }
28c32713
JB
3358 /* There's no way to relocate by segment. */
3359 else
3360 do_segments = 0;
31d99776
DJ
3361
3362 if (do_segments)
3363 {
3364 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3365 offs, num_segments, segments);
3366
3367 if (ret == 0 && !do_sections)
3e43a32a
MS
3368 error (_("Can not handle qOffsets TextSeg "
3369 "response with this symbol file"));
31d99776
DJ
3370
3371 if (ret > 0)
3372 do_sections = 0;
3373 }
c906108c 3374
9ef895d6
DJ
3375 if (data)
3376 free_symfile_segment_data (data);
31d99776
DJ
3377
3378 if (do_sections)
3379 {
3380 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3381
3e43a32a
MS
3382 /* This is a temporary kludge to force data and bss to use the
3383 same offsets because that's what nlmconv does now. The real
3384 solution requires changes to the stub and remote.c that I
3385 don't have time to do right now. */
31d99776
DJ
3386
3387 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3388 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3389 }
c906108c
SS
3390
3391 objfile_relocate (symfile_objfile, offs);
3392}
3393
74531fed
PA
3394/* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3395 threads we know are stopped already. This is used during the
3396 initial remote connection in non-stop mode --- threads that are
3397 reported as already being stopped are left stopped. */
3398
3399static int
3400set_stop_requested_callback (struct thread_info *thread, void *data)
3401{
3402 /* If we have a stop reply for this thread, it must be stopped. */
3403 if (peek_stop_reply (thread->ptid))
3404 set_stop_requested (thread->ptid, 1);
3405
3406 return 0;
3407}
3408
9a7071a8
JB
3409/* Send interrupt_sequence to remote target. */
3410static void
eeae04df 3411send_interrupt_sequence (void)
9a7071a8 3412{
5d93a237
TT
3413 struct remote_state *rs = get_remote_state ();
3414
9a7071a8 3415 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3416 remote_serial_write ("\x03", 1);
9a7071a8 3417 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3418 serial_send_break (rs->remote_desc);
9a7071a8
JB
3419 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3420 {
5d93a237 3421 serial_send_break (rs->remote_desc);
c33e31fd 3422 remote_serial_write ("g", 1);
9a7071a8
JB
3423 }
3424 else
3425 internal_error (__FILE__, __LINE__,
3426 _("Invalid value for interrupt_sequence_mode: %s."),
3427 interrupt_sequence_mode);
3428}
3429
3405876a
PA
3430
3431/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3432 and extract the PTID. Returns NULL_PTID if not found. */
3433
3434static ptid_t
3435stop_reply_extract_thread (char *stop_reply)
3436{
3437 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3438 {
3439 char *p;
3440
3441 /* Txx r:val ; r:val (...) */
3442 p = &stop_reply[3];
3443
3444 /* Look for "register" named "thread". */
3445 while (*p != '\0')
3446 {
3447 char *p1;
3448
3449 p1 = strchr (p, ':');
3450 if (p1 == NULL)
3451 return null_ptid;
3452
3453 if (strncmp (p, "thread", p1 - p) == 0)
3454 return read_ptid (++p1, &p);
3455
3456 p1 = strchr (p, ';');
3457 if (p1 == NULL)
3458 return null_ptid;
3459 p1++;
3460
3461 p = p1;
3462 }
3463 }
3464
3465 return null_ptid;
3466}
3467
b7ea362b
PA
3468/* Determine the remote side's current thread. If we have a stop
3469 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3470 "thread" register we can extract the current thread from. If not,
3471 ask the remote which is the current thread with qC. The former
3472 method avoids a roundtrip. */
3473
3474static ptid_t
3475get_current_thread (char *wait_status)
3476{
3477 ptid_t ptid;
3478
3479 /* Note we don't use remote_parse_stop_reply as that makes use of
3480 the target architecture, which we haven't yet fully determined at
3481 this point. */
3482 if (wait_status != NULL)
3483 ptid = stop_reply_extract_thread (wait_status);
3484 if (ptid_equal (ptid, null_ptid))
3485 ptid = remote_current_thread (inferior_ptid);
3486
3487 return ptid;
3488}
3489
49c62f2e
PA
3490/* Query the remote target for which is the current thread/process,
3491 add it to our tables, and update INFERIOR_PTID. The caller is
3492 responsible for setting the state such that the remote end is ready
3405876a
PA
3493 to return the current thread.
3494
3495 This function is called after handling the '?' or 'vRun' packets,
3496 whose response is a stop reply from which we can also try
3497 extracting the thread. If the target doesn't support the explicit
3498 qC query, we infer the current thread from that stop reply, passed
3499 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
3500
3501static void
3405876a 3502add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
3503{
3504 struct remote_state *rs = get_remote_state ();
3505 int fake_pid_p = 0;
3405876a 3506 ptid_t ptid = null_ptid;
49c62f2e
PA
3507
3508 inferior_ptid = null_ptid;
3509
b7ea362b
PA
3510 /* Now, if we have thread information, update inferior_ptid. */
3511 ptid = get_current_thread (wait_status);
3405876a 3512
49c62f2e
PA
3513 if (!ptid_equal (ptid, null_ptid))
3514 {
3515 if (!remote_multi_process_p (rs))
3516 fake_pid_p = 1;
3517
3518 inferior_ptid = ptid;
3519 }
3520 else
3521 {
3522 /* Without this, some commands which require an active target
3523 (such as kill) won't work. This variable serves (at least)
3524 double duty as both the pid of the target process (if it has
3525 such), and as a flag indicating that a target is active. */
3526 inferior_ptid = magic_null_ptid;
3527 fake_pid_p = 1;
3528 }
3529
1b6e6f5c 3530 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1, 1);
49c62f2e
PA
3531
3532 /* Add the main thread. */
3533 add_thread_silent (inferior_ptid);
3534}
3535
9cbc821d 3536static void
04bd08de 3537remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
c906108c 3538{
c8d104ad
PA
3539 struct remote_state *rs = get_remote_state ();
3540 struct packet_config *noack_config;
2d717e4f 3541 char *wait_status = NULL;
8621d6a9 3542
23860348 3543 immediate_quit++; /* Allow user to interrupt it. */
522002f9 3544 QUIT;
c906108c 3545
9a7071a8
JB
3546 if (interrupt_on_connect)
3547 send_interrupt_sequence ();
3548
57e12211 3549 /* Ack any packet which the remote side has already sent. */
5d93a237 3550 serial_write (rs->remote_desc, "+", 1);
57e12211 3551
1e51243a
PA
3552 /* Signal other parts that we're going through the initial setup,
3553 and so things may not be stable yet. */
3554 rs->starting_up = 1;
3555
c8d104ad
PA
3556 /* The first packet we send to the target is the optional "supported
3557 packets" request. If the target can answer this, it will tell us
3558 which later probes to skip. */
3559 remote_query_supported ();
3560
d914c394 3561 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 3562 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
c378d69d 3563 remote_set_permissions (target);
d914c394 3564
c8d104ad
PA
3565 /* Next, we possibly activate noack mode.
3566
3567 If the QStartNoAckMode packet configuration is set to AUTO,
3568 enable noack mode if the stub reported a wish for it with
3569 qSupported.
3570
3571 If set to TRUE, then enable noack mode even if the stub didn't
3572 report it in qSupported. If the stub doesn't reply OK, the
3573 session ends with an error.
3574
3575 If FALSE, then don't activate noack mode, regardless of what the
3576 stub claimed should be the default with qSupported. */
3577
3578 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 3579 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
3580 {
3581 putpkt ("QStartNoAckMode");
3582 getpkt (&rs->buf, &rs->buf_size, 0);
3583 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3584 rs->noack_mode = 1;
3585 }
3586
04bd08de 3587 if (extended_p)
5fe04517
PA
3588 {
3589 /* Tell the remote that we are using the extended protocol. */
3590 putpkt ("!");
3591 getpkt (&rs->buf, &rs->buf_size, 0);
3592 }
3593
9b224c5e
PA
3594 /* Let the target know which signals it is allowed to pass down to
3595 the program. */
3596 update_signals_program_target ();
3597
d962ef82
DJ
3598 /* Next, if the target can specify a description, read it. We do
3599 this before anything involving memory or registers. */
3600 target_find_description ();
3601
6c95b8df
PA
3602 /* Next, now that we know something about the target, update the
3603 address spaces in the program spaces. */
3604 update_address_spaces ();
3605
50c71eaf
PA
3606 /* On OSs where the list of libraries is global to all
3607 processes, we fetch them early. */
f5656ead 3608 if (gdbarch_has_global_solist (target_gdbarch ()))
04bd08de 3609 solib_add (NULL, from_tty, target, auto_solib_add);
50c71eaf 3610
74531fed
PA
3611 if (non_stop)
3612 {
4082afcc 3613 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
3614 error (_("Non-stop mode requested, but remote "
3615 "does not support non-stop"));
74531fed
PA
3616
3617 putpkt ("QNonStop:1");
3618 getpkt (&rs->buf, &rs->buf_size, 0);
3619
3620 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3621 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
3622
3623 /* Find about threads and processes the stub is already
3624 controlling. We default to adding them in the running state.
3625 The '?' query below will then tell us about which threads are
3626 stopped. */
e8032dde 3627 remote_update_thread_list (target);
74531fed 3628 }
4082afcc 3629 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
3630 {
3631 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 3632 Request it explicitly. */
74531fed
PA
3633 putpkt ("QNonStop:0");
3634 getpkt (&rs->buf, &rs->buf_size, 0);
3635
3636 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3637 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
3638 }
3639
a0743c90
YQ
3640 /* Upload TSVs regardless of whether the target is running or not. The
3641 remote stub, such as GDBserver, may have some predefined or builtin
3642 TSVs, even if the target is not running. */
8bd200f1 3643 if (remote_get_trace_status (target, current_trace_status ()) != -1)
a0743c90
YQ
3644 {
3645 struct uploaded_tsv *uploaded_tsvs = NULL;
3646
181e3713 3647 remote_upload_trace_state_variables (target, &uploaded_tsvs);
a0743c90
YQ
3648 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3649 }
3650
2d717e4f
DJ
3651 /* Check whether the target is running now. */
3652 putpkt ("?");
3653 getpkt (&rs->buf, &rs->buf_size, 0);
3654
74531fed 3655 if (!non_stop)
2d717e4f 3656 {
e714e1bf
UW
3657 ptid_t ptid;
3658 int fake_pid_p = 0;
3659 struct inferior *inf;
3660
74531fed 3661 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 3662 {
04bd08de 3663 if (!extended_p)
74531fed 3664 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
3665
3666 /* We're connected, but not running. Drop out before we
3667 call start_remote. */
e278ad5b 3668 rs->starting_up = 0;
c35b1492 3669 return;
2d717e4f
DJ
3670 }
3671 else
74531fed 3672 {
74531fed
PA
3673 /* Save the reply for later. */
3674 wait_status = alloca (strlen (rs->buf) + 1);
3675 strcpy (wait_status, rs->buf);
3676 }
3677
b7ea362b 3678 /* Fetch thread list. */
e8032dde 3679 target_update_thread_list ();
b7ea362b 3680
74531fed
PA
3681 /* Let the stub know that we want it to return the thread. */
3682 set_continue_thread (minus_one_ptid);
3683
b7ea362b
PA
3684 if (thread_count () == 0)
3685 {
3686 /* Target has no concept of threads at all. GDB treats
3687 non-threaded target as single-threaded; add a main
3688 thread. */
3689 add_current_inferior_and_thread (wait_status);
3690 }
3691 else
3692 {
3693 /* We have thread information; select the thread the target
3694 says should be current. If we're reconnecting to a
3695 multi-threaded program, this will ideally be the thread
3696 that last reported an event before GDB disconnected. */
3697 inferior_ptid = get_current_thread (wait_status);
3698 if (ptid_equal (inferior_ptid, null_ptid))
3699 {
3700 /* Odd... The target was able to list threads, but not
3701 tell us which thread was current (no "thread"
3702 register in T stop reply?). Just pick the first
3703 thread in the thread list then. */
3704 inferior_ptid = thread_list->ptid;
3705 }
3706 }
74531fed 3707
6e586cc5
YQ
3708 /* init_wait_for_inferior should be called before get_offsets in order
3709 to manage `inserted' flag in bp loc in a correct state.
3710 breakpoint_init_inferior, called from init_wait_for_inferior, set
3711 `inserted' flag to 0, while before breakpoint_re_set, called from
3712 start_remote, set `inserted' flag to 1. In the initialization of
3713 inferior, breakpoint_init_inferior should be called first, and then
3714 breakpoint_re_set can be called. If this order is broken, state of
3715 `inserted' flag is wrong, and cause some problems on breakpoint
3716 manipulation. */
3717 init_wait_for_inferior ();
3718
74531fed
PA
3719 get_offsets (); /* Get text, data & bss offsets. */
3720
d962ef82
DJ
3721 /* If we could not find a description using qXfer, and we know
3722 how to do it some other way, try again. This is not
3723 supported for non-stop; it could be, but it is tricky if
3724 there are no stopped threads when we connect. */
04bd08de 3725 if (remote_read_description_p (target)
f5656ead 3726 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
3727 {
3728 target_clear_description ();
3729 target_find_description ();
3730 }
3731
74531fed
PA
3732 /* Use the previously fetched status. */
3733 gdb_assert (wait_status != NULL);
3734 strcpy (rs->buf, wait_status);
3735 rs->cached_wait_status = 1;
3736
3737 immediate_quit--;
04bd08de 3738 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
3739 }
3740 else
3741 {
68c97600
PA
3742 /* Clear WFI global state. Do this before finding about new
3743 threads and inferiors, and setting the current inferior.
3744 Otherwise we would clear the proceed status of the current
3745 inferior when we want its stop_soon state to be preserved
3746 (see notice_new_inferior). */
3747 init_wait_for_inferior ();
3748
74531fed
PA
3749 /* In non-stop, we will either get an "OK", meaning that there
3750 are no stopped threads at this time; or, a regular stop
3751 reply. In the latter case, there may be more than one thread
3752 stopped --- we pull them all out using the vStopped
3753 mechanism. */
3754 if (strcmp (rs->buf, "OK") != 0)
3755 {
722247f1 3756 struct notif_client *notif = &notif_client_stop;
2d717e4f 3757
722247f1
YQ
3758 /* remote_notif_get_pending_replies acks this one, and gets
3759 the rest out. */
f48ff2a7 3760 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
3761 = remote_notif_parse (notif, rs->buf);
3762 remote_notif_get_pending_events (notif);
c906108c 3763
74531fed
PA
3764 /* Make sure that threads that were stopped remain
3765 stopped. */
3766 iterate_over_threads (set_stop_requested_callback, NULL);
3767 }
2d717e4f 3768
74531fed 3769 if (target_can_async_p ())
6a3753b3 3770 target_async (1);
c906108c 3771
74531fed
PA
3772 if (thread_count () == 0)
3773 {
04bd08de 3774 if (!extended_p)
74531fed 3775 error (_("The target is not running (try extended-remote?)"));
82f73884 3776
c35b1492
PA
3777 /* We're connected, but not running. Drop out before we
3778 call start_remote. */
e278ad5b 3779 rs->starting_up = 0;
c35b1492
PA
3780 return;
3781 }
74531fed
PA
3782
3783 /* Let the stub know that we want it to return the thread. */
c0a2216e 3784
74531fed
PA
3785 /* Force the stub to choose a thread. */
3786 set_general_thread (null_ptid);
c906108c 3787
74531fed
PA
3788 /* Query it. */
3789 inferior_ptid = remote_current_thread (minus_one_ptid);
3790 if (ptid_equal (inferior_ptid, minus_one_ptid))
3791 error (_("remote didn't report the current thread in non-stop mode"));
c906108c 3792
74531fed
PA
3793 get_offsets (); /* Get text, data & bss offsets. */
3794
3795 /* In non-stop mode, any cached wait status will be stored in
3796 the stop reply queue. */
3797 gdb_assert (wait_status == NULL);
f0223081 3798
2455069d 3799 /* Report all signals during attach/startup. */
94bedb42 3800 remote_pass_signals (target, 0, NULL);
74531fed 3801 }
c8d104ad 3802
c8d104ad
PA
3803 /* If we connected to a live target, do some additional setup. */
3804 if (target_has_execution)
3805 {
f4ccffad 3806 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 3807 remote_check_symbols ();
c8d104ad 3808 }
50c71eaf 3809
d5551862
SS
3810 /* Possibly the target has been engaged in a trace run started
3811 previously; find out where things are at. */
8bd200f1 3812 if (remote_get_trace_status (target, current_trace_status ()) != -1)
d5551862 3813 {
00bf0b85 3814 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 3815
00bf0b85
SS
3816 if (current_trace_status ()->running)
3817 printf_filtered (_("Trace is already running on the target.\n"));
3818
ab6617cc 3819 remote_upload_tracepoints (target, &uploaded_tps);
00bf0b85
SS
3820
3821 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
3822 }
3823
1e51243a
PA
3824 /* The thread and inferior lists are now synchronized with the
3825 target, our symbols have been relocated, and we're merged the
3826 target's tracepoints with ours. We're done with basic start
3827 up. */
3828 rs->starting_up = 0;
3829
a25a5a45
PA
3830 /* Maybe breakpoints are global and need to be inserted now. */
3831 if (breakpoints_should_be_inserted_now ())
50c71eaf 3832 insert_breakpoints ();
c906108c
SS
3833}
3834
3835/* Open a connection to a remote debugger.
3836 NAME is the filename used for communication. */
3837
3838static void
014f9477 3839remote_open (const char *name, int from_tty)
c906108c 3840{
75c99385 3841 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
3842}
3843
c906108c
SS
3844/* Open a connection to a remote debugger using the extended
3845 remote gdb protocol. NAME is the filename used for communication. */
3846
3847static void
014f9477 3848extended_remote_open (const char *name, int from_tty)
c906108c 3849{
75c99385 3850 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
3851}
3852
ca4f7f8b
PA
3853/* Reset all packets back to "unknown support". Called when opening a
3854 new connection to a remote target. */
c906108c 3855
d471ea57 3856static void
ca4f7f8b 3857reset_all_packet_configs_support (void)
d471ea57
AC
3858{
3859 int i;
a744cf53 3860
444abaca 3861 for (i = 0; i < PACKET_MAX; i++)
4082afcc 3862 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
3863}
3864
ca4f7f8b
PA
3865/* Initialize all packet configs. */
3866
3867static void
3868init_all_packet_configs (void)
3869{
3870 int i;
3871
3872 for (i = 0; i < PACKET_MAX; i++)
3873 {
3874 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
3875 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3876 }
3877}
3878
23860348 3879/* Symbol look-up. */
dc8acb97
MS
3880
3881static void
36d25514 3882remote_check_symbols (void)
dc8acb97 3883{
d01949b6 3884 struct remote_state *rs = get_remote_state ();
dc8acb97 3885 char *msg, *reply, *tmp;
3b7344d5 3886 struct bound_minimal_symbol sym;
dc8acb97
MS
3887 int end;
3888
63154eca
PA
3889 /* The remote side has no concept of inferiors that aren't running
3890 yet, it only knows about running processes. If we're connected
3891 but our current inferior is not running, we should not invite the
3892 remote target to request symbol lookups related to its
3893 (unrelated) current process. */
3894 if (!target_has_execution)
3895 return;
3896
4082afcc 3897 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
3898 return;
3899
63154eca
PA
3900 /* Make sure the remote is pointing at the right process. Note
3901 there's no way to select "no process". */
3c9c4b83
PA
3902 set_general_process ();
3903
6d820c5c
DJ
3904 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3905 because we need both at the same time. */
ea9c271d 3906 msg = alloca (get_remote_packet_size ());
6d820c5c 3907
23860348 3908 /* Invite target to request symbol lookups. */
dc8acb97
MS
3909
3910 putpkt ("qSymbol::");
6d820c5c
DJ
3911 getpkt (&rs->buf, &rs->buf_size, 0);
3912 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
2e9f7625 3913 reply = rs->buf;
dc8acb97 3914
61012eef 3915 while (startswith (reply, "qSymbol:"))
dc8acb97 3916 {
77e371c0
TT
3917 struct bound_minimal_symbol sym;
3918
dc8acb97 3919 tmp = &reply[8];
cfd77fa1 3920 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
3921 msg[end] = '\0';
3922 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 3923 if (sym.minsym == NULL)
ea9c271d 3924 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 3925 else
2bbe3cc1 3926 {
f5656ead 3927 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 3928 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
3929
3930 /* If this is a function address, return the start of code
3931 instead of any data function descriptor. */
f5656ead 3932 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
3933 sym_addr,
3934 &current_target);
3935
3936 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 3937 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
3938 }
3939
dc8acb97 3940 putpkt (msg);
6d820c5c 3941 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3942 reply = rs->buf;
dc8acb97
MS
3943 }
3944}
3945
9db8d71f 3946static struct serial *
baa336ce 3947remote_serial_open (const char *name)
9db8d71f
DJ
3948{
3949 static int udp_warning = 0;
3950
3951 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3952 of in ser-tcp.c, because it is the remote protocol assuming that the
3953 serial connection is reliable and not the serial connection promising
3954 to be. */
61012eef 3955 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 3956 {
3e43a32a
MS
3957 warning (_("The remote protocol may be unreliable over UDP.\n"
3958 "Some events may be lost, rendering further debugging "
3959 "impossible."));
9db8d71f
DJ
3960 udp_warning = 1;
3961 }
3962
3963 return serial_open (name);
3964}
3965
d914c394
SS
3966/* Inform the target of our permission settings. The permission flags
3967 work without this, but if the target knows the settings, it can do
3968 a couple things. First, it can add its own check, to catch cases
3969 that somehow manage to get by the permissions checks in target
3970 methods. Second, if the target is wired to disallow particular
3971 settings (for instance, a system in the field that is not set up to
3972 be able to stop at a breakpoint), it can object to any unavailable
3973 permissions. */
3974
3975void
c378d69d 3976remote_set_permissions (struct target_ops *self)
d914c394
SS
3977{
3978 struct remote_state *rs = get_remote_state ();
3979
bba74b36
YQ
3980 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3981 "WriteReg:%x;WriteMem:%x;"
3982 "InsertBreak:%x;InsertTrace:%x;"
3983 "InsertFastTrace:%x;Stop:%x",
3984 may_write_registers, may_write_memory,
3985 may_insert_breakpoints, may_insert_tracepoints,
3986 may_insert_fast_tracepoints, may_stop);
d914c394
SS
3987 putpkt (rs->buf);
3988 getpkt (&rs->buf, &rs->buf_size, 0);
3989
3990 /* If the target didn't like the packet, warn the user. Do not try
3991 to undo the user's settings, that would just be maddening. */
3992 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 3993 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
3994}
3995
be2a5f71
DJ
3996/* This type describes each known response to the qSupported
3997 packet. */
3998struct protocol_feature
3999{
4000 /* The name of this protocol feature. */
4001 const char *name;
4002
4003 /* The default for this protocol feature. */
4004 enum packet_support default_support;
4005
4006 /* The function to call when this feature is reported, or after
4007 qSupported processing if the feature is not supported.
4008 The first argument points to this structure. The second
4009 argument indicates whether the packet requested support be
4010 enabled, disabled, or probed (or the default, if this function
4011 is being called at the end of processing and this feature was
4012 not reported). The third argument may be NULL; if not NULL, it
4013 is a NUL-terminated string taken from the packet following
4014 this feature's name and an equals sign. */
4015 void (*func) (const struct protocol_feature *, enum packet_support,
4016 const char *);
4017
4018 /* The corresponding packet for this feature. Only used if
4019 FUNC is remote_supported_packet. */
4020 int packet;
4021};
4022
be2a5f71
DJ
4023static void
4024remote_supported_packet (const struct protocol_feature *feature,
4025 enum packet_support support,
4026 const char *argument)
4027{
4028 if (argument)
4029 {
4030 warning (_("Remote qSupported response supplied an unexpected value for"
4031 " \"%s\"."), feature->name);
4032 return;
4033 }
4034
4082afcc 4035 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4036}
be2a5f71
DJ
4037
4038static void
4039remote_packet_size (const struct protocol_feature *feature,
4040 enum packet_support support, const char *value)
4041{
4042 struct remote_state *rs = get_remote_state ();
4043
4044 int packet_size;
4045 char *value_end;
4046
4047 if (support != PACKET_ENABLE)
4048 return;
4049
4050 if (value == NULL || *value == '\0')
4051 {
4052 warning (_("Remote target reported \"%s\" without a size."),
4053 feature->name);
4054 return;
4055 }
4056
4057 errno = 0;
4058 packet_size = strtol (value, &value_end, 16);
4059 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4060 {
4061 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4062 feature->name, value);
4063 return;
4064 }
4065
4066 if (packet_size > MAX_REMOTE_PACKET_SIZE)
4067 {
4068 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
4069 packet_size, MAX_REMOTE_PACKET_SIZE);
4070 packet_size = MAX_REMOTE_PACKET_SIZE;
4071 }
4072
4073 /* Record the new maximum packet size. */
4074 rs->explicit_packet_size = packet_size;
4075}
4076
dc473cfb 4077static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4078 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4079 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4080 PACKET_qXfer_auxv },
c78fa86a
GB
4081 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4082 PACKET_qXfer_exec_file },
23181151
DJ
4083 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4084 PACKET_qXfer_features },
cfa9d6d9
DJ
4085 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4086 PACKET_qXfer_libraries },
2268b414
JK
4087 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4088 PACKET_qXfer_libraries_svr4 },
ced63ec0 4089 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4090 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4091 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4092 PACKET_qXfer_memory_map },
4de6483e
UW
4093 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4094 PACKET_qXfer_spu_read },
4095 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4096 PACKET_qXfer_spu_write },
07e059b5
VP
4097 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4098 PACKET_qXfer_osdata },
dc146f7c
VP
4099 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4100 PACKET_qXfer_threads },
b3b9301e
PA
4101 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4102 PACKET_qXfer_traceframe_info },
89be2091
DJ
4103 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4104 PACKET_QPassSignals },
9b224c5e
PA
4105 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4106 PACKET_QProgramSignals },
a6f3e723
SL
4107 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4108 PACKET_QStartNoAckMode },
4082afcc
PA
4109 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4110 PACKET_multiprocess_feature },
4111 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4112 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4113 PACKET_qXfer_siginfo_read },
4114 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4115 PACKET_qXfer_siginfo_write },
4082afcc 4116 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4117 PACKET_ConditionalTracepoints },
4082afcc 4118 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4119 PACKET_ConditionalBreakpoints },
4082afcc 4120 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4121 PACKET_BreakpointCommands },
4082afcc 4122 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4123 PACKET_FastTracepoints },
4082afcc 4124 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4125 PACKET_StaticTracepoints },
4082afcc 4126 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4127 PACKET_InstallInTrace},
4082afcc
PA
4128 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4129 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4130 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4131 PACKET_bc },
4132 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4133 PACKET_bs },
409873ef
SS
4134 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4135 PACKET_TracepointSource },
d914c394
SS
4136 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4137 PACKET_QAllow },
4082afcc
PA
4138 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4139 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4140 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4141 PACKET_qXfer_fdpic },
169081d0
TG
4142 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4143 PACKET_qXfer_uib },
03583c20
UW
4144 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4145 PACKET_QDisableRandomization },
d1feda86 4146 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4147 { "QTBuffer:size", PACKET_DISABLE,
4148 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4149 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4150 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4151 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
b20a6524 4152 { "Qbtrace:pt", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_pt },
9accd112 4153 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4154 PACKET_qXfer_btrace },
4155 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4156 PACKET_qXfer_btrace_conf },
4157 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4158 PACKET_Qbtrace_conf_bts_size },
4159 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4160 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4161 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4162 PACKET_fork_event_feature },
4163 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4164 PACKET_vfork_event_feature },
b20a6524
MM
4165 { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
4166 PACKET_Qbtrace_conf_pt_size }
be2a5f71
DJ
4167};
4168
c8d5aac9
L
4169static char *remote_support_xml;
4170
4171/* Register string appended to "xmlRegisters=" in qSupported query. */
4172
4173void
6e39997a 4174register_remote_support_xml (const char *xml)
c8d5aac9
L
4175{
4176#if defined(HAVE_LIBEXPAT)
4177 if (remote_support_xml == NULL)
c4f7c687 4178 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4179 else
4180 {
4181 char *copy = xstrdup (remote_support_xml + 13);
4182 char *p = strtok (copy, ",");
4183
4184 do
4185 {
4186 if (strcmp (p, xml) == 0)
4187 {
4188 /* already there */
4189 xfree (copy);
4190 return;
4191 }
4192 }
4193 while ((p = strtok (NULL, ",")) != NULL);
4194 xfree (copy);
4195
94b0dee1
PA
4196 remote_support_xml = reconcat (remote_support_xml,
4197 remote_support_xml, ",", xml,
4198 (char *) NULL);
c8d5aac9
L
4199 }
4200#endif
4201}
4202
4203static char *
4204remote_query_supported_append (char *msg, const char *append)
4205{
4206 if (msg)
94b0dee1 4207 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
4208 else
4209 return xstrdup (append);
4210}
4211
be2a5f71
DJ
4212static void
4213remote_query_supported (void)
4214{
4215 struct remote_state *rs = get_remote_state ();
4216 char *next;
4217 int i;
4218 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4219
4220 /* The packet support flags are handled differently for this packet
4221 than for most others. We treat an error, a disabled packet, and
4222 an empty response identically: any features which must be reported
4223 to be used will be automatically disabled. An empty buffer
4224 accomplishes this, since that is also the representation for a list
4225 containing no features. */
4226
4227 rs->buf[0] = 0;
4082afcc 4228 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 4229 {
c8d5aac9 4230 char *q = NULL;
94b0dee1 4231 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 4232
901f9912 4233 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9 4234
f7e6eed5
PA
4235 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4236 q = remote_query_supported_append (q, "swbreak+");
4237 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4238 q = remote_query_supported_append (q, "hwbreak+");
4239
c8d5aac9
L
4240 if (remote_support_xml)
4241 q = remote_query_supported_append (q, remote_support_xml);
4242
dde08ee1
PA
4243 q = remote_query_supported_append (q, "qRelocInsn+");
4244
89245bc0
DB
4245 if (rs->extended)
4246 {
4247 if (packet_set_cmd_state (PACKET_fork_event_feature)
4248 != AUTO_BOOLEAN_FALSE)
4249 q = remote_query_supported_append (q, "fork-events+");
4250 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4251 != AUTO_BOOLEAN_FALSE)
4252 q = remote_query_supported_append (q, "vfork-events+");
4253 }
4254
dde08ee1
PA
4255 q = reconcat (q, "qSupported:", q, (char *) NULL);
4256 putpkt (q);
82f73884 4257
94b0dee1
PA
4258 do_cleanups (old_chain);
4259
be2a5f71
DJ
4260 getpkt (&rs->buf, &rs->buf_size, 0);
4261
4262 /* If an error occured, warn, but do not return - just reset the
4263 buffer to empty and go on to disable features. */
4264 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4265 == PACKET_ERROR)
4266 {
4267 warning (_("Remote failure reply: %s"), rs->buf);
4268 rs->buf[0] = 0;
4269 }
4270 }
4271
4272 memset (seen, 0, sizeof (seen));
4273
4274 next = rs->buf;
4275 while (*next)
4276 {
4277 enum packet_support is_supported;
4278 char *p, *end, *name_end, *value;
4279
4280 /* First separate out this item from the rest of the packet. If
4281 there's another item after this, we overwrite the separator
4282 (terminated strings are much easier to work with). */
4283 p = next;
4284 end = strchr (p, ';');
4285 if (end == NULL)
4286 {
4287 end = p + strlen (p);
4288 next = end;
4289 }
4290 else
4291 {
89be2091
DJ
4292 *end = '\0';
4293 next = end + 1;
4294
be2a5f71
DJ
4295 if (end == p)
4296 {
4297 warning (_("empty item in \"qSupported\" response"));
4298 continue;
4299 }
be2a5f71
DJ
4300 }
4301
4302 name_end = strchr (p, '=');
4303 if (name_end)
4304 {
4305 /* This is a name=value entry. */
4306 is_supported = PACKET_ENABLE;
4307 value = name_end + 1;
4308 *name_end = '\0';
4309 }
4310 else
4311 {
4312 value = NULL;
4313 switch (end[-1])
4314 {
4315 case '+':
4316 is_supported = PACKET_ENABLE;
4317 break;
4318
4319 case '-':
4320 is_supported = PACKET_DISABLE;
4321 break;
4322
4323 case '?':
4324 is_supported = PACKET_SUPPORT_UNKNOWN;
4325 break;
4326
4327 default:
3e43a32a
MS
4328 warning (_("unrecognized item \"%s\" "
4329 "in \"qSupported\" response"), p);
be2a5f71
DJ
4330 continue;
4331 }
4332 end[-1] = '\0';
4333 }
4334
4335 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4336 if (strcmp (remote_protocol_features[i].name, p) == 0)
4337 {
4338 const struct protocol_feature *feature;
4339
4340 seen[i] = 1;
4341 feature = &remote_protocol_features[i];
4342 feature->func (feature, is_supported, value);
4343 break;
4344 }
4345 }
4346
4347 /* If we increased the packet size, make sure to increase the global
4348 buffer size also. We delay this until after parsing the entire
4349 qSupported packet, because this is the same buffer we were
4350 parsing. */
4351 if (rs->buf_size < rs->explicit_packet_size)
4352 {
4353 rs->buf_size = rs->explicit_packet_size;
4354 rs->buf = xrealloc (rs->buf, rs->buf_size);
4355 }
4356
4357 /* Handle the defaults for unmentioned features. */
4358 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4359 if (!seen[i])
4360 {
4361 const struct protocol_feature *feature;
4362
4363 feature = &remote_protocol_features[i];
4364 feature->func (feature, feature->default_support, NULL);
4365 }
4366}
4367
78a095c3
JK
4368/* Remove any of the remote.c targets from target stack. Upper targets depend
4369 on it so remove them first. */
4370
4371static void
4372remote_unpush_target (void)
4373{
4374 pop_all_targets_above (process_stratum - 1);
4375}
be2a5f71 4376
c906108c 4377static void
014f9477 4378remote_open_1 (const char *name, int from_tty,
3e43a32a 4379 struct target_ops *target, int extended_p)
c906108c 4380{
d01949b6 4381 struct remote_state *rs = get_remote_state ();
a6f3e723 4382
c906108c 4383 if (name == 0)
8a3fe4f8 4384 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 4385 "serial device is attached to the remote system\n"
8a3fe4f8 4386 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 4387
23860348 4388 /* See FIXME above. */
c6ebd6cf 4389 if (!target_async_permitted)
92d1e331 4390 wait_forever_enabled_p = 1;
6426a772 4391
2d717e4f 4392 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
4393 Ask this question first, before target_preopen has a chance to kill
4394 anything. */
5d93a237 4395 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 4396 {
78a095c3
JK
4397 if (from_tty
4398 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
4399 error (_("Still connected."));
4400 }
4401
78a095c3 4402 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
4403 target_preopen (from_tty);
4404
89be2091 4405 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
4406 xfree (rs->last_pass_packet);
4407 rs->last_pass_packet = NULL;
89be2091 4408
9b224c5e
PA
4409 /* Make sure we send the program signals list the next time we
4410 resume. */
5e4a05c4
TT
4411 xfree (rs->last_program_signals_packet);
4412 rs->last_program_signals_packet = NULL;
9b224c5e 4413
ad9a8f3f 4414 remote_fileio_reset ();
1dd41f16
NS
4415 reopen_exec_file ();
4416 reread_symbols ();
4417
5d93a237
TT
4418 rs->remote_desc = remote_serial_open (name);
4419 if (!rs->remote_desc)
c906108c
SS
4420 perror_with_name (name);
4421
4422 if (baud_rate != -1)
4423 {
5d93a237 4424 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 4425 {
9b74d5d3
KB
4426 /* The requested speed could not be set. Error out to
4427 top level after closing remote_desc. Take care to
4428 set remote_desc to NULL to avoid closing remote_desc
4429 more than once. */
5d93a237
TT
4430 serial_close (rs->remote_desc);
4431 rs->remote_desc = NULL;
c906108c
SS
4432 perror_with_name (name);
4433 }
4434 }
4435
236af5e3 4436 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 4437 serial_raw (rs->remote_desc);
c906108c
SS
4438
4439 /* If there is something sitting in the buffer we might take it as a
4440 response to a command, which would be bad. */
5d93a237 4441 serial_flush_input (rs->remote_desc);
c906108c
SS
4442
4443 if (from_tty)
4444 {
4445 puts_filtered ("Remote debugging using ");
4446 puts_filtered (name);
4447 puts_filtered ("\n");
4448 }
23860348 4449 push_target (target); /* Switch to using remote target now. */
c906108c 4450
74531fed
PA
4451 /* Register extra event sources in the event loop. */
4452 remote_async_inferior_event_token
4453 = create_async_event_handler (remote_async_inferior_event_handler,
4454 NULL);
5965e028 4455 rs->notif_state = remote_notif_state_allocate ();
74531fed 4456
be2a5f71
DJ
4457 /* Reset the target state; these things will be queried either by
4458 remote_query_supported or as they are needed. */
ca4f7f8b 4459 reset_all_packet_configs_support ();
74531fed 4460 rs->cached_wait_status = 0;
be2a5f71 4461 rs->explicit_packet_size = 0;
a6f3e723 4462 rs->noack_mode = 0;
82f73884 4463 rs->extended = extended_p;
e24a49d8 4464 rs->waiting_for_stop_reply = 0;
3a29589a 4465 rs->ctrlc_pending_p = 0;
802188a7 4466
47f8a51d
TT
4467 rs->general_thread = not_sent_ptid;
4468 rs->continue_thread = not_sent_ptid;
262e1174 4469 rs->remote_traceframe_number = -1;
c906108c 4470
9d1f7ab2 4471 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
4472 rs->use_threadinfo_query = 1;
4473 rs->use_threadextra_query = 1;
9d1f7ab2 4474
c6ebd6cf 4475 if (target_async_permitted)
92d1e331 4476 {
23860348 4477 /* With this target we start out by owning the terminal. */
92d1e331
DJ
4478 remote_async_terminal_ours_p = 1;
4479
4480 /* FIXME: cagney/1999-09-23: During the initial connection it is
4481 assumed that the target is already ready and able to respond to
0df8b418 4482 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 4483 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 4484 around this. Eventually a mechanism that allows
92d1e331 4485 wait_for_inferior() to expect/get timeouts will be
23860348 4486 implemented. */
92d1e331
DJ
4487 wait_forever_enabled_p = 0;
4488 }
4489
23860348 4490 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 4491 no_shared_libraries (NULL, 0);
f78f6cf1 4492
74531fed
PA
4493 /* Start afresh. */
4494 init_thread_list ();
4495
36918e70 4496 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
4497 target (we'd otherwise be in an inconsistent state) and then
4498 propogate the error on up the exception chain. This ensures that
4499 the caller doesn't stumble along blindly assuming that the
4500 function succeeded. The CLI doesn't have this problem but other
4501 UI's, such as MI do.
36918e70
AC
4502
4503 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4504 this function should return an error indication letting the
ce2826aa 4505 caller restore the previous state. Unfortunately the command
36918e70
AC
4506 ``target remote'' is directly wired to this function making that
4507 impossible. On a positive note, the CLI side of this problem has
4508 been fixed - the function set_cmd_context() makes it possible for
4509 all the ``target ....'' commands to share a common callback
4510 function. See cli-dump.c. */
109c3e39 4511 {
2d717e4f 4512
492d29ea 4513 TRY
04bd08de
TT
4514 {
4515 remote_start_remote (from_tty, target, extended_p);
4516 }
492d29ea 4517 CATCH (ex, RETURN_MASK_ALL)
109c3e39 4518 {
c8d104ad
PA
4519 /* Pop the partially set up target - unless something else did
4520 already before throwing the exception. */
5d93a237 4521 if (rs->remote_desc != NULL)
78a095c3 4522 remote_unpush_target ();
c6ebd6cf 4523 if (target_async_permitted)
109c3e39
AC
4524 wait_forever_enabled_p = 1;
4525 throw_exception (ex);
4526 }
492d29ea 4527 END_CATCH
109c3e39 4528 }
c906108c 4529
f4abbc16
MM
4530 remote_btrace_reset ();
4531
c6ebd6cf 4532 if (target_async_permitted)
92d1e331 4533 wait_forever_enabled_p = 1;
43ff13b4
JM
4534}
4535
de0d863e
DB
4536/* Detach the specified process. */
4537
4538static void
4539remote_detach_pid (int pid)
4540{
4541 struct remote_state *rs = get_remote_state ();
4542
4543 if (remote_multi_process_p (rs))
4544 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4545 else
4546 strcpy (rs->buf, "D");
4547
4548 putpkt (rs->buf);
4549 getpkt (&rs->buf, &rs->buf_size, 0);
4550
4551 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4552 ;
4553 else if (rs->buf[0] == '\0')
4554 error (_("Remote doesn't know how to detach"));
4555 else
4556 error (_("Can't detach process."));
4557}
4558
4559/* This detaches a program to which we previously attached, using
4560 inferior_ptid to identify the process. After this is done, GDB
4561 can be used to debug some other program. We better not have left
4562 any breakpoints in the target program or it'll die when it hits
4563 one. */
c906108c
SS
4564
4565static void
de0d863e 4566remote_detach_1 (const char *args, int from_tty)
c906108c 4567{
82f73884 4568 int pid = ptid_get_pid (inferior_ptid);
d01949b6 4569 struct remote_state *rs = get_remote_state ();
de0d863e
DB
4570 struct thread_info *tp = find_thread_ptid (inferior_ptid);
4571 int is_fork_parent;
c906108c
SS
4572
4573 if (args)
8a3fe4f8 4574 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 4575
2d717e4f
DJ
4576 if (!target_has_execution)
4577 error (_("No process to detach from."));
4578
7cee1e54
PA
4579 if (from_tty)
4580 {
4581 char *exec_file = get_exec_file (0);
4582 if (exec_file == NULL)
4583 exec_file = "";
4584 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4585 target_pid_to_str (pid_to_ptid (pid)));
4586 gdb_flush (gdb_stdout);
4587 }
4588
c906108c 4589 /* Tell the remote target to detach. */
de0d863e 4590 remote_detach_pid (pid);
82f73884 4591
de0d863e 4592 if (from_tty && !rs->extended)
7cee1e54 4593 puts_filtered (_("Ending remote debugging.\n"));
82f73884 4594
de0d863e
DB
4595 /* Check to see if we are detaching a fork parent. Note that if we
4596 are detaching a fork child, tp == NULL. */
4597 is_fork_parent = (tp != NULL
4598 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
4599
4600 /* If doing detach-on-fork, we don't mourn, because that will delete
4601 breakpoints that should be available for the followed inferior. */
4602 if (!is_fork_parent)
4603 target_mourn_inferior ();
4604 else
4605 {
4606 inferior_ptid = null_ptid;
4607 detach_inferior (pid);
4608 }
2d717e4f
DJ
4609}
4610
4611static void
52554a0e 4612remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4613{
de0d863e 4614 remote_detach_1 (args, from_tty);
2d717e4f
DJ
4615}
4616
4617static void
52554a0e 4618extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4619{
de0d863e
DB
4620 remote_detach_1 (args, from_tty);
4621}
4622
4623/* Target follow-fork function for remote targets. On entry, and
4624 at return, the current inferior is the fork parent.
4625
4626 Note that although this is currently only used for extended-remote,
4627 it is named remote_follow_fork in anticipation of using it for the
4628 remote target as well. */
4629
4630static int
4631remote_follow_fork (struct target_ops *ops, int follow_child,
4632 int detach_fork)
4633{
4634 struct remote_state *rs = get_remote_state ();
c269dbdb 4635 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 4636
c269dbdb
DB
4637 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
4638 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
4639 {
4640 /* When following the parent and detaching the child, we detach
4641 the child here. For the case of following the child and
4642 detaching the parent, the detach is done in the target-
4643 independent follow fork code in infrun.c. We can't use
4644 target_detach when detaching an unfollowed child because
4645 the client side doesn't know anything about the child. */
4646 if (detach_fork && !follow_child)
4647 {
4648 /* Detach the fork child. */
4649 ptid_t child_ptid;
4650 pid_t child_pid;
4651
4652 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
4653 child_pid = ptid_get_pid (child_ptid);
4654
4655 remote_detach_pid (child_pid);
4656 detach_inferior (child_pid);
4657 }
4658 }
4659 return 0;
c906108c
SS
4660}
4661
6ad8ae5c
DJ
4662/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4663
43ff13b4 4664static void
fee354ee 4665remote_disconnect (struct target_ops *target, const char *args, int from_tty)
43ff13b4 4666{
43ff13b4 4667 if (args)
2d717e4f 4668 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 4669
2d717e4f 4670 /* Make sure we unpush even the extended remote targets; mourn
20f796c9 4671 won't do it. So call remote_mourn directly instead of
2d717e4f 4672 target_mourn_inferior. */
20f796c9 4673 remote_mourn (target);
2d717e4f 4674
43ff13b4
JM
4675 if (from_tty)
4676 puts_filtered ("Ending remote debugging.\n");
4677}
4678
2d717e4f
DJ
4679/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4680 be chatty about it. */
4681
4682static void
20f796c9
GB
4683extended_remote_attach (struct target_ops *target, const char *args,
4684 int from_tty)
2d717e4f
DJ
4685{
4686 struct remote_state *rs = get_remote_state ();
be86555c 4687 int pid;
96ef3384 4688 char *wait_status = NULL;
2d717e4f 4689
74164c56 4690 pid = parse_pid_to_attach (args);
2d717e4f 4691
74164c56
JK
4692 /* Remote PID can be freely equal to getpid, do not check it here the same
4693 way as in other targets. */
2d717e4f 4694
4082afcc 4695 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
4696 error (_("This target does not support attaching to a process"));
4697
7cee1e54
PA
4698 if (from_tty)
4699 {
4700 char *exec_file = get_exec_file (0);
4701
4702 if (exec_file)
4703 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4704 target_pid_to_str (pid_to_ptid (pid)));
4705 else
4706 printf_unfiltered (_("Attaching to %s\n"),
4707 target_pid_to_str (pid_to_ptid (pid)));
4708
4709 gdb_flush (gdb_stdout);
4710 }
4711
bba74b36 4712 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
4713 putpkt (rs->buf);
4714 getpkt (&rs->buf, &rs->buf_size, 0);
4715
4082afcc
PA
4716 switch (packet_ok (rs->buf,
4717 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 4718 {
4082afcc 4719 case PACKET_OK:
74531fed
PA
4720 if (!non_stop)
4721 {
4722 /* Save the reply for later. */
4723 wait_status = alloca (strlen (rs->buf) + 1);
4724 strcpy (wait_status, rs->buf);
4725 }
4726 else if (strcmp (rs->buf, "OK") != 0)
4727 error (_("Attaching to %s failed with: %s"),
4728 target_pid_to_str (pid_to_ptid (pid)),
4729 rs->buf);
4082afcc
PA
4730 break;
4731 case PACKET_UNKNOWN:
4732 error (_("This target does not support attaching to a process"));
4733 default:
4734 error (_("Attaching to %s failed"),
4735 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 4736 }
2d717e4f 4737
1b6e6f5c 4738 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 4739
2d717e4f 4740 inferior_ptid = pid_to_ptid (pid);
79d7f229 4741
bad34192
PA
4742 if (non_stop)
4743 {
4744 struct thread_info *thread;
79d7f229 4745
bad34192 4746 /* Get list of threads. */
e8032dde 4747 remote_update_thread_list (target);
82f73884 4748
bad34192
PA
4749 thread = first_thread_of_process (pid);
4750 if (thread)
4751 inferior_ptid = thread->ptid;
4752 else
4753 inferior_ptid = pid_to_ptid (pid);
4754
4755 /* Invalidate our notion of the remote current thread. */
47f8a51d 4756 record_currthread (rs, minus_one_ptid);
bad34192 4757 }
74531fed 4758 else
bad34192
PA
4759 {
4760 /* Now, if we have thread information, update inferior_ptid. */
4761 inferior_ptid = remote_current_thread (inferior_ptid);
4762
4763 /* Add the main thread to the thread list. */
4764 add_thread_silent (inferior_ptid);
4765 }
c0a2216e 4766
96ef3384
UW
4767 /* Next, if the target can specify a description, read it. We do
4768 this before anything involving memory or registers. */
4769 target_find_description ();
4770
74531fed
PA
4771 if (!non_stop)
4772 {
4773 /* Use the previously fetched status. */
4774 gdb_assert (wait_status != NULL);
4775
4776 if (target_can_async_p ())
4777 {
722247f1
YQ
4778 struct notif_event *reply
4779 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 4780
722247f1 4781 push_stop_reply ((struct stop_reply *) reply);
74531fed 4782
6a3753b3 4783 target_async (1);
74531fed
PA
4784 }
4785 else
4786 {
4787 gdb_assert (wait_status != NULL);
4788 strcpy (rs->buf, wait_status);
4789 rs->cached_wait_status = 1;
4790 }
4791 }
4792 else
4793 gdb_assert (wait_status == NULL);
2d717e4f
DJ
4794}
4795
b9c1d481
AS
4796/* Implementation of the to_post_attach method. */
4797
4798static void
4799extended_remote_post_attach (struct target_ops *ops, int pid)
4800{
4801 /* In certain cases GDB might not have had the chance to start
4802 symbol lookup up until now. This could happen if the debugged
4803 binary is not using shared libraries, the vsyscall page is not
4804 present (on Linux) and the binary itself hadn't changed since the
4805 debugging process was started. */
4806 if (symfile_objfile != NULL)
4807 remote_check_symbols();
4808}
4809
c906108c 4810\f
506fb367
DJ
4811/* Check for the availability of vCont. This function should also check
4812 the response. */
c906108c
SS
4813
4814static void
6d820c5c 4815remote_vcont_probe (struct remote_state *rs)
c906108c 4816{
2e9f7625 4817 char *buf;
6d820c5c 4818
2e9f7625
DJ
4819 strcpy (rs->buf, "vCont?");
4820 putpkt (rs->buf);
6d820c5c 4821 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 4822 buf = rs->buf;
c906108c 4823
506fb367 4824 /* Make sure that the features we assume are supported. */
61012eef 4825 if (startswith (buf, "vCont"))
506fb367
DJ
4826 {
4827 char *p = &buf[5];
4828 int support_s, support_S, support_c, support_C;
4829
4830 support_s = 0;
4831 support_S = 0;
4832 support_c = 0;
4833 support_C = 0;
d458bd84 4834 rs->supports_vCont.t = 0;
c1e36e3e 4835 rs->supports_vCont.r = 0;
506fb367
DJ
4836 while (p && *p == ';')
4837 {
4838 p++;
4839 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4840 support_s = 1;
4841 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4842 support_S = 1;
4843 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4844 support_c = 1;
4845 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4846 support_C = 1;
74531fed 4847 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 4848 rs->supports_vCont.t = 1;
c1e36e3e
PA
4849 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
4850 rs->supports_vCont.r = 1;
506fb367
DJ
4851
4852 p = strchr (p, ';');
4853 }
c906108c 4854
506fb367
DJ
4855 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4856 BUF will make packet_ok disable the packet. */
4857 if (!support_s || !support_S || !support_c || !support_C)
4858 buf[0] = 0;
4859 }
c906108c 4860
444abaca 4861 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 4862}
c906108c 4863
0d8f58ca
PA
4864/* Helper function for building "vCont" resumptions. Write a
4865 resumption to P. ENDP points to one-passed-the-end of the buffer
4866 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4867 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4868 resumed thread should be single-stepped and/or signalled. If PTID
4869 equals minus_one_ptid, then all threads are resumed; if PTID
4870 represents a process, then all threads of the process are resumed;
4871 the thread to be stepped and/or signalled is given in the global
4872 INFERIOR_PTID. */
4873
4874static char *
4875append_resumption (char *p, char *endp,
2ea28649 4876 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
4877{
4878 struct remote_state *rs = get_remote_state ();
4879
a493e3e2 4880 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 4881 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
4882 else if (step
4883 /* GDB is willing to range step. */
4884 && use_range_stepping
4885 /* Target supports range stepping. */
4886 && rs->supports_vCont.r
4887 /* We don't currently support range stepping multiple
4888 threads with a wildcard (though the protocol allows it,
4889 so stubs shouldn't make an active effort to forbid
4890 it). */
4891 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4892 {
4893 struct thread_info *tp;
4894
4895 if (ptid_equal (ptid, minus_one_ptid))
4896 {
4897 /* If we don't know about the target thread's tid, then
4898 we're resuming magic_null_ptid (see caller). */
4899 tp = find_thread_ptid (magic_null_ptid);
4900 }
4901 else
4902 tp = find_thread_ptid (ptid);
4903 gdb_assert (tp != NULL);
4904
4905 if (tp->control.may_range_step)
4906 {
4907 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
4908
4909 p += xsnprintf (p, endp - p, ";r%s,%s",
4910 phex_nz (tp->control.step_range_start,
4911 addr_size),
4912 phex_nz (tp->control.step_range_end,
4913 addr_size));
4914 }
4915 else
4916 p += xsnprintf (p, endp - p, ";s");
4917 }
0d8f58ca
PA
4918 else if (step)
4919 p += xsnprintf (p, endp - p, ";s");
a493e3e2 4920 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
4921 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4922 else
4923 p += xsnprintf (p, endp - p, ";c");
4924
4925 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4926 {
4927 ptid_t nptid;
4928
4929 /* All (-1) threads of process. */
ba348170 4930 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
4931
4932 p += xsnprintf (p, endp - p, ":");
4933 p = write_ptid (p, endp, nptid);
4934 }
4935 else if (!ptid_equal (ptid, minus_one_ptid))
4936 {
4937 p += xsnprintf (p, endp - p, ":");
4938 p = write_ptid (p, endp, ptid);
4939 }
4940
4941 return p;
4942}
4943
e5ef252a
PA
4944/* Append a vCont continue-with-signal action for threads that have a
4945 non-zero stop signal. */
4946
4947static char *
4948append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4949{
4950 struct thread_info *thread;
4951
034f788c 4952 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
4953 if (ptid_match (thread->ptid, ptid)
4954 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 4955 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
4956 {
4957 p = append_resumption (p, endp, thread->ptid,
4958 0, thread->suspend.stop_signal);
4959 thread->suspend.stop_signal = GDB_SIGNAL_0;
4960 }
4961
4962 return p;
4963}
4964
506fb367
DJ
4965/* Resume the remote inferior by using a "vCont" packet. The thread
4966 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
4967 resumed thread should be single-stepped and/or signalled. If PTID
4968 equals minus_one_ptid, then all threads are resumed; the thread to
4969 be stepped and/or signalled is given in the global INFERIOR_PTID.
4970 This function returns non-zero iff it resumes the inferior.
44eaed12 4971
506fb367
DJ
4972 This function issues a strict subset of all possible vCont commands at the
4973 moment. */
44eaed12 4974
506fb367 4975static int
2ea28649 4976remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
4977{
4978 struct remote_state *rs = get_remote_state ();
82f73884
PA
4979 char *p;
4980 char *endp;
44eaed12 4981
4082afcc 4982 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 4983 remote_vcont_probe (rs);
44eaed12 4984
4082afcc 4985 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 4986 return 0;
44eaed12 4987
82f73884
PA
4988 p = rs->buf;
4989 endp = rs->buf + get_remote_packet_size ();
4990
506fb367
DJ
4991 /* If we could generate a wider range of packets, we'd have to worry
4992 about overflowing BUF. Should there be a generic
4993 "multi-part-packet" packet? */
4994
0d8f58ca
PA
4995 p += xsnprintf (p, endp - p, "vCont");
4996
79d7f229 4997 if (ptid_equal (ptid, magic_null_ptid))
c906108c 4998 {
79d7f229
PA
4999 /* MAGIC_NULL_PTID means that we don't have any active threads,
5000 so we don't have any TID numbers the inferior will
5001 understand. Make sure to only send forms that do not specify
5002 a TID. */
a9cbf802 5003 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 5004 }
0d8f58ca 5005 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 5006 {
0d8f58ca
PA
5007 /* Resume all threads (of all processes, or of a single
5008 process), with preference for INFERIOR_PTID. This assumes
5009 inferior_ptid belongs to the set of all threads we are about
5010 to resume. */
a493e3e2 5011 if (step || siggnal != GDB_SIGNAL_0)
82f73884 5012 {
0d8f58ca
PA
5013 /* Step inferior_ptid, with or without signal. */
5014 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5015 }
0d8f58ca 5016
e5ef252a
PA
5017 /* Also pass down any pending signaled resumption for other
5018 threads not the current. */
5019 p = append_pending_thread_resumptions (p, endp, ptid);
5020
0d8f58ca 5021 /* And continue others without a signal. */
a493e3e2 5022 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5023 }
5024 else
506fb367
DJ
5025 {
5026 /* Scheduler locking; resume only PTID. */
a9cbf802 5027 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5028 }
c906108c 5029
82f73884
PA
5030 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5031 putpkt (rs->buf);
506fb367 5032
74531fed
PA
5033 if (non_stop)
5034 {
5035 /* In non-stop, the stub replies to vCont with "OK". The stop
5036 reply will be reported asynchronously by means of a `%Stop'
5037 notification. */
5038 getpkt (&rs->buf, &rs->buf_size, 0);
5039 if (strcmp (rs->buf, "OK") != 0)
5040 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5041 }
5042
506fb367 5043 return 1;
c906108c 5044}
43ff13b4 5045
506fb367
DJ
5046/* Tell the remote machine to resume. */
5047
43ff13b4 5048static void
28439f5e 5049remote_resume (struct target_ops *ops,
2ea28649 5050 ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 5051{
d01949b6 5052 struct remote_state *rs = get_remote_state ();
2e9f7625 5053 char *buf;
43ff13b4 5054
722247f1
YQ
5055 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
5056 (explained in remote-notif.c:handle_notification) so
5057 remote_notif_process is not called. We need find a place where
5058 it is safe to start a 'vNotif' sequence. It is good to do it
5059 before resuming inferior, because inferior was stopped and no RSP
5060 traffic at that moment. */
5061 if (!non_stop)
5965e028 5062 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 5063
b73be471 5064 rs->last_sent_signal = siggnal;
280ceea3 5065 rs->last_sent_step = step;
43ff13b4 5066
506fb367 5067 /* The vCont packet doesn't need to specify threads via Hc. */
40ab02ce
MS
5068 /* No reverse support (yet) for vCont. */
5069 if (execution_direction != EXEC_REVERSE)
5070 if (remote_vcont_resume (ptid, step, siggnal))
5071 goto done;
506fb367 5072
79d7f229
PA
5073 /* All other supported resume packets do use Hc, so set the continue
5074 thread. */
5075 if (ptid_equal (ptid, minus_one_ptid))
5076 set_continue_thread (any_thread_ptid);
506fb367 5077 else
79d7f229 5078 set_continue_thread (ptid);
506fb367 5079
2e9f7625 5080 buf = rs->buf;
b2175913
MS
5081 if (execution_direction == EXEC_REVERSE)
5082 {
5083 /* We don't pass signals to the target in reverse exec mode. */
a493e3e2 5084 if (info_verbose && siggnal != GDB_SIGNAL_0)
7ea6d463 5085 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
b2175913 5086 siggnal);
40ab02ce 5087
4082afcc 5088 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
40ab02ce 5089 error (_("Remote reverse-step not supported."));
4082afcc 5090 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
08c93ed9 5091 error (_("Remote reverse-continue not supported."));
40ab02ce 5092
b2175913
MS
5093 strcpy (buf, step ? "bs" : "bc");
5094 }
a493e3e2 5095 else if (siggnal != GDB_SIGNAL_0)
43ff13b4
JM
5096 {
5097 buf[0] = step ? 'S' : 'C';
c5aa993b 5098 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 5099 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
5100 buf[3] = '\0';
5101 }
5102 else
c5aa993b 5103 strcpy (buf, step ? "s" : "c");
506fb367 5104
44eaed12 5105 putpkt (buf);
43ff13b4 5106
75c99385 5107 done:
2acceee2 5108 /* We are about to start executing the inferior, let's register it
0df8b418
MS
5109 with the event loop. NOTE: this is the one place where all the
5110 execution commands end up. We could alternatively do this in each
23860348 5111 of the execution commands in infcmd.c. */
2acceee2
JM
5112 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
5113 into infcmd.c in order to allow inferior function calls to work
23860348 5114 NOT asynchronously. */
362646f5 5115 if (target_can_async_p ())
6a3753b3 5116 target_async (1);
e24a49d8
PA
5117
5118 /* We've just told the target to resume. The remote server will
5119 wait for the inferior to stop, and then send a stop reply. In
5120 the mean time, we can't start another command/query ourselves
74531fed
PA
5121 because the stub wouldn't be ready to process it. This applies
5122 only to the base all-stop protocol, however. In non-stop (which
5123 only supports vCont), the stub replies with an "OK", and is
5124 immediate able to process further serial input. */
5125 if (!non_stop)
5126 rs->waiting_for_stop_reply = 1;
43ff13b4 5127}
c906108c 5128\f
43ff13b4
JM
5129
5130/* Set up the signal handler for SIGINT, while the target is
23860348 5131 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 5132static void
934b9bac 5133async_initialize_sigint_signal_handler (void)
43ff13b4 5134{
934b9bac 5135 signal (SIGINT, async_handle_remote_sigint);
43ff13b4
JM
5136}
5137
23860348 5138/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 5139static void
934b9bac 5140async_handle_remote_sigint (int sig)
43ff13b4 5141{
934b9bac 5142 signal (sig, async_handle_remote_sigint_twice);
b2ee242b
PA
5143 /* Note we need to go through gdb_call_async_signal_handler in order
5144 to wake up the event loop on Windows. */
5145 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
43ff13b4
JM
5146}
5147
5148/* Signal handler for SIGINT, installed after SIGINT has already been
5149 sent once. It will take effect the second time that the user sends
23860348 5150 a ^C. */
43ff13b4 5151static void
934b9bac 5152async_handle_remote_sigint_twice (int sig)
43ff13b4 5153{
934b9bac 5154 signal (sig, async_handle_remote_sigint);
b2ee242b
PA
5155 /* See note in async_handle_remote_sigint. */
5156 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
43ff13b4
JM
5157}
5158
6426a772 5159/* Perform the real interruption of the target execution, in response
23860348 5160 to a ^C. */
c5aa993b 5161static void
fba45db2 5162async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
5163{
5164 if (remote_debug)
248fd3bf 5165 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
43ff13b4 5166
94cc34af 5167 target_stop (inferior_ptid);
43ff13b4
JM
5168}
5169
0df8b418 5170/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 5171 up on the target alltogether. */
47e1ce27 5172static void
fba45db2 5173async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 5174{
2df3850c 5175 if (remote_debug)
248fd3bf 5176 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
b803fb0f
DJ
5177
5178 interrupt_query ();
43ff13b4
JM
5179}
5180
5181/* Reinstall the usual SIGINT handlers, after the target has
23860348 5182 stopped. */
6426a772 5183static void
934b9bac 5184async_cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
5185{
5186 signal (SIGINT, handle_sigint);
43ff13b4
JM
5187}
5188
c906108c
SS
5189/* Send ^C to target to halt it. Target will respond, and send us a
5190 packet. */
507f3c78 5191static void (*ofunc) (int);
c906108c 5192
0df8b418
MS
5193/* The command line interface's stop routine. This function is installed
5194 as a signal handler for SIGINT. The first time a user requests a
5195 stop, we call remote_stop to send a break or ^C. If there is no
7a292a7a 5196 response from the target (it didn't stop when the user requested it),
23860348 5197 we ask the user if he'd like to detach from the target. */
c906108c 5198static void
934b9bac 5199sync_remote_interrupt (int signo)
c906108c 5200{
23860348 5201 /* If this doesn't work, try more severe steps. */
934b9bac 5202 signal (signo, sync_remote_interrupt_twice);
7a292a7a 5203
934b9bac 5204 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
7a292a7a
SS
5205}
5206
5207/* The user typed ^C twice. */
5208
5209static void
934b9bac 5210sync_remote_interrupt_twice (int signo)
7a292a7a
SS
5211{
5212 signal (signo, ofunc);
934b9bac
JK
5213 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
5214 signal (signo, sync_remote_interrupt);
c906108c 5215}
7a292a7a 5216
74531fed
PA
5217/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5218 thread, all threads of a remote process, or all threads of all
5219 processes. */
5220
5221static void
5222remote_stop_ns (ptid_t ptid)
5223{
5224 struct remote_state *rs = get_remote_state ();
5225 char *p = rs->buf;
5226 char *endp = rs->buf + get_remote_packet_size ();
74531fed 5227
4082afcc 5228 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
5229 remote_vcont_probe (rs);
5230
d458bd84 5231 if (!rs->supports_vCont.t)
74531fed
PA
5232 error (_("Remote server does not support stopping threads"));
5233
f91d3df5
PA
5234 if (ptid_equal (ptid, minus_one_ptid)
5235 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
5236 p += xsnprintf (p, endp - p, "vCont;t");
5237 else
5238 {
5239 ptid_t nptid;
5240
74531fed
PA
5241 p += xsnprintf (p, endp - p, "vCont;t:");
5242
5243 if (ptid_is_pid (ptid))
5244 /* All (-1) threads of process. */
ba348170 5245 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
5246 else
5247 {
5248 /* Small optimization: if we already have a stop reply for
5249 this thread, no use in telling the stub we want this
5250 stopped. */
5251 if (peek_stop_reply (ptid))
5252 return;
5253
5254 nptid = ptid;
5255 }
5256
a9cbf802 5257 write_ptid (p, endp, nptid);
74531fed
PA
5258 }
5259
5260 /* In non-stop, we get an immediate OK reply. The stop reply will
5261 come in asynchronously by notification. */
5262 putpkt (rs->buf);
5263 getpkt (&rs->buf, &rs->buf_size, 0);
5264 if (strcmp (rs->buf, "OK") != 0)
5265 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
5266}
5267
5268/* All-stop version of target_stop. Sends a break or a ^C to stop the
5269 remote target. It is undefined which thread of which process
5270 reports the stop. */
5271
5272static void
5273remote_stop_as (ptid_t ptid)
5274{
5275 struct remote_state *rs = get_remote_state ();
5276
3a29589a
DJ
5277 rs->ctrlc_pending_p = 1;
5278
74531fed
PA
5279 /* If the inferior is stopped already, but the core didn't know
5280 about it yet, just ignore the request. The cached wait status
5281 will be collected in remote_wait. */
5282 if (rs->cached_wait_status)
5283 return;
5284
9a7071a8
JB
5285 /* Send interrupt_sequence to remote target. */
5286 send_interrupt_sequence ();
74531fed
PA
5287}
5288
0df8b418 5289/* This is the generic stop called via the target vector. When a target
7a292a7a 5290 interrupt is requested, either by the command line or the GUI, we
23860348 5291 will eventually end up here. */
74531fed 5292
c906108c 5293static void
1eab8a48 5294remote_stop (struct target_ops *self, ptid_t ptid)
c906108c 5295{
7a292a7a 5296 if (remote_debug)
0f71a2f6 5297 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 5298
74531fed
PA
5299 if (non_stop)
5300 remote_stop_ns (ptid);
c906108c 5301 else
74531fed 5302 remote_stop_as (ptid);
c906108c
SS
5303}
5304
5305/* Ask the user what to do when an interrupt is received. */
5306
5307static void
fba45db2 5308interrupt_query (void)
c906108c
SS
5309{
5310 target_terminal_ours ();
5311
d9d41e78 5312 if (target_is_async_p ())
c906108c 5313 {
74531fed 5314 signal (SIGINT, handle_sigint);
039e3c22 5315 quit ();
c906108c 5316 }
74531fed
PA
5317 else
5318 {
9e2f0ad4
HZ
5319 if (query (_("Interrupted while waiting for the program.\n\
5320Give up (and stop debugging it)? ")))
74531fed 5321 {
78a095c3 5322 remote_unpush_target ();
039e3c22 5323 quit ();
74531fed
PA
5324 }
5325 }
c906108c
SS
5326
5327 target_terminal_inferior ();
5328}
5329
6426a772
JM
5330/* Enable/disable target terminal ownership. Most targets can use
5331 terminal groups to control terminal ownership. Remote targets are
5332 different in that explicit transfer of ownership to/from GDB/target
23860348 5333 is required. */
6426a772
JM
5334
5335static void
d2f640d4 5336remote_terminal_inferior (struct target_ops *self)
6426a772 5337{
c6ebd6cf 5338 if (!target_async_permitted)
75c99385
PA
5339 /* Nothing to do. */
5340 return;
5341
d9d2d8b6
PA
5342 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5343 idempotent. The event-loop GDB talking to an asynchronous target
5344 with a synchronous command calls this function from both
5345 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5346 transfer the terminal to the target when it shouldn't this guard
5347 can go away. */
6426a772
JM
5348 if (!remote_async_terminal_ours_p)
5349 return;
5350 delete_file_handler (input_fd);
5351 remote_async_terminal_ours_p = 0;
934b9bac 5352 async_initialize_sigint_signal_handler ();
6426a772
JM
5353 /* NOTE: At this point we could also register our selves as the
5354 recipient of all input. Any characters typed could then be
23860348 5355 passed on down to the target. */
6426a772
JM
5356}
5357
5358static void
e3594fd1 5359remote_terminal_ours (struct target_ops *self)
6426a772 5360{
c6ebd6cf 5361 if (!target_async_permitted)
75c99385
PA
5362 /* Nothing to do. */
5363 return;
5364
5365 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
5366 if (remote_async_terminal_ours_p)
5367 return;
934b9bac 5368 async_cleanup_sigint_signal_handler (NULL);
6426a772
JM
5369 add_file_handler (input_fd, stdin_event_handler, 0);
5370 remote_async_terminal_ours_p = 1;
5371}
5372
176a6961 5373static void
917317f4 5374remote_console_output (char *msg)
c906108c
SS
5375{
5376 char *p;
5377
c5aa993b 5378 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
5379 {
5380 char tb[2];
5381 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 5382
c906108c
SS
5383 tb[0] = c;
5384 tb[1] = 0;
43ff13b4 5385 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 5386 }
00db5b94
PA
5387 gdb_flush (gdb_stdtarg);
5388}
74531fed
PA
5389
5390typedef struct cached_reg
5391{
5392 int num;
5393 gdb_byte data[MAX_REGISTER_SIZE];
5394} cached_reg_t;
5395
5396DEF_VEC_O(cached_reg_t);
5397
722247f1 5398typedef struct stop_reply
74531fed 5399{
722247f1 5400 struct notif_event base;
74531fed 5401
722247f1 5402 /* The identifier of the thread about this event */
74531fed
PA
5403 ptid_t ptid;
5404
340e3c99 5405 /* The remote state this event is associated with. When the remote
bcc75809
YQ
5406 connection, represented by a remote_state object, is closed,
5407 all the associated stop_reply events should be released. */
5408 struct remote_state *rs;
5409
74531fed
PA
5410 struct target_waitstatus ws;
5411
15148d6a
PA
5412 /* Expedited registers. This makes remote debugging a bit more
5413 efficient for those targets that provide critical registers as
5414 part of their normal status mechanism (as another roundtrip to
5415 fetch them is avoided). */
74531fed
PA
5416 VEC(cached_reg_t) *regcache;
5417
f7e6eed5
PA
5418 enum target_stop_reason stop_reason;
5419
74531fed
PA
5420 CORE_ADDR watch_data_address;
5421
dc146f7c 5422 int core;
722247f1 5423} *stop_reply_p;
a744cf53 5424
722247f1
YQ
5425DECLARE_QUEUE_P (stop_reply_p);
5426DEFINE_QUEUE_P (stop_reply_p);
5427/* The list of already fetched and acknowledged stop events. This
5428 queue is used for notification Stop, and other notifications
5429 don't need queue for their events, because the notification events
5430 of Stop can't be consumed immediately, so that events should be
5431 queued first, and be consumed by remote_wait_{ns,as} one per
5432 time. Other notifications can consume their events immediately,
5433 so queue is not needed for them. */
5434static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
5435
5436static void
5437stop_reply_xfree (struct stop_reply *r)
5438{
f48ff2a7 5439 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
5440}
5441
722247f1
YQ
5442static void
5443remote_notif_stop_parse (struct notif_client *self, char *buf,
5444 struct notif_event *event)
5445{
5446 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5447}
5448
5449static void
5450remote_notif_stop_ack (struct notif_client *self, char *buf,
5451 struct notif_event *event)
5452{
5453 struct stop_reply *stop_reply = (struct stop_reply *) event;
5454
5455 /* acknowledge */
5456 putpkt ((char *) self->ack_command);
5457
5458 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5459 /* We got an unknown stop reply. */
5460 error (_("Unknown stop reply"));
5461
5462 push_stop_reply (stop_reply);
5463}
5464
5465static int
5466remote_notif_stop_can_get_pending_events (struct notif_client *self)
5467{
5468 /* We can't get pending events in remote_notif_process for
5469 notification stop, and we have to do this in remote_wait_ns
5470 instead. If we fetch all queued events from stub, remote stub
5471 may exit and we have no chance to process them back in
5472 remote_wait_ns. */
5473 mark_async_event_handler (remote_async_inferior_event_token);
5474 return 0;
5475}
5476
5477static void
5478stop_reply_dtr (struct notif_event *event)
5479{
5480 struct stop_reply *r = (struct stop_reply *) event;
5481
5482 VEC_free (cached_reg_t, r->regcache);
5483}
5484
5485static struct notif_event *
5486remote_notif_stop_alloc_reply (void)
5487{
5488 struct notif_event *r
70ba0933 5489 = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
5490
5491 r->dtr = stop_reply_dtr;
5492
5493 return r;
5494}
5495
5496/* A client of notification Stop. */
5497
5498struct notif_client notif_client_stop =
5499{
5500 "Stop",
5501 "vStopped",
5502 remote_notif_stop_parse,
5503 remote_notif_stop_ack,
5504 remote_notif_stop_can_get_pending_events,
5505 remote_notif_stop_alloc_reply,
f48ff2a7 5506 REMOTE_NOTIF_STOP,
722247f1
YQ
5507};
5508
5509/* A parameter to pass data in and out. */
5510
5511struct queue_iter_param
5512{
5513 void *input;
5514 struct stop_reply *output;
5515};
5516
cbb8991c
DB
5517/* Determine if THREAD is a pending fork parent thread. ARG contains
5518 the pid of the process that owns the threads we want to check, or
5519 -1 if we want to check all threads. */
5520
5521static int
5522is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
5523 ptid_t thread_ptid)
5524{
5525 if (ws->kind == TARGET_WAITKIND_FORKED
5526 || ws->kind == TARGET_WAITKIND_VFORKED)
5527 {
5528 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
5529 return 1;
5530 }
5531
5532 return 0;
5533}
5534
5535/* Check whether EVENT is a fork event, and if it is, remove the
5536 fork child from the context list passed in DATA. */
5537
5538static int
5539remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
5540 QUEUE_ITER (stop_reply_p) *iter,
5541 stop_reply_p event,
5542 void *data)
5543{
5544 struct queue_iter_param *param = data;
5545 struct threads_listing_context *context = param->input;
5546
5547 if (event->ws.kind == TARGET_WAITKIND_FORKED
5548 || event->ws.kind == TARGET_WAITKIND_VFORKED)
5549 {
5550 threads_listing_context_remove (&event->ws, context);
5551 }
5552
5553 return 1;
5554}
5555
5556/* If CONTEXT contains any fork child threads that have not been
5557 reported yet, remove them from the CONTEXT list. If such a
5558 thread exists it is because we are stopped at a fork catchpoint
5559 and have not yet called follow_fork, which will set up the
5560 host-side data structures for the new process. */
5561
5562static void
5563remove_new_fork_children (struct threads_listing_context *context)
5564{
5565 struct thread_info * thread;
5566 int pid = -1;
5567 struct notif_client *notif = &notif_client_stop;
5568 struct queue_iter_param param;
5569
5570 /* For any threads stopped at a fork event, remove the corresponding
5571 fork child threads from the CONTEXT list. */
5572 ALL_NON_EXITED_THREADS (thread)
5573 {
5574 struct target_waitstatus *ws = &thread->pending_follow;
5575
5576 if (is_pending_fork_parent (ws, pid, thread->ptid))
5577 {
5578 threads_listing_context_remove (ws, context);
5579 }
5580 }
5581
5582 /* Check for any pending fork events (not reported or processed yet)
5583 in process PID and remove those fork child threads from the
5584 CONTEXT list as well. */
5585 remote_notif_get_pending_events (notif);
5586 param.input = context;
5587 param.output = NULL;
5588 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5589 remove_child_of_pending_fork, &param);
5590}
5591
f48ff2a7
YQ
5592/* Remove stop replies in the queue if its pid is equal to the given
5593 inferior's pid. */
722247f1
YQ
5594
5595static int
f48ff2a7
YQ
5596remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5597 QUEUE_ITER (stop_reply_p) *iter,
5598 stop_reply_p event,
5599 void *data)
722247f1
YQ
5600{
5601 struct queue_iter_param *param = data;
5602 struct inferior *inf = param->input;
5603
f48ff2a7 5604 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
5605 {
5606 stop_reply_xfree (event);
5607 QUEUE_remove_elem (stop_reply_p, q, iter);
5608 }
5609
5610 return 1;
5611}
5612
f48ff2a7 5613/* Discard all pending stop replies of inferior INF. */
c906108c 5614
74531fed 5615static void
5f4cf0bb 5616discard_pending_stop_replies (struct inferior *inf)
c906108c 5617{
722247f1
YQ
5618 int i;
5619 struct queue_iter_param param;
f48ff2a7
YQ
5620 struct stop_reply *reply;
5621 struct remote_state *rs = get_remote_state ();
5622 struct remote_notif_state *rns = rs->notif_state;
5623
5624 /* This function can be notified when an inferior exists. When the
5625 target is not remote, the notification state is NULL. */
5626 if (rs->remote_desc == NULL)
5627 return;
5628
5629 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 5630
74531fed 5631 /* Discard the in-flight notification. */
f48ff2a7 5632 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 5633 {
722247f1 5634 stop_reply_xfree (reply);
f48ff2a7 5635 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 5636 }
c906108c 5637
722247f1
YQ
5638 param.input = inf;
5639 param.output = NULL;
74531fed
PA
5640 /* Discard the stop replies we have already pulled with
5641 vStopped. */
722247f1 5642 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
5643 remove_stop_reply_for_inferior, &param);
5644}
5645
bcc75809
YQ
5646/* If its remote state is equal to the given remote state,
5647 remove EVENT from the stop reply queue. */
5648
5649static int
5650remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5651 QUEUE_ITER (stop_reply_p) *iter,
5652 stop_reply_p event,
5653 void *data)
5654{
5655 struct queue_iter_param *param = data;
5656 struct remote_state *rs = param->input;
5657
5658 if (event->rs == rs)
5659 {
5660 stop_reply_xfree (event);
5661 QUEUE_remove_elem (stop_reply_p, q, iter);
5662 }
5663
5664 return 1;
5665}
5666
5667/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
5668
5669static void
bcc75809 5670discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
5671{
5672 struct queue_iter_param param;
5673
bcc75809 5674 param.input = rs;
f48ff2a7
YQ
5675 param.output = NULL;
5676 /* Discard the stop replies we have already pulled with
5677 vStopped. */
5678 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 5679 remove_stop_reply_of_remote_state, &param);
722247f1 5680}
74531fed 5681
722247f1
YQ
5682/* A parameter to pass data in and out. */
5683
5684static int
5685remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5686 QUEUE_ITER (stop_reply_p) *iter,
5687 stop_reply_p event,
5688 void *data)
5689{
5690 struct queue_iter_param *param = data;
5691 ptid_t *ptid = param->input;
5692
5693 if (ptid_match (event->ptid, *ptid))
5694 {
5695 param->output = event;
5696 QUEUE_remove_elem (stop_reply_p, q, iter);
5697 return 0;
c8e38a49 5698 }
722247f1
YQ
5699
5700 return 1;
74531fed 5701}
43ff13b4 5702
722247f1
YQ
5703/* Remove the first reply in 'stop_reply_queue' which matches
5704 PTID. */
2e9f7625 5705
722247f1
YQ
5706static struct stop_reply *
5707remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 5708{
722247f1
YQ
5709 struct queue_iter_param param;
5710
5711 param.input = &ptid;
5712 param.output = NULL;
5713
5714 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5715 remote_notif_remove_once_on_match, &param);
5716 if (notif_debug)
5717 fprintf_unfiltered (gdb_stdlog,
5718 "notif: discard queued event: 'Stop' in %s\n",
5719 target_pid_to_str (ptid));
a744cf53 5720
722247f1 5721 return param.output;
74531fed 5722}
75c99385 5723
74531fed
PA
5724/* Look for a queued stop reply belonging to PTID. If one is found,
5725 remove it from the queue, and return it. Returns NULL if none is
5726 found. If there are still queued events left to process, tell the
5727 event loop to get back to target_wait soon. */
e24a49d8 5728
74531fed
PA
5729static struct stop_reply *
5730queued_stop_reply (ptid_t ptid)
5731{
722247f1 5732 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 5733
722247f1 5734 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
5735 /* There's still at least an event left. */
5736 mark_async_event_handler (remote_async_inferior_event_token);
5737
722247f1 5738 return r;
74531fed
PA
5739}
5740
5741/* Push a fully parsed stop reply in the stop reply queue. Since we
5742 know that we now have at least one queued event left to pass to the
5743 core side, tell the event loop to get back to target_wait soon. */
5744
5745static void
5746push_stop_reply (struct stop_reply *new_event)
5747{
722247f1 5748 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 5749
722247f1
YQ
5750 if (notif_debug)
5751 fprintf_unfiltered (gdb_stdlog,
5752 "notif: push 'Stop' %s to queue %d\n",
5753 target_pid_to_str (new_event->ptid),
5754 QUEUE_length (stop_reply_p,
5755 stop_reply_queue));
74531fed
PA
5756
5757 mark_async_event_handler (remote_async_inferior_event_token);
5758}
5759
722247f1
YQ
5760static int
5761stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5762 QUEUE_ITER (stop_reply_p) *iter,
5763 struct stop_reply *event,
5764 void *data)
5765{
5766 ptid_t *ptid = data;
5767
5768 return !(ptid_equal (*ptid, event->ptid)
5769 && event->ws.kind == TARGET_WAITKIND_STOPPED);
5770}
5771
74531fed
PA
5772/* Returns true if we have a stop reply for PTID. */
5773
5774static int
5775peek_stop_reply (ptid_t ptid)
5776{
722247f1
YQ
5777 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5778 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
5779}
5780
1f10ba14
PA
5781/* Skip PACKET until the next semi-colon (or end of string). */
5782
5783static char *
5784skip_to_semicolon (char *p)
5785{
5786 while (*p != '\0' && *p != ';')
5787 p++;
5788 return p;
5789}
5790
74531fed
PA
5791/* Parse the stop reply in BUF. Either the function succeeds, and the
5792 result is stored in EVENT, or throws an error. */
5793
5794static void
5795remote_parse_stop_reply (char *buf, struct stop_reply *event)
5796{
5797 struct remote_arch_state *rsa = get_remote_arch_state ();
5798 ULONGEST addr;
5799 char *p;
5800
5801 event->ptid = null_ptid;
bcc75809 5802 event->rs = get_remote_state ();
74531fed
PA
5803 event->ws.kind = TARGET_WAITKIND_IGNORE;
5804 event->ws.value.integer = 0;
f7e6eed5 5805 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 5806 event->regcache = NULL;
dc146f7c 5807 event->core = -1;
74531fed
PA
5808
5809 switch (buf[0])
5810 {
5811 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
5812 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5813 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5814 ss = signal number
5815 n... = register number
5816 r... = register contents
5817 */
5818
5819 p = &buf[3]; /* after Txx */
5820 while (*p)
5821 {
5822 char *p1;
cea39f65 5823 int fieldsize;
43ff13b4 5824
1f10ba14
PA
5825 p1 = strchr (p, ':');
5826 if (p1 == NULL)
5827 error (_("Malformed packet(a) (missing colon): %s\n\
5828Packet: '%s'\n"),
5829 p, buf);
5830 if (p == p1)
5831 error (_("Malformed packet(a) (missing register number): %s\n\
5832Packet: '%s'\n"),
5833 p, buf);
3c3bea1c 5834
1f10ba14
PA
5835 /* Some "registers" are actually extended stop information.
5836 Note if you're adding a new entry here: GDB 7.9 and
5837 earlier assume that all register "numbers" that start
5838 with an hex digit are real register numbers. Make sure
5839 the server only sends such a packet if it knows the
5840 client understands it. */
c8e38a49 5841
1f10ba14
PA
5842 if (strncmp (p, "thread", p1 - p) == 0)
5843 event->ptid = read_ptid (++p1, &p);
5844 else if ((strncmp (p, "watch", p1 - p) == 0)
5845 || (strncmp (p, "rwatch", p1 - p) == 0)
5846 || (strncmp (p, "awatch", p1 - p) == 0))
cea39f65 5847 {
f7e6eed5 5848 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
5849 p = unpack_varlen_hex (++p1, &addr);
5850 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 5851 }
f7e6eed5
PA
5852 else if (strncmp (p, "swbreak", p1 - p) == 0)
5853 {
5854 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
5855
5856 /* Make sure the stub doesn't forget to indicate support
5857 with qSupported. */
5858 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
5859 error (_("Unexpected swbreak stop reason"));
5860
5861 /* The value part is documented as "must be empty",
5862 though we ignore it, in case we ever decide to make
5863 use of it in a backward compatible way. */
5864 p = skip_to_semicolon (p1 + 1);
5865 }
5866 else if (strncmp (p, "hwbreak", p1 - p) == 0)
5867 {
5868 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
5869
5870 /* Make sure the stub doesn't forget to indicate support
5871 with qSupported. */
5872 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
5873 error (_("Unexpected hwbreak stop reason"));
5874
5875 /* See above. */
5876 p = skip_to_semicolon (p1 + 1);
5877 }
1f10ba14 5878 else if (strncmp (p, "library", p1 - p) == 0)
cea39f65 5879 {
1f10ba14
PA
5880 event->ws.kind = TARGET_WAITKIND_LOADED;
5881 p = skip_to_semicolon (p1 + 1);
5882 }
5883 else if (strncmp (p, "replaylog", p1 - p) == 0)
5884 {
5885 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5886 /* p1 will indicate "begin" or "end", but it makes
5887 no difference for now, so ignore it. */
5888 p = skip_to_semicolon (p1 + 1);
5889 }
5890 else if (strncmp (p, "core", p1 - p) == 0)
5891 {
5892 ULONGEST c;
a744cf53 5893
1f10ba14
PA
5894 p = unpack_varlen_hex (++p1, &c);
5895 event->core = c;
cea39f65 5896 }
de0d863e
DB
5897 else if (strncmp (p, "fork", p1 - p) == 0)
5898 {
5899 event->ws.value.related_pid = read_ptid (++p1, &p);
5900 event->ws.kind = TARGET_WAITKIND_FORKED;
5901 }
c269dbdb
DB
5902 else if (strncmp (p, "vfork", p1 - p) == 0)
5903 {
5904 event->ws.value.related_pid = read_ptid (++p1, &p);
5905 event->ws.kind = TARGET_WAITKIND_VFORKED;
5906 }
5907 else if (strncmp (p, "vforkdone", p1 - p) == 0)
5908 {
5909 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
5910 p = skip_to_semicolon (p1 + 1);
5911 }
cea39f65
MS
5912 else
5913 {
1f10ba14
PA
5914 ULONGEST pnum;
5915 char *p_temp;
5916
5917 /* Maybe a real ``P'' register number. */
5918 p_temp = unpack_varlen_hex (p, &pnum);
5919 /* If the first invalid character is the colon, we got a
5920 register number. Otherwise, it's an unknown stop
5921 reason. */
5922 if (p_temp == p1)
5923 {
5924 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5925 cached_reg_t cached_reg;
43ff13b4 5926
1f10ba14
PA
5927 if (reg == NULL)
5928 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 5929Packet: '%s'\n"),
1f10ba14 5930 hex_string (pnum), p, buf);
c8e38a49 5931
1f10ba14 5932 cached_reg.num = reg->regnum;
4100683b 5933
1f10ba14
PA
5934 p = p1 + 1;
5935 fieldsize = hex2bin (p, cached_reg.data,
5936 register_size (target_gdbarch (),
5937 reg->regnum));
5938 p += 2 * fieldsize;
5939 if (fieldsize < register_size (target_gdbarch (),
5940 reg->regnum))
5941 warning (_("Remote reply is too short: %s"), buf);
74531fed 5942
1f10ba14
PA
5943 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5944 }
5945 else
5946 {
5947 /* Not a number. Silently skip unknown optional
5948 info. */
5949 p = skip_to_semicolon (p1 + 1);
5950 }
cea39f65 5951 }
c8e38a49 5952
cea39f65
MS
5953 if (*p != ';')
5954 error (_("Remote register badly formatted: %s\nhere: %s"),
5955 buf, p);
5956 ++p;
5957 }
5b5596ff
PA
5958
5959 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
5960 break;
5961
c8e38a49
PA
5962 /* fall through */
5963 case 'S': /* Old style status, just signal only. */
3a09da41
PA
5964 {
5965 int sig;
5966
5967 event->ws.kind = TARGET_WAITKIND_STOPPED;
5968 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
5969 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
5970 event->ws.value.sig = (enum gdb_signal) sig;
5971 else
5972 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5973 }
c8e38a49
PA
5974 break;
5975 case 'W': /* Target exited. */
5976 case 'X':
5977 {
5978 char *p;
5979 int pid;
5980 ULONGEST value;
82f73884 5981
c8e38a49
PA
5982 /* GDB used to accept only 2 hex chars here. Stubs should
5983 only send more if they detect GDB supports multi-process
5984 support. */
5985 p = unpack_varlen_hex (&buf[1], &value);
82f73884 5986
c8e38a49
PA
5987 if (buf[0] == 'W')
5988 {
5989 /* The remote process exited. */
74531fed
PA
5990 event->ws.kind = TARGET_WAITKIND_EXITED;
5991 event->ws.value.integer = value;
c8e38a49
PA
5992 }
5993 else
5994 {
5995 /* The remote process exited with a signal. */
74531fed 5996 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
5997 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
5998 event->ws.value.sig = (enum gdb_signal) value;
5999 else
6000 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 6001 }
82f73884 6002
c8e38a49
PA
6003 /* If no process is specified, assume inferior_ptid. */
6004 pid = ptid_get_pid (inferior_ptid);
6005 if (*p == '\0')
6006 ;
6007 else if (*p == ';')
6008 {
6009 p++;
6010
0b24eb2d 6011 if (*p == '\0')
82f73884 6012 ;
61012eef 6013 else if (startswith (p, "process:"))
82f73884 6014 {
c8e38a49 6015 ULONGEST upid;
a744cf53 6016
c8e38a49
PA
6017 p += sizeof ("process:") - 1;
6018 unpack_varlen_hex (p, &upid);
6019 pid = upid;
82f73884
PA
6020 }
6021 else
6022 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 6023 }
c8e38a49
PA
6024 else
6025 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
6026 event->ptid = pid_to_ptid (pid);
6027 }
6028 break;
6029 }
6030
6031 if (non_stop && ptid_equal (event->ptid, null_ptid))
6032 error (_("No process or thread specified in stop reply: %s"), buf);
6033}
6034
722247f1
YQ
6035/* When the stub wants to tell GDB about a new notification reply, it
6036 sends a notification (%Stop, for example). Those can come it at
6037 any time, hence, we have to make sure that any pending
6038 putpkt/getpkt sequence we're making is finished, before querying
6039 the stub for more events with the corresponding ack command
6040 (vStopped, for example). E.g., if we started a vStopped sequence
6041 immediately upon receiving the notification, something like this
6042 could happen:
74531fed
PA
6043
6044 1.1) --> Hg 1
6045 1.2) <-- OK
6046 1.3) --> g
6047 1.4) <-- %Stop
6048 1.5) --> vStopped
6049 1.6) <-- (registers reply to step #1.3)
6050
6051 Obviously, the reply in step #1.6 would be unexpected to a vStopped
6052 query.
6053
796cb314 6054 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
6055 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
6056 doing whatever we were doing:
6057
6058 2.1) --> Hg 1
6059 2.2) <-- OK
6060 2.3) --> g
6061 2.4) <-- %Stop
6062 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
6063 2.5) <-- (registers reply to step #2.3)
6064
6065 Eventualy after step #2.5, we return to the event loop, which
6066 notices there's an event on the
6067 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
6068 associated callback --- the function below. At this point, we're
6069 always safe to start a vStopped sequence. :
6070
6071 2.6) --> vStopped
6072 2.7) <-- T05 thread:2
6073 2.8) --> vStopped
6074 2.9) --> OK
6075*/
6076
722247f1
YQ
6077void
6078remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
6079{
6080 struct remote_state *rs = get_remote_state ();
74531fed 6081
f48ff2a7 6082 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 6083 {
722247f1
YQ
6084 if (notif_debug)
6085 fprintf_unfiltered (gdb_stdlog,
6086 "notif: process: '%s' ack pending event\n",
6087 nc->name);
74531fed 6088
722247f1 6089 /* acknowledge */
f48ff2a7
YQ
6090 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
6091 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
6092
6093 while (1)
6094 {
6095 getpkt (&rs->buf, &rs->buf_size, 0);
6096 if (strcmp (rs->buf, "OK") == 0)
6097 break;
6098 else
722247f1 6099 remote_notif_ack (nc, rs->buf);
74531fed
PA
6100 }
6101 }
722247f1
YQ
6102 else
6103 {
6104 if (notif_debug)
6105 fprintf_unfiltered (gdb_stdlog,
6106 "notif: process: '%s' no pending reply\n",
6107 nc->name);
6108 }
74531fed
PA
6109}
6110
74531fed
PA
6111/* Called when it is decided that STOP_REPLY holds the info of the
6112 event that is to be returned to the core. This function always
6113 destroys STOP_REPLY. */
6114
6115static ptid_t
6116process_stop_reply (struct stop_reply *stop_reply,
6117 struct target_waitstatus *status)
6118{
6119 ptid_t ptid;
6120
6121 *status = stop_reply->ws;
6122 ptid = stop_reply->ptid;
6123
6124 /* If no thread/process was reported by the stub, assume the current
6125 inferior. */
6126 if (ptid_equal (ptid, null_ptid))
6127 ptid = inferior_ptid;
6128
5f3563ea
PA
6129 if (status->kind != TARGET_WAITKIND_EXITED
6130 && status->kind != TARGET_WAITKIND_SIGNALLED)
74531fed 6131 {
ee154bee
TT
6132 struct remote_state *rs = get_remote_state ();
6133
5f3563ea
PA
6134 /* Expedited registers. */
6135 if (stop_reply->regcache)
6136 {
217f1f79 6137 struct regcache *regcache
f5656ead 6138 = get_thread_arch_regcache (ptid, target_gdbarch ());
5f3563ea
PA
6139 cached_reg_t *reg;
6140 int ix;
6141
6142 for (ix = 0;
6143 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
6144 ix++)
217f1f79 6145 regcache_raw_supply (regcache, reg->num, reg->data);
5f3563ea
PA
6146 VEC_free (cached_reg_t, stop_reply->regcache);
6147 }
74531fed 6148
f7e6eed5 6149 rs->stop_reason = stop_reply->stop_reason;
ee154bee 6150 rs->remote_watch_data_address = stop_reply->watch_data_address;
1941c569
PA
6151
6152 remote_notice_new_inferior (ptid, 0);
dc146f7c 6153 demand_private_info (ptid)->core = stop_reply->core;
74531fed
PA
6154 }
6155
74531fed
PA
6156 stop_reply_xfree (stop_reply);
6157 return ptid;
6158}
6159
6160/* The non-stop mode version of target_wait. */
6161
6162static ptid_t
47608cb1 6163remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6164{
6165 struct remote_state *rs = get_remote_state ();
74531fed
PA
6166 struct stop_reply *stop_reply;
6167 int ret;
fee9eda9 6168 int is_notif = 0;
74531fed
PA
6169
6170 /* If in non-stop mode, get out of getpkt even if a
6171 notification is received. */
6172
6173 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6174 0 /* forever */, &is_notif);
74531fed
PA
6175 while (1)
6176 {
fee9eda9 6177 if (ret != -1 && !is_notif)
74531fed
PA
6178 switch (rs->buf[0])
6179 {
6180 case 'E': /* Error of some sort. */
6181 /* We're out of sync with the target now. Did it continue
6182 or not? We can't tell which thread it was in non-stop,
6183 so just ignore this. */
6184 warning (_("Remote failure reply: %s"), rs->buf);
6185 break;
6186 case 'O': /* Console output. */
6187 remote_console_output (rs->buf + 1);
6188 break;
6189 default:
6190 warning (_("Invalid remote reply: %s"), rs->buf);
6191 break;
6192 }
6193
6194 /* Acknowledge a pending stop reply that may have arrived in the
6195 mean time. */
f48ff2a7 6196 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 6197 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
6198
6199 /* If indeed we noticed a stop reply, we're done. */
6200 stop_reply = queued_stop_reply (ptid);
6201 if (stop_reply != NULL)
6202 return process_stop_reply (stop_reply, status);
6203
47608cb1 6204 /* Still no event. If we're just polling for an event, then
74531fed 6205 return to the event loop. */
47608cb1 6206 if (options & TARGET_WNOHANG)
74531fed
PA
6207 {
6208 status->kind = TARGET_WAITKIND_IGNORE;
6209 return minus_one_ptid;
6210 }
6211
47608cb1 6212 /* Otherwise do a blocking wait. */
74531fed 6213 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6214 1 /* forever */, &is_notif);
74531fed
PA
6215 }
6216}
6217
6218/* Wait until the remote machine stops, then return, storing status in
6219 STATUS just as `wait' would. */
6220
6221static ptid_t
47608cb1 6222remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6223{
6224 struct remote_state *rs = get_remote_state ();
74531fed 6225 ptid_t event_ptid = null_ptid;
cea39f65 6226 char *buf;
74531fed
PA
6227 struct stop_reply *stop_reply;
6228
47608cb1
PA
6229 again:
6230
74531fed
PA
6231 status->kind = TARGET_WAITKIND_IGNORE;
6232 status->value.integer = 0;
6233
6234 stop_reply = queued_stop_reply (ptid);
6235 if (stop_reply != NULL)
6236 return process_stop_reply (stop_reply, status);
6237
6238 if (rs->cached_wait_status)
6239 /* Use the cached wait status, but only once. */
6240 rs->cached_wait_status = 0;
6241 else
6242 {
6243 int ret;
722247f1 6244 int is_notif;
74531fed
PA
6245
6246 if (!target_is_async_p ())
6247 {
934b9bac 6248 ofunc = signal (SIGINT, sync_remote_interrupt);
74531fed
PA
6249 /* If the user hit C-c before this packet, or between packets,
6250 pretend that it was hit right here. */
522002f9 6251 if (check_quit_flag ())
74531fed 6252 {
522002f9 6253 clear_quit_flag ();
934b9bac 6254 sync_remote_interrupt (SIGINT);
74531fed
PA
6255 }
6256 }
6257
6258 /* FIXME: cagney/1999-09-27: If we're in async mode we should
6259 _never_ wait for ever -> test on target_is_async_p().
6260 However, before we do that we need to ensure that the caller
6261 knows how to take the target into/out of async mode. */
722247f1
YQ
6262 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
6263 wait_forever_enabled_p, &is_notif);
6264
5e1b953b
SDJ
6265 if (!target_is_async_p ())
6266 signal (SIGINT, ofunc);
6267
722247f1
YQ
6268 /* GDB gets a notification. Return to core as this event is
6269 not interesting. */
6270 if (ret != -1 && is_notif)
6271 return minus_one_ptid;
74531fed
PA
6272 }
6273
6274 buf = rs->buf;
6275
f7e6eed5 6276 rs->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed
PA
6277
6278 /* We got something. */
6279 rs->waiting_for_stop_reply = 0;
6280
3a29589a
DJ
6281 /* Assume that the target has acknowledged Ctrl-C unless we receive
6282 an 'F' or 'O' packet. */
6283 if (buf[0] != 'F' && buf[0] != 'O')
6284 rs->ctrlc_pending_p = 0;
6285
74531fed
PA
6286 switch (buf[0])
6287 {
6288 case 'E': /* Error of some sort. */
6289 /* We're out of sync with the target now. Did it continue or
6290 not? Not is more likely, so report a stop. */
6291 warning (_("Remote failure reply: %s"), buf);
6292 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 6293 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
6294 break;
6295 case 'F': /* File-I/O request. */
3a29589a
DJ
6296 remote_fileio_request (buf, rs->ctrlc_pending_p);
6297 rs->ctrlc_pending_p = 0;
74531fed
PA
6298 break;
6299 case 'T': case 'S': case 'X': case 'W':
6300 {
722247f1
YQ
6301 struct stop_reply *stop_reply
6302 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
6303 rs->buf);
74531fed 6304
74531fed 6305 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
6306 break;
6307 }
6308 case 'O': /* Console output. */
6309 remote_console_output (buf + 1);
e24a49d8 6310
c8e38a49
PA
6311 /* The target didn't really stop; keep waiting. */
6312 rs->waiting_for_stop_reply = 1;
e24a49d8 6313
c8e38a49
PA
6314 break;
6315 case '\0':
b73be471 6316 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
6317 {
6318 /* Zero length reply means that we tried 'S' or 'C' and the
6319 remote system doesn't support it. */
6320 target_terminal_ours_for_output ();
6321 printf_filtered
6322 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
6323 gdb_signal_to_name (rs->last_sent_signal));
6324 rs->last_sent_signal = GDB_SIGNAL_0;
c8e38a49
PA
6325 target_terminal_inferior ();
6326
280ceea3 6327 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
c8e38a49
PA
6328 putpkt ((char *) buf);
6329
6330 /* We just told the target to resume, so a stop reply is in
6331 order. */
e24a49d8 6332 rs->waiting_for_stop_reply = 1;
c8e38a49 6333 break;
43ff13b4 6334 }
c8e38a49
PA
6335 /* else fallthrough */
6336 default:
6337 warning (_("Invalid remote reply: %s"), buf);
6338 /* Keep waiting. */
6339 rs->waiting_for_stop_reply = 1;
6340 break;
43ff13b4 6341 }
c8e38a49 6342
c8e38a49 6343 if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
6344 {
6345 /* Nothing interesting happened. If we're doing a non-blocking
6346 poll, we're done. Otherwise, go back to waiting. */
6347 if (options & TARGET_WNOHANG)
6348 return minus_one_ptid;
6349 else
6350 goto again;
6351 }
74531fed
PA
6352 else if (status->kind != TARGET_WAITKIND_EXITED
6353 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
6354 {
6355 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 6356 record_currthread (rs, event_ptid);
82f73884
PA
6357 else
6358 event_ptid = inferior_ptid;
43ff13b4 6359 }
74531fed
PA
6360 else
6361 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 6362 record_currthread (rs, minus_one_ptid);
79d7f229 6363
82f73884 6364 return event_ptid;
43ff13b4
JM
6365}
6366
74531fed
PA
6367/* Wait until the remote machine stops, then return, storing status in
6368 STATUS just as `wait' would. */
6369
c8e38a49 6370static ptid_t
117de6a9 6371remote_wait (struct target_ops *ops,
47608cb1 6372 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
6373{
6374 ptid_t event_ptid;
6375
74531fed 6376 if (non_stop)
47608cb1 6377 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 6378 else
47608cb1 6379 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 6380
d9d41e78 6381 if (target_is_async_p ())
c8e38a49 6382 {
74531fed
PA
6383 /* If there are are events left in the queue tell the event loop
6384 to return here. */
722247f1 6385 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 6386 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 6387 }
c8e38a49
PA
6388
6389 return event_ptid;
6390}
6391
74ca34ce 6392/* Fetch a single register using a 'p' packet. */
c906108c 6393
b96ec7ac 6394static int
56be3814 6395fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac
AC
6396{
6397 struct remote_state *rs = get_remote_state ();
2e9f7625 6398 char *buf, *p;
b96ec7ac
AC
6399 char regp[MAX_REGISTER_SIZE];
6400 int i;
6401
4082afcc 6402 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
6403 return 0;
6404
6405 if (reg->pnum == -1)
6406 return 0;
6407
2e9f7625 6408 p = rs->buf;
fcad0fa4 6409 *p++ = 'p';
74ca34ce 6410 p += hexnumstr (p, reg->pnum);
fcad0fa4 6411 *p++ = '\0';
1f4437a4
MS
6412 putpkt (rs->buf);
6413 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 6414
2e9f7625
DJ
6415 buf = rs->buf;
6416
74ca34ce
DJ
6417 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
6418 {
6419 case PACKET_OK:
6420 break;
6421 case PACKET_UNKNOWN:
6422 return 0;
6423 case PACKET_ERROR:
27a9c0bf
MS
6424 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6425 gdbarch_register_name (get_regcache_arch (regcache),
6426 reg->regnum),
6427 buf);
74ca34ce 6428 }
3f9a994c
JB
6429
6430 /* If this register is unfetchable, tell the regcache. */
6431 if (buf[0] == 'x')
8480adf2 6432 {
56be3814 6433 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 6434 return 1;
b96ec7ac 6435 }
b96ec7ac 6436
3f9a994c
JB
6437 /* Otherwise, parse and supply the value. */
6438 p = buf;
6439 i = 0;
6440 while (p[0] != 0)
6441 {
6442 if (p[1] == 0)
74ca34ce 6443 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
6444
6445 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6446 p += 2;
6447 }
56be3814 6448 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 6449 return 1;
b96ec7ac
AC
6450}
6451
74ca34ce
DJ
6452/* Fetch the registers included in the target's 'g' packet. */
6453
29709017
DJ
6454static int
6455send_g_packet (void)
c906108c 6456{
d01949b6 6457 struct remote_state *rs = get_remote_state ();
cea39f65 6458 int buf_len;
c906108c 6459
bba74b36 6460 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 6461 remote_send (&rs->buf, &rs->buf_size);
c906108c 6462
29709017
DJ
6463 /* We can get out of synch in various cases. If the first character
6464 in the buffer is not a hex character, assume that has happened
6465 and try to fetch another packet to read. */
6466 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6467 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6468 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6469 && rs->buf[0] != 'x') /* New: unavailable register value. */
6470 {
6471 if (remote_debug)
6472 fprintf_unfiltered (gdb_stdlog,
6473 "Bad register packet; fetching a new packet\n");
6474 getpkt (&rs->buf, &rs->buf_size, 0);
6475 }
6476
74ca34ce
DJ
6477 buf_len = strlen (rs->buf);
6478
6479 /* Sanity check the received packet. */
6480 if (buf_len % 2 != 0)
6481 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
6482
6483 return buf_len / 2;
6484}
6485
6486static void
56be3814 6487process_g_packet (struct regcache *regcache)
29709017 6488{
4a22f64d 6489 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
6490 struct remote_state *rs = get_remote_state ();
6491 struct remote_arch_state *rsa = get_remote_arch_state ();
6492 int i, buf_len;
6493 char *p;
6494 char *regs;
6495
6496 buf_len = strlen (rs->buf);
6497
6498 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce
DJ
6499 if (buf_len > 2 * rsa->sizeof_g_packet)
6500 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6501
6502 /* Save the size of the packet sent to us by the target. It is used
6503 as a heuristic when determining the max size of packets that the
6504 target can safely receive. */
6505 if (rsa->actual_register_packet_size == 0)
6506 rsa->actual_register_packet_size = buf_len;
6507
6508 /* If this is smaller than we guessed the 'g' packet would be,
6509 update our records. A 'g' reply that doesn't include a register's
6510 value implies either that the register is not available, or that
6511 the 'p' packet must be used. */
6512 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 6513 {
74ca34ce
DJ
6514 rsa->sizeof_g_packet = buf_len / 2;
6515
4a22f64d 6516 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 6517 {
74ca34ce
DJ
6518 if (rsa->regs[i].pnum == -1)
6519 continue;
6520
6521 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6522 rsa->regs[i].in_g_packet = 0;
b96ec7ac 6523 else
74ca34ce 6524 rsa->regs[i].in_g_packet = 1;
b96ec7ac 6525 }
74ca34ce 6526 }
b323314b 6527
74ca34ce 6528 regs = alloca (rsa->sizeof_g_packet);
c906108c
SS
6529
6530 /* Unimplemented registers read as all bits zero. */
ea9c271d 6531 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 6532
c906108c
SS
6533 /* Reply describes registers byte by byte, each byte encoded as two
6534 hex characters. Suck them all up, then supply them to the
6535 register cacheing/storage mechanism. */
6536
74ca34ce 6537 p = rs->buf;
ea9c271d 6538 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 6539 {
74ca34ce
DJ
6540 if (p[0] == 0 || p[1] == 0)
6541 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6542 internal_error (__FILE__, __LINE__,
9b20d036 6543 _("unexpected end of 'g' packet reply"));
74ca34ce 6544
c906108c 6545 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 6546 regs[i] = 0; /* 'x' */
c906108c
SS
6547 else
6548 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6549 p += 2;
6550 }
6551
a744cf53
MS
6552 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6553 {
6554 struct packet_reg *r = &rsa->regs[i];
6555
6556 if (r->in_g_packet)
6557 {
6558 if (r->offset * 2 >= strlen (rs->buf))
6559 /* This shouldn't happen - we adjusted in_g_packet above. */
6560 internal_error (__FILE__, __LINE__,
9b20d036 6561 _("unexpected end of 'g' packet reply"));
a744cf53
MS
6562 else if (rs->buf[r->offset * 2] == 'x')
6563 {
6564 gdb_assert (r->offset * 2 < strlen (rs->buf));
6565 /* The register isn't available, mark it as such (at
6566 the same time setting the value to zero). */
6567 regcache_raw_supply (regcache, r->regnum, NULL);
6568 }
6569 else
6570 regcache_raw_supply (regcache, r->regnum,
6571 regs + r->offset);
6572 }
6573 }
c906108c
SS
6574}
6575
29709017 6576static void
56be3814 6577fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
6578{
6579 send_g_packet ();
56be3814 6580 process_g_packet (regcache);
29709017
DJ
6581}
6582
e6e4e701
PA
6583/* Make the remote selected traceframe match GDB's selected
6584 traceframe. */
6585
6586static void
6587set_remote_traceframe (void)
6588{
6589 int newnum;
262e1174 6590 struct remote_state *rs = get_remote_state ();
e6e4e701 6591
262e1174 6592 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
6593 return;
6594
6595 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 6596 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
6597
6598 newnum = target_trace_find (tfind_number,
6599 get_traceframe_number (), 0, 0, NULL);
6600
6601 /* Should not happen. If it does, all bets are off. */
6602 if (newnum != get_traceframe_number ())
6603 warning (_("could not set remote traceframe"));
6604}
6605
74ca34ce 6606static void
28439f5e
PA
6607remote_fetch_registers (struct target_ops *ops,
6608 struct regcache *regcache, int regnum)
74ca34ce 6609{
74ca34ce
DJ
6610 struct remote_arch_state *rsa = get_remote_arch_state ();
6611 int i;
6612
e6e4e701 6613 set_remote_traceframe ();
79d7f229 6614 set_general_thread (inferior_ptid);
74ca34ce
DJ
6615
6616 if (regnum >= 0)
6617 {
6618 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6619
74ca34ce
DJ
6620 gdb_assert (reg != NULL);
6621
6622 /* If this register might be in the 'g' packet, try that first -
6623 we are likely to read more than one register. If this is the
6624 first 'g' packet, we might be overly optimistic about its
6625 contents, so fall back to 'p'. */
6626 if (reg->in_g_packet)
6627 {
56be3814 6628 fetch_registers_using_g (regcache);
74ca34ce
DJ
6629 if (reg->in_g_packet)
6630 return;
6631 }
6632
56be3814 6633 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
6634 return;
6635
6636 /* This register is not available. */
56be3814 6637 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
6638
6639 return;
6640 }
6641
56be3814 6642 fetch_registers_using_g (regcache);
74ca34ce 6643
4a22f64d 6644 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6645 if (!rsa->regs[i].in_g_packet)
56be3814 6646 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
6647 {
6648 /* This register is not available. */
56be3814 6649 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
6650 }
6651}
6652
c906108c
SS
6653/* Prepare to store registers. Since we may send them all (using a
6654 'G' request), we have to read out the ones we don't want to change
6655 first. */
6656
c5aa993b 6657static void
f32dbf8c 6658remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 6659{
ea9c271d 6660 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 6661 int i;
cfd77fa1 6662 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 6663
c906108c 6664 /* Make sure the entire registers array is valid. */
4082afcc 6665 switch (packet_support (PACKET_P))
5a2468f5
JM
6666 {
6667 case PACKET_DISABLE:
6668 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 6669 /* Make sure all the necessary registers are cached. */
4a22f64d 6670 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 6671 if (rsa->regs[i].in_g_packet)
316f2060 6672 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
6673 break;
6674 case PACKET_ENABLE:
6675 break;
6676 }
6677}
6678
ad10f812 6679/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 6680 packet was not recognized. */
5a2468f5
JM
6681
6682static int
1f4437a4
MS
6683store_register_using_P (const struct regcache *regcache,
6684 struct packet_reg *reg)
5a2468f5 6685{
4a22f64d 6686 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 6687 struct remote_state *rs = get_remote_state ();
5a2468f5 6688 /* Try storing a single register. */
6d820c5c 6689 char *buf = rs->buf;
cfd77fa1 6690 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 6691 char *p;
5a2468f5 6692
4082afcc 6693 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
6694 return 0;
6695
6696 if (reg->pnum == -1)
6697 return 0;
6698
ea9c271d 6699 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 6700 p = buf + strlen (buf);
56be3814 6701 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 6702 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
6703 putpkt (rs->buf);
6704 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 6705
74ca34ce
DJ
6706 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6707 {
6708 case PACKET_OK:
6709 return 1;
6710 case PACKET_ERROR:
27a9c0bf
MS
6711 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6712 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
6713 case PACKET_UNKNOWN:
6714 return 0;
6715 default:
6716 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6717 }
c906108c
SS
6718}
6719
23860348
MS
6720/* Store register REGNUM, or all registers if REGNUM == -1, from the
6721 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
6722
6723static void
56be3814 6724store_registers_using_G (const struct regcache *regcache)
c906108c 6725{
d01949b6 6726 struct remote_state *rs = get_remote_state ();
ea9c271d 6727 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 6728 gdb_byte *regs;
c906108c
SS
6729 char *p;
6730
193cb69f
AC
6731 /* Extract all the registers in the regcache copying them into a
6732 local buffer. */
6733 {
b323314b 6734 int i;
a744cf53 6735
ea9c271d
DJ
6736 regs = alloca (rsa->sizeof_g_packet);
6737 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 6738 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 6739 {
ea9c271d 6740 struct packet_reg *r = &rsa->regs[i];
a744cf53 6741
b323314b 6742 if (r->in_g_packet)
56be3814 6743 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
6744 }
6745 }
c906108c
SS
6746
6747 /* Command describes registers byte by byte,
6748 each byte encoded as two hex characters. */
6d820c5c 6749 p = rs->buf;
193cb69f 6750 *p++ = 'G';
74ca34ce
DJ
6751 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6752 updated. */
6753 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
6754 putpkt (rs->buf);
6755 getpkt (&rs->buf, &rs->buf_size, 0);
6756 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
6757 error (_("Could not write registers; remote failure reply '%s'"),
6758 rs->buf);
c906108c 6759}
74ca34ce
DJ
6760
6761/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6762 of the register cache buffer. FIXME: ignores errors. */
6763
6764static void
28439f5e
PA
6765remote_store_registers (struct target_ops *ops,
6766 struct regcache *regcache, int regnum)
74ca34ce 6767{
74ca34ce
DJ
6768 struct remote_arch_state *rsa = get_remote_arch_state ();
6769 int i;
6770
e6e4e701 6771 set_remote_traceframe ();
79d7f229 6772 set_general_thread (inferior_ptid);
74ca34ce
DJ
6773
6774 if (regnum >= 0)
6775 {
6776 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6777
74ca34ce
DJ
6778 gdb_assert (reg != NULL);
6779
6780 /* Always prefer to store registers using the 'P' packet if
6781 possible; we often change only a small number of registers.
6782 Sometimes we change a larger number; we'd need help from a
6783 higher layer to know to use 'G'. */
56be3814 6784 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
6785 return;
6786
6787 /* For now, don't complain if we have no way to write the
6788 register. GDB loses track of unavailable registers too
6789 easily. Some day, this may be an error. We don't have
0df8b418 6790 any way to read the register, either... */
74ca34ce
DJ
6791 if (!reg->in_g_packet)
6792 return;
6793
56be3814 6794 store_registers_using_G (regcache);
74ca34ce
DJ
6795 return;
6796 }
6797
56be3814 6798 store_registers_using_G (regcache);
74ca34ce 6799
4a22f64d 6800 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6801 if (!rsa->regs[i].in_g_packet)
56be3814 6802 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
6803 /* See above for why we do not issue an error here. */
6804 continue;
6805}
c906108c
SS
6806\f
6807
6808/* Return the number of hex digits in num. */
6809
6810static int
fba45db2 6811hexnumlen (ULONGEST num)
c906108c
SS
6812{
6813 int i;
6814
6815 for (i = 0; num != 0; i++)
6816 num >>= 4;
6817
6818 return max (i, 1);
6819}
6820
2df3850c 6821/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
6822
6823static int
fba45db2 6824hexnumstr (char *buf, ULONGEST num)
c906108c 6825{
c906108c 6826 int len = hexnumlen (num);
a744cf53 6827
2df3850c
JM
6828 return hexnumnstr (buf, num, len);
6829}
6830
c906108c 6831
2df3850c 6832/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 6833
2df3850c 6834static int
fba45db2 6835hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
6836{
6837 int i;
6838
6839 buf[width] = '\0';
6840
6841 for (i = width - 1; i >= 0; i--)
c906108c 6842 {
c5aa993b 6843 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
6844 num >>= 4;
6845 }
6846
2df3850c 6847 return width;
c906108c
SS
6848}
6849
23860348 6850/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
6851
6852static CORE_ADDR
fba45db2 6853remote_address_masked (CORE_ADDR addr)
c906108c 6854{
883b9c6c 6855 unsigned int address_size = remote_address_size;
a744cf53 6856
911c95a5
UW
6857 /* If "remoteaddresssize" was not set, default to target address size. */
6858 if (!address_size)
f5656ead 6859 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
6860
6861 if (address_size > 0
6862 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
6863 {
6864 /* Only create a mask when that mask can safely be constructed
23860348 6865 in a ULONGEST variable. */
c906108c 6866 ULONGEST mask = 1;
a744cf53 6867
911c95a5 6868 mask = (mask << address_size) - 1;
c906108c
SS
6869 addr &= mask;
6870 }
6871 return addr;
6872}
6873
6874/* Determine whether the remote target supports binary downloading.
6875 This is accomplished by sending a no-op memory write of zero length
6876 to the target at the specified address. It does not suffice to send
23860348
MS
6877 the whole packet, since many stubs strip the eighth bit and
6878 subsequently compute a wrong checksum, which causes real havoc with
6879 remote_write_bytes.
7a292a7a 6880
96baa820 6881 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 6882 clean. In cases like this, the user should clear "remote
23860348 6883 X-packet". */
96baa820 6884
c906108c 6885static void
fba45db2 6886check_binary_download (CORE_ADDR addr)
c906108c 6887{
d01949b6 6888 struct remote_state *rs = get_remote_state ();
24b06219 6889
4082afcc 6890 switch (packet_support (PACKET_X))
c906108c 6891 {
96baa820
JM
6892 case PACKET_DISABLE:
6893 break;
6894 case PACKET_ENABLE:
6895 break;
6896 case PACKET_SUPPORT_UNKNOWN:
6897 {
96baa820 6898 char *p;
802188a7 6899
2e9f7625 6900 p = rs->buf;
96baa820
JM
6901 *p++ = 'X';
6902 p += hexnumstr (p, (ULONGEST) addr);
6903 *p++ = ',';
6904 p += hexnumstr (p, (ULONGEST) 0);
6905 *p++ = ':';
6906 *p = '\0';
802188a7 6907
2e9f7625 6908 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 6909 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 6910
2e9f7625 6911 if (rs->buf[0] == '\0')
96baa820
JM
6912 {
6913 if (remote_debug)
6914 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
6915 "binary downloading NOT "
6916 "supported by target\n");
444abaca 6917 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
6918 }
6919 else
6920 {
6921 if (remote_debug)
6922 fprintf_unfiltered (gdb_stdlog,
64b9b334 6923 "binary downloading supported by target\n");
444abaca 6924 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
6925 }
6926 break;
6927 }
c906108c
SS
6928 }
6929}
6930
124e13d9
SM
6931/* Helper function to resize the payload in order to try to get a good
6932 alignment. We try to write an amount of data such that the next write will
6933 start on an address aligned on REMOTE_ALIGN_WRITES. */
6934
6935static int
6936align_for_efficient_write (int todo, CORE_ADDR memaddr)
6937{
6938 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6939}
6940
c906108c
SS
6941/* Write memory data directly to the remote machine.
6942 This does not inform the data cache; the data cache uses this.
a76d924d 6943 HEADER is the starting part of the packet.
c906108c
SS
6944 MEMADDR is the address in the remote memory space.
6945 MYADDR is the address of the buffer in our space.
124e13d9
SM
6946 LEN_UNITS is the number of addressable units to write.
6947 UNIT_SIZE is the length in bytes of an addressable unit.
a76d924d
DJ
6948 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6949 should send data as binary ('X'), or hex-encoded ('M').
6950
6951 The function creates packet of the form
6952 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6953
124e13d9 6954 where encoding of <DATA> is terminated by PACKET_FORMAT.
a76d924d
DJ
6955
6956 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6957 are omitted.
6958
9b409511 6959 Return the transferred status, error or OK (an
124e13d9
SM
6960 'enum target_xfer_status' value). Save the number of addressable units
6961 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
6962
6963 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
6964 exchange between gdb and the stub could look like (?? in place of the
6965 checksum):
6966
6967 -> $m1000,4#??
6968 <- aaaabbbbccccdddd
6969
6970 -> $M1000,3:eeeeffffeeee#??
6971 <- OK
6972
6973 -> $m1000,4#??
6974 <- eeeeffffeeeedddd */
c906108c 6975
9b409511 6976static enum target_xfer_status
a76d924d 6977remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
124e13d9
SM
6978 const gdb_byte *myaddr, ULONGEST len_units,
6979 int unit_size, ULONGEST *xfered_len_units,
6980 char packet_format, int use_length)
c906108c 6981{
6d820c5c 6982 struct remote_state *rs = get_remote_state ();
cfd77fa1 6983 char *p;
a76d924d
DJ
6984 char *plen = NULL;
6985 int plenlen = 0;
124e13d9
SM
6986 int todo_units;
6987 int units_written;
6988 int payload_capacity_bytes;
6989 int payload_length_bytes;
a76d924d
DJ
6990
6991 if (packet_format != 'X' && packet_format != 'M')
6992 internal_error (__FILE__, __LINE__,
9b20d036 6993 _("remote_write_bytes_aux: bad packet format"));
c906108c 6994
124e13d9 6995 if (len_units == 0)
9b409511 6996 return TARGET_XFER_EOF;
b2182ed2 6997
124e13d9 6998 payload_capacity_bytes = get_memory_write_packet_size ();
2bc416ba 6999
6d820c5c
DJ
7000 /* The packet buffer will be large enough for the payload;
7001 get_memory_packet_size ensures this. */
a76d924d 7002 rs->buf[0] = '\0';
c906108c 7003
a257b5bb 7004 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
7005 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
7006
124e13d9 7007 payload_capacity_bytes -= strlen ("$,:#NN");
a76d924d 7008 if (!use_length)
0df8b418 7009 /* The comma won't be used. */
124e13d9
SM
7010 payload_capacity_bytes += 1;
7011 payload_capacity_bytes -= strlen (header);
7012 payload_capacity_bytes -= hexnumlen (memaddr);
c906108c 7013
a76d924d 7014 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 7015
a76d924d
DJ
7016 strcat (rs->buf, header);
7017 p = rs->buf + strlen (header);
7018
7019 /* Compute a best guess of the number of bytes actually transfered. */
7020 if (packet_format == 'X')
c906108c 7021 {
23860348 7022 /* Best guess at number of bytes that will fit. */
124e13d9 7023 todo_units = min (len_units, payload_capacity_bytes / unit_size);
a76d924d 7024 if (use_length)
124e13d9
SM
7025 payload_capacity_bytes -= hexnumlen (todo_units);
7026 todo_units = min (todo_units, payload_capacity_bytes / unit_size);
a76d924d
DJ
7027 }
7028 else
7029 {
124e13d9
SM
7030 /* Number of bytes that will fit. */
7031 todo_units = min (len_units, (payload_capacity_bytes / unit_size) / 2);
a76d924d 7032 if (use_length)
124e13d9
SM
7033 payload_capacity_bytes -= hexnumlen (todo_units);
7034 todo_units = min (todo_units, (payload_capacity_bytes / unit_size) / 2);
917317f4 7035 }
a76d924d 7036
124e13d9 7037 if (todo_units <= 0)
3de11b2e 7038 internal_error (__FILE__, __LINE__,
405f8e94 7039 _("minimum packet size too small to write data"));
802188a7 7040
6765f3e5
DJ
7041 /* If we already need another packet, then try to align the end
7042 of this packet to a useful boundary. */
124e13d9
SM
7043 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
7044 todo_units = align_for_efficient_write (todo_units, memaddr);
6765f3e5 7045
a257b5bb 7046 /* Append "<memaddr>". */
917317f4
JM
7047 memaddr = remote_address_masked (memaddr);
7048 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 7049
a76d924d
DJ
7050 if (use_length)
7051 {
7052 /* Append ",". */
7053 *p++ = ',';
802188a7 7054
124e13d9
SM
7055 /* Append the length and retain its location and size. It may need to be
7056 adjusted once the packet body has been created. */
a76d924d 7057 plen = p;
124e13d9 7058 plenlen = hexnumstr (p, (ULONGEST) todo_units);
a76d924d
DJ
7059 p += plenlen;
7060 }
a257b5bb
AC
7061
7062 /* Append ":". */
917317f4
JM
7063 *p++ = ':';
7064 *p = '\0';
802188a7 7065
a257b5bb 7066 /* Append the packet body. */
a76d924d 7067 if (packet_format == 'X')
917317f4 7068 {
917317f4
JM
7069 /* Binary mode. Send target system values byte by byte, in
7070 increasing byte addresses. Only escape certain critical
7071 characters. */
124e13d9
SM
7072 payload_length_bytes =
7073 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
7074 &units_written, payload_capacity_bytes);
6765f3e5 7075
124e13d9 7076 /* If not all TODO units fit, then we'll need another packet. Make
9b7194bc
DJ
7077 a second try to keep the end of the packet aligned. Don't do
7078 this if the packet is tiny. */
124e13d9 7079 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
6765f3e5 7080 {
124e13d9
SM
7081 int new_todo_units;
7082
7083 new_todo_units = align_for_efficient_write (units_written, memaddr);
7084
7085 if (new_todo_units != units_written)
7086 payload_length_bytes =
7087 remote_escape_output (myaddr, new_todo_units, unit_size,
7088 (gdb_byte *) p, &units_written,
7089 payload_capacity_bytes);
6765f3e5
DJ
7090 }
7091
124e13d9
SM
7092 p += payload_length_bytes;
7093 if (use_length && units_written < todo_units)
c906108c 7094 {
802188a7 7095 /* Escape chars have filled up the buffer prematurely,
124e13d9 7096 and we have actually sent fewer units than planned.
917317f4
JM
7097 Fix-up the length field of the packet. Use the same
7098 number of characters as before. */
124e13d9
SM
7099 plen += hexnumnstr (plen, (ULONGEST) units_written,
7100 plenlen);
917317f4 7101 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 7102 }
a76d924d
DJ
7103 }
7104 else
7105 {
917317f4
JM
7106 /* Normal mode: Send target system values byte by byte, in
7107 increasing byte addresses. Each byte is encoded as a two hex
7108 value. */
124e13d9
SM
7109 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
7110 units_written = todo_units;
c906108c 7111 }
802188a7 7112
2e9f7625 7113 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 7114 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 7115
2e9f7625 7116 if (rs->buf[0] == 'E')
00d84524 7117 return TARGET_XFER_E_IO;
802188a7 7118
124e13d9
SM
7119 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
7120 send fewer units than we'd planned. */
7121 *xfered_len_units = (ULONGEST) units_written;
9b409511 7122 return TARGET_XFER_OK;
c906108c
SS
7123}
7124
a76d924d
DJ
7125/* Write memory data directly to the remote machine.
7126 This does not inform the data cache; the data cache uses this.
7127 MEMADDR is the address in the remote memory space.
7128 MYADDR is the address of the buffer in our space.
7129 LEN is the number of bytes.
7130
9b409511
YQ
7131 Return the transferred status, error or OK (an
7132 'enum target_xfer_status' value). Save the number of bytes
7133 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 7134
9b409511
YQ
7135static enum target_xfer_status
7136remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
124e13d9 7137 int unit_size, ULONGEST *xfered_len)
a76d924d
DJ
7138{
7139 char *packet_format = 0;
7140
7141 /* Check whether the target supports binary download. */
7142 check_binary_download (memaddr);
7143
4082afcc 7144 switch (packet_support (PACKET_X))
a76d924d
DJ
7145 {
7146 case PACKET_ENABLE:
7147 packet_format = "X";
7148 break;
7149 case PACKET_DISABLE:
7150 packet_format = "M";
7151 break;
7152 case PACKET_SUPPORT_UNKNOWN:
7153 internal_error (__FILE__, __LINE__,
7154 _("remote_write_bytes: bad internal state"));
7155 default:
7156 internal_error (__FILE__, __LINE__, _("bad switch"));
7157 }
7158
7159 return remote_write_bytes_aux (packet_format,
124e13d9 7160 memaddr, myaddr, len, unit_size, xfered_len,
9b409511 7161 packet_format[0], 1);
a76d924d
DJ
7162}
7163
9217e74e
YQ
7164/* Read memory data directly from the remote machine.
7165 This does not use the data cache; the data cache uses this.
7166 MEMADDR is the address in the remote memory space.
7167 MYADDR is the address of the buffer in our space.
124e13d9
SM
7168 LEN_UNITS is the number of addressable memory units to read..
7169 UNIT_SIZE is the length in bytes of an addressable unit.
9217e74e
YQ
7170
7171 Return the transferred status, error or OK (an
7172 'enum target_xfer_status' value). Save the number of bytes
124e13d9
SM
7173 transferred in *XFERED_LEN_UNITS.
7174
7175 See the comment of remote_write_bytes_aux for an example of
7176 memory read/write exchange between gdb and the stub. */
9217e74e
YQ
7177
7178static enum target_xfer_status
124e13d9
SM
7179remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
7180 int unit_size, ULONGEST *xfered_len_units)
9217e74e
YQ
7181{
7182 struct remote_state *rs = get_remote_state ();
124e13d9 7183 int buf_size_bytes; /* Max size of packet output buffer. */
9217e74e 7184 char *p;
124e13d9
SM
7185 int todo_units;
7186 int decoded_bytes;
9217e74e 7187
124e13d9 7188 buf_size_bytes = get_memory_read_packet_size ();
9217e74e
YQ
7189 /* The packet buffer will be large enough for the payload;
7190 get_memory_packet_size ensures this. */
7191
124e13d9
SM
7192 /* Number of units that will fit. */
7193 todo_units = min (len_units, (buf_size_bytes / unit_size) / 2);
9217e74e
YQ
7194
7195 /* Construct "m"<memaddr>","<len>". */
7196 memaddr = remote_address_masked (memaddr);
7197 p = rs->buf;
7198 *p++ = 'm';
7199 p += hexnumstr (p, (ULONGEST) memaddr);
7200 *p++ = ',';
124e13d9 7201 p += hexnumstr (p, (ULONGEST) todo_units);
9217e74e
YQ
7202 *p = '\0';
7203 putpkt (rs->buf);
7204 getpkt (&rs->buf, &rs->buf_size, 0);
7205 if (rs->buf[0] == 'E'
7206 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
7207 && rs->buf[3] == '\0')
7208 return TARGET_XFER_E_IO;
7209 /* Reply describes memory byte by byte, each byte encoded as two hex
7210 characters. */
7211 p = rs->buf;
124e13d9 7212 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
9217e74e 7213 /* Return what we have. Let higher layers handle partial reads. */
124e13d9 7214 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
9217e74e
YQ
7215 return TARGET_XFER_OK;
7216}
7217
b55fbac4
YQ
7218/* Using the set of read-only target sections of remote, read live
7219 read-only memory.
8acf9577
YQ
7220
7221 For interface/parameters/return description see target.h,
7222 to_xfer_partial. */
7223
7224static enum target_xfer_status
b55fbac4
YQ
7225remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
7226 ULONGEST memaddr, ULONGEST len,
124e13d9 7227 int unit_size, ULONGEST *xfered_len)
8acf9577
YQ
7228{
7229 struct target_section *secp;
7230 struct target_section_table *table;
7231
7232 secp = target_section_by_addr (ops, memaddr);
7233 if (secp != NULL
7234 && (bfd_get_section_flags (secp->the_bfd_section->owner,
7235 secp->the_bfd_section)
7236 & SEC_READONLY))
7237 {
7238 struct target_section *p;
7239 ULONGEST memend = memaddr + len;
7240
7241 table = target_get_section_table (ops);
7242
7243 for (p = table->sections; p < table->sections_end; p++)
7244 {
7245 if (memaddr >= p->addr)
7246 {
7247 if (memend <= p->endaddr)
7248 {
7249 /* Entire transfer is within this section. */
124e13d9 7250 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7251 xfered_len);
8acf9577
YQ
7252 }
7253 else if (memaddr >= p->endaddr)
7254 {
7255 /* This section ends before the transfer starts. */
7256 continue;
7257 }
7258 else
7259 {
7260 /* This section overlaps the transfer. Just do half. */
7261 len = p->endaddr - memaddr;
124e13d9 7262 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7263 xfered_len);
8acf9577
YQ
7264 }
7265 }
7266 }
7267 }
7268
7269 return TARGET_XFER_EOF;
7270}
7271
9217e74e
YQ
7272/* Similar to remote_read_bytes_1, but it reads from the remote stub
7273 first if the requested memory is unavailable in traceframe.
7274 Otherwise, fall back to remote_read_bytes_1. */
c906108c 7275
9b409511 7276static enum target_xfer_status
8acf9577 7277remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
124e13d9
SM
7278 gdb_byte *myaddr, ULONGEST len, int unit_size,
7279 ULONGEST *xfered_len)
c906108c 7280{
6b6aa828 7281 if (len == 0)
96c4f946 7282 return TARGET_XFER_EOF;
b2182ed2 7283
8acf9577
YQ
7284 if (get_traceframe_number () != -1)
7285 {
7286 VEC(mem_range_s) *available;
7287
7288 /* If we fail to get the set of available memory, then the
7289 target does not support querying traceframe info, and so we
7290 attempt reading from the traceframe anyway (assuming the
7291 target implements the old QTro packet then). */
7292 if (traceframe_available_memory (&available, memaddr, len))
7293 {
7294 struct cleanup *old_chain;
7295
7296 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
7297
7298 if (VEC_empty (mem_range_s, available)
7299 || VEC_index (mem_range_s, available, 0)->start != memaddr)
7300 {
7301 enum target_xfer_status res;
7302
7303 /* Don't read into the traceframe's available
7304 memory. */
7305 if (!VEC_empty (mem_range_s, available))
7306 {
7307 LONGEST oldlen = len;
7308
7309 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
7310 gdb_assert (len <= oldlen);
7311 }
7312
7313 do_cleanups (old_chain);
7314
7315 /* This goes through the topmost target again. */
b55fbac4 7316 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
124e13d9 7317 len, unit_size, xfered_len);
8acf9577
YQ
7318 if (res == TARGET_XFER_OK)
7319 return TARGET_XFER_OK;
7320 else
7321 {
7322 /* No use trying further, we know some memory starting
7323 at MEMADDR isn't available. */
7324 *xfered_len = len;
7325 return TARGET_XFER_UNAVAILABLE;
7326 }
7327 }
7328
7329 /* Don't try to read more than how much is available, in
7330 case the target implements the deprecated QTro packet to
7331 cater for older GDBs (the target's knowledge of read-only
7332 sections may be outdated by now). */
7333 len = VEC_index (mem_range_s, available, 0)->length;
7334
7335 do_cleanups (old_chain);
7336 }
7337 }
7338
124e13d9 7339 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
c906108c 7340}
74531fed 7341
c906108c 7342\f
c906108c 7343
a76d924d
DJ
7344/* Sends a packet with content determined by the printf format string
7345 FORMAT and the remaining arguments, then gets the reply. Returns
7346 whether the packet was a success, a failure, or unknown. */
7347
77b64a49
PA
7348static enum packet_result remote_send_printf (const char *format, ...)
7349 ATTRIBUTE_PRINTF (1, 2);
7350
2c0b251b 7351static enum packet_result
a76d924d
DJ
7352remote_send_printf (const char *format, ...)
7353{
7354 struct remote_state *rs = get_remote_state ();
7355 int max_size = get_remote_packet_size ();
a76d924d 7356 va_list ap;
a744cf53 7357
a76d924d
DJ
7358 va_start (ap, format);
7359
7360 rs->buf[0] = '\0';
7361 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 7362 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
7363
7364 if (putpkt (rs->buf) < 0)
7365 error (_("Communication problem with target."));
7366
7367 rs->buf[0] = '\0';
7368 getpkt (&rs->buf, &rs->buf_size, 0);
7369
7370 return packet_check_result (rs->buf);
7371}
7372
7373static void
7374restore_remote_timeout (void *p)
7375{
7376 int value = *(int *)p;
a744cf53 7377
a76d924d
DJ
7378 remote_timeout = value;
7379}
7380
7381/* Flash writing can take quite some time. We'll set
7382 effectively infinite timeout for flash operations.
7383 In future, we'll need to decide on a better approach. */
7384static const int remote_flash_timeout = 1000;
7385
7386static void
7387remote_flash_erase (struct target_ops *ops,
7388 ULONGEST address, LONGEST length)
7389{
f5656ead 7390 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d
DJ
7391 int saved_remote_timeout = remote_timeout;
7392 enum packet_result ret;
a76d924d
DJ
7393 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7394 &saved_remote_timeout);
a744cf53 7395
a76d924d
DJ
7396 remote_timeout = remote_flash_timeout;
7397
7398 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 7399 phex (address, addr_size),
a76d924d
DJ
7400 phex (length, 4));
7401 switch (ret)
7402 {
7403 case PACKET_UNKNOWN:
7404 error (_("Remote target does not support flash erase"));
7405 case PACKET_ERROR:
7406 error (_("Error erasing flash with vFlashErase packet"));
7407 default:
7408 break;
7409 }
7410
7411 do_cleanups (back_to);
7412}
7413
9b409511
YQ
7414static enum target_xfer_status
7415remote_flash_write (struct target_ops *ops, ULONGEST address,
7416 ULONGEST length, ULONGEST *xfered_len,
7417 const gdb_byte *data)
a76d924d
DJ
7418{
7419 int saved_remote_timeout = remote_timeout;
9b409511 7420 enum target_xfer_status ret;
a76d924d 7421 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
9b409511 7422 &saved_remote_timeout);
a76d924d
DJ
7423
7424 remote_timeout = remote_flash_timeout;
124e13d9 7425 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
9b409511 7426 xfered_len,'X', 0);
a76d924d
DJ
7427 do_cleanups (back_to);
7428
7429 return ret;
7430}
7431
7432static void
7433remote_flash_done (struct target_ops *ops)
7434{
7435 int saved_remote_timeout = remote_timeout;
7436 int ret;
7437 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7438 &saved_remote_timeout);
7439
7440 remote_timeout = remote_flash_timeout;
7441 ret = remote_send_printf ("vFlashDone");
7442 do_cleanups (back_to);
7443
7444 switch (ret)
7445 {
7446 case PACKET_UNKNOWN:
7447 error (_("Remote target does not support vFlashDone"));
7448 case PACKET_ERROR:
7449 error (_("Error finishing flash operation"));
7450 default:
7451 break;
7452 }
7453}
7454
c906108c 7455static void
fba45db2 7456remote_files_info (struct target_ops *ignore)
c906108c
SS
7457{
7458 puts_filtered ("Debugging a target over a serial line.\n");
7459}
7460\f
7461/* Stuff for dealing with the packets which are part of this protocol.
7462 See comment at top of file for details. */
7463
1927e618
PA
7464/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7465 error to higher layers. Called when a serial error is detected.
7466 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
7467 the system error message for errno at function entry and final dot
7468 for output compatibility with throw_perror_with_name. */
1927e618
PA
7469
7470static void
7471unpush_and_perror (const char *string)
7472{
d6cb50a2 7473 int saved_errno = errno;
1927e618
PA
7474
7475 remote_unpush_target ();
d6cb50a2
JK
7476 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7477 safe_strerror (saved_errno));
1927e618
PA
7478}
7479
0876f84a 7480/* Read a single character from the remote end. */
c906108c
SS
7481
7482static int
fba45db2 7483readchar (int timeout)
c906108c
SS
7484{
7485 int ch;
5d93a237 7486 struct remote_state *rs = get_remote_state ();
c906108c 7487
5d93a237 7488 ch = serial_readchar (rs->remote_desc, timeout);
c906108c 7489
2acceee2 7490 if (ch >= 0)
0876f84a 7491 return ch;
2acceee2
JM
7492
7493 switch ((enum serial_rc) ch)
c906108c
SS
7494 {
7495 case SERIAL_EOF:
78a095c3 7496 remote_unpush_target ();
598d3636 7497 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 7498 /* no return */
c906108c 7499 case SERIAL_ERROR:
1927e618
PA
7500 unpush_and_perror (_("Remote communication error. "
7501 "Target disconnected."));
2acceee2 7502 /* no return */
c906108c 7503 case SERIAL_TIMEOUT:
2acceee2 7504 break;
c906108c 7505 }
2acceee2 7506 return ch;
c906108c
SS
7507}
7508
c33e31fd
PA
7509/* Wrapper for serial_write that closes the target and throws if
7510 writing fails. */
7511
7512static void
7513remote_serial_write (const char *str, int len)
7514{
5d93a237
TT
7515 struct remote_state *rs = get_remote_state ();
7516
7517 if (serial_write (rs->remote_desc, str, len))
c33e31fd 7518 {
1927e618
PA
7519 unpush_and_perror (_("Remote communication error. "
7520 "Target disconnected."));
c33e31fd
PA
7521 }
7522}
7523
6d820c5c
DJ
7524/* Send the command in *BUF to the remote machine, and read the reply
7525 into *BUF. Report an error if we get an error reply. Resize
7526 *BUF using xrealloc if necessary to hold the result, and update
7527 *SIZEOF_BUF. */
c906108c
SS
7528
7529static void
6d820c5c
DJ
7530remote_send (char **buf,
7531 long *sizeof_buf)
c906108c 7532{
6d820c5c 7533 putpkt (*buf);
c2d11a7d 7534 getpkt (buf, sizeof_buf, 0);
c906108c 7535
6d820c5c
DJ
7536 if ((*buf)[0] == 'E')
7537 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
7538}
7539
6e5abd65
PA
7540/* Return a pointer to an xmalloc'ed string representing an escaped
7541 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7542 etc. The caller is responsible for releasing the returned
7543 memory. */
7544
7545static char *
7546escape_buffer (const char *buf, int n)
7547{
7548 struct cleanup *old_chain;
7549 struct ui_file *stb;
7550 char *str;
6e5abd65
PA
7551
7552 stb = mem_fileopen ();
7553 old_chain = make_cleanup_ui_file_delete (stb);
7554
6ef284bd 7555 fputstrn_unfiltered (buf, n, '\\', stb);
759ef836 7556 str = ui_file_xstrdup (stb, NULL);
6e5abd65
PA
7557 do_cleanups (old_chain);
7558 return str;
7559}
7560
c906108c
SS
7561/* Display a null-terminated packet on stdout, for debugging, using C
7562 string notation. */
7563
7564static void
baa336ce 7565print_packet (const char *buf)
c906108c
SS
7566{
7567 puts_filtered ("\"");
43e526b9 7568 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
7569 puts_filtered ("\"");
7570}
7571
7572int
baa336ce 7573putpkt (const char *buf)
c906108c
SS
7574{
7575 return putpkt_binary (buf, strlen (buf));
7576}
7577
7578/* Send a packet to the remote machine, with error checking. The data
23860348 7579 of the packet is in BUF. The string in BUF can be at most
ea9c271d 7580 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
7581 and for a possible /0 if we are debugging (remote_debug) and want
7582 to print the sent packet as a string. */
c906108c
SS
7583
7584static int
baa336ce 7585putpkt_binary (const char *buf, int cnt)
c906108c 7586{
2d717e4f 7587 struct remote_state *rs = get_remote_state ();
c906108c
SS
7588 int i;
7589 unsigned char csum = 0;
11cf8741 7590 char *buf2 = alloca (cnt + 6);
085dd6e6 7591
c906108c
SS
7592 int ch;
7593 int tcount = 0;
7594 char *p;
dd61ec5c 7595 char *message;
c906108c 7596
e24a49d8
PA
7597 /* Catch cases like trying to read memory or listing threads while
7598 we're waiting for a stop reply. The remote server wouldn't be
7599 ready to handle this request, so we'd hang and timeout. We don't
7600 have to worry about this in synchronous mode, because in that
7601 case it's not possible to issue a command while the target is
74531fed
PA
7602 running. This is not a problem in non-stop mode, because in that
7603 case, the stub is always ready to process serial input. */
d9d41e78 7604 if (!non_stop && target_is_async_p () && rs->waiting_for_stop_reply)
9597b22a
DE
7605 {
7606 error (_("Cannot execute this command while the target is running.\n"
7607 "Use the \"interrupt\" command to stop the target\n"
7608 "and then try again."));
7609 }
e24a49d8 7610
2d717e4f
DJ
7611 /* We're sending out a new packet. Make sure we don't look at a
7612 stale cached response. */
7613 rs->cached_wait_status = 0;
7614
c906108c
SS
7615 /* Copy the packet into buffer BUF2, encapsulating it
7616 and giving it a checksum. */
7617
c906108c
SS
7618 p = buf2;
7619 *p++ = '$';
7620
7621 for (i = 0; i < cnt; i++)
7622 {
7623 csum += buf[i];
7624 *p++ = buf[i];
7625 }
7626 *p++ = '#';
7627 *p++ = tohex ((csum >> 4) & 0xf);
7628 *p++ = tohex (csum & 0xf);
7629
7630 /* Send it over and over until we get a positive ack. */
7631
7632 while (1)
7633 {
7634 int started_error_output = 0;
7635
7636 if (remote_debug)
7637 {
6e5abd65
PA
7638 struct cleanup *old_chain;
7639 char *str;
7640
c906108c 7641 *p = '\0';
6e5abd65
PA
7642 str = escape_buffer (buf2, p - buf2);
7643 old_chain = make_cleanup (xfree, str);
7644 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
0f71a2f6 7645 gdb_flush (gdb_stdlog);
6e5abd65 7646 do_cleanups (old_chain);
c906108c 7647 }
c33e31fd 7648 remote_serial_write (buf2, p - buf2);
c906108c 7649
a6f3e723
SL
7650 /* If this is a no acks version of the remote protocol, send the
7651 packet and move on. */
7652 if (rs->noack_mode)
7653 break;
7654
74531fed
PA
7655 /* Read until either a timeout occurs (-2) or '+' is read.
7656 Handle any notification that arrives in the mean time. */
c906108c
SS
7657 while (1)
7658 {
7659 ch = readchar (remote_timeout);
7660
c5aa993b 7661 if (remote_debug)
c906108c
SS
7662 {
7663 switch (ch)
7664 {
7665 case '+':
1216fa2c 7666 case '-':
c906108c
SS
7667 case SERIAL_TIMEOUT:
7668 case '$':
74531fed 7669 case '%':
c906108c
SS
7670 if (started_error_output)
7671 {
7672 putchar_unfiltered ('\n');
7673 started_error_output = 0;
7674 }
7675 }
7676 }
7677
7678 switch (ch)
7679 {
7680 case '+':
7681 if (remote_debug)
0f71a2f6 7682 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 7683 return 1;
1216fa2c
AC
7684 case '-':
7685 if (remote_debug)
7686 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 7687 /* FALLTHROUGH */
c906108c 7688 case SERIAL_TIMEOUT:
c5aa993b 7689 tcount++;
c906108c
SS
7690 if (tcount > 3)
7691 return 0;
23860348 7692 break; /* Retransmit buffer. */
c906108c
SS
7693 case '$':
7694 {
40e3f985 7695 if (remote_debug)
2bc416ba 7696 fprintf_unfiltered (gdb_stdlog,
23860348 7697 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
7698 /* It's probably an old response sent because an ACK
7699 was lost. Gobble up the packet and ack it so it
7700 doesn't get retransmitted when we resend this
7701 packet. */
6d820c5c 7702 skip_frame ();
c33e31fd 7703 remote_serial_write ("+", 1);
23860348 7704 continue; /* Now, go look for +. */
c906108c 7705 }
74531fed
PA
7706
7707 case '%':
7708 {
7709 int val;
7710
7711 /* If we got a notification, handle it, and go back to looking
7712 for an ack. */
7713 /* We've found the start of a notification. Now
7714 collect the data. */
7715 val = read_frame (&rs->buf, &rs->buf_size);
7716 if (val >= 0)
7717 {
7718 if (remote_debug)
7719 {
6e5abd65
PA
7720 struct cleanup *old_chain;
7721 char *str;
7722
7723 str = escape_buffer (rs->buf, val);
7724 old_chain = make_cleanup (xfree, str);
7725 fprintf_unfiltered (gdb_stdlog,
7726 " Notification received: %s\n",
7727 str);
7728 do_cleanups (old_chain);
74531fed 7729 }
5965e028 7730 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
7731 /* We're in sync now, rewait for the ack. */
7732 tcount = 0;
7733 }
7734 else
7735 {
7736 if (remote_debug)
7737 {
7738 if (!started_error_output)
7739 {
7740 started_error_output = 1;
7741 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7742 }
7743 fputc_unfiltered (ch & 0177, gdb_stdlog);
7744 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7745 }
7746 }
7747 continue;
7748 }
7749 /* fall-through */
c906108c
SS
7750 default:
7751 if (remote_debug)
7752 {
7753 if (!started_error_output)
7754 {
7755 started_error_output = 1;
0f71a2f6 7756 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 7757 }
0f71a2f6 7758 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
7759 }
7760 continue;
7761 }
23860348 7762 break; /* Here to retransmit. */
c906108c
SS
7763 }
7764
7765#if 0
7766 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
7767 able to get out next time we call QUIT, without anything as
7768 violent as interrupt_query. If we want to provide a way out of
7769 here without getting to the next QUIT, it should be based on
7770 hitting ^C twice as in remote_wait. */
c906108c
SS
7771 if (quit_flag)
7772 {
7773 quit_flag = 0;
7774 interrupt_query ();
7775 }
7776#endif
7777 }
a6f3e723 7778 return 0;
c906108c
SS
7779}
7780
6d820c5c
DJ
7781/* Come here after finding the start of a frame when we expected an
7782 ack. Do our best to discard the rest of this packet. */
7783
7784static void
7785skip_frame (void)
7786{
7787 int c;
7788
7789 while (1)
7790 {
7791 c = readchar (remote_timeout);
7792 switch (c)
7793 {
7794 case SERIAL_TIMEOUT:
7795 /* Nothing we can do. */
7796 return;
7797 case '#':
7798 /* Discard the two bytes of checksum and stop. */
7799 c = readchar (remote_timeout);
7800 if (c >= 0)
7801 c = readchar (remote_timeout);
7802
7803 return;
7804 case '*': /* Run length encoding. */
7805 /* Discard the repeat count. */
7806 c = readchar (remote_timeout);
7807 if (c < 0)
7808 return;
7809 break;
7810 default:
7811 /* A regular character. */
7812 break;
7813 }
7814 }
7815}
7816
c906108c 7817/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
7818 into *BUF, verifying the checksum, length, and handling run-length
7819 compression. NUL terminate the buffer. If there is not enough room,
7820 expand *BUF using xrealloc.
c906108c 7821
c2d11a7d
JM
7822 Returns -1 on error, number of characters in buffer (ignoring the
7823 trailing NULL) on success. (could be extended to return one of the
23860348 7824 SERIAL status indications). */
c2d11a7d
JM
7825
7826static long
6d820c5c
DJ
7827read_frame (char **buf_p,
7828 long *sizeof_buf)
c906108c
SS
7829{
7830 unsigned char csum;
c2d11a7d 7831 long bc;
c906108c 7832 int c;
6d820c5c 7833 char *buf = *buf_p;
a6f3e723 7834 struct remote_state *rs = get_remote_state ();
c906108c
SS
7835
7836 csum = 0;
c2d11a7d 7837 bc = 0;
c906108c
SS
7838
7839 while (1)
7840 {
7841 c = readchar (remote_timeout);
c906108c
SS
7842 switch (c)
7843 {
7844 case SERIAL_TIMEOUT:
7845 if (remote_debug)
0f71a2f6 7846 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 7847 return -1;
c906108c
SS
7848 case '$':
7849 if (remote_debug)
0f71a2f6
JM
7850 fputs_filtered ("Saw new packet start in middle of old one\n",
7851 gdb_stdlog);
23860348 7852 return -1; /* Start a new packet, count retries. */
c906108c
SS
7853 case '#':
7854 {
7855 unsigned char pktcsum;
e1b09194
AC
7856 int check_0 = 0;
7857 int check_1 = 0;
c906108c 7858
c2d11a7d 7859 buf[bc] = '\0';
c906108c 7860
e1b09194
AC
7861 check_0 = readchar (remote_timeout);
7862 if (check_0 >= 0)
7863 check_1 = readchar (remote_timeout);
802188a7 7864
e1b09194
AC
7865 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7866 {
7867 if (remote_debug)
2bc416ba 7868 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 7869 gdb_stdlog);
e1b09194
AC
7870 return -1;
7871 }
7872 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
7873 {
7874 if (remote_debug)
2bc416ba 7875 fputs_filtered ("Communication error in checksum\n",
23860348 7876 gdb_stdlog);
40e3f985
FN
7877 return -1;
7878 }
c906108c 7879
a6f3e723
SL
7880 /* Don't recompute the checksum; with no ack packets we
7881 don't have any way to indicate a packet retransmission
7882 is necessary. */
7883 if (rs->noack_mode)
7884 return bc;
7885
e1b09194 7886 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 7887 if (csum == pktcsum)
c2d11a7d 7888 return bc;
c906108c 7889
c5aa993b 7890 if (remote_debug)
c906108c 7891 {
6e5abd65
PA
7892 struct cleanup *old_chain;
7893 char *str;
7894
7895 str = escape_buffer (buf, bc);
7896 old_chain = make_cleanup (xfree, str);
7897 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
7898 "Bad checksum, sentsum=0x%x, "
7899 "csum=0x%x, buf=%s\n",
6e5abd65
PA
7900 pktcsum, csum, str);
7901 do_cleanups (old_chain);
c906108c 7902 }
c2d11a7d 7903 /* Number of characters in buffer ignoring trailing
23860348 7904 NULL. */
c2d11a7d 7905 return -1;
c906108c 7906 }
23860348 7907 case '*': /* Run length encoding. */
c2c6d25f
JM
7908 {
7909 int repeat;
c906108c 7910
a744cf53 7911 csum += c;
b4501125
AC
7912 c = readchar (remote_timeout);
7913 csum += c;
23860348 7914 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 7915
23860348 7916 /* The character before ``*'' is repeated. */
c2d11a7d 7917
6d820c5c 7918 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 7919 {
6d820c5c
DJ
7920 if (bc + repeat - 1 >= *sizeof_buf - 1)
7921 {
7922 /* Make some more room in the buffer. */
7923 *sizeof_buf += repeat;
7924 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7925 buf = *buf_p;
7926 }
7927
c2d11a7d
JM
7928 memset (&buf[bc], buf[bc - 1], repeat);
7929 bc += repeat;
c2c6d25f
JM
7930 continue;
7931 }
7932
c2d11a7d 7933 buf[bc] = '\0';
6d820c5c 7934 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 7935 return -1;
c2c6d25f 7936 }
c906108c 7937 default:
6d820c5c 7938 if (bc >= *sizeof_buf - 1)
c906108c 7939 {
6d820c5c
DJ
7940 /* Make some more room in the buffer. */
7941 *sizeof_buf *= 2;
7942 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7943 buf = *buf_p;
c906108c
SS
7944 }
7945
6d820c5c
DJ
7946 buf[bc++] = c;
7947 csum += c;
7948 continue;
c906108c
SS
7949 }
7950 }
7951}
7952
7953/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7954 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7955 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7956 rather than timing out; this is used (in synchronous mode) to wait
7957 for a target that is is executing user code to stop. */
d9fcf2fb
JM
7958/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7959 don't have to change all the calls to getpkt to deal with the
7960 return value, because at the moment I don't know what the right
23860348 7961 thing to do it for those. */
c906108c 7962void
6d820c5c
DJ
7963getpkt (char **buf,
7964 long *sizeof_buf,
c2d11a7d 7965 int forever)
d9fcf2fb
JM
7966{
7967 int timed_out;
7968
7969 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7970}
7971
7972
7973/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7974 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7975 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7976 rather than timing out; this is used (in synchronous mode) to wait
7977 for a target that is is executing user code to stop. If FOREVER ==
7978 0, this function is allowed to time out gracefully and return an
74531fed
PA
7979 indication of this to the caller. Otherwise return the number of
7980 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
7981 enough reason to return to the caller. *IS_NOTIF is an output
7982 boolean that indicates whether *BUF holds a notification or not
7983 (a regular packet). */
74531fed 7984
3172dc30 7985static int
74531fed 7986getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 7987 int expecting_notif, int *is_notif)
c906108c 7988{
2d717e4f 7989 struct remote_state *rs = get_remote_state ();
c906108c
SS
7990 int c;
7991 int tries;
7992 int timeout;
df4b58fe 7993 int val = -1;
c906108c 7994
2d717e4f
DJ
7995 /* We're reading a new response. Make sure we don't look at a
7996 previously cached response. */
7997 rs->cached_wait_status = 0;
7998
6d820c5c 7999 strcpy (*buf, "timeout");
c906108c
SS
8000
8001 if (forever)
74531fed
PA
8002 timeout = watchdog > 0 ? watchdog : -1;
8003 else if (expecting_notif)
8004 timeout = 0; /* There should already be a char in the buffer. If
8005 not, bail out. */
c906108c
SS
8006 else
8007 timeout = remote_timeout;
8008
8009#define MAX_TRIES 3
8010
74531fed
PA
8011 /* Process any number of notifications, and then return when
8012 we get a packet. */
8013 for (;;)
c906108c 8014 {
d9c43928 8015 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
8016 times. */
8017 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 8018 {
74531fed
PA
8019 /* This can loop forever if the remote side sends us
8020 characters continuously, but if it pauses, we'll get
8021 SERIAL_TIMEOUT from readchar because of timeout. Then
8022 we'll count that as a retry.
8023
8024 Note that even when forever is set, we will only wait
8025 forever prior to the start of a packet. After that, we
8026 expect characters to arrive at a brisk pace. They should
8027 show up within remote_timeout intervals. */
8028 do
8029 c = readchar (timeout);
8030 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
8031
8032 if (c == SERIAL_TIMEOUT)
8033 {
74531fed
PA
8034 if (expecting_notif)
8035 return -1; /* Don't complain, it's normal to not get
8036 anything in this case. */
8037
23860348 8038 if (forever) /* Watchdog went off? Kill the target. */
c906108c 8039 {
2acceee2 8040 QUIT;
78a095c3 8041 remote_unpush_target ();
598d3636
JK
8042 throw_error (TARGET_CLOSE_ERROR,
8043 _("Watchdog timeout has expired. "
8044 "Target detached."));
c906108c 8045 }
c906108c 8046 if (remote_debug)
0f71a2f6 8047 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 8048 }
74531fed
PA
8049 else
8050 {
8051 /* We've found the start of a packet or notification.
8052 Now collect the data. */
8053 val = read_frame (buf, sizeof_buf);
8054 if (val >= 0)
8055 break;
8056 }
8057
c33e31fd 8058 remote_serial_write ("-", 1);
c906108c 8059 }
c906108c 8060
74531fed
PA
8061 if (tries > MAX_TRIES)
8062 {
8063 /* We have tried hard enough, and just can't receive the
8064 packet/notification. Give up. */
8065 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 8066
74531fed
PA
8067 /* Skip the ack char if we're in no-ack mode. */
8068 if (!rs->noack_mode)
c33e31fd 8069 remote_serial_write ("+", 1);
74531fed
PA
8070 return -1;
8071 }
c906108c 8072
74531fed
PA
8073 /* If we got an ordinary packet, return that to our caller. */
8074 if (c == '$')
c906108c
SS
8075 {
8076 if (remote_debug)
43e526b9 8077 {
6e5abd65
PA
8078 struct cleanup *old_chain;
8079 char *str;
8080
8081 str = escape_buffer (*buf, val);
8082 old_chain = make_cleanup (xfree, str);
8083 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
8084 do_cleanups (old_chain);
43e526b9 8085 }
a6f3e723
SL
8086
8087 /* Skip the ack char if we're in no-ack mode. */
8088 if (!rs->noack_mode)
c33e31fd 8089 remote_serial_write ("+", 1);
fee9eda9
YQ
8090 if (is_notif != NULL)
8091 *is_notif = 0;
0876f84a 8092 return val;
c906108c
SS
8093 }
8094
74531fed
PA
8095 /* If we got a notification, handle it, and go back to looking
8096 for a packet. */
8097 else
8098 {
8099 gdb_assert (c == '%');
8100
8101 if (remote_debug)
8102 {
6e5abd65
PA
8103 struct cleanup *old_chain;
8104 char *str;
8105
8106 str = escape_buffer (*buf, val);
8107 old_chain = make_cleanup (xfree, str);
8108 fprintf_unfiltered (gdb_stdlog,
8109 " Notification received: %s\n",
8110 str);
8111 do_cleanups (old_chain);
74531fed 8112 }
fee9eda9
YQ
8113 if (is_notif != NULL)
8114 *is_notif = 1;
c906108c 8115
5965e028 8116 handle_notification (rs->notif_state, *buf);
c906108c 8117
74531fed 8118 /* Notifications require no acknowledgement. */
a6f3e723 8119
74531fed 8120 if (expecting_notif)
fee9eda9 8121 return val;
74531fed
PA
8122 }
8123 }
8124}
8125
8126static int
8127getpkt_sane (char **buf, long *sizeof_buf, int forever)
8128{
fee9eda9 8129 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
8130}
8131
8132static int
fee9eda9
YQ
8133getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
8134 int *is_notif)
74531fed 8135{
fee9eda9
YQ
8136 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
8137 is_notif);
c906108c 8138}
74531fed 8139
cbb8991c
DB
8140/* Check whether EVENT is a fork event for the process specified
8141 by the pid passed in DATA, and if it is, kill the fork child. */
8142
8143static int
8144kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
8145 QUEUE_ITER (stop_reply_p) *iter,
8146 stop_reply_p event,
8147 void *data)
8148{
8149 struct queue_iter_param *param = data;
8150 int parent_pid = *(int *) param->input;
8151
8152 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
8153 {
8154 struct remote_state *rs = get_remote_state ();
8155 int child_pid = ptid_get_pid (event->ws.value.related_pid);
8156 int res;
8157
8158 res = remote_vkill (child_pid, rs);
8159 if (res != 0)
8160 error (_("Can't kill fork child process %d"), child_pid);
8161 }
8162
8163 return 1;
8164}
8165
8166/* Kill any new fork children of process PID that haven't been
8167 processed by follow_fork. */
8168
8169static void
8170kill_new_fork_children (int pid, struct remote_state *rs)
8171{
8172 struct thread_info *thread;
8173 struct notif_client *notif = &notif_client_stop;
8174 struct queue_iter_param param;
8175
8176 /* Kill the fork child threads of any threads in process PID
8177 that are stopped at a fork event. */
8178 ALL_NON_EXITED_THREADS (thread)
8179 {
8180 struct target_waitstatus *ws = &thread->pending_follow;
8181
8182 if (is_pending_fork_parent (ws, pid, thread->ptid))
8183 {
8184 struct remote_state *rs = get_remote_state ();
8185 int child_pid = ptid_get_pid (ws->value.related_pid);
8186 int res;
8187
8188 res = remote_vkill (child_pid, rs);
8189 if (res != 0)
8190 error (_("Can't kill fork child process %d"), child_pid);
8191 }
8192 }
8193
8194 /* Check for any pending fork events (not reported or processed yet)
8195 in process PID and kill those fork child threads as well. */
8196 remote_notif_get_pending_events (notif);
8197 param.input = &pid;
8198 param.output = NULL;
8199 QUEUE_iterate (stop_reply_p, stop_reply_queue,
8200 kill_child_of_pending_fork, &param);
8201}
8202
c906108c
SS
8203\f
8204static void
7d85a9c0 8205remote_kill (struct target_ops *ops)
43ff13b4 8206{
0fdf84ca
PA
8207
8208 /* Catch errors so the user can quit from gdb even when we
23860348 8209 aren't on speaking terms with the remote system. */
492d29ea 8210 TRY
0fdf84ca
PA
8211 {
8212 putpkt ("k");
8213 }
492d29ea 8214 CATCH (ex, RETURN_MASK_ERROR)
0fdf84ca
PA
8215 {
8216 if (ex.error == TARGET_CLOSE_ERROR)
8217 {
8218 /* If we got an (EOF) error that caused the target
8219 to go away, then we're done, that's what we wanted.
8220 "k" is susceptible to cause a premature EOF, given
8221 that the remote server isn't actually required to
8222 reply to "k", and it can happen that it doesn't
8223 even get to reply ACK to the "k". */
8224 return;
8225 }
8226
8227 /* Otherwise, something went wrong. We didn't actually kill
8228 the target. Just propagate the exception, and let the
8229 user or higher layers decide what to do. */
8230 throw_exception (ex);
8231 }
492d29ea 8232 END_CATCH
43ff13b4 8233
0fdf84ca
PA
8234 /* We've killed the remote end, we get to mourn it. Since this is
8235 target remote, single-process, mourning the inferior also
8236 unpushes remote_ops. */
43ff13b4
JM
8237 target_mourn_inferior ();
8238}
8239
82f73884
PA
8240static int
8241remote_vkill (int pid, struct remote_state *rs)
8242{
4082afcc 8243 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
8244 return -1;
8245
8246 /* Tell the remote target to detach. */
bba74b36 8247 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
8248 putpkt (rs->buf);
8249 getpkt (&rs->buf, &rs->buf_size, 0);
8250
4082afcc
PA
8251 switch (packet_ok (rs->buf,
8252 &remote_protocol_packets[PACKET_vKill]))
8253 {
8254 case PACKET_OK:
8255 return 0;
8256 case PACKET_ERROR:
8257 return 1;
8258 case PACKET_UNKNOWN:
8259 return -1;
8260 default:
8261 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
8262 }
82f73884
PA
8263}
8264
8265static void
7d85a9c0 8266extended_remote_kill (struct target_ops *ops)
82f73884
PA
8267{
8268 int res;
8269 int pid = ptid_get_pid (inferior_ptid);
8270 struct remote_state *rs = get_remote_state ();
8271
cbb8991c
DB
8272 /* If we're stopped while forking and we haven't followed yet, kill the
8273 child task. We need to do this before killing the parent task
8274 because if this is a vfork then the parent will be sleeping. */
8275 kill_new_fork_children (pid, rs);
8276
82f73884 8277 res = remote_vkill (pid, rs);
901f9912 8278 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
82f73884
PA
8279 {
8280 /* Don't try 'k' on a multi-process aware stub -- it has no way
8281 to specify the pid. */
8282
8283 putpkt ("k");
8284#if 0
8285 getpkt (&rs->buf, &rs->buf_size, 0);
8286 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
8287 res = 1;
8288#else
8289 /* Don't wait for it to die. I'm not really sure it matters whether
8290 we do or not. For the existing stubs, kill is a noop. */
8291 res = 0;
8292#endif
8293 }
8294
8295 if (res != 0)
8296 error (_("Can't kill process"));
8297
82f73884
PA
8298 target_mourn_inferior ();
8299}
8300
c906108c 8301static void
20f796c9 8302remote_mourn (struct target_ops *target)
c906108c
SS
8303{
8304 unpush_target (target);
ce5ce7ed 8305
8a2492ee
PA
8306 /* remote_close takes care of doing most of the clean up. */
8307 generic_mourn_inferior ();
c906108c
SS
8308}
8309
2d717e4f 8310static void
20f796c9 8311extended_remote_mourn (struct target_ops *target)
2d717e4f
DJ
8312{
8313 struct remote_state *rs = get_remote_state ();
c906108c 8314
e24a49d8
PA
8315 /* In case we got here due to an error, but we're going to stay
8316 connected. */
8317 rs->waiting_for_stop_reply = 0;
8318
dc1981d7
PA
8319 /* If the current general thread belonged to the process we just
8320 detached from or has exited, the remote side current general
8321 thread becomes undefined. Considering a case like this:
8322
8323 - We just got here due to a detach.
8324 - The process that we're detaching from happens to immediately
8325 report a global breakpoint being hit in non-stop mode, in the
8326 same thread we had selected before.
8327 - GDB attaches to this process again.
8328 - This event happens to be the next event we handle.
8329
8330 GDB would consider that the current general thread didn't need to
8331 be set on the stub side (with Hg), since for all it knew,
8332 GENERAL_THREAD hadn't changed.
8333
8334 Notice that although in all-stop mode, the remote server always
8335 sets the current thread to the thread reporting the stop event,
8336 that doesn't happen in non-stop mode; in non-stop, the stub *must
8337 not* change the current thread when reporting a breakpoint hit,
8338 due to the decoupling of event reporting and event handling.
8339
8340 To keep things simple, we always invalidate our notion of the
8341 current thread. */
47f8a51d 8342 record_currthread (rs, minus_one_ptid);
dc1981d7 8343
2d717e4f
DJ
8344 /* Unlike "target remote", we do not want to unpush the target; then
8345 the next time the user says "run", we won't be connected. */
8346
48aa3c27
PA
8347 /* Call common code to mark the inferior as not running. */
8348 generic_mourn_inferior ();
8349
d729566a 8350 if (!have_inferiors ())
2d717e4f 8351 {
82f73884
PA
8352 if (!remote_multi_process_p (rs))
8353 {
8354 /* Check whether the target is running now - some remote stubs
8355 automatically restart after kill. */
8356 putpkt ("?");
8357 getpkt (&rs->buf, &rs->buf_size, 0);
8358
8359 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
8360 {
3e43a32a
MS
8361 /* Assume that the target has been restarted. Set
8362 inferior_ptid so that bits of core GDB realizes
8363 there's something here, e.g., so that the user can
8364 say "kill" again. */
82f73884
PA
8365 inferior_ptid = magic_null_ptid;
8366 }
82f73884 8367 }
2d717e4f
DJ
8368 }
8369}
c906108c 8370
03583c20 8371static int
2bfc0540 8372extended_remote_supports_disable_randomization (struct target_ops *self)
03583c20 8373{
4082afcc 8374 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
8375}
8376
8377static void
8378extended_remote_disable_randomization (int val)
8379{
8380 struct remote_state *rs = get_remote_state ();
8381 char *reply;
8382
bba74b36
YQ
8383 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
8384 val);
03583c20
UW
8385 putpkt (rs->buf);
8386 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
8387 if (*reply == '\0')
8388 error (_("Target does not support QDisableRandomization."));
8389 if (strcmp (reply, "OK") != 0)
8390 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
8391}
8392
2d717e4f
DJ
8393static int
8394extended_remote_run (char *args)
8395{
8396 struct remote_state *rs = get_remote_state ();
2d717e4f 8397 int len;
c906108c 8398
2d717e4f
DJ
8399 /* If the user has disabled vRun support, or we have detected that
8400 support is not available, do not try it. */
4082afcc 8401 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 8402 return -1;
424163ea 8403
2d717e4f
DJ
8404 strcpy (rs->buf, "vRun;");
8405 len = strlen (rs->buf);
c906108c 8406
2d717e4f
DJ
8407 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
8408 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
8409 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
8410 strlen (remote_exec_file));
2d717e4f 8411
d1a41061 8412 gdb_assert (args != NULL);
2d717e4f
DJ
8413 if (*args)
8414 {
8415 struct cleanup *back_to;
8416 int i;
8417 char **argv;
8418
d1a41061 8419 argv = gdb_buildargv (args);
6e366df1 8420 back_to = make_cleanup_freeargv (argv);
2d717e4f
DJ
8421 for (i = 0; argv[i] != NULL; i++)
8422 {
8423 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
8424 error (_("Argument list too long for run packet"));
8425 rs->buf[len++] = ';';
9f1b45b0
TT
8426 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
8427 strlen (argv[i]));
2d717e4f
DJ
8428 }
8429 do_cleanups (back_to);
8430 }
8431
8432 rs->buf[len++] = '\0';
8433
8434 putpkt (rs->buf);
8435 getpkt (&rs->buf, &rs->buf_size, 0);
8436
4082afcc 8437 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 8438 {
4082afcc 8439 case PACKET_OK:
3405876a 8440 /* We have a wait response. All is well. */
2d717e4f 8441 return 0;
4082afcc
PA
8442 case PACKET_UNKNOWN:
8443 return -1;
8444 case PACKET_ERROR:
2d717e4f
DJ
8445 if (remote_exec_file[0] == '\0')
8446 error (_("Running the default executable on the remote target failed; "
8447 "try \"set remote exec-file\"?"));
8448 else
8449 error (_("Running \"%s\" on the remote target failed"),
8450 remote_exec_file);
4082afcc
PA
8451 default:
8452 gdb_assert_not_reached (_("bad switch"));
2d717e4f 8453 }
c906108c
SS
8454}
8455
2d717e4f
DJ
8456/* In the extended protocol we want to be able to do things like
8457 "run" and have them basically work as expected. So we need
8458 a special create_inferior function. We support changing the
8459 executable file and the command line arguments, but not the
8460 environment. */
8461
43ff13b4 8462static void
77a19445
TT
8463extended_remote_create_inferior (struct target_ops *ops,
8464 char *exec_file, char *args,
8465 char **env, int from_tty)
43ff13b4 8466{
3405876a
PA
8467 int run_worked;
8468 char *stop_reply;
8469 struct remote_state *rs = get_remote_state ();
8470
43ff13b4 8471 /* If running asynchronously, register the target file descriptor
23860348 8472 with the event loop. */
75c99385 8473 if (target_can_async_p ())
6a3753b3 8474 target_async (1);
43ff13b4 8475
03583c20 8476 /* Disable address space randomization if requested (and supported). */
2bfc0540 8477 if (extended_remote_supports_disable_randomization (ops))
03583c20
UW
8478 extended_remote_disable_randomization (disable_randomization);
8479
43ff13b4 8480 /* Now restart the remote server. */
3405876a
PA
8481 run_worked = extended_remote_run (args) != -1;
8482 if (!run_worked)
2d717e4f
DJ
8483 {
8484 /* vRun was not supported. Fail if we need it to do what the
8485 user requested. */
8486 if (remote_exec_file[0])
8487 error (_("Remote target does not support \"set remote exec-file\""));
8488 if (args[0])
8489 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 8490
2d717e4f
DJ
8491 /* Fall back to "R". */
8492 extended_remote_restart ();
8493 }
424163ea 8494
6c95b8df
PA
8495 if (!have_inferiors ())
8496 {
8497 /* Clean up from the last time we ran, before we mark the target
8498 running again. This will mark breakpoints uninserted, and
8499 get_offsets may insert breakpoints. */
8500 init_thread_list ();
8501 init_wait_for_inferior ();
8502 }
45280a52 8503
3405876a
PA
8504 /* vRun's success return is a stop reply. */
8505 stop_reply = run_worked ? rs->buf : NULL;
8506 add_current_inferior_and_thread (stop_reply);
c0a2216e 8507
2d717e4f
DJ
8508 /* Get updated offsets, if the stub uses qOffsets. */
8509 get_offsets ();
2d717e4f 8510}
c906108c 8511\f
c5aa993b 8512
b775012e
LM
8513/* Given a location's target info BP_TGT and the packet buffer BUF, output
8514 the list of conditions (in agent expression bytecode format), if any, the
8515 target needs to evaluate. The output is placed into the packet buffer
bba74b36 8516 started from BUF and ended at BUF_END. */
b775012e
LM
8517
8518static int
8519remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
8520 struct bp_target_info *bp_tgt, char *buf,
8521 char *buf_end)
b775012e
LM
8522{
8523 struct agent_expr *aexpr = NULL;
8524 int i, ix;
8525 char *pkt;
8526 char *buf_start = buf;
8527
8528 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8529 return 0;
8530
8531 buf += strlen (buf);
bba74b36 8532 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
8533 buf++;
8534
8535 /* Send conditions to the target and free the vector. */
8536 for (ix = 0;
8537 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8538 ix++)
8539 {
bba74b36 8540 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e
LM
8541 buf += strlen (buf);
8542 for (i = 0; i < aexpr->len; ++i)
8543 buf = pack_hex_byte (buf, aexpr->buf[i]);
8544 *buf = '\0';
8545 }
b775012e
LM
8546 return 0;
8547}
8548
d3ce09f5
SS
8549static void
8550remote_add_target_side_commands (struct gdbarch *gdbarch,
8551 struct bp_target_info *bp_tgt, char *buf)
8552{
8553 struct agent_expr *aexpr = NULL;
8554 int i, ix;
8555
8556 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8557 return;
8558
8559 buf += strlen (buf);
8560
8561 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8562 buf += strlen (buf);
8563
8564 /* Concatenate all the agent expressions that are commands into the
8565 cmds parameter. */
8566 for (ix = 0;
8567 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8568 ix++)
8569 {
8570 sprintf (buf, "X%x,", aexpr->len);
8571 buf += strlen (buf);
8572 for (i = 0; i < aexpr->len; ++i)
8573 buf = pack_hex_byte (buf, aexpr->buf[i]);
8574 *buf = '\0';
8575 }
d3ce09f5
SS
8576}
8577
8181d85f
DJ
8578/* Insert a breakpoint. On targets that have software breakpoint
8579 support, we ask the remote target to do the work; on targets
8580 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
8581
8582static int
3db08215
MM
8583remote_insert_breakpoint (struct target_ops *ops,
8584 struct gdbarch *gdbarch,
a6d9a66e 8585 struct bp_target_info *bp_tgt)
c906108c 8586{
d471ea57
AC
8587 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8588 If it succeeds, then set the support to PACKET_ENABLE. If it
8589 fails, and the user has explicitly requested the Z support then
23860348 8590 report an error, otherwise, mark it disabled and go on. */
802188a7 8591
4082afcc 8592 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8593 {
0d5ed153 8594 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8595 struct remote_state *rs;
bba74b36 8596 char *p, *endbuf;
7c0f6dcc 8597 int bpsize;
b775012e 8598 struct condition_list *cond = NULL;
4fff2411 8599
28439a30
PA
8600 /* Make sure the remote is pointing at the right process, if
8601 necessary. */
8602 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8603 set_general_process ();
8604
a1dcb23a 8605 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
4fff2411
JZ
8606
8607 rs = get_remote_state ();
8608 p = rs->buf;
bba74b36 8609 endbuf = rs->buf + get_remote_packet_size ();
802188a7 8610
96baa820
JM
8611 *(p++) = 'Z';
8612 *(p++) = '0';
8613 *(p++) = ',';
7c0f6dcc 8614 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 8615 p += hexnumstr (p, addr);
bba74b36 8616 xsnprintf (p, endbuf - p, ",%d", bpsize);
802188a7 8617
efcc2da7 8618 if (remote_supports_cond_breakpoints (ops))
bba74b36 8619 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8620
78eff0ec 8621 if (remote_can_run_breakpoint_commands (ops))
d3ce09f5
SS
8622 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8623
6d820c5c
DJ
8624 putpkt (rs->buf);
8625 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8626
6d820c5c 8627 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 8628 {
d471ea57
AC
8629 case PACKET_ERROR:
8630 return -1;
8631 case PACKET_OK:
7c0f6dcc
JL
8632 bp_tgt->placed_address = addr;
8633 bp_tgt->placed_size = bpsize;
d471ea57
AC
8634 return 0;
8635 case PACKET_UNKNOWN:
8636 break;
96baa820
JM
8637 }
8638 }
c906108c 8639
0000e5cc
PA
8640 /* If this breakpoint has target-side commands but this stub doesn't
8641 support Z0 packets, throw error. */
8642 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8643 throw_error (NOT_SUPPORTED_ERROR, _("\
8644Target doesn't support breakpoints that have target side commands."));
8645
3db08215 8646 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8647}
8648
8649static int
3db08215
MM
8650remote_remove_breakpoint (struct target_ops *ops,
8651 struct gdbarch *gdbarch,
a6d9a66e 8652 struct bp_target_info *bp_tgt)
c906108c 8653{
8181d85f 8654 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 8655 struct remote_state *rs = get_remote_state ();
96baa820 8656
4082afcc 8657 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8658 {
6d820c5c 8659 char *p = rs->buf;
bba74b36 8660 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 8661
28439a30
PA
8662 /* Make sure the remote is pointing at the right process, if
8663 necessary. */
8664 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8665 set_general_process ();
8666
96baa820
JM
8667 *(p++) = 'z';
8668 *(p++) = '0';
8669 *(p++) = ',';
8670
8181d85f
DJ
8671 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8672 p += hexnumstr (p, addr);
bba74b36 8673 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
802188a7 8674
6d820c5c
DJ
8675 putpkt (rs->buf);
8676 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8677
6d820c5c 8678 return (rs->buf[0] == 'E');
96baa820
JM
8679 }
8680
3db08215 8681 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8682}
8683
d471ea57
AC
8684static int
8685watchpoint_to_Z_packet (int type)
8686{
8687 switch (type)
8688 {
8689 case hw_write:
bb858e6a 8690 return Z_PACKET_WRITE_WP;
d471ea57
AC
8691 break;
8692 case hw_read:
bb858e6a 8693 return Z_PACKET_READ_WP;
d471ea57
AC
8694 break;
8695 case hw_access:
bb858e6a 8696 return Z_PACKET_ACCESS_WP;
d471ea57
AC
8697 break;
8698 default:
8e65ff28 8699 internal_error (__FILE__, __LINE__,
e2e0b3e5 8700 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
8701 }
8702}
8703
3c3bea1c 8704static int
7bb99c53
TT
8705remote_insert_watchpoint (struct target_ops *self,
8706 CORE_ADDR addr, int len, int type,
0cf6dd15 8707 struct expression *cond)
96baa820 8708{
d01949b6 8709 struct remote_state *rs = get_remote_state ();
bba74b36 8710 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8711 char *p;
d471ea57 8712 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 8713
4082afcc 8714 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 8715 return 1;
802188a7 8716
28439a30
PA
8717 /* Make sure the remote is pointing at the right process, if
8718 necessary. */
8719 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8720 set_general_process ();
8721
bba74b36 8722 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 8723 p = strchr (rs->buf, '\0');
96baa820
JM
8724 addr = remote_address_masked (addr);
8725 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8726 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 8727
6d820c5c
DJ
8728 putpkt (rs->buf);
8729 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8730
6d820c5c 8731 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8732 {
8733 case PACKET_ERROR:
d471ea57 8734 return -1;
85d721b8
PA
8735 case PACKET_UNKNOWN:
8736 return 1;
d471ea57
AC
8737 case PACKET_OK:
8738 return 0;
8739 }
8e65ff28 8740 internal_error (__FILE__, __LINE__,
e2e0b3e5 8741 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
8742}
8743
283002cf
MR
8744static int
8745remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8746 CORE_ADDR start, int length)
8747{
8748 CORE_ADDR diff = remote_address_masked (addr - start);
8749
8750 return diff < length;
8751}
8752
d471ea57 8753
3c3bea1c 8754static int
11b5219a
TT
8755remote_remove_watchpoint (struct target_ops *self,
8756 CORE_ADDR addr, int len, int type,
0cf6dd15 8757 struct expression *cond)
96baa820 8758{
d01949b6 8759 struct remote_state *rs = get_remote_state ();
bba74b36 8760 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8761 char *p;
d471ea57
AC
8762 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8763
4082afcc 8764 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 8765 return -1;
802188a7 8766
28439a30
PA
8767 /* Make sure the remote is pointing at the right process, if
8768 necessary. */
8769 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8770 set_general_process ();
8771
bba74b36 8772 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 8773 p = strchr (rs->buf, '\0');
96baa820
JM
8774 addr = remote_address_masked (addr);
8775 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8776 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
8777 putpkt (rs->buf);
8778 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8779
6d820c5c 8780 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8781 {
8782 case PACKET_ERROR:
8783 case PACKET_UNKNOWN:
8784 return -1;
8785 case PACKET_OK:
8786 return 0;
8787 }
8e65ff28 8788 internal_error (__FILE__, __LINE__,
e2e0b3e5 8789 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
8790}
8791
3c3bea1c 8792
501eef12 8793int remote_hw_watchpoint_limit = -1;
480a3f21 8794int remote_hw_watchpoint_length_limit = -1;
501eef12 8795int remote_hw_breakpoint_limit = -1;
d471ea57 8796
480a3f21 8797static int
31568a15
TT
8798remote_region_ok_for_hw_watchpoint (struct target_ops *self,
8799 CORE_ADDR addr, int len)
480a3f21
PW
8800{
8801 if (remote_hw_watchpoint_length_limit == 0)
8802 return 0;
8803 else if (remote_hw_watchpoint_length_limit < 0)
8804 return 1;
8805 else if (len <= remote_hw_watchpoint_length_limit)
8806 return 1;
8807 else
8808 return 0;
8809}
8810
b9362cc7 8811static int
5461485a
TT
8812remote_check_watch_resources (struct target_ops *self,
8813 int type, int cnt, int ot)
96baa820 8814{
3c3bea1c
GS
8815 if (type == bp_hardware_breakpoint)
8816 {
8817 if (remote_hw_breakpoint_limit == 0)
8818 return 0;
501eef12
AC
8819 else if (remote_hw_breakpoint_limit < 0)
8820 return 1;
3c3bea1c
GS
8821 else if (cnt <= remote_hw_breakpoint_limit)
8822 return 1;
8823 }
8824 else
8825 {
8826 if (remote_hw_watchpoint_limit == 0)
8827 return 0;
501eef12
AC
8828 else if (remote_hw_watchpoint_limit < 0)
8829 return 1;
3c3bea1c
GS
8830 else if (ot)
8831 return -1;
8832 else if (cnt <= remote_hw_watchpoint_limit)
8833 return 1;
8834 }
8835 return -1;
8836}
8837
f7e6eed5
PA
8838/* The to_stopped_by_sw_breakpoint method of target remote. */
8839
8840static int
8841remote_stopped_by_sw_breakpoint (struct target_ops *ops)
8842{
8843 struct remote_state *rs = get_remote_state ();
8844
8845 return rs->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
8846}
8847
8848/* The to_supports_stopped_by_sw_breakpoint method of target
8849 remote. */
8850
8851static int
8852remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
8853{
8854 struct remote_state *rs = get_remote_state ();
8855
8856 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
8857}
8858
8859/* The to_stopped_by_hw_breakpoint method of target remote. */
8860
8861static int
8862remote_stopped_by_hw_breakpoint (struct target_ops *ops)
8863{
8864 struct remote_state *rs = get_remote_state ();
8865
8866 return rs->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
8867}
8868
8869/* The to_supports_stopped_by_hw_breakpoint method of target
8870 remote. */
8871
8872static int
8873remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
8874{
8875 struct remote_state *rs = get_remote_state ();
8876
8877 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
8878}
8879
b9362cc7 8880static int
6a109b6b 8881remote_stopped_by_watchpoint (struct target_ops *ops)
3c3bea1c 8882{
ee154bee
TT
8883 struct remote_state *rs = get_remote_state ();
8884
f7e6eed5 8885 return rs->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
3c3bea1c
GS
8886}
8887
4aa7a7f5
JJ
8888static int
8889remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 8890{
ee154bee 8891 struct remote_state *rs = get_remote_state ();
4aa7a7f5 8892 int rc = 0;
a744cf53 8893
6a109b6b 8894 if (remote_stopped_by_watchpoint (target))
4aa7a7f5 8895 {
ee154bee 8896 *addr_p = rs->remote_watch_data_address;
4aa7a7f5
JJ
8897 rc = 1;
8898 }
8899
8900 return rc;
3c3bea1c
GS
8901}
8902
8903
8904static int
23a26771 8905remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8906 struct bp_target_info *bp_tgt)
3c3bea1c 8907{
0d5ed153 8908 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8909 struct remote_state *rs;
bba74b36 8910 char *p, *endbuf;
dd61ec5c 8911 char *message;
0d5ed153 8912 int bpsize;
802188a7 8913
c8189ed1 8914 /* The length field should be set to the size of a breakpoint
8181d85f 8915 instruction, even though we aren't inserting one ourselves. */
c8189ed1 8916
0d5ed153 8917 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
3c3bea1c 8918
4082afcc 8919 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 8920 return -1;
2bc416ba 8921
28439a30
PA
8922 /* Make sure the remote is pointing at the right process, if
8923 necessary. */
8924 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8925 set_general_process ();
8926
4fff2411
JZ
8927 rs = get_remote_state ();
8928 p = rs->buf;
bba74b36 8929 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 8930
96baa820
JM
8931 *(p++) = 'Z';
8932 *(p++) = '1';
8933 *(p++) = ',';
802188a7 8934
0d5ed153 8935 addr = remote_address_masked (addr);
96baa820 8936 p += hexnumstr (p, (ULONGEST) addr);
0d5ed153 8937 xsnprintf (p, endbuf - p, ",%x", bpsize);
96baa820 8938
efcc2da7 8939 if (remote_supports_cond_breakpoints (self))
bba74b36 8940 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8941
78eff0ec 8942 if (remote_can_run_breakpoint_commands (self))
d3ce09f5
SS
8943 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8944
6d820c5c
DJ
8945 putpkt (rs->buf);
8946 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8947
6d820c5c 8948 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8949 {
8950 case PACKET_ERROR:
dd61ec5c
MW
8951 if (rs->buf[1] == '.')
8952 {
8953 message = strchr (rs->buf + 2, '.');
8954 if (message)
0316657e 8955 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
8956 }
8957 return -1;
d471ea57
AC
8958 case PACKET_UNKNOWN:
8959 return -1;
8960 case PACKET_OK:
0d5ed153
MR
8961 bp_tgt->placed_address = addr;
8962 bp_tgt->placed_size = bpsize;
d471ea57
AC
8963 return 0;
8964 }
8e65ff28 8965 internal_error (__FILE__, __LINE__,
e2e0b3e5 8966 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
8967}
8968
d471ea57 8969
802188a7 8970static int
a64dc96c 8971remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8972 struct bp_target_info *bp_tgt)
96baa820 8973{
8181d85f 8974 CORE_ADDR addr;
d01949b6 8975 struct remote_state *rs = get_remote_state ();
6d820c5c 8976 char *p = rs->buf;
bba74b36 8977 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 8978
4082afcc 8979 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 8980 return -1;
802188a7 8981
28439a30
PA
8982 /* Make sure the remote is pointing at the right process, if
8983 necessary. */
8984 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8985 set_general_process ();
8986
96baa820
JM
8987 *(p++) = 'z';
8988 *(p++) = '1';
8989 *(p++) = ',';
802188a7 8990
8181d85f 8991 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 8992 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8993 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
96baa820 8994
6d820c5c
DJ
8995 putpkt (rs->buf);
8996 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 8997
6d820c5c 8998 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8999 {
9000 case PACKET_ERROR:
9001 case PACKET_UNKNOWN:
9002 return -1;
9003 case PACKET_OK:
9004 return 0;
9005 }
8e65ff28 9006 internal_error (__FILE__, __LINE__,
e2e0b3e5 9007 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 9008}
96baa820 9009
4a5e7a5b
PA
9010/* Verify memory using the "qCRC:" request. */
9011
9012static int
9013remote_verify_memory (struct target_ops *ops,
9014 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
9015{
9016 struct remote_state *rs = get_remote_state ();
9017 unsigned long host_crc, target_crc;
9018 char *tmp;
9019
936d2992
PA
9020 /* It doesn't make sense to use qCRC if the remote target is
9021 connected but not running. */
9022 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
9023 {
9024 enum packet_result result;
28439a30 9025
936d2992
PA
9026 /* Make sure the remote is pointing at the right process. */
9027 set_general_process ();
4a5e7a5b 9028
936d2992
PA
9029 /* FIXME: assumes lma can fit into long. */
9030 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
9031 (long) lma, (long) size);
9032 putpkt (rs->buf);
4a5e7a5b 9033
936d2992
PA
9034 /* Be clever; compute the host_crc before waiting for target
9035 reply. */
9036 host_crc = xcrc32 (data, size, 0xffffffff);
9037
9038 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 9039
936d2992
PA
9040 result = packet_ok (rs->buf,
9041 &remote_protocol_packets[PACKET_qCRC]);
9042 if (result == PACKET_ERROR)
9043 return -1;
9044 else if (result == PACKET_OK)
9045 {
9046 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
9047 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 9048
936d2992
PA
9049 return (host_crc == target_crc);
9050 }
9051 }
4a5e7a5b 9052
936d2992 9053 return simple_verify_memory (ops, data, lma, size);
4a5e7a5b
PA
9054}
9055
c906108c
SS
9056/* compare-sections command
9057
9058 With no arguments, compares each loadable section in the exec bfd
9059 with the same memory range on the target, and reports mismatches.
4a5e7a5b 9060 Useful for verifying the image on the target against the exec file. */
e514a9d6 9061
c906108c 9062static void
fba45db2 9063compare_sections_command (char *args, int from_tty)
c906108c
SS
9064{
9065 asection *s;
c906108c 9066 struct cleanup *old_chain;
948f8e3d 9067 gdb_byte *sectdata;
ce359b09 9068 const char *sectname;
c906108c
SS
9069 bfd_size_type size;
9070 bfd_vma lma;
9071 int matched = 0;
9072 int mismatched = 0;
4a5e7a5b 9073 int res;
95cf3b38 9074 int read_only = 0;
c906108c
SS
9075
9076 if (!exec_bfd)
8a3fe4f8 9077 error (_("command cannot be used without an exec file"));
c906108c 9078
28439a30
PA
9079 /* Make sure the remote is pointing at the right process. */
9080 set_general_process ();
9081
95cf3b38
DT
9082 if (args != NULL && strcmp (args, "-r") == 0)
9083 {
9084 read_only = 1;
9085 args = NULL;
9086 }
9087
c5aa993b 9088 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
9089 {
9090 if (!(s->flags & SEC_LOAD))
0df8b418 9091 continue; /* Skip non-loadable section. */
c906108c 9092
95cf3b38
DT
9093 if (read_only && (s->flags & SEC_READONLY) == 0)
9094 continue; /* Skip writeable sections */
9095
2c500098 9096 size = bfd_get_section_size (s);
c906108c 9097 if (size == 0)
0df8b418 9098 continue; /* Skip zero-length section. */
c906108c 9099
ce359b09 9100 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 9101 if (args && strcmp (args, sectname) != 0)
0df8b418 9102 continue; /* Not the section selected by user. */
c906108c 9103
0df8b418 9104 matched = 1; /* Do this section. */
c906108c 9105 lma = s->lma;
c906108c 9106
c906108c 9107 sectdata = xmalloc (size);
b8c9b27d 9108 old_chain = make_cleanup (xfree, sectdata);
c906108c 9109 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 9110
4a5e7a5b
PA
9111 res = target_verify_memory (sectdata, lma, size);
9112
9113 if (res == -1)
5af949e3 9114 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
9115 paddress (target_gdbarch (), lma),
9116 paddress (target_gdbarch (), lma + size));
c906108c 9117
5af949e3 9118 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
9119 paddress (target_gdbarch (), lma),
9120 paddress (target_gdbarch (), lma + size));
4a5e7a5b 9121 if (res)
c906108c
SS
9122 printf_filtered ("matched.\n");
9123 else
c5aa993b
JM
9124 {
9125 printf_filtered ("MIS-MATCHED!\n");
9126 mismatched++;
9127 }
c906108c
SS
9128
9129 do_cleanups (old_chain);
9130 }
9131 if (mismatched > 0)
936d2992 9132 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 9133the loaded file\n"));
c906108c 9134 if (args && !matched)
a3f17187 9135 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
9136}
9137
0e7f50da
UW
9138/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
9139 into remote target. The number of bytes written to the remote
9140 target is returned, or -1 for error. */
9141
9b409511 9142static enum target_xfer_status
0e7f50da
UW
9143remote_write_qxfer (struct target_ops *ops, const char *object_name,
9144 const char *annex, const gdb_byte *writebuf,
9b409511 9145 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
9146 struct packet_config *packet)
9147{
9148 int i, buf_len;
9149 ULONGEST n;
0e7f50da
UW
9150 struct remote_state *rs = get_remote_state ();
9151 int max_size = get_memory_write_packet_size ();
9152
9153 if (packet->support == PACKET_DISABLE)
2ed4b548 9154 return TARGET_XFER_E_IO;
0e7f50da
UW
9155
9156 /* Insert header. */
9157 i = snprintf (rs->buf, max_size,
9158 "qXfer:%s:write:%s:%s:",
9159 object_name, annex ? annex : "",
9160 phex_nz (offset, sizeof offset));
9161 max_size -= (i + 1);
9162
9163 /* Escape as much data as fits into rs->buf. */
9164 buf_len = remote_escape_output
124e13d9 9165 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
9166
9167 if (putpkt_binary (rs->buf, i + buf_len) < 0
9168 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9169 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9170 return TARGET_XFER_E_IO;
0e7f50da
UW
9171
9172 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
9173
9174 *xfered_len = n;
9175 return TARGET_XFER_OK;
0e7f50da
UW
9176}
9177
0876f84a
DJ
9178/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
9179 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
9180 number of bytes read is returned, or 0 for EOF, or -1 for error.
9181 The number of bytes read may be less than LEN without indicating an
9182 EOF. PACKET is checked and updated to indicate whether the remote
9183 target supports this object. */
9184
9b409511 9185static enum target_xfer_status
0876f84a
DJ
9186remote_read_qxfer (struct target_ops *ops, const char *object_name,
9187 const char *annex,
9188 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 9189 ULONGEST *xfered_len,
0876f84a
DJ
9190 struct packet_config *packet)
9191{
0876f84a 9192 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
9193 LONGEST i, n, packet_len;
9194
9195 if (packet->support == PACKET_DISABLE)
2ed4b548 9196 return TARGET_XFER_E_IO;
0876f84a
DJ
9197
9198 /* Check whether we've cached an end-of-object packet that matches
9199 this request. */
8e88304f 9200 if (rs->finished_object)
0876f84a 9201 {
8e88304f
TT
9202 if (strcmp (object_name, rs->finished_object) == 0
9203 && strcmp (annex ? annex : "", rs->finished_annex) == 0
9204 && offset == rs->finished_offset)
9b409511
YQ
9205 return TARGET_XFER_EOF;
9206
0876f84a
DJ
9207
9208 /* Otherwise, we're now reading something different. Discard
9209 the cache. */
8e88304f
TT
9210 xfree (rs->finished_object);
9211 xfree (rs->finished_annex);
9212 rs->finished_object = NULL;
9213 rs->finished_annex = NULL;
0876f84a
DJ
9214 }
9215
9216 /* Request only enough to fit in a single packet. The actual data
9217 may not, since we don't know how much of it will need to be escaped;
9218 the target is free to respond with slightly less data. We subtract
9219 five to account for the response type and the protocol frame. */
9220 n = min (get_remote_packet_size () - 5, len);
9221 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
9222 object_name, annex ? annex : "",
9223 phex_nz (offset, sizeof offset),
9224 phex_nz (n, sizeof n));
9225 i = putpkt (rs->buf);
9226 if (i < 0)
2ed4b548 9227 return TARGET_XFER_E_IO;
0876f84a
DJ
9228
9229 rs->buf[0] = '\0';
9230 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9231 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9232 return TARGET_XFER_E_IO;
0876f84a
DJ
9233
9234 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
9235 error (_("Unknown remote qXfer reply: %s"), rs->buf);
9236
9237 /* 'm' means there is (or at least might be) more data after this
9238 batch. That does not make sense unless there's at least one byte
9239 of data in this reply. */
9240 if (rs->buf[0] == 'm' && packet_len == 1)
9241 error (_("Remote qXfer reply contained no data."));
9242
9243 /* Got some data. */
bc20a4af
PA
9244 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
9245 packet_len - 1, readbuf, n);
0876f84a
DJ
9246
9247 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
9248 or possibly empty. If we have the final block of a non-empty
9249 object, record this fact to bypass a subsequent partial read. */
9250 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 9251 {
8e88304f
TT
9252 rs->finished_object = xstrdup (object_name);
9253 rs->finished_annex = xstrdup (annex ? annex : "");
9254 rs->finished_offset = offset + i;
0876f84a
DJ
9255 }
9256
9b409511
YQ
9257 if (i == 0)
9258 return TARGET_XFER_EOF;
9259 else
9260 {
9261 *xfered_len = i;
9262 return TARGET_XFER_OK;
9263 }
0876f84a
DJ
9264}
9265
9b409511 9266static enum target_xfer_status
4b8a223f 9267remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5 9268 const char *annex, gdb_byte *readbuf,
9b409511
YQ
9269 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
9270 ULONGEST *xfered_len)
c906108c 9271{
82f73884 9272 struct remote_state *rs;
c906108c 9273 int i;
6d820c5c 9274 char *p2;
1e3ff5ad 9275 char query_type;
124e13d9 9276 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
c906108c 9277
e6e4e701 9278 set_remote_traceframe ();
82f73884
PA
9279 set_general_thread (inferior_ptid);
9280
9281 rs = get_remote_state ();
9282
b2182ed2 9283 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
9284 if (object == TARGET_OBJECT_MEMORY)
9285 {
2d717e4f
DJ
9286 /* If the remote target is connected but not running, we should
9287 pass this request down to a lower stratum (e.g. the executable
9288 file). */
9289 if (!target_has_execution)
9b409511 9290 return TARGET_XFER_EOF;
2d717e4f 9291
21e3b9b9 9292 if (writebuf != NULL)
124e13d9
SM
9293 return remote_write_bytes (offset, writebuf, len, unit_size,
9294 xfered_len);
21e3b9b9 9295 else
124e13d9
SM
9296 return remote_read_bytes (ops, offset, readbuf, len, unit_size,
9297 xfered_len);
21e3b9b9
DJ
9298 }
9299
0df8b418 9300 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
9301 if (object == TARGET_OBJECT_SPU)
9302 {
9303 if (readbuf)
9304 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9b409511
YQ
9305 xfered_len, &remote_protocol_packets
9306 [PACKET_qXfer_spu_read]);
0e7f50da
UW
9307 else
9308 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9b409511
YQ
9309 xfered_len, &remote_protocol_packets
9310 [PACKET_qXfer_spu_write]);
0e7f50da
UW
9311 }
9312
4aa995e1
PA
9313 /* Handle extra signal info using qxfer packets. */
9314 if (object == TARGET_OBJECT_SIGNAL_INFO)
9315 {
9316 if (readbuf)
9317 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9b409511 9318 xfered_len, &remote_protocol_packets
4aa995e1
PA
9319 [PACKET_qXfer_siginfo_read]);
9320 else
3e43a32a 9321 return remote_write_qxfer (ops, "siginfo", annex,
9b409511 9322 writebuf, offset, len, xfered_len,
4aa995e1
PA
9323 &remote_protocol_packets
9324 [PACKET_qXfer_siginfo_write]);
9325 }
9326
0fb4aa4b
PA
9327 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
9328 {
9329 if (readbuf)
3e43a32a 9330 return remote_read_qxfer (ops, "statictrace", annex,
9b409511 9331 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
9332 &remote_protocol_packets
9333 [PACKET_qXfer_statictrace_read]);
9334 else
2ed4b548 9335 return TARGET_XFER_E_IO;
0fb4aa4b
PA
9336 }
9337
a76d924d
DJ
9338 /* Only handle flash writes. */
9339 if (writebuf != NULL)
9340 {
9341 LONGEST xfered;
9342
9343 switch (object)
9344 {
9345 case TARGET_OBJECT_FLASH:
9b409511
YQ
9346 return remote_flash_write (ops, offset, len, xfered_len,
9347 writebuf);
a76d924d
DJ
9348
9349 default:
2ed4b548 9350 return TARGET_XFER_E_IO;
a76d924d
DJ
9351 }
9352 }
4b8a223f 9353
1e3ff5ad
AC
9354 /* Map pre-existing objects onto letters. DO NOT do this for new
9355 objects!!! Instead specify new query packets. */
9356 switch (object)
c906108c 9357 {
1e3ff5ad
AC
9358 case TARGET_OBJECT_AVR:
9359 query_type = 'R';
9360 break;
802188a7
RM
9361
9362 case TARGET_OBJECT_AUXV:
0876f84a
DJ
9363 gdb_assert (annex == NULL);
9364 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9b409511 9365 xfered_len,
0876f84a 9366 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 9367
23181151
DJ
9368 case TARGET_OBJECT_AVAILABLE_FEATURES:
9369 return remote_read_qxfer
9b409511 9370 (ops, "features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
9371 &remote_protocol_packets[PACKET_qXfer_features]);
9372
cfa9d6d9
DJ
9373 case TARGET_OBJECT_LIBRARIES:
9374 return remote_read_qxfer
9b409511 9375 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
9376 &remote_protocol_packets[PACKET_qXfer_libraries]);
9377
2268b414
JK
9378 case TARGET_OBJECT_LIBRARIES_SVR4:
9379 return remote_read_qxfer
9b409511 9380 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
9381 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
9382
fd79ecee
DJ
9383 case TARGET_OBJECT_MEMORY_MAP:
9384 gdb_assert (annex == NULL);
9385 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9b409511 9386 xfered_len,
fd79ecee
DJ
9387 &remote_protocol_packets[PACKET_qXfer_memory_map]);
9388
07e059b5
VP
9389 case TARGET_OBJECT_OSDATA:
9390 /* Should only get here if we're connected. */
5d93a237 9391 gdb_assert (rs->remote_desc);
07e059b5 9392 return remote_read_qxfer
9b409511 9393 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
9394 &remote_protocol_packets[PACKET_qXfer_osdata]);
9395
dc146f7c
VP
9396 case TARGET_OBJECT_THREADS:
9397 gdb_assert (annex == NULL);
9398 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9b409511 9399 xfered_len,
dc146f7c
VP
9400 &remote_protocol_packets[PACKET_qXfer_threads]);
9401
b3b9301e
PA
9402 case TARGET_OBJECT_TRACEFRAME_INFO:
9403 gdb_assert (annex == NULL);
9404 return remote_read_qxfer
9b409511 9405 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 9406 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
9407
9408 case TARGET_OBJECT_FDPIC:
9409 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9b409511 9410 xfered_len,
78d85199 9411 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
9412
9413 case TARGET_OBJECT_OPENVMS_UIB:
9414 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9b409511 9415 xfered_len,
169081d0
TG
9416 &remote_protocol_packets[PACKET_qXfer_uib]);
9417
9accd112
MM
9418 case TARGET_OBJECT_BTRACE:
9419 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9b409511 9420 xfered_len,
9accd112
MM
9421 &remote_protocol_packets[PACKET_qXfer_btrace]);
9422
f4abbc16
MM
9423 case TARGET_OBJECT_BTRACE_CONF:
9424 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
9425 len, xfered_len,
9426 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
9427
c78fa86a
GB
9428 case TARGET_OBJECT_EXEC_FILE:
9429 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
9430 len, xfered_len,
9431 &remote_protocol_packets[PACKET_qXfer_exec_file]);
9432
1e3ff5ad 9433 default:
2ed4b548 9434 return TARGET_XFER_E_IO;
c906108c
SS
9435 }
9436
0df8b418 9437 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 9438 large enough let the caller deal with it. */
ea9c271d 9439 if (len < get_remote_packet_size ())
2ed4b548 9440 return TARGET_XFER_E_IO;
ea9c271d 9441 len = get_remote_packet_size ();
1e3ff5ad 9442
23860348 9443 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 9444 if (!rs->remote_desc)
8a3fe4f8 9445 error (_("remote query is only available after target open"));
c906108c 9446
1e3ff5ad 9447 gdb_assert (annex != NULL);
4b8a223f 9448 gdb_assert (readbuf != NULL);
c906108c 9449
6d820c5c 9450 p2 = rs->buf;
c906108c
SS
9451 *p2++ = 'q';
9452 *p2++ = query_type;
9453
23860348
MS
9454 /* We used one buffer char for the remote protocol q command and
9455 another for the query type. As the remote protocol encapsulation
9456 uses 4 chars plus one extra in case we are debugging
9457 (remote_debug), we have PBUFZIZ - 7 left to pack the query
9458 string. */
c906108c 9459 i = 0;
ea9c271d 9460 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 9461 {
1e3ff5ad
AC
9462 /* Bad caller may have sent forbidden characters. */
9463 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
9464 *p2++ = annex[i];
c906108c
SS
9465 i++;
9466 }
1e3ff5ad
AC
9467 *p2 = '\0';
9468 gdb_assert (annex[i] == '\0');
c906108c 9469
6d820c5c 9470 i = putpkt (rs->buf);
c5aa993b 9471 if (i < 0)
2ed4b548 9472 return TARGET_XFER_E_IO;
c906108c 9473
6d820c5c
DJ
9474 getpkt (&rs->buf, &rs->buf_size, 0);
9475 strcpy ((char *) readbuf, rs->buf);
c906108c 9476
9b409511
YQ
9477 *xfered_len = strlen ((char *) readbuf);
9478 return TARGET_XFER_OK;
c906108c
SS
9479}
9480
08388c79
DE
9481static int
9482remote_search_memory (struct target_ops* ops,
9483 CORE_ADDR start_addr, ULONGEST search_space_len,
9484 const gdb_byte *pattern, ULONGEST pattern_len,
9485 CORE_ADDR *found_addrp)
9486{
f5656ead 9487 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
9488 struct remote_state *rs = get_remote_state ();
9489 int max_size = get_memory_write_packet_size ();
9490 struct packet_config *packet =
9491 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
9492 /* Number of packet bytes used to encode the pattern;
9493 this could be more than PATTERN_LEN due to escape characters. */
08388c79 9494 int escaped_pattern_len;
0df8b418 9495 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
9496 int used_pattern_len;
9497 int i;
9498 int found;
9499 ULONGEST found_addr;
9500
9501 /* Don't go to the target if we don't have to.
9502 This is done before checking packet->support to avoid the possibility that
9503 a success for this edge case means the facility works in general. */
9504 if (pattern_len > search_space_len)
9505 return 0;
9506 if (pattern_len == 0)
9507 {
9508 *found_addrp = start_addr;
9509 return 1;
9510 }
9511
9512 /* If we already know the packet isn't supported, fall back to the simple
9513 way of searching memory. */
9514
4082afcc 9515 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
9516 {
9517 /* Target doesn't provided special support, fall back and use the
9518 standard support (copy memory and do the search here). */
9519 return simple_search_memory (ops, start_addr, search_space_len,
9520 pattern, pattern_len, found_addrp);
9521 }
9522
28439a30
PA
9523 /* Make sure the remote is pointing at the right process. */
9524 set_general_process ();
9525
08388c79
DE
9526 /* Insert header. */
9527 i = snprintf (rs->buf, max_size,
9528 "qSearch:memory:%s;%s;",
5af949e3 9529 phex_nz (start_addr, addr_size),
08388c79
DE
9530 phex_nz (search_space_len, sizeof (search_space_len)));
9531 max_size -= (i + 1);
9532
9533 /* Escape as much data as fits into rs->buf. */
9534 escaped_pattern_len =
124e13d9 9535 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
08388c79
DE
9536 &used_pattern_len, max_size);
9537
9538 /* Bail if the pattern is too large. */
9539 if (used_pattern_len != pattern_len)
9b20d036 9540 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
9541
9542 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
9543 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9544 || packet_ok (rs->buf, packet) != PACKET_OK)
9545 {
9546 /* The request may not have worked because the command is not
9547 supported. If so, fall back to the simple way. */
9548 if (packet->support == PACKET_DISABLE)
9549 {
9550 return simple_search_memory (ops, start_addr, search_space_len,
9551 pattern, pattern_len, found_addrp);
9552 }
9553 return -1;
9554 }
9555
9556 if (rs->buf[0] == '0')
9557 found = 0;
9558 else if (rs->buf[0] == '1')
9559 {
9560 found = 1;
9561 if (rs->buf[1] != ',')
10e0fa18 9562 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9563 unpack_varlen_hex (rs->buf + 2, &found_addr);
9564 *found_addrp = found_addr;
9565 }
9566 else
10e0fa18 9567 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9568
9569 return found;
9570}
9571
96baa820 9572static void
a30bf1f1 9573remote_rcmd (struct target_ops *self, const char *command,
d9fcf2fb 9574 struct ui_file *outbuf)
96baa820 9575{
d01949b6 9576 struct remote_state *rs = get_remote_state ();
2e9f7625 9577 char *p = rs->buf;
96baa820 9578
5d93a237 9579 if (!rs->remote_desc)
8a3fe4f8 9580 error (_("remote rcmd is only available after target open"));
96baa820 9581
23860348 9582 /* Send a NULL command across as an empty command. */
7be570e7
JM
9583 if (command == NULL)
9584 command = "";
9585
23860348 9586 /* The query prefix. */
2e9f7625
DJ
9587 strcpy (rs->buf, "qRcmd,");
9588 p = strchr (rs->buf, '\0');
96baa820 9589
3e43a32a
MS
9590 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9591 > get_remote_packet_size ())
8a3fe4f8 9592 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 9593
23860348 9594 /* Encode the actual command. */
a30bf1f1 9595 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 9596
6d820c5c 9597 if (putpkt (rs->buf) < 0)
8a3fe4f8 9598 error (_("Communication problem with target."));
96baa820
JM
9599
9600 /* get/display the response */
9601 while (1)
9602 {
2e9f7625
DJ
9603 char *buf;
9604
00bf0b85 9605 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 9606 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 9607 rs->buf[0] = '\0';
5b37825d
PW
9608 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9609 {
9610 /* Timeout. Continue to (try to) read responses.
9611 This is better than stopping with an error, assuming the stub
9612 is still executing the (long) monitor command.
9613 If needed, the user can interrupt gdb using C-c, obtaining
9614 an effect similar to stop on timeout. */
9615 continue;
9616 }
2e9f7625 9617 buf = rs->buf;
96baa820 9618 if (buf[0] == '\0')
8a3fe4f8 9619 error (_("Target does not support this command."));
96baa820
JM
9620 if (buf[0] == 'O' && buf[1] != 'K')
9621 {
23860348 9622 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
9623 continue;
9624 }
9625 if (strcmp (buf, "OK") == 0)
9626 break;
7be570e7
JM
9627 if (strlen (buf) == 3 && buf[0] == 'E'
9628 && isdigit (buf[1]) && isdigit (buf[2]))
9629 {
8a3fe4f8 9630 error (_("Protocol error with Rcmd"));
7be570e7 9631 }
96baa820
JM
9632 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9633 {
9634 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 9635
96baa820
JM
9636 fputc_unfiltered (c, outbuf);
9637 }
9638 break;
9639 }
9640}
9641
fd79ecee
DJ
9642static VEC(mem_region_s) *
9643remote_memory_map (struct target_ops *ops)
9644{
9645 VEC(mem_region_s) *result = NULL;
9646 char *text = target_read_stralloc (&current_target,
9647 TARGET_OBJECT_MEMORY_MAP, NULL);
9648
9649 if (text)
9650 {
9651 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 9652
fd79ecee
DJ
9653 result = parse_memory_map (text);
9654 do_cleanups (back_to);
9655 }
9656
9657 return result;
9658}
9659
c906108c 9660static void
fba45db2 9661packet_command (char *args, int from_tty)
c906108c 9662{
d01949b6 9663 struct remote_state *rs = get_remote_state ();
c906108c 9664
5d93a237 9665 if (!rs->remote_desc)
8a3fe4f8 9666 error (_("command can only be used with remote target"));
c906108c 9667
c5aa993b 9668 if (!args)
8a3fe4f8 9669 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
9670
9671 puts_filtered ("sending: ");
9672 print_packet (args);
9673 puts_filtered ("\n");
9674 putpkt (args);
9675
6d820c5c 9676 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 9677 puts_filtered ("received: ");
6d820c5c 9678 print_packet (rs->buf);
c906108c
SS
9679 puts_filtered ("\n");
9680}
9681
9682#if 0
23860348 9683/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 9684
a14ed312 9685static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 9686
a14ed312 9687static void threadset_test_cmd (char *cmd, int tty);
c906108c 9688
a14ed312 9689static void threadalive_test (char *cmd, int tty);
c906108c 9690
a14ed312 9691static void threadlist_test_cmd (char *cmd, int tty);
c906108c 9692
23860348 9693int get_and_display_threadinfo (threadref *ref);
c906108c 9694
a14ed312 9695static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 9696
23860348 9697static int thread_display_step (threadref *ref, void *context);
c906108c 9698
a14ed312 9699static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 9700
a14ed312 9701static void init_remote_threadtests (void);
c906108c 9702
23860348 9703#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
9704
9705static void
fba45db2 9706threadset_test_cmd (char *cmd, int tty)
c906108c
SS
9707{
9708 int sample_thread = SAMPLE_THREAD;
9709
a3f17187 9710 printf_filtered (_("Remote threadset test\n"));
79d7f229 9711 set_general_thread (sample_thread);
c906108c
SS
9712}
9713
9714
9715static void
fba45db2 9716threadalive_test (char *cmd, int tty)
c906108c
SS
9717{
9718 int sample_thread = SAMPLE_THREAD;
79d7f229 9719 int pid = ptid_get_pid (inferior_ptid);
ba348170 9720 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 9721
79d7f229 9722 if (remote_thread_alive (ptid))
c906108c
SS
9723 printf_filtered ("PASS: Thread alive test\n");
9724 else
9725 printf_filtered ("FAIL: Thread alive test\n");
9726}
9727
23860348 9728void output_threadid (char *title, threadref *ref);
c906108c
SS
9729
9730void
fba45db2 9731output_threadid (char *title, threadref *ref)
c906108c
SS
9732{
9733 char hexid[20];
9734
23860348 9735 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
9736 hexid[16] = 0;
9737 printf_filtered ("%s %s\n", title, (&hexid[0]));
9738}
9739
9740static void
fba45db2 9741threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
9742{
9743 int startflag = 1;
9744 threadref nextthread;
9745 int done, result_count;
9746 threadref threadlist[3];
9747
9748 printf_filtered ("Remote Threadlist test\n");
9749 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
9750 &result_count, &threadlist[0]))
9751 printf_filtered ("FAIL: threadlist test\n");
9752 else
9753 {
9754 threadref *scan = threadlist;
9755 threadref *limit = scan + result_count;
9756
9757 while (scan < limit)
9758 output_threadid (" thread ", scan++);
9759 }
9760}
9761
9762void
fba45db2 9763display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
9764{
9765 output_threadid ("Threadid: ", &info->threadid);
9766 printf_filtered ("Name: %s\n ", info->shortname);
9767 printf_filtered ("State: %s\n", info->display);
9768 printf_filtered ("other: %s\n\n", info->more_display);
9769}
9770
9771int
fba45db2 9772get_and_display_threadinfo (threadref *ref)
c906108c
SS
9773{
9774 int result;
9775 int set;
9776 struct gdb_ext_thread_info threadinfo;
9777
9778 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9779 | TAG_MOREDISPLAY | TAG_DISPLAY;
9780 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9781 display_thread_info (&threadinfo);
9782 return result;
9783}
9784
9785static void
fba45db2 9786threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
9787{
9788 int athread = SAMPLE_THREAD;
9789 threadref thread;
9790 int set;
9791
9792 int_to_threadref (&thread, athread);
9793 printf_filtered ("Remote Threadinfo test\n");
9794 if (!get_and_display_threadinfo (&thread))
9795 printf_filtered ("FAIL cannot get thread info\n");
9796}
9797
9798static int
fba45db2 9799thread_display_step (threadref *ref, void *context)
c906108c
SS
9800{
9801 /* output_threadid(" threadstep ",ref); *//* simple test */
9802 return get_and_display_threadinfo (ref);
9803}
9804
9805static void
fba45db2 9806threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
9807{
9808 printf_filtered ("Remote Threadlist update test\n");
9809 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9810}
9811
9812static void
9813init_remote_threadtests (void)
9814{
3e43a32a
MS
9815 add_com ("tlist", class_obscure, threadlist_test_cmd,
9816 _("Fetch and print the remote list of "
9817 "thread identifiers, one pkt only"));
c906108c 9818 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 9819 _("Fetch and display info about one thread"));
c906108c 9820 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 9821 _("Test setting to a different thread"));
c906108c 9822 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 9823 _("Iterate through updating all remote thread info"));
c906108c 9824 add_com ("talive", class_obscure, threadalive_test,
1bedd215 9825 _(" Remote thread alive test "));
c906108c
SS
9826}
9827
9828#endif /* 0 */
9829
f3fb8c85
MS
9830/* Convert a thread ID to a string. Returns the string in a static
9831 buffer. */
9832
9833static char *
117de6a9 9834remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 9835{
79d7f229 9836 static char buf[64];
82f73884 9837 struct remote_state *rs = get_remote_state ();
f3fb8c85 9838
7cee1e54
PA
9839 if (ptid_equal (ptid, null_ptid))
9840 return normal_pid_to_str (ptid);
9841 else if (ptid_is_pid (ptid))
ecd0ada5
PA
9842 {
9843 /* Printing an inferior target id. */
9844
9845 /* When multi-process extensions are off, there's no way in the
9846 remote protocol to know the remote process id, if there's any
9847 at all. There's one exception --- when we're connected with
9848 target extended-remote, and we manually attached to a process
9849 with "attach PID". We don't record anywhere a flag that
9850 allows us to distinguish that case from the case of
9851 connecting with extended-remote and the stub already being
9852 attached to a process, and reporting yes to qAttached, hence
9853 no smart special casing here. */
9854 if (!remote_multi_process_p (rs))
9855 {
9856 xsnprintf (buf, sizeof buf, "Remote target");
9857 return buf;
9858 }
9859
9860 return normal_pid_to_str (ptid);
82f73884 9861 }
ecd0ada5 9862 else
79d7f229 9863 {
ecd0ada5
PA
9864 if (ptid_equal (magic_null_ptid, ptid))
9865 xsnprintf (buf, sizeof buf, "Thread <main>");
901f9912 9866 else if (rs->extended && remote_multi_process_p (rs))
de0d863e
DB
9867 if (ptid_get_lwp (ptid) == 0)
9868 return normal_pid_to_str (ptid);
9869 else
9870 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9871 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
9872 else
9873 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 9874 ptid_get_lwp (ptid));
79d7f229
PA
9875 return buf;
9876 }
f3fb8c85
MS
9877}
9878
38691318
KB
9879/* Get the address of the thread local variable in OBJFILE which is
9880 stored at OFFSET within the thread local storage for thread PTID. */
9881
9882static CORE_ADDR
117de6a9
PA
9883remote_get_thread_local_address (struct target_ops *ops,
9884 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 9885{
4082afcc 9886 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
9887 {
9888 struct remote_state *rs = get_remote_state ();
6d820c5c 9889 char *p = rs->buf;
82f73884 9890 char *endp = rs->buf + get_remote_packet_size ();
571dd617 9891 enum packet_result result;
38691318
KB
9892
9893 strcpy (p, "qGetTLSAddr:");
9894 p += strlen (p);
82f73884 9895 p = write_ptid (p, endp, ptid);
38691318
KB
9896 *p++ = ',';
9897 p += hexnumstr (p, offset);
9898 *p++ = ',';
9899 p += hexnumstr (p, lm);
9900 *p++ = '\0';
9901
6d820c5c
DJ
9902 putpkt (rs->buf);
9903 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
9904 result = packet_ok (rs->buf,
9905 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 9906 if (result == PACKET_OK)
38691318
KB
9907 {
9908 ULONGEST result;
9909
6d820c5c 9910 unpack_varlen_hex (rs->buf, &result);
38691318
KB
9911 return result;
9912 }
571dd617 9913 else if (result == PACKET_UNKNOWN)
109c3e39
AC
9914 throw_error (TLS_GENERIC_ERROR,
9915 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 9916 else
109c3e39
AC
9917 throw_error (TLS_GENERIC_ERROR,
9918 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
9919 }
9920 else
109c3e39
AC
9921 throw_error (TLS_GENERIC_ERROR,
9922 _("TLS not supported or disabled on this target"));
38691318
KB
9923 /* Not reached. */
9924 return 0;
9925}
9926
711e434b
PM
9927/* Provide thread local base, i.e. Thread Information Block address.
9928 Returns 1 if ptid is found and thread_local_base is non zero. */
9929
70221824 9930static int
bd7ae0f5 9931remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
711e434b 9932{
4082afcc 9933 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
9934 {
9935 struct remote_state *rs = get_remote_state ();
9936 char *p = rs->buf;
9937 char *endp = rs->buf + get_remote_packet_size ();
9938 enum packet_result result;
9939
9940 strcpy (p, "qGetTIBAddr:");
9941 p += strlen (p);
9942 p = write_ptid (p, endp, ptid);
9943 *p++ = '\0';
9944
9945 putpkt (rs->buf);
9946 getpkt (&rs->buf, &rs->buf_size, 0);
9947 result = packet_ok (rs->buf,
9948 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9949 if (result == PACKET_OK)
9950 {
9951 ULONGEST result;
9952
9953 unpack_varlen_hex (rs->buf, &result);
9954 if (addr)
9955 *addr = (CORE_ADDR) result;
9956 return 1;
9957 }
9958 else if (result == PACKET_UNKNOWN)
9959 error (_("Remote target doesn't support qGetTIBAddr packet"));
9960 else
9961 error (_("Remote target failed to process qGetTIBAddr request"));
9962 }
9963 else
9964 error (_("qGetTIBAddr not supported or disabled on this target"));
9965 /* Not reached. */
9966 return 0;
9967}
9968
29709017
DJ
9969/* Support for inferring a target description based on the current
9970 architecture and the size of a 'g' packet. While the 'g' packet
9971 can have any size (since optional registers can be left off the
9972 end), some sizes are easily recognizable given knowledge of the
9973 approximate architecture. */
9974
9975struct remote_g_packet_guess
9976{
9977 int bytes;
9978 const struct target_desc *tdesc;
9979};
9980typedef struct remote_g_packet_guess remote_g_packet_guess_s;
9981DEF_VEC_O(remote_g_packet_guess_s);
9982
9983struct remote_g_packet_data
9984{
9985 VEC(remote_g_packet_guess_s) *guesses;
9986};
9987
9988static struct gdbarch_data *remote_g_packet_data_handle;
9989
9990static void *
9991remote_g_packet_data_init (struct obstack *obstack)
9992{
9993 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
9994}
9995
9996void
9997register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
9998 const struct target_desc *tdesc)
9999{
10000 struct remote_g_packet_data *data
10001 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
10002 struct remote_g_packet_guess new_guess, *guess;
10003 int ix;
10004
10005 gdb_assert (tdesc != NULL);
10006
10007 for (ix = 0;
10008 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10009 ix++)
10010 if (guess->bytes == bytes)
10011 internal_error (__FILE__, __LINE__,
9b20d036 10012 _("Duplicate g packet description added for size %d"),
29709017
DJ
10013 bytes);
10014
10015 new_guess.bytes = bytes;
10016 new_guess.tdesc = tdesc;
10017 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
10018}
10019
d962ef82
DJ
10020/* Return 1 if remote_read_description would do anything on this target
10021 and architecture, 0 otherwise. */
10022
10023static int
10024remote_read_description_p (struct target_ops *target)
10025{
10026 struct remote_g_packet_data *data
f5656ead 10027 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
d962ef82
DJ
10028
10029 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10030 return 1;
10031
10032 return 0;
10033}
10034
29709017
DJ
10035static const struct target_desc *
10036remote_read_description (struct target_ops *target)
10037{
10038 struct remote_g_packet_data *data
f5656ead 10039 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
29709017 10040
d962ef82
DJ
10041 /* Do not try this during initial connection, when we do not know
10042 whether there is a running but stopped thread. */
10043 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
2117c711 10044 return target->beneath->to_read_description (target->beneath);
d962ef82 10045
29709017
DJ
10046 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10047 {
10048 struct remote_g_packet_guess *guess;
10049 int ix;
10050 int bytes = send_g_packet ();
10051
10052 for (ix = 0;
10053 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10054 ix++)
10055 if (guess->bytes == bytes)
10056 return guess->tdesc;
10057
10058 /* We discard the g packet. A minor optimization would be to
10059 hold on to it, and fill the register cache once we have selected
10060 an architecture, but it's too tricky to do safely. */
10061 }
10062
2117c711 10063 return target->beneath->to_read_description (target->beneath);
29709017
DJ
10064}
10065
a6b151f1
DJ
10066/* Remote file transfer support. This is host-initiated I/O, not
10067 target-initiated; for target-initiated, see remote-fileio.c. */
10068
10069/* If *LEFT is at least the length of STRING, copy STRING to
10070 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10071 decrease *LEFT. Otherwise raise an error. */
10072
10073static void
10074remote_buffer_add_string (char **buffer, int *left, char *string)
10075{
10076 int len = strlen (string);
10077
10078 if (len > *left)
10079 error (_("Packet too long for target."));
10080
10081 memcpy (*buffer, string, len);
10082 *buffer += len;
10083 *left -= len;
10084
10085 /* NUL-terminate the buffer as a convenience, if there is
10086 room. */
10087 if (*left)
10088 **buffer = '\0';
10089}
10090
10091/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
10092 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10093 decrease *LEFT. Otherwise raise an error. */
10094
10095static void
10096remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
10097 int len)
10098{
10099 if (2 * len > *left)
10100 error (_("Packet too long for target."));
10101
10102 bin2hex (bytes, *buffer, len);
10103 *buffer += 2 * len;
10104 *left -= 2 * len;
10105
10106 /* NUL-terminate the buffer as a convenience, if there is
10107 room. */
10108 if (*left)
10109 **buffer = '\0';
10110}
10111
10112/* If *LEFT is large enough, convert VALUE to hex and add it to
10113 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10114 decrease *LEFT. Otherwise raise an error. */
10115
10116static void
10117remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
10118{
10119 int len = hexnumlen (value);
10120
10121 if (len > *left)
10122 error (_("Packet too long for target."));
10123
10124 hexnumstr (*buffer, value);
10125 *buffer += len;
10126 *left -= len;
10127
10128 /* NUL-terminate the buffer as a convenience, if there is
10129 room. */
10130 if (*left)
10131 **buffer = '\0';
10132}
10133
10134/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
10135 value, *REMOTE_ERRNO to the remote error number or zero if none
10136 was included, and *ATTACHMENT to point to the start of the annex
10137 if any. The length of the packet isn't needed here; there may
10138 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
10139
10140 Return 0 if the packet could be parsed, -1 if it could not. If
10141 -1 is returned, the other variables may not be initialized. */
10142
10143static int
10144remote_hostio_parse_result (char *buffer, int *retcode,
10145 int *remote_errno, char **attachment)
10146{
10147 char *p, *p2;
10148
10149 *remote_errno = 0;
10150 *attachment = NULL;
10151
10152 if (buffer[0] != 'F')
10153 return -1;
10154
10155 errno = 0;
10156 *retcode = strtol (&buffer[1], &p, 16);
10157 if (errno != 0 || p == &buffer[1])
10158 return -1;
10159
10160 /* Check for ",errno". */
10161 if (*p == ',')
10162 {
10163 errno = 0;
10164 *remote_errno = strtol (p + 1, &p2, 16);
10165 if (errno != 0 || p + 1 == p2)
10166 return -1;
10167 p = p2;
10168 }
10169
10170 /* Check for ";attachment". If there is no attachment, the
10171 packet should end here. */
10172 if (*p == ';')
10173 {
10174 *attachment = p + 1;
10175 return 0;
10176 }
10177 else if (*p == '\0')
10178 return 0;
10179 else
10180 return -1;
10181}
10182
10183/* Send a prepared I/O packet to the target and read its response.
10184 The prepared packet is in the global RS->BUF before this function
10185 is called, and the answer is there when we return.
10186
10187 COMMAND_BYTES is the length of the request to send, which may include
10188 binary data. WHICH_PACKET is the packet configuration to check
10189 before attempting a packet. If an error occurs, *REMOTE_ERRNO
10190 is set to the error number and -1 is returned. Otherwise the value
10191 returned by the function is returned.
10192
10193 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
10194 attachment is expected; an error will be reported if there's a
10195 mismatch. If one is found, *ATTACHMENT will be set to point into
10196 the packet buffer and *ATTACHMENT_LEN will be set to the
10197 attachment's length. */
10198
10199static int
10200remote_hostio_send_command (int command_bytes, int which_packet,
10201 int *remote_errno, char **attachment,
10202 int *attachment_len)
10203{
10204 struct remote_state *rs = get_remote_state ();
10205 int ret, bytes_read;
10206 char *attachment_tmp;
10207
5d93a237 10208 if (!rs->remote_desc
4082afcc 10209 || packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
10210 {
10211 *remote_errno = FILEIO_ENOSYS;
10212 return -1;
10213 }
10214
10215 putpkt_binary (rs->buf, command_bytes);
10216 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10217
10218 /* If it timed out, something is wrong. Don't try to parse the
10219 buffer. */
10220 if (bytes_read < 0)
10221 {
10222 *remote_errno = FILEIO_EINVAL;
10223 return -1;
10224 }
10225
10226 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
10227 {
10228 case PACKET_ERROR:
10229 *remote_errno = FILEIO_EINVAL;
10230 return -1;
10231 case PACKET_UNKNOWN:
10232 *remote_errno = FILEIO_ENOSYS;
10233 return -1;
10234 case PACKET_OK:
10235 break;
10236 }
10237
10238 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
10239 &attachment_tmp))
10240 {
10241 *remote_errno = FILEIO_EINVAL;
10242 return -1;
10243 }
10244
10245 /* Make sure we saw an attachment if and only if we expected one. */
10246 if ((attachment_tmp == NULL && attachment != NULL)
10247 || (attachment_tmp != NULL && attachment == NULL))
10248 {
10249 *remote_errno = FILEIO_EINVAL;
10250 return -1;
10251 }
10252
10253 /* If an attachment was found, it must point into the packet buffer;
10254 work out how many bytes there were. */
10255 if (attachment_tmp != NULL)
10256 {
10257 *attachment = attachment_tmp;
10258 *attachment_len = bytes_read - (*attachment - rs->buf);
10259 }
10260
10261 return ret;
10262}
10263
15a201c8
GB
10264/* Set the filesystem remote_hostio functions that take FILENAME
10265 arguments will use. Return 0 on success, or -1 if an error
10266 occurs (and set *REMOTE_ERRNO). */
10267
10268static int
10269remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
10270{
10271 struct remote_state *rs = get_remote_state ();
10272 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
10273 char *p = rs->buf;
10274 int left = get_remote_packet_size () - 1;
10275 char arg[9];
10276 int ret;
10277
10278 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10279 return 0;
10280
10281 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
10282 return 0;
10283
10284 remote_buffer_add_string (&p, &left, "vFile:setfs:");
10285
10286 xsnprintf (arg, sizeof (arg), "%x", required_pid);
10287 remote_buffer_add_string (&p, &left, arg);
10288
10289 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
10290 remote_errno, NULL, NULL);
10291
10292 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10293 return 0;
10294
10295 if (ret == 0)
10296 rs->fs_pid = required_pid;
10297
10298 return ret;
10299}
10300
12e2a5fd 10301/* Implementation of to_fileio_open. */
a6b151f1
DJ
10302
10303static int
cd897586 10304remote_hostio_open (struct target_ops *self,
07c138c8
GB
10305 struct inferior *inf, const char *filename,
10306 int flags, int mode, int *remote_errno)
a6b151f1
DJ
10307{
10308 struct remote_state *rs = get_remote_state ();
10309 char *p = rs->buf;
10310 int left = get_remote_packet_size () - 1;
10311
15a201c8
GB
10312 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10313 return -1;
10314
a6b151f1
DJ
10315 remote_buffer_add_string (&p, &left, "vFile:open:");
10316
10317 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10318 strlen (filename));
10319 remote_buffer_add_string (&p, &left, ",");
10320
10321 remote_buffer_add_int (&p, &left, flags);
10322 remote_buffer_add_string (&p, &left, ",");
10323
10324 remote_buffer_add_int (&p, &left, mode);
10325
10326 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
10327 remote_errno, NULL, NULL);
10328}
10329
12e2a5fd 10330/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
10331
10332static int
0d866f62
TT
10333remote_hostio_pwrite (struct target_ops *self,
10334 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
10335 ULONGEST offset, int *remote_errno)
10336{
10337 struct remote_state *rs = get_remote_state ();
10338 char *p = rs->buf;
10339 int left = get_remote_packet_size ();
10340 int out_len;
10341
10342 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
10343
10344 remote_buffer_add_int (&p, &left, fd);
10345 remote_buffer_add_string (&p, &left, ",");
10346
10347 remote_buffer_add_int (&p, &left, offset);
10348 remote_buffer_add_string (&p, &left, ",");
10349
124e13d9 10350 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
a6b151f1
DJ
10351 get_remote_packet_size () - (p - rs->buf));
10352
10353 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
10354 remote_errno, NULL, NULL);
10355}
10356
12e2a5fd 10357/* Implementation of to_fileio_pread. */
a6b151f1
DJ
10358
10359static int
a3be983c
TT
10360remote_hostio_pread (struct target_ops *self,
10361 int fd, gdb_byte *read_buf, int len,
a6b151f1
DJ
10362 ULONGEST offset, int *remote_errno)
10363{
10364 struct remote_state *rs = get_remote_state ();
10365 char *p = rs->buf;
10366 char *attachment;
10367 int left = get_remote_packet_size ();
10368 int ret, attachment_len;
10369 int read_len;
10370
10371 remote_buffer_add_string (&p, &left, "vFile:pread:");
10372
10373 remote_buffer_add_int (&p, &left, fd);
10374 remote_buffer_add_string (&p, &left, ",");
10375
10376 remote_buffer_add_int (&p, &left, len);
10377 remote_buffer_add_string (&p, &left, ",");
10378
10379 remote_buffer_add_int (&p, &left, offset);
10380
10381 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
10382 remote_errno, &attachment,
10383 &attachment_len);
10384
10385 if (ret < 0)
10386 return ret;
10387
bc20a4af 10388 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
10389 read_buf, len);
10390 if (read_len != ret)
10391 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
10392
10393 return ret;
10394}
10395
12e2a5fd 10396/* Implementation of to_fileio_close. */
a6b151f1
DJ
10397
10398static int
df39ea25 10399remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
10400{
10401 struct remote_state *rs = get_remote_state ();
10402 char *p = rs->buf;
10403 int left = get_remote_packet_size () - 1;
10404
10405 remote_buffer_add_string (&p, &left, "vFile:close:");
10406
10407 remote_buffer_add_int (&p, &left, fd);
10408
10409 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
10410 remote_errno, NULL, NULL);
10411}
10412
12e2a5fd 10413/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
10414
10415static int
dbbca37d 10416remote_hostio_unlink (struct target_ops *self,
07c138c8
GB
10417 struct inferior *inf, const char *filename,
10418 int *remote_errno)
a6b151f1
DJ
10419{
10420 struct remote_state *rs = get_remote_state ();
10421 char *p = rs->buf;
10422 int left = get_remote_packet_size () - 1;
10423
15a201c8
GB
10424 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10425 return -1;
10426
a6b151f1
DJ
10427 remote_buffer_add_string (&p, &left, "vFile:unlink:");
10428
10429 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10430 strlen (filename));
10431
10432 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
10433 remote_errno, NULL, NULL);
10434}
10435
12e2a5fd 10436/* Implementation of to_fileio_readlink. */
b9e7b9c3
UW
10437
10438static char *
fab5aa7c 10439remote_hostio_readlink (struct target_ops *self,
07c138c8
GB
10440 struct inferior *inf, const char *filename,
10441 int *remote_errno)
b9e7b9c3
UW
10442{
10443 struct remote_state *rs = get_remote_state ();
10444 char *p = rs->buf;
10445 char *attachment;
10446 int left = get_remote_packet_size ();
10447 int len, attachment_len;
10448 int read_len;
10449 char *ret;
10450
15a201c8
GB
10451 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10452 return NULL;
10453
b9e7b9c3
UW
10454 remote_buffer_add_string (&p, &left, "vFile:readlink:");
10455
10456 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10457 strlen (filename));
10458
10459 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
10460 remote_errno, &attachment,
10461 &attachment_len);
10462
10463 if (len < 0)
10464 return NULL;
10465
10466 ret = xmalloc (len + 1);
10467
bc20a4af
PA
10468 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10469 (gdb_byte *) ret, len);
b9e7b9c3
UW
10470 if (read_len != len)
10471 error (_("Readlink returned %d, but %d bytes."), len, read_len);
10472
10473 ret[len] = '\0';
10474 return ret;
10475}
10476
12e2a5fd 10477/* Implementation of to_fileio_fstat. */
0a93529c
GB
10478
10479static int
10480remote_hostio_fstat (struct target_ops *self,
10481 int fd, struct stat *st,
10482 int *remote_errno)
10483{
10484 struct remote_state *rs = get_remote_state ();
10485 char *p = rs->buf;
10486 int left = get_remote_packet_size ();
10487 int attachment_len, ret;
10488 char *attachment;
10489 struct fio_stat fst;
10490 int read_len;
10491
464b0089
GB
10492 remote_buffer_add_string (&p, &left, "vFile:fstat:");
10493
10494 remote_buffer_add_int (&p, &left, fd);
10495
10496 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
10497 remote_errno, &attachment,
10498 &attachment_len);
10499 if (ret < 0)
0a93529c 10500 {
464b0089
GB
10501 if (*remote_errno != FILEIO_ENOSYS)
10502 return ret;
10503
0a93529c
GB
10504 /* Strictly we should return -1, ENOSYS here, but when
10505 "set sysroot remote:" was implemented in August 2008
10506 BFD's need for a stat function was sidestepped with
10507 this hack. This was not remedied until March 2015
10508 so we retain the previous behavior to avoid breaking
10509 compatibility.
10510
10511 Note that the memset is a March 2015 addition; older
10512 GDBs set st_size *and nothing else* so the structure
10513 would have garbage in all other fields. This might
10514 break something but retaining the previous behavior
10515 here would be just too wrong. */
10516
10517 memset (st, 0, sizeof (struct stat));
10518 st->st_size = INT_MAX;
10519 return 0;
10520 }
10521
0a93529c
GB
10522 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10523 (gdb_byte *) &fst, sizeof (fst));
10524
10525 if (read_len != ret)
10526 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
10527
10528 if (read_len != sizeof (fst))
10529 error (_("vFile:fstat returned %d bytes, but expecting %d."),
10530 read_len, (int) sizeof (fst));
10531
10532 remote_fileio_to_host_stat (&fst, st);
10533
10534 return 0;
10535}
10536
12e2a5fd 10537/* Implementation of to_filesystem_is_local. */
e3dd7556
GB
10538
10539static int
10540remote_filesystem_is_local (struct target_ops *self)
10541{
10542 /* Valgrind GDB presents itself as a remote target but works
10543 on the local filesystem: it does not implement remote get
10544 and users are not expected to set a sysroot. To handle
10545 this case we treat the remote filesystem as local if the
10546 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
10547 does not support vFile:open. */
a3be80c3 10548 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
e3dd7556
GB
10549 {
10550 enum packet_support ps = packet_support (PACKET_vFile_open);
10551
10552 if (ps == PACKET_SUPPORT_UNKNOWN)
10553 {
10554 int fd, remote_errno;
10555
10556 /* Try opening a file to probe support. The supplied
10557 filename is irrelevant, we only care about whether
10558 the stub recognizes the packet or not. */
07c138c8 10559 fd = remote_hostio_open (self, NULL, "just probing",
e3dd7556
GB
10560 FILEIO_O_RDONLY, 0700,
10561 &remote_errno);
10562
10563 if (fd >= 0)
10564 remote_hostio_close (self, fd, &remote_errno);
10565
10566 ps = packet_support (PACKET_vFile_open);
10567 }
10568
10569 if (ps == PACKET_DISABLE)
10570 {
10571 static int warning_issued = 0;
10572
10573 if (!warning_issued)
10574 {
10575 warning (_("remote target does not support file"
10576 " transfer, attempting to access files"
10577 " from local filesystem."));
10578 warning_issued = 1;
10579 }
10580
10581 return 1;
10582 }
10583 }
10584
10585 return 0;
10586}
10587
a6b151f1
DJ
10588static int
10589remote_fileio_errno_to_host (int errnum)
10590{
10591 switch (errnum)
10592 {
10593 case FILEIO_EPERM:
10594 return EPERM;
10595 case FILEIO_ENOENT:
10596 return ENOENT;
10597 case FILEIO_EINTR:
10598 return EINTR;
10599 case FILEIO_EIO:
10600 return EIO;
10601 case FILEIO_EBADF:
10602 return EBADF;
10603 case FILEIO_EACCES:
10604 return EACCES;
10605 case FILEIO_EFAULT:
10606 return EFAULT;
10607 case FILEIO_EBUSY:
10608 return EBUSY;
10609 case FILEIO_EEXIST:
10610 return EEXIST;
10611 case FILEIO_ENODEV:
10612 return ENODEV;
10613 case FILEIO_ENOTDIR:
10614 return ENOTDIR;
10615 case FILEIO_EISDIR:
10616 return EISDIR;
10617 case FILEIO_EINVAL:
10618 return EINVAL;
10619 case FILEIO_ENFILE:
10620 return ENFILE;
10621 case FILEIO_EMFILE:
10622 return EMFILE;
10623 case FILEIO_EFBIG:
10624 return EFBIG;
10625 case FILEIO_ENOSPC:
10626 return ENOSPC;
10627 case FILEIO_ESPIPE:
10628 return ESPIPE;
10629 case FILEIO_EROFS:
10630 return EROFS;
10631 case FILEIO_ENOSYS:
10632 return ENOSYS;
10633 case FILEIO_ENAMETOOLONG:
10634 return ENAMETOOLONG;
10635 }
10636 return -1;
10637}
10638
10639static char *
10640remote_hostio_error (int errnum)
10641{
10642 int host_error = remote_fileio_errno_to_host (errnum);
10643
10644 if (host_error == -1)
10645 error (_("Unknown remote I/O error %d"), errnum);
10646 else
10647 error (_("Remote I/O error: %s"), safe_strerror (host_error));
10648}
10649
a6b151f1
DJ
10650static void
10651remote_hostio_close_cleanup (void *opaque)
10652{
10653 int fd = *(int *) opaque;
10654 int remote_errno;
10655
df39ea25 10656 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
a6b151f1
DJ
10657}
10658
10659void
10660remote_file_put (const char *local_file, const char *remote_file, int from_tty)
10661{
10662 struct cleanup *back_to, *close_cleanup;
10663 int retcode, fd, remote_errno, bytes, io_size;
10664 FILE *file;
10665 gdb_byte *buffer;
10666 int bytes_in_buffer;
10667 int saw_eof;
10668 ULONGEST offset;
5d93a237 10669 struct remote_state *rs = get_remote_state ();
a6b151f1 10670
5d93a237 10671 if (!rs->remote_desc)
a6b151f1
DJ
10672 error (_("command can only be used with remote target"));
10673
614c279d 10674 file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
10675 if (file == NULL)
10676 perror_with_name (local_file);
7c8a8b04 10677 back_to = make_cleanup_fclose (file);
a6b151f1 10678
07c138c8 10679 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 10680 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
a6b151f1
DJ
10681 | FILEIO_O_TRUNC),
10682 0700, &remote_errno);
10683 if (fd == -1)
10684 remote_hostio_error (remote_errno);
10685
10686 /* Send up to this many bytes at once. They won't all fit in the
10687 remote packet limit, so we'll transfer slightly fewer. */
10688 io_size = get_remote_packet_size ();
10689 buffer = xmalloc (io_size);
10690 make_cleanup (xfree, buffer);
10691
10692 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10693
10694 bytes_in_buffer = 0;
10695 saw_eof = 0;
10696 offset = 0;
10697 while (bytes_in_buffer || !saw_eof)
10698 {
10699 if (!saw_eof)
10700 {
3e43a32a
MS
10701 bytes = fread (buffer + bytes_in_buffer, 1,
10702 io_size - bytes_in_buffer,
a6b151f1
DJ
10703 file);
10704 if (bytes == 0)
10705 {
10706 if (ferror (file))
10707 error (_("Error reading %s."), local_file);
10708 else
10709 {
10710 /* EOF. Unless there is something still in the
10711 buffer from the last iteration, we are done. */
10712 saw_eof = 1;
10713 if (bytes_in_buffer == 0)
10714 break;
10715 }
10716 }
10717 }
10718 else
10719 bytes = 0;
10720
10721 bytes += bytes_in_buffer;
10722 bytes_in_buffer = 0;
10723
0d866f62
TT
10724 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
10725 fd, buffer, bytes,
3e43a32a 10726 offset, &remote_errno);
a6b151f1
DJ
10727
10728 if (retcode < 0)
10729 remote_hostio_error (remote_errno);
10730 else if (retcode == 0)
10731 error (_("Remote write of %d bytes returned 0!"), bytes);
10732 else if (retcode < bytes)
10733 {
10734 /* Short write. Save the rest of the read data for the next
10735 write. */
10736 bytes_in_buffer = bytes - retcode;
10737 memmove (buffer, buffer + retcode, bytes_in_buffer);
10738 }
10739
10740 offset += retcode;
10741 }
10742
10743 discard_cleanups (close_cleanup);
df39ea25 10744 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10745 remote_hostio_error (remote_errno);
10746
10747 if (from_tty)
10748 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
10749 do_cleanups (back_to);
10750}
10751
10752void
10753remote_file_get (const char *remote_file, const char *local_file, int from_tty)
10754{
10755 struct cleanup *back_to, *close_cleanup;
cea39f65 10756 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
10757 FILE *file;
10758 gdb_byte *buffer;
10759 ULONGEST offset;
5d93a237 10760 struct remote_state *rs = get_remote_state ();
a6b151f1 10761
5d93a237 10762 if (!rs->remote_desc)
a6b151f1
DJ
10763 error (_("command can only be used with remote target"));
10764
07c138c8 10765 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 10766 remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
a6b151f1
DJ
10767 if (fd == -1)
10768 remote_hostio_error (remote_errno);
10769
614c279d 10770 file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
10771 if (file == NULL)
10772 perror_with_name (local_file);
7c8a8b04 10773 back_to = make_cleanup_fclose (file);
a6b151f1
DJ
10774
10775 /* Send up to this many bytes at once. They won't all fit in the
10776 remote packet limit, so we'll transfer slightly fewer. */
10777 io_size = get_remote_packet_size ();
10778 buffer = xmalloc (io_size);
10779 make_cleanup (xfree, buffer);
10780
10781 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10782
10783 offset = 0;
10784 while (1)
10785 {
a3be983c
TT
10786 bytes = remote_hostio_pread (find_target_at (process_stratum),
10787 fd, buffer, io_size, offset, &remote_errno);
a6b151f1
DJ
10788 if (bytes == 0)
10789 /* Success, but no bytes, means end-of-file. */
10790 break;
10791 if (bytes == -1)
10792 remote_hostio_error (remote_errno);
10793
10794 offset += bytes;
10795
10796 bytes = fwrite (buffer, 1, bytes, file);
10797 if (bytes == 0)
10798 perror_with_name (local_file);
10799 }
10800
10801 discard_cleanups (close_cleanup);
df39ea25 10802 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10803 remote_hostio_error (remote_errno);
10804
10805 if (from_tty)
10806 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
10807 do_cleanups (back_to);
10808}
10809
10810void
10811remote_file_delete (const char *remote_file, int from_tty)
10812{
10813 int retcode, remote_errno;
5d93a237 10814 struct remote_state *rs = get_remote_state ();
a6b151f1 10815
5d93a237 10816 if (!rs->remote_desc)
a6b151f1
DJ
10817 error (_("command can only be used with remote target"));
10818
dbbca37d 10819 retcode = remote_hostio_unlink (find_target_at (process_stratum),
07c138c8 10820 NULL, remote_file, &remote_errno);
a6b151f1
DJ
10821 if (retcode == -1)
10822 remote_hostio_error (remote_errno);
10823
10824 if (from_tty)
10825 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
10826}
10827
10828static void
10829remote_put_command (char *args, int from_tty)
10830{
10831 struct cleanup *back_to;
10832 char **argv;
10833
d1a41061
PP
10834 if (args == NULL)
10835 error_no_arg (_("file to put"));
10836
10837 argv = gdb_buildargv (args);
a6b151f1
DJ
10838 back_to = make_cleanup_freeargv (argv);
10839 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10840 error (_("Invalid parameters to remote put"));
10841
10842 remote_file_put (argv[0], argv[1], from_tty);
10843
10844 do_cleanups (back_to);
10845}
10846
10847static void
10848remote_get_command (char *args, int from_tty)
10849{
10850 struct cleanup *back_to;
10851 char **argv;
10852
d1a41061
PP
10853 if (args == NULL)
10854 error_no_arg (_("file to get"));
10855
10856 argv = gdb_buildargv (args);
a6b151f1
DJ
10857 back_to = make_cleanup_freeargv (argv);
10858 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10859 error (_("Invalid parameters to remote get"));
10860
10861 remote_file_get (argv[0], argv[1], from_tty);
10862
10863 do_cleanups (back_to);
10864}
10865
10866static void
10867remote_delete_command (char *args, int from_tty)
10868{
10869 struct cleanup *back_to;
10870 char **argv;
10871
d1a41061
PP
10872 if (args == NULL)
10873 error_no_arg (_("file to delete"));
10874
10875 argv = gdb_buildargv (args);
a6b151f1
DJ
10876 back_to = make_cleanup_freeargv (argv);
10877 if (argv[0] == NULL || argv[1] != NULL)
10878 error (_("Invalid parameters to remote delete"));
10879
10880 remote_file_delete (argv[0], from_tty);
10881
10882 do_cleanups (back_to);
10883}
10884
10885static void
10886remote_command (char *args, int from_tty)
10887{
635c7e8a 10888 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
10889}
10890
b2175913 10891static int
19db3e69 10892remote_can_execute_reverse (struct target_ops *self)
b2175913 10893{
4082afcc
PA
10894 if (packet_support (PACKET_bs) == PACKET_ENABLE
10895 || packet_support (PACKET_bc) == PACKET_ENABLE)
40ab02ce
MS
10896 return 1;
10897 else
10898 return 0;
b2175913
MS
10899}
10900
74531fed 10901static int
2a9a2795 10902remote_supports_non_stop (struct target_ops *self)
74531fed
PA
10903{
10904 return 1;
10905}
10906
03583c20 10907static int
2bfc0540 10908remote_supports_disable_randomization (struct target_ops *self)
03583c20
UW
10909{
10910 /* Only supported in extended mode. */
10911 return 0;
10912}
10913
8a305172 10914static int
86ce2668 10915remote_supports_multi_process (struct target_ops *self)
8a305172
PA
10916{
10917 struct remote_state *rs = get_remote_state ();
a744cf53 10918
901f9912
UW
10919 /* Only extended-remote handles being attached to multiple
10920 processes, even though plain remote can use the multi-process
10921 thread id extensions, so that GDB knows the target process's
10922 PID. */
10923 return rs->extended && remote_multi_process_p (rs);
8a305172
PA
10924}
10925
70221824 10926static int
782b2b07
SS
10927remote_supports_cond_tracepoints (void)
10928{
4082afcc 10929 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
10930}
10931
3788aec7 10932static int
efcc2da7 10933remote_supports_cond_breakpoints (struct target_ops *self)
3788aec7 10934{
4082afcc 10935 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
10936}
10937
70221824 10938static int
7a697b8d
SS
10939remote_supports_fast_tracepoints (void)
10940{
4082afcc 10941 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
10942}
10943
0fb4aa4b
PA
10944static int
10945remote_supports_static_tracepoints (void)
10946{
4082afcc 10947 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
10948}
10949
1e4d1764
YQ
10950static int
10951remote_supports_install_in_trace (void)
10952{
4082afcc 10953 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
10954}
10955
d248b706 10956static int
7d178d6a 10957remote_supports_enable_disable_tracepoint (struct target_ops *self)
d248b706 10958{
4082afcc
PA
10959 return (packet_support (PACKET_EnableDisableTracepoints_feature)
10960 == PACKET_ENABLE);
d248b706
KY
10961}
10962
3065dfb6 10963static int
6de37a3a 10964remote_supports_string_tracing (struct target_ops *self)
3065dfb6 10965{
4082afcc 10966 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
10967}
10968
d3ce09f5 10969static int
78eff0ec 10970remote_can_run_breakpoint_commands (struct target_ops *self)
d3ce09f5 10971{
4082afcc 10972 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
10973}
10974
35b1e5cc 10975static void
ecae04e1 10976remote_trace_init (struct target_ops *self)
35b1e5cc
SS
10977{
10978 putpkt ("QTinit");
10979 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99 10980 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
10981 error (_("Target does not support this command."));
10982}
10983
10984static void free_actions_list (char **actions_list);
10985static void free_actions_list_cleanup_wrapper (void *);
10986static void
10987free_actions_list_cleanup_wrapper (void *al)
10988{
10989 free_actions_list (al);
10990}
10991
10992static void
10993free_actions_list (char **actions_list)
10994{
10995 int ndx;
10996
10997 if (actions_list == 0)
10998 return;
10999
11000 for (ndx = 0; actions_list[ndx]; ndx++)
11001 xfree (actions_list[ndx]);
11002
11003 xfree (actions_list);
11004}
11005
409873ef
SS
11006/* Recursive routine to walk through command list including loops, and
11007 download packets for each command. */
11008
11009static void
11010remote_download_command_source (int num, ULONGEST addr,
11011 struct command_line *cmds)
11012{
11013 struct remote_state *rs = get_remote_state ();
11014 struct command_line *cmd;
11015
11016 for (cmd = cmds; cmd; cmd = cmd->next)
11017 {
0df8b418 11018 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11019 strcpy (rs->buf, "QTDPsrc:");
11020 encode_source_string (num, addr, "cmd", cmd->line,
11021 rs->buf + strlen (rs->buf),
11022 rs->buf_size - strlen (rs->buf));
11023 putpkt (rs->buf);
11024 remote_get_noisy_reply (&target_buf, &target_buf_size);
11025 if (strcmp (target_buf, "OK"))
11026 warning (_("Target does not support source download."));
11027
11028 if (cmd->control_type == while_control
11029 || cmd->control_type == while_stepping_control)
11030 {
11031 remote_download_command_source (num, addr, *cmd->body_list);
11032
0df8b418 11033 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11034 strcpy (rs->buf, "QTDPsrc:");
11035 encode_source_string (num, addr, "cmd", "end",
11036 rs->buf + strlen (rs->buf),
11037 rs->buf_size - strlen (rs->buf));
11038 putpkt (rs->buf);
11039 remote_get_noisy_reply (&target_buf, &target_buf_size);
11040 if (strcmp (target_buf, "OK"))
11041 warning (_("Target does not support source download."));
11042 }
11043 }
11044}
11045
35b1e5cc 11046static void
548f7808 11047remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
35b1e5cc 11048{
bba74b36 11049#define BUF_SIZE 2048
e8ba3115 11050
35b1e5cc 11051 CORE_ADDR tpaddr;
409873ef 11052 char addrbuf[40];
bba74b36 11053 char buf[BUF_SIZE];
35b1e5cc
SS
11054 char **tdp_actions;
11055 char **stepping_actions;
11056 int ndx;
11057 struct cleanup *old_chain = NULL;
11058 struct agent_expr *aexpr;
11059 struct cleanup *aexpr_chain = NULL;
11060 char *pkt;
e8ba3115 11061 struct breakpoint *b = loc->owner;
d9b3f62e 11062 struct tracepoint *t = (struct tracepoint *) b;
35b1e5cc 11063
dc673c81 11064 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
11065 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
11066 tdp_actions);
11067 (void) make_cleanup (free_actions_list_cleanup_wrapper,
11068 stepping_actions);
11069
11070 tpaddr = loc->address;
11071 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
11072 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
11073 addrbuf, /* address */
11074 (b->enable_state == bp_enabled ? 'E' : 'D'),
11075 t->step_count, t->pass_count);
e8ba3115
YQ
11076 /* Fast tracepoints are mostly handled by the target, but we can
11077 tell the target how big of an instruction block should be moved
11078 around. */
11079 if (b->type == bp_fast_tracepoint)
11080 {
11081 /* Only test for support at download time; we may not know
11082 target capabilities at definition time. */
11083 if (remote_supports_fast_tracepoints ())
35b1e5cc 11084 {
e8ba3115 11085 int isize;
35b1e5cc 11086
f5656ead 11087 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
e8ba3115 11088 tpaddr, &isize, NULL))
bba74b36
YQ
11089 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
11090 isize);
35b1e5cc 11091 else
e8ba3115
YQ
11092 /* If it passed validation at definition but fails now,
11093 something is very wrong. */
11094 internal_error (__FILE__, __LINE__,
11095 _("Fast tracepoint not "
11096 "valid during download"));
35b1e5cc 11097 }
e8ba3115
YQ
11098 else
11099 /* Fast tracepoints are functionally identical to regular
11100 tracepoints, so don't take lack of support as a reason to
11101 give up on the trace run. */
11102 warning (_("Target does not support fast tracepoints, "
11103 "downloading %d as regular tracepoint"), b->number);
11104 }
11105 else if (b->type == bp_static_tracepoint)
11106 {
11107 /* Only test for support at download time; we may not know
11108 target capabilities at definition time. */
11109 if (remote_supports_static_tracepoints ())
0fb4aa4b 11110 {
e8ba3115 11111 struct static_tracepoint_marker marker;
0fb4aa4b 11112
e8ba3115
YQ
11113 if (target_static_tracepoint_marker_at (tpaddr, &marker))
11114 strcat (buf, ":S");
0fb4aa4b 11115 else
e8ba3115 11116 error (_("Static tracepoint not valid during download"));
0fb4aa4b 11117 }
e8ba3115
YQ
11118 else
11119 /* Fast tracepoints are functionally identical to regular
11120 tracepoints, so don't take lack of support as a reason
11121 to give up on the trace run. */
11122 error (_("Target does not support static tracepoints"));
11123 }
11124 /* If the tracepoint has a conditional, make it into an agent
11125 expression and append to the definition. */
11126 if (loc->cond)
11127 {
11128 /* Only test support at download time, we may not know target
11129 capabilities at definition time. */
11130 if (remote_supports_cond_tracepoints ())
35b1e5cc 11131 {
e8ba3115
YQ
11132 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
11133 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
bba74b36
YQ
11134 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
11135 aexpr->len);
e8ba3115
YQ
11136 pkt = buf + strlen (buf);
11137 for (ndx = 0; ndx < aexpr->len; ++ndx)
11138 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
11139 *pkt = '\0';
11140 do_cleanups (aexpr_chain);
35b1e5cc 11141 }
e8ba3115
YQ
11142 else
11143 warning (_("Target does not support conditional tracepoints, "
11144 "ignoring tp %d cond"), b->number);
11145 }
35b1e5cc 11146
d9b3f62e 11147 if (b->commands || *default_collect)
e8ba3115
YQ
11148 strcat (buf, "-");
11149 putpkt (buf);
11150 remote_get_noisy_reply (&target_buf, &target_buf_size);
11151 if (strcmp (target_buf, "OK"))
11152 error (_("Target does not support tracepoints."));
35b1e5cc 11153
e8ba3115
YQ
11154 /* do_single_steps (t); */
11155 if (tdp_actions)
11156 {
11157 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 11158 {
e8ba3115 11159 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11160 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
11161 b->number, addrbuf, /* address */
11162 tdp_actions[ndx],
11163 ((tdp_actions[ndx + 1] || stepping_actions)
11164 ? '-' : 0));
e8ba3115
YQ
11165 putpkt (buf);
11166 remote_get_noisy_reply (&target_buf,
11167 &target_buf_size);
11168 if (strcmp (target_buf, "OK"))
11169 error (_("Error on target while setting tracepoints."));
35b1e5cc 11170 }
e8ba3115
YQ
11171 }
11172 if (stepping_actions)
11173 {
11174 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 11175 {
e8ba3115 11176 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11177 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
11178 b->number, addrbuf, /* address */
11179 ((ndx == 0) ? "S" : ""),
11180 stepping_actions[ndx],
11181 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115
YQ
11182 putpkt (buf);
11183 remote_get_noisy_reply (&target_buf,
11184 &target_buf_size);
11185 if (strcmp (target_buf, "OK"))
11186 error (_("Error on target while setting tracepoints."));
35b1e5cc 11187 }
e8ba3115 11188 }
409873ef 11189
4082afcc 11190 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115
YQ
11191 {
11192 if (b->addr_string)
409873ef 11193 {
e8ba3115
YQ
11194 strcpy (buf, "QTDPsrc:");
11195 encode_source_string (b->number, loc->address,
11196 "at", b->addr_string, buf + strlen (buf),
11197 2048 - strlen (buf));
409873ef 11198
e8ba3115
YQ
11199 putpkt (buf);
11200 remote_get_noisy_reply (&target_buf, &target_buf_size);
11201 if (strcmp (target_buf, "OK"))
11202 warning (_("Target does not support source download."));
409873ef 11203 }
e8ba3115
YQ
11204 if (b->cond_string)
11205 {
11206 strcpy (buf, "QTDPsrc:");
11207 encode_source_string (b->number, loc->address,
11208 "cond", b->cond_string, buf + strlen (buf),
11209 2048 - strlen (buf));
11210 putpkt (buf);
11211 remote_get_noisy_reply (&target_buf, &target_buf_size);
11212 if (strcmp (target_buf, "OK"))
11213 warning (_("Target does not support source download."));
11214 }
11215 remote_download_command_source (b->number, loc->address,
11216 breakpoint_commands (b));
35b1e5cc 11217 }
e8ba3115
YQ
11218
11219 do_cleanups (old_chain);
35b1e5cc
SS
11220}
11221
1e4d1764 11222static int
a52a8357 11223remote_can_download_tracepoint (struct target_ops *self)
1e4d1764 11224{
1e51243a
PA
11225 struct remote_state *rs = get_remote_state ();
11226 struct trace_status *ts;
11227 int status;
11228
11229 /* Don't try to install tracepoints until we've relocated our
11230 symbols, and fetched and merged the target's tracepoint list with
11231 ours. */
11232 if (rs->starting_up)
11233 return 0;
11234
11235 ts = current_trace_status ();
8bd200f1 11236 status = remote_get_trace_status (self, ts);
1e4d1764
YQ
11237
11238 if (status == -1 || !ts->running_known || !ts->running)
11239 return 0;
11240
11241 /* If we are in a tracing experiment, but remote stub doesn't support
11242 installing tracepoint in trace, we have to return. */
11243 if (!remote_supports_install_in_trace ())
11244 return 0;
11245
11246 return 1;
11247}
11248
11249
35b1e5cc 11250static void
559d2b81
TT
11251remote_download_trace_state_variable (struct target_ops *self,
11252 struct trace_state_variable *tsv)
35b1e5cc
SS
11253{
11254 struct remote_state *rs = get_remote_state ();
00bf0b85 11255 char *p;
35b1e5cc 11256
bba74b36
YQ
11257 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
11258 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
11259 tsv->builtin);
00bf0b85
SS
11260 p = rs->buf + strlen (rs->buf);
11261 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
11262 error (_("Trace state variable name too long for tsv definition packet"));
9f1b45b0 11263 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
00bf0b85 11264 *p++ = '\0';
35b1e5cc
SS
11265 putpkt (rs->buf);
11266 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11267 if (*target_buf == '\0')
11268 error (_("Target does not support this command."));
11269 if (strcmp (target_buf, "OK") != 0)
11270 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
11271}
11272
d248b706 11273static void
46670d57
TT
11274remote_enable_tracepoint (struct target_ops *self,
11275 struct bp_location *location)
d248b706
KY
11276{
11277 struct remote_state *rs = get_remote_state ();
11278 char addr_buf[40];
11279
11280 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11281 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
11282 location->owner->number, addr_buf);
d248b706
KY
11283 putpkt (rs->buf);
11284 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11285 if (*rs->buf == '\0')
11286 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
11287 if (strcmp (rs->buf, "OK") != 0)
11288 error (_("Error on target while enabling tracepoint."));
11289}
11290
11291static void
780b049c
TT
11292remote_disable_tracepoint (struct target_ops *self,
11293 struct bp_location *location)
d248b706
KY
11294{
11295 struct remote_state *rs = get_remote_state ();
11296 char addr_buf[40];
11297
11298 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11299 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
11300 location->owner->number, addr_buf);
d248b706
KY
11301 putpkt (rs->buf);
11302 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11303 if (*rs->buf == '\0')
11304 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
11305 if (strcmp (rs->buf, "OK") != 0)
11306 error (_("Error on target while disabling tracepoint."));
11307}
11308
35b1e5cc 11309static void
583f9a86 11310remote_trace_set_readonly_regions (struct target_ops *self)
35b1e5cc
SS
11311{
11312 asection *s;
81b9b86e 11313 bfd *abfd = NULL;
35b1e5cc 11314 bfd_size_type size;
608bcef2 11315 bfd_vma vma;
35b1e5cc 11316 int anysecs = 0;
c2fa21f1 11317 int offset = 0;
35b1e5cc
SS
11318
11319 if (!exec_bfd)
11320 return; /* No information to give. */
11321
11322 strcpy (target_buf, "QTro");
9779ab84 11323 offset = strlen (target_buf);
35b1e5cc
SS
11324 for (s = exec_bfd->sections; s; s = s->next)
11325 {
11326 char tmp1[40], tmp2[40];
c2fa21f1 11327 int sec_length;
35b1e5cc
SS
11328
11329 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 11330 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
11331 (s->flags & SEC_READONLY) == 0)
11332 continue;
11333
11334 anysecs = 1;
81b9b86e 11335 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 11336 size = bfd_get_section_size (s);
608bcef2
HZ
11337 sprintf_vma (tmp1, vma);
11338 sprintf_vma (tmp2, vma + size);
c2fa21f1
HZ
11339 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
11340 if (offset + sec_length + 1 > target_buf_size)
11341 {
4082afcc 11342 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 11343 warning (_("\
c2fa21f1
HZ
11344Too many sections for read-only sections definition packet."));
11345 break;
11346 }
bba74b36
YQ
11347 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
11348 tmp1, tmp2);
c2fa21f1 11349 offset += sec_length;
35b1e5cc
SS
11350 }
11351 if (anysecs)
11352 {
11353 putpkt (target_buf);
11354 getpkt (&target_buf, &target_buf_size, 0);
11355 }
11356}
11357
11358static void
e2d1aae3 11359remote_trace_start (struct target_ops *self)
35b1e5cc
SS
11360{
11361 putpkt ("QTStart");
11362 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11363 if (*target_buf == '\0')
11364 error (_("Target does not support this command."));
11365 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11366 error (_("Bogus reply from target: %s"), target_buf);
11367}
11368
11369static int
8bd200f1 11370remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
35b1e5cc 11371{
953b98d1 11372 /* Initialize it just to avoid a GCC false warning. */
f652de6f 11373 char *p = NULL;
0df8b418 11374 /* FIXME we need to get register block size some other way. */
00bf0b85 11375 extern int trace_regblock_size;
bd3eecc3
PA
11376 enum packet_result result;
11377
4082afcc 11378 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 11379 return -1;
a744cf53 11380
00bf0b85
SS
11381 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
11382
049dc89b
JK
11383 putpkt ("qTStatus");
11384
492d29ea 11385 TRY
67f41397
JK
11386 {
11387 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
11388 }
492d29ea 11389 CATCH (ex, RETURN_MASK_ERROR)
67f41397 11390 {
598d3636
JK
11391 if (ex.error != TARGET_CLOSE_ERROR)
11392 {
11393 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
11394 return -1;
11395 }
11396 throw_exception (ex);
67f41397 11397 }
492d29ea 11398 END_CATCH
00bf0b85 11399
bd3eecc3
PA
11400 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
11401
00bf0b85 11402 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 11403 if (result == PACKET_UNKNOWN)
00bf0b85 11404 return -1;
35b1e5cc 11405
00bf0b85 11406 /* We're working with a live target. */
f5911ea1 11407 ts->filename = NULL;
00bf0b85 11408
00bf0b85 11409 if (*p++ != 'T')
35b1e5cc
SS
11410 error (_("Bogus trace status reply from target: %s"), target_buf);
11411
84cebc4a
YQ
11412 /* Function 'parse_trace_status' sets default value of each field of
11413 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
11414 parse_trace_status (p, ts);
11415
11416 return ts->running;
35b1e5cc
SS
11417}
11418
70221824 11419static void
db90e85c 11420remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
f196051f
SS
11421 struct uploaded_tp *utp)
11422{
11423 struct remote_state *rs = get_remote_state ();
f196051f
SS
11424 char *reply;
11425 struct bp_location *loc;
11426 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 11427 size_t size = get_remote_packet_size ();
f196051f
SS
11428
11429 if (tp)
11430 {
11431 tp->base.hit_count = 0;
11432 tp->traceframe_usage = 0;
11433 for (loc = tp->base.loc; loc; loc = loc->next)
11434 {
11435 /* If the tracepoint was never downloaded, don't go asking for
11436 any status. */
11437 if (tp->number_on_target == 0)
11438 continue;
bba74b36
YQ
11439 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
11440 phex_nz (loc->address, 0));
f196051f
SS
11441 putpkt (rs->buf);
11442 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11443 if (reply && *reply)
11444 {
11445 if (*reply == 'V')
11446 parse_tracepoint_status (reply + 1, bp, utp);
11447 }
11448 }
11449 }
11450 else if (utp)
11451 {
11452 utp->hit_count = 0;
11453 utp->traceframe_usage = 0;
bba74b36
YQ
11454 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
11455 phex_nz (utp->addr, 0));
f196051f
SS
11456 putpkt (rs->buf);
11457 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11458 if (reply && *reply)
11459 {
11460 if (*reply == 'V')
11461 parse_tracepoint_status (reply + 1, bp, utp);
11462 }
11463 }
11464}
11465
35b1e5cc 11466static void
74499f1b 11467remote_trace_stop (struct target_ops *self)
35b1e5cc
SS
11468{
11469 putpkt ("QTStop");
11470 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11471 if (*target_buf == '\0')
11472 error (_("Target does not support this command."));
11473 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11474 error (_("Bogus reply from target: %s"), target_buf);
11475}
11476
11477static int
bd4c6793
TT
11478remote_trace_find (struct target_ops *self,
11479 enum trace_find_type type, int num,
cc5925ad 11480 CORE_ADDR addr1, CORE_ADDR addr2,
35b1e5cc
SS
11481 int *tpp)
11482{
11483 struct remote_state *rs = get_remote_state ();
bba74b36 11484 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
11485 char *p, *reply;
11486 int target_frameno = -1, target_tracept = -1;
11487
e6e4e701
PA
11488 /* Lookups other than by absolute frame number depend on the current
11489 trace selected, so make sure it is correct on the remote end
11490 first. */
11491 if (type != tfind_number)
11492 set_remote_traceframe ();
11493
35b1e5cc
SS
11494 p = rs->buf;
11495 strcpy (p, "QTFrame:");
11496 p = strchr (p, '\0');
11497 switch (type)
11498 {
11499 case tfind_number:
bba74b36 11500 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
11501 break;
11502 case tfind_pc:
bba74b36 11503 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
11504 break;
11505 case tfind_tp:
bba74b36 11506 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
11507 break;
11508 case tfind_range:
bba74b36
YQ
11509 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
11510 phex_nz (addr2, 0));
35b1e5cc
SS
11511 break;
11512 case tfind_outside:
bba74b36
YQ
11513 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
11514 phex_nz (addr2, 0));
35b1e5cc
SS
11515 break;
11516 default:
9b20d036 11517 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
11518 }
11519
11520 putpkt (rs->buf);
2f65bcb7 11521 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
ad91cd99
PA
11522 if (*reply == '\0')
11523 error (_("Target does not support this command."));
35b1e5cc
SS
11524
11525 while (reply && *reply)
11526 switch (*reply)
11527 {
11528 case 'F':
f197e0f1
VP
11529 p = ++reply;
11530 target_frameno = (int) strtol (p, &reply, 16);
11531 if (reply == p)
11532 error (_("Unable to parse trace frame number"));
e6e4e701
PA
11533 /* Don't update our remote traceframe number cache on failure
11534 to select a remote traceframe. */
f197e0f1
VP
11535 if (target_frameno == -1)
11536 return -1;
35b1e5cc
SS
11537 break;
11538 case 'T':
f197e0f1
VP
11539 p = ++reply;
11540 target_tracept = (int) strtol (p, &reply, 16);
11541 if (reply == p)
11542 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
11543 break;
11544 case 'O': /* "OK"? */
11545 if (reply[1] == 'K' && reply[2] == '\0')
11546 reply += 2;
11547 else
11548 error (_("Bogus reply from target: %s"), reply);
11549 break;
11550 default:
11551 error (_("Bogus reply from target: %s"), reply);
11552 }
11553 if (tpp)
11554 *tpp = target_tracept;
e6e4e701 11555
262e1174 11556 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
11557 return target_frameno;
11558}
11559
11560static int
4011015b
TT
11561remote_get_trace_state_variable_value (struct target_ops *self,
11562 int tsvnum, LONGEST *val)
35b1e5cc
SS
11563{
11564 struct remote_state *rs = get_remote_state ();
11565 char *reply;
11566 ULONGEST uval;
11567
e6e4e701
PA
11568 set_remote_traceframe ();
11569
bba74b36 11570 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc
SS
11571 putpkt (rs->buf);
11572 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11573 if (reply && *reply)
11574 {
11575 if (*reply == 'V')
11576 {
11577 unpack_varlen_hex (reply + 1, &uval);
11578 *val = (LONGEST) uval;
11579 return 1;
11580 }
11581 }
11582 return 0;
11583}
11584
00bf0b85 11585static int
dc3decaf 11586remote_save_trace_data (struct target_ops *self, const char *filename)
00bf0b85
SS
11587{
11588 struct remote_state *rs = get_remote_state ();
11589 char *p, *reply;
11590
11591 p = rs->buf;
11592 strcpy (p, "QTSave:");
11593 p += strlen (p);
11594 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
11595 error (_("Remote file name too long for trace save packet"));
9f1b45b0 11596 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
11597 *p++ = '\0';
11598 putpkt (rs->buf);
ad91cd99 11599 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
d6c5869f 11600 if (*reply == '\0')
ad91cd99
PA
11601 error (_("Target does not support this command."));
11602 if (strcmp (reply, "OK") != 0)
11603 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
11604 return 0;
11605}
11606
11607/* This is basically a memory transfer, but needs to be its own packet
11608 because we don't know how the target actually organizes its trace
11609 memory, plus we want to be able to ask for as much as possible, but
11610 not be unhappy if we don't get as much as we ask for. */
11611
11612static LONGEST
88ee6f45
TT
11613remote_get_raw_trace_data (struct target_ops *self,
11614 gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
11615{
11616 struct remote_state *rs = get_remote_state ();
11617 char *reply;
11618 char *p;
11619 int rslt;
11620
11621 p = rs->buf;
11622 strcpy (p, "qTBuffer:");
11623 p += strlen (p);
11624 p += hexnumstr (p, offset);
11625 *p++ = ',';
11626 p += hexnumstr (p, len);
11627 *p++ = '\0';
11628
11629 putpkt (rs->buf);
11630 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11631 if (reply && *reply)
11632 {
11633 /* 'l' by itself means we're at the end of the buffer and
11634 there is nothing more to get. */
11635 if (*reply == 'l')
11636 return 0;
11637
11638 /* Convert the reply into binary. Limit the number of bytes to
11639 convert according to our passed-in buffer size, rather than
11640 what was returned in the packet; if the target is
11641 unexpectedly generous and gives us a bigger reply than we
11642 asked for, we don't want to crash. */
11643 rslt = hex2bin (target_buf, buf, len);
11644 return rslt;
11645 }
11646
11647 /* Something went wrong, flag as an error. */
11648 return -1;
11649}
11650
35b1e5cc 11651static void
37b25738 11652remote_set_disconnected_tracing (struct target_ops *self, int val)
35b1e5cc
SS
11653{
11654 struct remote_state *rs = get_remote_state ();
11655
4082afcc 11656 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 11657 {
ad91cd99
PA
11658 char *reply;
11659
bba74b36 11660 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 11661 putpkt (rs->buf);
ad91cd99
PA
11662 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11663 if (*reply == '\0')
33da3f1c 11664 error (_("Target does not support this command."));
ad91cd99
PA
11665 if (strcmp (reply, "OK") != 0)
11666 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
11667 }
11668 else if (val)
11669 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
11670}
11671
dc146f7c
VP
11672static int
11673remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
11674{
11675 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 11676
fe978cb0
PA
11677 if (info && info->priv)
11678 return info->priv->core;
dc146f7c
VP
11679 return -1;
11680}
11681
4daf5ac0 11682static void
736d5b1f 11683remote_set_circular_trace_buffer (struct target_ops *self, int val)
4daf5ac0
SS
11684{
11685 struct remote_state *rs = get_remote_state ();
ad91cd99 11686 char *reply;
4daf5ac0 11687
bba74b36 11688 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 11689 putpkt (rs->buf);
ad91cd99
PA
11690 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11691 if (*reply == '\0')
4daf5ac0 11692 error (_("Target does not support this command."));
ad91cd99
PA
11693 if (strcmp (reply, "OK") != 0)
11694 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
11695}
11696
b3b9301e 11697static struct traceframe_info *
a893e81f 11698remote_traceframe_info (struct target_ops *self)
b3b9301e
PA
11699{
11700 char *text;
11701
11702 text = target_read_stralloc (&current_target,
11703 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11704 if (text != NULL)
11705 {
11706 struct traceframe_info *info;
11707 struct cleanup *back_to = make_cleanup (xfree, text);
11708
11709 info = parse_traceframe_info (text);
11710 do_cleanups (back_to);
11711 return info;
11712 }
11713
11714 return NULL;
11715}
11716
405f8e94
SS
11717/* Handle the qTMinFTPILen packet. Returns the minimum length of
11718 instruction on which a fast tracepoint may be placed. Returns -1
11719 if the packet is not supported, and 0 if the minimum instruction
11720 length is unknown. */
11721
11722static int
0e67620a 11723remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
405f8e94
SS
11724{
11725 struct remote_state *rs = get_remote_state ();
11726 char *reply;
11727
e886a173
PA
11728 /* If we're not debugging a process yet, the IPA can't be
11729 loaded. */
11730 if (!target_has_execution)
11731 return 0;
11732
11733 /* Make sure the remote is pointing at the right process. */
11734 set_general_process ();
11735
bba74b36 11736 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94
SS
11737 putpkt (rs->buf);
11738 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11739 if (*reply == '\0')
11740 return -1;
11741 else
11742 {
11743 ULONGEST min_insn_len;
11744
11745 unpack_varlen_hex (reply, &min_insn_len);
11746
11747 return (int) min_insn_len;
11748 }
11749}
11750
f6f899bf 11751static void
4da384be 11752remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
f6f899bf 11753{
4082afcc 11754 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
11755 {
11756 struct remote_state *rs = get_remote_state ();
11757 char *buf = rs->buf;
11758 char *endbuf = rs->buf + get_remote_packet_size ();
11759 enum packet_result result;
11760
11761 gdb_assert (val >= 0 || val == -1);
11762 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11763 /* Send -1 as literal "-1" to avoid host size dependency. */
11764 if (val < 0)
11765 {
11766 *buf++ = '-';
11767 buf += hexnumstr (buf, (ULONGEST) -val);
11768 }
11769 else
11770 buf += hexnumstr (buf, (ULONGEST) val);
11771
11772 putpkt (rs->buf);
11773 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11774 result = packet_ok (rs->buf,
11775 &remote_protocol_packets[PACKET_QTBuffer_size]);
11776
11777 if (result != PACKET_OK)
11778 warning (_("Bogus reply from target: %s"), rs->buf);
11779 }
11780}
11781
f196051f 11782static int
d9e68a2c
TT
11783remote_set_trace_notes (struct target_ops *self,
11784 const char *user, const char *notes,
ca623f82 11785 const char *stop_notes)
f196051f
SS
11786{
11787 struct remote_state *rs = get_remote_state ();
11788 char *reply;
11789 char *buf = rs->buf;
11790 char *endbuf = rs->buf + get_remote_packet_size ();
11791 int nbytes;
11792
11793 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11794 if (user)
11795 {
11796 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 11797 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
11798 buf += 2 * nbytes;
11799 *buf++ = ';';
11800 }
11801 if (notes)
11802 {
11803 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 11804 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
11805 buf += 2 * nbytes;
11806 *buf++ = ';';
11807 }
11808 if (stop_notes)
11809 {
11810 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 11811 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
11812 buf += 2 * nbytes;
11813 *buf++ = ';';
11814 }
11815 /* Ensure the buffer is terminated. */
11816 *buf = '\0';
11817
11818 putpkt (rs->buf);
11819 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11820 if (*reply == '\0')
11821 return 0;
11822
11823 if (strcmp (reply, "OK") != 0)
11824 error (_("Bogus reply from target: %s"), reply);
11825
11826 return 1;
11827}
11828
d1feda86 11829static int
2c152180 11830remote_use_agent (struct target_ops *self, int use)
d1feda86 11831{
4082afcc 11832 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
11833 {
11834 struct remote_state *rs = get_remote_state ();
11835
11836 /* If the stub supports QAgent. */
bba74b36 11837 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
11838 putpkt (rs->buf);
11839 getpkt (&rs->buf, &rs->buf_size, 0);
11840
11841 if (strcmp (rs->buf, "OK") == 0)
11842 {
11843 use_agent = use;
11844 return 1;
11845 }
11846 }
11847
11848 return 0;
11849}
11850
11851static int
fe38f897 11852remote_can_use_agent (struct target_ops *self)
d1feda86 11853{
4082afcc 11854 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
11855}
11856
9accd112
MM
11857struct btrace_target_info
11858{
11859 /* The ptid of the traced thread. */
11860 ptid_t ptid;
f4abbc16
MM
11861
11862 /* The obtained branch trace configuration. */
11863 struct btrace_config conf;
9accd112
MM
11864};
11865
f4abbc16
MM
11866/* Reset our idea of our target's btrace configuration. */
11867
11868static void
11869remote_btrace_reset (void)
11870{
11871 struct remote_state *rs = get_remote_state ();
11872
11873 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
11874}
11875
9accd112
MM
11876/* Check whether the target supports branch tracing. */
11877
11878static int
043c3577 11879remote_supports_btrace (struct target_ops *self, enum btrace_format format)
9accd112 11880{
4082afcc 11881 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
9accd112 11882 return 0;
4082afcc 11883 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
9accd112
MM
11884 return 0;
11885
043c3577
MM
11886 switch (format)
11887 {
11888 case BTRACE_FORMAT_NONE:
11889 return 0;
11890
11891 case BTRACE_FORMAT_BTS:
11892 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
b20a6524
MM
11893
11894 case BTRACE_FORMAT_PT:
11895 /* The trace is decoded on the host. Even if our target supports it,
11896 we still need to have libipt to decode the trace. */
11897#if defined (HAVE_LIBIPT)
11898 return (packet_support (PACKET_Qbtrace_pt) == PACKET_ENABLE);
11899#else /* !defined (HAVE_LIBIPT) */
11900 return 0;
11901#endif /* !defined (HAVE_LIBIPT) */
043c3577
MM
11902 }
11903
11904 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
9accd112
MM
11905}
11906
f4abbc16
MM
11907/* Synchronize the configuration with the target. */
11908
11909static void
11910btrace_sync_conf (const struct btrace_config *conf)
11911{
d33501a5
MM
11912 struct packet_config *packet;
11913 struct remote_state *rs;
11914 char *buf, *pos, *endbuf;
11915
11916 rs = get_remote_state ();
11917 buf = rs->buf;
11918 endbuf = buf + get_remote_packet_size ();
11919
11920 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
11921 if (packet_config_support (packet) == PACKET_ENABLE
11922 && conf->bts.size != rs->btrace_config.bts.size)
11923 {
11924 pos = buf;
11925 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
11926 conf->bts.size);
11927
11928 putpkt (buf);
11929 getpkt (&buf, &rs->buf_size, 0);
11930
11931 if (packet_ok (buf, packet) == PACKET_ERROR)
11932 {
11933 if (buf[0] == 'E' && buf[1] == '.')
11934 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
11935 else
11936 error (_("Failed to configure the BTS buffer size."));
11937 }
11938
11939 rs->btrace_config.bts.size = conf->bts.size;
11940 }
b20a6524
MM
11941
11942 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_pt_size];
11943 if (packet_config_support (packet) == PACKET_ENABLE
11944 && conf->pt.size != rs->btrace_config.pt.size)
11945 {
11946 pos = buf;
11947 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
11948 conf->pt.size);
11949
11950 putpkt (buf);
11951 getpkt (&buf, &rs->buf_size, 0);
11952
11953 if (packet_ok (buf, packet) == PACKET_ERROR)
11954 {
11955 if (buf[0] == 'E' && buf[1] == '.')
11956 error (_("Failed to configure the trace buffer size: %s"), buf + 2);
11957 else
11958 error (_("Failed to configure the trace buffer size."));
11959 }
11960
11961 rs->btrace_config.pt.size = conf->pt.size;
11962 }
f4abbc16
MM
11963}
11964
11965/* Read the current thread's btrace configuration from the target and
11966 store it into CONF. */
11967
11968static void
11969btrace_read_config (struct btrace_config *conf)
11970{
11971 char *xml;
11972
11973 xml = target_read_stralloc (&current_target,
b20a6524 11974 TARGET_OBJECT_BTRACE_CONF, "");
f4abbc16
MM
11975 if (xml != NULL)
11976 {
11977 struct cleanup *cleanup;
11978
11979 cleanup = make_cleanup (xfree, xml);
11980 parse_xml_btrace_conf (conf, xml);
11981 do_cleanups (cleanup);
11982 }
11983}
11984
9accd112
MM
11985/* Enable branch tracing. */
11986
11987static struct btrace_target_info *
f4abbc16
MM
11988remote_enable_btrace (struct target_ops *self, ptid_t ptid,
11989 const struct btrace_config *conf)
9accd112
MM
11990{
11991 struct btrace_target_info *tinfo = NULL;
b20a6524 11992 struct packet_config *packet = NULL;
9accd112
MM
11993 struct remote_state *rs = get_remote_state ();
11994 char *buf = rs->buf;
11995 char *endbuf = rs->buf + get_remote_packet_size ();
11996
b20a6524
MM
11997 switch (conf->format)
11998 {
11999 case BTRACE_FORMAT_BTS:
12000 packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
12001 break;
12002
12003 case BTRACE_FORMAT_PT:
12004 packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
12005 break;
12006 }
12007
12008 if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12009 error (_("Target does not support branch tracing."));
12010
f4abbc16
MM
12011 btrace_sync_conf (conf);
12012
9accd112
MM
12013 set_general_thread (ptid);
12014
12015 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12016 putpkt (rs->buf);
12017 getpkt (&rs->buf, &rs->buf_size, 0);
12018
12019 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12020 {
12021 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12022 error (_("Could not enable branch tracing for %s: %s"),
12023 target_pid_to_str (ptid), rs->buf + 2);
12024 else
12025 error (_("Could not enable branch tracing for %s."),
12026 target_pid_to_str (ptid));
12027 }
12028
12029 tinfo = xzalloc (sizeof (*tinfo));
12030 tinfo->ptid = ptid;
12031
f4abbc16
MM
12032 /* If we fail to read the configuration, we lose some information, but the
12033 tracing itself is not impacted. */
492d29ea
PA
12034 TRY
12035 {
12036 btrace_read_config (&tinfo->conf);
12037 }
12038 CATCH (err, RETURN_MASK_ERROR)
12039 {
12040 if (err.message != NULL)
12041 warning ("%s", err.message);
12042 }
12043 END_CATCH
f4abbc16 12044
9accd112
MM
12045 return tinfo;
12046}
12047
12048/* Disable branch tracing. */
12049
12050static void
25e95349
TT
12051remote_disable_btrace (struct target_ops *self,
12052 struct btrace_target_info *tinfo)
9accd112
MM
12053{
12054 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
12055 struct remote_state *rs = get_remote_state ();
12056 char *buf = rs->buf;
12057 char *endbuf = rs->buf + get_remote_packet_size ();
12058
4082afcc 12059 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12060 error (_("Target does not support branch tracing."));
12061
12062 set_general_thread (tinfo->ptid);
12063
12064 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12065 putpkt (rs->buf);
12066 getpkt (&rs->buf, &rs->buf_size, 0);
12067
12068 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12069 {
12070 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12071 error (_("Could not disable branch tracing for %s: %s"),
12072 target_pid_to_str (tinfo->ptid), rs->buf + 2);
12073 else
12074 error (_("Could not disable branch tracing for %s."),
12075 target_pid_to_str (tinfo->ptid));
12076 }
12077
12078 xfree (tinfo);
12079}
12080
12081/* Teardown branch tracing. */
12082
12083static void
1777056d
TT
12084remote_teardown_btrace (struct target_ops *self,
12085 struct btrace_target_info *tinfo)
9accd112
MM
12086{
12087 /* We must not talk to the target during teardown. */
12088 xfree (tinfo);
12089}
12090
12091/* Read the branch trace. */
12092
969c39fb 12093static enum btrace_error
39c49f83 12094remote_read_btrace (struct target_ops *self,
734b0e4b 12095 struct btrace_data *btrace,
969c39fb 12096 struct btrace_target_info *tinfo,
9accd112
MM
12097 enum btrace_read_type type)
12098{
12099 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
12100 struct remote_state *rs = get_remote_state ();
969c39fb 12101 struct cleanup *cleanup;
9accd112
MM
12102 const char *annex;
12103 char *xml;
12104
4082afcc 12105 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12106 error (_("Target does not support branch tracing."));
12107
12108#if !defined(HAVE_LIBEXPAT)
12109 error (_("Cannot process branch tracing result. XML parsing not supported."));
12110#endif
12111
12112 switch (type)
12113 {
864089d2 12114 case BTRACE_READ_ALL:
9accd112
MM
12115 annex = "all";
12116 break;
864089d2 12117 case BTRACE_READ_NEW:
9accd112
MM
12118 annex = "new";
12119 break;
969c39fb
MM
12120 case BTRACE_READ_DELTA:
12121 annex = "delta";
12122 break;
9accd112
MM
12123 default:
12124 internal_error (__FILE__, __LINE__,
12125 _("Bad branch tracing read type: %u."),
12126 (unsigned int) type);
12127 }
12128
12129 xml = target_read_stralloc (&current_target,
b20a6524 12130 TARGET_OBJECT_BTRACE, annex);
969c39fb
MM
12131 if (xml == NULL)
12132 return BTRACE_ERR_UNKNOWN;
9accd112 12133
969c39fb 12134 cleanup = make_cleanup (xfree, xml);
734b0e4b 12135 parse_xml_btrace (btrace, xml);
969c39fb 12136 do_cleanups (cleanup);
9accd112 12137
969c39fb 12138 return BTRACE_ERR_NONE;
9accd112
MM
12139}
12140
f4abbc16
MM
12141static const struct btrace_config *
12142remote_btrace_conf (struct target_ops *self,
12143 const struct btrace_target_info *tinfo)
12144{
12145 return &tinfo->conf;
12146}
12147
ced63ec0 12148static int
5436ff03 12149remote_augmented_libraries_svr4_read (struct target_ops *self)
ced63ec0 12150{
4082afcc
PA
12151 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
12152 == PACKET_ENABLE);
ced63ec0
GB
12153}
12154
9dd130a0
TT
12155/* Implementation of to_load. */
12156
12157static void
9cbe5fff 12158remote_load (struct target_ops *self, const char *name, int from_tty)
9dd130a0
TT
12159{
12160 generic_load (name, from_tty);
12161}
12162
c78fa86a
GB
12163/* Accepts an integer PID; returns a string representing a file that
12164 can be opened on the remote side to get the symbols for the child
12165 process. Returns NULL if the operation is not supported. */
12166
12167static char *
12168remote_pid_to_exec_file (struct target_ops *self, int pid)
12169{
12170 static char *filename = NULL;
835205d0
GB
12171 struct inferior *inf;
12172 char *annex = NULL;
c78fa86a
GB
12173
12174 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
12175 return NULL;
12176
12177 if (filename != NULL)
12178 xfree (filename);
12179
835205d0
GB
12180 inf = find_inferior_pid (pid);
12181 if (inf == NULL)
12182 internal_error (__FILE__, __LINE__,
12183 _("not currently attached to process %d"), pid);
12184
12185 if (!inf->fake_pid_p)
12186 {
12187 const int annex_size = 9;
12188
12189 annex = alloca (annex_size);
12190 xsnprintf (annex, annex_size, "%x", pid);
12191 }
12192
c78fa86a
GB
12193 filename = target_read_stralloc (&current_target,
12194 TARGET_OBJECT_EXEC_FILE, annex);
12195
12196 return filename;
12197}
12198
c906108c 12199static void
fba45db2 12200init_remote_ops (void)
c906108c 12201{
c5aa993b 12202 remote_ops.to_shortname = "remote";
c906108c 12203 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 12204 remote_ops.to_doc =
c906108c 12205 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
12206Specify the serial device it is connected to\n\
12207(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
12208 remote_ops.to_open = remote_open;
12209 remote_ops.to_close = remote_close;
c906108c 12210 remote_ops.to_detach = remote_detach;
6ad8ae5c 12211 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 12212 remote_ops.to_resume = remote_resume;
c906108c
SS
12213 remote_ops.to_wait = remote_wait;
12214 remote_ops.to_fetch_registers = remote_fetch_registers;
12215 remote_ops.to_store_registers = remote_store_registers;
12216 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b 12217 remote_ops.to_files_info = remote_files_info;
c906108c
SS
12218 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
12219 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
f7e6eed5
PA
12220 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
12221 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
12222 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
12223 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
3c3bea1c
GS
12224 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
12225 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
12226 remote_ops.to_watchpoint_addr_within_range =
12227 remote_watchpoint_addr_within_range;
3c3bea1c
GS
12228 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
12229 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
12230 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
12231 remote_ops.to_region_ok_for_hw_watchpoint
12232 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
12233 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
12234 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b 12235 remote_ops.to_kill = remote_kill;
9dd130a0 12236 remote_ops.to_load = remote_load;
c906108c 12237 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 12238 remote_ops.to_pass_signals = remote_pass_signals;
9b224c5e 12239 remote_ops.to_program_signals = remote_program_signals;
c906108c 12240 remote_ops.to_thread_alive = remote_thread_alive;
e8032dde 12241 remote_ops.to_update_thread_list = remote_update_thread_list;
0caabb7e 12242 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 12243 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 12244 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 12245 remote_ops.to_stop = remote_stop;
4b8a223f 12246 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 12247 remote_ops.to_rcmd = remote_rcmd;
c78fa86a 12248 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
49d03eab 12249 remote_ops.to_log_command = serial_log_command;
38691318 12250 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 12251 remote_ops.to_stratum = process_stratum;
c35b1492
PA
12252 remote_ops.to_has_all_memory = default_child_has_all_memory;
12253 remote_ops.to_has_memory = default_child_has_memory;
12254 remote_ops.to_has_stack = default_child_has_stack;
12255 remote_ops.to_has_registers = default_child_has_registers;
12256 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 12257 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 12258 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 12259 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 12260 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
12261 remote_ops.to_flash_erase = remote_flash_erase;
12262 remote_ops.to_flash_done = remote_flash_done;
29709017 12263 remote_ops.to_read_description = remote_read_description;
08388c79 12264 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
12265 remote_ops.to_can_async_p = remote_can_async_p;
12266 remote_ops.to_is_async_p = remote_is_async_p;
12267 remote_ops.to_async = remote_async;
75c99385
PA
12268 remote_ops.to_terminal_inferior = remote_terminal_inferior;
12269 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 12270 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 12271 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
12272 remote_ops.to_supports_disable_randomization
12273 = remote_supports_disable_randomization;
4bd7dc42 12274 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
7313baad
UW
12275 remote_ops.to_fileio_open = remote_hostio_open;
12276 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
12277 remote_ops.to_fileio_pread = remote_hostio_pread;
9b15c1f0 12278 remote_ops.to_fileio_fstat = remote_hostio_fstat;
7313baad
UW
12279 remote_ops.to_fileio_close = remote_hostio_close;
12280 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 12281 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 12282 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 12283 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 12284 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
d3ce09f5 12285 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
35b1e5cc
SS
12286 remote_ops.to_trace_init = remote_trace_init;
12287 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 12288 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
12289 remote_ops.to_download_trace_state_variable
12290 = remote_download_trace_state_variable;
d248b706
KY
12291 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
12292 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
12293 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
12294 remote_ops.to_trace_start = remote_trace_start;
12295 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 12296 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
12297 remote_ops.to_trace_stop = remote_trace_stop;
12298 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
12299 remote_ops.to_get_trace_state_variable_value
12300 = remote_get_trace_state_variable_value;
00bf0b85
SS
12301 remote_ops.to_save_trace_data = remote_save_trace_data;
12302 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
12303 remote_ops.to_upload_trace_state_variables
12304 = remote_upload_trace_state_variables;
00bf0b85 12305 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 12306 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 12307 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 12308 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f6f899bf 12309 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
f196051f 12310 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 12311 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 12312 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 12313 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 12314 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
12315 remote_ops.to_static_tracepoint_marker_at
12316 = remote_static_tracepoint_marker_at;
12317 remote_ops.to_static_tracepoint_markers_by_strid
12318 = remote_static_tracepoint_markers_by_strid;
b3b9301e 12319 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
12320 remote_ops.to_use_agent = remote_use_agent;
12321 remote_ops.to_can_use_agent = remote_can_use_agent;
9accd112
MM
12322 remote_ops.to_supports_btrace = remote_supports_btrace;
12323 remote_ops.to_enable_btrace = remote_enable_btrace;
12324 remote_ops.to_disable_btrace = remote_disable_btrace;
12325 remote_ops.to_teardown_btrace = remote_teardown_btrace;
12326 remote_ops.to_read_btrace = remote_read_btrace;
f4abbc16 12327 remote_ops.to_btrace_conf = remote_btrace_conf;
ced63ec0
GB
12328 remote_ops.to_augmented_libraries_svr4_read =
12329 remote_augmented_libraries_svr4_read;
c906108c
SS
12330}
12331
12332/* Set up the extended remote vector by making a copy of the standard
12333 remote vector and adding to it. */
12334
12335static void
fba45db2 12336init_extended_remote_ops (void)
c906108c
SS
12337{
12338 extended_remote_ops = remote_ops;
12339
0f71a2f6 12340 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 12341 extended_remote_ops.to_longname =
c906108c 12342 "Extended remote serial target in gdb-specific protocol";
c5aa993b 12343 extended_remote_ops.to_doc =
c906108c 12344 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
12345Specify the serial device it is connected to (e.g. /dev/ttya).";
12346 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
12347 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
12348 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2d717e4f
DJ
12349 extended_remote_ops.to_detach = extended_remote_detach;
12350 extended_remote_ops.to_attach = extended_remote_attach;
b9c1d481 12351 extended_remote_ops.to_post_attach = extended_remote_post_attach;
82f73884 12352 extended_remote_ops.to_kill = extended_remote_kill;
03583c20
UW
12353 extended_remote_ops.to_supports_disable_randomization
12354 = extended_remote_supports_disable_randomization;
de0d863e 12355 extended_remote_ops.to_follow_fork = remote_follow_fork;
cbb8991c
DB
12356 extended_remote_ops.to_insert_fork_catchpoint
12357 = remote_insert_fork_catchpoint;
12358 extended_remote_ops.to_remove_fork_catchpoint
12359 = remote_remove_fork_catchpoint;
12360 extended_remote_ops.to_insert_vfork_catchpoint
12361 = remote_insert_vfork_catchpoint;
12362 extended_remote_ops.to_remove_vfork_catchpoint
12363 = remote_remove_vfork_catchpoint;
0f71a2f6
JM
12364}
12365
6426a772 12366static int
6a109b6b 12367remote_can_async_p (struct target_ops *ops)
6426a772 12368{
5d93a237
TT
12369 struct remote_state *rs = get_remote_state ();
12370
c6ebd6cf 12371 if (!target_async_permitted)
75c99385
PA
12372 /* We only enable async when the user specifically asks for it. */
12373 return 0;
12374
23860348 12375 /* We're async whenever the serial device is. */
5d93a237 12376 return serial_can_async_p (rs->remote_desc);
6426a772
JM
12377}
12378
12379static int
6a109b6b 12380remote_is_async_p (struct target_ops *ops)
6426a772 12381{
5d93a237
TT
12382 struct remote_state *rs = get_remote_state ();
12383
c6ebd6cf 12384 if (!target_async_permitted)
75c99385
PA
12385 /* We only enable async when the user specifically asks for it. */
12386 return 0;
12387
23860348 12388 /* We're async whenever the serial device is. */
5d93a237 12389 return serial_is_async_p (rs->remote_desc);
6426a772
JM
12390}
12391
2acceee2
JM
12392/* Pass the SERIAL event on and up to the client. One day this code
12393 will be able to delay notifying the client of an event until the
23860348 12394 point where an entire packet has been received. */
2acceee2 12395
2acceee2
JM
12396static serial_event_ftype remote_async_serial_handler;
12397
6426a772 12398static void
819cc324 12399remote_async_serial_handler (struct serial *scb, void *context)
6426a772 12400{
88b496c3
TT
12401 struct remote_state *rs = context;
12402
2acceee2
JM
12403 /* Don't propogate error information up to the client. Instead let
12404 the client find out about the error by querying the target. */
6a3753b3 12405 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
12406}
12407
74531fed
PA
12408static void
12409remote_async_inferior_event_handler (gdb_client_data data)
12410{
12411 inferior_event_handler (INF_REG_EVENT, NULL);
12412}
12413
2acceee2 12414static void
6a3753b3 12415remote_async (struct target_ops *ops, int enable)
2acceee2 12416{
5d93a237
TT
12417 struct remote_state *rs = get_remote_state ();
12418
6a3753b3 12419 if (enable)
2acceee2 12420 {
88b496c3 12421 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
12422
12423 /* If there are pending events in the stop reply queue tell the
12424 event loop to process them. */
12425 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
12426 mark_async_event_handler (remote_async_inferior_event_token);
2acceee2
JM
12427 }
12428 else
b7d2e916
PA
12429 {
12430 serial_async (rs->remote_desc, NULL, NULL);
12431 clear_async_event_handler (remote_async_inferior_event_token);
12432 }
6426a772
JM
12433}
12434
5a2468f5 12435static void
c2d11a7d 12436set_remote_cmd (char *args, int from_tty)
5a2468f5 12437{
635c7e8a 12438 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
12439}
12440
d471ea57
AC
12441static void
12442show_remote_cmd (char *args, int from_tty)
12443{
37a105a1 12444 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 12445 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1
DJ
12446 struct cleanup *showlist_chain;
12447 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 12448 struct ui_out *uiout = current_uiout;
37a105a1
DJ
12449
12450 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
12451 for (; list != NULL; list = list->next)
12452 if (strcmp (list->name, "Z-packet") == 0)
12453 continue;
427c3a89
DJ
12454 else if (list->type == not_set_cmd)
12455 /* Alias commands are exactly like the original, except they
12456 don't have the normal type. */
12457 continue;
12458 else
37a105a1
DJ
12459 {
12460 struct cleanup *option_chain
12461 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
a744cf53 12462
37a105a1
DJ
12463 ui_out_field_string (uiout, "name", list->name);
12464 ui_out_text (uiout, ": ");
427c3a89 12465 if (list->type == show_cmd)
5b9afe8a 12466 do_show_command ((char *) NULL, from_tty, list);
427c3a89
DJ
12467 else
12468 cmd_func (list, NULL, from_tty);
37a105a1
DJ
12469 /* Close the tuple. */
12470 do_cleanups (option_chain);
12471 }
427c3a89
DJ
12472
12473 /* Close the tuple. */
12474 do_cleanups (showlist_chain);
d471ea57 12475}
5a2468f5 12476
0f71a2f6 12477
23860348 12478/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
12479static void
12480remote_new_objfile (struct objfile *objfile)
12481{
5d93a237
TT
12482 struct remote_state *rs = get_remote_state ();
12483
12484 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 12485 remote_check_symbols ();
dc8acb97
MS
12486}
12487
00bf0b85
SS
12488/* Pull all the tracepoints defined on the target and create local
12489 data structures representing them. We don't want to create real
12490 tracepoints yet, we don't want to mess up the user's existing
12491 collection. */
12492
12493static int
ab6617cc 12494remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
d5551862 12495{
00bf0b85
SS
12496 struct remote_state *rs = get_remote_state ();
12497 char *p;
d5551862 12498
00bf0b85
SS
12499 /* Ask for a first packet of tracepoint definition. */
12500 putpkt ("qTfP");
12501 getpkt (&rs->buf, &rs->buf_size, 0);
12502 p = rs->buf;
12503 while (*p && *p != 'l')
d5551862 12504 {
00bf0b85
SS
12505 parse_tracepoint_definition (p, utpp);
12506 /* Ask for another packet of tracepoint definition. */
12507 putpkt ("qTsP");
12508 getpkt (&rs->buf, &rs->buf_size, 0);
12509 p = rs->buf;
d5551862 12510 }
00bf0b85 12511 return 0;
d5551862
SS
12512}
12513
00bf0b85 12514static int
181e3713
TT
12515remote_upload_trace_state_variables (struct target_ops *self,
12516 struct uploaded_tsv **utsvp)
d5551862 12517{
00bf0b85 12518 struct remote_state *rs = get_remote_state ();
d5551862 12519 char *p;
d5551862 12520
00bf0b85
SS
12521 /* Ask for a first packet of variable definition. */
12522 putpkt ("qTfV");
d5551862
SS
12523 getpkt (&rs->buf, &rs->buf_size, 0);
12524 p = rs->buf;
00bf0b85 12525 while (*p && *p != 'l')
d5551862 12526 {
00bf0b85
SS
12527 parse_tsv_definition (p, utsvp);
12528 /* Ask for another packet of variable definition. */
12529 putpkt ("qTsV");
d5551862
SS
12530 getpkt (&rs->buf, &rs->buf_size, 0);
12531 p = rs->buf;
12532 }
00bf0b85 12533 return 0;
d5551862
SS
12534}
12535
c1e36e3e
PA
12536/* The "set/show range-stepping" show hook. */
12537
12538static void
12539show_range_stepping (struct ui_file *file, int from_tty,
12540 struct cmd_list_element *c,
12541 const char *value)
12542{
12543 fprintf_filtered (file,
12544 _("Debugger's willingness to use range stepping "
12545 "is %s.\n"), value);
12546}
12547
12548/* The "set/show range-stepping" set hook. */
12549
12550static void
12551set_range_stepping (char *ignore_args, int from_tty,
12552 struct cmd_list_element *c)
12553{
5d93a237
TT
12554 struct remote_state *rs = get_remote_state ();
12555
c1e36e3e
PA
12556 /* Whene enabling, check whether range stepping is actually
12557 supported by the target, and warn if not. */
12558 if (use_range_stepping)
12559 {
5d93a237 12560 if (rs->remote_desc != NULL)
c1e36e3e 12561 {
4082afcc 12562 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
12563 remote_vcont_probe (rs);
12564
4082afcc 12565 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
12566 && rs->supports_vCont.r)
12567 return;
12568 }
12569
12570 warning (_("Range stepping is not supported by the current target"));
12571 }
12572}
12573
c906108c 12574void
fba45db2 12575_initialize_remote (void)
c906108c 12576{
ea9c271d 12577 struct remote_state *rs;
9a7071a8 12578 struct cmd_list_element *cmd;
6f937416 12579 const char *cmd_name;
ea9c271d 12580
0f71a2f6 12581 /* architecture specific data */
2bc416ba 12582 remote_gdbarch_data_handle =
23860348 12583 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
12584 remote_g_packet_data_handle =
12585 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 12586
ea9c271d
DJ
12587 /* Initialize the per-target state. At the moment there is only one
12588 of these, not one per target. Only one target is active at a
cf792862
TT
12589 time. */
12590 remote_state = new_remote_state ();
ea9c271d 12591
c906108c
SS
12592 init_remote_ops ();
12593 add_target (&remote_ops);
12594
12595 init_extended_remote_ops ();
12596 add_target (&extended_remote_ops);
cce74817 12597
dc8acb97 12598 /* Hook into new objfile notification. */
06d3b283 12599 observer_attach_new_objfile (remote_new_objfile);
5f4cf0bb
YQ
12600 /* We're no longer interested in notification events of an inferior
12601 when it exits. */
12602 observer_attach_inferior_exit (discard_pending_stop_replies);
dc8acb97 12603
b803fb0f 12604 /* Set up signal handlers. */
934b9bac 12605 async_sigint_remote_token =
b803fb0f 12606 create_async_signal_handler (async_remote_interrupt, NULL);
934b9bac 12607 async_sigint_remote_twice_token =
6d549500 12608 create_async_signal_handler (async_remote_interrupt_twice, NULL);
b803fb0f 12609
c906108c
SS
12610#if 0
12611 init_remote_threadtests ();
12612#endif
12613
722247f1 12614 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 12615 /* set/show remote ... */
d471ea57 12616
1bedd215 12617 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
12618Remote protocol specific variables\n\
12619Configure various remote-protocol specific variables such as\n\
1bedd215 12620the packets being used"),
cff3e48b 12621 &remote_set_cmdlist, "set remote ",
23860348 12622 0 /* allow-unknown */, &setlist);
1bedd215 12623 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
12624Remote protocol specific variables\n\
12625Configure various remote-protocol specific variables such as\n\
1bedd215 12626the packets being used"),
cff3e48b 12627 &remote_show_cmdlist, "show remote ",
23860348 12628 0 /* allow-unknown */, &showlist);
5a2468f5 12629
1a966eab
AC
12630 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
12631Compare section data on target to the exec file.\n\
95cf3b38
DT
12632Argument is a single section name (default: all loaded sections).\n\
12633To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
12634 &cmdlist);
12635
1a966eab
AC
12636 add_cmd ("packet", class_maintenance, packet_command, _("\
12637Send an arbitrary packet to a remote target.\n\
c906108c
SS
12638 maintenance packet TEXT\n\
12639If GDB is talking to an inferior via the GDB serial protocol, then\n\
12640this command sends the string TEXT to the inferior, and displays the\n\
12641response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 12642terminating `#' character and checksum."),
c906108c
SS
12643 &maintenancelist);
12644
7915a72c
AC
12645 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
12646Set whether to send break if interrupted."), _("\
12647Show whether to send break if interrupted."), _("\
12648If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 12649 set_remotebreak, show_remotebreak,
e707bbc2 12650 &setlist, &showlist);
9a7071a8
JB
12651 cmd_name = "remotebreak";
12652 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
12653 deprecate_cmd (cmd, "set remote interrupt-sequence");
12654 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
12655 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
12656 deprecate_cmd (cmd, "show remote interrupt-sequence");
12657
12658 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
12659 interrupt_sequence_modes, &interrupt_sequence_mode,
12660 _("\
9a7071a8
JB
12661Set interrupt sequence to remote target."), _("\
12662Show interrupt sequence to remote target."), _("\
12663Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
12664 NULL, show_interrupt_sequence,
12665 &remote_set_cmdlist,
12666 &remote_show_cmdlist);
12667
12668 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
12669 &interrupt_on_connect, _("\
12670Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12671Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12672If set, interrupt sequence is sent to remote target."),
12673 NULL, NULL,
12674 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 12675
23860348 12676 /* Install commands for configuring memory read/write packets. */
11cf8741 12677
1a966eab
AC
12678 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
12679Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 12680 &setlist);
1a966eab
AC
12681 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
12682Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
12683 &showlist);
12684 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
12685 set_memory_write_packet_size, _("\
12686Set the maximum number of bytes per memory-write packet.\n\
12687Specify the number of bytes in a packet or 0 (zero) for the\n\
12688default packet size. The actual limit is further reduced\n\
12689dependent on the target. Specify ``fixed'' to disable the\n\
12690further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12691 &remote_set_cmdlist);
12692 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
12693 set_memory_read_packet_size, _("\
12694Set the maximum number of bytes per memory-read packet.\n\
12695Specify the number of bytes in a packet or 0 (zero) for the\n\
12696default packet size. The actual limit is further reduced\n\
12697dependent on the target. Specify ``fixed'' to disable the\n\
12698further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12699 &remote_set_cmdlist);
12700 add_cmd ("memory-write-packet-size", no_class,
12701 show_memory_write_packet_size,
1a966eab 12702 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
12703 &remote_show_cmdlist);
12704 add_cmd ("memory-read-packet-size", no_class,
12705 show_memory_read_packet_size,
1a966eab 12706 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 12707 &remote_show_cmdlist);
c906108c 12708
b3f42336 12709 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
12710 &remote_hw_watchpoint_limit, _("\
12711Set the maximum number of target hardware watchpoints."), _("\
12712Show the maximum number of target hardware watchpoints."), _("\
12713Specify a negative limit for unlimited."),
3e43a32a
MS
12714 NULL, NULL, /* FIXME: i18n: The maximum
12715 number of target hardware
12716 watchpoints is %s. */
b3f42336 12717 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
12718 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
12719 &remote_hw_watchpoint_length_limit, _("\
12720Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
12721Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
12722Specify a negative limit for unlimited."),
12723 NULL, NULL, /* FIXME: i18n: The maximum
12724 length (in bytes) of a target
12725 hardware watchpoint is %s. */
12726 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 12727 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
12728 &remote_hw_breakpoint_limit, _("\
12729Set the maximum number of target hardware breakpoints."), _("\
12730Show the maximum number of target hardware breakpoints."), _("\
12731Specify a negative limit for unlimited."),
3e43a32a
MS
12732 NULL, NULL, /* FIXME: i18n: The maximum
12733 number of target hardware
12734 breakpoints is %s. */
b3f42336 12735 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 12736
1b493192
PA
12737 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
12738 &remote_address_size, _("\
4d28ad1e
AC
12739Set the maximum size of the address (in bits) in a memory packet."), _("\
12740Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
12741 NULL,
12742 NULL, /* FIXME: i18n: */
12743 &setlist, &showlist);
c906108c 12744
ca4f7f8b
PA
12745 init_all_packet_configs ();
12746
444abaca 12747 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 12748 "X", "binary-download", 1);
0f71a2f6 12749
444abaca 12750 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 12751 "vCont", "verbose-resume", 0);
506fb367 12752
89be2091
DJ
12753 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
12754 "QPassSignals", "pass-signals", 0);
12755
9b224c5e
PA
12756 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
12757 "QProgramSignals", "program-signals", 0);
12758
444abaca 12759 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 12760 "qSymbol", "symbol-lookup", 0);
dc8acb97 12761
444abaca 12762 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 12763 "P", "set-register", 1);
d471ea57 12764
444abaca 12765 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 12766 "p", "fetch-register", 1);
b96ec7ac 12767
444abaca 12768 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 12769 "Z0", "software-breakpoint", 0);
d471ea57 12770
444abaca 12771 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 12772 "Z1", "hardware-breakpoint", 0);
d471ea57 12773
444abaca 12774 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 12775 "Z2", "write-watchpoint", 0);
d471ea57 12776
444abaca 12777 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 12778 "Z3", "read-watchpoint", 0);
d471ea57 12779
444abaca 12780 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 12781 "Z4", "access-watchpoint", 0);
d471ea57 12782
0876f84a
DJ
12783 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
12784 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 12785
c78fa86a
GB
12786 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
12787 "qXfer:exec-file:read", "pid-to-exec-file", 0);
12788
23181151
DJ
12789 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
12790 "qXfer:features:read", "target-features", 0);
12791
cfa9d6d9
DJ
12792 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
12793 "qXfer:libraries:read", "library-info", 0);
12794
2268b414
JK
12795 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
12796 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
12797
fd79ecee
DJ
12798 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
12799 "qXfer:memory-map:read", "memory-map", 0);
12800
0e7f50da
UW
12801 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
12802 "qXfer:spu:read", "read-spu-object", 0);
12803
12804 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
12805 "qXfer:spu:write", "write-spu-object", 0);
12806
07e059b5
VP
12807 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
12808 "qXfer:osdata:read", "osdata", 0);
12809
dc146f7c
VP
12810 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
12811 "qXfer:threads:read", "threads", 0);
12812
4aa995e1
PA
12813 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
12814 "qXfer:siginfo:read", "read-siginfo-object", 0);
12815
12816 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
12817 "qXfer:siginfo:write", "write-siginfo-object", 0);
12818
b3b9301e
PA
12819 add_packet_config_cmd
12820 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 12821 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 12822
169081d0
TG
12823 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
12824 "qXfer:uib:read", "unwind-info-block", 0);
12825
444abaca 12826 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 12827 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
12828 0);
12829
711e434b
PM
12830 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
12831 "qGetTIBAddr", "get-thread-information-block-address",
12832 0);
12833
40ab02ce
MS
12834 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
12835 "bc", "reverse-continue", 0);
12836
12837 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
12838 "bs", "reverse-step", 0);
12839
be2a5f71
DJ
12840 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
12841 "qSupported", "supported-packets", 0);
12842
08388c79
DE
12843 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
12844 "qSearch:memory", "search-memory", 0);
12845
bd3eecc3
PA
12846 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
12847 "qTStatus", "trace-status", 0);
12848
15a201c8
GB
12849 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
12850 "vFile:setfs", "hostio-setfs", 0);
12851
a6b151f1
DJ
12852 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
12853 "vFile:open", "hostio-open", 0);
12854
12855 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
12856 "vFile:pread", "hostio-pread", 0);
12857
12858 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
12859 "vFile:pwrite", "hostio-pwrite", 0);
12860
12861 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
12862 "vFile:close", "hostio-close", 0);
12863
12864 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
12865 "vFile:unlink", "hostio-unlink", 0);
12866
b9e7b9c3
UW
12867 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
12868 "vFile:readlink", "hostio-readlink", 0);
12869
0a93529c
GB
12870 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
12871 "vFile:fstat", "hostio-fstat", 0);
12872
2d717e4f
DJ
12873 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
12874 "vAttach", "attach", 0);
12875
12876 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
12877 "vRun", "run", 0);
12878
a6f3e723
SL
12879 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
12880 "QStartNoAckMode", "noack", 0);
12881
82f73884
PA
12882 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
12883 "vKill", "kill", 0);
12884
0b16c5cf
PA
12885 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
12886 "qAttached", "query-attached", 0);
12887
782b2b07 12888 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
12889 "ConditionalTracepoints",
12890 "conditional-tracepoints", 0);
3788aec7
LM
12891
12892 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
12893 "ConditionalBreakpoints",
12894 "conditional-breakpoints", 0);
12895
d3ce09f5
SS
12896 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
12897 "BreakpointCommands",
12898 "breakpoint-commands", 0);
12899
7a697b8d
SS
12900 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
12901 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 12902
409873ef
SS
12903 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
12904 "TracepointSource", "TracepointSource", 0);
12905
d914c394
SS
12906 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
12907 "QAllow", "allow", 0);
12908
0fb4aa4b
PA
12909 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
12910 "StaticTracepoints", "static-tracepoints", 0);
12911
1e4d1764
YQ
12912 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
12913 "InstallInTrace", "install-in-trace", 0);
12914
0fb4aa4b
PA
12915 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
12916 "qXfer:statictrace:read", "read-sdata-object", 0);
12917
78d85199
YQ
12918 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
12919 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12920
03583c20
UW
12921 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
12922 "QDisableRandomization", "disable-randomization", 0);
12923
d1feda86
YQ
12924 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
12925 "QAgent", "agent", 0);
12926
f6f899bf
HAQ
12927 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
12928 "QTBuffer:size", "trace-buffer-size", 0);
12929
9accd112
MM
12930 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
12931 "Qbtrace:off", "disable-btrace", 0);
12932
12933 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
b20a6524
MM
12934 "Qbtrace:bts", "enable-btrace-bts", 0);
12935
12936 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_pt],
12937 "Qbtrace:pt", "enable-btrace-pt", 0);
9accd112
MM
12938
12939 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
12940 "qXfer:btrace", "read-btrace", 0);
12941
f4abbc16
MM
12942 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
12943 "qXfer:btrace-conf", "read-btrace-conf", 0);
12944
d33501a5
MM
12945 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
12946 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
12947
f7e6eed5
PA
12948 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
12949 "swbreak-feature", "swbreak-feature", 0);
12950
12951 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
12952 "hwbreak-feature", "hwbreak-feature", 0);
12953
89245bc0
DB
12954 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
12955 "fork-event-feature", "fork-event-feature", 0);
12956
12957 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
12958 "vfork-event-feature", "vfork-event-feature", 0);
12959
b20a6524
MM
12960 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_pt_size],
12961 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
12962
0b736949
DB
12963 /* Assert that we've registered "set remote foo-packet" commands
12964 for all packet configs. */
ca4f7f8b
PA
12965 {
12966 int i;
12967
12968 for (i = 0; i < PACKET_MAX; i++)
12969 {
12970 /* Ideally all configs would have a command associated. Some
12971 still don't though. */
12972 int excepted;
12973
12974 switch (i)
12975 {
12976 case PACKET_QNonStop:
12977 case PACKET_multiprocess_feature:
12978 case PACKET_EnableDisableTracepoints_feature:
12979 case PACKET_tracenz_feature:
12980 case PACKET_DisconnectedTracing_feature:
12981 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
12982 case PACKET_qCRC:
12983 /* Additions to this list need to be well justified:
12984 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
12985 excepted = 1;
12986 break;
12987 default:
12988 excepted = 0;
12989 break;
12990 }
12991
12992 /* This catches both forgetting to add a config command, and
12993 forgetting to remove a packet from the exception list. */
12994 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
12995 }
12996 }
12997
37a105a1
DJ
12998 /* Keep the old ``set remote Z-packet ...'' working. Each individual
12999 Z sub-packet has its own set and show commands, but users may
13000 have sets to this variable in their .gdbinit files (or in their
13001 documentation). */
e9e68a56 13002 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
13003 &remote_Z_packet_detect, _("\
13004Set use of remote protocol `Z' packets"), _("\
13005Show use of remote protocol `Z' packets "), _("\
3b64bf98 13006When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 13007packets."),
e9e68a56 13008 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
13009 show_remote_protocol_Z_packet_cmd,
13010 /* FIXME: i18n: Use of remote protocol
13011 `Z' packets is %s. */
e9e68a56 13012 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 13013
a6b151f1
DJ
13014 add_prefix_cmd ("remote", class_files, remote_command, _("\
13015Manipulate files on the remote system\n\
13016Transfer files to and from the remote target system."),
13017 &remote_cmdlist, "remote ",
13018 0 /* allow-unknown */, &cmdlist);
13019
13020 add_cmd ("put", class_files, remote_put_command,
13021 _("Copy a local file to the remote system."),
13022 &remote_cmdlist);
13023
13024 add_cmd ("get", class_files, remote_get_command,
13025 _("Copy a remote file to the local system."),
13026 &remote_cmdlist);
13027
13028 add_cmd ("delete", class_files, remote_delete_command,
13029 _("Delete a remote file."),
13030 &remote_cmdlist);
13031
2d717e4f
DJ
13032 remote_exec_file = xstrdup ("");
13033 add_setshow_string_noescape_cmd ("exec-file", class_files,
13034 &remote_exec_file, _("\
13035Set the remote pathname for \"run\""), _("\
13036Show the remote pathname for \"run\""), NULL, NULL, NULL,
13037 &remote_set_cmdlist, &remote_show_cmdlist);
13038
c1e36e3e
PA
13039 add_setshow_boolean_cmd ("range-stepping", class_run,
13040 &use_range_stepping, _("\
13041Enable or disable range stepping."), _("\
13042Show whether target-assisted range stepping is enabled."), _("\
13043If on, and the target supports it, when stepping a source line, GDB\n\
13044tells the target to step the corresponding range of addresses itself instead\n\
13045of issuing multiple single-steps. This speeds up source level\n\
13046stepping. If off, GDB always issues single-steps, even if range\n\
13047stepping is supported by the target. The default is on."),
13048 set_range_stepping,
13049 show_range_stepping,
13050 &setlist,
13051 &showlist);
13052
449092f6
CV
13053 /* Eventually initialize fileio. See fileio.c */
13054 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 13055
ba348170 13056 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 13057 special ptids with it set to != 0. */
ba348170
PA
13058 magic_null_ptid = ptid_build (42000, -1, 1);
13059 not_sent_ptid = ptid_build (42000, -2, 1);
13060 any_thread_ptid = ptid_build (42000, 0, 1);
35b1e5cc
SS
13061
13062 target_buf_size = 2048;
13063 target_buf = xmalloc (target_buf_size);
c906108c 13064}
10760264 13065
This page took 2.922022 seconds and 4 git commands to generate.