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