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