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