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