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