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