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