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