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