1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
31 #include "exceptions.h"
33 /*#include "terminal.h" */
36 #include "gdb-stabs.h"
37 #include "gdbthread.h"
41 #include "gdb_assert.h"
44 #include "cli/cli-decode.h"
45 #include "cli/cli-setshow.h"
46 #include "target-descriptions.h"
51 #include "event-loop.h"
52 #include "event-top.h"
58 #include "gdbcore.h" /* for exec_bfd */
60 #include "remote-fileio.h"
61 #include "gdb/fileio.h"
64 #include "memory-map.h"
66 /* The size to align memory write packets, when practical. The protocol
67 does not guarantee any alignment, and gdb will generate short
68 writes and unaligned writes, but even as a best-effort attempt this
69 can improve bulk transfers. For instance, if a write is misaligned
70 relative to the target's data bus, the stub may need to make an extra
71 round trip fetching data from the target. This doesn't make a
72 huge difference, but it's easy to do, so we try to be helpful.
74 The alignment chosen is arbitrary; usually data bus width is
75 important here, not the possibly larger cache line size. */
76 enum { REMOTE_ALIGN_WRITES
= 16 };
78 /* Prototypes for local functions. */
79 static void cleanup_sigint_signal_handler (void *dummy
);
80 static void initialize_sigint_signal_handler (void);
81 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
82 static int getpkt_or_notif_sane (char **buf
, long *sizeof_buf
,
85 static void handle_remote_sigint (int);
86 static void handle_remote_sigint_twice (int);
87 static void async_remote_interrupt (gdb_client_data
);
88 void async_remote_interrupt_twice (gdb_client_data
);
90 static void remote_files_info (struct target_ops
*ignore
);
92 static void remote_prepare_to_store (struct regcache
*regcache
);
94 static void remote_fetch_registers (struct regcache
*regcache
, int regno
);
96 static void remote_resume (ptid_t ptid
, int step
,
97 enum target_signal siggnal
);
98 static void remote_open (char *name
, int from_tty
);
100 static void extended_remote_open (char *name
, int from_tty
);
102 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
);
104 static void remote_close (int quitting
);
106 static void remote_store_registers (struct regcache
*regcache
, int regno
);
108 static void remote_mourn (struct target_ops
*ops
);
110 static void extended_remote_restart (void);
112 static void extended_remote_mourn (struct target_ops
*);
114 static void remote_mourn_1 (struct target_ops
*);
116 static void remote_send (char **buf
, long *sizeof_buf_p
);
118 static int readchar (int timeout
);
120 static void remote_kill (void);
122 static int tohex (int nib
);
124 static int remote_can_async_p (void);
126 static int remote_is_async_p (void);
128 static void remote_async (void (*callback
) (enum inferior_event_type event_type
,
129 void *context
), void *context
);
131 static int remote_async_mask (int new_mask
);
133 static void remote_detach (struct target_ops
*ops
, char *args
, int from_tty
);
135 static void remote_interrupt (int signo
);
137 static void remote_interrupt_twice (int signo
);
139 static void interrupt_query (void);
141 static void set_general_thread (struct ptid ptid
);
142 static void set_continue_thread (struct ptid ptid
);
144 static int remote_thread_alive (ptid_t
);
146 static void get_offsets (void);
148 static void skip_frame (void);
150 static long read_frame (char **buf_p
, long *sizeof_buf
);
152 static int hexnumlen (ULONGEST num
);
154 static void init_remote_ops (void);
156 static void init_extended_remote_ops (void);
158 static void remote_stop (ptid_t
);
160 static int ishex (int ch
, int *val
);
162 static int stubhex (int ch
);
164 static int hexnumstr (char *, ULONGEST
);
166 static int hexnumnstr (char *, ULONGEST
, int);
168 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
170 static void print_packet (char *);
172 static unsigned long crc32 (unsigned char *, int, unsigned int);
174 static void compare_sections_command (char *, int);
176 static void packet_command (char *, int);
178 static int stub_unpack_int (char *buff
, int fieldlength
);
180 static ptid_t
remote_current_thread (ptid_t oldptid
);
182 static void remote_find_new_threads (void);
184 static void record_currthread (ptid_t currthread
);
186 static int fromhex (int a
);
188 static int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
190 static int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
192 static int putpkt_binary (char *buf
, int cnt
);
194 static void check_binary_download (CORE_ADDR addr
);
196 struct packet_config
;
198 static void show_packet_config_cmd (struct packet_config
*config
);
200 static void update_packet_config (struct packet_config
*config
);
202 static void set_remote_protocol_packet_cmd (char *args
, int from_tty
,
203 struct cmd_list_element
*c
);
205 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
207 struct cmd_list_element
*c
,
210 static char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
211 static ptid_t
read_ptid (char *buf
, char **obuf
);
213 static void remote_query_supported (void);
215 static void remote_check_symbols (struct objfile
*objfile
);
217 void _initialize_remote (void);
220 static struct stop_reply
*stop_reply_xmalloc (void);
221 static void stop_reply_xfree (struct stop_reply
*);
222 static void do_stop_reply_xfree (void *arg
);
223 static void remote_parse_stop_reply (char *buf
, struct stop_reply
*);
224 static void push_stop_reply (struct stop_reply
*);
225 static void remote_get_pending_stop_replies (void);
226 static void discard_pending_stop_replies (int pid
);
227 static int peek_stop_reply (ptid_t ptid
);
229 static void remote_async_inferior_event_handler (gdb_client_data
);
230 static void remote_async_get_pending_events_handler (gdb_client_data
);
232 static void remote_terminal_ours (void);
234 static int remote_read_description_p (struct target_ops
*target
);
236 /* The non-stop remote protocol provisions for one pending stop reply.
237 This is where we keep it until it is acknowledged. */
239 static struct stop_reply
*pending_stop_reply
= NULL
;
243 static struct cmd_list_element
*remote_cmdlist
;
245 /* For "set remote" and "show remote". */
247 static struct cmd_list_element
*remote_set_cmdlist
;
248 static struct cmd_list_element
*remote_show_cmdlist
;
250 /* Description of the remote protocol state for the currently
251 connected target. This is per-target state, and independent of the
252 selected architecture. */
256 /* A buffer to use for incoming packets, and its current size. The
257 buffer is grown dynamically for larger incoming packets.
258 Outgoing packets may also be constructed in this buffer.
259 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
260 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
265 /* If we negotiated packet size explicitly (and thus can bypass
266 heuristics for the largest packet size that will not overflow
267 a buffer in the stub), this will be set to that packet size.
268 Otherwise zero, meaning to use the guessed size. */
269 long explicit_packet_size
;
271 /* remote_wait is normally called when the target is running and
272 waits for a stop reply packet. But sometimes we need to call it
273 when the target is already stopped. We can send a "?" packet
274 and have remote_wait read the response. Or, if we already have
275 the response, we can stash it in BUF and tell remote_wait to
276 skip calling getpkt. This flag is set when BUF contains a
277 stop reply packet and the target is not waiting. */
278 int cached_wait_status
;
280 /* True, if in no ack mode. That is, neither GDB nor the stub will
281 expect acks from each other. The connection is assumed to be
285 /* True if we're connected in extended remote mode. */
288 /* True if the stub reported support for multi-process
290 int multi_process_aware
;
292 /* True if we resumed the target and we're waiting for the target to
293 stop. In the mean time, we can't start another command/query.
294 The remote server wouldn't be ready to process it, so we'd
295 timeout waiting for a reply that would never come and eventually
296 we'd close the connection. This can happen in asynchronous mode
297 because we allow GDB commands while the target is running. */
298 int waiting_for_stop_reply
;
300 /* True if the stub reports support for non-stop mode. */
303 /* True if the stub reports support for vCont;t. */
307 /* Returns true if the multi-process extensions are in effect. */
309 remote_multi_process_p (struct remote_state
*rs
)
311 return rs
->extended
&& rs
->multi_process_aware
;
314 /* This data could be associated with a target, but we do not always
315 have access to the current target when we need it, so for now it is
316 static. This will be fine for as long as only one target is in use
318 static struct remote_state remote_state
;
320 static struct remote_state
*
321 get_remote_state_raw (void)
323 return &remote_state
;
326 /* Description of the remote protocol for a given architecture. */
330 long offset
; /* Offset into G packet. */
331 long regnum
; /* GDB's internal register number. */
332 LONGEST pnum
; /* Remote protocol register number. */
333 int in_g_packet
; /* Always part of G packet. */
334 /* long size in bytes; == register_size (target_gdbarch, regnum);
336 /* char *name; == gdbarch_register_name (target_gdbarch, regnum);
340 struct remote_arch_state
342 /* Description of the remote protocol registers. */
343 long sizeof_g_packet
;
345 /* Description of the remote protocol registers indexed by REGNUM
346 (making an array gdbarch_num_regs in size). */
347 struct packet_reg
*regs
;
349 /* This is the size (in chars) of the first response to the ``g''
350 packet. It is used as a heuristic when determining the maximum
351 size of memory-read and memory-write packets. A target will
352 typically only reserve a buffer large enough to hold the ``g''
353 packet. The size does not include packet overhead (headers and
355 long actual_register_packet_size
;
357 /* This is the maximum size (in chars) of a non read/write packet.
358 It is also used as a cap on the size of read/write packets. */
359 long remote_packet_size
;
363 /* Handle for retreving the remote protocol data from gdbarch. */
364 static struct gdbarch_data
*remote_gdbarch_data_handle
;
366 static struct remote_arch_state
*
367 get_remote_arch_state (void)
369 return gdbarch_data (target_gdbarch
, remote_gdbarch_data_handle
);
372 /* Fetch the global remote target state. */
374 static struct remote_state
*
375 get_remote_state (void)
377 /* Make sure that the remote architecture state has been
378 initialized, because doing so might reallocate rs->buf. Any
379 function which calls getpkt also needs to be mindful of changes
380 to rs->buf, but this call limits the number of places which run
382 get_remote_arch_state ();
384 return get_remote_state_raw ();
388 compare_pnums (const void *lhs_
, const void *rhs_
)
390 const struct packet_reg
* const *lhs
= lhs_
;
391 const struct packet_reg
* const *rhs
= rhs_
;
393 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
395 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
402 init_remote_state (struct gdbarch
*gdbarch
)
404 int regnum
, num_remote_regs
, offset
;
405 struct remote_state
*rs
= get_remote_state_raw ();
406 struct remote_arch_state
*rsa
;
407 struct packet_reg
**remote_regs
;
409 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
411 /* Use the architecture to build a regnum<->pnum table, which will be
412 1:1 unless a feature set specifies otherwise. */
413 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
,
414 gdbarch_num_regs (gdbarch
),
416 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
418 struct packet_reg
*r
= &rsa
->regs
[regnum
];
420 if (register_size (gdbarch
, regnum
) == 0)
421 /* Do not try to fetch zero-sized (placeholder) registers. */
424 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
429 /* Define the g/G packet format as the contents of each register
430 with a remote protocol number, in order of ascending protocol
433 remote_regs
= alloca (gdbarch_num_regs (gdbarch
)
434 * sizeof (struct packet_reg
*));
435 for (num_remote_regs
= 0, regnum
= 0;
436 regnum
< gdbarch_num_regs (gdbarch
);
438 if (rsa
->regs
[regnum
].pnum
!= -1)
439 remote_regs
[num_remote_regs
++] = &rsa
->regs
[regnum
];
441 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
444 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
446 remote_regs
[regnum
]->in_g_packet
= 1;
447 remote_regs
[regnum
]->offset
= offset
;
448 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
451 /* Record the maximum possible size of the g packet - it may turn out
453 rsa
->sizeof_g_packet
= offset
;
455 /* Default maximum number of characters in a packet body. Many
456 remote stubs have a hardwired buffer size of 400 bytes
457 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
458 as the maximum packet-size to ensure that the packet and an extra
459 NUL character can always fit in the buffer. This stops GDB
460 trashing stubs that try to squeeze an extra NUL into what is
461 already a full buffer (As of 1999-12-04 that was most stubs). */
462 rsa
->remote_packet_size
= 400 - 1;
464 /* This one is filled in when a ``g'' packet is received. */
465 rsa
->actual_register_packet_size
= 0;
467 /* Should rsa->sizeof_g_packet needs more space than the
468 default, adjust the size accordingly. Remember that each byte is
469 encoded as two characters. 32 is the overhead for the packet
470 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
471 (``$NN:G...#NN'') is a better guess, the below has been padded a
473 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
474 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
476 /* Make sure that the packet buffer is plenty big enough for
477 this architecture. */
478 if (rs
->buf_size
< rsa
->remote_packet_size
)
480 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
481 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
487 /* Return the current allowed size of a remote packet. This is
488 inferred from the current architecture, and should be used to
489 limit the length of outgoing packets. */
491 get_remote_packet_size (void)
493 struct remote_state
*rs
= get_remote_state ();
494 struct remote_arch_state
*rsa
= get_remote_arch_state ();
496 if (rs
->explicit_packet_size
)
497 return rs
->explicit_packet_size
;
499 return rsa
->remote_packet_size
;
502 static struct packet_reg
*
503 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
505 if (regnum
< 0 && regnum
>= gdbarch_num_regs (target_gdbarch
))
509 struct packet_reg
*r
= &rsa
->regs
[regnum
];
510 gdb_assert (r
->regnum
== regnum
);
515 static struct packet_reg
*
516 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
519 for (i
= 0; i
< gdbarch_num_regs (target_gdbarch
); i
++)
521 struct packet_reg
*r
= &rsa
->regs
[i
];
528 /* FIXME: graces/2002-08-08: These variables should eventually be
529 bound to an instance of the target object (as in gdbarch-tdep()),
530 when such a thing exists. */
532 /* This is set to the data address of the access causing the target
533 to stop for a watchpoint. */
534 static CORE_ADDR remote_watch_data_address
;
536 /* This is non-zero if target stopped for a watchpoint. */
537 static int remote_stopped_by_watchpoint_p
;
539 static struct target_ops remote_ops
;
541 static struct target_ops extended_remote_ops
;
543 static int remote_async_mask_value
= 1;
545 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
546 ``forever'' still use the normal timeout mechanism. This is
547 currently used by the ASYNC code to guarentee that target reads
548 during the initial connect always time-out. Once getpkt has been
549 modified to return a timeout indication and, in turn
550 remote_wait()/wait_for_inferior() have gained a timeout parameter
552 static int wait_forever_enabled_p
= 1;
555 /* This variable chooses whether to send a ^C or a break when the user
556 requests program interruption. Although ^C is usually what remote
557 systems expect, and that is the default here, sometimes a break is
558 preferable instead. */
560 static int remote_break
;
562 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
563 remote_open knows that we don't have a file open when the program
565 static struct serial
*remote_desc
= NULL
;
567 /* This variable sets the number of bits in an address that are to be
568 sent in a memory ("M" or "m") packet. Normally, after stripping
569 leading zeros, the entire address would be sent. This variable
570 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
571 initial implementation of remote.c restricted the address sent in
572 memory packets to ``host::sizeof long'' bytes - (typically 32
573 bits). Consequently, for 64 bit targets, the upper 32 bits of an
574 address was never sent. Since fixing this bug may cause a break in
575 some remote targets this variable is principly provided to
576 facilitate backward compatibility. */
578 static int remote_address_size
;
580 /* Temporary to track who currently owns the terminal. See
581 remote_terminal_* for more details. */
583 static int remote_async_terminal_ours_p
;
585 /* The executable file to use for "run" on the remote side. */
587 static char *remote_exec_file
= "";
590 /* User configurable variables for the number of characters in a
591 memory read/write packet. MIN (rsa->remote_packet_size,
592 rsa->sizeof_g_packet) is the default. Some targets need smaller
593 values (fifo overruns, et.al.) and some users need larger values
594 (speed up transfers). The variables ``preferred_*'' (the user
595 request), ``current_*'' (what was actually set) and ``forced_*''
596 (Positive - a soft limit, negative - a hard limit). */
598 struct memory_packet_config
605 /* Compute the current size of a read/write packet. Since this makes
606 use of ``actual_register_packet_size'' the computation is dynamic. */
609 get_memory_packet_size (struct memory_packet_config
*config
)
611 struct remote_state
*rs
= get_remote_state ();
612 struct remote_arch_state
*rsa
= get_remote_arch_state ();
614 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
615 law?) that some hosts don't cope very well with large alloca()
616 calls. Eventually the alloca() code will be replaced by calls to
617 xmalloc() and make_cleanups() allowing this restriction to either
618 be lifted or removed. */
619 #ifndef MAX_REMOTE_PACKET_SIZE
620 #define MAX_REMOTE_PACKET_SIZE 16384
622 /* NOTE: 20 ensures we can write at least one byte. */
623 #ifndef MIN_REMOTE_PACKET_SIZE
624 #define MIN_REMOTE_PACKET_SIZE 20
629 if (config
->size
<= 0)
630 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
632 what_they_get
= config
->size
;
636 what_they_get
= get_remote_packet_size ();
637 /* Limit the packet to the size specified by the user. */
639 && what_they_get
> config
->size
)
640 what_they_get
= config
->size
;
642 /* Limit it to the size of the targets ``g'' response unless we have
643 permission from the stub to use a larger packet size. */
644 if (rs
->explicit_packet_size
== 0
645 && rsa
->actual_register_packet_size
> 0
646 && what_they_get
> rsa
->actual_register_packet_size
)
647 what_they_get
= rsa
->actual_register_packet_size
;
649 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
650 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
651 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
652 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
654 /* Make sure there is room in the global buffer for this packet
655 (including its trailing NUL byte). */
656 if (rs
->buf_size
< what_they_get
+ 1)
658 rs
->buf_size
= 2 * what_they_get
;
659 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
662 return what_they_get
;
665 /* Update the size of a read/write packet. If they user wants
666 something really big then do a sanity check. */
669 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
671 int fixed_p
= config
->fixed_p
;
672 long size
= config
->size
;
674 error (_("Argument required (integer, `fixed' or `limited')."));
675 else if (strcmp (args
, "hard") == 0
676 || strcmp (args
, "fixed") == 0)
678 else if (strcmp (args
, "soft") == 0
679 || strcmp (args
, "limit") == 0)
684 size
= strtoul (args
, &end
, 0);
686 error (_("Invalid %s (bad syntax)."), config
->name
);
688 /* Instead of explicitly capping the size of a packet to
689 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
690 instead allowed to set the size to something arbitrarily
692 if (size
> MAX_REMOTE_PACKET_SIZE
)
693 error (_("Invalid %s (too large)."), config
->name
);
697 if (fixed_p
&& !config
->fixed_p
)
699 if (! query (_("The target may not be able to correctly handle a %s\n"
700 "of %ld bytes. Change the packet size? "),
702 error (_("Packet size not changed."));
704 /* Update the config. */
705 config
->fixed_p
= fixed_p
;
710 show_memory_packet_size (struct memory_packet_config
*config
)
712 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
714 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
715 get_memory_packet_size (config
));
717 printf_filtered (_("Packets are limited to %ld bytes.\n"),
718 get_memory_packet_size (config
));
721 static struct memory_packet_config memory_write_packet_config
=
723 "memory-write-packet-size",
727 set_memory_write_packet_size (char *args
, int from_tty
)
729 set_memory_packet_size (args
, &memory_write_packet_config
);
733 show_memory_write_packet_size (char *args
, int from_tty
)
735 show_memory_packet_size (&memory_write_packet_config
);
739 get_memory_write_packet_size (void)
741 return get_memory_packet_size (&memory_write_packet_config
);
744 static struct memory_packet_config memory_read_packet_config
=
746 "memory-read-packet-size",
750 set_memory_read_packet_size (char *args
, int from_tty
)
752 set_memory_packet_size (args
, &memory_read_packet_config
);
756 show_memory_read_packet_size (char *args
, int from_tty
)
758 show_memory_packet_size (&memory_read_packet_config
);
762 get_memory_read_packet_size (void)
764 long size
= get_memory_packet_size (&memory_read_packet_config
);
765 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
766 extra buffer size argument before the memory read size can be
767 increased beyond this. */
768 if (size
> get_remote_packet_size ())
769 size
= get_remote_packet_size ();
774 /* Generic configuration support for packets the stub optionally
775 supports. Allows the user to specify the use of the packet as well
776 as allowing GDB to auto-detect support in the remote stub. */
780 PACKET_SUPPORT_UNKNOWN
= 0,
789 enum auto_boolean detect
;
790 enum packet_support support
;
793 /* Analyze a packet's return value and update the packet config
804 update_packet_config (struct packet_config
*config
)
806 switch (config
->detect
)
808 case AUTO_BOOLEAN_TRUE
:
809 config
->support
= PACKET_ENABLE
;
811 case AUTO_BOOLEAN_FALSE
:
812 config
->support
= PACKET_DISABLE
;
814 case AUTO_BOOLEAN_AUTO
:
815 config
->support
= PACKET_SUPPORT_UNKNOWN
;
821 show_packet_config_cmd (struct packet_config
*config
)
823 char *support
= "internal-error";
824 switch (config
->support
)
830 support
= "disabled";
832 case PACKET_SUPPORT_UNKNOWN
:
836 switch (config
->detect
)
838 case AUTO_BOOLEAN_AUTO
:
839 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
840 config
->name
, support
);
842 case AUTO_BOOLEAN_TRUE
:
843 case AUTO_BOOLEAN_FALSE
:
844 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
845 config
->name
, support
);
851 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
852 const char *title
, int legacy
)
859 config
->title
= title
;
860 config
->detect
= AUTO_BOOLEAN_AUTO
;
861 config
->support
= PACKET_SUPPORT_UNKNOWN
;
862 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
864 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
866 /* set/show TITLE-packet {auto,on,off} */
867 cmd_name
= xstrprintf ("%s-packet", title
);
868 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
869 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
870 set_remote_protocol_packet_cmd
,
871 show_remote_protocol_packet_cmd
,
872 &remote_set_cmdlist
, &remote_show_cmdlist
);
873 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
877 legacy_name
= xstrprintf ("%s-packet", name
);
878 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
879 &remote_set_cmdlist
);
880 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
881 &remote_show_cmdlist
);
885 static enum packet_result
886 packet_check_result (const char *buf
)
890 /* The stub recognized the packet request. Check that the
891 operation succeeded. */
893 && isxdigit (buf
[1]) && isxdigit (buf
[2])
895 /* "Enn" - definitly an error. */
898 /* Always treat "E." as an error. This will be used for
899 more verbose error messages, such as E.memtypes. */
900 if (buf
[0] == 'E' && buf
[1] == '.')
903 /* The packet may or may not be OK. Just assume it is. */
907 /* The stub does not support the packet. */
908 return PACKET_UNKNOWN
;
911 static enum packet_result
912 packet_ok (const char *buf
, struct packet_config
*config
)
914 enum packet_result result
;
916 result
= packet_check_result (buf
);
921 /* The stub recognized the packet request. */
922 switch (config
->support
)
924 case PACKET_SUPPORT_UNKNOWN
:
926 fprintf_unfiltered (gdb_stdlog
,
927 "Packet %s (%s) is supported\n",
928 config
->name
, config
->title
);
929 config
->support
= PACKET_ENABLE
;
932 internal_error (__FILE__
, __LINE__
,
933 _("packet_ok: attempt to use a disabled packet"));
940 /* The stub does not support the packet. */
941 switch (config
->support
)
944 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
945 /* If the stub previously indicated that the packet was
946 supported then there is a protocol error.. */
947 error (_("Protocol error: %s (%s) conflicting enabled responses."),
948 config
->name
, config
->title
);
950 /* The user set it wrong. */
951 error (_("Enabled packet %s (%s) not recognized by stub"),
952 config
->name
, config
->title
);
954 case PACKET_SUPPORT_UNKNOWN
:
956 fprintf_unfiltered (gdb_stdlog
,
957 "Packet %s (%s) is NOT supported\n",
958 config
->name
, config
->title
);
959 config
->support
= PACKET_DISABLE
;
987 PACKET_qXfer_features
,
988 PACKET_qXfer_libraries
,
989 PACKET_qXfer_memory_map
,
990 PACKET_qXfer_spu_read
,
991 PACKET_qXfer_spu_write
,
996 PACKET_qSearch_memory
,
999 PACKET_QStartNoAckMode
,
1001 PACKET_qXfer_siginfo_read
,
1002 PACKET_qXfer_siginfo_write
,
1006 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
1009 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
1010 struct cmd_list_element
*c
)
1012 struct packet_config
*packet
;
1014 for (packet
= remote_protocol_packets
;
1015 packet
< &remote_protocol_packets
[PACKET_MAX
];
1018 if (&packet
->detect
== c
->var
)
1020 update_packet_config (packet
);
1024 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1029 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
1030 struct cmd_list_element
*c
,
1033 struct packet_config
*packet
;
1035 for (packet
= remote_protocol_packets
;
1036 packet
< &remote_protocol_packets
[PACKET_MAX
];
1039 if (&packet
->detect
== c
->var
)
1041 show_packet_config_cmd (packet
);
1045 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1049 /* Should we try one of the 'Z' requests? */
1053 Z_PACKET_SOFTWARE_BP
,
1054 Z_PACKET_HARDWARE_BP
,
1061 /* For compatibility with older distributions. Provide a ``set remote
1062 Z-packet ...'' command that updates all the Z packet types. */
1064 static enum auto_boolean remote_Z_packet_detect
;
1067 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1068 struct cmd_list_element
*c
)
1071 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1073 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1074 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1079 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1080 struct cmd_list_element
*c
,
1084 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1086 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1090 /* Should we try the 'ThreadInfo' query packet?
1092 This variable (NOT available to the user: auto-detect only!)
1093 determines whether GDB will use the new, simpler "ThreadInfo"
1094 query or the older, more complex syntax for thread queries.
1095 This is an auto-detect variable (set to true at each connect,
1096 and set to false when the target fails to recognize it). */
1098 static int use_threadinfo_query
;
1099 static int use_threadextra_query
;
1101 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1102 static struct async_signal_handler
*sigint_remote_twice_token
;
1103 static struct async_signal_handler
*sigint_remote_token
;
1106 /* Asynchronous signal handle registered as event loop source for
1107 when we have pending events ready to be passed to the core. */
1109 static struct async_event_handler
*remote_async_inferior_event_token
;
1111 /* Asynchronous signal handle registered as event loop source for when
1112 the remote sent us a %Stop notification. The registered callback
1113 will do a vStopped sequence to pull the rest of the events out of
1114 the remote side into our event queue. */
1116 static struct async_event_handler
*remote_async_get_pending_events_token
;
1119 static ptid_t magic_null_ptid
;
1120 static ptid_t not_sent_ptid
;
1121 static ptid_t any_thread_ptid
;
1123 /* These are the threads which we last sent to the remote system. The
1124 TID member will be -1 for all or -2 for not sent yet. */
1126 static ptid_t general_thread
;
1127 static ptid_t continue_thread
;
1130 notice_new_inferiors (ptid_t currthread
)
1132 /* If this is a new thread, add it to GDB's thread list.
1133 If we leave it up to WFI to do this, bad things will happen. */
1135 if (in_thread_list (currthread
) && is_exited (currthread
))
1137 /* We're seeing an event on a thread id we knew had exited.
1138 This has to be a new thread reusing the old id. Add it. */
1139 add_thread (currthread
);
1143 if (!in_thread_list (currthread
))
1145 if (ptid_equal (pid_to_ptid (ptid_get_pid (currthread
)), inferior_ptid
))
1147 /* inferior_ptid has no thread member yet. This can happen
1148 with the vAttach -> remote_wait,"TAAthread:" path if the
1149 stub doesn't support qC. This is the first stop reported
1150 after an attach, so this is the main thread. Update the
1151 ptid in the thread list. */
1152 thread_change_ptid (inferior_ptid
, currthread
);
1156 if (ptid_equal (magic_null_ptid
, inferior_ptid
))
1158 /* inferior_ptid is not set yet. This can happen with the
1159 vRun -> remote_wait,"TAAthread:" path if the stub
1160 doesn't support qC. This is the first stop reported
1161 after an attach, so this is the main thread. Update the
1162 ptid in the thread list. */
1163 thread_change_ptid (inferior_ptid
, currthread
);
1167 /* When connecting to a target remote, or to a target
1168 extended-remote which already was debugging an inferior, we
1169 may not know about it yet. Add it before adding its child
1170 thread, so notifications are emitted in a sensible order. */
1171 if (!in_inferior_list (ptid_get_pid (currthread
)))
1172 add_inferior (ptid_get_pid (currthread
));
1174 /* This is really a new thread. Add it. */
1175 add_thread (currthread
);
1179 /* Call this function as a result of
1180 1) A halt indication (T packet) containing a thread id
1181 2) A direct query of currthread
1182 3) Successful execution of set thread
1186 record_currthread (ptid_t currthread
)
1188 general_thread
= currthread
;
1190 if (ptid_equal (currthread
, minus_one_ptid
))
1191 /* We're just invalidating the local thread mirror. */
1194 notice_new_inferiors (currthread
);
1197 static char *last_pass_packet
;
1199 /* If 'QPassSignals' is supported, tell the remote stub what signals
1200 it can simply pass through to the inferior without reporting. */
1203 remote_pass_signals (void)
1205 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1207 char *pass_packet
, *p
;
1208 int numsigs
= (int) TARGET_SIGNAL_LAST
;
1211 gdb_assert (numsigs
< 256);
1212 for (i
= 0; i
< numsigs
; i
++)
1214 if (signal_stop_state (i
) == 0
1215 && signal_print_state (i
) == 0
1216 && signal_pass_state (i
) == 1)
1219 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1220 strcpy (pass_packet
, "QPassSignals:");
1221 p
= pass_packet
+ strlen (pass_packet
);
1222 for (i
= 0; i
< numsigs
; i
++)
1224 if (signal_stop_state (i
) == 0
1225 && signal_print_state (i
) == 0
1226 && signal_pass_state (i
) == 1)
1229 *p
++ = tohex (i
>> 4);
1230 *p
++ = tohex (i
& 15);
1239 if (!last_pass_packet
|| strcmp (last_pass_packet
, pass_packet
))
1241 struct remote_state
*rs
= get_remote_state ();
1242 char *buf
= rs
->buf
;
1244 putpkt (pass_packet
);
1245 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1246 packet_ok (buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1247 if (last_pass_packet
)
1248 xfree (last_pass_packet
);
1249 last_pass_packet
= pass_packet
;
1252 xfree (pass_packet
);
1256 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1257 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1258 thread. If GEN is set, set the general thread, if not, then set
1259 the step/continue thread. */
1261 set_thread (struct ptid ptid
, int gen
)
1263 struct remote_state
*rs
= get_remote_state ();
1264 ptid_t state
= gen
? general_thread
: continue_thread
;
1265 char *buf
= rs
->buf
;
1266 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1268 if (ptid_equal (state
, ptid
))
1272 *buf
++ = gen
? 'g' : 'c';
1273 if (ptid_equal (ptid
, magic_null_ptid
))
1274 xsnprintf (buf
, endbuf
- buf
, "0");
1275 else if (ptid_equal (ptid
, any_thread_ptid
))
1276 xsnprintf (buf
, endbuf
- buf
, "0");
1277 else if (ptid_equal (ptid
, minus_one_ptid
))
1278 xsnprintf (buf
, endbuf
- buf
, "-1");
1280 write_ptid (buf
, endbuf
, ptid
);
1282 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1284 general_thread
= ptid
;
1286 continue_thread
= ptid
;
1290 set_general_thread (struct ptid ptid
)
1292 set_thread (ptid
, 1);
1296 set_continue_thread (struct ptid ptid
)
1298 set_thread (ptid
, 0);
1301 /* Change the remote current process. Which thread within the process
1302 ends up selected isn't important, as long as it is the same process
1303 as what INFERIOR_PTID points to.
1305 This comes from that fact that there is no explicit notion of
1306 "selected process" in the protocol. The selected process for
1307 general operations is the process the selected general thread
1311 set_general_process (void)
1313 struct remote_state
*rs
= get_remote_state ();
1315 /* If the remote can't handle multiple processes, don't bother. */
1316 if (!remote_multi_process_p (rs
))
1319 /* We only need to change the remote current thread if it's pointing
1320 at some other process. */
1321 if (ptid_get_pid (general_thread
) != ptid_get_pid (inferior_ptid
))
1322 set_general_thread (inferior_ptid
);
1326 /* Return nonzero if the thread PTID is still alive on the remote
1330 remote_thread_alive (ptid_t ptid
)
1332 struct remote_state
*rs
= get_remote_state ();
1333 int tid
= ptid_get_tid (ptid
);
1336 if (ptid_equal (ptid
, magic_null_ptid
))
1337 /* The main thread is always alive. */
1340 if (ptid_get_pid (ptid
) != 0 && ptid_get_tid (ptid
) == 0)
1341 /* The main thread is always alive. This can happen after a
1342 vAttach, if the remote side doesn't support
1347 endp
= rs
->buf
+ get_remote_packet_size ();
1350 write_ptid (p
, endp
, ptid
);
1353 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1354 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1357 /* About these extended threadlist and threadinfo packets. They are
1358 variable length packets but, the fields within them are often fixed
1359 length. They are redundent enough to send over UDP as is the
1360 remote protocol in general. There is a matching unit test module
1363 #define OPAQUETHREADBYTES 8
1365 /* a 64 bit opaque identifier */
1366 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1368 /* WARNING: This threadref data structure comes from the remote O.S.,
1369 libstub protocol encoding, and remote.c. it is not particularly
1372 /* Right now, the internal structure is int. We want it to be bigger.
1376 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1378 /* gdb_ext_thread_info is an internal GDB data structure which is
1379 equivalent to the reply of the remote threadinfo packet. */
1381 struct gdb_ext_thread_info
1383 threadref threadid
; /* External form of thread reference. */
1384 int active
; /* Has state interesting to GDB?
1386 char display
[256]; /* Brief state display, name,
1387 blocked/suspended. */
1388 char shortname
[32]; /* To be used to name threads. */
1389 char more_display
[256]; /* Long info, statistics, queue depth,
1393 /* The volume of remote transfers can be limited by submitting
1394 a mask containing bits specifying the desired information.
1395 Use a union of these values as the 'selection' parameter to
1396 get_thread_info. FIXME: Make these TAG names more thread specific.
1399 #define TAG_THREADID 1
1400 #define TAG_EXISTS 2
1401 #define TAG_DISPLAY 4
1402 #define TAG_THREADNAME 8
1403 #define TAG_MOREDISPLAY 16
1405 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1407 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1409 static char *unpack_nibble (char *buf
, int *val
);
1411 static char *pack_nibble (char *buf
, int nibble
);
1413 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1415 static char *unpack_byte (char *buf
, int *value
);
1417 static char *pack_int (char *buf
, int value
);
1419 static char *unpack_int (char *buf
, int *value
);
1421 static char *unpack_string (char *src
, char *dest
, int length
);
1423 static char *pack_threadid (char *pkt
, threadref
*id
);
1425 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1427 void int_to_threadref (threadref
*id
, int value
);
1429 static int threadref_to_int (threadref
*ref
);
1431 static void copy_threadref (threadref
*dest
, threadref
*src
);
1433 static int threadmatch (threadref
*dest
, threadref
*src
);
1435 static char *pack_threadinfo_request (char *pkt
, int mode
,
1438 static int remote_unpack_thread_info_response (char *pkt
,
1439 threadref
*expectedref
,
1440 struct gdb_ext_thread_info
1444 static int remote_get_threadinfo (threadref
*threadid
,
1445 int fieldset
, /*TAG mask */
1446 struct gdb_ext_thread_info
*info
);
1448 static char *pack_threadlist_request (char *pkt
, int startflag
,
1450 threadref
*nextthread
);
1452 static int parse_threadlist_response (char *pkt
,
1454 threadref
*original_echo
,
1455 threadref
*resultlist
,
1458 static int remote_get_threadlist (int startflag
,
1459 threadref
*nextthread
,
1463 threadref
*threadlist
);
1465 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1467 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1468 void *context
, int looplimit
);
1470 static int remote_newthread_step (threadref
*ref
, void *context
);
1473 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1474 buffer we're allowed to write to. Returns
1475 BUF+CHARACTERS_WRITTEN. */
1478 write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
1481 struct remote_state
*rs
= get_remote_state ();
1483 if (remote_multi_process_p (rs
))
1485 pid
= ptid_get_pid (ptid
);
1487 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
1489 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
1491 tid
= ptid_get_tid (ptid
);
1493 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
1495 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
1500 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
1501 passed the last parsed char. Returns null_ptid on error. */
1504 read_ptid (char *buf
, char **obuf
)
1508 ULONGEST pid
= 0, tid
= 0;
1513 /* Multi-process ptid. */
1514 pp
= unpack_varlen_hex (p
+ 1, &pid
);
1516 error (_("invalid remote ptid: %s\n"), p
);
1519 pp
= unpack_varlen_hex (p
+ 1, &tid
);
1522 return ptid_build (pid
, 0, tid
);
1525 /* No multi-process. Just a tid. */
1526 pp
= unpack_varlen_hex (p
, &tid
);
1528 /* Since the stub is not sending a process id, then default to
1529 what's in inferior_ptid, unless it's null at this point. If so,
1530 then since there's no way to know the pid of the reported
1531 threads, use the magic number. */
1532 if (ptid_equal (inferior_ptid
, null_ptid
))
1533 pid
= ptid_get_pid (magic_null_ptid
);
1535 pid
= ptid_get_pid (inferior_ptid
);
1539 return ptid_build (pid
, 0, tid
);
1542 /* Encode 64 bits in 16 chars of hex. */
1544 static const char hexchars
[] = "0123456789abcdef";
1547 ishex (int ch
, int *val
)
1549 if ((ch
>= 'a') && (ch
<= 'f'))
1551 *val
= ch
- 'a' + 10;
1554 if ((ch
>= 'A') && (ch
<= 'F'))
1556 *val
= ch
- 'A' + 10;
1559 if ((ch
>= '0') && (ch
<= '9'))
1570 if (ch
>= 'a' && ch
<= 'f')
1571 return ch
- 'a' + 10;
1572 if (ch
>= '0' && ch
<= '9')
1574 if (ch
>= 'A' && ch
<= 'F')
1575 return ch
- 'A' + 10;
1580 stub_unpack_int (char *buff
, int fieldlength
)
1587 nibble
= stubhex (*buff
++);
1591 retval
= retval
<< 4;
1597 unpack_varlen_hex (char *buff
, /* packet to parse */
1601 ULONGEST retval
= 0;
1603 while (ishex (*buff
, &nibble
))
1606 retval
= retval
<< 4;
1607 retval
|= nibble
& 0x0f;
1614 unpack_nibble (char *buf
, int *val
)
1616 *val
= fromhex (*buf
++);
1621 pack_nibble (char *buf
, int nibble
)
1623 *buf
++ = hexchars
[(nibble
& 0x0f)];
1628 pack_hex_byte (char *pkt
, int byte
)
1630 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1631 *pkt
++ = hexchars
[(byte
& 0xf)];
1636 unpack_byte (char *buf
, int *value
)
1638 *value
= stub_unpack_int (buf
, 2);
1643 pack_int (char *buf
, int value
)
1645 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1646 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1647 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1648 buf
= pack_hex_byte (buf
, (value
& 0xff));
1653 unpack_int (char *buf
, int *value
)
1655 *value
= stub_unpack_int (buf
, 8);
1659 #if 0 /* Currently unused, uncomment when needed. */
1660 static char *pack_string (char *pkt
, char *string
);
1663 pack_string (char *pkt
, char *string
)
1668 len
= strlen (string
);
1670 len
= 200; /* Bigger than most GDB packets, junk??? */
1671 pkt
= pack_hex_byte (pkt
, len
);
1675 if ((ch
== '\0') || (ch
== '#'))
1676 ch
= '*'; /* Protect encapsulation. */
1681 #endif /* 0 (unused) */
1684 unpack_string (char *src
, char *dest
, int length
)
1693 pack_threadid (char *pkt
, threadref
*id
)
1696 unsigned char *altid
;
1698 altid
= (unsigned char *) id
;
1699 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1701 pkt
= pack_hex_byte (pkt
, *altid
++);
1707 unpack_threadid (char *inbuf
, threadref
*id
)
1710 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1713 altref
= (char *) id
;
1715 while (inbuf
< limit
)
1717 x
= stubhex (*inbuf
++);
1718 y
= stubhex (*inbuf
++);
1719 *altref
++ = (x
<< 4) | y
;
1724 /* Externally, threadrefs are 64 bits but internally, they are still
1725 ints. This is due to a mismatch of specifications. We would like
1726 to use 64bit thread references internally. This is an adapter
1730 int_to_threadref (threadref
*id
, int value
)
1732 unsigned char *scan
;
1734 scan
= (unsigned char *) id
;
1740 *scan
++ = (value
>> 24) & 0xff;
1741 *scan
++ = (value
>> 16) & 0xff;
1742 *scan
++ = (value
>> 8) & 0xff;
1743 *scan
++ = (value
& 0xff);
1747 threadref_to_int (threadref
*ref
)
1750 unsigned char *scan
;
1756 value
= (value
<< 8) | ((*scan
++) & 0xff);
1761 copy_threadref (threadref
*dest
, threadref
*src
)
1764 unsigned char *csrc
, *cdest
;
1766 csrc
= (unsigned char *) src
;
1767 cdest
= (unsigned char *) dest
;
1774 threadmatch (threadref
*dest
, threadref
*src
)
1776 /* Things are broken right now, so just assume we got a match. */
1778 unsigned char *srcp
, *destp
;
1780 srcp
= (char *) src
;
1781 destp
= (char *) dest
;
1785 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1792 threadid:1, # always request threadid
1799 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1802 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1804 *pkt
++ = 'q'; /* Info Query */
1805 *pkt
++ = 'P'; /* process or thread info */
1806 pkt
= pack_int (pkt
, mode
); /* mode */
1807 pkt
= pack_threadid (pkt
, id
); /* threadid */
1808 *pkt
= '\0'; /* terminate */
1812 /* These values tag the fields in a thread info response packet. */
1813 /* Tagging the fields allows us to request specific fields and to
1814 add more fields as time goes by. */
1816 #define TAG_THREADID 1 /* Echo the thread identifier. */
1817 #define TAG_EXISTS 2 /* Is this process defined enough to
1818 fetch registers and its stack? */
1819 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1820 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1821 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1825 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1826 struct gdb_ext_thread_info
*info
)
1828 struct remote_state
*rs
= get_remote_state ();
1832 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
1835 /* info->threadid = 0; FIXME: implement zero_threadref. */
1837 info
->display
[0] = '\0';
1838 info
->shortname
[0] = '\0';
1839 info
->more_display
[0] = '\0';
1841 /* Assume the characters indicating the packet type have been
1843 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1844 pkt
= unpack_threadid (pkt
, &ref
);
1847 warning (_("Incomplete response to threadinfo request."));
1848 if (!threadmatch (&ref
, expectedref
))
1849 { /* This is an answer to a different request. */
1850 warning (_("ERROR RMT Thread info mismatch."));
1853 copy_threadref (&info
->threadid
, &ref
);
1855 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1857 /* Packets are terminated with nulls. */
1858 while ((pkt
< limit
) && mask
&& *pkt
)
1860 pkt
= unpack_int (pkt
, &tag
); /* tag */
1861 pkt
= unpack_byte (pkt
, &length
); /* length */
1862 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1864 warning (_("ERROR RMT: threadinfo tag mismatch."));
1868 if (tag
== TAG_THREADID
)
1872 warning (_("ERROR RMT: length of threadid is not 16."));
1876 pkt
= unpack_threadid (pkt
, &ref
);
1877 mask
= mask
& ~TAG_THREADID
;
1880 if (tag
== TAG_EXISTS
)
1882 info
->active
= stub_unpack_int (pkt
, length
);
1884 mask
= mask
& ~(TAG_EXISTS
);
1887 warning (_("ERROR RMT: 'exists' length too long."));
1893 if (tag
== TAG_THREADNAME
)
1895 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1896 mask
= mask
& ~TAG_THREADNAME
;
1899 if (tag
== TAG_DISPLAY
)
1901 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1902 mask
= mask
& ~TAG_DISPLAY
;
1905 if (tag
== TAG_MOREDISPLAY
)
1907 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1908 mask
= mask
& ~TAG_MOREDISPLAY
;
1911 warning (_("ERROR RMT: unknown thread info tag."));
1912 break; /* Not a tag we know about. */
1918 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1919 struct gdb_ext_thread_info
*info
)
1921 struct remote_state
*rs
= get_remote_state ();
1924 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
1926 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1928 if (rs
->buf
[0] == '\0')
1931 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
1936 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1939 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1940 threadref
*nextthread
)
1942 *pkt
++ = 'q'; /* info query packet */
1943 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1944 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1945 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1946 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1951 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1954 parse_threadlist_response (char *pkt
, int result_limit
,
1955 threadref
*original_echo
, threadref
*resultlist
,
1958 struct remote_state
*rs
= get_remote_state ();
1960 int count
, resultcount
, done
;
1963 /* Assume the 'q' and 'M chars have been stripped. */
1964 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
1965 /* done parse past here */
1966 pkt
= unpack_byte (pkt
, &count
); /* count field */
1967 pkt
= unpack_nibble (pkt
, &done
);
1968 /* The first threadid is the argument threadid. */
1969 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1970 while ((count
-- > 0) && (pkt
< limit
))
1972 pkt
= unpack_threadid (pkt
, resultlist
++);
1973 if (resultcount
++ >= result_limit
)
1982 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1983 int *done
, int *result_count
, threadref
*threadlist
)
1985 struct remote_state
*rs
= get_remote_state ();
1986 static threadref echo_nextthread
;
1989 /* Trancate result limit to be smaller than the packet size. */
1990 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= get_remote_packet_size ())
1991 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
1993 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
1995 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1997 if (*rs
->buf
== '\0')
2001 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
2004 if (!threadmatch (&echo_nextthread
, nextthread
))
2006 /* FIXME: This is a good reason to drop the packet. */
2007 /* Possably, there is a duplicate response. */
2009 retransmit immediatly - race conditions
2010 retransmit after timeout - yes
2012 wait for packet, then exit
2014 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2015 return 0; /* I choose simply exiting. */
2017 if (*result_count
<= 0)
2021 warning (_("RMT ERROR : failed to get remote thread list."));
2024 return result
; /* break; */
2026 if (*result_count
> result_limit
)
2029 warning (_("RMT ERROR: threadlist response longer than requested."));
2035 /* This is the interface between remote and threads, remotes upper
2038 /* remote_find_new_threads retrieves the thread list and for each
2039 thread in the list, looks up the thread in GDB's internal list,
2040 adding the thread if it does not already exist. This involves
2041 getting partial thread lists from the remote target so, polling the
2042 quit_flag is required. */
2045 /* About this many threadisds fit in a packet. */
2047 #define MAXTHREADLISTRESULTS 32
2050 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2053 int done
, i
, result_count
;
2057 static threadref nextthread
;
2058 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
2063 if (loopcount
++ > looplimit
)
2066 warning (_("Remote fetch threadlist -infinite loop-."));
2069 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
2070 &done
, &result_count
, resultthreadlist
))
2075 /* Clear for later iterations. */
2077 /* Setup to resume next batch of thread references, set nextthread. */
2078 if (result_count
>= 1)
2079 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
2081 while (result_count
--)
2082 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
2089 remote_newthread_step (threadref
*ref
, void *context
)
2091 int pid
= ptid_get_pid (inferior_ptid
);
2092 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2094 if (!in_thread_list (ptid
))
2096 return 1; /* continue iterator */
2099 #define CRAZY_MAX_THREADS 1000
2102 remote_current_thread (ptid_t oldpid
)
2104 struct remote_state
*rs
= get_remote_state ();
2110 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2111 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2112 return read_ptid (&rs
->buf
[2], NULL
);
2117 /* Find new threads for info threads command.
2118 * Original version, using John Metzler's thread protocol.
2122 remote_find_new_threads (void)
2124 remote_threadlist_iterator (remote_newthread_step
, 0,
2129 * Find all threads for info threads command.
2130 * Uses new thread protocol contributed by Cisco.
2131 * Falls back and attempts to use the older method (above)
2132 * if the target doesn't respond to the new method.
2136 remote_threads_info (void)
2138 struct remote_state
*rs
= get_remote_state ();
2142 if (remote_desc
== 0) /* paranoia */
2143 error (_("Command can only be used when connected to the remote target."));
2145 if (use_threadinfo_query
)
2147 putpkt ("qfThreadInfo");
2148 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2150 if (bufp
[0] != '\0') /* q packet recognized */
2152 while (*bufp
++ == 'm') /* reply contains one or more TID */
2156 new_thread
= read_ptid (bufp
, &bufp
);
2157 if (!ptid_equal (new_thread
, null_ptid
)
2158 && (!in_thread_list (new_thread
)
2159 || is_exited (new_thread
)))
2161 /* When connected to a multi-process aware stub,
2162 "info threads" may show up threads of
2163 inferiors we didn't know about yet. Add them
2164 now, and before adding any of its child
2165 threads, so notifications are emitted in a
2167 if (!in_inferior_list (ptid_get_pid (new_thread
)))
2168 add_inferior (ptid_get_pid (new_thread
));
2170 add_thread (new_thread
);
2172 /* In non-stop mode, we assume new found threads
2173 are running until we proven otherwise with a
2174 stop reply. In all-stop, we can only get
2175 here if all threads are stopped. */
2176 set_executing (new_thread
, non_stop
? 1 : 0);
2177 set_running (new_thread
, non_stop
? 1 : 0);
2180 while (*bufp
++ == ','); /* comma-separated list */
2181 putpkt ("qsThreadInfo");
2182 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2189 /* Only qfThreadInfo is supported in non-stop mode. */
2193 /* Else fall back to old method based on jmetzler protocol. */
2194 use_threadinfo_query
= 0;
2195 remote_find_new_threads ();
2200 * Collect a descriptive string about the given thread.
2201 * The target may say anything it wants to about the thread
2202 * (typically info about its blocked / runnable state, name, etc.).
2203 * This string will appear in the info threads display.
2205 * Optional: targets are not required to implement this function.
2209 remote_threads_extra_info (struct thread_info
*tp
)
2211 struct remote_state
*rs
= get_remote_state ();
2215 struct gdb_ext_thread_info threadinfo
;
2216 static char display_buf
[100]; /* arbitrary... */
2217 int n
= 0; /* position in display_buf */
2219 if (remote_desc
== 0) /* paranoia */
2220 internal_error (__FILE__
, __LINE__
,
2221 _("remote_threads_extra_info"));
2223 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2224 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2225 /* This is the main thread which was added by GDB. The remote
2226 server doesn't know about it. */
2229 if (use_threadextra_query
)
2232 char *endb
= rs
->buf
+ get_remote_packet_size ();
2234 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2236 write_ptid (b
, endb
, tp
->ptid
);
2239 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2240 if (rs
->buf
[0] != 0)
2242 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2243 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2244 display_buf
[result
] = '\0';
2249 /* If the above query fails, fall back to the old method. */
2250 use_threadextra_query
= 0;
2251 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2252 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2253 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2254 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2255 if (threadinfo
.active
)
2257 if (*threadinfo
.shortname
)
2258 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2259 " Name: %s,", threadinfo
.shortname
);
2260 if (*threadinfo
.display
)
2261 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2262 " State: %s,", threadinfo
.display
);
2263 if (*threadinfo
.more_display
)
2264 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2265 " Priority: %s", threadinfo
.more_display
);
2269 /* For purely cosmetic reasons, clear up trailing commas. */
2270 if (',' == display_buf
[n
-1])
2271 display_buf
[n
-1] = ' ';
2279 /* Restart the remote side; this is an extended protocol operation. */
2282 extended_remote_restart (void)
2284 struct remote_state
*rs
= get_remote_state ();
2286 /* Send the restart command; for reasons I don't understand the
2287 remote side really expects a number after the "R". */
2288 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2291 remote_fileio_reset ();
2294 /* Clean up connection to a remote debugger. */
2297 remote_close (int quitting
)
2299 if (remote_desc
== NULL
)
2300 return; /* already closed */
2302 /* Make sure we leave stdin registered in the event loop, and we
2303 don't leave the async SIGINT signal handler installed. */
2304 remote_terminal_ours ();
2306 serial_close (remote_desc
);
2309 /* We don't have a connection to the remote stub anymore. Get rid
2310 of all the inferiors and their threads we were controlling. */
2311 discard_all_inferiors ();
2313 /* We're no longer interested in any of these events. */
2314 discard_pending_stop_replies (-1);
2316 if (remote_async_inferior_event_token
)
2317 delete_async_event_handler (&remote_async_inferior_event_token
);
2318 if (remote_async_get_pending_events_token
)
2319 delete_async_event_handler (&remote_async_get_pending_events_token
);
2321 generic_mourn_inferior ();
2324 /* Query the remote side for the text, data and bss offsets. */
2329 struct remote_state
*rs
= get_remote_state ();
2332 int lose
, num_segments
= 0, do_sections
, do_segments
;
2333 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
2334 struct section_offsets
*offs
;
2335 struct symfile_segment_data
*data
;
2337 if (symfile_objfile
== NULL
)
2340 putpkt ("qOffsets");
2341 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2344 if (buf
[0] == '\000')
2345 return; /* Return silently. Stub doesn't support
2349 warning (_("Remote failure reply: %s"), buf
);
2353 /* Pick up each field in turn. This used to be done with scanf, but
2354 scanf will make trouble if CORE_ADDR size doesn't match
2355 conversion directives correctly. The following code will work
2356 with any size of CORE_ADDR. */
2357 text_addr
= data_addr
= bss_addr
= 0;
2361 if (strncmp (ptr
, "Text=", 5) == 0)
2364 /* Don't use strtol, could lose on big values. */
2365 while (*ptr
&& *ptr
!= ';')
2366 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2368 if (strncmp (ptr
, ";Data=", 6) == 0)
2371 while (*ptr
&& *ptr
!= ';')
2372 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2377 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
2380 while (*ptr
&& *ptr
!= ';')
2381 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
2383 if (bss_addr
!= data_addr
)
2384 warning (_("Target reported unsupported offsets: %s"), buf
);
2389 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
2392 /* Don't use strtol, could lose on big values. */
2393 while (*ptr
&& *ptr
!= ';')
2394 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2397 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
2400 while (*ptr
&& *ptr
!= ';')
2401 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2409 error (_("Malformed response to offset query, %s"), buf
);
2410 else if (*ptr
!= '\0')
2411 warning (_("Target reported unsupported offsets: %s"), buf
);
2413 offs
= ((struct section_offsets
*)
2414 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
2415 memcpy (offs
, symfile_objfile
->section_offsets
,
2416 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
2418 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
2419 do_segments
= (data
!= NULL
);
2420 do_sections
= num_segments
== 0;
2422 if (num_segments
> 0)
2424 segments
[0] = text_addr
;
2425 segments
[1] = data_addr
;
2427 /* If we have two segments, we can still try to relocate everything
2428 by assuming that the .text and .data offsets apply to the whole
2429 text and data segments. Convert the offsets given in the packet
2430 to base addresses for symfile_map_offsets_to_segments. */
2431 else if (data
&& data
->num_segments
== 2)
2433 segments
[0] = data
->segment_bases
[0] + text_addr
;
2434 segments
[1] = data
->segment_bases
[1] + data_addr
;
2437 /* If the object file has only one segment, assume that it is text
2438 rather than data; main programs with no writable data are rare,
2439 but programs with no code are useless. Of course the code might
2440 have ended up in the data segment... to detect that we would need
2441 the permissions here. */
2442 else if (data
&& data
->num_segments
== 1)
2444 segments
[0] = data
->segment_bases
[0] + text_addr
;
2447 /* There's no way to relocate by segment. */
2453 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
2454 offs
, num_segments
, segments
);
2456 if (ret
== 0 && !do_sections
)
2457 error (_("Can not handle qOffsets TextSeg response with this symbol file"));
2464 free_symfile_segment_data (data
);
2468 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
2470 /* This is a temporary kludge to force data and bss to use the same offsets
2471 because that's what nlmconv does now. The real solution requires changes
2472 to the stub and remote.c that I don't have time to do right now. */
2474 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
2475 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
2478 objfile_relocate (symfile_objfile
, offs
);
2481 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
2482 threads we know are stopped already. This is used during the
2483 initial remote connection in non-stop mode --- threads that are
2484 reported as already being stopped are left stopped. */
2487 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
2489 /* If we have a stop reply for this thread, it must be stopped. */
2490 if (peek_stop_reply (thread
->ptid
))
2491 set_stop_requested (thread
->ptid
, 1);
2496 /* Stub for catch_exception. */
2498 struct start_remote_args
2502 /* The current target. */
2503 struct target_ops
*target
;
2505 /* Non-zero if this is an extended-remote target. */
2510 remote_start_remote (struct ui_out
*uiout
, void *opaque
)
2512 struct start_remote_args
*args
= opaque
;
2513 struct remote_state
*rs
= get_remote_state ();
2514 struct packet_config
*noack_config
;
2515 char *wait_status
= NULL
;
2517 immediate_quit
++; /* Allow user to interrupt it. */
2519 /* Ack any packet which the remote side has already sent. */
2520 serial_write (remote_desc
, "+", 1);
2522 /* The first packet we send to the target is the optional "supported
2523 packets" request. If the target can answer this, it will tell us
2524 which later probes to skip. */
2525 remote_query_supported ();
2527 /* Next, we possibly activate noack mode.
2529 If the QStartNoAckMode packet configuration is set to AUTO,
2530 enable noack mode if the stub reported a wish for it with
2533 If set to TRUE, then enable noack mode even if the stub didn't
2534 report it in qSupported. If the stub doesn't reply OK, the
2535 session ends with an error.
2537 If FALSE, then don't activate noack mode, regardless of what the
2538 stub claimed should be the default with qSupported. */
2540 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
2542 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
2543 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
2544 && noack_config
->support
== PACKET_ENABLE
))
2546 putpkt ("QStartNoAckMode");
2547 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2548 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
2552 if (args
->extended_p
)
2554 /* Tell the remote that we are using the extended protocol. */
2556 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2559 /* Next, if the target can specify a description, read it. We do
2560 this before anything involving memory or registers. */
2561 target_find_description ();
2563 /* On OSs where the list of libraries is global to all
2564 processes, we fetch them early. */
2565 if (gdbarch_has_global_solist (target_gdbarch
))
2566 solib_add (NULL
, args
->from_tty
, args
->target
, auto_solib_add
);
2570 if (!rs
->non_stop_aware
)
2571 error (_("Non-stop mode requested, but remote does not support non-stop"));
2573 putpkt ("QNonStop:1");
2574 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2576 if (strcmp (rs
->buf
, "OK") != 0)
2577 error ("Remote refused setting non-stop mode with: %s", rs
->buf
);
2579 /* Find about threads and processes the stub is already
2580 controlling. We default to adding them in the running state.
2581 The '?' query below will then tell us about which threads are
2583 remote_threads_info ();
2585 else if (rs
->non_stop_aware
)
2587 /* Don't assume that the stub can operate in all-stop mode.
2588 Request it explicitely. */
2589 putpkt ("QNonStop:0");
2590 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2592 if (strcmp (rs
->buf
, "OK") != 0)
2593 error ("Remote refused setting all-stop mode with: %s", rs
->buf
);
2596 /* Check whether the target is running now. */
2598 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2602 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
2604 if (args
->extended_p
)
2606 /* We're connected, but not running. Drop out before we
2607 call start_remote. */
2608 target_mark_exited (args
->target
);
2612 error (_("The target is not running (try extended-remote?)"));
2616 if (args
->extended_p
)
2617 target_mark_running (args
->target
);
2619 /* Save the reply for later. */
2620 wait_status
= alloca (strlen (rs
->buf
) + 1);
2621 strcpy (wait_status
, rs
->buf
);
2624 /* Let the stub know that we want it to return the thread. */
2625 set_continue_thread (minus_one_ptid
);
2627 /* Without this, some commands which require an active target
2628 (such as kill) won't work. This variable serves (at least)
2629 double duty as both the pid of the target process (if it has
2630 such), and as a flag indicating that a target is active.
2631 These functions should be split out into seperate variables,
2632 especially since GDB will someday have a notion of debugging
2633 several processes. */
2634 inferior_ptid
= magic_null_ptid
;
2636 /* Now, if we have thread information, update inferior_ptid. */
2637 inferior_ptid
= remote_current_thread (inferior_ptid
);
2639 add_inferior (ptid_get_pid (inferior_ptid
));
2641 /* Always add the main thread. */
2642 add_thread_silent (inferior_ptid
);
2644 get_offsets (); /* Get text, data & bss offsets. */
2646 /* If we could not find a description using qXfer, and we know
2647 how to do it some other way, try again. This is not
2648 supported for non-stop; it could be, but it is tricky if
2649 there are no stopped threads when we connect. */
2650 if (remote_read_description_p (args
->target
)
2651 && gdbarch_target_desc (target_gdbarch
) == NULL
)
2653 target_clear_description ();
2654 target_find_description ();
2657 /* Use the previously fetched status. */
2658 gdb_assert (wait_status
!= NULL
);
2659 strcpy (rs
->buf
, wait_status
);
2660 rs
->cached_wait_status
= 1;
2663 start_remote (args
->from_tty
); /* Initialize gdb process mechanisms. */
2667 /* In non-stop, we will either get an "OK", meaning that there
2668 are no stopped threads at this time; or, a regular stop
2669 reply. In the latter case, there may be more than one thread
2670 stopped --- we pull them all out using the vStopped
2672 if (strcmp (rs
->buf
, "OK") != 0)
2674 struct stop_reply
*stop_reply
;
2675 struct cleanup
*old_chain
;
2677 stop_reply
= stop_reply_xmalloc ();
2678 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
2680 remote_parse_stop_reply (rs
->buf
, stop_reply
);
2681 discard_cleanups (old_chain
);
2683 /* get_pending_stop_replies acks this one, and gets the rest
2685 pending_stop_reply
= stop_reply
;
2686 remote_get_pending_stop_replies ();
2688 /* Make sure that threads that were stopped remain
2690 iterate_over_threads (set_stop_requested_callback
, NULL
);
2693 if (target_can_async_p ())
2694 target_async (inferior_event_handler
, 0);
2696 if (thread_count () == 0)
2698 if (args
->extended_p
)
2700 /* We're connected, but not running. Drop out before we
2701 call start_remote. */
2702 target_mark_exited (args
->target
);
2706 error (_("The target is not running (try extended-remote?)"));
2709 if (args
->extended_p
)
2710 target_mark_running (args
->target
);
2712 /* Let the stub know that we want it to return the thread. */
2714 /* Force the stub to choose a thread. */
2715 set_general_thread (null_ptid
);
2718 inferior_ptid
= remote_current_thread (minus_one_ptid
);
2719 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
2720 error (_("remote didn't report the current thread in non-stop mode"));
2722 get_offsets (); /* Get text, data & bss offsets. */
2724 /* In non-stop mode, any cached wait status will be stored in
2725 the stop reply queue. */
2726 gdb_assert (wait_status
== NULL
);
2729 /* If we connected to a live target, do some additional setup. */
2730 if (target_has_execution
)
2732 if (exec_bfd
) /* No use without an exec file. */
2733 remote_check_symbols (symfile_objfile
);
2736 /* If code is shared between processes, then breakpoints are global
2737 too; Insert them now. */
2738 if (gdbarch_has_global_solist (target_gdbarch
)
2739 && breakpoints_always_inserted_mode ())
2740 insert_breakpoints ();
2743 /* Open a connection to a remote debugger.
2744 NAME is the filename used for communication. */
2747 remote_open (char *name
, int from_tty
)
2749 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
2752 /* Open a connection to a remote debugger using the extended
2753 remote gdb protocol. NAME is the filename used for communication. */
2756 extended_remote_open (char *name
, int from_tty
)
2758 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
2761 /* Generic code for opening a connection to a remote target. */
2764 init_all_packet_configs (void)
2767 for (i
= 0; i
< PACKET_MAX
; i
++)
2768 update_packet_config (&remote_protocol_packets
[i
]);
2771 /* Symbol look-up. */
2774 remote_check_symbols (struct objfile
*objfile
)
2776 struct remote_state
*rs
= get_remote_state ();
2777 char *msg
, *reply
, *tmp
;
2778 struct minimal_symbol
*sym
;
2781 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
2784 /* Make sure the remote is pointing at the right process. */
2785 set_general_process ();
2787 /* Allocate a message buffer. We can't reuse the input buffer in RS,
2788 because we need both at the same time. */
2789 msg
= alloca (get_remote_packet_size ());
2791 /* Invite target to request symbol lookups. */
2793 putpkt ("qSymbol::");
2794 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2795 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
2798 while (strncmp (reply
, "qSymbol:", 8) == 0)
2801 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
2803 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
2805 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
2808 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
2810 /* If this is a function address, return the start of code
2811 instead of any data function descriptor. */
2812 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
2816 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
2817 paddr_nz (sym_addr
), &reply
[8]);
2821 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2826 static struct serial
*
2827 remote_serial_open (char *name
)
2829 static int udp_warning
= 0;
2831 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2832 of in ser-tcp.c, because it is the remote protocol assuming that the
2833 serial connection is reliable and not the serial connection promising
2835 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
2838 The remote protocol may be unreliable over UDP.\n\
2839 Some events may be lost, rendering further debugging impossible."));
2843 return serial_open (name
);
2846 /* This type describes each known response to the qSupported
2848 struct protocol_feature
2850 /* The name of this protocol feature. */
2853 /* The default for this protocol feature. */
2854 enum packet_support default_support
;
2856 /* The function to call when this feature is reported, or after
2857 qSupported processing if the feature is not supported.
2858 The first argument points to this structure. The second
2859 argument indicates whether the packet requested support be
2860 enabled, disabled, or probed (or the default, if this function
2861 is being called at the end of processing and this feature was
2862 not reported). The third argument may be NULL; if not NULL, it
2863 is a NUL-terminated string taken from the packet following
2864 this feature's name and an equals sign. */
2865 void (*func
) (const struct protocol_feature
*, enum packet_support
,
2868 /* The corresponding packet for this feature. Only used if
2869 FUNC is remote_supported_packet. */
2874 remote_supported_packet (const struct protocol_feature
*feature
,
2875 enum packet_support support
,
2876 const char *argument
)
2880 warning (_("Remote qSupported response supplied an unexpected value for"
2881 " \"%s\"."), feature
->name
);
2885 if (remote_protocol_packets
[feature
->packet
].support
2886 == PACKET_SUPPORT_UNKNOWN
)
2887 remote_protocol_packets
[feature
->packet
].support
= support
;
2891 remote_packet_size (const struct protocol_feature
*feature
,
2892 enum packet_support support
, const char *value
)
2894 struct remote_state
*rs
= get_remote_state ();
2899 if (support
!= PACKET_ENABLE
)
2902 if (value
== NULL
|| *value
== '\0')
2904 warning (_("Remote target reported \"%s\" without a size."),
2910 packet_size
= strtol (value
, &value_end
, 16);
2911 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
2913 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
2914 feature
->name
, value
);
2918 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
2920 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
2921 packet_size
, MAX_REMOTE_PACKET_SIZE
);
2922 packet_size
= MAX_REMOTE_PACKET_SIZE
;
2925 /* Record the new maximum packet size. */
2926 rs
->explicit_packet_size
= packet_size
;
2930 remote_multi_process_feature (const struct protocol_feature
*feature
,
2931 enum packet_support support
, const char *value
)
2933 struct remote_state
*rs
= get_remote_state ();
2934 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
2938 remote_non_stop_feature (const struct protocol_feature
*feature
,
2939 enum packet_support support
, const char *value
)
2941 struct remote_state
*rs
= get_remote_state ();
2942 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
2945 static struct protocol_feature remote_protocol_features
[] = {
2946 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
2947 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
2948 PACKET_qXfer_auxv
},
2949 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
2950 PACKET_qXfer_features
},
2951 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
2952 PACKET_qXfer_libraries
},
2953 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
2954 PACKET_qXfer_memory_map
},
2955 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
2956 PACKET_qXfer_spu_read
},
2957 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
2958 PACKET_qXfer_spu_write
},
2959 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
2960 PACKET_qXfer_osdata
},
2961 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
2962 PACKET_QPassSignals
},
2963 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
2964 PACKET_QStartNoAckMode
},
2965 { "multiprocess", PACKET_DISABLE
, remote_multi_process_feature
, -1 },
2966 { "QNonStop", PACKET_DISABLE
, remote_non_stop_feature
, -1 },
2967 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
2968 PACKET_qXfer_siginfo_read
},
2969 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
2970 PACKET_qXfer_siginfo_write
},
2974 remote_query_supported (void)
2976 struct remote_state
*rs
= get_remote_state ();
2979 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
2981 /* The packet support flags are handled differently for this packet
2982 than for most others. We treat an error, a disabled packet, and
2983 an empty response identically: any features which must be reported
2984 to be used will be automatically disabled. An empty buffer
2985 accomplishes this, since that is also the representation for a list
2986 containing no features. */
2989 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
2992 putpkt ("qSupported:multiprocess+");
2994 putpkt ("qSupported");
2996 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2998 /* If an error occured, warn, but do not return - just reset the
2999 buffer to empty and go on to disable features. */
3000 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
3003 warning (_("Remote failure reply: %s"), rs
->buf
);
3008 memset (seen
, 0, sizeof (seen
));
3013 enum packet_support is_supported
;
3014 char *p
, *end
, *name_end
, *value
;
3016 /* First separate out this item from the rest of the packet. If
3017 there's another item after this, we overwrite the separator
3018 (terminated strings are much easier to work with). */
3020 end
= strchr (p
, ';');
3023 end
= p
+ strlen (p
);
3033 warning (_("empty item in \"qSupported\" response"));
3038 name_end
= strchr (p
, '=');
3041 /* This is a name=value entry. */
3042 is_supported
= PACKET_ENABLE
;
3043 value
= name_end
+ 1;
3052 is_supported
= PACKET_ENABLE
;
3056 is_supported
= PACKET_DISABLE
;
3060 is_supported
= PACKET_SUPPORT_UNKNOWN
;
3064 warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p
);
3070 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3071 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
3073 const struct protocol_feature
*feature
;
3076 feature
= &remote_protocol_features
[i
];
3077 feature
->func (feature
, is_supported
, value
);
3082 /* If we increased the packet size, make sure to increase the global
3083 buffer size also. We delay this until after parsing the entire
3084 qSupported packet, because this is the same buffer we were
3086 if (rs
->buf_size
< rs
->explicit_packet_size
)
3088 rs
->buf_size
= rs
->explicit_packet_size
;
3089 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
3092 /* Handle the defaults for unmentioned features. */
3093 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3096 const struct protocol_feature
*feature
;
3098 feature
= &remote_protocol_features
[i
];
3099 feature
->func (feature
, feature
->default_support
, NULL
);
3105 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
, int extended_p
)
3107 struct remote_state
*rs
= get_remote_state ();
3110 error (_("To open a remote debug connection, you need to specify what\n"
3111 "serial device is attached to the remote system\n"
3112 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
3114 /* See FIXME above. */
3115 if (!target_async_permitted
)
3116 wait_forever_enabled_p
= 1;
3118 /* If we're connected to a running target, target_preopen will kill it.
3119 But if we're connected to a target system with no running process,
3120 then we will still be connected when it returns. Ask this question
3121 first, before target_preopen has a chance to kill anything. */
3122 if (remote_desc
!= NULL
&& !target_has_execution
)
3125 || query (_("Already connected to a remote target. Disconnect? ")))
3128 error (_("Still connected."));
3131 target_preopen (from_tty
);
3133 unpush_target (target
);
3135 /* This time without a query. If we were connected to an
3136 extended-remote target and target_preopen killed the running
3137 process, we may still be connected. If we are starting "target
3138 remote" now, the extended-remote target will not have been
3139 removed by unpush_target. */
3140 if (remote_desc
!= NULL
&& !target_has_execution
)
3143 /* Make sure we send the passed signals list the next time we resume. */
3144 xfree (last_pass_packet
);
3145 last_pass_packet
= NULL
;
3147 remote_fileio_reset ();
3148 reopen_exec_file ();
3151 remote_desc
= remote_serial_open (name
);
3153 perror_with_name (name
);
3155 if (baud_rate
!= -1)
3157 if (serial_setbaudrate (remote_desc
, baud_rate
))
3159 /* The requested speed could not be set. Error out to
3160 top level after closing remote_desc. Take care to
3161 set remote_desc to NULL to avoid closing remote_desc
3163 serial_close (remote_desc
);
3165 perror_with_name (name
);
3169 serial_raw (remote_desc
);
3171 /* If there is something sitting in the buffer we might take it as a
3172 response to a command, which would be bad. */
3173 serial_flush_input (remote_desc
);
3177 puts_filtered ("Remote debugging using ");
3178 puts_filtered (name
);
3179 puts_filtered ("\n");
3181 push_target (target
); /* Switch to using remote target now. */
3183 /* Assume that the target is not running, until we learn otherwise. */
3185 target_mark_exited (target
);
3187 /* Register extra event sources in the event loop. */
3188 remote_async_inferior_event_token
3189 = create_async_event_handler (remote_async_inferior_event_handler
,
3191 remote_async_get_pending_events_token
3192 = create_async_event_handler (remote_async_get_pending_events_handler
,
3195 /* Reset the target state; these things will be queried either by
3196 remote_query_supported or as they are needed. */
3197 init_all_packet_configs ();
3198 rs
->cached_wait_status
= 0;
3199 rs
->explicit_packet_size
= 0;
3201 rs
->multi_process_aware
= 0;
3202 rs
->extended
= extended_p
;
3203 rs
->non_stop_aware
= 0;
3204 rs
->waiting_for_stop_reply
= 0;
3206 general_thread
= not_sent_ptid
;
3207 continue_thread
= not_sent_ptid
;
3209 /* Probe for ability to use "ThreadInfo" query, as required. */
3210 use_threadinfo_query
= 1;
3211 use_threadextra_query
= 1;
3213 if (target_async_permitted
)
3215 /* With this target we start out by owning the terminal. */
3216 remote_async_terminal_ours_p
= 1;
3218 /* FIXME: cagney/1999-09-23: During the initial connection it is
3219 assumed that the target is already ready and able to respond to
3220 requests. Unfortunately remote_start_remote() eventually calls
3221 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
3222 around this. Eventually a mechanism that allows
3223 wait_for_inferior() to expect/get timeouts will be
3225 wait_forever_enabled_p
= 0;
3228 /* First delete any symbols previously loaded from shared libraries. */
3229 no_shared_libraries (NULL
, 0);
3232 init_thread_list ();
3234 /* Start the remote connection. If error() or QUIT, discard this
3235 target (we'd otherwise be in an inconsistent state) and then
3236 propogate the error on up the exception chain. This ensures that
3237 the caller doesn't stumble along blindly assuming that the
3238 function succeeded. The CLI doesn't have this problem but other
3239 UI's, such as MI do.
3241 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
3242 this function should return an error indication letting the
3243 caller restore the previous state. Unfortunately the command
3244 ``target remote'' is directly wired to this function making that
3245 impossible. On a positive note, the CLI side of this problem has
3246 been fixed - the function set_cmd_context() makes it possible for
3247 all the ``target ....'' commands to share a common callback
3248 function. See cli-dump.c. */
3250 struct gdb_exception ex
;
3251 struct start_remote_args args
;
3253 args
.from_tty
= from_tty
;
3254 args
.target
= target
;
3255 args
.extended_p
= extended_p
;
3257 ex
= catch_exception (uiout
, remote_start_remote
, &args
, RETURN_MASK_ALL
);
3260 /* Pop the partially set up target - unless something else did
3261 already before throwing the exception. */
3262 if (remote_desc
!= NULL
)
3264 if (target_async_permitted
)
3265 wait_forever_enabled_p
= 1;
3266 throw_exception (ex
);
3270 if (target_async_permitted
)
3271 wait_forever_enabled_p
= 1;
3274 /* This takes a program previously attached to and detaches it. After
3275 this is done, GDB can be used to debug some other program. We
3276 better not have left any breakpoints in the target program or it'll
3277 die when it hits one. */
3280 remote_detach_1 (char *args
, int from_tty
, int extended
)
3282 int pid
= ptid_get_pid (inferior_ptid
);
3283 struct remote_state
*rs
= get_remote_state ();
3286 error (_("Argument given to \"detach\" when remotely debugging."));
3288 if (!target_has_execution
)
3289 error (_("No process to detach from."));
3291 /* Tell the remote target to detach. */
3292 if (remote_multi_process_p (rs
))
3293 sprintf (rs
->buf
, "D;%x", pid
);
3295 strcpy (rs
->buf
, "D");
3298 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3300 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
3302 else if (rs
->buf
[0] == '\0')
3303 error (_("Remote doesn't know how to detach"));
3305 error (_("Can't detach process."));
3309 if (remote_multi_process_p (rs
))
3310 printf_filtered (_("Detached from remote %s.\n"),
3311 target_pid_to_str (pid_to_ptid (pid
)));
3315 puts_filtered (_("Detached from remote process.\n"));
3317 puts_filtered (_("Ending remote debugging.\n"));
3321 discard_pending_stop_replies (pid
);
3322 target_mourn_inferior ();
3326 remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
3328 remote_detach_1 (args
, from_tty
, 0);
3332 extended_remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
3334 remote_detach_1 (args
, from_tty
, 1);
3337 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
3340 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
3343 error (_("Argument given to \"disconnect\" when remotely debugging."));
3345 /* Make sure we unpush even the extended remote targets; mourn
3346 won't do it. So call remote_mourn_1 directly instead of
3347 target_mourn_inferior. */
3348 remote_mourn_1 (target
);
3351 puts_filtered ("Ending remote debugging.\n");
3354 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
3355 be chatty about it. */
3358 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
3360 struct remote_state
*rs
= get_remote_state ();
3363 char *wait_status
= NULL
;
3364 struct inferior
*inf
;
3367 error_no_arg (_("process-id to attach"));
3370 pid
= strtol (args
, &dummy
, 0);
3371 /* Some targets don't set errno on errors, grrr! */
3372 if (pid
== 0 && args
== dummy
)
3373 error (_("Illegal process-id: %s."), args
);
3375 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
3376 error (_("This target does not support attaching to a process"));
3378 sprintf (rs
->buf
, "vAttach;%x", pid
);
3380 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3382 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
3385 printf_unfiltered (_("Attached to %s\n"),
3386 target_pid_to_str (pid_to_ptid (pid
)));
3390 /* Save the reply for later. */
3391 wait_status
= alloca (strlen (rs
->buf
) + 1);
3392 strcpy (wait_status
, rs
->buf
);
3394 else if (strcmp (rs
->buf
, "OK") != 0)
3395 error (_("Attaching to %s failed with: %s"),
3396 target_pid_to_str (pid_to_ptid (pid
)),
3399 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
3400 error (_("This target does not support attaching to a process"));
3402 error (_("Attaching to %s failed"),
3403 target_pid_to_str (pid_to_ptid (pid
)));
3405 target_mark_running (target
);
3406 inferior_ptid
= pid_to_ptid (pid
);
3408 /* Now, if we have thread information, update inferior_ptid. */
3409 inferior_ptid
= remote_current_thread (inferior_ptid
);
3411 inf
= add_inferior (pid
);
3412 inf
->attach_flag
= 1;
3415 /* Get list of threads. */
3416 remote_threads_info ();
3418 /* Add the main thread to the thread list. */
3419 add_thread_silent (inferior_ptid
);
3421 /* Next, if the target can specify a description, read it. We do
3422 this before anything involving memory or registers. */
3423 target_find_description ();
3427 /* Use the previously fetched status. */
3428 gdb_assert (wait_status
!= NULL
);
3430 if (target_can_async_p ())
3432 struct stop_reply
*stop_reply
;
3433 struct cleanup
*old_chain
;
3435 stop_reply
= stop_reply_xmalloc ();
3436 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
3437 remote_parse_stop_reply (wait_status
, stop_reply
);
3438 discard_cleanups (old_chain
);
3439 push_stop_reply (stop_reply
);
3441 target_async (inferior_event_handler
, 0);
3445 gdb_assert (wait_status
!= NULL
);
3446 strcpy (rs
->buf
, wait_status
);
3447 rs
->cached_wait_status
= 1;
3451 gdb_assert (wait_status
== NULL
);
3455 extended_remote_attach (struct target_ops
*ops
, char *args
, int from_tty
)
3457 extended_remote_attach_1 (ops
, args
, from_tty
);
3460 /* Convert hex digit A to a number. */
3465 if (a
>= '0' && a
<= '9')
3467 else if (a
>= 'a' && a
<= 'f')
3468 return a
- 'a' + 10;
3469 else if (a
>= 'A' && a
<= 'F')
3470 return a
- 'A' + 10;
3472 error (_("Reply contains invalid hex digit %d"), a
);
3476 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
3480 for (i
= 0; i
< count
; i
++)
3482 if (hex
[0] == 0 || hex
[1] == 0)
3484 /* Hex string is short, or of uneven length.
3485 Return the count that has been converted so far. */
3488 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
3494 /* Convert number NIB to a hex digit. */
3502 return 'a' + nib
- 10;
3506 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
3509 /* May use a length, or a nul-terminated string as input. */
3511 count
= strlen ((char *) bin
);
3513 for (i
= 0; i
< count
; i
++)
3515 *hex
++ = tohex ((*bin
>> 4) & 0xf);
3516 *hex
++ = tohex (*bin
++ & 0xf);
3522 /* Check for the availability of vCont. This function should also check
3526 remote_vcont_probe (struct remote_state
*rs
)
3530 strcpy (rs
->buf
, "vCont?");
3532 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3535 /* Make sure that the features we assume are supported. */
3536 if (strncmp (buf
, "vCont", 5) == 0)
3539 int support_s
, support_S
, support_c
, support_C
;
3545 rs
->support_vCont_t
= 0;
3546 while (p
&& *p
== ';')
3549 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3551 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3553 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3555 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3557 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3558 rs
->support_vCont_t
= 1;
3560 p
= strchr (p
, ';');
3563 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
3564 BUF will make packet_ok disable the packet. */
3565 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
3569 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
3572 /* Resume the remote inferior by using a "vCont" packet. The thread
3573 to be resumed is PTID; STEP and SIGGNAL indicate whether the
3574 resumed thread should be single-stepped and/or signalled. If PTID
3575 equals minus_one_ptid, then all threads are resumed; the thread to
3576 be stepped and/or signalled is given in the global INFERIOR_PTID.
3577 This function returns non-zero iff it resumes the inferior.
3579 This function issues a strict subset of all possible vCont commands at the
3583 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
3585 struct remote_state
*rs
= get_remote_state ();
3589 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
3590 remote_vcont_probe (rs
);
3592 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
3596 endp
= rs
->buf
+ get_remote_packet_size ();
3598 /* If we could generate a wider range of packets, we'd have to worry
3599 about overflowing BUF. Should there be a generic
3600 "multi-part-packet" packet? */
3602 if (ptid_equal (ptid
, magic_null_ptid
))
3604 /* MAGIC_NULL_PTID means that we don't have any active threads,
3605 so we don't have any TID numbers the inferior will
3606 understand. Make sure to only send forms that do not specify
3608 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3609 xsnprintf (p
, endp
- p
, "vCont;S%02x", siggnal
);
3611 xsnprintf (p
, endp
- p
, "vCont;s");
3612 else if (siggnal
!= TARGET_SIGNAL_0
)
3613 xsnprintf (p
, endp
- p
, "vCont;C%02x", siggnal
);
3615 xsnprintf (p
, endp
- p
, "vCont;c");
3617 else if (ptid_equal (ptid
, minus_one_ptid
))
3619 /* Resume all threads, with preference for INFERIOR_PTID. */
3620 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3622 /* Step inferior_ptid with signal. */
3623 p
+= xsnprintf (p
, endp
- p
, "vCont;S%02x:", siggnal
);
3624 p
= write_ptid (p
, endp
, inferior_ptid
);
3625 /* And continue others. */
3626 p
+= xsnprintf (p
, endp
- p
, ";c");
3630 /* Step inferior_ptid. */
3631 p
+= xsnprintf (p
, endp
- p
, "vCont;s:");
3632 p
= write_ptid (p
, endp
, inferior_ptid
);
3633 /* And continue others. */
3634 p
+= xsnprintf (p
, endp
- p
, ";c");
3636 else if (siggnal
!= TARGET_SIGNAL_0
)
3638 /* Continue inferior_ptid with signal. */
3639 p
+= xsnprintf (p
, endp
- p
, "vCont;C%02x:", siggnal
);
3640 p
= write_ptid (p
, endp
, inferior_ptid
);
3641 /* And continue others. */
3642 p
+= xsnprintf (p
, endp
- p
, ";c");
3645 xsnprintf (p
, endp
- p
, "vCont;c");
3649 /* Scheduler locking; resume only PTID. */
3650 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3652 /* Step ptid with signal. */
3653 p
+= xsnprintf (p
, endp
- p
, "vCont;S%02x:", siggnal
);
3654 p
= write_ptid (p
, endp
, ptid
);
3659 p
+= xsnprintf (p
, endp
- p
, "vCont;s:");
3660 p
= write_ptid (p
, endp
, ptid
);
3662 else if (siggnal
!= TARGET_SIGNAL_0
)
3664 /* Continue ptid with signal. */
3665 p
+= xsnprintf (p
, endp
- p
, "vCont;C%02x:", siggnal
);
3666 p
= write_ptid (p
, endp
, ptid
);
3670 /* Continue ptid. */
3671 p
+= xsnprintf (p
, endp
- p
, "vCont;c:");
3672 p
= write_ptid (p
, endp
, ptid
);
3676 gdb_assert (strlen (rs
->buf
) < get_remote_packet_size ());
3681 /* In non-stop, the stub replies to vCont with "OK". The stop
3682 reply will be reported asynchronously by means of a `%Stop'
3684 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3685 if (strcmp (rs
->buf
, "OK") != 0)
3686 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
);
3692 /* Tell the remote machine to resume. */
3694 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
3696 static int last_sent_step
;
3699 remote_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
3701 struct remote_state
*rs
= get_remote_state ();
3704 last_sent_signal
= siggnal
;
3705 last_sent_step
= step
;
3707 /* Update the inferior on signals to silently pass, if they've changed. */
3708 remote_pass_signals ();
3710 /* The vCont packet doesn't need to specify threads via Hc. */
3711 if (remote_vcont_resume (ptid
, step
, siggnal
))
3714 /* All other supported resume packets do use Hc, so set the continue
3716 if (ptid_equal (ptid
, minus_one_ptid
))
3717 set_continue_thread (any_thread_ptid
);
3719 set_continue_thread (ptid
);
3722 if (execution_direction
== EXEC_REVERSE
)
3724 /* We don't pass signals to the target in reverse exec mode. */
3725 if (info_verbose
&& siggnal
!= TARGET_SIGNAL_0
)
3726 warning (" - Can't pass signal %d to target in reverse: ignored.\n",
3728 strcpy (buf
, step
? "bs" : "bc");
3730 else if (siggnal
!= TARGET_SIGNAL_0
)
3732 buf
[0] = step
? 'S' : 'C';
3733 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
3734 buf
[2] = tohex (((int) siggnal
) & 0xf);
3738 strcpy (buf
, step
? "s" : "c");
3743 /* We are about to start executing the inferior, let's register it
3744 with the event loop. NOTE: this is the one place where all the
3745 execution commands end up. We could alternatively do this in each
3746 of the execution commands in infcmd.c. */
3747 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
3748 into infcmd.c in order to allow inferior function calls to work
3749 NOT asynchronously. */
3750 if (target_can_async_p ())
3751 target_async (inferior_event_handler
, 0);
3753 /* We've just told the target to resume. The remote server will
3754 wait for the inferior to stop, and then send a stop reply. In
3755 the mean time, we can't start another command/query ourselves
3756 because the stub wouldn't be ready to process it. This applies
3757 only to the base all-stop protocol, however. In non-stop (which
3758 only supports vCont), the stub replies with an "OK", and is
3759 immediate able to process further serial input. */
3761 rs
->waiting_for_stop_reply
= 1;
3765 /* Set up the signal handler for SIGINT, while the target is
3766 executing, ovewriting the 'regular' SIGINT signal handler. */
3768 initialize_sigint_signal_handler (void)
3770 signal (SIGINT
, handle_remote_sigint
);
3773 /* Signal handler for SIGINT, while the target is executing. */
3775 handle_remote_sigint (int sig
)
3777 signal (sig
, handle_remote_sigint_twice
);
3778 mark_async_signal_handler_wrapper (sigint_remote_token
);
3781 /* Signal handler for SIGINT, installed after SIGINT has already been
3782 sent once. It will take effect the second time that the user sends
3785 handle_remote_sigint_twice (int sig
)
3787 signal (sig
, handle_remote_sigint
);
3788 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
3791 /* Perform the real interruption of the target execution, in response
3794 async_remote_interrupt (gdb_client_data arg
)
3797 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
3799 target_stop (inferior_ptid
);
3802 /* Perform interrupt, if the first attempt did not succeed. Just give
3803 up on the target alltogether. */
3805 async_remote_interrupt_twice (gdb_client_data arg
)
3808 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
3813 /* Reinstall the usual SIGINT handlers, after the target has
3816 cleanup_sigint_signal_handler (void *dummy
)
3818 signal (SIGINT
, handle_sigint
);
3821 /* Send ^C to target to halt it. Target will respond, and send us a
3823 static void (*ofunc
) (int);
3825 /* The command line interface's stop routine. This function is installed
3826 as a signal handler for SIGINT. The first time a user requests a
3827 stop, we call remote_stop to send a break or ^C. If there is no
3828 response from the target (it didn't stop when the user requested it),
3829 we ask the user if he'd like to detach from the target. */
3831 remote_interrupt (int signo
)
3833 /* If this doesn't work, try more severe steps. */
3834 signal (signo
, remote_interrupt_twice
);
3836 gdb_call_async_signal_handler (sigint_remote_token
, 1);
3839 /* The user typed ^C twice. */
3842 remote_interrupt_twice (int signo
)
3844 signal (signo
, ofunc
);
3845 gdb_call_async_signal_handler (sigint_remote_twice_token
, 1);
3846 signal (signo
, remote_interrupt
);
3849 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
3850 thread, all threads of a remote process, or all threads of all
3854 remote_stop_ns (ptid_t ptid
)
3856 struct remote_state
*rs
= get_remote_state ();
3858 char *endp
= rs
->buf
+ get_remote_packet_size ();
3859 struct stop_reply
*reply
, *next
;
3861 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
3862 remote_vcont_probe (rs
);
3864 if (!rs
->support_vCont_t
)
3865 error (_("Remote server does not support stopping threads"));
3867 if (ptid_equal (ptid
, minus_one_ptid
))
3868 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
3873 /* Step inferior_ptid. */
3874 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
3876 if (ptid_is_pid (ptid
))
3877 /* All (-1) threads of process. */
3878 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
3881 /* Small optimization: if we already have a stop reply for
3882 this thread, no use in telling the stub we want this
3884 if (peek_stop_reply (ptid
))
3890 p
= write_ptid (p
, endp
, nptid
);
3893 /* In non-stop, we get an immediate OK reply. The stop reply will
3894 come in asynchronously by notification. */
3896 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3897 if (strcmp (rs
->buf
, "OK") != 0)
3898 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
), rs
->buf
);
3901 /* All-stop version of target_stop. Sends a break or a ^C to stop the
3902 remote target. It is undefined which thread of which process
3903 reports the stop. */
3906 remote_stop_as (ptid_t ptid
)
3908 struct remote_state
*rs
= get_remote_state ();
3910 /* If the inferior is stopped already, but the core didn't know
3911 about it yet, just ignore the request. The cached wait status
3912 will be collected in remote_wait. */
3913 if (rs
->cached_wait_status
)
3916 /* Send a break or a ^C, depending on user preference. */
3919 serial_send_break (remote_desc
);
3921 serial_write (remote_desc
, "\003", 1);
3924 /* This is the generic stop called via the target vector. When a target
3925 interrupt is requested, either by the command line or the GUI, we
3926 will eventually end up here. */
3929 remote_stop (ptid_t ptid
)
3932 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
3935 remote_stop_ns (ptid
);
3937 remote_stop_as (ptid
);
3940 /* Ask the user what to do when an interrupt is received. */
3943 interrupt_query (void)
3945 target_terminal_ours ();
3947 if (target_can_async_p ())
3949 signal (SIGINT
, handle_sigint
);
3950 deprecated_throw_reason (RETURN_QUIT
);
3954 if (query ("Interrupted while waiting for the program.\n\
3955 Give up (and stop debugging it)? "))
3958 deprecated_throw_reason (RETURN_QUIT
);
3962 target_terminal_inferior ();
3965 /* Enable/disable target terminal ownership. Most targets can use
3966 terminal groups to control terminal ownership. Remote targets are
3967 different in that explicit transfer of ownership to/from GDB/target
3971 remote_terminal_inferior (void)
3973 if (!target_async_permitted
)
3974 /* Nothing to do. */
3977 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
3978 sync_execution here. This function should only be called when
3979 GDB is resuming the inferior in the forground. A background
3980 resume (``run&'') should leave GDB in control of the terminal and
3981 consequently should not call this code. */
3982 if (!sync_execution
)
3984 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
3985 calls target_terminal_*() idenpotent. The event-loop GDB talking
3986 to an asynchronous target with a synchronous command calls this
3987 function from both event-top.c and infrun.c/infcmd.c. Once GDB
3988 stops trying to transfer the terminal to the target when it
3989 shouldn't this guard can go away. */
3990 if (!remote_async_terminal_ours_p
)
3992 delete_file_handler (input_fd
);
3993 remote_async_terminal_ours_p
= 0;
3994 initialize_sigint_signal_handler ();
3995 /* NOTE: At this point we could also register our selves as the
3996 recipient of all input. Any characters typed could then be
3997 passed on down to the target. */
4001 remote_terminal_ours (void)
4003 if (!target_async_permitted
)
4004 /* Nothing to do. */
4007 /* See FIXME in remote_terminal_inferior. */
4008 if (!sync_execution
)
4010 /* See FIXME in remote_terminal_inferior. */
4011 if (remote_async_terminal_ours_p
)
4013 cleanup_sigint_signal_handler (NULL
);
4014 add_file_handler (input_fd
, stdin_event_handler
, 0);
4015 remote_async_terminal_ours_p
= 1;
4019 remote_console_output (char *msg
)
4023 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
4026 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
4029 fputs_unfiltered (tb
, gdb_stdtarg
);
4031 gdb_flush (gdb_stdtarg
);
4034 typedef struct cached_reg
4037 gdb_byte data
[MAX_REGISTER_SIZE
];
4040 DEF_VEC_O(cached_reg_t
);
4044 struct stop_reply
*next
;
4048 struct target_waitstatus ws
;
4050 VEC(cached_reg_t
) *regcache
;
4052 int stopped_by_watchpoint_p
;
4053 CORE_ADDR watch_data_address
;
4059 /* The list of already fetched and acknowledged stop events. */
4060 static struct stop_reply
*stop_reply_queue
;
4062 static struct stop_reply
*
4063 stop_reply_xmalloc (void)
4065 struct stop_reply
*r
= XMALLOC (struct stop_reply
);
4071 stop_reply_xfree (struct stop_reply
*r
)
4075 VEC_free (cached_reg_t
, r
->regcache
);
4080 /* Discard all pending stop replies of inferior PID. If PID is -1,
4081 discard everything. */
4084 discard_pending_stop_replies (int pid
)
4086 struct stop_reply
*prev
= NULL
, *reply
, *next
;
4088 /* Discard the in-flight notification. */
4089 if (pending_stop_reply
!= NULL
4091 || ptid_get_pid (pending_stop_reply
->ptid
) == pid
))
4093 stop_reply_xfree (pending_stop_reply
);
4094 pending_stop_reply
= NULL
;
4097 /* Discard the stop replies we have already pulled with
4099 for (reply
= stop_reply_queue
; reply
; reply
= next
)
4103 || ptid_get_pid (reply
->ptid
) == pid
)
4105 if (reply
== stop_reply_queue
)
4106 stop_reply_queue
= reply
->next
;
4108 prev
->next
= reply
->next
;
4110 stop_reply_xfree (reply
);
4117 /* Cleanup wrapper. */
4120 do_stop_reply_xfree (void *arg
)
4122 struct stop_reply
*r
= arg
;
4123 stop_reply_xfree (r
);
4126 /* Look for a queued stop reply belonging to PTID. If one is found,
4127 remove it from the queue, and return it. Returns NULL if none is
4128 found. If there are still queued events left to process, tell the
4129 event loop to get back to target_wait soon. */
4131 static struct stop_reply
*
4132 queued_stop_reply (ptid_t ptid
)
4134 struct stop_reply
*it
, *prev
;
4135 struct stop_reply head
;
4137 head
.next
= stop_reply_queue
;
4142 if (!ptid_equal (ptid
, minus_one_ptid
))
4143 for (; it
; prev
= it
, it
= it
->next
)
4144 if (ptid_equal (ptid
, it
->ptid
))
4149 prev
->next
= it
->next
;
4153 stop_reply_queue
= head
.next
;
4155 if (stop_reply_queue
)
4156 /* There's still at least an event left. */
4157 mark_async_event_handler (remote_async_inferior_event_token
);
4162 /* Push a fully parsed stop reply in the stop reply queue. Since we
4163 know that we now have at least one queued event left to pass to the
4164 core side, tell the event loop to get back to target_wait soon. */
4167 push_stop_reply (struct stop_reply
*new_event
)
4169 struct stop_reply
*event
;
4171 if (stop_reply_queue
)
4173 for (event
= stop_reply_queue
;
4174 event
&& event
->next
;
4175 event
= event
->next
)
4178 event
->next
= new_event
;
4181 stop_reply_queue
= new_event
;
4183 mark_async_event_handler (remote_async_inferior_event_token
);
4186 /* Returns true if we have a stop reply for PTID. */
4189 peek_stop_reply (ptid_t ptid
)
4191 struct stop_reply
*it
;
4193 for (it
= stop_reply_queue
; it
; it
= it
->next
)
4194 if (ptid_equal (ptid
, it
->ptid
))
4196 if (it
->ws
.kind
== TARGET_WAITKIND_STOPPED
)
4203 /* Parse the stop reply in BUF. Either the function succeeds, and the
4204 result is stored in EVENT, or throws an error. */
4207 remote_parse_stop_reply (char *buf
, struct stop_reply
*event
)
4209 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4213 event
->ptid
= null_ptid
;
4214 event
->ws
.kind
= TARGET_WAITKIND_IGNORE
;
4215 event
->ws
.value
.integer
= 0;
4216 event
->solibs_changed
= 0;
4217 event
->replay_event
= 0;
4218 event
->stopped_by_watchpoint_p
= 0;
4219 event
->regcache
= NULL
;
4223 case 'T': /* Status with PC, SP, FP, ... */
4225 gdb_byte regs
[MAX_REGISTER_SIZE
];
4227 /* Expedited reply, containing Signal, {regno, reg} repeat. */
4228 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
4230 n... = register number
4231 r... = register contents
4234 p
= &buf
[3]; /* after Txx */
4242 /* If the packet contains a register number, save it in
4243 pnum and set p1 to point to the character following it.
4244 Otherwise p1 points to p. */
4246 /* If this packet is an awatch packet, don't parse the 'a'
4247 as a register number. */
4249 if (strncmp (p
, "awatch", strlen("awatch")) != 0)
4251 /* Read the ``P'' register number. */
4252 pnum
= strtol (p
, &p_temp
, 16);
4258 if (p1
== p
) /* No register number present here. */
4260 p1
= strchr (p
, ':');
4262 error (_("Malformed packet(a) (missing colon): %s\n\
4265 if (strncmp (p
, "thread", p1
- p
) == 0)
4266 event
->ptid
= read_ptid (++p1
, &p
);
4267 else if ((strncmp (p
, "watch", p1
- p
) == 0)
4268 || (strncmp (p
, "rwatch", p1
- p
) == 0)
4269 || (strncmp (p
, "awatch", p1
- p
) == 0))
4271 event
->stopped_by_watchpoint_p
= 1;
4272 p
= unpack_varlen_hex (++p1
, &addr
);
4273 event
->watch_data_address
= (CORE_ADDR
) addr
;
4275 else if (strncmp (p
, "library", p1
- p
) == 0)
4279 while (*p_temp
&& *p_temp
!= ';')
4282 event
->solibs_changed
= 1;
4285 else if (strncmp (p
, "replaylog", p1
- p
) == 0)
4287 /* NO_HISTORY event.
4288 p1 will indicate "begin" or "end", but
4289 it makes no difference for now, so ignore it. */
4290 event
->replay_event
= 1;
4291 p_temp
= strchr (p1
+ 1, ';');
4297 /* Silently skip unknown optional info. */
4298 p_temp
= strchr (p1
+ 1, ';');
4305 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
4306 cached_reg_t cached_reg
;
4311 error (_("Malformed packet(b) (missing colon): %s\n\
4317 error (_("Remote sent bad register number %s: %s\n\
4319 phex_nz (pnum
, 0), p
, buf
);
4321 cached_reg
.num
= reg
->regnum
;
4323 fieldsize
= hex2bin (p
, cached_reg
.data
,
4324 register_size (target_gdbarch
,
4327 if (fieldsize
< register_size (target_gdbarch
,
4329 warning (_("Remote reply is too short: %s"), buf
);
4331 VEC_safe_push (cached_reg_t
, event
->regcache
, &cached_reg
);
4335 error (_("Remote register badly formatted: %s\nhere: %s"),
4341 case 'S': /* Old style status, just signal only. */
4342 if (event
->solibs_changed
)
4343 event
->ws
.kind
= TARGET_WAITKIND_LOADED
;
4344 else if (event
->replay_event
)
4345 event
->ws
.kind
= TARGET_WAITKIND_NO_HISTORY
;
4348 event
->ws
.kind
= TARGET_WAITKIND_STOPPED
;
4349 event
->ws
.value
.sig
= (enum target_signal
)
4350 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
4353 case 'W': /* Target exited. */
4360 /* GDB used to accept only 2 hex chars here. Stubs should
4361 only send more if they detect GDB supports multi-process
4363 p
= unpack_varlen_hex (&buf
[1], &value
);
4367 /* The remote process exited. */
4368 event
->ws
.kind
= TARGET_WAITKIND_EXITED
;
4369 event
->ws
.value
.integer
= value
;
4373 /* The remote process exited with a signal. */
4374 event
->ws
.kind
= TARGET_WAITKIND_SIGNALLED
;
4375 event
->ws
.value
.sig
= (enum target_signal
) value
;
4378 /* If no process is specified, assume inferior_ptid. */
4379 pid
= ptid_get_pid (inferior_ptid
);
4388 else if (strncmp (p
,
4389 "process:", sizeof ("process:") - 1) == 0)
4392 p
+= sizeof ("process:") - 1;
4393 unpack_varlen_hex (p
, &upid
);
4397 error (_("unknown stop reply packet: %s"), buf
);
4400 error (_("unknown stop reply packet: %s"), buf
);
4401 event
->ptid
= pid_to_ptid (pid
);
4406 if (non_stop
&& ptid_equal (event
->ptid
, null_ptid
))
4407 error (_("No process or thread specified in stop reply: %s"), buf
);
4410 /* When the stub wants to tell GDB about a new stop reply, it sends a
4411 stop notification (%Stop). Those can come it at any time, hence,
4412 we have to make sure that any pending putpkt/getpkt sequence we're
4413 making is finished, before querying the stub for more events with
4414 vStopped. E.g., if we started a vStopped sequence immediatelly
4415 upon receiving the %Stop notification, something like this could
4423 1.6) <-- (registers reply to step #1.3)
4425 Obviously, the reply in step #1.6 would be unexpected to a vStopped
4428 To solve this, whenever we parse a %Stop notification sucessfully,
4429 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
4430 doing whatever we were doing:
4436 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
4437 2.5) <-- (registers reply to step #2.3)
4439 Eventualy after step #2.5, we return to the event loop, which
4440 notices there's an event on the
4441 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
4442 associated callback --- the function below. At this point, we're
4443 always safe to start a vStopped sequence. :
4446 2.7) <-- T05 thread:2
4452 remote_get_pending_stop_replies (void)
4454 struct remote_state
*rs
= get_remote_state ();
4457 if (pending_stop_reply
)
4460 putpkt ("vStopped");
4462 /* Now we can rely on it. */
4463 push_stop_reply (pending_stop_reply
);
4464 pending_stop_reply
= NULL
;
4468 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4469 if (strcmp (rs
->buf
, "OK") == 0)
4473 struct cleanup
*old_chain
;
4474 struct stop_reply
*stop_reply
= stop_reply_xmalloc ();
4476 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4477 remote_parse_stop_reply (rs
->buf
, stop_reply
);
4480 putpkt ("vStopped");
4482 if (stop_reply
->ws
.kind
!= TARGET_WAITKIND_IGNORE
)
4484 /* Now we can rely on it. */
4485 discard_cleanups (old_chain
);
4486 push_stop_reply (stop_reply
);
4489 /* We got an unknown stop reply. */
4490 do_cleanups (old_chain
);
4497 /* Called when it is decided that STOP_REPLY holds the info of the
4498 event that is to be returned to the core. This function always
4499 destroys STOP_REPLY. */
4502 process_stop_reply (struct stop_reply
*stop_reply
,
4503 struct target_waitstatus
*status
)
4507 *status
= stop_reply
->ws
;
4508 ptid
= stop_reply
->ptid
;
4510 /* If no thread/process was reported by the stub, assume the current
4512 if (ptid_equal (ptid
, null_ptid
))
4513 ptid
= inferior_ptid
;
4515 if (status
->kind
!= TARGET_WAITKIND_EXITED
4516 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
4518 notice_new_inferiors (ptid
);
4520 /* Expedited registers. */
4521 if (stop_reply
->regcache
)
4527 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
4529 regcache_raw_supply (get_thread_regcache (ptid
),
4530 reg
->num
, reg
->data
);
4531 VEC_free (cached_reg_t
, stop_reply
->regcache
);
4534 remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
4535 remote_watch_data_address
= stop_reply
->watch_data_address
;
4538 stop_reply_xfree (stop_reply
);
4542 /* The non-stop mode version of target_wait. */
4545 remote_wait_ns (ptid_t ptid
, struct target_waitstatus
*status
)
4547 struct remote_state
*rs
= get_remote_state ();
4548 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4549 ptid_t event_ptid
= null_ptid
;
4550 struct stop_reply
*stop_reply
;
4553 /* If in non-stop mode, get out of getpkt even if a
4554 notification is received. */
4556 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
4563 case 'E': /* Error of some sort. */
4564 /* We're out of sync with the target now. Did it continue
4565 or not? We can't tell which thread it was in non-stop,
4566 so just ignore this. */
4567 warning (_("Remote failure reply: %s"), rs
->buf
);
4569 case 'O': /* Console output. */
4570 remote_console_output (rs
->buf
+ 1);
4573 warning (_("Invalid remote reply: %s"), rs
->buf
);
4577 /* Acknowledge a pending stop reply that may have arrived in the
4579 if (pending_stop_reply
!= NULL
)
4580 remote_get_pending_stop_replies ();
4582 /* If indeed we noticed a stop reply, we're done. */
4583 stop_reply
= queued_stop_reply (ptid
);
4584 if (stop_reply
!= NULL
)
4585 return process_stop_reply (stop_reply
, status
);
4587 /* Still no event. If we're in asynchronous mode, then just
4588 return to the event loop. */
4589 if (remote_is_async_p ())
4591 status
->kind
= TARGET_WAITKIND_IGNORE
;
4592 return minus_one_ptid
;
4595 /* Otherwise, asynchronous mode is masked, so do a blocking
4597 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
4602 /* Wait until the remote machine stops, then return, storing status in
4603 STATUS just as `wait' would. */
4606 remote_wait_as (ptid_t ptid
, struct target_waitstatus
*status
)
4608 struct remote_state
*rs
= get_remote_state ();
4609 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4610 ptid_t event_ptid
= null_ptid
;
4612 int solibs_changed
= 0;
4614 struct stop_reply
*stop_reply
;
4616 status
->kind
= TARGET_WAITKIND_IGNORE
;
4617 status
->value
.integer
= 0;
4619 stop_reply
= queued_stop_reply (ptid
);
4620 if (stop_reply
!= NULL
)
4621 return process_stop_reply (stop_reply
, status
);
4623 if (rs
->cached_wait_status
)
4624 /* Use the cached wait status, but only once. */
4625 rs
->cached_wait_status
= 0;
4630 if (!target_is_async_p ())
4632 ofunc
= signal (SIGINT
, remote_interrupt
);
4633 /* If the user hit C-c before this packet, or between packets,
4634 pretend that it was hit right here. */
4638 remote_interrupt (SIGINT
);
4642 /* FIXME: cagney/1999-09-27: If we're in async mode we should
4643 _never_ wait for ever -> test on target_is_async_p().
4644 However, before we do that we need to ensure that the caller
4645 knows how to take the target into/out of async mode. */
4646 ret
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
4647 if (!target_is_async_p ())
4648 signal (SIGINT
, ofunc
);
4653 remote_stopped_by_watchpoint_p
= 0;
4655 /* We got something. */
4656 rs
->waiting_for_stop_reply
= 0;
4660 case 'E': /* Error of some sort. */
4661 /* We're out of sync with the target now. Did it continue or
4662 not? Not is more likely, so report a stop. */
4663 warning (_("Remote failure reply: %s"), buf
);
4664 status
->kind
= TARGET_WAITKIND_STOPPED
;
4665 status
->value
.sig
= TARGET_SIGNAL_0
;
4667 case 'F': /* File-I/O request. */
4668 remote_fileio_request (buf
);
4670 case 'T': case 'S': case 'X': case 'W':
4672 struct stop_reply
*stop_reply
;
4673 struct cleanup
*old_chain
;
4675 stop_reply
= stop_reply_xmalloc ();
4676 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4677 remote_parse_stop_reply (buf
, stop_reply
);
4678 discard_cleanups (old_chain
);
4679 event_ptid
= process_stop_reply (stop_reply
, status
);
4682 case 'O': /* Console output. */
4683 remote_console_output (buf
+ 1);
4685 /* The target didn't really stop; keep waiting. */
4686 rs
->waiting_for_stop_reply
= 1;
4690 if (last_sent_signal
!= TARGET_SIGNAL_0
)
4692 /* Zero length reply means that we tried 'S' or 'C' and the
4693 remote system doesn't support it. */
4694 target_terminal_ours_for_output ();
4696 ("Can't send signals to this remote system. %s not sent.\n",
4697 target_signal_to_name (last_sent_signal
));
4698 last_sent_signal
= TARGET_SIGNAL_0
;
4699 target_terminal_inferior ();
4701 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
4702 putpkt ((char *) buf
);
4704 /* We just told the target to resume, so a stop reply is in
4706 rs
->waiting_for_stop_reply
= 1;
4709 /* else fallthrough */
4711 warning (_("Invalid remote reply: %s"), buf
);
4713 rs
->waiting_for_stop_reply
= 1;
4717 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
4718 /* Nothing interesting happened. */
4719 return minus_one_ptid
;
4720 else if (status
->kind
!= TARGET_WAITKIND_EXITED
4721 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
4723 if (!ptid_equal (event_ptid
, null_ptid
))
4724 record_currthread (event_ptid
);
4726 event_ptid
= inferior_ptid
;
4729 /* A process exit. Invalidate our notion of current thread. */
4730 record_currthread (minus_one_ptid
);
4735 /* Wait until the remote machine stops, then return, storing status in
4736 STATUS just as `wait' would. */
4739 remote_wait (struct target_ops
*ops
,
4740 ptid_t ptid
, struct target_waitstatus
*status
)
4745 event_ptid
= remote_wait_ns (ptid
, status
);
4748 /* In synchronous mode, keep waiting until the target stops. In
4749 asynchronous mode, always return to the event loop. */
4753 event_ptid
= remote_wait_as (ptid
, status
);
4755 while (status
->kind
== TARGET_WAITKIND_IGNORE
4756 && !target_can_async_p ());
4759 if (target_can_async_p ())
4761 /* If there are are events left in the queue tell the event loop
4763 if (stop_reply_queue
)
4764 mark_async_event_handler (remote_async_inferior_event_token
);
4770 /* Fetch a single register using a 'p' packet. */
4773 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
4775 struct remote_state
*rs
= get_remote_state ();
4777 char regp
[MAX_REGISTER_SIZE
];
4780 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
4783 if (reg
->pnum
== -1)
4788 p
+= hexnumstr (p
, reg
->pnum
);
4790 remote_send (&rs
->buf
, &rs
->buf_size
);
4794 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
4798 case PACKET_UNKNOWN
:
4801 error (_("Could not fetch register \"%s\""),
4802 gdbarch_register_name (get_regcache_arch (regcache
), reg
->regnum
));
4805 /* If this register is unfetchable, tell the regcache. */
4808 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
4812 /* Otherwise, parse and supply the value. */
4818 error (_("fetch_register_using_p: early buf termination"));
4820 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
4823 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
4827 /* Fetch the registers included in the target's 'g' packet. */
4830 send_g_packet (void)
4832 struct remote_state
*rs
= get_remote_state ();
4837 sprintf (rs
->buf
, "g");
4838 remote_send (&rs
->buf
, &rs
->buf_size
);
4840 /* We can get out of synch in various cases. If the first character
4841 in the buffer is not a hex character, assume that has happened
4842 and try to fetch another packet to read. */
4843 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
4844 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
4845 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
4846 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
4849 fprintf_unfiltered (gdb_stdlog
,
4850 "Bad register packet; fetching a new packet\n");
4851 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4854 buf_len
= strlen (rs
->buf
);
4856 /* Sanity check the received packet. */
4857 if (buf_len
% 2 != 0)
4858 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
4864 process_g_packet (struct regcache
*regcache
)
4866 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
4867 struct remote_state
*rs
= get_remote_state ();
4868 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4873 buf_len
= strlen (rs
->buf
);
4875 /* Further sanity checks, with knowledge of the architecture. */
4876 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
4877 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
4879 /* Save the size of the packet sent to us by the target. It is used
4880 as a heuristic when determining the max size of packets that the
4881 target can safely receive. */
4882 if (rsa
->actual_register_packet_size
== 0)
4883 rsa
->actual_register_packet_size
= buf_len
;
4885 /* If this is smaller than we guessed the 'g' packet would be,
4886 update our records. A 'g' reply that doesn't include a register's
4887 value implies either that the register is not available, or that
4888 the 'p' packet must be used. */
4889 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
4891 rsa
->sizeof_g_packet
= buf_len
/ 2;
4893 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
4895 if (rsa
->regs
[i
].pnum
== -1)
4898 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
4899 rsa
->regs
[i
].in_g_packet
= 0;
4901 rsa
->regs
[i
].in_g_packet
= 1;
4905 regs
= alloca (rsa
->sizeof_g_packet
);
4907 /* Unimplemented registers read as all bits zero. */
4908 memset (regs
, 0, rsa
->sizeof_g_packet
);
4910 /* Reply describes registers byte by byte, each byte encoded as two
4911 hex characters. Suck them all up, then supply them to the
4912 register cacheing/storage mechanism. */
4915 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
4917 if (p
[0] == 0 || p
[1] == 0)
4918 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
4919 internal_error (__FILE__
, __LINE__
,
4920 "unexpected end of 'g' packet reply");
4922 if (p
[0] == 'x' && p
[1] == 'x')
4923 regs
[i
] = 0; /* 'x' */
4925 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
4931 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
4933 struct packet_reg
*r
= &rsa
->regs
[i
];
4936 if (r
->offset
* 2 >= strlen (rs
->buf
))
4937 /* This shouldn't happen - we adjusted in_g_packet above. */
4938 internal_error (__FILE__
, __LINE__
,
4939 "unexpected end of 'g' packet reply");
4940 else if (rs
->buf
[r
->offset
* 2] == 'x')
4942 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
4943 /* The register isn't available, mark it as such (at
4944 the same time setting the value to zero). */
4945 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
4948 regcache_raw_supply (regcache
, r
->regnum
,
4956 fetch_registers_using_g (struct regcache
*regcache
)
4959 process_g_packet (regcache
);
4963 remote_fetch_registers (struct regcache
*regcache
, int regnum
)
4965 struct remote_state
*rs
= get_remote_state ();
4966 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4969 set_general_thread (inferior_ptid
);
4973 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
4974 gdb_assert (reg
!= NULL
);
4976 /* If this register might be in the 'g' packet, try that first -
4977 we are likely to read more than one register. If this is the
4978 first 'g' packet, we might be overly optimistic about its
4979 contents, so fall back to 'p'. */
4980 if (reg
->in_g_packet
)
4982 fetch_registers_using_g (regcache
);
4983 if (reg
->in_g_packet
)
4987 if (fetch_register_using_p (regcache
, reg
))
4990 /* This register is not available. */
4991 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
4996 fetch_registers_using_g (regcache
);
4998 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
4999 if (!rsa
->regs
[i
].in_g_packet
)
5000 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
5002 /* This register is not available. */
5003 regcache_raw_supply (regcache
, i
, NULL
);
5007 /* Prepare to store registers. Since we may send them all (using a
5008 'G' request), we have to read out the ones we don't want to change
5012 remote_prepare_to_store (struct regcache
*regcache
)
5014 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5016 gdb_byte buf
[MAX_REGISTER_SIZE
];
5018 /* Make sure the entire registers array is valid. */
5019 switch (remote_protocol_packets
[PACKET_P
].support
)
5021 case PACKET_DISABLE
:
5022 case PACKET_SUPPORT_UNKNOWN
:
5023 /* Make sure all the necessary registers are cached. */
5024 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5025 if (rsa
->regs
[i
].in_g_packet
)
5026 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
5033 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
5034 packet was not recognized. */
5037 store_register_using_P (const struct regcache
*regcache
, struct packet_reg
*reg
)
5039 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5040 struct remote_state
*rs
= get_remote_state ();
5041 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5042 /* Try storing a single register. */
5043 char *buf
= rs
->buf
;
5044 gdb_byte regp
[MAX_REGISTER_SIZE
];
5047 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
5050 if (reg
->pnum
== -1)
5053 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
5054 p
= buf
+ strlen (buf
);
5055 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
5056 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
5057 remote_send (&rs
->buf
, &rs
->buf_size
);
5059 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
5064 error (_("Could not write register \"%s\""),
5065 gdbarch_register_name (gdbarch
, reg
->regnum
));
5066 case PACKET_UNKNOWN
:
5069 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
5073 /* Store register REGNUM, or all registers if REGNUM == -1, from the
5074 contents of the register cache buffer. FIXME: ignores errors. */
5077 store_registers_using_G (const struct regcache
*regcache
)
5079 struct remote_state
*rs
= get_remote_state ();
5080 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5084 /* Extract all the registers in the regcache copying them into a
5088 regs
= alloca (rsa
->sizeof_g_packet
);
5089 memset (regs
, 0, rsa
->sizeof_g_packet
);
5090 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5092 struct packet_reg
*r
= &rsa
->regs
[i
];
5094 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
5098 /* Command describes registers byte by byte,
5099 each byte encoded as two hex characters. */
5102 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
5104 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
5105 remote_send (&rs
->buf
, &rs
->buf_size
);
5108 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
5109 of the register cache buffer. FIXME: ignores errors. */
5112 remote_store_registers (struct regcache
*regcache
, int regnum
)
5114 struct remote_state
*rs
= get_remote_state ();
5115 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5118 set_general_thread (inferior_ptid
);
5122 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5123 gdb_assert (reg
!= NULL
);
5125 /* Always prefer to store registers using the 'P' packet if
5126 possible; we often change only a small number of registers.
5127 Sometimes we change a larger number; we'd need help from a
5128 higher layer to know to use 'G'. */
5129 if (store_register_using_P (regcache
, reg
))
5132 /* For now, don't complain if we have no way to write the
5133 register. GDB loses track of unavailable registers too
5134 easily. Some day, this may be an error. We don't have
5135 any way to read the register, either... */
5136 if (!reg
->in_g_packet
)
5139 store_registers_using_G (regcache
);
5143 store_registers_using_G (regcache
);
5145 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5146 if (!rsa
->regs
[i
].in_g_packet
)
5147 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
5148 /* See above for why we do not issue an error here. */
5153 /* Return the number of hex digits in num. */
5156 hexnumlen (ULONGEST num
)
5160 for (i
= 0; num
!= 0; i
++)
5166 /* Set BUF to the minimum number of hex digits representing NUM. */
5169 hexnumstr (char *buf
, ULONGEST num
)
5171 int len
= hexnumlen (num
);
5172 return hexnumnstr (buf
, num
, len
);
5176 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
5179 hexnumnstr (char *buf
, ULONGEST num
, int width
)
5185 for (i
= width
- 1; i
>= 0; i
--)
5187 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
5194 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
5197 remote_address_masked (CORE_ADDR addr
)
5199 int address_size
= remote_address_size
;
5200 /* If "remoteaddresssize" was not set, default to target address size. */
5202 address_size
= gdbarch_addr_bit (target_gdbarch
);
5204 if (address_size
> 0
5205 && address_size
< (sizeof (ULONGEST
) * 8))
5207 /* Only create a mask when that mask can safely be constructed
5208 in a ULONGEST variable. */
5210 mask
= (mask
<< address_size
) - 1;
5216 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
5217 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
5218 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
5219 (which may be more than *OUT_LEN due to escape characters). The
5220 total number of bytes in the output buffer will be at most
5224 remote_escape_output (const gdb_byte
*buffer
, int len
,
5225 gdb_byte
*out_buf
, int *out_len
,
5228 int input_index
, output_index
;
5231 for (input_index
= 0; input_index
< len
; input_index
++)
5233 gdb_byte b
= buffer
[input_index
];
5235 if (b
== '$' || b
== '#' || b
== '}')
5237 /* These must be escaped. */
5238 if (output_index
+ 2 > out_maxlen
)
5240 out_buf
[output_index
++] = '}';
5241 out_buf
[output_index
++] = b
^ 0x20;
5245 if (output_index
+ 1 > out_maxlen
)
5247 out_buf
[output_index
++] = b
;
5251 *out_len
= input_index
;
5252 return output_index
;
5255 /* Convert BUFFER, escaped data LEN bytes long, into binary data
5256 in OUT_BUF. Return the number of bytes written to OUT_BUF.
5257 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
5259 This function reverses remote_escape_output. It allows more
5260 escaped characters than that function does, in particular because
5261 '*' must be escaped to avoid the run-length encoding processing
5262 in reading packets. */
5265 remote_unescape_input (const gdb_byte
*buffer
, int len
,
5266 gdb_byte
*out_buf
, int out_maxlen
)
5268 int input_index
, output_index
;
5273 for (input_index
= 0; input_index
< len
; input_index
++)
5275 gdb_byte b
= buffer
[input_index
];
5277 if (output_index
+ 1 > out_maxlen
)
5279 warning (_("Received too much data from remote target;"
5280 " ignoring overflow."));
5281 return output_index
;
5286 out_buf
[output_index
++] = b
^ 0x20;
5292 out_buf
[output_index
++] = b
;
5296 error (_("Unmatched escape character in target response."));
5298 return output_index
;
5301 /* Determine whether the remote target supports binary downloading.
5302 This is accomplished by sending a no-op memory write of zero length
5303 to the target at the specified address. It does not suffice to send
5304 the whole packet, since many stubs strip the eighth bit and
5305 subsequently compute a wrong checksum, which causes real havoc with
5308 NOTE: This can still lose if the serial line is not eight-bit
5309 clean. In cases like this, the user should clear "remote
5313 check_binary_download (CORE_ADDR addr
)
5315 struct remote_state
*rs
= get_remote_state ();
5317 switch (remote_protocol_packets
[PACKET_X
].support
)
5319 case PACKET_DISABLE
:
5323 case PACKET_SUPPORT_UNKNOWN
:
5329 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5331 p
+= hexnumstr (p
, (ULONGEST
) 0);
5335 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
5336 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5338 if (rs
->buf
[0] == '\0')
5341 fprintf_unfiltered (gdb_stdlog
,
5342 "binary downloading NOT suppported by target\n");
5343 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
5348 fprintf_unfiltered (gdb_stdlog
,
5349 "binary downloading suppported by target\n");
5350 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
5357 /* Write memory data directly to the remote machine.
5358 This does not inform the data cache; the data cache uses this.
5359 HEADER is the starting part of the packet.
5360 MEMADDR is the address in the remote memory space.
5361 MYADDR is the address of the buffer in our space.
5362 LEN is the number of bytes.
5363 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
5364 should send data as binary ('X'), or hex-encoded ('M').
5366 The function creates packet of the form
5367 <HEADER><ADDRESS>,<LENGTH>:<DATA>
5369 where encoding of <DATA> is termined by PACKET_FORMAT.
5371 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
5374 Returns the number of bytes transferred, or 0 (setting errno) for
5375 error. Only transfer a single packet. */
5378 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
5379 const gdb_byte
*myaddr
, int len
,
5380 char packet_format
, int use_length
)
5382 struct remote_state
*rs
= get_remote_state ();
5392 if (packet_format
!= 'X' && packet_format
!= 'M')
5393 internal_error (__FILE__
, __LINE__
,
5394 "remote_write_bytes_aux: bad packet format");
5399 payload_size
= get_memory_write_packet_size ();
5401 /* The packet buffer will be large enough for the payload;
5402 get_memory_packet_size ensures this. */
5405 /* Compute the size of the actual payload by subtracting out the
5406 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
5408 payload_size
-= strlen ("$,:#NN");
5410 /* The comma won't be used. */
5412 header_length
= strlen (header
);
5413 payload_size
-= header_length
;
5414 payload_size
-= hexnumlen (memaddr
);
5416 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
5418 strcat (rs
->buf
, header
);
5419 p
= rs
->buf
+ strlen (header
);
5421 /* Compute a best guess of the number of bytes actually transfered. */
5422 if (packet_format
== 'X')
5424 /* Best guess at number of bytes that will fit. */
5425 todo
= min (len
, payload_size
);
5427 payload_size
-= hexnumlen (todo
);
5428 todo
= min (todo
, payload_size
);
5432 /* Num bytes that will fit. */
5433 todo
= min (len
, payload_size
/ 2);
5435 payload_size
-= hexnumlen (todo
);
5436 todo
= min (todo
, payload_size
/ 2);
5440 internal_error (__FILE__
, __LINE__
,
5441 _("minumum packet size too small to write data"));
5443 /* If we already need another packet, then try to align the end
5444 of this packet to a useful boundary. */
5445 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
5446 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
5448 /* Append "<memaddr>". */
5449 memaddr
= remote_address_masked (memaddr
);
5450 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
5457 /* Append <len>. Retain the location/size of <len>. It may need to
5458 be adjusted once the packet body has been created. */
5460 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
5468 /* Append the packet body. */
5469 if (packet_format
== 'X')
5471 /* Binary mode. Send target system values byte by byte, in
5472 increasing byte addresses. Only escape certain critical
5474 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
5477 /* If not all TODO bytes fit, then we'll need another packet. Make
5478 a second try to keep the end of the packet aligned. Don't do
5479 this if the packet is tiny. */
5480 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
5484 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
5486 if (new_nr_bytes
!= nr_bytes
)
5487 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
5492 p
+= payload_length
;
5493 if (use_length
&& nr_bytes
< todo
)
5495 /* Escape chars have filled up the buffer prematurely,
5496 and we have actually sent fewer bytes than planned.
5497 Fix-up the length field of the packet. Use the same
5498 number of characters as before. */
5499 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
5500 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
5505 /* Normal mode: Send target system values byte by byte, in
5506 increasing byte addresses. Each byte is encoded as a two hex
5508 nr_bytes
= bin2hex (myaddr
, p
, todo
);
5512 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
5513 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5515 if (rs
->buf
[0] == 'E')
5517 /* There is no correspondance between what the remote protocol
5518 uses for errors and errno codes. We would like a cleaner way
5519 of representing errors (big enough to include errno codes,
5520 bfd_error codes, and others). But for now just return EIO. */
5525 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
5526 fewer bytes than we'd planned. */
5530 /* Write memory data directly to the remote machine.
5531 This does not inform the data cache; the data cache uses this.
5532 MEMADDR is the address in the remote memory space.
5533 MYADDR is the address of the buffer in our space.
5534 LEN is the number of bytes.
5536 Returns number of bytes transferred, or 0 (setting errno) for
5537 error. Only transfer a single packet. */
5540 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
5542 char *packet_format
= 0;
5544 /* Check whether the target supports binary download. */
5545 check_binary_download (memaddr
);
5547 switch (remote_protocol_packets
[PACKET_X
].support
)
5550 packet_format
= "X";
5552 case PACKET_DISABLE
:
5553 packet_format
= "M";
5555 case PACKET_SUPPORT_UNKNOWN
:
5556 internal_error (__FILE__
, __LINE__
,
5557 _("remote_write_bytes: bad internal state"));
5559 internal_error (__FILE__
, __LINE__
, _("bad switch"));
5562 return remote_write_bytes_aux (packet_format
,
5563 memaddr
, myaddr
, len
, packet_format
[0], 1);
5566 /* Read memory data directly from the remote machine.
5567 This does not use the data cache; the data cache uses this.
5568 MEMADDR is the address in the remote memory space.
5569 MYADDR is the address of the buffer in our space.
5570 LEN is the number of bytes.
5572 Returns number of bytes transferred, or 0 for error. */
5574 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
5575 remote targets) shouldn't attempt to read the entire buffer.
5576 Instead it should read a single packet worth of data and then
5577 return the byte size of that packet to the caller. The caller (its
5578 caller and its callers caller ;-) already contains code for
5579 handling partial reads. */
5582 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
5584 struct remote_state
*rs
= get_remote_state ();
5585 int max_buf_size
; /* Max size of packet output buffer. */
5591 max_buf_size
= get_memory_read_packet_size ();
5592 /* The packet buffer will be large enough for the payload;
5593 get_memory_packet_size ensures this. */
5602 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
5604 /* construct "m"<memaddr>","<len>" */
5605 /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */
5606 memaddr
= remote_address_masked (memaddr
);
5609 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
5611 p
+= hexnumstr (p
, (ULONGEST
) todo
);
5615 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5617 if (rs
->buf
[0] == 'E'
5618 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
5619 && rs
->buf
[3] == '\0')
5621 /* There is no correspondance between what the remote
5622 protocol uses for errors and errno codes. We would like
5623 a cleaner way of representing errors (big enough to
5624 include errno codes, bfd_error codes, and others). But
5625 for now just return EIO. */
5630 /* Reply describes memory byte by byte,
5631 each byte encoded as two hex characters. */
5634 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
5636 /* Reply is short. This means that we were able to read
5637 only part of what we wanted to. */
5638 return i
+ (origlen
- len
);
5648 /* Remote notification handler. */
5651 handle_notification (char *buf
, size_t length
)
5653 if (strncmp (buf
, "Stop:", 5) == 0)
5655 if (pending_stop_reply
)
5656 /* We've already parsed the in-flight stop-reply, but the stub
5657 for some reason thought we didn't, possibly due to timeout
5658 on its side. Just ignore it. */
5662 struct cleanup
*old_chain
;
5663 struct stop_reply
*reply
= stop_reply_xmalloc ();
5664 old_chain
= make_cleanup (do_stop_reply_xfree
, reply
);
5666 remote_parse_stop_reply (buf
+ 5, reply
);
5668 discard_cleanups (old_chain
);
5670 /* Be careful to only set it after parsing, since an error
5671 may be thrown then. */
5672 pending_stop_reply
= reply
;
5674 /* Notify the event loop there's a stop reply to acknowledge
5675 and that there may be more events to fetch. */
5676 mark_async_event_handler (remote_async_get_pending_events_token
);
5680 /* We ignore notifications we don't recognize, for compatibility
5681 with newer stubs. */
5686 /* Read or write LEN bytes from inferior memory at MEMADDR,
5687 transferring to or from debugger address BUFFER. Write to inferior
5688 if SHOULD_WRITE is nonzero. Returns length of data written or
5689 read; 0 for error. TARGET is unused. */
5692 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
5693 int should_write
, struct mem_attrib
*attrib
,
5694 struct target_ops
*target
)
5698 set_general_thread (inferior_ptid
);
5701 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
5703 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
5708 /* Sends a packet with content determined by the printf format string
5709 FORMAT and the remaining arguments, then gets the reply. Returns
5710 whether the packet was a success, a failure, or unknown. */
5712 static enum packet_result
5713 remote_send_printf (const char *format
, ...)
5715 struct remote_state
*rs
= get_remote_state ();
5716 int max_size
= get_remote_packet_size ();
5719 va_start (ap
, format
);
5722 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
5723 internal_error (__FILE__
, __LINE__
, "Too long remote packet.");
5725 if (putpkt (rs
->buf
) < 0)
5726 error (_("Communication problem with target."));
5729 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5731 return packet_check_result (rs
->buf
);
5735 restore_remote_timeout (void *p
)
5737 int value
= *(int *)p
;
5738 remote_timeout
= value
;
5741 /* Flash writing can take quite some time. We'll set
5742 effectively infinite timeout for flash operations.
5743 In future, we'll need to decide on a better approach. */
5744 static const int remote_flash_timeout
= 1000;
5747 remote_flash_erase (struct target_ops
*ops
,
5748 ULONGEST address
, LONGEST length
)
5750 int saved_remote_timeout
= remote_timeout
;
5751 enum packet_result ret
;
5753 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5754 &saved_remote_timeout
);
5755 remote_timeout
= remote_flash_timeout
;
5757 ret
= remote_send_printf ("vFlashErase:%s,%s",
5762 case PACKET_UNKNOWN
:
5763 error (_("Remote target does not support flash erase"));
5765 error (_("Error erasing flash with vFlashErase packet"));
5770 do_cleanups (back_to
);
5774 remote_flash_write (struct target_ops
*ops
,
5775 ULONGEST address
, LONGEST length
,
5776 const gdb_byte
*data
)
5778 int saved_remote_timeout
= remote_timeout
;
5780 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5781 &saved_remote_timeout
);
5783 remote_timeout
= remote_flash_timeout
;
5784 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
5785 do_cleanups (back_to
);
5791 remote_flash_done (struct target_ops
*ops
)
5793 int saved_remote_timeout
= remote_timeout
;
5795 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5796 &saved_remote_timeout
);
5798 remote_timeout
= remote_flash_timeout
;
5799 ret
= remote_send_printf ("vFlashDone");
5800 do_cleanups (back_to
);
5804 case PACKET_UNKNOWN
:
5805 error (_("Remote target does not support vFlashDone"));
5807 error (_("Error finishing flash operation"));
5814 remote_files_info (struct target_ops
*ignore
)
5816 puts_filtered ("Debugging a target over a serial line.\n");
5819 /* Stuff for dealing with the packets which are part of this protocol.
5820 See comment at top of file for details. */
5822 /* Read a single character from the remote end. */
5825 readchar (int timeout
)
5829 ch
= serial_readchar (remote_desc
, timeout
);
5834 switch ((enum serial_rc
) ch
)
5838 error (_("Remote connection closed"));
5841 perror_with_name (_("Remote communication error"));
5843 case SERIAL_TIMEOUT
:
5849 /* Send the command in *BUF to the remote machine, and read the reply
5850 into *BUF. Report an error if we get an error reply. Resize
5851 *BUF using xrealloc if necessary to hold the result, and update
5855 remote_send (char **buf
,
5859 getpkt (buf
, sizeof_buf
, 0);
5861 if ((*buf
)[0] == 'E')
5862 error (_("Remote failure reply: %s"), *buf
);
5865 /* Return a pointer to an xmalloc'ed string representing an escaped
5866 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
5867 etc. The caller is responsible for releasing the returned
5871 escape_buffer (const char *buf
, int n
)
5873 struct cleanup
*old_chain
;
5874 struct ui_file
*stb
;
5878 stb
= mem_fileopen ();
5879 old_chain
= make_cleanup_ui_file_delete (stb
);
5881 fputstrn_unfiltered (buf
, n
, 0, stb
);
5882 str
= ui_file_xstrdup (stb
, &length
);
5883 do_cleanups (old_chain
);
5887 /* Display a null-terminated packet on stdout, for debugging, using C
5891 print_packet (char *buf
)
5893 puts_filtered ("\"");
5894 fputstr_filtered (buf
, '"', gdb_stdout
);
5895 puts_filtered ("\"");
5901 return putpkt_binary (buf
, strlen (buf
));
5904 /* Send a packet to the remote machine, with error checking. The data
5905 of the packet is in BUF. The string in BUF can be at most
5906 get_remote_packet_size () - 5 to account for the $, # and checksum,
5907 and for a possible /0 if we are debugging (remote_debug) and want
5908 to print the sent packet as a string. */
5911 putpkt_binary (char *buf
, int cnt
)
5913 struct remote_state
*rs
= get_remote_state ();
5915 unsigned char csum
= 0;
5916 char *buf2
= alloca (cnt
+ 6);
5922 /* Catch cases like trying to read memory or listing threads while
5923 we're waiting for a stop reply. The remote server wouldn't be
5924 ready to handle this request, so we'd hang and timeout. We don't
5925 have to worry about this in synchronous mode, because in that
5926 case it's not possible to issue a command while the target is
5927 running. This is not a problem in non-stop mode, because in that
5928 case, the stub is always ready to process serial input. */
5929 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
5930 error (_("Cannot execute this command while the target is running."));
5932 /* We're sending out a new packet. Make sure we don't look at a
5933 stale cached response. */
5934 rs
->cached_wait_status
= 0;
5936 /* Copy the packet into buffer BUF2, encapsulating it
5937 and giving it a checksum. */
5942 for (i
= 0; i
< cnt
; i
++)
5948 *p
++ = tohex ((csum
>> 4) & 0xf);
5949 *p
++ = tohex (csum
& 0xf);
5951 /* Send it over and over until we get a positive ack. */
5955 int started_error_output
= 0;
5959 struct cleanup
*old_chain
;
5963 str
= escape_buffer (buf2
, p
- buf2
);
5964 old_chain
= make_cleanup (xfree
, str
);
5965 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", str
);
5966 gdb_flush (gdb_stdlog
);
5967 do_cleanups (old_chain
);
5969 if (serial_write (remote_desc
, buf2
, p
- buf2
))
5970 perror_with_name (_("putpkt: write failed"));
5972 /* If this is a no acks version of the remote protocol, send the
5973 packet and move on. */
5977 /* Read until either a timeout occurs (-2) or '+' is read.
5978 Handle any notification that arrives in the mean time. */
5981 ch
= readchar (remote_timeout
);
5989 case SERIAL_TIMEOUT
:
5992 if (started_error_output
)
5994 putchar_unfiltered ('\n');
5995 started_error_output
= 0;
6004 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
6008 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
6009 case SERIAL_TIMEOUT
:
6013 break; /* Retransmit buffer. */
6017 fprintf_unfiltered (gdb_stdlog
,
6018 "Packet instead of Ack, ignoring it\n");
6019 /* It's probably an old response sent because an ACK
6020 was lost. Gobble up the packet and ack it so it
6021 doesn't get retransmitted when we resend this
6024 serial_write (remote_desc
, "+", 1);
6025 continue; /* Now, go look for +. */
6032 /* If we got a notification, handle it, and go back to looking
6034 /* We've found the start of a notification. Now
6035 collect the data. */
6036 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
6041 struct cleanup
*old_chain
;
6044 str
= escape_buffer (rs
->buf
, val
);
6045 old_chain
= make_cleanup (xfree
, str
);
6046 fprintf_unfiltered (gdb_stdlog
,
6047 " Notification received: %s\n",
6049 do_cleanups (old_chain
);
6051 handle_notification (rs
->buf
, val
);
6052 /* We're in sync now, rewait for the ack. */
6059 if (!started_error_output
)
6061 started_error_output
= 1;
6062 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6064 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6065 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
6074 if (!started_error_output
)
6076 started_error_output
= 1;
6077 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6079 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6083 break; /* Here to retransmit. */
6087 /* This is wrong. If doing a long backtrace, the user should be
6088 able to get out next time we call QUIT, without anything as
6089 violent as interrupt_query. If we want to provide a way out of
6090 here without getting to the next QUIT, it should be based on
6091 hitting ^C twice as in remote_wait. */
6102 /* Come here after finding the start of a frame when we expected an
6103 ack. Do our best to discard the rest of this packet. */
6112 c
= readchar (remote_timeout
);
6115 case SERIAL_TIMEOUT
:
6116 /* Nothing we can do. */
6119 /* Discard the two bytes of checksum and stop. */
6120 c
= readchar (remote_timeout
);
6122 c
= readchar (remote_timeout
);
6125 case '*': /* Run length encoding. */
6126 /* Discard the repeat count. */
6127 c
= readchar (remote_timeout
);
6132 /* A regular character. */
6138 /* Come here after finding the start of the frame. Collect the rest
6139 into *BUF, verifying the checksum, length, and handling run-length
6140 compression. NUL terminate the buffer. If there is not enough room,
6141 expand *BUF using xrealloc.
6143 Returns -1 on error, number of characters in buffer (ignoring the
6144 trailing NULL) on success. (could be extended to return one of the
6145 SERIAL status indications). */
6148 read_frame (char **buf_p
,
6155 struct remote_state
*rs
= get_remote_state ();
6162 c
= readchar (remote_timeout
);
6165 case SERIAL_TIMEOUT
:
6167 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
6171 fputs_filtered ("Saw new packet start in middle of old one\n",
6173 return -1; /* Start a new packet, count retries. */
6176 unsigned char pktcsum
;
6182 check_0
= readchar (remote_timeout
);
6184 check_1
= readchar (remote_timeout
);
6186 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
6189 fputs_filtered ("Timeout in checksum, retrying\n",
6193 else if (check_0
< 0 || check_1
< 0)
6196 fputs_filtered ("Communication error in checksum\n",
6201 /* Don't recompute the checksum; with no ack packets we
6202 don't have any way to indicate a packet retransmission
6207 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
6208 if (csum
== pktcsum
)
6213 struct cleanup
*old_chain
;
6216 str
= escape_buffer (buf
, bc
);
6217 old_chain
= make_cleanup (xfree
, str
);
6218 fprintf_unfiltered (gdb_stdlog
,
6220 Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
6221 pktcsum
, csum
, str
);
6222 do_cleanups (old_chain
);
6224 /* Number of characters in buffer ignoring trailing
6228 case '*': /* Run length encoding. */
6233 c
= readchar (remote_timeout
);
6235 repeat
= c
- ' ' + 3; /* Compute repeat count. */
6237 /* The character before ``*'' is repeated. */
6239 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
6241 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
6243 /* Make some more room in the buffer. */
6244 *sizeof_buf
+= repeat
;
6245 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
6249 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
6255 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
6259 if (bc
>= *sizeof_buf
- 1)
6261 /* Make some more room in the buffer. */
6263 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
6274 /* Read a packet from the remote machine, with error checking, and
6275 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
6276 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
6277 rather than timing out; this is used (in synchronous mode) to wait
6278 for a target that is is executing user code to stop. */
6279 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
6280 don't have to change all the calls to getpkt to deal with the
6281 return value, because at the moment I don't know what the right
6282 thing to do it for those. */
6290 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
6294 /* Read a packet from the remote machine, with error checking, and
6295 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
6296 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
6297 rather than timing out; this is used (in synchronous mode) to wait
6298 for a target that is is executing user code to stop. If FOREVER ==
6299 0, this function is allowed to time out gracefully and return an
6300 indication of this to the caller. Otherwise return the number of
6301 bytes read. If EXPECTING_NOTIF, consider receiving a notification
6302 enough reason to return to the caller. */
6305 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
6306 int expecting_notif
)
6308 struct remote_state
*rs
= get_remote_state ();
6314 /* We're reading a new response. Make sure we don't look at a
6315 previously cached response. */
6316 rs
->cached_wait_status
= 0;
6318 strcpy (*buf
, "timeout");
6321 timeout
= watchdog
> 0 ? watchdog
: -1;
6322 else if (expecting_notif
)
6323 timeout
= 0; /* There should already be a char in the buffer. If
6326 timeout
= remote_timeout
;
6330 /* Process any number of notifications, and then return when
6334 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
6336 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
6338 /* This can loop forever if the remote side sends us
6339 characters continuously, but if it pauses, we'll get
6340 SERIAL_TIMEOUT from readchar because of timeout. Then
6341 we'll count that as a retry.
6343 Note that even when forever is set, we will only wait
6344 forever prior to the start of a packet. After that, we
6345 expect characters to arrive at a brisk pace. They should
6346 show up within remote_timeout intervals. */
6348 c
= readchar (timeout
);
6349 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
6351 if (c
== SERIAL_TIMEOUT
)
6353 if (expecting_notif
)
6354 return -1; /* Don't complain, it's normal to not get
6355 anything in this case. */
6357 if (forever
) /* Watchdog went off? Kill the target. */
6361 error (_("Watchdog timeout has expired. Target detached."));
6364 fputs_filtered ("Timed out.\n", gdb_stdlog
);
6368 /* We've found the start of a packet or notification.
6369 Now collect the data. */
6370 val
= read_frame (buf
, sizeof_buf
);
6375 serial_write (remote_desc
, "-", 1);
6378 if (tries
> MAX_TRIES
)
6380 /* We have tried hard enough, and just can't receive the
6381 packet/notification. Give up. */
6382 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
6384 /* Skip the ack char if we're in no-ack mode. */
6385 if (!rs
->noack_mode
)
6386 serial_write (remote_desc
, "+", 1);
6390 /* If we got an ordinary packet, return that to our caller. */
6395 struct cleanup
*old_chain
;
6398 str
= escape_buffer (*buf
, val
);
6399 old_chain
= make_cleanup (xfree
, str
);
6400 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", str
);
6401 do_cleanups (old_chain
);
6404 /* Skip the ack char if we're in no-ack mode. */
6405 if (!rs
->noack_mode
)
6406 serial_write (remote_desc
, "+", 1);
6410 /* If we got a notification, handle it, and go back to looking
6414 gdb_assert (c
== '%');
6418 struct cleanup
*old_chain
;
6421 str
= escape_buffer (*buf
, val
);
6422 old_chain
= make_cleanup (xfree
, str
);
6423 fprintf_unfiltered (gdb_stdlog
,
6424 " Notification received: %s\n",
6426 do_cleanups (old_chain
);
6429 handle_notification (*buf
, val
);
6431 /* Notifications require no acknowledgement. */
6433 if (expecting_notif
)
6440 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
6442 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0);
6446 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
)
6448 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1);
6455 /* Use catch_errors so the user can quit from gdb even when we
6456 aren't on speaking terms with the remote system. */
6457 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
6459 /* Don't wait for it to die. I'm not really sure it matters whether
6460 we do or not. For the existing stubs, kill is a noop. */
6461 target_mourn_inferior ();
6465 remote_vkill (int pid
, struct remote_state
*rs
)
6467 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
6470 /* Tell the remote target to detach. */
6471 sprintf (rs
->buf
, "vKill;%x", pid
);
6473 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6475 if (packet_ok (rs
->buf
,
6476 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
6478 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
6485 extended_remote_kill (void)
6488 int pid
= ptid_get_pid (inferior_ptid
);
6489 struct remote_state
*rs
= get_remote_state ();
6491 res
= remote_vkill (pid
, rs
);
6492 if (res
== -1 && !remote_multi_process_p (rs
))
6494 /* Don't try 'k' on a multi-process aware stub -- it has no way
6495 to specify the pid. */
6499 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6500 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
6503 /* Don't wait for it to die. I'm not really sure it matters whether
6504 we do or not. For the existing stubs, kill is a noop. */
6510 error (_("Can't kill process"));
6512 target_mourn_inferior ();
6516 remote_mourn (struct target_ops
*ops
)
6518 remote_mourn_1 (ops
);
6521 /* Worker function for remote_mourn. */
6523 remote_mourn_1 (struct target_ops
*target
)
6525 unpush_target (target
);
6527 /* remote_close takes care of cleaning up. */
6531 select_new_thread_callback (struct thread_info
*th
, void* data
)
6533 if (!is_exited (th
->ptid
))
6535 switch_to_thread (th
->ptid
);
6536 printf_filtered (_("[Switching to %s]\n"),
6537 target_pid_to_str (inferior_ptid
));
6544 extended_remote_mourn_1 (struct target_ops
*target
)
6546 struct remote_state
*rs
= get_remote_state ();
6548 /* In case we got here due to an error, but we're going to stay
6550 rs
->waiting_for_stop_reply
= 0;
6552 /* We're no longer interested in these events. */
6553 discard_pending_stop_replies (ptid_get_pid (inferior_ptid
));
6555 /* If the current general thread belonged to the process we just
6556 detached from or has exited, the remote side current general
6557 thread becomes undefined. Considering a case like this:
6559 - We just got here due to a detach.
6560 - The process that we're detaching from happens to immediately
6561 report a global breakpoint being hit in non-stop mode, in the
6562 same thread we had selected before.
6563 - GDB attaches to this process again.
6564 - This event happens to be the next event we handle.
6566 GDB would consider that the current general thread didn't need to
6567 be set on the stub side (with Hg), since for all it knew,
6568 GENERAL_THREAD hadn't changed.
6570 Notice that although in all-stop mode, the remote server always
6571 sets the current thread to the thread reporting the stop event,
6572 that doesn't happen in non-stop mode; in non-stop, the stub *must
6573 not* change the current thread when reporting a breakpoint hit,
6574 due to the decoupling of event reporting and event handling.
6576 To keep things simple, we always invalidate our notion of the
6578 record_currthread (minus_one_ptid
);
6580 /* Unlike "target remote", we do not want to unpush the target; then
6581 the next time the user says "run", we won't be connected. */
6583 /* Call common code to mark the inferior as not running. */
6584 generic_mourn_inferior ();
6586 if (have_inferiors ())
6588 extern void nullify_last_target_wait_ptid ();
6589 /* Multi-process case. The current process has exited, but
6590 there are other processes to debug. Switch to the first
6592 iterate_over_threads (select_new_thread_callback
, NULL
);
6593 nullify_last_target_wait_ptid ();
6597 if (!remote_multi_process_p (rs
))
6599 /* Check whether the target is running now - some remote stubs
6600 automatically restart after kill. */
6602 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6604 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
6606 /* Assume that the target has been restarted. Set inferior_ptid
6607 so that bits of core GDB realizes there's something here, e.g.,
6608 so that the user can say "kill" again. */
6609 inferior_ptid
= magic_null_ptid
;
6613 /* Mark this (still pushed) target as not executable until we
6615 target_mark_exited (target
);
6619 /* Always remove execution if this was the last process. */
6620 target_mark_exited (target
);
6625 extended_remote_mourn (struct target_ops
*ops
)
6627 extended_remote_mourn_1 (ops
);
6631 extended_remote_run (char *args
)
6633 struct remote_state
*rs
= get_remote_state ();
6637 /* If the user has disabled vRun support, or we have detected that
6638 support is not available, do not try it. */
6639 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
6642 strcpy (rs
->buf
, "vRun;");
6643 len
= strlen (rs
->buf
);
6645 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
6646 error (_("Remote file name too long for run packet"));
6647 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
, 0);
6649 gdb_assert (args
!= NULL
);
6652 struct cleanup
*back_to
;
6656 argv
= gdb_buildargv (args
);
6657 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
6658 for (i
= 0; argv
[i
] != NULL
; i
++)
6660 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
6661 error (_("Argument list too long for run packet"));
6662 rs
->buf
[len
++] = ';';
6663 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
, 0);
6665 do_cleanups (back_to
);
6668 rs
->buf
[len
++] = '\0';
6671 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6673 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
6675 /* We have a wait response; we don't need it, though. All is well. */
6678 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
6679 /* It wasn't disabled before, but it is now. */
6683 if (remote_exec_file
[0] == '\0')
6684 error (_("Running the default executable on the remote target failed; "
6685 "try \"set remote exec-file\"?"));
6687 error (_("Running \"%s\" on the remote target failed"),
6692 /* In the extended protocol we want to be able to do things like
6693 "run" and have them basically work as expected. So we need
6694 a special create_inferior function. We support changing the
6695 executable file and the command line arguments, but not the
6699 extended_remote_create_inferior_1 (char *exec_file
, char *args
,
6700 char **env
, int from_tty
)
6702 /* If running asynchronously, register the target file descriptor
6703 with the event loop. */
6704 if (target_can_async_p ())
6705 target_async (inferior_event_handler
, 0);
6707 /* Now restart the remote server. */
6708 if (extended_remote_run (args
) == -1)
6710 /* vRun was not supported. Fail if we need it to do what the
6712 if (remote_exec_file
[0])
6713 error (_("Remote target does not support \"set remote exec-file\""));
6715 error (_("Remote target does not support \"set args\" or run <ARGS>"));
6717 /* Fall back to "R". */
6718 extended_remote_restart ();
6721 /* Clean up from the last time we ran, before we mark the target
6722 running again. This will mark breakpoints uninserted, and
6723 get_offsets may insert breakpoints. */
6724 init_thread_list ();
6725 init_wait_for_inferior ();
6727 /* Now mark the inferior as running before we do anything else. */
6728 inferior_ptid
= magic_null_ptid
;
6730 /* Now, if we have thread information, update inferior_ptid. */
6731 inferior_ptid
= remote_current_thread (inferior_ptid
);
6733 add_inferior (ptid_get_pid (inferior_ptid
));
6734 add_thread_silent (inferior_ptid
);
6736 target_mark_running (&extended_remote_ops
);
6738 /* Get updated offsets, if the stub uses qOffsets. */
6743 extended_remote_create_inferior (struct target_ops
*ops
,
6744 char *exec_file
, char *args
,
6745 char **env
, int from_tty
)
6747 extended_remote_create_inferior_1 (exec_file
, args
, env
, from_tty
);
6751 /* Insert a breakpoint. On targets that have software breakpoint
6752 support, we ask the remote target to do the work; on targets
6753 which don't, we insert a traditional memory breakpoint. */
6756 remote_insert_breakpoint (struct bp_target_info
*bp_tgt
)
6758 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
6759 If it succeeds, then set the support to PACKET_ENABLE. If it
6760 fails, and the user has explicitly requested the Z support then
6761 report an error, otherwise, mark it disabled and go on. */
6763 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
6765 CORE_ADDR addr
= bp_tgt
->placed_address
;
6766 struct remote_state
*rs
;
6770 gdbarch_breakpoint_from_pc (target_gdbarch
, &addr
, &bpsize
);
6772 rs
= get_remote_state ();
6778 addr
= (ULONGEST
) remote_address_masked (addr
);
6779 p
+= hexnumstr (p
, addr
);
6780 sprintf (p
, ",%d", bpsize
);
6783 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6785 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
6790 bp_tgt
->placed_address
= addr
;
6791 bp_tgt
->placed_size
= bpsize
;
6793 case PACKET_UNKNOWN
:
6798 return memory_insert_breakpoint (bp_tgt
);
6802 remote_remove_breakpoint (struct bp_target_info
*bp_tgt
)
6804 CORE_ADDR addr
= bp_tgt
->placed_address
;
6805 struct remote_state
*rs
= get_remote_state ();
6808 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
6816 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
6817 p
+= hexnumstr (p
, addr
);
6818 sprintf (p
, ",%d", bp_tgt
->placed_size
);
6821 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6823 return (rs
->buf
[0] == 'E');
6826 return memory_remove_breakpoint (bp_tgt
);
6830 watchpoint_to_Z_packet (int type
)
6835 return Z_PACKET_WRITE_WP
;
6838 return Z_PACKET_READ_WP
;
6841 return Z_PACKET_ACCESS_WP
;
6844 internal_error (__FILE__
, __LINE__
,
6845 _("hw_bp_to_z: bad watchpoint type %d"), type
);
6850 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
6852 struct remote_state
*rs
= get_remote_state ();
6854 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
6856 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
6859 sprintf (rs
->buf
, "Z%x,", packet
);
6860 p
= strchr (rs
->buf
, '\0');
6861 addr
= remote_address_masked (addr
);
6862 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6863 sprintf (p
, ",%x", len
);
6866 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6868 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
6871 case PACKET_UNKNOWN
:
6876 internal_error (__FILE__
, __LINE__
,
6877 _("remote_insert_watchpoint: reached end of function"));
6882 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
6884 struct remote_state
*rs
= get_remote_state ();
6886 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
6888 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
6891 sprintf (rs
->buf
, "z%x,", packet
);
6892 p
= strchr (rs
->buf
, '\0');
6893 addr
= remote_address_masked (addr
);
6894 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6895 sprintf (p
, ",%x", len
);
6897 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6899 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
6902 case PACKET_UNKNOWN
:
6907 internal_error (__FILE__
, __LINE__
,
6908 _("remote_remove_watchpoint: reached end of function"));
6912 int remote_hw_watchpoint_limit
= -1;
6913 int remote_hw_breakpoint_limit
= -1;
6916 remote_check_watch_resources (int type
, int cnt
, int ot
)
6918 if (type
== bp_hardware_breakpoint
)
6920 if (remote_hw_breakpoint_limit
== 0)
6922 else if (remote_hw_breakpoint_limit
< 0)
6924 else if (cnt
<= remote_hw_breakpoint_limit
)
6929 if (remote_hw_watchpoint_limit
== 0)
6931 else if (remote_hw_watchpoint_limit
< 0)
6935 else if (cnt
<= remote_hw_watchpoint_limit
)
6942 remote_stopped_by_watchpoint (void)
6944 return remote_stopped_by_watchpoint_p
;
6948 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
6951 if (remote_stopped_by_watchpoint ())
6953 *addr_p
= remote_watch_data_address
;
6962 remote_insert_hw_breakpoint (struct bp_target_info
*bp_tgt
)
6965 struct remote_state
*rs
;
6968 /* The length field should be set to the size of a breakpoint
6969 instruction, even though we aren't inserting one ourselves. */
6971 gdbarch_breakpoint_from_pc
6972 (target_gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
6974 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
6977 rs
= get_remote_state ();
6984 addr
= remote_address_masked (bp_tgt
->placed_address
);
6985 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6986 sprintf (p
, ",%x", bp_tgt
->placed_size
);
6989 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6991 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
6994 case PACKET_UNKNOWN
:
6999 internal_error (__FILE__
, __LINE__
,
7000 _("remote_insert_hw_breakpoint: reached end of function"));
7005 remote_remove_hw_breakpoint (struct bp_target_info
*bp_tgt
)
7008 struct remote_state
*rs
= get_remote_state ();
7011 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
7018 addr
= remote_address_masked (bp_tgt
->placed_address
);
7019 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7020 sprintf (p
, ",%x", bp_tgt
->placed_size
);
7023 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7025 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
7028 case PACKET_UNKNOWN
:
7033 internal_error (__FILE__
, __LINE__
,
7034 _("remote_remove_hw_breakpoint: reached end of function"));
7037 /* Table used by the crc32 function to calcuate the checksum. */
7039 static unsigned long crc32_table
[256] =
7042 static unsigned long
7043 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
7045 if (!crc32_table
[1])
7047 /* Initialize the CRC table and the decoding table. */
7051 for (i
= 0; i
< 256; i
++)
7053 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
7054 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
7061 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
7067 /* compare-sections command
7069 With no arguments, compares each loadable section in the exec bfd
7070 with the same memory range on the target, and reports mismatches.
7071 Useful for verifying the image on the target against the exec file.
7072 Depends on the target understanding the new "qCRC:" request. */
7074 /* FIXME: cagney/1999-10-26: This command should be broken down into a
7075 target method (target verify memory) and generic version of the
7076 actual command. This will allow other high-level code (especially
7077 generic_load()) to make use of this target functionality. */
7080 compare_sections_command (char *args
, int from_tty
)
7082 struct remote_state
*rs
= get_remote_state ();
7084 unsigned long host_crc
, target_crc
;
7085 extern bfd
*exec_bfd
;
7086 struct cleanup
*old_chain
;
7089 const char *sectname
;
7096 error (_("command cannot be used without an exec file"));
7097 if (!current_target
.to_shortname
||
7098 strcmp (current_target
.to_shortname
, "remote") != 0)
7099 error (_("command can only be used with remote target"));
7101 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
7103 if (!(s
->flags
& SEC_LOAD
))
7104 continue; /* skip non-loadable section */
7106 size
= bfd_get_section_size (s
);
7108 continue; /* skip zero-length section */
7110 sectname
= bfd_get_section_name (exec_bfd
, s
);
7111 if (args
&& strcmp (args
, sectname
) != 0)
7112 continue; /* not the section selected by user */
7114 matched
= 1; /* do this section */
7116 /* FIXME: assumes lma can fit into long. */
7117 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
7118 (long) lma
, (long) size
);
7121 /* Be clever; compute the host_crc before waiting for target
7123 sectdata
= xmalloc (size
);
7124 old_chain
= make_cleanup (xfree
, sectdata
);
7125 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
7126 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
7128 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7129 if (rs
->buf
[0] == 'E')
7130 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
7131 sectname
, paddr (lma
), paddr (lma
+ size
));
7132 if (rs
->buf
[0] != 'C')
7133 error (_("remote target does not support this operation"));
7135 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
7136 target_crc
= target_crc
* 16 + fromhex (*tmp
);
7138 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
7139 sectname
, paddr (lma
), paddr (lma
+ size
));
7140 if (host_crc
== target_crc
)
7141 printf_filtered ("matched.\n");
7144 printf_filtered ("MIS-MATCHED!\n");
7148 do_cleanups (old_chain
);
7151 warning (_("One or more sections of the remote executable does not match\n\
7152 the loaded file\n"));
7153 if (args
&& !matched
)
7154 printf_filtered (_("No loaded section named '%s'.\n"), args
);
7157 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
7158 into remote target. The number of bytes written to the remote
7159 target is returned, or -1 for error. */
7162 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
7163 const char *annex
, const gdb_byte
*writebuf
,
7164 ULONGEST offset
, LONGEST len
,
7165 struct packet_config
*packet
)
7170 struct remote_state
*rs
= get_remote_state ();
7171 int max_size
= get_memory_write_packet_size ();
7173 if (packet
->support
== PACKET_DISABLE
)
7176 /* Insert header. */
7177 i
= snprintf (rs
->buf
, max_size
,
7178 "qXfer:%s:write:%s:%s:",
7179 object_name
, annex
? annex
: "",
7180 phex_nz (offset
, sizeof offset
));
7181 max_size
-= (i
+ 1);
7183 /* Escape as much data as fits into rs->buf. */
7184 buf_len
= remote_escape_output
7185 (writebuf
, len
, (rs
->buf
+ i
), &max_size
, max_size
);
7187 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
7188 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
7189 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7192 unpack_varlen_hex (rs
->buf
, &n
);
7196 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
7197 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
7198 number of bytes read is returned, or 0 for EOF, or -1 for error.
7199 The number of bytes read may be less than LEN without indicating an
7200 EOF. PACKET is checked and updated to indicate whether the remote
7201 target supports this object. */
7204 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
7206 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
7207 struct packet_config
*packet
)
7209 static char *finished_object
;
7210 static char *finished_annex
;
7211 static ULONGEST finished_offset
;
7213 struct remote_state
*rs
= get_remote_state ();
7214 unsigned int total
= 0;
7215 LONGEST i
, n
, packet_len
;
7217 if (packet
->support
== PACKET_DISABLE
)
7220 /* Check whether we've cached an end-of-object packet that matches
7222 if (finished_object
)
7224 if (strcmp (object_name
, finished_object
) == 0
7225 && strcmp (annex
? annex
: "", finished_annex
) == 0
7226 && offset
== finished_offset
)
7229 /* Otherwise, we're now reading something different. Discard
7231 xfree (finished_object
);
7232 xfree (finished_annex
);
7233 finished_object
= NULL
;
7234 finished_annex
= NULL
;
7237 /* Request only enough to fit in a single packet. The actual data
7238 may not, since we don't know how much of it will need to be escaped;
7239 the target is free to respond with slightly less data. We subtract
7240 five to account for the response type and the protocol frame. */
7241 n
= min (get_remote_packet_size () - 5, len
);
7242 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
7243 object_name
, annex
? annex
: "",
7244 phex_nz (offset
, sizeof offset
),
7245 phex_nz (n
, sizeof n
));
7246 i
= putpkt (rs
->buf
);
7251 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
7252 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7255 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
7256 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
7258 /* 'm' means there is (or at least might be) more data after this
7259 batch. That does not make sense unless there's at least one byte
7260 of data in this reply. */
7261 if (rs
->buf
[0] == 'm' && packet_len
== 1)
7262 error (_("Remote qXfer reply contained no data."));
7264 /* Got some data. */
7265 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
7267 /* 'l' is an EOF marker, possibly including a final block of data,
7268 or possibly empty. If we have the final block of a non-empty
7269 object, record this fact to bypass a subsequent partial read. */
7270 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
7272 finished_object
= xstrdup (object_name
);
7273 finished_annex
= xstrdup (annex
? annex
: "");
7274 finished_offset
= offset
+ i
;
7281 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
7282 const char *annex
, gdb_byte
*readbuf
,
7283 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
7285 struct remote_state
*rs
;
7290 set_general_thread (inferior_ptid
);
7292 rs
= get_remote_state ();
7294 /* Handle memory using the standard memory routines. */
7295 if (object
== TARGET_OBJECT_MEMORY
)
7300 /* If the remote target is connected but not running, we should
7301 pass this request down to a lower stratum (e.g. the executable
7303 if (!target_has_execution
)
7306 if (writebuf
!= NULL
)
7307 xfered
= remote_write_bytes (offset
, writebuf
, len
);
7309 xfered
= remote_read_bytes (offset
, readbuf
, len
);
7313 else if (xfered
== 0 && errno
== 0)
7319 /* Handle SPU memory using qxfer packets. */
7320 if (object
== TARGET_OBJECT_SPU
)
7323 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
7324 &remote_protocol_packets
7325 [PACKET_qXfer_spu_read
]);
7327 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
7328 &remote_protocol_packets
7329 [PACKET_qXfer_spu_write
]);
7332 /* Handle extra signal info using qxfer packets. */
7333 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
7336 return remote_read_qxfer (ops
, "siginfo", annex
, readbuf
, offset
, len
,
7337 &remote_protocol_packets
7338 [PACKET_qXfer_siginfo_read
]);
7340 return remote_write_qxfer (ops
, "siginfo", annex
, writebuf
, offset
, len
,
7341 &remote_protocol_packets
7342 [PACKET_qXfer_siginfo_write
]);
7345 /* Only handle flash writes. */
7346 if (writebuf
!= NULL
)
7352 case TARGET_OBJECT_FLASH
:
7353 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
7357 else if (xfered
== 0 && errno
== 0)
7367 /* Map pre-existing objects onto letters. DO NOT do this for new
7368 objects!!! Instead specify new query packets. */
7371 case TARGET_OBJECT_AVR
:
7375 case TARGET_OBJECT_AUXV
:
7376 gdb_assert (annex
== NULL
);
7377 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
7378 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
7380 case TARGET_OBJECT_AVAILABLE_FEATURES
:
7381 return remote_read_qxfer
7382 (ops
, "features", annex
, readbuf
, offset
, len
,
7383 &remote_protocol_packets
[PACKET_qXfer_features
]);
7385 case TARGET_OBJECT_LIBRARIES
:
7386 return remote_read_qxfer
7387 (ops
, "libraries", annex
, readbuf
, offset
, len
,
7388 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
7390 case TARGET_OBJECT_MEMORY_MAP
:
7391 gdb_assert (annex
== NULL
);
7392 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
7393 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
7395 case TARGET_OBJECT_OSDATA
:
7396 /* Should only get here if we're connected. */
7397 gdb_assert (remote_desc
);
7398 return remote_read_qxfer
7399 (ops
, "osdata", annex
, readbuf
, offset
, len
,
7400 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
7406 /* Note: a zero OFFSET and LEN can be used to query the minimum
7408 if (offset
== 0 && len
== 0)
7409 return (get_remote_packet_size ());
7410 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
7411 large enough let the caller deal with it. */
7412 if (len
< get_remote_packet_size ())
7414 len
= get_remote_packet_size ();
7416 /* Except for querying the minimum buffer size, target must be open. */
7418 error (_("remote query is only available after target open"));
7420 gdb_assert (annex
!= NULL
);
7421 gdb_assert (readbuf
!= NULL
);
7427 /* We used one buffer char for the remote protocol q command and
7428 another for the query type. As the remote protocol encapsulation
7429 uses 4 chars plus one extra in case we are debugging
7430 (remote_debug), we have PBUFZIZ - 7 left to pack the query
7433 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
7435 /* Bad caller may have sent forbidden characters. */
7436 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
7441 gdb_assert (annex
[i
] == '\0');
7443 i
= putpkt (rs
->buf
);
7447 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7448 strcpy ((char *) readbuf
, rs
->buf
);
7450 return strlen ((char *) readbuf
);
7454 remote_search_memory (struct target_ops
* ops
,
7455 CORE_ADDR start_addr
, ULONGEST search_space_len
,
7456 const gdb_byte
*pattern
, ULONGEST pattern_len
,
7457 CORE_ADDR
*found_addrp
)
7459 struct remote_state
*rs
= get_remote_state ();
7460 int max_size
= get_memory_write_packet_size ();
7461 struct packet_config
*packet
=
7462 &remote_protocol_packets
[PACKET_qSearch_memory
];
7463 /* number of packet bytes used to encode the pattern,
7464 this could be more than PATTERN_LEN due to escape characters */
7465 int escaped_pattern_len
;
7466 /* amount of pattern that was encodable in the packet */
7467 int used_pattern_len
;
7470 ULONGEST found_addr
;
7472 /* Don't go to the target if we don't have to.
7473 This is done before checking packet->support to avoid the possibility that
7474 a success for this edge case means the facility works in general. */
7475 if (pattern_len
> search_space_len
)
7477 if (pattern_len
== 0)
7479 *found_addrp
= start_addr
;
7483 /* If we already know the packet isn't supported, fall back to the simple
7484 way of searching memory. */
7486 if (packet
->support
== PACKET_DISABLE
)
7488 /* Target doesn't provided special support, fall back and use the
7489 standard support (copy memory and do the search here). */
7490 return simple_search_memory (ops
, start_addr
, search_space_len
,
7491 pattern
, pattern_len
, found_addrp
);
7494 /* Insert header. */
7495 i
= snprintf (rs
->buf
, max_size
,
7496 "qSearch:memory:%s;%s;",
7497 paddr_nz (start_addr
),
7498 phex_nz (search_space_len
, sizeof (search_space_len
)));
7499 max_size
-= (i
+ 1);
7501 /* Escape as much data as fits into rs->buf. */
7502 escaped_pattern_len
=
7503 remote_escape_output (pattern
, pattern_len
, (rs
->buf
+ i
),
7504 &used_pattern_len
, max_size
);
7506 /* Bail if the pattern is too large. */
7507 if (used_pattern_len
!= pattern_len
)
7508 error ("Pattern is too large to transmit to remote target.");
7510 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
7511 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
7512 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7514 /* The request may not have worked because the command is not
7515 supported. If so, fall back to the simple way. */
7516 if (packet
->support
== PACKET_DISABLE
)
7518 return simple_search_memory (ops
, start_addr
, search_space_len
,
7519 pattern
, pattern_len
, found_addrp
);
7524 if (rs
->buf
[0] == '0')
7526 else if (rs
->buf
[0] == '1')
7529 if (rs
->buf
[1] != ',')
7530 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
7531 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
7532 *found_addrp
= found_addr
;
7535 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
7541 remote_rcmd (char *command
,
7542 struct ui_file
*outbuf
)
7544 struct remote_state
*rs
= get_remote_state ();
7548 error (_("remote rcmd is only available after target open"));
7550 /* Send a NULL command across as an empty command. */
7551 if (command
== NULL
)
7554 /* The query prefix. */
7555 strcpy (rs
->buf
, "qRcmd,");
7556 p
= strchr (rs
->buf
, '\0');
7558 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/) > get_remote_packet_size ())
7559 error (_("\"monitor\" command ``%s'' is too long."), command
);
7561 /* Encode the actual command. */
7562 bin2hex ((gdb_byte
*) command
, p
, 0);
7564 if (putpkt (rs
->buf
) < 0)
7565 error (_("Communication problem with target."));
7567 /* get/display the response */
7572 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
7574 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7577 error (_("Target does not support this command."));
7578 if (buf
[0] == 'O' && buf
[1] != 'K')
7580 remote_console_output (buf
+ 1); /* 'O' message from stub. */
7583 if (strcmp (buf
, "OK") == 0)
7585 if (strlen (buf
) == 3 && buf
[0] == 'E'
7586 && isdigit (buf
[1]) && isdigit (buf
[2]))
7588 error (_("Protocol error with Rcmd"));
7590 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
7592 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
7593 fputc_unfiltered (c
, outbuf
);
7599 static VEC(mem_region_s
) *
7600 remote_memory_map (struct target_ops
*ops
)
7602 VEC(mem_region_s
) *result
= NULL
;
7603 char *text
= target_read_stralloc (¤t_target
,
7604 TARGET_OBJECT_MEMORY_MAP
, NULL
);
7608 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
7609 result
= parse_memory_map (text
);
7610 do_cleanups (back_to
);
7617 packet_command (char *args
, int from_tty
)
7619 struct remote_state
*rs
= get_remote_state ();
7622 error (_("command can only be used with remote target"));
7625 error (_("remote-packet command requires packet text as argument"));
7627 puts_filtered ("sending: ");
7628 print_packet (args
);
7629 puts_filtered ("\n");
7632 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7633 puts_filtered ("received: ");
7634 print_packet (rs
->buf
);
7635 puts_filtered ("\n");
7639 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
7641 static void display_thread_info (struct gdb_ext_thread_info
*info
);
7643 static void threadset_test_cmd (char *cmd
, int tty
);
7645 static void threadalive_test (char *cmd
, int tty
);
7647 static void threadlist_test_cmd (char *cmd
, int tty
);
7649 int get_and_display_threadinfo (threadref
*ref
);
7651 static void threadinfo_test_cmd (char *cmd
, int tty
);
7653 static int thread_display_step (threadref
*ref
, void *context
);
7655 static void threadlist_update_test_cmd (char *cmd
, int tty
);
7657 static void init_remote_threadtests (void);
7659 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
7662 threadset_test_cmd (char *cmd
, int tty
)
7664 int sample_thread
= SAMPLE_THREAD
;
7666 printf_filtered (_("Remote threadset test\n"));
7667 set_general_thread (sample_thread
);
7672 threadalive_test (char *cmd
, int tty
)
7674 int sample_thread
= SAMPLE_THREAD
;
7675 int pid
= ptid_get_pid (inferior_ptid
);
7676 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
7678 if (remote_thread_alive (ptid
))
7679 printf_filtered ("PASS: Thread alive test\n");
7681 printf_filtered ("FAIL: Thread alive test\n");
7684 void output_threadid (char *title
, threadref
*ref
);
7687 output_threadid (char *title
, threadref
*ref
)
7691 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
7693 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
7697 threadlist_test_cmd (char *cmd
, int tty
)
7700 threadref nextthread
;
7701 int done
, result_count
;
7702 threadref threadlist
[3];
7704 printf_filtered ("Remote Threadlist test\n");
7705 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
7706 &result_count
, &threadlist
[0]))
7707 printf_filtered ("FAIL: threadlist test\n");
7710 threadref
*scan
= threadlist
;
7711 threadref
*limit
= scan
+ result_count
;
7713 while (scan
< limit
)
7714 output_threadid (" thread ", scan
++);
7719 display_thread_info (struct gdb_ext_thread_info
*info
)
7721 output_threadid ("Threadid: ", &info
->threadid
);
7722 printf_filtered ("Name: %s\n ", info
->shortname
);
7723 printf_filtered ("State: %s\n", info
->display
);
7724 printf_filtered ("other: %s\n\n", info
->more_display
);
7728 get_and_display_threadinfo (threadref
*ref
)
7732 struct gdb_ext_thread_info threadinfo
;
7734 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
7735 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
7736 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
7737 display_thread_info (&threadinfo
);
7742 threadinfo_test_cmd (char *cmd
, int tty
)
7744 int athread
= SAMPLE_THREAD
;
7748 int_to_threadref (&thread
, athread
);
7749 printf_filtered ("Remote Threadinfo test\n");
7750 if (!get_and_display_threadinfo (&thread
))
7751 printf_filtered ("FAIL cannot get thread info\n");
7755 thread_display_step (threadref
*ref
, void *context
)
7757 /* output_threadid(" threadstep ",ref); *//* simple test */
7758 return get_and_display_threadinfo (ref
);
7762 threadlist_update_test_cmd (char *cmd
, int tty
)
7764 printf_filtered ("Remote Threadlist update test\n");
7765 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
7769 init_remote_threadtests (void)
7771 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
7772 Fetch and print the remote list of thread identifiers, one pkt only"));
7773 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
7774 _("Fetch and display info about one thread"));
7775 add_com ("tset", class_obscure
, threadset_test_cmd
,
7776 _("Test setting to a different thread"));
7777 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
7778 _("Iterate through updating all remote thread info"));
7779 add_com ("talive", class_obscure
, threadalive_test
,
7780 _(" Remote thread alive test "));
7785 /* Convert a thread ID to a string. Returns the string in a static
7789 remote_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
7791 static char buf
[64];
7792 struct remote_state
*rs
= get_remote_state ();
7794 if (ptid_equal (magic_null_ptid
, ptid
))
7796 xsnprintf (buf
, sizeof buf
, "Thread <main>");
7799 else if (remote_multi_process_p (rs
)
7800 && ptid_get_tid (ptid
) != 0 && ptid_get_pid (ptid
) != 0)
7802 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
7803 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
7806 else if (ptid_get_tid (ptid
) != 0)
7808 xsnprintf (buf
, sizeof buf
, "Thread %ld",
7809 ptid_get_tid (ptid
));
7813 return normal_pid_to_str (ptid
);
7816 /* Get the address of the thread local variable in OBJFILE which is
7817 stored at OFFSET within the thread local storage for thread PTID. */
7820 remote_get_thread_local_address (struct target_ops
*ops
,
7821 ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
7823 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
7825 struct remote_state
*rs
= get_remote_state ();
7827 char *endp
= rs
->buf
+ get_remote_packet_size ();
7828 enum packet_result result
;
7830 strcpy (p
, "qGetTLSAddr:");
7832 p
= write_ptid (p
, endp
, ptid
);
7834 p
+= hexnumstr (p
, offset
);
7836 p
+= hexnumstr (p
, lm
);
7840 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7841 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
7842 if (result
== PACKET_OK
)
7846 unpack_varlen_hex (rs
->buf
, &result
);
7849 else if (result
== PACKET_UNKNOWN
)
7850 throw_error (TLS_GENERIC_ERROR
,
7851 _("Remote target doesn't support qGetTLSAddr packet"));
7853 throw_error (TLS_GENERIC_ERROR
,
7854 _("Remote target failed to process qGetTLSAddr request"));
7857 throw_error (TLS_GENERIC_ERROR
,
7858 _("TLS not supported or disabled on this target"));
7863 /* Support for inferring a target description based on the current
7864 architecture and the size of a 'g' packet. While the 'g' packet
7865 can have any size (since optional registers can be left off the
7866 end), some sizes are easily recognizable given knowledge of the
7867 approximate architecture. */
7869 struct remote_g_packet_guess
7872 const struct target_desc
*tdesc
;
7874 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
7875 DEF_VEC_O(remote_g_packet_guess_s
);
7877 struct remote_g_packet_data
7879 VEC(remote_g_packet_guess_s
) *guesses
;
7882 static struct gdbarch_data
*remote_g_packet_data_handle
;
7885 remote_g_packet_data_init (struct obstack
*obstack
)
7887 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
7891 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
7892 const struct target_desc
*tdesc
)
7894 struct remote_g_packet_data
*data
7895 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
7896 struct remote_g_packet_guess new_guess
, *guess
;
7899 gdb_assert (tdesc
!= NULL
);
7902 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
7904 if (guess
->bytes
== bytes
)
7905 internal_error (__FILE__
, __LINE__
,
7906 "Duplicate g packet description added for size %d",
7909 new_guess
.bytes
= bytes
;
7910 new_guess
.tdesc
= tdesc
;
7911 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
7914 /* Return 1 if remote_read_description would do anything on this target
7915 and architecture, 0 otherwise. */
7918 remote_read_description_p (struct target_ops
*target
)
7920 struct remote_g_packet_data
*data
7921 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
7923 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
7929 static const struct target_desc
*
7930 remote_read_description (struct target_ops
*target
)
7932 struct remote_g_packet_data
*data
7933 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
7935 /* Do not try this during initial connection, when we do not know
7936 whether there is a running but stopped thread. */
7937 if (!target_has_execution
|| ptid_equal (inferior_ptid
, null_ptid
))
7940 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
7942 struct remote_g_packet_guess
*guess
;
7944 int bytes
= send_g_packet ();
7947 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
7949 if (guess
->bytes
== bytes
)
7950 return guess
->tdesc
;
7952 /* We discard the g packet. A minor optimization would be to
7953 hold on to it, and fill the register cache once we have selected
7954 an architecture, but it's too tricky to do safely. */
7960 /* Remote file transfer support. This is host-initiated I/O, not
7961 target-initiated; for target-initiated, see remote-fileio.c. */
7963 /* If *LEFT is at least the length of STRING, copy STRING to
7964 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7965 decrease *LEFT. Otherwise raise an error. */
7968 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
7970 int len
= strlen (string
);
7973 error (_("Packet too long for target."));
7975 memcpy (*buffer
, string
, len
);
7979 /* NUL-terminate the buffer as a convenience, if there is
7985 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
7986 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7987 decrease *LEFT. Otherwise raise an error. */
7990 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
7993 if (2 * len
> *left
)
7994 error (_("Packet too long for target."));
7996 bin2hex (bytes
, *buffer
, len
);
8000 /* NUL-terminate the buffer as a convenience, if there is
8006 /* If *LEFT is large enough, convert VALUE to hex and add it to
8007 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8008 decrease *LEFT. Otherwise raise an error. */
8011 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
8013 int len
= hexnumlen (value
);
8016 error (_("Packet too long for target."));
8018 hexnumstr (*buffer
, value
);
8022 /* NUL-terminate the buffer as a convenience, if there is
8028 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
8029 value, *REMOTE_ERRNO to the remote error number or zero if none
8030 was included, and *ATTACHMENT to point to the start of the annex
8031 if any. The length of the packet isn't needed here; there may
8032 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
8034 Return 0 if the packet could be parsed, -1 if it could not. If
8035 -1 is returned, the other variables may not be initialized. */
8038 remote_hostio_parse_result (char *buffer
, int *retcode
,
8039 int *remote_errno
, char **attachment
)
8046 if (buffer
[0] != 'F')
8050 *retcode
= strtol (&buffer
[1], &p
, 16);
8051 if (errno
!= 0 || p
== &buffer
[1])
8054 /* Check for ",errno". */
8058 *remote_errno
= strtol (p
+ 1, &p2
, 16);
8059 if (errno
!= 0 || p
+ 1 == p2
)
8064 /* Check for ";attachment". If there is no attachment, the
8065 packet should end here. */
8068 *attachment
= p
+ 1;
8071 else if (*p
== '\0')
8077 /* Send a prepared I/O packet to the target and read its response.
8078 The prepared packet is in the global RS->BUF before this function
8079 is called, and the answer is there when we return.
8081 COMMAND_BYTES is the length of the request to send, which may include
8082 binary data. WHICH_PACKET is the packet configuration to check
8083 before attempting a packet. If an error occurs, *REMOTE_ERRNO
8084 is set to the error number and -1 is returned. Otherwise the value
8085 returned by the function is returned.
8087 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
8088 attachment is expected; an error will be reported if there's a
8089 mismatch. If one is found, *ATTACHMENT will be set to point into
8090 the packet buffer and *ATTACHMENT_LEN will be set to the
8091 attachment's length. */
8094 remote_hostio_send_command (int command_bytes
, int which_packet
,
8095 int *remote_errno
, char **attachment
,
8096 int *attachment_len
)
8098 struct remote_state
*rs
= get_remote_state ();
8099 int ret
, bytes_read
;
8100 char *attachment_tmp
;
8103 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
8105 *remote_errno
= FILEIO_ENOSYS
;
8109 putpkt_binary (rs
->buf
, command_bytes
);
8110 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
8112 /* If it timed out, something is wrong. Don't try to parse the
8116 *remote_errno
= FILEIO_EINVAL
;
8120 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
8123 *remote_errno
= FILEIO_EINVAL
;
8125 case PACKET_UNKNOWN
:
8126 *remote_errno
= FILEIO_ENOSYS
;
8132 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
8135 *remote_errno
= FILEIO_EINVAL
;
8139 /* Make sure we saw an attachment if and only if we expected one. */
8140 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
8141 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
8143 *remote_errno
= FILEIO_EINVAL
;
8147 /* If an attachment was found, it must point into the packet buffer;
8148 work out how many bytes there were. */
8149 if (attachment_tmp
!= NULL
)
8151 *attachment
= attachment_tmp
;
8152 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
8158 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
8159 remote file descriptor, or -1 if an error occurs (and set
8163 remote_hostio_open (const char *filename
, int flags
, int mode
,
8166 struct remote_state
*rs
= get_remote_state ();
8168 int left
= get_remote_packet_size () - 1;
8170 remote_buffer_add_string (&p
, &left
, "vFile:open:");
8172 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
8174 remote_buffer_add_string (&p
, &left
, ",");
8176 remote_buffer_add_int (&p
, &left
, flags
);
8177 remote_buffer_add_string (&p
, &left
, ",");
8179 remote_buffer_add_int (&p
, &left
, mode
);
8181 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
8182 remote_errno
, NULL
, NULL
);
8185 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
8186 Return the number of bytes written, or -1 if an error occurs (and
8187 set *REMOTE_ERRNO). */
8190 remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
8191 ULONGEST offset
, int *remote_errno
)
8193 struct remote_state
*rs
= get_remote_state ();
8195 int left
= get_remote_packet_size ();
8198 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
8200 remote_buffer_add_int (&p
, &left
, fd
);
8201 remote_buffer_add_string (&p
, &left
, ",");
8203 remote_buffer_add_int (&p
, &left
, offset
);
8204 remote_buffer_add_string (&p
, &left
, ",");
8206 p
+= remote_escape_output (write_buf
, len
, p
, &out_len
,
8207 get_remote_packet_size () - (p
- rs
->buf
));
8209 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
8210 remote_errno
, NULL
, NULL
);
8213 /* Read up to LEN bytes FD on the remote target into READ_BUF
8214 Return the number of bytes read, or -1 if an error occurs (and
8215 set *REMOTE_ERRNO). */
8218 remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
8219 ULONGEST offset
, int *remote_errno
)
8221 struct remote_state
*rs
= get_remote_state ();
8224 int left
= get_remote_packet_size ();
8225 int ret
, attachment_len
;
8228 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
8230 remote_buffer_add_int (&p
, &left
, fd
);
8231 remote_buffer_add_string (&p
, &left
, ",");
8233 remote_buffer_add_int (&p
, &left
, len
);
8234 remote_buffer_add_string (&p
, &left
, ",");
8236 remote_buffer_add_int (&p
, &left
, offset
);
8238 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
8239 remote_errno
, &attachment
,
8245 read_len
= remote_unescape_input (attachment
, attachment_len
,
8247 if (read_len
!= ret
)
8248 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
8253 /* Close FD on the remote target. Return 0, or -1 if an error occurs
8254 (and set *REMOTE_ERRNO). */
8257 remote_hostio_close (int fd
, int *remote_errno
)
8259 struct remote_state
*rs
= get_remote_state ();
8261 int left
= get_remote_packet_size () - 1;
8263 remote_buffer_add_string (&p
, &left
, "vFile:close:");
8265 remote_buffer_add_int (&p
, &left
, fd
);
8267 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
8268 remote_errno
, NULL
, NULL
);
8271 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
8272 occurs (and set *REMOTE_ERRNO). */
8275 remote_hostio_unlink (const char *filename
, int *remote_errno
)
8277 struct remote_state
*rs
= get_remote_state ();
8279 int left
= get_remote_packet_size () - 1;
8281 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
8283 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
8286 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
8287 remote_errno
, NULL
, NULL
);
8291 remote_fileio_errno_to_host (int errnum
)
8315 case FILEIO_ENOTDIR
:
8335 case FILEIO_ENAMETOOLONG
:
8336 return ENAMETOOLONG
;
8342 remote_hostio_error (int errnum
)
8344 int host_error
= remote_fileio_errno_to_host (errnum
);
8346 if (host_error
== -1)
8347 error (_("Unknown remote I/O error %d"), errnum
);
8349 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
8353 remote_hostio_close_cleanup (void *opaque
)
8355 int fd
= *(int *) opaque
;
8358 remote_hostio_close (fd
, &remote_errno
);
8363 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
8365 const char *filename
= bfd_get_filename (abfd
);
8366 int fd
, remote_errno
;
8369 gdb_assert (remote_filename_p (filename
));
8371 fd
= remote_hostio_open (filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
8374 errno
= remote_fileio_errno_to_host (remote_errno
);
8375 bfd_set_error (bfd_error_system_call
);
8379 stream
= xmalloc (sizeof (int));
8385 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
8387 int fd
= *(int *)stream
;
8392 /* Ignore errors on close; these may happen if the remote
8393 connection was already torn down. */
8394 remote_hostio_close (fd
, &remote_errno
);
8400 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
8401 file_ptr nbytes
, file_ptr offset
)
8403 int fd
= *(int *)stream
;
8405 file_ptr pos
, bytes
;
8408 while (nbytes
> pos
)
8410 bytes
= remote_hostio_pread (fd
, (char *)buf
+ pos
, nbytes
- pos
,
8411 offset
+ pos
, &remote_errno
);
8413 /* Success, but no bytes, means end-of-file. */
8417 errno
= remote_fileio_errno_to_host (remote_errno
);
8418 bfd_set_error (bfd_error_system_call
);
8429 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
8431 /* FIXME: We should probably implement remote_hostio_stat. */
8432 sb
->st_size
= INT_MAX
;
8437 remote_filename_p (const char *filename
)
8439 return strncmp (filename
, "remote:", 7) == 0;
8443 remote_bfd_open (const char *remote_file
, const char *target
)
8445 return bfd_openr_iovec (remote_file
, target
,
8446 remote_bfd_iovec_open
, NULL
,
8447 remote_bfd_iovec_pread
,
8448 remote_bfd_iovec_close
,
8449 remote_bfd_iovec_stat
);
8453 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
8455 struct cleanup
*back_to
, *close_cleanup
;
8456 int retcode
, fd
, remote_errno
, bytes
, io_size
;
8459 int bytes_in_buffer
;
8464 error (_("command can only be used with remote target"));
8466 file
= fopen (local_file
, "rb");
8468 perror_with_name (local_file
);
8469 back_to
= make_cleanup_fclose (file
);
8471 fd
= remote_hostio_open (remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
8473 0700, &remote_errno
);
8475 remote_hostio_error (remote_errno
);
8477 /* Send up to this many bytes at once. They won't all fit in the
8478 remote packet limit, so we'll transfer slightly fewer. */
8479 io_size
= get_remote_packet_size ();
8480 buffer
= xmalloc (io_size
);
8481 make_cleanup (xfree
, buffer
);
8483 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
8485 bytes_in_buffer
= 0;
8488 while (bytes_in_buffer
|| !saw_eof
)
8492 bytes
= fread (buffer
+ bytes_in_buffer
, 1, io_size
- bytes_in_buffer
,
8497 error (_("Error reading %s."), local_file
);
8500 /* EOF. Unless there is something still in the
8501 buffer from the last iteration, we are done. */
8503 if (bytes_in_buffer
== 0)
8511 bytes
+= bytes_in_buffer
;
8512 bytes_in_buffer
= 0;
8514 retcode
= remote_hostio_pwrite (fd
, buffer
, bytes
, offset
, &remote_errno
);
8517 remote_hostio_error (remote_errno
);
8518 else if (retcode
== 0)
8519 error (_("Remote write of %d bytes returned 0!"), bytes
);
8520 else if (retcode
< bytes
)
8522 /* Short write. Save the rest of the read data for the next
8524 bytes_in_buffer
= bytes
- retcode
;
8525 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
8531 discard_cleanups (close_cleanup
);
8532 if (remote_hostio_close (fd
, &remote_errno
))
8533 remote_hostio_error (remote_errno
);
8536 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
8537 do_cleanups (back_to
);
8541 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
8543 struct cleanup
*back_to
, *close_cleanup
;
8544 int retcode
, fd
, remote_errno
, bytes
, io_size
;
8550 error (_("command can only be used with remote target"));
8552 fd
= remote_hostio_open (remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
8554 remote_hostio_error (remote_errno
);
8556 file
= fopen (local_file
, "wb");
8558 perror_with_name (local_file
);
8559 back_to
= make_cleanup_fclose (file
);
8561 /* Send up to this many bytes at once. They won't all fit in the
8562 remote packet limit, so we'll transfer slightly fewer. */
8563 io_size
= get_remote_packet_size ();
8564 buffer
= xmalloc (io_size
);
8565 make_cleanup (xfree
, buffer
);
8567 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
8572 bytes
= remote_hostio_pread (fd
, buffer
, io_size
, offset
, &remote_errno
);
8574 /* Success, but no bytes, means end-of-file. */
8577 remote_hostio_error (remote_errno
);
8581 bytes
= fwrite (buffer
, 1, bytes
, file
);
8583 perror_with_name (local_file
);
8586 discard_cleanups (close_cleanup
);
8587 if (remote_hostio_close (fd
, &remote_errno
))
8588 remote_hostio_error (remote_errno
);
8591 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
8592 do_cleanups (back_to
);
8596 remote_file_delete (const char *remote_file
, int from_tty
)
8598 int retcode
, remote_errno
;
8601 error (_("command can only be used with remote target"));
8603 retcode
= remote_hostio_unlink (remote_file
, &remote_errno
);
8605 remote_hostio_error (remote_errno
);
8608 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
8612 remote_put_command (char *args
, int from_tty
)
8614 struct cleanup
*back_to
;
8618 error_no_arg (_("file to put"));
8620 argv
= gdb_buildargv (args
);
8621 back_to
= make_cleanup_freeargv (argv
);
8622 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
8623 error (_("Invalid parameters to remote put"));
8625 remote_file_put (argv
[0], argv
[1], from_tty
);
8627 do_cleanups (back_to
);
8631 remote_get_command (char *args
, int from_tty
)
8633 struct cleanup
*back_to
;
8637 error_no_arg (_("file to get"));
8639 argv
= gdb_buildargv (args
);
8640 back_to
= make_cleanup_freeargv (argv
);
8641 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
8642 error (_("Invalid parameters to remote get"));
8644 remote_file_get (argv
[0], argv
[1], from_tty
);
8646 do_cleanups (back_to
);
8650 remote_delete_command (char *args
, int from_tty
)
8652 struct cleanup
*back_to
;
8656 error_no_arg (_("file to delete"));
8658 argv
= gdb_buildargv (args
);
8659 back_to
= make_cleanup_freeargv (argv
);
8660 if (argv
[0] == NULL
|| argv
[1] != NULL
)
8661 error (_("Invalid parameters to remote delete"));
8663 remote_file_delete (argv
[0], from_tty
);
8665 do_cleanups (back_to
);
8669 remote_command (char *args
, int from_tty
)
8671 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
8674 static int remote_target_can_reverse
= 1;
8677 remote_can_execute_reverse (void)
8679 return remote_target_can_reverse
;
8683 remote_supports_non_stop (void)
8689 remote_supports_multi_process (void)
8691 struct remote_state
*rs
= get_remote_state ();
8692 return remote_multi_process_p (rs
);
8696 init_remote_ops (void)
8698 remote_ops
.to_shortname
= "remote";
8699 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
8701 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
8702 Specify the serial device it is connected to\n\
8703 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
8704 remote_ops
.to_open
= remote_open
;
8705 remote_ops
.to_close
= remote_close
;
8706 remote_ops
.to_detach
= remote_detach
;
8707 remote_ops
.to_disconnect
= remote_disconnect
;
8708 remote_ops
.to_resume
= remote_resume
;
8709 remote_ops
.to_wait
= remote_wait
;
8710 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
8711 remote_ops
.to_store_registers
= remote_store_registers
;
8712 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
8713 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
8714 remote_ops
.to_files_info
= remote_files_info
;
8715 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
8716 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
8717 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
8718 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
8719 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
8720 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
8721 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
8722 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
8723 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
8724 remote_ops
.to_kill
= remote_kill
;
8725 remote_ops
.to_load
= generic_load
;
8726 remote_ops
.to_mourn_inferior
= remote_mourn
;
8727 remote_ops
.to_thread_alive
= remote_thread_alive
;
8728 remote_ops
.to_find_new_threads
= remote_threads_info
;
8729 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
8730 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
8731 remote_ops
.to_stop
= remote_stop
;
8732 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
8733 remote_ops
.to_rcmd
= remote_rcmd
;
8734 remote_ops
.to_log_command
= serial_log_command
;
8735 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
8736 remote_ops
.to_stratum
= process_stratum
;
8737 remote_ops
.to_has_all_memory
= 1;
8738 remote_ops
.to_has_memory
= 1;
8739 remote_ops
.to_has_stack
= 1;
8740 remote_ops
.to_has_registers
= 1;
8741 remote_ops
.to_has_execution
= 1;
8742 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
8743 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
8744 remote_ops
.to_magic
= OPS_MAGIC
;
8745 remote_ops
.to_memory_map
= remote_memory_map
;
8746 remote_ops
.to_flash_erase
= remote_flash_erase
;
8747 remote_ops
.to_flash_done
= remote_flash_done
;
8748 remote_ops
.to_read_description
= remote_read_description
;
8749 remote_ops
.to_search_memory
= remote_search_memory
;
8750 remote_ops
.to_can_async_p
= remote_can_async_p
;
8751 remote_ops
.to_is_async_p
= remote_is_async_p
;
8752 remote_ops
.to_async
= remote_async
;
8753 remote_ops
.to_async_mask
= remote_async_mask
;
8754 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
8755 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
8756 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
8757 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
8760 /* Set up the extended remote vector by making a copy of the standard
8761 remote vector and adding to it. */
8764 init_extended_remote_ops (void)
8766 extended_remote_ops
= remote_ops
;
8768 extended_remote_ops
.to_shortname
= "extended-remote";
8769 extended_remote_ops
.to_longname
=
8770 "Extended remote serial target in gdb-specific protocol";
8771 extended_remote_ops
.to_doc
=
8772 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
8773 Specify the serial device it is connected to (e.g. /dev/ttya).";
8774 extended_remote_ops
.to_open
= extended_remote_open
;
8775 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
8776 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
8777 extended_remote_ops
.to_detach
= extended_remote_detach
;
8778 extended_remote_ops
.to_attach
= extended_remote_attach
;
8779 extended_remote_ops
.to_kill
= extended_remote_kill
;
8783 remote_can_async_p (void)
8785 if (!target_async_permitted
)
8786 /* We only enable async when the user specifically asks for it. */
8789 /* We're async whenever the serial device is. */
8790 return remote_async_mask_value
&& serial_can_async_p (remote_desc
);
8794 remote_is_async_p (void)
8796 if (!target_async_permitted
)
8797 /* We only enable async when the user specifically asks for it. */
8800 /* We're async whenever the serial device is. */
8801 return remote_async_mask_value
&& serial_is_async_p (remote_desc
);
8804 /* Pass the SERIAL event on and up to the client. One day this code
8805 will be able to delay notifying the client of an event until the
8806 point where an entire packet has been received. */
8808 static void (*async_client_callback
) (enum inferior_event_type event_type
,
8810 static void *async_client_context
;
8811 static serial_event_ftype remote_async_serial_handler
;
8814 remote_async_serial_handler (struct serial
*scb
, void *context
)
8816 /* Don't propogate error information up to the client. Instead let
8817 the client find out about the error by querying the target. */
8818 async_client_callback (INF_REG_EVENT
, async_client_context
);
8822 remote_async_inferior_event_handler (gdb_client_data data
)
8824 inferior_event_handler (INF_REG_EVENT
, NULL
);
8828 remote_async_get_pending_events_handler (gdb_client_data data
)
8830 remote_get_pending_stop_replies ();
8834 remote_async (void (*callback
) (enum inferior_event_type event_type
,
8835 void *context
), void *context
)
8837 if (remote_async_mask_value
== 0)
8838 internal_error (__FILE__
, __LINE__
,
8839 _("Calling remote_async when async is masked"));
8841 if (callback
!= NULL
)
8843 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
8844 async_client_callback
= callback
;
8845 async_client_context
= context
;
8848 serial_async (remote_desc
, NULL
, NULL
);
8852 remote_async_mask (int new_mask
)
8854 int curr_mask
= remote_async_mask_value
;
8855 remote_async_mask_value
= new_mask
;
8860 set_remote_cmd (char *args
, int from_tty
)
8862 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
8866 show_remote_cmd (char *args
, int from_tty
)
8868 /* We can't just use cmd_show_list here, because we want to skip
8869 the redundant "show remote Z-packet" and the legacy aliases. */
8870 struct cleanup
*showlist_chain
;
8871 struct cmd_list_element
*list
= remote_show_cmdlist
;
8873 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
8874 for (; list
!= NULL
; list
= list
->next
)
8875 if (strcmp (list
->name
, "Z-packet") == 0)
8877 else if (list
->type
== not_set_cmd
)
8878 /* Alias commands are exactly like the original, except they
8879 don't have the normal type. */
8883 struct cleanup
*option_chain
8884 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
8885 ui_out_field_string (uiout
, "name", list
->name
);
8886 ui_out_text (uiout
, ": ");
8887 if (list
->type
== show_cmd
)
8888 do_setshow_command ((char *) NULL
, from_tty
, list
);
8890 cmd_func (list
, NULL
, from_tty
);
8891 /* Close the tuple. */
8892 do_cleanups (option_chain
);
8895 /* Close the tuple. */
8896 do_cleanups (showlist_chain
);
8900 /* Function to be called whenever a new objfile (shlib) is detected. */
8902 remote_new_objfile (struct objfile
*objfile
)
8904 if (remote_desc
!= 0) /* Have a remote connection. */
8905 remote_check_symbols (objfile
);
8909 _initialize_remote (void)
8911 struct remote_state
*rs
;
8913 /* architecture specific data */
8914 remote_gdbarch_data_handle
=
8915 gdbarch_data_register_post_init (init_remote_state
);
8916 remote_g_packet_data_handle
=
8917 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
8919 /* Initialize the per-target state. At the moment there is only one
8920 of these, not one per target. Only one target is active at a
8921 time. The default buffer size is unimportant; it will be expanded
8922 whenever a larger buffer is needed. */
8923 rs
= get_remote_state_raw ();
8925 rs
->buf
= xmalloc (rs
->buf_size
);
8928 add_target (&remote_ops
);
8930 init_extended_remote_ops ();
8931 add_target (&extended_remote_ops
);
8933 /* Hook into new objfile notification. */
8934 observer_attach_new_objfile (remote_new_objfile
);
8936 /* Set up signal handlers. */
8937 sigint_remote_token
=
8938 create_async_signal_handler (async_remote_interrupt
, NULL
);
8939 sigint_remote_twice_token
=
8940 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
8943 init_remote_threadtests ();
8946 /* set/show remote ... */
8948 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
8949 Remote protocol specific variables\n\
8950 Configure various remote-protocol specific variables such as\n\
8951 the packets being used"),
8952 &remote_set_cmdlist
, "set remote ",
8953 0 /* allow-unknown */, &setlist
);
8954 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
8955 Remote protocol specific variables\n\
8956 Configure various remote-protocol specific variables such as\n\
8957 the packets being used"),
8958 &remote_show_cmdlist
, "show remote ",
8959 0 /* allow-unknown */, &showlist
);
8961 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
8962 Compare section data on target to the exec file.\n\
8963 Argument is a single section name (default: all loaded sections)."),
8966 add_cmd ("packet", class_maintenance
, packet_command
, _("\
8967 Send an arbitrary packet to a remote target.\n\
8968 maintenance packet TEXT\n\
8969 If GDB is talking to an inferior via the GDB serial protocol, then\n\
8970 this command sends the string TEXT to the inferior, and displays the\n\
8971 response packet. GDB supplies the initial `$' character, and the\n\
8972 terminating `#' character and checksum."),
8975 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
8976 Set whether to send break if interrupted."), _("\
8977 Show whether to send break if interrupted."), _("\
8978 If set, a break, instead of a cntrl-c, is sent to the remote target."),
8979 NULL
, NULL
, /* FIXME: i18n: Whether to send break if interrupted is %s. */
8980 &setlist
, &showlist
);
8982 /* Install commands for configuring memory read/write packets. */
8984 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
8985 Set the maximum number of bytes per memory write packet (deprecated)."),
8987 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
8988 Show the maximum number of bytes per memory write packet (deprecated)."),
8990 add_cmd ("memory-write-packet-size", no_class
,
8991 set_memory_write_packet_size
, _("\
8992 Set the maximum number of bytes per memory-write packet.\n\
8993 Specify the number of bytes in a packet or 0 (zero) for the\n\
8994 default packet size. The actual limit is further reduced\n\
8995 dependent on the target. Specify ``fixed'' to disable the\n\
8996 further restriction and ``limit'' to enable that restriction."),
8997 &remote_set_cmdlist
);
8998 add_cmd ("memory-read-packet-size", no_class
,
8999 set_memory_read_packet_size
, _("\
9000 Set the maximum number of bytes per memory-read packet.\n\
9001 Specify the number of bytes in a packet or 0 (zero) for the\n\
9002 default packet size. The actual limit is further reduced\n\
9003 dependent on the target. Specify ``fixed'' to disable the\n\
9004 further restriction and ``limit'' to enable that restriction."),
9005 &remote_set_cmdlist
);
9006 add_cmd ("memory-write-packet-size", no_class
,
9007 show_memory_write_packet_size
,
9008 _("Show the maximum number of bytes per memory-write packet."),
9009 &remote_show_cmdlist
);
9010 add_cmd ("memory-read-packet-size", no_class
,
9011 show_memory_read_packet_size
,
9012 _("Show the maximum number of bytes per memory-read packet."),
9013 &remote_show_cmdlist
);
9015 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
9016 &remote_hw_watchpoint_limit
, _("\
9017 Set the maximum number of target hardware watchpoints."), _("\
9018 Show the maximum number of target hardware watchpoints."), _("\
9019 Specify a negative limit for unlimited."),
9020 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
9021 &remote_set_cmdlist
, &remote_show_cmdlist
);
9022 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
9023 &remote_hw_breakpoint_limit
, _("\
9024 Set the maximum number of target hardware breakpoints."), _("\
9025 Show the maximum number of target hardware breakpoints."), _("\
9026 Specify a negative limit for unlimited."),
9027 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
9028 &remote_set_cmdlist
, &remote_show_cmdlist
);
9030 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
9031 &remote_address_size
, _("\
9032 Set the maximum size of the address (in bits) in a memory packet."), _("\
9033 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
9035 NULL
, /* FIXME: i18n: */
9036 &setlist
, &showlist
);
9038 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
9039 "X", "binary-download", 1);
9041 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
9042 "vCont", "verbose-resume", 0);
9044 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
9045 "QPassSignals", "pass-signals", 0);
9047 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
9048 "qSymbol", "symbol-lookup", 0);
9050 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
9051 "P", "set-register", 1);
9053 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
9054 "p", "fetch-register", 1);
9056 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
9057 "Z0", "software-breakpoint", 0);
9059 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
9060 "Z1", "hardware-breakpoint", 0);
9062 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
9063 "Z2", "write-watchpoint", 0);
9065 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
9066 "Z3", "read-watchpoint", 0);
9068 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
9069 "Z4", "access-watchpoint", 0);
9071 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
9072 "qXfer:auxv:read", "read-aux-vector", 0);
9074 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
9075 "qXfer:features:read", "target-features", 0);
9077 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
9078 "qXfer:libraries:read", "library-info", 0);
9080 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
9081 "qXfer:memory-map:read", "memory-map", 0);
9083 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
9084 "qXfer:spu:read", "read-spu-object", 0);
9086 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
9087 "qXfer:spu:write", "write-spu-object", 0);
9089 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
9090 "qXfer:osdata:read", "osdata", 0);
9092 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_read
],
9093 "qXfer:siginfo:read", "read-siginfo-object", 0);
9095 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_write
],
9096 "qXfer:siginfo:write", "write-siginfo-object", 0);
9098 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
9099 "qGetTLSAddr", "get-thread-local-storage-address",
9102 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
9103 "qSupported", "supported-packets", 0);
9105 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
9106 "qSearch:memory", "search-memory", 0);
9108 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
9109 "vFile:open", "hostio-open", 0);
9111 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
9112 "vFile:pread", "hostio-pread", 0);
9114 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
9115 "vFile:pwrite", "hostio-pwrite", 0);
9117 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
9118 "vFile:close", "hostio-close", 0);
9120 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
9121 "vFile:unlink", "hostio-unlink", 0);
9123 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
9124 "vAttach", "attach", 0);
9126 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
9129 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
9130 "QStartNoAckMode", "noack", 0);
9132 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
9133 "vKill", "kill", 0);
9135 /* Keep the old ``set remote Z-packet ...'' working. Each individual
9136 Z sub-packet has its own set and show commands, but users may
9137 have sets to this variable in their .gdbinit files (or in their
9139 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
9140 &remote_Z_packet_detect
, _("\
9141 Set use of remote protocol `Z' packets"), _("\
9142 Show use of remote protocol `Z' packets "), _("\
9143 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
9145 set_remote_protocol_Z_packet_cmd
,
9146 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
9147 &remote_set_cmdlist
, &remote_show_cmdlist
);
9149 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
9150 Manipulate files on the remote system\n\
9151 Transfer files to and from the remote target system."),
9152 &remote_cmdlist
, "remote ",
9153 0 /* allow-unknown */, &cmdlist
);
9155 add_cmd ("put", class_files
, remote_put_command
,
9156 _("Copy a local file to the remote system."),
9159 add_cmd ("get", class_files
, remote_get_command
,
9160 _("Copy a remote file to the local system."),
9163 add_cmd ("delete", class_files
, remote_delete_command
,
9164 _("Delete a remote file."),
9167 remote_exec_file
= xstrdup ("");
9168 add_setshow_string_noescape_cmd ("exec-file", class_files
,
9169 &remote_exec_file
, _("\
9170 Set the remote pathname for \"run\""), _("\
9171 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
9172 &remote_set_cmdlist
, &remote_show_cmdlist
);
9174 /* Eventually initialize fileio. See fileio.c */
9175 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
9177 /* Take advantage of the fact that the LWP field is not used, to tag
9178 special ptids with it set to != 0. */
9179 magic_null_ptid
= ptid_build (42000, 1, -1);
9180 not_sent_ptid
= ptid_build (42000, 1, -2);
9181 any_thread_ptid
= ptid_build (42000, 1, 0);