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