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