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