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