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