1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
31 #include "exceptions.h"
33 /*#include "terminal.h" */
36 #include "gdb-stabs.h"
37 #include "gdbthread.h"
41 #include "gdb_assert.h"
44 #include "cli/cli-decode.h"
45 #include "cli/cli-setshow.h"
46 #include "target-descriptions.h"
51 #include "event-loop.h"
52 #include "event-top.h"
58 #include "gdbcore.h" /* for exec_bfd */
60 #include "remote-fileio.h"
61 #include "gdb/fileio.h"
64 #include "memory-map.h"
66 /* The size to align memory write packets, when practical. The protocol
67 does not guarantee any alignment, and gdb will generate short
68 writes and unaligned writes, but even as a best-effort attempt this
69 can improve bulk transfers. For instance, if a write is misaligned
70 relative to the target's data bus, the stub may need to make an extra
71 round trip fetching data from the target. This doesn't make a
72 huge difference, but it's easy to do, so we try to be helpful.
74 The alignment chosen is arbitrary; usually data bus width is
75 important here, not the possibly larger cache line size. */
76 enum { REMOTE_ALIGN_WRITES
= 16 };
78 /* Prototypes for local functions. */
79 static void cleanup_sigint_signal_handler (void *dummy
);
80 static void initialize_sigint_signal_handler (void);
81 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
82 static int getpkt_or_notif_sane (char **buf
, long *sizeof_buf
,
85 static void handle_remote_sigint (int);
86 static void handle_remote_sigint_twice (int);
87 static void async_remote_interrupt (gdb_client_data
);
88 void async_remote_interrupt_twice (gdb_client_data
);
90 static void remote_files_info (struct target_ops
*ignore
);
92 static void remote_prepare_to_store (struct regcache
*regcache
);
94 static void remote_fetch_registers (struct regcache
*regcache
, int regno
);
96 static void remote_resume (ptid_t ptid
, int step
,
97 enum target_signal siggnal
);
98 static void remote_open (char *name
, int from_tty
);
100 static void extended_remote_open (char *name
, int from_tty
);
102 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
);
104 static void remote_close (int quitting
);
106 static void remote_store_registers (struct regcache
*regcache
, int regno
);
108 static void remote_mourn (struct target_ops
*ops
);
110 static void extended_remote_restart (void);
112 static void extended_remote_mourn (struct target_ops
*);
114 static void remote_mourn_1 (struct target_ops
*);
116 static void remote_send (char **buf
, long *sizeof_buf_p
);
118 static int readchar (int timeout
);
120 static 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
,
999 PACKET_qSearch_memory
,
1002 PACKET_QStartNoAckMode
,
1007 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
1010 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
1011 struct cmd_list_element
*c
)
1013 struct packet_config
*packet
;
1015 for (packet
= remote_protocol_packets
;
1016 packet
< &remote_protocol_packets
[PACKET_MAX
];
1019 if (&packet
->detect
== c
->var
)
1021 update_packet_config (packet
);
1025 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1030 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
1031 struct cmd_list_element
*c
,
1034 struct packet_config
*packet
;
1036 for (packet
= remote_protocol_packets
;
1037 packet
< &remote_protocol_packets
[PACKET_MAX
];
1040 if (&packet
->detect
== c
->var
)
1042 show_packet_config_cmd (packet
);
1046 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1050 /* Should we try one of the 'Z' requests? */
1054 Z_PACKET_SOFTWARE_BP
,
1055 Z_PACKET_HARDWARE_BP
,
1062 /* For compatibility with older distributions. Provide a ``set remote
1063 Z-packet ...'' command that updates all the Z packet types. */
1065 static enum auto_boolean remote_Z_packet_detect
;
1068 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1069 struct cmd_list_element
*c
)
1072 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1074 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1075 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1080 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1081 struct cmd_list_element
*c
,
1085 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1087 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1091 /* Should we try the 'ThreadInfo' query packet?
1093 This variable (NOT available to the user: auto-detect only!)
1094 determines whether GDB will use the new, simpler "ThreadInfo"
1095 query or the older, more complex syntax for thread queries.
1096 This is an auto-detect variable (set to true at each connect,
1097 and set to false when the target fails to recognize it). */
1099 static int use_threadinfo_query
;
1100 static int use_threadextra_query
;
1102 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1103 static struct async_signal_handler
*sigint_remote_twice_token
;
1104 static struct async_signal_handler
*sigint_remote_token
;
1107 /* Asynchronous signal handle registered as event loop source for
1108 when we have pending events ready to be passed to the core. */
1110 static struct async_event_handler
*remote_async_inferior_event_token
;
1112 /* Asynchronous signal handle registered as event loop source for when
1113 the remote sent us a %Stop notification. The registered callback
1114 will do a vStopped sequence to pull the rest of the events out of
1115 the remote side into our event queue. */
1117 static struct async_event_handler
*remote_async_get_pending_events_token
;
1120 static ptid_t magic_null_ptid
;
1121 static ptid_t not_sent_ptid
;
1122 static ptid_t any_thread_ptid
;
1124 /* These are the threads which we last sent to the remote system. The
1125 TID member will be -1 for all or -2 for not sent yet. */
1127 static ptid_t general_thread
;
1128 static ptid_t continue_thread
;
1131 notice_new_inferiors (ptid_t currthread
)
1133 /* If this is a new thread, add it to GDB's thread list.
1134 If we leave it up to WFI to do this, bad things will happen. */
1136 if (in_thread_list (currthread
) && is_exited (currthread
))
1138 /* We're seeing an event on a thread id we knew had exited.
1139 This has to be a new thread reusing the old id. Add it. */
1140 add_thread (currthread
);
1144 if (!in_thread_list (currthread
))
1146 if (ptid_equal (pid_to_ptid (ptid_get_pid (currthread
)), inferior_ptid
))
1148 /* inferior_ptid has no thread member yet. This can happen
1149 with the vAttach -> remote_wait,"TAAthread:" path if the
1150 stub doesn't support qC. This is the first stop reported
1151 after an attach, so this is the main thread. Update the
1152 ptid in the thread list. */
1153 thread_change_ptid (inferior_ptid
, currthread
);
1157 if (ptid_equal (magic_null_ptid
, inferior_ptid
))
1159 /* inferior_ptid is not set yet. This can happen with the
1160 vRun -> remote_wait,"TAAthread:" path if the stub
1161 doesn't support qC. This is the first stop reported
1162 after an attach, so this is the main thread. Update the
1163 ptid in the thread list. */
1164 thread_change_ptid (inferior_ptid
, currthread
);
1168 /* When connecting to a target remote, or to a target
1169 extended-remote which already was debugging an inferior, we
1170 may not know about it yet. Add it before adding its child
1171 thread, so notifications are emitted in a sensible order. */
1172 if (!in_inferior_list (ptid_get_pid (currthread
)))
1173 add_inferior (ptid_get_pid (currthread
));
1175 /* This is really a new thread. Add it. */
1176 add_thread (currthread
);
1180 /* Call this function as a result of
1181 1) A halt indication (T packet) containing a thread id
1182 2) A direct query of currthread
1183 3) Successful execution of set thread
1187 record_currthread (ptid_t currthread
)
1189 general_thread
= currthread
;
1191 if (ptid_equal (currthread
, minus_one_ptid
))
1192 /* We're just invalidating the local thread mirror. */
1195 notice_new_inferiors (currthread
);
1198 static char *last_pass_packet
;
1200 /* If 'QPassSignals' is supported, tell the remote stub what signals
1201 it can simply pass through to the inferior without reporting. */
1204 remote_pass_signals (void)
1206 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1208 char *pass_packet
, *p
;
1209 int numsigs
= (int) TARGET_SIGNAL_LAST
;
1212 gdb_assert (numsigs
< 256);
1213 for (i
= 0; i
< numsigs
; i
++)
1215 if (signal_stop_state (i
) == 0
1216 && signal_print_state (i
) == 0
1217 && signal_pass_state (i
) == 1)
1220 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1221 strcpy (pass_packet
, "QPassSignals:");
1222 p
= pass_packet
+ strlen (pass_packet
);
1223 for (i
= 0; i
< numsigs
; i
++)
1225 if (signal_stop_state (i
) == 0
1226 && signal_print_state (i
) == 0
1227 && signal_pass_state (i
) == 1)
1230 *p
++ = tohex (i
>> 4);
1231 *p
++ = tohex (i
& 15);
1240 if (!last_pass_packet
|| strcmp (last_pass_packet
, pass_packet
))
1242 struct remote_state
*rs
= get_remote_state ();
1243 char *buf
= rs
->buf
;
1245 putpkt (pass_packet
);
1246 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1247 packet_ok (buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1248 if (last_pass_packet
)
1249 xfree (last_pass_packet
);
1250 last_pass_packet
= pass_packet
;
1253 xfree (pass_packet
);
1257 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1258 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1259 thread. If GEN is set, set the general thread, if not, then set
1260 the step/continue thread. */
1262 set_thread (struct ptid ptid
, int gen
)
1264 struct remote_state
*rs
= get_remote_state ();
1265 ptid_t state
= gen
? general_thread
: continue_thread
;
1266 char *buf
= rs
->buf
;
1267 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1269 if (ptid_equal (state
, ptid
))
1273 *buf
++ = gen
? 'g' : 'c';
1274 if (ptid_equal (ptid
, magic_null_ptid
))
1275 xsnprintf (buf
, endbuf
- buf
, "0");
1276 else if (ptid_equal (ptid
, any_thread_ptid
))
1277 xsnprintf (buf
, endbuf
- buf
, "0");
1278 else if (ptid_equal (ptid
, minus_one_ptid
))
1279 xsnprintf (buf
, endbuf
- buf
, "-1");
1281 write_ptid (buf
, endbuf
, ptid
);
1283 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1285 general_thread
= ptid
;
1287 continue_thread
= ptid
;
1291 set_general_thread (struct ptid ptid
)
1293 set_thread (ptid
, 1);
1297 set_continue_thread (struct ptid ptid
)
1299 set_thread (ptid
, 0);
1302 /* Change the remote current process. Which thread within the process
1303 ends up selected isn't important, as long as it is the same process
1304 as what INFERIOR_PTID points to.
1306 This comes from that fact that there is no explicit notion of
1307 "selected process" in the protocol. The selected process for
1308 general operations is the process the selected general thread
1312 set_general_process (void)
1314 struct remote_state
*rs
= get_remote_state ();
1316 /* If the remote can't handle multiple processes, don't bother. */
1317 if (!remote_multi_process_p (rs
))
1320 /* We only need to change the remote current thread if it's pointing
1321 at some other process. */
1322 if (ptid_get_pid (general_thread
) != ptid_get_pid (inferior_ptid
))
1323 set_general_thread (inferior_ptid
);
1327 /* Return nonzero if the thread PTID is still alive on the remote
1331 remote_thread_alive (ptid_t ptid
)
1333 struct remote_state
*rs
= get_remote_state ();
1334 int tid
= ptid_get_tid (ptid
);
1337 if (ptid_equal (ptid
, magic_null_ptid
))
1338 /* The main thread is always alive. */
1341 if (ptid_get_pid (ptid
) != 0 && ptid_get_tid (ptid
) == 0)
1342 /* The main thread is always alive. This can happen after a
1343 vAttach, if the remote side doesn't support
1348 endp
= rs
->buf
+ get_remote_packet_size ();
1351 write_ptid (p
, endp
, ptid
);
1354 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1355 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1358 /* About these extended threadlist and threadinfo packets. They are
1359 variable length packets but, the fields within them are often fixed
1360 length. They are redundent enough to send over UDP as is the
1361 remote protocol in general. There is a matching unit test module
1364 #define OPAQUETHREADBYTES 8
1366 /* a 64 bit opaque identifier */
1367 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1369 /* WARNING: This threadref data structure comes from the remote O.S.,
1370 libstub protocol encoding, and remote.c. it is not particularly
1373 /* Right now, the internal structure is int. We want it to be bigger.
1377 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1379 /* gdb_ext_thread_info is an internal GDB data structure which is
1380 equivalent to the reply of the remote threadinfo packet. */
1382 struct gdb_ext_thread_info
1384 threadref threadid
; /* External form of thread reference. */
1385 int active
; /* Has state interesting to GDB?
1387 char display
[256]; /* Brief state display, name,
1388 blocked/suspended. */
1389 char shortname
[32]; /* To be used to name threads. */
1390 char more_display
[256]; /* Long info, statistics, queue depth,
1394 /* The volume of remote transfers can be limited by submitting
1395 a mask containing bits specifying the desired information.
1396 Use a union of these values as the 'selection' parameter to
1397 get_thread_info. FIXME: Make these TAG names more thread specific.
1400 #define TAG_THREADID 1
1401 #define TAG_EXISTS 2
1402 #define TAG_DISPLAY 4
1403 #define TAG_THREADNAME 8
1404 #define TAG_MOREDISPLAY 16
1406 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1408 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1410 static char *unpack_nibble (char *buf
, int *val
);
1412 static char *pack_nibble (char *buf
, int nibble
);
1414 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1416 static char *unpack_byte (char *buf
, int *value
);
1418 static char *pack_int (char *buf
, int value
);
1420 static char *unpack_int (char *buf
, int *value
);
1422 static char *unpack_string (char *src
, char *dest
, int length
);
1424 static char *pack_threadid (char *pkt
, threadref
*id
);
1426 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1428 void int_to_threadref (threadref
*id
, int value
);
1430 static int threadref_to_int (threadref
*ref
);
1432 static void copy_threadref (threadref
*dest
, threadref
*src
);
1434 static int threadmatch (threadref
*dest
, threadref
*src
);
1436 static char *pack_threadinfo_request (char *pkt
, int mode
,
1439 static int remote_unpack_thread_info_response (char *pkt
,
1440 threadref
*expectedref
,
1441 struct gdb_ext_thread_info
1445 static int remote_get_threadinfo (threadref
*threadid
,
1446 int fieldset
, /*TAG mask */
1447 struct gdb_ext_thread_info
*info
);
1449 static char *pack_threadlist_request (char *pkt
, int startflag
,
1451 threadref
*nextthread
);
1453 static int parse_threadlist_response (char *pkt
,
1455 threadref
*original_echo
,
1456 threadref
*resultlist
,
1459 static int remote_get_threadlist (int startflag
,
1460 threadref
*nextthread
,
1464 threadref
*threadlist
);
1466 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1468 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1469 void *context
, int looplimit
);
1471 static int remote_newthread_step (threadref
*ref
, void *context
);
1474 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1475 buffer we're allowed to write to. Returns
1476 BUF+CHARACTERS_WRITTEN. */
1479 write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
1482 struct remote_state
*rs
= get_remote_state ();
1484 if (remote_multi_process_p (rs
))
1486 pid
= ptid_get_pid (ptid
);
1488 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
1490 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
1492 tid
= ptid_get_tid (ptid
);
1494 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
1496 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
1501 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
1502 passed the last parsed char. Returns null_ptid on error. */
1505 read_ptid (char *buf
, char **obuf
)
1509 ULONGEST pid
= 0, tid
= 0;
1514 /* Multi-process ptid. */
1515 pp
= unpack_varlen_hex (p
+ 1, &pid
);
1517 error (_("invalid remote ptid: %s\n"), p
);
1520 pp
= unpack_varlen_hex (p
+ 1, &tid
);
1523 return ptid_build (pid
, 0, tid
);
1526 /* No multi-process. Just a tid. */
1527 pp
= unpack_varlen_hex (p
, &tid
);
1529 /* Since the stub is not sending a process id, then default to
1530 what's in inferior_ptid, unless it's null at this point. If so,
1531 then since there's no way to know the pid of the reported
1532 threads, use the magic number. */
1533 if (ptid_equal (inferior_ptid
, null_ptid
))
1534 pid
= ptid_get_pid (magic_null_ptid
);
1536 pid
= ptid_get_pid (inferior_ptid
);
1540 return ptid_build (pid
, 0, tid
);
1543 /* Encode 64 bits in 16 chars of hex. */
1545 static const char hexchars
[] = "0123456789abcdef";
1548 ishex (int ch
, int *val
)
1550 if ((ch
>= 'a') && (ch
<= 'f'))
1552 *val
= ch
- 'a' + 10;
1555 if ((ch
>= 'A') && (ch
<= 'F'))
1557 *val
= ch
- 'A' + 10;
1560 if ((ch
>= '0') && (ch
<= '9'))
1571 if (ch
>= 'a' && ch
<= 'f')
1572 return ch
- 'a' + 10;
1573 if (ch
>= '0' && ch
<= '9')
1575 if (ch
>= 'A' && ch
<= 'F')
1576 return ch
- 'A' + 10;
1581 stub_unpack_int (char *buff
, int fieldlength
)
1588 nibble
= stubhex (*buff
++);
1592 retval
= retval
<< 4;
1598 unpack_varlen_hex (char *buff
, /* packet to parse */
1602 ULONGEST retval
= 0;
1604 while (ishex (*buff
, &nibble
))
1607 retval
= retval
<< 4;
1608 retval
|= nibble
& 0x0f;
1615 unpack_nibble (char *buf
, int *val
)
1617 *val
= fromhex (*buf
++);
1622 pack_nibble (char *buf
, int nibble
)
1624 *buf
++ = hexchars
[(nibble
& 0x0f)];
1629 pack_hex_byte (char *pkt
, int byte
)
1631 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1632 *pkt
++ = hexchars
[(byte
& 0xf)];
1637 unpack_byte (char *buf
, int *value
)
1639 *value
= stub_unpack_int (buf
, 2);
1644 pack_int (char *buf
, int value
)
1646 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1647 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1648 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1649 buf
= pack_hex_byte (buf
, (value
& 0xff));
1654 unpack_int (char *buf
, int *value
)
1656 *value
= stub_unpack_int (buf
, 8);
1660 #if 0 /* Currently unused, uncomment when needed. */
1661 static char *pack_string (char *pkt
, char *string
);
1664 pack_string (char *pkt
, char *string
)
1669 len
= strlen (string
);
1671 len
= 200; /* Bigger than most GDB packets, junk??? */
1672 pkt
= pack_hex_byte (pkt
, len
);
1676 if ((ch
== '\0') || (ch
== '#'))
1677 ch
= '*'; /* Protect encapsulation. */
1682 #endif /* 0 (unused) */
1685 unpack_string (char *src
, char *dest
, int length
)
1694 pack_threadid (char *pkt
, threadref
*id
)
1697 unsigned char *altid
;
1699 altid
= (unsigned char *) id
;
1700 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1702 pkt
= pack_hex_byte (pkt
, *altid
++);
1708 unpack_threadid (char *inbuf
, threadref
*id
)
1711 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1714 altref
= (char *) id
;
1716 while (inbuf
< limit
)
1718 x
= stubhex (*inbuf
++);
1719 y
= stubhex (*inbuf
++);
1720 *altref
++ = (x
<< 4) | y
;
1725 /* Externally, threadrefs are 64 bits but internally, they are still
1726 ints. This is due to a mismatch of specifications. We would like
1727 to use 64bit thread references internally. This is an adapter
1731 int_to_threadref (threadref
*id
, int value
)
1733 unsigned char *scan
;
1735 scan
= (unsigned char *) id
;
1741 *scan
++ = (value
>> 24) & 0xff;
1742 *scan
++ = (value
>> 16) & 0xff;
1743 *scan
++ = (value
>> 8) & 0xff;
1744 *scan
++ = (value
& 0xff);
1748 threadref_to_int (threadref
*ref
)
1751 unsigned char *scan
;
1757 value
= (value
<< 8) | ((*scan
++) & 0xff);
1762 copy_threadref (threadref
*dest
, threadref
*src
)
1765 unsigned char *csrc
, *cdest
;
1767 csrc
= (unsigned char *) src
;
1768 cdest
= (unsigned char *) dest
;
1775 threadmatch (threadref
*dest
, threadref
*src
)
1777 /* Things are broken right now, so just assume we got a match. */
1779 unsigned char *srcp
, *destp
;
1781 srcp
= (char *) src
;
1782 destp
= (char *) dest
;
1786 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1793 threadid:1, # always request threadid
1800 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1803 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1805 *pkt
++ = 'q'; /* Info Query */
1806 *pkt
++ = 'P'; /* process or thread info */
1807 pkt
= pack_int (pkt
, mode
); /* mode */
1808 pkt
= pack_threadid (pkt
, id
); /* threadid */
1809 *pkt
= '\0'; /* terminate */
1813 /* These values tag the fields in a thread info response packet. */
1814 /* Tagging the fields allows us to request specific fields and to
1815 add more fields as time goes by. */
1817 #define TAG_THREADID 1 /* Echo the thread identifier. */
1818 #define TAG_EXISTS 2 /* Is this process defined enough to
1819 fetch registers and its stack? */
1820 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1821 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1822 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1826 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1827 struct gdb_ext_thread_info
*info
)
1829 struct remote_state
*rs
= get_remote_state ();
1833 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
1836 /* info->threadid = 0; FIXME: implement zero_threadref. */
1838 info
->display
[0] = '\0';
1839 info
->shortname
[0] = '\0';
1840 info
->more_display
[0] = '\0';
1842 /* Assume the characters indicating the packet type have been
1844 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1845 pkt
= unpack_threadid (pkt
, &ref
);
1848 warning (_("Incomplete response to threadinfo request."));
1849 if (!threadmatch (&ref
, expectedref
))
1850 { /* This is an answer to a different request. */
1851 warning (_("ERROR RMT Thread info mismatch."));
1854 copy_threadref (&info
->threadid
, &ref
);
1856 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1858 /* Packets are terminated with nulls. */
1859 while ((pkt
< limit
) && mask
&& *pkt
)
1861 pkt
= unpack_int (pkt
, &tag
); /* tag */
1862 pkt
= unpack_byte (pkt
, &length
); /* length */
1863 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1865 warning (_("ERROR RMT: threadinfo tag mismatch."));
1869 if (tag
== TAG_THREADID
)
1873 warning (_("ERROR RMT: length of threadid is not 16."));
1877 pkt
= unpack_threadid (pkt
, &ref
);
1878 mask
= mask
& ~TAG_THREADID
;
1881 if (tag
== TAG_EXISTS
)
1883 info
->active
= stub_unpack_int (pkt
, length
);
1885 mask
= mask
& ~(TAG_EXISTS
);
1888 warning (_("ERROR RMT: 'exists' length too long."));
1894 if (tag
== TAG_THREADNAME
)
1896 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1897 mask
= mask
& ~TAG_THREADNAME
;
1900 if (tag
== TAG_DISPLAY
)
1902 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1903 mask
= mask
& ~TAG_DISPLAY
;
1906 if (tag
== TAG_MOREDISPLAY
)
1908 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1909 mask
= mask
& ~TAG_MOREDISPLAY
;
1912 warning (_("ERROR RMT: unknown thread info tag."));
1913 break; /* Not a tag we know about. */
1919 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1920 struct gdb_ext_thread_info
*info
)
1922 struct remote_state
*rs
= get_remote_state ();
1925 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
1927 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1929 if (rs
->buf
[0] == '\0')
1932 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
1937 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1940 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1941 threadref
*nextthread
)
1943 *pkt
++ = 'q'; /* info query packet */
1944 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1945 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1946 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1947 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1952 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1955 parse_threadlist_response (char *pkt
, int result_limit
,
1956 threadref
*original_echo
, threadref
*resultlist
,
1959 struct remote_state
*rs
= get_remote_state ();
1961 int count
, resultcount
, done
;
1964 /* Assume the 'q' and 'M chars have been stripped. */
1965 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
1966 /* done parse past here */
1967 pkt
= unpack_byte (pkt
, &count
); /* count field */
1968 pkt
= unpack_nibble (pkt
, &done
);
1969 /* The first threadid is the argument threadid. */
1970 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1971 while ((count
-- > 0) && (pkt
< limit
))
1973 pkt
= unpack_threadid (pkt
, resultlist
++);
1974 if (resultcount
++ >= result_limit
)
1983 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1984 int *done
, int *result_count
, threadref
*threadlist
)
1986 struct remote_state
*rs
= get_remote_state ();
1987 static threadref echo_nextthread
;
1990 /* Trancate result limit to be smaller than the packet size. */
1991 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= get_remote_packet_size ())
1992 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
1994 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
1996 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1998 if (*rs
->buf
== '\0')
2002 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
2005 if (!threadmatch (&echo_nextthread
, nextthread
))
2007 /* FIXME: This is a good reason to drop the packet. */
2008 /* Possably, there is a duplicate response. */
2010 retransmit immediatly - race conditions
2011 retransmit after timeout - yes
2013 wait for packet, then exit
2015 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2016 return 0; /* I choose simply exiting. */
2018 if (*result_count
<= 0)
2022 warning (_("RMT ERROR : failed to get remote thread list."));
2025 return result
; /* break; */
2027 if (*result_count
> result_limit
)
2030 warning (_("RMT ERROR: threadlist response longer than requested."));
2036 /* This is the interface between remote and threads, remotes upper
2039 /* remote_find_new_threads retrieves the thread list and for each
2040 thread in the list, looks up the thread in GDB's internal list,
2041 adding the thread if it does not already exist. This involves
2042 getting partial thread lists from the remote target so, polling the
2043 quit_flag is required. */
2046 /* About this many threadisds fit in a packet. */
2048 #define MAXTHREADLISTRESULTS 32
2051 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2054 int done
, i
, result_count
;
2058 static threadref nextthread
;
2059 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
2064 if (loopcount
++ > looplimit
)
2067 warning (_("Remote fetch threadlist -infinite loop-."));
2070 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
2071 &done
, &result_count
, resultthreadlist
))
2076 /* Clear for later iterations. */
2078 /* Setup to resume next batch of thread references, set nextthread. */
2079 if (result_count
>= 1)
2080 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
2082 while (result_count
--)
2083 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
2090 remote_newthread_step (threadref
*ref
, void *context
)
2092 int pid
= ptid_get_pid (inferior_ptid
);
2093 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2095 if (!in_thread_list (ptid
))
2097 return 1; /* continue iterator */
2100 #define CRAZY_MAX_THREADS 1000
2103 remote_current_thread (ptid_t oldpid
)
2105 struct remote_state
*rs
= get_remote_state ();
2111 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2112 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2113 return read_ptid (&rs
->buf
[2], NULL
);
2118 /* Find new threads for info threads command.
2119 * Original version, using John Metzler's thread protocol.
2123 remote_find_new_threads (void)
2125 remote_threadlist_iterator (remote_newthread_step
, 0,
2130 * Find all threads for info threads command.
2131 * Uses new thread protocol contributed by Cisco.
2132 * Falls back and attempts to use the older method (above)
2133 * if the target doesn't respond to the new method.
2137 remote_threads_info (void)
2139 struct remote_state
*rs
= get_remote_state ();
2143 if (remote_desc
== 0) /* paranoia */
2144 error (_("Command can only be used when connected to the remote target."));
2146 if (use_threadinfo_query
)
2148 putpkt ("qfThreadInfo");
2149 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2151 if (bufp
[0] != '\0') /* q packet recognized */
2153 while (*bufp
++ == 'm') /* reply contains one or more TID */
2157 new_thread
= read_ptid (bufp
, &bufp
);
2158 if (!ptid_equal (new_thread
, null_ptid
)
2159 && (!in_thread_list (new_thread
)
2160 || is_exited (new_thread
)))
2162 /* When connected to a multi-process aware stub,
2163 "info threads" may show up threads of
2164 inferiors we didn't know about yet. Add them
2165 now, and before adding any of its child
2166 threads, so notifications are emitted in a
2168 if (!in_inferior_list (ptid_get_pid (new_thread
)))
2169 add_inferior (ptid_get_pid (new_thread
));
2171 add_thread (new_thread
);
2173 /* In non-stop mode, we assume new found threads
2174 are running until we proven otherwise with a
2175 stop reply. In all-stop, we can only get
2176 here if all threads are stopped. */
2177 set_executing (new_thread
, non_stop
? 1 : 0);
2178 set_running (new_thread
, non_stop
? 1 : 0);
2181 while (*bufp
++ == ','); /* comma-separated list */
2182 putpkt ("qsThreadInfo");
2183 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2190 /* Only qfThreadInfo is supported in non-stop mode. */
2194 /* Else fall back to old method based on jmetzler protocol. */
2195 use_threadinfo_query
= 0;
2196 remote_find_new_threads ();
2201 * Collect a descriptive string about the given thread.
2202 * The target may say anything it wants to about the thread
2203 * (typically info about its blocked / runnable state, name, etc.).
2204 * This string will appear in the info threads display.
2206 * Optional: targets are not required to implement this function.
2210 remote_threads_extra_info (struct thread_info
*tp
)
2212 struct remote_state
*rs
= get_remote_state ();
2216 struct gdb_ext_thread_info threadinfo
;
2217 static char display_buf
[100]; /* arbitrary... */
2218 int n
= 0; /* position in display_buf */
2220 if (remote_desc
== 0) /* paranoia */
2221 internal_error (__FILE__
, __LINE__
,
2222 _("remote_threads_extra_info"));
2224 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2225 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2226 /* This is the main thread which was added by GDB. The remote
2227 server doesn't know about it. */
2230 if (use_threadextra_query
)
2233 char *endb
= rs
->buf
+ get_remote_packet_size ();
2235 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2237 write_ptid (b
, endb
, tp
->ptid
);
2240 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2241 if (rs
->buf
[0] != 0)
2243 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2244 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2245 display_buf
[result
] = '\0';
2250 /* If the above query fails, fall back to the old method. */
2251 use_threadextra_query
= 0;
2252 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2253 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2254 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2255 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2256 if (threadinfo
.active
)
2258 if (*threadinfo
.shortname
)
2259 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2260 " Name: %s,", threadinfo
.shortname
);
2261 if (*threadinfo
.display
)
2262 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2263 " State: %s,", threadinfo
.display
);
2264 if (*threadinfo
.more_display
)
2265 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2266 " Priority: %s", threadinfo
.more_display
);
2270 /* For purely cosmetic reasons, clear up trailing commas. */
2271 if (',' == display_buf
[n
-1])
2272 display_buf
[n
-1] = ' ';
2280 /* Restart the remote side; this is an extended protocol operation. */
2283 extended_remote_restart (void)
2285 struct remote_state
*rs
= get_remote_state ();
2287 /* Send the restart command; for reasons I don't understand the
2288 remote side really expects a number after the "R". */
2289 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2292 remote_fileio_reset ();
2295 /* Clean up connection to a remote debugger. */
2298 remote_close (int quitting
)
2300 if (remote_desc
== NULL
)
2301 return; /* already closed */
2303 /* Make sure we leave stdin registered in the event loop, and we
2304 don't leave the async SIGINT signal handler installed. */
2305 remote_terminal_ours ();
2307 serial_close (remote_desc
);
2310 /* We don't have a connection to the remote stub anymore. Get rid
2311 of all the inferiors and their threads we were controlling. */
2312 discard_all_inferiors ();
2314 /* We're no longer interested in any of these events. */
2315 discard_pending_stop_replies (-1);
2317 if (remote_async_inferior_event_token
)
2318 delete_async_event_handler (&remote_async_inferior_event_token
);
2319 if (remote_async_get_pending_events_token
)
2320 delete_async_event_handler (&remote_async_get_pending_events_token
);
2322 generic_mourn_inferior ();
2325 /* Query the remote side for the text, data and bss offsets. */
2330 struct remote_state
*rs
= get_remote_state ();
2333 int lose
, num_segments
= 0, do_sections
, do_segments
;
2334 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
2335 struct section_offsets
*offs
;
2336 struct symfile_segment_data
*data
;
2338 if (symfile_objfile
== NULL
)
2341 putpkt ("qOffsets");
2342 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2345 if (buf
[0] == '\000')
2346 return; /* Return silently. Stub doesn't support
2350 warning (_("Remote failure reply: %s"), buf
);
2354 /* Pick up each field in turn. This used to be done with scanf, but
2355 scanf will make trouble if CORE_ADDR size doesn't match
2356 conversion directives correctly. The following code will work
2357 with any size of CORE_ADDR. */
2358 text_addr
= data_addr
= bss_addr
= 0;
2362 if (strncmp (ptr
, "Text=", 5) == 0)
2365 /* Don't use strtol, could lose on big values. */
2366 while (*ptr
&& *ptr
!= ';')
2367 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2369 if (strncmp (ptr
, ";Data=", 6) == 0)
2372 while (*ptr
&& *ptr
!= ';')
2373 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2378 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
2381 while (*ptr
&& *ptr
!= ';')
2382 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
2384 if (bss_addr
!= data_addr
)
2385 warning (_("Target reported unsupported offsets: %s"), buf
);
2390 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
2393 /* Don't use strtol, could lose on big values. */
2394 while (*ptr
&& *ptr
!= ';')
2395 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2398 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
2401 while (*ptr
&& *ptr
!= ';')
2402 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2410 error (_("Malformed response to offset query, %s"), buf
);
2411 else if (*ptr
!= '\0')
2412 warning (_("Target reported unsupported offsets: %s"), buf
);
2414 offs
= ((struct section_offsets
*)
2415 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
2416 memcpy (offs
, symfile_objfile
->section_offsets
,
2417 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
2419 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
2420 do_segments
= (data
!= NULL
);
2421 do_sections
= num_segments
== 0;
2423 if (num_segments
> 0)
2425 segments
[0] = text_addr
;
2426 segments
[1] = data_addr
;
2428 /* If we have two segments, we can still try to relocate everything
2429 by assuming that the .text and .data offsets apply to the whole
2430 text and data segments. Convert the offsets given in the packet
2431 to base addresses for symfile_map_offsets_to_segments. */
2432 else if (data
&& data
->num_segments
== 2)
2434 segments
[0] = data
->segment_bases
[0] + text_addr
;
2435 segments
[1] = data
->segment_bases
[1] + data_addr
;
2438 /* If the object file has only one segment, assume that it is text
2439 rather than data; main programs with no writable data are rare,
2440 but programs with no code are useless. Of course the code might
2441 have ended up in the data segment... to detect that we would need
2442 the permissions here. */
2443 else if (data
&& data
->num_segments
== 1)
2445 segments
[0] = data
->segment_bases
[0] + text_addr
;
2448 /* There's no way to relocate by segment. */
2454 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
2455 offs
, num_segments
, segments
);
2457 if (ret
== 0 && !do_sections
)
2458 error (_("Can not handle qOffsets TextSeg response with this symbol file"));
2465 free_symfile_segment_data (data
);
2469 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
2471 /* This is a temporary kludge to force data and bss to use the same offsets
2472 because that's what nlmconv does now. The real solution requires changes
2473 to the stub and remote.c that I don't have time to do right now. */
2475 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
2476 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
2479 objfile_relocate (symfile_objfile
, offs
);
2482 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
2483 threads we know are stopped already. This is used during the
2484 initial remote connection in non-stop mode --- threads that are
2485 reported as already being stopped are left stopped. */
2488 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
2490 /* If we have a stop reply for this thread, it must be stopped. */
2491 if (peek_stop_reply (thread
->ptid
))
2492 set_stop_requested (thread
->ptid
, 1);
2497 /* Stub for catch_exception. */
2499 struct start_remote_args
2503 /* The current target. */
2504 struct target_ops
*target
;
2506 /* Non-zero if this is an extended-remote target. */
2511 remote_start_remote (struct ui_out
*uiout
, void *opaque
)
2513 struct start_remote_args
*args
= opaque
;
2514 struct remote_state
*rs
= get_remote_state ();
2515 struct packet_config
*noack_config
;
2516 char *wait_status
= NULL
;
2518 immediate_quit
++; /* Allow user to interrupt it. */
2520 /* Ack any packet which the remote side has already sent. */
2521 serial_write (remote_desc
, "+", 1);
2523 /* The first packet we send to the target is the optional "supported
2524 packets" request. If the target can answer this, it will tell us
2525 which later probes to skip. */
2526 remote_query_supported ();
2528 /* Next, we possibly activate noack mode.
2530 If the QStartNoAckMode packet configuration is set to AUTO,
2531 enable noack mode if the stub reported a wish for it with
2534 If set to TRUE, then enable noack mode even if the stub didn't
2535 report it in qSupported. If the stub doesn't reply OK, the
2536 session ends with an error.
2538 If FALSE, then don't activate noack mode, regardless of what the
2539 stub claimed should be the default with qSupported. */
2541 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
2543 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
2544 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
2545 && noack_config
->support
== PACKET_ENABLE
))
2547 putpkt ("QStartNoAckMode");
2548 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2549 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
2553 if (args
->extended_p
)
2555 /* Tell the remote that we are using the extended protocol. */
2557 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2560 /* Next, if the target can specify a description, read it. We do
2561 this before anything involving memory or registers. */
2562 target_find_description ();
2564 /* On OSs where the list of libraries is global to all
2565 processes, we fetch them early. */
2566 if (gdbarch_has_global_solist (target_gdbarch
))
2567 solib_add (NULL
, args
->from_tty
, args
->target
, auto_solib_add
);
2571 if (!rs
->non_stop_aware
)
2572 error (_("Non-stop mode requested, but remote does not support non-stop"));
2574 putpkt ("QNonStop:1");
2575 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2577 if (strcmp (rs
->buf
, "OK") != 0)
2578 error ("Remote refused setting non-stop mode with: %s", rs
->buf
);
2580 /* Find about threads and processes the stub is already
2581 controlling. We default to adding them in the running state.
2582 The '?' query below will then tell us about which threads are
2584 remote_threads_info ();
2586 else if (rs
->non_stop_aware
)
2588 /* Don't assume that the stub can operate in all-stop mode.
2589 Request it explicitely. */
2590 putpkt ("QNonStop:0");
2591 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2593 if (strcmp (rs
->buf
, "OK") != 0)
2594 error ("Remote refused setting all-stop mode with: %s", rs
->buf
);
2597 /* Check whether the target is running now. */
2599 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2603 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
2605 if (args
->extended_p
)
2607 /* We're connected, but not running. Drop out before we
2608 call start_remote. */
2609 target_mark_exited (args
->target
);
2613 error (_("The target is not running (try extended-remote?)"));
2617 if (args
->extended_p
)
2618 target_mark_running (args
->target
);
2620 /* Save the reply for later. */
2621 wait_status
= alloca (strlen (rs
->buf
) + 1);
2622 strcpy (wait_status
, rs
->buf
);
2625 /* Let the stub know that we want it to return the thread. */
2626 set_continue_thread (minus_one_ptid
);
2628 /* Without this, some commands which require an active target
2629 (such as kill) won't work. This variable serves (at least)
2630 double duty as both the pid of the target process (if it has
2631 such), and as a flag indicating that a target is active.
2632 These functions should be split out into seperate variables,
2633 especially since GDB will someday have a notion of debugging
2634 several processes. */
2635 inferior_ptid
= magic_null_ptid
;
2637 /* Now, if we have thread information, update inferior_ptid. */
2638 inferior_ptid
= remote_current_thread (inferior_ptid
);
2640 add_inferior (ptid_get_pid (inferior_ptid
));
2642 /* Always add the main thread. */
2643 add_thread_silent (inferior_ptid
);
2645 get_offsets (); /* Get text, data & bss offsets. */
2647 /* If we could not find a description using qXfer, and we know
2648 how to do it some other way, try again. This is not
2649 supported for non-stop; it could be, but it is tricky if
2650 there are no stopped threads when we connect. */
2651 if (remote_read_description_p (args
->target
)
2652 && gdbarch_target_desc (target_gdbarch
) == NULL
)
2654 target_clear_description ();
2655 target_find_description ();
2658 /* Use the previously fetched status. */
2659 gdb_assert (wait_status
!= NULL
);
2660 strcpy (rs
->buf
, wait_status
);
2661 rs
->cached_wait_status
= 1;
2664 start_remote (args
->from_tty
); /* Initialize gdb process mechanisms. */
2668 /* In non-stop, we will either get an "OK", meaning that there
2669 are no stopped threads at this time; or, a regular stop
2670 reply. In the latter case, there may be more than one thread
2671 stopped --- we pull them all out using the vStopped
2673 if (strcmp (rs
->buf
, "OK") != 0)
2675 struct stop_reply
*stop_reply
;
2676 struct cleanup
*old_chain
;
2678 stop_reply
= stop_reply_xmalloc ();
2679 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
2681 remote_parse_stop_reply (rs
->buf
, stop_reply
);
2682 discard_cleanups (old_chain
);
2684 /* get_pending_stop_replies acks this one, and gets the rest
2686 pending_stop_reply
= stop_reply
;
2687 remote_get_pending_stop_replies ();
2689 /* Make sure that threads that were stopped remain
2691 iterate_over_threads (set_stop_requested_callback
, NULL
);
2694 if (target_can_async_p ())
2695 target_async (inferior_event_handler
, 0);
2697 if (thread_count () == 0)
2699 if (args
->extended_p
)
2701 /* We're connected, but not running. Drop out before we
2702 call start_remote. */
2703 target_mark_exited (args
->target
);
2707 error (_("The target is not running (try extended-remote?)"));
2710 if (args
->extended_p
)
2711 target_mark_running (args
->target
);
2713 /* Let the stub know that we want it to return the thread. */
2715 /* Force the stub to choose a thread. */
2716 set_general_thread (null_ptid
);
2719 inferior_ptid
= remote_current_thread (minus_one_ptid
);
2720 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
2721 error (_("remote didn't report the current thread in non-stop mode"));
2723 get_offsets (); /* Get text, data & bss offsets. */
2725 /* In non-stop mode, any cached wait status will be stored in
2726 the stop reply queue. */
2727 gdb_assert (wait_status
== NULL
);
2730 /* If we connected to a live target, do some additional setup. */
2731 if (target_has_execution
)
2733 if (exec_bfd
) /* No use without an exec file. */
2734 remote_check_symbols (symfile_objfile
);
2737 /* If code is shared between processes, then breakpoints are global
2738 too; Insert them now. */
2739 if (gdbarch_has_global_solist (target_gdbarch
)
2740 && breakpoints_always_inserted_mode ())
2741 insert_breakpoints ();
2744 /* Open a connection to a remote debugger.
2745 NAME is the filename used for communication. */
2748 remote_open (char *name
, int from_tty
)
2750 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
2753 /* Open a connection to a remote debugger using the extended
2754 remote gdb protocol. NAME is the filename used for communication. */
2757 extended_remote_open (char *name
, int from_tty
)
2759 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
2762 /* Generic code for opening a connection to a remote target. */
2765 init_all_packet_configs (void)
2768 for (i
= 0; i
< PACKET_MAX
; i
++)
2769 update_packet_config (&remote_protocol_packets
[i
]);
2772 /* Symbol look-up. */
2775 remote_check_symbols (struct objfile
*objfile
)
2777 struct remote_state
*rs
= get_remote_state ();
2778 char *msg
, *reply
, *tmp
;
2779 struct minimal_symbol
*sym
;
2782 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
2785 /* Make sure the remote is pointing at the right process. */
2786 set_general_process ();
2788 /* Allocate a message buffer. We can't reuse the input buffer in RS,
2789 because we need both at the same time. */
2790 msg
= alloca (get_remote_packet_size ());
2792 /* Invite target to request symbol lookups. */
2794 putpkt ("qSymbol::");
2795 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2796 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
2799 while (strncmp (reply
, "qSymbol:", 8) == 0)
2802 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
2804 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
2806 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
2809 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
2811 /* If this is a function address, return the start of code
2812 instead of any data function descriptor. */
2813 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
2817 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
2818 paddr_nz (sym_addr
), &reply
[8]);
2822 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2827 static struct serial
*
2828 remote_serial_open (char *name
)
2830 static int udp_warning
= 0;
2832 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2833 of in ser-tcp.c, because it is the remote protocol assuming that the
2834 serial connection is reliable and not the serial connection promising
2836 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
2839 The remote protocol may be unreliable over UDP.\n\
2840 Some events may be lost, rendering further debugging impossible."));
2844 return serial_open (name
);
2847 /* This type describes each known response to the qSupported
2849 struct protocol_feature
2851 /* The name of this protocol feature. */
2854 /* The default for this protocol feature. */
2855 enum packet_support default_support
;
2857 /* The function to call when this feature is reported, or after
2858 qSupported processing if the feature is not supported.
2859 The first argument points to this structure. The second
2860 argument indicates whether the packet requested support be
2861 enabled, disabled, or probed (or the default, if this function
2862 is being called at the end of processing and this feature was
2863 not reported). The third argument may be NULL; if not NULL, it
2864 is a NUL-terminated string taken from the packet following
2865 this feature's name and an equals sign. */
2866 void (*func
) (const struct protocol_feature
*, enum packet_support
,
2869 /* The corresponding packet for this feature. Only used if
2870 FUNC is remote_supported_packet. */
2875 remote_supported_packet (const struct protocol_feature
*feature
,
2876 enum packet_support support
,
2877 const char *argument
)
2881 warning (_("Remote qSupported response supplied an unexpected value for"
2882 " \"%s\"."), feature
->name
);
2886 if (remote_protocol_packets
[feature
->packet
].support
2887 == PACKET_SUPPORT_UNKNOWN
)
2888 remote_protocol_packets
[feature
->packet
].support
= support
;
2892 remote_packet_size (const struct protocol_feature
*feature
,
2893 enum packet_support support
, const char *value
)
2895 struct remote_state
*rs
= get_remote_state ();
2900 if (support
!= PACKET_ENABLE
)
2903 if (value
== NULL
|| *value
== '\0')
2905 warning (_("Remote target reported \"%s\" without a size."),
2911 packet_size
= strtol (value
, &value_end
, 16);
2912 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
2914 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
2915 feature
->name
, value
);
2919 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
2921 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
2922 packet_size
, MAX_REMOTE_PACKET_SIZE
);
2923 packet_size
= MAX_REMOTE_PACKET_SIZE
;
2926 /* Record the new maximum packet size. */
2927 rs
->explicit_packet_size
= packet_size
;
2931 remote_multi_process_feature (const struct protocol_feature
*feature
,
2932 enum packet_support support
, const char *value
)
2934 struct remote_state
*rs
= get_remote_state ();
2935 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
2939 remote_non_stop_feature (const struct protocol_feature
*feature
,
2940 enum packet_support support
, const char *value
)
2942 struct remote_state
*rs
= get_remote_state ();
2943 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
2946 static struct protocol_feature remote_protocol_features
[] = {
2947 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
2948 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
2949 PACKET_qXfer_auxv
},
2950 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
2951 PACKET_qXfer_features
},
2952 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
2953 PACKET_qXfer_libraries
},
2954 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
2955 PACKET_qXfer_memory_map
},
2956 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
2957 PACKET_qXfer_spu_read
},
2958 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
2959 PACKET_qXfer_spu_write
},
2960 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
2961 PACKET_qXfer_osdata
},
2962 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
2963 PACKET_QPassSignals
},
2964 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
2965 PACKET_QStartNoAckMode
},
2966 { "multiprocess", PACKET_DISABLE
, remote_multi_process_feature
, -1 },
2967 { "QNonStop", PACKET_DISABLE
, remote_non_stop_feature
, -1 },
2971 remote_query_supported (void)
2973 struct remote_state
*rs
= get_remote_state ();
2976 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
2978 /* The packet support flags are handled differently for this packet
2979 than for most others. We treat an error, a disabled packet, and
2980 an empty response identically: any features which must be reported
2981 to be used will be automatically disabled. An empty buffer
2982 accomplishes this, since that is also the representation for a list
2983 containing no features. */
2986 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
2989 putpkt ("qSupported:multiprocess+");
2991 putpkt ("qSupported");
2993 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2995 /* If an error occured, warn, but do not return - just reset the
2996 buffer to empty and go on to disable features. */
2997 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
3000 warning (_("Remote failure reply: %s"), rs
->buf
);
3005 memset (seen
, 0, sizeof (seen
));
3010 enum packet_support is_supported
;
3011 char *p
, *end
, *name_end
, *value
;
3013 /* First separate out this item from the rest of the packet. If
3014 there's another item after this, we overwrite the separator
3015 (terminated strings are much easier to work with). */
3017 end
= strchr (p
, ';');
3020 end
= p
+ strlen (p
);
3030 warning (_("empty item in \"qSupported\" response"));
3035 name_end
= strchr (p
, '=');
3038 /* This is a name=value entry. */
3039 is_supported
= PACKET_ENABLE
;
3040 value
= name_end
+ 1;
3049 is_supported
= PACKET_ENABLE
;
3053 is_supported
= PACKET_DISABLE
;
3057 is_supported
= PACKET_SUPPORT_UNKNOWN
;
3061 warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p
);
3067 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3068 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
3070 const struct protocol_feature
*feature
;
3073 feature
= &remote_protocol_features
[i
];
3074 feature
->func (feature
, is_supported
, value
);
3079 /* If we increased the packet size, make sure to increase the global
3080 buffer size also. We delay this until after parsing the entire
3081 qSupported packet, because this is the same buffer we were
3083 if (rs
->buf_size
< rs
->explicit_packet_size
)
3085 rs
->buf_size
= rs
->explicit_packet_size
;
3086 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
3089 /* Handle the defaults for unmentioned features. */
3090 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3093 const struct protocol_feature
*feature
;
3095 feature
= &remote_protocol_features
[i
];
3096 feature
->func (feature
, feature
->default_support
, NULL
);
3102 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
, int extended_p
)
3104 struct remote_state
*rs
= get_remote_state ();
3107 error (_("To open a remote debug connection, you need to specify what\n"
3108 "serial device is attached to the remote system\n"
3109 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
3111 /* See FIXME above. */
3112 if (!target_async_permitted
)
3113 wait_forever_enabled_p
= 1;
3115 /* If we're connected to a running target, target_preopen will kill it.
3116 But if we're connected to a target system with no running process,
3117 then we will still be connected when it returns. Ask this question
3118 first, before target_preopen has a chance to kill anything. */
3119 if (remote_desc
!= NULL
&& !target_has_execution
)
3122 || query (_("Already connected to a remote target. Disconnect? ")))
3125 error (_("Still connected."));
3128 target_preopen (from_tty
);
3130 unpush_target (target
);
3132 /* This time without a query. If we were connected to an
3133 extended-remote target and target_preopen killed the running
3134 process, we may still be connected. If we are starting "target
3135 remote" now, the extended-remote target will not have been
3136 removed by unpush_target. */
3137 if (remote_desc
!= NULL
&& !target_has_execution
)
3140 /* Make sure we send the passed signals list the next time we resume. */
3141 xfree (last_pass_packet
);
3142 last_pass_packet
= NULL
;
3144 remote_fileio_reset ();
3145 reopen_exec_file ();
3148 remote_desc
= remote_serial_open (name
);
3150 perror_with_name (name
);
3152 if (baud_rate
!= -1)
3154 if (serial_setbaudrate (remote_desc
, baud_rate
))
3156 /* The requested speed could not be set. Error out to
3157 top level after closing remote_desc. Take care to
3158 set remote_desc to NULL to avoid closing remote_desc
3160 serial_close (remote_desc
);
3162 perror_with_name (name
);
3166 serial_raw (remote_desc
);
3168 /* If there is something sitting in the buffer we might take it as a
3169 response to a command, which would be bad. */
3170 serial_flush_input (remote_desc
);
3174 puts_filtered ("Remote debugging using ");
3175 puts_filtered (name
);
3176 puts_filtered ("\n");
3178 push_target (target
); /* Switch to using remote target now. */
3180 /* Assume that the target is not running, until we learn otherwise. */
3182 target_mark_exited (target
);
3184 /* Register extra event sources in the event loop. */
3185 remote_async_inferior_event_token
3186 = create_async_event_handler (remote_async_inferior_event_handler
,
3188 remote_async_get_pending_events_token
3189 = create_async_event_handler (remote_async_get_pending_events_handler
,
3192 /* Reset the target state; these things will be queried either by
3193 remote_query_supported or as they are needed. */
3194 init_all_packet_configs ();
3195 rs
->cached_wait_status
= 0;
3196 rs
->explicit_packet_size
= 0;
3198 rs
->multi_process_aware
= 0;
3199 rs
->extended
= extended_p
;
3200 rs
->non_stop_aware
= 0;
3201 rs
->waiting_for_stop_reply
= 0;
3203 general_thread
= not_sent_ptid
;
3204 continue_thread
= not_sent_ptid
;
3206 /* Probe for ability to use "ThreadInfo" query, as required. */
3207 use_threadinfo_query
= 1;
3208 use_threadextra_query
= 1;
3210 if (target_async_permitted
)
3212 /* With this target we start out by owning the terminal. */
3213 remote_async_terminal_ours_p
= 1;
3215 /* FIXME: cagney/1999-09-23: During the initial connection it is
3216 assumed that the target is already ready and able to respond to
3217 requests. Unfortunately remote_start_remote() eventually calls
3218 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
3219 around this. Eventually a mechanism that allows
3220 wait_for_inferior() to expect/get timeouts will be
3222 wait_forever_enabled_p
= 0;
3225 /* First delete any symbols previously loaded from shared libraries. */
3226 no_shared_libraries (NULL
, 0);
3229 init_thread_list ();
3231 /* Start the remote connection. If error() or QUIT, discard this
3232 target (we'd otherwise be in an inconsistent state) and then
3233 propogate the error on up the exception chain. This ensures that
3234 the caller doesn't stumble along blindly assuming that the
3235 function succeeded. The CLI doesn't have this problem but other
3236 UI's, such as MI do.
3238 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
3239 this function should return an error indication letting the
3240 caller restore the previous state. Unfortunately the command
3241 ``target remote'' is directly wired to this function making that
3242 impossible. On a positive note, the CLI side of this problem has
3243 been fixed - the function set_cmd_context() makes it possible for
3244 all the ``target ....'' commands to share a common callback
3245 function. See cli-dump.c. */
3247 struct gdb_exception ex
;
3248 struct start_remote_args args
;
3250 args
.from_tty
= from_tty
;
3251 args
.target
= target
;
3252 args
.extended_p
= extended_p
;
3254 ex
= catch_exception (uiout
, remote_start_remote
, &args
, RETURN_MASK_ALL
);
3257 /* Pop the partially set up target - unless something else did
3258 already before throwing the exception. */
3259 if (remote_desc
!= NULL
)
3261 if (target_async_permitted
)
3262 wait_forever_enabled_p
= 1;
3263 throw_exception (ex
);
3267 if (target_async_permitted
)
3268 wait_forever_enabled_p
= 1;
3271 /* This takes a program previously attached to and detaches it. After
3272 this is done, GDB can be used to debug some other program. We
3273 better not have left any breakpoints in the target program or it'll
3274 die when it hits one. */
3277 remote_detach_1 (char *args
, int from_tty
, int extended
)
3279 int pid
= ptid_get_pid (inferior_ptid
);
3280 struct remote_state
*rs
= get_remote_state ();
3283 error (_("Argument given to \"detach\" when remotely debugging."));
3285 if (!target_has_execution
)
3286 error (_("No process to detach from."));
3288 /* Tell the remote target to detach. */
3289 if (remote_multi_process_p (rs
))
3290 sprintf (rs
->buf
, "D;%x", pid
);
3292 strcpy (rs
->buf
, "D");
3295 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3297 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
3299 else if (rs
->buf
[0] == '\0')
3300 error (_("Remote doesn't know how to detach"));
3302 error (_("Can't detach process."));
3306 if (remote_multi_process_p (rs
))
3307 printf_filtered (_("Detached from remote %s.\n"),
3308 target_pid_to_str (pid_to_ptid (pid
)));
3312 puts_filtered (_("Detached from remote process.\n"));
3314 puts_filtered (_("Ending remote debugging.\n"));
3318 discard_pending_stop_replies (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
;
4308 error (_("Malformed packet(b) (missing colon): %s\n\
4314 error (_("Remote sent bad register number %s: %s\n\
4316 phex_nz (pnum
, 0), p
, buf
);
4318 cached_reg
.num
= reg
->regnum
;
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 notice_new_inferiors (ptid
);
4517 /* Expedited registers. */
4518 if (stop_reply
->regcache
)
4524 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
4526 regcache_raw_supply (get_thread_regcache (ptid
),
4527 reg
->num
, reg
->data
);
4528 VEC_free (cached_reg_t
, stop_reply
->regcache
);
4531 remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
4532 remote_watch_data_address
= stop_reply
->watch_data_address
;
4535 stop_reply_xfree (stop_reply
);
4539 /* The non-stop mode version of target_wait. */
4542 remote_wait_ns (ptid_t ptid
, struct target_waitstatus
*status
)
4544 struct remote_state
*rs
= get_remote_state ();
4545 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4546 ptid_t event_ptid
= null_ptid
;
4547 struct stop_reply
*stop_reply
;
4550 /* If in non-stop mode, get out of getpkt even if a
4551 notification is received. */
4553 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
4560 case 'E': /* Error of some sort. */
4561 /* We're out of sync with the target now. Did it continue
4562 or not? We can't tell which thread it was in non-stop,
4563 so just ignore this. */
4564 warning (_("Remote failure reply: %s"), rs
->buf
);
4566 case 'O': /* Console output. */
4567 remote_console_output (rs
->buf
+ 1);
4570 warning (_("Invalid remote reply: %s"), rs
->buf
);
4574 /* Acknowledge a pending stop reply that may have arrived in the
4576 if (pending_stop_reply
!= NULL
)
4577 remote_get_pending_stop_replies ();
4579 /* If indeed we noticed a stop reply, we're done. */
4580 stop_reply
= queued_stop_reply (ptid
);
4581 if (stop_reply
!= NULL
)
4582 return process_stop_reply (stop_reply
, status
);
4584 /* Still no event. If we're in asynchronous mode, then just
4585 return to the event loop. */
4586 if (remote_is_async_p ())
4588 status
->kind
= TARGET_WAITKIND_IGNORE
;
4589 return minus_one_ptid
;
4592 /* Otherwise, asynchronous mode is masked, so do a blocking
4594 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
4599 /* Wait until the remote machine stops, then return, storing status in
4600 STATUS just as `wait' would. */
4603 remote_wait_as (ptid_t ptid
, struct target_waitstatus
*status
)
4605 struct remote_state
*rs
= get_remote_state ();
4606 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4607 ptid_t event_ptid
= null_ptid
;
4609 int solibs_changed
= 0;
4611 struct stop_reply
*stop_reply
;
4613 status
->kind
= TARGET_WAITKIND_IGNORE
;
4614 status
->value
.integer
= 0;
4616 stop_reply
= queued_stop_reply (ptid
);
4617 if (stop_reply
!= NULL
)
4618 return process_stop_reply (stop_reply
, status
);
4620 if (rs
->cached_wait_status
)
4621 /* Use the cached wait status, but only once. */
4622 rs
->cached_wait_status
= 0;
4627 if (!target_is_async_p ())
4629 ofunc
= signal (SIGINT
, remote_interrupt
);
4630 /* If the user hit C-c before this packet, or between packets,
4631 pretend that it was hit right here. */
4635 remote_interrupt (SIGINT
);
4639 /* FIXME: cagney/1999-09-27: If we're in async mode we should
4640 _never_ wait for ever -> test on target_is_async_p().
4641 However, before we do that we need to ensure that the caller
4642 knows how to take the target into/out of async mode. */
4643 ret
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
4644 if (!target_is_async_p ())
4645 signal (SIGINT
, ofunc
);
4650 remote_stopped_by_watchpoint_p
= 0;
4652 /* We got something. */
4653 rs
->waiting_for_stop_reply
= 0;
4657 case 'E': /* Error of some sort. */
4658 /* We're out of sync with the target now. Did it continue or
4659 not? Not is more likely, so report a stop. */
4660 warning (_("Remote failure reply: %s"), buf
);
4661 status
->kind
= TARGET_WAITKIND_STOPPED
;
4662 status
->value
.sig
= TARGET_SIGNAL_0
;
4664 case 'F': /* File-I/O request. */
4665 remote_fileio_request (buf
);
4667 case 'T': case 'S': case 'X': case 'W':
4669 struct stop_reply
*stop_reply
;
4670 struct cleanup
*old_chain
;
4672 stop_reply
= stop_reply_xmalloc ();
4673 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4674 remote_parse_stop_reply (buf
, stop_reply
);
4675 discard_cleanups (old_chain
);
4676 event_ptid
= process_stop_reply (stop_reply
, status
);
4679 case 'O': /* Console output. */
4680 remote_console_output (buf
+ 1);
4682 /* The target didn't really stop; keep waiting. */
4683 rs
->waiting_for_stop_reply
= 1;
4687 if (last_sent_signal
!= TARGET_SIGNAL_0
)
4689 /* Zero length reply means that we tried 'S' or 'C' and the
4690 remote system doesn't support it. */
4691 target_terminal_ours_for_output ();
4693 ("Can't send signals to this remote system. %s not sent.\n",
4694 target_signal_to_name (last_sent_signal
));
4695 last_sent_signal
= TARGET_SIGNAL_0
;
4696 target_terminal_inferior ();
4698 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
4699 putpkt ((char *) buf
);
4701 /* We just told the target to resume, so a stop reply is in
4703 rs
->waiting_for_stop_reply
= 1;
4706 /* else fallthrough */
4708 warning (_("Invalid remote reply: %s"), buf
);
4710 rs
->waiting_for_stop_reply
= 1;
4714 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
4715 /* Nothing interesting happened. */
4716 return minus_one_ptid
;
4717 else if (status
->kind
!= TARGET_WAITKIND_EXITED
4718 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
4720 if (!ptid_equal (event_ptid
, null_ptid
))
4721 record_currthread (event_ptid
);
4723 event_ptid
= inferior_ptid
;
4726 /* A process exit. Invalidate our notion of current thread. */
4727 record_currthread (minus_one_ptid
);
4732 /* Wait until the remote machine stops, then return, storing status in
4733 STATUS just as `wait' would. */
4736 remote_wait (ptid_t ptid
, struct target_waitstatus
*status
)
4741 event_ptid
= remote_wait_ns (ptid
, status
);
4744 /* In synchronous mode, keep waiting until the target stops. In
4745 asynchronous mode, always return to the event loop. */
4749 event_ptid
= remote_wait_as (ptid
, status
);
4751 while (status
->kind
== TARGET_WAITKIND_IGNORE
4752 && !target_can_async_p ());
4755 if (target_can_async_p ())
4757 /* If there are are events left in the queue tell the event loop
4759 if (stop_reply_queue
)
4760 mark_async_event_handler (remote_async_inferior_event_token
);
4766 /* Fetch a single register using a 'p' packet. */
4769 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
4771 struct remote_state
*rs
= get_remote_state ();
4773 char regp
[MAX_REGISTER_SIZE
];
4776 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
4779 if (reg
->pnum
== -1)
4784 p
+= hexnumstr (p
, reg
->pnum
);
4786 remote_send (&rs
->buf
, &rs
->buf_size
);
4790 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
4794 case PACKET_UNKNOWN
:
4797 error (_("Could not fetch register \"%s\""),
4798 gdbarch_register_name (get_regcache_arch (regcache
), reg
->regnum
));
4801 /* If this register is unfetchable, tell the regcache. */
4804 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
4808 /* Otherwise, parse and supply the value. */
4814 error (_("fetch_register_using_p: early buf termination"));
4816 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
4819 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
4823 /* Fetch the registers included in the target's 'g' packet. */
4826 send_g_packet (void)
4828 struct remote_state
*rs
= get_remote_state ();
4833 sprintf (rs
->buf
, "g");
4834 remote_send (&rs
->buf
, &rs
->buf_size
);
4836 /* We can get out of synch in various cases. If the first character
4837 in the buffer is not a hex character, assume that has happened
4838 and try to fetch another packet to read. */
4839 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
4840 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
4841 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
4842 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
4845 fprintf_unfiltered (gdb_stdlog
,
4846 "Bad register packet; fetching a new packet\n");
4847 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4850 buf_len
= strlen (rs
->buf
);
4852 /* Sanity check the received packet. */
4853 if (buf_len
% 2 != 0)
4854 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
4860 process_g_packet (struct regcache
*regcache
)
4862 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
4863 struct remote_state
*rs
= get_remote_state ();
4864 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4869 buf_len
= strlen (rs
->buf
);
4871 /* Further sanity checks, with knowledge of the architecture. */
4872 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
4873 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
4875 /* Save the size of the packet sent to us by the target. It is used
4876 as a heuristic when determining the max size of packets that the
4877 target can safely receive. */
4878 if (rsa
->actual_register_packet_size
== 0)
4879 rsa
->actual_register_packet_size
= buf_len
;
4881 /* If this is smaller than we guessed the 'g' packet would be,
4882 update our records. A 'g' reply that doesn't include a register's
4883 value implies either that the register is not available, or that
4884 the 'p' packet must be used. */
4885 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
4887 rsa
->sizeof_g_packet
= buf_len
/ 2;
4889 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
4891 if (rsa
->regs
[i
].pnum
== -1)
4894 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
4895 rsa
->regs
[i
].in_g_packet
= 0;
4897 rsa
->regs
[i
].in_g_packet
= 1;
4901 regs
= alloca (rsa
->sizeof_g_packet
);
4903 /* Unimplemented registers read as all bits zero. */
4904 memset (regs
, 0, rsa
->sizeof_g_packet
);
4906 /* Reply describes registers byte by byte, each byte encoded as two
4907 hex characters. Suck them all up, then supply them to the
4908 register cacheing/storage mechanism. */
4911 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
4913 if (p
[0] == 0 || p
[1] == 0)
4914 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
4915 internal_error (__FILE__
, __LINE__
,
4916 "unexpected end of 'g' packet reply");
4918 if (p
[0] == 'x' && p
[1] == 'x')
4919 regs
[i
] = 0; /* 'x' */
4921 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
4927 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
4929 struct packet_reg
*r
= &rsa
->regs
[i
];
4932 if (r
->offset
* 2 >= strlen (rs
->buf
))
4933 /* This shouldn't happen - we adjusted in_g_packet above. */
4934 internal_error (__FILE__
, __LINE__
,
4935 "unexpected end of 'g' packet reply");
4936 else if (rs
->buf
[r
->offset
* 2] == 'x')
4938 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
4939 /* The register isn't available, mark it as such (at
4940 the same time setting the value to zero). */
4941 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
4944 regcache_raw_supply (regcache
, r
->regnum
,
4952 fetch_registers_using_g (struct regcache
*regcache
)
4955 process_g_packet (regcache
);
4959 remote_fetch_registers (struct regcache
*regcache
, int regnum
)
4961 struct remote_state
*rs
= get_remote_state ();
4962 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4965 set_general_thread (inferior_ptid
);
4969 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
4970 gdb_assert (reg
!= NULL
);
4972 /* If this register might be in the 'g' packet, try that first -
4973 we are likely to read more than one register. If this is the
4974 first 'g' packet, we might be overly optimistic about its
4975 contents, so fall back to 'p'. */
4976 if (reg
->in_g_packet
)
4978 fetch_registers_using_g (regcache
);
4979 if (reg
->in_g_packet
)
4983 if (fetch_register_using_p (regcache
, reg
))
4986 /* This register is not available. */
4987 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
4992 fetch_registers_using_g (regcache
);
4994 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
4995 if (!rsa
->regs
[i
].in_g_packet
)
4996 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
4998 /* This register is not available. */
4999 regcache_raw_supply (regcache
, i
, NULL
);
5003 /* Prepare to store registers. Since we may send them all (using a
5004 'G' request), we have to read out the ones we don't want to change
5008 remote_prepare_to_store (struct regcache
*regcache
)
5010 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5012 gdb_byte buf
[MAX_REGISTER_SIZE
];
5014 /* Make sure the entire registers array is valid. */
5015 switch (remote_protocol_packets
[PACKET_P
].support
)
5017 case PACKET_DISABLE
:
5018 case PACKET_SUPPORT_UNKNOWN
:
5019 /* Make sure all the necessary registers are cached. */
5020 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5021 if (rsa
->regs
[i
].in_g_packet
)
5022 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
5029 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
5030 packet was not recognized. */
5033 store_register_using_P (const struct regcache
*regcache
, struct packet_reg
*reg
)
5035 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5036 struct remote_state
*rs
= get_remote_state ();
5037 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5038 /* Try storing a single register. */
5039 char *buf
= rs
->buf
;
5040 gdb_byte regp
[MAX_REGISTER_SIZE
];
5043 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
5046 if (reg
->pnum
== -1)
5049 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
5050 p
= buf
+ strlen (buf
);
5051 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
5052 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
5053 remote_send (&rs
->buf
, &rs
->buf_size
);
5055 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
5060 error (_("Could not write register \"%s\""),
5061 gdbarch_register_name (gdbarch
, reg
->regnum
));
5062 case PACKET_UNKNOWN
:
5065 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
5069 /* Store register REGNUM, or all registers if REGNUM == -1, from the
5070 contents of the register cache buffer. FIXME: ignores errors. */
5073 store_registers_using_G (const struct regcache
*regcache
)
5075 struct remote_state
*rs
= get_remote_state ();
5076 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5080 /* Extract all the registers in the regcache copying them into a
5084 regs
= alloca (rsa
->sizeof_g_packet
);
5085 memset (regs
, 0, rsa
->sizeof_g_packet
);
5086 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5088 struct packet_reg
*r
= &rsa
->regs
[i
];
5090 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
5094 /* Command describes registers byte by byte,
5095 each byte encoded as two hex characters. */
5098 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
5100 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
5101 remote_send (&rs
->buf
, &rs
->buf_size
);
5104 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
5105 of the register cache buffer. FIXME: ignores errors. */
5108 remote_store_registers (struct regcache
*regcache
, int regnum
)
5110 struct remote_state
*rs
= get_remote_state ();
5111 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5114 set_general_thread (inferior_ptid
);
5118 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5119 gdb_assert (reg
!= NULL
);
5121 /* Always prefer to store registers using the 'P' packet if
5122 possible; we often change only a small number of registers.
5123 Sometimes we change a larger number; we'd need help from a
5124 higher layer to know to use 'G'. */
5125 if (store_register_using_P (regcache
, reg
))
5128 /* For now, don't complain if we have no way to write the
5129 register. GDB loses track of unavailable registers too
5130 easily. Some day, this may be an error. We don't have
5131 any way to read the register, either... */
5132 if (!reg
->in_g_packet
)
5135 store_registers_using_G (regcache
);
5139 store_registers_using_G (regcache
);
5141 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5142 if (!rsa
->regs
[i
].in_g_packet
)
5143 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
5144 /* See above for why we do not issue an error here. */
5149 /* Return the number of hex digits in num. */
5152 hexnumlen (ULONGEST num
)
5156 for (i
= 0; num
!= 0; i
++)
5162 /* Set BUF to the minimum number of hex digits representing NUM. */
5165 hexnumstr (char *buf
, ULONGEST num
)
5167 int len
= hexnumlen (num
);
5168 return hexnumnstr (buf
, num
, len
);
5172 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
5175 hexnumnstr (char *buf
, ULONGEST num
, int width
)
5181 for (i
= width
- 1; i
>= 0; i
--)
5183 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
5190 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
5193 remote_address_masked (CORE_ADDR addr
)
5195 int address_size
= remote_address_size
;
5196 /* If "remoteaddresssize" was not set, default to target address size. */
5198 address_size
= gdbarch_addr_bit (target_gdbarch
);
5200 if (address_size
> 0
5201 && address_size
< (sizeof (ULONGEST
) * 8))
5203 /* Only create a mask when that mask can safely be constructed
5204 in a ULONGEST variable. */
5206 mask
= (mask
<< address_size
) - 1;
5212 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
5213 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
5214 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
5215 (which may be more than *OUT_LEN due to escape characters). The
5216 total number of bytes in the output buffer will be at most
5220 remote_escape_output (const gdb_byte
*buffer
, int len
,
5221 gdb_byte
*out_buf
, int *out_len
,
5224 int input_index
, output_index
;
5227 for (input_index
= 0; input_index
< len
; input_index
++)
5229 gdb_byte b
= buffer
[input_index
];
5231 if (b
== '$' || b
== '#' || b
== '}')
5233 /* These must be escaped. */
5234 if (output_index
+ 2 > out_maxlen
)
5236 out_buf
[output_index
++] = '}';
5237 out_buf
[output_index
++] = b
^ 0x20;
5241 if (output_index
+ 1 > out_maxlen
)
5243 out_buf
[output_index
++] = b
;
5247 *out_len
= input_index
;
5248 return output_index
;
5251 /* Convert BUFFER, escaped data LEN bytes long, into binary data
5252 in OUT_BUF. Return the number of bytes written to OUT_BUF.
5253 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
5255 This function reverses remote_escape_output. It allows more
5256 escaped characters than that function does, in particular because
5257 '*' must be escaped to avoid the run-length encoding processing
5258 in reading packets. */
5261 remote_unescape_input (const gdb_byte
*buffer
, int len
,
5262 gdb_byte
*out_buf
, int out_maxlen
)
5264 int input_index
, output_index
;
5269 for (input_index
= 0; input_index
< len
; input_index
++)
5271 gdb_byte b
= buffer
[input_index
];
5273 if (output_index
+ 1 > out_maxlen
)
5275 warning (_("Received too much data from remote target;"
5276 " ignoring overflow."));
5277 return output_index
;
5282 out_buf
[output_index
++] = b
^ 0x20;
5288 out_buf
[output_index
++] = b
;
5292 error (_("Unmatched escape character in target response."));
5294 return output_index
;
5297 /* Determine whether the remote target supports binary downloading.
5298 This is accomplished by sending a no-op memory write of zero length
5299 to the target at the specified address. It does not suffice to send
5300 the whole packet, since many stubs strip the eighth bit and
5301 subsequently compute a wrong checksum, which causes real havoc with
5304 NOTE: This can still lose if the serial line is not eight-bit
5305 clean. In cases like this, the user should clear "remote
5309 check_binary_download (CORE_ADDR addr
)
5311 struct remote_state
*rs
= get_remote_state ();
5313 switch (remote_protocol_packets
[PACKET_X
].support
)
5315 case PACKET_DISABLE
:
5319 case PACKET_SUPPORT_UNKNOWN
:
5325 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5327 p
+= hexnumstr (p
, (ULONGEST
) 0);
5331 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
5332 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5334 if (rs
->buf
[0] == '\0')
5337 fprintf_unfiltered (gdb_stdlog
,
5338 "binary downloading NOT suppported by target\n");
5339 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
5344 fprintf_unfiltered (gdb_stdlog
,
5345 "binary downloading suppported by target\n");
5346 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
5353 /* Write memory data directly to the remote machine.
5354 This does not inform the data cache; the data cache uses this.
5355 HEADER is the starting part of the packet.
5356 MEMADDR is the address in the remote memory space.
5357 MYADDR is the address of the buffer in our space.
5358 LEN is the number of bytes.
5359 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
5360 should send data as binary ('X'), or hex-encoded ('M').
5362 The function creates packet of the form
5363 <HEADER><ADDRESS>,<LENGTH>:<DATA>
5365 where encoding of <DATA> is termined by PACKET_FORMAT.
5367 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
5370 Returns the number of bytes transferred, or 0 (setting errno) for
5371 error. Only transfer a single packet. */
5374 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
5375 const gdb_byte
*myaddr
, int len
,
5376 char packet_format
, int use_length
)
5378 struct remote_state
*rs
= get_remote_state ();
5388 if (packet_format
!= 'X' && packet_format
!= 'M')
5389 internal_error (__FILE__
, __LINE__
,
5390 "remote_write_bytes_aux: bad packet format");
5395 payload_size
= get_memory_write_packet_size ();
5397 /* The packet buffer will be large enough for the payload;
5398 get_memory_packet_size ensures this. */
5401 /* Compute the size of the actual payload by subtracting out the
5402 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
5404 payload_size
-= strlen ("$,:#NN");
5406 /* The comma won't be used. */
5408 header_length
= strlen (header
);
5409 payload_size
-= header_length
;
5410 payload_size
-= hexnumlen (memaddr
);
5412 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
5414 strcat (rs
->buf
, header
);
5415 p
= rs
->buf
+ strlen (header
);
5417 /* Compute a best guess of the number of bytes actually transfered. */
5418 if (packet_format
== 'X')
5420 /* Best guess at number of bytes that will fit. */
5421 todo
= min (len
, payload_size
);
5423 payload_size
-= hexnumlen (todo
);
5424 todo
= min (todo
, payload_size
);
5428 /* Num bytes that will fit. */
5429 todo
= min (len
, payload_size
/ 2);
5431 payload_size
-= hexnumlen (todo
);
5432 todo
= min (todo
, payload_size
/ 2);
5436 internal_error (__FILE__
, __LINE__
,
5437 _("minumum packet size too small to write data"));
5439 /* If we already need another packet, then try to align the end
5440 of this packet to a useful boundary. */
5441 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
5442 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
5444 /* Append "<memaddr>". */
5445 memaddr
= remote_address_masked (memaddr
);
5446 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
5453 /* Append <len>. Retain the location/size of <len>. It may need to
5454 be adjusted once the packet body has been created. */
5456 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
5464 /* Append the packet body. */
5465 if (packet_format
== 'X')
5467 /* Binary mode. Send target system values byte by byte, in
5468 increasing byte addresses. Only escape certain critical
5470 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
5473 /* If not all TODO bytes fit, then we'll need another packet. Make
5474 a second try to keep the end of the packet aligned. Don't do
5475 this if the packet is tiny. */
5476 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
5480 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
5482 if (new_nr_bytes
!= nr_bytes
)
5483 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
5488 p
+= payload_length
;
5489 if (use_length
&& nr_bytes
< todo
)
5491 /* Escape chars have filled up the buffer prematurely,
5492 and we have actually sent fewer bytes than planned.
5493 Fix-up the length field of the packet. Use the same
5494 number of characters as before. */
5495 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
5496 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
5501 /* Normal mode: Send target system values byte by byte, in
5502 increasing byte addresses. Each byte is encoded as a two hex
5504 nr_bytes
= bin2hex (myaddr
, p
, todo
);
5508 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
5509 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5511 if (rs
->buf
[0] == 'E')
5513 /* There is no correspondance between what the remote protocol
5514 uses for errors and errno codes. We would like a cleaner way
5515 of representing errors (big enough to include errno codes,
5516 bfd_error codes, and others). But for now just return EIO. */
5521 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
5522 fewer bytes than we'd planned. */
5526 /* Write memory data directly to the remote machine.
5527 This does not inform the data cache; the data cache uses this.
5528 MEMADDR is the address in the remote memory space.
5529 MYADDR is the address of the buffer in our space.
5530 LEN is the number of bytes.
5532 Returns number of bytes transferred, or 0 (setting errno) for
5533 error. Only transfer a single packet. */
5536 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
5538 char *packet_format
= 0;
5540 /* Check whether the target supports binary download. */
5541 check_binary_download (memaddr
);
5543 switch (remote_protocol_packets
[PACKET_X
].support
)
5546 packet_format
= "X";
5548 case PACKET_DISABLE
:
5549 packet_format
= "M";
5551 case PACKET_SUPPORT_UNKNOWN
:
5552 internal_error (__FILE__
, __LINE__
,
5553 _("remote_write_bytes: bad internal state"));
5555 internal_error (__FILE__
, __LINE__
, _("bad switch"));
5558 return remote_write_bytes_aux (packet_format
,
5559 memaddr
, myaddr
, len
, packet_format
[0], 1);
5562 /* Read memory data directly from the remote machine.
5563 This does not use the data cache; the data cache uses this.
5564 MEMADDR is the address in the remote memory space.
5565 MYADDR is the address of the buffer in our space.
5566 LEN is the number of bytes.
5568 Returns number of bytes transferred, or 0 for error. */
5570 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
5571 remote targets) shouldn't attempt to read the entire buffer.
5572 Instead it should read a single packet worth of data and then
5573 return the byte size of that packet to the caller. The caller (its
5574 caller and its callers caller ;-) already contains code for
5575 handling partial reads. */
5578 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
5580 struct remote_state
*rs
= get_remote_state ();
5581 int max_buf_size
; /* Max size of packet output buffer. */
5587 max_buf_size
= get_memory_read_packet_size ();
5588 /* The packet buffer will be large enough for the payload;
5589 get_memory_packet_size ensures this. */
5598 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
5600 /* construct "m"<memaddr>","<len>" */
5601 /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */
5602 memaddr
= remote_address_masked (memaddr
);
5605 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
5607 p
+= hexnumstr (p
, (ULONGEST
) todo
);
5611 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5613 if (rs
->buf
[0] == 'E'
5614 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
5615 && rs
->buf
[3] == '\0')
5617 /* There is no correspondance between what the remote
5618 protocol uses for errors and errno codes. We would like
5619 a cleaner way of representing errors (big enough to
5620 include errno codes, bfd_error codes, and others). But
5621 for now just return EIO. */
5626 /* Reply describes memory byte by byte,
5627 each byte encoded as two hex characters. */
5630 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
5632 /* Reply is short. This means that we were able to read
5633 only part of what we wanted to. */
5634 return i
+ (origlen
- len
);
5644 /* Remote notification handler. */
5647 handle_notification (char *buf
, size_t length
)
5649 if (strncmp (buf
, "Stop:", 5) == 0)
5651 if (pending_stop_reply
)
5652 /* We've already parsed the in-flight stop-reply, but the stub
5653 for some reason thought we didn't, possibly due to timeout
5654 on its side. Just ignore it. */
5658 struct cleanup
*old_chain
;
5659 struct stop_reply
*reply
= stop_reply_xmalloc ();
5660 old_chain
= make_cleanup (do_stop_reply_xfree
, reply
);
5662 remote_parse_stop_reply (buf
+ 5, reply
);
5664 discard_cleanups (old_chain
);
5666 /* Be careful to only set it after parsing, since an error
5667 may be thrown then. */
5668 pending_stop_reply
= reply
;
5670 /* Notify the event loop there's a stop reply to acknowledge
5671 and that there may be more events to fetch. */
5672 mark_async_event_handler (remote_async_get_pending_events_token
);
5676 /* We ignore notifications we don't recognize, for compatibility
5677 with newer stubs. */
5682 /* Read or write LEN bytes from inferior memory at MEMADDR,
5683 transferring to or from debugger address BUFFER. Write to inferior
5684 if SHOULD_WRITE is nonzero. Returns length of data written or
5685 read; 0 for error. TARGET is unused. */
5688 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
5689 int should_write
, struct mem_attrib
*attrib
,
5690 struct target_ops
*target
)
5694 set_general_thread (inferior_ptid
);
5697 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
5699 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
5704 /* Sends a packet with content determined by the printf format string
5705 FORMAT and the remaining arguments, then gets the reply. Returns
5706 whether the packet was a success, a failure, or unknown. */
5709 remote_send_printf (const char *format
, ...)
5711 struct remote_state
*rs
= get_remote_state ();
5712 int max_size
= get_remote_packet_size ();
5715 va_start (ap
, format
);
5718 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
5719 internal_error (__FILE__
, __LINE__
, "Too long remote packet.");
5721 if (putpkt (rs
->buf
) < 0)
5722 error (_("Communication problem with target."));
5725 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5727 return packet_check_result (rs
->buf
);
5731 restore_remote_timeout (void *p
)
5733 int value
= *(int *)p
;
5734 remote_timeout
= value
;
5737 /* Flash writing can take quite some time. We'll set
5738 effectively infinite timeout for flash operations.
5739 In future, we'll need to decide on a better approach. */
5740 static const int remote_flash_timeout
= 1000;
5743 remote_flash_erase (struct target_ops
*ops
,
5744 ULONGEST address
, LONGEST length
)
5746 int saved_remote_timeout
= remote_timeout
;
5747 enum packet_result ret
;
5749 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5750 &saved_remote_timeout
);
5751 remote_timeout
= remote_flash_timeout
;
5753 ret
= remote_send_printf ("vFlashErase:%s,%s",
5758 case PACKET_UNKNOWN
:
5759 error (_("Remote target does not support flash erase"));
5761 error (_("Error erasing flash with vFlashErase packet"));
5766 do_cleanups (back_to
);
5770 remote_flash_write (struct target_ops
*ops
,
5771 ULONGEST address
, LONGEST length
,
5772 const gdb_byte
*data
)
5774 int saved_remote_timeout
= remote_timeout
;
5776 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5777 &saved_remote_timeout
);
5779 remote_timeout
= remote_flash_timeout
;
5780 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
5781 do_cleanups (back_to
);
5787 remote_flash_done (struct target_ops
*ops
)
5789 int saved_remote_timeout
= remote_timeout
;
5791 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5792 &saved_remote_timeout
);
5794 remote_timeout
= remote_flash_timeout
;
5795 ret
= remote_send_printf ("vFlashDone");
5796 do_cleanups (back_to
);
5800 case PACKET_UNKNOWN
:
5801 error (_("Remote target does not support vFlashDone"));
5803 error (_("Error finishing flash operation"));
5810 remote_files_info (struct target_ops
*ignore
)
5812 puts_filtered ("Debugging a target over a serial line.\n");
5815 /* Stuff for dealing with the packets which are part of this protocol.
5816 See comment at top of file for details. */
5818 /* Read a single character from the remote end. */
5821 readchar (int timeout
)
5825 ch
= serial_readchar (remote_desc
, timeout
);
5830 switch ((enum serial_rc
) ch
)
5834 error (_("Remote connection closed"));
5837 perror_with_name (_("Remote communication error"));
5839 case SERIAL_TIMEOUT
:
5845 /* Send the command in *BUF to the remote machine, and read the reply
5846 into *BUF. Report an error if we get an error reply. Resize
5847 *BUF using xrealloc if necessary to hold the result, and update
5851 remote_send (char **buf
,
5855 getpkt (buf
, sizeof_buf
, 0);
5857 if ((*buf
)[0] == 'E')
5858 error (_("Remote failure reply: %s"), *buf
);
5861 /* Return a pointer to an xmalloc'ed string representing an escaped
5862 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
5863 etc. The caller is responsible for releasing the returned
5867 escape_buffer (const char *buf
, int n
)
5869 struct cleanup
*old_chain
;
5870 struct ui_file
*stb
;
5874 stb
= mem_fileopen ();
5875 old_chain
= make_cleanup_ui_file_delete (stb
);
5877 fputstrn_unfiltered (buf
, n
, 0, stb
);
5878 str
= ui_file_xstrdup (stb
, &length
);
5879 do_cleanups (old_chain
);
5883 /* Display a null-terminated packet on stdout, for debugging, using C
5887 print_packet (char *buf
)
5889 puts_filtered ("\"");
5890 fputstr_filtered (buf
, '"', gdb_stdout
);
5891 puts_filtered ("\"");
5897 return putpkt_binary (buf
, strlen (buf
));
5900 /* Send a packet to the remote machine, with error checking. The data
5901 of the packet is in BUF. The string in BUF can be at most
5902 get_remote_packet_size () - 5 to account for the $, # and checksum,
5903 and for a possible /0 if we are debugging (remote_debug) and want
5904 to print the sent packet as a string. */
5907 putpkt_binary (char *buf
, int cnt
)
5909 struct remote_state
*rs
= get_remote_state ();
5911 unsigned char csum
= 0;
5912 char *buf2
= alloca (cnt
+ 6);
5918 /* Catch cases like trying to read memory or listing threads while
5919 we're waiting for a stop reply. The remote server wouldn't be
5920 ready to handle this request, so we'd hang and timeout. We don't
5921 have to worry about this in synchronous mode, because in that
5922 case it's not possible to issue a command while the target is
5923 running. This is not a problem in non-stop mode, because in that
5924 case, the stub is always ready to process serial input. */
5925 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
5926 error (_("Cannot execute this command while the target is running."));
5928 /* We're sending out a new packet. Make sure we don't look at a
5929 stale cached response. */
5930 rs
->cached_wait_status
= 0;
5932 /* Copy the packet into buffer BUF2, encapsulating it
5933 and giving it a checksum. */
5938 for (i
= 0; i
< cnt
; i
++)
5944 *p
++ = tohex ((csum
>> 4) & 0xf);
5945 *p
++ = tohex (csum
& 0xf);
5947 /* Send it over and over until we get a positive ack. */
5951 int started_error_output
= 0;
5955 struct cleanup
*old_chain
;
5959 str
= escape_buffer (buf2
, p
- buf2
);
5960 old_chain
= make_cleanup (xfree
, str
);
5961 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", str
);
5962 gdb_flush (gdb_stdlog
);
5963 do_cleanups (old_chain
);
5965 if (serial_write (remote_desc
, buf2
, p
- buf2
))
5966 perror_with_name (_("putpkt: write failed"));
5968 /* If this is a no acks version of the remote protocol, send the
5969 packet and move on. */
5973 /* Read until either a timeout occurs (-2) or '+' is read.
5974 Handle any notification that arrives in the mean time. */
5977 ch
= readchar (remote_timeout
);
5985 case SERIAL_TIMEOUT
:
5988 if (started_error_output
)
5990 putchar_unfiltered ('\n');
5991 started_error_output
= 0;
6000 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
6004 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
6005 case SERIAL_TIMEOUT
:
6009 break; /* Retransmit buffer. */
6013 fprintf_unfiltered (gdb_stdlog
,
6014 "Packet instead of Ack, ignoring it\n");
6015 /* It's probably an old response sent because an ACK
6016 was lost. Gobble up the packet and ack it so it
6017 doesn't get retransmitted when we resend this
6020 serial_write (remote_desc
, "+", 1);
6021 continue; /* Now, go look for +. */
6028 /* If we got a notification, handle it, and go back to looking
6030 /* We've found the start of a notification. Now
6031 collect the data. */
6032 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
6037 struct cleanup
*old_chain
;
6040 str
= escape_buffer (rs
->buf
, val
);
6041 old_chain
= make_cleanup (xfree
, str
);
6042 fprintf_unfiltered (gdb_stdlog
,
6043 " Notification received: %s\n",
6045 do_cleanups (old_chain
);
6047 handle_notification (rs
->buf
, val
);
6048 /* We're in sync now, rewait for the ack. */
6055 if (!started_error_output
)
6057 started_error_output
= 1;
6058 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6060 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6061 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
6070 if (!started_error_output
)
6072 started_error_output
= 1;
6073 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6075 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6079 break; /* Here to retransmit. */
6083 /* This is wrong. If doing a long backtrace, the user should be
6084 able to get out next time we call QUIT, without anything as
6085 violent as interrupt_query. If we want to provide a way out of
6086 here without getting to the next QUIT, it should be based on
6087 hitting ^C twice as in remote_wait. */
6098 /* Come here after finding the start of a frame when we expected an
6099 ack. Do our best to discard the rest of this packet. */
6108 c
= readchar (remote_timeout
);
6111 case SERIAL_TIMEOUT
:
6112 /* Nothing we can do. */
6115 /* Discard the two bytes of checksum and stop. */
6116 c
= readchar (remote_timeout
);
6118 c
= readchar (remote_timeout
);
6121 case '*': /* Run length encoding. */
6122 /* Discard the repeat count. */
6123 c
= readchar (remote_timeout
);
6128 /* A regular character. */
6134 /* Come here after finding the start of the frame. Collect the rest
6135 into *BUF, verifying the checksum, length, and handling run-length
6136 compression. NUL terminate the buffer. If there is not enough room,
6137 expand *BUF using xrealloc.
6139 Returns -1 on error, number of characters in buffer (ignoring the
6140 trailing NULL) on success. (could be extended to return one of the
6141 SERIAL status indications). */
6144 read_frame (char **buf_p
,
6151 struct remote_state
*rs
= get_remote_state ();
6158 c
= readchar (remote_timeout
);
6161 case SERIAL_TIMEOUT
:
6163 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
6167 fputs_filtered ("Saw new packet start in middle of old one\n",
6169 return -1; /* Start a new packet, count retries. */
6172 unsigned char pktcsum
;
6178 check_0
= readchar (remote_timeout
);
6180 check_1
= readchar (remote_timeout
);
6182 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
6185 fputs_filtered ("Timeout in checksum, retrying\n",
6189 else if (check_0
< 0 || check_1
< 0)
6192 fputs_filtered ("Communication error in checksum\n",
6197 /* Don't recompute the checksum; with no ack packets we
6198 don't have any way to indicate a packet retransmission
6203 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
6204 if (csum
== pktcsum
)
6209 struct cleanup
*old_chain
;
6212 str
= escape_buffer (buf
, bc
);
6213 old_chain
= make_cleanup (xfree
, str
);
6214 fprintf_unfiltered (gdb_stdlog
,
6216 Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
6217 pktcsum
, csum
, str
);
6218 do_cleanups (old_chain
);
6220 /* Number of characters in buffer ignoring trailing
6224 case '*': /* Run length encoding. */
6229 c
= readchar (remote_timeout
);
6231 repeat
= c
- ' ' + 3; /* Compute repeat count. */
6233 /* The character before ``*'' is repeated. */
6235 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
6237 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
6239 /* Make some more room in the buffer. */
6240 *sizeof_buf
+= repeat
;
6241 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
6245 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
6251 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
6255 if (bc
>= *sizeof_buf
- 1)
6257 /* Make some more room in the buffer. */
6259 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
6270 /* Read a packet from the remote machine, with error checking, and
6271 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
6272 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
6273 rather than timing out; this is used (in synchronous mode) to wait
6274 for a target that is is executing user code to stop. */
6275 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
6276 don't have to change all the calls to getpkt to deal with the
6277 return value, because at the moment I don't know what the right
6278 thing to do it for those. */
6286 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
6290 /* Read a packet from the remote machine, with error checking, and
6291 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
6292 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
6293 rather than timing out; this is used (in synchronous mode) to wait
6294 for a target that is is executing user code to stop. If FOREVER ==
6295 0, this function is allowed to time out gracefully and return an
6296 indication of this to the caller. Otherwise return the number of
6297 bytes read. If EXPECTING_NOTIF, consider receiving a notification
6298 enough reason to return to the caller. */
6301 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
6302 int expecting_notif
)
6304 struct remote_state
*rs
= get_remote_state ();
6310 /* We're reading a new response. Make sure we don't look at a
6311 previously cached response. */
6312 rs
->cached_wait_status
= 0;
6314 strcpy (*buf
, "timeout");
6317 timeout
= watchdog
> 0 ? watchdog
: -1;
6318 else if (expecting_notif
)
6319 timeout
= 0; /* There should already be a char in the buffer. If
6322 timeout
= remote_timeout
;
6326 /* Process any number of notifications, and then return when
6330 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
6332 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
6334 /* This can loop forever if the remote side sends us
6335 characters continuously, but if it pauses, we'll get
6336 SERIAL_TIMEOUT from readchar because of timeout. Then
6337 we'll count that as a retry.
6339 Note that even when forever is set, we will only wait
6340 forever prior to the start of a packet. After that, we
6341 expect characters to arrive at a brisk pace. They should
6342 show up within remote_timeout intervals. */
6344 c
= readchar (timeout
);
6345 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
6347 if (c
== SERIAL_TIMEOUT
)
6349 if (expecting_notif
)
6350 return -1; /* Don't complain, it's normal to not get
6351 anything in this case. */
6353 if (forever
) /* Watchdog went off? Kill the target. */
6357 error (_("Watchdog timeout has expired. Target detached."));
6360 fputs_filtered ("Timed out.\n", gdb_stdlog
);
6364 /* We've found the start of a packet or notification.
6365 Now collect the data. */
6366 val
= read_frame (buf
, sizeof_buf
);
6371 serial_write (remote_desc
, "-", 1);
6374 if (tries
> MAX_TRIES
)
6376 /* We have tried hard enough, and just can't receive the
6377 packet/notification. Give up. */
6378 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
6380 /* Skip the ack char if we're in no-ack mode. */
6381 if (!rs
->noack_mode
)
6382 serial_write (remote_desc
, "+", 1);
6386 /* If we got an ordinary packet, return that to our caller. */
6391 struct cleanup
*old_chain
;
6394 str
= escape_buffer (*buf
, val
);
6395 old_chain
= make_cleanup (xfree
, str
);
6396 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", str
);
6397 do_cleanups (old_chain
);
6400 /* Skip the ack char if we're in no-ack mode. */
6401 if (!rs
->noack_mode
)
6402 serial_write (remote_desc
, "+", 1);
6406 /* If we got a notification, handle it, and go back to looking
6410 gdb_assert (c
== '%');
6414 struct cleanup
*old_chain
;
6417 str
= escape_buffer (*buf
, val
);
6418 old_chain
= make_cleanup (xfree
, str
);
6419 fprintf_unfiltered (gdb_stdlog
,
6420 " Notification received: %s\n",
6422 do_cleanups (old_chain
);
6425 handle_notification (*buf
, val
);
6427 /* Notifications require no acknowledgement. */
6429 if (expecting_notif
)
6436 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
6438 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0);
6442 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
)
6444 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1);
6451 /* Use catch_errors so the user can quit from gdb even when we
6452 aren't on speaking terms with the remote system. */
6453 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
6455 /* Don't wait for it to die. I'm not really sure it matters whether
6456 we do or not. For the existing stubs, kill is a noop. */
6457 target_mourn_inferior ();
6461 remote_vkill (int pid
, struct remote_state
*rs
)
6463 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
6466 /* Tell the remote target to detach. */
6467 sprintf (rs
->buf
, "vKill;%x", pid
);
6469 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6471 if (packet_ok (rs
->buf
,
6472 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
6474 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
6481 extended_remote_kill (void)
6484 int pid
= ptid_get_pid (inferior_ptid
);
6485 struct remote_state
*rs
= get_remote_state ();
6487 res
= remote_vkill (pid
, rs
);
6488 if (res
== -1 && !remote_multi_process_p (rs
))
6490 /* Don't try 'k' on a multi-process aware stub -- it has no way
6491 to specify the pid. */
6495 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6496 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
6499 /* Don't wait for it to die. I'm not really sure it matters whether
6500 we do or not. For the existing stubs, kill is a noop. */
6506 error (_("Can't kill process"));
6508 target_mourn_inferior ();
6512 remote_mourn (struct target_ops
*ops
)
6514 remote_mourn_1 (ops
);
6517 /* Worker function for remote_mourn. */
6519 remote_mourn_1 (struct target_ops
*target
)
6521 unpush_target (target
);
6523 /* remote_close takes care of cleaning up. */
6527 select_new_thread_callback (struct thread_info
*th
, void* data
)
6529 if (!is_exited (th
->ptid
))
6531 switch_to_thread (th
->ptid
);
6532 printf_filtered (_("[Switching to %s]\n"),
6533 target_pid_to_str (inferior_ptid
));
6540 extended_remote_mourn_1 (struct target_ops
*target
)
6542 struct remote_state
*rs
= get_remote_state ();
6544 /* In case we got here due to an error, but we're going to stay
6546 rs
->waiting_for_stop_reply
= 0;
6548 /* We're no longer interested in these events. */
6549 discard_pending_stop_replies (ptid_get_pid (inferior_ptid
));
6551 /* Unlike "target remote", we do not want to unpush the target; then
6552 the next time the user says "run", we won't be connected. */
6554 /* Call common code to mark the inferior as not running. */
6555 generic_mourn_inferior ();
6557 if (have_inferiors ())
6559 extern void nullify_last_target_wait_ptid ();
6560 /* Multi-process case. The current process has exited, but
6561 there are other processes to debug. Switch to the first
6563 iterate_over_threads (select_new_thread_callback
, NULL
);
6564 nullify_last_target_wait_ptid ();
6568 if (!remote_multi_process_p (rs
))
6570 /* Check whether the target is running now - some remote stubs
6571 automatically restart after kill. */
6573 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6575 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
6577 /* Assume that the target has been restarted. Set inferior_ptid
6578 so that bits of core GDB realizes there's something here, e.g.,
6579 so that the user can say "kill" again. */
6580 inferior_ptid
= magic_null_ptid
;
6584 /* Mark this (still pushed) target as not executable until we
6586 target_mark_exited (target
);
6590 /* Always remove execution if this was the last process. */
6591 target_mark_exited (target
);
6596 extended_remote_mourn (struct target_ops
*ops
)
6598 extended_remote_mourn_1 (ops
);
6602 extended_remote_run (char *args
)
6604 struct remote_state
*rs
= get_remote_state ();
6608 /* If the user has disabled vRun support, or we have detected that
6609 support is not available, do not try it. */
6610 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
6613 strcpy (rs
->buf
, "vRun;");
6614 len
= strlen (rs
->buf
);
6616 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
6617 error (_("Remote file name too long for run packet"));
6618 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
, 0);
6620 gdb_assert (args
!= NULL
);
6623 struct cleanup
*back_to
;
6627 argv
= gdb_buildargv (args
);
6628 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
6629 for (i
= 0; argv
[i
] != NULL
; i
++)
6631 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
6632 error (_("Argument list too long for run packet"));
6633 rs
->buf
[len
++] = ';';
6634 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
, 0);
6636 do_cleanups (back_to
);
6639 rs
->buf
[len
++] = '\0';
6642 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6644 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
6646 /* We have a wait response; we don't need it, though. All is well. */
6649 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
6650 /* It wasn't disabled before, but it is now. */
6654 if (remote_exec_file
[0] == '\0')
6655 error (_("Running the default executable on the remote target failed; "
6656 "try \"set remote exec-file\"?"));
6658 error (_("Running \"%s\" on the remote target failed"),
6663 /* In the extended protocol we want to be able to do things like
6664 "run" and have them basically work as expected. So we need
6665 a special create_inferior function. We support changing the
6666 executable file and the command line arguments, but not the
6670 extended_remote_create_inferior_1 (char *exec_file
, char *args
,
6671 char **env
, int from_tty
)
6673 /* If running asynchronously, register the target file descriptor
6674 with the event loop. */
6675 if (target_can_async_p ())
6676 target_async (inferior_event_handler
, 0);
6678 /* Now restart the remote server. */
6679 if (extended_remote_run (args
) == -1)
6681 /* vRun was not supported. Fail if we need it to do what the
6683 if (remote_exec_file
[0])
6684 error (_("Remote target does not support \"set remote exec-file\""));
6686 error (_("Remote target does not support \"set args\" or run <ARGS>"));
6688 /* Fall back to "R". */
6689 extended_remote_restart ();
6692 /* Clean up from the last time we ran, before we mark the target
6693 running again. This will mark breakpoints uninserted, and
6694 get_offsets may insert breakpoints. */
6695 init_thread_list ();
6696 init_wait_for_inferior ();
6698 /* Now mark the inferior as running before we do anything else. */
6699 inferior_ptid
= magic_null_ptid
;
6701 /* Now, if we have thread information, update inferior_ptid. */
6702 inferior_ptid
= remote_current_thread (inferior_ptid
);
6704 add_inferior (ptid_get_pid (inferior_ptid
));
6705 add_thread_silent (inferior_ptid
);
6707 target_mark_running (&extended_remote_ops
);
6709 /* Get updated offsets, if the stub uses qOffsets. */
6714 extended_remote_create_inferior (struct target_ops
*ops
,
6715 char *exec_file
, char *args
,
6716 char **env
, int from_tty
)
6718 extended_remote_create_inferior_1 (exec_file
, args
, env
, from_tty
);
6722 /* Insert a breakpoint. On targets that have software breakpoint
6723 support, we ask the remote target to do the work; on targets
6724 which don't, we insert a traditional memory breakpoint. */
6727 remote_insert_breakpoint (struct bp_target_info
*bp_tgt
)
6729 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
6730 If it succeeds, then set the support to PACKET_ENABLE. If it
6731 fails, and the user has explicitly requested the Z support then
6732 report an error, otherwise, mark it disabled and go on. */
6734 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
6736 CORE_ADDR addr
= bp_tgt
->placed_address
;
6737 struct remote_state
*rs
;
6741 gdbarch_breakpoint_from_pc (target_gdbarch
, &addr
, &bpsize
);
6743 rs
= get_remote_state ();
6749 addr
= (ULONGEST
) remote_address_masked (addr
);
6750 p
+= hexnumstr (p
, addr
);
6751 sprintf (p
, ",%d", bpsize
);
6754 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6756 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
6761 bp_tgt
->placed_address
= addr
;
6762 bp_tgt
->placed_size
= bpsize
;
6764 case PACKET_UNKNOWN
:
6769 return memory_insert_breakpoint (bp_tgt
);
6773 remote_remove_breakpoint (struct bp_target_info
*bp_tgt
)
6775 CORE_ADDR addr
= bp_tgt
->placed_address
;
6776 struct remote_state
*rs
= get_remote_state ();
6779 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
6787 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
6788 p
+= hexnumstr (p
, addr
);
6789 sprintf (p
, ",%d", bp_tgt
->placed_size
);
6792 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6794 return (rs
->buf
[0] == 'E');
6797 return memory_remove_breakpoint (bp_tgt
);
6801 watchpoint_to_Z_packet (int type
)
6806 return Z_PACKET_WRITE_WP
;
6809 return Z_PACKET_READ_WP
;
6812 return Z_PACKET_ACCESS_WP
;
6815 internal_error (__FILE__
, __LINE__
,
6816 _("hw_bp_to_z: bad watchpoint type %d"), type
);
6821 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
6823 struct remote_state
*rs
= get_remote_state ();
6825 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
6827 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
6830 sprintf (rs
->buf
, "Z%x,", packet
);
6831 p
= strchr (rs
->buf
, '\0');
6832 addr
= remote_address_masked (addr
);
6833 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6834 sprintf (p
, ",%x", len
);
6837 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6839 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
6842 case PACKET_UNKNOWN
:
6847 internal_error (__FILE__
, __LINE__
,
6848 _("remote_insert_watchpoint: reached end of function"));
6853 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
6855 struct remote_state
*rs
= get_remote_state ();
6857 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
6859 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
6862 sprintf (rs
->buf
, "z%x,", packet
);
6863 p
= strchr (rs
->buf
, '\0');
6864 addr
= remote_address_masked (addr
);
6865 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6866 sprintf (p
, ",%x", len
);
6868 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6870 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
6873 case PACKET_UNKNOWN
:
6878 internal_error (__FILE__
, __LINE__
,
6879 _("remote_remove_watchpoint: reached end of function"));
6883 int remote_hw_watchpoint_limit
= -1;
6884 int remote_hw_breakpoint_limit
= -1;
6887 remote_check_watch_resources (int type
, int cnt
, int ot
)
6889 if (type
== bp_hardware_breakpoint
)
6891 if (remote_hw_breakpoint_limit
== 0)
6893 else if (remote_hw_breakpoint_limit
< 0)
6895 else if (cnt
<= remote_hw_breakpoint_limit
)
6900 if (remote_hw_watchpoint_limit
== 0)
6902 else if (remote_hw_watchpoint_limit
< 0)
6906 else if (cnt
<= remote_hw_watchpoint_limit
)
6913 remote_stopped_by_watchpoint (void)
6915 return remote_stopped_by_watchpoint_p
;
6919 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
6922 if (remote_stopped_by_watchpoint ())
6924 *addr_p
= remote_watch_data_address
;
6933 remote_insert_hw_breakpoint (struct bp_target_info
*bp_tgt
)
6936 struct remote_state
*rs
;
6939 /* The length field should be set to the size of a breakpoint
6940 instruction, even though we aren't inserting one ourselves. */
6942 gdbarch_breakpoint_from_pc
6943 (target_gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
6945 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
6948 rs
= get_remote_state ();
6955 addr
= remote_address_masked (bp_tgt
->placed_address
);
6956 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6957 sprintf (p
, ",%x", bp_tgt
->placed_size
);
6960 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6962 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
6965 case PACKET_UNKNOWN
:
6970 internal_error (__FILE__
, __LINE__
,
6971 _("remote_insert_hw_breakpoint: reached end of function"));
6976 remote_remove_hw_breakpoint (struct bp_target_info
*bp_tgt
)
6979 struct remote_state
*rs
= get_remote_state ();
6982 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
6989 addr
= remote_address_masked (bp_tgt
->placed_address
);
6990 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6991 sprintf (p
, ",%x", bp_tgt
->placed_size
);
6994 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6996 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
6999 case PACKET_UNKNOWN
:
7004 internal_error (__FILE__
, __LINE__
,
7005 _("remote_remove_hw_breakpoint: reached end of function"));
7008 /* Table used by the crc32 function to calcuate the checksum. */
7010 static unsigned long crc32_table
[256] =
7013 static unsigned long
7014 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
7016 if (!crc32_table
[1])
7018 /* Initialize the CRC table and the decoding table. */
7022 for (i
= 0; i
< 256; i
++)
7024 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
7025 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
7032 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
7038 /* compare-sections command
7040 With no arguments, compares each loadable section in the exec bfd
7041 with the same memory range on the target, and reports mismatches.
7042 Useful for verifying the image on the target against the exec file.
7043 Depends on the target understanding the new "qCRC:" request. */
7045 /* FIXME: cagney/1999-10-26: This command should be broken down into a
7046 target method (target verify memory) and generic version of the
7047 actual command. This will allow other high-level code (especially
7048 generic_load()) to make use of this target functionality. */
7051 compare_sections_command (char *args
, int from_tty
)
7053 struct remote_state
*rs
= get_remote_state ();
7055 unsigned long host_crc
, target_crc
;
7056 extern bfd
*exec_bfd
;
7057 struct cleanup
*old_chain
;
7060 const char *sectname
;
7067 error (_("command cannot be used without an exec file"));
7068 if (!current_target
.to_shortname
||
7069 strcmp (current_target
.to_shortname
, "remote") != 0)
7070 error (_("command can only be used with remote target"));
7072 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
7074 if (!(s
->flags
& SEC_LOAD
))
7075 continue; /* skip non-loadable section */
7077 size
= bfd_get_section_size (s
);
7079 continue; /* skip zero-length section */
7081 sectname
= bfd_get_section_name (exec_bfd
, s
);
7082 if (args
&& strcmp (args
, sectname
) != 0)
7083 continue; /* not the section selected by user */
7085 matched
= 1; /* do this section */
7087 /* FIXME: assumes lma can fit into long. */
7088 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
7089 (long) lma
, (long) size
);
7092 /* Be clever; compute the host_crc before waiting for target
7094 sectdata
= xmalloc (size
);
7095 old_chain
= make_cleanup (xfree
, sectdata
);
7096 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
7097 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
7099 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7100 if (rs
->buf
[0] == 'E')
7101 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
7102 sectname
, paddr (lma
), paddr (lma
+ size
));
7103 if (rs
->buf
[0] != 'C')
7104 error (_("remote target does not support this operation"));
7106 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
7107 target_crc
= target_crc
* 16 + fromhex (*tmp
);
7109 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
7110 sectname
, paddr (lma
), paddr (lma
+ size
));
7111 if (host_crc
== target_crc
)
7112 printf_filtered ("matched.\n");
7115 printf_filtered ("MIS-MATCHED!\n");
7119 do_cleanups (old_chain
);
7122 warning (_("One or more sections of the remote executable does not match\n\
7123 the loaded file\n"));
7124 if (args
&& !matched
)
7125 printf_filtered (_("No loaded section named '%s'.\n"), args
);
7128 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
7129 into remote target. The number of bytes written to the remote
7130 target is returned, or -1 for error. */
7133 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
7134 const char *annex
, const gdb_byte
*writebuf
,
7135 ULONGEST offset
, LONGEST len
,
7136 struct packet_config
*packet
)
7141 struct remote_state
*rs
= get_remote_state ();
7142 int max_size
= get_memory_write_packet_size ();
7144 if (packet
->support
== PACKET_DISABLE
)
7147 /* Insert header. */
7148 i
= snprintf (rs
->buf
, max_size
,
7149 "qXfer:%s:write:%s:%s:",
7150 object_name
, annex
? annex
: "",
7151 phex_nz (offset
, sizeof offset
));
7152 max_size
-= (i
+ 1);
7154 /* Escape as much data as fits into rs->buf. */
7155 buf_len
= remote_escape_output
7156 (writebuf
, len
, (rs
->buf
+ i
), &max_size
, max_size
);
7158 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
7159 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
7160 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7163 unpack_varlen_hex (rs
->buf
, &n
);
7167 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
7168 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
7169 number of bytes read is returned, or 0 for EOF, or -1 for error.
7170 The number of bytes read may be less than LEN without indicating an
7171 EOF. PACKET is checked and updated to indicate whether the remote
7172 target supports this object. */
7175 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
7177 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
7178 struct packet_config
*packet
)
7180 static char *finished_object
;
7181 static char *finished_annex
;
7182 static ULONGEST finished_offset
;
7184 struct remote_state
*rs
= get_remote_state ();
7185 unsigned int total
= 0;
7186 LONGEST i
, n
, packet_len
;
7188 if (packet
->support
== PACKET_DISABLE
)
7191 /* Check whether we've cached an end-of-object packet that matches
7193 if (finished_object
)
7195 if (strcmp (object_name
, finished_object
) == 0
7196 && strcmp (annex
? annex
: "", finished_annex
) == 0
7197 && offset
== finished_offset
)
7200 /* Otherwise, we're now reading something different. Discard
7202 xfree (finished_object
);
7203 xfree (finished_annex
);
7204 finished_object
= NULL
;
7205 finished_annex
= NULL
;
7208 /* Request only enough to fit in a single packet. The actual data
7209 may not, since we don't know how much of it will need to be escaped;
7210 the target is free to respond with slightly less data. We subtract
7211 five to account for the response type and the protocol frame. */
7212 n
= min (get_remote_packet_size () - 5, len
);
7213 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
7214 object_name
, annex
? annex
: "",
7215 phex_nz (offset
, sizeof offset
),
7216 phex_nz (n
, sizeof n
));
7217 i
= putpkt (rs
->buf
);
7222 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
7223 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7226 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
7227 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
7229 /* 'm' means there is (or at least might be) more data after this
7230 batch. That does not make sense unless there's at least one byte
7231 of data in this reply. */
7232 if (rs
->buf
[0] == 'm' && packet_len
== 1)
7233 error (_("Remote qXfer reply contained no data."));
7235 /* Got some data. */
7236 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
7238 /* 'l' is an EOF marker, possibly including a final block of data,
7239 or possibly empty. If we have the final block of a non-empty
7240 object, record this fact to bypass a subsequent partial read. */
7241 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
7243 finished_object
= xstrdup (object_name
);
7244 finished_annex
= xstrdup (annex
? annex
: "");
7245 finished_offset
= offset
+ i
;
7252 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
7253 const char *annex
, gdb_byte
*readbuf
,
7254 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
7256 struct remote_state
*rs
;
7261 set_general_thread (inferior_ptid
);
7263 rs
= get_remote_state ();
7265 /* Handle memory using the standard memory routines. */
7266 if (object
== TARGET_OBJECT_MEMORY
)
7271 /* If the remote target is connected but not running, we should
7272 pass this request down to a lower stratum (e.g. the executable
7274 if (!target_has_execution
)
7277 if (writebuf
!= NULL
)
7278 xfered
= remote_write_bytes (offset
, writebuf
, len
);
7280 xfered
= remote_read_bytes (offset
, readbuf
, len
);
7284 else if (xfered
== 0 && errno
== 0)
7290 /* Handle SPU memory using qxfer packets. */
7291 if (object
== TARGET_OBJECT_SPU
)
7294 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
7295 &remote_protocol_packets
7296 [PACKET_qXfer_spu_read
]);
7298 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
7299 &remote_protocol_packets
7300 [PACKET_qXfer_spu_write
]);
7303 /* Only handle flash writes. */
7304 if (writebuf
!= NULL
)
7310 case TARGET_OBJECT_FLASH
:
7311 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
7315 else if (xfered
== 0 && errno
== 0)
7325 /* Map pre-existing objects onto letters. DO NOT do this for new
7326 objects!!! Instead specify new query packets. */
7329 case TARGET_OBJECT_AVR
:
7333 case TARGET_OBJECT_AUXV
:
7334 gdb_assert (annex
== NULL
);
7335 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
7336 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
7338 case TARGET_OBJECT_AVAILABLE_FEATURES
:
7339 return remote_read_qxfer
7340 (ops
, "features", annex
, readbuf
, offset
, len
,
7341 &remote_protocol_packets
[PACKET_qXfer_features
]);
7343 case TARGET_OBJECT_LIBRARIES
:
7344 return remote_read_qxfer
7345 (ops
, "libraries", annex
, readbuf
, offset
, len
,
7346 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
7348 case TARGET_OBJECT_MEMORY_MAP
:
7349 gdb_assert (annex
== NULL
);
7350 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
7351 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
7353 case TARGET_OBJECT_OSDATA
:
7354 /* Should only get here if we're connected. */
7355 gdb_assert (remote_desc
);
7356 return remote_read_qxfer
7357 (ops
, "osdata", annex
, readbuf
, offset
, len
,
7358 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
7364 /* Note: a zero OFFSET and LEN can be used to query the minimum
7366 if (offset
== 0 && len
== 0)
7367 return (get_remote_packet_size ());
7368 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
7369 large enough let the caller deal with it. */
7370 if (len
< get_remote_packet_size ())
7372 len
= get_remote_packet_size ();
7374 /* Except for querying the minimum buffer size, target must be open. */
7376 error (_("remote query is only available after target open"));
7378 gdb_assert (annex
!= NULL
);
7379 gdb_assert (readbuf
!= NULL
);
7385 /* We used one buffer char for the remote protocol q command and
7386 another for the query type. As the remote protocol encapsulation
7387 uses 4 chars plus one extra in case we are debugging
7388 (remote_debug), we have PBUFZIZ - 7 left to pack the query
7391 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
7393 /* Bad caller may have sent forbidden characters. */
7394 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
7399 gdb_assert (annex
[i
] == '\0');
7401 i
= putpkt (rs
->buf
);
7405 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7406 strcpy ((char *) readbuf
, rs
->buf
);
7408 return strlen ((char *) readbuf
);
7412 remote_search_memory (struct target_ops
* ops
,
7413 CORE_ADDR start_addr
, ULONGEST search_space_len
,
7414 const gdb_byte
*pattern
, ULONGEST pattern_len
,
7415 CORE_ADDR
*found_addrp
)
7417 struct remote_state
*rs
= get_remote_state ();
7418 int max_size
= get_memory_write_packet_size ();
7419 struct packet_config
*packet
=
7420 &remote_protocol_packets
[PACKET_qSearch_memory
];
7421 /* number of packet bytes used to encode the pattern,
7422 this could be more than PATTERN_LEN due to escape characters */
7423 int escaped_pattern_len
;
7424 /* amount of pattern that was encodable in the packet */
7425 int used_pattern_len
;
7428 ULONGEST found_addr
;
7430 /* Don't go to the target if we don't have to.
7431 This is done before checking packet->support to avoid the possibility that
7432 a success for this edge case means the facility works in general. */
7433 if (pattern_len
> search_space_len
)
7435 if (pattern_len
== 0)
7437 *found_addrp
= start_addr
;
7441 /* If we already know the packet isn't supported, fall back to the simple
7442 way of searching memory. */
7444 if (packet
->support
== PACKET_DISABLE
)
7446 /* Target doesn't provided special support, fall back and use the
7447 standard support (copy memory and do the search here). */
7448 return simple_search_memory (ops
, start_addr
, search_space_len
,
7449 pattern
, pattern_len
, found_addrp
);
7452 /* Insert header. */
7453 i
= snprintf (rs
->buf
, max_size
,
7454 "qSearch:memory:%s;%s;",
7455 paddr_nz (start_addr
),
7456 phex_nz (search_space_len
, sizeof (search_space_len
)));
7457 max_size
-= (i
+ 1);
7459 /* Escape as much data as fits into rs->buf. */
7460 escaped_pattern_len
=
7461 remote_escape_output (pattern
, pattern_len
, (rs
->buf
+ i
),
7462 &used_pattern_len
, max_size
);
7464 /* Bail if the pattern is too large. */
7465 if (used_pattern_len
!= pattern_len
)
7466 error ("Pattern is too large to transmit to remote target.");
7468 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
7469 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
7470 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7472 /* The request may not have worked because the command is not
7473 supported. If so, fall back to the simple way. */
7474 if (packet
->support
== PACKET_DISABLE
)
7476 return simple_search_memory (ops
, start_addr
, search_space_len
,
7477 pattern
, pattern_len
, found_addrp
);
7482 if (rs
->buf
[0] == '0')
7484 else if (rs
->buf
[0] == '1')
7487 if (rs
->buf
[1] != ',')
7488 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
7489 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
7490 *found_addrp
= found_addr
;
7493 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
7499 remote_rcmd (char *command
,
7500 struct ui_file
*outbuf
)
7502 struct remote_state
*rs
= get_remote_state ();
7506 error (_("remote rcmd is only available after target open"));
7508 /* Send a NULL command across as an empty command. */
7509 if (command
== NULL
)
7512 /* The query prefix. */
7513 strcpy (rs
->buf
, "qRcmd,");
7514 p
= strchr (rs
->buf
, '\0');
7516 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/) > get_remote_packet_size ())
7517 error (_("\"monitor\" command ``%s'' is too long."), command
);
7519 /* Encode the actual command. */
7520 bin2hex ((gdb_byte
*) command
, p
, 0);
7522 if (putpkt (rs
->buf
) < 0)
7523 error (_("Communication problem with target."));
7525 /* get/display the response */
7530 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
7532 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7535 error (_("Target does not support this command."));
7536 if (buf
[0] == 'O' && buf
[1] != 'K')
7538 remote_console_output (buf
+ 1); /* 'O' message from stub. */
7541 if (strcmp (buf
, "OK") == 0)
7543 if (strlen (buf
) == 3 && buf
[0] == 'E'
7544 && isdigit (buf
[1]) && isdigit (buf
[2]))
7546 error (_("Protocol error with Rcmd"));
7548 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
7550 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
7551 fputc_unfiltered (c
, outbuf
);
7557 static VEC(mem_region_s
) *
7558 remote_memory_map (struct target_ops
*ops
)
7560 VEC(mem_region_s
) *result
= NULL
;
7561 char *text
= target_read_stralloc (¤t_target
,
7562 TARGET_OBJECT_MEMORY_MAP
, NULL
);
7566 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
7567 result
= parse_memory_map (text
);
7568 do_cleanups (back_to
);
7575 packet_command (char *args
, int from_tty
)
7577 struct remote_state
*rs
= get_remote_state ();
7580 error (_("command can only be used with remote target"));
7583 error (_("remote-packet command requires packet text as argument"));
7585 puts_filtered ("sending: ");
7586 print_packet (args
);
7587 puts_filtered ("\n");
7590 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7591 puts_filtered ("received: ");
7592 print_packet (rs
->buf
);
7593 puts_filtered ("\n");
7597 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
7599 static void display_thread_info (struct gdb_ext_thread_info
*info
);
7601 static void threadset_test_cmd (char *cmd
, int tty
);
7603 static void threadalive_test (char *cmd
, int tty
);
7605 static void threadlist_test_cmd (char *cmd
, int tty
);
7607 int get_and_display_threadinfo (threadref
*ref
);
7609 static void threadinfo_test_cmd (char *cmd
, int tty
);
7611 static int thread_display_step (threadref
*ref
, void *context
);
7613 static void threadlist_update_test_cmd (char *cmd
, int tty
);
7615 static void init_remote_threadtests (void);
7617 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
7620 threadset_test_cmd (char *cmd
, int tty
)
7622 int sample_thread
= SAMPLE_THREAD
;
7624 printf_filtered (_("Remote threadset test\n"));
7625 set_general_thread (sample_thread
);
7630 threadalive_test (char *cmd
, int tty
)
7632 int sample_thread
= SAMPLE_THREAD
;
7633 int pid
= ptid_get_pid (inferior_ptid
);
7634 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
7636 if (remote_thread_alive (ptid
))
7637 printf_filtered ("PASS: Thread alive test\n");
7639 printf_filtered ("FAIL: Thread alive test\n");
7642 void output_threadid (char *title
, threadref
*ref
);
7645 output_threadid (char *title
, threadref
*ref
)
7649 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
7651 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
7655 threadlist_test_cmd (char *cmd
, int tty
)
7658 threadref nextthread
;
7659 int done
, result_count
;
7660 threadref threadlist
[3];
7662 printf_filtered ("Remote Threadlist test\n");
7663 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
7664 &result_count
, &threadlist
[0]))
7665 printf_filtered ("FAIL: threadlist test\n");
7668 threadref
*scan
= threadlist
;
7669 threadref
*limit
= scan
+ result_count
;
7671 while (scan
< limit
)
7672 output_threadid (" thread ", scan
++);
7677 display_thread_info (struct gdb_ext_thread_info
*info
)
7679 output_threadid ("Threadid: ", &info
->threadid
);
7680 printf_filtered ("Name: %s\n ", info
->shortname
);
7681 printf_filtered ("State: %s\n", info
->display
);
7682 printf_filtered ("other: %s\n\n", info
->more_display
);
7686 get_and_display_threadinfo (threadref
*ref
)
7690 struct gdb_ext_thread_info threadinfo
;
7692 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
7693 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
7694 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
7695 display_thread_info (&threadinfo
);
7700 threadinfo_test_cmd (char *cmd
, int tty
)
7702 int athread
= SAMPLE_THREAD
;
7706 int_to_threadref (&thread
, athread
);
7707 printf_filtered ("Remote Threadinfo test\n");
7708 if (!get_and_display_threadinfo (&thread
))
7709 printf_filtered ("FAIL cannot get thread info\n");
7713 thread_display_step (threadref
*ref
, void *context
)
7715 /* output_threadid(" threadstep ",ref); *//* simple test */
7716 return get_and_display_threadinfo (ref
);
7720 threadlist_update_test_cmd (char *cmd
, int tty
)
7722 printf_filtered ("Remote Threadlist update test\n");
7723 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
7727 init_remote_threadtests (void)
7729 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
7730 Fetch and print the remote list of thread identifiers, one pkt only"));
7731 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
7732 _("Fetch and display info about one thread"));
7733 add_com ("tset", class_obscure
, threadset_test_cmd
,
7734 _("Test setting to a different thread"));
7735 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
7736 _("Iterate through updating all remote thread info"));
7737 add_com ("talive", class_obscure
, threadalive_test
,
7738 _(" Remote thread alive test "));
7743 /* Convert a thread ID to a string. Returns the string in a static
7747 remote_pid_to_str (ptid_t ptid
)
7749 static char buf
[64];
7750 struct remote_state
*rs
= get_remote_state ();
7752 if (ptid_equal (magic_null_ptid
, ptid
))
7754 xsnprintf (buf
, sizeof buf
, "Thread <main>");
7757 else if (remote_multi_process_p (rs
)
7758 && ptid_get_tid (ptid
) != 0 && ptid_get_pid (ptid
) != 0)
7760 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
7761 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
7764 else if (ptid_get_tid (ptid
) != 0)
7766 xsnprintf (buf
, sizeof buf
, "Thread %ld",
7767 ptid_get_tid (ptid
));
7771 return normal_pid_to_str (ptid
);
7774 /* Get the address of the thread local variable in OBJFILE which is
7775 stored at OFFSET within the thread local storage for thread PTID. */
7778 remote_get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
7780 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
7782 struct remote_state
*rs
= get_remote_state ();
7784 char *endp
= rs
->buf
+ get_remote_packet_size ();
7785 enum packet_result result
;
7787 strcpy (p
, "qGetTLSAddr:");
7789 p
= write_ptid (p
, endp
, ptid
);
7791 p
+= hexnumstr (p
, offset
);
7793 p
+= hexnumstr (p
, lm
);
7797 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7798 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
7799 if (result
== PACKET_OK
)
7803 unpack_varlen_hex (rs
->buf
, &result
);
7806 else if (result
== PACKET_UNKNOWN
)
7807 throw_error (TLS_GENERIC_ERROR
,
7808 _("Remote target doesn't support qGetTLSAddr packet"));
7810 throw_error (TLS_GENERIC_ERROR
,
7811 _("Remote target failed to process qGetTLSAddr request"));
7814 throw_error (TLS_GENERIC_ERROR
,
7815 _("TLS not supported or disabled on this target"));
7820 /* Support for inferring a target description based on the current
7821 architecture and the size of a 'g' packet. While the 'g' packet
7822 can have any size (since optional registers can be left off the
7823 end), some sizes are easily recognizable given knowledge of the
7824 approximate architecture. */
7826 struct remote_g_packet_guess
7829 const struct target_desc
*tdesc
;
7831 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
7832 DEF_VEC_O(remote_g_packet_guess_s
);
7834 struct remote_g_packet_data
7836 VEC(remote_g_packet_guess_s
) *guesses
;
7839 static struct gdbarch_data
*remote_g_packet_data_handle
;
7842 remote_g_packet_data_init (struct obstack
*obstack
)
7844 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
7848 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
7849 const struct target_desc
*tdesc
)
7851 struct remote_g_packet_data
*data
7852 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
7853 struct remote_g_packet_guess new_guess
, *guess
;
7856 gdb_assert (tdesc
!= NULL
);
7859 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
7861 if (guess
->bytes
== bytes
)
7862 internal_error (__FILE__
, __LINE__
,
7863 "Duplicate g packet description added for size %d",
7866 new_guess
.bytes
= bytes
;
7867 new_guess
.tdesc
= tdesc
;
7868 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
7871 /* Return 1 if remote_read_description would do anything on this target
7872 and architecture, 0 otherwise. */
7875 remote_read_description_p (struct target_ops
*target
)
7877 struct remote_g_packet_data
*data
7878 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
7880 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
7886 static const struct target_desc
*
7887 remote_read_description (struct target_ops
*target
)
7889 struct remote_g_packet_data
*data
7890 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
7892 /* Do not try this during initial connection, when we do not know
7893 whether there is a running but stopped thread. */
7894 if (!target_has_execution
|| ptid_equal (inferior_ptid
, null_ptid
))
7897 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
7899 struct remote_g_packet_guess
*guess
;
7901 int bytes
= send_g_packet ();
7904 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
7906 if (guess
->bytes
== bytes
)
7907 return guess
->tdesc
;
7909 /* We discard the g packet. A minor optimization would be to
7910 hold on to it, and fill the register cache once we have selected
7911 an architecture, but it's too tricky to do safely. */
7917 /* Remote file transfer support. This is host-initiated I/O, not
7918 target-initiated; for target-initiated, see remote-fileio.c. */
7920 /* If *LEFT is at least the length of STRING, copy STRING to
7921 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7922 decrease *LEFT. Otherwise raise an error. */
7925 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
7927 int len
= strlen (string
);
7930 error (_("Packet too long for target."));
7932 memcpy (*buffer
, string
, len
);
7936 /* NUL-terminate the buffer as a convenience, if there is
7942 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
7943 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7944 decrease *LEFT. Otherwise raise an error. */
7947 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
7950 if (2 * len
> *left
)
7951 error (_("Packet too long for target."));
7953 bin2hex (bytes
, *buffer
, len
);
7957 /* NUL-terminate the buffer as a convenience, if there is
7963 /* If *LEFT is large enough, convert VALUE to hex and add it to
7964 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7965 decrease *LEFT. Otherwise raise an error. */
7968 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
7970 int len
= hexnumlen (value
);
7973 error (_("Packet too long for target."));
7975 hexnumstr (*buffer
, value
);
7979 /* NUL-terminate the buffer as a convenience, if there is
7985 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
7986 value, *REMOTE_ERRNO to the remote error number or zero if none
7987 was included, and *ATTACHMENT to point to the start of the annex
7988 if any. The length of the packet isn't needed here; there may
7989 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
7991 Return 0 if the packet could be parsed, -1 if it could not. If
7992 -1 is returned, the other variables may not be initialized. */
7995 remote_hostio_parse_result (char *buffer
, int *retcode
,
7996 int *remote_errno
, char **attachment
)
8003 if (buffer
[0] != 'F')
8007 *retcode
= strtol (&buffer
[1], &p
, 16);
8008 if (errno
!= 0 || p
== &buffer
[1])
8011 /* Check for ",errno". */
8015 *remote_errno
= strtol (p
+ 1, &p2
, 16);
8016 if (errno
!= 0 || p
+ 1 == p2
)
8021 /* Check for ";attachment". If there is no attachment, the
8022 packet should end here. */
8025 *attachment
= p
+ 1;
8028 else if (*p
== '\0')
8034 /* Send a prepared I/O packet to the target and read its response.
8035 The prepared packet is in the global RS->BUF before this function
8036 is called, and the answer is there when we return.
8038 COMMAND_BYTES is the length of the request to send, which may include
8039 binary data. WHICH_PACKET is the packet configuration to check
8040 before attempting a packet. If an error occurs, *REMOTE_ERRNO
8041 is set to the error number and -1 is returned. Otherwise the value
8042 returned by the function is returned.
8044 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
8045 attachment is expected; an error will be reported if there's a
8046 mismatch. If one is found, *ATTACHMENT will be set to point into
8047 the packet buffer and *ATTACHMENT_LEN will be set to the
8048 attachment's length. */
8051 remote_hostio_send_command (int command_bytes
, int which_packet
,
8052 int *remote_errno
, char **attachment
,
8053 int *attachment_len
)
8055 struct remote_state
*rs
= get_remote_state ();
8056 int ret
, bytes_read
;
8057 char *attachment_tmp
;
8060 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
8062 *remote_errno
= FILEIO_ENOSYS
;
8066 putpkt_binary (rs
->buf
, command_bytes
);
8067 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
8069 /* If it timed out, something is wrong. Don't try to parse the
8073 *remote_errno
= FILEIO_EINVAL
;
8077 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
8080 *remote_errno
= FILEIO_EINVAL
;
8082 case PACKET_UNKNOWN
:
8083 *remote_errno
= FILEIO_ENOSYS
;
8089 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
8092 *remote_errno
= FILEIO_EINVAL
;
8096 /* Make sure we saw an attachment if and only if we expected one. */
8097 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
8098 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
8100 *remote_errno
= FILEIO_EINVAL
;
8104 /* If an attachment was found, it must point into the packet buffer;
8105 work out how many bytes there were. */
8106 if (attachment_tmp
!= NULL
)
8108 *attachment
= attachment_tmp
;
8109 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
8115 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
8116 remote file descriptor, or -1 if an error occurs (and set
8120 remote_hostio_open (const char *filename
, int flags
, int mode
,
8123 struct remote_state
*rs
= get_remote_state ();
8125 int left
= get_remote_packet_size () - 1;
8127 remote_buffer_add_string (&p
, &left
, "vFile:open:");
8129 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
8131 remote_buffer_add_string (&p
, &left
, ",");
8133 remote_buffer_add_int (&p
, &left
, flags
);
8134 remote_buffer_add_string (&p
, &left
, ",");
8136 remote_buffer_add_int (&p
, &left
, mode
);
8138 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
8139 remote_errno
, NULL
, NULL
);
8142 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
8143 Return the number of bytes written, or -1 if an error occurs (and
8144 set *REMOTE_ERRNO). */
8147 remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
8148 ULONGEST offset
, int *remote_errno
)
8150 struct remote_state
*rs
= get_remote_state ();
8152 int left
= get_remote_packet_size ();
8155 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
8157 remote_buffer_add_int (&p
, &left
, fd
);
8158 remote_buffer_add_string (&p
, &left
, ",");
8160 remote_buffer_add_int (&p
, &left
, offset
);
8161 remote_buffer_add_string (&p
, &left
, ",");
8163 p
+= remote_escape_output (write_buf
, len
, p
, &out_len
,
8164 get_remote_packet_size () - (p
- rs
->buf
));
8166 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
8167 remote_errno
, NULL
, NULL
);
8170 /* Read up to LEN bytes FD on the remote target into READ_BUF
8171 Return the number of bytes read, or -1 if an error occurs (and
8172 set *REMOTE_ERRNO). */
8175 remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
8176 ULONGEST offset
, int *remote_errno
)
8178 struct remote_state
*rs
= get_remote_state ();
8181 int left
= get_remote_packet_size ();
8182 int ret
, attachment_len
;
8185 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
8187 remote_buffer_add_int (&p
, &left
, fd
);
8188 remote_buffer_add_string (&p
, &left
, ",");
8190 remote_buffer_add_int (&p
, &left
, len
);
8191 remote_buffer_add_string (&p
, &left
, ",");
8193 remote_buffer_add_int (&p
, &left
, offset
);
8195 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
8196 remote_errno
, &attachment
,
8202 read_len
= remote_unescape_input (attachment
, attachment_len
,
8204 if (read_len
!= ret
)
8205 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
8210 /* Close FD on the remote target. Return 0, or -1 if an error occurs
8211 (and set *REMOTE_ERRNO). */
8214 remote_hostio_close (int fd
, int *remote_errno
)
8216 struct remote_state
*rs
= get_remote_state ();
8218 int left
= get_remote_packet_size () - 1;
8220 remote_buffer_add_string (&p
, &left
, "vFile:close:");
8222 remote_buffer_add_int (&p
, &left
, fd
);
8224 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
8225 remote_errno
, NULL
, NULL
);
8228 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
8229 occurs (and set *REMOTE_ERRNO). */
8232 remote_hostio_unlink (const char *filename
, int *remote_errno
)
8234 struct remote_state
*rs
= get_remote_state ();
8236 int left
= get_remote_packet_size () - 1;
8238 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
8240 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
8243 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
8244 remote_errno
, NULL
, NULL
);
8248 remote_fileio_errno_to_host (int errnum
)
8272 case FILEIO_ENOTDIR
:
8292 case FILEIO_ENAMETOOLONG
:
8293 return ENAMETOOLONG
;
8299 remote_hostio_error (int errnum
)
8301 int host_error
= remote_fileio_errno_to_host (errnum
);
8303 if (host_error
== -1)
8304 error (_("Unknown remote I/O error %d"), errnum
);
8306 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
8310 remote_hostio_close_cleanup (void *opaque
)
8312 int fd
= *(int *) opaque
;
8315 remote_hostio_close (fd
, &remote_errno
);
8320 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
8322 const char *filename
= bfd_get_filename (abfd
);
8323 int fd
, remote_errno
;
8326 gdb_assert (remote_filename_p (filename
));
8328 fd
= remote_hostio_open (filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
8331 errno
= remote_fileio_errno_to_host (remote_errno
);
8332 bfd_set_error (bfd_error_system_call
);
8336 stream
= xmalloc (sizeof (int));
8342 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
8344 int fd
= *(int *)stream
;
8349 /* Ignore errors on close; these may happen if the remote
8350 connection was already torn down. */
8351 remote_hostio_close (fd
, &remote_errno
);
8357 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
8358 file_ptr nbytes
, file_ptr offset
)
8360 int fd
= *(int *)stream
;
8362 file_ptr pos
, bytes
;
8365 while (nbytes
> pos
)
8367 bytes
= remote_hostio_pread (fd
, (char *)buf
+ pos
, nbytes
- pos
,
8368 offset
+ pos
, &remote_errno
);
8370 /* Success, but no bytes, means end-of-file. */
8374 errno
= remote_fileio_errno_to_host (remote_errno
);
8375 bfd_set_error (bfd_error_system_call
);
8386 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
8388 /* FIXME: We should probably implement remote_hostio_stat. */
8389 sb
->st_size
= INT_MAX
;
8394 remote_filename_p (const char *filename
)
8396 return strncmp (filename
, "remote:", 7) == 0;
8400 remote_bfd_open (const char *remote_file
, const char *target
)
8402 return bfd_openr_iovec (remote_file
, target
,
8403 remote_bfd_iovec_open
, NULL
,
8404 remote_bfd_iovec_pread
,
8405 remote_bfd_iovec_close
,
8406 remote_bfd_iovec_stat
);
8410 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
8412 struct cleanup
*back_to
, *close_cleanup
;
8413 int retcode
, fd
, remote_errno
, bytes
, io_size
;
8416 int bytes_in_buffer
;
8421 error (_("command can only be used with remote target"));
8423 file
= fopen (local_file
, "rb");
8425 perror_with_name (local_file
);
8426 back_to
= make_cleanup_fclose (file
);
8428 fd
= remote_hostio_open (remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
8430 0700, &remote_errno
);
8432 remote_hostio_error (remote_errno
);
8434 /* Send up to this many bytes at once. They won't all fit in the
8435 remote packet limit, so we'll transfer slightly fewer. */
8436 io_size
= get_remote_packet_size ();
8437 buffer
= xmalloc (io_size
);
8438 make_cleanup (xfree
, buffer
);
8440 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
8442 bytes_in_buffer
= 0;
8445 while (bytes_in_buffer
|| !saw_eof
)
8449 bytes
= fread (buffer
+ bytes_in_buffer
, 1, io_size
- bytes_in_buffer
,
8454 error (_("Error reading %s."), local_file
);
8457 /* EOF. Unless there is something still in the
8458 buffer from the last iteration, we are done. */
8460 if (bytes_in_buffer
== 0)
8468 bytes
+= bytes_in_buffer
;
8469 bytes_in_buffer
= 0;
8471 retcode
= remote_hostio_pwrite (fd
, buffer
, bytes
, offset
, &remote_errno
);
8474 remote_hostio_error (remote_errno
);
8475 else if (retcode
== 0)
8476 error (_("Remote write of %d bytes returned 0!"), bytes
);
8477 else if (retcode
< bytes
)
8479 /* Short write. Save the rest of the read data for the next
8481 bytes_in_buffer
= bytes
- retcode
;
8482 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
8488 discard_cleanups (close_cleanup
);
8489 if (remote_hostio_close (fd
, &remote_errno
))
8490 remote_hostio_error (remote_errno
);
8493 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
8494 do_cleanups (back_to
);
8498 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
8500 struct cleanup
*back_to
, *close_cleanup
;
8501 int retcode
, fd
, remote_errno
, bytes
, io_size
;
8507 error (_("command can only be used with remote target"));
8509 fd
= remote_hostio_open (remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
8511 remote_hostio_error (remote_errno
);
8513 file
= fopen (local_file
, "wb");
8515 perror_with_name (local_file
);
8516 back_to
= make_cleanup_fclose (file
);
8518 /* Send up to this many bytes at once. They won't all fit in the
8519 remote packet limit, so we'll transfer slightly fewer. */
8520 io_size
= get_remote_packet_size ();
8521 buffer
= xmalloc (io_size
);
8522 make_cleanup (xfree
, buffer
);
8524 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
8529 bytes
= remote_hostio_pread (fd
, buffer
, io_size
, offset
, &remote_errno
);
8531 /* Success, but no bytes, means end-of-file. */
8534 remote_hostio_error (remote_errno
);
8538 bytes
= fwrite (buffer
, 1, bytes
, file
);
8540 perror_with_name (local_file
);
8543 discard_cleanups (close_cleanup
);
8544 if (remote_hostio_close (fd
, &remote_errno
))
8545 remote_hostio_error (remote_errno
);
8548 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
8549 do_cleanups (back_to
);
8553 remote_file_delete (const char *remote_file
, int from_tty
)
8555 int retcode
, remote_errno
;
8558 error (_("command can only be used with remote target"));
8560 retcode
= remote_hostio_unlink (remote_file
, &remote_errno
);
8562 remote_hostio_error (remote_errno
);
8565 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
8569 remote_put_command (char *args
, int from_tty
)
8571 struct cleanup
*back_to
;
8575 error_no_arg (_("file to put"));
8577 argv
= gdb_buildargv (args
);
8578 back_to
= make_cleanup_freeargv (argv
);
8579 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
8580 error (_("Invalid parameters to remote put"));
8582 remote_file_put (argv
[0], argv
[1], from_tty
);
8584 do_cleanups (back_to
);
8588 remote_get_command (char *args
, int from_tty
)
8590 struct cleanup
*back_to
;
8594 error_no_arg (_("file to get"));
8596 argv
= gdb_buildargv (args
);
8597 back_to
= make_cleanup_freeargv (argv
);
8598 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
8599 error (_("Invalid parameters to remote get"));
8601 remote_file_get (argv
[0], argv
[1], from_tty
);
8603 do_cleanups (back_to
);
8607 remote_delete_command (char *args
, int from_tty
)
8609 struct cleanup
*back_to
;
8613 error_no_arg (_("file to delete"));
8615 argv
= gdb_buildargv (args
);
8616 back_to
= make_cleanup_freeargv (argv
);
8617 if (argv
[0] == NULL
|| argv
[1] != NULL
)
8618 error (_("Invalid parameters to remote delete"));
8620 remote_file_delete (argv
[0], from_tty
);
8622 do_cleanups (back_to
);
8626 remote_command (char *args
, int from_tty
)
8628 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
8631 static int remote_target_can_reverse
= 1;
8634 remote_can_execute_reverse (void)
8636 return remote_target_can_reverse
;
8640 remote_supports_non_stop (void)
8646 remote_supports_multi_process (void)
8648 struct remote_state
*rs
= get_remote_state ();
8649 return remote_multi_process_p (rs
);
8653 extended_remote_can_run (void)
8655 if (remote_desc
!= NULL
)
8662 init_remote_ops (void)
8664 remote_ops
.to_shortname
= "remote";
8665 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
8667 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
8668 Specify the serial device it is connected to\n\
8669 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
8670 remote_ops
.to_open
= remote_open
;
8671 remote_ops
.to_close
= remote_close
;
8672 remote_ops
.to_detach
= remote_detach
;
8673 remote_ops
.to_disconnect
= remote_disconnect
;
8674 remote_ops
.to_resume
= remote_resume
;
8675 remote_ops
.to_wait
= remote_wait
;
8676 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
8677 remote_ops
.to_store_registers
= remote_store_registers
;
8678 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
8679 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
8680 remote_ops
.to_files_info
= remote_files_info
;
8681 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
8682 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
8683 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
8684 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
8685 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
8686 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
8687 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
8688 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
8689 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
8690 remote_ops
.to_kill
= remote_kill
;
8691 remote_ops
.to_load
= generic_load
;
8692 remote_ops
.to_mourn_inferior
= remote_mourn
;
8693 remote_ops
.to_thread_alive
= remote_thread_alive
;
8694 remote_ops
.to_find_new_threads
= remote_threads_info
;
8695 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
8696 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
8697 remote_ops
.to_stop
= remote_stop
;
8698 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
8699 remote_ops
.to_rcmd
= remote_rcmd
;
8700 remote_ops
.to_log_command
= serial_log_command
;
8701 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
8702 remote_ops
.to_stratum
= process_stratum
;
8703 remote_ops
.to_has_all_memory
= 1;
8704 remote_ops
.to_has_memory
= 1;
8705 remote_ops
.to_has_stack
= 1;
8706 remote_ops
.to_has_registers
= 1;
8707 remote_ops
.to_has_execution
= 1;
8708 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
8709 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
8710 remote_ops
.to_magic
= OPS_MAGIC
;
8711 remote_ops
.to_memory_map
= remote_memory_map
;
8712 remote_ops
.to_flash_erase
= remote_flash_erase
;
8713 remote_ops
.to_flash_done
= remote_flash_done
;
8714 remote_ops
.to_read_description
= remote_read_description
;
8715 remote_ops
.to_search_memory
= remote_search_memory
;
8716 remote_ops
.to_can_async_p
= remote_can_async_p
;
8717 remote_ops
.to_is_async_p
= remote_is_async_p
;
8718 remote_ops
.to_async
= remote_async
;
8719 remote_ops
.to_async_mask
= remote_async_mask
;
8720 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
8721 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
8722 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
8723 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
8726 /* Set up the extended remote vector by making a copy of the standard
8727 remote vector and adding to it. */
8730 init_extended_remote_ops (void)
8732 extended_remote_ops
= remote_ops
;
8734 extended_remote_ops
.to_shortname
= "extended-remote";
8735 extended_remote_ops
.to_longname
=
8736 "Extended remote serial target in gdb-specific protocol";
8737 extended_remote_ops
.to_doc
=
8738 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
8739 Specify the serial device it is connected to (e.g. /dev/ttya).";
8740 extended_remote_ops
.to_open
= extended_remote_open
;
8741 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
8742 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
8743 extended_remote_ops
.to_detach
= extended_remote_detach
;
8744 extended_remote_ops
.to_attach
= extended_remote_attach
;
8745 extended_remote_ops
.to_kill
= extended_remote_kill
;
8746 extended_remote_ops
.to_can_run
= extended_remote_can_run
;
8750 remote_can_async_p (void)
8752 if (!target_async_permitted
)
8753 /* We only enable async when the user specifically asks for it. */
8756 /* We're async whenever the serial device is. */
8757 return remote_async_mask_value
&& serial_can_async_p (remote_desc
);
8761 remote_is_async_p (void)
8763 if (!target_async_permitted
)
8764 /* We only enable async when the user specifically asks for it. */
8767 /* We're async whenever the serial device is. */
8768 return remote_async_mask_value
&& serial_is_async_p (remote_desc
);
8771 /* Pass the SERIAL event on and up to the client. One day this code
8772 will be able to delay notifying the client of an event until the
8773 point where an entire packet has been received. */
8775 static void (*async_client_callback
) (enum inferior_event_type event_type
,
8777 static void *async_client_context
;
8778 static serial_event_ftype remote_async_serial_handler
;
8781 remote_async_serial_handler (struct serial
*scb
, void *context
)
8783 /* Don't propogate error information up to the client. Instead let
8784 the client find out about the error by querying the target. */
8785 async_client_callback (INF_REG_EVENT
, async_client_context
);
8789 remote_async_inferior_event_handler (gdb_client_data data
)
8791 inferior_event_handler (INF_REG_EVENT
, NULL
);
8795 remote_async_get_pending_events_handler (gdb_client_data data
)
8797 remote_get_pending_stop_replies ();
8801 remote_async (void (*callback
) (enum inferior_event_type event_type
,
8802 void *context
), void *context
)
8804 if (remote_async_mask_value
== 0)
8805 internal_error (__FILE__
, __LINE__
,
8806 _("Calling remote_async when async is masked"));
8808 if (callback
!= NULL
)
8810 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
8811 async_client_callback
= callback
;
8812 async_client_context
= context
;
8815 serial_async (remote_desc
, NULL
, NULL
);
8819 remote_async_mask (int new_mask
)
8821 int curr_mask
= remote_async_mask_value
;
8822 remote_async_mask_value
= new_mask
;
8827 set_remote_cmd (char *args
, int from_tty
)
8829 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
8833 show_remote_cmd (char *args
, int from_tty
)
8835 /* We can't just use cmd_show_list here, because we want to skip
8836 the redundant "show remote Z-packet" and the legacy aliases. */
8837 struct cleanup
*showlist_chain
;
8838 struct cmd_list_element
*list
= remote_show_cmdlist
;
8840 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
8841 for (; list
!= NULL
; list
= list
->next
)
8842 if (strcmp (list
->name
, "Z-packet") == 0)
8844 else if (list
->type
== not_set_cmd
)
8845 /* Alias commands are exactly like the original, except they
8846 don't have the normal type. */
8850 struct cleanup
*option_chain
8851 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
8852 ui_out_field_string (uiout
, "name", list
->name
);
8853 ui_out_text (uiout
, ": ");
8854 if (list
->type
== show_cmd
)
8855 do_setshow_command ((char *) NULL
, from_tty
, list
);
8857 cmd_func (list
, NULL
, from_tty
);
8858 /* Close the tuple. */
8859 do_cleanups (option_chain
);
8862 /* Close the tuple. */
8863 do_cleanups (showlist_chain
);
8867 /* Function to be called whenever a new objfile (shlib) is detected. */
8869 remote_new_objfile (struct objfile
*objfile
)
8871 if (remote_desc
!= 0) /* Have a remote connection. */
8872 remote_check_symbols (objfile
);
8876 _initialize_remote (void)
8878 struct remote_state
*rs
;
8880 /* architecture specific data */
8881 remote_gdbarch_data_handle
=
8882 gdbarch_data_register_post_init (init_remote_state
);
8883 remote_g_packet_data_handle
=
8884 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
8886 /* Initialize the per-target state. At the moment there is only one
8887 of these, not one per target. Only one target is active at a
8888 time. The default buffer size is unimportant; it will be expanded
8889 whenever a larger buffer is needed. */
8890 rs
= get_remote_state_raw ();
8892 rs
->buf
= xmalloc (rs
->buf_size
);
8895 add_target (&remote_ops
);
8897 init_extended_remote_ops ();
8898 add_target (&extended_remote_ops
);
8900 /* Hook into new objfile notification. */
8901 observer_attach_new_objfile (remote_new_objfile
);
8903 /* Set up signal handlers. */
8904 sigint_remote_token
=
8905 create_async_signal_handler (async_remote_interrupt
, NULL
);
8906 sigint_remote_twice_token
=
8907 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
8910 init_remote_threadtests ();
8913 /* set/show remote ... */
8915 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
8916 Remote protocol specific variables\n\
8917 Configure various remote-protocol specific variables such as\n\
8918 the packets being used"),
8919 &remote_set_cmdlist
, "set remote ",
8920 0 /* allow-unknown */, &setlist
);
8921 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
8922 Remote protocol specific variables\n\
8923 Configure various remote-protocol specific variables such as\n\
8924 the packets being used"),
8925 &remote_show_cmdlist
, "show remote ",
8926 0 /* allow-unknown */, &showlist
);
8928 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
8929 Compare section data on target to the exec file.\n\
8930 Argument is a single section name (default: all loaded sections)."),
8933 add_cmd ("packet", class_maintenance
, packet_command
, _("\
8934 Send an arbitrary packet to a remote target.\n\
8935 maintenance packet TEXT\n\
8936 If GDB is talking to an inferior via the GDB serial protocol, then\n\
8937 this command sends the string TEXT to the inferior, and displays the\n\
8938 response packet. GDB supplies the initial `$' character, and the\n\
8939 terminating `#' character and checksum."),
8942 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
8943 Set whether to send break if interrupted."), _("\
8944 Show whether to send break if interrupted."), _("\
8945 If set, a break, instead of a cntrl-c, is sent to the remote target."),
8946 NULL
, NULL
, /* FIXME: i18n: Whether to send break if interrupted is %s. */
8947 &setlist
, &showlist
);
8949 /* Install commands for configuring memory read/write packets. */
8951 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
8952 Set the maximum number of bytes per memory write packet (deprecated)."),
8954 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
8955 Show the maximum number of bytes per memory write packet (deprecated)."),
8957 add_cmd ("memory-write-packet-size", no_class
,
8958 set_memory_write_packet_size
, _("\
8959 Set the maximum number of bytes per memory-write packet.\n\
8960 Specify the number of bytes in a packet or 0 (zero) for the\n\
8961 default packet size. The actual limit is further reduced\n\
8962 dependent on the target. Specify ``fixed'' to disable the\n\
8963 further restriction and ``limit'' to enable that restriction."),
8964 &remote_set_cmdlist
);
8965 add_cmd ("memory-read-packet-size", no_class
,
8966 set_memory_read_packet_size
, _("\
8967 Set the maximum number of bytes per memory-read packet.\n\
8968 Specify the number of bytes in a packet or 0 (zero) for the\n\
8969 default packet size. The actual limit is further reduced\n\
8970 dependent on the target. Specify ``fixed'' to disable the\n\
8971 further restriction and ``limit'' to enable that restriction."),
8972 &remote_set_cmdlist
);
8973 add_cmd ("memory-write-packet-size", no_class
,
8974 show_memory_write_packet_size
,
8975 _("Show the maximum number of bytes per memory-write packet."),
8976 &remote_show_cmdlist
);
8977 add_cmd ("memory-read-packet-size", no_class
,
8978 show_memory_read_packet_size
,
8979 _("Show the maximum number of bytes per memory-read packet."),
8980 &remote_show_cmdlist
);
8982 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
8983 &remote_hw_watchpoint_limit
, _("\
8984 Set the maximum number of target hardware watchpoints."), _("\
8985 Show the maximum number of target hardware watchpoints."), _("\
8986 Specify a negative limit for unlimited."),
8987 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
8988 &remote_set_cmdlist
, &remote_show_cmdlist
);
8989 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
8990 &remote_hw_breakpoint_limit
, _("\
8991 Set the maximum number of target hardware breakpoints."), _("\
8992 Show the maximum number of target hardware breakpoints."), _("\
8993 Specify a negative limit for unlimited."),
8994 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
8995 &remote_set_cmdlist
, &remote_show_cmdlist
);
8997 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
8998 &remote_address_size
, _("\
8999 Set the maximum size of the address (in bits) in a memory packet."), _("\
9000 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
9002 NULL
, /* FIXME: i18n: */
9003 &setlist
, &showlist
);
9005 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
9006 "X", "binary-download", 1);
9008 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
9009 "vCont", "verbose-resume", 0);
9011 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
9012 "QPassSignals", "pass-signals", 0);
9014 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
9015 "qSymbol", "symbol-lookup", 0);
9017 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
9018 "P", "set-register", 1);
9020 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
9021 "p", "fetch-register", 1);
9023 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
9024 "Z0", "software-breakpoint", 0);
9026 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
9027 "Z1", "hardware-breakpoint", 0);
9029 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
9030 "Z2", "write-watchpoint", 0);
9032 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
9033 "Z3", "read-watchpoint", 0);
9035 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
9036 "Z4", "access-watchpoint", 0);
9038 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
9039 "qXfer:auxv:read", "read-aux-vector", 0);
9041 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
9042 "qXfer:features:read", "target-features", 0);
9044 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
9045 "qXfer:libraries:read", "library-info", 0);
9047 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
9048 "qXfer:memory-map:read", "memory-map", 0);
9050 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
9051 "qXfer:spu:read", "read-spu-object", 0);
9053 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
9054 "qXfer:spu:write", "write-spu-object", 0);
9056 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
9057 "qXfer:osdata:read", "osdata", 0);
9059 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
9060 "qGetTLSAddr", "get-thread-local-storage-address",
9063 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
9064 "qSupported", "supported-packets", 0);
9066 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
9067 "qSearch:memory", "search-memory", 0);
9069 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
9070 "vFile:open", "hostio-open", 0);
9072 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
9073 "vFile:pread", "hostio-pread", 0);
9075 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
9076 "vFile:pwrite", "hostio-pwrite", 0);
9078 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
9079 "vFile:close", "hostio-close", 0);
9081 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
9082 "vFile:unlink", "hostio-unlink", 0);
9084 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
9085 "vAttach", "attach", 0);
9087 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
9090 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
9091 "QStartNoAckMode", "noack", 0);
9093 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
9094 "vKill", "kill", 0);
9096 /* Keep the old ``set remote Z-packet ...'' working. Each individual
9097 Z sub-packet has its own set and show commands, but users may
9098 have sets to this variable in their .gdbinit files (or in their
9100 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
9101 &remote_Z_packet_detect
, _("\
9102 Set use of remote protocol `Z' packets"), _("\
9103 Show use of remote protocol `Z' packets "), _("\
9104 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
9106 set_remote_protocol_Z_packet_cmd
,
9107 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
9108 &remote_set_cmdlist
, &remote_show_cmdlist
);
9110 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
9111 Manipulate files on the remote system\n\
9112 Transfer files to and from the remote target system."),
9113 &remote_cmdlist
, "remote ",
9114 0 /* allow-unknown */, &cmdlist
);
9116 add_cmd ("put", class_files
, remote_put_command
,
9117 _("Copy a local file to the remote system."),
9120 add_cmd ("get", class_files
, remote_get_command
,
9121 _("Copy a remote file to the local system."),
9124 add_cmd ("delete", class_files
, remote_delete_command
,
9125 _("Delete a remote file."),
9128 remote_exec_file
= xstrdup ("");
9129 add_setshow_string_noescape_cmd ("exec-file", class_files
,
9130 &remote_exec_file
, _("\
9131 Set the remote pathname for \"run\""), _("\
9132 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
9133 &remote_set_cmdlist
, &remote_show_cmdlist
);
9135 /* Eventually initialize fileio. See fileio.c */
9136 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
9138 /* Take advantage of the fact that the LWP field is not used, to tag
9139 special ptids with it set to != 0. */
9140 magic_null_ptid
= ptid_build (42000, 1, -1);
9141 not_sent_ptid
= ptid_build (42000, 1, -2);
9142 any_thread_ptid
= ptid_build (42000, 1, 0);