Update comment for struct type's length field, introduce type_length_units
[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
c9f35b34
KB
2161 /* Return null_ptid when no thread id is found. */
2162 if (p == pp)
2163 {
2164 if (obuf)
2165 *obuf = pp;
2166 return null_ptid;
2167 }
2168
82f73884 2169 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2170 what's in inferior_ptid, unless it's null at this point. If so,
2171 then since there's no way to know the pid of the reported
2172 threads, use the magic number. */
2173 if (ptid_equal (inferior_ptid, null_ptid))
2174 pid = ptid_get_pid (magic_null_ptid);
2175 else
2176 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2177
2178 if (obuf)
2179 *obuf = pp;
ba348170 2180 return ptid_build (pid, tid, 0);
82f73884
PA
2181}
2182
c906108c 2183static int
fba45db2 2184stubhex (int ch)
c906108c
SS
2185{
2186 if (ch >= 'a' && ch <= 'f')
2187 return ch - 'a' + 10;
2188 if (ch >= '0' && ch <= '9')
2189 return ch - '0';
2190 if (ch >= 'A' && ch <= 'F')
2191 return ch - 'A' + 10;
2192 return -1;
2193}
2194
2195static int
fba45db2 2196stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2197{
2198 int nibble;
2199 int retval = 0;
2200
2201 while (fieldlength)
2202 {
2203 nibble = stubhex (*buff++);
2204 retval |= nibble;
2205 fieldlength--;
2206 if (fieldlength)
2207 retval = retval << 4;
2208 }
2209 return retval;
2210}
2211
c906108c 2212static char *
fba45db2 2213unpack_nibble (char *buf, int *val)
c906108c 2214{
b7589f7d 2215 *val = fromhex (*buf++);
c906108c
SS
2216 return buf;
2217}
2218
c906108c 2219static char *
fba45db2 2220unpack_byte (char *buf, int *value)
c906108c
SS
2221{
2222 *value = stub_unpack_int (buf, 2);
2223 return buf + 2;
2224}
2225
2226static char *
fba45db2 2227pack_int (char *buf, int value)
c906108c
SS
2228{
2229 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2230 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2231 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2232 buf = pack_hex_byte (buf, (value & 0xff));
2233 return buf;
2234}
2235
2236static char *
fba45db2 2237unpack_int (char *buf, int *value)
c906108c
SS
2238{
2239 *value = stub_unpack_int (buf, 8);
2240 return buf + 8;
2241}
2242
23860348 2243#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2244static char *pack_string (char *pkt, char *string);
c906108c
SS
2245
2246static char *
fba45db2 2247pack_string (char *pkt, char *string)
c906108c
SS
2248{
2249 char ch;
2250 int len;
2251
2252 len = strlen (string);
2253 if (len > 200)
23860348 2254 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2255 pkt = pack_hex_byte (pkt, len);
2256 while (len-- > 0)
2257 {
2258 ch = *string++;
2259 if ((ch == '\0') || (ch == '#'))
23860348 2260 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2261 *pkt++ = ch;
2262 }
2263 return pkt;
2264}
2265#endif /* 0 (unused) */
2266
2267static char *
fba45db2 2268unpack_string (char *src, char *dest, int length)
c906108c
SS
2269{
2270 while (length--)
2271 *dest++ = *src++;
2272 *dest = '\0';
2273 return src;
2274}
2275
2276static char *
fba45db2 2277pack_threadid (char *pkt, threadref *id)
c906108c
SS
2278{
2279 char *limit;
2280 unsigned char *altid;
2281
2282 altid = (unsigned char *) id;
2283 limit = pkt + BUF_THREAD_ID_SIZE;
2284 while (pkt < limit)
2285 pkt = pack_hex_byte (pkt, *altid++);
2286 return pkt;
2287}
2288
2289
2290static char *
fba45db2 2291unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2292{
2293 char *altref;
2294 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2295 int x, y;
2296
2297 altref = (char *) id;
2298
2299 while (inbuf < limit)
2300 {
2301 x = stubhex (*inbuf++);
2302 y = stubhex (*inbuf++);
2303 *altref++ = (x << 4) | y;
2304 }
2305 return inbuf;
2306}
2307
2308/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2309 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2310 to use 64bit thread references internally. This is an adapter
2311 function. */
2312
2313void
fba45db2 2314int_to_threadref (threadref *id, int value)
c906108c
SS
2315{
2316 unsigned char *scan;
2317
2318 scan = (unsigned char *) id;
2319 {
2320 int i = 4;
2321 while (i--)
2322 *scan++ = 0;
2323 }
2324 *scan++ = (value >> 24) & 0xff;
2325 *scan++ = (value >> 16) & 0xff;
2326 *scan++ = (value >> 8) & 0xff;
2327 *scan++ = (value & 0xff);
2328}
2329
2330static int
fba45db2 2331threadref_to_int (threadref *ref)
c906108c
SS
2332{
2333 int i, value = 0;
2334 unsigned char *scan;
2335
cfd77fa1 2336 scan = *ref;
c906108c
SS
2337 scan += 4;
2338 i = 4;
2339 while (i-- > 0)
2340 value = (value << 8) | ((*scan++) & 0xff);
2341 return value;
2342}
2343
2344static void
fba45db2 2345copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2346{
2347 int i;
2348 unsigned char *csrc, *cdest;
2349
2350 csrc = (unsigned char *) src;
2351 cdest = (unsigned char *) dest;
2352 i = 8;
2353 while (i--)
2354 *cdest++ = *csrc++;
2355}
2356
2357static int
fba45db2 2358threadmatch (threadref *dest, threadref *src)
c906108c 2359{
23860348 2360 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2361#if 0
2362 unsigned char *srcp, *destp;
2363 int i, result;
2364 srcp = (char *) src;
2365 destp = (char *) dest;
2366
2367 result = 1;
2368 while (i-- > 0)
2369 result &= (*srcp++ == *destp++) ? 1 : 0;
2370 return result;
2371#endif
2372 return 1;
2373}
2374
2375/*
c5aa993b
JM
2376 threadid:1, # always request threadid
2377 context_exists:2,
2378 display:4,
2379 unique_name:8,
2380 more_display:16
2381 */
c906108c
SS
2382
2383/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2384
2385static char *
fba45db2 2386pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2387{
23860348
MS
2388 *pkt++ = 'q'; /* Info Query */
2389 *pkt++ = 'P'; /* process or thread info */
2390 pkt = pack_int (pkt, mode); /* mode */
c906108c 2391 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2392 *pkt = '\0'; /* terminate */
c906108c
SS
2393 return pkt;
2394}
2395
23860348 2396/* These values tag the fields in a thread info response packet. */
c906108c 2397/* Tagging the fields allows us to request specific fields and to
23860348 2398 add more fields as time goes by. */
c906108c 2399
23860348 2400#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2401#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2402 fetch registers and its stack? */
c5aa993b 2403#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2404#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2405#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2406 the process. */
c906108c
SS
2407
2408static int
fba45db2
KB
2409remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2410 struct gdb_ext_thread_info *info)
c906108c 2411{
d01949b6 2412 struct remote_state *rs = get_remote_state ();
c906108c 2413 int mask, length;
cfd77fa1 2414 int tag;
c906108c 2415 threadref ref;
6d820c5c 2416 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
2417 int retval = 1;
2418
23860348 2419 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
2420 info->active = 0;
2421 info->display[0] = '\0';
2422 info->shortname[0] = '\0';
2423 info->more_display[0] = '\0';
2424
23860348
MS
2425 /* Assume the characters indicating the packet type have been
2426 stripped. */
c906108c
SS
2427 pkt = unpack_int (pkt, &mask); /* arg mask */
2428 pkt = unpack_threadid (pkt, &ref);
2429
2430 if (mask == 0)
8a3fe4f8 2431 warning (_("Incomplete response to threadinfo request."));
c906108c 2432 if (!threadmatch (&ref, expectedref))
23860348 2433 { /* This is an answer to a different request. */
8a3fe4f8 2434 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
2435 return 0;
2436 }
2437 copy_threadref (&info->threadid, &ref);
2438
23860348 2439 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 2440
23860348
MS
2441 /* Packets are terminated with nulls. */
2442 while ((pkt < limit) && mask && *pkt)
c906108c
SS
2443 {
2444 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
2445 pkt = unpack_byte (pkt, &length); /* length */
2446 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 2447 {
8a3fe4f8 2448 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
2449 retval = 0;
2450 break;
2451 }
2452 if (tag == TAG_THREADID)
2453 {
2454 if (length != 16)
2455 {
8a3fe4f8 2456 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
2457 retval = 0;
2458 break;
2459 }
2460 pkt = unpack_threadid (pkt, &ref);
2461 mask = mask & ~TAG_THREADID;
2462 continue;
2463 }
2464 if (tag == TAG_EXISTS)
2465 {
2466 info->active = stub_unpack_int (pkt, length);
2467 pkt += length;
2468 mask = mask & ~(TAG_EXISTS);
2469 if (length > 8)
2470 {
8a3fe4f8 2471 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
2472 retval = 0;
2473 break;
2474 }
2475 continue;
2476 }
2477 if (tag == TAG_THREADNAME)
2478 {
2479 pkt = unpack_string (pkt, &info->shortname[0], length);
2480 mask = mask & ~TAG_THREADNAME;
2481 continue;
2482 }
2483 if (tag == TAG_DISPLAY)
2484 {
2485 pkt = unpack_string (pkt, &info->display[0], length);
2486 mask = mask & ~TAG_DISPLAY;
2487 continue;
2488 }
2489 if (tag == TAG_MOREDISPLAY)
2490 {
2491 pkt = unpack_string (pkt, &info->more_display[0], length);
2492 mask = mask & ~TAG_MOREDISPLAY;
2493 continue;
2494 }
8a3fe4f8 2495 warning (_("ERROR RMT: unknown thread info tag."));
23860348 2496 break; /* Not a tag we know about. */
c906108c
SS
2497 }
2498 return retval;
2499}
2500
2501static int
fba45db2
KB
2502remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2503 struct gdb_ext_thread_info *info)
c906108c 2504{
d01949b6 2505 struct remote_state *rs = get_remote_state ();
c906108c 2506 int result;
c906108c 2507
2e9f7625
DJ
2508 pack_threadinfo_request (rs->buf, fieldset, threadid);
2509 putpkt (rs->buf);
6d820c5c 2510 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
2511
2512 if (rs->buf[0] == '\0')
2513 return 0;
2514
2e9f7625 2515 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 2516 threadid, info);
c906108c
SS
2517 return result;
2518}
2519
c906108c
SS
2520/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2521
2522static char *
fba45db2
KB
2523pack_threadlist_request (char *pkt, int startflag, int threadcount,
2524 threadref *nextthread)
c906108c
SS
2525{
2526 *pkt++ = 'q'; /* info query packet */
2527 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 2528 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
2529 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2530 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2531 *pkt = '\0';
2532 return pkt;
2533}
2534
2535/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2536
2537static int
fba45db2
KB
2538parse_threadlist_response (char *pkt, int result_limit,
2539 threadref *original_echo, threadref *resultlist,
2540 int *doneflag)
c906108c 2541{
d01949b6 2542 struct remote_state *rs = get_remote_state ();
c906108c
SS
2543 char *limit;
2544 int count, resultcount, done;
2545
2546 resultcount = 0;
2547 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 2548 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 2549 /* done parse past here */
c906108c
SS
2550 pkt = unpack_byte (pkt, &count); /* count field */
2551 pkt = unpack_nibble (pkt, &done);
2552 /* The first threadid is the argument threadid. */
2553 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2554 while ((count-- > 0) && (pkt < limit))
2555 {
2556 pkt = unpack_threadid (pkt, resultlist++);
2557 if (resultcount++ >= result_limit)
2558 break;
2559 }
2560 if (doneflag)
2561 *doneflag = done;
2562 return resultcount;
2563}
2564
6dc54d91
PA
2565/* Fetch the next batch of threads from the remote. Returns -1 if the
2566 qL packet is not supported, 0 on error and 1 on success. */
2567
c906108c 2568static int
fba45db2
KB
2569remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2570 int *done, int *result_count, threadref *threadlist)
c906108c 2571{
d01949b6 2572 struct remote_state *rs = get_remote_state ();
c906108c
SS
2573 int result = 1;
2574
23860348 2575 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
2576 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2577 >= get_remote_packet_size ())
ea9c271d 2578 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 2579
6d820c5c
DJ
2580 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2581 putpkt (rs->buf);
2582 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 2583 if (*rs->buf == '\0')
6dc54d91
PA
2584 {
2585 /* Packet not supported. */
2586 return -1;
2587 }
2588
2589 *result_count =
2590 parse_threadlist_response (rs->buf + 2, result_limit,
2591 &rs->echo_nextthread, threadlist, done);
c906108c 2592
0d031856 2593 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 2594 {
23860348
MS
2595 /* FIXME: This is a good reason to drop the packet. */
2596 /* Possably, there is a duplicate response. */
c906108c
SS
2597 /* Possabilities :
2598 retransmit immediatly - race conditions
2599 retransmit after timeout - yes
2600 exit
2601 wait for packet, then exit
2602 */
8a3fe4f8 2603 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 2604 return 0; /* I choose simply exiting. */
c906108c
SS
2605 }
2606 if (*result_count <= 0)
2607 {
2608 if (*done != 1)
2609 {
8a3fe4f8 2610 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
2611 result = 0;
2612 }
2613 return result; /* break; */
2614 }
2615 if (*result_count > result_limit)
2616 {
2617 *result_count = 0;
8a3fe4f8 2618 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
2619 return 0;
2620 }
2621 return result;
2622}
2623
6dc54d91
PA
2624/* Fetch the list of remote threads, with the qL packet, and call
2625 STEPFUNCTION for each thread found. Stops iterating and returns 1
2626 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2627 STEPFUNCTION returns false. If the packet is not supported,
2628 returns -1. */
c906108c 2629
c906108c 2630static int
fba45db2
KB
2631remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2632 int looplimit)
c906108c 2633{
0d031856 2634 struct remote_state *rs = get_remote_state ();
c906108c
SS
2635 int done, i, result_count;
2636 int startflag = 1;
2637 int result = 1;
2638 int loopcount = 0;
c906108c
SS
2639
2640 done = 0;
2641 while (!done)
2642 {
2643 if (loopcount++ > looplimit)
2644 {
2645 result = 0;
8a3fe4f8 2646 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
2647 break;
2648 }
6dc54d91
PA
2649 result = remote_get_threadlist (startflag, &rs->nextthread,
2650 MAXTHREADLISTRESULTS,
2651 &done, &result_count,
2652 rs->resultthreadlist);
2653 if (result <= 0)
2654 break;
23860348 2655 /* Clear for later iterations. */
c906108c
SS
2656 startflag = 0;
2657 /* Setup to resume next batch of thread references, set nextthread. */
2658 if (result_count >= 1)
0d031856
TT
2659 copy_threadref (&rs->nextthread,
2660 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
2661 i = 0;
2662 while (result_count--)
6dc54d91
PA
2663 {
2664 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2665 {
2666 result = 0;
2667 break;
2668 }
2669 }
c906108c
SS
2670 }
2671 return result;
2672}
2673
6dc54d91
PA
2674/* A thread found on the remote target. */
2675
2676typedef struct thread_item
2677{
2678 /* The thread's PTID. */
2679 ptid_t ptid;
2680
2681 /* The thread's extra info. May be NULL. */
2682 char *extra;
2683
2684 /* The core the thread was running on. -1 if not known. */
2685 int core;
2686} thread_item_t;
2687DEF_VEC_O(thread_item_t);
2688
2689/* Context passed around to the various methods listing remote
2690 threads. As new threads are found, they're added to the ITEMS
2691 vector. */
2692
2693struct threads_listing_context
2694{
2695 /* The threads found on the remote target. */
2696 VEC (thread_item_t) *items;
2697};
2698
80134cf5
PA
2699/* Discard the contents of the constructed thread listing context. */
2700
2701static void
2702clear_threads_listing_context (void *p)
2703{
2704 struct threads_listing_context *context = p;
2705 int i;
2706 struct thread_item *item;
2707
2708 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2709 xfree (item->extra);
2710
2711 VEC_free (thread_item_t, context->items);
2712}
2713
cbb8991c
DB
2714/* Remove the thread specified as the related_pid field of WS
2715 from the CONTEXT list. */
2716
2717static void
2718threads_listing_context_remove (struct target_waitstatus *ws,
2719 struct threads_listing_context *context)
2720{
2721 struct thread_item *item;
2722 int i;
2723 ptid_t child_ptid = ws->value.related_pid;
2724
2725 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2726 {
2727 if (ptid_equal (item->ptid, child_ptid))
2728 {
2729 VEC_ordered_remove (thread_item_t, context->items, i);
2730 break;
2731 }
2732 }
2733}
2734
c906108c 2735static int
6dc54d91 2736remote_newthread_step (threadref *ref, void *data)
c906108c 2737{
6dc54d91
PA
2738 struct threads_listing_context *context = data;
2739 struct thread_item item;
79d7f229 2740 int pid = ptid_get_pid (inferior_ptid);
39f77062 2741
6dc54d91
PA
2742 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
2743 item.core = -1;
2744 item.extra = NULL;
2745
2746 VEC_safe_push (thread_item_t, context->items, &item);
2747
c906108c
SS
2748 return 1; /* continue iterator */
2749}
2750
2751#define CRAZY_MAX_THREADS 1000
2752
39f77062
KB
2753static ptid_t
2754remote_current_thread (ptid_t oldpid)
c906108c 2755{
d01949b6 2756 struct remote_state *rs = get_remote_state ();
c906108c
SS
2757
2758 putpkt ("qC");
6d820c5c 2759 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2760 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
c9f35b34
KB
2761 {
2762 char *obuf;
2763 ptid_t result;
2764
2765 result = read_ptid (&rs->buf[2], &obuf);
2766 if (*obuf != '\0' && remote_debug)
2767 fprintf_unfiltered (gdb_stdlog,
2768 "warning: garbage in qC reply\n");
2769
2770 return result;
2771 }
c906108c
SS
2772 else
2773 return oldpid;
2774}
2775
6dc54d91 2776/* List remote threads using the deprecated qL packet. */
cce74817 2777
6dc54d91
PA
2778static int
2779remote_get_threads_with_ql (struct target_ops *ops,
2780 struct threads_listing_context *context)
c906108c 2781{
6dc54d91
PA
2782 if (remote_threadlist_iterator (remote_newthread_step, context,
2783 CRAZY_MAX_THREADS) >= 0)
2784 return 1;
2785
2786 return 0;
c906108c
SS
2787}
2788
dc146f7c
VP
2789#if defined(HAVE_LIBEXPAT)
2790
dc146f7c
VP
2791static void
2792start_thread (struct gdb_xml_parser *parser,
2793 const struct gdb_xml_element *element,
2794 void *user_data, VEC(gdb_xml_value_s) *attributes)
2795{
6dc54d91 2796 struct threads_listing_context *data = user_data;
dc146f7c
VP
2797
2798 struct thread_item item;
2799 char *id;
3d2c1d41 2800 struct gdb_xml_value *attr;
dc146f7c 2801
3d2c1d41 2802 id = xml_find_attribute (attributes, "id")->value;
dc146f7c
VP
2803 item.ptid = read_ptid (id, NULL);
2804
3d2c1d41
PA
2805 attr = xml_find_attribute (attributes, "core");
2806 if (attr != NULL)
2807 item.core = *(ULONGEST *) attr->value;
dc146f7c
VP
2808 else
2809 item.core = -1;
2810
2811 item.extra = 0;
2812
2813 VEC_safe_push (thread_item_t, data->items, &item);
2814}
2815
2816static void
2817end_thread (struct gdb_xml_parser *parser,
2818 const struct gdb_xml_element *element,
2819 void *user_data, const char *body_text)
2820{
6dc54d91 2821 struct threads_listing_context *data = user_data;
dc146f7c
VP
2822
2823 if (body_text && *body_text)
2ae2a0b7 2824 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
dc146f7c
VP
2825}
2826
2827const struct gdb_xml_attribute thread_attributes[] = {
2828 { "id", GDB_XML_AF_NONE, NULL, NULL },
2829 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2830 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2831};
2832
2833const struct gdb_xml_element thread_children[] = {
2834 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2835};
2836
2837const struct gdb_xml_element threads_children[] = {
2838 { "thread", thread_attributes, thread_children,
2839 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2840 start_thread, end_thread },
2841 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2842};
2843
2844const struct gdb_xml_element threads_elements[] = {
2845 { "threads", NULL, threads_children,
2846 GDB_XML_EF_NONE, NULL, NULL },
2847 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2848};
2849
2850#endif
2851
6dc54d91 2852/* List remote threads using qXfer:threads:read. */
9d1f7ab2 2853
6dc54d91
PA
2854static int
2855remote_get_threads_with_qxfer (struct target_ops *ops,
2856 struct threads_listing_context *context)
0f71a2f6 2857{
dc146f7c 2858#if defined(HAVE_LIBEXPAT)
4082afcc 2859 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 2860 {
6dc54d91 2861 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
dc146f7c 2862 struct cleanup *back_to = make_cleanup (xfree, xml);
efc0eabd 2863
6dc54d91 2864 if (xml != NULL && *xml != '\0')
dc146f7c 2865 {
6dc54d91
PA
2866 gdb_xml_parse_quick (_("threads"), "threads.dtd",
2867 threads_elements, xml, context);
dc146f7c
VP
2868 }
2869
2870 do_cleanups (back_to);
6dc54d91 2871 return 1;
dc146f7c
VP
2872 }
2873#endif
2874
6dc54d91
PA
2875 return 0;
2876}
2877
2878/* List remote threads using qfThreadInfo/qsThreadInfo. */
2879
2880static int
2881remote_get_threads_with_qthreadinfo (struct target_ops *ops,
2882 struct threads_listing_context *context)
2883{
2884 struct remote_state *rs = get_remote_state ();
2885
b80fafe3 2886 if (rs->use_threadinfo_query)
9d1f7ab2 2887 {
6dc54d91
PA
2888 char *bufp;
2889
9d1f7ab2 2890 putpkt ("qfThreadInfo");
6d820c5c 2891 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2892 bufp = rs->buf;
9d1f7ab2 2893 if (bufp[0] != '\0') /* q packet recognized */
802188a7 2894 {
9d1f7ab2
MS
2895 while (*bufp++ == 'm') /* reply contains one or more TID */
2896 {
2897 do
2898 {
6dc54d91
PA
2899 struct thread_item item;
2900
2901 item.ptid = read_ptid (bufp, &bufp);
2902 item.core = -1;
2903 item.extra = NULL;
2904
2905 VEC_safe_push (thread_item_t, context->items, &item);
9d1f7ab2
MS
2906 }
2907 while (*bufp++ == ','); /* comma-separated list */
2908 putpkt ("qsThreadInfo");
6d820c5c 2909 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 2910 bufp = rs->buf;
9d1f7ab2 2911 }
6dc54d91
PA
2912 return 1;
2913 }
2914 else
2915 {
2916 /* Packet not recognized. */
2917 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
2918 }
2919 }
2920
6dc54d91
PA
2921 return 0;
2922}
2923
e8032dde 2924/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
2925 targets. */
2926
2927static void
e8032dde 2928remote_update_thread_list (struct target_ops *ops)
6dc54d91
PA
2929{
2930 struct remote_state *rs = get_remote_state ();
2931 struct threads_listing_context context;
2932 struct cleanup *old_chain;
ab970af1 2933 int got_list = 0;
e8032dde 2934
6dc54d91
PA
2935 context.items = NULL;
2936 old_chain = make_cleanup (clear_threads_listing_context, &context);
2937
2938 /* We have a few different mechanisms to fetch the thread list. Try
2939 them all, starting with the most preferred one first, falling
2940 back to older methods. */
2941 if (remote_get_threads_with_qxfer (ops, &context)
2942 || remote_get_threads_with_qthreadinfo (ops, &context)
2943 || remote_get_threads_with_ql (ops, &context))
2944 {
2945 int i;
2946 struct thread_item *item;
ab970af1
PA
2947 struct thread_info *tp, *tmp;
2948
2949 got_list = 1;
2950
7d1a114c
PA
2951 if (VEC_empty (thread_item_t, context.items)
2952 && remote_thread_always_alive (ops, inferior_ptid))
2953 {
2954 /* Some targets don't really support threads, but still
2955 reply an (empty) thread list in response to the thread
2956 listing packets, instead of replying "packet not
2957 supported". Exit early so we don't delete the main
2958 thread. */
2959 do_cleanups (old_chain);
2960 return;
2961 }
2962
ab970af1
PA
2963 /* CONTEXT now holds the current thread list on the remote
2964 target end. Delete GDB-side threads no longer found on the
2965 target. */
8a06aea7 2966 ALL_THREADS_SAFE (tp, tmp)
cbb8991c 2967 {
ab970af1
PA
2968 for (i = 0;
2969 VEC_iterate (thread_item_t, context.items, i, item);
2970 ++i)
2971 {
2972 if (ptid_equal (item->ptid, tp->ptid))
2973 break;
2974 }
2975
2976 if (i == VEC_length (thread_item_t, context.items))
2977 {
2978 /* Not found. */
2979 delete_thread (tp->ptid);
2980 }
cbb8991c
DB
2981 }
2982
2983 /* Remove any unreported fork child threads from CONTEXT so
2984 that we don't interfere with follow fork, which is where
2985 creation of such threads is handled. */
2986 remove_new_fork_children (&context);
74531fed 2987
ab970af1 2988 /* And now add threads we don't know about yet to our list. */
6dc54d91
PA
2989 for (i = 0;
2990 VEC_iterate (thread_item_t, context.items, i, item);
2991 ++i)
2992 {
2993 if (!ptid_equal (item->ptid, null_ptid))
2994 {
2995 struct private_thread_info *info;
2996 /* In non-stop mode, we assume new found threads are
2997 running until proven otherwise with a stop reply. In
2998 all-stop, we can only get here if all threads are
2999 stopped. */
3000 int running = non_stop ? 1 : 0;
3001
3002 remote_notice_new_inferior (item->ptid, running);
3003
3004 info = demand_private_info (item->ptid);
3005 info->core = item->core;
3006 info->extra = item->extra;
3007 item->extra = NULL;
3008 }
3009 }
3010 }
3011
ab970af1
PA
3012 if (!got_list)
3013 {
3014 /* If no thread listing method is supported, then query whether
3015 each known thread is alive, one by one, with the T packet.
3016 If the target doesn't support threads at all, then this is a
3017 no-op. See remote_thread_alive. */
3018 prune_threads ();
3019 }
3020
6dc54d91 3021 do_cleanups (old_chain);
9d1f7ab2
MS
3022}
3023
802188a7 3024/*
9d1f7ab2
MS
3025 * Collect a descriptive string about the given thread.
3026 * The target may say anything it wants to about the thread
3027 * (typically info about its blocked / runnable state, name, etc.).
3028 * This string will appear in the info threads display.
802188a7 3029 *
9d1f7ab2
MS
3030 * Optional: targets are not required to implement this function.
3031 */
3032
3033static char *
c15906d8 3034remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
9d1f7ab2 3035{
d01949b6 3036 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
3037 int result;
3038 int set;
3039 threadref id;
3040 struct gdb_ext_thread_info threadinfo;
23860348 3041 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
3042 int n = 0; /* position in display_buf */
3043
5d93a237 3044 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 3045 internal_error (__FILE__, __LINE__,
e2e0b3e5 3046 _("remote_threads_extra_info"));
9d1f7ab2 3047
60e569b9 3048 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 3049 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
3050 /* This is the main thread which was added by GDB. The remote
3051 server doesn't know about it. */
3052 return NULL;
3053
4082afcc 3054 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
3055 {
3056 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 3057
fe978cb0
PA
3058 if (info && info->priv)
3059 return info->priv->extra;
dc146f7c
VP
3060 else
3061 return NULL;
3062 }
3063
b80fafe3 3064 if (rs->use_threadextra_query)
9d1f7ab2 3065 {
82f73884
PA
3066 char *b = rs->buf;
3067 char *endb = rs->buf + get_remote_packet_size ();
3068
3069 xsnprintf (b, endb - b, "qThreadExtraInfo,");
3070 b += strlen (b);
3071 write_ptid (b, endb, tp->ptid);
3072
2e9f7625 3073 putpkt (rs->buf);
6d820c5c 3074 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3075 if (rs->buf[0] != 0)
9d1f7ab2 3076 {
2e9f7625
DJ
3077 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
3078 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 3079 display_buf [result] = '\0';
9d1f7ab2
MS
3080 return display_buf;
3081 }
0f71a2f6 3082 }
9d1f7ab2
MS
3083
3084 /* If the above query fails, fall back to the old method. */
b80fafe3 3085 rs->use_threadextra_query = 0;
9d1f7ab2
MS
3086 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3087 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 3088 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
3089 if (remote_get_threadinfo (&id, set, &threadinfo))
3090 if (threadinfo.active)
0f71a2f6 3091 {
9d1f7ab2 3092 if (*threadinfo.shortname)
2bc416ba 3093 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 3094 " Name: %s,", threadinfo.shortname);
9d1f7ab2 3095 if (*threadinfo.display)
2bc416ba 3096 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3097 " State: %s,", threadinfo.display);
9d1f7ab2 3098 if (*threadinfo.more_display)
2bc416ba 3099 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 3100 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
3101
3102 if (n > 0)
c5aa993b 3103 {
23860348 3104 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
3105 if (',' == display_buf[n-1])
3106 display_buf[n-1] = ' ';
3107 return display_buf;
c5aa993b 3108 }
0f71a2f6 3109 }
9d1f7ab2 3110 return NULL;
0f71a2f6 3111}
c906108c 3112\f
c5aa993b 3113
0fb4aa4b 3114static int
61fc905d 3115remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
0fb4aa4b
PA
3116 struct static_tracepoint_marker *marker)
3117{
3118 struct remote_state *rs = get_remote_state ();
3119 char *p = rs->buf;
3120
bba74b36 3121 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3122 p += strlen (p);
3123 p += hexnumstr (p, addr);
3124 putpkt (rs->buf);
3125 getpkt (&rs->buf, &rs->buf_size, 0);
3126 p = rs->buf;
3127
3128 if (*p == 'E')
3129 error (_("Remote failure reply: %s"), p);
3130
3131 if (*p++ == 'm')
3132 {
3133 parse_static_tracepoint_marker_definition (p, &p, marker);
3134 return 1;
3135 }
3136
3137 return 0;
3138}
3139
0fb4aa4b 3140static VEC(static_tracepoint_marker_p) *
c686c57f
TT
3141remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3142 const char *strid)
0fb4aa4b
PA
3143{
3144 struct remote_state *rs = get_remote_state ();
3145 VEC(static_tracepoint_marker_p) *markers = NULL;
3146 struct static_tracepoint_marker *marker = NULL;
3147 struct cleanup *old_chain;
3148 char *p;
3149
3150 /* Ask for a first packet of static tracepoint marker
3151 definition. */
3152 putpkt ("qTfSTM");
3153 getpkt (&rs->buf, &rs->buf_size, 0);
3154 p = rs->buf;
3155 if (*p == 'E')
3156 error (_("Remote failure reply: %s"), p);
3157
3158 old_chain = make_cleanup (free_current_marker, &marker);
3159
3160 while (*p++ == 'm')
3161 {
3162 if (marker == NULL)
3163 marker = XCNEW (struct static_tracepoint_marker);
3164
3165 do
3166 {
3167 parse_static_tracepoint_marker_definition (p, &p, marker);
3168
3169 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3170 {
3171 VEC_safe_push (static_tracepoint_marker_p,
3172 markers, marker);
3173 marker = NULL;
3174 }
3175 else
3176 {
3177 release_static_tracepoint_marker (marker);
3178 memset (marker, 0, sizeof (*marker));
3179 }
3180 }
3181 while (*p++ == ','); /* comma-separated list */
3182 /* Ask for another packet of static tracepoint definition. */
3183 putpkt ("qTsSTM");
3184 getpkt (&rs->buf, &rs->buf_size, 0);
3185 p = rs->buf;
3186 }
3187
3188 do_cleanups (old_chain);
3189 return markers;
3190}
3191
3192\f
10760264
JB
3193/* Implement the to_get_ada_task_ptid function for the remote targets. */
3194
3195static ptid_t
1e6b91a4 3196remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
10760264 3197{
ba348170 3198 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3199}
3200\f
3201
24b06219 3202/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3203
3204static void
fba45db2 3205extended_remote_restart (void)
c906108c 3206{
d01949b6 3207 struct remote_state *rs = get_remote_state ();
c906108c
SS
3208
3209 /* Send the restart command; for reasons I don't understand the
3210 remote side really expects a number after the "R". */
ea9c271d 3211 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3212 putpkt (rs->buf);
c906108c 3213
ad9a8f3f 3214 remote_fileio_reset ();
c906108c
SS
3215}
3216\f
3217/* Clean up connection to a remote debugger. */
3218
c906108c 3219static void
de90e03d 3220remote_close (struct target_ops *self)
c906108c 3221{
5d93a237
TT
3222 struct remote_state *rs = get_remote_state ();
3223
3224 if (rs->remote_desc == NULL)
d3fd5342
PA
3225 return; /* already closed */
3226
3227 /* Make sure we leave stdin registered in the event loop, and we
3228 don't leave the async SIGINT signal handler installed. */
e3594fd1 3229 remote_terminal_ours (self);
ce5ce7ed 3230
5d93a237
TT
3231 serial_close (rs->remote_desc);
3232 rs->remote_desc = NULL;
ce5ce7ed
PA
3233
3234 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3235 of all the inferiors and their threads we were controlling.
3236 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3237 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3238 inferior_ptid = null_ptid;
f67fd822 3239 discard_all_inferiors ();
ce5ce7ed 3240
f48ff2a7
YQ
3241 /* We are closing the remote target, so we should discard
3242 everything of this target. */
bcc75809 3243 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3244
3245 if (remote_async_inferior_event_token)
3246 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3247
5965e028 3248 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3249
3250 trace_reset_local_state ();
c906108c
SS
3251}
3252
23860348 3253/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3254
3255static void
fba45db2 3256get_offsets (void)
c906108c 3257{
d01949b6 3258 struct remote_state *rs = get_remote_state ();
2e9f7625 3259 char *buf;
085dd6e6 3260 char *ptr;
31d99776
DJ
3261 int lose, num_segments = 0, do_sections, do_segments;
3262 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3263 struct section_offsets *offs;
31d99776
DJ
3264 struct symfile_segment_data *data;
3265
3266 if (symfile_objfile == NULL)
3267 return;
c906108c
SS
3268
3269 putpkt ("qOffsets");
6d820c5c 3270 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3271 buf = rs->buf;
c906108c
SS
3272
3273 if (buf[0] == '\000')
3274 return; /* Return silently. Stub doesn't support
23860348 3275 this command. */
c906108c
SS
3276 if (buf[0] == 'E')
3277 {
8a3fe4f8 3278 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3279 return;
3280 }
3281
3282 /* Pick up each field in turn. This used to be done with scanf, but
3283 scanf will make trouble if CORE_ADDR size doesn't match
3284 conversion directives correctly. The following code will work
3285 with any size of CORE_ADDR. */
3286 text_addr = data_addr = bss_addr = 0;
3287 ptr = buf;
3288 lose = 0;
3289
61012eef 3290 if (startswith (ptr, "Text="))
c906108c
SS
3291 {
3292 ptr += 5;
3293 /* Don't use strtol, could lose on big values. */
3294 while (*ptr && *ptr != ';')
3295 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3296
61012eef 3297 if (startswith (ptr, ";Data="))
31d99776
DJ
3298 {
3299 ptr += 6;
3300 while (*ptr && *ptr != ';')
3301 data_addr = (data_addr << 4) + fromhex (*ptr++);
3302 }
3303 else
3304 lose = 1;
3305
61012eef 3306 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3307 {
3308 ptr += 5;
3309 while (*ptr && *ptr != ';')
3310 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3311
31d99776
DJ
3312 if (bss_addr != data_addr)
3313 warning (_("Target reported unsupported offsets: %s"), buf);
3314 }
3315 else
3316 lose = 1;
3317 }
61012eef 3318 else if (startswith (ptr, "TextSeg="))
c906108c 3319 {
31d99776
DJ
3320 ptr += 8;
3321 /* Don't use strtol, could lose on big values. */
c906108c 3322 while (*ptr && *ptr != ';')
31d99776
DJ
3323 text_addr = (text_addr << 4) + fromhex (*ptr++);
3324 num_segments = 1;
3325
61012eef 3326 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3327 {
3328 ptr += 9;
3329 while (*ptr && *ptr != ';')
3330 data_addr = (data_addr << 4) + fromhex (*ptr++);
3331 num_segments++;
3332 }
c906108c
SS
3333 }
3334 else
3335 lose = 1;
3336
3337 if (lose)
8a3fe4f8 3338 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3339 else if (*ptr != '\0')
3340 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3341
802188a7 3342 offs = ((struct section_offsets *)
a39a16c4 3343 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3344 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3345 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3346
31d99776
DJ
3347 data = get_symfile_segment_data (symfile_objfile->obfd);
3348 do_segments = (data != NULL);
3349 do_sections = num_segments == 0;
c906108c 3350
28c32713 3351 if (num_segments > 0)
31d99776 3352 {
31d99776
DJ
3353 segments[0] = text_addr;
3354 segments[1] = data_addr;
3355 }
28c32713
JB
3356 /* If we have two segments, we can still try to relocate everything
3357 by assuming that the .text and .data offsets apply to the whole
3358 text and data segments. Convert the offsets given in the packet
3359 to base addresses for symfile_map_offsets_to_segments. */
3360 else if (data && data->num_segments == 2)
3361 {
3362 segments[0] = data->segment_bases[0] + text_addr;
3363 segments[1] = data->segment_bases[1] + data_addr;
3364 num_segments = 2;
3365 }
8d385431
DJ
3366 /* If the object file has only one segment, assume that it is text
3367 rather than data; main programs with no writable data are rare,
3368 but programs with no code are useless. Of course the code might
3369 have ended up in the data segment... to detect that we would need
3370 the permissions here. */
3371 else if (data && data->num_segments == 1)
3372 {
3373 segments[0] = data->segment_bases[0] + text_addr;
3374 num_segments = 1;
3375 }
28c32713
JB
3376 /* There's no way to relocate by segment. */
3377 else
3378 do_segments = 0;
31d99776
DJ
3379
3380 if (do_segments)
3381 {
3382 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3383 offs, num_segments, segments);
3384
3385 if (ret == 0 && !do_sections)
3e43a32a
MS
3386 error (_("Can not handle qOffsets TextSeg "
3387 "response with this symbol file"));
31d99776
DJ
3388
3389 if (ret > 0)
3390 do_sections = 0;
3391 }
c906108c 3392
9ef895d6
DJ
3393 if (data)
3394 free_symfile_segment_data (data);
31d99776
DJ
3395
3396 if (do_sections)
3397 {
3398 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3399
3e43a32a
MS
3400 /* This is a temporary kludge to force data and bss to use the
3401 same offsets because that's what nlmconv does now. The real
3402 solution requires changes to the stub and remote.c that I
3403 don't have time to do right now. */
31d99776
DJ
3404
3405 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3406 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3407 }
c906108c
SS
3408
3409 objfile_relocate (symfile_objfile, offs);
3410}
3411
74531fed
PA
3412/* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3413 threads we know are stopped already. This is used during the
3414 initial remote connection in non-stop mode --- threads that are
3415 reported as already being stopped are left stopped. */
3416
3417static int
3418set_stop_requested_callback (struct thread_info *thread, void *data)
3419{
3420 /* If we have a stop reply for this thread, it must be stopped. */
3421 if (peek_stop_reply (thread->ptid))
3422 set_stop_requested (thread->ptid, 1);
3423
3424 return 0;
3425}
3426
9a7071a8
JB
3427/* Send interrupt_sequence to remote target. */
3428static void
eeae04df 3429send_interrupt_sequence (void)
9a7071a8 3430{
5d93a237
TT
3431 struct remote_state *rs = get_remote_state ();
3432
9a7071a8 3433 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3434 remote_serial_write ("\x03", 1);
9a7071a8 3435 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3436 serial_send_break (rs->remote_desc);
9a7071a8
JB
3437 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3438 {
5d93a237 3439 serial_send_break (rs->remote_desc);
c33e31fd 3440 remote_serial_write ("g", 1);
9a7071a8
JB
3441 }
3442 else
3443 internal_error (__FILE__, __LINE__,
3444 _("Invalid value for interrupt_sequence_mode: %s."),
3445 interrupt_sequence_mode);
3446}
3447
3405876a
PA
3448
3449/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3450 and extract the PTID. Returns NULL_PTID if not found. */
3451
3452static ptid_t
3453stop_reply_extract_thread (char *stop_reply)
3454{
3455 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3456 {
3457 char *p;
3458
3459 /* Txx r:val ; r:val (...) */
3460 p = &stop_reply[3];
3461
3462 /* Look for "register" named "thread". */
3463 while (*p != '\0')
3464 {
3465 char *p1;
3466
3467 p1 = strchr (p, ':');
3468 if (p1 == NULL)
3469 return null_ptid;
3470
3471 if (strncmp (p, "thread", p1 - p) == 0)
3472 return read_ptid (++p1, &p);
3473
3474 p1 = strchr (p, ';');
3475 if (p1 == NULL)
3476 return null_ptid;
3477 p1++;
3478
3479 p = p1;
3480 }
3481 }
3482
3483 return null_ptid;
3484}
3485
b7ea362b
PA
3486/* Determine the remote side's current thread. If we have a stop
3487 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3488 "thread" register we can extract the current thread from. If not,
3489 ask the remote which is the current thread with qC. The former
3490 method avoids a roundtrip. */
3491
3492static ptid_t
3493get_current_thread (char *wait_status)
3494{
6a49a997 3495 ptid_t ptid = null_ptid;
b7ea362b
PA
3496
3497 /* Note we don't use remote_parse_stop_reply as that makes use of
3498 the target architecture, which we haven't yet fully determined at
3499 this point. */
3500 if (wait_status != NULL)
3501 ptid = stop_reply_extract_thread (wait_status);
3502 if (ptid_equal (ptid, null_ptid))
3503 ptid = remote_current_thread (inferior_ptid);
3504
3505 return ptid;
3506}
3507
49c62f2e
PA
3508/* Query the remote target for which is the current thread/process,
3509 add it to our tables, and update INFERIOR_PTID. The caller is
3510 responsible for setting the state such that the remote end is ready
3405876a
PA
3511 to return the current thread.
3512
3513 This function is called after handling the '?' or 'vRun' packets,
3514 whose response is a stop reply from which we can also try
3515 extracting the thread. If the target doesn't support the explicit
3516 qC query, we infer the current thread from that stop reply, passed
3517 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
3518
3519static void
3405876a 3520add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
3521{
3522 struct remote_state *rs = get_remote_state ();
3523 int fake_pid_p = 0;
6a49a997 3524 ptid_t ptid;
49c62f2e
PA
3525
3526 inferior_ptid = null_ptid;
3527
b7ea362b
PA
3528 /* Now, if we have thread information, update inferior_ptid. */
3529 ptid = get_current_thread (wait_status);
3405876a 3530
49c62f2e
PA
3531 if (!ptid_equal (ptid, null_ptid))
3532 {
3533 if (!remote_multi_process_p (rs))
3534 fake_pid_p = 1;
3535
3536 inferior_ptid = ptid;
3537 }
3538 else
3539 {
3540 /* Without this, some commands which require an active target
3541 (such as kill) won't work. This variable serves (at least)
3542 double duty as both the pid of the target process (if it has
3543 such), and as a flag indicating that a target is active. */
3544 inferior_ptid = magic_null_ptid;
3545 fake_pid_p = 1;
3546 }
3547
1b6e6f5c 3548 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1, 1);
49c62f2e
PA
3549
3550 /* Add the main thread. */
3551 add_thread_silent (inferior_ptid);
3552}
3553
9cbc821d 3554static void
04bd08de 3555remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
c906108c 3556{
c8d104ad
PA
3557 struct remote_state *rs = get_remote_state ();
3558 struct packet_config *noack_config;
2d717e4f 3559 char *wait_status = NULL;
8621d6a9 3560
23860348 3561 immediate_quit++; /* Allow user to interrupt it. */
522002f9 3562 QUIT;
c906108c 3563
9a7071a8
JB
3564 if (interrupt_on_connect)
3565 send_interrupt_sequence ();
3566
57e12211 3567 /* Ack any packet which the remote side has already sent. */
5d93a237 3568 serial_write (rs->remote_desc, "+", 1);
57e12211 3569
1e51243a
PA
3570 /* Signal other parts that we're going through the initial setup,
3571 and so things may not be stable yet. */
3572 rs->starting_up = 1;
3573
c8d104ad
PA
3574 /* The first packet we send to the target is the optional "supported
3575 packets" request. If the target can answer this, it will tell us
3576 which later probes to skip. */
3577 remote_query_supported ();
3578
d914c394 3579 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 3580 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
c378d69d 3581 remote_set_permissions (target);
d914c394 3582
c8d104ad
PA
3583 /* Next, we possibly activate noack mode.
3584
3585 If the QStartNoAckMode packet configuration is set to AUTO,
3586 enable noack mode if the stub reported a wish for it with
3587 qSupported.
3588
3589 If set to TRUE, then enable noack mode even if the stub didn't
3590 report it in qSupported. If the stub doesn't reply OK, the
3591 session ends with an error.
3592
3593 If FALSE, then don't activate noack mode, regardless of what the
3594 stub claimed should be the default with qSupported. */
3595
3596 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 3597 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
3598 {
3599 putpkt ("QStartNoAckMode");
3600 getpkt (&rs->buf, &rs->buf_size, 0);
3601 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3602 rs->noack_mode = 1;
3603 }
3604
04bd08de 3605 if (extended_p)
5fe04517
PA
3606 {
3607 /* Tell the remote that we are using the extended protocol. */
3608 putpkt ("!");
3609 getpkt (&rs->buf, &rs->buf_size, 0);
3610 }
3611
9b224c5e
PA
3612 /* Let the target know which signals it is allowed to pass down to
3613 the program. */
3614 update_signals_program_target ();
3615
d962ef82
DJ
3616 /* Next, if the target can specify a description, read it. We do
3617 this before anything involving memory or registers. */
3618 target_find_description ();
3619
6c95b8df
PA
3620 /* Next, now that we know something about the target, update the
3621 address spaces in the program spaces. */
3622 update_address_spaces ();
3623
50c71eaf
PA
3624 /* On OSs where the list of libraries is global to all
3625 processes, we fetch them early. */
f5656ead 3626 if (gdbarch_has_global_solist (target_gdbarch ()))
04bd08de 3627 solib_add (NULL, from_tty, target, auto_solib_add);
50c71eaf 3628
74531fed
PA
3629 if (non_stop)
3630 {
4082afcc 3631 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
3632 error (_("Non-stop mode requested, but remote "
3633 "does not support non-stop"));
74531fed
PA
3634
3635 putpkt ("QNonStop:1");
3636 getpkt (&rs->buf, &rs->buf_size, 0);
3637
3638 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3639 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
3640
3641 /* Find about threads and processes the stub is already
3642 controlling. We default to adding them in the running state.
3643 The '?' query below will then tell us about which threads are
3644 stopped. */
e8032dde 3645 remote_update_thread_list (target);
74531fed 3646 }
4082afcc 3647 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
3648 {
3649 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 3650 Request it explicitly. */
74531fed
PA
3651 putpkt ("QNonStop:0");
3652 getpkt (&rs->buf, &rs->buf_size, 0);
3653
3654 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3655 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
3656 }
3657
a0743c90
YQ
3658 /* Upload TSVs regardless of whether the target is running or not. The
3659 remote stub, such as GDBserver, may have some predefined or builtin
3660 TSVs, even if the target is not running. */
8bd200f1 3661 if (remote_get_trace_status (target, current_trace_status ()) != -1)
a0743c90
YQ
3662 {
3663 struct uploaded_tsv *uploaded_tsvs = NULL;
3664
181e3713 3665 remote_upload_trace_state_variables (target, &uploaded_tsvs);
a0743c90
YQ
3666 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3667 }
3668
2d717e4f
DJ
3669 /* Check whether the target is running now. */
3670 putpkt ("?");
3671 getpkt (&rs->buf, &rs->buf_size, 0);
3672
74531fed 3673 if (!non_stop)
2d717e4f 3674 {
e714e1bf
UW
3675 ptid_t ptid;
3676 int fake_pid_p = 0;
3677 struct inferior *inf;
3678
74531fed 3679 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 3680 {
04bd08de 3681 if (!extended_p)
74531fed 3682 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
3683
3684 /* We're connected, but not running. Drop out before we
3685 call start_remote. */
e278ad5b 3686 rs->starting_up = 0;
c35b1492 3687 return;
2d717e4f
DJ
3688 }
3689 else
74531fed 3690 {
74531fed
PA
3691 /* Save the reply for later. */
3692 wait_status = alloca (strlen (rs->buf) + 1);
3693 strcpy (wait_status, rs->buf);
3694 }
3695
b7ea362b 3696 /* Fetch thread list. */
e8032dde 3697 target_update_thread_list ();
b7ea362b 3698
74531fed
PA
3699 /* Let the stub know that we want it to return the thread. */
3700 set_continue_thread (minus_one_ptid);
3701
b7ea362b
PA
3702 if (thread_count () == 0)
3703 {
3704 /* Target has no concept of threads at all. GDB treats
3705 non-threaded target as single-threaded; add a main
3706 thread. */
3707 add_current_inferior_and_thread (wait_status);
3708 }
3709 else
3710 {
3711 /* We have thread information; select the thread the target
3712 says should be current. If we're reconnecting to a
3713 multi-threaded program, this will ideally be the thread
3714 that last reported an event before GDB disconnected. */
3715 inferior_ptid = get_current_thread (wait_status);
3716 if (ptid_equal (inferior_ptid, null_ptid))
3717 {
3718 /* Odd... The target was able to list threads, but not
3719 tell us which thread was current (no "thread"
3720 register in T stop reply?). Just pick the first
3721 thread in the thread list then. */
c9f35b34
KB
3722
3723 if (remote_debug)
3724 fprintf_unfiltered (gdb_stdlog,
3725 "warning: couldn't determine remote "
3726 "current thread; picking first in list.\n");
3727
b7ea362b
PA
3728 inferior_ptid = thread_list->ptid;
3729 }
3730 }
74531fed 3731
6e586cc5
YQ
3732 /* init_wait_for_inferior should be called before get_offsets in order
3733 to manage `inserted' flag in bp loc in a correct state.
3734 breakpoint_init_inferior, called from init_wait_for_inferior, set
3735 `inserted' flag to 0, while before breakpoint_re_set, called from
3736 start_remote, set `inserted' flag to 1. In the initialization of
3737 inferior, breakpoint_init_inferior should be called first, and then
3738 breakpoint_re_set can be called. If this order is broken, state of
3739 `inserted' flag is wrong, and cause some problems on breakpoint
3740 manipulation. */
3741 init_wait_for_inferior ();
3742
74531fed
PA
3743 get_offsets (); /* Get text, data & bss offsets. */
3744
d962ef82
DJ
3745 /* If we could not find a description using qXfer, and we know
3746 how to do it some other way, try again. This is not
3747 supported for non-stop; it could be, but it is tricky if
3748 there are no stopped threads when we connect. */
04bd08de 3749 if (remote_read_description_p (target)
f5656ead 3750 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
3751 {
3752 target_clear_description ();
3753 target_find_description ();
3754 }
3755
74531fed
PA
3756 /* Use the previously fetched status. */
3757 gdb_assert (wait_status != NULL);
3758 strcpy (rs->buf, wait_status);
3759 rs->cached_wait_status = 1;
3760
3761 immediate_quit--;
04bd08de 3762 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
3763 }
3764 else
3765 {
68c97600
PA
3766 /* Clear WFI global state. Do this before finding about new
3767 threads and inferiors, and setting the current inferior.
3768 Otherwise we would clear the proceed status of the current
3769 inferior when we want its stop_soon state to be preserved
3770 (see notice_new_inferior). */
3771 init_wait_for_inferior ();
3772
74531fed
PA
3773 /* In non-stop, we will either get an "OK", meaning that there
3774 are no stopped threads at this time; or, a regular stop
3775 reply. In the latter case, there may be more than one thread
3776 stopped --- we pull them all out using the vStopped
3777 mechanism. */
3778 if (strcmp (rs->buf, "OK") != 0)
3779 {
722247f1 3780 struct notif_client *notif = &notif_client_stop;
2d717e4f 3781
722247f1
YQ
3782 /* remote_notif_get_pending_replies acks this one, and gets
3783 the rest out. */
f48ff2a7 3784 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
3785 = remote_notif_parse (notif, rs->buf);
3786 remote_notif_get_pending_events (notif);
c906108c 3787
74531fed
PA
3788 /* Make sure that threads that were stopped remain
3789 stopped. */
3790 iterate_over_threads (set_stop_requested_callback, NULL);
3791 }
2d717e4f 3792
74531fed 3793 if (target_can_async_p ())
6a3753b3 3794 target_async (1);
c906108c 3795
74531fed
PA
3796 if (thread_count () == 0)
3797 {
04bd08de 3798 if (!extended_p)
74531fed 3799 error (_("The target is not running (try extended-remote?)"));
82f73884 3800
c35b1492
PA
3801 /* We're connected, but not running. Drop out before we
3802 call start_remote. */
e278ad5b 3803 rs->starting_up = 0;
c35b1492
PA
3804 return;
3805 }
74531fed
PA
3806
3807 /* Let the stub know that we want it to return the thread. */
c0a2216e 3808
74531fed
PA
3809 /* Force the stub to choose a thread. */
3810 set_general_thread (null_ptid);
c906108c 3811
74531fed
PA
3812 /* Query it. */
3813 inferior_ptid = remote_current_thread (minus_one_ptid);
3814 if (ptid_equal (inferior_ptid, minus_one_ptid))
3815 error (_("remote didn't report the current thread in non-stop mode"));
c906108c 3816
74531fed
PA
3817 get_offsets (); /* Get text, data & bss offsets. */
3818
3819 /* In non-stop mode, any cached wait status will be stored in
3820 the stop reply queue. */
3821 gdb_assert (wait_status == NULL);
f0223081 3822
2455069d 3823 /* Report all signals during attach/startup. */
94bedb42 3824 remote_pass_signals (target, 0, NULL);
74531fed 3825 }
c8d104ad 3826
c8d104ad
PA
3827 /* If we connected to a live target, do some additional setup. */
3828 if (target_has_execution)
3829 {
f4ccffad 3830 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 3831 remote_check_symbols ();
c8d104ad 3832 }
50c71eaf 3833
d5551862
SS
3834 /* Possibly the target has been engaged in a trace run started
3835 previously; find out where things are at. */
8bd200f1 3836 if (remote_get_trace_status (target, current_trace_status ()) != -1)
d5551862 3837 {
00bf0b85 3838 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 3839
00bf0b85
SS
3840 if (current_trace_status ()->running)
3841 printf_filtered (_("Trace is already running on the target.\n"));
3842
ab6617cc 3843 remote_upload_tracepoints (target, &uploaded_tps);
00bf0b85
SS
3844
3845 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
3846 }
3847
1e51243a
PA
3848 /* The thread and inferior lists are now synchronized with the
3849 target, our symbols have been relocated, and we're merged the
3850 target's tracepoints with ours. We're done with basic start
3851 up. */
3852 rs->starting_up = 0;
3853
a25a5a45
PA
3854 /* Maybe breakpoints are global and need to be inserted now. */
3855 if (breakpoints_should_be_inserted_now ())
50c71eaf 3856 insert_breakpoints ();
c906108c
SS
3857}
3858
3859/* Open a connection to a remote debugger.
3860 NAME is the filename used for communication. */
3861
3862static void
014f9477 3863remote_open (const char *name, int from_tty)
c906108c 3864{
75c99385 3865 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
3866}
3867
c906108c
SS
3868/* Open a connection to a remote debugger using the extended
3869 remote gdb protocol. NAME is the filename used for communication. */
3870
3871static void
014f9477 3872extended_remote_open (const char *name, int from_tty)
c906108c 3873{
75c99385 3874 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
3875}
3876
ca4f7f8b
PA
3877/* Reset all packets back to "unknown support". Called when opening a
3878 new connection to a remote target. */
c906108c 3879
d471ea57 3880static void
ca4f7f8b 3881reset_all_packet_configs_support (void)
d471ea57
AC
3882{
3883 int i;
a744cf53 3884
444abaca 3885 for (i = 0; i < PACKET_MAX; i++)
4082afcc 3886 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
3887}
3888
ca4f7f8b
PA
3889/* Initialize all packet configs. */
3890
3891static void
3892init_all_packet_configs (void)
3893{
3894 int i;
3895
3896 for (i = 0; i < PACKET_MAX; i++)
3897 {
3898 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
3899 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3900 }
3901}
3902
23860348 3903/* Symbol look-up. */
dc8acb97
MS
3904
3905static void
36d25514 3906remote_check_symbols (void)
dc8acb97 3907{
d01949b6 3908 struct remote_state *rs = get_remote_state ();
dc8acb97 3909 char *msg, *reply, *tmp;
3b7344d5 3910 struct bound_minimal_symbol sym;
dc8acb97
MS
3911 int end;
3912
63154eca
PA
3913 /* The remote side has no concept of inferiors that aren't running
3914 yet, it only knows about running processes. If we're connected
3915 but our current inferior is not running, we should not invite the
3916 remote target to request symbol lookups related to its
3917 (unrelated) current process. */
3918 if (!target_has_execution)
3919 return;
3920
4082afcc 3921 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
3922 return;
3923
63154eca
PA
3924 /* Make sure the remote is pointing at the right process. Note
3925 there's no way to select "no process". */
3c9c4b83
PA
3926 set_general_process ();
3927
6d820c5c
DJ
3928 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3929 because we need both at the same time. */
ea9c271d 3930 msg = alloca (get_remote_packet_size ());
6d820c5c 3931
23860348 3932 /* Invite target to request symbol lookups. */
dc8acb97
MS
3933
3934 putpkt ("qSymbol::");
6d820c5c
DJ
3935 getpkt (&rs->buf, &rs->buf_size, 0);
3936 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
2e9f7625 3937 reply = rs->buf;
dc8acb97 3938
61012eef 3939 while (startswith (reply, "qSymbol:"))
dc8acb97 3940 {
77e371c0
TT
3941 struct bound_minimal_symbol sym;
3942
dc8acb97 3943 tmp = &reply[8];
cfd77fa1 3944 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
3945 msg[end] = '\0';
3946 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 3947 if (sym.minsym == NULL)
ea9c271d 3948 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 3949 else
2bbe3cc1 3950 {
f5656ead 3951 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 3952 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
3953
3954 /* If this is a function address, return the start of code
3955 instead of any data function descriptor. */
f5656ead 3956 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
3957 sym_addr,
3958 &current_target);
3959
3960 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 3961 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
3962 }
3963
dc8acb97 3964 putpkt (msg);
6d820c5c 3965 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3966 reply = rs->buf;
dc8acb97
MS
3967 }
3968}
3969
9db8d71f 3970static struct serial *
baa336ce 3971remote_serial_open (const char *name)
9db8d71f
DJ
3972{
3973 static int udp_warning = 0;
3974
3975 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3976 of in ser-tcp.c, because it is the remote protocol assuming that the
3977 serial connection is reliable and not the serial connection promising
3978 to be. */
61012eef 3979 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 3980 {
3e43a32a
MS
3981 warning (_("The remote protocol may be unreliable over UDP.\n"
3982 "Some events may be lost, rendering further debugging "
3983 "impossible."));
9db8d71f
DJ
3984 udp_warning = 1;
3985 }
3986
3987 return serial_open (name);
3988}
3989
d914c394
SS
3990/* Inform the target of our permission settings. The permission flags
3991 work without this, but if the target knows the settings, it can do
3992 a couple things. First, it can add its own check, to catch cases
3993 that somehow manage to get by the permissions checks in target
3994 methods. Second, if the target is wired to disallow particular
3995 settings (for instance, a system in the field that is not set up to
3996 be able to stop at a breakpoint), it can object to any unavailable
3997 permissions. */
3998
3999void
c378d69d 4000remote_set_permissions (struct target_ops *self)
d914c394
SS
4001{
4002 struct remote_state *rs = get_remote_state ();
4003
bba74b36
YQ
4004 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
4005 "WriteReg:%x;WriteMem:%x;"
4006 "InsertBreak:%x;InsertTrace:%x;"
4007 "InsertFastTrace:%x;Stop:%x",
4008 may_write_registers, may_write_memory,
4009 may_insert_breakpoints, may_insert_tracepoints,
4010 may_insert_fast_tracepoints, may_stop);
d914c394
SS
4011 putpkt (rs->buf);
4012 getpkt (&rs->buf, &rs->buf_size, 0);
4013
4014 /* If the target didn't like the packet, warn the user. Do not try
4015 to undo the user's settings, that would just be maddening. */
4016 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 4017 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
4018}
4019
be2a5f71
DJ
4020/* This type describes each known response to the qSupported
4021 packet. */
4022struct protocol_feature
4023{
4024 /* The name of this protocol feature. */
4025 const char *name;
4026
4027 /* The default for this protocol feature. */
4028 enum packet_support default_support;
4029
4030 /* The function to call when this feature is reported, or after
4031 qSupported processing if the feature is not supported.
4032 The first argument points to this structure. The second
4033 argument indicates whether the packet requested support be
4034 enabled, disabled, or probed (or the default, if this function
4035 is being called at the end of processing and this feature was
4036 not reported). The third argument may be NULL; if not NULL, it
4037 is a NUL-terminated string taken from the packet following
4038 this feature's name and an equals sign. */
4039 void (*func) (const struct protocol_feature *, enum packet_support,
4040 const char *);
4041
4042 /* The corresponding packet for this feature. Only used if
4043 FUNC is remote_supported_packet. */
4044 int packet;
4045};
4046
be2a5f71
DJ
4047static void
4048remote_supported_packet (const struct protocol_feature *feature,
4049 enum packet_support support,
4050 const char *argument)
4051{
4052 if (argument)
4053 {
4054 warning (_("Remote qSupported response supplied an unexpected value for"
4055 " \"%s\"."), feature->name);
4056 return;
4057 }
4058
4082afcc 4059 remote_protocol_packets[feature->packet].support = support;
be2a5f71 4060}
be2a5f71
DJ
4061
4062static void
4063remote_packet_size (const struct protocol_feature *feature,
4064 enum packet_support support, const char *value)
4065{
4066 struct remote_state *rs = get_remote_state ();
4067
4068 int packet_size;
4069 char *value_end;
4070
4071 if (support != PACKET_ENABLE)
4072 return;
4073
4074 if (value == NULL || *value == '\0')
4075 {
4076 warning (_("Remote target reported \"%s\" without a size."),
4077 feature->name);
4078 return;
4079 }
4080
4081 errno = 0;
4082 packet_size = strtol (value, &value_end, 16);
4083 if (errno != 0 || *value_end != '\0' || packet_size < 0)
4084 {
4085 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
4086 feature->name, value);
4087 return;
4088 }
4089
4090 if (packet_size > MAX_REMOTE_PACKET_SIZE)
4091 {
4092 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
4093 packet_size, MAX_REMOTE_PACKET_SIZE);
4094 packet_size = MAX_REMOTE_PACKET_SIZE;
4095 }
4096
4097 /* Record the new maximum packet size. */
4098 rs->explicit_packet_size = packet_size;
4099}
4100
dc473cfb 4101static const struct protocol_feature remote_protocol_features[] = {
0876f84a 4102 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 4103 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 4104 PACKET_qXfer_auxv },
c78fa86a
GB
4105 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
4106 PACKET_qXfer_exec_file },
23181151
DJ
4107 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
4108 PACKET_qXfer_features },
cfa9d6d9
DJ
4109 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
4110 PACKET_qXfer_libraries },
2268b414
JK
4111 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
4112 PACKET_qXfer_libraries_svr4 },
ced63ec0 4113 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 4114 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 4115 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 4116 PACKET_qXfer_memory_map },
4de6483e
UW
4117 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
4118 PACKET_qXfer_spu_read },
4119 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
4120 PACKET_qXfer_spu_write },
07e059b5
VP
4121 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
4122 PACKET_qXfer_osdata },
dc146f7c
VP
4123 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4124 PACKET_qXfer_threads },
b3b9301e
PA
4125 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4126 PACKET_qXfer_traceframe_info },
89be2091
DJ
4127 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4128 PACKET_QPassSignals },
9b224c5e
PA
4129 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4130 PACKET_QProgramSignals },
a6f3e723
SL
4131 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4132 PACKET_QStartNoAckMode },
4082afcc
PA
4133 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4134 PACKET_multiprocess_feature },
4135 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4136 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4137 PACKET_qXfer_siginfo_read },
4138 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4139 PACKET_qXfer_siginfo_write },
4082afcc 4140 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4141 PACKET_ConditionalTracepoints },
4082afcc 4142 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4143 PACKET_ConditionalBreakpoints },
4082afcc 4144 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4145 PACKET_BreakpointCommands },
4082afcc 4146 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4147 PACKET_FastTracepoints },
4082afcc 4148 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4149 PACKET_StaticTracepoints },
4082afcc 4150 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4151 PACKET_InstallInTrace},
4082afcc
PA
4152 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4153 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4154 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4155 PACKET_bc },
4156 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4157 PACKET_bs },
409873ef
SS
4158 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4159 PACKET_TracepointSource },
d914c394
SS
4160 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4161 PACKET_QAllow },
4082afcc
PA
4162 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4163 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4164 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4165 PACKET_qXfer_fdpic },
169081d0
TG
4166 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4167 PACKET_qXfer_uib },
03583c20
UW
4168 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4169 PACKET_QDisableRandomization },
d1feda86 4170 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4171 { "QTBuffer:size", PACKET_DISABLE,
4172 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4173 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4174 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4175 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
b20a6524 4176 { "Qbtrace:pt", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_pt },
9accd112 4177 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4178 PACKET_qXfer_btrace },
4179 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4180 PACKET_qXfer_btrace_conf },
4181 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4182 PACKET_Qbtrace_conf_bts_size },
4183 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4184 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4185 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4186 PACKET_fork_event_feature },
4187 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4188 PACKET_vfork_event_feature },
b20a6524
MM
4189 { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
4190 PACKET_Qbtrace_conf_pt_size }
be2a5f71
DJ
4191};
4192
c8d5aac9
L
4193static char *remote_support_xml;
4194
4195/* Register string appended to "xmlRegisters=" in qSupported query. */
4196
4197void
6e39997a 4198register_remote_support_xml (const char *xml)
c8d5aac9
L
4199{
4200#if defined(HAVE_LIBEXPAT)
4201 if (remote_support_xml == NULL)
c4f7c687 4202 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4203 else
4204 {
4205 char *copy = xstrdup (remote_support_xml + 13);
4206 char *p = strtok (copy, ",");
4207
4208 do
4209 {
4210 if (strcmp (p, xml) == 0)
4211 {
4212 /* already there */
4213 xfree (copy);
4214 return;
4215 }
4216 }
4217 while ((p = strtok (NULL, ",")) != NULL);
4218 xfree (copy);
4219
94b0dee1
PA
4220 remote_support_xml = reconcat (remote_support_xml,
4221 remote_support_xml, ",", xml,
4222 (char *) NULL);
c8d5aac9
L
4223 }
4224#endif
4225}
4226
4227static char *
4228remote_query_supported_append (char *msg, const char *append)
4229{
4230 if (msg)
94b0dee1 4231 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
4232 else
4233 return xstrdup (append);
4234}
4235
be2a5f71
DJ
4236static void
4237remote_query_supported (void)
4238{
4239 struct remote_state *rs = get_remote_state ();
4240 char *next;
4241 int i;
4242 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4243
4244 /* The packet support flags are handled differently for this packet
4245 than for most others. We treat an error, a disabled packet, and
4246 an empty response identically: any features which must be reported
4247 to be used will be automatically disabled. An empty buffer
4248 accomplishes this, since that is also the representation for a list
4249 containing no features. */
4250
4251 rs->buf[0] = 0;
4082afcc 4252 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 4253 {
c8d5aac9 4254 char *q = NULL;
94b0dee1 4255 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 4256
901f9912 4257 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9 4258
f7e6eed5
PA
4259 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4260 q = remote_query_supported_append (q, "swbreak+");
4261 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4262 q = remote_query_supported_append (q, "hwbreak+");
4263
c8d5aac9
L
4264 if (remote_support_xml)
4265 q = remote_query_supported_append (q, remote_support_xml);
4266
dde08ee1
PA
4267 q = remote_query_supported_append (q, "qRelocInsn+");
4268
89245bc0
DB
4269 if (rs->extended)
4270 {
4271 if (packet_set_cmd_state (PACKET_fork_event_feature)
4272 != AUTO_BOOLEAN_FALSE)
4273 q = remote_query_supported_append (q, "fork-events+");
4274 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4275 != AUTO_BOOLEAN_FALSE)
4276 q = remote_query_supported_append (q, "vfork-events+");
4277 }
4278
dde08ee1
PA
4279 q = reconcat (q, "qSupported:", q, (char *) NULL);
4280 putpkt (q);
82f73884 4281
94b0dee1
PA
4282 do_cleanups (old_chain);
4283
be2a5f71
DJ
4284 getpkt (&rs->buf, &rs->buf_size, 0);
4285
4286 /* If an error occured, warn, but do not return - just reset the
4287 buffer to empty and go on to disable features. */
4288 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4289 == PACKET_ERROR)
4290 {
4291 warning (_("Remote failure reply: %s"), rs->buf);
4292 rs->buf[0] = 0;
4293 }
4294 }
4295
4296 memset (seen, 0, sizeof (seen));
4297
4298 next = rs->buf;
4299 while (*next)
4300 {
4301 enum packet_support is_supported;
4302 char *p, *end, *name_end, *value;
4303
4304 /* First separate out this item from the rest of the packet. If
4305 there's another item after this, we overwrite the separator
4306 (terminated strings are much easier to work with). */
4307 p = next;
4308 end = strchr (p, ';');
4309 if (end == NULL)
4310 {
4311 end = p + strlen (p);
4312 next = end;
4313 }
4314 else
4315 {
89be2091
DJ
4316 *end = '\0';
4317 next = end + 1;
4318
be2a5f71
DJ
4319 if (end == p)
4320 {
4321 warning (_("empty item in \"qSupported\" response"));
4322 continue;
4323 }
be2a5f71
DJ
4324 }
4325
4326 name_end = strchr (p, '=');
4327 if (name_end)
4328 {
4329 /* This is a name=value entry. */
4330 is_supported = PACKET_ENABLE;
4331 value = name_end + 1;
4332 *name_end = '\0';
4333 }
4334 else
4335 {
4336 value = NULL;
4337 switch (end[-1])
4338 {
4339 case '+':
4340 is_supported = PACKET_ENABLE;
4341 break;
4342
4343 case '-':
4344 is_supported = PACKET_DISABLE;
4345 break;
4346
4347 case '?':
4348 is_supported = PACKET_SUPPORT_UNKNOWN;
4349 break;
4350
4351 default:
3e43a32a
MS
4352 warning (_("unrecognized item \"%s\" "
4353 "in \"qSupported\" response"), p);
be2a5f71
DJ
4354 continue;
4355 }
4356 end[-1] = '\0';
4357 }
4358
4359 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4360 if (strcmp (remote_protocol_features[i].name, p) == 0)
4361 {
4362 const struct protocol_feature *feature;
4363
4364 seen[i] = 1;
4365 feature = &remote_protocol_features[i];
4366 feature->func (feature, is_supported, value);
4367 break;
4368 }
4369 }
4370
4371 /* If we increased the packet size, make sure to increase the global
4372 buffer size also. We delay this until after parsing the entire
4373 qSupported packet, because this is the same buffer we were
4374 parsing. */
4375 if (rs->buf_size < rs->explicit_packet_size)
4376 {
4377 rs->buf_size = rs->explicit_packet_size;
4378 rs->buf = xrealloc (rs->buf, rs->buf_size);
4379 }
4380
4381 /* Handle the defaults for unmentioned features. */
4382 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4383 if (!seen[i])
4384 {
4385 const struct protocol_feature *feature;
4386
4387 feature = &remote_protocol_features[i];
4388 feature->func (feature, feature->default_support, NULL);
4389 }
4390}
4391
78a095c3
JK
4392/* Remove any of the remote.c targets from target stack. Upper targets depend
4393 on it so remove them first. */
4394
4395static void
4396remote_unpush_target (void)
4397{
4398 pop_all_targets_above (process_stratum - 1);
4399}
be2a5f71 4400
c906108c 4401static void
014f9477 4402remote_open_1 (const char *name, int from_tty,
3e43a32a 4403 struct target_ops *target, int extended_p)
c906108c 4404{
d01949b6 4405 struct remote_state *rs = get_remote_state ();
a6f3e723 4406
c906108c 4407 if (name == 0)
8a3fe4f8 4408 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 4409 "serial device is attached to the remote system\n"
8a3fe4f8 4410 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 4411
23860348 4412 /* See FIXME above. */
c6ebd6cf 4413 if (!target_async_permitted)
92d1e331 4414 wait_forever_enabled_p = 1;
6426a772 4415
2d717e4f 4416 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
4417 Ask this question first, before target_preopen has a chance to kill
4418 anything. */
5d93a237 4419 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 4420 {
78a095c3
JK
4421 if (from_tty
4422 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
4423 error (_("Still connected."));
4424 }
4425
78a095c3 4426 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
4427 target_preopen (from_tty);
4428
89be2091 4429 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
4430 xfree (rs->last_pass_packet);
4431 rs->last_pass_packet = NULL;
89be2091 4432
9b224c5e
PA
4433 /* Make sure we send the program signals list the next time we
4434 resume. */
5e4a05c4
TT
4435 xfree (rs->last_program_signals_packet);
4436 rs->last_program_signals_packet = NULL;
9b224c5e 4437
ad9a8f3f 4438 remote_fileio_reset ();
1dd41f16
NS
4439 reopen_exec_file ();
4440 reread_symbols ();
4441
5d93a237
TT
4442 rs->remote_desc = remote_serial_open (name);
4443 if (!rs->remote_desc)
c906108c
SS
4444 perror_with_name (name);
4445
4446 if (baud_rate != -1)
4447 {
5d93a237 4448 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 4449 {
9b74d5d3
KB
4450 /* The requested speed could not be set. Error out to
4451 top level after closing remote_desc. Take care to
4452 set remote_desc to NULL to avoid closing remote_desc
4453 more than once. */
5d93a237
TT
4454 serial_close (rs->remote_desc);
4455 rs->remote_desc = NULL;
c906108c
SS
4456 perror_with_name (name);
4457 }
4458 }
4459
236af5e3 4460 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 4461 serial_raw (rs->remote_desc);
c906108c
SS
4462
4463 /* If there is something sitting in the buffer we might take it as a
4464 response to a command, which would be bad. */
5d93a237 4465 serial_flush_input (rs->remote_desc);
c906108c
SS
4466
4467 if (from_tty)
4468 {
4469 puts_filtered ("Remote debugging using ");
4470 puts_filtered (name);
4471 puts_filtered ("\n");
4472 }
23860348 4473 push_target (target); /* Switch to using remote target now. */
c906108c 4474
74531fed
PA
4475 /* Register extra event sources in the event loop. */
4476 remote_async_inferior_event_token
4477 = create_async_event_handler (remote_async_inferior_event_handler,
4478 NULL);
5965e028 4479 rs->notif_state = remote_notif_state_allocate ();
74531fed 4480
be2a5f71
DJ
4481 /* Reset the target state; these things will be queried either by
4482 remote_query_supported or as they are needed. */
ca4f7f8b 4483 reset_all_packet_configs_support ();
74531fed 4484 rs->cached_wait_status = 0;
be2a5f71 4485 rs->explicit_packet_size = 0;
a6f3e723 4486 rs->noack_mode = 0;
82f73884 4487 rs->extended = extended_p;
e24a49d8 4488 rs->waiting_for_stop_reply = 0;
3a29589a 4489 rs->ctrlc_pending_p = 0;
802188a7 4490
47f8a51d
TT
4491 rs->general_thread = not_sent_ptid;
4492 rs->continue_thread = not_sent_ptid;
262e1174 4493 rs->remote_traceframe_number = -1;
c906108c 4494
9d1f7ab2 4495 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
4496 rs->use_threadinfo_query = 1;
4497 rs->use_threadextra_query = 1;
9d1f7ab2 4498
c6ebd6cf 4499 if (target_async_permitted)
92d1e331 4500 {
23860348 4501 /* With this target we start out by owning the terminal. */
92d1e331
DJ
4502 remote_async_terminal_ours_p = 1;
4503
4504 /* FIXME: cagney/1999-09-23: During the initial connection it is
4505 assumed that the target is already ready and able to respond to
0df8b418 4506 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 4507 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 4508 around this. Eventually a mechanism that allows
92d1e331 4509 wait_for_inferior() to expect/get timeouts will be
23860348 4510 implemented. */
92d1e331
DJ
4511 wait_forever_enabled_p = 0;
4512 }
4513
23860348 4514 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 4515 no_shared_libraries (NULL, 0);
f78f6cf1 4516
74531fed
PA
4517 /* Start afresh. */
4518 init_thread_list ();
4519
36918e70 4520 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
4521 target (we'd otherwise be in an inconsistent state) and then
4522 propogate the error on up the exception chain. This ensures that
4523 the caller doesn't stumble along blindly assuming that the
4524 function succeeded. The CLI doesn't have this problem but other
4525 UI's, such as MI do.
36918e70
AC
4526
4527 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4528 this function should return an error indication letting the
ce2826aa 4529 caller restore the previous state. Unfortunately the command
36918e70
AC
4530 ``target remote'' is directly wired to this function making that
4531 impossible. On a positive note, the CLI side of this problem has
4532 been fixed - the function set_cmd_context() makes it possible for
4533 all the ``target ....'' commands to share a common callback
4534 function. See cli-dump.c. */
109c3e39 4535 {
2d717e4f 4536
492d29ea 4537 TRY
04bd08de
TT
4538 {
4539 remote_start_remote (from_tty, target, extended_p);
4540 }
492d29ea 4541 CATCH (ex, RETURN_MASK_ALL)
109c3e39 4542 {
c8d104ad
PA
4543 /* Pop the partially set up target - unless something else did
4544 already before throwing the exception. */
5d93a237 4545 if (rs->remote_desc != NULL)
78a095c3 4546 remote_unpush_target ();
c6ebd6cf 4547 if (target_async_permitted)
109c3e39
AC
4548 wait_forever_enabled_p = 1;
4549 throw_exception (ex);
4550 }
492d29ea 4551 END_CATCH
109c3e39 4552 }
c906108c 4553
f4abbc16
MM
4554 remote_btrace_reset ();
4555
c6ebd6cf 4556 if (target_async_permitted)
92d1e331 4557 wait_forever_enabled_p = 1;
43ff13b4
JM
4558}
4559
de0d863e
DB
4560/* Detach the specified process. */
4561
4562static void
4563remote_detach_pid (int pid)
4564{
4565 struct remote_state *rs = get_remote_state ();
4566
4567 if (remote_multi_process_p (rs))
4568 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
4569 else
4570 strcpy (rs->buf, "D");
4571
4572 putpkt (rs->buf);
4573 getpkt (&rs->buf, &rs->buf_size, 0);
4574
4575 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4576 ;
4577 else if (rs->buf[0] == '\0')
4578 error (_("Remote doesn't know how to detach"));
4579 else
4580 error (_("Can't detach process."));
4581}
4582
4583/* This detaches a program to which we previously attached, using
4584 inferior_ptid to identify the process. After this is done, GDB
4585 can be used to debug some other program. We better not have left
4586 any breakpoints in the target program or it'll die when it hits
4587 one. */
c906108c
SS
4588
4589static void
de0d863e 4590remote_detach_1 (const char *args, int from_tty)
c906108c 4591{
82f73884 4592 int pid = ptid_get_pid (inferior_ptid);
d01949b6 4593 struct remote_state *rs = get_remote_state ();
de0d863e
DB
4594 struct thread_info *tp = find_thread_ptid (inferior_ptid);
4595 int is_fork_parent;
c906108c
SS
4596
4597 if (args)
8a3fe4f8 4598 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 4599
2d717e4f
DJ
4600 if (!target_has_execution)
4601 error (_("No process to detach from."));
4602
7cee1e54
PA
4603 if (from_tty)
4604 {
4605 char *exec_file = get_exec_file (0);
4606 if (exec_file == NULL)
4607 exec_file = "";
4608 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4609 target_pid_to_str (pid_to_ptid (pid)));
4610 gdb_flush (gdb_stdout);
4611 }
4612
c906108c 4613 /* Tell the remote target to detach. */
de0d863e 4614 remote_detach_pid (pid);
82f73884 4615
de0d863e 4616 if (from_tty && !rs->extended)
7cee1e54 4617 puts_filtered (_("Ending remote debugging.\n"));
82f73884 4618
de0d863e
DB
4619 /* Check to see if we are detaching a fork parent. Note that if we
4620 are detaching a fork child, tp == NULL. */
4621 is_fork_parent = (tp != NULL
4622 && tp->pending_follow.kind == TARGET_WAITKIND_FORKED);
4623
4624 /* If doing detach-on-fork, we don't mourn, because that will delete
4625 breakpoints that should be available for the followed inferior. */
4626 if (!is_fork_parent)
4627 target_mourn_inferior ();
4628 else
4629 {
4630 inferior_ptid = null_ptid;
4631 detach_inferior (pid);
4632 }
2d717e4f
DJ
4633}
4634
4635static void
52554a0e 4636remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4637{
de0d863e 4638 remote_detach_1 (args, from_tty);
2d717e4f
DJ
4639}
4640
4641static void
52554a0e 4642extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f 4643{
de0d863e
DB
4644 remote_detach_1 (args, from_tty);
4645}
4646
4647/* Target follow-fork function for remote targets. On entry, and
4648 at return, the current inferior is the fork parent.
4649
4650 Note that although this is currently only used for extended-remote,
4651 it is named remote_follow_fork in anticipation of using it for the
4652 remote target as well. */
4653
4654static int
4655remote_follow_fork (struct target_ops *ops, int follow_child,
4656 int detach_fork)
4657{
4658 struct remote_state *rs = get_remote_state ();
c269dbdb 4659 enum target_waitkind kind = inferior_thread ()->pending_follow.kind;
de0d863e 4660
c269dbdb
DB
4661 if ((kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
4662 || (kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
de0d863e
DB
4663 {
4664 /* When following the parent and detaching the child, we detach
4665 the child here. For the case of following the child and
4666 detaching the parent, the detach is done in the target-
4667 independent follow fork code in infrun.c. We can't use
4668 target_detach when detaching an unfollowed child because
4669 the client side doesn't know anything about the child. */
4670 if (detach_fork && !follow_child)
4671 {
4672 /* Detach the fork child. */
4673 ptid_t child_ptid;
4674 pid_t child_pid;
4675
4676 child_ptid = inferior_thread ()->pending_follow.value.related_pid;
4677 child_pid = ptid_get_pid (child_ptid);
4678
4679 remote_detach_pid (child_pid);
4680 detach_inferior (child_pid);
4681 }
4682 }
4683 return 0;
c906108c
SS
4684}
4685
6ad8ae5c
DJ
4686/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4687
43ff13b4 4688static void
fee354ee 4689remote_disconnect (struct target_ops *target, const char *args, int from_tty)
43ff13b4 4690{
43ff13b4 4691 if (args)
2d717e4f 4692 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 4693
2d717e4f 4694 /* Make sure we unpush even the extended remote targets; mourn
20f796c9 4695 won't do it. So call remote_mourn directly instead of
2d717e4f 4696 target_mourn_inferior. */
20f796c9 4697 remote_mourn (target);
2d717e4f 4698
43ff13b4
JM
4699 if (from_tty)
4700 puts_filtered ("Ending remote debugging.\n");
4701}
4702
2d717e4f
DJ
4703/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4704 be chatty about it. */
4705
4706static void
20f796c9
GB
4707extended_remote_attach (struct target_ops *target, const char *args,
4708 int from_tty)
2d717e4f
DJ
4709{
4710 struct remote_state *rs = get_remote_state ();
be86555c 4711 int pid;
96ef3384 4712 char *wait_status = NULL;
2d717e4f 4713
74164c56 4714 pid = parse_pid_to_attach (args);
2d717e4f 4715
74164c56
JK
4716 /* Remote PID can be freely equal to getpid, do not check it here the same
4717 way as in other targets. */
2d717e4f 4718
4082afcc 4719 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
4720 error (_("This target does not support attaching to a process"));
4721
7cee1e54
PA
4722 if (from_tty)
4723 {
4724 char *exec_file = get_exec_file (0);
4725
4726 if (exec_file)
4727 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4728 target_pid_to_str (pid_to_ptid (pid)));
4729 else
4730 printf_unfiltered (_("Attaching to %s\n"),
4731 target_pid_to_str (pid_to_ptid (pid)));
4732
4733 gdb_flush (gdb_stdout);
4734 }
4735
bba74b36 4736 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
4737 putpkt (rs->buf);
4738 getpkt (&rs->buf, &rs->buf_size, 0);
4739
4082afcc
PA
4740 switch (packet_ok (rs->buf,
4741 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 4742 {
4082afcc 4743 case PACKET_OK:
74531fed
PA
4744 if (!non_stop)
4745 {
4746 /* Save the reply for later. */
4747 wait_status = alloca (strlen (rs->buf) + 1);
4748 strcpy (wait_status, rs->buf);
4749 }
4750 else if (strcmp (rs->buf, "OK") != 0)
4751 error (_("Attaching to %s failed with: %s"),
4752 target_pid_to_str (pid_to_ptid (pid)),
4753 rs->buf);
4082afcc
PA
4754 break;
4755 case PACKET_UNKNOWN:
4756 error (_("This target does not support attaching to a process"));
4757 default:
4758 error (_("Attaching to %s failed"),
4759 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 4760 }
2d717e4f 4761
1b6e6f5c 4762 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 4763
2d717e4f 4764 inferior_ptid = pid_to_ptid (pid);
79d7f229 4765
bad34192
PA
4766 if (non_stop)
4767 {
4768 struct thread_info *thread;
79d7f229 4769
bad34192 4770 /* Get list of threads. */
e8032dde 4771 remote_update_thread_list (target);
82f73884 4772
bad34192
PA
4773 thread = first_thread_of_process (pid);
4774 if (thread)
4775 inferior_ptid = thread->ptid;
4776 else
4777 inferior_ptid = pid_to_ptid (pid);
4778
4779 /* Invalidate our notion of the remote current thread. */
47f8a51d 4780 record_currthread (rs, minus_one_ptid);
bad34192 4781 }
74531fed 4782 else
bad34192
PA
4783 {
4784 /* Now, if we have thread information, update inferior_ptid. */
4785 inferior_ptid = remote_current_thread (inferior_ptid);
4786
4787 /* Add the main thread to the thread list. */
4788 add_thread_silent (inferior_ptid);
4789 }
c0a2216e 4790
96ef3384
UW
4791 /* Next, if the target can specify a description, read it. We do
4792 this before anything involving memory or registers. */
4793 target_find_description ();
4794
74531fed
PA
4795 if (!non_stop)
4796 {
4797 /* Use the previously fetched status. */
4798 gdb_assert (wait_status != NULL);
4799
4800 if (target_can_async_p ())
4801 {
722247f1
YQ
4802 struct notif_event *reply
4803 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 4804
722247f1 4805 push_stop_reply ((struct stop_reply *) reply);
74531fed 4806
6a3753b3 4807 target_async (1);
74531fed
PA
4808 }
4809 else
4810 {
4811 gdb_assert (wait_status != NULL);
4812 strcpy (rs->buf, wait_status);
4813 rs->cached_wait_status = 1;
4814 }
4815 }
4816 else
4817 gdb_assert (wait_status == NULL);
2d717e4f
DJ
4818}
4819
b9c1d481
AS
4820/* Implementation of the to_post_attach method. */
4821
4822static void
4823extended_remote_post_attach (struct target_ops *ops, int pid)
4824{
4825 /* In certain cases GDB might not have had the chance to start
4826 symbol lookup up until now. This could happen if the debugged
4827 binary is not using shared libraries, the vsyscall page is not
4828 present (on Linux) and the binary itself hadn't changed since the
4829 debugging process was started. */
4830 if (symfile_objfile != NULL)
4831 remote_check_symbols();
4832}
4833
c906108c 4834\f
506fb367
DJ
4835/* Check for the availability of vCont. This function should also check
4836 the response. */
c906108c
SS
4837
4838static void
6d820c5c 4839remote_vcont_probe (struct remote_state *rs)
c906108c 4840{
2e9f7625 4841 char *buf;
6d820c5c 4842
2e9f7625
DJ
4843 strcpy (rs->buf, "vCont?");
4844 putpkt (rs->buf);
6d820c5c 4845 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 4846 buf = rs->buf;
c906108c 4847
506fb367 4848 /* Make sure that the features we assume are supported. */
61012eef 4849 if (startswith (buf, "vCont"))
506fb367
DJ
4850 {
4851 char *p = &buf[5];
4852 int support_s, support_S, support_c, support_C;
4853
4854 support_s = 0;
4855 support_S = 0;
4856 support_c = 0;
4857 support_C = 0;
d458bd84 4858 rs->supports_vCont.t = 0;
c1e36e3e 4859 rs->supports_vCont.r = 0;
506fb367
DJ
4860 while (p && *p == ';')
4861 {
4862 p++;
4863 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4864 support_s = 1;
4865 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4866 support_S = 1;
4867 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4868 support_c = 1;
4869 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4870 support_C = 1;
74531fed 4871 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 4872 rs->supports_vCont.t = 1;
c1e36e3e
PA
4873 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
4874 rs->supports_vCont.r = 1;
506fb367
DJ
4875
4876 p = strchr (p, ';');
4877 }
c906108c 4878
506fb367
DJ
4879 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4880 BUF will make packet_ok disable the packet. */
4881 if (!support_s || !support_S || !support_c || !support_C)
4882 buf[0] = 0;
4883 }
c906108c 4884
444abaca 4885 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 4886}
c906108c 4887
0d8f58ca
PA
4888/* Helper function for building "vCont" resumptions. Write a
4889 resumption to P. ENDP points to one-passed-the-end of the buffer
4890 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4891 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4892 resumed thread should be single-stepped and/or signalled. If PTID
4893 equals minus_one_ptid, then all threads are resumed; if PTID
4894 represents a process, then all threads of the process are resumed;
4895 the thread to be stepped and/or signalled is given in the global
4896 INFERIOR_PTID. */
4897
4898static char *
4899append_resumption (char *p, char *endp,
2ea28649 4900 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
4901{
4902 struct remote_state *rs = get_remote_state ();
4903
a493e3e2 4904 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 4905 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
4906 else if (step
4907 /* GDB is willing to range step. */
4908 && use_range_stepping
4909 /* Target supports range stepping. */
4910 && rs->supports_vCont.r
4911 /* We don't currently support range stepping multiple
4912 threads with a wildcard (though the protocol allows it,
4913 so stubs shouldn't make an active effort to forbid
4914 it). */
4915 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4916 {
4917 struct thread_info *tp;
4918
4919 if (ptid_equal (ptid, minus_one_ptid))
4920 {
4921 /* If we don't know about the target thread's tid, then
4922 we're resuming magic_null_ptid (see caller). */
4923 tp = find_thread_ptid (magic_null_ptid);
4924 }
4925 else
4926 tp = find_thread_ptid (ptid);
4927 gdb_assert (tp != NULL);
4928
4929 if (tp->control.may_range_step)
4930 {
4931 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
4932
4933 p += xsnprintf (p, endp - p, ";r%s,%s",
4934 phex_nz (tp->control.step_range_start,
4935 addr_size),
4936 phex_nz (tp->control.step_range_end,
4937 addr_size));
4938 }
4939 else
4940 p += xsnprintf (p, endp - p, ";s");
4941 }
0d8f58ca
PA
4942 else if (step)
4943 p += xsnprintf (p, endp - p, ";s");
a493e3e2 4944 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
4945 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4946 else
4947 p += xsnprintf (p, endp - p, ";c");
4948
4949 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4950 {
4951 ptid_t nptid;
4952
4953 /* All (-1) threads of process. */
ba348170 4954 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
4955
4956 p += xsnprintf (p, endp - p, ":");
4957 p = write_ptid (p, endp, nptid);
4958 }
4959 else if (!ptid_equal (ptid, minus_one_ptid))
4960 {
4961 p += xsnprintf (p, endp - p, ":");
4962 p = write_ptid (p, endp, ptid);
4963 }
4964
4965 return p;
4966}
4967
e5ef252a
PA
4968/* Append a vCont continue-with-signal action for threads that have a
4969 non-zero stop signal. */
4970
4971static char *
4972append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4973{
4974 struct thread_info *thread;
4975
034f788c 4976 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
4977 if (ptid_match (thread->ptid, ptid)
4978 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 4979 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
4980 {
4981 p = append_resumption (p, endp, thread->ptid,
4982 0, thread->suspend.stop_signal);
4983 thread->suspend.stop_signal = GDB_SIGNAL_0;
4984 }
4985
4986 return p;
4987}
4988
506fb367
DJ
4989/* Resume the remote inferior by using a "vCont" packet. The thread
4990 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
4991 resumed thread should be single-stepped and/or signalled. If PTID
4992 equals minus_one_ptid, then all threads are resumed; the thread to
4993 be stepped and/or signalled is given in the global INFERIOR_PTID.
4994 This function returns non-zero iff it resumes the inferior.
44eaed12 4995
506fb367
DJ
4996 This function issues a strict subset of all possible vCont commands at the
4997 moment. */
44eaed12 4998
506fb367 4999static int
2ea28649 5000remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
5001{
5002 struct remote_state *rs = get_remote_state ();
82f73884
PA
5003 char *p;
5004 char *endp;
44eaed12 5005
4082afcc 5006 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 5007 remote_vcont_probe (rs);
44eaed12 5008
4082afcc 5009 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 5010 return 0;
44eaed12 5011
82f73884
PA
5012 p = rs->buf;
5013 endp = rs->buf + get_remote_packet_size ();
5014
506fb367
DJ
5015 /* If we could generate a wider range of packets, we'd have to worry
5016 about overflowing BUF. Should there be a generic
5017 "multi-part-packet" packet? */
5018
0d8f58ca
PA
5019 p += xsnprintf (p, endp - p, "vCont");
5020
79d7f229 5021 if (ptid_equal (ptid, magic_null_ptid))
c906108c 5022 {
79d7f229
PA
5023 /* MAGIC_NULL_PTID means that we don't have any active threads,
5024 so we don't have any TID numbers the inferior will
5025 understand. Make sure to only send forms that do not specify
5026 a TID. */
a9cbf802 5027 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 5028 }
0d8f58ca 5029 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 5030 {
0d8f58ca
PA
5031 /* Resume all threads (of all processes, or of a single
5032 process), with preference for INFERIOR_PTID. This assumes
5033 inferior_ptid belongs to the set of all threads we are about
5034 to resume. */
a493e3e2 5035 if (step || siggnal != GDB_SIGNAL_0)
82f73884 5036 {
0d8f58ca
PA
5037 /* Step inferior_ptid, with or without signal. */
5038 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 5039 }
0d8f58ca 5040
e5ef252a
PA
5041 /* Also pass down any pending signaled resumption for other
5042 threads not the current. */
5043 p = append_pending_thread_resumptions (p, endp, ptid);
5044
0d8f58ca 5045 /* And continue others without a signal. */
a493e3e2 5046 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
5047 }
5048 else
506fb367
DJ
5049 {
5050 /* Scheduler locking; resume only PTID. */
a9cbf802 5051 append_resumption (p, endp, ptid, step, siggnal);
506fb367 5052 }
c906108c 5053
82f73884
PA
5054 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
5055 putpkt (rs->buf);
506fb367 5056
74531fed
PA
5057 if (non_stop)
5058 {
5059 /* In non-stop, the stub replies to vCont with "OK". The stop
5060 reply will be reported asynchronously by means of a `%Stop'
5061 notification. */
5062 getpkt (&rs->buf, &rs->buf_size, 0);
5063 if (strcmp (rs->buf, "OK") != 0)
5064 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
5065 }
5066
506fb367 5067 return 1;
c906108c 5068}
43ff13b4 5069
506fb367
DJ
5070/* Tell the remote machine to resume. */
5071
43ff13b4 5072static void
28439f5e 5073remote_resume (struct target_ops *ops,
2ea28649 5074 ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 5075{
d01949b6 5076 struct remote_state *rs = get_remote_state ();
2e9f7625 5077 char *buf;
43ff13b4 5078
722247f1
YQ
5079 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
5080 (explained in remote-notif.c:handle_notification) so
5081 remote_notif_process is not called. We need find a place where
5082 it is safe to start a 'vNotif' sequence. It is good to do it
5083 before resuming inferior, because inferior was stopped and no RSP
5084 traffic at that moment. */
5085 if (!non_stop)
5965e028 5086 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 5087
b73be471 5088 rs->last_sent_signal = siggnal;
280ceea3 5089 rs->last_sent_step = step;
43ff13b4 5090
506fb367 5091 /* The vCont packet doesn't need to specify threads via Hc. */
40ab02ce
MS
5092 /* No reverse support (yet) for vCont. */
5093 if (execution_direction != EXEC_REVERSE)
5094 if (remote_vcont_resume (ptid, step, siggnal))
5095 goto done;
506fb367 5096
79d7f229
PA
5097 /* All other supported resume packets do use Hc, so set the continue
5098 thread. */
5099 if (ptid_equal (ptid, minus_one_ptid))
5100 set_continue_thread (any_thread_ptid);
506fb367 5101 else
79d7f229 5102 set_continue_thread (ptid);
506fb367 5103
2e9f7625 5104 buf = rs->buf;
b2175913
MS
5105 if (execution_direction == EXEC_REVERSE)
5106 {
5107 /* We don't pass signals to the target in reverse exec mode. */
a493e3e2 5108 if (info_verbose && siggnal != GDB_SIGNAL_0)
7ea6d463 5109 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
b2175913 5110 siggnal);
40ab02ce 5111
4082afcc 5112 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
40ab02ce 5113 error (_("Remote reverse-step not supported."));
4082afcc 5114 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
08c93ed9 5115 error (_("Remote reverse-continue not supported."));
40ab02ce 5116
b2175913
MS
5117 strcpy (buf, step ? "bs" : "bc");
5118 }
a493e3e2 5119 else if (siggnal != GDB_SIGNAL_0)
43ff13b4
JM
5120 {
5121 buf[0] = step ? 'S' : 'C';
c5aa993b 5122 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 5123 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
5124 buf[3] = '\0';
5125 }
5126 else
c5aa993b 5127 strcpy (buf, step ? "s" : "c");
506fb367 5128
44eaed12 5129 putpkt (buf);
43ff13b4 5130
75c99385 5131 done:
2acceee2 5132 /* We are about to start executing the inferior, let's register it
0df8b418
MS
5133 with the event loop. NOTE: this is the one place where all the
5134 execution commands end up. We could alternatively do this in each
23860348 5135 of the execution commands in infcmd.c. */
2acceee2
JM
5136 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
5137 into infcmd.c in order to allow inferior function calls to work
23860348 5138 NOT asynchronously. */
362646f5 5139 if (target_can_async_p ())
6a3753b3 5140 target_async (1);
e24a49d8
PA
5141
5142 /* We've just told the target to resume. The remote server will
5143 wait for the inferior to stop, and then send a stop reply. In
5144 the mean time, we can't start another command/query ourselves
74531fed
PA
5145 because the stub wouldn't be ready to process it. This applies
5146 only to the base all-stop protocol, however. In non-stop (which
5147 only supports vCont), the stub replies with an "OK", and is
5148 immediate able to process further serial input. */
5149 if (!non_stop)
5150 rs->waiting_for_stop_reply = 1;
43ff13b4 5151}
c906108c 5152\f
43ff13b4
JM
5153
5154/* Set up the signal handler for SIGINT, while the target is
23860348 5155 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 5156static void
934b9bac 5157async_initialize_sigint_signal_handler (void)
43ff13b4 5158{
934b9bac 5159 signal (SIGINT, async_handle_remote_sigint);
43ff13b4
JM
5160}
5161
23860348 5162/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 5163static void
934b9bac 5164async_handle_remote_sigint (int sig)
43ff13b4 5165{
934b9bac 5166 signal (sig, async_handle_remote_sigint_twice);
b2ee242b
PA
5167 /* Note we need to go through gdb_call_async_signal_handler in order
5168 to wake up the event loop on Windows. */
5169 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
43ff13b4
JM
5170}
5171
5172/* Signal handler for SIGINT, installed after SIGINT has already been
5173 sent once. It will take effect the second time that the user sends
23860348 5174 a ^C. */
43ff13b4 5175static void
934b9bac 5176async_handle_remote_sigint_twice (int sig)
43ff13b4 5177{
934b9bac 5178 signal (sig, async_handle_remote_sigint);
b2ee242b
PA
5179 /* See note in async_handle_remote_sigint. */
5180 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
43ff13b4
JM
5181}
5182
6426a772 5183/* Perform the real interruption of the target execution, in response
23860348 5184 to a ^C. */
c5aa993b 5185static void
fba45db2 5186async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
5187{
5188 if (remote_debug)
248fd3bf 5189 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
43ff13b4 5190
94cc34af 5191 target_stop (inferior_ptid);
43ff13b4
JM
5192}
5193
0df8b418 5194/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 5195 up on the target alltogether. */
47e1ce27 5196static void
fba45db2 5197async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 5198{
2df3850c 5199 if (remote_debug)
248fd3bf 5200 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
b803fb0f
DJ
5201
5202 interrupt_query ();
43ff13b4
JM
5203}
5204
5205/* Reinstall the usual SIGINT handlers, after the target has
23860348 5206 stopped. */
6426a772 5207static void
934b9bac 5208async_cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
5209{
5210 signal (SIGINT, handle_sigint);
43ff13b4
JM
5211}
5212
c906108c
SS
5213/* Send ^C to target to halt it. Target will respond, and send us a
5214 packet. */
507f3c78 5215static void (*ofunc) (int);
c906108c 5216
0df8b418
MS
5217/* The command line interface's stop routine. This function is installed
5218 as a signal handler for SIGINT. The first time a user requests a
5219 stop, we call remote_stop to send a break or ^C. If there is no
7a292a7a 5220 response from the target (it didn't stop when the user requested it),
23860348 5221 we ask the user if he'd like to detach from the target. */
c906108c 5222static void
934b9bac 5223sync_remote_interrupt (int signo)
c906108c 5224{
23860348 5225 /* If this doesn't work, try more severe steps. */
934b9bac 5226 signal (signo, sync_remote_interrupt_twice);
7a292a7a 5227
934b9bac 5228 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
7a292a7a
SS
5229}
5230
5231/* The user typed ^C twice. */
5232
5233static void
934b9bac 5234sync_remote_interrupt_twice (int signo)
7a292a7a
SS
5235{
5236 signal (signo, ofunc);
934b9bac
JK
5237 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
5238 signal (signo, sync_remote_interrupt);
c906108c 5239}
7a292a7a 5240
74531fed
PA
5241/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5242 thread, all threads of a remote process, or all threads of all
5243 processes. */
5244
5245static void
5246remote_stop_ns (ptid_t ptid)
5247{
5248 struct remote_state *rs = get_remote_state ();
5249 char *p = rs->buf;
5250 char *endp = rs->buf + get_remote_packet_size ();
74531fed 5251
4082afcc 5252 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
5253 remote_vcont_probe (rs);
5254
d458bd84 5255 if (!rs->supports_vCont.t)
74531fed
PA
5256 error (_("Remote server does not support stopping threads"));
5257
f91d3df5
PA
5258 if (ptid_equal (ptid, minus_one_ptid)
5259 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
5260 p += xsnprintf (p, endp - p, "vCont;t");
5261 else
5262 {
5263 ptid_t nptid;
5264
74531fed
PA
5265 p += xsnprintf (p, endp - p, "vCont;t:");
5266
5267 if (ptid_is_pid (ptid))
5268 /* All (-1) threads of process. */
ba348170 5269 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
5270 else
5271 {
5272 /* Small optimization: if we already have a stop reply for
5273 this thread, no use in telling the stub we want this
5274 stopped. */
5275 if (peek_stop_reply (ptid))
5276 return;
5277
5278 nptid = ptid;
5279 }
5280
a9cbf802 5281 write_ptid (p, endp, nptid);
74531fed
PA
5282 }
5283
5284 /* In non-stop, we get an immediate OK reply. The stop reply will
5285 come in asynchronously by notification. */
5286 putpkt (rs->buf);
5287 getpkt (&rs->buf, &rs->buf_size, 0);
5288 if (strcmp (rs->buf, "OK") != 0)
5289 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
5290}
5291
5292/* All-stop version of target_stop. Sends a break or a ^C to stop the
5293 remote target. It is undefined which thread of which process
5294 reports the stop. */
5295
5296static void
5297remote_stop_as (ptid_t ptid)
5298{
5299 struct remote_state *rs = get_remote_state ();
5300
3a29589a
DJ
5301 rs->ctrlc_pending_p = 1;
5302
74531fed
PA
5303 /* If the inferior is stopped already, but the core didn't know
5304 about it yet, just ignore the request. The cached wait status
5305 will be collected in remote_wait. */
5306 if (rs->cached_wait_status)
5307 return;
5308
9a7071a8
JB
5309 /* Send interrupt_sequence to remote target. */
5310 send_interrupt_sequence ();
74531fed
PA
5311}
5312
0df8b418 5313/* This is the generic stop called via the target vector. When a target
7a292a7a 5314 interrupt is requested, either by the command line or the GUI, we
23860348 5315 will eventually end up here. */
74531fed 5316
c906108c 5317static void
1eab8a48 5318remote_stop (struct target_ops *self, ptid_t ptid)
c906108c 5319{
7a292a7a 5320 if (remote_debug)
0f71a2f6 5321 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 5322
74531fed
PA
5323 if (non_stop)
5324 remote_stop_ns (ptid);
c906108c 5325 else
74531fed 5326 remote_stop_as (ptid);
c906108c
SS
5327}
5328
5329/* Ask the user what to do when an interrupt is received. */
5330
5331static void
fba45db2 5332interrupt_query (void)
c906108c
SS
5333{
5334 target_terminal_ours ();
5335
d9d41e78 5336 if (target_is_async_p ())
c906108c 5337 {
74531fed 5338 signal (SIGINT, handle_sigint);
039e3c22 5339 quit ();
c906108c 5340 }
74531fed
PA
5341 else
5342 {
9e2f0ad4
HZ
5343 if (query (_("Interrupted while waiting for the program.\n\
5344Give up (and stop debugging it)? ")))
74531fed 5345 {
78a095c3 5346 remote_unpush_target ();
039e3c22 5347 quit ();
74531fed
PA
5348 }
5349 }
c906108c
SS
5350
5351 target_terminal_inferior ();
5352}
5353
6426a772
JM
5354/* Enable/disable target terminal ownership. Most targets can use
5355 terminal groups to control terminal ownership. Remote targets are
5356 different in that explicit transfer of ownership to/from GDB/target
23860348 5357 is required. */
6426a772
JM
5358
5359static void
d2f640d4 5360remote_terminal_inferior (struct target_ops *self)
6426a772 5361{
c6ebd6cf 5362 if (!target_async_permitted)
75c99385
PA
5363 /* Nothing to do. */
5364 return;
5365
d9d2d8b6
PA
5366 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5367 idempotent. The event-loop GDB talking to an asynchronous target
5368 with a synchronous command calls this function from both
5369 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5370 transfer the terminal to the target when it shouldn't this guard
5371 can go away. */
6426a772
JM
5372 if (!remote_async_terminal_ours_p)
5373 return;
5374 delete_file_handler (input_fd);
5375 remote_async_terminal_ours_p = 0;
934b9bac 5376 async_initialize_sigint_signal_handler ();
6426a772
JM
5377 /* NOTE: At this point we could also register our selves as the
5378 recipient of all input. Any characters typed could then be
23860348 5379 passed on down to the target. */
6426a772
JM
5380}
5381
5382static void
e3594fd1 5383remote_terminal_ours (struct target_ops *self)
6426a772 5384{
c6ebd6cf 5385 if (!target_async_permitted)
75c99385
PA
5386 /* Nothing to do. */
5387 return;
5388
5389 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
5390 if (remote_async_terminal_ours_p)
5391 return;
934b9bac 5392 async_cleanup_sigint_signal_handler (NULL);
6426a772
JM
5393 add_file_handler (input_fd, stdin_event_handler, 0);
5394 remote_async_terminal_ours_p = 1;
5395}
5396
176a6961 5397static void
917317f4 5398remote_console_output (char *msg)
c906108c
SS
5399{
5400 char *p;
5401
c5aa993b 5402 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
5403 {
5404 char tb[2];
5405 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 5406
c906108c
SS
5407 tb[0] = c;
5408 tb[1] = 0;
43ff13b4 5409 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 5410 }
00db5b94
PA
5411 gdb_flush (gdb_stdtarg);
5412}
74531fed
PA
5413
5414typedef struct cached_reg
5415{
5416 int num;
5417 gdb_byte data[MAX_REGISTER_SIZE];
5418} cached_reg_t;
5419
5420DEF_VEC_O(cached_reg_t);
5421
722247f1 5422typedef struct stop_reply
74531fed 5423{
722247f1 5424 struct notif_event base;
74531fed 5425
722247f1 5426 /* The identifier of the thread about this event */
74531fed
PA
5427 ptid_t ptid;
5428
340e3c99 5429 /* The remote state this event is associated with. When the remote
bcc75809
YQ
5430 connection, represented by a remote_state object, is closed,
5431 all the associated stop_reply events should be released. */
5432 struct remote_state *rs;
5433
74531fed
PA
5434 struct target_waitstatus ws;
5435
15148d6a
PA
5436 /* Expedited registers. This makes remote debugging a bit more
5437 efficient for those targets that provide critical registers as
5438 part of their normal status mechanism (as another roundtrip to
5439 fetch them is avoided). */
74531fed
PA
5440 VEC(cached_reg_t) *regcache;
5441
f7e6eed5
PA
5442 enum target_stop_reason stop_reason;
5443
74531fed
PA
5444 CORE_ADDR watch_data_address;
5445
dc146f7c 5446 int core;
722247f1 5447} *stop_reply_p;
a744cf53 5448
722247f1
YQ
5449DECLARE_QUEUE_P (stop_reply_p);
5450DEFINE_QUEUE_P (stop_reply_p);
5451/* The list of already fetched and acknowledged stop events. This
5452 queue is used for notification Stop, and other notifications
5453 don't need queue for their events, because the notification events
5454 of Stop can't be consumed immediately, so that events should be
5455 queued first, and be consumed by remote_wait_{ns,as} one per
5456 time. Other notifications can consume their events immediately,
5457 so queue is not needed for them. */
5458static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
5459
5460static void
5461stop_reply_xfree (struct stop_reply *r)
5462{
f48ff2a7 5463 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
5464}
5465
722247f1
YQ
5466static void
5467remote_notif_stop_parse (struct notif_client *self, char *buf,
5468 struct notif_event *event)
5469{
5470 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5471}
5472
5473static void
5474remote_notif_stop_ack (struct notif_client *self, char *buf,
5475 struct notif_event *event)
5476{
5477 struct stop_reply *stop_reply = (struct stop_reply *) event;
5478
5479 /* acknowledge */
5480 putpkt ((char *) self->ack_command);
5481
5482 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5483 /* We got an unknown stop reply. */
5484 error (_("Unknown stop reply"));
5485
5486 push_stop_reply (stop_reply);
5487}
5488
5489static int
5490remote_notif_stop_can_get_pending_events (struct notif_client *self)
5491{
5492 /* We can't get pending events in remote_notif_process for
5493 notification stop, and we have to do this in remote_wait_ns
5494 instead. If we fetch all queued events from stub, remote stub
5495 may exit and we have no chance to process them back in
5496 remote_wait_ns. */
5497 mark_async_event_handler (remote_async_inferior_event_token);
5498 return 0;
5499}
5500
5501static void
5502stop_reply_dtr (struct notif_event *event)
5503{
5504 struct stop_reply *r = (struct stop_reply *) event;
5505
5506 VEC_free (cached_reg_t, r->regcache);
5507}
5508
5509static struct notif_event *
5510remote_notif_stop_alloc_reply (void)
5511{
5512 struct notif_event *r
70ba0933 5513 = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
5514
5515 r->dtr = stop_reply_dtr;
5516
5517 return r;
5518}
5519
5520/* A client of notification Stop. */
5521
5522struct notif_client notif_client_stop =
5523{
5524 "Stop",
5525 "vStopped",
5526 remote_notif_stop_parse,
5527 remote_notif_stop_ack,
5528 remote_notif_stop_can_get_pending_events,
5529 remote_notif_stop_alloc_reply,
f48ff2a7 5530 REMOTE_NOTIF_STOP,
722247f1
YQ
5531};
5532
5533/* A parameter to pass data in and out. */
5534
5535struct queue_iter_param
5536{
5537 void *input;
5538 struct stop_reply *output;
5539};
5540
cbb8991c
DB
5541/* Determine if THREAD is a pending fork parent thread. ARG contains
5542 the pid of the process that owns the threads we want to check, or
5543 -1 if we want to check all threads. */
5544
5545static int
5546is_pending_fork_parent (struct target_waitstatus *ws, int event_pid,
5547 ptid_t thread_ptid)
5548{
5549 if (ws->kind == TARGET_WAITKIND_FORKED
5550 || ws->kind == TARGET_WAITKIND_VFORKED)
5551 {
5552 if (event_pid == -1 || event_pid == ptid_get_pid (thread_ptid))
5553 return 1;
5554 }
5555
5556 return 0;
5557}
5558
5559/* Check whether EVENT is a fork event, and if it is, remove the
5560 fork child from the context list passed in DATA. */
5561
5562static int
5563remove_child_of_pending_fork (QUEUE (stop_reply_p) *q,
5564 QUEUE_ITER (stop_reply_p) *iter,
5565 stop_reply_p event,
5566 void *data)
5567{
5568 struct queue_iter_param *param = data;
5569 struct threads_listing_context *context = param->input;
5570
5571 if (event->ws.kind == TARGET_WAITKIND_FORKED
5572 || event->ws.kind == TARGET_WAITKIND_VFORKED)
5573 {
5574 threads_listing_context_remove (&event->ws, context);
5575 }
5576
5577 return 1;
5578}
5579
5580/* If CONTEXT contains any fork child threads that have not been
5581 reported yet, remove them from the CONTEXT list. If such a
5582 thread exists it is because we are stopped at a fork catchpoint
5583 and have not yet called follow_fork, which will set up the
5584 host-side data structures for the new process. */
5585
5586static void
5587remove_new_fork_children (struct threads_listing_context *context)
5588{
5589 struct thread_info * thread;
5590 int pid = -1;
5591 struct notif_client *notif = &notif_client_stop;
5592 struct queue_iter_param param;
5593
5594 /* For any threads stopped at a fork event, remove the corresponding
5595 fork child threads from the CONTEXT list. */
5596 ALL_NON_EXITED_THREADS (thread)
5597 {
5598 struct target_waitstatus *ws = &thread->pending_follow;
5599
5600 if (is_pending_fork_parent (ws, pid, thread->ptid))
5601 {
5602 threads_listing_context_remove (ws, context);
5603 }
5604 }
5605
5606 /* Check for any pending fork events (not reported or processed yet)
5607 in process PID and remove those fork child threads from the
5608 CONTEXT list as well. */
5609 remote_notif_get_pending_events (notif);
5610 param.input = context;
5611 param.output = NULL;
5612 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5613 remove_child_of_pending_fork, &param);
5614}
5615
f48ff2a7
YQ
5616/* Remove stop replies in the queue if its pid is equal to the given
5617 inferior's pid. */
722247f1
YQ
5618
5619static int
f48ff2a7
YQ
5620remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5621 QUEUE_ITER (stop_reply_p) *iter,
5622 stop_reply_p event,
5623 void *data)
722247f1
YQ
5624{
5625 struct queue_iter_param *param = data;
5626 struct inferior *inf = param->input;
5627
f48ff2a7 5628 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
5629 {
5630 stop_reply_xfree (event);
5631 QUEUE_remove_elem (stop_reply_p, q, iter);
5632 }
5633
5634 return 1;
5635}
5636
f48ff2a7 5637/* Discard all pending stop replies of inferior INF. */
c906108c 5638
74531fed 5639static void
5f4cf0bb 5640discard_pending_stop_replies (struct inferior *inf)
c906108c 5641{
722247f1
YQ
5642 int i;
5643 struct queue_iter_param param;
f48ff2a7
YQ
5644 struct stop_reply *reply;
5645 struct remote_state *rs = get_remote_state ();
5646 struct remote_notif_state *rns = rs->notif_state;
5647
5648 /* This function can be notified when an inferior exists. When the
5649 target is not remote, the notification state is NULL. */
5650 if (rs->remote_desc == NULL)
5651 return;
5652
5653 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 5654
74531fed 5655 /* Discard the in-flight notification. */
f48ff2a7 5656 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 5657 {
722247f1 5658 stop_reply_xfree (reply);
f48ff2a7 5659 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 5660 }
c906108c 5661
722247f1
YQ
5662 param.input = inf;
5663 param.output = NULL;
74531fed
PA
5664 /* Discard the stop replies we have already pulled with
5665 vStopped. */
722247f1 5666 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
5667 remove_stop_reply_for_inferior, &param);
5668}
5669
bcc75809
YQ
5670/* If its remote state is equal to the given remote state,
5671 remove EVENT from the stop reply queue. */
5672
5673static int
5674remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5675 QUEUE_ITER (stop_reply_p) *iter,
5676 stop_reply_p event,
5677 void *data)
5678{
5679 struct queue_iter_param *param = data;
5680 struct remote_state *rs = param->input;
5681
5682 if (event->rs == rs)
5683 {
5684 stop_reply_xfree (event);
5685 QUEUE_remove_elem (stop_reply_p, q, iter);
5686 }
5687
5688 return 1;
5689}
5690
5691/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
5692
5693static void
bcc75809 5694discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
5695{
5696 struct queue_iter_param param;
5697
bcc75809 5698 param.input = rs;
f48ff2a7
YQ
5699 param.output = NULL;
5700 /* Discard the stop replies we have already pulled with
5701 vStopped. */
5702 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 5703 remove_stop_reply_of_remote_state, &param);
722247f1 5704}
74531fed 5705
722247f1
YQ
5706/* A parameter to pass data in and out. */
5707
5708static int
5709remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5710 QUEUE_ITER (stop_reply_p) *iter,
5711 stop_reply_p event,
5712 void *data)
5713{
5714 struct queue_iter_param *param = data;
5715 ptid_t *ptid = param->input;
5716
5717 if (ptid_match (event->ptid, *ptid))
5718 {
5719 param->output = event;
5720 QUEUE_remove_elem (stop_reply_p, q, iter);
5721 return 0;
c8e38a49 5722 }
722247f1
YQ
5723
5724 return 1;
74531fed 5725}
43ff13b4 5726
722247f1
YQ
5727/* Remove the first reply in 'stop_reply_queue' which matches
5728 PTID. */
2e9f7625 5729
722247f1
YQ
5730static struct stop_reply *
5731remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 5732{
722247f1
YQ
5733 struct queue_iter_param param;
5734
5735 param.input = &ptid;
5736 param.output = NULL;
5737
5738 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5739 remote_notif_remove_once_on_match, &param);
5740 if (notif_debug)
5741 fprintf_unfiltered (gdb_stdlog,
5742 "notif: discard queued event: 'Stop' in %s\n",
5743 target_pid_to_str (ptid));
a744cf53 5744
722247f1 5745 return param.output;
74531fed 5746}
75c99385 5747
74531fed
PA
5748/* Look for a queued stop reply belonging to PTID. If one is found,
5749 remove it from the queue, and return it. Returns NULL if none is
5750 found. If there are still queued events left to process, tell the
5751 event loop to get back to target_wait soon. */
e24a49d8 5752
74531fed
PA
5753static struct stop_reply *
5754queued_stop_reply (ptid_t ptid)
5755{
722247f1 5756 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 5757
722247f1 5758 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
5759 /* There's still at least an event left. */
5760 mark_async_event_handler (remote_async_inferior_event_token);
5761
722247f1 5762 return r;
74531fed
PA
5763}
5764
5765/* Push a fully parsed stop reply in the stop reply queue. Since we
5766 know that we now have at least one queued event left to pass to the
5767 core side, tell the event loop to get back to target_wait soon. */
5768
5769static void
5770push_stop_reply (struct stop_reply *new_event)
5771{
722247f1 5772 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 5773
722247f1
YQ
5774 if (notif_debug)
5775 fprintf_unfiltered (gdb_stdlog,
5776 "notif: push 'Stop' %s to queue %d\n",
5777 target_pid_to_str (new_event->ptid),
5778 QUEUE_length (stop_reply_p,
5779 stop_reply_queue));
74531fed
PA
5780
5781 mark_async_event_handler (remote_async_inferior_event_token);
5782}
5783
722247f1
YQ
5784static int
5785stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5786 QUEUE_ITER (stop_reply_p) *iter,
5787 struct stop_reply *event,
5788 void *data)
5789{
5790 ptid_t *ptid = data;
5791
5792 return !(ptid_equal (*ptid, event->ptid)
5793 && event->ws.kind == TARGET_WAITKIND_STOPPED);
5794}
5795
74531fed
PA
5796/* Returns true if we have a stop reply for PTID. */
5797
5798static int
5799peek_stop_reply (ptid_t ptid)
5800{
722247f1
YQ
5801 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5802 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
5803}
5804
1f10ba14
PA
5805/* Skip PACKET until the next semi-colon (or end of string). */
5806
5807static char *
5808skip_to_semicolon (char *p)
5809{
5810 while (*p != '\0' && *p != ';')
5811 p++;
5812 return p;
5813}
5814
74531fed
PA
5815/* Parse the stop reply in BUF. Either the function succeeds, and the
5816 result is stored in EVENT, or throws an error. */
5817
5818static void
5819remote_parse_stop_reply (char *buf, struct stop_reply *event)
5820{
5821 struct remote_arch_state *rsa = get_remote_arch_state ();
5822 ULONGEST addr;
5823 char *p;
5824
5825 event->ptid = null_ptid;
bcc75809 5826 event->rs = get_remote_state ();
74531fed
PA
5827 event->ws.kind = TARGET_WAITKIND_IGNORE;
5828 event->ws.value.integer = 0;
f7e6eed5 5829 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 5830 event->regcache = NULL;
dc146f7c 5831 event->core = -1;
74531fed
PA
5832
5833 switch (buf[0])
5834 {
5835 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
5836 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5837 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5838 ss = signal number
5839 n... = register number
5840 r... = register contents
5841 */
5842
5843 p = &buf[3]; /* after Txx */
5844 while (*p)
5845 {
5846 char *p1;
cea39f65 5847 int fieldsize;
43ff13b4 5848
1f10ba14
PA
5849 p1 = strchr (p, ':');
5850 if (p1 == NULL)
5851 error (_("Malformed packet(a) (missing colon): %s\n\
5852Packet: '%s'\n"),
5853 p, buf);
5854 if (p == p1)
5855 error (_("Malformed packet(a) (missing register number): %s\n\
5856Packet: '%s'\n"),
5857 p, buf);
3c3bea1c 5858
1f10ba14
PA
5859 /* Some "registers" are actually extended stop information.
5860 Note if you're adding a new entry here: GDB 7.9 and
5861 earlier assume that all register "numbers" that start
5862 with an hex digit are real register numbers. Make sure
5863 the server only sends such a packet if it knows the
5864 client understands it. */
c8e38a49 5865
1f10ba14
PA
5866 if (strncmp (p, "thread", p1 - p) == 0)
5867 event->ptid = read_ptid (++p1, &p);
5868 else if ((strncmp (p, "watch", p1 - p) == 0)
5869 || (strncmp (p, "rwatch", p1 - p) == 0)
5870 || (strncmp (p, "awatch", p1 - p) == 0))
cea39f65 5871 {
f7e6eed5 5872 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
5873 p = unpack_varlen_hex (++p1, &addr);
5874 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 5875 }
f7e6eed5
PA
5876 else if (strncmp (p, "swbreak", p1 - p) == 0)
5877 {
5878 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
5879
5880 /* Make sure the stub doesn't forget to indicate support
5881 with qSupported. */
5882 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
5883 error (_("Unexpected swbreak stop reason"));
5884
5885 /* The value part is documented as "must be empty",
5886 though we ignore it, in case we ever decide to make
5887 use of it in a backward compatible way. */
5888 p = skip_to_semicolon (p1 + 1);
5889 }
5890 else if (strncmp (p, "hwbreak", p1 - p) == 0)
5891 {
5892 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
5893
5894 /* Make sure the stub doesn't forget to indicate support
5895 with qSupported. */
5896 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
5897 error (_("Unexpected hwbreak stop reason"));
5898
5899 /* See above. */
5900 p = skip_to_semicolon (p1 + 1);
5901 }
1f10ba14 5902 else if (strncmp (p, "library", p1 - p) == 0)
cea39f65 5903 {
1f10ba14
PA
5904 event->ws.kind = TARGET_WAITKIND_LOADED;
5905 p = skip_to_semicolon (p1 + 1);
5906 }
5907 else if (strncmp (p, "replaylog", p1 - p) == 0)
5908 {
5909 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5910 /* p1 will indicate "begin" or "end", but it makes
5911 no difference for now, so ignore it. */
5912 p = skip_to_semicolon (p1 + 1);
5913 }
5914 else if (strncmp (p, "core", p1 - p) == 0)
5915 {
5916 ULONGEST c;
a744cf53 5917
1f10ba14
PA
5918 p = unpack_varlen_hex (++p1, &c);
5919 event->core = c;
cea39f65 5920 }
de0d863e
DB
5921 else if (strncmp (p, "fork", p1 - p) == 0)
5922 {
5923 event->ws.value.related_pid = read_ptid (++p1, &p);
5924 event->ws.kind = TARGET_WAITKIND_FORKED;
5925 }
c269dbdb
DB
5926 else if (strncmp (p, "vfork", p1 - p) == 0)
5927 {
5928 event->ws.value.related_pid = read_ptid (++p1, &p);
5929 event->ws.kind = TARGET_WAITKIND_VFORKED;
5930 }
5931 else if (strncmp (p, "vforkdone", p1 - p) == 0)
5932 {
5933 event->ws.kind = TARGET_WAITKIND_VFORK_DONE;
5934 p = skip_to_semicolon (p1 + 1);
5935 }
cea39f65
MS
5936 else
5937 {
1f10ba14
PA
5938 ULONGEST pnum;
5939 char *p_temp;
5940
5941 /* Maybe a real ``P'' register number. */
5942 p_temp = unpack_varlen_hex (p, &pnum);
5943 /* If the first invalid character is the colon, we got a
5944 register number. Otherwise, it's an unknown stop
5945 reason. */
5946 if (p_temp == p1)
5947 {
5948 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5949 cached_reg_t cached_reg;
43ff13b4 5950
1f10ba14
PA
5951 if (reg == NULL)
5952 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 5953Packet: '%s'\n"),
1f10ba14 5954 hex_string (pnum), p, buf);
c8e38a49 5955
1f10ba14 5956 cached_reg.num = reg->regnum;
4100683b 5957
1f10ba14
PA
5958 p = p1 + 1;
5959 fieldsize = hex2bin (p, cached_reg.data,
5960 register_size (target_gdbarch (),
5961 reg->regnum));
5962 p += 2 * fieldsize;
5963 if (fieldsize < register_size (target_gdbarch (),
5964 reg->regnum))
5965 warning (_("Remote reply is too short: %s"), buf);
74531fed 5966
1f10ba14
PA
5967 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5968 }
5969 else
5970 {
5971 /* Not a number. Silently skip unknown optional
5972 info. */
5973 p = skip_to_semicolon (p1 + 1);
5974 }
cea39f65 5975 }
c8e38a49 5976
cea39f65
MS
5977 if (*p != ';')
5978 error (_("Remote register badly formatted: %s\nhere: %s"),
5979 buf, p);
5980 ++p;
5981 }
5b5596ff
PA
5982
5983 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
5984 break;
5985
c8e38a49
PA
5986 /* fall through */
5987 case 'S': /* Old style status, just signal only. */
3a09da41
PA
5988 {
5989 int sig;
5990
5991 event->ws.kind = TARGET_WAITKIND_STOPPED;
5992 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
5993 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
5994 event->ws.value.sig = (enum gdb_signal) sig;
5995 else
5996 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5997 }
c8e38a49
PA
5998 break;
5999 case 'W': /* Target exited. */
6000 case 'X':
6001 {
6002 char *p;
6003 int pid;
6004 ULONGEST value;
82f73884 6005
c8e38a49
PA
6006 /* GDB used to accept only 2 hex chars here. Stubs should
6007 only send more if they detect GDB supports multi-process
6008 support. */
6009 p = unpack_varlen_hex (&buf[1], &value);
82f73884 6010
c8e38a49
PA
6011 if (buf[0] == 'W')
6012 {
6013 /* The remote process exited. */
74531fed
PA
6014 event->ws.kind = TARGET_WAITKIND_EXITED;
6015 event->ws.value.integer = value;
c8e38a49
PA
6016 }
6017 else
6018 {
6019 /* The remote process exited with a signal. */
74531fed 6020 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
6021 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
6022 event->ws.value.sig = (enum gdb_signal) value;
6023 else
6024 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 6025 }
82f73884 6026
c8e38a49
PA
6027 /* If no process is specified, assume inferior_ptid. */
6028 pid = ptid_get_pid (inferior_ptid);
6029 if (*p == '\0')
6030 ;
6031 else if (*p == ';')
6032 {
6033 p++;
6034
0b24eb2d 6035 if (*p == '\0')
82f73884 6036 ;
61012eef 6037 else if (startswith (p, "process:"))
82f73884 6038 {
c8e38a49 6039 ULONGEST upid;
a744cf53 6040
c8e38a49
PA
6041 p += sizeof ("process:") - 1;
6042 unpack_varlen_hex (p, &upid);
6043 pid = upid;
82f73884
PA
6044 }
6045 else
6046 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 6047 }
c8e38a49
PA
6048 else
6049 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
6050 event->ptid = pid_to_ptid (pid);
6051 }
6052 break;
6053 }
6054
6055 if (non_stop && ptid_equal (event->ptid, null_ptid))
6056 error (_("No process or thread specified in stop reply: %s"), buf);
6057}
6058
722247f1
YQ
6059/* When the stub wants to tell GDB about a new notification reply, it
6060 sends a notification (%Stop, for example). Those can come it at
6061 any time, hence, we have to make sure that any pending
6062 putpkt/getpkt sequence we're making is finished, before querying
6063 the stub for more events with the corresponding ack command
6064 (vStopped, for example). E.g., if we started a vStopped sequence
6065 immediately upon receiving the notification, something like this
6066 could happen:
74531fed
PA
6067
6068 1.1) --> Hg 1
6069 1.2) <-- OK
6070 1.3) --> g
6071 1.4) <-- %Stop
6072 1.5) --> vStopped
6073 1.6) <-- (registers reply to step #1.3)
6074
6075 Obviously, the reply in step #1.6 would be unexpected to a vStopped
6076 query.
6077
796cb314 6078 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
6079 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
6080 doing whatever we were doing:
6081
6082 2.1) --> Hg 1
6083 2.2) <-- OK
6084 2.3) --> g
6085 2.4) <-- %Stop
6086 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
6087 2.5) <-- (registers reply to step #2.3)
6088
6089 Eventualy after step #2.5, we return to the event loop, which
6090 notices there's an event on the
6091 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
6092 associated callback --- the function below. At this point, we're
6093 always safe to start a vStopped sequence. :
6094
6095 2.6) --> vStopped
6096 2.7) <-- T05 thread:2
6097 2.8) --> vStopped
6098 2.9) --> OK
6099*/
6100
722247f1
YQ
6101void
6102remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
6103{
6104 struct remote_state *rs = get_remote_state ();
74531fed 6105
f48ff2a7 6106 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 6107 {
722247f1
YQ
6108 if (notif_debug)
6109 fprintf_unfiltered (gdb_stdlog,
6110 "notif: process: '%s' ack pending event\n",
6111 nc->name);
74531fed 6112
722247f1 6113 /* acknowledge */
f48ff2a7
YQ
6114 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
6115 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
6116
6117 while (1)
6118 {
6119 getpkt (&rs->buf, &rs->buf_size, 0);
6120 if (strcmp (rs->buf, "OK") == 0)
6121 break;
6122 else
722247f1 6123 remote_notif_ack (nc, rs->buf);
74531fed
PA
6124 }
6125 }
722247f1
YQ
6126 else
6127 {
6128 if (notif_debug)
6129 fprintf_unfiltered (gdb_stdlog,
6130 "notif: process: '%s' no pending reply\n",
6131 nc->name);
6132 }
74531fed
PA
6133}
6134
74531fed
PA
6135/* Called when it is decided that STOP_REPLY holds the info of the
6136 event that is to be returned to the core. This function always
6137 destroys STOP_REPLY. */
6138
6139static ptid_t
6140process_stop_reply (struct stop_reply *stop_reply,
6141 struct target_waitstatus *status)
6142{
6143 ptid_t ptid;
6144
6145 *status = stop_reply->ws;
6146 ptid = stop_reply->ptid;
6147
6148 /* If no thread/process was reported by the stub, assume the current
6149 inferior. */
6150 if (ptid_equal (ptid, null_ptid))
6151 ptid = inferior_ptid;
6152
5f3563ea
PA
6153 if (status->kind != TARGET_WAITKIND_EXITED
6154 && status->kind != TARGET_WAITKIND_SIGNALLED)
74531fed 6155 {
ee154bee
TT
6156 struct remote_state *rs = get_remote_state ();
6157
5f3563ea
PA
6158 /* Expedited registers. */
6159 if (stop_reply->regcache)
6160 {
217f1f79 6161 struct regcache *regcache
f5656ead 6162 = get_thread_arch_regcache (ptid, target_gdbarch ());
5f3563ea
PA
6163 cached_reg_t *reg;
6164 int ix;
6165
6166 for (ix = 0;
6167 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
6168 ix++)
217f1f79 6169 regcache_raw_supply (regcache, reg->num, reg->data);
5f3563ea
PA
6170 VEC_free (cached_reg_t, stop_reply->regcache);
6171 }
74531fed 6172
f7e6eed5 6173 rs->stop_reason = stop_reply->stop_reason;
ee154bee 6174 rs->remote_watch_data_address = stop_reply->watch_data_address;
1941c569
PA
6175
6176 remote_notice_new_inferior (ptid, 0);
dc146f7c 6177 demand_private_info (ptid)->core = stop_reply->core;
74531fed
PA
6178 }
6179
74531fed
PA
6180 stop_reply_xfree (stop_reply);
6181 return ptid;
6182}
6183
6184/* The non-stop mode version of target_wait. */
6185
6186static ptid_t
47608cb1 6187remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6188{
6189 struct remote_state *rs = get_remote_state ();
74531fed
PA
6190 struct stop_reply *stop_reply;
6191 int ret;
fee9eda9 6192 int is_notif = 0;
74531fed
PA
6193
6194 /* If in non-stop mode, get out of getpkt even if a
6195 notification is received. */
6196
6197 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6198 0 /* forever */, &is_notif);
74531fed
PA
6199 while (1)
6200 {
fee9eda9 6201 if (ret != -1 && !is_notif)
74531fed
PA
6202 switch (rs->buf[0])
6203 {
6204 case 'E': /* Error of some sort. */
6205 /* We're out of sync with the target now. Did it continue
6206 or not? We can't tell which thread it was in non-stop,
6207 so just ignore this. */
6208 warning (_("Remote failure reply: %s"), rs->buf);
6209 break;
6210 case 'O': /* Console output. */
6211 remote_console_output (rs->buf + 1);
6212 break;
6213 default:
6214 warning (_("Invalid remote reply: %s"), rs->buf);
6215 break;
6216 }
6217
6218 /* Acknowledge a pending stop reply that may have arrived in the
6219 mean time. */
f48ff2a7 6220 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 6221 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
6222
6223 /* If indeed we noticed a stop reply, we're done. */
6224 stop_reply = queued_stop_reply (ptid);
6225 if (stop_reply != NULL)
6226 return process_stop_reply (stop_reply, status);
6227
47608cb1 6228 /* Still no event. If we're just polling for an event, then
74531fed 6229 return to the event loop. */
47608cb1 6230 if (options & TARGET_WNOHANG)
74531fed
PA
6231 {
6232 status->kind = TARGET_WAITKIND_IGNORE;
6233 return minus_one_ptid;
6234 }
6235
47608cb1 6236 /* Otherwise do a blocking wait. */
74531fed 6237 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 6238 1 /* forever */, &is_notif);
74531fed
PA
6239 }
6240}
6241
6242/* Wait until the remote machine stops, then return, storing status in
6243 STATUS just as `wait' would. */
6244
6245static ptid_t
47608cb1 6246remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
6247{
6248 struct remote_state *rs = get_remote_state ();
74531fed 6249 ptid_t event_ptid = null_ptid;
cea39f65 6250 char *buf;
74531fed
PA
6251 struct stop_reply *stop_reply;
6252
47608cb1
PA
6253 again:
6254
74531fed
PA
6255 status->kind = TARGET_WAITKIND_IGNORE;
6256 status->value.integer = 0;
6257
6258 stop_reply = queued_stop_reply (ptid);
6259 if (stop_reply != NULL)
6260 return process_stop_reply (stop_reply, status);
6261
6262 if (rs->cached_wait_status)
6263 /* Use the cached wait status, but only once. */
6264 rs->cached_wait_status = 0;
6265 else
6266 {
6267 int ret;
722247f1 6268 int is_notif;
74531fed
PA
6269
6270 if (!target_is_async_p ())
6271 {
934b9bac 6272 ofunc = signal (SIGINT, sync_remote_interrupt);
74531fed
PA
6273 /* If the user hit C-c before this packet, or between packets,
6274 pretend that it was hit right here. */
522002f9 6275 if (check_quit_flag ())
74531fed 6276 {
522002f9 6277 clear_quit_flag ();
934b9bac 6278 sync_remote_interrupt (SIGINT);
74531fed
PA
6279 }
6280 }
6281
6282 /* FIXME: cagney/1999-09-27: If we're in async mode we should
6283 _never_ wait for ever -> test on target_is_async_p().
6284 However, before we do that we need to ensure that the caller
6285 knows how to take the target into/out of async mode. */
722247f1
YQ
6286 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
6287 wait_forever_enabled_p, &is_notif);
6288
5e1b953b
SDJ
6289 if (!target_is_async_p ())
6290 signal (SIGINT, ofunc);
6291
722247f1
YQ
6292 /* GDB gets a notification. Return to core as this event is
6293 not interesting. */
6294 if (ret != -1 && is_notif)
6295 return minus_one_ptid;
74531fed
PA
6296 }
6297
6298 buf = rs->buf;
6299
f7e6eed5 6300 rs->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed
PA
6301
6302 /* We got something. */
6303 rs->waiting_for_stop_reply = 0;
6304
3a29589a
DJ
6305 /* Assume that the target has acknowledged Ctrl-C unless we receive
6306 an 'F' or 'O' packet. */
6307 if (buf[0] != 'F' && buf[0] != 'O')
6308 rs->ctrlc_pending_p = 0;
6309
74531fed
PA
6310 switch (buf[0])
6311 {
6312 case 'E': /* Error of some sort. */
6313 /* We're out of sync with the target now. Did it continue or
6314 not? Not is more likely, so report a stop. */
6315 warning (_("Remote failure reply: %s"), buf);
6316 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 6317 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
6318 break;
6319 case 'F': /* File-I/O request. */
3a29589a
DJ
6320 remote_fileio_request (buf, rs->ctrlc_pending_p);
6321 rs->ctrlc_pending_p = 0;
74531fed
PA
6322 break;
6323 case 'T': case 'S': case 'X': case 'W':
6324 {
722247f1
YQ
6325 struct stop_reply *stop_reply
6326 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
6327 rs->buf);
74531fed 6328
74531fed 6329 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
6330 break;
6331 }
6332 case 'O': /* Console output. */
6333 remote_console_output (buf + 1);
e24a49d8 6334
c8e38a49
PA
6335 /* The target didn't really stop; keep waiting. */
6336 rs->waiting_for_stop_reply = 1;
e24a49d8 6337
c8e38a49
PA
6338 break;
6339 case '\0':
b73be471 6340 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
6341 {
6342 /* Zero length reply means that we tried 'S' or 'C' and the
6343 remote system doesn't support it. */
6344 target_terminal_ours_for_output ();
6345 printf_filtered
6346 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
6347 gdb_signal_to_name (rs->last_sent_signal));
6348 rs->last_sent_signal = GDB_SIGNAL_0;
c8e38a49
PA
6349 target_terminal_inferior ();
6350
280ceea3 6351 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
c8e38a49
PA
6352 putpkt ((char *) buf);
6353
6354 /* We just told the target to resume, so a stop reply is in
6355 order. */
e24a49d8 6356 rs->waiting_for_stop_reply = 1;
c8e38a49 6357 break;
43ff13b4 6358 }
c8e38a49
PA
6359 /* else fallthrough */
6360 default:
6361 warning (_("Invalid remote reply: %s"), buf);
6362 /* Keep waiting. */
6363 rs->waiting_for_stop_reply = 1;
6364 break;
43ff13b4 6365 }
c8e38a49 6366
c8e38a49 6367 if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
6368 {
6369 /* Nothing interesting happened. If we're doing a non-blocking
6370 poll, we're done. Otherwise, go back to waiting. */
6371 if (options & TARGET_WNOHANG)
6372 return minus_one_ptid;
6373 else
6374 goto again;
6375 }
74531fed
PA
6376 else if (status->kind != TARGET_WAITKIND_EXITED
6377 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
6378 {
6379 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 6380 record_currthread (rs, event_ptid);
82f73884
PA
6381 else
6382 event_ptid = inferior_ptid;
43ff13b4 6383 }
74531fed
PA
6384 else
6385 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 6386 record_currthread (rs, minus_one_ptid);
79d7f229 6387
82f73884 6388 return event_ptid;
43ff13b4
JM
6389}
6390
74531fed
PA
6391/* Wait until the remote machine stops, then return, storing status in
6392 STATUS just as `wait' would. */
6393
c8e38a49 6394static ptid_t
117de6a9 6395remote_wait (struct target_ops *ops,
47608cb1 6396 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
6397{
6398 ptid_t event_ptid;
6399
74531fed 6400 if (non_stop)
47608cb1 6401 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 6402 else
47608cb1 6403 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 6404
d9d41e78 6405 if (target_is_async_p ())
c8e38a49 6406 {
74531fed
PA
6407 /* If there are are events left in the queue tell the event loop
6408 to return here. */
722247f1 6409 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 6410 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 6411 }
c8e38a49
PA
6412
6413 return event_ptid;
6414}
6415
74ca34ce 6416/* Fetch a single register using a 'p' packet. */
c906108c 6417
b96ec7ac 6418static int
56be3814 6419fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac
AC
6420{
6421 struct remote_state *rs = get_remote_state ();
2e9f7625 6422 char *buf, *p;
b96ec7ac
AC
6423 char regp[MAX_REGISTER_SIZE];
6424 int i;
6425
4082afcc 6426 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
6427 return 0;
6428
6429 if (reg->pnum == -1)
6430 return 0;
6431
2e9f7625 6432 p = rs->buf;
fcad0fa4 6433 *p++ = 'p';
74ca34ce 6434 p += hexnumstr (p, reg->pnum);
fcad0fa4 6435 *p++ = '\0';
1f4437a4
MS
6436 putpkt (rs->buf);
6437 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 6438
2e9f7625
DJ
6439 buf = rs->buf;
6440
74ca34ce
DJ
6441 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
6442 {
6443 case PACKET_OK:
6444 break;
6445 case PACKET_UNKNOWN:
6446 return 0;
6447 case PACKET_ERROR:
27a9c0bf
MS
6448 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6449 gdbarch_register_name (get_regcache_arch (regcache),
6450 reg->regnum),
6451 buf);
74ca34ce 6452 }
3f9a994c
JB
6453
6454 /* If this register is unfetchable, tell the regcache. */
6455 if (buf[0] == 'x')
8480adf2 6456 {
56be3814 6457 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 6458 return 1;
b96ec7ac 6459 }
b96ec7ac 6460
3f9a994c
JB
6461 /* Otherwise, parse and supply the value. */
6462 p = buf;
6463 i = 0;
6464 while (p[0] != 0)
6465 {
6466 if (p[1] == 0)
74ca34ce 6467 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
6468
6469 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6470 p += 2;
6471 }
56be3814 6472 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 6473 return 1;
b96ec7ac
AC
6474}
6475
74ca34ce
DJ
6476/* Fetch the registers included in the target's 'g' packet. */
6477
29709017
DJ
6478static int
6479send_g_packet (void)
c906108c 6480{
d01949b6 6481 struct remote_state *rs = get_remote_state ();
cea39f65 6482 int buf_len;
c906108c 6483
bba74b36 6484 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 6485 remote_send (&rs->buf, &rs->buf_size);
c906108c 6486
29709017
DJ
6487 /* We can get out of synch in various cases. If the first character
6488 in the buffer is not a hex character, assume that has happened
6489 and try to fetch another packet to read. */
6490 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6491 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6492 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6493 && rs->buf[0] != 'x') /* New: unavailable register value. */
6494 {
6495 if (remote_debug)
6496 fprintf_unfiltered (gdb_stdlog,
6497 "Bad register packet; fetching a new packet\n");
6498 getpkt (&rs->buf, &rs->buf_size, 0);
6499 }
6500
74ca34ce
DJ
6501 buf_len = strlen (rs->buf);
6502
6503 /* Sanity check the received packet. */
6504 if (buf_len % 2 != 0)
6505 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
6506
6507 return buf_len / 2;
6508}
6509
6510static void
56be3814 6511process_g_packet (struct regcache *regcache)
29709017 6512{
4a22f64d 6513 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
6514 struct remote_state *rs = get_remote_state ();
6515 struct remote_arch_state *rsa = get_remote_arch_state ();
6516 int i, buf_len;
6517 char *p;
6518 char *regs;
6519
6520 buf_len = strlen (rs->buf);
6521
6522 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce
DJ
6523 if (buf_len > 2 * rsa->sizeof_g_packet)
6524 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6525
6526 /* Save the size of the packet sent to us by the target. It is used
6527 as a heuristic when determining the max size of packets that the
6528 target can safely receive. */
6529 if (rsa->actual_register_packet_size == 0)
6530 rsa->actual_register_packet_size = buf_len;
6531
6532 /* If this is smaller than we guessed the 'g' packet would be,
6533 update our records. A 'g' reply that doesn't include a register's
6534 value implies either that the register is not available, or that
6535 the 'p' packet must be used. */
6536 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 6537 {
74ca34ce
DJ
6538 rsa->sizeof_g_packet = buf_len / 2;
6539
4a22f64d 6540 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 6541 {
74ca34ce
DJ
6542 if (rsa->regs[i].pnum == -1)
6543 continue;
6544
6545 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6546 rsa->regs[i].in_g_packet = 0;
b96ec7ac 6547 else
74ca34ce 6548 rsa->regs[i].in_g_packet = 1;
b96ec7ac 6549 }
74ca34ce 6550 }
b323314b 6551
74ca34ce 6552 regs = alloca (rsa->sizeof_g_packet);
c906108c
SS
6553
6554 /* Unimplemented registers read as all bits zero. */
ea9c271d 6555 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 6556
c906108c
SS
6557 /* Reply describes registers byte by byte, each byte encoded as two
6558 hex characters. Suck them all up, then supply them to the
6559 register cacheing/storage mechanism. */
6560
74ca34ce 6561 p = rs->buf;
ea9c271d 6562 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 6563 {
74ca34ce
DJ
6564 if (p[0] == 0 || p[1] == 0)
6565 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6566 internal_error (__FILE__, __LINE__,
9b20d036 6567 _("unexpected end of 'g' packet reply"));
74ca34ce 6568
c906108c 6569 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 6570 regs[i] = 0; /* 'x' */
c906108c
SS
6571 else
6572 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6573 p += 2;
6574 }
6575
a744cf53
MS
6576 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6577 {
6578 struct packet_reg *r = &rsa->regs[i];
6579
6580 if (r->in_g_packet)
6581 {
6582 if (r->offset * 2 >= strlen (rs->buf))
6583 /* This shouldn't happen - we adjusted in_g_packet above. */
6584 internal_error (__FILE__, __LINE__,
9b20d036 6585 _("unexpected end of 'g' packet reply"));
a744cf53
MS
6586 else if (rs->buf[r->offset * 2] == 'x')
6587 {
6588 gdb_assert (r->offset * 2 < strlen (rs->buf));
6589 /* The register isn't available, mark it as such (at
6590 the same time setting the value to zero). */
6591 regcache_raw_supply (regcache, r->regnum, NULL);
6592 }
6593 else
6594 regcache_raw_supply (regcache, r->regnum,
6595 regs + r->offset);
6596 }
6597 }
c906108c
SS
6598}
6599
29709017 6600static void
56be3814 6601fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
6602{
6603 send_g_packet ();
56be3814 6604 process_g_packet (regcache);
29709017
DJ
6605}
6606
e6e4e701
PA
6607/* Make the remote selected traceframe match GDB's selected
6608 traceframe. */
6609
6610static void
6611set_remote_traceframe (void)
6612{
6613 int newnum;
262e1174 6614 struct remote_state *rs = get_remote_state ();
e6e4e701 6615
262e1174 6616 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
6617 return;
6618
6619 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 6620 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
6621
6622 newnum = target_trace_find (tfind_number,
6623 get_traceframe_number (), 0, 0, NULL);
6624
6625 /* Should not happen. If it does, all bets are off. */
6626 if (newnum != get_traceframe_number ())
6627 warning (_("could not set remote traceframe"));
6628}
6629
74ca34ce 6630static void
28439f5e
PA
6631remote_fetch_registers (struct target_ops *ops,
6632 struct regcache *regcache, int regnum)
74ca34ce 6633{
74ca34ce
DJ
6634 struct remote_arch_state *rsa = get_remote_arch_state ();
6635 int i;
6636
e6e4e701 6637 set_remote_traceframe ();
79d7f229 6638 set_general_thread (inferior_ptid);
74ca34ce
DJ
6639
6640 if (regnum >= 0)
6641 {
6642 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6643
74ca34ce
DJ
6644 gdb_assert (reg != NULL);
6645
6646 /* If this register might be in the 'g' packet, try that first -
6647 we are likely to read more than one register. If this is the
6648 first 'g' packet, we might be overly optimistic about its
6649 contents, so fall back to 'p'. */
6650 if (reg->in_g_packet)
6651 {
56be3814 6652 fetch_registers_using_g (regcache);
74ca34ce
DJ
6653 if (reg->in_g_packet)
6654 return;
6655 }
6656
56be3814 6657 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
6658 return;
6659
6660 /* This register is not available. */
56be3814 6661 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
6662
6663 return;
6664 }
6665
56be3814 6666 fetch_registers_using_g (regcache);
74ca34ce 6667
4a22f64d 6668 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6669 if (!rsa->regs[i].in_g_packet)
56be3814 6670 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
6671 {
6672 /* This register is not available. */
56be3814 6673 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
6674 }
6675}
6676
c906108c
SS
6677/* Prepare to store registers. Since we may send them all (using a
6678 'G' request), we have to read out the ones we don't want to change
6679 first. */
6680
c5aa993b 6681static void
f32dbf8c 6682remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 6683{
ea9c271d 6684 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 6685 int i;
cfd77fa1 6686 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 6687
c906108c 6688 /* Make sure the entire registers array is valid. */
4082afcc 6689 switch (packet_support (PACKET_P))
5a2468f5
JM
6690 {
6691 case PACKET_DISABLE:
6692 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 6693 /* Make sure all the necessary registers are cached. */
4a22f64d 6694 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 6695 if (rsa->regs[i].in_g_packet)
316f2060 6696 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
6697 break;
6698 case PACKET_ENABLE:
6699 break;
6700 }
6701}
6702
ad10f812 6703/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 6704 packet was not recognized. */
5a2468f5
JM
6705
6706static int
1f4437a4
MS
6707store_register_using_P (const struct regcache *regcache,
6708 struct packet_reg *reg)
5a2468f5 6709{
4a22f64d 6710 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 6711 struct remote_state *rs = get_remote_state ();
5a2468f5 6712 /* Try storing a single register. */
6d820c5c 6713 char *buf = rs->buf;
cfd77fa1 6714 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 6715 char *p;
5a2468f5 6716
4082afcc 6717 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
6718 return 0;
6719
6720 if (reg->pnum == -1)
6721 return 0;
6722
ea9c271d 6723 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 6724 p = buf + strlen (buf);
56be3814 6725 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 6726 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
6727 putpkt (rs->buf);
6728 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 6729
74ca34ce
DJ
6730 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6731 {
6732 case PACKET_OK:
6733 return 1;
6734 case PACKET_ERROR:
27a9c0bf
MS
6735 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6736 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
6737 case PACKET_UNKNOWN:
6738 return 0;
6739 default:
6740 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6741 }
c906108c
SS
6742}
6743
23860348
MS
6744/* Store register REGNUM, or all registers if REGNUM == -1, from the
6745 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
6746
6747static void
56be3814 6748store_registers_using_G (const struct regcache *regcache)
c906108c 6749{
d01949b6 6750 struct remote_state *rs = get_remote_state ();
ea9c271d 6751 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 6752 gdb_byte *regs;
c906108c
SS
6753 char *p;
6754
193cb69f
AC
6755 /* Extract all the registers in the regcache copying them into a
6756 local buffer. */
6757 {
b323314b 6758 int i;
a744cf53 6759
ea9c271d
DJ
6760 regs = alloca (rsa->sizeof_g_packet);
6761 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 6762 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 6763 {
ea9c271d 6764 struct packet_reg *r = &rsa->regs[i];
a744cf53 6765
b323314b 6766 if (r->in_g_packet)
56be3814 6767 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
6768 }
6769 }
c906108c
SS
6770
6771 /* Command describes registers byte by byte,
6772 each byte encoded as two hex characters. */
6d820c5c 6773 p = rs->buf;
193cb69f 6774 *p++ = 'G';
74ca34ce
DJ
6775 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6776 updated. */
6777 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
6778 putpkt (rs->buf);
6779 getpkt (&rs->buf, &rs->buf_size, 0);
6780 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
6781 error (_("Could not write registers; remote failure reply '%s'"),
6782 rs->buf);
c906108c 6783}
74ca34ce
DJ
6784
6785/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6786 of the register cache buffer. FIXME: ignores errors. */
6787
6788static void
28439f5e
PA
6789remote_store_registers (struct target_ops *ops,
6790 struct regcache *regcache, int regnum)
74ca34ce 6791{
74ca34ce
DJ
6792 struct remote_arch_state *rsa = get_remote_arch_state ();
6793 int i;
6794
e6e4e701 6795 set_remote_traceframe ();
79d7f229 6796 set_general_thread (inferior_ptid);
74ca34ce
DJ
6797
6798 if (regnum >= 0)
6799 {
6800 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6801
74ca34ce
DJ
6802 gdb_assert (reg != NULL);
6803
6804 /* Always prefer to store registers using the 'P' packet if
6805 possible; we often change only a small number of registers.
6806 Sometimes we change a larger number; we'd need help from a
6807 higher layer to know to use 'G'. */
56be3814 6808 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
6809 return;
6810
6811 /* For now, don't complain if we have no way to write the
6812 register. GDB loses track of unavailable registers too
6813 easily. Some day, this may be an error. We don't have
0df8b418 6814 any way to read the register, either... */
74ca34ce
DJ
6815 if (!reg->in_g_packet)
6816 return;
6817
56be3814 6818 store_registers_using_G (regcache);
74ca34ce
DJ
6819 return;
6820 }
6821
56be3814 6822 store_registers_using_G (regcache);
74ca34ce 6823
4a22f64d 6824 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6825 if (!rsa->regs[i].in_g_packet)
56be3814 6826 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
6827 /* See above for why we do not issue an error here. */
6828 continue;
6829}
c906108c
SS
6830\f
6831
6832/* Return the number of hex digits in num. */
6833
6834static int
fba45db2 6835hexnumlen (ULONGEST num)
c906108c
SS
6836{
6837 int i;
6838
6839 for (i = 0; num != 0; i++)
6840 num >>= 4;
6841
6842 return max (i, 1);
6843}
6844
2df3850c 6845/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
6846
6847static int
fba45db2 6848hexnumstr (char *buf, ULONGEST num)
c906108c 6849{
c906108c 6850 int len = hexnumlen (num);
a744cf53 6851
2df3850c
JM
6852 return hexnumnstr (buf, num, len);
6853}
6854
c906108c 6855
2df3850c 6856/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 6857
2df3850c 6858static int
fba45db2 6859hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
6860{
6861 int i;
6862
6863 buf[width] = '\0';
6864
6865 for (i = width - 1; i >= 0; i--)
c906108c 6866 {
c5aa993b 6867 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
6868 num >>= 4;
6869 }
6870
2df3850c 6871 return width;
c906108c
SS
6872}
6873
23860348 6874/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
6875
6876static CORE_ADDR
fba45db2 6877remote_address_masked (CORE_ADDR addr)
c906108c 6878{
883b9c6c 6879 unsigned int address_size = remote_address_size;
a744cf53 6880
911c95a5
UW
6881 /* If "remoteaddresssize" was not set, default to target address size. */
6882 if (!address_size)
f5656ead 6883 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
6884
6885 if (address_size > 0
6886 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
6887 {
6888 /* Only create a mask when that mask can safely be constructed
23860348 6889 in a ULONGEST variable. */
c906108c 6890 ULONGEST mask = 1;
a744cf53 6891
911c95a5 6892 mask = (mask << address_size) - 1;
c906108c
SS
6893 addr &= mask;
6894 }
6895 return addr;
6896}
6897
6898/* Determine whether the remote target supports binary downloading.
6899 This is accomplished by sending a no-op memory write of zero length
6900 to the target at the specified address. It does not suffice to send
23860348
MS
6901 the whole packet, since many stubs strip the eighth bit and
6902 subsequently compute a wrong checksum, which causes real havoc with
6903 remote_write_bytes.
7a292a7a 6904
96baa820 6905 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 6906 clean. In cases like this, the user should clear "remote
23860348 6907 X-packet". */
96baa820 6908
c906108c 6909static void
fba45db2 6910check_binary_download (CORE_ADDR addr)
c906108c 6911{
d01949b6 6912 struct remote_state *rs = get_remote_state ();
24b06219 6913
4082afcc 6914 switch (packet_support (PACKET_X))
c906108c 6915 {
96baa820
JM
6916 case PACKET_DISABLE:
6917 break;
6918 case PACKET_ENABLE:
6919 break;
6920 case PACKET_SUPPORT_UNKNOWN:
6921 {
96baa820 6922 char *p;
802188a7 6923
2e9f7625 6924 p = rs->buf;
96baa820
JM
6925 *p++ = 'X';
6926 p += hexnumstr (p, (ULONGEST) addr);
6927 *p++ = ',';
6928 p += hexnumstr (p, (ULONGEST) 0);
6929 *p++ = ':';
6930 *p = '\0';
802188a7 6931
2e9f7625 6932 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 6933 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 6934
2e9f7625 6935 if (rs->buf[0] == '\0')
96baa820
JM
6936 {
6937 if (remote_debug)
6938 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
6939 "binary downloading NOT "
6940 "supported by target\n");
444abaca 6941 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
6942 }
6943 else
6944 {
6945 if (remote_debug)
6946 fprintf_unfiltered (gdb_stdlog,
64b9b334 6947 "binary downloading supported by target\n");
444abaca 6948 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
6949 }
6950 break;
6951 }
c906108c
SS
6952 }
6953}
6954
124e13d9
SM
6955/* Helper function to resize the payload in order to try to get a good
6956 alignment. We try to write an amount of data such that the next write will
6957 start on an address aligned on REMOTE_ALIGN_WRITES. */
6958
6959static int
6960align_for_efficient_write (int todo, CORE_ADDR memaddr)
6961{
6962 return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6963}
6964
c906108c
SS
6965/* Write memory data directly to the remote machine.
6966 This does not inform the data cache; the data cache uses this.
a76d924d 6967 HEADER is the starting part of the packet.
c906108c
SS
6968 MEMADDR is the address in the remote memory space.
6969 MYADDR is the address of the buffer in our space.
124e13d9
SM
6970 LEN_UNITS is the number of addressable units to write.
6971 UNIT_SIZE is the length in bytes of an addressable unit.
a76d924d
DJ
6972 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6973 should send data as binary ('X'), or hex-encoded ('M').
6974
6975 The function creates packet of the form
6976 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6977
124e13d9 6978 where encoding of <DATA> is terminated by PACKET_FORMAT.
a76d924d
DJ
6979
6980 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6981 are omitted.
6982
9b409511 6983 Return the transferred status, error or OK (an
124e13d9
SM
6984 'enum target_xfer_status' value). Save the number of addressable units
6985 transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
6986
6987 On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
6988 exchange between gdb and the stub could look like (?? in place of the
6989 checksum):
6990
6991 -> $m1000,4#??
6992 <- aaaabbbbccccdddd
6993
6994 -> $M1000,3:eeeeffffeeee#??
6995 <- OK
6996
6997 -> $m1000,4#??
6998 <- eeeeffffeeeedddd */
c906108c 6999
9b409511 7000static enum target_xfer_status
a76d924d 7001remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
124e13d9
SM
7002 const gdb_byte *myaddr, ULONGEST len_units,
7003 int unit_size, ULONGEST *xfered_len_units,
7004 char packet_format, int use_length)
c906108c 7005{
6d820c5c 7006 struct remote_state *rs = get_remote_state ();
cfd77fa1 7007 char *p;
a76d924d
DJ
7008 char *plen = NULL;
7009 int plenlen = 0;
124e13d9
SM
7010 int todo_units;
7011 int units_written;
7012 int payload_capacity_bytes;
7013 int payload_length_bytes;
a76d924d
DJ
7014
7015 if (packet_format != 'X' && packet_format != 'M')
7016 internal_error (__FILE__, __LINE__,
9b20d036 7017 _("remote_write_bytes_aux: bad packet format"));
c906108c 7018
124e13d9 7019 if (len_units == 0)
9b409511 7020 return TARGET_XFER_EOF;
b2182ed2 7021
124e13d9 7022 payload_capacity_bytes = get_memory_write_packet_size ();
2bc416ba 7023
6d820c5c
DJ
7024 /* The packet buffer will be large enough for the payload;
7025 get_memory_packet_size ensures this. */
a76d924d 7026 rs->buf[0] = '\0';
c906108c 7027
a257b5bb 7028 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
7029 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
7030
124e13d9 7031 payload_capacity_bytes -= strlen ("$,:#NN");
a76d924d 7032 if (!use_length)
0df8b418 7033 /* The comma won't be used. */
124e13d9
SM
7034 payload_capacity_bytes += 1;
7035 payload_capacity_bytes -= strlen (header);
7036 payload_capacity_bytes -= hexnumlen (memaddr);
c906108c 7037
a76d924d 7038 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 7039
a76d924d
DJ
7040 strcat (rs->buf, header);
7041 p = rs->buf + strlen (header);
7042
7043 /* Compute a best guess of the number of bytes actually transfered. */
7044 if (packet_format == 'X')
c906108c 7045 {
23860348 7046 /* Best guess at number of bytes that will fit. */
124e13d9 7047 todo_units = min (len_units, payload_capacity_bytes / unit_size);
a76d924d 7048 if (use_length)
124e13d9
SM
7049 payload_capacity_bytes -= hexnumlen (todo_units);
7050 todo_units = min (todo_units, payload_capacity_bytes / unit_size);
a76d924d
DJ
7051 }
7052 else
7053 {
124e13d9
SM
7054 /* Number of bytes that will fit. */
7055 todo_units = min (len_units, (payload_capacity_bytes / unit_size) / 2);
a76d924d 7056 if (use_length)
124e13d9
SM
7057 payload_capacity_bytes -= hexnumlen (todo_units);
7058 todo_units = min (todo_units, (payload_capacity_bytes / unit_size) / 2);
917317f4 7059 }
a76d924d 7060
124e13d9 7061 if (todo_units <= 0)
3de11b2e 7062 internal_error (__FILE__, __LINE__,
405f8e94 7063 _("minimum packet size too small to write data"));
802188a7 7064
6765f3e5
DJ
7065 /* If we already need another packet, then try to align the end
7066 of this packet to a useful boundary. */
124e13d9
SM
7067 if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
7068 todo_units = align_for_efficient_write (todo_units, memaddr);
6765f3e5 7069
a257b5bb 7070 /* Append "<memaddr>". */
917317f4
JM
7071 memaddr = remote_address_masked (memaddr);
7072 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 7073
a76d924d
DJ
7074 if (use_length)
7075 {
7076 /* Append ",". */
7077 *p++ = ',';
802188a7 7078
124e13d9
SM
7079 /* Append the length and retain its location and size. It may need to be
7080 adjusted once the packet body has been created. */
a76d924d 7081 plen = p;
124e13d9 7082 plenlen = hexnumstr (p, (ULONGEST) todo_units);
a76d924d
DJ
7083 p += plenlen;
7084 }
a257b5bb
AC
7085
7086 /* Append ":". */
917317f4
JM
7087 *p++ = ':';
7088 *p = '\0';
802188a7 7089
a257b5bb 7090 /* Append the packet body. */
a76d924d 7091 if (packet_format == 'X')
917317f4 7092 {
917317f4
JM
7093 /* Binary mode. Send target system values byte by byte, in
7094 increasing byte addresses. Only escape certain critical
7095 characters. */
124e13d9
SM
7096 payload_length_bytes =
7097 remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
7098 &units_written, payload_capacity_bytes);
6765f3e5 7099
124e13d9 7100 /* If not all TODO units fit, then we'll need another packet. Make
9b7194bc
DJ
7101 a second try to keep the end of the packet aligned. Don't do
7102 this if the packet is tiny. */
124e13d9 7103 if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
6765f3e5 7104 {
124e13d9
SM
7105 int new_todo_units;
7106
7107 new_todo_units = align_for_efficient_write (units_written, memaddr);
7108
7109 if (new_todo_units != units_written)
7110 payload_length_bytes =
7111 remote_escape_output (myaddr, new_todo_units, unit_size,
7112 (gdb_byte *) p, &units_written,
7113 payload_capacity_bytes);
6765f3e5
DJ
7114 }
7115
124e13d9
SM
7116 p += payload_length_bytes;
7117 if (use_length && units_written < todo_units)
c906108c 7118 {
802188a7 7119 /* Escape chars have filled up the buffer prematurely,
124e13d9 7120 and we have actually sent fewer units than planned.
917317f4
JM
7121 Fix-up the length field of the packet. Use the same
7122 number of characters as before. */
124e13d9
SM
7123 plen += hexnumnstr (plen, (ULONGEST) units_written,
7124 plenlen);
917317f4 7125 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 7126 }
a76d924d
DJ
7127 }
7128 else
7129 {
917317f4
JM
7130 /* Normal mode: Send target system values byte by byte, in
7131 increasing byte addresses. Each byte is encoded as a two hex
7132 value. */
124e13d9
SM
7133 p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
7134 units_written = todo_units;
c906108c 7135 }
802188a7 7136
2e9f7625 7137 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 7138 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 7139
2e9f7625 7140 if (rs->buf[0] == 'E')
00d84524 7141 return TARGET_XFER_E_IO;
802188a7 7142
124e13d9
SM
7143 /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
7144 send fewer units than we'd planned. */
7145 *xfered_len_units = (ULONGEST) units_written;
9b409511 7146 return TARGET_XFER_OK;
c906108c
SS
7147}
7148
a76d924d
DJ
7149/* Write memory data directly to the remote machine.
7150 This does not inform the data cache; the data cache uses this.
7151 MEMADDR is the address in the remote memory space.
7152 MYADDR is the address of the buffer in our space.
7153 LEN is the number of bytes.
7154
9b409511
YQ
7155 Return the transferred status, error or OK (an
7156 'enum target_xfer_status' value). Save the number of bytes
7157 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 7158
9b409511
YQ
7159static enum target_xfer_status
7160remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
124e13d9 7161 int unit_size, ULONGEST *xfered_len)
a76d924d
DJ
7162{
7163 char *packet_format = 0;
7164
7165 /* Check whether the target supports binary download. */
7166 check_binary_download (memaddr);
7167
4082afcc 7168 switch (packet_support (PACKET_X))
a76d924d
DJ
7169 {
7170 case PACKET_ENABLE:
7171 packet_format = "X";
7172 break;
7173 case PACKET_DISABLE:
7174 packet_format = "M";
7175 break;
7176 case PACKET_SUPPORT_UNKNOWN:
7177 internal_error (__FILE__, __LINE__,
7178 _("remote_write_bytes: bad internal state"));
7179 default:
7180 internal_error (__FILE__, __LINE__, _("bad switch"));
7181 }
7182
7183 return remote_write_bytes_aux (packet_format,
124e13d9 7184 memaddr, myaddr, len, unit_size, xfered_len,
9b409511 7185 packet_format[0], 1);
a76d924d
DJ
7186}
7187
9217e74e
YQ
7188/* Read memory data directly from the remote machine.
7189 This does not use the data cache; the data cache uses this.
7190 MEMADDR is the address in the remote memory space.
7191 MYADDR is the address of the buffer in our space.
124e13d9
SM
7192 LEN_UNITS is the number of addressable memory units to read..
7193 UNIT_SIZE is the length in bytes of an addressable unit.
9217e74e
YQ
7194
7195 Return the transferred status, error or OK (an
7196 'enum target_xfer_status' value). Save the number of bytes
124e13d9
SM
7197 transferred in *XFERED_LEN_UNITS.
7198
7199 See the comment of remote_write_bytes_aux for an example of
7200 memory read/write exchange between gdb and the stub. */
9217e74e
YQ
7201
7202static enum target_xfer_status
124e13d9
SM
7203remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len_units,
7204 int unit_size, ULONGEST *xfered_len_units)
9217e74e
YQ
7205{
7206 struct remote_state *rs = get_remote_state ();
124e13d9 7207 int buf_size_bytes; /* Max size of packet output buffer. */
9217e74e 7208 char *p;
124e13d9
SM
7209 int todo_units;
7210 int decoded_bytes;
9217e74e 7211
124e13d9 7212 buf_size_bytes = get_memory_read_packet_size ();
9217e74e
YQ
7213 /* The packet buffer will be large enough for the payload;
7214 get_memory_packet_size ensures this. */
7215
124e13d9
SM
7216 /* Number of units that will fit. */
7217 todo_units = min (len_units, (buf_size_bytes / unit_size) / 2);
9217e74e
YQ
7218
7219 /* Construct "m"<memaddr>","<len>". */
7220 memaddr = remote_address_masked (memaddr);
7221 p = rs->buf;
7222 *p++ = 'm';
7223 p += hexnumstr (p, (ULONGEST) memaddr);
7224 *p++ = ',';
124e13d9 7225 p += hexnumstr (p, (ULONGEST) todo_units);
9217e74e
YQ
7226 *p = '\0';
7227 putpkt (rs->buf);
7228 getpkt (&rs->buf, &rs->buf_size, 0);
7229 if (rs->buf[0] == 'E'
7230 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
7231 && rs->buf[3] == '\0')
7232 return TARGET_XFER_E_IO;
7233 /* Reply describes memory byte by byte, each byte encoded as two hex
7234 characters. */
7235 p = rs->buf;
124e13d9 7236 decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
9217e74e 7237 /* Return what we have. Let higher layers handle partial reads. */
124e13d9 7238 *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
9217e74e
YQ
7239 return TARGET_XFER_OK;
7240}
7241
b55fbac4
YQ
7242/* Using the set of read-only target sections of remote, read live
7243 read-only memory.
8acf9577
YQ
7244
7245 For interface/parameters/return description see target.h,
7246 to_xfer_partial. */
7247
7248static enum target_xfer_status
b55fbac4
YQ
7249remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
7250 ULONGEST memaddr, ULONGEST len,
124e13d9 7251 int unit_size, ULONGEST *xfered_len)
8acf9577
YQ
7252{
7253 struct target_section *secp;
7254 struct target_section_table *table;
7255
7256 secp = target_section_by_addr (ops, memaddr);
7257 if (secp != NULL
7258 && (bfd_get_section_flags (secp->the_bfd_section->owner,
7259 secp->the_bfd_section)
7260 & SEC_READONLY))
7261 {
7262 struct target_section *p;
7263 ULONGEST memend = memaddr + len;
7264
7265 table = target_get_section_table (ops);
7266
7267 for (p = table->sections; p < table->sections_end; p++)
7268 {
7269 if (memaddr >= p->addr)
7270 {
7271 if (memend <= p->endaddr)
7272 {
7273 /* Entire transfer is within this section. */
124e13d9 7274 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7275 xfered_len);
8acf9577
YQ
7276 }
7277 else if (memaddr >= p->endaddr)
7278 {
7279 /* This section ends before the transfer starts. */
7280 continue;
7281 }
7282 else
7283 {
7284 /* This section overlaps the transfer. Just do half. */
7285 len = p->endaddr - memaddr;
124e13d9 7286 return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
b55fbac4 7287 xfered_len);
8acf9577
YQ
7288 }
7289 }
7290 }
7291 }
7292
7293 return TARGET_XFER_EOF;
7294}
7295
9217e74e
YQ
7296/* Similar to remote_read_bytes_1, but it reads from the remote stub
7297 first if the requested memory is unavailable in traceframe.
7298 Otherwise, fall back to remote_read_bytes_1. */
c906108c 7299
9b409511 7300static enum target_xfer_status
8acf9577 7301remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
124e13d9
SM
7302 gdb_byte *myaddr, ULONGEST len, int unit_size,
7303 ULONGEST *xfered_len)
c906108c 7304{
6b6aa828 7305 if (len == 0)
96c4f946 7306 return TARGET_XFER_EOF;
b2182ed2 7307
8acf9577
YQ
7308 if (get_traceframe_number () != -1)
7309 {
7310 VEC(mem_range_s) *available;
7311
7312 /* If we fail to get the set of available memory, then the
7313 target does not support querying traceframe info, and so we
7314 attempt reading from the traceframe anyway (assuming the
7315 target implements the old QTro packet then). */
7316 if (traceframe_available_memory (&available, memaddr, len))
7317 {
7318 struct cleanup *old_chain;
7319
7320 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
7321
7322 if (VEC_empty (mem_range_s, available)
7323 || VEC_index (mem_range_s, available, 0)->start != memaddr)
7324 {
7325 enum target_xfer_status res;
7326
7327 /* Don't read into the traceframe's available
7328 memory. */
7329 if (!VEC_empty (mem_range_s, available))
7330 {
7331 LONGEST oldlen = len;
7332
7333 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
7334 gdb_assert (len <= oldlen);
7335 }
7336
7337 do_cleanups (old_chain);
7338
7339 /* This goes through the topmost target again. */
b55fbac4 7340 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
124e13d9 7341 len, unit_size, xfered_len);
8acf9577
YQ
7342 if (res == TARGET_XFER_OK)
7343 return TARGET_XFER_OK;
7344 else
7345 {
7346 /* No use trying further, we know some memory starting
7347 at MEMADDR isn't available. */
7348 *xfered_len = len;
7349 return TARGET_XFER_UNAVAILABLE;
7350 }
7351 }
7352
7353 /* Don't try to read more than how much is available, in
7354 case the target implements the deprecated QTro packet to
7355 cater for older GDBs (the target's knowledge of read-only
7356 sections may be outdated by now). */
7357 len = VEC_index (mem_range_s, available, 0)->length;
7358
7359 do_cleanups (old_chain);
7360 }
7361 }
7362
124e13d9 7363 return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
c906108c 7364}
74531fed 7365
c906108c 7366\f
c906108c 7367
a76d924d
DJ
7368/* Sends a packet with content determined by the printf format string
7369 FORMAT and the remaining arguments, then gets the reply. Returns
7370 whether the packet was a success, a failure, or unknown. */
7371
77b64a49
PA
7372static enum packet_result remote_send_printf (const char *format, ...)
7373 ATTRIBUTE_PRINTF (1, 2);
7374
2c0b251b 7375static enum packet_result
a76d924d
DJ
7376remote_send_printf (const char *format, ...)
7377{
7378 struct remote_state *rs = get_remote_state ();
7379 int max_size = get_remote_packet_size ();
a76d924d 7380 va_list ap;
a744cf53 7381
a76d924d
DJ
7382 va_start (ap, format);
7383
7384 rs->buf[0] = '\0';
7385 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 7386 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
7387
7388 if (putpkt (rs->buf) < 0)
7389 error (_("Communication problem with target."));
7390
7391 rs->buf[0] = '\0';
7392 getpkt (&rs->buf, &rs->buf_size, 0);
7393
7394 return packet_check_result (rs->buf);
7395}
7396
7397static void
7398restore_remote_timeout (void *p)
7399{
7400 int value = *(int *)p;
a744cf53 7401
a76d924d
DJ
7402 remote_timeout = value;
7403}
7404
7405/* Flash writing can take quite some time. We'll set
7406 effectively infinite timeout for flash operations.
7407 In future, we'll need to decide on a better approach. */
7408static const int remote_flash_timeout = 1000;
7409
7410static void
7411remote_flash_erase (struct target_ops *ops,
7412 ULONGEST address, LONGEST length)
7413{
f5656ead 7414 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d
DJ
7415 int saved_remote_timeout = remote_timeout;
7416 enum packet_result ret;
a76d924d
DJ
7417 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7418 &saved_remote_timeout);
a744cf53 7419
a76d924d
DJ
7420 remote_timeout = remote_flash_timeout;
7421
7422 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 7423 phex (address, addr_size),
a76d924d
DJ
7424 phex (length, 4));
7425 switch (ret)
7426 {
7427 case PACKET_UNKNOWN:
7428 error (_("Remote target does not support flash erase"));
7429 case PACKET_ERROR:
7430 error (_("Error erasing flash with vFlashErase packet"));
7431 default:
7432 break;
7433 }
7434
7435 do_cleanups (back_to);
7436}
7437
9b409511
YQ
7438static enum target_xfer_status
7439remote_flash_write (struct target_ops *ops, ULONGEST address,
7440 ULONGEST length, ULONGEST *xfered_len,
7441 const gdb_byte *data)
a76d924d
DJ
7442{
7443 int saved_remote_timeout = remote_timeout;
9b409511 7444 enum target_xfer_status ret;
a76d924d 7445 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
9b409511 7446 &saved_remote_timeout);
a76d924d
DJ
7447
7448 remote_timeout = remote_flash_timeout;
124e13d9 7449 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
9b409511 7450 xfered_len,'X', 0);
a76d924d
DJ
7451 do_cleanups (back_to);
7452
7453 return ret;
7454}
7455
7456static void
7457remote_flash_done (struct target_ops *ops)
7458{
7459 int saved_remote_timeout = remote_timeout;
7460 int ret;
7461 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7462 &saved_remote_timeout);
7463
7464 remote_timeout = remote_flash_timeout;
7465 ret = remote_send_printf ("vFlashDone");
7466 do_cleanups (back_to);
7467
7468 switch (ret)
7469 {
7470 case PACKET_UNKNOWN:
7471 error (_("Remote target does not support vFlashDone"));
7472 case PACKET_ERROR:
7473 error (_("Error finishing flash operation"));
7474 default:
7475 break;
7476 }
7477}
7478
c906108c 7479static void
fba45db2 7480remote_files_info (struct target_ops *ignore)
c906108c
SS
7481{
7482 puts_filtered ("Debugging a target over a serial line.\n");
7483}
7484\f
7485/* Stuff for dealing with the packets which are part of this protocol.
7486 See comment at top of file for details. */
7487
1927e618
PA
7488/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7489 error to higher layers. Called when a serial error is detected.
7490 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
7491 the system error message for errno at function entry and final dot
7492 for output compatibility with throw_perror_with_name. */
1927e618
PA
7493
7494static void
7495unpush_and_perror (const char *string)
7496{
d6cb50a2 7497 int saved_errno = errno;
1927e618
PA
7498
7499 remote_unpush_target ();
d6cb50a2
JK
7500 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7501 safe_strerror (saved_errno));
1927e618
PA
7502}
7503
0876f84a 7504/* Read a single character from the remote end. */
c906108c
SS
7505
7506static int
fba45db2 7507readchar (int timeout)
c906108c
SS
7508{
7509 int ch;
5d93a237 7510 struct remote_state *rs = get_remote_state ();
c906108c 7511
5d93a237 7512 ch = serial_readchar (rs->remote_desc, timeout);
c906108c 7513
2acceee2 7514 if (ch >= 0)
0876f84a 7515 return ch;
2acceee2
JM
7516
7517 switch ((enum serial_rc) ch)
c906108c
SS
7518 {
7519 case SERIAL_EOF:
78a095c3 7520 remote_unpush_target ();
598d3636 7521 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 7522 /* no return */
c906108c 7523 case SERIAL_ERROR:
1927e618
PA
7524 unpush_and_perror (_("Remote communication error. "
7525 "Target disconnected."));
2acceee2 7526 /* no return */
c906108c 7527 case SERIAL_TIMEOUT:
2acceee2 7528 break;
c906108c 7529 }
2acceee2 7530 return ch;
c906108c
SS
7531}
7532
c33e31fd
PA
7533/* Wrapper for serial_write that closes the target and throws if
7534 writing fails. */
7535
7536static void
7537remote_serial_write (const char *str, int len)
7538{
5d93a237
TT
7539 struct remote_state *rs = get_remote_state ();
7540
7541 if (serial_write (rs->remote_desc, str, len))
c33e31fd 7542 {
1927e618
PA
7543 unpush_and_perror (_("Remote communication error. "
7544 "Target disconnected."));
c33e31fd
PA
7545 }
7546}
7547
6d820c5c
DJ
7548/* Send the command in *BUF to the remote machine, and read the reply
7549 into *BUF. Report an error if we get an error reply. Resize
7550 *BUF using xrealloc if necessary to hold the result, and update
7551 *SIZEOF_BUF. */
c906108c
SS
7552
7553static void
6d820c5c
DJ
7554remote_send (char **buf,
7555 long *sizeof_buf)
c906108c 7556{
6d820c5c 7557 putpkt (*buf);
c2d11a7d 7558 getpkt (buf, sizeof_buf, 0);
c906108c 7559
6d820c5c
DJ
7560 if ((*buf)[0] == 'E')
7561 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
7562}
7563
6e5abd65
PA
7564/* Return a pointer to an xmalloc'ed string representing an escaped
7565 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7566 etc. The caller is responsible for releasing the returned
7567 memory. */
7568
7569static char *
7570escape_buffer (const char *buf, int n)
7571{
7572 struct cleanup *old_chain;
7573 struct ui_file *stb;
7574 char *str;
6e5abd65
PA
7575
7576 stb = mem_fileopen ();
7577 old_chain = make_cleanup_ui_file_delete (stb);
7578
6ef284bd 7579 fputstrn_unfiltered (buf, n, '\\', stb);
759ef836 7580 str = ui_file_xstrdup (stb, NULL);
6e5abd65
PA
7581 do_cleanups (old_chain);
7582 return str;
7583}
7584
c906108c
SS
7585/* Display a null-terminated packet on stdout, for debugging, using C
7586 string notation. */
7587
7588static void
baa336ce 7589print_packet (const char *buf)
c906108c
SS
7590{
7591 puts_filtered ("\"");
43e526b9 7592 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
7593 puts_filtered ("\"");
7594}
7595
7596int
baa336ce 7597putpkt (const char *buf)
c906108c
SS
7598{
7599 return putpkt_binary (buf, strlen (buf));
7600}
7601
7602/* Send a packet to the remote machine, with error checking. The data
23860348 7603 of the packet is in BUF. The string in BUF can be at most
ea9c271d 7604 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
7605 and for a possible /0 if we are debugging (remote_debug) and want
7606 to print the sent packet as a string. */
c906108c
SS
7607
7608static int
baa336ce 7609putpkt_binary (const char *buf, int cnt)
c906108c 7610{
2d717e4f 7611 struct remote_state *rs = get_remote_state ();
c906108c
SS
7612 int i;
7613 unsigned char csum = 0;
11cf8741 7614 char *buf2 = alloca (cnt + 6);
085dd6e6 7615
c906108c
SS
7616 int ch;
7617 int tcount = 0;
7618 char *p;
dd61ec5c 7619 char *message;
c906108c 7620
e24a49d8
PA
7621 /* Catch cases like trying to read memory or listing threads while
7622 we're waiting for a stop reply. The remote server wouldn't be
7623 ready to handle this request, so we'd hang and timeout. We don't
7624 have to worry about this in synchronous mode, because in that
7625 case it's not possible to issue a command while the target is
74531fed
PA
7626 running. This is not a problem in non-stop mode, because in that
7627 case, the stub is always ready to process serial input. */
d9d41e78 7628 if (!non_stop && target_is_async_p () && rs->waiting_for_stop_reply)
9597b22a
DE
7629 {
7630 error (_("Cannot execute this command while the target is running.\n"
7631 "Use the \"interrupt\" command to stop the target\n"
7632 "and then try again."));
7633 }
e24a49d8 7634
2d717e4f
DJ
7635 /* We're sending out a new packet. Make sure we don't look at a
7636 stale cached response. */
7637 rs->cached_wait_status = 0;
7638
c906108c
SS
7639 /* Copy the packet into buffer BUF2, encapsulating it
7640 and giving it a checksum. */
7641
c906108c
SS
7642 p = buf2;
7643 *p++ = '$';
7644
7645 for (i = 0; i < cnt; i++)
7646 {
7647 csum += buf[i];
7648 *p++ = buf[i];
7649 }
7650 *p++ = '#';
7651 *p++ = tohex ((csum >> 4) & 0xf);
7652 *p++ = tohex (csum & 0xf);
7653
7654 /* Send it over and over until we get a positive ack. */
7655
7656 while (1)
7657 {
7658 int started_error_output = 0;
7659
7660 if (remote_debug)
7661 {
6e5abd65
PA
7662 struct cleanup *old_chain;
7663 char *str;
7664
c906108c 7665 *p = '\0';
6e5abd65
PA
7666 str = escape_buffer (buf2, p - buf2);
7667 old_chain = make_cleanup (xfree, str);
7668 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
0f71a2f6 7669 gdb_flush (gdb_stdlog);
6e5abd65 7670 do_cleanups (old_chain);
c906108c 7671 }
c33e31fd 7672 remote_serial_write (buf2, p - buf2);
c906108c 7673
a6f3e723
SL
7674 /* If this is a no acks version of the remote protocol, send the
7675 packet and move on. */
7676 if (rs->noack_mode)
7677 break;
7678
74531fed
PA
7679 /* Read until either a timeout occurs (-2) or '+' is read.
7680 Handle any notification that arrives in the mean time. */
c906108c
SS
7681 while (1)
7682 {
7683 ch = readchar (remote_timeout);
7684
c5aa993b 7685 if (remote_debug)
c906108c
SS
7686 {
7687 switch (ch)
7688 {
7689 case '+':
1216fa2c 7690 case '-':
c906108c
SS
7691 case SERIAL_TIMEOUT:
7692 case '$':
74531fed 7693 case '%':
c906108c
SS
7694 if (started_error_output)
7695 {
7696 putchar_unfiltered ('\n');
7697 started_error_output = 0;
7698 }
7699 }
7700 }
7701
7702 switch (ch)
7703 {
7704 case '+':
7705 if (remote_debug)
0f71a2f6 7706 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 7707 return 1;
1216fa2c
AC
7708 case '-':
7709 if (remote_debug)
7710 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 7711 /* FALLTHROUGH */
c906108c 7712 case SERIAL_TIMEOUT:
c5aa993b 7713 tcount++;
c906108c
SS
7714 if (tcount > 3)
7715 return 0;
23860348 7716 break; /* Retransmit buffer. */
c906108c
SS
7717 case '$':
7718 {
40e3f985 7719 if (remote_debug)
2bc416ba 7720 fprintf_unfiltered (gdb_stdlog,
23860348 7721 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
7722 /* It's probably an old response sent because an ACK
7723 was lost. Gobble up the packet and ack it so it
7724 doesn't get retransmitted when we resend this
7725 packet. */
6d820c5c 7726 skip_frame ();
c33e31fd 7727 remote_serial_write ("+", 1);
23860348 7728 continue; /* Now, go look for +. */
c906108c 7729 }
74531fed
PA
7730
7731 case '%':
7732 {
7733 int val;
7734
7735 /* If we got a notification, handle it, and go back to looking
7736 for an ack. */
7737 /* We've found the start of a notification. Now
7738 collect the data. */
7739 val = read_frame (&rs->buf, &rs->buf_size);
7740 if (val >= 0)
7741 {
7742 if (remote_debug)
7743 {
6e5abd65
PA
7744 struct cleanup *old_chain;
7745 char *str;
7746
7747 str = escape_buffer (rs->buf, val);
7748 old_chain = make_cleanup (xfree, str);
7749 fprintf_unfiltered (gdb_stdlog,
7750 " Notification received: %s\n",
7751 str);
7752 do_cleanups (old_chain);
74531fed 7753 }
5965e028 7754 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
7755 /* We're in sync now, rewait for the ack. */
7756 tcount = 0;
7757 }
7758 else
7759 {
7760 if (remote_debug)
7761 {
7762 if (!started_error_output)
7763 {
7764 started_error_output = 1;
7765 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7766 }
7767 fputc_unfiltered (ch & 0177, gdb_stdlog);
7768 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7769 }
7770 }
7771 continue;
7772 }
7773 /* fall-through */
c906108c
SS
7774 default:
7775 if (remote_debug)
7776 {
7777 if (!started_error_output)
7778 {
7779 started_error_output = 1;
0f71a2f6 7780 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 7781 }
0f71a2f6 7782 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
7783 }
7784 continue;
7785 }
23860348 7786 break; /* Here to retransmit. */
c906108c
SS
7787 }
7788
7789#if 0
7790 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
7791 able to get out next time we call QUIT, without anything as
7792 violent as interrupt_query. If we want to provide a way out of
7793 here without getting to the next QUIT, it should be based on
7794 hitting ^C twice as in remote_wait. */
c906108c
SS
7795 if (quit_flag)
7796 {
7797 quit_flag = 0;
7798 interrupt_query ();
7799 }
7800#endif
7801 }
a6f3e723 7802 return 0;
c906108c
SS
7803}
7804
6d820c5c
DJ
7805/* Come here after finding the start of a frame when we expected an
7806 ack. Do our best to discard the rest of this packet. */
7807
7808static void
7809skip_frame (void)
7810{
7811 int c;
7812
7813 while (1)
7814 {
7815 c = readchar (remote_timeout);
7816 switch (c)
7817 {
7818 case SERIAL_TIMEOUT:
7819 /* Nothing we can do. */
7820 return;
7821 case '#':
7822 /* Discard the two bytes of checksum and stop. */
7823 c = readchar (remote_timeout);
7824 if (c >= 0)
7825 c = readchar (remote_timeout);
7826
7827 return;
7828 case '*': /* Run length encoding. */
7829 /* Discard the repeat count. */
7830 c = readchar (remote_timeout);
7831 if (c < 0)
7832 return;
7833 break;
7834 default:
7835 /* A regular character. */
7836 break;
7837 }
7838 }
7839}
7840
c906108c 7841/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
7842 into *BUF, verifying the checksum, length, and handling run-length
7843 compression. NUL terminate the buffer. If there is not enough room,
7844 expand *BUF using xrealloc.
c906108c 7845
c2d11a7d
JM
7846 Returns -1 on error, number of characters in buffer (ignoring the
7847 trailing NULL) on success. (could be extended to return one of the
23860348 7848 SERIAL status indications). */
c2d11a7d
JM
7849
7850static long
6d820c5c
DJ
7851read_frame (char **buf_p,
7852 long *sizeof_buf)
c906108c
SS
7853{
7854 unsigned char csum;
c2d11a7d 7855 long bc;
c906108c 7856 int c;
6d820c5c 7857 char *buf = *buf_p;
a6f3e723 7858 struct remote_state *rs = get_remote_state ();
c906108c
SS
7859
7860 csum = 0;
c2d11a7d 7861 bc = 0;
c906108c
SS
7862
7863 while (1)
7864 {
7865 c = readchar (remote_timeout);
c906108c
SS
7866 switch (c)
7867 {
7868 case SERIAL_TIMEOUT:
7869 if (remote_debug)
0f71a2f6 7870 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 7871 return -1;
c906108c
SS
7872 case '$':
7873 if (remote_debug)
0f71a2f6
JM
7874 fputs_filtered ("Saw new packet start in middle of old one\n",
7875 gdb_stdlog);
23860348 7876 return -1; /* Start a new packet, count retries. */
c906108c
SS
7877 case '#':
7878 {
7879 unsigned char pktcsum;
e1b09194
AC
7880 int check_0 = 0;
7881 int check_1 = 0;
c906108c 7882
c2d11a7d 7883 buf[bc] = '\0';
c906108c 7884
e1b09194
AC
7885 check_0 = readchar (remote_timeout);
7886 if (check_0 >= 0)
7887 check_1 = readchar (remote_timeout);
802188a7 7888
e1b09194
AC
7889 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7890 {
7891 if (remote_debug)
2bc416ba 7892 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 7893 gdb_stdlog);
e1b09194
AC
7894 return -1;
7895 }
7896 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
7897 {
7898 if (remote_debug)
2bc416ba 7899 fputs_filtered ("Communication error in checksum\n",
23860348 7900 gdb_stdlog);
40e3f985
FN
7901 return -1;
7902 }
c906108c 7903
a6f3e723
SL
7904 /* Don't recompute the checksum; with no ack packets we
7905 don't have any way to indicate a packet retransmission
7906 is necessary. */
7907 if (rs->noack_mode)
7908 return bc;
7909
e1b09194 7910 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 7911 if (csum == pktcsum)
c2d11a7d 7912 return bc;
c906108c 7913
c5aa993b 7914 if (remote_debug)
c906108c 7915 {
6e5abd65
PA
7916 struct cleanup *old_chain;
7917 char *str;
7918
7919 str = escape_buffer (buf, bc);
7920 old_chain = make_cleanup (xfree, str);
7921 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
7922 "Bad checksum, sentsum=0x%x, "
7923 "csum=0x%x, buf=%s\n",
6e5abd65
PA
7924 pktcsum, csum, str);
7925 do_cleanups (old_chain);
c906108c 7926 }
c2d11a7d 7927 /* Number of characters in buffer ignoring trailing
23860348 7928 NULL. */
c2d11a7d 7929 return -1;
c906108c 7930 }
23860348 7931 case '*': /* Run length encoding. */
c2c6d25f
JM
7932 {
7933 int repeat;
c906108c 7934
a744cf53 7935 csum += c;
b4501125
AC
7936 c = readchar (remote_timeout);
7937 csum += c;
23860348 7938 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 7939
23860348 7940 /* The character before ``*'' is repeated. */
c2d11a7d 7941
6d820c5c 7942 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 7943 {
6d820c5c
DJ
7944 if (bc + repeat - 1 >= *sizeof_buf - 1)
7945 {
7946 /* Make some more room in the buffer. */
7947 *sizeof_buf += repeat;
7948 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7949 buf = *buf_p;
7950 }
7951
c2d11a7d
JM
7952 memset (&buf[bc], buf[bc - 1], repeat);
7953 bc += repeat;
c2c6d25f
JM
7954 continue;
7955 }
7956
c2d11a7d 7957 buf[bc] = '\0';
6d820c5c 7958 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 7959 return -1;
c2c6d25f 7960 }
c906108c 7961 default:
6d820c5c 7962 if (bc >= *sizeof_buf - 1)
c906108c 7963 {
6d820c5c
DJ
7964 /* Make some more room in the buffer. */
7965 *sizeof_buf *= 2;
7966 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7967 buf = *buf_p;
c906108c
SS
7968 }
7969
6d820c5c
DJ
7970 buf[bc++] = c;
7971 csum += c;
7972 continue;
c906108c
SS
7973 }
7974 }
7975}
7976
7977/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7978 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7979 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7980 rather than timing out; this is used (in synchronous mode) to wait
7981 for a target that is is executing user code to stop. */
d9fcf2fb
JM
7982/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7983 don't have to change all the calls to getpkt to deal with the
7984 return value, because at the moment I don't know what the right
23860348 7985 thing to do it for those. */
c906108c 7986void
6d820c5c
DJ
7987getpkt (char **buf,
7988 long *sizeof_buf,
c2d11a7d 7989 int forever)
d9fcf2fb
JM
7990{
7991 int timed_out;
7992
7993 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7994}
7995
7996
7997/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7998 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7999 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
8000 rather than timing out; this is used (in synchronous mode) to wait
8001 for a target that is is executing user code to stop. If FOREVER ==
8002 0, this function is allowed to time out gracefully and return an
74531fed
PA
8003 indication of this to the caller. Otherwise return the number of
8004 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
8005 enough reason to return to the caller. *IS_NOTIF is an output
8006 boolean that indicates whether *BUF holds a notification or not
8007 (a regular packet). */
74531fed 8008
3172dc30 8009static int
74531fed 8010getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 8011 int expecting_notif, int *is_notif)
c906108c 8012{
2d717e4f 8013 struct remote_state *rs = get_remote_state ();
c906108c
SS
8014 int c;
8015 int tries;
8016 int timeout;
df4b58fe 8017 int val = -1;
c906108c 8018
2d717e4f
DJ
8019 /* We're reading a new response. Make sure we don't look at a
8020 previously cached response. */
8021 rs->cached_wait_status = 0;
8022
6d820c5c 8023 strcpy (*buf, "timeout");
c906108c
SS
8024
8025 if (forever)
74531fed
PA
8026 timeout = watchdog > 0 ? watchdog : -1;
8027 else if (expecting_notif)
8028 timeout = 0; /* There should already be a char in the buffer. If
8029 not, bail out. */
c906108c
SS
8030 else
8031 timeout = remote_timeout;
8032
8033#define MAX_TRIES 3
8034
74531fed
PA
8035 /* Process any number of notifications, and then return when
8036 we get a packet. */
8037 for (;;)
c906108c 8038 {
d9c43928 8039 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
8040 times. */
8041 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 8042 {
74531fed
PA
8043 /* This can loop forever if the remote side sends us
8044 characters continuously, but if it pauses, we'll get
8045 SERIAL_TIMEOUT from readchar because of timeout. Then
8046 we'll count that as a retry.
8047
8048 Note that even when forever is set, we will only wait
8049 forever prior to the start of a packet. After that, we
8050 expect characters to arrive at a brisk pace. They should
8051 show up within remote_timeout intervals. */
8052 do
8053 c = readchar (timeout);
8054 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
8055
8056 if (c == SERIAL_TIMEOUT)
8057 {
74531fed
PA
8058 if (expecting_notif)
8059 return -1; /* Don't complain, it's normal to not get
8060 anything in this case. */
8061
23860348 8062 if (forever) /* Watchdog went off? Kill the target. */
c906108c 8063 {
2acceee2 8064 QUIT;
78a095c3 8065 remote_unpush_target ();
598d3636
JK
8066 throw_error (TARGET_CLOSE_ERROR,
8067 _("Watchdog timeout has expired. "
8068 "Target detached."));
c906108c 8069 }
c906108c 8070 if (remote_debug)
0f71a2f6 8071 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 8072 }
74531fed
PA
8073 else
8074 {
8075 /* We've found the start of a packet or notification.
8076 Now collect the data. */
8077 val = read_frame (buf, sizeof_buf);
8078 if (val >= 0)
8079 break;
8080 }
8081
c33e31fd 8082 remote_serial_write ("-", 1);
c906108c 8083 }
c906108c 8084
74531fed
PA
8085 if (tries > MAX_TRIES)
8086 {
8087 /* We have tried hard enough, and just can't receive the
8088 packet/notification. Give up. */
8089 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 8090
74531fed
PA
8091 /* Skip the ack char if we're in no-ack mode. */
8092 if (!rs->noack_mode)
c33e31fd 8093 remote_serial_write ("+", 1);
74531fed
PA
8094 return -1;
8095 }
c906108c 8096
74531fed
PA
8097 /* If we got an ordinary packet, return that to our caller. */
8098 if (c == '$')
c906108c
SS
8099 {
8100 if (remote_debug)
43e526b9 8101 {
6e5abd65
PA
8102 struct cleanup *old_chain;
8103 char *str;
8104
8105 str = escape_buffer (*buf, val);
8106 old_chain = make_cleanup (xfree, str);
8107 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
8108 do_cleanups (old_chain);
43e526b9 8109 }
a6f3e723
SL
8110
8111 /* Skip the ack char if we're in no-ack mode. */
8112 if (!rs->noack_mode)
c33e31fd 8113 remote_serial_write ("+", 1);
fee9eda9
YQ
8114 if (is_notif != NULL)
8115 *is_notif = 0;
0876f84a 8116 return val;
c906108c
SS
8117 }
8118
74531fed
PA
8119 /* If we got a notification, handle it, and go back to looking
8120 for a packet. */
8121 else
8122 {
8123 gdb_assert (c == '%');
8124
8125 if (remote_debug)
8126 {
6e5abd65
PA
8127 struct cleanup *old_chain;
8128 char *str;
8129
8130 str = escape_buffer (*buf, val);
8131 old_chain = make_cleanup (xfree, str);
8132 fprintf_unfiltered (gdb_stdlog,
8133 " Notification received: %s\n",
8134 str);
8135 do_cleanups (old_chain);
74531fed 8136 }
fee9eda9
YQ
8137 if (is_notif != NULL)
8138 *is_notif = 1;
c906108c 8139
5965e028 8140 handle_notification (rs->notif_state, *buf);
c906108c 8141
74531fed 8142 /* Notifications require no acknowledgement. */
a6f3e723 8143
74531fed 8144 if (expecting_notif)
fee9eda9 8145 return val;
74531fed
PA
8146 }
8147 }
8148}
8149
8150static int
8151getpkt_sane (char **buf, long *sizeof_buf, int forever)
8152{
fee9eda9 8153 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
8154}
8155
8156static int
fee9eda9
YQ
8157getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
8158 int *is_notif)
74531fed 8159{
fee9eda9
YQ
8160 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
8161 is_notif);
c906108c 8162}
74531fed 8163
cbb8991c
DB
8164/* Check whether EVENT is a fork event for the process specified
8165 by the pid passed in DATA, and if it is, kill the fork child. */
8166
8167static int
8168kill_child_of_pending_fork (QUEUE (stop_reply_p) *q,
8169 QUEUE_ITER (stop_reply_p) *iter,
8170 stop_reply_p event,
8171 void *data)
8172{
8173 struct queue_iter_param *param = data;
8174 int parent_pid = *(int *) param->input;
8175
8176 if (is_pending_fork_parent (&event->ws, parent_pid, event->ptid))
8177 {
8178 struct remote_state *rs = get_remote_state ();
8179 int child_pid = ptid_get_pid (event->ws.value.related_pid);
8180 int res;
8181
8182 res = remote_vkill (child_pid, rs);
8183 if (res != 0)
8184 error (_("Can't kill fork child process %d"), child_pid);
8185 }
8186
8187 return 1;
8188}
8189
8190/* Kill any new fork children of process PID that haven't been
8191 processed by follow_fork. */
8192
8193static void
8194kill_new_fork_children (int pid, struct remote_state *rs)
8195{
8196 struct thread_info *thread;
8197 struct notif_client *notif = &notif_client_stop;
8198 struct queue_iter_param param;
8199
8200 /* Kill the fork child threads of any threads in process PID
8201 that are stopped at a fork event. */
8202 ALL_NON_EXITED_THREADS (thread)
8203 {
8204 struct target_waitstatus *ws = &thread->pending_follow;
8205
8206 if (is_pending_fork_parent (ws, pid, thread->ptid))
8207 {
8208 struct remote_state *rs = get_remote_state ();
8209 int child_pid = ptid_get_pid (ws->value.related_pid);
8210 int res;
8211
8212 res = remote_vkill (child_pid, rs);
8213 if (res != 0)
8214 error (_("Can't kill fork child process %d"), child_pid);
8215 }
8216 }
8217
8218 /* Check for any pending fork events (not reported or processed yet)
8219 in process PID and kill those fork child threads as well. */
8220 remote_notif_get_pending_events (notif);
8221 param.input = &pid;
8222 param.output = NULL;
8223 QUEUE_iterate (stop_reply_p, stop_reply_queue,
8224 kill_child_of_pending_fork, &param);
8225}
8226
c906108c
SS
8227\f
8228static void
7d85a9c0 8229remote_kill (struct target_ops *ops)
43ff13b4 8230{
0fdf84ca
PA
8231
8232 /* Catch errors so the user can quit from gdb even when we
23860348 8233 aren't on speaking terms with the remote system. */
492d29ea 8234 TRY
0fdf84ca
PA
8235 {
8236 putpkt ("k");
8237 }
492d29ea 8238 CATCH (ex, RETURN_MASK_ERROR)
0fdf84ca
PA
8239 {
8240 if (ex.error == TARGET_CLOSE_ERROR)
8241 {
8242 /* If we got an (EOF) error that caused the target
8243 to go away, then we're done, that's what we wanted.
8244 "k" is susceptible to cause a premature EOF, given
8245 that the remote server isn't actually required to
8246 reply to "k", and it can happen that it doesn't
8247 even get to reply ACK to the "k". */
8248 return;
8249 }
8250
8251 /* Otherwise, something went wrong. We didn't actually kill
8252 the target. Just propagate the exception, and let the
8253 user or higher layers decide what to do. */
8254 throw_exception (ex);
8255 }
492d29ea 8256 END_CATCH
43ff13b4 8257
0fdf84ca
PA
8258 /* We've killed the remote end, we get to mourn it. Since this is
8259 target remote, single-process, mourning the inferior also
8260 unpushes remote_ops. */
43ff13b4
JM
8261 target_mourn_inferior ();
8262}
8263
82f73884
PA
8264static int
8265remote_vkill (int pid, struct remote_state *rs)
8266{
4082afcc 8267 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
8268 return -1;
8269
8270 /* Tell the remote target to detach. */
bba74b36 8271 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
8272 putpkt (rs->buf);
8273 getpkt (&rs->buf, &rs->buf_size, 0);
8274
4082afcc
PA
8275 switch (packet_ok (rs->buf,
8276 &remote_protocol_packets[PACKET_vKill]))
8277 {
8278 case PACKET_OK:
8279 return 0;
8280 case PACKET_ERROR:
8281 return 1;
8282 case PACKET_UNKNOWN:
8283 return -1;
8284 default:
8285 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
8286 }
82f73884
PA
8287}
8288
8289static void
7d85a9c0 8290extended_remote_kill (struct target_ops *ops)
82f73884
PA
8291{
8292 int res;
8293 int pid = ptid_get_pid (inferior_ptid);
8294 struct remote_state *rs = get_remote_state ();
8295
cbb8991c
DB
8296 /* If we're stopped while forking and we haven't followed yet, kill the
8297 child task. We need to do this before killing the parent task
8298 because if this is a vfork then the parent will be sleeping. */
8299 kill_new_fork_children (pid, rs);
8300
82f73884 8301 res = remote_vkill (pid, rs);
901f9912 8302 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
82f73884
PA
8303 {
8304 /* Don't try 'k' on a multi-process aware stub -- it has no way
8305 to specify the pid. */
8306
8307 putpkt ("k");
8308#if 0
8309 getpkt (&rs->buf, &rs->buf_size, 0);
8310 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
8311 res = 1;
8312#else
8313 /* Don't wait for it to die. I'm not really sure it matters whether
8314 we do or not. For the existing stubs, kill is a noop. */
8315 res = 0;
8316#endif
8317 }
8318
8319 if (res != 0)
8320 error (_("Can't kill process"));
8321
82f73884
PA
8322 target_mourn_inferior ();
8323}
8324
c906108c 8325static void
20f796c9 8326remote_mourn (struct target_ops *target)
c906108c
SS
8327{
8328 unpush_target (target);
ce5ce7ed 8329
8a2492ee
PA
8330 /* remote_close takes care of doing most of the clean up. */
8331 generic_mourn_inferior ();
c906108c
SS
8332}
8333
2d717e4f 8334static void
20f796c9 8335extended_remote_mourn (struct target_ops *target)
2d717e4f
DJ
8336{
8337 struct remote_state *rs = get_remote_state ();
c906108c 8338
e24a49d8
PA
8339 /* In case we got here due to an error, but we're going to stay
8340 connected. */
8341 rs->waiting_for_stop_reply = 0;
8342
dc1981d7
PA
8343 /* If the current general thread belonged to the process we just
8344 detached from or has exited, the remote side current general
8345 thread becomes undefined. Considering a case like this:
8346
8347 - We just got here due to a detach.
8348 - The process that we're detaching from happens to immediately
8349 report a global breakpoint being hit in non-stop mode, in the
8350 same thread we had selected before.
8351 - GDB attaches to this process again.
8352 - This event happens to be the next event we handle.
8353
8354 GDB would consider that the current general thread didn't need to
8355 be set on the stub side (with Hg), since for all it knew,
8356 GENERAL_THREAD hadn't changed.
8357
8358 Notice that although in all-stop mode, the remote server always
8359 sets the current thread to the thread reporting the stop event,
8360 that doesn't happen in non-stop mode; in non-stop, the stub *must
8361 not* change the current thread when reporting a breakpoint hit,
8362 due to the decoupling of event reporting and event handling.
8363
8364 To keep things simple, we always invalidate our notion of the
8365 current thread. */
47f8a51d 8366 record_currthread (rs, minus_one_ptid);
dc1981d7 8367
2d717e4f
DJ
8368 /* Unlike "target remote", we do not want to unpush the target; then
8369 the next time the user says "run", we won't be connected. */
8370
48aa3c27
PA
8371 /* Call common code to mark the inferior as not running. */
8372 generic_mourn_inferior ();
8373
d729566a 8374 if (!have_inferiors ())
2d717e4f 8375 {
82f73884
PA
8376 if (!remote_multi_process_p (rs))
8377 {
8378 /* Check whether the target is running now - some remote stubs
8379 automatically restart after kill. */
8380 putpkt ("?");
8381 getpkt (&rs->buf, &rs->buf_size, 0);
8382
8383 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
8384 {
3e43a32a
MS
8385 /* Assume that the target has been restarted. Set
8386 inferior_ptid so that bits of core GDB realizes
8387 there's something here, e.g., so that the user can
8388 say "kill" again. */
82f73884
PA
8389 inferior_ptid = magic_null_ptid;
8390 }
82f73884 8391 }
2d717e4f
DJ
8392 }
8393}
c906108c 8394
03583c20 8395static int
2bfc0540 8396extended_remote_supports_disable_randomization (struct target_ops *self)
03583c20 8397{
4082afcc 8398 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
8399}
8400
8401static void
8402extended_remote_disable_randomization (int val)
8403{
8404 struct remote_state *rs = get_remote_state ();
8405 char *reply;
8406
bba74b36
YQ
8407 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
8408 val);
03583c20
UW
8409 putpkt (rs->buf);
8410 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
8411 if (*reply == '\0')
8412 error (_("Target does not support QDisableRandomization."));
8413 if (strcmp (reply, "OK") != 0)
8414 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
8415}
8416
2d717e4f
DJ
8417static int
8418extended_remote_run (char *args)
8419{
8420 struct remote_state *rs = get_remote_state ();
2d717e4f 8421 int len;
c906108c 8422
2d717e4f
DJ
8423 /* If the user has disabled vRun support, or we have detected that
8424 support is not available, do not try it. */
4082afcc 8425 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 8426 return -1;
424163ea 8427
2d717e4f
DJ
8428 strcpy (rs->buf, "vRun;");
8429 len = strlen (rs->buf);
c906108c 8430
2d717e4f
DJ
8431 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
8432 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
8433 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
8434 strlen (remote_exec_file));
2d717e4f 8435
d1a41061 8436 gdb_assert (args != NULL);
2d717e4f
DJ
8437 if (*args)
8438 {
8439 struct cleanup *back_to;
8440 int i;
8441 char **argv;
8442
d1a41061 8443 argv = gdb_buildargv (args);
6e366df1 8444 back_to = make_cleanup_freeargv (argv);
2d717e4f
DJ
8445 for (i = 0; argv[i] != NULL; i++)
8446 {
8447 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
8448 error (_("Argument list too long for run packet"));
8449 rs->buf[len++] = ';';
9f1b45b0
TT
8450 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
8451 strlen (argv[i]));
2d717e4f
DJ
8452 }
8453 do_cleanups (back_to);
8454 }
8455
8456 rs->buf[len++] = '\0';
8457
8458 putpkt (rs->buf);
8459 getpkt (&rs->buf, &rs->buf_size, 0);
8460
4082afcc 8461 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 8462 {
4082afcc 8463 case PACKET_OK:
3405876a 8464 /* We have a wait response. All is well. */
2d717e4f 8465 return 0;
4082afcc
PA
8466 case PACKET_UNKNOWN:
8467 return -1;
8468 case PACKET_ERROR:
2d717e4f
DJ
8469 if (remote_exec_file[0] == '\0')
8470 error (_("Running the default executable on the remote target failed; "
8471 "try \"set remote exec-file\"?"));
8472 else
8473 error (_("Running \"%s\" on the remote target failed"),
8474 remote_exec_file);
4082afcc
PA
8475 default:
8476 gdb_assert_not_reached (_("bad switch"));
2d717e4f 8477 }
c906108c
SS
8478}
8479
2d717e4f
DJ
8480/* In the extended protocol we want to be able to do things like
8481 "run" and have them basically work as expected. So we need
8482 a special create_inferior function. We support changing the
8483 executable file and the command line arguments, but not the
8484 environment. */
8485
43ff13b4 8486static void
77a19445
TT
8487extended_remote_create_inferior (struct target_ops *ops,
8488 char *exec_file, char *args,
8489 char **env, int from_tty)
43ff13b4 8490{
3405876a
PA
8491 int run_worked;
8492 char *stop_reply;
8493 struct remote_state *rs = get_remote_state ();
8494
43ff13b4 8495 /* If running asynchronously, register the target file descriptor
23860348 8496 with the event loop. */
75c99385 8497 if (target_can_async_p ())
6a3753b3 8498 target_async (1);
43ff13b4 8499
03583c20 8500 /* Disable address space randomization if requested (and supported). */
2bfc0540 8501 if (extended_remote_supports_disable_randomization (ops))
03583c20
UW
8502 extended_remote_disable_randomization (disable_randomization);
8503
43ff13b4 8504 /* Now restart the remote server. */
3405876a
PA
8505 run_worked = extended_remote_run (args) != -1;
8506 if (!run_worked)
2d717e4f
DJ
8507 {
8508 /* vRun was not supported. Fail if we need it to do what the
8509 user requested. */
8510 if (remote_exec_file[0])
8511 error (_("Remote target does not support \"set remote exec-file\""));
8512 if (args[0])
8513 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 8514
2d717e4f
DJ
8515 /* Fall back to "R". */
8516 extended_remote_restart ();
8517 }
424163ea 8518
6c95b8df
PA
8519 if (!have_inferiors ())
8520 {
8521 /* Clean up from the last time we ran, before we mark the target
8522 running again. This will mark breakpoints uninserted, and
8523 get_offsets may insert breakpoints. */
8524 init_thread_list ();
8525 init_wait_for_inferior ();
8526 }
45280a52 8527
3405876a
PA
8528 /* vRun's success return is a stop reply. */
8529 stop_reply = run_worked ? rs->buf : NULL;
8530 add_current_inferior_and_thread (stop_reply);
c0a2216e 8531
2d717e4f
DJ
8532 /* Get updated offsets, if the stub uses qOffsets. */
8533 get_offsets ();
2d717e4f 8534}
c906108c 8535\f
c5aa993b 8536
b775012e
LM
8537/* Given a location's target info BP_TGT and the packet buffer BUF, output
8538 the list of conditions (in agent expression bytecode format), if any, the
8539 target needs to evaluate. The output is placed into the packet buffer
bba74b36 8540 started from BUF and ended at BUF_END. */
b775012e
LM
8541
8542static int
8543remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
8544 struct bp_target_info *bp_tgt, char *buf,
8545 char *buf_end)
b775012e
LM
8546{
8547 struct agent_expr *aexpr = NULL;
8548 int i, ix;
8549 char *pkt;
8550 char *buf_start = buf;
8551
8552 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8553 return 0;
8554
8555 buf += strlen (buf);
bba74b36 8556 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
8557 buf++;
8558
8559 /* Send conditions to the target and free the vector. */
8560 for (ix = 0;
8561 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8562 ix++)
8563 {
bba74b36 8564 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e
LM
8565 buf += strlen (buf);
8566 for (i = 0; i < aexpr->len; ++i)
8567 buf = pack_hex_byte (buf, aexpr->buf[i]);
8568 *buf = '\0';
8569 }
b775012e
LM
8570 return 0;
8571}
8572
d3ce09f5
SS
8573static void
8574remote_add_target_side_commands (struct gdbarch *gdbarch,
8575 struct bp_target_info *bp_tgt, char *buf)
8576{
8577 struct agent_expr *aexpr = NULL;
8578 int i, ix;
8579
8580 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8581 return;
8582
8583 buf += strlen (buf);
8584
8585 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8586 buf += strlen (buf);
8587
8588 /* Concatenate all the agent expressions that are commands into the
8589 cmds parameter. */
8590 for (ix = 0;
8591 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8592 ix++)
8593 {
8594 sprintf (buf, "X%x,", aexpr->len);
8595 buf += strlen (buf);
8596 for (i = 0; i < aexpr->len; ++i)
8597 buf = pack_hex_byte (buf, aexpr->buf[i]);
8598 *buf = '\0';
8599 }
d3ce09f5
SS
8600}
8601
8181d85f
DJ
8602/* Insert a breakpoint. On targets that have software breakpoint
8603 support, we ask the remote target to do the work; on targets
8604 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
8605
8606static int
3db08215
MM
8607remote_insert_breakpoint (struct target_ops *ops,
8608 struct gdbarch *gdbarch,
a6d9a66e 8609 struct bp_target_info *bp_tgt)
c906108c 8610{
d471ea57
AC
8611 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8612 If it succeeds, then set the support to PACKET_ENABLE. If it
8613 fails, and the user has explicitly requested the Z support then
23860348 8614 report an error, otherwise, mark it disabled and go on. */
802188a7 8615
4082afcc 8616 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8617 {
0d5ed153 8618 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8619 struct remote_state *rs;
bba74b36 8620 char *p, *endbuf;
7c0f6dcc 8621 int bpsize;
b775012e 8622 struct condition_list *cond = NULL;
4fff2411 8623
28439a30
PA
8624 /* Make sure the remote is pointing at the right process, if
8625 necessary. */
8626 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8627 set_general_process ();
8628
a1dcb23a 8629 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
4fff2411
JZ
8630
8631 rs = get_remote_state ();
8632 p = rs->buf;
bba74b36 8633 endbuf = rs->buf + get_remote_packet_size ();
802188a7 8634
96baa820
JM
8635 *(p++) = 'Z';
8636 *(p++) = '0';
8637 *(p++) = ',';
7c0f6dcc 8638 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 8639 p += hexnumstr (p, addr);
bba74b36 8640 xsnprintf (p, endbuf - p, ",%d", bpsize);
802188a7 8641
efcc2da7 8642 if (remote_supports_cond_breakpoints (ops))
bba74b36 8643 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8644
78eff0ec 8645 if (remote_can_run_breakpoint_commands (ops))
d3ce09f5
SS
8646 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8647
6d820c5c
DJ
8648 putpkt (rs->buf);
8649 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8650
6d820c5c 8651 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 8652 {
d471ea57
AC
8653 case PACKET_ERROR:
8654 return -1;
8655 case PACKET_OK:
7c0f6dcc
JL
8656 bp_tgt->placed_address = addr;
8657 bp_tgt->placed_size = bpsize;
d471ea57
AC
8658 return 0;
8659 case PACKET_UNKNOWN:
8660 break;
96baa820
JM
8661 }
8662 }
c906108c 8663
0000e5cc
PA
8664 /* If this breakpoint has target-side commands but this stub doesn't
8665 support Z0 packets, throw error. */
8666 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8667 throw_error (NOT_SUPPORTED_ERROR, _("\
8668Target doesn't support breakpoints that have target side commands."));
8669
3db08215 8670 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8671}
8672
8673static int
3db08215
MM
8674remote_remove_breakpoint (struct target_ops *ops,
8675 struct gdbarch *gdbarch,
a6d9a66e 8676 struct bp_target_info *bp_tgt)
c906108c 8677{
8181d85f 8678 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 8679 struct remote_state *rs = get_remote_state ();
96baa820 8680
4082afcc 8681 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8682 {
6d820c5c 8683 char *p = rs->buf;
bba74b36 8684 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 8685
28439a30
PA
8686 /* Make sure the remote is pointing at the right process, if
8687 necessary. */
8688 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8689 set_general_process ();
8690
96baa820
JM
8691 *(p++) = 'z';
8692 *(p++) = '0';
8693 *(p++) = ',';
8694
8181d85f
DJ
8695 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8696 p += hexnumstr (p, addr);
bba74b36 8697 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
802188a7 8698
6d820c5c
DJ
8699 putpkt (rs->buf);
8700 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8701
6d820c5c 8702 return (rs->buf[0] == 'E');
96baa820
JM
8703 }
8704
3db08215 8705 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8706}
8707
d471ea57
AC
8708static int
8709watchpoint_to_Z_packet (int type)
8710{
8711 switch (type)
8712 {
8713 case hw_write:
bb858e6a 8714 return Z_PACKET_WRITE_WP;
d471ea57
AC
8715 break;
8716 case hw_read:
bb858e6a 8717 return Z_PACKET_READ_WP;
d471ea57
AC
8718 break;
8719 case hw_access:
bb858e6a 8720 return Z_PACKET_ACCESS_WP;
d471ea57
AC
8721 break;
8722 default:
8e65ff28 8723 internal_error (__FILE__, __LINE__,
e2e0b3e5 8724 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
8725 }
8726}
8727
3c3bea1c 8728static int
7bb99c53
TT
8729remote_insert_watchpoint (struct target_ops *self,
8730 CORE_ADDR addr, int len, int type,
0cf6dd15 8731 struct expression *cond)
96baa820 8732{
d01949b6 8733 struct remote_state *rs = get_remote_state ();
bba74b36 8734 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8735 char *p;
d471ea57 8736 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 8737
4082afcc 8738 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 8739 return 1;
802188a7 8740
28439a30
PA
8741 /* Make sure the remote is pointing at the right process, if
8742 necessary. */
8743 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8744 set_general_process ();
8745
bba74b36 8746 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 8747 p = strchr (rs->buf, '\0');
96baa820
JM
8748 addr = remote_address_masked (addr);
8749 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8750 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 8751
6d820c5c
DJ
8752 putpkt (rs->buf);
8753 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8754
6d820c5c 8755 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8756 {
8757 case PACKET_ERROR:
d471ea57 8758 return -1;
85d721b8
PA
8759 case PACKET_UNKNOWN:
8760 return 1;
d471ea57
AC
8761 case PACKET_OK:
8762 return 0;
8763 }
8e65ff28 8764 internal_error (__FILE__, __LINE__,
e2e0b3e5 8765 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
8766}
8767
283002cf
MR
8768static int
8769remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8770 CORE_ADDR start, int length)
8771{
8772 CORE_ADDR diff = remote_address_masked (addr - start);
8773
8774 return diff < length;
8775}
8776
d471ea57 8777
3c3bea1c 8778static int
11b5219a
TT
8779remote_remove_watchpoint (struct target_ops *self,
8780 CORE_ADDR addr, int len, int type,
0cf6dd15 8781 struct expression *cond)
96baa820 8782{
d01949b6 8783 struct remote_state *rs = get_remote_state ();
bba74b36 8784 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8785 char *p;
d471ea57
AC
8786 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8787
4082afcc 8788 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 8789 return -1;
802188a7 8790
28439a30
PA
8791 /* Make sure the remote is pointing at the right process, if
8792 necessary. */
8793 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8794 set_general_process ();
8795
bba74b36 8796 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 8797 p = strchr (rs->buf, '\0');
96baa820
JM
8798 addr = remote_address_masked (addr);
8799 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8800 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
8801 putpkt (rs->buf);
8802 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8803
6d820c5c 8804 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8805 {
8806 case PACKET_ERROR:
8807 case PACKET_UNKNOWN:
8808 return -1;
8809 case PACKET_OK:
8810 return 0;
8811 }
8e65ff28 8812 internal_error (__FILE__, __LINE__,
e2e0b3e5 8813 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
8814}
8815
3c3bea1c 8816
501eef12 8817int remote_hw_watchpoint_limit = -1;
480a3f21 8818int remote_hw_watchpoint_length_limit = -1;
501eef12 8819int remote_hw_breakpoint_limit = -1;
d471ea57 8820
480a3f21 8821static int
31568a15
TT
8822remote_region_ok_for_hw_watchpoint (struct target_ops *self,
8823 CORE_ADDR addr, int len)
480a3f21
PW
8824{
8825 if (remote_hw_watchpoint_length_limit == 0)
8826 return 0;
8827 else if (remote_hw_watchpoint_length_limit < 0)
8828 return 1;
8829 else if (len <= remote_hw_watchpoint_length_limit)
8830 return 1;
8831 else
8832 return 0;
8833}
8834
b9362cc7 8835static int
5461485a
TT
8836remote_check_watch_resources (struct target_ops *self,
8837 int type, int cnt, int ot)
96baa820 8838{
3c3bea1c
GS
8839 if (type == bp_hardware_breakpoint)
8840 {
8841 if (remote_hw_breakpoint_limit == 0)
8842 return 0;
501eef12
AC
8843 else if (remote_hw_breakpoint_limit < 0)
8844 return 1;
3c3bea1c
GS
8845 else if (cnt <= remote_hw_breakpoint_limit)
8846 return 1;
8847 }
8848 else
8849 {
8850 if (remote_hw_watchpoint_limit == 0)
8851 return 0;
501eef12
AC
8852 else if (remote_hw_watchpoint_limit < 0)
8853 return 1;
3c3bea1c
GS
8854 else if (ot)
8855 return -1;
8856 else if (cnt <= remote_hw_watchpoint_limit)
8857 return 1;
8858 }
8859 return -1;
8860}
8861
f7e6eed5
PA
8862/* The to_stopped_by_sw_breakpoint method of target remote. */
8863
8864static int
8865remote_stopped_by_sw_breakpoint (struct target_ops *ops)
8866{
8867 struct remote_state *rs = get_remote_state ();
8868
8869 return rs->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
8870}
8871
8872/* The to_supports_stopped_by_sw_breakpoint method of target
8873 remote. */
8874
8875static int
8876remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
8877{
8878 struct remote_state *rs = get_remote_state ();
8879
8880 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
8881}
8882
8883/* The to_stopped_by_hw_breakpoint method of target remote. */
8884
8885static int
8886remote_stopped_by_hw_breakpoint (struct target_ops *ops)
8887{
8888 struct remote_state *rs = get_remote_state ();
8889
8890 return rs->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
8891}
8892
8893/* The to_supports_stopped_by_hw_breakpoint method of target
8894 remote. */
8895
8896static int
8897remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
8898{
8899 struct remote_state *rs = get_remote_state ();
8900
8901 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
8902}
8903
b9362cc7 8904static int
6a109b6b 8905remote_stopped_by_watchpoint (struct target_ops *ops)
3c3bea1c 8906{
ee154bee
TT
8907 struct remote_state *rs = get_remote_state ();
8908
f7e6eed5 8909 return rs->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
3c3bea1c
GS
8910}
8911
4aa7a7f5
JJ
8912static int
8913remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 8914{
ee154bee 8915 struct remote_state *rs = get_remote_state ();
4aa7a7f5 8916 int rc = 0;
a744cf53 8917
6a109b6b 8918 if (remote_stopped_by_watchpoint (target))
4aa7a7f5 8919 {
ee154bee 8920 *addr_p = rs->remote_watch_data_address;
4aa7a7f5
JJ
8921 rc = 1;
8922 }
8923
8924 return rc;
3c3bea1c
GS
8925}
8926
8927
8928static int
23a26771 8929remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8930 struct bp_target_info *bp_tgt)
3c3bea1c 8931{
0d5ed153 8932 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8933 struct remote_state *rs;
bba74b36 8934 char *p, *endbuf;
dd61ec5c 8935 char *message;
0d5ed153 8936 int bpsize;
802188a7 8937
c8189ed1 8938 /* The length field should be set to the size of a breakpoint
8181d85f 8939 instruction, even though we aren't inserting one ourselves. */
c8189ed1 8940
0d5ed153 8941 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
3c3bea1c 8942
4082afcc 8943 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 8944 return -1;
2bc416ba 8945
28439a30
PA
8946 /* Make sure the remote is pointing at the right process, if
8947 necessary. */
8948 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8949 set_general_process ();
8950
4fff2411
JZ
8951 rs = get_remote_state ();
8952 p = rs->buf;
bba74b36 8953 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 8954
96baa820
JM
8955 *(p++) = 'Z';
8956 *(p++) = '1';
8957 *(p++) = ',';
802188a7 8958
0d5ed153 8959 addr = remote_address_masked (addr);
96baa820 8960 p += hexnumstr (p, (ULONGEST) addr);
0d5ed153 8961 xsnprintf (p, endbuf - p, ",%x", bpsize);
96baa820 8962
efcc2da7 8963 if (remote_supports_cond_breakpoints (self))
bba74b36 8964 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8965
78eff0ec 8966 if (remote_can_run_breakpoint_commands (self))
d3ce09f5
SS
8967 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8968
6d820c5c
DJ
8969 putpkt (rs->buf);
8970 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8971
6d820c5c 8972 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8973 {
8974 case PACKET_ERROR:
dd61ec5c
MW
8975 if (rs->buf[1] == '.')
8976 {
8977 message = strchr (rs->buf + 2, '.');
8978 if (message)
0316657e 8979 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
8980 }
8981 return -1;
d471ea57
AC
8982 case PACKET_UNKNOWN:
8983 return -1;
8984 case PACKET_OK:
0d5ed153
MR
8985 bp_tgt->placed_address = addr;
8986 bp_tgt->placed_size = bpsize;
d471ea57
AC
8987 return 0;
8988 }
8e65ff28 8989 internal_error (__FILE__, __LINE__,
e2e0b3e5 8990 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
8991}
8992
d471ea57 8993
802188a7 8994static int
a64dc96c 8995remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8996 struct bp_target_info *bp_tgt)
96baa820 8997{
8181d85f 8998 CORE_ADDR addr;
d01949b6 8999 struct remote_state *rs = get_remote_state ();
6d820c5c 9000 char *p = rs->buf;
bba74b36 9001 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 9002
4082afcc 9003 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 9004 return -1;
802188a7 9005
28439a30
PA
9006 /* Make sure the remote is pointing at the right process, if
9007 necessary. */
9008 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
9009 set_general_process ();
9010
96baa820
JM
9011 *(p++) = 'z';
9012 *(p++) = '1';
9013 *(p++) = ',';
802188a7 9014
8181d85f 9015 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 9016 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 9017 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
96baa820 9018
6d820c5c
DJ
9019 putpkt (rs->buf);
9020 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 9021
6d820c5c 9022 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
9023 {
9024 case PACKET_ERROR:
9025 case PACKET_UNKNOWN:
9026 return -1;
9027 case PACKET_OK:
9028 return 0;
9029 }
8e65ff28 9030 internal_error (__FILE__, __LINE__,
e2e0b3e5 9031 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 9032}
96baa820 9033
4a5e7a5b
PA
9034/* Verify memory using the "qCRC:" request. */
9035
9036static int
9037remote_verify_memory (struct target_ops *ops,
9038 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
9039{
9040 struct remote_state *rs = get_remote_state ();
9041 unsigned long host_crc, target_crc;
9042 char *tmp;
9043
936d2992
PA
9044 /* It doesn't make sense to use qCRC if the remote target is
9045 connected but not running. */
9046 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
9047 {
9048 enum packet_result result;
28439a30 9049
936d2992
PA
9050 /* Make sure the remote is pointing at the right process. */
9051 set_general_process ();
4a5e7a5b 9052
936d2992
PA
9053 /* FIXME: assumes lma can fit into long. */
9054 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
9055 (long) lma, (long) size);
9056 putpkt (rs->buf);
4a5e7a5b 9057
936d2992
PA
9058 /* Be clever; compute the host_crc before waiting for target
9059 reply. */
9060 host_crc = xcrc32 (data, size, 0xffffffff);
9061
9062 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 9063
936d2992
PA
9064 result = packet_ok (rs->buf,
9065 &remote_protocol_packets[PACKET_qCRC]);
9066 if (result == PACKET_ERROR)
9067 return -1;
9068 else if (result == PACKET_OK)
9069 {
9070 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
9071 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 9072
936d2992
PA
9073 return (host_crc == target_crc);
9074 }
9075 }
4a5e7a5b 9076
936d2992 9077 return simple_verify_memory (ops, data, lma, size);
4a5e7a5b
PA
9078}
9079
c906108c
SS
9080/* compare-sections command
9081
9082 With no arguments, compares each loadable section in the exec bfd
9083 with the same memory range on the target, and reports mismatches.
4a5e7a5b 9084 Useful for verifying the image on the target against the exec file. */
e514a9d6 9085
c906108c 9086static void
fba45db2 9087compare_sections_command (char *args, int from_tty)
c906108c
SS
9088{
9089 asection *s;
c906108c 9090 struct cleanup *old_chain;
948f8e3d 9091 gdb_byte *sectdata;
ce359b09 9092 const char *sectname;
c906108c
SS
9093 bfd_size_type size;
9094 bfd_vma lma;
9095 int matched = 0;
9096 int mismatched = 0;
4a5e7a5b 9097 int res;
95cf3b38 9098 int read_only = 0;
c906108c
SS
9099
9100 if (!exec_bfd)
8a3fe4f8 9101 error (_("command cannot be used without an exec file"));
c906108c 9102
28439a30
PA
9103 /* Make sure the remote is pointing at the right process. */
9104 set_general_process ();
9105
95cf3b38
DT
9106 if (args != NULL && strcmp (args, "-r") == 0)
9107 {
9108 read_only = 1;
9109 args = NULL;
9110 }
9111
c5aa993b 9112 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
9113 {
9114 if (!(s->flags & SEC_LOAD))
0df8b418 9115 continue; /* Skip non-loadable section. */
c906108c 9116
95cf3b38
DT
9117 if (read_only && (s->flags & SEC_READONLY) == 0)
9118 continue; /* Skip writeable sections */
9119
2c500098 9120 size = bfd_get_section_size (s);
c906108c 9121 if (size == 0)
0df8b418 9122 continue; /* Skip zero-length section. */
c906108c 9123
ce359b09 9124 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 9125 if (args && strcmp (args, sectname) != 0)
0df8b418 9126 continue; /* Not the section selected by user. */
c906108c 9127
0df8b418 9128 matched = 1; /* Do this section. */
c906108c 9129 lma = s->lma;
c906108c 9130
c906108c 9131 sectdata = xmalloc (size);
b8c9b27d 9132 old_chain = make_cleanup (xfree, sectdata);
c906108c 9133 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 9134
4a5e7a5b
PA
9135 res = target_verify_memory (sectdata, lma, size);
9136
9137 if (res == -1)
5af949e3 9138 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
9139 paddress (target_gdbarch (), lma),
9140 paddress (target_gdbarch (), lma + size));
c906108c 9141
5af949e3 9142 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
9143 paddress (target_gdbarch (), lma),
9144 paddress (target_gdbarch (), lma + size));
4a5e7a5b 9145 if (res)
c906108c
SS
9146 printf_filtered ("matched.\n");
9147 else
c5aa993b
JM
9148 {
9149 printf_filtered ("MIS-MATCHED!\n");
9150 mismatched++;
9151 }
c906108c
SS
9152
9153 do_cleanups (old_chain);
9154 }
9155 if (mismatched > 0)
936d2992 9156 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 9157the loaded file\n"));
c906108c 9158 if (args && !matched)
a3f17187 9159 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
9160}
9161
0e7f50da
UW
9162/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
9163 into remote target. The number of bytes written to the remote
9164 target is returned, or -1 for error. */
9165
9b409511 9166static enum target_xfer_status
0e7f50da
UW
9167remote_write_qxfer (struct target_ops *ops, const char *object_name,
9168 const char *annex, const gdb_byte *writebuf,
9b409511 9169 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
9170 struct packet_config *packet)
9171{
9172 int i, buf_len;
9173 ULONGEST n;
0e7f50da
UW
9174 struct remote_state *rs = get_remote_state ();
9175 int max_size = get_memory_write_packet_size ();
9176
9177 if (packet->support == PACKET_DISABLE)
2ed4b548 9178 return TARGET_XFER_E_IO;
0e7f50da
UW
9179
9180 /* Insert header. */
9181 i = snprintf (rs->buf, max_size,
9182 "qXfer:%s:write:%s:%s:",
9183 object_name, annex ? annex : "",
9184 phex_nz (offset, sizeof offset));
9185 max_size -= (i + 1);
9186
9187 /* Escape as much data as fits into rs->buf. */
9188 buf_len = remote_escape_output
124e13d9 9189 (writebuf, len, 1, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
9190
9191 if (putpkt_binary (rs->buf, i + buf_len) < 0
9192 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9193 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9194 return TARGET_XFER_E_IO;
0e7f50da
UW
9195
9196 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
9197
9198 *xfered_len = n;
9199 return TARGET_XFER_OK;
0e7f50da
UW
9200}
9201
0876f84a
DJ
9202/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
9203 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
9204 number of bytes read is returned, or 0 for EOF, or -1 for error.
9205 The number of bytes read may be less than LEN without indicating an
9206 EOF. PACKET is checked and updated to indicate whether the remote
9207 target supports this object. */
9208
9b409511 9209static enum target_xfer_status
0876f84a
DJ
9210remote_read_qxfer (struct target_ops *ops, const char *object_name,
9211 const char *annex,
9212 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 9213 ULONGEST *xfered_len,
0876f84a
DJ
9214 struct packet_config *packet)
9215{
0876f84a 9216 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
9217 LONGEST i, n, packet_len;
9218
9219 if (packet->support == PACKET_DISABLE)
2ed4b548 9220 return TARGET_XFER_E_IO;
0876f84a
DJ
9221
9222 /* Check whether we've cached an end-of-object packet that matches
9223 this request. */
8e88304f 9224 if (rs->finished_object)
0876f84a 9225 {
8e88304f
TT
9226 if (strcmp (object_name, rs->finished_object) == 0
9227 && strcmp (annex ? annex : "", rs->finished_annex) == 0
9228 && offset == rs->finished_offset)
9b409511
YQ
9229 return TARGET_XFER_EOF;
9230
0876f84a
DJ
9231
9232 /* Otherwise, we're now reading something different. Discard
9233 the cache. */
8e88304f
TT
9234 xfree (rs->finished_object);
9235 xfree (rs->finished_annex);
9236 rs->finished_object = NULL;
9237 rs->finished_annex = NULL;
0876f84a
DJ
9238 }
9239
9240 /* Request only enough to fit in a single packet. The actual data
9241 may not, since we don't know how much of it will need to be escaped;
9242 the target is free to respond with slightly less data. We subtract
9243 five to account for the response type and the protocol frame. */
9244 n = min (get_remote_packet_size () - 5, len);
9245 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
9246 object_name, annex ? annex : "",
9247 phex_nz (offset, sizeof offset),
9248 phex_nz (n, sizeof n));
9249 i = putpkt (rs->buf);
9250 if (i < 0)
2ed4b548 9251 return TARGET_XFER_E_IO;
0876f84a
DJ
9252
9253 rs->buf[0] = '\0';
9254 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9255 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 9256 return TARGET_XFER_E_IO;
0876f84a
DJ
9257
9258 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
9259 error (_("Unknown remote qXfer reply: %s"), rs->buf);
9260
9261 /* 'm' means there is (or at least might be) more data after this
9262 batch. That does not make sense unless there's at least one byte
9263 of data in this reply. */
9264 if (rs->buf[0] == 'm' && packet_len == 1)
9265 error (_("Remote qXfer reply contained no data."));
9266
9267 /* Got some data. */
bc20a4af
PA
9268 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
9269 packet_len - 1, readbuf, n);
0876f84a
DJ
9270
9271 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
9272 or possibly empty. If we have the final block of a non-empty
9273 object, record this fact to bypass a subsequent partial read. */
9274 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 9275 {
8e88304f
TT
9276 rs->finished_object = xstrdup (object_name);
9277 rs->finished_annex = xstrdup (annex ? annex : "");
9278 rs->finished_offset = offset + i;
0876f84a
DJ
9279 }
9280
9b409511
YQ
9281 if (i == 0)
9282 return TARGET_XFER_EOF;
9283 else
9284 {
9285 *xfered_len = i;
9286 return TARGET_XFER_OK;
9287 }
0876f84a
DJ
9288}
9289
9b409511 9290static enum target_xfer_status
4b8a223f 9291remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5 9292 const char *annex, gdb_byte *readbuf,
9b409511
YQ
9293 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
9294 ULONGEST *xfered_len)
c906108c 9295{
82f73884 9296 struct remote_state *rs;
c906108c 9297 int i;
6d820c5c 9298 char *p2;
1e3ff5ad 9299 char query_type;
124e13d9 9300 int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
c906108c 9301
e6e4e701 9302 set_remote_traceframe ();
82f73884
PA
9303 set_general_thread (inferior_ptid);
9304
9305 rs = get_remote_state ();
9306
b2182ed2 9307 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
9308 if (object == TARGET_OBJECT_MEMORY)
9309 {
2d717e4f
DJ
9310 /* If the remote target is connected but not running, we should
9311 pass this request down to a lower stratum (e.g. the executable
9312 file). */
9313 if (!target_has_execution)
9b409511 9314 return TARGET_XFER_EOF;
2d717e4f 9315
21e3b9b9 9316 if (writebuf != NULL)
124e13d9
SM
9317 return remote_write_bytes (offset, writebuf, len, unit_size,
9318 xfered_len);
21e3b9b9 9319 else
124e13d9
SM
9320 return remote_read_bytes (ops, offset, readbuf, len, unit_size,
9321 xfered_len);
21e3b9b9
DJ
9322 }
9323
0df8b418 9324 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
9325 if (object == TARGET_OBJECT_SPU)
9326 {
9327 if (readbuf)
9328 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9b409511
YQ
9329 xfered_len, &remote_protocol_packets
9330 [PACKET_qXfer_spu_read]);
0e7f50da
UW
9331 else
9332 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9b409511
YQ
9333 xfered_len, &remote_protocol_packets
9334 [PACKET_qXfer_spu_write]);
0e7f50da
UW
9335 }
9336
4aa995e1
PA
9337 /* Handle extra signal info using qxfer packets. */
9338 if (object == TARGET_OBJECT_SIGNAL_INFO)
9339 {
9340 if (readbuf)
9341 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9b409511 9342 xfered_len, &remote_protocol_packets
4aa995e1
PA
9343 [PACKET_qXfer_siginfo_read]);
9344 else
3e43a32a 9345 return remote_write_qxfer (ops, "siginfo", annex,
9b409511 9346 writebuf, offset, len, xfered_len,
4aa995e1
PA
9347 &remote_protocol_packets
9348 [PACKET_qXfer_siginfo_write]);
9349 }
9350
0fb4aa4b
PA
9351 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
9352 {
9353 if (readbuf)
3e43a32a 9354 return remote_read_qxfer (ops, "statictrace", annex,
9b409511 9355 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
9356 &remote_protocol_packets
9357 [PACKET_qXfer_statictrace_read]);
9358 else
2ed4b548 9359 return TARGET_XFER_E_IO;
0fb4aa4b
PA
9360 }
9361
a76d924d
DJ
9362 /* Only handle flash writes. */
9363 if (writebuf != NULL)
9364 {
9365 LONGEST xfered;
9366
9367 switch (object)
9368 {
9369 case TARGET_OBJECT_FLASH:
9b409511
YQ
9370 return remote_flash_write (ops, offset, len, xfered_len,
9371 writebuf);
a76d924d
DJ
9372
9373 default:
2ed4b548 9374 return TARGET_XFER_E_IO;
a76d924d
DJ
9375 }
9376 }
4b8a223f 9377
1e3ff5ad
AC
9378 /* Map pre-existing objects onto letters. DO NOT do this for new
9379 objects!!! Instead specify new query packets. */
9380 switch (object)
c906108c 9381 {
1e3ff5ad
AC
9382 case TARGET_OBJECT_AVR:
9383 query_type = 'R';
9384 break;
802188a7
RM
9385
9386 case TARGET_OBJECT_AUXV:
0876f84a
DJ
9387 gdb_assert (annex == NULL);
9388 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9b409511 9389 xfered_len,
0876f84a 9390 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 9391
23181151
DJ
9392 case TARGET_OBJECT_AVAILABLE_FEATURES:
9393 return remote_read_qxfer
9b409511 9394 (ops, "features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
9395 &remote_protocol_packets[PACKET_qXfer_features]);
9396
cfa9d6d9
DJ
9397 case TARGET_OBJECT_LIBRARIES:
9398 return remote_read_qxfer
9b409511 9399 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
9400 &remote_protocol_packets[PACKET_qXfer_libraries]);
9401
2268b414
JK
9402 case TARGET_OBJECT_LIBRARIES_SVR4:
9403 return remote_read_qxfer
9b409511 9404 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
9405 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
9406
fd79ecee
DJ
9407 case TARGET_OBJECT_MEMORY_MAP:
9408 gdb_assert (annex == NULL);
9409 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9b409511 9410 xfered_len,
fd79ecee
DJ
9411 &remote_protocol_packets[PACKET_qXfer_memory_map]);
9412
07e059b5
VP
9413 case TARGET_OBJECT_OSDATA:
9414 /* Should only get here if we're connected. */
5d93a237 9415 gdb_assert (rs->remote_desc);
07e059b5 9416 return remote_read_qxfer
9b409511 9417 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
9418 &remote_protocol_packets[PACKET_qXfer_osdata]);
9419
dc146f7c
VP
9420 case TARGET_OBJECT_THREADS:
9421 gdb_assert (annex == NULL);
9422 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9b409511 9423 xfered_len,
dc146f7c
VP
9424 &remote_protocol_packets[PACKET_qXfer_threads]);
9425
b3b9301e
PA
9426 case TARGET_OBJECT_TRACEFRAME_INFO:
9427 gdb_assert (annex == NULL);
9428 return remote_read_qxfer
9b409511 9429 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 9430 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
9431
9432 case TARGET_OBJECT_FDPIC:
9433 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9b409511 9434 xfered_len,
78d85199 9435 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
9436
9437 case TARGET_OBJECT_OPENVMS_UIB:
9438 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9b409511 9439 xfered_len,
169081d0
TG
9440 &remote_protocol_packets[PACKET_qXfer_uib]);
9441
9accd112
MM
9442 case TARGET_OBJECT_BTRACE:
9443 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9b409511 9444 xfered_len,
9accd112
MM
9445 &remote_protocol_packets[PACKET_qXfer_btrace]);
9446
f4abbc16
MM
9447 case TARGET_OBJECT_BTRACE_CONF:
9448 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
9449 len, xfered_len,
9450 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
9451
c78fa86a
GB
9452 case TARGET_OBJECT_EXEC_FILE:
9453 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
9454 len, xfered_len,
9455 &remote_protocol_packets[PACKET_qXfer_exec_file]);
9456
1e3ff5ad 9457 default:
2ed4b548 9458 return TARGET_XFER_E_IO;
c906108c
SS
9459 }
9460
0df8b418 9461 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 9462 large enough let the caller deal with it. */
ea9c271d 9463 if (len < get_remote_packet_size ())
2ed4b548 9464 return TARGET_XFER_E_IO;
ea9c271d 9465 len = get_remote_packet_size ();
1e3ff5ad 9466
23860348 9467 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 9468 if (!rs->remote_desc)
8a3fe4f8 9469 error (_("remote query is only available after target open"));
c906108c 9470
1e3ff5ad 9471 gdb_assert (annex != NULL);
4b8a223f 9472 gdb_assert (readbuf != NULL);
c906108c 9473
6d820c5c 9474 p2 = rs->buf;
c906108c
SS
9475 *p2++ = 'q';
9476 *p2++ = query_type;
9477
23860348
MS
9478 /* We used one buffer char for the remote protocol q command and
9479 another for the query type. As the remote protocol encapsulation
9480 uses 4 chars plus one extra in case we are debugging
9481 (remote_debug), we have PBUFZIZ - 7 left to pack the query
9482 string. */
c906108c 9483 i = 0;
ea9c271d 9484 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 9485 {
1e3ff5ad
AC
9486 /* Bad caller may have sent forbidden characters. */
9487 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
9488 *p2++ = annex[i];
c906108c
SS
9489 i++;
9490 }
1e3ff5ad
AC
9491 *p2 = '\0';
9492 gdb_assert (annex[i] == '\0');
c906108c 9493
6d820c5c 9494 i = putpkt (rs->buf);
c5aa993b 9495 if (i < 0)
2ed4b548 9496 return TARGET_XFER_E_IO;
c906108c 9497
6d820c5c
DJ
9498 getpkt (&rs->buf, &rs->buf_size, 0);
9499 strcpy ((char *) readbuf, rs->buf);
c906108c 9500
9b409511
YQ
9501 *xfered_len = strlen ((char *) readbuf);
9502 return TARGET_XFER_OK;
c906108c
SS
9503}
9504
08388c79
DE
9505static int
9506remote_search_memory (struct target_ops* ops,
9507 CORE_ADDR start_addr, ULONGEST search_space_len,
9508 const gdb_byte *pattern, ULONGEST pattern_len,
9509 CORE_ADDR *found_addrp)
9510{
f5656ead 9511 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
9512 struct remote_state *rs = get_remote_state ();
9513 int max_size = get_memory_write_packet_size ();
9514 struct packet_config *packet =
9515 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
9516 /* Number of packet bytes used to encode the pattern;
9517 this could be more than PATTERN_LEN due to escape characters. */
08388c79 9518 int escaped_pattern_len;
0df8b418 9519 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
9520 int used_pattern_len;
9521 int i;
9522 int found;
9523 ULONGEST found_addr;
9524
9525 /* Don't go to the target if we don't have to.
9526 This is done before checking packet->support to avoid the possibility that
9527 a success for this edge case means the facility works in general. */
9528 if (pattern_len > search_space_len)
9529 return 0;
9530 if (pattern_len == 0)
9531 {
9532 *found_addrp = start_addr;
9533 return 1;
9534 }
9535
9536 /* If we already know the packet isn't supported, fall back to the simple
9537 way of searching memory. */
9538
4082afcc 9539 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
9540 {
9541 /* Target doesn't provided special support, fall back and use the
9542 standard support (copy memory and do the search here). */
9543 return simple_search_memory (ops, start_addr, search_space_len,
9544 pattern, pattern_len, found_addrp);
9545 }
9546
28439a30
PA
9547 /* Make sure the remote is pointing at the right process. */
9548 set_general_process ();
9549
08388c79
DE
9550 /* Insert header. */
9551 i = snprintf (rs->buf, max_size,
9552 "qSearch:memory:%s;%s;",
5af949e3 9553 phex_nz (start_addr, addr_size),
08388c79
DE
9554 phex_nz (search_space_len, sizeof (search_space_len)));
9555 max_size -= (i + 1);
9556
9557 /* Escape as much data as fits into rs->buf. */
9558 escaped_pattern_len =
124e13d9 9559 remote_escape_output (pattern, pattern_len, 1, (gdb_byte *) rs->buf + i,
08388c79
DE
9560 &used_pattern_len, max_size);
9561
9562 /* Bail if the pattern is too large. */
9563 if (used_pattern_len != pattern_len)
9b20d036 9564 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
9565
9566 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
9567 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9568 || packet_ok (rs->buf, packet) != PACKET_OK)
9569 {
9570 /* The request may not have worked because the command is not
9571 supported. If so, fall back to the simple way. */
9572 if (packet->support == PACKET_DISABLE)
9573 {
9574 return simple_search_memory (ops, start_addr, search_space_len,
9575 pattern, pattern_len, found_addrp);
9576 }
9577 return -1;
9578 }
9579
9580 if (rs->buf[0] == '0')
9581 found = 0;
9582 else if (rs->buf[0] == '1')
9583 {
9584 found = 1;
9585 if (rs->buf[1] != ',')
10e0fa18 9586 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9587 unpack_varlen_hex (rs->buf + 2, &found_addr);
9588 *found_addrp = found_addr;
9589 }
9590 else
10e0fa18 9591 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9592
9593 return found;
9594}
9595
96baa820 9596static void
a30bf1f1 9597remote_rcmd (struct target_ops *self, const char *command,
d9fcf2fb 9598 struct ui_file *outbuf)
96baa820 9599{
d01949b6 9600 struct remote_state *rs = get_remote_state ();
2e9f7625 9601 char *p = rs->buf;
96baa820 9602
5d93a237 9603 if (!rs->remote_desc)
8a3fe4f8 9604 error (_("remote rcmd is only available after target open"));
96baa820 9605
23860348 9606 /* Send a NULL command across as an empty command. */
7be570e7
JM
9607 if (command == NULL)
9608 command = "";
9609
23860348 9610 /* The query prefix. */
2e9f7625
DJ
9611 strcpy (rs->buf, "qRcmd,");
9612 p = strchr (rs->buf, '\0');
96baa820 9613
3e43a32a
MS
9614 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9615 > get_remote_packet_size ())
8a3fe4f8 9616 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 9617
23860348 9618 /* Encode the actual command. */
a30bf1f1 9619 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 9620
6d820c5c 9621 if (putpkt (rs->buf) < 0)
8a3fe4f8 9622 error (_("Communication problem with target."));
96baa820
JM
9623
9624 /* get/display the response */
9625 while (1)
9626 {
2e9f7625
DJ
9627 char *buf;
9628
00bf0b85 9629 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 9630 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 9631 rs->buf[0] = '\0';
5b37825d
PW
9632 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9633 {
9634 /* Timeout. Continue to (try to) read responses.
9635 This is better than stopping with an error, assuming the stub
9636 is still executing the (long) monitor command.
9637 If needed, the user can interrupt gdb using C-c, obtaining
9638 an effect similar to stop on timeout. */
9639 continue;
9640 }
2e9f7625 9641 buf = rs->buf;
96baa820 9642 if (buf[0] == '\0')
8a3fe4f8 9643 error (_("Target does not support this command."));
96baa820
JM
9644 if (buf[0] == 'O' && buf[1] != 'K')
9645 {
23860348 9646 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
9647 continue;
9648 }
9649 if (strcmp (buf, "OK") == 0)
9650 break;
7be570e7
JM
9651 if (strlen (buf) == 3 && buf[0] == 'E'
9652 && isdigit (buf[1]) && isdigit (buf[2]))
9653 {
8a3fe4f8 9654 error (_("Protocol error with Rcmd"));
7be570e7 9655 }
96baa820
JM
9656 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9657 {
9658 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 9659
96baa820
JM
9660 fputc_unfiltered (c, outbuf);
9661 }
9662 break;
9663 }
9664}
9665
fd79ecee
DJ
9666static VEC(mem_region_s) *
9667remote_memory_map (struct target_ops *ops)
9668{
9669 VEC(mem_region_s) *result = NULL;
9670 char *text = target_read_stralloc (&current_target,
9671 TARGET_OBJECT_MEMORY_MAP, NULL);
9672
9673 if (text)
9674 {
9675 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 9676
fd79ecee
DJ
9677 result = parse_memory_map (text);
9678 do_cleanups (back_to);
9679 }
9680
9681 return result;
9682}
9683
c906108c 9684static void
fba45db2 9685packet_command (char *args, int from_tty)
c906108c 9686{
d01949b6 9687 struct remote_state *rs = get_remote_state ();
c906108c 9688
5d93a237 9689 if (!rs->remote_desc)
8a3fe4f8 9690 error (_("command can only be used with remote target"));
c906108c 9691
c5aa993b 9692 if (!args)
8a3fe4f8 9693 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
9694
9695 puts_filtered ("sending: ");
9696 print_packet (args);
9697 puts_filtered ("\n");
9698 putpkt (args);
9699
6d820c5c 9700 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 9701 puts_filtered ("received: ");
6d820c5c 9702 print_packet (rs->buf);
c906108c
SS
9703 puts_filtered ("\n");
9704}
9705
9706#if 0
23860348 9707/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 9708
a14ed312 9709static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 9710
a14ed312 9711static void threadset_test_cmd (char *cmd, int tty);
c906108c 9712
a14ed312 9713static void threadalive_test (char *cmd, int tty);
c906108c 9714
a14ed312 9715static void threadlist_test_cmd (char *cmd, int tty);
c906108c 9716
23860348 9717int get_and_display_threadinfo (threadref *ref);
c906108c 9718
a14ed312 9719static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 9720
23860348 9721static int thread_display_step (threadref *ref, void *context);
c906108c 9722
a14ed312 9723static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 9724
a14ed312 9725static void init_remote_threadtests (void);
c906108c 9726
23860348 9727#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
9728
9729static void
fba45db2 9730threadset_test_cmd (char *cmd, int tty)
c906108c
SS
9731{
9732 int sample_thread = SAMPLE_THREAD;
9733
a3f17187 9734 printf_filtered (_("Remote threadset test\n"));
79d7f229 9735 set_general_thread (sample_thread);
c906108c
SS
9736}
9737
9738
9739static void
fba45db2 9740threadalive_test (char *cmd, int tty)
c906108c
SS
9741{
9742 int sample_thread = SAMPLE_THREAD;
79d7f229 9743 int pid = ptid_get_pid (inferior_ptid);
ba348170 9744 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 9745
79d7f229 9746 if (remote_thread_alive (ptid))
c906108c
SS
9747 printf_filtered ("PASS: Thread alive test\n");
9748 else
9749 printf_filtered ("FAIL: Thread alive test\n");
9750}
9751
23860348 9752void output_threadid (char *title, threadref *ref);
c906108c
SS
9753
9754void
fba45db2 9755output_threadid (char *title, threadref *ref)
c906108c
SS
9756{
9757 char hexid[20];
9758
23860348 9759 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
9760 hexid[16] = 0;
9761 printf_filtered ("%s %s\n", title, (&hexid[0]));
9762}
9763
9764static void
fba45db2 9765threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
9766{
9767 int startflag = 1;
9768 threadref nextthread;
9769 int done, result_count;
9770 threadref threadlist[3];
9771
9772 printf_filtered ("Remote Threadlist test\n");
9773 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
9774 &result_count, &threadlist[0]))
9775 printf_filtered ("FAIL: threadlist test\n");
9776 else
9777 {
9778 threadref *scan = threadlist;
9779 threadref *limit = scan + result_count;
9780
9781 while (scan < limit)
9782 output_threadid (" thread ", scan++);
9783 }
9784}
9785
9786void
fba45db2 9787display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
9788{
9789 output_threadid ("Threadid: ", &info->threadid);
9790 printf_filtered ("Name: %s\n ", info->shortname);
9791 printf_filtered ("State: %s\n", info->display);
9792 printf_filtered ("other: %s\n\n", info->more_display);
9793}
9794
9795int
fba45db2 9796get_and_display_threadinfo (threadref *ref)
c906108c
SS
9797{
9798 int result;
9799 int set;
9800 struct gdb_ext_thread_info threadinfo;
9801
9802 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9803 | TAG_MOREDISPLAY | TAG_DISPLAY;
9804 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9805 display_thread_info (&threadinfo);
9806 return result;
9807}
9808
9809static void
fba45db2 9810threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
9811{
9812 int athread = SAMPLE_THREAD;
9813 threadref thread;
9814 int set;
9815
9816 int_to_threadref (&thread, athread);
9817 printf_filtered ("Remote Threadinfo test\n");
9818 if (!get_and_display_threadinfo (&thread))
9819 printf_filtered ("FAIL cannot get thread info\n");
9820}
9821
9822static int
fba45db2 9823thread_display_step (threadref *ref, void *context)
c906108c
SS
9824{
9825 /* output_threadid(" threadstep ",ref); *//* simple test */
9826 return get_and_display_threadinfo (ref);
9827}
9828
9829static void
fba45db2 9830threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
9831{
9832 printf_filtered ("Remote Threadlist update test\n");
9833 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9834}
9835
9836static void
9837init_remote_threadtests (void)
9838{
3e43a32a
MS
9839 add_com ("tlist", class_obscure, threadlist_test_cmd,
9840 _("Fetch and print the remote list of "
9841 "thread identifiers, one pkt only"));
c906108c 9842 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 9843 _("Fetch and display info about one thread"));
c906108c 9844 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 9845 _("Test setting to a different thread"));
c906108c 9846 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 9847 _("Iterate through updating all remote thread info"));
c906108c 9848 add_com ("talive", class_obscure, threadalive_test,
1bedd215 9849 _(" Remote thread alive test "));
c906108c
SS
9850}
9851
9852#endif /* 0 */
9853
f3fb8c85
MS
9854/* Convert a thread ID to a string. Returns the string in a static
9855 buffer. */
9856
9857static char *
117de6a9 9858remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 9859{
79d7f229 9860 static char buf[64];
82f73884 9861 struct remote_state *rs = get_remote_state ();
f3fb8c85 9862
7cee1e54
PA
9863 if (ptid_equal (ptid, null_ptid))
9864 return normal_pid_to_str (ptid);
9865 else if (ptid_is_pid (ptid))
ecd0ada5
PA
9866 {
9867 /* Printing an inferior target id. */
9868
9869 /* When multi-process extensions are off, there's no way in the
9870 remote protocol to know the remote process id, if there's any
9871 at all. There's one exception --- when we're connected with
9872 target extended-remote, and we manually attached to a process
9873 with "attach PID". We don't record anywhere a flag that
9874 allows us to distinguish that case from the case of
9875 connecting with extended-remote and the stub already being
9876 attached to a process, and reporting yes to qAttached, hence
9877 no smart special casing here. */
9878 if (!remote_multi_process_p (rs))
9879 {
9880 xsnprintf (buf, sizeof buf, "Remote target");
9881 return buf;
9882 }
9883
9884 return normal_pid_to_str (ptid);
82f73884 9885 }
ecd0ada5 9886 else
79d7f229 9887 {
ecd0ada5
PA
9888 if (ptid_equal (magic_null_ptid, ptid))
9889 xsnprintf (buf, sizeof buf, "Thread <main>");
901f9912 9890 else if (rs->extended && remote_multi_process_p (rs))
de0d863e
DB
9891 if (ptid_get_lwp (ptid) == 0)
9892 return normal_pid_to_str (ptid);
9893 else
9894 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9895 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
9896 else
9897 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 9898 ptid_get_lwp (ptid));
79d7f229
PA
9899 return buf;
9900 }
f3fb8c85
MS
9901}
9902
38691318
KB
9903/* Get the address of the thread local variable in OBJFILE which is
9904 stored at OFFSET within the thread local storage for thread PTID. */
9905
9906static CORE_ADDR
117de6a9
PA
9907remote_get_thread_local_address (struct target_ops *ops,
9908 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 9909{
4082afcc 9910 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
9911 {
9912 struct remote_state *rs = get_remote_state ();
6d820c5c 9913 char *p = rs->buf;
82f73884 9914 char *endp = rs->buf + get_remote_packet_size ();
571dd617 9915 enum packet_result result;
38691318
KB
9916
9917 strcpy (p, "qGetTLSAddr:");
9918 p += strlen (p);
82f73884 9919 p = write_ptid (p, endp, ptid);
38691318
KB
9920 *p++ = ',';
9921 p += hexnumstr (p, offset);
9922 *p++ = ',';
9923 p += hexnumstr (p, lm);
9924 *p++ = '\0';
9925
6d820c5c
DJ
9926 putpkt (rs->buf);
9927 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
9928 result = packet_ok (rs->buf,
9929 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 9930 if (result == PACKET_OK)
38691318
KB
9931 {
9932 ULONGEST result;
9933
6d820c5c 9934 unpack_varlen_hex (rs->buf, &result);
38691318
KB
9935 return result;
9936 }
571dd617 9937 else if (result == PACKET_UNKNOWN)
109c3e39
AC
9938 throw_error (TLS_GENERIC_ERROR,
9939 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 9940 else
109c3e39
AC
9941 throw_error (TLS_GENERIC_ERROR,
9942 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
9943 }
9944 else
109c3e39
AC
9945 throw_error (TLS_GENERIC_ERROR,
9946 _("TLS not supported or disabled on this target"));
38691318
KB
9947 /* Not reached. */
9948 return 0;
9949}
9950
711e434b
PM
9951/* Provide thread local base, i.e. Thread Information Block address.
9952 Returns 1 if ptid is found and thread_local_base is non zero. */
9953
70221824 9954static int
bd7ae0f5 9955remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
711e434b 9956{
4082afcc 9957 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
9958 {
9959 struct remote_state *rs = get_remote_state ();
9960 char *p = rs->buf;
9961 char *endp = rs->buf + get_remote_packet_size ();
9962 enum packet_result result;
9963
9964 strcpy (p, "qGetTIBAddr:");
9965 p += strlen (p);
9966 p = write_ptid (p, endp, ptid);
9967 *p++ = '\0';
9968
9969 putpkt (rs->buf);
9970 getpkt (&rs->buf, &rs->buf_size, 0);
9971 result = packet_ok (rs->buf,
9972 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9973 if (result == PACKET_OK)
9974 {
9975 ULONGEST result;
9976
9977 unpack_varlen_hex (rs->buf, &result);
9978 if (addr)
9979 *addr = (CORE_ADDR) result;
9980 return 1;
9981 }
9982 else if (result == PACKET_UNKNOWN)
9983 error (_("Remote target doesn't support qGetTIBAddr packet"));
9984 else
9985 error (_("Remote target failed to process qGetTIBAddr request"));
9986 }
9987 else
9988 error (_("qGetTIBAddr not supported or disabled on this target"));
9989 /* Not reached. */
9990 return 0;
9991}
9992
29709017
DJ
9993/* Support for inferring a target description based on the current
9994 architecture and the size of a 'g' packet. While the 'g' packet
9995 can have any size (since optional registers can be left off the
9996 end), some sizes are easily recognizable given knowledge of the
9997 approximate architecture. */
9998
9999struct remote_g_packet_guess
10000{
10001 int bytes;
10002 const struct target_desc *tdesc;
10003};
10004typedef struct remote_g_packet_guess remote_g_packet_guess_s;
10005DEF_VEC_O(remote_g_packet_guess_s);
10006
10007struct remote_g_packet_data
10008{
10009 VEC(remote_g_packet_guess_s) *guesses;
10010};
10011
10012static struct gdbarch_data *remote_g_packet_data_handle;
10013
10014static void *
10015remote_g_packet_data_init (struct obstack *obstack)
10016{
10017 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
10018}
10019
10020void
10021register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
10022 const struct target_desc *tdesc)
10023{
10024 struct remote_g_packet_data *data
10025 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
10026 struct remote_g_packet_guess new_guess, *guess;
10027 int ix;
10028
10029 gdb_assert (tdesc != NULL);
10030
10031 for (ix = 0;
10032 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10033 ix++)
10034 if (guess->bytes == bytes)
10035 internal_error (__FILE__, __LINE__,
9b20d036 10036 _("Duplicate g packet description added for size %d"),
29709017
DJ
10037 bytes);
10038
10039 new_guess.bytes = bytes;
10040 new_guess.tdesc = tdesc;
10041 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
10042}
10043
d962ef82
DJ
10044/* Return 1 if remote_read_description would do anything on this target
10045 and architecture, 0 otherwise. */
10046
10047static int
10048remote_read_description_p (struct target_ops *target)
10049{
10050 struct remote_g_packet_data *data
f5656ead 10051 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
d962ef82
DJ
10052
10053 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10054 return 1;
10055
10056 return 0;
10057}
10058
29709017
DJ
10059static const struct target_desc *
10060remote_read_description (struct target_ops *target)
10061{
10062 struct remote_g_packet_data *data
f5656ead 10063 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
29709017 10064
d962ef82
DJ
10065 /* Do not try this during initial connection, when we do not know
10066 whether there is a running but stopped thread. */
10067 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
2117c711 10068 return target->beneath->to_read_description (target->beneath);
d962ef82 10069
29709017
DJ
10070 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
10071 {
10072 struct remote_g_packet_guess *guess;
10073 int ix;
10074 int bytes = send_g_packet ();
10075
10076 for (ix = 0;
10077 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
10078 ix++)
10079 if (guess->bytes == bytes)
10080 return guess->tdesc;
10081
10082 /* We discard the g packet. A minor optimization would be to
10083 hold on to it, and fill the register cache once we have selected
10084 an architecture, but it's too tricky to do safely. */
10085 }
10086
2117c711 10087 return target->beneath->to_read_description (target->beneath);
29709017
DJ
10088}
10089
a6b151f1
DJ
10090/* Remote file transfer support. This is host-initiated I/O, not
10091 target-initiated; for target-initiated, see remote-fileio.c. */
10092
10093/* If *LEFT is at least the length of STRING, copy STRING to
10094 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10095 decrease *LEFT. Otherwise raise an error. */
10096
10097static void
10098remote_buffer_add_string (char **buffer, int *left, char *string)
10099{
10100 int len = strlen (string);
10101
10102 if (len > *left)
10103 error (_("Packet too long for target."));
10104
10105 memcpy (*buffer, string, len);
10106 *buffer += len;
10107 *left -= len;
10108
10109 /* NUL-terminate the buffer as a convenience, if there is
10110 room. */
10111 if (*left)
10112 **buffer = '\0';
10113}
10114
10115/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
10116 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10117 decrease *LEFT. Otherwise raise an error. */
10118
10119static void
10120remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
10121 int len)
10122{
10123 if (2 * len > *left)
10124 error (_("Packet too long for target."));
10125
10126 bin2hex (bytes, *buffer, len);
10127 *buffer += 2 * len;
10128 *left -= 2 * len;
10129
10130 /* NUL-terminate the buffer as a convenience, if there is
10131 room. */
10132 if (*left)
10133 **buffer = '\0';
10134}
10135
10136/* If *LEFT is large enough, convert VALUE to hex and add it to
10137 *BUFFER, update *BUFFER to point to the new end of the buffer, and
10138 decrease *LEFT. Otherwise raise an error. */
10139
10140static void
10141remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
10142{
10143 int len = hexnumlen (value);
10144
10145 if (len > *left)
10146 error (_("Packet too long for target."));
10147
10148 hexnumstr (*buffer, value);
10149 *buffer += len;
10150 *left -= len;
10151
10152 /* NUL-terminate the buffer as a convenience, if there is
10153 room. */
10154 if (*left)
10155 **buffer = '\0';
10156}
10157
10158/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
10159 value, *REMOTE_ERRNO to the remote error number or zero if none
10160 was included, and *ATTACHMENT to point to the start of the annex
10161 if any. The length of the packet isn't needed here; there may
10162 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
10163
10164 Return 0 if the packet could be parsed, -1 if it could not. If
10165 -1 is returned, the other variables may not be initialized. */
10166
10167static int
10168remote_hostio_parse_result (char *buffer, int *retcode,
10169 int *remote_errno, char **attachment)
10170{
10171 char *p, *p2;
10172
10173 *remote_errno = 0;
10174 *attachment = NULL;
10175
10176 if (buffer[0] != 'F')
10177 return -1;
10178
10179 errno = 0;
10180 *retcode = strtol (&buffer[1], &p, 16);
10181 if (errno != 0 || p == &buffer[1])
10182 return -1;
10183
10184 /* Check for ",errno". */
10185 if (*p == ',')
10186 {
10187 errno = 0;
10188 *remote_errno = strtol (p + 1, &p2, 16);
10189 if (errno != 0 || p + 1 == p2)
10190 return -1;
10191 p = p2;
10192 }
10193
10194 /* Check for ";attachment". If there is no attachment, the
10195 packet should end here. */
10196 if (*p == ';')
10197 {
10198 *attachment = p + 1;
10199 return 0;
10200 }
10201 else if (*p == '\0')
10202 return 0;
10203 else
10204 return -1;
10205}
10206
10207/* Send a prepared I/O packet to the target and read its response.
10208 The prepared packet is in the global RS->BUF before this function
10209 is called, and the answer is there when we return.
10210
10211 COMMAND_BYTES is the length of the request to send, which may include
10212 binary data. WHICH_PACKET is the packet configuration to check
10213 before attempting a packet. If an error occurs, *REMOTE_ERRNO
10214 is set to the error number and -1 is returned. Otherwise the value
10215 returned by the function is returned.
10216
10217 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
10218 attachment is expected; an error will be reported if there's a
10219 mismatch. If one is found, *ATTACHMENT will be set to point into
10220 the packet buffer and *ATTACHMENT_LEN will be set to the
10221 attachment's length. */
10222
10223static int
10224remote_hostio_send_command (int command_bytes, int which_packet,
10225 int *remote_errno, char **attachment,
10226 int *attachment_len)
10227{
10228 struct remote_state *rs = get_remote_state ();
10229 int ret, bytes_read;
10230 char *attachment_tmp;
10231
5d93a237 10232 if (!rs->remote_desc
4082afcc 10233 || packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
10234 {
10235 *remote_errno = FILEIO_ENOSYS;
10236 return -1;
10237 }
10238
10239 putpkt_binary (rs->buf, command_bytes);
10240 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
10241
10242 /* If it timed out, something is wrong. Don't try to parse the
10243 buffer. */
10244 if (bytes_read < 0)
10245 {
10246 *remote_errno = FILEIO_EINVAL;
10247 return -1;
10248 }
10249
10250 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
10251 {
10252 case PACKET_ERROR:
10253 *remote_errno = FILEIO_EINVAL;
10254 return -1;
10255 case PACKET_UNKNOWN:
10256 *remote_errno = FILEIO_ENOSYS;
10257 return -1;
10258 case PACKET_OK:
10259 break;
10260 }
10261
10262 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
10263 &attachment_tmp))
10264 {
10265 *remote_errno = FILEIO_EINVAL;
10266 return -1;
10267 }
10268
10269 /* Make sure we saw an attachment if and only if we expected one. */
10270 if ((attachment_tmp == NULL && attachment != NULL)
10271 || (attachment_tmp != NULL && attachment == NULL))
10272 {
10273 *remote_errno = FILEIO_EINVAL;
10274 return -1;
10275 }
10276
10277 /* If an attachment was found, it must point into the packet buffer;
10278 work out how many bytes there were. */
10279 if (attachment_tmp != NULL)
10280 {
10281 *attachment = attachment_tmp;
10282 *attachment_len = bytes_read - (*attachment - rs->buf);
10283 }
10284
10285 return ret;
10286}
10287
15a201c8
GB
10288/* Set the filesystem remote_hostio functions that take FILENAME
10289 arguments will use. Return 0 on success, or -1 if an error
10290 occurs (and set *REMOTE_ERRNO). */
10291
10292static int
10293remote_hostio_set_filesystem (struct inferior *inf, int *remote_errno)
10294{
10295 struct remote_state *rs = get_remote_state ();
10296 int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
10297 char *p = rs->buf;
10298 int left = get_remote_packet_size () - 1;
10299 char arg[9];
10300 int ret;
10301
10302 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10303 return 0;
10304
10305 if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
10306 return 0;
10307
10308 remote_buffer_add_string (&p, &left, "vFile:setfs:");
10309
10310 xsnprintf (arg, sizeof (arg), "%x", required_pid);
10311 remote_buffer_add_string (&p, &left, arg);
10312
10313 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_setfs,
10314 remote_errno, NULL, NULL);
10315
10316 if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
10317 return 0;
10318
10319 if (ret == 0)
10320 rs->fs_pid = required_pid;
10321
10322 return ret;
10323}
10324
12e2a5fd 10325/* Implementation of to_fileio_open. */
a6b151f1
DJ
10326
10327static int
cd897586 10328remote_hostio_open (struct target_ops *self,
07c138c8
GB
10329 struct inferior *inf, const char *filename,
10330 int flags, int mode, int *remote_errno)
a6b151f1
DJ
10331{
10332 struct remote_state *rs = get_remote_state ();
10333 char *p = rs->buf;
10334 int left = get_remote_packet_size () - 1;
10335
15a201c8
GB
10336 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10337 return -1;
10338
a6b151f1
DJ
10339 remote_buffer_add_string (&p, &left, "vFile:open:");
10340
10341 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10342 strlen (filename));
10343 remote_buffer_add_string (&p, &left, ",");
10344
10345 remote_buffer_add_int (&p, &left, flags);
10346 remote_buffer_add_string (&p, &left, ",");
10347
10348 remote_buffer_add_int (&p, &left, mode);
10349
10350 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
10351 remote_errno, NULL, NULL);
10352}
10353
12e2a5fd 10354/* Implementation of to_fileio_pwrite. */
a6b151f1
DJ
10355
10356static int
0d866f62
TT
10357remote_hostio_pwrite (struct target_ops *self,
10358 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
10359 ULONGEST offset, int *remote_errno)
10360{
10361 struct remote_state *rs = get_remote_state ();
10362 char *p = rs->buf;
10363 int left = get_remote_packet_size ();
10364 int out_len;
10365
10366 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
10367
10368 remote_buffer_add_int (&p, &left, fd);
10369 remote_buffer_add_string (&p, &left, ",");
10370
10371 remote_buffer_add_int (&p, &left, offset);
10372 remote_buffer_add_string (&p, &left, ",");
10373
124e13d9 10374 p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
a6b151f1
DJ
10375 get_remote_packet_size () - (p - rs->buf));
10376
10377 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
10378 remote_errno, NULL, NULL);
10379}
10380
12e2a5fd 10381/* Implementation of to_fileio_pread. */
a6b151f1
DJ
10382
10383static int
a3be983c
TT
10384remote_hostio_pread (struct target_ops *self,
10385 int fd, gdb_byte *read_buf, int len,
a6b151f1
DJ
10386 ULONGEST offset, int *remote_errno)
10387{
10388 struct remote_state *rs = get_remote_state ();
10389 char *p = rs->buf;
10390 char *attachment;
10391 int left = get_remote_packet_size ();
10392 int ret, attachment_len;
10393 int read_len;
10394
10395 remote_buffer_add_string (&p, &left, "vFile:pread:");
10396
10397 remote_buffer_add_int (&p, &left, fd);
10398 remote_buffer_add_string (&p, &left, ",");
10399
10400 remote_buffer_add_int (&p, &left, len);
10401 remote_buffer_add_string (&p, &left, ",");
10402
10403 remote_buffer_add_int (&p, &left, offset);
10404
10405 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
10406 remote_errno, &attachment,
10407 &attachment_len);
10408
10409 if (ret < 0)
10410 return ret;
10411
bc20a4af 10412 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
10413 read_buf, len);
10414 if (read_len != ret)
10415 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
10416
10417 return ret;
10418}
10419
12e2a5fd 10420/* Implementation of to_fileio_close. */
a6b151f1
DJ
10421
10422static int
df39ea25 10423remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
10424{
10425 struct remote_state *rs = get_remote_state ();
10426 char *p = rs->buf;
10427 int left = get_remote_packet_size () - 1;
10428
10429 remote_buffer_add_string (&p, &left, "vFile:close:");
10430
10431 remote_buffer_add_int (&p, &left, fd);
10432
10433 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
10434 remote_errno, NULL, NULL);
10435}
10436
12e2a5fd 10437/* Implementation of to_fileio_unlink. */
a6b151f1
DJ
10438
10439static int
dbbca37d 10440remote_hostio_unlink (struct target_ops *self,
07c138c8
GB
10441 struct inferior *inf, const char *filename,
10442 int *remote_errno)
a6b151f1
DJ
10443{
10444 struct remote_state *rs = get_remote_state ();
10445 char *p = rs->buf;
10446 int left = get_remote_packet_size () - 1;
10447
15a201c8
GB
10448 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10449 return -1;
10450
a6b151f1
DJ
10451 remote_buffer_add_string (&p, &left, "vFile:unlink:");
10452
10453 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10454 strlen (filename));
10455
10456 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
10457 remote_errno, NULL, NULL);
10458}
10459
12e2a5fd 10460/* Implementation of to_fileio_readlink. */
b9e7b9c3
UW
10461
10462static char *
fab5aa7c 10463remote_hostio_readlink (struct target_ops *self,
07c138c8
GB
10464 struct inferior *inf, const char *filename,
10465 int *remote_errno)
b9e7b9c3
UW
10466{
10467 struct remote_state *rs = get_remote_state ();
10468 char *p = rs->buf;
10469 char *attachment;
10470 int left = get_remote_packet_size ();
10471 int len, attachment_len;
10472 int read_len;
10473 char *ret;
10474
15a201c8
GB
10475 if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
10476 return NULL;
10477
b9e7b9c3
UW
10478 remote_buffer_add_string (&p, &left, "vFile:readlink:");
10479
10480 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10481 strlen (filename));
10482
10483 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
10484 remote_errno, &attachment,
10485 &attachment_len);
10486
10487 if (len < 0)
10488 return NULL;
10489
10490 ret = xmalloc (len + 1);
10491
bc20a4af
PA
10492 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10493 (gdb_byte *) ret, len);
b9e7b9c3
UW
10494 if (read_len != len)
10495 error (_("Readlink returned %d, but %d bytes."), len, read_len);
10496
10497 ret[len] = '\0';
10498 return ret;
10499}
10500
12e2a5fd 10501/* Implementation of to_fileio_fstat. */
0a93529c
GB
10502
10503static int
10504remote_hostio_fstat (struct target_ops *self,
10505 int fd, struct stat *st,
10506 int *remote_errno)
10507{
10508 struct remote_state *rs = get_remote_state ();
10509 char *p = rs->buf;
10510 int left = get_remote_packet_size ();
10511 int attachment_len, ret;
10512 char *attachment;
10513 struct fio_stat fst;
10514 int read_len;
10515
464b0089
GB
10516 remote_buffer_add_string (&p, &left, "vFile:fstat:");
10517
10518 remote_buffer_add_int (&p, &left, fd);
10519
10520 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
10521 remote_errno, &attachment,
10522 &attachment_len);
10523 if (ret < 0)
0a93529c 10524 {
464b0089
GB
10525 if (*remote_errno != FILEIO_ENOSYS)
10526 return ret;
10527
0a93529c
GB
10528 /* Strictly we should return -1, ENOSYS here, but when
10529 "set sysroot remote:" was implemented in August 2008
10530 BFD's need for a stat function was sidestepped with
10531 this hack. This was not remedied until March 2015
10532 so we retain the previous behavior to avoid breaking
10533 compatibility.
10534
10535 Note that the memset is a March 2015 addition; older
10536 GDBs set st_size *and nothing else* so the structure
10537 would have garbage in all other fields. This might
10538 break something but retaining the previous behavior
10539 here would be just too wrong. */
10540
10541 memset (st, 0, sizeof (struct stat));
10542 st->st_size = INT_MAX;
10543 return 0;
10544 }
10545
0a93529c
GB
10546 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10547 (gdb_byte *) &fst, sizeof (fst));
10548
10549 if (read_len != ret)
10550 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
10551
10552 if (read_len != sizeof (fst))
10553 error (_("vFile:fstat returned %d bytes, but expecting %d."),
10554 read_len, (int) sizeof (fst));
10555
10556 remote_fileio_to_host_stat (&fst, st);
10557
10558 return 0;
10559}
10560
12e2a5fd 10561/* Implementation of to_filesystem_is_local. */
e3dd7556
GB
10562
10563static int
10564remote_filesystem_is_local (struct target_ops *self)
10565{
10566 /* Valgrind GDB presents itself as a remote target but works
10567 on the local filesystem: it does not implement remote get
10568 and users are not expected to set a sysroot. To handle
10569 this case we treat the remote filesystem as local if the
10570 sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
10571 does not support vFile:open. */
a3be80c3 10572 if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) == 0)
e3dd7556
GB
10573 {
10574 enum packet_support ps = packet_support (PACKET_vFile_open);
10575
10576 if (ps == PACKET_SUPPORT_UNKNOWN)
10577 {
10578 int fd, remote_errno;
10579
10580 /* Try opening a file to probe support. The supplied
10581 filename is irrelevant, we only care about whether
10582 the stub recognizes the packet or not. */
07c138c8 10583 fd = remote_hostio_open (self, NULL, "just probing",
e3dd7556
GB
10584 FILEIO_O_RDONLY, 0700,
10585 &remote_errno);
10586
10587 if (fd >= 0)
10588 remote_hostio_close (self, fd, &remote_errno);
10589
10590 ps = packet_support (PACKET_vFile_open);
10591 }
10592
10593 if (ps == PACKET_DISABLE)
10594 {
10595 static int warning_issued = 0;
10596
10597 if (!warning_issued)
10598 {
10599 warning (_("remote target does not support file"
10600 " transfer, attempting to access files"
10601 " from local filesystem."));
10602 warning_issued = 1;
10603 }
10604
10605 return 1;
10606 }
10607 }
10608
10609 return 0;
10610}
10611
a6b151f1
DJ
10612static int
10613remote_fileio_errno_to_host (int errnum)
10614{
10615 switch (errnum)
10616 {
10617 case FILEIO_EPERM:
10618 return EPERM;
10619 case FILEIO_ENOENT:
10620 return ENOENT;
10621 case FILEIO_EINTR:
10622 return EINTR;
10623 case FILEIO_EIO:
10624 return EIO;
10625 case FILEIO_EBADF:
10626 return EBADF;
10627 case FILEIO_EACCES:
10628 return EACCES;
10629 case FILEIO_EFAULT:
10630 return EFAULT;
10631 case FILEIO_EBUSY:
10632 return EBUSY;
10633 case FILEIO_EEXIST:
10634 return EEXIST;
10635 case FILEIO_ENODEV:
10636 return ENODEV;
10637 case FILEIO_ENOTDIR:
10638 return ENOTDIR;
10639 case FILEIO_EISDIR:
10640 return EISDIR;
10641 case FILEIO_EINVAL:
10642 return EINVAL;
10643 case FILEIO_ENFILE:
10644 return ENFILE;
10645 case FILEIO_EMFILE:
10646 return EMFILE;
10647 case FILEIO_EFBIG:
10648 return EFBIG;
10649 case FILEIO_ENOSPC:
10650 return ENOSPC;
10651 case FILEIO_ESPIPE:
10652 return ESPIPE;
10653 case FILEIO_EROFS:
10654 return EROFS;
10655 case FILEIO_ENOSYS:
10656 return ENOSYS;
10657 case FILEIO_ENAMETOOLONG:
10658 return ENAMETOOLONG;
10659 }
10660 return -1;
10661}
10662
10663static char *
10664remote_hostio_error (int errnum)
10665{
10666 int host_error = remote_fileio_errno_to_host (errnum);
10667
10668 if (host_error == -1)
10669 error (_("Unknown remote I/O error %d"), errnum);
10670 else
10671 error (_("Remote I/O error: %s"), safe_strerror (host_error));
10672}
10673
a6b151f1
DJ
10674static void
10675remote_hostio_close_cleanup (void *opaque)
10676{
10677 int fd = *(int *) opaque;
10678 int remote_errno;
10679
df39ea25 10680 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
a6b151f1
DJ
10681}
10682
10683void
10684remote_file_put (const char *local_file, const char *remote_file, int from_tty)
10685{
10686 struct cleanup *back_to, *close_cleanup;
10687 int retcode, fd, remote_errno, bytes, io_size;
10688 FILE *file;
10689 gdb_byte *buffer;
10690 int bytes_in_buffer;
10691 int saw_eof;
10692 ULONGEST offset;
5d93a237 10693 struct remote_state *rs = get_remote_state ();
a6b151f1 10694
5d93a237 10695 if (!rs->remote_desc)
a6b151f1
DJ
10696 error (_("command can only be used with remote target"));
10697
614c279d 10698 file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
10699 if (file == NULL)
10700 perror_with_name (local_file);
7c8a8b04 10701 back_to = make_cleanup_fclose (file);
a6b151f1 10702
07c138c8 10703 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 10704 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
a6b151f1
DJ
10705 | FILEIO_O_TRUNC),
10706 0700, &remote_errno);
10707 if (fd == -1)
10708 remote_hostio_error (remote_errno);
10709
10710 /* Send up to this many bytes at once. They won't all fit in the
10711 remote packet limit, so we'll transfer slightly fewer. */
10712 io_size = get_remote_packet_size ();
10713 buffer = xmalloc (io_size);
10714 make_cleanup (xfree, buffer);
10715
10716 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10717
10718 bytes_in_buffer = 0;
10719 saw_eof = 0;
10720 offset = 0;
10721 while (bytes_in_buffer || !saw_eof)
10722 {
10723 if (!saw_eof)
10724 {
3e43a32a
MS
10725 bytes = fread (buffer + bytes_in_buffer, 1,
10726 io_size - bytes_in_buffer,
a6b151f1
DJ
10727 file);
10728 if (bytes == 0)
10729 {
10730 if (ferror (file))
10731 error (_("Error reading %s."), local_file);
10732 else
10733 {
10734 /* EOF. Unless there is something still in the
10735 buffer from the last iteration, we are done. */
10736 saw_eof = 1;
10737 if (bytes_in_buffer == 0)
10738 break;
10739 }
10740 }
10741 }
10742 else
10743 bytes = 0;
10744
10745 bytes += bytes_in_buffer;
10746 bytes_in_buffer = 0;
10747
0d866f62
TT
10748 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
10749 fd, buffer, bytes,
3e43a32a 10750 offset, &remote_errno);
a6b151f1
DJ
10751
10752 if (retcode < 0)
10753 remote_hostio_error (remote_errno);
10754 else if (retcode == 0)
10755 error (_("Remote write of %d bytes returned 0!"), bytes);
10756 else if (retcode < bytes)
10757 {
10758 /* Short write. Save the rest of the read data for the next
10759 write. */
10760 bytes_in_buffer = bytes - retcode;
10761 memmove (buffer, buffer + retcode, bytes_in_buffer);
10762 }
10763
10764 offset += retcode;
10765 }
10766
10767 discard_cleanups (close_cleanup);
df39ea25 10768 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10769 remote_hostio_error (remote_errno);
10770
10771 if (from_tty)
10772 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
10773 do_cleanups (back_to);
10774}
10775
10776void
10777remote_file_get (const char *remote_file, const char *local_file, int from_tty)
10778{
10779 struct cleanup *back_to, *close_cleanup;
cea39f65 10780 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
10781 FILE *file;
10782 gdb_byte *buffer;
10783 ULONGEST offset;
5d93a237 10784 struct remote_state *rs = get_remote_state ();
a6b151f1 10785
5d93a237 10786 if (!rs->remote_desc)
a6b151f1
DJ
10787 error (_("command can only be used with remote target"));
10788
07c138c8 10789 fd = remote_hostio_open (find_target_at (process_stratum), NULL,
cd897586 10790 remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
a6b151f1
DJ
10791 if (fd == -1)
10792 remote_hostio_error (remote_errno);
10793
614c279d 10794 file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
10795 if (file == NULL)
10796 perror_with_name (local_file);
7c8a8b04 10797 back_to = make_cleanup_fclose (file);
a6b151f1
DJ
10798
10799 /* Send up to this many bytes at once. They won't all fit in the
10800 remote packet limit, so we'll transfer slightly fewer. */
10801 io_size = get_remote_packet_size ();
10802 buffer = xmalloc (io_size);
10803 make_cleanup (xfree, buffer);
10804
10805 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10806
10807 offset = 0;
10808 while (1)
10809 {
a3be983c
TT
10810 bytes = remote_hostio_pread (find_target_at (process_stratum),
10811 fd, buffer, io_size, offset, &remote_errno);
a6b151f1
DJ
10812 if (bytes == 0)
10813 /* Success, but no bytes, means end-of-file. */
10814 break;
10815 if (bytes == -1)
10816 remote_hostio_error (remote_errno);
10817
10818 offset += bytes;
10819
10820 bytes = fwrite (buffer, 1, bytes, file);
10821 if (bytes == 0)
10822 perror_with_name (local_file);
10823 }
10824
10825 discard_cleanups (close_cleanup);
df39ea25 10826 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10827 remote_hostio_error (remote_errno);
10828
10829 if (from_tty)
10830 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
10831 do_cleanups (back_to);
10832}
10833
10834void
10835remote_file_delete (const char *remote_file, int from_tty)
10836{
10837 int retcode, remote_errno;
5d93a237 10838 struct remote_state *rs = get_remote_state ();
a6b151f1 10839
5d93a237 10840 if (!rs->remote_desc)
a6b151f1
DJ
10841 error (_("command can only be used with remote target"));
10842
dbbca37d 10843 retcode = remote_hostio_unlink (find_target_at (process_stratum),
07c138c8 10844 NULL, remote_file, &remote_errno);
a6b151f1
DJ
10845 if (retcode == -1)
10846 remote_hostio_error (remote_errno);
10847
10848 if (from_tty)
10849 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
10850}
10851
10852static void
10853remote_put_command (char *args, int from_tty)
10854{
10855 struct cleanup *back_to;
10856 char **argv;
10857
d1a41061
PP
10858 if (args == NULL)
10859 error_no_arg (_("file to put"));
10860
10861 argv = gdb_buildargv (args);
a6b151f1
DJ
10862 back_to = make_cleanup_freeargv (argv);
10863 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10864 error (_("Invalid parameters to remote put"));
10865
10866 remote_file_put (argv[0], argv[1], from_tty);
10867
10868 do_cleanups (back_to);
10869}
10870
10871static void
10872remote_get_command (char *args, int from_tty)
10873{
10874 struct cleanup *back_to;
10875 char **argv;
10876
d1a41061
PP
10877 if (args == NULL)
10878 error_no_arg (_("file to get"));
10879
10880 argv = gdb_buildargv (args);
a6b151f1
DJ
10881 back_to = make_cleanup_freeargv (argv);
10882 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10883 error (_("Invalid parameters to remote get"));
10884
10885 remote_file_get (argv[0], argv[1], from_tty);
10886
10887 do_cleanups (back_to);
10888}
10889
10890static void
10891remote_delete_command (char *args, int from_tty)
10892{
10893 struct cleanup *back_to;
10894 char **argv;
10895
d1a41061
PP
10896 if (args == NULL)
10897 error_no_arg (_("file to delete"));
10898
10899 argv = gdb_buildargv (args);
a6b151f1
DJ
10900 back_to = make_cleanup_freeargv (argv);
10901 if (argv[0] == NULL || argv[1] != NULL)
10902 error (_("Invalid parameters to remote delete"));
10903
10904 remote_file_delete (argv[0], from_tty);
10905
10906 do_cleanups (back_to);
10907}
10908
10909static void
10910remote_command (char *args, int from_tty)
10911{
635c7e8a 10912 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
10913}
10914
b2175913 10915static int
19db3e69 10916remote_can_execute_reverse (struct target_ops *self)
b2175913 10917{
4082afcc
PA
10918 if (packet_support (PACKET_bs) == PACKET_ENABLE
10919 || packet_support (PACKET_bc) == PACKET_ENABLE)
40ab02ce
MS
10920 return 1;
10921 else
10922 return 0;
b2175913
MS
10923}
10924
74531fed 10925static int
2a9a2795 10926remote_supports_non_stop (struct target_ops *self)
74531fed
PA
10927{
10928 return 1;
10929}
10930
03583c20 10931static int
2bfc0540 10932remote_supports_disable_randomization (struct target_ops *self)
03583c20
UW
10933{
10934 /* Only supported in extended mode. */
10935 return 0;
10936}
10937
8a305172 10938static int
86ce2668 10939remote_supports_multi_process (struct target_ops *self)
8a305172
PA
10940{
10941 struct remote_state *rs = get_remote_state ();
a744cf53 10942
901f9912
UW
10943 /* Only extended-remote handles being attached to multiple
10944 processes, even though plain remote can use the multi-process
10945 thread id extensions, so that GDB knows the target process's
10946 PID. */
10947 return rs->extended && remote_multi_process_p (rs);
8a305172
PA
10948}
10949
70221824 10950static int
782b2b07
SS
10951remote_supports_cond_tracepoints (void)
10952{
4082afcc 10953 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
10954}
10955
3788aec7 10956static int
efcc2da7 10957remote_supports_cond_breakpoints (struct target_ops *self)
3788aec7 10958{
4082afcc 10959 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
10960}
10961
70221824 10962static int
7a697b8d
SS
10963remote_supports_fast_tracepoints (void)
10964{
4082afcc 10965 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
10966}
10967
0fb4aa4b
PA
10968static int
10969remote_supports_static_tracepoints (void)
10970{
4082afcc 10971 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
10972}
10973
1e4d1764
YQ
10974static int
10975remote_supports_install_in_trace (void)
10976{
4082afcc 10977 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
10978}
10979
d248b706 10980static int
7d178d6a 10981remote_supports_enable_disable_tracepoint (struct target_ops *self)
d248b706 10982{
4082afcc
PA
10983 return (packet_support (PACKET_EnableDisableTracepoints_feature)
10984 == PACKET_ENABLE);
d248b706
KY
10985}
10986
3065dfb6 10987static int
6de37a3a 10988remote_supports_string_tracing (struct target_ops *self)
3065dfb6 10989{
4082afcc 10990 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
10991}
10992
d3ce09f5 10993static int
78eff0ec 10994remote_can_run_breakpoint_commands (struct target_ops *self)
d3ce09f5 10995{
4082afcc 10996 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
10997}
10998
35b1e5cc 10999static void
ecae04e1 11000remote_trace_init (struct target_ops *self)
35b1e5cc
SS
11001{
11002 putpkt ("QTinit");
11003 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99 11004 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11005 error (_("Target does not support this command."));
11006}
11007
11008static void free_actions_list (char **actions_list);
11009static void free_actions_list_cleanup_wrapper (void *);
11010static void
11011free_actions_list_cleanup_wrapper (void *al)
11012{
11013 free_actions_list (al);
11014}
11015
11016static void
11017free_actions_list (char **actions_list)
11018{
11019 int ndx;
11020
11021 if (actions_list == 0)
11022 return;
11023
11024 for (ndx = 0; actions_list[ndx]; ndx++)
11025 xfree (actions_list[ndx]);
11026
11027 xfree (actions_list);
11028}
11029
409873ef
SS
11030/* Recursive routine to walk through command list including loops, and
11031 download packets for each command. */
11032
11033static void
11034remote_download_command_source (int num, ULONGEST addr,
11035 struct command_line *cmds)
11036{
11037 struct remote_state *rs = get_remote_state ();
11038 struct command_line *cmd;
11039
11040 for (cmd = cmds; cmd; cmd = cmd->next)
11041 {
0df8b418 11042 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11043 strcpy (rs->buf, "QTDPsrc:");
11044 encode_source_string (num, addr, "cmd", cmd->line,
11045 rs->buf + strlen (rs->buf),
11046 rs->buf_size - strlen (rs->buf));
11047 putpkt (rs->buf);
11048 remote_get_noisy_reply (&target_buf, &target_buf_size);
11049 if (strcmp (target_buf, "OK"))
11050 warning (_("Target does not support source download."));
11051
11052 if (cmd->control_type == while_control
11053 || cmd->control_type == while_stepping_control)
11054 {
11055 remote_download_command_source (num, addr, *cmd->body_list);
11056
0df8b418 11057 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
11058 strcpy (rs->buf, "QTDPsrc:");
11059 encode_source_string (num, addr, "cmd", "end",
11060 rs->buf + strlen (rs->buf),
11061 rs->buf_size - strlen (rs->buf));
11062 putpkt (rs->buf);
11063 remote_get_noisy_reply (&target_buf, &target_buf_size);
11064 if (strcmp (target_buf, "OK"))
11065 warning (_("Target does not support source download."));
11066 }
11067 }
11068}
11069
35b1e5cc 11070static void
548f7808 11071remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
35b1e5cc 11072{
bba74b36 11073#define BUF_SIZE 2048
e8ba3115 11074
35b1e5cc 11075 CORE_ADDR tpaddr;
409873ef 11076 char addrbuf[40];
bba74b36 11077 char buf[BUF_SIZE];
35b1e5cc
SS
11078 char **tdp_actions;
11079 char **stepping_actions;
11080 int ndx;
11081 struct cleanup *old_chain = NULL;
11082 struct agent_expr *aexpr;
11083 struct cleanup *aexpr_chain = NULL;
11084 char *pkt;
e8ba3115 11085 struct breakpoint *b = loc->owner;
d9b3f62e 11086 struct tracepoint *t = (struct tracepoint *) b;
35b1e5cc 11087
dc673c81 11088 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
11089 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
11090 tdp_actions);
11091 (void) make_cleanup (free_actions_list_cleanup_wrapper,
11092 stepping_actions);
11093
11094 tpaddr = loc->address;
11095 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
11096 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
11097 addrbuf, /* address */
11098 (b->enable_state == bp_enabled ? 'E' : 'D'),
11099 t->step_count, t->pass_count);
e8ba3115
YQ
11100 /* Fast tracepoints are mostly handled by the target, but we can
11101 tell the target how big of an instruction block should be moved
11102 around. */
11103 if (b->type == bp_fast_tracepoint)
11104 {
11105 /* Only test for support at download time; we may not know
11106 target capabilities at definition time. */
11107 if (remote_supports_fast_tracepoints ())
35b1e5cc 11108 {
e8ba3115 11109 int isize;
35b1e5cc 11110
f5656ead 11111 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
e8ba3115 11112 tpaddr, &isize, NULL))
bba74b36
YQ
11113 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
11114 isize);
35b1e5cc 11115 else
e8ba3115
YQ
11116 /* If it passed validation at definition but fails now,
11117 something is very wrong. */
11118 internal_error (__FILE__, __LINE__,
11119 _("Fast tracepoint not "
11120 "valid during download"));
35b1e5cc 11121 }
e8ba3115
YQ
11122 else
11123 /* Fast tracepoints are functionally identical to regular
11124 tracepoints, so don't take lack of support as a reason to
11125 give up on the trace run. */
11126 warning (_("Target does not support fast tracepoints, "
11127 "downloading %d as regular tracepoint"), b->number);
11128 }
11129 else if (b->type == bp_static_tracepoint)
11130 {
11131 /* Only test for support at download time; we may not know
11132 target capabilities at definition time. */
11133 if (remote_supports_static_tracepoints ())
0fb4aa4b 11134 {
e8ba3115 11135 struct static_tracepoint_marker marker;
0fb4aa4b 11136
e8ba3115
YQ
11137 if (target_static_tracepoint_marker_at (tpaddr, &marker))
11138 strcat (buf, ":S");
0fb4aa4b 11139 else
e8ba3115 11140 error (_("Static tracepoint not valid during download"));
0fb4aa4b 11141 }
e8ba3115
YQ
11142 else
11143 /* Fast tracepoints are functionally identical to regular
11144 tracepoints, so don't take lack of support as a reason
11145 to give up on the trace run. */
11146 error (_("Target does not support static tracepoints"));
11147 }
11148 /* If the tracepoint has a conditional, make it into an agent
11149 expression and append to the definition. */
11150 if (loc->cond)
11151 {
11152 /* Only test support at download time, we may not know target
11153 capabilities at definition time. */
11154 if (remote_supports_cond_tracepoints ())
35b1e5cc 11155 {
e8ba3115
YQ
11156 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
11157 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
bba74b36
YQ
11158 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
11159 aexpr->len);
e8ba3115
YQ
11160 pkt = buf + strlen (buf);
11161 for (ndx = 0; ndx < aexpr->len; ++ndx)
11162 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
11163 *pkt = '\0';
11164 do_cleanups (aexpr_chain);
35b1e5cc 11165 }
e8ba3115
YQ
11166 else
11167 warning (_("Target does not support conditional tracepoints, "
11168 "ignoring tp %d cond"), b->number);
11169 }
35b1e5cc 11170
d9b3f62e 11171 if (b->commands || *default_collect)
e8ba3115
YQ
11172 strcat (buf, "-");
11173 putpkt (buf);
11174 remote_get_noisy_reply (&target_buf, &target_buf_size);
11175 if (strcmp (target_buf, "OK"))
11176 error (_("Target does not support tracepoints."));
35b1e5cc 11177
e8ba3115
YQ
11178 /* do_single_steps (t); */
11179 if (tdp_actions)
11180 {
11181 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 11182 {
e8ba3115 11183 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11184 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
11185 b->number, addrbuf, /* address */
11186 tdp_actions[ndx],
11187 ((tdp_actions[ndx + 1] || stepping_actions)
11188 ? '-' : 0));
e8ba3115
YQ
11189 putpkt (buf);
11190 remote_get_noisy_reply (&target_buf,
11191 &target_buf_size);
11192 if (strcmp (target_buf, "OK"))
11193 error (_("Error on target while setting tracepoints."));
35b1e5cc 11194 }
e8ba3115
YQ
11195 }
11196 if (stepping_actions)
11197 {
11198 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 11199 {
e8ba3115 11200 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
11201 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
11202 b->number, addrbuf, /* address */
11203 ((ndx == 0) ? "S" : ""),
11204 stepping_actions[ndx],
11205 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115
YQ
11206 putpkt (buf);
11207 remote_get_noisy_reply (&target_buf,
11208 &target_buf_size);
11209 if (strcmp (target_buf, "OK"))
11210 error (_("Error on target while setting tracepoints."));
35b1e5cc 11211 }
e8ba3115 11212 }
409873ef 11213
4082afcc 11214 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115
YQ
11215 {
11216 if (b->addr_string)
409873ef 11217 {
e8ba3115
YQ
11218 strcpy (buf, "QTDPsrc:");
11219 encode_source_string (b->number, loc->address,
11220 "at", b->addr_string, buf + strlen (buf),
11221 2048 - strlen (buf));
409873ef 11222
e8ba3115
YQ
11223 putpkt (buf);
11224 remote_get_noisy_reply (&target_buf, &target_buf_size);
11225 if (strcmp (target_buf, "OK"))
11226 warning (_("Target does not support source download."));
409873ef 11227 }
e8ba3115
YQ
11228 if (b->cond_string)
11229 {
11230 strcpy (buf, "QTDPsrc:");
11231 encode_source_string (b->number, loc->address,
11232 "cond", b->cond_string, buf + strlen (buf),
11233 2048 - strlen (buf));
11234 putpkt (buf);
11235 remote_get_noisy_reply (&target_buf, &target_buf_size);
11236 if (strcmp (target_buf, "OK"))
11237 warning (_("Target does not support source download."));
11238 }
11239 remote_download_command_source (b->number, loc->address,
11240 breakpoint_commands (b));
35b1e5cc 11241 }
e8ba3115
YQ
11242
11243 do_cleanups (old_chain);
35b1e5cc
SS
11244}
11245
1e4d1764 11246static int
a52a8357 11247remote_can_download_tracepoint (struct target_ops *self)
1e4d1764 11248{
1e51243a
PA
11249 struct remote_state *rs = get_remote_state ();
11250 struct trace_status *ts;
11251 int status;
11252
11253 /* Don't try to install tracepoints until we've relocated our
11254 symbols, and fetched and merged the target's tracepoint list with
11255 ours. */
11256 if (rs->starting_up)
11257 return 0;
11258
11259 ts = current_trace_status ();
8bd200f1 11260 status = remote_get_trace_status (self, ts);
1e4d1764
YQ
11261
11262 if (status == -1 || !ts->running_known || !ts->running)
11263 return 0;
11264
11265 /* If we are in a tracing experiment, but remote stub doesn't support
11266 installing tracepoint in trace, we have to return. */
11267 if (!remote_supports_install_in_trace ())
11268 return 0;
11269
11270 return 1;
11271}
11272
11273
35b1e5cc 11274static void
559d2b81
TT
11275remote_download_trace_state_variable (struct target_ops *self,
11276 struct trace_state_variable *tsv)
35b1e5cc
SS
11277{
11278 struct remote_state *rs = get_remote_state ();
00bf0b85 11279 char *p;
35b1e5cc 11280
bba74b36
YQ
11281 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
11282 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
11283 tsv->builtin);
00bf0b85
SS
11284 p = rs->buf + strlen (rs->buf);
11285 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
11286 error (_("Trace state variable name too long for tsv definition packet"));
9f1b45b0 11287 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
00bf0b85 11288 *p++ = '\0';
35b1e5cc
SS
11289 putpkt (rs->buf);
11290 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11291 if (*target_buf == '\0')
11292 error (_("Target does not support this command."));
11293 if (strcmp (target_buf, "OK") != 0)
11294 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
11295}
11296
d248b706 11297static void
46670d57
TT
11298remote_enable_tracepoint (struct target_ops *self,
11299 struct bp_location *location)
d248b706
KY
11300{
11301 struct remote_state *rs = get_remote_state ();
11302 char addr_buf[40];
11303
11304 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11305 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
11306 location->owner->number, addr_buf);
d248b706
KY
11307 putpkt (rs->buf);
11308 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11309 if (*rs->buf == '\0')
11310 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
11311 if (strcmp (rs->buf, "OK") != 0)
11312 error (_("Error on target while enabling tracepoint."));
11313}
11314
11315static void
780b049c
TT
11316remote_disable_tracepoint (struct target_ops *self,
11317 struct bp_location *location)
d248b706
KY
11318{
11319 struct remote_state *rs = get_remote_state ();
11320 char addr_buf[40];
11321
11322 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
11323 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
11324 location->owner->number, addr_buf);
d248b706
KY
11325 putpkt (rs->buf);
11326 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11327 if (*rs->buf == '\0')
11328 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
11329 if (strcmp (rs->buf, "OK") != 0)
11330 error (_("Error on target while disabling tracepoint."));
11331}
11332
35b1e5cc 11333static void
583f9a86 11334remote_trace_set_readonly_regions (struct target_ops *self)
35b1e5cc
SS
11335{
11336 asection *s;
81b9b86e 11337 bfd *abfd = NULL;
35b1e5cc 11338 bfd_size_type size;
608bcef2 11339 bfd_vma vma;
35b1e5cc 11340 int anysecs = 0;
c2fa21f1 11341 int offset = 0;
35b1e5cc
SS
11342
11343 if (!exec_bfd)
11344 return; /* No information to give. */
11345
11346 strcpy (target_buf, "QTro");
9779ab84 11347 offset = strlen (target_buf);
35b1e5cc
SS
11348 for (s = exec_bfd->sections; s; s = s->next)
11349 {
11350 char tmp1[40], tmp2[40];
c2fa21f1 11351 int sec_length;
35b1e5cc
SS
11352
11353 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 11354 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
11355 (s->flags & SEC_READONLY) == 0)
11356 continue;
11357
11358 anysecs = 1;
81b9b86e 11359 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 11360 size = bfd_get_section_size (s);
608bcef2
HZ
11361 sprintf_vma (tmp1, vma);
11362 sprintf_vma (tmp2, vma + size);
c2fa21f1
HZ
11363 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
11364 if (offset + sec_length + 1 > target_buf_size)
11365 {
4082afcc 11366 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 11367 warning (_("\
c2fa21f1
HZ
11368Too many sections for read-only sections definition packet."));
11369 break;
11370 }
bba74b36
YQ
11371 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
11372 tmp1, tmp2);
c2fa21f1 11373 offset += sec_length;
35b1e5cc
SS
11374 }
11375 if (anysecs)
11376 {
11377 putpkt (target_buf);
11378 getpkt (&target_buf, &target_buf_size, 0);
11379 }
11380}
11381
11382static void
e2d1aae3 11383remote_trace_start (struct target_ops *self)
35b1e5cc
SS
11384{
11385 putpkt ("QTStart");
11386 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11387 if (*target_buf == '\0')
11388 error (_("Target does not support this command."));
11389 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11390 error (_("Bogus reply from target: %s"), target_buf);
11391}
11392
11393static int
8bd200f1 11394remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
35b1e5cc 11395{
953b98d1 11396 /* Initialize it just to avoid a GCC false warning. */
f652de6f 11397 char *p = NULL;
0df8b418 11398 /* FIXME we need to get register block size some other way. */
00bf0b85 11399 extern int trace_regblock_size;
bd3eecc3
PA
11400 enum packet_result result;
11401
4082afcc 11402 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 11403 return -1;
a744cf53 11404
00bf0b85
SS
11405 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
11406
049dc89b
JK
11407 putpkt ("qTStatus");
11408
492d29ea 11409 TRY
67f41397
JK
11410 {
11411 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
11412 }
492d29ea 11413 CATCH (ex, RETURN_MASK_ERROR)
67f41397 11414 {
598d3636
JK
11415 if (ex.error != TARGET_CLOSE_ERROR)
11416 {
11417 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
11418 return -1;
11419 }
11420 throw_exception (ex);
67f41397 11421 }
492d29ea 11422 END_CATCH
00bf0b85 11423
bd3eecc3
PA
11424 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
11425
00bf0b85 11426 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 11427 if (result == PACKET_UNKNOWN)
00bf0b85 11428 return -1;
35b1e5cc 11429
00bf0b85 11430 /* We're working with a live target. */
f5911ea1 11431 ts->filename = NULL;
00bf0b85 11432
00bf0b85 11433 if (*p++ != 'T')
35b1e5cc
SS
11434 error (_("Bogus trace status reply from target: %s"), target_buf);
11435
84cebc4a
YQ
11436 /* Function 'parse_trace_status' sets default value of each field of
11437 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
11438 parse_trace_status (p, ts);
11439
11440 return ts->running;
35b1e5cc
SS
11441}
11442
70221824 11443static void
db90e85c 11444remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
f196051f
SS
11445 struct uploaded_tp *utp)
11446{
11447 struct remote_state *rs = get_remote_state ();
f196051f
SS
11448 char *reply;
11449 struct bp_location *loc;
11450 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 11451 size_t size = get_remote_packet_size ();
f196051f
SS
11452
11453 if (tp)
11454 {
11455 tp->base.hit_count = 0;
11456 tp->traceframe_usage = 0;
11457 for (loc = tp->base.loc; loc; loc = loc->next)
11458 {
11459 /* If the tracepoint was never downloaded, don't go asking for
11460 any status. */
11461 if (tp->number_on_target == 0)
11462 continue;
bba74b36
YQ
11463 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
11464 phex_nz (loc->address, 0));
f196051f
SS
11465 putpkt (rs->buf);
11466 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11467 if (reply && *reply)
11468 {
11469 if (*reply == 'V')
11470 parse_tracepoint_status (reply + 1, bp, utp);
11471 }
11472 }
11473 }
11474 else if (utp)
11475 {
11476 utp->hit_count = 0;
11477 utp->traceframe_usage = 0;
bba74b36
YQ
11478 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
11479 phex_nz (utp->addr, 0));
f196051f
SS
11480 putpkt (rs->buf);
11481 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11482 if (reply && *reply)
11483 {
11484 if (*reply == 'V')
11485 parse_tracepoint_status (reply + 1, bp, utp);
11486 }
11487 }
11488}
11489
35b1e5cc 11490static void
74499f1b 11491remote_trace_stop (struct target_ops *self)
35b1e5cc
SS
11492{
11493 putpkt ("QTStop");
11494 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11495 if (*target_buf == '\0')
11496 error (_("Target does not support this command."));
11497 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11498 error (_("Bogus reply from target: %s"), target_buf);
11499}
11500
11501static int
bd4c6793
TT
11502remote_trace_find (struct target_ops *self,
11503 enum trace_find_type type, int num,
cc5925ad 11504 CORE_ADDR addr1, CORE_ADDR addr2,
35b1e5cc
SS
11505 int *tpp)
11506{
11507 struct remote_state *rs = get_remote_state ();
bba74b36 11508 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
11509 char *p, *reply;
11510 int target_frameno = -1, target_tracept = -1;
11511
e6e4e701
PA
11512 /* Lookups other than by absolute frame number depend on the current
11513 trace selected, so make sure it is correct on the remote end
11514 first. */
11515 if (type != tfind_number)
11516 set_remote_traceframe ();
11517
35b1e5cc
SS
11518 p = rs->buf;
11519 strcpy (p, "QTFrame:");
11520 p = strchr (p, '\0');
11521 switch (type)
11522 {
11523 case tfind_number:
bba74b36 11524 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
11525 break;
11526 case tfind_pc:
bba74b36 11527 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
11528 break;
11529 case tfind_tp:
bba74b36 11530 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
11531 break;
11532 case tfind_range:
bba74b36
YQ
11533 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
11534 phex_nz (addr2, 0));
35b1e5cc
SS
11535 break;
11536 case tfind_outside:
bba74b36
YQ
11537 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
11538 phex_nz (addr2, 0));
35b1e5cc
SS
11539 break;
11540 default:
9b20d036 11541 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
11542 }
11543
11544 putpkt (rs->buf);
2f65bcb7 11545 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
ad91cd99
PA
11546 if (*reply == '\0')
11547 error (_("Target does not support this command."));
35b1e5cc
SS
11548
11549 while (reply && *reply)
11550 switch (*reply)
11551 {
11552 case 'F':
f197e0f1
VP
11553 p = ++reply;
11554 target_frameno = (int) strtol (p, &reply, 16);
11555 if (reply == p)
11556 error (_("Unable to parse trace frame number"));
e6e4e701
PA
11557 /* Don't update our remote traceframe number cache on failure
11558 to select a remote traceframe. */
f197e0f1
VP
11559 if (target_frameno == -1)
11560 return -1;
35b1e5cc
SS
11561 break;
11562 case 'T':
f197e0f1
VP
11563 p = ++reply;
11564 target_tracept = (int) strtol (p, &reply, 16);
11565 if (reply == p)
11566 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
11567 break;
11568 case 'O': /* "OK"? */
11569 if (reply[1] == 'K' && reply[2] == '\0')
11570 reply += 2;
11571 else
11572 error (_("Bogus reply from target: %s"), reply);
11573 break;
11574 default:
11575 error (_("Bogus reply from target: %s"), reply);
11576 }
11577 if (tpp)
11578 *tpp = target_tracept;
e6e4e701 11579
262e1174 11580 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
11581 return target_frameno;
11582}
11583
11584static int
4011015b
TT
11585remote_get_trace_state_variable_value (struct target_ops *self,
11586 int tsvnum, LONGEST *val)
35b1e5cc
SS
11587{
11588 struct remote_state *rs = get_remote_state ();
11589 char *reply;
11590 ULONGEST uval;
11591
e6e4e701
PA
11592 set_remote_traceframe ();
11593
bba74b36 11594 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc
SS
11595 putpkt (rs->buf);
11596 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11597 if (reply && *reply)
11598 {
11599 if (*reply == 'V')
11600 {
11601 unpack_varlen_hex (reply + 1, &uval);
11602 *val = (LONGEST) uval;
11603 return 1;
11604 }
11605 }
11606 return 0;
11607}
11608
00bf0b85 11609static int
dc3decaf 11610remote_save_trace_data (struct target_ops *self, const char *filename)
00bf0b85
SS
11611{
11612 struct remote_state *rs = get_remote_state ();
11613 char *p, *reply;
11614
11615 p = rs->buf;
11616 strcpy (p, "QTSave:");
11617 p += strlen (p);
11618 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
11619 error (_("Remote file name too long for trace save packet"));
9f1b45b0 11620 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
11621 *p++ = '\0';
11622 putpkt (rs->buf);
ad91cd99 11623 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
d6c5869f 11624 if (*reply == '\0')
ad91cd99
PA
11625 error (_("Target does not support this command."));
11626 if (strcmp (reply, "OK") != 0)
11627 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
11628 return 0;
11629}
11630
11631/* This is basically a memory transfer, but needs to be its own packet
11632 because we don't know how the target actually organizes its trace
11633 memory, plus we want to be able to ask for as much as possible, but
11634 not be unhappy if we don't get as much as we ask for. */
11635
11636static LONGEST
88ee6f45
TT
11637remote_get_raw_trace_data (struct target_ops *self,
11638 gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
11639{
11640 struct remote_state *rs = get_remote_state ();
11641 char *reply;
11642 char *p;
11643 int rslt;
11644
11645 p = rs->buf;
11646 strcpy (p, "qTBuffer:");
11647 p += strlen (p);
11648 p += hexnumstr (p, offset);
11649 *p++ = ',';
11650 p += hexnumstr (p, len);
11651 *p++ = '\0';
11652
11653 putpkt (rs->buf);
11654 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11655 if (reply && *reply)
11656 {
11657 /* 'l' by itself means we're at the end of the buffer and
11658 there is nothing more to get. */
11659 if (*reply == 'l')
11660 return 0;
11661
11662 /* Convert the reply into binary. Limit the number of bytes to
11663 convert according to our passed-in buffer size, rather than
11664 what was returned in the packet; if the target is
11665 unexpectedly generous and gives us a bigger reply than we
11666 asked for, we don't want to crash. */
11667 rslt = hex2bin (target_buf, buf, len);
11668 return rslt;
11669 }
11670
11671 /* Something went wrong, flag as an error. */
11672 return -1;
11673}
11674
35b1e5cc 11675static void
37b25738 11676remote_set_disconnected_tracing (struct target_ops *self, int val)
35b1e5cc
SS
11677{
11678 struct remote_state *rs = get_remote_state ();
11679
4082afcc 11680 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 11681 {
ad91cd99
PA
11682 char *reply;
11683
bba74b36 11684 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 11685 putpkt (rs->buf);
ad91cd99
PA
11686 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11687 if (*reply == '\0')
33da3f1c 11688 error (_("Target does not support this command."));
ad91cd99
PA
11689 if (strcmp (reply, "OK") != 0)
11690 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
11691 }
11692 else if (val)
11693 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
11694}
11695
dc146f7c
VP
11696static int
11697remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
11698{
11699 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 11700
fe978cb0
PA
11701 if (info && info->priv)
11702 return info->priv->core;
dc146f7c
VP
11703 return -1;
11704}
11705
4daf5ac0 11706static void
736d5b1f 11707remote_set_circular_trace_buffer (struct target_ops *self, int val)
4daf5ac0
SS
11708{
11709 struct remote_state *rs = get_remote_state ();
ad91cd99 11710 char *reply;
4daf5ac0 11711
bba74b36 11712 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 11713 putpkt (rs->buf);
ad91cd99
PA
11714 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11715 if (*reply == '\0')
4daf5ac0 11716 error (_("Target does not support this command."));
ad91cd99
PA
11717 if (strcmp (reply, "OK") != 0)
11718 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
11719}
11720
b3b9301e 11721static struct traceframe_info *
a893e81f 11722remote_traceframe_info (struct target_ops *self)
b3b9301e
PA
11723{
11724 char *text;
11725
11726 text = target_read_stralloc (&current_target,
11727 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11728 if (text != NULL)
11729 {
11730 struct traceframe_info *info;
11731 struct cleanup *back_to = make_cleanup (xfree, text);
11732
11733 info = parse_traceframe_info (text);
11734 do_cleanups (back_to);
11735 return info;
11736 }
11737
11738 return NULL;
11739}
11740
405f8e94
SS
11741/* Handle the qTMinFTPILen packet. Returns the minimum length of
11742 instruction on which a fast tracepoint may be placed. Returns -1
11743 if the packet is not supported, and 0 if the minimum instruction
11744 length is unknown. */
11745
11746static int
0e67620a 11747remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
405f8e94
SS
11748{
11749 struct remote_state *rs = get_remote_state ();
11750 char *reply;
11751
e886a173
PA
11752 /* If we're not debugging a process yet, the IPA can't be
11753 loaded. */
11754 if (!target_has_execution)
11755 return 0;
11756
11757 /* Make sure the remote is pointing at the right process. */
11758 set_general_process ();
11759
bba74b36 11760 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94
SS
11761 putpkt (rs->buf);
11762 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11763 if (*reply == '\0')
11764 return -1;
11765 else
11766 {
11767 ULONGEST min_insn_len;
11768
11769 unpack_varlen_hex (reply, &min_insn_len);
11770
11771 return (int) min_insn_len;
11772 }
11773}
11774
f6f899bf 11775static void
4da384be 11776remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
f6f899bf 11777{
4082afcc 11778 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
11779 {
11780 struct remote_state *rs = get_remote_state ();
11781 char *buf = rs->buf;
11782 char *endbuf = rs->buf + get_remote_packet_size ();
11783 enum packet_result result;
11784
11785 gdb_assert (val >= 0 || val == -1);
11786 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11787 /* Send -1 as literal "-1" to avoid host size dependency. */
11788 if (val < 0)
11789 {
11790 *buf++ = '-';
11791 buf += hexnumstr (buf, (ULONGEST) -val);
11792 }
11793 else
11794 buf += hexnumstr (buf, (ULONGEST) val);
11795
11796 putpkt (rs->buf);
11797 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11798 result = packet_ok (rs->buf,
11799 &remote_protocol_packets[PACKET_QTBuffer_size]);
11800
11801 if (result != PACKET_OK)
11802 warning (_("Bogus reply from target: %s"), rs->buf);
11803 }
11804}
11805
f196051f 11806static int
d9e68a2c
TT
11807remote_set_trace_notes (struct target_ops *self,
11808 const char *user, const char *notes,
ca623f82 11809 const char *stop_notes)
f196051f
SS
11810{
11811 struct remote_state *rs = get_remote_state ();
11812 char *reply;
11813 char *buf = rs->buf;
11814 char *endbuf = rs->buf + get_remote_packet_size ();
11815 int nbytes;
11816
11817 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11818 if (user)
11819 {
11820 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 11821 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
11822 buf += 2 * nbytes;
11823 *buf++ = ';';
11824 }
11825 if (notes)
11826 {
11827 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 11828 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
11829 buf += 2 * nbytes;
11830 *buf++ = ';';
11831 }
11832 if (stop_notes)
11833 {
11834 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 11835 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
11836 buf += 2 * nbytes;
11837 *buf++ = ';';
11838 }
11839 /* Ensure the buffer is terminated. */
11840 *buf = '\0';
11841
11842 putpkt (rs->buf);
11843 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11844 if (*reply == '\0')
11845 return 0;
11846
11847 if (strcmp (reply, "OK") != 0)
11848 error (_("Bogus reply from target: %s"), reply);
11849
11850 return 1;
11851}
11852
d1feda86 11853static int
2c152180 11854remote_use_agent (struct target_ops *self, int use)
d1feda86 11855{
4082afcc 11856 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
11857 {
11858 struct remote_state *rs = get_remote_state ();
11859
11860 /* If the stub supports QAgent. */
bba74b36 11861 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
11862 putpkt (rs->buf);
11863 getpkt (&rs->buf, &rs->buf_size, 0);
11864
11865 if (strcmp (rs->buf, "OK") == 0)
11866 {
11867 use_agent = use;
11868 return 1;
11869 }
11870 }
11871
11872 return 0;
11873}
11874
11875static int
fe38f897 11876remote_can_use_agent (struct target_ops *self)
d1feda86 11877{
4082afcc 11878 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
11879}
11880
9accd112
MM
11881struct btrace_target_info
11882{
11883 /* The ptid of the traced thread. */
11884 ptid_t ptid;
f4abbc16
MM
11885
11886 /* The obtained branch trace configuration. */
11887 struct btrace_config conf;
9accd112
MM
11888};
11889
f4abbc16
MM
11890/* Reset our idea of our target's btrace configuration. */
11891
11892static void
11893remote_btrace_reset (void)
11894{
11895 struct remote_state *rs = get_remote_state ();
11896
11897 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
11898}
11899
9accd112
MM
11900/* Check whether the target supports branch tracing. */
11901
11902static int
043c3577 11903remote_supports_btrace (struct target_ops *self, enum btrace_format format)
9accd112 11904{
4082afcc 11905 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
9accd112 11906 return 0;
4082afcc 11907 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
9accd112
MM
11908 return 0;
11909
043c3577
MM
11910 switch (format)
11911 {
11912 case BTRACE_FORMAT_NONE:
11913 return 0;
11914
11915 case BTRACE_FORMAT_BTS:
11916 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
b20a6524
MM
11917
11918 case BTRACE_FORMAT_PT:
11919 /* The trace is decoded on the host. Even if our target supports it,
11920 we still need to have libipt to decode the trace. */
11921#if defined (HAVE_LIBIPT)
11922 return (packet_support (PACKET_Qbtrace_pt) == PACKET_ENABLE);
11923#else /* !defined (HAVE_LIBIPT) */
11924 return 0;
11925#endif /* !defined (HAVE_LIBIPT) */
043c3577
MM
11926 }
11927
11928 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
9accd112
MM
11929}
11930
f4abbc16
MM
11931/* Synchronize the configuration with the target. */
11932
11933static void
11934btrace_sync_conf (const struct btrace_config *conf)
11935{
d33501a5
MM
11936 struct packet_config *packet;
11937 struct remote_state *rs;
11938 char *buf, *pos, *endbuf;
11939
11940 rs = get_remote_state ();
11941 buf = rs->buf;
11942 endbuf = buf + get_remote_packet_size ();
11943
11944 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
11945 if (packet_config_support (packet) == PACKET_ENABLE
11946 && conf->bts.size != rs->btrace_config.bts.size)
11947 {
11948 pos = buf;
11949 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
11950 conf->bts.size);
11951
11952 putpkt (buf);
11953 getpkt (&buf, &rs->buf_size, 0);
11954
11955 if (packet_ok (buf, packet) == PACKET_ERROR)
11956 {
11957 if (buf[0] == 'E' && buf[1] == '.')
11958 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
11959 else
11960 error (_("Failed to configure the BTS buffer size."));
11961 }
11962
11963 rs->btrace_config.bts.size = conf->bts.size;
11964 }
b20a6524
MM
11965
11966 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_pt_size];
11967 if (packet_config_support (packet) == PACKET_ENABLE
11968 && conf->pt.size != rs->btrace_config.pt.size)
11969 {
11970 pos = buf;
11971 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
11972 conf->pt.size);
11973
11974 putpkt (buf);
11975 getpkt (&buf, &rs->buf_size, 0);
11976
11977 if (packet_ok (buf, packet) == PACKET_ERROR)
11978 {
11979 if (buf[0] == 'E' && buf[1] == '.')
11980 error (_("Failed to configure the trace buffer size: %s"), buf + 2);
11981 else
11982 error (_("Failed to configure the trace buffer size."));
11983 }
11984
11985 rs->btrace_config.pt.size = conf->pt.size;
11986 }
f4abbc16
MM
11987}
11988
11989/* Read the current thread's btrace configuration from the target and
11990 store it into CONF. */
11991
11992static void
11993btrace_read_config (struct btrace_config *conf)
11994{
11995 char *xml;
11996
11997 xml = target_read_stralloc (&current_target,
b20a6524 11998 TARGET_OBJECT_BTRACE_CONF, "");
f4abbc16
MM
11999 if (xml != NULL)
12000 {
12001 struct cleanup *cleanup;
12002
12003 cleanup = make_cleanup (xfree, xml);
12004 parse_xml_btrace_conf (conf, xml);
12005 do_cleanups (cleanup);
12006 }
12007}
12008
9accd112
MM
12009/* Enable branch tracing. */
12010
12011static struct btrace_target_info *
f4abbc16
MM
12012remote_enable_btrace (struct target_ops *self, ptid_t ptid,
12013 const struct btrace_config *conf)
9accd112
MM
12014{
12015 struct btrace_target_info *tinfo = NULL;
b20a6524 12016 struct packet_config *packet = NULL;
9accd112
MM
12017 struct remote_state *rs = get_remote_state ();
12018 char *buf = rs->buf;
12019 char *endbuf = rs->buf + get_remote_packet_size ();
12020
b20a6524
MM
12021 switch (conf->format)
12022 {
12023 case BTRACE_FORMAT_BTS:
12024 packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
12025 break;
12026
12027 case BTRACE_FORMAT_PT:
12028 packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
12029 break;
12030 }
12031
12032 if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12033 error (_("Target does not support branch tracing."));
12034
f4abbc16
MM
12035 btrace_sync_conf (conf);
12036
9accd112
MM
12037 set_general_thread (ptid);
12038
12039 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12040 putpkt (rs->buf);
12041 getpkt (&rs->buf, &rs->buf_size, 0);
12042
12043 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12044 {
12045 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12046 error (_("Could not enable branch tracing for %s: %s"),
12047 target_pid_to_str (ptid), rs->buf + 2);
12048 else
12049 error (_("Could not enable branch tracing for %s."),
12050 target_pid_to_str (ptid));
12051 }
12052
12053 tinfo = xzalloc (sizeof (*tinfo));
12054 tinfo->ptid = ptid;
12055
f4abbc16
MM
12056 /* If we fail to read the configuration, we lose some information, but the
12057 tracing itself is not impacted. */
492d29ea
PA
12058 TRY
12059 {
12060 btrace_read_config (&tinfo->conf);
12061 }
12062 CATCH (err, RETURN_MASK_ERROR)
12063 {
12064 if (err.message != NULL)
12065 warning ("%s", err.message);
12066 }
12067 END_CATCH
f4abbc16 12068
9accd112
MM
12069 return tinfo;
12070}
12071
12072/* Disable branch tracing. */
12073
12074static void
25e95349
TT
12075remote_disable_btrace (struct target_ops *self,
12076 struct btrace_target_info *tinfo)
9accd112
MM
12077{
12078 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
12079 struct remote_state *rs = get_remote_state ();
12080 char *buf = rs->buf;
12081 char *endbuf = rs->buf + get_remote_packet_size ();
12082
4082afcc 12083 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12084 error (_("Target does not support branch tracing."));
12085
12086 set_general_thread (tinfo->ptid);
12087
12088 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
12089 putpkt (rs->buf);
12090 getpkt (&rs->buf, &rs->buf_size, 0);
12091
12092 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
12093 {
12094 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
12095 error (_("Could not disable branch tracing for %s: %s"),
12096 target_pid_to_str (tinfo->ptid), rs->buf + 2);
12097 else
12098 error (_("Could not disable branch tracing for %s."),
12099 target_pid_to_str (tinfo->ptid));
12100 }
12101
12102 xfree (tinfo);
12103}
12104
12105/* Teardown branch tracing. */
12106
12107static void
1777056d
TT
12108remote_teardown_btrace (struct target_ops *self,
12109 struct btrace_target_info *tinfo)
9accd112
MM
12110{
12111 /* We must not talk to the target during teardown. */
12112 xfree (tinfo);
12113}
12114
12115/* Read the branch trace. */
12116
969c39fb 12117static enum btrace_error
39c49f83 12118remote_read_btrace (struct target_ops *self,
734b0e4b 12119 struct btrace_data *btrace,
969c39fb 12120 struct btrace_target_info *tinfo,
9accd112
MM
12121 enum btrace_read_type type)
12122{
12123 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
12124 struct remote_state *rs = get_remote_state ();
969c39fb 12125 struct cleanup *cleanup;
9accd112
MM
12126 const char *annex;
12127 char *xml;
12128
4082afcc 12129 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
12130 error (_("Target does not support branch tracing."));
12131
12132#if !defined(HAVE_LIBEXPAT)
12133 error (_("Cannot process branch tracing result. XML parsing not supported."));
12134#endif
12135
12136 switch (type)
12137 {
864089d2 12138 case BTRACE_READ_ALL:
9accd112
MM
12139 annex = "all";
12140 break;
864089d2 12141 case BTRACE_READ_NEW:
9accd112
MM
12142 annex = "new";
12143 break;
969c39fb
MM
12144 case BTRACE_READ_DELTA:
12145 annex = "delta";
12146 break;
9accd112
MM
12147 default:
12148 internal_error (__FILE__, __LINE__,
12149 _("Bad branch tracing read type: %u."),
12150 (unsigned int) type);
12151 }
12152
12153 xml = target_read_stralloc (&current_target,
b20a6524 12154 TARGET_OBJECT_BTRACE, annex);
969c39fb
MM
12155 if (xml == NULL)
12156 return BTRACE_ERR_UNKNOWN;
9accd112 12157
969c39fb 12158 cleanup = make_cleanup (xfree, xml);
734b0e4b 12159 parse_xml_btrace (btrace, xml);
969c39fb 12160 do_cleanups (cleanup);
9accd112 12161
969c39fb 12162 return BTRACE_ERR_NONE;
9accd112
MM
12163}
12164
f4abbc16
MM
12165static const struct btrace_config *
12166remote_btrace_conf (struct target_ops *self,
12167 const struct btrace_target_info *tinfo)
12168{
12169 return &tinfo->conf;
12170}
12171
ced63ec0 12172static int
5436ff03 12173remote_augmented_libraries_svr4_read (struct target_ops *self)
ced63ec0 12174{
4082afcc
PA
12175 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
12176 == PACKET_ENABLE);
ced63ec0
GB
12177}
12178
9dd130a0
TT
12179/* Implementation of to_load. */
12180
12181static void
9cbe5fff 12182remote_load (struct target_ops *self, const char *name, int from_tty)
9dd130a0
TT
12183{
12184 generic_load (name, from_tty);
12185}
12186
c78fa86a
GB
12187/* Accepts an integer PID; returns a string representing a file that
12188 can be opened on the remote side to get the symbols for the child
12189 process. Returns NULL if the operation is not supported. */
12190
12191static char *
12192remote_pid_to_exec_file (struct target_ops *self, int pid)
12193{
12194 static char *filename = NULL;
835205d0
GB
12195 struct inferior *inf;
12196 char *annex = NULL;
c78fa86a
GB
12197
12198 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
12199 return NULL;
12200
12201 if (filename != NULL)
12202 xfree (filename);
12203
835205d0
GB
12204 inf = find_inferior_pid (pid);
12205 if (inf == NULL)
12206 internal_error (__FILE__, __LINE__,
12207 _("not currently attached to process %d"), pid);
12208
12209 if (!inf->fake_pid_p)
12210 {
12211 const int annex_size = 9;
12212
12213 annex = alloca (annex_size);
12214 xsnprintf (annex, annex_size, "%x", pid);
12215 }
12216
c78fa86a
GB
12217 filename = target_read_stralloc (&current_target,
12218 TARGET_OBJECT_EXEC_FILE, annex);
12219
12220 return filename;
12221}
12222
c906108c 12223static void
fba45db2 12224init_remote_ops (void)
c906108c 12225{
c5aa993b 12226 remote_ops.to_shortname = "remote";
c906108c 12227 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 12228 remote_ops.to_doc =
c906108c 12229 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
12230Specify the serial device it is connected to\n\
12231(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
12232 remote_ops.to_open = remote_open;
12233 remote_ops.to_close = remote_close;
c906108c 12234 remote_ops.to_detach = remote_detach;
6ad8ae5c 12235 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 12236 remote_ops.to_resume = remote_resume;
c906108c
SS
12237 remote_ops.to_wait = remote_wait;
12238 remote_ops.to_fetch_registers = remote_fetch_registers;
12239 remote_ops.to_store_registers = remote_store_registers;
12240 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b 12241 remote_ops.to_files_info = remote_files_info;
c906108c
SS
12242 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
12243 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
f7e6eed5
PA
12244 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
12245 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
12246 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
12247 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
3c3bea1c
GS
12248 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
12249 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
12250 remote_ops.to_watchpoint_addr_within_range =
12251 remote_watchpoint_addr_within_range;
3c3bea1c
GS
12252 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
12253 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
12254 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
12255 remote_ops.to_region_ok_for_hw_watchpoint
12256 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
12257 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
12258 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b 12259 remote_ops.to_kill = remote_kill;
9dd130a0 12260 remote_ops.to_load = remote_load;
c906108c 12261 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 12262 remote_ops.to_pass_signals = remote_pass_signals;
9b224c5e 12263 remote_ops.to_program_signals = remote_program_signals;
c906108c 12264 remote_ops.to_thread_alive = remote_thread_alive;
e8032dde 12265 remote_ops.to_update_thread_list = remote_update_thread_list;
0caabb7e 12266 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 12267 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 12268 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 12269 remote_ops.to_stop = remote_stop;
4b8a223f 12270 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 12271 remote_ops.to_rcmd = remote_rcmd;
c78fa86a 12272 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
49d03eab 12273 remote_ops.to_log_command = serial_log_command;
38691318 12274 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 12275 remote_ops.to_stratum = process_stratum;
c35b1492
PA
12276 remote_ops.to_has_all_memory = default_child_has_all_memory;
12277 remote_ops.to_has_memory = default_child_has_memory;
12278 remote_ops.to_has_stack = default_child_has_stack;
12279 remote_ops.to_has_registers = default_child_has_registers;
12280 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 12281 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 12282 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 12283 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 12284 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
12285 remote_ops.to_flash_erase = remote_flash_erase;
12286 remote_ops.to_flash_done = remote_flash_done;
29709017 12287 remote_ops.to_read_description = remote_read_description;
08388c79 12288 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
12289 remote_ops.to_can_async_p = remote_can_async_p;
12290 remote_ops.to_is_async_p = remote_is_async_p;
12291 remote_ops.to_async = remote_async;
75c99385
PA
12292 remote_ops.to_terminal_inferior = remote_terminal_inferior;
12293 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 12294 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 12295 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
12296 remote_ops.to_supports_disable_randomization
12297 = remote_supports_disable_randomization;
4bd7dc42 12298 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
7313baad
UW
12299 remote_ops.to_fileio_open = remote_hostio_open;
12300 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
12301 remote_ops.to_fileio_pread = remote_hostio_pread;
9b15c1f0 12302 remote_ops.to_fileio_fstat = remote_hostio_fstat;
7313baad
UW
12303 remote_ops.to_fileio_close = remote_hostio_close;
12304 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 12305 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 12306 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 12307 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 12308 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
d3ce09f5 12309 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
35b1e5cc
SS
12310 remote_ops.to_trace_init = remote_trace_init;
12311 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 12312 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
12313 remote_ops.to_download_trace_state_variable
12314 = remote_download_trace_state_variable;
d248b706
KY
12315 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
12316 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
12317 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
12318 remote_ops.to_trace_start = remote_trace_start;
12319 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 12320 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
12321 remote_ops.to_trace_stop = remote_trace_stop;
12322 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
12323 remote_ops.to_get_trace_state_variable_value
12324 = remote_get_trace_state_variable_value;
00bf0b85
SS
12325 remote_ops.to_save_trace_data = remote_save_trace_data;
12326 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
12327 remote_ops.to_upload_trace_state_variables
12328 = remote_upload_trace_state_variables;
00bf0b85 12329 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 12330 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 12331 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 12332 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f6f899bf 12333 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
f196051f 12334 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 12335 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 12336 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 12337 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 12338 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
12339 remote_ops.to_static_tracepoint_marker_at
12340 = remote_static_tracepoint_marker_at;
12341 remote_ops.to_static_tracepoint_markers_by_strid
12342 = remote_static_tracepoint_markers_by_strid;
b3b9301e 12343 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
12344 remote_ops.to_use_agent = remote_use_agent;
12345 remote_ops.to_can_use_agent = remote_can_use_agent;
9accd112
MM
12346 remote_ops.to_supports_btrace = remote_supports_btrace;
12347 remote_ops.to_enable_btrace = remote_enable_btrace;
12348 remote_ops.to_disable_btrace = remote_disable_btrace;
12349 remote_ops.to_teardown_btrace = remote_teardown_btrace;
12350 remote_ops.to_read_btrace = remote_read_btrace;
f4abbc16 12351 remote_ops.to_btrace_conf = remote_btrace_conf;
ced63ec0
GB
12352 remote_ops.to_augmented_libraries_svr4_read =
12353 remote_augmented_libraries_svr4_read;
c906108c
SS
12354}
12355
12356/* Set up the extended remote vector by making a copy of the standard
12357 remote vector and adding to it. */
12358
12359static void
fba45db2 12360init_extended_remote_ops (void)
c906108c
SS
12361{
12362 extended_remote_ops = remote_ops;
12363
0f71a2f6 12364 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 12365 extended_remote_ops.to_longname =
c906108c 12366 "Extended remote serial target in gdb-specific protocol";
c5aa993b 12367 extended_remote_ops.to_doc =
c906108c 12368 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
12369Specify the serial device it is connected to (e.g. /dev/ttya).";
12370 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
12371 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
12372 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2d717e4f
DJ
12373 extended_remote_ops.to_detach = extended_remote_detach;
12374 extended_remote_ops.to_attach = extended_remote_attach;
b9c1d481 12375 extended_remote_ops.to_post_attach = extended_remote_post_attach;
82f73884 12376 extended_remote_ops.to_kill = extended_remote_kill;
03583c20
UW
12377 extended_remote_ops.to_supports_disable_randomization
12378 = extended_remote_supports_disable_randomization;
de0d863e 12379 extended_remote_ops.to_follow_fork = remote_follow_fork;
cbb8991c
DB
12380 extended_remote_ops.to_insert_fork_catchpoint
12381 = remote_insert_fork_catchpoint;
12382 extended_remote_ops.to_remove_fork_catchpoint
12383 = remote_remove_fork_catchpoint;
12384 extended_remote_ops.to_insert_vfork_catchpoint
12385 = remote_insert_vfork_catchpoint;
12386 extended_remote_ops.to_remove_vfork_catchpoint
12387 = remote_remove_vfork_catchpoint;
0f71a2f6
JM
12388}
12389
6426a772 12390static int
6a109b6b 12391remote_can_async_p (struct target_ops *ops)
6426a772 12392{
5d93a237
TT
12393 struct remote_state *rs = get_remote_state ();
12394
c6ebd6cf 12395 if (!target_async_permitted)
75c99385
PA
12396 /* We only enable async when the user specifically asks for it. */
12397 return 0;
12398
23860348 12399 /* We're async whenever the serial device is. */
5d93a237 12400 return serial_can_async_p (rs->remote_desc);
6426a772
JM
12401}
12402
12403static int
6a109b6b 12404remote_is_async_p (struct target_ops *ops)
6426a772 12405{
5d93a237
TT
12406 struct remote_state *rs = get_remote_state ();
12407
c6ebd6cf 12408 if (!target_async_permitted)
75c99385
PA
12409 /* We only enable async when the user specifically asks for it. */
12410 return 0;
12411
23860348 12412 /* We're async whenever the serial device is. */
5d93a237 12413 return serial_is_async_p (rs->remote_desc);
6426a772
JM
12414}
12415
2acceee2
JM
12416/* Pass the SERIAL event on and up to the client. One day this code
12417 will be able to delay notifying the client of an event until the
23860348 12418 point where an entire packet has been received. */
2acceee2 12419
2acceee2
JM
12420static serial_event_ftype remote_async_serial_handler;
12421
6426a772 12422static void
819cc324 12423remote_async_serial_handler (struct serial *scb, void *context)
6426a772 12424{
88b496c3
TT
12425 struct remote_state *rs = context;
12426
2acceee2
JM
12427 /* Don't propogate error information up to the client. Instead let
12428 the client find out about the error by querying the target. */
6a3753b3 12429 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
12430}
12431
74531fed
PA
12432static void
12433remote_async_inferior_event_handler (gdb_client_data data)
12434{
12435 inferior_event_handler (INF_REG_EVENT, NULL);
12436}
12437
2acceee2 12438static void
6a3753b3 12439remote_async (struct target_ops *ops, int enable)
2acceee2 12440{
5d93a237
TT
12441 struct remote_state *rs = get_remote_state ();
12442
6a3753b3 12443 if (enable)
2acceee2 12444 {
88b496c3 12445 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
12446
12447 /* If there are pending events in the stop reply queue tell the
12448 event loop to process them. */
12449 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
12450 mark_async_event_handler (remote_async_inferior_event_token);
2acceee2
JM
12451 }
12452 else
b7d2e916
PA
12453 {
12454 serial_async (rs->remote_desc, NULL, NULL);
12455 clear_async_event_handler (remote_async_inferior_event_token);
12456 }
6426a772
JM
12457}
12458
5a2468f5 12459static void
c2d11a7d 12460set_remote_cmd (char *args, int from_tty)
5a2468f5 12461{
635c7e8a 12462 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
12463}
12464
d471ea57
AC
12465static void
12466show_remote_cmd (char *args, int from_tty)
12467{
37a105a1 12468 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 12469 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1
DJ
12470 struct cleanup *showlist_chain;
12471 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 12472 struct ui_out *uiout = current_uiout;
37a105a1
DJ
12473
12474 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
12475 for (; list != NULL; list = list->next)
12476 if (strcmp (list->name, "Z-packet") == 0)
12477 continue;
427c3a89
DJ
12478 else if (list->type == not_set_cmd)
12479 /* Alias commands are exactly like the original, except they
12480 don't have the normal type. */
12481 continue;
12482 else
37a105a1
DJ
12483 {
12484 struct cleanup *option_chain
12485 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
a744cf53 12486
37a105a1
DJ
12487 ui_out_field_string (uiout, "name", list->name);
12488 ui_out_text (uiout, ": ");
427c3a89 12489 if (list->type == show_cmd)
5b9afe8a 12490 do_show_command ((char *) NULL, from_tty, list);
427c3a89
DJ
12491 else
12492 cmd_func (list, NULL, from_tty);
37a105a1
DJ
12493 /* Close the tuple. */
12494 do_cleanups (option_chain);
12495 }
427c3a89
DJ
12496
12497 /* Close the tuple. */
12498 do_cleanups (showlist_chain);
d471ea57 12499}
5a2468f5 12500
0f71a2f6 12501
23860348 12502/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
12503static void
12504remote_new_objfile (struct objfile *objfile)
12505{
5d93a237
TT
12506 struct remote_state *rs = get_remote_state ();
12507
12508 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 12509 remote_check_symbols ();
dc8acb97
MS
12510}
12511
00bf0b85
SS
12512/* Pull all the tracepoints defined on the target and create local
12513 data structures representing them. We don't want to create real
12514 tracepoints yet, we don't want to mess up the user's existing
12515 collection. */
12516
12517static int
ab6617cc 12518remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
d5551862 12519{
00bf0b85
SS
12520 struct remote_state *rs = get_remote_state ();
12521 char *p;
d5551862 12522
00bf0b85
SS
12523 /* Ask for a first packet of tracepoint definition. */
12524 putpkt ("qTfP");
12525 getpkt (&rs->buf, &rs->buf_size, 0);
12526 p = rs->buf;
12527 while (*p && *p != 'l')
d5551862 12528 {
00bf0b85
SS
12529 parse_tracepoint_definition (p, utpp);
12530 /* Ask for another packet of tracepoint definition. */
12531 putpkt ("qTsP");
12532 getpkt (&rs->buf, &rs->buf_size, 0);
12533 p = rs->buf;
d5551862 12534 }
00bf0b85 12535 return 0;
d5551862
SS
12536}
12537
00bf0b85 12538static int
181e3713
TT
12539remote_upload_trace_state_variables (struct target_ops *self,
12540 struct uploaded_tsv **utsvp)
d5551862 12541{
00bf0b85 12542 struct remote_state *rs = get_remote_state ();
d5551862 12543 char *p;
d5551862 12544
00bf0b85
SS
12545 /* Ask for a first packet of variable definition. */
12546 putpkt ("qTfV");
d5551862
SS
12547 getpkt (&rs->buf, &rs->buf_size, 0);
12548 p = rs->buf;
00bf0b85 12549 while (*p && *p != 'l')
d5551862 12550 {
00bf0b85
SS
12551 parse_tsv_definition (p, utsvp);
12552 /* Ask for another packet of variable definition. */
12553 putpkt ("qTsV");
d5551862
SS
12554 getpkt (&rs->buf, &rs->buf_size, 0);
12555 p = rs->buf;
12556 }
00bf0b85 12557 return 0;
d5551862
SS
12558}
12559
c1e36e3e
PA
12560/* The "set/show range-stepping" show hook. */
12561
12562static void
12563show_range_stepping (struct ui_file *file, int from_tty,
12564 struct cmd_list_element *c,
12565 const char *value)
12566{
12567 fprintf_filtered (file,
12568 _("Debugger's willingness to use range stepping "
12569 "is %s.\n"), value);
12570}
12571
12572/* The "set/show range-stepping" set hook. */
12573
12574static void
12575set_range_stepping (char *ignore_args, int from_tty,
12576 struct cmd_list_element *c)
12577{
5d93a237
TT
12578 struct remote_state *rs = get_remote_state ();
12579
c1e36e3e
PA
12580 /* Whene enabling, check whether range stepping is actually
12581 supported by the target, and warn if not. */
12582 if (use_range_stepping)
12583 {
5d93a237 12584 if (rs->remote_desc != NULL)
c1e36e3e 12585 {
4082afcc 12586 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
12587 remote_vcont_probe (rs);
12588
4082afcc 12589 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
12590 && rs->supports_vCont.r)
12591 return;
12592 }
12593
12594 warning (_("Range stepping is not supported by the current target"));
12595 }
12596}
12597
c906108c 12598void
fba45db2 12599_initialize_remote (void)
c906108c 12600{
ea9c271d 12601 struct remote_state *rs;
9a7071a8 12602 struct cmd_list_element *cmd;
6f937416 12603 const char *cmd_name;
ea9c271d 12604
0f71a2f6 12605 /* architecture specific data */
2bc416ba 12606 remote_gdbarch_data_handle =
23860348 12607 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
12608 remote_g_packet_data_handle =
12609 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 12610
ea9c271d
DJ
12611 /* Initialize the per-target state. At the moment there is only one
12612 of these, not one per target. Only one target is active at a
cf792862
TT
12613 time. */
12614 remote_state = new_remote_state ();
ea9c271d 12615
c906108c
SS
12616 init_remote_ops ();
12617 add_target (&remote_ops);
12618
12619 init_extended_remote_ops ();
12620 add_target (&extended_remote_ops);
cce74817 12621
dc8acb97 12622 /* Hook into new objfile notification. */
06d3b283 12623 observer_attach_new_objfile (remote_new_objfile);
5f4cf0bb
YQ
12624 /* We're no longer interested in notification events of an inferior
12625 when it exits. */
12626 observer_attach_inferior_exit (discard_pending_stop_replies);
dc8acb97 12627
b803fb0f 12628 /* Set up signal handlers. */
934b9bac 12629 async_sigint_remote_token =
b803fb0f 12630 create_async_signal_handler (async_remote_interrupt, NULL);
934b9bac 12631 async_sigint_remote_twice_token =
6d549500 12632 create_async_signal_handler (async_remote_interrupt_twice, NULL);
b803fb0f 12633
c906108c
SS
12634#if 0
12635 init_remote_threadtests ();
12636#endif
12637
722247f1 12638 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 12639 /* set/show remote ... */
d471ea57 12640
1bedd215 12641 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
12642Remote protocol specific variables\n\
12643Configure various remote-protocol specific variables such as\n\
1bedd215 12644the packets being used"),
cff3e48b 12645 &remote_set_cmdlist, "set remote ",
23860348 12646 0 /* allow-unknown */, &setlist);
1bedd215 12647 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
12648Remote protocol specific variables\n\
12649Configure various remote-protocol specific variables such as\n\
1bedd215 12650the packets being used"),
cff3e48b 12651 &remote_show_cmdlist, "show remote ",
23860348 12652 0 /* allow-unknown */, &showlist);
5a2468f5 12653
1a966eab
AC
12654 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
12655Compare section data on target to the exec file.\n\
95cf3b38
DT
12656Argument is a single section name (default: all loaded sections).\n\
12657To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
12658 &cmdlist);
12659
1a966eab
AC
12660 add_cmd ("packet", class_maintenance, packet_command, _("\
12661Send an arbitrary packet to a remote target.\n\
c906108c
SS
12662 maintenance packet TEXT\n\
12663If GDB is talking to an inferior via the GDB serial protocol, then\n\
12664this command sends the string TEXT to the inferior, and displays the\n\
12665response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 12666terminating `#' character and checksum."),
c906108c
SS
12667 &maintenancelist);
12668
7915a72c
AC
12669 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
12670Set whether to send break if interrupted."), _("\
12671Show whether to send break if interrupted."), _("\
12672If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 12673 set_remotebreak, show_remotebreak,
e707bbc2 12674 &setlist, &showlist);
9a7071a8
JB
12675 cmd_name = "remotebreak";
12676 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
12677 deprecate_cmd (cmd, "set remote interrupt-sequence");
12678 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
12679 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
12680 deprecate_cmd (cmd, "show remote interrupt-sequence");
12681
12682 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
12683 interrupt_sequence_modes, &interrupt_sequence_mode,
12684 _("\
9a7071a8
JB
12685Set interrupt sequence to remote target."), _("\
12686Show interrupt sequence to remote target."), _("\
12687Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
12688 NULL, show_interrupt_sequence,
12689 &remote_set_cmdlist,
12690 &remote_show_cmdlist);
12691
12692 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
12693 &interrupt_on_connect, _("\
12694Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12695Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12696If set, interrupt sequence is sent to remote target."),
12697 NULL, NULL,
12698 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 12699
23860348 12700 /* Install commands for configuring memory read/write packets. */
11cf8741 12701
1a966eab
AC
12702 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
12703Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 12704 &setlist);
1a966eab
AC
12705 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
12706Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
12707 &showlist);
12708 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
12709 set_memory_write_packet_size, _("\
12710Set the maximum number of bytes per memory-write packet.\n\
12711Specify the number of bytes in a packet or 0 (zero) for the\n\
12712default packet size. The actual limit is further reduced\n\
12713dependent on the target. Specify ``fixed'' to disable the\n\
12714further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12715 &remote_set_cmdlist);
12716 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
12717 set_memory_read_packet_size, _("\
12718Set the maximum number of bytes per memory-read packet.\n\
12719Specify the number of bytes in a packet or 0 (zero) for the\n\
12720default packet size. The actual limit is further reduced\n\
12721dependent on the target. Specify ``fixed'' to disable the\n\
12722further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12723 &remote_set_cmdlist);
12724 add_cmd ("memory-write-packet-size", no_class,
12725 show_memory_write_packet_size,
1a966eab 12726 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
12727 &remote_show_cmdlist);
12728 add_cmd ("memory-read-packet-size", no_class,
12729 show_memory_read_packet_size,
1a966eab 12730 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 12731 &remote_show_cmdlist);
c906108c 12732
b3f42336 12733 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
12734 &remote_hw_watchpoint_limit, _("\
12735Set the maximum number of target hardware watchpoints."), _("\
12736Show the maximum number of target hardware watchpoints."), _("\
12737Specify a negative limit for unlimited."),
3e43a32a
MS
12738 NULL, NULL, /* FIXME: i18n: The maximum
12739 number of target hardware
12740 watchpoints is %s. */
b3f42336 12741 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
12742 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
12743 &remote_hw_watchpoint_length_limit, _("\
12744Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
12745Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
12746Specify a negative limit for unlimited."),
12747 NULL, NULL, /* FIXME: i18n: The maximum
12748 length (in bytes) of a target
12749 hardware watchpoint is %s. */
12750 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 12751 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
12752 &remote_hw_breakpoint_limit, _("\
12753Set the maximum number of target hardware breakpoints."), _("\
12754Show the maximum number of target hardware breakpoints."), _("\
12755Specify a negative limit for unlimited."),
3e43a32a
MS
12756 NULL, NULL, /* FIXME: i18n: The maximum
12757 number of target hardware
12758 breakpoints is %s. */
b3f42336 12759 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 12760
1b493192
PA
12761 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
12762 &remote_address_size, _("\
4d28ad1e
AC
12763Set the maximum size of the address (in bits) in a memory packet."), _("\
12764Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
12765 NULL,
12766 NULL, /* FIXME: i18n: */
12767 &setlist, &showlist);
c906108c 12768
ca4f7f8b
PA
12769 init_all_packet_configs ();
12770
444abaca 12771 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 12772 "X", "binary-download", 1);
0f71a2f6 12773
444abaca 12774 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 12775 "vCont", "verbose-resume", 0);
506fb367 12776
89be2091
DJ
12777 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
12778 "QPassSignals", "pass-signals", 0);
12779
9b224c5e
PA
12780 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
12781 "QProgramSignals", "program-signals", 0);
12782
444abaca 12783 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 12784 "qSymbol", "symbol-lookup", 0);
dc8acb97 12785
444abaca 12786 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 12787 "P", "set-register", 1);
d471ea57 12788
444abaca 12789 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 12790 "p", "fetch-register", 1);
b96ec7ac 12791
444abaca 12792 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 12793 "Z0", "software-breakpoint", 0);
d471ea57 12794
444abaca 12795 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 12796 "Z1", "hardware-breakpoint", 0);
d471ea57 12797
444abaca 12798 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 12799 "Z2", "write-watchpoint", 0);
d471ea57 12800
444abaca 12801 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 12802 "Z3", "read-watchpoint", 0);
d471ea57 12803
444abaca 12804 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 12805 "Z4", "access-watchpoint", 0);
d471ea57 12806
0876f84a
DJ
12807 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
12808 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 12809
c78fa86a
GB
12810 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
12811 "qXfer:exec-file:read", "pid-to-exec-file", 0);
12812
23181151
DJ
12813 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
12814 "qXfer:features:read", "target-features", 0);
12815
cfa9d6d9
DJ
12816 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
12817 "qXfer:libraries:read", "library-info", 0);
12818
2268b414
JK
12819 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
12820 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
12821
fd79ecee
DJ
12822 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
12823 "qXfer:memory-map:read", "memory-map", 0);
12824
0e7f50da
UW
12825 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
12826 "qXfer:spu:read", "read-spu-object", 0);
12827
12828 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
12829 "qXfer:spu:write", "write-spu-object", 0);
12830
07e059b5
VP
12831 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
12832 "qXfer:osdata:read", "osdata", 0);
12833
dc146f7c
VP
12834 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
12835 "qXfer:threads:read", "threads", 0);
12836
4aa995e1
PA
12837 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
12838 "qXfer:siginfo:read", "read-siginfo-object", 0);
12839
12840 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
12841 "qXfer:siginfo:write", "write-siginfo-object", 0);
12842
b3b9301e
PA
12843 add_packet_config_cmd
12844 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 12845 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 12846
169081d0
TG
12847 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
12848 "qXfer:uib:read", "unwind-info-block", 0);
12849
444abaca 12850 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 12851 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
12852 0);
12853
711e434b
PM
12854 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
12855 "qGetTIBAddr", "get-thread-information-block-address",
12856 0);
12857
40ab02ce
MS
12858 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
12859 "bc", "reverse-continue", 0);
12860
12861 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
12862 "bs", "reverse-step", 0);
12863
be2a5f71
DJ
12864 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
12865 "qSupported", "supported-packets", 0);
12866
08388c79
DE
12867 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
12868 "qSearch:memory", "search-memory", 0);
12869
bd3eecc3
PA
12870 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
12871 "qTStatus", "trace-status", 0);
12872
15a201c8
GB
12873 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
12874 "vFile:setfs", "hostio-setfs", 0);
12875
a6b151f1
DJ
12876 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
12877 "vFile:open", "hostio-open", 0);
12878
12879 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
12880 "vFile:pread", "hostio-pread", 0);
12881
12882 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
12883 "vFile:pwrite", "hostio-pwrite", 0);
12884
12885 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
12886 "vFile:close", "hostio-close", 0);
12887
12888 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
12889 "vFile:unlink", "hostio-unlink", 0);
12890
b9e7b9c3
UW
12891 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
12892 "vFile:readlink", "hostio-readlink", 0);
12893
0a93529c
GB
12894 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
12895 "vFile:fstat", "hostio-fstat", 0);
12896
2d717e4f
DJ
12897 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
12898 "vAttach", "attach", 0);
12899
12900 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
12901 "vRun", "run", 0);
12902
a6f3e723
SL
12903 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
12904 "QStartNoAckMode", "noack", 0);
12905
82f73884
PA
12906 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
12907 "vKill", "kill", 0);
12908
0b16c5cf
PA
12909 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
12910 "qAttached", "query-attached", 0);
12911
782b2b07 12912 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
12913 "ConditionalTracepoints",
12914 "conditional-tracepoints", 0);
3788aec7
LM
12915
12916 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
12917 "ConditionalBreakpoints",
12918 "conditional-breakpoints", 0);
12919
d3ce09f5
SS
12920 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
12921 "BreakpointCommands",
12922 "breakpoint-commands", 0);
12923
7a697b8d
SS
12924 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
12925 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 12926
409873ef
SS
12927 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
12928 "TracepointSource", "TracepointSource", 0);
12929
d914c394
SS
12930 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
12931 "QAllow", "allow", 0);
12932
0fb4aa4b
PA
12933 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
12934 "StaticTracepoints", "static-tracepoints", 0);
12935
1e4d1764
YQ
12936 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
12937 "InstallInTrace", "install-in-trace", 0);
12938
0fb4aa4b
PA
12939 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
12940 "qXfer:statictrace:read", "read-sdata-object", 0);
12941
78d85199
YQ
12942 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
12943 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12944
03583c20
UW
12945 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
12946 "QDisableRandomization", "disable-randomization", 0);
12947
d1feda86
YQ
12948 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
12949 "QAgent", "agent", 0);
12950
f6f899bf
HAQ
12951 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
12952 "QTBuffer:size", "trace-buffer-size", 0);
12953
9accd112
MM
12954 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
12955 "Qbtrace:off", "disable-btrace", 0);
12956
12957 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
b20a6524
MM
12958 "Qbtrace:bts", "enable-btrace-bts", 0);
12959
12960 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_pt],
12961 "Qbtrace:pt", "enable-btrace-pt", 0);
9accd112
MM
12962
12963 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
12964 "qXfer:btrace", "read-btrace", 0);
12965
f4abbc16
MM
12966 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
12967 "qXfer:btrace-conf", "read-btrace-conf", 0);
12968
d33501a5
MM
12969 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
12970 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
12971
f7e6eed5
PA
12972 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
12973 "swbreak-feature", "swbreak-feature", 0);
12974
12975 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
12976 "hwbreak-feature", "hwbreak-feature", 0);
12977
89245bc0
DB
12978 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
12979 "fork-event-feature", "fork-event-feature", 0);
12980
12981 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
12982 "vfork-event-feature", "vfork-event-feature", 0);
12983
b20a6524
MM
12984 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_pt_size],
12985 "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
12986
0b736949
DB
12987 /* Assert that we've registered "set remote foo-packet" commands
12988 for all packet configs. */
ca4f7f8b
PA
12989 {
12990 int i;
12991
12992 for (i = 0; i < PACKET_MAX; i++)
12993 {
12994 /* Ideally all configs would have a command associated. Some
12995 still don't though. */
12996 int excepted;
12997
12998 switch (i)
12999 {
13000 case PACKET_QNonStop:
13001 case PACKET_multiprocess_feature:
13002 case PACKET_EnableDisableTracepoints_feature:
13003 case PACKET_tracenz_feature:
13004 case PACKET_DisconnectedTracing_feature:
13005 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
13006 case PACKET_qCRC:
13007 /* Additions to this list need to be well justified:
13008 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
13009 excepted = 1;
13010 break;
13011 default:
13012 excepted = 0;
13013 break;
13014 }
13015
13016 /* This catches both forgetting to add a config command, and
13017 forgetting to remove a packet from the exception list. */
13018 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
13019 }
13020 }
13021
37a105a1
DJ
13022 /* Keep the old ``set remote Z-packet ...'' working. Each individual
13023 Z sub-packet has its own set and show commands, but users may
13024 have sets to this variable in their .gdbinit files (or in their
13025 documentation). */
e9e68a56 13026 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
13027 &remote_Z_packet_detect, _("\
13028Set use of remote protocol `Z' packets"), _("\
13029Show use of remote protocol `Z' packets "), _("\
3b64bf98 13030When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 13031packets."),
e9e68a56 13032 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
13033 show_remote_protocol_Z_packet_cmd,
13034 /* FIXME: i18n: Use of remote protocol
13035 `Z' packets is %s. */
e9e68a56 13036 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 13037
a6b151f1
DJ
13038 add_prefix_cmd ("remote", class_files, remote_command, _("\
13039Manipulate files on the remote system\n\
13040Transfer files to and from the remote target system."),
13041 &remote_cmdlist, "remote ",
13042 0 /* allow-unknown */, &cmdlist);
13043
13044 add_cmd ("put", class_files, remote_put_command,
13045 _("Copy a local file to the remote system."),
13046 &remote_cmdlist);
13047
13048 add_cmd ("get", class_files, remote_get_command,
13049 _("Copy a remote file to the local system."),
13050 &remote_cmdlist);
13051
13052 add_cmd ("delete", class_files, remote_delete_command,
13053 _("Delete a remote file."),
13054 &remote_cmdlist);
13055
2d717e4f
DJ
13056 remote_exec_file = xstrdup ("");
13057 add_setshow_string_noescape_cmd ("exec-file", class_files,
13058 &remote_exec_file, _("\
13059Set the remote pathname for \"run\""), _("\
13060Show the remote pathname for \"run\""), NULL, NULL, NULL,
13061 &remote_set_cmdlist, &remote_show_cmdlist);
13062
c1e36e3e
PA
13063 add_setshow_boolean_cmd ("range-stepping", class_run,
13064 &use_range_stepping, _("\
13065Enable or disable range stepping."), _("\
13066Show whether target-assisted range stepping is enabled."), _("\
13067If on, and the target supports it, when stepping a source line, GDB\n\
13068tells the target to step the corresponding range of addresses itself instead\n\
13069of issuing multiple single-steps. This speeds up source level\n\
13070stepping. If off, GDB always issues single-steps, even if range\n\
13071stepping is supported by the target. The default is on."),
13072 set_range_stepping,
13073 show_range_stepping,
13074 &setlist,
13075 &showlist);
13076
449092f6
CV
13077 /* Eventually initialize fileio. See fileio.c */
13078 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 13079
ba348170 13080 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 13081 special ptids with it set to != 0. */
ba348170
PA
13082 magic_null_ptid = ptid_build (42000, -1, 1);
13083 not_sent_ptid = ptid_build (42000, -2, 1);
13084 any_thread_ptid = ptid_build (42000, 0, 1);
35b1e5cc
SS
13085
13086 target_buf_size = 2048;
13087 target_buf = xmalloc (target_buf_size);
c906108c 13088}
10760264 13089
This page took 2.943833 seconds and 4 git commands to generate.