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