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