1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
31 #include "exceptions.h"
33 /*#include "terminal.h" */
36 #include "gdb-stabs.h"
37 #include "gdbthread.h"
41 #include "gdb_assert.h"
44 #include "cli/cli-decode.h"
45 #include "cli/cli-setshow.h"
46 #include "target-descriptions.h"
51 #include "event-loop.h"
52 #include "event-top.h"
58 #include "gdbcore.h" /* for exec_bfd */
60 #include "remote-fileio.h"
61 #include "gdb/fileio.h"
64 #include "memory-map.h"
66 /* The size to align memory write packets, when practical. The protocol
67 does not guarantee any alignment, and gdb will generate short
68 writes and unaligned writes, but even as a best-effort attempt this
69 can improve bulk transfers. For instance, if a write is misaligned
70 relative to the target's data bus, the stub may need to make an extra
71 round trip fetching data from the target. This doesn't make a
72 huge difference, but it's easy to do, so we try to be helpful.
74 The alignment chosen is arbitrary; usually data bus width is
75 important here, not the possibly larger cache line size. */
76 enum { REMOTE_ALIGN_WRITES
= 16 };
78 /* Prototypes for local functions. */
79 static void cleanup_sigint_signal_handler (void *dummy
);
80 static void initialize_sigint_signal_handler (void);
81 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
82 static int getpkt_or_notif_sane (char **buf
, long *sizeof_buf
,
85 static void handle_remote_sigint (int);
86 static void handle_remote_sigint_twice (int);
87 static void async_remote_interrupt (gdb_client_data
);
88 void async_remote_interrupt_twice (gdb_client_data
);
90 static void remote_files_info (struct target_ops
*ignore
);
92 static void remote_prepare_to_store (struct regcache
*regcache
);
94 static void remote_fetch_registers (struct regcache
*regcache
, int regno
);
96 static void remote_resume (ptid_t ptid
, int step
,
97 enum target_signal siggnal
);
98 static void remote_open (char *name
, int from_tty
);
100 static void extended_remote_open (char *name
, int from_tty
);
102 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
);
104 static void remote_close (int quitting
);
106 static void remote_store_registers (struct regcache
*regcache
, int regno
);
108 static void remote_mourn (struct target_ops
*ops
);
110 static void extended_remote_restart (void);
112 static void extended_remote_mourn (struct target_ops
*);
114 static void remote_mourn_1 (struct target_ops
*);
116 static void remote_send (char **buf
, long *sizeof_buf_p
);
118 static int readchar (int timeout
);
120 static ptid_t
remote_wait (ptid_t ptid
,
121 struct target_waitstatus
*status
);
123 static void remote_kill (void);
125 static int tohex (int nib
);
127 static int remote_can_async_p (void);
129 static int remote_is_async_p (void);
131 static void remote_async (void (*callback
) (enum inferior_event_type event_type
,
132 void *context
), void *context
);
134 static int remote_async_mask (int new_mask
);
136 static void remote_detach (struct target_ops
*ops
, char *args
, int from_tty
);
138 static void remote_interrupt (int signo
);
140 static void remote_interrupt_twice (int signo
);
142 static void interrupt_query (void);
144 static void set_general_thread (struct ptid ptid
);
145 static void set_continue_thread (struct ptid ptid
);
147 static int remote_thread_alive (ptid_t
);
149 static void get_offsets (void);
151 static void skip_frame (void);
153 static long read_frame (char **buf_p
, long *sizeof_buf
);
155 static int hexnumlen (ULONGEST num
);
157 static void init_remote_ops (void);
159 static void init_extended_remote_ops (void);
161 static void remote_stop (ptid_t
);
163 static int ishex (int ch
, int *val
);
165 static int stubhex (int ch
);
167 static int hexnumstr (char *, ULONGEST
);
169 static int hexnumnstr (char *, ULONGEST
, int);
171 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
173 static void print_packet (char *);
175 static unsigned long crc32 (unsigned char *, int, unsigned int);
177 static void compare_sections_command (char *, int);
179 static void packet_command (char *, int);
181 static int stub_unpack_int (char *buff
, int fieldlength
);
183 static ptid_t
remote_current_thread (ptid_t oldptid
);
185 static void remote_find_new_threads (void);
187 static void record_currthread (ptid_t currthread
);
189 static int fromhex (int a
);
191 static int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
193 static int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
195 static int putpkt_binary (char *buf
, int cnt
);
197 static void check_binary_download (CORE_ADDR addr
);
199 struct packet_config
;
201 static void show_packet_config_cmd (struct packet_config
*config
);
203 static void update_packet_config (struct packet_config
*config
);
205 static void set_remote_protocol_packet_cmd (char *args
, int from_tty
,
206 struct cmd_list_element
*c
);
208 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
210 struct cmd_list_element
*c
,
213 static char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
214 static ptid_t
read_ptid (char *buf
, char **obuf
);
216 static void remote_query_supported (void);
218 static void remote_check_symbols (struct objfile
*objfile
);
220 void _initialize_remote (void);
223 static struct stop_reply
*stop_reply_xmalloc (void);
224 static void stop_reply_xfree (struct stop_reply
*);
225 static void do_stop_reply_xfree (void *arg
);
226 static void remote_parse_stop_reply (char *buf
, struct stop_reply
*);
227 static void push_stop_reply (struct stop_reply
*);
228 static void remote_get_pending_stop_replies (void);
229 static void discard_pending_stop_replies (int pid
);
230 static int peek_stop_reply (ptid_t ptid
);
232 static void remote_async_inferior_event_handler (gdb_client_data
);
233 static void remote_async_get_pending_events_handler (gdb_client_data
);
235 static void remote_terminal_ours (void);
237 /* The non-stop remote protocol provisions for one pending stop reply.
238 This is where we keep it until it is acknowledged. */
240 static struct stop_reply
*pending_stop_reply
= NULL
;
244 static struct cmd_list_element
*remote_cmdlist
;
246 /* For "set remote" and "show remote". */
248 static struct cmd_list_element
*remote_set_cmdlist
;
249 static struct cmd_list_element
*remote_show_cmdlist
;
251 /* Description of the remote protocol state for the currently
252 connected target. This is per-target state, and independent of the
253 selected architecture. */
257 /* A buffer to use for incoming packets, and its current size. The
258 buffer is grown dynamically for larger incoming packets.
259 Outgoing packets may also be constructed in this buffer.
260 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
261 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
266 /* If we negotiated packet size explicitly (and thus can bypass
267 heuristics for the largest packet size that will not overflow
268 a buffer in the stub), this will be set to that packet size.
269 Otherwise zero, meaning to use the guessed size. */
270 long explicit_packet_size
;
272 /* remote_wait is normally called when the target is running and
273 waits for a stop reply packet. But sometimes we need to call it
274 when the target is already stopped. We can send a "?" packet
275 and have remote_wait read the response. Or, if we already have
276 the response, we can stash it in BUF and tell remote_wait to
277 skip calling getpkt. This flag is set when BUF contains a
278 stop reply packet and the target is not waiting. */
279 int cached_wait_status
;
281 /* True, if in no ack mode. That is, neither GDB nor the stub will
282 expect acks from each other. The connection is assumed to be
286 /* True if we're connected in extended remote mode. */
289 /* True if the stub reported support for multi-process
291 int multi_process_aware
;
293 /* True if we resumed the target and we're waiting for the target to
294 stop. In the mean time, we can't start another command/query.
295 The remote server wouldn't be ready to process it, so we'd
296 timeout waiting for a reply that would never come and eventually
297 we'd close the connection. This can happen in asynchronous mode
298 because we allow GDB commands while the target is running. */
299 int waiting_for_stop_reply
;
301 /* True if the stub reports support for non-stop mode. */
304 /* True if the stub reports support for vCont;t. */
308 /* Returns true if the multi-process extensions are in effect. */
310 remote_multi_process_p (struct remote_state
*rs
)
312 return rs
->extended
&& rs
->multi_process_aware
;
315 /* This data could be associated with a target, but we do not always
316 have access to the current target when we need it, so for now it is
317 static. This will be fine for as long as only one target is in use
319 static struct remote_state remote_state
;
321 static struct remote_state
*
322 get_remote_state_raw (void)
324 return &remote_state
;
327 /* Description of the remote protocol for a given architecture. */
331 long offset
; /* Offset into G packet. */
332 long regnum
; /* GDB's internal register number. */
333 LONGEST pnum
; /* Remote protocol register number. */
334 int in_g_packet
; /* Always part of G packet. */
335 /* long size in bytes; == register_size (target_gdbarch, regnum);
337 /* char *name; == gdbarch_register_name (target_gdbarch, regnum);
341 struct remote_arch_state
343 /* Description of the remote protocol registers. */
344 long sizeof_g_packet
;
346 /* Description of the remote protocol registers indexed by REGNUM
347 (making an array gdbarch_num_regs in size). */
348 struct packet_reg
*regs
;
350 /* This is the size (in chars) of the first response to the ``g''
351 packet. It is used as a heuristic when determining the maximum
352 size of memory-read and memory-write packets. A target will
353 typically only reserve a buffer large enough to hold the ``g''
354 packet. The size does not include packet overhead (headers and
356 long actual_register_packet_size
;
358 /* This is the maximum size (in chars) of a non read/write packet.
359 It is also used as a cap on the size of read/write packets. */
360 long remote_packet_size
;
364 /* Handle for retreving the remote protocol data from gdbarch. */
365 static struct gdbarch_data
*remote_gdbarch_data_handle
;
367 static struct remote_arch_state
*
368 get_remote_arch_state (void)
370 return gdbarch_data (target_gdbarch
, remote_gdbarch_data_handle
);
373 /* Fetch the global remote target state. */
375 static struct remote_state
*
376 get_remote_state (void)
378 /* Make sure that the remote architecture state has been
379 initialized, because doing so might reallocate rs->buf. Any
380 function which calls getpkt also needs to be mindful of changes
381 to rs->buf, but this call limits the number of places which run
383 get_remote_arch_state ();
385 return get_remote_state_raw ();
389 compare_pnums (const void *lhs_
, const void *rhs_
)
391 const struct packet_reg
* const *lhs
= lhs_
;
392 const struct packet_reg
* const *rhs
= rhs_
;
394 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
396 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
403 init_remote_state (struct gdbarch
*gdbarch
)
405 int regnum
, num_remote_regs
, offset
;
406 struct remote_state
*rs
= get_remote_state_raw ();
407 struct remote_arch_state
*rsa
;
408 struct packet_reg
**remote_regs
;
410 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
412 /* Use the architecture to build a regnum<->pnum table, which will be
413 1:1 unless a feature set specifies otherwise. */
414 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
,
415 gdbarch_num_regs (gdbarch
),
417 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
419 struct packet_reg
*r
= &rsa
->regs
[regnum
];
421 if (register_size (gdbarch
, regnum
) == 0)
422 /* Do not try to fetch zero-sized (placeholder) registers. */
425 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
430 /* Define the g/G packet format as the contents of each register
431 with a remote protocol number, in order of ascending protocol
434 remote_regs
= alloca (gdbarch_num_regs (gdbarch
)
435 * sizeof (struct packet_reg
*));
436 for (num_remote_regs
= 0, regnum
= 0;
437 regnum
< gdbarch_num_regs (gdbarch
);
439 if (rsa
->regs
[regnum
].pnum
!= -1)
440 remote_regs
[num_remote_regs
++] = &rsa
->regs
[regnum
];
442 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
445 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
447 remote_regs
[regnum
]->in_g_packet
= 1;
448 remote_regs
[regnum
]->offset
= offset
;
449 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
452 /* Record the maximum possible size of the g packet - it may turn out
454 rsa
->sizeof_g_packet
= offset
;
456 /* Default maximum number of characters in a packet body. Many
457 remote stubs have a hardwired buffer size of 400 bytes
458 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
459 as the maximum packet-size to ensure that the packet and an extra
460 NUL character can always fit in the buffer. This stops GDB
461 trashing stubs that try to squeeze an extra NUL into what is
462 already a full buffer (As of 1999-12-04 that was most stubs). */
463 rsa
->remote_packet_size
= 400 - 1;
465 /* This one is filled in when a ``g'' packet is received. */
466 rsa
->actual_register_packet_size
= 0;
468 /* Should rsa->sizeof_g_packet needs more space than the
469 default, adjust the size accordingly. Remember that each byte is
470 encoded as two characters. 32 is the overhead for the packet
471 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
472 (``$NN:G...#NN'') is a better guess, the below has been padded a
474 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
475 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
477 /* Make sure that the packet buffer is plenty big enough for
478 this architecture. */
479 if (rs
->buf_size
< rsa
->remote_packet_size
)
481 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
482 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
488 /* Return the current allowed size of a remote packet. This is
489 inferred from the current architecture, and should be used to
490 limit the length of outgoing packets. */
492 get_remote_packet_size (void)
494 struct remote_state
*rs
= get_remote_state ();
495 struct remote_arch_state
*rsa
= get_remote_arch_state ();
497 if (rs
->explicit_packet_size
)
498 return rs
->explicit_packet_size
;
500 return rsa
->remote_packet_size
;
503 static struct packet_reg
*
504 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
506 if (regnum
< 0 && regnum
>= gdbarch_num_regs (target_gdbarch
))
510 struct packet_reg
*r
= &rsa
->regs
[regnum
];
511 gdb_assert (r
->regnum
== regnum
);
516 static struct packet_reg
*
517 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
520 for (i
= 0; i
< gdbarch_num_regs (target_gdbarch
); i
++)
522 struct packet_reg
*r
= &rsa
->regs
[i
];
529 /* FIXME: graces/2002-08-08: These variables should eventually be
530 bound to an instance of the target object (as in gdbarch-tdep()),
531 when such a thing exists. */
533 /* This is set to the data address of the access causing the target
534 to stop for a watchpoint. */
535 static CORE_ADDR remote_watch_data_address
;
537 /* This is non-zero if target stopped for a watchpoint. */
538 static int remote_stopped_by_watchpoint_p
;
540 static struct target_ops remote_ops
;
542 static struct target_ops extended_remote_ops
;
544 static int remote_async_mask_value
= 1;
546 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
547 ``forever'' still use the normal timeout mechanism. This is
548 currently used by the ASYNC code to guarentee that target reads
549 during the initial connect always time-out. Once getpkt has been
550 modified to return a timeout indication and, in turn
551 remote_wait()/wait_for_inferior() have gained a timeout parameter
553 static int wait_forever_enabled_p
= 1;
556 /* This variable chooses whether to send a ^C or a break when the user
557 requests program interruption. Although ^C is usually what remote
558 systems expect, and that is the default here, sometimes a break is
559 preferable instead. */
561 static int remote_break
;
563 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
564 remote_open knows that we don't have a file open when the program
566 static struct serial
*remote_desc
= NULL
;
568 /* This variable sets the number of bits in an address that are to be
569 sent in a memory ("M" or "m") packet. Normally, after stripping
570 leading zeros, the entire address would be sent. This variable
571 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
572 initial implementation of remote.c restricted the address sent in
573 memory packets to ``host::sizeof long'' bytes - (typically 32
574 bits). Consequently, for 64 bit targets, the upper 32 bits of an
575 address was never sent. Since fixing this bug may cause a break in
576 some remote targets this variable is principly provided to
577 facilitate backward compatibility. */
579 static int remote_address_size
;
581 /* Temporary to track who currently owns the terminal. See
582 remote_terminal_* for more details. */
584 static int remote_async_terminal_ours_p
;
586 /* The executable file to use for "run" on the remote side. */
588 static char *remote_exec_file
= "";
591 /* User configurable variables for the number of characters in a
592 memory read/write packet. MIN (rsa->remote_packet_size,
593 rsa->sizeof_g_packet) is the default. Some targets need smaller
594 values (fifo overruns, et.al.) and some users need larger values
595 (speed up transfers). The variables ``preferred_*'' (the user
596 request), ``current_*'' (what was actually set) and ``forced_*''
597 (Positive - a soft limit, negative - a hard limit). */
599 struct memory_packet_config
606 /* Compute the current size of a read/write packet. Since this makes
607 use of ``actual_register_packet_size'' the computation is dynamic. */
610 get_memory_packet_size (struct memory_packet_config
*config
)
612 struct remote_state
*rs
= get_remote_state ();
613 struct remote_arch_state
*rsa
= get_remote_arch_state ();
615 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
616 law?) that some hosts don't cope very well with large alloca()
617 calls. Eventually the alloca() code will be replaced by calls to
618 xmalloc() and make_cleanups() allowing this restriction to either
619 be lifted or removed. */
620 #ifndef MAX_REMOTE_PACKET_SIZE
621 #define MAX_REMOTE_PACKET_SIZE 16384
623 /* NOTE: 20 ensures we can write at least one byte. */
624 #ifndef MIN_REMOTE_PACKET_SIZE
625 #define MIN_REMOTE_PACKET_SIZE 20
630 if (config
->size
<= 0)
631 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
633 what_they_get
= config
->size
;
637 what_they_get
= get_remote_packet_size ();
638 /* Limit the packet to the size specified by the user. */
640 && what_they_get
> config
->size
)
641 what_they_get
= config
->size
;
643 /* Limit it to the size of the targets ``g'' response unless we have
644 permission from the stub to use a larger packet size. */
645 if (rs
->explicit_packet_size
== 0
646 && rsa
->actual_register_packet_size
> 0
647 && what_they_get
> rsa
->actual_register_packet_size
)
648 what_they_get
= rsa
->actual_register_packet_size
;
650 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
651 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
652 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
653 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
655 /* Make sure there is room in the global buffer for this packet
656 (including its trailing NUL byte). */
657 if (rs
->buf_size
< what_they_get
+ 1)
659 rs
->buf_size
= 2 * what_they_get
;
660 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
663 return what_they_get
;
666 /* Update the size of a read/write packet. If they user wants
667 something really big then do a sanity check. */
670 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
672 int fixed_p
= config
->fixed_p
;
673 long size
= config
->size
;
675 error (_("Argument required (integer, `fixed' or `limited')."));
676 else if (strcmp (args
, "hard") == 0
677 || strcmp (args
, "fixed") == 0)
679 else if (strcmp (args
, "soft") == 0
680 || strcmp (args
, "limit") == 0)
685 size
= strtoul (args
, &end
, 0);
687 error (_("Invalid %s (bad syntax)."), config
->name
);
689 /* Instead of explicitly capping the size of a packet to
690 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
691 instead allowed to set the size to something arbitrarily
693 if (size
> MAX_REMOTE_PACKET_SIZE
)
694 error (_("Invalid %s (too large)."), config
->name
);
698 if (fixed_p
&& !config
->fixed_p
)
700 if (! query (_("The target may not be able to correctly handle a %s\n"
701 "of %ld bytes. Change the packet size? "),
703 error (_("Packet size not changed."));
705 /* Update the config. */
706 config
->fixed_p
= fixed_p
;
711 show_memory_packet_size (struct memory_packet_config
*config
)
713 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
715 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
716 get_memory_packet_size (config
));
718 printf_filtered (_("Packets are limited to %ld bytes.\n"),
719 get_memory_packet_size (config
));
722 static struct memory_packet_config memory_write_packet_config
=
724 "memory-write-packet-size",
728 set_memory_write_packet_size (char *args
, int from_tty
)
730 set_memory_packet_size (args
, &memory_write_packet_config
);
734 show_memory_write_packet_size (char *args
, int from_tty
)
736 show_memory_packet_size (&memory_write_packet_config
);
740 get_memory_write_packet_size (void)
742 return get_memory_packet_size (&memory_write_packet_config
);
745 static struct memory_packet_config memory_read_packet_config
=
747 "memory-read-packet-size",
751 set_memory_read_packet_size (char *args
, int from_tty
)
753 set_memory_packet_size (args
, &memory_read_packet_config
);
757 show_memory_read_packet_size (char *args
, int from_tty
)
759 show_memory_packet_size (&memory_read_packet_config
);
763 get_memory_read_packet_size (void)
765 long size
= get_memory_packet_size (&memory_read_packet_config
);
766 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
767 extra buffer size argument before the memory read size can be
768 increased beyond this. */
769 if (size
> get_remote_packet_size ())
770 size
= get_remote_packet_size ();
775 /* Generic configuration support for packets the stub optionally
776 supports. Allows the user to specify the use of the packet as well
777 as allowing GDB to auto-detect support in the remote stub. */
781 PACKET_SUPPORT_UNKNOWN
= 0,
790 enum auto_boolean detect
;
791 enum packet_support support
;
794 /* Analyze a packet's return value and update the packet config
805 update_packet_config (struct packet_config
*config
)
807 switch (config
->detect
)
809 case AUTO_BOOLEAN_TRUE
:
810 config
->support
= PACKET_ENABLE
;
812 case AUTO_BOOLEAN_FALSE
:
813 config
->support
= PACKET_DISABLE
;
815 case AUTO_BOOLEAN_AUTO
:
816 config
->support
= PACKET_SUPPORT_UNKNOWN
;
822 show_packet_config_cmd (struct packet_config
*config
)
824 char *support
= "internal-error";
825 switch (config
->support
)
831 support
= "disabled";
833 case PACKET_SUPPORT_UNKNOWN
:
837 switch (config
->detect
)
839 case AUTO_BOOLEAN_AUTO
:
840 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
841 config
->name
, support
);
843 case AUTO_BOOLEAN_TRUE
:
844 case AUTO_BOOLEAN_FALSE
:
845 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
846 config
->name
, support
);
852 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
853 const char *title
, int legacy
)
860 config
->title
= title
;
861 config
->detect
= AUTO_BOOLEAN_AUTO
;
862 config
->support
= PACKET_SUPPORT_UNKNOWN
;
863 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
865 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
867 /* set/show TITLE-packet {auto,on,off} */
868 cmd_name
= xstrprintf ("%s-packet", title
);
869 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
870 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
871 set_remote_protocol_packet_cmd
,
872 show_remote_protocol_packet_cmd
,
873 &remote_set_cmdlist
, &remote_show_cmdlist
);
874 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
878 legacy_name
= xstrprintf ("%s-packet", name
);
879 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
880 &remote_set_cmdlist
);
881 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
882 &remote_show_cmdlist
);
886 static enum packet_result
887 packet_check_result (const char *buf
)
891 /* The stub recognized the packet request. Check that the
892 operation succeeded. */
894 && isxdigit (buf
[1]) && isxdigit (buf
[2])
896 /* "Enn" - definitly an error. */
899 /* Always treat "E." as an error. This will be used for
900 more verbose error messages, such as E.memtypes. */
901 if (buf
[0] == 'E' && buf
[1] == '.')
904 /* The packet may or may not be OK. Just assume it is. */
908 /* The stub does not support the packet. */
909 return PACKET_UNKNOWN
;
912 static enum packet_result
913 packet_ok (const char *buf
, struct packet_config
*config
)
915 enum packet_result result
;
917 result
= packet_check_result (buf
);
922 /* The stub recognized the packet request. */
923 switch (config
->support
)
925 case PACKET_SUPPORT_UNKNOWN
:
927 fprintf_unfiltered (gdb_stdlog
,
928 "Packet %s (%s) is supported\n",
929 config
->name
, config
->title
);
930 config
->support
= PACKET_ENABLE
;
933 internal_error (__FILE__
, __LINE__
,
934 _("packet_ok: attempt to use a disabled packet"));
941 /* The stub does not support the packet. */
942 switch (config
->support
)
945 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
946 /* If the stub previously indicated that the packet was
947 supported then there is a protocol error.. */
948 error (_("Protocol error: %s (%s) conflicting enabled responses."),
949 config
->name
, config
->title
);
951 /* The user set it wrong. */
952 error (_("Enabled packet %s (%s) not recognized by stub"),
953 config
->name
, config
->title
);
955 case PACKET_SUPPORT_UNKNOWN
:
957 fprintf_unfiltered (gdb_stdlog
,
958 "Packet %s (%s) is NOT supported\n",
959 config
->name
, config
->title
);
960 config
->support
= PACKET_DISABLE
;
988 PACKET_qXfer_features
,
989 PACKET_qXfer_libraries
,
990 PACKET_qXfer_memory_map
,
991 PACKET_qXfer_spu_read
,
992 PACKET_qXfer_spu_write
,
996 PACKET_qSearch_memory
,
999 PACKET_QStartNoAckMode
,
1004 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
1007 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
1008 struct cmd_list_element
*c
)
1010 struct packet_config
*packet
;
1012 for (packet
= remote_protocol_packets
;
1013 packet
< &remote_protocol_packets
[PACKET_MAX
];
1016 if (&packet
->detect
== c
->var
)
1018 update_packet_config (packet
);
1022 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1027 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
1028 struct cmd_list_element
*c
,
1031 struct packet_config
*packet
;
1033 for (packet
= remote_protocol_packets
;
1034 packet
< &remote_protocol_packets
[PACKET_MAX
];
1037 if (&packet
->detect
== c
->var
)
1039 show_packet_config_cmd (packet
);
1043 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1047 /* Should we try one of the 'Z' requests? */
1051 Z_PACKET_SOFTWARE_BP
,
1052 Z_PACKET_HARDWARE_BP
,
1059 /* For compatibility with older distributions. Provide a ``set remote
1060 Z-packet ...'' command that updates all the Z packet types. */
1062 static enum auto_boolean remote_Z_packet_detect
;
1065 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1066 struct cmd_list_element
*c
)
1069 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1071 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1072 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1077 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1078 struct cmd_list_element
*c
,
1082 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1084 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1088 /* Should we try the 'ThreadInfo' query packet?
1090 This variable (NOT available to the user: auto-detect only!)
1091 determines whether GDB will use the new, simpler "ThreadInfo"
1092 query or the older, more complex syntax for thread queries.
1093 This is an auto-detect variable (set to true at each connect,
1094 and set to false when the target fails to recognize it). */
1096 static int use_threadinfo_query
;
1097 static int use_threadextra_query
;
1099 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1100 static struct async_signal_handler
*sigint_remote_twice_token
;
1101 static struct async_signal_handler
*sigint_remote_token
;
1104 /* Asynchronous signal handle registered as event loop source for
1105 when we have pending events ready to be passed to the core. */
1107 static struct async_event_handler
*remote_async_inferior_event_token
;
1109 /* Asynchronous signal handle registered as event loop source for when
1110 the remote sent us a %Stop notification. The registered callback
1111 will do a vStopped sequence to pull the rest of the events out of
1112 the remote side into our event queue. */
1114 static struct async_event_handler
*remote_async_get_pending_events_token
;
1117 static ptid_t magic_null_ptid
;
1118 static ptid_t not_sent_ptid
;
1119 static ptid_t any_thread_ptid
;
1121 /* These are the threads which we last sent to the remote system. The
1122 TID member will be -1 for all or -2 for not sent yet. */
1124 static ptid_t general_thread
;
1125 static ptid_t continue_thread
;
1128 notice_new_inferiors (ptid_t currthread
)
1130 /* If this is a new thread, add it to GDB's thread list.
1131 If we leave it up to WFI to do this, bad things will happen. */
1133 if (in_thread_list (currthread
) && is_exited (currthread
))
1135 /* We're seeing an event on a thread id we knew had exited.
1136 This has to be a new thread reusing the old id. Add it. */
1137 add_thread (currthread
);
1141 if (!in_thread_list (currthread
))
1143 if (ptid_equal (pid_to_ptid (ptid_get_pid (currthread
)), inferior_ptid
))
1145 /* inferior_ptid has no thread member yet. This can happen
1146 with the vAttach -> remote_wait,"TAAthread:" path if the
1147 stub doesn't support qC. This is the first stop reported
1148 after an attach, so this is the main thread. Update the
1149 ptid in the thread list. */
1150 thread_change_ptid (inferior_ptid
, currthread
);
1154 if (ptid_equal (magic_null_ptid
, inferior_ptid
))
1156 /* inferior_ptid is not set yet. This can happen with the
1157 vRun -> remote_wait,"TAAthread:" path if the stub
1158 doesn't support qC. This is the first stop reported
1159 after an attach, so this is the main thread. Update the
1160 ptid in the thread list. */
1161 thread_change_ptid (inferior_ptid
, currthread
);
1165 /* When connecting to a target remote, or to a target
1166 extended-remote which already was debugging an inferior, we
1167 may not know about it yet. Add it before adding its child
1168 thread, so notifications are emitted in a sensible order. */
1169 if (!in_inferior_list (ptid_get_pid (currthread
)))
1170 add_inferior (ptid_get_pid (currthread
));
1172 /* This is really a new thread. Add it. */
1173 add_thread (currthread
);
1177 /* Call this function as a result of
1178 1) A halt indication (T packet) containing a thread id
1179 2) A direct query of currthread
1180 3) Successful execution of set thread
1184 record_currthread (ptid_t currthread
)
1186 general_thread
= currthread
;
1188 if (ptid_equal (currthread
, minus_one_ptid
))
1189 /* We're just invalidating the local thread mirror. */
1192 notice_new_inferiors (currthread
);
1195 static char *last_pass_packet
;
1197 /* If 'QPassSignals' is supported, tell the remote stub what signals
1198 it can simply pass through to the inferior without reporting. */
1201 remote_pass_signals (void)
1203 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1205 char *pass_packet
, *p
;
1206 int numsigs
= (int) TARGET_SIGNAL_LAST
;
1209 gdb_assert (numsigs
< 256);
1210 for (i
= 0; i
< numsigs
; i
++)
1212 if (signal_stop_state (i
) == 0
1213 && signal_print_state (i
) == 0
1214 && signal_pass_state (i
) == 1)
1217 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1218 strcpy (pass_packet
, "QPassSignals:");
1219 p
= pass_packet
+ strlen (pass_packet
);
1220 for (i
= 0; i
< numsigs
; i
++)
1222 if (signal_stop_state (i
) == 0
1223 && signal_print_state (i
) == 0
1224 && signal_pass_state (i
) == 1)
1227 *p
++ = tohex (i
>> 4);
1228 *p
++ = tohex (i
& 15);
1237 if (!last_pass_packet
|| strcmp (last_pass_packet
, pass_packet
))
1239 struct remote_state
*rs
= get_remote_state ();
1240 char *buf
= rs
->buf
;
1242 putpkt (pass_packet
);
1243 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1244 packet_ok (buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1245 if (last_pass_packet
)
1246 xfree (last_pass_packet
);
1247 last_pass_packet
= pass_packet
;
1250 xfree (pass_packet
);
1254 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1255 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1256 thread. If GEN is set, set the general thread, if not, then set
1257 the step/continue thread. */
1259 set_thread (struct ptid ptid
, int gen
)
1261 struct remote_state
*rs
= get_remote_state ();
1262 ptid_t state
= gen
? general_thread
: continue_thread
;
1263 char *buf
= rs
->buf
;
1264 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1266 if (ptid_equal (state
, ptid
))
1270 *buf
++ = gen
? 'g' : 'c';
1271 if (ptid_equal (ptid
, magic_null_ptid
))
1272 xsnprintf (buf
, endbuf
- buf
, "0");
1273 else if (ptid_equal (ptid
, any_thread_ptid
))
1274 xsnprintf (buf
, endbuf
- buf
, "0");
1275 else if (ptid_equal (ptid
, minus_one_ptid
))
1276 xsnprintf (buf
, endbuf
- buf
, "-1");
1278 write_ptid (buf
, endbuf
, ptid
);
1280 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1282 general_thread
= ptid
;
1284 continue_thread
= ptid
;
1288 set_general_thread (struct ptid ptid
)
1290 set_thread (ptid
, 1);
1294 set_continue_thread (struct ptid ptid
)
1296 set_thread (ptid
, 0);
1299 /* Change the remote current process. Which thread within the process
1300 ends up selected isn't important, as long as it is the same process
1301 as what INFERIOR_PTID points to.
1303 This comes from that fact that there is no explicit notion of
1304 "selected process" in the protocol. The selected process for
1305 general operations is the process the selected general thread
1309 set_general_process (void)
1311 struct remote_state
*rs
= get_remote_state ();
1313 /* If the remote can't handle multiple processes, don't bother. */
1314 if (!remote_multi_process_p (rs
))
1317 /* We only need to change the remote current thread if it's pointing
1318 at some other process. */
1319 if (ptid_get_pid (general_thread
) != ptid_get_pid (inferior_ptid
))
1320 set_general_thread (inferior_ptid
);
1324 /* Return nonzero if the thread PTID is still alive on the remote
1328 remote_thread_alive (ptid_t ptid
)
1330 struct remote_state
*rs
= get_remote_state ();
1331 int tid
= ptid_get_tid (ptid
);
1334 if (ptid_equal (ptid
, magic_null_ptid
))
1335 /* The main thread is always alive. */
1338 if (ptid_get_pid (ptid
) != 0 && ptid_get_tid (ptid
) == 0)
1339 /* The main thread is always alive. This can happen after a
1340 vAttach, if the remote side doesn't support
1345 endp
= rs
->buf
+ get_remote_packet_size ();
1348 write_ptid (p
, endp
, ptid
);
1351 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1352 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1355 /* About these extended threadlist and threadinfo packets. They are
1356 variable length packets but, the fields within them are often fixed
1357 length. They are redundent enough to send over UDP as is the
1358 remote protocol in general. There is a matching unit test module
1361 #define OPAQUETHREADBYTES 8
1363 /* a 64 bit opaque identifier */
1364 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1366 /* WARNING: This threadref data structure comes from the remote O.S.,
1367 libstub protocol encoding, and remote.c. it is not particularly
1370 /* Right now, the internal structure is int. We want it to be bigger.
1374 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1376 /* gdb_ext_thread_info is an internal GDB data structure which is
1377 equivalent to the reply of the remote threadinfo packet. */
1379 struct gdb_ext_thread_info
1381 threadref threadid
; /* External form of thread reference. */
1382 int active
; /* Has state interesting to GDB?
1384 char display
[256]; /* Brief state display, name,
1385 blocked/suspended. */
1386 char shortname
[32]; /* To be used to name threads. */
1387 char more_display
[256]; /* Long info, statistics, queue depth,
1391 /* The volume of remote transfers can be limited by submitting
1392 a mask containing bits specifying the desired information.
1393 Use a union of these values as the 'selection' parameter to
1394 get_thread_info. FIXME: Make these TAG names more thread specific.
1397 #define TAG_THREADID 1
1398 #define TAG_EXISTS 2
1399 #define TAG_DISPLAY 4
1400 #define TAG_THREADNAME 8
1401 #define TAG_MOREDISPLAY 16
1403 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1405 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1407 static char *unpack_nibble (char *buf
, int *val
);
1409 static char *pack_nibble (char *buf
, int nibble
);
1411 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1413 static char *unpack_byte (char *buf
, int *value
);
1415 static char *pack_int (char *buf
, int value
);
1417 static char *unpack_int (char *buf
, int *value
);
1419 static char *unpack_string (char *src
, char *dest
, int length
);
1421 static char *pack_threadid (char *pkt
, threadref
*id
);
1423 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1425 void int_to_threadref (threadref
*id
, int value
);
1427 static int threadref_to_int (threadref
*ref
);
1429 static void copy_threadref (threadref
*dest
, threadref
*src
);
1431 static int threadmatch (threadref
*dest
, threadref
*src
);
1433 static char *pack_threadinfo_request (char *pkt
, int mode
,
1436 static int remote_unpack_thread_info_response (char *pkt
,
1437 threadref
*expectedref
,
1438 struct gdb_ext_thread_info
1442 static int remote_get_threadinfo (threadref
*threadid
,
1443 int fieldset
, /*TAG mask */
1444 struct gdb_ext_thread_info
*info
);
1446 static char *pack_threadlist_request (char *pkt
, int startflag
,
1448 threadref
*nextthread
);
1450 static int parse_threadlist_response (char *pkt
,
1452 threadref
*original_echo
,
1453 threadref
*resultlist
,
1456 static int remote_get_threadlist (int startflag
,
1457 threadref
*nextthread
,
1461 threadref
*threadlist
);
1463 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1465 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1466 void *context
, int looplimit
);
1468 static int remote_newthread_step (threadref
*ref
, void *context
);
1471 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1472 buffer we're allowed to write to. Returns
1473 BUF+CHARACTERS_WRITTEN. */
1476 write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
1479 struct remote_state
*rs
= get_remote_state ();
1481 if (remote_multi_process_p (rs
))
1483 pid
= ptid_get_pid (ptid
);
1485 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
1487 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
1489 tid
= ptid_get_tid (ptid
);
1491 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
1493 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
1498 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
1499 passed the last parsed char. Returns null_ptid on error. */
1502 read_ptid (char *buf
, char **obuf
)
1506 ULONGEST pid
= 0, tid
= 0;
1511 /* Multi-process ptid. */
1512 pp
= unpack_varlen_hex (p
+ 1, &pid
);
1514 error (_("invalid remote ptid: %s\n"), p
);
1517 pp
= unpack_varlen_hex (p
+ 1, &tid
);
1520 return ptid_build (pid
, 0, tid
);
1523 /* No multi-process. Just a tid. */
1524 pp
= unpack_varlen_hex (p
, &tid
);
1526 /* Since the stub is not sending a process id, then default to
1527 what's in inferior_ptid. */
1528 pid
= ptid_get_pid (inferior_ptid
);
1532 return ptid_build (pid
, 0, tid
);
1535 /* Encode 64 bits in 16 chars of hex. */
1537 static const char hexchars
[] = "0123456789abcdef";
1540 ishex (int ch
, int *val
)
1542 if ((ch
>= 'a') && (ch
<= 'f'))
1544 *val
= ch
- 'a' + 10;
1547 if ((ch
>= 'A') && (ch
<= 'F'))
1549 *val
= ch
- 'A' + 10;
1552 if ((ch
>= '0') && (ch
<= '9'))
1563 if (ch
>= 'a' && ch
<= 'f')
1564 return ch
- 'a' + 10;
1565 if (ch
>= '0' && ch
<= '9')
1567 if (ch
>= 'A' && ch
<= 'F')
1568 return ch
- 'A' + 10;
1573 stub_unpack_int (char *buff
, int fieldlength
)
1580 nibble
= stubhex (*buff
++);
1584 retval
= retval
<< 4;
1590 unpack_varlen_hex (char *buff
, /* packet to parse */
1594 ULONGEST retval
= 0;
1596 while (ishex (*buff
, &nibble
))
1599 retval
= retval
<< 4;
1600 retval
|= nibble
& 0x0f;
1607 unpack_nibble (char *buf
, int *val
)
1609 *val
= fromhex (*buf
++);
1614 pack_nibble (char *buf
, int nibble
)
1616 *buf
++ = hexchars
[(nibble
& 0x0f)];
1621 pack_hex_byte (char *pkt
, int byte
)
1623 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1624 *pkt
++ = hexchars
[(byte
& 0xf)];
1629 unpack_byte (char *buf
, int *value
)
1631 *value
= stub_unpack_int (buf
, 2);
1636 pack_int (char *buf
, int value
)
1638 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1639 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1640 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1641 buf
= pack_hex_byte (buf
, (value
& 0xff));
1646 unpack_int (char *buf
, int *value
)
1648 *value
= stub_unpack_int (buf
, 8);
1652 #if 0 /* Currently unused, uncomment when needed. */
1653 static char *pack_string (char *pkt
, char *string
);
1656 pack_string (char *pkt
, char *string
)
1661 len
= strlen (string
);
1663 len
= 200; /* Bigger than most GDB packets, junk??? */
1664 pkt
= pack_hex_byte (pkt
, len
);
1668 if ((ch
== '\0') || (ch
== '#'))
1669 ch
= '*'; /* Protect encapsulation. */
1674 #endif /* 0 (unused) */
1677 unpack_string (char *src
, char *dest
, int length
)
1686 pack_threadid (char *pkt
, threadref
*id
)
1689 unsigned char *altid
;
1691 altid
= (unsigned char *) id
;
1692 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1694 pkt
= pack_hex_byte (pkt
, *altid
++);
1700 unpack_threadid (char *inbuf
, threadref
*id
)
1703 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1706 altref
= (char *) id
;
1708 while (inbuf
< limit
)
1710 x
= stubhex (*inbuf
++);
1711 y
= stubhex (*inbuf
++);
1712 *altref
++ = (x
<< 4) | y
;
1717 /* Externally, threadrefs are 64 bits but internally, they are still
1718 ints. This is due to a mismatch of specifications. We would like
1719 to use 64bit thread references internally. This is an adapter
1723 int_to_threadref (threadref
*id
, int value
)
1725 unsigned char *scan
;
1727 scan
= (unsigned char *) id
;
1733 *scan
++ = (value
>> 24) & 0xff;
1734 *scan
++ = (value
>> 16) & 0xff;
1735 *scan
++ = (value
>> 8) & 0xff;
1736 *scan
++ = (value
& 0xff);
1740 threadref_to_int (threadref
*ref
)
1743 unsigned char *scan
;
1749 value
= (value
<< 8) | ((*scan
++) & 0xff);
1754 copy_threadref (threadref
*dest
, threadref
*src
)
1757 unsigned char *csrc
, *cdest
;
1759 csrc
= (unsigned char *) src
;
1760 cdest
= (unsigned char *) dest
;
1767 threadmatch (threadref
*dest
, threadref
*src
)
1769 /* Things are broken right now, so just assume we got a match. */
1771 unsigned char *srcp
, *destp
;
1773 srcp
= (char *) src
;
1774 destp
= (char *) dest
;
1778 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1785 threadid:1, # always request threadid
1792 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1795 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1797 *pkt
++ = 'q'; /* Info Query */
1798 *pkt
++ = 'P'; /* process or thread info */
1799 pkt
= pack_int (pkt
, mode
); /* mode */
1800 pkt
= pack_threadid (pkt
, id
); /* threadid */
1801 *pkt
= '\0'; /* terminate */
1805 /* These values tag the fields in a thread info response packet. */
1806 /* Tagging the fields allows us to request specific fields and to
1807 add more fields as time goes by. */
1809 #define TAG_THREADID 1 /* Echo the thread identifier. */
1810 #define TAG_EXISTS 2 /* Is this process defined enough to
1811 fetch registers and its stack? */
1812 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1813 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1814 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1818 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1819 struct gdb_ext_thread_info
*info
)
1821 struct remote_state
*rs
= get_remote_state ();
1825 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
1828 /* info->threadid = 0; FIXME: implement zero_threadref. */
1830 info
->display
[0] = '\0';
1831 info
->shortname
[0] = '\0';
1832 info
->more_display
[0] = '\0';
1834 /* Assume the characters indicating the packet type have been
1836 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1837 pkt
= unpack_threadid (pkt
, &ref
);
1840 warning (_("Incomplete response to threadinfo request."));
1841 if (!threadmatch (&ref
, expectedref
))
1842 { /* This is an answer to a different request. */
1843 warning (_("ERROR RMT Thread info mismatch."));
1846 copy_threadref (&info
->threadid
, &ref
);
1848 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1850 /* Packets are terminated with nulls. */
1851 while ((pkt
< limit
) && mask
&& *pkt
)
1853 pkt
= unpack_int (pkt
, &tag
); /* tag */
1854 pkt
= unpack_byte (pkt
, &length
); /* length */
1855 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1857 warning (_("ERROR RMT: threadinfo tag mismatch."));
1861 if (tag
== TAG_THREADID
)
1865 warning (_("ERROR RMT: length of threadid is not 16."));
1869 pkt
= unpack_threadid (pkt
, &ref
);
1870 mask
= mask
& ~TAG_THREADID
;
1873 if (tag
== TAG_EXISTS
)
1875 info
->active
= stub_unpack_int (pkt
, length
);
1877 mask
= mask
& ~(TAG_EXISTS
);
1880 warning (_("ERROR RMT: 'exists' length too long."));
1886 if (tag
== TAG_THREADNAME
)
1888 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1889 mask
= mask
& ~TAG_THREADNAME
;
1892 if (tag
== TAG_DISPLAY
)
1894 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1895 mask
= mask
& ~TAG_DISPLAY
;
1898 if (tag
== TAG_MOREDISPLAY
)
1900 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1901 mask
= mask
& ~TAG_MOREDISPLAY
;
1904 warning (_("ERROR RMT: unknown thread info tag."));
1905 break; /* Not a tag we know about. */
1911 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1912 struct gdb_ext_thread_info
*info
)
1914 struct remote_state
*rs
= get_remote_state ();
1917 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
1919 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1921 if (rs
->buf
[0] == '\0')
1924 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
1929 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1932 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1933 threadref
*nextthread
)
1935 *pkt
++ = 'q'; /* info query packet */
1936 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1937 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1938 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1939 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1944 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1947 parse_threadlist_response (char *pkt
, int result_limit
,
1948 threadref
*original_echo
, threadref
*resultlist
,
1951 struct remote_state
*rs
= get_remote_state ();
1953 int count
, resultcount
, done
;
1956 /* Assume the 'q' and 'M chars have been stripped. */
1957 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
1958 /* done parse past here */
1959 pkt
= unpack_byte (pkt
, &count
); /* count field */
1960 pkt
= unpack_nibble (pkt
, &done
);
1961 /* The first threadid is the argument threadid. */
1962 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1963 while ((count
-- > 0) && (pkt
< limit
))
1965 pkt
= unpack_threadid (pkt
, resultlist
++);
1966 if (resultcount
++ >= result_limit
)
1975 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1976 int *done
, int *result_count
, threadref
*threadlist
)
1978 struct remote_state
*rs
= get_remote_state ();
1979 static threadref echo_nextthread
;
1982 /* Trancate result limit to be smaller than the packet size. */
1983 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= get_remote_packet_size ())
1984 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
1986 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
1988 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1990 if (*rs
->buf
== '\0')
1994 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
1997 if (!threadmatch (&echo_nextthread
, nextthread
))
1999 /* FIXME: This is a good reason to drop the packet. */
2000 /* Possably, there is a duplicate response. */
2002 retransmit immediatly - race conditions
2003 retransmit after timeout - yes
2005 wait for packet, then exit
2007 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2008 return 0; /* I choose simply exiting. */
2010 if (*result_count
<= 0)
2014 warning (_("RMT ERROR : failed to get remote thread list."));
2017 return result
; /* break; */
2019 if (*result_count
> result_limit
)
2022 warning (_("RMT ERROR: threadlist response longer than requested."));
2028 /* This is the interface between remote and threads, remotes upper
2031 /* remote_find_new_threads retrieves the thread list and for each
2032 thread in the list, looks up the thread in GDB's internal list,
2033 adding the thread if it does not already exist. This involves
2034 getting partial thread lists from the remote target so, polling the
2035 quit_flag is required. */
2038 /* About this many threadisds fit in a packet. */
2040 #define MAXTHREADLISTRESULTS 32
2043 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2046 int done
, i
, result_count
;
2050 static threadref nextthread
;
2051 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
2056 if (loopcount
++ > looplimit
)
2059 warning (_("Remote fetch threadlist -infinite loop-."));
2062 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
2063 &done
, &result_count
, resultthreadlist
))
2068 /* Clear for later iterations. */
2070 /* Setup to resume next batch of thread references, set nextthread. */
2071 if (result_count
>= 1)
2072 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
2074 while (result_count
--)
2075 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
2082 remote_newthread_step (threadref
*ref
, void *context
)
2084 int pid
= ptid_get_pid (inferior_ptid
);
2085 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2087 if (!in_thread_list (ptid
))
2089 return 1; /* continue iterator */
2092 #define CRAZY_MAX_THREADS 1000
2095 remote_current_thread (ptid_t oldpid
)
2097 struct remote_state
*rs
= get_remote_state ();
2103 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2104 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2105 return read_ptid (&rs
->buf
[2], NULL
);
2110 /* Find new threads for info threads command.
2111 * Original version, using John Metzler's thread protocol.
2115 remote_find_new_threads (void)
2117 remote_threadlist_iterator (remote_newthread_step
, 0,
2122 * Find all threads for info threads command.
2123 * Uses new thread protocol contributed by Cisco.
2124 * Falls back and attempts to use the older method (above)
2125 * if the target doesn't respond to the new method.
2129 remote_threads_info (void)
2131 struct remote_state
*rs
= get_remote_state ();
2135 if (remote_desc
== 0) /* paranoia */
2136 error (_("Command can only be used when connected to the remote target."));
2138 if (use_threadinfo_query
)
2140 putpkt ("qfThreadInfo");
2141 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2143 if (bufp
[0] != '\0') /* q packet recognized */
2145 while (*bufp
++ == 'm') /* reply contains one or more TID */
2149 new_thread
= read_ptid (bufp
, &bufp
);
2150 if (!ptid_equal (new_thread
, null_ptid
)
2151 && (!in_thread_list (new_thread
)
2152 || is_exited (new_thread
)))
2154 /* When connected to a multi-process aware stub,
2155 "info threads" may show up threads of
2156 inferiors we didn't know about yet. Add them
2157 now, and before adding any of its child
2158 threads, so notifications are emitted in a
2160 if (!in_inferior_list (ptid_get_pid (new_thread
)))
2161 add_inferior (ptid_get_pid (new_thread
));
2163 add_thread (new_thread
);
2165 /* In non-stop mode, we assume new found threads
2166 are running until we proven otherwise with a
2167 stop reply. In all-stop, we can only get
2168 here if all threads are stopped. */
2169 set_executing (new_thread
, non_stop
? 1 : 0);
2170 set_running (new_thread
, non_stop
? 1 : 0);
2173 while (*bufp
++ == ','); /* comma-separated list */
2174 putpkt ("qsThreadInfo");
2175 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2182 /* Only qfThreadInfo is supported in non-stop mode. */
2186 /* Else fall back to old method based on jmetzler protocol. */
2187 use_threadinfo_query
= 0;
2188 remote_find_new_threads ();
2193 * Collect a descriptive string about the given thread.
2194 * The target may say anything it wants to about the thread
2195 * (typically info about its blocked / runnable state, name, etc.).
2196 * This string will appear in the info threads display.
2198 * Optional: targets are not required to implement this function.
2202 remote_threads_extra_info (struct thread_info
*tp
)
2204 struct remote_state
*rs
= get_remote_state ();
2208 struct gdb_ext_thread_info threadinfo
;
2209 static char display_buf
[100]; /* arbitrary... */
2210 int n
= 0; /* position in display_buf */
2212 if (remote_desc
== 0) /* paranoia */
2213 internal_error (__FILE__
, __LINE__
,
2214 _("remote_threads_extra_info"));
2216 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2217 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2218 /* This is the main thread which was added by GDB. The remote
2219 server doesn't know about it. */
2222 if (use_threadextra_query
)
2225 char *endb
= rs
->buf
+ get_remote_packet_size ();
2227 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2229 write_ptid (b
, endb
, tp
->ptid
);
2232 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2233 if (rs
->buf
[0] != 0)
2235 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2236 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2237 display_buf
[result
] = '\0';
2242 /* If the above query fails, fall back to the old method. */
2243 use_threadextra_query
= 0;
2244 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2245 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2246 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2247 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2248 if (threadinfo
.active
)
2250 if (*threadinfo
.shortname
)
2251 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2252 " Name: %s,", threadinfo
.shortname
);
2253 if (*threadinfo
.display
)
2254 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2255 " State: %s,", threadinfo
.display
);
2256 if (*threadinfo
.more_display
)
2257 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2258 " Priority: %s", threadinfo
.more_display
);
2262 /* For purely cosmetic reasons, clear up trailing commas. */
2263 if (',' == display_buf
[n
-1])
2264 display_buf
[n
-1] = ' ';
2272 /* Restart the remote side; this is an extended protocol operation. */
2275 extended_remote_restart (void)
2277 struct remote_state
*rs
= get_remote_state ();
2279 /* Send the restart command; for reasons I don't understand the
2280 remote side really expects a number after the "R". */
2281 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2284 remote_fileio_reset ();
2287 /* Clean up connection to a remote debugger. */
2290 remote_close (int quitting
)
2292 if (remote_desc
== NULL
)
2293 return; /* already closed */
2295 /* Make sure we leave stdin registered in the event loop, and we
2296 don't leave the async SIGINT signal handler installed. */
2297 remote_terminal_ours ();
2299 serial_close (remote_desc
);
2302 /* We don't have a connection to the remote stub anymore. Get rid
2303 of all the inferiors and their threads we were controlling. */
2304 discard_all_inferiors ();
2306 /* We're no longer interested in any of these events. */
2307 discard_pending_stop_replies (-1);
2309 if (remote_async_inferior_event_token
)
2310 delete_async_event_handler (&remote_async_inferior_event_token
);
2311 if (remote_async_get_pending_events_token
)
2312 delete_async_event_handler (&remote_async_get_pending_events_token
);
2314 generic_mourn_inferior ();
2317 /* Query the remote side for the text, data and bss offsets. */
2322 struct remote_state
*rs
= get_remote_state ();
2325 int lose
, num_segments
= 0, do_sections
, do_segments
;
2326 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
2327 struct section_offsets
*offs
;
2328 struct symfile_segment_data
*data
;
2330 if (symfile_objfile
== NULL
)
2333 putpkt ("qOffsets");
2334 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2337 if (buf
[0] == '\000')
2338 return; /* Return silently. Stub doesn't support
2342 warning (_("Remote failure reply: %s"), buf
);
2346 /* Pick up each field in turn. This used to be done with scanf, but
2347 scanf will make trouble if CORE_ADDR size doesn't match
2348 conversion directives correctly. The following code will work
2349 with any size of CORE_ADDR. */
2350 text_addr
= data_addr
= bss_addr
= 0;
2354 if (strncmp (ptr
, "Text=", 5) == 0)
2357 /* Don't use strtol, could lose on big values. */
2358 while (*ptr
&& *ptr
!= ';')
2359 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2361 if (strncmp (ptr
, ";Data=", 6) == 0)
2364 while (*ptr
&& *ptr
!= ';')
2365 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2370 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
2373 while (*ptr
&& *ptr
!= ';')
2374 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
2376 if (bss_addr
!= data_addr
)
2377 warning (_("Target reported unsupported offsets: %s"), buf
);
2382 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
2385 /* Don't use strtol, could lose on big values. */
2386 while (*ptr
&& *ptr
!= ';')
2387 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2390 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
2393 while (*ptr
&& *ptr
!= ';')
2394 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2402 error (_("Malformed response to offset query, %s"), buf
);
2403 else if (*ptr
!= '\0')
2404 warning (_("Target reported unsupported offsets: %s"), buf
);
2406 offs
= ((struct section_offsets
*)
2407 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
2408 memcpy (offs
, symfile_objfile
->section_offsets
,
2409 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
2411 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
2412 do_segments
= (data
!= NULL
);
2413 do_sections
= num_segments
== 0;
2415 if (num_segments
> 0)
2417 segments
[0] = text_addr
;
2418 segments
[1] = data_addr
;
2420 /* If we have two segments, we can still try to relocate everything
2421 by assuming that the .text and .data offsets apply to the whole
2422 text and data segments. Convert the offsets given in the packet
2423 to base addresses for symfile_map_offsets_to_segments. */
2424 else if (data
&& data
->num_segments
== 2)
2426 segments
[0] = data
->segment_bases
[0] + text_addr
;
2427 segments
[1] = data
->segment_bases
[1] + data_addr
;
2430 /* If the object file has only one segment, assume that it is text
2431 rather than data; main programs with no writable data are rare,
2432 but programs with no code are useless. Of course the code might
2433 have ended up in the data segment... to detect that we would need
2434 the permissions here. */
2435 else if (data
&& data
->num_segments
== 1)
2437 segments
[0] = data
->segment_bases
[0] + text_addr
;
2440 /* There's no way to relocate by segment. */
2446 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
2447 offs
, num_segments
, segments
);
2449 if (ret
== 0 && !do_sections
)
2450 error (_("Can not handle qOffsets TextSeg response with this symbol file"));
2457 free_symfile_segment_data (data
);
2461 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
2463 /* This is a temporary kludge to force data and bss to use the same offsets
2464 because that's what nlmconv does now. The real solution requires changes
2465 to the stub and remote.c that I don't have time to do right now. */
2467 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
2468 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
2471 objfile_relocate (symfile_objfile
, offs
);
2474 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
2475 threads we know are stopped already. This is used during the
2476 initial remote connection in non-stop mode --- threads that are
2477 reported as already being stopped are left stopped. */
2480 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
2482 /* If we have a stop reply for this thread, it must be stopped. */
2483 if (peek_stop_reply (thread
->ptid
))
2484 set_stop_requested (thread
->ptid
, 1);
2489 /* Stub for catch_exception. */
2491 struct start_remote_args
2495 /* The current target. */
2496 struct target_ops
*target
;
2498 /* Non-zero if this is an extended-remote target. */
2503 remote_start_remote (struct ui_out
*uiout
, void *opaque
)
2505 struct start_remote_args
*args
= opaque
;
2506 struct remote_state
*rs
= get_remote_state ();
2507 struct packet_config
*noack_config
;
2508 char *wait_status
= NULL
;
2510 immediate_quit
++; /* Allow user to interrupt it. */
2512 /* Ack any packet which the remote side has already sent. */
2513 serial_write (remote_desc
, "+", 1);
2515 /* The first packet we send to the target is the optional "supported
2516 packets" request. If the target can answer this, it will tell us
2517 which later probes to skip. */
2518 remote_query_supported ();
2520 /* Next, we possibly activate noack mode.
2522 If the QStartNoAckMode packet configuration is set to AUTO,
2523 enable noack mode if the stub reported a wish for it with
2526 If set to TRUE, then enable noack mode even if the stub didn't
2527 report it in qSupported. If the stub doesn't reply OK, the
2528 session ends with an error.
2530 If FALSE, then don't activate noack mode, regardless of what the
2531 stub claimed should be the default with qSupported. */
2533 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
2535 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
2536 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
2537 && noack_config
->support
== PACKET_ENABLE
))
2539 putpkt ("QStartNoAckMode");
2540 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2541 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
2545 if (args
->extended_p
)
2547 /* Tell the remote that we are using the extended protocol. */
2549 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2552 /* On OSs where the list of libraries is global to all
2553 processes, we fetch them early. */
2554 if (gdbarch_has_global_solist (target_gdbarch
))
2555 solib_add (NULL
, args
->from_tty
, args
->target
, auto_solib_add
);
2557 /* Next, if the target can specify a description, read it. We do
2558 this before anything involving memory or registers. */
2559 target_find_description ();
2563 if (!rs
->non_stop_aware
)
2564 error (_("Non-stop mode requested, but remote does not support non-stop"));
2566 putpkt ("QNonStop:1");
2567 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2569 if (strcmp (rs
->buf
, "OK") != 0)
2570 error ("Remote refused setting non-stop mode with: %s", rs
->buf
);
2572 /* Find about threads and processes the stub is already
2573 controlling. We default to adding them in the running state.
2574 The '?' query below will then tell us about which threads are
2577 /* If we're not using the multi-process extensions, there's no
2578 way to know the pid of the reported threads; use the magic
2580 if (!remote_multi_process_p (rs
))
2581 inferior_ptid
= magic_null_ptid
;
2583 remote_threads_info ();
2585 else if (rs
->non_stop_aware
)
2587 /* Don't assume that the stub can operate in all-stop mode.
2588 Request it explicitely. */
2589 putpkt ("QNonStop:0");
2590 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2592 if (strcmp (rs
->buf
, "OK") != 0)
2593 error ("Remote refused setting all-stop mode with: %s", rs
->buf
);
2596 /* Check whether the target is running now. */
2598 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2602 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
2604 if (args
->extended_p
)
2606 /* We're connected, but not running. Drop out before we
2607 call start_remote. */
2608 target_mark_exited (args
->target
);
2612 error (_("The target is not running (try extended-remote?)"));
2616 if (args
->extended_p
)
2617 target_mark_running (args
->target
);
2619 /* Save the reply for later. */
2620 wait_status
= alloca (strlen (rs
->buf
) + 1);
2621 strcpy (wait_status
, rs
->buf
);
2624 /* Let the stub know that we want it to return the thread. */
2625 set_continue_thread (minus_one_ptid
);
2627 /* Without this, some commands which require an active target
2628 (such as kill) won't work. This variable serves (at least)
2629 double duty as both the pid of the target process (if it has
2630 such), and as a flag indicating that a target is active.
2631 These functions should be split out into seperate variables,
2632 especially since GDB will someday have a notion of debugging
2633 several processes. */
2634 inferior_ptid
= magic_null_ptid
;
2636 /* Now, if we have thread information, update inferior_ptid. */
2637 inferior_ptid
= remote_current_thread (inferior_ptid
);
2639 add_inferior (ptid_get_pid (inferior_ptid
));
2641 /* Always add the main thread. */
2642 add_thread_silent (inferior_ptid
);
2644 get_offsets (); /* Get text, data & bss offsets. */
2646 /* Use the previously fetched status. */
2647 gdb_assert (wait_status
!= NULL
);
2648 strcpy (rs
->buf
, wait_status
);
2649 rs
->cached_wait_status
= 1;
2652 start_remote (args
->from_tty
); /* Initialize gdb process mechanisms. */
2656 /* In non-stop, we will either get an "OK", meaning that there
2657 are no stopped threads at this time; or, a regular stop
2658 reply. In the latter case, there may be more than one thread
2659 stopped --- we pull them all out using the vStopped
2661 if (strcmp (rs
->buf
, "OK") != 0)
2663 struct stop_reply
*stop_reply
;
2664 struct cleanup
*old_chain
;
2666 stop_reply
= stop_reply_xmalloc ();
2667 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
2669 remote_parse_stop_reply (rs
->buf
, stop_reply
);
2670 discard_cleanups (old_chain
);
2672 /* get_pending_stop_replies acks this one, and gets the rest
2674 pending_stop_reply
= stop_reply
;
2675 remote_get_pending_stop_replies ();
2677 /* Make sure that threads that were stopped remain
2679 iterate_over_threads (set_stop_requested_callback
, NULL
);
2682 if (target_can_async_p ())
2683 target_async (inferior_event_handler
, 0);
2685 if (thread_count () == 0)
2687 if (args
->extended_p
)
2689 /* We're connected, but not running. Drop out before we
2690 call start_remote. */
2691 target_mark_exited (args
->target
);
2695 error (_("The target is not running (try extended-remote?)"));
2698 if (args
->extended_p
)
2699 target_mark_running (args
->target
);
2701 /* Let the stub know that we want it to return the thread. */
2703 /* Force the stub to choose a thread. */
2704 set_general_thread (null_ptid
);
2707 inferior_ptid
= remote_current_thread (minus_one_ptid
);
2708 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
2709 error (_("remote didn't report the current thread in non-stop mode"));
2711 get_offsets (); /* Get text, data & bss offsets. */
2713 /* In non-stop mode, any cached wait status will be stored in
2714 the stop reply queue. */
2715 gdb_assert (wait_status
== NULL
);
2718 /* If we connected to a live target, do some additional setup. */
2719 if (target_has_execution
)
2721 if (exec_bfd
) /* No use without an exec file. */
2722 remote_check_symbols (symfile_objfile
);
2725 /* If code is shared between processes, then breakpoints are global
2726 too; Insert them now. */
2727 if (gdbarch_has_global_solist (target_gdbarch
)
2728 && breakpoints_always_inserted_mode ())
2729 insert_breakpoints ();
2732 /* Open a connection to a remote debugger.
2733 NAME is the filename used for communication. */
2736 remote_open (char *name
, int from_tty
)
2738 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
2741 /* Open a connection to a remote debugger using the extended
2742 remote gdb protocol. NAME is the filename used for communication. */
2745 extended_remote_open (char *name
, int from_tty
)
2747 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
2750 /* Generic code for opening a connection to a remote target. */
2753 init_all_packet_configs (void)
2756 for (i
= 0; i
< PACKET_MAX
; i
++)
2757 update_packet_config (&remote_protocol_packets
[i
]);
2760 /* Symbol look-up. */
2763 remote_check_symbols (struct objfile
*objfile
)
2765 struct remote_state
*rs
= get_remote_state ();
2766 char *msg
, *reply
, *tmp
;
2767 struct minimal_symbol
*sym
;
2770 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
2773 /* Make sure the remote is pointing at the right process. */
2774 set_general_process ();
2776 /* Allocate a message buffer. We can't reuse the input buffer in RS,
2777 because we need both at the same time. */
2778 msg
= alloca (get_remote_packet_size ());
2780 /* Invite target to request symbol lookups. */
2782 putpkt ("qSymbol::");
2783 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2784 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
2787 while (strncmp (reply
, "qSymbol:", 8) == 0)
2790 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
2792 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
2794 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
2797 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
2799 /* If this is a function address, return the start of code
2800 instead of any data function descriptor. */
2801 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
2805 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
2806 paddr_nz (sym_addr
), &reply
[8]);
2810 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2815 static struct serial
*
2816 remote_serial_open (char *name
)
2818 static int udp_warning
= 0;
2820 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2821 of in ser-tcp.c, because it is the remote protocol assuming that the
2822 serial connection is reliable and not the serial connection promising
2824 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
2827 The remote protocol may be unreliable over UDP.\n\
2828 Some events may be lost, rendering further debugging impossible."));
2832 return serial_open (name
);
2835 /* This type describes each known response to the qSupported
2837 struct protocol_feature
2839 /* The name of this protocol feature. */
2842 /* The default for this protocol feature. */
2843 enum packet_support default_support
;
2845 /* The function to call when this feature is reported, or after
2846 qSupported processing if the feature is not supported.
2847 The first argument points to this structure. The second
2848 argument indicates whether the packet requested support be
2849 enabled, disabled, or probed (or the default, if this function
2850 is being called at the end of processing and this feature was
2851 not reported). The third argument may be NULL; if not NULL, it
2852 is a NUL-terminated string taken from the packet following
2853 this feature's name and an equals sign. */
2854 void (*func
) (const struct protocol_feature
*, enum packet_support
,
2857 /* The corresponding packet for this feature. Only used if
2858 FUNC is remote_supported_packet. */
2863 remote_supported_packet (const struct protocol_feature
*feature
,
2864 enum packet_support support
,
2865 const char *argument
)
2869 warning (_("Remote qSupported response supplied an unexpected value for"
2870 " \"%s\"."), feature
->name
);
2874 if (remote_protocol_packets
[feature
->packet
].support
2875 == PACKET_SUPPORT_UNKNOWN
)
2876 remote_protocol_packets
[feature
->packet
].support
= support
;
2880 remote_packet_size (const struct protocol_feature
*feature
,
2881 enum packet_support support
, const char *value
)
2883 struct remote_state
*rs
= get_remote_state ();
2888 if (support
!= PACKET_ENABLE
)
2891 if (value
== NULL
|| *value
== '\0')
2893 warning (_("Remote target reported \"%s\" without a size."),
2899 packet_size
= strtol (value
, &value_end
, 16);
2900 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
2902 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
2903 feature
->name
, value
);
2907 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
2909 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
2910 packet_size
, MAX_REMOTE_PACKET_SIZE
);
2911 packet_size
= MAX_REMOTE_PACKET_SIZE
;
2914 /* Record the new maximum packet size. */
2915 rs
->explicit_packet_size
= packet_size
;
2919 remote_multi_process_feature (const struct protocol_feature
*feature
,
2920 enum packet_support support
, const char *value
)
2922 struct remote_state
*rs
= get_remote_state ();
2923 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
2927 remote_non_stop_feature (const struct protocol_feature
*feature
,
2928 enum packet_support support
, const char *value
)
2930 struct remote_state
*rs
= get_remote_state ();
2931 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
2934 static struct protocol_feature remote_protocol_features
[] = {
2935 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
2936 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
2937 PACKET_qXfer_auxv
},
2938 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
2939 PACKET_qXfer_features
},
2940 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
2941 PACKET_qXfer_libraries
},
2942 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
2943 PACKET_qXfer_memory_map
},
2944 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
2945 PACKET_qXfer_spu_read
},
2946 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
2947 PACKET_qXfer_spu_write
},
2948 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
2949 PACKET_QPassSignals
},
2950 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
2951 PACKET_QStartNoAckMode
},
2952 { "multiprocess", PACKET_DISABLE
, remote_multi_process_feature
, -1 },
2953 { "QNonStop", PACKET_DISABLE
, remote_non_stop_feature
, -1 },
2957 remote_query_supported (void)
2959 struct remote_state
*rs
= get_remote_state ();
2962 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
2964 /* The packet support flags are handled differently for this packet
2965 than for most others. We treat an error, a disabled packet, and
2966 an empty response identically: any features which must be reported
2967 to be used will be automatically disabled. An empty buffer
2968 accomplishes this, since that is also the representation for a list
2969 containing no features. */
2972 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
2975 putpkt ("qSupported:multiprocess+");
2977 putpkt ("qSupported");
2979 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2981 /* If an error occured, warn, but do not return - just reset the
2982 buffer to empty and go on to disable features. */
2983 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
2986 warning (_("Remote failure reply: %s"), rs
->buf
);
2991 memset (seen
, 0, sizeof (seen
));
2996 enum packet_support is_supported
;
2997 char *p
, *end
, *name_end
, *value
;
2999 /* First separate out this item from the rest of the packet. If
3000 there's another item after this, we overwrite the separator
3001 (terminated strings are much easier to work with). */
3003 end
= strchr (p
, ';');
3006 end
= p
+ strlen (p
);
3016 warning (_("empty item in \"qSupported\" response"));
3021 name_end
= strchr (p
, '=');
3024 /* This is a name=value entry. */
3025 is_supported
= PACKET_ENABLE
;
3026 value
= name_end
+ 1;
3035 is_supported
= PACKET_ENABLE
;
3039 is_supported
= PACKET_DISABLE
;
3043 is_supported
= PACKET_SUPPORT_UNKNOWN
;
3047 warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p
);
3053 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3054 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
3056 const struct protocol_feature
*feature
;
3059 feature
= &remote_protocol_features
[i
];
3060 feature
->func (feature
, is_supported
, value
);
3065 /* If we increased the packet size, make sure to increase the global
3066 buffer size also. We delay this until after parsing the entire
3067 qSupported packet, because this is the same buffer we were
3069 if (rs
->buf_size
< rs
->explicit_packet_size
)
3071 rs
->buf_size
= rs
->explicit_packet_size
;
3072 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
3075 /* Handle the defaults for unmentioned features. */
3076 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3079 const struct protocol_feature
*feature
;
3081 feature
= &remote_protocol_features
[i
];
3082 feature
->func (feature
, feature
->default_support
, NULL
);
3088 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
, int extended_p
)
3090 struct remote_state
*rs
= get_remote_state ();
3093 error (_("To open a remote debug connection, you need to specify what\n"
3094 "serial device is attached to the remote system\n"
3095 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
3097 /* See FIXME above. */
3098 if (!target_async_permitted
)
3099 wait_forever_enabled_p
= 1;
3101 /* If we're connected to a running target, target_preopen will kill it.
3102 But if we're connected to a target system with no running process,
3103 then we will still be connected when it returns. Ask this question
3104 first, before target_preopen has a chance to kill anything. */
3105 if (remote_desc
!= NULL
&& !target_has_execution
)
3108 || query (_("Already connected to a remote target. Disconnect? ")))
3111 error (_("Still connected."));
3114 target_preopen (from_tty
);
3116 unpush_target (target
);
3118 /* This time without a query. If we were connected to an
3119 extended-remote target and target_preopen killed the running
3120 process, we may still be connected. If we are starting "target
3121 remote" now, the extended-remote target will not have been
3122 removed by unpush_target. */
3123 if (remote_desc
!= NULL
&& !target_has_execution
)
3126 /* Make sure we send the passed signals list the next time we resume. */
3127 xfree (last_pass_packet
);
3128 last_pass_packet
= NULL
;
3130 remote_fileio_reset ();
3131 reopen_exec_file ();
3134 remote_desc
= remote_serial_open (name
);
3136 perror_with_name (name
);
3138 if (baud_rate
!= -1)
3140 if (serial_setbaudrate (remote_desc
, baud_rate
))
3142 /* The requested speed could not be set. Error out to
3143 top level after closing remote_desc. Take care to
3144 set remote_desc to NULL to avoid closing remote_desc
3146 serial_close (remote_desc
);
3148 perror_with_name (name
);
3152 serial_raw (remote_desc
);
3154 /* If there is something sitting in the buffer we might take it as a
3155 response to a command, which would be bad. */
3156 serial_flush_input (remote_desc
);
3160 puts_filtered ("Remote debugging using ");
3161 puts_filtered (name
);
3162 puts_filtered ("\n");
3164 push_target (target
); /* Switch to using remote target now. */
3166 /* Assume that the target is running, unless we learn otherwise. */
3167 target_mark_running (target
);
3169 /* Register extra event sources in the event loop. */
3170 remote_async_inferior_event_token
3171 = create_async_event_handler (remote_async_inferior_event_handler
,
3173 remote_async_get_pending_events_token
3174 = create_async_event_handler (remote_async_get_pending_events_handler
,
3177 /* Reset the target state; these things will be queried either by
3178 remote_query_supported or as they are needed. */
3179 init_all_packet_configs ();
3180 rs
->cached_wait_status
= 0;
3181 rs
->explicit_packet_size
= 0;
3183 rs
->multi_process_aware
= 0;
3184 rs
->extended
= extended_p
;
3185 rs
->non_stop_aware
= 0;
3186 rs
->waiting_for_stop_reply
= 0;
3188 general_thread
= not_sent_ptid
;
3189 continue_thread
= not_sent_ptid
;
3191 /* Probe for ability to use "ThreadInfo" query, as required. */
3192 use_threadinfo_query
= 1;
3193 use_threadextra_query
= 1;
3195 if (target_async_permitted
)
3197 /* With this target we start out by owning the terminal. */
3198 remote_async_terminal_ours_p
= 1;
3200 /* FIXME: cagney/1999-09-23: During the initial connection it is
3201 assumed that the target is already ready and able to respond to
3202 requests. Unfortunately remote_start_remote() eventually calls
3203 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
3204 around this. Eventually a mechanism that allows
3205 wait_for_inferior() to expect/get timeouts will be
3207 wait_forever_enabled_p
= 0;
3210 /* First delete any symbols previously loaded from shared libraries. */
3211 no_shared_libraries (NULL
, 0);
3214 init_thread_list ();
3216 /* Start the remote connection. If error() or QUIT, discard this
3217 target (we'd otherwise be in an inconsistent state) and then
3218 propogate the error on up the exception chain. This ensures that
3219 the caller doesn't stumble along blindly assuming that the
3220 function succeeded. The CLI doesn't have this problem but other
3221 UI's, such as MI do.
3223 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
3224 this function should return an error indication letting the
3225 caller restore the previous state. Unfortunately the command
3226 ``target remote'' is directly wired to this function making that
3227 impossible. On a positive note, the CLI side of this problem has
3228 been fixed - the function set_cmd_context() makes it possible for
3229 all the ``target ....'' commands to share a common callback
3230 function. See cli-dump.c. */
3232 struct gdb_exception ex
;
3233 struct start_remote_args args
;
3235 args
.from_tty
= from_tty
;
3236 args
.target
= target
;
3237 args
.extended_p
= extended_p
;
3239 ex
= catch_exception (uiout
, remote_start_remote
, &args
, RETURN_MASK_ALL
);
3242 /* Pop the partially set up target - unless something else did
3243 already before throwing the exception. */
3244 if (remote_desc
!= NULL
)
3246 if (target_async_permitted
)
3247 wait_forever_enabled_p
= 1;
3248 throw_exception (ex
);
3252 if (target_async_permitted
)
3253 wait_forever_enabled_p
= 1;
3256 /* This takes a program previously attached to and detaches it. After
3257 this is done, GDB can be used to debug some other program. We
3258 better not have left any breakpoints in the target program or it'll
3259 die when it hits one. */
3262 remote_detach_1 (char *args
, int from_tty
, int extended
)
3264 int pid
= ptid_get_pid (inferior_ptid
);
3265 struct remote_state
*rs
= get_remote_state ();
3268 error (_("Argument given to \"detach\" when remotely debugging."));
3270 if (!target_has_execution
)
3271 error (_("No process to detach from."));
3273 /* Tell the remote target to detach. */
3274 if (remote_multi_process_p (rs
))
3275 sprintf (rs
->buf
, "D;%x", pid
);
3277 strcpy (rs
->buf
, "D");
3280 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3282 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
3284 else if (rs
->buf
[0] == '\0')
3285 error (_("Remote doesn't know how to detach"));
3287 error (_("Can't detach process."));
3291 if (remote_multi_process_p (rs
))
3292 printf_filtered (_("Detached from remote %s.\n"),
3293 target_pid_to_str (pid_to_ptid (pid
)));
3297 puts_filtered (_("Detached from remote process.\n"));
3299 puts_filtered (_("Ending remote debugging.\n"));
3303 discard_pending_stop_replies (pid
);
3304 detach_inferior (pid
);
3305 target_mourn_inferior ();
3309 remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
3311 remote_detach_1 (args
, from_tty
, 0);
3315 extended_remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
3317 remote_detach_1 (args
, from_tty
, 1);
3320 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
3323 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
3326 error (_("Argument given to \"disconnect\" when remotely debugging."));
3328 /* Make sure we unpush even the extended remote targets; mourn
3329 won't do it. So call remote_mourn_1 directly instead of
3330 target_mourn_inferior. */
3331 remote_mourn_1 (target
);
3334 puts_filtered ("Ending remote debugging.\n");
3337 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
3338 be chatty about it. */
3341 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
3343 struct remote_state
*rs
= get_remote_state ();
3346 char *wait_status
= NULL
;
3347 struct inferior
*inf
;
3350 error_no_arg (_("process-id to attach"));
3353 pid
= strtol (args
, &dummy
, 0);
3354 /* Some targets don't set errno on errors, grrr! */
3355 if (pid
== 0 && args
== dummy
)
3356 error (_("Illegal process-id: %s."), args
);
3358 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
3359 error (_("This target does not support attaching to a process"));
3361 sprintf (rs
->buf
, "vAttach;%x", pid
);
3363 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3365 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
3368 printf_unfiltered (_("Attached to %s\n"),
3369 target_pid_to_str (pid_to_ptid (pid
)));
3373 /* Save the reply for later. */
3374 wait_status
= alloca (strlen (rs
->buf
) + 1);
3375 strcpy (wait_status
, rs
->buf
);
3377 else if (strcmp (rs
->buf
, "OK") != 0)
3378 error (_("Attaching to %s failed with: %s"),
3379 target_pid_to_str (pid_to_ptid (pid
)),
3382 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
3383 error (_("This target does not support attaching to a process"));
3385 error (_("Attaching to %s failed"),
3386 target_pid_to_str (pid_to_ptid (pid
)));
3388 target_mark_running (target
);
3389 inferior_ptid
= pid_to_ptid (pid
);
3391 /* Now, if we have thread information, update inferior_ptid. */
3392 inferior_ptid
= remote_current_thread (inferior_ptid
);
3394 inf
= add_inferior (pid
);
3395 inf
->attach_flag
= 1;
3398 /* Get list of threads. */
3399 remote_threads_info ();
3401 /* Add the main thread to the thread list. */
3402 add_thread_silent (inferior_ptid
);
3404 /* Next, if the target can specify a description, read it. We do
3405 this before anything involving memory or registers. */
3406 target_find_description ();
3410 /* Use the previously fetched status. */
3411 gdb_assert (wait_status
!= NULL
);
3413 if (target_can_async_p ())
3415 struct stop_reply
*stop_reply
;
3416 struct cleanup
*old_chain
;
3418 stop_reply
= stop_reply_xmalloc ();
3419 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
3420 remote_parse_stop_reply (wait_status
, stop_reply
);
3421 discard_cleanups (old_chain
);
3422 push_stop_reply (stop_reply
);
3424 target_async (inferior_event_handler
, 0);
3428 gdb_assert (wait_status
!= NULL
);
3429 strcpy (rs
->buf
, wait_status
);
3430 rs
->cached_wait_status
= 1;
3434 gdb_assert (wait_status
== NULL
);
3438 extended_remote_attach (struct target_ops
*ops
, char *args
, int from_tty
)
3440 extended_remote_attach_1 (ops
, args
, from_tty
);
3443 /* Convert hex digit A to a number. */
3448 if (a
>= '0' && a
<= '9')
3450 else if (a
>= 'a' && a
<= 'f')
3451 return a
- 'a' + 10;
3452 else if (a
>= 'A' && a
<= 'F')
3453 return a
- 'A' + 10;
3455 error (_("Reply contains invalid hex digit %d"), a
);
3459 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
3463 for (i
= 0; i
< count
; i
++)
3465 if (hex
[0] == 0 || hex
[1] == 0)
3467 /* Hex string is short, or of uneven length.
3468 Return the count that has been converted so far. */
3471 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
3477 /* Convert number NIB to a hex digit. */
3485 return 'a' + nib
- 10;
3489 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
3492 /* May use a length, or a nul-terminated string as input. */
3494 count
= strlen ((char *) bin
);
3496 for (i
= 0; i
< count
; i
++)
3498 *hex
++ = tohex ((*bin
>> 4) & 0xf);
3499 *hex
++ = tohex (*bin
++ & 0xf);
3505 /* Check for the availability of vCont. This function should also check
3509 remote_vcont_probe (struct remote_state
*rs
)
3513 strcpy (rs
->buf
, "vCont?");
3515 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3518 /* Make sure that the features we assume are supported. */
3519 if (strncmp (buf
, "vCont", 5) == 0)
3522 int support_s
, support_S
, support_c
, support_C
;
3528 rs
->support_vCont_t
= 0;
3529 while (p
&& *p
== ';')
3532 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3534 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3536 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3538 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3540 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3541 rs
->support_vCont_t
= 1;
3543 p
= strchr (p
, ';');
3546 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
3547 BUF will make packet_ok disable the packet. */
3548 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
3552 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
3555 /* Resume the remote inferior by using a "vCont" packet. The thread
3556 to be resumed is PTID; STEP and SIGGNAL indicate whether the
3557 resumed thread should be single-stepped and/or signalled. If PTID
3558 equals minus_one_ptid, then all threads are resumed; the thread to
3559 be stepped and/or signalled is given in the global INFERIOR_PTID.
3560 This function returns non-zero iff it resumes the inferior.
3562 This function issues a strict subset of all possible vCont commands at the
3566 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
3568 struct remote_state
*rs
= get_remote_state ();
3572 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
3573 remote_vcont_probe (rs
);
3575 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
3579 endp
= rs
->buf
+ get_remote_packet_size ();
3581 /* If we could generate a wider range of packets, we'd have to worry
3582 about overflowing BUF. Should there be a generic
3583 "multi-part-packet" packet? */
3585 if (ptid_equal (ptid
, magic_null_ptid
))
3587 /* MAGIC_NULL_PTID means that we don't have any active threads,
3588 so we don't have any TID numbers the inferior will
3589 understand. Make sure to only send forms that do not specify
3591 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3592 xsnprintf (p
, endp
- p
, "vCont;S%02x", siggnal
);
3594 xsnprintf (p
, endp
- p
, "vCont;s");
3595 else if (siggnal
!= TARGET_SIGNAL_0
)
3596 xsnprintf (p
, endp
- p
, "vCont;C%02x", siggnal
);
3598 xsnprintf (p
, endp
- p
, "vCont;c");
3600 else if (ptid_equal (ptid
, minus_one_ptid
))
3602 /* Resume all threads, with preference for INFERIOR_PTID. */
3603 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3605 /* Step inferior_ptid with signal. */
3606 p
+= xsnprintf (p
, endp
- p
, "vCont;S%02x:", siggnal
);
3607 p
= write_ptid (p
, endp
, inferior_ptid
);
3608 /* And continue others. */
3609 p
+= xsnprintf (p
, endp
- p
, ";c");
3613 /* Step inferior_ptid. */
3614 p
+= xsnprintf (p
, endp
- p
, "vCont;s:");
3615 p
= write_ptid (p
, endp
, inferior_ptid
);
3616 /* And continue others. */
3617 p
+= xsnprintf (p
, endp
- p
, ";c");
3619 else if (siggnal
!= TARGET_SIGNAL_0
)
3621 /* Continue inferior_ptid with signal. */
3622 p
+= xsnprintf (p
, endp
- p
, "vCont;C%02x:", siggnal
);
3623 p
= write_ptid (p
, endp
, inferior_ptid
);
3624 /* And continue others. */
3625 p
+= xsnprintf (p
, endp
- p
, ";c");
3628 xsnprintf (p
, endp
- p
, "vCont;c");
3632 /* Scheduler locking; resume only PTID. */
3633 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3635 /* Step ptid with signal. */
3636 p
+= xsnprintf (p
, endp
- p
, "vCont;S%02x:", siggnal
);
3637 p
= write_ptid (p
, endp
, ptid
);
3642 p
+= xsnprintf (p
, endp
- p
, "vCont;s:");
3643 p
= write_ptid (p
, endp
, ptid
);
3645 else if (siggnal
!= TARGET_SIGNAL_0
)
3647 /* Continue ptid with signal. */
3648 p
+= xsnprintf (p
, endp
- p
, "vCont;C%02x:", siggnal
);
3649 p
= write_ptid (p
, endp
, ptid
);
3653 /* Continue ptid. */
3654 p
+= xsnprintf (p
, endp
- p
, "vCont;c:");
3655 p
= write_ptid (p
, endp
, ptid
);
3659 gdb_assert (strlen (rs
->buf
) < get_remote_packet_size ());
3664 /* In non-stop, the stub replies to vCont with "OK". The stop
3665 reply will be reported asynchronously by means of a `%Stop'
3667 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3668 if (strcmp (rs
->buf
, "OK") != 0)
3669 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
);
3675 /* Tell the remote machine to resume. */
3677 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
3679 static int last_sent_step
;
3682 remote_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
3684 struct remote_state
*rs
= get_remote_state ();
3687 last_sent_signal
= siggnal
;
3688 last_sent_step
= step
;
3690 /* Update the inferior on signals to silently pass, if they've changed. */
3691 remote_pass_signals ();
3693 /* The vCont packet doesn't need to specify threads via Hc. */
3694 if (remote_vcont_resume (ptid
, step
, siggnal
))
3697 /* All other supported resume packets do use Hc, so set the continue
3699 if (ptid_equal (ptid
, minus_one_ptid
))
3700 set_continue_thread (any_thread_ptid
);
3702 set_continue_thread (ptid
);
3705 if (execution_direction
== EXEC_REVERSE
)
3707 /* We don't pass signals to the target in reverse exec mode. */
3708 if (info_verbose
&& siggnal
!= TARGET_SIGNAL_0
)
3709 warning (" - Can't pass signal %d to target in reverse: ignored.\n",
3711 strcpy (buf
, step
? "bs" : "bc");
3713 else if (siggnal
!= TARGET_SIGNAL_0
)
3715 buf
[0] = step
? 'S' : 'C';
3716 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
3717 buf
[2] = tohex (((int) siggnal
) & 0xf);
3721 strcpy (buf
, step
? "s" : "c");
3726 /* We are about to start executing the inferior, let's register it
3727 with the event loop. NOTE: this is the one place where all the
3728 execution commands end up. We could alternatively do this in each
3729 of the execution commands in infcmd.c. */
3730 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
3731 into infcmd.c in order to allow inferior function calls to work
3732 NOT asynchronously. */
3733 if (target_can_async_p ())
3734 target_async (inferior_event_handler
, 0);
3736 /* We've just told the target to resume. The remote server will
3737 wait for the inferior to stop, and then send a stop reply. In
3738 the mean time, we can't start another command/query ourselves
3739 because the stub wouldn't be ready to process it. This applies
3740 only to the base all-stop protocol, however. In non-stop (which
3741 only supports vCont), the stub replies with an "OK", and is
3742 immediate able to process further serial input. */
3744 rs
->waiting_for_stop_reply
= 1;
3748 /* Set up the signal handler for SIGINT, while the target is
3749 executing, ovewriting the 'regular' SIGINT signal handler. */
3751 initialize_sigint_signal_handler (void)
3753 signal (SIGINT
, handle_remote_sigint
);
3756 /* Signal handler for SIGINT, while the target is executing. */
3758 handle_remote_sigint (int sig
)
3760 signal (sig
, handle_remote_sigint_twice
);
3761 mark_async_signal_handler_wrapper (sigint_remote_token
);
3764 /* Signal handler for SIGINT, installed after SIGINT has already been
3765 sent once. It will take effect the second time that the user sends
3768 handle_remote_sigint_twice (int sig
)
3770 signal (sig
, handle_remote_sigint
);
3771 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
3774 /* Perform the real interruption of the target execution, in response
3777 async_remote_interrupt (gdb_client_data arg
)
3780 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
3782 target_stop (inferior_ptid
);
3785 /* Perform interrupt, if the first attempt did not succeed. Just give
3786 up on the target alltogether. */
3788 async_remote_interrupt_twice (gdb_client_data arg
)
3791 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
3796 /* Reinstall the usual SIGINT handlers, after the target has
3799 cleanup_sigint_signal_handler (void *dummy
)
3801 signal (SIGINT
, handle_sigint
);
3804 /* Send ^C to target to halt it. Target will respond, and send us a
3806 static void (*ofunc
) (int);
3808 /* The command line interface's stop routine. This function is installed
3809 as a signal handler for SIGINT. The first time a user requests a
3810 stop, we call remote_stop to send a break or ^C. If there is no
3811 response from the target (it didn't stop when the user requested it),
3812 we ask the user if he'd like to detach from the target. */
3814 remote_interrupt (int signo
)
3816 /* If this doesn't work, try more severe steps. */
3817 signal (signo
, remote_interrupt_twice
);
3819 gdb_call_async_signal_handler (sigint_remote_token
, 1);
3822 /* The user typed ^C twice. */
3825 remote_interrupt_twice (int signo
)
3827 signal (signo
, ofunc
);
3828 gdb_call_async_signal_handler (sigint_remote_twice_token
, 1);
3829 signal (signo
, remote_interrupt
);
3832 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
3833 thread, all threads of a remote process, or all threads of all
3837 remote_stop_ns (ptid_t ptid
)
3839 struct remote_state
*rs
= get_remote_state ();
3841 char *endp
= rs
->buf
+ get_remote_packet_size ();
3842 struct stop_reply
*reply
, *next
;
3844 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
3845 remote_vcont_probe (rs
);
3847 if (!rs
->support_vCont_t
)
3848 error (_("Remote server does not support stopping threads"));
3850 if (ptid_equal (ptid
, minus_one_ptid
))
3851 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
3856 /* Step inferior_ptid. */
3857 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
3859 if (ptid_is_pid (ptid
))
3860 /* All (-1) threads of process. */
3861 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
3864 /* Small optimization: if we already have a stop reply for
3865 this thread, no use in telling the stub we want this
3867 if (peek_stop_reply (ptid
))
3873 p
= write_ptid (p
, endp
, nptid
);
3876 /* In non-stop, we get an immediate OK reply. The stop reply will
3877 come in asynchronously by notification. */
3879 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3880 if (strcmp (rs
->buf
, "OK") != 0)
3881 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
), rs
->buf
);
3884 /* All-stop version of target_stop. Sends a break or a ^C to stop the
3885 remote target. It is undefined which thread of which process
3886 reports the stop. */
3889 remote_stop_as (ptid_t ptid
)
3891 struct remote_state
*rs
= get_remote_state ();
3893 /* If the inferior is stopped already, but the core didn't know
3894 about it yet, just ignore the request. The cached wait status
3895 will be collected in remote_wait. */
3896 if (rs
->cached_wait_status
)
3899 /* Send a break or a ^C, depending on user preference. */
3902 serial_send_break (remote_desc
);
3904 serial_write (remote_desc
, "\003", 1);
3907 /* This is the generic stop called via the target vector. When a target
3908 interrupt is requested, either by the command line or the GUI, we
3909 will eventually end up here. */
3912 remote_stop (ptid_t ptid
)
3915 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
3918 remote_stop_ns (ptid
);
3920 remote_stop_as (ptid
);
3923 /* Ask the user what to do when an interrupt is received. */
3926 interrupt_query (void)
3928 target_terminal_ours ();
3930 if (target_can_async_p ())
3932 signal (SIGINT
, handle_sigint
);
3933 deprecated_throw_reason (RETURN_QUIT
);
3937 if (query ("Interrupted while waiting for the program.\n\
3938 Give up (and stop debugging it)? "))
3941 deprecated_throw_reason (RETURN_QUIT
);
3945 target_terminal_inferior ();
3948 /* Enable/disable target terminal ownership. Most targets can use
3949 terminal groups to control terminal ownership. Remote targets are
3950 different in that explicit transfer of ownership to/from GDB/target
3954 remote_terminal_inferior (void)
3956 if (!target_async_permitted
)
3957 /* Nothing to do. */
3960 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
3961 sync_execution here. This function should only be called when
3962 GDB is resuming the inferior in the forground. A background
3963 resume (``run&'') should leave GDB in control of the terminal and
3964 consequently should not call this code. */
3965 if (!sync_execution
)
3967 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
3968 calls target_terminal_*() idenpotent. The event-loop GDB talking
3969 to an asynchronous target with a synchronous command calls this
3970 function from both event-top.c and infrun.c/infcmd.c. Once GDB
3971 stops trying to transfer the terminal to the target when it
3972 shouldn't this guard can go away. */
3973 if (!remote_async_terminal_ours_p
)
3975 delete_file_handler (input_fd
);
3976 remote_async_terminal_ours_p
= 0;
3977 initialize_sigint_signal_handler ();
3978 /* NOTE: At this point we could also register our selves as the
3979 recipient of all input. Any characters typed could then be
3980 passed on down to the target. */
3984 remote_terminal_ours (void)
3986 if (!target_async_permitted
)
3987 /* Nothing to do. */
3990 /* See FIXME in remote_terminal_inferior. */
3991 if (!sync_execution
)
3993 /* See FIXME in remote_terminal_inferior. */
3994 if (remote_async_terminal_ours_p
)
3996 cleanup_sigint_signal_handler (NULL
);
3997 add_file_handler (input_fd
, stdin_event_handler
, 0);
3998 remote_async_terminal_ours_p
= 1;
4002 remote_console_output (char *msg
)
4006 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
4009 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
4012 fputs_unfiltered (tb
, gdb_stdtarg
);
4014 gdb_flush (gdb_stdtarg
);
4017 typedef struct cached_reg
4020 gdb_byte data
[MAX_REGISTER_SIZE
];
4023 DEF_VEC_O(cached_reg_t
);
4027 struct stop_reply
*next
;
4031 struct target_waitstatus ws
;
4033 VEC(cached_reg_t
) *regcache
;
4035 int stopped_by_watchpoint_p
;
4036 CORE_ADDR watch_data_address
;
4042 /* The list of already fetched and acknowledged stop events. */
4043 static struct stop_reply
*stop_reply_queue
;
4045 static struct stop_reply
*
4046 stop_reply_xmalloc (void)
4048 struct stop_reply
*r
= XMALLOC (struct stop_reply
);
4054 stop_reply_xfree (struct stop_reply
*r
)
4058 VEC_free (cached_reg_t
, r
->regcache
);
4063 /* Discard all pending stop replies of inferior PID. If PID is -1,
4064 discard everything. */
4067 discard_pending_stop_replies (int pid
)
4069 struct stop_reply
*prev
= NULL
, *reply
, *next
;
4071 /* Discard the in-flight notification. */
4072 if (pending_stop_reply
!= NULL
4074 || ptid_get_pid (pending_stop_reply
->ptid
) == pid
))
4076 stop_reply_xfree (pending_stop_reply
);
4077 pending_stop_reply
= NULL
;
4080 /* Discard the stop replies we have already pulled with
4082 for (reply
= stop_reply_queue
; reply
; reply
= next
)
4086 || ptid_get_pid (reply
->ptid
) == pid
)
4088 if (reply
== stop_reply_queue
)
4089 stop_reply_queue
= reply
->next
;
4091 prev
->next
= reply
->next
;
4093 stop_reply_xfree (reply
);
4100 /* Cleanup wrapper. */
4103 do_stop_reply_xfree (void *arg
)
4105 struct stop_reply
*r
= arg
;
4106 stop_reply_xfree (r
);
4109 /* Look for a queued stop reply belonging to PTID. If one is found,
4110 remove it from the queue, and return it. Returns NULL if none is
4111 found. If there are still queued events left to process, tell the
4112 event loop to get back to target_wait soon. */
4114 static struct stop_reply
*
4115 queued_stop_reply (ptid_t ptid
)
4117 struct stop_reply
*it
, *prev
;
4118 struct stop_reply head
;
4120 head
.next
= stop_reply_queue
;
4125 if (!ptid_equal (ptid
, minus_one_ptid
))
4126 for (; it
; prev
= it
, it
= it
->next
)
4127 if (ptid_equal (ptid
, it
->ptid
))
4132 prev
->next
= it
->next
;
4136 stop_reply_queue
= head
.next
;
4138 if (stop_reply_queue
)
4139 /* There's still at least an event left. */
4140 mark_async_event_handler (remote_async_inferior_event_token
);
4145 /* Push a fully parsed stop reply in the stop reply queue. Since we
4146 know that we now have at least one queued event left to pass to the
4147 core side, tell the event loop to get back to target_wait soon. */
4150 push_stop_reply (struct stop_reply
*new_event
)
4152 struct stop_reply
*event
;
4154 if (stop_reply_queue
)
4156 for (event
= stop_reply_queue
;
4157 event
&& event
->next
;
4158 event
= event
->next
)
4161 event
->next
= new_event
;
4164 stop_reply_queue
= new_event
;
4166 mark_async_event_handler (remote_async_inferior_event_token
);
4169 /* Returns true if we have a stop reply for PTID. */
4172 peek_stop_reply (ptid_t ptid
)
4174 struct stop_reply
*it
;
4176 for (it
= stop_reply_queue
; it
; it
= it
->next
)
4177 if (ptid_equal (ptid
, it
->ptid
))
4179 if (it
->ws
.kind
== TARGET_WAITKIND_STOPPED
)
4186 /* Parse the stop reply in BUF. Either the function succeeds, and the
4187 result is stored in EVENT, or throws an error. */
4190 remote_parse_stop_reply (char *buf
, struct stop_reply
*event
)
4192 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4196 event
->ptid
= null_ptid
;
4197 event
->ws
.kind
= TARGET_WAITKIND_IGNORE
;
4198 event
->ws
.value
.integer
= 0;
4199 event
->solibs_changed
= 0;
4200 event
->replay_event
= 0;
4201 event
->stopped_by_watchpoint_p
= 0;
4202 event
->regcache
= NULL
;
4206 case 'T': /* Status with PC, SP, FP, ... */
4208 gdb_byte regs
[MAX_REGISTER_SIZE
];
4210 /* Expedited reply, containing Signal, {regno, reg} repeat. */
4211 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
4213 n... = register number
4214 r... = register contents
4217 p
= &buf
[3]; /* after Txx */
4225 /* If the packet contains a register number, save it in
4226 pnum and set p1 to point to the character following it.
4227 Otherwise p1 points to p. */
4229 /* If this packet is an awatch packet, don't parse the 'a'
4230 as a register number. */
4232 if (strncmp (p
, "awatch", strlen("awatch")) != 0)
4234 /* Read the ``P'' register number. */
4235 pnum
= strtol (p
, &p_temp
, 16);
4241 if (p1
== p
) /* No register number present here. */
4243 p1
= strchr (p
, ':');
4245 error (_("Malformed packet(a) (missing colon): %s\n\
4248 if (strncmp (p
, "thread", p1
- p
) == 0)
4249 event
->ptid
= read_ptid (++p1
, &p
);
4250 else if ((strncmp (p
, "watch", p1
- p
) == 0)
4251 || (strncmp (p
, "rwatch", p1
- p
) == 0)
4252 || (strncmp (p
, "awatch", p1
- p
) == 0))
4254 event
->stopped_by_watchpoint_p
= 1;
4255 p
= unpack_varlen_hex (++p1
, &addr
);
4256 event
->watch_data_address
= (CORE_ADDR
) addr
;
4258 else if (strncmp (p
, "library", p1
- p
) == 0)
4262 while (*p_temp
&& *p_temp
!= ';')
4265 event
->solibs_changed
= 1;
4268 else if (strncmp (p
, "replaylog", p1
- p
) == 0)
4270 /* NO_HISTORY event.
4271 p1 will indicate "begin" or "end", but
4272 it makes no difference for now, so ignore it. */
4273 event
->replay_event
= 1;
4274 p_temp
= strchr (p1
+ 1, ';');
4280 /* Silently skip unknown optional info. */
4281 p_temp
= strchr (p1
+ 1, ';');
4288 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
4289 cached_reg_t cached_reg
;
4291 cached_reg
.num
= reg
->regnum
;
4296 error (_("Malformed packet(b) (missing colon): %s\n\
4302 error (_("Remote sent bad register number %s: %s\n\
4304 phex_nz (pnum
, 0), p
, buf
);
4306 fieldsize
= hex2bin (p
, cached_reg
.data
,
4307 register_size (target_gdbarch
,
4310 if (fieldsize
< register_size (target_gdbarch
,
4312 warning (_("Remote reply is too short: %s"), buf
);
4314 VEC_safe_push (cached_reg_t
, event
->regcache
, &cached_reg
);
4318 error (_("Remote register badly formatted: %s\nhere: %s"),
4324 case 'S': /* Old style status, just signal only. */
4325 if (event
->solibs_changed
)
4326 event
->ws
.kind
= TARGET_WAITKIND_LOADED
;
4327 else if (event
->replay_event
)
4328 event
->ws
.kind
= TARGET_WAITKIND_NO_HISTORY
;
4331 event
->ws
.kind
= TARGET_WAITKIND_STOPPED
;
4332 event
->ws
.value
.sig
= (enum target_signal
)
4333 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
4336 case 'W': /* Target exited. */
4343 /* GDB used to accept only 2 hex chars here. Stubs should
4344 only send more if they detect GDB supports multi-process
4346 p
= unpack_varlen_hex (&buf
[1], &value
);
4350 /* The remote process exited. */
4351 event
->ws
.kind
= TARGET_WAITKIND_EXITED
;
4352 event
->ws
.value
.integer
= value
;
4356 /* The remote process exited with a signal. */
4357 event
->ws
.kind
= TARGET_WAITKIND_SIGNALLED
;
4358 event
->ws
.value
.sig
= (enum target_signal
) value
;
4361 /* If no process is specified, assume inferior_ptid. */
4362 pid
= ptid_get_pid (inferior_ptid
);
4371 else if (strncmp (p
,
4372 "process:", sizeof ("process:") - 1) == 0)
4375 p
+= sizeof ("process:") - 1;
4376 unpack_varlen_hex (p
, &upid
);
4380 error (_("unknown stop reply packet: %s"), buf
);
4383 error (_("unknown stop reply packet: %s"), buf
);
4384 event
->ptid
= pid_to_ptid (pid
);
4389 if (non_stop
&& ptid_equal (event
->ptid
, null_ptid
))
4390 error (_("No process or thread specified in stop reply: %s"), buf
);
4393 /* When the stub wants to tell GDB about a new stop reply, it sends a
4394 stop notification (%Stop). Those can come it at any time, hence,
4395 we have to make sure that any pending putpkt/getpkt sequence we're
4396 making is finished, before querying the stub for more events with
4397 vStopped. E.g., if we started a vStopped sequence immediatelly
4398 upon receiving the %Stop notification, something like this could
4406 1.6) <-- (registers reply to step #1.3)
4408 Obviously, the reply in step #1.6 would be unexpected to a vStopped
4411 To solve this, whenever we parse a %Stop notification sucessfully,
4412 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
4413 doing whatever we were doing:
4419 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
4420 2.5) <-- (registers reply to step #2.3)
4422 Eventualy after step #2.5, we return to the event loop, which
4423 notices there's an event on the
4424 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
4425 associated callback --- the function below. At this point, we're
4426 always safe to start a vStopped sequence. :
4429 2.7) <-- T05 thread:2
4435 remote_get_pending_stop_replies (void)
4437 struct remote_state
*rs
= get_remote_state ();
4440 if (pending_stop_reply
)
4443 putpkt ("vStopped");
4445 /* Now we can rely on it. */
4446 push_stop_reply (pending_stop_reply
);
4447 pending_stop_reply
= NULL
;
4451 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4452 if (strcmp (rs
->buf
, "OK") == 0)
4456 struct cleanup
*old_chain
;
4457 struct stop_reply
*stop_reply
= stop_reply_xmalloc ();
4459 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4460 remote_parse_stop_reply (rs
->buf
, stop_reply
);
4463 putpkt ("vStopped");
4465 if (stop_reply
->ws
.kind
!= TARGET_WAITKIND_IGNORE
)
4467 /* Now we can rely on it. */
4468 discard_cleanups (old_chain
);
4469 push_stop_reply (stop_reply
);
4472 /* We got an unknown stop reply. */
4473 do_cleanups (old_chain
);
4480 /* Called when it is decided that STOP_REPLY holds the info of the
4481 event that is to be returned to the core. This function always
4482 destroys STOP_REPLY. */
4485 process_stop_reply (struct stop_reply
*stop_reply
,
4486 struct target_waitstatus
*status
)
4490 *status
= stop_reply
->ws
;
4491 ptid
= stop_reply
->ptid
;
4493 /* If no thread/process was reported by the stub, assume the current
4495 if (ptid_equal (ptid
, null_ptid
))
4496 ptid
= inferior_ptid
;
4498 if (status
->kind
== TARGET_WAITKIND_EXITED
4499 || status
->kind
== TARGET_WAITKIND_SIGNALLED
)
4501 int pid
= ptid_get_pid (ptid
);
4502 delete_inferior (pid
);
4505 notice_new_inferiors (ptid
);
4507 /* Expedited registers. */
4508 if (stop_reply
->regcache
)
4514 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
4516 regcache_raw_supply (get_thread_regcache (ptid
),
4517 reg
->num
, reg
->data
);
4518 VEC_free (cached_reg_t
, stop_reply
->regcache
);
4521 remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
4522 remote_watch_data_address
= stop_reply
->watch_data_address
;
4524 stop_reply_xfree (stop_reply
);
4528 /* The non-stop mode version of target_wait. */
4531 remote_wait_ns (ptid_t ptid
, struct target_waitstatus
*status
)
4533 struct remote_state
*rs
= get_remote_state ();
4534 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4535 ptid_t event_ptid
= null_ptid
;
4536 struct stop_reply
*stop_reply
;
4539 /* If in non-stop mode, get out of getpkt even if a
4540 notification is received. */
4542 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
4549 case 'E': /* Error of some sort. */
4550 /* We're out of sync with the target now. Did it continue
4551 or not? We can't tell which thread it was in non-stop,
4552 so just ignore this. */
4553 warning (_("Remote failure reply: %s"), rs
->buf
);
4555 case 'O': /* Console output. */
4556 remote_console_output (rs
->buf
+ 1);
4559 warning (_("Invalid remote reply: %s"), rs
->buf
);
4563 /* Acknowledge a pending stop reply that may have arrived in the
4565 if (pending_stop_reply
!= NULL
)
4566 remote_get_pending_stop_replies ();
4568 /* If indeed we noticed a stop reply, we're done. */
4569 stop_reply
= queued_stop_reply (ptid
);
4570 if (stop_reply
!= NULL
)
4571 return process_stop_reply (stop_reply
, status
);
4573 /* Still no event. If we're in asynchronous mode, then just
4574 return to the event loop. */
4575 if (remote_is_async_p ())
4577 status
->kind
= TARGET_WAITKIND_IGNORE
;
4578 return minus_one_ptid
;
4581 /* Otherwise, asynchronous mode is masked, so do a blocking
4583 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
4588 /* Wait until the remote machine stops, then return, storing status in
4589 STATUS just as `wait' would. */
4592 remote_wait_as (ptid_t ptid
, struct target_waitstatus
*status
)
4594 struct remote_state
*rs
= get_remote_state ();
4595 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4596 ptid_t event_ptid
= null_ptid
;
4598 int solibs_changed
= 0;
4600 struct stop_reply
*stop_reply
;
4602 status
->kind
= TARGET_WAITKIND_IGNORE
;
4603 status
->value
.integer
= 0;
4605 stop_reply
= queued_stop_reply (ptid
);
4606 if (stop_reply
!= NULL
)
4607 return process_stop_reply (stop_reply
, status
);
4609 if (rs
->cached_wait_status
)
4610 /* Use the cached wait status, but only once. */
4611 rs
->cached_wait_status
= 0;
4616 if (!target_is_async_p ())
4618 ofunc
= signal (SIGINT
, remote_interrupt
);
4619 /* If the user hit C-c before this packet, or between packets,
4620 pretend that it was hit right here. */
4624 remote_interrupt (SIGINT
);
4628 /* FIXME: cagney/1999-09-27: If we're in async mode we should
4629 _never_ wait for ever -> test on target_is_async_p().
4630 However, before we do that we need to ensure that the caller
4631 knows how to take the target into/out of async mode. */
4632 ret
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
4633 if (!target_is_async_p ())
4634 signal (SIGINT
, ofunc
);
4639 remote_stopped_by_watchpoint_p
= 0;
4641 /* We got something. */
4642 rs
->waiting_for_stop_reply
= 0;
4646 case 'E': /* Error of some sort. */
4647 /* We're out of sync with the target now. Did it continue or
4648 not? Not is more likely, so report a stop. */
4649 warning (_("Remote failure reply: %s"), buf
);
4650 status
->kind
= TARGET_WAITKIND_STOPPED
;
4651 status
->value
.sig
= TARGET_SIGNAL_0
;
4653 case 'F': /* File-I/O request. */
4654 remote_fileio_request (buf
);
4656 case 'T': case 'S': case 'X': case 'W':
4658 struct stop_reply
*stop_reply
;
4659 struct cleanup
*old_chain
;
4661 stop_reply
= stop_reply_xmalloc ();
4662 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4663 remote_parse_stop_reply (buf
, stop_reply
);
4664 discard_cleanups (old_chain
);
4665 event_ptid
= process_stop_reply (stop_reply
, status
);
4668 case 'O': /* Console output. */
4669 remote_console_output (buf
+ 1);
4671 /* The target didn't really stop; keep waiting. */
4672 rs
->waiting_for_stop_reply
= 1;
4676 if (last_sent_signal
!= TARGET_SIGNAL_0
)
4678 /* Zero length reply means that we tried 'S' or 'C' and the
4679 remote system doesn't support it. */
4680 target_terminal_ours_for_output ();
4682 ("Can't send signals to this remote system. %s not sent.\n",
4683 target_signal_to_name (last_sent_signal
));
4684 last_sent_signal
= TARGET_SIGNAL_0
;
4685 target_terminal_inferior ();
4687 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
4688 putpkt ((char *) buf
);
4690 /* We just told the target to resume, so a stop reply is in
4692 rs
->waiting_for_stop_reply
= 1;
4695 /* else fallthrough */
4697 warning (_("Invalid remote reply: %s"), buf
);
4699 rs
->waiting_for_stop_reply
= 1;
4703 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
4704 /* Nothing interesting happened. */
4705 return minus_one_ptid
;
4706 else if (status
->kind
!= TARGET_WAITKIND_EXITED
4707 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
4709 if (!ptid_equal (event_ptid
, null_ptid
))
4710 record_currthread (event_ptid
);
4712 event_ptid
= inferior_ptid
;
4715 /* A process exit. Invalidate our notion of current thread. */
4716 record_currthread (minus_one_ptid
);
4721 /* Wait until the remote machine stops, then return, storing status in
4722 STATUS just as `wait' would. */
4725 remote_wait (ptid_t ptid
, struct target_waitstatus
*status
)
4730 event_ptid
= remote_wait_ns (ptid
, status
);
4733 /* In synchronous mode, keep waiting until the target stops. In
4734 asynchronous mode, always return to the event loop. */
4738 event_ptid
= remote_wait_as (ptid
, status
);
4740 while (status
->kind
== TARGET_WAITKIND_IGNORE
4741 && !target_can_async_p ());
4744 if (target_can_async_p ())
4746 /* If there are are events left in the queue tell the event loop
4748 if (stop_reply_queue
)
4749 mark_async_event_handler (remote_async_inferior_event_token
);
4755 /* Fetch a single register using a 'p' packet. */
4758 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
4760 struct remote_state
*rs
= get_remote_state ();
4762 char regp
[MAX_REGISTER_SIZE
];
4765 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
4768 if (reg
->pnum
== -1)
4773 p
+= hexnumstr (p
, reg
->pnum
);
4775 remote_send (&rs
->buf
, &rs
->buf_size
);
4779 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
4783 case PACKET_UNKNOWN
:
4786 error (_("Could not fetch register \"%s\""),
4787 gdbarch_register_name (get_regcache_arch (regcache
), reg
->regnum
));
4790 /* If this register is unfetchable, tell the regcache. */
4793 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
4797 /* Otherwise, parse and supply the value. */
4803 error (_("fetch_register_using_p: early buf termination"));
4805 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
4808 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
4812 /* Fetch the registers included in the target's 'g' packet. */
4815 send_g_packet (void)
4817 struct remote_state
*rs
= get_remote_state ();
4822 sprintf (rs
->buf
, "g");
4823 remote_send (&rs
->buf
, &rs
->buf_size
);
4825 /* We can get out of synch in various cases. If the first character
4826 in the buffer is not a hex character, assume that has happened
4827 and try to fetch another packet to read. */
4828 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
4829 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
4830 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
4831 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
4834 fprintf_unfiltered (gdb_stdlog
,
4835 "Bad register packet; fetching a new packet\n");
4836 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4839 buf_len
= strlen (rs
->buf
);
4841 /* Sanity check the received packet. */
4842 if (buf_len
% 2 != 0)
4843 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
4849 process_g_packet (struct regcache
*regcache
)
4851 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
4852 struct remote_state
*rs
= get_remote_state ();
4853 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4858 buf_len
= strlen (rs
->buf
);
4860 /* Further sanity checks, with knowledge of the architecture. */
4861 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
4862 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
4864 /* Save the size of the packet sent to us by the target. It is used
4865 as a heuristic when determining the max size of packets that the
4866 target can safely receive. */
4867 if (rsa
->actual_register_packet_size
== 0)
4868 rsa
->actual_register_packet_size
= buf_len
;
4870 /* If this is smaller than we guessed the 'g' packet would be,
4871 update our records. A 'g' reply that doesn't include a register's
4872 value implies either that the register is not available, or that
4873 the 'p' packet must be used. */
4874 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
4876 rsa
->sizeof_g_packet
= buf_len
/ 2;
4878 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
4880 if (rsa
->regs
[i
].pnum
== -1)
4883 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
4884 rsa
->regs
[i
].in_g_packet
= 0;
4886 rsa
->regs
[i
].in_g_packet
= 1;
4890 regs
= alloca (rsa
->sizeof_g_packet
);
4892 /* Unimplemented registers read as all bits zero. */
4893 memset (regs
, 0, rsa
->sizeof_g_packet
);
4895 /* Reply describes registers byte by byte, each byte encoded as two
4896 hex characters. Suck them all up, then supply them to the
4897 register cacheing/storage mechanism. */
4900 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
4902 if (p
[0] == 0 || p
[1] == 0)
4903 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
4904 internal_error (__FILE__
, __LINE__
,
4905 "unexpected end of 'g' packet reply");
4907 if (p
[0] == 'x' && p
[1] == 'x')
4908 regs
[i
] = 0; /* 'x' */
4910 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
4916 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
4918 struct packet_reg
*r
= &rsa
->regs
[i
];
4921 if (r
->offset
* 2 >= strlen (rs
->buf
))
4922 /* This shouldn't happen - we adjusted in_g_packet above. */
4923 internal_error (__FILE__
, __LINE__
,
4924 "unexpected end of 'g' packet reply");
4925 else if (rs
->buf
[r
->offset
* 2] == 'x')
4927 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
4928 /* The register isn't available, mark it as such (at
4929 the same time setting the value to zero). */
4930 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
4933 regcache_raw_supply (regcache
, r
->regnum
,
4941 fetch_registers_using_g (struct regcache
*regcache
)
4944 process_g_packet (regcache
);
4948 remote_fetch_registers (struct regcache
*regcache
, int regnum
)
4950 struct remote_state
*rs
= get_remote_state ();
4951 struct remote_arch_state
*rsa
= get_remote_arch_state ();
4954 set_general_thread (inferior_ptid
);
4958 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
4959 gdb_assert (reg
!= NULL
);
4961 /* If this register might be in the 'g' packet, try that first -
4962 we are likely to read more than one register. If this is the
4963 first 'g' packet, we might be overly optimistic about its
4964 contents, so fall back to 'p'. */
4965 if (reg
->in_g_packet
)
4967 fetch_registers_using_g (regcache
);
4968 if (reg
->in_g_packet
)
4972 if (fetch_register_using_p (regcache
, reg
))
4975 /* This register is not available. */
4976 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
4981 fetch_registers_using_g (regcache
);
4983 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
4984 if (!rsa
->regs
[i
].in_g_packet
)
4985 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
4987 /* This register is not available. */
4988 regcache_raw_supply (regcache
, i
, NULL
);
4992 /* Prepare to store registers. Since we may send them all (using a
4993 'G' request), we have to read out the ones we don't want to change
4997 remote_prepare_to_store (struct regcache
*regcache
)
4999 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5001 gdb_byte buf
[MAX_REGISTER_SIZE
];
5003 /* Make sure the entire registers array is valid. */
5004 switch (remote_protocol_packets
[PACKET_P
].support
)
5006 case PACKET_DISABLE
:
5007 case PACKET_SUPPORT_UNKNOWN
:
5008 /* Make sure all the necessary registers are cached. */
5009 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5010 if (rsa
->regs
[i
].in_g_packet
)
5011 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
5018 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
5019 packet was not recognized. */
5022 store_register_using_P (const struct regcache
*regcache
, struct packet_reg
*reg
)
5024 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5025 struct remote_state
*rs
= get_remote_state ();
5026 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5027 /* Try storing a single register. */
5028 char *buf
= rs
->buf
;
5029 gdb_byte regp
[MAX_REGISTER_SIZE
];
5032 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
5035 if (reg
->pnum
== -1)
5038 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
5039 p
= buf
+ strlen (buf
);
5040 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
5041 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
5042 remote_send (&rs
->buf
, &rs
->buf_size
);
5044 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
5049 error (_("Could not write register \"%s\""),
5050 gdbarch_register_name (gdbarch
, reg
->regnum
));
5051 case PACKET_UNKNOWN
:
5054 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
5058 /* Store register REGNUM, or all registers if REGNUM == -1, from the
5059 contents of the register cache buffer. FIXME: ignores errors. */
5062 store_registers_using_G (const struct regcache
*regcache
)
5064 struct remote_state
*rs
= get_remote_state ();
5065 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5069 /* Extract all the registers in the regcache copying them into a
5073 regs
= alloca (rsa
->sizeof_g_packet
);
5074 memset (regs
, 0, rsa
->sizeof_g_packet
);
5075 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5077 struct packet_reg
*r
= &rsa
->regs
[i
];
5079 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
5083 /* Command describes registers byte by byte,
5084 each byte encoded as two hex characters. */
5087 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
5089 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
5090 remote_send (&rs
->buf
, &rs
->buf_size
);
5093 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
5094 of the register cache buffer. FIXME: ignores errors. */
5097 remote_store_registers (struct regcache
*regcache
, int regnum
)
5099 struct remote_state
*rs
= get_remote_state ();
5100 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5103 set_general_thread (inferior_ptid
);
5107 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5108 gdb_assert (reg
!= NULL
);
5110 /* Always prefer to store registers using the 'P' packet if
5111 possible; we often change only a small number of registers.
5112 Sometimes we change a larger number; we'd need help from a
5113 higher layer to know to use 'G'. */
5114 if (store_register_using_P (regcache
, reg
))
5117 /* For now, don't complain if we have no way to write the
5118 register. GDB loses track of unavailable registers too
5119 easily. Some day, this may be an error. We don't have
5120 any way to read the register, either... */
5121 if (!reg
->in_g_packet
)
5124 store_registers_using_G (regcache
);
5128 store_registers_using_G (regcache
);
5130 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5131 if (!rsa
->regs
[i
].in_g_packet
)
5132 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
5133 /* See above for why we do not issue an error here. */
5138 /* Return the number of hex digits in num. */
5141 hexnumlen (ULONGEST num
)
5145 for (i
= 0; num
!= 0; i
++)
5151 /* Set BUF to the minimum number of hex digits representing NUM. */
5154 hexnumstr (char *buf
, ULONGEST num
)
5156 int len
= hexnumlen (num
);
5157 return hexnumnstr (buf
, num
, len
);
5161 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
5164 hexnumnstr (char *buf
, ULONGEST num
, int width
)
5170 for (i
= width
- 1; i
>= 0; i
--)
5172 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
5179 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
5182 remote_address_masked (CORE_ADDR addr
)
5184 int address_size
= remote_address_size
;
5185 /* If "remoteaddresssize" was not set, default to target address size. */
5187 address_size
= gdbarch_addr_bit (target_gdbarch
);
5189 if (address_size
> 0
5190 && address_size
< (sizeof (ULONGEST
) * 8))
5192 /* Only create a mask when that mask can safely be constructed
5193 in a ULONGEST variable. */
5195 mask
= (mask
<< address_size
) - 1;
5201 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
5202 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
5203 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
5204 (which may be more than *OUT_LEN due to escape characters). The
5205 total number of bytes in the output buffer will be at most
5209 remote_escape_output (const gdb_byte
*buffer
, int len
,
5210 gdb_byte
*out_buf
, int *out_len
,
5213 int input_index
, output_index
;
5216 for (input_index
= 0; input_index
< len
; input_index
++)
5218 gdb_byte b
= buffer
[input_index
];
5220 if (b
== '$' || b
== '#' || b
== '}')
5222 /* These must be escaped. */
5223 if (output_index
+ 2 > out_maxlen
)
5225 out_buf
[output_index
++] = '}';
5226 out_buf
[output_index
++] = b
^ 0x20;
5230 if (output_index
+ 1 > out_maxlen
)
5232 out_buf
[output_index
++] = b
;
5236 *out_len
= input_index
;
5237 return output_index
;
5240 /* Convert BUFFER, escaped data LEN bytes long, into binary data
5241 in OUT_BUF. Return the number of bytes written to OUT_BUF.
5242 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
5244 This function reverses remote_escape_output. It allows more
5245 escaped characters than that function does, in particular because
5246 '*' must be escaped to avoid the run-length encoding processing
5247 in reading packets. */
5250 remote_unescape_input (const gdb_byte
*buffer
, int len
,
5251 gdb_byte
*out_buf
, int out_maxlen
)
5253 int input_index
, output_index
;
5258 for (input_index
= 0; input_index
< len
; input_index
++)
5260 gdb_byte b
= buffer
[input_index
];
5262 if (output_index
+ 1 > out_maxlen
)
5264 warning (_("Received too much data from remote target;"
5265 " ignoring overflow."));
5266 return output_index
;
5271 out_buf
[output_index
++] = b
^ 0x20;
5277 out_buf
[output_index
++] = b
;
5281 error (_("Unmatched escape character in target response."));
5283 return output_index
;
5286 /* Determine whether the remote target supports binary downloading.
5287 This is accomplished by sending a no-op memory write of zero length
5288 to the target at the specified address. It does not suffice to send
5289 the whole packet, since many stubs strip the eighth bit and
5290 subsequently compute a wrong checksum, which causes real havoc with
5293 NOTE: This can still lose if the serial line is not eight-bit
5294 clean. In cases like this, the user should clear "remote
5298 check_binary_download (CORE_ADDR addr
)
5300 struct remote_state
*rs
= get_remote_state ();
5302 switch (remote_protocol_packets
[PACKET_X
].support
)
5304 case PACKET_DISABLE
:
5308 case PACKET_SUPPORT_UNKNOWN
:
5314 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5316 p
+= hexnumstr (p
, (ULONGEST
) 0);
5320 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
5321 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5323 if (rs
->buf
[0] == '\0')
5326 fprintf_unfiltered (gdb_stdlog
,
5327 "binary downloading NOT suppported by target\n");
5328 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
5333 fprintf_unfiltered (gdb_stdlog
,
5334 "binary downloading suppported by target\n");
5335 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
5342 /* Write memory data directly to the remote machine.
5343 This does not inform the data cache; the data cache uses this.
5344 HEADER is the starting part of the packet.
5345 MEMADDR is the address in the remote memory space.
5346 MYADDR is the address of the buffer in our space.
5347 LEN is the number of bytes.
5348 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
5349 should send data as binary ('X'), or hex-encoded ('M').
5351 The function creates packet of the form
5352 <HEADER><ADDRESS>,<LENGTH>:<DATA>
5354 where encoding of <DATA> is termined by PACKET_FORMAT.
5356 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
5359 Returns the number of bytes transferred, or 0 (setting errno) for
5360 error. Only transfer a single packet. */
5363 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
5364 const gdb_byte
*myaddr
, int len
,
5365 char packet_format
, int use_length
)
5367 struct remote_state
*rs
= get_remote_state ();
5377 if (packet_format
!= 'X' && packet_format
!= 'M')
5378 internal_error (__FILE__
, __LINE__
,
5379 "remote_write_bytes_aux: bad packet format");
5384 payload_size
= get_memory_write_packet_size ();
5386 /* The packet buffer will be large enough for the payload;
5387 get_memory_packet_size ensures this. */
5390 /* Compute the size of the actual payload by subtracting out the
5391 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
5393 payload_size
-= strlen ("$,:#NN");
5395 /* The comma won't be used. */
5397 header_length
= strlen (header
);
5398 payload_size
-= header_length
;
5399 payload_size
-= hexnumlen (memaddr
);
5401 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
5403 strcat (rs
->buf
, header
);
5404 p
= rs
->buf
+ strlen (header
);
5406 /* Compute a best guess of the number of bytes actually transfered. */
5407 if (packet_format
== 'X')
5409 /* Best guess at number of bytes that will fit. */
5410 todo
= min (len
, payload_size
);
5412 payload_size
-= hexnumlen (todo
);
5413 todo
= min (todo
, payload_size
);
5417 /* Num bytes that will fit. */
5418 todo
= min (len
, payload_size
/ 2);
5420 payload_size
-= hexnumlen (todo
);
5421 todo
= min (todo
, payload_size
/ 2);
5425 internal_error (__FILE__
, __LINE__
,
5426 _("minumum packet size too small to write data"));
5428 /* If we already need another packet, then try to align the end
5429 of this packet to a useful boundary. */
5430 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
5431 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
5433 /* Append "<memaddr>". */
5434 memaddr
= remote_address_masked (memaddr
);
5435 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
5442 /* Append <len>. Retain the location/size of <len>. It may need to
5443 be adjusted once the packet body has been created. */
5445 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
5453 /* Append the packet body. */
5454 if (packet_format
== 'X')
5456 /* Binary mode. Send target system values byte by byte, in
5457 increasing byte addresses. Only escape certain critical
5459 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
5462 /* If not all TODO bytes fit, then we'll need another packet. Make
5463 a second try to keep the end of the packet aligned. Don't do
5464 this if the packet is tiny. */
5465 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
5469 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
5471 if (new_nr_bytes
!= nr_bytes
)
5472 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
5477 p
+= payload_length
;
5478 if (use_length
&& nr_bytes
< todo
)
5480 /* Escape chars have filled up the buffer prematurely,
5481 and we have actually sent fewer bytes than planned.
5482 Fix-up the length field of the packet. Use the same
5483 number of characters as before. */
5484 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
5485 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
5490 /* Normal mode: Send target system values byte by byte, in
5491 increasing byte addresses. Each byte is encoded as a two hex
5493 nr_bytes
= bin2hex (myaddr
, p
, todo
);
5497 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
5498 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5500 if (rs
->buf
[0] == 'E')
5502 /* There is no correspondance between what the remote protocol
5503 uses for errors and errno codes. We would like a cleaner way
5504 of representing errors (big enough to include errno codes,
5505 bfd_error codes, and others). But for now just return EIO. */
5510 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
5511 fewer bytes than we'd planned. */
5515 /* Write memory data directly to the remote machine.
5516 This does not inform the data cache; the data cache uses this.
5517 MEMADDR is the address in the remote memory space.
5518 MYADDR is the address of the buffer in our space.
5519 LEN is the number of bytes.
5521 Returns number of bytes transferred, or 0 (setting errno) for
5522 error. Only transfer a single packet. */
5525 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
5527 char *packet_format
= 0;
5529 /* Check whether the target supports binary download. */
5530 check_binary_download (memaddr
);
5532 switch (remote_protocol_packets
[PACKET_X
].support
)
5535 packet_format
= "X";
5537 case PACKET_DISABLE
:
5538 packet_format
= "M";
5540 case PACKET_SUPPORT_UNKNOWN
:
5541 internal_error (__FILE__
, __LINE__
,
5542 _("remote_write_bytes: bad internal state"));
5544 internal_error (__FILE__
, __LINE__
, _("bad switch"));
5547 return remote_write_bytes_aux (packet_format
,
5548 memaddr
, myaddr
, len
, packet_format
[0], 1);
5551 /* Read memory data directly from the remote machine.
5552 This does not use the data cache; the data cache uses this.
5553 MEMADDR is the address in the remote memory space.
5554 MYADDR is the address of the buffer in our space.
5555 LEN is the number of bytes.
5557 Returns number of bytes transferred, or 0 for error. */
5559 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
5560 remote targets) shouldn't attempt to read the entire buffer.
5561 Instead it should read a single packet worth of data and then
5562 return the byte size of that packet to the caller. The caller (its
5563 caller and its callers caller ;-) already contains code for
5564 handling partial reads. */
5567 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
5569 struct remote_state
*rs
= get_remote_state ();
5570 int max_buf_size
; /* Max size of packet output buffer. */
5576 max_buf_size
= get_memory_read_packet_size ();
5577 /* The packet buffer will be large enough for the payload;
5578 get_memory_packet_size ensures this. */
5587 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
5589 /* construct "m"<memaddr>","<len>" */
5590 /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */
5591 memaddr
= remote_address_masked (memaddr
);
5594 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
5596 p
+= hexnumstr (p
, (ULONGEST
) todo
);
5600 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5602 if (rs
->buf
[0] == 'E'
5603 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
5604 && rs
->buf
[3] == '\0')
5606 /* There is no correspondance between what the remote
5607 protocol uses for errors and errno codes. We would like
5608 a cleaner way of representing errors (big enough to
5609 include errno codes, bfd_error codes, and others). But
5610 for now just return EIO. */
5615 /* Reply describes memory byte by byte,
5616 each byte encoded as two hex characters. */
5619 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
5621 /* Reply is short. This means that we were able to read
5622 only part of what we wanted to. */
5623 return i
+ (origlen
- len
);
5633 /* Remote notification handler. */
5636 handle_notification (char *buf
, size_t length
)
5638 if (strncmp (buf
, "Stop:", 5) == 0)
5640 if (pending_stop_reply
)
5641 /* We've already parsed the in-flight stop-reply, but the stub
5642 for some reason thought we didn't, possibly due to timeout
5643 on its side. Just ignore it. */
5647 struct cleanup
*old_chain
;
5648 struct stop_reply
*reply
= stop_reply_xmalloc ();
5649 old_chain
= make_cleanup (do_stop_reply_xfree
, reply
);
5651 remote_parse_stop_reply (buf
+ 5, reply
);
5653 discard_cleanups (old_chain
);
5655 /* Be careful to only set it after parsing, since an error
5656 may be thrown then. */
5657 pending_stop_reply
= reply
;
5659 /* Notify the event loop there's a stop reply to acknowledge
5660 and that there may be more events to fetch. */
5661 mark_async_event_handler (remote_async_get_pending_events_token
);
5665 /* We ignore notifications we don't recognize, for compatibility
5666 with newer stubs. */
5671 /* Read or write LEN bytes from inferior memory at MEMADDR,
5672 transferring to or from debugger address BUFFER. Write to inferior
5673 if SHOULD_WRITE is nonzero. Returns length of data written or
5674 read; 0 for error. TARGET is unused. */
5677 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
5678 int should_write
, struct mem_attrib
*attrib
,
5679 struct target_ops
*target
)
5683 set_general_thread (inferior_ptid
);
5686 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
5688 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
5693 /* Sends a packet with content determined by the printf format string
5694 FORMAT and the remaining arguments, then gets the reply. Returns
5695 whether the packet was a success, a failure, or unknown. */
5698 remote_send_printf (const char *format
, ...)
5700 struct remote_state
*rs
= get_remote_state ();
5701 int max_size
= get_remote_packet_size ();
5704 va_start (ap
, format
);
5707 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
5708 internal_error (__FILE__
, __LINE__
, "Too long remote packet.");
5710 if (putpkt (rs
->buf
) < 0)
5711 error (_("Communication problem with target."));
5714 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5716 return packet_check_result (rs
->buf
);
5720 restore_remote_timeout (void *p
)
5722 int value
= *(int *)p
;
5723 remote_timeout
= value
;
5726 /* Flash writing can take quite some time. We'll set
5727 effectively infinite timeout for flash operations.
5728 In future, we'll need to decide on a better approach. */
5729 static const int remote_flash_timeout
= 1000;
5732 remote_flash_erase (struct target_ops
*ops
,
5733 ULONGEST address
, LONGEST length
)
5735 int saved_remote_timeout
= remote_timeout
;
5736 enum packet_result ret
;
5738 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5739 &saved_remote_timeout
);
5740 remote_timeout
= remote_flash_timeout
;
5742 ret
= remote_send_printf ("vFlashErase:%s,%s",
5747 case PACKET_UNKNOWN
:
5748 error (_("Remote target does not support flash erase"));
5750 error (_("Error erasing flash with vFlashErase packet"));
5755 do_cleanups (back_to
);
5759 remote_flash_write (struct target_ops
*ops
,
5760 ULONGEST address
, LONGEST length
,
5761 const gdb_byte
*data
)
5763 int saved_remote_timeout
= remote_timeout
;
5765 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5766 &saved_remote_timeout
);
5768 remote_timeout
= remote_flash_timeout
;
5769 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
5770 do_cleanups (back_to
);
5776 remote_flash_done (struct target_ops
*ops
)
5778 int saved_remote_timeout
= remote_timeout
;
5780 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
5781 &saved_remote_timeout
);
5783 remote_timeout
= remote_flash_timeout
;
5784 ret
= remote_send_printf ("vFlashDone");
5785 do_cleanups (back_to
);
5789 case PACKET_UNKNOWN
:
5790 error (_("Remote target does not support vFlashDone"));
5792 error (_("Error finishing flash operation"));
5799 remote_files_info (struct target_ops
*ignore
)
5801 puts_filtered ("Debugging a target over a serial line.\n");
5804 /* Stuff for dealing with the packets which are part of this protocol.
5805 See comment at top of file for details. */
5807 /* Read a single character from the remote end. */
5810 readchar (int timeout
)
5814 ch
= serial_readchar (remote_desc
, timeout
);
5819 switch ((enum serial_rc
) ch
)
5823 error (_("Remote connection closed"));
5826 perror_with_name (_("Remote communication error"));
5828 case SERIAL_TIMEOUT
:
5834 /* Send the command in *BUF to the remote machine, and read the reply
5835 into *BUF. Report an error if we get an error reply. Resize
5836 *BUF using xrealloc if necessary to hold the result, and update
5840 remote_send (char **buf
,
5844 getpkt (buf
, sizeof_buf
, 0);
5846 if ((*buf
)[0] == 'E')
5847 error (_("Remote failure reply: %s"), *buf
);
5850 /* Display a null-terminated packet on stdout, for debugging, using C
5854 print_packet (char *buf
)
5856 puts_filtered ("\"");
5857 fputstr_filtered (buf
, '"', gdb_stdout
);
5858 puts_filtered ("\"");
5864 return putpkt_binary (buf
, strlen (buf
));
5867 /* Send a packet to the remote machine, with error checking. The data
5868 of the packet is in BUF. The string in BUF can be at most
5869 get_remote_packet_size () - 5 to account for the $, # and checksum,
5870 and for a possible /0 if we are debugging (remote_debug) and want
5871 to print the sent packet as a string. */
5874 putpkt_binary (char *buf
, int cnt
)
5876 struct remote_state
*rs
= get_remote_state ();
5878 unsigned char csum
= 0;
5879 char *buf2
= alloca (cnt
+ 6);
5885 /* Catch cases like trying to read memory or listing threads while
5886 we're waiting for a stop reply. The remote server wouldn't be
5887 ready to handle this request, so we'd hang and timeout. We don't
5888 have to worry about this in synchronous mode, because in that
5889 case it's not possible to issue a command while the target is
5890 running. This is not a problem in non-stop mode, because in that
5891 case, the stub is always ready to process serial input. */
5892 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
5893 error (_("Cannot execute this command while the target is running."));
5895 /* We're sending out a new packet. Make sure we don't look at a
5896 stale cached response. */
5897 rs
->cached_wait_status
= 0;
5899 /* Copy the packet into buffer BUF2, encapsulating it
5900 and giving it a checksum. */
5905 for (i
= 0; i
< cnt
; i
++)
5911 *p
++ = tohex ((csum
>> 4) & 0xf);
5912 *p
++ = tohex (csum
& 0xf);
5914 /* Send it over and over until we get a positive ack. */
5918 int started_error_output
= 0;
5923 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
5924 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
5925 fprintf_unfiltered (gdb_stdlog
, "...");
5926 gdb_flush (gdb_stdlog
);
5928 if (serial_write (remote_desc
, buf2
, p
- buf2
))
5929 perror_with_name (_("putpkt: write failed"));
5931 /* If this is a no acks version of the remote protocol, send the
5932 packet and move on. */
5936 /* Read until either a timeout occurs (-2) or '+' is read.
5937 Handle any notification that arrives in the mean time. */
5940 ch
= readchar (remote_timeout
);
5948 case SERIAL_TIMEOUT
:
5951 if (started_error_output
)
5953 putchar_unfiltered ('\n');
5954 started_error_output
= 0;
5963 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
5967 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
5968 case SERIAL_TIMEOUT
:
5972 break; /* Retransmit buffer. */
5976 fprintf_unfiltered (gdb_stdlog
,
5977 "Packet instead of Ack, ignoring it\n");
5978 /* It's probably an old response sent because an ACK
5979 was lost. Gobble up the packet and ack it so it
5980 doesn't get retransmitted when we resend this
5983 serial_write (remote_desc
, "+", 1);
5984 continue; /* Now, go look for +. */
5991 /* If we got a notification, handle it, and go back to looking
5993 /* We've found the start of a notification. Now
5994 collect the data. */
5995 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
6000 fprintf_unfiltered (gdb_stdlog
, " Notification received: ");
6001 fputstrn_unfiltered (rs
->buf
, val
, 0, gdb_stdlog
);
6002 fprintf_unfiltered (gdb_stdlog
, "\n");
6004 handle_notification (rs
->buf
, val
);
6005 /* We're in sync now, rewait for the ack. */
6012 if (!started_error_output
)
6014 started_error_output
= 1;
6015 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6017 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6018 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
6027 if (!started_error_output
)
6029 started_error_output
= 1;
6030 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6032 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6036 break; /* Here to retransmit. */
6040 /* This is wrong. If doing a long backtrace, the user should be
6041 able to get out next time we call QUIT, without anything as
6042 violent as interrupt_query. If we want to provide a way out of
6043 here without getting to the next QUIT, it should be based on
6044 hitting ^C twice as in remote_wait. */
6055 /* Come here after finding the start of a frame when we expected an
6056 ack. Do our best to discard the rest of this packet. */
6065 c
= readchar (remote_timeout
);
6068 case SERIAL_TIMEOUT
:
6069 /* Nothing we can do. */
6072 /* Discard the two bytes of checksum and stop. */
6073 c
= readchar (remote_timeout
);
6075 c
= readchar (remote_timeout
);
6078 case '*': /* Run length encoding. */
6079 /* Discard the repeat count. */
6080 c
= readchar (remote_timeout
);
6085 /* A regular character. */
6091 /* Come here after finding the start of the frame. Collect the rest
6092 into *BUF, verifying the checksum, length, and handling run-length
6093 compression. NUL terminate the buffer. If there is not enough room,
6094 expand *BUF using xrealloc.
6096 Returns -1 on error, number of characters in buffer (ignoring the
6097 trailing NULL) on success. (could be extended to return one of the
6098 SERIAL status indications). */
6101 read_frame (char **buf_p
,
6108 struct remote_state
*rs
= get_remote_state ();
6115 c
= readchar (remote_timeout
);
6118 case SERIAL_TIMEOUT
:
6120 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
6124 fputs_filtered ("Saw new packet start in middle of old one\n",
6126 return -1; /* Start a new packet, count retries. */
6129 unsigned char pktcsum
;
6135 check_0
= readchar (remote_timeout
);
6137 check_1
= readchar (remote_timeout
);
6139 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
6142 fputs_filtered ("Timeout in checksum, retrying\n",
6146 else if (check_0
< 0 || check_1
< 0)
6149 fputs_filtered ("Communication error in checksum\n",
6154 /* Don't recompute the checksum; with no ack packets we
6155 don't have any way to indicate a packet retransmission
6160 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
6161 if (csum
== pktcsum
)
6166 fprintf_filtered (gdb_stdlog
,
6167 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
6169 fputstrn_filtered (buf
, bc
, 0, gdb_stdlog
);
6170 fputs_filtered ("\n", gdb_stdlog
);
6172 /* Number of characters in buffer ignoring trailing
6176 case '*': /* Run length encoding. */
6181 c
= readchar (remote_timeout
);
6183 repeat
= c
- ' ' + 3; /* Compute repeat count. */
6185 /* The character before ``*'' is repeated. */
6187 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
6189 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
6191 /* Make some more room in the buffer. */
6192 *sizeof_buf
+= repeat
;
6193 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
6197 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
6203 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
6207 if (bc
>= *sizeof_buf
- 1)
6209 /* Make some more room in the buffer. */
6211 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
6222 /* Read a packet from the remote machine, with error checking, and
6223 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
6224 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
6225 rather than timing out; this is used (in synchronous mode) to wait
6226 for a target that is is executing user code to stop. */
6227 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
6228 don't have to change all the calls to getpkt to deal with the
6229 return value, because at the moment I don't know what the right
6230 thing to do it for those. */
6238 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
6242 /* Read a packet from the remote machine, with error checking, and
6243 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
6244 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
6245 rather than timing out; this is used (in synchronous mode) to wait
6246 for a target that is is executing user code to stop. If FOREVER ==
6247 0, this function is allowed to time out gracefully and return an
6248 indication of this to the caller. Otherwise return the number of
6249 bytes read. If EXPECTING_NOTIF, consider receiving a notification
6250 enough reason to return to the caller. */
6253 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
6254 int expecting_notif
)
6256 struct remote_state
*rs
= get_remote_state ();
6262 /* We're reading a new response. Make sure we don't look at a
6263 previously cached response. */
6264 rs
->cached_wait_status
= 0;
6266 strcpy (*buf
, "timeout");
6269 timeout
= watchdog
> 0 ? watchdog
: -1;
6270 else if (expecting_notif
)
6271 timeout
= 0; /* There should already be a char in the buffer. If
6274 timeout
= remote_timeout
;
6278 /* Process any number of notifications, and then return when
6282 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
6284 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
6286 /* This can loop forever if the remote side sends us
6287 characters continuously, but if it pauses, we'll get
6288 SERIAL_TIMEOUT from readchar because of timeout. Then
6289 we'll count that as a retry.
6291 Note that even when forever is set, we will only wait
6292 forever prior to the start of a packet. After that, we
6293 expect characters to arrive at a brisk pace. They should
6294 show up within remote_timeout intervals. */
6296 c
= readchar (timeout
);
6297 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
6299 if (c
== SERIAL_TIMEOUT
)
6301 if (expecting_notif
)
6302 return -1; /* Don't complain, it's normal to not get
6303 anything in this case. */
6305 if (forever
) /* Watchdog went off? Kill the target. */
6309 error (_("Watchdog timeout has expired. Target detached."));
6312 fputs_filtered ("Timed out.\n", gdb_stdlog
);
6316 /* We've found the start of a packet or notification.
6317 Now collect the data. */
6318 val
= read_frame (buf
, sizeof_buf
);
6323 serial_write (remote_desc
, "-", 1);
6326 if (tries
> MAX_TRIES
)
6328 /* We have tried hard enough, and just can't receive the
6329 packet/notification. Give up. */
6330 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
6332 /* Skip the ack char if we're in no-ack mode. */
6333 if (!rs
->noack_mode
)
6334 serial_write (remote_desc
, "+", 1);
6338 /* If we got an ordinary packet, return that to our caller. */
6343 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
6344 fputstrn_unfiltered (*buf
, val
, 0, gdb_stdlog
);
6345 fprintf_unfiltered (gdb_stdlog
, "\n");
6348 /* Skip the ack char if we're in no-ack mode. */
6349 if (!rs
->noack_mode
)
6350 serial_write (remote_desc
, "+", 1);
6354 /* If we got a notification, handle it, and go back to looking
6358 gdb_assert (c
== '%');
6362 fprintf_unfiltered (gdb_stdlog
, " Notification received: ");
6363 fputstrn_unfiltered (*buf
, val
, 0, gdb_stdlog
);
6364 fprintf_unfiltered (gdb_stdlog
, "\n");
6367 handle_notification (*buf
, val
);
6369 /* Notifications require no acknowledgement. */
6371 if (expecting_notif
)
6378 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
6380 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0);
6384 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
)
6386 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1);
6393 /* Use catch_errors so the user can quit from gdb even when we
6394 aren't on speaking terms with the remote system. */
6395 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
6397 /* Don't wait for it to die. I'm not really sure it matters whether
6398 we do or not. For the existing stubs, kill is a noop. */
6399 target_mourn_inferior ();
6403 remote_vkill (int pid
, struct remote_state
*rs
)
6405 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
6408 /* Tell the remote target to detach. */
6409 sprintf (rs
->buf
, "vKill;%x", pid
);
6411 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6413 if (packet_ok (rs
->buf
,
6414 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
6416 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
6423 extended_remote_kill (void)
6426 int pid
= ptid_get_pid (inferior_ptid
);
6427 struct remote_state
*rs
= get_remote_state ();
6429 res
= remote_vkill (pid
, rs
);
6430 if (res
== -1 && !remote_multi_process_p (rs
))
6432 /* Don't try 'k' on a multi-process aware stub -- it has no way
6433 to specify the pid. */
6437 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6438 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
6441 /* Don't wait for it to die. I'm not really sure it matters whether
6442 we do or not. For the existing stubs, kill is a noop. */
6448 error (_("Can't kill process"));
6450 delete_inferior (pid
);
6451 target_mourn_inferior ();
6455 remote_mourn (struct target_ops
*ops
)
6457 remote_mourn_1 (ops
);
6460 /* Worker function for remote_mourn. */
6462 remote_mourn_1 (struct target_ops
*target
)
6464 unpush_target (target
);
6466 /* remote_close takes care of cleaning up. */
6470 select_new_thread_callback (struct thread_info
*th
, void* data
)
6472 if (!is_exited (th
->ptid
))
6474 switch_to_thread (th
->ptid
);
6475 printf_filtered (_("[Switching to %s]\n"),
6476 target_pid_to_str (inferior_ptid
));
6483 extended_remote_mourn_1 (struct target_ops
*target
)
6485 struct remote_state
*rs
= get_remote_state ();
6487 /* In case we got here due to an error, but we're going to stay
6489 rs
->waiting_for_stop_reply
= 0;
6491 /* We're no longer interested in these events. */
6492 discard_pending_stop_replies (ptid_get_pid (inferior_ptid
));
6494 /* Unlike "target remote", we do not want to unpush the target; then
6495 the next time the user says "run", we won't be connected. */
6497 if (have_inferiors ())
6499 extern void nullify_last_target_wait_ptid ();
6500 /* Multi-process case. The current process has exited, but
6501 there are other processes to debug. Switch to the first
6503 iterate_over_threads (select_new_thread_callback
, NULL
);
6504 nullify_last_target_wait_ptid ();
6508 struct remote_state
*rs
= get_remote_state ();
6510 /* Call common code to mark the inferior as not running. */
6511 generic_mourn_inferior ();
6512 if (!remote_multi_process_p (rs
))
6514 /* Check whether the target is running now - some remote stubs
6515 automatically restart after kill. */
6517 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6519 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
6521 /* Assume that the target has been restarted. Set inferior_ptid
6522 so that bits of core GDB realizes there's something here, e.g.,
6523 so that the user can say "kill" again. */
6524 inferior_ptid
= magic_null_ptid
;
6528 /* Mark this (still pushed) target as not executable until we
6530 target_mark_exited (target
);
6534 /* Always remove execution if this was the last process. */
6535 target_mark_exited (target
);
6540 extended_remote_mourn (struct target_ops
*ops
)
6542 extended_remote_mourn_1 (ops
);
6546 extended_remote_run (char *args
)
6548 struct remote_state
*rs
= get_remote_state ();
6552 /* If the user has disabled vRun support, or we have detected that
6553 support is not available, do not try it. */
6554 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
6557 strcpy (rs
->buf
, "vRun;");
6558 len
= strlen (rs
->buf
);
6560 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
6561 error (_("Remote file name too long for run packet"));
6562 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
, 0);
6564 gdb_assert (args
!= NULL
);
6567 struct cleanup
*back_to
;
6571 argv
= gdb_buildargv (args
);
6572 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
6573 for (i
= 0; argv
[i
] != NULL
; i
++)
6575 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
6576 error (_("Argument list too long for run packet"));
6577 rs
->buf
[len
++] = ';';
6578 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
, 0);
6580 do_cleanups (back_to
);
6583 rs
->buf
[len
++] = '\0';
6586 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6588 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
6590 /* We have a wait response; we don't need it, though. All is well. */
6593 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
6594 /* It wasn't disabled before, but it is now. */
6598 if (remote_exec_file
[0] == '\0')
6599 error (_("Running the default executable on the remote target failed; "
6600 "try \"set remote exec-file\"?"));
6602 error (_("Running \"%s\" on the remote target failed"),
6607 /* In the extended protocol we want to be able to do things like
6608 "run" and have them basically work as expected. So we need
6609 a special create_inferior function. We support changing the
6610 executable file and the command line arguments, but not the
6614 extended_remote_create_inferior_1 (char *exec_file
, char *args
,
6615 char **env
, int from_tty
)
6617 /* If running asynchronously, register the target file descriptor
6618 with the event loop. */
6619 if (target_can_async_p ())
6620 target_async (inferior_event_handler
, 0);
6622 /* Now restart the remote server. */
6623 if (extended_remote_run (args
) == -1)
6625 /* vRun was not supported. Fail if we need it to do what the
6627 if (remote_exec_file
[0])
6628 error (_("Remote target does not support \"set remote exec-file\""));
6630 error (_("Remote target does not support \"set args\" or run <ARGS>"));
6632 /* Fall back to "R". */
6633 extended_remote_restart ();
6636 /* Clean up from the last time we ran, before we mark the target
6637 running again. This will mark breakpoints uninserted, and
6638 get_offsets may insert breakpoints. */
6639 init_thread_list ();
6640 init_wait_for_inferior ();
6642 /* Now mark the inferior as running before we do anything else. */
6643 inferior_ptid
= magic_null_ptid
;
6645 /* Now, if we have thread information, update inferior_ptid. */
6646 inferior_ptid
= remote_current_thread (inferior_ptid
);
6648 add_inferior (ptid_get_pid (inferior_ptid
));
6649 add_thread_silent (inferior_ptid
);
6651 target_mark_running (&extended_remote_ops
);
6653 /* Get updated offsets, if the stub uses qOffsets. */
6658 extended_remote_create_inferior (struct target_ops
*ops
,
6659 char *exec_file
, char *args
,
6660 char **env
, int from_tty
)
6662 extended_remote_create_inferior_1 (exec_file
, args
, env
, from_tty
);
6666 /* Insert a breakpoint. On targets that have software breakpoint
6667 support, we ask the remote target to do the work; on targets
6668 which don't, we insert a traditional memory breakpoint. */
6671 remote_insert_breakpoint (struct bp_target_info
*bp_tgt
)
6673 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
6674 If it succeeds, then set the support to PACKET_ENABLE. If it
6675 fails, and the user has explicitly requested the Z support then
6676 report an error, otherwise, mark it disabled and go on. */
6678 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
6680 CORE_ADDR addr
= bp_tgt
->placed_address
;
6681 struct remote_state
*rs
;
6685 gdbarch_breakpoint_from_pc (target_gdbarch
, &addr
, &bpsize
);
6687 rs
= get_remote_state ();
6693 addr
= (ULONGEST
) remote_address_masked (addr
);
6694 p
+= hexnumstr (p
, addr
);
6695 sprintf (p
, ",%d", bpsize
);
6698 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6700 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
6705 bp_tgt
->placed_address
= addr
;
6706 bp_tgt
->placed_size
= bpsize
;
6708 case PACKET_UNKNOWN
:
6713 return memory_insert_breakpoint (bp_tgt
);
6717 remote_remove_breakpoint (struct bp_target_info
*bp_tgt
)
6719 CORE_ADDR addr
= bp_tgt
->placed_address
;
6720 struct remote_state
*rs
= get_remote_state ();
6723 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
6731 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
6732 p
+= hexnumstr (p
, addr
);
6733 sprintf (p
, ",%d", bp_tgt
->placed_size
);
6736 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6738 return (rs
->buf
[0] == 'E');
6741 return memory_remove_breakpoint (bp_tgt
);
6745 watchpoint_to_Z_packet (int type
)
6750 return Z_PACKET_WRITE_WP
;
6753 return Z_PACKET_READ_WP
;
6756 return Z_PACKET_ACCESS_WP
;
6759 internal_error (__FILE__
, __LINE__
,
6760 _("hw_bp_to_z: bad watchpoint type %d"), type
);
6765 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
6767 struct remote_state
*rs
= get_remote_state ();
6769 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
6771 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
6774 sprintf (rs
->buf
, "Z%x,", packet
);
6775 p
= strchr (rs
->buf
, '\0');
6776 addr
= remote_address_masked (addr
);
6777 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6778 sprintf (p
, ",%x", len
);
6781 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6783 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
6786 case PACKET_UNKNOWN
:
6791 internal_error (__FILE__
, __LINE__
,
6792 _("remote_insert_watchpoint: reached end of function"));
6797 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
6799 struct remote_state
*rs
= get_remote_state ();
6801 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
6803 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
6806 sprintf (rs
->buf
, "z%x,", packet
);
6807 p
= strchr (rs
->buf
, '\0');
6808 addr
= remote_address_masked (addr
);
6809 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6810 sprintf (p
, ",%x", len
);
6812 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6814 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
6817 case PACKET_UNKNOWN
:
6822 internal_error (__FILE__
, __LINE__
,
6823 _("remote_remove_watchpoint: reached end of function"));
6827 int remote_hw_watchpoint_limit
= -1;
6828 int remote_hw_breakpoint_limit
= -1;
6831 remote_check_watch_resources (int type
, int cnt
, int ot
)
6833 if (type
== bp_hardware_breakpoint
)
6835 if (remote_hw_breakpoint_limit
== 0)
6837 else if (remote_hw_breakpoint_limit
< 0)
6839 else if (cnt
<= remote_hw_breakpoint_limit
)
6844 if (remote_hw_watchpoint_limit
== 0)
6846 else if (remote_hw_watchpoint_limit
< 0)
6850 else if (cnt
<= remote_hw_watchpoint_limit
)
6857 remote_stopped_by_watchpoint (void)
6859 return remote_stopped_by_watchpoint_p
;
6863 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
6866 if (remote_stopped_by_watchpoint ())
6868 *addr_p
= remote_watch_data_address
;
6877 remote_insert_hw_breakpoint (struct bp_target_info
*bp_tgt
)
6880 struct remote_state
*rs
;
6883 /* The length field should be set to the size of a breakpoint
6884 instruction, even though we aren't inserting one ourselves. */
6886 gdbarch_breakpoint_from_pc
6887 (target_gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
6889 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
6892 rs
= get_remote_state ();
6899 addr
= remote_address_masked (bp_tgt
->placed_address
);
6900 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6901 sprintf (p
, ",%x", bp_tgt
->placed_size
);
6904 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6906 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
6909 case PACKET_UNKNOWN
:
6914 internal_error (__FILE__
, __LINE__
,
6915 _("remote_insert_hw_breakpoint: reached end of function"));
6920 remote_remove_hw_breakpoint (struct bp_target_info
*bp_tgt
)
6923 struct remote_state
*rs
= get_remote_state ();
6926 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
6933 addr
= remote_address_masked (bp_tgt
->placed_address
);
6934 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6935 sprintf (p
, ",%x", bp_tgt
->placed_size
);
6938 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6940 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
6943 case PACKET_UNKNOWN
:
6948 internal_error (__FILE__
, __LINE__
,
6949 _("remote_remove_hw_breakpoint: reached end of function"));
6952 /* Table used by the crc32 function to calcuate the checksum. */
6954 static unsigned long crc32_table
[256] =
6957 static unsigned long
6958 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
6960 if (!crc32_table
[1])
6962 /* Initialize the CRC table and the decoding table. */
6966 for (i
= 0; i
< 256; i
++)
6968 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
6969 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
6976 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
6982 /* compare-sections command
6984 With no arguments, compares each loadable section in the exec bfd
6985 with the same memory range on the target, and reports mismatches.
6986 Useful for verifying the image on the target against the exec file.
6987 Depends on the target understanding the new "qCRC:" request. */
6989 /* FIXME: cagney/1999-10-26: This command should be broken down into a
6990 target method (target verify memory) and generic version of the
6991 actual command. This will allow other high-level code (especially
6992 generic_load()) to make use of this target functionality. */
6995 compare_sections_command (char *args
, int from_tty
)
6997 struct remote_state
*rs
= get_remote_state ();
6999 unsigned long host_crc
, target_crc
;
7000 extern bfd
*exec_bfd
;
7001 struct cleanup
*old_chain
;
7004 const char *sectname
;
7011 error (_("command cannot be used without an exec file"));
7012 if (!current_target
.to_shortname
||
7013 strcmp (current_target
.to_shortname
, "remote") != 0)
7014 error (_("command can only be used with remote target"));
7016 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
7018 if (!(s
->flags
& SEC_LOAD
))
7019 continue; /* skip non-loadable section */
7021 size
= bfd_get_section_size (s
);
7023 continue; /* skip zero-length section */
7025 sectname
= bfd_get_section_name (exec_bfd
, s
);
7026 if (args
&& strcmp (args
, sectname
) != 0)
7027 continue; /* not the section selected by user */
7029 matched
= 1; /* do this section */
7031 /* FIXME: assumes lma can fit into long. */
7032 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
7033 (long) lma
, (long) size
);
7036 /* Be clever; compute the host_crc before waiting for target
7038 sectdata
= xmalloc (size
);
7039 old_chain
= make_cleanup (xfree
, sectdata
);
7040 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
7041 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
7043 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7044 if (rs
->buf
[0] == 'E')
7045 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
7046 sectname
, paddr (lma
), paddr (lma
+ size
));
7047 if (rs
->buf
[0] != 'C')
7048 error (_("remote target does not support this operation"));
7050 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
7051 target_crc
= target_crc
* 16 + fromhex (*tmp
);
7053 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
7054 sectname
, paddr (lma
), paddr (lma
+ size
));
7055 if (host_crc
== target_crc
)
7056 printf_filtered ("matched.\n");
7059 printf_filtered ("MIS-MATCHED!\n");
7063 do_cleanups (old_chain
);
7066 warning (_("One or more sections of the remote executable does not match\n\
7067 the loaded file\n"));
7068 if (args
&& !matched
)
7069 printf_filtered (_("No loaded section named '%s'.\n"), args
);
7072 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
7073 into remote target. The number of bytes written to the remote
7074 target is returned, or -1 for error. */
7077 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
7078 const char *annex
, const gdb_byte
*writebuf
,
7079 ULONGEST offset
, LONGEST len
,
7080 struct packet_config
*packet
)
7085 struct remote_state
*rs
= get_remote_state ();
7086 int max_size
= get_memory_write_packet_size ();
7088 if (packet
->support
== PACKET_DISABLE
)
7091 /* Insert header. */
7092 i
= snprintf (rs
->buf
, max_size
,
7093 "qXfer:%s:write:%s:%s:",
7094 object_name
, annex
? annex
: "",
7095 phex_nz (offset
, sizeof offset
));
7096 max_size
-= (i
+ 1);
7098 /* Escape as much data as fits into rs->buf. */
7099 buf_len
= remote_escape_output
7100 (writebuf
, len
, (rs
->buf
+ i
), &max_size
, max_size
);
7102 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
7103 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
7104 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7107 unpack_varlen_hex (rs
->buf
, &n
);
7111 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
7112 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
7113 number of bytes read is returned, or 0 for EOF, or -1 for error.
7114 The number of bytes read may be less than LEN without indicating an
7115 EOF. PACKET is checked and updated to indicate whether the remote
7116 target supports this object. */
7119 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
7121 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
7122 struct packet_config
*packet
)
7124 static char *finished_object
;
7125 static char *finished_annex
;
7126 static ULONGEST finished_offset
;
7128 struct remote_state
*rs
= get_remote_state ();
7129 unsigned int total
= 0;
7130 LONGEST i
, n
, packet_len
;
7132 if (packet
->support
== PACKET_DISABLE
)
7135 /* Check whether we've cached an end-of-object packet that matches
7137 if (finished_object
)
7139 if (strcmp (object_name
, finished_object
) == 0
7140 && strcmp (annex
? annex
: "", finished_annex
) == 0
7141 && offset
== finished_offset
)
7144 /* Otherwise, we're now reading something different. Discard
7146 xfree (finished_object
);
7147 xfree (finished_annex
);
7148 finished_object
= NULL
;
7149 finished_annex
= NULL
;
7152 /* Request only enough to fit in a single packet. The actual data
7153 may not, since we don't know how much of it will need to be escaped;
7154 the target is free to respond with slightly less data. We subtract
7155 five to account for the response type and the protocol frame. */
7156 n
= min (get_remote_packet_size () - 5, len
);
7157 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
7158 object_name
, annex
? annex
: "",
7159 phex_nz (offset
, sizeof offset
),
7160 phex_nz (n
, sizeof n
));
7161 i
= putpkt (rs
->buf
);
7166 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
7167 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7170 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
7171 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
7173 /* 'm' means there is (or at least might be) more data after this
7174 batch. That does not make sense unless there's at least one byte
7175 of data in this reply. */
7176 if (rs
->buf
[0] == 'm' && packet_len
== 1)
7177 error (_("Remote qXfer reply contained no data."));
7179 /* Got some data. */
7180 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
7182 /* 'l' is an EOF marker, possibly including a final block of data,
7183 or possibly empty. If we have the final block of a non-empty
7184 object, record this fact to bypass a subsequent partial read. */
7185 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
7187 finished_object
= xstrdup (object_name
);
7188 finished_annex
= xstrdup (annex
? annex
: "");
7189 finished_offset
= offset
+ i
;
7196 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
7197 const char *annex
, gdb_byte
*readbuf
,
7198 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
7200 struct remote_state
*rs
;
7205 set_general_thread (inferior_ptid
);
7207 rs
= get_remote_state ();
7209 /* Handle memory using the standard memory routines. */
7210 if (object
== TARGET_OBJECT_MEMORY
)
7215 /* If the remote target is connected but not running, we should
7216 pass this request down to a lower stratum (e.g. the executable
7218 if (!target_has_execution
)
7221 if (writebuf
!= NULL
)
7222 xfered
= remote_write_bytes (offset
, writebuf
, len
);
7224 xfered
= remote_read_bytes (offset
, readbuf
, len
);
7228 else if (xfered
== 0 && errno
== 0)
7234 /* Handle SPU memory using qxfer packets. */
7235 if (object
== TARGET_OBJECT_SPU
)
7238 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
7239 &remote_protocol_packets
7240 [PACKET_qXfer_spu_read
]);
7242 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
7243 &remote_protocol_packets
7244 [PACKET_qXfer_spu_write
]);
7247 /* Only handle flash writes. */
7248 if (writebuf
!= NULL
)
7254 case TARGET_OBJECT_FLASH
:
7255 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
7259 else if (xfered
== 0 && errno
== 0)
7269 /* Map pre-existing objects onto letters. DO NOT do this for new
7270 objects!!! Instead specify new query packets. */
7273 case TARGET_OBJECT_AVR
:
7277 case TARGET_OBJECT_AUXV
:
7278 gdb_assert (annex
== NULL
);
7279 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
7280 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
7282 case TARGET_OBJECT_AVAILABLE_FEATURES
:
7283 return remote_read_qxfer
7284 (ops
, "features", annex
, readbuf
, offset
, len
,
7285 &remote_protocol_packets
[PACKET_qXfer_features
]);
7287 case TARGET_OBJECT_LIBRARIES
:
7288 return remote_read_qxfer
7289 (ops
, "libraries", annex
, readbuf
, offset
, len
,
7290 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
7292 case TARGET_OBJECT_MEMORY_MAP
:
7293 gdb_assert (annex
== NULL
);
7294 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
7295 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
7301 /* Note: a zero OFFSET and LEN can be used to query the minimum
7303 if (offset
== 0 && len
== 0)
7304 return (get_remote_packet_size ());
7305 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
7306 large enough let the caller deal with it. */
7307 if (len
< get_remote_packet_size ())
7309 len
= get_remote_packet_size ();
7311 /* Except for querying the minimum buffer size, target must be open. */
7313 error (_("remote query is only available after target open"));
7315 gdb_assert (annex
!= NULL
);
7316 gdb_assert (readbuf
!= NULL
);
7322 /* We used one buffer char for the remote protocol q command and
7323 another for the query type. As the remote protocol encapsulation
7324 uses 4 chars plus one extra in case we are debugging
7325 (remote_debug), we have PBUFZIZ - 7 left to pack the query
7328 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
7330 /* Bad caller may have sent forbidden characters. */
7331 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
7336 gdb_assert (annex
[i
] == '\0');
7338 i
= putpkt (rs
->buf
);
7342 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7343 strcpy ((char *) readbuf
, rs
->buf
);
7345 return strlen ((char *) readbuf
);
7349 remote_search_memory (struct target_ops
* ops
,
7350 CORE_ADDR start_addr
, ULONGEST search_space_len
,
7351 const gdb_byte
*pattern
, ULONGEST pattern_len
,
7352 CORE_ADDR
*found_addrp
)
7354 struct remote_state
*rs
= get_remote_state ();
7355 int max_size
= get_memory_write_packet_size ();
7356 struct packet_config
*packet
=
7357 &remote_protocol_packets
[PACKET_qSearch_memory
];
7358 /* number of packet bytes used to encode the pattern,
7359 this could be more than PATTERN_LEN due to escape characters */
7360 int escaped_pattern_len
;
7361 /* amount of pattern that was encodable in the packet */
7362 int used_pattern_len
;
7365 ULONGEST found_addr
;
7367 /* Don't go to the target if we don't have to.
7368 This is done before checking packet->support to avoid the possibility that
7369 a success for this edge case means the facility works in general. */
7370 if (pattern_len
> search_space_len
)
7372 if (pattern_len
== 0)
7374 *found_addrp
= start_addr
;
7378 /* If we already know the packet isn't supported, fall back to the simple
7379 way of searching memory. */
7381 if (packet
->support
== PACKET_DISABLE
)
7383 /* Target doesn't provided special support, fall back and use the
7384 standard support (copy memory and do the search here). */
7385 return simple_search_memory (ops
, start_addr
, search_space_len
,
7386 pattern
, pattern_len
, found_addrp
);
7389 /* Insert header. */
7390 i
= snprintf (rs
->buf
, max_size
,
7391 "qSearch:memory:%s;%s;",
7392 paddr_nz (start_addr
),
7393 phex_nz (search_space_len
, sizeof (search_space_len
)));
7394 max_size
-= (i
+ 1);
7396 /* Escape as much data as fits into rs->buf. */
7397 escaped_pattern_len
=
7398 remote_escape_output (pattern
, pattern_len
, (rs
->buf
+ i
),
7399 &used_pattern_len
, max_size
);
7401 /* Bail if the pattern is too large. */
7402 if (used_pattern_len
!= pattern_len
)
7403 error ("Pattern is too large to transmit to remote target.");
7405 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
7406 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
7407 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
7409 /* The request may not have worked because the command is not
7410 supported. If so, fall back to the simple way. */
7411 if (packet
->support
== PACKET_DISABLE
)
7413 return simple_search_memory (ops
, start_addr
, search_space_len
,
7414 pattern
, pattern_len
, found_addrp
);
7419 if (rs
->buf
[0] == '0')
7421 else if (rs
->buf
[0] == '1')
7424 if (rs
->buf
[1] != ',')
7425 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
7426 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
7427 *found_addrp
= found_addr
;
7430 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
7436 remote_rcmd (char *command
,
7437 struct ui_file
*outbuf
)
7439 struct remote_state
*rs
= get_remote_state ();
7443 error (_("remote rcmd is only available after target open"));
7445 /* Send a NULL command across as an empty command. */
7446 if (command
== NULL
)
7449 /* The query prefix. */
7450 strcpy (rs
->buf
, "qRcmd,");
7451 p
= strchr (rs
->buf
, '\0');
7453 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/) > get_remote_packet_size ())
7454 error (_("\"monitor\" command ``%s'' is too long."), command
);
7456 /* Encode the actual command. */
7457 bin2hex ((gdb_byte
*) command
, p
, 0);
7459 if (putpkt (rs
->buf
) < 0)
7460 error (_("Communication problem with target."));
7462 /* get/display the response */
7467 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
7469 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7472 error (_("Target does not support this command."));
7473 if (buf
[0] == 'O' && buf
[1] != 'K')
7475 remote_console_output (buf
+ 1); /* 'O' message from stub. */
7478 if (strcmp (buf
, "OK") == 0)
7480 if (strlen (buf
) == 3 && buf
[0] == 'E'
7481 && isdigit (buf
[1]) && isdigit (buf
[2]))
7483 error (_("Protocol error with Rcmd"));
7485 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
7487 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
7488 fputc_unfiltered (c
, outbuf
);
7494 static VEC(mem_region_s
) *
7495 remote_memory_map (struct target_ops
*ops
)
7497 VEC(mem_region_s
) *result
= NULL
;
7498 char *text
= target_read_stralloc (¤t_target
,
7499 TARGET_OBJECT_MEMORY_MAP
, NULL
);
7503 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
7504 result
= parse_memory_map (text
);
7505 do_cleanups (back_to
);
7512 packet_command (char *args
, int from_tty
)
7514 struct remote_state
*rs
= get_remote_state ();
7517 error (_("command can only be used with remote target"));
7520 error (_("remote-packet command requires packet text as argument"));
7522 puts_filtered ("sending: ");
7523 print_packet (args
);
7524 puts_filtered ("\n");
7527 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7528 puts_filtered ("received: ");
7529 print_packet (rs
->buf
);
7530 puts_filtered ("\n");
7534 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
7536 static void display_thread_info (struct gdb_ext_thread_info
*info
);
7538 static void threadset_test_cmd (char *cmd
, int tty
);
7540 static void threadalive_test (char *cmd
, int tty
);
7542 static void threadlist_test_cmd (char *cmd
, int tty
);
7544 int get_and_display_threadinfo (threadref
*ref
);
7546 static void threadinfo_test_cmd (char *cmd
, int tty
);
7548 static int thread_display_step (threadref
*ref
, void *context
);
7550 static void threadlist_update_test_cmd (char *cmd
, int tty
);
7552 static void init_remote_threadtests (void);
7554 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
7557 threadset_test_cmd (char *cmd
, int tty
)
7559 int sample_thread
= SAMPLE_THREAD
;
7561 printf_filtered (_("Remote threadset test\n"));
7562 set_general_thread (sample_thread
);
7567 threadalive_test (char *cmd
, int tty
)
7569 int sample_thread
= SAMPLE_THREAD
;
7570 int pid
= ptid_get_pid (inferior_ptid
);
7571 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
7573 if (remote_thread_alive (ptid
))
7574 printf_filtered ("PASS: Thread alive test\n");
7576 printf_filtered ("FAIL: Thread alive test\n");
7579 void output_threadid (char *title
, threadref
*ref
);
7582 output_threadid (char *title
, threadref
*ref
)
7586 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
7588 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
7592 threadlist_test_cmd (char *cmd
, int tty
)
7595 threadref nextthread
;
7596 int done
, result_count
;
7597 threadref threadlist
[3];
7599 printf_filtered ("Remote Threadlist test\n");
7600 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
7601 &result_count
, &threadlist
[0]))
7602 printf_filtered ("FAIL: threadlist test\n");
7605 threadref
*scan
= threadlist
;
7606 threadref
*limit
= scan
+ result_count
;
7608 while (scan
< limit
)
7609 output_threadid (" thread ", scan
++);
7614 display_thread_info (struct gdb_ext_thread_info
*info
)
7616 output_threadid ("Threadid: ", &info
->threadid
);
7617 printf_filtered ("Name: %s\n ", info
->shortname
);
7618 printf_filtered ("State: %s\n", info
->display
);
7619 printf_filtered ("other: %s\n\n", info
->more_display
);
7623 get_and_display_threadinfo (threadref
*ref
)
7627 struct gdb_ext_thread_info threadinfo
;
7629 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
7630 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
7631 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
7632 display_thread_info (&threadinfo
);
7637 threadinfo_test_cmd (char *cmd
, int tty
)
7639 int athread
= SAMPLE_THREAD
;
7643 int_to_threadref (&thread
, athread
);
7644 printf_filtered ("Remote Threadinfo test\n");
7645 if (!get_and_display_threadinfo (&thread
))
7646 printf_filtered ("FAIL cannot get thread info\n");
7650 thread_display_step (threadref
*ref
, void *context
)
7652 /* output_threadid(" threadstep ",ref); *//* simple test */
7653 return get_and_display_threadinfo (ref
);
7657 threadlist_update_test_cmd (char *cmd
, int tty
)
7659 printf_filtered ("Remote Threadlist update test\n");
7660 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
7664 init_remote_threadtests (void)
7666 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
7667 Fetch and print the remote list of thread identifiers, one pkt only"));
7668 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
7669 _("Fetch and display info about one thread"));
7670 add_com ("tset", class_obscure
, threadset_test_cmd
,
7671 _("Test setting to a different thread"));
7672 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
7673 _("Iterate through updating all remote thread info"));
7674 add_com ("talive", class_obscure
, threadalive_test
,
7675 _(" Remote thread alive test "));
7680 /* Convert a thread ID to a string. Returns the string in a static
7684 remote_pid_to_str (ptid_t ptid
)
7686 static char buf
[64];
7687 struct remote_state
*rs
= get_remote_state ();
7689 if (ptid_equal (magic_null_ptid
, ptid
))
7691 xsnprintf (buf
, sizeof buf
, "Thread <main>");
7694 else if (remote_multi_process_p (rs
)
7695 && ptid_get_tid (ptid
) != 0 && ptid_get_pid (ptid
) != 0)
7697 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
7698 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
7701 else if (ptid_get_tid (ptid
) != 0)
7703 xsnprintf (buf
, sizeof buf
, "Thread %ld",
7704 ptid_get_tid (ptid
));
7708 return normal_pid_to_str (ptid
);
7711 /* Get the address of the thread local variable in OBJFILE which is
7712 stored at OFFSET within the thread local storage for thread PTID. */
7715 remote_get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
7717 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
7719 struct remote_state
*rs
= get_remote_state ();
7721 char *endp
= rs
->buf
+ get_remote_packet_size ();
7722 enum packet_result result
;
7724 strcpy (p
, "qGetTLSAddr:");
7726 p
= write_ptid (p
, endp
, ptid
);
7728 p
+= hexnumstr (p
, offset
);
7730 p
+= hexnumstr (p
, lm
);
7734 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7735 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
7736 if (result
== PACKET_OK
)
7740 unpack_varlen_hex (rs
->buf
, &result
);
7743 else if (result
== PACKET_UNKNOWN
)
7744 throw_error (TLS_GENERIC_ERROR
,
7745 _("Remote target doesn't support qGetTLSAddr packet"));
7747 throw_error (TLS_GENERIC_ERROR
,
7748 _("Remote target failed to process qGetTLSAddr request"));
7751 throw_error (TLS_GENERIC_ERROR
,
7752 _("TLS not supported or disabled on this target"));
7757 /* Support for inferring a target description based on the current
7758 architecture and the size of a 'g' packet. While the 'g' packet
7759 can have any size (since optional registers can be left off the
7760 end), some sizes are easily recognizable given knowledge of the
7761 approximate architecture. */
7763 struct remote_g_packet_guess
7766 const struct target_desc
*tdesc
;
7768 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
7769 DEF_VEC_O(remote_g_packet_guess_s
);
7771 struct remote_g_packet_data
7773 VEC(remote_g_packet_guess_s
) *guesses
;
7776 static struct gdbarch_data
*remote_g_packet_data_handle
;
7779 remote_g_packet_data_init (struct obstack
*obstack
)
7781 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
7785 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
7786 const struct target_desc
*tdesc
)
7788 struct remote_g_packet_data
*data
7789 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
7790 struct remote_g_packet_guess new_guess
, *guess
;
7793 gdb_assert (tdesc
!= NULL
);
7796 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
7798 if (guess
->bytes
== bytes
)
7799 internal_error (__FILE__
, __LINE__
,
7800 "Duplicate g packet description added for size %d",
7803 new_guess
.bytes
= bytes
;
7804 new_guess
.tdesc
= tdesc
;
7805 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
7808 static const struct target_desc
*
7809 remote_read_description (struct target_ops
*target
)
7811 struct remote_g_packet_data
*data
7812 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
7814 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
7816 struct remote_g_packet_guess
*guess
;
7818 int bytes
= send_g_packet ();
7821 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
7823 if (guess
->bytes
== bytes
)
7824 return guess
->tdesc
;
7826 /* We discard the g packet. A minor optimization would be to
7827 hold on to it, and fill the register cache once we have selected
7828 an architecture, but it's too tricky to do safely. */
7834 /* Remote file transfer support. This is host-initiated I/O, not
7835 target-initiated; for target-initiated, see remote-fileio.c. */
7837 /* If *LEFT is at least the length of STRING, copy STRING to
7838 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7839 decrease *LEFT. Otherwise raise an error. */
7842 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
7844 int len
= strlen (string
);
7847 error (_("Packet too long for target."));
7849 memcpy (*buffer
, string
, len
);
7853 /* NUL-terminate the buffer as a convenience, if there is
7859 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
7860 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7861 decrease *LEFT. Otherwise raise an error. */
7864 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
7867 if (2 * len
> *left
)
7868 error (_("Packet too long for target."));
7870 bin2hex (bytes
, *buffer
, len
);
7874 /* NUL-terminate the buffer as a convenience, if there is
7880 /* If *LEFT is large enough, convert VALUE to hex and add it to
7881 *BUFFER, update *BUFFER to point to the new end of the buffer, and
7882 decrease *LEFT. Otherwise raise an error. */
7885 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
7887 int len
= hexnumlen (value
);
7890 error (_("Packet too long for target."));
7892 hexnumstr (*buffer
, value
);
7896 /* NUL-terminate the buffer as a convenience, if there is
7902 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
7903 value, *REMOTE_ERRNO to the remote error number or zero if none
7904 was included, and *ATTACHMENT to point to the start of the annex
7905 if any. The length of the packet isn't needed here; there may
7906 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
7908 Return 0 if the packet could be parsed, -1 if it could not. If
7909 -1 is returned, the other variables may not be initialized. */
7912 remote_hostio_parse_result (char *buffer
, int *retcode
,
7913 int *remote_errno
, char **attachment
)
7920 if (buffer
[0] != 'F')
7924 *retcode
= strtol (&buffer
[1], &p
, 16);
7925 if (errno
!= 0 || p
== &buffer
[1])
7928 /* Check for ",errno". */
7932 *remote_errno
= strtol (p
+ 1, &p2
, 16);
7933 if (errno
!= 0 || p
+ 1 == p2
)
7938 /* Check for ";attachment". If there is no attachment, the
7939 packet should end here. */
7942 *attachment
= p
+ 1;
7945 else if (*p
== '\0')
7951 /* Send a prepared I/O packet to the target and read its response.
7952 The prepared packet is in the global RS->BUF before this function
7953 is called, and the answer is there when we return.
7955 COMMAND_BYTES is the length of the request to send, which may include
7956 binary data. WHICH_PACKET is the packet configuration to check
7957 before attempting a packet. If an error occurs, *REMOTE_ERRNO
7958 is set to the error number and -1 is returned. Otherwise the value
7959 returned by the function is returned.
7961 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
7962 attachment is expected; an error will be reported if there's a
7963 mismatch. If one is found, *ATTACHMENT will be set to point into
7964 the packet buffer and *ATTACHMENT_LEN will be set to the
7965 attachment's length. */
7968 remote_hostio_send_command (int command_bytes
, int which_packet
,
7969 int *remote_errno
, char **attachment
,
7970 int *attachment_len
)
7972 struct remote_state
*rs
= get_remote_state ();
7973 int ret
, bytes_read
;
7974 char *attachment_tmp
;
7977 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
7979 *remote_errno
= FILEIO_ENOSYS
;
7983 putpkt_binary (rs
->buf
, command_bytes
);
7984 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
7986 /* If it timed out, something is wrong. Don't try to parse the
7990 *remote_errno
= FILEIO_EINVAL
;
7994 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
7997 *remote_errno
= FILEIO_EINVAL
;
7999 case PACKET_UNKNOWN
:
8000 *remote_errno
= FILEIO_ENOSYS
;
8006 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
8009 *remote_errno
= FILEIO_EINVAL
;
8013 /* Make sure we saw an attachment if and only if we expected one. */
8014 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
8015 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
8017 *remote_errno
= FILEIO_EINVAL
;
8021 /* If an attachment was found, it must point into the packet buffer;
8022 work out how many bytes there were. */
8023 if (attachment_tmp
!= NULL
)
8025 *attachment
= attachment_tmp
;
8026 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
8032 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
8033 remote file descriptor, or -1 if an error occurs (and set
8037 remote_hostio_open (const char *filename
, int flags
, int mode
,
8040 struct remote_state
*rs
= get_remote_state ();
8042 int left
= get_remote_packet_size () - 1;
8044 remote_buffer_add_string (&p
, &left
, "vFile:open:");
8046 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
8048 remote_buffer_add_string (&p
, &left
, ",");
8050 remote_buffer_add_int (&p
, &left
, flags
);
8051 remote_buffer_add_string (&p
, &left
, ",");
8053 remote_buffer_add_int (&p
, &left
, mode
);
8055 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
8056 remote_errno
, NULL
, NULL
);
8059 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
8060 Return the number of bytes written, or -1 if an error occurs (and
8061 set *REMOTE_ERRNO). */
8064 remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
8065 ULONGEST offset
, int *remote_errno
)
8067 struct remote_state
*rs
= get_remote_state ();
8069 int left
= get_remote_packet_size ();
8072 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
8074 remote_buffer_add_int (&p
, &left
, fd
);
8075 remote_buffer_add_string (&p
, &left
, ",");
8077 remote_buffer_add_int (&p
, &left
, offset
);
8078 remote_buffer_add_string (&p
, &left
, ",");
8080 p
+= remote_escape_output (write_buf
, len
, p
, &out_len
,
8081 get_remote_packet_size () - (p
- rs
->buf
));
8083 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
8084 remote_errno
, NULL
, NULL
);
8087 /* Read up to LEN bytes FD on the remote target into READ_BUF
8088 Return the number of bytes read, or -1 if an error occurs (and
8089 set *REMOTE_ERRNO). */
8092 remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
8093 ULONGEST offset
, int *remote_errno
)
8095 struct remote_state
*rs
= get_remote_state ();
8098 int left
= get_remote_packet_size ();
8099 int ret
, attachment_len
;
8102 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
8104 remote_buffer_add_int (&p
, &left
, fd
);
8105 remote_buffer_add_string (&p
, &left
, ",");
8107 remote_buffer_add_int (&p
, &left
, len
);
8108 remote_buffer_add_string (&p
, &left
, ",");
8110 remote_buffer_add_int (&p
, &left
, offset
);
8112 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
8113 remote_errno
, &attachment
,
8119 read_len
= remote_unescape_input (attachment
, attachment_len
,
8121 if (read_len
!= ret
)
8122 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
8127 /* Close FD on the remote target. Return 0, or -1 if an error occurs
8128 (and set *REMOTE_ERRNO). */
8131 remote_hostio_close (int fd
, int *remote_errno
)
8133 struct remote_state
*rs
= get_remote_state ();
8135 int left
= get_remote_packet_size () - 1;
8137 remote_buffer_add_string (&p
, &left
, "vFile:close:");
8139 remote_buffer_add_int (&p
, &left
, fd
);
8141 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
8142 remote_errno
, NULL
, NULL
);
8145 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
8146 occurs (and set *REMOTE_ERRNO). */
8149 remote_hostio_unlink (const char *filename
, int *remote_errno
)
8151 struct remote_state
*rs
= get_remote_state ();
8153 int left
= get_remote_packet_size () - 1;
8155 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
8157 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
8160 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
8161 remote_errno
, NULL
, NULL
);
8165 remote_fileio_errno_to_host (int errnum
)
8189 case FILEIO_ENOTDIR
:
8209 case FILEIO_ENAMETOOLONG
:
8210 return ENAMETOOLONG
;
8216 remote_hostio_error (int errnum
)
8218 int host_error
= remote_fileio_errno_to_host (errnum
);
8220 if (host_error
== -1)
8221 error (_("Unknown remote I/O error %d"), errnum
);
8223 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
8227 remote_hostio_close_cleanup (void *opaque
)
8229 int fd
= *(int *) opaque
;
8232 remote_hostio_close (fd
, &remote_errno
);
8237 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
8239 const char *filename
= bfd_get_filename (abfd
);
8240 int fd
, remote_errno
;
8243 gdb_assert (remote_filename_p (filename
));
8245 fd
= remote_hostio_open (filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
8248 errno
= remote_fileio_errno_to_host (remote_errno
);
8249 bfd_set_error (bfd_error_system_call
);
8253 stream
= xmalloc (sizeof (int));
8259 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
8261 int fd
= *(int *)stream
;
8266 /* Ignore errors on close; these may happen if the remote
8267 connection was already torn down. */
8268 remote_hostio_close (fd
, &remote_errno
);
8274 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
8275 file_ptr nbytes
, file_ptr offset
)
8277 int fd
= *(int *)stream
;
8279 file_ptr pos
, bytes
;
8282 while (nbytes
> pos
)
8284 bytes
= remote_hostio_pread (fd
, (char *)buf
+ pos
, nbytes
- pos
,
8285 offset
+ pos
, &remote_errno
);
8287 /* Success, but no bytes, means end-of-file. */
8291 errno
= remote_fileio_errno_to_host (remote_errno
);
8292 bfd_set_error (bfd_error_system_call
);
8303 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
8305 /* FIXME: We should probably implement remote_hostio_stat. */
8306 sb
->st_size
= INT_MAX
;
8311 remote_filename_p (const char *filename
)
8313 return strncmp (filename
, "remote:", 7) == 0;
8317 remote_bfd_open (const char *remote_file
, const char *target
)
8319 return bfd_openr_iovec (remote_file
, target
,
8320 remote_bfd_iovec_open
, NULL
,
8321 remote_bfd_iovec_pread
,
8322 remote_bfd_iovec_close
,
8323 remote_bfd_iovec_stat
);
8327 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
8329 struct cleanup
*back_to
, *close_cleanup
;
8330 int retcode
, fd
, remote_errno
, bytes
, io_size
;
8333 int bytes_in_buffer
;
8338 error (_("command can only be used with remote target"));
8340 file
= fopen (local_file
, "rb");
8342 perror_with_name (local_file
);
8343 back_to
= make_cleanup_fclose (file
);
8345 fd
= remote_hostio_open (remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
8347 0700, &remote_errno
);
8349 remote_hostio_error (remote_errno
);
8351 /* Send up to this many bytes at once. They won't all fit in the
8352 remote packet limit, so we'll transfer slightly fewer. */
8353 io_size
= get_remote_packet_size ();
8354 buffer
= xmalloc (io_size
);
8355 make_cleanup (xfree
, buffer
);
8357 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
8359 bytes_in_buffer
= 0;
8362 while (bytes_in_buffer
|| !saw_eof
)
8366 bytes
= fread (buffer
+ bytes_in_buffer
, 1, io_size
- bytes_in_buffer
,
8371 error (_("Error reading %s."), local_file
);
8374 /* EOF. Unless there is something still in the
8375 buffer from the last iteration, we are done. */
8377 if (bytes_in_buffer
== 0)
8385 bytes
+= bytes_in_buffer
;
8386 bytes_in_buffer
= 0;
8388 retcode
= remote_hostio_pwrite (fd
, buffer
, bytes
, offset
, &remote_errno
);
8391 remote_hostio_error (remote_errno
);
8392 else if (retcode
== 0)
8393 error (_("Remote write of %d bytes returned 0!"), bytes
);
8394 else if (retcode
< bytes
)
8396 /* Short write. Save the rest of the read data for the next
8398 bytes_in_buffer
= bytes
- retcode
;
8399 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
8405 discard_cleanups (close_cleanup
);
8406 if (remote_hostio_close (fd
, &remote_errno
))
8407 remote_hostio_error (remote_errno
);
8410 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
8411 do_cleanups (back_to
);
8415 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
8417 struct cleanup
*back_to
, *close_cleanup
;
8418 int retcode
, fd
, remote_errno
, bytes
, io_size
;
8424 error (_("command can only be used with remote target"));
8426 fd
= remote_hostio_open (remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
8428 remote_hostio_error (remote_errno
);
8430 file
= fopen (local_file
, "wb");
8432 perror_with_name (local_file
);
8433 back_to
= make_cleanup_fclose (file
);
8435 /* Send up to this many bytes at once. They won't all fit in the
8436 remote packet limit, so we'll transfer slightly fewer. */
8437 io_size
= get_remote_packet_size ();
8438 buffer
= xmalloc (io_size
);
8439 make_cleanup (xfree
, buffer
);
8441 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
8446 bytes
= remote_hostio_pread (fd
, buffer
, io_size
, offset
, &remote_errno
);
8448 /* Success, but no bytes, means end-of-file. */
8451 remote_hostio_error (remote_errno
);
8455 bytes
= fwrite (buffer
, 1, bytes
, file
);
8457 perror_with_name (local_file
);
8460 discard_cleanups (close_cleanup
);
8461 if (remote_hostio_close (fd
, &remote_errno
))
8462 remote_hostio_error (remote_errno
);
8465 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
8466 do_cleanups (back_to
);
8470 remote_file_delete (const char *remote_file
, int from_tty
)
8472 int retcode
, remote_errno
;
8475 error (_("command can only be used with remote target"));
8477 retcode
= remote_hostio_unlink (remote_file
, &remote_errno
);
8479 remote_hostio_error (remote_errno
);
8482 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
8486 remote_put_command (char *args
, int from_tty
)
8488 struct cleanup
*back_to
;
8492 error_no_arg (_("file to put"));
8494 argv
= gdb_buildargv (args
);
8495 back_to
= make_cleanup_freeargv (argv
);
8496 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
8497 error (_("Invalid parameters to remote put"));
8499 remote_file_put (argv
[0], argv
[1], from_tty
);
8501 do_cleanups (back_to
);
8505 remote_get_command (char *args
, int from_tty
)
8507 struct cleanup
*back_to
;
8511 error_no_arg (_("file to get"));
8513 argv
= gdb_buildargv (args
);
8514 back_to
= make_cleanup_freeargv (argv
);
8515 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
8516 error (_("Invalid parameters to remote get"));
8518 remote_file_get (argv
[0], argv
[1], from_tty
);
8520 do_cleanups (back_to
);
8524 remote_delete_command (char *args
, int from_tty
)
8526 struct cleanup
*back_to
;
8530 error_no_arg (_("file to delete"));
8532 argv
= gdb_buildargv (args
);
8533 back_to
= make_cleanup_freeargv (argv
);
8534 if (argv
[0] == NULL
|| argv
[1] != NULL
)
8535 error (_("Invalid parameters to remote delete"));
8537 remote_file_delete (argv
[0], from_tty
);
8539 do_cleanups (back_to
);
8543 remote_command (char *args
, int from_tty
)
8545 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
8548 static int remote_target_can_reverse
= 1;
8551 remote_can_execute_reverse (void)
8553 return remote_target_can_reverse
;
8557 remote_supports_non_stop (void)
8563 remote_supports_multi_process (void)
8565 struct remote_state
*rs
= get_remote_state ();
8566 return remote_multi_process_p (rs
);
8570 init_remote_ops (void)
8572 remote_ops
.to_shortname
= "remote";
8573 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
8575 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
8576 Specify the serial device it is connected to\n\
8577 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
8578 remote_ops
.to_open
= remote_open
;
8579 remote_ops
.to_close
= remote_close
;
8580 remote_ops
.to_detach
= remote_detach
;
8581 remote_ops
.to_disconnect
= remote_disconnect
;
8582 remote_ops
.to_resume
= remote_resume
;
8583 remote_ops
.to_wait
= remote_wait
;
8584 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
8585 remote_ops
.to_store_registers
= remote_store_registers
;
8586 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
8587 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
8588 remote_ops
.to_files_info
= remote_files_info
;
8589 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
8590 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
8591 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
8592 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
8593 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
8594 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
8595 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
8596 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
8597 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
8598 remote_ops
.to_kill
= remote_kill
;
8599 remote_ops
.to_load
= generic_load
;
8600 remote_ops
.to_mourn_inferior
= remote_mourn
;
8601 remote_ops
.to_thread_alive
= remote_thread_alive
;
8602 remote_ops
.to_find_new_threads
= remote_threads_info
;
8603 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
8604 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
8605 remote_ops
.to_stop
= remote_stop
;
8606 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
8607 remote_ops
.to_rcmd
= remote_rcmd
;
8608 remote_ops
.to_log_command
= serial_log_command
;
8609 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
8610 remote_ops
.to_stratum
= process_stratum
;
8611 remote_ops
.to_has_all_memory
= 1;
8612 remote_ops
.to_has_memory
= 1;
8613 remote_ops
.to_has_stack
= 1;
8614 remote_ops
.to_has_registers
= 1;
8615 remote_ops
.to_has_execution
= 1;
8616 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
8617 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
8618 remote_ops
.to_magic
= OPS_MAGIC
;
8619 remote_ops
.to_memory_map
= remote_memory_map
;
8620 remote_ops
.to_flash_erase
= remote_flash_erase
;
8621 remote_ops
.to_flash_done
= remote_flash_done
;
8622 remote_ops
.to_read_description
= remote_read_description
;
8623 remote_ops
.to_search_memory
= remote_search_memory
;
8624 remote_ops
.to_can_async_p
= remote_can_async_p
;
8625 remote_ops
.to_is_async_p
= remote_is_async_p
;
8626 remote_ops
.to_async
= remote_async
;
8627 remote_ops
.to_async_mask
= remote_async_mask
;
8628 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
8629 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
8630 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
8631 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
8634 /* Set up the extended remote vector by making a copy of the standard
8635 remote vector and adding to it. */
8638 init_extended_remote_ops (void)
8640 extended_remote_ops
= remote_ops
;
8642 extended_remote_ops
.to_shortname
= "extended-remote";
8643 extended_remote_ops
.to_longname
=
8644 "Extended remote serial target in gdb-specific protocol";
8645 extended_remote_ops
.to_doc
=
8646 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
8647 Specify the serial device it is connected to (e.g. /dev/ttya).";
8648 extended_remote_ops
.to_open
= extended_remote_open
;
8649 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
8650 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
8651 extended_remote_ops
.to_detach
= extended_remote_detach
;
8652 extended_remote_ops
.to_attach
= extended_remote_attach
;
8653 extended_remote_ops
.to_kill
= extended_remote_kill
;
8657 remote_can_async_p (void)
8659 if (!target_async_permitted
)
8660 /* We only enable async when the user specifically asks for it. */
8663 /* We're async whenever the serial device is. */
8664 return remote_async_mask_value
&& serial_can_async_p (remote_desc
);
8668 remote_is_async_p (void)
8670 if (!target_async_permitted
)
8671 /* We only enable async when the user specifically asks for it. */
8674 /* We're async whenever the serial device is. */
8675 return remote_async_mask_value
&& serial_is_async_p (remote_desc
);
8678 /* Pass the SERIAL event on and up to the client. One day this code
8679 will be able to delay notifying the client of an event until the
8680 point where an entire packet has been received. */
8682 static void (*async_client_callback
) (enum inferior_event_type event_type
,
8684 static void *async_client_context
;
8685 static serial_event_ftype remote_async_serial_handler
;
8688 remote_async_serial_handler (struct serial
*scb
, void *context
)
8690 /* Don't propogate error information up to the client. Instead let
8691 the client find out about the error by querying the target. */
8692 async_client_callback (INF_REG_EVENT
, async_client_context
);
8696 remote_async_inferior_event_handler (gdb_client_data data
)
8698 inferior_event_handler (INF_REG_EVENT
, NULL
);
8702 remote_async_get_pending_events_handler (gdb_client_data data
)
8704 remote_get_pending_stop_replies ();
8708 remote_async (void (*callback
) (enum inferior_event_type event_type
,
8709 void *context
), void *context
)
8711 if (remote_async_mask_value
== 0)
8712 internal_error (__FILE__
, __LINE__
,
8713 _("Calling remote_async when async is masked"));
8715 if (callback
!= NULL
)
8717 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
8718 async_client_callback
= callback
;
8719 async_client_context
= context
;
8722 serial_async (remote_desc
, NULL
, NULL
);
8726 remote_async_mask (int new_mask
)
8728 int curr_mask
= remote_async_mask_value
;
8729 remote_async_mask_value
= new_mask
;
8734 set_remote_cmd (char *args
, int from_tty
)
8736 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
8740 show_remote_cmd (char *args
, int from_tty
)
8742 /* We can't just use cmd_show_list here, because we want to skip
8743 the redundant "show remote Z-packet" and the legacy aliases. */
8744 struct cleanup
*showlist_chain
;
8745 struct cmd_list_element
*list
= remote_show_cmdlist
;
8747 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
8748 for (; list
!= NULL
; list
= list
->next
)
8749 if (strcmp (list
->name
, "Z-packet") == 0)
8751 else if (list
->type
== not_set_cmd
)
8752 /* Alias commands are exactly like the original, except they
8753 don't have the normal type. */
8757 struct cleanup
*option_chain
8758 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
8759 ui_out_field_string (uiout
, "name", list
->name
);
8760 ui_out_text (uiout
, ": ");
8761 if (list
->type
== show_cmd
)
8762 do_setshow_command ((char *) NULL
, from_tty
, list
);
8764 cmd_func (list
, NULL
, from_tty
);
8765 /* Close the tuple. */
8766 do_cleanups (option_chain
);
8769 /* Close the tuple. */
8770 do_cleanups (showlist_chain
);
8774 /* Function to be called whenever a new objfile (shlib) is detected. */
8776 remote_new_objfile (struct objfile
*objfile
)
8778 if (remote_desc
!= 0) /* Have a remote connection. */
8779 remote_check_symbols (objfile
);
8783 _initialize_remote (void)
8785 struct remote_state
*rs
;
8787 /* architecture specific data */
8788 remote_gdbarch_data_handle
=
8789 gdbarch_data_register_post_init (init_remote_state
);
8790 remote_g_packet_data_handle
=
8791 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
8793 /* Initialize the per-target state. At the moment there is only one
8794 of these, not one per target. Only one target is active at a
8795 time. The default buffer size is unimportant; it will be expanded
8796 whenever a larger buffer is needed. */
8797 rs
= get_remote_state_raw ();
8799 rs
->buf
= xmalloc (rs
->buf_size
);
8802 add_target (&remote_ops
);
8804 init_extended_remote_ops ();
8805 add_target (&extended_remote_ops
);
8807 /* Hook into new objfile notification. */
8808 observer_attach_new_objfile (remote_new_objfile
);
8810 /* Set up signal handlers. */
8811 sigint_remote_token
=
8812 create_async_signal_handler (async_remote_interrupt
, NULL
);
8813 sigint_remote_twice_token
=
8814 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
8817 init_remote_threadtests ();
8820 /* set/show remote ... */
8822 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
8823 Remote protocol specific variables\n\
8824 Configure various remote-protocol specific variables such as\n\
8825 the packets being used"),
8826 &remote_set_cmdlist
, "set remote ",
8827 0 /* allow-unknown */, &setlist
);
8828 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
8829 Remote protocol specific variables\n\
8830 Configure various remote-protocol specific variables such as\n\
8831 the packets being used"),
8832 &remote_show_cmdlist
, "show remote ",
8833 0 /* allow-unknown */, &showlist
);
8835 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
8836 Compare section data on target to the exec file.\n\
8837 Argument is a single section name (default: all loaded sections)."),
8840 add_cmd ("packet", class_maintenance
, packet_command
, _("\
8841 Send an arbitrary packet to a remote target.\n\
8842 maintenance packet TEXT\n\
8843 If GDB is talking to an inferior via the GDB serial protocol, then\n\
8844 this command sends the string TEXT to the inferior, and displays the\n\
8845 response packet. GDB supplies the initial `$' character, and the\n\
8846 terminating `#' character and checksum."),
8849 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
8850 Set whether to send break if interrupted."), _("\
8851 Show whether to send break if interrupted."), _("\
8852 If set, a break, instead of a cntrl-c, is sent to the remote target."),
8853 NULL
, NULL
, /* FIXME: i18n: Whether to send break if interrupted is %s. */
8854 &setlist
, &showlist
);
8856 /* Install commands for configuring memory read/write packets. */
8858 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
8859 Set the maximum number of bytes per memory write packet (deprecated)."),
8861 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
8862 Show the maximum number of bytes per memory write packet (deprecated)."),
8864 add_cmd ("memory-write-packet-size", no_class
,
8865 set_memory_write_packet_size
, _("\
8866 Set the maximum number of bytes per memory-write packet.\n\
8867 Specify the number of bytes in a packet or 0 (zero) for the\n\
8868 default packet size. The actual limit is further reduced\n\
8869 dependent on the target. Specify ``fixed'' to disable the\n\
8870 further restriction and ``limit'' to enable that restriction."),
8871 &remote_set_cmdlist
);
8872 add_cmd ("memory-read-packet-size", no_class
,
8873 set_memory_read_packet_size
, _("\
8874 Set the maximum number of bytes per memory-read packet.\n\
8875 Specify the number of bytes in a packet or 0 (zero) for the\n\
8876 default packet size. The actual limit is further reduced\n\
8877 dependent on the target. Specify ``fixed'' to disable the\n\
8878 further restriction and ``limit'' to enable that restriction."),
8879 &remote_set_cmdlist
);
8880 add_cmd ("memory-write-packet-size", no_class
,
8881 show_memory_write_packet_size
,
8882 _("Show the maximum number of bytes per memory-write packet."),
8883 &remote_show_cmdlist
);
8884 add_cmd ("memory-read-packet-size", no_class
,
8885 show_memory_read_packet_size
,
8886 _("Show the maximum number of bytes per memory-read packet."),
8887 &remote_show_cmdlist
);
8889 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
8890 &remote_hw_watchpoint_limit
, _("\
8891 Set the maximum number of target hardware watchpoints."), _("\
8892 Show the maximum number of target hardware watchpoints."), _("\
8893 Specify a negative limit for unlimited."),
8894 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
8895 &remote_set_cmdlist
, &remote_show_cmdlist
);
8896 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
8897 &remote_hw_breakpoint_limit
, _("\
8898 Set the maximum number of target hardware breakpoints."), _("\
8899 Show the maximum number of target hardware breakpoints."), _("\
8900 Specify a negative limit for unlimited."),
8901 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
8902 &remote_set_cmdlist
, &remote_show_cmdlist
);
8904 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
8905 &remote_address_size
, _("\
8906 Set the maximum size of the address (in bits) in a memory packet."), _("\
8907 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
8909 NULL
, /* FIXME: i18n: */
8910 &setlist
, &showlist
);
8912 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
8913 "X", "binary-download", 1);
8915 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
8916 "vCont", "verbose-resume", 0);
8918 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
8919 "QPassSignals", "pass-signals", 0);
8921 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
8922 "qSymbol", "symbol-lookup", 0);
8924 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
8925 "P", "set-register", 1);
8927 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
8928 "p", "fetch-register", 1);
8930 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
8931 "Z0", "software-breakpoint", 0);
8933 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
8934 "Z1", "hardware-breakpoint", 0);
8936 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
8937 "Z2", "write-watchpoint", 0);
8939 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
8940 "Z3", "read-watchpoint", 0);
8942 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
8943 "Z4", "access-watchpoint", 0);
8945 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
8946 "qXfer:auxv:read", "read-aux-vector", 0);
8948 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
8949 "qXfer:features:read", "target-features", 0);
8951 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
8952 "qXfer:libraries:read", "library-info", 0);
8954 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
8955 "qXfer:memory-map:read", "memory-map", 0);
8957 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
8958 "qXfer:spu:read", "read-spu-object", 0);
8960 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
8961 "qXfer:spu:write", "write-spu-object", 0);
8963 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
8964 "qGetTLSAddr", "get-thread-local-storage-address",
8967 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
8968 "qSupported", "supported-packets", 0);
8970 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
8971 "qSearch:memory", "search-memory", 0);
8973 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
8974 "vFile:open", "hostio-open", 0);
8976 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
8977 "vFile:pread", "hostio-pread", 0);
8979 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
8980 "vFile:pwrite", "hostio-pwrite", 0);
8982 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
8983 "vFile:close", "hostio-close", 0);
8985 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
8986 "vFile:unlink", "hostio-unlink", 0);
8988 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
8989 "vAttach", "attach", 0);
8991 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
8994 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
8995 "QStartNoAckMode", "noack", 0);
8997 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
8998 "vKill", "kill", 0);
9000 /* Keep the old ``set remote Z-packet ...'' working. Each individual
9001 Z sub-packet has its own set and show commands, but users may
9002 have sets to this variable in their .gdbinit files (or in their
9004 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
9005 &remote_Z_packet_detect
, _("\
9006 Set use of remote protocol `Z' packets"), _("\
9007 Show use of remote protocol `Z' packets "), _("\
9008 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
9010 set_remote_protocol_Z_packet_cmd
,
9011 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
9012 &remote_set_cmdlist
, &remote_show_cmdlist
);
9014 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
9015 Manipulate files on the remote system\n\
9016 Transfer files to and from the remote target system."),
9017 &remote_cmdlist
, "remote ",
9018 0 /* allow-unknown */, &cmdlist
);
9020 add_cmd ("put", class_files
, remote_put_command
,
9021 _("Copy a local file to the remote system."),
9024 add_cmd ("get", class_files
, remote_get_command
,
9025 _("Copy a remote file to the local system."),
9028 add_cmd ("delete", class_files
, remote_delete_command
,
9029 _("Delete a remote file."),
9032 remote_exec_file
= xstrdup ("");
9033 add_setshow_string_noescape_cmd ("exec-file", class_files
,
9034 &remote_exec_file
, _("\
9035 Set the remote pathname for \"run\""), _("\
9036 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
9037 &remote_set_cmdlist
, &remote_show_cmdlist
);
9039 /* Eventually initialize fileio. See fileio.c */
9040 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
9042 /* Take advantage of the fact that the LWP field is not used, to tag
9043 special ptids with it set to != 0. */
9044 magic_null_ptid
= ptid_build (42000, 1, -1);
9045 not_sent_ptid
= ptid_build (42000, 1, -2);
9046 any_thread_ptid
= ptid_build (42000, 1, 0);