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