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
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 ptid_t
remote_wait (ptid_t ptid
,
121 struct target_waitstatus
*status
);
123 static void remote_kill (void);
125 static int tohex (int nib
);
127 static int remote_can_async_p (void);
129 static int remote_is_async_p (void);
131 static void remote_async (void (*callback
) (enum inferior_event_type event_type
,
132 void *context
), void *context
);
134 static int remote_async_mask (int new_mask
);
136 static void remote_detach (struct target_ops
*ops
, char *args
, int from_tty
);
138 static void remote_interrupt (int signo
);
140 static void remote_interrupt_twice (int signo
);
142 static void interrupt_query (void);
144 static void set_general_thread (struct ptid ptid
);
145 static void set_continue_thread (struct ptid ptid
);
147 static int remote_thread_alive (ptid_t
);
149 static void get_offsets (void);
151 static void skip_frame (void);
153 static long read_frame (char **buf_p
, long *sizeof_buf
);
155 static int hexnumlen (ULONGEST num
);
157 static void init_remote_ops (void);
159 static void init_extended_remote_ops (void);
161 static void remote_stop (ptid_t
);
163 static int ishex (int ch
, int *val
);
165 static int stubhex (int ch
);
167 static int hexnumstr (char *, ULONGEST
);
169 static int hexnumnstr (char *, ULONGEST
, int);
171 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
173 static void print_packet (char *);
175 static unsigned long crc32 (unsigned char *, int, unsigned int);
177 static void compare_sections_command (char *, int);
179 static void packet_command (char *, int);
181 static int stub_unpack_int (char *buff
, int fieldlength
);
183 static ptid_t
remote_current_thread (ptid_t oldptid
);
185 static void remote_find_new_threads (void);
187 static void record_currthread (ptid_t currthread
);
189 static int fromhex (int a
);
191 static int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
193 static int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
195 static int putpkt_binary (char *buf
, int cnt
);
197 static void check_binary_download (CORE_ADDR addr
);
199 struct packet_config
;
201 static void show_packet_config_cmd (struct packet_config
*config
);
203 static void update_packet_config (struct packet_config
*config
);
205 static void set_remote_protocol_packet_cmd (char *args
, int from_tty
,
206 struct cmd_list_element
*c
);
208 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
210 struct cmd_list_element
*c
,
213 static char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
214 static ptid_t
read_ptid (char *buf
, char **obuf
);
216 static void remote_query_supported (void);
218 static void remote_check_symbols (struct objfile
*objfile
);
220 void _initialize_remote (void);
223 static struct stop_reply
*stop_reply_xmalloc (void);
224 static void stop_reply_xfree (struct stop_reply
*);
225 static void do_stop_reply_xfree (void *arg
);
226 static void remote_parse_stop_reply (char *buf
, struct stop_reply
*);
227 static void push_stop_reply (struct stop_reply
*);
228 static void remote_get_pending_stop_replies (void);
229 static void discard_pending_stop_replies (int pid
);
230 static int peek_stop_reply (ptid_t ptid
);
232 static void remote_async_inferior_event_handler (gdb_client_data
);
233 static void remote_async_get_pending_events_handler (gdb_client_data
);
235 static void remote_terminal_ours (void);
237 static int remote_read_description_p (struct target_ops
*target
);
239 /* The non-stop remote protocol provisions for one pending stop reply.
240 This is where we keep it until it is acknowledged. */
242 static struct stop_reply
*pending_stop_reply
= NULL
;
246 static struct cmd_list_element
*remote_cmdlist
;
248 /* For "set remote" and "show remote". */
250 static struct cmd_list_element
*remote_set_cmdlist
;
251 static struct cmd_list_element
*remote_show_cmdlist
;
253 /* Description of the remote protocol state for the currently
254 connected target. This is per-target state, and independent of the
255 selected architecture. */
259 /* A buffer to use for incoming packets, and its current size. The
260 buffer is grown dynamically for larger incoming packets.
261 Outgoing packets may also be constructed in this buffer.
262 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
263 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
268 /* If we negotiated packet size explicitly (and thus can bypass
269 heuristics for the largest packet size that will not overflow
270 a buffer in the stub), this will be set to that packet size.
271 Otherwise zero, meaning to use the guessed size. */
272 long explicit_packet_size
;
274 /* remote_wait is normally called when the target is running and
275 waits for a stop reply packet. But sometimes we need to call it
276 when the target is already stopped. We can send a "?" packet
277 and have remote_wait read the response. Or, if we already have
278 the response, we can stash it in BUF and tell remote_wait to
279 skip calling getpkt. This flag is set when BUF contains a
280 stop reply packet and the target is not waiting. */
281 int cached_wait_status
;
283 /* True, if in no ack mode. That is, neither GDB nor the stub will
284 expect acks from each other. The connection is assumed to be
288 /* True if we're connected in extended remote mode. */
291 /* True if the stub reported support for multi-process
293 int multi_process_aware
;
295 /* True if we resumed the target and we're waiting for the target to
296 stop. In the mean time, we can't start another command/query.
297 The remote server wouldn't be ready to process it, so we'd
298 timeout waiting for a reply that would never come and eventually
299 we'd close the connection. This can happen in asynchronous mode
300 because we allow GDB commands while the target is running. */
301 int waiting_for_stop_reply
;
303 /* True if the stub reports support for non-stop mode. */
306 /* True if the stub reports support for vCont;t. */
310 /* Returns true if the multi-process extensions are in effect. */
312 remote_multi_process_p (struct remote_state
*rs
)
314 return rs
->extended
&& rs
->multi_process_aware
;
317 /* This data could be associated with a target, but we do not always
318 have access to the current target when we need it, so for now it is
319 static. This will be fine for as long as only one target is in use
321 static struct remote_state remote_state
;
323 static struct remote_state
*
324 get_remote_state_raw (void)
326 return &remote_state
;
329 /* Description of the remote protocol for a given architecture. */
333 long offset
; /* Offset into G packet. */
334 long regnum
; /* GDB's internal register number. */
335 LONGEST pnum
; /* Remote protocol register number. */
336 int in_g_packet
; /* Always part of G packet. */
337 /* long size in bytes; == register_size (target_gdbarch, regnum);
339 /* char *name; == gdbarch_register_name (target_gdbarch, regnum);
343 struct remote_arch_state
345 /* Description of the remote protocol registers. */
346 long sizeof_g_packet
;
348 /* Description of the remote protocol registers indexed by REGNUM
349 (making an array gdbarch_num_regs in size). */
350 struct packet_reg
*regs
;
352 /* This is the size (in chars) of the first response to the ``g''
353 packet. It is used as a heuristic when determining the maximum
354 size of memory-read and memory-write packets. A target will
355 typically only reserve a buffer large enough to hold the ``g''
356 packet. The size does not include packet overhead (headers and
358 long actual_register_packet_size
;
360 /* This is the maximum size (in chars) of a non read/write packet.
361 It is also used as a cap on the size of read/write packets. */
362 long remote_packet_size
;
366 /* Handle for retreving the remote protocol data from gdbarch. */
367 static struct gdbarch_data
*remote_gdbarch_data_handle
;
369 static struct remote_arch_state
*
370 get_remote_arch_state (void)
372 return gdbarch_data (target_gdbarch
, remote_gdbarch_data_handle
);
375 /* Fetch the global remote target state. */
377 static struct remote_state
*
378 get_remote_state (void)
380 /* Make sure that the remote architecture state has been
381 initialized, because doing so might reallocate rs->buf. Any
382 function which calls getpkt also needs to be mindful of changes
383 to rs->buf, but this call limits the number of places which run
385 get_remote_arch_state ();
387 return get_remote_state_raw ();
391 compare_pnums (const void *lhs_
, const void *rhs_
)
393 const struct packet_reg
* const *lhs
= lhs_
;
394 const struct packet_reg
* const *rhs
= rhs_
;
396 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
398 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
405 init_remote_state (struct gdbarch
*gdbarch
)
407 int regnum
, num_remote_regs
, offset
;
408 struct remote_state
*rs
= get_remote_state_raw ();
409 struct remote_arch_state
*rsa
;
410 struct packet_reg
**remote_regs
;
412 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
414 /* Use the architecture to build a regnum<->pnum table, which will be
415 1:1 unless a feature set specifies otherwise. */
416 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
,
417 gdbarch_num_regs (gdbarch
),
419 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
421 struct packet_reg
*r
= &rsa
->regs
[regnum
];
423 if (register_size (gdbarch
, regnum
) == 0)
424 /* Do not try to fetch zero-sized (placeholder) registers. */
427 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
432 /* Define the g/G packet format as the contents of each register
433 with a remote protocol number, in order of ascending protocol
436 remote_regs
= alloca (gdbarch_num_regs (gdbarch
)
437 * sizeof (struct packet_reg
*));
438 for (num_remote_regs
= 0, regnum
= 0;
439 regnum
< gdbarch_num_regs (gdbarch
);
441 if (rsa
->regs
[regnum
].pnum
!= -1)
442 remote_regs
[num_remote_regs
++] = &rsa
->regs
[regnum
];
444 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
447 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
449 remote_regs
[regnum
]->in_g_packet
= 1;
450 remote_regs
[regnum
]->offset
= offset
;
451 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
454 /* Record the maximum possible size of the g packet - it may turn out
456 rsa
->sizeof_g_packet
= offset
;
458 /* Default maximum number of characters in a packet body. Many
459 remote stubs have a hardwired buffer size of 400 bytes
460 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
461 as the maximum packet-size to ensure that the packet and an extra
462 NUL character can always fit in the buffer. This stops GDB
463 trashing stubs that try to squeeze an extra NUL into what is
464 already a full buffer (As of 1999-12-04 that was most stubs). */
465 rsa
->remote_packet_size
= 400 - 1;
467 /* This one is filled in when a ``g'' packet is received. */
468 rsa
->actual_register_packet_size
= 0;
470 /* Should rsa->sizeof_g_packet needs more space than the
471 default, adjust the size accordingly. Remember that each byte is
472 encoded as two characters. 32 is the overhead for the packet
473 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
474 (``$NN:G...#NN'') is a better guess, the below has been padded a
476 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
477 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
479 /* Make sure that the packet buffer is plenty big enough for
480 this architecture. */
481 if (rs
->buf_size
< rsa
->remote_packet_size
)
483 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
484 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
490 /* Return the current allowed size of a remote packet. This is
491 inferred from the current architecture, and should be used to
492 limit the length of outgoing packets. */
494 get_remote_packet_size (void)
496 struct remote_state
*rs
= get_remote_state ();
497 struct remote_arch_state
*rsa
= get_remote_arch_state ();
499 if (rs
->explicit_packet_size
)
500 return rs
->explicit_packet_size
;
502 return rsa
->remote_packet_size
;
505 static struct packet_reg
*
506 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
508 if (regnum
< 0 && regnum
>= gdbarch_num_regs (target_gdbarch
))
512 struct packet_reg
*r
= &rsa
->regs
[regnum
];
513 gdb_assert (r
->regnum
== regnum
);
518 static struct packet_reg
*
519 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
522 for (i
= 0; i
< gdbarch_num_regs (target_gdbarch
); i
++)
524 struct packet_reg
*r
= &rsa
->regs
[i
];
531 /* FIXME: graces/2002-08-08: These variables should eventually be
532 bound to an instance of the target object (as in gdbarch-tdep()),
533 when such a thing exists. */
535 /* This is set to the data address of the access causing the target
536 to stop for a watchpoint. */
537 static CORE_ADDR remote_watch_data_address
;
539 /* This is non-zero if target stopped for a watchpoint. */
540 static int remote_stopped_by_watchpoint_p
;
542 static struct target_ops remote_ops
;
544 static struct target_ops extended_remote_ops
;
546 static int remote_async_mask_value
= 1;
548 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
549 ``forever'' still use the normal timeout mechanism. This is
550 currently used by the ASYNC code to guarentee that target reads
551 during the initial connect always time-out. Once getpkt has been
552 modified to return a timeout indication and, in turn
553 remote_wait()/wait_for_inferior() have gained a timeout parameter
555 static int wait_forever_enabled_p
= 1;
558 /* This variable chooses whether to send a ^C or a break when the user
559 requests program interruption. Although ^C is usually what remote
560 systems expect, and that is the default here, sometimes a break is
561 preferable instead. */
563 static int remote_break
;
565 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
566 remote_open knows that we don't have a file open when the program
568 static struct serial
*remote_desc
= NULL
;
570 /* This variable sets the number of bits in an address that are to be
571 sent in a memory ("M" or "m") packet. Normally, after stripping
572 leading zeros, the entire address would be sent. This variable
573 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
574 initial implementation of remote.c restricted the address sent in
575 memory packets to ``host::sizeof long'' bytes - (typically 32
576 bits). Consequently, for 64 bit targets, the upper 32 bits of an
577 address was never sent. Since fixing this bug may cause a break in
578 some remote targets this variable is principly provided to
579 facilitate backward compatibility. */
581 static int remote_address_size
;
583 /* Temporary to track who currently owns the terminal. See
584 remote_terminal_* for more details. */
586 static int remote_async_terminal_ours_p
;
588 /* The executable file to use for "run" on the remote side. */
590 static char *remote_exec_file
= "";
593 /* User configurable variables for the number of characters in a
594 memory read/write packet. MIN (rsa->remote_packet_size,
595 rsa->sizeof_g_packet) is the default. Some targets need smaller
596 values (fifo overruns, et.al.) and some users need larger values
597 (speed up transfers). The variables ``preferred_*'' (the user
598 request), ``current_*'' (what was actually set) and ``forced_*''
599 (Positive - a soft limit, negative - a hard limit). */
601 struct memory_packet_config
608 /* Compute the current size of a read/write packet. Since this makes
609 use of ``actual_register_packet_size'' the computation is dynamic. */
612 get_memory_packet_size (struct memory_packet_config
*config
)
614 struct remote_state
*rs
= get_remote_state ();
615 struct remote_arch_state
*rsa
= get_remote_arch_state ();
617 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
618 law?) that some hosts don't cope very well with large alloca()
619 calls. Eventually the alloca() code will be replaced by calls to
620 xmalloc() and make_cleanups() allowing this restriction to either
621 be lifted or removed. */
622 #ifndef MAX_REMOTE_PACKET_SIZE
623 #define MAX_REMOTE_PACKET_SIZE 16384
625 /* NOTE: 20 ensures we can write at least one byte. */
626 #ifndef MIN_REMOTE_PACKET_SIZE
627 #define MIN_REMOTE_PACKET_SIZE 20
632 if (config
->size
<= 0)
633 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
635 what_they_get
= config
->size
;
639 what_they_get
= get_remote_packet_size ();
640 /* Limit the packet to the size specified by the user. */
642 && what_they_get
> config
->size
)
643 what_they_get
= config
->size
;
645 /* Limit it to the size of the targets ``g'' response unless we have
646 permission from the stub to use a larger packet size. */
647 if (rs
->explicit_packet_size
== 0
648 && rsa
->actual_register_packet_size
> 0
649 && what_they_get
> rsa
->actual_register_packet_size
)
650 what_they_get
= rsa
->actual_register_packet_size
;
652 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
653 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
654 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
655 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
657 /* Make sure there is room in the global buffer for this packet
658 (including its trailing NUL byte). */
659 if (rs
->buf_size
< what_they_get
+ 1)
661 rs
->buf_size
= 2 * what_they_get
;
662 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
665 return what_they_get
;
668 /* Update the size of a read/write packet. If they user wants
669 something really big then do a sanity check. */
672 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
674 int fixed_p
= config
->fixed_p
;
675 long size
= config
->size
;
677 error (_("Argument required (integer, `fixed' or `limited')."));
678 else if (strcmp (args
, "hard") == 0
679 || strcmp (args
, "fixed") == 0)
681 else if (strcmp (args
, "soft") == 0
682 || strcmp (args
, "limit") == 0)
687 size
= strtoul (args
, &end
, 0);
689 error (_("Invalid %s (bad syntax)."), config
->name
);
691 /* Instead of explicitly capping the size of a packet to
692 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
693 instead allowed to set the size to something arbitrarily
695 if (size
> MAX_REMOTE_PACKET_SIZE
)
696 error (_("Invalid %s (too large)."), config
->name
);
700 if (fixed_p
&& !config
->fixed_p
)
702 if (! query (_("The target may not be able to correctly handle a %s\n"
703 "of %ld bytes. Change the packet size? "),
705 error (_("Packet size not changed."));
707 /* Update the config. */
708 config
->fixed_p
= fixed_p
;
713 show_memory_packet_size (struct memory_packet_config
*config
)
715 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
717 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
718 get_memory_packet_size (config
));
720 printf_filtered (_("Packets are limited to %ld bytes.\n"),
721 get_memory_packet_size (config
));
724 static struct memory_packet_config memory_write_packet_config
=
726 "memory-write-packet-size",
730 set_memory_write_packet_size (char *args
, int from_tty
)
732 set_memory_packet_size (args
, &memory_write_packet_config
);
736 show_memory_write_packet_size (char *args
, int from_tty
)
738 show_memory_packet_size (&memory_write_packet_config
);
742 get_memory_write_packet_size (void)
744 return get_memory_packet_size (&memory_write_packet_config
);
747 static struct memory_packet_config memory_read_packet_config
=
749 "memory-read-packet-size",
753 set_memory_read_packet_size (char *args
, int from_tty
)
755 set_memory_packet_size (args
, &memory_read_packet_config
);
759 show_memory_read_packet_size (char *args
, int from_tty
)
761 show_memory_packet_size (&memory_read_packet_config
);
765 get_memory_read_packet_size (void)
767 long size
= get_memory_packet_size (&memory_read_packet_config
);
768 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
769 extra buffer size argument before the memory read size can be
770 increased beyond this. */
771 if (size
> get_remote_packet_size ())
772 size
= get_remote_packet_size ();
777 /* Generic configuration support for packets the stub optionally
778 supports. Allows the user to specify the use of the packet as well
779 as allowing GDB to auto-detect support in the remote stub. */
783 PACKET_SUPPORT_UNKNOWN
= 0,
792 enum auto_boolean detect
;
793 enum packet_support support
;
796 /* Analyze a packet's return value and update the packet config
807 update_packet_config (struct packet_config
*config
)
809 switch (config
->detect
)
811 case AUTO_BOOLEAN_TRUE
:
812 config
->support
= PACKET_ENABLE
;
814 case AUTO_BOOLEAN_FALSE
:
815 config
->support
= PACKET_DISABLE
;
817 case AUTO_BOOLEAN_AUTO
:
818 config
->support
= PACKET_SUPPORT_UNKNOWN
;
824 show_packet_config_cmd (struct packet_config
*config
)
826 char *support
= "internal-error";
827 switch (config
->support
)
833 support
= "disabled";
835 case PACKET_SUPPORT_UNKNOWN
:
839 switch (config
->detect
)
841 case AUTO_BOOLEAN_AUTO
:
842 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
843 config
->name
, support
);
845 case AUTO_BOOLEAN_TRUE
:
846 case AUTO_BOOLEAN_FALSE
:
847 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
848 config
->name
, support
);
854 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
855 const char *title
, int legacy
)
862 config
->title
= title
;
863 config
->detect
= AUTO_BOOLEAN_AUTO
;
864 config
->support
= PACKET_SUPPORT_UNKNOWN
;
865 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
867 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
869 /* set/show TITLE-packet {auto,on,off} */
870 cmd_name
= xstrprintf ("%s-packet", title
);
871 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
872 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
873 set_remote_protocol_packet_cmd
,
874 show_remote_protocol_packet_cmd
,
875 &remote_set_cmdlist
, &remote_show_cmdlist
);
876 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
880 legacy_name
= xstrprintf ("%s-packet", name
);
881 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
882 &remote_set_cmdlist
);
883 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
884 &remote_show_cmdlist
);
888 static enum packet_result
889 packet_check_result (const char *buf
)
893 /* The stub recognized the packet request. Check that the
894 operation succeeded. */
896 && isxdigit (buf
[1]) && isxdigit (buf
[2])
898 /* "Enn" - definitly an error. */
901 /* Always treat "E." as an error. This will be used for
902 more verbose error messages, such as E.memtypes. */
903 if (buf
[0] == 'E' && buf
[1] == '.')
906 /* The packet may or may not be OK. Just assume it is. */
910 /* The stub does not support the packet. */
911 return PACKET_UNKNOWN
;
914 static enum packet_result
915 packet_ok (const char *buf
, struct packet_config
*config
)
917 enum packet_result result
;
919 result
= packet_check_result (buf
);
924 /* The stub recognized the packet request. */
925 switch (config
->support
)
927 case PACKET_SUPPORT_UNKNOWN
:
929 fprintf_unfiltered (gdb_stdlog
,
930 "Packet %s (%s) is supported\n",
931 config
->name
, config
->title
);
932 config
->support
= PACKET_ENABLE
;
935 internal_error (__FILE__
, __LINE__
,
936 _("packet_ok: attempt to use a disabled packet"));
943 /* The stub does not support the packet. */
944 switch (config
->support
)
947 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
948 /* If the stub previously indicated that the packet was
949 supported then there is a protocol error.. */
950 error (_("Protocol error: %s (%s) conflicting enabled responses."),
951 config
->name
, config
->title
);
953 /* The user set it wrong. */
954 error (_("Enabled packet %s (%s) not recognized by stub"),
955 config
->name
, config
->title
);
957 case PACKET_SUPPORT_UNKNOWN
:
959 fprintf_unfiltered (gdb_stdlog
,
960 "Packet %s (%s) is NOT supported\n",
961 config
->name
, config
->title
);
962 config
->support
= PACKET_DISABLE
;
990 PACKET_qXfer_features
,
991 PACKET_qXfer_libraries
,
992 PACKET_qXfer_memory_map
,
993 PACKET_qXfer_spu_read
,
994 PACKET_qXfer_spu_write
,
998 PACKET_qSearch_memory
,
1001 PACKET_QStartNoAckMode
,
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. */
1530 pid
= ptid_get_pid (inferior_ptid
);
1534 return ptid_build (pid
, 0, tid
);
1537 /* Encode 64 bits in 16 chars of hex. */
1539 static const char hexchars
[] = "0123456789abcdef";
1542 ishex (int ch
, int *val
)
1544 if ((ch
>= 'a') && (ch
<= 'f'))
1546 *val
= ch
- 'a' + 10;
1549 if ((ch
>= 'A') && (ch
<= 'F'))
1551 *val
= ch
- 'A' + 10;
1554 if ((ch
>= '0') && (ch
<= '9'))
1565 if (ch
>= 'a' && ch
<= 'f')
1566 return ch
- 'a' + 10;
1567 if (ch
>= '0' && ch
<= '9')
1569 if (ch
>= 'A' && ch
<= 'F')
1570 return ch
- 'A' + 10;
1575 stub_unpack_int (char *buff
, int fieldlength
)
1582 nibble
= stubhex (*buff
++);
1586 retval
= retval
<< 4;
1592 unpack_varlen_hex (char *buff
, /* packet to parse */
1596 ULONGEST retval
= 0;
1598 while (ishex (*buff
, &nibble
))
1601 retval
= retval
<< 4;
1602 retval
|= nibble
& 0x0f;
1609 unpack_nibble (char *buf
, int *val
)
1611 *val
= fromhex (*buf
++);
1616 pack_nibble (char *buf
, int nibble
)
1618 *buf
++ = hexchars
[(nibble
& 0x0f)];
1623 pack_hex_byte (char *pkt
, int byte
)
1625 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1626 *pkt
++ = hexchars
[(byte
& 0xf)];
1631 unpack_byte (char *buf
, int *value
)
1633 *value
= stub_unpack_int (buf
, 2);
1638 pack_int (char *buf
, int value
)
1640 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1641 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1642 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1643 buf
= pack_hex_byte (buf
, (value
& 0xff));
1648 unpack_int (char *buf
, int *value
)
1650 *value
= stub_unpack_int (buf
, 8);
1654 #if 0 /* Currently unused, uncomment when needed. */
1655 static char *pack_string (char *pkt
, char *string
);
1658 pack_string (char *pkt
, char *string
)
1663 len
= strlen (string
);
1665 len
= 200; /* Bigger than most GDB packets, junk??? */
1666 pkt
= pack_hex_byte (pkt
, len
);
1670 if ((ch
== '\0') || (ch
== '#'))
1671 ch
= '*'; /* Protect encapsulation. */
1676 #endif /* 0 (unused) */
1679 unpack_string (char *src
, char *dest
, int length
)
1688 pack_threadid (char *pkt
, threadref
*id
)
1691 unsigned char *altid
;
1693 altid
= (unsigned char *) id
;
1694 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1696 pkt
= pack_hex_byte (pkt
, *altid
++);
1702 unpack_threadid (char *inbuf
, threadref
*id
)
1705 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1708 altref
= (char *) id
;
1710 while (inbuf
< limit
)
1712 x
= stubhex (*inbuf
++);
1713 y
= stubhex (*inbuf
++);
1714 *altref
++ = (x
<< 4) | y
;
1719 /* Externally, threadrefs are 64 bits but internally, they are still
1720 ints. This is due to a mismatch of specifications. We would like
1721 to use 64bit thread references internally. This is an adapter
1725 int_to_threadref (threadref
*id
, int value
)
1727 unsigned char *scan
;
1729 scan
= (unsigned char *) id
;
1735 *scan
++ = (value
>> 24) & 0xff;
1736 *scan
++ = (value
>> 16) & 0xff;
1737 *scan
++ = (value
>> 8) & 0xff;
1738 *scan
++ = (value
& 0xff);
1742 threadref_to_int (threadref
*ref
)
1745 unsigned char *scan
;
1751 value
= (value
<< 8) | ((*scan
++) & 0xff);
1756 copy_threadref (threadref
*dest
, threadref
*src
)
1759 unsigned char *csrc
, *cdest
;
1761 csrc
= (unsigned char *) src
;
1762 cdest
= (unsigned char *) dest
;
1769 threadmatch (threadref
*dest
, threadref
*src
)
1771 /* Things are broken right now, so just assume we got a match. */
1773 unsigned char *srcp
, *destp
;
1775 srcp
= (char *) src
;
1776 destp
= (char *) dest
;
1780 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1787 threadid:1, # always request threadid
1794 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1797 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1799 *pkt
++ = 'q'; /* Info Query */
1800 *pkt
++ = 'P'; /* process or thread info */
1801 pkt
= pack_int (pkt
, mode
); /* mode */
1802 pkt
= pack_threadid (pkt
, id
); /* threadid */
1803 *pkt
= '\0'; /* terminate */
1807 /* These values tag the fields in a thread info response packet. */
1808 /* Tagging the fields allows us to request specific fields and to
1809 add more fields as time goes by. */
1811 #define TAG_THREADID 1 /* Echo the thread identifier. */
1812 #define TAG_EXISTS 2 /* Is this process defined enough to
1813 fetch registers and its stack? */
1814 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1815 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1816 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1820 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1821 struct gdb_ext_thread_info
*info
)
1823 struct remote_state
*rs
= get_remote_state ();
1827 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
1830 /* info->threadid = 0; FIXME: implement zero_threadref. */
1832 info
->display
[0] = '\0';
1833 info
->shortname
[0] = '\0';
1834 info
->more_display
[0] = '\0';
1836 /* Assume the characters indicating the packet type have been
1838 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1839 pkt
= unpack_threadid (pkt
, &ref
);
1842 warning (_("Incomplete response to threadinfo request."));
1843 if (!threadmatch (&ref
, expectedref
))
1844 { /* This is an answer to a different request. */
1845 warning (_("ERROR RMT Thread info mismatch."));
1848 copy_threadref (&info
->threadid
, &ref
);
1850 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1852 /* Packets are terminated with nulls. */
1853 while ((pkt
< limit
) && mask
&& *pkt
)
1855 pkt
= unpack_int (pkt
, &tag
); /* tag */
1856 pkt
= unpack_byte (pkt
, &length
); /* length */
1857 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1859 warning (_("ERROR RMT: threadinfo tag mismatch."));
1863 if (tag
== TAG_THREADID
)
1867 warning (_("ERROR RMT: length of threadid is not 16."));
1871 pkt
= unpack_threadid (pkt
, &ref
);
1872 mask
= mask
& ~TAG_THREADID
;
1875 if (tag
== TAG_EXISTS
)
1877 info
->active
= stub_unpack_int (pkt
, length
);
1879 mask
= mask
& ~(TAG_EXISTS
);
1882 warning (_("ERROR RMT: 'exists' length too long."));
1888 if (tag
== TAG_THREADNAME
)
1890 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1891 mask
= mask
& ~TAG_THREADNAME
;
1894 if (tag
== TAG_DISPLAY
)
1896 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1897 mask
= mask
& ~TAG_DISPLAY
;
1900 if (tag
== TAG_MOREDISPLAY
)
1902 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1903 mask
= mask
& ~TAG_MOREDISPLAY
;
1906 warning (_("ERROR RMT: unknown thread info tag."));
1907 break; /* Not a tag we know about. */
1913 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1914 struct gdb_ext_thread_info
*info
)
1916 struct remote_state
*rs
= get_remote_state ();
1919 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
1921 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1923 if (rs
->buf
[0] == '\0')
1926 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
1931 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1934 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1935 threadref
*nextthread
)
1937 *pkt
++ = 'q'; /* info query packet */
1938 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1939 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1940 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1941 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1946 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1949 parse_threadlist_response (char *pkt
, int result_limit
,
1950 threadref
*original_echo
, threadref
*resultlist
,
1953 struct remote_state
*rs
= get_remote_state ();
1955 int count
, resultcount
, done
;
1958 /* Assume the 'q' and 'M chars have been stripped. */
1959 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
1960 /* done parse past here */
1961 pkt
= unpack_byte (pkt
, &count
); /* count field */
1962 pkt
= unpack_nibble (pkt
, &done
);
1963 /* The first threadid is the argument threadid. */
1964 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1965 while ((count
-- > 0) && (pkt
< limit
))
1967 pkt
= unpack_threadid (pkt
, resultlist
++);
1968 if (resultcount
++ >= result_limit
)
1977 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1978 int *done
, int *result_count
, threadref
*threadlist
)
1980 struct remote_state
*rs
= get_remote_state ();
1981 static threadref echo_nextthread
;
1984 /* Trancate result limit to be smaller than the packet size. */
1985 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= get_remote_packet_size ())
1986 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
1988 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
1990 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1992 if (*rs
->buf
== '\0')
1996 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
1999 if (!threadmatch (&echo_nextthread
, nextthread
))
2001 /* FIXME: This is a good reason to drop the packet. */
2002 /* Possably, there is a duplicate response. */
2004 retransmit immediatly - race conditions
2005 retransmit after timeout - yes
2007 wait for packet, then exit
2009 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2010 return 0; /* I choose simply exiting. */
2012 if (*result_count
<= 0)
2016 warning (_("RMT ERROR : failed to get remote thread list."));
2019 return result
; /* break; */
2021 if (*result_count
> result_limit
)
2024 warning (_("RMT ERROR: threadlist response longer than requested."));
2030 /* This is the interface between remote and threads, remotes upper
2033 /* remote_find_new_threads retrieves the thread list and for each
2034 thread in the list, looks up the thread in GDB's internal list,
2035 adding the thread if it does not already exist. This involves
2036 getting partial thread lists from the remote target so, polling the
2037 quit_flag is required. */
2040 /* About this many threadisds fit in a packet. */
2042 #define MAXTHREADLISTRESULTS 32
2045 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2048 int done
, i
, result_count
;
2052 static threadref nextthread
;
2053 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
2058 if (loopcount
++ > looplimit
)
2061 warning (_("Remote fetch threadlist -infinite loop-."));
2064 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
2065 &done
, &result_count
, resultthreadlist
))
2070 /* Clear for later iterations. */
2072 /* Setup to resume next batch of thread references, set nextthread. */
2073 if (result_count
>= 1)
2074 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
2076 while (result_count
--)
2077 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
2084 remote_newthread_step (threadref
*ref
, void *context
)
2086 int pid
= ptid_get_pid (inferior_ptid
);
2087 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2089 if (!in_thread_list (ptid
))
2091 return 1; /* continue iterator */
2094 #define CRAZY_MAX_THREADS 1000
2097 remote_current_thread (ptid_t oldpid
)
2099 struct remote_state
*rs
= get_remote_state ();
2105 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2106 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2107 return read_ptid (&rs
->buf
[2], NULL
);
2112 /* Find new threads for info threads command.
2113 * Original version, using John Metzler's thread protocol.
2117 remote_find_new_threads (void)
2119 remote_threadlist_iterator (remote_newthread_step
, 0,
2124 * Find all threads for info threads command.
2125 * Uses new thread protocol contributed by Cisco.
2126 * Falls back and attempts to use the older method (above)
2127 * if the target doesn't respond to the new method.
2131 remote_threads_info (void)
2133 struct remote_state
*rs
= get_remote_state ();
2137 if (remote_desc
== 0) /* paranoia */
2138 error (_("Command can only be used when connected to the remote target."));
2140 if (use_threadinfo_query
)
2142 putpkt ("qfThreadInfo");
2143 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2145 if (bufp
[0] != '\0') /* q packet recognized */
2147 while (*bufp
++ == 'm') /* reply contains one or more TID */
2151 new_thread
= read_ptid (bufp
, &bufp
);
2152 if (!ptid_equal (new_thread
, null_ptid
)
2153 && (!in_thread_list (new_thread
)
2154 || is_exited (new_thread
)))
2156 /* When connected to a multi-process aware stub,
2157 "info threads" may show up threads of
2158 inferiors we didn't know about yet. Add them
2159 now, and before adding any of its child
2160 threads, so notifications are emitted in a
2162 if (!in_inferior_list (ptid_get_pid (new_thread
)))
2163 add_inferior (ptid_get_pid (new_thread
));
2165 add_thread (new_thread
);
2167 /* In non-stop mode, we assume new found threads
2168 are running until we proven otherwise with a
2169 stop reply. In all-stop, we can only get
2170 here if all threads are stopped. */
2171 set_executing (new_thread
, non_stop
? 1 : 0);
2172 set_running (new_thread
, non_stop
? 1 : 0);
2175 while (*bufp
++ == ','); /* comma-separated list */
2176 putpkt ("qsThreadInfo");
2177 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2184 /* Only qfThreadInfo is supported in non-stop mode. */
2188 /* Else fall back to old method based on jmetzler protocol. */
2189 use_threadinfo_query
= 0;
2190 remote_find_new_threads ();
2195 * Collect a descriptive string about the given thread.
2196 * The target may say anything it wants to about the thread
2197 * (typically info about its blocked / runnable state, name, etc.).
2198 * This string will appear in the info threads display.
2200 * Optional: targets are not required to implement this function.
2204 remote_threads_extra_info (struct thread_info
*tp
)
2206 struct remote_state
*rs
= get_remote_state ();
2210 struct gdb_ext_thread_info threadinfo
;
2211 static char display_buf
[100]; /* arbitrary... */
2212 int n
= 0; /* position in display_buf */
2214 if (remote_desc
== 0) /* paranoia */
2215 internal_error (__FILE__
, __LINE__
,
2216 _("remote_threads_extra_info"));
2218 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2219 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2220 /* This is the main thread which was added by GDB. The remote
2221 server doesn't know about it. */
2224 if (use_threadextra_query
)
2227 char *endb
= rs
->buf
+ get_remote_packet_size ();
2229 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2231 write_ptid (b
, endb
, tp
->ptid
);
2234 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2235 if (rs
->buf
[0] != 0)
2237 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2238 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2239 display_buf
[result
] = '\0';
2244 /* If the above query fails, fall back to the old method. */
2245 use_threadextra_query
= 0;
2246 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2247 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2248 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2249 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2250 if (threadinfo
.active
)
2252 if (*threadinfo
.shortname
)
2253 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2254 " Name: %s,", threadinfo
.shortname
);
2255 if (*threadinfo
.display
)
2256 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2257 " State: %s,", threadinfo
.display
);
2258 if (*threadinfo
.more_display
)
2259 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2260 " Priority: %s", threadinfo
.more_display
);
2264 /* For purely cosmetic reasons, clear up trailing commas. */
2265 if (',' == display_buf
[n
-1])
2266 display_buf
[n
-1] = ' ';
2274 /* Restart the remote side; this is an extended protocol operation. */
2277 extended_remote_restart (void)
2279 struct remote_state
*rs
= get_remote_state ();
2281 /* Send the restart command; for reasons I don't understand the
2282 remote side really expects a number after the "R". */
2283 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2286 remote_fileio_reset ();
2289 /* Clean up connection to a remote debugger. */
2292 remote_close (int quitting
)
2294 if (remote_desc
== NULL
)
2295 return; /* already closed */
2297 /* Make sure we leave stdin registered in the event loop, and we
2298 don't leave the async SIGINT signal handler installed. */
2299 remote_terminal_ours ();
2301 serial_close (remote_desc
);
2304 /* We don't have a connection to the remote stub anymore. Get rid
2305 of all the inferiors and their threads we were controlling. */
2306 discard_all_inferiors ();
2308 /* We're no longer interested in any of these events. */
2309 discard_pending_stop_replies (-1);
2311 if (remote_async_inferior_event_token
)
2312 delete_async_event_handler (&remote_async_inferior_event_token
);
2313 if (remote_async_get_pending_events_token
)
2314 delete_async_event_handler (&remote_async_get_pending_events_token
);
2316 generic_mourn_inferior ();
2319 /* Query the remote side for the text, data and bss offsets. */
2324 struct remote_state
*rs
= get_remote_state ();
2327 int lose
, num_segments
= 0, do_sections
, do_segments
;
2328 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
2329 struct section_offsets
*offs
;
2330 struct symfile_segment_data
*data
;
2332 if (symfile_objfile
== NULL
)
2335 putpkt ("qOffsets");
2336 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2339 if (buf
[0] == '\000')
2340 return; /* Return silently. Stub doesn't support
2344 warning (_("Remote failure reply: %s"), buf
);
2348 /* Pick up each field in turn. This used to be done with scanf, but
2349 scanf will make trouble if CORE_ADDR size doesn't match
2350 conversion directives correctly. The following code will work
2351 with any size of CORE_ADDR. */
2352 text_addr
= data_addr
= bss_addr
= 0;
2356 if (strncmp (ptr
, "Text=", 5) == 0)
2359 /* Don't use strtol, could lose on big values. */
2360 while (*ptr
&& *ptr
!= ';')
2361 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2363 if (strncmp (ptr
, ";Data=", 6) == 0)
2366 while (*ptr
&& *ptr
!= ';')
2367 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2372 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
2375 while (*ptr
&& *ptr
!= ';')
2376 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
2378 if (bss_addr
!= data_addr
)
2379 warning (_("Target reported unsupported offsets: %s"), buf
);
2384 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
2387 /* Don't use strtol, could lose on big values. */
2388 while (*ptr
&& *ptr
!= ';')
2389 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2392 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
2395 while (*ptr
&& *ptr
!= ';')
2396 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2404 error (_("Malformed response to offset query, %s"), buf
);
2405 else if (*ptr
!= '\0')
2406 warning (_("Target reported unsupported offsets: %s"), buf
);
2408 offs
= ((struct section_offsets
*)
2409 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
2410 memcpy (offs
, symfile_objfile
->section_offsets
,
2411 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
2413 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
2414 do_segments
= (data
!= NULL
);
2415 do_sections
= num_segments
== 0;
2417 if (num_segments
> 0)
2419 segments
[0] = text_addr
;
2420 segments
[1] = data_addr
;
2422 /* If we have two segments, we can still try to relocate everything
2423 by assuming that the .text and .data offsets apply to the whole
2424 text and data segments. Convert the offsets given in the packet
2425 to base addresses for symfile_map_offsets_to_segments. */
2426 else if (data
&& data
->num_segments
== 2)
2428 segments
[0] = data
->segment_bases
[0] + text_addr
;
2429 segments
[1] = data
->segment_bases
[1] + data_addr
;
2432 /* If the object file has only one segment, assume that it is text
2433 rather than data; main programs with no writable data are rare,
2434 but programs with no code are useless. Of course the code might
2435 have ended up in the data segment... to detect that we would need
2436 the permissions here. */
2437 else if (data
&& data
->num_segments
== 1)
2439 segments
[0] = data
->segment_bases
[0] + text_addr
;
2442 /* There's no way to relocate by segment. */
2448 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
2449 offs
, num_segments
, segments
);
2451 if (ret
== 0 && !do_sections
)
2452 error (_("Can not handle qOffsets TextSeg response with this symbol file"));
2459 free_symfile_segment_data (data
);
2463 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
2465 /* This is a temporary kludge to force data and bss to use the same offsets
2466 because that's what nlmconv does now. The real solution requires changes
2467 to the stub and remote.c that I don't have time to do right now. */
2469 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
2470 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
2473 objfile_relocate (symfile_objfile
, offs
);
2476 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
2477 threads we know are stopped already. This is used during the
2478 initial remote connection in non-stop mode --- threads that are
2479 reported as already being stopped are left stopped. */
2482 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
2484 /* If we have a stop reply for this thread, it must be stopped. */
2485 if (peek_stop_reply (thread
->ptid
))
2486 set_stop_requested (thread
->ptid
, 1);
2491 /* Stub for catch_exception. */
2493 struct start_remote_args
2497 /* The current target. */
2498 struct target_ops
*target
;
2500 /* Non-zero if this is an extended-remote target. */
2505 remote_start_remote (struct ui_out
*uiout
, void *opaque
)
2507 struct start_remote_args
*args
= opaque
;
2508 struct remote_state
*rs
= get_remote_state ();
2509 struct packet_config
*noack_config
;
2510 char *wait_status
= NULL
;
2512 immediate_quit
++; /* Allow user to interrupt it. */
2514 /* Ack any packet which the remote side has already sent. */
2515 serial_write (remote_desc
, "+", 1);
2517 /* The first packet we send to the target is the optional "supported
2518 packets" request. If the target can answer this, it will tell us
2519 which later probes to skip. */
2520 remote_query_supported ();
2522 /* Next, we possibly activate noack mode.
2524 If the QStartNoAckMode packet configuration is set to AUTO,
2525 enable noack mode if the stub reported a wish for it with
2528 If set to TRUE, then enable noack mode even if the stub didn't
2529 report it in qSupported. If the stub doesn't reply OK, the
2530 session ends with an error.
2532 If FALSE, then don't activate noack mode, regardless of what the
2533 stub claimed should be the default with qSupported. */
2535 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
2537 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
2538 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
2539 && noack_config
->support
== PACKET_ENABLE
))
2541 putpkt ("QStartNoAckMode");
2542 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2543 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
2547 if (args
->extended_p
)
2549 /* Tell the remote that we are using the extended protocol. */
2551 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2554 /* Next, if the target can specify a description, read it. We do
2555 this before anything involving memory or registers. */
2556 target_find_description ();
2558 /* On OSs where the list of libraries is global to all
2559 processes, we fetch them early. */
2560 if (gdbarch_has_global_solist (target_gdbarch
))
2561 solib_add (NULL
, args
->from_tty
, args
->target
, auto_solib_add
);
2565 if (!rs
->non_stop_aware
)
2566 error (_("Non-stop mode requested, but remote does not support non-stop"));
2568 putpkt ("QNonStop:1");
2569 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2571 if (strcmp (rs
->buf
, "OK") != 0)
2572 error ("Remote refused setting non-stop mode with: %s", rs
->buf
);
2574 /* Find about threads and processes the stub is already
2575 controlling. We default to adding them in the running state.
2576 The '?' query below will then tell us about which threads are
2579 /* If we're not using the multi-process extensions, there's no
2580 way to know the pid of the reported threads; use the magic
2582 if (!remote_multi_process_p (rs
))
2583 inferior_ptid
= magic_null_ptid
;
2585 remote_threads_info ();
2587 else if (rs
->non_stop_aware
)
2589 /* Don't assume that the stub can operate in all-stop mode.
2590 Request it explicitely. */
2591 putpkt ("QNonStop:0");
2592 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2594 if (strcmp (rs
->buf
, "OK") != 0)
2595 error ("Remote refused setting all-stop mode with: %s", rs
->buf
);
2598 /* Check whether the target is running now. */
2600 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2604 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
2606 if (args
->extended_p
)
2608 /* We're connected, but not running. Drop out before we
2609 call start_remote. */
2610 target_mark_exited (args
->target
);
2614 error (_("The target is not running (try extended-remote?)"));
2618 if (args
->extended_p
)
2619 target_mark_running (args
->target
);
2621 /* Save the reply for later. */
2622 wait_status
= alloca (strlen (rs
->buf
) + 1);
2623 strcpy (wait_status
, rs
->buf
);
2626 /* Let the stub know that we want it to return the thread. */
2627 set_continue_thread (minus_one_ptid
);
2629 /* Without this, some commands which require an active target
2630 (such as kill) won't work. This variable serves (at least)
2631 double duty as both the pid of the target process (if it has
2632 such), and as a flag indicating that a target is active.
2633 These functions should be split out into seperate variables,
2634 especially since GDB will someday have a notion of debugging
2635 several processes. */
2636 inferior_ptid
= magic_null_ptid
;
2638 /* Now, if we have thread information, update inferior_ptid. */
2639 inferior_ptid
= remote_current_thread (inferior_ptid
);
2641 add_inferior (ptid_get_pid (inferior_ptid
));
2643 /* Always add the main thread. */
2644 add_thread_silent (inferior_ptid
);
2646 get_offsets (); /* Get text, data & bss offsets. */
2648 /* If we could not find a description using qXfer, and we know
2649 how to do it some other way, try again. This is not
2650 supported for non-stop; it could be, but it is tricky if
2651 there are no stopped threads when we connect. */
2652 if (remote_read_description_p (args
->target
)
2653 && gdbarch_target_desc (target_gdbarch
) == NULL
)
2655 target_clear_description ();
2656 target_find_description ();
2659 /* Use the previously fetched status. */
2660 gdb_assert (wait_status
!= NULL
);
2661 strcpy (rs
->buf
, wait_status
);
2662 rs
->cached_wait_status
= 1;
2665 start_remote (args
->from_tty
); /* Initialize gdb process mechanisms. */
2669 /* In non-stop, we will either get an "OK", meaning that there
2670 are no stopped threads at this time; or, a regular stop
2671 reply. In the latter case, there may be more than one thread
2672 stopped --- we pull them all out using the vStopped
2674 if (strcmp (rs
->buf
, "OK") != 0)
2676 struct stop_reply
*stop_reply
;
2677 struct cleanup
*old_chain
;
2679 stop_reply
= stop_reply_xmalloc ();
2680 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
2682 remote_parse_stop_reply (rs
->buf
, stop_reply
);
2683 discard_cleanups (old_chain
);
2685 /* get_pending_stop_replies acks this one, and gets the rest
2687 pending_stop_reply
= stop_reply
;
2688 remote_get_pending_stop_replies ();
2690 /* Make sure that threads that were stopped remain
2692 iterate_over_threads (set_stop_requested_callback
, NULL
);
2695 if (target_can_async_p ())
2696 target_async (inferior_event_handler
, 0);
2698 if (thread_count () == 0)
2700 if (args
->extended_p
)
2702 /* We're connected, but not running. Drop out before we
2703 call start_remote. */
2704 target_mark_exited (args
->target
);
2708 error (_("The target is not running (try extended-remote?)"));
2711 if (args
->extended_p
)
2712 target_mark_running (args
->target
);
2714 /* Let the stub know that we want it to return the thread. */
2716 /* Force the stub to choose a thread. */
2717 set_general_thread (null_ptid
);
2720 inferior_ptid
= remote_current_thread (minus_one_ptid
);
2721 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
2722 error (_("remote didn't report the current thread in non-stop mode"));
2724 get_offsets (); /* Get text, data & bss offsets. */
2726 /* In non-stop mode, any cached wait status will be stored in
2727 the stop reply queue. */
2728 gdb_assert (wait_status
== NULL
);
2731 /* If we connected to a live target, do some additional setup. */
2732 if (target_has_execution
)
2734 if (exec_bfd
) /* No use without an exec file. */
2735 remote_check_symbols (symfile_objfile
);
2738 /* If code is shared between processes, then breakpoints are global
2739 too; Insert them now. */
2740 if (gdbarch_has_global_solist (target_gdbarch
)
2741 && breakpoints_always_inserted_mode ())
2742 insert_breakpoints ();
2745 /* Open a connection to a remote debugger.
2746 NAME is the filename used for communication. */
2749 remote_open (char *name
, int from_tty
)
2751 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
2754 /* Open a connection to a remote debugger using the extended
2755 remote gdb protocol. NAME is the filename used for communication. */
2758 extended_remote_open (char *name
, int from_tty
)
2760 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
2763 /* Generic code for opening a connection to a remote target. */
2766 init_all_packet_configs (void)
2769 for (i
= 0; i
< PACKET_MAX
; i
++)
2770 update_packet_config (&remote_protocol_packets
[i
]);
2773 /* Symbol look-up. */
2776 remote_check_symbols (struct objfile
*objfile
)
2778 struct remote_state
*rs
= get_remote_state ();
2779 char *msg
, *reply
, *tmp
;
2780 struct minimal_symbol
*sym
;
2783 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
2786 /* Make sure the remote is pointing at the right process. */
2787 set_general_process ();
2789 /* Allocate a message buffer. We can't reuse the input buffer in RS,
2790 because we need both at the same time. */
2791 msg
= alloca (get_remote_packet_size ());
2793 /* Invite target to request symbol lookups. */
2795 putpkt ("qSymbol::");
2796 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2797 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
2800 while (strncmp (reply
, "qSymbol:", 8) == 0)
2803 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
2805 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
2807 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
2810 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
2812 /* If this is a function address, return the start of code
2813 instead of any data function descriptor. */
2814 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
2818 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
2819 paddr_nz (sym_addr
), &reply
[8]);
2823 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2828 static struct serial
*
2829 remote_serial_open (char *name
)
2831 static int udp_warning
= 0;
2833 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2834 of in ser-tcp.c, because it is the remote protocol assuming that the
2835 serial connection is reliable and not the serial connection promising
2837 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
2840 The remote protocol may be unreliable over UDP.\n\
2841 Some events may be lost, rendering further debugging impossible."));
2845 return serial_open (name
);
2848 /* This type describes each known response to the qSupported
2850 struct protocol_feature
2852 /* The name of this protocol feature. */
2855 /* The default for this protocol feature. */
2856 enum packet_support default_support
;
2858 /* The function to call when this feature is reported, or after
2859 qSupported processing if the feature is not supported.
2860 The first argument points to this structure. The second
2861 argument indicates whether the packet requested support be
2862 enabled, disabled, or probed (or the default, if this function
2863 is being called at the end of processing and this feature was
2864 not reported). The third argument may be NULL; if not NULL, it
2865 is a NUL-terminated string taken from the packet following
2866 this feature's name and an equals sign. */
2867 void (*func
) (const struct protocol_feature
*, enum packet_support
,
2870 /* The corresponding packet for this feature. Only used if
2871 FUNC is remote_supported_packet. */
2876 remote_supported_packet (const struct protocol_feature
*feature
,
2877 enum packet_support support
,
2878 const char *argument
)
2882 warning (_("Remote qSupported response supplied an unexpected value for"
2883 " \"%s\"."), feature
->name
);
2887 if (remote_protocol_packets
[feature
->packet
].support
2888 == PACKET_SUPPORT_UNKNOWN
)
2889 remote_protocol_packets
[feature
->packet
].support
= support
;
2893 remote_packet_size (const struct protocol_feature
*feature
,
2894 enum packet_support support
, const char *value
)
2896 struct remote_state
*rs
= get_remote_state ();
2901 if (support
!= PACKET_ENABLE
)
2904 if (value
== NULL
|| *value
== '\0')
2906 warning (_("Remote target reported \"%s\" without a size."),
2912 packet_size
= strtol (value
, &value_end
, 16);
2913 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
2915 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
2916 feature
->name
, value
);
2920 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
2922 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
2923 packet_size
, MAX_REMOTE_PACKET_SIZE
);
2924 packet_size
= MAX_REMOTE_PACKET_SIZE
;
2927 /* Record the new maximum packet size. */
2928 rs
->explicit_packet_size
= packet_size
;
2932 remote_multi_process_feature (const struct protocol_feature
*feature
,
2933 enum packet_support support
, const char *value
)
2935 struct remote_state
*rs
= get_remote_state ();
2936 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
2940 remote_non_stop_feature (const struct protocol_feature
*feature
,
2941 enum packet_support support
, const char *value
)
2943 struct remote_state
*rs
= get_remote_state ();
2944 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
2947 static struct protocol_feature remote_protocol_features
[] = {
2948 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
2949 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
2950 PACKET_qXfer_auxv
},
2951 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
2952 PACKET_qXfer_features
},
2953 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
2954 PACKET_qXfer_libraries
},
2955 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
2956 PACKET_qXfer_memory_map
},
2957 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
2958 PACKET_qXfer_spu_read
},
2959 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
2960 PACKET_qXfer_spu_write
},
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 },
2970 remote_query_supported (void)
2972 struct remote_state
*rs
= get_remote_state ();
2975 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
2977 /* The packet support flags are handled differently for this packet
2978 than for most others. We treat an error, a disabled packet, and
2979 an empty response identically: any features which must be reported
2980 to be used will be automatically disabled. An empty buffer
2981 accomplishes this, since that is also the representation for a list
2982 containing no features. */
2985 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
2988 putpkt ("qSupported:multiprocess+");
2990 putpkt ("qSupported");
2992 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2994 /* If an error occured, warn, but do not return - just reset the
2995 buffer to empty and go on to disable features. */
2996 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
2999 warning (_("Remote failure reply: %s"), rs
->buf
);
3004 memset (seen
, 0, sizeof (seen
));
3009 enum packet_support is_supported
;
3010 char *p
, *end
, *name_end
, *value
;
3012 /* First separate out this item from the rest of the packet. If
3013 there's another item after this, we overwrite the separator
3014 (terminated strings are much easier to work with). */
3016 end
= strchr (p
, ';');
3019 end
= p
+ strlen (p
);
3029 warning (_("empty item in \"qSupported\" response"));
3034 name_end
= strchr (p
, '=');
3037 /* This is a name=value entry. */
3038 is_supported
= PACKET_ENABLE
;
3039 value
= name_end
+ 1;
3048 is_supported
= PACKET_ENABLE
;
3052 is_supported
= PACKET_DISABLE
;
3056 is_supported
= PACKET_SUPPORT_UNKNOWN
;
3060 warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p
);
3066 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3067 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
3069 const struct protocol_feature
*feature
;
3072 feature
= &remote_protocol_features
[i
];
3073 feature
->func (feature
, is_supported
, value
);
3078 /* If we increased the packet size, make sure to increase the global
3079 buffer size also. We delay this until after parsing the entire
3080 qSupported packet, because this is the same buffer we were
3082 if (rs
->buf_size
< rs
->explicit_packet_size
)
3084 rs
->buf_size
= rs
->explicit_packet_size
;
3085 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
3088 /* Handle the defaults for unmentioned features. */
3089 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3092 const struct protocol_feature
*feature
;
3094 feature
= &remote_protocol_features
[i
];
3095 feature
->func (feature
, feature
->default_support
, NULL
);
3101 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
, int extended_p
)
3103 struct remote_state
*rs
= get_remote_state ();
3106 error (_("To open a remote debug connection, you need to specify what\n"
3107 "serial device is attached to the remote system\n"
3108 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
3110 /* See FIXME above. */
3111 if (!target_async_permitted
)
3112 wait_forever_enabled_p
= 1;
3114 /* If we're connected to a running target, target_preopen will kill it.
3115 But if we're connected to a target system with no running process,
3116 then we will still be connected when it returns. Ask this question
3117 first, before target_preopen has a chance to kill anything. */
3118 if (remote_desc
!= NULL
&& !target_has_execution
)
3121 || query (_("Already connected to a remote target. Disconnect? ")))
3124 error (_("Still connected."));
3127 target_preopen (from_tty
);
3129 unpush_target (target
);
3131 /* This time without a query. If we were connected to an
3132 extended-remote target and target_preopen killed the running
3133 process, we may still be connected. If we are starting "target
3134 remote" now, the extended-remote target will not have been
3135 removed by unpush_target. */
3136 if (remote_desc
!= NULL
&& !target_has_execution
)
3139 /* Make sure we send the passed signals list the next time we resume. */
3140 xfree (last_pass_packet
);
3141 last_pass_packet
= NULL
;
3143 remote_fileio_reset ();
3144 reopen_exec_file ();
3147 remote_desc
= remote_serial_open (name
);
3149 perror_with_name (name
);
3151 if (baud_rate
!= -1)
3153 if (serial_setbaudrate (remote_desc
, baud_rate
))
3155 /* The requested speed could not be set. Error out to
3156 top level after closing remote_desc. Take care to
3157 set remote_desc to NULL to avoid closing remote_desc
3159 serial_close (remote_desc
);
3161 perror_with_name (name
);
3165 serial_raw (remote_desc
);
3167 /* If there is something sitting in the buffer we might take it as a
3168 response to a command, which would be bad. */
3169 serial_flush_input (remote_desc
);
3173 puts_filtered ("Remote debugging using ");
3174 puts_filtered (name
);
3175 puts_filtered ("\n");
3177 push_target (target
); /* Switch to using remote target now. */
3179 /* Assume that the target is not running, until we learn otherwise. */
3181 target_mark_exited (target
);
3183 /* Register extra event sources in the event loop. */
3184 remote_async_inferior_event_token
3185 = create_async_event_handler (remote_async_inferior_event_handler
,
3187 remote_async_get_pending_events_token
3188 = create_async_event_handler (remote_async_get_pending_events_handler
,
3191 /* Reset the target state; these things will be queried either by
3192 remote_query_supported or as they are needed. */
3193 init_all_packet_configs ();
3194 rs
->cached_wait_status
= 0;
3195 rs
->explicit_packet_size
= 0;
3197 rs
->multi_process_aware
= 0;
3198 rs
->extended
= extended_p
;
3199 rs
->non_stop_aware
= 0;
3200 rs
->waiting_for_stop_reply
= 0;
3202 general_thread
= not_sent_ptid
;
3203 continue_thread
= not_sent_ptid
;
3205 /* Probe for ability to use "ThreadInfo" query, as required. */
3206 use_threadinfo_query
= 1;
3207 use_threadextra_query
= 1;
3209 if (target_async_permitted
)
3211 /* With this target we start out by owning the terminal. */
3212 remote_async_terminal_ours_p
= 1;
3214 /* FIXME: cagney/1999-09-23: During the initial connection it is
3215 assumed that the target is already ready and able to respond to
3216 requests. Unfortunately remote_start_remote() eventually calls
3217 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
3218 around this. Eventually a mechanism that allows
3219 wait_for_inferior() to expect/get timeouts will be
3221 wait_forever_enabled_p
= 0;
3224 /* First delete any symbols previously loaded from shared libraries. */
3225 no_shared_libraries (NULL
, 0);
3228 init_thread_list ();
3230 /* Start the remote connection. If error() or QUIT, discard this
3231 target (we'd otherwise be in an inconsistent state) and then
3232 propogate the error on up the exception chain. This ensures that
3233 the caller doesn't stumble along blindly assuming that the
3234 function succeeded. The CLI doesn't have this problem but other
3235 UI's, such as MI do.
3237 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
3238 this function should return an error indication letting the
3239 caller restore the previous state. Unfortunately the command
3240 ``target remote'' is directly wired to this function making that
3241 impossible. On a positive note, the CLI side of this problem has
3242 been fixed - the function set_cmd_context() makes it possible for
3243 all the ``target ....'' commands to share a common callback
3244 function. See cli-dump.c. */
3246 struct gdb_exception ex
;
3247 struct start_remote_args args
;
3249 args
.from_tty
= from_tty
;
3250 args
.target
= target
;
3251 args
.extended_p
= extended_p
;
3253 ex
= catch_exception (uiout
, remote_start_remote
, &args
, RETURN_MASK_ALL
);
3256 /* Pop the partially set up target - unless something else did
3257 already before throwing the exception. */
3258 if (remote_desc
!= NULL
)
3260 if (target_async_permitted
)
3261 wait_forever_enabled_p
= 1;
3262 throw_exception (ex
);
3266 if (target_async_permitted
)
3267 wait_forever_enabled_p
= 1;
3270 /* This takes a program previously attached to and detaches it. After
3271 this is done, GDB can be used to debug some other program. We
3272 better not have left any breakpoints in the target program or it'll
3273 die when it hits one. */
3276 remote_detach_1 (char *args
, int from_tty
, int extended
)
3278 int pid
= ptid_get_pid (inferior_ptid
);
3279 struct remote_state
*rs
= get_remote_state ();
3282 error (_("Argument given to \"detach\" when remotely debugging."));
3284 if (!target_has_execution
)
3285 error (_("No process to detach from."));
3287 /* Tell the remote target to detach. */
3288 if (remote_multi_process_p (rs
))
3289 sprintf (rs
->buf
, "D;%x", pid
);
3291 strcpy (rs
->buf
, "D");
3294 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3296 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
3298 else if (rs
->buf
[0] == '\0')
3299 error (_("Remote doesn't know how to detach"));
3301 error (_("Can't detach process."));
3305 if (remote_multi_process_p (rs
))
3306 printf_filtered (_("Detached from remote %s.\n"),
3307 target_pid_to_str (pid_to_ptid (pid
)));
3311 puts_filtered (_("Detached from remote process.\n"));
3313 puts_filtered (_("Ending remote debugging.\n"));
3317 discard_pending_stop_replies (pid
);
3318 detach_inferior (pid
);
3319 target_mourn_inferior ();
3323 remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
3325 remote_detach_1 (args
, from_tty
, 0);
3329 extended_remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
3331 remote_detach_1 (args
, from_tty
, 1);
3334 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
3337 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
3340 error (_("Argument given to \"disconnect\" when remotely debugging."));
3342 /* Make sure we unpush even the extended remote targets; mourn
3343 won't do it. So call remote_mourn_1 directly instead of
3344 target_mourn_inferior. */
3345 remote_mourn_1 (target
);
3348 puts_filtered ("Ending remote debugging.\n");
3351 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
3352 be chatty about it. */
3355 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
3357 struct remote_state
*rs
= get_remote_state ();
3360 char *wait_status
= NULL
;
3361 struct inferior
*inf
;
3364 error_no_arg (_("process-id to attach"));
3367 pid
= strtol (args
, &dummy
, 0);
3368 /* Some targets don't set errno on errors, grrr! */
3369 if (pid
== 0 && args
== dummy
)
3370 error (_("Illegal process-id: %s."), args
);
3372 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
3373 error (_("This target does not support attaching to a process"));
3375 sprintf (rs
->buf
, "vAttach;%x", pid
);
3377 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3379 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
3382 printf_unfiltered (_("Attached to %s\n"),
3383 target_pid_to_str (pid_to_ptid (pid
)));
3387 /* Save the reply for later. */
3388 wait_status
= alloca (strlen (rs
->buf
) + 1);
3389 strcpy (wait_status
, rs
->buf
);
3391 else if (strcmp (rs
->buf
, "OK") != 0)
3392 error (_("Attaching to %s failed with: %s"),
3393 target_pid_to_str (pid_to_ptid (pid
)),
3396 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
3397 error (_("This target does not support attaching to a process"));
3399 error (_("Attaching to %s failed"),
3400 target_pid_to_str (pid_to_ptid (pid
)));
3402 target_mark_running (target
);
3403 inferior_ptid
= pid_to_ptid (pid
);
3405 /* Now, if we have thread information, update inferior_ptid. */
3406 inferior_ptid
= remote_current_thread (inferior_ptid
);
3408 inf
= add_inferior (pid
);
3409 inf
->attach_flag
= 1;
3412 /* Get list of threads. */
3413 remote_threads_info ();
3415 /* Add the main thread to the thread list. */
3416 add_thread_silent (inferior_ptid
);
3418 /* Next, if the target can specify a description, read it. We do
3419 this before anything involving memory or registers. */
3420 target_find_description ();
3424 /* Use the previously fetched status. */
3425 gdb_assert (wait_status
!= NULL
);
3427 if (target_can_async_p ())
3429 struct stop_reply
*stop_reply
;
3430 struct cleanup
*old_chain
;
3432 stop_reply
= stop_reply_xmalloc ();
3433 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
3434 remote_parse_stop_reply (wait_status
, stop_reply
);
3435 discard_cleanups (old_chain
);
3436 push_stop_reply (stop_reply
);
3438 target_async (inferior_event_handler
, 0);
3442 gdb_assert (wait_status
!= NULL
);
3443 strcpy (rs
->buf
, wait_status
);
3444 rs
->cached_wait_status
= 1;
3448 gdb_assert (wait_status
== NULL
);
3452 extended_remote_attach (struct target_ops
*ops
, char *args
, int from_tty
)
3454 extended_remote_attach_1 (ops
, args
, from_tty
);
3457 /* Convert hex digit A to a number. */
3462 if (a
>= '0' && a
<= '9')
3464 else if (a
>= 'a' && a
<= 'f')
3465 return a
- 'a' + 10;
3466 else if (a
>= 'A' && a
<= 'F')
3467 return a
- 'A' + 10;
3469 error (_("Reply contains invalid hex digit %d"), a
);
3473 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
3477 for (i
= 0; i
< count
; i
++)
3479 if (hex
[0] == 0 || hex
[1] == 0)
3481 /* Hex string is short, or of uneven length.
3482 Return the count that has been converted so far. */
3485 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
3491 /* Convert number NIB to a hex digit. */
3499 return 'a' + nib
- 10;
3503 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
3506 /* May use a length, or a nul-terminated string as input. */
3508 count
= strlen ((char *) bin
);
3510 for (i
= 0; i
< count
; i
++)
3512 *hex
++ = tohex ((*bin
>> 4) & 0xf);
3513 *hex
++ = tohex (*bin
++ & 0xf);
3519 /* Check for the availability of vCont. This function should also check
3523 remote_vcont_probe (struct remote_state
*rs
)
3527 strcpy (rs
->buf
, "vCont?");
3529 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3532 /* Make sure that the features we assume are supported. */
3533 if (strncmp (buf
, "vCont", 5) == 0)
3536 int support_s
, support_S
, support_c
, support_C
;
3542 rs
->support_vCont_t
= 0;
3543 while (p
&& *p
== ';')
3546 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3548 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3550 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3552 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3554 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3555 rs
->support_vCont_t
= 1;
3557 p
= strchr (p
, ';');
3560 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
3561 BUF will make packet_ok disable the packet. */
3562 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
3566 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
3569 /* Resume the remote inferior by using a "vCont" packet. The thread
3570 to be resumed is PTID; STEP and SIGGNAL indicate whether the
3571 resumed thread should be single-stepped and/or signalled. If PTID
3572 equals minus_one_ptid, then all threads are resumed; the thread to
3573 be stepped and/or signalled is given in the global INFERIOR_PTID.
3574 This function returns non-zero iff it resumes the inferior.
3576 This function issues a strict subset of all possible vCont commands at the
3580 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
3582 struct remote_state
*rs
= get_remote_state ();
3586 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
3587 remote_vcont_probe (rs
);
3589 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
3593 endp
= rs
->buf
+ get_remote_packet_size ();
3595 /* If we could generate a wider range of packets, we'd have to worry
3596 about overflowing BUF. Should there be a generic
3597 "multi-part-packet" packet? */
3599 if (ptid_equal (ptid
, magic_null_ptid
))
3601 /* MAGIC_NULL_PTID means that we don't have any active threads,
3602 so we don't have any TID numbers the inferior will
3603 understand. Make sure to only send forms that do not specify
3605 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3606 xsnprintf (p
, endp
- p
, "vCont;S%02x", siggnal
);
3608 xsnprintf (p
, endp
- p
, "vCont;s");
3609 else if (siggnal
!= TARGET_SIGNAL_0
)
3610 xsnprintf (p
, endp
- p
, "vCont;C%02x", siggnal
);
3612 xsnprintf (p
, endp
- p
, "vCont;c");
3614 else if (ptid_equal (ptid
, minus_one_ptid
))
3616 /* Resume all threads, with preference for INFERIOR_PTID. */
3617 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3619 /* Step inferior_ptid with signal. */
3620 p
+= xsnprintf (p
, endp
- p
, "vCont;S%02x:", siggnal
);
3621 p
= write_ptid (p
, endp
, inferior_ptid
);
3622 /* And continue others. */
3623 p
+= xsnprintf (p
, endp
- p
, ";c");
3627 /* Step inferior_ptid. */
3628 p
+= xsnprintf (p
, endp
- p
, "vCont;s:");
3629 p
= write_ptid (p
, endp
, inferior_ptid
);
3630 /* And continue others. */
3631 p
+= xsnprintf (p
, endp
- p
, ";c");
3633 else if (siggnal
!= TARGET_SIGNAL_0
)
3635 /* Continue inferior_ptid with signal. */
3636 p
+= xsnprintf (p
, endp
- p
, "vCont;C%02x:", siggnal
);
3637 p
= write_ptid (p
, endp
, inferior_ptid
);
3638 /* And continue others. */
3639 p
+= xsnprintf (p
, endp
- p
, ";c");
3642 xsnprintf (p
, endp
- p
, "vCont;c");
3646 /* Scheduler locking; resume only PTID. */
3647 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3649 /* Step ptid with signal. */
3650 p
+= xsnprintf (p
, endp
- p
, "vCont;S%02x:", siggnal
);
3651 p
= write_ptid (p
, endp
, ptid
);
3656 p
+= xsnprintf (p
, endp
- p
, "vCont;s:");
3657 p
= write_ptid (p
, endp
, ptid
);
3659 else if (siggnal
!= TARGET_SIGNAL_0
)
3661 /* Continue ptid with signal. */
3662 p
+= xsnprintf (p
, endp
- p
, "vCont;C%02x:", siggnal
);
3663 p
= write_ptid (p
, endp
, ptid
);
3667 /* Continue ptid. */
3668 p
+= xsnprintf (p
, endp
- p
, "vCont;c:");
3669 p
= write_ptid (p
, endp
, ptid
);
3673 gdb_assert (strlen (rs
->buf
) < get_remote_packet_size ());
3678 /* In non-stop, the stub replies to vCont with "OK". The stop
3679 reply will be reported asynchronously by means of a `%Stop'
3681 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3682 if (strcmp (rs
->buf
, "OK") != 0)
3683 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
);
3689 /* Tell the remote machine to resume. */
3691 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
3693 static int last_sent_step
;
3696 remote_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
3698 struct remote_state
*rs
= get_remote_state ();
3701 last_sent_signal
= siggnal
;
3702 last_sent_step
= step
;
3704 /* Update the inferior on signals to silently pass, if they've changed. */
3705 remote_pass_signals ();
3707 /* The vCont packet doesn't need to specify threads via Hc. */
3708 if (remote_vcont_resume (ptid
, step
, siggnal
))
3711 /* All other supported resume packets do use Hc, so set the continue
3713 if (ptid_equal (ptid
, minus_one_ptid
))
3714 set_continue_thread (any_thread_ptid
);
3716 set_continue_thread (ptid
);
3719 if (execution_direction
== EXEC_REVERSE
)
3721 /* We don't pass signals to the target in reverse exec mode. */
3722 if (info_verbose
&& siggnal
!= TARGET_SIGNAL_0
)
3723 warning (" - Can't pass signal %d to target in reverse: ignored.\n",
3725 strcpy (buf
, step
? "bs" : "bc");
3727 else if (siggnal
!= TARGET_SIGNAL_0
)
3729 buf
[0] = step
? 'S' : 'C';
3730 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
3731 buf
[2] = tohex (((int) siggnal
) & 0xf);
3735 strcpy (buf
, step
? "s" : "c");
3740 /* We are about to start executing the inferior, let's register it
3741 with the event loop. NOTE: this is the one place where all the
3742 execution commands end up. We could alternatively do this in each
3743 of the execution commands in infcmd.c. */
3744 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
3745 into infcmd.c in order to allow inferior function calls to work
3746 NOT asynchronously. */
3747 if (target_can_async_p ())
3748 target_async (inferior_event_handler
, 0);
3750 /* We've just told the target to resume. The remote server will
3751 wait for the inferior to stop, and then send a stop reply. In
3752 the mean time, we can't start another command/query ourselves
3753 because the stub wouldn't be ready to process it. This applies
3754 only to the base all-stop protocol, however. In non-stop (which
3755 only supports vCont), the stub replies with an "OK", and is
3756 immediate able to process further serial input. */
3758 rs
->waiting_for_stop_reply
= 1;
3762 /* Set up the signal handler for SIGINT, while the target is
3763 executing, ovewriting the 'regular' SIGINT signal handler. */
3765 initialize_sigint_signal_handler (void)
3767 signal (SIGINT
, handle_remote_sigint
);
3770 /* Signal handler for SIGINT, while the target is executing. */
3772 handle_remote_sigint (int sig
)
3774 signal (sig
, handle_remote_sigint_twice
);
3775 mark_async_signal_handler_wrapper (sigint_remote_token
);
3778 /* Signal handler for SIGINT, installed after SIGINT has already been
3779 sent once. It will take effect the second time that the user sends
3782 handle_remote_sigint_twice (int sig
)
3784 signal (sig
, handle_remote_sigint
);
3785 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
3788 /* Perform the real interruption of the target execution, in response
3791 async_remote_interrupt (gdb_client_data arg
)
3794 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
3796 target_stop (inferior_ptid
);
3799 /* Perform interrupt, if the first attempt did not succeed. Just give
3800 up on the target alltogether. */
3802 async_remote_interrupt_twice (gdb_client_data arg
)
3805 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
3810 /* Reinstall the usual SIGINT handlers, after the target has
3813 cleanup_sigint_signal_handler (void *dummy
)
3815 signal (SIGINT
, handle_sigint
);
3818 /* Send ^C to target to halt it. Target will respond, and send us a
3820 static void (*ofunc
) (int);
3822 /* The command line interface's stop routine. This function is installed
3823 as a signal handler for SIGINT. The first time a user requests a
3824 stop, we call remote_stop to send a break or ^C. If there is no
3825 response from the target (it didn't stop when the user requested it),
3826 we ask the user if he'd like to detach from the target. */
3828 remote_interrupt (int signo
)
3830 /* If this doesn't work, try more severe steps. */
3831 signal (signo
, remote_interrupt_twice
);
3833 gdb_call_async_signal_handler (sigint_remote_token
, 1);
3836 /* The user typed ^C twice. */
3839 remote_interrupt_twice (int signo
)
3841 signal (signo
, ofunc
);
3842 gdb_call_async_signal_handler (sigint_remote_twice_token
, 1);
3843 signal (signo
, remote_interrupt
);
3846 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
3847 thread, all threads of a remote process, or all threads of all
3851 remote_stop_ns (ptid_t ptid
)
3853 struct remote_state
*rs
= get_remote_state ();
3855 char *endp
= rs
->buf
+ get_remote_packet_size ();
3856 struct stop_reply
*reply
, *next
;
3858 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
3859 remote_vcont_probe (rs
);
3861 if (!rs
->support_vCont_t
)
3862 error (_("Remote server does not support stopping threads"));
3864 if (ptid_equal (ptid
, minus_one_ptid
))
3865 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
3870 /* Step inferior_ptid. */
3871 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
3873 if (ptid_is_pid (ptid
))
3874 /* All (-1) threads of process. */
3875 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
3878 /* Small optimization: if we already have a stop reply for
3879 this thread, no use in telling the stub we want this
3881 if (peek_stop_reply (ptid
))
3887 p
= write_ptid (p
, endp
, nptid
);
3890 /* In non-stop, we get an immediate OK reply. The stop reply will
3891 come in asynchronously by notification. */
3893 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3894 if (strcmp (rs
->buf
, "OK") != 0)
3895 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
), rs
->buf
);
3898 /* All-stop version of target_stop. Sends a break or a ^C to stop the
3899 remote target. It is undefined which thread of which process
3900 reports the stop. */
3903 remote_stop_as (ptid_t ptid
)
3905 struct remote_state
*rs
= get_remote_state ();
3907 /* If the inferior is stopped already, but the core didn't know
3908 about it yet, just ignore the request. The cached wait status
3909 will be collected in remote_wait. */
3910 if (rs
->cached_wait_status
)
3913 /* Send a break or a ^C, depending on user preference. */
3916 serial_send_break (remote_desc
);
3918 serial_write (remote_desc
, "\003", 1);
3921 /* This is the generic stop called via the target vector. When a target
3922 interrupt is requested, either by the command line or the GUI, we
3923 will eventually end up here. */
3926 remote_stop (ptid_t ptid
)
3929 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
3932 remote_stop_ns (ptid
);
3934 remote_stop_as (ptid
);
3937 /* Ask the user what to do when an interrupt is received. */
3940 interrupt_query (void)
3942 target_terminal_ours ();
3944 if (target_can_async_p ())
3946 signal (SIGINT
, handle_sigint
);
3947 deprecated_throw_reason (RETURN_QUIT
);
3951 if (query ("Interrupted while waiting for the program.\n\
3952 Give up (and stop debugging it)? "))
3955 deprecated_throw_reason (RETURN_QUIT
);
3959 target_terminal_inferior ();
3962 /* Enable/disable target terminal ownership. Most targets can use
3963 terminal groups to control terminal ownership. Remote targets are
3964 different in that explicit transfer of ownership to/from GDB/target
3968 remote_terminal_inferior (void)
3970 if (!target_async_permitted
)
3971 /* Nothing to do. */
3974 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
3975 sync_execution here. This function should only be called when
3976 GDB is resuming the inferior in the forground. A background
3977 resume (``run&'') should leave GDB in control of the terminal and
3978 consequently should not call this code. */
3979 if (!sync_execution
)
3981 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
3982 calls target_terminal_*() idenpotent. The event-loop GDB talking
3983 to an asynchronous target with a synchronous command calls this
3984 function from both event-top.c and infrun.c/infcmd.c. Once GDB
3985 stops trying to transfer the terminal to the target when it
3986 shouldn't this guard can go away. */
3987 if (!remote_async_terminal_ours_p
)
3989 delete_file_handler (input_fd
);
3990 remote_async_terminal_ours_p
= 0;
3991 initialize_sigint_signal_handler ();
3992 /* NOTE: At this point we could also register our selves as the
3993 recipient of all input. Any characters typed could then be
3994 passed on down to the target. */
3998 remote_terminal_ours (void)
4000 if (!target_async_permitted
)
4001 /* Nothing to do. */
4004 /* See FIXME in remote_terminal_inferior. */
4005 if (!sync_execution
)
4007 /* See FIXME in remote_terminal_inferior. */
4008 if (remote_async_terminal_ours_p
)
4010 cleanup_sigint_signal_handler (NULL
);
4011 add_file_handler (input_fd
, stdin_event_handler
, 0);
4012 remote_async_terminal_ours_p
= 1;
4016 remote_console_output (char *msg
)
4020 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
4023 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
4026 fputs_unfiltered (tb
, gdb_stdtarg
);
4028 gdb_flush (gdb_stdtarg
);
4031 typedef struct cached_reg
4034 gdb_byte data
[MAX_REGISTER_SIZE
];
4037 DEF_VEC_O(cached_reg_t
);
4041 struct stop_reply
*next
;
4045 struct target_waitstatus ws
;
4047 VEC(cached_reg_t
) *regcache
;
4049 int stopped_by_watchpoint_p
;
4050 CORE_ADDR watch_data_address
;
4056 /* The list of already fetched and acknowledged stop events. */
4057 static struct stop_reply
*stop_reply_queue
;
4059 static struct stop_reply
*
4060 stop_reply_xmalloc (void)
4062 struct stop_reply
*r
= XMALLOC (struct stop_reply
);
4068 stop_reply_xfree (struct stop_reply
*r
)
4072 VEC_free (cached_reg_t
, r
->regcache
);
4077 /* Discard all pending stop replies of inferior PID. If PID is -1,
4078 discard everything. */
4081 discard_pending_stop_replies (int pid
)
4083 struct stop_reply
*prev
= NULL
, *reply
, *next
;
4085 /* Discard the in-flight notification. */
4086 if (pending_stop_reply
!= NULL
4088 || ptid_get_pid (pending_stop_reply
->ptid
) == pid
))
4090 stop_reply_xfree (pending_stop_reply
);
4091 pending_stop_reply
= NULL
;
4094 /* Discard the stop replies we have already pulled with
4096 for (reply
= stop_reply_queue
; reply
; reply
= next
)
4100 || ptid_get_pid (reply
->ptid
) == pid
)
4102 if (reply
== stop_reply_queue
)
4103 stop_reply_queue
= reply
->next
;
4105 prev
->next
= reply
->next
;
4107 stop_reply_xfree (reply
);
4114 /* Cleanup wrapper. */
4117 do_stop_reply_xfree (void *arg
)
4119 struct stop_reply
*r
= arg
;
4120 stop_reply_xfree (r
);
4123 /* Look for a queued stop reply belonging to PTID. If one is found,
4124 remove it from the queue, and return it. Returns NULL if none is
4125 found. If there are still queued events left to process, tell the
4126 event loop to get back to target_wait soon. */
4128 static struct stop_reply
*
4129 queued_stop_reply (ptid_t ptid
)
4131 struct stop_reply
*it
, *prev
;
4132 struct stop_reply head
;
4134 head
.next
= stop_reply_queue
;
4139 if (!ptid_equal (ptid
, minus_one_ptid
))
4140 for (; it
; prev
= it
, it
= it
->next
)
4141 if (ptid_equal (ptid
, it
->ptid
))
4146 prev
->next
= it
->next
;
4150 stop_reply_queue
= head
.next
;
4152 if (stop_reply_queue
)
4153 /* There's still at least an event left. */
4154 mark_async_event_handler (remote_async_inferior_event_token
);
4159 /* Push a fully parsed stop reply in the stop reply queue. Since we
4160 know that we now have at least one queued event left to pass to the
4161 core side, tell the event loop to get back to target_wait soon. */
4164 push_stop_reply (struct stop_reply
*new_event
)
4166 struct stop_reply
*event
;
4168 if (stop_reply_queue
)
4170 for (event
= stop_reply_queue
;
4171 event
&& event
->next
;
4172 event
= event
->next
)
4175 event
->next
= new_event
;
4178 stop_reply_queue
= new_event
;
4180 mark_async_event_handler (remote_async_inferior_event_token
);
4183 /* Returns true if we have a stop reply for PTID. */
4186 peek_stop_reply (ptid_t ptid
)
4188 struct stop_reply
*it
;
4190 for (it
= stop_reply_queue
; it
; it
= it
->next
)
4191 if (ptid_equal (ptid
, it
->ptid
))
4193 if (it
->ws
.kind
== TARGET_WAITKIND_STOPPED
)
4200 /* Parse the stop reply in BUF. Either the function succeeds, and the
4201 result is stored in EVENT, or throws an error. */
4204 remote_parse_stop_reply (char *buf
, struct stop_reply
*event
)
4206 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4210 event
->ptid
= null_ptid
;
4211 event
->ws
.kind
= TARGET_WAITKIND_IGNORE
;
4212 event
->ws
.value
.integer
= 0;
4213 event
->solibs_changed
= 0;
4214 event
->replay_event
= 0;
4215 event
->stopped_by_watchpoint_p
= 0;
4216 event
->regcache
= NULL
;
4220 case 'T': /* Status with PC, SP, FP, ... */
4222 gdb_byte regs
[MAX_REGISTER_SIZE
];
4224 /* Expedited reply, containing Signal, {regno, reg} repeat. */
4225 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
4227 n... = register number
4228 r... = register contents
4231 p
= &buf
[3]; /* after Txx */
4239 /* If the packet contains a register number, save it in
4240 pnum and set p1 to point to the character following it.
4241 Otherwise p1 points to p. */
4243 /* If this packet is an awatch packet, don't parse the 'a'
4244 as a register number. */
4246 if (strncmp (p
, "awatch", strlen("awatch")) != 0)
4248 /* Read the ``P'' register number. */
4249 pnum
= strtol (p
, &p_temp
, 16);
4255 if (p1
== p
) /* No register number present here. */
4257 p1
= strchr (p
, ':');
4259 error (_("Malformed packet(a) (missing colon): %s\n\
4262 if (strncmp (p
, "thread", p1
- p
) == 0)
4263 event
->ptid
= read_ptid (++p1
, &p
);
4264 else if ((strncmp (p
, "watch", p1
- p
) == 0)
4265 || (strncmp (p
, "rwatch", p1
- p
) == 0)
4266 || (strncmp (p
, "awatch", p1
- p
) == 0))
4268 event
->stopped_by_watchpoint_p
= 1;
4269 p
= unpack_varlen_hex (++p1
, &addr
);
4270 event
->watch_data_address
= (CORE_ADDR
) addr
;
4272 else if (strncmp (p
, "library", p1
- p
) == 0)
4276 while (*p_temp
&& *p_temp
!= ';')
4279 event
->solibs_changed
= 1;
4282 else if (strncmp (p
, "replaylog", p1
- p
) == 0)
4284 /* NO_HISTORY event.
4285 p1 will indicate "begin" or "end", but
4286 it makes no difference for now, so ignore it. */
4287 event
->replay_event
= 1;
4288 p_temp
= strchr (p1
+ 1, ';');
4294 /* Silently skip unknown optional info. */
4295 p_temp
= strchr (p1
+ 1, ';');
4302 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
4303 cached_reg_t cached_reg
;
4305 cached_reg
.num
= reg
->regnum
;
4310 error (_("Malformed packet(b) (missing colon): %s\n\
4316 error (_("Remote sent bad register number %s: %s\n\
4318 phex_nz (pnum
, 0), p
, buf
);
4320 fieldsize
= hex2bin (p
, cached_reg
.data
,
4321 register_size (target_gdbarch
,
4324 if (fieldsize
< register_size (target_gdbarch
,
4326 warning (_("Remote reply is too short: %s"), buf
);
4328 VEC_safe_push (cached_reg_t
, event
->regcache
, &cached_reg
);
4332 error (_("Remote register badly formatted: %s\nhere: %s"),
4338 case 'S': /* Old style status, just signal only. */
4339 if (event
->solibs_changed
)
4340 event
->ws
.kind
= TARGET_WAITKIND_LOADED
;
4341 else if (event
->replay_event
)
4342 event
->ws
.kind
= TARGET_WAITKIND_NO_HISTORY
;
4345 event
->ws
.kind
= TARGET_WAITKIND_STOPPED
;
4346 event
->ws
.value
.sig
= (enum target_signal
)
4347 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
4350 case 'W': /* Target exited. */
4357 /* GDB used to accept only 2 hex chars here. Stubs should
4358 only send more if they detect GDB supports multi-process
4360 p
= unpack_varlen_hex (&buf
[1], &value
);
4364 /* The remote process exited. */
4365 event
->ws
.kind
= TARGET_WAITKIND_EXITED
;
4366 event
->ws
.value
.integer
= value
;
4370 /* The remote process exited with a signal. */
4371 event
->ws
.kind
= TARGET_WAITKIND_SIGNALLED
;
4372 event
->ws
.value
.sig
= (enum target_signal
) value
;
4375 /* If no process is specified, assume inferior_ptid. */
4376 pid
= ptid_get_pid (inferior_ptid
);
4385 else if (strncmp (p
,
4386 "process:", sizeof ("process:") - 1) == 0)
4389 p
+= sizeof ("process:") - 1;
4390 unpack_varlen_hex (p
, &upid
);
4394 error (_("unknown stop reply packet: %s"), buf
);
4397 error (_("unknown stop reply packet: %s"), buf
);
4398 event
->ptid
= pid_to_ptid (pid
);
4403 if (non_stop
&& ptid_equal (event
->ptid
, null_ptid
))
4404 error (_("No process or thread specified in stop reply: %s"), buf
);
4407 /* When the stub wants to tell GDB about a new stop reply, it sends a
4408 stop notification (%Stop). Those can come it at any time, hence,
4409 we have to make sure that any pending putpkt/getpkt sequence we're
4410 making is finished, before querying the stub for more events with
4411 vStopped. E.g., if we started a vStopped sequence immediatelly
4412 upon receiving the %Stop notification, something like this could
4420 1.6) <-- (registers reply to step #1.3)
4422 Obviously, the reply in step #1.6 would be unexpected to a vStopped
4425 To solve this, whenever we parse a %Stop notification sucessfully,
4426 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
4427 doing whatever we were doing:
4433 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
4434 2.5) <-- (registers reply to step #2.3)
4436 Eventualy after step #2.5, we return to the event loop, which
4437 notices there's an event on the
4438 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
4439 associated callback --- the function below. At this point, we're
4440 always safe to start a vStopped sequence. :
4443 2.7) <-- T05 thread:2
4449 remote_get_pending_stop_replies (void)
4451 struct remote_state
*rs
= get_remote_state ();
4454 if (pending_stop_reply
)
4457 putpkt ("vStopped");
4459 /* Now we can rely on it. */
4460 push_stop_reply (pending_stop_reply
);
4461 pending_stop_reply
= NULL
;
4465 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4466 if (strcmp (rs
->buf
, "OK") == 0)
4470 struct cleanup
*old_chain
;
4471 struct stop_reply
*stop_reply
= stop_reply_xmalloc ();
4473 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4474 remote_parse_stop_reply (rs
->buf
, stop_reply
);
4477 putpkt ("vStopped");
4479 if (stop_reply
->ws
.kind
!= TARGET_WAITKIND_IGNORE
)
4481 /* Now we can rely on it. */
4482 discard_cleanups (old_chain
);
4483 push_stop_reply (stop_reply
);
4486 /* We got an unknown stop reply. */
4487 do_cleanups (old_chain
);
4494 /* Called when it is decided that STOP_REPLY holds the info of the
4495 event that is to be returned to the core. This function always
4496 destroys STOP_REPLY. */
4499 process_stop_reply (struct stop_reply
*stop_reply
,
4500 struct target_waitstatus
*status
)
4504 *status
= stop_reply
->ws
;
4505 ptid
= stop_reply
->ptid
;
4507 /* If no thread/process was reported by the stub, assume the current
4509 if (ptid_equal (ptid
, null_ptid
))
4510 ptid
= inferior_ptid
;
4512 if (status
->kind
== TARGET_WAITKIND_EXITED
4513 || status
->kind
== TARGET_WAITKIND_SIGNALLED
)
4515 int pid
= ptid_get_pid (ptid
);
4516 delete_inferior (pid
);
4519 notice_new_inferiors (ptid
);
4521 /* Expedited registers. */
4522 if (stop_reply
->regcache
)
4528 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
4530 regcache_raw_supply (get_thread_regcache (ptid
),
4531 reg
->num
, reg
->data
);
4532 VEC_free (cached_reg_t
, stop_reply
->regcache
);
4535 remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
4536 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 (ptid_t ptid
, struct target_waitstatus
*status
)
4744 event_ptid
= remote_wait_ns (ptid
, status
);
4747 /* In synchronous mode, keep waiting until the target stops. In
4748 asynchronous mode, always return to the event loop. */
4752 event_ptid
= remote_wait_as (ptid
, status
);
4754 while (status
->kind
== TARGET_WAITKIND_IGNORE
4755 && !target_can_async_p ());
4758 if (target_can_async_p ())
4760 /* If there are are events left in the queue tell the event loop
4762 if (stop_reply_queue
)
4763 mark_async_event_handler (remote_async_inferior_event_token
);
4769 /* Fetch a single register using a 'p' packet. */
4772 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
4774 struct remote_state
*rs
= get_remote_state ();
4776 char regp
[MAX_REGISTER_SIZE
];
4779 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
4782 if (reg
->pnum
== -1)
4787 p
+= hexnumstr (p
, reg
->pnum
);
4789 remote_send (&rs
->buf
, &rs
->buf_size
);
4793 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
4797 case PACKET_UNKNOWN
:
4800 error (_("Could not fetch register \"%s\""),
4801 gdbarch_register_name (get_regcache_arch (regcache
), reg
->regnum
));
4804 /* If this register is unfetchable, tell the regcache. */
4807 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
4811 /* Otherwise, parse and supply the value. */
4817 error (_("fetch_register_using_p: early buf termination"));
4819 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
4822 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
4826 /* Fetch the registers included in the target's 'g' packet. */
4829 send_g_packet (void)
4831 struct remote_state
*rs
= get_remote_state ();
4836 sprintf (rs
->buf
, "g");
4837 remote_send (&rs
->buf
, &rs
->buf_size
);
4839 /* We can get out of synch in various cases. If the first character
4840 in the buffer is not a hex character, assume that has happened
4841 and try to fetch another packet to read. */
4842 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
4843 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
4844 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
4845 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
4848 fprintf_unfiltered (gdb_stdlog
,
4849 "Bad register packet; fetching a new packet\n");
4850 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4853 buf_len
= strlen (rs
->buf
);
4855 /* Sanity check the received packet. */
4856 if (buf_len
% 2 != 0)
4857 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
4863 process_g_packet (struct regcache
*regcache
)
4865 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
4866 struct remote_state
*rs
= get_remote_state ();
4867 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4872 buf_len
= strlen (rs
->buf
);
4874 /* Further sanity checks, with knowledge of the architecture. */
4875 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
4876 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
4878 /* Save the size of the packet sent to us by the target. It is used
4879 as a heuristic when determining the max size of packets that the
4880 target can safely receive. */
4881 if (rsa
->actual_register_packet_size
== 0)
4882 rsa
->actual_register_packet_size
= buf_len
;
4884 /* If this is smaller than we guessed the 'g' packet would be,
4885 update our records. A 'g' reply that doesn't include a register's
4886 value implies either that the register is not available, or that
4887 the 'p' packet must be used. */
4888 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
4890 rsa
->sizeof_g_packet
= buf_len
/ 2;
4892 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
4894 if (rsa
->regs
[i
].pnum
== -1)
4897 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
4898 rsa
->regs
[i
].in_g_packet
= 0;
4900 rsa
->regs
[i
].in_g_packet
= 1;
4904 regs
= alloca (rsa
->sizeof_g_packet
);
4906 /* Unimplemented registers read as all bits zero. */
4907 memset (regs
, 0, rsa
->sizeof_g_packet
);
4909 /* Reply describes registers byte by byte, each byte encoded as two
4910 hex characters. Suck them all up, then supply them to the
4911 register cacheing/storage mechanism. */
4914 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
4916 if (p
[0] == 0 || p
[1] == 0)
4917 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
4918 internal_error (__FILE__
, __LINE__
,
4919 "unexpected end of 'g' packet reply");
4921 if (p
[0] == 'x' && p
[1] == 'x')
4922 regs
[i
] = 0; /* 'x' */
4924 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
4930 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
4932 struct packet_reg
*r
= &rsa
->regs
[i
];
4935 if (r
->offset
* 2 >= strlen (rs
->buf
))
4936 /* This shouldn't happen - we adjusted in_g_packet above. */
4937 internal_error (__FILE__
, __LINE__
,
4938 "unexpected end of 'g' packet reply");
4939 else if (rs
->buf
[r
->offset
* 2] == 'x')
4941 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
4942 /* The register isn't available, mark it as such (at
4943 the same time setting the value to zero). */
4944 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
4947 regcache_raw_supply (regcache
, r
->regnum
,
4955 fetch_registers_using_g (struct regcache
*regcache
)
4958 process_g_packet (regcache
);
4962 remote_fetch_registers (struct regcache
*regcache
, int regnum
)
4964 struct remote_state
*rs
= get_remote_state ();
4965 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4968 set_general_thread (inferior_ptid
);
4972 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
4973 gdb_assert (reg
!= NULL
);
4975 /* If this register might be in the 'g' packet, try that first -
4976 we are likely to read more than one register. If this is the
4977 first 'g' packet, we might be overly optimistic about its
4978 contents, so fall back to 'p'. */
4979 if (reg
->in_g_packet
)
4981 fetch_registers_using_g (regcache
);
4982 if (reg
->in_g_packet
)
4986 if (fetch_register_using_p (regcache
, reg
))
4989 /* This register is not available. */
4990 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
4995 fetch_registers_using_g (regcache
);
4997 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
4998 if (!rsa
->regs
[i
].in_g_packet
)
4999 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
5001 /* This register is not available. */
5002 regcache_raw_supply (regcache
, i
, NULL
);
5006 /* Prepare to store registers. Since we may send them all (using a
5007 'G' request), we have to read out the ones we don't want to change
5011 remote_prepare_to_store (struct regcache
*regcache
)
5013 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5015 gdb_byte buf
[MAX_REGISTER_SIZE
];
5017 /* Make sure the entire registers array is valid. */
5018 switch (remote_protocol_packets
[PACKET_P
].support
)
5020 case PACKET_DISABLE
:
5021 case PACKET_SUPPORT_UNKNOWN
:
5022 /* Make sure all the necessary registers are cached. */
5023 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5024 if (rsa
->regs
[i
].in_g_packet
)
5025 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
5032 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
5033 packet was not recognized. */
5036 store_register_using_P (const struct regcache
*regcache
, struct packet_reg
*reg
)
5038 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5039 struct remote_state
*rs
= get_remote_state ();
5040 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5041 /* Try storing a single register. */
5042 char *buf
= rs
->buf
;
5043 gdb_byte regp
[MAX_REGISTER_SIZE
];
5046 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
5049 if (reg
->pnum
== -1)
5052 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
5053 p
= buf
+ strlen (buf
);
5054 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
5055 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
5056 remote_send (&rs
->buf
, &rs
->buf_size
);
5058 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
5063 error (_("Could not write register \"%s\""),
5064 gdbarch_register_name (gdbarch
, reg
->regnum
));
5065 case PACKET_UNKNOWN
:
5068 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
5072 /* Store register REGNUM, or all registers if REGNUM == -1, from the
5073 contents of the register cache buffer. FIXME: ignores errors. */
5076 store_registers_using_G (const struct regcache
*regcache
)
5078 struct remote_state
*rs
= get_remote_state ();
5079 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5083 /* Extract all the registers in the regcache copying them into a
5087 regs
= alloca (rsa
->sizeof_g_packet
);
5088 memset (regs
, 0, rsa
->sizeof_g_packet
);
5089 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5091 struct packet_reg
*r
= &rsa
->regs
[i
];
5093 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
5097 /* Command describes registers byte by byte,
5098 each byte encoded as two hex characters. */
5101 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
5103 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
5104 remote_send (&rs
->buf
, &rs
->buf_size
);
5107 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
5108 of the register cache buffer. FIXME: ignores errors. */
5111 remote_store_registers (struct regcache
*regcache
, int regnum
)
5113 struct remote_state
*rs
= get_remote_state ();
5114 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5117 set_general_thread (inferior_ptid
);
5121 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5122 gdb_assert (reg
!= NULL
);
5124 /* Always prefer to store registers using the 'P' packet if
5125 possible; we often change only a small number of registers.
5126 Sometimes we change a larger number; we'd need help from a
5127 higher layer to know to use 'G'. */
5128 if (store_register_using_P (regcache
, reg
))
5131 /* For now, don't complain if we have no way to write the
5132 register. GDB loses track of unavailable registers too
5133 easily. Some day, this may be an error. We don't have
5134 any way to read the register, either... */
5135 if (!reg
->in_g_packet
)
5138 store_registers_using_G (regcache
);
5142 store_registers_using_G (regcache
);
5144 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5145 if (!rsa
->regs
[i
].in_g_packet
)
5146 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
5147 /* See above for why we do not issue an error here. */
5152 /* Return the number of hex digits in num. */
5155 hexnumlen (ULONGEST num
)
5159 for (i
= 0; num
!= 0; i
++)
5165 /* Set BUF to the minimum number of hex digits representing NUM. */
5168 hexnumstr (char *buf
, ULONGEST num
)
5170 int len
= hexnumlen (num
);
5171 return hexnumnstr (buf
, num
, len
);
5175 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
5178 hexnumnstr (char *buf
, ULONGEST num
, int width
)
5184 for (i
= width
- 1; i
>= 0; i
--)
5186 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
5193 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
5196 remote_address_masked (CORE_ADDR addr
)
5198 int address_size
= remote_address_size
;
5199 /* If "remoteaddresssize" was not set, default to target address size. */
5201 address_size
= gdbarch_addr_bit (target_gdbarch
);
5203 if (address_size
> 0
5204 && address_size
< (sizeof (ULONGEST
) * 8))
5206 /* Only create a mask when that mask can safely be constructed
5207 in a ULONGEST variable. */
5209 mask
= (mask
<< address_size
) - 1;
5215 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
5216 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
5217 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
5218 (which may be more than *OUT_LEN due to escape characters). The
5219 total number of bytes in the output buffer will be at most
5223 remote_escape_output (const gdb_byte
*buffer
, int len
,
5224 gdb_byte
*out_buf
, int *out_len
,
5227 int input_index
, output_index
;
5230 for (input_index
= 0; input_index
< len
; input_index
++)
5232 gdb_byte b
= buffer
[input_index
];
5234 if (b
== '$' || b
== '#' || b
== '}')
5236 /* These must be escaped. */
5237 if (output_index
+ 2 > out_maxlen
)
5239 out_buf
[output_index
++] = '}';
5240 out_buf
[output_index
++] = b
^ 0x20;
5244 if (output_index
+ 1 > out_maxlen
)
5246 out_buf
[output_index
++] = b
;
5250 *out_len
= input_index
;
5251 return output_index
;
5254 /* Convert BUFFER, escaped data LEN bytes long, into binary data
5255 in OUT_BUF. Return the number of bytes written to OUT_BUF.
5256 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
5258 This function reverses remote_escape_output. It allows more
5259 escaped characters than that function does, in particular because
5260 '*' must be escaped to avoid the run-length encoding processing
5261 in reading packets. */
5264 remote_unescape_input (const gdb_byte
*buffer
, int len
,
5265 gdb_byte
*out_buf
, int out_maxlen
)
5267 int input_index
, output_index
;
5272 for (input_index
= 0; input_index
< len
; input_index
++)
5274 gdb_byte b
= buffer
[input_index
];
5276 if (output_index
+ 1 > out_maxlen
)
5278 warning (_("Received too much data from remote target;"
5279 " ignoring overflow."));
5280 return output_index
;
5285 out_buf
[output_index
++] = b
^ 0x20;
5291 out_buf
[output_index
++] = b
;
5295 error (_("Unmatched escape character in target response."));
5297 return output_index
;
5300 /* Determine whether the remote target supports binary downloading.
5301 This is accomplished by sending a no-op memory write of zero length
5302 to the target at the specified address. It does not suffice to send
5303 the whole packet, since many stubs strip the eighth bit and
5304 subsequently compute a wrong checksum, which causes real havoc with
5307 NOTE: This can still lose if the serial line is not eight-bit
5308 clean. In cases like this, the user should clear "remote
5312 check_binary_download (CORE_ADDR addr
)
5314 struct remote_state
*rs
= get_remote_state ();
5316 switch (remote_protocol_packets
[PACKET_X
].support
)
5318 case PACKET_DISABLE
:
5322 case PACKET_SUPPORT_UNKNOWN
:
5328 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5330 p
+= hexnumstr (p
, (ULONGEST
) 0);
5334 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
5335 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5337 if (rs
->buf
[0] == '\0')
5340 fprintf_unfiltered (gdb_stdlog
,
5341 "binary downloading NOT suppported by target\n");
5342 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
5347 fprintf_unfiltered (gdb_stdlog
,
5348 "binary downloading suppported by target\n");
5349 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
5356 /* Write memory data directly to the remote machine.
5357 This does not inform the data cache; the data cache uses this.
5358 HEADER is the starting part of the packet.
5359 MEMADDR is the address in the remote memory space.
5360 MYADDR is the address of the buffer in our space.
5361 LEN is the number of bytes.
5362 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
5363 should send data as binary ('X'), or hex-encoded ('M').
5365 The function creates packet of the form
5366 <HEADER><ADDRESS>,<LENGTH>:<DATA>
5368 where encoding of <DATA> is termined by PACKET_FORMAT.
5370 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
5373 Returns the number of bytes transferred, or 0 (setting errno) for
5374 error. Only transfer a single packet. */
5377 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
5378 const gdb_byte
*myaddr
, int len
,
5379 char packet_format
, int use_length
)
5381 struct remote_state
*rs
= get_remote_state ();
5391 if (packet_format
!= 'X' && packet_format
!= 'M')
5392 internal_error (__FILE__
, __LINE__
,
5393 "remote_write_bytes_aux: bad packet format");
5398 payload_size
= get_memory_write_packet_size ();
5400 /* The packet buffer will be large enough for the payload;
5401 get_memory_packet_size ensures this. */
5404 /* Compute the size of the actual payload by subtracting out the
5405 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
5407 payload_size
-= strlen ("$,:#NN");
5409 /* The comma won't be used. */
5411 header_length
= strlen (header
);
5412 payload_size
-= header_length
;
5413 payload_size
-= hexnumlen (memaddr
);
5415 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
5417 strcat (rs
->buf
, header
);
5418 p
= rs
->buf
+ strlen (header
);
5420 /* Compute a best guess of the number of bytes actually transfered. */
5421 if (packet_format
== 'X')
5423 /* Best guess at number of bytes that will fit. */
5424 todo
= min (len
, payload_size
);
5426 payload_size
-= hexnumlen (todo
);
5427 todo
= min (todo
, payload_size
);
5431 /* Num bytes that will fit. */
5432 todo
= min (len
, payload_size
/ 2);
5434 payload_size
-= hexnumlen (todo
);
5435 todo
= min (todo
, payload_size
/ 2);
5439 internal_error (__FILE__
, __LINE__
,
5440 _("minumum packet size too small to write data"));
5442 /* If we already need another packet, then try to align the end
5443 of this packet to a useful boundary. */
5444 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
5445 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
5447 /* Append "<memaddr>". */
5448 memaddr
= remote_address_masked (memaddr
);
5449 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
5456 /* Append <len>. Retain the location/size of <len>. It may need to
5457 be adjusted once the packet body has been created. */
5459 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
5467 /* Append the packet body. */
5468 if (packet_format
== 'X')
5470 /* Binary mode. Send target system values byte by byte, in
5471 increasing byte addresses. Only escape certain critical
5473 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
5476 /* If not all TODO bytes fit, then we'll need another packet. Make
5477 a second try to keep the end of the packet aligned. Don't do
5478 this if the packet is tiny. */
5479 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
5483 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
5485 if (new_nr_bytes
!= nr_bytes
)
5486 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
5491 p
+= payload_length
;
5492 if (use_length
&& nr_bytes
< todo
)
5494 /* Escape chars have filled up the buffer prematurely,
5495 and we have actually sent fewer bytes than planned.
5496 Fix-up the length field of the packet. Use the same
5497 number of characters as before. */
5498 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
5499 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
5504 /* Normal mode: Send target system values byte by byte, in
5505 increasing byte addresses. Each byte is encoded as a two hex
5507 nr_bytes
= bin2hex (myaddr
, p
, todo
);
5511 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
5512 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5514 if (rs
->buf
[0] == 'E')
5516 /* There is no correspondance between what the remote protocol
5517 uses for errors and errno codes. We would like a cleaner way
5518 of representing errors (big enough to include errno codes,
5519 bfd_error codes, and others). But for now just return EIO. */
5524 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
5525 fewer bytes than we'd planned. */
5529 /* Write memory data directly to the remote machine.
5530 This does not inform the data cache; the data cache uses this.
5531 MEMADDR is the address in the remote memory space.
5532 MYADDR is the address of the buffer in our space.
5533 LEN is the number of bytes.
5535 Returns number of bytes transferred, or 0 (setting errno) for
5536 error. Only transfer a single packet. */
5539 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
5541 char *packet_format
= 0;
5543 /* Check whether the target supports binary download. */
5544 check_binary_download (memaddr
);
5546 switch (remote_protocol_packets
[PACKET_X
].support
)
5549 packet_format
= "X";
5551 case PACKET_DISABLE
:
5552 packet_format
= "M";
5554 case PACKET_SUPPORT_UNKNOWN
:
5555 internal_error (__FILE__
, __LINE__
,
5556 _("remote_write_bytes: bad internal state"));
5558 internal_error (__FILE__
, __LINE__
, _("bad switch"));
5561 return remote_write_bytes_aux (packet_format
,
5562 memaddr
, myaddr
, len
, packet_format
[0], 1);
5565 /* Read memory data directly from the remote machine.
5566 This does not use the data cache; the data cache uses this.
5567 MEMADDR is the address in the remote memory space.
5568 MYADDR is the address of the buffer in our space.
5569 LEN is the number of bytes.
5571 Returns number of bytes transferred, or 0 for error. */
5573 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
5574 remote targets) shouldn't attempt to read the entire buffer.
5575 Instead it should read a single packet worth of data and then
5576 return the byte size of that packet to the caller. The caller (its
5577 caller and its callers caller ;-) already contains code for
5578 handling partial reads. */
5581 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
5583 struct remote_state
*rs
= get_remote_state ();
5584 int max_buf_size
; /* Max size of packet output buffer. */
5590 max_buf_size
= get_memory_read_packet_size ();
5591 /* The packet buffer will be large enough for the payload;
5592 get_memory_packet_size ensures this. */
5601 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
5603 /* construct "m"<memaddr>","<len>" */
5604 /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */
5605 memaddr
= remote_address_masked (memaddr
);
5608 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
5610 p
+= hexnumstr (p
, (ULONGEST
) todo
);
5614 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5616 if (rs
->buf
[0] == 'E'
5617 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
5618 && rs
->buf
[3] == '\0')
5620 /* There is no correspondance between what the remote
5621 protocol uses for errors and errno codes. We would like
5622 a cleaner way of representing errors (big enough to
5623 include errno codes, bfd_error codes, and others). But
5624 for now just return EIO. */
5629 /* Reply describes memory byte by byte,
5630 each byte encoded as two hex characters. */
5633 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
5635 /* Reply is short. This means that we were able to read
5636 only part of what we wanted to. */
5637 return i
+ (origlen
- len
);
5647 /* Remote notification handler. */
5650 handle_notification (char *buf
, size_t length
)
5652 if (strncmp (buf
, "Stop:", 5) == 0)
5654 if (pending_stop_reply
)
5655 /* We've already parsed the in-flight stop-reply, but the stub
5656 for some reason thought we didn't, possibly due to timeout
5657 on its side. Just ignore it. */
5661 struct cleanup
*old_chain
;
5662 struct stop_reply
*reply
= stop_reply_xmalloc ();
5663 old_chain
= make_cleanup (do_stop_reply_xfree
, reply
);
5665 remote_parse_stop_reply (buf
+ 5, reply
);
5667 discard_cleanups (old_chain
);
5669 /* Be careful to only set it after parsing, since an error
5670 may be thrown then. */
5671 pending_stop_reply
= reply
;
5673 /* Notify the event loop there's a stop reply to acknowledge
5674 and that there may be more events to fetch. */
5675 mark_async_event_handler (remote_async_get_pending_events_token
);
5679 /* We ignore notifications we don't recognize, for compatibility
5680 with newer stubs. */
5685 /* Read or write LEN bytes from inferior memory at MEMADDR,
5686 transferring to or from debugger address BUFFER. Write to inferior
5687 if SHOULD_WRITE is nonzero. Returns length of data written or
5688 read; 0 for error. TARGET is unused. */
5691 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
5692 int should_write
, struct mem_attrib
*attrib
,
5693 struct target_ops
*target
)
5697 set_general_thread (inferior_ptid
);
5700 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
5702 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
5707 /* Sends a packet with content determined by the printf format string
5708 FORMAT and the remaining arguments, then gets the reply. Returns
5709 whether the packet was a success, a failure, or unknown. */
5712 remote_send_printf (const char *format
, ...)
5714 struct remote_state
*rs
= get_remote_state ();
5715 int max_size
= get_remote_packet_size ();
5718 va_start (ap
, format
);
5721 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
5722 internal_error (__FILE__
, __LINE__
, "Too long remote packet.");
5724 if (putpkt (rs
->buf
) < 0)
5725 error (_("Communication problem with target."));
5728 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5730 return packet_check_result (rs
->buf
);
5734 restore_remote_timeout (void *p
)
5736 int value
= *(int *)p
;
5737 remote_timeout
= value
;
5740 /* Flash writing can take quite some time. We'll set
5741 effectively infinite timeout for flash operations.
5742 In future, we'll need to decide on a better approach. */
5743 static const int remote_flash_timeout
= 1000;
5746 remote_flash_erase (struct target_ops
*ops
,
5747 ULONGEST address
, LONGEST length
)
5749 int saved_remote_timeout
= remote_timeout
;
5750 enum packet_result ret
;
5752 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5753 &saved_remote_timeout
);
5754 remote_timeout
= remote_flash_timeout
;
5756 ret
= remote_send_printf ("vFlashErase:%s,%s",
5761 case PACKET_UNKNOWN
:
5762 error (_("Remote target does not support flash erase"));
5764 error (_("Error erasing flash with vFlashErase packet"));
5769 do_cleanups (back_to
);
5773 remote_flash_write (struct target_ops
*ops
,
5774 ULONGEST address
, LONGEST length
,
5775 const gdb_byte
*data
)
5777 int saved_remote_timeout
= remote_timeout
;
5779 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5780 &saved_remote_timeout
);
5782 remote_timeout
= remote_flash_timeout
;
5783 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
5784 do_cleanups (back_to
);
5790 remote_flash_done (struct target_ops
*ops
)
5792 int saved_remote_timeout
= remote_timeout
;
5794 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5795 &saved_remote_timeout
);
5797 remote_timeout
= remote_flash_timeout
;
5798 ret
= remote_send_printf ("vFlashDone");
5799 do_cleanups (back_to
);
5803 case PACKET_UNKNOWN
:
5804 error (_("Remote target does not support vFlashDone"));
5806 error (_("Error finishing flash operation"));
5813 remote_files_info (struct target_ops
*ignore
)
5815 puts_filtered ("Debugging a target over a serial line.\n");
5818 /* Stuff for dealing with the packets which are part of this protocol.
5819 See comment at top of file for details. */
5821 /* Read a single character from the remote end. */
5824 readchar (int timeout
)
5828 ch
= serial_readchar (remote_desc
, timeout
);
5833 switch ((enum serial_rc
) ch
)
5837 error (_("Remote connection closed"));
5840 perror_with_name (_("Remote communication error"));
5842 case SERIAL_TIMEOUT
:
5848 /* Send the command in *BUF to the remote machine, and read the reply
5849 into *BUF. Report an error if we get an error reply. Resize
5850 *BUF using xrealloc if necessary to hold the result, and update
5854 remote_send (char **buf
,
5858 getpkt (buf
, sizeof_buf
, 0);
5860 if ((*buf
)[0] == 'E')
5861 error (_("Remote failure reply: %s"), *buf
);
5864 /* Return a pointer to an xmalloc'ed string representing an escaped
5865 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
5866 etc. The caller is responsible for releasing the returned
5870 escape_buffer (const char *buf
, int n
)
5872 struct cleanup
*old_chain
;
5873 struct ui_file
*stb
;
5877 stb
= mem_fileopen ();
5878 old_chain
= make_cleanup_ui_file_delete (stb
);
5880 fputstrn_unfiltered (buf
, n
, 0, stb
);
5881 str
= ui_file_xstrdup (stb
, &length
);
5882 do_cleanups (old_chain
);
5886 /* Display a null-terminated packet on stdout, for debugging, using C
5890 print_packet (char *buf
)
5892 puts_filtered ("\"");
5893 fputstr_filtered (buf
, '"', gdb_stdout
);
5894 puts_filtered ("\"");
5900 return putpkt_binary (buf
, strlen (buf
));
5903 /* Send a packet to the remote machine, with error checking. The data
5904 of the packet is in BUF. The string in BUF can be at most
5905 get_remote_packet_size () - 5 to account for the $, # and checksum,
5906 and for a possible /0 if we are debugging (remote_debug) and want
5907 to print the sent packet as a string. */
5910 putpkt_binary (char *buf
, int cnt
)
5912 struct remote_state
*rs
= get_remote_state ();
5914 unsigned char csum
= 0;
5915 char *buf2
= alloca (cnt
+ 6);
5921 /* Catch cases like trying to read memory or listing threads while
5922 we're waiting for a stop reply. The remote server wouldn't be
5923 ready to handle this request, so we'd hang and timeout. We don't
5924 have to worry about this in synchronous mode, because in that
5925 case it's not possible to issue a command while the target is
5926 running. This is not a problem in non-stop mode, because in that
5927 case, the stub is always ready to process serial input. */
5928 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
5929 error (_("Cannot execute this command while the target is running."));
5931 /* We're sending out a new packet. Make sure we don't look at a
5932 stale cached response. */
5933 rs
->cached_wait_status
= 0;
5935 /* Copy the packet into buffer BUF2, encapsulating it
5936 and giving it a checksum. */
5941 for (i
= 0; i
< cnt
; i
++)
5947 *p
++ = tohex ((csum
>> 4) & 0xf);
5948 *p
++ = tohex (csum
& 0xf);
5950 /* Send it over and over until we get a positive ack. */
5954 int started_error_output
= 0;
5958 struct cleanup
*old_chain
;
5962 str
= escape_buffer (buf2
, p
- buf2
);
5963 old_chain
= make_cleanup (xfree
, str
);
5964 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", str
);
5965 gdb_flush (gdb_stdlog
);
5966 do_cleanups (old_chain
);
5968 if (serial_write (remote_desc
, buf2
, p
- buf2
))
5969 perror_with_name (_("putpkt: write failed"));
5971 /* If this is a no acks version of the remote protocol, send the
5972 packet and move on. */
5976 /* Read until either a timeout occurs (-2) or '+' is read.
5977 Handle any notification that arrives in the mean time. */
5980 ch
= readchar (remote_timeout
);
5988 case SERIAL_TIMEOUT
:
5991 if (started_error_output
)
5993 putchar_unfiltered ('\n');
5994 started_error_output
= 0;
6003 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
6007 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
6008 case SERIAL_TIMEOUT
:
6012 break; /* Retransmit buffer. */
6016 fprintf_unfiltered (gdb_stdlog
,
6017 "Packet instead of Ack, ignoring it\n");
6018 /* It's probably an old response sent because an ACK
6019 was lost. Gobble up the packet and ack it so it
6020 doesn't get retransmitted when we resend this
6023 serial_write (remote_desc
, "+", 1);
6024 continue; /* Now, go look for +. */
6031 /* If we got a notification, handle it, and go back to looking
6033 /* We've found the start of a notification. Now
6034 collect the data. */
6035 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
6040 struct cleanup
*old_chain
;
6043 str
= escape_buffer (rs
->buf
, val
);
6044 old_chain
= make_cleanup (xfree
, str
);
6045 fprintf_unfiltered (gdb_stdlog
,
6046 " Notification received: %s\n",
6048 do_cleanups (old_chain
);
6050 handle_notification (rs
->buf
, val
);
6051 /* We're in sync now, rewait for the ack. */
6058 if (!started_error_output
)
6060 started_error_output
= 1;
6061 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6063 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6064 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
6073 if (!started_error_output
)
6075 started_error_output
= 1;
6076 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6078 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6082 break; /* Here to retransmit. */
6086 /* This is wrong. If doing a long backtrace, the user should be
6087 able to get out next time we call QUIT, without anything as
6088 violent as interrupt_query. If we want to provide a way out of
6089 here without getting to the next QUIT, it should be based on
6090 hitting ^C twice as in remote_wait. */
6101 /* Come here after finding the start of a frame when we expected an
6102 ack. Do our best to discard the rest of this packet. */
6111 c
= readchar (remote_timeout
);
6114 case SERIAL_TIMEOUT
:
6115 /* Nothing we can do. */
6118 /* Discard the two bytes of checksum and stop. */
6119 c
= readchar (remote_timeout
);
6121 c
= readchar (remote_timeout
);
6124 case '*': /* Run length encoding. */
6125 /* Discard the repeat count. */
6126 c
= readchar (remote_timeout
);
6131 /* A regular character. */
6137 /* Come here after finding the start of the frame. Collect the rest
6138 into *BUF, verifying the checksum, length, and handling run-length
6139 compression. NUL terminate the buffer. If there is not enough room,
6140 expand *BUF using xrealloc.
6142 Returns -1 on error, number of characters in buffer (ignoring the
6143 trailing NULL) on success. (could be extended to return one of the
6144 SERIAL status indications). */
6147 read_frame (char **buf_p
,
6154 struct remote_state
*rs
= get_remote_state ();
6161 c
= readchar (remote_timeout
);
6164 case SERIAL_TIMEOUT
:
6166 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
6170 fputs_filtered ("Saw new packet start in middle of old one\n",
6172 return -1; /* Start a new packet, count retries. */
6175 unsigned char pktcsum
;
6181 check_0
= readchar (remote_timeout
);
6183 check_1
= readchar (remote_timeout
);
6185 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
6188 fputs_filtered ("Timeout in checksum, retrying\n",
6192 else if (check_0
< 0 || check_1
< 0)
6195 fputs_filtered ("Communication error in checksum\n",
6200 /* Don't recompute the checksum; with no ack packets we
6201 don't have any way to indicate a packet retransmission
6206 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
6207 if (csum
== pktcsum
)
6212 struct cleanup
*old_chain
;
6215 str
= escape_buffer (buf
, bc
);
6216 old_chain
= make_cleanup (xfree
, str
);
6217 fprintf_unfiltered (gdb_stdlog
,
6219 Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
6220 pktcsum
, csum
, str
);
6221 do_cleanups (old_chain
);
6223 /* Number of characters in buffer ignoring trailing
6227 case '*': /* Run length encoding. */
6232 c
= readchar (remote_timeout
);
6234 repeat
= c
- ' ' + 3; /* Compute repeat count. */
6236 /* The character before ``*'' is repeated. */
6238 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
6240 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
6242 /* Make some more room in the buffer. */
6243 *sizeof_buf
+= repeat
;
6244 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
6248 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
6254 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
6258 if (bc
>= *sizeof_buf
- 1)
6260 /* Make some more room in the buffer. */
6262 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
6273 /* Read a packet from the remote machine, with error checking, and
6274 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
6275 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
6276 rather than timing out; this is used (in synchronous mode) to wait
6277 for a target that is is executing user code to stop. */
6278 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
6279 don't have to change all the calls to getpkt to deal with the
6280 return value, because at the moment I don't know what the right
6281 thing to do it for those. */
6289 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
6293 /* Read a packet from the remote machine, with error checking, and
6294 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
6295 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
6296 rather than timing out; this is used (in synchronous mode) to wait
6297 for a target that is is executing user code to stop. If FOREVER ==
6298 0, this function is allowed to time out gracefully and return an
6299 indication of this to the caller. Otherwise return the number of
6300 bytes read. If EXPECTING_NOTIF, consider receiving a notification
6301 enough reason to return to the caller. */
6304 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
6305 int expecting_notif
)
6307 struct remote_state
*rs
= get_remote_state ();
6313 /* We're reading a new response. Make sure we don't look at a
6314 previously cached response. */
6315 rs
->cached_wait_status
= 0;
6317 strcpy (*buf
, "timeout");
6320 timeout
= watchdog
> 0 ? watchdog
: -1;
6321 else if (expecting_notif
)
6322 timeout
= 0; /* There should already be a char in the buffer. If
6325 timeout
= remote_timeout
;
6329 /* Process any number of notifications, and then return when
6333 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
6335 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
6337 /* This can loop forever if the remote side sends us
6338 characters continuously, but if it pauses, we'll get
6339 SERIAL_TIMEOUT from readchar because of timeout. Then
6340 we'll count that as a retry.
6342 Note that even when forever is set, we will only wait
6343 forever prior to the start of a packet. After that, we
6344 expect characters to arrive at a brisk pace. They should
6345 show up within remote_timeout intervals. */
6347 c
= readchar (timeout
);
6348 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
6350 if (c
== SERIAL_TIMEOUT
)
6352 if (expecting_notif
)
6353 return -1; /* Don't complain, it's normal to not get
6354 anything in this case. */
6356 if (forever
) /* Watchdog went off? Kill the target. */
6360 error (_("Watchdog timeout has expired. Target detached."));
6363 fputs_filtered ("Timed out.\n", gdb_stdlog
);
6367 /* We've found the start of a packet or notification.
6368 Now collect the data. */
6369 val
= read_frame (buf
, sizeof_buf
);
6374 serial_write (remote_desc
, "-", 1);
6377 if (tries
> MAX_TRIES
)
6379 /* We have tried hard enough, and just can't receive the
6380 packet/notification. Give up. */
6381 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
6383 /* Skip the ack char if we're in no-ack mode. */
6384 if (!rs
->noack_mode
)
6385 serial_write (remote_desc
, "+", 1);
6389 /* If we got an ordinary packet, return that to our caller. */
6394 struct cleanup
*old_chain
;
6397 str
= escape_buffer (*buf
, val
);
6398 old_chain
= make_cleanup (xfree
, str
);
6399 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", str
);
6400 do_cleanups (old_chain
);
6403 /* Skip the ack char if we're in no-ack mode. */
6404 if (!rs
->noack_mode
)
6405 serial_write (remote_desc
, "+", 1);
6409 /* If we got a notification, handle it, and go back to looking
6413 gdb_assert (c
== '%');
6417 struct cleanup
*old_chain
;
6420 str
= escape_buffer (*buf
, val
);
6421 old_chain
= make_cleanup (xfree
, str
);
6422 fprintf_unfiltered (gdb_stdlog
,
6423 " Notification received: %s\n",
6425 do_cleanups (old_chain
);
6428 handle_notification (*buf
, val
);
6430 /* Notifications require no acknowledgement. */
6432 if (expecting_notif
)
6439 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
6441 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0);
6445 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
)
6447 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1);
6454 /* Use catch_errors so the user can quit from gdb even when we
6455 aren't on speaking terms with the remote system. */
6456 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
6458 /* Don't wait for it to die. I'm not really sure it matters whether
6459 we do or not. For the existing stubs, kill is a noop. */
6460 target_mourn_inferior ();
6464 remote_vkill (int pid
, struct remote_state
*rs
)
6466 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
6469 /* Tell the remote target to detach. */
6470 sprintf (rs
->buf
, "vKill;%x", pid
);
6472 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6474 if (packet_ok (rs
->buf
,
6475 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
6477 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
6484 extended_remote_kill (void)
6487 int pid
= ptid_get_pid (inferior_ptid
);
6488 struct remote_state
*rs
= get_remote_state ();
6490 res
= remote_vkill (pid
, rs
);
6491 if (res
== -1 && !remote_multi_process_p (rs
))
6493 /* Don't try 'k' on a multi-process aware stub -- it has no way
6494 to specify the pid. */
6498 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6499 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
6502 /* Don't wait for it to die. I'm not really sure it matters whether
6503 we do or not. For the existing stubs, kill is a noop. */
6509 error (_("Can't kill process"));
6511 delete_inferior (pid
);
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 /* Unlike "target remote", we do not want to unpush the target; then
6556 the next time the user says "run", we won't be connected. */
6558 if (have_inferiors ())
6560 extern void nullify_last_target_wait_ptid ();
6561 /* Multi-process case. The current process has exited, but
6562 there are other processes to debug. Switch to the first
6564 iterate_over_threads (select_new_thread_callback
, NULL
);
6565 nullify_last_target_wait_ptid ();
6569 struct remote_state
*rs
= get_remote_state ();
6571 /* Call common code to mark the inferior as not running. */
6572 generic_mourn_inferior ();
6573 if (!remote_multi_process_p (rs
))
6575 /* Check whether the target is running now - some remote stubs
6576 automatically restart after kill. */
6578 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6580 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
6582 /* Assume that the target has been restarted. Set inferior_ptid
6583 so that bits of core GDB realizes there's something here, e.g.,
6584 so that the user can say "kill" again. */
6585 inferior_ptid
= magic_null_ptid
;
6589 /* Mark this (still pushed) target as not executable until we
6591 target_mark_exited (target
);
6595 /* Always remove execution if this was the last process. */
6596 target_mark_exited (target
);
6601 extended_remote_mourn (struct target_ops
*ops
)
6603 extended_remote_mourn_1 (ops
);
6607 extended_remote_run (char *args
)
6609 struct remote_state
*rs
= get_remote_state ();
6613 /* If the user has disabled vRun support, or we have detected that
6614 support is not available, do not try it. */
6615 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
6618 strcpy (rs
->buf
, "vRun;");
6619 len
= strlen (rs
->buf
);
6621 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
6622 error (_("Remote file name too long for run packet"));
6623 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
, 0);
6625 gdb_assert (args
!= NULL
);
6628 struct cleanup
*back_to
;
6632 argv
= gdb_buildargv (args
);
6633 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
6634 for (i
= 0; argv
[i
] != NULL
; i
++)
6636 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
6637 error (_("Argument list too long for run packet"));
6638 rs
->buf
[len
++] = ';';
6639 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
, 0);
6641 do_cleanups (back_to
);
6644 rs
->buf
[len
++] = '\0';
6647 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6649 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
6651 /* We have a wait response; we don't need it, though. All is well. */
6654 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
6655 /* It wasn't disabled before, but it is now. */
6659 if (remote_exec_file
[0] == '\0')
6660 error (_("Running the default executable on the remote target failed; "
6661 "try \"set remote exec-file\"?"));
6663 error (_("Running \"%s\" on the remote target failed"),
6668 /* In the extended protocol we want to be able to do things like
6669 "run" and have them basically work as expected. So we need
6670 a special create_inferior function. We support changing the
6671 executable file and the command line arguments, but not the
6675 extended_remote_create_inferior_1 (char *exec_file
, char *args
,
6676 char **env
, int from_tty
)
6678 /* If running asynchronously, register the target file descriptor
6679 with the event loop. */
6680 if (target_can_async_p ())
6681 target_async (inferior_event_handler
, 0);
6683 /* Now restart the remote server. */
6684 if (extended_remote_run (args
) == -1)
6686 /* vRun was not supported. Fail if we need it to do what the
6688 if (remote_exec_file
[0])
6689 error (_("Remote target does not support \"set remote exec-file\""));
6691 error (_("Remote target does not support \"set args\" or run <ARGS>"));
6693 /* Fall back to "R". */
6694 extended_remote_restart ();
6697 /* Clean up from the last time we ran, before we mark the target
6698 running again. This will mark breakpoints uninserted, and
6699 get_offsets may insert breakpoints. */
6700 init_thread_list ();
6701 init_wait_for_inferior ();
6703 /* Now mark the inferior as running before we do anything else. */
6704 inferior_ptid
= magic_null_ptid
;
6706 /* Now, if we have thread information, update inferior_ptid. */
6707 inferior_ptid
= remote_current_thread (inferior_ptid
);
6709 add_inferior (ptid_get_pid (inferior_ptid
));
6710 add_thread_silent (inferior_ptid
);
6712 target_mark_running (&extended_remote_ops
);
6714 /* Get updated offsets, if the stub uses qOffsets. */
6719 extended_remote_create_inferior (struct target_ops
*ops
,
6720 char *exec_file
, char *args
,
6721 char **env
, int from_tty
)
6723 extended_remote_create_inferior_1 (exec_file
, args
, env
, from_tty
);
6727 /* Insert a breakpoint. On targets that have software breakpoint
6728 support, we ask the remote target to do the work; on targets
6729 which don't, we insert a traditional memory breakpoint. */
6732 remote_insert_breakpoint (struct bp_target_info
*bp_tgt
)
6734 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
6735 If it succeeds, then set the support to PACKET_ENABLE. If it
6736 fails, and the user has explicitly requested the Z support then
6737 report an error, otherwise, mark it disabled and go on. */
6739 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
6741 CORE_ADDR addr
= bp_tgt
->placed_address
;
6742 struct remote_state
*rs
;
6746 gdbarch_breakpoint_from_pc (target_gdbarch
, &addr
, &bpsize
);
6748 rs
= get_remote_state ();
6754 addr
= (ULONGEST
) remote_address_masked (addr
);
6755 p
+= hexnumstr (p
, addr
);
6756 sprintf (p
, ",%d", bpsize
);
6759 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6761 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
6766 bp_tgt
->placed_address
= addr
;
6767 bp_tgt
->placed_size
= bpsize
;
6769 case PACKET_UNKNOWN
:
6774 return memory_insert_breakpoint (bp_tgt
);
6778 remote_remove_breakpoint (struct bp_target_info
*bp_tgt
)
6780 CORE_ADDR addr
= bp_tgt
->placed_address
;
6781 struct remote_state
*rs
= get_remote_state ();
6784 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
6792 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
6793 p
+= hexnumstr (p
, addr
);
6794 sprintf (p
, ",%d", bp_tgt
->placed_size
);
6797 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6799 return (rs
->buf
[0] == 'E');
6802 return memory_remove_breakpoint (bp_tgt
);
6806 watchpoint_to_Z_packet (int type
)
6811 return Z_PACKET_WRITE_WP
;
6814 return Z_PACKET_READ_WP
;
6817 return Z_PACKET_ACCESS_WP
;
6820 internal_error (__FILE__
, __LINE__
,
6821 _("hw_bp_to_z: bad watchpoint type %d"), type
);
6826 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
6828 struct remote_state
*rs
= get_remote_state ();
6830 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
6832 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
6835 sprintf (rs
->buf
, "Z%x,", packet
);
6836 p
= strchr (rs
->buf
, '\0');
6837 addr
= remote_address_masked (addr
);
6838 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6839 sprintf (p
, ",%x", len
);
6842 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6844 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
6847 case PACKET_UNKNOWN
:
6852 internal_error (__FILE__
, __LINE__
,
6853 _("remote_insert_watchpoint: reached end of function"));
6858 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
6860 struct remote_state
*rs
= get_remote_state ();
6862 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
6864 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
6867 sprintf (rs
->buf
, "z%x,", packet
);
6868 p
= strchr (rs
->buf
, '\0');
6869 addr
= remote_address_masked (addr
);
6870 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6871 sprintf (p
, ",%x", len
);
6873 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6875 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
6878 case PACKET_UNKNOWN
:
6883 internal_error (__FILE__
, __LINE__
,
6884 _("remote_remove_watchpoint: reached end of function"));
6888 int remote_hw_watchpoint_limit
= -1;
6889 int remote_hw_breakpoint_limit
= -1;
6892 remote_check_watch_resources (int type
, int cnt
, int ot
)
6894 if (type
== bp_hardware_breakpoint
)
6896 if (remote_hw_breakpoint_limit
== 0)
6898 else if (remote_hw_breakpoint_limit
< 0)
6900 else if (cnt
<= remote_hw_breakpoint_limit
)
6905 if (remote_hw_watchpoint_limit
== 0)
6907 else if (remote_hw_watchpoint_limit
< 0)
6911 else if (cnt
<= remote_hw_watchpoint_limit
)
6918 remote_stopped_by_watchpoint (void)
6920 return remote_stopped_by_watchpoint_p
;
6924 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
6927 if (remote_stopped_by_watchpoint ())
6929 *addr_p
= remote_watch_data_address
;
6938 remote_insert_hw_breakpoint (struct bp_target_info
*bp_tgt
)
6941 struct remote_state
*rs
;
6944 /* The length field should be set to the size of a breakpoint
6945 instruction, even though we aren't inserting one ourselves. */
6947 gdbarch_breakpoint_from_pc
6948 (target_gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
6950 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
6953 rs
= get_remote_state ();
6960 addr
= remote_address_masked (bp_tgt
->placed_address
);
6961 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6962 sprintf (p
, ",%x", bp_tgt
->placed_size
);
6965 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6967 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
6970 case PACKET_UNKNOWN
:
6975 internal_error (__FILE__
, __LINE__
,
6976 _("remote_insert_hw_breakpoint: reached end of function"));
6981 remote_remove_hw_breakpoint (struct bp_target_info
*bp_tgt
)
6984 struct remote_state
*rs
= get_remote_state ();
6987 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
6994 addr
= remote_address_masked (bp_tgt
->placed_address
);
6995 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6996 sprintf (p
, ",%x", bp_tgt
->placed_size
);
6999 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7001 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
7004 case PACKET_UNKNOWN
:
7009 internal_error (__FILE__
, __LINE__
,
7010 _("remote_remove_hw_breakpoint: reached end of function"));
7013 /* Table used by the crc32 function to calcuate the checksum. */
7015 static unsigned long crc32_table
[256] =
7018 static unsigned long
7019 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
7021 if (!crc32_table
[1])
7023 /* Initialize the CRC table and the decoding table. */
7027 for (i
= 0; i
< 256; i
++)
7029 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
7030 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
7037 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
7043 /* compare-sections command
7045 With no arguments, compares each loadable section in the exec bfd
7046 with the same memory range on the target, and reports mismatches.
7047 Useful for verifying the image on the target against the exec file.
7048 Depends on the target understanding the new "qCRC:" request. */
7050 /* FIXME: cagney/1999-10-26: This command should be broken down into a
7051 target method (target verify memory) and generic version of the
7052 actual command. This will allow other high-level code (especially
7053 generic_load()) to make use of this target functionality. */
7056 compare_sections_command (char *args
, int from_tty
)
7058 struct remote_state
*rs
= get_remote_state ();
7060 unsigned long host_crc
, target_crc
;
7061 extern bfd
*exec_bfd
;
7062 struct cleanup
*old_chain
;
7065 const char *sectname
;
7072 error (_("command cannot be used without an exec file"));
7073 if (!current_target
.to_shortname
||
7074 strcmp (current_target
.to_shortname
, "remote") != 0)
7075 error (_("command can only be used with remote target"));
7077 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
7079 if (!(s
->flags
& SEC_LOAD
))
7080 continue; /* skip non-loadable section */
7082 size
= bfd_get_section_size (s
);
7084 continue; /* skip zero-length section */
7086 sectname
= bfd_get_section_name (exec_bfd
, s
);
7087 if (args
&& strcmp (args
, sectname
) != 0)
7088 continue; /* not the section selected by user */
7090 matched
= 1; /* do this section */
7092 /* FIXME: assumes lma can fit into long. */
7093 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
7094 (long) lma
, (long) size
);
7097 /* Be clever; compute the host_crc before waiting for target
7099 sectdata
= xmalloc (size
);
7100 old_chain
= make_cleanup (xfree
, sectdata
);
7101 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
7102 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
7104 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7105 if (rs
->buf
[0] == 'E')
7106 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
7107 sectname
, paddr (lma
), paddr (lma
+ size
));
7108 if (rs
->buf
[0] != 'C')
7109 error (_("remote target does not support this operation"));
7111 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
7112 target_crc
= target_crc
* 16 + fromhex (*tmp
);
7114 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
7115 sectname
, paddr (lma
), paddr (lma
+ size
));
7116 if (host_crc
== target_crc
)
7117 printf_filtered ("matched.\n");
7120 printf_filtered ("MIS-MATCHED!\n");
7124 do_cleanups (old_chain
);
7127 warning (_("One or more sections of the remote executable does not match\n\
7128 the loaded file\n"));
7129 if (args
&& !matched
)
7130 printf_filtered (_("No loaded section named '%s'.\n"), args
);
7133 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
7134 into remote target. The number of bytes written to the remote
7135 target is returned, or -1 for error. */
7138 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
7139 const char *annex
, const gdb_byte
*writebuf
,
7140 ULONGEST offset
, LONGEST len
,
7141 struct packet_config
*packet
)
7146 struct remote_state
*rs
= get_remote_state ();
7147 int max_size
= get_memory_write_packet_size ();
7149 if (packet
->support
== PACKET_DISABLE
)
7152 /* Insert header. */
7153 i
= snprintf (rs
->buf
, max_size
,
7154 "qXfer:%s:write:%s:%s:",
7155 object_name
, annex
? annex
: "",
7156 phex_nz (offset
, sizeof offset
));
7157 max_size
-= (i
+ 1);
7159 /* Escape as much data as fits into rs->buf. */
7160 buf_len
= remote_escape_output
7161 (writebuf
, len
, (rs
->buf
+ i
), &max_size
, max_size
);
7163 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
7164 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
7165 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7168 unpack_varlen_hex (rs
->buf
, &n
);
7172 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
7173 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
7174 number of bytes read is returned, or 0 for EOF, or -1 for error.
7175 The number of bytes read may be less than LEN without indicating an
7176 EOF. PACKET is checked and updated to indicate whether the remote
7177 target supports this object. */
7180 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
7182 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
7183 struct packet_config
*packet
)
7185 static char *finished_object
;
7186 static char *finished_annex
;
7187 static ULONGEST finished_offset
;
7189 struct remote_state
*rs
= get_remote_state ();
7190 unsigned int total
= 0;
7191 LONGEST i
, n
, packet_len
;
7193 if (packet
->support
== PACKET_DISABLE
)
7196 /* Check whether we've cached an end-of-object packet that matches
7198 if (finished_object
)
7200 if (strcmp (object_name
, finished_object
) == 0
7201 && strcmp (annex
? annex
: "", finished_annex
) == 0
7202 && offset
== finished_offset
)
7205 /* Otherwise, we're now reading something different. Discard
7207 xfree (finished_object
);
7208 xfree (finished_annex
);
7209 finished_object
= NULL
;
7210 finished_annex
= NULL
;
7213 /* Request only enough to fit in a single packet. The actual data
7214 may not, since we don't know how much of it will need to be escaped;
7215 the target is free to respond with slightly less data. We subtract
7216 five to account for the response type and the protocol frame. */
7217 n
= min (get_remote_packet_size () - 5, len
);
7218 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
7219 object_name
, annex
? annex
: "",
7220 phex_nz (offset
, sizeof offset
),
7221 phex_nz (n
, sizeof n
));
7222 i
= putpkt (rs
->buf
);
7227 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
7228 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7231 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
7232 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
7234 /* 'm' means there is (or at least might be) more data after this
7235 batch. That does not make sense unless there's at least one byte
7236 of data in this reply. */
7237 if (rs
->buf
[0] == 'm' && packet_len
== 1)
7238 error (_("Remote qXfer reply contained no data."));
7240 /* Got some data. */
7241 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
7243 /* 'l' is an EOF marker, possibly including a final block of data,
7244 or possibly empty. If we have the final block of a non-empty
7245 object, record this fact to bypass a subsequent partial read. */
7246 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
7248 finished_object
= xstrdup (object_name
);
7249 finished_annex
= xstrdup (annex
? annex
: "");
7250 finished_offset
= offset
+ i
;
7257 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
7258 const char *annex
, gdb_byte
*readbuf
,
7259 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
7261 struct remote_state
*rs
;
7266 set_general_thread (inferior_ptid
);
7268 rs
= get_remote_state ();
7270 /* Handle memory using the standard memory routines. */
7271 if (object
== TARGET_OBJECT_MEMORY
)
7276 /* If the remote target is connected but not running, we should
7277 pass this request down to a lower stratum (e.g. the executable
7279 if (!target_has_execution
)
7282 if (writebuf
!= NULL
)
7283 xfered
= remote_write_bytes (offset
, writebuf
, len
);
7285 xfered
= remote_read_bytes (offset
, readbuf
, len
);
7289 else if (xfered
== 0 && errno
== 0)
7295 /* Handle SPU memory using qxfer packets. */
7296 if (object
== TARGET_OBJECT_SPU
)
7299 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
7300 &remote_protocol_packets
7301 [PACKET_qXfer_spu_read
]);
7303 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
7304 &remote_protocol_packets
7305 [PACKET_qXfer_spu_write
]);
7308 /* Only handle flash writes. */
7309 if (writebuf
!= NULL
)
7315 case TARGET_OBJECT_FLASH
:
7316 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
7320 else if (xfered
== 0 && errno
== 0)
7330 /* Map pre-existing objects onto letters. DO NOT do this for new
7331 objects!!! Instead specify new query packets. */
7334 case TARGET_OBJECT_AVR
:
7338 case TARGET_OBJECT_AUXV
:
7339 gdb_assert (annex
== NULL
);
7340 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
7341 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
7343 case TARGET_OBJECT_AVAILABLE_FEATURES
:
7344 return remote_read_qxfer
7345 (ops
, "features", annex
, readbuf
, offset
, len
,
7346 &remote_protocol_packets
[PACKET_qXfer_features
]);
7348 case TARGET_OBJECT_LIBRARIES
:
7349 return remote_read_qxfer
7350 (ops
, "libraries", annex
, readbuf
, offset
, len
,
7351 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
7353 case TARGET_OBJECT_MEMORY_MAP
:
7354 gdb_assert (annex
== NULL
);
7355 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
7356 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
7362 /* Note: a zero OFFSET and LEN can be used to query the minimum
7364 if (offset
== 0 && len
== 0)
7365 return (get_remote_packet_size ());
7366 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
7367 large enough let the caller deal with it. */
7368 if (len
< get_remote_packet_size ())
7370 len
= get_remote_packet_size ();
7372 /* Except for querying the minimum buffer size, target must be open. */
7374 error (_("remote query is only available after target open"));
7376 gdb_assert (annex
!= NULL
);
7377 gdb_assert (readbuf
!= NULL
);
7383 /* We used one buffer char for the remote protocol q command and
7384 another for the query type. As the remote protocol encapsulation
7385 uses 4 chars plus one extra in case we are debugging
7386 (remote_debug), we have PBUFZIZ - 7 left to pack the query
7389 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
7391 /* Bad caller may have sent forbidden characters. */
7392 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
7397 gdb_assert (annex
[i
] == '\0');
7399 i
= putpkt (rs
->buf
);
7403 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7404 strcpy ((char *) readbuf
, rs
->buf
);
7406 return strlen ((char *) readbuf
);
7410 remote_search_memory (struct target_ops
* ops
,
7411 CORE_ADDR start_addr
, ULONGEST search_space_len
,
7412 const gdb_byte
*pattern
, ULONGEST pattern_len
,
7413 CORE_ADDR
*found_addrp
)
7415 struct remote_state
*rs
= get_remote_state ();
7416 int max_size
= get_memory_write_packet_size ();
7417 struct packet_config
*packet
=
7418 &remote_protocol_packets
[PACKET_qSearch_memory
];
7419 /* number of packet bytes used to encode the pattern,
7420 this could be more than PATTERN_LEN due to escape characters */
7421 int escaped_pattern_len
;
7422 /* amount of pattern that was encodable in the packet */
7423 int used_pattern_len
;
7426 ULONGEST found_addr
;
7428 /* Don't go to the target if we don't have to.
7429 This is done before checking packet->support to avoid the possibility that
7430 a success for this edge case means the facility works in general. */
7431 if (pattern_len
> search_space_len
)
7433 if (pattern_len
== 0)
7435 *found_addrp
= start_addr
;
7439 /* If we already know the packet isn't supported, fall back to the simple
7440 way of searching memory. */
7442 if (packet
->support
== PACKET_DISABLE
)
7444 /* Target doesn't provided special support, fall back and use the
7445 standard support (copy memory and do the search here). */
7446 return simple_search_memory (ops
, start_addr
, search_space_len
,
7447 pattern
, pattern_len
, found_addrp
);
7450 /* Insert header. */
7451 i
= snprintf (rs
->buf
, max_size
,
7452 "qSearch:memory:%s;%s;",
7453 paddr_nz (start_addr
),
7454 phex_nz (search_space_len
, sizeof (search_space_len
)));
7455 max_size
-= (i
+ 1);
7457 /* Escape as much data as fits into rs->buf. */
7458 escaped_pattern_len
=
7459 remote_escape_output (pattern
, pattern_len
, (rs
->buf
+ i
),
7460 &used_pattern_len
, max_size
);
7462 /* Bail if the pattern is too large. */
7463 if (used_pattern_len
!= pattern_len
)
7464 error ("Pattern is too large to transmit to remote target.");
7466 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
7467 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
7468 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7470 /* The request may not have worked because the command is not
7471 supported. If so, fall back to the simple way. */
7472 if (packet
->support
== PACKET_DISABLE
)
7474 return simple_search_memory (ops
, start_addr
, search_space_len
,
7475 pattern
, pattern_len
, found_addrp
);
7480 if (rs
->buf
[0] == '0')
7482 else if (rs
->buf
[0] == '1')
7485 if (rs
->buf
[1] != ',')
7486 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
7487 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
7488 *found_addrp
= found_addr
;
7491 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
7497 remote_rcmd (char *command
,
7498 struct ui_file
*outbuf
)
7500 struct remote_state
*rs
= get_remote_state ();
7504 error (_("remote rcmd is only available after target open"));
7506 /* Send a NULL command across as an empty command. */
7507 if (command
== NULL
)
7510 /* The query prefix. */
7511 strcpy (rs
->buf
, "qRcmd,");
7512 p
= strchr (rs
->buf
, '\0');
7514 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/) > get_remote_packet_size ())
7515 error (_("\"monitor\" command ``%s'' is too long."), command
);
7517 /* Encode the actual command. */
7518 bin2hex ((gdb_byte
*) command
, p
, 0);
7520 if (putpkt (rs
->buf
) < 0)
7521 error (_("Communication problem with target."));
7523 /* get/display the response */
7528 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
7530 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7533 error (_("Target does not support this command."));
7534 if (buf
[0] == 'O' && buf
[1] != 'K')
7536 remote_console_output (buf
+ 1); /* 'O' message from stub. */
7539 if (strcmp (buf
, "OK") == 0)
7541 if (strlen (buf
) == 3 && buf
[0] == 'E'
7542 && isdigit (buf
[1]) && isdigit (buf
[2]))
7544 error (_("Protocol error with Rcmd"));
7546 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
7548 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
7549 fputc_unfiltered (c
, outbuf
);
7555 static VEC(mem_region_s
) *
7556 remote_memory_map (struct target_ops
*ops
)
7558 VEC(mem_region_s
) *result
= NULL
;
7559 char *text
= target_read_stralloc (¤t_target
,
7560 TARGET_OBJECT_MEMORY_MAP
, NULL
);
7564 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
7565 result
= parse_memory_map (text
);
7566 do_cleanups (back_to
);
7573 packet_command (char *args
, int from_tty
)
7575 struct remote_state
*rs
= get_remote_state ();
7578 error (_("command can only be used with remote target"));
7581 error (_("remote-packet command requires packet text as argument"));
7583 puts_filtered ("sending: ");
7584 print_packet (args
);
7585 puts_filtered ("\n");
7588 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7589 puts_filtered ("received: ");
7590 print_packet (rs
->buf
);
7591 puts_filtered ("\n");
7595 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
7597 static void display_thread_info (struct gdb_ext_thread_info
*info
);
7599 static void threadset_test_cmd (char *cmd
, int tty
);
7601 static void threadalive_test (char *cmd
, int tty
);
7603 static void threadlist_test_cmd (char *cmd
, int tty
);
7605 int get_and_display_threadinfo (threadref
*ref
);
7607 static void threadinfo_test_cmd (char *cmd
, int tty
);
7609 static int thread_display_step (threadref
*ref
, void *context
);
7611 static void threadlist_update_test_cmd (char *cmd
, int tty
);
7613 static void init_remote_threadtests (void);
7615 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
7618 threadset_test_cmd (char *cmd
, int tty
)
7620 int sample_thread
= SAMPLE_THREAD
;
7622 printf_filtered (_("Remote threadset test\n"));
7623 set_general_thread (sample_thread
);
7628 threadalive_test (char *cmd
, int tty
)
7630 int sample_thread
= SAMPLE_THREAD
;
7631 int pid
= ptid_get_pid (inferior_ptid
);
7632 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
7634 if (remote_thread_alive (ptid
))
7635 printf_filtered ("PASS: Thread alive test\n");
7637 printf_filtered ("FAIL: Thread alive test\n");
7640 void output_threadid (char *title
, threadref
*ref
);
7643 output_threadid (char *title
, threadref
*ref
)
7647 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
7649 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
7653 threadlist_test_cmd (char *cmd
, int tty
)
7656 threadref nextthread
;
7657 int done
, result_count
;
7658 threadref threadlist
[3];
7660 printf_filtered ("Remote Threadlist test\n");
7661 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
7662 &result_count
, &threadlist
[0]))
7663 printf_filtered ("FAIL: threadlist test\n");
7666 threadref
*scan
= threadlist
;
7667 threadref
*limit
= scan
+ result_count
;
7669 while (scan
< limit
)
7670 output_threadid (" thread ", scan
++);
7675 display_thread_info (struct gdb_ext_thread_info
*info
)
7677 output_threadid ("Threadid: ", &info
->threadid
);
7678 printf_filtered ("Name: %s\n ", info
->shortname
);
7679 printf_filtered ("State: %s\n", info
->display
);
7680 printf_filtered ("other: %s\n\n", info
->more_display
);
7684 get_and_display_threadinfo (threadref
*ref
)
7688 struct gdb_ext_thread_info threadinfo
;
7690 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
7691 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
7692 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
7693 display_thread_info (&threadinfo
);
7698 threadinfo_test_cmd (char *cmd
, int tty
)
7700 int athread
= SAMPLE_THREAD
;
7704 int_to_threadref (&thread
, athread
);
7705 printf_filtered ("Remote Threadinfo test\n");
7706 if (!get_and_display_threadinfo (&thread
))
7707 printf_filtered ("FAIL cannot get thread info\n");
7711 thread_display_step (threadref
*ref
, void *context
)
7713 /* output_threadid(" threadstep ",ref); *//* simple test */
7714 return get_and_display_threadinfo (ref
);
7718 threadlist_update_test_cmd (char *cmd
, int tty
)
7720 printf_filtered ("Remote Threadlist update test\n");
7721 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
7725 init_remote_threadtests (void)
7727 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
7728 Fetch and print the remote list of thread identifiers, one pkt only"));
7729 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
7730 _("Fetch and display info about one thread"));
7731 add_com ("tset", class_obscure
, threadset_test_cmd
,
7732 _("Test setting to a different thread"));
7733 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
7734 _("Iterate through updating all remote thread info"));
7735 add_com ("talive", class_obscure
, threadalive_test
,
7736 _(" Remote thread alive test "));
7741 /* Convert a thread ID to a string. Returns the string in a static
7745 remote_pid_to_str (ptid_t ptid
)
7747 static char buf
[64];
7748 struct remote_state
*rs
= get_remote_state ();
7750 if (ptid_equal (magic_null_ptid
, ptid
))
7752 xsnprintf (buf
, sizeof buf
, "Thread <main>");
7755 else if (remote_multi_process_p (rs
)
7756 && ptid_get_tid (ptid
) != 0 && ptid_get_pid (ptid
) != 0)
7758 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
7759 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
7762 else if (ptid_get_tid (ptid
) != 0)
7764 xsnprintf (buf
, sizeof buf
, "Thread %ld",
7765 ptid_get_tid (ptid
));
7769 return normal_pid_to_str (ptid
);
7772 /* Get the address of the thread local variable in OBJFILE which is
7773 stored at OFFSET within the thread local storage for thread PTID. */
7776 remote_get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
7778 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
7780 struct remote_state
*rs
= get_remote_state ();
7782 char *endp
= rs
->buf
+ get_remote_packet_size ();
7783 enum packet_result result
;
7785 strcpy (p
, "qGetTLSAddr:");
7787 p
= write_ptid (p
, endp
, ptid
);
7789 p
+= hexnumstr (p
, offset
);
7791 p
+= hexnumstr (p
, lm
);
7795 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7796 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
7797 if (result
== PACKET_OK
)
7801 unpack_varlen_hex (rs
->buf
, &result
);
7804 else if (result
== PACKET_UNKNOWN
)
7805 throw_error (TLS_GENERIC_ERROR
,
7806 _("Remote target doesn't support qGetTLSAddr packet"));
7808 throw_error (TLS_GENERIC_ERROR
,
7809 _("Remote target failed to process qGetTLSAddr request"));
7812 throw_error (TLS_GENERIC_ERROR
,
7813 _("TLS not supported or disabled on this target"));
7818 /* Support for inferring a target description based on the current
7819 architecture and the size of a 'g' packet. While the 'g' packet
7820 can have any size (since optional registers can be left off the
7821 end), some sizes are easily recognizable given knowledge of the
7822 approximate architecture. */
7824 struct remote_g_packet_guess
7827 const struct target_desc
*tdesc
;
7829 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
7830 DEF_VEC_O(remote_g_packet_guess_s
);
7832 struct remote_g_packet_data
7834 VEC(remote_g_packet_guess_s
) *guesses
;
7837 static struct gdbarch_data
*remote_g_packet_data_handle
;
7840 remote_g_packet_data_init (struct obstack
*obstack
)
7842 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
7846 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
7847 const struct target_desc
*tdesc
)
7849 struct remote_g_packet_data
*data
7850 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
7851 struct remote_g_packet_guess new_guess
, *guess
;
7854 gdb_assert (tdesc
!= NULL
);
7857 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
7859 if (guess
->bytes
== bytes
)
7860 internal_error (__FILE__
, __LINE__
,
7861 "Duplicate g packet description added for size %d",
7864 new_guess
.bytes
= bytes
;
7865 new_guess
.tdesc
= tdesc
;
7866 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
7869 /* Return 1 if remote_read_description would do anything on this target
7870 and architecture, 0 otherwise. */
7873 remote_read_description_p (struct target_ops
*target
)
7875 struct remote_g_packet_data
*data
7876 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
7878 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
7884 static const struct target_desc
*
7885 remote_read_description (struct target_ops
*target
)
7887 struct remote_g_packet_data
*data
7888 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
7890 /* Do not try this during initial connection, when we do not know
7891 whether there is a running but stopped thread. */
7892 if (!target_has_execution
|| ptid_equal (inferior_ptid
, null_ptid
))
7895 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
7897 struct remote_g_packet_guess
*guess
;
7899 int bytes
= send_g_packet ();
7902 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
7904 if (guess
->bytes
== bytes
)
7905 return guess
->tdesc
;
7907 /* We discard the g packet. A minor optimization would be to
7908 hold on to it, and fill the register cache once we have selected
7909 an architecture, but it's too tricky to do safely. */
7915 /* Remote file transfer support. This is host-initiated I/O, not
7916 target-initiated; for target-initiated, see remote-fileio.c. */
7918 /* If *LEFT is at least the length of STRING, copy STRING to
7919 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7920 decrease *LEFT. Otherwise raise an error. */
7923 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
7925 int len
= strlen (string
);
7928 error (_("Packet too long for target."));
7930 memcpy (*buffer
, string
, len
);
7934 /* NUL-terminate the buffer as a convenience, if there is
7940 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
7941 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7942 decrease *LEFT. Otherwise raise an error. */
7945 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
7948 if (2 * len
> *left
)
7949 error (_("Packet too long for target."));
7951 bin2hex (bytes
, *buffer
, len
);
7955 /* NUL-terminate the buffer as a convenience, if there is
7961 /* If *LEFT is large enough, convert VALUE to hex and add it to
7962 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7963 decrease *LEFT. Otherwise raise an error. */
7966 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
7968 int len
= hexnumlen (value
);
7971 error (_("Packet too long for target."));
7973 hexnumstr (*buffer
, value
);
7977 /* NUL-terminate the buffer as a convenience, if there is
7983 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
7984 value, *REMOTE_ERRNO to the remote error number or zero if none
7985 was included, and *ATTACHMENT to point to the start of the annex
7986 if any. The length of the packet isn't needed here; there may
7987 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
7989 Return 0 if the packet could be parsed, -1 if it could not. If
7990 -1 is returned, the other variables may not be initialized. */
7993 remote_hostio_parse_result (char *buffer
, int *retcode
,
7994 int *remote_errno
, char **attachment
)
8001 if (buffer
[0] != 'F')
8005 *retcode
= strtol (&buffer
[1], &p
, 16);
8006 if (errno
!= 0 || p
== &buffer
[1])
8009 /* Check for ",errno". */
8013 *remote_errno
= strtol (p
+ 1, &p2
, 16);
8014 if (errno
!= 0 || p
+ 1 == p2
)
8019 /* Check for ";attachment". If there is no attachment, the
8020 packet should end here. */
8023 *attachment
= p
+ 1;
8026 else if (*p
== '\0')
8032 /* Send a prepared I/O packet to the target and read its response.
8033 The prepared packet is in the global RS->BUF before this function
8034 is called, and the answer is there when we return.
8036 COMMAND_BYTES is the length of the request to send, which may include
8037 binary data. WHICH_PACKET is the packet configuration to check
8038 before attempting a packet. If an error occurs, *REMOTE_ERRNO
8039 is set to the error number and -1 is returned. Otherwise the value
8040 returned by the function is returned.
8042 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
8043 attachment is expected; an error will be reported if there's a
8044 mismatch. If one is found, *ATTACHMENT will be set to point into
8045 the packet buffer and *ATTACHMENT_LEN will be set to the
8046 attachment's length. */
8049 remote_hostio_send_command (int command_bytes
, int which_packet
,
8050 int *remote_errno
, char **attachment
,
8051 int *attachment_len
)
8053 struct remote_state
*rs
= get_remote_state ();
8054 int ret
, bytes_read
;
8055 char *attachment_tmp
;
8058 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
8060 *remote_errno
= FILEIO_ENOSYS
;
8064 putpkt_binary (rs
->buf
, command_bytes
);
8065 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
8067 /* If it timed out, something is wrong. Don't try to parse the
8071 *remote_errno
= FILEIO_EINVAL
;
8075 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
8078 *remote_errno
= FILEIO_EINVAL
;
8080 case PACKET_UNKNOWN
:
8081 *remote_errno
= FILEIO_ENOSYS
;
8087 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
8090 *remote_errno
= FILEIO_EINVAL
;
8094 /* Make sure we saw an attachment if and only if we expected one. */
8095 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
8096 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
8098 *remote_errno
= FILEIO_EINVAL
;
8102 /* If an attachment was found, it must point into the packet buffer;
8103 work out how many bytes there were. */
8104 if (attachment_tmp
!= NULL
)
8106 *attachment
= attachment_tmp
;
8107 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
8113 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
8114 remote file descriptor, or -1 if an error occurs (and set
8118 remote_hostio_open (const char *filename
, int flags
, int mode
,
8121 struct remote_state
*rs
= get_remote_state ();
8123 int left
= get_remote_packet_size () - 1;
8125 remote_buffer_add_string (&p
, &left
, "vFile:open:");
8127 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
8129 remote_buffer_add_string (&p
, &left
, ",");
8131 remote_buffer_add_int (&p
, &left
, flags
);
8132 remote_buffer_add_string (&p
, &left
, ",");
8134 remote_buffer_add_int (&p
, &left
, mode
);
8136 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
8137 remote_errno
, NULL
, NULL
);
8140 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
8141 Return the number of bytes written, or -1 if an error occurs (and
8142 set *REMOTE_ERRNO). */
8145 remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
8146 ULONGEST offset
, int *remote_errno
)
8148 struct remote_state
*rs
= get_remote_state ();
8150 int left
= get_remote_packet_size ();
8153 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
8155 remote_buffer_add_int (&p
, &left
, fd
);
8156 remote_buffer_add_string (&p
, &left
, ",");
8158 remote_buffer_add_int (&p
, &left
, offset
);
8159 remote_buffer_add_string (&p
, &left
, ",");
8161 p
+= remote_escape_output (write_buf
, len
, p
, &out_len
,
8162 get_remote_packet_size () - (p
- rs
->buf
));
8164 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
8165 remote_errno
, NULL
, NULL
);
8168 /* Read up to LEN bytes FD on the remote target into READ_BUF
8169 Return the number of bytes read, or -1 if an error occurs (and
8170 set *REMOTE_ERRNO). */
8173 remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
8174 ULONGEST offset
, int *remote_errno
)
8176 struct remote_state
*rs
= get_remote_state ();
8179 int left
= get_remote_packet_size ();
8180 int ret
, attachment_len
;
8183 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
8185 remote_buffer_add_int (&p
, &left
, fd
);
8186 remote_buffer_add_string (&p
, &left
, ",");
8188 remote_buffer_add_int (&p
, &left
, len
);
8189 remote_buffer_add_string (&p
, &left
, ",");
8191 remote_buffer_add_int (&p
, &left
, offset
);
8193 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
8194 remote_errno
, &attachment
,
8200 read_len
= remote_unescape_input (attachment
, attachment_len
,
8202 if (read_len
!= ret
)
8203 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
8208 /* Close FD on the remote target. Return 0, or -1 if an error occurs
8209 (and set *REMOTE_ERRNO). */
8212 remote_hostio_close (int fd
, int *remote_errno
)
8214 struct remote_state
*rs
= get_remote_state ();
8216 int left
= get_remote_packet_size () - 1;
8218 remote_buffer_add_string (&p
, &left
, "vFile:close:");
8220 remote_buffer_add_int (&p
, &left
, fd
);
8222 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
8223 remote_errno
, NULL
, NULL
);
8226 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
8227 occurs (and set *REMOTE_ERRNO). */
8230 remote_hostio_unlink (const char *filename
, int *remote_errno
)
8232 struct remote_state
*rs
= get_remote_state ();
8234 int left
= get_remote_packet_size () - 1;
8236 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
8238 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
8241 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
8242 remote_errno
, NULL
, NULL
);
8246 remote_fileio_errno_to_host (int errnum
)
8270 case FILEIO_ENOTDIR
:
8290 case FILEIO_ENAMETOOLONG
:
8291 return ENAMETOOLONG
;
8297 remote_hostio_error (int errnum
)
8299 int host_error
= remote_fileio_errno_to_host (errnum
);
8301 if (host_error
== -1)
8302 error (_("Unknown remote I/O error %d"), errnum
);
8304 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
8308 remote_hostio_close_cleanup (void *opaque
)
8310 int fd
= *(int *) opaque
;
8313 remote_hostio_close (fd
, &remote_errno
);
8318 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
8320 const char *filename
= bfd_get_filename (abfd
);
8321 int fd
, remote_errno
;
8324 gdb_assert (remote_filename_p (filename
));
8326 fd
= remote_hostio_open (filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
8329 errno
= remote_fileio_errno_to_host (remote_errno
);
8330 bfd_set_error (bfd_error_system_call
);
8334 stream
= xmalloc (sizeof (int));
8340 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
8342 int fd
= *(int *)stream
;
8347 /* Ignore errors on close; these may happen if the remote
8348 connection was already torn down. */
8349 remote_hostio_close (fd
, &remote_errno
);
8355 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
8356 file_ptr nbytes
, file_ptr offset
)
8358 int fd
= *(int *)stream
;
8360 file_ptr pos
, bytes
;
8363 while (nbytes
> pos
)
8365 bytes
= remote_hostio_pread (fd
, (char *)buf
+ pos
, nbytes
- pos
,
8366 offset
+ pos
, &remote_errno
);
8368 /* Success, but no bytes, means end-of-file. */
8372 errno
= remote_fileio_errno_to_host (remote_errno
);
8373 bfd_set_error (bfd_error_system_call
);
8384 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
8386 /* FIXME: We should probably implement remote_hostio_stat. */
8387 sb
->st_size
= INT_MAX
;
8392 remote_filename_p (const char *filename
)
8394 return strncmp (filename
, "remote:", 7) == 0;
8398 remote_bfd_open (const char *remote_file
, const char *target
)
8400 return bfd_openr_iovec (remote_file
, target
,
8401 remote_bfd_iovec_open
, NULL
,
8402 remote_bfd_iovec_pread
,
8403 remote_bfd_iovec_close
,
8404 remote_bfd_iovec_stat
);
8408 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
8410 struct cleanup
*back_to
, *close_cleanup
;
8411 int retcode
, fd
, remote_errno
, bytes
, io_size
;
8414 int bytes_in_buffer
;
8419 error (_("command can only be used with remote target"));
8421 file
= fopen (local_file
, "rb");
8423 perror_with_name (local_file
);
8424 back_to
= make_cleanup_fclose (file
);
8426 fd
= remote_hostio_open (remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
8428 0700, &remote_errno
);
8430 remote_hostio_error (remote_errno
);
8432 /* Send up to this many bytes at once. They won't all fit in the
8433 remote packet limit, so we'll transfer slightly fewer. */
8434 io_size
= get_remote_packet_size ();
8435 buffer
= xmalloc (io_size
);
8436 make_cleanup (xfree
, buffer
);
8438 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
8440 bytes_in_buffer
= 0;
8443 while (bytes_in_buffer
|| !saw_eof
)
8447 bytes
= fread (buffer
+ bytes_in_buffer
, 1, io_size
- bytes_in_buffer
,
8452 error (_("Error reading %s."), local_file
);
8455 /* EOF. Unless there is something still in the
8456 buffer from the last iteration, we are done. */
8458 if (bytes_in_buffer
== 0)
8466 bytes
+= bytes_in_buffer
;
8467 bytes_in_buffer
= 0;
8469 retcode
= remote_hostio_pwrite (fd
, buffer
, bytes
, offset
, &remote_errno
);
8472 remote_hostio_error (remote_errno
);
8473 else if (retcode
== 0)
8474 error (_("Remote write of %d bytes returned 0!"), bytes
);
8475 else if (retcode
< bytes
)
8477 /* Short write. Save the rest of the read data for the next
8479 bytes_in_buffer
= bytes
- retcode
;
8480 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
8486 discard_cleanups (close_cleanup
);
8487 if (remote_hostio_close (fd
, &remote_errno
))
8488 remote_hostio_error (remote_errno
);
8491 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
8492 do_cleanups (back_to
);
8496 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
8498 struct cleanup
*back_to
, *close_cleanup
;
8499 int retcode
, fd
, remote_errno
, bytes
, io_size
;
8505 error (_("command can only be used with remote target"));
8507 fd
= remote_hostio_open (remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
8509 remote_hostio_error (remote_errno
);
8511 file
= fopen (local_file
, "wb");
8513 perror_with_name (local_file
);
8514 back_to
= make_cleanup_fclose (file
);
8516 /* Send up to this many bytes at once. They won't all fit in the
8517 remote packet limit, so we'll transfer slightly fewer. */
8518 io_size
= get_remote_packet_size ();
8519 buffer
= xmalloc (io_size
);
8520 make_cleanup (xfree
, buffer
);
8522 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
8527 bytes
= remote_hostio_pread (fd
, buffer
, io_size
, offset
, &remote_errno
);
8529 /* Success, but no bytes, means end-of-file. */
8532 remote_hostio_error (remote_errno
);
8536 bytes
= fwrite (buffer
, 1, bytes
, file
);
8538 perror_with_name (local_file
);
8541 discard_cleanups (close_cleanup
);
8542 if (remote_hostio_close (fd
, &remote_errno
))
8543 remote_hostio_error (remote_errno
);
8546 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
8547 do_cleanups (back_to
);
8551 remote_file_delete (const char *remote_file
, int from_tty
)
8553 int retcode
, remote_errno
;
8556 error (_("command can only be used with remote target"));
8558 retcode
= remote_hostio_unlink (remote_file
, &remote_errno
);
8560 remote_hostio_error (remote_errno
);
8563 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
8567 remote_put_command (char *args
, int from_tty
)
8569 struct cleanup
*back_to
;
8573 error_no_arg (_("file to put"));
8575 argv
= gdb_buildargv (args
);
8576 back_to
= make_cleanup_freeargv (argv
);
8577 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
8578 error (_("Invalid parameters to remote put"));
8580 remote_file_put (argv
[0], argv
[1], from_tty
);
8582 do_cleanups (back_to
);
8586 remote_get_command (char *args
, int from_tty
)
8588 struct cleanup
*back_to
;
8592 error_no_arg (_("file to get"));
8594 argv
= gdb_buildargv (args
);
8595 back_to
= make_cleanup_freeargv (argv
);
8596 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
8597 error (_("Invalid parameters to remote get"));
8599 remote_file_get (argv
[0], argv
[1], from_tty
);
8601 do_cleanups (back_to
);
8605 remote_delete_command (char *args
, int from_tty
)
8607 struct cleanup
*back_to
;
8611 error_no_arg (_("file to delete"));
8613 argv
= gdb_buildargv (args
);
8614 back_to
= make_cleanup_freeargv (argv
);
8615 if (argv
[0] == NULL
|| argv
[1] != NULL
)
8616 error (_("Invalid parameters to remote delete"));
8618 remote_file_delete (argv
[0], from_tty
);
8620 do_cleanups (back_to
);
8624 remote_command (char *args
, int from_tty
)
8626 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
8629 static int remote_target_can_reverse
= 1;
8632 remote_can_execute_reverse (void)
8634 return remote_target_can_reverse
;
8638 remote_supports_non_stop (void)
8644 remote_supports_multi_process (void)
8646 struct remote_state
*rs
= get_remote_state ();
8647 return remote_multi_process_p (rs
);
8651 init_remote_ops (void)
8653 remote_ops
.to_shortname
= "remote";
8654 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
8656 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
8657 Specify the serial device it is connected to\n\
8658 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
8659 remote_ops
.to_open
= remote_open
;
8660 remote_ops
.to_close
= remote_close
;
8661 remote_ops
.to_detach
= remote_detach
;
8662 remote_ops
.to_disconnect
= remote_disconnect
;
8663 remote_ops
.to_resume
= remote_resume
;
8664 remote_ops
.to_wait
= remote_wait
;
8665 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
8666 remote_ops
.to_store_registers
= remote_store_registers
;
8667 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
8668 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
8669 remote_ops
.to_files_info
= remote_files_info
;
8670 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
8671 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
8672 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
8673 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
8674 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
8675 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
8676 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
8677 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
8678 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
8679 remote_ops
.to_kill
= remote_kill
;
8680 remote_ops
.to_load
= generic_load
;
8681 remote_ops
.to_mourn_inferior
= remote_mourn
;
8682 remote_ops
.to_thread_alive
= remote_thread_alive
;
8683 remote_ops
.to_find_new_threads
= remote_threads_info
;
8684 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
8685 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
8686 remote_ops
.to_stop
= remote_stop
;
8687 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
8688 remote_ops
.to_rcmd
= remote_rcmd
;
8689 remote_ops
.to_log_command
= serial_log_command
;
8690 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
8691 remote_ops
.to_stratum
= process_stratum
;
8692 remote_ops
.to_has_all_memory
= 1;
8693 remote_ops
.to_has_memory
= 1;
8694 remote_ops
.to_has_stack
= 1;
8695 remote_ops
.to_has_registers
= 1;
8696 remote_ops
.to_has_execution
= 1;
8697 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
8698 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
8699 remote_ops
.to_magic
= OPS_MAGIC
;
8700 remote_ops
.to_memory_map
= remote_memory_map
;
8701 remote_ops
.to_flash_erase
= remote_flash_erase
;
8702 remote_ops
.to_flash_done
= remote_flash_done
;
8703 remote_ops
.to_read_description
= remote_read_description
;
8704 remote_ops
.to_search_memory
= remote_search_memory
;
8705 remote_ops
.to_can_async_p
= remote_can_async_p
;
8706 remote_ops
.to_is_async_p
= remote_is_async_p
;
8707 remote_ops
.to_async
= remote_async
;
8708 remote_ops
.to_async_mask
= remote_async_mask
;
8709 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
8710 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
8711 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
8712 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
8715 /* Set up the extended remote vector by making a copy of the standard
8716 remote vector and adding to it. */
8719 init_extended_remote_ops (void)
8721 extended_remote_ops
= remote_ops
;
8723 extended_remote_ops
.to_shortname
= "extended-remote";
8724 extended_remote_ops
.to_longname
=
8725 "Extended remote serial target in gdb-specific protocol";
8726 extended_remote_ops
.to_doc
=
8727 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
8728 Specify the serial device it is connected to (e.g. /dev/ttya).";
8729 extended_remote_ops
.to_open
= extended_remote_open
;
8730 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
8731 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
8732 extended_remote_ops
.to_detach
= extended_remote_detach
;
8733 extended_remote_ops
.to_attach
= extended_remote_attach
;
8734 extended_remote_ops
.to_kill
= extended_remote_kill
;
8738 remote_can_async_p (void)
8740 if (!target_async_permitted
)
8741 /* We only enable async when the user specifically asks for it. */
8744 /* We're async whenever the serial device is. */
8745 return remote_async_mask_value
&& serial_can_async_p (remote_desc
);
8749 remote_is_async_p (void)
8751 if (!target_async_permitted
)
8752 /* We only enable async when the user specifically asks for it. */
8755 /* We're async whenever the serial device is. */
8756 return remote_async_mask_value
&& serial_is_async_p (remote_desc
);
8759 /* Pass the SERIAL event on and up to the client. One day this code
8760 will be able to delay notifying the client of an event until the
8761 point where an entire packet has been received. */
8763 static void (*async_client_callback
) (enum inferior_event_type event_type
,
8765 static void *async_client_context
;
8766 static serial_event_ftype remote_async_serial_handler
;
8769 remote_async_serial_handler (struct serial
*scb
, void *context
)
8771 /* Don't propogate error information up to the client. Instead let
8772 the client find out about the error by querying the target. */
8773 async_client_callback (INF_REG_EVENT
, async_client_context
);
8777 remote_async_inferior_event_handler (gdb_client_data data
)
8779 inferior_event_handler (INF_REG_EVENT
, NULL
);
8783 remote_async_get_pending_events_handler (gdb_client_data data
)
8785 remote_get_pending_stop_replies ();
8789 remote_async (void (*callback
) (enum inferior_event_type event_type
,
8790 void *context
), void *context
)
8792 if (remote_async_mask_value
== 0)
8793 internal_error (__FILE__
, __LINE__
,
8794 _("Calling remote_async when async is masked"));
8796 if (callback
!= NULL
)
8798 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
8799 async_client_callback
= callback
;
8800 async_client_context
= context
;
8803 serial_async (remote_desc
, NULL
, NULL
);
8807 remote_async_mask (int new_mask
)
8809 int curr_mask
= remote_async_mask_value
;
8810 remote_async_mask_value
= new_mask
;
8815 set_remote_cmd (char *args
, int from_tty
)
8817 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
8821 show_remote_cmd (char *args
, int from_tty
)
8823 /* We can't just use cmd_show_list here, because we want to skip
8824 the redundant "show remote Z-packet" and the legacy aliases. */
8825 struct cleanup
*showlist_chain
;
8826 struct cmd_list_element
*list
= remote_show_cmdlist
;
8828 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
8829 for (; list
!= NULL
; list
= list
->next
)
8830 if (strcmp (list
->name
, "Z-packet") == 0)
8832 else if (list
->type
== not_set_cmd
)
8833 /* Alias commands are exactly like the original, except they
8834 don't have the normal type. */
8838 struct cleanup
*option_chain
8839 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
8840 ui_out_field_string (uiout
, "name", list
->name
);
8841 ui_out_text (uiout
, ": ");
8842 if (list
->type
== show_cmd
)
8843 do_setshow_command ((char *) NULL
, from_tty
, list
);
8845 cmd_func (list
, NULL
, from_tty
);
8846 /* Close the tuple. */
8847 do_cleanups (option_chain
);
8850 /* Close the tuple. */
8851 do_cleanups (showlist_chain
);
8855 /* Function to be called whenever a new objfile (shlib) is detected. */
8857 remote_new_objfile (struct objfile
*objfile
)
8859 if (remote_desc
!= 0) /* Have a remote connection. */
8860 remote_check_symbols (objfile
);
8864 _initialize_remote (void)
8866 struct remote_state
*rs
;
8868 /* architecture specific data */
8869 remote_gdbarch_data_handle
=
8870 gdbarch_data_register_post_init (init_remote_state
);
8871 remote_g_packet_data_handle
=
8872 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
8874 /* Initialize the per-target state. At the moment there is only one
8875 of these, not one per target. Only one target is active at a
8876 time. The default buffer size is unimportant; it will be expanded
8877 whenever a larger buffer is needed. */
8878 rs
= get_remote_state_raw ();
8880 rs
->buf
= xmalloc (rs
->buf_size
);
8883 add_target (&remote_ops
);
8885 init_extended_remote_ops ();
8886 add_target (&extended_remote_ops
);
8888 /* Hook into new objfile notification. */
8889 observer_attach_new_objfile (remote_new_objfile
);
8891 /* Set up signal handlers. */
8892 sigint_remote_token
=
8893 create_async_signal_handler (async_remote_interrupt
, NULL
);
8894 sigint_remote_twice_token
=
8895 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
8898 init_remote_threadtests ();
8901 /* set/show remote ... */
8903 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
8904 Remote protocol specific variables\n\
8905 Configure various remote-protocol specific variables such as\n\
8906 the packets being used"),
8907 &remote_set_cmdlist
, "set remote ",
8908 0 /* allow-unknown */, &setlist
);
8909 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
8910 Remote protocol specific variables\n\
8911 Configure various remote-protocol specific variables such as\n\
8912 the packets being used"),
8913 &remote_show_cmdlist
, "show remote ",
8914 0 /* allow-unknown */, &showlist
);
8916 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
8917 Compare section data on target to the exec file.\n\
8918 Argument is a single section name (default: all loaded sections)."),
8921 add_cmd ("packet", class_maintenance
, packet_command
, _("\
8922 Send an arbitrary packet to a remote target.\n\
8923 maintenance packet TEXT\n\
8924 If GDB is talking to an inferior via the GDB serial protocol, then\n\
8925 this command sends the string TEXT to the inferior, and displays the\n\
8926 response packet. GDB supplies the initial `$' character, and the\n\
8927 terminating `#' character and checksum."),
8930 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
8931 Set whether to send break if interrupted."), _("\
8932 Show whether to send break if interrupted."), _("\
8933 If set, a break, instead of a cntrl-c, is sent to the remote target."),
8934 NULL
, NULL
, /* FIXME: i18n: Whether to send break if interrupted is %s. */
8935 &setlist
, &showlist
);
8937 /* Install commands for configuring memory read/write packets. */
8939 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
8940 Set the maximum number of bytes per memory write packet (deprecated)."),
8942 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
8943 Show the maximum number of bytes per memory write packet (deprecated)."),
8945 add_cmd ("memory-write-packet-size", no_class
,
8946 set_memory_write_packet_size
, _("\
8947 Set the maximum number of bytes per memory-write packet.\n\
8948 Specify the number of bytes in a packet or 0 (zero) for the\n\
8949 default packet size. The actual limit is further reduced\n\
8950 dependent on the target. Specify ``fixed'' to disable the\n\
8951 further restriction and ``limit'' to enable that restriction."),
8952 &remote_set_cmdlist
);
8953 add_cmd ("memory-read-packet-size", no_class
,
8954 set_memory_read_packet_size
, _("\
8955 Set the maximum number of bytes per memory-read packet.\n\
8956 Specify the number of bytes in a packet or 0 (zero) for the\n\
8957 default packet size. The actual limit is further reduced\n\
8958 dependent on the target. Specify ``fixed'' to disable the\n\
8959 further restriction and ``limit'' to enable that restriction."),
8960 &remote_set_cmdlist
);
8961 add_cmd ("memory-write-packet-size", no_class
,
8962 show_memory_write_packet_size
,
8963 _("Show the maximum number of bytes per memory-write packet."),
8964 &remote_show_cmdlist
);
8965 add_cmd ("memory-read-packet-size", no_class
,
8966 show_memory_read_packet_size
,
8967 _("Show the maximum number of bytes per memory-read packet."),
8968 &remote_show_cmdlist
);
8970 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
8971 &remote_hw_watchpoint_limit
, _("\
8972 Set the maximum number of target hardware watchpoints."), _("\
8973 Show the maximum number of target hardware watchpoints."), _("\
8974 Specify a negative limit for unlimited."),
8975 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
8976 &remote_set_cmdlist
, &remote_show_cmdlist
);
8977 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
8978 &remote_hw_breakpoint_limit
, _("\
8979 Set the maximum number of target hardware breakpoints."), _("\
8980 Show the maximum number of target hardware breakpoints."), _("\
8981 Specify a negative limit for unlimited."),
8982 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
8983 &remote_set_cmdlist
, &remote_show_cmdlist
);
8985 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
8986 &remote_address_size
, _("\
8987 Set the maximum size of the address (in bits) in a memory packet."), _("\
8988 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
8990 NULL
, /* FIXME: i18n: */
8991 &setlist
, &showlist
);
8993 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
8994 "X", "binary-download", 1);
8996 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
8997 "vCont", "verbose-resume", 0);
8999 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
9000 "QPassSignals", "pass-signals", 0);
9002 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
9003 "qSymbol", "symbol-lookup", 0);
9005 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
9006 "P", "set-register", 1);
9008 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
9009 "p", "fetch-register", 1);
9011 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
9012 "Z0", "software-breakpoint", 0);
9014 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
9015 "Z1", "hardware-breakpoint", 0);
9017 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
9018 "Z2", "write-watchpoint", 0);
9020 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
9021 "Z3", "read-watchpoint", 0);
9023 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
9024 "Z4", "access-watchpoint", 0);
9026 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
9027 "qXfer:auxv:read", "read-aux-vector", 0);
9029 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
9030 "qXfer:features:read", "target-features", 0);
9032 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
9033 "qXfer:libraries:read", "library-info", 0);
9035 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
9036 "qXfer:memory-map:read", "memory-map", 0);
9038 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
9039 "qXfer:spu:read", "read-spu-object", 0);
9041 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
9042 "qXfer:spu:write", "write-spu-object", 0);
9044 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
9045 "qGetTLSAddr", "get-thread-local-storage-address",
9048 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
9049 "qSupported", "supported-packets", 0);
9051 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
9052 "qSearch:memory", "search-memory", 0);
9054 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
9055 "vFile:open", "hostio-open", 0);
9057 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
9058 "vFile:pread", "hostio-pread", 0);
9060 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
9061 "vFile:pwrite", "hostio-pwrite", 0);
9063 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
9064 "vFile:close", "hostio-close", 0);
9066 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
9067 "vFile:unlink", "hostio-unlink", 0);
9069 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
9070 "vAttach", "attach", 0);
9072 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
9075 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
9076 "QStartNoAckMode", "noack", 0);
9078 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
9079 "vKill", "kill", 0);
9081 /* Keep the old ``set remote Z-packet ...'' working. Each individual
9082 Z sub-packet has its own set and show commands, but users may
9083 have sets to this variable in their .gdbinit files (or in their
9085 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
9086 &remote_Z_packet_detect
, _("\
9087 Set use of remote protocol `Z' packets"), _("\
9088 Show use of remote protocol `Z' packets "), _("\
9089 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
9091 set_remote_protocol_Z_packet_cmd
,
9092 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
9093 &remote_set_cmdlist
, &remote_show_cmdlist
);
9095 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
9096 Manipulate files on the remote system\n\
9097 Transfer files to and from the remote target system."),
9098 &remote_cmdlist
, "remote ",
9099 0 /* allow-unknown */, &cmdlist
);
9101 add_cmd ("put", class_files
, remote_put_command
,
9102 _("Copy a local file to the remote system."),
9105 add_cmd ("get", class_files
, remote_get_command
,
9106 _("Copy a remote file to the local system."),
9109 add_cmd ("delete", class_files
, remote_delete_command
,
9110 _("Delete a remote file."),
9113 remote_exec_file
= xstrdup ("");
9114 add_setshow_string_noescape_cmd ("exec-file", class_files
,
9115 &remote_exec_file
, _("\
9116 Set the remote pathname for \"run\""), _("\
9117 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
9118 &remote_set_cmdlist
, &remote_show_cmdlist
);
9120 /* Eventually initialize fileio. See fileio.c */
9121 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
9123 /* Take advantage of the fact that the LWP field is not used, to tag
9124 special ptids with it set to != 0. */
9125 magic_null_ptid
= ptid_build (42000, 1, -1);
9126 not_sent_ptid
= ptid_build (42000, 1, -2);
9127 any_thread_ptid
= ptid_build (42000, 1, 0);