1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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 2 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, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* See the GDB User Guide for details of the GDB remote protocol. */
27 #include "gdb_string.h"
33 #include "exceptions.h"
35 /*#include "terminal.h" */
38 #include "gdb-stabs.h"
39 #include "gdbthread.h"
43 #include "gdb_assert.h"
46 #include "cli/cli-decode.h"
47 #include "cli/cli-setshow.h"
52 #include "event-loop.h"
53 #include "event-top.h"
59 #include "gdbcore.h" /* for exec_bfd */
61 #include "remote-fileio.h"
63 /* Prototypes for local functions. */
64 static void cleanup_sigint_signal_handler (void *dummy
);
65 static void initialize_sigint_signal_handler (void);
66 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
68 static void handle_remote_sigint (int);
69 static void handle_remote_sigint_twice (int);
70 static void async_remote_interrupt (gdb_client_data
);
71 void async_remote_interrupt_twice (gdb_client_data
);
73 static void build_remote_gdbarch_data (void);
75 static void remote_files_info (struct target_ops
*ignore
);
77 static void remote_prepare_to_store (void);
79 static void remote_fetch_registers (int regno
);
81 static void remote_resume (ptid_t ptid
, int step
,
82 enum target_signal siggnal
);
83 static void remote_async_resume (ptid_t ptid
, int step
,
84 enum target_signal siggnal
);
85 static void remote_open (char *name
, int from_tty
);
86 static void remote_async_open (char *name
, int from_tty
);
88 static void extended_remote_open (char *name
, int from_tty
);
89 static void extended_remote_async_open (char *name
, int from_tty
);
91 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
,
94 static void remote_close (int quitting
);
96 static void remote_store_registers (int regno
);
98 static void remote_mourn (void);
99 static void remote_async_mourn (void);
101 static void extended_remote_restart (void);
103 static void extended_remote_mourn (void);
105 static void remote_mourn_1 (struct target_ops
*);
107 static void remote_send (char **buf
, long *sizeof_buf_p
);
109 static int readchar (int timeout
);
111 static ptid_t
remote_wait (ptid_t ptid
,
112 struct target_waitstatus
*status
);
113 static ptid_t
remote_async_wait (ptid_t ptid
,
114 struct target_waitstatus
*status
);
116 static void remote_kill (void);
117 static void remote_async_kill (void);
119 static int tohex (int nib
);
121 static void remote_detach (char *args
, int from_tty
);
123 static void remote_interrupt (int signo
);
125 static void remote_interrupt_twice (int signo
);
127 static void interrupt_query (void);
129 static void set_thread (int, int);
131 static int remote_thread_alive (ptid_t
);
133 static void get_offsets (void);
135 static void skip_frame (void);
137 static long read_frame (char **buf_p
, long *sizeof_buf
);
139 static int hexnumlen (ULONGEST num
);
141 static void init_remote_ops (void);
143 static void init_extended_remote_ops (void);
145 static void remote_stop (void);
147 static int ishex (int ch
, int *val
);
149 static int stubhex (int ch
);
151 static int hexnumstr (char *, ULONGEST
);
153 static int hexnumnstr (char *, ULONGEST
, int);
155 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
157 static void print_packet (char *);
159 static unsigned long crc32 (unsigned char *, int, unsigned int);
161 static void compare_sections_command (char *, int);
163 static void packet_command (char *, int);
165 static int stub_unpack_int (char *buff
, int fieldlength
);
167 static ptid_t
remote_current_thread (ptid_t oldptid
);
169 static void remote_find_new_threads (void);
171 static void record_currthread (int currthread
);
173 static int fromhex (int a
);
175 static int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
177 static int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
179 static int putpkt_binary (char *buf
, int cnt
);
181 static void check_binary_download (CORE_ADDR addr
);
183 struct packet_config
;
185 static void show_packet_config_cmd (struct packet_config
*config
);
187 static void update_packet_config (struct packet_config
*config
);
189 static void set_remote_protocol_packet_cmd (char *args
, int from_tty
,
190 struct cmd_list_element
*c
);
192 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
194 struct cmd_list_element
*c
,
197 void _initialize_remote (void);
199 /* For "set remote" and "show remote". */
201 static struct cmd_list_element
*remote_set_cmdlist
;
202 static struct cmd_list_element
*remote_show_cmdlist
;
204 /* Description of the remote protocol state for the currently
205 connected target. This is per-target state, and independent of the
206 selected architecture. */
210 /* A buffer to use for incoming packets, and its current size. The
211 buffer is grown dynamically for larger incoming packets.
212 Outgoing packets may also be constructed in this buffer.
213 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
214 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
220 /* This data could be associated with a target, but we do not always
221 have access to the current target when we need it, so for now it is
222 static. This will be fine for as long as only one target is in use
224 static struct remote_state remote_state
;
226 static struct remote_state
*
227 get_remote_state (void)
229 return &remote_state
;
232 /* Description of the remote protocol for a given architecture. */
236 long offset
; /* Offset into G packet. */
237 long regnum
; /* GDB's internal register number. */
238 LONGEST pnum
; /* Remote protocol register number. */
239 int in_g_packet
; /* Always part of G packet. */
240 /* long size in bytes; == register_size (current_gdbarch, regnum);
242 /* char *name; == REGISTER_NAME (regnum); at present. */
245 struct remote_arch_state
247 /* Description of the remote protocol registers. */
248 long sizeof_g_packet
;
250 /* Description of the remote protocol registers indexed by REGNUM
251 (making an array of NUM_REGS + NUM_PSEUDO_REGS in size). */
252 struct packet_reg
*regs
;
254 /* This is the size (in chars) of the first response to the ``g''
255 packet. It is used as a heuristic when determining the maximum
256 size of memory-read and memory-write packets. A target will
257 typically only reserve a buffer large enough to hold the ``g''
258 packet. The size does not include packet overhead (headers and
260 long actual_register_packet_size
;
262 /* This is the maximum size (in chars) of a non read/write packet.
263 It is also used as a cap on the size of read/write packets. */
264 long remote_packet_size
;
268 /* Handle for retreving the remote protocol data from gdbarch. */
269 static struct gdbarch_data
*remote_gdbarch_data_handle
;
271 static struct remote_arch_state
*
272 get_remote_arch_state (void)
274 return gdbarch_data (current_gdbarch
, remote_gdbarch_data_handle
);
278 init_remote_state (struct gdbarch
*gdbarch
)
281 struct remote_state
*rs
= get_remote_state ();
282 struct remote_arch_state
*rsa
;
284 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
286 rsa
->sizeof_g_packet
= 0;
288 /* Assume a 1:1 regnum<->pnum table. */
289 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
, NUM_REGS
+ NUM_PSEUDO_REGS
,
291 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
293 struct packet_reg
*r
= &rsa
->regs
[regnum
];
296 r
->offset
= DEPRECATED_REGISTER_BYTE (regnum
);
297 r
->in_g_packet
= (regnum
< NUM_REGS
);
298 /* ...name = REGISTER_NAME (regnum); */
300 /* Compute packet size by accumulating the size of all registers. */
301 if (regnum
< NUM_REGS
)
302 rsa
->sizeof_g_packet
+= register_size (current_gdbarch
, regnum
);
305 /* Default maximum number of characters in a packet body. Many
306 remote stubs have a hardwired buffer size of 400 bytes
307 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
308 as the maximum packet-size to ensure that the packet and an extra
309 NUL character can always fit in the buffer. This stops GDB
310 trashing stubs that try to squeeze an extra NUL into what is
311 already a full buffer (As of 1999-12-04 that was most stubs). */
312 rsa
->remote_packet_size
= 400 - 1;
314 /* This one is filled in when a ``g'' packet is received. */
315 rsa
->actual_register_packet_size
= 0;
317 /* Should rsa->sizeof_g_packet needs more space than the
318 default, adjust the size accordingly. Remember that each byte is
319 encoded as two characters. 32 is the overhead for the packet
320 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
321 (``$NN:G...#NN'') is a better guess, the below has been padded a
323 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
324 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
326 /* Make sure that the packet buffer is plenty big enough for
327 this architecture. */
328 if (rs
->buf_size
< rsa
->remote_packet_size
)
330 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
331 rs
->buf
= xmalloc (rs
->buf_size
);
337 /* Return the current allowed size of a remote packet. This is
338 inferred from the current architecture, and should be used to
339 limit the length of outgoing packets. */
341 get_remote_packet_size (void)
343 struct remote_arch_state
*rsa
= get_remote_arch_state ();
345 return rsa
->remote_packet_size
;
348 static struct packet_reg
*
349 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
351 if (regnum
< 0 && regnum
>= NUM_REGS
+ NUM_PSEUDO_REGS
)
355 struct packet_reg
*r
= &rsa
->regs
[regnum
];
356 gdb_assert (r
->regnum
== regnum
);
361 static struct packet_reg
*
362 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
365 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
367 struct packet_reg
*r
= &rsa
->regs
[i
];
374 /* FIXME: graces/2002-08-08: These variables should eventually be
375 bound to an instance of the target object (as in gdbarch-tdep()),
376 when such a thing exists. */
378 /* This is set to the data address of the access causing the target
379 to stop for a watchpoint. */
380 static CORE_ADDR remote_watch_data_address
;
382 /* This is non-zero if target stopped for a watchpoint. */
383 static int remote_stopped_by_watchpoint_p
;
385 static struct target_ops remote_ops
;
387 static struct target_ops extended_remote_ops
;
389 /* Temporary target ops. Just like the remote_ops and
390 extended_remote_ops, but with asynchronous support. */
391 static struct target_ops remote_async_ops
;
393 static struct target_ops extended_async_remote_ops
;
395 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
396 ``forever'' still use the normal timeout mechanism. This is
397 currently used by the ASYNC code to guarentee that target reads
398 during the initial connect always time-out. Once getpkt has been
399 modified to return a timeout indication and, in turn
400 remote_wait()/wait_for_inferior() have gained a timeout parameter
402 static int wait_forever_enabled_p
= 1;
405 /* This variable chooses whether to send a ^C or a break when the user
406 requests program interruption. Although ^C is usually what remote
407 systems expect, and that is the default here, sometimes a break is
408 preferable instead. */
410 static int remote_break
;
412 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
413 remote_open knows that we don't have a file open when the program
415 static struct serial
*remote_desc
= NULL
;
417 /* This variable sets the number of bits in an address that are to be
418 sent in a memory ("M" or "m") packet. Normally, after stripping
419 leading zeros, the entire address would be sent. This variable
420 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
421 initial implementation of remote.c restricted the address sent in
422 memory packets to ``host::sizeof long'' bytes - (typically 32
423 bits). Consequently, for 64 bit targets, the upper 32 bits of an
424 address was never sent. Since fixing this bug may cause a break in
425 some remote targets this variable is principly provided to
426 facilitate backward compatibility. */
428 static int remote_address_size
;
430 /* Tempoary to track who currently owns the terminal. See
431 target_async_terminal_* for more details. */
433 static int remote_async_terminal_ours_p
;
436 /* User configurable variables for the number of characters in a
437 memory read/write packet. MIN (rsa->remote_packet_size,
438 rsa->sizeof_g_packet) is the default. Some targets need smaller
439 values (fifo overruns, et.al.) and some users need larger values
440 (speed up transfers). The variables ``preferred_*'' (the user
441 request), ``current_*'' (what was actually set) and ``forced_*''
442 (Positive - a soft limit, negative - a hard limit). */
444 struct memory_packet_config
451 /* Compute the current size of a read/write packet. Since this makes
452 use of ``actual_register_packet_size'' the computation is dynamic. */
455 get_memory_packet_size (struct memory_packet_config
*config
)
457 struct remote_state
*rs
= get_remote_state ();
458 struct remote_arch_state
*rsa
= get_remote_arch_state ();
460 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
461 law?) that some hosts don't cope very well with large alloca()
462 calls. Eventually the alloca() code will be replaced by calls to
463 xmalloc() and make_cleanups() allowing this restriction to either
464 be lifted or removed. */
465 #ifndef MAX_REMOTE_PACKET_SIZE
466 #define MAX_REMOTE_PACKET_SIZE 16384
468 /* NOTE: 20 ensures we can write at least one byte. */
469 #ifndef MIN_REMOTE_PACKET_SIZE
470 #define MIN_REMOTE_PACKET_SIZE 20
475 if (config
->size
<= 0)
476 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
478 what_they_get
= config
->size
;
482 what_they_get
= get_remote_packet_size ();
483 /* Limit the packet to the size specified by the user. */
485 && what_they_get
> config
->size
)
486 what_they_get
= config
->size
;
487 /* Limit it to the size of the targets ``g'' response. */
488 if ((rsa
->actual_register_packet_size
) > 0
489 && what_they_get
> (rsa
->actual_register_packet_size
))
490 what_they_get
= (rsa
->actual_register_packet_size
);
492 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
493 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
494 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
495 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
497 /* Make sure there is room in the global buffer for this packet
498 (including its trailing NUL byte). */
499 if (rs
->buf_size
< what_they_get
+ 1)
501 rs
->buf_size
= 2 * what_they_get
;
502 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
505 return what_they_get
;
508 /* Update the size of a read/write packet. If they user wants
509 something really big then do a sanity check. */
512 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
514 int fixed_p
= config
->fixed_p
;
515 long size
= config
->size
;
517 error (_("Argument required (integer, `fixed' or `limited')."));
518 else if (strcmp (args
, "hard") == 0
519 || strcmp (args
, "fixed") == 0)
521 else if (strcmp (args
, "soft") == 0
522 || strcmp (args
, "limit") == 0)
527 size
= strtoul (args
, &end
, 0);
529 error (_("Invalid %s (bad syntax)."), config
->name
);
531 /* Instead of explicitly capping the size of a packet to
532 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
533 instead allowed to set the size to something arbitrarily
535 if (size
> MAX_REMOTE_PACKET_SIZE
)
536 error (_("Invalid %s (too large)."), config
->name
);
540 if (fixed_p
&& !config
->fixed_p
)
542 if (! query (_("The target may not be able to correctly handle a %s\n"
543 "of %ld bytes. Change the packet size? "),
545 error (_("Packet size not changed."));
547 /* Update the config. */
548 config
->fixed_p
= fixed_p
;
553 show_memory_packet_size (struct memory_packet_config
*config
)
555 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
557 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
558 get_memory_packet_size (config
));
560 printf_filtered (_("Packets are limited to %ld bytes.\n"),
561 get_memory_packet_size (config
));
564 static struct memory_packet_config memory_write_packet_config
=
566 "memory-write-packet-size",
570 set_memory_write_packet_size (char *args
, int from_tty
)
572 set_memory_packet_size (args
, &memory_write_packet_config
);
576 show_memory_write_packet_size (char *args
, int from_tty
)
578 show_memory_packet_size (&memory_write_packet_config
);
582 get_memory_write_packet_size (void)
584 return get_memory_packet_size (&memory_write_packet_config
);
587 static struct memory_packet_config memory_read_packet_config
=
589 "memory-read-packet-size",
593 set_memory_read_packet_size (char *args
, int from_tty
)
595 set_memory_packet_size (args
, &memory_read_packet_config
);
599 show_memory_read_packet_size (char *args
, int from_tty
)
601 show_memory_packet_size (&memory_read_packet_config
);
605 get_memory_read_packet_size (void)
607 long size
= get_memory_packet_size (&memory_read_packet_config
);
608 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
609 extra buffer size argument before the memory read size can be
610 increased beyond this. */
611 if (size
> get_remote_packet_size ())
612 size
= get_remote_packet_size ();
617 /* Generic configuration support for packets the stub optionally
618 supports. Allows the user to specify the use of the packet as well
619 as allowing GDB to auto-detect support in the remote stub. */
623 PACKET_SUPPORT_UNKNOWN
= 0,
632 enum auto_boolean detect
;
633 enum packet_support support
;
636 /* Analyze a packet's return value and update the packet config
647 update_packet_config (struct packet_config
*config
)
649 switch (config
->detect
)
651 case AUTO_BOOLEAN_TRUE
:
652 config
->support
= PACKET_ENABLE
;
654 case AUTO_BOOLEAN_FALSE
:
655 config
->support
= PACKET_DISABLE
;
657 case AUTO_BOOLEAN_AUTO
:
658 config
->support
= PACKET_SUPPORT_UNKNOWN
;
664 show_packet_config_cmd (struct packet_config
*config
)
666 char *support
= "internal-error";
667 switch (config
->support
)
673 support
= "disabled";
675 case PACKET_SUPPORT_UNKNOWN
:
679 switch (config
->detect
)
681 case AUTO_BOOLEAN_AUTO
:
682 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
683 config
->name
, support
);
685 case AUTO_BOOLEAN_TRUE
:
686 case AUTO_BOOLEAN_FALSE
:
687 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
688 config
->name
, support
);
694 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
695 const char *title
, int legacy
)
702 config
->title
= title
;
703 config
->detect
= AUTO_BOOLEAN_AUTO
;
704 config
->support
= PACKET_SUPPORT_UNKNOWN
;
705 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
707 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
709 /* set/show TITLE-packet {auto,on,off} */
710 cmd_name
= xstrprintf ("%s-packet", title
);
711 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
712 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
713 set_remote_protocol_packet_cmd
,
714 show_remote_protocol_packet_cmd
,
715 &remote_set_cmdlist
, &remote_show_cmdlist
);
716 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
720 legacy_name
= xstrprintf ("%s-packet", name
);
721 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
722 &remote_set_cmdlist
);
723 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
724 &remote_show_cmdlist
);
728 static enum packet_result
729 packet_ok (const char *buf
, struct packet_config
*config
)
733 /* The stub recognized the packet request. Check that the
734 operation succeeded. */
735 switch (config
->support
)
737 case PACKET_SUPPORT_UNKNOWN
:
739 fprintf_unfiltered (gdb_stdlog
,
740 "Packet %s (%s) is supported\n",
741 config
->name
, config
->title
);
742 config
->support
= PACKET_ENABLE
;
745 internal_error (__FILE__
, __LINE__
,
746 _("packet_ok: attempt to use a disabled packet"));
751 if (buf
[0] == 'O' && buf
[1] == 'K' && buf
[2] == '\0')
752 /* "OK" - definitly OK. */
755 && isxdigit (buf
[1]) && isxdigit (buf
[2])
757 /* "Enn" - definitly an error. */
759 /* The packet may or may not be OK. Just assume it is. */
764 /* The stub does not support the packet. */
765 switch (config
->support
)
768 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
769 /* If the stub previously indicated that the packet was
770 supported then there is a protocol error.. */
771 error (_("Protocol error: %s (%s) conflicting enabled responses."),
772 config
->name
, config
->title
);
774 /* The user set it wrong. */
775 error (_("Enabled packet %s (%s) not recognized by stub"),
776 config
->name
, config
->title
);
778 case PACKET_SUPPORT_UNKNOWN
:
780 fprintf_unfiltered (gdb_stdlog
,
781 "Packet %s (%s) is NOT supported\n",
782 config
->name
, config
->title
);
783 config
->support
= PACKET_DISABLE
;
788 return PACKET_UNKNOWN
;
808 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
811 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
812 struct cmd_list_element
*c
)
814 struct packet_config
*packet
;
816 for (packet
= remote_protocol_packets
;
817 packet
< &remote_protocol_packets
[PACKET_MAX
];
820 if (&packet
->detect
== c
->var
)
822 update_packet_config (packet
);
826 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
831 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
832 struct cmd_list_element
*c
,
835 struct packet_config
*packet
;
837 for (packet
= remote_protocol_packets
;
838 packet
< &remote_protocol_packets
[PACKET_MAX
];
841 if (&packet
->detect
== c
->var
)
843 show_packet_config_cmd (packet
);
847 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
851 /* Should we try one of the 'Z' requests? */
855 Z_PACKET_SOFTWARE_BP
,
856 Z_PACKET_HARDWARE_BP
,
863 /* For compatibility with older distributions. Provide a ``set remote
864 Z-packet ...'' command that updates all the Z packet types. */
866 static enum auto_boolean remote_Z_packet_detect
;
869 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
870 struct cmd_list_element
*c
)
873 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
875 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
876 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
881 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
882 struct cmd_list_element
*c
,
886 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
888 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
892 /* Should we try the 'ThreadInfo' query packet?
894 This variable (NOT available to the user: auto-detect only!)
895 determines whether GDB will use the new, simpler "ThreadInfo"
896 query or the older, more complex syntax for thread queries.
897 This is an auto-detect variable (set to true at each connect,
898 and set to false when the target fails to recognize it). */
900 static int use_threadinfo_query
;
901 static int use_threadextra_query
;
903 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
904 static void *sigint_remote_twice_token
;
905 static void *sigint_remote_token
;
907 /* These are pointers to hook functions that may be set in order to
908 modify resume/wait behavior for a particular architecture. */
910 void (*deprecated_target_resume_hook
) (void);
911 void (*deprecated_target_wait_loop_hook
) (void);
915 /* These are the threads which we last sent to the remote system.
916 -1 for all or -2 for not sent yet. */
917 static int general_thread
;
918 static int continue_thread
;
920 /* Call this function as a result of
921 1) A halt indication (T packet) containing a thread id
922 2) A direct query of currthread
923 3) Successful execution of set thread
927 record_currthread (int currthread
)
929 general_thread
= currthread
;
931 /* If this is a new thread, add it to GDB's thread list.
932 If we leave it up to WFI to do this, bad things will happen. */
933 if (!in_thread_list (pid_to_ptid (currthread
)))
935 add_thread (pid_to_ptid (currthread
));
936 ui_out_text (uiout
, "[New ");
937 ui_out_text (uiout
, target_pid_to_str (pid_to_ptid (currthread
)));
938 ui_out_text (uiout
, "]\n");
942 #define MAGIC_NULL_PID 42000
945 set_thread (int th
, int gen
)
947 struct remote_state
*rs
= get_remote_state ();
949 int state
= gen
? general_thread
: continue_thread
;
955 buf
[1] = gen
? 'g' : 'c';
956 if (th
== MAGIC_NULL_PID
)
962 xsnprintf (&buf
[2], get_remote_packet_size () - 2, "-%x", -th
);
964 xsnprintf (&buf
[2], get_remote_packet_size () - 2, "%x", th
);
966 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
970 continue_thread
= th
;
973 /* Return nonzero if the thread TH is still alive on the remote system. */
976 remote_thread_alive (ptid_t ptid
)
978 struct remote_state
*rs
= get_remote_state ();
979 int tid
= PIDGET (ptid
);
983 xsnprintf (buf
, get_remote_packet_size (), "T-%08x", -tid
);
985 xsnprintf (buf
, get_remote_packet_size (), "T%08x", tid
);
987 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
988 return (buf
[0] == 'O' && buf
[1] == 'K');
991 /* About these extended threadlist and threadinfo packets. They are
992 variable length packets but, the fields within them are often fixed
993 length. They are redundent enough to send over UDP as is the
994 remote protocol in general. There is a matching unit test module
997 #define OPAQUETHREADBYTES 8
999 /* a 64 bit opaque identifier */
1000 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1002 /* WARNING: This threadref data structure comes from the remote O.S.,
1003 libstub protocol encoding, and remote.c. it is not particularly
1006 /* Right now, the internal structure is int. We want it to be bigger.
1010 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1012 /* gdb_ext_thread_info is an internal GDB data structure which is
1013 equivalent to the reply of the remote threadinfo packet. */
1015 struct gdb_ext_thread_info
1017 threadref threadid
; /* External form of thread reference. */
1018 int active
; /* Has state interesting to GDB?
1020 char display
[256]; /* Brief state display, name,
1021 blocked/suspended. */
1022 char shortname
[32]; /* To be used to name threads. */
1023 char more_display
[256]; /* Long info, statistics, queue depth,
1027 /* The volume of remote transfers can be limited by submitting
1028 a mask containing bits specifying the desired information.
1029 Use a union of these values as the 'selection' parameter to
1030 get_thread_info. FIXME: Make these TAG names more thread specific.
1033 #define TAG_THREADID 1
1034 #define TAG_EXISTS 2
1035 #define TAG_DISPLAY 4
1036 #define TAG_THREADNAME 8
1037 #define TAG_MOREDISPLAY 16
1039 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1041 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1043 static char *unpack_nibble (char *buf
, int *val
);
1045 static char *pack_nibble (char *buf
, int nibble
);
1047 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1049 static char *unpack_byte (char *buf
, int *value
);
1051 static char *pack_int (char *buf
, int value
);
1053 static char *unpack_int (char *buf
, int *value
);
1055 static char *unpack_string (char *src
, char *dest
, int length
);
1057 static char *pack_threadid (char *pkt
, threadref
*id
);
1059 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1061 void int_to_threadref (threadref
*id
, int value
);
1063 static int threadref_to_int (threadref
*ref
);
1065 static void copy_threadref (threadref
*dest
, threadref
*src
);
1067 static int threadmatch (threadref
*dest
, threadref
*src
);
1069 static char *pack_threadinfo_request (char *pkt
, int mode
,
1072 static int remote_unpack_thread_info_response (char *pkt
,
1073 threadref
*expectedref
,
1074 struct gdb_ext_thread_info
1078 static int remote_get_threadinfo (threadref
*threadid
,
1079 int fieldset
, /*TAG mask */
1080 struct gdb_ext_thread_info
*info
);
1082 static char *pack_threadlist_request (char *pkt
, int startflag
,
1084 threadref
*nextthread
);
1086 static int parse_threadlist_response (char *pkt
,
1088 threadref
*original_echo
,
1089 threadref
*resultlist
,
1092 static int remote_get_threadlist (int startflag
,
1093 threadref
*nextthread
,
1097 threadref
*threadlist
);
1099 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1101 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1102 void *context
, int looplimit
);
1104 static int remote_newthread_step (threadref
*ref
, void *context
);
1106 /* Encode 64 bits in 16 chars of hex. */
1108 static const char hexchars
[] = "0123456789abcdef";
1111 ishex (int ch
, int *val
)
1113 if ((ch
>= 'a') && (ch
<= 'f'))
1115 *val
= ch
- 'a' + 10;
1118 if ((ch
>= 'A') && (ch
<= 'F'))
1120 *val
= ch
- 'A' + 10;
1123 if ((ch
>= '0') && (ch
<= '9'))
1134 if (ch
>= 'a' && ch
<= 'f')
1135 return ch
- 'a' + 10;
1136 if (ch
>= '0' && ch
<= '9')
1138 if (ch
>= 'A' && ch
<= 'F')
1139 return ch
- 'A' + 10;
1144 stub_unpack_int (char *buff
, int fieldlength
)
1151 nibble
= stubhex (*buff
++);
1155 retval
= retval
<< 4;
1161 unpack_varlen_hex (char *buff
, /* packet to parse */
1167 while (ishex (*buff
, &nibble
))
1170 retval
= retval
<< 4;
1171 retval
|= nibble
& 0x0f;
1178 unpack_nibble (char *buf
, int *val
)
1180 ishex (*buf
++, val
);
1185 pack_nibble (char *buf
, int nibble
)
1187 *buf
++ = hexchars
[(nibble
& 0x0f)];
1192 pack_hex_byte (char *pkt
, int byte
)
1194 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1195 *pkt
++ = hexchars
[(byte
& 0xf)];
1200 unpack_byte (char *buf
, int *value
)
1202 *value
= stub_unpack_int (buf
, 2);
1207 pack_int (char *buf
, int value
)
1209 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1210 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1211 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1212 buf
= pack_hex_byte (buf
, (value
& 0xff));
1217 unpack_int (char *buf
, int *value
)
1219 *value
= stub_unpack_int (buf
, 8);
1223 #if 0 /* Currently unused, uncomment when needed. */
1224 static char *pack_string (char *pkt
, char *string
);
1227 pack_string (char *pkt
, char *string
)
1232 len
= strlen (string
);
1234 len
= 200; /* Bigger than most GDB packets, junk??? */
1235 pkt
= pack_hex_byte (pkt
, len
);
1239 if ((ch
== '\0') || (ch
== '#'))
1240 ch
= '*'; /* Protect encapsulation. */
1245 #endif /* 0 (unused) */
1248 unpack_string (char *src
, char *dest
, int length
)
1257 pack_threadid (char *pkt
, threadref
*id
)
1260 unsigned char *altid
;
1262 altid
= (unsigned char *) id
;
1263 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1265 pkt
= pack_hex_byte (pkt
, *altid
++);
1271 unpack_threadid (char *inbuf
, threadref
*id
)
1274 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1277 altref
= (char *) id
;
1279 while (inbuf
< limit
)
1281 x
= stubhex (*inbuf
++);
1282 y
= stubhex (*inbuf
++);
1283 *altref
++ = (x
<< 4) | y
;
1288 /* Externally, threadrefs are 64 bits but internally, they are still
1289 ints. This is due to a mismatch of specifications. We would like
1290 to use 64bit thread references internally. This is an adapter
1294 int_to_threadref (threadref
*id
, int value
)
1296 unsigned char *scan
;
1298 scan
= (unsigned char *) id
;
1304 *scan
++ = (value
>> 24) & 0xff;
1305 *scan
++ = (value
>> 16) & 0xff;
1306 *scan
++ = (value
>> 8) & 0xff;
1307 *scan
++ = (value
& 0xff);
1311 threadref_to_int (threadref
*ref
)
1314 unsigned char *scan
;
1320 value
= (value
<< 8) | ((*scan
++) & 0xff);
1325 copy_threadref (threadref
*dest
, threadref
*src
)
1328 unsigned char *csrc
, *cdest
;
1330 csrc
= (unsigned char *) src
;
1331 cdest
= (unsigned char *) dest
;
1338 threadmatch (threadref
*dest
, threadref
*src
)
1340 /* Things are broken right now, so just assume we got a match. */
1342 unsigned char *srcp
, *destp
;
1344 srcp
= (char *) src
;
1345 destp
= (char *) dest
;
1349 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1356 threadid:1, # always request threadid
1363 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1366 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1368 *pkt
++ = 'q'; /* Info Query */
1369 *pkt
++ = 'P'; /* process or thread info */
1370 pkt
= pack_int (pkt
, mode
); /* mode */
1371 pkt
= pack_threadid (pkt
, id
); /* threadid */
1372 *pkt
= '\0'; /* terminate */
1376 /* These values tag the fields in a thread info response packet. */
1377 /* Tagging the fields allows us to request specific fields and to
1378 add more fields as time goes by. */
1380 #define TAG_THREADID 1 /* Echo the thread identifier. */
1381 #define TAG_EXISTS 2 /* Is this process defined enough to
1382 fetch registers and its stack? */
1383 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1384 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1385 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1389 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1390 struct gdb_ext_thread_info
*info
)
1392 struct remote_state
*rs
= get_remote_state ();
1396 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
1399 /* info->threadid = 0; FIXME: implement zero_threadref. */
1401 info
->display
[0] = '\0';
1402 info
->shortname
[0] = '\0';
1403 info
->more_display
[0] = '\0';
1405 /* Assume the characters indicating the packet type have been
1407 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1408 pkt
= unpack_threadid (pkt
, &ref
);
1411 warning (_("Incomplete response to threadinfo request."));
1412 if (!threadmatch (&ref
, expectedref
))
1413 { /* This is an answer to a different request. */
1414 warning (_("ERROR RMT Thread info mismatch."));
1417 copy_threadref (&info
->threadid
, &ref
);
1419 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1421 /* Packets are terminated with nulls. */
1422 while ((pkt
< limit
) && mask
&& *pkt
)
1424 pkt
= unpack_int (pkt
, &tag
); /* tag */
1425 pkt
= unpack_byte (pkt
, &length
); /* length */
1426 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1428 warning (_("ERROR RMT: threadinfo tag mismatch."));
1432 if (tag
== TAG_THREADID
)
1436 warning (_("ERROR RMT: length of threadid is not 16."));
1440 pkt
= unpack_threadid (pkt
, &ref
);
1441 mask
= mask
& ~TAG_THREADID
;
1444 if (tag
== TAG_EXISTS
)
1446 info
->active
= stub_unpack_int (pkt
, length
);
1448 mask
= mask
& ~(TAG_EXISTS
);
1451 warning (_("ERROR RMT: 'exists' length too long."));
1457 if (tag
== TAG_THREADNAME
)
1459 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1460 mask
= mask
& ~TAG_THREADNAME
;
1463 if (tag
== TAG_DISPLAY
)
1465 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1466 mask
= mask
& ~TAG_DISPLAY
;
1469 if (tag
== TAG_MOREDISPLAY
)
1471 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1472 mask
= mask
& ~TAG_MOREDISPLAY
;
1475 warning (_("ERROR RMT: unknown thread info tag."));
1476 break; /* Not a tag we know about. */
1482 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1483 struct gdb_ext_thread_info
*info
)
1485 struct remote_state
*rs
= get_remote_state ();
1487 char *threadinfo_pkt
= rs
->buf
;
1489 pack_threadinfo_request (threadinfo_pkt
, fieldset
, threadid
);
1490 putpkt (threadinfo_pkt
);
1491 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1492 result
= remote_unpack_thread_info_response (threadinfo_pkt
+ 2,
1497 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1500 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1501 threadref
*nextthread
)
1503 *pkt
++ = 'q'; /* info query packet */
1504 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1505 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1506 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1507 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1512 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1515 parse_threadlist_response (char *pkt
, int result_limit
,
1516 threadref
*original_echo
, threadref
*resultlist
,
1519 struct remote_state
*rs
= get_remote_state ();
1521 int count
, resultcount
, done
;
1524 /* Assume the 'q' and 'M chars have been stripped. */
1525 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
1526 /* done parse past here */
1527 pkt
= unpack_byte (pkt
, &count
); /* count field */
1528 pkt
= unpack_nibble (pkt
, &done
);
1529 /* The first threadid is the argument threadid. */
1530 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1531 while ((count
-- > 0) && (pkt
< limit
))
1533 pkt
= unpack_threadid (pkt
, resultlist
++);
1534 if (resultcount
++ >= result_limit
)
1543 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1544 int *done
, int *result_count
, threadref
*threadlist
)
1546 struct remote_state
*rs
= get_remote_state ();
1547 static threadref echo_nextthread
;
1548 char *threadlist_packet
= rs
->buf
;
1551 /* Trancate result limit to be smaller than the packet size. */
1552 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= get_remote_packet_size ())
1553 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
1555 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
1557 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1560 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
1563 if (!threadmatch (&echo_nextthread
, nextthread
))
1565 /* FIXME: This is a good reason to drop the packet. */
1566 /* Possably, there is a duplicate response. */
1568 retransmit immediatly - race conditions
1569 retransmit after timeout - yes
1571 wait for packet, then exit
1573 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
1574 return 0; /* I choose simply exiting. */
1576 if (*result_count
<= 0)
1580 warning (_("RMT ERROR : failed to get remote thread list."));
1583 return result
; /* break; */
1585 if (*result_count
> result_limit
)
1588 warning (_("RMT ERROR: threadlist response longer than requested."));
1594 /* This is the interface between remote and threads, remotes upper
1597 /* remote_find_new_threads retrieves the thread list and for each
1598 thread in the list, looks up the thread in GDB's internal list,
1599 ading the thread if it does not already exist. This involves
1600 getting partial thread lists from the remote target so, polling the
1601 quit_flag is required. */
1604 /* About this many threadisds fit in a packet. */
1606 #define MAXTHREADLISTRESULTS 32
1609 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
1612 int done
, i
, result_count
;
1616 static threadref nextthread
;
1617 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1622 if (loopcount
++ > looplimit
)
1625 warning (_("Remote fetch threadlist -infinite loop-."));
1628 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1629 &done
, &result_count
, resultthreadlist
))
1634 /* Clear for later iterations. */
1636 /* Setup to resume next batch of thread references, set nextthread. */
1637 if (result_count
>= 1)
1638 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1640 while (result_count
--)
1641 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1648 remote_newthread_step (threadref
*ref
, void *context
)
1652 ptid
= pid_to_ptid (threadref_to_int (ref
));
1654 if (!in_thread_list (ptid
))
1656 return 1; /* continue iterator */
1659 #define CRAZY_MAX_THREADS 1000
1662 remote_current_thread (ptid_t oldpid
)
1664 struct remote_state
*rs
= get_remote_state ();
1665 char *buf
= rs
->buf
;
1668 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1669 if (buf
[0] == 'Q' && buf
[1] == 'C')
1670 /* Use strtoul here, so we'll correctly parse values whose highest
1671 bit is set. The protocol carries them as a simple series of
1672 hex digits; in the absence of a sign, strtol will see such
1673 values as positive numbers out of range for signed 'long', and
1674 return LONG_MAX to indicate an overflow. */
1675 return pid_to_ptid (strtoul (&buf
[2], NULL
, 16));
1680 /* Find new threads for info threads command.
1681 * Original version, using John Metzler's thread protocol.
1685 remote_find_new_threads (void)
1687 remote_threadlist_iterator (remote_newthread_step
, 0,
1689 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
) /* ack ack ack */
1690 inferior_ptid
= remote_current_thread (inferior_ptid
);
1694 * Find all threads for info threads command.
1695 * Uses new thread protocol contributed by Cisco.
1696 * Falls back and attempts to use the older method (above)
1697 * if the target doesn't respond to the new method.
1701 remote_threads_info (void)
1703 struct remote_state
*rs
= get_remote_state ();
1707 if (remote_desc
== 0) /* paranoia */
1708 error (_("Command can only be used when connected to the remote target."));
1710 if (use_threadinfo_query
)
1712 putpkt ("qfThreadInfo");
1714 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1715 if (bufp
[0] != '\0') /* q packet recognized */
1717 while (*bufp
++ == 'm') /* reply contains one or more TID */
1721 /* Use strtoul here, so we'll correctly parse values
1722 whose highest bit is set. The protocol carries
1723 them as a simple series of hex digits; in the
1724 absence of a sign, strtol will see such values as
1725 positive numbers out of range for signed 'long',
1726 and return LONG_MAX to indicate an overflow. */
1727 tid
= strtoul (bufp
, &bufp
, 16);
1728 if (tid
!= 0 && !in_thread_list (pid_to_ptid (tid
)))
1729 add_thread (pid_to_ptid (tid
));
1731 while (*bufp
++ == ','); /* comma-separated list */
1732 putpkt ("qsThreadInfo");
1734 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1740 /* Else fall back to old method based on jmetzler protocol. */
1741 use_threadinfo_query
= 0;
1742 remote_find_new_threads ();
1747 * Collect a descriptive string about the given thread.
1748 * The target may say anything it wants to about the thread
1749 * (typically info about its blocked / runnable state, name, etc.).
1750 * This string will appear in the info threads display.
1752 * Optional: targets are not required to implement this function.
1756 remote_threads_extra_info (struct thread_info
*tp
)
1758 struct remote_state
*rs
= get_remote_state ();
1762 struct gdb_ext_thread_info threadinfo
;
1763 static char display_buf
[100]; /* arbitrary... */
1764 int n
= 0; /* position in display_buf */
1766 if (remote_desc
== 0) /* paranoia */
1767 internal_error (__FILE__
, __LINE__
,
1768 _("remote_threads_extra_info"));
1770 if (use_threadextra_query
)
1772 char *bufp
= rs
->buf
;
1774 xsnprintf (bufp
, get_remote_packet_size (), "qThreadExtraInfo,%x",
1777 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1780 n
= min (strlen (bufp
) / 2, sizeof (display_buf
));
1781 result
= hex2bin (bufp
, (gdb_byte
*) display_buf
, n
);
1782 display_buf
[result
] = '\0';
1787 /* If the above query fails, fall back to the old method. */
1788 use_threadextra_query
= 0;
1789 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
1790 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
1791 int_to_threadref (&id
, PIDGET (tp
->ptid
));
1792 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
1793 if (threadinfo
.active
)
1795 if (*threadinfo
.shortname
)
1796 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
1797 " Name: %s,", threadinfo
.shortname
);
1798 if (*threadinfo
.display
)
1799 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
1800 " State: %s,", threadinfo
.display
);
1801 if (*threadinfo
.more_display
)
1802 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
1803 " Priority: %s", threadinfo
.more_display
);
1807 /* For purely cosmetic reasons, clear up trailing commas. */
1808 if (',' == display_buf
[n
-1])
1809 display_buf
[n
-1] = ' ';
1817 /* Restart the remote side; this is an extended protocol operation. */
1820 extended_remote_restart (void)
1822 struct remote_state
*rs
= get_remote_state ();
1824 /* Send the restart command; for reasons I don't understand the
1825 remote side really expects a number after the "R". */
1826 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
1829 remote_fileio_reset ();
1831 /* Now query for status so this looks just like we restarted
1832 gdbserver from scratch. */
1834 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1837 /* Clean up connection to a remote debugger. */
1840 remote_close (int quitting
)
1843 serial_close (remote_desc
);
1847 /* Query the remote side for the text, data and bss offsets. */
1852 struct remote_state
*rs
= get_remote_state ();
1853 char *buf
= rs
->buf
;
1856 CORE_ADDR text_addr
, data_addr
, bss_addr
;
1857 struct section_offsets
*offs
;
1859 putpkt ("qOffsets");
1860 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1862 if (buf
[0] == '\000')
1863 return; /* Return silently. Stub doesn't support
1867 warning (_("Remote failure reply: %s"), buf
);
1871 /* Pick up each field in turn. This used to be done with scanf, but
1872 scanf will make trouble if CORE_ADDR size doesn't match
1873 conversion directives correctly. The following code will work
1874 with any size of CORE_ADDR. */
1875 text_addr
= data_addr
= bss_addr
= 0;
1879 if (strncmp (ptr
, "Text=", 5) == 0)
1882 /* Don't use strtol, could lose on big values. */
1883 while (*ptr
&& *ptr
!= ';')
1884 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
1889 if (!lose
&& strncmp (ptr
, ";Data=", 6) == 0)
1892 while (*ptr
&& *ptr
!= ';')
1893 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
1898 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
1901 while (*ptr
&& *ptr
!= ';')
1902 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
1908 error (_("Malformed response to offset query, %s"), buf
);
1910 if (symfile_objfile
== NULL
)
1913 offs
= ((struct section_offsets
*)
1914 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
1915 memcpy (offs
, symfile_objfile
->section_offsets
,
1916 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
1918 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
1920 /* This is a temporary kludge to force data and bss to use the same offsets
1921 because that's what nlmconv does now. The real solution requires changes
1922 to the stub and remote.c that I don't have time to do right now. */
1924 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
1925 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
1927 objfile_relocate (symfile_objfile
, offs
);
1930 /* Stub for catch_errors. */
1933 remote_start_remote_dummy (struct ui_out
*uiout
, void *dummy
)
1935 start_remote (); /* Initialize gdb process mechanisms. */
1936 /* NOTE: Return something >=0. A -ve value is reserved for
1937 catch_exceptions. */
1942 remote_start_remote (struct ui_out
*uiout
, void *dummy
)
1944 immediate_quit
++; /* Allow user to interrupt it. */
1946 /* Ack any packet which the remote side has already sent. */
1947 serial_write (remote_desc
, "+", 1);
1949 /* Let the stub know that we want it to return the thread. */
1952 inferior_ptid
= remote_current_thread (inferior_ptid
);
1954 get_offsets (); /* Get text, data & bss offsets. */
1956 putpkt ("?"); /* Initiate a query from remote machine. */
1959 remote_start_remote_dummy (uiout
, dummy
);
1962 /* Open a connection to a remote debugger.
1963 NAME is the filename used for communication. */
1966 remote_open (char *name
, int from_tty
)
1968 remote_open_1 (name
, from_tty
, &remote_ops
, 0, 0);
1971 /* Just like remote_open, but with asynchronous support. */
1973 remote_async_open (char *name
, int from_tty
)
1975 remote_open_1 (name
, from_tty
, &remote_async_ops
, 0, 1);
1978 /* Open a connection to a remote debugger using the extended
1979 remote gdb protocol. NAME is the filename used for communication. */
1982 extended_remote_open (char *name
, int from_tty
)
1984 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */,
1988 /* Just like extended_remote_open, but with asynchronous support. */
1990 extended_remote_async_open (char *name
, int from_tty
)
1992 remote_open_1 (name
, from_tty
, &extended_async_remote_ops
,
1993 1 /*extended_p */, 1 /* async_p */);
1996 /* Generic code for opening a connection to a remote target. */
1999 init_all_packet_configs (void)
2002 for (i
= 0; i
< PACKET_MAX
; i
++)
2003 update_packet_config (&remote_protocol_packets
[i
]);
2006 /* Symbol look-up. */
2009 remote_check_symbols (struct objfile
*objfile
)
2011 struct remote_state
*rs
= get_remote_state ();
2012 char *msg
, *reply
, *tmp
;
2013 struct minimal_symbol
*sym
;
2016 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
2019 /* Allocate a message buffer. We can't reuse the input buffer in RS,
2020 because we need both at the same time. */
2021 msg
= alloca (get_remote_packet_size ());
2025 /* Invite target to request symbol lookups. */
2027 putpkt ("qSymbol::");
2028 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2029 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
2031 while (strncmp (reply
, "qSymbol:", 8) == 0)
2034 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
2036 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
2038 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
2040 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
2041 paddr_nz (SYMBOL_VALUE_ADDRESS (sym
)),
2044 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2048 static struct serial
*
2049 remote_serial_open (char *name
)
2051 static int udp_warning
= 0;
2053 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2054 of in ser-tcp.c, because it is the remote protocol assuming that the
2055 serial connection is reliable and not the serial connection promising
2057 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
2060 The remote protocol may be unreliable over UDP.\n\
2061 Some events may be lost, rendering further debugging impossible."));
2065 return serial_open (name
);
2069 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
,
2070 int extended_p
, int async_p
)
2072 struct remote_state
*rs
= get_remote_state ();
2074 error (_("To open a remote debug connection, you need to specify what\n"
2075 "serial device is attached to the remote system\n"
2076 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
2078 /* See FIXME above. */
2080 wait_forever_enabled_p
= 1;
2082 target_preopen (from_tty
);
2084 unpush_target (target
);
2086 remote_fileio_reset ();
2087 reopen_exec_file ();
2090 remote_desc
= remote_serial_open (name
);
2092 perror_with_name (name
);
2094 if (baud_rate
!= -1)
2096 if (serial_setbaudrate (remote_desc
, baud_rate
))
2098 /* The requested speed could not be set. Error out to
2099 top level after closing remote_desc. Take care to
2100 set remote_desc to NULL to avoid closing remote_desc
2102 serial_close (remote_desc
);
2104 perror_with_name (name
);
2108 serial_raw (remote_desc
);
2110 /* If there is something sitting in the buffer we might take it as a
2111 response to a command, which would be bad. */
2112 serial_flush_input (remote_desc
);
2116 puts_filtered ("Remote debugging using ");
2117 puts_filtered (name
);
2118 puts_filtered ("\n");
2120 push_target (target
); /* Switch to using remote target now. */
2122 init_all_packet_configs ();
2124 general_thread
= -2;
2125 continue_thread
= -2;
2127 /* Probe for ability to use "ThreadInfo" query, as required. */
2128 use_threadinfo_query
= 1;
2129 use_threadextra_query
= 1;
2131 /* Without this, some commands which require an active target (such
2132 as kill) won't work. This variable serves (at least) double duty
2133 as both the pid of the target process (if it has such), and as a
2134 flag indicating that a target is active. These functions should
2135 be split out into seperate variables, especially since GDB will
2136 someday have a notion of debugging several processes. */
2138 inferior_ptid
= pid_to_ptid (MAGIC_NULL_PID
);
2142 /* With this target we start out by owning the terminal. */
2143 remote_async_terminal_ours_p
= 1;
2145 /* FIXME: cagney/1999-09-23: During the initial connection it is
2146 assumed that the target is already ready and able to respond to
2147 requests. Unfortunately remote_start_remote() eventually calls
2148 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2149 around this. Eventually a mechanism that allows
2150 wait_for_inferior() to expect/get timeouts will be
2152 wait_forever_enabled_p
= 0;
2155 /* First delete any symbols previously loaded from shared libraries. */
2156 no_shared_libraries (NULL
, 0);
2158 /* Start the remote connection. If error() or QUIT, discard this
2159 target (we'd otherwise be in an inconsistent state) and then
2160 propogate the error on up the exception chain. This ensures that
2161 the caller doesn't stumble along blindly assuming that the
2162 function succeeded. The CLI doesn't have this problem but other
2163 UI's, such as MI do.
2165 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2166 this function should return an error indication letting the
2167 caller restore the previous state. Unfortunately the command
2168 ``target remote'' is directly wired to this function making that
2169 impossible. On a positive note, the CLI side of this problem has
2170 been fixed - the function set_cmd_context() makes it possible for
2171 all the ``target ....'' commands to share a common callback
2172 function. See cli-dump.c. */
2174 struct gdb_exception ex
2175 = catch_exception (uiout
, remote_start_remote
, NULL
, RETURN_MASK_ALL
);
2180 wait_forever_enabled_p
= 1;
2181 throw_exception (ex
);
2186 wait_forever_enabled_p
= 1;
2190 /* Tell the remote that we are using the extended protocol. */
2192 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2195 post_create_inferior (¤t_target
, from_tty
);
2197 if (exec_bfd
) /* No use without an exec file. */
2198 remote_check_symbols (symfile_objfile
);
2201 /* This takes a program previously attached to and detaches it. After
2202 this is done, GDB can be used to debug some other program. We
2203 better not have left any breakpoints in the target program or it'll
2204 die when it hits one. */
2207 remote_detach (char *args
, int from_tty
)
2209 struct remote_state
*rs
= get_remote_state ();
2212 error (_("Argument given to \"detach\" when remotely debugging."));
2214 /* Tell the remote target to detach. */
2215 strcpy (rs
->buf
, "D");
2216 remote_send (&rs
->buf
, &rs
->buf_size
);
2218 /* Unregister the file descriptor from the event loop. */
2219 if (target_is_async_p ())
2220 serial_async (remote_desc
, NULL
, 0);
2222 target_mourn_inferior ();
2224 puts_filtered ("Ending remote debugging.\n");
2227 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2230 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
2233 error (_("Argument given to \"detach\" when remotely debugging."));
2235 /* Unregister the file descriptor from the event loop. */
2236 if (target_is_async_p ())
2237 serial_async (remote_desc
, NULL
, 0);
2239 target_mourn_inferior ();
2241 puts_filtered ("Ending remote debugging.\n");
2244 /* Convert hex digit A to a number. */
2249 if (a
>= '0' && a
<= '9')
2251 else if (a
>= 'a' && a
<= 'f')
2252 return a
- 'a' + 10;
2253 else if (a
>= 'A' && a
<= 'F')
2254 return a
- 'A' + 10;
2256 error (_("Reply contains invalid hex digit %d"), a
);
2260 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
2264 for (i
= 0; i
< count
; i
++)
2266 if (hex
[0] == 0 || hex
[1] == 0)
2268 /* Hex string is short, or of uneven length.
2269 Return the count that has been converted so far. */
2272 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
2278 /* Convert number NIB to a hex digit. */
2286 return 'a' + nib
- 10;
2290 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
2293 /* May use a length, or a nul-terminated string as input. */
2295 count
= strlen ((char *) bin
);
2297 for (i
= 0; i
< count
; i
++)
2299 *hex
++ = tohex ((*bin
>> 4) & 0xf);
2300 *hex
++ = tohex (*bin
++ & 0xf);
2306 /* Check for the availability of vCont. This function should also check
2310 remote_vcont_probe (struct remote_state
*rs
)
2312 char *buf
= rs
->buf
;
2314 strcpy (buf
, "vCont?");
2316 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2318 /* Make sure that the features we assume are supported. */
2319 if (strncmp (buf
, "vCont", 5) == 0)
2322 int support_s
, support_S
, support_c
, support_C
;
2328 while (p
&& *p
== ';')
2331 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2333 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2335 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2337 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2340 p
= strchr (p
, ';');
2343 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2344 BUF will make packet_ok disable the packet. */
2345 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
2349 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
2352 /* Resume the remote inferior by using a "vCont" packet. The thread
2353 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2354 resumed thread should be single-stepped and/or signalled. If PTID's
2355 PID is -1, then all threads are resumed; the thread to be stepped and/or
2356 signalled is given in the global INFERIOR_PTID. This function returns
2357 non-zero iff it resumes the inferior.
2359 This function issues a strict subset of all possible vCont commands at the
2363 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2365 struct remote_state
*rs
= get_remote_state ();
2366 int pid
= PIDGET (ptid
);
2367 char *buf
= NULL
, *outbuf
;
2368 struct cleanup
*old_cleanup
;
2370 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
2371 remote_vcont_probe (rs
);
2373 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
2376 /* If we could generate a wider range of packets, we'd have to worry
2377 about overflowing BUF. Should there be a generic
2378 "multi-part-packet" packet? */
2380 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
)
2382 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2383 don't have any PID numbers the inferior will understand. Make sure
2384 to only send forms that do not specify a PID. */
2385 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2386 outbuf
= xstrprintf ("vCont;S%02x", siggnal
);
2388 outbuf
= xstrprintf ("vCont;s");
2389 else if (siggnal
!= TARGET_SIGNAL_0
)
2390 outbuf
= xstrprintf ("vCont;C%02x", siggnal
);
2392 outbuf
= xstrprintf ("vCont;c");
2396 /* Resume all threads, with preference for INFERIOR_PTID. */
2397 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2398 outbuf
= xstrprintf ("vCont;S%02x:%x;c", siggnal
,
2399 PIDGET (inferior_ptid
));
2401 outbuf
= xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid
));
2402 else if (siggnal
!= TARGET_SIGNAL_0
)
2403 outbuf
= xstrprintf ("vCont;C%02x:%x;c", siggnal
,
2404 PIDGET (inferior_ptid
));
2406 outbuf
= xstrprintf ("vCont;c");
2410 /* Scheduler locking; resume only PTID. */
2411 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2412 outbuf
= xstrprintf ("vCont;S%02x:%x", siggnal
, pid
);
2414 outbuf
= xstrprintf ("vCont;s:%x", pid
);
2415 else if (siggnal
!= TARGET_SIGNAL_0
)
2416 outbuf
= xstrprintf ("vCont;C%02x:%x", siggnal
, pid
);
2418 outbuf
= xstrprintf ("vCont;c:%x", pid
);
2421 gdb_assert (outbuf
&& strlen (outbuf
) < get_remote_packet_size ());
2422 old_cleanup
= make_cleanup (xfree
, outbuf
);
2426 do_cleanups (old_cleanup
);
2431 /* Tell the remote machine to resume. */
2433 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
2435 static int last_sent_step
;
2438 remote_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2440 struct remote_state
*rs
= get_remote_state ();
2441 char *buf
= rs
->buf
;
2442 int pid
= PIDGET (ptid
);
2444 last_sent_signal
= siggnal
;
2445 last_sent_step
= step
;
2447 /* A hook for when we need to do something at the last moment before
2449 if (deprecated_target_resume_hook
)
2450 (*deprecated_target_resume_hook
) ();
2452 /* The vCont packet doesn't need to specify threads via Hc. */
2453 if (remote_vcont_resume (ptid
, step
, siggnal
))
2456 /* All other supported resume packets do use Hc, so call set_thread. */
2458 set_thread (0, 0); /* Run any thread. */
2460 set_thread (pid
, 0); /* Run this thread. */
2462 if (siggnal
!= TARGET_SIGNAL_0
)
2464 buf
[0] = step
? 'S' : 'C';
2465 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2466 buf
[2] = tohex (((int) siggnal
) & 0xf);
2470 strcpy (buf
, step
? "s" : "c");
2475 /* Same as remote_resume, but with async support. */
2477 remote_async_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2479 remote_resume (ptid
, step
, siggnal
);
2481 /* We are about to start executing the inferior, let's register it
2482 with the event loop. NOTE: this is the one place where all the
2483 execution commands end up. We could alternatively do this in each
2484 of the execution commands in infcmd.c. */
2485 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2486 into infcmd.c in order to allow inferior function calls to work
2487 NOT asynchronously. */
2488 if (target_can_async_p ())
2489 target_async (inferior_event_handler
, 0);
2490 /* Tell the world that the target is now executing. */
2491 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2492 this? Instead, should the client of target just assume (for
2493 async targets) that the target is going to start executing? Is
2494 this information already found in the continuation block? */
2495 if (target_is_async_p ())
2496 target_executing
= 1;
2500 /* Set up the signal handler for SIGINT, while the target is
2501 executing, ovewriting the 'regular' SIGINT signal handler. */
2503 initialize_sigint_signal_handler (void)
2505 sigint_remote_token
=
2506 create_async_signal_handler (async_remote_interrupt
, NULL
);
2507 signal (SIGINT
, handle_remote_sigint
);
2510 /* Signal handler for SIGINT, while the target is executing. */
2512 handle_remote_sigint (int sig
)
2514 signal (sig
, handle_remote_sigint_twice
);
2515 sigint_remote_twice_token
=
2516 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
2517 mark_async_signal_handler_wrapper (sigint_remote_token
);
2520 /* Signal handler for SIGINT, installed after SIGINT has already been
2521 sent once. It will take effect the second time that the user sends
2524 handle_remote_sigint_twice (int sig
)
2526 signal (sig
, handle_sigint
);
2527 sigint_remote_twice_token
=
2528 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
2529 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
2532 /* Perform the real interruption of the target execution, in response
2535 async_remote_interrupt (gdb_client_data arg
)
2538 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2543 /* Perform interrupt, if the first attempt did not succeed. Just give
2544 up on the target alltogether. */
2546 async_remote_interrupt_twice (gdb_client_data arg
)
2549 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
2550 /* Do something only if the target was not killed by the previous
2552 if (target_executing
)
2555 signal (SIGINT
, handle_remote_sigint
);
2559 /* Reinstall the usual SIGINT handlers, after the target has
2562 cleanup_sigint_signal_handler (void *dummy
)
2564 signal (SIGINT
, handle_sigint
);
2565 if (sigint_remote_twice_token
)
2566 delete_async_signal_handler ((struct async_signal_handler
**)
2567 &sigint_remote_twice_token
);
2568 if (sigint_remote_token
)
2569 delete_async_signal_handler ((struct async_signal_handler
**)
2570 &sigint_remote_token
);
2573 /* Send ^C to target to halt it. Target will respond, and send us a
2575 static void (*ofunc
) (int);
2577 /* The command line interface's stop routine. This function is installed
2578 as a signal handler for SIGINT. The first time a user requests a
2579 stop, we call remote_stop to send a break or ^C. If there is no
2580 response from the target (it didn't stop when the user requested it),
2581 we ask the user if he'd like to detach from the target. */
2583 remote_interrupt (int signo
)
2585 /* If this doesn't work, try more severe steps. */
2586 signal (signo
, remote_interrupt_twice
);
2589 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2594 /* The user typed ^C twice. */
2597 remote_interrupt_twice (int signo
)
2599 signal (signo
, ofunc
);
2601 signal (signo
, remote_interrupt
);
2604 /* This is the generic stop called via the target vector. When a target
2605 interrupt is requested, either by the command line or the GUI, we
2606 will eventually end up here. */
2610 /* Send a break or a ^C, depending on user preference. */
2612 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
2615 serial_send_break (remote_desc
);
2617 serial_write (remote_desc
, "\003", 1);
2620 /* Ask the user what to do when an interrupt is received. */
2623 interrupt_query (void)
2625 target_terminal_ours ();
2627 if (query ("Interrupted while waiting for the program.\n\
2628 Give up (and stop debugging it)? "))
2630 target_mourn_inferior ();
2631 deprecated_throw_reason (RETURN_QUIT
);
2634 target_terminal_inferior ();
2637 /* Enable/disable target terminal ownership. Most targets can use
2638 terminal groups to control terminal ownership. Remote targets are
2639 different in that explicit transfer of ownership to/from GDB/target
2643 remote_async_terminal_inferior (void)
2645 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2646 sync_execution here. This function should only be called when
2647 GDB is resuming the inferior in the forground. A background
2648 resume (``run&'') should leave GDB in control of the terminal and
2649 consequently should not call this code. */
2650 if (!sync_execution
)
2652 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2653 calls target_terminal_*() idenpotent. The event-loop GDB talking
2654 to an asynchronous target with a synchronous command calls this
2655 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2656 stops trying to transfer the terminal to the target when it
2657 shouldn't this guard can go away. */
2658 if (!remote_async_terminal_ours_p
)
2660 delete_file_handler (input_fd
);
2661 remote_async_terminal_ours_p
= 0;
2662 initialize_sigint_signal_handler ();
2663 /* NOTE: At this point we could also register our selves as the
2664 recipient of all input. Any characters typed could then be
2665 passed on down to the target. */
2669 remote_async_terminal_ours (void)
2671 /* See FIXME in remote_async_terminal_inferior. */
2672 if (!sync_execution
)
2674 /* See FIXME in remote_async_terminal_inferior. */
2675 if (remote_async_terminal_ours_p
)
2677 cleanup_sigint_signal_handler (NULL
);
2678 add_file_handler (input_fd
, stdin_event_handler
, 0);
2679 remote_async_terminal_ours_p
= 1;
2682 /* If nonzero, ignore the next kill. */
2687 remote_console_output (char *msg
)
2691 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
2694 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
2697 fputs_unfiltered (tb
, gdb_stdtarg
);
2699 gdb_flush (gdb_stdtarg
);
2702 /* Wait until the remote machine stops, then return,
2703 storing status in STATUS just as `wait' would.
2704 Returns "pid", which in the case of a multi-threaded
2705 remote OS, is the thread-id. */
2708 remote_wait (ptid_t ptid
, struct target_waitstatus
*status
)
2710 struct remote_state
*rs
= get_remote_state ();
2711 struct remote_arch_state
*rsa
= get_remote_arch_state ();
2712 char *buf
= rs
->buf
;
2713 ULONGEST thread_num
= -1;
2716 status
->kind
= TARGET_WAITKIND_EXITED
;
2717 status
->value
.integer
= 0;
2723 ofunc
= signal (SIGINT
, remote_interrupt
);
2724 getpkt (&rs
->buf
, &rs
->buf_size
, 1);
2725 signal (SIGINT
, ofunc
);
2727 /* This is a hook for when we need to do something (perhaps the
2728 collection of trace data) every time the target stops. */
2729 if (deprecated_target_wait_loop_hook
)
2730 (*deprecated_target_wait_loop_hook
) ();
2732 remote_stopped_by_watchpoint_p
= 0;
2736 case 'E': /* Error of some sort. */
2737 warning (_("Remote failure reply: %s"), buf
);
2739 case 'F': /* File-I/O request. */
2740 remote_fileio_request (buf
);
2742 case 'T': /* Status with PC, SP, FP, ... */
2744 gdb_byte regs
[MAX_REGISTER_SIZE
];
2746 /* Expedited reply, containing Signal, {regno, reg} repeat. */
2747 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2749 n... = register number
2750 r... = register contents
2752 p
= &buf
[3]; /* after Txx */
2761 /* If the packet contains a register number save it in
2762 pnum and set p1 to point to the character following
2763 it. Otherwise p1 points to p. */
2765 /* If this packet is an awatch packet, don't parse the
2766 'a' as a register number. */
2768 if (strncmp (p
, "awatch", strlen("awatch")) != 0)
2770 /* Read the ``P'' register number. */
2771 pnum
= strtol (p
, &p_temp
, 16);
2777 if (p1
== p
) /* No register number present here. */
2779 p1
= strchr (p
, ':');
2781 error (_("Malformed packet(a) (missing colon): %s\n\
2784 if (strncmp (p
, "thread", p1
- p
) == 0)
2786 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2787 record_currthread (thread_num
);
2790 else if ((strncmp (p
, "watch", p1
- p
) == 0)
2791 || (strncmp (p
, "rwatch", p1
- p
) == 0)
2792 || (strncmp (p
, "awatch", p1
- p
) == 0))
2794 remote_stopped_by_watchpoint_p
= 1;
2795 p
= unpack_varlen_hex (++p1
, &addr
);
2796 remote_watch_data_address
= (CORE_ADDR
)addr
;
2800 /* Silently skip unknown optional info. */
2801 p_temp
= strchr (p1
+ 1, ';');
2808 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
2812 error (_("Malformed packet(b) (missing colon): %s\n\
2817 error (_("Remote sent bad register number %s: %s\n\
2819 phex_nz (pnum
, 0), p
, buf
);
2821 fieldsize
= hex2bin (p
, regs
,
2822 register_size (current_gdbarch
,
2825 if (fieldsize
< register_size (current_gdbarch
,
2827 warning (_("Remote reply is too short: %s"), buf
);
2828 regcache_raw_supply (current_regcache
,
2833 error (_("Remote register badly formatted: %s\nhere: %s"),
2838 case 'S': /* Old style status, just signal only. */
2839 status
->kind
= TARGET_WAITKIND_STOPPED
;
2840 status
->value
.sig
= (enum target_signal
)
2841 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2845 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2846 record_currthread (thread_num
);
2849 case 'W': /* Target exited. */
2851 /* The remote process exited. */
2852 status
->kind
= TARGET_WAITKIND_EXITED
;
2853 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2857 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2858 status
->value
.sig
= (enum target_signal
)
2859 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2863 case 'O': /* Console output. */
2864 remote_console_output (buf
+ 1);
2867 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2869 /* Zero length reply means that we tried 'S' or 'C' and
2870 the remote system doesn't support it. */
2871 target_terminal_ours_for_output ();
2873 ("Can't send signals to this remote system. %s not sent.\n",
2874 target_signal_to_name (last_sent_signal
));
2875 last_sent_signal
= TARGET_SIGNAL_0
;
2876 target_terminal_inferior ();
2878 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2879 putpkt ((char *) buf
);
2882 /* else fallthrough */
2884 warning (_("Invalid remote reply: %s"), buf
);
2889 if (thread_num
!= -1)
2891 return pid_to_ptid (thread_num
);
2893 return inferior_ptid
;
2896 /* Async version of remote_wait. */
2898 remote_async_wait (ptid_t ptid
, struct target_waitstatus
*status
)
2900 struct remote_state
*rs
= get_remote_state ();
2901 struct remote_arch_state
*rsa
= get_remote_arch_state ();
2902 char *buf
= rs
->buf
;
2903 ULONGEST thread_num
= -1;
2906 status
->kind
= TARGET_WAITKIND_EXITED
;
2907 status
->value
.integer
= 0;
2909 remote_stopped_by_watchpoint_p
= 0;
2915 if (!target_is_async_p ())
2916 ofunc
= signal (SIGINT
, remote_interrupt
);
2917 /* FIXME: cagney/1999-09-27: If we're in async mode we should
2918 _never_ wait for ever -> test on target_is_async_p().
2919 However, before we do that we need to ensure that the caller
2920 knows how to take the target into/out of async mode. */
2921 getpkt (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
2922 if (!target_is_async_p ())
2923 signal (SIGINT
, ofunc
);
2925 /* This is a hook for when we need to do something (perhaps the
2926 collection of trace data) every time the target stops. */
2927 if (deprecated_target_wait_loop_hook
)
2928 (*deprecated_target_wait_loop_hook
) ();
2932 case 'E': /* Error of some sort. */
2933 warning (_("Remote failure reply: %s"), buf
);
2935 case 'F': /* File-I/O request. */
2936 remote_fileio_request (buf
);
2938 case 'T': /* Status with PC, SP, FP, ... */
2940 gdb_byte regs
[MAX_REGISTER_SIZE
];
2942 /* Expedited reply, containing Signal, {regno, reg} repeat. */
2943 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2945 n... = register number
2946 r... = register contents
2948 p
= &buf
[3]; /* after Txx */
2957 /* If the packet contains a register number, save it
2958 in pnum and set p1 to point to the character
2959 following it. Otherwise p1 points to p. */
2961 /* If this packet is an awatch packet, don't parse the 'a'
2962 as a register number. */
2964 if (!strncmp (p
, "awatch", strlen ("awatch")) != 0)
2966 /* Read the register number. */
2967 pnum
= strtol (p
, &p_temp
, 16);
2973 if (p1
== p
) /* No register number present here. */
2975 p1
= strchr (p
, ':');
2977 error (_("Malformed packet(a) (missing colon): %s\n\
2980 if (strncmp (p
, "thread", p1
- p
) == 0)
2982 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2983 record_currthread (thread_num
);
2986 else if ((strncmp (p
, "watch", p1
- p
) == 0)
2987 || (strncmp (p
, "rwatch", p1
- p
) == 0)
2988 || (strncmp (p
, "awatch", p1
- p
) == 0))
2990 remote_stopped_by_watchpoint_p
= 1;
2991 p
= unpack_varlen_hex (++p1
, &addr
);
2992 remote_watch_data_address
= (CORE_ADDR
)addr
;
2996 /* Silently skip unknown optional info. */
2997 p_temp
= strchr (p1
+ 1, ';');
3005 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
3008 error (_("Malformed packet(b) (missing colon): %s\n\
3013 error (_("Remote sent bad register number %ld: %s\n\
3017 fieldsize
= hex2bin (p
, regs
,
3018 register_size (current_gdbarch
,
3021 if (fieldsize
< register_size (current_gdbarch
,
3023 warning (_("Remote reply is too short: %s"), buf
);
3024 regcache_raw_supply (current_regcache
, reg
->regnum
, regs
);
3028 error (_("Remote register badly formatted: %s\nhere: %s"),
3033 case 'S': /* Old style status, just signal only. */
3034 status
->kind
= TARGET_WAITKIND_STOPPED
;
3035 status
->value
.sig
= (enum target_signal
)
3036 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3040 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
3041 record_currthread (thread_num
);
3044 case 'W': /* Target exited. */
3046 /* The remote process exited. */
3047 status
->kind
= TARGET_WAITKIND_EXITED
;
3048 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
3052 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
3053 status
->value
.sig
= (enum target_signal
)
3054 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3058 case 'O': /* Console output. */
3059 remote_console_output (buf
+ 1);
3060 /* Return immediately to the event loop. The event loop will
3061 still be waiting on the inferior afterwards. */
3062 status
->kind
= TARGET_WAITKIND_IGNORE
;
3065 if (last_sent_signal
!= TARGET_SIGNAL_0
)
3067 /* Zero length reply means that we tried 'S' or 'C' and
3068 the remote system doesn't support it. */
3069 target_terminal_ours_for_output ();
3071 ("Can't send signals to this remote system. %s not sent.\n",
3072 target_signal_to_name (last_sent_signal
));
3073 last_sent_signal
= TARGET_SIGNAL_0
;
3074 target_terminal_inferior ();
3076 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
3077 putpkt ((char *) buf
);
3080 /* else fallthrough */
3082 warning (_("Invalid remote reply: %s"), buf
);
3087 if (thread_num
!= -1)
3089 return pid_to_ptid (thread_num
);
3091 return inferior_ptid
;
3094 /* Number of bytes of registers this stub implements. */
3096 static int register_bytes_found
;
3098 /* Read the remote registers into the block REGS. */
3099 /* Currently we just read all the registers, so we don't use regnum. */
3102 fetch_register_using_p (int regnum
)
3104 struct remote_state
*rs
= get_remote_state ();
3105 char *buf
= rs
->buf
, *p
;
3106 char regp
[MAX_REGISTER_SIZE
];
3111 p
+= hexnumstr (p
, regnum
);
3113 remote_send (&rs
->buf
, &rs
->buf_size
);
3115 /* If the stub didn't recognize the packet, or if we got an error,
3117 if (buf
[0] == '\0' || buf
[0] == 'E')
3120 /* If this register is unfetchable, tell the regcache. */
3123 regcache_raw_supply (current_regcache
, regnum
, NULL
);
3124 set_register_cached (regnum
, -1);
3128 /* Otherwise, parse and supply the value. */
3135 error (_("fetch_register_using_p: early buf termination"));
3139 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3142 regcache_raw_supply (current_regcache
, regnum
, regp
);
3147 remote_fetch_registers (int regnum
)
3149 struct remote_state
*rs
= get_remote_state ();
3150 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3151 char *buf
= rs
->buf
;
3154 char *regs
= alloca (rsa
->sizeof_g_packet
);
3156 set_thread (PIDGET (inferior_ptid
), 1);
3160 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
3161 gdb_assert (reg
!= NULL
);
3162 if (!reg
->in_g_packet
)
3163 internal_error (__FILE__
, __LINE__
,
3164 _("Attempt to fetch a non G-packet register when this "
3165 "remote.c does not support the p-packet."));
3167 switch (remote_protocol_packets
[PACKET_p
].support
)
3169 case PACKET_DISABLE
:
3172 if (fetch_register_using_p (regnum
))
3175 error (_("Protocol error: p packet not recognized by stub"));
3176 case PACKET_SUPPORT_UNKNOWN
:
3177 if (fetch_register_using_p (regnum
))
3179 /* The stub recognized the 'p' packet. Remember this. */
3180 remote_protocol_packets
[PACKET_p
].support
= PACKET_ENABLE
;
3185 /* The stub does not support the 'P' packet. Use 'G'
3186 instead, and don't try using 'P' in the future (it
3187 will just waste our time). */
3188 remote_protocol_packets
[PACKET_p
].support
= PACKET_DISABLE
;
3194 remote_send (&rs
->buf
, &rs
->buf_size
);
3196 /* Save the size of the packet sent to us by the target. Its used
3197 as a heuristic when determining the max size of packets that the
3198 target can safely receive. */
3199 if ((rsa
->actual_register_packet_size
) == 0)
3200 (rsa
->actual_register_packet_size
) = strlen (buf
);
3202 /* Unimplemented registers read as all bits zero. */
3203 memset (regs
, 0, rsa
->sizeof_g_packet
);
3205 /* We can get out of synch in various cases. If the first character
3206 in the buffer is not a hex character, assume that has happened
3207 and try to fetch another packet to read. */
3208 while ((buf
[0] < '0' || buf
[0] > '9')
3209 && (buf
[0] < 'A' || buf
[0] > 'F')
3210 && (buf
[0] < 'a' || buf
[0] > 'f')
3211 && buf
[0] != 'x') /* New: unavailable register value. */
3214 fprintf_unfiltered (gdb_stdlog
,
3215 "Bad register packet; fetching a new packet\n");
3216 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3219 /* Reply describes registers byte by byte, each byte encoded as two
3220 hex characters. Suck them all up, then supply them to the
3221 register cacheing/storage mechanism. */
3224 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
3230 warning (_("Remote reply is of odd length: %s"), buf
);
3231 /* Don't change register_bytes_found in this case, and don't
3232 print a second warning. */
3235 if (p
[0] == 'x' && p
[1] == 'x')
3236 regs
[i
] = 0; /* 'x' */
3238 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3242 if (i
!= register_bytes_found
)
3244 register_bytes_found
= i
;
3245 if (REGISTER_BYTES_OK_P ()
3246 && !REGISTER_BYTES_OK (i
))
3247 warning (_("Remote reply is too short: %s"), buf
);
3253 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
3255 struct packet_reg
*r
= &rsa
->regs
[i
];
3258 if (r
->offset
* 2 >= strlen (buf
))
3259 /* A short packet that didn't include the register's
3260 value, this implies that the register is zero (and
3261 not that the register is unavailable). Supply that
3263 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3264 else if (buf
[r
->offset
* 2] == 'x')
3266 gdb_assert (r
->offset
* 2 < strlen (buf
));
3267 /* The register isn't available, mark it as such (at
3268 the same time setting the value to zero). */
3269 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3270 set_register_cached (i
, -1);
3273 regcache_raw_supply (current_regcache
, r
->regnum
,
3280 /* Prepare to store registers. Since we may send them all (using a
3281 'G' request), we have to read out the ones we don't want to change
3285 remote_prepare_to_store (void)
3287 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3289 gdb_byte buf
[MAX_REGISTER_SIZE
];
3291 /* Make sure the entire registers array is valid. */
3292 switch (remote_protocol_packets
[PACKET_P
].support
)
3294 case PACKET_DISABLE
:
3295 case PACKET_SUPPORT_UNKNOWN
:
3296 /* Make sure all the necessary registers are cached. */
3297 for (i
= 0; i
< NUM_REGS
; i
++)
3298 if (rsa
->regs
[i
].in_g_packet
)
3299 regcache_raw_read (current_regcache
, rsa
->regs
[i
].regnum
, buf
);
3306 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
3307 packet was not recognized. */
3310 store_register_using_P (int regnum
)
3312 struct remote_state
*rs
= get_remote_state ();
3313 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3314 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
3315 /* Try storing a single register. */
3316 char *buf
= rs
->buf
;
3317 gdb_byte regp
[MAX_REGISTER_SIZE
];
3320 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
3321 p
= buf
+ strlen (buf
);
3322 regcache_raw_collect (current_regcache
, reg
->regnum
, regp
);
3323 bin2hex (regp
, p
, register_size (current_gdbarch
, reg
->regnum
));
3324 remote_send (&rs
->buf
, &rs
->buf_size
);
3326 return buf
[0] != '\0';
3330 /* Store register REGNUM, or all registers if REGNUM == -1, from the
3331 contents of the register cache buffer. FIXME: ignores errors. */
3334 remote_store_registers (int regnum
)
3336 struct remote_state
*rs
= get_remote_state ();
3337 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3341 set_thread (PIDGET (inferior_ptid
), 1);
3345 switch (remote_protocol_packets
[PACKET_P
].support
)
3347 case PACKET_DISABLE
:
3350 if (store_register_using_P (regnum
))
3353 error (_("Protocol error: P packet not recognized by stub"));
3354 case PACKET_SUPPORT_UNKNOWN
:
3355 if (store_register_using_P (regnum
))
3357 /* The stub recognized the 'P' packet. Remember this. */
3358 remote_protocol_packets
[PACKET_P
].support
= PACKET_ENABLE
;
3363 /* The stub does not support the 'P' packet. Use 'G'
3364 instead, and don't try using 'P' in the future (it
3365 will just waste our time). */
3366 remote_protocol_packets
[PACKET_P
].support
= PACKET_DISABLE
;
3372 /* Extract all the registers in the regcache copying them into a
3376 regs
= alloca (rsa
->sizeof_g_packet
);
3377 memset (regs
, 0, rsa
->sizeof_g_packet
);
3378 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
3380 struct packet_reg
*r
= &rsa
->regs
[i
];
3382 regcache_raw_collect (current_regcache
, r
->regnum
, regs
+ r
->offset
);
3386 /* Command describes registers byte by byte,
3387 each byte encoded as two hex characters. */
3390 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3391 bin2hex (regs
, p
, register_bytes_found
);
3392 remote_send (&rs
->buf
, &rs
->buf_size
);
3396 /* Return the number of hex digits in num. */
3399 hexnumlen (ULONGEST num
)
3403 for (i
= 0; num
!= 0; i
++)
3409 /* Set BUF to the minimum number of hex digits representing NUM. */
3412 hexnumstr (char *buf
, ULONGEST num
)
3414 int len
= hexnumlen (num
);
3415 return hexnumnstr (buf
, num
, len
);
3419 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3422 hexnumnstr (char *buf
, ULONGEST num
, int width
)
3428 for (i
= width
- 1; i
>= 0; i
--)
3430 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
3437 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3440 remote_address_masked (CORE_ADDR addr
)
3442 if (remote_address_size
> 0
3443 && remote_address_size
< (sizeof (ULONGEST
) * 8))
3445 /* Only create a mask when that mask can safely be constructed
3446 in a ULONGEST variable. */
3448 mask
= (mask
<< remote_address_size
) - 1;
3454 /* Determine whether the remote target supports binary downloading.
3455 This is accomplished by sending a no-op memory write of zero length
3456 to the target at the specified address. It does not suffice to send
3457 the whole packet, since many stubs strip the eighth bit and
3458 subsequently compute a wrong checksum, which causes real havoc with
3461 NOTE: This can still lose if the serial line is not eight-bit
3462 clean. In cases like this, the user should clear "remote
3466 check_binary_download (CORE_ADDR addr
)
3468 struct remote_state
*rs
= get_remote_state ();
3470 switch (remote_protocol_packets
[PACKET_X
].support
)
3472 case PACKET_DISABLE
:
3476 case PACKET_SUPPORT_UNKNOWN
:
3478 char *buf
= rs
->buf
;
3483 p
+= hexnumstr (p
, (ULONGEST
) addr
);
3485 p
+= hexnumstr (p
, (ULONGEST
) 0);
3489 putpkt_binary (buf
, (int) (p
- buf
));
3490 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3495 fprintf_unfiltered (gdb_stdlog
,
3496 "binary downloading NOT suppported by target\n");
3497 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
3502 fprintf_unfiltered (gdb_stdlog
,
3503 "binary downloading suppported by target\n");
3504 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
3511 /* Write memory data directly to the remote machine.
3512 This does not inform the data cache; the data cache uses this.
3513 MEMADDR is the address in the remote memory space.
3514 MYADDR is the address of the buffer in our space.
3515 LEN is the number of bytes.
3517 Returns number of bytes transferred, or 0 (setting errno) for
3518 error. Only transfer a single packet. */
3521 remote_write_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
3523 struct remote_state
*rs
= get_remote_state ();
3531 char *payload_start
;
3533 /* Verify that the target can support a binary download. */
3534 check_binary_download (memaddr
);
3536 payload_size
= get_memory_write_packet_size ();
3538 /* The packet buffer will be large enough for the payload;
3539 get_memory_packet_size ensures this. */
3542 /* Compute the size of the actual payload by subtracting out the
3543 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
3545 payload_size
-= strlen ("$M,:#NN");
3546 payload_size
-= hexnumlen (memaddr
);
3548 /* Construct the packet header: "[MX]<memaddr>,<len>:". */
3550 /* Append "[XM]". Compute a best guess of the number of bytes
3551 actually transfered. */
3553 switch (remote_protocol_packets
[PACKET_X
].support
)
3557 /* Best guess at number of bytes that will fit. */
3558 todo
= min (len
, payload_size
);
3559 payload_size
-= hexnumlen (todo
);
3560 todo
= min (todo
, payload_size
);
3562 case PACKET_DISABLE
:
3564 /* Num bytes that will fit. */
3565 todo
= min (len
, payload_size
/ 2);
3566 payload_size
-= hexnumlen (todo
);
3567 todo
= min (todo
, payload_size
/ 2);
3569 case PACKET_SUPPORT_UNKNOWN
:
3570 internal_error (__FILE__
, __LINE__
,
3571 _("remote_write_bytes: bad internal state"));
3573 internal_error (__FILE__
, __LINE__
, _("bad switch"));
3576 internal_error (__FILE__
, __LINE__
,
3577 _("minumum packet size too small to write data"));
3579 /* Append "<memaddr>". */
3580 memaddr
= remote_address_masked (memaddr
);
3581 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3586 /* Append <len>. Retain the location/size of <len>. It may need to
3587 be adjusted once the packet body has been created. */
3589 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
3596 /* Append the packet body. */
3598 switch (remote_protocol_packets
[PACKET_X
].support
)
3601 /* Binary mode. Send target system values byte by byte, in
3602 increasing byte addresses. Only escape certain critical
3605 (nr_bytes
< todo
) && (p
- payload_start
) < payload_size
;
3608 switch (myaddr
[nr_bytes
] & 0xff)
3613 /* These must be escaped. */
3615 *p
++ = (myaddr
[nr_bytes
] & 0xff) ^ 0x20;
3618 *p
++ = myaddr
[nr_bytes
] & 0xff;
3622 if (nr_bytes
< todo
)
3624 /* Escape chars have filled up the buffer prematurely,
3625 and we have actually sent fewer bytes than planned.
3626 Fix-up the length field of the packet. Use the same
3627 number of characters as before. */
3628 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
3629 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
3632 case PACKET_DISABLE
:
3633 /* Normal mode: Send target system values byte by byte, in
3634 increasing byte addresses. Each byte is encoded as a two hex
3636 nr_bytes
= bin2hex (myaddr
, p
, todo
);
3639 case PACKET_SUPPORT_UNKNOWN
:
3640 internal_error (__FILE__
, __LINE__
,
3641 _("remote_write_bytes: bad internal state"));
3643 internal_error (__FILE__
, __LINE__
, _("bad switch"));
3646 putpkt_binary (buf
, (int) (p
- buf
));
3647 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3651 /* There is no correspondance between what the remote protocol
3652 uses for errors and errno codes. We would like a cleaner way
3653 of representing errors (big enough to include errno codes,
3654 bfd_error codes, and others). But for now just return EIO. */
3659 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
3660 fewer bytes than we'd planned. */
3664 /* Read memory data directly from the remote machine.
3665 This does not use the data cache; the data cache uses this.
3666 MEMADDR is the address in the remote memory space.
3667 MYADDR is the address of the buffer in our space.
3668 LEN is the number of bytes.
3670 Returns number of bytes transferred, or 0 for error. */
3672 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3673 remote targets) shouldn't attempt to read the entire buffer.
3674 Instead it should read a single packet worth of data and then
3675 return the byte size of that packet to the caller. The caller (its
3676 caller and its callers caller ;-) already contains code for
3677 handling partial reads. */
3680 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
3682 struct remote_state
*rs
= get_remote_state ();
3684 int max_buf_size
; /* Max size of packet output buffer. */
3687 max_buf_size
= get_memory_read_packet_size ();
3688 /* The packet buffer will be large enough for the payload;
3689 get_memory_packet_size ensures this. */
3699 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
3701 /* construct "m"<memaddr>","<len>" */
3702 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3703 memaddr
= remote_address_masked (memaddr
);
3706 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3708 p
+= hexnumstr (p
, (ULONGEST
) todo
);
3712 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3715 && isxdigit (buf
[1]) && isxdigit (buf
[2])
3718 /* There is no correspondance between what the remote
3719 protocol uses for errors and errno codes. We would like
3720 a cleaner way of representing errors (big enough to
3721 include errno codes, bfd_error codes, and others). But
3722 for now just return EIO. */
3727 /* Reply describes memory byte by byte,
3728 each byte encoded as two hex characters. */
3731 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
3733 /* Reply is short. This means that we were able to read
3734 only part of what we wanted to. */
3735 return i
+ (origlen
- len
);
3744 /* Read or write LEN bytes from inferior memory at MEMADDR,
3745 transferring to or from debugger address BUFFER. Write to inferior
3746 if SHOULD_WRITE is nonzero. Returns length of data written or
3747 read; 0 for error. TARGET is unused. */
3750 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
3751 int should_write
, struct mem_attrib
*attrib
,
3752 struct target_ops
*target
)
3754 CORE_ADDR targ_addr
;
3758 /* Should this be the selected frame? */
3759 gdbarch_remote_translate_xfer_address (current_gdbarch
,
3762 &targ_addr
, &targ_len
);
3767 res
= remote_write_bytes (targ_addr
, buffer
, targ_len
);
3769 res
= remote_read_bytes (targ_addr
, buffer
, targ_len
);
3775 remote_files_info (struct target_ops
*ignore
)
3777 puts_filtered ("Debugging a target over a serial line.\n");
3780 /* Stuff for dealing with the packets which are part of this protocol.
3781 See comment at top of file for details. */
3783 /* Read a single character from the remote end, masking it down to 7
3787 readchar (int timeout
)
3791 ch
= serial_readchar (remote_desc
, timeout
);
3796 switch ((enum serial_rc
) ch
)
3799 target_mourn_inferior ();
3800 error (_("Remote connection closed"));
3803 perror_with_name (_("Remote communication error"));
3805 case SERIAL_TIMEOUT
:
3811 /* Send the command in *BUF to the remote machine, and read the reply
3812 into *BUF. Report an error if we get an error reply. Resize
3813 *BUF using xrealloc if necessary to hold the result, and update
3817 remote_send (char **buf
,
3821 getpkt (buf
, sizeof_buf
, 0);
3823 if ((*buf
)[0] == 'E')
3824 error (_("Remote failure reply: %s"), *buf
);
3827 /* Display a null-terminated packet on stdout, for debugging, using C
3831 print_packet (char *buf
)
3833 puts_filtered ("\"");
3834 fputstr_filtered (buf
, '"', gdb_stdout
);
3835 puts_filtered ("\"");
3841 return putpkt_binary (buf
, strlen (buf
));
3844 /* Send a packet to the remote machine, with error checking. The data
3845 of the packet is in BUF. The string in BUF can be at most
3846 get_remote_packet_size () - 5 to account for the $, # and checksum,
3847 and for a possible /0 if we are debugging (remote_debug) and want
3848 to print the sent packet as a string. */
3851 putpkt_binary (char *buf
, int cnt
)
3854 unsigned char csum
= 0;
3855 char *buf2
= alloca (cnt
+ 6);
3861 /* Copy the packet into buffer BUF2, encapsulating it
3862 and giving it a checksum. */
3867 for (i
= 0; i
< cnt
; i
++)
3873 *p
++ = tohex ((csum
>> 4) & 0xf);
3874 *p
++ = tohex (csum
& 0xf);
3876 /* Send it over and over until we get a positive ack. */
3880 int started_error_output
= 0;
3885 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
3886 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
3887 fprintf_unfiltered (gdb_stdlog
, "...");
3888 gdb_flush (gdb_stdlog
);
3890 if (serial_write (remote_desc
, buf2
, p
- buf2
))
3891 perror_with_name (_("putpkt: write failed"));
3893 /* Read until either a timeout occurs (-2) or '+' is read. */
3896 ch
= readchar (remote_timeout
);
3904 case SERIAL_TIMEOUT
:
3906 if (started_error_output
)
3908 putchar_unfiltered ('\n');
3909 started_error_output
= 0;
3918 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
3922 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
3923 case SERIAL_TIMEOUT
:
3927 break; /* Retransmit buffer. */
3931 fprintf_unfiltered (gdb_stdlog
,
3932 "Packet instead of Ack, ignoring it\n");
3933 /* It's probably an old response sent because an ACK
3934 was lost. Gobble up the packet and ack it so it
3935 doesn't get retransmitted when we resend this
3938 serial_write (remote_desc
, "+", 1);
3939 continue; /* Now, go look for +. */
3944 if (!started_error_output
)
3946 started_error_output
= 1;
3947 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
3949 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
3953 break; /* Here to retransmit. */
3957 /* This is wrong. If doing a long backtrace, the user should be
3958 able to get out next time we call QUIT, without anything as
3959 violent as interrupt_query. If we want to provide a way out of
3960 here without getting to the next QUIT, it should be based on
3961 hitting ^C twice as in remote_wait. */
3971 /* Come here after finding the start of a frame when we expected an
3972 ack. Do our best to discard the rest of this packet. */
3981 c
= readchar (remote_timeout
);
3984 case SERIAL_TIMEOUT
:
3985 /* Nothing we can do. */
3988 /* Discard the two bytes of checksum and stop. */
3989 c
= readchar (remote_timeout
);
3991 c
= readchar (remote_timeout
);
3994 case '*': /* Run length encoding. */
3995 /* Discard the repeat count. */
3996 c
= readchar (remote_timeout
);
4001 /* A regular character. */
4007 /* Come here after finding the start of the frame. Collect the rest
4008 into *BUF, verifying the checksum, length, and handling run-length
4009 compression. NUL terminate the buffer. If there is not enough room,
4010 expand *BUF using xrealloc.
4012 Returns -1 on error, number of characters in buffer (ignoring the
4013 trailing NULL) on success. (could be extended to return one of the
4014 SERIAL status indications). */
4017 read_frame (char **buf_p
,
4030 c
= readchar (remote_timeout
);
4033 case SERIAL_TIMEOUT
:
4035 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
4039 fputs_filtered ("Saw new packet start in middle of old one\n",
4041 return -1; /* Start a new packet, count retries. */
4044 unsigned char pktcsum
;
4050 check_0
= readchar (remote_timeout
);
4052 check_1
= readchar (remote_timeout
);
4054 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
4057 fputs_filtered ("Timeout in checksum, retrying\n",
4061 else if (check_0
< 0 || check_1
< 0)
4064 fputs_filtered ("Communication error in checksum\n",
4069 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
4070 if (csum
== pktcsum
)
4075 fprintf_filtered (gdb_stdlog
,
4076 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4078 fputs_filtered (buf
, gdb_stdlog
);
4079 fputs_filtered ("\n", gdb_stdlog
);
4081 /* Number of characters in buffer ignoring trailing
4085 case '*': /* Run length encoding. */
4090 c
= readchar (remote_timeout
);
4092 repeat
= c
- ' ' + 3; /* Compute repeat count. */
4094 /* The character before ``*'' is repeated. */
4096 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
4098 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
4100 /* Make some more room in the buffer. */
4101 *sizeof_buf
+= repeat
;
4102 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
4106 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
4112 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
4116 if (bc
>= *sizeof_buf
- 1)
4118 /* Make some more room in the buffer. */
4120 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
4131 /* Read a packet from the remote machine, with error checking, and
4132 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4133 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4134 rather than timing out; this is used (in synchronous mode) to wait
4135 for a target that is is executing user code to stop. */
4136 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4137 don't have to change all the calls to getpkt to deal with the
4138 return value, because at the moment I don't know what the right
4139 thing to do it for those. */
4147 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
4151 /* Read a packet from the remote machine, with error checking, and
4152 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4153 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4154 rather than timing out; this is used (in synchronous mode) to wait
4155 for a target that is is executing user code to stop. If FOREVER ==
4156 0, this function is allowed to time out gracefully and return an
4157 indication of this to the caller. */
4159 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
4166 strcpy (*buf
, "timeout");
4170 timeout
= watchdog
> 0 ? watchdog
: -1;
4174 timeout
= remote_timeout
;
4178 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
4180 /* This can loop forever if the remote side sends us characters
4181 continuously, but if it pauses, we'll get a zero from
4182 readchar because of timeout. Then we'll count that as a
4185 /* Note that we will only wait forever prior to the start of a
4186 packet. After that, we expect characters to arrive at a
4187 brisk pace. They should show up within remote_timeout
4192 c
= readchar (timeout
);
4194 if (c
== SERIAL_TIMEOUT
)
4196 if (forever
) /* Watchdog went off? Kill the target. */
4199 target_mourn_inferior ();
4200 error (_("Watchdog has expired. Target detached."));
4203 fputs_filtered ("Timed out.\n", gdb_stdlog
);
4209 /* We've found the start of a packet, now collect the data. */
4211 val
= read_frame (buf
, sizeof_buf
);
4217 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
4218 fputstr_unfiltered (*buf
, 0, gdb_stdlog
);
4219 fprintf_unfiltered (gdb_stdlog
, "\n");
4221 serial_write (remote_desc
, "+", 1);
4225 /* Try the whole thing again. */
4227 serial_write (remote_desc
, "-", 1);
4230 /* We have tried hard enough, and just can't receive the packet.
4233 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
4234 serial_write (remote_desc
, "+", 1);
4241 /* For some mysterious reason, wait_for_inferior calls kill instead of
4242 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4246 target_mourn_inferior ();
4250 /* Use catch_errors so the user can quit from gdb even when we aren't on
4251 speaking terms with the remote system. */
4252 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4254 /* Don't wait for it to die. I'm not really sure it matters whether
4255 we do or not. For the existing stubs, kill is a noop. */
4256 target_mourn_inferior ();
4259 /* Async version of remote_kill. */
4261 remote_async_kill (void)
4263 /* Unregister the file descriptor from the event loop. */
4264 if (target_is_async_p ())
4265 serial_async (remote_desc
, NULL
, 0);
4267 /* For some mysterious reason, wait_for_inferior calls kill instead of
4268 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4272 target_mourn_inferior ();
4276 /* Use catch_errors so the user can quit from gdb even when we
4277 aren't on speaking terms with the remote system. */
4278 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4280 /* Don't wait for it to die. I'm not really sure it matters whether
4281 we do or not. For the existing stubs, kill is a noop. */
4282 target_mourn_inferior ();
4288 remote_mourn_1 (&remote_ops
);
4292 remote_async_mourn (void)
4294 remote_mourn_1 (&remote_async_ops
);
4298 extended_remote_mourn (void)
4300 /* We do _not_ want to mourn the target like this; this will
4301 remove the extended remote target from the target stack,
4302 and the next time the user says "run" it'll fail.
4304 FIXME: What is the right thing to do here? */
4306 remote_mourn_1 (&extended_remote_ops
);
4310 /* Worker function for remote_mourn. */
4312 remote_mourn_1 (struct target_ops
*target
)
4314 unpush_target (target
);
4315 generic_mourn_inferior ();
4318 /* In the extended protocol we want to be able to do things like
4319 "run" and have them basically work as expected. So we need
4320 a special create_inferior function.
4322 FIXME: One day add support for changing the exec file
4323 we're debugging, arguments and an environment. */
4326 extended_remote_create_inferior (char *exec_file
, char *args
,
4327 char **env
, int from_tty
)
4329 /* Rip out the breakpoints; we'll reinsert them after restarting
4330 the remote server. */
4331 remove_breakpoints ();
4333 /* Now restart the remote server. */
4334 extended_remote_restart ();
4336 /* Now put the breakpoints back in. This way we're safe if the
4337 restart function works via a unix fork on the remote side. */
4338 insert_breakpoints ();
4340 /* Clean up from the last time we were running. */
4341 clear_proceed_status ();
4344 /* Async version of extended_remote_create_inferior. */
4346 extended_remote_async_create_inferior (char *exec_file
, char *args
,
4347 char **env
, int from_tty
)
4349 /* Rip out the breakpoints; we'll reinsert them after restarting
4350 the remote server. */
4351 remove_breakpoints ();
4353 /* If running asynchronously, register the target file descriptor
4354 with the event loop. */
4355 if (target_can_async_p ())
4356 target_async (inferior_event_handler
, 0);
4358 /* Now restart the remote server. */
4359 extended_remote_restart ();
4361 /* Now put the breakpoints back in. This way we're safe if the
4362 restart function works via a unix fork on the remote side. */
4363 insert_breakpoints ();
4365 /* Clean up from the last time we were running. */
4366 clear_proceed_status ();
4370 /* On some machines, e.g. 68k, we may use a different breakpoint
4371 instruction than other targets; in those use
4372 DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4373 Also, bi-endian targets may define
4374 DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4375 DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we
4376 just call the standard routines that are in mem-break.c. */
4378 /* NOTE: cagney/2003-06-08: This is silly. A remote and simulator
4379 target should use an identical BREAKPOINT_FROM_PC. As for native,
4380 the ARCH-OS-tdep.c code can override the default. */
4382 #if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4383 #define DEPRECATED_REMOTE_BREAKPOINT
4386 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4388 /* If the target isn't bi-endian, just pretend it is. */
4389 #if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4390 #define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4391 #define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4394 static unsigned char big_break_insn
[] = DEPRECATED_BIG_REMOTE_BREAKPOINT
;
4395 static unsigned char little_break_insn
[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT
;
4397 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4399 /* Insert a breakpoint. On targets that have software breakpoint
4400 support, we ask the remote target to do the work; on targets
4401 which don't, we insert a traditional memory breakpoint. */
4404 remote_insert_breakpoint (struct bp_target_info
*bp_tgt
)
4406 CORE_ADDR addr
= bp_tgt
->placed_address
;
4407 struct remote_state
*rs
= get_remote_state ();
4408 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4412 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4413 If it succeeds, then set the support to PACKET_ENABLE. If it
4414 fails, and the user has explicitly requested the Z support then
4415 report an error, otherwise, mark it disabled and go on. */
4417 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
4424 BREAKPOINT_FROM_PC (&bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
4425 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
4426 p
+= hexnumstr (p
, addr
);
4427 sprintf (p
, ",%d", bp_tgt
->placed_size
);
4430 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4432 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
4438 case PACKET_UNKNOWN
:
4443 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4444 bp_tgt
->placed_size
= bp_tgt
->shadow_len
= sizeof big_break_insn
;
4445 val
= target_read_memory (addr
, bp_tgt
->shadow_contents
, bp_tgt
->shadow_len
);
4449 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
4450 val
= target_write_memory (addr
, (char *) big_break_insn
,
4451 sizeof big_break_insn
);
4453 val
= target_write_memory (addr
, (char *) little_break_insn
,
4454 sizeof little_break_insn
);
4459 return memory_insert_breakpoint (bp_tgt
);
4460 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4464 remote_remove_breakpoint (struct bp_target_info
*bp_tgt
)
4466 CORE_ADDR addr
= bp_tgt
->placed_address
;
4467 struct remote_state
*rs
= get_remote_state ();
4470 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
4478 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
4479 p
+= hexnumstr (p
, addr
);
4480 sprintf (p
, ",%d", bp_tgt
->placed_size
);
4483 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4485 return (rs
->buf
[0] == 'E');
4488 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4489 return target_write_memory (bp_tgt
->placed_address
, bp_tgt
->shadow_contents
,
4490 bp_tgt
->shadow_len
);
4492 return memory_remove_breakpoint (bp_tgt
);
4493 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4497 watchpoint_to_Z_packet (int type
)
4502 return Z_PACKET_WRITE_WP
;
4505 return Z_PACKET_READ_WP
;
4508 return Z_PACKET_ACCESS_WP
;
4511 internal_error (__FILE__
, __LINE__
,
4512 _("hw_bp_to_z: bad watchpoint type %d"), type
);
4517 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
4519 struct remote_state
*rs
= get_remote_state ();
4521 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
4523 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
4526 sprintf (rs
->buf
, "Z%x,", packet
);
4527 p
= strchr (rs
->buf
, '\0');
4528 addr
= remote_address_masked (addr
);
4529 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4530 sprintf (p
, ",%x", len
);
4533 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4535 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
4538 case PACKET_UNKNOWN
:
4543 internal_error (__FILE__
, __LINE__
,
4544 _("remote_insert_watchpoint: reached end of function"));
4549 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
4551 struct remote_state
*rs
= get_remote_state ();
4553 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
4555 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
4558 sprintf (rs
->buf
, "z%x,", packet
);
4559 p
= strchr (rs
->buf
, '\0');
4560 addr
= remote_address_masked (addr
);
4561 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4562 sprintf (p
, ",%x", len
);
4564 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4566 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
4569 case PACKET_UNKNOWN
:
4574 internal_error (__FILE__
, __LINE__
,
4575 _("remote_remove_watchpoint: reached end of function"));
4579 int remote_hw_watchpoint_limit
= -1;
4580 int remote_hw_breakpoint_limit
= -1;
4583 remote_check_watch_resources (int type
, int cnt
, int ot
)
4585 if (type
== bp_hardware_breakpoint
)
4587 if (remote_hw_breakpoint_limit
== 0)
4589 else if (remote_hw_breakpoint_limit
< 0)
4591 else if (cnt
<= remote_hw_breakpoint_limit
)
4596 if (remote_hw_watchpoint_limit
== 0)
4598 else if (remote_hw_watchpoint_limit
< 0)
4602 else if (cnt
<= remote_hw_watchpoint_limit
)
4609 remote_stopped_by_watchpoint (void)
4611 return remote_stopped_by_watchpoint_p
;
4614 extern int stepped_after_stopped_by_watchpoint
;
4617 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
4620 if (remote_stopped_by_watchpoint ()
4621 || stepped_after_stopped_by_watchpoint
)
4623 *addr_p
= remote_watch_data_address
;
4632 remote_insert_hw_breakpoint (struct bp_target_info
*bp_tgt
)
4635 struct remote_state
*rs
= get_remote_state ();
4638 /* The length field should be set to the size of a breakpoint
4639 instruction, even though we aren't inserting one ourselves. */
4641 BREAKPOINT_FROM_PC (&bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
4643 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
4650 addr
= remote_address_masked (bp_tgt
->placed_address
);
4651 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4652 sprintf (p
, ",%x", bp_tgt
->placed_size
);
4655 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4657 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
4660 case PACKET_UNKNOWN
:
4665 internal_error (__FILE__
, __LINE__
,
4666 _("remote_insert_hw_breakpoint: reached end of function"));
4671 remote_remove_hw_breakpoint (struct bp_target_info
*bp_tgt
)
4674 struct remote_state
*rs
= get_remote_state ();
4677 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
4684 addr
= remote_address_masked (bp_tgt
->placed_address
);
4685 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4686 sprintf (p
, ",%x", bp_tgt
->placed_size
);
4689 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4691 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
4694 case PACKET_UNKNOWN
:
4699 internal_error (__FILE__
, __LINE__
,
4700 _("remote_remove_hw_breakpoint: reached end of function"));
4703 /* Some targets are only capable of doing downloads, and afterwards
4704 they switch to the remote serial protocol. This function provides
4705 a clean way to get from the download target to the remote target.
4706 It's basically just a wrapper so that we don't have to expose any
4707 of the internal workings of remote.c.
4709 Prior to calling this routine, you should shutdown the current
4710 target code, else you will get the "A program is being debugged
4711 already..." message. Usually a call to pop_target() suffices. */
4714 push_remote_target (char *name
, int from_tty
)
4716 printf_filtered (_("Switching to remote protocol\n"));
4717 remote_open (name
, from_tty
);
4720 /* Table used by the crc32 function to calcuate the checksum. */
4722 static unsigned long crc32_table
[256] =
4725 static unsigned long
4726 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
4728 if (!crc32_table
[1])
4730 /* Initialize the CRC table and the decoding table. */
4734 for (i
= 0; i
< 256; i
++)
4736 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
4737 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
4744 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
4750 /* compare-sections command
4752 With no arguments, compares each loadable section in the exec bfd
4753 with the same memory range on the target, and reports mismatches.
4754 Useful for verifying the image on the target against the exec file.
4755 Depends on the target understanding the new "qCRC:" request. */
4757 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4758 target method (target verify memory) and generic version of the
4759 actual command. This will allow other high-level code (especially
4760 generic_load()) to make use of this target functionality. */
4763 compare_sections_command (char *args
, int from_tty
)
4765 struct remote_state
*rs
= get_remote_state ();
4767 unsigned long host_crc
, target_crc
;
4768 extern bfd
*exec_bfd
;
4769 struct cleanup
*old_chain
;
4772 const char *sectname
;
4779 error (_("command cannot be used without an exec file"));
4780 if (!current_target
.to_shortname
||
4781 strcmp (current_target
.to_shortname
, "remote") != 0)
4782 error (_("command can only be used with remote target"));
4784 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
4786 if (!(s
->flags
& SEC_LOAD
))
4787 continue; /* skip non-loadable section */
4789 size
= bfd_get_section_size (s
);
4791 continue; /* skip zero-length section */
4793 sectname
= bfd_get_section_name (exec_bfd
, s
);
4794 if (args
&& strcmp (args
, sectname
) != 0)
4795 continue; /* not the section selected by user */
4797 matched
= 1; /* do this section */
4799 /* FIXME: assumes lma can fit into long. */
4800 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
4801 (long) lma
, (long) size
);
4804 /* Be clever; compute the host_crc before waiting for target
4806 sectdata
= xmalloc (size
);
4807 old_chain
= make_cleanup (xfree
, sectdata
);
4808 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
4809 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
4811 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4812 if (rs
->buf
[0] == 'E')
4813 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
4814 sectname
, paddr (lma
), paddr (lma
+ size
));
4815 if (rs
->buf
[0] != 'C')
4816 error (_("remote target does not support this operation"));
4818 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
4819 target_crc
= target_crc
* 16 + fromhex (*tmp
);
4821 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4822 sectname
, paddr (lma
), paddr (lma
+ size
));
4823 if (host_crc
== target_crc
)
4824 printf_filtered ("matched.\n");
4827 printf_filtered ("MIS-MATCHED!\n");
4831 do_cleanups (old_chain
);
4834 warning (_("One or more sections of the remote executable does not match\n\
4835 the loaded file\n"));
4836 if (args
&& !matched
)
4837 printf_filtered (_("No loaded section named '%s'.\n"), args
);
4841 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4842 const char *annex
, gdb_byte
*readbuf
,
4843 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4845 struct remote_state
*rs
= get_remote_state ();
4850 /* Handle memory using remote_xfer_memory. */
4851 if (object
== TARGET_OBJECT_MEMORY
)
4856 if (writebuf
!= NULL
)
4858 void *buffer
= xmalloc (len
);
4859 struct cleanup
*cleanup
= make_cleanup (xfree
, buffer
);
4860 memcpy (buffer
, writebuf
, len
);
4861 xfered
= remote_xfer_memory (offset
, buffer
, len
, 1, NULL
, ops
);
4862 do_cleanups (cleanup
);
4865 xfered
= remote_xfer_memory (offset
, readbuf
, len
, 0, NULL
, ops
);
4869 else if (xfered
== 0 && errno
== 0)
4875 /* Only handle reads. */
4876 if (writebuf
!= NULL
|| readbuf
== NULL
)
4879 /* Map pre-existing objects onto letters. DO NOT do this for new
4880 objects!!! Instead specify new query packets. */
4883 case TARGET_OBJECT_KOD
:
4886 case TARGET_OBJECT_AVR
:
4890 case TARGET_OBJECT_AUXV
:
4891 if (remote_protocol_packets
[PACKET_qPart_auxv
].support
!= PACKET_DISABLE
)
4893 unsigned int total
= 0;
4896 LONGEST n
= min ((get_remote_packet_size () - 2) / 2, len
);
4897 snprintf (rs
->buf
, get_remote_packet_size (),
4898 "qPart:auxv:read::%s,%s",
4899 phex_nz (offset
, sizeof offset
),
4900 phex_nz (n
, sizeof n
));
4901 i
= putpkt (rs
->buf
);
4903 return total
> 0 ? total
: i
;
4905 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4906 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qPart_auxv
])
4908 return total
> 0 ? total
: -1;
4909 if (strcmp (rs
->buf
, "OK") == 0)
4910 break; /* Got EOF indicator. */
4911 /* Got some data. */
4912 i
= hex2bin (rs
->buf
, readbuf
, len
);
4915 readbuf
= (void *) ((char *) readbuf
+ i
);
4929 /* Note: a zero OFFSET and LEN can be used to query the minimum
4931 if (offset
== 0 && len
== 0)
4932 return (get_remote_packet_size ());
4933 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
4934 large enough let the caller deal with it. */
4935 if (len
< get_remote_packet_size ())
4937 len
= get_remote_packet_size ();
4939 /* Except for querying the minimum buffer size, target must be open. */
4941 error (_("remote query is only available after target open"));
4943 gdb_assert (annex
!= NULL
);
4944 gdb_assert (readbuf
!= NULL
);
4950 /* We used one buffer char for the remote protocol q command and
4951 another for the query type. As the remote protocol encapsulation
4952 uses 4 chars plus one extra in case we are debugging
4953 (remote_debug), we have PBUFZIZ - 7 left to pack the query
4956 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
4958 /* Bad caller may have sent forbidden characters. */
4959 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
4964 gdb_assert (annex
[i
] == '\0');
4966 i
= putpkt (rs
->buf
);
4970 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4971 strcpy ((char *) readbuf
, rs
->buf
);
4973 return strlen ((char *) readbuf
);
4977 remote_rcmd (char *command
,
4978 struct ui_file
*outbuf
)
4980 struct remote_state
*rs
= get_remote_state ();
4981 char *buf
= rs
->buf
;
4985 error (_("remote rcmd is only available after target open"));
4987 /* Send a NULL command across as an empty command. */
4988 if (command
== NULL
)
4991 /* The query prefix. */
4992 strcpy (buf
, "qRcmd,");
4993 p
= strchr (buf
, '\0');
4995 if ((strlen (buf
) + strlen (command
) * 2 + 8/*misc*/) > get_remote_packet_size ())
4996 error (_("\"monitor\" command ``%s'' is too long."), command
);
4998 /* Encode the actual command. */
4999 bin2hex ((gdb_byte
*) command
, p
, 0);
5001 if (putpkt (rs
->buf
) < 0)
5002 error (_("Communication problem with target."));
5004 /* get/display the response */
5007 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
5009 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5011 error (_("Target does not support this command."));
5012 if (buf
[0] == 'O' && buf
[1] != 'K')
5014 remote_console_output (buf
+ 1); /* 'O' message from stub. */
5017 if (strcmp (buf
, "OK") == 0)
5019 if (strlen (buf
) == 3 && buf
[0] == 'E'
5020 && isdigit (buf
[1]) && isdigit (buf
[2]))
5022 error (_("Protocol error with Rcmd"));
5024 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
5026 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
5027 fputc_unfiltered (c
, outbuf
);
5034 packet_command (char *args
, int from_tty
)
5036 struct remote_state
*rs
= get_remote_state ();
5039 error (_("command can only be used with remote target"));
5042 error (_("remote-packet command requires packet text as argument"));
5044 puts_filtered ("sending: ");
5045 print_packet (args
);
5046 puts_filtered ("\n");
5049 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5050 puts_filtered ("received: ");
5051 print_packet (rs
->buf
);
5052 puts_filtered ("\n");
5056 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
5058 static void display_thread_info (struct gdb_ext_thread_info
*info
);
5060 static void threadset_test_cmd (char *cmd
, int tty
);
5062 static void threadalive_test (char *cmd
, int tty
);
5064 static void threadlist_test_cmd (char *cmd
, int tty
);
5066 int get_and_display_threadinfo (threadref
*ref
);
5068 static void threadinfo_test_cmd (char *cmd
, int tty
);
5070 static int thread_display_step (threadref
*ref
, void *context
);
5072 static void threadlist_update_test_cmd (char *cmd
, int tty
);
5074 static void init_remote_threadtests (void);
5076 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
5079 threadset_test_cmd (char *cmd
, int tty
)
5081 int sample_thread
= SAMPLE_THREAD
;
5083 printf_filtered (_("Remote threadset test\n"));
5084 set_thread (sample_thread
, 1);
5089 threadalive_test (char *cmd
, int tty
)
5091 int sample_thread
= SAMPLE_THREAD
;
5093 if (remote_thread_alive (pid_to_ptid (sample_thread
)))
5094 printf_filtered ("PASS: Thread alive test\n");
5096 printf_filtered ("FAIL: Thread alive test\n");
5099 void output_threadid (char *title
, threadref
*ref
);
5102 output_threadid (char *title
, threadref
*ref
)
5106 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
5108 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
5112 threadlist_test_cmd (char *cmd
, int tty
)
5115 threadref nextthread
;
5116 int done
, result_count
;
5117 threadref threadlist
[3];
5119 printf_filtered ("Remote Threadlist test\n");
5120 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
5121 &result_count
, &threadlist
[0]))
5122 printf_filtered ("FAIL: threadlist test\n");
5125 threadref
*scan
= threadlist
;
5126 threadref
*limit
= scan
+ result_count
;
5128 while (scan
< limit
)
5129 output_threadid (" thread ", scan
++);
5134 display_thread_info (struct gdb_ext_thread_info
*info
)
5136 output_threadid ("Threadid: ", &info
->threadid
);
5137 printf_filtered ("Name: %s\n ", info
->shortname
);
5138 printf_filtered ("State: %s\n", info
->display
);
5139 printf_filtered ("other: %s\n\n", info
->more_display
);
5143 get_and_display_threadinfo (threadref
*ref
)
5147 struct gdb_ext_thread_info threadinfo
;
5149 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
5150 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
5151 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
5152 display_thread_info (&threadinfo
);
5157 threadinfo_test_cmd (char *cmd
, int tty
)
5159 int athread
= SAMPLE_THREAD
;
5163 int_to_threadref (&thread
, athread
);
5164 printf_filtered ("Remote Threadinfo test\n");
5165 if (!get_and_display_threadinfo (&thread
))
5166 printf_filtered ("FAIL cannot get thread info\n");
5170 thread_display_step (threadref
*ref
, void *context
)
5172 /* output_threadid(" threadstep ",ref); *//* simple test */
5173 return get_and_display_threadinfo (ref
);
5177 threadlist_update_test_cmd (char *cmd
, int tty
)
5179 printf_filtered ("Remote Threadlist update test\n");
5180 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
5184 init_remote_threadtests (void)
5186 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
5187 Fetch and print the remote list of thread identifiers, one pkt only"));
5188 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
5189 _("Fetch and display info about one thread"));
5190 add_com ("tset", class_obscure
, threadset_test_cmd
,
5191 _("Test setting to a different thread"));
5192 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
5193 _("Iterate through updating all remote thread info"));
5194 add_com ("talive", class_obscure
, threadalive_test
,
5195 _(" Remote thread alive test "));
5200 /* Convert a thread ID to a string. Returns the string in a static
5204 remote_pid_to_str (ptid_t ptid
)
5206 static char buf
[32];
5208 xsnprintf (buf
, sizeof buf
, "thread %d", ptid_get_pid (ptid
));
5212 /* Get the address of the thread local variable in OBJFILE which is
5213 stored at OFFSET within the thread local storage for thread PTID. */
5216 remote_get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
5218 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
5220 struct remote_state
*rs
= get_remote_state ();
5222 enum packet_result result
;
5224 strcpy (p
, "qGetTLSAddr:");
5226 p
+= hexnumstr (p
, PIDGET (ptid
));
5228 p
+= hexnumstr (p
, offset
);
5230 p
+= hexnumstr (p
, lm
);
5234 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5235 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
5236 if (result
== PACKET_OK
)
5240 unpack_varlen_hex (rs
->buf
, &result
);
5243 else if (result
== PACKET_UNKNOWN
)
5244 throw_error (TLS_GENERIC_ERROR
,
5245 _("Remote target doesn't support qGetTLSAddr packet"));
5247 throw_error (TLS_GENERIC_ERROR
,
5248 _("Remote target failed to process qGetTLSAddr request"));
5251 throw_error (TLS_GENERIC_ERROR
,
5252 _("TLS not supported or disabled on this target"));
5258 init_remote_ops (void)
5260 remote_ops
.to_shortname
= "remote";
5261 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
5263 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5264 Specify the serial device it is connected to\n\
5265 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
5266 remote_ops
.to_open
= remote_open
;
5267 remote_ops
.to_close
= remote_close
;
5268 remote_ops
.to_detach
= remote_detach
;
5269 remote_ops
.to_disconnect
= remote_disconnect
;
5270 remote_ops
.to_resume
= remote_resume
;
5271 remote_ops
.to_wait
= remote_wait
;
5272 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
5273 remote_ops
.to_store_registers
= remote_store_registers
;
5274 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5275 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
5276 remote_ops
.to_files_info
= remote_files_info
;
5277 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5278 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5279 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
5280 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
5281 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
5282 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
5283 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
5284 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
5285 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
5286 remote_ops
.to_kill
= remote_kill
;
5287 remote_ops
.to_load
= generic_load
;
5288 remote_ops
.to_mourn_inferior
= remote_mourn
;
5289 remote_ops
.to_thread_alive
= remote_thread_alive
;
5290 remote_ops
.to_find_new_threads
= remote_threads_info
;
5291 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
5292 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
5293 remote_ops
.to_stop
= remote_stop
;
5294 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
5295 remote_ops
.to_rcmd
= remote_rcmd
;
5296 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
5297 remote_ops
.to_stratum
= process_stratum
;
5298 remote_ops
.to_has_all_memory
= 1;
5299 remote_ops
.to_has_memory
= 1;
5300 remote_ops
.to_has_stack
= 1;
5301 remote_ops
.to_has_registers
= 1;
5302 remote_ops
.to_has_execution
= 1;
5303 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5304 remote_ops
.to_magic
= OPS_MAGIC
;
5307 /* Set up the extended remote vector by making a copy of the standard
5308 remote vector and adding to it. */
5311 init_extended_remote_ops (void)
5313 extended_remote_ops
= remote_ops
;
5315 extended_remote_ops
.to_shortname
= "extended-remote";
5316 extended_remote_ops
.to_longname
=
5317 "Extended remote serial target in gdb-specific protocol";
5318 extended_remote_ops
.to_doc
=
5319 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5320 Specify the serial device it is connected to (e.g. /dev/ttya).",
5321 extended_remote_ops
.to_open
= extended_remote_open
;
5322 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
5323 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5327 remote_can_async_p (void)
5329 /* We're async whenever the serial device is. */
5330 return (current_target
.to_async_mask_value
) && serial_can_async_p (remote_desc
);
5334 remote_is_async_p (void)
5336 /* We're async whenever the serial device is. */
5337 return (current_target
.to_async_mask_value
) && serial_is_async_p (remote_desc
);
5340 /* Pass the SERIAL event on and up to the client. One day this code
5341 will be able to delay notifying the client of an event until the
5342 point where an entire packet has been received. */
5344 static void (*async_client_callback
) (enum inferior_event_type event_type
,
5346 static void *async_client_context
;
5347 static serial_event_ftype remote_async_serial_handler
;
5350 remote_async_serial_handler (struct serial
*scb
, void *context
)
5352 /* Don't propogate error information up to the client. Instead let
5353 the client find out about the error by querying the target. */
5354 async_client_callback (INF_REG_EVENT
, async_client_context
);
5358 remote_async (void (*callback
) (enum inferior_event_type event_type
,
5359 void *context
), void *context
)
5361 if (current_target
.to_async_mask_value
== 0)
5362 internal_error (__FILE__
, __LINE__
,
5363 _("Calling remote_async when async is masked"));
5365 if (callback
!= NULL
)
5367 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
5368 async_client_callback
= callback
;
5369 async_client_context
= context
;
5372 serial_async (remote_desc
, NULL
, NULL
);
5375 /* Target async and target extended-async.
5377 This are temporary targets, until it is all tested. Eventually
5378 async support will be incorporated int the usual 'remote'
5382 init_remote_async_ops (void)
5384 remote_async_ops
.to_shortname
= "async";
5385 remote_async_ops
.to_longname
=
5386 "Remote serial target in async version of the gdb-specific protocol";
5387 remote_async_ops
.to_doc
=
5388 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5389 Specify the serial device it is connected to (e.g. /dev/ttya).";
5390 remote_async_ops
.to_open
= remote_async_open
;
5391 remote_async_ops
.to_close
= remote_close
;
5392 remote_async_ops
.to_detach
= remote_detach
;
5393 remote_async_ops
.to_disconnect
= remote_disconnect
;
5394 remote_async_ops
.to_resume
= remote_async_resume
;
5395 remote_async_ops
.to_wait
= remote_async_wait
;
5396 remote_async_ops
.to_fetch_registers
= remote_fetch_registers
;
5397 remote_async_ops
.to_store_registers
= remote_store_registers
;
5398 remote_async_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5399 remote_async_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
5400 remote_async_ops
.to_files_info
= remote_files_info
;
5401 remote_async_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5402 remote_async_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5403 remote_async_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
5404 remote_async_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
5405 remote_async_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
5406 remote_async_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
5407 remote_async_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
5408 remote_async_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
5409 remote_async_ops
.to_stopped_data_address
= remote_stopped_data_address
;
5410 remote_async_ops
.to_terminal_inferior
= remote_async_terminal_inferior
;
5411 remote_async_ops
.to_terminal_ours
= remote_async_terminal_ours
;
5412 remote_async_ops
.to_kill
= remote_async_kill
;
5413 remote_async_ops
.to_load
= generic_load
;
5414 remote_async_ops
.to_mourn_inferior
= remote_async_mourn
;
5415 remote_async_ops
.to_thread_alive
= remote_thread_alive
;
5416 remote_async_ops
.to_find_new_threads
= remote_threads_info
;
5417 remote_async_ops
.to_pid_to_str
= remote_pid_to_str
;
5418 remote_async_ops
.to_extra_thread_info
= remote_threads_extra_info
;
5419 remote_async_ops
.to_stop
= remote_stop
;
5420 remote_async_ops
.to_xfer_partial
= remote_xfer_partial
;
5421 remote_async_ops
.to_rcmd
= remote_rcmd
;
5422 remote_async_ops
.to_stratum
= process_stratum
;
5423 remote_async_ops
.to_has_all_memory
= 1;
5424 remote_async_ops
.to_has_memory
= 1;
5425 remote_async_ops
.to_has_stack
= 1;
5426 remote_async_ops
.to_has_registers
= 1;
5427 remote_async_ops
.to_has_execution
= 1;
5428 remote_async_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5429 remote_async_ops
.to_can_async_p
= remote_can_async_p
;
5430 remote_async_ops
.to_is_async_p
= remote_is_async_p
;
5431 remote_async_ops
.to_async
= remote_async
;
5432 remote_async_ops
.to_async_mask_value
= 1;
5433 remote_async_ops
.to_magic
= OPS_MAGIC
;
5436 /* Set up the async extended remote vector by making a copy of the standard
5437 remote vector and adding to it. */
5440 init_extended_async_remote_ops (void)
5442 extended_async_remote_ops
= remote_async_ops
;
5444 extended_async_remote_ops
.to_shortname
= "extended-async";
5445 extended_async_remote_ops
.to_longname
=
5446 "Extended remote serial target in async gdb-specific protocol";
5447 extended_async_remote_ops
.to_doc
=
5448 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5449 Specify the serial device it is connected to (e.g. /dev/ttya).",
5450 extended_async_remote_ops
.to_open
= extended_remote_async_open
;
5451 extended_async_remote_ops
.to_create_inferior
= extended_remote_async_create_inferior
;
5452 extended_async_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5456 set_remote_cmd (char *args
, int from_tty
)
5461 show_remote_cmd (char *args
, int from_tty
)
5463 /* We can't just use cmd_show_list here, because we want to skip
5464 the redundant "show remote Z-packet". */
5465 struct cleanup
*showlist_chain
;
5466 struct cmd_list_element
*list
= remote_show_cmdlist
;
5468 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
5469 for (; list
!= NULL
; list
= list
->next
)
5470 if (strcmp (list
->name
, "Z-packet") == 0)
5472 else if (list
->type
== show_cmd
)
5474 struct cleanup
*option_chain
5475 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
5476 ui_out_field_string (uiout
, "name", list
->name
);
5477 ui_out_text (uiout
, ": ");
5478 do_setshow_command ((char *) NULL
, from_tty
, list
);
5479 /* Close the tuple. */
5480 do_cleanups (option_chain
);
5485 build_remote_gdbarch_data (void)
5487 remote_address_size
= TARGET_ADDR_BIT
;
5490 /* Saved pointer to previous owner of the new_objfile event. */
5491 static void (*remote_new_objfile_chain
) (struct objfile
*);
5493 /* Function to be called whenever a new objfile (shlib) is detected. */
5495 remote_new_objfile (struct objfile
*objfile
)
5497 if (remote_desc
!= 0) /* Have a remote connection. */
5499 remote_check_symbols (objfile
);
5501 /* Call predecessor on chain, if any. */
5502 if (remote_new_objfile_chain
!= 0 &&
5504 remote_new_objfile_chain (objfile
);
5508 _initialize_remote (void)
5510 struct remote_state
*rs
;
5512 /* architecture specific data */
5513 remote_gdbarch_data_handle
=
5514 gdbarch_data_register_post_init (init_remote_state
);
5516 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5517 that the remote protocol has been initialized. */
5518 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size
);
5519 deprecated_register_gdbarch_swap (NULL
, 0, build_remote_gdbarch_data
);
5521 /* Initialize the per-target state. At the moment there is only one
5522 of these, not one per target. Only one target is active at a
5523 time. The default buffer size is unimportant; it will be expanded
5524 whenever a larger buffer is needed. */
5525 rs
= get_remote_state ();
5527 rs
->buf
= xmalloc (rs
->buf_size
);
5530 add_target (&remote_ops
);
5532 init_extended_remote_ops ();
5533 add_target (&extended_remote_ops
);
5535 init_remote_async_ops ();
5536 add_target (&remote_async_ops
);
5538 init_extended_async_remote_ops ();
5539 add_target (&extended_async_remote_ops
);
5541 /* Hook into new objfile notification. */
5542 remote_new_objfile_chain
= deprecated_target_new_objfile_hook
;
5543 deprecated_target_new_objfile_hook
= remote_new_objfile
;
5546 init_remote_threadtests ();
5549 /* set/show remote ... */
5551 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
5552 Remote protocol specific variables\n\
5553 Configure various remote-protocol specific variables such as\n\
5554 the packets being used"),
5555 &remote_set_cmdlist
, "set remote ",
5556 0 /* allow-unknown */, &setlist
);
5557 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
5558 Remote protocol specific variables\n\
5559 Configure various remote-protocol specific variables such as\n\
5560 the packets being used"),
5561 &remote_show_cmdlist
, "show remote ",
5562 0 /* allow-unknown */, &showlist
);
5564 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
5565 Compare section data on target to the exec file.\n\
5566 Argument is a single section name (default: all loaded sections)."),
5569 add_cmd ("packet", class_maintenance
, packet_command
, _("\
5570 Send an arbitrary packet to a remote target.\n\
5571 maintenance packet TEXT\n\
5572 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5573 this command sends the string TEXT to the inferior, and displays the\n\
5574 response packet. GDB supplies the initial `$' character, and the\n\
5575 terminating `#' character and checksum."),
5578 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
5579 Set whether to send break if interrupted."), _("\
5580 Show whether to send break if interrupted."), _("\
5581 If set, a break, instead of a cntrl-c, is sent to the remote target."),
5582 NULL
, NULL
, /* FIXME: i18n: Whether to send break if interrupted is %s. */
5583 &setlist
, &showlist
);
5585 /* Install commands for configuring memory read/write packets. */
5587 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
5588 Set the maximum number of bytes per memory write packet (deprecated)."),
5590 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
5591 Show the maximum number of bytes per memory write packet (deprecated)."),
5593 add_cmd ("memory-write-packet-size", no_class
,
5594 set_memory_write_packet_size
, _("\
5595 Set the maximum number of bytes per memory-write packet.\n\
5596 Specify the number of bytes in a packet or 0 (zero) for the\n\
5597 default packet size. The actual limit is further reduced\n\
5598 dependent on the target. Specify ``fixed'' to disable the\n\
5599 further restriction and ``limit'' to enable that restriction."),
5600 &remote_set_cmdlist
);
5601 add_cmd ("memory-read-packet-size", no_class
,
5602 set_memory_read_packet_size
, _("\
5603 Set the maximum number of bytes per memory-read packet.\n\
5604 Specify the number of bytes in a packet or 0 (zero) for the\n\
5605 default packet size. The actual limit is further reduced\n\
5606 dependent on the target. Specify ``fixed'' to disable the\n\
5607 further restriction and ``limit'' to enable that restriction."),
5608 &remote_set_cmdlist
);
5609 add_cmd ("memory-write-packet-size", no_class
,
5610 show_memory_write_packet_size
,
5611 _("Show the maximum number of bytes per memory-write packet."),
5612 &remote_show_cmdlist
);
5613 add_cmd ("memory-read-packet-size", no_class
,
5614 show_memory_read_packet_size
,
5615 _("Show the maximum number of bytes per memory-read packet."),
5616 &remote_show_cmdlist
);
5618 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
5619 &remote_hw_watchpoint_limit
, _("\
5620 Set the maximum number of target hardware watchpoints."), _("\
5621 Show the maximum number of target hardware watchpoints."), _("\
5622 Specify a negative limit for unlimited."),
5623 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
5624 &remote_set_cmdlist
, &remote_show_cmdlist
);
5625 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
5626 &remote_hw_breakpoint_limit
, _("\
5627 Set the maximum number of target hardware breakpoints."), _("\
5628 Show the maximum number of target hardware breakpoints."), _("\
5629 Specify a negative limit for unlimited."),
5630 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
5631 &remote_set_cmdlist
, &remote_show_cmdlist
);
5633 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
5634 &remote_address_size
, _("\
5635 Set the maximum size of the address (in bits) in a memory packet."), _("\
5636 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
5638 NULL
, /* FIXME: i18n: */
5639 &setlist
, &showlist
);
5641 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
5642 "X", "binary-download", 1);
5644 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
5645 "vCont", "verbose-resume", 0);
5647 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
5648 "qSymbol", "symbol-lookup", 0);
5650 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
5651 "P", "set-register", 1);
5653 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
5654 "p", "fetch-register", 1);
5656 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
5657 "Z0", "software-breakpoint", 0);
5659 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
5660 "Z1", "hardware-breakpoint", 0);
5662 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
5663 "Z2", "write-watchpoint", 0);
5665 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
5666 "Z3", "read-watchpoint", 0);
5668 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
5669 "Z4", "access-watchpoint", 0);
5671 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qPart_auxv
],
5672 "qPart:auxv", "read-aux-vector", 0);
5674 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
5675 "qGetTLSAddr", "get-thread-local-storage-address",
5678 /* Keep the old ``set remote Z-packet ...'' working. Each individual
5679 Z sub-packet has its own set and show commands, but users may
5680 have sets to this variable in their .gdbinit files (or in their
5682 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
5683 &remote_Z_packet_detect
, _("\
5684 Set use of remote protocol `Z' packets"), _("\
5685 Show use of remote protocol `Z' packets "), _("\
5686 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
5688 set_remote_protocol_Z_packet_cmd
,
5689 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
5690 &remote_set_cmdlist
, &remote_show_cmdlist
);
5692 /* Eventually initialize fileio. See fileio.c */
5693 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);