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