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