1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* See the GDB User Guide for details of the GDB remote protocol. */
27 #include "gdb_string.h"
33 #include "exceptions.h"
35 /*#include "terminal.h" */
38 #include "gdb-stabs.h"
39 #include "gdbthread.h"
43 #include "gdb_assert.h"
46 #include "cli/cli-decode.h"
47 #include "cli/cli-setshow.h"
52 #include "event-loop.h"
53 #include "event-top.h"
59 #include "gdbcore.h" /* for exec_bfd */
61 #include "remote-fileio.h"
63 /* Prototypes for local functions. */
64 static void cleanup_sigint_signal_handler (void *dummy
);
65 static void initialize_sigint_signal_handler (void);
66 static int getpkt_sane (char *buf
, long sizeof_buf
, int forever
);
68 static void handle_remote_sigint (int);
69 static void handle_remote_sigint_twice (int);
70 static void async_remote_interrupt (gdb_client_data
);
71 void async_remote_interrupt_twice (gdb_client_data
);
73 static void build_remote_gdbarch_data (void);
75 static void remote_files_info (struct target_ops
*ignore
);
77 static void remote_prepare_to_store (void);
79 static void remote_fetch_registers (int regno
);
81 static void remote_resume (ptid_t ptid
, int step
,
82 enum target_signal siggnal
);
83 static void remote_async_resume (ptid_t ptid
, int step
,
84 enum target_signal siggnal
);
85 static void remote_open (char *name
, int from_tty
);
86 static void remote_async_open (char *name
, int from_tty
);
88 static void extended_remote_open (char *name
, int from_tty
);
89 static void extended_remote_async_open (char *name
, int from_tty
);
91 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
,
94 static void remote_close (int quitting
);
96 static void remote_store_registers (int regno
);
98 static void remote_mourn (void);
99 static void remote_async_mourn (void);
101 static void extended_remote_restart (void);
103 static void extended_remote_mourn (void);
105 static void remote_mourn_1 (struct target_ops
*);
107 static void remote_send (char *buf
, long sizeof_buf
);
109 static int readchar (int timeout
);
111 static ptid_t
remote_wait (ptid_t ptid
,
112 struct target_waitstatus
*status
);
113 static ptid_t
remote_async_wait (ptid_t ptid
,
114 struct target_waitstatus
*status
);
116 static void remote_kill (void);
117 static void remote_async_kill (void);
119 static int tohex (int nib
);
121 static void remote_detach (char *args
, int from_tty
);
123 static void remote_interrupt (int signo
);
125 static void remote_interrupt_twice (int signo
);
127 static void interrupt_query (void);
129 static void set_thread (int, int);
131 static int remote_thread_alive (ptid_t
);
133 static void get_offsets (void);
135 static long read_frame (char *buf
, long sizeof_buf
);
137 static int remote_insert_breakpoint (CORE_ADDR
, bfd_byte
*);
139 static int remote_remove_breakpoint (CORE_ADDR
, bfd_byte
*);
141 static int hexnumlen (ULONGEST num
);
143 static void init_remote_ops (void);
145 static void init_extended_remote_ops (void);
147 static void remote_stop (void);
149 static int ishex (int ch
, int *val
);
151 static int stubhex (int ch
);
153 static int hexnumstr (char *, ULONGEST
);
155 static int hexnumnstr (char *, ULONGEST
, int);
157 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
159 static void print_packet (char *);
161 static unsigned long crc32 (unsigned char *, int, unsigned int);
163 static void compare_sections_command (char *, int);
165 static void packet_command (char *, int);
167 static int stub_unpack_int (char *buff
, int fieldlength
);
169 static ptid_t
remote_current_thread (ptid_t oldptid
);
171 static void remote_find_new_threads (void);
173 static void record_currthread (int currthread
);
175 static int fromhex (int a
);
177 static int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
179 static int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
181 static int putpkt_binary (char *buf
, int cnt
);
183 static void check_binary_download (CORE_ADDR addr
);
185 struct packet_config
;
187 static void show_packet_config_cmd (struct packet_config
*config
);
189 static void update_packet_config (struct packet_config
*config
);
191 void _initialize_remote (void);
193 /* Description of the remote protocol. Strictly speaking, when the
194 target is open()ed, remote.c should create a per-target description
195 of the remote protocol using that target's architecture.
196 Unfortunately, the target stack doesn't include local state. For
197 the moment keep the information in the target's architecture
202 long offset
; /* Offset into G packet. */
203 long regnum
; /* GDB's internal register number. */
204 LONGEST pnum
; /* Remote protocol register number. */
205 int in_g_packet
; /* Always part of G packet. */
206 /* long size in bytes; == register_size (current_gdbarch, regnum);
208 /* char *name; == REGISTER_NAME (regnum); at present. */
213 /* Description of the remote protocol registers. */
214 long sizeof_g_packet
;
216 /* Description of the remote protocol registers indexed by REGNUM
217 (making an array of NUM_REGS + NUM_PSEUDO_REGS in size). */
218 struct packet_reg
*regs
;
220 /* This is the size (in chars) of the first response to the ``g''
221 packet. It is used as a heuristic when determining the maximum
222 size of memory-read and memory-write packets. A target will
223 typically only reserve a buffer large enough to hold the ``g''
224 packet. The size does not include packet overhead (headers and
226 long actual_register_packet_size
;
228 /* This is the maximum size (in chars) of a non read/write packet.
229 It is also used as a cap on the size of read/write packets. */
230 long remote_packet_size
;
234 /* Handle for retreving the remote protocol data from gdbarch. */
235 static struct gdbarch_data
*remote_gdbarch_data_handle
;
237 static struct remote_state
*
238 get_remote_state (void)
240 return gdbarch_data (current_gdbarch
, remote_gdbarch_data_handle
);
244 init_remote_state (struct gdbarch
*gdbarch
)
247 struct remote_state
*rs
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_state
);
249 rs
->sizeof_g_packet
= 0;
251 /* Assume a 1:1 regnum<->pnum table. */
252 rs
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
, NUM_REGS
+ NUM_PSEUDO_REGS
,
254 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
256 struct packet_reg
*r
= &rs
->regs
[regnum
];
259 r
->offset
= DEPRECATED_REGISTER_BYTE (regnum
);
260 r
->in_g_packet
= (regnum
< NUM_REGS
);
261 /* ...name = REGISTER_NAME (regnum); */
263 /* Compute packet size by accumulating the size of all registers. */
264 if (regnum
< NUM_REGS
)
265 rs
->sizeof_g_packet
+= register_size (current_gdbarch
, regnum
);
268 /* Default maximum number of characters in a packet body. Many
269 remote stubs have a hardwired buffer size of 400 bytes
270 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
271 as the maximum packet-size to ensure that the packet and an extra
272 NUL character can always fit in the buffer. This stops GDB
273 trashing stubs that try to squeeze an extra NUL into what is
274 already a full buffer (As of 1999-12-04 that was most stubs. */
275 rs
->remote_packet_size
= 400 - 1;
277 /* Should rs->sizeof_g_packet needs more space than the
278 default, adjust the size accordingly. Remember that each byte is
279 encoded as two characters. 32 is the overhead for the packet
280 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
281 (``$NN:G...#NN'') is a better guess, the below has been padded a
283 if (rs
->sizeof_g_packet
> ((rs
->remote_packet_size
- 32) / 2))
284 rs
->remote_packet_size
= (rs
->sizeof_g_packet
* 2 + 32);
286 /* This one is filled in when a ``g'' packet is received. */
287 rs
->actual_register_packet_size
= 0;
292 static struct packet_reg
*
293 packet_reg_from_regnum (struct remote_state
*rs
, long regnum
)
295 if (regnum
< 0 && regnum
>= NUM_REGS
+ NUM_PSEUDO_REGS
)
299 struct packet_reg
*r
= &rs
->regs
[regnum
];
300 gdb_assert (r
->regnum
== regnum
);
305 static struct packet_reg
*
306 packet_reg_from_pnum (struct remote_state
*rs
, LONGEST pnum
)
309 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
311 struct packet_reg
*r
= &rs
->regs
[i
];
318 /* FIXME: graces/2002-08-08: These variables should eventually be
319 bound to an instance of the target object (as in gdbarch-tdep()),
320 when such a thing exists. */
322 /* This is set to the data address of the access causing the target
323 to stop for a watchpoint. */
324 static CORE_ADDR remote_watch_data_address
;
326 /* This is non-zero if taregt stopped for a watchpoint. */
327 static int remote_stopped_by_watchpoint_p
;
330 static struct target_ops remote_ops
;
332 static struct target_ops extended_remote_ops
;
334 /* Temporary target ops. Just like the remote_ops and
335 extended_remote_ops, but with asynchronous support. */
336 static struct target_ops remote_async_ops
;
338 static struct target_ops extended_async_remote_ops
;
340 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
341 ``forever'' still use the normal timeout mechanism. This is
342 currently used by the ASYNC code to guarentee that target reads
343 during the initial connect always time-out. Once getpkt has been
344 modified to return a timeout indication and, in turn
345 remote_wait()/wait_for_inferior() have gained a timeout parameter
347 static int wait_forever_enabled_p
= 1;
350 /* This variable chooses whether to send a ^C or a break when the user
351 requests program interruption. Although ^C is usually what remote
352 systems expect, and that is the default here, sometimes a break is
353 preferable instead. */
355 static int remote_break
;
357 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
358 remote_open knows that we don't have a file open when the program
360 static struct serial
*remote_desc
= NULL
;
362 /* This variable sets the number of bits in an address that are to be
363 sent in a memory ("M" or "m") packet. Normally, after stripping
364 leading zeros, the entire address would be sent. This variable
365 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
366 initial implementation of remote.c restricted the address sent in
367 memory packets to ``host::sizeof long'' bytes - (typically 32
368 bits). Consequently, for 64 bit targets, the upper 32 bits of an
369 address was never sent. Since fixing this bug may cause a break in
370 some remote targets this variable is principly provided to
371 facilitate backward compatibility. */
373 static int remote_address_size
;
375 /* Tempoary to track who currently owns the terminal. See
376 target_async_terminal_* for more details. */
378 static int remote_async_terminal_ours_p
;
381 /* User configurable variables for the number of characters in a
382 memory read/write packet. MIN (rs->remote_packet_size,
383 rs->sizeof_g_packet) is the default. Some targets need smaller
384 values (fifo overruns, et.al.) and some users need larger values
385 (speed up transfers). The variables ``preferred_*'' (the user
386 request), ``current_*'' (what was actually set) and ``forced_*''
387 (Positive - a soft limit, negative - a hard limit). */
389 struct memory_packet_config
396 /* Compute the current size of a read/write packet. Since this makes
397 use of ``actual_register_packet_size'' the computation is dynamic. */
400 get_memory_packet_size (struct memory_packet_config
*config
)
402 struct remote_state
*rs
= get_remote_state ();
403 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
404 law?) that some hosts don't cope very well with large alloca()
405 calls. Eventually the alloca() code will be replaced by calls to
406 xmalloc() and make_cleanups() allowing this restriction to either
407 be lifted or removed. */
408 #ifndef MAX_REMOTE_PACKET_SIZE
409 #define MAX_REMOTE_PACKET_SIZE 16384
411 /* NOTE: 20 ensures we can write at least one byte. */
412 #ifndef MIN_REMOTE_PACKET_SIZE
413 #define MIN_REMOTE_PACKET_SIZE 20
418 if (config
->size
<= 0)
419 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
421 what_they_get
= config
->size
;
425 what_they_get
= rs
->remote_packet_size
;
426 /* Limit the packet to the size specified by the user. */
428 && what_they_get
> config
->size
)
429 what_they_get
= config
->size
;
430 /* Limit it to the size of the targets ``g'' response. */
431 if ((rs
->actual_register_packet_size
) > 0
432 && what_they_get
> (rs
->actual_register_packet_size
))
433 what_they_get
= (rs
->actual_register_packet_size
);
435 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
436 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
437 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
438 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
439 return what_they_get
;
442 /* Update the size of a read/write packet. If they user wants
443 something really big then do a sanity check. */
446 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
448 int fixed_p
= config
->fixed_p
;
449 long size
= config
->size
;
451 error (_("Argument required (integer, `fixed' or `limited')."));
452 else if (strcmp (args
, "hard") == 0
453 || strcmp (args
, "fixed") == 0)
455 else if (strcmp (args
, "soft") == 0
456 || strcmp (args
, "limit") == 0)
461 size
= strtoul (args
, &end
, 0);
463 error (_("Invalid %s (bad syntax)."), config
->name
);
465 /* Instead of explicitly capping the size of a packet to
466 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
467 instead allowed to set the size to something arbitrarily
469 if (size
> MAX_REMOTE_PACKET_SIZE
)
470 error (_("Invalid %s (too large)."), config
->name
);
474 if (fixed_p
&& !config
->fixed_p
)
476 if (! query (_("The target may not be able to correctly handle a %s\n"
477 "of %ld bytes. Change the packet size? "),
479 error (_("Packet size not changed."));
481 /* Update the config. */
482 config
->fixed_p
= fixed_p
;
487 show_memory_packet_size (struct memory_packet_config
*config
)
489 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
491 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
492 get_memory_packet_size (config
));
494 printf_filtered (_("Packets are limited to %ld bytes.\n"),
495 get_memory_packet_size (config
));
498 static struct memory_packet_config memory_write_packet_config
=
500 "memory-write-packet-size",
504 set_memory_write_packet_size (char *args
, int from_tty
)
506 set_memory_packet_size (args
, &memory_write_packet_config
);
510 show_memory_write_packet_size (char *args
, int from_tty
)
512 show_memory_packet_size (&memory_write_packet_config
);
516 get_memory_write_packet_size (void)
518 return get_memory_packet_size (&memory_write_packet_config
);
521 static struct memory_packet_config memory_read_packet_config
=
523 "memory-read-packet-size",
527 set_memory_read_packet_size (char *args
, int from_tty
)
529 set_memory_packet_size (args
, &memory_read_packet_config
);
533 show_memory_read_packet_size (char *args
, int from_tty
)
535 show_memory_packet_size (&memory_read_packet_config
);
539 get_memory_read_packet_size (void)
541 struct remote_state
*rs
= get_remote_state ();
542 long size
= get_memory_packet_size (&memory_read_packet_config
);
543 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
544 extra buffer size argument before the memory read size can be
545 increased beyond RS->remote_packet_size. */
546 if (size
> rs
->remote_packet_size
)
547 size
= rs
->remote_packet_size
;
552 /* Generic configuration support for packets the stub optionally
553 supports. Allows the user to specify the use of the packet as well
554 as allowing GDB to auto-detect support in the remote stub. */
558 PACKET_SUPPORT_UNKNOWN
= 0,
567 enum auto_boolean detect
;
568 enum packet_support support
;
571 /* Analyze a packet's return value and update the packet config
582 update_packet_config (struct packet_config
*config
)
584 switch (config
->detect
)
586 case AUTO_BOOLEAN_TRUE
:
587 config
->support
= PACKET_ENABLE
;
589 case AUTO_BOOLEAN_FALSE
:
590 config
->support
= PACKET_DISABLE
;
592 case AUTO_BOOLEAN_AUTO
:
593 config
->support
= PACKET_SUPPORT_UNKNOWN
;
599 show_packet_config_cmd (struct packet_config
*config
)
601 char *support
= "internal-error";
602 switch (config
->support
)
608 support
= "disabled";
610 case PACKET_SUPPORT_UNKNOWN
:
614 switch (config
->detect
)
616 case AUTO_BOOLEAN_AUTO
:
617 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
618 config
->name
, support
);
620 case AUTO_BOOLEAN_TRUE
:
621 case AUTO_BOOLEAN_FALSE
:
622 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
623 config
->name
, support
);
629 add_packet_config_cmd (struct packet_config
*config
,
632 cmd_sfunc_ftype
*set_func
,
633 show_value_ftype
*show_func
,
634 struct cmd_list_element
**set_remote_list
,
635 struct cmd_list_element
**show_remote_list
,
643 config
->title
= title
;
644 config
->detect
= AUTO_BOOLEAN_AUTO
;
645 config
->support
= PACKET_SUPPORT_UNKNOWN
;
646 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
648 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
650 /* set/show TITLE-packet {auto,on,off} */
651 cmd_name
= xstrprintf ("%s-packet", title
);
652 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
653 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
655 set_remote_list
, show_remote_list
);
656 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
660 legacy_name
= xstrprintf ("%s-packet", name
);
661 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
663 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
668 static enum packet_result
669 packet_ok (const char *buf
, struct packet_config
*config
)
673 /* The stub recognized the packet request. Check that the
674 operation succeeded. */
675 switch (config
->support
)
677 case PACKET_SUPPORT_UNKNOWN
:
679 fprintf_unfiltered (gdb_stdlog
,
680 "Packet %s (%s) is supported\n",
681 config
->name
, config
->title
);
682 config
->support
= PACKET_ENABLE
;
685 internal_error (__FILE__
, __LINE__
,
686 _("packet_ok: attempt to use a disabled packet"));
691 if (buf
[0] == 'O' && buf
[1] == 'K' && buf
[2] == '\0')
692 /* "OK" - definitly OK. */
695 && isxdigit (buf
[1]) && isxdigit (buf
[2])
697 /* "Enn" - definitly an error. */
699 /* The packet may or may not be OK. Just assume it is. */
704 /* The stub does not support the packet. */
705 switch (config
->support
)
708 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
709 /* If the stub previously indicated that the packet was
710 supported then there is a protocol error.. */
711 error (_("Protocol error: %s (%s) conflicting enabled responses."),
712 config
->name
, config
->title
);
714 /* The user set it wrong. */
715 error (_("Enabled packet %s (%s) not recognized by stub"),
716 config
->name
, config
->title
);
718 case PACKET_SUPPORT_UNKNOWN
:
720 fprintf_unfiltered (gdb_stdlog
,
721 "Packet %s (%s) is NOT supported\n",
722 config
->name
, config
->title
);
723 config
->support
= PACKET_DISABLE
;
728 return PACKET_UNKNOWN
;
748 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
751 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
752 struct cmd_list_element
*c
)
754 struct packet_config
*packet
;
756 for (packet
= remote_protocol_packets
;
757 packet
< &remote_protocol_packets
[PACKET_MAX
];
760 if (&packet
->detect
== c
->var
)
762 update_packet_config (packet
);
766 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
771 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
772 struct cmd_list_element
*c
,
775 struct packet_config
*packet
;
777 for (packet
= remote_protocol_packets
;
778 packet
< &remote_protocol_packets
[PACKET_MAX
];
781 if (&packet
->detect
== c
->var
)
783 show_packet_config_cmd (packet
);
787 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
791 /* Should we try one of the 'Z' requests? */
795 Z_PACKET_SOFTWARE_BP
,
796 Z_PACKET_HARDWARE_BP
,
803 /* For compatibility with older distributions. Provide a ``set remote
804 Z-packet ...'' command that updates all the Z packet types. */
806 static enum auto_boolean remote_Z_packet_detect
;
809 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
810 struct cmd_list_element
*c
)
813 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
815 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
816 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
821 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
822 struct cmd_list_element
*c
,
826 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
828 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
832 /* Should we try the 'ThreadInfo' query packet?
834 This variable (NOT available to the user: auto-detect only!)
835 determines whether GDB will use the new, simpler "ThreadInfo"
836 query or the older, more complex syntax for thread queries.
837 This is an auto-detect variable (set to true at each connect,
838 and set to false when the target fails to recognize it). */
840 static int use_threadinfo_query
;
841 static int use_threadextra_query
;
843 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
844 static void *sigint_remote_twice_token
;
845 static void *sigint_remote_token
;
847 /* These are pointers to hook functions that may be set in order to
848 modify resume/wait behavior for a particular architecture. */
850 void (*deprecated_target_resume_hook
) (void);
851 void (*deprecated_target_wait_loop_hook
) (void);
855 /* These are the threads which we last sent to the remote system.
856 -1 for all or -2 for not sent yet. */
857 static int general_thread
;
858 static int continue_thread
;
860 /* Call this function as a result of
861 1) A halt indication (T packet) containing a thread id
862 2) A direct query of currthread
863 3) Successful execution of set thread
867 record_currthread (int currthread
)
869 general_thread
= currthread
;
871 /* If this is a new thread, add it to GDB's thread list.
872 If we leave it up to WFI to do this, bad things will happen. */
873 if (!in_thread_list (pid_to_ptid (currthread
)))
875 add_thread (pid_to_ptid (currthread
));
876 ui_out_text (uiout
, "[New ");
877 ui_out_text (uiout
, target_pid_to_str (pid_to_ptid (currthread
)));
878 ui_out_text (uiout
, "]\n");
882 #define MAGIC_NULL_PID 42000
885 set_thread (int th
, int gen
)
887 struct remote_state
*rs
= get_remote_state ();
888 char *buf
= alloca (rs
->remote_packet_size
);
889 int state
= gen
? general_thread
: continue_thread
;
895 buf
[1] = gen
? 'g' : 'c';
896 if (th
== MAGIC_NULL_PID
)
902 xsnprintf (&buf
[2], rs
->remote_packet_size
- 2, "-%x", -th
);
904 xsnprintf (&buf
[2], rs
->remote_packet_size
- 2, "%x", th
);
906 getpkt (buf
, rs
->remote_packet_size
, 0);
910 continue_thread
= th
;
913 /* Return nonzero if the thread TH is still alive on the remote system. */
916 remote_thread_alive (ptid_t ptid
)
918 int tid
= PIDGET (ptid
);
922 xsnprintf (buf
, sizeof (buf
), "T-%08x", -tid
);
924 xsnprintf (buf
, sizeof (buf
), "T%08x", tid
);
926 getpkt (buf
, sizeof (buf
), 0);
927 return (buf
[0] == 'O' && buf
[1] == 'K');
930 /* About these extended threadlist and threadinfo packets. They are
931 variable length packets but, the fields within them are often fixed
932 length. They are redundent enough to send over UDP as is the
933 remote protocol in general. There is a matching unit test module
936 #define OPAQUETHREADBYTES 8
938 /* a 64 bit opaque identifier */
939 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
941 /* WARNING: This threadref data structure comes from the remote O.S.,
942 libstub protocol encoding, and remote.c. it is not particularly
945 /* Right now, the internal structure is int. We want it to be bigger.
949 typedef int gdb_threadref
; /* Internal GDB thread reference. */
951 /* gdb_ext_thread_info is an internal GDB data structure which is
952 equivalint to the reply of the remote threadinfo packet. */
954 struct gdb_ext_thread_info
956 threadref threadid
; /* External form of thread reference. */
957 int active
; /* Has state interesting to GDB?
959 char display
[256]; /* Brief state display, name,
960 blocked/suspended. */
961 char shortname
[32]; /* To be used to name threads. */
962 char more_display
[256]; /* Long info, statistics, queue depth,
966 /* The volume of remote transfers can be limited by submitting
967 a mask containing bits specifying the desired information.
968 Use a union of these values as the 'selection' parameter to
969 get_thread_info. FIXME: Make these TAG names more thread specific.
972 #define TAG_THREADID 1
974 #define TAG_DISPLAY 4
975 #define TAG_THREADNAME 8
976 #define TAG_MOREDISPLAY 16
978 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
980 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
982 static char *unpack_nibble (char *buf
, int *val
);
984 static char *pack_nibble (char *buf
, int nibble
);
986 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
988 static char *unpack_byte (char *buf
, int *value
);
990 static char *pack_int (char *buf
, int value
);
992 static char *unpack_int (char *buf
, int *value
);
994 static char *unpack_string (char *src
, char *dest
, int length
);
996 static char *pack_threadid (char *pkt
, threadref
*id
);
998 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1000 void int_to_threadref (threadref
*id
, int value
);
1002 static int threadref_to_int (threadref
*ref
);
1004 static void copy_threadref (threadref
*dest
, threadref
*src
);
1006 static int threadmatch (threadref
*dest
, threadref
*src
);
1008 static char *pack_threadinfo_request (char *pkt
, int mode
,
1011 static int remote_unpack_thread_info_response (char *pkt
,
1012 threadref
*expectedref
,
1013 struct gdb_ext_thread_info
1017 static int remote_get_threadinfo (threadref
*threadid
,
1018 int fieldset
, /*TAG mask */
1019 struct gdb_ext_thread_info
*info
);
1021 static char *pack_threadlist_request (char *pkt
, int startflag
,
1023 threadref
*nextthread
);
1025 static int parse_threadlist_response (char *pkt
,
1027 threadref
*original_echo
,
1028 threadref
*resultlist
,
1031 static int remote_get_threadlist (int startflag
,
1032 threadref
*nextthread
,
1036 threadref
*threadlist
);
1038 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1040 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1041 void *context
, int looplimit
);
1043 static int remote_newthread_step (threadref
*ref
, void *context
);
1045 /* Encode 64 bits in 16 chars of hex. */
1047 static const char hexchars
[] = "0123456789abcdef";
1050 ishex (int ch
, int *val
)
1052 if ((ch
>= 'a') && (ch
<= 'f'))
1054 *val
= ch
- 'a' + 10;
1057 if ((ch
>= 'A') && (ch
<= 'F'))
1059 *val
= ch
- 'A' + 10;
1062 if ((ch
>= '0') && (ch
<= '9'))
1073 if (ch
>= 'a' && ch
<= 'f')
1074 return ch
- 'a' + 10;
1075 if (ch
>= '0' && ch
<= '9')
1077 if (ch
>= 'A' && ch
<= 'F')
1078 return ch
- 'A' + 10;
1083 stub_unpack_int (char *buff
, int fieldlength
)
1090 nibble
= stubhex (*buff
++);
1094 retval
= retval
<< 4;
1100 unpack_varlen_hex (char *buff
, /* packet to parse */
1106 while (ishex (*buff
, &nibble
))
1109 retval
= retval
<< 4;
1110 retval
|= nibble
& 0x0f;
1117 unpack_nibble (char *buf
, int *val
)
1119 ishex (*buf
++, val
);
1124 pack_nibble (char *buf
, int nibble
)
1126 *buf
++ = hexchars
[(nibble
& 0x0f)];
1131 pack_hex_byte (char *pkt
, int byte
)
1133 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1134 *pkt
++ = hexchars
[(byte
& 0xf)];
1139 unpack_byte (char *buf
, int *value
)
1141 *value
= stub_unpack_int (buf
, 2);
1146 pack_int (char *buf
, int value
)
1148 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1149 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1150 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1151 buf
= pack_hex_byte (buf
, (value
& 0xff));
1156 unpack_int (char *buf
, int *value
)
1158 *value
= stub_unpack_int (buf
, 8);
1162 #if 0 /* Currently unused, uncomment when needed. */
1163 static char *pack_string (char *pkt
, char *string
);
1166 pack_string (char *pkt
, char *string
)
1171 len
= strlen (string
);
1173 len
= 200; /* Bigger than most GDB packets, junk??? */
1174 pkt
= pack_hex_byte (pkt
, len
);
1178 if ((ch
== '\0') || (ch
== '#'))
1179 ch
= '*'; /* Protect encapsulation. */
1184 #endif /* 0 (unused) */
1187 unpack_string (char *src
, char *dest
, int length
)
1196 pack_threadid (char *pkt
, threadref
*id
)
1199 unsigned char *altid
;
1201 altid
= (unsigned char *) id
;
1202 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1204 pkt
= pack_hex_byte (pkt
, *altid
++);
1210 unpack_threadid (char *inbuf
, threadref
*id
)
1213 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1216 altref
= (char *) id
;
1218 while (inbuf
< limit
)
1220 x
= stubhex (*inbuf
++);
1221 y
= stubhex (*inbuf
++);
1222 *altref
++ = (x
<< 4) | y
;
1227 /* Externally, threadrefs are 64 bits but internally, they are still
1228 ints. This is due to a mismatch of specifications. We would like
1229 to use 64bit thread references internally. This is an adapter
1233 int_to_threadref (threadref
*id
, int value
)
1235 unsigned char *scan
;
1237 scan
= (unsigned char *) id
;
1243 *scan
++ = (value
>> 24) & 0xff;
1244 *scan
++ = (value
>> 16) & 0xff;
1245 *scan
++ = (value
>> 8) & 0xff;
1246 *scan
++ = (value
& 0xff);
1250 threadref_to_int (threadref
*ref
)
1253 unsigned char *scan
;
1259 value
= (value
<< 8) | ((*scan
++) & 0xff);
1264 copy_threadref (threadref
*dest
, threadref
*src
)
1267 unsigned char *csrc
, *cdest
;
1269 csrc
= (unsigned char *) src
;
1270 cdest
= (unsigned char *) dest
;
1277 threadmatch (threadref
*dest
, threadref
*src
)
1279 /* Things are broken right now, so just assume we got a match. */
1281 unsigned char *srcp
, *destp
;
1283 srcp
= (char *) src
;
1284 destp
= (char *) dest
;
1288 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1295 threadid:1, # always request threadid
1302 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1305 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1307 *pkt
++ = 'q'; /* Info Query */
1308 *pkt
++ = 'P'; /* process or thread info */
1309 pkt
= pack_int (pkt
, mode
); /* mode */
1310 pkt
= pack_threadid (pkt
, id
); /* threadid */
1311 *pkt
= '\0'; /* terminate */
1315 /* These values tag the fields in a thread info response packet. */
1316 /* Tagging the fields allows us to request specific fields and to
1317 add more fields as time goes by. */
1319 #define TAG_THREADID 1 /* Echo the thread identifier. */
1320 #define TAG_EXISTS 2 /* Is this process defined enough to
1321 fetch registers and its stack? */
1322 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1323 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1324 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1328 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1329 struct gdb_ext_thread_info
*info
)
1331 struct remote_state
*rs
= get_remote_state ();
1335 char *limit
= pkt
+ rs
->remote_packet_size
; /* Plausible parsing limit. */
1338 /* info->threadid = 0; FIXME: implement zero_threadref. */
1340 info
->display
[0] = '\0';
1341 info
->shortname
[0] = '\0';
1342 info
->more_display
[0] = '\0';
1344 /* Assume the characters indicating the packet type have been
1346 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1347 pkt
= unpack_threadid (pkt
, &ref
);
1350 warning (_("Incomplete response to threadinfo request."));
1351 if (!threadmatch (&ref
, expectedref
))
1352 { /* This is an answer to a different request. */
1353 warning (_("ERROR RMT Thread info mismatch."));
1356 copy_threadref (&info
->threadid
, &ref
);
1358 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1360 /* Packets are terminated with nulls. */
1361 while ((pkt
< limit
) && mask
&& *pkt
)
1363 pkt
= unpack_int (pkt
, &tag
); /* tag */
1364 pkt
= unpack_byte (pkt
, &length
); /* length */
1365 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1367 warning (_("ERROR RMT: threadinfo tag mismatch."));
1371 if (tag
== TAG_THREADID
)
1375 warning (_("ERROR RMT: length of threadid is not 16."));
1379 pkt
= unpack_threadid (pkt
, &ref
);
1380 mask
= mask
& ~TAG_THREADID
;
1383 if (tag
== TAG_EXISTS
)
1385 info
->active
= stub_unpack_int (pkt
, length
);
1387 mask
= mask
& ~(TAG_EXISTS
);
1390 warning (_("ERROR RMT: 'exists' length too long."));
1396 if (tag
== TAG_THREADNAME
)
1398 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1399 mask
= mask
& ~TAG_THREADNAME
;
1402 if (tag
== TAG_DISPLAY
)
1404 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1405 mask
= mask
& ~TAG_DISPLAY
;
1408 if (tag
== TAG_MOREDISPLAY
)
1410 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1411 mask
= mask
& ~TAG_MOREDISPLAY
;
1414 warning (_("ERROR RMT: unknown thread info tag."));
1415 break; /* Not a tag we know about. */
1421 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1422 struct gdb_ext_thread_info
*info
)
1424 struct remote_state
*rs
= get_remote_state ();
1426 char *threadinfo_pkt
= alloca (rs
->remote_packet_size
);
1428 pack_threadinfo_request (threadinfo_pkt
, fieldset
, threadid
);
1429 putpkt (threadinfo_pkt
);
1430 getpkt (threadinfo_pkt
, rs
->remote_packet_size
, 0);
1431 result
= remote_unpack_thread_info_response (threadinfo_pkt
+ 2,
1436 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1439 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1440 threadref
*nextthread
)
1442 *pkt
++ = 'q'; /* info query packet */
1443 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1444 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1445 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1446 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1451 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1454 parse_threadlist_response (char *pkt
, int result_limit
,
1455 threadref
*original_echo
, threadref
*resultlist
,
1458 struct remote_state
*rs
= get_remote_state ();
1460 int count
, resultcount
, done
;
1463 /* Assume the 'q' and 'M chars have been stripped. */
1464 limit
= pkt
+ (rs
->remote_packet_size
- BUF_THREAD_ID_SIZE
);
1465 /* done parse past here */
1466 pkt
= unpack_byte (pkt
, &count
); /* count field */
1467 pkt
= unpack_nibble (pkt
, &done
);
1468 /* The first threadid is the argument threadid. */
1469 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1470 while ((count
-- > 0) && (pkt
< limit
))
1472 pkt
= unpack_threadid (pkt
, resultlist
++);
1473 if (resultcount
++ >= result_limit
)
1482 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1483 int *done
, int *result_count
, threadref
*threadlist
)
1485 struct remote_state
*rs
= get_remote_state ();
1486 static threadref echo_nextthread
;
1487 char *threadlist_packet
= alloca (rs
->remote_packet_size
);
1488 char *t_response
= alloca (rs
->remote_packet_size
);
1491 /* Trancate result limit to be smaller than the packet size. */
1492 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= rs
->remote_packet_size
)
1493 result_limit
= (rs
->remote_packet_size
/ BUF_THREAD_ID_SIZE
) - 2;
1495 pack_threadlist_request (threadlist_packet
,
1496 startflag
, result_limit
, nextthread
);
1497 putpkt (threadlist_packet
);
1498 getpkt (t_response
, rs
->remote_packet_size
, 0);
1501 parse_threadlist_response (t_response
+ 2, result_limit
, &echo_nextthread
,
1504 if (!threadmatch (&echo_nextthread
, nextthread
))
1506 /* FIXME: This is a good reason to drop the packet. */
1507 /* Possably, there is a duplicate response. */
1509 retransmit immediatly - race conditions
1510 retransmit after timeout - yes
1512 wait for packet, then exit
1514 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
1515 return 0; /* I choose simply exiting. */
1517 if (*result_count
<= 0)
1521 warning (_("RMT ERROR : failed to get remote thread list."));
1524 return result
; /* break; */
1526 if (*result_count
> result_limit
)
1529 warning (_("RMT ERROR: threadlist response longer than requested."));
1535 /* This is the interface between remote and threads, remotes upper
1538 /* remote_find_new_threads retrieves the thread list and for each
1539 thread in the list, looks up the thread in GDB's internal list,
1540 ading the thread if it does not already exist. This involves
1541 getting partial thread lists from the remote target so, polling the
1542 quit_flag is required. */
1545 /* About this many threadisds fit in a packet. */
1547 #define MAXTHREADLISTRESULTS 32
1550 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
1553 int done
, i
, result_count
;
1557 static threadref nextthread
;
1558 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1563 if (loopcount
++ > looplimit
)
1566 warning (_("Remote fetch threadlist -infinite loop-."));
1569 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1570 &done
, &result_count
, resultthreadlist
))
1575 /* Clear for later iterations. */
1577 /* Setup to resume next batch of thread references, set nextthread. */
1578 if (result_count
>= 1)
1579 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1581 while (result_count
--)
1582 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1589 remote_newthread_step (threadref
*ref
, void *context
)
1593 ptid
= pid_to_ptid (threadref_to_int (ref
));
1595 if (!in_thread_list (ptid
))
1597 return 1; /* continue iterator */
1600 #define CRAZY_MAX_THREADS 1000
1603 remote_current_thread (ptid_t oldpid
)
1605 struct remote_state
*rs
= get_remote_state ();
1606 char *buf
= alloca (rs
->remote_packet_size
);
1609 getpkt (buf
, rs
->remote_packet_size
, 0);
1610 if (buf
[0] == 'Q' && buf
[1] == 'C')
1611 /* Use strtoul here, so we'll correctly parse values whose highest
1612 bit is set. The protocol carries them as a simple series of
1613 hex digits; in the absence of a sign, strtol will see such
1614 values as positive numbers out of range for signed 'long', and
1615 return LONG_MAX to indicate an overflow. */
1616 return pid_to_ptid (strtoul (&buf
[2], NULL
, 16));
1621 /* Find new threads for info threads command.
1622 * Original version, using John Metzler's thread protocol.
1626 remote_find_new_threads (void)
1628 remote_threadlist_iterator (remote_newthread_step
, 0,
1630 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
) /* ack ack ack */
1631 inferior_ptid
= remote_current_thread (inferior_ptid
);
1635 * Find all threads for info threads command.
1636 * Uses new thread protocol contributed by Cisco.
1637 * Falls back and attempts to use the older method (above)
1638 * if the target doesn't respond to the new method.
1642 remote_threads_info (void)
1644 struct remote_state
*rs
= get_remote_state ();
1645 char *buf
= alloca (rs
->remote_packet_size
);
1649 if (remote_desc
== 0) /* paranoia */
1650 error (_("Command can only be used when connected to the remote target."));
1652 if (use_threadinfo_query
)
1654 putpkt ("qfThreadInfo");
1656 getpkt (bufp
, rs
->remote_packet_size
, 0);
1657 if (bufp
[0] != '\0') /* q packet recognized */
1659 while (*bufp
++ == 'm') /* reply contains one or more TID */
1663 /* Use strtoul here, so we'll correctly parse values
1664 whose highest bit is set. The protocol carries
1665 them as a simple series of hex digits; in the
1666 absence of a sign, strtol will see such values as
1667 positive numbers out of range for signed 'long',
1668 and return LONG_MAX to indicate an overflow. */
1669 tid
= strtoul (bufp
, &bufp
, 16);
1670 if (tid
!= 0 && !in_thread_list (pid_to_ptid (tid
)))
1671 add_thread (pid_to_ptid (tid
));
1673 while (*bufp
++ == ','); /* comma-separated list */
1674 putpkt ("qsThreadInfo");
1676 getpkt (bufp
, rs
->remote_packet_size
, 0);
1682 /* Else fall back to old method based on jmetzler protocol. */
1683 use_threadinfo_query
= 0;
1684 remote_find_new_threads ();
1689 * Collect a descriptive string about the given thread.
1690 * The target may say anything it wants to about the thread
1691 * (typically info about its blocked / runnable state, name, etc.).
1692 * This string will appear in the info threads display.
1694 * Optional: targets are not required to implement this function.
1698 remote_threads_extra_info (struct thread_info
*tp
)
1700 struct remote_state
*rs
= get_remote_state ();
1704 struct gdb_ext_thread_info threadinfo
;
1705 static char display_buf
[100]; /* arbitrary... */
1706 char *bufp
= alloca (rs
->remote_packet_size
);
1707 int n
= 0; /* position in display_buf */
1709 if (remote_desc
== 0) /* paranoia */
1710 internal_error (__FILE__
, __LINE__
,
1711 _("remote_threads_extra_info"));
1713 if (use_threadextra_query
)
1715 xsnprintf (bufp
, rs
->remote_packet_size
, "qThreadExtraInfo,%x",
1718 getpkt (bufp
, rs
->remote_packet_size
, 0);
1721 n
= min (strlen (bufp
) / 2, sizeof (display_buf
));
1722 result
= hex2bin (bufp
, (gdb_byte
*) display_buf
, n
);
1723 display_buf
[result
] = '\0';
1728 /* If the above query fails, fall back to the old method. */
1729 use_threadextra_query
= 0;
1730 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
1731 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
1732 int_to_threadref (&id
, PIDGET (tp
->ptid
));
1733 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
1734 if (threadinfo
.active
)
1736 if (*threadinfo
.shortname
)
1737 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
1738 " Name: %s,", threadinfo
.shortname
);
1739 if (*threadinfo
.display
)
1740 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
1741 " State: %s,", threadinfo
.display
);
1742 if (*threadinfo
.more_display
)
1743 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
1744 " Priority: %s", threadinfo
.more_display
);
1748 /* For purely cosmetic reasons, clear up trailing commas. */
1749 if (',' == display_buf
[n
-1])
1750 display_buf
[n
-1] = ' ';
1758 /* Restart the remote side; this is an extended protocol operation. */
1761 extended_remote_restart (void)
1763 struct remote_state
*rs
= get_remote_state ();
1764 char *buf
= alloca (rs
->remote_packet_size
);
1766 /* Send the restart command; for reasons I don't understand the
1767 remote side really expects a number after the "R". */
1768 xsnprintf (buf
, rs
->remote_packet_size
, "R%x", 0);
1771 /* Now query for status so this looks just like we restarted
1772 gdbserver from scratch. */
1774 getpkt (buf
, rs
->remote_packet_size
, 0);
1777 /* Clean up connection to a remote debugger. */
1780 remote_close (int quitting
)
1783 serial_close (remote_desc
);
1787 /* Query the remote side for the text, data and bss offsets. */
1792 struct remote_state
*rs
= get_remote_state ();
1793 char *buf
= alloca (rs
->remote_packet_size
);
1796 CORE_ADDR text_addr
, data_addr
, bss_addr
;
1797 struct section_offsets
*offs
;
1799 putpkt ("qOffsets");
1800 getpkt (buf
, rs
->remote_packet_size
, 0);
1802 if (buf
[0] == '\000')
1803 return; /* Return silently. Stub doesn't support
1807 warning (_("Remote failure reply: %s"), buf
);
1811 /* Pick up each field in turn. This used to be done with scanf, but
1812 scanf will make trouble if CORE_ADDR size doesn't match
1813 conversion directives correctly. The following code will work
1814 with any size of CORE_ADDR. */
1815 text_addr
= data_addr
= bss_addr
= 0;
1819 if (strncmp (ptr
, "Text=", 5) == 0)
1822 /* Don't use strtol, could lose on big values. */
1823 while (*ptr
&& *ptr
!= ';')
1824 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
1829 if (!lose
&& strncmp (ptr
, ";Data=", 6) == 0)
1832 while (*ptr
&& *ptr
!= ';')
1833 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
1838 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
1841 while (*ptr
&& *ptr
!= ';')
1842 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
1848 error (_("Malformed response to offset query, %s"), buf
);
1850 if (symfile_objfile
== NULL
)
1853 offs
= ((struct section_offsets
*)
1854 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
1855 memcpy (offs
, symfile_objfile
->section_offsets
,
1856 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
1858 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
1860 /* This is a temporary kludge to force data and bss to use the same offsets
1861 because that's what nlmconv does now. The real solution requires changes
1862 to the stub and remote.c that I don't have time to do right now. */
1864 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
1865 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
1867 objfile_relocate (symfile_objfile
, offs
);
1870 /* Stub for catch_errors. */
1873 remote_start_remote_dummy (struct ui_out
*uiout
, void *dummy
)
1875 start_remote (); /* Initialize gdb process mechanisms. */
1876 /* NOTE: Return something >=0. A -ve value is reserved for
1877 catch_exceptions. */
1882 remote_start_remote (struct ui_out
*uiout
, void *dummy
)
1884 immediate_quit
++; /* Allow user to interrupt it. */
1886 /* Ack any packet which the remote side has already sent. */
1887 serial_write (remote_desc
, "+", 1);
1889 /* Let the stub know that we want it to return the thread. */
1892 inferior_ptid
= remote_current_thread (inferior_ptid
);
1894 get_offsets (); /* Get text, data & bss offsets. */
1896 putpkt ("?"); /* Initiate a query from remote machine. */
1899 remote_start_remote_dummy (uiout
, dummy
);
1902 /* Open a connection to a remote debugger.
1903 NAME is the filename used for communication. */
1906 remote_open (char *name
, int from_tty
)
1908 remote_open_1 (name
, from_tty
, &remote_ops
, 0, 0);
1911 /* Just like remote_open, but with asynchronous support. */
1913 remote_async_open (char *name
, int from_tty
)
1915 remote_open_1 (name
, from_tty
, &remote_async_ops
, 0, 1);
1918 /* Open a connection to a remote debugger using the extended
1919 remote gdb protocol. NAME is the filename used for communication. */
1922 extended_remote_open (char *name
, int from_tty
)
1924 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */,
1928 /* Just like extended_remote_open, but with asynchronous support. */
1930 extended_remote_async_open (char *name
, int from_tty
)
1932 remote_open_1 (name
, from_tty
, &extended_async_remote_ops
,
1933 1 /*extended_p */, 1 /* async_p */);
1936 /* Generic code for opening a connection to a remote target. */
1939 init_all_packet_configs (void)
1942 for (i
= 0; i
< PACKET_MAX
; i
++)
1943 update_packet_config (&remote_protocol_packets
[i
]);
1946 /* Symbol look-up. */
1949 remote_check_symbols (struct objfile
*objfile
)
1951 struct remote_state
*rs
= get_remote_state ();
1952 char *msg
, *reply
, *tmp
;
1953 struct minimal_symbol
*sym
;
1956 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
1959 msg
= alloca (rs
->remote_packet_size
);
1960 reply
= alloca (rs
->remote_packet_size
);
1962 /* Invite target to request symbol lookups. */
1964 putpkt ("qSymbol::");
1965 getpkt (reply
, rs
->remote_packet_size
, 0);
1966 packet_ok (reply
, &remote_protocol_packets
[PACKET_qSymbol
]);
1968 while (strncmp (reply
, "qSymbol:", 8) == 0)
1971 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
1973 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
1975 xsnprintf (msg
, rs
->remote_packet_size
, "qSymbol::%s", &reply
[8]);
1977 xsnprintf (msg
, rs
->remote_packet_size
, "qSymbol:%s:%s",
1978 paddr_nz (SYMBOL_VALUE_ADDRESS (sym
)),
1981 getpkt (reply
, rs
->remote_packet_size
, 0);
1985 static struct serial
*
1986 remote_serial_open (char *name
)
1988 static int udp_warning
= 0;
1990 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
1991 of in ser-tcp.c, because it is the remote protocol assuming that the
1992 serial connection is reliable and not the serial connection promising
1994 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
1997 The remote protocol may be unreliable over UDP.\n\
1998 Some events may be lost, rendering further debugging impossible."));
2002 return serial_open (name
);
2006 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
,
2007 int extended_p
, int async_p
)
2009 struct remote_state
*rs
= get_remote_state ();
2011 error (_("To open a remote debug connection, you need to specify what\n"
2012 "serial device is attached to the remote system\n"
2013 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
2015 /* See FIXME above. */
2017 wait_forever_enabled_p
= 1;
2019 reopen_exec_file ();
2022 target_preopen (from_tty
);
2024 unpush_target (target
);
2026 remote_desc
= remote_serial_open (name
);
2028 perror_with_name (name
);
2030 if (baud_rate
!= -1)
2032 if (serial_setbaudrate (remote_desc
, baud_rate
))
2034 /* The requested speed could not be set. Error out to
2035 top level after closing remote_desc. Take care to
2036 set remote_desc to NULL to avoid closing remote_desc
2038 serial_close (remote_desc
);
2040 perror_with_name (name
);
2044 serial_raw (remote_desc
);
2046 /* If there is something sitting in the buffer we might take it as a
2047 response to a command, which would be bad. */
2048 serial_flush_input (remote_desc
);
2052 puts_filtered ("Remote debugging using ");
2053 puts_filtered (name
);
2054 puts_filtered ("\n");
2056 push_target (target
); /* Switch to using remote target now. */
2058 init_all_packet_configs ();
2060 general_thread
= -2;
2061 continue_thread
= -2;
2063 /* Probe for ability to use "ThreadInfo" query, as required. */
2064 use_threadinfo_query
= 1;
2065 use_threadextra_query
= 1;
2067 /* Without this, some commands which require an active target (such
2068 as kill) won't work. This variable serves (at least) double duty
2069 as both the pid of the target process (if it has such), and as a
2070 flag indicating that a target is active. These functions should
2071 be split out into seperate variables, especially since GDB will
2072 someday have a notion of debugging several processes. */
2074 inferior_ptid
= pid_to_ptid (MAGIC_NULL_PID
);
2078 /* With this target we start out by owning the terminal. */
2079 remote_async_terminal_ours_p
= 1;
2081 /* FIXME: cagney/1999-09-23: During the initial connection it is
2082 assumed that the target is already ready and able to respond to
2083 requests. Unfortunately remote_start_remote() eventually calls
2084 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2085 around this. Eventually a mechanism that allows
2086 wait_for_inferior() to expect/get timeouts will be
2088 wait_forever_enabled_p
= 0;
2091 /* First delete any symbols previously loaded from shared libraries. */
2092 no_shared_libraries (NULL
, 0);
2094 /* Start the remote connection. If error() or QUIT, discard this
2095 target (we'd otherwise be in an inconsistent state) and then
2096 propogate the error on up the exception chain. This ensures that
2097 the caller doesn't stumble along blindly assuming that the
2098 function succeeded. The CLI doesn't have this problem but other
2099 UI's, such as MI do.
2101 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2102 this function should return an error indication letting the
2103 caller restore the previous state. Unfortunately the command
2104 ``target remote'' is directly wired to this function making that
2105 impossible. On a positive note, the CLI side of this problem has
2106 been fixed - the function set_cmd_context() makes it possible for
2107 all the ``target ....'' commands to share a common callback
2108 function. See cli-dump.c. */
2110 struct gdb_exception ex
2111 = catch_exception (uiout
, remote_start_remote
, NULL
, RETURN_MASK_ALL
);
2116 wait_forever_enabled_p
= 1;
2117 throw_exception (ex
);
2122 wait_forever_enabled_p
= 1;
2126 /* Tell the remote that we are using the extended protocol. */
2127 char *buf
= alloca (rs
->remote_packet_size
);
2129 getpkt (buf
, rs
->remote_packet_size
, 0);
2132 post_create_inferior (¤t_target
, from_tty
);
2134 if (exec_bfd
) /* No use without an exec file. */
2135 remote_check_symbols (symfile_objfile
);
2138 /* This takes a program previously attached to and detaches it. After
2139 this is done, GDB can be used to debug some other program. We
2140 better not have left any breakpoints in the target program or it'll
2141 die when it hits one. */
2144 remote_detach (char *args
, int from_tty
)
2146 struct remote_state
*rs
= get_remote_state ();
2147 char *buf
= alloca (rs
->remote_packet_size
);
2150 error (_("Argument given to \"detach\" when remotely debugging."));
2152 /* Tell the remote target to detach. */
2154 remote_send (buf
, rs
->remote_packet_size
);
2156 /* Unregister the file descriptor from the event loop. */
2157 if (target_is_async_p ())
2158 serial_async (remote_desc
, NULL
, 0);
2160 target_mourn_inferior ();
2162 puts_filtered ("Ending remote debugging.\n");
2165 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2168 remote_disconnect (char *args
, int from_tty
)
2171 error (_("Argument given to \"detach\" when remotely debugging."));
2173 /* Unregister the file descriptor from the event loop. */
2174 if (target_is_async_p ())
2175 serial_async (remote_desc
, NULL
, 0);
2177 target_mourn_inferior ();
2179 puts_filtered ("Ending remote debugging.\n");
2182 /* Convert hex digit A to a number. */
2187 if (a
>= '0' && a
<= '9')
2189 else if (a
>= 'a' && a
<= 'f')
2190 return a
- 'a' + 10;
2191 else if (a
>= 'A' && a
<= 'F')
2192 return a
- 'A' + 10;
2194 error (_("Reply contains invalid hex digit %d"), a
);
2198 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
2202 for (i
= 0; i
< count
; i
++)
2204 if (hex
[0] == 0 || hex
[1] == 0)
2206 /* Hex string is short, or of uneven length.
2207 Return the count that has been converted so far. */
2210 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
2216 /* Convert number NIB to a hex digit. */
2224 return 'a' + nib
- 10;
2228 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
2231 /* May use a length, or a nul-terminated string as input. */
2233 count
= strlen ((char *) bin
);
2235 for (i
= 0; i
< count
; i
++)
2237 *hex
++ = tohex ((*bin
>> 4) & 0xf);
2238 *hex
++ = tohex (*bin
++ & 0xf);
2244 /* Check for the availability of vCont. This function should also check
2248 remote_vcont_probe (struct remote_state
*rs
, char *buf
)
2250 strcpy (buf
, "vCont?");
2252 getpkt (buf
, rs
->remote_packet_size
, 0);
2254 /* Make sure that the features we assume are supported. */
2255 if (strncmp (buf
, "vCont", 5) == 0)
2258 int support_s
, support_S
, support_c
, support_C
;
2264 while (p
&& *p
== ';')
2267 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2269 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2271 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2273 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2276 p
= strchr (p
, ';');
2279 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2280 BUF will make packet_ok disable the packet. */
2281 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
2285 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
2288 /* Resume the remote inferior by using a "vCont" packet. The thread
2289 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2290 resumed thread should be single-stepped and/or signalled. If PTID's
2291 PID is -1, then all threads are resumed; the thread to be stepped and/or
2292 signalled is given in the global INFERIOR_PTID. This function returns
2293 non-zero iff it resumes the inferior.
2295 This function issues a strict subset of all possible vCont commands at the
2299 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2301 struct remote_state
*rs
= get_remote_state ();
2302 int pid
= PIDGET (ptid
);
2303 char *buf
= NULL
, *outbuf
;
2304 struct cleanup
*old_cleanup
;
2306 buf
= xmalloc (rs
->remote_packet_size
);
2307 old_cleanup
= make_cleanup (xfree
, buf
);
2309 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
2310 remote_vcont_probe (rs
, buf
);
2312 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
2314 do_cleanups (old_cleanup
);
2318 /* If we could generate a wider range of packets, we'd have to worry
2319 about overflowing BUF. Should there be a generic
2320 "multi-part-packet" packet? */
2322 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
)
2324 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2325 don't have any PID numbers the inferior will understand. Make sure
2326 to only send forms that do not specify a PID. */
2327 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2328 outbuf
= xstrprintf ("vCont;S%02x", siggnal
);
2330 outbuf
= xstrprintf ("vCont;s");
2331 else if (siggnal
!= TARGET_SIGNAL_0
)
2332 outbuf
= xstrprintf ("vCont;C%02x", siggnal
);
2334 outbuf
= xstrprintf ("vCont;c");
2338 /* Resume all threads, with preference for INFERIOR_PTID. */
2339 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2340 outbuf
= xstrprintf ("vCont;S%02x:%x;c", siggnal
,
2341 PIDGET (inferior_ptid
));
2343 outbuf
= xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid
));
2344 else if (siggnal
!= TARGET_SIGNAL_0
)
2345 outbuf
= xstrprintf ("vCont;C%02x:%x;c", siggnal
,
2346 PIDGET (inferior_ptid
));
2348 outbuf
= xstrprintf ("vCont;c");
2352 /* Scheduler locking; resume only PTID. */
2353 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2354 outbuf
= xstrprintf ("vCont;S%02x:%x", siggnal
, pid
);
2356 outbuf
= xstrprintf ("vCont;s:%x", pid
);
2357 else if (siggnal
!= TARGET_SIGNAL_0
)
2358 outbuf
= xstrprintf ("vCont;C%02x:%x", siggnal
, pid
);
2360 outbuf
= xstrprintf ("vCont;c:%x", pid
);
2363 gdb_assert (outbuf
&& strlen (outbuf
) < rs
->remote_packet_size
);
2364 make_cleanup (xfree
, outbuf
);
2368 do_cleanups (old_cleanup
);
2373 /* Tell the remote machine to resume. */
2375 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
2377 static int last_sent_step
;
2380 remote_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2382 struct remote_state
*rs
= get_remote_state ();
2383 char *buf
= alloca (rs
->remote_packet_size
);
2384 int pid
= PIDGET (ptid
);
2386 last_sent_signal
= siggnal
;
2387 last_sent_step
= step
;
2389 /* A hook for when we need to do something at the last moment before
2391 if (deprecated_target_resume_hook
)
2392 (*deprecated_target_resume_hook
) ();
2394 /* The vCont packet doesn't need to specify threads via Hc. */
2395 if (remote_vcont_resume (ptid
, step
, siggnal
))
2398 /* All other supported resume packets do use Hc, so call set_thread. */
2400 set_thread (0, 0); /* Run any thread. */
2402 set_thread (pid
, 0); /* Run this thread. */
2404 if (siggnal
!= TARGET_SIGNAL_0
)
2406 buf
[0] = step
? 'S' : 'C';
2407 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2408 buf
[2] = tohex (((int) siggnal
) & 0xf);
2412 strcpy (buf
, step
? "s" : "c");
2417 /* Same as remote_resume, but with async support. */
2419 remote_async_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2421 remote_resume (ptid
, step
, siggnal
);
2423 /* We are about to start executing the inferior, let's register it
2424 with the event loop. NOTE: this is the one place where all the
2425 execution commands end up. We could alternatively do this in each
2426 of the execution commands in infcmd.c. */
2427 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2428 into infcmd.c in order to allow inferior function calls to work
2429 NOT asynchronously. */
2430 if (target_can_async_p ())
2431 target_async (inferior_event_handler
, 0);
2432 /* Tell the world that the target is now executing. */
2433 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2434 this? Instead, should the client of target just assume (for
2435 async targets) that the target is going to start executing? Is
2436 this information already found in the continuation block? */
2437 if (target_is_async_p ())
2438 target_executing
= 1;
2442 /* Set up the signal handler for SIGINT, while the target is
2443 executing, ovewriting the 'regular' SIGINT signal handler. */
2445 initialize_sigint_signal_handler (void)
2447 sigint_remote_token
=
2448 create_async_signal_handler (async_remote_interrupt
, NULL
);
2449 signal (SIGINT
, handle_remote_sigint
);
2452 /* Signal handler for SIGINT, while the target is executing. */
2454 handle_remote_sigint (int sig
)
2456 signal (sig
, handle_remote_sigint_twice
);
2457 sigint_remote_twice_token
=
2458 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
2459 mark_async_signal_handler_wrapper (sigint_remote_token
);
2462 /* Signal handler for SIGINT, installed after SIGINT has already been
2463 sent once. It will take effect the second time that the user sends
2466 handle_remote_sigint_twice (int sig
)
2468 signal (sig
, handle_sigint
);
2469 sigint_remote_twice_token
=
2470 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
2471 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
2474 /* Perform the real interruption of the target execution, in response
2477 async_remote_interrupt (gdb_client_data arg
)
2480 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2485 /* Perform interrupt, if the first attempt did not succeed. Just give
2486 up on the target alltogether. */
2488 async_remote_interrupt_twice (gdb_client_data arg
)
2491 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
2492 /* Do something only if the target was not killed by the previous
2494 if (target_executing
)
2497 signal (SIGINT
, handle_remote_sigint
);
2501 /* Reinstall the usual SIGINT handlers, after the target has
2504 cleanup_sigint_signal_handler (void *dummy
)
2506 signal (SIGINT
, handle_sigint
);
2507 if (sigint_remote_twice_token
)
2508 delete_async_signal_handler ((struct async_signal_handler
**)
2509 &sigint_remote_twice_token
);
2510 if (sigint_remote_token
)
2511 delete_async_signal_handler ((struct async_signal_handler
**)
2512 &sigint_remote_token
);
2515 /* Send ^C to target to halt it. Target will respond, and send us a
2517 static void (*ofunc
) (int);
2519 /* The command line interface's stop routine. This function is installed
2520 as a signal handler for SIGINT. The first time a user requests a
2521 stop, we call remote_stop to send a break or ^C. If there is no
2522 response from the target (it didn't stop when the user requested it),
2523 we ask the user if he'd like to detach from the target. */
2525 remote_interrupt (int signo
)
2527 /* If this doesn't work, try more severe steps. */
2528 signal (signo
, remote_interrupt_twice
);
2531 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2536 /* The user typed ^C twice. */
2539 remote_interrupt_twice (int signo
)
2541 signal (signo
, ofunc
);
2543 signal (signo
, remote_interrupt
);
2546 /* This is the generic stop called via the target vector. When a target
2547 interrupt is requested, either by the command line or the GUI, we
2548 will eventually end up here. */
2552 /* Send a break or a ^C, depending on user preference. */
2554 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
2557 serial_send_break (remote_desc
);
2559 serial_write (remote_desc
, "\003", 1);
2562 /* Ask the user what to do when an interrupt is received. */
2565 interrupt_query (void)
2567 target_terminal_ours ();
2569 if (query ("Interrupted while waiting for the program.\n\
2570 Give up (and stop debugging it)? "))
2572 target_mourn_inferior ();
2573 deprecated_throw_reason (RETURN_QUIT
);
2576 target_terminal_inferior ();
2579 /* Enable/disable target terminal ownership. Most targets can use
2580 terminal groups to control terminal ownership. Remote targets are
2581 different in that explicit transfer of ownership to/from GDB/target
2585 remote_async_terminal_inferior (void)
2587 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2588 sync_execution here. This function should only be called when
2589 GDB is resuming the inferior in the forground. A background
2590 resume (``run&'') should leave GDB in control of the terminal and
2591 consequently should not call this code. */
2592 if (!sync_execution
)
2594 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2595 calls target_terminal_*() idenpotent. The event-loop GDB talking
2596 to an asynchronous target with a synchronous command calls this
2597 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2598 stops trying to transfer the terminal to the target when it
2599 shouldn't this guard can go away. */
2600 if (!remote_async_terminal_ours_p
)
2602 delete_file_handler (input_fd
);
2603 remote_async_terminal_ours_p
= 0;
2604 initialize_sigint_signal_handler ();
2605 /* NOTE: At this point we could also register our selves as the
2606 recipient of all input. Any characters typed could then be
2607 passed on down to the target. */
2611 remote_async_terminal_ours (void)
2613 /* See FIXME in remote_async_terminal_inferior. */
2614 if (!sync_execution
)
2616 /* See FIXME in remote_async_terminal_inferior. */
2617 if (remote_async_terminal_ours_p
)
2619 cleanup_sigint_signal_handler (NULL
);
2620 add_file_handler (input_fd
, stdin_event_handler
, 0);
2621 remote_async_terminal_ours_p
= 1;
2624 /* If nonzero, ignore the next kill. */
2629 remote_console_output (char *msg
)
2633 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
2636 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
2639 fputs_unfiltered (tb
, gdb_stdtarg
);
2641 gdb_flush (gdb_stdtarg
);
2644 /* Wait until the remote machine stops, then return,
2645 storing status in STATUS just as `wait' would.
2646 Returns "pid", which in the case of a multi-threaded
2647 remote OS, is the thread-id. */
2650 remote_wait (ptid_t ptid
, struct target_waitstatus
*status
)
2652 struct remote_state
*rs
= get_remote_state ();
2653 char *buf
= alloca (rs
->remote_packet_size
);
2654 ULONGEST thread_num
= -1;
2657 status
->kind
= TARGET_WAITKIND_EXITED
;
2658 status
->value
.integer
= 0;
2664 ofunc
= signal (SIGINT
, remote_interrupt
);
2665 getpkt (buf
, rs
->remote_packet_size
, 1);
2666 signal (SIGINT
, ofunc
);
2668 /* This is a hook for when we need to do something (perhaps the
2669 collection of trace data) every time the target stops. */
2670 if (deprecated_target_wait_loop_hook
)
2671 (*deprecated_target_wait_loop_hook
) ();
2673 remote_stopped_by_watchpoint_p
= 0;
2677 case 'E': /* Error of some sort. */
2678 warning (_("Remote failure reply: %s"), buf
);
2680 case 'F': /* File-I/O request. */
2681 remote_fileio_request (buf
);
2683 case 'T': /* Status with PC, SP, FP, ... */
2685 gdb_byte regs
[MAX_REGISTER_SIZE
];
2687 /* Expedited reply, containing Signal, {regno, reg} repeat. */
2688 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2690 n... = register number
2691 r... = register contents
2693 p
= &buf
[3]; /* after Txx */
2702 /* If the packet contains a register number save it in
2703 pnum and set p1 to point to the character following
2704 it. Otherwise p1 points to p. */
2706 /* If this packet is an awatch packet, don't parse the
2707 'a' as a register number. */
2709 if (strncmp (p
, "awatch", strlen("awatch")) != 0)
2711 /* Read the ``P'' register number. */
2712 pnum
= strtol (p
, &p_temp
, 16);
2718 if (p1
== p
) /* No register number present here. */
2720 p1
= strchr (p
, ':');
2722 warning (_("Malformed packet(a) (missing colon): %s\n\
2725 if (strncmp (p
, "thread", p1
- p
) == 0)
2727 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2728 record_currthread (thread_num
);
2731 else if ((strncmp (p
, "watch", p1
- p
) == 0)
2732 || (strncmp (p
, "rwatch", p1
- p
) == 0)
2733 || (strncmp (p
, "awatch", p1
- p
) == 0))
2735 remote_stopped_by_watchpoint_p
= 1;
2736 p
= unpack_varlen_hex (++p1
, &addr
);
2737 remote_watch_data_address
= (CORE_ADDR
)addr
;
2741 /* Silently skip unknown optional info. */
2742 p_temp
= strchr (p1
+ 1, ';');
2749 struct packet_reg
*reg
= packet_reg_from_pnum (rs
, pnum
);
2753 error (_("Malformed packet(b) (missing colon): %s\n\
2758 error (_("Remote sent bad register number %s: %s\n\
2760 phex_nz (pnum
, 0), p
, buf
);
2762 fieldsize
= hex2bin (p
, regs
,
2763 register_size (current_gdbarch
,
2766 if (fieldsize
< register_size (current_gdbarch
,
2768 warning (_("Remote reply is too short: %s"), buf
);
2769 regcache_raw_supply (current_regcache
,
2774 error (_("Remote register badly formatted: %s\nhere: %s"),
2779 case 'S': /* Old style status, just signal only. */
2780 status
->kind
= TARGET_WAITKIND_STOPPED
;
2781 status
->value
.sig
= (enum target_signal
)
2782 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2786 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2787 record_currthread (thread_num
);
2790 case 'W': /* Target exited. */
2792 /* The remote process exited. */
2793 status
->kind
= TARGET_WAITKIND_EXITED
;
2794 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2798 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2799 status
->value
.sig
= (enum target_signal
)
2800 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2804 case 'O': /* Console output. */
2805 remote_console_output (buf
+ 1);
2808 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2810 /* Zero length reply means that we tried 'S' or 'C' and
2811 the remote system doesn't support it. */
2812 target_terminal_ours_for_output ();
2814 ("Can't send signals to this remote system. %s not sent.\n",
2815 target_signal_to_name (last_sent_signal
));
2816 last_sent_signal
= TARGET_SIGNAL_0
;
2817 target_terminal_inferior ();
2819 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2820 putpkt ((char *) buf
);
2823 /* else fallthrough */
2825 warning (_("Invalid remote reply: %s"), buf
);
2830 if (thread_num
!= -1)
2832 return pid_to_ptid (thread_num
);
2834 return inferior_ptid
;
2837 /* Async version of remote_wait. */
2839 remote_async_wait (ptid_t ptid
, struct target_waitstatus
*status
)
2841 struct remote_state
*rs
= get_remote_state ();
2842 char *buf
= alloca (rs
->remote_packet_size
);
2843 ULONGEST thread_num
= -1;
2846 status
->kind
= TARGET_WAITKIND_EXITED
;
2847 status
->value
.integer
= 0;
2849 remote_stopped_by_watchpoint_p
= 0;
2855 if (!target_is_async_p ())
2856 ofunc
= signal (SIGINT
, remote_interrupt
);
2857 /* FIXME: cagney/1999-09-27: If we're in async mode we should
2858 _never_ wait for ever -> test on target_is_async_p().
2859 However, before we do that we need to ensure that the caller
2860 knows how to take the target into/out of async mode. */
2861 getpkt (buf
, rs
->remote_packet_size
, wait_forever_enabled_p
);
2862 if (!target_is_async_p ())
2863 signal (SIGINT
, ofunc
);
2865 /* This is a hook for when we need to do something (perhaps the
2866 collection of trace data) every time the target stops. */
2867 if (deprecated_target_wait_loop_hook
)
2868 (*deprecated_target_wait_loop_hook
) ();
2872 case 'E': /* Error of some sort. */
2873 warning (_("Remote failure reply: %s"), buf
);
2875 case 'F': /* File-I/O request. */
2876 remote_fileio_request (buf
);
2878 case 'T': /* Status with PC, SP, FP, ... */
2880 gdb_byte regs
[MAX_REGISTER_SIZE
];
2882 /* Expedited reply, containing Signal, {regno, reg} repeat. */
2883 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2885 n... = register number
2886 r... = register contents
2888 p
= &buf
[3]; /* after Txx */
2897 /* If the packet contains a register number, save it
2898 in pnum and set p1 to point to the character
2899 following it. Otherwise p1 points to p. */
2901 /* If this packet is an awatch packet, don't parse the 'a'
2902 as a register number. */
2904 if (!strncmp (p
, "awatch", strlen ("awatch")) != 0)
2906 /* Read the register number. */
2907 pnum
= strtol (p
, &p_temp
, 16);
2913 if (p1
== p
) /* No register number present here. */
2915 p1
= strchr (p
, ':');
2917 error (_("Malformed packet(a) (missing colon): %s\n\
2920 if (strncmp (p
, "thread", p1
- p
) == 0)
2922 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2923 record_currthread (thread_num
);
2926 else if ((strncmp (p
, "watch", p1
- p
) == 0)
2927 || (strncmp (p
, "rwatch", p1
- p
) == 0)
2928 || (strncmp (p
, "awatch", p1
- p
) == 0))
2930 remote_stopped_by_watchpoint_p
= 1;
2931 p
= unpack_varlen_hex (++p1
, &addr
);
2932 remote_watch_data_address
= (CORE_ADDR
)addr
;
2936 /* Silently skip unknown optional info. */
2937 p_temp
= strchr (p1
+ 1, ';');
2945 struct packet_reg
*reg
= packet_reg_from_pnum (rs
, pnum
);
2948 error (_("Malformed packet(b) (missing colon): %s\n\
2953 error (_("Remote sent bad register number %ld: %s\n\
2957 fieldsize
= hex2bin (p
, regs
,
2958 register_size (current_gdbarch
,
2961 if (fieldsize
< register_size (current_gdbarch
,
2963 warning (_("Remote reply is too short: %s"), buf
);
2964 regcache_raw_supply (current_regcache
, reg
->regnum
, regs
);
2968 error (_("Remote register badly formatted: %s\nhere: %s"),
2973 case 'S': /* Old style status, just signal only. */
2974 status
->kind
= TARGET_WAITKIND_STOPPED
;
2975 status
->value
.sig
= (enum target_signal
)
2976 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2980 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2981 record_currthread (thread_num
);
2984 case 'W': /* Target exited. */
2986 /* The remote process exited. */
2987 status
->kind
= TARGET_WAITKIND_EXITED
;
2988 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2992 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2993 status
->value
.sig
= (enum target_signal
)
2994 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2998 case 'O': /* Console output. */
2999 remote_console_output (buf
+ 1);
3000 /* Return immediately to the event loop. The event loop will
3001 still be waiting on the inferior afterwards. */
3002 status
->kind
= TARGET_WAITKIND_IGNORE
;
3005 if (last_sent_signal
!= TARGET_SIGNAL_0
)
3007 /* Zero length reply means that we tried 'S' or 'C' and
3008 the remote system doesn't support it. */
3009 target_terminal_ours_for_output ();
3011 ("Can't send signals to this remote system. %s not sent.\n",
3012 target_signal_to_name (last_sent_signal
));
3013 last_sent_signal
= TARGET_SIGNAL_0
;
3014 target_terminal_inferior ();
3016 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
3017 putpkt ((char *) buf
);
3020 /* else fallthrough */
3022 warning (_("Invalid remote reply: %s"), buf
);
3027 if (thread_num
!= -1)
3029 return pid_to_ptid (thread_num
);
3031 return inferior_ptid
;
3034 /* Number of bytes of registers this stub implements. */
3036 static int register_bytes_found
;
3038 /* Read the remote registers into the block REGS. */
3039 /* Currently we just read all the registers, so we don't use regnum. */
3042 fetch_register_using_p (int regnum
)
3044 struct remote_state
*rs
= get_remote_state ();
3045 char *buf
= alloca (rs
->remote_packet_size
), *p
;
3046 char regp
[MAX_REGISTER_SIZE
];
3051 p
+= hexnumstr (p
, regnum
);
3053 remote_send (buf
, rs
->remote_packet_size
);
3055 /* If the stub didn't recognize the packet, or if we got an error,
3057 if (buf
[0] == '\0' || buf
[0] == 'E')
3060 /* If this register is unfetchable, tell the regcache. */
3063 regcache_raw_supply (current_regcache
, regnum
, NULL
);
3064 set_register_cached (regnum
, -1);
3068 /* Otherwise, parse and supply the value. */
3075 error (_("fetch_register_using_p: early buf termination"));
3079 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3082 regcache_raw_supply (current_regcache
, regnum
, regp
);
3087 remote_fetch_registers (int regnum
)
3089 struct remote_state
*rs
= get_remote_state ();
3090 char *buf
= alloca (rs
->remote_packet_size
);
3093 char *regs
= alloca (rs
->sizeof_g_packet
);
3095 set_thread (PIDGET (inferior_ptid
), 1);
3099 struct packet_reg
*reg
= packet_reg_from_regnum (rs
, regnum
);
3100 gdb_assert (reg
!= NULL
);
3101 if (!reg
->in_g_packet
)
3102 internal_error (__FILE__
, __LINE__
,
3103 _("Attempt to fetch a non G-packet register when this "
3104 "remote.c does not support the p-packet."));
3106 switch (remote_protocol_packets
[PACKET_p
].support
)
3108 case PACKET_DISABLE
:
3111 if (fetch_register_using_p (regnum
))
3114 error (_("Protocol error: p packet not recognized by stub"));
3115 case PACKET_SUPPORT_UNKNOWN
:
3116 if (fetch_register_using_p (regnum
))
3118 /* The stub recognized the 'p' packet. Remember this. */
3119 remote_protocol_packets
[PACKET_p
].support
= PACKET_ENABLE
;
3124 /* The stub does not support the 'P' packet. Use 'G'
3125 instead, and don't try using 'P' in the future (it
3126 will just waste our time). */
3127 remote_protocol_packets
[PACKET_p
].support
= PACKET_DISABLE
;
3133 remote_send (buf
, rs
->remote_packet_size
);
3135 /* Save the size of the packet sent to us by the target. Its used
3136 as a heuristic when determining the max size of packets that the
3137 target can safely receive. */
3138 if ((rs
->actual_register_packet_size
) == 0)
3139 (rs
->actual_register_packet_size
) = strlen (buf
);
3141 /* Unimplemented registers read as all bits zero. */
3142 memset (regs
, 0, rs
->sizeof_g_packet
);
3144 /* We can get out of synch in various cases. If the first character
3145 in the buffer is not a hex character, assume that has happened
3146 and try to fetch another packet to read. */
3147 while ((buf
[0] < '0' || buf
[0] > '9')
3148 && (buf
[0] < 'A' || buf
[0] > 'F')
3149 && (buf
[0] < 'a' || buf
[0] > 'f')
3150 && buf
[0] != 'x') /* New: unavailable register value. */
3153 fprintf_unfiltered (gdb_stdlog
,
3154 "Bad register packet; fetching a new packet\n");
3155 getpkt (buf
, rs
->remote_packet_size
, 0);
3158 /* Reply describes registers byte by byte, each byte encoded as two
3159 hex characters. Suck them all up, then supply them to the
3160 register cacheing/storage mechanism. */
3163 for (i
= 0; i
< rs
->sizeof_g_packet
; i
++)
3169 warning (_("Remote reply is of odd length: %s"), buf
);
3170 /* Don't change register_bytes_found in this case, and don't
3171 print a second warning. */
3174 if (p
[0] == 'x' && p
[1] == 'x')
3175 regs
[i
] = 0; /* 'x' */
3177 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3181 if (i
!= register_bytes_found
)
3183 register_bytes_found
= i
;
3184 if (REGISTER_BYTES_OK_P ()
3185 && !REGISTER_BYTES_OK (i
))
3186 warning (_("Remote reply is too short: %s"), buf
);
3192 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
3194 struct packet_reg
*r
= &rs
->regs
[i
];
3197 if (r
->offset
* 2 >= strlen (buf
))
3198 /* A short packet that didn't include the register's
3199 value, this implies that the register is zero (and
3200 not that the register is unavailable). Supply that
3202 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3203 else if (buf
[r
->offset
* 2] == 'x')
3205 gdb_assert (r
->offset
* 2 < strlen (buf
));
3206 /* The register isn't available, mark it as such (at
3207 the same time setting the value to zero). */
3208 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3209 set_register_cached (i
, -1);
3212 regcache_raw_supply (current_regcache
, r
->regnum
,
3219 /* Prepare to store registers. Since we may send them all (using a
3220 'G' request), we have to read out the ones we don't want to change
3224 remote_prepare_to_store (void)
3226 struct remote_state
*rs
= get_remote_state ();
3228 gdb_byte buf
[MAX_REGISTER_SIZE
];
3230 /* Make sure the entire registers array is valid. */
3231 switch (remote_protocol_packets
[PACKET_P
].support
)
3233 case PACKET_DISABLE
:
3234 case PACKET_SUPPORT_UNKNOWN
:
3235 /* Make sure all the necessary registers are cached. */
3236 for (i
= 0; i
< NUM_REGS
; i
++)
3237 if (rs
->regs
[i
].in_g_packet
)
3238 regcache_raw_read (current_regcache
, rs
->regs
[i
].regnum
, buf
);
3245 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
3246 packet was not recognized. */
3249 store_register_using_P (int regnum
)
3251 struct remote_state
*rs
= get_remote_state ();
3252 struct packet_reg
*reg
= packet_reg_from_regnum (rs
, regnum
);
3253 /* Try storing a single register. */
3254 char *buf
= alloca (rs
->remote_packet_size
);
3255 gdb_byte regp
[MAX_REGISTER_SIZE
];
3258 xsnprintf (buf
, rs
->remote_packet_size
, "P%s=", phex_nz (reg
->pnum
, 0));
3259 p
= buf
+ strlen (buf
);
3260 regcache_raw_collect (current_regcache
, reg
->regnum
, regp
);
3261 bin2hex (regp
, p
, register_size (current_gdbarch
, reg
->regnum
));
3262 remote_send (buf
, rs
->remote_packet_size
);
3264 return buf
[0] != '\0';
3268 /* Store register REGNUM, or all registers if REGNUM == -1, from the
3269 contents of the register cache buffer. FIXME: ignores errors. */
3272 remote_store_registers (int regnum
)
3274 struct remote_state
*rs
= get_remote_state ();
3279 set_thread (PIDGET (inferior_ptid
), 1);
3283 switch (remote_protocol_packets
[PACKET_P
].support
)
3285 case PACKET_DISABLE
:
3288 if (store_register_using_P (regnum
))
3291 error (_("Protocol error: P packet not recognized by stub"));
3292 case PACKET_SUPPORT_UNKNOWN
:
3293 if (store_register_using_P (regnum
))
3295 /* The stub recognized the 'P' packet. Remember this. */
3296 remote_protocol_packets
[PACKET_P
].support
= PACKET_ENABLE
;
3301 /* The stub does not support the 'P' packet. Use 'G'
3302 instead, and don't try using 'P' in the future (it
3303 will just waste our time). */
3304 remote_protocol_packets
[PACKET_P
].support
= PACKET_DISABLE
;
3310 /* Extract all the registers in the regcache copying them into a
3314 regs
= alloca (rs
->sizeof_g_packet
);
3315 memset (regs
, 0, rs
->sizeof_g_packet
);
3316 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
3318 struct packet_reg
*r
= &rs
->regs
[i
];
3320 regcache_raw_collect (current_regcache
, r
->regnum
, regs
+ r
->offset
);
3324 /* Command describes registers byte by byte,
3325 each byte encoded as two hex characters. */
3326 buf
= alloca (rs
->remote_packet_size
);
3329 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3330 bin2hex (regs
, p
, register_bytes_found
);
3331 remote_send (buf
, rs
->remote_packet_size
);
3335 /* Return the number of hex digits in num. */
3338 hexnumlen (ULONGEST num
)
3342 for (i
= 0; num
!= 0; i
++)
3348 /* Set BUF to the minimum number of hex digits representing NUM. */
3351 hexnumstr (char *buf
, ULONGEST num
)
3353 int len
= hexnumlen (num
);
3354 return hexnumnstr (buf
, num
, len
);
3358 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3361 hexnumnstr (char *buf
, ULONGEST num
, int width
)
3367 for (i
= width
- 1; i
>= 0; i
--)
3369 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
3376 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3379 remote_address_masked (CORE_ADDR addr
)
3381 if (remote_address_size
> 0
3382 && remote_address_size
< (sizeof (ULONGEST
) * 8))
3384 /* Only create a mask when that mask can safely be constructed
3385 in a ULONGEST variable. */
3387 mask
= (mask
<< remote_address_size
) - 1;
3393 /* Determine whether the remote target supports binary downloading.
3394 This is accomplished by sending a no-op memory write of zero length
3395 to the target at the specified address. It does not suffice to send
3396 the whole packet, since many stubs strip the eighth bit and
3397 subsequently compute a wrong checksum, which causes real havoc with
3400 NOTE: This can still lose if the serial line is not eight-bit
3401 clean. In cases like this, the user should clear "remote
3405 check_binary_download (CORE_ADDR addr
)
3407 struct remote_state
*rs
= get_remote_state ();
3409 switch (remote_protocol_packets
[PACKET_X
].support
)
3411 case PACKET_DISABLE
:
3415 case PACKET_SUPPORT_UNKNOWN
:
3417 char *buf
= alloca (rs
->remote_packet_size
);
3422 p
+= hexnumstr (p
, (ULONGEST
) addr
);
3424 p
+= hexnumstr (p
, (ULONGEST
) 0);
3428 putpkt_binary (buf
, (int) (p
- buf
));
3429 getpkt (buf
, rs
->remote_packet_size
, 0);
3434 fprintf_unfiltered (gdb_stdlog
,
3435 "binary downloading NOT suppported by target\n");
3436 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
3441 fprintf_unfiltered (gdb_stdlog
,
3442 "binary downloading suppported by target\n");
3443 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
3450 /* Write memory data directly to the remote machine.
3451 This does not inform the data cache; the data cache uses this.
3452 MEMADDR is the address in the remote memory space.
3453 MYADDR is the address of the buffer in our space.
3454 LEN is the number of bytes.
3456 Returns number of bytes transferred, or 0 (setting errno) for
3457 error. Only transfer a single packet. */
3460 remote_write_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
3470 char *payload_start
;
3472 /* Verify that the target can support a binary download. */
3473 check_binary_download (memaddr
);
3475 payload_size
= get_memory_write_packet_size ();
3477 /* Compute the size, and then allocate space for the largest
3478 possible packet. Include space for an extra trailing NUL. */
3479 sizeof_buf
= payload_size
+ 1;
3480 buf
= alloca (sizeof_buf
);
3482 /* Compute the size of the actual payload by subtracting out the
3483 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
3485 payload_size
-= strlen ("$M,:#NN");
3486 payload_size
-= hexnumlen (memaddr
);
3488 /* Construct the packet header: "[MX]<memaddr>,<len>:". */
3490 /* Append "[XM]". Compute a best guess of the number of bytes
3491 actually transfered. */
3493 switch (remote_protocol_packets
[PACKET_X
].support
)
3497 /* Best guess at number of bytes that will fit. */
3498 todo
= min (len
, payload_size
);
3499 payload_size
-= hexnumlen (todo
);
3500 todo
= min (todo
, payload_size
);
3502 case PACKET_DISABLE
:
3504 /* Num bytes that will fit. */
3505 todo
= min (len
, payload_size
/ 2);
3506 payload_size
-= hexnumlen (todo
);
3507 todo
= min (todo
, payload_size
/ 2);
3509 case PACKET_SUPPORT_UNKNOWN
:
3510 internal_error (__FILE__
, __LINE__
,
3511 _("remote_write_bytes: bad internal state"));
3513 internal_error (__FILE__
, __LINE__
, _("bad switch"));
3516 internal_error (__FILE__
, __LINE__
,
3517 _("minumum packet size too small to write data"));
3519 /* Append "<memaddr>". */
3520 memaddr
= remote_address_masked (memaddr
);
3521 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3526 /* Append <len>. Retain the location/size of <len>. It may need to
3527 be adjusted once the packet body has been created. */
3529 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
3536 /* Append the packet body. */
3538 switch (remote_protocol_packets
[PACKET_X
].support
)
3541 /* Binary mode. Send target system values byte by byte, in
3542 increasing byte addresses. Only escape certain critical
3545 (nr_bytes
< todo
) && (p
- payload_start
) < payload_size
;
3548 switch (myaddr
[nr_bytes
] & 0xff)
3553 /* These must be escaped. */
3555 *p
++ = (myaddr
[nr_bytes
] & 0xff) ^ 0x20;
3558 *p
++ = myaddr
[nr_bytes
] & 0xff;
3562 if (nr_bytes
< todo
)
3564 /* Escape chars have filled up the buffer prematurely,
3565 and we have actually sent fewer bytes than planned.
3566 Fix-up the length field of the packet. Use the same
3567 number of characters as before. */
3568 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
3569 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
3572 case PACKET_DISABLE
:
3573 /* Normal mode: Send target system values byte by byte, in
3574 increasing byte addresses. Each byte is encoded as a two hex
3576 nr_bytes
= bin2hex (myaddr
, p
, todo
);
3579 case PACKET_SUPPORT_UNKNOWN
:
3580 internal_error (__FILE__
, __LINE__
,
3581 _("remote_write_bytes: bad internal state"));
3583 internal_error (__FILE__
, __LINE__
, _("bad switch"));
3586 putpkt_binary (buf
, (int) (p
- buf
));
3587 getpkt (buf
, sizeof_buf
, 0);
3591 /* There is no correspondance between what the remote protocol
3592 uses for errors and errno codes. We would like a cleaner way
3593 of representing errors (big enough to include errno codes,
3594 bfd_error codes, and others). But for now just return EIO. */
3599 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
3600 fewer bytes than we'd planned. */
3604 /* Read memory data directly from the remote machine.
3605 This does not use the data cache; the data cache uses this.
3606 MEMADDR is the address in the remote memory space.
3607 MYADDR is the address of the buffer in our space.
3608 LEN is the number of bytes.
3610 Returns number of bytes transferred, or 0 for error. */
3612 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3613 remote targets) shouldn't attempt to read the entire buffer.
3614 Instead it should read a single packet worth of data and then
3615 return the byte size of that packet to the caller. The caller (its
3616 caller and its callers caller ;-) already contains code for
3617 handling partial reads. */
3620 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
3623 int max_buf_size
; /* Max size of packet output buffer. */
3627 /* Create a buffer big enough for this packet. */
3628 max_buf_size
= get_memory_read_packet_size ();
3629 sizeof_buf
= max_buf_size
+ 1; /* Space for trailing NULL. */
3630 buf
= alloca (sizeof_buf
);
3639 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
3641 /* construct "m"<memaddr>","<len>" */
3642 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3643 memaddr
= remote_address_masked (memaddr
);
3646 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3648 p
+= hexnumstr (p
, (ULONGEST
) todo
);
3652 getpkt (buf
, sizeof_buf
, 0);
3655 && isxdigit (buf
[1]) && isxdigit (buf
[2])
3658 /* There is no correspondance between what the remote
3659 protocol uses for errors and errno codes. We would like
3660 a cleaner way of representing errors (big enough to
3661 include errno codes, bfd_error codes, and others). But
3662 for now just return EIO. */
3667 /* Reply describes memory byte by byte,
3668 each byte encoded as two hex characters. */
3671 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
3673 /* Reply is short. This means that we were able to read
3674 only part of what we wanted to. */
3675 return i
+ (origlen
- len
);
3684 /* Read or write LEN bytes from inferior memory at MEMADDR,
3685 transferring to or from debugger address BUFFER. Write to inferior
3686 if SHOULD_WRITE is nonzero. Returns length of data written or
3687 read; 0 for error. TARGET is unused. */
3690 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
3691 int should_write
, struct mem_attrib
*attrib
,
3692 struct target_ops
*target
)
3694 CORE_ADDR targ_addr
;
3698 /* Should this be the selected frame? */
3699 gdbarch_remote_translate_xfer_address (current_gdbarch
,
3702 &targ_addr
, &targ_len
);
3707 res
= remote_write_bytes (targ_addr
, buffer
, targ_len
);
3709 res
= remote_read_bytes (targ_addr
, buffer
, targ_len
);
3715 remote_files_info (struct target_ops
*ignore
)
3717 puts_filtered ("Debugging a target over a serial line.\n");
3720 /* Stuff for dealing with the packets which are part of this protocol.
3721 See comment at top of file for details. */
3723 /* Read a single character from the remote end, masking it down to 7
3727 readchar (int timeout
)
3731 ch
= serial_readchar (remote_desc
, timeout
);
3736 switch ((enum serial_rc
) ch
)
3739 target_mourn_inferior ();
3740 error (_("Remote connection closed"));
3743 perror_with_name (_("Remote communication error"));
3745 case SERIAL_TIMEOUT
:
3751 /* Send the command in BUF to the remote machine, and read the reply
3752 into BUF. Report an error if we get an error reply. */
3755 remote_send (char *buf
,
3759 getpkt (buf
, sizeof_buf
, 0);
3762 error (_("Remote failure reply: %s"), buf
);
3765 /* Display a null-terminated packet on stdout, for debugging, using C
3769 print_packet (char *buf
)
3771 puts_filtered ("\"");
3772 fputstr_filtered (buf
, '"', gdb_stdout
);
3773 puts_filtered ("\"");
3779 return putpkt_binary (buf
, strlen (buf
));
3782 /* Send a packet to the remote machine, with error checking. The data
3783 of the packet is in BUF. The string in BUF can be at most
3784 RS->remote_packet_size - 5 to account for the $, # and checksum,
3785 and for a possible /0 if we are debugging (remote_debug) and want
3786 to print the sent packet as a string. */
3789 putpkt_binary (char *buf
, int cnt
)
3791 struct remote_state
*rs
= get_remote_state ();
3793 unsigned char csum
= 0;
3794 char *buf2
= alloca (cnt
+ 6);
3795 long sizeof_junkbuf
= rs
->remote_packet_size
;
3796 char *junkbuf
= alloca (sizeof_junkbuf
);
3802 /* Copy the packet into buffer BUF2, encapsulating it
3803 and giving it a checksum. */
3808 for (i
= 0; i
< cnt
; i
++)
3814 *p
++ = tohex ((csum
>> 4) & 0xf);
3815 *p
++ = tohex (csum
& 0xf);
3817 /* Send it over and over until we get a positive ack. */
3821 int started_error_output
= 0;
3826 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
3827 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
3828 fprintf_unfiltered (gdb_stdlog
, "...");
3829 gdb_flush (gdb_stdlog
);
3831 if (serial_write (remote_desc
, buf2
, p
- buf2
))
3832 perror_with_name (_("putpkt: write failed"));
3834 /* Read until either a timeout occurs (-2) or '+' is read. */
3837 ch
= readchar (remote_timeout
);
3845 case SERIAL_TIMEOUT
:
3847 if (started_error_output
)
3849 putchar_unfiltered ('\n');
3850 started_error_output
= 0;
3859 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
3863 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
3864 case SERIAL_TIMEOUT
:
3868 break; /* Retransmit buffer. */
3872 fprintf_unfiltered (gdb_stdlog
,
3873 "Packet instead of Ack, ignoring it\n");
3874 /* It's probably an old response sent because an ACK
3875 was lost. Gobble up the packet and ack it so it
3876 doesn't get retransmitted when we resend this
3878 read_frame (junkbuf
, sizeof_junkbuf
);
3879 serial_write (remote_desc
, "+", 1);
3880 continue; /* Now, go look for +. */
3885 if (!started_error_output
)
3887 started_error_output
= 1;
3888 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
3890 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
3894 break; /* Here to retransmit. */
3898 /* This is wrong. If doing a long backtrace, the user should be
3899 able to get out next time we call QUIT, without anything as
3900 violent as interrupt_query. If we want to provide a way out of
3901 here without getting to the next QUIT, it should be based on
3902 hitting ^C twice as in remote_wait. */
3912 /* Come here after finding the start of the frame. Collect the rest
3913 into BUF, verifying the checksum, length, and handling run-length
3914 compression. No more than sizeof_buf-1 characters are read so that
3915 the buffer can be NUL terminated.
3917 Returns -1 on error, number of characters in buffer (ignoring the
3918 trailing NULL) on success. (could be extended to return one of the
3919 SERIAL status indications). */
3922 read_frame (char *buf
,
3934 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NULL. */
3935 c
= readchar (remote_timeout
);
3938 case SERIAL_TIMEOUT
:
3940 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
3944 fputs_filtered ("Saw new packet start in middle of old one\n",
3946 return -1; /* Start a new packet, count retries. */
3949 unsigned char pktcsum
;
3955 check_0
= readchar (remote_timeout
);
3957 check_1
= readchar (remote_timeout
);
3959 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
3962 fputs_filtered ("Timeout in checksum, retrying\n",
3966 else if (check_0
< 0 || check_1
< 0)
3969 fputs_filtered ("Communication error in checksum\n",
3974 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
3975 if (csum
== pktcsum
)
3980 fprintf_filtered (gdb_stdlog
,
3981 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
3983 fputs_filtered (buf
, gdb_stdlog
);
3984 fputs_filtered ("\n", gdb_stdlog
);
3986 /* Number of characters in buffer ignoring trailing
3990 case '*': /* Run length encoding. */
3995 c
= readchar (remote_timeout
);
3997 repeat
= c
- ' ' + 3; /* Compute repeat count. */
3999 /* The character before ``*'' is repeated. */
4001 if (repeat
> 0 && repeat
<= 255
4003 && bc
+ repeat
- 1 < sizeof_buf
- 1)
4005 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
4011 printf_filtered (_("Repeat count %d too large for buffer: "),
4013 puts_filtered (buf
);
4014 puts_filtered ("\n");
4018 if (bc
< sizeof_buf
- 1)
4026 puts_filtered ("Remote packet too long: ");
4027 puts_filtered (buf
);
4028 puts_filtered ("\n");
4035 /* Read a packet from the remote machine, with error checking, and
4036 store it in BUF. If FOREVER, wait forever rather than timing out;
4037 this is used (in synchronous mode) to wait for a target that is is
4038 executing user code to stop. */
4039 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4040 don't have to change all the calls to getpkt to deal with the
4041 return value, because at the moment I don't know what the right
4042 thing to do it for those. */
4050 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
4054 /* Read a packet from the remote machine, with error checking, and
4055 store it in BUF. If FOREVER, wait forever rather than timing out;
4056 this is used (in synchronous mode) to wait for a target that is is
4057 executing user code to stop. If FOREVER == 0, this function is
4058 allowed to time out gracefully and return an indication of this to
4061 getpkt_sane (char *buf
,
4070 strcpy (buf
, "timeout");
4074 timeout
= watchdog
> 0 ? watchdog
: -1;
4078 timeout
= remote_timeout
;
4082 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
4084 /* This can loop forever if the remote side sends us characters
4085 continuously, but if it pauses, we'll get a zero from
4086 readchar because of timeout. Then we'll count that as a
4089 /* Note that we will only wait forever prior to the start of a
4090 packet. After that, we expect characters to arrive at a
4091 brisk pace. They should show up within remote_timeout
4096 c
= readchar (timeout
);
4098 if (c
== SERIAL_TIMEOUT
)
4100 if (forever
) /* Watchdog went off? Kill the target. */
4103 target_mourn_inferior ();
4104 error (_("Watchdog has expired. Target detached."));
4107 fputs_filtered ("Timed out.\n", gdb_stdlog
);
4113 /* We've found the start of a packet, now collect the data. */
4115 val
= read_frame (buf
, sizeof_buf
);
4121 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
4122 fputstr_unfiltered (buf
, 0, gdb_stdlog
);
4123 fprintf_unfiltered (gdb_stdlog
, "\n");
4125 serial_write (remote_desc
, "+", 1);
4129 /* Try the whole thing again. */
4131 serial_write (remote_desc
, "-", 1);
4134 /* We have tried hard enough, and just can't receive the packet.
4137 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
4138 serial_write (remote_desc
, "+", 1);
4145 /* For some mysterious reason, wait_for_inferior calls kill instead of
4146 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4150 target_mourn_inferior ();
4154 /* Use catch_errors so the user can quit from gdb even when we aren't on
4155 speaking terms with the remote system. */
4156 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4158 /* Don't wait for it to die. I'm not really sure it matters whether
4159 we do or not. For the existing stubs, kill is a noop. */
4160 target_mourn_inferior ();
4163 /* Async version of remote_kill. */
4165 remote_async_kill (void)
4167 /* Unregister the file descriptor from the event loop. */
4168 if (target_is_async_p ())
4169 serial_async (remote_desc
, NULL
, 0);
4171 /* For some mysterious reason, wait_for_inferior calls kill instead of
4172 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4176 target_mourn_inferior ();
4180 /* Use catch_errors so the user can quit from gdb even when we
4181 aren't on speaking terms with the remote system. */
4182 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4184 /* Don't wait for it to die. I'm not really sure it matters whether
4185 we do or not. For the existing stubs, kill is a noop. */
4186 target_mourn_inferior ();
4192 remote_mourn_1 (&remote_ops
);
4196 remote_async_mourn (void)
4198 remote_mourn_1 (&remote_async_ops
);
4202 extended_remote_mourn (void)
4204 /* We do _not_ want to mourn the target like this; this will
4205 remove the extended remote target from the target stack,
4206 and the next time the user says "run" it'll fail.
4208 FIXME: What is the right thing to do here? */
4210 remote_mourn_1 (&extended_remote_ops
);
4214 /* Worker function for remote_mourn. */
4216 remote_mourn_1 (struct target_ops
*target
)
4218 unpush_target (target
);
4219 generic_mourn_inferior ();
4222 /* In the extended protocol we want to be able to do things like
4223 "run" and have them basically work as expected. So we need
4224 a special create_inferior function.
4226 FIXME: One day add support for changing the exec file
4227 we're debugging, arguments and an environment. */
4230 extended_remote_create_inferior (char *exec_file
, char *args
,
4231 char **env
, int from_tty
)
4233 /* Rip out the breakpoints; we'll reinsert them after restarting
4234 the remote server. */
4235 remove_breakpoints ();
4237 /* Now restart the remote server. */
4238 extended_remote_restart ();
4240 /* Now put the breakpoints back in. This way we're safe if the
4241 restart function works via a unix fork on the remote side. */
4242 insert_breakpoints ();
4244 /* Clean up from the last time we were running. */
4245 clear_proceed_status ();
4248 /* Async version of extended_remote_create_inferior. */
4250 extended_remote_async_create_inferior (char *exec_file
, char *args
,
4251 char **env
, int from_tty
)
4253 /* Rip out the breakpoints; we'll reinsert them after restarting
4254 the remote server. */
4255 remove_breakpoints ();
4257 /* If running asynchronously, register the target file descriptor
4258 with the event loop. */
4259 if (target_can_async_p ())
4260 target_async (inferior_event_handler
, 0);
4262 /* Now restart the remote server. */
4263 extended_remote_restart ();
4265 /* Now put the breakpoints back in. This way we're safe if the
4266 restart function works via a unix fork on the remote side. */
4267 insert_breakpoints ();
4269 /* Clean up from the last time we were running. */
4270 clear_proceed_status ();
4274 /* On some machines, e.g. 68k, we may use a different breakpoint
4275 instruction than other targets; in those use
4276 DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4277 Also, bi-endian targets may define
4278 DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4279 DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we
4280 just call the standard routines that are in mem-break.c. */
4282 /* NOTE: cagney/2003-06-08: This is silly. A remote and simulator
4283 target should use an identical BREAKPOINT_FROM_PC. As for native,
4284 the ARCH-OS-tdep.c code can override the default. */
4286 #if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4287 #define DEPRECATED_REMOTE_BREAKPOINT
4290 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4292 /* If the target isn't bi-endian, just pretend it is. */
4293 #if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4294 #define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4295 #define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4298 static unsigned char big_break_insn
[] = DEPRECATED_BIG_REMOTE_BREAKPOINT
;
4299 static unsigned char little_break_insn
[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT
;
4301 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4303 /* Insert a breakpoint on targets that don't have any better
4304 breakpoint support. We read the contents of the target location
4305 and stash it, then overwrite it with a breakpoint instruction.
4306 ADDR is the target location in the target machine. CONTENTS_CACHE
4307 is a pointer to memory allocated for saving the target contents.
4308 It is guaranteed by the caller to be long enough to save the number
4309 of bytes returned by BREAKPOINT_FROM_PC. */
4312 remote_insert_breakpoint (CORE_ADDR addr
, bfd_byte
*contents_cache
)
4314 struct remote_state
*rs
= get_remote_state ();
4315 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4320 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4321 If it succeeds, then set the support to PACKET_ENABLE. If it
4322 fails, and the user has explicitly requested the Z support then
4323 report an error, otherwise, mark it disabled and go on. */
4325 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
4327 char *buf
= alloca (rs
->remote_packet_size
);
4330 addr
= remote_address_masked (addr
);
4334 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4335 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
4336 sprintf (p
, ",%d", bp_size
);
4339 getpkt (buf
, rs
->remote_packet_size
, 0);
4341 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_Z0
]))
4347 case PACKET_UNKNOWN
:
4352 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4353 val
= target_read_memory (addr
, contents_cache
, sizeof big_break_insn
);
4357 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
4358 val
= target_write_memory (addr
, (char *) big_break_insn
,
4359 sizeof big_break_insn
);
4361 val
= target_write_memory (addr
, (char *) little_break_insn
,
4362 sizeof little_break_insn
);
4367 return memory_insert_breakpoint (addr
, contents_cache
);
4368 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4372 remote_remove_breakpoint (CORE_ADDR addr
, bfd_byte
*contents_cache
)
4374 struct remote_state
*rs
= get_remote_state ();
4377 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
4379 char *buf
= alloca (rs
->remote_packet_size
);
4386 addr
= remote_address_masked (addr
);
4387 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4388 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
4389 sprintf (p
, ",%d", bp_size
);
4392 getpkt (buf
, rs
->remote_packet_size
, 0);
4394 return (buf
[0] == 'E');
4397 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4398 return target_write_memory (addr
, contents_cache
, sizeof big_break_insn
);
4400 return memory_remove_breakpoint (addr
, contents_cache
);
4401 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4405 watchpoint_to_Z_packet (int type
)
4419 internal_error (__FILE__
, __LINE__
,
4420 _("hw_bp_to_z: bad watchpoint type %d"), type
);
4425 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
4427 struct remote_state
*rs
= get_remote_state ();
4428 char *buf
= alloca (rs
->remote_packet_size
);
4430 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
4432 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
4433 error (_("Can't set hardware watchpoints without the '%s' (%s) packet."),
4434 remote_protocol_packets
[PACKET_Z0
+ packet
].name
,
4435 remote_protocol_packets
[PACKET_Z0
+ packet
].title
);
4437 sprintf (buf
, "Z%x,", packet
);
4438 p
= strchr (buf
, '\0');
4439 addr
= remote_address_masked (addr
);
4440 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4441 sprintf (p
, ",%x", len
);
4444 getpkt (buf
, rs
->remote_packet_size
, 0);
4446 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
4449 case PACKET_UNKNOWN
:
4454 internal_error (__FILE__
, __LINE__
,
4455 _("remote_insert_watchpoint: reached end of function"));
4460 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
4462 struct remote_state
*rs
= get_remote_state ();
4463 char *buf
= alloca (rs
->remote_packet_size
);
4465 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
4467 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
4468 error (_("Can't clear hardware watchpoints without the '%s' (%s) packet."),
4469 remote_protocol_packets
[PACKET_Z0
+ packet
].name
,
4470 remote_protocol_packets
[PACKET_Z0
+ packet
].title
);
4472 sprintf (buf
, "z%x,", packet
);
4473 p
= strchr (buf
, '\0');
4474 addr
= remote_address_masked (addr
);
4475 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4476 sprintf (p
, ",%x", len
);
4478 getpkt (buf
, rs
->remote_packet_size
, 0);
4480 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
4483 case PACKET_UNKNOWN
:
4488 internal_error (__FILE__
, __LINE__
,
4489 _("remote_remove_watchpoint: reached end of function"));
4493 int remote_hw_watchpoint_limit
= -1;
4494 int remote_hw_breakpoint_limit
= -1;
4497 remote_check_watch_resources (int type
, int cnt
, int ot
)
4499 if (type
== bp_hardware_breakpoint
)
4501 if (remote_hw_breakpoint_limit
== 0)
4503 else if (remote_hw_breakpoint_limit
< 0)
4505 else if (cnt
<= remote_hw_breakpoint_limit
)
4510 if (remote_hw_watchpoint_limit
== 0)
4512 else if (remote_hw_watchpoint_limit
< 0)
4516 else if (cnt
<= remote_hw_watchpoint_limit
)
4523 remote_stopped_by_watchpoint (void)
4525 return remote_stopped_by_watchpoint_p
;
4528 extern int stepped_after_stopped_by_watchpoint
;
4531 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
4534 if (remote_stopped_by_watchpoint ()
4535 || stepped_after_stopped_by_watchpoint
)
4537 *addr_p
= remote_watch_data_address
;
4546 remote_insert_hw_breakpoint (CORE_ADDR addr
, gdb_byte
*shadow
)
4549 struct remote_state
*rs
= get_remote_state ();
4550 char *buf
= alloca (rs
->remote_packet_size
);
4553 /* The length field should be set to the size of a breakpoint
4556 BREAKPOINT_FROM_PC (&addr
, &len
);
4558 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
4559 error (_("Can't set hardware breakpoint without the '%s' (%s) packet."),
4560 remote_protocol_packets
[PACKET_Z1
].name
,
4561 remote_protocol_packets
[PACKET_Z1
].title
);
4567 addr
= remote_address_masked (addr
);
4568 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4569 sprintf (p
, ",%x", len
);
4572 getpkt (buf
, rs
->remote_packet_size
, 0);
4574 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_Z1
]))
4577 case PACKET_UNKNOWN
:
4582 internal_error (__FILE__
, __LINE__
,
4583 _("remote_insert_hw_breakpoint: reached end of function"));
4588 remote_remove_hw_breakpoint (CORE_ADDR addr
, gdb_byte
*shadow
)
4591 struct remote_state
*rs
= get_remote_state ();
4592 char *buf
= alloca (rs
->remote_packet_size
);
4595 /* The length field should be set to the size of a breakpoint
4598 BREAKPOINT_FROM_PC (&addr
, &len
);
4600 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
4601 error (_("Can't clear hardware breakpoint without the '%s' (%s) packet."),
4602 remote_protocol_packets
[PACKET_Z1
].name
,
4603 remote_protocol_packets
[PACKET_Z1
].title
);
4609 addr
= remote_address_masked (addr
);
4610 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4611 sprintf (p
, ",%x", len
);
4614 getpkt (buf
, rs
->remote_packet_size
, 0);
4616 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_Z1
]))
4619 case PACKET_UNKNOWN
:
4624 internal_error (__FILE__
, __LINE__
,
4625 _("remote_remove_hw_breakpoint: reached end of function"));
4628 /* Some targets are only capable of doing downloads, and afterwards
4629 they switch to the remote serial protocol. This function provides
4630 a clean way to get from the download target to the remote target.
4631 It's basically just a wrapper so that we don't have to expose any
4632 of the internal workings of remote.c.
4634 Prior to calling this routine, you should shutdown the current
4635 target code, else you will get the "A program is being debugged
4636 already..." message. Usually a call to pop_target() suffices. */
4639 push_remote_target (char *name
, int from_tty
)
4641 printf_filtered (_("Switching to remote protocol\n"));
4642 remote_open (name
, from_tty
);
4645 /* Table used by the crc32 function to calcuate the checksum. */
4647 static unsigned long crc32_table
[256] =
4650 static unsigned long
4651 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
4653 if (!crc32_table
[1])
4655 /* Initialize the CRC table and the decoding table. */
4659 for (i
= 0; i
< 256; i
++)
4661 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
4662 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
4669 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
4675 /* compare-sections command
4677 With no arguments, compares each loadable section in the exec bfd
4678 with the same memory range on the target, and reports mismatches.
4679 Useful for verifying the image on the target against the exec file.
4680 Depends on the target understanding the new "qCRC:" request. */
4682 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4683 target method (target verify memory) and generic version of the
4684 actual command. This will allow other high-level code (especially
4685 generic_load()) to make use of this target functionality. */
4688 compare_sections_command (char *args
, int from_tty
)
4690 struct remote_state
*rs
= get_remote_state ();
4692 unsigned long host_crc
, target_crc
;
4693 extern bfd
*exec_bfd
;
4694 struct cleanup
*old_chain
;
4697 const char *sectname
;
4698 char *buf
= alloca (rs
->remote_packet_size
);
4705 error (_("command cannot be used without an exec file"));
4706 if (!current_target
.to_shortname
||
4707 strcmp (current_target
.to_shortname
, "remote") != 0)
4708 error (_("command can only be used with remote target"));
4710 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
4712 if (!(s
->flags
& SEC_LOAD
))
4713 continue; /* skip non-loadable section */
4715 size
= bfd_get_section_size (s
);
4717 continue; /* skip zero-length section */
4719 sectname
= bfd_get_section_name (exec_bfd
, s
);
4720 if (args
&& strcmp (args
, sectname
) != 0)
4721 continue; /* not the section selected by user */
4723 matched
= 1; /* do this section */
4725 /* FIXME: assumes lma can fit into long. */
4726 xsnprintf (buf
, rs
->remote_packet_size
, "qCRC:%lx,%lx",
4727 (long) lma
, (long) size
);
4730 /* Be clever; compute the host_crc before waiting for target
4732 sectdata
= xmalloc (size
);
4733 old_chain
= make_cleanup (xfree
, sectdata
);
4734 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
4735 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
4737 getpkt (buf
, rs
->remote_packet_size
, 0);
4739 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
4740 sectname
, paddr (lma
), paddr (lma
+ size
));
4742 error (_("remote target does not support this operation"));
4744 for (target_crc
= 0, tmp
= &buf
[1]; *tmp
; tmp
++)
4745 target_crc
= target_crc
* 16 + fromhex (*tmp
);
4747 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4748 sectname
, paddr (lma
), paddr (lma
+ size
));
4749 if (host_crc
== target_crc
)
4750 printf_filtered ("matched.\n");
4753 printf_filtered ("MIS-MATCHED!\n");
4757 do_cleanups (old_chain
);
4760 warning (_("One or more sections of the remote executable does not match\n\
4761 the loaded file\n"));
4762 if (args
&& !matched
)
4763 printf_filtered (_("No loaded section named '%s'.\n"), args
);
4767 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4768 const char *annex
, gdb_byte
*readbuf
,
4769 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4771 struct remote_state
*rs
= get_remote_state ();
4773 char *buf2
= alloca (rs
->remote_packet_size
);
4774 char *p2
= &buf2
[0];
4777 /* Handle memory using remote_xfer_memory. */
4778 if (object
== TARGET_OBJECT_MEMORY
)
4783 if (writebuf
!= NULL
)
4785 void *buffer
= xmalloc (len
);
4786 struct cleanup
*cleanup
= make_cleanup (xfree
, buffer
);
4787 memcpy (buffer
, writebuf
, len
);
4788 xfered
= remote_xfer_memory (offset
, buffer
, len
, 1, NULL
, ops
);
4789 do_cleanups (cleanup
);
4792 xfered
= remote_xfer_memory (offset
, readbuf
, len
, 0, NULL
, ops
);
4796 else if (xfered
== 0 && errno
== 0)
4802 /* Only handle reads. */
4803 if (writebuf
!= NULL
|| readbuf
== NULL
)
4806 /* Map pre-existing objects onto letters. DO NOT do this for new
4807 objects!!! Instead specify new query packets. */
4810 case TARGET_OBJECT_KOD
:
4813 case TARGET_OBJECT_AVR
:
4817 case TARGET_OBJECT_AUXV
:
4818 if (remote_protocol_packets
[PACKET_qPart_auxv
].support
!= PACKET_DISABLE
)
4820 unsigned int total
= 0;
4823 LONGEST n
= min ((rs
->remote_packet_size
- 2) / 2, len
);
4824 snprintf (buf2
, rs
->remote_packet_size
,
4825 "qPart:auxv:read::%s,%s",
4826 phex_nz (offset
, sizeof offset
),
4827 phex_nz (n
, sizeof n
));
4830 return total
> 0 ? total
: i
;
4832 getpkt (buf2
, rs
->remote_packet_size
, 0);
4833 if (packet_ok (buf2
, &remote_protocol_packets
[PACKET_qPart_auxv
])
4835 return total
> 0 ? total
: -1;
4836 if (buf2
[0] == 'O' && buf2
[1] == 'K' && buf2
[2] == '\0')
4837 break; /* Got EOF indicator. */
4838 /* Got some data. */
4839 i
= hex2bin (buf2
, readbuf
, len
);
4842 readbuf
= (void *) ((char *) readbuf
+ i
);
4856 /* Note: a zero OFFSET and LEN can be used to query the minimum
4858 if (offset
== 0 && len
== 0)
4859 return (rs
->remote_packet_size
);
4860 /* Minimum outbuf size is RS->remote_packet_size. If LEN is not
4861 large enough let the caller deal with it. */
4862 if (len
< rs
->remote_packet_size
)
4864 len
= rs
->remote_packet_size
;
4866 /* Except for querying the minimum buffer size, target must be open. */
4868 error (_("remote query is only available after target open"));
4870 gdb_assert (annex
!= NULL
);
4871 gdb_assert (readbuf
!= NULL
);
4876 /* We used one buffer char for the remote protocol q command and
4877 another for the query type. As the remote protocol encapsulation
4878 uses 4 chars plus one extra in case we are debugging
4879 (remote_debug), we have PBUFZIZ - 7 left to pack the query
4882 while (annex
[i
] && (i
< (rs
->remote_packet_size
- 8)))
4884 /* Bad caller may have sent forbidden characters. */
4885 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
4890 gdb_assert (annex
[i
] == '\0');
4896 getpkt ((char *) readbuf
, len
, 0);
4898 return strlen ((char *) readbuf
);
4902 remote_rcmd (char *command
,
4903 struct ui_file
*outbuf
)
4905 struct remote_state
*rs
= get_remote_state ();
4906 char *buf
= alloca (rs
->remote_packet_size
);
4910 error (_("remote rcmd is only available after target open"));
4912 /* Send a NULL command across as an empty command. */
4913 if (command
== NULL
)
4916 /* The query prefix. */
4917 strcpy (buf
, "qRcmd,");
4918 p
= strchr (buf
, '\0');
4920 if ((strlen (buf
) + strlen (command
) * 2 + 8/*misc*/) > rs
->remote_packet_size
)
4921 error (_("\"monitor\" command ``%s'' is too long."), command
);
4923 /* Encode the actual command. */
4924 bin2hex ((gdb_byte
*) command
, p
, 0);
4926 if (putpkt (buf
) < 0)
4927 error (_("Communication problem with target."));
4929 /* get/display the response */
4932 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
4934 getpkt (buf
, rs
->remote_packet_size
, 0);
4936 error (_("Target does not support this command."));
4937 if (buf
[0] == 'O' && buf
[1] != 'K')
4939 remote_console_output (buf
+ 1); /* 'O' message from stub. */
4942 if (strcmp (buf
, "OK") == 0)
4944 if (strlen (buf
) == 3 && buf
[0] == 'E'
4945 && isdigit (buf
[1]) && isdigit (buf
[2]))
4947 error (_("Protocol error with Rcmd"));
4949 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
4951 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
4952 fputc_unfiltered (c
, outbuf
);
4959 packet_command (char *args
, int from_tty
)
4961 struct remote_state
*rs
= get_remote_state ();
4962 char *buf
= alloca (rs
->remote_packet_size
);
4965 error (_("command can only be used with remote target"));
4968 error (_("remote-packet command requires packet text as argument"));
4970 puts_filtered ("sending: ");
4971 print_packet (args
);
4972 puts_filtered ("\n");
4975 getpkt (buf
, rs
->remote_packet_size
, 0);
4976 puts_filtered ("received: ");
4978 puts_filtered ("\n");
4982 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
4984 static void display_thread_info (struct gdb_ext_thread_info
*info
);
4986 static void threadset_test_cmd (char *cmd
, int tty
);
4988 static void threadalive_test (char *cmd
, int tty
);
4990 static void threadlist_test_cmd (char *cmd
, int tty
);
4992 int get_and_display_threadinfo (threadref
*ref
);
4994 static void threadinfo_test_cmd (char *cmd
, int tty
);
4996 static int thread_display_step (threadref
*ref
, void *context
);
4998 static void threadlist_update_test_cmd (char *cmd
, int tty
);
5000 static void init_remote_threadtests (void);
5002 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
5005 threadset_test_cmd (char *cmd
, int tty
)
5007 int sample_thread
= SAMPLE_THREAD
;
5009 printf_filtered (_("Remote threadset test\n"));
5010 set_thread (sample_thread
, 1);
5015 threadalive_test (char *cmd
, int tty
)
5017 int sample_thread
= SAMPLE_THREAD
;
5019 if (remote_thread_alive (pid_to_ptid (sample_thread
)))
5020 printf_filtered ("PASS: Thread alive test\n");
5022 printf_filtered ("FAIL: Thread alive test\n");
5025 void output_threadid (char *title
, threadref
*ref
);
5028 output_threadid (char *title
, threadref
*ref
)
5032 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
5034 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
5038 threadlist_test_cmd (char *cmd
, int tty
)
5041 threadref nextthread
;
5042 int done
, result_count
;
5043 threadref threadlist
[3];
5045 printf_filtered ("Remote Threadlist test\n");
5046 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
5047 &result_count
, &threadlist
[0]))
5048 printf_filtered ("FAIL: threadlist test\n");
5051 threadref
*scan
= threadlist
;
5052 threadref
*limit
= scan
+ result_count
;
5054 while (scan
< limit
)
5055 output_threadid (" thread ", scan
++);
5060 display_thread_info (struct gdb_ext_thread_info
*info
)
5062 output_threadid ("Threadid: ", &info
->threadid
);
5063 printf_filtered ("Name: %s\n ", info
->shortname
);
5064 printf_filtered ("State: %s\n", info
->display
);
5065 printf_filtered ("other: %s\n\n", info
->more_display
);
5069 get_and_display_threadinfo (threadref
*ref
)
5073 struct gdb_ext_thread_info threadinfo
;
5075 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
5076 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
5077 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
5078 display_thread_info (&threadinfo
);
5083 threadinfo_test_cmd (char *cmd
, int tty
)
5085 int athread
= SAMPLE_THREAD
;
5089 int_to_threadref (&thread
, athread
);
5090 printf_filtered ("Remote Threadinfo test\n");
5091 if (!get_and_display_threadinfo (&thread
))
5092 printf_filtered ("FAIL cannot get thread info\n");
5096 thread_display_step (threadref
*ref
, void *context
)
5098 /* output_threadid(" threadstep ",ref); *//* simple test */
5099 return get_and_display_threadinfo (ref
);
5103 threadlist_update_test_cmd (char *cmd
, int tty
)
5105 printf_filtered ("Remote Threadlist update test\n");
5106 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
5110 init_remote_threadtests (void)
5112 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
5113 Fetch and print the remote list of thread identifiers, one pkt only"));
5114 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
5115 _("Fetch and display info about one thread"));
5116 add_com ("tset", class_obscure
, threadset_test_cmd
,
5117 _("Test setting to a different thread"));
5118 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
5119 _("Iterate through updating all remote thread info"));
5120 add_com ("talive", class_obscure
, threadalive_test
,
5121 _(" Remote thread alive test "));
5126 /* Convert a thread ID to a string. Returns the string in a static
5130 remote_pid_to_str (ptid_t ptid
)
5132 static char buf
[32];
5134 xsnprintf (buf
, sizeof buf
, "thread %d", ptid_get_pid (ptid
));
5138 /* Get the address of the thread local variable in OBJFILE which is
5139 stored at OFFSET within the thread local storage for thread PTID. */
5142 remote_get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
5144 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
5146 struct remote_state
*rs
= get_remote_state ();
5147 char *buf
= alloca (rs
->remote_packet_size
);
5149 enum packet_result result
;
5151 strcpy (p
, "qGetTLSAddr:");
5153 p
+= hexnumstr (p
, PIDGET (ptid
));
5155 p
+= hexnumstr (p
, offset
);
5157 p
+= hexnumstr (p
, lm
);
5161 getpkt (buf
, rs
->remote_packet_size
, 0);
5162 result
= packet_ok (buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
5163 if (result
== PACKET_OK
)
5167 unpack_varlen_hex (buf
, &result
);
5170 else if (result
== PACKET_UNKNOWN
)
5171 throw_error (TLS_GENERIC_ERROR
,
5172 _("Remote target doesn't support qGetTLSAddr packet"));
5174 throw_error (TLS_GENERIC_ERROR
,
5175 _("Remote target failed to process qGetTLSAddr request"));
5178 throw_error (TLS_GENERIC_ERROR
,
5179 _("TLS not supported or disabled on this target"));
5185 init_remote_ops (void)
5187 remote_ops
.to_shortname
= "remote";
5188 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
5190 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5191 Specify the serial device it is connected to\n\
5192 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
5193 remote_ops
.to_open
= remote_open
;
5194 remote_ops
.to_close
= remote_close
;
5195 remote_ops
.to_detach
= remote_detach
;
5196 remote_ops
.to_disconnect
= remote_disconnect
;
5197 remote_ops
.to_resume
= remote_resume
;
5198 remote_ops
.to_wait
= remote_wait
;
5199 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
5200 remote_ops
.to_store_registers
= remote_store_registers
;
5201 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5202 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
5203 remote_ops
.to_files_info
= remote_files_info
;
5204 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5205 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5206 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
5207 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
5208 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
5209 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
5210 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
5211 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
5212 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
5213 remote_ops
.to_kill
= remote_kill
;
5214 remote_ops
.to_load
= generic_load
;
5215 remote_ops
.to_mourn_inferior
= remote_mourn
;
5216 remote_ops
.to_thread_alive
= remote_thread_alive
;
5217 remote_ops
.to_find_new_threads
= remote_threads_info
;
5218 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
5219 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
5220 remote_ops
.to_stop
= remote_stop
;
5221 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
5222 remote_ops
.to_rcmd
= remote_rcmd
;
5223 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
5224 remote_ops
.to_stratum
= process_stratum
;
5225 remote_ops
.to_has_all_memory
= 1;
5226 remote_ops
.to_has_memory
= 1;
5227 remote_ops
.to_has_stack
= 1;
5228 remote_ops
.to_has_registers
= 1;
5229 remote_ops
.to_has_execution
= 1;
5230 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5231 remote_ops
.to_magic
= OPS_MAGIC
;
5234 /* Set up the extended remote vector by making a copy of the standard
5235 remote vector and adding to it. */
5238 init_extended_remote_ops (void)
5240 extended_remote_ops
= remote_ops
;
5242 extended_remote_ops
.to_shortname
= "extended-remote";
5243 extended_remote_ops
.to_longname
=
5244 "Extended remote serial target in gdb-specific protocol";
5245 extended_remote_ops
.to_doc
=
5246 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5247 Specify the serial device it is connected to (e.g. /dev/ttya).",
5248 extended_remote_ops
.to_open
= extended_remote_open
;
5249 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
5250 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5254 remote_can_async_p (void)
5256 /* We're async whenever the serial device is. */
5257 return (current_target
.to_async_mask_value
) && serial_can_async_p (remote_desc
);
5261 remote_is_async_p (void)
5263 /* We're async whenever the serial device is. */
5264 return (current_target
.to_async_mask_value
) && serial_is_async_p (remote_desc
);
5267 /* Pass the SERIAL event on and up to the client. One day this code
5268 will be able to delay notifying the client of an event until the
5269 point where an entire packet has been received. */
5271 static void (*async_client_callback
) (enum inferior_event_type event_type
,
5273 static void *async_client_context
;
5274 static serial_event_ftype remote_async_serial_handler
;
5277 remote_async_serial_handler (struct serial
*scb
, void *context
)
5279 /* Don't propogate error information up to the client. Instead let
5280 the client find out about the error by querying the target. */
5281 async_client_callback (INF_REG_EVENT
, async_client_context
);
5285 remote_async (void (*callback
) (enum inferior_event_type event_type
,
5286 void *context
), void *context
)
5288 if (current_target
.to_async_mask_value
== 0)
5289 internal_error (__FILE__
, __LINE__
,
5290 _("Calling remote_async when async is masked"));
5292 if (callback
!= NULL
)
5294 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
5295 async_client_callback
= callback
;
5296 async_client_context
= context
;
5299 serial_async (remote_desc
, NULL
, NULL
);
5302 /* Target async and target extended-async.
5304 This are temporary targets, until it is all tested. Eventually
5305 async support will be incorporated int the usual 'remote'
5309 init_remote_async_ops (void)
5311 remote_async_ops
.to_shortname
= "async";
5312 remote_async_ops
.to_longname
=
5313 "Remote serial target in async version of the gdb-specific protocol";
5314 remote_async_ops
.to_doc
=
5315 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5316 Specify the serial device it is connected to (e.g. /dev/ttya).";
5317 remote_async_ops
.to_open
= remote_async_open
;
5318 remote_async_ops
.to_close
= remote_close
;
5319 remote_async_ops
.to_detach
= remote_detach
;
5320 remote_async_ops
.to_disconnect
= remote_disconnect
;
5321 remote_async_ops
.to_resume
= remote_async_resume
;
5322 remote_async_ops
.to_wait
= remote_async_wait
;
5323 remote_async_ops
.to_fetch_registers
= remote_fetch_registers
;
5324 remote_async_ops
.to_store_registers
= remote_store_registers
;
5325 remote_async_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5326 remote_async_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
5327 remote_async_ops
.to_files_info
= remote_files_info
;
5328 remote_async_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5329 remote_async_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5330 remote_async_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
5331 remote_async_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
5332 remote_async_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
5333 remote_async_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
5334 remote_async_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
5335 remote_async_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
5336 remote_async_ops
.to_stopped_data_address
= remote_stopped_data_address
;
5337 remote_async_ops
.to_terminal_inferior
= remote_async_terminal_inferior
;
5338 remote_async_ops
.to_terminal_ours
= remote_async_terminal_ours
;
5339 remote_async_ops
.to_kill
= remote_async_kill
;
5340 remote_async_ops
.to_load
= generic_load
;
5341 remote_async_ops
.to_mourn_inferior
= remote_async_mourn
;
5342 remote_async_ops
.to_thread_alive
= remote_thread_alive
;
5343 remote_async_ops
.to_find_new_threads
= remote_threads_info
;
5344 remote_async_ops
.to_pid_to_str
= remote_pid_to_str
;
5345 remote_async_ops
.to_extra_thread_info
= remote_threads_extra_info
;
5346 remote_async_ops
.to_stop
= remote_stop
;
5347 remote_async_ops
.to_xfer_partial
= remote_xfer_partial
;
5348 remote_async_ops
.to_rcmd
= remote_rcmd
;
5349 remote_async_ops
.to_stratum
= process_stratum
;
5350 remote_async_ops
.to_has_all_memory
= 1;
5351 remote_async_ops
.to_has_memory
= 1;
5352 remote_async_ops
.to_has_stack
= 1;
5353 remote_async_ops
.to_has_registers
= 1;
5354 remote_async_ops
.to_has_execution
= 1;
5355 remote_async_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5356 remote_async_ops
.to_can_async_p
= remote_can_async_p
;
5357 remote_async_ops
.to_is_async_p
= remote_is_async_p
;
5358 remote_async_ops
.to_async
= remote_async
;
5359 remote_async_ops
.to_async_mask_value
= 1;
5360 remote_async_ops
.to_magic
= OPS_MAGIC
;
5363 /* Set up the async extended remote vector by making a copy of the standard
5364 remote vector and adding to it. */
5367 init_extended_async_remote_ops (void)
5369 extended_async_remote_ops
= remote_async_ops
;
5371 extended_async_remote_ops
.to_shortname
= "extended-async";
5372 extended_async_remote_ops
.to_longname
=
5373 "Extended remote serial target in async gdb-specific protocol";
5374 extended_async_remote_ops
.to_doc
=
5375 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5376 Specify the serial device it is connected to (e.g. /dev/ttya).",
5377 extended_async_remote_ops
.to_open
= extended_remote_async_open
;
5378 extended_async_remote_ops
.to_create_inferior
= extended_remote_async_create_inferior
;
5379 extended_async_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5382 static struct cmd_list_element
*remote_set_cmdlist
;
5383 static struct cmd_list_element
*remote_show_cmdlist
;
5386 set_remote_cmd (char *args
, int from_tty
)
5391 show_remote_cmd (char *args
, int from_tty
)
5393 /* We can't just use cmd_show_list here, because we want to skip
5394 the redundant "show remote Z-packet". */
5395 struct cleanup
*showlist_chain
;
5396 struct cmd_list_element
*list
= remote_show_cmdlist
;
5398 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
5399 for (; list
!= NULL
; list
= list
->next
)
5400 if (strcmp (list
->name
, "Z-packet") == 0)
5402 else if (list
->type
== show_cmd
)
5404 struct cleanup
*option_chain
5405 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
5406 ui_out_field_string (uiout
, "name", list
->name
);
5407 ui_out_text (uiout
, ": ");
5408 do_setshow_command ((char *) NULL
, from_tty
, list
);
5409 /* Close the tuple. */
5410 do_cleanups (option_chain
);
5415 build_remote_gdbarch_data (void)
5417 remote_address_size
= TARGET_ADDR_BIT
;
5420 /* Saved pointer to previous owner of the new_objfile event. */
5421 static void (*remote_new_objfile_chain
) (struct objfile
*);
5423 /* Function to be called whenever a new objfile (shlib) is detected. */
5425 remote_new_objfile (struct objfile
*objfile
)
5427 if (remote_desc
!= 0) /* Have a remote connection. */
5429 remote_check_symbols (objfile
);
5431 /* Call predecessor on chain, if any. */
5432 if (remote_new_objfile_chain
!= 0 &&
5434 remote_new_objfile_chain (objfile
);
5438 _initialize_remote (void)
5440 /* architecture specific data */
5441 remote_gdbarch_data_handle
=
5442 gdbarch_data_register_post_init (init_remote_state
);
5444 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5445 that the remote protocol has been initialized. */
5446 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size
);
5447 deprecated_register_gdbarch_swap (NULL
, 0, build_remote_gdbarch_data
);
5450 add_target (&remote_ops
);
5452 init_extended_remote_ops ();
5453 add_target (&extended_remote_ops
);
5455 init_remote_async_ops ();
5456 add_target (&remote_async_ops
);
5458 init_extended_async_remote_ops ();
5459 add_target (&extended_async_remote_ops
);
5461 /* Hook into new objfile notification. */
5462 remote_new_objfile_chain
= deprecated_target_new_objfile_hook
;
5463 deprecated_target_new_objfile_hook
= remote_new_objfile
;
5466 init_remote_threadtests ();
5469 /* set/show remote ... */
5471 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
5472 Remote protocol specific variables\n\
5473 Configure various remote-protocol specific variables such as\n\
5474 the packets being used"),
5475 &remote_set_cmdlist
, "set remote ",
5476 0 /* allow-unknown */, &setlist
);
5477 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
5478 Remote protocol specific variables\n\
5479 Configure various remote-protocol specific variables such as\n\
5480 the packets being used"),
5481 &remote_show_cmdlist
, "show remote ",
5482 0 /* allow-unknown */, &showlist
);
5484 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
5485 Compare section data on target to the exec file.\n\
5486 Argument is a single section name (default: all loaded sections)."),
5489 add_cmd ("packet", class_maintenance
, packet_command
, _("\
5490 Send an arbitrary packet to a remote target.\n\
5491 maintenance packet TEXT\n\
5492 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5493 this command sends the string TEXT to the inferior, and displays the\n\
5494 response packet. GDB supplies the initial `$' character, and the\n\
5495 terminating `#' character and checksum."),
5498 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
5499 Set whether to send break if interrupted."), _("\
5500 Show whether to send break if interrupted."), _("\
5501 If set, a break, instead of a cntrl-c, is sent to the remote target."),
5502 NULL
, NULL
, /* FIXME: i18n: Whether to send break if interrupted is %s. */
5503 &setlist
, &showlist
);
5505 /* Install commands for configuring memory read/write packets. */
5507 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
5508 Set the maximum number of bytes per memory write packet (deprecated)."),
5510 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
5511 Show the maximum number of bytes per memory write packet (deprecated)."),
5513 add_cmd ("memory-write-packet-size", no_class
,
5514 set_memory_write_packet_size
, _("\
5515 Set the maximum number of bytes per memory-write packet.\n\
5516 Specify the number of bytes in a packet or 0 (zero) for the\n\
5517 default packet size. The actual limit is further reduced\n\
5518 dependent on the target. Specify ``fixed'' to disable the\n\
5519 further restriction and ``limit'' to enable that restriction."),
5520 &remote_set_cmdlist
);
5521 add_cmd ("memory-read-packet-size", no_class
,
5522 set_memory_read_packet_size
, _("\
5523 Set the maximum number of bytes per memory-read packet.\n\
5524 Specify the number of bytes in a packet or 0 (zero) for the\n\
5525 default packet size. The actual limit is further reduced\n\
5526 dependent on the target. Specify ``fixed'' to disable the\n\
5527 further restriction and ``limit'' to enable that restriction."),
5528 &remote_set_cmdlist
);
5529 add_cmd ("memory-write-packet-size", no_class
,
5530 show_memory_write_packet_size
,
5531 _("Show the maximum number of bytes per memory-write packet."),
5532 &remote_show_cmdlist
);
5533 add_cmd ("memory-read-packet-size", no_class
,
5534 show_memory_read_packet_size
,
5535 _("Show the maximum number of bytes per memory-read packet."),
5536 &remote_show_cmdlist
);
5538 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
5539 &remote_hw_watchpoint_limit
, _("\
5540 Set the maximum number of target hardware watchpoints."), _("\
5541 Show the maximum number of target hardware watchpoints."), _("\
5542 Specify a negative limit for unlimited."),
5543 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
5544 &remote_set_cmdlist
, &remote_show_cmdlist
);
5545 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
5546 &remote_hw_breakpoint_limit
, _("\
5547 Set the maximum number of target hardware breakpoints."), _("\
5548 Show the maximum number of target hardware breakpoints."), _("\
5549 Specify a negative limit for unlimited."),
5550 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
5551 &remote_set_cmdlist
, &remote_show_cmdlist
);
5553 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
5554 &remote_address_size
, _("\
5555 Set the maximum size of the address (in bits) in a memory packet."), _("\
5556 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
5558 NULL
, /* FIXME: i18n: */
5559 &setlist
, &showlist
);
5561 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
5562 "X", "binary-download",
5563 set_remote_protocol_packet_cmd
,
5564 show_remote_protocol_packet_cmd
,
5565 &remote_set_cmdlist
, &remote_show_cmdlist
,
5568 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
5569 "vCont", "verbose-resume",
5570 set_remote_protocol_packet_cmd
,
5571 show_remote_protocol_packet_cmd
,
5572 &remote_set_cmdlist
, &remote_show_cmdlist
,
5575 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
5576 "qSymbol", "symbol-lookup",
5577 set_remote_protocol_packet_cmd
,
5578 show_remote_protocol_packet_cmd
,
5579 &remote_set_cmdlist
, &remote_show_cmdlist
,
5582 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
5583 "P", "set-register",
5584 set_remote_protocol_packet_cmd
,
5585 show_remote_protocol_packet_cmd
,
5586 &remote_set_cmdlist
, &remote_show_cmdlist
,
5589 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
5590 "p", "fetch-register",
5591 set_remote_protocol_packet_cmd
,
5592 show_remote_protocol_packet_cmd
,
5593 &remote_set_cmdlist
, &remote_show_cmdlist
,
5596 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
5597 "Z0", "software-breakpoint",
5598 set_remote_protocol_packet_cmd
,
5599 show_remote_protocol_packet_cmd
,
5600 &remote_set_cmdlist
, &remote_show_cmdlist
,
5603 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
5604 "Z1", "hardware-breakpoint",
5605 set_remote_protocol_packet_cmd
,
5606 show_remote_protocol_packet_cmd
,
5607 &remote_set_cmdlist
, &remote_show_cmdlist
,
5610 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
5611 "Z2", "write-watchpoint",
5612 set_remote_protocol_packet_cmd
,
5613 show_remote_protocol_packet_cmd
,
5614 &remote_set_cmdlist
, &remote_show_cmdlist
,
5617 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
5618 "Z3", "read-watchpoint",
5619 set_remote_protocol_packet_cmd
,
5620 show_remote_protocol_packet_cmd
,
5621 &remote_set_cmdlist
, &remote_show_cmdlist
,
5624 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
5625 "Z4", "access-watchpoint",
5626 set_remote_protocol_packet_cmd
,
5627 show_remote_protocol_packet_cmd
,
5628 &remote_set_cmdlist
, &remote_show_cmdlist
,
5631 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qPart_auxv
],
5632 "qPart_auxv", "read-aux-vector",
5633 set_remote_protocol_packet_cmd
,
5634 show_remote_protocol_packet_cmd
,
5635 &remote_set_cmdlist
, &remote_show_cmdlist
,
5638 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
5639 "qGetTLSAddr", "get-thread-local-storage-address",
5640 set_remote_protocol_packet_cmd
,
5641 show_remote_protocol_packet_cmd
,
5642 &remote_set_cmdlist
, &remote_show_cmdlist
,
5645 /* Keep the old ``set remote Z-packet ...'' working. Each individual
5646 Z sub-packet has its own set and show commands, but users may
5647 have sets to this variable in their .gdbinit files (or in their
5649 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
5650 &remote_Z_packet_detect
, _("\
5651 Set use of remote protocol `Z' packets"), _("\
5652 Show use of remote protocol `Z' packets "), _("\
5653 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
5655 set_remote_protocol_Z_packet_cmd
,
5656 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
5657 &remote_set_cmdlist
, &remote_show_cmdlist
);
5659 /* Eventually initialize fileio. See fileio.c */
5660 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);