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