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