1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, 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"
49 #include "event-loop.h"
50 #include "event-top.h"
56 #include "gdbcore.h" /* for exec_bfd */
58 #include "remote-fileio.h"
60 /* Prototypes for local functions. */
61 static void cleanup_sigint_signal_handler (void *dummy
);
62 static void initialize_sigint_signal_handler (void);
63 static int getpkt_sane (char *buf
, long sizeof_buf
, int forever
);
65 static void handle_remote_sigint (int);
66 static void handle_remote_sigint_twice (int);
67 static void async_remote_interrupt (gdb_client_data
);
68 void async_remote_interrupt_twice (gdb_client_data
);
70 static void build_remote_gdbarch_data (void);
72 static void remote_files_info (struct target_ops
*ignore
);
74 static int remote_xfer_memory (CORE_ADDR memaddr
, char *myaddr
,
75 int len
, int should_write
,
76 struct mem_attrib
*attrib
,
77 struct target_ops
*target
);
79 static void remote_prepare_to_store (void);
81 static void remote_fetch_registers (int regno
);
83 static void remote_resume (ptid_t ptid
, int step
,
84 enum target_signal siggnal
);
85 static void remote_async_resume (ptid_t ptid
, int step
,
86 enum target_signal siggnal
);
87 static void remote_open (char *name
, int from_tty
);
88 static void remote_async_open (char *name
, int from_tty
);
90 static void extended_remote_open (char *name
, int from_tty
);
91 static void extended_remote_async_open (char *name
, int from_tty
);
93 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
,
96 static void remote_close (int quitting
);
98 static void remote_store_registers (int regno
);
100 static void remote_mourn (void);
101 static void remote_async_mourn (void);
103 static void extended_remote_restart (void);
105 static void extended_remote_mourn (void);
107 static void remote_mourn_1 (struct target_ops
*);
109 static void remote_send (char *buf
, long sizeof_buf
);
111 static int readchar (int timeout
);
113 static ptid_t
remote_wait (ptid_t ptid
,
114 struct target_waitstatus
*status
);
115 static ptid_t
remote_async_wait (ptid_t ptid
,
116 struct target_waitstatus
*status
);
118 static void remote_kill (void);
119 static void remote_async_kill (void);
121 static int tohex (int nib
);
123 static void remote_detach (char *args
, int from_tty
);
125 static void remote_interrupt (int signo
);
127 static void remote_interrupt_twice (int signo
);
129 static void interrupt_query (void);
131 static void set_thread (int, int);
133 static int remote_thread_alive (ptid_t
);
135 static void get_offsets (void);
137 static long read_frame (char *buf
, long sizeof_buf
);
139 static int remote_insert_breakpoint (CORE_ADDR
, char *);
141 static int remote_remove_breakpoint (CORE_ADDR
, char *);
143 static int hexnumlen (ULONGEST num
);
145 static void init_remote_ops (void);
147 static void init_extended_remote_ops (void);
149 static void remote_stop (void);
151 static int ishex (int ch
, int *val
);
153 static int stubhex (int ch
);
155 static int hexnumstr (char *, ULONGEST
);
157 static int hexnumnstr (char *, ULONGEST
, int);
159 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
161 static void print_packet (char *);
163 static unsigned long crc32 (unsigned char *, int, unsigned int);
165 static void compare_sections_command (char *, int);
167 static void packet_command (char *, int);
169 static int stub_unpack_int (char *buff
, int fieldlength
);
171 static ptid_t
remote_current_thread (ptid_t oldptid
);
173 static void remote_find_new_threads (void);
175 static void record_currthread (int currthread
);
177 static int fromhex (int a
);
179 static int hex2bin (const char *hex
, char *bin
, int count
);
181 static int bin2hex (const char *bin
, char *hex
, int count
);
183 static int putpkt_binary (char *buf
, int cnt
);
185 static void check_binary_download (CORE_ADDR addr
);
187 struct packet_config
;
189 static void show_packet_config_cmd (struct packet_config
*config
);
191 static void update_packet_config (struct packet_config
*config
);
193 void _initialize_remote (void);
195 /* Description of the remote protocol. Strictly speaking, when the
196 target is open()ed, remote.c should create a per-target description
197 of the remote protocol using that target's architecture.
198 Unfortunately, the target stack doesn't include local state. For
199 the moment keep the information in the target's architecture
204 long offset
; /* Offset into G packet. */
205 long regnum
; /* GDB's internal register number. */
206 LONGEST pnum
; /* Remote protocol register number. */
207 int in_g_packet
; /* Always part of G packet. */
208 /* long size in bytes; == register_size (current_gdbarch, regnum);
210 /* char *name; == REGISTER_NAME (regnum); at present. */
215 /* Description of the remote protocol registers. */
216 long sizeof_g_packet
;
218 /* Description of the remote protocol registers indexed by REGNUM
219 (making an array of NUM_REGS + NUM_PSEUDO_REGS in size). */
220 struct packet_reg
*regs
;
222 /* This is the size (in chars) of the first response to the ``g''
223 packet. It is used as a heuristic when determining the maximum
224 size of memory-read and memory-write packets. A target will
225 typically only reserve a buffer large enough to hold the ``g''
226 packet. The size does not include packet overhead (headers and
228 long actual_register_packet_size
;
230 /* This is the maximum size (in chars) of a non read/write packet.
231 It is also used as a cap on the size of read/write packets. */
232 long remote_packet_size
;
236 /* Handle for retreving the remote protocol data from gdbarch. */
237 static struct gdbarch_data
*remote_gdbarch_data_handle
;
239 static struct remote_state
*
240 get_remote_state (void)
242 return gdbarch_data (current_gdbarch
, remote_gdbarch_data_handle
);
246 init_remote_state (struct gdbarch
*gdbarch
)
249 struct remote_state
*rs
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_state
);
251 if (deprecated_register_bytes () != 0)
252 rs
->sizeof_g_packet
= deprecated_register_bytes ();
254 rs
->sizeof_g_packet
= 0;
256 /* Assume a 1:1 regnum<->pnum table. */
257 rs
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
, NUM_REGS
+ NUM_PSEUDO_REGS
,
259 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
261 struct packet_reg
*r
= &rs
->regs
[regnum
];
264 r
->offset
= DEPRECATED_REGISTER_BYTE (regnum
);
265 r
->in_g_packet
= (regnum
< NUM_REGS
);
266 /* ...name = REGISTER_NAME (regnum); */
268 /* Compute packet size by accumulating the size of all registers. */
269 if (deprecated_register_bytes () == 0)
270 rs
->sizeof_g_packet
+= register_size (current_gdbarch
, regnum
);
273 /* Default maximum number of characters in a packet body. Many
274 remote stubs have a hardwired buffer size of 400 bytes
275 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
276 as the maximum packet-size to ensure that the packet and an extra
277 NUL character can always fit in the buffer. This stops GDB
278 trashing stubs that try to squeeze an extra NUL into what is
279 already a full buffer (As of 1999-12-04 that was most stubs. */
280 rs
->remote_packet_size
= 400 - 1;
282 /* Should rs->sizeof_g_packet needs more space than the
283 default, adjust the size accordingly. Remember that each byte is
284 encoded as two characters. 32 is the overhead for the packet
285 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
286 (``$NN:G...#NN'') is a better guess, the below has been padded a
288 if (rs
->sizeof_g_packet
> ((rs
->remote_packet_size
- 32) / 2))
289 rs
->remote_packet_size
= (rs
->sizeof_g_packet
* 2 + 32);
291 /* This one is filled in when a ``g'' packet is received. */
292 rs
->actual_register_packet_size
= 0;
297 static struct packet_reg
*
298 packet_reg_from_regnum (struct remote_state
*rs
, long regnum
)
300 if (regnum
< 0 && regnum
>= NUM_REGS
+ NUM_PSEUDO_REGS
)
304 struct packet_reg
*r
= &rs
->regs
[regnum
];
305 gdb_assert (r
->regnum
== regnum
);
310 static struct packet_reg
*
311 packet_reg_from_pnum (struct remote_state
*rs
, LONGEST pnum
)
314 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
316 struct packet_reg
*r
= &rs
->regs
[i
];
323 /* FIXME: graces/2002-08-08: These variables should eventually be
324 bound to an instance of the target object (as in gdbarch-tdep()),
325 when such a thing exists. */
327 /* This is set to the data address of the access causing the target
328 to stop for a watchpoint. */
329 static CORE_ADDR remote_watch_data_address
;
331 /* This is non-zero if taregt stopped for a watchpoint. */
332 static int remote_stopped_by_watchpoint_p
;
335 static struct target_ops remote_ops
;
337 static struct target_ops extended_remote_ops
;
339 /* Temporary target ops. Just like the remote_ops and
340 extended_remote_ops, but with asynchronous support. */
341 static struct target_ops remote_async_ops
;
343 static struct target_ops extended_async_remote_ops
;
345 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
346 ``forever'' still use the normal timeout mechanism. This is
347 currently used by the ASYNC code to guarentee that target reads
348 during the initial connect always time-out. Once getpkt has been
349 modified to return a timeout indication and, in turn
350 remote_wait()/wait_for_inferior() have gained a timeout parameter
352 static int wait_forever_enabled_p
= 1;
355 /* This variable chooses whether to send a ^C or a break when the user
356 requests program interruption. Although ^C is usually what remote
357 systems expect, and that is the default here, sometimes a break is
358 preferable instead. */
360 static int remote_break
;
362 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
363 remote_open knows that we don't have a file open when the program
365 static struct serial
*remote_desc
= NULL
;
367 /* This variable sets the number of bits in an address that are to be
368 sent in a memory ("M" or "m") packet. Normally, after stripping
369 leading zeros, the entire address would be sent. This variable
370 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
371 initial implementation of remote.c restricted the address sent in
372 memory packets to ``host::sizeof long'' bytes - (typically 32
373 bits). Consequently, for 64 bit targets, the upper 32 bits of an
374 address was never sent. Since fixing this bug may cause a break in
375 some remote targets this variable is principly provided to
376 facilitate backward compatibility. */
378 static int remote_address_size
;
380 /* Tempoary to track who currently owns the terminal. See
381 target_async_terminal_* for more details. */
383 static int remote_async_terminal_ours_p
;
386 /* User configurable variables for the number of characters in a
387 memory read/write packet. MIN ((rs->remote_packet_size),
388 rs->sizeof_g_packet) is the default. Some targets need smaller
389 values (fifo overruns, et.al.) and some users need larger values
390 (speed up transfers). The variables ``preferred_*'' (the user
391 request), ``current_*'' (what was actually set) and ``forced_*''
392 (Positive - a soft limit, negative - a hard limit). */
394 struct memory_packet_config
401 /* Compute the current size of a read/write packet. Since this makes
402 use of ``actual_register_packet_size'' the computation is dynamic. */
405 get_memory_packet_size (struct memory_packet_config
*config
)
407 struct remote_state
*rs
= get_remote_state ();
408 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
409 law?) that some hosts don't cope very well with large alloca()
410 calls. Eventually the alloca() code will be replaced by calls to
411 xmalloc() and make_cleanups() allowing this restriction to either
412 be lifted or removed. */
413 #ifndef MAX_REMOTE_PACKET_SIZE
414 #define MAX_REMOTE_PACKET_SIZE 16384
416 /* NOTE: 20 ensures we can write at least one byte. */
417 #ifndef MIN_REMOTE_PACKET_SIZE
418 #define MIN_REMOTE_PACKET_SIZE 20
423 if (config
->size
<= 0)
424 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
426 what_they_get
= config
->size
;
430 what_they_get
= (rs
->remote_packet_size
);
431 /* Limit the packet to the size specified by the user. */
433 && what_they_get
> config
->size
)
434 what_they_get
= config
->size
;
435 /* Limit it to the size of the targets ``g'' response. */
436 if ((rs
->actual_register_packet_size
) > 0
437 && what_they_get
> (rs
->actual_register_packet_size
))
438 what_they_get
= (rs
->actual_register_packet_size
);
440 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
441 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
442 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
443 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
444 return what_they_get
;
447 /* Update the size of a read/write packet. If they user wants
448 something really big then do a sanity check. */
451 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
453 int fixed_p
= config
->fixed_p
;
454 long size
= config
->size
;
456 error (_("Argument required (integer, `fixed' or `limited')."));
457 else if (strcmp (args
, "hard") == 0
458 || strcmp (args
, "fixed") == 0)
460 else if (strcmp (args
, "soft") == 0
461 || strcmp (args
, "limit") == 0)
466 size
= strtoul (args
, &end
, 0);
468 error (_("Invalid %s (bad syntax)."), config
->name
);
470 /* Instead of explicitly capping the size of a packet to
471 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
472 instead allowed to set the size to something arbitrarily
474 if (size
> MAX_REMOTE_PACKET_SIZE
)
475 error (_("Invalid %s (too large)."), config
->name
);
479 if (fixed_p
&& !config
->fixed_p
)
481 if (! query (_("The target may not be able to correctly handle a %s\n"
482 "of %ld bytes. Change the packet size? "),
484 error (_("Packet size not changed."));
486 /* Update the config. */
487 config
->fixed_p
= fixed_p
;
492 show_memory_packet_size (struct memory_packet_config
*config
)
494 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
496 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
497 get_memory_packet_size (config
));
499 printf_filtered (_("Packets are limited to %ld bytes.\n"),
500 get_memory_packet_size (config
));
503 static struct memory_packet_config memory_write_packet_config
=
505 "memory-write-packet-size",
509 set_memory_write_packet_size (char *args
, int from_tty
)
511 set_memory_packet_size (args
, &memory_write_packet_config
);
515 show_memory_write_packet_size (char *args
, int from_tty
)
517 show_memory_packet_size (&memory_write_packet_config
);
521 get_memory_write_packet_size (void)
523 return get_memory_packet_size (&memory_write_packet_config
);
526 static struct memory_packet_config memory_read_packet_config
=
528 "memory-read-packet-size",
532 set_memory_read_packet_size (char *args
, int from_tty
)
534 set_memory_packet_size (args
, &memory_read_packet_config
);
538 show_memory_read_packet_size (char *args
, int from_tty
)
540 show_memory_packet_size (&memory_read_packet_config
);
544 get_memory_read_packet_size (void)
546 struct remote_state
*rs
= get_remote_state ();
547 long size
= get_memory_packet_size (&memory_read_packet_config
);
548 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
549 extra buffer size argument before the memory read size can be
550 increased beyond (rs->remote_packet_size). */
551 if (size
> (rs
->remote_packet_size
))
552 size
= (rs
->remote_packet_size
);
557 /* Generic configuration support for packets the stub optionally
558 supports. Allows the user to specify the use of the packet as well
559 as allowing GDB to auto-detect support in the remote stub. */
563 PACKET_SUPPORT_UNKNOWN
= 0,
572 enum auto_boolean detect
;
573 enum packet_support support
;
576 /* Analyze a packet's return value and update the packet config
587 update_packet_config (struct packet_config
*config
)
589 switch (config
->detect
)
591 case AUTO_BOOLEAN_TRUE
:
592 config
->support
= PACKET_ENABLE
;
594 case AUTO_BOOLEAN_FALSE
:
595 config
->support
= PACKET_DISABLE
;
597 case AUTO_BOOLEAN_AUTO
:
598 config
->support
= PACKET_SUPPORT_UNKNOWN
;
604 show_packet_config_cmd (struct packet_config
*config
)
606 char *support
= "internal-error";
607 switch (config
->support
)
613 support
= "disabled";
615 case PACKET_SUPPORT_UNKNOWN
:
619 switch (config
->detect
)
621 case AUTO_BOOLEAN_AUTO
:
622 printf_filtered (_("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n"),
623 config
->name
, config
->title
, support
);
625 case AUTO_BOOLEAN_TRUE
:
626 case AUTO_BOOLEAN_FALSE
:
627 printf_filtered (_("Support for remote protocol `%s' (%s) packet is currently %s.\n"),
628 config
->name
, config
->title
, support
);
634 add_packet_config_cmd (struct packet_config
*config
,
637 cmd_sfunc_ftype
*set_func
,
638 show_value_ftype
*show_func
,
639 struct cmd_list_element
**set_remote_list
,
640 struct cmd_list_element
**show_remote_list
,
648 config
->title
= title
;
649 config
->detect
= AUTO_BOOLEAN_AUTO
;
650 config
->support
= PACKET_SUPPORT_UNKNOWN
;
651 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
653 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
655 /* set/show TITLE-packet {auto,on,off} */
656 cmd_name
= xstrprintf ("%s-packet", title
);
657 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
658 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
660 set_remote_list
, show_remote_list
);
661 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
665 legacy_name
= xstrprintf ("%s-packet", name
);
666 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
668 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
673 static enum packet_result
674 packet_ok (const char *buf
, struct packet_config
*config
)
678 /* The stub recognized the packet request. Check that the
679 operation succeeded. */
680 switch (config
->support
)
682 case PACKET_SUPPORT_UNKNOWN
:
684 fprintf_unfiltered (gdb_stdlog
,
685 "Packet %s (%s) is supported\n",
686 config
->name
, config
->title
);
687 config
->support
= PACKET_ENABLE
;
690 internal_error (__FILE__
, __LINE__
,
691 _("packet_ok: attempt to use a disabled packet"));
696 if (buf
[0] == 'O' && buf
[1] == 'K' && buf
[2] == '\0')
697 /* "OK" - definitly OK. */
700 && isxdigit (buf
[1]) && isxdigit (buf
[2])
702 /* "Enn" - definitly an error. */
704 /* The packet may or may not be OK. Just assume it is. */
709 /* The stub does not support the packet. */
710 switch (config
->support
)
713 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
714 /* If the stub previously indicated that the packet was
715 supported then there is a protocol error.. */
716 error (_("Protocol error: %s (%s) conflicting enabled responses."),
717 config
->name
, config
->title
);
719 /* The user set it wrong. */
720 error (_("Enabled packet %s (%s) not recognized by stub"),
721 config
->name
, config
->title
);
723 case PACKET_SUPPORT_UNKNOWN
:
725 fprintf_unfiltered (gdb_stdlog
,
726 "Packet %s (%s) is NOT supported\n",
727 config
->name
, config
->title
);
728 config
->support
= PACKET_DISABLE
;
733 return PACKET_UNKNOWN
;
737 /* Should we try the 'vCont' (descriptive resume) request? */
738 static struct packet_config remote_protocol_vcont
;
741 set_remote_protocol_vcont_packet_cmd (char *args
, int from_tty
,
742 struct cmd_list_element
*c
)
744 update_packet_config (&remote_protocol_vcont
);
748 show_remote_protocol_vcont_packet_cmd (struct ui_file
*file
, int from_tty
,
749 struct cmd_list_element
*c
,
752 show_packet_config_cmd (&remote_protocol_vcont
);
755 /* Should we try the 'qSymbol' (target symbol lookup service) request? */
756 static struct packet_config remote_protocol_qSymbol
;
759 set_remote_protocol_qSymbol_packet_cmd (char *args
, int from_tty
,
760 struct cmd_list_element
*c
)
762 update_packet_config (&remote_protocol_qSymbol
);
766 show_remote_protocol_qSymbol_packet_cmd (struct ui_file
*file
, int from_tty
,
767 struct cmd_list_element
*c
,
770 show_packet_config_cmd (&remote_protocol_qSymbol
);
773 /* Should we try the 'P' (set register) request? */
775 static struct packet_config remote_protocol_P
;
778 set_remote_protocol_P_packet_cmd (char *args
, int from_tty
,
779 struct cmd_list_element
*c
)
781 update_packet_config (&remote_protocol_P
);
785 show_remote_protocol_P_packet_cmd (struct ui_file
*file
, int from_tty
,
786 struct cmd_list_element
*c
,
789 show_packet_config_cmd (&remote_protocol_P
);
792 /* Should we try one of the 'Z' requests? */
796 Z_PACKET_SOFTWARE_BP
,
797 Z_PACKET_HARDWARE_BP
,
804 static struct packet_config remote_protocol_Z
[NR_Z_PACKET_TYPES
];
806 /* FIXME: Instead of having all these boiler plate functions, the
807 command callback should include a context argument. */
810 set_remote_protocol_Z_software_bp_packet_cmd (char *args
, int from_tty
,
811 struct cmd_list_element
*c
)
813 update_packet_config (&remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
]);
817 show_remote_protocol_Z_software_bp_packet_cmd (struct ui_file
*file
, int from_tty
,
818 struct cmd_list_element
*c
,
821 show_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
]);
825 set_remote_protocol_Z_hardware_bp_packet_cmd (char *args
, int from_tty
,
826 struct cmd_list_element
*c
)
828 update_packet_config (&remote_protocol_Z
[Z_PACKET_HARDWARE_BP
]);
832 show_remote_protocol_Z_hardware_bp_packet_cmd (struct ui_file
*file
, int from_tty
,
833 struct cmd_list_element
*c
,
836 show_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_HARDWARE_BP
]);
840 set_remote_protocol_Z_write_wp_packet_cmd (char *args
, int from_tty
,
841 struct cmd_list_element
*c
)
843 update_packet_config (&remote_protocol_Z
[Z_PACKET_WRITE_WP
]);
847 show_remote_protocol_Z_write_wp_packet_cmd (struct ui_file
*file
, int from_tty
,
848 struct cmd_list_element
*c
,
851 show_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_WRITE_WP
]);
855 set_remote_protocol_Z_read_wp_packet_cmd (char *args
, int from_tty
,
856 struct cmd_list_element
*c
)
858 update_packet_config (&remote_protocol_Z
[Z_PACKET_READ_WP
]);
862 show_remote_protocol_Z_read_wp_packet_cmd (struct ui_file
*file
, int from_tty
,
863 struct cmd_list_element
*c
,
866 show_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_READ_WP
]);
870 set_remote_protocol_Z_access_wp_packet_cmd (char *args
, int from_tty
,
871 struct cmd_list_element
*c
)
873 update_packet_config (&remote_protocol_Z
[Z_PACKET_ACCESS_WP
]);
877 show_remote_protocol_Z_access_wp_packet_cmd (struct ui_file
*file
, int from_tty
,
878 struct cmd_list_element
*c
,
881 show_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_ACCESS_WP
]);
884 /* For compatibility with older distributions. Provide a ``set remote
885 Z-packet ...'' command that updates all the Z packet types. */
887 static enum auto_boolean remote_Z_packet_detect
;
890 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
891 struct cmd_list_element
*c
)
894 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
896 remote_protocol_Z
[i
].detect
= remote_Z_packet_detect
;
897 update_packet_config (&remote_protocol_Z
[i
]);
902 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
903 struct cmd_list_element
*c
,
907 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
909 show_packet_config_cmd (&remote_protocol_Z
[i
]);
913 /* Should we try the 'X' (remote binary download) packet?
915 This variable (available to the user via "set remote X-packet")
916 dictates whether downloads are sent in binary (via the 'X' packet).
917 We assume that the stub can, and attempt to do it. This will be
918 cleared if the stub does not understand it. This switch is still
919 needed, though in cases when the packet is supported in the stub,
920 but the connection does not allow it (i.e., 7-bit serial connection
923 static struct packet_config remote_protocol_binary_download
;
925 /* Should we try the 'ThreadInfo' query packet?
927 This variable (NOT available to the user: auto-detect only!)
928 determines whether GDB will use the new, simpler "ThreadInfo"
929 query or the older, more complex syntax for thread queries.
930 This is an auto-detect variable (set to true at each connect,
931 and set to false when the target fails to recognize it). */
933 static int use_threadinfo_query
;
934 static int use_threadextra_query
;
937 set_remote_protocol_binary_download_cmd (char *args
,
939 struct cmd_list_element
*c
)
941 update_packet_config (&remote_protocol_binary_download
);
945 show_remote_protocol_binary_download_cmd (struct ui_file
*file
, int from_tty
,
946 struct cmd_list_element
*c
,
949 show_packet_config_cmd (&remote_protocol_binary_download
);
952 /* Should we try the 'qPart:auxv' (target auxiliary vector read) request? */
953 static struct packet_config remote_protocol_qPart_auxv
;
956 set_remote_protocol_qPart_auxv_packet_cmd (char *args
, int from_tty
,
957 struct cmd_list_element
*c
)
959 update_packet_config (&remote_protocol_qPart_auxv
);
963 show_remote_protocol_qPart_auxv_packet_cmd (struct ui_file
*file
, int from_tty
,
964 struct cmd_list_element
*c
,
967 show_packet_config_cmd (&remote_protocol_qPart_auxv
);
970 static struct packet_config remote_protocol_p
;
973 set_remote_protocol_p_packet_cmd (char *args
, int from_tty
,
974 struct cmd_list_element
*c
)
976 update_packet_config (&remote_protocol_p
);
980 show_remote_protocol_p_packet_cmd (struct ui_file
*file
, int from_tty
,
981 struct cmd_list_element
*c
,
984 show_packet_config_cmd (&remote_protocol_p
);
989 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
990 static void *sigint_remote_twice_token
;
991 static void *sigint_remote_token
;
993 /* These are pointers to hook functions that may be set in order to
994 modify resume/wait behavior for a particular architecture. */
996 void (*deprecated_target_resume_hook
) (void);
997 void (*deprecated_target_wait_loop_hook
) (void);
1001 /* These are the threads which we last sent to the remote system.
1002 -1 for all or -2 for not sent yet. */
1003 static int general_thread
;
1004 static int continue_thread
;
1006 /* Call this function as a result of
1007 1) A halt indication (T packet) containing a thread id
1008 2) A direct query of currthread
1009 3) Successful execution of set thread
1013 record_currthread (int currthread
)
1015 general_thread
= currthread
;
1017 /* If this is a new thread, add it to GDB's thread list.
1018 If we leave it up to WFI to do this, bad things will happen. */
1019 if (!in_thread_list (pid_to_ptid (currthread
)))
1021 add_thread (pid_to_ptid (currthread
));
1022 ui_out_text (uiout
, "[New ");
1023 ui_out_text (uiout
, target_pid_to_str (pid_to_ptid (currthread
)));
1024 ui_out_text (uiout
, "]\n");
1028 #define MAGIC_NULL_PID 42000
1031 set_thread (int th
, int gen
)
1033 struct remote_state
*rs
= get_remote_state ();
1034 char *buf
= alloca (rs
->remote_packet_size
);
1035 int state
= gen
? general_thread
: continue_thread
;
1041 buf
[1] = gen
? 'g' : 'c';
1042 if (th
== MAGIC_NULL_PID
)
1048 sprintf (&buf
[2], "-%x", -th
);
1050 sprintf (&buf
[2], "%x", th
);
1052 getpkt (buf
, (rs
->remote_packet_size
), 0);
1054 general_thread
= th
;
1056 continue_thread
= th
;
1059 /* Return nonzero if the thread TH is still alive on the remote system. */
1062 remote_thread_alive (ptid_t ptid
)
1064 int tid
= PIDGET (ptid
);
1068 sprintf (buf
, "T-%08x", -tid
);
1070 sprintf (buf
, "T%08x", tid
);
1072 getpkt (buf
, sizeof (buf
), 0);
1073 return (buf
[0] == 'O' && buf
[1] == 'K');
1076 /* About these extended threadlist and threadinfo packets. They are
1077 variable length packets but, the fields within them are often fixed
1078 length. They are redundent enough to send over UDP as is the
1079 remote protocol in general. There is a matching unit test module
1082 #define OPAQUETHREADBYTES 8
1084 /* a 64 bit opaque identifier */
1085 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1087 /* WARNING: This threadref data structure comes from the remote O.S.,
1088 libstub protocol encoding, and remote.c. it is not particularly
1091 /* Right now, the internal structure is int. We want it to be bigger.
1095 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1097 /* gdb_ext_thread_info is an internal GDB data structure which is
1098 equivalint to the reply of the remote threadinfo packet. */
1100 struct gdb_ext_thread_info
1102 threadref threadid
; /* External form of thread reference. */
1103 int active
; /* Has state interesting to GDB?
1105 char display
[256]; /* Brief state display, name,
1106 blocked/syspended. */
1107 char shortname
[32]; /* To be used to name threads. */
1108 char more_display
[256]; /* Long info, statistics, queue depth,
1112 /* The volume of remote transfers can be limited by submitting
1113 a mask containing bits specifying the desired information.
1114 Use a union of these values as the 'selection' parameter to
1115 get_thread_info. FIXME: Make these TAG names more thread specific.
1118 #define TAG_THREADID 1
1119 #define TAG_EXISTS 2
1120 #define TAG_DISPLAY 4
1121 #define TAG_THREADNAME 8
1122 #define TAG_MOREDISPLAY 16
1124 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1126 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1128 static char *unpack_nibble (char *buf
, int *val
);
1130 static char *pack_nibble (char *buf
, int nibble
);
1132 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1134 static char *unpack_byte (char *buf
, int *value
);
1136 static char *pack_int (char *buf
, int value
);
1138 static char *unpack_int (char *buf
, int *value
);
1140 static char *unpack_string (char *src
, char *dest
, int length
);
1142 static char *pack_threadid (char *pkt
, threadref
*id
);
1144 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1146 void int_to_threadref (threadref
*id
, int value
);
1148 static int threadref_to_int (threadref
*ref
);
1150 static void copy_threadref (threadref
*dest
, threadref
*src
);
1152 static int threadmatch (threadref
*dest
, threadref
*src
);
1154 static char *pack_threadinfo_request (char *pkt
, int mode
,
1157 static int remote_unpack_thread_info_response (char *pkt
,
1158 threadref
*expectedref
,
1159 struct gdb_ext_thread_info
1163 static int remote_get_threadinfo (threadref
*threadid
,
1164 int fieldset
, /*TAG mask */
1165 struct gdb_ext_thread_info
*info
);
1167 static char *pack_threadlist_request (char *pkt
, int startflag
,
1169 threadref
*nextthread
);
1171 static int parse_threadlist_response (char *pkt
,
1173 threadref
*original_echo
,
1174 threadref
*resultlist
,
1177 static int remote_get_threadlist (int startflag
,
1178 threadref
*nextthread
,
1182 threadref
*threadlist
);
1184 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1186 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1187 void *context
, int looplimit
);
1189 static int remote_newthread_step (threadref
*ref
, void *context
);
1191 /* Encode 64 bits in 16 chars of hex. */
1193 static const char hexchars
[] = "0123456789abcdef";
1196 ishex (int ch
, int *val
)
1198 if ((ch
>= 'a') && (ch
<= 'f'))
1200 *val
= ch
- 'a' + 10;
1203 if ((ch
>= 'A') && (ch
<= 'F'))
1205 *val
= ch
- 'A' + 10;
1208 if ((ch
>= '0') && (ch
<= '9'))
1219 if (ch
>= 'a' && ch
<= 'f')
1220 return ch
- 'a' + 10;
1221 if (ch
>= '0' && ch
<= '9')
1223 if (ch
>= 'A' && ch
<= 'F')
1224 return ch
- 'A' + 10;
1229 stub_unpack_int (char *buff
, int fieldlength
)
1236 nibble
= stubhex (*buff
++);
1240 retval
= retval
<< 4;
1246 unpack_varlen_hex (char *buff
, /* packet to parse */
1252 while (ishex (*buff
, &nibble
))
1255 retval
= retval
<< 4;
1256 retval
|= nibble
& 0x0f;
1263 unpack_nibble (char *buf
, int *val
)
1265 ishex (*buf
++, val
);
1270 pack_nibble (char *buf
, int nibble
)
1272 *buf
++ = hexchars
[(nibble
& 0x0f)];
1277 pack_hex_byte (char *pkt
, int byte
)
1279 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1280 *pkt
++ = hexchars
[(byte
& 0xf)];
1285 unpack_byte (char *buf
, int *value
)
1287 *value
= stub_unpack_int (buf
, 2);
1292 pack_int (char *buf
, int value
)
1294 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1295 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1296 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1297 buf
= pack_hex_byte (buf
, (value
& 0xff));
1302 unpack_int (char *buf
, int *value
)
1304 *value
= stub_unpack_int (buf
, 8);
1308 #if 0 /* Currently unused, uncomment when needed. */
1309 static char *pack_string (char *pkt
, char *string
);
1312 pack_string (char *pkt
, char *string
)
1317 len
= strlen (string
);
1319 len
= 200; /* Bigger than most GDB packets, junk??? */
1320 pkt
= pack_hex_byte (pkt
, len
);
1324 if ((ch
== '\0') || (ch
== '#'))
1325 ch
= '*'; /* Protect encapsulation. */
1330 #endif /* 0 (unused) */
1333 unpack_string (char *src
, char *dest
, int length
)
1342 pack_threadid (char *pkt
, threadref
*id
)
1345 unsigned char *altid
;
1347 altid
= (unsigned char *) id
;
1348 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1350 pkt
= pack_hex_byte (pkt
, *altid
++);
1356 unpack_threadid (char *inbuf
, threadref
*id
)
1359 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1362 altref
= (char *) id
;
1364 while (inbuf
< limit
)
1366 x
= stubhex (*inbuf
++);
1367 y
= stubhex (*inbuf
++);
1368 *altref
++ = (x
<< 4) | y
;
1373 /* Externally, threadrefs are 64 bits but internally, they are still
1374 ints. This is due to a mismatch of specifications. We would like
1375 to use 64bit thread references internally. This is an adapter
1379 int_to_threadref (threadref
*id
, int value
)
1381 unsigned char *scan
;
1383 scan
= (unsigned char *) id
;
1389 *scan
++ = (value
>> 24) & 0xff;
1390 *scan
++ = (value
>> 16) & 0xff;
1391 *scan
++ = (value
>> 8) & 0xff;
1392 *scan
++ = (value
& 0xff);
1396 threadref_to_int (threadref
*ref
)
1399 unsigned char *scan
;
1401 scan
= (char *) ref
;
1405 value
= (value
<< 8) | ((*scan
++) & 0xff);
1410 copy_threadref (threadref
*dest
, threadref
*src
)
1413 unsigned char *csrc
, *cdest
;
1415 csrc
= (unsigned char *) src
;
1416 cdest
= (unsigned char *) dest
;
1423 threadmatch (threadref
*dest
, threadref
*src
)
1425 /* Things are broken right now, so just assume we got a match. */
1427 unsigned char *srcp
, *destp
;
1429 srcp
= (char *) src
;
1430 destp
= (char *) dest
;
1434 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1441 threadid:1, # always request threadid
1448 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1451 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1453 *pkt
++ = 'q'; /* Info Query */
1454 *pkt
++ = 'P'; /* process or thread info */
1455 pkt
= pack_int (pkt
, mode
); /* mode */
1456 pkt
= pack_threadid (pkt
, id
); /* threadid */
1457 *pkt
= '\0'; /* terminate */
1461 /* These values tag the fields in a thread info response packet. */
1462 /* Tagging the fields allows us to request specific fields and to
1463 add more fields as time goes by. */
1465 #define TAG_THREADID 1 /* Echo the thread identifier. */
1466 #define TAG_EXISTS 2 /* Is this process defined enough to
1467 fetch registers and its stack? */
1468 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1469 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1470 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1474 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1475 struct gdb_ext_thread_info
*info
)
1477 struct remote_state
*rs
= get_remote_state ();
1481 char *limit
= pkt
+ (rs
->remote_packet_size
); /* plausable parsing limit */
1484 /* info->threadid = 0; FIXME: implement zero_threadref. */
1486 info
->display
[0] = '\0';
1487 info
->shortname
[0] = '\0';
1488 info
->more_display
[0] = '\0';
1490 /* Assume the characters indicating the packet type have been
1492 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1493 pkt
= unpack_threadid (pkt
, &ref
);
1496 warning (_("Incomplete response to threadinfo request."));
1497 if (!threadmatch (&ref
, expectedref
))
1498 { /* This is an answer to a different request. */
1499 warning (_("ERROR RMT Thread info mismatch."));
1502 copy_threadref (&info
->threadid
, &ref
);
1504 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1506 /* Packets are terminated with nulls. */
1507 while ((pkt
< limit
) && mask
&& *pkt
)
1509 pkt
= unpack_int (pkt
, &tag
); /* tag */
1510 pkt
= unpack_byte (pkt
, &length
); /* length */
1511 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1513 warning (_("ERROR RMT: threadinfo tag mismatch."));
1517 if (tag
== TAG_THREADID
)
1521 warning (_("ERROR RMT: length of threadid is not 16."));
1525 pkt
= unpack_threadid (pkt
, &ref
);
1526 mask
= mask
& ~TAG_THREADID
;
1529 if (tag
== TAG_EXISTS
)
1531 info
->active
= stub_unpack_int (pkt
, length
);
1533 mask
= mask
& ~(TAG_EXISTS
);
1536 warning (_("ERROR RMT: 'exists' length too long."));
1542 if (tag
== TAG_THREADNAME
)
1544 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1545 mask
= mask
& ~TAG_THREADNAME
;
1548 if (tag
== TAG_DISPLAY
)
1550 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1551 mask
= mask
& ~TAG_DISPLAY
;
1554 if (tag
== TAG_MOREDISPLAY
)
1556 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1557 mask
= mask
& ~TAG_MOREDISPLAY
;
1560 warning (_("ERROR RMT: unknown thread info tag."));
1561 break; /* Not a tag we know about. */
1567 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1568 struct gdb_ext_thread_info
*info
)
1570 struct remote_state
*rs
= get_remote_state ();
1572 char *threadinfo_pkt
= alloca (rs
->remote_packet_size
);
1574 pack_threadinfo_request (threadinfo_pkt
, fieldset
, threadid
);
1575 putpkt (threadinfo_pkt
);
1576 getpkt (threadinfo_pkt
, (rs
->remote_packet_size
), 0);
1577 result
= remote_unpack_thread_info_response (threadinfo_pkt
+ 2,
1582 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1585 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1586 threadref
*nextthread
)
1588 *pkt
++ = 'q'; /* info query packet */
1589 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1590 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1591 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1592 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1597 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1600 parse_threadlist_response (char *pkt
, int result_limit
,
1601 threadref
*original_echo
, threadref
*resultlist
,
1604 struct remote_state
*rs
= get_remote_state ();
1606 int count
, resultcount
, done
;
1609 /* Assume the 'q' and 'M chars have been stripped. */
1610 limit
= pkt
+ ((rs
->remote_packet_size
) - BUF_THREAD_ID_SIZE
);
1611 /* done parse past here */
1612 pkt
= unpack_byte (pkt
, &count
); /* count field */
1613 pkt
= unpack_nibble (pkt
, &done
);
1614 /* The first threadid is the argument threadid. */
1615 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1616 while ((count
-- > 0) && (pkt
< limit
))
1618 pkt
= unpack_threadid (pkt
, resultlist
++);
1619 if (resultcount
++ >= result_limit
)
1628 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1629 int *done
, int *result_count
, threadref
*threadlist
)
1631 struct remote_state
*rs
= get_remote_state ();
1632 static threadref echo_nextthread
;
1633 char *threadlist_packet
= alloca (rs
->remote_packet_size
);
1634 char *t_response
= alloca (rs
->remote_packet_size
);
1637 /* Trancate result limit to be smaller than the packet size. */
1638 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= (rs
->remote_packet_size
))
1639 result_limit
= ((rs
->remote_packet_size
) / BUF_THREAD_ID_SIZE
) - 2;
1641 pack_threadlist_request (threadlist_packet
,
1642 startflag
, result_limit
, nextthread
);
1643 putpkt (threadlist_packet
);
1644 getpkt (t_response
, (rs
->remote_packet_size
), 0);
1647 parse_threadlist_response (t_response
+ 2, result_limit
, &echo_nextthread
,
1650 if (!threadmatch (&echo_nextthread
, nextthread
))
1652 /* FIXME: This is a good reason to drop the packet. */
1653 /* Possably, there is a duplicate response. */
1655 retransmit immediatly - race conditions
1656 retransmit after timeout - yes
1658 wait for packet, then exit
1660 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
1661 return 0; /* I choose simply exiting. */
1663 if (*result_count
<= 0)
1667 warning (_("RMT ERROR : failed to get remote thread list."));
1670 return result
; /* break; */
1672 if (*result_count
> result_limit
)
1675 warning (_("RMT ERROR: threadlist response longer than requested."));
1681 /* This is the interface between remote and threads, remotes upper
1684 /* remote_find_new_threads retrieves the thread list and for each
1685 thread in the list, looks up the thread in GDB's internal list,
1686 ading the thread if it does not already exist. This involves
1687 getting partial thread lists from the remote target so, polling the
1688 quit_flag is required. */
1691 /* About this many threadisds fit in a packet. */
1693 #define MAXTHREADLISTRESULTS 32
1696 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
1699 int done
, i
, result_count
;
1703 static threadref nextthread
;
1704 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1709 if (loopcount
++ > looplimit
)
1712 warning (_("Remote fetch threadlist -infinite loop-."));
1715 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1716 &done
, &result_count
, resultthreadlist
))
1721 /* Clear for later iterations. */
1723 /* Setup to resume next batch of thread references, set nextthread. */
1724 if (result_count
>= 1)
1725 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1727 while (result_count
--)
1728 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1735 remote_newthread_step (threadref
*ref
, void *context
)
1739 ptid
= pid_to_ptid (threadref_to_int (ref
));
1741 if (!in_thread_list (ptid
))
1743 return 1; /* continue iterator */
1746 #define CRAZY_MAX_THREADS 1000
1749 remote_current_thread (ptid_t oldpid
)
1751 struct remote_state
*rs
= get_remote_state ();
1752 char *buf
= alloca (rs
->remote_packet_size
);
1755 getpkt (buf
, (rs
->remote_packet_size
), 0);
1756 if (buf
[0] == 'Q' && buf
[1] == 'C')
1757 /* Use strtoul here, so we'll correctly parse values whose highest
1758 bit is set. The protocol carries them as a simple series of
1759 hex digits; in the absence of a sign, strtol will see such
1760 values as positive numbers out of range for signed 'long', and
1761 return LONG_MAX to indicate an overflow. */
1762 return pid_to_ptid (strtoul (&buf
[2], NULL
, 16));
1767 /* Find new threads for info threads command.
1768 * Original version, using John Metzler's thread protocol.
1772 remote_find_new_threads (void)
1774 remote_threadlist_iterator (remote_newthread_step
, 0,
1776 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
) /* ack ack ack */
1777 inferior_ptid
= remote_current_thread (inferior_ptid
);
1781 * Find all threads for info threads command.
1782 * Uses new thread protocol contributed by Cisco.
1783 * Falls back and attempts to use the older method (above)
1784 * if the target doesn't respond to the new method.
1788 remote_threads_info (void)
1790 struct remote_state
*rs
= get_remote_state ();
1791 char *buf
= alloca (rs
->remote_packet_size
);
1795 if (remote_desc
== 0) /* paranoia */
1796 error (_("Command can only be used when connected to the remote target."));
1798 if (use_threadinfo_query
)
1800 putpkt ("qfThreadInfo");
1802 getpkt (bufp
, (rs
->remote_packet_size
), 0);
1803 if (bufp
[0] != '\0') /* q packet recognized */
1805 while (*bufp
++ == 'm') /* reply contains one or more TID */
1809 /* Use strtoul here, so we'll correctly parse values
1810 whose highest bit is set. The protocol carries
1811 them as a simple series of hex digits; in the
1812 absence of a sign, strtol will see such values as
1813 positive numbers out of range for signed 'long',
1814 and return LONG_MAX to indicate an overflow. */
1815 tid
= strtoul (bufp
, &bufp
, 16);
1816 if (tid
!= 0 && !in_thread_list (pid_to_ptid (tid
)))
1817 add_thread (pid_to_ptid (tid
));
1819 while (*bufp
++ == ','); /* comma-separated list */
1820 putpkt ("qsThreadInfo");
1822 getpkt (bufp
, (rs
->remote_packet_size
), 0);
1828 /* Else fall back to old method based on jmetzler protocol. */
1829 use_threadinfo_query
= 0;
1830 remote_find_new_threads ();
1835 * Collect a descriptive string about the given thread.
1836 * The target may say anything it wants to about the thread
1837 * (typically info about its blocked / runnable state, name, etc.).
1838 * This string will appear in the info threads display.
1840 * Optional: targets are not required to implement this function.
1844 remote_threads_extra_info (struct thread_info
*tp
)
1846 struct remote_state
*rs
= get_remote_state ();
1850 struct gdb_ext_thread_info threadinfo
;
1851 static char display_buf
[100]; /* arbitrary... */
1852 char *bufp
= alloca (rs
->remote_packet_size
);
1853 int n
= 0; /* position in display_buf */
1855 if (remote_desc
== 0) /* paranoia */
1856 internal_error (__FILE__
, __LINE__
,
1857 _("remote_threads_extra_info"));
1859 if (use_threadextra_query
)
1861 sprintf (bufp
, "qThreadExtraInfo,%x", PIDGET (tp
->ptid
));
1863 getpkt (bufp
, (rs
->remote_packet_size
), 0);
1866 n
= min (strlen (bufp
) / 2, sizeof (display_buf
));
1867 result
= hex2bin (bufp
, display_buf
, n
);
1868 display_buf
[result
] = '\0';
1873 /* If the above query fails, fall back to the old method. */
1874 use_threadextra_query
= 0;
1875 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
1876 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
1877 int_to_threadref (&id
, PIDGET (tp
->ptid
));
1878 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
1879 if (threadinfo
.active
)
1881 if (*threadinfo
.shortname
)
1882 n
+= sprintf(&display_buf
[0], " Name: %s,", threadinfo
.shortname
);
1883 if (*threadinfo
.display
)
1884 n
+= sprintf(&display_buf
[n
], " State: %s,", threadinfo
.display
);
1885 if (*threadinfo
.more_display
)
1886 n
+= sprintf(&display_buf
[n
], " Priority: %s",
1887 threadinfo
.more_display
);
1891 /* For purely cosmetic reasons, clear up trailing commas. */
1892 if (',' == display_buf
[n
-1])
1893 display_buf
[n
-1] = ' ';
1902 /* Restart the remote side; this is an extended protocol operation. */
1905 extended_remote_restart (void)
1907 struct remote_state
*rs
= get_remote_state ();
1908 char *buf
= alloca (rs
->remote_packet_size
);
1910 /* Send the restart command; for reasons I don't understand the
1911 remote side really expects a number after the "R". */
1913 sprintf (&buf
[1], "%x", 0);
1916 /* Now query for status so this looks just like we restarted
1917 gdbserver from scratch. */
1919 getpkt (buf
, (rs
->remote_packet_size
), 0);
1922 /* Clean up connection to a remote debugger. */
1925 remote_close (int quitting
)
1928 serial_close (remote_desc
);
1932 /* Query the remote side for the text, data and bss offsets. */
1937 struct remote_state
*rs
= get_remote_state ();
1938 char *buf
= alloca (rs
->remote_packet_size
);
1941 CORE_ADDR text_addr
, data_addr
, bss_addr
;
1942 struct section_offsets
*offs
;
1944 putpkt ("qOffsets");
1946 getpkt (buf
, (rs
->remote_packet_size
), 0);
1948 if (buf
[0] == '\000')
1949 return; /* Return silently. Stub doesn't support
1953 warning (_("Remote failure reply: %s"), buf
);
1957 /* Pick up each field in turn. This used to be done with scanf, but
1958 scanf will make trouble if CORE_ADDR size doesn't match
1959 conversion directives correctly. The following code will work
1960 with any size of CORE_ADDR. */
1961 text_addr
= data_addr
= bss_addr
= 0;
1965 if (strncmp (ptr
, "Text=", 5) == 0)
1968 /* Don't use strtol, could lose on big values. */
1969 while (*ptr
&& *ptr
!= ';')
1970 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
1975 if (!lose
&& strncmp (ptr
, ";Data=", 6) == 0)
1978 while (*ptr
&& *ptr
!= ';')
1979 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
1984 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
1987 while (*ptr
&& *ptr
!= ';')
1988 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
1994 error (_("Malformed response to offset query, %s"), buf
);
1996 if (symfile_objfile
== NULL
)
1999 offs
= ((struct section_offsets
*)
2000 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
2001 memcpy (offs
, symfile_objfile
->section_offsets
,
2002 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
2004 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
2006 /* This is a temporary kludge to force data and bss to use the same offsets
2007 because that's what nlmconv does now. The real solution requires changes
2008 to the stub and remote.c that I don't have time to do right now. */
2010 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
2011 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
2013 objfile_relocate (symfile_objfile
, offs
);
2016 /* Stub for catch_errors. */
2019 remote_start_remote_dummy (struct ui_out
*uiout
, void *dummy
)
2021 start_remote (); /* Initialize gdb process mechanisms. */
2022 /* NOTE: Return something >=0. A -ve value is reserved for
2023 catch_exceptions. */
2028 remote_start_remote (struct ui_out
*uiout
, void *dummy
)
2030 immediate_quit
++; /* Allow user to interrupt it. */
2032 /* Ack any packet which the remote side has already sent. */
2033 serial_write (remote_desc
, "+", 1);
2035 /* Let the stub know that we want it to return the thread. */
2038 inferior_ptid
= remote_current_thread (inferior_ptid
);
2040 get_offsets (); /* Get text, data & bss offsets. */
2042 putpkt ("?"); /* Initiate a query from remote machine. */
2045 remote_start_remote_dummy (uiout
, dummy
);
2048 /* Open a connection to a remote debugger.
2049 NAME is the filename used for communication. */
2052 remote_open (char *name
, int from_tty
)
2054 remote_open_1 (name
, from_tty
, &remote_ops
, 0, 0);
2057 /* Just like remote_open, but with asynchronous support. */
2059 remote_async_open (char *name
, int from_tty
)
2061 remote_open_1 (name
, from_tty
, &remote_async_ops
, 0, 1);
2064 /* Open a connection to a remote debugger using the extended
2065 remote gdb protocol. NAME is the filename used for communication. */
2068 extended_remote_open (char *name
, int from_tty
)
2070 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */,
2074 /* Just like extended_remote_open, but with asynchronous support. */
2076 extended_remote_async_open (char *name
, int from_tty
)
2078 remote_open_1 (name
, from_tty
, &extended_async_remote_ops
,
2079 1 /*extended_p */, 1 /* async_p */);
2082 /* Generic code for opening a connection to a remote target. */
2085 init_all_packet_configs (void)
2088 update_packet_config (&remote_protocol_P
);
2089 update_packet_config (&remote_protocol_p
);
2090 update_packet_config (&remote_protocol_qSymbol
);
2091 update_packet_config (&remote_protocol_vcont
);
2092 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2093 update_packet_config (&remote_protocol_Z
[i
]);
2094 /* Force remote_write_bytes to check whether target supports binary
2096 update_packet_config (&remote_protocol_binary_download
);
2097 update_packet_config (&remote_protocol_qPart_auxv
);
2100 /* Symbol look-up. */
2103 remote_check_symbols (struct objfile
*objfile
)
2105 struct remote_state
*rs
= get_remote_state ();
2106 char *msg
, *reply
, *tmp
;
2107 struct minimal_symbol
*sym
;
2110 if (remote_protocol_qSymbol
.support
== PACKET_DISABLE
)
2113 msg
= alloca (rs
->remote_packet_size
);
2114 reply
= alloca (rs
->remote_packet_size
);
2116 /* Invite target to request symbol lookups. */
2118 putpkt ("qSymbol::");
2119 getpkt (reply
, (rs
->remote_packet_size
), 0);
2120 packet_ok (reply
, &remote_protocol_qSymbol
);
2122 while (strncmp (reply
, "qSymbol:", 8) == 0)
2125 end
= hex2bin (tmp
, msg
, strlen (tmp
) / 2);
2127 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
2129 sprintf (msg
, "qSymbol::%s", &reply
[8]);
2131 sprintf (msg
, "qSymbol:%s:%s",
2132 paddr_nz (SYMBOL_VALUE_ADDRESS (sym
)),
2135 getpkt (reply
, (rs
->remote_packet_size
), 0);
2139 static struct serial
*
2140 remote_serial_open (char *name
)
2142 static int udp_warning
= 0;
2144 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2145 of in ser-tcp.c, because it is the remote protocol assuming that the
2146 serial connection is reliable and not the serial connection promising
2148 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
2151 The remote protocol may be unreliable over UDP.\n\
2152 Some events may be lost, rendering further debugging impossible."));
2156 return serial_open (name
);
2160 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
,
2161 int extended_p
, int async_p
)
2163 struct exception ex
;
2164 struct remote_state
*rs
= get_remote_state ();
2166 error (_("To open a remote debug connection, you need to specify what\n"
2167 "serial device is attached to the remote system\n"
2168 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
2170 /* See FIXME above. */
2172 wait_forever_enabled_p
= 1;
2174 reopen_exec_file ();
2177 target_preopen (from_tty
);
2179 unpush_target (target
);
2181 remote_desc
= remote_serial_open (name
);
2183 perror_with_name (name
);
2185 if (baud_rate
!= -1)
2187 if (serial_setbaudrate (remote_desc
, baud_rate
))
2189 /* The requested speed could not be set. Error out to
2190 top level after closing remote_desc. Take care to
2191 set remote_desc to NULL to avoid closing remote_desc
2193 serial_close (remote_desc
);
2195 perror_with_name (name
);
2199 serial_raw (remote_desc
);
2201 /* If there is something sitting in the buffer we might take it as a
2202 response to a command, which would be bad. */
2203 serial_flush_input (remote_desc
);
2207 puts_filtered ("Remote debugging using ");
2208 puts_filtered (name
);
2209 puts_filtered ("\n");
2211 push_target (target
); /* Switch to using remote target now. */
2213 init_all_packet_configs ();
2215 general_thread
= -2;
2216 continue_thread
= -2;
2218 /* Probe for ability to use "ThreadInfo" query, as required. */
2219 use_threadinfo_query
= 1;
2220 use_threadextra_query
= 1;
2222 /* Without this, some commands which require an active target (such
2223 as kill) won't work. This variable serves (at least) double duty
2224 as both the pid of the target process (if it has such), and as a
2225 flag indicating that a target is active. These functions should
2226 be split out into seperate variables, especially since GDB will
2227 someday have a notion of debugging several processes. */
2229 inferior_ptid
= pid_to_ptid (MAGIC_NULL_PID
);
2233 /* With this target we start out by owning the terminal. */
2234 remote_async_terminal_ours_p
= 1;
2236 /* FIXME: cagney/1999-09-23: During the initial connection it is
2237 assumed that the target is already ready and able to respond to
2238 requests. Unfortunately remote_start_remote() eventually calls
2239 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2240 around this. Eventually a mechanism that allows
2241 wait_for_inferior() to expect/get timeouts will be
2243 wait_forever_enabled_p
= 0;
2246 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2247 /* First delete any symbols previously loaded from shared libraries. */
2248 no_shared_libraries (NULL
, 0);
2251 /* Start the remote connection. If error() or QUIT, discard this
2252 target (we'd otherwise be in an inconsistent state) and then
2253 propogate the error on up the exception chain. This ensures that
2254 the caller doesn't stumble along blindly assuming that the
2255 function succeeded. The CLI doesn't have this problem but other
2256 UI's, such as MI do.
2258 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2259 this function should return an error indication letting the
2260 caller restore the previous state. Unfortunately the command
2261 ``target remote'' is directly wired to this function making that
2262 impossible. On a positive note, the CLI side of this problem has
2263 been fixed - the function set_cmd_context() makes it possible for
2264 all the ``target ....'' commands to share a common callback
2265 function. See cli-dump.c. */
2266 ex
= catch_exception (uiout
, remote_start_remote
, NULL
, RETURN_MASK_ALL
);
2271 wait_forever_enabled_p
= 1;
2272 throw_exception (ex
);
2276 wait_forever_enabled_p
= 1;
2280 /* Tell the remote that we are using the extended protocol. */
2281 char *buf
= alloca (rs
->remote_packet_size
);
2283 getpkt (buf
, (rs
->remote_packet_size
), 0);
2285 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2286 /* FIXME: need a master target_open vector from which all
2287 remote_opens can be called, so that stuff like this can
2288 go there. Failing that, the following code must be copied
2289 to the open function for any remote target that wants to
2290 support svr4 shared libraries. */
2292 /* Set up to detect and load shared libraries. */
2293 if (exec_bfd
) /* No use without an exec file. */
2295 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid
));
2296 remote_check_symbols (symfile_objfile
);
2300 observer_notify_inferior_created (¤t_target
, from_tty
);
2303 /* This takes a program previously attached to and detaches it. After
2304 this is done, GDB can be used to debug some other program. We
2305 better not have left any breakpoints in the target program or it'll
2306 die when it hits one. */
2309 remote_detach (char *args
, int from_tty
)
2311 struct remote_state
*rs
= get_remote_state ();
2312 char *buf
= alloca (rs
->remote_packet_size
);
2315 error (_("Argument given to \"detach\" when remotely debugging."));
2317 /* Tell the remote target to detach. */
2319 remote_send (buf
, (rs
->remote_packet_size
));
2321 /* Unregister the file descriptor from the event loop. */
2322 if (target_is_async_p ())
2323 serial_async (remote_desc
, NULL
, 0);
2325 target_mourn_inferior ();
2327 puts_filtered ("Ending remote debugging.\n");
2330 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2333 remote_disconnect (char *args
, int from_tty
)
2335 struct remote_state
*rs
= get_remote_state ();
2336 char *buf
= alloca (rs
->remote_packet_size
);
2339 error (_("Argument given to \"detach\" when remotely debugging."));
2341 /* Unregister the file descriptor from the event loop. */
2342 if (target_is_async_p ())
2343 serial_async (remote_desc
, NULL
, 0);
2345 target_mourn_inferior ();
2347 puts_filtered ("Ending remote debugging.\n");
2350 /* Convert hex digit A to a number. */
2355 if (a
>= '0' && a
<= '9')
2357 else if (a
>= 'a' && a
<= 'f')
2358 return a
- 'a' + 10;
2359 else if (a
>= 'A' && a
<= 'F')
2360 return a
- 'A' + 10;
2362 error (_("Reply contains invalid hex digit %d"), a
);
2366 hex2bin (const char *hex
, char *bin
, int count
)
2370 for (i
= 0; i
< count
; i
++)
2372 if (hex
[0] == 0 || hex
[1] == 0)
2374 /* Hex string is short, or of uneven length.
2375 Return the count that has been converted so far. */
2378 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
2384 /* Convert number NIB to a hex digit. */
2392 return 'a' + nib
- 10;
2396 bin2hex (const char *bin
, char *hex
, int count
)
2399 /* May use a length, or a nul-terminated string as input. */
2401 count
= strlen (bin
);
2403 for (i
= 0; i
< count
; i
++)
2405 *hex
++ = tohex ((*bin
>> 4) & 0xf);
2406 *hex
++ = tohex (*bin
++ & 0xf);
2412 /* Check for the availability of vCont. This function should also check
2416 remote_vcont_probe (struct remote_state
*rs
, char *buf
)
2418 strcpy (buf
, "vCont?");
2420 getpkt (buf
, rs
->remote_packet_size
, 0);
2422 /* Make sure that the features we assume are supported. */
2423 if (strncmp (buf
, "vCont", 5) == 0)
2426 int support_s
, support_S
, support_c
, support_C
;
2432 while (p
&& *p
== ';')
2435 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2437 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2439 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2441 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2444 p
= strchr (p
, ';');
2447 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2448 BUF will make packet_ok disable the packet. */
2449 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
2453 packet_ok (buf
, &remote_protocol_vcont
);
2456 /* Resume the remote inferior by using a "vCont" packet. The thread
2457 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2458 resumed thread should be single-stepped and/or signalled. If PTID's
2459 PID is -1, then all threads are resumed; the thread to be stepped and/or
2460 signalled is given in the global INFERIOR_PTID. This function returns
2461 non-zero iff it resumes the inferior.
2463 This function issues a strict subset of all possible vCont commands at the
2467 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2469 struct remote_state
*rs
= get_remote_state ();
2470 int pid
= PIDGET (ptid
);
2471 char *buf
= NULL
, *outbuf
;
2472 struct cleanup
*old_cleanup
;
2474 buf
= xmalloc (rs
->remote_packet_size
);
2475 old_cleanup
= make_cleanup (xfree
, buf
);
2477 if (remote_protocol_vcont
.support
== PACKET_SUPPORT_UNKNOWN
)
2478 remote_vcont_probe (rs
, buf
);
2480 if (remote_protocol_vcont
.support
== PACKET_DISABLE
)
2482 do_cleanups (old_cleanup
);
2486 /* If we could generate a wider range of packets, we'd have to worry
2487 about overflowing BUF. Should there be a generic
2488 "multi-part-packet" packet? */
2490 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
)
2492 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2493 don't have any PID numbers the inferior will understand. Make sure
2494 to only send forms that do not specify a PID. */
2495 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2496 outbuf
= xstrprintf ("vCont;S%02x", siggnal
);
2498 outbuf
= xstrprintf ("vCont;s");
2499 else if (siggnal
!= TARGET_SIGNAL_0
)
2500 outbuf
= xstrprintf ("vCont;C%02x", siggnal
);
2502 outbuf
= xstrprintf ("vCont;c");
2506 /* Resume all threads, with preference for INFERIOR_PTID. */
2507 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2508 outbuf
= xstrprintf ("vCont;S%02x:%x;c", siggnal
,
2509 PIDGET (inferior_ptid
));
2511 outbuf
= xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid
));
2512 else if (siggnal
!= TARGET_SIGNAL_0
)
2513 outbuf
= xstrprintf ("vCont;C%02x:%x;c", siggnal
,
2514 PIDGET (inferior_ptid
));
2516 outbuf
= xstrprintf ("vCont;c");
2520 /* Scheduler locking; resume only PTID. */
2521 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2522 outbuf
= xstrprintf ("vCont;S%02x:%x", siggnal
, pid
);
2524 outbuf
= xstrprintf ("vCont;s:%x", pid
);
2525 else if (siggnal
!= TARGET_SIGNAL_0
)
2526 outbuf
= xstrprintf ("vCont;C%02x:%x", siggnal
, pid
);
2528 outbuf
= xstrprintf ("vCont;c:%x", pid
);
2531 gdb_assert (outbuf
&& strlen (outbuf
) < rs
->remote_packet_size
);
2532 make_cleanup (xfree
, outbuf
);
2536 do_cleanups (old_cleanup
);
2541 /* Tell the remote machine to resume. */
2543 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
2545 static int last_sent_step
;
2548 remote_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2550 struct remote_state
*rs
= get_remote_state ();
2551 char *buf
= alloca (rs
->remote_packet_size
);
2552 int pid
= PIDGET (ptid
);
2555 last_sent_signal
= siggnal
;
2556 last_sent_step
= step
;
2558 /* A hook for when we need to do something at the last moment before
2560 if (deprecated_target_resume_hook
)
2561 (*deprecated_target_resume_hook
) ();
2563 /* The vCont packet doesn't need to specify threads via Hc. */
2564 if (remote_vcont_resume (ptid
, step
, siggnal
))
2567 /* All other supported resume packets do use Hc, so call set_thread. */
2569 set_thread (0, 0); /* Run any thread. */
2571 set_thread (pid
, 0); /* Run this thread. */
2573 if (siggnal
!= TARGET_SIGNAL_0
)
2575 buf
[0] = step
? 'S' : 'C';
2576 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2577 buf
[2] = tohex (((int) siggnal
) & 0xf);
2581 strcpy (buf
, step
? "s" : "c");
2586 /* Same as remote_resume, but with async support. */
2588 remote_async_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2590 remote_resume (ptid
, step
, siggnal
);
2592 /* We are about to start executing the inferior, let's register it
2593 with the event loop. NOTE: this is the one place where all the
2594 execution commands end up. We could alternatively do this in each
2595 of the execution commands in infcmd.c. */
2596 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2597 into infcmd.c in order to allow inferior function calls to work
2598 NOT asynchronously. */
2599 if (target_can_async_p ())
2600 target_async (inferior_event_handler
, 0);
2601 /* Tell the world that the target is now executing. */
2602 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2603 this? Instead, should the client of target just assume (for
2604 async targets) that the target is going to start executing? Is
2605 this information already found in the continuation block? */
2606 if (target_is_async_p ())
2607 target_executing
= 1;
2611 /* Set up the signal handler for SIGINT, while the target is
2612 executing, ovewriting the 'regular' SIGINT signal handler. */
2614 initialize_sigint_signal_handler (void)
2616 sigint_remote_token
=
2617 create_async_signal_handler (async_remote_interrupt
, NULL
);
2618 signal (SIGINT
, handle_remote_sigint
);
2621 /* Signal handler for SIGINT, while the target is executing. */
2623 handle_remote_sigint (int sig
)
2625 signal (sig
, handle_remote_sigint_twice
);
2626 sigint_remote_twice_token
=
2627 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
2628 mark_async_signal_handler_wrapper (sigint_remote_token
);
2631 /* Signal handler for SIGINT, installed after SIGINT has already been
2632 sent once. It will take effect the second time that the user sends
2635 handle_remote_sigint_twice (int sig
)
2637 signal (sig
, handle_sigint
);
2638 sigint_remote_twice_token
=
2639 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
2640 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
2643 /* Perform the real interruption of the target execution, in response
2646 async_remote_interrupt (gdb_client_data arg
)
2649 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2654 /* Perform interrupt, if the first attempt did not succeed. Just give
2655 up on the target alltogether. */
2657 async_remote_interrupt_twice (gdb_client_data arg
)
2660 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
2661 /* Do something only if the target was not killed by the previous
2663 if (target_executing
)
2666 signal (SIGINT
, handle_remote_sigint
);
2670 /* Reinstall the usual SIGINT handlers, after the target has
2673 cleanup_sigint_signal_handler (void *dummy
)
2675 signal (SIGINT
, handle_sigint
);
2676 if (sigint_remote_twice_token
)
2677 delete_async_signal_handler ((struct async_signal_handler
**)
2678 &sigint_remote_twice_token
);
2679 if (sigint_remote_token
)
2680 delete_async_signal_handler ((struct async_signal_handler
**)
2681 &sigint_remote_token
);
2684 /* Send ^C to target to halt it. Target will respond, and send us a
2686 static void (*ofunc
) (int);
2688 /* The command line interface's stop routine. This function is installed
2689 as a signal handler for SIGINT. The first time a user requests a
2690 stop, we call remote_stop to send a break or ^C. If there is no
2691 response from the target (it didn't stop when the user requested it),
2692 we ask the user if he'd like to detach from the target. */
2694 remote_interrupt (int signo
)
2696 /* If this doesn't work, try more severe steps. */
2697 signal (signo
, remote_interrupt_twice
);
2700 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2705 /* The user typed ^C twice. */
2708 remote_interrupt_twice (int signo
)
2710 signal (signo
, ofunc
);
2712 signal (signo
, remote_interrupt
);
2715 /* This is the generic stop called via the target vector. When a target
2716 interrupt is requested, either by the command line or the GUI, we
2717 will eventually end up here. */
2721 /* Send a break or a ^C, depending on user preference. */
2723 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
2726 serial_send_break (remote_desc
);
2728 serial_write (remote_desc
, "\003", 1);
2731 /* Ask the user what to do when an interrupt is received. */
2734 interrupt_query (void)
2736 target_terminal_ours ();
2738 if (query ("Interrupted while waiting for the program.\n\
2739 Give up (and stop debugging it)? "))
2741 target_mourn_inferior ();
2742 deprecated_throw_reason (RETURN_QUIT
);
2745 target_terminal_inferior ();
2748 /* Enable/disable target terminal ownership. Most targets can use
2749 terminal groups to control terminal ownership. Remote targets are
2750 different in that explicit transfer of ownership to/from GDB/target
2754 remote_async_terminal_inferior (void)
2756 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2757 sync_execution here. This function should only be called when
2758 GDB is resuming the inferior in the forground. A background
2759 resume (``run&'') should leave GDB in control of the terminal and
2760 consequently should not call this code. */
2761 if (!sync_execution
)
2763 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2764 calls target_terminal_*() idenpotent. The event-loop GDB talking
2765 to an asynchronous target with a synchronous command calls this
2766 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2767 stops trying to transfer the terminal to the target when it
2768 shouldn't this guard can go away. */
2769 if (!remote_async_terminal_ours_p
)
2771 delete_file_handler (input_fd
);
2772 remote_async_terminal_ours_p
= 0;
2773 initialize_sigint_signal_handler ();
2774 /* NOTE: At this point we could also register our selves as the
2775 recipient of all input. Any characters typed could then be
2776 passed on down to the target. */
2780 remote_async_terminal_ours (void)
2782 /* See FIXME in remote_async_terminal_inferior. */
2783 if (!sync_execution
)
2785 /* See FIXME in remote_async_terminal_inferior. */
2786 if (remote_async_terminal_ours_p
)
2788 cleanup_sigint_signal_handler (NULL
);
2789 add_file_handler (input_fd
, stdin_event_handler
, 0);
2790 remote_async_terminal_ours_p
= 1;
2793 /* If nonzero, ignore the next kill. */
2798 remote_console_output (char *msg
)
2802 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
2805 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
2808 fputs_unfiltered (tb
, gdb_stdtarg
);
2810 gdb_flush (gdb_stdtarg
);
2813 /* Wait until the remote machine stops, then return,
2814 storing status in STATUS just as `wait' would.
2815 Returns "pid", which in the case of a multi-threaded
2816 remote OS, is the thread-id. */
2819 remote_wait (ptid_t ptid
, struct target_waitstatus
*status
)
2821 struct remote_state
*rs
= get_remote_state ();
2822 unsigned char *buf
= alloca (rs
->remote_packet_size
);
2823 ULONGEST thread_num
= -1;
2826 status
->kind
= TARGET_WAITKIND_EXITED
;
2827 status
->value
.integer
= 0;
2833 ofunc
= signal (SIGINT
, remote_interrupt
);
2834 getpkt (buf
, (rs
->remote_packet_size
), 1);
2835 signal (SIGINT
, ofunc
);
2837 /* This is a hook for when we need to do something (perhaps the
2838 collection of trace data) every time the target stops. */
2839 if (deprecated_target_wait_loop_hook
)
2840 (*deprecated_target_wait_loop_hook
) ();
2842 remote_stopped_by_watchpoint_p
= 0;
2846 case 'E': /* Error of some sort. */
2847 warning (_("Remote failure reply: %s"), buf
);
2849 case 'F': /* File-I/O request. */
2850 remote_fileio_request (buf
);
2852 case 'T': /* Status with PC, SP, FP, ... */
2855 char regs
[MAX_REGISTER_SIZE
];
2857 /* Expedited reply, containing Signal, {regno, reg} repeat. */
2858 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2860 n... = register number
2861 r... = register contents
2863 p
= &buf
[3]; /* after Txx */
2872 /* If the packet contains a register number save it in
2873 pnum and set p1 to point to the character following
2874 it. Otherwise p1 points to p. */
2876 /* If this packet is an awatch packet, don't parse the
2877 'a' as a register number. */
2879 if (strncmp (p
, "awatch", strlen("awatch")) != 0)
2881 /* Read the ``P'' register number. */
2882 pnum
= strtol (p
, &p_temp
, 16);
2883 p1
= (unsigned char *) p_temp
;
2888 if (p1
== p
) /* No register number present here. */
2890 p1
= (unsigned char *) strchr (p
, ':');
2892 warning (_("Malformed packet(a) (missing colon): %s\n\
2895 if (strncmp (p
, "thread", p1
- p
) == 0)
2897 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2898 record_currthread (thread_num
);
2899 p
= (unsigned char *) p_temp
;
2901 else if ((strncmp (p
, "watch", p1
- p
) == 0)
2902 || (strncmp (p
, "rwatch", p1
- p
) == 0)
2903 || (strncmp (p
, "awatch", p1
- p
) == 0))
2905 remote_stopped_by_watchpoint_p
= 1;
2906 p
= unpack_varlen_hex (++p1
, &addr
);
2907 remote_watch_data_address
= (CORE_ADDR
)addr
;
2911 /* Silently skip unknown optional info. */
2912 p_temp
= strchr (p1
+ 1, ';');
2914 p
= (unsigned char *) p_temp
;
2919 struct packet_reg
*reg
= packet_reg_from_pnum (rs
, pnum
);
2923 error (_("Malformed packet(b) (missing colon): %s\n\
2928 error (_("Remote sent bad register number %s: %s\n\
2930 phex_nz (pnum
, 0), p
, buf
);
2932 fieldsize
= hex2bin (p
, regs
,
2933 register_size (current_gdbarch
,
2936 if (fieldsize
< register_size (current_gdbarch
,
2938 warning (_("Remote reply is too short: %s"), buf
);
2939 regcache_raw_supply (current_regcache
,
2944 error (_("Remote register badly formatted: %s\nhere: %s"),
2949 case 'S': /* Old style status, just signal only. */
2950 status
->kind
= TARGET_WAITKIND_STOPPED
;
2951 status
->value
.sig
= (enum target_signal
)
2952 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2956 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2957 record_currthread (thread_num
);
2960 case 'W': /* Target exited. */
2962 /* The remote process exited. */
2963 status
->kind
= TARGET_WAITKIND_EXITED
;
2964 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2968 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2969 status
->value
.sig
= (enum target_signal
)
2970 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2974 case 'O': /* Console output. */
2975 remote_console_output (buf
+ 1);
2978 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2980 /* Zero length reply means that we tried 'S' or 'C' and
2981 the remote system doesn't support it. */
2982 target_terminal_ours_for_output ();
2984 ("Can't send signals to this remote system. %s not sent.\n",
2985 target_signal_to_name (last_sent_signal
));
2986 last_sent_signal
= TARGET_SIGNAL_0
;
2987 target_terminal_inferior ();
2989 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2990 putpkt ((char *) buf
);
2993 /* else fallthrough */
2995 warning (_("Invalid remote reply: %s"), buf
);
3000 if (thread_num
!= -1)
3002 return pid_to_ptid (thread_num
);
3004 return inferior_ptid
;
3007 /* Async version of remote_wait. */
3009 remote_async_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3011 struct remote_state
*rs
= get_remote_state ();
3012 unsigned char *buf
= alloca (rs
->remote_packet_size
);
3013 ULONGEST thread_num
= -1;
3016 status
->kind
= TARGET_WAITKIND_EXITED
;
3017 status
->value
.integer
= 0;
3019 remote_stopped_by_watchpoint_p
= 0;
3025 if (!target_is_async_p ())
3026 ofunc
= signal (SIGINT
, remote_interrupt
);
3027 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3028 _never_ wait for ever -> test on target_is_async_p().
3029 However, before we do that we need to ensure that the caller
3030 knows how to take the target into/out of async mode. */
3031 getpkt (buf
, (rs
->remote_packet_size
), wait_forever_enabled_p
);
3032 if (!target_is_async_p ())
3033 signal (SIGINT
, ofunc
);
3035 /* This is a hook for when we need to do something (perhaps the
3036 collection of trace data) every time the target stops. */
3037 if (deprecated_target_wait_loop_hook
)
3038 (*deprecated_target_wait_loop_hook
) ();
3042 case 'E': /* Error of some sort. */
3043 warning (_("Remote failure reply: %s"), buf
);
3045 case 'F': /* File-I/O request. */
3046 remote_fileio_request (buf
);
3048 case 'T': /* Status with PC, SP, FP, ... */
3051 char regs
[MAX_REGISTER_SIZE
];
3053 /* Expedited reply, containing Signal, {regno, reg} repeat. */
3054 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
3056 n... = register number
3057 r... = register contents
3059 p
= &buf
[3]; /* after Txx */
3068 /* If the packet contains a register number, save it
3069 in pnum and set p1 to point to the character
3070 following it. Otherwise p1 points to p. */
3072 /* If this packet is an awatch packet, don't parse the 'a'
3073 as a register number. */
3075 if (!strncmp (p
, "awatch", strlen ("awatch")) != 0)
3077 /* Read the register number. */
3078 pnum
= strtol (p
, &p_temp
, 16);
3079 p1
= (unsigned char *) p_temp
;
3084 if (p1
== p
) /* No register number present here. */
3086 p1
= (unsigned char *) strchr (p
, ':');
3088 error (_("Malformed packet(a) (missing colon): %s\n\
3091 if (strncmp (p
, "thread", p1
- p
) == 0)
3093 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
3094 record_currthread (thread_num
);
3095 p
= (unsigned char *) p_temp
;
3097 else if ((strncmp (p
, "watch", p1
- p
) == 0)
3098 || (strncmp (p
, "rwatch", p1
- p
) == 0)
3099 || (strncmp (p
, "awatch", p1
- p
) == 0))
3101 remote_stopped_by_watchpoint_p
= 1;
3102 p
= unpack_varlen_hex (++p1
, &addr
);
3103 remote_watch_data_address
= (CORE_ADDR
)addr
;
3107 /* Silently skip unknown optional info. */
3108 p_temp
= (unsigned char *) strchr (p1
+ 1, ';');
3116 struct packet_reg
*reg
= packet_reg_from_pnum (rs
, pnum
);
3119 error (_("Malformed packet(b) (missing colon): %s\n\
3124 error (_("Remote sent bad register number %ld: %s\n\
3128 fieldsize
= hex2bin (p
, regs
,
3129 register_size (current_gdbarch
,
3132 if (fieldsize
< register_size (current_gdbarch
,
3134 warning (_("Remote reply is too short: %s"), buf
);
3135 regcache_raw_supply (current_regcache
, reg
->regnum
, regs
);
3139 error (_("Remote register badly formatted: %s\nhere: %s"),
3144 case 'S': /* Old style status, just signal only. */
3145 status
->kind
= TARGET_WAITKIND_STOPPED
;
3146 status
->value
.sig
= (enum target_signal
)
3147 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3151 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
3152 record_currthread (thread_num
);
3155 case 'W': /* Target exited. */
3157 /* The remote process exited. */
3158 status
->kind
= TARGET_WAITKIND_EXITED
;
3159 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
3163 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
3164 status
->value
.sig
= (enum target_signal
)
3165 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3169 case 'O': /* Console output. */
3170 remote_console_output (buf
+ 1);
3171 /* Return immediately to the event loop. The event loop will
3172 still be waiting on the inferior afterwards. */
3173 status
->kind
= TARGET_WAITKIND_IGNORE
;
3176 if (last_sent_signal
!= TARGET_SIGNAL_0
)
3178 /* Zero length reply means that we tried 'S' or 'C' and
3179 the remote system doesn't support it. */
3180 target_terminal_ours_for_output ();
3182 ("Can't send signals to this remote system. %s not sent.\n",
3183 target_signal_to_name (last_sent_signal
));
3184 last_sent_signal
= TARGET_SIGNAL_0
;
3185 target_terminal_inferior ();
3187 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
3188 putpkt ((char *) buf
);
3191 /* else fallthrough */
3193 warning (_("Invalid remote reply: %s"), buf
);
3198 if (thread_num
!= -1)
3200 return pid_to_ptid (thread_num
);
3202 return inferior_ptid
;
3205 /* Number of bytes of registers this stub implements. */
3207 static int register_bytes_found
;
3209 /* Read the remote registers into the block REGS. */
3210 /* Currently we just read all the registers, so we don't use regnum. */
3213 fetch_register_using_p (int regnum
)
3215 struct remote_state
*rs
= get_remote_state ();
3216 char *buf
= alloca (rs
->remote_packet_size
), *p
;
3217 char regp
[MAX_REGISTER_SIZE
];
3222 p
+= hexnumstr (p
, regnum
);
3224 remote_send (buf
, rs
->remote_packet_size
);
3226 /* If the stub didn't recognize the packet, or if we got an error,
3228 if (buf
[0] == '\0' || buf
[0] == 'E')
3231 /* If this register is unfetchable, tell the regcache. */
3234 regcache_raw_supply (current_regcache
, regnum
, NULL
);
3235 set_register_cached (regnum
, -1);
3239 /* Otherwise, parse and supply the value. */
3246 error (_("fetch_register_using_p: early buf termination"));
3250 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3253 regcache_raw_supply (current_regcache
, regnum
, regp
);
3258 remote_fetch_registers (int regnum
)
3260 struct remote_state
*rs
= get_remote_state ();
3261 char *buf
= alloca (rs
->remote_packet_size
);
3264 char *regs
= alloca (rs
->sizeof_g_packet
);
3266 set_thread (PIDGET (inferior_ptid
), 1);
3270 struct packet_reg
*reg
= packet_reg_from_regnum (rs
, regnum
);
3271 gdb_assert (reg
!= NULL
);
3272 if (!reg
->in_g_packet
)
3273 internal_error (__FILE__
, __LINE__
,
3274 _("Attempt to fetch a non G-packet register when this "
3275 "remote.c does not support the p-packet."));
3277 switch (remote_protocol_p
.support
)
3279 case PACKET_DISABLE
:
3282 if (fetch_register_using_p (regnum
))
3285 error (_("Protocol error: p packet not recognized by stub"));
3286 case PACKET_SUPPORT_UNKNOWN
:
3287 if (fetch_register_using_p (regnum
))
3289 /* The stub recognized the 'p' packet. Remember this. */
3290 remote_protocol_p
.support
= PACKET_ENABLE
;
3295 /* The stub does not support the 'P' packet. Use 'G'
3296 instead, and don't try using 'P' in the future (it
3297 will just waste our time). */
3298 remote_protocol_p
.support
= PACKET_DISABLE
;
3304 remote_send (buf
, (rs
->remote_packet_size
));
3306 /* Save the size of the packet sent to us by the target. Its used
3307 as a heuristic when determining the max size of packets that the
3308 target can safely receive. */
3309 if ((rs
->actual_register_packet_size
) == 0)
3310 (rs
->actual_register_packet_size
) = strlen (buf
);
3312 /* Unimplemented registers read as all bits zero. */
3313 memset (regs
, 0, rs
->sizeof_g_packet
);
3315 /* We can get out of synch in various cases. If the first character
3316 in the buffer is not a hex character, assume that has happened
3317 and try to fetch another packet to read. */
3318 while ((buf
[0] < '0' || buf
[0] > '9')
3319 && (buf
[0] < 'A' || buf
[0] > 'F')
3320 && (buf
[0] < 'a' || buf
[0] > 'f')
3321 && buf
[0] != 'x') /* New: unavailable register value. */
3324 fprintf_unfiltered (gdb_stdlog
,
3325 "Bad register packet; fetching a new packet\n");
3326 getpkt (buf
, (rs
->remote_packet_size
), 0);
3329 /* Reply describes registers byte by byte, each byte encoded as two
3330 hex characters. Suck them all up, then supply them to the
3331 register cacheing/storage mechanism. */
3334 for (i
= 0; i
< rs
->sizeof_g_packet
; i
++)
3340 warning (_("Remote reply is of odd length: %s"), buf
);
3341 /* Don't change register_bytes_found in this case, and don't
3342 print a second warning. */
3345 if (p
[0] == 'x' && p
[1] == 'x')
3346 regs
[i
] = 0; /* 'x' */
3348 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3352 if (i
!= register_bytes_found
)
3354 register_bytes_found
= i
;
3355 if (REGISTER_BYTES_OK_P ()
3356 && !REGISTER_BYTES_OK (i
))
3357 warning (_("Remote reply is too short: %s"), buf
);
3363 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
3365 struct packet_reg
*r
= &rs
->regs
[i
];
3368 if (r
->offset
* 2 >= strlen (buf
))
3369 /* A short packet that didn't include the register's
3370 value, this implies that the register is zero (and
3371 not that the register is unavailable). Supply that
3373 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3374 else if (buf
[r
->offset
* 2] == 'x')
3376 gdb_assert (r
->offset
* 2 < strlen (buf
));
3377 /* The register isn't available, mark it as such (at
3378 the same time setting the value to zero). */
3379 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3380 set_register_cached (i
, -1);
3383 regcache_raw_supply (current_regcache
, r
->regnum
,
3390 /* Prepare to store registers. Since we may send them all (using a
3391 'G' request), we have to read out the ones we don't want to change
3395 remote_prepare_to_store (void)
3397 struct remote_state
*rs
= get_remote_state ();
3399 char buf
[MAX_REGISTER_SIZE
];
3401 /* Make sure the entire registers array is valid. */
3402 switch (remote_protocol_P
.support
)
3404 case PACKET_DISABLE
:
3405 case PACKET_SUPPORT_UNKNOWN
:
3406 /* Make sure all the necessary registers are cached. */
3407 for (i
= 0; i
< NUM_REGS
; i
++)
3408 if (rs
->regs
[i
].in_g_packet
)
3409 regcache_raw_read (current_regcache
, rs
->regs
[i
].regnum
, buf
);
3416 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
3417 packet was not recognized. */
3420 store_register_using_P (int regnum
)
3422 struct remote_state
*rs
= get_remote_state ();
3423 struct packet_reg
*reg
= packet_reg_from_regnum (rs
, regnum
);
3424 /* Try storing a single register. */
3425 char *buf
= alloca (rs
->remote_packet_size
);
3426 char regp
[MAX_REGISTER_SIZE
];
3430 sprintf (buf
, "P%s=", phex_nz (reg
->pnum
, 0));
3431 p
= buf
+ strlen (buf
);
3432 regcache_raw_collect (current_regcache
, reg
->regnum
, regp
);
3433 bin2hex (regp
, p
, register_size (current_gdbarch
, reg
->regnum
));
3434 remote_send (buf
, rs
->remote_packet_size
);
3436 return buf
[0] != '\0';
3440 /* Store register REGNUM, or all registers if REGNUM == -1, from the
3441 contents of the register cache buffer. FIXME: ignores errors. */
3444 remote_store_registers (int regnum
)
3446 struct remote_state
*rs
= get_remote_state ();
3452 set_thread (PIDGET (inferior_ptid
), 1);
3456 switch (remote_protocol_P
.support
)
3458 case PACKET_DISABLE
:
3461 if (store_register_using_P (regnum
))
3464 error (_("Protocol error: P packet not recognized by stub"));
3465 case PACKET_SUPPORT_UNKNOWN
:
3466 if (store_register_using_P (regnum
))
3468 /* The stub recognized the 'P' packet. Remember this. */
3469 remote_protocol_P
.support
= PACKET_ENABLE
;
3474 /* The stub does not support the 'P' packet. Use 'G'
3475 instead, and don't try using 'P' in the future (it
3476 will just waste our time). */
3477 remote_protocol_P
.support
= PACKET_DISABLE
;
3483 /* Extract all the registers in the regcache copying them into a
3487 regs
= alloca (rs
->sizeof_g_packet
);
3488 memset (regs
, 0, rs
->sizeof_g_packet
);
3489 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
3491 struct packet_reg
*r
= &rs
->regs
[i
];
3493 regcache_raw_collect (current_regcache
, r
->regnum
, regs
+ r
->offset
);
3497 /* Command describes registers byte by byte,
3498 each byte encoded as two hex characters. */
3499 buf
= alloca (rs
->remote_packet_size
);
3502 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3503 bin2hex (regs
, p
, register_bytes_found
);
3504 remote_send (buf
, (rs
->remote_packet_size
));
3508 /* Return the number of hex digits in num. */
3511 hexnumlen (ULONGEST num
)
3515 for (i
= 0; num
!= 0; i
++)
3521 /* Set BUF to the minimum number of hex digits representing NUM. */
3524 hexnumstr (char *buf
, ULONGEST num
)
3526 int len
= hexnumlen (num
);
3527 return hexnumnstr (buf
, num
, len
);
3531 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3534 hexnumnstr (char *buf
, ULONGEST num
, int width
)
3540 for (i
= width
- 1; i
>= 0; i
--)
3542 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
3549 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3552 remote_address_masked (CORE_ADDR addr
)
3554 if (remote_address_size
> 0
3555 && remote_address_size
< (sizeof (ULONGEST
) * 8))
3557 /* Only create a mask when that mask can safely be constructed
3558 in a ULONGEST variable. */
3560 mask
= (mask
<< remote_address_size
) - 1;
3566 /* Determine whether the remote target supports binary downloading.
3567 This is accomplished by sending a no-op memory write of zero length
3568 to the target at the specified address. It does not suffice to send
3569 the whole packet, since many stubs strip the eighth bit and
3570 subsequently compute a wrong checksum, which causes real havoc with
3573 NOTE: This can still lose if the serial line is not eight-bit
3574 clean. In cases like this, the user should clear "remote
3578 check_binary_download (CORE_ADDR addr
)
3580 struct remote_state
*rs
= get_remote_state ();
3581 switch (remote_protocol_binary_download
.support
)
3583 case PACKET_DISABLE
:
3587 case PACKET_SUPPORT_UNKNOWN
:
3589 char *buf
= alloca (rs
->remote_packet_size
);
3594 p
+= hexnumstr (p
, (ULONGEST
) addr
);
3596 p
+= hexnumstr (p
, (ULONGEST
) 0);
3600 putpkt_binary (buf
, (int) (p
- buf
));
3601 getpkt (buf
, (rs
->remote_packet_size
), 0);
3606 fprintf_unfiltered (gdb_stdlog
,
3607 "binary downloading NOT suppported by target\n");
3608 remote_protocol_binary_download
.support
= PACKET_DISABLE
;
3613 fprintf_unfiltered (gdb_stdlog
,
3614 "binary downloading suppported by target\n");
3615 remote_protocol_binary_download
.support
= PACKET_ENABLE
;
3622 /* Write memory data directly to the remote machine.
3623 This does not inform the data cache; the data cache uses this.
3624 MEMADDR is the address in the remote memory space.
3625 MYADDR is the address of the buffer in our space.
3626 LEN is the number of bytes.
3628 Returns number of bytes transferred, or 0 (setting errno) for
3629 error. Only transfer a single packet. */
3632 remote_write_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
3636 unsigned char *plen
;
3642 unsigned char *payload_start
;
3644 /* Verify that the target can support a binary download. */
3645 check_binary_download (memaddr
);
3647 payload_size
= get_memory_write_packet_size ();
3649 /* Compute the size, and then allocate space for the largest
3650 possible packet. Include space for an extra trailing NUL. */
3651 sizeof_buf
= payload_size
+ 1;
3652 buf
= alloca (sizeof_buf
);
3654 /* Compute the size of the actual payload by subtracting out the
3655 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
3657 payload_size
-= strlen ("$M,:#NN");
3658 payload_size
-= hexnumlen (memaddr
);
3660 /* Construct the packet header: "[MX]<memaddr>,<len>:". */
3662 /* Append "[XM]". Compute a best guess of the number of bytes
3663 actually transfered. */
3665 switch (remote_protocol_binary_download
.support
)
3669 /* Best guess at number of bytes that will fit. */
3670 todo
= min (len
, payload_size
);
3671 payload_size
-= hexnumlen (todo
);
3672 todo
= min (todo
, payload_size
);
3674 case PACKET_DISABLE
:
3676 /* Num bytes that will fit. */
3677 todo
= min (len
, payload_size
/ 2);
3678 payload_size
-= hexnumlen (todo
);
3679 todo
= min (todo
, payload_size
/ 2);
3681 case PACKET_SUPPORT_UNKNOWN
:
3682 internal_error (__FILE__
, __LINE__
,
3683 _("remote_write_bytes: bad internal state"));
3685 internal_error (__FILE__
, __LINE__
, _("bad switch"));
3688 internal_error (__FILE__
, __LINE__
,
3689 _("minumum packet size too small to write data"));
3691 /* Append "<memaddr>". */
3692 memaddr
= remote_address_masked (memaddr
);
3693 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3698 /* Append <len>. Retain the location/size of <len>. It may need to
3699 be adjusted once the packet body has been created. */
3701 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
3708 /* Append the packet body. */
3710 switch (remote_protocol_binary_download
.support
)
3713 /* Binary mode. Send target system values byte by byte, in
3714 increasing byte addresses. Only escape certain critical
3717 (nr_bytes
< todo
) && (p
- payload_start
) < payload_size
;
3720 switch (myaddr
[nr_bytes
] & 0xff)
3725 /* These must be escaped. */
3727 *p
++ = (myaddr
[nr_bytes
] & 0xff) ^ 0x20;
3730 *p
++ = myaddr
[nr_bytes
] & 0xff;
3734 if (nr_bytes
< todo
)
3736 /* Escape chars have filled up the buffer prematurely,
3737 and we have actually sent fewer bytes than planned.
3738 Fix-up the length field of the packet. Use the same
3739 number of characters as before. */
3740 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
3741 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
3744 case PACKET_DISABLE
:
3745 /* Normal mode: Send target system values byte by byte, in
3746 increasing byte addresses. Each byte is encoded as a two hex
3748 nr_bytes
= bin2hex (myaddr
, p
, todo
);
3751 case PACKET_SUPPORT_UNKNOWN
:
3752 internal_error (__FILE__
, __LINE__
,
3753 _("remote_write_bytes: bad internal state"));
3755 internal_error (__FILE__
, __LINE__
, _("bad switch"));
3758 putpkt_binary (buf
, (int) (p
- buf
));
3759 getpkt (buf
, sizeof_buf
, 0);
3763 /* There is no correspondance between what the remote protocol
3764 uses for errors and errno codes. We would like a cleaner way
3765 of representing errors (big enough to include errno codes,
3766 bfd_error codes, and others). But for now just return EIO. */
3771 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
3772 fewer bytes than we'd planned. */
3776 /* Read memory data directly from the remote machine.
3777 This does not use the data cache; the data cache uses this.
3778 MEMADDR is the address in the remote memory space.
3779 MYADDR is the address of the buffer in our space.
3780 LEN is the number of bytes.
3782 Returns number of bytes transferred, or 0 for error. */
3784 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3785 remote targets) shouldn't attempt to read the entire buffer.
3786 Instead it should read a single packet worth of data and then
3787 return the byte size of that packet to the caller. The caller (its
3788 caller and its callers caller ;-) already contains code for
3789 handling partial reads. */
3792 remote_read_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
3795 int max_buf_size
; /* Max size of packet output buffer. */
3799 /* Create a buffer big enough for this packet. */
3800 max_buf_size
= get_memory_read_packet_size ();
3801 sizeof_buf
= max_buf_size
+ 1; /* Space for trailing NULL. */
3802 buf
= alloca (sizeof_buf
);
3811 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
3813 /* construct "m"<memaddr>","<len>" */
3814 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3815 memaddr
= remote_address_masked (memaddr
);
3818 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3820 p
+= hexnumstr (p
, (ULONGEST
) todo
);
3824 getpkt (buf
, sizeof_buf
, 0);
3827 && isxdigit (buf
[1]) && isxdigit (buf
[2])
3830 /* There is no correspondance between what the remote
3831 protocol uses for errors and errno codes. We would like
3832 a cleaner way of representing errors (big enough to
3833 include errno codes, bfd_error codes, and others). But
3834 for now just return EIO. */
3839 /* Reply describes memory byte by byte,
3840 each byte encoded as two hex characters. */
3843 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
3845 /* Reply is short. This means that we were able to read
3846 only part of what we wanted to. */
3847 return i
+ (origlen
- len
);
3856 /* Read or write LEN bytes from inferior memory at MEMADDR,
3857 transferring to or from debugger address BUFFER. Write to inferior
3858 if SHOULD_WRITE is nonzero. Returns length of data written or
3859 read; 0 for error. TARGET is unused. */
3862 remote_xfer_memory (CORE_ADDR mem_addr
, char *buffer
, int mem_len
,
3863 int should_write
, struct mem_attrib
*attrib
,
3864 struct target_ops
*target
)
3866 CORE_ADDR targ_addr
;
3870 /* Should this be the selected frame? */
3871 gdbarch_remote_translate_xfer_address (current_gdbarch
,
3874 &targ_addr
, &targ_len
);
3879 res
= remote_write_bytes (targ_addr
, buffer
, targ_len
);
3881 res
= remote_read_bytes (targ_addr
, buffer
, targ_len
);
3887 remote_files_info (struct target_ops
*ignore
)
3889 puts_filtered ("Debugging a target over a serial line.\n");
3892 /* Stuff for dealing with the packets which are part of this protocol.
3893 See comment at top of file for details. */
3895 /* Read a single character from the remote end, masking it down to 7
3899 readchar (int timeout
)
3903 ch
= serial_readchar (remote_desc
, timeout
);
3908 switch ((enum serial_rc
) ch
)
3911 target_mourn_inferior ();
3912 error (_("Remote connection closed"));
3915 perror_with_name (_("Remote communication error"));
3917 case SERIAL_TIMEOUT
:
3923 /* Send the command in BUF to the remote machine, and read the reply
3924 into BUF. Report an error if we get an error reply. */
3927 remote_send (char *buf
,
3931 getpkt (buf
, sizeof_buf
, 0);
3934 error (_("Remote failure reply: %s"), buf
);
3937 /* Display a null-terminated packet on stdout, for debugging, using C
3941 print_packet (char *buf
)
3943 puts_filtered ("\"");
3944 fputstr_filtered (buf
, '"', gdb_stdout
);
3945 puts_filtered ("\"");
3951 return putpkt_binary (buf
, strlen (buf
));
3954 /* Send a packet to the remote machine, with error checking. The data
3955 of the packet is in BUF. The string in BUF can be at most
3956 (rs->remote_packet_size) - 5 to account for the $, # and checksum,
3957 and for a possible /0 if we are debugging (remote_debug) and want
3958 to print the sent packet as a string. */
3961 putpkt_binary (char *buf
, int cnt
)
3963 struct remote_state
*rs
= get_remote_state ();
3965 unsigned char csum
= 0;
3966 char *buf2
= alloca (cnt
+ 6);
3967 long sizeof_junkbuf
= (rs
->remote_packet_size
);
3968 char *junkbuf
= alloca (sizeof_junkbuf
);
3974 /* Copy the packet into buffer BUF2, encapsulating it
3975 and giving it a checksum. */
3980 for (i
= 0; i
< cnt
; i
++)
3986 *p
++ = tohex ((csum
>> 4) & 0xf);
3987 *p
++ = tohex (csum
& 0xf);
3989 /* Send it over and over until we get a positive ack. */
3993 int started_error_output
= 0;
3998 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
3999 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
4000 fprintf_unfiltered (gdb_stdlog
, "...");
4001 gdb_flush (gdb_stdlog
);
4003 if (serial_write (remote_desc
, buf2
, p
- buf2
))
4004 perror_with_name (_("putpkt: write failed"));
4006 /* Read until either a timeout occurs (-2) or '+' is read. */
4009 ch
= readchar (remote_timeout
);
4017 case SERIAL_TIMEOUT
:
4019 if (started_error_output
)
4021 putchar_unfiltered ('\n');
4022 started_error_output
= 0;
4031 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
4035 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
4036 case SERIAL_TIMEOUT
:
4040 break; /* Retransmit buffer. */
4044 fprintf_unfiltered (gdb_stdlog
,
4045 "Packet instead of Ack, ignoring it\n");
4046 /* It's probably an old response sent because an ACK
4047 was lost. Gobble up the packet and ack it so it
4048 doesn't get retransmitted when we resend this
4050 read_frame (junkbuf
, sizeof_junkbuf
);
4051 serial_write (remote_desc
, "+", 1);
4052 continue; /* Now, go look for +. */
4057 if (!started_error_output
)
4059 started_error_output
= 1;
4060 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
4062 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
4066 break; /* Here to retransmit. */
4070 /* This is wrong. If doing a long backtrace, the user should be
4071 able to get out next time we call QUIT, without anything as
4072 violent as interrupt_query. If we want to provide a way out of
4073 here without getting to the next QUIT, it should be based on
4074 hitting ^C twice as in remote_wait. */
4084 /* Come here after finding the start of the frame. Collect the rest
4085 into BUF, verifying the checksum, length, and handling run-length
4086 compression. No more than sizeof_buf-1 characters are read so that
4087 the buffer can be NUL terminated.
4089 Returns -1 on error, number of characters in buffer (ignoring the
4090 trailing NULL) on success. (could be extended to return one of the
4091 SERIAL status indications). */
4094 read_frame (char *buf
,
4106 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NULL. */
4107 c
= readchar (remote_timeout
);
4110 case SERIAL_TIMEOUT
:
4112 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
4116 fputs_filtered ("Saw new packet start in middle of old one\n",
4118 return -1; /* Start a new packet, count retries. */
4121 unsigned char pktcsum
;
4127 check_0
= readchar (remote_timeout
);
4129 check_1
= readchar (remote_timeout
);
4131 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
4134 fputs_filtered ("Timeout in checksum, retrying\n",
4138 else if (check_0
< 0 || check_1
< 0)
4141 fputs_filtered ("Communication error in checksum\n",
4146 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
4147 if (csum
== pktcsum
)
4152 fprintf_filtered (gdb_stdlog
,
4153 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4155 fputs_filtered (buf
, gdb_stdlog
);
4156 fputs_filtered ("\n", gdb_stdlog
);
4158 /* Number of characters in buffer ignoring trailing
4162 case '*': /* Run length encoding. */
4167 c
= readchar (remote_timeout
);
4169 repeat
= c
- ' ' + 3; /* Compute repeat count. */
4171 /* The character before ``*'' is repeated. */
4173 if (repeat
> 0 && repeat
<= 255
4175 && bc
+ repeat
- 1 < sizeof_buf
- 1)
4177 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
4183 printf_filtered (_("Repeat count %d too large for buffer: "),
4185 puts_filtered (buf
);
4186 puts_filtered ("\n");
4190 if (bc
< sizeof_buf
- 1)
4198 puts_filtered ("Remote packet too long: ");
4199 puts_filtered (buf
);
4200 puts_filtered ("\n");
4207 /* Read a packet from the remote machine, with error checking, and
4208 store it in BUF. If FOREVER, wait forever rather than timing out;
4209 this is used (in synchronous mode) to wait for a target that is is
4210 executing user code to stop. */
4211 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4212 don't have to change all the calls to getpkt to deal with the
4213 return value, because at the moment I don't know what the right
4214 thing to do it for those. */
4222 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
4226 /* Read a packet from the remote machine, with error checking, and
4227 store it in BUF. If FOREVER, wait forever rather than timing out;
4228 this is used (in synchronous mode) to wait for a target that is is
4229 executing user code to stop. If FOREVER == 0, this function is
4230 allowed to time out gracefully and return an indication of this to
4233 getpkt_sane (char *buf
,
4242 strcpy (buf
, "timeout");
4246 timeout
= watchdog
> 0 ? watchdog
: -1;
4250 timeout
= remote_timeout
;
4254 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
4256 /* This can loop forever if the remote side sends us characters
4257 continuously, but if it pauses, we'll get a zero from
4258 readchar because of timeout. Then we'll count that as a
4261 /* Note that we will only wait forever prior to the start of a
4262 packet. After that, we expect characters to arrive at a
4263 brisk pace. They should show up within remote_timeout
4268 c
= readchar (timeout
);
4270 if (c
== SERIAL_TIMEOUT
)
4272 if (forever
) /* Watchdog went off? Kill the target. */
4275 target_mourn_inferior ();
4276 error (_("Watchdog has expired. Target detached."));
4279 fputs_filtered ("Timed out.\n", gdb_stdlog
);
4285 /* We've found the start of a packet, now collect the data. */
4287 val
= read_frame (buf
, sizeof_buf
);
4293 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
4294 fputstr_unfiltered (buf
, 0, gdb_stdlog
);
4295 fprintf_unfiltered (gdb_stdlog
, "\n");
4297 serial_write (remote_desc
, "+", 1);
4301 /* Try the whole thing again. */
4303 serial_write (remote_desc
, "-", 1);
4306 /* We have tried hard enough, and just can't receive the packet.
4309 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
4310 serial_write (remote_desc
, "+", 1);
4317 /* For some mysterious reason, wait_for_inferior calls kill instead of
4318 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4322 target_mourn_inferior ();
4326 /* Use catch_errors so the user can quit from gdb even when we aren't on
4327 speaking terms with the remote system. */
4328 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4330 /* Don't wait for it to die. I'm not really sure it matters whether
4331 we do or not. For the existing stubs, kill is a noop. */
4332 target_mourn_inferior ();
4335 /* Async version of remote_kill. */
4337 remote_async_kill (void)
4339 /* Unregister the file descriptor from the event loop. */
4340 if (target_is_async_p ())
4341 serial_async (remote_desc
, NULL
, 0);
4343 /* For some mysterious reason, wait_for_inferior calls kill instead of
4344 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4348 target_mourn_inferior ();
4352 /* Use catch_errors so the user can quit from gdb even when we
4353 aren't on speaking terms with the remote system. */
4354 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4356 /* Don't wait for it to die. I'm not really sure it matters whether
4357 we do or not. For the existing stubs, kill is a noop. */
4358 target_mourn_inferior ();
4364 remote_mourn_1 (&remote_ops
);
4368 remote_async_mourn (void)
4370 remote_mourn_1 (&remote_async_ops
);
4374 extended_remote_mourn (void)
4376 /* We do _not_ want to mourn the target like this; this will
4377 remove the extended remote target from the target stack,
4378 and the next time the user says "run" it'll fail.
4380 FIXME: What is the right thing to do here? */
4382 remote_mourn_1 (&extended_remote_ops
);
4386 /* Worker function for remote_mourn. */
4388 remote_mourn_1 (struct target_ops
*target
)
4390 unpush_target (target
);
4391 generic_mourn_inferior ();
4394 /* In the extended protocol we want to be able to do things like
4395 "run" and have them basically work as expected. So we need
4396 a special create_inferior function.
4398 FIXME: One day add support for changing the exec file
4399 we're debugging, arguments and an environment. */
4402 extended_remote_create_inferior (char *exec_file
, char *args
,
4403 char **env
, int from_tty
)
4405 /* Rip out the breakpoints; we'll reinsert them after restarting
4406 the remote server. */
4407 remove_breakpoints ();
4409 /* Now restart the remote server. */
4410 extended_remote_restart ();
4412 /* Now put the breakpoints back in. This way we're safe if the
4413 restart function works via a unix fork on the remote side. */
4414 insert_breakpoints ();
4416 /* Clean up from the last time we were running. */
4417 clear_proceed_status ();
4419 /* Let the remote process run. */
4420 proceed (-1, TARGET_SIGNAL_0
, 0);
4423 /* Async version of extended_remote_create_inferior. */
4425 extended_remote_async_create_inferior (char *exec_file
, char *args
,
4426 char **env
, int from_tty
)
4428 /* Rip out the breakpoints; we'll reinsert them after restarting
4429 the remote server. */
4430 remove_breakpoints ();
4432 /* If running asynchronously, register the target file descriptor
4433 with the event loop. */
4434 if (target_can_async_p ())
4435 target_async (inferior_event_handler
, 0);
4437 /* Now restart the remote server. */
4438 extended_remote_restart ();
4440 /* Now put the breakpoints back in. This way we're safe if the
4441 restart function works via a unix fork on the remote side. */
4442 insert_breakpoints ();
4444 /* Clean up from the last time we were running. */
4445 clear_proceed_status ();
4447 /* Let the remote process run. */
4448 proceed (-1, TARGET_SIGNAL_0
, 0);
4452 /* On some machines, e.g. 68k, we may use a different breakpoint
4453 instruction than other targets; in those use
4454 DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4455 Also, bi-endian targets may define
4456 DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4457 DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we
4458 just call the standard routines that are in mem-break.c. */
4460 /* NOTE: cagney/2003-06-08: This is silly. A remote and simulator
4461 target should use an identical BREAKPOINT_FROM_PC. As for native,
4462 the ARCH-OS-tdep.c code can override the default. */
4464 #if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4465 #define DEPRECATED_REMOTE_BREAKPOINT
4468 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4470 /* If the target isn't bi-endian, just pretend it is. */
4471 #if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4472 #define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4473 #define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4476 static unsigned char big_break_insn
[] = DEPRECATED_BIG_REMOTE_BREAKPOINT
;
4477 static unsigned char little_break_insn
[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT
;
4479 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4481 /* Insert a breakpoint on targets that don't have any better
4482 breakpoint support. We read the contents of the target location
4483 and stash it, then overwrite it with a breakpoint instruction.
4484 ADDR is the target location in the target machine. CONTENTS_CACHE
4485 is a pointer to memory allocated for saving the target contents.
4486 It is guaranteed by the caller to be long enough to save the number
4487 of bytes returned by BREAKPOINT_FROM_PC. */
4490 remote_insert_breakpoint (CORE_ADDR addr
, char *contents_cache
)
4492 struct remote_state
*rs
= get_remote_state ();
4493 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4498 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4499 If it succeeds, then set the support to PACKET_ENABLE. If it
4500 fails, and the user has explicitly requested the Z support then
4501 report an error, otherwise, mark it disabled and go on. */
4503 if (remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
].support
!= PACKET_DISABLE
)
4505 char *buf
= alloca (rs
->remote_packet_size
);
4508 addr
= remote_address_masked (addr
);
4512 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4513 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
4514 sprintf (p
, ",%d", bp_size
);
4517 getpkt (buf
, (rs
->remote_packet_size
), 0);
4519 switch (packet_ok (buf
, &remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
]))
4525 case PACKET_UNKNOWN
:
4530 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4531 val
= target_read_memory (addr
, contents_cache
, sizeof big_break_insn
);
4535 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
4536 val
= target_write_memory (addr
, (char *) big_break_insn
,
4537 sizeof big_break_insn
);
4539 val
= target_write_memory (addr
, (char *) little_break_insn
,
4540 sizeof little_break_insn
);
4545 return memory_insert_breakpoint (addr
, contents_cache
);
4546 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4550 remote_remove_breakpoint (CORE_ADDR addr
, char *contents_cache
)
4552 struct remote_state
*rs
= get_remote_state ();
4555 if (remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
].support
!= PACKET_DISABLE
)
4557 char *buf
= alloca (rs
->remote_packet_size
);
4564 addr
= remote_address_masked (addr
);
4565 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4566 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
4567 sprintf (p
, ",%d", bp_size
);
4570 getpkt (buf
, (rs
->remote_packet_size
), 0);
4572 return (buf
[0] == 'E');
4575 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4576 return target_write_memory (addr
, contents_cache
, sizeof big_break_insn
);
4578 return memory_remove_breakpoint (addr
, contents_cache
);
4579 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4583 watchpoint_to_Z_packet (int type
)
4597 internal_error (__FILE__
, __LINE__
,
4598 _("hw_bp_to_z: bad watchpoint type %d"), type
);
4603 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
4605 struct remote_state
*rs
= get_remote_state ();
4606 char *buf
= alloca (rs
->remote_packet_size
);
4608 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
4610 if (remote_protocol_Z
[packet
].support
== PACKET_DISABLE
)
4611 error (_("Can't set hardware watchpoints without the '%s' (%s) packet."),
4612 remote_protocol_Z
[packet
].name
,
4613 remote_protocol_Z
[packet
].title
);
4615 sprintf (buf
, "Z%x,", packet
);
4616 p
= strchr (buf
, '\0');
4617 addr
= remote_address_masked (addr
);
4618 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4619 sprintf (p
, ",%x", len
);
4622 getpkt (buf
, (rs
->remote_packet_size
), 0);
4624 switch (packet_ok (buf
, &remote_protocol_Z
[packet
]))
4627 case PACKET_UNKNOWN
:
4632 internal_error (__FILE__
, __LINE__
,
4633 _("remote_insert_watchpoint: reached end of function"));
4638 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
4640 struct remote_state
*rs
= get_remote_state ();
4641 char *buf
= alloca (rs
->remote_packet_size
);
4643 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
4645 if (remote_protocol_Z
[packet
].support
== PACKET_DISABLE
)
4646 error (_("Can't clear hardware watchpoints without the '%s' (%s) packet."),
4647 remote_protocol_Z
[packet
].name
,
4648 remote_protocol_Z
[packet
].title
);
4650 sprintf (buf
, "z%x,", packet
);
4651 p
= strchr (buf
, '\0');
4652 addr
= remote_address_masked (addr
);
4653 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4654 sprintf (p
, ",%x", len
);
4656 getpkt (buf
, (rs
->remote_packet_size
), 0);
4658 switch (packet_ok (buf
, &remote_protocol_Z
[packet
]))
4661 case PACKET_UNKNOWN
:
4666 internal_error (__FILE__
, __LINE__
,
4667 _("remote_remove_watchpoint: reached end of function"));
4671 int remote_hw_watchpoint_limit
= -1;
4672 int remote_hw_breakpoint_limit
= -1;
4675 remote_check_watch_resources (int type
, int cnt
, int ot
)
4677 if (type
== bp_hardware_breakpoint
)
4679 if (remote_hw_breakpoint_limit
== 0)
4681 else if (remote_hw_breakpoint_limit
< 0)
4683 else if (cnt
<= remote_hw_breakpoint_limit
)
4688 if (remote_hw_watchpoint_limit
== 0)
4690 else if (remote_hw_watchpoint_limit
< 0)
4694 else if (cnt
<= remote_hw_watchpoint_limit
)
4701 remote_stopped_by_watchpoint (void)
4703 return remote_stopped_by_watchpoint_p
;
4706 extern int stepped_after_stopped_by_watchpoint
;
4709 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
4712 if (remote_stopped_by_watchpoint ()
4713 || stepped_after_stopped_by_watchpoint
)
4715 *addr_p
= remote_watch_data_address
;
4724 remote_insert_hw_breakpoint (CORE_ADDR addr
, char *shadow
)
4727 struct remote_state
*rs
= get_remote_state ();
4728 char *buf
= alloca (rs
->remote_packet_size
);
4731 /* The length field should be set to the size of a breakpoint
4734 BREAKPOINT_FROM_PC (&addr
, &len
);
4736 if (remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].support
== PACKET_DISABLE
)
4737 error (_("Can't set hardware breakpoint without the '%s' (%s) packet."),
4738 remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].name
,
4739 remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].title
);
4745 addr
= remote_address_masked (addr
);
4746 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4747 sprintf (p
, ",%x", len
);
4750 getpkt (buf
, (rs
->remote_packet_size
), 0);
4752 switch (packet_ok (buf
, &remote_protocol_Z
[Z_PACKET_HARDWARE_BP
]))
4755 case PACKET_UNKNOWN
:
4760 internal_error (__FILE__
, __LINE__
,
4761 _("remote_insert_hw_breakpoint: reached end of function"));
4766 remote_remove_hw_breakpoint (CORE_ADDR addr
, char *shadow
)
4769 struct remote_state
*rs
= get_remote_state ();
4770 char *buf
= alloca (rs
->remote_packet_size
);
4773 /* The length field should be set to the size of a breakpoint
4776 BREAKPOINT_FROM_PC (&addr
, &len
);
4778 if (remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].support
== PACKET_DISABLE
)
4779 error (_("Can't clear hardware breakpoint without the '%s' (%s) packet."),
4780 remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].name
,
4781 remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].title
);
4787 addr
= remote_address_masked (addr
);
4788 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4789 sprintf (p
, ",%x", len
);
4792 getpkt (buf
, (rs
->remote_packet_size
), 0);
4794 switch (packet_ok (buf
, &remote_protocol_Z
[Z_PACKET_HARDWARE_BP
]))
4797 case PACKET_UNKNOWN
:
4802 internal_error (__FILE__
, __LINE__
,
4803 _("remote_remove_hw_breakpoint: reached end of function"));
4806 /* Some targets are only capable of doing downloads, and afterwards
4807 they switch to the remote serial protocol. This function provides
4808 a clean way to get from the download target to the remote target.
4809 It's basically just a wrapper so that we don't have to expose any
4810 of the internal workings of remote.c.
4812 Prior to calling this routine, you should shutdown the current
4813 target code, else you will get the "A program is being debugged
4814 already..." message. Usually a call to pop_target() suffices. */
4817 push_remote_target (char *name
, int from_tty
)
4819 printf_filtered (_("Switching to remote protocol\n"));
4820 remote_open (name
, from_tty
);
4823 /* Table used by the crc32 function to calcuate the checksum. */
4825 static unsigned long crc32_table
[256] =
4828 static unsigned long
4829 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
4831 if (!crc32_table
[1])
4833 /* Initialize the CRC table and the decoding table. */
4837 for (i
= 0; i
< 256; i
++)
4839 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
4840 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
4847 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
4853 /* compare-sections command
4855 With no arguments, compares each loadable section in the exec bfd
4856 with the same memory range on the target, and reports mismatches.
4857 Useful for verifying the image on the target against the exec file.
4858 Depends on the target understanding the new "qCRC:" request. */
4860 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4861 target method (target verify memory) and generic version of the
4862 actual command. This will allow other high-level code (especially
4863 generic_load()) to make use of this target functionality. */
4866 compare_sections_command (char *args
, int from_tty
)
4868 struct remote_state
*rs
= get_remote_state ();
4870 unsigned long host_crc
, target_crc
;
4871 extern bfd
*exec_bfd
;
4872 struct cleanup
*old_chain
;
4875 const char *sectname
;
4876 char *buf
= alloca (rs
->remote_packet_size
);
4883 error (_("command cannot be used without an exec file"));
4884 if (!current_target
.to_shortname
||
4885 strcmp (current_target
.to_shortname
, "remote") != 0)
4886 error (_("command can only be used with remote target"));
4888 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
4890 if (!(s
->flags
& SEC_LOAD
))
4891 continue; /* skip non-loadable section */
4893 size
= bfd_get_section_size (s
);
4895 continue; /* skip zero-length section */
4897 sectname
= bfd_get_section_name (exec_bfd
, s
);
4898 if (args
&& strcmp (args
, sectname
) != 0)
4899 continue; /* not the section selected by user */
4901 matched
= 1; /* do this section */
4903 /* FIXME: assumes lma can fit into long. */
4904 sprintf (buf
, "qCRC:%lx,%lx", (long) lma
, (long) size
);
4907 /* Be clever; compute the host_crc before waiting for target
4909 sectdata
= xmalloc (size
);
4910 old_chain
= make_cleanup (xfree
, sectdata
);
4911 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
4912 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
4914 getpkt (buf
, (rs
->remote_packet_size
), 0);
4916 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
4917 sectname
, paddr (lma
), paddr (lma
+ size
));
4919 error (_("remote target does not support this operation"));
4921 for (target_crc
= 0, tmp
= &buf
[1]; *tmp
; tmp
++)
4922 target_crc
= target_crc
* 16 + fromhex (*tmp
);
4924 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4925 sectname
, paddr (lma
), paddr (lma
+ size
));
4926 if (host_crc
== target_crc
)
4927 printf_filtered ("matched.\n");
4930 printf_filtered ("MIS-MATCHED!\n");
4934 do_cleanups (old_chain
);
4937 warning (_("One or more sections of the remote executable does not match\n\
4938 the loaded file\n"));
4939 if (args
&& !matched
)
4940 printf_filtered (_("No loaded section named '%s'.\n"), args
);
4944 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4945 const char *annex
, void *readbuf
, const void *writebuf
,
4946 ULONGEST offset
, LONGEST len
)
4948 struct remote_state
*rs
= get_remote_state ();
4950 char *buf2
= alloca (rs
->remote_packet_size
);
4951 char *p2
= &buf2
[0];
4954 /* Handle memory using remote_xfer_memory. */
4955 if (object
== TARGET_OBJECT_MEMORY
)
4960 if (writebuf
!= NULL
)
4962 void *buffer
= xmalloc (len
);
4963 struct cleanup
*cleanup
= make_cleanup (xfree
, buffer
);
4964 memcpy (buffer
, writebuf
, len
);
4965 xfered
= remote_xfer_memory (offset
, buffer
, len
, 1, NULL
, ops
);
4966 do_cleanups (cleanup
);
4969 xfered
= remote_xfer_memory (offset
, readbuf
, len
, 0, NULL
, ops
);
4973 else if (xfered
== 0 && errno
== 0)
4979 /* Only handle reads. */
4980 if (writebuf
!= NULL
|| readbuf
== NULL
)
4983 /* Map pre-existing objects onto letters. DO NOT do this for new
4984 objects!!! Instead specify new query packets. */
4987 case TARGET_OBJECT_KOD
:
4990 case TARGET_OBJECT_AVR
:
4994 case TARGET_OBJECT_AUXV
:
4995 if (remote_protocol_qPart_auxv
.support
!= PACKET_DISABLE
)
4997 unsigned int total
= 0;
5000 LONGEST n
= min ((rs
->remote_packet_size
- 2) / 2, len
);
5001 snprintf (buf2
, rs
->remote_packet_size
,
5002 "qPart:auxv:read::%s,%s",
5003 phex_nz (offset
, sizeof offset
),
5004 phex_nz (n
, sizeof n
));
5007 return total
> 0 ? total
: i
;
5009 getpkt (buf2
, rs
->remote_packet_size
, 0);
5010 if (packet_ok (buf2
, &remote_protocol_qPart_auxv
) != PACKET_OK
)
5011 return total
> 0 ? total
: -1;
5012 if (buf2
[0] == 'O' && buf2
[1] == 'K' && buf2
[2] == '\0')
5013 break; /* Got EOF indicator. */
5014 /* Got some data. */
5015 i
= hex2bin (buf2
, readbuf
, len
);
5018 readbuf
= (void *) ((char *) readbuf
+ i
);
5032 /* Note: a zero OFFSET and LEN can be used to query the minimum
5034 if (offset
== 0 && len
== 0)
5035 return (rs
->remote_packet_size
);
5036 /* Minimum outbuf size is (rs->remote_packet_size) - if bufsiz is
5037 not large enough let the caller. */
5038 if (len
< (rs
->remote_packet_size
))
5040 len
= rs
->remote_packet_size
;
5042 /* Except for querying the minimum buffer size, target must be open. */
5044 error (_("remote query is only available after target open"));
5046 gdb_assert (annex
!= NULL
);
5047 gdb_assert (readbuf
!= NULL
);
5052 /* We used one buffer char for the remote protocol q command and
5053 another for the query type. As the remote protocol encapsulation
5054 uses 4 chars plus one extra in case we are debugging
5055 (remote_debug), we have PBUFZIZ - 7 left to pack the query
5058 while (annex
[i
] && (i
< ((rs
->remote_packet_size
) - 8)))
5060 /* Bad caller may have sent forbidden characters. */
5061 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
5066 gdb_assert (annex
[i
] == '\0');
5072 getpkt (readbuf
, len
, 0);
5074 return strlen (readbuf
);
5078 remote_rcmd (char *command
,
5079 struct ui_file
*outbuf
)
5081 struct remote_state
*rs
= get_remote_state ();
5083 char *buf
= alloca (rs
->remote_packet_size
);
5087 error (_("remote rcmd is only available after target open"));
5089 /* Send a NULL command across as an empty command. */
5090 if (command
== NULL
)
5093 /* The query prefix. */
5094 strcpy (buf
, "qRcmd,");
5095 p
= strchr (buf
, '\0');
5097 if ((strlen (buf
) + strlen (command
) * 2 + 8/*misc*/) > (rs
->remote_packet_size
))
5098 error (_("\"monitor\" command ``%s'' is too long."), command
);
5100 /* Encode the actual command. */
5101 bin2hex (command
, p
, 0);
5103 if (putpkt (buf
) < 0)
5104 error (_("Communication problem with target."));
5106 /* get/display the response */
5109 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
5111 getpkt (buf
, (rs
->remote_packet_size
), 0);
5113 error (_("Target does not support this command."));
5114 if (buf
[0] == 'O' && buf
[1] != 'K')
5116 remote_console_output (buf
+ 1); /* 'O' message from stub. */
5119 if (strcmp (buf
, "OK") == 0)
5121 if (strlen (buf
) == 3 && buf
[0] == 'E'
5122 && isdigit (buf
[1]) && isdigit (buf
[2]))
5124 error (_("Protocol error with Rcmd"));
5126 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
5128 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
5129 fputc_unfiltered (c
, outbuf
);
5136 packet_command (char *args
, int from_tty
)
5138 struct remote_state
*rs
= get_remote_state ();
5139 char *buf
= alloca (rs
->remote_packet_size
);
5142 error (_("command can only be used with remote target"));
5145 error (_("remote-packet command requires packet text as argument"));
5147 puts_filtered ("sending: ");
5148 print_packet (args
);
5149 puts_filtered ("\n");
5152 getpkt (buf
, (rs
->remote_packet_size
), 0);
5153 puts_filtered ("received: ");
5155 puts_filtered ("\n");
5159 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
5161 static void display_thread_info (struct gdb_ext_thread_info
*info
);
5163 static void threadset_test_cmd (char *cmd
, int tty
);
5165 static void threadalive_test (char *cmd
, int tty
);
5167 static void threadlist_test_cmd (char *cmd
, int tty
);
5169 int get_and_display_threadinfo (threadref
*ref
);
5171 static void threadinfo_test_cmd (char *cmd
, int tty
);
5173 static int thread_display_step (threadref
*ref
, void *context
);
5175 static void threadlist_update_test_cmd (char *cmd
, int tty
);
5177 static void init_remote_threadtests (void);
5179 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
5182 threadset_test_cmd (char *cmd
, int tty
)
5184 int sample_thread
= SAMPLE_THREAD
;
5186 printf_filtered (_("Remote threadset test\n"));
5187 set_thread (sample_thread
, 1);
5192 threadalive_test (char *cmd
, int tty
)
5194 int sample_thread
= SAMPLE_THREAD
;
5196 if (remote_thread_alive (pid_to_ptid (sample_thread
)))
5197 printf_filtered ("PASS: Thread alive test\n");
5199 printf_filtered ("FAIL: Thread alive test\n");
5202 void output_threadid (char *title
, threadref
*ref
);
5205 output_threadid (char *title
, threadref
*ref
)
5209 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
5211 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
5215 threadlist_test_cmd (char *cmd
, int tty
)
5218 threadref nextthread
;
5219 int done
, result_count
;
5220 threadref threadlist
[3];
5222 printf_filtered ("Remote Threadlist test\n");
5223 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
5224 &result_count
, &threadlist
[0]))
5225 printf_filtered ("FAIL: threadlist test\n");
5228 threadref
*scan
= threadlist
;
5229 threadref
*limit
= scan
+ result_count
;
5231 while (scan
< limit
)
5232 output_threadid (" thread ", scan
++);
5237 display_thread_info (struct gdb_ext_thread_info
*info
)
5239 output_threadid ("Threadid: ", &info
->threadid
);
5240 printf_filtered ("Name: %s\n ", info
->shortname
);
5241 printf_filtered ("State: %s\n", info
->display
);
5242 printf_filtered ("other: %s\n\n", info
->more_display
);
5246 get_and_display_threadinfo (threadref
*ref
)
5250 struct gdb_ext_thread_info threadinfo
;
5252 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
5253 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
5254 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
5255 display_thread_info (&threadinfo
);
5260 threadinfo_test_cmd (char *cmd
, int tty
)
5262 int athread
= SAMPLE_THREAD
;
5266 int_to_threadref (&thread
, athread
);
5267 printf_filtered ("Remote Threadinfo test\n");
5268 if (!get_and_display_threadinfo (&thread
))
5269 printf_filtered ("FAIL cannot get thread info\n");
5273 thread_display_step (threadref
*ref
, void *context
)
5275 /* output_threadid(" threadstep ",ref); *//* simple test */
5276 return get_and_display_threadinfo (ref
);
5280 threadlist_update_test_cmd (char *cmd
, int tty
)
5282 printf_filtered ("Remote Threadlist update test\n");
5283 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
5287 init_remote_threadtests (void)
5289 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
5290 Fetch and print the remote list of thread identifiers, one pkt only"));
5291 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
5292 _("Fetch and display info about one thread"));
5293 add_com ("tset", class_obscure
, threadset_test_cmd
,
5294 _("Test setting to a different thread"));
5295 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
5296 _("Iterate through updating all remote thread info"));
5297 add_com ("talive", class_obscure
, threadalive_test
,
5298 _(" Remote thread alive test "));
5303 /* Convert a thread ID to a string. Returns the string in a static
5307 remote_pid_to_str (ptid_t ptid
)
5309 static char buf
[32];
5312 size
= snprintf (buf
, sizeof buf
, "thread %d", ptid_get_pid (ptid
));
5313 gdb_assert (size
< sizeof buf
);
5318 init_remote_ops (void)
5320 remote_ops
.to_shortname
= "remote";
5321 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
5323 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5324 Specify the serial device it is connected to\n\
5325 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
5326 remote_ops
.to_open
= remote_open
;
5327 remote_ops
.to_close
= remote_close
;
5328 remote_ops
.to_detach
= remote_detach
;
5329 remote_ops
.to_disconnect
= remote_disconnect
;
5330 remote_ops
.to_resume
= remote_resume
;
5331 remote_ops
.to_wait
= remote_wait
;
5332 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
5333 remote_ops
.to_store_registers
= remote_store_registers
;
5334 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5335 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
5336 remote_ops
.to_files_info
= remote_files_info
;
5337 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5338 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5339 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
5340 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
5341 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
5342 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
5343 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
5344 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
5345 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
5346 remote_ops
.to_kill
= remote_kill
;
5347 remote_ops
.to_load
= generic_load
;
5348 remote_ops
.to_mourn_inferior
= remote_mourn
;
5349 remote_ops
.to_thread_alive
= remote_thread_alive
;
5350 remote_ops
.to_find_new_threads
= remote_threads_info
;
5351 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
5352 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
5353 remote_ops
.to_stop
= remote_stop
;
5354 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
5355 remote_ops
.to_rcmd
= remote_rcmd
;
5356 remote_ops
.to_stratum
= process_stratum
;
5357 remote_ops
.to_has_all_memory
= 1;
5358 remote_ops
.to_has_memory
= 1;
5359 remote_ops
.to_has_stack
= 1;
5360 remote_ops
.to_has_registers
= 1;
5361 remote_ops
.to_has_execution
= 1;
5362 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5363 remote_ops
.to_magic
= OPS_MAGIC
;
5366 /* Set up the extended remote vector by making a copy of the standard
5367 remote vector and adding to it. */
5370 init_extended_remote_ops (void)
5372 extended_remote_ops
= remote_ops
;
5374 extended_remote_ops
.to_shortname
= "extended-remote";
5375 extended_remote_ops
.to_longname
=
5376 "Extended remote serial target in gdb-specific protocol";
5377 extended_remote_ops
.to_doc
=
5378 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5379 Specify the serial device it is connected to (e.g. /dev/ttya).",
5380 extended_remote_ops
.to_open
= extended_remote_open
;
5381 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
5382 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5386 remote_can_async_p (void)
5388 /* We're async whenever the serial device is. */
5389 return (current_target
.to_async_mask_value
) && serial_can_async_p (remote_desc
);
5393 remote_is_async_p (void)
5395 /* We're async whenever the serial device is. */
5396 return (current_target
.to_async_mask_value
) && serial_is_async_p (remote_desc
);
5399 /* Pass the SERIAL event on and up to the client. One day this code
5400 will be able to delay notifying the client of an event until the
5401 point where an entire packet has been received. */
5403 static void (*async_client_callback
) (enum inferior_event_type event_type
,
5405 static void *async_client_context
;
5406 static serial_event_ftype remote_async_serial_handler
;
5409 remote_async_serial_handler (struct serial
*scb
, void *context
)
5411 /* Don't propogate error information up to the client. Instead let
5412 the client find out about the error by querying the target. */
5413 async_client_callback (INF_REG_EVENT
, async_client_context
);
5417 remote_async (void (*callback
) (enum inferior_event_type event_type
,
5418 void *context
), void *context
)
5420 if (current_target
.to_async_mask_value
== 0)
5421 internal_error (__FILE__
, __LINE__
,
5422 _("Calling remote_async when async is masked"));
5424 if (callback
!= NULL
)
5426 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
5427 async_client_callback
= callback
;
5428 async_client_context
= context
;
5431 serial_async (remote_desc
, NULL
, NULL
);
5434 /* Target async and target extended-async.
5436 This are temporary targets, until it is all tested. Eventually
5437 async support will be incorporated int the usual 'remote'
5441 init_remote_async_ops (void)
5443 remote_async_ops
.to_shortname
= "async";
5444 remote_async_ops
.to_longname
=
5445 "Remote serial target in async version of the gdb-specific protocol";
5446 remote_async_ops
.to_doc
=
5447 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5448 Specify the serial device it is connected to (e.g. /dev/ttya).";
5449 remote_async_ops
.to_open
= remote_async_open
;
5450 remote_async_ops
.to_close
= remote_close
;
5451 remote_async_ops
.to_detach
= remote_detach
;
5452 remote_async_ops
.to_disconnect
= remote_disconnect
;
5453 remote_async_ops
.to_resume
= remote_async_resume
;
5454 remote_async_ops
.to_wait
= remote_async_wait
;
5455 remote_async_ops
.to_fetch_registers
= remote_fetch_registers
;
5456 remote_async_ops
.to_store_registers
= remote_store_registers
;
5457 remote_async_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5458 remote_async_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
5459 remote_async_ops
.to_files_info
= remote_files_info
;
5460 remote_async_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5461 remote_async_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5462 remote_async_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
5463 remote_async_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
5464 remote_async_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
5465 remote_async_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
5466 remote_async_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
5467 remote_async_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
5468 remote_async_ops
.to_stopped_data_address
= remote_stopped_data_address
;
5469 remote_async_ops
.to_terminal_inferior
= remote_async_terminal_inferior
;
5470 remote_async_ops
.to_terminal_ours
= remote_async_terminal_ours
;
5471 remote_async_ops
.to_kill
= remote_async_kill
;
5472 remote_async_ops
.to_load
= generic_load
;
5473 remote_async_ops
.to_mourn_inferior
= remote_async_mourn
;
5474 remote_async_ops
.to_thread_alive
= remote_thread_alive
;
5475 remote_async_ops
.to_find_new_threads
= remote_threads_info
;
5476 remote_async_ops
.to_pid_to_str
= remote_pid_to_str
;
5477 remote_async_ops
.to_extra_thread_info
= remote_threads_extra_info
;
5478 remote_async_ops
.to_stop
= remote_stop
;
5479 remote_async_ops
.to_xfer_partial
= remote_xfer_partial
;
5480 remote_async_ops
.to_rcmd
= remote_rcmd
;
5481 remote_async_ops
.to_stratum
= process_stratum
;
5482 remote_async_ops
.to_has_all_memory
= 1;
5483 remote_async_ops
.to_has_memory
= 1;
5484 remote_async_ops
.to_has_stack
= 1;
5485 remote_async_ops
.to_has_registers
= 1;
5486 remote_async_ops
.to_has_execution
= 1;
5487 remote_async_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5488 remote_async_ops
.to_can_async_p
= remote_can_async_p
;
5489 remote_async_ops
.to_is_async_p
= remote_is_async_p
;
5490 remote_async_ops
.to_async
= remote_async
;
5491 remote_async_ops
.to_async_mask_value
= 1;
5492 remote_async_ops
.to_magic
= OPS_MAGIC
;
5495 /* Set up the async extended remote vector by making a copy of the standard
5496 remote vector and adding to it. */
5499 init_extended_async_remote_ops (void)
5501 extended_async_remote_ops
= remote_async_ops
;
5503 extended_async_remote_ops
.to_shortname
= "extended-async";
5504 extended_async_remote_ops
.to_longname
=
5505 "Extended remote serial target in async gdb-specific protocol";
5506 extended_async_remote_ops
.to_doc
=
5507 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5508 Specify the serial device it is connected to (e.g. /dev/ttya).",
5509 extended_async_remote_ops
.to_open
= extended_remote_async_open
;
5510 extended_async_remote_ops
.to_create_inferior
= extended_remote_async_create_inferior
;
5511 extended_async_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5515 set_remote_cmd (char *args
, int from_tty
)
5520 show_remote_cmd (char *args
, int from_tty
)
5522 /* FIXME: cagney/2002-06-15: This function should iterate over
5523 remote_show_cmdlist for a list of sub commands to show. */
5524 show_remote_protocol_Z_packet_cmd (gdb_stdout
, from_tty
, NULL
, NULL
);
5525 show_remote_protocol_P_packet_cmd (gdb_stdout
, from_tty
, NULL
, NULL
);
5526 show_remote_protocol_p_packet_cmd (gdb_stdout
, from_tty
, NULL
, NULL
);
5527 show_remote_protocol_qSymbol_packet_cmd (gdb_stdout
, from_tty
, NULL
, NULL
);
5528 show_remote_protocol_vcont_packet_cmd (gdb_stdout
, from_tty
, NULL
, NULL
);
5529 show_remote_protocol_binary_download_cmd (gdb_stdout
, from_tty
, NULL
, NULL
);
5530 show_remote_protocol_qPart_auxv_packet_cmd (gdb_stdout
, from_tty
, NULL
, NULL
);
5534 build_remote_gdbarch_data (void)
5536 remote_address_size
= TARGET_ADDR_BIT
;
5539 /* Saved pointer to previous owner of the new_objfile event. */
5540 static void (*remote_new_objfile_chain
) (struct objfile
*);
5542 /* Function to be called whenever a new objfile (shlib) is detected. */
5544 remote_new_objfile (struct objfile
*objfile
)
5546 if (remote_desc
!= 0) /* Have a remote connection. */
5548 remote_check_symbols (objfile
);
5550 /* Call predecessor on chain, if any. */
5551 if (remote_new_objfile_chain
!= 0 &&
5553 remote_new_objfile_chain (objfile
);
5557 _initialize_remote (void)
5559 static struct cmd_list_element
*remote_set_cmdlist
;
5560 static struct cmd_list_element
*remote_show_cmdlist
;
5561 struct cmd_list_element
*tmpcmd
;
5563 /* architecture specific data */
5564 remote_gdbarch_data_handle
=
5565 gdbarch_data_register_post_init (init_remote_state
);
5567 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5568 that the remote protocol has been initialized. */
5569 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size
);
5570 deprecated_register_gdbarch_swap (NULL
, 0, build_remote_gdbarch_data
);
5573 add_target (&remote_ops
);
5575 init_extended_remote_ops ();
5576 add_target (&extended_remote_ops
);
5578 init_remote_async_ops ();
5579 add_target (&remote_async_ops
);
5581 init_extended_async_remote_ops ();
5582 add_target (&extended_async_remote_ops
);
5584 /* Hook into new objfile notification. */
5585 remote_new_objfile_chain
= deprecated_target_new_objfile_hook
;
5586 deprecated_target_new_objfile_hook
= remote_new_objfile
;
5589 init_remote_threadtests ();
5592 /* set/show remote ... */
5594 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
5595 Remote protocol specific variables\n\
5596 Configure various remote-protocol specific variables such as\n\
5597 the packets being used"),
5598 &remote_set_cmdlist
, "set remote ",
5599 0 /* allow-unknown */, &setlist
);
5600 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
5601 Remote protocol specific variables\n\
5602 Configure various remote-protocol specific variables such as\n\
5603 the packets being used"),
5604 &remote_show_cmdlist
, "show remote ",
5605 0 /* allow-unknown */, &showlist
);
5607 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
5608 Compare section data on target to the exec file.\n\
5609 Argument is a single section name (default: all loaded sections)."),
5612 add_cmd ("packet", class_maintenance
, packet_command
, _("\
5613 Send an arbitrary packet to a remote target.\n\
5614 maintenance packet TEXT\n\
5615 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5616 this command sends the string TEXT to the inferior, and displays the\n\
5617 response packet. GDB supplies the initial `$' character, and the\n\
5618 terminating `#' character and checksum."),
5621 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
5622 Set whether to send break if interrupted."), _("\
5623 Show whether to send break if interrupted."), _("\
5624 If set, a break, instead of a cntrl-c, is sent to the remote target."),
5625 NULL
, NULL
, /* FIXME: i18n: Whether to send break if interrupted is %s. */
5626 &setlist
, &showlist
);
5628 /* Install commands for configuring memory read/write packets. */
5630 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
5631 Set the maximum number of bytes per memory write packet (deprecated)."),
5633 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
5634 Show the maximum number of bytes per memory write packet (deprecated)."),
5636 add_cmd ("memory-write-packet-size", no_class
,
5637 set_memory_write_packet_size
, _("\
5638 Set the maximum number of bytes per memory-write packet.\n\
5639 Specify the number of bytes in a packet or 0 (zero) for the\n\
5640 default packet size. The actual limit is further reduced\n\
5641 dependent on the target. Specify ``fixed'' to disable the\n\
5642 further restriction and ``limit'' to enable that restriction."),
5643 &remote_set_cmdlist
);
5644 add_cmd ("memory-read-packet-size", no_class
,
5645 set_memory_read_packet_size
, _("\
5646 Set the maximum number of bytes per memory-read packet.\n\
5647 Specify the number of bytes in a packet or 0 (zero) for the\n\
5648 default packet size. The actual limit is further reduced\n\
5649 dependent on the target. Specify ``fixed'' to disable the\n\
5650 further restriction and ``limit'' to enable that restriction."),
5651 &remote_set_cmdlist
);
5652 add_cmd ("memory-write-packet-size", no_class
,
5653 show_memory_write_packet_size
,
5654 _("Show the maximum number of bytes per memory-write packet."),
5655 &remote_show_cmdlist
);
5656 add_cmd ("memory-read-packet-size", no_class
,
5657 show_memory_read_packet_size
,
5658 _("Show the maximum number of bytes per memory-read packet."),
5659 &remote_show_cmdlist
);
5661 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
5662 &remote_hw_watchpoint_limit
, _("\
5663 Set the maximum number of target hardware watchpoints."), _("\
5664 Show the maximum number of target hardware watchpoints."), _("\
5665 Specify a negative limit for unlimited."),
5666 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
5667 &remote_set_cmdlist
, &remote_show_cmdlist
);
5668 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
5669 &remote_hw_breakpoint_limit
, _("\
5670 Set the maximum number of target hardware breakpoints."), _("\
5671 Show the maximum number of target hardware breakpoints."), _("\
5672 Specify a negative limit for unlimited."),
5673 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
5674 &remote_set_cmdlist
, &remote_show_cmdlist
);
5676 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
5677 &remote_address_size
, _("\
5678 Set the maximum size of the address (in bits) in a memory packet."), _("\
5679 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
5681 NULL
, /* FIXME: i18n: */
5682 &setlist
, &showlist
);
5684 add_packet_config_cmd (&remote_protocol_binary_download
,
5685 "X", "binary-download",
5686 set_remote_protocol_binary_download_cmd
,
5687 show_remote_protocol_binary_download_cmd
,
5688 &remote_set_cmdlist
, &remote_show_cmdlist
,
5691 add_packet_config_cmd (&remote_protocol_vcont
,
5692 "vCont", "verbose-resume",
5693 set_remote_protocol_vcont_packet_cmd
,
5694 show_remote_protocol_vcont_packet_cmd
,
5695 &remote_set_cmdlist
, &remote_show_cmdlist
,
5698 add_packet_config_cmd (&remote_protocol_qSymbol
,
5699 "qSymbol", "symbol-lookup",
5700 set_remote_protocol_qSymbol_packet_cmd
,
5701 show_remote_protocol_qSymbol_packet_cmd
,
5702 &remote_set_cmdlist
, &remote_show_cmdlist
,
5705 add_packet_config_cmd (&remote_protocol_P
,
5706 "P", "set-register",
5707 set_remote_protocol_P_packet_cmd
,
5708 show_remote_protocol_P_packet_cmd
,
5709 &remote_set_cmdlist
, &remote_show_cmdlist
,
5712 add_packet_config_cmd (&remote_protocol_p
,
5713 "p", "fetch-register",
5714 set_remote_protocol_p_packet_cmd
,
5715 show_remote_protocol_p_packet_cmd
,
5716 &remote_set_cmdlist
, &remote_show_cmdlist
,
5719 add_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
],
5720 "Z0", "software-breakpoint",
5721 set_remote_protocol_Z_software_bp_packet_cmd
,
5722 show_remote_protocol_Z_software_bp_packet_cmd
,
5723 &remote_set_cmdlist
, &remote_show_cmdlist
,
5726 add_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_HARDWARE_BP
],
5727 "Z1", "hardware-breakpoint",
5728 set_remote_protocol_Z_hardware_bp_packet_cmd
,
5729 show_remote_protocol_Z_hardware_bp_packet_cmd
,
5730 &remote_set_cmdlist
, &remote_show_cmdlist
,
5733 add_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_WRITE_WP
],
5734 "Z2", "write-watchpoint",
5735 set_remote_protocol_Z_write_wp_packet_cmd
,
5736 show_remote_protocol_Z_write_wp_packet_cmd
,
5737 &remote_set_cmdlist
, &remote_show_cmdlist
,
5740 add_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_READ_WP
],
5741 "Z3", "read-watchpoint",
5742 set_remote_protocol_Z_read_wp_packet_cmd
,
5743 show_remote_protocol_Z_read_wp_packet_cmd
,
5744 &remote_set_cmdlist
, &remote_show_cmdlist
,
5747 add_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_ACCESS_WP
],
5748 "Z4", "access-watchpoint",
5749 set_remote_protocol_Z_access_wp_packet_cmd
,
5750 show_remote_protocol_Z_access_wp_packet_cmd
,
5751 &remote_set_cmdlist
, &remote_show_cmdlist
,
5754 add_packet_config_cmd (&remote_protocol_qPart_auxv
,
5755 "qPart_auxv", "read-aux-vector",
5756 set_remote_protocol_qPart_auxv_packet_cmd
,
5757 show_remote_protocol_qPart_auxv_packet_cmd
,
5758 &remote_set_cmdlist
, &remote_show_cmdlist
,
5761 /* Keep the old ``set remote Z-packet ...'' working. */
5762 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
5763 &remote_Z_packet_detect
, _("\
5764 Set use of remote protocol `Z' packets"), _("\
5765 Show use of remote protocol `Z' packets "), _("\
5766 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
5768 set_remote_protocol_Z_packet_cmd
,
5769 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
5770 &remote_set_cmdlist
, &remote_show_cmdlist
);
5772 /* Eventually initialize fileio. See fileio.c */
5773 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);