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
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"
48 #include "event-loop.h"
49 #include "event-top.h"
55 #include "gdbcore.h" /* for exec_bfd */
57 #include "remote-fileio.h"
59 /* Prototypes for local functions. */
60 static void cleanup_sigint_signal_handler (void *dummy
);
61 static void initialize_sigint_signal_handler (void);
62 static int getpkt_sane (char *buf
, long sizeof_buf
, int forever
);
64 static void handle_remote_sigint (int);
65 static void handle_remote_sigint_twice (int);
66 static void async_remote_interrupt (gdb_client_data
);
67 void async_remote_interrupt_twice (gdb_client_data
);
69 static void build_remote_gdbarch_data (void);
71 static void remote_files_info (struct target_ops
*ignore
);
73 static int remote_xfer_memory (CORE_ADDR memaddr
, char *myaddr
,
74 int len
, int should_write
,
75 struct mem_attrib
*attrib
,
76 struct target_ops
*target
);
78 static void remote_prepare_to_store (void);
80 static void remote_fetch_registers (int regno
);
82 static void remote_resume (ptid_t ptid
, int step
,
83 enum target_signal siggnal
);
84 static void remote_async_resume (ptid_t ptid
, int step
,
85 enum target_signal siggnal
);
86 static int remote_start_remote (struct ui_out
*uiout
, void *dummy
);
88 static void remote_open (char *name
, int from_tty
);
89 static void remote_async_open (char *name
, int from_tty
);
91 static void extended_remote_open (char *name
, int from_tty
);
92 static void extended_remote_async_open (char *name
, int from_tty
);
94 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
,
97 static void remote_close (int quitting
);
99 static void remote_store_registers (int regno
);
101 static void remote_mourn (void);
102 static void remote_async_mourn (void);
104 static void extended_remote_restart (void);
106 static void extended_remote_mourn (void);
108 static void remote_mourn_1 (struct target_ops
*);
110 static void remote_send (char *buf
, long sizeof_buf
);
112 static int readchar (int timeout
);
114 static ptid_t
remote_wait (ptid_t ptid
,
115 struct target_waitstatus
*status
);
116 static ptid_t
remote_async_wait (ptid_t ptid
,
117 struct target_waitstatus
*status
);
119 static void remote_kill (void);
120 static void remote_async_kill (void);
122 static int tohex (int nib
);
124 static void remote_detach (char *args
, int from_tty
);
126 static void remote_interrupt (int signo
);
128 static void remote_interrupt_twice (int signo
);
130 static void interrupt_query (void);
132 static void set_thread (int, int);
134 static int remote_thread_alive (ptid_t
);
136 static void get_offsets (void);
138 static long read_frame (char *buf
, long sizeof_buf
);
140 static int remote_insert_breakpoint (CORE_ADDR
, char *);
142 static int remote_remove_breakpoint (CORE_ADDR
, char *);
144 static int hexnumlen (ULONGEST num
);
146 static void init_remote_ops (void);
148 static void init_extended_remote_ops (void);
150 static void remote_stop (void);
152 static int ishex (int ch
, int *val
);
154 static int stubhex (int ch
);
156 static int hexnumstr (char *, ULONGEST
);
158 static int hexnumnstr (char *, ULONGEST
, int);
160 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
162 static void print_packet (char *);
164 static unsigned long crc32 (unsigned char *, int, unsigned int);
166 static void compare_sections_command (char *, int);
168 static void packet_command (char *, int);
170 static int stub_unpack_int (char *buff
, int fieldlength
);
172 static ptid_t
remote_current_thread (ptid_t oldptid
);
174 static void remote_find_new_threads (void);
176 static void record_currthread (int currthread
);
178 static int fromhex (int a
);
180 static int hex2bin (const char *hex
, char *bin
, int count
);
182 static int bin2hex (const char *bin
, char *hex
, int count
);
184 static int putpkt_binary (char *buf
, int cnt
);
186 static void check_binary_download (CORE_ADDR addr
);
188 struct packet_config
;
190 static void show_packet_config_cmd (struct packet_config
*config
);
192 static void update_packet_config (struct packet_config
*config
);
194 void _initialize_remote (void);
196 /* Description of the remote protocol. Strictly speaking, when the
197 target is open()ed, remote.c should create a per-target description
198 of the remote protocol using that target's architecture.
199 Unfortunately, the target stack doesn't include local state. For
200 the moment keep the information in the target's architecture
205 long offset
; /* Offset into G packet. */
206 long regnum
; /* GDB's internal register number. */
207 LONGEST pnum
; /* Remote protocol register number. */
208 int in_g_packet
; /* Always part of G packet. */
209 /* long size in bytes; == register_size (current_gdbarch, regnum);
211 /* char *name; == REGISTER_NAME (regnum); at present. */
216 /* Description of the remote protocol registers. */
217 long sizeof_g_packet
;
219 /* Description of the remote protocol registers indexed by REGNUM
220 (making an array of NUM_REGS + NUM_PSEUDO_REGS in size). */
221 struct packet_reg
*regs
;
223 /* This is the size (in chars) of the first response to the ``g''
224 packet. It is used as a heuristic when determining the maximum
225 size of memory-read and memory-write packets. A target will
226 typically only reserve a buffer large enough to hold the ``g''
227 packet. The size does not include packet overhead (headers and
229 long actual_register_packet_size
;
231 /* This is the maximum size (in chars) of a non read/write packet.
232 It is also used as a cap on the size of read/write packets. */
233 long remote_packet_size
;
237 /* Handle for retreving the remote protocol data from gdbarch. */
238 static struct gdbarch_data
*remote_gdbarch_data_handle
;
240 static struct remote_state
*
241 get_remote_state (void)
243 return gdbarch_data (current_gdbarch
, remote_gdbarch_data_handle
);
247 init_remote_state (struct gdbarch
*gdbarch
)
250 struct remote_state
*rs
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_state
);
252 if (deprecated_register_bytes () != 0)
253 rs
->sizeof_g_packet
= deprecated_register_bytes ();
255 rs
->sizeof_g_packet
= 0;
257 /* Assume a 1:1 regnum<->pnum table. */
258 rs
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
, NUM_REGS
+ NUM_PSEUDO_REGS
,
260 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
262 struct packet_reg
*r
= &rs
->regs
[regnum
];
265 r
->offset
= DEPRECATED_REGISTER_BYTE (regnum
);
266 r
->in_g_packet
= (regnum
< NUM_REGS
);
267 /* ...name = REGISTER_NAME (regnum); */
269 /* Compute packet size by accumulating the size of all registers. */
270 if (deprecated_register_bytes () == 0)
271 rs
->sizeof_g_packet
+= register_size (current_gdbarch
, regnum
);
274 /* Default maximum number of characters in a packet body. Many
275 remote stubs have a hardwired buffer size of 400 bytes
276 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
277 as the maximum packet-size to ensure that the packet and an extra
278 NUL character can always fit in the buffer. This stops GDB
279 trashing stubs that try to squeeze an extra NUL into what is
280 already a full buffer (As of 1999-12-04 that was most stubs. */
281 rs
->remote_packet_size
= 400 - 1;
283 /* Should rs->sizeof_g_packet needs more space than the
284 default, adjust the size accordingly. Remember that each byte is
285 encoded as two characters. 32 is the overhead for the packet
286 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
287 (``$NN:G...#NN'') is a better guess, the below has been padded a
289 if (rs
->sizeof_g_packet
> ((rs
->remote_packet_size
- 32) / 2))
290 rs
->remote_packet_size
= (rs
->sizeof_g_packet
* 2 + 32);
292 /* This one is filled in when a ``g'' packet is received. */
293 rs
->actual_register_packet_size
= 0;
298 static struct packet_reg
*
299 packet_reg_from_regnum (struct remote_state
*rs
, long regnum
)
301 if (regnum
< 0 && regnum
>= NUM_REGS
+ NUM_PSEUDO_REGS
)
305 struct packet_reg
*r
= &rs
->regs
[regnum
];
306 gdb_assert (r
->regnum
== regnum
);
311 static struct packet_reg
*
312 packet_reg_from_pnum (struct remote_state
*rs
, LONGEST pnum
)
315 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
317 struct packet_reg
*r
= &rs
->regs
[i
];
324 /* FIXME: graces/2002-08-08: These variables should eventually be
325 bound to an instance of the target object (as in gdbarch-tdep()),
326 when such a thing exists. */
328 /* This is set to the data address of the access causing the target
329 to stop for a watchpoint. */
330 static CORE_ADDR remote_watch_data_address
;
332 /* This is non-zero if taregt stopped for a watchpoint. */
333 static int remote_stopped_by_watchpoint_p
;
336 static struct target_ops remote_ops
;
338 static struct target_ops extended_remote_ops
;
340 /* Temporary target ops. Just like the remote_ops and
341 extended_remote_ops, but with asynchronous support. */
342 static struct target_ops remote_async_ops
;
344 static struct target_ops extended_async_remote_ops
;
346 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
347 ``forever'' still use the normal timeout mechanism. This is
348 currently used by the ASYNC code to guarentee that target reads
349 during the initial connect always time-out. Once getpkt has been
350 modified to return a timeout indication and, in turn
351 remote_wait()/wait_for_inferior() have gained a timeout parameter
353 static int wait_forever_enabled_p
= 1;
356 /* This variable chooses whether to send a ^C or a break when the user
357 requests program interruption. Although ^C is usually what remote
358 systems expect, and that is the default here, sometimes a break is
359 preferable instead. */
361 static int remote_break
;
363 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
364 remote_open knows that we don't have a file open when the program
366 static struct serial
*remote_desc
= NULL
;
368 /* This variable sets the number of bits in an address that are to be
369 sent in a memory ("M" or "m") packet. Normally, after stripping
370 leading zeros, the entire address would be sent. This variable
371 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
372 initial implementation of remote.c restricted the address sent in
373 memory packets to ``host::sizeof long'' bytes - (typically 32
374 bits). Consequently, for 64 bit targets, the upper 32 bits of an
375 address was never sent. Since fixing this bug may cause a break in
376 some remote targets this variable is principly provided to
377 facilitate backward compatibility. */
379 static int remote_address_size
;
381 /* Tempoary to track who currently owns the terminal. See
382 target_async_terminal_* for more details. */
384 static int remote_async_terminal_ours_p
;
387 /* User configurable variables for the number of characters in a
388 memory read/write packet. MIN ((rs->remote_packet_size),
389 rs->sizeof_g_packet) is the default. Some targets need smaller
390 values (fifo overruns, et.al.) and some users need larger values
391 (speed up transfers). The variables ``preferred_*'' (the user
392 request), ``current_*'' (what was actually set) and ``forced_*''
393 (Positive - a soft limit, negative - a hard limit). */
395 struct memory_packet_config
402 /* Compute the current size of a read/write packet. Since this makes
403 use of ``actual_register_packet_size'' the computation is dynamic. */
406 get_memory_packet_size (struct memory_packet_config
*config
)
408 struct remote_state
*rs
= get_remote_state ();
409 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
410 law?) that some hosts don't cope very well with large alloca()
411 calls. Eventually the alloca() code will be replaced by calls to
412 xmalloc() and make_cleanups() allowing this restriction to either
413 be lifted or removed. */
414 #ifndef MAX_REMOTE_PACKET_SIZE
415 #define MAX_REMOTE_PACKET_SIZE 16384
417 /* NOTE: 16 is just chosen at random. */
418 #ifndef MIN_REMOTE_PACKET_SIZE
419 #define MIN_REMOTE_PACKET_SIZE 16
424 if (config
->size
<= 0)
425 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
427 what_they_get
= config
->size
;
431 what_they_get
= (rs
->remote_packet_size
);
432 /* Limit the packet to the size specified by the user. */
434 && what_they_get
> config
->size
)
435 what_they_get
= config
->size
;
436 /* Limit it to the size of the targets ``g'' response. */
437 if ((rs
->actual_register_packet_size
) > 0
438 && what_they_get
> (rs
->actual_register_packet_size
))
439 what_they_get
= (rs
->actual_register_packet_size
);
441 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
442 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
443 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
444 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
445 return what_they_get
;
448 /* Update the size of a read/write packet. If they user wants
449 something really big then do a sanity check. */
452 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
454 int fixed_p
= config
->fixed_p
;
455 long size
= config
->size
;
457 error ("Argument required (integer, `fixed' or `limited').");
458 else if (strcmp (args
, "hard") == 0
459 || strcmp (args
, "fixed") == 0)
461 else if (strcmp (args
, "soft") == 0
462 || strcmp (args
, "limit") == 0)
467 size
= strtoul (args
, &end
, 0);
469 error ("Invalid %s (bad syntax).", config
->name
);
471 /* Instead of explicitly capping the size of a packet to
472 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
473 instead allowed to set the size to something arbitrarily
475 if (size
> MAX_REMOTE_PACKET_SIZE
)
476 error ("Invalid %s (too large).", config
->name
);
480 if (fixed_p
&& !config
->fixed_p
)
482 if (! query ("The target may not be able to correctly handle a %s\n"
483 "of %ld bytes. Change the packet size? ",
485 error ("Packet size not changed.");
487 /* Update the config. */
488 config
->fixed_p
= fixed_p
;
493 show_memory_packet_size (struct memory_packet_config
*config
)
495 printf_filtered ("The %s is %ld. ", config
->name
, config
->size
);
497 printf_filtered ("Packets are fixed at %ld bytes.\n",
498 get_memory_packet_size (config
));
500 printf_filtered ("Packets are limited to %ld bytes.\n",
501 get_memory_packet_size (config
));
504 static struct memory_packet_config memory_write_packet_config
=
506 "memory-write-packet-size",
510 set_memory_write_packet_size (char *args
, int from_tty
)
512 set_memory_packet_size (args
, &memory_write_packet_config
);
516 show_memory_write_packet_size (char *args
, int from_tty
)
518 show_memory_packet_size (&memory_write_packet_config
);
522 get_memory_write_packet_size (void)
524 return get_memory_packet_size (&memory_write_packet_config
);
527 static struct memory_packet_config memory_read_packet_config
=
529 "memory-read-packet-size",
533 set_memory_read_packet_size (char *args
, int from_tty
)
535 set_memory_packet_size (args
, &memory_read_packet_config
);
539 show_memory_read_packet_size (char *args
, int from_tty
)
541 show_memory_packet_size (&memory_read_packet_config
);
545 get_memory_read_packet_size (void)
547 struct remote_state
*rs
= get_remote_state ();
548 long size
= get_memory_packet_size (&memory_read_packet_config
);
549 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
550 extra buffer size argument before the memory read size can be
551 increased beyond (rs->remote_packet_size). */
552 if (size
> (rs
->remote_packet_size
))
553 size
= (rs
->remote_packet_size
);
558 /* Generic configuration support for packets the stub optionally
559 supports. Allows the user to specify the use of the packet as well
560 as allowing GDB to auto-detect support in the remote stub. */
564 PACKET_SUPPORT_UNKNOWN
= 0,
573 enum auto_boolean detect
;
574 enum packet_support support
;
577 /* Analyze a packet's return value and update the packet config
588 update_packet_config (struct packet_config
*config
)
590 switch (config
->detect
)
592 case AUTO_BOOLEAN_TRUE
:
593 config
->support
= PACKET_ENABLE
;
595 case AUTO_BOOLEAN_FALSE
:
596 config
->support
= PACKET_DISABLE
;
598 case AUTO_BOOLEAN_AUTO
:
599 config
->support
= PACKET_SUPPORT_UNKNOWN
;
605 show_packet_config_cmd (struct packet_config
*config
)
607 char *support
= "internal-error";
608 switch (config
->support
)
614 support
= "disabled";
616 case PACKET_SUPPORT_UNKNOWN
:
620 switch (config
->detect
)
622 case AUTO_BOOLEAN_AUTO
:
623 printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n",
624 config
->name
, config
->title
, support
);
626 case AUTO_BOOLEAN_TRUE
:
627 case AUTO_BOOLEAN_FALSE
:
628 printf_filtered ("Support for remote protocol `%s' (%s) packet is currently %s.\n",
629 config
->name
, config
->title
, support
);
635 add_packet_config_cmd (struct packet_config
*config
,
638 cmd_sfunc_ftype
*set_func
,
639 cmd_sfunc_ftype
*show_func
,
640 struct cmd_list_element
**set_remote_list
,
641 struct cmd_list_element
**show_remote_list
,
644 struct cmd_list_element
*set_cmd
;
645 struct cmd_list_element
*show_cmd
;
652 config
->title
= title
;
653 config
->detect
= AUTO_BOOLEAN_AUTO
;
654 config
->support
= PACKET_SUPPORT_UNKNOWN
;
655 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
657 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
659 print
= xstrprintf ("Current use of remote protocol `%s' (%s) is %%s",
661 /* set/show TITLE-packet {auto,on,off} */
662 cmd_name
= xstrprintf ("%s-packet", title
);
663 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
664 &config
->detect
, set_doc
, show_doc
,
667 set_remote_list
, show_remote_list
);
668 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
672 legacy_name
= xstrprintf ("%s-packet", name
);
673 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
675 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
680 static enum packet_result
681 packet_ok (const char *buf
, struct packet_config
*config
)
685 /* The stub recognized the packet request. Check that the
686 operation succeeded. */
687 switch (config
->support
)
689 case PACKET_SUPPORT_UNKNOWN
:
691 fprintf_unfiltered (gdb_stdlog
,
692 "Packet %s (%s) is supported\n",
693 config
->name
, config
->title
);
694 config
->support
= PACKET_ENABLE
;
697 internal_error (__FILE__
, __LINE__
,
698 "packet_ok: attempt to use a disabled packet");
703 if (buf
[0] == 'O' && buf
[1] == 'K' && buf
[2] == '\0')
704 /* "OK" - definitly OK. */
707 && isxdigit (buf
[1]) && isxdigit (buf
[2])
709 /* "Enn" - definitly an error. */
711 /* The packet may or may not be OK. Just assume it is. */
716 /* The stub does not support the packet. */
717 switch (config
->support
)
720 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
721 /* If the stub previously indicated that the packet was
722 supported then there is a protocol error.. */
723 error ("Protocol error: %s (%s) conflicting enabled responses.",
724 config
->name
, config
->title
);
726 /* The user set it wrong. */
727 error ("Enabled packet %s (%s) not recognized by stub",
728 config
->name
, config
->title
);
730 case PACKET_SUPPORT_UNKNOWN
:
732 fprintf_unfiltered (gdb_stdlog
,
733 "Packet %s (%s) is NOT supported\n",
734 config
->name
, config
->title
);
735 config
->support
= PACKET_DISABLE
;
740 return PACKET_UNKNOWN
;
744 /* Should we try the 'vCont' (descriptive resume) request? */
745 static struct packet_config remote_protocol_vcont
;
748 set_remote_protocol_vcont_packet_cmd (char *args
, int from_tty
,
749 struct cmd_list_element
*c
)
751 update_packet_config (&remote_protocol_vcont
);
755 show_remote_protocol_vcont_packet_cmd (char *args
, int from_tty
,
756 struct cmd_list_element
*c
)
758 show_packet_config_cmd (&remote_protocol_vcont
);
761 /* Should we try the 'qSymbol' (target symbol lookup service) request? */
762 static struct packet_config remote_protocol_qSymbol
;
765 set_remote_protocol_qSymbol_packet_cmd (char *args
, int from_tty
,
766 struct cmd_list_element
*c
)
768 update_packet_config (&remote_protocol_qSymbol
);
772 show_remote_protocol_qSymbol_packet_cmd (char *args
, int from_tty
,
773 struct cmd_list_element
*c
)
775 show_packet_config_cmd (&remote_protocol_qSymbol
);
778 /* Should we try the 'P' (set register) request? */
780 static struct packet_config remote_protocol_P
;
783 set_remote_protocol_P_packet_cmd (char *args
, int from_tty
,
784 struct cmd_list_element
*c
)
786 update_packet_config (&remote_protocol_P
);
790 show_remote_protocol_P_packet_cmd (char *args
, int from_tty
,
791 struct cmd_list_element
*c
)
793 show_packet_config_cmd (&remote_protocol_P
);
796 /* Should we try one of the 'Z' requests? */
800 Z_PACKET_SOFTWARE_BP
,
801 Z_PACKET_HARDWARE_BP
,
808 static struct packet_config remote_protocol_Z
[NR_Z_PACKET_TYPES
];
810 /* FIXME: Instead of having all these boiler plate functions, the
811 command callback should include a context argument. */
814 set_remote_protocol_Z_software_bp_packet_cmd (char *args
, int from_tty
,
815 struct cmd_list_element
*c
)
817 update_packet_config (&remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
]);
821 show_remote_protocol_Z_software_bp_packet_cmd (char *args
, int from_tty
,
822 struct cmd_list_element
*c
)
824 show_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
]);
828 set_remote_protocol_Z_hardware_bp_packet_cmd (char *args
, int from_tty
,
829 struct cmd_list_element
*c
)
831 update_packet_config (&remote_protocol_Z
[Z_PACKET_HARDWARE_BP
]);
835 show_remote_protocol_Z_hardware_bp_packet_cmd (char *args
, int from_tty
,
836 struct cmd_list_element
*c
)
838 show_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_HARDWARE_BP
]);
842 set_remote_protocol_Z_write_wp_packet_cmd (char *args
, int from_tty
,
843 struct cmd_list_element
*c
)
845 update_packet_config (&remote_protocol_Z
[Z_PACKET_WRITE_WP
]);
849 show_remote_protocol_Z_write_wp_packet_cmd (char *args
, int from_tty
,
850 struct cmd_list_element
*c
)
852 show_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_WRITE_WP
]);
856 set_remote_protocol_Z_read_wp_packet_cmd (char *args
, int from_tty
,
857 struct cmd_list_element
*c
)
859 update_packet_config (&remote_protocol_Z
[Z_PACKET_READ_WP
]);
863 show_remote_protocol_Z_read_wp_packet_cmd (char *args
, int from_tty
,
864 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 (char *args
, int from_tty
,
878 struct cmd_list_element
*c
)
880 show_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_ACCESS_WP
]);
883 /* For compatibility with older distributions. Provide a ``set remote
884 Z-packet ...'' command that updates all the Z packet types. */
886 static enum auto_boolean remote_Z_packet_detect
;
889 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
890 struct cmd_list_element
*c
)
893 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
895 remote_protocol_Z
[i
].detect
= remote_Z_packet_detect
;
896 update_packet_config (&remote_protocol_Z
[i
]);
901 show_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
902 struct cmd_list_element
*c
)
905 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
907 show_packet_config_cmd (&remote_protocol_Z
[i
]);
911 /* Should we try the 'X' (remote binary download) packet?
913 This variable (available to the user via "set remote X-packet")
914 dictates whether downloads are sent in binary (via the 'X' packet).
915 We assume that the stub can, and attempt to do it. This will be
916 cleared if the stub does not understand it. This switch is still
917 needed, though in cases when the packet is supported in the stub,
918 but the connection does not allow it (i.e., 7-bit serial connection
921 static struct packet_config remote_protocol_binary_download
;
923 /* Should we try the 'ThreadInfo' query packet?
925 This variable (NOT available to the user: auto-detect only!)
926 determines whether GDB will use the new, simpler "ThreadInfo"
927 query or the older, more complex syntax for thread queries.
928 This is an auto-detect variable (set to true at each connect,
929 and set to false when the target fails to recognize it). */
931 static int use_threadinfo_query
;
932 static int use_threadextra_query
;
935 set_remote_protocol_binary_download_cmd (char *args
,
937 struct cmd_list_element
*c
)
939 update_packet_config (&remote_protocol_binary_download
);
943 show_remote_protocol_binary_download_cmd (char *args
, int from_tty
,
944 struct cmd_list_element
*c
)
946 show_packet_config_cmd (&remote_protocol_binary_download
);
949 /* Should we try the 'qPart:auxv' (target auxiliary vector read) request? */
950 static struct packet_config remote_protocol_qPart_auxv
;
953 set_remote_protocol_qPart_auxv_packet_cmd (char *args
, int from_tty
,
954 struct cmd_list_element
*c
)
956 update_packet_config (&remote_protocol_qPart_auxv
);
960 show_remote_protocol_qPart_auxv_packet_cmd (char *args
, int from_tty
,
961 struct cmd_list_element
*c
)
963 show_packet_config_cmd (&remote_protocol_qPart_auxv
);
966 static struct packet_config remote_protocol_p
;
969 set_remote_protocol_p_packet_cmd (char *args
, int from_tty
,
970 struct cmd_list_element
*c
)
972 update_packet_config (&remote_protocol_p
);
976 show_remote_protocol_p_packet_cmd (char *args
, int from_tty
,
977 struct cmd_list_element
*c
)
979 show_packet_config_cmd (&remote_protocol_p
);
984 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
985 static void *sigint_remote_twice_token
;
986 static void *sigint_remote_token
;
988 /* These are pointers to hook functions that may be set in order to
989 modify resume/wait behavior for a particular architecture. */
991 void (*deprecated_target_resume_hook
) (void);
992 void (*deprecated_target_wait_loop_hook
) (void);
996 /* These are the threads which we last sent to the remote system.
997 -1 for all or -2 for not sent yet. */
998 static int general_thread
;
999 static int continue_thread
;
1001 /* Call this function as a result of
1002 1) A halt indication (T packet) containing a thread id
1003 2) A direct query of currthread
1004 3) Successful execution of set thread
1008 record_currthread (int currthread
)
1010 general_thread
= currthread
;
1012 /* If this is a new thread, add it to GDB's thread list.
1013 If we leave it up to WFI to do this, bad things will happen. */
1014 if (!in_thread_list (pid_to_ptid (currthread
)))
1016 add_thread (pid_to_ptid (currthread
));
1017 ui_out_text (uiout
, "[New ");
1018 ui_out_text (uiout
, target_pid_to_str (pid_to_ptid (currthread
)));
1019 ui_out_text (uiout
, "]\n");
1023 #define MAGIC_NULL_PID 42000
1026 set_thread (int th
, int gen
)
1028 struct remote_state
*rs
= get_remote_state ();
1029 char *buf
= alloca (rs
->remote_packet_size
);
1030 int state
= gen
? general_thread
: continue_thread
;
1036 buf
[1] = gen
? 'g' : 'c';
1037 if (th
== MAGIC_NULL_PID
)
1043 sprintf (&buf
[2], "-%x", -th
);
1045 sprintf (&buf
[2], "%x", th
);
1047 getpkt (buf
, (rs
->remote_packet_size
), 0);
1049 general_thread
= th
;
1051 continue_thread
= th
;
1054 /* Return nonzero if the thread TH is still alive on the remote system. */
1057 remote_thread_alive (ptid_t ptid
)
1059 int tid
= PIDGET (ptid
);
1063 sprintf (buf
, "T-%08x", -tid
);
1065 sprintf (buf
, "T%08x", tid
);
1067 getpkt (buf
, sizeof (buf
), 0);
1068 return (buf
[0] == 'O' && buf
[1] == 'K');
1071 /* About these extended threadlist and threadinfo packets. They are
1072 variable length packets but, the fields within them are often fixed
1073 length. They are redundent enough to send over UDP as is the
1074 remote protocol in general. There is a matching unit test module
1077 #define OPAQUETHREADBYTES 8
1079 /* a 64 bit opaque identifier */
1080 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1082 /* WARNING: This threadref data structure comes from the remote O.S.,
1083 libstub protocol encoding, and remote.c. it is not particularly
1086 /* Right now, the internal structure is int. We want it to be bigger.
1090 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1092 /* gdb_ext_thread_info is an internal GDB data structure which is
1093 equivalint to the reply of the remote threadinfo packet. */
1095 struct gdb_ext_thread_info
1097 threadref threadid
; /* External form of thread reference. */
1098 int active
; /* Has state interesting to GDB?
1100 char display
[256]; /* Brief state display, name,
1101 blocked/syspended. */
1102 char shortname
[32]; /* To be used to name threads. */
1103 char more_display
[256]; /* Long info, statistics, queue depth,
1107 /* The volume of remote transfers can be limited by submitting
1108 a mask containing bits specifying the desired information.
1109 Use a union of these values as the 'selection' parameter to
1110 get_thread_info. FIXME: Make these TAG names more thread specific.
1113 #define TAG_THREADID 1
1114 #define TAG_EXISTS 2
1115 #define TAG_DISPLAY 4
1116 #define TAG_THREADNAME 8
1117 #define TAG_MOREDISPLAY 16
1119 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1121 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1123 static char *unpack_nibble (char *buf
, int *val
);
1125 static char *pack_nibble (char *buf
, int nibble
);
1127 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1129 static char *unpack_byte (char *buf
, int *value
);
1131 static char *pack_int (char *buf
, int value
);
1133 static char *unpack_int (char *buf
, int *value
);
1135 static char *unpack_string (char *src
, char *dest
, int length
);
1137 static char *pack_threadid (char *pkt
, threadref
*id
);
1139 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1141 void int_to_threadref (threadref
*id
, int value
);
1143 static int threadref_to_int (threadref
*ref
);
1145 static void copy_threadref (threadref
*dest
, threadref
*src
);
1147 static int threadmatch (threadref
*dest
, threadref
*src
);
1149 static char *pack_threadinfo_request (char *pkt
, int mode
,
1152 static int remote_unpack_thread_info_response (char *pkt
,
1153 threadref
*expectedref
,
1154 struct gdb_ext_thread_info
1158 static int remote_get_threadinfo (threadref
*threadid
,
1159 int fieldset
, /*TAG mask */
1160 struct gdb_ext_thread_info
*info
);
1162 static char *pack_threadlist_request (char *pkt
, int startflag
,
1164 threadref
*nextthread
);
1166 static int parse_threadlist_response (char *pkt
,
1168 threadref
*original_echo
,
1169 threadref
*resultlist
,
1172 static int remote_get_threadlist (int startflag
,
1173 threadref
*nextthread
,
1177 threadref
*threadlist
);
1179 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1181 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1182 void *context
, int looplimit
);
1184 static int remote_newthread_step (threadref
*ref
, void *context
);
1186 /* Encode 64 bits in 16 chars of hex. */
1188 static const char hexchars
[] = "0123456789abcdef";
1191 ishex (int ch
, int *val
)
1193 if ((ch
>= 'a') && (ch
<= 'f'))
1195 *val
= ch
- 'a' + 10;
1198 if ((ch
>= 'A') && (ch
<= 'F'))
1200 *val
= ch
- 'A' + 10;
1203 if ((ch
>= '0') && (ch
<= '9'))
1214 if (ch
>= 'a' && ch
<= 'f')
1215 return ch
- 'a' + 10;
1216 if (ch
>= '0' && ch
<= '9')
1218 if (ch
>= 'A' && ch
<= 'F')
1219 return ch
- 'A' + 10;
1224 stub_unpack_int (char *buff
, int fieldlength
)
1231 nibble
= stubhex (*buff
++);
1235 retval
= retval
<< 4;
1241 unpack_varlen_hex (char *buff
, /* packet to parse */
1247 while (ishex (*buff
, &nibble
))
1250 retval
= retval
<< 4;
1251 retval
|= nibble
& 0x0f;
1258 unpack_nibble (char *buf
, int *val
)
1260 ishex (*buf
++, val
);
1265 pack_nibble (char *buf
, int nibble
)
1267 *buf
++ = hexchars
[(nibble
& 0x0f)];
1272 pack_hex_byte (char *pkt
, int byte
)
1274 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1275 *pkt
++ = hexchars
[(byte
& 0xf)];
1280 unpack_byte (char *buf
, int *value
)
1282 *value
= stub_unpack_int (buf
, 2);
1287 pack_int (char *buf
, int value
)
1289 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1290 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1291 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1292 buf
= pack_hex_byte (buf
, (value
& 0xff));
1297 unpack_int (char *buf
, int *value
)
1299 *value
= stub_unpack_int (buf
, 8);
1303 #if 0 /* Currently unused, uncomment when needed. */
1304 static char *pack_string (char *pkt
, char *string
);
1307 pack_string (char *pkt
, char *string
)
1312 len
= strlen (string
);
1314 len
= 200; /* Bigger than most GDB packets, junk??? */
1315 pkt
= pack_hex_byte (pkt
, len
);
1319 if ((ch
== '\0') || (ch
== '#'))
1320 ch
= '*'; /* Protect encapsulation. */
1325 #endif /* 0 (unused) */
1328 unpack_string (char *src
, char *dest
, int length
)
1337 pack_threadid (char *pkt
, threadref
*id
)
1340 unsigned char *altid
;
1342 altid
= (unsigned char *) id
;
1343 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1345 pkt
= pack_hex_byte (pkt
, *altid
++);
1351 unpack_threadid (char *inbuf
, threadref
*id
)
1354 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1357 altref
= (char *) id
;
1359 while (inbuf
< limit
)
1361 x
= stubhex (*inbuf
++);
1362 y
= stubhex (*inbuf
++);
1363 *altref
++ = (x
<< 4) | y
;
1368 /* Externally, threadrefs are 64 bits but internally, they are still
1369 ints. This is due to a mismatch of specifications. We would like
1370 to use 64bit thread references internally. This is an adapter
1374 int_to_threadref (threadref
*id
, int value
)
1376 unsigned char *scan
;
1378 scan
= (unsigned char *) id
;
1384 *scan
++ = (value
>> 24) & 0xff;
1385 *scan
++ = (value
>> 16) & 0xff;
1386 *scan
++ = (value
>> 8) & 0xff;
1387 *scan
++ = (value
& 0xff);
1391 threadref_to_int (threadref
*ref
)
1394 unsigned char *scan
;
1396 scan
= (char *) ref
;
1400 value
= (value
<< 8) | ((*scan
++) & 0xff);
1405 copy_threadref (threadref
*dest
, threadref
*src
)
1408 unsigned char *csrc
, *cdest
;
1410 csrc
= (unsigned char *) src
;
1411 cdest
= (unsigned char *) dest
;
1418 threadmatch (threadref
*dest
, threadref
*src
)
1420 /* Things are broken right now, so just assume we got a match. */
1422 unsigned char *srcp
, *destp
;
1424 srcp
= (char *) src
;
1425 destp
= (char *) dest
;
1429 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1436 threadid:1, # always request threadid
1443 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1446 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1448 *pkt
++ = 'q'; /* Info Query */
1449 *pkt
++ = 'P'; /* process or thread info */
1450 pkt
= pack_int (pkt
, mode
); /* mode */
1451 pkt
= pack_threadid (pkt
, id
); /* threadid */
1452 *pkt
= '\0'; /* terminate */
1456 /* These values tag the fields in a thread info response packet. */
1457 /* Tagging the fields allows us to request specific fields and to
1458 add more fields as time goes by. */
1460 #define TAG_THREADID 1 /* Echo the thread identifier. */
1461 #define TAG_EXISTS 2 /* Is this process defined enough to
1462 fetch registers and its stack? */
1463 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1464 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1465 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1469 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1470 struct gdb_ext_thread_info
*info
)
1472 struct remote_state
*rs
= get_remote_state ();
1476 char *limit
= pkt
+ (rs
->remote_packet_size
); /* plausable parsing limit */
1479 /* info->threadid = 0; FIXME: implement zero_threadref. */
1481 info
->display
[0] = '\0';
1482 info
->shortname
[0] = '\0';
1483 info
->more_display
[0] = '\0';
1485 /* Assume the characters indicating the packet type have been
1487 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1488 pkt
= unpack_threadid (pkt
, &ref
);
1491 warning ("Incomplete response to threadinfo request\n");
1492 if (!threadmatch (&ref
, expectedref
))
1493 { /* This is an answer to a different request. */
1494 warning ("ERROR RMT Thread info mismatch\n");
1497 copy_threadref (&info
->threadid
, &ref
);
1499 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1501 /* Packets are terminated with nulls. */
1502 while ((pkt
< limit
) && mask
&& *pkt
)
1504 pkt
= unpack_int (pkt
, &tag
); /* tag */
1505 pkt
= unpack_byte (pkt
, &length
); /* length */
1506 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1508 warning ("ERROR RMT: threadinfo tag mismatch\n");
1512 if (tag
== TAG_THREADID
)
1516 warning ("ERROR RMT: length of threadid is not 16\n");
1520 pkt
= unpack_threadid (pkt
, &ref
);
1521 mask
= mask
& ~TAG_THREADID
;
1524 if (tag
== TAG_EXISTS
)
1526 info
->active
= stub_unpack_int (pkt
, length
);
1528 mask
= mask
& ~(TAG_EXISTS
);
1531 warning ("ERROR RMT: 'exists' length too long\n");
1537 if (tag
== TAG_THREADNAME
)
1539 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1540 mask
= mask
& ~TAG_THREADNAME
;
1543 if (tag
== TAG_DISPLAY
)
1545 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1546 mask
= mask
& ~TAG_DISPLAY
;
1549 if (tag
== TAG_MOREDISPLAY
)
1551 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1552 mask
= mask
& ~TAG_MOREDISPLAY
;
1555 warning ("ERROR RMT: unknown thread info tag\n");
1556 break; /* Not a tag we know about. */
1562 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1563 struct gdb_ext_thread_info
*info
)
1565 struct remote_state
*rs
= get_remote_state ();
1567 char *threadinfo_pkt
= alloca (rs
->remote_packet_size
);
1569 pack_threadinfo_request (threadinfo_pkt
, fieldset
, threadid
);
1570 putpkt (threadinfo_pkt
);
1571 getpkt (threadinfo_pkt
, (rs
->remote_packet_size
), 0);
1572 result
= remote_unpack_thread_info_response (threadinfo_pkt
+ 2,
1577 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1580 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1581 threadref
*nextthread
)
1583 *pkt
++ = 'q'; /* info query packet */
1584 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1585 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1586 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1587 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1592 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1595 parse_threadlist_response (char *pkt
, int result_limit
,
1596 threadref
*original_echo
, threadref
*resultlist
,
1599 struct remote_state
*rs
= get_remote_state ();
1601 int count
, resultcount
, done
;
1604 /* Assume the 'q' and 'M chars have been stripped. */
1605 limit
= pkt
+ ((rs
->remote_packet_size
) - BUF_THREAD_ID_SIZE
);
1606 /* done parse past here */
1607 pkt
= unpack_byte (pkt
, &count
); /* count field */
1608 pkt
= unpack_nibble (pkt
, &done
);
1609 /* The first threadid is the argument threadid. */
1610 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1611 while ((count
-- > 0) && (pkt
< limit
))
1613 pkt
= unpack_threadid (pkt
, resultlist
++);
1614 if (resultcount
++ >= result_limit
)
1623 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1624 int *done
, int *result_count
, threadref
*threadlist
)
1626 struct remote_state
*rs
= get_remote_state ();
1627 static threadref echo_nextthread
;
1628 char *threadlist_packet
= alloca (rs
->remote_packet_size
);
1629 char *t_response
= alloca (rs
->remote_packet_size
);
1632 /* Trancate result limit to be smaller than the packet size. */
1633 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= (rs
->remote_packet_size
))
1634 result_limit
= ((rs
->remote_packet_size
) / BUF_THREAD_ID_SIZE
) - 2;
1636 pack_threadlist_request (threadlist_packet
,
1637 startflag
, result_limit
, nextthread
);
1638 putpkt (threadlist_packet
);
1639 getpkt (t_response
, (rs
->remote_packet_size
), 0);
1642 parse_threadlist_response (t_response
+ 2, result_limit
, &echo_nextthread
,
1645 if (!threadmatch (&echo_nextthread
, nextthread
))
1647 /* FIXME: This is a good reason to drop the packet. */
1648 /* Possably, there is a duplicate response. */
1650 retransmit immediatly - race conditions
1651 retransmit after timeout - yes
1653 wait for packet, then exit
1655 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1656 return 0; /* I choose simply exiting. */
1658 if (*result_count
<= 0)
1662 warning ("RMT ERROR : failed to get remote thread list\n");
1665 return result
; /* break; */
1667 if (*result_count
> result_limit
)
1670 warning ("RMT ERROR: threadlist response longer than requested\n");
1676 /* This is the interface between remote and threads, remotes upper
1679 /* remote_find_new_threads retrieves the thread list and for each
1680 thread in the list, looks up the thread in GDB's internal list,
1681 ading the thread if it does not already exist. This involves
1682 getting partial thread lists from the remote target so, polling the
1683 quit_flag is required. */
1686 /* About this many threadisds fit in a packet. */
1688 #define MAXTHREADLISTRESULTS 32
1691 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
1694 int done
, i
, result_count
;
1698 static threadref nextthread
;
1699 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1704 if (loopcount
++ > looplimit
)
1707 warning ("Remote fetch threadlist -infinite loop-\n");
1710 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1711 &done
, &result_count
, resultthreadlist
))
1716 /* Clear for later iterations. */
1718 /* Setup to resume next batch of thread references, set nextthread. */
1719 if (result_count
>= 1)
1720 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1722 while (result_count
--)
1723 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1730 remote_newthread_step (threadref
*ref
, void *context
)
1734 ptid
= pid_to_ptid (threadref_to_int (ref
));
1736 if (!in_thread_list (ptid
))
1738 return 1; /* continue iterator */
1741 #define CRAZY_MAX_THREADS 1000
1744 remote_current_thread (ptid_t oldpid
)
1746 struct remote_state
*rs
= get_remote_state ();
1747 char *buf
= alloca (rs
->remote_packet_size
);
1750 getpkt (buf
, (rs
->remote_packet_size
), 0);
1751 if (buf
[0] == 'Q' && buf
[1] == 'C')
1752 /* Use strtoul here, so we'll correctly parse values whose highest
1753 bit is set. The protocol carries them as a simple series of
1754 hex digits; in the absence of a sign, strtol will see such
1755 values as positive numbers out of range for signed 'long', and
1756 return LONG_MAX to indicate an overflow. */
1757 return pid_to_ptid (strtoul (&buf
[2], NULL
, 16));
1762 /* Find new threads for info threads command.
1763 * Original version, using John Metzler's thread protocol.
1767 remote_find_new_threads (void)
1769 remote_threadlist_iterator (remote_newthread_step
, 0,
1771 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
) /* ack ack ack */
1772 inferior_ptid
= remote_current_thread (inferior_ptid
);
1776 * Find all threads for info threads command.
1777 * Uses new thread protocol contributed by Cisco.
1778 * Falls back and attempts to use the older method (above)
1779 * if the target doesn't respond to the new method.
1783 remote_threads_info (void)
1785 struct remote_state
*rs
= get_remote_state ();
1786 char *buf
= alloca (rs
->remote_packet_size
);
1790 if (remote_desc
== 0) /* paranoia */
1791 error ("Command can only be used when connected to the remote target.");
1793 if (use_threadinfo_query
)
1795 putpkt ("qfThreadInfo");
1797 getpkt (bufp
, (rs
->remote_packet_size
), 0);
1798 if (bufp
[0] != '\0') /* q packet recognized */
1800 while (*bufp
++ == 'm') /* reply contains one or more TID */
1804 /* Use strtoul here, so we'll correctly parse values
1805 whose highest bit is set. The protocol carries
1806 them as a simple series of hex digits; in the
1807 absence of a sign, strtol will see such values as
1808 positive numbers out of range for signed 'long',
1809 and return LONG_MAX to indicate an overflow. */
1810 tid
= strtoul (bufp
, &bufp
, 16);
1811 if (tid
!= 0 && !in_thread_list (pid_to_ptid (tid
)))
1812 add_thread (pid_to_ptid (tid
));
1814 while (*bufp
++ == ','); /* comma-separated list */
1815 putpkt ("qsThreadInfo");
1817 getpkt (bufp
, (rs
->remote_packet_size
), 0);
1823 /* Else fall back to old method based on jmetzler protocol. */
1824 use_threadinfo_query
= 0;
1825 remote_find_new_threads ();
1830 * Collect a descriptive string about the given thread.
1831 * The target may say anything it wants to about the thread
1832 * (typically info about its blocked / runnable state, name, etc.).
1833 * This string will appear in the info threads display.
1835 * Optional: targets are not required to implement this function.
1839 remote_threads_extra_info (struct thread_info
*tp
)
1841 struct remote_state
*rs
= get_remote_state ();
1845 struct gdb_ext_thread_info threadinfo
;
1846 static char display_buf
[100]; /* arbitrary... */
1847 char *bufp
= alloca (rs
->remote_packet_size
);
1848 int n
= 0; /* position in display_buf */
1850 if (remote_desc
== 0) /* paranoia */
1851 internal_error (__FILE__
, __LINE__
,
1852 "remote_threads_extra_info");
1854 if (use_threadextra_query
)
1856 sprintf (bufp
, "qThreadExtraInfo,%x", PIDGET (tp
->ptid
));
1858 getpkt (bufp
, (rs
->remote_packet_size
), 0);
1861 n
= min (strlen (bufp
) / 2, sizeof (display_buf
));
1862 result
= hex2bin (bufp
, display_buf
, n
);
1863 display_buf
[result
] = '\0';
1868 /* If the above query fails, fall back to the old method. */
1869 use_threadextra_query
= 0;
1870 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
1871 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
1872 int_to_threadref (&id
, PIDGET (tp
->ptid
));
1873 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
1874 if (threadinfo
.active
)
1876 if (*threadinfo
.shortname
)
1877 n
+= sprintf(&display_buf
[0], " Name: %s,", threadinfo
.shortname
);
1878 if (*threadinfo
.display
)
1879 n
+= sprintf(&display_buf
[n
], " State: %s,", threadinfo
.display
);
1880 if (*threadinfo
.more_display
)
1881 n
+= sprintf(&display_buf
[n
], " Priority: %s",
1882 threadinfo
.more_display
);
1886 /* For purely cosmetic reasons, clear up trailing commas. */
1887 if (',' == display_buf
[n
-1])
1888 display_buf
[n
-1] = ' ';
1897 /* Restart the remote side; this is an extended protocol operation. */
1900 extended_remote_restart (void)
1902 struct remote_state
*rs
= get_remote_state ();
1903 char *buf
= alloca (rs
->remote_packet_size
);
1905 /* Send the restart command; for reasons I don't understand the
1906 remote side really expects a number after the "R". */
1908 sprintf (&buf
[1], "%x", 0);
1911 /* Now query for status so this looks just like we restarted
1912 gdbserver from scratch. */
1914 getpkt (buf
, (rs
->remote_packet_size
), 0);
1917 /* Clean up connection to a remote debugger. */
1920 remote_close (int quitting
)
1923 serial_close (remote_desc
);
1927 /* Query the remote side for the text, data and bss offsets. */
1932 struct remote_state
*rs
= get_remote_state ();
1933 char *buf
= alloca (rs
->remote_packet_size
);
1936 CORE_ADDR text_addr
, data_addr
, bss_addr
;
1937 struct section_offsets
*offs
;
1939 putpkt ("qOffsets");
1941 getpkt (buf
, (rs
->remote_packet_size
), 0);
1943 if (buf
[0] == '\000')
1944 return; /* Return silently. Stub doesn't support
1948 warning ("Remote failure reply: %s", buf
);
1952 /* Pick up each field in turn. This used to be done with scanf, but
1953 scanf will make trouble if CORE_ADDR size doesn't match
1954 conversion directives correctly. The following code will work
1955 with any size of CORE_ADDR. */
1956 text_addr
= data_addr
= bss_addr
= 0;
1960 if (strncmp (ptr
, "Text=", 5) == 0)
1963 /* Don't use strtol, could lose on big values. */
1964 while (*ptr
&& *ptr
!= ';')
1965 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
1970 if (!lose
&& strncmp (ptr
, ";Data=", 6) == 0)
1973 while (*ptr
&& *ptr
!= ';')
1974 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
1979 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
1982 while (*ptr
&& *ptr
!= ';')
1983 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
1989 error ("Malformed response to offset query, %s", buf
);
1991 if (symfile_objfile
== NULL
)
1994 offs
= ((struct section_offsets
*)
1995 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
1996 memcpy (offs
, symfile_objfile
->section_offsets
,
1997 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
1999 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
2001 /* This is a temporary kludge to force data and bss to use the same offsets
2002 because that's what nlmconv does now. The real solution requires changes
2003 to the stub and remote.c that I don't have time to do right now. */
2005 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
2006 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
2008 objfile_relocate (symfile_objfile
, offs
);
2011 /* Stub for catch_errors. */
2014 remote_start_remote_dummy (struct ui_out
*uiout
, void *dummy
)
2016 start_remote (); /* Initialize gdb process mechanisms. */
2017 /* NOTE: Return something >=0. A -ve value is reserved for
2018 catch_exceptions. */
2023 remote_start_remote (struct ui_out
*uiout
, void *dummy
)
2025 immediate_quit
++; /* Allow user to interrupt it. */
2027 /* Ack any packet which the remote side has already sent. */
2028 serial_write (remote_desc
, "+", 1);
2030 /* Let the stub know that we want it to return the thread. */
2033 inferior_ptid
= remote_current_thread (inferior_ptid
);
2035 get_offsets (); /* Get text, data & bss offsets. */
2037 putpkt ("?"); /* Initiate a query from remote machine. */
2040 /* NOTE: See comment above in remote_start_remote_dummy(). This
2041 function returns something >=0. */
2042 return remote_start_remote_dummy (uiout
, dummy
);
2045 /* Open a connection to a remote debugger.
2046 NAME is the filename used for communication. */
2049 remote_open (char *name
, int from_tty
)
2051 remote_open_1 (name
, from_tty
, &remote_ops
, 0, 0);
2054 /* Just like remote_open, but with asynchronous support. */
2056 remote_async_open (char *name
, int from_tty
)
2058 remote_open_1 (name
, from_tty
, &remote_async_ops
, 0, 1);
2061 /* Open a connection to a remote debugger using the extended
2062 remote gdb protocol. NAME is the filename used for communication. */
2065 extended_remote_open (char *name
, int from_tty
)
2067 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */,
2071 /* Just like extended_remote_open, but with asynchronous support. */
2073 extended_remote_async_open (char *name
, int from_tty
)
2075 remote_open_1 (name
, from_tty
, &extended_async_remote_ops
,
2076 1 /*extended_p */, 1 /* async_p */);
2079 /* Generic code for opening a connection to a remote target. */
2082 init_all_packet_configs (void)
2085 update_packet_config (&remote_protocol_P
);
2086 update_packet_config (&remote_protocol_p
);
2087 update_packet_config (&remote_protocol_qSymbol
);
2088 update_packet_config (&remote_protocol_vcont
);
2089 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
2090 update_packet_config (&remote_protocol_Z
[i
]);
2091 /* Force remote_write_bytes to check whether target supports binary
2093 update_packet_config (&remote_protocol_binary_download
);
2094 update_packet_config (&remote_protocol_qPart_auxv
);
2097 /* Symbol look-up. */
2100 remote_check_symbols (struct objfile
*objfile
)
2102 struct remote_state
*rs
= get_remote_state ();
2103 char *msg
, *reply
, *tmp
;
2104 struct minimal_symbol
*sym
;
2107 if (remote_protocol_qSymbol
.support
== PACKET_DISABLE
)
2110 msg
= alloca (rs
->remote_packet_size
);
2111 reply
= alloca (rs
->remote_packet_size
);
2113 /* Invite target to request symbol lookups. */
2115 putpkt ("qSymbol::");
2116 getpkt (reply
, (rs
->remote_packet_size
), 0);
2117 packet_ok (reply
, &remote_protocol_qSymbol
);
2119 while (strncmp (reply
, "qSymbol:", 8) == 0)
2122 end
= hex2bin (tmp
, msg
, strlen (tmp
) / 2);
2124 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
2126 sprintf (msg
, "qSymbol::%s", &reply
[8]);
2128 sprintf (msg
, "qSymbol:%s:%s",
2129 paddr_nz (SYMBOL_VALUE_ADDRESS (sym
)),
2132 getpkt (reply
, (rs
->remote_packet_size
), 0);
2136 static struct serial
*
2137 remote_serial_open (char *name
)
2139 static int udp_warning
= 0;
2141 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2142 of in ser-tcp.c, because it is the remote protocol assuming that the
2143 serial connection is reliable and not the serial connection promising
2145 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
2147 warning ("The remote protocol may be unreliable over UDP.");
2148 warning ("Some events may be lost, rendering further debugging "
2153 return serial_open (name
);
2157 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
,
2158 int extended_p
, int async_p
)
2161 struct remote_state
*rs
= get_remote_state ();
2163 error ("To open a remote debug connection, you need to specify what\n"
2164 "serial device is attached to the remote system\n"
2165 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
2167 /* See FIXME above. */
2169 wait_forever_enabled_p
= 1;
2171 reopen_exec_file ();
2174 target_preopen (from_tty
);
2176 unpush_target (target
);
2178 remote_desc
= remote_serial_open (name
);
2180 perror_with_name (name
);
2182 if (baud_rate
!= -1)
2184 if (serial_setbaudrate (remote_desc
, baud_rate
))
2186 /* The requested speed could not be set. Error out to
2187 top level after closing remote_desc. Take care to
2188 set remote_desc to NULL to avoid closing remote_desc
2190 serial_close (remote_desc
);
2192 perror_with_name (name
);
2196 serial_raw (remote_desc
);
2198 /* If there is something sitting in the buffer we might take it as a
2199 response to a command, which would be bad. */
2200 serial_flush_input (remote_desc
);
2204 puts_filtered ("Remote debugging using ");
2205 puts_filtered (name
);
2206 puts_filtered ("\n");
2208 push_target (target
); /* Switch to using remote target now. */
2210 init_all_packet_configs ();
2212 general_thread
= -2;
2213 continue_thread
= -2;
2215 /* Probe for ability to use "ThreadInfo" query, as required. */
2216 use_threadinfo_query
= 1;
2217 use_threadextra_query
= 1;
2219 /* Without this, some commands which require an active target (such
2220 as kill) won't work. This variable serves (at least) double duty
2221 as both the pid of the target process (if it has such), and as a
2222 flag indicating that a target is active. These functions should
2223 be split out into seperate variables, especially since GDB will
2224 someday have a notion of debugging several processes. */
2226 inferior_ptid
= pid_to_ptid (MAGIC_NULL_PID
);
2230 /* With this target we start out by owning the terminal. */
2231 remote_async_terminal_ours_p
= 1;
2233 /* FIXME: cagney/1999-09-23: During the initial connection it is
2234 assumed that the target is already ready and able to respond to
2235 requests. Unfortunately remote_start_remote() eventually calls
2236 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2237 around this. Eventually a mechanism that allows
2238 wait_for_inferior() to expect/get timeouts will be
2240 wait_forever_enabled_p
= 0;
2243 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2244 /* First delete any symbols previously loaded from shared libraries. */
2245 no_shared_libraries (NULL
, 0);
2248 /* Start the remote connection. If error() or QUIT, discard this
2249 target (we'd otherwise be in an inconsistent state) and then
2250 propogate the error on up the exception chain. This ensures that
2251 the caller doesn't stumble along blindly assuming that the
2252 function succeeded. The CLI doesn't have this problem but other
2253 UI's, such as MI do.
2255 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2256 this function should return an error indication letting the
2257 caller restore the previous state. Unfortunately the command
2258 ``target remote'' is directly wired to this function making that
2259 impossible. On a positive note, the CLI side of this problem has
2260 been fixed - the function set_cmd_context() makes it possible for
2261 all the ``target ....'' commands to share a common callback
2262 function. See cli-dump.c. */
2263 ex
= catch_exceptions (uiout
,
2264 remote_start_remote
, NULL
,
2265 "Couldn't establish connection to remote"
2272 wait_forever_enabled_p
= 1;
2277 wait_forever_enabled_p
= 1;
2281 /* Tell the remote that we are using the extended protocol. */
2282 char *buf
= alloca (rs
->remote_packet_size
);
2284 getpkt (buf
, (rs
->remote_packet_size
), 0);
2286 #ifdef SOLIB_CREATE_INFERIOR_HOOK
2287 /* FIXME: need a master target_open vector from which all
2288 remote_opens can be called, so that stuff like this can
2289 go there. Failing that, the following code must be copied
2290 to the open function for any remote target that wants to
2291 support svr4 shared libraries. */
2293 /* Set up to detect and load shared libraries. */
2294 if (exec_bfd
) /* No use without an exec file. */
2296 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid
));
2297 remote_check_symbols (symfile_objfile
);
2302 /* This takes a program previously attached to and detaches it. After
2303 this is done, GDB can be used to debug some other program. We
2304 better not have left any breakpoints in the target program or it'll
2305 die when it hits one. */
2308 remote_detach (char *args
, int from_tty
)
2310 struct remote_state
*rs
= get_remote_state ();
2311 char *buf
= alloca (rs
->remote_packet_size
);
2314 error ("Argument given to \"detach\" when remotely debugging.");
2316 /* Tell the remote target to detach. */
2318 remote_send (buf
, (rs
->remote_packet_size
));
2320 /* Unregister the file descriptor from the event loop. */
2321 if (target_is_async_p ())
2322 serial_async (remote_desc
, NULL
, 0);
2324 target_mourn_inferior ();
2326 puts_filtered ("Ending remote debugging.\n");
2329 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2332 remote_disconnect (char *args
, int from_tty
)
2334 struct remote_state
*rs
= get_remote_state ();
2335 char *buf
= alloca (rs
->remote_packet_size
);
2338 error ("Argument given to \"detach\" when remotely debugging.");
2340 /* Unregister the file descriptor from the event loop. */
2341 if (target_is_async_p ())
2342 serial_async (remote_desc
, NULL
, 0);
2344 target_mourn_inferior ();
2346 puts_filtered ("Ending remote debugging.\n");
2349 /* Convert hex digit A to a number. */
2354 if (a
>= '0' && a
<= '9')
2356 else if (a
>= 'a' && a
<= 'f')
2357 return a
- 'a' + 10;
2358 else if (a
>= 'A' && a
<= 'F')
2359 return a
- 'A' + 10;
2361 error ("Reply contains invalid hex digit %d", a
);
2365 hex2bin (const char *hex
, char *bin
, int count
)
2369 for (i
= 0; i
< count
; i
++)
2371 if (hex
[0] == 0 || hex
[1] == 0)
2373 /* Hex string is short, or of uneven length.
2374 Return the count that has been converted so far. */
2377 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
2383 /* Convert number NIB to a hex digit. */
2391 return 'a' + nib
- 10;
2395 bin2hex (const char *bin
, char *hex
, int count
)
2398 /* May use a length, or a nul-terminated string as input. */
2400 count
= strlen (bin
);
2402 for (i
= 0; i
< count
; i
++)
2404 *hex
++ = tohex ((*bin
>> 4) & 0xf);
2405 *hex
++ = tohex (*bin
++ & 0xf);
2411 /* Check for the availability of vCont. This function should also check
2415 remote_vcont_probe (struct remote_state
*rs
, char *buf
)
2417 strcpy (buf
, "vCont?");
2419 getpkt (buf
, rs
->remote_packet_size
, 0);
2421 /* Make sure that the features we assume are supported. */
2422 if (strncmp (buf
, "vCont", 5) == 0)
2425 int support_s
, support_S
, support_c
, support_C
;
2431 while (p
&& *p
== ';')
2434 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2436 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2438 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2440 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2443 p
= strchr (p
, ';');
2446 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2447 BUF will make packet_ok disable the packet. */
2448 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
2452 packet_ok (buf
, &remote_protocol_vcont
);
2455 /* Resume the remote inferior by using a "vCont" packet. The thread
2456 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2457 resumed thread should be single-stepped and/or signalled. If PTID's
2458 PID is -1, then all threads are resumed; the thread to be stepped and/or
2459 signalled is given in the global INFERIOR_PTID. This function returns
2460 non-zero iff it resumes the inferior.
2462 This function issues a strict subset of all possible vCont commands at the
2466 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2468 struct remote_state
*rs
= get_remote_state ();
2469 int pid
= PIDGET (ptid
);
2470 char *buf
= NULL
, *outbuf
;
2471 struct cleanup
*old_cleanup
;
2473 buf
= xmalloc (rs
->remote_packet_size
);
2474 old_cleanup
= make_cleanup (xfree
, buf
);
2476 if (remote_protocol_vcont
.support
== PACKET_SUPPORT_UNKNOWN
)
2477 remote_vcont_probe (rs
, buf
);
2479 if (remote_protocol_vcont
.support
== PACKET_DISABLE
)
2481 do_cleanups (old_cleanup
);
2485 /* If we could generate a wider range of packets, we'd have to worry
2486 about overflowing BUF. Should there be a generic
2487 "multi-part-packet" packet? */
2489 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
)
2491 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2492 don't have any PID numbers the inferior will understand. Make sure
2493 to only send forms that do not specify a PID. */
2494 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2495 outbuf
= xstrprintf ("vCont;S%02x", siggnal
);
2497 outbuf
= xstrprintf ("vCont;s");
2498 else if (siggnal
!= TARGET_SIGNAL_0
)
2499 outbuf
= xstrprintf ("vCont;C%02x", siggnal
);
2501 outbuf
= xstrprintf ("vCont;c");
2505 /* Resume all threads, with preference for INFERIOR_PTID. */
2506 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2507 outbuf
= xstrprintf ("vCont;S%02x:%x;c", siggnal
,
2508 PIDGET (inferior_ptid
));
2510 outbuf
= xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid
));
2511 else if (siggnal
!= TARGET_SIGNAL_0
)
2512 outbuf
= xstrprintf ("vCont;C%02x:%x;c", siggnal
,
2513 PIDGET (inferior_ptid
));
2515 outbuf
= xstrprintf ("vCont;c");
2519 /* Scheduler locking; resume only PTID. */
2520 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2521 outbuf
= xstrprintf ("vCont;S%02x:%x", siggnal
, pid
);
2523 outbuf
= xstrprintf ("vCont;s:%x", pid
);
2524 else if (siggnal
!= TARGET_SIGNAL_0
)
2525 outbuf
= xstrprintf ("vCont;C%02x:%x", siggnal
, pid
);
2527 outbuf
= xstrprintf ("vCont;c:%x", pid
);
2530 gdb_assert (outbuf
&& strlen (outbuf
) < rs
->remote_packet_size
);
2531 make_cleanup (xfree
, outbuf
);
2535 do_cleanups (old_cleanup
);
2540 /* Tell the remote machine to resume. */
2542 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
2544 static int last_sent_step
;
2547 remote_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2549 struct remote_state
*rs
= get_remote_state ();
2550 char *buf
= alloca (rs
->remote_packet_size
);
2551 int pid
= PIDGET (ptid
);
2554 last_sent_signal
= siggnal
;
2555 last_sent_step
= step
;
2557 /* A hook for when we need to do something at the last moment before
2559 if (deprecated_target_resume_hook
)
2560 (*deprecated_target_resume_hook
) ();
2562 /* The vCont packet doesn't need to specify threads via Hc. */
2563 if (remote_vcont_resume (ptid
, step
, siggnal
))
2566 /* All other supported resume packets do use Hc, so call set_thread. */
2568 set_thread (0, 0); /* Run any thread. */
2570 set_thread (pid
, 0); /* Run this thread. */
2572 if (siggnal
!= TARGET_SIGNAL_0
)
2574 buf
[0] = step
? 'S' : 'C';
2575 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2576 buf
[2] = tohex (((int) siggnal
) & 0xf);
2580 strcpy (buf
, step
? "s" : "c");
2585 /* Same as remote_resume, but with async support. */
2587 remote_async_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2589 remote_resume (ptid
, step
, siggnal
);
2591 /* We are about to start executing the inferior, let's register it
2592 with the event loop. NOTE: this is the one place where all the
2593 execution commands end up. We could alternatively do this in each
2594 of the execution commands in infcmd.c. */
2595 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2596 into infcmd.c in order to allow inferior function calls to work
2597 NOT asynchronously. */
2598 if (target_can_async_p ())
2599 target_async (inferior_event_handler
, 0);
2600 /* Tell the world that the target is now executing. */
2601 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2602 this? Instead, should the client of target just assume (for
2603 async targets) that the target is going to start executing? Is
2604 this information already found in the continuation block? */
2605 if (target_is_async_p ())
2606 target_executing
= 1;
2610 /* Set up the signal handler for SIGINT, while the target is
2611 executing, ovewriting the 'regular' SIGINT signal handler. */
2613 initialize_sigint_signal_handler (void)
2615 sigint_remote_token
=
2616 create_async_signal_handler (async_remote_interrupt
, NULL
);
2617 signal (SIGINT
, handle_remote_sigint
);
2620 /* Signal handler for SIGINT, while the target is executing. */
2622 handle_remote_sigint (int sig
)
2624 signal (sig
, handle_remote_sigint_twice
);
2625 sigint_remote_twice_token
=
2626 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
2627 mark_async_signal_handler_wrapper (sigint_remote_token
);
2630 /* Signal handler for SIGINT, installed after SIGINT has already been
2631 sent once. It will take effect the second time that the user sends
2634 handle_remote_sigint_twice (int sig
)
2636 signal (sig
, handle_sigint
);
2637 sigint_remote_twice_token
=
2638 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
2639 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
2642 /* Perform the real interruption of the target execution, in response
2645 async_remote_interrupt (gdb_client_data arg
)
2648 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2653 /* Perform interrupt, if the first attempt did not succeed. Just give
2654 up on the target alltogether. */
2656 async_remote_interrupt_twice (gdb_client_data arg
)
2659 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
2660 /* Do something only if the target was not killed by the previous
2662 if (target_executing
)
2665 signal (SIGINT
, handle_remote_sigint
);
2669 /* Reinstall the usual SIGINT handlers, after the target has
2672 cleanup_sigint_signal_handler (void *dummy
)
2674 signal (SIGINT
, handle_sigint
);
2675 if (sigint_remote_twice_token
)
2676 delete_async_signal_handler ((struct async_signal_handler
**)
2677 &sigint_remote_twice_token
);
2678 if (sigint_remote_token
)
2679 delete_async_signal_handler ((struct async_signal_handler
**)
2680 &sigint_remote_token
);
2683 /* Send ^C to target to halt it. Target will respond, and send us a
2685 static void (*ofunc
) (int);
2687 /* The command line interface's stop routine. This function is installed
2688 as a signal handler for SIGINT. The first time a user requests a
2689 stop, we call remote_stop to send a break or ^C. If there is no
2690 response from the target (it didn't stop when the user requested it),
2691 we ask the user if he'd like to detach from the target. */
2693 remote_interrupt (int signo
)
2695 /* If this doesn't work, try more severe steps. */
2696 signal (signo
, remote_interrupt_twice
);
2699 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2704 /* The user typed ^C twice. */
2707 remote_interrupt_twice (int signo
)
2709 signal (signo
, ofunc
);
2711 signal (signo
, remote_interrupt
);
2714 /* This is the generic stop called via the target vector. When a target
2715 interrupt is requested, either by the command line or the GUI, we
2716 will eventually end up here. */
2720 /* Send a break or a ^C, depending on user preference. */
2722 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
2725 serial_send_break (remote_desc
);
2727 serial_write (remote_desc
, "\003", 1);
2730 /* Ask the user what to do when an interrupt is received. */
2733 interrupt_query (void)
2735 target_terminal_ours ();
2737 if (query ("Interrupted while waiting for the program.\n\
2738 Give up (and stop debugging it)? "))
2740 target_mourn_inferior ();
2741 throw_reason (RETURN_QUIT
);
2744 target_terminal_inferior ();
2747 /* Enable/disable target terminal ownership. Most targets can use
2748 terminal groups to control terminal ownership. Remote targets are
2749 different in that explicit transfer of ownership to/from GDB/target
2753 remote_async_terminal_inferior (void)
2755 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2756 sync_execution here. This function should only be called when
2757 GDB is resuming the inferior in the forground. A background
2758 resume (``run&'') should leave GDB in control of the terminal and
2759 consequently should not call this code. */
2760 if (!sync_execution
)
2762 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2763 calls target_terminal_*() idenpotent. The event-loop GDB talking
2764 to an asynchronous target with a synchronous command calls this
2765 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2766 stops trying to transfer the terminal to the target when it
2767 shouldn't this guard can go away. */
2768 if (!remote_async_terminal_ours_p
)
2770 delete_file_handler (input_fd
);
2771 remote_async_terminal_ours_p
= 0;
2772 initialize_sigint_signal_handler ();
2773 /* NOTE: At this point we could also register our selves as the
2774 recipient of all input. Any characters typed could then be
2775 passed on down to the target. */
2779 remote_async_terminal_ours (void)
2781 /* See FIXME in remote_async_terminal_inferior. */
2782 if (!sync_execution
)
2784 /* See FIXME in remote_async_terminal_inferior. */
2785 if (remote_async_terminal_ours_p
)
2787 cleanup_sigint_signal_handler (NULL
);
2788 add_file_handler (input_fd
, stdin_event_handler
, 0);
2789 remote_async_terminal_ours_p
= 1;
2792 /* If nonzero, ignore the next kill. */
2797 remote_console_output (char *msg
)
2801 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
2804 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
2807 fputs_unfiltered (tb
, gdb_stdtarg
);
2809 gdb_flush (gdb_stdtarg
);
2812 /* Wait until the remote machine stops, then return,
2813 storing status in STATUS just as `wait' would.
2814 Returns "pid", which in the case of a multi-threaded
2815 remote OS, is the thread-id. */
2818 remote_wait (ptid_t ptid
, struct target_waitstatus
*status
)
2820 struct remote_state
*rs
= get_remote_state ();
2821 unsigned char *buf
= alloca (rs
->remote_packet_size
);
2822 ULONGEST thread_num
= -1;
2825 status
->kind
= TARGET_WAITKIND_EXITED
;
2826 status
->value
.integer
= 0;
2832 ofunc
= signal (SIGINT
, remote_interrupt
);
2833 getpkt (buf
, (rs
->remote_packet_size
), 1);
2834 signal (SIGINT
, ofunc
);
2836 /* This is a hook for when we need to do something (perhaps the
2837 collection of trace data) every time the target stops. */
2838 if (deprecated_target_wait_loop_hook
)
2839 (*deprecated_target_wait_loop_hook
) ();
2841 remote_stopped_by_watchpoint_p
= 0;
2845 case 'E': /* Error of some sort. */
2846 warning ("Remote failure reply: %s", buf
);
2848 case 'F': /* File-I/O request. */
2849 remote_fileio_request (buf
);
2851 case 'T': /* Status with PC, SP, FP, ... */
2854 char regs
[MAX_REGISTER_SIZE
];
2856 /* Expedited reply, containing Signal, {regno, reg} repeat. */
2857 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2859 n... = register number
2860 r... = register contents
2862 p
= &buf
[3]; /* after Txx */
2871 /* If the packet contains a register number save it in
2872 pnum and set p1 to point to the character following
2873 it. Otherwise p1 points to p. */
2875 /* If this packet is an awatch packet, don't parse the
2876 'a' as a register number. */
2878 if (strncmp (p
, "awatch", strlen("awatch")) != 0)
2880 /* Read the ``P'' register number. */
2881 pnum
= strtol (p
, &p_temp
, 16);
2882 p1
= (unsigned char *) p_temp
;
2887 if (p1
== p
) /* No register number present here. */
2889 p1
= (unsigned char *) strchr (p
, ':');
2891 warning ("Malformed packet(a) (missing colon): %s\n\
2894 if (strncmp (p
, "thread", p1
- p
) == 0)
2896 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2897 record_currthread (thread_num
);
2898 p
= (unsigned char *) p_temp
;
2900 else if ((strncmp (p
, "watch", p1
- p
) == 0)
2901 || (strncmp (p
, "rwatch", p1
- p
) == 0)
2902 || (strncmp (p
, "awatch", p1
- p
) == 0))
2904 remote_stopped_by_watchpoint_p
= 1;
2905 p
= unpack_varlen_hex (++p1
, &addr
);
2906 remote_watch_data_address
= (CORE_ADDR
)addr
;
2910 /* Silently skip unknown optional info. */
2911 p_temp
= strchr (p1
+ 1, ';');
2913 p
= (unsigned char *) p_temp
;
2918 struct packet_reg
*reg
= packet_reg_from_pnum (rs
, pnum
);
2922 error ("Malformed packet(b) (missing colon): %s\n\
2927 error ("Remote sent bad register number %s: %s\n\
2929 phex_nz (pnum
, 0), p
, buf
);
2931 fieldsize
= hex2bin (p
, regs
,
2932 register_size (current_gdbarch
,
2935 if (fieldsize
< register_size (current_gdbarch
,
2937 warning ("Remote reply is too short: %s", buf
);
2938 regcache_raw_supply (current_regcache
,
2943 error ("Remote register badly formatted: %s\nhere: %s",
2948 case 'S': /* Old style status, just signal only. */
2949 status
->kind
= TARGET_WAITKIND_STOPPED
;
2950 status
->value
.sig
= (enum target_signal
)
2951 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2955 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2956 record_currthread (thread_num
);
2959 case 'W': /* Target exited. */
2961 /* The remote process exited. */
2962 status
->kind
= TARGET_WAITKIND_EXITED
;
2963 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2967 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2968 status
->value
.sig
= (enum target_signal
)
2969 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2973 case 'O': /* Console output. */
2974 remote_console_output (buf
+ 1);
2977 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2979 /* Zero length reply means that we tried 'S' or 'C' and
2980 the remote system doesn't support it. */
2981 target_terminal_ours_for_output ();
2983 ("Can't send signals to this remote system. %s not sent.\n",
2984 target_signal_to_name (last_sent_signal
));
2985 last_sent_signal
= TARGET_SIGNAL_0
;
2986 target_terminal_inferior ();
2988 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2989 putpkt ((char *) buf
);
2992 /* else fallthrough */
2994 warning ("Invalid remote reply: %s", buf
);
2999 if (thread_num
!= -1)
3001 return pid_to_ptid (thread_num
);
3003 return inferior_ptid
;
3006 /* Async version of remote_wait. */
3008 remote_async_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3010 struct remote_state
*rs
= get_remote_state ();
3011 unsigned char *buf
= alloca (rs
->remote_packet_size
);
3012 ULONGEST thread_num
= -1;
3015 status
->kind
= TARGET_WAITKIND_EXITED
;
3016 status
->value
.integer
= 0;
3018 remote_stopped_by_watchpoint_p
= 0;
3024 if (!target_is_async_p ())
3025 ofunc
= signal (SIGINT
, remote_interrupt
);
3026 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3027 _never_ wait for ever -> test on target_is_async_p().
3028 However, before we do that we need to ensure that the caller
3029 knows how to take the target into/out of async mode. */
3030 getpkt (buf
, (rs
->remote_packet_size
), wait_forever_enabled_p
);
3031 if (!target_is_async_p ())
3032 signal (SIGINT
, ofunc
);
3034 /* This is a hook for when we need to do something (perhaps the
3035 collection of trace data) every time the target stops. */
3036 if (deprecated_target_wait_loop_hook
)
3037 (*deprecated_target_wait_loop_hook
) ();
3041 case 'E': /* Error of some sort. */
3042 warning ("Remote failure reply: %s", buf
);
3044 case 'F': /* File-I/O request. */
3045 remote_fileio_request (buf
);
3047 case 'T': /* Status with PC, SP, FP, ... */
3050 char regs
[MAX_REGISTER_SIZE
];
3052 /* Expedited reply, containing Signal, {regno, reg} repeat. */
3053 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
3055 n... = register number
3056 r... = register contents
3058 p
= &buf
[3]; /* after Txx */
3067 /* If the packet contains a register number, save it
3068 in pnum and set p1 to point to the character
3069 following it. Otherwise p1 points to p. */
3071 /* If this packet is an awatch packet, don't parse the 'a'
3072 as a register number. */
3074 if (!strncmp (p
, "awatch", strlen ("awatch")) != 0)
3076 /* Read the register number. */
3077 pnum
= strtol (p
, &p_temp
, 16);
3078 p1
= (unsigned char *) p_temp
;
3083 if (p1
== p
) /* No register number present here. */
3085 p1
= (unsigned char *) strchr (p
, ':');
3087 error ("Malformed packet(a) (missing colon): %s\n\
3090 if (strncmp (p
, "thread", p1
- p
) == 0)
3092 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
3093 record_currthread (thread_num
);
3094 p
= (unsigned char *) p_temp
;
3096 else if ((strncmp (p
, "watch", p1
- p
) == 0)
3097 || (strncmp (p
, "rwatch", p1
- p
) == 0)
3098 || (strncmp (p
, "awatch", p1
- p
) == 0))
3100 remote_stopped_by_watchpoint_p
= 1;
3101 p
= unpack_varlen_hex (++p1
, &addr
);
3102 remote_watch_data_address
= (CORE_ADDR
)addr
;
3106 /* Silently skip unknown optional info. */
3107 p_temp
= (unsigned char *) strchr (p1
+ 1, ';');
3115 struct packet_reg
*reg
= packet_reg_from_pnum (rs
, pnum
);
3118 error ("Malformed packet(b) (missing colon): %s\n\
3123 error ("Remote sent bad register number %ld: %s\n\
3127 fieldsize
= hex2bin (p
, regs
,
3128 register_size (current_gdbarch
,
3131 if (fieldsize
< register_size (current_gdbarch
,
3133 warning ("Remote reply is too short: %s", buf
);
3134 regcache_raw_supply (current_regcache
, reg
->regnum
, regs
);
3138 error ("Remote register badly formatted: %s\nhere: %s",
3143 case 'S': /* Old style status, just signal only. */
3144 status
->kind
= TARGET_WAITKIND_STOPPED
;
3145 status
->value
.sig
= (enum target_signal
)
3146 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3150 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
3151 record_currthread (thread_num
);
3154 case 'W': /* Target exited. */
3156 /* The remote process exited. */
3157 status
->kind
= TARGET_WAITKIND_EXITED
;
3158 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
3162 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
3163 status
->value
.sig
= (enum target_signal
)
3164 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3168 case 'O': /* Console output. */
3169 remote_console_output (buf
+ 1);
3170 /* Return immediately to the event loop. The event loop will
3171 still be waiting on the inferior afterwards. */
3172 status
->kind
= TARGET_WAITKIND_IGNORE
;
3175 if (last_sent_signal
!= TARGET_SIGNAL_0
)
3177 /* Zero length reply means that we tried 'S' or 'C' and
3178 the remote system doesn't support it. */
3179 target_terminal_ours_for_output ();
3181 ("Can't send signals to this remote system. %s not sent.\n",
3182 target_signal_to_name (last_sent_signal
));
3183 last_sent_signal
= TARGET_SIGNAL_0
;
3184 target_terminal_inferior ();
3186 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
3187 putpkt ((char *) buf
);
3190 /* else fallthrough */
3192 warning ("Invalid remote reply: %s", buf
);
3197 if (thread_num
!= -1)
3199 return pid_to_ptid (thread_num
);
3201 return inferior_ptid
;
3204 /* Number of bytes of registers this stub implements. */
3206 static int register_bytes_found
;
3208 /* Read the remote registers into the block REGS. */
3209 /* Currently we just read all the registers, so we don't use regnum. */
3212 fetch_register_using_p (int regnum
)
3214 struct remote_state
*rs
= get_remote_state ();
3215 char *buf
= alloca (rs
->remote_packet_size
), *p
;
3216 char regp
[MAX_REGISTER_SIZE
];
3221 p
+= hexnumstr (p
, regnum
);
3223 remote_send (buf
, rs
->remote_packet_size
);
3225 /* If the stub didn't recognize the packet, or if we got an error,
3227 if (buf
[0] == '\0' || buf
[0] == 'E')
3230 /* If this register is unfetchable, tell the regcache. */
3233 regcache_raw_supply (current_regcache
, regnum
, NULL
);
3234 set_register_cached (regnum
, -1);
3238 /* Otherwise, parse and supply the value. */
3245 error ("fetch_register_using_p: early buf termination");
3249 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3252 regcache_raw_supply (current_regcache
, regnum
, regp
);
3257 remote_fetch_registers (int regnum
)
3259 struct remote_state
*rs
= get_remote_state ();
3260 char *buf
= alloca (rs
->remote_packet_size
);
3263 char *regs
= alloca (rs
->sizeof_g_packet
);
3265 set_thread (PIDGET (inferior_ptid
), 1);
3269 struct packet_reg
*reg
= packet_reg_from_regnum (rs
, regnum
);
3270 gdb_assert (reg
!= NULL
);
3271 if (!reg
->in_g_packet
)
3272 internal_error (__FILE__
, __LINE__
,
3273 "Attempt to fetch a non G-packet register when this "
3274 "remote.c does not support the p-packet.");
3276 switch (remote_protocol_p
.support
)
3278 case PACKET_DISABLE
:
3281 if (fetch_register_using_p (regnum
))
3284 error ("Protocol error: p packet not recognized by stub");
3285 case PACKET_SUPPORT_UNKNOWN
:
3286 if (fetch_register_using_p (regnum
))
3288 /* The stub recognized the 'p' packet. Remember this. */
3289 remote_protocol_p
.support
= PACKET_ENABLE
;
3294 /* The stub does not support the 'P' packet. Use 'G'
3295 instead, and don't try using 'P' in the future (it
3296 will just waste our time). */
3297 remote_protocol_p
.support
= PACKET_DISABLE
;
3303 remote_send (buf
, (rs
->remote_packet_size
));
3305 /* Save the size of the packet sent to us by the target. Its used
3306 as a heuristic when determining the max size of packets that the
3307 target can safely receive. */
3308 if ((rs
->actual_register_packet_size
) == 0)
3309 (rs
->actual_register_packet_size
) = strlen (buf
);
3311 /* Unimplemented registers read as all bits zero. */
3312 memset (regs
, 0, rs
->sizeof_g_packet
);
3314 /* We can get out of synch in various cases. If the first character
3315 in the buffer is not a hex character, assume that has happened
3316 and try to fetch another packet to read. */
3317 while ((buf
[0] < '0' || buf
[0] > '9')
3318 && (buf
[0] < 'a' || buf
[0] > 'f')
3319 && buf
[0] != 'x') /* New: unavailable register value. */
3322 fprintf_unfiltered (gdb_stdlog
,
3323 "Bad register packet; fetching a new packet\n");
3324 getpkt (buf
, (rs
->remote_packet_size
), 0);
3327 /* Reply describes registers byte by byte, each byte encoded as two
3328 hex characters. Suck them all up, then supply them to the
3329 register cacheing/storage mechanism. */
3332 for (i
= 0; i
< rs
->sizeof_g_packet
; i
++)
3338 warning ("Remote reply is of odd length: %s", buf
);
3339 /* Don't change register_bytes_found in this case, and don't
3340 print a second warning. */
3343 if (p
[0] == 'x' && p
[1] == 'x')
3344 regs
[i
] = 0; /* 'x' */
3346 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3350 if (i
!= register_bytes_found
)
3352 register_bytes_found
= i
;
3353 if (REGISTER_BYTES_OK_P ()
3354 && !REGISTER_BYTES_OK (i
))
3355 warning ("Remote reply is too short: %s", buf
);
3361 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
3363 struct packet_reg
*r
= &rs
->regs
[i
];
3366 if (r
->offset
* 2 >= strlen (buf
))
3367 /* A short packet that didn't include the register's
3368 value, this implies that the register is zero (and
3369 not that the register is unavailable). Supply that
3371 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3372 else if (buf
[r
->offset
* 2] == 'x')
3374 gdb_assert (r
->offset
* 2 < strlen (buf
));
3375 /* The register isn't available, mark it as such (at
3376 the same time setting the value to zero). */
3377 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3378 set_register_cached (i
, -1);
3381 regcache_raw_supply (current_regcache
, r
->regnum
,
3388 /* Prepare to store registers. Since we may send them all (using a
3389 'G' request), we have to read out the ones we don't want to change
3393 remote_prepare_to_store (void)
3395 struct remote_state
*rs
= get_remote_state ();
3397 char buf
[MAX_REGISTER_SIZE
];
3399 /* Make sure the entire registers array is valid. */
3400 switch (remote_protocol_P
.support
)
3402 case PACKET_DISABLE
:
3403 case PACKET_SUPPORT_UNKNOWN
:
3404 /* Make sure all the necessary registers are cached. */
3405 for (i
= 0; i
< NUM_REGS
; i
++)
3406 if (rs
->regs
[i
].in_g_packet
)
3407 regcache_raw_read (current_regcache
, rs
->regs
[i
].regnum
, buf
);
3414 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
3415 packet was not recognized. */
3418 store_register_using_P (int regnum
)
3420 struct remote_state
*rs
= get_remote_state ();
3421 struct packet_reg
*reg
= packet_reg_from_regnum (rs
, regnum
);
3422 /* Try storing a single register. */
3423 char *buf
= alloca (rs
->remote_packet_size
);
3424 char regp
[MAX_REGISTER_SIZE
];
3428 sprintf (buf
, "P%s=", phex_nz (reg
->pnum
, 0));
3429 p
= buf
+ strlen (buf
);
3430 regcache_raw_collect (current_regcache
, reg
->regnum
, regp
);
3431 bin2hex (regp
, p
, register_size (current_gdbarch
, reg
->regnum
));
3432 remote_send (buf
, rs
->remote_packet_size
);
3434 return buf
[0] != '\0';
3438 /* Store register REGNUM, or all registers if REGNUM == -1, from the
3439 contents of the register cache buffer. FIXME: ignores errors. */
3442 remote_store_registers (int regnum
)
3444 struct remote_state
*rs
= get_remote_state ();
3450 set_thread (PIDGET (inferior_ptid
), 1);
3454 switch (remote_protocol_P
.support
)
3456 case PACKET_DISABLE
:
3459 if (store_register_using_P (regnum
))
3462 error ("Protocol error: P packet not recognized by stub");
3463 case PACKET_SUPPORT_UNKNOWN
:
3464 if (store_register_using_P (regnum
))
3466 /* The stub recognized the 'P' packet. Remember this. */
3467 remote_protocol_P
.support
= PACKET_ENABLE
;
3472 /* The stub does not support the 'P' packet. Use 'G'
3473 instead, and don't try using 'P' in the future (it
3474 will just waste our time). */
3475 remote_protocol_P
.support
= PACKET_DISABLE
;
3481 /* Extract all the registers in the regcache copying them into a
3485 regs
= alloca (rs
->sizeof_g_packet
);
3486 memset (regs
, rs
->sizeof_g_packet
, 0);
3487 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
3489 struct packet_reg
*r
= &rs
->regs
[i
];
3491 regcache_raw_collect (current_regcache
, r
->regnum
, regs
+ r
->offset
);
3495 /* Command describes registers byte by byte,
3496 each byte encoded as two hex characters. */
3497 buf
= alloca (rs
->remote_packet_size
);
3500 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3501 bin2hex (regs
, p
, register_bytes_found
);
3502 remote_send (buf
, (rs
->remote_packet_size
));
3506 /* Return the number of hex digits in num. */
3509 hexnumlen (ULONGEST num
)
3513 for (i
= 0; num
!= 0; i
++)
3519 /* Set BUF to the minimum number of hex digits representing NUM. */
3522 hexnumstr (char *buf
, ULONGEST num
)
3524 int len
= hexnumlen (num
);
3525 return hexnumnstr (buf
, num
, len
);
3529 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3532 hexnumnstr (char *buf
, ULONGEST num
, int width
)
3538 for (i
= width
- 1; i
>= 0; i
--)
3540 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
3547 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3550 remote_address_masked (CORE_ADDR addr
)
3552 if (remote_address_size
> 0
3553 && remote_address_size
< (sizeof (ULONGEST
) * 8))
3555 /* Only create a mask when that mask can safely be constructed
3556 in a ULONGEST variable. */
3558 mask
= (mask
<< remote_address_size
) - 1;
3564 /* Determine whether the remote target supports binary downloading.
3565 This is accomplished by sending a no-op memory write of zero length
3566 to the target at the specified address. It does not suffice to send
3567 the whole packet, since many stubs strip the eighth bit and
3568 subsequently compute a wrong checksum, which causes real havoc with
3571 NOTE: This can still lose if the serial line is not eight-bit
3572 clean. In cases like this, the user should clear "remote
3576 check_binary_download (CORE_ADDR addr
)
3578 struct remote_state
*rs
= get_remote_state ();
3579 switch (remote_protocol_binary_download
.support
)
3581 case PACKET_DISABLE
:
3585 case PACKET_SUPPORT_UNKNOWN
:
3587 char *buf
= alloca (rs
->remote_packet_size
);
3592 p
+= hexnumstr (p
, (ULONGEST
) addr
);
3594 p
+= hexnumstr (p
, (ULONGEST
) 0);
3598 putpkt_binary (buf
, (int) (p
- buf
));
3599 getpkt (buf
, (rs
->remote_packet_size
), 0);
3604 fprintf_unfiltered (gdb_stdlog
,
3605 "binary downloading NOT suppported by target\n");
3606 remote_protocol_binary_download
.support
= PACKET_DISABLE
;
3611 fprintf_unfiltered (gdb_stdlog
,
3612 "binary downloading suppported by target\n");
3613 remote_protocol_binary_download
.support
= PACKET_ENABLE
;
3620 /* Write memory data directly to the remote machine.
3621 This does not inform the data cache; the data cache uses this.
3622 MEMADDR is the address in the remote memory space.
3623 MYADDR is the address of the buffer in our space.
3624 LEN is the number of bytes.
3626 Returns number of bytes transferred, or 0 (setting errno) for
3627 error. Only transfer a single packet. */
3630 remote_write_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
3634 unsigned char *plen
;
3640 unsigned char *payload_start
;
3642 /* Verify that the target can support a binary download. */
3643 check_binary_download (memaddr
);
3645 /* Compute the size, and then allocate space for the largest
3646 possible packet. Include space for an extra trailing NULL. */
3647 sizeof_buf
= get_memory_write_packet_size () + 1;
3648 buf
= alloca (sizeof_buf
);
3650 /* Compute the size of the actual payload by subtracting out the
3651 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
3652 payload_size
= (get_memory_write_packet_size () - (strlen ("$M,:#NN")
3653 + hexnumlen (memaddr
)
3654 + hexnumlen (len
)));
3656 /* Construct the packet header: "[MX]<memaddr>,<len>:". */
3658 /* Append "[XM]". Compute a best guess of the number of bytes
3659 actually transfered. */
3661 switch (remote_protocol_binary_download
.support
)
3665 /* Best guess at number of bytes that will fit. */
3666 todo
= min (len
, payload_size
);
3668 case PACKET_DISABLE
:
3670 /* Num bytes that will fit. */
3671 todo
= min (len
, payload_size
/ 2);
3673 case PACKET_SUPPORT_UNKNOWN
:
3674 internal_error (__FILE__
, __LINE__
,
3675 "remote_write_bytes: bad internal state");
3677 internal_error (__FILE__
, __LINE__
, "bad switch");
3680 /* Append "<memaddr>". */
3681 memaddr
= remote_address_masked (memaddr
);
3682 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3687 /* Append <len>. Retain the location/size of <len>. It may need to
3688 be adjusted once the packet body has been created. */
3690 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
3697 /* Append the packet body. */
3699 switch (remote_protocol_binary_download
.support
)
3702 /* Binary mode. Send target system values byte by byte, in
3703 increasing byte addresses. Only escape certain critical
3706 (nr_bytes
< todo
) && (p
- payload_start
) < payload_size
;
3709 switch (myaddr
[nr_bytes
] & 0xff)
3714 /* These must be escaped. */
3716 *p
++ = (myaddr
[nr_bytes
] & 0xff) ^ 0x20;
3719 *p
++ = myaddr
[nr_bytes
] & 0xff;
3723 if (nr_bytes
< todo
)
3725 /* Escape chars have filled up the buffer prematurely,
3726 and we have actually sent fewer bytes than planned.
3727 Fix-up the length field of the packet. Use the same
3728 number of characters as before. */
3729 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
3730 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
3733 case PACKET_DISABLE
:
3734 /* Normal mode: Send target system values byte by byte, in
3735 increasing byte addresses. Each byte is encoded as a two hex
3737 nr_bytes
= bin2hex (myaddr
, p
, todo
);
3740 case PACKET_SUPPORT_UNKNOWN
:
3741 internal_error (__FILE__
, __LINE__
,
3742 "remote_write_bytes: bad internal state");
3744 internal_error (__FILE__
, __LINE__
, "bad switch");
3747 putpkt_binary (buf
, (int) (p
- buf
));
3748 getpkt (buf
, sizeof_buf
, 0);
3752 /* There is no correspondance between what the remote protocol
3753 uses for errors and errno codes. We would like a cleaner way
3754 of representing errors (big enough to include errno codes,
3755 bfd_error codes, and others). But for now just return EIO. */
3760 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
3761 fewer bytes than we'd planned. */
3765 /* Read memory data directly from the remote machine.
3766 This does not use the data cache; the data cache uses this.
3767 MEMADDR is the address in the remote memory space.
3768 MYADDR is the address of the buffer in our space.
3769 LEN is the number of bytes.
3771 Returns number of bytes transferred, or 0 for error. */
3773 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3774 remote targets) shouldn't attempt to read the entire buffer.
3775 Instead it should read a single packet worth of data and then
3776 return the byte size of that packet to the caller. The caller (its
3777 caller and its callers caller ;-) already contains code for
3778 handling partial reads. */
3781 remote_read_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
3784 int max_buf_size
; /* Max size of packet output buffer. */
3788 /* Create a buffer big enough for this packet. */
3789 max_buf_size
= get_memory_read_packet_size ();
3790 sizeof_buf
= max_buf_size
+ 1; /* Space for trailing NULL. */
3791 buf
= alloca (sizeof_buf
);
3800 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
3802 /* construct "m"<memaddr>","<len>" */
3803 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3804 memaddr
= remote_address_masked (memaddr
);
3807 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3809 p
+= hexnumstr (p
, (ULONGEST
) todo
);
3813 getpkt (buf
, sizeof_buf
, 0);
3816 && isxdigit (buf
[1]) && isxdigit (buf
[2])
3819 /* There is no correspondance between what the remote
3820 protocol uses for errors and errno codes. We would like
3821 a cleaner way of representing errors (big enough to
3822 include errno codes, bfd_error codes, and others). But
3823 for now just return EIO. */
3828 /* Reply describes memory byte by byte,
3829 each byte encoded as two hex characters. */
3832 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
3834 /* Reply is short. This means that we were able to read
3835 only part of what we wanted to. */
3836 return i
+ (origlen
- len
);
3845 /* Read or write LEN bytes from inferior memory at MEMADDR,
3846 transferring to or from debugger address BUFFER. Write to inferior
3847 if SHOULD_WRITE is nonzero. Returns length of data written or
3848 read; 0 for error. TARGET is unused. */
3851 remote_xfer_memory (CORE_ADDR mem_addr
, char *buffer
, int mem_len
,
3852 int should_write
, struct mem_attrib
*attrib
,
3853 struct target_ops
*target
)
3855 CORE_ADDR targ_addr
;
3859 /* Should this be the selected frame? */
3860 gdbarch_remote_translate_xfer_address (current_gdbarch
,
3863 &targ_addr
, &targ_len
);
3868 res
= remote_write_bytes (targ_addr
, buffer
, targ_len
);
3870 res
= remote_read_bytes (targ_addr
, buffer
, targ_len
);
3876 remote_files_info (struct target_ops
*ignore
)
3878 puts_filtered ("Debugging a target over a serial line.\n");
3881 /* Stuff for dealing with the packets which are part of this protocol.
3882 See comment at top of file for details. */
3884 /* Read a single character from the remote end, masking it down to 7
3888 readchar (int timeout
)
3892 ch
= serial_readchar (remote_desc
, timeout
);
3897 switch ((enum serial_rc
) ch
)
3900 target_mourn_inferior ();
3901 error ("Remote connection closed");
3904 perror_with_name ("Remote communication error");
3906 case SERIAL_TIMEOUT
:
3912 /* Send the command in BUF to the remote machine, and read the reply
3913 into BUF. Report an error if we get an error reply. */
3916 remote_send (char *buf
,
3920 getpkt (buf
, sizeof_buf
, 0);
3923 error ("Remote failure reply: %s", buf
);
3926 /* Display a null-terminated packet on stdout, for debugging, using C
3930 print_packet (char *buf
)
3932 puts_filtered ("\"");
3933 fputstr_filtered (buf
, '"', gdb_stdout
);
3934 puts_filtered ("\"");
3940 return putpkt_binary (buf
, strlen (buf
));
3943 /* Send a packet to the remote machine, with error checking. The data
3944 of the packet is in BUF. The string in BUF can be at most
3945 (rs->remote_packet_size) - 5 to account for the $, # and checksum,
3946 and for a possible /0 if we are debugging (remote_debug) and want
3947 to print the sent packet as a string. */
3950 putpkt_binary (char *buf
, int cnt
)
3952 struct remote_state
*rs
= get_remote_state ();
3954 unsigned char csum
= 0;
3955 char *buf2
= alloca (cnt
+ 6);
3956 long sizeof_junkbuf
= (rs
->remote_packet_size
);
3957 char *junkbuf
= alloca (sizeof_junkbuf
);
3963 /* Copy the packet into buffer BUF2, encapsulating it
3964 and giving it a checksum. */
3969 for (i
= 0; i
< cnt
; i
++)
3975 *p
++ = tohex ((csum
>> 4) & 0xf);
3976 *p
++ = tohex (csum
& 0xf);
3978 /* Send it over and over until we get a positive ack. */
3982 int started_error_output
= 0;
3987 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
3988 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
3989 fprintf_unfiltered (gdb_stdlog
, "...");
3990 gdb_flush (gdb_stdlog
);
3992 if (serial_write (remote_desc
, buf2
, p
- buf2
))
3993 perror_with_name ("putpkt: write failed");
3995 /* Read until either a timeout occurs (-2) or '+' is read. */
3998 ch
= readchar (remote_timeout
);
4006 case SERIAL_TIMEOUT
:
4008 if (started_error_output
)
4010 putchar_unfiltered ('\n');
4011 started_error_output
= 0;
4020 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
4024 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
4025 case SERIAL_TIMEOUT
:
4029 break; /* Retransmit buffer. */
4033 fprintf_unfiltered (gdb_stdlog
,
4034 "Packet instead of Ack, ignoring it\n");
4035 /* It's probably an old response sent because an ACK
4036 was lost. Gobble up the packet and ack it so it
4037 doesn't get retransmitted when we resend this
4039 read_frame (junkbuf
, sizeof_junkbuf
);
4040 serial_write (remote_desc
, "+", 1);
4041 continue; /* Now, go look for +. */
4046 if (!started_error_output
)
4048 started_error_output
= 1;
4049 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
4051 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
4055 break; /* Here to retransmit. */
4059 /* This is wrong. If doing a long backtrace, the user should be
4060 able to get out next time we call QUIT, without anything as
4061 violent as interrupt_query. If we want to provide a way out of
4062 here without getting to the next QUIT, it should be based on
4063 hitting ^C twice as in remote_wait. */
4073 /* Come here after finding the start of the frame. Collect the rest
4074 into BUF, verifying the checksum, length, and handling run-length
4075 compression. No more than sizeof_buf-1 characters are read so that
4076 the buffer can be NUL terminated.
4078 Returns -1 on error, number of characters in buffer (ignoring the
4079 trailing NULL) on success. (could be extended to return one of the
4080 SERIAL status indications). */
4083 read_frame (char *buf
,
4095 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NULL. */
4096 c
= readchar (remote_timeout
);
4099 case SERIAL_TIMEOUT
:
4101 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
4105 fputs_filtered ("Saw new packet start in middle of old one\n",
4107 return -1; /* Start a new packet, count retries. */
4110 unsigned char pktcsum
;
4116 check_0
= readchar (remote_timeout
);
4118 check_1
= readchar (remote_timeout
);
4120 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
4123 fputs_filtered ("Timeout in checksum, retrying\n",
4127 else if (check_0
< 0 || check_1
< 0)
4130 fputs_filtered ("Communication error in checksum\n",
4135 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
4136 if (csum
== pktcsum
)
4141 fprintf_filtered (gdb_stdlog
,
4142 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4144 fputs_filtered (buf
, gdb_stdlog
);
4145 fputs_filtered ("\n", gdb_stdlog
);
4147 /* Number of characters in buffer ignoring trailing
4151 case '*': /* Run length encoding. */
4156 c
= readchar (remote_timeout
);
4158 repeat
= c
- ' ' + 3; /* Compute repeat count. */
4160 /* The character before ``*'' is repeated. */
4162 if (repeat
> 0 && repeat
<= 255
4164 && bc
+ repeat
- 1 < sizeof_buf
- 1)
4166 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
4172 printf_filtered ("Repeat count %d too large for buffer: ",
4174 puts_filtered (buf
);
4175 puts_filtered ("\n");
4179 if (bc
< sizeof_buf
- 1)
4187 puts_filtered ("Remote packet too long: ");
4188 puts_filtered (buf
);
4189 puts_filtered ("\n");
4196 /* Read a packet from the remote machine, with error checking, and
4197 store it in BUF. If FOREVER, wait forever rather than timing out;
4198 this is used (in synchronous mode) to wait for a target that is is
4199 executing user code to stop. */
4200 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4201 don't have to change all the calls to getpkt to deal with the
4202 return value, because at the moment I don't know what the right
4203 thing to do it for those. */
4211 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
4215 /* Read a packet from the remote machine, with error checking, and
4216 store it in BUF. If FOREVER, wait forever rather than timing out;
4217 this is used (in synchronous mode) to wait for a target that is is
4218 executing user code to stop. If FOREVER == 0, this function is
4219 allowed to time out gracefully and return an indication of this to
4222 getpkt_sane (char *buf
,
4231 strcpy (buf
, "timeout");
4235 timeout
= watchdog
> 0 ? watchdog
: -1;
4239 timeout
= remote_timeout
;
4243 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
4245 /* This can loop forever if the remote side sends us characters
4246 continuously, but if it pauses, we'll get a zero from
4247 readchar because of timeout. Then we'll count that as a
4250 /* Note that we will only wait forever prior to the start of a
4251 packet. After that, we expect characters to arrive at a
4252 brisk pace. They should show up within remote_timeout
4257 c
= readchar (timeout
);
4259 if (c
== SERIAL_TIMEOUT
)
4261 if (forever
) /* Watchdog went off? Kill the target. */
4264 target_mourn_inferior ();
4265 error ("Watchdog has expired. Target detached.\n");
4268 fputs_filtered ("Timed out.\n", gdb_stdlog
);
4274 /* We've found the start of a packet, now collect the data. */
4276 val
= read_frame (buf
, sizeof_buf
);
4282 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
4283 fputstr_unfiltered (buf
, 0, gdb_stdlog
);
4284 fprintf_unfiltered (gdb_stdlog
, "\n");
4286 serial_write (remote_desc
, "+", 1);
4290 /* Try the whole thing again. */
4292 serial_write (remote_desc
, "-", 1);
4295 /* We have tried hard enough, and just can't receive the packet.
4298 printf_unfiltered ("Ignoring packet error, continuing...\n");
4299 serial_write (remote_desc
, "+", 1);
4306 /* For some mysterious reason, wait_for_inferior calls kill instead of
4307 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4311 target_mourn_inferior ();
4315 /* Use catch_errors so the user can quit from gdb even when we aren't on
4316 speaking terms with the remote system. */
4317 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4319 /* Don't wait for it to die. I'm not really sure it matters whether
4320 we do or not. For the existing stubs, kill is a noop. */
4321 target_mourn_inferior ();
4324 /* Async version of remote_kill. */
4326 remote_async_kill (void)
4328 /* Unregister the file descriptor from the event loop. */
4329 if (target_is_async_p ())
4330 serial_async (remote_desc
, NULL
, 0);
4332 /* For some mysterious reason, wait_for_inferior calls kill instead of
4333 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4337 target_mourn_inferior ();
4341 /* Use catch_errors so the user can quit from gdb even when we
4342 aren't on speaking terms with the remote system. */
4343 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4345 /* Don't wait for it to die. I'm not really sure it matters whether
4346 we do or not. For the existing stubs, kill is a noop. */
4347 target_mourn_inferior ();
4353 remote_mourn_1 (&remote_ops
);
4357 remote_async_mourn (void)
4359 remote_mourn_1 (&remote_async_ops
);
4363 extended_remote_mourn (void)
4365 /* We do _not_ want to mourn the target like this; this will
4366 remove the extended remote target from the target stack,
4367 and the next time the user says "run" it'll fail.
4369 FIXME: What is the right thing to do here? */
4371 remote_mourn_1 (&extended_remote_ops
);
4375 /* Worker function for remote_mourn. */
4377 remote_mourn_1 (struct target_ops
*target
)
4379 unpush_target (target
);
4380 generic_mourn_inferior ();
4383 /* In the extended protocol we want to be able to do things like
4384 "run" and have them basically work as expected. So we need
4385 a special create_inferior function.
4387 FIXME: One day add support for changing the exec file
4388 we're debugging, arguments and an environment. */
4391 extended_remote_create_inferior (char *exec_file
, char *args
,
4392 char **env
, int from_tty
)
4394 /* Rip out the breakpoints; we'll reinsert them after restarting
4395 the remote server. */
4396 remove_breakpoints ();
4398 /* Now restart the remote server. */
4399 extended_remote_restart ();
4401 /* Now put the breakpoints back in. This way we're safe if the
4402 restart function works via a unix fork on the remote side. */
4403 insert_breakpoints ();
4405 /* Clean up from the last time we were running. */
4406 clear_proceed_status ();
4408 /* Let the remote process run. */
4409 proceed (-1, TARGET_SIGNAL_0
, 0);
4412 /* Async version of extended_remote_create_inferior. */
4414 extended_remote_async_create_inferior (char *exec_file
, char *args
,
4415 char **env
, int from_tty
)
4417 /* Rip out the breakpoints; we'll reinsert them after restarting
4418 the remote server. */
4419 remove_breakpoints ();
4421 /* If running asynchronously, register the target file descriptor
4422 with the event loop. */
4423 if (target_can_async_p ())
4424 target_async (inferior_event_handler
, 0);
4426 /* Now restart the remote server. */
4427 extended_remote_restart ();
4429 /* Now put the breakpoints back in. This way we're safe if the
4430 restart function works via a unix fork on the remote side. */
4431 insert_breakpoints ();
4433 /* Clean up from the last time we were running. */
4434 clear_proceed_status ();
4436 /* Let the remote process run. */
4437 proceed (-1, TARGET_SIGNAL_0
, 0);
4441 /* On some machines, e.g. 68k, we may use a different breakpoint
4442 instruction than other targets; in those use
4443 DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4444 Also, bi-endian targets may define
4445 DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4446 DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we
4447 just call the standard routines that are in mem-break.c. */
4449 /* NOTE: cagney/2003-06-08: This is silly. A remote and simulator
4450 target should use an identical BREAKPOINT_FROM_PC. As for native,
4451 the ARCH-OS-tdep.c code can override the default. */
4453 #if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4454 #define DEPRECATED_REMOTE_BREAKPOINT
4457 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4459 /* If the target isn't bi-endian, just pretend it is. */
4460 #if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4461 #define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4462 #define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4465 static unsigned char big_break_insn
[] = DEPRECATED_BIG_REMOTE_BREAKPOINT
;
4466 static unsigned char little_break_insn
[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT
;
4468 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4470 /* Insert a breakpoint on targets that don't have any better
4471 breakpoint support. We read the contents of the target location
4472 and stash it, then overwrite it with a breakpoint instruction.
4473 ADDR is the target location in the target machine. CONTENTS_CACHE
4474 is a pointer to memory allocated for saving the target contents.
4475 It is guaranteed by the caller to be long enough to save the number
4476 of bytes returned by BREAKPOINT_FROM_PC. */
4479 remote_insert_breakpoint (CORE_ADDR addr
, char *contents_cache
)
4481 struct remote_state
*rs
= get_remote_state ();
4482 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4487 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4488 If it succeeds, then set the support to PACKET_ENABLE. If it
4489 fails, and the user has explicitly requested the Z support then
4490 report an error, otherwise, mark it disabled and go on. */
4492 if (remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
].support
!= PACKET_DISABLE
)
4494 char *buf
= alloca (rs
->remote_packet_size
);
4497 addr
= remote_address_masked (addr
);
4501 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4502 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
4503 sprintf (p
, ",%d", bp_size
);
4506 getpkt (buf
, (rs
->remote_packet_size
), 0);
4508 switch (packet_ok (buf
, &remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
]))
4514 case PACKET_UNKNOWN
:
4519 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4520 val
= target_read_memory (addr
, contents_cache
, sizeof big_break_insn
);
4524 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
4525 val
= target_write_memory (addr
, (char *) big_break_insn
,
4526 sizeof big_break_insn
);
4528 val
= target_write_memory (addr
, (char *) little_break_insn
,
4529 sizeof little_break_insn
);
4534 return memory_insert_breakpoint (addr
, contents_cache
);
4535 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4539 remote_remove_breakpoint (CORE_ADDR addr
, char *contents_cache
)
4541 struct remote_state
*rs
= get_remote_state ();
4544 if (remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
].support
!= PACKET_DISABLE
)
4546 char *buf
= alloca (rs
->remote_packet_size
);
4553 addr
= remote_address_masked (addr
);
4554 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4555 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
4556 sprintf (p
, ",%d", bp_size
);
4559 getpkt (buf
, (rs
->remote_packet_size
), 0);
4561 return (buf
[0] == 'E');
4564 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4565 return target_write_memory (addr
, contents_cache
, sizeof big_break_insn
);
4567 return memory_remove_breakpoint (addr
, contents_cache
);
4568 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4572 watchpoint_to_Z_packet (int type
)
4586 internal_error (__FILE__
, __LINE__
,
4587 "hw_bp_to_z: bad watchpoint type %d", type
);
4592 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
4594 struct remote_state
*rs
= get_remote_state ();
4595 char *buf
= alloca (rs
->remote_packet_size
);
4597 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
4599 if (remote_protocol_Z
[packet
].support
== PACKET_DISABLE
)
4600 error ("Can't set hardware watchpoints without the '%s' (%s) packet\n",
4601 remote_protocol_Z
[packet
].name
,
4602 remote_protocol_Z
[packet
].title
);
4604 sprintf (buf
, "Z%x,", packet
);
4605 p
= strchr (buf
, '\0');
4606 addr
= remote_address_masked (addr
);
4607 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4608 sprintf (p
, ",%x", len
);
4611 getpkt (buf
, (rs
->remote_packet_size
), 0);
4613 switch (packet_ok (buf
, &remote_protocol_Z
[packet
]))
4616 case PACKET_UNKNOWN
:
4621 internal_error (__FILE__
, __LINE__
,
4622 "remote_insert_watchpoint: reached end of function");
4627 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
4629 struct remote_state
*rs
= get_remote_state ();
4630 char *buf
= alloca (rs
->remote_packet_size
);
4632 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
4634 if (remote_protocol_Z
[packet
].support
== PACKET_DISABLE
)
4635 error ("Can't clear hardware watchpoints without the '%s' (%s) packet\n",
4636 remote_protocol_Z
[packet
].name
,
4637 remote_protocol_Z
[packet
].title
);
4639 sprintf (buf
, "z%x,", packet
);
4640 p
= strchr (buf
, '\0');
4641 addr
= remote_address_masked (addr
);
4642 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4643 sprintf (p
, ",%x", len
);
4645 getpkt (buf
, (rs
->remote_packet_size
), 0);
4647 switch (packet_ok (buf
, &remote_protocol_Z
[packet
]))
4650 case PACKET_UNKNOWN
:
4655 internal_error (__FILE__
, __LINE__
,
4656 "remote_remove_watchpoint: reached end of function");
4660 int remote_hw_watchpoint_limit
= -1;
4661 int remote_hw_breakpoint_limit
= -1;
4664 remote_check_watch_resources (int type
, int cnt
, int ot
)
4666 if (type
== bp_hardware_breakpoint
)
4668 if (remote_hw_breakpoint_limit
== 0)
4670 else if (remote_hw_breakpoint_limit
< 0)
4672 else if (cnt
<= remote_hw_breakpoint_limit
)
4677 if (remote_hw_watchpoint_limit
== 0)
4679 else if (remote_hw_watchpoint_limit
< 0)
4683 else if (cnt
<= remote_hw_watchpoint_limit
)
4690 remote_stopped_by_watchpoint (void)
4692 return remote_stopped_by_watchpoint_p
;
4695 extern int stepped_after_stopped_by_watchpoint
;
4698 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
4701 if (remote_stopped_by_watchpoint ()
4702 || stepped_after_stopped_by_watchpoint
)
4704 *addr_p
= remote_watch_data_address
;
4713 remote_insert_hw_breakpoint (CORE_ADDR addr
, char *shadow
)
4716 struct remote_state
*rs
= get_remote_state ();
4717 char *buf
= alloca (rs
->remote_packet_size
);
4720 /* The length field should be set to the size of a breakpoint
4723 BREAKPOINT_FROM_PC (&addr
, &len
);
4725 if (remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].support
== PACKET_DISABLE
)
4726 error ("Can't set hardware breakpoint without the '%s' (%s) packet\n",
4727 remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].name
,
4728 remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].title
);
4734 addr
= remote_address_masked (addr
);
4735 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4736 sprintf (p
, ",%x", len
);
4739 getpkt (buf
, (rs
->remote_packet_size
), 0);
4741 switch (packet_ok (buf
, &remote_protocol_Z
[Z_PACKET_HARDWARE_BP
]))
4744 case PACKET_UNKNOWN
:
4749 internal_error (__FILE__
, __LINE__
,
4750 "remote_insert_hw_breakpoint: reached end of function");
4755 remote_remove_hw_breakpoint (CORE_ADDR addr
, char *shadow
)
4758 struct remote_state
*rs
= get_remote_state ();
4759 char *buf
= alloca (rs
->remote_packet_size
);
4762 /* The length field should be set to the size of a breakpoint
4765 BREAKPOINT_FROM_PC (&addr
, &len
);
4767 if (remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].support
== PACKET_DISABLE
)
4768 error ("Can't clear hardware breakpoint without the '%s' (%s) packet\n",
4769 remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].name
,
4770 remote_protocol_Z
[Z_PACKET_HARDWARE_BP
].title
);
4776 addr
= remote_address_masked (addr
);
4777 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4778 sprintf (p
, ",%x", len
);
4781 getpkt (buf
, (rs
->remote_packet_size
), 0);
4783 switch (packet_ok (buf
, &remote_protocol_Z
[Z_PACKET_HARDWARE_BP
]))
4786 case PACKET_UNKNOWN
:
4791 internal_error (__FILE__
, __LINE__
,
4792 "remote_remove_hw_breakpoint: reached end of function");
4795 /* Some targets are only capable of doing downloads, and afterwards
4796 they switch to the remote serial protocol. This function provides
4797 a clean way to get from the download target to the remote target.
4798 It's basically just a wrapper so that we don't have to expose any
4799 of the internal workings of remote.c.
4801 Prior to calling this routine, you should shutdown the current
4802 target code, else you will get the "A program is being debugged
4803 already..." message. Usually a call to pop_target() suffices. */
4806 push_remote_target (char *name
, int from_tty
)
4808 printf_filtered ("Switching to remote protocol\n");
4809 remote_open (name
, from_tty
);
4812 /* Table used by the crc32 function to calcuate the checksum. */
4814 static unsigned long crc32_table
[256] =
4817 static unsigned long
4818 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
4820 if (!crc32_table
[1])
4822 /* Initialize the CRC table and the decoding table. */
4826 for (i
= 0; i
< 256; i
++)
4828 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
4829 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
4836 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
4842 /* compare-sections command
4844 With no arguments, compares each loadable section in the exec bfd
4845 with the same memory range on the target, and reports mismatches.
4846 Useful for verifying the image on the target against the exec file.
4847 Depends on the target understanding the new "qCRC:" request. */
4849 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4850 target method (target verify memory) and generic version of the
4851 actual command. This will allow other high-level code (especially
4852 generic_load()) to make use of this target functionality. */
4855 compare_sections_command (char *args
, int from_tty
)
4857 struct remote_state
*rs
= get_remote_state ();
4859 unsigned long host_crc
, target_crc
;
4860 extern bfd
*exec_bfd
;
4861 struct cleanup
*old_chain
;
4864 const char *sectname
;
4865 char *buf
= alloca (rs
->remote_packet_size
);
4872 error ("command cannot be used without an exec file");
4873 if (!current_target
.to_shortname
||
4874 strcmp (current_target
.to_shortname
, "remote") != 0)
4875 error ("command can only be used with remote target");
4877 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
4879 if (!(s
->flags
& SEC_LOAD
))
4880 continue; /* skip non-loadable section */
4882 size
= bfd_get_section_size (s
);
4884 continue; /* skip zero-length section */
4886 sectname
= bfd_get_section_name (exec_bfd
, s
);
4887 if (args
&& strcmp (args
, sectname
) != 0)
4888 continue; /* not the section selected by user */
4890 matched
= 1; /* do this section */
4892 /* FIXME: assumes lma can fit into long. */
4893 sprintf (buf
, "qCRC:%lx,%lx", (long) lma
, (long) size
);
4896 /* Be clever; compute the host_crc before waiting for target
4898 sectdata
= xmalloc (size
);
4899 old_chain
= make_cleanup (xfree
, sectdata
);
4900 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
4901 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
4903 getpkt (buf
, (rs
->remote_packet_size
), 0);
4905 error ("target memory fault, section %s, range 0x%s -- 0x%s",
4906 sectname
, paddr (lma
), paddr (lma
+ size
));
4908 error ("remote target does not support this operation");
4910 for (target_crc
= 0, tmp
= &buf
[1]; *tmp
; tmp
++)
4911 target_crc
= target_crc
* 16 + fromhex (*tmp
);
4913 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4914 sectname
, paddr (lma
), paddr (lma
+ size
));
4915 if (host_crc
== target_crc
)
4916 printf_filtered ("matched.\n");
4919 printf_filtered ("MIS-MATCHED!\n");
4923 do_cleanups (old_chain
);
4926 warning ("One or more sections of the remote executable does not match\n\
4927 the loaded file\n");
4928 if (args
&& !matched
)
4929 printf_filtered ("No loaded section named '%s'.\n", args
);
4933 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4934 const char *annex
, void *readbuf
, const void *writebuf
,
4935 ULONGEST offset
, LONGEST len
)
4937 struct remote_state
*rs
= get_remote_state ();
4939 char *buf2
= alloca (rs
->remote_packet_size
);
4940 char *p2
= &buf2
[0];
4943 /* Handle memory using remote_xfer_memory. */
4944 if (object
== TARGET_OBJECT_MEMORY
)
4949 if (writebuf
!= NULL
)
4951 void *buffer
= xmalloc (len
);
4952 struct cleanup
*cleanup
= make_cleanup (xfree
, buffer
);
4953 memcpy (buffer
, writebuf
, len
);
4954 xfered
= remote_xfer_memory (offset
, buffer
, len
, 1, NULL
, ops
);
4955 do_cleanups (cleanup
);
4958 xfered
= remote_xfer_memory (offset
, readbuf
, len
, 0, NULL
, ops
);
4962 else if (xfered
== 0 && errno
== 0)
4968 /* Only handle reads. */
4969 if (writebuf
!= NULL
|| readbuf
== NULL
)
4972 /* Map pre-existing objects onto letters. DO NOT do this for new
4973 objects!!! Instead specify new query packets. */
4976 case TARGET_OBJECT_KOD
:
4979 case TARGET_OBJECT_AVR
:
4983 case TARGET_OBJECT_AUXV
:
4984 if (remote_protocol_qPart_auxv
.support
!= PACKET_DISABLE
)
4986 unsigned int total
= 0;
4989 LONGEST n
= min ((rs
->remote_packet_size
- 2) / 2, len
);
4990 snprintf (buf2
, rs
->remote_packet_size
,
4991 "qPart:auxv:read::%s,%s",
4992 phex_nz (offset
, sizeof offset
),
4993 phex_nz (n
, sizeof n
));
4996 return total
> 0 ? total
: i
;
4998 getpkt (buf2
, rs
->remote_packet_size
, 0);
4999 if (packet_ok (buf2
, &remote_protocol_qPart_auxv
) != PACKET_OK
)
5000 return total
> 0 ? total
: -1;
5001 if (buf2
[0] == 'O' && buf2
[1] == 'K' && buf2
[2] == '\0')
5002 break; /* Got EOF indicator. */
5003 /* Got some data. */
5004 i
= hex2bin (buf2
, readbuf
, len
);
5007 readbuf
= (void *) ((char *) readbuf
+ i
);
5021 /* Note: a zero OFFSET and LEN can be used to query the minimum
5023 if (offset
== 0 && len
== 0)
5024 return (rs
->remote_packet_size
);
5025 /* Minimum outbuf size is (rs->remote_packet_size) - if bufsiz is
5026 not large enough let the caller. */
5027 if (len
< (rs
->remote_packet_size
))
5029 len
= rs
->remote_packet_size
;
5031 /* Except for querying the minimum buffer size, target must be open. */
5033 error ("remote query is only available after target open");
5035 gdb_assert (annex
!= NULL
);
5036 gdb_assert (readbuf
!= NULL
);
5041 /* We used one buffer char for the remote protocol q command and
5042 another for the query type. As the remote protocol encapsulation
5043 uses 4 chars plus one extra in case we are debugging
5044 (remote_debug), we have PBUFZIZ - 7 left to pack the query
5047 while (annex
[i
] && (i
< ((rs
->remote_packet_size
) - 8)))
5049 /* Bad caller may have sent forbidden characters. */
5050 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
5055 gdb_assert (annex
[i
] == '\0');
5061 getpkt (readbuf
, len
, 0);
5063 return strlen (readbuf
);
5067 remote_rcmd (char *command
,
5068 struct ui_file
*outbuf
)
5070 struct remote_state
*rs
= get_remote_state ();
5072 char *buf
= alloca (rs
->remote_packet_size
);
5076 error ("remote rcmd is only available after target open");
5078 /* Send a NULL command across as an empty command. */
5079 if (command
== NULL
)
5082 /* The query prefix. */
5083 strcpy (buf
, "qRcmd,");
5084 p
= strchr (buf
, '\0');
5086 if ((strlen (buf
) + strlen (command
) * 2 + 8/*misc*/) > (rs
->remote_packet_size
))
5087 error ("\"monitor\" command ``%s'' is too long\n", command
);
5089 /* Encode the actual command. */
5090 bin2hex (command
, p
, 0);
5092 if (putpkt (buf
) < 0)
5093 error ("Communication problem with target\n");
5095 /* get/display the response */
5098 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
5100 getpkt (buf
, (rs
->remote_packet_size
), 0);
5102 error ("Target does not support this command\n");
5103 if (buf
[0] == 'O' && buf
[1] != 'K')
5105 remote_console_output (buf
+ 1); /* 'O' message from stub. */
5108 if (strcmp (buf
, "OK") == 0)
5110 if (strlen (buf
) == 3 && buf
[0] == 'E'
5111 && isdigit (buf
[1]) && isdigit (buf
[2]))
5113 error ("Protocol error with Rcmd");
5115 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
5117 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
5118 fputc_unfiltered (c
, outbuf
);
5125 packet_command (char *args
, int from_tty
)
5127 struct remote_state
*rs
= get_remote_state ();
5128 char *buf
= alloca (rs
->remote_packet_size
);
5131 error ("command can only be used with remote target");
5134 error ("remote-packet command requires packet text as argument");
5136 puts_filtered ("sending: ");
5137 print_packet (args
);
5138 puts_filtered ("\n");
5141 getpkt (buf
, (rs
->remote_packet_size
), 0);
5142 puts_filtered ("received: ");
5144 puts_filtered ("\n");
5148 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
5150 static void display_thread_info (struct gdb_ext_thread_info
*info
);
5152 static void threadset_test_cmd (char *cmd
, int tty
);
5154 static void threadalive_test (char *cmd
, int tty
);
5156 static void threadlist_test_cmd (char *cmd
, int tty
);
5158 int get_and_display_threadinfo (threadref
*ref
);
5160 static void threadinfo_test_cmd (char *cmd
, int tty
);
5162 static int thread_display_step (threadref
*ref
, void *context
);
5164 static void threadlist_update_test_cmd (char *cmd
, int tty
);
5166 static void init_remote_threadtests (void);
5168 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
5171 threadset_test_cmd (char *cmd
, int tty
)
5173 int sample_thread
= SAMPLE_THREAD
;
5175 printf_filtered ("Remote threadset test\n");
5176 set_thread (sample_thread
, 1);
5181 threadalive_test (char *cmd
, int tty
)
5183 int sample_thread
= SAMPLE_THREAD
;
5185 if (remote_thread_alive (pid_to_ptid (sample_thread
)))
5186 printf_filtered ("PASS: Thread alive test\n");
5188 printf_filtered ("FAIL: Thread alive test\n");
5191 void output_threadid (char *title
, threadref
*ref
);
5194 output_threadid (char *title
, threadref
*ref
)
5198 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
5200 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
5204 threadlist_test_cmd (char *cmd
, int tty
)
5207 threadref nextthread
;
5208 int done
, result_count
;
5209 threadref threadlist
[3];
5211 printf_filtered ("Remote Threadlist test\n");
5212 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
5213 &result_count
, &threadlist
[0]))
5214 printf_filtered ("FAIL: threadlist test\n");
5217 threadref
*scan
= threadlist
;
5218 threadref
*limit
= scan
+ result_count
;
5220 while (scan
< limit
)
5221 output_threadid (" thread ", scan
++);
5226 display_thread_info (struct gdb_ext_thread_info
*info
)
5228 output_threadid ("Threadid: ", &info
->threadid
);
5229 printf_filtered ("Name: %s\n ", info
->shortname
);
5230 printf_filtered ("State: %s\n", info
->display
);
5231 printf_filtered ("other: %s\n\n", info
->more_display
);
5235 get_and_display_threadinfo (threadref
*ref
)
5239 struct gdb_ext_thread_info threadinfo
;
5241 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
5242 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
5243 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
5244 display_thread_info (&threadinfo
);
5249 threadinfo_test_cmd (char *cmd
, int tty
)
5251 int athread
= SAMPLE_THREAD
;
5255 int_to_threadref (&thread
, athread
);
5256 printf_filtered ("Remote Threadinfo test\n");
5257 if (!get_and_display_threadinfo (&thread
))
5258 printf_filtered ("FAIL cannot get thread info\n");
5262 thread_display_step (threadref
*ref
, void *context
)
5264 /* output_threadid(" threadstep ",ref); *//* simple test */
5265 return get_and_display_threadinfo (ref
);
5269 threadlist_update_test_cmd (char *cmd
, int tty
)
5271 printf_filtered ("Remote Threadlist update test\n");
5272 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
5276 init_remote_threadtests (void)
5278 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
5279 "Fetch and print the remote list of thread identifiers, one pkt only");
5280 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
5281 "Fetch and display info about one thread");
5282 add_com ("tset", class_obscure
, threadset_test_cmd
,
5283 "Test setting to a different thread");
5284 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
5285 "Iterate through updating all remote thread info");
5286 add_com ("talive", class_obscure
, threadalive_test
,
5287 " Remote thread alive test ");
5292 /* Convert a thread ID to a string. Returns the string in a static
5296 remote_pid_to_str (ptid_t ptid
)
5298 static char buf
[30];
5300 sprintf (buf
, "Thread %d", PIDGET (ptid
));
5305 init_remote_ops (void)
5307 remote_ops
.to_shortname
= "remote";
5308 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
5310 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5311 Specify the serial device it is connected to\n\
5312 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
5313 remote_ops
.to_open
= remote_open
;
5314 remote_ops
.to_close
= remote_close
;
5315 remote_ops
.to_detach
= remote_detach
;
5316 remote_ops
.to_disconnect
= remote_disconnect
;
5317 remote_ops
.to_resume
= remote_resume
;
5318 remote_ops
.to_wait
= remote_wait
;
5319 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
5320 remote_ops
.to_store_registers
= remote_store_registers
;
5321 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5322 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
5323 remote_ops
.to_files_info
= remote_files_info
;
5324 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5325 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5326 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
5327 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
5328 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
5329 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
5330 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
5331 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
5332 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
5333 remote_ops
.to_kill
= remote_kill
;
5334 remote_ops
.to_load
= generic_load
;
5335 remote_ops
.to_mourn_inferior
= remote_mourn
;
5336 remote_ops
.to_thread_alive
= remote_thread_alive
;
5337 remote_ops
.to_find_new_threads
= remote_threads_info
;
5338 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
5339 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
5340 remote_ops
.to_stop
= remote_stop
;
5341 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
5342 remote_ops
.to_rcmd
= remote_rcmd
;
5343 remote_ops
.to_stratum
= process_stratum
;
5344 remote_ops
.to_has_all_memory
= 1;
5345 remote_ops
.to_has_memory
= 1;
5346 remote_ops
.to_has_stack
= 1;
5347 remote_ops
.to_has_registers
= 1;
5348 remote_ops
.to_has_execution
= 1;
5349 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5350 remote_ops
.to_magic
= OPS_MAGIC
;
5353 /* Set up the extended remote vector by making a copy of the standard
5354 remote vector and adding to it. */
5357 init_extended_remote_ops (void)
5359 extended_remote_ops
= remote_ops
;
5361 extended_remote_ops
.to_shortname
= "extended-remote";
5362 extended_remote_ops
.to_longname
=
5363 "Extended remote serial target in gdb-specific protocol";
5364 extended_remote_ops
.to_doc
=
5365 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5366 Specify the serial device it is connected to (e.g. /dev/ttya).",
5367 extended_remote_ops
.to_open
= extended_remote_open
;
5368 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
5369 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5373 remote_can_async_p (void)
5375 /* We're async whenever the serial device is. */
5376 return (current_target
.to_async_mask_value
) && serial_can_async_p (remote_desc
);
5380 remote_is_async_p (void)
5382 /* We're async whenever the serial device is. */
5383 return (current_target
.to_async_mask_value
) && serial_is_async_p (remote_desc
);
5386 /* Pass the SERIAL event on and up to the client. One day this code
5387 will be able to delay notifying the client of an event until the
5388 point where an entire packet has been received. */
5390 static void (*async_client_callback
) (enum inferior_event_type event_type
,
5392 static void *async_client_context
;
5393 static serial_event_ftype remote_async_serial_handler
;
5396 remote_async_serial_handler (struct serial
*scb
, void *context
)
5398 /* Don't propogate error information up to the client. Instead let
5399 the client find out about the error by querying the target. */
5400 async_client_callback (INF_REG_EVENT
, async_client_context
);
5404 remote_async (void (*callback
) (enum inferior_event_type event_type
,
5405 void *context
), void *context
)
5407 if (current_target
.to_async_mask_value
== 0)
5408 internal_error (__FILE__
, __LINE__
,
5409 "Calling remote_async when async is masked");
5411 if (callback
!= NULL
)
5413 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
5414 async_client_callback
= callback
;
5415 async_client_context
= context
;
5418 serial_async (remote_desc
, NULL
, NULL
);
5421 /* Target async and target extended-async.
5423 This are temporary targets, until it is all tested. Eventually
5424 async support will be incorporated int the usual 'remote'
5428 init_remote_async_ops (void)
5430 remote_async_ops
.to_shortname
= "async";
5431 remote_async_ops
.to_longname
=
5432 "Remote serial target in async version of the gdb-specific protocol";
5433 remote_async_ops
.to_doc
=
5434 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5435 Specify the serial device it is connected to (e.g. /dev/ttya).";
5436 remote_async_ops
.to_open
= remote_async_open
;
5437 remote_async_ops
.to_close
= remote_close
;
5438 remote_async_ops
.to_detach
= remote_detach
;
5439 remote_async_ops
.to_disconnect
= remote_disconnect
;
5440 remote_async_ops
.to_resume
= remote_async_resume
;
5441 remote_async_ops
.to_wait
= remote_async_wait
;
5442 remote_async_ops
.to_fetch_registers
= remote_fetch_registers
;
5443 remote_async_ops
.to_store_registers
= remote_store_registers
;
5444 remote_async_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5445 remote_async_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
5446 remote_async_ops
.to_files_info
= remote_files_info
;
5447 remote_async_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5448 remote_async_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5449 remote_async_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
5450 remote_async_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
5451 remote_async_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
5452 remote_async_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
5453 remote_async_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
5454 remote_async_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
5455 remote_async_ops
.to_stopped_data_address
= remote_stopped_data_address
;
5456 remote_async_ops
.to_terminal_inferior
= remote_async_terminal_inferior
;
5457 remote_async_ops
.to_terminal_ours
= remote_async_terminal_ours
;
5458 remote_async_ops
.to_kill
= remote_async_kill
;
5459 remote_async_ops
.to_load
= generic_load
;
5460 remote_async_ops
.to_mourn_inferior
= remote_async_mourn
;
5461 remote_async_ops
.to_thread_alive
= remote_thread_alive
;
5462 remote_async_ops
.to_find_new_threads
= remote_threads_info
;
5463 remote_async_ops
.to_pid_to_str
= remote_pid_to_str
;
5464 remote_async_ops
.to_extra_thread_info
= remote_threads_extra_info
;
5465 remote_async_ops
.to_stop
= remote_stop
;
5466 remote_async_ops
.to_xfer_partial
= remote_xfer_partial
;
5467 remote_async_ops
.to_rcmd
= remote_rcmd
;
5468 remote_async_ops
.to_stratum
= process_stratum
;
5469 remote_async_ops
.to_has_all_memory
= 1;
5470 remote_async_ops
.to_has_memory
= 1;
5471 remote_async_ops
.to_has_stack
= 1;
5472 remote_async_ops
.to_has_registers
= 1;
5473 remote_async_ops
.to_has_execution
= 1;
5474 remote_async_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5475 remote_async_ops
.to_can_async_p
= remote_can_async_p
;
5476 remote_async_ops
.to_is_async_p
= remote_is_async_p
;
5477 remote_async_ops
.to_async
= remote_async
;
5478 remote_async_ops
.to_async_mask_value
= 1;
5479 remote_async_ops
.to_magic
= OPS_MAGIC
;
5482 /* Set up the async extended remote vector by making a copy of the standard
5483 remote vector and adding to it. */
5486 init_extended_async_remote_ops (void)
5488 extended_async_remote_ops
= remote_async_ops
;
5490 extended_async_remote_ops
.to_shortname
= "extended-async";
5491 extended_async_remote_ops
.to_longname
=
5492 "Extended remote serial target in async gdb-specific protocol";
5493 extended_async_remote_ops
.to_doc
=
5494 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5495 Specify the serial device it is connected to (e.g. /dev/ttya).",
5496 extended_async_remote_ops
.to_open
= extended_remote_async_open
;
5497 extended_async_remote_ops
.to_create_inferior
= extended_remote_async_create_inferior
;
5498 extended_async_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5502 set_remote_cmd (char *args
, int from_tty
)
5507 show_remote_cmd (char *args
, int from_tty
)
5509 /* FIXME: cagney/2002-06-15: This function should iterate over
5510 remote_show_cmdlist for a list of sub commands to show. */
5511 show_remote_protocol_Z_packet_cmd (args
, from_tty
, NULL
);
5512 show_remote_protocol_P_packet_cmd (args
, from_tty
, NULL
);
5513 show_remote_protocol_p_packet_cmd (args
, from_tty
, NULL
);
5514 show_remote_protocol_qSymbol_packet_cmd (args
, from_tty
, NULL
);
5515 show_remote_protocol_vcont_packet_cmd (args
, from_tty
, NULL
);
5516 show_remote_protocol_binary_download_cmd (args
, from_tty
, NULL
);
5517 show_remote_protocol_qPart_auxv_packet_cmd (args
, from_tty
, NULL
);
5521 build_remote_gdbarch_data (void)
5523 remote_address_size
= TARGET_ADDR_BIT
;
5526 /* Saved pointer to previous owner of the new_objfile event. */
5527 static void (*remote_new_objfile_chain
) (struct objfile
*);
5529 /* Function to be called whenever a new objfile (shlib) is detected. */
5531 remote_new_objfile (struct objfile
*objfile
)
5533 if (remote_desc
!= 0) /* Have a remote connection. */
5535 remote_check_symbols (objfile
);
5537 /* Call predecessor on chain, if any. */
5538 if (remote_new_objfile_chain
!= 0 &&
5540 remote_new_objfile_chain (objfile
);
5544 _initialize_remote (void)
5546 static struct cmd_list_element
*remote_set_cmdlist
;
5547 static struct cmd_list_element
*remote_show_cmdlist
;
5548 struct cmd_list_element
*tmpcmd
;
5550 /* architecture specific data */
5551 remote_gdbarch_data_handle
=
5552 gdbarch_data_register_post_init (init_remote_state
);
5554 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5555 that the remote protocol has been initialized. */
5556 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size
);
5557 deprecated_register_gdbarch_swap (NULL
, 0, build_remote_gdbarch_data
);
5560 add_target (&remote_ops
);
5562 init_extended_remote_ops ();
5563 add_target (&extended_remote_ops
);
5565 init_remote_async_ops ();
5566 add_target (&remote_async_ops
);
5568 init_extended_async_remote_ops ();
5569 add_target (&extended_async_remote_ops
);
5571 /* Hook into new objfile notification. */
5572 remote_new_objfile_chain
= deprecated_target_new_objfile_hook
;
5573 deprecated_target_new_objfile_hook
= remote_new_objfile
;
5576 init_remote_threadtests ();
5579 /* set/show remote ... */
5581 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, "\
5582 Remote protocol specific variables\n\
5583 Configure various remote-protocol specific variables such as\n\
5584 the packets being used",
5585 &remote_set_cmdlist
, "set remote ",
5586 0 /* allow-unknown */, &setlist
);
5587 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, "\
5588 Remote protocol specific variables\n\
5589 Configure various remote-protocol specific variables such as\n\
5590 the packets being used",
5591 &remote_show_cmdlist
, "show remote ",
5592 0 /* allow-unknown */, &showlist
);
5594 add_cmd ("compare-sections", class_obscure
, compare_sections_command
,
5595 "Compare section data on target to the exec file.\n\
5596 Argument is a single section name (default: all loaded sections).",
5599 add_cmd ("packet", class_maintenance
, packet_command
,
5600 "Send an arbitrary packet to a remote target.\n\
5601 maintenance packet TEXT\n\
5602 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5603 this command sends the string TEXT to the inferior, and displays the\n\
5604 response packet. GDB supplies the initial `$' character, and the\n\
5605 terminating `#' character and checksum.",
5608 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, "\
5609 Set whether to send break if interrupted.", "\
5610 Show whether to send break if interrupted.", "\
5611 If set, a break, instead of a cntrl-c, is sent to the remote target.", "\
5612 Whether to send break if interrupted is %s.",
5614 &setlist
, &showlist
);
5616 /* Install commands for configuring memory read/write packets. */
5618 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
,
5619 "Set the maximum number of bytes per memory write packet (deprecated).\n",
5621 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
,
5622 "Show the maximum number of bytes per memory write packet (deprecated).\n",
5624 add_cmd ("memory-write-packet-size", no_class
,
5625 set_memory_write_packet_size
,
5626 "Set the maximum number of bytes per memory-write packet.\n"
5627 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5628 "default packet size. The actual limit is further reduced\n"
5629 "dependent on the target. Specify ``fixed'' to disable the\n"
5630 "further restriction and ``limit'' to enable that restriction\n",
5631 &remote_set_cmdlist
);
5632 add_cmd ("memory-read-packet-size", no_class
,
5633 set_memory_read_packet_size
,
5634 "Set the maximum number of bytes per memory-read packet.\n"
5635 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5636 "default packet size. The actual limit is further reduced\n"
5637 "dependent on the target. Specify ``fixed'' to disable the\n"
5638 "further restriction and ``limit'' to enable that restriction\n",
5639 &remote_set_cmdlist
);
5640 add_cmd ("memory-write-packet-size", no_class
,
5641 show_memory_write_packet_size
,
5642 "Show the maximum number of bytes per memory-write packet.\n",
5643 &remote_show_cmdlist
);
5644 add_cmd ("memory-read-packet-size", no_class
,
5645 show_memory_read_packet_size
,
5646 "Show the maximum number of bytes per memory-read packet.\n",
5647 &remote_show_cmdlist
);
5649 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
5650 &remote_hw_watchpoint_limit
, "\
5651 Set the maximum number of target hardware watchpoints.", "\
5652 Show the maximum number of target hardware watchpoints.", "\
5653 Specify a negative limit for unlimited.", "\
5654 The maximum number of target hardware watchpoints is %s.",
5656 &remote_set_cmdlist
, &remote_show_cmdlist
);
5657 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
5658 &remote_hw_breakpoint_limit
, "\
5659 Set the maximum number of target hardware breakpoints.", "\
5660 Show the maximum number of target hardware breakpoints.", "\
5661 Specify a negative limit for unlimited.", "\
5662 The maximum number of target hardware breakpoints is %s.",
5664 &remote_set_cmdlist
, &remote_show_cmdlist
);
5666 deprecated_add_show_from_set
5667 (add_set_cmd ("remoteaddresssize", class_obscure
,
5668 var_integer
, (char *) &remote_address_size
,
5669 "Set the maximum size of the address (in bits) \
5670 in a memory packet.\n",
5674 add_packet_config_cmd (&remote_protocol_binary_download
,
5675 "X", "binary-download",
5676 set_remote_protocol_binary_download_cmd
,
5677 show_remote_protocol_binary_download_cmd
,
5678 &remote_set_cmdlist
, &remote_show_cmdlist
,
5681 /* XXXX - should ``set remotebinarydownload'' be retained for
5683 deprecated_add_show_from_set
5684 (add_set_cmd ("remotebinarydownload", no_class
,
5685 var_boolean
, (char *) &remote_binary_download
,
5686 "Set binary downloads.\n", &setlist
),
5690 add_packet_config_cmd (&remote_protocol_vcont
,
5691 "vCont", "verbose-resume",
5692 set_remote_protocol_vcont_packet_cmd
,
5693 show_remote_protocol_vcont_packet_cmd
,
5694 &remote_set_cmdlist
, &remote_show_cmdlist
,
5697 add_packet_config_cmd (&remote_protocol_qSymbol
,
5698 "qSymbol", "symbol-lookup",
5699 set_remote_protocol_qSymbol_packet_cmd
,
5700 show_remote_protocol_qSymbol_packet_cmd
,
5701 &remote_set_cmdlist
, &remote_show_cmdlist
,
5704 add_packet_config_cmd (&remote_protocol_P
,
5705 "P", "set-register",
5706 set_remote_protocol_P_packet_cmd
,
5707 show_remote_protocol_P_packet_cmd
,
5708 &remote_set_cmdlist
, &remote_show_cmdlist
,
5711 add_packet_config_cmd (&remote_protocol_p
,
5712 "p", "fetch-register",
5713 set_remote_protocol_p_packet_cmd
,
5714 show_remote_protocol_p_packet_cmd
,
5715 &remote_set_cmdlist
, &remote_show_cmdlist
,
5718 add_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_SOFTWARE_BP
],
5719 "Z0", "software-breakpoint",
5720 set_remote_protocol_Z_software_bp_packet_cmd
,
5721 show_remote_protocol_Z_software_bp_packet_cmd
,
5722 &remote_set_cmdlist
, &remote_show_cmdlist
,
5725 add_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_HARDWARE_BP
],
5726 "Z1", "hardware-breakpoint",
5727 set_remote_protocol_Z_hardware_bp_packet_cmd
,
5728 show_remote_protocol_Z_hardware_bp_packet_cmd
,
5729 &remote_set_cmdlist
, &remote_show_cmdlist
,
5732 add_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_WRITE_WP
],
5733 "Z2", "write-watchpoint",
5734 set_remote_protocol_Z_write_wp_packet_cmd
,
5735 show_remote_protocol_Z_write_wp_packet_cmd
,
5736 &remote_set_cmdlist
, &remote_show_cmdlist
,
5739 add_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_READ_WP
],
5740 "Z3", "read-watchpoint",
5741 set_remote_protocol_Z_read_wp_packet_cmd
,
5742 show_remote_protocol_Z_read_wp_packet_cmd
,
5743 &remote_set_cmdlist
, &remote_show_cmdlist
,
5746 add_packet_config_cmd (&remote_protocol_Z
[Z_PACKET_ACCESS_WP
],
5747 "Z4", "access-watchpoint",
5748 set_remote_protocol_Z_access_wp_packet_cmd
,
5749 show_remote_protocol_Z_access_wp_packet_cmd
,
5750 &remote_set_cmdlist
, &remote_show_cmdlist
,
5753 add_packet_config_cmd (&remote_protocol_qPart_auxv
,
5754 "qPart_auxv", "read-aux-vector",
5755 set_remote_protocol_qPart_auxv_packet_cmd
,
5756 show_remote_protocol_qPart_auxv_packet_cmd
,
5757 &remote_set_cmdlist
, &remote_show_cmdlist
,
5760 /* Keep the old ``set remote Z-packet ...'' working. */
5761 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
5762 &remote_Z_packet_detect
, "\
5763 Set use of remote protocol `Z' packets", "\
5764 Show use of remote protocol `Z' packets ", "\
5765 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
5767 Use of remote protocol `Z' packets is %s",
5768 set_remote_protocol_Z_packet_cmd
,
5769 show_remote_protocol_Z_packet_cmd
,
5770 &remote_set_cmdlist
, &remote_show_cmdlist
);
5772 /* Eventually initialize fileio. See fileio.c */
5773 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);