1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* See the GDB User Guide for details of the GDB remote protocol. */
27 #include "gdb_string.h"
33 #include "exceptions.h"
35 /*#include "terminal.h" */
38 #include "gdb-stabs.h"
39 #include "gdbthread.h"
43 #include "gdb_assert.h"
46 #include "cli/cli-decode.h"
47 #include "cli/cli-setshow.h"
52 #include "event-loop.h"
53 #include "event-top.h"
59 #include "gdbcore.h" /* for exec_bfd */
61 #include "remote-fileio.h"
63 /* Prototypes for local functions. */
64 static void cleanup_sigint_signal_handler (void *dummy
);
65 static void initialize_sigint_signal_handler (void);
66 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
68 static void handle_remote_sigint (int);
69 static void handle_remote_sigint_twice (int);
70 static void async_remote_interrupt (gdb_client_data
);
71 void async_remote_interrupt_twice (gdb_client_data
);
73 static void build_remote_gdbarch_data (void);
75 static void remote_files_info (struct target_ops
*ignore
);
77 static void remote_prepare_to_store (void);
79 static void remote_fetch_registers (int regno
);
81 static void remote_resume (ptid_t ptid
, int step
,
82 enum target_signal siggnal
);
83 static void remote_async_resume (ptid_t ptid
, int step
,
84 enum target_signal siggnal
);
85 static void remote_open (char *name
, int from_tty
);
86 static void remote_async_open (char *name
, int from_tty
);
88 static void extended_remote_open (char *name
, int from_tty
);
89 static void extended_remote_async_open (char *name
, int from_tty
);
91 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
,
94 static void remote_close (int quitting
);
96 static void remote_store_registers (int regno
);
98 static void remote_mourn (void);
99 static void remote_async_mourn (void);
101 static void extended_remote_restart (void);
103 static void extended_remote_mourn (void);
105 static void remote_mourn_1 (struct target_ops
*);
107 static void remote_send (char **buf
, long *sizeof_buf_p
);
109 static int readchar (int timeout
);
111 static ptid_t
remote_wait (ptid_t ptid
,
112 struct target_waitstatus
*status
);
113 static ptid_t
remote_async_wait (ptid_t ptid
,
114 struct target_waitstatus
*status
);
116 static void remote_kill (void);
117 static void remote_async_kill (void);
119 static int tohex (int nib
);
121 static void remote_detach (char *args
, int from_tty
);
123 static void remote_interrupt (int signo
);
125 static void remote_interrupt_twice (int signo
);
127 static void interrupt_query (void);
129 static void set_thread (int, int);
131 static int remote_thread_alive (ptid_t
);
133 static void get_offsets (void);
135 static void skip_frame (void);
137 static long read_frame (char **buf_p
, long *sizeof_buf
);
139 static int hexnumlen (ULONGEST num
);
141 static void init_remote_ops (void);
143 static void init_extended_remote_ops (void);
145 static void remote_stop (void);
147 static int ishex (int ch
, int *val
);
149 static int stubhex (int ch
);
151 static int hexnumstr (char *, ULONGEST
);
153 static int hexnumnstr (char *, ULONGEST
, int);
155 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
157 static void print_packet (char *);
159 static unsigned long crc32 (unsigned char *, int, unsigned int);
161 static void compare_sections_command (char *, int);
163 static void packet_command (char *, int);
165 static int stub_unpack_int (char *buff
, int fieldlength
);
167 static ptid_t
remote_current_thread (ptid_t oldptid
);
169 static void remote_find_new_threads (void);
171 static void record_currthread (int currthread
);
173 static int fromhex (int a
);
175 static int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
177 static int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
179 static int putpkt_binary (char *buf
, int cnt
);
181 static void check_binary_download (CORE_ADDR addr
);
183 struct packet_config
;
185 static void show_packet_config_cmd (struct packet_config
*config
);
187 static void update_packet_config (struct packet_config
*config
);
189 void _initialize_remote (void);
191 /* Description of the remote protocol. Strictly speaking, when the
192 target is open()ed, remote.c should create a per-target description
193 of the remote protocol using that target's architecture.
194 Unfortunately, the target stack doesn't include local state. For
195 the moment keep the information in the target's architecture
200 long offset
; /* Offset into G packet. */
201 long regnum
; /* GDB's internal register number. */
202 LONGEST pnum
; /* Remote protocol register number. */
203 int in_g_packet
; /* Always part of G packet. */
204 /* long size in bytes; == register_size (current_gdbarch, regnum);
206 /* char *name; == REGISTER_NAME (regnum); at present. */
211 /* Description of the remote protocol registers. */
212 long sizeof_g_packet
;
214 /* Description of the remote protocol registers indexed by REGNUM
215 (making an array of NUM_REGS + NUM_PSEUDO_REGS in size). */
216 struct packet_reg
*regs
;
218 /* This is the size (in chars) of the first response to the ``g''
219 packet. It is used as a heuristic when determining the maximum
220 size of memory-read and memory-write packets. A target will
221 typically only reserve a buffer large enough to hold the ``g''
222 packet. The size does not include packet overhead (headers and
224 long actual_register_packet_size
;
226 /* This is the maximum size (in chars) of a non read/write packet.
227 It is also used as a cap on the size of read/write packets. */
228 long remote_packet_size
;
230 /* A buffer to use for incoming packets, and its current size. The
231 buffer is grown dynamically for larger incoming packets.
232 Outgoing packets may also be constructed in this buffer.
233 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
234 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
241 /* Handle for retreving the remote protocol data from gdbarch. */
242 static struct gdbarch_data
*remote_gdbarch_data_handle
;
244 static struct remote_state
*
245 get_remote_state (void)
247 return gdbarch_data (current_gdbarch
, remote_gdbarch_data_handle
);
251 init_remote_state (struct gdbarch
*gdbarch
)
254 struct remote_state
*rs
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_state
);
256 rs
->sizeof_g_packet
= 0;
258 /* Assume a 1:1 regnum<->pnum table. */
259 rs
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
, NUM_REGS
+ NUM_PSEUDO_REGS
,
261 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
263 struct packet_reg
*r
= &rs
->regs
[regnum
];
266 r
->offset
= DEPRECATED_REGISTER_BYTE (regnum
);
267 r
->in_g_packet
= (regnum
< NUM_REGS
);
268 /* ...name = REGISTER_NAME (regnum); */
270 /* Compute packet size by accumulating the size of all registers. */
271 if (regnum
< NUM_REGS
)
272 rs
->sizeof_g_packet
+= register_size (current_gdbarch
, regnum
);
275 /* Default maximum number of characters in a packet body. Many
276 remote stubs have a hardwired buffer size of 400 bytes
277 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
278 as the maximum packet-size to ensure that the packet and an extra
279 NUL character can always fit in the buffer. This stops GDB
280 trashing stubs that try to squeeze an extra NUL into what is
281 already a full buffer (As of 1999-12-04 that was most stubs. */
282 rs
->remote_packet_size
= 400 - 1;
284 /* Should rs->sizeof_g_packet needs more space than the
285 default, adjust the size accordingly. Remember that each byte is
286 encoded as two characters. 32 is the overhead for the packet
287 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
288 (``$NN:G...#NN'') is a better guess, the below has been padded a
290 if (rs
->sizeof_g_packet
> ((rs
->remote_packet_size
- 32) / 2))
291 rs
->remote_packet_size
= (rs
->sizeof_g_packet
* 2 + 32);
293 /* This one is filled in when a ``g'' packet is received. */
294 rs
->actual_register_packet_size
= 0;
296 /* Create the buffer at a default size. Note that this would
297 leak memory if the gdbarch were ever destroyed; there's no
298 way to register a destructor for it, and we can't realloc
299 using the gdbarch obstack. But gdbarches are never
301 rs
->buf_size
= rs
->remote_packet_size
;
302 rs
->buf
= xmalloc (rs
->buf_size
);
307 static struct packet_reg
*
308 packet_reg_from_regnum (struct remote_state
*rs
, long regnum
)
310 if (regnum
< 0 && regnum
>= NUM_REGS
+ NUM_PSEUDO_REGS
)
314 struct packet_reg
*r
= &rs
->regs
[regnum
];
315 gdb_assert (r
->regnum
== regnum
);
320 static struct packet_reg
*
321 packet_reg_from_pnum (struct remote_state
*rs
, LONGEST pnum
)
324 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
326 struct packet_reg
*r
= &rs
->regs
[i
];
333 /* FIXME: graces/2002-08-08: These variables should eventually be
334 bound to an instance of the target object (as in gdbarch-tdep()),
335 when such a thing exists. */
337 /* This is set to the data address of the access causing the target
338 to stop for a watchpoint. */
339 static CORE_ADDR remote_watch_data_address
;
341 /* This is non-zero if target stopped for a watchpoint. */
342 static int remote_stopped_by_watchpoint_p
;
344 static struct target_ops remote_ops
;
346 static struct target_ops extended_remote_ops
;
348 /* Temporary target ops. Just like the remote_ops and
349 extended_remote_ops, but with asynchronous support. */
350 static struct target_ops remote_async_ops
;
352 static struct target_ops extended_async_remote_ops
;
354 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
355 ``forever'' still use the normal timeout mechanism. This is
356 currently used by the ASYNC code to guarentee that target reads
357 during the initial connect always time-out. Once getpkt has been
358 modified to return a timeout indication and, in turn
359 remote_wait()/wait_for_inferior() have gained a timeout parameter
361 static int wait_forever_enabled_p
= 1;
364 /* This variable chooses whether to send a ^C or a break when the user
365 requests program interruption. Although ^C is usually what remote
366 systems expect, and that is the default here, sometimes a break is
367 preferable instead. */
369 static int remote_break
;
371 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
372 remote_open knows that we don't have a file open when the program
374 static struct serial
*remote_desc
= NULL
;
376 /* This variable sets the number of bits in an address that are to be
377 sent in a memory ("M" or "m") packet. Normally, after stripping
378 leading zeros, the entire address would be sent. This variable
379 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
380 initial implementation of remote.c restricted the address sent in
381 memory packets to ``host::sizeof long'' bytes - (typically 32
382 bits). Consequently, for 64 bit targets, the upper 32 bits of an
383 address was never sent. Since fixing this bug may cause a break in
384 some remote targets this variable is principly provided to
385 facilitate backward compatibility. */
387 static int remote_address_size
;
389 /* Tempoary to track who currently owns the terminal. See
390 target_async_terminal_* for more details. */
392 static int remote_async_terminal_ours_p
;
395 /* User configurable variables for the number of characters in a
396 memory read/write packet. MIN (rs->remote_packet_size,
397 rs->sizeof_g_packet) is the default. Some targets need smaller
398 values (fifo overruns, et.al.) and some users need larger values
399 (speed up transfers). The variables ``preferred_*'' (the user
400 request), ``current_*'' (what was actually set) and ``forced_*''
401 (Positive - a soft limit, negative - a hard limit). */
403 struct memory_packet_config
410 /* Compute the current size of a read/write packet. Since this makes
411 use of ``actual_register_packet_size'' the computation is dynamic. */
414 get_memory_packet_size (struct memory_packet_config
*config
)
416 struct remote_state
*rs
= get_remote_state ();
417 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
418 law?) that some hosts don't cope very well with large alloca()
419 calls. Eventually the alloca() code will be replaced by calls to
420 xmalloc() and make_cleanups() allowing this restriction to either
421 be lifted or removed. */
422 #ifndef MAX_REMOTE_PACKET_SIZE
423 #define MAX_REMOTE_PACKET_SIZE 16384
425 /* NOTE: 20 ensures we can write at least one byte. */
426 #ifndef MIN_REMOTE_PACKET_SIZE
427 #define MIN_REMOTE_PACKET_SIZE 20
432 if (config
->size
<= 0)
433 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
435 what_they_get
= config
->size
;
439 what_they_get
= rs
->remote_packet_size
;
440 /* Limit the packet to the size specified by the user. */
442 && what_they_get
> config
->size
)
443 what_they_get
= config
->size
;
444 /* Limit it to the size of the targets ``g'' response. */
445 if ((rs
->actual_register_packet_size
) > 0
446 && what_they_get
> (rs
->actual_register_packet_size
))
447 what_they_get
= (rs
->actual_register_packet_size
);
449 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
450 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
451 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
452 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
454 /* Make sure there is room in the global buffer for this packet
455 (including its trailing NUL byte). */
456 if (rs
->buf_size
< what_they_get
+ 1)
458 rs
->buf_size
= 2 * what_they_get
;
459 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
462 return what_they_get
;
465 /* Update the size of a read/write packet. If they user wants
466 something really big then do a sanity check. */
469 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
471 int fixed_p
= config
->fixed_p
;
472 long size
= config
->size
;
474 error (_("Argument required (integer, `fixed' or `limited')."));
475 else if (strcmp (args
, "hard") == 0
476 || strcmp (args
, "fixed") == 0)
478 else if (strcmp (args
, "soft") == 0
479 || strcmp (args
, "limit") == 0)
484 size
= strtoul (args
, &end
, 0);
486 error (_("Invalid %s (bad syntax)."), config
->name
);
488 /* Instead of explicitly capping the size of a packet to
489 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
490 instead allowed to set the size to something arbitrarily
492 if (size
> MAX_REMOTE_PACKET_SIZE
)
493 error (_("Invalid %s (too large)."), config
->name
);
497 if (fixed_p
&& !config
->fixed_p
)
499 if (! query (_("The target may not be able to correctly handle a %s\n"
500 "of %ld bytes. Change the packet size? "),
502 error (_("Packet size not changed."));
504 /* Update the config. */
505 config
->fixed_p
= fixed_p
;
510 show_memory_packet_size (struct memory_packet_config
*config
)
512 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
514 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
515 get_memory_packet_size (config
));
517 printf_filtered (_("Packets are limited to %ld bytes.\n"),
518 get_memory_packet_size (config
));
521 static struct memory_packet_config memory_write_packet_config
=
523 "memory-write-packet-size",
527 set_memory_write_packet_size (char *args
, int from_tty
)
529 set_memory_packet_size (args
, &memory_write_packet_config
);
533 show_memory_write_packet_size (char *args
, int from_tty
)
535 show_memory_packet_size (&memory_write_packet_config
);
539 get_memory_write_packet_size (void)
541 return get_memory_packet_size (&memory_write_packet_config
);
544 static struct memory_packet_config memory_read_packet_config
=
546 "memory-read-packet-size",
550 set_memory_read_packet_size (char *args
, int from_tty
)
552 set_memory_packet_size (args
, &memory_read_packet_config
);
556 show_memory_read_packet_size (char *args
, int from_tty
)
558 show_memory_packet_size (&memory_read_packet_config
);
562 get_memory_read_packet_size (void)
564 struct remote_state
*rs
= get_remote_state ();
565 long size
= get_memory_packet_size (&memory_read_packet_config
);
566 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
567 extra buffer size argument before the memory read size can be
568 increased beyond RS->remote_packet_size. */
569 if (size
> rs
->remote_packet_size
)
570 size
= rs
->remote_packet_size
;
575 /* Generic configuration support for packets the stub optionally
576 supports. Allows the user to specify the use of the packet as well
577 as allowing GDB to auto-detect support in the remote stub. */
581 PACKET_SUPPORT_UNKNOWN
= 0,
590 enum auto_boolean detect
;
591 enum packet_support support
;
594 /* Analyze a packet's return value and update the packet config
605 update_packet_config (struct packet_config
*config
)
607 switch (config
->detect
)
609 case AUTO_BOOLEAN_TRUE
:
610 config
->support
= PACKET_ENABLE
;
612 case AUTO_BOOLEAN_FALSE
:
613 config
->support
= PACKET_DISABLE
;
615 case AUTO_BOOLEAN_AUTO
:
616 config
->support
= PACKET_SUPPORT_UNKNOWN
;
622 show_packet_config_cmd (struct packet_config
*config
)
624 char *support
= "internal-error";
625 switch (config
->support
)
631 support
= "disabled";
633 case PACKET_SUPPORT_UNKNOWN
:
637 switch (config
->detect
)
639 case AUTO_BOOLEAN_AUTO
:
640 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
641 config
->name
, support
);
643 case AUTO_BOOLEAN_TRUE
:
644 case AUTO_BOOLEAN_FALSE
:
645 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
646 config
->name
, support
);
652 add_packet_config_cmd (struct packet_config
*config
,
655 cmd_sfunc_ftype
*set_func
,
656 show_value_ftype
*show_func
,
657 struct cmd_list_element
**set_remote_list
,
658 struct cmd_list_element
**show_remote_list
,
666 config
->title
= title
;
667 config
->detect
= AUTO_BOOLEAN_AUTO
;
668 config
->support
= PACKET_SUPPORT_UNKNOWN
;
669 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
671 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
673 /* set/show TITLE-packet {auto,on,off} */
674 cmd_name
= xstrprintf ("%s-packet", title
);
675 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
676 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
678 set_remote_list
, show_remote_list
);
679 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
683 legacy_name
= xstrprintf ("%s-packet", name
);
684 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
686 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
691 static enum packet_result
692 packet_ok (const char *buf
, struct packet_config
*config
)
696 /* The stub recognized the packet request. Check that the
697 operation succeeded. */
698 switch (config
->support
)
700 case PACKET_SUPPORT_UNKNOWN
:
702 fprintf_unfiltered (gdb_stdlog
,
703 "Packet %s (%s) is supported\n",
704 config
->name
, config
->title
);
705 config
->support
= PACKET_ENABLE
;
708 internal_error (__FILE__
, __LINE__
,
709 _("packet_ok: attempt to use a disabled packet"));
714 if (buf
[0] == 'O' && buf
[1] == 'K' && buf
[2] == '\0')
715 /* "OK" - definitly OK. */
718 && isxdigit (buf
[1]) && isxdigit (buf
[2])
720 /* "Enn" - definitly an error. */
722 /* The packet may or may not be OK. Just assume it is. */
727 /* The stub does not support the packet. */
728 switch (config
->support
)
731 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
732 /* If the stub previously indicated that the packet was
733 supported then there is a protocol error.. */
734 error (_("Protocol error: %s (%s) conflicting enabled responses."),
735 config
->name
, config
->title
);
737 /* The user set it wrong. */
738 error (_("Enabled packet %s (%s) not recognized by stub"),
739 config
->name
, config
->title
);
741 case PACKET_SUPPORT_UNKNOWN
:
743 fprintf_unfiltered (gdb_stdlog
,
744 "Packet %s (%s) is NOT supported\n",
745 config
->name
, config
->title
);
746 config
->support
= PACKET_DISABLE
;
751 return PACKET_UNKNOWN
;
771 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
774 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
775 struct cmd_list_element
*c
)
777 struct packet_config
*packet
;
779 for (packet
= remote_protocol_packets
;
780 packet
< &remote_protocol_packets
[PACKET_MAX
];
783 if (&packet
->detect
== c
->var
)
785 update_packet_config (packet
);
789 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
794 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
795 struct cmd_list_element
*c
,
798 struct packet_config
*packet
;
800 for (packet
= remote_protocol_packets
;
801 packet
< &remote_protocol_packets
[PACKET_MAX
];
804 if (&packet
->detect
== c
->var
)
806 show_packet_config_cmd (packet
);
810 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
814 /* Should we try one of the 'Z' requests? */
818 Z_PACKET_SOFTWARE_BP
,
819 Z_PACKET_HARDWARE_BP
,
826 /* For compatibility with older distributions. Provide a ``set remote
827 Z-packet ...'' command that updates all the Z packet types. */
829 static enum auto_boolean remote_Z_packet_detect
;
832 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
833 struct cmd_list_element
*c
)
836 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
838 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
839 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
844 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
845 struct cmd_list_element
*c
,
849 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
851 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
855 /* Should we try the 'ThreadInfo' query packet?
857 This variable (NOT available to the user: auto-detect only!)
858 determines whether GDB will use the new, simpler "ThreadInfo"
859 query or the older, more complex syntax for thread queries.
860 This is an auto-detect variable (set to true at each connect,
861 and set to false when the target fails to recognize it). */
863 static int use_threadinfo_query
;
864 static int use_threadextra_query
;
866 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
867 static void *sigint_remote_twice_token
;
868 static void *sigint_remote_token
;
870 /* These are pointers to hook functions that may be set in order to
871 modify resume/wait behavior for a particular architecture. */
873 void (*deprecated_target_resume_hook
) (void);
874 void (*deprecated_target_wait_loop_hook
) (void);
878 /* These are the threads which we last sent to the remote system.
879 -1 for all or -2 for not sent yet. */
880 static int general_thread
;
881 static int continue_thread
;
883 /* Call this function as a result of
884 1) A halt indication (T packet) containing a thread id
885 2) A direct query of currthread
886 3) Successful execution of set thread
890 record_currthread (int currthread
)
892 general_thread
= currthread
;
894 /* If this is a new thread, add it to GDB's thread list.
895 If we leave it up to WFI to do this, bad things will happen. */
896 if (!in_thread_list (pid_to_ptid (currthread
)))
898 add_thread (pid_to_ptid (currthread
));
899 ui_out_text (uiout
, "[New ");
900 ui_out_text (uiout
, target_pid_to_str (pid_to_ptid (currthread
)));
901 ui_out_text (uiout
, "]\n");
905 #define MAGIC_NULL_PID 42000
908 set_thread (int th
, int gen
)
910 struct remote_state
*rs
= get_remote_state ();
912 int state
= gen
? general_thread
: continue_thread
;
918 buf
[1] = gen
? 'g' : 'c';
919 if (th
== MAGIC_NULL_PID
)
925 xsnprintf (&buf
[2], rs
->remote_packet_size
- 2, "-%x", -th
);
927 xsnprintf (&buf
[2], rs
->remote_packet_size
- 2, "%x", th
);
929 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
933 continue_thread
= th
;
936 /* Return nonzero if the thread TH is still alive on the remote system. */
939 remote_thread_alive (ptid_t ptid
)
941 struct remote_state
*rs
= get_remote_state ();
942 int tid
= PIDGET (ptid
);
946 xsnprintf (buf
, rs
->remote_packet_size
, "T-%08x", -tid
);
948 xsnprintf (buf
, rs
->remote_packet_size
, "T%08x", tid
);
950 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
951 return (buf
[0] == 'O' && buf
[1] == 'K');
954 /* About these extended threadlist and threadinfo packets. They are
955 variable length packets but, the fields within them are often fixed
956 length. They are redundent enough to send over UDP as is the
957 remote protocol in general. There is a matching unit test module
960 #define OPAQUETHREADBYTES 8
962 /* a 64 bit opaque identifier */
963 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
965 /* WARNING: This threadref data structure comes from the remote O.S.,
966 libstub protocol encoding, and remote.c. it is not particularly
969 /* Right now, the internal structure is int. We want it to be bigger.
973 typedef int gdb_threadref
; /* Internal GDB thread reference. */
975 /* gdb_ext_thread_info is an internal GDB data structure which is
976 equivalent to the reply of the remote threadinfo packet. */
978 struct gdb_ext_thread_info
980 threadref threadid
; /* External form of thread reference. */
981 int active
; /* Has state interesting to GDB?
983 char display
[256]; /* Brief state display, name,
984 blocked/suspended. */
985 char shortname
[32]; /* To be used to name threads. */
986 char more_display
[256]; /* Long info, statistics, queue depth,
990 /* The volume of remote transfers can be limited by submitting
991 a mask containing bits specifying the desired information.
992 Use a union of these values as the 'selection' parameter to
993 get_thread_info. FIXME: Make these TAG names more thread specific.
996 #define TAG_THREADID 1
998 #define TAG_DISPLAY 4
999 #define TAG_THREADNAME 8
1000 #define TAG_MOREDISPLAY 16
1002 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1004 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1006 static char *unpack_nibble (char *buf
, int *val
);
1008 static char *pack_nibble (char *buf
, int nibble
);
1010 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1012 static char *unpack_byte (char *buf
, int *value
);
1014 static char *pack_int (char *buf
, int value
);
1016 static char *unpack_int (char *buf
, int *value
);
1018 static char *unpack_string (char *src
, char *dest
, int length
);
1020 static char *pack_threadid (char *pkt
, threadref
*id
);
1022 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1024 void int_to_threadref (threadref
*id
, int value
);
1026 static int threadref_to_int (threadref
*ref
);
1028 static void copy_threadref (threadref
*dest
, threadref
*src
);
1030 static int threadmatch (threadref
*dest
, threadref
*src
);
1032 static char *pack_threadinfo_request (char *pkt
, int mode
,
1035 static int remote_unpack_thread_info_response (char *pkt
,
1036 threadref
*expectedref
,
1037 struct gdb_ext_thread_info
1041 static int remote_get_threadinfo (threadref
*threadid
,
1042 int fieldset
, /*TAG mask */
1043 struct gdb_ext_thread_info
*info
);
1045 static char *pack_threadlist_request (char *pkt
, int startflag
,
1047 threadref
*nextthread
);
1049 static int parse_threadlist_response (char *pkt
,
1051 threadref
*original_echo
,
1052 threadref
*resultlist
,
1055 static int remote_get_threadlist (int startflag
,
1056 threadref
*nextthread
,
1060 threadref
*threadlist
);
1062 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1064 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1065 void *context
, int looplimit
);
1067 static int remote_newthread_step (threadref
*ref
, void *context
);
1069 /* Encode 64 bits in 16 chars of hex. */
1071 static const char hexchars
[] = "0123456789abcdef";
1074 ishex (int ch
, int *val
)
1076 if ((ch
>= 'a') && (ch
<= 'f'))
1078 *val
= ch
- 'a' + 10;
1081 if ((ch
>= 'A') && (ch
<= 'F'))
1083 *val
= ch
- 'A' + 10;
1086 if ((ch
>= '0') && (ch
<= '9'))
1097 if (ch
>= 'a' && ch
<= 'f')
1098 return ch
- 'a' + 10;
1099 if (ch
>= '0' && ch
<= '9')
1101 if (ch
>= 'A' && ch
<= 'F')
1102 return ch
- 'A' + 10;
1107 stub_unpack_int (char *buff
, int fieldlength
)
1114 nibble
= stubhex (*buff
++);
1118 retval
= retval
<< 4;
1124 unpack_varlen_hex (char *buff
, /* packet to parse */
1130 while (ishex (*buff
, &nibble
))
1133 retval
= retval
<< 4;
1134 retval
|= nibble
& 0x0f;
1141 unpack_nibble (char *buf
, int *val
)
1143 ishex (*buf
++, val
);
1148 pack_nibble (char *buf
, int nibble
)
1150 *buf
++ = hexchars
[(nibble
& 0x0f)];
1155 pack_hex_byte (char *pkt
, int byte
)
1157 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1158 *pkt
++ = hexchars
[(byte
& 0xf)];
1163 unpack_byte (char *buf
, int *value
)
1165 *value
= stub_unpack_int (buf
, 2);
1170 pack_int (char *buf
, int value
)
1172 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1173 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1174 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1175 buf
= pack_hex_byte (buf
, (value
& 0xff));
1180 unpack_int (char *buf
, int *value
)
1182 *value
= stub_unpack_int (buf
, 8);
1186 #if 0 /* Currently unused, uncomment when needed. */
1187 static char *pack_string (char *pkt
, char *string
);
1190 pack_string (char *pkt
, char *string
)
1195 len
= strlen (string
);
1197 len
= 200; /* Bigger than most GDB packets, junk??? */
1198 pkt
= pack_hex_byte (pkt
, len
);
1202 if ((ch
== '\0') || (ch
== '#'))
1203 ch
= '*'; /* Protect encapsulation. */
1208 #endif /* 0 (unused) */
1211 unpack_string (char *src
, char *dest
, int length
)
1220 pack_threadid (char *pkt
, threadref
*id
)
1223 unsigned char *altid
;
1225 altid
= (unsigned char *) id
;
1226 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1228 pkt
= pack_hex_byte (pkt
, *altid
++);
1234 unpack_threadid (char *inbuf
, threadref
*id
)
1237 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1240 altref
= (char *) id
;
1242 while (inbuf
< limit
)
1244 x
= stubhex (*inbuf
++);
1245 y
= stubhex (*inbuf
++);
1246 *altref
++ = (x
<< 4) | y
;
1251 /* Externally, threadrefs are 64 bits but internally, they are still
1252 ints. This is due to a mismatch of specifications. We would like
1253 to use 64bit thread references internally. This is an adapter
1257 int_to_threadref (threadref
*id
, int value
)
1259 unsigned char *scan
;
1261 scan
= (unsigned char *) id
;
1267 *scan
++ = (value
>> 24) & 0xff;
1268 *scan
++ = (value
>> 16) & 0xff;
1269 *scan
++ = (value
>> 8) & 0xff;
1270 *scan
++ = (value
& 0xff);
1274 threadref_to_int (threadref
*ref
)
1277 unsigned char *scan
;
1283 value
= (value
<< 8) | ((*scan
++) & 0xff);
1288 copy_threadref (threadref
*dest
, threadref
*src
)
1291 unsigned char *csrc
, *cdest
;
1293 csrc
= (unsigned char *) src
;
1294 cdest
= (unsigned char *) dest
;
1301 threadmatch (threadref
*dest
, threadref
*src
)
1303 /* Things are broken right now, so just assume we got a match. */
1305 unsigned char *srcp
, *destp
;
1307 srcp
= (char *) src
;
1308 destp
= (char *) dest
;
1312 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1319 threadid:1, # always request threadid
1326 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1329 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1331 *pkt
++ = 'q'; /* Info Query */
1332 *pkt
++ = 'P'; /* process or thread info */
1333 pkt
= pack_int (pkt
, mode
); /* mode */
1334 pkt
= pack_threadid (pkt
, id
); /* threadid */
1335 *pkt
= '\0'; /* terminate */
1339 /* These values tag the fields in a thread info response packet. */
1340 /* Tagging the fields allows us to request specific fields and to
1341 add more fields as time goes by. */
1343 #define TAG_THREADID 1 /* Echo the thread identifier. */
1344 #define TAG_EXISTS 2 /* Is this process defined enough to
1345 fetch registers and its stack? */
1346 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1347 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1348 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1352 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1353 struct gdb_ext_thread_info
*info
)
1355 struct remote_state
*rs
= get_remote_state ();
1359 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
1362 /* info->threadid = 0; FIXME: implement zero_threadref. */
1364 info
->display
[0] = '\0';
1365 info
->shortname
[0] = '\0';
1366 info
->more_display
[0] = '\0';
1368 /* Assume the characters indicating the packet type have been
1370 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1371 pkt
= unpack_threadid (pkt
, &ref
);
1374 warning (_("Incomplete response to threadinfo request."));
1375 if (!threadmatch (&ref
, expectedref
))
1376 { /* This is an answer to a different request. */
1377 warning (_("ERROR RMT Thread info mismatch."));
1380 copy_threadref (&info
->threadid
, &ref
);
1382 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1384 /* Packets are terminated with nulls. */
1385 while ((pkt
< limit
) && mask
&& *pkt
)
1387 pkt
= unpack_int (pkt
, &tag
); /* tag */
1388 pkt
= unpack_byte (pkt
, &length
); /* length */
1389 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1391 warning (_("ERROR RMT: threadinfo tag mismatch."));
1395 if (tag
== TAG_THREADID
)
1399 warning (_("ERROR RMT: length of threadid is not 16."));
1403 pkt
= unpack_threadid (pkt
, &ref
);
1404 mask
= mask
& ~TAG_THREADID
;
1407 if (tag
== TAG_EXISTS
)
1409 info
->active
= stub_unpack_int (pkt
, length
);
1411 mask
= mask
& ~(TAG_EXISTS
);
1414 warning (_("ERROR RMT: 'exists' length too long."));
1420 if (tag
== TAG_THREADNAME
)
1422 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1423 mask
= mask
& ~TAG_THREADNAME
;
1426 if (tag
== TAG_DISPLAY
)
1428 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1429 mask
= mask
& ~TAG_DISPLAY
;
1432 if (tag
== TAG_MOREDISPLAY
)
1434 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1435 mask
= mask
& ~TAG_MOREDISPLAY
;
1438 warning (_("ERROR RMT: unknown thread info tag."));
1439 break; /* Not a tag we know about. */
1445 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1446 struct gdb_ext_thread_info
*info
)
1448 struct remote_state
*rs
= get_remote_state ();
1450 char *threadinfo_pkt
= rs
->buf
;
1452 pack_threadinfo_request (threadinfo_pkt
, fieldset
, threadid
);
1453 putpkt (threadinfo_pkt
);
1454 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1455 result
= remote_unpack_thread_info_response (threadinfo_pkt
+ 2,
1460 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1463 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1464 threadref
*nextthread
)
1466 *pkt
++ = 'q'; /* info query packet */
1467 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1468 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1469 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1470 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1475 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1478 parse_threadlist_response (char *pkt
, int result_limit
,
1479 threadref
*original_echo
, threadref
*resultlist
,
1482 struct remote_state
*rs
= get_remote_state ();
1484 int count
, resultcount
, done
;
1487 /* Assume the 'q' and 'M chars have been stripped. */
1488 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
1489 /* done parse past here */
1490 pkt
= unpack_byte (pkt
, &count
); /* count field */
1491 pkt
= unpack_nibble (pkt
, &done
);
1492 /* The first threadid is the argument threadid. */
1493 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1494 while ((count
-- > 0) && (pkt
< limit
))
1496 pkt
= unpack_threadid (pkt
, resultlist
++);
1497 if (resultcount
++ >= result_limit
)
1506 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1507 int *done
, int *result_count
, threadref
*threadlist
)
1509 struct remote_state
*rs
= get_remote_state ();
1510 static threadref echo_nextthread
;
1511 char *threadlist_packet
= rs
->buf
;
1514 /* Trancate result limit to be smaller than the packet size. */
1515 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= rs
->remote_packet_size
)
1516 result_limit
= (rs
->remote_packet_size
/ BUF_THREAD_ID_SIZE
) - 2;
1518 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
1520 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1523 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
1526 if (!threadmatch (&echo_nextthread
, nextthread
))
1528 /* FIXME: This is a good reason to drop the packet. */
1529 /* Possably, there is a duplicate response. */
1531 retransmit immediatly - race conditions
1532 retransmit after timeout - yes
1534 wait for packet, then exit
1536 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
1537 return 0; /* I choose simply exiting. */
1539 if (*result_count
<= 0)
1543 warning (_("RMT ERROR : failed to get remote thread list."));
1546 return result
; /* break; */
1548 if (*result_count
> result_limit
)
1551 warning (_("RMT ERROR: threadlist response longer than requested."));
1557 /* This is the interface between remote and threads, remotes upper
1560 /* remote_find_new_threads retrieves the thread list and for each
1561 thread in the list, looks up the thread in GDB's internal list,
1562 ading the thread if it does not already exist. This involves
1563 getting partial thread lists from the remote target so, polling the
1564 quit_flag is required. */
1567 /* About this many threadisds fit in a packet. */
1569 #define MAXTHREADLISTRESULTS 32
1572 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
1575 int done
, i
, result_count
;
1579 static threadref nextthread
;
1580 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1585 if (loopcount
++ > looplimit
)
1588 warning (_("Remote fetch threadlist -infinite loop-."));
1591 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1592 &done
, &result_count
, resultthreadlist
))
1597 /* Clear for later iterations. */
1599 /* Setup to resume next batch of thread references, set nextthread. */
1600 if (result_count
>= 1)
1601 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1603 while (result_count
--)
1604 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1611 remote_newthread_step (threadref
*ref
, void *context
)
1615 ptid
= pid_to_ptid (threadref_to_int (ref
));
1617 if (!in_thread_list (ptid
))
1619 return 1; /* continue iterator */
1622 #define CRAZY_MAX_THREADS 1000
1625 remote_current_thread (ptid_t oldpid
)
1627 struct remote_state
*rs
= get_remote_state ();
1628 char *buf
= rs
->buf
;
1631 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1632 if (buf
[0] == 'Q' && buf
[1] == 'C')
1633 /* Use strtoul here, so we'll correctly parse values whose highest
1634 bit is set. The protocol carries them as a simple series of
1635 hex digits; in the absence of a sign, strtol will see such
1636 values as positive numbers out of range for signed 'long', and
1637 return LONG_MAX to indicate an overflow. */
1638 return pid_to_ptid (strtoul (&buf
[2], NULL
, 16));
1643 /* Find new threads for info threads command.
1644 * Original version, using John Metzler's thread protocol.
1648 remote_find_new_threads (void)
1650 remote_threadlist_iterator (remote_newthread_step
, 0,
1652 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
) /* ack ack ack */
1653 inferior_ptid
= remote_current_thread (inferior_ptid
);
1657 * Find all threads for info threads command.
1658 * Uses new thread protocol contributed by Cisco.
1659 * Falls back and attempts to use the older method (above)
1660 * if the target doesn't respond to the new method.
1664 remote_threads_info (void)
1666 struct remote_state
*rs
= get_remote_state ();
1670 if (remote_desc
== 0) /* paranoia */
1671 error (_("Command can only be used when connected to the remote target."));
1673 if (use_threadinfo_query
)
1675 putpkt ("qfThreadInfo");
1677 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1678 if (bufp
[0] != '\0') /* q packet recognized */
1680 while (*bufp
++ == 'm') /* reply contains one or more TID */
1684 /* Use strtoul here, so we'll correctly parse values
1685 whose highest bit is set. The protocol carries
1686 them as a simple series of hex digits; in the
1687 absence of a sign, strtol will see such values as
1688 positive numbers out of range for signed 'long',
1689 and return LONG_MAX to indicate an overflow. */
1690 tid
= strtoul (bufp
, &bufp
, 16);
1691 if (tid
!= 0 && !in_thread_list (pid_to_ptid (tid
)))
1692 add_thread (pid_to_ptid (tid
));
1694 while (*bufp
++ == ','); /* comma-separated list */
1695 putpkt ("qsThreadInfo");
1697 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1703 /* Else fall back to old method based on jmetzler protocol. */
1704 use_threadinfo_query
= 0;
1705 remote_find_new_threads ();
1710 * Collect a descriptive string about the given thread.
1711 * The target may say anything it wants to about the thread
1712 * (typically info about its blocked / runnable state, name, etc.).
1713 * This string will appear in the info threads display.
1715 * Optional: targets are not required to implement this function.
1719 remote_threads_extra_info (struct thread_info
*tp
)
1721 struct remote_state
*rs
= get_remote_state ();
1725 struct gdb_ext_thread_info threadinfo
;
1726 static char display_buf
[100]; /* arbitrary... */
1727 int n
= 0; /* position in display_buf */
1729 if (remote_desc
== 0) /* paranoia */
1730 internal_error (__FILE__
, __LINE__
,
1731 _("remote_threads_extra_info"));
1733 if (use_threadextra_query
)
1735 char *bufp
= rs
->buf
;
1737 xsnprintf (bufp
, rs
->remote_packet_size
, "qThreadExtraInfo,%x",
1740 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1743 n
= min (strlen (bufp
) / 2, sizeof (display_buf
));
1744 result
= hex2bin (bufp
, (gdb_byte
*) display_buf
, n
);
1745 display_buf
[result
] = '\0';
1750 /* If the above query fails, fall back to the old method. */
1751 use_threadextra_query
= 0;
1752 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
1753 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
1754 int_to_threadref (&id
, PIDGET (tp
->ptid
));
1755 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
1756 if (threadinfo
.active
)
1758 if (*threadinfo
.shortname
)
1759 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
1760 " Name: %s,", threadinfo
.shortname
);
1761 if (*threadinfo
.display
)
1762 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
1763 " State: %s,", threadinfo
.display
);
1764 if (*threadinfo
.more_display
)
1765 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
1766 " Priority: %s", threadinfo
.more_display
);
1770 /* For purely cosmetic reasons, clear up trailing commas. */
1771 if (',' == display_buf
[n
-1])
1772 display_buf
[n
-1] = ' ';
1780 /* Restart the remote side; this is an extended protocol operation. */
1783 extended_remote_restart (void)
1785 struct remote_state
*rs
= get_remote_state ();
1787 /* Send the restart command; for reasons I don't understand the
1788 remote side really expects a number after the "R". */
1789 xsnprintf (rs
->buf
, rs
->remote_packet_size
, "R%x", 0);
1792 /* Now query for status so this looks just like we restarted
1793 gdbserver from scratch. */
1795 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1798 /* Clean up connection to a remote debugger. */
1801 remote_close (int quitting
)
1804 serial_close (remote_desc
);
1808 /* Query the remote side for the text, data and bss offsets. */
1813 struct remote_state
*rs
= get_remote_state ();
1814 char *buf
= rs
->buf
;
1817 CORE_ADDR text_addr
, data_addr
, bss_addr
;
1818 struct section_offsets
*offs
;
1820 putpkt ("qOffsets");
1821 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1823 if (buf
[0] == '\000')
1824 return; /* Return silently. Stub doesn't support
1828 warning (_("Remote failure reply: %s"), buf
);
1832 /* Pick up each field in turn. This used to be done with scanf, but
1833 scanf will make trouble if CORE_ADDR size doesn't match
1834 conversion directives correctly. The following code will work
1835 with any size of CORE_ADDR. */
1836 text_addr
= data_addr
= bss_addr
= 0;
1840 if (strncmp (ptr
, "Text=", 5) == 0)
1843 /* Don't use strtol, could lose on big values. */
1844 while (*ptr
&& *ptr
!= ';')
1845 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
1850 if (!lose
&& strncmp (ptr
, ";Data=", 6) == 0)
1853 while (*ptr
&& *ptr
!= ';')
1854 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
1859 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
1862 while (*ptr
&& *ptr
!= ';')
1863 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
1869 error (_("Malformed response to offset query, %s"), buf
);
1871 if (symfile_objfile
== NULL
)
1874 offs
= ((struct section_offsets
*)
1875 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
1876 memcpy (offs
, symfile_objfile
->section_offsets
,
1877 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
1879 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
1881 /* This is a temporary kludge to force data and bss to use the same offsets
1882 because that's what nlmconv does now. The real solution requires changes
1883 to the stub and remote.c that I don't have time to do right now. */
1885 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
1886 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
1888 objfile_relocate (symfile_objfile
, offs
);
1891 /* Stub for catch_errors. */
1894 remote_start_remote_dummy (struct ui_out
*uiout
, void *dummy
)
1896 start_remote (); /* Initialize gdb process mechanisms. */
1897 /* NOTE: Return something >=0. A -ve value is reserved for
1898 catch_exceptions. */
1903 remote_start_remote (struct ui_out
*uiout
, void *dummy
)
1905 immediate_quit
++; /* Allow user to interrupt it. */
1907 /* Ack any packet which the remote side has already sent. */
1908 serial_write (remote_desc
, "+", 1);
1910 /* Let the stub know that we want it to return the thread. */
1913 inferior_ptid
= remote_current_thread (inferior_ptid
);
1915 get_offsets (); /* Get text, data & bss offsets. */
1917 putpkt ("?"); /* Initiate a query from remote machine. */
1920 remote_start_remote_dummy (uiout
, dummy
);
1923 /* Open a connection to a remote debugger.
1924 NAME is the filename used for communication. */
1927 remote_open (char *name
, int from_tty
)
1929 remote_open_1 (name
, from_tty
, &remote_ops
, 0, 0);
1932 /* Just like remote_open, but with asynchronous support. */
1934 remote_async_open (char *name
, int from_tty
)
1936 remote_open_1 (name
, from_tty
, &remote_async_ops
, 0, 1);
1939 /* Open a connection to a remote debugger using the extended
1940 remote gdb protocol. NAME is the filename used for communication. */
1943 extended_remote_open (char *name
, int from_tty
)
1945 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */,
1949 /* Just like extended_remote_open, but with asynchronous support. */
1951 extended_remote_async_open (char *name
, int from_tty
)
1953 remote_open_1 (name
, from_tty
, &extended_async_remote_ops
,
1954 1 /*extended_p */, 1 /* async_p */);
1957 /* Generic code for opening a connection to a remote target. */
1960 init_all_packet_configs (void)
1963 for (i
= 0; i
< PACKET_MAX
; i
++)
1964 update_packet_config (&remote_protocol_packets
[i
]);
1967 /* Symbol look-up. */
1970 remote_check_symbols (struct objfile
*objfile
)
1972 struct remote_state
*rs
= get_remote_state ();
1973 char *msg
, *reply
, *tmp
;
1974 struct minimal_symbol
*sym
;
1977 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
1980 /* Allocate a message buffer. We can't reuse the input buffer in RS,
1981 because we need both at the same time. */
1982 msg
= alloca (rs
->remote_packet_size
);
1986 /* Invite target to request symbol lookups. */
1988 putpkt ("qSymbol::");
1989 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1990 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
1992 while (strncmp (reply
, "qSymbol:", 8) == 0)
1995 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
1997 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
1999 xsnprintf (msg
, rs
->remote_packet_size
, "qSymbol::%s", &reply
[8]);
2001 xsnprintf (msg
, rs
->remote_packet_size
, "qSymbol:%s:%s",
2002 paddr_nz (SYMBOL_VALUE_ADDRESS (sym
)),
2005 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2009 static struct serial
*
2010 remote_serial_open (char *name
)
2012 static int udp_warning
= 0;
2014 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2015 of in ser-tcp.c, because it is the remote protocol assuming that the
2016 serial connection is reliable and not the serial connection promising
2018 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
2021 The remote protocol may be unreliable over UDP.\n\
2022 Some events may be lost, rendering further debugging impossible."));
2026 return serial_open (name
);
2030 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
,
2031 int extended_p
, int async_p
)
2033 struct remote_state
*rs
= get_remote_state ();
2035 error (_("To open a remote debug connection, you need to specify what\n"
2036 "serial device is attached to the remote system\n"
2037 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
2039 /* See FIXME above. */
2041 wait_forever_enabled_p
= 1;
2043 reopen_exec_file ();
2046 target_preopen (from_tty
);
2048 unpush_target (target
);
2050 remote_desc
= remote_serial_open (name
);
2052 perror_with_name (name
);
2054 if (baud_rate
!= -1)
2056 if (serial_setbaudrate (remote_desc
, baud_rate
))
2058 /* The requested speed could not be set. Error out to
2059 top level after closing remote_desc. Take care to
2060 set remote_desc to NULL to avoid closing remote_desc
2062 serial_close (remote_desc
);
2064 perror_with_name (name
);
2068 serial_raw (remote_desc
);
2070 /* If there is something sitting in the buffer we might take it as a
2071 response to a command, which would be bad. */
2072 serial_flush_input (remote_desc
);
2076 puts_filtered ("Remote debugging using ");
2077 puts_filtered (name
);
2078 puts_filtered ("\n");
2080 push_target (target
); /* Switch to using remote target now. */
2082 init_all_packet_configs ();
2084 general_thread
= -2;
2085 continue_thread
= -2;
2087 /* Probe for ability to use "ThreadInfo" query, as required. */
2088 use_threadinfo_query
= 1;
2089 use_threadextra_query
= 1;
2091 /* Without this, some commands which require an active target (such
2092 as kill) won't work. This variable serves (at least) double duty
2093 as both the pid of the target process (if it has such), and as a
2094 flag indicating that a target is active. These functions should
2095 be split out into seperate variables, especially since GDB will
2096 someday have a notion of debugging several processes. */
2098 inferior_ptid
= pid_to_ptid (MAGIC_NULL_PID
);
2102 /* With this target we start out by owning the terminal. */
2103 remote_async_terminal_ours_p
= 1;
2105 /* FIXME: cagney/1999-09-23: During the initial connection it is
2106 assumed that the target is already ready and able to respond to
2107 requests. Unfortunately remote_start_remote() eventually calls
2108 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2109 around this. Eventually a mechanism that allows
2110 wait_for_inferior() to expect/get timeouts will be
2112 wait_forever_enabled_p
= 0;
2115 /* First delete any symbols previously loaded from shared libraries. */
2116 no_shared_libraries (NULL
, 0);
2118 /* Start the remote connection. If error() or QUIT, discard this
2119 target (we'd otherwise be in an inconsistent state) and then
2120 propogate the error on up the exception chain. This ensures that
2121 the caller doesn't stumble along blindly assuming that the
2122 function succeeded. The CLI doesn't have this problem but other
2123 UI's, such as MI do.
2125 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2126 this function should return an error indication letting the
2127 caller restore the previous state. Unfortunately the command
2128 ``target remote'' is directly wired to this function making that
2129 impossible. On a positive note, the CLI side of this problem has
2130 been fixed - the function set_cmd_context() makes it possible for
2131 all the ``target ....'' commands to share a common callback
2132 function. See cli-dump.c. */
2134 struct gdb_exception ex
2135 = catch_exception (uiout
, remote_start_remote
, NULL
, RETURN_MASK_ALL
);
2140 wait_forever_enabled_p
= 1;
2141 throw_exception (ex
);
2146 wait_forever_enabled_p
= 1;
2150 /* Tell the remote that we are using the extended protocol. */
2152 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2155 post_create_inferior (¤t_target
, from_tty
);
2157 if (exec_bfd
) /* No use without an exec file. */
2158 remote_check_symbols (symfile_objfile
);
2161 /* This takes a program previously attached to and detaches it. After
2162 this is done, GDB can be used to debug some other program. We
2163 better not have left any breakpoints in the target program or it'll
2164 die when it hits one. */
2167 remote_detach (char *args
, int from_tty
)
2169 struct remote_state
*rs
= get_remote_state ();
2172 error (_("Argument given to \"detach\" when remotely debugging."));
2174 /* Tell the remote target to detach. */
2175 strcpy (rs
->buf
, "D");
2176 remote_send (&rs
->buf
, &rs
->buf_size
);
2178 /* Unregister the file descriptor from the event loop. */
2179 if (target_is_async_p ())
2180 serial_async (remote_desc
, NULL
, 0);
2182 target_mourn_inferior ();
2184 puts_filtered ("Ending remote debugging.\n");
2187 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2190 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
2193 error (_("Argument given to \"detach\" when remotely debugging."));
2195 /* Unregister the file descriptor from the event loop. */
2196 if (target_is_async_p ())
2197 serial_async (remote_desc
, NULL
, 0);
2199 target_mourn_inferior ();
2201 puts_filtered ("Ending remote debugging.\n");
2204 /* Convert hex digit A to a number. */
2209 if (a
>= '0' && a
<= '9')
2211 else if (a
>= 'a' && a
<= 'f')
2212 return a
- 'a' + 10;
2213 else if (a
>= 'A' && a
<= 'F')
2214 return a
- 'A' + 10;
2216 error (_("Reply contains invalid hex digit %d"), a
);
2220 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
2224 for (i
= 0; i
< count
; i
++)
2226 if (hex
[0] == 0 || hex
[1] == 0)
2228 /* Hex string is short, or of uneven length.
2229 Return the count that has been converted so far. */
2232 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
2238 /* Convert number NIB to a hex digit. */
2246 return 'a' + nib
- 10;
2250 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
2253 /* May use a length, or a nul-terminated string as input. */
2255 count
= strlen ((char *) bin
);
2257 for (i
= 0; i
< count
; i
++)
2259 *hex
++ = tohex ((*bin
>> 4) & 0xf);
2260 *hex
++ = tohex (*bin
++ & 0xf);
2266 /* Check for the availability of vCont. This function should also check
2270 remote_vcont_probe (struct remote_state
*rs
)
2272 char *buf
= rs
->buf
;
2274 strcpy (buf
, "vCont?");
2276 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2278 /* Make sure that the features we assume are supported. */
2279 if (strncmp (buf
, "vCont", 5) == 0)
2282 int support_s
, support_S
, support_c
, support_C
;
2288 while (p
&& *p
== ';')
2291 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2293 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2295 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2297 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2300 p
= strchr (p
, ';');
2303 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2304 BUF will make packet_ok disable the packet. */
2305 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
2309 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
2312 /* Resume the remote inferior by using a "vCont" packet. The thread
2313 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2314 resumed thread should be single-stepped and/or signalled. If PTID's
2315 PID is -1, then all threads are resumed; the thread to be stepped and/or
2316 signalled is given in the global INFERIOR_PTID. This function returns
2317 non-zero iff it resumes the inferior.
2319 This function issues a strict subset of all possible vCont commands at the
2323 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2325 struct remote_state
*rs
= get_remote_state ();
2326 int pid
= PIDGET (ptid
);
2327 char *buf
= NULL
, *outbuf
;
2328 struct cleanup
*old_cleanup
;
2330 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
2331 remote_vcont_probe (rs
);
2333 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
2336 /* If we could generate a wider range of packets, we'd have to worry
2337 about overflowing BUF. Should there be a generic
2338 "multi-part-packet" packet? */
2340 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
)
2342 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2343 don't have any PID numbers the inferior will understand. Make sure
2344 to only send forms that do not specify a PID. */
2345 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2346 outbuf
= xstrprintf ("vCont;S%02x", siggnal
);
2348 outbuf
= xstrprintf ("vCont;s");
2349 else if (siggnal
!= TARGET_SIGNAL_0
)
2350 outbuf
= xstrprintf ("vCont;C%02x", siggnal
);
2352 outbuf
= xstrprintf ("vCont;c");
2356 /* Resume all threads, with preference for INFERIOR_PTID. */
2357 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2358 outbuf
= xstrprintf ("vCont;S%02x:%x;c", siggnal
,
2359 PIDGET (inferior_ptid
));
2361 outbuf
= xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid
));
2362 else if (siggnal
!= TARGET_SIGNAL_0
)
2363 outbuf
= xstrprintf ("vCont;C%02x:%x;c", siggnal
,
2364 PIDGET (inferior_ptid
));
2366 outbuf
= xstrprintf ("vCont;c");
2370 /* Scheduler locking; resume only PTID. */
2371 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2372 outbuf
= xstrprintf ("vCont;S%02x:%x", siggnal
, pid
);
2374 outbuf
= xstrprintf ("vCont;s:%x", pid
);
2375 else if (siggnal
!= TARGET_SIGNAL_0
)
2376 outbuf
= xstrprintf ("vCont;C%02x:%x", siggnal
, pid
);
2378 outbuf
= xstrprintf ("vCont;c:%x", pid
);
2381 gdb_assert (outbuf
&& strlen (outbuf
) < rs
->remote_packet_size
);
2382 old_cleanup
= make_cleanup (xfree
, outbuf
);
2386 do_cleanups (old_cleanup
);
2391 /* Tell the remote machine to resume. */
2393 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
2395 static int last_sent_step
;
2398 remote_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2400 struct remote_state
*rs
= get_remote_state ();
2401 char *buf
= rs
->buf
;
2402 int pid
= PIDGET (ptid
);
2404 last_sent_signal
= siggnal
;
2405 last_sent_step
= step
;
2407 /* A hook for when we need to do something at the last moment before
2409 if (deprecated_target_resume_hook
)
2410 (*deprecated_target_resume_hook
) ();
2412 /* The vCont packet doesn't need to specify threads via Hc. */
2413 if (remote_vcont_resume (ptid
, step
, siggnal
))
2416 /* All other supported resume packets do use Hc, so call set_thread. */
2418 set_thread (0, 0); /* Run any thread. */
2420 set_thread (pid
, 0); /* Run this thread. */
2422 if (siggnal
!= TARGET_SIGNAL_0
)
2424 buf
[0] = step
? 'S' : 'C';
2425 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2426 buf
[2] = tohex (((int) siggnal
) & 0xf);
2430 strcpy (buf
, step
? "s" : "c");
2435 /* Same as remote_resume, but with async support. */
2437 remote_async_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2439 remote_resume (ptid
, step
, siggnal
);
2441 /* We are about to start executing the inferior, let's register it
2442 with the event loop. NOTE: this is the one place where all the
2443 execution commands end up. We could alternatively do this in each
2444 of the execution commands in infcmd.c. */
2445 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2446 into infcmd.c in order to allow inferior function calls to work
2447 NOT asynchronously. */
2448 if (target_can_async_p ())
2449 target_async (inferior_event_handler
, 0);
2450 /* Tell the world that the target is now executing. */
2451 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2452 this? Instead, should the client of target just assume (for
2453 async targets) that the target is going to start executing? Is
2454 this information already found in the continuation block? */
2455 if (target_is_async_p ())
2456 target_executing
= 1;
2460 /* Set up the signal handler for SIGINT, while the target is
2461 executing, ovewriting the 'regular' SIGINT signal handler. */
2463 initialize_sigint_signal_handler (void)
2465 sigint_remote_token
=
2466 create_async_signal_handler (async_remote_interrupt
, NULL
);
2467 signal (SIGINT
, handle_remote_sigint
);
2470 /* Signal handler for SIGINT, while the target is executing. */
2472 handle_remote_sigint (int sig
)
2474 signal (sig
, handle_remote_sigint_twice
);
2475 sigint_remote_twice_token
=
2476 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
2477 mark_async_signal_handler_wrapper (sigint_remote_token
);
2480 /* Signal handler for SIGINT, installed after SIGINT has already been
2481 sent once. It will take effect the second time that the user sends
2484 handle_remote_sigint_twice (int sig
)
2486 signal (sig
, handle_sigint
);
2487 sigint_remote_twice_token
=
2488 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
2489 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
2492 /* Perform the real interruption of the target execution, in response
2495 async_remote_interrupt (gdb_client_data arg
)
2498 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2503 /* Perform interrupt, if the first attempt did not succeed. Just give
2504 up on the target alltogether. */
2506 async_remote_interrupt_twice (gdb_client_data arg
)
2509 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
2510 /* Do something only if the target was not killed by the previous
2512 if (target_executing
)
2515 signal (SIGINT
, handle_remote_sigint
);
2519 /* Reinstall the usual SIGINT handlers, after the target has
2522 cleanup_sigint_signal_handler (void *dummy
)
2524 signal (SIGINT
, handle_sigint
);
2525 if (sigint_remote_twice_token
)
2526 delete_async_signal_handler ((struct async_signal_handler
**)
2527 &sigint_remote_twice_token
);
2528 if (sigint_remote_token
)
2529 delete_async_signal_handler ((struct async_signal_handler
**)
2530 &sigint_remote_token
);
2533 /* Send ^C to target to halt it. Target will respond, and send us a
2535 static void (*ofunc
) (int);
2537 /* The command line interface's stop routine. This function is installed
2538 as a signal handler for SIGINT. The first time a user requests a
2539 stop, we call remote_stop to send a break or ^C. If there is no
2540 response from the target (it didn't stop when the user requested it),
2541 we ask the user if he'd like to detach from the target. */
2543 remote_interrupt (int signo
)
2545 /* If this doesn't work, try more severe steps. */
2546 signal (signo
, remote_interrupt_twice
);
2549 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2554 /* The user typed ^C twice. */
2557 remote_interrupt_twice (int signo
)
2559 signal (signo
, ofunc
);
2561 signal (signo
, remote_interrupt
);
2564 /* This is the generic stop called via the target vector. When a target
2565 interrupt is requested, either by the command line or the GUI, we
2566 will eventually end up here. */
2570 /* Send a break or a ^C, depending on user preference. */
2572 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
2575 serial_send_break (remote_desc
);
2577 serial_write (remote_desc
, "\003", 1);
2580 /* Ask the user what to do when an interrupt is received. */
2583 interrupt_query (void)
2585 target_terminal_ours ();
2587 if (query ("Interrupted while waiting for the program.\n\
2588 Give up (and stop debugging it)? "))
2590 target_mourn_inferior ();
2591 deprecated_throw_reason (RETURN_QUIT
);
2594 target_terminal_inferior ();
2597 /* Enable/disable target terminal ownership. Most targets can use
2598 terminal groups to control terminal ownership. Remote targets are
2599 different in that explicit transfer of ownership to/from GDB/target
2603 remote_async_terminal_inferior (void)
2605 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2606 sync_execution here. This function should only be called when
2607 GDB is resuming the inferior in the forground. A background
2608 resume (``run&'') should leave GDB in control of the terminal and
2609 consequently should not call this code. */
2610 if (!sync_execution
)
2612 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2613 calls target_terminal_*() idenpotent. The event-loop GDB talking
2614 to an asynchronous target with a synchronous command calls this
2615 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2616 stops trying to transfer the terminal to the target when it
2617 shouldn't this guard can go away. */
2618 if (!remote_async_terminal_ours_p
)
2620 delete_file_handler (input_fd
);
2621 remote_async_terminal_ours_p
= 0;
2622 initialize_sigint_signal_handler ();
2623 /* NOTE: At this point we could also register our selves as the
2624 recipient of all input. Any characters typed could then be
2625 passed on down to the target. */
2629 remote_async_terminal_ours (void)
2631 /* See FIXME in remote_async_terminal_inferior. */
2632 if (!sync_execution
)
2634 /* See FIXME in remote_async_terminal_inferior. */
2635 if (remote_async_terminal_ours_p
)
2637 cleanup_sigint_signal_handler (NULL
);
2638 add_file_handler (input_fd
, stdin_event_handler
, 0);
2639 remote_async_terminal_ours_p
= 1;
2642 /* If nonzero, ignore the next kill. */
2647 remote_console_output (char *msg
)
2651 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
2654 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
2657 fputs_unfiltered (tb
, gdb_stdtarg
);
2659 gdb_flush (gdb_stdtarg
);
2662 /* Wait until the remote machine stops, then return,
2663 storing status in STATUS just as `wait' would.
2664 Returns "pid", which in the case of a multi-threaded
2665 remote OS, is the thread-id. */
2668 remote_wait (ptid_t ptid
, struct target_waitstatus
*status
)
2670 struct remote_state
*rs
= get_remote_state ();
2671 char *buf
= rs
->buf
;
2672 ULONGEST thread_num
= -1;
2675 status
->kind
= TARGET_WAITKIND_EXITED
;
2676 status
->value
.integer
= 0;
2682 ofunc
= signal (SIGINT
, remote_interrupt
);
2683 getpkt (&rs
->buf
, &rs
->buf_size
, 1);
2684 signal (SIGINT
, ofunc
);
2686 /* This is a hook for when we need to do something (perhaps the
2687 collection of trace data) every time the target stops. */
2688 if (deprecated_target_wait_loop_hook
)
2689 (*deprecated_target_wait_loop_hook
) ();
2691 remote_stopped_by_watchpoint_p
= 0;
2695 case 'E': /* Error of some sort. */
2696 warning (_("Remote failure reply: %s"), buf
);
2698 case 'F': /* File-I/O request. */
2699 remote_fileio_request (buf
);
2701 case 'T': /* Status with PC, SP, FP, ... */
2703 gdb_byte regs
[MAX_REGISTER_SIZE
];
2705 /* Expedited reply, containing Signal, {regno, reg} repeat. */
2706 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2708 n... = register number
2709 r... = register contents
2711 p
= &buf
[3]; /* after Txx */
2720 /* If the packet contains a register number save it in
2721 pnum and set p1 to point to the character following
2722 it. Otherwise p1 points to p. */
2724 /* If this packet is an awatch packet, don't parse the
2725 'a' as a register number. */
2727 if (strncmp (p
, "awatch", strlen("awatch")) != 0)
2729 /* Read the ``P'' register number. */
2730 pnum
= strtol (p
, &p_temp
, 16);
2736 if (p1
== p
) /* No register number present here. */
2738 p1
= strchr (p
, ':');
2740 error (_("Malformed packet(a) (missing colon): %s\n\
2743 if (strncmp (p
, "thread", p1
- p
) == 0)
2745 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2746 record_currthread (thread_num
);
2749 else if ((strncmp (p
, "watch", p1
- p
) == 0)
2750 || (strncmp (p
, "rwatch", p1
- p
) == 0)
2751 || (strncmp (p
, "awatch", p1
- p
) == 0))
2753 remote_stopped_by_watchpoint_p
= 1;
2754 p
= unpack_varlen_hex (++p1
, &addr
);
2755 remote_watch_data_address
= (CORE_ADDR
)addr
;
2759 /* Silently skip unknown optional info. */
2760 p_temp
= strchr (p1
+ 1, ';');
2767 struct packet_reg
*reg
= packet_reg_from_pnum (rs
, pnum
);
2771 error (_("Malformed packet(b) (missing colon): %s\n\
2776 error (_("Remote sent bad register number %s: %s\n\
2778 phex_nz (pnum
, 0), p
, buf
);
2780 fieldsize
= hex2bin (p
, regs
,
2781 register_size (current_gdbarch
,
2784 if (fieldsize
< register_size (current_gdbarch
,
2786 warning (_("Remote reply is too short: %s"), buf
);
2787 regcache_raw_supply (current_regcache
,
2792 error (_("Remote register badly formatted: %s\nhere: %s"),
2797 case 'S': /* Old style status, just signal only. */
2798 status
->kind
= TARGET_WAITKIND_STOPPED
;
2799 status
->value
.sig
= (enum target_signal
)
2800 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2804 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2805 record_currthread (thread_num
);
2808 case 'W': /* Target exited. */
2810 /* The remote process exited. */
2811 status
->kind
= TARGET_WAITKIND_EXITED
;
2812 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2816 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2817 status
->value
.sig
= (enum target_signal
)
2818 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2822 case 'O': /* Console output. */
2823 remote_console_output (buf
+ 1);
2826 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2828 /* Zero length reply means that we tried 'S' or 'C' and
2829 the remote system doesn't support it. */
2830 target_terminal_ours_for_output ();
2832 ("Can't send signals to this remote system. %s not sent.\n",
2833 target_signal_to_name (last_sent_signal
));
2834 last_sent_signal
= TARGET_SIGNAL_0
;
2835 target_terminal_inferior ();
2837 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2838 putpkt ((char *) buf
);
2841 /* else fallthrough */
2843 warning (_("Invalid remote reply: %s"), buf
);
2848 if (thread_num
!= -1)
2850 return pid_to_ptid (thread_num
);
2852 return inferior_ptid
;
2855 /* Async version of remote_wait. */
2857 remote_async_wait (ptid_t ptid
, struct target_waitstatus
*status
)
2859 struct remote_state
*rs
= get_remote_state ();
2860 char *buf
= rs
->buf
;
2861 ULONGEST thread_num
= -1;
2864 status
->kind
= TARGET_WAITKIND_EXITED
;
2865 status
->value
.integer
= 0;
2867 remote_stopped_by_watchpoint_p
= 0;
2873 if (!target_is_async_p ())
2874 ofunc
= signal (SIGINT
, remote_interrupt
);
2875 /* FIXME: cagney/1999-09-27: If we're in async mode we should
2876 _never_ wait for ever -> test on target_is_async_p().
2877 However, before we do that we need to ensure that the caller
2878 knows how to take the target into/out of async mode. */
2879 getpkt (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
2880 if (!target_is_async_p ())
2881 signal (SIGINT
, ofunc
);
2883 /* This is a hook for when we need to do something (perhaps the
2884 collection of trace data) every time the target stops. */
2885 if (deprecated_target_wait_loop_hook
)
2886 (*deprecated_target_wait_loop_hook
) ();
2890 case 'E': /* Error of some sort. */
2891 warning (_("Remote failure reply: %s"), buf
);
2893 case 'F': /* File-I/O request. */
2894 remote_fileio_request (buf
);
2896 case 'T': /* Status with PC, SP, FP, ... */
2898 gdb_byte regs
[MAX_REGISTER_SIZE
];
2900 /* Expedited reply, containing Signal, {regno, reg} repeat. */
2901 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2903 n... = register number
2904 r... = register contents
2906 p
= &buf
[3]; /* after Txx */
2915 /* If the packet contains a register number, save it
2916 in pnum and set p1 to point to the character
2917 following it. Otherwise p1 points to p. */
2919 /* If this packet is an awatch packet, don't parse the 'a'
2920 as a register number. */
2922 if (!strncmp (p
, "awatch", strlen ("awatch")) != 0)
2924 /* Read the register number. */
2925 pnum
= strtol (p
, &p_temp
, 16);
2931 if (p1
== p
) /* No register number present here. */
2933 p1
= strchr (p
, ':');
2935 error (_("Malformed packet(a) (missing colon): %s\n\
2938 if (strncmp (p
, "thread", p1
- p
) == 0)
2940 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2941 record_currthread (thread_num
);
2944 else if ((strncmp (p
, "watch", p1
- p
) == 0)
2945 || (strncmp (p
, "rwatch", p1
- p
) == 0)
2946 || (strncmp (p
, "awatch", p1
- p
) == 0))
2948 remote_stopped_by_watchpoint_p
= 1;
2949 p
= unpack_varlen_hex (++p1
, &addr
);
2950 remote_watch_data_address
= (CORE_ADDR
)addr
;
2954 /* Silently skip unknown optional info. */
2955 p_temp
= strchr (p1
+ 1, ';');
2963 struct packet_reg
*reg
= packet_reg_from_pnum (rs
, pnum
);
2966 error (_("Malformed packet(b) (missing colon): %s\n\
2971 error (_("Remote sent bad register number %ld: %s\n\
2975 fieldsize
= hex2bin (p
, regs
,
2976 register_size (current_gdbarch
,
2979 if (fieldsize
< register_size (current_gdbarch
,
2981 warning (_("Remote reply is too short: %s"), buf
);
2982 regcache_raw_supply (current_regcache
, reg
->regnum
, regs
);
2986 error (_("Remote register badly formatted: %s\nhere: %s"),
2991 case 'S': /* Old style status, just signal only. */
2992 status
->kind
= TARGET_WAITKIND_STOPPED
;
2993 status
->value
.sig
= (enum target_signal
)
2994 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2998 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2999 record_currthread (thread_num
);
3002 case 'W': /* Target exited. */
3004 /* The remote process exited. */
3005 status
->kind
= TARGET_WAITKIND_EXITED
;
3006 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
3010 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
3011 status
->value
.sig
= (enum target_signal
)
3012 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3016 case 'O': /* Console output. */
3017 remote_console_output (buf
+ 1);
3018 /* Return immediately to the event loop. The event loop will
3019 still be waiting on the inferior afterwards. */
3020 status
->kind
= TARGET_WAITKIND_IGNORE
;
3023 if (last_sent_signal
!= TARGET_SIGNAL_0
)
3025 /* Zero length reply means that we tried 'S' or 'C' and
3026 the remote system doesn't support it. */
3027 target_terminal_ours_for_output ();
3029 ("Can't send signals to this remote system. %s not sent.\n",
3030 target_signal_to_name (last_sent_signal
));
3031 last_sent_signal
= TARGET_SIGNAL_0
;
3032 target_terminal_inferior ();
3034 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
3035 putpkt ((char *) buf
);
3038 /* else fallthrough */
3040 warning (_("Invalid remote reply: %s"), buf
);
3045 if (thread_num
!= -1)
3047 return pid_to_ptid (thread_num
);
3049 return inferior_ptid
;
3052 /* Number of bytes of registers this stub implements. */
3054 static int register_bytes_found
;
3056 /* Read the remote registers into the block REGS. */
3057 /* Currently we just read all the registers, so we don't use regnum. */
3060 fetch_register_using_p (int regnum
)
3062 struct remote_state
*rs
= get_remote_state ();
3063 char *buf
= rs
->buf
, *p
;
3064 char regp
[MAX_REGISTER_SIZE
];
3069 p
+= hexnumstr (p
, regnum
);
3071 remote_send (&rs
->buf
, &rs
->buf_size
);
3073 /* If the stub didn't recognize the packet, or if we got an error,
3075 if (buf
[0] == '\0' || buf
[0] == 'E')
3078 /* If this register is unfetchable, tell the regcache. */
3081 regcache_raw_supply (current_regcache
, regnum
, NULL
);
3082 set_register_cached (regnum
, -1);
3086 /* Otherwise, parse and supply the value. */
3093 error (_("fetch_register_using_p: early buf termination"));
3097 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3100 regcache_raw_supply (current_regcache
, regnum
, regp
);
3105 remote_fetch_registers (int regnum
)
3107 struct remote_state
*rs
= get_remote_state ();
3108 char *buf
= rs
->buf
;
3111 char *regs
= alloca (rs
->sizeof_g_packet
);
3113 set_thread (PIDGET (inferior_ptid
), 1);
3117 struct packet_reg
*reg
= packet_reg_from_regnum (rs
, regnum
);
3118 gdb_assert (reg
!= NULL
);
3119 if (!reg
->in_g_packet
)
3120 internal_error (__FILE__
, __LINE__
,
3121 _("Attempt to fetch a non G-packet register when this "
3122 "remote.c does not support the p-packet."));
3124 switch (remote_protocol_packets
[PACKET_p
].support
)
3126 case PACKET_DISABLE
:
3129 if (fetch_register_using_p (regnum
))
3132 error (_("Protocol error: p packet not recognized by stub"));
3133 case PACKET_SUPPORT_UNKNOWN
:
3134 if (fetch_register_using_p (regnum
))
3136 /* The stub recognized the 'p' packet. Remember this. */
3137 remote_protocol_packets
[PACKET_p
].support
= PACKET_ENABLE
;
3142 /* The stub does not support the 'P' packet. Use 'G'
3143 instead, and don't try using 'P' in the future (it
3144 will just waste our time). */
3145 remote_protocol_packets
[PACKET_p
].support
= PACKET_DISABLE
;
3151 remote_send (&rs
->buf
, &rs
->buf_size
);
3153 /* Save the size of the packet sent to us by the target. Its used
3154 as a heuristic when determining the max size of packets that the
3155 target can safely receive. */
3156 if ((rs
->actual_register_packet_size
) == 0)
3157 (rs
->actual_register_packet_size
) = strlen (buf
);
3159 /* Unimplemented registers read as all bits zero. */
3160 memset (regs
, 0, rs
->sizeof_g_packet
);
3162 /* We can get out of synch in various cases. If the first character
3163 in the buffer is not a hex character, assume that has happened
3164 and try to fetch another packet to read. */
3165 while ((buf
[0] < '0' || buf
[0] > '9')
3166 && (buf
[0] < 'A' || buf
[0] > 'F')
3167 && (buf
[0] < 'a' || buf
[0] > 'f')
3168 && buf
[0] != 'x') /* New: unavailable register value. */
3171 fprintf_unfiltered (gdb_stdlog
,
3172 "Bad register packet; fetching a new packet\n");
3173 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3176 /* Reply describes registers byte by byte, each byte encoded as two
3177 hex characters. Suck them all up, then supply them to the
3178 register cacheing/storage mechanism. */
3181 for (i
= 0; i
< rs
->sizeof_g_packet
; i
++)
3187 warning (_("Remote reply is of odd length: %s"), buf
);
3188 /* Don't change register_bytes_found in this case, and don't
3189 print a second warning. */
3192 if (p
[0] == 'x' && p
[1] == 'x')
3193 regs
[i
] = 0; /* 'x' */
3195 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3199 if (i
!= register_bytes_found
)
3201 register_bytes_found
= i
;
3202 if (REGISTER_BYTES_OK_P ()
3203 && !REGISTER_BYTES_OK (i
))
3204 warning (_("Remote reply is too short: %s"), buf
);
3210 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
3212 struct packet_reg
*r
= &rs
->regs
[i
];
3215 if (r
->offset
* 2 >= strlen (buf
))
3216 /* A short packet that didn't include the register's
3217 value, this implies that the register is zero (and
3218 not that the register is unavailable). Supply that
3220 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3221 else if (buf
[r
->offset
* 2] == 'x')
3223 gdb_assert (r
->offset
* 2 < strlen (buf
));
3224 /* The register isn't available, mark it as such (at
3225 the same time setting the value to zero). */
3226 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3227 set_register_cached (i
, -1);
3230 regcache_raw_supply (current_regcache
, r
->regnum
,
3237 /* Prepare to store registers. Since we may send them all (using a
3238 'G' request), we have to read out the ones we don't want to change
3242 remote_prepare_to_store (void)
3244 struct remote_state
*rs
= get_remote_state ();
3246 gdb_byte buf
[MAX_REGISTER_SIZE
];
3248 /* Make sure the entire registers array is valid. */
3249 switch (remote_protocol_packets
[PACKET_P
].support
)
3251 case PACKET_DISABLE
:
3252 case PACKET_SUPPORT_UNKNOWN
:
3253 /* Make sure all the necessary registers are cached. */
3254 for (i
= 0; i
< NUM_REGS
; i
++)
3255 if (rs
->regs
[i
].in_g_packet
)
3256 regcache_raw_read (current_regcache
, rs
->regs
[i
].regnum
, buf
);
3263 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
3264 packet was not recognized. */
3267 store_register_using_P (int regnum
)
3269 struct remote_state
*rs
= get_remote_state ();
3270 struct packet_reg
*reg
= packet_reg_from_regnum (rs
, regnum
);
3271 /* Try storing a single register. */
3272 char *buf
= rs
->buf
;
3273 gdb_byte regp
[MAX_REGISTER_SIZE
];
3276 xsnprintf (buf
, rs
->remote_packet_size
, "P%s=", phex_nz (reg
->pnum
, 0));
3277 p
= buf
+ strlen (buf
);
3278 regcache_raw_collect (current_regcache
, reg
->regnum
, regp
);
3279 bin2hex (regp
, p
, register_size (current_gdbarch
, reg
->regnum
));
3280 remote_send (&rs
->buf
, &rs
->buf_size
);
3282 return buf
[0] != '\0';
3286 /* Store register REGNUM, or all registers if REGNUM == -1, from the
3287 contents of the register cache buffer. FIXME: ignores errors. */
3290 remote_store_registers (int regnum
)
3292 struct remote_state
*rs
= get_remote_state ();
3296 set_thread (PIDGET (inferior_ptid
), 1);
3300 switch (remote_protocol_packets
[PACKET_P
].support
)
3302 case PACKET_DISABLE
:
3305 if (store_register_using_P (regnum
))
3308 error (_("Protocol error: P packet not recognized by stub"));
3309 case PACKET_SUPPORT_UNKNOWN
:
3310 if (store_register_using_P (regnum
))
3312 /* The stub recognized the 'P' packet. Remember this. */
3313 remote_protocol_packets
[PACKET_P
].support
= PACKET_ENABLE
;
3318 /* The stub does not support the 'P' packet. Use 'G'
3319 instead, and don't try using 'P' in the future (it
3320 will just waste our time). */
3321 remote_protocol_packets
[PACKET_P
].support
= PACKET_DISABLE
;
3327 /* Extract all the registers in the regcache copying them into a
3331 regs
= alloca (rs
->sizeof_g_packet
);
3332 memset (regs
, 0, rs
->sizeof_g_packet
);
3333 for (i
= 0; i
< NUM_REGS
+ NUM_PSEUDO_REGS
; i
++)
3335 struct packet_reg
*r
= &rs
->regs
[i
];
3337 regcache_raw_collect (current_regcache
, r
->regnum
, regs
+ r
->offset
);
3341 /* Command describes registers byte by byte,
3342 each byte encoded as two hex characters. */
3345 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3346 bin2hex (regs
, p
, register_bytes_found
);
3347 remote_send (&rs
->buf
, &rs
->buf_size
);
3351 /* Return the number of hex digits in num. */
3354 hexnumlen (ULONGEST num
)
3358 for (i
= 0; num
!= 0; i
++)
3364 /* Set BUF to the minimum number of hex digits representing NUM. */
3367 hexnumstr (char *buf
, ULONGEST num
)
3369 int len
= hexnumlen (num
);
3370 return hexnumnstr (buf
, num
, len
);
3374 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3377 hexnumnstr (char *buf
, ULONGEST num
, int width
)
3383 for (i
= width
- 1; i
>= 0; i
--)
3385 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
3392 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3395 remote_address_masked (CORE_ADDR addr
)
3397 if (remote_address_size
> 0
3398 && remote_address_size
< (sizeof (ULONGEST
) * 8))
3400 /* Only create a mask when that mask can safely be constructed
3401 in a ULONGEST variable. */
3403 mask
= (mask
<< remote_address_size
) - 1;
3409 /* Determine whether the remote target supports binary downloading.
3410 This is accomplished by sending a no-op memory write of zero length
3411 to the target at the specified address. It does not suffice to send
3412 the whole packet, since many stubs strip the eighth bit and
3413 subsequently compute a wrong checksum, which causes real havoc with
3416 NOTE: This can still lose if the serial line is not eight-bit
3417 clean. In cases like this, the user should clear "remote
3421 check_binary_download (CORE_ADDR addr
)
3423 struct remote_state
*rs
= get_remote_state ();
3425 switch (remote_protocol_packets
[PACKET_X
].support
)
3427 case PACKET_DISABLE
:
3431 case PACKET_SUPPORT_UNKNOWN
:
3433 char *buf
= rs
->buf
;
3438 p
+= hexnumstr (p
, (ULONGEST
) addr
);
3440 p
+= hexnumstr (p
, (ULONGEST
) 0);
3444 putpkt_binary (buf
, (int) (p
- buf
));
3445 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3450 fprintf_unfiltered (gdb_stdlog
,
3451 "binary downloading NOT suppported by target\n");
3452 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
3457 fprintf_unfiltered (gdb_stdlog
,
3458 "binary downloading suppported by target\n");
3459 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
3466 /* Write memory data directly to the remote machine.
3467 This does not inform the data cache; the data cache uses this.
3468 MEMADDR is the address in the remote memory space.
3469 MYADDR is the address of the buffer in our space.
3470 LEN is the number of bytes.
3472 Returns number of bytes transferred, or 0 (setting errno) for
3473 error. Only transfer a single packet. */
3476 remote_write_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
3478 struct remote_state
*rs
= get_remote_state ();
3486 char *payload_start
;
3488 /* Verify that the target can support a binary download. */
3489 check_binary_download (memaddr
);
3491 payload_size
= get_memory_write_packet_size ();
3493 /* The packet buffer will be large enough for the payload;
3494 get_memory_packet_size ensures this. */
3497 /* Compute the size of the actual payload by subtracting out the
3498 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
3500 payload_size
-= strlen ("$M,:#NN");
3501 payload_size
-= hexnumlen (memaddr
);
3503 /* Construct the packet header: "[MX]<memaddr>,<len>:". */
3505 /* Append "[XM]". Compute a best guess of the number of bytes
3506 actually transfered. */
3508 switch (remote_protocol_packets
[PACKET_X
].support
)
3512 /* Best guess at number of bytes that will fit. */
3513 todo
= min (len
, payload_size
);
3514 payload_size
-= hexnumlen (todo
);
3515 todo
= min (todo
, payload_size
);
3517 case PACKET_DISABLE
:
3519 /* Num bytes that will fit. */
3520 todo
= min (len
, payload_size
/ 2);
3521 payload_size
-= hexnumlen (todo
);
3522 todo
= min (todo
, payload_size
/ 2);
3524 case PACKET_SUPPORT_UNKNOWN
:
3525 internal_error (__FILE__
, __LINE__
,
3526 _("remote_write_bytes: bad internal state"));
3528 internal_error (__FILE__
, __LINE__
, _("bad switch"));
3531 internal_error (__FILE__
, __LINE__
,
3532 _("minumum packet size too small to write data"));
3534 /* Append "<memaddr>". */
3535 memaddr
= remote_address_masked (memaddr
);
3536 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3541 /* Append <len>. Retain the location/size of <len>. It may need to
3542 be adjusted once the packet body has been created. */
3544 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
3551 /* Append the packet body. */
3553 switch (remote_protocol_packets
[PACKET_X
].support
)
3556 /* Binary mode. Send target system values byte by byte, in
3557 increasing byte addresses. Only escape certain critical
3560 (nr_bytes
< todo
) && (p
- payload_start
) < payload_size
;
3563 switch (myaddr
[nr_bytes
] & 0xff)
3568 /* These must be escaped. */
3570 *p
++ = (myaddr
[nr_bytes
] & 0xff) ^ 0x20;
3573 *p
++ = myaddr
[nr_bytes
] & 0xff;
3577 if (nr_bytes
< todo
)
3579 /* Escape chars have filled up the buffer prematurely,
3580 and we have actually sent fewer bytes than planned.
3581 Fix-up the length field of the packet. Use the same
3582 number of characters as before. */
3583 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
3584 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
3587 case PACKET_DISABLE
:
3588 /* Normal mode: Send target system values byte by byte, in
3589 increasing byte addresses. Each byte is encoded as a two hex
3591 nr_bytes
= bin2hex (myaddr
, p
, todo
);
3594 case PACKET_SUPPORT_UNKNOWN
:
3595 internal_error (__FILE__
, __LINE__
,
3596 _("remote_write_bytes: bad internal state"));
3598 internal_error (__FILE__
, __LINE__
, _("bad switch"));
3601 putpkt_binary (buf
, (int) (p
- buf
));
3602 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3606 /* There is no correspondance between what the remote protocol
3607 uses for errors and errno codes. We would like a cleaner way
3608 of representing errors (big enough to include errno codes,
3609 bfd_error codes, and others). But for now just return EIO. */
3614 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
3615 fewer bytes than we'd planned. */
3619 /* Read memory data directly from the remote machine.
3620 This does not use the data cache; the data cache uses this.
3621 MEMADDR is the address in the remote memory space.
3622 MYADDR is the address of the buffer in our space.
3623 LEN is the number of bytes.
3625 Returns number of bytes transferred, or 0 for error. */
3627 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3628 remote targets) shouldn't attempt to read the entire buffer.
3629 Instead it should read a single packet worth of data and then
3630 return the byte size of that packet to the caller. The caller (its
3631 caller and its callers caller ;-) already contains code for
3632 handling partial reads. */
3635 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
3637 struct remote_state
*rs
= get_remote_state ();
3639 int max_buf_size
; /* Max size of packet output buffer. */
3642 max_buf_size
= get_memory_read_packet_size ();
3643 /* The packet buffer will be large enough for the payload;
3644 get_memory_packet_size ensures this. */
3654 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
3656 /* construct "m"<memaddr>","<len>" */
3657 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3658 memaddr
= remote_address_masked (memaddr
);
3661 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3663 p
+= hexnumstr (p
, (ULONGEST
) todo
);
3667 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3670 && isxdigit (buf
[1]) && isxdigit (buf
[2])
3673 /* There is no correspondance between what the remote
3674 protocol uses for errors and errno codes. We would like
3675 a cleaner way of representing errors (big enough to
3676 include errno codes, bfd_error codes, and others). But
3677 for now just return EIO. */
3682 /* Reply describes memory byte by byte,
3683 each byte encoded as two hex characters. */
3686 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
3688 /* Reply is short. This means that we were able to read
3689 only part of what we wanted to. */
3690 return i
+ (origlen
- len
);
3699 /* Read or write LEN bytes from inferior memory at MEMADDR,
3700 transferring to or from debugger address BUFFER. Write to inferior
3701 if SHOULD_WRITE is nonzero. Returns length of data written or
3702 read; 0 for error. TARGET is unused. */
3705 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
3706 int should_write
, struct mem_attrib
*attrib
,
3707 struct target_ops
*target
)
3709 CORE_ADDR targ_addr
;
3713 /* Should this be the selected frame? */
3714 gdbarch_remote_translate_xfer_address (current_gdbarch
,
3717 &targ_addr
, &targ_len
);
3722 res
= remote_write_bytes (targ_addr
, buffer
, targ_len
);
3724 res
= remote_read_bytes (targ_addr
, buffer
, targ_len
);
3730 remote_files_info (struct target_ops
*ignore
)
3732 puts_filtered ("Debugging a target over a serial line.\n");
3735 /* Stuff for dealing with the packets which are part of this protocol.
3736 See comment at top of file for details. */
3738 /* Read a single character from the remote end, masking it down to 7
3742 readchar (int timeout
)
3746 ch
= serial_readchar (remote_desc
, timeout
);
3751 switch ((enum serial_rc
) ch
)
3754 target_mourn_inferior ();
3755 error (_("Remote connection closed"));
3758 perror_with_name (_("Remote communication error"));
3760 case SERIAL_TIMEOUT
:
3766 /* Send the command in *BUF to the remote machine, and read the reply
3767 into *BUF. Report an error if we get an error reply. Resize
3768 *BUF using xrealloc if necessary to hold the result, and update
3772 remote_send (char **buf
,
3776 getpkt (buf
, sizeof_buf
, 0);
3778 if ((*buf
)[0] == 'E')
3779 error (_("Remote failure reply: %s"), *buf
);
3782 /* Display a null-terminated packet on stdout, for debugging, using C
3786 print_packet (char *buf
)
3788 puts_filtered ("\"");
3789 fputstr_filtered (buf
, '"', gdb_stdout
);
3790 puts_filtered ("\"");
3796 return putpkt_binary (buf
, strlen (buf
));
3799 /* Send a packet to the remote machine, with error checking. The data
3800 of the packet is in BUF. The string in BUF can be at most
3801 RS->remote_packet_size - 5 to account for the $, # and checksum,
3802 and for a possible /0 if we are debugging (remote_debug) and want
3803 to print the sent packet as a string. */
3806 putpkt_binary (char *buf
, int cnt
)
3808 struct remote_state
*rs
= get_remote_state ();
3810 unsigned char csum
= 0;
3811 char *buf2
= alloca (cnt
+ 6);
3817 /* Copy the packet into buffer BUF2, encapsulating it
3818 and giving it a checksum. */
3823 for (i
= 0; i
< cnt
; i
++)
3829 *p
++ = tohex ((csum
>> 4) & 0xf);
3830 *p
++ = tohex (csum
& 0xf);
3832 /* Send it over and over until we get a positive ack. */
3836 int started_error_output
= 0;
3841 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
3842 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
3843 fprintf_unfiltered (gdb_stdlog
, "...");
3844 gdb_flush (gdb_stdlog
);
3846 if (serial_write (remote_desc
, buf2
, p
- buf2
))
3847 perror_with_name (_("putpkt: write failed"));
3849 /* Read until either a timeout occurs (-2) or '+' is read. */
3852 ch
= readchar (remote_timeout
);
3860 case SERIAL_TIMEOUT
:
3862 if (started_error_output
)
3864 putchar_unfiltered ('\n');
3865 started_error_output
= 0;
3874 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
3878 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
3879 case SERIAL_TIMEOUT
:
3883 break; /* Retransmit buffer. */
3887 fprintf_unfiltered (gdb_stdlog
,
3888 "Packet instead of Ack, ignoring it\n");
3889 /* It's probably an old response sent because an ACK
3890 was lost. Gobble up the packet and ack it so it
3891 doesn't get retransmitted when we resend this
3894 serial_write (remote_desc
, "+", 1);
3895 continue; /* Now, go look for +. */
3900 if (!started_error_output
)
3902 started_error_output
= 1;
3903 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
3905 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
3909 break; /* Here to retransmit. */
3913 /* This is wrong. If doing a long backtrace, the user should be
3914 able to get out next time we call QUIT, without anything as
3915 violent as interrupt_query. If we want to provide a way out of
3916 here without getting to the next QUIT, it should be based on
3917 hitting ^C twice as in remote_wait. */
3927 /* Come here after finding the start of a frame when we expected an
3928 ack. Do our best to discard the rest of this packet. */
3937 c
= readchar (remote_timeout
);
3940 case SERIAL_TIMEOUT
:
3941 /* Nothing we can do. */
3944 /* Discard the two bytes of checksum and stop. */
3945 c
= readchar (remote_timeout
);
3947 c
= readchar (remote_timeout
);
3950 case '*': /* Run length encoding. */
3951 /* Discard the repeat count. */
3952 c
= readchar (remote_timeout
);
3957 /* A regular character. */
3963 /* Come here after finding the start of the frame. Collect the rest
3964 into *BUF, verifying the checksum, length, and handling run-length
3965 compression. NUL terminate the buffer. If there is not enough room,
3966 expand *BUF using xrealloc.
3968 Returns -1 on error, number of characters in buffer (ignoring the
3969 trailing NULL) on success. (could be extended to return one of the
3970 SERIAL status indications). */
3973 read_frame (char **buf_p
,
3986 c
= readchar (remote_timeout
);
3989 case SERIAL_TIMEOUT
:
3991 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
3995 fputs_filtered ("Saw new packet start in middle of old one\n",
3997 return -1; /* Start a new packet, count retries. */
4000 unsigned char pktcsum
;
4006 check_0
= readchar (remote_timeout
);
4008 check_1
= readchar (remote_timeout
);
4010 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
4013 fputs_filtered ("Timeout in checksum, retrying\n",
4017 else if (check_0
< 0 || check_1
< 0)
4020 fputs_filtered ("Communication error in checksum\n",
4025 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
4026 if (csum
== pktcsum
)
4031 fprintf_filtered (gdb_stdlog
,
4032 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4034 fputs_filtered (buf
, gdb_stdlog
);
4035 fputs_filtered ("\n", gdb_stdlog
);
4037 /* Number of characters in buffer ignoring trailing
4041 case '*': /* Run length encoding. */
4046 c
= readchar (remote_timeout
);
4048 repeat
= c
- ' ' + 3; /* Compute repeat count. */
4050 /* The character before ``*'' is repeated. */
4052 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
4054 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
4056 /* Make some more room in the buffer. */
4057 *sizeof_buf
+= repeat
;
4058 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
4062 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
4068 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
4072 if (bc
>= *sizeof_buf
- 1)
4074 /* Make some more room in the buffer. */
4076 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
4087 /* Read a packet from the remote machine, with error checking, and
4088 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4089 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4090 rather than timing out; this is used (in synchronous mode) to wait
4091 for a target that is is executing user code to stop. */
4092 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4093 don't have to change all the calls to getpkt to deal with the
4094 return value, because at the moment I don't know what the right
4095 thing to do it for those. */
4103 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
4107 /* Read a packet from the remote machine, with error checking, and
4108 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4109 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4110 rather than timing out; this is used (in synchronous mode) to wait
4111 for a target that is is executing user code to stop. If FOREVER ==
4112 0, this function is allowed to time out gracefully and return an
4113 indication of this to the caller. */
4115 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
4122 strcpy (*buf
, "timeout");
4126 timeout
= watchdog
> 0 ? watchdog
: -1;
4130 timeout
= remote_timeout
;
4134 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
4136 /* This can loop forever if the remote side sends us characters
4137 continuously, but if it pauses, we'll get a zero from
4138 readchar because of timeout. Then we'll count that as a
4141 /* Note that we will only wait forever prior to the start of a
4142 packet. After that, we expect characters to arrive at a
4143 brisk pace. They should show up within remote_timeout
4148 c
= readchar (timeout
);
4150 if (c
== SERIAL_TIMEOUT
)
4152 if (forever
) /* Watchdog went off? Kill the target. */
4155 target_mourn_inferior ();
4156 error (_("Watchdog has expired. Target detached."));
4159 fputs_filtered ("Timed out.\n", gdb_stdlog
);
4165 /* We've found the start of a packet, now collect the data. */
4167 val
= read_frame (buf
, sizeof_buf
);
4173 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
4174 fputstr_unfiltered (*buf
, 0, gdb_stdlog
);
4175 fprintf_unfiltered (gdb_stdlog
, "\n");
4177 serial_write (remote_desc
, "+", 1);
4181 /* Try the whole thing again. */
4183 serial_write (remote_desc
, "-", 1);
4186 /* We have tried hard enough, and just can't receive the packet.
4189 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
4190 serial_write (remote_desc
, "+", 1);
4197 /* For some mysterious reason, wait_for_inferior calls kill instead of
4198 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4202 target_mourn_inferior ();
4206 /* Use catch_errors so the user can quit from gdb even when we aren't on
4207 speaking terms with the remote system. */
4208 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4210 /* Don't wait for it to die. I'm not really sure it matters whether
4211 we do or not. For the existing stubs, kill is a noop. */
4212 target_mourn_inferior ();
4215 /* Async version of remote_kill. */
4217 remote_async_kill (void)
4219 /* Unregister the file descriptor from the event loop. */
4220 if (target_is_async_p ())
4221 serial_async (remote_desc
, NULL
, 0);
4223 /* For some mysterious reason, wait_for_inferior calls kill instead of
4224 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4228 target_mourn_inferior ();
4232 /* Use catch_errors so the user can quit from gdb even when we
4233 aren't on speaking terms with the remote system. */
4234 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4236 /* Don't wait for it to die. I'm not really sure it matters whether
4237 we do or not. For the existing stubs, kill is a noop. */
4238 target_mourn_inferior ();
4244 remote_mourn_1 (&remote_ops
);
4248 remote_async_mourn (void)
4250 remote_mourn_1 (&remote_async_ops
);
4254 extended_remote_mourn (void)
4256 /* We do _not_ want to mourn the target like this; this will
4257 remove the extended remote target from the target stack,
4258 and the next time the user says "run" it'll fail.
4260 FIXME: What is the right thing to do here? */
4262 remote_mourn_1 (&extended_remote_ops
);
4266 /* Worker function for remote_mourn. */
4268 remote_mourn_1 (struct target_ops
*target
)
4270 unpush_target (target
);
4271 generic_mourn_inferior ();
4274 /* In the extended protocol we want to be able to do things like
4275 "run" and have them basically work as expected. So we need
4276 a special create_inferior function.
4278 FIXME: One day add support for changing the exec file
4279 we're debugging, arguments and an environment. */
4282 extended_remote_create_inferior (char *exec_file
, char *args
,
4283 char **env
, int from_tty
)
4285 /* Rip out the breakpoints; we'll reinsert them after restarting
4286 the remote server. */
4287 remove_breakpoints ();
4289 /* Now restart the remote server. */
4290 extended_remote_restart ();
4292 /* Now put the breakpoints back in. This way we're safe if the
4293 restart function works via a unix fork on the remote side. */
4294 insert_breakpoints ();
4296 /* Clean up from the last time we were running. */
4297 clear_proceed_status ();
4300 /* Async version of extended_remote_create_inferior. */
4302 extended_remote_async_create_inferior (char *exec_file
, char *args
,
4303 char **env
, int from_tty
)
4305 /* Rip out the breakpoints; we'll reinsert them after restarting
4306 the remote server. */
4307 remove_breakpoints ();
4309 /* If running asynchronously, register the target file descriptor
4310 with the event loop. */
4311 if (target_can_async_p ())
4312 target_async (inferior_event_handler
, 0);
4314 /* Now restart the remote server. */
4315 extended_remote_restart ();
4317 /* Now put the breakpoints back in. This way we're safe if the
4318 restart function works via a unix fork on the remote side. */
4319 insert_breakpoints ();
4321 /* Clean up from the last time we were running. */
4322 clear_proceed_status ();
4326 /* On some machines, e.g. 68k, we may use a different breakpoint
4327 instruction than other targets; in those use
4328 DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4329 Also, bi-endian targets may define
4330 DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4331 DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we
4332 just call the standard routines that are in mem-break.c. */
4334 /* NOTE: cagney/2003-06-08: This is silly. A remote and simulator
4335 target should use an identical BREAKPOINT_FROM_PC. As for native,
4336 the ARCH-OS-tdep.c code can override the default. */
4338 #if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4339 #define DEPRECATED_REMOTE_BREAKPOINT
4342 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4344 /* If the target isn't bi-endian, just pretend it is. */
4345 #if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4346 #define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4347 #define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4350 static unsigned char big_break_insn
[] = DEPRECATED_BIG_REMOTE_BREAKPOINT
;
4351 static unsigned char little_break_insn
[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT
;
4353 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4355 /* Insert a breakpoint. On targets that have software breakpoint
4356 support, we ask the remote target to do the work; on targets
4357 which don't, we insert a traditional memory breakpoint. */
4360 remote_insert_breakpoint (struct bp_target_info
*bp_tgt
)
4362 CORE_ADDR addr
= bp_tgt
->placed_address
;
4363 struct remote_state
*rs
= get_remote_state ();
4364 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4368 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4369 If it succeeds, then set the support to PACKET_ENABLE. If it
4370 fails, and the user has explicitly requested the Z support then
4371 report an error, otherwise, mark it disabled and go on. */
4373 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
4380 BREAKPOINT_FROM_PC (&bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
4381 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
4382 p
+= hexnumstr (p
, addr
);
4383 sprintf (p
, ",%d", bp_tgt
->placed_size
);
4386 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4388 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
4394 case PACKET_UNKNOWN
:
4399 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4400 bp_tgt
->placed_size
= bp_tgt
->shadow_len
= sizeof big_break_insn
;
4401 val
= target_read_memory (addr
, bp_tgt
->shadow_contents
, bp_tgt
->shadow_len
);
4405 if (TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
4406 val
= target_write_memory (addr
, (char *) big_break_insn
,
4407 sizeof big_break_insn
);
4409 val
= target_write_memory (addr
, (char *) little_break_insn
,
4410 sizeof little_break_insn
);
4415 return memory_insert_breakpoint (bp_tgt
);
4416 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4420 remote_remove_breakpoint (struct bp_target_info
*bp_tgt
)
4422 CORE_ADDR addr
= bp_tgt
->placed_address
;
4423 struct remote_state
*rs
= get_remote_state ();
4426 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
4434 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
4435 p
+= hexnumstr (p
, addr
);
4436 sprintf (p
, ",%d", bp_tgt
->placed_size
);
4439 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4441 return (rs
->buf
[0] == 'E');
4444 #ifdef DEPRECATED_REMOTE_BREAKPOINT
4445 return target_write_memory (bp_tgt
->placed_address
, bp_tgt
->shadow_contents
,
4446 bp_tgt
->shadow_len
);
4448 return memory_remove_breakpoint (bp_tgt
);
4449 #endif /* DEPRECATED_REMOTE_BREAKPOINT */
4453 watchpoint_to_Z_packet (int type
)
4458 return Z_PACKET_WRITE_WP
;
4461 return Z_PACKET_READ_WP
;
4464 return Z_PACKET_ACCESS_WP
;
4467 internal_error (__FILE__
, __LINE__
,
4468 _("hw_bp_to_z: bad watchpoint type %d"), type
);
4473 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
4475 struct remote_state
*rs
= get_remote_state ();
4477 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
4479 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
4480 error (_("Can't set hardware watchpoints without the '%s' (%s) packet."),
4481 remote_protocol_packets
[PACKET_Z0
+ packet
].name
,
4482 remote_protocol_packets
[PACKET_Z0
+ packet
].title
);
4484 sprintf (rs
->buf
, "Z%x,", packet
);
4485 p
= strchr (rs
->buf
, '\0');
4486 addr
= remote_address_masked (addr
);
4487 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4488 sprintf (p
, ",%x", len
);
4491 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4493 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
4496 case PACKET_UNKNOWN
:
4501 internal_error (__FILE__
, __LINE__
,
4502 _("remote_insert_watchpoint: reached end of function"));
4507 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
4509 struct remote_state
*rs
= get_remote_state ();
4511 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
4513 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
4514 error (_("Can't clear hardware watchpoints without the '%s' (%s) packet."),
4515 remote_protocol_packets
[PACKET_Z0
+ packet
].name
,
4516 remote_protocol_packets
[PACKET_Z0
+ packet
].title
);
4518 sprintf (rs
->buf
, "z%x,", packet
);
4519 p
= strchr (rs
->buf
, '\0');
4520 addr
= remote_address_masked (addr
);
4521 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4522 sprintf (p
, ",%x", len
);
4524 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4526 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
4529 case PACKET_UNKNOWN
:
4534 internal_error (__FILE__
, __LINE__
,
4535 _("remote_remove_watchpoint: reached end of function"));
4539 int remote_hw_watchpoint_limit
= -1;
4540 int remote_hw_breakpoint_limit
= -1;
4543 remote_check_watch_resources (int type
, int cnt
, int ot
)
4545 if (type
== bp_hardware_breakpoint
)
4547 if (remote_hw_breakpoint_limit
== 0)
4549 else if (remote_hw_breakpoint_limit
< 0)
4551 else if (cnt
<= remote_hw_breakpoint_limit
)
4556 if (remote_hw_watchpoint_limit
== 0)
4558 else if (remote_hw_watchpoint_limit
< 0)
4562 else if (cnt
<= remote_hw_watchpoint_limit
)
4569 remote_stopped_by_watchpoint (void)
4571 return remote_stopped_by_watchpoint_p
;
4574 extern int stepped_after_stopped_by_watchpoint
;
4577 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
4580 if (remote_stopped_by_watchpoint ()
4581 || stepped_after_stopped_by_watchpoint
)
4583 *addr_p
= remote_watch_data_address
;
4592 remote_insert_hw_breakpoint (struct bp_target_info
*bp_tgt
)
4595 struct remote_state
*rs
= get_remote_state ();
4598 /* The length field should be set to the size of a breakpoint
4599 instruction, even though we aren't inserting one ourselves. */
4601 BREAKPOINT_FROM_PC (&bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
4603 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
4604 error (_("Can't set hardware breakpoint without the '%s' (%s) packet."),
4605 remote_protocol_packets
[PACKET_Z1
].name
,
4606 remote_protocol_packets
[PACKET_Z1
].title
);
4612 addr
= remote_address_masked (bp_tgt
->placed_address
);
4613 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4614 sprintf (p
, ",%x", bp_tgt
->placed_size
);
4617 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4619 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
4622 case PACKET_UNKNOWN
:
4627 internal_error (__FILE__
, __LINE__
,
4628 _("remote_insert_hw_breakpoint: reached end of function"));
4633 remote_remove_hw_breakpoint (struct bp_target_info
*bp_tgt
)
4636 struct remote_state
*rs
= get_remote_state ();
4639 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
4640 error (_("Can't clear hardware breakpoint without the '%s' (%s) packet."),
4641 remote_protocol_packets
[PACKET_Z1
].name
,
4642 remote_protocol_packets
[PACKET_Z1
].title
);
4648 addr
= remote_address_masked (bp_tgt
->placed_address
);
4649 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4650 sprintf (p
, ",%x", bp_tgt
->placed_size
);
4653 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4655 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
4658 case PACKET_UNKNOWN
:
4663 internal_error (__FILE__
, __LINE__
,
4664 _("remote_remove_hw_breakpoint: reached end of function"));
4667 /* Some targets are only capable of doing downloads, and afterwards
4668 they switch to the remote serial protocol. This function provides
4669 a clean way to get from the download target to the remote target.
4670 It's basically just a wrapper so that we don't have to expose any
4671 of the internal workings of remote.c.
4673 Prior to calling this routine, you should shutdown the current
4674 target code, else you will get the "A program is being debugged
4675 already..." message. Usually a call to pop_target() suffices. */
4678 push_remote_target (char *name
, int from_tty
)
4680 printf_filtered (_("Switching to remote protocol\n"));
4681 remote_open (name
, from_tty
);
4684 /* Table used by the crc32 function to calcuate the checksum. */
4686 static unsigned long crc32_table
[256] =
4689 static unsigned long
4690 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
4692 if (!crc32_table
[1])
4694 /* Initialize the CRC table and the decoding table. */
4698 for (i
= 0; i
< 256; i
++)
4700 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
4701 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
4708 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
4714 /* compare-sections command
4716 With no arguments, compares each loadable section in the exec bfd
4717 with the same memory range on the target, and reports mismatches.
4718 Useful for verifying the image on the target against the exec file.
4719 Depends on the target understanding the new "qCRC:" request. */
4721 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4722 target method (target verify memory) and generic version of the
4723 actual command. This will allow other high-level code (especially
4724 generic_load()) to make use of this target functionality. */
4727 compare_sections_command (char *args
, int from_tty
)
4729 struct remote_state
*rs
= get_remote_state ();
4731 unsigned long host_crc
, target_crc
;
4732 extern bfd
*exec_bfd
;
4733 struct cleanup
*old_chain
;
4736 const char *sectname
;
4743 error (_("command cannot be used without an exec file"));
4744 if (!current_target
.to_shortname
||
4745 strcmp (current_target
.to_shortname
, "remote") != 0)
4746 error (_("command can only be used with remote target"));
4748 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
4750 if (!(s
->flags
& SEC_LOAD
))
4751 continue; /* skip non-loadable section */
4753 size
= bfd_get_section_size (s
);
4755 continue; /* skip zero-length section */
4757 sectname
= bfd_get_section_name (exec_bfd
, s
);
4758 if (args
&& strcmp (args
, sectname
) != 0)
4759 continue; /* not the section selected by user */
4761 matched
= 1; /* do this section */
4763 /* FIXME: assumes lma can fit into long. */
4764 xsnprintf (rs
->buf
, rs
->remote_packet_size
, "qCRC:%lx,%lx",
4765 (long) lma
, (long) size
);
4768 /* Be clever; compute the host_crc before waiting for target
4770 sectdata
= xmalloc (size
);
4771 old_chain
= make_cleanup (xfree
, sectdata
);
4772 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
4773 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
4775 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4776 if (rs
->buf
[0] == 'E')
4777 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
4778 sectname
, paddr (lma
), paddr (lma
+ size
));
4779 if (rs
->buf
[0] != 'C')
4780 error (_("remote target does not support this operation"));
4782 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
4783 target_crc
= target_crc
* 16 + fromhex (*tmp
);
4785 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4786 sectname
, paddr (lma
), paddr (lma
+ size
));
4787 if (host_crc
== target_crc
)
4788 printf_filtered ("matched.\n");
4791 printf_filtered ("MIS-MATCHED!\n");
4795 do_cleanups (old_chain
);
4798 warning (_("One or more sections of the remote executable does not match\n\
4799 the loaded file\n"));
4800 if (args
&& !matched
)
4801 printf_filtered (_("No loaded section named '%s'.\n"), args
);
4805 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
4806 const char *annex
, gdb_byte
*readbuf
,
4807 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
4809 struct remote_state
*rs
= get_remote_state ();
4814 /* Handle memory using remote_xfer_memory. */
4815 if (object
== TARGET_OBJECT_MEMORY
)
4820 if (writebuf
!= NULL
)
4822 void *buffer
= xmalloc (len
);
4823 struct cleanup
*cleanup
= make_cleanup (xfree
, buffer
);
4824 memcpy (buffer
, writebuf
, len
);
4825 xfered
= remote_xfer_memory (offset
, buffer
, len
, 1, NULL
, ops
);
4826 do_cleanups (cleanup
);
4829 xfered
= remote_xfer_memory (offset
, readbuf
, len
, 0, NULL
, ops
);
4833 else if (xfered
== 0 && errno
== 0)
4839 /* Only handle reads. */
4840 if (writebuf
!= NULL
|| readbuf
== NULL
)
4843 /* Map pre-existing objects onto letters. DO NOT do this for new
4844 objects!!! Instead specify new query packets. */
4847 case TARGET_OBJECT_KOD
:
4850 case TARGET_OBJECT_AVR
:
4854 case TARGET_OBJECT_AUXV
:
4855 if (remote_protocol_packets
[PACKET_qPart_auxv
].support
!= PACKET_DISABLE
)
4857 unsigned int total
= 0;
4860 LONGEST n
= min ((rs
->remote_packet_size
- 2) / 2, len
);
4861 snprintf (rs
->buf
, rs
->remote_packet_size
,
4862 "qPart:auxv:read::%s,%s",
4863 phex_nz (offset
, sizeof offset
),
4864 phex_nz (n
, sizeof n
));
4865 i
= putpkt (rs
->buf
);
4867 return total
> 0 ? total
: i
;
4869 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4870 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qPart_auxv
])
4872 return total
> 0 ? total
: -1;
4873 if (strcmp (rs
->buf
, "OK") == 0)
4874 break; /* Got EOF indicator. */
4875 /* Got some data. */
4876 i
= hex2bin (rs
->buf
, readbuf
, len
);
4879 readbuf
= (void *) ((char *) readbuf
+ i
);
4893 /* Note: a zero OFFSET and LEN can be used to query the minimum
4895 if (offset
== 0 && len
== 0)
4896 return (rs
->remote_packet_size
);
4897 /* Minimum outbuf size is RS->remote_packet_size. If LEN is not
4898 large enough let the caller deal with it. */
4899 if (len
< rs
->remote_packet_size
)
4901 len
= rs
->remote_packet_size
;
4903 /* Except for querying the minimum buffer size, target must be open. */
4905 error (_("remote query is only available after target open"));
4907 gdb_assert (annex
!= NULL
);
4908 gdb_assert (readbuf
!= NULL
);
4914 /* We used one buffer char for the remote protocol q command and
4915 another for the query type. As the remote protocol encapsulation
4916 uses 4 chars plus one extra in case we are debugging
4917 (remote_debug), we have PBUFZIZ - 7 left to pack the query
4920 while (annex
[i
] && (i
< (rs
->remote_packet_size
- 8)))
4922 /* Bad caller may have sent forbidden characters. */
4923 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
4928 gdb_assert (annex
[i
] == '\0');
4930 i
= putpkt (rs
->buf
);
4934 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4935 strcpy ((char *) readbuf
, rs
->buf
);
4937 return strlen ((char *) readbuf
);
4941 remote_rcmd (char *command
,
4942 struct ui_file
*outbuf
)
4944 struct remote_state
*rs
= get_remote_state ();
4945 char *buf
= rs
->buf
;
4949 error (_("remote rcmd is only available after target open"));
4951 /* Send a NULL command across as an empty command. */
4952 if (command
== NULL
)
4955 /* The query prefix. */
4956 strcpy (buf
, "qRcmd,");
4957 p
= strchr (buf
, '\0');
4959 if ((strlen (buf
) + strlen (command
) * 2 + 8/*misc*/) > rs
->remote_packet_size
)
4960 error (_("\"monitor\" command ``%s'' is too long."), command
);
4962 /* Encode the actual command. */
4963 bin2hex ((gdb_byte
*) command
, p
, 0);
4965 if (putpkt (rs
->buf
) < 0)
4966 error (_("Communication problem with target."));
4968 /* get/display the response */
4971 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
4973 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4975 error (_("Target does not support this command."));
4976 if (buf
[0] == 'O' && buf
[1] != 'K')
4978 remote_console_output (buf
+ 1); /* 'O' message from stub. */
4981 if (strcmp (buf
, "OK") == 0)
4983 if (strlen (buf
) == 3 && buf
[0] == 'E'
4984 && isdigit (buf
[1]) && isdigit (buf
[2]))
4986 error (_("Protocol error with Rcmd"));
4988 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
4990 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
4991 fputc_unfiltered (c
, outbuf
);
4998 packet_command (char *args
, int from_tty
)
5000 struct remote_state
*rs
= get_remote_state ();
5003 error (_("command can only be used with remote target"));
5006 error (_("remote-packet command requires packet text as argument"));
5008 puts_filtered ("sending: ");
5009 print_packet (args
);
5010 puts_filtered ("\n");
5013 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5014 puts_filtered ("received: ");
5015 print_packet (rs
->buf
);
5016 puts_filtered ("\n");
5020 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
5022 static void display_thread_info (struct gdb_ext_thread_info
*info
);
5024 static void threadset_test_cmd (char *cmd
, int tty
);
5026 static void threadalive_test (char *cmd
, int tty
);
5028 static void threadlist_test_cmd (char *cmd
, int tty
);
5030 int get_and_display_threadinfo (threadref
*ref
);
5032 static void threadinfo_test_cmd (char *cmd
, int tty
);
5034 static int thread_display_step (threadref
*ref
, void *context
);
5036 static void threadlist_update_test_cmd (char *cmd
, int tty
);
5038 static void init_remote_threadtests (void);
5040 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
5043 threadset_test_cmd (char *cmd
, int tty
)
5045 int sample_thread
= SAMPLE_THREAD
;
5047 printf_filtered (_("Remote threadset test\n"));
5048 set_thread (sample_thread
, 1);
5053 threadalive_test (char *cmd
, int tty
)
5055 int sample_thread
= SAMPLE_THREAD
;
5057 if (remote_thread_alive (pid_to_ptid (sample_thread
)))
5058 printf_filtered ("PASS: Thread alive test\n");
5060 printf_filtered ("FAIL: Thread alive test\n");
5063 void output_threadid (char *title
, threadref
*ref
);
5066 output_threadid (char *title
, threadref
*ref
)
5070 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
5072 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
5076 threadlist_test_cmd (char *cmd
, int tty
)
5079 threadref nextthread
;
5080 int done
, result_count
;
5081 threadref threadlist
[3];
5083 printf_filtered ("Remote Threadlist test\n");
5084 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
5085 &result_count
, &threadlist
[0]))
5086 printf_filtered ("FAIL: threadlist test\n");
5089 threadref
*scan
= threadlist
;
5090 threadref
*limit
= scan
+ result_count
;
5092 while (scan
< limit
)
5093 output_threadid (" thread ", scan
++);
5098 display_thread_info (struct gdb_ext_thread_info
*info
)
5100 output_threadid ("Threadid: ", &info
->threadid
);
5101 printf_filtered ("Name: %s\n ", info
->shortname
);
5102 printf_filtered ("State: %s\n", info
->display
);
5103 printf_filtered ("other: %s\n\n", info
->more_display
);
5107 get_and_display_threadinfo (threadref
*ref
)
5111 struct gdb_ext_thread_info threadinfo
;
5113 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
5114 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
5115 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
5116 display_thread_info (&threadinfo
);
5121 threadinfo_test_cmd (char *cmd
, int tty
)
5123 int athread
= SAMPLE_THREAD
;
5127 int_to_threadref (&thread
, athread
);
5128 printf_filtered ("Remote Threadinfo test\n");
5129 if (!get_and_display_threadinfo (&thread
))
5130 printf_filtered ("FAIL cannot get thread info\n");
5134 thread_display_step (threadref
*ref
, void *context
)
5136 /* output_threadid(" threadstep ",ref); *//* simple test */
5137 return get_and_display_threadinfo (ref
);
5141 threadlist_update_test_cmd (char *cmd
, int tty
)
5143 printf_filtered ("Remote Threadlist update test\n");
5144 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
5148 init_remote_threadtests (void)
5150 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
5151 Fetch and print the remote list of thread identifiers, one pkt only"));
5152 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
5153 _("Fetch and display info about one thread"));
5154 add_com ("tset", class_obscure
, threadset_test_cmd
,
5155 _("Test setting to a different thread"));
5156 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
5157 _("Iterate through updating all remote thread info"));
5158 add_com ("talive", class_obscure
, threadalive_test
,
5159 _(" Remote thread alive test "));
5164 /* Convert a thread ID to a string. Returns the string in a static
5168 remote_pid_to_str (ptid_t ptid
)
5170 static char buf
[32];
5172 xsnprintf (buf
, sizeof buf
, "thread %d", ptid_get_pid (ptid
));
5176 /* Get the address of the thread local variable in OBJFILE which is
5177 stored at OFFSET within the thread local storage for thread PTID. */
5180 remote_get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
5182 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
5184 struct remote_state
*rs
= get_remote_state ();
5186 enum packet_result result
;
5188 strcpy (p
, "qGetTLSAddr:");
5190 p
+= hexnumstr (p
, PIDGET (ptid
));
5192 p
+= hexnumstr (p
, offset
);
5194 p
+= hexnumstr (p
, lm
);
5198 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5199 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
5200 if (result
== PACKET_OK
)
5204 unpack_varlen_hex (rs
->buf
, &result
);
5207 else if (result
== PACKET_UNKNOWN
)
5208 throw_error (TLS_GENERIC_ERROR
,
5209 _("Remote target doesn't support qGetTLSAddr packet"));
5211 throw_error (TLS_GENERIC_ERROR
,
5212 _("Remote target failed to process qGetTLSAddr request"));
5215 throw_error (TLS_GENERIC_ERROR
,
5216 _("TLS not supported or disabled on this target"));
5222 init_remote_ops (void)
5224 remote_ops
.to_shortname
= "remote";
5225 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
5227 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5228 Specify the serial device it is connected to\n\
5229 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
5230 remote_ops
.to_open
= remote_open
;
5231 remote_ops
.to_close
= remote_close
;
5232 remote_ops
.to_detach
= remote_detach
;
5233 remote_ops
.to_disconnect
= remote_disconnect
;
5234 remote_ops
.to_resume
= remote_resume
;
5235 remote_ops
.to_wait
= remote_wait
;
5236 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
5237 remote_ops
.to_store_registers
= remote_store_registers
;
5238 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5239 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
5240 remote_ops
.to_files_info
= remote_files_info
;
5241 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5242 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5243 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
5244 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
5245 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
5246 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
5247 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
5248 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
5249 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
5250 remote_ops
.to_kill
= remote_kill
;
5251 remote_ops
.to_load
= generic_load
;
5252 remote_ops
.to_mourn_inferior
= remote_mourn
;
5253 remote_ops
.to_thread_alive
= remote_thread_alive
;
5254 remote_ops
.to_find_new_threads
= remote_threads_info
;
5255 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
5256 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
5257 remote_ops
.to_stop
= remote_stop
;
5258 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
5259 remote_ops
.to_rcmd
= remote_rcmd
;
5260 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
5261 remote_ops
.to_stratum
= process_stratum
;
5262 remote_ops
.to_has_all_memory
= 1;
5263 remote_ops
.to_has_memory
= 1;
5264 remote_ops
.to_has_stack
= 1;
5265 remote_ops
.to_has_registers
= 1;
5266 remote_ops
.to_has_execution
= 1;
5267 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5268 remote_ops
.to_magic
= OPS_MAGIC
;
5271 /* Set up the extended remote vector by making a copy of the standard
5272 remote vector and adding to it. */
5275 init_extended_remote_ops (void)
5277 extended_remote_ops
= remote_ops
;
5279 extended_remote_ops
.to_shortname
= "extended-remote";
5280 extended_remote_ops
.to_longname
=
5281 "Extended remote serial target in gdb-specific protocol";
5282 extended_remote_ops
.to_doc
=
5283 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5284 Specify the serial device it is connected to (e.g. /dev/ttya).",
5285 extended_remote_ops
.to_open
= extended_remote_open
;
5286 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
5287 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5291 remote_can_async_p (void)
5293 /* We're async whenever the serial device is. */
5294 return (current_target
.to_async_mask_value
) && serial_can_async_p (remote_desc
);
5298 remote_is_async_p (void)
5300 /* We're async whenever the serial device is. */
5301 return (current_target
.to_async_mask_value
) && serial_is_async_p (remote_desc
);
5304 /* Pass the SERIAL event on and up to the client. One day this code
5305 will be able to delay notifying the client of an event until the
5306 point where an entire packet has been received. */
5308 static void (*async_client_callback
) (enum inferior_event_type event_type
,
5310 static void *async_client_context
;
5311 static serial_event_ftype remote_async_serial_handler
;
5314 remote_async_serial_handler (struct serial
*scb
, void *context
)
5316 /* Don't propogate error information up to the client. Instead let
5317 the client find out about the error by querying the target. */
5318 async_client_callback (INF_REG_EVENT
, async_client_context
);
5322 remote_async (void (*callback
) (enum inferior_event_type event_type
,
5323 void *context
), void *context
)
5325 if (current_target
.to_async_mask_value
== 0)
5326 internal_error (__FILE__
, __LINE__
,
5327 _("Calling remote_async when async is masked"));
5329 if (callback
!= NULL
)
5331 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
5332 async_client_callback
= callback
;
5333 async_client_context
= context
;
5336 serial_async (remote_desc
, NULL
, NULL
);
5339 /* Target async and target extended-async.
5341 This are temporary targets, until it is all tested. Eventually
5342 async support will be incorporated int the usual 'remote'
5346 init_remote_async_ops (void)
5348 remote_async_ops
.to_shortname
= "async";
5349 remote_async_ops
.to_longname
=
5350 "Remote serial target in async version of the gdb-specific protocol";
5351 remote_async_ops
.to_doc
=
5352 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5353 Specify the serial device it is connected to (e.g. /dev/ttya).";
5354 remote_async_ops
.to_open
= remote_async_open
;
5355 remote_async_ops
.to_close
= remote_close
;
5356 remote_async_ops
.to_detach
= remote_detach
;
5357 remote_async_ops
.to_disconnect
= remote_disconnect
;
5358 remote_async_ops
.to_resume
= remote_async_resume
;
5359 remote_async_ops
.to_wait
= remote_async_wait
;
5360 remote_async_ops
.to_fetch_registers
= remote_fetch_registers
;
5361 remote_async_ops
.to_store_registers
= remote_store_registers
;
5362 remote_async_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5363 remote_async_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
5364 remote_async_ops
.to_files_info
= remote_files_info
;
5365 remote_async_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5366 remote_async_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5367 remote_async_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
5368 remote_async_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
5369 remote_async_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
5370 remote_async_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
5371 remote_async_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
5372 remote_async_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
5373 remote_async_ops
.to_stopped_data_address
= remote_stopped_data_address
;
5374 remote_async_ops
.to_terminal_inferior
= remote_async_terminal_inferior
;
5375 remote_async_ops
.to_terminal_ours
= remote_async_terminal_ours
;
5376 remote_async_ops
.to_kill
= remote_async_kill
;
5377 remote_async_ops
.to_load
= generic_load
;
5378 remote_async_ops
.to_mourn_inferior
= remote_async_mourn
;
5379 remote_async_ops
.to_thread_alive
= remote_thread_alive
;
5380 remote_async_ops
.to_find_new_threads
= remote_threads_info
;
5381 remote_async_ops
.to_pid_to_str
= remote_pid_to_str
;
5382 remote_async_ops
.to_extra_thread_info
= remote_threads_extra_info
;
5383 remote_async_ops
.to_stop
= remote_stop
;
5384 remote_async_ops
.to_xfer_partial
= remote_xfer_partial
;
5385 remote_async_ops
.to_rcmd
= remote_rcmd
;
5386 remote_async_ops
.to_stratum
= process_stratum
;
5387 remote_async_ops
.to_has_all_memory
= 1;
5388 remote_async_ops
.to_has_memory
= 1;
5389 remote_async_ops
.to_has_stack
= 1;
5390 remote_async_ops
.to_has_registers
= 1;
5391 remote_async_ops
.to_has_execution
= 1;
5392 remote_async_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5393 remote_async_ops
.to_can_async_p
= remote_can_async_p
;
5394 remote_async_ops
.to_is_async_p
= remote_is_async_p
;
5395 remote_async_ops
.to_async
= remote_async
;
5396 remote_async_ops
.to_async_mask_value
= 1;
5397 remote_async_ops
.to_magic
= OPS_MAGIC
;
5400 /* Set up the async extended remote vector by making a copy of the standard
5401 remote vector and adding to it. */
5404 init_extended_async_remote_ops (void)
5406 extended_async_remote_ops
= remote_async_ops
;
5408 extended_async_remote_ops
.to_shortname
= "extended-async";
5409 extended_async_remote_ops
.to_longname
=
5410 "Extended remote serial target in async gdb-specific protocol";
5411 extended_async_remote_ops
.to_doc
=
5412 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5413 Specify the serial device it is connected to (e.g. /dev/ttya).",
5414 extended_async_remote_ops
.to_open
= extended_remote_async_open
;
5415 extended_async_remote_ops
.to_create_inferior
= extended_remote_async_create_inferior
;
5416 extended_async_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5419 static struct cmd_list_element
*remote_set_cmdlist
;
5420 static struct cmd_list_element
*remote_show_cmdlist
;
5423 set_remote_cmd (char *args
, int from_tty
)
5428 show_remote_cmd (char *args
, int from_tty
)
5430 /* We can't just use cmd_show_list here, because we want to skip
5431 the redundant "show remote Z-packet". */
5432 struct cleanup
*showlist_chain
;
5433 struct cmd_list_element
*list
= remote_show_cmdlist
;
5435 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
5436 for (; list
!= NULL
; list
= list
->next
)
5437 if (strcmp (list
->name
, "Z-packet") == 0)
5439 else if (list
->type
== show_cmd
)
5441 struct cleanup
*option_chain
5442 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
5443 ui_out_field_string (uiout
, "name", list
->name
);
5444 ui_out_text (uiout
, ": ");
5445 do_setshow_command ((char *) NULL
, from_tty
, list
);
5446 /* Close the tuple. */
5447 do_cleanups (option_chain
);
5452 build_remote_gdbarch_data (void)
5454 remote_address_size
= TARGET_ADDR_BIT
;
5457 /* Saved pointer to previous owner of the new_objfile event. */
5458 static void (*remote_new_objfile_chain
) (struct objfile
*);
5460 /* Function to be called whenever a new objfile (shlib) is detected. */
5462 remote_new_objfile (struct objfile
*objfile
)
5464 if (remote_desc
!= 0) /* Have a remote connection. */
5466 remote_check_symbols (objfile
);
5468 /* Call predecessor on chain, if any. */
5469 if (remote_new_objfile_chain
!= 0 &&
5471 remote_new_objfile_chain (objfile
);
5475 _initialize_remote (void)
5477 /* architecture specific data */
5478 remote_gdbarch_data_handle
=
5479 gdbarch_data_register_post_init (init_remote_state
);
5481 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5482 that the remote protocol has been initialized. */
5483 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size
);
5484 deprecated_register_gdbarch_swap (NULL
, 0, build_remote_gdbarch_data
);
5487 add_target (&remote_ops
);
5489 init_extended_remote_ops ();
5490 add_target (&extended_remote_ops
);
5492 init_remote_async_ops ();
5493 add_target (&remote_async_ops
);
5495 init_extended_async_remote_ops ();
5496 add_target (&extended_async_remote_ops
);
5498 /* Hook into new objfile notification. */
5499 remote_new_objfile_chain
= deprecated_target_new_objfile_hook
;
5500 deprecated_target_new_objfile_hook
= remote_new_objfile
;
5503 init_remote_threadtests ();
5506 /* set/show remote ... */
5508 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
5509 Remote protocol specific variables\n\
5510 Configure various remote-protocol specific variables such as\n\
5511 the packets being used"),
5512 &remote_set_cmdlist
, "set remote ",
5513 0 /* allow-unknown */, &setlist
);
5514 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
5515 Remote protocol specific variables\n\
5516 Configure various remote-protocol specific variables such as\n\
5517 the packets being used"),
5518 &remote_show_cmdlist
, "show remote ",
5519 0 /* allow-unknown */, &showlist
);
5521 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
5522 Compare section data on target to the exec file.\n\
5523 Argument is a single section name (default: all loaded sections)."),
5526 add_cmd ("packet", class_maintenance
, packet_command
, _("\
5527 Send an arbitrary packet to a remote target.\n\
5528 maintenance packet TEXT\n\
5529 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5530 this command sends the string TEXT to the inferior, and displays the\n\
5531 response packet. GDB supplies the initial `$' character, and the\n\
5532 terminating `#' character and checksum."),
5535 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
5536 Set whether to send break if interrupted."), _("\
5537 Show whether to send break if interrupted."), _("\
5538 If set, a break, instead of a cntrl-c, is sent to the remote target."),
5539 NULL
, NULL
, /* FIXME: i18n: Whether to send break if interrupted is %s. */
5540 &setlist
, &showlist
);
5542 /* Install commands for configuring memory read/write packets. */
5544 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
5545 Set the maximum number of bytes per memory write packet (deprecated)."),
5547 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
5548 Show the maximum number of bytes per memory write packet (deprecated)."),
5550 add_cmd ("memory-write-packet-size", no_class
,
5551 set_memory_write_packet_size
, _("\
5552 Set the maximum number of bytes per memory-write packet.\n\
5553 Specify the number of bytes in a packet or 0 (zero) for the\n\
5554 default packet size. The actual limit is further reduced\n\
5555 dependent on the target. Specify ``fixed'' to disable the\n\
5556 further restriction and ``limit'' to enable that restriction."),
5557 &remote_set_cmdlist
);
5558 add_cmd ("memory-read-packet-size", no_class
,
5559 set_memory_read_packet_size
, _("\
5560 Set the maximum number of bytes per memory-read packet.\n\
5561 Specify the number of bytes in a packet or 0 (zero) for the\n\
5562 default packet size. The actual limit is further reduced\n\
5563 dependent on the target. Specify ``fixed'' to disable the\n\
5564 further restriction and ``limit'' to enable that restriction."),
5565 &remote_set_cmdlist
);
5566 add_cmd ("memory-write-packet-size", no_class
,
5567 show_memory_write_packet_size
,
5568 _("Show the maximum number of bytes per memory-write packet."),
5569 &remote_show_cmdlist
);
5570 add_cmd ("memory-read-packet-size", no_class
,
5571 show_memory_read_packet_size
,
5572 _("Show the maximum number of bytes per memory-read packet."),
5573 &remote_show_cmdlist
);
5575 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
5576 &remote_hw_watchpoint_limit
, _("\
5577 Set the maximum number of target hardware watchpoints."), _("\
5578 Show the maximum number of target hardware watchpoints."), _("\
5579 Specify a negative limit for unlimited."),
5580 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
5581 &remote_set_cmdlist
, &remote_show_cmdlist
);
5582 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
5583 &remote_hw_breakpoint_limit
, _("\
5584 Set the maximum number of target hardware breakpoints."), _("\
5585 Show the maximum number of target hardware breakpoints."), _("\
5586 Specify a negative limit for unlimited."),
5587 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
5588 &remote_set_cmdlist
, &remote_show_cmdlist
);
5590 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
5591 &remote_address_size
, _("\
5592 Set the maximum size of the address (in bits) in a memory packet."), _("\
5593 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
5595 NULL
, /* FIXME: i18n: */
5596 &setlist
, &showlist
);
5598 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
5599 "X", "binary-download",
5600 set_remote_protocol_packet_cmd
,
5601 show_remote_protocol_packet_cmd
,
5602 &remote_set_cmdlist
, &remote_show_cmdlist
,
5605 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
5606 "vCont", "verbose-resume",
5607 set_remote_protocol_packet_cmd
,
5608 show_remote_protocol_packet_cmd
,
5609 &remote_set_cmdlist
, &remote_show_cmdlist
,
5612 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
5613 "qSymbol", "symbol-lookup",
5614 set_remote_protocol_packet_cmd
,
5615 show_remote_protocol_packet_cmd
,
5616 &remote_set_cmdlist
, &remote_show_cmdlist
,
5619 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
5620 "P", "set-register",
5621 set_remote_protocol_packet_cmd
,
5622 show_remote_protocol_packet_cmd
,
5623 &remote_set_cmdlist
, &remote_show_cmdlist
,
5626 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
5627 "p", "fetch-register",
5628 set_remote_protocol_packet_cmd
,
5629 show_remote_protocol_packet_cmd
,
5630 &remote_set_cmdlist
, &remote_show_cmdlist
,
5633 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
5634 "Z0", "software-breakpoint",
5635 set_remote_protocol_packet_cmd
,
5636 show_remote_protocol_packet_cmd
,
5637 &remote_set_cmdlist
, &remote_show_cmdlist
,
5640 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
5641 "Z1", "hardware-breakpoint",
5642 set_remote_protocol_packet_cmd
,
5643 show_remote_protocol_packet_cmd
,
5644 &remote_set_cmdlist
, &remote_show_cmdlist
,
5647 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
5648 "Z2", "write-watchpoint",
5649 set_remote_protocol_packet_cmd
,
5650 show_remote_protocol_packet_cmd
,
5651 &remote_set_cmdlist
, &remote_show_cmdlist
,
5654 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
5655 "Z3", "read-watchpoint",
5656 set_remote_protocol_packet_cmd
,
5657 show_remote_protocol_packet_cmd
,
5658 &remote_set_cmdlist
, &remote_show_cmdlist
,
5661 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
5662 "Z4", "access-watchpoint",
5663 set_remote_protocol_packet_cmd
,
5664 show_remote_protocol_packet_cmd
,
5665 &remote_set_cmdlist
, &remote_show_cmdlist
,
5668 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qPart_auxv
],
5669 "qPart_auxv", "read-aux-vector",
5670 set_remote_protocol_packet_cmd
,
5671 show_remote_protocol_packet_cmd
,
5672 &remote_set_cmdlist
, &remote_show_cmdlist
,
5675 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
5676 "qGetTLSAddr", "get-thread-local-storage-address",
5677 set_remote_protocol_packet_cmd
,
5678 show_remote_protocol_packet_cmd
,
5679 &remote_set_cmdlist
, &remote_show_cmdlist
,
5682 /* Keep the old ``set remote Z-packet ...'' working. Each individual
5683 Z sub-packet has its own set and show commands, but users may
5684 have sets to this variable in their .gdbinit files (or in their
5686 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
5687 &remote_Z_packet_detect
, _("\
5688 Set use of remote protocol `Z' packets"), _("\
5689 Show use of remote protocol `Z' packets "), _("\
5690 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
5692 set_remote_protocol_Z_packet_cmd
,
5693 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
5694 &remote_set_cmdlist
, &remote_show_cmdlist
);
5696 /* Eventually initialize fileio. See fileio.c */
5697 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);