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