Clone remote breakpoints
[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
136d6dae 107static void remote_mourn (struct target_ops *ops);
c906108c 108
a14ed312 109static void extended_remote_restart (void);
c906108c 110
136d6dae 111static void extended_remote_mourn (struct target_ops *);
c906108c 112
6d820c5c 113static void remote_send (char **buf, long *sizeof_buf_p);
c906108c 114
a14ed312 115static int readchar (int timeout);
c906108c 116
c33e31fd
PA
117static void remote_serial_write (const char *str, int len);
118
7d85a9c0 119static void remote_kill (struct target_ops *ops);
c906108c 120
6a109b6b 121static int remote_can_async_p (struct target_ops *);
75c99385 122
6a109b6b 123static int remote_is_async_p (struct target_ops *);
75c99385 124
6a3753b3 125static void remote_async (struct target_ops *ops, int enable);
75c99385 126
934b9bac 127static void sync_remote_interrupt_twice (int signo);
7a292a7a 128
a14ed312 129static void interrupt_query (void);
c906108c 130
79d7f229
PA
131static void set_general_thread (struct ptid ptid);
132static void set_continue_thread (struct ptid ptid);
c906108c 133
a14ed312 134static void get_offsets (void);
c906108c 135
6d820c5c
DJ
136static void skip_frame (void);
137
138static long read_frame (char **buf_p, long *sizeof_buf);
c906108c 139
a14ed312 140static int hexnumlen (ULONGEST num);
c906108c 141
a14ed312 142static void init_remote_ops (void);
c906108c 143
a14ed312 144static void init_extended_remote_ops (void);
c906108c 145
1eab8a48 146static void remote_stop (struct target_ops *self, ptid_t);
c906108c 147
a14ed312 148static int stubhex (int ch);
c906108c 149
a14ed312 150static int hexnumstr (char *, ULONGEST);
c906108c 151
a14ed312 152static int hexnumnstr (char *, ULONGEST, int);
2df3850c 153
a14ed312 154static CORE_ADDR remote_address_masked (CORE_ADDR);
c906108c 155
baa336ce 156static void print_packet (const char *);
c906108c 157
a14ed312 158static void compare_sections_command (char *, int);
c906108c 159
a14ed312 160static void packet_command (char *, int);
c906108c 161
a14ed312 162static int stub_unpack_int (char *buff, int fieldlength);
c906108c 163
39f77062 164static ptid_t remote_current_thread (ptid_t oldptid);
c906108c 165
baa336ce 166static int putpkt_binary (const char *buf, int cnt);
c906108c 167
a14ed312 168static void check_binary_download (CORE_ADDR addr);
c906108c 169
5a2468f5 170struct packet_config;
5a2468f5 171
a14ed312 172static void show_packet_config_cmd (struct packet_config *config);
5a2468f5 173
bb572ddd
DJ
174static void show_remote_protocol_packet_cmd (struct ui_file *file,
175 int from_tty,
176 struct cmd_list_element *c,
177 const char *value);
178
82f73884
PA
179static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
180static ptid_t read_ptid (char *buf, char **obuf);
181
c378d69d 182static void remote_set_permissions (struct target_ops *self);
d914c394 183
d5551862 184struct remote_state;
8bd200f1
TT
185static int remote_get_trace_status (struct target_ops *self,
186 struct trace_status *ts);
d5551862 187
ab6617cc
TT
188static int remote_upload_tracepoints (struct target_ops *self,
189 struct uploaded_tp **utpp);
00bf0b85 190
181e3713
TT
191static int remote_upload_trace_state_variables (struct target_ops *self,
192 struct uploaded_tsv **utsvp);
00bf0b85 193
c8d104ad
PA
194static void remote_query_supported (void);
195
36d25514 196static void remote_check_symbols (void);
c8d104ad 197
a14ed312 198void _initialize_remote (void);
c906108c 199
74531fed 200struct stop_reply;
74531fed 201static void stop_reply_xfree (struct stop_reply *);
722247f1 202static void remote_parse_stop_reply (char *, struct stop_reply *);
74531fed 203static void push_stop_reply (struct stop_reply *);
bcc75809 204static void discard_pending_stop_replies_in_queue (struct remote_state *);
74531fed
PA
205static int peek_stop_reply (ptid_t ptid);
206
207static void remote_async_inferior_event_handler (gdb_client_data);
74531fed 208
e3594fd1 209static void remote_terminal_ours (struct target_ops *self);
d3fd5342 210
d962ef82
DJ
211static int remote_read_description_p (struct target_ops *target);
212
176a6961 213static void remote_console_output (char *msg);
dde08ee1 214
efcc2da7 215static int remote_supports_cond_breakpoints (struct target_ops *self);
b775012e 216
78eff0ec 217static int remote_can_run_breakpoint_commands (struct target_ops *self);
d3ce09f5 218
f4abbc16
MM
219static void remote_btrace_reset (void);
220
a6b151f1
DJ
221/* For "remote". */
222
223static struct cmd_list_element *remote_cmdlist;
224
bb572ddd
DJ
225/* For "set remote" and "show remote". */
226
227static struct cmd_list_element *remote_set_cmdlist;
228static struct cmd_list_element *remote_show_cmdlist;
229
d458bd84
PA
230/* Stub vCont actions support.
231
232 Each field is a boolean flag indicating whether the stub reports
233 support for the corresponding action. */
234
235struct vCont_action_support
236{
237 /* vCont;t */
238 int t;
c1e36e3e
PA
239
240 /* vCont;r */
241 int r;
d458bd84
PA
242};
243
c1e36e3e
PA
244/* Controls whether GDB is willing to use range stepping. */
245
246static int use_range_stepping = 1;
247
0d031856
TT
248#define OPAQUETHREADBYTES 8
249
250/* a 64 bit opaque identifier */
251typedef unsigned char threadref[OPAQUETHREADBYTES];
252
253/* About this many threadisds fit in a packet. */
254
255#define MAXTHREADLISTRESULTS 32
256
ea9c271d
DJ
257/* Description of the remote protocol state for the currently
258 connected target. This is per-target state, and independent of the
259 selected architecture. */
260
261struct remote_state
262{
263 /* A buffer to use for incoming packets, and its current size. The
264 buffer is grown dynamically for larger incoming packets.
265 Outgoing packets may also be constructed in this buffer.
266 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
267 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
268 packets. */
269 char *buf;
270 long buf_size;
be2a5f71 271
1e51243a
PA
272 /* True if we're going through initial connection setup (finding out
273 about the remote side's threads, relocating symbols, etc.). */
274 int starting_up;
275
be2a5f71
DJ
276 /* If we negotiated packet size explicitly (and thus can bypass
277 heuristics for the largest packet size that will not overflow
278 a buffer in the stub), this will be set to that packet size.
279 Otherwise zero, meaning to use the guessed size. */
280 long explicit_packet_size;
2d717e4f
DJ
281
282 /* remote_wait is normally called when the target is running and
283 waits for a stop reply packet. But sometimes we need to call it
284 when the target is already stopped. We can send a "?" packet
285 and have remote_wait read the response. Or, if we already have
286 the response, we can stash it in BUF and tell remote_wait to
287 skip calling getpkt. This flag is set when BUF contains a
288 stop reply packet and the target is not waiting. */
289 int cached_wait_status;
a6f3e723
SL
290
291 /* True, if in no ack mode. That is, neither GDB nor the stub will
292 expect acks from each other. The connection is assumed to be
293 reliable. */
294 int noack_mode;
82f73884
PA
295
296 /* True if we're connected in extended remote mode. */
297 int extended;
298
e24a49d8
PA
299 /* True if we resumed the target and we're waiting for the target to
300 stop. In the mean time, we can't start another command/query.
301 The remote server wouldn't be ready to process it, so we'd
302 timeout waiting for a reply that would never come and eventually
303 we'd close the connection. This can happen in asynchronous mode
304 because we allow GDB commands while the target is running. */
305 int waiting_for_stop_reply;
74531fed 306
d458bd84
PA
307 /* The status of the stub support for the various vCont actions. */
308 struct vCont_action_support supports_vCont;
782b2b07 309
3a29589a
DJ
310 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
311 responded to that. */
312 int ctrlc_pending_p;
5d93a237
TT
313
314 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
315 remote_open knows that we don't have a file open when the program
316 starts. */
317 struct serial *remote_desc;
47f8a51d
TT
318
319 /* These are the threads which we last sent to the remote system. The
320 TID member will be -1 for all or -2 for not sent yet. */
321 ptid_t general_thread;
322 ptid_t continue_thread;
262e1174
TT
323
324 /* This is the traceframe which we last selected on the remote system.
325 It will be -1 if no traceframe is selected. */
326 int remote_traceframe_number;
747dc59d
TT
327
328 char *last_pass_packet;
5e4a05c4
TT
329
330 /* The last QProgramSignals packet sent to the target. We bypass
331 sending a new program signals list down to the target if the new
332 packet is exactly the same as the last we sent. IOW, we only let
333 the target know about program signals list changes. */
334 char *last_program_signals_packet;
b73be471
TT
335
336 enum gdb_signal last_sent_signal;
280ceea3
TT
337
338 int last_sent_step;
8e88304f
TT
339
340 char *finished_object;
341 char *finished_annex;
342 ULONGEST finished_offset;
b80fafe3
TT
343
344 /* Should we try the 'ThreadInfo' query packet?
345
346 This variable (NOT available to the user: auto-detect only!)
347 determines whether GDB will use the new, simpler "ThreadInfo"
348 query or the older, more complex syntax for thread queries.
349 This is an auto-detect variable (set to true at each connect,
350 and set to false when the target fails to recognize it). */
351 int use_threadinfo_query;
352 int use_threadextra_query;
88b496c3 353
ee154bee
TT
354 /* This is set to the data address of the access causing the target
355 to stop for a watchpoint. */
356 CORE_ADDR remote_watch_data_address;
357
f7e6eed5
PA
358 /* Whether the target stopped for a breakpoint/watchpoint. */
359 enum target_stop_reason stop_reason;
0d031856
TT
360
361 threadref echo_nextthread;
362 threadref nextthread;
363 threadref resultthreadlist[MAXTHREADLISTRESULTS];
5965e028
YQ
364
365 /* The state of remote notification. */
366 struct remote_notif_state *notif_state;
f4abbc16
MM
367
368 /* The branch trace configuration. */
369 struct btrace_config btrace_config;
ea9c271d
DJ
370};
371
dc146f7c
VP
372/* Private data that we'll store in (struct thread_info)->private. */
373struct private_thread_info
374{
375 char *extra;
376 int core;
377};
378
379static void
380free_private_thread_info (struct private_thread_info *info)
381{
382 xfree (info->extra);
383 xfree (info);
384}
385
ea9c271d
DJ
386/* This data could be associated with a target, but we do not always
387 have access to the current target when we need it, so for now it is
388 static. This will be fine for as long as only one target is in use
389 at a time. */
cf792862 390static struct remote_state *remote_state;
ea9c271d
DJ
391
392static struct remote_state *
0b83947e 393get_remote_state_raw (void)
ea9c271d 394{
cf792862
TT
395 return remote_state;
396}
397
398/* Allocate a new struct remote_state with xmalloc, initialize it, and
399 return it. */
400
401static struct remote_state *
402new_remote_state (void)
403{
404 struct remote_state *result = XCNEW (struct remote_state);
405
406 /* The default buffer size is unimportant; it will be expanded
407 whenever a larger buffer is needed. */
408 result->buf_size = 400;
409 result->buf = xmalloc (result->buf_size);
262e1174 410 result->remote_traceframe_number = -1;
b73be471 411 result->last_sent_signal = GDB_SIGNAL_0;
cf792862
TT
412
413 return result;
ea9c271d
DJ
414}
415
416/* Description of the remote protocol for a given architecture. */
d01949b6 417
ad10f812
AC
418struct packet_reg
419{
420 long offset; /* Offset into G packet. */
421 long regnum; /* GDB's internal register number. */
422 LONGEST pnum; /* Remote protocol register number. */
b323314b 423 int in_g_packet; /* Always part of G packet. */
f5656ead 424 /* long size in bytes; == register_size (target_gdbarch (), regnum);
23860348 425 at present. */
f5656ead 426 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
c9f4d572 427 at present. */
ad10f812
AC
428};
429
ea9c271d 430struct remote_arch_state
d01949b6 431{
ad10f812
AC
432 /* Description of the remote protocol registers. */
433 long sizeof_g_packet;
b323314b
AC
434
435 /* Description of the remote protocol registers indexed by REGNUM
f57d151a 436 (making an array gdbarch_num_regs in size). */
b323314b 437 struct packet_reg *regs;
ad10f812 438
d01949b6
AC
439 /* This is the size (in chars) of the first response to the ``g''
440 packet. It is used as a heuristic when determining the maximum
441 size of memory-read and memory-write packets. A target will
442 typically only reserve a buffer large enough to hold the ``g''
443 packet. The size does not include packet overhead (headers and
23860348 444 trailers). */
d01949b6
AC
445 long actual_register_packet_size;
446
447 /* This is the maximum size (in chars) of a non read/write packet.
23860348 448 It is also used as a cap on the size of read/write packets. */
d01949b6
AC
449 long remote_packet_size;
450};
451
35b1e5cc
SS
452/* Utility: generate error from an incoming stub packet. */
453static void
454trace_error (char *buf)
455{
456 if (*buf++ != 'E')
457 return; /* not an error msg */
458 switch (*buf)
459 {
460 case '1': /* malformed packet error */
461 if (*++buf == '0') /* general case: */
462 error (_("remote.c: error in outgoing packet."));
463 else
464 error (_("remote.c: error in outgoing packet at field #%ld."),
465 strtol (buf, NULL, 16));
35b1e5cc
SS
466 default:
467 error (_("Target returns error code '%s'."), buf);
468 }
469}
470
471/* Utility: wait for reply from stub, while accepting "O" packets. */
472static char *
473remote_get_noisy_reply (char **buf_p,
474 long *sizeof_buf)
475{
476 do /* Loop on reply from remote stub. */
477 {
478 char *buf;
a744cf53 479
0df8b418 480 QUIT; /* Allow user to bail out with ^C. */
35b1e5cc
SS
481 getpkt (buf_p, sizeof_buf, 0);
482 buf = *buf_p;
ad91cd99 483 if (buf[0] == 'E')
35b1e5cc 484 trace_error (buf);
61012eef 485 else if (startswith (buf, "qRelocInsn:"))
dde08ee1
PA
486 {
487 ULONGEST ul;
488 CORE_ADDR from, to, org_to;
489 char *p, *pp;
490 int adjusted_size = 0;
7556d4a4 491 int relocated = 0;
dde08ee1
PA
492
493 p = buf + strlen ("qRelocInsn:");
494 pp = unpack_varlen_hex (p, &ul);
495 if (*pp != ';')
cb91c06a 496 error (_("invalid qRelocInsn packet: %s"), buf);
dde08ee1
PA
497 from = ul;
498
499 p = pp + 1;
a9cbf802 500 unpack_varlen_hex (p, &ul);
dde08ee1
PA
501 to = ul;
502
503 org_to = to;
504
492d29ea 505 TRY
dde08ee1 506 {
f5656ead 507 gdbarch_relocate_instruction (target_gdbarch (), &to, from);
7556d4a4 508 relocated = 1;
dde08ee1 509 }
492d29ea 510 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
511 {
512 if (ex.error == MEMORY_ERROR)
513 {
514 /* Propagate memory errors silently back to the
515 target. The stub may have limited the range of
516 addresses we can write to, for example. */
517 }
518 else
519 {
520 /* Something unexpectedly bad happened. Be verbose
521 so we can tell what, and propagate the error back
522 to the stub, so it doesn't get stuck waiting for
523 a response. */
524 exception_fprintf (gdb_stderr, ex,
525 _("warning: relocating instruction: "));
526 }
527 putpkt ("E01");
528 }
492d29ea 529 END_CATCH
7556d4a4
PA
530
531 if (relocated)
dde08ee1
PA
532 {
533 adjusted_size = to - org_to;
534
bba74b36 535 xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
dde08ee1
PA
536 putpkt (buf);
537 }
dde08ee1 538 }
ad91cd99 539 else if (buf[0] == 'O' && buf[1] != 'K')
35b1e5cc
SS
540 remote_console_output (buf + 1); /* 'O' message from stub */
541 else
0df8b418 542 return buf; /* Here's the actual reply. */
35b1e5cc
SS
543 }
544 while (1);
545}
3c3bea1c 546
d01949b6
AC
547/* Handle for retreving the remote protocol data from gdbarch. */
548static struct gdbarch_data *remote_gdbarch_data_handle;
549
ea9c271d
DJ
550static struct remote_arch_state *
551get_remote_arch_state (void)
d01949b6 552{
f5656ead 553 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
d01949b6
AC
554}
555
0b83947e
DJ
556/* Fetch the global remote target state. */
557
558static struct remote_state *
559get_remote_state (void)
560{
561 /* Make sure that the remote architecture state has been
562 initialized, because doing so might reallocate rs->buf. Any
563 function which calls getpkt also needs to be mindful of changes
564 to rs->buf, but this call limits the number of places which run
565 into trouble. */
566 get_remote_arch_state ();
567
568 return get_remote_state_raw ();
569}
570
74ca34ce
DJ
571static int
572compare_pnums (const void *lhs_, const void *rhs_)
573{
574 const struct packet_reg * const *lhs = lhs_;
575 const struct packet_reg * const *rhs = rhs_;
576
577 if ((*lhs)->pnum < (*rhs)->pnum)
578 return -1;
579 else if ((*lhs)->pnum == (*rhs)->pnum)
580 return 0;
581 else
582 return 1;
583}
584
c21236dc
PA
585static int
586map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
d01949b6 587{
74ca34ce 588 int regnum, num_remote_regs, offset;
74ca34ce 589 struct packet_reg **remote_regs;
ea9c271d 590
4a22f64d 591 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
ad10f812 592 {
c21236dc 593 struct packet_reg *r = &regs[regnum];
baef701f 594
4a22f64d 595 if (register_size (gdbarch, regnum) == 0)
baef701f
DJ
596 /* Do not try to fetch zero-sized (placeholder) registers. */
597 r->pnum = -1;
598 else
599 r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
600
b323314b 601 r->regnum = regnum;
74ca34ce
DJ
602 }
603
604 /* Define the g/G packet format as the contents of each register
605 with a remote protocol number, in order of ascending protocol
606 number. */
607
4a22f64d 608 remote_regs = alloca (gdbarch_num_regs (gdbarch)
c21236dc 609 * sizeof (struct packet_reg *));
f57d151a 610 for (num_remote_regs = 0, regnum = 0;
4a22f64d 611 regnum < gdbarch_num_regs (gdbarch);
f57d151a 612 regnum++)
c21236dc
PA
613 if (regs[regnum].pnum != -1)
614 remote_regs[num_remote_regs++] = &regs[regnum];
7d58c67d 615
74ca34ce
DJ
616 qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
617 compare_pnums);
618
619 for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
620 {
621 remote_regs[regnum]->in_g_packet = 1;
622 remote_regs[regnum]->offset = offset;
4a22f64d 623 offset += register_size (gdbarch, remote_regs[regnum]->regnum);
ad10f812
AC
624 }
625
c21236dc
PA
626 return offset;
627}
628
629/* Given the architecture described by GDBARCH, return the remote
630 protocol register's number and the register's offset in the g/G
631 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
632 If the target does not have a mapping for REGNUM, return false,
633 otherwise, return true. */
634
635int
636remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
637 int *pnum, int *poffset)
638{
639 int sizeof_g_packet;
640 struct packet_reg *regs;
641 struct cleanup *old_chain;
642
643 gdb_assert (regnum < gdbarch_num_regs (gdbarch));
644
645 regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
646 old_chain = make_cleanup (xfree, regs);
647
648 sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
649
650 *pnum = regs[regnum].pnum;
651 *poffset = regs[regnum].offset;
652
653 do_cleanups (old_chain);
654
655 return *pnum != -1;
656}
657
658static void *
659init_remote_state (struct gdbarch *gdbarch)
660{
661 struct remote_state *rs = get_remote_state_raw ();
662 struct remote_arch_state *rsa;
663
664 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
665
666 /* Use the architecture to build a regnum<->pnum table, which will be
667 1:1 unless a feature set specifies otherwise. */
668 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
669 gdbarch_num_regs (gdbarch),
670 struct packet_reg);
671
74ca34ce
DJ
672 /* Record the maximum possible size of the g packet - it may turn out
673 to be smaller. */
c21236dc 674 rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
74ca34ce 675
0df8b418 676 /* Default maximum number of characters in a packet body. Many
d01949b6
AC
677 remote stubs have a hardwired buffer size of 400 bytes
678 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
679 as the maximum packet-size to ensure that the packet and an extra
680 NUL character can always fit in the buffer. This stops GDB
681 trashing stubs that try to squeeze an extra NUL into what is
ea9c271d
DJ
682 already a full buffer (As of 1999-12-04 that was most stubs). */
683 rsa->remote_packet_size = 400 - 1;
d01949b6 684
ea9c271d
DJ
685 /* This one is filled in when a ``g'' packet is received. */
686 rsa->actual_register_packet_size = 0;
687
688 /* Should rsa->sizeof_g_packet needs more space than the
0df8b418
MS
689 default, adjust the size accordingly. Remember that each byte is
690 encoded as two characters. 32 is the overhead for the packet
691 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 692 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 693 little. */
ea9c271d
DJ
694 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
695 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
802188a7 696
ea9c271d
DJ
697 /* Make sure that the packet buffer is plenty big enough for
698 this architecture. */
699 if (rs->buf_size < rsa->remote_packet_size)
700 {
701 rs->buf_size = 2 * rsa->remote_packet_size;
7fca722e 702 rs->buf = xrealloc (rs->buf, rs->buf_size);
ea9c271d 703 }
6d820c5c 704
ea9c271d
DJ
705 return rsa;
706}
707
708/* Return the current allowed size of a remote packet. This is
709 inferred from the current architecture, and should be used to
710 limit the length of outgoing packets. */
711static long
712get_remote_packet_size (void)
713{
be2a5f71 714 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
715 struct remote_arch_state *rsa = get_remote_arch_state ();
716
be2a5f71
DJ
717 if (rs->explicit_packet_size)
718 return rs->explicit_packet_size;
719
ea9c271d 720 return rsa->remote_packet_size;
d01949b6
AC
721}
722
ad10f812 723static struct packet_reg *
ea9c271d 724packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
ad10f812 725{
f5656ead 726 if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
b323314b
AC
727 return NULL;
728 else
ad10f812 729 {
ea9c271d 730 struct packet_reg *r = &rsa->regs[regnum];
a744cf53 731
b323314b
AC
732 gdb_assert (r->regnum == regnum);
733 return r;
ad10f812 734 }
ad10f812
AC
735}
736
737static struct packet_reg *
ea9c271d 738packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
ad10f812 739{
b323314b 740 int i;
a744cf53 741
f5656ead 742 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
ad10f812 743 {
ea9c271d 744 struct packet_reg *r = &rsa->regs[i];
a744cf53 745
b323314b
AC
746 if (r->pnum == pnum)
747 return r;
ad10f812
AC
748 }
749 return NULL;
d01949b6
AC
750}
751
c906108c
SS
752static struct target_ops remote_ops;
753
754static struct target_ops extended_remote_ops;
755
6426a772
JM
756/* FIXME: cagney/1999-09-23: Even though getpkt was called with
757 ``forever'' still use the normal timeout mechanism. This is
758 currently used by the ASYNC code to guarentee that target reads
759 during the initial connect always time-out. Once getpkt has been
760 modified to return a timeout indication and, in turn
761 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 762 this can go away. */
6426a772
JM
763static int wait_forever_enabled_p = 1;
764
9a7071a8
JB
765/* Allow the user to specify what sequence to send to the remote
766 when he requests a program interruption: Although ^C is usually
767 what remote systems expect (this is the default, here), it is
768 sometimes preferable to send a break. On other systems such
769 as the Linux kernel, a break followed by g, which is Magic SysRq g
770 is required in order to interrupt the execution. */
771const char interrupt_sequence_control_c[] = "Ctrl-C";
772const char interrupt_sequence_break[] = "BREAK";
773const char interrupt_sequence_break_g[] = "BREAK-g";
40478521 774static const char *const interrupt_sequence_modes[] =
9a7071a8
JB
775 {
776 interrupt_sequence_control_c,
777 interrupt_sequence_break,
778 interrupt_sequence_break_g,
779 NULL
780 };
781static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
782
783static void
784show_interrupt_sequence (struct ui_file *file, int from_tty,
785 struct cmd_list_element *c,
786 const char *value)
787{
788 if (interrupt_sequence_mode == interrupt_sequence_control_c)
789 fprintf_filtered (file,
790 _("Send the ASCII ETX character (Ctrl-c) "
791 "to the remote target to interrupt the "
792 "execution of the program.\n"));
793 else if (interrupt_sequence_mode == interrupt_sequence_break)
794 fprintf_filtered (file,
795 _("send a break signal to the remote target "
796 "to interrupt the execution of the program.\n"));
797 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
798 fprintf_filtered (file,
799 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
800 "the remote target to interrupt the execution "
801 "of Linux kernel.\n"));
802 else
803 internal_error (__FILE__, __LINE__,
804 _("Invalid value for interrupt_sequence_mode: %s."),
805 interrupt_sequence_mode);
806}
6426a772 807
9a7071a8
JB
808/* This boolean variable specifies whether interrupt_sequence is sent
809 to the remote target when gdb connects to it.
810 This is mostly needed when you debug the Linux kernel: The Linux kernel
811 expects BREAK g which is Magic SysRq g for connecting gdb. */
812static int interrupt_on_connect = 0;
c906108c 813
9a7071a8
JB
814/* This variable is used to implement the "set/show remotebreak" commands.
815 Since these commands are now deprecated in favor of "set/show remote
816 interrupt-sequence", it no longer has any effect on the code. */
c906108c
SS
817static int remote_break;
818
9a7071a8
JB
819static void
820set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
821{
822 if (remote_break)
823 interrupt_sequence_mode = interrupt_sequence_break;
824 else
825 interrupt_sequence_mode = interrupt_sequence_control_c;
826}
827
828static void
829show_remotebreak (struct ui_file *file, int from_tty,
830 struct cmd_list_element *c,
831 const char *value)
832{
833}
834
c906108c
SS
835/* This variable sets the number of bits in an address that are to be
836 sent in a memory ("M" or "m") packet. Normally, after stripping
0df8b418 837 leading zeros, the entire address would be sent. This variable
c906108c
SS
838 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
839 initial implementation of remote.c restricted the address sent in
840 memory packets to ``host::sizeof long'' bytes - (typically 32
841 bits). Consequently, for 64 bit targets, the upper 32 bits of an
842 address was never sent. Since fixing this bug may cause a break in
843 some remote targets this variable is principly provided to
23860348 844 facilitate backward compatibility. */
c906108c 845
883b9c6c 846static unsigned int remote_address_size;
c906108c 847
75c99385
PA
848/* Temporary to track who currently owns the terminal. See
849 remote_terminal_* for more details. */
6426a772
JM
850
851static int remote_async_terminal_ours_p;
852
2d717e4f
DJ
853/* The executable file to use for "run" on the remote side. */
854
855static char *remote_exec_file = "";
856
11cf8741 857\f
11cf8741 858/* User configurable variables for the number of characters in a
ea9c271d
DJ
859 memory read/write packet. MIN (rsa->remote_packet_size,
860 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 861 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
862 (speed up transfers). The variables ``preferred_*'' (the user
863 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 864 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
865
866struct memory_packet_config
867{
868 char *name;
869 long size;
870 int fixed_p;
871};
872
873/* Compute the current size of a read/write packet. Since this makes
874 use of ``actual_register_packet_size'' the computation is dynamic. */
875
876static long
877get_memory_packet_size (struct memory_packet_config *config)
878{
d01949b6 879 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
880 struct remote_arch_state *rsa = get_remote_arch_state ();
881
11cf8741
JM
882 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
883 law?) that some hosts don't cope very well with large alloca()
884 calls. Eventually the alloca() code will be replaced by calls to
885 xmalloc() and make_cleanups() allowing this restriction to either
23860348 886 be lifted or removed. */
11cf8741
JM
887#ifndef MAX_REMOTE_PACKET_SIZE
888#define MAX_REMOTE_PACKET_SIZE 16384
889#endif
3de11b2e 890 /* NOTE: 20 ensures we can write at least one byte. */
11cf8741 891#ifndef MIN_REMOTE_PACKET_SIZE
3de11b2e 892#define MIN_REMOTE_PACKET_SIZE 20
11cf8741
JM
893#endif
894 long what_they_get;
895 if (config->fixed_p)
896 {
897 if (config->size <= 0)
898 what_they_get = MAX_REMOTE_PACKET_SIZE;
899 else
900 what_they_get = config->size;
901 }
902 else
903 {
ea9c271d 904 what_they_get = get_remote_packet_size ();
23860348 905 /* Limit the packet to the size specified by the user. */
11cf8741
JM
906 if (config->size > 0
907 && what_they_get > config->size)
908 what_they_get = config->size;
be2a5f71
DJ
909
910 /* Limit it to the size of the targets ``g'' response unless we have
911 permission from the stub to use a larger packet size. */
912 if (rs->explicit_packet_size == 0
913 && rsa->actual_register_packet_size > 0
914 && what_they_get > rsa->actual_register_packet_size)
915 what_they_get = rsa->actual_register_packet_size;
11cf8741
JM
916 }
917 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
918 what_they_get = MAX_REMOTE_PACKET_SIZE;
919 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
920 what_they_get = MIN_REMOTE_PACKET_SIZE;
6d820c5c
DJ
921
922 /* Make sure there is room in the global buffer for this packet
923 (including its trailing NUL byte). */
924 if (rs->buf_size < what_they_get + 1)
925 {
926 rs->buf_size = 2 * what_they_get;
927 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
928 }
929
11cf8741
JM
930 return what_they_get;
931}
932
0df8b418 933/* Update the size of a read/write packet. If they user wants
23860348 934 something really big then do a sanity check. */
11cf8741
JM
935
936static void
937set_memory_packet_size (char *args, struct memory_packet_config *config)
938{
939 int fixed_p = config->fixed_p;
940 long size = config->size;
a744cf53 941
11cf8741 942 if (args == NULL)
8a3fe4f8 943 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
944 else if (strcmp (args, "hard") == 0
945 || strcmp (args, "fixed") == 0)
946 fixed_p = 1;
947 else if (strcmp (args, "soft") == 0
948 || strcmp (args, "limit") == 0)
949 fixed_p = 0;
950 else
951 {
952 char *end;
a744cf53 953
11cf8741
JM
954 size = strtoul (args, &end, 0);
955 if (args == end)
8a3fe4f8 956 error (_("Invalid %s (bad syntax)."), config->name);
11cf8741
JM
957#if 0
958 /* Instead of explicitly capping the size of a packet to
959 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
960 instead allowed to set the size to something arbitrarily
23860348 961 large. */
11cf8741 962 if (size > MAX_REMOTE_PACKET_SIZE)
8a3fe4f8 963 error (_("Invalid %s (too large)."), config->name);
11cf8741
JM
964#endif
965 }
23860348 966 /* Extra checks? */
11cf8741
JM
967 if (fixed_p && !config->fixed_p)
968 {
e2e0b3e5
AC
969 if (! query (_("The target may not be able to correctly handle a %s\n"
970 "of %ld bytes. Change the packet size? "),
11cf8741 971 config->name, size))
8a3fe4f8 972 error (_("Packet size not changed."));
11cf8741 973 }
23860348 974 /* Update the config. */
11cf8741
JM
975 config->fixed_p = fixed_p;
976 config->size = size;
977}
978
979static void
980show_memory_packet_size (struct memory_packet_config *config)
981{
a3f17187 982 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 983 if (config->fixed_p)
a3f17187 984 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
985 get_memory_packet_size (config));
986 else
a3f17187 987 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
988 get_memory_packet_size (config));
989}
990
991static struct memory_packet_config memory_write_packet_config =
992{
993 "memory-write-packet-size",
994};
995
996static void
997set_memory_write_packet_size (char *args, int from_tty)
998{
999 set_memory_packet_size (args, &memory_write_packet_config);
1000}
1001
1002static void
1003show_memory_write_packet_size (char *args, int from_tty)
1004{
1005 show_memory_packet_size (&memory_write_packet_config);
1006}
1007
1008static long
1009get_memory_write_packet_size (void)
1010{
1011 return get_memory_packet_size (&memory_write_packet_config);
1012}
1013
1014static struct memory_packet_config memory_read_packet_config =
1015{
1016 "memory-read-packet-size",
1017};
1018
1019static void
1020set_memory_read_packet_size (char *args, int from_tty)
1021{
1022 set_memory_packet_size (args, &memory_read_packet_config);
1023}
1024
1025static void
1026show_memory_read_packet_size (char *args, int from_tty)
1027{
1028 show_memory_packet_size (&memory_read_packet_config);
1029}
1030
1031static long
1032get_memory_read_packet_size (void)
1033{
1034 long size = get_memory_packet_size (&memory_read_packet_config);
a744cf53 1035
11cf8741
JM
1036 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1037 extra buffer size argument before the memory read size can be
ea9c271d
DJ
1038 increased beyond this. */
1039 if (size > get_remote_packet_size ())
1040 size = get_remote_packet_size ();
11cf8741
JM
1041 return size;
1042}
1043
11cf8741 1044\f
5a2468f5 1045/* Generic configuration support for packets the stub optionally
0df8b418 1046 supports. Allows the user to specify the use of the packet as well
23860348 1047 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
1048
1049enum packet_support
1050 {
1051 PACKET_SUPPORT_UNKNOWN = 0,
1052 PACKET_ENABLE,
1053 PACKET_DISABLE
1054 };
1055
5a2468f5
JM
1056struct packet_config
1057 {
bb572ddd
DJ
1058 const char *name;
1059 const char *title;
4082afcc
PA
1060
1061 /* If auto, GDB auto-detects support for this packet or feature,
1062 either through qSupported, or by trying the packet and looking
1063 at the response. If true, GDB assumes the target supports this
ca4f7f8b
PA
1064 packet. If false, the packet is disabled. Configs that don't
1065 have an associated command always have this set to auto. */
7f19b9a2 1066 enum auto_boolean detect;
4082afcc
PA
1067
1068 /* Does the target support this packet? */
5a2468f5
JM
1069 enum packet_support support;
1070 };
1071
d471ea57 1072/* Analyze a packet's return value and update the packet config
23860348 1073 accordingly. */
d471ea57
AC
1074
1075enum packet_result
1076{
1077 PACKET_ERROR,
1078 PACKET_OK,
1079 PACKET_UNKNOWN
1080};
1081
4082afcc
PA
1082static enum packet_support packet_config_support (struct packet_config *config);
1083static enum packet_support packet_support (int packet);
5a2468f5
JM
1084
1085static void
fba45db2 1086show_packet_config_cmd (struct packet_config *config)
5a2468f5
JM
1087{
1088 char *support = "internal-error";
a744cf53 1089
4082afcc 1090 switch (packet_config_support (config))
5a2468f5
JM
1091 {
1092 case PACKET_ENABLE:
1093 support = "enabled";
1094 break;
1095 case PACKET_DISABLE:
1096 support = "disabled";
1097 break;
1098 case PACKET_SUPPORT_UNKNOWN:
1099 support = "unknown";
1100 break;
1101 }
1102 switch (config->detect)
1103 {
7f19b9a2 1104 case AUTO_BOOLEAN_AUTO:
3e43a32a
MS
1105 printf_filtered (_("Support for the `%s' packet "
1106 "is auto-detected, currently %s.\n"),
37a105a1 1107 config->name, support);
5a2468f5 1108 break;
7f19b9a2
AC
1109 case AUTO_BOOLEAN_TRUE:
1110 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
1111 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1112 config->name, support);
8e248173 1113 break;
5a2468f5
JM
1114 }
1115}
1116
1117static void
bb572ddd
DJ
1118add_packet_config_cmd (struct packet_config *config, const char *name,
1119 const char *title, int legacy)
d471ea57 1120{
5a2468f5
JM
1121 char *set_doc;
1122 char *show_doc;
d471ea57 1123 char *cmd_name;
3ed07be4 1124
5a2468f5
JM
1125 config->name = name;
1126 config->title = title;
b435e160
AC
1127 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1128 name, title);
3e43a32a
MS
1129 show_doc = xstrprintf ("Show current use of remote "
1130 "protocol `%s' (%s) packet",
b435e160 1131 name, title);
d471ea57 1132 /* set/show TITLE-packet {auto,on,off} */
b435e160 1133 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 1134 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
3e43a32a
MS
1135 &config->detect, set_doc,
1136 show_doc, NULL, /* help_doc */
4082afcc 1137 NULL,
bb572ddd
DJ
1138 show_remote_protocol_packet_cmd,
1139 &remote_set_cmdlist, &remote_show_cmdlist);
1eefb858
TT
1140 /* The command code copies the documentation strings. */
1141 xfree (set_doc);
1142 xfree (show_doc);
23860348 1143 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
1144 if (legacy)
1145 {
1146 char *legacy_name;
a744cf53 1147
b435e160 1148 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 1149 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1150 &remote_set_cmdlist);
d471ea57 1151 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 1152 &remote_show_cmdlist);
d471ea57 1153 }
5a2468f5
JM
1154}
1155
d471ea57 1156static enum packet_result
a76d924d 1157packet_check_result (const char *buf)
5a2468f5 1158{
d471ea57 1159 if (buf[0] != '\0')
5a2468f5 1160 {
d471ea57 1161 /* The stub recognized the packet request. Check that the
23860348 1162 operation succeeded. */
a76d924d
DJ
1163 if (buf[0] == 'E'
1164 && isxdigit (buf[1]) && isxdigit (buf[2])
1165 && buf[3] == '\0')
1166 /* "Enn" - definitly an error. */
1167 return PACKET_ERROR;
1168
1169 /* Always treat "E." as an error. This will be used for
1170 more verbose error messages, such as E.memtypes. */
1171 if (buf[0] == 'E' && buf[1] == '.')
1172 return PACKET_ERROR;
1173
1174 /* The packet may or may not be OK. Just assume it is. */
1175 return PACKET_OK;
1176 }
1177 else
1178 /* The stub does not support the packet. */
1179 return PACKET_UNKNOWN;
1180}
1181
1182static enum packet_result
1183packet_ok (const char *buf, struct packet_config *config)
1184{
1185 enum packet_result result;
1186
4082afcc
PA
1187 if (config->detect != AUTO_BOOLEAN_TRUE
1188 && config->support == PACKET_DISABLE)
1189 internal_error (__FILE__, __LINE__,
1190 _("packet_ok: attempt to use a disabled packet"));
1191
a76d924d
DJ
1192 result = packet_check_result (buf);
1193 switch (result)
1194 {
1195 case PACKET_OK:
1196 case PACKET_ERROR:
1197 /* The stub recognized the packet request. */
4082afcc 1198 if (config->support == PACKET_SUPPORT_UNKNOWN)
d471ea57 1199 {
d471ea57
AC
1200 if (remote_debug)
1201 fprintf_unfiltered (gdb_stdlog,
4082afcc
PA
1202 "Packet %s (%s) is supported\n",
1203 config->name, config->title);
d471ea57 1204 config->support = PACKET_ENABLE;
d471ea57 1205 }
a76d924d
DJ
1206 break;
1207 case PACKET_UNKNOWN:
23860348 1208 /* The stub does not support the packet. */
4082afcc
PA
1209 if (config->detect == AUTO_BOOLEAN_AUTO
1210 && config->support == PACKET_ENABLE)
d471ea57 1211 {
4082afcc
PA
1212 /* If the stub previously indicated that the packet was
1213 supported then there is a protocol error. */
1214 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1215 config->name, config->title);
1216 }
1217 else if (config->detect == AUTO_BOOLEAN_TRUE)
1218 {
1219 /* The user set it wrong. */
1220 error (_("Enabled packet %s (%s) not recognized by stub"),
1221 config->name, config->title);
d471ea57 1222 }
4082afcc
PA
1223
1224 if (remote_debug)
1225 fprintf_unfiltered (gdb_stdlog,
1226 "Packet %s (%s) is NOT supported\n",
1227 config->name, config->title);
1228 config->support = PACKET_DISABLE;
a76d924d 1229 break;
5a2468f5 1230 }
a76d924d
DJ
1231
1232 return result;
5a2468f5
JM
1233}
1234
444abaca
DJ
1235enum {
1236 PACKET_vCont = 0,
1237 PACKET_X,
1238 PACKET_qSymbol,
1239 PACKET_P,
1240 PACKET_p,
1241 PACKET_Z0,
1242 PACKET_Z1,
1243 PACKET_Z2,
1244 PACKET_Z3,
1245 PACKET_Z4,
a6b151f1
DJ
1246 PACKET_vFile_open,
1247 PACKET_vFile_pread,
1248 PACKET_vFile_pwrite,
1249 PACKET_vFile_close,
1250 PACKET_vFile_unlink,
b9e7b9c3 1251 PACKET_vFile_readlink,
0a93529c 1252 PACKET_vFile_fstat,
0876f84a 1253 PACKET_qXfer_auxv,
23181151 1254 PACKET_qXfer_features,
c78fa86a 1255 PACKET_qXfer_exec_file,
cfa9d6d9 1256 PACKET_qXfer_libraries,
2268b414 1257 PACKET_qXfer_libraries_svr4,
fd79ecee 1258 PACKET_qXfer_memory_map,
0e7f50da
UW
1259 PACKET_qXfer_spu_read,
1260 PACKET_qXfer_spu_write,
07e059b5 1261 PACKET_qXfer_osdata,
dc146f7c 1262 PACKET_qXfer_threads,
0fb4aa4b 1263 PACKET_qXfer_statictrace_read,
b3b9301e 1264 PACKET_qXfer_traceframe_info,
169081d0 1265 PACKET_qXfer_uib,
711e434b 1266 PACKET_qGetTIBAddr,
444abaca 1267 PACKET_qGetTLSAddr,
be2a5f71 1268 PACKET_qSupported,
bd3eecc3 1269 PACKET_qTStatus,
89be2091 1270 PACKET_QPassSignals,
9b224c5e 1271 PACKET_QProgramSignals,
936d2992 1272 PACKET_qCRC,
08388c79 1273 PACKET_qSearch_memory,
2d717e4f
DJ
1274 PACKET_vAttach,
1275 PACKET_vRun,
a6f3e723 1276 PACKET_QStartNoAckMode,
82f73884 1277 PACKET_vKill,
4aa995e1
PA
1278 PACKET_qXfer_siginfo_read,
1279 PACKET_qXfer_siginfo_write,
0b16c5cf 1280 PACKET_qAttached,
4082afcc
PA
1281
1282 /* Support for conditional tracepoints. */
782b2b07 1283 PACKET_ConditionalTracepoints,
4082afcc
PA
1284
1285 /* Support for target-side breakpoint conditions. */
3788aec7 1286 PACKET_ConditionalBreakpoints,
4082afcc
PA
1287
1288 /* Support for target-side breakpoint commands. */
d3ce09f5 1289 PACKET_BreakpointCommands,
4082afcc
PA
1290
1291 /* Support for fast tracepoints. */
7a697b8d 1292 PACKET_FastTracepoints,
4082afcc
PA
1293
1294 /* Support for static tracepoints. */
0fb4aa4b 1295 PACKET_StaticTracepoints,
4082afcc
PA
1296
1297 /* Support for installing tracepoints while a trace experiment is
1298 running. */
1e4d1764 1299 PACKET_InstallInTrace,
4082afcc 1300
40ab02ce
MS
1301 PACKET_bc,
1302 PACKET_bs,
409873ef 1303 PACKET_TracepointSource,
d914c394 1304 PACKET_QAllow,
78d85199 1305 PACKET_qXfer_fdpic,
03583c20 1306 PACKET_QDisableRandomization,
d1feda86 1307 PACKET_QAgent,
f6f899bf 1308 PACKET_QTBuffer_size,
9accd112
MM
1309 PACKET_Qbtrace_off,
1310 PACKET_Qbtrace_bts,
1311 PACKET_qXfer_btrace,
4082afcc
PA
1312
1313 /* Support for the QNonStop packet. */
1314 PACKET_QNonStop,
1315
1316 /* Support for multi-process extensions. */
1317 PACKET_multiprocess_feature,
1318
1319 /* Support for enabling and disabling tracepoints while a trace
1320 experiment is running. */
1321 PACKET_EnableDisableTracepoints_feature,
1322
1323 /* Support for collecting strings using the tracenz bytecode. */
1324 PACKET_tracenz_feature,
1325
1326 /* Support for continuing to run a trace experiment while GDB is
1327 disconnected. */
1328 PACKET_DisconnectedTracing_feature,
1329
1330 /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
1331 PACKET_augmented_libraries_svr4_read_feature,
1332
f4abbc16
MM
1333 /* Support for the qXfer:btrace-conf:read packet. */
1334 PACKET_qXfer_btrace_conf,
1335
d33501a5
MM
1336 /* Support for the Qbtrace-conf:bts:size packet. */
1337 PACKET_Qbtrace_conf_bts_size,
1338
f7e6eed5
PA
1339 /* Support for swbreak+ feature. */
1340 PACKET_swbreak_feature,
1341
1342 /* Support for hwbreak+ feature. */
1343 PACKET_hwbreak_feature,
1344
89245bc0
DB
1345 /* Support for fork events. */
1346 PACKET_fork_event_feature,
1347
1348 /* Support for vfork events. */
1349 PACKET_vfork_event_feature,
1350
444abaca
DJ
1351 PACKET_MAX
1352};
506fb367 1353
444abaca 1354static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97 1355
f7e6eed5
PA
1356/* Returns the packet's corresponding "set remote foo-packet" command
1357 state. See struct packet_config for more details. */
1358
1359static enum auto_boolean
1360packet_set_cmd_state (int packet)
1361{
1362 return remote_protocol_packets[packet].detect;
1363}
1364
4082afcc
PA
1365/* Returns whether a given packet or feature is supported. This takes
1366 into account the state of the corresponding "set remote foo-packet"
1367 command, which may be used to bypass auto-detection. */
dc8acb97 1368
4082afcc
PA
1369static enum packet_support
1370packet_config_support (struct packet_config *config)
1371{
1372 switch (config->detect)
444abaca 1373 {
4082afcc
PA
1374 case AUTO_BOOLEAN_TRUE:
1375 return PACKET_ENABLE;
1376 case AUTO_BOOLEAN_FALSE:
1377 return PACKET_DISABLE;
1378 case AUTO_BOOLEAN_AUTO:
1379 return config->support;
1380 default:
1381 gdb_assert_not_reached (_("bad switch"));
444abaca 1382 }
4082afcc
PA
1383}
1384
1385/* Same as packet_config_support, but takes the packet's enum value as
1386 argument. */
1387
1388static enum packet_support
1389packet_support (int packet)
1390{
1391 struct packet_config *config = &remote_protocol_packets[packet];
1392
1393 return packet_config_support (config);
dc8acb97
MS
1394}
1395
5a2468f5 1396static void
444abaca
DJ
1397show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
1398 struct cmd_list_element *c,
1399 const char *value)
5a2468f5 1400{
444abaca 1401 struct packet_config *packet;
5a2468f5 1402
444abaca
DJ
1403 for (packet = remote_protocol_packets;
1404 packet < &remote_protocol_packets[PACKET_MAX];
1405 packet++)
1406 {
1407 if (&packet->detect == c->var)
1408 {
1409 show_packet_config_cmd (packet);
1410 return;
1411 }
1412 }
9b20d036 1413 internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
444abaca 1414 c->name);
5a2468f5
JM
1415}
1416
d471ea57
AC
1417/* Should we try one of the 'Z' requests? */
1418
1419enum Z_packet_type
1420{
1421 Z_PACKET_SOFTWARE_BP,
1422 Z_PACKET_HARDWARE_BP,
1423 Z_PACKET_WRITE_WP,
1424 Z_PACKET_READ_WP,
1425 Z_PACKET_ACCESS_WP,
1426 NR_Z_PACKET_TYPES
1427};
96baa820 1428
d471ea57 1429/* For compatibility with older distributions. Provide a ``set remote
23860348 1430 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 1431
7f19b9a2 1432static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
1433
1434static void
fba45db2
KB
1435set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
1436 struct cmd_list_element *c)
96baa820 1437{
d471ea57 1438 int i;
a744cf53 1439
d471ea57 1440 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
4082afcc 1441 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
96baa820
JM
1442}
1443
1444static void
08546159
AC
1445show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
1446 struct cmd_list_element *c,
1447 const char *value)
96baa820 1448{
d471ea57 1449 int i;
a744cf53 1450
d471ea57
AC
1451 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
1452 {
444abaca 1453 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 1454 }
96baa820
JM
1455}
1456
4082afcc
PA
1457/* Returns true if the multi-process extensions are in effect. */
1458
1459static int
1460remote_multi_process_p (struct remote_state *rs)
1461{
1462 return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
1463}
1464
23860348 1465/* Tokens for use by the asynchronous signal handlers for SIGINT. */
934b9bac
JK
1466static struct async_signal_handler *async_sigint_remote_twice_token;
1467static struct async_signal_handler *async_sigint_remote_token;
43ff13b4 1468
74531fed
PA
1469\f
1470/* Asynchronous signal handle registered as event loop source for
1471 when we have pending events ready to be passed to the core. */
1472
1473static struct async_event_handler *remote_async_inferior_event_token;
1474
c906108c
SS
1475\f
1476
79d7f229
PA
1477static ptid_t magic_null_ptid;
1478static ptid_t not_sent_ptid;
1479static ptid_t any_thread_ptid;
1480
0b16c5cf
PA
1481/* Find out if the stub attached to PID (and hence GDB should offer to
1482 detach instead of killing it when bailing out). */
1483
1484static int
1485remote_query_attached (int pid)
1486{
1487 struct remote_state *rs = get_remote_state ();
bba74b36 1488 size_t size = get_remote_packet_size ();
0b16c5cf 1489
4082afcc 1490 if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
0b16c5cf
PA
1491 return 0;
1492
1493 if (remote_multi_process_p (rs))
bba74b36 1494 xsnprintf (rs->buf, size, "qAttached:%x", pid);
0b16c5cf 1495 else
bba74b36 1496 xsnprintf (rs->buf, size, "qAttached");
0b16c5cf
PA
1497
1498 putpkt (rs->buf);
1499 getpkt (&rs->buf, &rs->buf_size, 0);
1500
1501 switch (packet_ok (rs->buf,
1554e9be 1502 &remote_protocol_packets[PACKET_qAttached]))
0b16c5cf
PA
1503 {
1504 case PACKET_OK:
1505 if (strcmp (rs->buf, "1") == 0)
1506 return 1;
1507 break;
1508 case PACKET_ERROR:
1509 warning (_("Remote failure reply: %s"), rs->buf);
1510 break;
1511 case PACKET_UNKNOWN:
1512 break;
1513 }
1514
1515 return 0;
1516}
1517
49c62f2e
PA
1518/* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1519 has been invented by GDB, instead of reported by the target. Since
1520 we can be connected to a remote system before before knowing about
1521 any inferior, mark the target with execution when we find the first
1522 inferior. If ATTACHED is 1, then we had just attached to this
1523 inferior. If it is 0, then we just created this inferior. If it
1524 is -1, then try querying the remote stub to find out if it had
1b6e6f5c
GB
1525 attached to the inferior or not. If TRY_OPEN_EXEC is true then
1526 attempt to open this inferior's executable as the main executable
1527 if no main executable is open already. */
1941c569
PA
1528
1529static struct inferior *
1b6e6f5c
GB
1530remote_add_inferior (int fake_pid_p, int pid, int attached,
1531 int try_open_exec)
1941c569 1532{
1941c569
PA
1533 struct inferior *inf;
1534
0b16c5cf
PA
1535 /* Check whether this process we're learning about is to be
1536 considered attached, or if is to be considered to have been
1537 spawned by the stub. */
1538 if (attached == -1)
1539 attached = remote_query_attached (pid);
1540
f5656ead 1541 if (gdbarch_has_global_solist (target_gdbarch ()))
6c95b8df
PA
1542 {
1543 /* If the target shares code across all inferiors, then every
1544 attach adds a new inferior. */
1545 inf = add_inferior (pid);
1546
1547 /* ... and every inferior is bound to the same program space.
1548 However, each inferior may still have its own address
1549 space. */
1550 inf->aspace = maybe_new_address_space ();
1551 inf->pspace = current_program_space;
1552 }
1553 else
1554 {
1555 /* In the traditional debugging scenario, there's a 1-1 match
1556 between program/address spaces. We simply bind the inferior
1557 to the program space's address space. */
1558 inf = current_inferior ();
1559 inferior_appeared (inf, pid);
1560 }
1941c569 1561
0b16c5cf 1562 inf->attach_flag = attached;
49c62f2e 1563 inf->fake_pid_p = fake_pid_p;
0b16c5cf 1564
1b6e6f5c
GB
1565 /* If no main executable is currently open then attempt to
1566 open the file that was executed to create this inferior. */
835205d0 1567 if (try_open_exec && get_exec_file (0) == NULL)
1b6e6f5c
GB
1568 exec_file_locate_attach (pid, 1);
1569
1941c569
PA
1570 return inf;
1571}
1572
1573/* Add thread PTID to GDB's thread list. Tag it as executing/running
1574 according to RUNNING. */
1575
c906108c 1576static void
1941c569 1577remote_add_thread (ptid_t ptid, int running)
c906108c 1578{
b7ea362b
PA
1579 struct remote_state *rs = get_remote_state ();
1580
1581 /* GDB historically didn't pull threads in the initial connection
1582 setup. If the remote target doesn't even have a concept of
1583 threads (e.g., a bare-metal target), even if internally we
1584 consider that a single-threaded target, mentioning a new thread
1585 might be confusing to the user. Be silent then, preserving the
1586 age old behavior. */
1587 if (rs->starting_up)
1588 add_thread_silent (ptid);
1589 else
1590 add_thread (ptid);
1941c569
PA
1591
1592 set_executing (ptid, running);
1593 set_running (ptid, running);
1594}
1595
1596/* Come here when we learn about a thread id from the remote target.
1597 It may be the first time we hear about such thread, so take the
1598 opportunity to add it to GDB's thread list. In case this is the
1599 first time we're noticing its corresponding inferior, add it to
1600 GDB's inferior list as well. */
1601
1602static void
1603remote_notice_new_inferior (ptid_t currthread, int running)
1604{
c906108c
SS
1605 /* If this is a new thread, add it to GDB's thread list.
1606 If we leave it up to WFI to do this, bad things will happen. */
82f73884
PA
1607
1608 if (in_thread_list (currthread) && is_exited (currthread))
1609 {
1610 /* We're seeing an event on a thread id we knew had exited.
1611 This has to be a new thread reusing the old id. Add it. */
1941c569 1612 remote_add_thread (currthread, running);
82f73884
PA
1613 return;
1614 }
1615
79d7f229 1616 if (!in_thread_list (currthread))
c0a2216e 1617 {
1941c569 1618 struct inferior *inf = NULL;
bad34192 1619 int pid = ptid_get_pid (currthread);
1941c569 1620
bad34192
PA
1621 if (ptid_is_pid (inferior_ptid)
1622 && pid == ptid_get_pid (inferior_ptid))
c0a2216e
PA
1623 {
1624 /* inferior_ptid has no thread member yet. This can happen
1625 with the vAttach -> remote_wait,"TAAthread:" path if the
1626 stub doesn't support qC. This is the first stop reported
1627 after an attach, so this is the main thread. Update the
1628 ptid in the thread list. */
bad34192
PA
1629 if (in_thread_list (pid_to_ptid (pid)))
1630 thread_change_ptid (inferior_ptid, currthread);
1631 else
1632 {
1633 remote_add_thread (currthread, running);
1634 inferior_ptid = currthread;
1635 }
dc146f7c 1636 return;
c0a2216e 1637 }
82f73884
PA
1638
1639 if (ptid_equal (magic_null_ptid, inferior_ptid))
c0a2216e
PA
1640 {
1641 /* inferior_ptid is not set yet. This can happen with the
1642 vRun -> remote_wait,"TAAthread:" path if the stub
1643 doesn't support qC. This is the first stop reported
1644 after an attach, so this is the main thread. Update the
1645 ptid in the thread list. */
dc146f7c 1646 thread_change_ptid (inferior_ptid, currthread);
82f73884 1647 return;
c0a2216e 1648 }
82f73884 1649
29c87f7f
PA
1650 /* When connecting to a target remote, or to a target
1651 extended-remote which already was debugging an inferior, we
1652 may not know about it yet. Add it before adding its child
1653 thread, so notifications are emitted in a sensible order. */
1654 if (!in_inferior_list (ptid_get_pid (currthread)))
49c62f2e
PA
1655 {
1656 struct remote_state *rs = get_remote_state ();
1657 int fake_pid_p = !remote_multi_process_p (rs);
1658
1659 inf = remote_add_inferior (fake_pid_p,
1b6e6f5c 1660 ptid_get_pid (currthread), -1, 1);
49c62f2e 1661 }
29c87f7f 1662
82f73884 1663 /* This is really a new thread. Add it. */
1941c569
PA
1664 remote_add_thread (currthread, running);
1665
1666 /* If we found a new inferior, let the common code do whatever
1667 it needs to with it (e.g., read shared libraries, insert
b7ea362b
PA
1668 breakpoints), unless we're just setting up an all-stop
1669 connection. */
1941c569 1670 if (inf != NULL)
b7ea362b
PA
1671 {
1672 struct remote_state *rs = get_remote_state ();
1673
1674 if (non_stop || !rs->starting_up)
1675 notice_new_inferior (currthread, running, 0);
1676 }
c0a2216e 1677 }
c906108c
SS
1678}
1679
dc146f7c
VP
1680/* Return the private thread data, creating it if necessary. */
1681
70221824 1682static struct private_thread_info *
dc146f7c
VP
1683demand_private_info (ptid_t ptid)
1684{
1685 struct thread_info *info = find_thread_ptid (ptid);
1686
1687 gdb_assert (info);
1688
fe978cb0 1689 if (!info->priv)
dc146f7c 1690 {
fe978cb0 1691 info->priv = xmalloc (sizeof (*(info->priv)));
dc146f7c 1692 info->private_dtor = free_private_thread_info;
fe978cb0
PA
1693 info->priv->core = -1;
1694 info->priv->extra = 0;
dc146f7c
VP
1695 }
1696
fe978cb0 1697 return info->priv;
dc146f7c
VP
1698}
1699
74531fed
PA
1700/* Call this function as a result of
1701 1) A halt indication (T packet) containing a thread id
1702 2) A direct query of currthread
0df8b418 1703 3) Successful execution of set thread */
74531fed
PA
1704
1705static void
47f8a51d 1706record_currthread (struct remote_state *rs, ptid_t currthread)
74531fed 1707{
47f8a51d 1708 rs->general_thread = currthread;
74531fed
PA
1709}
1710
89be2091
DJ
1711/* If 'QPassSignals' is supported, tell the remote stub what signals
1712 it can simply pass through to the inferior without reporting. */
1713
1714static void
94bedb42
TT
1715remote_pass_signals (struct target_ops *self,
1716 int numsigs, unsigned char *pass_signals)
89be2091 1717{
4082afcc 1718 if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
89be2091
DJ
1719 {
1720 char *pass_packet, *p;
89be2091 1721 int count = 0, i;
747dc59d 1722 struct remote_state *rs = get_remote_state ();
89be2091
DJ
1723
1724 gdb_assert (numsigs < 256);
1725 for (i = 0; i < numsigs; i++)
1726 {
2455069d 1727 if (pass_signals[i])
89be2091
DJ
1728 count++;
1729 }
1730 pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
1731 strcpy (pass_packet, "QPassSignals:");
1732 p = pass_packet + strlen (pass_packet);
1733 for (i = 0; i < numsigs; i++)
1734 {
2455069d 1735 if (pass_signals[i])
89be2091
DJ
1736 {
1737 if (i >= 16)
1738 *p++ = tohex (i >> 4);
1739 *p++ = tohex (i & 15);
1740 if (count)
1741 *p++ = ';';
1742 else
1743 break;
1744 count--;
1745 }
1746 }
1747 *p = 0;
747dc59d 1748 if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
89be2091 1749 {
89be2091
DJ
1750 putpkt (pass_packet);
1751 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1752 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
747dc59d
TT
1753 if (rs->last_pass_packet)
1754 xfree (rs->last_pass_packet);
1755 rs->last_pass_packet = pass_packet;
89be2091
DJ
1756 }
1757 else
1758 xfree (pass_packet);
1759 }
1760}
1761
9b224c5e
PA
1762/* If 'QProgramSignals' is supported, tell the remote stub what
1763 signals it should pass through to the inferior when detaching. */
1764
1765static void
daf5e9b6
TT
1766remote_program_signals (struct target_ops *self,
1767 int numsigs, unsigned char *signals)
9b224c5e 1768{
4082afcc 1769 if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
9b224c5e
PA
1770 {
1771 char *packet, *p;
1772 int count = 0, i;
5e4a05c4 1773 struct remote_state *rs = get_remote_state ();
9b224c5e
PA
1774
1775 gdb_assert (numsigs < 256);
1776 for (i = 0; i < numsigs; i++)
1777 {
1778 if (signals[i])
1779 count++;
1780 }
1781 packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1782 strcpy (packet, "QProgramSignals:");
1783 p = packet + strlen (packet);
1784 for (i = 0; i < numsigs; i++)
1785 {
1786 if (signal_pass_state (i))
1787 {
1788 if (i >= 16)
1789 *p++ = tohex (i >> 4);
1790 *p++ = tohex (i & 15);
1791 if (count)
1792 *p++ = ';';
1793 else
1794 break;
1795 count--;
1796 }
1797 }
1798 *p = 0;
5e4a05c4
TT
1799 if (!rs->last_program_signals_packet
1800 || strcmp (rs->last_program_signals_packet, packet) != 0)
9b224c5e 1801 {
9b224c5e
PA
1802 putpkt (packet);
1803 getpkt (&rs->buf, &rs->buf_size, 0);
8dc5b319 1804 packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
5e4a05c4
TT
1805 xfree (rs->last_program_signals_packet);
1806 rs->last_program_signals_packet = packet;
9b224c5e
PA
1807 }
1808 else
1809 xfree (packet);
1810 }
1811}
1812
79d7f229
PA
1813/* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1814 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1815 thread. If GEN is set, set the general thread, if not, then set
1816 the step/continue thread. */
c906108c 1817static void
79d7f229 1818set_thread (struct ptid ptid, int gen)
c906108c 1819{
d01949b6 1820 struct remote_state *rs = get_remote_state ();
47f8a51d 1821 ptid_t state = gen ? rs->general_thread : rs->continue_thread;
6d820c5c 1822 char *buf = rs->buf;
79d7f229 1823 char *endbuf = rs->buf + get_remote_packet_size ();
c906108c 1824
79d7f229 1825 if (ptid_equal (state, ptid))
c906108c
SS
1826 return;
1827
79d7f229
PA
1828 *buf++ = 'H';
1829 *buf++ = gen ? 'g' : 'c';
1830 if (ptid_equal (ptid, magic_null_ptid))
1831 xsnprintf (buf, endbuf - buf, "0");
1832 else if (ptid_equal (ptid, any_thread_ptid))
1833 xsnprintf (buf, endbuf - buf, "0");
1834 else if (ptid_equal (ptid, minus_one_ptid))
1835 xsnprintf (buf, endbuf - buf, "-1");
1836 else
82f73884 1837 write_ptid (buf, endbuf, ptid);
79d7f229 1838 putpkt (rs->buf);
6d820c5c 1839 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 1840 if (gen)
47f8a51d 1841 rs->general_thread = ptid;
c906108c 1842 else
47f8a51d 1843 rs->continue_thread = ptid;
c906108c 1844}
79d7f229
PA
1845
1846static void
1847set_general_thread (struct ptid ptid)
1848{
1849 set_thread (ptid, 1);
1850}
1851
1852static void
1853set_continue_thread (struct ptid ptid)
1854{
1855 set_thread (ptid, 0);
1856}
1857
3c9c4b83
PA
1858/* Change the remote current process. Which thread within the process
1859 ends up selected isn't important, as long as it is the same process
1860 as what INFERIOR_PTID points to.
1861
1862 This comes from that fact that there is no explicit notion of
1863 "selected process" in the protocol. The selected process for
1864 general operations is the process the selected general thread
1865 belongs to. */
1866
1867static void
1868set_general_process (void)
1869{
1870 struct remote_state *rs = get_remote_state ();
1871
1872 /* If the remote can't handle multiple processes, don't bother. */
901f9912 1873 if (!rs->extended || !remote_multi_process_p (rs))
3c9c4b83
PA
1874 return;
1875
1876 /* We only need to change the remote current thread if it's pointing
1877 at some other process. */
47f8a51d 1878 if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid))
3c9c4b83
PA
1879 set_general_thread (inferior_ptid);
1880}
1881
c906108c 1882\f
7d1a114c
PA
1883/* Return nonzero if this is the main thread that we made up ourselves
1884 to model non-threaded targets as single-threaded. */
c906108c
SS
1885
1886static int
7d1a114c 1887remote_thread_always_alive (struct target_ops *ops, ptid_t ptid)
c906108c 1888{
6d820c5c 1889 struct remote_state *rs = get_remote_state ();
82f73884 1890 char *p, *endp;
c906108c 1891
c0a2216e
PA
1892 if (ptid_equal (ptid, magic_null_ptid))
1893 /* The main thread is always alive. */
1894 return 1;
1895
ba348170 1896 if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0)
c0a2216e
PA
1897 /* The main thread is always alive. This can happen after a
1898 vAttach, if the remote side doesn't support
1899 multi-threading. */
1900 return 1;
1901
7d1a114c
PA
1902 return 0;
1903}
1904
1905/* Return nonzero if the thread PTID is still alive on the remote
1906 system. */
1907
1908static int
1909remote_thread_alive (struct target_ops *ops, ptid_t ptid)
1910{
1911 struct remote_state *rs = get_remote_state ();
1912 char *p, *endp;
1913
1914 /* Check if this is a thread that we made up ourselves to model
1915 non-threaded targets as single-threaded. */
1916 if (remote_thread_always_alive (ops, ptid))
1917 return 1;
1918
82f73884
PA
1919 p = rs->buf;
1920 endp = rs->buf + get_remote_packet_size ();
1921
1922 *p++ = 'T';
1923 write_ptid (p, endp, ptid);
1924
2e9f7625 1925 putpkt (rs->buf);
6d820c5c 1926 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 1927 return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
c906108c
SS
1928}
1929
1930/* About these extended threadlist and threadinfo packets. They are
1931 variable length packets but, the fields within them are often fixed
1932 length. They are redundent enough to send over UDP as is the
1933 remote protocol in general. There is a matching unit test module
1934 in libstub. */
1935
23860348 1936/* WARNING: This threadref data structure comes from the remote O.S.,
0df8b418 1937 libstub protocol encoding, and remote.c. It is not particularly
23860348 1938 changable. */
cce74817
JM
1939
1940/* Right now, the internal structure is int. We want it to be bigger.
0df8b418 1941 Plan to fix this. */
cce74817 1942
23860348 1943typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 1944
9d1f7ab2 1945/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 1946 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
1947
1948struct gdb_ext_thread_info
c5aa993b 1949 {
23860348 1950 threadref threadid; /* External form of thread reference. */
2bc416ba 1951 int active; /* Has state interesting to GDB?
23860348 1952 regs, stack. */
2bc416ba 1953 char display[256]; /* Brief state display, name,
cedea757 1954 blocked/suspended. */
23860348 1955 char shortname[32]; /* To be used to name threads. */
2bc416ba 1956 char more_display[256]; /* Long info, statistics, queue depth,
23860348 1957 whatever. */
c5aa993b 1958 };
cce74817
JM
1959
1960/* The volume of remote transfers can be limited by submitting
1961 a mask containing bits specifying the desired information.
1962 Use a union of these values as the 'selection' parameter to
0df8b418 1963 get_thread_info. FIXME: Make these TAG names more thread specific. */
cce74817
JM
1964
1965#define TAG_THREADID 1
1966#define TAG_EXISTS 2
1967#define TAG_DISPLAY 4
1968#define TAG_THREADNAME 8
c5aa993b 1969#define TAG_MOREDISPLAY 16
cce74817 1970
23860348 1971#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 1972
a14ed312 1973static char *unpack_nibble (char *buf, int *val);
cce74817 1974
a14ed312 1975static char *unpack_byte (char *buf, int *value);
cce74817 1976
a14ed312 1977static char *pack_int (char *buf, int value);
cce74817 1978
a14ed312 1979static char *unpack_int (char *buf, int *value);
cce74817 1980
a14ed312 1981static char *unpack_string (char *src, char *dest, int length);
cce74817 1982
23860348 1983static char *pack_threadid (char *pkt, threadref *id);
cce74817 1984
23860348 1985static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 1986
23860348 1987void int_to_threadref (threadref *id, int value);
cce74817 1988
23860348 1989static int threadref_to_int (threadref *ref);
cce74817 1990
23860348 1991static void copy_threadref (threadref *dest, threadref *src);
cce74817 1992
23860348 1993static int threadmatch (threadref *dest, threadref *src);
cce74817 1994
2bc416ba 1995static char *pack_threadinfo_request (char *pkt, int mode,
23860348 1996 threadref *id);
cce74817 1997
a14ed312 1998static int remote_unpack_thread_info_response (char *pkt,
23860348 1999 threadref *expectedref,
a14ed312
KB
2000 struct gdb_ext_thread_info
2001 *info);
cce74817
JM
2002
2003
2bc416ba 2004static int remote_get_threadinfo (threadref *threadid,
23860348 2005 int fieldset, /*TAG mask */
a14ed312 2006 struct gdb_ext_thread_info *info);
cce74817 2007
a14ed312
KB
2008static char *pack_threadlist_request (char *pkt, int startflag,
2009 int threadcount,
23860348 2010 threadref *nextthread);
cce74817 2011
a14ed312
KB
2012static int parse_threadlist_response (char *pkt,
2013 int result_limit,
23860348 2014 threadref *original_echo,
2bc416ba 2015 threadref *resultlist,
23860348 2016 int *doneflag);
cce74817 2017
a14ed312 2018static int remote_get_threadlist (int startflag,
23860348 2019 threadref *nextthread,
a14ed312
KB
2020 int result_limit,
2021 int *done,
2bc416ba 2022 int *result_count,
23860348 2023 threadref *threadlist);
cce74817 2024
23860348 2025typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 2026
a14ed312
KB
2027static int remote_threadlist_iterator (rmt_thread_action stepfunction,
2028 void *context, int looplimit);
cce74817 2029
23860348 2030static int remote_newthread_step (threadref *ref, void *context);
cce74817 2031
82f73884
PA
2032
2033/* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2034 buffer we're allowed to write to. Returns
2035 BUF+CHARACTERS_WRITTEN. */
2036
2037static char *
2038write_ptid (char *buf, const char *endbuf, ptid_t ptid)
2039{
2040 int pid, tid;
2041 struct remote_state *rs = get_remote_state ();
2042
2043 if (remote_multi_process_p (rs))
2044 {
2045 pid = ptid_get_pid (ptid);
2046 if (pid < 0)
2047 buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
2048 else
2049 buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
2050 }
ba348170 2051 tid = ptid_get_lwp (ptid);
82f73884
PA
2052 if (tid < 0)
2053 buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
2054 else
2055 buf += xsnprintf (buf, endbuf - buf, "%x", tid);
2056
2057 return buf;
2058}
2059
2060/* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2061 passed the last parsed char. Returns null_ptid on error. */
2062
2063static ptid_t
2064read_ptid (char *buf, char **obuf)
2065{
2066 char *p = buf;
2067 char *pp;
2068 ULONGEST pid = 0, tid = 0;
82f73884
PA
2069
2070 if (*p == 'p')
2071 {
2072 /* Multi-process ptid. */
2073 pp = unpack_varlen_hex (p + 1, &pid);
2074 if (*pp != '.')
b37520b6 2075 error (_("invalid remote ptid: %s"), p);
82f73884
PA
2076
2077 p = pp;
2078 pp = unpack_varlen_hex (p + 1, &tid);
2079 if (obuf)
2080 *obuf = pp;
ba348170 2081 return ptid_build (pid, tid, 0);
82f73884
PA
2082 }
2083
2084 /* No multi-process. Just a tid. */
2085 pp = unpack_varlen_hex (p, &tid);
2086
2087 /* Since the stub is not sending a process id, then default to
ca19bf23
PA
2088 what's in inferior_ptid, unless it's null at this point. If so,
2089 then since there's no way to know the pid of the reported
2090 threads, use the magic number. */
2091 if (ptid_equal (inferior_ptid, null_ptid))
2092 pid = ptid_get_pid (magic_null_ptid);
2093 else
2094 pid = ptid_get_pid (inferior_ptid);
82f73884
PA
2095
2096 if (obuf)
2097 *obuf = pp;
ba348170 2098 return ptid_build (pid, tid, 0);
82f73884
PA
2099}
2100
c906108c 2101static int
fba45db2 2102stubhex (int ch)
c906108c
SS
2103{
2104 if (ch >= 'a' && ch <= 'f')
2105 return ch - 'a' + 10;
2106 if (ch >= '0' && ch <= '9')
2107 return ch - '0';
2108 if (ch >= 'A' && ch <= 'F')
2109 return ch - 'A' + 10;
2110 return -1;
2111}
2112
2113static int
fba45db2 2114stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
2115{
2116 int nibble;
2117 int retval = 0;
2118
2119 while (fieldlength)
2120 {
2121 nibble = stubhex (*buff++);
2122 retval |= nibble;
2123 fieldlength--;
2124 if (fieldlength)
2125 retval = retval << 4;
2126 }
2127 return retval;
2128}
2129
c906108c 2130static char *
fba45db2 2131unpack_nibble (char *buf, int *val)
c906108c 2132{
b7589f7d 2133 *val = fromhex (*buf++);
c906108c
SS
2134 return buf;
2135}
2136
c906108c 2137static char *
fba45db2 2138unpack_byte (char *buf, int *value)
c906108c
SS
2139{
2140 *value = stub_unpack_int (buf, 2);
2141 return buf + 2;
2142}
2143
2144static char *
fba45db2 2145pack_int (char *buf, int value)
c906108c
SS
2146{
2147 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
2148 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
2149 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
2150 buf = pack_hex_byte (buf, (value & 0xff));
2151 return buf;
2152}
2153
2154static char *
fba45db2 2155unpack_int (char *buf, int *value)
c906108c
SS
2156{
2157 *value = stub_unpack_int (buf, 8);
2158 return buf + 8;
2159}
2160
23860348 2161#if 0 /* Currently unused, uncomment when needed. */
a14ed312 2162static char *pack_string (char *pkt, char *string);
c906108c
SS
2163
2164static char *
fba45db2 2165pack_string (char *pkt, char *string)
c906108c
SS
2166{
2167 char ch;
2168 int len;
2169
2170 len = strlen (string);
2171 if (len > 200)
23860348 2172 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
2173 pkt = pack_hex_byte (pkt, len);
2174 while (len-- > 0)
2175 {
2176 ch = *string++;
2177 if ((ch == '\0') || (ch == '#'))
23860348 2178 ch = '*'; /* Protect encapsulation. */
c906108c
SS
2179 *pkt++ = ch;
2180 }
2181 return pkt;
2182}
2183#endif /* 0 (unused) */
2184
2185static char *
fba45db2 2186unpack_string (char *src, char *dest, int length)
c906108c
SS
2187{
2188 while (length--)
2189 *dest++ = *src++;
2190 *dest = '\0';
2191 return src;
2192}
2193
2194static char *
fba45db2 2195pack_threadid (char *pkt, threadref *id)
c906108c
SS
2196{
2197 char *limit;
2198 unsigned char *altid;
2199
2200 altid = (unsigned char *) id;
2201 limit = pkt + BUF_THREAD_ID_SIZE;
2202 while (pkt < limit)
2203 pkt = pack_hex_byte (pkt, *altid++);
2204 return pkt;
2205}
2206
2207
2208static char *
fba45db2 2209unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
2210{
2211 char *altref;
2212 char *limit = inbuf + BUF_THREAD_ID_SIZE;
2213 int x, y;
2214
2215 altref = (char *) id;
2216
2217 while (inbuf < limit)
2218 {
2219 x = stubhex (*inbuf++);
2220 y = stubhex (*inbuf++);
2221 *altref++ = (x << 4) | y;
2222 }
2223 return inbuf;
2224}
2225
2226/* Externally, threadrefs are 64 bits but internally, they are still
0df8b418 2227 ints. This is due to a mismatch of specifications. We would like
c906108c
SS
2228 to use 64bit thread references internally. This is an adapter
2229 function. */
2230
2231void
fba45db2 2232int_to_threadref (threadref *id, int value)
c906108c
SS
2233{
2234 unsigned char *scan;
2235
2236 scan = (unsigned char *) id;
2237 {
2238 int i = 4;
2239 while (i--)
2240 *scan++ = 0;
2241 }
2242 *scan++ = (value >> 24) & 0xff;
2243 *scan++ = (value >> 16) & 0xff;
2244 *scan++ = (value >> 8) & 0xff;
2245 *scan++ = (value & 0xff);
2246}
2247
2248static int
fba45db2 2249threadref_to_int (threadref *ref)
c906108c
SS
2250{
2251 int i, value = 0;
2252 unsigned char *scan;
2253
cfd77fa1 2254 scan = *ref;
c906108c
SS
2255 scan += 4;
2256 i = 4;
2257 while (i-- > 0)
2258 value = (value << 8) | ((*scan++) & 0xff);
2259 return value;
2260}
2261
2262static void
fba45db2 2263copy_threadref (threadref *dest, threadref *src)
c906108c
SS
2264{
2265 int i;
2266 unsigned char *csrc, *cdest;
2267
2268 csrc = (unsigned char *) src;
2269 cdest = (unsigned char *) dest;
2270 i = 8;
2271 while (i--)
2272 *cdest++ = *csrc++;
2273}
2274
2275static int
fba45db2 2276threadmatch (threadref *dest, threadref *src)
c906108c 2277{
23860348 2278 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
2279#if 0
2280 unsigned char *srcp, *destp;
2281 int i, result;
2282 srcp = (char *) src;
2283 destp = (char *) dest;
2284
2285 result = 1;
2286 while (i-- > 0)
2287 result &= (*srcp++ == *destp++) ? 1 : 0;
2288 return result;
2289#endif
2290 return 1;
2291}
2292
2293/*
c5aa993b
JM
2294 threadid:1, # always request threadid
2295 context_exists:2,
2296 display:4,
2297 unique_name:8,
2298 more_display:16
2299 */
c906108c
SS
2300
2301/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2302
2303static char *
fba45db2 2304pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 2305{
23860348
MS
2306 *pkt++ = 'q'; /* Info Query */
2307 *pkt++ = 'P'; /* process or thread info */
2308 pkt = pack_int (pkt, mode); /* mode */
c906108c 2309 pkt = pack_threadid (pkt, id); /* threadid */
23860348 2310 *pkt = '\0'; /* terminate */
c906108c
SS
2311 return pkt;
2312}
2313
23860348 2314/* These values tag the fields in a thread info response packet. */
c906108c 2315/* Tagging the fields allows us to request specific fields and to
23860348 2316 add more fields as time goes by. */
c906108c 2317
23860348 2318#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 2319#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 2320 fetch registers and its stack? */
c5aa993b 2321#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 2322#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 2323#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 2324 the process. */
c906108c
SS
2325
2326static int
fba45db2
KB
2327remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
2328 struct gdb_ext_thread_info *info)
c906108c 2329{
d01949b6 2330 struct remote_state *rs = get_remote_state ();
c906108c 2331 int mask, length;
cfd77fa1 2332 int tag;
c906108c 2333 threadref ref;
6d820c5c 2334 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
2335 int retval = 1;
2336
23860348 2337 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
2338 info->active = 0;
2339 info->display[0] = '\0';
2340 info->shortname[0] = '\0';
2341 info->more_display[0] = '\0';
2342
23860348
MS
2343 /* Assume the characters indicating the packet type have been
2344 stripped. */
c906108c
SS
2345 pkt = unpack_int (pkt, &mask); /* arg mask */
2346 pkt = unpack_threadid (pkt, &ref);
2347
2348 if (mask == 0)
8a3fe4f8 2349 warning (_("Incomplete response to threadinfo request."));
c906108c 2350 if (!threadmatch (&ref, expectedref))
23860348 2351 { /* This is an answer to a different request. */
8a3fe4f8 2352 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
2353 return 0;
2354 }
2355 copy_threadref (&info->threadid, &ref);
2356
23860348 2357 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 2358
23860348
MS
2359 /* Packets are terminated with nulls. */
2360 while ((pkt < limit) && mask && *pkt)
c906108c
SS
2361 {
2362 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
2363 pkt = unpack_byte (pkt, &length); /* length */
2364 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 2365 {
8a3fe4f8 2366 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
2367 retval = 0;
2368 break;
2369 }
2370 if (tag == TAG_THREADID)
2371 {
2372 if (length != 16)
2373 {
8a3fe4f8 2374 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
2375 retval = 0;
2376 break;
2377 }
2378 pkt = unpack_threadid (pkt, &ref);
2379 mask = mask & ~TAG_THREADID;
2380 continue;
2381 }
2382 if (tag == TAG_EXISTS)
2383 {
2384 info->active = stub_unpack_int (pkt, length);
2385 pkt += length;
2386 mask = mask & ~(TAG_EXISTS);
2387 if (length > 8)
2388 {
8a3fe4f8 2389 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
2390 retval = 0;
2391 break;
2392 }
2393 continue;
2394 }
2395 if (tag == TAG_THREADNAME)
2396 {
2397 pkt = unpack_string (pkt, &info->shortname[0], length);
2398 mask = mask & ~TAG_THREADNAME;
2399 continue;
2400 }
2401 if (tag == TAG_DISPLAY)
2402 {
2403 pkt = unpack_string (pkt, &info->display[0], length);
2404 mask = mask & ~TAG_DISPLAY;
2405 continue;
2406 }
2407 if (tag == TAG_MOREDISPLAY)
2408 {
2409 pkt = unpack_string (pkt, &info->more_display[0], length);
2410 mask = mask & ~TAG_MOREDISPLAY;
2411 continue;
2412 }
8a3fe4f8 2413 warning (_("ERROR RMT: unknown thread info tag."));
23860348 2414 break; /* Not a tag we know about. */
c906108c
SS
2415 }
2416 return retval;
2417}
2418
2419static int
fba45db2
KB
2420remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
2421 struct gdb_ext_thread_info *info)
c906108c 2422{
d01949b6 2423 struct remote_state *rs = get_remote_state ();
c906108c 2424 int result;
c906108c 2425
2e9f7625
DJ
2426 pack_threadinfo_request (rs->buf, fieldset, threadid);
2427 putpkt (rs->buf);
6d820c5c 2428 getpkt (&rs->buf, &rs->buf_size, 0);
3084dd77
PA
2429
2430 if (rs->buf[0] == '\0')
2431 return 0;
2432
2e9f7625 2433 result = remote_unpack_thread_info_response (rs->buf + 2,
23860348 2434 threadid, info);
c906108c
SS
2435 return result;
2436}
2437
c906108c
SS
2438/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2439
2440static char *
fba45db2
KB
2441pack_threadlist_request (char *pkt, int startflag, int threadcount,
2442 threadref *nextthread)
c906108c
SS
2443{
2444 *pkt++ = 'q'; /* info query packet */
2445 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 2446 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
2447 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
2448 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
2449 *pkt = '\0';
2450 return pkt;
2451}
2452
2453/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2454
2455static int
fba45db2
KB
2456parse_threadlist_response (char *pkt, int result_limit,
2457 threadref *original_echo, threadref *resultlist,
2458 int *doneflag)
c906108c 2459{
d01949b6 2460 struct remote_state *rs = get_remote_state ();
c906108c
SS
2461 char *limit;
2462 int count, resultcount, done;
2463
2464 resultcount = 0;
2465 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 2466 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 2467 /* done parse past here */
c906108c
SS
2468 pkt = unpack_byte (pkt, &count); /* count field */
2469 pkt = unpack_nibble (pkt, &done);
2470 /* The first threadid is the argument threadid. */
2471 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
2472 while ((count-- > 0) && (pkt < limit))
2473 {
2474 pkt = unpack_threadid (pkt, resultlist++);
2475 if (resultcount++ >= result_limit)
2476 break;
2477 }
2478 if (doneflag)
2479 *doneflag = done;
2480 return resultcount;
2481}
2482
6dc54d91
PA
2483/* Fetch the next batch of threads from the remote. Returns -1 if the
2484 qL packet is not supported, 0 on error and 1 on success. */
2485
c906108c 2486static int
fba45db2
KB
2487remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
2488 int *done, int *result_count, threadref *threadlist)
c906108c 2489{
d01949b6 2490 struct remote_state *rs = get_remote_state ();
c906108c
SS
2491 int result = 1;
2492
23860348 2493 /* Trancate result limit to be smaller than the packet size. */
3e43a32a
MS
2494 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2495 >= get_remote_packet_size ())
ea9c271d 2496 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 2497
6d820c5c
DJ
2498 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
2499 putpkt (rs->buf);
2500 getpkt (&rs->buf, &rs->buf_size, 0);
d8f2712d 2501 if (*rs->buf == '\0')
6dc54d91
PA
2502 {
2503 /* Packet not supported. */
2504 return -1;
2505 }
2506
2507 *result_count =
2508 parse_threadlist_response (rs->buf + 2, result_limit,
2509 &rs->echo_nextthread, threadlist, done);
c906108c 2510
0d031856 2511 if (!threadmatch (&rs->echo_nextthread, nextthread))
c906108c 2512 {
23860348
MS
2513 /* FIXME: This is a good reason to drop the packet. */
2514 /* Possably, there is a duplicate response. */
c906108c
SS
2515 /* Possabilities :
2516 retransmit immediatly - race conditions
2517 retransmit after timeout - yes
2518 exit
2519 wait for packet, then exit
2520 */
8a3fe4f8 2521 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 2522 return 0; /* I choose simply exiting. */
c906108c
SS
2523 }
2524 if (*result_count <= 0)
2525 {
2526 if (*done != 1)
2527 {
8a3fe4f8 2528 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
2529 result = 0;
2530 }
2531 return result; /* break; */
2532 }
2533 if (*result_count > result_limit)
2534 {
2535 *result_count = 0;
8a3fe4f8 2536 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
2537 return 0;
2538 }
2539 return result;
2540}
2541
6dc54d91
PA
2542/* Fetch the list of remote threads, with the qL packet, and call
2543 STEPFUNCTION for each thread found. Stops iterating and returns 1
2544 if STEPFUNCTION returns true. Stops iterating and returns 0 if the
2545 STEPFUNCTION returns false. If the packet is not supported,
2546 returns -1. */
c906108c 2547
c906108c 2548static int
fba45db2
KB
2549remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
2550 int looplimit)
c906108c 2551{
0d031856 2552 struct remote_state *rs = get_remote_state ();
c906108c
SS
2553 int done, i, result_count;
2554 int startflag = 1;
2555 int result = 1;
2556 int loopcount = 0;
c906108c
SS
2557
2558 done = 0;
2559 while (!done)
2560 {
2561 if (loopcount++ > looplimit)
2562 {
2563 result = 0;
8a3fe4f8 2564 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
2565 break;
2566 }
6dc54d91
PA
2567 result = remote_get_threadlist (startflag, &rs->nextthread,
2568 MAXTHREADLISTRESULTS,
2569 &done, &result_count,
2570 rs->resultthreadlist);
2571 if (result <= 0)
2572 break;
23860348 2573 /* Clear for later iterations. */
c906108c
SS
2574 startflag = 0;
2575 /* Setup to resume next batch of thread references, set nextthread. */
2576 if (result_count >= 1)
0d031856
TT
2577 copy_threadref (&rs->nextthread,
2578 &rs->resultthreadlist[result_count - 1]);
c906108c
SS
2579 i = 0;
2580 while (result_count--)
6dc54d91
PA
2581 {
2582 if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
2583 {
2584 result = 0;
2585 break;
2586 }
2587 }
c906108c
SS
2588 }
2589 return result;
2590}
2591
6dc54d91
PA
2592/* A thread found on the remote target. */
2593
2594typedef struct thread_item
2595{
2596 /* The thread's PTID. */
2597 ptid_t ptid;
2598
2599 /* The thread's extra info. May be NULL. */
2600 char *extra;
2601
2602 /* The core the thread was running on. -1 if not known. */
2603 int core;
2604} thread_item_t;
2605DEF_VEC_O(thread_item_t);
2606
2607/* Context passed around to the various methods listing remote
2608 threads. As new threads are found, they're added to the ITEMS
2609 vector. */
2610
2611struct threads_listing_context
2612{
2613 /* The threads found on the remote target. */
2614 VEC (thread_item_t) *items;
2615};
2616
80134cf5
PA
2617/* Discard the contents of the constructed thread listing context. */
2618
2619static void
2620clear_threads_listing_context (void *p)
2621{
2622 struct threads_listing_context *context = p;
2623 int i;
2624 struct thread_item *item;
2625
2626 for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2627 xfree (item->extra);
2628
2629 VEC_free (thread_item_t, context->items);
2630}
2631
c906108c 2632static int
6dc54d91 2633remote_newthread_step (threadref *ref, void *data)
c906108c 2634{
6dc54d91
PA
2635 struct threads_listing_context *context = data;
2636 struct thread_item item;
79d7f229 2637 int pid = ptid_get_pid (inferior_ptid);
39f77062 2638
6dc54d91
PA
2639 item.ptid = ptid_build (pid, threadref_to_int (ref), 0);
2640 item.core = -1;
2641 item.extra = NULL;
2642
2643 VEC_safe_push (thread_item_t, context->items, &item);
2644
c906108c
SS
2645 return 1; /* continue iterator */
2646}
2647
2648#define CRAZY_MAX_THREADS 1000
2649
39f77062
KB
2650static ptid_t
2651remote_current_thread (ptid_t oldpid)
c906108c 2652{
d01949b6 2653 struct remote_state *rs = get_remote_state ();
c906108c
SS
2654
2655 putpkt ("qC");
6d820c5c 2656 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2657 if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
82f73884 2658 return read_ptid (&rs->buf[2], NULL);
c906108c
SS
2659 else
2660 return oldpid;
2661}
2662
6dc54d91 2663/* List remote threads using the deprecated qL packet. */
cce74817 2664
6dc54d91
PA
2665static int
2666remote_get_threads_with_ql (struct target_ops *ops,
2667 struct threads_listing_context *context)
c906108c 2668{
6dc54d91
PA
2669 if (remote_threadlist_iterator (remote_newthread_step, context,
2670 CRAZY_MAX_THREADS) >= 0)
2671 return 1;
2672
2673 return 0;
c906108c
SS
2674}
2675
dc146f7c
VP
2676#if defined(HAVE_LIBEXPAT)
2677
dc146f7c
VP
2678static void
2679start_thread (struct gdb_xml_parser *parser,
2680 const struct gdb_xml_element *element,
2681 void *user_data, VEC(gdb_xml_value_s) *attributes)
2682{
6dc54d91 2683 struct threads_listing_context *data = user_data;
dc146f7c
VP
2684
2685 struct thread_item item;
2686 char *id;
3d2c1d41 2687 struct gdb_xml_value *attr;
dc146f7c 2688
3d2c1d41 2689 id = xml_find_attribute (attributes, "id")->value;
dc146f7c
VP
2690 item.ptid = read_ptid (id, NULL);
2691
3d2c1d41
PA
2692 attr = xml_find_attribute (attributes, "core");
2693 if (attr != NULL)
2694 item.core = *(ULONGEST *) attr->value;
dc146f7c
VP
2695 else
2696 item.core = -1;
2697
2698 item.extra = 0;
2699
2700 VEC_safe_push (thread_item_t, data->items, &item);
2701}
2702
2703static void
2704end_thread (struct gdb_xml_parser *parser,
2705 const struct gdb_xml_element *element,
2706 void *user_data, const char *body_text)
2707{
6dc54d91 2708 struct threads_listing_context *data = user_data;
dc146f7c
VP
2709
2710 if (body_text && *body_text)
2ae2a0b7 2711 VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
dc146f7c
VP
2712}
2713
2714const struct gdb_xml_attribute thread_attributes[] = {
2715 { "id", GDB_XML_AF_NONE, NULL, NULL },
2716 { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2717 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2718};
2719
2720const struct gdb_xml_element thread_children[] = {
2721 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2722};
2723
2724const struct gdb_xml_element threads_children[] = {
2725 { "thread", thread_attributes, thread_children,
2726 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2727 start_thread, end_thread },
2728 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2729};
2730
2731const struct gdb_xml_element threads_elements[] = {
2732 { "threads", NULL, threads_children,
2733 GDB_XML_EF_NONE, NULL, NULL },
2734 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2735};
2736
2737#endif
2738
6dc54d91 2739/* List remote threads using qXfer:threads:read. */
9d1f7ab2 2740
6dc54d91
PA
2741static int
2742remote_get_threads_with_qxfer (struct target_ops *ops,
2743 struct threads_listing_context *context)
0f71a2f6 2744{
dc146f7c 2745#if defined(HAVE_LIBEXPAT)
4082afcc 2746 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c 2747 {
6dc54d91 2748 char *xml = target_read_stralloc (ops, TARGET_OBJECT_THREADS, NULL);
dc146f7c 2749 struct cleanup *back_to = make_cleanup (xfree, xml);
efc0eabd 2750
6dc54d91 2751 if (xml != NULL && *xml != '\0')
dc146f7c 2752 {
6dc54d91
PA
2753 gdb_xml_parse_quick (_("threads"), "threads.dtd",
2754 threads_elements, xml, context);
dc146f7c
VP
2755 }
2756
2757 do_cleanups (back_to);
6dc54d91 2758 return 1;
dc146f7c
VP
2759 }
2760#endif
2761
6dc54d91
PA
2762 return 0;
2763}
2764
2765/* List remote threads using qfThreadInfo/qsThreadInfo. */
2766
2767static int
2768remote_get_threads_with_qthreadinfo (struct target_ops *ops,
2769 struct threads_listing_context *context)
2770{
2771 struct remote_state *rs = get_remote_state ();
2772
b80fafe3 2773 if (rs->use_threadinfo_query)
9d1f7ab2 2774 {
6dc54d91
PA
2775 char *bufp;
2776
9d1f7ab2 2777 putpkt ("qfThreadInfo");
6d820c5c 2778 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2779 bufp = rs->buf;
9d1f7ab2 2780 if (bufp[0] != '\0') /* q packet recognized */
802188a7 2781 {
9d1f7ab2
MS
2782 while (*bufp++ == 'm') /* reply contains one or more TID */
2783 {
2784 do
2785 {
6dc54d91
PA
2786 struct thread_item item;
2787
2788 item.ptid = read_ptid (bufp, &bufp);
2789 item.core = -1;
2790 item.extra = NULL;
2791
2792 VEC_safe_push (thread_item_t, context->items, &item);
9d1f7ab2
MS
2793 }
2794 while (*bufp++ == ','); /* comma-separated list */
2795 putpkt ("qsThreadInfo");
6d820c5c 2796 getpkt (&rs->buf, &rs->buf_size, 0);
6dc54d91 2797 bufp = rs->buf;
9d1f7ab2 2798 }
6dc54d91
PA
2799 return 1;
2800 }
2801 else
2802 {
2803 /* Packet not recognized. */
2804 rs->use_threadinfo_query = 0;
9d1f7ab2
MS
2805 }
2806 }
2807
6dc54d91
PA
2808 return 0;
2809}
2810
e8032dde 2811/* Implement the to_update_thread_list function for the remote
6dc54d91
PA
2812 targets. */
2813
2814static void
e8032dde 2815remote_update_thread_list (struct target_ops *ops)
6dc54d91
PA
2816{
2817 struct remote_state *rs = get_remote_state ();
2818 struct threads_listing_context context;
2819 struct cleanup *old_chain;
ab970af1 2820 int got_list = 0;
e8032dde 2821
6dc54d91
PA
2822 context.items = NULL;
2823 old_chain = make_cleanup (clear_threads_listing_context, &context);
2824
2825 /* We have a few different mechanisms to fetch the thread list. Try
2826 them all, starting with the most preferred one first, falling
2827 back to older methods. */
2828 if (remote_get_threads_with_qxfer (ops, &context)
2829 || remote_get_threads_with_qthreadinfo (ops, &context)
2830 || remote_get_threads_with_ql (ops, &context))
2831 {
2832 int i;
2833 struct thread_item *item;
ab970af1
PA
2834 struct thread_info *tp, *tmp;
2835
2836 got_list = 1;
2837
7d1a114c
PA
2838 if (VEC_empty (thread_item_t, context.items)
2839 && remote_thread_always_alive (ops, inferior_ptid))
2840 {
2841 /* Some targets don't really support threads, but still
2842 reply an (empty) thread list in response to the thread
2843 listing packets, instead of replying "packet not
2844 supported". Exit early so we don't delete the main
2845 thread. */
2846 do_cleanups (old_chain);
2847 return;
2848 }
2849
ab970af1
PA
2850 /* CONTEXT now holds the current thread list on the remote
2851 target end. Delete GDB-side threads no longer found on the
2852 target. */
8a06aea7 2853 ALL_THREADS_SAFE (tp, tmp)
ab970af1
PA
2854 {
2855 for (i = 0;
2856 VEC_iterate (thread_item_t, context.items, i, item);
2857 ++i)
2858 {
2859 if (ptid_equal (item->ptid, tp->ptid))
2860 break;
2861 }
2862
2863 if (i == VEC_length (thread_item_t, context.items))
2864 {
2865 /* Not found. */
2866 delete_thread (tp->ptid);
2867 }
2868 }
74531fed 2869
ab970af1 2870 /* And now add threads we don't know about yet to our list. */
6dc54d91
PA
2871 for (i = 0;
2872 VEC_iterate (thread_item_t, context.items, i, item);
2873 ++i)
2874 {
2875 if (!ptid_equal (item->ptid, null_ptid))
2876 {
2877 struct private_thread_info *info;
2878 /* In non-stop mode, we assume new found threads are
2879 running until proven otherwise with a stop reply. In
2880 all-stop, we can only get here if all threads are
2881 stopped. */
2882 int running = non_stop ? 1 : 0;
2883
2884 remote_notice_new_inferior (item->ptid, running);
2885
2886 info = demand_private_info (item->ptid);
2887 info->core = item->core;
2888 info->extra = item->extra;
2889 item->extra = NULL;
2890 }
2891 }
2892 }
2893
ab970af1
PA
2894 if (!got_list)
2895 {
2896 /* If no thread listing method is supported, then query whether
2897 each known thread is alive, one by one, with the T packet.
2898 If the target doesn't support threads at all, then this is a
2899 no-op. See remote_thread_alive. */
2900 prune_threads ();
2901 }
2902
6dc54d91 2903 do_cleanups (old_chain);
9d1f7ab2
MS
2904}
2905
802188a7 2906/*
9d1f7ab2
MS
2907 * Collect a descriptive string about the given thread.
2908 * The target may say anything it wants to about the thread
2909 * (typically info about its blocked / runnable state, name, etc.).
2910 * This string will appear in the info threads display.
802188a7 2911 *
9d1f7ab2
MS
2912 * Optional: targets are not required to implement this function.
2913 */
2914
2915static char *
c15906d8 2916remote_threads_extra_info (struct target_ops *self, struct thread_info *tp)
9d1f7ab2 2917{
d01949b6 2918 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
2919 int result;
2920 int set;
2921 threadref id;
2922 struct gdb_ext_thread_info threadinfo;
23860348 2923 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
2924 int n = 0; /* position in display_buf */
2925
5d93a237 2926 if (rs->remote_desc == 0) /* paranoia */
8e65ff28 2927 internal_error (__FILE__, __LINE__,
e2e0b3e5 2928 _("remote_threads_extra_info"));
9d1f7ab2 2929
60e569b9 2930 if (ptid_equal (tp->ptid, magic_null_ptid)
ba348170 2931 || (ptid_get_pid (tp->ptid) != 0 && ptid_get_lwp (tp->ptid) == 0))
60e569b9
PA
2932 /* This is the main thread which was added by GDB. The remote
2933 server doesn't know about it. */
2934 return NULL;
2935
4082afcc 2936 if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
dc146f7c
VP
2937 {
2938 struct thread_info *info = find_thread_ptid (tp->ptid);
a744cf53 2939
fe978cb0
PA
2940 if (info && info->priv)
2941 return info->priv->extra;
dc146f7c
VP
2942 else
2943 return NULL;
2944 }
2945
b80fafe3 2946 if (rs->use_threadextra_query)
9d1f7ab2 2947 {
82f73884
PA
2948 char *b = rs->buf;
2949 char *endb = rs->buf + get_remote_packet_size ();
2950
2951 xsnprintf (b, endb - b, "qThreadExtraInfo,");
2952 b += strlen (b);
2953 write_ptid (b, endb, tp->ptid);
2954
2e9f7625 2955 putpkt (rs->buf);
6d820c5c 2956 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2957 if (rs->buf[0] != 0)
9d1f7ab2 2958 {
2e9f7625
DJ
2959 n = min (strlen (rs->buf) / 2, sizeof (display_buf));
2960 result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
30559e10 2961 display_buf [result] = '\0';
9d1f7ab2
MS
2962 return display_buf;
2963 }
0f71a2f6 2964 }
9d1f7ab2
MS
2965
2966 /* If the above query fails, fall back to the old method. */
b80fafe3 2967 rs->use_threadextra_query = 0;
9d1f7ab2
MS
2968 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
2969 | TAG_MOREDISPLAY | TAG_DISPLAY;
ba348170 2970 int_to_threadref (&id, ptid_get_lwp (tp->ptid));
9d1f7ab2
MS
2971 if (remote_get_threadinfo (&id, set, &threadinfo))
2972 if (threadinfo.active)
0f71a2f6 2973 {
9d1f7ab2 2974 if (*threadinfo.shortname)
2bc416ba 2975 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
ecbc58df 2976 " Name: %s,", threadinfo.shortname);
9d1f7ab2 2977 if (*threadinfo.display)
2bc416ba 2978 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 2979 " State: %s,", threadinfo.display);
9d1f7ab2 2980 if (*threadinfo.more_display)
2bc416ba 2981 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
ecbc58df 2982 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
2983
2984 if (n > 0)
c5aa993b 2985 {
23860348 2986 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
2987 if (',' == display_buf[n-1])
2988 display_buf[n-1] = ' ';
2989 return display_buf;
c5aa993b 2990 }
0f71a2f6 2991 }
9d1f7ab2 2992 return NULL;
0f71a2f6 2993}
c906108c 2994\f
c5aa993b 2995
0fb4aa4b 2996static int
61fc905d 2997remote_static_tracepoint_marker_at (struct target_ops *self, CORE_ADDR addr,
0fb4aa4b
PA
2998 struct static_tracepoint_marker *marker)
2999{
3000 struct remote_state *rs = get_remote_state ();
3001 char *p = rs->buf;
3002
bba74b36 3003 xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
0fb4aa4b
PA
3004 p += strlen (p);
3005 p += hexnumstr (p, addr);
3006 putpkt (rs->buf);
3007 getpkt (&rs->buf, &rs->buf_size, 0);
3008 p = rs->buf;
3009
3010 if (*p == 'E')
3011 error (_("Remote failure reply: %s"), p);
3012
3013 if (*p++ == 'm')
3014 {
3015 parse_static_tracepoint_marker_definition (p, &p, marker);
3016 return 1;
3017 }
3018
3019 return 0;
3020}
3021
0fb4aa4b 3022static VEC(static_tracepoint_marker_p) *
c686c57f
TT
3023remote_static_tracepoint_markers_by_strid (struct target_ops *self,
3024 const char *strid)
0fb4aa4b
PA
3025{
3026 struct remote_state *rs = get_remote_state ();
3027 VEC(static_tracepoint_marker_p) *markers = NULL;
3028 struct static_tracepoint_marker *marker = NULL;
3029 struct cleanup *old_chain;
3030 char *p;
3031
3032 /* Ask for a first packet of static tracepoint marker
3033 definition. */
3034 putpkt ("qTfSTM");
3035 getpkt (&rs->buf, &rs->buf_size, 0);
3036 p = rs->buf;
3037 if (*p == 'E')
3038 error (_("Remote failure reply: %s"), p);
3039
3040 old_chain = make_cleanup (free_current_marker, &marker);
3041
3042 while (*p++ == 'm')
3043 {
3044 if (marker == NULL)
3045 marker = XCNEW (struct static_tracepoint_marker);
3046
3047 do
3048 {
3049 parse_static_tracepoint_marker_definition (p, &p, marker);
3050
3051 if (strid == NULL || strcmp (strid, marker->str_id) == 0)
3052 {
3053 VEC_safe_push (static_tracepoint_marker_p,
3054 markers, marker);
3055 marker = NULL;
3056 }
3057 else
3058 {
3059 release_static_tracepoint_marker (marker);
3060 memset (marker, 0, sizeof (*marker));
3061 }
3062 }
3063 while (*p++ == ','); /* comma-separated list */
3064 /* Ask for another packet of static tracepoint definition. */
3065 putpkt ("qTsSTM");
3066 getpkt (&rs->buf, &rs->buf_size, 0);
3067 p = rs->buf;
3068 }
3069
3070 do_cleanups (old_chain);
3071 return markers;
3072}
3073
3074\f
10760264
JB
3075/* Implement the to_get_ada_task_ptid function for the remote targets. */
3076
3077static ptid_t
1e6b91a4 3078remote_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
10760264 3079{
ba348170 3080 return ptid_build (ptid_get_pid (inferior_ptid), lwp, 0);
10760264
JB
3081}
3082\f
3083
24b06219 3084/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
3085
3086static void
fba45db2 3087extended_remote_restart (void)
c906108c 3088{
d01949b6 3089 struct remote_state *rs = get_remote_state ();
c906108c
SS
3090
3091 /* Send the restart command; for reasons I don't understand the
3092 remote side really expects a number after the "R". */
ea9c271d 3093 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 3094 putpkt (rs->buf);
c906108c 3095
ad9a8f3f 3096 remote_fileio_reset ();
c906108c
SS
3097}
3098\f
3099/* Clean up connection to a remote debugger. */
3100
c906108c 3101static void
de90e03d 3102remote_close (struct target_ops *self)
c906108c 3103{
5d93a237
TT
3104 struct remote_state *rs = get_remote_state ();
3105
3106 if (rs->remote_desc == NULL)
d3fd5342
PA
3107 return; /* already closed */
3108
3109 /* Make sure we leave stdin registered in the event loop, and we
3110 don't leave the async SIGINT signal handler installed. */
e3594fd1 3111 remote_terminal_ours (self);
ce5ce7ed 3112
5d93a237
TT
3113 serial_close (rs->remote_desc);
3114 rs->remote_desc = NULL;
ce5ce7ed
PA
3115
3116 /* We don't have a connection to the remote stub anymore. Get rid
f67fd822
PM
3117 of all the inferiors and their threads we were controlling.
3118 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3119 will be unable to find the thread corresponding to (pid, 0, 0). */
0f2caa1b 3120 inferior_ptid = null_ptid;
f67fd822 3121 discard_all_inferiors ();
ce5ce7ed 3122
f48ff2a7
YQ
3123 /* We are closing the remote target, so we should discard
3124 everything of this target. */
bcc75809 3125 discard_pending_stop_replies_in_queue (rs);
74531fed
PA
3126
3127 if (remote_async_inferior_event_token)
3128 delete_async_event_handler (&remote_async_inferior_event_token);
722247f1 3129
5965e028 3130 remote_notif_state_xfree (rs->notif_state);
aef525cb
YQ
3131
3132 trace_reset_local_state ();
c906108c
SS
3133}
3134
23860348 3135/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
3136
3137static void
fba45db2 3138get_offsets (void)
c906108c 3139{
d01949b6 3140 struct remote_state *rs = get_remote_state ();
2e9f7625 3141 char *buf;
085dd6e6 3142 char *ptr;
31d99776
DJ
3143 int lose, num_segments = 0, do_sections, do_segments;
3144 CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
c906108c 3145 struct section_offsets *offs;
31d99776
DJ
3146 struct symfile_segment_data *data;
3147
3148 if (symfile_objfile == NULL)
3149 return;
c906108c
SS
3150
3151 putpkt ("qOffsets");
6d820c5c 3152 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3153 buf = rs->buf;
c906108c
SS
3154
3155 if (buf[0] == '\000')
3156 return; /* Return silently. Stub doesn't support
23860348 3157 this command. */
c906108c
SS
3158 if (buf[0] == 'E')
3159 {
8a3fe4f8 3160 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
3161 return;
3162 }
3163
3164 /* Pick up each field in turn. This used to be done with scanf, but
3165 scanf will make trouble if CORE_ADDR size doesn't match
3166 conversion directives correctly. The following code will work
3167 with any size of CORE_ADDR. */
3168 text_addr = data_addr = bss_addr = 0;
3169 ptr = buf;
3170 lose = 0;
3171
61012eef 3172 if (startswith (ptr, "Text="))
c906108c
SS
3173 {
3174 ptr += 5;
3175 /* Don't use strtol, could lose on big values. */
3176 while (*ptr && *ptr != ';')
3177 text_addr = (text_addr << 4) + fromhex (*ptr++);
c906108c 3178
61012eef 3179 if (startswith (ptr, ";Data="))
31d99776
DJ
3180 {
3181 ptr += 6;
3182 while (*ptr && *ptr != ';')
3183 data_addr = (data_addr << 4) + fromhex (*ptr++);
3184 }
3185 else
3186 lose = 1;
3187
61012eef 3188 if (!lose && startswith (ptr, ";Bss="))
31d99776
DJ
3189 {
3190 ptr += 5;
3191 while (*ptr && *ptr != ';')
3192 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
c906108c 3193
31d99776
DJ
3194 if (bss_addr != data_addr)
3195 warning (_("Target reported unsupported offsets: %s"), buf);
3196 }
3197 else
3198 lose = 1;
3199 }
61012eef 3200 else if (startswith (ptr, "TextSeg="))
c906108c 3201 {
31d99776
DJ
3202 ptr += 8;
3203 /* Don't use strtol, could lose on big values. */
c906108c 3204 while (*ptr && *ptr != ';')
31d99776
DJ
3205 text_addr = (text_addr << 4) + fromhex (*ptr++);
3206 num_segments = 1;
3207
61012eef 3208 if (startswith (ptr, ";DataSeg="))
31d99776
DJ
3209 {
3210 ptr += 9;
3211 while (*ptr && *ptr != ';')
3212 data_addr = (data_addr << 4) + fromhex (*ptr++);
3213 num_segments++;
3214 }
c906108c
SS
3215 }
3216 else
3217 lose = 1;
3218
3219 if (lose)
8a3fe4f8 3220 error (_("Malformed response to offset query, %s"), buf);
31d99776
DJ
3221 else if (*ptr != '\0')
3222 warning (_("Target reported unsupported offsets: %s"), buf);
c906108c 3223
802188a7 3224 offs = ((struct section_offsets *)
a39a16c4 3225 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 3226 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 3227 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 3228
31d99776
DJ
3229 data = get_symfile_segment_data (symfile_objfile->obfd);
3230 do_segments = (data != NULL);
3231 do_sections = num_segments == 0;
c906108c 3232
28c32713 3233 if (num_segments > 0)
31d99776 3234 {
31d99776
DJ
3235 segments[0] = text_addr;
3236 segments[1] = data_addr;
3237 }
28c32713
JB
3238 /* If we have two segments, we can still try to relocate everything
3239 by assuming that the .text and .data offsets apply to the whole
3240 text and data segments. Convert the offsets given in the packet
3241 to base addresses for symfile_map_offsets_to_segments. */
3242 else if (data && data->num_segments == 2)
3243 {
3244 segments[0] = data->segment_bases[0] + text_addr;
3245 segments[1] = data->segment_bases[1] + data_addr;
3246 num_segments = 2;
3247 }
8d385431
DJ
3248 /* If the object file has only one segment, assume that it is text
3249 rather than data; main programs with no writable data are rare,
3250 but programs with no code are useless. Of course the code might
3251 have ended up in the data segment... to detect that we would need
3252 the permissions here. */
3253 else if (data && data->num_segments == 1)
3254 {
3255 segments[0] = data->segment_bases[0] + text_addr;
3256 num_segments = 1;
3257 }
28c32713
JB
3258 /* There's no way to relocate by segment. */
3259 else
3260 do_segments = 0;
31d99776
DJ
3261
3262 if (do_segments)
3263 {
3264 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
3265 offs, num_segments, segments);
3266
3267 if (ret == 0 && !do_sections)
3e43a32a
MS
3268 error (_("Can not handle qOffsets TextSeg "
3269 "response with this symbol file"));
31d99776
DJ
3270
3271 if (ret > 0)
3272 do_sections = 0;
3273 }
c906108c 3274
9ef895d6
DJ
3275 if (data)
3276 free_symfile_segment_data (data);
31d99776
DJ
3277
3278 if (do_sections)
3279 {
3280 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
3281
3e43a32a
MS
3282 /* This is a temporary kludge to force data and bss to use the
3283 same offsets because that's what nlmconv does now. The real
3284 solution requires changes to the stub and remote.c that I
3285 don't have time to do right now. */
31d99776
DJ
3286
3287 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
3288 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
3289 }
c906108c
SS
3290
3291 objfile_relocate (symfile_objfile, offs);
3292}
3293
74531fed
PA
3294/* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3295 threads we know are stopped already. This is used during the
3296 initial remote connection in non-stop mode --- threads that are
3297 reported as already being stopped are left stopped. */
3298
3299static int
3300set_stop_requested_callback (struct thread_info *thread, void *data)
3301{
3302 /* If we have a stop reply for this thread, it must be stopped. */
3303 if (peek_stop_reply (thread->ptid))
3304 set_stop_requested (thread->ptid, 1);
3305
3306 return 0;
3307}
3308
9a7071a8
JB
3309/* Send interrupt_sequence to remote target. */
3310static void
eeae04df 3311send_interrupt_sequence (void)
9a7071a8 3312{
5d93a237
TT
3313 struct remote_state *rs = get_remote_state ();
3314
9a7071a8 3315 if (interrupt_sequence_mode == interrupt_sequence_control_c)
c33e31fd 3316 remote_serial_write ("\x03", 1);
9a7071a8 3317 else if (interrupt_sequence_mode == interrupt_sequence_break)
5d93a237 3318 serial_send_break (rs->remote_desc);
9a7071a8
JB
3319 else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3320 {
5d93a237 3321 serial_send_break (rs->remote_desc);
c33e31fd 3322 remote_serial_write ("g", 1);
9a7071a8
JB
3323 }
3324 else
3325 internal_error (__FILE__, __LINE__,
3326 _("Invalid value for interrupt_sequence_mode: %s."),
3327 interrupt_sequence_mode);
3328}
3329
3405876a
PA
3330
3331/* If STOP_REPLY is a T stop reply, look for the "thread" register,
3332 and extract the PTID. Returns NULL_PTID if not found. */
3333
3334static ptid_t
3335stop_reply_extract_thread (char *stop_reply)
3336{
3337 if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3338 {
3339 char *p;
3340
3341 /* Txx r:val ; r:val (...) */
3342 p = &stop_reply[3];
3343
3344 /* Look for "register" named "thread". */
3345 while (*p != '\0')
3346 {
3347 char *p1;
3348
3349 p1 = strchr (p, ':');
3350 if (p1 == NULL)
3351 return null_ptid;
3352
3353 if (strncmp (p, "thread", p1 - p) == 0)
3354 return read_ptid (++p1, &p);
3355
3356 p1 = strchr (p, ';');
3357 if (p1 == NULL)
3358 return null_ptid;
3359 p1++;
3360
3361 p = p1;
3362 }
3363 }
3364
3365 return null_ptid;
3366}
3367
b7ea362b
PA
3368/* Determine the remote side's current thread. If we have a stop
3369 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3370 "thread" register we can extract the current thread from. If not,
3371 ask the remote which is the current thread with qC. The former
3372 method avoids a roundtrip. */
3373
3374static ptid_t
3375get_current_thread (char *wait_status)
3376{
3377 ptid_t ptid;
3378
3379 /* Note we don't use remote_parse_stop_reply as that makes use of
3380 the target architecture, which we haven't yet fully determined at
3381 this point. */
3382 if (wait_status != NULL)
3383 ptid = stop_reply_extract_thread (wait_status);
3384 if (ptid_equal (ptid, null_ptid))
3385 ptid = remote_current_thread (inferior_ptid);
3386
3387 return ptid;
3388}
3389
49c62f2e
PA
3390/* Query the remote target for which is the current thread/process,
3391 add it to our tables, and update INFERIOR_PTID. The caller is
3392 responsible for setting the state such that the remote end is ready
3405876a
PA
3393 to return the current thread.
3394
3395 This function is called after handling the '?' or 'vRun' packets,
3396 whose response is a stop reply from which we can also try
3397 extracting the thread. If the target doesn't support the explicit
3398 qC query, we infer the current thread from that stop reply, passed
3399 in in WAIT_STATUS, which may be NULL. */
49c62f2e
PA
3400
3401static void
3405876a 3402add_current_inferior_and_thread (char *wait_status)
49c62f2e
PA
3403{
3404 struct remote_state *rs = get_remote_state ();
3405 int fake_pid_p = 0;
3405876a 3406 ptid_t ptid = null_ptid;
49c62f2e
PA
3407
3408 inferior_ptid = null_ptid;
3409
b7ea362b
PA
3410 /* Now, if we have thread information, update inferior_ptid. */
3411 ptid = get_current_thread (wait_status);
3405876a 3412
49c62f2e
PA
3413 if (!ptid_equal (ptid, null_ptid))
3414 {
3415 if (!remote_multi_process_p (rs))
3416 fake_pid_p = 1;
3417
3418 inferior_ptid = ptid;
3419 }
3420 else
3421 {
3422 /* Without this, some commands which require an active target
3423 (such as kill) won't work. This variable serves (at least)
3424 double duty as both the pid of the target process (if it has
3425 such), and as a flag indicating that a target is active. */
3426 inferior_ptid = magic_null_ptid;
3427 fake_pid_p = 1;
3428 }
3429
1b6e6f5c 3430 remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1, 1);
49c62f2e
PA
3431
3432 /* Add the main thread. */
3433 add_thread_silent (inferior_ptid);
3434}
3435
9cbc821d 3436static void
04bd08de 3437remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
c906108c 3438{
c8d104ad
PA
3439 struct remote_state *rs = get_remote_state ();
3440 struct packet_config *noack_config;
2d717e4f 3441 char *wait_status = NULL;
8621d6a9 3442
23860348 3443 immediate_quit++; /* Allow user to interrupt it. */
522002f9 3444 QUIT;
c906108c 3445
9a7071a8
JB
3446 if (interrupt_on_connect)
3447 send_interrupt_sequence ();
3448
57e12211 3449 /* Ack any packet which the remote side has already sent. */
5d93a237 3450 serial_write (rs->remote_desc, "+", 1);
57e12211 3451
1e51243a
PA
3452 /* Signal other parts that we're going through the initial setup,
3453 and so things may not be stable yet. */
3454 rs->starting_up = 1;
3455
c8d104ad
PA
3456 /* The first packet we send to the target is the optional "supported
3457 packets" request. If the target can answer this, it will tell us
3458 which later probes to skip. */
3459 remote_query_supported ();
3460
d914c394 3461 /* If the stub wants to get a QAllow, compose one and send it. */
4082afcc 3462 if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
c378d69d 3463 remote_set_permissions (target);
d914c394 3464
c8d104ad
PA
3465 /* Next, we possibly activate noack mode.
3466
3467 If the QStartNoAckMode packet configuration is set to AUTO,
3468 enable noack mode if the stub reported a wish for it with
3469 qSupported.
3470
3471 If set to TRUE, then enable noack mode even if the stub didn't
3472 report it in qSupported. If the stub doesn't reply OK, the
3473 session ends with an error.
3474
3475 If FALSE, then don't activate noack mode, regardless of what the
3476 stub claimed should be the default with qSupported. */
3477
3478 noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
4082afcc 3479 if (packet_config_support (noack_config) != PACKET_DISABLE)
c8d104ad
PA
3480 {
3481 putpkt ("QStartNoAckMode");
3482 getpkt (&rs->buf, &rs->buf_size, 0);
3483 if (packet_ok (rs->buf, noack_config) == PACKET_OK)
3484 rs->noack_mode = 1;
3485 }
3486
04bd08de 3487 if (extended_p)
5fe04517
PA
3488 {
3489 /* Tell the remote that we are using the extended protocol. */
3490 putpkt ("!");
3491 getpkt (&rs->buf, &rs->buf_size, 0);
3492 }
3493
9b224c5e
PA
3494 /* Let the target know which signals it is allowed to pass down to
3495 the program. */
3496 update_signals_program_target ();
3497
d962ef82
DJ
3498 /* Next, if the target can specify a description, read it. We do
3499 this before anything involving memory or registers. */
3500 target_find_description ();
3501
6c95b8df
PA
3502 /* Next, now that we know something about the target, update the
3503 address spaces in the program spaces. */
3504 update_address_spaces ();
3505
50c71eaf
PA
3506 /* On OSs where the list of libraries is global to all
3507 processes, we fetch them early. */
f5656ead 3508 if (gdbarch_has_global_solist (target_gdbarch ()))
04bd08de 3509 solib_add (NULL, from_tty, target, auto_solib_add);
50c71eaf 3510
74531fed
PA
3511 if (non_stop)
3512 {
4082afcc 3513 if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
3e43a32a
MS
3514 error (_("Non-stop mode requested, but remote "
3515 "does not support non-stop"));
74531fed
PA
3516
3517 putpkt ("QNonStop:1");
3518 getpkt (&rs->buf, &rs->buf_size, 0);
3519
3520 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3521 error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
74531fed
PA
3522
3523 /* Find about threads and processes the stub is already
3524 controlling. We default to adding them in the running state.
3525 The '?' query below will then tell us about which threads are
3526 stopped. */
e8032dde 3527 remote_update_thread_list (target);
74531fed 3528 }
4082afcc 3529 else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
74531fed
PA
3530 {
3531 /* Don't assume that the stub can operate in all-stop mode.
e6f3fa52 3532 Request it explicitly. */
74531fed
PA
3533 putpkt ("QNonStop:0");
3534 getpkt (&rs->buf, &rs->buf_size, 0);
3535
3536 if (strcmp (rs->buf, "OK") != 0)
9b20d036 3537 error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
74531fed
PA
3538 }
3539
a0743c90
YQ
3540 /* Upload TSVs regardless of whether the target is running or not. The
3541 remote stub, such as GDBserver, may have some predefined or builtin
3542 TSVs, even if the target is not running. */
8bd200f1 3543 if (remote_get_trace_status (target, current_trace_status ()) != -1)
a0743c90
YQ
3544 {
3545 struct uploaded_tsv *uploaded_tsvs = NULL;
3546
181e3713 3547 remote_upload_trace_state_variables (target, &uploaded_tsvs);
a0743c90
YQ
3548 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3549 }
3550
2d717e4f
DJ
3551 /* Check whether the target is running now. */
3552 putpkt ("?");
3553 getpkt (&rs->buf, &rs->buf_size, 0);
3554
74531fed 3555 if (!non_stop)
2d717e4f 3556 {
e714e1bf
UW
3557 ptid_t ptid;
3558 int fake_pid_p = 0;
3559 struct inferior *inf;
3560
74531fed 3561 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2d717e4f 3562 {
04bd08de 3563 if (!extended_p)
74531fed 3564 error (_("The target is not running (try extended-remote?)"));
c35b1492
PA
3565
3566 /* We're connected, but not running. Drop out before we
3567 call start_remote. */
e278ad5b 3568 rs->starting_up = 0;
c35b1492 3569 return;
2d717e4f
DJ
3570 }
3571 else
74531fed 3572 {
74531fed
PA
3573 /* Save the reply for later. */
3574 wait_status = alloca (strlen (rs->buf) + 1);
3575 strcpy (wait_status, rs->buf);
3576 }
3577
b7ea362b 3578 /* Fetch thread list. */
e8032dde 3579 target_update_thread_list ();
b7ea362b 3580
74531fed
PA
3581 /* Let the stub know that we want it to return the thread. */
3582 set_continue_thread (minus_one_ptid);
3583
b7ea362b
PA
3584 if (thread_count () == 0)
3585 {
3586 /* Target has no concept of threads at all. GDB treats
3587 non-threaded target as single-threaded; add a main
3588 thread. */
3589 add_current_inferior_and_thread (wait_status);
3590 }
3591 else
3592 {
3593 /* We have thread information; select the thread the target
3594 says should be current. If we're reconnecting to a
3595 multi-threaded program, this will ideally be the thread
3596 that last reported an event before GDB disconnected. */
3597 inferior_ptid = get_current_thread (wait_status);
3598 if (ptid_equal (inferior_ptid, null_ptid))
3599 {
3600 /* Odd... The target was able to list threads, but not
3601 tell us which thread was current (no "thread"
3602 register in T stop reply?). Just pick the first
3603 thread in the thread list then. */
3604 inferior_ptid = thread_list->ptid;
3605 }
3606 }
74531fed 3607
6e586cc5
YQ
3608 /* init_wait_for_inferior should be called before get_offsets in order
3609 to manage `inserted' flag in bp loc in a correct state.
3610 breakpoint_init_inferior, called from init_wait_for_inferior, set
3611 `inserted' flag to 0, while before breakpoint_re_set, called from
3612 start_remote, set `inserted' flag to 1. In the initialization of
3613 inferior, breakpoint_init_inferior should be called first, and then
3614 breakpoint_re_set can be called. If this order is broken, state of
3615 `inserted' flag is wrong, and cause some problems on breakpoint
3616 manipulation. */
3617 init_wait_for_inferior ();
3618
74531fed
PA
3619 get_offsets (); /* Get text, data & bss offsets. */
3620
d962ef82
DJ
3621 /* If we could not find a description using qXfer, and we know
3622 how to do it some other way, try again. This is not
3623 supported for non-stop; it could be, but it is tricky if
3624 there are no stopped threads when we connect. */
04bd08de 3625 if (remote_read_description_p (target)
f5656ead 3626 && gdbarch_target_desc (target_gdbarch ()) == NULL)
d962ef82
DJ
3627 {
3628 target_clear_description ();
3629 target_find_description ();
3630 }
3631
74531fed
PA
3632 /* Use the previously fetched status. */
3633 gdb_assert (wait_status != NULL);
3634 strcpy (rs->buf, wait_status);
3635 rs->cached_wait_status = 1;
3636
3637 immediate_quit--;
04bd08de 3638 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
3639 }
3640 else
3641 {
68c97600
PA
3642 /* Clear WFI global state. Do this before finding about new
3643 threads and inferiors, and setting the current inferior.
3644 Otherwise we would clear the proceed status of the current
3645 inferior when we want its stop_soon state to be preserved
3646 (see notice_new_inferior). */
3647 init_wait_for_inferior ();
3648
74531fed
PA
3649 /* In non-stop, we will either get an "OK", meaning that there
3650 are no stopped threads at this time; or, a regular stop
3651 reply. In the latter case, there may be more than one thread
3652 stopped --- we pull them all out using the vStopped
3653 mechanism. */
3654 if (strcmp (rs->buf, "OK") != 0)
3655 {
722247f1 3656 struct notif_client *notif = &notif_client_stop;
2d717e4f 3657
722247f1
YQ
3658 /* remote_notif_get_pending_replies acks this one, and gets
3659 the rest out. */
f48ff2a7 3660 rs->notif_state->pending_event[notif_client_stop.id]
722247f1
YQ
3661 = remote_notif_parse (notif, rs->buf);
3662 remote_notif_get_pending_events (notif);
c906108c 3663
74531fed
PA
3664 /* Make sure that threads that were stopped remain
3665 stopped. */
3666 iterate_over_threads (set_stop_requested_callback, NULL);
3667 }
2d717e4f 3668
74531fed 3669 if (target_can_async_p ())
6a3753b3 3670 target_async (1);
c906108c 3671
74531fed
PA
3672 if (thread_count () == 0)
3673 {
04bd08de 3674 if (!extended_p)
74531fed 3675 error (_("The target is not running (try extended-remote?)"));
82f73884 3676
c35b1492
PA
3677 /* We're connected, but not running. Drop out before we
3678 call start_remote. */
e278ad5b 3679 rs->starting_up = 0;
c35b1492
PA
3680 return;
3681 }
74531fed
PA
3682
3683 /* Let the stub know that we want it to return the thread. */
c0a2216e 3684
74531fed
PA
3685 /* Force the stub to choose a thread. */
3686 set_general_thread (null_ptid);
c906108c 3687
74531fed
PA
3688 /* Query it. */
3689 inferior_ptid = remote_current_thread (minus_one_ptid);
3690 if (ptid_equal (inferior_ptid, minus_one_ptid))
3691 error (_("remote didn't report the current thread in non-stop mode"));
c906108c 3692
74531fed
PA
3693 get_offsets (); /* Get text, data & bss offsets. */
3694
3695 /* In non-stop mode, any cached wait status will be stored in
3696 the stop reply queue. */
3697 gdb_assert (wait_status == NULL);
f0223081 3698
2455069d 3699 /* Report all signals during attach/startup. */
94bedb42 3700 remote_pass_signals (target, 0, NULL);
74531fed 3701 }
c8d104ad 3702
c8d104ad
PA
3703 /* If we connected to a live target, do some additional setup. */
3704 if (target_has_execution)
3705 {
f4ccffad 3706 if (symfile_objfile) /* No use without a symbol-file. */
36d25514 3707 remote_check_symbols ();
c8d104ad 3708 }
50c71eaf 3709
d5551862
SS
3710 /* Possibly the target has been engaged in a trace run started
3711 previously; find out where things are at. */
8bd200f1 3712 if (remote_get_trace_status (target, current_trace_status ()) != -1)
d5551862 3713 {
00bf0b85 3714 struct uploaded_tp *uploaded_tps = NULL;
00bf0b85 3715
00bf0b85
SS
3716 if (current_trace_status ()->running)
3717 printf_filtered (_("Trace is already running on the target.\n"));
3718
ab6617cc 3719 remote_upload_tracepoints (target, &uploaded_tps);
00bf0b85
SS
3720
3721 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
3722 }
3723
1e51243a
PA
3724 /* The thread and inferior lists are now synchronized with the
3725 target, our symbols have been relocated, and we're merged the
3726 target's tracepoints with ours. We're done with basic start
3727 up. */
3728 rs->starting_up = 0;
3729
a25a5a45
PA
3730 /* Maybe breakpoints are global and need to be inserted now. */
3731 if (breakpoints_should_be_inserted_now ())
50c71eaf 3732 insert_breakpoints ();
c906108c
SS
3733}
3734
3735/* Open a connection to a remote debugger.
3736 NAME is the filename used for communication. */
3737
3738static void
014f9477 3739remote_open (const char *name, int from_tty)
c906108c 3740{
75c99385 3741 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
3742}
3743
c906108c
SS
3744/* Open a connection to a remote debugger using the extended
3745 remote gdb protocol. NAME is the filename used for communication. */
3746
3747static void
014f9477 3748extended_remote_open (const char *name, int from_tty)
c906108c 3749{
75c99385 3750 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
3751}
3752
ca4f7f8b
PA
3753/* Reset all packets back to "unknown support". Called when opening a
3754 new connection to a remote target. */
c906108c 3755
d471ea57 3756static void
ca4f7f8b 3757reset_all_packet_configs_support (void)
d471ea57
AC
3758{
3759 int i;
a744cf53 3760
444abaca 3761 for (i = 0; i < PACKET_MAX; i++)
4082afcc 3762 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
d471ea57
AC
3763}
3764
ca4f7f8b
PA
3765/* Initialize all packet configs. */
3766
3767static void
3768init_all_packet_configs (void)
3769{
3770 int i;
3771
3772 for (i = 0; i < PACKET_MAX; i++)
3773 {
3774 remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
3775 remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
3776 }
3777}
3778
23860348 3779/* Symbol look-up. */
dc8acb97
MS
3780
3781static void
36d25514 3782remote_check_symbols (void)
dc8acb97 3783{
d01949b6 3784 struct remote_state *rs = get_remote_state ();
dc8acb97 3785 char *msg, *reply, *tmp;
3b7344d5 3786 struct bound_minimal_symbol sym;
dc8acb97
MS
3787 int end;
3788
63154eca
PA
3789 /* The remote side has no concept of inferiors that aren't running
3790 yet, it only knows about running processes. If we're connected
3791 but our current inferior is not running, we should not invite the
3792 remote target to request symbol lookups related to its
3793 (unrelated) current process. */
3794 if (!target_has_execution)
3795 return;
3796
4082afcc 3797 if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
dc8acb97
MS
3798 return;
3799
63154eca
PA
3800 /* Make sure the remote is pointing at the right process. Note
3801 there's no way to select "no process". */
3c9c4b83
PA
3802 set_general_process ();
3803
6d820c5c
DJ
3804 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3805 because we need both at the same time. */
ea9c271d 3806 msg = alloca (get_remote_packet_size ());
6d820c5c 3807
23860348 3808 /* Invite target to request symbol lookups. */
dc8acb97
MS
3809
3810 putpkt ("qSymbol::");
6d820c5c
DJ
3811 getpkt (&rs->buf, &rs->buf_size, 0);
3812 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
2e9f7625 3813 reply = rs->buf;
dc8acb97 3814
61012eef 3815 while (startswith (reply, "qSymbol:"))
dc8acb97 3816 {
77e371c0
TT
3817 struct bound_minimal_symbol sym;
3818
dc8acb97 3819 tmp = &reply[8];
cfd77fa1 3820 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
3821 msg[end] = '\0';
3822 sym = lookup_minimal_symbol (msg, NULL, NULL);
3b7344d5 3823 if (sym.minsym == NULL)
ea9c271d 3824 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 3825 else
2bbe3cc1 3826 {
f5656ead 3827 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
77e371c0 3828 CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
2bbe3cc1
DJ
3829
3830 /* If this is a function address, return the start of code
3831 instead of any data function descriptor. */
f5656ead 3832 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2bbe3cc1
DJ
3833 sym_addr,
3834 &current_target);
3835
3836 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 3837 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
3838 }
3839
dc8acb97 3840 putpkt (msg);
6d820c5c 3841 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3842 reply = rs->buf;
dc8acb97
MS
3843 }
3844}
3845
9db8d71f 3846static struct serial *
baa336ce 3847remote_serial_open (const char *name)
9db8d71f
DJ
3848{
3849 static int udp_warning = 0;
3850
3851 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3852 of in ser-tcp.c, because it is the remote protocol assuming that the
3853 serial connection is reliable and not the serial connection promising
3854 to be. */
61012eef 3855 if (!udp_warning && startswith (name, "udp:"))
9db8d71f 3856 {
3e43a32a
MS
3857 warning (_("The remote protocol may be unreliable over UDP.\n"
3858 "Some events may be lost, rendering further debugging "
3859 "impossible."));
9db8d71f
DJ
3860 udp_warning = 1;
3861 }
3862
3863 return serial_open (name);
3864}
3865
d914c394
SS
3866/* Inform the target of our permission settings. The permission flags
3867 work without this, but if the target knows the settings, it can do
3868 a couple things. First, it can add its own check, to catch cases
3869 that somehow manage to get by the permissions checks in target
3870 methods. Second, if the target is wired to disallow particular
3871 settings (for instance, a system in the field that is not set up to
3872 be able to stop at a breakpoint), it can object to any unavailable
3873 permissions. */
3874
3875void
c378d69d 3876remote_set_permissions (struct target_ops *self)
d914c394
SS
3877{
3878 struct remote_state *rs = get_remote_state ();
3879
bba74b36
YQ
3880 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3881 "WriteReg:%x;WriteMem:%x;"
3882 "InsertBreak:%x;InsertTrace:%x;"
3883 "InsertFastTrace:%x;Stop:%x",
3884 may_write_registers, may_write_memory,
3885 may_insert_breakpoints, may_insert_tracepoints,
3886 may_insert_fast_tracepoints, may_stop);
d914c394
SS
3887 putpkt (rs->buf);
3888 getpkt (&rs->buf, &rs->buf_size, 0);
3889
3890 /* If the target didn't like the packet, warn the user. Do not try
3891 to undo the user's settings, that would just be maddening. */
3892 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 3893 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
3894}
3895
be2a5f71
DJ
3896/* This type describes each known response to the qSupported
3897 packet. */
3898struct protocol_feature
3899{
3900 /* The name of this protocol feature. */
3901 const char *name;
3902
3903 /* The default for this protocol feature. */
3904 enum packet_support default_support;
3905
3906 /* The function to call when this feature is reported, or after
3907 qSupported processing if the feature is not supported.
3908 The first argument points to this structure. The second
3909 argument indicates whether the packet requested support be
3910 enabled, disabled, or probed (or the default, if this function
3911 is being called at the end of processing and this feature was
3912 not reported). The third argument may be NULL; if not NULL, it
3913 is a NUL-terminated string taken from the packet following
3914 this feature's name and an equals sign. */
3915 void (*func) (const struct protocol_feature *, enum packet_support,
3916 const char *);
3917
3918 /* The corresponding packet for this feature. Only used if
3919 FUNC is remote_supported_packet. */
3920 int packet;
3921};
3922
be2a5f71
DJ
3923static void
3924remote_supported_packet (const struct protocol_feature *feature,
3925 enum packet_support support,
3926 const char *argument)
3927{
3928 if (argument)
3929 {
3930 warning (_("Remote qSupported response supplied an unexpected value for"
3931 " \"%s\"."), feature->name);
3932 return;
3933 }
3934
4082afcc 3935 remote_protocol_packets[feature->packet].support = support;
be2a5f71 3936}
be2a5f71
DJ
3937
3938static void
3939remote_packet_size (const struct protocol_feature *feature,
3940 enum packet_support support, const char *value)
3941{
3942 struct remote_state *rs = get_remote_state ();
3943
3944 int packet_size;
3945 char *value_end;
3946
3947 if (support != PACKET_ENABLE)
3948 return;
3949
3950 if (value == NULL || *value == '\0')
3951 {
3952 warning (_("Remote target reported \"%s\" without a size."),
3953 feature->name);
3954 return;
3955 }
3956
3957 errno = 0;
3958 packet_size = strtol (value, &value_end, 16);
3959 if (errno != 0 || *value_end != '\0' || packet_size < 0)
3960 {
3961 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3962 feature->name, value);
3963 return;
3964 }
3965
3966 if (packet_size > MAX_REMOTE_PACKET_SIZE)
3967 {
3968 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3969 packet_size, MAX_REMOTE_PACKET_SIZE);
3970 packet_size = MAX_REMOTE_PACKET_SIZE;
3971 }
3972
3973 /* Record the new maximum packet size. */
3974 rs->explicit_packet_size = packet_size;
3975}
3976
dc473cfb 3977static const struct protocol_feature remote_protocol_features[] = {
0876f84a 3978 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 3979 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 3980 PACKET_qXfer_auxv },
c78fa86a
GB
3981 { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
3982 PACKET_qXfer_exec_file },
23181151
DJ
3983 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
3984 PACKET_qXfer_features },
cfa9d6d9
DJ
3985 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
3986 PACKET_qXfer_libraries },
2268b414
JK
3987 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
3988 PACKET_qXfer_libraries_svr4 },
ced63ec0 3989 { "augmented-libraries-svr4-read", PACKET_DISABLE,
4082afcc 3990 remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
fd79ecee 3991 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 3992 PACKET_qXfer_memory_map },
4de6483e
UW
3993 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
3994 PACKET_qXfer_spu_read },
3995 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
3996 PACKET_qXfer_spu_write },
07e059b5
VP
3997 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
3998 PACKET_qXfer_osdata },
dc146f7c
VP
3999 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
4000 PACKET_qXfer_threads },
b3b9301e
PA
4001 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
4002 PACKET_qXfer_traceframe_info },
89be2091
DJ
4003 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
4004 PACKET_QPassSignals },
9b224c5e
PA
4005 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
4006 PACKET_QProgramSignals },
a6f3e723
SL
4007 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
4008 PACKET_QStartNoAckMode },
4082afcc
PA
4009 { "multiprocess", PACKET_DISABLE, remote_supported_packet,
4010 PACKET_multiprocess_feature },
4011 { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
4aa995e1
PA
4012 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
4013 PACKET_qXfer_siginfo_read },
4014 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
4015 PACKET_qXfer_siginfo_write },
4082afcc 4016 { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
782b2b07 4017 PACKET_ConditionalTracepoints },
4082afcc 4018 { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
3788aec7 4019 PACKET_ConditionalBreakpoints },
4082afcc 4020 { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
d3ce09f5 4021 PACKET_BreakpointCommands },
4082afcc 4022 { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
7a697b8d 4023 PACKET_FastTracepoints },
4082afcc 4024 { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
0fb4aa4b 4025 PACKET_StaticTracepoints },
4082afcc 4026 {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
1e4d1764 4027 PACKET_InstallInTrace},
4082afcc
PA
4028 { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
4029 PACKET_DisconnectedTracing_feature },
40ab02ce
MS
4030 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
4031 PACKET_bc },
4032 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
4033 PACKET_bs },
409873ef
SS
4034 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
4035 PACKET_TracepointSource },
d914c394
SS
4036 { "QAllow", PACKET_DISABLE, remote_supported_packet,
4037 PACKET_QAllow },
4082afcc
PA
4038 { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
4039 PACKET_EnableDisableTracepoints_feature },
78d85199
YQ
4040 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
4041 PACKET_qXfer_fdpic },
169081d0
TG
4042 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
4043 PACKET_qXfer_uib },
03583c20
UW
4044 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
4045 PACKET_QDisableRandomization },
d1feda86 4046 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
f6f899bf
HAQ
4047 { "QTBuffer:size", PACKET_DISABLE,
4048 remote_supported_packet, PACKET_QTBuffer_size},
4082afcc 4049 { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
9accd112
MM
4050 { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4051 { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
4052 { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
f4abbc16
MM
4053 PACKET_qXfer_btrace },
4054 { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
d33501a5
MM
4055 PACKET_qXfer_btrace_conf },
4056 { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
f7e6eed5
PA
4057 PACKET_Qbtrace_conf_bts_size },
4058 { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
0a93529c 4059 { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
89245bc0
DB
4060 { "fork-events", PACKET_DISABLE, remote_supported_packet,
4061 PACKET_fork_event_feature },
4062 { "vfork-events", PACKET_DISABLE, remote_supported_packet,
4063 PACKET_vfork_event_feature },
be2a5f71
DJ
4064};
4065
c8d5aac9
L
4066static char *remote_support_xml;
4067
4068/* Register string appended to "xmlRegisters=" in qSupported query. */
4069
4070void
6e39997a 4071register_remote_support_xml (const char *xml)
c8d5aac9
L
4072{
4073#if defined(HAVE_LIBEXPAT)
4074 if (remote_support_xml == NULL)
c4f7c687 4075 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
4076 else
4077 {
4078 char *copy = xstrdup (remote_support_xml + 13);
4079 char *p = strtok (copy, ",");
4080
4081 do
4082 {
4083 if (strcmp (p, xml) == 0)
4084 {
4085 /* already there */
4086 xfree (copy);
4087 return;
4088 }
4089 }
4090 while ((p = strtok (NULL, ",")) != NULL);
4091 xfree (copy);
4092
94b0dee1
PA
4093 remote_support_xml = reconcat (remote_support_xml,
4094 remote_support_xml, ",", xml,
4095 (char *) NULL);
c8d5aac9
L
4096 }
4097#endif
4098}
4099
4100static char *
4101remote_query_supported_append (char *msg, const char *append)
4102{
4103 if (msg)
94b0dee1 4104 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
4105 else
4106 return xstrdup (append);
4107}
4108
be2a5f71
DJ
4109static void
4110remote_query_supported (void)
4111{
4112 struct remote_state *rs = get_remote_state ();
4113 char *next;
4114 int i;
4115 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
4116
4117 /* The packet support flags are handled differently for this packet
4118 than for most others. We treat an error, a disabled packet, and
4119 an empty response identically: any features which must be reported
4120 to be used will be automatically disabled. An empty buffer
4121 accomplishes this, since that is also the representation for a list
4122 containing no features. */
4123
4124 rs->buf[0] = 0;
4082afcc 4125 if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
be2a5f71 4126 {
c8d5aac9 4127 char *q = NULL;
94b0dee1 4128 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 4129
901f9912 4130 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9 4131
f7e6eed5
PA
4132 if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
4133 q = remote_query_supported_append (q, "swbreak+");
4134 if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
4135 q = remote_query_supported_append (q, "hwbreak+");
4136
c8d5aac9
L
4137 if (remote_support_xml)
4138 q = remote_query_supported_append (q, remote_support_xml);
4139
dde08ee1
PA
4140 q = remote_query_supported_append (q, "qRelocInsn+");
4141
89245bc0
DB
4142 if (rs->extended)
4143 {
4144 if (packet_set_cmd_state (PACKET_fork_event_feature)
4145 != AUTO_BOOLEAN_FALSE)
4146 q = remote_query_supported_append (q, "fork-events+");
4147 if (packet_set_cmd_state (PACKET_vfork_event_feature)
4148 != AUTO_BOOLEAN_FALSE)
4149 q = remote_query_supported_append (q, "vfork-events+");
4150 }
4151
dde08ee1
PA
4152 q = reconcat (q, "qSupported:", q, (char *) NULL);
4153 putpkt (q);
82f73884 4154
94b0dee1
PA
4155 do_cleanups (old_chain);
4156
be2a5f71
DJ
4157 getpkt (&rs->buf, &rs->buf_size, 0);
4158
4159 /* If an error occured, warn, but do not return - just reset the
4160 buffer to empty and go on to disable features. */
4161 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4162 == PACKET_ERROR)
4163 {
4164 warning (_("Remote failure reply: %s"), rs->buf);
4165 rs->buf[0] = 0;
4166 }
4167 }
4168
4169 memset (seen, 0, sizeof (seen));
4170
4171 next = rs->buf;
4172 while (*next)
4173 {
4174 enum packet_support is_supported;
4175 char *p, *end, *name_end, *value;
4176
4177 /* First separate out this item from the rest of the packet. If
4178 there's another item after this, we overwrite the separator
4179 (terminated strings are much easier to work with). */
4180 p = next;
4181 end = strchr (p, ';');
4182 if (end == NULL)
4183 {
4184 end = p + strlen (p);
4185 next = end;
4186 }
4187 else
4188 {
89be2091
DJ
4189 *end = '\0';
4190 next = end + 1;
4191
be2a5f71
DJ
4192 if (end == p)
4193 {
4194 warning (_("empty item in \"qSupported\" response"));
4195 continue;
4196 }
be2a5f71
DJ
4197 }
4198
4199 name_end = strchr (p, '=');
4200 if (name_end)
4201 {
4202 /* This is a name=value entry. */
4203 is_supported = PACKET_ENABLE;
4204 value = name_end + 1;
4205 *name_end = '\0';
4206 }
4207 else
4208 {
4209 value = NULL;
4210 switch (end[-1])
4211 {
4212 case '+':
4213 is_supported = PACKET_ENABLE;
4214 break;
4215
4216 case '-':
4217 is_supported = PACKET_DISABLE;
4218 break;
4219
4220 case '?':
4221 is_supported = PACKET_SUPPORT_UNKNOWN;
4222 break;
4223
4224 default:
3e43a32a
MS
4225 warning (_("unrecognized item \"%s\" "
4226 "in \"qSupported\" response"), p);
be2a5f71
DJ
4227 continue;
4228 }
4229 end[-1] = '\0';
4230 }
4231
4232 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4233 if (strcmp (remote_protocol_features[i].name, p) == 0)
4234 {
4235 const struct protocol_feature *feature;
4236
4237 seen[i] = 1;
4238 feature = &remote_protocol_features[i];
4239 feature->func (feature, is_supported, value);
4240 break;
4241 }
4242 }
4243
4244 /* If we increased the packet size, make sure to increase the global
4245 buffer size also. We delay this until after parsing the entire
4246 qSupported packet, because this is the same buffer we were
4247 parsing. */
4248 if (rs->buf_size < rs->explicit_packet_size)
4249 {
4250 rs->buf_size = rs->explicit_packet_size;
4251 rs->buf = xrealloc (rs->buf, rs->buf_size);
4252 }
4253
4254 /* Handle the defaults for unmentioned features. */
4255 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4256 if (!seen[i])
4257 {
4258 const struct protocol_feature *feature;
4259
4260 feature = &remote_protocol_features[i];
4261 feature->func (feature, feature->default_support, NULL);
4262 }
4263}
4264
78a095c3
JK
4265/* Remove any of the remote.c targets from target stack. Upper targets depend
4266 on it so remove them first. */
4267
4268static void
4269remote_unpush_target (void)
4270{
4271 pop_all_targets_above (process_stratum - 1);
4272}
be2a5f71 4273
c906108c 4274static void
014f9477 4275remote_open_1 (const char *name, int from_tty,
3e43a32a 4276 struct target_ops *target, int extended_p)
c906108c 4277{
d01949b6 4278 struct remote_state *rs = get_remote_state ();
a6f3e723 4279
c906108c 4280 if (name == 0)
8a3fe4f8 4281 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 4282 "serial device is attached to the remote system\n"
8a3fe4f8 4283 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 4284
23860348 4285 /* See FIXME above. */
c6ebd6cf 4286 if (!target_async_permitted)
92d1e331 4287 wait_forever_enabled_p = 1;
6426a772 4288
2d717e4f 4289 /* If we're connected to a running target, target_preopen will kill it.
78a095c3
JK
4290 Ask this question first, before target_preopen has a chance to kill
4291 anything. */
5d93a237 4292 if (rs->remote_desc != NULL && !have_inferiors ())
2d717e4f 4293 {
78a095c3
JK
4294 if (from_tty
4295 && !query (_("Already connected to a remote target. Disconnect? ")))
2d717e4f
DJ
4296 error (_("Still connected."));
4297 }
4298
78a095c3 4299 /* Here the possibly existing remote target gets unpushed. */
c906108c
SS
4300 target_preopen (from_tty);
4301
89be2091 4302 /* Make sure we send the passed signals list the next time we resume. */
747dc59d
TT
4303 xfree (rs->last_pass_packet);
4304 rs->last_pass_packet = NULL;
89be2091 4305
9b224c5e
PA
4306 /* Make sure we send the program signals list the next time we
4307 resume. */
5e4a05c4
TT
4308 xfree (rs->last_program_signals_packet);
4309 rs->last_program_signals_packet = NULL;
9b224c5e 4310
ad9a8f3f 4311 remote_fileio_reset ();
1dd41f16
NS
4312 reopen_exec_file ();
4313 reread_symbols ();
4314
5d93a237
TT
4315 rs->remote_desc = remote_serial_open (name);
4316 if (!rs->remote_desc)
c906108c
SS
4317 perror_with_name (name);
4318
4319 if (baud_rate != -1)
4320 {
5d93a237 4321 if (serial_setbaudrate (rs->remote_desc, baud_rate))
c906108c 4322 {
9b74d5d3
KB
4323 /* The requested speed could not be set. Error out to
4324 top level after closing remote_desc. Take care to
4325 set remote_desc to NULL to avoid closing remote_desc
4326 more than once. */
5d93a237
TT
4327 serial_close (rs->remote_desc);
4328 rs->remote_desc = NULL;
c906108c
SS
4329 perror_with_name (name);
4330 }
4331 }
4332
236af5e3 4333 serial_setparity (rs->remote_desc, serial_parity);
5d93a237 4334 serial_raw (rs->remote_desc);
c906108c
SS
4335
4336 /* If there is something sitting in the buffer we might take it as a
4337 response to a command, which would be bad. */
5d93a237 4338 serial_flush_input (rs->remote_desc);
c906108c
SS
4339
4340 if (from_tty)
4341 {
4342 puts_filtered ("Remote debugging using ");
4343 puts_filtered (name);
4344 puts_filtered ("\n");
4345 }
23860348 4346 push_target (target); /* Switch to using remote target now. */
c906108c 4347
74531fed
PA
4348 /* Register extra event sources in the event loop. */
4349 remote_async_inferior_event_token
4350 = create_async_event_handler (remote_async_inferior_event_handler,
4351 NULL);
5965e028 4352 rs->notif_state = remote_notif_state_allocate ();
74531fed 4353
be2a5f71
DJ
4354 /* Reset the target state; these things will be queried either by
4355 remote_query_supported or as they are needed. */
ca4f7f8b 4356 reset_all_packet_configs_support ();
74531fed 4357 rs->cached_wait_status = 0;
be2a5f71 4358 rs->explicit_packet_size = 0;
a6f3e723 4359 rs->noack_mode = 0;
82f73884 4360 rs->extended = extended_p;
e24a49d8 4361 rs->waiting_for_stop_reply = 0;
3a29589a 4362 rs->ctrlc_pending_p = 0;
802188a7 4363
47f8a51d
TT
4364 rs->general_thread = not_sent_ptid;
4365 rs->continue_thread = not_sent_ptid;
262e1174 4366 rs->remote_traceframe_number = -1;
c906108c 4367
9d1f7ab2 4368 /* Probe for ability to use "ThreadInfo" query, as required. */
b80fafe3
TT
4369 rs->use_threadinfo_query = 1;
4370 rs->use_threadextra_query = 1;
9d1f7ab2 4371
c6ebd6cf 4372 if (target_async_permitted)
92d1e331 4373 {
23860348 4374 /* With this target we start out by owning the terminal. */
92d1e331
DJ
4375 remote_async_terminal_ours_p = 1;
4376
4377 /* FIXME: cagney/1999-09-23: During the initial connection it is
4378 assumed that the target is already ready and able to respond to
0df8b418 4379 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 4380 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 4381 around this. Eventually a mechanism that allows
92d1e331 4382 wait_for_inferior() to expect/get timeouts will be
23860348 4383 implemented. */
92d1e331
DJ
4384 wait_forever_enabled_p = 0;
4385 }
4386
23860348 4387 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 4388 no_shared_libraries (NULL, 0);
f78f6cf1 4389
74531fed
PA
4390 /* Start afresh. */
4391 init_thread_list ();
4392
36918e70 4393 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
4394 target (we'd otherwise be in an inconsistent state) and then
4395 propogate the error on up the exception chain. This ensures that
4396 the caller doesn't stumble along blindly assuming that the
4397 function succeeded. The CLI doesn't have this problem but other
4398 UI's, such as MI do.
36918e70
AC
4399
4400 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4401 this function should return an error indication letting the
ce2826aa 4402 caller restore the previous state. Unfortunately the command
36918e70
AC
4403 ``target remote'' is directly wired to this function making that
4404 impossible. On a positive note, the CLI side of this problem has
4405 been fixed - the function set_cmd_context() makes it possible for
4406 all the ``target ....'' commands to share a common callback
4407 function. See cli-dump.c. */
109c3e39 4408 {
2d717e4f 4409
492d29ea 4410 TRY
04bd08de
TT
4411 {
4412 remote_start_remote (from_tty, target, extended_p);
4413 }
492d29ea 4414 CATCH (ex, RETURN_MASK_ALL)
109c3e39 4415 {
c8d104ad
PA
4416 /* Pop the partially set up target - unless something else did
4417 already before throwing the exception. */
5d93a237 4418 if (rs->remote_desc != NULL)
78a095c3 4419 remote_unpush_target ();
c6ebd6cf 4420 if (target_async_permitted)
109c3e39
AC
4421 wait_forever_enabled_p = 1;
4422 throw_exception (ex);
4423 }
492d29ea 4424 END_CATCH
109c3e39 4425 }
c906108c 4426
f4abbc16
MM
4427 remote_btrace_reset ();
4428
c6ebd6cf 4429 if (target_async_permitted)
92d1e331 4430 wait_forever_enabled_p = 1;
43ff13b4
JM
4431}
4432
c906108c
SS
4433/* This takes a program previously attached to and detaches it. After
4434 this is done, GDB can be used to debug some other program. We
4435 better not have left any breakpoints in the target program or it'll
4436 die when it hits one. */
4437
4438static void
52554a0e 4439remote_detach_1 (const char *args, int from_tty, int extended)
c906108c 4440{
82f73884 4441 int pid = ptid_get_pid (inferior_ptid);
d01949b6 4442 struct remote_state *rs = get_remote_state ();
c906108c
SS
4443
4444 if (args)
8a3fe4f8 4445 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 4446
2d717e4f
DJ
4447 if (!target_has_execution)
4448 error (_("No process to detach from."));
4449
7cee1e54
PA
4450 if (from_tty)
4451 {
4452 char *exec_file = get_exec_file (0);
4453 if (exec_file == NULL)
4454 exec_file = "";
4455 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4456 target_pid_to_str (pid_to_ptid (pid)));
4457 gdb_flush (gdb_stdout);
4458 }
4459
c906108c 4460 /* Tell the remote target to detach. */
82f73884 4461 if (remote_multi_process_p (rs))
bba74b36 4462 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
82f73884
PA
4463 else
4464 strcpy (rs->buf, "D");
4465
4ddda9b5
PA
4466 putpkt (rs->buf);
4467 getpkt (&rs->buf, &rs->buf_size, 0);
4468
82f73884
PA
4469 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4470 ;
4471 else if (rs->buf[0] == '\0')
4472 error (_("Remote doesn't know how to detach"));
4473 else
4ddda9b5 4474 error (_("Can't detach process."));
c906108c 4475
7cee1e54
PA
4476 if (from_tty && !extended)
4477 puts_filtered (_("Ending remote debugging.\n"));
82f73884 4478
82f73884 4479 target_mourn_inferior ();
2d717e4f
DJ
4480}
4481
4482static void
52554a0e 4483remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f
DJ
4484{
4485 remote_detach_1 (args, from_tty, 0);
4486}
4487
4488static void
52554a0e 4489extended_remote_detach (struct target_ops *ops, const char *args, int from_tty)
2d717e4f
DJ
4490{
4491 remote_detach_1 (args, from_tty, 1);
c906108c
SS
4492}
4493
6ad8ae5c
DJ
4494/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4495
43ff13b4 4496static void
fee354ee 4497remote_disconnect (struct target_ops *target, const char *args, int from_tty)
43ff13b4 4498{
43ff13b4 4499 if (args)
2d717e4f 4500 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 4501
2d717e4f 4502 /* Make sure we unpush even the extended remote targets; mourn
20f796c9 4503 won't do it. So call remote_mourn directly instead of
2d717e4f 4504 target_mourn_inferior. */
20f796c9 4505 remote_mourn (target);
2d717e4f 4506
43ff13b4
JM
4507 if (from_tty)
4508 puts_filtered ("Ending remote debugging.\n");
4509}
4510
2d717e4f
DJ
4511/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4512 be chatty about it. */
4513
4514static void
20f796c9
GB
4515extended_remote_attach (struct target_ops *target, const char *args,
4516 int from_tty)
2d717e4f
DJ
4517{
4518 struct remote_state *rs = get_remote_state ();
be86555c 4519 int pid;
96ef3384 4520 char *wait_status = NULL;
2d717e4f 4521
74164c56 4522 pid = parse_pid_to_attach (args);
2d717e4f 4523
74164c56
JK
4524 /* Remote PID can be freely equal to getpid, do not check it here the same
4525 way as in other targets. */
2d717e4f 4526
4082afcc 4527 if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
2d717e4f
DJ
4528 error (_("This target does not support attaching to a process"));
4529
7cee1e54
PA
4530 if (from_tty)
4531 {
4532 char *exec_file = get_exec_file (0);
4533
4534 if (exec_file)
4535 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4536 target_pid_to_str (pid_to_ptid (pid)));
4537 else
4538 printf_unfiltered (_("Attaching to %s\n"),
4539 target_pid_to_str (pid_to_ptid (pid)));
4540
4541 gdb_flush (gdb_stdout);
4542 }
4543
bba74b36 4544 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
4545 putpkt (rs->buf);
4546 getpkt (&rs->buf, &rs->buf_size, 0);
4547
4082afcc
PA
4548 switch (packet_ok (rs->buf,
4549 &remote_protocol_packets[PACKET_vAttach]))
2d717e4f 4550 {
4082afcc 4551 case PACKET_OK:
74531fed
PA
4552 if (!non_stop)
4553 {
4554 /* Save the reply for later. */
4555 wait_status = alloca (strlen (rs->buf) + 1);
4556 strcpy (wait_status, rs->buf);
4557 }
4558 else if (strcmp (rs->buf, "OK") != 0)
4559 error (_("Attaching to %s failed with: %s"),
4560 target_pid_to_str (pid_to_ptid (pid)),
4561 rs->buf);
4082afcc
PA
4562 break;
4563 case PACKET_UNKNOWN:
4564 error (_("This target does not support attaching to a process"));
4565 default:
4566 error (_("Attaching to %s failed"),
4567 target_pid_to_str (pid_to_ptid (pid)));
2d717e4f 4568 }
2d717e4f 4569
1b6e6f5c 4570 set_current_inferior (remote_add_inferior (0, pid, 1, 0));
bad34192 4571
2d717e4f 4572 inferior_ptid = pid_to_ptid (pid);
79d7f229 4573
bad34192
PA
4574 if (non_stop)
4575 {
4576 struct thread_info *thread;
79d7f229 4577
bad34192 4578 /* Get list of threads. */
e8032dde 4579 remote_update_thread_list (target);
82f73884 4580
bad34192
PA
4581 thread = first_thread_of_process (pid);
4582 if (thread)
4583 inferior_ptid = thread->ptid;
4584 else
4585 inferior_ptid = pid_to_ptid (pid);
4586
4587 /* Invalidate our notion of the remote current thread. */
47f8a51d 4588 record_currthread (rs, minus_one_ptid);
bad34192 4589 }
74531fed 4590 else
bad34192
PA
4591 {
4592 /* Now, if we have thread information, update inferior_ptid. */
4593 inferior_ptid = remote_current_thread (inferior_ptid);
4594
4595 /* Add the main thread to the thread list. */
4596 add_thread_silent (inferior_ptid);
4597 }
c0a2216e 4598
96ef3384
UW
4599 /* Next, if the target can specify a description, read it. We do
4600 this before anything involving memory or registers. */
4601 target_find_description ();
4602
74531fed
PA
4603 if (!non_stop)
4604 {
4605 /* Use the previously fetched status. */
4606 gdb_assert (wait_status != NULL);
4607
4608 if (target_can_async_p ())
4609 {
722247f1
YQ
4610 struct notif_event *reply
4611 = remote_notif_parse (&notif_client_stop, wait_status);
74531fed 4612
722247f1 4613 push_stop_reply ((struct stop_reply *) reply);
74531fed 4614
6a3753b3 4615 target_async (1);
74531fed
PA
4616 }
4617 else
4618 {
4619 gdb_assert (wait_status != NULL);
4620 strcpy (rs->buf, wait_status);
4621 rs->cached_wait_status = 1;
4622 }
4623 }
4624 else
4625 gdb_assert (wait_status == NULL);
2d717e4f
DJ
4626}
4627
b9c1d481
AS
4628/* Implementation of the to_post_attach method. */
4629
4630static void
4631extended_remote_post_attach (struct target_ops *ops, int pid)
4632{
4633 /* In certain cases GDB might not have had the chance to start
4634 symbol lookup up until now. This could happen if the debugged
4635 binary is not using shared libraries, the vsyscall page is not
4636 present (on Linux) and the binary itself hadn't changed since the
4637 debugging process was started. */
4638 if (symfile_objfile != NULL)
4639 remote_check_symbols();
4640}
4641
c906108c 4642\f
506fb367
DJ
4643/* Check for the availability of vCont. This function should also check
4644 the response. */
c906108c
SS
4645
4646static void
6d820c5c 4647remote_vcont_probe (struct remote_state *rs)
c906108c 4648{
2e9f7625 4649 char *buf;
6d820c5c 4650
2e9f7625
DJ
4651 strcpy (rs->buf, "vCont?");
4652 putpkt (rs->buf);
6d820c5c 4653 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 4654 buf = rs->buf;
c906108c 4655
506fb367 4656 /* Make sure that the features we assume are supported. */
61012eef 4657 if (startswith (buf, "vCont"))
506fb367
DJ
4658 {
4659 char *p = &buf[5];
4660 int support_s, support_S, support_c, support_C;
4661
4662 support_s = 0;
4663 support_S = 0;
4664 support_c = 0;
4665 support_C = 0;
d458bd84 4666 rs->supports_vCont.t = 0;
c1e36e3e 4667 rs->supports_vCont.r = 0;
506fb367
DJ
4668 while (p && *p == ';')
4669 {
4670 p++;
4671 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4672 support_s = 1;
4673 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4674 support_S = 1;
4675 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4676 support_c = 1;
4677 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4678 support_C = 1;
74531fed 4679 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
d458bd84 4680 rs->supports_vCont.t = 1;
c1e36e3e
PA
4681 else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
4682 rs->supports_vCont.r = 1;
506fb367
DJ
4683
4684 p = strchr (p, ';');
4685 }
c906108c 4686
506fb367
DJ
4687 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4688 BUF will make packet_ok disable the packet. */
4689 if (!support_s || !support_S || !support_c || !support_C)
4690 buf[0] = 0;
4691 }
c906108c 4692
444abaca 4693 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 4694}
c906108c 4695
0d8f58ca
PA
4696/* Helper function for building "vCont" resumptions. Write a
4697 resumption to P. ENDP points to one-passed-the-end of the buffer
4698 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4699 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4700 resumed thread should be single-stepped and/or signalled. If PTID
4701 equals minus_one_ptid, then all threads are resumed; if PTID
4702 represents a process, then all threads of the process are resumed;
4703 the thread to be stepped and/or signalled is given in the global
4704 INFERIOR_PTID. */
4705
4706static char *
4707append_resumption (char *p, char *endp,
2ea28649 4708 ptid_t ptid, int step, enum gdb_signal siggnal)
0d8f58ca
PA
4709{
4710 struct remote_state *rs = get_remote_state ();
4711
a493e3e2 4712 if (step && siggnal != GDB_SIGNAL_0)
0d8f58ca 4713 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
c1e36e3e
PA
4714 else if (step
4715 /* GDB is willing to range step. */
4716 && use_range_stepping
4717 /* Target supports range stepping. */
4718 && rs->supports_vCont.r
4719 /* We don't currently support range stepping multiple
4720 threads with a wildcard (though the protocol allows it,
4721 so stubs shouldn't make an active effort to forbid
4722 it). */
4723 && !(remote_multi_process_p (rs) && ptid_is_pid (ptid)))
4724 {
4725 struct thread_info *tp;
4726
4727 if (ptid_equal (ptid, minus_one_ptid))
4728 {
4729 /* If we don't know about the target thread's tid, then
4730 we're resuming magic_null_ptid (see caller). */
4731 tp = find_thread_ptid (magic_null_ptid);
4732 }
4733 else
4734 tp = find_thread_ptid (ptid);
4735 gdb_assert (tp != NULL);
4736
4737 if (tp->control.may_range_step)
4738 {
4739 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
4740
4741 p += xsnprintf (p, endp - p, ";r%s,%s",
4742 phex_nz (tp->control.step_range_start,
4743 addr_size),
4744 phex_nz (tp->control.step_range_end,
4745 addr_size));
4746 }
4747 else
4748 p += xsnprintf (p, endp - p, ";s");
4749 }
0d8f58ca
PA
4750 else if (step)
4751 p += xsnprintf (p, endp - p, ";s");
a493e3e2 4752 else if (siggnal != GDB_SIGNAL_0)
0d8f58ca
PA
4753 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4754 else
4755 p += xsnprintf (p, endp - p, ";c");
4756
4757 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4758 {
4759 ptid_t nptid;
4760
4761 /* All (-1) threads of process. */
ba348170 4762 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
0d8f58ca
PA
4763
4764 p += xsnprintf (p, endp - p, ":");
4765 p = write_ptid (p, endp, nptid);
4766 }
4767 else if (!ptid_equal (ptid, minus_one_ptid))
4768 {
4769 p += xsnprintf (p, endp - p, ":");
4770 p = write_ptid (p, endp, ptid);
4771 }
4772
4773 return p;
4774}
4775
e5ef252a
PA
4776/* Append a vCont continue-with-signal action for threads that have a
4777 non-zero stop signal. */
4778
4779static char *
4780append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4781{
4782 struct thread_info *thread;
4783
034f788c 4784 ALL_NON_EXITED_THREADS (thread)
e5ef252a
PA
4785 if (ptid_match (thread->ptid, ptid)
4786 && !ptid_equal (inferior_ptid, thread->ptid)
70509625 4787 && thread->suspend.stop_signal != GDB_SIGNAL_0)
e5ef252a
PA
4788 {
4789 p = append_resumption (p, endp, thread->ptid,
4790 0, thread->suspend.stop_signal);
4791 thread->suspend.stop_signal = GDB_SIGNAL_0;
4792 }
4793
4794 return p;
4795}
4796
506fb367
DJ
4797/* Resume the remote inferior by using a "vCont" packet. The thread
4798 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
4799 resumed thread should be single-stepped and/or signalled. If PTID
4800 equals minus_one_ptid, then all threads are resumed; the thread to
4801 be stepped and/or signalled is given in the global INFERIOR_PTID.
4802 This function returns non-zero iff it resumes the inferior.
44eaed12 4803
506fb367
DJ
4804 This function issues a strict subset of all possible vCont commands at the
4805 moment. */
44eaed12 4806
506fb367 4807static int
2ea28649 4808remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
506fb367
DJ
4809{
4810 struct remote_state *rs = get_remote_state ();
82f73884
PA
4811 char *p;
4812 char *endp;
44eaed12 4813
4082afcc 4814 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
6d820c5c 4815 remote_vcont_probe (rs);
44eaed12 4816
4082afcc 4817 if (packet_support (PACKET_vCont) == PACKET_DISABLE)
6d820c5c 4818 return 0;
44eaed12 4819
82f73884
PA
4820 p = rs->buf;
4821 endp = rs->buf + get_remote_packet_size ();
4822
506fb367
DJ
4823 /* If we could generate a wider range of packets, we'd have to worry
4824 about overflowing BUF. Should there be a generic
4825 "multi-part-packet" packet? */
4826
0d8f58ca
PA
4827 p += xsnprintf (p, endp - p, "vCont");
4828
79d7f229 4829 if (ptid_equal (ptid, magic_null_ptid))
c906108c 4830 {
79d7f229
PA
4831 /* MAGIC_NULL_PTID means that we don't have any active threads,
4832 so we don't have any TID numbers the inferior will
4833 understand. Make sure to only send forms that do not specify
4834 a TID. */
a9cbf802 4835 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 4836 }
0d8f58ca 4837 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 4838 {
0d8f58ca
PA
4839 /* Resume all threads (of all processes, or of a single
4840 process), with preference for INFERIOR_PTID. This assumes
4841 inferior_ptid belongs to the set of all threads we are about
4842 to resume. */
a493e3e2 4843 if (step || siggnal != GDB_SIGNAL_0)
82f73884 4844 {
0d8f58ca
PA
4845 /* Step inferior_ptid, with or without signal. */
4846 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 4847 }
0d8f58ca 4848
e5ef252a
PA
4849 /* Also pass down any pending signaled resumption for other
4850 threads not the current. */
4851 p = append_pending_thread_resumptions (p, endp, ptid);
4852
0d8f58ca 4853 /* And continue others without a signal. */
a493e3e2 4854 append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
c906108c
SS
4855 }
4856 else
506fb367
DJ
4857 {
4858 /* Scheduler locking; resume only PTID. */
a9cbf802 4859 append_resumption (p, endp, ptid, step, siggnal);
506fb367 4860 }
c906108c 4861
82f73884
PA
4862 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
4863 putpkt (rs->buf);
506fb367 4864
74531fed
PA
4865 if (non_stop)
4866 {
4867 /* In non-stop, the stub replies to vCont with "OK". The stop
4868 reply will be reported asynchronously by means of a `%Stop'
4869 notification. */
4870 getpkt (&rs->buf, &rs->buf_size, 0);
4871 if (strcmp (rs->buf, "OK") != 0)
4872 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
4873 }
4874
506fb367 4875 return 1;
c906108c 4876}
43ff13b4 4877
506fb367
DJ
4878/* Tell the remote machine to resume. */
4879
43ff13b4 4880static void
28439f5e 4881remote_resume (struct target_ops *ops,
2ea28649 4882 ptid_t ptid, int step, enum gdb_signal siggnal)
43ff13b4 4883{
d01949b6 4884 struct remote_state *rs = get_remote_state ();
2e9f7625 4885 char *buf;
43ff13b4 4886
722247f1
YQ
4887 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
4888 (explained in remote-notif.c:handle_notification) so
4889 remote_notif_process is not called. We need find a place where
4890 it is safe to start a 'vNotif' sequence. It is good to do it
4891 before resuming inferior, because inferior was stopped and no RSP
4892 traffic at that moment. */
4893 if (!non_stop)
5965e028 4894 remote_notif_process (rs->notif_state, &notif_client_stop);
722247f1 4895
b73be471 4896 rs->last_sent_signal = siggnal;
280ceea3 4897 rs->last_sent_step = step;
43ff13b4 4898
506fb367 4899 /* The vCont packet doesn't need to specify threads via Hc. */
40ab02ce
MS
4900 /* No reverse support (yet) for vCont. */
4901 if (execution_direction != EXEC_REVERSE)
4902 if (remote_vcont_resume (ptid, step, siggnal))
4903 goto done;
506fb367 4904
79d7f229
PA
4905 /* All other supported resume packets do use Hc, so set the continue
4906 thread. */
4907 if (ptid_equal (ptid, minus_one_ptid))
4908 set_continue_thread (any_thread_ptid);
506fb367 4909 else
79d7f229 4910 set_continue_thread (ptid);
506fb367 4911
2e9f7625 4912 buf = rs->buf;
b2175913
MS
4913 if (execution_direction == EXEC_REVERSE)
4914 {
4915 /* We don't pass signals to the target in reverse exec mode. */
a493e3e2 4916 if (info_verbose && siggnal != GDB_SIGNAL_0)
7ea6d463 4917 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
b2175913 4918 siggnal);
40ab02ce 4919
4082afcc 4920 if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
40ab02ce 4921 error (_("Remote reverse-step not supported."));
4082afcc 4922 if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
08c93ed9 4923 error (_("Remote reverse-continue not supported."));
40ab02ce 4924
b2175913
MS
4925 strcpy (buf, step ? "bs" : "bc");
4926 }
a493e3e2 4927 else if (siggnal != GDB_SIGNAL_0)
43ff13b4
JM
4928 {
4929 buf[0] = step ? 'S' : 'C';
c5aa993b 4930 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 4931 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
4932 buf[3] = '\0';
4933 }
4934 else
c5aa993b 4935 strcpy (buf, step ? "s" : "c");
506fb367 4936
44eaed12 4937 putpkt (buf);
43ff13b4 4938
75c99385 4939 done:
2acceee2 4940 /* We are about to start executing the inferior, let's register it
0df8b418
MS
4941 with the event loop. NOTE: this is the one place where all the
4942 execution commands end up. We could alternatively do this in each
23860348 4943 of the execution commands in infcmd.c. */
2acceee2
JM
4944 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4945 into infcmd.c in order to allow inferior function calls to work
23860348 4946 NOT asynchronously. */
362646f5 4947 if (target_can_async_p ())
6a3753b3 4948 target_async (1);
e24a49d8
PA
4949
4950 /* We've just told the target to resume. The remote server will
4951 wait for the inferior to stop, and then send a stop reply. In
4952 the mean time, we can't start another command/query ourselves
74531fed
PA
4953 because the stub wouldn't be ready to process it. This applies
4954 only to the base all-stop protocol, however. In non-stop (which
4955 only supports vCont), the stub replies with an "OK", and is
4956 immediate able to process further serial input. */
4957 if (!non_stop)
4958 rs->waiting_for_stop_reply = 1;
43ff13b4 4959}
c906108c 4960\f
43ff13b4
JM
4961
4962/* Set up the signal handler for SIGINT, while the target is
23860348 4963 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 4964static void
934b9bac 4965async_initialize_sigint_signal_handler (void)
43ff13b4 4966{
934b9bac 4967 signal (SIGINT, async_handle_remote_sigint);
43ff13b4
JM
4968}
4969
23860348 4970/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 4971static void
934b9bac 4972async_handle_remote_sigint (int sig)
43ff13b4 4973{
934b9bac 4974 signal (sig, async_handle_remote_sigint_twice);
b2ee242b
PA
4975 /* Note we need to go through gdb_call_async_signal_handler in order
4976 to wake up the event loop on Windows. */
4977 gdb_call_async_signal_handler (async_sigint_remote_token, 0);
43ff13b4
JM
4978}
4979
4980/* Signal handler for SIGINT, installed after SIGINT has already been
4981 sent once. It will take effect the second time that the user sends
23860348 4982 a ^C. */
43ff13b4 4983static void
934b9bac 4984async_handle_remote_sigint_twice (int sig)
43ff13b4 4985{
934b9bac 4986 signal (sig, async_handle_remote_sigint);
b2ee242b
PA
4987 /* See note in async_handle_remote_sigint. */
4988 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 0);
43ff13b4
JM
4989}
4990
6426a772 4991/* Perform the real interruption of the target execution, in response
23860348 4992 to a ^C. */
c5aa993b 4993static void
fba45db2 4994async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
4995{
4996 if (remote_debug)
248fd3bf 4997 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
43ff13b4 4998
94cc34af 4999 target_stop (inferior_ptid);
43ff13b4
JM
5000}
5001
0df8b418 5002/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 5003 up on the target alltogether. */
47e1ce27 5004static void
fba45db2 5005async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 5006{
2df3850c 5007 if (remote_debug)
248fd3bf 5008 fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
b803fb0f
DJ
5009
5010 interrupt_query ();
43ff13b4
JM
5011}
5012
5013/* Reinstall the usual SIGINT handlers, after the target has
23860348 5014 stopped. */
6426a772 5015static void
934b9bac 5016async_cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
5017{
5018 signal (SIGINT, handle_sigint);
43ff13b4
JM
5019}
5020
c906108c
SS
5021/* Send ^C to target to halt it. Target will respond, and send us a
5022 packet. */
507f3c78 5023static void (*ofunc) (int);
c906108c 5024
0df8b418
MS
5025/* The command line interface's stop routine. This function is installed
5026 as a signal handler for SIGINT. The first time a user requests a
5027 stop, we call remote_stop to send a break or ^C. If there is no
7a292a7a 5028 response from the target (it didn't stop when the user requested it),
23860348 5029 we ask the user if he'd like to detach from the target. */
c906108c 5030static void
934b9bac 5031sync_remote_interrupt (int signo)
c906108c 5032{
23860348 5033 /* If this doesn't work, try more severe steps. */
934b9bac 5034 signal (signo, sync_remote_interrupt_twice);
7a292a7a 5035
934b9bac 5036 gdb_call_async_signal_handler (async_sigint_remote_token, 1);
7a292a7a
SS
5037}
5038
5039/* The user typed ^C twice. */
5040
5041static void
934b9bac 5042sync_remote_interrupt_twice (int signo)
7a292a7a
SS
5043{
5044 signal (signo, ofunc);
934b9bac
JK
5045 gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1);
5046 signal (signo, sync_remote_interrupt);
c906108c 5047}
7a292a7a 5048
74531fed
PA
5049/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5050 thread, all threads of a remote process, or all threads of all
5051 processes. */
5052
5053static void
5054remote_stop_ns (ptid_t ptid)
5055{
5056 struct remote_state *rs = get_remote_state ();
5057 char *p = rs->buf;
5058 char *endp = rs->buf + get_remote_packet_size ();
74531fed 5059
4082afcc 5060 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
74531fed
PA
5061 remote_vcont_probe (rs);
5062
d458bd84 5063 if (!rs->supports_vCont.t)
74531fed
PA
5064 error (_("Remote server does not support stopping threads"));
5065
f91d3df5
PA
5066 if (ptid_equal (ptid, minus_one_ptid)
5067 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
5068 p += xsnprintf (p, endp - p, "vCont;t");
5069 else
5070 {
5071 ptid_t nptid;
5072
74531fed
PA
5073 p += xsnprintf (p, endp - p, "vCont;t:");
5074
5075 if (ptid_is_pid (ptid))
5076 /* All (-1) threads of process. */
ba348170 5077 nptid = ptid_build (ptid_get_pid (ptid), -1, 0);
74531fed
PA
5078 else
5079 {
5080 /* Small optimization: if we already have a stop reply for
5081 this thread, no use in telling the stub we want this
5082 stopped. */
5083 if (peek_stop_reply (ptid))
5084 return;
5085
5086 nptid = ptid;
5087 }
5088
a9cbf802 5089 write_ptid (p, endp, nptid);
74531fed
PA
5090 }
5091
5092 /* In non-stop, we get an immediate OK reply. The stop reply will
5093 come in asynchronously by notification. */
5094 putpkt (rs->buf);
5095 getpkt (&rs->buf, &rs->buf_size, 0);
5096 if (strcmp (rs->buf, "OK") != 0)
5097 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
5098}
5099
5100/* All-stop version of target_stop. Sends a break or a ^C to stop the
5101 remote target. It is undefined which thread of which process
5102 reports the stop. */
5103
5104static void
5105remote_stop_as (ptid_t ptid)
5106{
5107 struct remote_state *rs = get_remote_state ();
5108
3a29589a
DJ
5109 rs->ctrlc_pending_p = 1;
5110
74531fed
PA
5111 /* If the inferior is stopped already, but the core didn't know
5112 about it yet, just ignore the request. The cached wait status
5113 will be collected in remote_wait. */
5114 if (rs->cached_wait_status)
5115 return;
5116
9a7071a8
JB
5117 /* Send interrupt_sequence to remote target. */
5118 send_interrupt_sequence ();
74531fed
PA
5119}
5120
0df8b418 5121/* This is the generic stop called via the target vector. When a target
7a292a7a 5122 interrupt is requested, either by the command line or the GUI, we
23860348 5123 will eventually end up here. */
74531fed 5124
c906108c 5125static void
1eab8a48 5126remote_stop (struct target_ops *self, ptid_t ptid)
c906108c 5127{
7a292a7a 5128 if (remote_debug)
0f71a2f6 5129 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 5130
74531fed
PA
5131 if (non_stop)
5132 remote_stop_ns (ptid);
c906108c 5133 else
74531fed 5134 remote_stop_as (ptid);
c906108c
SS
5135}
5136
5137/* Ask the user what to do when an interrupt is received. */
5138
5139static void
fba45db2 5140interrupt_query (void)
c906108c
SS
5141{
5142 target_terminal_ours ();
5143
d9d41e78 5144 if (target_is_async_p ())
c906108c 5145 {
74531fed 5146 signal (SIGINT, handle_sigint);
039e3c22 5147 quit ();
c906108c 5148 }
74531fed
PA
5149 else
5150 {
9e2f0ad4
HZ
5151 if (query (_("Interrupted while waiting for the program.\n\
5152Give up (and stop debugging it)? ")))
74531fed 5153 {
78a095c3 5154 remote_unpush_target ();
039e3c22 5155 quit ();
74531fed
PA
5156 }
5157 }
c906108c
SS
5158
5159 target_terminal_inferior ();
5160}
5161
6426a772
JM
5162/* Enable/disable target terminal ownership. Most targets can use
5163 terminal groups to control terminal ownership. Remote targets are
5164 different in that explicit transfer of ownership to/from GDB/target
23860348 5165 is required. */
6426a772
JM
5166
5167static void
d2f640d4 5168remote_terminal_inferior (struct target_ops *self)
6426a772 5169{
c6ebd6cf 5170 if (!target_async_permitted)
75c99385
PA
5171 /* Nothing to do. */
5172 return;
5173
d9d2d8b6
PA
5174 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5175 idempotent. The event-loop GDB talking to an asynchronous target
5176 with a synchronous command calls this function from both
5177 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5178 transfer the terminal to the target when it shouldn't this guard
5179 can go away. */
6426a772
JM
5180 if (!remote_async_terminal_ours_p)
5181 return;
5182 delete_file_handler (input_fd);
5183 remote_async_terminal_ours_p = 0;
934b9bac 5184 async_initialize_sigint_signal_handler ();
6426a772
JM
5185 /* NOTE: At this point we could also register our selves as the
5186 recipient of all input. Any characters typed could then be
23860348 5187 passed on down to the target. */
6426a772
JM
5188}
5189
5190static void
e3594fd1 5191remote_terminal_ours (struct target_ops *self)
6426a772 5192{
c6ebd6cf 5193 if (!target_async_permitted)
75c99385
PA
5194 /* Nothing to do. */
5195 return;
5196
5197 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
5198 if (remote_async_terminal_ours_p)
5199 return;
934b9bac 5200 async_cleanup_sigint_signal_handler (NULL);
6426a772
JM
5201 add_file_handler (input_fd, stdin_event_handler, 0);
5202 remote_async_terminal_ours_p = 1;
5203}
5204
176a6961 5205static void
917317f4 5206remote_console_output (char *msg)
c906108c
SS
5207{
5208 char *p;
5209
c5aa993b 5210 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
5211 {
5212 char tb[2];
5213 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 5214
c906108c
SS
5215 tb[0] = c;
5216 tb[1] = 0;
43ff13b4 5217 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 5218 }
00db5b94
PA
5219 gdb_flush (gdb_stdtarg);
5220}
74531fed
PA
5221
5222typedef struct cached_reg
5223{
5224 int num;
5225 gdb_byte data[MAX_REGISTER_SIZE];
5226} cached_reg_t;
5227
5228DEF_VEC_O(cached_reg_t);
5229
722247f1 5230typedef struct stop_reply
74531fed 5231{
722247f1 5232 struct notif_event base;
74531fed 5233
722247f1 5234 /* The identifier of the thread about this event */
74531fed
PA
5235 ptid_t ptid;
5236
340e3c99 5237 /* The remote state this event is associated with. When the remote
bcc75809
YQ
5238 connection, represented by a remote_state object, is closed,
5239 all the associated stop_reply events should be released. */
5240 struct remote_state *rs;
5241
74531fed
PA
5242 struct target_waitstatus ws;
5243
15148d6a
PA
5244 /* Expedited registers. This makes remote debugging a bit more
5245 efficient for those targets that provide critical registers as
5246 part of their normal status mechanism (as another roundtrip to
5247 fetch them is avoided). */
74531fed
PA
5248 VEC(cached_reg_t) *regcache;
5249
f7e6eed5
PA
5250 enum target_stop_reason stop_reason;
5251
74531fed
PA
5252 CORE_ADDR watch_data_address;
5253
dc146f7c 5254 int core;
722247f1 5255} *stop_reply_p;
a744cf53 5256
722247f1
YQ
5257DECLARE_QUEUE_P (stop_reply_p);
5258DEFINE_QUEUE_P (stop_reply_p);
5259/* The list of already fetched and acknowledged stop events. This
5260 queue is used for notification Stop, and other notifications
5261 don't need queue for their events, because the notification events
5262 of Stop can't be consumed immediately, so that events should be
5263 queued first, and be consumed by remote_wait_{ns,as} one per
5264 time. Other notifications can consume their events immediately,
5265 so queue is not needed for them. */
5266static QUEUE (stop_reply_p) *stop_reply_queue;
74531fed
PA
5267
5268static void
5269stop_reply_xfree (struct stop_reply *r)
5270{
f48ff2a7 5271 notif_event_xfree ((struct notif_event *) r);
c906108c
SS
5272}
5273
722247f1
YQ
5274static void
5275remote_notif_stop_parse (struct notif_client *self, char *buf,
5276 struct notif_event *event)
5277{
5278 remote_parse_stop_reply (buf, (struct stop_reply *) event);
5279}
5280
5281static void
5282remote_notif_stop_ack (struct notif_client *self, char *buf,
5283 struct notif_event *event)
5284{
5285 struct stop_reply *stop_reply = (struct stop_reply *) event;
5286
5287 /* acknowledge */
5288 putpkt ((char *) self->ack_command);
5289
5290 if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5291 /* We got an unknown stop reply. */
5292 error (_("Unknown stop reply"));
5293
5294 push_stop_reply (stop_reply);
5295}
5296
5297static int
5298remote_notif_stop_can_get_pending_events (struct notif_client *self)
5299{
5300 /* We can't get pending events in remote_notif_process for
5301 notification stop, and we have to do this in remote_wait_ns
5302 instead. If we fetch all queued events from stub, remote stub
5303 may exit and we have no chance to process them back in
5304 remote_wait_ns. */
5305 mark_async_event_handler (remote_async_inferior_event_token);
5306 return 0;
5307}
5308
5309static void
5310stop_reply_dtr (struct notif_event *event)
5311{
5312 struct stop_reply *r = (struct stop_reply *) event;
5313
5314 VEC_free (cached_reg_t, r->regcache);
5315}
5316
5317static struct notif_event *
5318remote_notif_stop_alloc_reply (void)
5319{
5320 struct notif_event *r
70ba0933 5321 = (struct notif_event *) XNEW (struct stop_reply);
722247f1
YQ
5322
5323 r->dtr = stop_reply_dtr;
5324
5325 return r;
5326}
5327
5328/* A client of notification Stop. */
5329
5330struct notif_client notif_client_stop =
5331{
5332 "Stop",
5333 "vStopped",
5334 remote_notif_stop_parse,
5335 remote_notif_stop_ack,
5336 remote_notif_stop_can_get_pending_events,
5337 remote_notif_stop_alloc_reply,
f48ff2a7 5338 REMOTE_NOTIF_STOP,
722247f1
YQ
5339};
5340
5341/* A parameter to pass data in and out. */
5342
5343struct queue_iter_param
5344{
5345 void *input;
5346 struct stop_reply *output;
5347};
5348
f48ff2a7
YQ
5349/* Remove stop replies in the queue if its pid is equal to the given
5350 inferior's pid. */
722247f1
YQ
5351
5352static int
f48ff2a7
YQ
5353remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q,
5354 QUEUE_ITER (stop_reply_p) *iter,
5355 stop_reply_p event,
5356 void *data)
722247f1
YQ
5357{
5358 struct queue_iter_param *param = data;
5359 struct inferior *inf = param->input;
5360
f48ff2a7 5361 if (ptid_get_pid (event->ptid) == inf->pid)
722247f1
YQ
5362 {
5363 stop_reply_xfree (event);
5364 QUEUE_remove_elem (stop_reply_p, q, iter);
5365 }
5366
5367 return 1;
5368}
5369
f48ff2a7 5370/* Discard all pending stop replies of inferior INF. */
c906108c 5371
74531fed 5372static void
5f4cf0bb 5373discard_pending_stop_replies (struct inferior *inf)
c906108c 5374{
722247f1
YQ
5375 int i;
5376 struct queue_iter_param param;
f48ff2a7
YQ
5377 struct stop_reply *reply;
5378 struct remote_state *rs = get_remote_state ();
5379 struct remote_notif_state *rns = rs->notif_state;
5380
5381 /* This function can be notified when an inferior exists. When the
5382 target is not remote, the notification state is NULL. */
5383 if (rs->remote_desc == NULL)
5384 return;
5385
5386 reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
c906108c 5387
74531fed 5388 /* Discard the in-flight notification. */
f48ff2a7 5389 if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid)
74531fed 5390 {
722247f1 5391 stop_reply_xfree (reply);
f48ff2a7 5392 rns->pending_event[notif_client_stop.id] = NULL;
74531fed 5393 }
c906108c 5394
722247f1
YQ
5395 param.input = inf;
5396 param.output = NULL;
74531fed
PA
5397 /* Discard the stop replies we have already pulled with
5398 vStopped. */
722247f1 5399 QUEUE_iterate (stop_reply_p, stop_reply_queue,
f48ff2a7
YQ
5400 remove_stop_reply_for_inferior, &param);
5401}
5402
bcc75809
YQ
5403/* If its remote state is equal to the given remote state,
5404 remove EVENT from the stop reply queue. */
5405
5406static int
5407remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q,
5408 QUEUE_ITER (stop_reply_p) *iter,
5409 stop_reply_p event,
5410 void *data)
5411{
5412 struct queue_iter_param *param = data;
5413 struct remote_state *rs = param->input;
5414
5415 if (event->rs == rs)
5416 {
5417 stop_reply_xfree (event);
5418 QUEUE_remove_elem (stop_reply_p, q, iter);
5419 }
5420
5421 return 1;
5422}
5423
5424/* Discard the stop replies for RS in stop_reply_queue. */
f48ff2a7
YQ
5425
5426static void
bcc75809 5427discard_pending_stop_replies_in_queue (struct remote_state *rs)
f48ff2a7
YQ
5428{
5429 struct queue_iter_param param;
5430
bcc75809 5431 param.input = rs;
f48ff2a7
YQ
5432 param.output = NULL;
5433 /* Discard the stop replies we have already pulled with
5434 vStopped. */
5435 QUEUE_iterate (stop_reply_p, stop_reply_queue,
bcc75809 5436 remove_stop_reply_of_remote_state, &param);
722247f1 5437}
74531fed 5438
722247f1
YQ
5439/* A parameter to pass data in and out. */
5440
5441static int
5442remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5443 QUEUE_ITER (stop_reply_p) *iter,
5444 stop_reply_p event,
5445 void *data)
5446{
5447 struct queue_iter_param *param = data;
5448 ptid_t *ptid = param->input;
5449
5450 if (ptid_match (event->ptid, *ptid))
5451 {
5452 param->output = event;
5453 QUEUE_remove_elem (stop_reply_p, q, iter);
5454 return 0;
c8e38a49 5455 }
722247f1
YQ
5456
5457 return 1;
74531fed 5458}
43ff13b4 5459
722247f1
YQ
5460/* Remove the first reply in 'stop_reply_queue' which matches
5461 PTID. */
2e9f7625 5462
722247f1
YQ
5463static struct stop_reply *
5464remote_notif_remove_queued_reply (ptid_t ptid)
74531fed 5465{
722247f1
YQ
5466 struct queue_iter_param param;
5467
5468 param.input = &ptid;
5469 param.output = NULL;
5470
5471 QUEUE_iterate (stop_reply_p, stop_reply_queue,
5472 remote_notif_remove_once_on_match, &param);
5473 if (notif_debug)
5474 fprintf_unfiltered (gdb_stdlog,
5475 "notif: discard queued event: 'Stop' in %s\n",
5476 target_pid_to_str (ptid));
a744cf53 5477
722247f1 5478 return param.output;
74531fed 5479}
75c99385 5480
74531fed
PA
5481/* Look for a queued stop reply belonging to PTID. If one is found,
5482 remove it from the queue, and return it. Returns NULL if none is
5483 found. If there are still queued events left to process, tell the
5484 event loop to get back to target_wait soon. */
e24a49d8 5485
74531fed
PA
5486static struct stop_reply *
5487queued_stop_reply (ptid_t ptid)
5488{
722247f1 5489 struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
74531fed 5490
722247f1 5491 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed
PA
5492 /* There's still at least an event left. */
5493 mark_async_event_handler (remote_async_inferior_event_token);
5494
722247f1 5495 return r;
74531fed
PA
5496}
5497
5498/* Push a fully parsed stop reply in the stop reply queue. Since we
5499 know that we now have at least one queued event left to pass to the
5500 core side, tell the event loop to get back to target_wait soon. */
5501
5502static void
5503push_stop_reply (struct stop_reply *new_event)
5504{
722247f1 5505 QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
74531fed 5506
722247f1
YQ
5507 if (notif_debug)
5508 fprintf_unfiltered (gdb_stdlog,
5509 "notif: push 'Stop' %s to queue %d\n",
5510 target_pid_to_str (new_event->ptid),
5511 QUEUE_length (stop_reply_p,
5512 stop_reply_queue));
74531fed
PA
5513
5514 mark_async_event_handler (remote_async_inferior_event_token);
5515}
5516
722247f1
YQ
5517static int
5518stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5519 QUEUE_ITER (stop_reply_p) *iter,
5520 struct stop_reply *event,
5521 void *data)
5522{
5523 ptid_t *ptid = data;
5524
5525 return !(ptid_equal (*ptid, event->ptid)
5526 && event->ws.kind == TARGET_WAITKIND_STOPPED);
5527}
5528
74531fed
PA
5529/* Returns true if we have a stop reply for PTID. */
5530
5531static int
5532peek_stop_reply (ptid_t ptid)
5533{
722247f1
YQ
5534 return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5535 stop_reply_match_ptid_and_ws, &ptid);
74531fed
PA
5536}
5537
1f10ba14
PA
5538/* Skip PACKET until the next semi-colon (or end of string). */
5539
5540static char *
5541skip_to_semicolon (char *p)
5542{
5543 while (*p != '\0' && *p != ';')
5544 p++;
5545 return p;
5546}
5547
74531fed
PA
5548/* Parse the stop reply in BUF. Either the function succeeds, and the
5549 result is stored in EVENT, or throws an error. */
5550
5551static void
5552remote_parse_stop_reply (char *buf, struct stop_reply *event)
5553{
5554 struct remote_arch_state *rsa = get_remote_arch_state ();
5555 ULONGEST addr;
5556 char *p;
5557
5558 event->ptid = null_ptid;
bcc75809 5559 event->rs = get_remote_state ();
74531fed
PA
5560 event->ws.kind = TARGET_WAITKIND_IGNORE;
5561 event->ws.value.integer = 0;
f7e6eed5 5562 event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed 5563 event->regcache = NULL;
dc146f7c 5564 event->core = -1;
74531fed
PA
5565
5566 switch (buf[0])
5567 {
5568 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
5569 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5570 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5571 ss = signal number
5572 n... = register number
5573 r... = register contents
5574 */
5575
5576 p = &buf[3]; /* after Txx */
5577 while (*p)
5578 {
5579 char *p1;
cea39f65 5580 int fieldsize;
43ff13b4 5581
1f10ba14
PA
5582 p1 = strchr (p, ':');
5583 if (p1 == NULL)
5584 error (_("Malformed packet(a) (missing colon): %s\n\
5585Packet: '%s'\n"),
5586 p, buf);
5587 if (p == p1)
5588 error (_("Malformed packet(a) (missing register number): %s\n\
5589Packet: '%s'\n"),
5590 p, buf);
3c3bea1c 5591
1f10ba14
PA
5592 /* Some "registers" are actually extended stop information.
5593 Note if you're adding a new entry here: GDB 7.9 and
5594 earlier assume that all register "numbers" that start
5595 with an hex digit are real register numbers. Make sure
5596 the server only sends such a packet if it knows the
5597 client understands it. */
c8e38a49 5598
1f10ba14
PA
5599 if (strncmp (p, "thread", p1 - p) == 0)
5600 event->ptid = read_ptid (++p1, &p);
5601 else if ((strncmp (p, "watch", p1 - p) == 0)
5602 || (strncmp (p, "rwatch", p1 - p) == 0)
5603 || (strncmp (p, "awatch", p1 - p) == 0))
cea39f65 5604 {
f7e6eed5 5605 event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
1f10ba14
PA
5606 p = unpack_varlen_hex (++p1, &addr);
5607 event->watch_data_address = (CORE_ADDR) addr;
cea39f65 5608 }
f7e6eed5
PA
5609 else if (strncmp (p, "swbreak", p1 - p) == 0)
5610 {
5611 event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
5612
5613 /* Make sure the stub doesn't forget to indicate support
5614 with qSupported. */
5615 if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
5616 error (_("Unexpected swbreak stop reason"));
5617
5618 /* The value part is documented as "must be empty",
5619 though we ignore it, in case we ever decide to make
5620 use of it in a backward compatible way. */
5621 p = skip_to_semicolon (p1 + 1);
5622 }
5623 else if (strncmp (p, "hwbreak", p1 - p) == 0)
5624 {
5625 event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
5626
5627 /* Make sure the stub doesn't forget to indicate support
5628 with qSupported. */
5629 if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
5630 error (_("Unexpected hwbreak stop reason"));
5631
5632 /* See above. */
5633 p = skip_to_semicolon (p1 + 1);
5634 }
1f10ba14 5635 else if (strncmp (p, "library", p1 - p) == 0)
cea39f65 5636 {
1f10ba14
PA
5637 event->ws.kind = TARGET_WAITKIND_LOADED;
5638 p = skip_to_semicolon (p1 + 1);
5639 }
5640 else if (strncmp (p, "replaylog", p1 - p) == 0)
5641 {
5642 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
5643 /* p1 will indicate "begin" or "end", but it makes
5644 no difference for now, so ignore it. */
5645 p = skip_to_semicolon (p1 + 1);
5646 }
5647 else if (strncmp (p, "core", p1 - p) == 0)
5648 {
5649 ULONGEST c;
a744cf53 5650
1f10ba14
PA
5651 p = unpack_varlen_hex (++p1, &c);
5652 event->core = c;
cea39f65
MS
5653 }
5654 else
5655 {
1f10ba14
PA
5656 ULONGEST pnum;
5657 char *p_temp;
5658
5659 /* Maybe a real ``P'' register number. */
5660 p_temp = unpack_varlen_hex (p, &pnum);
5661 /* If the first invalid character is the colon, we got a
5662 register number. Otherwise, it's an unknown stop
5663 reason. */
5664 if (p_temp == p1)
5665 {
5666 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5667 cached_reg_t cached_reg;
43ff13b4 5668
1f10ba14
PA
5669 if (reg == NULL)
5670 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 5671Packet: '%s'\n"),
1f10ba14 5672 hex_string (pnum), p, buf);
c8e38a49 5673
1f10ba14 5674 cached_reg.num = reg->regnum;
4100683b 5675
1f10ba14
PA
5676 p = p1 + 1;
5677 fieldsize = hex2bin (p, cached_reg.data,
5678 register_size (target_gdbarch (),
5679 reg->regnum));
5680 p += 2 * fieldsize;
5681 if (fieldsize < register_size (target_gdbarch (),
5682 reg->regnum))
5683 warning (_("Remote reply is too short: %s"), buf);
74531fed 5684
1f10ba14
PA
5685 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5686 }
5687 else
5688 {
5689 /* Not a number. Silently skip unknown optional
5690 info. */
5691 p = skip_to_semicolon (p1 + 1);
5692 }
cea39f65 5693 }
c8e38a49 5694
cea39f65
MS
5695 if (*p != ';')
5696 error (_("Remote register badly formatted: %s\nhere: %s"),
5697 buf, p);
5698 ++p;
5699 }
5b5596ff
PA
5700
5701 if (event->ws.kind != TARGET_WAITKIND_IGNORE)
5702 break;
5703
c8e38a49
PA
5704 /* fall through */
5705 case 'S': /* Old style status, just signal only. */
3a09da41
PA
5706 {
5707 int sig;
5708
5709 event->ws.kind = TARGET_WAITKIND_STOPPED;
5710 sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
5711 if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
5712 event->ws.value.sig = (enum gdb_signal) sig;
5713 else
5714 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
5715 }
c8e38a49
PA
5716 break;
5717 case 'W': /* Target exited. */
5718 case 'X':
5719 {
5720 char *p;
5721 int pid;
5722 ULONGEST value;
82f73884 5723
c8e38a49
PA
5724 /* GDB used to accept only 2 hex chars here. Stubs should
5725 only send more if they detect GDB supports multi-process
5726 support. */
5727 p = unpack_varlen_hex (&buf[1], &value);
82f73884 5728
c8e38a49
PA
5729 if (buf[0] == 'W')
5730 {
5731 /* The remote process exited. */
74531fed
PA
5732 event->ws.kind = TARGET_WAITKIND_EXITED;
5733 event->ws.value.integer = value;
c8e38a49
PA
5734 }
5735 else
5736 {
5737 /* The remote process exited with a signal. */
74531fed 5738 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
3a09da41
PA
5739 if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
5740 event->ws.value.sig = (enum gdb_signal) value;
5741 else
5742 event->ws.value.sig = GDB_SIGNAL_UNKNOWN;
c8e38a49 5743 }
82f73884 5744
c8e38a49
PA
5745 /* If no process is specified, assume inferior_ptid. */
5746 pid = ptid_get_pid (inferior_ptid);
5747 if (*p == '\0')
5748 ;
5749 else if (*p == ';')
5750 {
5751 p++;
5752
0b24eb2d 5753 if (*p == '\0')
82f73884 5754 ;
61012eef 5755 else if (startswith (p, "process:"))
82f73884 5756 {
c8e38a49 5757 ULONGEST upid;
a744cf53 5758
c8e38a49
PA
5759 p += sizeof ("process:") - 1;
5760 unpack_varlen_hex (p, &upid);
5761 pid = upid;
82f73884
PA
5762 }
5763 else
5764 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 5765 }
c8e38a49
PA
5766 else
5767 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
5768 event->ptid = pid_to_ptid (pid);
5769 }
5770 break;
5771 }
5772
5773 if (non_stop && ptid_equal (event->ptid, null_ptid))
5774 error (_("No process or thread specified in stop reply: %s"), buf);
5775}
5776
722247f1
YQ
5777/* When the stub wants to tell GDB about a new notification reply, it
5778 sends a notification (%Stop, for example). Those can come it at
5779 any time, hence, we have to make sure that any pending
5780 putpkt/getpkt sequence we're making is finished, before querying
5781 the stub for more events with the corresponding ack command
5782 (vStopped, for example). E.g., if we started a vStopped sequence
5783 immediately upon receiving the notification, something like this
5784 could happen:
74531fed
PA
5785
5786 1.1) --> Hg 1
5787 1.2) <-- OK
5788 1.3) --> g
5789 1.4) <-- %Stop
5790 1.5) --> vStopped
5791 1.6) <-- (registers reply to step #1.3)
5792
5793 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5794 query.
5795
796cb314 5796 To solve this, whenever we parse a %Stop notification successfully,
74531fed
PA
5797 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5798 doing whatever we were doing:
5799
5800 2.1) --> Hg 1
5801 2.2) <-- OK
5802 2.3) --> g
5803 2.4) <-- %Stop
5804 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5805 2.5) <-- (registers reply to step #2.3)
5806
5807 Eventualy after step #2.5, we return to the event loop, which
5808 notices there's an event on the
5809 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5810 associated callback --- the function below. At this point, we're
5811 always safe to start a vStopped sequence. :
5812
5813 2.6) --> vStopped
5814 2.7) <-- T05 thread:2
5815 2.8) --> vStopped
5816 2.9) --> OK
5817*/
5818
722247f1
YQ
5819void
5820remote_notif_get_pending_events (struct notif_client *nc)
74531fed
PA
5821{
5822 struct remote_state *rs = get_remote_state ();
74531fed 5823
f48ff2a7 5824 if (rs->notif_state->pending_event[nc->id] != NULL)
74531fed 5825 {
722247f1
YQ
5826 if (notif_debug)
5827 fprintf_unfiltered (gdb_stdlog,
5828 "notif: process: '%s' ack pending event\n",
5829 nc->name);
74531fed 5830
722247f1 5831 /* acknowledge */
f48ff2a7
YQ
5832 nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]);
5833 rs->notif_state->pending_event[nc->id] = NULL;
74531fed
PA
5834
5835 while (1)
5836 {
5837 getpkt (&rs->buf, &rs->buf_size, 0);
5838 if (strcmp (rs->buf, "OK") == 0)
5839 break;
5840 else
722247f1 5841 remote_notif_ack (nc, rs->buf);
74531fed
PA
5842 }
5843 }
722247f1
YQ
5844 else
5845 {
5846 if (notif_debug)
5847 fprintf_unfiltered (gdb_stdlog,
5848 "notif: process: '%s' no pending reply\n",
5849 nc->name);
5850 }
74531fed
PA
5851}
5852
74531fed
PA
5853/* Called when it is decided that STOP_REPLY holds the info of the
5854 event that is to be returned to the core. This function always
5855 destroys STOP_REPLY. */
5856
5857static ptid_t
5858process_stop_reply (struct stop_reply *stop_reply,
5859 struct target_waitstatus *status)
5860{
5861 ptid_t ptid;
5862
5863 *status = stop_reply->ws;
5864 ptid = stop_reply->ptid;
5865
5866 /* If no thread/process was reported by the stub, assume the current
5867 inferior. */
5868 if (ptid_equal (ptid, null_ptid))
5869 ptid = inferior_ptid;
5870
5f3563ea
PA
5871 if (status->kind != TARGET_WAITKIND_EXITED
5872 && status->kind != TARGET_WAITKIND_SIGNALLED)
74531fed 5873 {
ee154bee
TT
5874 struct remote_state *rs = get_remote_state ();
5875
5f3563ea
PA
5876 /* Expedited registers. */
5877 if (stop_reply->regcache)
5878 {
217f1f79 5879 struct regcache *regcache
f5656ead 5880 = get_thread_arch_regcache (ptid, target_gdbarch ());
5f3563ea
PA
5881 cached_reg_t *reg;
5882 int ix;
5883
5884 for (ix = 0;
5885 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
5886 ix++)
217f1f79 5887 regcache_raw_supply (regcache, reg->num, reg->data);
5f3563ea
PA
5888 VEC_free (cached_reg_t, stop_reply->regcache);
5889 }
74531fed 5890
f7e6eed5 5891 rs->stop_reason = stop_reply->stop_reason;
ee154bee 5892 rs->remote_watch_data_address = stop_reply->watch_data_address;
1941c569
PA
5893
5894 remote_notice_new_inferior (ptid, 0);
dc146f7c 5895 demand_private_info (ptid)->core = stop_reply->core;
74531fed
PA
5896 }
5897
74531fed
PA
5898 stop_reply_xfree (stop_reply);
5899 return ptid;
5900}
5901
5902/* The non-stop mode version of target_wait. */
5903
5904static ptid_t
47608cb1 5905remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
5906{
5907 struct remote_state *rs = get_remote_state ();
74531fed
PA
5908 struct stop_reply *stop_reply;
5909 int ret;
fee9eda9 5910 int is_notif = 0;
74531fed
PA
5911
5912 /* If in non-stop mode, get out of getpkt even if a
5913 notification is received. */
5914
5915 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 5916 0 /* forever */, &is_notif);
74531fed
PA
5917 while (1)
5918 {
fee9eda9 5919 if (ret != -1 && !is_notif)
74531fed
PA
5920 switch (rs->buf[0])
5921 {
5922 case 'E': /* Error of some sort. */
5923 /* We're out of sync with the target now. Did it continue
5924 or not? We can't tell which thread it was in non-stop,
5925 so just ignore this. */
5926 warning (_("Remote failure reply: %s"), rs->buf);
5927 break;
5928 case 'O': /* Console output. */
5929 remote_console_output (rs->buf + 1);
5930 break;
5931 default:
5932 warning (_("Invalid remote reply: %s"), rs->buf);
5933 break;
5934 }
5935
5936 /* Acknowledge a pending stop reply that may have arrived in the
5937 mean time. */
f48ff2a7 5938 if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
722247f1 5939 remote_notif_get_pending_events (&notif_client_stop);
74531fed
PA
5940
5941 /* If indeed we noticed a stop reply, we're done. */
5942 stop_reply = queued_stop_reply (ptid);
5943 if (stop_reply != NULL)
5944 return process_stop_reply (stop_reply, status);
5945
47608cb1 5946 /* Still no event. If we're just polling for an event, then
74531fed 5947 return to the event loop. */
47608cb1 5948 if (options & TARGET_WNOHANG)
74531fed
PA
5949 {
5950 status->kind = TARGET_WAITKIND_IGNORE;
5951 return minus_one_ptid;
5952 }
5953
47608cb1 5954 /* Otherwise do a blocking wait. */
74531fed 5955 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
fee9eda9 5956 1 /* forever */, &is_notif);
74531fed
PA
5957 }
5958}
5959
5960/* Wait until the remote machine stops, then return, storing status in
5961 STATUS just as `wait' would. */
5962
5963static ptid_t
47608cb1 5964remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
5965{
5966 struct remote_state *rs = get_remote_state ();
74531fed 5967 ptid_t event_ptid = null_ptid;
cea39f65 5968 char *buf;
74531fed
PA
5969 struct stop_reply *stop_reply;
5970
47608cb1
PA
5971 again:
5972
74531fed
PA
5973 status->kind = TARGET_WAITKIND_IGNORE;
5974 status->value.integer = 0;
5975
5976 stop_reply = queued_stop_reply (ptid);
5977 if (stop_reply != NULL)
5978 return process_stop_reply (stop_reply, status);
5979
5980 if (rs->cached_wait_status)
5981 /* Use the cached wait status, but only once. */
5982 rs->cached_wait_status = 0;
5983 else
5984 {
5985 int ret;
722247f1 5986 int is_notif;
74531fed
PA
5987
5988 if (!target_is_async_p ())
5989 {
934b9bac 5990 ofunc = signal (SIGINT, sync_remote_interrupt);
74531fed
PA
5991 /* If the user hit C-c before this packet, or between packets,
5992 pretend that it was hit right here. */
522002f9 5993 if (check_quit_flag ())
74531fed 5994 {
522002f9 5995 clear_quit_flag ();
934b9bac 5996 sync_remote_interrupt (SIGINT);
74531fed
PA
5997 }
5998 }
5999
6000 /* FIXME: cagney/1999-09-27: If we're in async mode we should
6001 _never_ wait for ever -> test on target_is_async_p().
6002 However, before we do that we need to ensure that the caller
6003 knows how to take the target into/out of async mode. */
722247f1
YQ
6004 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
6005 wait_forever_enabled_p, &is_notif);
6006
5e1b953b
SDJ
6007 if (!target_is_async_p ())
6008 signal (SIGINT, ofunc);
6009
722247f1
YQ
6010 /* GDB gets a notification. Return to core as this event is
6011 not interesting. */
6012 if (ret != -1 && is_notif)
6013 return minus_one_ptid;
74531fed
PA
6014 }
6015
6016 buf = rs->buf;
6017
f7e6eed5 6018 rs->stop_reason = TARGET_STOPPED_BY_NO_REASON;
74531fed
PA
6019
6020 /* We got something. */
6021 rs->waiting_for_stop_reply = 0;
6022
3a29589a
DJ
6023 /* Assume that the target has acknowledged Ctrl-C unless we receive
6024 an 'F' or 'O' packet. */
6025 if (buf[0] != 'F' && buf[0] != 'O')
6026 rs->ctrlc_pending_p = 0;
6027
74531fed
PA
6028 switch (buf[0])
6029 {
6030 case 'E': /* Error of some sort. */
6031 /* We're out of sync with the target now. Did it continue or
6032 not? Not is more likely, so report a stop. */
6033 warning (_("Remote failure reply: %s"), buf);
6034 status->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 6035 status->value.sig = GDB_SIGNAL_0;
74531fed
PA
6036 break;
6037 case 'F': /* File-I/O request. */
3a29589a
DJ
6038 remote_fileio_request (buf, rs->ctrlc_pending_p);
6039 rs->ctrlc_pending_p = 0;
74531fed
PA
6040 break;
6041 case 'T': case 'S': case 'X': case 'W':
6042 {
722247f1
YQ
6043 struct stop_reply *stop_reply
6044 = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
6045 rs->buf);
74531fed 6046
74531fed 6047 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
6048 break;
6049 }
6050 case 'O': /* Console output. */
6051 remote_console_output (buf + 1);
e24a49d8 6052
c8e38a49
PA
6053 /* The target didn't really stop; keep waiting. */
6054 rs->waiting_for_stop_reply = 1;
e24a49d8 6055
c8e38a49
PA
6056 break;
6057 case '\0':
b73be471 6058 if (rs->last_sent_signal != GDB_SIGNAL_0)
c8e38a49
PA
6059 {
6060 /* Zero length reply means that we tried 'S' or 'C' and the
6061 remote system doesn't support it. */
6062 target_terminal_ours_for_output ();
6063 printf_filtered
6064 ("Can't send signals to this remote system. %s not sent.\n",
b73be471
TT
6065 gdb_signal_to_name (rs->last_sent_signal));
6066 rs->last_sent_signal = GDB_SIGNAL_0;
c8e38a49
PA
6067 target_terminal_inferior ();
6068
280ceea3 6069 strcpy ((char *) buf, rs->last_sent_step ? "s" : "c");
c8e38a49
PA
6070 putpkt ((char *) buf);
6071
6072 /* We just told the target to resume, so a stop reply is in
6073 order. */
e24a49d8 6074 rs->waiting_for_stop_reply = 1;
c8e38a49 6075 break;
43ff13b4 6076 }
c8e38a49
PA
6077 /* else fallthrough */
6078 default:
6079 warning (_("Invalid remote reply: %s"), buf);
6080 /* Keep waiting. */
6081 rs->waiting_for_stop_reply = 1;
6082 break;
43ff13b4 6083 }
c8e38a49 6084
c8e38a49 6085 if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
6086 {
6087 /* Nothing interesting happened. If we're doing a non-blocking
6088 poll, we're done. Otherwise, go back to waiting. */
6089 if (options & TARGET_WNOHANG)
6090 return minus_one_ptid;
6091 else
6092 goto again;
6093 }
74531fed
PA
6094 else if (status->kind != TARGET_WAITKIND_EXITED
6095 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
6096 {
6097 if (!ptid_equal (event_ptid, null_ptid))
47f8a51d 6098 record_currthread (rs, event_ptid);
82f73884
PA
6099 else
6100 event_ptid = inferior_ptid;
43ff13b4 6101 }
74531fed
PA
6102 else
6103 /* A process exit. Invalidate our notion of current thread. */
47f8a51d 6104 record_currthread (rs, minus_one_ptid);
79d7f229 6105
82f73884 6106 return event_ptid;
43ff13b4
JM
6107}
6108
74531fed
PA
6109/* Wait until the remote machine stops, then return, storing status in
6110 STATUS just as `wait' would. */
6111
c8e38a49 6112static ptid_t
117de6a9 6113remote_wait (struct target_ops *ops,
47608cb1 6114 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
6115{
6116 ptid_t event_ptid;
6117
74531fed 6118 if (non_stop)
47608cb1 6119 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 6120 else
47608cb1 6121 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 6122
d9d41e78 6123 if (target_is_async_p ())
c8e38a49 6124 {
74531fed
PA
6125 /* If there are are events left in the queue tell the event loop
6126 to return here. */
722247f1 6127 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
74531fed 6128 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 6129 }
c8e38a49
PA
6130
6131 return event_ptid;
6132}
6133
74ca34ce 6134/* Fetch a single register using a 'p' packet. */
c906108c 6135
b96ec7ac 6136static int
56be3814 6137fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac
AC
6138{
6139 struct remote_state *rs = get_remote_state ();
2e9f7625 6140 char *buf, *p;
b96ec7ac
AC
6141 char regp[MAX_REGISTER_SIZE];
6142 int i;
6143
4082afcc 6144 if (packet_support (PACKET_p) == PACKET_DISABLE)
74ca34ce
DJ
6145 return 0;
6146
6147 if (reg->pnum == -1)
6148 return 0;
6149
2e9f7625 6150 p = rs->buf;
fcad0fa4 6151 *p++ = 'p';
74ca34ce 6152 p += hexnumstr (p, reg->pnum);
fcad0fa4 6153 *p++ = '\0';
1f4437a4
MS
6154 putpkt (rs->buf);
6155 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 6156
2e9f7625
DJ
6157 buf = rs->buf;
6158
74ca34ce
DJ
6159 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
6160 {
6161 case PACKET_OK:
6162 break;
6163 case PACKET_UNKNOWN:
6164 return 0;
6165 case PACKET_ERROR:
27a9c0bf
MS
6166 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6167 gdbarch_register_name (get_regcache_arch (regcache),
6168 reg->regnum),
6169 buf);
74ca34ce 6170 }
3f9a994c
JB
6171
6172 /* If this register is unfetchable, tell the regcache. */
6173 if (buf[0] == 'x')
8480adf2 6174 {
56be3814 6175 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 6176 return 1;
b96ec7ac 6177 }
b96ec7ac 6178
3f9a994c
JB
6179 /* Otherwise, parse and supply the value. */
6180 p = buf;
6181 i = 0;
6182 while (p[0] != 0)
6183 {
6184 if (p[1] == 0)
74ca34ce 6185 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
6186
6187 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
6188 p += 2;
6189 }
56be3814 6190 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 6191 return 1;
b96ec7ac
AC
6192}
6193
74ca34ce
DJ
6194/* Fetch the registers included in the target's 'g' packet. */
6195
29709017
DJ
6196static int
6197send_g_packet (void)
c906108c 6198{
d01949b6 6199 struct remote_state *rs = get_remote_state ();
cea39f65 6200 int buf_len;
c906108c 6201
bba74b36 6202 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 6203 remote_send (&rs->buf, &rs->buf_size);
c906108c 6204
29709017
DJ
6205 /* We can get out of synch in various cases. If the first character
6206 in the buffer is not a hex character, assume that has happened
6207 and try to fetch another packet to read. */
6208 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
6209 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
6210 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
6211 && rs->buf[0] != 'x') /* New: unavailable register value. */
6212 {
6213 if (remote_debug)
6214 fprintf_unfiltered (gdb_stdlog,
6215 "Bad register packet; fetching a new packet\n");
6216 getpkt (&rs->buf, &rs->buf_size, 0);
6217 }
6218
74ca34ce
DJ
6219 buf_len = strlen (rs->buf);
6220
6221 /* Sanity check the received packet. */
6222 if (buf_len % 2 != 0)
6223 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
6224
6225 return buf_len / 2;
6226}
6227
6228static void
56be3814 6229process_g_packet (struct regcache *regcache)
29709017 6230{
4a22f64d 6231 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
6232 struct remote_state *rs = get_remote_state ();
6233 struct remote_arch_state *rsa = get_remote_arch_state ();
6234 int i, buf_len;
6235 char *p;
6236 char *regs;
6237
6238 buf_len = strlen (rs->buf);
6239
6240 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce
DJ
6241 if (buf_len > 2 * rsa->sizeof_g_packet)
6242 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
6243
6244 /* Save the size of the packet sent to us by the target. It is used
6245 as a heuristic when determining the max size of packets that the
6246 target can safely receive. */
6247 if (rsa->actual_register_packet_size == 0)
6248 rsa->actual_register_packet_size = buf_len;
6249
6250 /* If this is smaller than we guessed the 'g' packet would be,
6251 update our records. A 'g' reply that doesn't include a register's
6252 value implies either that the register is not available, or that
6253 the 'p' packet must be used. */
6254 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 6255 {
74ca34ce
DJ
6256 rsa->sizeof_g_packet = buf_len / 2;
6257
4a22f64d 6258 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 6259 {
74ca34ce
DJ
6260 if (rsa->regs[i].pnum == -1)
6261 continue;
6262
6263 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
6264 rsa->regs[i].in_g_packet = 0;
b96ec7ac 6265 else
74ca34ce 6266 rsa->regs[i].in_g_packet = 1;
b96ec7ac 6267 }
74ca34ce 6268 }
b323314b 6269
74ca34ce 6270 regs = alloca (rsa->sizeof_g_packet);
c906108c
SS
6271
6272 /* Unimplemented registers read as all bits zero. */
ea9c271d 6273 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 6274
c906108c
SS
6275 /* Reply describes registers byte by byte, each byte encoded as two
6276 hex characters. Suck them all up, then supply them to the
6277 register cacheing/storage mechanism. */
6278
74ca34ce 6279 p = rs->buf;
ea9c271d 6280 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 6281 {
74ca34ce
DJ
6282 if (p[0] == 0 || p[1] == 0)
6283 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6284 internal_error (__FILE__, __LINE__,
9b20d036 6285 _("unexpected end of 'g' packet reply"));
74ca34ce 6286
c906108c 6287 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 6288 regs[i] = 0; /* 'x' */
c906108c
SS
6289 else
6290 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
6291 p += 2;
6292 }
6293
a744cf53
MS
6294 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
6295 {
6296 struct packet_reg *r = &rsa->regs[i];
6297
6298 if (r->in_g_packet)
6299 {
6300 if (r->offset * 2 >= strlen (rs->buf))
6301 /* This shouldn't happen - we adjusted in_g_packet above. */
6302 internal_error (__FILE__, __LINE__,
9b20d036 6303 _("unexpected end of 'g' packet reply"));
a744cf53
MS
6304 else if (rs->buf[r->offset * 2] == 'x')
6305 {
6306 gdb_assert (r->offset * 2 < strlen (rs->buf));
6307 /* The register isn't available, mark it as such (at
6308 the same time setting the value to zero). */
6309 regcache_raw_supply (regcache, r->regnum, NULL);
6310 }
6311 else
6312 regcache_raw_supply (regcache, r->regnum,
6313 regs + r->offset);
6314 }
6315 }
c906108c
SS
6316}
6317
29709017 6318static void
56be3814 6319fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
6320{
6321 send_g_packet ();
56be3814 6322 process_g_packet (regcache);
29709017
DJ
6323}
6324
e6e4e701
PA
6325/* Make the remote selected traceframe match GDB's selected
6326 traceframe. */
6327
6328static void
6329set_remote_traceframe (void)
6330{
6331 int newnum;
262e1174 6332 struct remote_state *rs = get_remote_state ();
e6e4e701 6333
262e1174 6334 if (rs->remote_traceframe_number == get_traceframe_number ())
e6e4e701
PA
6335 return;
6336
6337 /* Avoid recursion, remote_trace_find calls us again. */
262e1174 6338 rs->remote_traceframe_number = get_traceframe_number ();
e6e4e701
PA
6339
6340 newnum = target_trace_find (tfind_number,
6341 get_traceframe_number (), 0, 0, NULL);
6342
6343 /* Should not happen. If it does, all bets are off. */
6344 if (newnum != get_traceframe_number ())
6345 warning (_("could not set remote traceframe"));
6346}
6347
74ca34ce 6348static void
28439f5e
PA
6349remote_fetch_registers (struct target_ops *ops,
6350 struct regcache *regcache, int regnum)
74ca34ce 6351{
74ca34ce
DJ
6352 struct remote_arch_state *rsa = get_remote_arch_state ();
6353 int i;
6354
e6e4e701 6355 set_remote_traceframe ();
79d7f229 6356 set_general_thread (inferior_ptid);
74ca34ce
DJ
6357
6358 if (regnum >= 0)
6359 {
6360 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6361
74ca34ce
DJ
6362 gdb_assert (reg != NULL);
6363
6364 /* If this register might be in the 'g' packet, try that first -
6365 we are likely to read more than one register. If this is the
6366 first 'g' packet, we might be overly optimistic about its
6367 contents, so fall back to 'p'. */
6368 if (reg->in_g_packet)
6369 {
56be3814 6370 fetch_registers_using_g (regcache);
74ca34ce
DJ
6371 if (reg->in_g_packet)
6372 return;
6373 }
6374
56be3814 6375 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
6376 return;
6377
6378 /* This register is not available. */
56be3814 6379 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
6380
6381 return;
6382 }
6383
56be3814 6384 fetch_registers_using_g (regcache);
74ca34ce 6385
4a22f64d 6386 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6387 if (!rsa->regs[i].in_g_packet)
56be3814 6388 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
6389 {
6390 /* This register is not available. */
56be3814 6391 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
6392 }
6393}
6394
c906108c
SS
6395/* Prepare to store registers. Since we may send them all (using a
6396 'G' request), we have to read out the ones we don't want to change
6397 first. */
6398
c5aa993b 6399static void
f32dbf8c 6400remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
c906108c 6401{
ea9c271d 6402 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 6403 int i;
cfd77fa1 6404 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 6405
c906108c 6406 /* Make sure the entire registers array is valid. */
4082afcc 6407 switch (packet_support (PACKET_P))
5a2468f5
JM
6408 {
6409 case PACKET_DISABLE:
6410 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 6411 /* Make sure all the necessary registers are cached. */
4a22f64d 6412 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 6413 if (rsa->regs[i].in_g_packet)
316f2060 6414 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
6415 break;
6416 case PACKET_ENABLE:
6417 break;
6418 }
6419}
6420
ad10f812 6421/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 6422 packet was not recognized. */
5a2468f5
JM
6423
6424static int
1f4437a4
MS
6425store_register_using_P (const struct regcache *regcache,
6426 struct packet_reg *reg)
5a2468f5 6427{
4a22f64d 6428 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 6429 struct remote_state *rs = get_remote_state ();
5a2468f5 6430 /* Try storing a single register. */
6d820c5c 6431 char *buf = rs->buf;
cfd77fa1 6432 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 6433 char *p;
5a2468f5 6434
4082afcc 6435 if (packet_support (PACKET_P) == PACKET_DISABLE)
74ca34ce
DJ
6436 return 0;
6437
6438 if (reg->pnum == -1)
6439 return 0;
6440
ea9c271d 6441 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 6442 p = buf + strlen (buf);
56be3814 6443 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 6444 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
6445 putpkt (rs->buf);
6446 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 6447
74ca34ce
DJ
6448 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6449 {
6450 case PACKET_OK:
6451 return 1;
6452 case PACKET_ERROR:
27a9c0bf
MS
6453 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6454 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
6455 case PACKET_UNKNOWN:
6456 return 0;
6457 default:
6458 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6459 }
c906108c
SS
6460}
6461
23860348
MS
6462/* Store register REGNUM, or all registers if REGNUM == -1, from the
6463 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
6464
6465static void
56be3814 6466store_registers_using_G (const struct regcache *regcache)
c906108c 6467{
d01949b6 6468 struct remote_state *rs = get_remote_state ();
ea9c271d 6469 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 6470 gdb_byte *regs;
c906108c
SS
6471 char *p;
6472
193cb69f
AC
6473 /* Extract all the registers in the regcache copying them into a
6474 local buffer. */
6475 {
b323314b 6476 int i;
a744cf53 6477
ea9c271d
DJ
6478 regs = alloca (rsa->sizeof_g_packet);
6479 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 6480 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 6481 {
ea9c271d 6482 struct packet_reg *r = &rsa->regs[i];
a744cf53 6483
b323314b 6484 if (r->in_g_packet)
56be3814 6485 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
6486 }
6487 }
c906108c
SS
6488
6489 /* Command describes registers byte by byte,
6490 each byte encoded as two hex characters. */
6d820c5c 6491 p = rs->buf;
193cb69f 6492 *p++ = 'G';
74ca34ce
DJ
6493 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6494 updated. */
6495 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
6496 putpkt (rs->buf);
6497 getpkt (&rs->buf, &rs->buf_size, 0);
6498 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
6499 error (_("Could not write registers; remote failure reply '%s'"),
6500 rs->buf);
c906108c 6501}
74ca34ce
DJ
6502
6503/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6504 of the register cache buffer. FIXME: ignores errors. */
6505
6506static void
28439f5e
PA
6507remote_store_registers (struct target_ops *ops,
6508 struct regcache *regcache, int regnum)
74ca34ce 6509{
74ca34ce
DJ
6510 struct remote_arch_state *rsa = get_remote_arch_state ();
6511 int i;
6512
e6e4e701 6513 set_remote_traceframe ();
79d7f229 6514 set_general_thread (inferior_ptid);
74ca34ce
DJ
6515
6516 if (regnum >= 0)
6517 {
6518 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6519
74ca34ce
DJ
6520 gdb_assert (reg != NULL);
6521
6522 /* Always prefer to store registers using the 'P' packet if
6523 possible; we often change only a small number of registers.
6524 Sometimes we change a larger number; we'd need help from a
6525 higher layer to know to use 'G'. */
56be3814 6526 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
6527 return;
6528
6529 /* For now, don't complain if we have no way to write the
6530 register. GDB loses track of unavailable registers too
6531 easily. Some day, this may be an error. We don't have
0df8b418 6532 any way to read the register, either... */
74ca34ce
DJ
6533 if (!reg->in_g_packet)
6534 return;
6535
56be3814 6536 store_registers_using_G (regcache);
74ca34ce
DJ
6537 return;
6538 }
6539
56be3814 6540 store_registers_using_G (regcache);
74ca34ce 6541
4a22f64d 6542 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6543 if (!rsa->regs[i].in_g_packet)
56be3814 6544 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
6545 /* See above for why we do not issue an error here. */
6546 continue;
6547}
c906108c
SS
6548\f
6549
6550/* Return the number of hex digits in num. */
6551
6552static int
fba45db2 6553hexnumlen (ULONGEST num)
c906108c
SS
6554{
6555 int i;
6556
6557 for (i = 0; num != 0; i++)
6558 num >>= 4;
6559
6560 return max (i, 1);
6561}
6562
2df3850c 6563/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
6564
6565static int
fba45db2 6566hexnumstr (char *buf, ULONGEST num)
c906108c 6567{
c906108c 6568 int len = hexnumlen (num);
a744cf53 6569
2df3850c
JM
6570 return hexnumnstr (buf, num, len);
6571}
6572
c906108c 6573
2df3850c 6574/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 6575
2df3850c 6576static int
fba45db2 6577hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
6578{
6579 int i;
6580
6581 buf[width] = '\0';
6582
6583 for (i = width - 1; i >= 0; i--)
c906108c 6584 {
c5aa993b 6585 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
6586 num >>= 4;
6587 }
6588
2df3850c 6589 return width;
c906108c
SS
6590}
6591
23860348 6592/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
6593
6594static CORE_ADDR
fba45db2 6595remote_address_masked (CORE_ADDR addr)
c906108c 6596{
883b9c6c 6597 unsigned int address_size = remote_address_size;
a744cf53 6598
911c95a5
UW
6599 /* If "remoteaddresssize" was not set, default to target address size. */
6600 if (!address_size)
f5656ead 6601 address_size = gdbarch_addr_bit (target_gdbarch ());
911c95a5
UW
6602
6603 if (address_size > 0
6604 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
6605 {
6606 /* Only create a mask when that mask can safely be constructed
23860348 6607 in a ULONGEST variable. */
c906108c 6608 ULONGEST mask = 1;
a744cf53 6609
911c95a5 6610 mask = (mask << address_size) - 1;
c906108c
SS
6611 addr &= mask;
6612 }
6613 return addr;
6614}
6615
6616/* Determine whether the remote target supports binary downloading.
6617 This is accomplished by sending a no-op memory write of zero length
6618 to the target at the specified address. It does not suffice to send
23860348
MS
6619 the whole packet, since many stubs strip the eighth bit and
6620 subsequently compute a wrong checksum, which causes real havoc with
6621 remote_write_bytes.
7a292a7a 6622
96baa820 6623 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 6624 clean. In cases like this, the user should clear "remote
23860348 6625 X-packet". */
96baa820 6626
c906108c 6627static void
fba45db2 6628check_binary_download (CORE_ADDR addr)
c906108c 6629{
d01949b6 6630 struct remote_state *rs = get_remote_state ();
24b06219 6631
4082afcc 6632 switch (packet_support (PACKET_X))
c906108c 6633 {
96baa820
JM
6634 case PACKET_DISABLE:
6635 break;
6636 case PACKET_ENABLE:
6637 break;
6638 case PACKET_SUPPORT_UNKNOWN:
6639 {
96baa820 6640 char *p;
802188a7 6641
2e9f7625 6642 p = rs->buf;
96baa820
JM
6643 *p++ = 'X';
6644 p += hexnumstr (p, (ULONGEST) addr);
6645 *p++ = ',';
6646 p += hexnumstr (p, (ULONGEST) 0);
6647 *p++ = ':';
6648 *p = '\0';
802188a7 6649
2e9f7625 6650 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 6651 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 6652
2e9f7625 6653 if (rs->buf[0] == '\0')
96baa820
JM
6654 {
6655 if (remote_debug)
6656 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
6657 "binary downloading NOT "
6658 "supported by target\n");
444abaca 6659 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
6660 }
6661 else
6662 {
6663 if (remote_debug)
6664 fprintf_unfiltered (gdb_stdlog,
64b9b334 6665 "binary downloading supported by target\n");
444abaca 6666 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
6667 }
6668 break;
6669 }
c906108c
SS
6670 }
6671}
6672
6673/* Write memory data directly to the remote machine.
6674 This does not inform the data cache; the data cache uses this.
a76d924d 6675 HEADER is the starting part of the packet.
c906108c
SS
6676 MEMADDR is the address in the remote memory space.
6677 MYADDR is the address of the buffer in our space.
6678 LEN is the number of bytes.
a76d924d
DJ
6679 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6680 should send data as binary ('X'), or hex-encoded ('M').
6681
6682 The function creates packet of the form
6683 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6684
6685 where encoding of <DATA> is termined by PACKET_FORMAT.
6686
6687 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6688 are omitted.
6689
9b409511
YQ
6690 Return the transferred status, error or OK (an
6691 'enum target_xfer_status' value). Save the number of bytes
6692 transferred in *XFERED_LEN. Only transfer a single packet. */
c906108c 6693
9b409511 6694static enum target_xfer_status
a76d924d 6695remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
5d6df423 6696 const gdb_byte *myaddr, ULONGEST len,
9b409511
YQ
6697 ULONGEST *xfered_len, char packet_format,
6698 int use_length)
c906108c 6699{
6d820c5c 6700 struct remote_state *rs = get_remote_state ();
cfd77fa1 6701 char *p;
a76d924d
DJ
6702 char *plen = NULL;
6703 int plenlen = 0;
917317f4
JM
6704 int todo;
6705 int nr_bytes;
a257b5bb 6706 int payload_size;
6765f3e5 6707 int payload_length;
a76d924d
DJ
6708 int header_length;
6709
6710 if (packet_format != 'X' && packet_format != 'M')
6711 internal_error (__FILE__, __LINE__,
9b20d036 6712 _("remote_write_bytes_aux: bad packet format"));
c906108c 6713
5d6df423 6714 if (len == 0)
9b409511 6715 return TARGET_XFER_EOF;
b2182ed2 6716
3de11b2e 6717 payload_size = get_memory_write_packet_size ();
2bc416ba 6718
6d820c5c
DJ
6719 /* The packet buffer will be large enough for the payload;
6720 get_memory_packet_size ensures this. */
a76d924d 6721 rs->buf[0] = '\0';
c906108c 6722
a257b5bb 6723 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
6724 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6725
a76d924d
DJ
6726 payload_size -= strlen ("$,:#NN");
6727 if (!use_length)
0df8b418 6728 /* The comma won't be used. */
a76d924d
DJ
6729 payload_size += 1;
6730 header_length = strlen (header);
6731 payload_size -= header_length;
3de11b2e 6732 payload_size -= hexnumlen (memaddr);
c906108c 6733
a76d924d 6734 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 6735
a76d924d
DJ
6736 strcat (rs->buf, header);
6737 p = rs->buf + strlen (header);
6738
6739 /* Compute a best guess of the number of bytes actually transfered. */
6740 if (packet_format == 'X')
c906108c 6741 {
23860348 6742 /* Best guess at number of bytes that will fit. */
a257b5bb 6743 todo = min (len, payload_size);
a76d924d
DJ
6744 if (use_length)
6745 payload_size -= hexnumlen (todo);
3de11b2e 6746 todo = min (todo, payload_size);
a76d924d
DJ
6747 }
6748 else
6749 {
23860348 6750 /* Num bytes that will fit. */
a257b5bb 6751 todo = min (len, payload_size / 2);
a76d924d
DJ
6752 if (use_length)
6753 payload_size -= hexnumlen (todo);
3de11b2e 6754 todo = min (todo, payload_size / 2);
917317f4 6755 }
a76d924d 6756
3de11b2e
NS
6757 if (todo <= 0)
6758 internal_error (__FILE__, __LINE__,
405f8e94 6759 _("minimum packet size too small to write data"));
802188a7 6760
6765f3e5
DJ
6761 /* If we already need another packet, then try to align the end
6762 of this packet to a useful boundary. */
6763 if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len)
6764 todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6765
a257b5bb 6766 /* Append "<memaddr>". */
917317f4
JM
6767 memaddr = remote_address_masked (memaddr);
6768 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 6769
a76d924d
DJ
6770 if (use_length)
6771 {
6772 /* Append ",". */
6773 *p++ = ',';
802188a7 6774
a76d924d
DJ
6775 /* Append <len>. Retain the location/size of <len>. It may need to
6776 be adjusted once the packet body has been created. */
6777 plen = p;
6778 plenlen = hexnumstr (p, (ULONGEST) todo);
6779 p += plenlen;
6780 }
a257b5bb
AC
6781
6782 /* Append ":". */
917317f4
JM
6783 *p++ = ':';
6784 *p = '\0';
802188a7 6785
a257b5bb 6786 /* Append the packet body. */
a76d924d 6787 if (packet_format == 'X')
917317f4 6788 {
917317f4
JM
6789 /* Binary mode. Send target system values byte by byte, in
6790 increasing byte addresses. Only escape certain critical
6791 characters. */
bc20a4af
PA
6792 payload_length = remote_escape_output (myaddr, todo, (gdb_byte *) p,
6793 &nr_bytes, payload_size);
6765f3e5
DJ
6794
6795 /* If not all TODO bytes fit, then we'll need another packet. Make
9b7194bc
DJ
6796 a second try to keep the end of the packet aligned. Don't do
6797 this if the packet is tiny. */
6798 if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES)
6765f3e5
DJ
6799 {
6800 int new_nr_bytes;
6801
6802 new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1))
6803 - memaddr);
6804 if (new_nr_bytes != nr_bytes)
6805 payload_length = remote_escape_output (myaddr, new_nr_bytes,
bc20a4af 6806 (gdb_byte *) p, &nr_bytes,
6765f3e5
DJ
6807 payload_size);
6808 }
6809
6810 p += payload_length;
a76d924d 6811 if (use_length && nr_bytes < todo)
c906108c 6812 {
802188a7 6813 /* Escape chars have filled up the buffer prematurely,
917317f4
JM
6814 and we have actually sent fewer bytes than planned.
6815 Fix-up the length field of the packet. Use the same
6816 number of characters as before. */
917317f4
JM
6817 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
6818 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 6819 }
a76d924d
DJ
6820 }
6821 else
6822 {
917317f4
JM
6823 /* Normal mode: Send target system values byte by byte, in
6824 increasing byte addresses. Each byte is encoded as a two hex
6825 value. */
2644f393 6826 nr_bytes = bin2hex (myaddr, p, todo);
aa6c0017 6827 p += 2 * nr_bytes;
c906108c 6828 }
802188a7 6829
2e9f7625 6830 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 6831 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 6832
2e9f7625 6833 if (rs->buf[0] == 'E')
00d84524 6834 return TARGET_XFER_E_IO;
802188a7 6835
23860348
MS
6836 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6837 fewer bytes than we'd planned. */
9b409511
YQ
6838 *xfered_len = (ULONGEST) nr_bytes;
6839 return TARGET_XFER_OK;
c906108c
SS
6840}
6841
a76d924d
DJ
6842/* Write memory data directly to the remote machine.
6843 This does not inform the data cache; the data cache uses this.
6844 MEMADDR is the address in the remote memory space.
6845 MYADDR is the address of the buffer in our space.
6846 LEN is the number of bytes.
6847
9b409511
YQ
6848 Return the transferred status, error or OK (an
6849 'enum target_xfer_status' value). Save the number of bytes
6850 transferred in *XFERED_LEN. Only transfer a single packet. */
a76d924d 6851
9b409511
YQ
6852static enum target_xfer_status
6853remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ULONGEST len,
6854 ULONGEST *xfered_len)
a76d924d
DJ
6855{
6856 char *packet_format = 0;
6857
6858 /* Check whether the target supports binary download. */
6859 check_binary_download (memaddr);
6860
4082afcc 6861 switch (packet_support (PACKET_X))
a76d924d
DJ
6862 {
6863 case PACKET_ENABLE:
6864 packet_format = "X";
6865 break;
6866 case PACKET_DISABLE:
6867 packet_format = "M";
6868 break;
6869 case PACKET_SUPPORT_UNKNOWN:
6870 internal_error (__FILE__, __LINE__,
6871 _("remote_write_bytes: bad internal state"));
6872 default:
6873 internal_error (__FILE__, __LINE__, _("bad switch"));
6874 }
6875
6876 return remote_write_bytes_aux (packet_format,
9b409511
YQ
6877 memaddr, myaddr, len, xfered_len,
6878 packet_format[0], 1);
a76d924d
DJ
6879}
6880
9217e74e
YQ
6881/* Read memory data directly from the remote machine.
6882 This does not use the data cache; the data cache uses this.
6883 MEMADDR is the address in the remote memory space.
6884 MYADDR is the address of the buffer in our space.
6885 LEN is the number of bytes.
6886
6887 Return the transferred status, error or OK (an
6888 'enum target_xfer_status' value). Save the number of bytes
6889 transferred in *XFERED_LEN. */
6890
6891static enum target_xfer_status
6892remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr, ULONGEST len,
6893 ULONGEST *xfered_len)
6894{
6895 struct remote_state *rs = get_remote_state ();
6896 int max_buf_size; /* Max size of packet output buffer. */
6897 char *p;
6898 int todo;
6899 int i;
6900
6901 max_buf_size = get_memory_read_packet_size ();
6902 /* The packet buffer will be large enough for the payload;
6903 get_memory_packet_size ensures this. */
6904
6905 /* Number if bytes that will fit. */
6906 todo = min (len, max_buf_size / 2);
6907
6908 /* Construct "m"<memaddr>","<len>". */
6909 memaddr = remote_address_masked (memaddr);
6910 p = rs->buf;
6911 *p++ = 'm';
6912 p += hexnumstr (p, (ULONGEST) memaddr);
6913 *p++ = ',';
6914 p += hexnumstr (p, (ULONGEST) todo);
6915 *p = '\0';
6916 putpkt (rs->buf);
6917 getpkt (&rs->buf, &rs->buf_size, 0);
6918 if (rs->buf[0] == 'E'
6919 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
6920 && rs->buf[3] == '\0')
6921 return TARGET_XFER_E_IO;
6922 /* Reply describes memory byte by byte, each byte encoded as two hex
6923 characters. */
6924 p = rs->buf;
6925 i = hex2bin (p, myaddr, todo);
6926 /* Return what we have. Let higher layers handle partial reads. */
6927 *xfered_len = (ULONGEST) i;
6928 return TARGET_XFER_OK;
6929}
6930
b55fbac4
YQ
6931/* Using the set of read-only target sections of remote, read live
6932 read-only memory.
8acf9577
YQ
6933
6934 For interface/parameters/return description see target.h,
6935 to_xfer_partial. */
6936
6937static enum target_xfer_status
b55fbac4
YQ
6938remote_xfer_live_readonly_partial (struct target_ops *ops, gdb_byte *readbuf,
6939 ULONGEST memaddr, ULONGEST len,
6940 ULONGEST *xfered_len)
8acf9577
YQ
6941{
6942 struct target_section *secp;
6943 struct target_section_table *table;
6944
6945 secp = target_section_by_addr (ops, memaddr);
6946 if (secp != NULL
6947 && (bfd_get_section_flags (secp->the_bfd_section->owner,
6948 secp->the_bfd_section)
6949 & SEC_READONLY))
6950 {
6951 struct target_section *p;
6952 ULONGEST memend = memaddr + len;
6953
6954 table = target_get_section_table (ops);
6955
6956 for (p = table->sections; p < table->sections_end; p++)
6957 {
6958 if (memaddr >= p->addr)
6959 {
6960 if (memend <= p->endaddr)
6961 {
6962 /* Entire transfer is within this section. */
b55fbac4
YQ
6963 return remote_read_bytes_1 (memaddr, readbuf, len,
6964 xfered_len);
8acf9577
YQ
6965 }
6966 else if (memaddr >= p->endaddr)
6967 {
6968 /* This section ends before the transfer starts. */
6969 continue;
6970 }
6971 else
6972 {
6973 /* This section overlaps the transfer. Just do half. */
6974 len = p->endaddr - memaddr;
b55fbac4
YQ
6975 return remote_read_bytes_1 (memaddr, readbuf, len,
6976 xfered_len);
8acf9577
YQ
6977 }
6978 }
6979 }
6980 }
6981
6982 return TARGET_XFER_EOF;
6983}
6984
9217e74e
YQ
6985/* Similar to remote_read_bytes_1, but it reads from the remote stub
6986 first if the requested memory is unavailable in traceframe.
6987 Otherwise, fall back to remote_read_bytes_1. */
c906108c 6988
9b409511 6989static enum target_xfer_status
8acf9577
YQ
6990remote_read_bytes (struct target_ops *ops, CORE_ADDR memaddr,
6991 gdb_byte *myaddr, ULONGEST len, ULONGEST *xfered_len)
c906108c 6992{
6b6aa828 6993 if (len == 0)
96c4f946 6994 return TARGET_XFER_EOF;
b2182ed2 6995
8acf9577
YQ
6996 if (get_traceframe_number () != -1)
6997 {
6998 VEC(mem_range_s) *available;
6999
7000 /* If we fail to get the set of available memory, then the
7001 target does not support querying traceframe info, and so we
7002 attempt reading from the traceframe anyway (assuming the
7003 target implements the old QTro packet then). */
7004 if (traceframe_available_memory (&available, memaddr, len))
7005 {
7006 struct cleanup *old_chain;
7007
7008 old_chain = make_cleanup (VEC_cleanup(mem_range_s), &available);
7009
7010 if (VEC_empty (mem_range_s, available)
7011 || VEC_index (mem_range_s, available, 0)->start != memaddr)
7012 {
7013 enum target_xfer_status res;
7014
7015 /* Don't read into the traceframe's available
7016 memory. */
7017 if (!VEC_empty (mem_range_s, available))
7018 {
7019 LONGEST oldlen = len;
7020
7021 len = VEC_index (mem_range_s, available, 0)->start - memaddr;
7022 gdb_assert (len <= oldlen);
7023 }
7024
7025 do_cleanups (old_chain);
7026
7027 /* This goes through the topmost target again. */
b55fbac4 7028 res = remote_xfer_live_readonly_partial (ops, myaddr, memaddr,
8acf9577
YQ
7029 len, xfered_len);
7030 if (res == TARGET_XFER_OK)
7031 return TARGET_XFER_OK;
7032 else
7033 {
7034 /* No use trying further, we know some memory starting
7035 at MEMADDR isn't available. */
7036 *xfered_len = len;
7037 return TARGET_XFER_UNAVAILABLE;
7038 }
7039 }
7040
7041 /* Don't try to read more than how much is available, in
7042 case the target implements the deprecated QTro packet to
7043 cater for older GDBs (the target's knowledge of read-only
7044 sections may be outdated by now). */
7045 len = VEC_index (mem_range_s, available, 0)->length;
7046
7047 do_cleanups (old_chain);
7048 }
7049 }
7050
9217e74e 7051 return remote_read_bytes_1 (memaddr, myaddr, len, xfered_len);
c906108c 7052}
74531fed 7053
c906108c 7054\f
c906108c 7055
a76d924d
DJ
7056/* Sends a packet with content determined by the printf format string
7057 FORMAT and the remaining arguments, then gets the reply. Returns
7058 whether the packet was a success, a failure, or unknown. */
7059
77b64a49
PA
7060static enum packet_result remote_send_printf (const char *format, ...)
7061 ATTRIBUTE_PRINTF (1, 2);
7062
2c0b251b 7063static enum packet_result
a76d924d
DJ
7064remote_send_printf (const char *format, ...)
7065{
7066 struct remote_state *rs = get_remote_state ();
7067 int max_size = get_remote_packet_size ();
a76d924d 7068 va_list ap;
a744cf53 7069
a76d924d
DJ
7070 va_start (ap, format);
7071
7072 rs->buf[0] = '\0';
7073 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 7074 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
7075
7076 if (putpkt (rs->buf) < 0)
7077 error (_("Communication problem with target."));
7078
7079 rs->buf[0] = '\0';
7080 getpkt (&rs->buf, &rs->buf_size, 0);
7081
7082 return packet_check_result (rs->buf);
7083}
7084
7085static void
7086restore_remote_timeout (void *p)
7087{
7088 int value = *(int *)p;
a744cf53 7089
a76d924d
DJ
7090 remote_timeout = value;
7091}
7092
7093/* Flash writing can take quite some time. We'll set
7094 effectively infinite timeout for flash operations.
7095 In future, we'll need to decide on a better approach. */
7096static const int remote_flash_timeout = 1000;
7097
7098static void
7099remote_flash_erase (struct target_ops *ops,
7100 ULONGEST address, LONGEST length)
7101{
f5656ead 7102 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
a76d924d
DJ
7103 int saved_remote_timeout = remote_timeout;
7104 enum packet_result ret;
a76d924d
DJ
7105 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7106 &saved_remote_timeout);
a744cf53 7107
a76d924d
DJ
7108 remote_timeout = remote_flash_timeout;
7109
7110 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 7111 phex (address, addr_size),
a76d924d
DJ
7112 phex (length, 4));
7113 switch (ret)
7114 {
7115 case PACKET_UNKNOWN:
7116 error (_("Remote target does not support flash erase"));
7117 case PACKET_ERROR:
7118 error (_("Error erasing flash with vFlashErase packet"));
7119 default:
7120 break;
7121 }
7122
7123 do_cleanups (back_to);
7124}
7125
9b409511
YQ
7126static enum target_xfer_status
7127remote_flash_write (struct target_ops *ops, ULONGEST address,
7128 ULONGEST length, ULONGEST *xfered_len,
7129 const gdb_byte *data)
a76d924d
DJ
7130{
7131 int saved_remote_timeout = remote_timeout;
9b409511 7132 enum target_xfer_status ret;
a76d924d 7133 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
9b409511 7134 &saved_remote_timeout);
a76d924d
DJ
7135
7136 remote_timeout = remote_flash_timeout;
9b409511
YQ
7137 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length,
7138 xfered_len,'X', 0);
a76d924d
DJ
7139 do_cleanups (back_to);
7140
7141 return ret;
7142}
7143
7144static void
7145remote_flash_done (struct target_ops *ops)
7146{
7147 int saved_remote_timeout = remote_timeout;
7148 int ret;
7149 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
7150 &saved_remote_timeout);
7151
7152 remote_timeout = remote_flash_timeout;
7153 ret = remote_send_printf ("vFlashDone");
7154 do_cleanups (back_to);
7155
7156 switch (ret)
7157 {
7158 case PACKET_UNKNOWN:
7159 error (_("Remote target does not support vFlashDone"));
7160 case PACKET_ERROR:
7161 error (_("Error finishing flash operation"));
7162 default:
7163 break;
7164 }
7165}
7166
c906108c 7167static void
fba45db2 7168remote_files_info (struct target_ops *ignore)
c906108c
SS
7169{
7170 puts_filtered ("Debugging a target over a serial line.\n");
7171}
7172\f
7173/* Stuff for dealing with the packets which are part of this protocol.
7174 See comment at top of file for details. */
7175
1927e618
PA
7176/* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
7177 error to higher layers. Called when a serial error is detected.
7178 The exception message is STRING, followed by a colon and a blank,
d6cb50a2
JK
7179 the system error message for errno at function entry and final dot
7180 for output compatibility with throw_perror_with_name. */
1927e618
PA
7181
7182static void
7183unpush_and_perror (const char *string)
7184{
d6cb50a2 7185 int saved_errno = errno;
1927e618
PA
7186
7187 remote_unpush_target ();
d6cb50a2
JK
7188 throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
7189 safe_strerror (saved_errno));
1927e618
PA
7190}
7191
0876f84a 7192/* Read a single character from the remote end. */
c906108c
SS
7193
7194static int
fba45db2 7195readchar (int timeout)
c906108c
SS
7196{
7197 int ch;
5d93a237 7198 struct remote_state *rs = get_remote_state ();
c906108c 7199
5d93a237 7200 ch = serial_readchar (rs->remote_desc, timeout);
c906108c 7201
2acceee2 7202 if (ch >= 0)
0876f84a 7203 return ch;
2acceee2
JM
7204
7205 switch ((enum serial_rc) ch)
c906108c
SS
7206 {
7207 case SERIAL_EOF:
78a095c3 7208 remote_unpush_target ();
598d3636 7209 throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
2acceee2 7210 /* no return */
c906108c 7211 case SERIAL_ERROR:
1927e618
PA
7212 unpush_and_perror (_("Remote communication error. "
7213 "Target disconnected."));
2acceee2 7214 /* no return */
c906108c 7215 case SERIAL_TIMEOUT:
2acceee2 7216 break;
c906108c 7217 }
2acceee2 7218 return ch;
c906108c
SS
7219}
7220
c33e31fd
PA
7221/* Wrapper for serial_write that closes the target and throws if
7222 writing fails. */
7223
7224static void
7225remote_serial_write (const char *str, int len)
7226{
5d93a237
TT
7227 struct remote_state *rs = get_remote_state ();
7228
7229 if (serial_write (rs->remote_desc, str, len))
c33e31fd 7230 {
1927e618
PA
7231 unpush_and_perror (_("Remote communication error. "
7232 "Target disconnected."));
c33e31fd
PA
7233 }
7234}
7235
6d820c5c
DJ
7236/* Send the command in *BUF to the remote machine, and read the reply
7237 into *BUF. Report an error if we get an error reply. Resize
7238 *BUF using xrealloc if necessary to hold the result, and update
7239 *SIZEOF_BUF. */
c906108c
SS
7240
7241static void
6d820c5c
DJ
7242remote_send (char **buf,
7243 long *sizeof_buf)
c906108c 7244{
6d820c5c 7245 putpkt (*buf);
c2d11a7d 7246 getpkt (buf, sizeof_buf, 0);
c906108c 7247
6d820c5c
DJ
7248 if ((*buf)[0] == 'E')
7249 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
7250}
7251
6e5abd65
PA
7252/* Return a pointer to an xmalloc'ed string representing an escaped
7253 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7254 etc. The caller is responsible for releasing the returned
7255 memory. */
7256
7257static char *
7258escape_buffer (const char *buf, int n)
7259{
7260 struct cleanup *old_chain;
7261 struct ui_file *stb;
7262 char *str;
6e5abd65
PA
7263
7264 stb = mem_fileopen ();
7265 old_chain = make_cleanup_ui_file_delete (stb);
7266
6ef284bd 7267 fputstrn_unfiltered (buf, n, '\\', stb);
759ef836 7268 str = ui_file_xstrdup (stb, NULL);
6e5abd65
PA
7269 do_cleanups (old_chain);
7270 return str;
7271}
7272
c906108c
SS
7273/* Display a null-terminated packet on stdout, for debugging, using C
7274 string notation. */
7275
7276static void
baa336ce 7277print_packet (const char *buf)
c906108c
SS
7278{
7279 puts_filtered ("\"");
43e526b9 7280 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
7281 puts_filtered ("\"");
7282}
7283
7284int
baa336ce 7285putpkt (const char *buf)
c906108c
SS
7286{
7287 return putpkt_binary (buf, strlen (buf));
7288}
7289
7290/* Send a packet to the remote machine, with error checking. The data
23860348 7291 of the packet is in BUF. The string in BUF can be at most
ea9c271d 7292 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
7293 and for a possible /0 if we are debugging (remote_debug) and want
7294 to print the sent packet as a string. */
c906108c
SS
7295
7296static int
baa336ce 7297putpkt_binary (const char *buf, int cnt)
c906108c 7298{
2d717e4f 7299 struct remote_state *rs = get_remote_state ();
c906108c
SS
7300 int i;
7301 unsigned char csum = 0;
11cf8741 7302 char *buf2 = alloca (cnt + 6);
085dd6e6 7303
c906108c
SS
7304 int ch;
7305 int tcount = 0;
7306 char *p;
dd61ec5c 7307 char *message;
c906108c 7308
e24a49d8
PA
7309 /* Catch cases like trying to read memory or listing threads while
7310 we're waiting for a stop reply. The remote server wouldn't be
7311 ready to handle this request, so we'd hang and timeout. We don't
7312 have to worry about this in synchronous mode, because in that
7313 case it's not possible to issue a command while the target is
74531fed
PA
7314 running. This is not a problem in non-stop mode, because in that
7315 case, the stub is always ready to process serial input. */
d9d41e78 7316 if (!non_stop && target_is_async_p () && rs->waiting_for_stop_reply)
9597b22a
DE
7317 {
7318 error (_("Cannot execute this command while the target is running.\n"
7319 "Use the \"interrupt\" command to stop the target\n"
7320 "and then try again."));
7321 }
e24a49d8 7322
2d717e4f
DJ
7323 /* We're sending out a new packet. Make sure we don't look at a
7324 stale cached response. */
7325 rs->cached_wait_status = 0;
7326
c906108c
SS
7327 /* Copy the packet into buffer BUF2, encapsulating it
7328 and giving it a checksum. */
7329
c906108c
SS
7330 p = buf2;
7331 *p++ = '$';
7332
7333 for (i = 0; i < cnt; i++)
7334 {
7335 csum += buf[i];
7336 *p++ = buf[i];
7337 }
7338 *p++ = '#';
7339 *p++ = tohex ((csum >> 4) & 0xf);
7340 *p++ = tohex (csum & 0xf);
7341
7342 /* Send it over and over until we get a positive ack. */
7343
7344 while (1)
7345 {
7346 int started_error_output = 0;
7347
7348 if (remote_debug)
7349 {
6e5abd65
PA
7350 struct cleanup *old_chain;
7351 char *str;
7352
c906108c 7353 *p = '\0';
6e5abd65
PA
7354 str = escape_buffer (buf2, p - buf2);
7355 old_chain = make_cleanup (xfree, str);
7356 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
0f71a2f6 7357 gdb_flush (gdb_stdlog);
6e5abd65 7358 do_cleanups (old_chain);
c906108c 7359 }
c33e31fd 7360 remote_serial_write (buf2, p - buf2);
c906108c 7361
a6f3e723
SL
7362 /* If this is a no acks version of the remote protocol, send the
7363 packet and move on. */
7364 if (rs->noack_mode)
7365 break;
7366
74531fed
PA
7367 /* Read until either a timeout occurs (-2) or '+' is read.
7368 Handle any notification that arrives in the mean time. */
c906108c
SS
7369 while (1)
7370 {
7371 ch = readchar (remote_timeout);
7372
c5aa993b 7373 if (remote_debug)
c906108c
SS
7374 {
7375 switch (ch)
7376 {
7377 case '+':
1216fa2c 7378 case '-':
c906108c
SS
7379 case SERIAL_TIMEOUT:
7380 case '$':
74531fed 7381 case '%':
c906108c
SS
7382 if (started_error_output)
7383 {
7384 putchar_unfiltered ('\n');
7385 started_error_output = 0;
7386 }
7387 }
7388 }
7389
7390 switch (ch)
7391 {
7392 case '+':
7393 if (remote_debug)
0f71a2f6 7394 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 7395 return 1;
1216fa2c
AC
7396 case '-':
7397 if (remote_debug)
7398 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 7399 /* FALLTHROUGH */
c906108c 7400 case SERIAL_TIMEOUT:
c5aa993b 7401 tcount++;
c906108c
SS
7402 if (tcount > 3)
7403 return 0;
23860348 7404 break; /* Retransmit buffer. */
c906108c
SS
7405 case '$':
7406 {
40e3f985 7407 if (remote_debug)
2bc416ba 7408 fprintf_unfiltered (gdb_stdlog,
23860348 7409 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
7410 /* It's probably an old response sent because an ACK
7411 was lost. Gobble up the packet and ack it so it
7412 doesn't get retransmitted when we resend this
7413 packet. */
6d820c5c 7414 skip_frame ();
c33e31fd 7415 remote_serial_write ("+", 1);
23860348 7416 continue; /* Now, go look for +. */
c906108c 7417 }
74531fed
PA
7418
7419 case '%':
7420 {
7421 int val;
7422
7423 /* If we got a notification, handle it, and go back to looking
7424 for an ack. */
7425 /* We've found the start of a notification. Now
7426 collect the data. */
7427 val = read_frame (&rs->buf, &rs->buf_size);
7428 if (val >= 0)
7429 {
7430 if (remote_debug)
7431 {
6e5abd65
PA
7432 struct cleanup *old_chain;
7433 char *str;
7434
7435 str = escape_buffer (rs->buf, val);
7436 old_chain = make_cleanup (xfree, str);
7437 fprintf_unfiltered (gdb_stdlog,
7438 " Notification received: %s\n",
7439 str);
7440 do_cleanups (old_chain);
74531fed 7441 }
5965e028 7442 handle_notification (rs->notif_state, rs->buf);
74531fed
PA
7443 /* We're in sync now, rewait for the ack. */
7444 tcount = 0;
7445 }
7446 else
7447 {
7448 if (remote_debug)
7449 {
7450 if (!started_error_output)
7451 {
7452 started_error_output = 1;
7453 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7454 }
7455 fputc_unfiltered (ch & 0177, gdb_stdlog);
7456 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7457 }
7458 }
7459 continue;
7460 }
7461 /* fall-through */
c906108c
SS
7462 default:
7463 if (remote_debug)
7464 {
7465 if (!started_error_output)
7466 {
7467 started_error_output = 1;
0f71a2f6 7468 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 7469 }
0f71a2f6 7470 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
7471 }
7472 continue;
7473 }
23860348 7474 break; /* Here to retransmit. */
c906108c
SS
7475 }
7476
7477#if 0
7478 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
7479 able to get out next time we call QUIT, without anything as
7480 violent as interrupt_query. If we want to provide a way out of
7481 here without getting to the next QUIT, it should be based on
7482 hitting ^C twice as in remote_wait. */
c906108c
SS
7483 if (quit_flag)
7484 {
7485 quit_flag = 0;
7486 interrupt_query ();
7487 }
7488#endif
7489 }
a6f3e723 7490 return 0;
c906108c
SS
7491}
7492
6d820c5c
DJ
7493/* Come here after finding the start of a frame when we expected an
7494 ack. Do our best to discard the rest of this packet. */
7495
7496static void
7497skip_frame (void)
7498{
7499 int c;
7500
7501 while (1)
7502 {
7503 c = readchar (remote_timeout);
7504 switch (c)
7505 {
7506 case SERIAL_TIMEOUT:
7507 /* Nothing we can do. */
7508 return;
7509 case '#':
7510 /* Discard the two bytes of checksum and stop. */
7511 c = readchar (remote_timeout);
7512 if (c >= 0)
7513 c = readchar (remote_timeout);
7514
7515 return;
7516 case '*': /* Run length encoding. */
7517 /* Discard the repeat count. */
7518 c = readchar (remote_timeout);
7519 if (c < 0)
7520 return;
7521 break;
7522 default:
7523 /* A regular character. */
7524 break;
7525 }
7526 }
7527}
7528
c906108c 7529/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
7530 into *BUF, verifying the checksum, length, and handling run-length
7531 compression. NUL terminate the buffer. If there is not enough room,
7532 expand *BUF using xrealloc.
c906108c 7533
c2d11a7d
JM
7534 Returns -1 on error, number of characters in buffer (ignoring the
7535 trailing NULL) on success. (could be extended to return one of the
23860348 7536 SERIAL status indications). */
c2d11a7d
JM
7537
7538static long
6d820c5c
DJ
7539read_frame (char **buf_p,
7540 long *sizeof_buf)
c906108c
SS
7541{
7542 unsigned char csum;
c2d11a7d 7543 long bc;
c906108c 7544 int c;
6d820c5c 7545 char *buf = *buf_p;
a6f3e723 7546 struct remote_state *rs = get_remote_state ();
c906108c
SS
7547
7548 csum = 0;
c2d11a7d 7549 bc = 0;
c906108c
SS
7550
7551 while (1)
7552 {
7553 c = readchar (remote_timeout);
c906108c
SS
7554 switch (c)
7555 {
7556 case SERIAL_TIMEOUT:
7557 if (remote_debug)
0f71a2f6 7558 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 7559 return -1;
c906108c
SS
7560 case '$':
7561 if (remote_debug)
0f71a2f6
JM
7562 fputs_filtered ("Saw new packet start in middle of old one\n",
7563 gdb_stdlog);
23860348 7564 return -1; /* Start a new packet, count retries. */
c906108c
SS
7565 case '#':
7566 {
7567 unsigned char pktcsum;
e1b09194
AC
7568 int check_0 = 0;
7569 int check_1 = 0;
c906108c 7570
c2d11a7d 7571 buf[bc] = '\0';
c906108c 7572
e1b09194
AC
7573 check_0 = readchar (remote_timeout);
7574 if (check_0 >= 0)
7575 check_1 = readchar (remote_timeout);
802188a7 7576
e1b09194
AC
7577 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7578 {
7579 if (remote_debug)
2bc416ba 7580 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 7581 gdb_stdlog);
e1b09194
AC
7582 return -1;
7583 }
7584 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
7585 {
7586 if (remote_debug)
2bc416ba 7587 fputs_filtered ("Communication error in checksum\n",
23860348 7588 gdb_stdlog);
40e3f985
FN
7589 return -1;
7590 }
c906108c 7591
a6f3e723
SL
7592 /* Don't recompute the checksum; with no ack packets we
7593 don't have any way to indicate a packet retransmission
7594 is necessary. */
7595 if (rs->noack_mode)
7596 return bc;
7597
e1b09194 7598 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 7599 if (csum == pktcsum)
c2d11a7d 7600 return bc;
c906108c 7601
c5aa993b 7602 if (remote_debug)
c906108c 7603 {
6e5abd65
PA
7604 struct cleanup *old_chain;
7605 char *str;
7606
7607 str = escape_buffer (buf, bc);
7608 old_chain = make_cleanup (xfree, str);
7609 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
7610 "Bad checksum, sentsum=0x%x, "
7611 "csum=0x%x, buf=%s\n",
6e5abd65
PA
7612 pktcsum, csum, str);
7613 do_cleanups (old_chain);
c906108c 7614 }
c2d11a7d 7615 /* Number of characters in buffer ignoring trailing
23860348 7616 NULL. */
c2d11a7d 7617 return -1;
c906108c 7618 }
23860348 7619 case '*': /* Run length encoding. */
c2c6d25f
JM
7620 {
7621 int repeat;
c906108c 7622
a744cf53 7623 csum += c;
b4501125
AC
7624 c = readchar (remote_timeout);
7625 csum += c;
23860348 7626 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 7627
23860348 7628 /* The character before ``*'' is repeated. */
c2d11a7d 7629
6d820c5c 7630 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 7631 {
6d820c5c
DJ
7632 if (bc + repeat - 1 >= *sizeof_buf - 1)
7633 {
7634 /* Make some more room in the buffer. */
7635 *sizeof_buf += repeat;
7636 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7637 buf = *buf_p;
7638 }
7639
c2d11a7d
JM
7640 memset (&buf[bc], buf[bc - 1], repeat);
7641 bc += repeat;
c2c6d25f
JM
7642 continue;
7643 }
7644
c2d11a7d 7645 buf[bc] = '\0';
6d820c5c 7646 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 7647 return -1;
c2c6d25f 7648 }
c906108c 7649 default:
6d820c5c 7650 if (bc >= *sizeof_buf - 1)
c906108c 7651 {
6d820c5c
DJ
7652 /* Make some more room in the buffer. */
7653 *sizeof_buf *= 2;
7654 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7655 buf = *buf_p;
c906108c
SS
7656 }
7657
6d820c5c
DJ
7658 buf[bc++] = c;
7659 csum += c;
7660 continue;
c906108c
SS
7661 }
7662 }
7663}
7664
7665/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7666 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7667 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7668 rather than timing out; this is used (in synchronous mode) to wait
7669 for a target that is is executing user code to stop. */
d9fcf2fb
JM
7670/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7671 don't have to change all the calls to getpkt to deal with the
7672 return value, because at the moment I don't know what the right
23860348 7673 thing to do it for those. */
c906108c 7674void
6d820c5c
DJ
7675getpkt (char **buf,
7676 long *sizeof_buf,
c2d11a7d 7677 int forever)
d9fcf2fb
JM
7678{
7679 int timed_out;
7680
7681 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7682}
7683
7684
7685/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7686 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7687 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7688 rather than timing out; this is used (in synchronous mode) to wait
7689 for a target that is is executing user code to stop. If FOREVER ==
7690 0, this function is allowed to time out gracefully and return an
74531fed
PA
7691 indication of this to the caller. Otherwise return the number of
7692 bytes read. If EXPECTING_NOTIF, consider receiving a notification
fee9eda9
YQ
7693 enough reason to return to the caller. *IS_NOTIF is an output
7694 boolean that indicates whether *BUF holds a notification or not
7695 (a regular packet). */
74531fed 7696
3172dc30 7697static int
74531fed 7698getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
fee9eda9 7699 int expecting_notif, int *is_notif)
c906108c 7700{
2d717e4f 7701 struct remote_state *rs = get_remote_state ();
c906108c
SS
7702 int c;
7703 int tries;
7704 int timeout;
df4b58fe 7705 int val = -1;
c906108c 7706
2d717e4f
DJ
7707 /* We're reading a new response. Make sure we don't look at a
7708 previously cached response. */
7709 rs->cached_wait_status = 0;
7710
6d820c5c 7711 strcpy (*buf, "timeout");
c906108c
SS
7712
7713 if (forever)
74531fed
PA
7714 timeout = watchdog > 0 ? watchdog : -1;
7715 else if (expecting_notif)
7716 timeout = 0; /* There should already be a char in the buffer. If
7717 not, bail out. */
c906108c
SS
7718 else
7719 timeout = remote_timeout;
7720
7721#define MAX_TRIES 3
7722
74531fed
PA
7723 /* Process any number of notifications, and then return when
7724 we get a packet. */
7725 for (;;)
c906108c 7726 {
d9c43928 7727 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
74531fed
PA
7728 times. */
7729 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 7730 {
74531fed
PA
7731 /* This can loop forever if the remote side sends us
7732 characters continuously, but if it pauses, we'll get
7733 SERIAL_TIMEOUT from readchar because of timeout. Then
7734 we'll count that as a retry.
7735
7736 Note that even when forever is set, we will only wait
7737 forever prior to the start of a packet. After that, we
7738 expect characters to arrive at a brisk pace. They should
7739 show up within remote_timeout intervals. */
7740 do
7741 c = readchar (timeout);
7742 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
7743
7744 if (c == SERIAL_TIMEOUT)
7745 {
74531fed
PA
7746 if (expecting_notif)
7747 return -1; /* Don't complain, it's normal to not get
7748 anything in this case. */
7749
23860348 7750 if (forever) /* Watchdog went off? Kill the target. */
c906108c 7751 {
2acceee2 7752 QUIT;
78a095c3 7753 remote_unpush_target ();
598d3636
JK
7754 throw_error (TARGET_CLOSE_ERROR,
7755 _("Watchdog timeout has expired. "
7756 "Target detached."));
c906108c 7757 }
c906108c 7758 if (remote_debug)
0f71a2f6 7759 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 7760 }
74531fed
PA
7761 else
7762 {
7763 /* We've found the start of a packet or notification.
7764 Now collect the data. */
7765 val = read_frame (buf, sizeof_buf);
7766 if (val >= 0)
7767 break;
7768 }
7769
c33e31fd 7770 remote_serial_write ("-", 1);
c906108c 7771 }
c906108c 7772
74531fed
PA
7773 if (tries > MAX_TRIES)
7774 {
7775 /* We have tried hard enough, and just can't receive the
7776 packet/notification. Give up. */
7777 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 7778
74531fed
PA
7779 /* Skip the ack char if we're in no-ack mode. */
7780 if (!rs->noack_mode)
c33e31fd 7781 remote_serial_write ("+", 1);
74531fed
PA
7782 return -1;
7783 }
c906108c 7784
74531fed
PA
7785 /* If we got an ordinary packet, return that to our caller. */
7786 if (c == '$')
c906108c
SS
7787 {
7788 if (remote_debug)
43e526b9 7789 {
6e5abd65
PA
7790 struct cleanup *old_chain;
7791 char *str;
7792
7793 str = escape_buffer (*buf, val);
7794 old_chain = make_cleanup (xfree, str);
7795 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
7796 do_cleanups (old_chain);
43e526b9 7797 }
a6f3e723
SL
7798
7799 /* Skip the ack char if we're in no-ack mode. */
7800 if (!rs->noack_mode)
c33e31fd 7801 remote_serial_write ("+", 1);
fee9eda9
YQ
7802 if (is_notif != NULL)
7803 *is_notif = 0;
0876f84a 7804 return val;
c906108c
SS
7805 }
7806
74531fed
PA
7807 /* If we got a notification, handle it, and go back to looking
7808 for a packet. */
7809 else
7810 {
7811 gdb_assert (c == '%');
7812
7813 if (remote_debug)
7814 {
6e5abd65
PA
7815 struct cleanup *old_chain;
7816 char *str;
7817
7818 str = escape_buffer (*buf, val);
7819 old_chain = make_cleanup (xfree, str);
7820 fprintf_unfiltered (gdb_stdlog,
7821 " Notification received: %s\n",
7822 str);
7823 do_cleanups (old_chain);
74531fed 7824 }
fee9eda9
YQ
7825 if (is_notif != NULL)
7826 *is_notif = 1;
c906108c 7827
5965e028 7828 handle_notification (rs->notif_state, *buf);
c906108c 7829
74531fed 7830 /* Notifications require no acknowledgement. */
a6f3e723 7831
74531fed 7832 if (expecting_notif)
fee9eda9 7833 return val;
74531fed
PA
7834 }
7835 }
7836}
7837
7838static int
7839getpkt_sane (char **buf, long *sizeof_buf, int forever)
7840{
fee9eda9 7841 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
74531fed
PA
7842}
7843
7844static int
fee9eda9
YQ
7845getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
7846 int *is_notif)
74531fed 7847{
fee9eda9
YQ
7848 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
7849 is_notif);
c906108c 7850}
74531fed 7851
c906108c
SS
7852\f
7853static void
7d85a9c0 7854remote_kill (struct target_ops *ops)
43ff13b4 7855{
0fdf84ca
PA
7856
7857 /* Catch errors so the user can quit from gdb even when we
23860348 7858 aren't on speaking terms with the remote system. */
492d29ea 7859 TRY
0fdf84ca
PA
7860 {
7861 putpkt ("k");
7862 }
492d29ea 7863 CATCH (ex, RETURN_MASK_ERROR)
0fdf84ca
PA
7864 {
7865 if (ex.error == TARGET_CLOSE_ERROR)
7866 {
7867 /* If we got an (EOF) error that caused the target
7868 to go away, then we're done, that's what we wanted.
7869 "k" is susceptible to cause a premature EOF, given
7870 that the remote server isn't actually required to
7871 reply to "k", and it can happen that it doesn't
7872 even get to reply ACK to the "k". */
7873 return;
7874 }
7875
7876 /* Otherwise, something went wrong. We didn't actually kill
7877 the target. Just propagate the exception, and let the
7878 user or higher layers decide what to do. */
7879 throw_exception (ex);
7880 }
492d29ea 7881 END_CATCH
43ff13b4 7882
0fdf84ca
PA
7883 /* We've killed the remote end, we get to mourn it. Since this is
7884 target remote, single-process, mourning the inferior also
7885 unpushes remote_ops. */
43ff13b4
JM
7886 target_mourn_inferior ();
7887}
7888
82f73884
PA
7889static int
7890remote_vkill (int pid, struct remote_state *rs)
7891{
4082afcc 7892 if (packet_support (PACKET_vKill) == PACKET_DISABLE)
82f73884
PA
7893 return -1;
7894
7895 /* Tell the remote target to detach. */
bba74b36 7896 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
7897 putpkt (rs->buf);
7898 getpkt (&rs->buf, &rs->buf_size, 0);
7899
4082afcc
PA
7900 switch (packet_ok (rs->buf,
7901 &remote_protocol_packets[PACKET_vKill]))
7902 {
7903 case PACKET_OK:
7904 return 0;
7905 case PACKET_ERROR:
7906 return 1;
7907 case PACKET_UNKNOWN:
7908 return -1;
7909 default:
7910 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
7911 }
82f73884
PA
7912}
7913
7914static void
7d85a9c0 7915extended_remote_kill (struct target_ops *ops)
82f73884
PA
7916{
7917 int res;
7918 int pid = ptid_get_pid (inferior_ptid);
7919 struct remote_state *rs = get_remote_state ();
7920
7921 res = remote_vkill (pid, rs);
901f9912 7922 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
82f73884
PA
7923 {
7924 /* Don't try 'k' on a multi-process aware stub -- it has no way
7925 to specify the pid. */
7926
7927 putpkt ("k");
7928#if 0
7929 getpkt (&rs->buf, &rs->buf_size, 0);
7930 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
7931 res = 1;
7932#else
7933 /* Don't wait for it to die. I'm not really sure it matters whether
7934 we do or not. For the existing stubs, kill is a noop. */
7935 res = 0;
7936#endif
7937 }
7938
7939 if (res != 0)
7940 error (_("Can't kill process"));
7941
82f73884
PA
7942 target_mourn_inferior ();
7943}
7944
c906108c 7945static void
20f796c9 7946remote_mourn (struct target_ops *target)
c906108c
SS
7947{
7948 unpush_target (target);
ce5ce7ed 7949
8a2492ee
PA
7950 /* remote_close takes care of doing most of the clean up. */
7951 generic_mourn_inferior ();
c906108c
SS
7952}
7953
2d717e4f 7954static void
20f796c9 7955extended_remote_mourn (struct target_ops *target)
2d717e4f
DJ
7956{
7957 struct remote_state *rs = get_remote_state ();
c906108c 7958
e24a49d8
PA
7959 /* In case we got here due to an error, but we're going to stay
7960 connected. */
7961 rs->waiting_for_stop_reply = 0;
7962
dc1981d7
PA
7963 /* If the current general thread belonged to the process we just
7964 detached from or has exited, the remote side current general
7965 thread becomes undefined. Considering a case like this:
7966
7967 - We just got here due to a detach.
7968 - The process that we're detaching from happens to immediately
7969 report a global breakpoint being hit in non-stop mode, in the
7970 same thread we had selected before.
7971 - GDB attaches to this process again.
7972 - This event happens to be the next event we handle.
7973
7974 GDB would consider that the current general thread didn't need to
7975 be set on the stub side (with Hg), since for all it knew,
7976 GENERAL_THREAD hadn't changed.
7977
7978 Notice that although in all-stop mode, the remote server always
7979 sets the current thread to the thread reporting the stop event,
7980 that doesn't happen in non-stop mode; in non-stop, the stub *must
7981 not* change the current thread when reporting a breakpoint hit,
7982 due to the decoupling of event reporting and event handling.
7983
7984 To keep things simple, we always invalidate our notion of the
7985 current thread. */
47f8a51d 7986 record_currthread (rs, minus_one_ptid);
dc1981d7 7987
2d717e4f
DJ
7988 /* Unlike "target remote", we do not want to unpush the target; then
7989 the next time the user says "run", we won't be connected. */
7990
48aa3c27
PA
7991 /* Call common code to mark the inferior as not running. */
7992 generic_mourn_inferior ();
7993
d729566a 7994 if (!have_inferiors ())
2d717e4f 7995 {
82f73884
PA
7996 if (!remote_multi_process_p (rs))
7997 {
7998 /* Check whether the target is running now - some remote stubs
7999 automatically restart after kill. */
8000 putpkt ("?");
8001 getpkt (&rs->buf, &rs->buf_size, 0);
8002
8003 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
8004 {
3e43a32a
MS
8005 /* Assume that the target has been restarted. Set
8006 inferior_ptid so that bits of core GDB realizes
8007 there's something here, e.g., so that the user can
8008 say "kill" again. */
82f73884
PA
8009 inferior_ptid = magic_null_ptid;
8010 }
82f73884 8011 }
2d717e4f
DJ
8012 }
8013}
c906108c 8014
03583c20 8015static int
2bfc0540 8016extended_remote_supports_disable_randomization (struct target_ops *self)
03583c20 8017{
4082afcc 8018 return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
03583c20
UW
8019}
8020
8021static void
8022extended_remote_disable_randomization (int val)
8023{
8024 struct remote_state *rs = get_remote_state ();
8025 char *reply;
8026
bba74b36
YQ
8027 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
8028 val);
03583c20
UW
8029 putpkt (rs->buf);
8030 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
8031 if (*reply == '\0')
8032 error (_("Target does not support QDisableRandomization."));
8033 if (strcmp (reply, "OK") != 0)
8034 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
8035}
8036
2d717e4f
DJ
8037static int
8038extended_remote_run (char *args)
8039{
8040 struct remote_state *rs = get_remote_state ();
2d717e4f 8041 int len;
c906108c 8042
2d717e4f
DJ
8043 /* If the user has disabled vRun support, or we have detected that
8044 support is not available, do not try it. */
4082afcc 8045 if (packet_support (PACKET_vRun) == PACKET_DISABLE)
2d717e4f 8046 return -1;
424163ea 8047
2d717e4f
DJ
8048 strcpy (rs->buf, "vRun;");
8049 len = strlen (rs->buf);
c906108c 8050
2d717e4f
DJ
8051 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
8052 error (_("Remote file name too long for run packet"));
9f1b45b0
TT
8053 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len,
8054 strlen (remote_exec_file));
2d717e4f 8055
d1a41061 8056 gdb_assert (args != NULL);
2d717e4f
DJ
8057 if (*args)
8058 {
8059 struct cleanup *back_to;
8060 int i;
8061 char **argv;
8062
d1a41061 8063 argv = gdb_buildargv (args);
6e366df1 8064 back_to = make_cleanup_freeargv (argv);
2d717e4f
DJ
8065 for (i = 0; argv[i] != NULL; i++)
8066 {
8067 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
8068 error (_("Argument list too long for run packet"));
8069 rs->buf[len++] = ';';
9f1b45b0
TT
8070 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len,
8071 strlen (argv[i]));
2d717e4f
DJ
8072 }
8073 do_cleanups (back_to);
8074 }
8075
8076 rs->buf[len++] = '\0';
8077
8078 putpkt (rs->buf);
8079 getpkt (&rs->buf, &rs->buf_size, 0);
8080
4082afcc 8081 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
2d717e4f 8082 {
4082afcc 8083 case PACKET_OK:
3405876a 8084 /* We have a wait response. All is well. */
2d717e4f 8085 return 0;
4082afcc
PA
8086 case PACKET_UNKNOWN:
8087 return -1;
8088 case PACKET_ERROR:
2d717e4f
DJ
8089 if (remote_exec_file[0] == '\0')
8090 error (_("Running the default executable on the remote target failed; "
8091 "try \"set remote exec-file\"?"));
8092 else
8093 error (_("Running \"%s\" on the remote target failed"),
8094 remote_exec_file);
4082afcc
PA
8095 default:
8096 gdb_assert_not_reached (_("bad switch"));
2d717e4f 8097 }
c906108c
SS
8098}
8099
2d717e4f
DJ
8100/* In the extended protocol we want to be able to do things like
8101 "run" and have them basically work as expected. So we need
8102 a special create_inferior function. We support changing the
8103 executable file and the command line arguments, but not the
8104 environment. */
8105
43ff13b4 8106static void
77a19445
TT
8107extended_remote_create_inferior (struct target_ops *ops,
8108 char *exec_file, char *args,
8109 char **env, int from_tty)
43ff13b4 8110{
3405876a
PA
8111 int run_worked;
8112 char *stop_reply;
8113 struct remote_state *rs = get_remote_state ();
8114
43ff13b4 8115 /* If running asynchronously, register the target file descriptor
23860348 8116 with the event loop. */
75c99385 8117 if (target_can_async_p ())
6a3753b3 8118 target_async (1);
43ff13b4 8119
03583c20 8120 /* Disable address space randomization if requested (and supported). */
2bfc0540 8121 if (extended_remote_supports_disable_randomization (ops))
03583c20
UW
8122 extended_remote_disable_randomization (disable_randomization);
8123
43ff13b4 8124 /* Now restart the remote server. */
3405876a
PA
8125 run_worked = extended_remote_run (args) != -1;
8126 if (!run_worked)
2d717e4f
DJ
8127 {
8128 /* vRun was not supported. Fail if we need it to do what the
8129 user requested. */
8130 if (remote_exec_file[0])
8131 error (_("Remote target does not support \"set remote exec-file\""));
8132 if (args[0])
8133 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 8134
2d717e4f
DJ
8135 /* Fall back to "R". */
8136 extended_remote_restart ();
8137 }
424163ea 8138
6c95b8df
PA
8139 if (!have_inferiors ())
8140 {
8141 /* Clean up from the last time we ran, before we mark the target
8142 running again. This will mark breakpoints uninserted, and
8143 get_offsets may insert breakpoints. */
8144 init_thread_list ();
8145 init_wait_for_inferior ();
8146 }
45280a52 8147
3405876a
PA
8148 /* vRun's success return is a stop reply. */
8149 stop_reply = run_worked ? rs->buf : NULL;
8150 add_current_inferior_and_thread (stop_reply);
c0a2216e 8151
2d717e4f
DJ
8152 /* Get updated offsets, if the stub uses qOffsets. */
8153 get_offsets ();
2d717e4f 8154}
c906108c 8155\f
c5aa993b 8156
b775012e
LM
8157/* Given a location's target info BP_TGT and the packet buffer BUF, output
8158 the list of conditions (in agent expression bytecode format), if any, the
8159 target needs to evaluate. The output is placed into the packet buffer
bba74b36 8160 started from BUF and ended at BUF_END. */
b775012e
LM
8161
8162static int
8163remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
8164 struct bp_target_info *bp_tgt, char *buf,
8165 char *buf_end)
b775012e
LM
8166{
8167 struct agent_expr *aexpr = NULL;
8168 int i, ix;
8169 char *pkt;
8170 char *buf_start = buf;
8171
8172 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8173 return 0;
8174
8175 buf += strlen (buf);
bba74b36 8176 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
8177 buf++;
8178
8179 /* Send conditions to the target and free the vector. */
8180 for (ix = 0;
8181 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8182 ix++)
8183 {
bba74b36 8184 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e
LM
8185 buf += strlen (buf);
8186 for (i = 0; i < aexpr->len; ++i)
8187 buf = pack_hex_byte (buf, aexpr->buf[i]);
8188 *buf = '\0';
8189 }
b775012e
LM
8190 return 0;
8191}
8192
d3ce09f5
SS
8193static void
8194remote_add_target_side_commands (struct gdbarch *gdbarch,
8195 struct bp_target_info *bp_tgt, char *buf)
8196{
8197 struct agent_expr *aexpr = NULL;
8198 int i, ix;
8199
8200 if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8201 return;
8202
8203 buf += strlen (buf);
8204
8205 sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8206 buf += strlen (buf);
8207
8208 /* Concatenate all the agent expressions that are commands into the
8209 cmds parameter. */
8210 for (ix = 0;
8211 VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8212 ix++)
8213 {
8214 sprintf (buf, "X%x,", aexpr->len);
8215 buf += strlen (buf);
8216 for (i = 0; i < aexpr->len; ++i)
8217 buf = pack_hex_byte (buf, aexpr->buf[i]);
8218 *buf = '\0';
8219 }
d3ce09f5
SS
8220}
8221
8181d85f
DJ
8222/* Insert a breakpoint. On targets that have software breakpoint
8223 support, we ask the remote target to do the work; on targets
8224 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
8225
8226static int
3db08215
MM
8227remote_insert_breakpoint (struct target_ops *ops,
8228 struct gdbarch *gdbarch,
a6d9a66e 8229 struct bp_target_info *bp_tgt)
c906108c 8230{
d471ea57
AC
8231 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8232 If it succeeds, then set the support to PACKET_ENABLE. If it
8233 fails, and the user has explicitly requested the Z support then
23860348 8234 report an error, otherwise, mark it disabled and go on. */
802188a7 8235
4082afcc 8236 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8237 {
0d5ed153 8238 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8239 struct remote_state *rs;
bba74b36 8240 char *p, *endbuf;
7c0f6dcc 8241 int bpsize;
b775012e 8242 struct condition_list *cond = NULL;
4fff2411 8243
28439a30
PA
8244 /* Make sure the remote is pointing at the right process, if
8245 necessary. */
8246 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8247 set_general_process ();
8248
a1dcb23a 8249 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
4fff2411
JZ
8250
8251 rs = get_remote_state ();
8252 p = rs->buf;
bba74b36 8253 endbuf = rs->buf + get_remote_packet_size ();
802188a7 8254
96baa820
JM
8255 *(p++) = 'Z';
8256 *(p++) = '0';
8257 *(p++) = ',';
7c0f6dcc 8258 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 8259 p += hexnumstr (p, addr);
bba74b36 8260 xsnprintf (p, endbuf - p, ",%d", bpsize);
802188a7 8261
efcc2da7 8262 if (remote_supports_cond_breakpoints (ops))
bba74b36 8263 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8264
78eff0ec 8265 if (remote_can_run_breakpoint_commands (ops))
d3ce09f5
SS
8266 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8267
6d820c5c
DJ
8268 putpkt (rs->buf);
8269 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8270
6d820c5c 8271 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 8272 {
d471ea57
AC
8273 case PACKET_ERROR:
8274 return -1;
8275 case PACKET_OK:
7c0f6dcc
JL
8276 bp_tgt->placed_address = addr;
8277 bp_tgt->placed_size = bpsize;
d471ea57
AC
8278 return 0;
8279 case PACKET_UNKNOWN:
8280 break;
96baa820
JM
8281 }
8282 }
c906108c 8283
0000e5cc
PA
8284 /* If this breakpoint has target-side commands but this stub doesn't
8285 support Z0 packets, throw error. */
8286 if (!VEC_empty (agent_expr_p, bp_tgt->tcommands))
8287 throw_error (NOT_SUPPORTED_ERROR, _("\
8288Target doesn't support breakpoints that have target side commands."));
8289
3db08215 8290 return memory_insert_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8291}
8292
8293static int
3db08215
MM
8294remote_remove_breakpoint (struct target_ops *ops,
8295 struct gdbarch *gdbarch,
a6d9a66e 8296 struct bp_target_info *bp_tgt)
c906108c 8297{
8181d85f 8298 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 8299 struct remote_state *rs = get_remote_state ();
96baa820 8300
4082afcc 8301 if (packet_support (PACKET_Z0) != PACKET_DISABLE)
96baa820 8302 {
6d820c5c 8303 char *p = rs->buf;
bba74b36 8304 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 8305
28439a30
PA
8306 /* Make sure the remote is pointing at the right process, if
8307 necessary. */
8308 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8309 set_general_process ();
8310
96baa820
JM
8311 *(p++) = 'z';
8312 *(p++) = '0';
8313 *(p++) = ',';
8314
8181d85f
DJ
8315 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
8316 p += hexnumstr (p, addr);
bba74b36 8317 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
802188a7 8318
6d820c5c
DJ
8319 putpkt (rs->buf);
8320 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8321
6d820c5c 8322 return (rs->buf[0] == 'E');
96baa820
JM
8323 }
8324
3db08215 8325 return memory_remove_breakpoint (ops, gdbarch, bp_tgt);
c906108c
SS
8326}
8327
d471ea57
AC
8328static int
8329watchpoint_to_Z_packet (int type)
8330{
8331 switch (type)
8332 {
8333 case hw_write:
bb858e6a 8334 return Z_PACKET_WRITE_WP;
d471ea57
AC
8335 break;
8336 case hw_read:
bb858e6a 8337 return Z_PACKET_READ_WP;
d471ea57
AC
8338 break;
8339 case hw_access:
bb858e6a 8340 return Z_PACKET_ACCESS_WP;
d471ea57
AC
8341 break;
8342 default:
8e65ff28 8343 internal_error (__FILE__, __LINE__,
e2e0b3e5 8344 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
8345 }
8346}
8347
3c3bea1c 8348static int
7bb99c53
TT
8349remote_insert_watchpoint (struct target_ops *self,
8350 CORE_ADDR addr, int len, int type,
0cf6dd15 8351 struct expression *cond)
96baa820 8352{
d01949b6 8353 struct remote_state *rs = get_remote_state ();
bba74b36 8354 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8355 char *p;
d471ea57 8356 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 8357
4082afcc 8358 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
85d721b8 8359 return 1;
802188a7 8360
28439a30
PA
8361 /* Make sure the remote is pointing at the right process, if
8362 necessary. */
8363 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8364 set_general_process ();
8365
bba74b36 8366 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 8367 p = strchr (rs->buf, '\0');
96baa820
JM
8368 addr = remote_address_masked (addr);
8369 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8370 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 8371
6d820c5c
DJ
8372 putpkt (rs->buf);
8373 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8374
6d820c5c 8375 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8376 {
8377 case PACKET_ERROR:
d471ea57 8378 return -1;
85d721b8
PA
8379 case PACKET_UNKNOWN:
8380 return 1;
d471ea57
AC
8381 case PACKET_OK:
8382 return 0;
8383 }
8e65ff28 8384 internal_error (__FILE__, __LINE__,
e2e0b3e5 8385 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
8386}
8387
283002cf
MR
8388static int
8389remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8390 CORE_ADDR start, int length)
8391{
8392 CORE_ADDR diff = remote_address_masked (addr - start);
8393
8394 return diff < length;
8395}
8396
d471ea57 8397
3c3bea1c 8398static int
11b5219a
TT
8399remote_remove_watchpoint (struct target_ops *self,
8400 CORE_ADDR addr, int len, int type,
0cf6dd15 8401 struct expression *cond)
96baa820 8402{
d01949b6 8403 struct remote_state *rs = get_remote_state ();
bba74b36 8404 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8405 char *p;
d471ea57
AC
8406 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8407
4082afcc 8408 if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
5cffb350 8409 return -1;
802188a7 8410
28439a30
PA
8411 /* Make sure the remote is pointing at the right process, if
8412 necessary. */
8413 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8414 set_general_process ();
8415
bba74b36 8416 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 8417 p = strchr (rs->buf, '\0');
96baa820
JM
8418 addr = remote_address_masked (addr);
8419 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8420 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
8421 putpkt (rs->buf);
8422 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8423
6d820c5c 8424 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8425 {
8426 case PACKET_ERROR:
8427 case PACKET_UNKNOWN:
8428 return -1;
8429 case PACKET_OK:
8430 return 0;
8431 }
8e65ff28 8432 internal_error (__FILE__, __LINE__,
e2e0b3e5 8433 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
8434}
8435
3c3bea1c 8436
501eef12 8437int remote_hw_watchpoint_limit = -1;
480a3f21 8438int remote_hw_watchpoint_length_limit = -1;
501eef12 8439int remote_hw_breakpoint_limit = -1;
d471ea57 8440
480a3f21 8441static int
31568a15
TT
8442remote_region_ok_for_hw_watchpoint (struct target_ops *self,
8443 CORE_ADDR addr, int len)
480a3f21
PW
8444{
8445 if (remote_hw_watchpoint_length_limit == 0)
8446 return 0;
8447 else if (remote_hw_watchpoint_length_limit < 0)
8448 return 1;
8449 else if (len <= remote_hw_watchpoint_length_limit)
8450 return 1;
8451 else
8452 return 0;
8453}
8454
b9362cc7 8455static int
5461485a
TT
8456remote_check_watch_resources (struct target_ops *self,
8457 int type, int cnt, int ot)
96baa820 8458{
3c3bea1c
GS
8459 if (type == bp_hardware_breakpoint)
8460 {
8461 if (remote_hw_breakpoint_limit == 0)
8462 return 0;
501eef12
AC
8463 else if (remote_hw_breakpoint_limit < 0)
8464 return 1;
3c3bea1c
GS
8465 else if (cnt <= remote_hw_breakpoint_limit)
8466 return 1;
8467 }
8468 else
8469 {
8470 if (remote_hw_watchpoint_limit == 0)
8471 return 0;
501eef12
AC
8472 else if (remote_hw_watchpoint_limit < 0)
8473 return 1;
3c3bea1c
GS
8474 else if (ot)
8475 return -1;
8476 else if (cnt <= remote_hw_watchpoint_limit)
8477 return 1;
8478 }
8479 return -1;
8480}
8481
f7e6eed5
PA
8482/* The to_stopped_by_sw_breakpoint method of target remote. */
8483
8484static int
8485remote_stopped_by_sw_breakpoint (struct target_ops *ops)
8486{
8487 struct remote_state *rs = get_remote_state ();
8488
8489 return rs->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
8490}
8491
8492/* The to_supports_stopped_by_sw_breakpoint method of target
8493 remote. */
8494
8495static int
8496remote_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
8497{
8498 struct remote_state *rs = get_remote_state ();
8499
8500 return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
8501}
8502
8503/* The to_stopped_by_hw_breakpoint method of target remote. */
8504
8505static int
8506remote_stopped_by_hw_breakpoint (struct target_ops *ops)
8507{
8508 struct remote_state *rs = get_remote_state ();
8509
8510 return rs->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
8511}
8512
8513/* The to_supports_stopped_by_hw_breakpoint method of target
8514 remote. */
8515
8516static int
8517remote_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
8518{
8519 struct remote_state *rs = get_remote_state ();
8520
8521 return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
8522}
8523
b9362cc7 8524static int
6a109b6b 8525remote_stopped_by_watchpoint (struct target_ops *ops)
3c3bea1c 8526{
ee154bee
TT
8527 struct remote_state *rs = get_remote_state ();
8528
f7e6eed5 8529 return rs->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
3c3bea1c
GS
8530}
8531
4aa7a7f5
JJ
8532static int
8533remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 8534{
ee154bee 8535 struct remote_state *rs = get_remote_state ();
4aa7a7f5 8536 int rc = 0;
a744cf53 8537
6a109b6b 8538 if (remote_stopped_by_watchpoint (target))
4aa7a7f5 8539 {
ee154bee 8540 *addr_p = rs->remote_watch_data_address;
4aa7a7f5
JJ
8541 rc = 1;
8542 }
8543
8544 return rc;
3c3bea1c
GS
8545}
8546
8547
8548static int
23a26771 8549remote_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8550 struct bp_target_info *bp_tgt)
3c3bea1c 8551{
0d5ed153 8552 CORE_ADDR addr = bp_tgt->reqstd_address;
4fff2411 8553 struct remote_state *rs;
bba74b36 8554 char *p, *endbuf;
dd61ec5c 8555 char *message;
0d5ed153 8556 int bpsize;
802188a7 8557
c8189ed1 8558 /* The length field should be set to the size of a breakpoint
8181d85f 8559 instruction, even though we aren't inserting one ourselves. */
c8189ed1 8560
0d5ed153 8561 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
3c3bea1c 8562
4082afcc 8563 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 8564 return -1;
2bc416ba 8565
28439a30
PA
8566 /* Make sure the remote is pointing at the right process, if
8567 necessary. */
8568 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8569 set_general_process ();
8570
4fff2411
JZ
8571 rs = get_remote_state ();
8572 p = rs->buf;
bba74b36 8573 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 8574
96baa820
JM
8575 *(p++) = 'Z';
8576 *(p++) = '1';
8577 *(p++) = ',';
802188a7 8578
0d5ed153 8579 addr = remote_address_masked (addr);
96baa820 8580 p += hexnumstr (p, (ULONGEST) addr);
0d5ed153 8581 xsnprintf (p, endbuf - p, ",%x", bpsize);
96baa820 8582
efcc2da7 8583 if (remote_supports_cond_breakpoints (self))
bba74b36 8584 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8585
78eff0ec 8586 if (remote_can_run_breakpoint_commands (self))
d3ce09f5
SS
8587 remote_add_target_side_commands (gdbarch, bp_tgt, p);
8588
6d820c5c
DJ
8589 putpkt (rs->buf);
8590 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8591
6d820c5c 8592 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8593 {
8594 case PACKET_ERROR:
dd61ec5c
MW
8595 if (rs->buf[1] == '.')
8596 {
8597 message = strchr (rs->buf + 2, '.');
8598 if (message)
0316657e 8599 error (_("Remote failure reply: %s"), message + 1);
dd61ec5c
MW
8600 }
8601 return -1;
d471ea57
AC
8602 case PACKET_UNKNOWN:
8603 return -1;
8604 case PACKET_OK:
0d5ed153
MR
8605 bp_tgt->placed_address = addr;
8606 bp_tgt->placed_size = bpsize;
d471ea57
AC
8607 return 0;
8608 }
8e65ff28 8609 internal_error (__FILE__, __LINE__,
e2e0b3e5 8610 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
8611}
8612
d471ea57 8613
802188a7 8614static int
a64dc96c 8615remote_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
a6d9a66e 8616 struct bp_target_info *bp_tgt)
96baa820 8617{
8181d85f 8618 CORE_ADDR addr;
d01949b6 8619 struct remote_state *rs = get_remote_state ();
6d820c5c 8620 char *p = rs->buf;
bba74b36 8621 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 8622
4082afcc 8623 if (packet_support (PACKET_Z1) == PACKET_DISABLE)
5cffb350 8624 return -1;
802188a7 8625
28439a30
PA
8626 /* Make sure the remote is pointing at the right process, if
8627 necessary. */
8628 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8629 set_general_process ();
8630
96baa820
JM
8631 *(p++) = 'z';
8632 *(p++) = '1';
8633 *(p++) = ',';
802188a7 8634
8181d85f 8635 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 8636 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8637 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
96baa820 8638
6d820c5c
DJ
8639 putpkt (rs->buf);
8640 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 8641
6d820c5c 8642 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8643 {
8644 case PACKET_ERROR:
8645 case PACKET_UNKNOWN:
8646 return -1;
8647 case PACKET_OK:
8648 return 0;
8649 }
8e65ff28 8650 internal_error (__FILE__, __LINE__,
e2e0b3e5 8651 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 8652}
96baa820 8653
4a5e7a5b
PA
8654/* Verify memory using the "qCRC:" request. */
8655
8656static int
8657remote_verify_memory (struct target_ops *ops,
8658 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
8659{
8660 struct remote_state *rs = get_remote_state ();
8661 unsigned long host_crc, target_crc;
8662 char *tmp;
8663
936d2992
PA
8664 /* It doesn't make sense to use qCRC if the remote target is
8665 connected but not running. */
8666 if (target_has_execution && packet_support (PACKET_qCRC) != PACKET_DISABLE)
8667 {
8668 enum packet_result result;
28439a30 8669
936d2992
PA
8670 /* Make sure the remote is pointing at the right process. */
8671 set_general_process ();
4a5e7a5b 8672
936d2992
PA
8673 /* FIXME: assumes lma can fit into long. */
8674 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
8675 (long) lma, (long) size);
8676 putpkt (rs->buf);
4a5e7a5b 8677
936d2992
PA
8678 /* Be clever; compute the host_crc before waiting for target
8679 reply. */
8680 host_crc = xcrc32 (data, size, 0xffffffff);
8681
8682 getpkt (&rs->buf, &rs->buf_size, 0);
4a5e7a5b 8683
936d2992
PA
8684 result = packet_ok (rs->buf,
8685 &remote_protocol_packets[PACKET_qCRC]);
8686 if (result == PACKET_ERROR)
8687 return -1;
8688 else if (result == PACKET_OK)
8689 {
8690 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
8691 target_crc = target_crc * 16 + fromhex (*tmp);
4a5e7a5b 8692
936d2992
PA
8693 return (host_crc == target_crc);
8694 }
8695 }
4a5e7a5b 8696
936d2992 8697 return simple_verify_memory (ops, data, lma, size);
4a5e7a5b
PA
8698}
8699
c906108c
SS
8700/* compare-sections command
8701
8702 With no arguments, compares each loadable section in the exec bfd
8703 with the same memory range on the target, and reports mismatches.
4a5e7a5b 8704 Useful for verifying the image on the target against the exec file. */
e514a9d6 8705
c906108c 8706static void
fba45db2 8707compare_sections_command (char *args, int from_tty)
c906108c
SS
8708{
8709 asection *s;
c906108c 8710 struct cleanup *old_chain;
948f8e3d 8711 gdb_byte *sectdata;
ce359b09 8712 const char *sectname;
c906108c
SS
8713 bfd_size_type size;
8714 bfd_vma lma;
8715 int matched = 0;
8716 int mismatched = 0;
4a5e7a5b 8717 int res;
95cf3b38 8718 int read_only = 0;
c906108c
SS
8719
8720 if (!exec_bfd)
8a3fe4f8 8721 error (_("command cannot be used without an exec file"));
c906108c 8722
28439a30
PA
8723 /* Make sure the remote is pointing at the right process. */
8724 set_general_process ();
8725
95cf3b38
DT
8726 if (args != NULL && strcmp (args, "-r") == 0)
8727 {
8728 read_only = 1;
8729 args = NULL;
8730 }
8731
c5aa993b 8732 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
8733 {
8734 if (!(s->flags & SEC_LOAD))
0df8b418 8735 continue; /* Skip non-loadable section. */
c906108c 8736
95cf3b38
DT
8737 if (read_only && (s->flags & SEC_READONLY) == 0)
8738 continue; /* Skip writeable sections */
8739
2c500098 8740 size = bfd_get_section_size (s);
c906108c 8741 if (size == 0)
0df8b418 8742 continue; /* Skip zero-length section. */
c906108c 8743
ce359b09 8744 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 8745 if (args && strcmp (args, sectname) != 0)
0df8b418 8746 continue; /* Not the section selected by user. */
c906108c 8747
0df8b418 8748 matched = 1; /* Do this section. */
c906108c 8749 lma = s->lma;
c906108c 8750
c906108c 8751 sectdata = xmalloc (size);
b8c9b27d 8752 old_chain = make_cleanup (xfree, sectdata);
c906108c 8753 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 8754
4a5e7a5b
PA
8755 res = target_verify_memory (sectdata, lma, size);
8756
8757 if (res == -1)
5af949e3 8758 error (_("target memory fault, section %s, range %s -- %s"), sectname,
f5656ead
TT
8759 paddress (target_gdbarch (), lma),
8760 paddress (target_gdbarch (), lma + size));
c906108c 8761
5af949e3 8762 printf_filtered ("Section %s, range %s -- %s: ", sectname,
f5656ead
TT
8763 paddress (target_gdbarch (), lma),
8764 paddress (target_gdbarch (), lma + size));
4a5e7a5b 8765 if (res)
c906108c
SS
8766 printf_filtered ("matched.\n");
8767 else
c5aa993b
JM
8768 {
8769 printf_filtered ("MIS-MATCHED!\n");
8770 mismatched++;
8771 }
c906108c
SS
8772
8773 do_cleanups (old_chain);
8774 }
8775 if (mismatched > 0)
936d2992 8776 warning (_("One or more sections of the target image does not match\n\
8a3fe4f8 8777the loaded file\n"));
c906108c 8778 if (args && !matched)
a3f17187 8779 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
8780}
8781
0e7f50da
UW
8782/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
8783 into remote target. The number of bytes written to the remote
8784 target is returned, or -1 for error. */
8785
9b409511 8786static enum target_xfer_status
0e7f50da
UW
8787remote_write_qxfer (struct target_ops *ops, const char *object_name,
8788 const char *annex, const gdb_byte *writebuf,
9b409511 8789 ULONGEST offset, LONGEST len, ULONGEST *xfered_len,
0e7f50da
UW
8790 struct packet_config *packet)
8791{
8792 int i, buf_len;
8793 ULONGEST n;
0e7f50da
UW
8794 struct remote_state *rs = get_remote_state ();
8795 int max_size = get_memory_write_packet_size ();
8796
8797 if (packet->support == PACKET_DISABLE)
2ed4b548 8798 return TARGET_XFER_E_IO;
0e7f50da
UW
8799
8800 /* Insert header. */
8801 i = snprintf (rs->buf, max_size,
8802 "qXfer:%s:write:%s:%s:",
8803 object_name, annex ? annex : "",
8804 phex_nz (offset, sizeof offset));
8805 max_size -= (i + 1);
8806
8807 /* Escape as much data as fits into rs->buf. */
8808 buf_len = remote_escape_output
bc20a4af 8809 (writebuf, len, (gdb_byte *) rs->buf + i, &max_size, max_size);
0e7f50da
UW
8810
8811 if (putpkt_binary (rs->buf, i + buf_len) < 0
8812 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8813 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 8814 return TARGET_XFER_E_IO;
0e7f50da
UW
8815
8816 unpack_varlen_hex (rs->buf, &n);
9b409511
YQ
8817
8818 *xfered_len = n;
8819 return TARGET_XFER_OK;
0e7f50da
UW
8820}
8821
0876f84a
DJ
8822/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8823 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8824 number of bytes read is returned, or 0 for EOF, or -1 for error.
8825 The number of bytes read may be less than LEN without indicating an
8826 EOF. PACKET is checked and updated to indicate whether the remote
8827 target supports this object. */
8828
9b409511 8829static enum target_xfer_status
0876f84a
DJ
8830remote_read_qxfer (struct target_ops *ops, const char *object_name,
8831 const char *annex,
8832 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
9b409511 8833 ULONGEST *xfered_len,
0876f84a
DJ
8834 struct packet_config *packet)
8835{
0876f84a 8836 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
8837 LONGEST i, n, packet_len;
8838
8839 if (packet->support == PACKET_DISABLE)
2ed4b548 8840 return TARGET_XFER_E_IO;
0876f84a
DJ
8841
8842 /* Check whether we've cached an end-of-object packet that matches
8843 this request. */
8e88304f 8844 if (rs->finished_object)
0876f84a 8845 {
8e88304f
TT
8846 if (strcmp (object_name, rs->finished_object) == 0
8847 && strcmp (annex ? annex : "", rs->finished_annex) == 0
8848 && offset == rs->finished_offset)
9b409511
YQ
8849 return TARGET_XFER_EOF;
8850
0876f84a
DJ
8851
8852 /* Otherwise, we're now reading something different. Discard
8853 the cache. */
8e88304f
TT
8854 xfree (rs->finished_object);
8855 xfree (rs->finished_annex);
8856 rs->finished_object = NULL;
8857 rs->finished_annex = NULL;
0876f84a
DJ
8858 }
8859
8860 /* Request only enough to fit in a single packet. The actual data
8861 may not, since we don't know how much of it will need to be escaped;
8862 the target is free to respond with slightly less data. We subtract
8863 five to account for the response type and the protocol frame. */
8864 n = min (get_remote_packet_size () - 5, len);
8865 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8866 object_name, annex ? annex : "",
8867 phex_nz (offset, sizeof offset),
8868 phex_nz (n, sizeof n));
8869 i = putpkt (rs->buf);
8870 if (i < 0)
2ed4b548 8871 return TARGET_XFER_E_IO;
0876f84a
DJ
8872
8873 rs->buf[0] = '\0';
8874 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
8875 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
2ed4b548 8876 return TARGET_XFER_E_IO;
0876f84a
DJ
8877
8878 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
8879 error (_("Unknown remote qXfer reply: %s"), rs->buf);
8880
8881 /* 'm' means there is (or at least might be) more data after this
8882 batch. That does not make sense unless there's at least one byte
8883 of data in this reply. */
8884 if (rs->buf[0] == 'm' && packet_len == 1)
8885 error (_("Remote qXfer reply contained no data."));
8886
8887 /* Got some data. */
bc20a4af
PA
8888 i = remote_unescape_input ((gdb_byte *) rs->buf + 1,
8889 packet_len - 1, readbuf, n);
0876f84a
DJ
8890
8891 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
8892 or possibly empty. If we have the final block of a non-empty
8893 object, record this fact to bypass a subsequent partial read. */
8894 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a 8895 {
8e88304f
TT
8896 rs->finished_object = xstrdup (object_name);
8897 rs->finished_annex = xstrdup (annex ? annex : "");
8898 rs->finished_offset = offset + i;
0876f84a
DJ
8899 }
8900
9b409511
YQ
8901 if (i == 0)
8902 return TARGET_XFER_EOF;
8903 else
8904 {
8905 *xfered_len = i;
8906 return TARGET_XFER_OK;
8907 }
0876f84a
DJ
8908}
8909
9b409511 8910static enum target_xfer_status
4b8a223f 8911remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5 8912 const char *annex, gdb_byte *readbuf,
9b409511
YQ
8913 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
8914 ULONGEST *xfered_len)
c906108c 8915{
82f73884 8916 struct remote_state *rs;
c906108c 8917 int i;
6d820c5c 8918 char *p2;
1e3ff5ad 8919 char query_type;
c906108c 8920
e6e4e701 8921 set_remote_traceframe ();
82f73884
PA
8922 set_general_thread (inferior_ptid);
8923
8924 rs = get_remote_state ();
8925
b2182ed2 8926 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
8927 if (object == TARGET_OBJECT_MEMORY)
8928 {
2d717e4f
DJ
8929 /* If the remote target is connected but not running, we should
8930 pass this request down to a lower stratum (e.g. the executable
8931 file). */
8932 if (!target_has_execution)
9b409511 8933 return TARGET_XFER_EOF;
2d717e4f 8934
21e3b9b9 8935 if (writebuf != NULL)
9b409511 8936 return remote_write_bytes (offset, writebuf, len, xfered_len);
21e3b9b9 8937 else
8acf9577 8938 return remote_read_bytes (ops, offset, readbuf, len, xfered_len);
21e3b9b9
DJ
8939 }
8940
0df8b418 8941 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
8942 if (object == TARGET_OBJECT_SPU)
8943 {
8944 if (readbuf)
8945 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
9b409511
YQ
8946 xfered_len, &remote_protocol_packets
8947 [PACKET_qXfer_spu_read]);
0e7f50da
UW
8948 else
8949 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
9b409511
YQ
8950 xfered_len, &remote_protocol_packets
8951 [PACKET_qXfer_spu_write]);
0e7f50da
UW
8952 }
8953
4aa995e1
PA
8954 /* Handle extra signal info using qxfer packets. */
8955 if (object == TARGET_OBJECT_SIGNAL_INFO)
8956 {
8957 if (readbuf)
8958 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
9b409511 8959 xfered_len, &remote_protocol_packets
4aa995e1
PA
8960 [PACKET_qXfer_siginfo_read]);
8961 else
3e43a32a 8962 return remote_write_qxfer (ops, "siginfo", annex,
9b409511 8963 writebuf, offset, len, xfered_len,
4aa995e1
PA
8964 &remote_protocol_packets
8965 [PACKET_qXfer_siginfo_write]);
8966 }
8967
0fb4aa4b
PA
8968 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
8969 {
8970 if (readbuf)
3e43a32a 8971 return remote_read_qxfer (ops, "statictrace", annex,
9b409511 8972 readbuf, offset, len, xfered_len,
0fb4aa4b
PA
8973 &remote_protocol_packets
8974 [PACKET_qXfer_statictrace_read]);
8975 else
2ed4b548 8976 return TARGET_XFER_E_IO;
0fb4aa4b
PA
8977 }
8978
a76d924d
DJ
8979 /* Only handle flash writes. */
8980 if (writebuf != NULL)
8981 {
8982 LONGEST xfered;
8983
8984 switch (object)
8985 {
8986 case TARGET_OBJECT_FLASH:
9b409511
YQ
8987 return remote_flash_write (ops, offset, len, xfered_len,
8988 writebuf);
a76d924d
DJ
8989
8990 default:
2ed4b548 8991 return TARGET_XFER_E_IO;
a76d924d
DJ
8992 }
8993 }
4b8a223f 8994
1e3ff5ad
AC
8995 /* Map pre-existing objects onto letters. DO NOT do this for new
8996 objects!!! Instead specify new query packets. */
8997 switch (object)
c906108c 8998 {
1e3ff5ad
AC
8999 case TARGET_OBJECT_AVR:
9000 query_type = 'R';
9001 break;
802188a7
RM
9002
9003 case TARGET_OBJECT_AUXV:
0876f84a
DJ
9004 gdb_assert (annex == NULL);
9005 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
9b409511 9006 xfered_len,
0876f84a 9007 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 9008
23181151
DJ
9009 case TARGET_OBJECT_AVAILABLE_FEATURES:
9010 return remote_read_qxfer
9b409511 9011 (ops, "features", annex, readbuf, offset, len, xfered_len,
23181151
DJ
9012 &remote_protocol_packets[PACKET_qXfer_features]);
9013
cfa9d6d9
DJ
9014 case TARGET_OBJECT_LIBRARIES:
9015 return remote_read_qxfer
9b409511 9016 (ops, "libraries", annex, readbuf, offset, len, xfered_len,
cfa9d6d9
DJ
9017 &remote_protocol_packets[PACKET_qXfer_libraries]);
9018
2268b414
JK
9019 case TARGET_OBJECT_LIBRARIES_SVR4:
9020 return remote_read_qxfer
9b409511 9021 (ops, "libraries-svr4", annex, readbuf, offset, len, xfered_len,
2268b414
JK
9022 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
9023
fd79ecee
DJ
9024 case TARGET_OBJECT_MEMORY_MAP:
9025 gdb_assert (annex == NULL);
9026 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
9b409511 9027 xfered_len,
fd79ecee
DJ
9028 &remote_protocol_packets[PACKET_qXfer_memory_map]);
9029
07e059b5
VP
9030 case TARGET_OBJECT_OSDATA:
9031 /* Should only get here if we're connected. */
5d93a237 9032 gdb_assert (rs->remote_desc);
07e059b5 9033 return remote_read_qxfer
9b409511 9034 (ops, "osdata", annex, readbuf, offset, len, xfered_len,
07e059b5
VP
9035 &remote_protocol_packets[PACKET_qXfer_osdata]);
9036
dc146f7c
VP
9037 case TARGET_OBJECT_THREADS:
9038 gdb_assert (annex == NULL);
9039 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
9b409511 9040 xfered_len,
dc146f7c
VP
9041 &remote_protocol_packets[PACKET_qXfer_threads]);
9042
b3b9301e
PA
9043 case TARGET_OBJECT_TRACEFRAME_INFO:
9044 gdb_assert (annex == NULL);
9045 return remote_read_qxfer
9b409511 9046 (ops, "traceframe-info", annex, readbuf, offset, len, xfered_len,
b3b9301e 9047 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
9048
9049 case TARGET_OBJECT_FDPIC:
9050 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
9b409511 9051 xfered_len,
78d85199 9052 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
9053
9054 case TARGET_OBJECT_OPENVMS_UIB:
9055 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
9b409511 9056 xfered_len,
169081d0
TG
9057 &remote_protocol_packets[PACKET_qXfer_uib]);
9058
9accd112
MM
9059 case TARGET_OBJECT_BTRACE:
9060 return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
9b409511 9061 xfered_len,
9accd112
MM
9062 &remote_protocol_packets[PACKET_qXfer_btrace]);
9063
f4abbc16
MM
9064 case TARGET_OBJECT_BTRACE_CONF:
9065 return remote_read_qxfer (ops, "btrace-conf", annex, readbuf, offset,
9066 len, xfered_len,
9067 &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
9068
c78fa86a
GB
9069 case TARGET_OBJECT_EXEC_FILE:
9070 return remote_read_qxfer (ops, "exec-file", annex, readbuf, offset,
9071 len, xfered_len,
9072 &remote_protocol_packets[PACKET_qXfer_exec_file]);
9073
1e3ff5ad 9074 default:
2ed4b548 9075 return TARGET_XFER_E_IO;
c906108c
SS
9076 }
9077
0df8b418 9078 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 9079 large enough let the caller deal with it. */
ea9c271d 9080 if (len < get_remote_packet_size ())
2ed4b548 9081 return TARGET_XFER_E_IO;
ea9c271d 9082 len = get_remote_packet_size ();
1e3ff5ad 9083
23860348 9084 /* Except for querying the minimum buffer size, target must be open. */
5d93a237 9085 if (!rs->remote_desc)
8a3fe4f8 9086 error (_("remote query is only available after target open"));
c906108c 9087
1e3ff5ad 9088 gdb_assert (annex != NULL);
4b8a223f 9089 gdb_assert (readbuf != NULL);
c906108c 9090
6d820c5c 9091 p2 = rs->buf;
c906108c
SS
9092 *p2++ = 'q';
9093 *p2++ = query_type;
9094
23860348
MS
9095 /* We used one buffer char for the remote protocol q command and
9096 another for the query type. As the remote protocol encapsulation
9097 uses 4 chars plus one extra in case we are debugging
9098 (remote_debug), we have PBUFZIZ - 7 left to pack the query
9099 string. */
c906108c 9100 i = 0;
ea9c271d 9101 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 9102 {
1e3ff5ad
AC
9103 /* Bad caller may have sent forbidden characters. */
9104 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
9105 *p2++ = annex[i];
c906108c
SS
9106 i++;
9107 }
1e3ff5ad
AC
9108 *p2 = '\0';
9109 gdb_assert (annex[i] == '\0');
c906108c 9110
6d820c5c 9111 i = putpkt (rs->buf);
c5aa993b 9112 if (i < 0)
2ed4b548 9113 return TARGET_XFER_E_IO;
c906108c 9114
6d820c5c
DJ
9115 getpkt (&rs->buf, &rs->buf_size, 0);
9116 strcpy ((char *) readbuf, rs->buf);
c906108c 9117
9b409511
YQ
9118 *xfered_len = strlen ((char *) readbuf);
9119 return TARGET_XFER_OK;
c906108c
SS
9120}
9121
08388c79
DE
9122static int
9123remote_search_memory (struct target_ops* ops,
9124 CORE_ADDR start_addr, ULONGEST search_space_len,
9125 const gdb_byte *pattern, ULONGEST pattern_len,
9126 CORE_ADDR *found_addrp)
9127{
f5656ead 9128 int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
08388c79
DE
9129 struct remote_state *rs = get_remote_state ();
9130 int max_size = get_memory_write_packet_size ();
9131 struct packet_config *packet =
9132 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
9133 /* Number of packet bytes used to encode the pattern;
9134 this could be more than PATTERN_LEN due to escape characters. */
08388c79 9135 int escaped_pattern_len;
0df8b418 9136 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
9137 int used_pattern_len;
9138 int i;
9139 int found;
9140 ULONGEST found_addr;
9141
9142 /* Don't go to the target if we don't have to.
9143 This is done before checking packet->support to avoid the possibility that
9144 a success for this edge case means the facility works in general. */
9145 if (pattern_len > search_space_len)
9146 return 0;
9147 if (pattern_len == 0)
9148 {
9149 *found_addrp = start_addr;
9150 return 1;
9151 }
9152
9153 /* If we already know the packet isn't supported, fall back to the simple
9154 way of searching memory. */
9155
4082afcc 9156 if (packet_config_support (packet) == PACKET_DISABLE)
08388c79
DE
9157 {
9158 /* Target doesn't provided special support, fall back and use the
9159 standard support (copy memory and do the search here). */
9160 return simple_search_memory (ops, start_addr, search_space_len,
9161 pattern, pattern_len, found_addrp);
9162 }
9163
28439a30
PA
9164 /* Make sure the remote is pointing at the right process. */
9165 set_general_process ();
9166
08388c79
DE
9167 /* Insert header. */
9168 i = snprintf (rs->buf, max_size,
9169 "qSearch:memory:%s;%s;",
5af949e3 9170 phex_nz (start_addr, addr_size),
08388c79
DE
9171 phex_nz (search_space_len, sizeof (search_space_len)));
9172 max_size -= (i + 1);
9173
9174 /* Escape as much data as fits into rs->buf. */
9175 escaped_pattern_len =
bc20a4af 9176 remote_escape_output (pattern, pattern_len, (gdb_byte *) rs->buf + i,
08388c79
DE
9177 &used_pattern_len, max_size);
9178
9179 /* Bail if the pattern is too large. */
9180 if (used_pattern_len != pattern_len)
9b20d036 9181 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
9182
9183 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
9184 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
9185 || packet_ok (rs->buf, packet) != PACKET_OK)
9186 {
9187 /* The request may not have worked because the command is not
9188 supported. If so, fall back to the simple way. */
9189 if (packet->support == PACKET_DISABLE)
9190 {
9191 return simple_search_memory (ops, start_addr, search_space_len,
9192 pattern, pattern_len, found_addrp);
9193 }
9194 return -1;
9195 }
9196
9197 if (rs->buf[0] == '0')
9198 found = 0;
9199 else if (rs->buf[0] == '1')
9200 {
9201 found = 1;
9202 if (rs->buf[1] != ',')
10e0fa18 9203 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9204 unpack_varlen_hex (rs->buf + 2, &found_addr);
9205 *found_addrp = found_addr;
9206 }
9207 else
10e0fa18 9208 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
9209
9210 return found;
9211}
9212
96baa820 9213static void
a30bf1f1 9214remote_rcmd (struct target_ops *self, const char *command,
d9fcf2fb 9215 struct ui_file *outbuf)
96baa820 9216{
d01949b6 9217 struct remote_state *rs = get_remote_state ();
2e9f7625 9218 char *p = rs->buf;
96baa820 9219
5d93a237 9220 if (!rs->remote_desc)
8a3fe4f8 9221 error (_("remote rcmd is only available after target open"));
96baa820 9222
23860348 9223 /* Send a NULL command across as an empty command. */
7be570e7
JM
9224 if (command == NULL)
9225 command = "";
9226
23860348 9227 /* The query prefix. */
2e9f7625
DJ
9228 strcpy (rs->buf, "qRcmd,");
9229 p = strchr (rs->buf, '\0');
96baa820 9230
3e43a32a
MS
9231 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
9232 > get_remote_packet_size ())
8a3fe4f8 9233 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 9234
23860348 9235 /* Encode the actual command. */
a30bf1f1 9236 bin2hex ((const gdb_byte *) command, p, strlen (command));
96baa820 9237
6d820c5c 9238 if (putpkt (rs->buf) < 0)
8a3fe4f8 9239 error (_("Communication problem with target."));
96baa820
JM
9240
9241 /* get/display the response */
9242 while (1)
9243 {
2e9f7625
DJ
9244 char *buf;
9245
00bf0b85 9246 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 9247 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 9248 rs->buf[0] = '\0';
5b37825d
PW
9249 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
9250 {
9251 /* Timeout. Continue to (try to) read responses.
9252 This is better than stopping with an error, assuming the stub
9253 is still executing the (long) monitor command.
9254 If needed, the user can interrupt gdb using C-c, obtaining
9255 an effect similar to stop on timeout. */
9256 continue;
9257 }
2e9f7625 9258 buf = rs->buf;
96baa820 9259 if (buf[0] == '\0')
8a3fe4f8 9260 error (_("Target does not support this command."));
96baa820
JM
9261 if (buf[0] == 'O' && buf[1] != 'K')
9262 {
23860348 9263 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
9264 continue;
9265 }
9266 if (strcmp (buf, "OK") == 0)
9267 break;
7be570e7
JM
9268 if (strlen (buf) == 3 && buf[0] == 'E'
9269 && isdigit (buf[1]) && isdigit (buf[2]))
9270 {
8a3fe4f8 9271 error (_("Protocol error with Rcmd"));
7be570e7 9272 }
96baa820
JM
9273 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
9274 {
9275 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 9276
96baa820
JM
9277 fputc_unfiltered (c, outbuf);
9278 }
9279 break;
9280 }
9281}
9282
fd79ecee
DJ
9283static VEC(mem_region_s) *
9284remote_memory_map (struct target_ops *ops)
9285{
9286 VEC(mem_region_s) *result = NULL;
9287 char *text = target_read_stralloc (&current_target,
9288 TARGET_OBJECT_MEMORY_MAP, NULL);
9289
9290 if (text)
9291 {
9292 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 9293
fd79ecee
DJ
9294 result = parse_memory_map (text);
9295 do_cleanups (back_to);
9296 }
9297
9298 return result;
9299}
9300
c906108c 9301static void
fba45db2 9302packet_command (char *args, int from_tty)
c906108c 9303{
d01949b6 9304 struct remote_state *rs = get_remote_state ();
c906108c 9305
5d93a237 9306 if (!rs->remote_desc)
8a3fe4f8 9307 error (_("command can only be used with remote target"));
c906108c 9308
c5aa993b 9309 if (!args)
8a3fe4f8 9310 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
9311
9312 puts_filtered ("sending: ");
9313 print_packet (args);
9314 puts_filtered ("\n");
9315 putpkt (args);
9316
6d820c5c 9317 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 9318 puts_filtered ("received: ");
6d820c5c 9319 print_packet (rs->buf);
c906108c
SS
9320 puts_filtered ("\n");
9321}
9322
9323#if 0
23860348 9324/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 9325
a14ed312 9326static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 9327
a14ed312 9328static void threadset_test_cmd (char *cmd, int tty);
c906108c 9329
a14ed312 9330static void threadalive_test (char *cmd, int tty);
c906108c 9331
a14ed312 9332static void threadlist_test_cmd (char *cmd, int tty);
c906108c 9333
23860348 9334int get_and_display_threadinfo (threadref *ref);
c906108c 9335
a14ed312 9336static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 9337
23860348 9338static int thread_display_step (threadref *ref, void *context);
c906108c 9339
a14ed312 9340static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 9341
a14ed312 9342static void init_remote_threadtests (void);
c906108c 9343
23860348 9344#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
9345
9346static void
fba45db2 9347threadset_test_cmd (char *cmd, int tty)
c906108c
SS
9348{
9349 int sample_thread = SAMPLE_THREAD;
9350
a3f17187 9351 printf_filtered (_("Remote threadset test\n"));
79d7f229 9352 set_general_thread (sample_thread);
c906108c
SS
9353}
9354
9355
9356static void
fba45db2 9357threadalive_test (char *cmd, int tty)
c906108c
SS
9358{
9359 int sample_thread = SAMPLE_THREAD;
79d7f229 9360 int pid = ptid_get_pid (inferior_ptid);
ba348170 9361 ptid_t ptid = ptid_build (pid, sample_thread, 0);
c906108c 9362
79d7f229 9363 if (remote_thread_alive (ptid))
c906108c
SS
9364 printf_filtered ("PASS: Thread alive test\n");
9365 else
9366 printf_filtered ("FAIL: Thread alive test\n");
9367}
9368
23860348 9369void output_threadid (char *title, threadref *ref);
c906108c
SS
9370
9371void
fba45db2 9372output_threadid (char *title, threadref *ref)
c906108c
SS
9373{
9374 char hexid[20];
9375
23860348 9376 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
9377 hexid[16] = 0;
9378 printf_filtered ("%s %s\n", title, (&hexid[0]));
9379}
9380
9381static void
fba45db2 9382threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
9383{
9384 int startflag = 1;
9385 threadref nextthread;
9386 int done, result_count;
9387 threadref threadlist[3];
9388
9389 printf_filtered ("Remote Threadlist test\n");
9390 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
9391 &result_count, &threadlist[0]))
9392 printf_filtered ("FAIL: threadlist test\n");
9393 else
9394 {
9395 threadref *scan = threadlist;
9396 threadref *limit = scan + result_count;
9397
9398 while (scan < limit)
9399 output_threadid (" thread ", scan++);
9400 }
9401}
9402
9403void
fba45db2 9404display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
9405{
9406 output_threadid ("Threadid: ", &info->threadid);
9407 printf_filtered ("Name: %s\n ", info->shortname);
9408 printf_filtered ("State: %s\n", info->display);
9409 printf_filtered ("other: %s\n\n", info->more_display);
9410}
9411
9412int
fba45db2 9413get_and_display_threadinfo (threadref *ref)
c906108c
SS
9414{
9415 int result;
9416 int set;
9417 struct gdb_ext_thread_info threadinfo;
9418
9419 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
9420 | TAG_MOREDISPLAY | TAG_DISPLAY;
9421 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
9422 display_thread_info (&threadinfo);
9423 return result;
9424}
9425
9426static void
fba45db2 9427threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
9428{
9429 int athread = SAMPLE_THREAD;
9430 threadref thread;
9431 int set;
9432
9433 int_to_threadref (&thread, athread);
9434 printf_filtered ("Remote Threadinfo test\n");
9435 if (!get_and_display_threadinfo (&thread))
9436 printf_filtered ("FAIL cannot get thread info\n");
9437}
9438
9439static int
fba45db2 9440thread_display_step (threadref *ref, void *context)
c906108c
SS
9441{
9442 /* output_threadid(" threadstep ",ref); *//* simple test */
9443 return get_and_display_threadinfo (ref);
9444}
9445
9446static void
fba45db2 9447threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
9448{
9449 printf_filtered ("Remote Threadlist update test\n");
9450 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
9451}
9452
9453static void
9454init_remote_threadtests (void)
9455{
3e43a32a
MS
9456 add_com ("tlist", class_obscure, threadlist_test_cmd,
9457 _("Fetch and print the remote list of "
9458 "thread identifiers, one pkt only"));
c906108c 9459 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 9460 _("Fetch and display info about one thread"));
c906108c 9461 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 9462 _("Test setting to a different thread"));
c906108c 9463 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 9464 _("Iterate through updating all remote thread info"));
c906108c 9465 add_com ("talive", class_obscure, threadalive_test,
1bedd215 9466 _(" Remote thread alive test "));
c906108c
SS
9467}
9468
9469#endif /* 0 */
9470
f3fb8c85
MS
9471/* Convert a thread ID to a string. Returns the string in a static
9472 buffer. */
9473
9474static char *
117de6a9 9475remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 9476{
79d7f229 9477 static char buf[64];
82f73884 9478 struct remote_state *rs = get_remote_state ();
f3fb8c85 9479
7cee1e54
PA
9480 if (ptid_equal (ptid, null_ptid))
9481 return normal_pid_to_str (ptid);
9482 else if (ptid_is_pid (ptid))
ecd0ada5
PA
9483 {
9484 /* Printing an inferior target id. */
9485
9486 /* When multi-process extensions are off, there's no way in the
9487 remote protocol to know the remote process id, if there's any
9488 at all. There's one exception --- when we're connected with
9489 target extended-remote, and we manually attached to a process
9490 with "attach PID". We don't record anywhere a flag that
9491 allows us to distinguish that case from the case of
9492 connecting with extended-remote and the stub already being
9493 attached to a process, and reporting yes to qAttached, hence
9494 no smart special casing here. */
9495 if (!remote_multi_process_p (rs))
9496 {
9497 xsnprintf (buf, sizeof buf, "Remote target");
9498 return buf;
9499 }
9500
9501 return normal_pid_to_str (ptid);
82f73884 9502 }
ecd0ada5 9503 else
79d7f229 9504 {
ecd0ada5
PA
9505 if (ptid_equal (magic_null_ptid, ptid))
9506 xsnprintf (buf, sizeof buf, "Thread <main>");
901f9912 9507 else if (rs->extended && remote_multi_process_p (rs))
ecd0ada5 9508 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
ba348170 9509 ptid_get_pid (ptid), ptid_get_lwp (ptid));
ecd0ada5
PA
9510 else
9511 xsnprintf (buf, sizeof buf, "Thread %ld",
ba348170 9512 ptid_get_lwp (ptid));
79d7f229
PA
9513 return buf;
9514 }
f3fb8c85
MS
9515}
9516
38691318
KB
9517/* Get the address of the thread local variable in OBJFILE which is
9518 stored at OFFSET within the thread local storage for thread PTID. */
9519
9520static CORE_ADDR
117de6a9
PA
9521remote_get_thread_local_address (struct target_ops *ops,
9522 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 9523{
4082afcc 9524 if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
38691318
KB
9525 {
9526 struct remote_state *rs = get_remote_state ();
6d820c5c 9527 char *p = rs->buf;
82f73884 9528 char *endp = rs->buf + get_remote_packet_size ();
571dd617 9529 enum packet_result result;
38691318
KB
9530
9531 strcpy (p, "qGetTLSAddr:");
9532 p += strlen (p);
82f73884 9533 p = write_ptid (p, endp, ptid);
38691318
KB
9534 *p++ = ',';
9535 p += hexnumstr (p, offset);
9536 *p++ = ',';
9537 p += hexnumstr (p, lm);
9538 *p++ = '\0';
9539
6d820c5c
DJ
9540 putpkt (rs->buf);
9541 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
9542 result = packet_ok (rs->buf,
9543 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 9544 if (result == PACKET_OK)
38691318
KB
9545 {
9546 ULONGEST result;
9547
6d820c5c 9548 unpack_varlen_hex (rs->buf, &result);
38691318
KB
9549 return result;
9550 }
571dd617 9551 else if (result == PACKET_UNKNOWN)
109c3e39
AC
9552 throw_error (TLS_GENERIC_ERROR,
9553 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 9554 else
109c3e39
AC
9555 throw_error (TLS_GENERIC_ERROR,
9556 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
9557 }
9558 else
109c3e39
AC
9559 throw_error (TLS_GENERIC_ERROR,
9560 _("TLS not supported or disabled on this target"));
38691318
KB
9561 /* Not reached. */
9562 return 0;
9563}
9564
711e434b
PM
9565/* Provide thread local base, i.e. Thread Information Block address.
9566 Returns 1 if ptid is found and thread_local_base is non zero. */
9567
70221824 9568static int
bd7ae0f5 9569remote_get_tib_address (struct target_ops *self, ptid_t ptid, CORE_ADDR *addr)
711e434b 9570{
4082afcc 9571 if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
711e434b
PM
9572 {
9573 struct remote_state *rs = get_remote_state ();
9574 char *p = rs->buf;
9575 char *endp = rs->buf + get_remote_packet_size ();
9576 enum packet_result result;
9577
9578 strcpy (p, "qGetTIBAddr:");
9579 p += strlen (p);
9580 p = write_ptid (p, endp, ptid);
9581 *p++ = '\0';
9582
9583 putpkt (rs->buf);
9584 getpkt (&rs->buf, &rs->buf_size, 0);
9585 result = packet_ok (rs->buf,
9586 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9587 if (result == PACKET_OK)
9588 {
9589 ULONGEST result;
9590
9591 unpack_varlen_hex (rs->buf, &result);
9592 if (addr)
9593 *addr = (CORE_ADDR) result;
9594 return 1;
9595 }
9596 else if (result == PACKET_UNKNOWN)
9597 error (_("Remote target doesn't support qGetTIBAddr packet"));
9598 else
9599 error (_("Remote target failed to process qGetTIBAddr request"));
9600 }
9601 else
9602 error (_("qGetTIBAddr not supported or disabled on this target"));
9603 /* Not reached. */
9604 return 0;
9605}
9606
29709017
DJ
9607/* Support for inferring a target description based on the current
9608 architecture and the size of a 'g' packet. While the 'g' packet
9609 can have any size (since optional registers can be left off the
9610 end), some sizes are easily recognizable given knowledge of the
9611 approximate architecture. */
9612
9613struct remote_g_packet_guess
9614{
9615 int bytes;
9616 const struct target_desc *tdesc;
9617};
9618typedef struct remote_g_packet_guess remote_g_packet_guess_s;
9619DEF_VEC_O(remote_g_packet_guess_s);
9620
9621struct remote_g_packet_data
9622{
9623 VEC(remote_g_packet_guess_s) *guesses;
9624};
9625
9626static struct gdbarch_data *remote_g_packet_data_handle;
9627
9628static void *
9629remote_g_packet_data_init (struct obstack *obstack)
9630{
9631 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
9632}
9633
9634void
9635register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
9636 const struct target_desc *tdesc)
9637{
9638 struct remote_g_packet_data *data
9639 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
9640 struct remote_g_packet_guess new_guess, *guess;
9641 int ix;
9642
9643 gdb_assert (tdesc != NULL);
9644
9645 for (ix = 0;
9646 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9647 ix++)
9648 if (guess->bytes == bytes)
9649 internal_error (__FILE__, __LINE__,
9b20d036 9650 _("Duplicate g packet description added for size %d"),
29709017
DJ
9651 bytes);
9652
9653 new_guess.bytes = bytes;
9654 new_guess.tdesc = tdesc;
9655 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
9656}
9657
d962ef82
DJ
9658/* Return 1 if remote_read_description would do anything on this target
9659 and architecture, 0 otherwise. */
9660
9661static int
9662remote_read_description_p (struct target_ops *target)
9663{
9664 struct remote_g_packet_data *data
f5656ead 9665 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
d962ef82
DJ
9666
9667 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9668 return 1;
9669
9670 return 0;
9671}
9672
29709017
DJ
9673static const struct target_desc *
9674remote_read_description (struct target_ops *target)
9675{
9676 struct remote_g_packet_data *data
f5656ead 9677 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
29709017 9678
d962ef82
DJ
9679 /* Do not try this during initial connection, when we do not know
9680 whether there is a running but stopped thread. */
9681 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
2117c711 9682 return target->beneath->to_read_description (target->beneath);
d962ef82 9683
29709017
DJ
9684 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9685 {
9686 struct remote_g_packet_guess *guess;
9687 int ix;
9688 int bytes = send_g_packet ();
9689
9690 for (ix = 0;
9691 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9692 ix++)
9693 if (guess->bytes == bytes)
9694 return guess->tdesc;
9695
9696 /* We discard the g packet. A minor optimization would be to
9697 hold on to it, and fill the register cache once we have selected
9698 an architecture, but it's too tricky to do safely. */
9699 }
9700
2117c711 9701 return target->beneath->to_read_description (target->beneath);
29709017
DJ
9702}
9703
a6b151f1
DJ
9704/* Remote file transfer support. This is host-initiated I/O, not
9705 target-initiated; for target-initiated, see remote-fileio.c. */
9706
9707/* If *LEFT is at least the length of STRING, copy STRING to
9708 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9709 decrease *LEFT. Otherwise raise an error. */
9710
9711static void
9712remote_buffer_add_string (char **buffer, int *left, char *string)
9713{
9714 int len = strlen (string);
9715
9716 if (len > *left)
9717 error (_("Packet too long for target."));
9718
9719 memcpy (*buffer, string, len);
9720 *buffer += len;
9721 *left -= len;
9722
9723 /* NUL-terminate the buffer as a convenience, if there is
9724 room. */
9725 if (*left)
9726 **buffer = '\0';
9727}
9728
9729/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
9730 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9731 decrease *LEFT. Otherwise raise an error. */
9732
9733static void
9734remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
9735 int len)
9736{
9737 if (2 * len > *left)
9738 error (_("Packet too long for target."));
9739
9740 bin2hex (bytes, *buffer, len);
9741 *buffer += 2 * len;
9742 *left -= 2 * len;
9743
9744 /* NUL-terminate the buffer as a convenience, if there is
9745 room. */
9746 if (*left)
9747 **buffer = '\0';
9748}
9749
9750/* If *LEFT is large enough, convert VALUE to hex and add it to
9751 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9752 decrease *LEFT. Otherwise raise an error. */
9753
9754static void
9755remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
9756{
9757 int len = hexnumlen (value);
9758
9759 if (len > *left)
9760 error (_("Packet too long for target."));
9761
9762 hexnumstr (*buffer, value);
9763 *buffer += len;
9764 *left -= len;
9765
9766 /* NUL-terminate the buffer as a convenience, if there is
9767 room. */
9768 if (*left)
9769 **buffer = '\0';
9770}
9771
9772/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
9773 value, *REMOTE_ERRNO to the remote error number or zero if none
9774 was included, and *ATTACHMENT to point to the start of the annex
9775 if any. The length of the packet isn't needed here; there may
9776 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
9777
9778 Return 0 if the packet could be parsed, -1 if it could not. If
9779 -1 is returned, the other variables may not be initialized. */
9780
9781static int
9782remote_hostio_parse_result (char *buffer, int *retcode,
9783 int *remote_errno, char **attachment)
9784{
9785 char *p, *p2;
9786
9787 *remote_errno = 0;
9788 *attachment = NULL;
9789
9790 if (buffer[0] != 'F')
9791 return -1;
9792
9793 errno = 0;
9794 *retcode = strtol (&buffer[1], &p, 16);
9795 if (errno != 0 || p == &buffer[1])
9796 return -1;
9797
9798 /* Check for ",errno". */
9799 if (*p == ',')
9800 {
9801 errno = 0;
9802 *remote_errno = strtol (p + 1, &p2, 16);
9803 if (errno != 0 || p + 1 == p2)
9804 return -1;
9805 p = p2;
9806 }
9807
9808 /* Check for ";attachment". If there is no attachment, the
9809 packet should end here. */
9810 if (*p == ';')
9811 {
9812 *attachment = p + 1;
9813 return 0;
9814 }
9815 else if (*p == '\0')
9816 return 0;
9817 else
9818 return -1;
9819}
9820
9821/* Send a prepared I/O packet to the target and read its response.
9822 The prepared packet is in the global RS->BUF before this function
9823 is called, and the answer is there when we return.
9824
9825 COMMAND_BYTES is the length of the request to send, which may include
9826 binary data. WHICH_PACKET is the packet configuration to check
9827 before attempting a packet. If an error occurs, *REMOTE_ERRNO
9828 is set to the error number and -1 is returned. Otherwise the value
9829 returned by the function is returned.
9830
9831 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
9832 attachment is expected; an error will be reported if there's a
9833 mismatch. If one is found, *ATTACHMENT will be set to point into
9834 the packet buffer and *ATTACHMENT_LEN will be set to the
9835 attachment's length. */
9836
9837static int
9838remote_hostio_send_command (int command_bytes, int which_packet,
9839 int *remote_errno, char **attachment,
9840 int *attachment_len)
9841{
9842 struct remote_state *rs = get_remote_state ();
9843 int ret, bytes_read;
9844 char *attachment_tmp;
9845
5d93a237 9846 if (!rs->remote_desc
4082afcc 9847 || packet_support (which_packet) == PACKET_DISABLE)
a6b151f1
DJ
9848 {
9849 *remote_errno = FILEIO_ENOSYS;
9850 return -1;
9851 }
9852
9853 putpkt_binary (rs->buf, command_bytes);
9854 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9855
9856 /* If it timed out, something is wrong. Don't try to parse the
9857 buffer. */
9858 if (bytes_read < 0)
9859 {
9860 *remote_errno = FILEIO_EINVAL;
9861 return -1;
9862 }
9863
9864 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
9865 {
9866 case PACKET_ERROR:
9867 *remote_errno = FILEIO_EINVAL;
9868 return -1;
9869 case PACKET_UNKNOWN:
9870 *remote_errno = FILEIO_ENOSYS;
9871 return -1;
9872 case PACKET_OK:
9873 break;
9874 }
9875
9876 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
9877 &attachment_tmp))
9878 {
9879 *remote_errno = FILEIO_EINVAL;
9880 return -1;
9881 }
9882
9883 /* Make sure we saw an attachment if and only if we expected one. */
9884 if ((attachment_tmp == NULL && attachment != NULL)
9885 || (attachment_tmp != NULL && attachment == NULL))
9886 {
9887 *remote_errno = FILEIO_EINVAL;
9888 return -1;
9889 }
9890
9891 /* If an attachment was found, it must point into the packet buffer;
9892 work out how many bytes there were. */
9893 if (attachment_tmp != NULL)
9894 {
9895 *attachment = attachment_tmp;
9896 *attachment_len = bytes_read - (*attachment - rs->buf);
9897 }
9898
9899 return ret;
9900}
9901
4bd7dc42
GB
9902/* Return nonzero if the filesystem accessed by the target_fileio_*
9903 methods is the local filesystem, zero otherwise. */
9904
9905static int
9906remote_filesystem_is_local (struct target_ops *self)
9907{
9908 return 0;
9909}
9910
a6b151f1
DJ
9911/* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9912 remote file descriptor, or -1 if an error occurs (and set
9913 *REMOTE_ERRNO). */
9914
9915static int
cd897586
TT
9916remote_hostio_open (struct target_ops *self,
9917 const char *filename, int flags, int mode,
a6b151f1
DJ
9918 int *remote_errno)
9919{
9920 struct remote_state *rs = get_remote_state ();
9921 char *p = rs->buf;
9922 int left = get_remote_packet_size () - 1;
9923
9924 remote_buffer_add_string (&p, &left, "vFile:open:");
9925
9926 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9927 strlen (filename));
9928 remote_buffer_add_string (&p, &left, ",");
9929
9930 remote_buffer_add_int (&p, &left, flags);
9931 remote_buffer_add_string (&p, &left, ",");
9932
9933 remote_buffer_add_int (&p, &left, mode);
9934
9935 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
9936 remote_errno, NULL, NULL);
9937}
9938
9939/* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9940 Return the number of bytes written, or -1 if an error occurs (and
9941 set *REMOTE_ERRNO). */
9942
9943static int
0d866f62
TT
9944remote_hostio_pwrite (struct target_ops *self,
9945 int fd, const gdb_byte *write_buf, int len,
a6b151f1
DJ
9946 ULONGEST offset, int *remote_errno)
9947{
9948 struct remote_state *rs = get_remote_state ();
9949 char *p = rs->buf;
9950 int left = get_remote_packet_size ();
9951 int out_len;
9952
9953 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
9954
9955 remote_buffer_add_int (&p, &left, fd);
9956 remote_buffer_add_string (&p, &left, ",");
9957
9958 remote_buffer_add_int (&p, &left, offset);
9959 remote_buffer_add_string (&p, &left, ",");
9960
bc20a4af 9961 p += remote_escape_output (write_buf, len, (gdb_byte *) p, &out_len,
a6b151f1
DJ
9962 get_remote_packet_size () - (p - rs->buf));
9963
9964 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
9965 remote_errno, NULL, NULL);
9966}
9967
9968/* Read up to LEN bytes FD on the remote target into READ_BUF
9969 Return the number of bytes read, or -1 if an error occurs (and
9970 set *REMOTE_ERRNO). */
9971
9972static int
a3be983c
TT
9973remote_hostio_pread (struct target_ops *self,
9974 int fd, gdb_byte *read_buf, int len,
a6b151f1
DJ
9975 ULONGEST offset, int *remote_errno)
9976{
9977 struct remote_state *rs = get_remote_state ();
9978 char *p = rs->buf;
9979 char *attachment;
9980 int left = get_remote_packet_size ();
9981 int ret, attachment_len;
9982 int read_len;
9983
9984 remote_buffer_add_string (&p, &left, "vFile:pread:");
9985
9986 remote_buffer_add_int (&p, &left, fd);
9987 remote_buffer_add_string (&p, &left, ",");
9988
9989 remote_buffer_add_int (&p, &left, len);
9990 remote_buffer_add_string (&p, &left, ",");
9991
9992 remote_buffer_add_int (&p, &left, offset);
9993
9994 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
9995 remote_errno, &attachment,
9996 &attachment_len);
9997
9998 if (ret < 0)
9999 return ret;
10000
bc20a4af 10001 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
a6b151f1
DJ
10002 read_buf, len);
10003 if (read_len != ret)
10004 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
10005
10006 return ret;
10007}
10008
10009/* Close FD on the remote target. Return 0, or -1 if an error occurs
10010 (and set *REMOTE_ERRNO). */
10011
10012static int
df39ea25 10013remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
a6b151f1
DJ
10014{
10015 struct remote_state *rs = get_remote_state ();
10016 char *p = rs->buf;
10017 int left = get_remote_packet_size () - 1;
10018
10019 remote_buffer_add_string (&p, &left, "vFile:close:");
10020
10021 remote_buffer_add_int (&p, &left, fd);
10022
10023 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
10024 remote_errno, NULL, NULL);
10025}
10026
10027/* Unlink FILENAME on the remote target. Return 0, or -1 if an error
10028 occurs (and set *REMOTE_ERRNO). */
10029
10030static int
dbbca37d
TT
10031remote_hostio_unlink (struct target_ops *self,
10032 const char *filename, int *remote_errno)
a6b151f1
DJ
10033{
10034 struct remote_state *rs = get_remote_state ();
10035 char *p = rs->buf;
10036 int left = get_remote_packet_size () - 1;
10037
10038 remote_buffer_add_string (&p, &left, "vFile:unlink:");
10039
10040 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10041 strlen (filename));
10042
10043 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
10044 remote_errno, NULL, NULL);
10045}
10046
b9e7b9c3
UW
10047/* Read value of symbolic link FILENAME on the remote target. Return
10048 a null-terminated string allocated via xmalloc, or NULL if an error
10049 occurs (and set *REMOTE_ERRNO). */
10050
10051static char *
fab5aa7c
TT
10052remote_hostio_readlink (struct target_ops *self,
10053 const char *filename, int *remote_errno)
b9e7b9c3
UW
10054{
10055 struct remote_state *rs = get_remote_state ();
10056 char *p = rs->buf;
10057 char *attachment;
10058 int left = get_remote_packet_size ();
10059 int len, attachment_len;
10060 int read_len;
10061 char *ret;
10062
10063 remote_buffer_add_string (&p, &left, "vFile:readlink:");
10064
10065 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
10066 strlen (filename));
10067
10068 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
10069 remote_errno, &attachment,
10070 &attachment_len);
10071
10072 if (len < 0)
10073 return NULL;
10074
10075 ret = xmalloc (len + 1);
10076
bc20a4af
PA
10077 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10078 (gdb_byte *) ret, len);
b9e7b9c3
UW
10079 if (read_len != len)
10080 error (_("Readlink returned %d, but %d bytes."), len, read_len);
10081
10082 ret[len] = '\0';
10083 return ret;
10084}
10085
0a93529c
GB
10086/* Read information about the open file FD on the remote target
10087 into ST. Return 0 on success, or -1 if an error occurs (and
10088 set *REMOTE_ERRNO). */
10089
10090static int
10091remote_hostio_fstat (struct target_ops *self,
10092 int fd, struct stat *st,
10093 int *remote_errno)
10094{
10095 struct remote_state *rs = get_remote_state ();
10096 char *p = rs->buf;
10097 int left = get_remote_packet_size ();
10098 int attachment_len, ret;
10099 char *attachment;
10100 struct fio_stat fst;
10101 int read_len;
10102
464b0089
GB
10103 remote_buffer_add_string (&p, &left, "vFile:fstat:");
10104
10105 remote_buffer_add_int (&p, &left, fd);
10106
10107 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
10108 remote_errno, &attachment,
10109 &attachment_len);
10110 if (ret < 0)
0a93529c 10111 {
464b0089
GB
10112 if (*remote_errno != FILEIO_ENOSYS)
10113 return ret;
10114
0a93529c
GB
10115 /* Strictly we should return -1, ENOSYS here, but when
10116 "set sysroot remote:" was implemented in August 2008
10117 BFD's need for a stat function was sidestepped with
10118 this hack. This was not remedied until March 2015
10119 so we retain the previous behavior to avoid breaking
10120 compatibility.
10121
10122 Note that the memset is a March 2015 addition; older
10123 GDBs set st_size *and nothing else* so the structure
10124 would have garbage in all other fields. This might
10125 break something but retaining the previous behavior
10126 here would be just too wrong. */
10127
10128 memset (st, 0, sizeof (struct stat));
10129 st->st_size = INT_MAX;
10130 return 0;
10131 }
10132
0a93529c
GB
10133 read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
10134 (gdb_byte *) &fst, sizeof (fst));
10135
10136 if (read_len != ret)
10137 error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
10138
10139 if (read_len != sizeof (fst))
10140 error (_("vFile:fstat returned %d bytes, but expecting %d."),
10141 read_len, (int) sizeof (fst));
10142
10143 remote_fileio_to_host_stat (&fst, st);
10144
10145 return 0;
10146}
10147
a6b151f1
DJ
10148static int
10149remote_fileio_errno_to_host (int errnum)
10150{
10151 switch (errnum)
10152 {
10153 case FILEIO_EPERM:
10154 return EPERM;
10155 case FILEIO_ENOENT:
10156 return ENOENT;
10157 case FILEIO_EINTR:
10158 return EINTR;
10159 case FILEIO_EIO:
10160 return EIO;
10161 case FILEIO_EBADF:
10162 return EBADF;
10163 case FILEIO_EACCES:
10164 return EACCES;
10165 case FILEIO_EFAULT:
10166 return EFAULT;
10167 case FILEIO_EBUSY:
10168 return EBUSY;
10169 case FILEIO_EEXIST:
10170 return EEXIST;
10171 case FILEIO_ENODEV:
10172 return ENODEV;
10173 case FILEIO_ENOTDIR:
10174 return ENOTDIR;
10175 case FILEIO_EISDIR:
10176 return EISDIR;
10177 case FILEIO_EINVAL:
10178 return EINVAL;
10179 case FILEIO_ENFILE:
10180 return ENFILE;
10181 case FILEIO_EMFILE:
10182 return EMFILE;
10183 case FILEIO_EFBIG:
10184 return EFBIG;
10185 case FILEIO_ENOSPC:
10186 return ENOSPC;
10187 case FILEIO_ESPIPE:
10188 return ESPIPE;
10189 case FILEIO_EROFS:
10190 return EROFS;
10191 case FILEIO_ENOSYS:
10192 return ENOSYS;
10193 case FILEIO_ENAMETOOLONG:
10194 return ENAMETOOLONG;
10195 }
10196 return -1;
10197}
10198
10199static char *
10200remote_hostio_error (int errnum)
10201{
10202 int host_error = remote_fileio_errno_to_host (errnum);
10203
10204 if (host_error == -1)
10205 error (_("Unknown remote I/O error %d"), errnum);
10206 else
10207 error (_("Remote I/O error: %s"), safe_strerror (host_error));
10208}
10209
a6b151f1
DJ
10210static void
10211remote_hostio_close_cleanup (void *opaque)
10212{
10213 int fd = *(int *) opaque;
10214 int remote_errno;
10215
df39ea25 10216 remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
a6b151f1
DJ
10217}
10218
10219void
10220remote_file_put (const char *local_file, const char *remote_file, int from_tty)
10221{
10222 struct cleanup *back_to, *close_cleanup;
10223 int retcode, fd, remote_errno, bytes, io_size;
10224 FILE *file;
10225 gdb_byte *buffer;
10226 int bytes_in_buffer;
10227 int saw_eof;
10228 ULONGEST offset;
5d93a237 10229 struct remote_state *rs = get_remote_state ();
a6b151f1 10230
5d93a237 10231 if (!rs->remote_desc)
a6b151f1
DJ
10232 error (_("command can only be used with remote target"));
10233
614c279d 10234 file = gdb_fopen_cloexec (local_file, "rb");
a6b151f1
DJ
10235 if (file == NULL)
10236 perror_with_name (local_file);
7c8a8b04 10237 back_to = make_cleanup_fclose (file);
a6b151f1 10238
cd897586
TT
10239 fd = remote_hostio_open (find_target_at (process_stratum),
10240 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
a6b151f1
DJ
10241 | FILEIO_O_TRUNC),
10242 0700, &remote_errno);
10243 if (fd == -1)
10244 remote_hostio_error (remote_errno);
10245
10246 /* Send up to this many bytes at once. They won't all fit in the
10247 remote packet limit, so we'll transfer slightly fewer. */
10248 io_size = get_remote_packet_size ();
10249 buffer = xmalloc (io_size);
10250 make_cleanup (xfree, buffer);
10251
10252 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10253
10254 bytes_in_buffer = 0;
10255 saw_eof = 0;
10256 offset = 0;
10257 while (bytes_in_buffer || !saw_eof)
10258 {
10259 if (!saw_eof)
10260 {
3e43a32a
MS
10261 bytes = fread (buffer + bytes_in_buffer, 1,
10262 io_size - bytes_in_buffer,
a6b151f1
DJ
10263 file);
10264 if (bytes == 0)
10265 {
10266 if (ferror (file))
10267 error (_("Error reading %s."), local_file);
10268 else
10269 {
10270 /* EOF. Unless there is something still in the
10271 buffer from the last iteration, we are done. */
10272 saw_eof = 1;
10273 if (bytes_in_buffer == 0)
10274 break;
10275 }
10276 }
10277 }
10278 else
10279 bytes = 0;
10280
10281 bytes += bytes_in_buffer;
10282 bytes_in_buffer = 0;
10283
0d866f62
TT
10284 retcode = remote_hostio_pwrite (find_target_at (process_stratum),
10285 fd, buffer, bytes,
3e43a32a 10286 offset, &remote_errno);
a6b151f1
DJ
10287
10288 if (retcode < 0)
10289 remote_hostio_error (remote_errno);
10290 else if (retcode == 0)
10291 error (_("Remote write of %d bytes returned 0!"), bytes);
10292 else if (retcode < bytes)
10293 {
10294 /* Short write. Save the rest of the read data for the next
10295 write. */
10296 bytes_in_buffer = bytes - retcode;
10297 memmove (buffer, buffer + retcode, bytes_in_buffer);
10298 }
10299
10300 offset += retcode;
10301 }
10302
10303 discard_cleanups (close_cleanup);
df39ea25 10304 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10305 remote_hostio_error (remote_errno);
10306
10307 if (from_tty)
10308 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
10309 do_cleanups (back_to);
10310}
10311
10312void
10313remote_file_get (const char *remote_file, const char *local_file, int from_tty)
10314{
10315 struct cleanup *back_to, *close_cleanup;
cea39f65 10316 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
10317 FILE *file;
10318 gdb_byte *buffer;
10319 ULONGEST offset;
5d93a237 10320 struct remote_state *rs = get_remote_state ();
a6b151f1 10321
5d93a237 10322 if (!rs->remote_desc)
a6b151f1
DJ
10323 error (_("command can only be used with remote target"));
10324
cd897586
TT
10325 fd = remote_hostio_open (find_target_at (process_stratum),
10326 remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
a6b151f1
DJ
10327 if (fd == -1)
10328 remote_hostio_error (remote_errno);
10329
614c279d 10330 file = gdb_fopen_cloexec (local_file, "wb");
a6b151f1
DJ
10331 if (file == NULL)
10332 perror_with_name (local_file);
7c8a8b04 10333 back_to = make_cleanup_fclose (file);
a6b151f1
DJ
10334
10335 /* Send up to this many bytes at once. They won't all fit in the
10336 remote packet limit, so we'll transfer slightly fewer. */
10337 io_size = get_remote_packet_size ();
10338 buffer = xmalloc (io_size);
10339 make_cleanup (xfree, buffer);
10340
10341 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
10342
10343 offset = 0;
10344 while (1)
10345 {
a3be983c
TT
10346 bytes = remote_hostio_pread (find_target_at (process_stratum),
10347 fd, buffer, io_size, offset, &remote_errno);
a6b151f1
DJ
10348 if (bytes == 0)
10349 /* Success, but no bytes, means end-of-file. */
10350 break;
10351 if (bytes == -1)
10352 remote_hostio_error (remote_errno);
10353
10354 offset += bytes;
10355
10356 bytes = fwrite (buffer, 1, bytes, file);
10357 if (bytes == 0)
10358 perror_with_name (local_file);
10359 }
10360
10361 discard_cleanups (close_cleanup);
df39ea25 10362 if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
a6b151f1
DJ
10363 remote_hostio_error (remote_errno);
10364
10365 if (from_tty)
10366 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
10367 do_cleanups (back_to);
10368}
10369
10370void
10371remote_file_delete (const char *remote_file, int from_tty)
10372{
10373 int retcode, remote_errno;
5d93a237 10374 struct remote_state *rs = get_remote_state ();
a6b151f1 10375
5d93a237 10376 if (!rs->remote_desc)
a6b151f1
DJ
10377 error (_("command can only be used with remote target"));
10378
dbbca37d
TT
10379 retcode = remote_hostio_unlink (find_target_at (process_stratum),
10380 remote_file, &remote_errno);
a6b151f1
DJ
10381 if (retcode == -1)
10382 remote_hostio_error (remote_errno);
10383
10384 if (from_tty)
10385 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
10386}
10387
10388static void
10389remote_put_command (char *args, int from_tty)
10390{
10391 struct cleanup *back_to;
10392 char **argv;
10393
d1a41061
PP
10394 if (args == NULL)
10395 error_no_arg (_("file to put"));
10396
10397 argv = gdb_buildargv (args);
a6b151f1
DJ
10398 back_to = make_cleanup_freeargv (argv);
10399 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10400 error (_("Invalid parameters to remote put"));
10401
10402 remote_file_put (argv[0], argv[1], from_tty);
10403
10404 do_cleanups (back_to);
10405}
10406
10407static void
10408remote_get_command (char *args, int from_tty)
10409{
10410 struct cleanup *back_to;
10411 char **argv;
10412
d1a41061
PP
10413 if (args == NULL)
10414 error_no_arg (_("file to get"));
10415
10416 argv = gdb_buildargv (args);
a6b151f1
DJ
10417 back_to = make_cleanup_freeargv (argv);
10418 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
10419 error (_("Invalid parameters to remote get"));
10420
10421 remote_file_get (argv[0], argv[1], from_tty);
10422
10423 do_cleanups (back_to);
10424}
10425
10426static void
10427remote_delete_command (char *args, int from_tty)
10428{
10429 struct cleanup *back_to;
10430 char **argv;
10431
d1a41061
PP
10432 if (args == NULL)
10433 error_no_arg (_("file to delete"));
10434
10435 argv = gdb_buildargv (args);
a6b151f1
DJ
10436 back_to = make_cleanup_freeargv (argv);
10437 if (argv[0] == NULL || argv[1] != NULL)
10438 error (_("Invalid parameters to remote delete"));
10439
10440 remote_file_delete (argv[0], from_tty);
10441
10442 do_cleanups (back_to);
10443}
10444
10445static void
10446remote_command (char *args, int from_tty)
10447{
635c7e8a 10448 help_list (remote_cmdlist, "remote ", all_commands, gdb_stdout);
a6b151f1
DJ
10449}
10450
b2175913 10451static int
19db3e69 10452remote_can_execute_reverse (struct target_ops *self)
b2175913 10453{
4082afcc
PA
10454 if (packet_support (PACKET_bs) == PACKET_ENABLE
10455 || packet_support (PACKET_bc) == PACKET_ENABLE)
40ab02ce
MS
10456 return 1;
10457 else
10458 return 0;
b2175913
MS
10459}
10460
74531fed 10461static int
2a9a2795 10462remote_supports_non_stop (struct target_ops *self)
74531fed
PA
10463{
10464 return 1;
10465}
10466
03583c20 10467static int
2bfc0540 10468remote_supports_disable_randomization (struct target_ops *self)
03583c20
UW
10469{
10470 /* Only supported in extended mode. */
10471 return 0;
10472}
10473
8a305172 10474static int
86ce2668 10475remote_supports_multi_process (struct target_ops *self)
8a305172
PA
10476{
10477 struct remote_state *rs = get_remote_state ();
a744cf53 10478
901f9912
UW
10479 /* Only extended-remote handles being attached to multiple
10480 processes, even though plain remote can use the multi-process
10481 thread id extensions, so that GDB knows the target process's
10482 PID. */
10483 return rs->extended && remote_multi_process_p (rs);
8a305172
PA
10484}
10485
70221824 10486static int
782b2b07
SS
10487remote_supports_cond_tracepoints (void)
10488{
4082afcc 10489 return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
782b2b07
SS
10490}
10491
3788aec7 10492static int
efcc2da7 10493remote_supports_cond_breakpoints (struct target_ops *self)
3788aec7 10494{
4082afcc 10495 return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
3788aec7
LM
10496}
10497
70221824 10498static int
7a697b8d
SS
10499remote_supports_fast_tracepoints (void)
10500{
4082afcc 10501 return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
7a697b8d
SS
10502}
10503
0fb4aa4b
PA
10504static int
10505remote_supports_static_tracepoints (void)
10506{
4082afcc 10507 return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
0fb4aa4b
PA
10508}
10509
1e4d1764
YQ
10510static int
10511remote_supports_install_in_trace (void)
10512{
4082afcc 10513 return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
1e4d1764
YQ
10514}
10515
d248b706 10516static int
7d178d6a 10517remote_supports_enable_disable_tracepoint (struct target_ops *self)
d248b706 10518{
4082afcc
PA
10519 return (packet_support (PACKET_EnableDisableTracepoints_feature)
10520 == PACKET_ENABLE);
d248b706
KY
10521}
10522
3065dfb6 10523static int
6de37a3a 10524remote_supports_string_tracing (struct target_ops *self)
3065dfb6 10525{
4082afcc 10526 return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
3065dfb6
SS
10527}
10528
d3ce09f5 10529static int
78eff0ec 10530remote_can_run_breakpoint_commands (struct target_ops *self)
d3ce09f5 10531{
4082afcc 10532 return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
d3ce09f5
SS
10533}
10534
35b1e5cc 10535static void
ecae04e1 10536remote_trace_init (struct target_ops *self)
35b1e5cc
SS
10537{
10538 putpkt ("QTinit");
10539 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99 10540 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
10541 error (_("Target does not support this command."));
10542}
10543
10544static void free_actions_list (char **actions_list);
10545static void free_actions_list_cleanup_wrapper (void *);
10546static void
10547free_actions_list_cleanup_wrapper (void *al)
10548{
10549 free_actions_list (al);
10550}
10551
10552static void
10553free_actions_list (char **actions_list)
10554{
10555 int ndx;
10556
10557 if (actions_list == 0)
10558 return;
10559
10560 for (ndx = 0; actions_list[ndx]; ndx++)
10561 xfree (actions_list[ndx]);
10562
10563 xfree (actions_list);
10564}
10565
409873ef
SS
10566/* Recursive routine to walk through command list including loops, and
10567 download packets for each command. */
10568
10569static void
10570remote_download_command_source (int num, ULONGEST addr,
10571 struct command_line *cmds)
10572{
10573 struct remote_state *rs = get_remote_state ();
10574 struct command_line *cmd;
10575
10576 for (cmd = cmds; cmd; cmd = cmd->next)
10577 {
0df8b418 10578 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
10579 strcpy (rs->buf, "QTDPsrc:");
10580 encode_source_string (num, addr, "cmd", cmd->line,
10581 rs->buf + strlen (rs->buf),
10582 rs->buf_size - strlen (rs->buf));
10583 putpkt (rs->buf);
10584 remote_get_noisy_reply (&target_buf, &target_buf_size);
10585 if (strcmp (target_buf, "OK"))
10586 warning (_("Target does not support source download."));
10587
10588 if (cmd->control_type == while_control
10589 || cmd->control_type == while_stepping_control)
10590 {
10591 remote_download_command_source (num, addr, *cmd->body_list);
10592
0df8b418 10593 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
10594 strcpy (rs->buf, "QTDPsrc:");
10595 encode_source_string (num, addr, "cmd", "end",
10596 rs->buf + strlen (rs->buf),
10597 rs->buf_size - strlen (rs->buf));
10598 putpkt (rs->buf);
10599 remote_get_noisy_reply (&target_buf, &target_buf_size);
10600 if (strcmp (target_buf, "OK"))
10601 warning (_("Target does not support source download."));
10602 }
10603 }
10604}
10605
35b1e5cc 10606static void
548f7808 10607remote_download_tracepoint (struct target_ops *self, struct bp_location *loc)
35b1e5cc 10608{
bba74b36 10609#define BUF_SIZE 2048
e8ba3115 10610
35b1e5cc 10611 CORE_ADDR tpaddr;
409873ef 10612 char addrbuf[40];
bba74b36 10613 char buf[BUF_SIZE];
35b1e5cc
SS
10614 char **tdp_actions;
10615 char **stepping_actions;
10616 int ndx;
10617 struct cleanup *old_chain = NULL;
10618 struct agent_expr *aexpr;
10619 struct cleanup *aexpr_chain = NULL;
10620 char *pkt;
e8ba3115 10621 struct breakpoint *b = loc->owner;
d9b3f62e 10622 struct tracepoint *t = (struct tracepoint *) b;
35b1e5cc 10623
dc673c81 10624 encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
e8ba3115
YQ
10625 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
10626 tdp_actions);
10627 (void) make_cleanup (free_actions_list_cleanup_wrapper,
10628 stepping_actions);
10629
10630 tpaddr = loc->address;
10631 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
10632 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
10633 addrbuf, /* address */
10634 (b->enable_state == bp_enabled ? 'E' : 'D'),
10635 t->step_count, t->pass_count);
e8ba3115
YQ
10636 /* Fast tracepoints are mostly handled by the target, but we can
10637 tell the target how big of an instruction block should be moved
10638 around. */
10639 if (b->type == bp_fast_tracepoint)
10640 {
10641 /* Only test for support at download time; we may not know
10642 target capabilities at definition time. */
10643 if (remote_supports_fast_tracepoints ())
35b1e5cc 10644 {
e8ba3115 10645 int isize;
35b1e5cc 10646
f5656ead 10647 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
e8ba3115 10648 tpaddr, &isize, NULL))
bba74b36
YQ
10649 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
10650 isize);
35b1e5cc 10651 else
e8ba3115
YQ
10652 /* If it passed validation at definition but fails now,
10653 something is very wrong. */
10654 internal_error (__FILE__, __LINE__,
10655 _("Fast tracepoint not "
10656 "valid during download"));
35b1e5cc 10657 }
e8ba3115
YQ
10658 else
10659 /* Fast tracepoints are functionally identical to regular
10660 tracepoints, so don't take lack of support as a reason to
10661 give up on the trace run. */
10662 warning (_("Target does not support fast tracepoints, "
10663 "downloading %d as regular tracepoint"), b->number);
10664 }
10665 else if (b->type == bp_static_tracepoint)
10666 {
10667 /* Only test for support at download time; we may not know
10668 target capabilities at definition time. */
10669 if (remote_supports_static_tracepoints ())
0fb4aa4b 10670 {
e8ba3115 10671 struct static_tracepoint_marker marker;
0fb4aa4b 10672
e8ba3115
YQ
10673 if (target_static_tracepoint_marker_at (tpaddr, &marker))
10674 strcat (buf, ":S");
0fb4aa4b 10675 else
e8ba3115 10676 error (_("Static tracepoint not valid during download"));
0fb4aa4b 10677 }
e8ba3115
YQ
10678 else
10679 /* Fast tracepoints are functionally identical to regular
10680 tracepoints, so don't take lack of support as a reason
10681 to give up on the trace run. */
10682 error (_("Target does not support static tracepoints"));
10683 }
10684 /* If the tracepoint has a conditional, make it into an agent
10685 expression and append to the definition. */
10686 if (loc->cond)
10687 {
10688 /* Only test support at download time, we may not know target
10689 capabilities at definition time. */
10690 if (remote_supports_cond_tracepoints ())
35b1e5cc 10691 {
e8ba3115
YQ
10692 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
10693 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
bba74b36
YQ
10694 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
10695 aexpr->len);
e8ba3115
YQ
10696 pkt = buf + strlen (buf);
10697 for (ndx = 0; ndx < aexpr->len; ++ndx)
10698 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
10699 *pkt = '\0';
10700 do_cleanups (aexpr_chain);
35b1e5cc 10701 }
e8ba3115
YQ
10702 else
10703 warning (_("Target does not support conditional tracepoints, "
10704 "ignoring tp %d cond"), b->number);
10705 }
35b1e5cc 10706
d9b3f62e 10707 if (b->commands || *default_collect)
e8ba3115
YQ
10708 strcat (buf, "-");
10709 putpkt (buf);
10710 remote_get_noisy_reply (&target_buf, &target_buf_size);
10711 if (strcmp (target_buf, "OK"))
10712 error (_("Target does not support tracepoints."));
35b1e5cc 10713
e8ba3115
YQ
10714 /* do_single_steps (t); */
10715 if (tdp_actions)
10716 {
10717 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 10718 {
e8ba3115 10719 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
10720 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
10721 b->number, addrbuf, /* address */
10722 tdp_actions[ndx],
10723 ((tdp_actions[ndx + 1] || stepping_actions)
10724 ? '-' : 0));
e8ba3115
YQ
10725 putpkt (buf);
10726 remote_get_noisy_reply (&target_buf,
10727 &target_buf_size);
10728 if (strcmp (target_buf, "OK"))
10729 error (_("Error on target while setting tracepoints."));
35b1e5cc 10730 }
e8ba3115
YQ
10731 }
10732 if (stepping_actions)
10733 {
10734 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 10735 {
e8ba3115 10736 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
10737 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
10738 b->number, addrbuf, /* address */
10739 ((ndx == 0) ? "S" : ""),
10740 stepping_actions[ndx],
10741 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115
YQ
10742 putpkt (buf);
10743 remote_get_noisy_reply (&target_buf,
10744 &target_buf_size);
10745 if (strcmp (target_buf, "OK"))
10746 error (_("Error on target while setting tracepoints."));
35b1e5cc 10747 }
e8ba3115 10748 }
409873ef 10749
4082afcc 10750 if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
e8ba3115
YQ
10751 {
10752 if (b->addr_string)
409873ef 10753 {
e8ba3115
YQ
10754 strcpy (buf, "QTDPsrc:");
10755 encode_source_string (b->number, loc->address,
10756 "at", b->addr_string, buf + strlen (buf),
10757 2048 - strlen (buf));
409873ef 10758
e8ba3115
YQ
10759 putpkt (buf);
10760 remote_get_noisy_reply (&target_buf, &target_buf_size);
10761 if (strcmp (target_buf, "OK"))
10762 warning (_("Target does not support source download."));
409873ef 10763 }
e8ba3115
YQ
10764 if (b->cond_string)
10765 {
10766 strcpy (buf, "QTDPsrc:");
10767 encode_source_string (b->number, loc->address,
10768 "cond", b->cond_string, buf + strlen (buf),
10769 2048 - strlen (buf));
10770 putpkt (buf);
10771 remote_get_noisy_reply (&target_buf, &target_buf_size);
10772 if (strcmp (target_buf, "OK"))
10773 warning (_("Target does not support source download."));
10774 }
10775 remote_download_command_source (b->number, loc->address,
10776 breakpoint_commands (b));
35b1e5cc 10777 }
e8ba3115
YQ
10778
10779 do_cleanups (old_chain);
35b1e5cc
SS
10780}
10781
1e4d1764 10782static int
a52a8357 10783remote_can_download_tracepoint (struct target_ops *self)
1e4d1764 10784{
1e51243a
PA
10785 struct remote_state *rs = get_remote_state ();
10786 struct trace_status *ts;
10787 int status;
10788
10789 /* Don't try to install tracepoints until we've relocated our
10790 symbols, and fetched and merged the target's tracepoint list with
10791 ours. */
10792 if (rs->starting_up)
10793 return 0;
10794
10795 ts = current_trace_status ();
8bd200f1 10796 status = remote_get_trace_status (self, ts);
1e4d1764
YQ
10797
10798 if (status == -1 || !ts->running_known || !ts->running)
10799 return 0;
10800
10801 /* If we are in a tracing experiment, but remote stub doesn't support
10802 installing tracepoint in trace, we have to return. */
10803 if (!remote_supports_install_in_trace ())
10804 return 0;
10805
10806 return 1;
10807}
10808
10809
35b1e5cc 10810static void
559d2b81
TT
10811remote_download_trace_state_variable (struct target_ops *self,
10812 struct trace_state_variable *tsv)
35b1e5cc
SS
10813{
10814 struct remote_state *rs = get_remote_state ();
00bf0b85 10815 char *p;
35b1e5cc 10816
bba74b36
YQ
10817 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
10818 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
10819 tsv->builtin);
00bf0b85
SS
10820 p = rs->buf + strlen (rs->buf);
10821 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
10822 error (_("Trace state variable name too long for tsv definition packet"));
9f1b45b0 10823 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, strlen (tsv->name));
00bf0b85 10824 *p++ = '\0';
35b1e5cc
SS
10825 putpkt (rs->buf);
10826 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
10827 if (*target_buf == '\0')
10828 error (_("Target does not support this command."));
10829 if (strcmp (target_buf, "OK") != 0)
10830 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
10831}
10832
d248b706 10833static void
46670d57
TT
10834remote_enable_tracepoint (struct target_ops *self,
10835 struct bp_location *location)
d248b706
KY
10836{
10837 struct remote_state *rs = get_remote_state ();
10838 char addr_buf[40];
10839
10840 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
10841 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
10842 location->owner->number, addr_buf);
d248b706
KY
10843 putpkt (rs->buf);
10844 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10845 if (*rs->buf == '\0')
10846 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
10847 if (strcmp (rs->buf, "OK") != 0)
10848 error (_("Error on target while enabling tracepoint."));
10849}
10850
10851static void
780b049c
TT
10852remote_disable_tracepoint (struct target_ops *self,
10853 struct bp_location *location)
d248b706
KY
10854{
10855 struct remote_state *rs = get_remote_state ();
10856 char addr_buf[40];
10857
10858 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
10859 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
10860 location->owner->number, addr_buf);
d248b706
KY
10861 putpkt (rs->buf);
10862 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10863 if (*rs->buf == '\0')
10864 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
10865 if (strcmp (rs->buf, "OK") != 0)
10866 error (_("Error on target while disabling tracepoint."));
10867}
10868
35b1e5cc 10869static void
583f9a86 10870remote_trace_set_readonly_regions (struct target_ops *self)
35b1e5cc
SS
10871{
10872 asection *s;
81b9b86e 10873 bfd *abfd = NULL;
35b1e5cc 10874 bfd_size_type size;
608bcef2 10875 bfd_vma vma;
35b1e5cc 10876 int anysecs = 0;
c2fa21f1 10877 int offset = 0;
35b1e5cc
SS
10878
10879 if (!exec_bfd)
10880 return; /* No information to give. */
10881
10882 strcpy (target_buf, "QTro");
9779ab84 10883 offset = strlen (target_buf);
35b1e5cc
SS
10884 for (s = exec_bfd->sections; s; s = s->next)
10885 {
10886 char tmp1[40], tmp2[40];
c2fa21f1 10887 int sec_length;
35b1e5cc
SS
10888
10889 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 10890 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
10891 (s->flags & SEC_READONLY) == 0)
10892 continue;
10893
10894 anysecs = 1;
81b9b86e 10895 vma = bfd_get_section_vma (abfd, s);
35b1e5cc 10896 size = bfd_get_section_size (s);
608bcef2
HZ
10897 sprintf_vma (tmp1, vma);
10898 sprintf_vma (tmp2, vma + size);
c2fa21f1
HZ
10899 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
10900 if (offset + sec_length + 1 > target_buf_size)
10901 {
4082afcc 10902 if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
864ac8a7 10903 warning (_("\
c2fa21f1
HZ
10904Too many sections for read-only sections definition packet."));
10905 break;
10906 }
bba74b36
YQ
10907 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
10908 tmp1, tmp2);
c2fa21f1 10909 offset += sec_length;
35b1e5cc
SS
10910 }
10911 if (anysecs)
10912 {
10913 putpkt (target_buf);
10914 getpkt (&target_buf, &target_buf_size, 0);
10915 }
10916}
10917
10918static void
e2d1aae3 10919remote_trace_start (struct target_ops *self)
35b1e5cc
SS
10920{
10921 putpkt ("QTStart");
10922 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
10923 if (*target_buf == '\0')
10924 error (_("Target does not support this command."));
10925 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
10926 error (_("Bogus reply from target: %s"), target_buf);
10927}
10928
10929static int
8bd200f1 10930remote_get_trace_status (struct target_ops *self, struct trace_status *ts)
35b1e5cc 10931{
953b98d1 10932 /* Initialize it just to avoid a GCC false warning. */
f652de6f 10933 char *p = NULL;
0df8b418 10934 /* FIXME we need to get register block size some other way. */
00bf0b85 10935 extern int trace_regblock_size;
bd3eecc3
PA
10936 enum packet_result result;
10937
4082afcc 10938 if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
bd3eecc3 10939 return -1;
a744cf53 10940
00bf0b85
SS
10941 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
10942
049dc89b
JK
10943 putpkt ("qTStatus");
10944
492d29ea 10945 TRY
67f41397
JK
10946 {
10947 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
10948 }
492d29ea 10949 CATCH (ex, RETURN_MASK_ERROR)
67f41397 10950 {
598d3636
JK
10951 if (ex.error != TARGET_CLOSE_ERROR)
10952 {
10953 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
10954 return -1;
10955 }
10956 throw_exception (ex);
67f41397 10957 }
492d29ea 10958 END_CATCH
00bf0b85 10959
bd3eecc3
PA
10960 result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
10961
00bf0b85 10962 /* If the remote target doesn't do tracing, flag it. */
bd3eecc3 10963 if (result == PACKET_UNKNOWN)
00bf0b85 10964 return -1;
35b1e5cc 10965
00bf0b85 10966 /* We're working with a live target. */
f5911ea1 10967 ts->filename = NULL;
00bf0b85 10968
00bf0b85 10969 if (*p++ != 'T')
35b1e5cc
SS
10970 error (_("Bogus trace status reply from target: %s"), target_buf);
10971
84cebc4a
YQ
10972 /* Function 'parse_trace_status' sets default value of each field of
10973 'ts' at first, so we don't have to do it here. */
00bf0b85
SS
10974 parse_trace_status (p, ts);
10975
10976 return ts->running;
35b1e5cc
SS
10977}
10978
70221824 10979static void
db90e85c 10980remote_get_tracepoint_status (struct target_ops *self, struct breakpoint *bp,
f196051f
SS
10981 struct uploaded_tp *utp)
10982{
10983 struct remote_state *rs = get_remote_state ();
f196051f
SS
10984 char *reply;
10985 struct bp_location *loc;
10986 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 10987 size_t size = get_remote_packet_size ();
f196051f
SS
10988
10989 if (tp)
10990 {
10991 tp->base.hit_count = 0;
10992 tp->traceframe_usage = 0;
10993 for (loc = tp->base.loc; loc; loc = loc->next)
10994 {
10995 /* If the tracepoint was never downloaded, don't go asking for
10996 any status. */
10997 if (tp->number_on_target == 0)
10998 continue;
bba74b36
YQ
10999 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
11000 phex_nz (loc->address, 0));
f196051f
SS
11001 putpkt (rs->buf);
11002 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11003 if (reply && *reply)
11004 {
11005 if (*reply == 'V')
11006 parse_tracepoint_status (reply + 1, bp, utp);
11007 }
11008 }
11009 }
11010 else if (utp)
11011 {
11012 utp->hit_count = 0;
11013 utp->traceframe_usage = 0;
bba74b36
YQ
11014 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
11015 phex_nz (utp->addr, 0));
f196051f
SS
11016 putpkt (rs->buf);
11017 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11018 if (reply && *reply)
11019 {
11020 if (*reply == 'V')
11021 parse_tracepoint_status (reply + 1, bp, utp);
11022 }
11023 }
11024}
11025
35b1e5cc 11026static void
74499f1b 11027remote_trace_stop (struct target_ops *self)
35b1e5cc
SS
11028{
11029 putpkt ("QTStop");
11030 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
11031 if (*target_buf == '\0')
11032 error (_("Target does not support this command."));
11033 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
11034 error (_("Bogus reply from target: %s"), target_buf);
11035}
11036
11037static int
bd4c6793
TT
11038remote_trace_find (struct target_ops *self,
11039 enum trace_find_type type, int num,
cc5925ad 11040 CORE_ADDR addr1, CORE_ADDR addr2,
35b1e5cc
SS
11041 int *tpp)
11042{
11043 struct remote_state *rs = get_remote_state ();
bba74b36 11044 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
11045 char *p, *reply;
11046 int target_frameno = -1, target_tracept = -1;
11047
e6e4e701
PA
11048 /* Lookups other than by absolute frame number depend on the current
11049 trace selected, so make sure it is correct on the remote end
11050 first. */
11051 if (type != tfind_number)
11052 set_remote_traceframe ();
11053
35b1e5cc
SS
11054 p = rs->buf;
11055 strcpy (p, "QTFrame:");
11056 p = strchr (p, '\0');
11057 switch (type)
11058 {
11059 case tfind_number:
bba74b36 11060 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
11061 break;
11062 case tfind_pc:
bba74b36 11063 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
11064 break;
11065 case tfind_tp:
bba74b36 11066 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
11067 break;
11068 case tfind_range:
bba74b36
YQ
11069 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
11070 phex_nz (addr2, 0));
35b1e5cc
SS
11071 break;
11072 case tfind_outside:
bba74b36
YQ
11073 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
11074 phex_nz (addr2, 0));
35b1e5cc
SS
11075 break;
11076 default:
9b20d036 11077 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
11078 }
11079
11080 putpkt (rs->buf);
2f65bcb7 11081 reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size);
ad91cd99
PA
11082 if (*reply == '\0')
11083 error (_("Target does not support this command."));
35b1e5cc
SS
11084
11085 while (reply && *reply)
11086 switch (*reply)
11087 {
11088 case 'F':
f197e0f1
VP
11089 p = ++reply;
11090 target_frameno = (int) strtol (p, &reply, 16);
11091 if (reply == p)
11092 error (_("Unable to parse trace frame number"));
e6e4e701
PA
11093 /* Don't update our remote traceframe number cache on failure
11094 to select a remote traceframe. */
f197e0f1
VP
11095 if (target_frameno == -1)
11096 return -1;
35b1e5cc
SS
11097 break;
11098 case 'T':
f197e0f1
VP
11099 p = ++reply;
11100 target_tracept = (int) strtol (p, &reply, 16);
11101 if (reply == p)
11102 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
11103 break;
11104 case 'O': /* "OK"? */
11105 if (reply[1] == 'K' && reply[2] == '\0')
11106 reply += 2;
11107 else
11108 error (_("Bogus reply from target: %s"), reply);
11109 break;
11110 default:
11111 error (_("Bogus reply from target: %s"), reply);
11112 }
11113 if (tpp)
11114 *tpp = target_tracept;
e6e4e701 11115
262e1174 11116 rs->remote_traceframe_number = target_frameno;
35b1e5cc
SS
11117 return target_frameno;
11118}
11119
11120static int
4011015b
TT
11121remote_get_trace_state_variable_value (struct target_ops *self,
11122 int tsvnum, LONGEST *val)
35b1e5cc
SS
11123{
11124 struct remote_state *rs = get_remote_state ();
11125 char *reply;
11126 ULONGEST uval;
11127
e6e4e701
PA
11128 set_remote_traceframe ();
11129
bba74b36 11130 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc
SS
11131 putpkt (rs->buf);
11132 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11133 if (reply && *reply)
11134 {
11135 if (*reply == 'V')
11136 {
11137 unpack_varlen_hex (reply + 1, &uval);
11138 *val = (LONGEST) uval;
11139 return 1;
11140 }
11141 }
11142 return 0;
11143}
11144
00bf0b85 11145static int
dc3decaf 11146remote_save_trace_data (struct target_ops *self, const char *filename)
00bf0b85
SS
11147{
11148 struct remote_state *rs = get_remote_state ();
11149 char *p, *reply;
11150
11151 p = rs->buf;
11152 strcpy (p, "QTSave:");
11153 p += strlen (p);
11154 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
11155 error (_("Remote file name too long for trace save packet"));
9f1b45b0 11156 p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
00bf0b85
SS
11157 *p++ = '\0';
11158 putpkt (rs->buf);
ad91cd99 11159 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
d6c5869f 11160 if (*reply == '\0')
ad91cd99
PA
11161 error (_("Target does not support this command."));
11162 if (strcmp (reply, "OK") != 0)
11163 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
11164 return 0;
11165}
11166
11167/* This is basically a memory transfer, but needs to be its own packet
11168 because we don't know how the target actually organizes its trace
11169 memory, plus we want to be able to ask for as much as possible, but
11170 not be unhappy if we don't get as much as we ask for. */
11171
11172static LONGEST
88ee6f45
TT
11173remote_get_raw_trace_data (struct target_ops *self,
11174 gdb_byte *buf, ULONGEST offset, LONGEST len)
00bf0b85
SS
11175{
11176 struct remote_state *rs = get_remote_state ();
11177 char *reply;
11178 char *p;
11179 int rslt;
11180
11181 p = rs->buf;
11182 strcpy (p, "qTBuffer:");
11183 p += strlen (p);
11184 p += hexnumstr (p, offset);
11185 *p++ = ',';
11186 p += hexnumstr (p, len);
11187 *p++ = '\0';
11188
11189 putpkt (rs->buf);
11190 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11191 if (reply && *reply)
11192 {
11193 /* 'l' by itself means we're at the end of the buffer and
11194 there is nothing more to get. */
11195 if (*reply == 'l')
11196 return 0;
11197
11198 /* Convert the reply into binary. Limit the number of bytes to
11199 convert according to our passed-in buffer size, rather than
11200 what was returned in the packet; if the target is
11201 unexpectedly generous and gives us a bigger reply than we
11202 asked for, we don't want to crash. */
11203 rslt = hex2bin (target_buf, buf, len);
11204 return rslt;
11205 }
11206
11207 /* Something went wrong, flag as an error. */
11208 return -1;
11209}
11210
35b1e5cc 11211static void
37b25738 11212remote_set_disconnected_tracing (struct target_ops *self, int val)
35b1e5cc
SS
11213{
11214 struct remote_state *rs = get_remote_state ();
11215
4082afcc 11216 if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
33da3f1c 11217 {
ad91cd99
PA
11218 char *reply;
11219
bba74b36 11220 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 11221 putpkt (rs->buf);
ad91cd99
PA
11222 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11223 if (*reply == '\0')
33da3f1c 11224 error (_("Target does not support this command."));
ad91cd99
PA
11225 if (strcmp (reply, "OK") != 0)
11226 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
11227 }
11228 else if (val)
11229 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
11230}
11231
dc146f7c
VP
11232static int
11233remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
11234{
11235 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 11236
fe978cb0
PA
11237 if (info && info->priv)
11238 return info->priv->core;
dc146f7c
VP
11239 return -1;
11240}
11241
4daf5ac0 11242static void
736d5b1f 11243remote_set_circular_trace_buffer (struct target_ops *self, int val)
4daf5ac0
SS
11244{
11245 struct remote_state *rs = get_remote_state ();
ad91cd99 11246 char *reply;
4daf5ac0 11247
bba74b36 11248 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 11249 putpkt (rs->buf);
ad91cd99
PA
11250 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11251 if (*reply == '\0')
4daf5ac0 11252 error (_("Target does not support this command."));
ad91cd99
PA
11253 if (strcmp (reply, "OK") != 0)
11254 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
11255}
11256
b3b9301e 11257static struct traceframe_info *
a893e81f 11258remote_traceframe_info (struct target_ops *self)
b3b9301e
PA
11259{
11260 char *text;
11261
11262 text = target_read_stralloc (&current_target,
11263 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11264 if (text != NULL)
11265 {
11266 struct traceframe_info *info;
11267 struct cleanup *back_to = make_cleanup (xfree, text);
11268
11269 info = parse_traceframe_info (text);
11270 do_cleanups (back_to);
11271 return info;
11272 }
11273
11274 return NULL;
11275}
11276
405f8e94
SS
11277/* Handle the qTMinFTPILen packet. Returns the minimum length of
11278 instruction on which a fast tracepoint may be placed. Returns -1
11279 if the packet is not supported, and 0 if the minimum instruction
11280 length is unknown. */
11281
11282static int
0e67620a 11283remote_get_min_fast_tracepoint_insn_len (struct target_ops *self)
405f8e94
SS
11284{
11285 struct remote_state *rs = get_remote_state ();
11286 char *reply;
11287
e886a173
PA
11288 /* If we're not debugging a process yet, the IPA can't be
11289 loaded. */
11290 if (!target_has_execution)
11291 return 0;
11292
11293 /* Make sure the remote is pointing at the right process. */
11294 set_general_process ();
11295
bba74b36 11296 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94
SS
11297 putpkt (rs->buf);
11298 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11299 if (*reply == '\0')
11300 return -1;
11301 else
11302 {
11303 ULONGEST min_insn_len;
11304
11305 unpack_varlen_hex (reply, &min_insn_len);
11306
11307 return (int) min_insn_len;
11308 }
11309}
11310
f6f899bf 11311static void
4da384be 11312remote_set_trace_buffer_size (struct target_ops *self, LONGEST val)
f6f899bf 11313{
4082afcc 11314 if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
f6f899bf
HAQ
11315 {
11316 struct remote_state *rs = get_remote_state ();
11317 char *buf = rs->buf;
11318 char *endbuf = rs->buf + get_remote_packet_size ();
11319 enum packet_result result;
11320
11321 gdb_assert (val >= 0 || val == -1);
11322 buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11323 /* Send -1 as literal "-1" to avoid host size dependency. */
11324 if (val < 0)
11325 {
11326 *buf++ = '-';
11327 buf += hexnumstr (buf, (ULONGEST) -val);
11328 }
11329 else
11330 buf += hexnumstr (buf, (ULONGEST) val);
11331
11332 putpkt (rs->buf);
11333 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11334 result = packet_ok (rs->buf,
11335 &remote_protocol_packets[PACKET_QTBuffer_size]);
11336
11337 if (result != PACKET_OK)
11338 warning (_("Bogus reply from target: %s"), rs->buf);
11339 }
11340}
11341
f196051f 11342static int
d9e68a2c
TT
11343remote_set_trace_notes (struct target_ops *self,
11344 const char *user, const char *notes,
ca623f82 11345 const char *stop_notes)
f196051f
SS
11346{
11347 struct remote_state *rs = get_remote_state ();
11348 char *reply;
11349 char *buf = rs->buf;
11350 char *endbuf = rs->buf + get_remote_packet_size ();
11351 int nbytes;
11352
11353 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11354 if (user)
11355 {
11356 buf += xsnprintf (buf, endbuf - buf, "user:");
9f1b45b0 11357 nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
f196051f
SS
11358 buf += 2 * nbytes;
11359 *buf++ = ';';
11360 }
11361 if (notes)
11362 {
11363 buf += xsnprintf (buf, endbuf - buf, "notes:");
9f1b45b0 11364 nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
f196051f
SS
11365 buf += 2 * nbytes;
11366 *buf++ = ';';
11367 }
11368 if (stop_notes)
11369 {
11370 buf += xsnprintf (buf, endbuf - buf, "tstop:");
9f1b45b0 11371 nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
f196051f
SS
11372 buf += 2 * nbytes;
11373 *buf++ = ';';
11374 }
11375 /* Ensure the buffer is terminated. */
11376 *buf = '\0';
11377
11378 putpkt (rs->buf);
11379 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11380 if (*reply == '\0')
11381 return 0;
11382
11383 if (strcmp (reply, "OK") != 0)
11384 error (_("Bogus reply from target: %s"), reply);
11385
11386 return 1;
11387}
11388
d1feda86 11389static int
2c152180 11390remote_use_agent (struct target_ops *self, int use)
d1feda86 11391{
4082afcc 11392 if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
d1feda86
YQ
11393 {
11394 struct remote_state *rs = get_remote_state ();
11395
11396 /* If the stub supports QAgent. */
bba74b36 11397 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
11398 putpkt (rs->buf);
11399 getpkt (&rs->buf, &rs->buf_size, 0);
11400
11401 if (strcmp (rs->buf, "OK") == 0)
11402 {
11403 use_agent = use;
11404 return 1;
11405 }
11406 }
11407
11408 return 0;
11409}
11410
11411static int
fe38f897 11412remote_can_use_agent (struct target_ops *self)
d1feda86 11413{
4082afcc 11414 return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
d1feda86
YQ
11415}
11416
9accd112
MM
11417struct btrace_target_info
11418{
11419 /* The ptid of the traced thread. */
11420 ptid_t ptid;
f4abbc16
MM
11421
11422 /* The obtained branch trace configuration. */
11423 struct btrace_config conf;
9accd112
MM
11424};
11425
f4abbc16
MM
11426/* Reset our idea of our target's btrace configuration. */
11427
11428static void
11429remote_btrace_reset (void)
11430{
11431 struct remote_state *rs = get_remote_state ();
11432
11433 memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
11434}
11435
9accd112
MM
11436/* Check whether the target supports branch tracing. */
11437
11438static int
043c3577 11439remote_supports_btrace (struct target_ops *self, enum btrace_format format)
9accd112 11440{
4082afcc 11441 if (packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE)
9accd112 11442 return 0;
4082afcc 11443 if (packet_support (PACKET_qXfer_btrace) != PACKET_ENABLE)
9accd112
MM
11444 return 0;
11445
043c3577
MM
11446 switch (format)
11447 {
11448 case BTRACE_FORMAT_NONE:
11449 return 0;
11450
11451 case BTRACE_FORMAT_BTS:
11452 return (packet_support (PACKET_Qbtrace_bts) == PACKET_ENABLE);
11453 }
11454
11455 internal_error (__FILE__, __LINE__, _("Unknown branch trace format"));
9accd112
MM
11456}
11457
f4abbc16
MM
11458/* Synchronize the configuration with the target. */
11459
11460static void
11461btrace_sync_conf (const struct btrace_config *conf)
11462{
d33501a5
MM
11463 struct packet_config *packet;
11464 struct remote_state *rs;
11465 char *buf, *pos, *endbuf;
11466
11467 rs = get_remote_state ();
11468 buf = rs->buf;
11469 endbuf = buf + get_remote_packet_size ();
11470
11471 packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
11472 if (packet_config_support (packet) == PACKET_ENABLE
11473 && conf->bts.size != rs->btrace_config.bts.size)
11474 {
11475 pos = buf;
11476 pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
11477 conf->bts.size);
11478
11479 putpkt (buf);
11480 getpkt (&buf, &rs->buf_size, 0);
11481
11482 if (packet_ok (buf, packet) == PACKET_ERROR)
11483 {
11484 if (buf[0] == 'E' && buf[1] == '.')
11485 error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
11486 else
11487 error (_("Failed to configure the BTS buffer size."));
11488 }
11489
11490 rs->btrace_config.bts.size = conf->bts.size;
11491 }
f4abbc16
MM
11492}
11493
11494/* Read the current thread's btrace configuration from the target and
11495 store it into CONF. */
11496
11497static void
11498btrace_read_config (struct btrace_config *conf)
11499{
11500 char *xml;
11501
11502 xml = target_read_stralloc (&current_target,
11503 TARGET_OBJECT_BTRACE_CONF, "");
11504 if (xml != NULL)
11505 {
11506 struct cleanup *cleanup;
11507
11508 cleanup = make_cleanup (xfree, xml);
11509 parse_xml_btrace_conf (conf, xml);
11510 do_cleanups (cleanup);
11511 }
11512}
11513
9accd112
MM
11514/* Enable branch tracing. */
11515
11516static struct btrace_target_info *
f4abbc16
MM
11517remote_enable_btrace (struct target_ops *self, ptid_t ptid,
11518 const struct btrace_config *conf)
9accd112
MM
11519{
11520 struct btrace_target_info *tinfo = NULL;
11521 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
11522 struct remote_state *rs = get_remote_state ();
11523 char *buf = rs->buf;
11524 char *endbuf = rs->buf + get_remote_packet_size ();
11525
4082afcc 11526 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
11527 error (_("Target does not support branch tracing."));
11528
f4abbc16
MM
11529 btrace_sync_conf (conf);
11530
9accd112
MM
11531 set_general_thread (ptid);
11532
11533 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11534 putpkt (rs->buf);
11535 getpkt (&rs->buf, &rs->buf_size, 0);
11536
11537 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11538 {
11539 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11540 error (_("Could not enable branch tracing for %s: %s"),
11541 target_pid_to_str (ptid), rs->buf + 2);
11542 else
11543 error (_("Could not enable branch tracing for %s."),
11544 target_pid_to_str (ptid));
11545 }
11546
11547 tinfo = xzalloc (sizeof (*tinfo));
11548 tinfo->ptid = ptid;
11549
f4abbc16
MM
11550 /* If we fail to read the configuration, we lose some information, but the
11551 tracing itself is not impacted. */
492d29ea
PA
11552 TRY
11553 {
11554 btrace_read_config (&tinfo->conf);
11555 }
11556 CATCH (err, RETURN_MASK_ERROR)
11557 {
11558 if (err.message != NULL)
11559 warning ("%s", err.message);
11560 }
11561 END_CATCH
f4abbc16 11562
9accd112
MM
11563 return tinfo;
11564}
11565
11566/* Disable branch tracing. */
11567
11568static void
25e95349
TT
11569remote_disable_btrace (struct target_ops *self,
11570 struct btrace_target_info *tinfo)
9accd112
MM
11571{
11572 struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
11573 struct remote_state *rs = get_remote_state ();
11574 char *buf = rs->buf;
11575 char *endbuf = rs->buf + get_remote_packet_size ();
11576
4082afcc 11577 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
11578 error (_("Target does not support branch tracing."));
11579
11580 set_general_thread (tinfo->ptid);
11581
11582 buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11583 putpkt (rs->buf);
11584 getpkt (&rs->buf, &rs->buf_size, 0);
11585
11586 if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11587 {
11588 if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11589 error (_("Could not disable branch tracing for %s: %s"),
11590 target_pid_to_str (tinfo->ptid), rs->buf + 2);
11591 else
11592 error (_("Could not disable branch tracing for %s."),
11593 target_pid_to_str (tinfo->ptid));
11594 }
11595
11596 xfree (tinfo);
11597}
11598
11599/* Teardown branch tracing. */
11600
11601static void
1777056d
TT
11602remote_teardown_btrace (struct target_ops *self,
11603 struct btrace_target_info *tinfo)
9accd112
MM
11604{
11605 /* We must not talk to the target during teardown. */
11606 xfree (tinfo);
11607}
11608
11609/* Read the branch trace. */
11610
969c39fb 11611static enum btrace_error
39c49f83 11612remote_read_btrace (struct target_ops *self,
734b0e4b 11613 struct btrace_data *btrace,
969c39fb 11614 struct btrace_target_info *tinfo,
9accd112
MM
11615 enum btrace_read_type type)
11616{
11617 struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
11618 struct remote_state *rs = get_remote_state ();
969c39fb 11619 struct cleanup *cleanup;
9accd112
MM
11620 const char *annex;
11621 char *xml;
11622
4082afcc 11623 if (packet_config_support (packet) != PACKET_ENABLE)
9accd112
MM
11624 error (_("Target does not support branch tracing."));
11625
11626#if !defined(HAVE_LIBEXPAT)
11627 error (_("Cannot process branch tracing result. XML parsing not supported."));
11628#endif
11629
11630 switch (type)
11631 {
864089d2 11632 case BTRACE_READ_ALL:
9accd112
MM
11633 annex = "all";
11634 break;
864089d2 11635 case BTRACE_READ_NEW:
9accd112
MM
11636 annex = "new";
11637 break;
969c39fb
MM
11638 case BTRACE_READ_DELTA:
11639 annex = "delta";
11640 break;
9accd112
MM
11641 default:
11642 internal_error (__FILE__, __LINE__,
11643 _("Bad branch tracing read type: %u."),
11644 (unsigned int) type);
11645 }
11646
11647 xml = target_read_stralloc (&current_target,
11648 TARGET_OBJECT_BTRACE, annex);
969c39fb
MM
11649 if (xml == NULL)
11650 return BTRACE_ERR_UNKNOWN;
9accd112 11651
969c39fb 11652 cleanup = make_cleanup (xfree, xml);
734b0e4b 11653 parse_xml_btrace (btrace, xml);
969c39fb 11654 do_cleanups (cleanup);
9accd112 11655
969c39fb 11656 return BTRACE_ERR_NONE;
9accd112
MM
11657}
11658
f4abbc16
MM
11659static const struct btrace_config *
11660remote_btrace_conf (struct target_ops *self,
11661 const struct btrace_target_info *tinfo)
11662{
11663 return &tinfo->conf;
11664}
11665
ced63ec0 11666static int
5436ff03 11667remote_augmented_libraries_svr4_read (struct target_ops *self)
ced63ec0 11668{
4082afcc
PA
11669 return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
11670 == PACKET_ENABLE);
ced63ec0
GB
11671}
11672
9dd130a0
TT
11673/* Implementation of to_load. */
11674
11675static void
9cbe5fff 11676remote_load (struct target_ops *self, const char *name, int from_tty)
9dd130a0
TT
11677{
11678 generic_load (name, from_tty);
11679}
11680
c78fa86a
GB
11681/* Accepts an integer PID; returns a string representing a file that
11682 can be opened on the remote side to get the symbols for the child
11683 process. Returns NULL if the operation is not supported. */
11684
11685static char *
11686remote_pid_to_exec_file (struct target_ops *self, int pid)
11687{
11688 static char *filename = NULL;
835205d0
GB
11689 struct inferior *inf;
11690 char *annex = NULL;
c78fa86a
GB
11691
11692 if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
11693 return NULL;
11694
11695 if (filename != NULL)
11696 xfree (filename);
11697
835205d0
GB
11698 inf = find_inferior_pid (pid);
11699 if (inf == NULL)
11700 internal_error (__FILE__, __LINE__,
11701 _("not currently attached to process %d"), pid);
11702
11703 if (!inf->fake_pid_p)
11704 {
11705 const int annex_size = 9;
11706
11707 annex = alloca (annex_size);
11708 xsnprintf (annex, annex_size, "%x", pid);
11709 }
11710
c78fa86a
GB
11711 filename = target_read_stralloc (&current_target,
11712 TARGET_OBJECT_EXEC_FILE, annex);
11713
11714 return filename;
11715}
11716
c906108c 11717static void
fba45db2 11718init_remote_ops (void)
c906108c 11719{
c5aa993b 11720 remote_ops.to_shortname = "remote";
c906108c 11721 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 11722 remote_ops.to_doc =
c906108c 11723 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
11724Specify the serial device it is connected to\n\
11725(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
11726 remote_ops.to_open = remote_open;
11727 remote_ops.to_close = remote_close;
c906108c 11728 remote_ops.to_detach = remote_detach;
6ad8ae5c 11729 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 11730 remote_ops.to_resume = remote_resume;
c906108c
SS
11731 remote_ops.to_wait = remote_wait;
11732 remote_ops.to_fetch_registers = remote_fetch_registers;
11733 remote_ops.to_store_registers = remote_store_registers;
11734 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b 11735 remote_ops.to_files_info = remote_files_info;
c906108c
SS
11736 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
11737 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
f7e6eed5
PA
11738 remote_ops.to_stopped_by_sw_breakpoint = remote_stopped_by_sw_breakpoint;
11739 remote_ops.to_supports_stopped_by_sw_breakpoint = remote_supports_stopped_by_sw_breakpoint;
11740 remote_ops.to_stopped_by_hw_breakpoint = remote_stopped_by_hw_breakpoint;
11741 remote_ops.to_supports_stopped_by_hw_breakpoint = remote_supports_stopped_by_hw_breakpoint;
3c3bea1c
GS
11742 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
11743 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
11744 remote_ops.to_watchpoint_addr_within_range =
11745 remote_watchpoint_addr_within_range;
3c3bea1c
GS
11746 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
11747 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
11748 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
11749 remote_ops.to_region_ok_for_hw_watchpoint
11750 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
11751 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
11752 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b 11753 remote_ops.to_kill = remote_kill;
9dd130a0 11754 remote_ops.to_load = remote_load;
c906108c 11755 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 11756 remote_ops.to_pass_signals = remote_pass_signals;
9b224c5e 11757 remote_ops.to_program_signals = remote_program_signals;
c906108c 11758 remote_ops.to_thread_alive = remote_thread_alive;
e8032dde 11759 remote_ops.to_update_thread_list = remote_update_thread_list;
0caabb7e 11760 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 11761 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 11762 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 11763 remote_ops.to_stop = remote_stop;
4b8a223f 11764 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 11765 remote_ops.to_rcmd = remote_rcmd;
c78fa86a 11766 remote_ops.to_pid_to_exec_file = remote_pid_to_exec_file;
49d03eab 11767 remote_ops.to_log_command = serial_log_command;
38691318 11768 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 11769 remote_ops.to_stratum = process_stratum;
c35b1492
PA
11770 remote_ops.to_has_all_memory = default_child_has_all_memory;
11771 remote_ops.to_has_memory = default_child_has_memory;
11772 remote_ops.to_has_stack = default_child_has_stack;
11773 remote_ops.to_has_registers = default_child_has_registers;
11774 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 11775 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 11776 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 11777 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 11778 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
11779 remote_ops.to_flash_erase = remote_flash_erase;
11780 remote_ops.to_flash_done = remote_flash_done;
29709017 11781 remote_ops.to_read_description = remote_read_description;
08388c79 11782 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
11783 remote_ops.to_can_async_p = remote_can_async_p;
11784 remote_ops.to_is_async_p = remote_is_async_p;
11785 remote_ops.to_async = remote_async;
75c99385
PA
11786 remote_ops.to_terminal_inferior = remote_terminal_inferior;
11787 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 11788 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 11789 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
11790 remote_ops.to_supports_disable_randomization
11791 = remote_supports_disable_randomization;
4bd7dc42 11792 remote_ops.to_filesystem_is_local = remote_filesystem_is_local;
7313baad
UW
11793 remote_ops.to_fileio_open = remote_hostio_open;
11794 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
11795 remote_ops.to_fileio_pread = remote_hostio_pread;
9b15c1f0 11796 remote_ops.to_fileio_fstat = remote_hostio_fstat;
7313baad
UW
11797 remote_ops.to_fileio_close = remote_hostio_close;
11798 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 11799 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 11800 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 11801 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 11802 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
d3ce09f5 11803 remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
35b1e5cc
SS
11804 remote_ops.to_trace_init = remote_trace_init;
11805 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 11806 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
11807 remote_ops.to_download_trace_state_variable
11808 = remote_download_trace_state_variable;
d248b706
KY
11809 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
11810 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
11811 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
11812 remote_ops.to_trace_start = remote_trace_start;
11813 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 11814 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
11815 remote_ops.to_trace_stop = remote_trace_stop;
11816 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
11817 remote_ops.to_get_trace_state_variable_value
11818 = remote_get_trace_state_variable_value;
00bf0b85
SS
11819 remote_ops.to_save_trace_data = remote_save_trace_data;
11820 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
11821 remote_ops.to_upload_trace_state_variables
11822 = remote_upload_trace_state_variables;
00bf0b85 11823 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 11824 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 11825 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 11826 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f6f899bf 11827 remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
f196051f 11828 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 11829 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 11830 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 11831 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 11832 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
11833 remote_ops.to_static_tracepoint_marker_at
11834 = remote_static_tracepoint_marker_at;
11835 remote_ops.to_static_tracepoint_markers_by_strid
11836 = remote_static_tracepoint_markers_by_strid;
b3b9301e 11837 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
11838 remote_ops.to_use_agent = remote_use_agent;
11839 remote_ops.to_can_use_agent = remote_can_use_agent;
9accd112
MM
11840 remote_ops.to_supports_btrace = remote_supports_btrace;
11841 remote_ops.to_enable_btrace = remote_enable_btrace;
11842 remote_ops.to_disable_btrace = remote_disable_btrace;
11843 remote_ops.to_teardown_btrace = remote_teardown_btrace;
11844 remote_ops.to_read_btrace = remote_read_btrace;
f4abbc16 11845 remote_ops.to_btrace_conf = remote_btrace_conf;
ced63ec0
GB
11846 remote_ops.to_augmented_libraries_svr4_read =
11847 remote_augmented_libraries_svr4_read;
c906108c
SS
11848}
11849
11850/* Set up the extended remote vector by making a copy of the standard
11851 remote vector and adding to it. */
11852
11853static void
fba45db2 11854init_extended_remote_ops (void)
c906108c
SS
11855{
11856 extended_remote_ops = remote_ops;
11857
0f71a2f6 11858 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 11859 extended_remote_ops.to_longname =
c906108c 11860 "Extended remote serial target in gdb-specific protocol";
c5aa993b 11861 extended_remote_ops.to_doc =
c906108c 11862 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
11863Specify the serial device it is connected to (e.g. /dev/ttya).";
11864 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
11865 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
11866 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2d717e4f
DJ
11867 extended_remote_ops.to_detach = extended_remote_detach;
11868 extended_remote_ops.to_attach = extended_remote_attach;
b9c1d481 11869 extended_remote_ops.to_post_attach = extended_remote_post_attach;
82f73884 11870 extended_remote_ops.to_kill = extended_remote_kill;
03583c20
UW
11871 extended_remote_ops.to_supports_disable_randomization
11872 = extended_remote_supports_disable_randomization;
0f71a2f6
JM
11873}
11874
6426a772 11875static int
6a109b6b 11876remote_can_async_p (struct target_ops *ops)
6426a772 11877{
5d93a237
TT
11878 struct remote_state *rs = get_remote_state ();
11879
c6ebd6cf 11880 if (!target_async_permitted)
75c99385
PA
11881 /* We only enable async when the user specifically asks for it. */
11882 return 0;
11883
23860348 11884 /* We're async whenever the serial device is. */
5d93a237 11885 return serial_can_async_p (rs->remote_desc);
6426a772
JM
11886}
11887
11888static int
6a109b6b 11889remote_is_async_p (struct target_ops *ops)
6426a772 11890{
5d93a237
TT
11891 struct remote_state *rs = get_remote_state ();
11892
c6ebd6cf 11893 if (!target_async_permitted)
75c99385
PA
11894 /* We only enable async when the user specifically asks for it. */
11895 return 0;
11896
23860348 11897 /* We're async whenever the serial device is. */
5d93a237 11898 return serial_is_async_p (rs->remote_desc);
6426a772
JM
11899}
11900
2acceee2
JM
11901/* Pass the SERIAL event on and up to the client. One day this code
11902 will be able to delay notifying the client of an event until the
23860348 11903 point where an entire packet has been received. */
2acceee2 11904
2acceee2
JM
11905static serial_event_ftype remote_async_serial_handler;
11906
6426a772 11907static void
819cc324 11908remote_async_serial_handler (struct serial *scb, void *context)
6426a772 11909{
88b496c3
TT
11910 struct remote_state *rs = context;
11911
2acceee2
JM
11912 /* Don't propogate error information up to the client. Instead let
11913 the client find out about the error by querying the target. */
6a3753b3 11914 inferior_event_handler (INF_REG_EVENT, NULL);
2acceee2
JM
11915}
11916
74531fed
PA
11917static void
11918remote_async_inferior_event_handler (gdb_client_data data)
11919{
11920 inferior_event_handler (INF_REG_EVENT, NULL);
11921}
11922
2acceee2 11923static void
6a3753b3 11924remote_async (struct target_ops *ops, int enable)
2acceee2 11925{
5d93a237
TT
11926 struct remote_state *rs = get_remote_state ();
11927
6a3753b3 11928 if (enable)
2acceee2 11929 {
88b496c3 11930 serial_async (rs->remote_desc, remote_async_serial_handler, rs);
b7d2e916
PA
11931
11932 /* If there are pending events in the stop reply queue tell the
11933 event loop to process them. */
11934 if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
11935 mark_async_event_handler (remote_async_inferior_event_token);
2acceee2
JM
11936 }
11937 else
b7d2e916
PA
11938 {
11939 serial_async (rs->remote_desc, NULL, NULL);
11940 clear_async_event_handler (remote_async_inferior_event_token);
11941 }
6426a772
JM
11942}
11943
5a2468f5 11944static void
c2d11a7d 11945set_remote_cmd (char *args, int from_tty)
5a2468f5 11946{
635c7e8a 11947 help_list (remote_set_cmdlist, "set remote ", all_commands, gdb_stdout);
5a2468f5
JM
11948}
11949
d471ea57
AC
11950static void
11951show_remote_cmd (char *args, int from_tty)
11952{
37a105a1 11953 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 11954 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1
DJ
11955 struct cleanup *showlist_chain;
11956 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 11957 struct ui_out *uiout = current_uiout;
37a105a1
DJ
11958
11959 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
11960 for (; list != NULL; list = list->next)
11961 if (strcmp (list->name, "Z-packet") == 0)
11962 continue;
427c3a89
DJ
11963 else if (list->type == not_set_cmd)
11964 /* Alias commands are exactly like the original, except they
11965 don't have the normal type. */
11966 continue;
11967 else
37a105a1
DJ
11968 {
11969 struct cleanup *option_chain
11970 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
a744cf53 11971
37a105a1
DJ
11972 ui_out_field_string (uiout, "name", list->name);
11973 ui_out_text (uiout, ": ");
427c3a89 11974 if (list->type == show_cmd)
5b9afe8a 11975 do_show_command ((char *) NULL, from_tty, list);
427c3a89
DJ
11976 else
11977 cmd_func (list, NULL, from_tty);
37a105a1
DJ
11978 /* Close the tuple. */
11979 do_cleanups (option_chain);
11980 }
427c3a89
DJ
11981
11982 /* Close the tuple. */
11983 do_cleanups (showlist_chain);
d471ea57 11984}
5a2468f5 11985
0f71a2f6 11986
23860348 11987/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
11988static void
11989remote_new_objfile (struct objfile *objfile)
11990{
5d93a237
TT
11991 struct remote_state *rs = get_remote_state ();
11992
11993 if (rs->remote_desc != 0) /* Have a remote connection. */
36d25514 11994 remote_check_symbols ();
dc8acb97
MS
11995}
11996
00bf0b85
SS
11997/* Pull all the tracepoints defined on the target and create local
11998 data structures representing them. We don't want to create real
11999 tracepoints yet, we don't want to mess up the user's existing
12000 collection. */
12001
12002static int
ab6617cc 12003remote_upload_tracepoints (struct target_ops *self, struct uploaded_tp **utpp)
d5551862 12004{
00bf0b85
SS
12005 struct remote_state *rs = get_remote_state ();
12006 char *p;
d5551862 12007
00bf0b85
SS
12008 /* Ask for a first packet of tracepoint definition. */
12009 putpkt ("qTfP");
12010 getpkt (&rs->buf, &rs->buf_size, 0);
12011 p = rs->buf;
12012 while (*p && *p != 'l')
d5551862 12013 {
00bf0b85
SS
12014 parse_tracepoint_definition (p, utpp);
12015 /* Ask for another packet of tracepoint definition. */
12016 putpkt ("qTsP");
12017 getpkt (&rs->buf, &rs->buf_size, 0);
12018 p = rs->buf;
d5551862 12019 }
00bf0b85 12020 return 0;
d5551862
SS
12021}
12022
00bf0b85 12023static int
181e3713
TT
12024remote_upload_trace_state_variables (struct target_ops *self,
12025 struct uploaded_tsv **utsvp)
d5551862 12026{
00bf0b85 12027 struct remote_state *rs = get_remote_state ();
d5551862 12028 char *p;
d5551862 12029
00bf0b85
SS
12030 /* Ask for a first packet of variable definition. */
12031 putpkt ("qTfV");
d5551862
SS
12032 getpkt (&rs->buf, &rs->buf_size, 0);
12033 p = rs->buf;
00bf0b85 12034 while (*p && *p != 'l')
d5551862 12035 {
00bf0b85
SS
12036 parse_tsv_definition (p, utsvp);
12037 /* Ask for another packet of variable definition. */
12038 putpkt ("qTsV");
d5551862
SS
12039 getpkt (&rs->buf, &rs->buf_size, 0);
12040 p = rs->buf;
12041 }
00bf0b85 12042 return 0;
d5551862
SS
12043}
12044
c1e36e3e
PA
12045/* The "set/show range-stepping" show hook. */
12046
12047static void
12048show_range_stepping (struct ui_file *file, int from_tty,
12049 struct cmd_list_element *c,
12050 const char *value)
12051{
12052 fprintf_filtered (file,
12053 _("Debugger's willingness to use range stepping "
12054 "is %s.\n"), value);
12055}
12056
12057/* The "set/show range-stepping" set hook. */
12058
12059static void
12060set_range_stepping (char *ignore_args, int from_tty,
12061 struct cmd_list_element *c)
12062{
5d93a237
TT
12063 struct remote_state *rs = get_remote_state ();
12064
c1e36e3e
PA
12065 /* Whene enabling, check whether range stepping is actually
12066 supported by the target, and warn if not. */
12067 if (use_range_stepping)
12068 {
5d93a237 12069 if (rs->remote_desc != NULL)
c1e36e3e 12070 {
4082afcc 12071 if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
c1e36e3e
PA
12072 remote_vcont_probe (rs);
12073
4082afcc 12074 if (packet_support (PACKET_vCont) == PACKET_ENABLE
c1e36e3e
PA
12075 && rs->supports_vCont.r)
12076 return;
12077 }
12078
12079 warning (_("Range stepping is not supported by the current target"));
12080 }
12081}
12082
c906108c 12083void
fba45db2 12084_initialize_remote (void)
c906108c 12085{
ea9c271d 12086 struct remote_state *rs;
9a7071a8 12087 struct cmd_list_element *cmd;
6f937416 12088 const char *cmd_name;
ea9c271d 12089
0f71a2f6 12090 /* architecture specific data */
2bc416ba 12091 remote_gdbarch_data_handle =
23860348 12092 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
12093 remote_g_packet_data_handle =
12094 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 12095
ea9c271d
DJ
12096 /* Initialize the per-target state. At the moment there is only one
12097 of these, not one per target. Only one target is active at a
cf792862
TT
12098 time. */
12099 remote_state = new_remote_state ();
ea9c271d 12100
c906108c
SS
12101 init_remote_ops ();
12102 add_target (&remote_ops);
12103
12104 init_extended_remote_ops ();
12105 add_target (&extended_remote_ops);
cce74817 12106
dc8acb97 12107 /* Hook into new objfile notification. */
06d3b283 12108 observer_attach_new_objfile (remote_new_objfile);
5f4cf0bb
YQ
12109 /* We're no longer interested in notification events of an inferior
12110 when it exits. */
12111 observer_attach_inferior_exit (discard_pending_stop_replies);
dc8acb97 12112
b803fb0f 12113 /* Set up signal handlers. */
934b9bac 12114 async_sigint_remote_token =
b803fb0f 12115 create_async_signal_handler (async_remote_interrupt, NULL);
934b9bac 12116 async_sigint_remote_twice_token =
6d549500 12117 create_async_signal_handler (async_remote_interrupt_twice, NULL);
b803fb0f 12118
c906108c
SS
12119#if 0
12120 init_remote_threadtests ();
12121#endif
12122
722247f1 12123 stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
23860348 12124 /* set/show remote ... */
d471ea57 12125
1bedd215 12126 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
12127Remote protocol specific variables\n\
12128Configure various remote-protocol specific variables such as\n\
1bedd215 12129the packets being used"),
cff3e48b 12130 &remote_set_cmdlist, "set remote ",
23860348 12131 0 /* allow-unknown */, &setlist);
1bedd215 12132 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
12133Remote protocol specific variables\n\
12134Configure various remote-protocol specific variables such as\n\
1bedd215 12135the packets being used"),
cff3e48b 12136 &remote_show_cmdlist, "show remote ",
23860348 12137 0 /* allow-unknown */, &showlist);
5a2468f5 12138
1a966eab
AC
12139 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
12140Compare section data on target to the exec file.\n\
95cf3b38
DT
12141Argument is a single section name (default: all loaded sections).\n\
12142To compare only read-only loaded sections, specify the -r option."),
c906108c
SS
12143 &cmdlist);
12144
1a966eab
AC
12145 add_cmd ("packet", class_maintenance, packet_command, _("\
12146Send an arbitrary packet to a remote target.\n\
c906108c
SS
12147 maintenance packet TEXT\n\
12148If GDB is talking to an inferior via the GDB serial protocol, then\n\
12149this command sends the string TEXT to the inferior, and displays the\n\
12150response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 12151terminating `#' character and checksum."),
c906108c
SS
12152 &maintenancelist);
12153
7915a72c
AC
12154 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
12155Set whether to send break if interrupted."), _("\
12156Show whether to send break if interrupted."), _("\
12157If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 12158 set_remotebreak, show_remotebreak,
e707bbc2 12159 &setlist, &showlist);
9a7071a8
JB
12160 cmd_name = "remotebreak";
12161 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
12162 deprecate_cmd (cmd, "set remote interrupt-sequence");
12163 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
12164 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
12165 deprecate_cmd (cmd, "show remote interrupt-sequence");
12166
12167 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
12168 interrupt_sequence_modes, &interrupt_sequence_mode,
12169 _("\
9a7071a8
JB
12170Set interrupt sequence to remote target."), _("\
12171Show interrupt sequence to remote target."), _("\
12172Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
12173 NULL, show_interrupt_sequence,
12174 &remote_set_cmdlist,
12175 &remote_show_cmdlist);
12176
12177 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
12178 &interrupt_on_connect, _("\
12179Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12180Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
12181If set, interrupt sequence is sent to remote target."),
12182 NULL, NULL,
12183 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 12184
23860348 12185 /* Install commands for configuring memory read/write packets. */
11cf8741 12186
1a966eab
AC
12187 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
12188Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 12189 &setlist);
1a966eab
AC
12190 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
12191Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
12192 &showlist);
12193 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
12194 set_memory_write_packet_size, _("\
12195Set the maximum number of bytes per memory-write packet.\n\
12196Specify the number of bytes in a packet or 0 (zero) for the\n\
12197default packet size. The actual limit is further reduced\n\
12198dependent on the target. Specify ``fixed'' to disable the\n\
12199further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12200 &remote_set_cmdlist);
12201 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
12202 set_memory_read_packet_size, _("\
12203Set the maximum number of bytes per memory-read packet.\n\
12204Specify the number of bytes in a packet or 0 (zero) for the\n\
12205default packet size. The actual limit is further reduced\n\
12206dependent on the target. Specify ``fixed'' to disable the\n\
12207further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
12208 &remote_set_cmdlist);
12209 add_cmd ("memory-write-packet-size", no_class,
12210 show_memory_write_packet_size,
1a966eab 12211 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
12212 &remote_show_cmdlist);
12213 add_cmd ("memory-read-packet-size", no_class,
12214 show_memory_read_packet_size,
1a966eab 12215 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 12216 &remote_show_cmdlist);
c906108c 12217
b3f42336 12218 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
12219 &remote_hw_watchpoint_limit, _("\
12220Set the maximum number of target hardware watchpoints."), _("\
12221Show the maximum number of target hardware watchpoints."), _("\
12222Specify a negative limit for unlimited."),
3e43a32a
MS
12223 NULL, NULL, /* FIXME: i18n: The maximum
12224 number of target hardware
12225 watchpoints is %s. */
b3f42336 12226 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
12227 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
12228 &remote_hw_watchpoint_length_limit, _("\
12229Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
12230Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
12231Specify a negative limit for unlimited."),
12232 NULL, NULL, /* FIXME: i18n: The maximum
12233 length (in bytes) of a target
12234 hardware watchpoint is %s. */
12235 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 12236 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
12237 &remote_hw_breakpoint_limit, _("\
12238Set the maximum number of target hardware breakpoints."), _("\
12239Show the maximum number of target hardware breakpoints."), _("\
12240Specify a negative limit for unlimited."),
3e43a32a
MS
12241 NULL, NULL, /* FIXME: i18n: The maximum
12242 number of target hardware
12243 breakpoints is %s. */
b3f42336 12244 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 12245
1b493192
PA
12246 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
12247 &remote_address_size, _("\
4d28ad1e
AC
12248Set the maximum size of the address (in bits) in a memory packet."), _("\
12249Show the maximum size of the address (in bits) in a memory packet."), NULL,
1b493192
PA
12250 NULL,
12251 NULL, /* FIXME: i18n: */
12252 &setlist, &showlist);
c906108c 12253
ca4f7f8b
PA
12254 init_all_packet_configs ();
12255
444abaca 12256 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 12257 "X", "binary-download", 1);
0f71a2f6 12258
444abaca 12259 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 12260 "vCont", "verbose-resume", 0);
506fb367 12261
89be2091
DJ
12262 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
12263 "QPassSignals", "pass-signals", 0);
12264
9b224c5e
PA
12265 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
12266 "QProgramSignals", "program-signals", 0);
12267
444abaca 12268 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 12269 "qSymbol", "symbol-lookup", 0);
dc8acb97 12270
444abaca 12271 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 12272 "P", "set-register", 1);
d471ea57 12273
444abaca 12274 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 12275 "p", "fetch-register", 1);
b96ec7ac 12276
444abaca 12277 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 12278 "Z0", "software-breakpoint", 0);
d471ea57 12279
444abaca 12280 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 12281 "Z1", "hardware-breakpoint", 0);
d471ea57 12282
444abaca 12283 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 12284 "Z2", "write-watchpoint", 0);
d471ea57 12285
444abaca 12286 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 12287 "Z3", "read-watchpoint", 0);
d471ea57 12288
444abaca 12289 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 12290 "Z4", "access-watchpoint", 0);
d471ea57 12291
0876f84a
DJ
12292 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
12293 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 12294
c78fa86a
GB
12295 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
12296 "qXfer:exec-file:read", "pid-to-exec-file", 0);
12297
23181151
DJ
12298 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
12299 "qXfer:features:read", "target-features", 0);
12300
cfa9d6d9
DJ
12301 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
12302 "qXfer:libraries:read", "library-info", 0);
12303
2268b414
JK
12304 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
12305 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
12306
fd79ecee
DJ
12307 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
12308 "qXfer:memory-map:read", "memory-map", 0);
12309
0e7f50da
UW
12310 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
12311 "qXfer:spu:read", "read-spu-object", 0);
12312
12313 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
12314 "qXfer:spu:write", "write-spu-object", 0);
12315
07e059b5
VP
12316 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
12317 "qXfer:osdata:read", "osdata", 0);
12318
dc146f7c
VP
12319 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
12320 "qXfer:threads:read", "threads", 0);
12321
4aa995e1
PA
12322 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
12323 "qXfer:siginfo:read", "read-siginfo-object", 0);
12324
12325 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
12326 "qXfer:siginfo:write", "write-siginfo-object", 0);
12327
b3b9301e
PA
12328 add_packet_config_cmd
12329 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
eb9fe518 12330 "qXfer:traceframe-info:read", "traceframe-info", 0);
b3b9301e 12331
169081d0
TG
12332 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
12333 "qXfer:uib:read", "unwind-info-block", 0);
12334
444abaca 12335 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 12336 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
12337 0);
12338
711e434b
PM
12339 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
12340 "qGetTIBAddr", "get-thread-information-block-address",
12341 0);
12342
40ab02ce
MS
12343 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
12344 "bc", "reverse-continue", 0);
12345
12346 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
12347 "bs", "reverse-step", 0);
12348
be2a5f71
DJ
12349 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
12350 "qSupported", "supported-packets", 0);
12351
08388c79
DE
12352 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
12353 "qSearch:memory", "search-memory", 0);
12354
bd3eecc3
PA
12355 add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
12356 "qTStatus", "trace-status", 0);
12357
a6b151f1
DJ
12358 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
12359 "vFile:open", "hostio-open", 0);
12360
12361 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
12362 "vFile:pread", "hostio-pread", 0);
12363
12364 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
12365 "vFile:pwrite", "hostio-pwrite", 0);
12366
12367 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
12368 "vFile:close", "hostio-close", 0);
12369
12370 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
12371 "vFile:unlink", "hostio-unlink", 0);
12372
b9e7b9c3
UW
12373 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
12374 "vFile:readlink", "hostio-readlink", 0);
12375
0a93529c
GB
12376 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
12377 "vFile:fstat", "hostio-fstat", 0);
12378
2d717e4f
DJ
12379 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
12380 "vAttach", "attach", 0);
12381
12382 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
12383 "vRun", "run", 0);
12384
a6f3e723
SL
12385 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
12386 "QStartNoAckMode", "noack", 0);
12387
82f73884
PA
12388 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
12389 "vKill", "kill", 0);
12390
0b16c5cf
PA
12391 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
12392 "qAttached", "query-attached", 0);
12393
782b2b07 12394 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
12395 "ConditionalTracepoints",
12396 "conditional-tracepoints", 0);
3788aec7
LM
12397
12398 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
12399 "ConditionalBreakpoints",
12400 "conditional-breakpoints", 0);
12401
d3ce09f5
SS
12402 add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
12403 "BreakpointCommands",
12404 "breakpoint-commands", 0);
12405
7a697b8d
SS
12406 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
12407 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 12408
409873ef
SS
12409 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
12410 "TracepointSource", "TracepointSource", 0);
12411
d914c394
SS
12412 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
12413 "QAllow", "allow", 0);
12414
0fb4aa4b
PA
12415 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
12416 "StaticTracepoints", "static-tracepoints", 0);
12417
1e4d1764
YQ
12418 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
12419 "InstallInTrace", "install-in-trace", 0);
12420
0fb4aa4b
PA
12421 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
12422 "qXfer:statictrace:read", "read-sdata-object", 0);
12423
78d85199
YQ
12424 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
12425 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12426
03583c20
UW
12427 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
12428 "QDisableRandomization", "disable-randomization", 0);
12429
d1feda86
YQ
12430 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
12431 "QAgent", "agent", 0);
12432
f6f899bf
HAQ
12433 add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
12434 "QTBuffer:size", "trace-buffer-size", 0);
12435
9accd112
MM
12436 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
12437 "Qbtrace:off", "disable-btrace", 0);
12438
12439 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
12440 "Qbtrace:bts", "enable-btrace", 0);
12441
12442 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
12443 "qXfer:btrace", "read-btrace", 0);
12444
f4abbc16
MM
12445 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
12446 "qXfer:btrace-conf", "read-btrace-conf", 0);
12447
d33501a5
MM
12448 add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
12449 "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
12450
f7e6eed5
PA
12451 add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
12452 "swbreak-feature", "swbreak-feature", 0);
12453
12454 add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
12455 "hwbreak-feature", "hwbreak-feature", 0);
12456
89245bc0
DB
12457 add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
12458 "fork-event-feature", "fork-event-feature", 0);
12459
12460 add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
12461 "vfork-event-feature", "vfork-event-feature", 0);
12462
0b736949
DB
12463 /* Assert that we've registered "set remote foo-packet" commands
12464 for all packet configs. */
ca4f7f8b
PA
12465 {
12466 int i;
12467
12468 for (i = 0; i < PACKET_MAX; i++)
12469 {
12470 /* Ideally all configs would have a command associated. Some
12471 still don't though. */
12472 int excepted;
12473
12474 switch (i)
12475 {
12476 case PACKET_QNonStop:
12477 case PACKET_multiprocess_feature:
12478 case PACKET_EnableDisableTracepoints_feature:
12479 case PACKET_tracenz_feature:
12480 case PACKET_DisconnectedTracing_feature:
12481 case PACKET_augmented_libraries_svr4_read_feature:
936d2992
PA
12482 case PACKET_qCRC:
12483 /* Additions to this list need to be well justified:
12484 pre-existing packets are OK; new packets are not. */
ca4f7f8b
PA
12485 excepted = 1;
12486 break;
12487 default:
12488 excepted = 0;
12489 break;
12490 }
12491
12492 /* This catches both forgetting to add a config command, and
12493 forgetting to remove a packet from the exception list. */
12494 gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
12495 }
12496 }
12497
37a105a1
DJ
12498 /* Keep the old ``set remote Z-packet ...'' working. Each individual
12499 Z sub-packet has its own set and show commands, but users may
12500 have sets to this variable in their .gdbinit files (or in their
12501 documentation). */
e9e68a56 12502 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
12503 &remote_Z_packet_detect, _("\
12504Set use of remote protocol `Z' packets"), _("\
12505Show use of remote protocol `Z' packets "), _("\
3b64bf98 12506When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 12507packets."),
e9e68a56 12508 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
12509 show_remote_protocol_Z_packet_cmd,
12510 /* FIXME: i18n: Use of remote protocol
12511 `Z' packets is %s. */
e9e68a56 12512 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 12513
a6b151f1
DJ
12514 add_prefix_cmd ("remote", class_files, remote_command, _("\
12515Manipulate files on the remote system\n\
12516Transfer files to and from the remote target system."),
12517 &remote_cmdlist, "remote ",
12518 0 /* allow-unknown */, &cmdlist);
12519
12520 add_cmd ("put", class_files, remote_put_command,
12521 _("Copy a local file to the remote system."),
12522 &remote_cmdlist);
12523
12524 add_cmd ("get", class_files, remote_get_command,
12525 _("Copy a remote file to the local system."),
12526 &remote_cmdlist);
12527
12528 add_cmd ("delete", class_files, remote_delete_command,
12529 _("Delete a remote file."),
12530 &remote_cmdlist);
12531
2d717e4f
DJ
12532 remote_exec_file = xstrdup ("");
12533 add_setshow_string_noescape_cmd ("exec-file", class_files,
12534 &remote_exec_file, _("\
12535Set the remote pathname for \"run\""), _("\
12536Show the remote pathname for \"run\""), NULL, NULL, NULL,
12537 &remote_set_cmdlist, &remote_show_cmdlist);
12538
c1e36e3e
PA
12539 add_setshow_boolean_cmd ("range-stepping", class_run,
12540 &use_range_stepping, _("\
12541Enable or disable range stepping."), _("\
12542Show whether target-assisted range stepping is enabled."), _("\
12543If on, and the target supports it, when stepping a source line, GDB\n\
12544tells the target to step the corresponding range of addresses itself instead\n\
12545of issuing multiple single-steps. This speeds up source level\n\
12546stepping. If off, GDB always issues single-steps, even if range\n\
12547stepping is supported by the target. The default is on."),
12548 set_range_stepping,
12549 show_range_stepping,
12550 &setlist,
12551 &showlist);
12552
449092f6
CV
12553 /* Eventually initialize fileio. See fileio.c */
12554 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229 12555
ba348170 12556 /* Take advantage of the fact that the TID field is not used, to tag
79d7f229 12557 special ptids with it set to != 0. */
ba348170
PA
12558 magic_null_ptid = ptid_build (42000, -1, 1);
12559 not_sent_ptid = ptid_build (42000, -2, 1);
12560 any_thread_ptid = ptid_build (42000, 0, 1);
35b1e5cc
SS
12561
12562 target_buf_size = 2048;
12563 target_buf = xmalloc (target_buf_size);
c906108c 12564}
10760264 12565
This page took 3.645604 seconds and 4 git commands to generate.