1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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"
48 #include "target-descriptions.h"
53 #include "event-loop.h"
54 #include "event-top.h"
60 #include "gdbcore.h" /* for exec_bfd */
62 #include "remote-fileio.h"
64 #include "memory-map.h"
66 /* The size to align memory write packets, when practical. The protocol
67 does not guarantee any alignment, and gdb will generate short
68 writes and unaligned writes, but even as a best-effort attempt this
69 can improve bulk transfers. For instance, if a write is misaligned
70 relative to the target's data bus, the stub may need to make an extra
71 round trip fetching data from the target. This doesn't make a
72 huge difference, but it's easy to do, so we try to be helpful.
74 The alignment chosen is arbitrary; usually data bus width is
75 important here, not the possibly larger cache line size. */
76 enum { REMOTE_ALIGN_WRITES
= 16 };
78 /* Prototypes for local functions. */
79 static void cleanup_sigint_signal_handler (void *dummy
);
80 static void initialize_sigint_signal_handler (void);
81 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
83 static void handle_remote_sigint (int);
84 static void handle_remote_sigint_twice (int);
85 static void async_remote_interrupt (gdb_client_data
);
86 void async_remote_interrupt_twice (gdb_client_data
);
88 static void build_remote_gdbarch_data (void);
90 static void remote_files_info (struct target_ops
*ignore
);
92 static void remote_prepare_to_store (void);
94 static void remote_fetch_registers (int regno
);
96 static void remote_resume (ptid_t ptid
, int step
,
97 enum target_signal siggnal
);
98 static void remote_async_resume (ptid_t ptid
, int step
,
99 enum target_signal siggnal
);
100 static void remote_open (char *name
, int from_tty
);
101 static void remote_async_open (char *name
, int from_tty
);
103 static void extended_remote_open (char *name
, int from_tty
);
104 static void extended_remote_async_open (char *name
, int from_tty
);
106 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
,
109 static void remote_close (int quitting
);
111 static void remote_store_registers (int regno
);
113 static void remote_mourn (void);
114 static void remote_async_mourn (void);
116 static void extended_remote_restart (void);
118 static void extended_remote_mourn (void);
120 static void remote_mourn_1 (struct target_ops
*);
122 static void remote_send (char **buf
, long *sizeof_buf_p
);
124 static int readchar (int timeout
);
126 static ptid_t
remote_wait (ptid_t ptid
,
127 struct target_waitstatus
*status
);
128 static ptid_t
remote_async_wait (ptid_t ptid
,
129 struct target_waitstatus
*status
);
131 static void remote_kill (void);
132 static void remote_async_kill (void);
134 static int tohex (int nib
);
136 static void remote_detach (char *args
, int from_tty
);
138 static void remote_interrupt (int signo
);
140 static void remote_interrupt_twice (int signo
);
142 static void interrupt_query (void);
144 static void set_thread (int, int);
146 static int remote_thread_alive (ptid_t
);
148 static void get_offsets (void);
150 static void skip_frame (void);
152 static long read_frame (char **buf_p
, long *sizeof_buf
);
154 static int hexnumlen (ULONGEST num
);
156 static void init_remote_ops (void);
158 static void init_extended_remote_ops (void);
160 static void remote_stop (void);
162 static int ishex (int ch
, int *val
);
164 static int stubhex (int ch
);
166 static int hexnumstr (char *, ULONGEST
);
168 static int hexnumnstr (char *, ULONGEST
, int);
170 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
172 static void print_packet (char *);
174 static unsigned long crc32 (unsigned char *, int, unsigned int);
176 static void compare_sections_command (char *, int);
178 static void packet_command (char *, int);
180 static int stub_unpack_int (char *buff
, int fieldlength
);
182 static ptid_t
remote_current_thread (ptid_t oldptid
);
184 static void remote_find_new_threads (void);
186 static void record_currthread (int currthread
);
188 static int fromhex (int a
);
190 static int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
192 static int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
194 static int putpkt_binary (char *buf
, int cnt
);
196 static void check_binary_download (CORE_ADDR addr
);
198 struct packet_config
;
200 static void show_packet_config_cmd (struct packet_config
*config
);
202 static void update_packet_config (struct packet_config
*config
);
204 static void set_remote_protocol_packet_cmd (char *args
, int from_tty
,
205 struct cmd_list_element
*c
);
207 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
209 struct cmd_list_element
*c
,
212 void _initialize_remote (void);
214 /* For "set remote" and "show remote". */
216 static struct cmd_list_element
*remote_set_cmdlist
;
217 static struct cmd_list_element
*remote_show_cmdlist
;
219 /* Description of the remote protocol state for the currently
220 connected target. This is per-target state, and independent of the
221 selected architecture. */
225 /* A buffer to use for incoming packets, and its current size. The
226 buffer is grown dynamically for larger incoming packets.
227 Outgoing packets may also be constructed in this buffer.
228 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
229 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
234 /* If we negotiated packet size explicitly (and thus can bypass
235 heuristics for the largest packet size that will not overflow
236 a buffer in the stub), this will be set to that packet size.
237 Otherwise zero, meaning to use the guessed size. */
238 long explicit_packet_size
;
241 /* This data could be associated with a target, but we do not always
242 have access to the current target when we need it, so for now it is
243 static. This will be fine for as long as only one target is in use
245 static struct remote_state remote_state
;
247 static struct remote_state
*
248 get_remote_state_raw (void)
250 return &remote_state
;
253 /* Description of the remote protocol for a given architecture. */
257 long offset
; /* Offset into G packet. */
258 long regnum
; /* GDB's internal register number. */
259 LONGEST pnum
; /* Remote protocol register number. */
260 int in_g_packet
; /* Always part of G packet. */
261 /* long size in bytes; == register_size (current_gdbarch, regnum);
263 /* char *name; == REGISTER_NAME (regnum); at present. */
266 struct remote_arch_state
268 /* Description of the remote protocol registers. */
269 long sizeof_g_packet
;
271 /* Description of the remote protocol registers indexed by REGNUM
272 (making an array NUM_REGS in size). */
273 struct packet_reg
*regs
;
275 /* This is the size (in chars) of the first response to the ``g''
276 packet. It is used as a heuristic when determining the maximum
277 size of memory-read and memory-write packets. A target will
278 typically only reserve a buffer large enough to hold the ``g''
279 packet. The size does not include packet overhead (headers and
281 long actual_register_packet_size
;
283 /* This is the maximum size (in chars) of a non read/write packet.
284 It is also used as a cap on the size of read/write packets. */
285 long remote_packet_size
;
289 /* Handle for retreving the remote protocol data from gdbarch. */
290 static struct gdbarch_data
*remote_gdbarch_data_handle
;
292 static struct remote_arch_state
*
293 get_remote_arch_state (void)
295 return gdbarch_data (current_gdbarch
, remote_gdbarch_data_handle
);
298 /* Fetch the global remote target state. */
300 static struct remote_state
*
301 get_remote_state (void)
303 /* Make sure that the remote architecture state has been
304 initialized, because doing so might reallocate rs->buf. Any
305 function which calls getpkt also needs to be mindful of changes
306 to rs->buf, but this call limits the number of places which run
308 get_remote_arch_state ();
310 return get_remote_state_raw ();
314 compare_pnums (const void *lhs_
, const void *rhs_
)
316 const struct packet_reg
* const *lhs
= lhs_
;
317 const struct packet_reg
* const *rhs
= rhs_
;
319 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
321 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
328 init_remote_state (struct gdbarch
*gdbarch
)
330 int regnum
, num_remote_regs
, offset
;
331 struct remote_state
*rs
= get_remote_state_raw ();
332 struct remote_arch_state
*rsa
;
333 struct packet_reg
**remote_regs
;
335 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
337 /* Use the architecture to build a regnum<->pnum table, which will be
338 1:1 unless a feature set specifies otherwise. */
339 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
, NUM_REGS
, struct packet_reg
);
340 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
342 struct packet_reg
*r
= &rsa
->regs
[regnum
];
344 if (register_size (current_gdbarch
, regnum
) == 0)
345 /* Do not try to fetch zero-sized (placeholder) registers. */
348 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
353 /* Define the g/G packet format as the contents of each register
354 with a remote protocol number, in order of ascending protocol
357 remote_regs
= alloca (NUM_REGS
* sizeof (struct packet_reg
*));
358 for (num_remote_regs
= 0, regnum
= 0; regnum
< NUM_REGS
; regnum
++)
359 if (rsa
->regs
[regnum
].pnum
!= -1)
360 remote_regs
[num_remote_regs
++] = &rsa
->regs
[regnum
];
362 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
365 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
367 remote_regs
[regnum
]->in_g_packet
= 1;
368 remote_regs
[regnum
]->offset
= offset
;
369 offset
+= register_size (current_gdbarch
, remote_regs
[regnum
]->regnum
);
372 /* Record the maximum possible size of the g packet - it may turn out
374 rsa
->sizeof_g_packet
= offset
;
376 /* Default maximum number of characters in a packet body. Many
377 remote stubs have a hardwired buffer size of 400 bytes
378 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
379 as the maximum packet-size to ensure that the packet and an extra
380 NUL character can always fit in the buffer. This stops GDB
381 trashing stubs that try to squeeze an extra NUL into what is
382 already a full buffer (As of 1999-12-04 that was most stubs). */
383 rsa
->remote_packet_size
= 400 - 1;
385 /* This one is filled in when a ``g'' packet is received. */
386 rsa
->actual_register_packet_size
= 0;
388 /* Should rsa->sizeof_g_packet needs more space than the
389 default, adjust the size accordingly. Remember that each byte is
390 encoded as two characters. 32 is the overhead for the packet
391 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
392 (``$NN:G...#NN'') is a better guess, the below has been padded a
394 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
395 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
397 /* Make sure that the packet buffer is plenty big enough for
398 this architecture. */
399 if (rs
->buf_size
< rsa
->remote_packet_size
)
401 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
402 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
408 /* Return the current allowed size of a remote packet. This is
409 inferred from the current architecture, and should be used to
410 limit the length of outgoing packets. */
412 get_remote_packet_size (void)
414 struct remote_state
*rs
= get_remote_state ();
415 struct remote_arch_state
*rsa
= get_remote_arch_state ();
417 if (rs
->explicit_packet_size
)
418 return rs
->explicit_packet_size
;
420 return rsa
->remote_packet_size
;
423 static struct packet_reg
*
424 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
426 if (regnum
< 0 && regnum
>= NUM_REGS
)
430 struct packet_reg
*r
= &rsa
->regs
[regnum
];
431 gdb_assert (r
->regnum
== regnum
);
436 static struct packet_reg
*
437 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
440 for (i
= 0; i
< NUM_REGS
; i
++)
442 struct packet_reg
*r
= &rsa
->regs
[i
];
449 /* FIXME: graces/2002-08-08: These variables should eventually be
450 bound to an instance of the target object (as in gdbarch-tdep()),
451 when such a thing exists. */
453 /* This is set to the data address of the access causing the target
454 to stop for a watchpoint. */
455 static CORE_ADDR remote_watch_data_address
;
457 /* This is non-zero if target stopped for a watchpoint. */
458 static int remote_stopped_by_watchpoint_p
;
460 static struct target_ops remote_ops
;
462 static struct target_ops extended_remote_ops
;
464 /* Temporary target ops. Just like the remote_ops and
465 extended_remote_ops, but with asynchronous support. */
466 static struct target_ops remote_async_ops
;
468 static struct target_ops extended_async_remote_ops
;
470 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
471 ``forever'' still use the normal timeout mechanism. This is
472 currently used by the ASYNC code to guarentee that target reads
473 during the initial connect always time-out. Once getpkt has been
474 modified to return a timeout indication and, in turn
475 remote_wait()/wait_for_inferior() have gained a timeout parameter
477 static int wait_forever_enabled_p
= 1;
480 /* This variable chooses whether to send a ^C or a break when the user
481 requests program interruption. Although ^C is usually what remote
482 systems expect, and that is the default here, sometimes a break is
483 preferable instead. */
485 static int remote_break
;
487 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
488 remote_open knows that we don't have a file open when the program
490 static struct serial
*remote_desc
= NULL
;
492 /* This variable sets the number of bits in an address that are to be
493 sent in a memory ("M" or "m") packet. Normally, after stripping
494 leading zeros, the entire address would be sent. This variable
495 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
496 initial implementation of remote.c restricted the address sent in
497 memory packets to ``host::sizeof long'' bytes - (typically 32
498 bits). Consequently, for 64 bit targets, the upper 32 bits of an
499 address was never sent. Since fixing this bug may cause a break in
500 some remote targets this variable is principly provided to
501 facilitate backward compatibility. */
503 static int remote_address_size
;
505 /* Tempoary to track who currently owns the terminal. See
506 target_async_terminal_* for more details. */
508 static int remote_async_terminal_ours_p
;
511 /* User configurable variables for the number of characters in a
512 memory read/write packet. MIN (rsa->remote_packet_size,
513 rsa->sizeof_g_packet) is the default. Some targets need smaller
514 values (fifo overruns, et.al.) and some users need larger values
515 (speed up transfers). The variables ``preferred_*'' (the user
516 request), ``current_*'' (what was actually set) and ``forced_*''
517 (Positive - a soft limit, negative - a hard limit). */
519 struct memory_packet_config
526 /* Compute the current size of a read/write packet. Since this makes
527 use of ``actual_register_packet_size'' the computation is dynamic. */
530 get_memory_packet_size (struct memory_packet_config
*config
)
532 struct remote_state
*rs
= get_remote_state ();
533 struct remote_arch_state
*rsa
= get_remote_arch_state ();
535 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
536 law?) that some hosts don't cope very well with large alloca()
537 calls. Eventually the alloca() code will be replaced by calls to
538 xmalloc() and make_cleanups() allowing this restriction to either
539 be lifted or removed. */
540 #ifndef MAX_REMOTE_PACKET_SIZE
541 #define MAX_REMOTE_PACKET_SIZE 16384
543 /* NOTE: 20 ensures we can write at least one byte. */
544 #ifndef MIN_REMOTE_PACKET_SIZE
545 #define MIN_REMOTE_PACKET_SIZE 20
550 if (config
->size
<= 0)
551 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
553 what_they_get
= config
->size
;
557 what_they_get
= get_remote_packet_size ();
558 /* Limit the packet to the size specified by the user. */
560 && what_they_get
> config
->size
)
561 what_they_get
= config
->size
;
563 /* Limit it to the size of the targets ``g'' response unless we have
564 permission from the stub to use a larger packet size. */
565 if (rs
->explicit_packet_size
== 0
566 && rsa
->actual_register_packet_size
> 0
567 && what_they_get
> rsa
->actual_register_packet_size
)
568 what_they_get
= rsa
->actual_register_packet_size
;
570 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
571 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
572 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
573 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
575 /* Make sure there is room in the global buffer for this packet
576 (including its trailing NUL byte). */
577 if (rs
->buf_size
< what_they_get
+ 1)
579 rs
->buf_size
= 2 * what_they_get
;
580 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
583 return what_they_get
;
586 /* Update the size of a read/write packet. If they user wants
587 something really big then do a sanity check. */
590 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
592 int fixed_p
= config
->fixed_p
;
593 long size
= config
->size
;
595 error (_("Argument required (integer, `fixed' or `limited')."));
596 else if (strcmp (args
, "hard") == 0
597 || strcmp (args
, "fixed") == 0)
599 else if (strcmp (args
, "soft") == 0
600 || strcmp (args
, "limit") == 0)
605 size
= strtoul (args
, &end
, 0);
607 error (_("Invalid %s (bad syntax)."), config
->name
);
609 /* Instead of explicitly capping the size of a packet to
610 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
611 instead allowed to set the size to something arbitrarily
613 if (size
> MAX_REMOTE_PACKET_SIZE
)
614 error (_("Invalid %s (too large)."), config
->name
);
618 if (fixed_p
&& !config
->fixed_p
)
620 if (! query (_("The target may not be able to correctly handle a %s\n"
621 "of %ld bytes. Change the packet size? "),
623 error (_("Packet size not changed."));
625 /* Update the config. */
626 config
->fixed_p
= fixed_p
;
631 show_memory_packet_size (struct memory_packet_config
*config
)
633 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
635 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
636 get_memory_packet_size (config
));
638 printf_filtered (_("Packets are limited to %ld bytes.\n"),
639 get_memory_packet_size (config
));
642 static struct memory_packet_config memory_write_packet_config
=
644 "memory-write-packet-size",
648 set_memory_write_packet_size (char *args
, int from_tty
)
650 set_memory_packet_size (args
, &memory_write_packet_config
);
654 show_memory_write_packet_size (char *args
, int from_tty
)
656 show_memory_packet_size (&memory_write_packet_config
);
660 get_memory_write_packet_size (void)
662 return get_memory_packet_size (&memory_write_packet_config
);
665 static struct memory_packet_config memory_read_packet_config
=
667 "memory-read-packet-size",
671 set_memory_read_packet_size (char *args
, int from_tty
)
673 set_memory_packet_size (args
, &memory_read_packet_config
);
677 show_memory_read_packet_size (char *args
, int from_tty
)
679 show_memory_packet_size (&memory_read_packet_config
);
683 get_memory_read_packet_size (void)
685 long size
= get_memory_packet_size (&memory_read_packet_config
);
686 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
687 extra buffer size argument before the memory read size can be
688 increased beyond this. */
689 if (size
> get_remote_packet_size ())
690 size
= get_remote_packet_size ();
695 /* Generic configuration support for packets the stub optionally
696 supports. Allows the user to specify the use of the packet as well
697 as allowing GDB to auto-detect support in the remote stub. */
701 PACKET_SUPPORT_UNKNOWN
= 0,
710 enum auto_boolean detect
;
711 enum packet_support support
;
714 /* Analyze a packet's return value and update the packet config
725 update_packet_config (struct packet_config
*config
)
727 switch (config
->detect
)
729 case AUTO_BOOLEAN_TRUE
:
730 config
->support
= PACKET_ENABLE
;
732 case AUTO_BOOLEAN_FALSE
:
733 config
->support
= PACKET_DISABLE
;
735 case AUTO_BOOLEAN_AUTO
:
736 config
->support
= PACKET_SUPPORT_UNKNOWN
;
742 show_packet_config_cmd (struct packet_config
*config
)
744 char *support
= "internal-error";
745 switch (config
->support
)
751 support
= "disabled";
753 case PACKET_SUPPORT_UNKNOWN
:
757 switch (config
->detect
)
759 case AUTO_BOOLEAN_AUTO
:
760 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
761 config
->name
, support
);
763 case AUTO_BOOLEAN_TRUE
:
764 case AUTO_BOOLEAN_FALSE
:
765 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
766 config
->name
, support
);
772 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
773 const char *title
, int legacy
)
780 config
->title
= title
;
781 config
->detect
= AUTO_BOOLEAN_AUTO
;
782 config
->support
= PACKET_SUPPORT_UNKNOWN
;
783 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
785 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
787 /* set/show TITLE-packet {auto,on,off} */
788 cmd_name
= xstrprintf ("%s-packet", title
);
789 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
790 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
791 set_remote_protocol_packet_cmd
,
792 show_remote_protocol_packet_cmd
,
793 &remote_set_cmdlist
, &remote_show_cmdlist
);
794 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
798 legacy_name
= xstrprintf ("%s-packet", name
);
799 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
800 &remote_set_cmdlist
);
801 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
802 &remote_show_cmdlist
);
806 static enum packet_result
807 packet_check_result (const char *buf
)
811 /* The stub recognized the packet request. Check that the
812 operation succeeded. */
814 && isxdigit (buf
[1]) && isxdigit (buf
[2])
816 /* "Enn" - definitly an error. */
819 /* Always treat "E." as an error. This will be used for
820 more verbose error messages, such as E.memtypes. */
821 if (buf
[0] == 'E' && buf
[1] == '.')
824 /* The packet may or may not be OK. Just assume it is. */
828 /* The stub does not support the packet. */
829 return PACKET_UNKNOWN
;
832 static enum packet_result
833 packet_ok (const char *buf
, struct packet_config
*config
)
835 enum packet_result result
;
837 result
= packet_check_result (buf
);
842 /* The stub recognized the packet request. */
843 switch (config
->support
)
845 case PACKET_SUPPORT_UNKNOWN
:
847 fprintf_unfiltered (gdb_stdlog
,
848 "Packet %s (%s) is supported\n",
849 config
->name
, config
->title
);
850 config
->support
= PACKET_ENABLE
;
853 internal_error (__FILE__
, __LINE__
,
854 _("packet_ok: attempt to use a disabled packet"));
861 /* The stub does not support the packet. */
862 switch (config
->support
)
865 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
866 /* If the stub previously indicated that the packet was
867 supported then there is a protocol error.. */
868 error (_("Protocol error: %s (%s) conflicting enabled responses."),
869 config
->name
, config
->title
);
871 /* The user set it wrong. */
872 error (_("Enabled packet %s (%s) not recognized by stub"),
873 config
->name
, config
->title
);
875 case PACKET_SUPPORT_UNKNOWN
:
877 fprintf_unfiltered (gdb_stdlog
,
878 "Packet %s (%s) is NOT supported\n",
879 config
->name
, config
->title
);
880 config
->support
= PACKET_DISABLE
;
903 PACKET_qXfer_features
,
904 PACKET_qXfer_memory_map
,
911 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
914 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
915 struct cmd_list_element
*c
)
917 struct packet_config
*packet
;
919 for (packet
= remote_protocol_packets
;
920 packet
< &remote_protocol_packets
[PACKET_MAX
];
923 if (&packet
->detect
== c
->var
)
925 update_packet_config (packet
);
929 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
934 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
935 struct cmd_list_element
*c
,
938 struct packet_config
*packet
;
940 for (packet
= remote_protocol_packets
;
941 packet
< &remote_protocol_packets
[PACKET_MAX
];
944 if (&packet
->detect
== c
->var
)
946 show_packet_config_cmd (packet
);
950 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
954 /* Should we try one of the 'Z' requests? */
958 Z_PACKET_SOFTWARE_BP
,
959 Z_PACKET_HARDWARE_BP
,
966 /* For compatibility with older distributions. Provide a ``set remote
967 Z-packet ...'' command that updates all the Z packet types. */
969 static enum auto_boolean remote_Z_packet_detect
;
972 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
973 struct cmd_list_element
*c
)
976 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
978 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
979 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
984 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
985 struct cmd_list_element
*c
,
989 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
991 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
995 /* Should we try the 'ThreadInfo' query packet?
997 This variable (NOT available to the user: auto-detect only!)
998 determines whether GDB will use the new, simpler "ThreadInfo"
999 query or the older, more complex syntax for thread queries.
1000 This is an auto-detect variable (set to true at each connect,
1001 and set to false when the target fails to recognize it). */
1003 static int use_threadinfo_query
;
1004 static int use_threadextra_query
;
1006 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1007 static struct async_signal_handler
*sigint_remote_twice_token
;
1008 static struct async_signal_handler
*sigint_remote_token
;
1010 /* These are pointers to hook functions that may be set in order to
1011 modify resume/wait behavior for a particular architecture. */
1013 void (*deprecated_target_resume_hook
) (void);
1014 void (*deprecated_target_wait_loop_hook
) (void);
1018 /* These are the threads which we last sent to the remote system.
1019 -1 for all or -2 for not sent yet. */
1020 static int general_thread
;
1021 static int continue_thread
;
1023 /* Call this function as a result of
1024 1) A halt indication (T packet) containing a thread id
1025 2) A direct query of currthread
1026 3) Successful execution of set thread
1030 record_currthread (int currthread
)
1032 general_thread
= currthread
;
1034 /* If this is a new thread, add it to GDB's thread list.
1035 If we leave it up to WFI to do this, bad things will happen. */
1036 if (!in_thread_list (pid_to_ptid (currthread
)))
1038 add_thread (pid_to_ptid (currthread
));
1039 ui_out_text (uiout
, "[New ");
1040 ui_out_text (uiout
, target_pid_to_str (pid_to_ptid (currthread
)));
1041 ui_out_text (uiout
, "]\n");
1045 static char *last_pass_packet
;
1047 /* If 'QPassSignals' is supported, tell the remote stub what signals
1048 it can simply pass through to the inferior without reporting. */
1051 remote_pass_signals (void)
1053 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1055 char *pass_packet
, *p
;
1056 int numsigs
= (int) TARGET_SIGNAL_LAST
;
1059 gdb_assert (numsigs
< 256);
1060 for (i
= 0; i
< numsigs
; i
++)
1062 if (signal_stop_state (i
) == 0
1063 && signal_print_state (i
) == 0
1064 && signal_pass_state (i
) == 1)
1067 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1068 strcpy (pass_packet
, "QPassSignals:");
1069 p
= pass_packet
+ strlen (pass_packet
);
1070 for (i
= 0; i
< numsigs
; i
++)
1072 if (signal_stop_state (i
) == 0
1073 && signal_print_state (i
) == 0
1074 && signal_pass_state (i
) == 1)
1077 *p
++ = tohex (i
>> 4);
1078 *p
++ = tohex (i
& 15);
1087 if (!last_pass_packet
|| strcmp (last_pass_packet
, pass_packet
))
1089 struct remote_state
*rs
= get_remote_state ();
1090 char *buf
= rs
->buf
;
1092 putpkt (pass_packet
);
1093 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1094 packet_ok (buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1095 if (last_pass_packet
)
1096 xfree (last_pass_packet
);
1097 last_pass_packet
= pass_packet
;
1100 xfree (pass_packet
);
1104 #define MAGIC_NULL_PID 42000
1107 set_thread (int th
, int gen
)
1109 struct remote_state
*rs
= get_remote_state ();
1110 char *buf
= rs
->buf
;
1111 int state
= gen
? general_thread
: continue_thread
;
1117 buf
[1] = gen
? 'g' : 'c';
1118 if (th
== MAGIC_NULL_PID
)
1124 xsnprintf (&buf
[2], get_remote_packet_size () - 2, "-%x", -th
);
1126 xsnprintf (&buf
[2], get_remote_packet_size () - 2, "%x", th
);
1128 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1130 general_thread
= th
;
1132 continue_thread
= th
;
1135 /* Return nonzero if the thread TH is still alive on the remote system. */
1138 remote_thread_alive (ptid_t ptid
)
1140 struct remote_state
*rs
= get_remote_state ();
1141 int tid
= PIDGET (ptid
);
1144 xsnprintf (rs
->buf
, get_remote_packet_size (), "T-%08x", -tid
);
1146 xsnprintf (rs
->buf
, get_remote_packet_size (), "T%08x", tid
);
1148 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1149 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1152 /* About these extended threadlist and threadinfo packets. They are
1153 variable length packets but, the fields within them are often fixed
1154 length. They are redundent enough to send over UDP as is the
1155 remote protocol in general. There is a matching unit test module
1158 #define OPAQUETHREADBYTES 8
1160 /* a 64 bit opaque identifier */
1161 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1163 /* WARNING: This threadref data structure comes from the remote O.S.,
1164 libstub protocol encoding, and remote.c. it is not particularly
1167 /* Right now, the internal structure is int. We want it to be bigger.
1171 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1173 /* gdb_ext_thread_info is an internal GDB data structure which is
1174 equivalent to the reply of the remote threadinfo packet. */
1176 struct gdb_ext_thread_info
1178 threadref threadid
; /* External form of thread reference. */
1179 int active
; /* Has state interesting to GDB?
1181 char display
[256]; /* Brief state display, name,
1182 blocked/suspended. */
1183 char shortname
[32]; /* To be used to name threads. */
1184 char more_display
[256]; /* Long info, statistics, queue depth,
1188 /* The volume of remote transfers can be limited by submitting
1189 a mask containing bits specifying the desired information.
1190 Use a union of these values as the 'selection' parameter to
1191 get_thread_info. FIXME: Make these TAG names more thread specific.
1194 #define TAG_THREADID 1
1195 #define TAG_EXISTS 2
1196 #define TAG_DISPLAY 4
1197 #define TAG_THREADNAME 8
1198 #define TAG_MOREDISPLAY 16
1200 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1202 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1204 static char *unpack_nibble (char *buf
, int *val
);
1206 static char *pack_nibble (char *buf
, int nibble
);
1208 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1210 static char *unpack_byte (char *buf
, int *value
);
1212 static char *pack_int (char *buf
, int value
);
1214 static char *unpack_int (char *buf
, int *value
);
1216 static char *unpack_string (char *src
, char *dest
, int length
);
1218 static char *pack_threadid (char *pkt
, threadref
*id
);
1220 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1222 void int_to_threadref (threadref
*id
, int value
);
1224 static int threadref_to_int (threadref
*ref
);
1226 static void copy_threadref (threadref
*dest
, threadref
*src
);
1228 static int threadmatch (threadref
*dest
, threadref
*src
);
1230 static char *pack_threadinfo_request (char *pkt
, int mode
,
1233 static int remote_unpack_thread_info_response (char *pkt
,
1234 threadref
*expectedref
,
1235 struct gdb_ext_thread_info
1239 static int remote_get_threadinfo (threadref
*threadid
,
1240 int fieldset
, /*TAG mask */
1241 struct gdb_ext_thread_info
*info
);
1243 static char *pack_threadlist_request (char *pkt
, int startflag
,
1245 threadref
*nextthread
);
1247 static int parse_threadlist_response (char *pkt
,
1249 threadref
*original_echo
,
1250 threadref
*resultlist
,
1253 static int remote_get_threadlist (int startflag
,
1254 threadref
*nextthread
,
1258 threadref
*threadlist
);
1260 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1262 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1263 void *context
, int looplimit
);
1265 static int remote_newthread_step (threadref
*ref
, void *context
);
1267 /* Encode 64 bits in 16 chars of hex. */
1269 static const char hexchars
[] = "0123456789abcdef";
1272 ishex (int ch
, int *val
)
1274 if ((ch
>= 'a') && (ch
<= 'f'))
1276 *val
= ch
- 'a' + 10;
1279 if ((ch
>= 'A') && (ch
<= 'F'))
1281 *val
= ch
- 'A' + 10;
1284 if ((ch
>= '0') && (ch
<= '9'))
1295 if (ch
>= 'a' && ch
<= 'f')
1296 return ch
- 'a' + 10;
1297 if (ch
>= '0' && ch
<= '9')
1299 if (ch
>= 'A' && ch
<= 'F')
1300 return ch
- 'A' + 10;
1305 stub_unpack_int (char *buff
, int fieldlength
)
1312 nibble
= stubhex (*buff
++);
1316 retval
= retval
<< 4;
1322 unpack_varlen_hex (char *buff
, /* packet to parse */
1326 ULONGEST retval
= 0;
1328 while (ishex (*buff
, &nibble
))
1331 retval
= retval
<< 4;
1332 retval
|= nibble
& 0x0f;
1339 unpack_nibble (char *buf
, int *val
)
1341 ishex (*buf
++, val
);
1346 pack_nibble (char *buf
, int nibble
)
1348 *buf
++ = hexchars
[(nibble
& 0x0f)];
1353 pack_hex_byte (char *pkt
, int byte
)
1355 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1356 *pkt
++ = hexchars
[(byte
& 0xf)];
1361 unpack_byte (char *buf
, int *value
)
1363 *value
= stub_unpack_int (buf
, 2);
1368 pack_int (char *buf
, int value
)
1370 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1371 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1372 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1373 buf
= pack_hex_byte (buf
, (value
& 0xff));
1378 unpack_int (char *buf
, int *value
)
1380 *value
= stub_unpack_int (buf
, 8);
1384 #if 0 /* Currently unused, uncomment when needed. */
1385 static char *pack_string (char *pkt
, char *string
);
1388 pack_string (char *pkt
, char *string
)
1393 len
= strlen (string
);
1395 len
= 200; /* Bigger than most GDB packets, junk??? */
1396 pkt
= pack_hex_byte (pkt
, len
);
1400 if ((ch
== '\0') || (ch
== '#'))
1401 ch
= '*'; /* Protect encapsulation. */
1406 #endif /* 0 (unused) */
1409 unpack_string (char *src
, char *dest
, int length
)
1418 pack_threadid (char *pkt
, threadref
*id
)
1421 unsigned char *altid
;
1423 altid
= (unsigned char *) id
;
1424 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1426 pkt
= pack_hex_byte (pkt
, *altid
++);
1432 unpack_threadid (char *inbuf
, threadref
*id
)
1435 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1438 altref
= (char *) id
;
1440 while (inbuf
< limit
)
1442 x
= stubhex (*inbuf
++);
1443 y
= stubhex (*inbuf
++);
1444 *altref
++ = (x
<< 4) | y
;
1449 /* Externally, threadrefs are 64 bits but internally, they are still
1450 ints. This is due to a mismatch of specifications. We would like
1451 to use 64bit thread references internally. This is an adapter
1455 int_to_threadref (threadref
*id
, int value
)
1457 unsigned char *scan
;
1459 scan
= (unsigned char *) id
;
1465 *scan
++ = (value
>> 24) & 0xff;
1466 *scan
++ = (value
>> 16) & 0xff;
1467 *scan
++ = (value
>> 8) & 0xff;
1468 *scan
++ = (value
& 0xff);
1472 threadref_to_int (threadref
*ref
)
1475 unsigned char *scan
;
1481 value
= (value
<< 8) | ((*scan
++) & 0xff);
1486 copy_threadref (threadref
*dest
, threadref
*src
)
1489 unsigned char *csrc
, *cdest
;
1491 csrc
= (unsigned char *) src
;
1492 cdest
= (unsigned char *) dest
;
1499 threadmatch (threadref
*dest
, threadref
*src
)
1501 /* Things are broken right now, so just assume we got a match. */
1503 unsigned char *srcp
, *destp
;
1505 srcp
= (char *) src
;
1506 destp
= (char *) dest
;
1510 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1517 threadid:1, # always request threadid
1524 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1527 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1529 *pkt
++ = 'q'; /* Info Query */
1530 *pkt
++ = 'P'; /* process or thread info */
1531 pkt
= pack_int (pkt
, mode
); /* mode */
1532 pkt
= pack_threadid (pkt
, id
); /* threadid */
1533 *pkt
= '\0'; /* terminate */
1537 /* These values tag the fields in a thread info response packet. */
1538 /* Tagging the fields allows us to request specific fields and to
1539 add more fields as time goes by. */
1541 #define TAG_THREADID 1 /* Echo the thread identifier. */
1542 #define TAG_EXISTS 2 /* Is this process defined enough to
1543 fetch registers and its stack? */
1544 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1545 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1546 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1550 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1551 struct gdb_ext_thread_info
*info
)
1553 struct remote_state
*rs
= get_remote_state ();
1557 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
1560 /* info->threadid = 0; FIXME: implement zero_threadref. */
1562 info
->display
[0] = '\0';
1563 info
->shortname
[0] = '\0';
1564 info
->more_display
[0] = '\0';
1566 /* Assume the characters indicating the packet type have been
1568 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1569 pkt
= unpack_threadid (pkt
, &ref
);
1572 warning (_("Incomplete response to threadinfo request."));
1573 if (!threadmatch (&ref
, expectedref
))
1574 { /* This is an answer to a different request. */
1575 warning (_("ERROR RMT Thread info mismatch."));
1578 copy_threadref (&info
->threadid
, &ref
);
1580 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1582 /* Packets are terminated with nulls. */
1583 while ((pkt
< limit
) && mask
&& *pkt
)
1585 pkt
= unpack_int (pkt
, &tag
); /* tag */
1586 pkt
= unpack_byte (pkt
, &length
); /* length */
1587 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1589 warning (_("ERROR RMT: threadinfo tag mismatch."));
1593 if (tag
== TAG_THREADID
)
1597 warning (_("ERROR RMT: length of threadid is not 16."));
1601 pkt
= unpack_threadid (pkt
, &ref
);
1602 mask
= mask
& ~TAG_THREADID
;
1605 if (tag
== TAG_EXISTS
)
1607 info
->active
= stub_unpack_int (pkt
, length
);
1609 mask
= mask
& ~(TAG_EXISTS
);
1612 warning (_("ERROR RMT: 'exists' length too long."));
1618 if (tag
== TAG_THREADNAME
)
1620 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1621 mask
= mask
& ~TAG_THREADNAME
;
1624 if (tag
== TAG_DISPLAY
)
1626 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1627 mask
= mask
& ~TAG_DISPLAY
;
1630 if (tag
== TAG_MOREDISPLAY
)
1632 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1633 mask
= mask
& ~TAG_MOREDISPLAY
;
1636 warning (_("ERROR RMT: unknown thread info tag."));
1637 break; /* Not a tag we know about. */
1643 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1644 struct gdb_ext_thread_info
*info
)
1646 struct remote_state
*rs
= get_remote_state ();
1649 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
1651 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1652 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
1657 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1660 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1661 threadref
*nextthread
)
1663 *pkt
++ = 'q'; /* info query packet */
1664 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1665 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1666 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1667 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1672 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1675 parse_threadlist_response (char *pkt
, int result_limit
,
1676 threadref
*original_echo
, threadref
*resultlist
,
1679 struct remote_state
*rs
= get_remote_state ();
1681 int count
, resultcount
, done
;
1684 /* Assume the 'q' and 'M chars have been stripped. */
1685 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
1686 /* done parse past here */
1687 pkt
= unpack_byte (pkt
, &count
); /* count field */
1688 pkt
= unpack_nibble (pkt
, &done
);
1689 /* The first threadid is the argument threadid. */
1690 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1691 while ((count
-- > 0) && (pkt
< limit
))
1693 pkt
= unpack_threadid (pkt
, resultlist
++);
1694 if (resultcount
++ >= result_limit
)
1703 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1704 int *done
, int *result_count
, threadref
*threadlist
)
1706 struct remote_state
*rs
= get_remote_state ();
1707 static threadref echo_nextthread
;
1710 /* Trancate result limit to be smaller than the packet size. */
1711 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= get_remote_packet_size ())
1712 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
1714 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
1716 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1719 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
1722 if (!threadmatch (&echo_nextthread
, nextthread
))
1724 /* FIXME: This is a good reason to drop the packet. */
1725 /* Possably, there is a duplicate response. */
1727 retransmit immediatly - race conditions
1728 retransmit after timeout - yes
1730 wait for packet, then exit
1732 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
1733 return 0; /* I choose simply exiting. */
1735 if (*result_count
<= 0)
1739 warning (_("RMT ERROR : failed to get remote thread list."));
1742 return result
; /* break; */
1744 if (*result_count
> result_limit
)
1747 warning (_("RMT ERROR: threadlist response longer than requested."));
1753 /* This is the interface between remote and threads, remotes upper
1756 /* remote_find_new_threads retrieves the thread list and for each
1757 thread in the list, looks up the thread in GDB's internal list,
1758 ading the thread if it does not already exist. This involves
1759 getting partial thread lists from the remote target so, polling the
1760 quit_flag is required. */
1763 /* About this many threadisds fit in a packet. */
1765 #define MAXTHREADLISTRESULTS 32
1768 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
1771 int done
, i
, result_count
;
1775 static threadref nextthread
;
1776 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1781 if (loopcount
++ > looplimit
)
1784 warning (_("Remote fetch threadlist -infinite loop-."));
1787 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1788 &done
, &result_count
, resultthreadlist
))
1793 /* Clear for later iterations. */
1795 /* Setup to resume next batch of thread references, set nextthread. */
1796 if (result_count
>= 1)
1797 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1799 while (result_count
--)
1800 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1807 remote_newthread_step (threadref
*ref
, void *context
)
1811 ptid
= pid_to_ptid (threadref_to_int (ref
));
1813 if (!in_thread_list (ptid
))
1815 return 1; /* continue iterator */
1818 #define CRAZY_MAX_THREADS 1000
1821 remote_current_thread (ptid_t oldpid
)
1823 struct remote_state
*rs
= get_remote_state ();
1826 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1827 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
1828 /* Use strtoul here, so we'll correctly parse values whose highest
1829 bit is set. The protocol carries them as a simple series of
1830 hex digits; in the absence of a sign, strtol will see such
1831 values as positive numbers out of range for signed 'long', and
1832 return LONG_MAX to indicate an overflow. */
1833 return pid_to_ptid (strtoul (&rs
->buf
[2], NULL
, 16));
1838 /* Find new threads for info threads command.
1839 * Original version, using John Metzler's thread protocol.
1843 remote_find_new_threads (void)
1845 remote_threadlist_iterator (remote_newthread_step
, 0,
1847 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
) /* ack ack ack */
1848 inferior_ptid
= remote_current_thread (inferior_ptid
);
1852 * Find all threads for info threads command.
1853 * Uses new thread protocol contributed by Cisco.
1854 * Falls back and attempts to use the older method (above)
1855 * if the target doesn't respond to the new method.
1859 remote_threads_info (void)
1861 struct remote_state
*rs
= get_remote_state ();
1865 if (remote_desc
== 0) /* paranoia */
1866 error (_("Command can only be used when connected to the remote target."));
1868 if (use_threadinfo_query
)
1870 putpkt ("qfThreadInfo");
1871 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1873 if (bufp
[0] != '\0') /* q packet recognized */
1875 while (*bufp
++ == 'm') /* reply contains one or more TID */
1879 /* Use strtoul here, so we'll correctly parse values
1880 whose highest bit is set. The protocol carries
1881 them as a simple series of hex digits; in the
1882 absence of a sign, strtol will see such values as
1883 positive numbers out of range for signed 'long',
1884 and return LONG_MAX to indicate an overflow. */
1885 tid
= strtoul (bufp
, &bufp
, 16);
1886 if (tid
!= 0 && !in_thread_list (pid_to_ptid (tid
)))
1887 add_thread (pid_to_ptid (tid
));
1889 while (*bufp
++ == ','); /* comma-separated list */
1890 putpkt ("qsThreadInfo");
1891 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1898 /* Else fall back to old method based on jmetzler protocol. */
1899 use_threadinfo_query
= 0;
1900 remote_find_new_threads ();
1905 * Collect a descriptive string about the given thread.
1906 * The target may say anything it wants to about the thread
1907 * (typically info about its blocked / runnable state, name, etc.).
1908 * This string will appear in the info threads display.
1910 * Optional: targets are not required to implement this function.
1914 remote_threads_extra_info (struct thread_info
*tp
)
1916 struct remote_state
*rs
= get_remote_state ();
1920 struct gdb_ext_thread_info threadinfo
;
1921 static char display_buf
[100]; /* arbitrary... */
1922 int n
= 0; /* position in display_buf */
1924 if (remote_desc
== 0) /* paranoia */
1925 internal_error (__FILE__
, __LINE__
,
1926 _("remote_threads_extra_info"));
1928 if (use_threadextra_query
)
1930 xsnprintf (rs
->buf
, get_remote_packet_size (), "qThreadExtraInfo,%x",
1933 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1934 if (rs
->buf
[0] != 0)
1936 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
1937 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
1938 display_buf
[result
] = '\0';
1943 /* If the above query fails, fall back to the old method. */
1944 use_threadextra_query
= 0;
1945 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
1946 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
1947 int_to_threadref (&id
, PIDGET (tp
->ptid
));
1948 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
1949 if (threadinfo
.active
)
1951 if (*threadinfo
.shortname
)
1952 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
1953 " Name: %s,", threadinfo
.shortname
);
1954 if (*threadinfo
.display
)
1955 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
1956 " State: %s,", threadinfo
.display
);
1957 if (*threadinfo
.more_display
)
1958 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
1959 " Priority: %s", threadinfo
.more_display
);
1963 /* For purely cosmetic reasons, clear up trailing commas. */
1964 if (',' == display_buf
[n
-1])
1965 display_buf
[n
-1] = ' ';
1973 /* Restart the remote side; this is an extended protocol operation. */
1976 extended_remote_restart (void)
1978 struct remote_state
*rs
= get_remote_state ();
1980 /* Send the restart command; for reasons I don't understand the
1981 remote side really expects a number after the "R". */
1982 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
1985 remote_fileio_reset ();
1987 /* Now query for status so this looks just like we restarted
1988 gdbserver from scratch. */
1990 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1993 /* Clean up connection to a remote debugger. */
1996 remote_close (int quitting
)
1999 serial_close (remote_desc
);
2003 /* Query the remote side for the text, data and bss offsets. */
2008 struct remote_state
*rs
= get_remote_state ();
2012 CORE_ADDR text_addr
, data_addr
, bss_addr
;
2013 struct section_offsets
*offs
;
2015 putpkt ("qOffsets");
2016 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2019 if (buf
[0] == '\000')
2020 return; /* Return silently. Stub doesn't support
2024 warning (_("Remote failure reply: %s"), buf
);
2028 /* Pick up each field in turn. This used to be done with scanf, but
2029 scanf will make trouble if CORE_ADDR size doesn't match
2030 conversion directives correctly. The following code will work
2031 with any size of CORE_ADDR. */
2032 text_addr
= data_addr
= bss_addr
= 0;
2036 if (strncmp (ptr
, "Text=", 5) == 0)
2039 /* Don't use strtol, could lose on big values. */
2040 while (*ptr
&& *ptr
!= ';')
2041 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2046 if (!lose
&& strncmp (ptr
, ";Data=", 6) == 0)
2049 while (*ptr
&& *ptr
!= ';')
2050 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2055 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
2058 while (*ptr
&& *ptr
!= ';')
2059 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
2065 error (_("Malformed response to offset query, %s"), buf
);
2067 if (symfile_objfile
== NULL
)
2070 offs
= ((struct section_offsets
*)
2071 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
2072 memcpy (offs
, symfile_objfile
->section_offsets
,
2073 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
2075 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
2077 /* This is a temporary kludge to force data and bss to use the same offsets
2078 because that's what nlmconv does now. The real solution requires changes
2079 to the stub and remote.c that I don't have time to do right now. */
2081 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
2082 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
2084 objfile_relocate (symfile_objfile
, offs
);
2087 /* Stub for catch_exception. */
2090 remote_start_remote (struct ui_out
*uiout
, void *from_tty_p
)
2092 int from_tty
= * (int *) from_tty_p
;
2094 immediate_quit
++; /* Allow user to interrupt it. */
2096 /* Ack any packet which the remote side has already sent. */
2097 serial_write (remote_desc
, "+", 1);
2099 /* Let the stub know that we want it to return the thread. */
2102 inferior_ptid
= remote_current_thread (inferior_ptid
);
2104 get_offsets (); /* Get text, data & bss offsets. */
2106 putpkt ("?"); /* Initiate a query from remote machine. */
2109 start_remote (from_tty
); /* Initialize gdb process mechanisms. */
2112 /* Open a connection to a remote debugger.
2113 NAME is the filename used for communication. */
2116 remote_open (char *name
, int from_tty
)
2118 remote_open_1 (name
, from_tty
, &remote_ops
, 0, 0);
2121 /* Just like remote_open, but with asynchronous support. */
2123 remote_async_open (char *name
, int from_tty
)
2125 remote_open_1 (name
, from_tty
, &remote_async_ops
, 0, 1);
2128 /* Open a connection to a remote debugger using the extended
2129 remote gdb protocol. NAME is the filename used for communication. */
2132 extended_remote_open (char *name
, int from_tty
)
2134 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */,
2138 /* Just like extended_remote_open, but with asynchronous support. */
2140 extended_remote_async_open (char *name
, int from_tty
)
2142 remote_open_1 (name
, from_tty
, &extended_async_remote_ops
,
2143 1 /*extended_p */, 1 /* async_p */);
2146 /* Generic code for opening a connection to a remote target. */
2149 init_all_packet_configs (void)
2152 for (i
= 0; i
< PACKET_MAX
; i
++)
2153 update_packet_config (&remote_protocol_packets
[i
]);
2156 /* Symbol look-up. */
2159 remote_check_symbols (struct objfile
*objfile
)
2161 struct remote_state
*rs
= get_remote_state ();
2162 char *msg
, *reply
, *tmp
;
2163 struct minimal_symbol
*sym
;
2166 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
2169 /* Allocate a message buffer. We can't reuse the input buffer in RS,
2170 because we need both at the same time. */
2171 msg
= alloca (get_remote_packet_size ());
2173 /* Invite target to request symbol lookups. */
2175 putpkt ("qSymbol::");
2176 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2177 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
2180 while (strncmp (reply
, "qSymbol:", 8) == 0)
2183 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
2185 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
2187 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
2189 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
2190 paddr_nz (SYMBOL_VALUE_ADDRESS (sym
)),
2193 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2198 static struct serial
*
2199 remote_serial_open (char *name
)
2201 static int udp_warning
= 0;
2203 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2204 of in ser-tcp.c, because it is the remote protocol assuming that the
2205 serial connection is reliable and not the serial connection promising
2207 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
2210 The remote protocol may be unreliable over UDP.\n\
2211 Some events may be lost, rendering further debugging impossible."));
2215 return serial_open (name
);
2218 /* This type describes each known response to the qSupported
2220 struct protocol_feature
2222 /* The name of this protocol feature. */
2225 /* The default for this protocol feature. */
2226 enum packet_support default_support
;
2228 /* The function to call when this feature is reported, or after
2229 qSupported processing if the feature is not supported.
2230 The first argument points to this structure. The second
2231 argument indicates whether the packet requested support be
2232 enabled, disabled, or probed (or the default, if this function
2233 is being called at the end of processing and this feature was
2234 not reported). The third argument may be NULL; if not NULL, it
2235 is a NUL-terminated string taken from the packet following
2236 this feature's name and an equals sign. */
2237 void (*func
) (const struct protocol_feature
*, enum packet_support
,
2240 /* The corresponding packet for this feature. Only used if
2241 FUNC is remote_supported_packet. */
2246 remote_supported_packet (const struct protocol_feature
*feature
,
2247 enum packet_support support
,
2248 const char *argument
)
2252 warning (_("Remote qSupported response supplied an unexpected value for"
2253 " \"%s\"."), feature
->name
);
2257 if (remote_protocol_packets
[feature
->packet
].support
2258 == PACKET_SUPPORT_UNKNOWN
)
2259 remote_protocol_packets
[feature
->packet
].support
= support
;
2263 remote_packet_size (const struct protocol_feature
*feature
,
2264 enum packet_support support
, const char *value
)
2266 struct remote_state
*rs
= get_remote_state ();
2271 if (support
!= PACKET_ENABLE
)
2274 if (value
== NULL
|| *value
== '\0')
2276 warning (_("Remote target reported \"%s\" without a size."),
2282 packet_size
= strtol (value
, &value_end
, 16);
2283 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
2285 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
2286 feature
->name
, value
);
2290 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
2292 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
2293 packet_size
, MAX_REMOTE_PACKET_SIZE
);
2294 packet_size
= MAX_REMOTE_PACKET_SIZE
;
2297 /* Record the new maximum packet size. */
2298 rs
->explicit_packet_size
= packet_size
;
2301 static struct protocol_feature remote_protocol_features
[] = {
2302 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
2303 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
2304 PACKET_qXfer_auxv
},
2305 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
2306 PACKET_qXfer_features
},
2307 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
2308 PACKET_qXfer_memory_map
},
2309 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
2310 PACKET_QPassSignals
},
2314 remote_query_supported (void)
2316 struct remote_state
*rs
= get_remote_state ();
2319 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
2321 /* The packet support flags are handled differently for this packet
2322 than for most others. We treat an error, a disabled packet, and
2323 an empty response identically: any features which must be reported
2324 to be used will be automatically disabled. An empty buffer
2325 accomplishes this, since that is also the representation for a list
2326 containing no features. */
2329 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
2331 putpkt ("qSupported");
2332 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2334 /* If an error occured, warn, but do not return - just reset the
2335 buffer to empty and go on to disable features. */
2336 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
2339 warning (_("Remote failure reply: %s"), rs
->buf
);
2344 memset (seen
, 0, sizeof (seen
));
2349 enum packet_support is_supported
;
2350 char *p
, *end
, *name_end
, *value
;
2352 /* First separate out this item from the rest of the packet. If
2353 there's another item after this, we overwrite the separator
2354 (terminated strings are much easier to work with). */
2356 end
= strchr (p
, ';');
2359 end
= p
+ strlen (p
);
2369 warning (_("empty item in \"qSupported\" response"));
2374 name_end
= strchr (p
, '=');
2377 /* This is a name=value entry. */
2378 is_supported
= PACKET_ENABLE
;
2379 value
= name_end
+ 1;
2388 is_supported
= PACKET_ENABLE
;
2392 is_supported
= PACKET_DISABLE
;
2396 is_supported
= PACKET_SUPPORT_UNKNOWN
;
2400 warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p
);
2406 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
2407 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
2409 const struct protocol_feature
*feature
;
2412 feature
= &remote_protocol_features
[i
];
2413 feature
->func (feature
, is_supported
, value
);
2418 /* If we increased the packet size, make sure to increase the global
2419 buffer size also. We delay this until after parsing the entire
2420 qSupported packet, because this is the same buffer we were
2422 if (rs
->buf_size
< rs
->explicit_packet_size
)
2424 rs
->buf_size
= rs
->explicit_packet_size
;
2425 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
2428 /* Handle the defaults for unmentioned features. */
2429 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
2432 const struct protocol_feature
*feature
;
2434 feature
= &remote_protocol_features
[i
];
2435 feature
->func (feature
, feature
->default_support
, NULL
);
2441 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
,
2442 int extended_p
, int async_p
)
2444 struct remote_state
*rs
= get_remote_state ();
2446 error (_("To open a remote debug connection, you need to specify what\n"
2447 "serial device is attached to the remote system\n"
2448 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
2450 /* See FIXME above. */
2452 wait_forever_enabled_p
= 1;
2454 target_preopen (from_tty
);
2456 unpush_target (target
);
2458 /* Make sure we send the passed signals list the next time we resume. */
2459 xfree (last_pass_packet
);
2460 last_pass_packet
= NULL
;
2462 remote_fileio_reset ();
2463 reopen_exec_file ();
2466 remote_desc
= remote_serial_open (name
);
2468 perror_with_name (name
);
2470 if (baud_rate
!= -1)
2472 if (serial_setbaudrate (remote_desc
, baud_rate
))
2474 /* The requested speed could not be set. Error out to
2475 top level after closing remote_desc. Take care to
2476 set remote_desc to NULL to avoid closing remote_desc
2478 serial_close (remote_desc
);
2480 perror_with_name (name
);
2484 serial_raw (remote_desc
);
2486 /* If there is something sitting in the buffer we might take it as a
2487 response to a command, which would be bad. */
2488 serial_flush_input (remote_desc
);
2492 puts_filtered ("Remote debugging using ");
2493 puts_filtered (name
);
2494 puts_filtered ("\n");
2496 push_target (target
); /* Switch to using remote target now. */
2498 /* Reset the target state; these things will be queried either by
2499 remote_query_supported or as they are needed. */
2500 init_all_packet_configs ();
2501 rs
->explicit_packet_size
= 0;
2503 general_thread
= -2;
2504 continue_thread
= -2;
2506 /* Probe for ability to use "ThreadInfo" query, as required. */
2507 use_threadinfo_query
= 1;
2508 use_threadextra_query
= 1;
2510 /* The first packet we send to the target is the optional "supported
2511 packets" request. If the target can answer this, it will tell us
2512 which later probes to skip. */
2513 remote_query_supported ();
2515 /* Next, if the target can specify a description, read it. We do
2516 this before anything involving memory or registers. */
2517 target_find_description ();
2519 /* Without this, some commands which require an active target (such
2520 as kill) won't work. This variable serves (at least) double duty
2521 as both the pid of the target process (if it has such), and as a
2522 flag indicating that a target is active. These functions should
2523 be split out into seperate variables, especially since GDB will
2524 someday have a notion of debugging several processes. */
2526 inferior_ptid
= pid_to_ptid (MAGIC_NULL_PID
);
2530 /* With this target we start out by owning the terminal. */
2531 remote_async_terminal_ours_p
= 1;
2533 /* FIXME: cagney/1999-09-23: During the initial connection it is
2534 assumed that the target is already ready and able to respond to
2535 requests. Unfortunately remote_start_remote() eventually calls
2536 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2537 around this. Eventually a mechanism that allows
2538 wait_for_inferior() to expect/get timeouts will be
2540 wait_forever_enabled_p
= 0;
2543 /* First delete any symbols previously loaded from shared libraries. */
2544 no_shared_libraries (NULL
, 0);
2546 /* Start the remote connection. If error() or QUIT, discard this
2547 target (we'd otherwise be in an inconsistent state) and then
2548 propogate the error on up the exception chain. This ensures that
2549 the caller doesn't stumble along blindly assuming that the
2550 function succeeded. The CLI doesn't have this problem but other
2551 UI's, such as MI do.
2553 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2554 this function should return an error indication letting the
2555 caller restore the previous state. Unfortunately the command
2556 ``target remote'' is directly wired to this function making that
2557 impossible. On a positive note, the CLI side of this problem has
2558 been fixed - the function set_cmd_context() makes it possible for
2559 all the ``target ....'' commands to share a common callback
2560 function. See cli-dump.c. */
2562 struct gdb_exception ex
2563 = catch_exception (uiout
, remote_start_remote
, &from_tty
,
2569 wait_forever_enabled_p
= 1;
2570 throw_exception (ex
);
2575 wait_forever_enabled_p
= 1;
2579 /* Tell the remote that we are using the extended protocol. */
2581 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2584 if (exec_bfd
) /* No use without an exec file. */
2585 remote_check_symbols (symfile_objfile
);
2588 /* This takes a program previously attached to and detaches it. After
2589 this is done, GDB can be used to debug some other program. We
2590 better not have left any breakpoints in the target program or it'll
2591 die when it hits one. */
2594 remote_detach (char *args
, int from_tty
)
2596 struct remote_state
*rs
= get_remote_state ();
2599 error (_("Argument given to \"detach\" when remotely debugging."));
2601 /* Tell the remote target to detach. */
2602 strcpy (rs
->buf
, "D");
2603 remote_send (&rs
->buf
, &rs
->buf_size
);
2605 /* Unregister the file descriptor from the event loop. */
2606 if (target_is_async_p ())
2607 serial_async (remote_desc
, NULL
, 0);
2609 target_mourn_inferior ();
2611 puts_filtered ("Ending remote debugging.\n");
2614 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2617 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
2620 error (_("Argument given to \"detach\" when remotely debugging."));
2622 /* Unregister the file descriptor from the event loop. */
2623 if (target_is_async_p ())
2624 serial_async (remote_desc
, NULL
, 0);
2626 target_mourn_inferior ();
2628 puts_filtered ("Ending remote debugging.\n");
2631 /* Convert hex digit A to a number. */
2636 if (a
>= '0' && a
<= '9')
2638 else if (a
>= 'a' && a
<= 'f')
2639 return a
- 'a' + 10;
2640 else if (a
>= 'A' && a
<= 'F')
2641 return a
- 'A' + 10;
2643 error (_("Reply contains invalid hex digit %d"), a
);
2647 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
2651 for (i
= 0; i
< count
; i
++)
2653 if (hex
[0] == 0 || hex
[1] == 0)
2655 /* Hex string is short, or of uneven length.
2656 Return the count that has been converted so far. */
2659 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
2665 /* Convert number NIB to a hex digit. */
2673 return 'a' + nib
- 10;
2677 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
2680 /* May use a length, or a nul-terminated string as input. */
2682 count
= strlen ((char *) bin
);
2684 for (i
= 0; i
< count
; i
++)
2686 *hex
++ = tohex ((*bin
>> 4) & 0xf);
2687 *hex
++ = tohex (*bin
++ & 0xf);
2693 /* Check for the availability of vCont. This function should also check
2697 remote_vcont_probe (struct remote_state
*rs
)
2701 strcpy (rs
->buf
, "vCont?");
2703 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2706 /* Make sure that the features we assume are supported. */
2707 if (strncmp (buf
, "vCont", 5) == 0)
2710 int support_s
, support_S
, support_c
, support_C
;
2716 while (p
&& *p
== ';')
2719 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2721 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2723 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2725 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
2728 p
= strchr (p
, ';');
2731 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2732 BUF will make packet_ok disable the packet. */
2733 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
2737 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
2740 /* Resume the remote inferior by using a "vCont" packet. The thread
2741 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2742 resumed thread should be single-stepped and/or signalled. If PTID's
2743 PID is -1, then all threads are resumed; the thread to be stepped and/or
2744 signalled is given in the global INFERIOR_PTID. This function returns
2745 non-zero iff it resumes the inferior.
2747 This function issues a strict subset of all possible vCont commands at the
2751 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2753 struct remote_state
*rs
= get_remote_state ();
2754 int pid
= PIDGET (ptid
);
2755 char *buf
= NULL
, *outbuf
;
2756 struct cleanup
*old_cleanup
;
2758 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
2759 remote_vcont_probe (rs
);
2761 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
2764 /* If we could generate a wider range of packets, we'd have to worry
2765 about overflowing BUF. Should there be a generic
2766 "multi-part-packet" packet? */
2768 if (PIDGET (inferior_ptid
) == MAGIC_NULL_PID
)
2770 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2771 don't have any PID numbers the inferior will understand. Make sure
2772 to only send forms that do not specify a PID. */
2773 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2774 outbuf
= xstrprintf ("vCont;S%02x", siggnal
);
2776 outbuf
= xstrprintf ("vCont;s");
2777 else if (siggnal
!= TARGET_SIGNAL_0
)
2778 outbuf
= xstrprintf ("vCont;C%02x", siggnal
);
2780 outbuf
= xstrprintf ("vCont;c");
2784 /* Resume all threads, with preference for INFERIOR_PTID. */
2785 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2786 outbuf
= xstrprintf ("vCont;S%02x:%x;c", siggnal
,
2787 PIDGET (inferior_ptid
));
2789 outbuf
= xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid
));
2790 else if (siggnal
!= TARGET_SIGNAL_0
)
2791 outbuf
= xstrprintf ("vCont;C%02x:%x;c", siggnal
,
2792 PIDGET (inferior_ptid
));
2794 outbuf
= xstrprintf ("vCont;c");
2798 /* Scheduler locking; resume only PTID. */
2799 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
2800 outbuf
= xstrprintf ("vCont;S%02x:%x", siggnal
, pid
);
2802 outbuf
= xstrprintf ("vCont;s:%x", pid
);
2803 else if (siggnal
!= TARGET_SIGNAL_0
)
2804 outbuf
= xstrprintf ("vCont;C%02x:%x", siggnal
, pid
);
2806 outbuf
= xstrprintf ("vCont;c:%x", pid
);
2809 gdb_assert (outbuf
&& strlen (outbuf
) < get_remote_packet_size ());
2810 old_cleanup
= make_cleanup (xfree
, outbuf
);
2814 do_cleanups (old_cleanup
);
2819 /* Tell the remote machine to resume. */
2821 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
2823 static int last_sent_step
;
2826 remote_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2828 struct remote_state
*rs
= get_remote_state ();
2830 int pid
= PIDGET (ptid
);
2832 last_sent_signal
= siggnal
;
2833 last_sent_step
= step
;
2835 /* A hook for when we need to do something at the last moment before
2837 if (deprecated_target_resume_hook
)
2838 (*deprecated_target_resume_hook
) ();
2840 /* Update the inferior on signals to silently pass, if they've changed. */
2841 remote_pass_signals ();
2843 /* The vCont packet doesn't need to specify threads via Hc. */
2844 if (remote_vcont_resume (ptid
, step
, siggnal
))
2847 /* All other supported resume packets do use Hc, so call set_thread. */
2849 set_thread (0, 0); /* Run any thread. */
2851 set_thread (pid
, 0); /* Run this thread. */
2854 if (siggnal
!= TARGET_SIGNAL_0
)
2856 buf
[0] = step
? 'S' : 'C';
2857 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2858 buf
[2] = tohex (((int) siggnal
) & 0xf);
2862 strcpy (buf
, step
? "s" : "c");
2867 /* Same as remote_resume, but with async support. */
2869 remote_async_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
2871 remote_resume (ptid
, step
, siggnal
);
2873 /* We are about to start executing the inferior, let's register it
2874 with the event loop. NOTE: this is the one place where all the
2875 execution commands end up. We could alternatively do this in each
2876 of the execution commands in infcmd.c. */
2877 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2878 into infcmd.c in order to allow inferior function calls to work
2879 NOT asynchronously. */
2880 if (target_can_async_p ())
2881 target_async (inferior_event_handler
, 0);
2882 /* Tell the world that the target is now executing. */
2883 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2884 this? Instead, should the client of target just assume (for
2885 async targets) that the target is going to start executing? Is
2886 this information already found in the continuation block? */
2887 if (target_is_async_p ())
2888 target_executing
= 1;
2892 /* Set up the signal handler for SIGINT, while the target is
2893 executing, ovewriting the 'regular' SIGINT signal handler. */
2895 initialize_sigint_signal_handler (void)
2897 sigint_remote_token
=
2898 create_async_signal_handler (async_remote_interrupt
, NULL
);
2899 signal (SIGINT
, handle_remote_sigint
);
2902 /* Signal handler for SIGINT, while the target is executing. */
2904 handle_remote_sigint (int sig
)
2906 signal (sig
, handle_remote_sigint_twice
);
2907 sigint_remote_twice_token
=
2908 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
2909 mark_async_signal_handler_wrapper (sigint_remote_token
);
2912 /* Signal handler for SIGINT, installed after SIGINT has already been
2913 sent once. It will take effect the second time that the user sends
2916 handle_remote_sigint_twice (int sig
)
2918 signal (sig
, handle_sigint
);
2919 sigint_remote_twice_token
=
2920 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
2921 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
2924 /* Perform the real interruption of the target execution, in response
2927 async_remote_interrupt (gdb_client_data arg
)
2930 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2935 /* Perform interrupt, if the first attempt did not succeed. Just give
2936 up on the target alltogether. */
2938 async_remote_interrupt_twice (gdb_client_data arg
)
2941 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
2942 /* Do something only if the target was not killed by the previous
2944 if (target_executing
)
2947 signal (SIGINT
, handle_remote_sigint
);
2951 /* Reinstall the usual SIGINT handlers, after the target has
2954 cleanup_sigint_signal_handler (void *dummy
)
2956 signal (SIGINT
, handle_sigint
);
2957 if (sigint_remote_twice_token
)
2958 delete_async_signal_handler (&sigint_remote_twice_token
);
2959 if (sigint_remote_token
)
2960 delete_async_signal_handler (&sigint_remote_token
);
2963 /* Send ^C to target to halt it. Target will respond, and send us a
2965 static void (*ofunc
) (int);
2967 /* The command line interface's stop routine. This function is installed
2968 as a signal handler for SIGINT. The first time a user requests a
2969 stop, we call remote_stop to send a break or ^C. If there is no
2970 response from the target (it didn't stop when the user requested it),
2971 we ask the user if he'd like to detach from the target. */
2973 remote_interrupt (int signo
)
2975 /* If this doesn't work, try more severe steps. */
2976 signal (signo
, remote_interrupt_twice
);
2979 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2984 /* The user typed ^C twice. */
2987 remote_interrupt_twice (int signo
)
2989 signal (signo
, ofunc
);
2991 signal (signo
, remote_interrupt
);
2994 /* This is the generic stop called via the target vector. When a target
2995 interrupt is requested, either by the command line or the GUI, we
2996 will eventually end up here. */
3000 /* Send a break or a ^C, depending on user preference. */
3002 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
3005 serial_send_break (remote_desc
);
3007 serial_write (remote_desc
, "\003", 1);
3010 /* Ask the user what to do when an interrupt is received. */
3013 interrupt_query (void)
3015 target_terminal_ours ();
3017 if (query ("Interrupted while waiting for the program.\n\
3018 Give up (and stop debugging it)? "))
3020 target_mourn_inferior ();
3021 deprecated_throw_reason (RETURN_QUIT
);
3024 target_terminal_inferior ();
3027 /* Enable/disable target terminal ownership. Most targets can use
3028 terminal groups to control terminal ownership. Remote targets are
3029 different in that explicit transfer of ownership to/from GDB/target
3033 remote_async_terminal_inferior (void)
3035 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
3036 sync_execution here. This function should only be called when
3037 GDB is resuming the inferior in the forground. A background
3038 resume (``run&'') should leave GDB in control of the terminal and
3039 consequently should not call this code. */
3040 if (!sync_execution
)
3042 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
3043 calls target_terminal_*() idenpotent. The event-loop GDB talking
3044 to an asynchronous target with a synchronous command calls this
3045 function from both event-top.c and infrun.c/infcmd.c. Once GDB
3046 stops trying to transfer the terminal to the target when it
3047 shouldn't this guard can go away. */
3048 if (!remote_async_terminal_ours_p
)
3050 delete_file_handler (input_fd
);
3051 remote_async_terminal_ours_p
= 0;
3052 initialize_sigint_signal_handler ();
3053 /* NOTE: At this point we could also register our selves as the
3054 recipient of all input. Any characters typed could then be
3055 passed on down to the target. */
3059 remote_async_terminal_ours (void)
3061 /* See FIXME in remote_async_terminal_inferior. */
3062 if (!sync_execution
)
3064 /* See FIXME in remote_async_terminal_inferior. */
3065 if (remote_async_terminal_ours_p
)
3067 cleanup_sigint_signal_handler (NULL
);
3068 add_file_handler (input_fd
, stdin_event_handler
, 0);
3069 remote_async_terminal_ours_p
= 1;
3072 /* If nonzero, ignore the next kill. */
3077 remote_console_output (char *msg
)
3081 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
3084 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
3087 fputs_unfiltered (tb
, gdb_stdtarg
);
3089 gdb_flush (gdb_stdtarg
);
3092 /* Wait until the remote machine stops, then return,
3093 storing status in STATUS just as `wait' would.
3094 Returns "pid", which in the case of a multi-threaded
3095 remote OS, is the thread-id. */
3098 remote_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3100 struct remote_state
*rs
= get_remote_state ();
3101 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3102 ULONGEST thread_num
= -1;
3105 status
->kind
= TARGET_WAITKIND_EXITED
;
3106 status
->value
.integer
= 0;
3112 ofunc
= signal (SIGINT
, remote_interrupt
);
3113 getpkt (&rs
->buf
, &rs
->buf_size
, 1);
3114 signal (SIGINT
, ofunc
);
3118 /* This is a hook for when we need to do something (perhaps the
3119 collection of trace data) every time the target stops. */
3120 if (deprecated_target_wait_loop_hook
)
3121 (*deprecated_target_wait_loop_hook
) ();
3123 remote_stopped_by_watchpoint_p
= 0;
3127 case 'E': /* Error of some sort. */
3128 warning (_("Remote failure reply: %s"), buf
);
3130 case 'F': /* File-I/O request. */
3131 remote_fileio_request (buf
);
3133 case 'T': /* Status with PC, SP, FP, ... */
3135 gdb_byte regs
[MAX_REGISTER_SIZE
];
3137 /* Expedited reply, containing Signal, {regno, reg} repeat. */
3138 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
3140 n... = register number
3141 r... = register contents
3143 p
= &buf
[3]; /* after Txx */
3152 /* If the packet contains a register number save it in
3153 pnum and set p1 to point to the character following
3154 it. Otherwise p1 points to p. */
3156 /* If this packet is an awatch packet, don't parse the
3157 'a' as a register number. */
3159 if (strncmp (p
, "awatch", strlen("awatch")) != 0)
3161 /* Read the ``P'' register number. */
3162 pnum
= strtol (p
, &p_temp
, 16);
3168 if (p1
== p
) /* No register number present here. */
3170 p1
= strchr (p
, ':');
3172 error (_("Malformed packet(a) (missing colon): %s\n\
3175 if (strncmp (p
, "thread", p1
- p
) == 0)
3177 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
3178 record_currthread (thread_num
);
3181 else if ((strncmp (p
, "watch", p1
- p
) == 0)
3182 || (strncmp (p
, "rwatch", p1
- p
) == 0)
3183 || (strncmp (p
, "awatch", p1
- p
) == 0))
3185 remote_stopped_by_watchpoint_p
= 1;
3186 p
= unpack_varlen_hex (++p1
, &addr
);
3187 remote_watch_data_address
= (CORE_ADDR
)addr
;
3191 /* Silently skip unknown optional info. */
3192 p_temp
= strchr (p1
+ 1, ';');
3199 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
3203 error (_("Malformed packet(b) (missing colon): %s\n\
3208 error (_("Remote sent bad register number %s: %s\n\
3210 phex_nz (pnum
, 0), p
, buf
);
3212 fieldsize
= hex2bin (p
, regs
,
3213 register_size (current_gdbarch
,
3216 if (fieldsize
< register_size (current_gdbarch
,
3218 warning (_("Remote reply is too short: %s"), buf
);
3219 regcache_raw_supply (current_regcache
,
3224 error (_("Remote register badly formatted: %s\nhere: %s"),
3229 case 'S': /* Old style status, just signal only. */
3230 status
->kind
= TARGET_WAITKIND_STOPPED
;
3231 status
->value
.sig
= (enum target_signal
)
3232 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3236 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
3237 record_currthread (thread_num
);
3240 case 'W': /* Target exited. */
3242 /* The remote process exited. */
3243 status
->kind
= TARGET_WAITKIND_EXITED
;
3244 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
3248 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
3249 status
->value
.sig
= (enum target_signal
)
3250 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3254 case 'O': /* Console output. */
3255 remote_console_output (buf
+ 1);
3258 if (last_sent_signal
!= TARGET_SIGNAL_0
)
3260 /* Zero length reply means that we tried 'S' or 'C' and
3261 the remote system doesn't support it. */
3262 target_terminal_ours_for_output ();
3264 ("Can't send signals to this remote system. %s not sent.\n",
3265 target_signal_to_name (last_sent_signal
));
3266 last_sent_signal
= TARGET_SIGNAL_0
;
3267 target_terminal_inferior ();
3269 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
3270 putpkt ((char *) buf
);
3273 /* else fallthrough */
3275 warning (_("Invalid remote reply: %s"), buf
);
3280 if (thread_num
!= -1)
3282 return pid_to_ptid (thread_num
);
3284 return inferior_ptid
;
3287 /* Async version of remote_wait. */
3289 remote_async_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3291 struct remote_state
*rs
= get_remote_state ();
3292 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3293 ULONGEST thread_num
= -1;
3296 status
->kind
= TARGET_WAITKIND_EXITED
;
3297 status
->value
.integer
= 0;
3299 remote_stopped_by_watchpoint_p
= 0;
3305 if (!target_is_async_p ())
3306 ofunc
= signal (SIGINT
, remote_interrupt
);
3307 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3308 _never_ wait for ever -> test on target_is_async_p().
3309 However, before we do that we need to ensure that the caller
3310 knows how to take the target into/out of async mode. */
3311 getpkt (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
3312 if (!target_is_async_p ())
3313 signal (SIGINT
, ofunc
);
3317 /* This is a hook for when we need to do something (perhaps the
3318 collection of trace data) every time the target stops. */
3319 if (deprecated_target_wait_loop_hook
)
3320 (*deprecated_target_wait_loop_hook
) ();
3324 case 'E': /* Error of some sort. */
3325 warning (_("Remote failure reply: %s"), buf
);
3327 case 'F': /* File-I/O request. */
3328 remote_fileio_request (buf
);
3330 case 'T': /* Status with PC, SP, FP, ... */
3332 gdb_byte regs
[MAX_REGISTER_SIZE
];
3334 /* Expedited reply, containing Signal, {regno, reg} repeat. */
3335 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
3337 n... = register number
3338 r... = register contents
3340 p
= &buf
[3]; /* after Txx */
3349 /* If the packet contains a register number, save it
3350 in pnum and set p1 to point to the character
3351 following it. Otherwise p1 points to p. */
3353 /* If this packet is an awatch packet, don't parse the 'a'
3354 as a register number. */
3356 if (!strncmp (p
, "awatch", strlen ("awatch")) != 0)
3358 /* Read the register number. */
3359 pnum
= strtol (p
, &p_temp
, 16);
3365 if (p1
== p
) /* No register number present here. */
3367 p1
= strchr (p
, ':');
3369 error (_("Malformed packet(a) (missing colon): %s\n\
3372 if (strncmp (p
, "thread", p1
- p
) == 0)
3374 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
3375 record_currthread (thread_num
);
3378 else if ((strncmp (p
, "watch", p1
- p
) == 0)
3379 || (strncmp (p
, "rwatch", p1
- p
) == 0)
3380 || (strncmp (p
, "awatch", p1
- p
) == 0))
3382 remote_stopped_by_watchpoint_p
= 1;
3383 p
= unpack_varlen_hex (++p1
, &addr
);
3384 remote_watch_data_address
= (CORE_ADDR
)addr
;
3388 /* Silently skip unknown optional info. */
3389 p_temp
= strchr (p1
+ 1, ';');
3397 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
3400 error (_("Malformed packet(b) (missing colon): %s\n\
3405 error (_("Remote sent bad register number %ld: %s\n\
3409 fieldsize
= hex2bin (p
, regs
,
3410 register_size (current_gdbarch
,
3413 if (fieldsize
< register_size (current_gdbarch
,
3415 warning (_("Remote reply is too short: %s"), buf
);
3416 regcache_raw_supply (current_regcache
, reg
->regnum
, regs
);
3420 error (_("Remote register badly formatted: %s\nhere: %s"),
3425 case 'S': /* Old style status, just signal only. */
3426 status
->kind
= TARGET_WAITKIND_STOPPED
;
3427 status
->value
.sig
= (enum target_signal
)
3428 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3432 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
3433 record_currthread (thread_num
);
3436 case 'W': /* Target exited. */
3438 /* The remote process exited. */
3439 status
->kind
= TARGET_WAITKIND_EXITED
;
3440 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
3444 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
3445 status
->value
.sig
= (enum target_signal
)
3446 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3450 case 'O': /* Console output. */
3451 remote_console_output (buf
+ 1);
3452 /* Return immediately to the event loop. The event loop will
3453 still be waiting on the inferior afterwards. */
3454 status
->kind
= TARGET_WAITKIND_IGNORE
;
3457 if (last_sent_signal
!= TARGET_SIGNAL_0
)
3459 /* Zero length reply means that we tried 'S' or 'C' and
3460 the remote system doesn't support it. */
3461 target_terminal_ours_for_output ();
3463 ("Can't send signals to this remote system. %s not sent.\n",
3464 target_signal_to_name (last_sent_signal
));
3465 last_sent_signal
= TARGET_SIGNAL_0
;
3466 target_terminal_inferior ();
3468 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
3469 putpkt ((char *) buf
);
3472 /* else fallthrough */
3474 warning (_("Invalid remote reply: %s"), buf
);
3479 if (thread_num
!= -1)
3481 return pid_to_ptid (thread_num
);
3483 return inferior_ptid
;
3486 /* Fetch a single register using a 'p' packet. */
3489 fetch_register_using_p (struct packet_reg
*reg
)
3491 struct remote_state
*rs
= get_remote_state ();
3493 char regp
[MAX_REGISTER_SIZE
];
3496 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
3499 if (reg
->pnum
== -1)
3504 p
+= hexnumstr (p
, reg
->pnum
);
3506 remote_send (&rs
->buf
, &rs
->buf_size
);
3510 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
3514 case PACKET_UNKNOWN
:
3517 error (_("Could not fetch register \"%s\""),
3518 gdbarch_register_name (current_gdbarch
, reg
->regnum
));
3521 /* If this register is unfetchable, tell the regcache. */
3524 regcache_raw_supply (current_regcache
, reg
->regnum
, NULL
);
3525 set_register_cached (reg
->regnum
, -1);
3529 /* Otherwise, parse and supply the value. */
3535 error (_("fetch_register_using_p: early buf termination"));
3537 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3540 regcache_raw_supply (current_regcache
, reg
->regnum
, regp
);
3544 /* Fetch the registers included in the target's 'g' packet. */
3547 send_g_packet (void)
3549 struct remote_state
*rs
= get_remote_state ();
3554 sprintf (rs
->buf
, "g");
3555 remote_send (&rs
->buf
, &rs
->buf_size
);
3557 /* We can get out of synch in various cases. If the first character
3558 in the buffer is not a hex character, assume that has happened
3559 and try to fetch another packet to read. */
3560 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
3561 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
3562 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
3563 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
3566 fprintf_unfiltered (gdb_stdlog
,
3567 "Bad register packet; fetching a new packet\n");
3568 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3571 buf_len
= strlen (rs
->buf
);
3573 /* Sanity check the received packet. */
3574 if (buf_len
% 2 != 0)
3575 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
3581 process_g_packet (void)
3583 struct remote_state
*rs
= get_remote_state ();
3584 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3589 buf_len
= strlen (rs
->buf
);
3591 /* Further sanity checks, with knowledge of the architecture. */
3592 if (REGISTER_BYTES_OK_P () && !REGISTER_BYTES_OK (buf_len
/ 2))
3593 error (_("Remote 'g' packet reply is wrong length: %s"), rs
->buf
);
3594 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
3595 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
3597 /* Save the size of the packet sent to us by the target. It is used
3598 as a heuristic when determining the max size of packets that the
3599 target can safely receive. */
3600 if (rsa
->actual_register_packet_size
== 0)
3601 rsa
->actual_register_packet_size
= buf_len
;
3603 /* If this is smaller than we guessed the 'g' packet would be,
3604 update our records. A 'g' reply that doesn't include a register's
3605 value implies either that the register is not available, or that
3606 the 'p' packet must be used. */
3607 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
3609 rsa
->sizeof_g_packet
= buf_len
/ 2;
3611 for (i
= 0; i
< NUM_REGS
; i
++)
3613 if (rsa
->regs
[i
].pnum
== -1)
3616 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
3617 rsa
->regs
[i
].in_g_packet
= 0;
3619 rsa
->regs
[i
].in_g_packet
= 1;
3623 regs
= alloca (rsa
->sizeof_g_packet
);
3625 /* Unimplemented registers read as all bits zero. */
3626 memset (regs
, 0, rsa
->sizeof_g_packet
);
3628 /* Reply describes registers byte by byte, each byte encoded as two
3629 hex characters. Suck them all up, then supply them to the
3630 register cacheing/storage mechanism. */
3633 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
3635 if (p
[0] == 0 || p
[1] == 0)
3636 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
3637 internal_error (__FILE__
, __LINE__
,
3638 "unexpected end of 'g' packet reply");
3640 if (p
[0] == 'x' && p
[1] == 'x')
3641 regs
[i
] = 0; /* 'x' */
3643 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3649 for (i
= 0; i
< NUM_REGS
; i
++)
3651 struct packet_reg
*r
= &rsa
->regs
[i
];
3654 if (r
->offset
* 2 >= strlen (rs
->buf
))
3655 /* This shouldn't happen - we adjusted in_g_packet above. */
3656 internal_error (__FILE__
, __LINE__
,
3657 "unexpected end of 'g' packet reply");
3658 else if (rs
->buf
[r
->offset
* 2] == 'x')
3660 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
3661 /* The register isn't available, mark it as such (at
3662 the same time setting the value to zero). */
3663 regcache_raw_supply (current_regcache
, r
->regnum
, NULL
);
3664 set_register_cached (i
, -1);
3667 regcache_raw_supply (current_regcache
, r
->regnum
,
3675 fetch_registers_using_g (void)
3678 process_g_packet ();
3682 remote_fetch_registers (int regnum
)
3684 struct remote_state
*rs
= get_remote_state ();
3685 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3688 set_thread (PIDGET (inferior_ptid
), 1);
3692 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
3693 gdb_assert (reg
!= NULL
);
3695 /* If this register might be in the 'g' packet, try that first -
3696 we are likely to read more than one register. If this is the
3697 first 'g' packet, we might be overly optimistic about its
3698 contents, so fall back to 'p'. */
3699 if (reg
->in_g_packet
)
3701 fetch_registers_using_g ();
3702 if (reg
->in_g_packet
)
3706 if (fetch_register_using_p (reg
))
3709 /* This register is not available. */
3710 regcache_raw_supply (current_regcache
, reg
->regnum
, NULL
);
3711 set_register_cached (reg
->regnum
, -1);
3716 fetch_registers_using_g ();
3718 for (i
= 0; i
< NUM_REGS
; i
++)
3719 if (!rsa
->regs
[i
].in_g_packet
)
3720 if (!fetch_register_using_p (&rsa
->regs
[i
]))
3722 /* This register is not available. */
3723 regcache_raw_supply (current_regcache
, i
, NULL
);
3724 set_register_cached (i
, -1);
3728 /* Prepare to store registers. Since we may send them all (using a
3729 'G' request), we have to read out the ones we don't want to change
3733 remote_prepare_to_store (void)
3735 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3737 gdb_byte buf
[MAX_REGISTER_SIZE
];
3739 /* Make sure the entire registers array is valid. */
3740 switch (remote_protocol_packets
[PACKET_P
].support
)
3742 case PACKET_DISABLE
:
3743 case PACKET_SUPPORT_UNKNOWN
:
3744 /* Make sure all the necessary registers are cached. */
3745 for (i
= 0; i
< NUM_REGS
; i
++)
3746 if (rsa
->regs
[i
].in_g_packet
)
3747 regcache_raw_read (current_regcache
, rsa
->regs
[i
].regnum
, buf
);
3754 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
3755 packet was not recognized. */
3758 store_register_using_P (struct packet_reg
*reg
)
3760 struct remote_state
*rs
= get_remote_state ();
3761 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3762 /* Try storing a single register. */
3763 char *buf
= rs
->buf
;
3764 gdb_byte regp
[MAX_REGISTER_SIZE
];
3767 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
3770 if (reg
->pnum
== -1)
3773 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
3774 p
= buf
+ strlen (buf
);
3775 regcache_raw_collect (current_regcache
, reg
->regnum
, regp
);
3776 bin2hex (regp
, p
, register_size (current_gdbarch
, reg
->regnum
));
3777 remote_send (&rs
->buf
, &rs
->buf_size
);
3779 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
3784 error (_("Could not write register \"%s\""),
3785 gdbarch_register_name (current_gdbarch
, reg
->regnum
));
3786 case PACKET_UNKNOWN
:
3789 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
3793 /* Store register REGNUM, or all registers if REGNUM == -1, from the
3794 contents of the register cache buffer. FIXME: ignores errors. */
3797 store_registers_using_G (void)
3799 struct remote_state
*rs
= get_remote_state ();
3800 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3804 /* Extract all the registers in the regcache copying them into a
3808 regs
= alloca (rsa
->sizeof_g_packet
);
3809 memset (regs
, 0, rsa
->sizeof_g_packet
);
3810 for (i
= 0; i
< NUM_REGS
; i
++)
3812 struct packet_reg
*r
= &rsa
->regs
[i
];
3814 regcache_raw_collect (current_regcache
, r
->regnum
, regs
+ r
->offset
);
3818 /* Command describes registers byte by byte,
3819 each byte encoded as two hex characters. */
3822 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
3824 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
3825 remote_send (&rs
->buf
, &rs
->buf_size
);
3828 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
3829 of the register cache buffer. FIXME: ignores errors. */
3832 remote_store_registers (int regnum
)
3834 struct remote_state
*rs
= get_remote_state ();
3835 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3838 set_thread (PIDGET (inferior_ptid
), 1);
3842 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
3843 gdb_assert (reg
!= NULL
);
3845 /* Always prefer to store registers using the 'P' packet if
3846 possible; we often change only a small number of registers.
3847 Sometimes we change a larger number; we'd need help from a
3848 higher layer to know to use 'G'. */
3849 if (store_register_using_P (reg
))
3852 /* For now, don't complain if we have no way to write the
3853 register. GDB loses track of unavailable registers too
3854 easily. Some day, this may be an error. We don't have
3855 any way to read the register, either... */
3856 if (!reg
->in_g_packet
)
3859 store_registers_using_G ();
3863 store_registers_using_G ();
3865 for (i
= 0; i
< NUM_REGS
; i
++)
3866 if (!rsa
->regs
[i
].in_g_packet
)
3867 if (!store_register_using_P (&rsa
->regs
[i
]))
3868 /* See above for why we do not issue an error here. */
3873 /* Return the number of hex digits in num. */
3876 hexnumlen (ULONGEST num
)
3880 for (i
= 0; num
!= 0; i
++)
3886 /* Set BUF to the minimum number of hex digits representing NUM. */
3889 hexnumstr (char *buf
, ULONGEST num
)
3891 int len
= hexnumlen (num
);
3892 return hexnumnstr (buf
, num
, len
);
3896 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3899 hexnumnstr (char *buf
, ULONGEST num
, int width
)
3905 for (i
= width
- 1; i
>= 0; i
--)
3907 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
3914 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3917 remote_address_masked (CORE_ADDR addr
)
3919 if (remote_address_size
> 0
3920 && remote_address_size
< (sizeof (ULONGEST
) * 8))
3922 /* Only create a mask when that mask can safely be constructed
3923 in a ULONGEST variable. */
3925 mask
= (mask
<< remote_address_size
) - 1;
3931 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
3932 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
3933 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
3934 (which may be more than *OUT_LEN due to escape characters). The
3935 total number of bytes in the output buffer will be at most
3939 remote_escape_output (const gdb_byte
*buffer
, int len
,
3940 gdb_byte
*out_buf
, int *out_len
,
3943 int input_index
, output_index
;
3946 for (input_index
= 0; input_index
< len
; input_index
++)
3948 gdb_byte b
= buffer
[input_index
];
3950 if (b
== '$' || b
== '#' || b
== '}')
3952 /* These must be escaped. */
3953 if (output_index
+ 2 > out_maxlen
)
3955 out_buf
[output_index
++] = '}';
3956 out_buf
[output_index
++] = b
^ 0x20;
3960 if (output_index
+ 1 > out_maxlen
)
3962 out_buf
[output_index
++] = b
;
3966 *out_len
= input_index
;
3967 return output_index
;
3970 /* Convert BUFFER, escaped data LEN bytes long, into binary data
3971 in OUT_BUF. Return the number of bytes written to OUT_BUF.
3972 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
3974 This function reverses remote_escape_output. It allows more
3975 escaped characters than that function does, in particular because
3976 '*' must be escaped to avoid the run-length encoding processing
3977 in reading packets. */
3980 remote_unescape_input (const gdb_byte
*buffer
, int len
,
3981 gdb_byte
*out_buf
, int out_maxlen
)
3983 int input_index
, output_index
;
3988 for (input_index
= 0; input_index
< len
; input_index
++)
3990 gdb_byte b
= buffer
[input_index
];
3992 if (output_index
+ 1 > out_maxlen
)
3994 warning (_("Received too much data from remote target;"
3995 " ignoring overflow."));
3996 return output_index
;
4001 out_buf
[output_index
++] = b
^ 0x20;
4007 out_buf
[output_index
++] = b
;
4011 error (_("Unmatched escape character in target response."));
4013 return output_index
;
4016 /* Determine whether the remote target supports binary downloading.
4017 This is accomplished by sending a no-op memory write of zero length
4018 to the target at the specified address. It does not suffice to send
4019 the whole packet, since many stubs strip the eighth bit and
4020 subsequently compute a wrong checksum, which causes real havoc with
4023 NOTE: This can still lose if the serial line is not eight-bit
4024 clean. In cases like this, the user should clear "remote
4028 check_binary_download (CORE_ADDR addr
)
4030 struct remote_state
*rs
= get_remote_state ();
4032 switch (remote_protocol_packets
[PACKET_X
].support
)
4034 case PACKET_DISABLE
:
4038 case PACKET_SUPPORT_UNKNOWN
:
4044 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4046 p
+= hexnumstr (p
, (ULONGEST
) 0);
4050 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
4051 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4053 if (rs
->buf
[0] == '\0')
4056 fprintf_unfiltered (gdb_stdlog
,
4057 "binary downloading NOT suppported by target\n");
4058 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
4063 fprintf_unfiltered (gdb_stdlog
,
4064 "binary downloading suppported by target\n");
4065 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
4072 /* Write memory data directly to the remote machine.
4073 This does not inform the data cache; the data cache uses this.
4074 HEADER is the starting part of the packet.
4075 MEMADDR is the address in the remote memory space.
4076 MYADDR is the address of the buffer in our space.
4077 LEN is the number of bytes.
4078 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
4079 should send data as binary ('X'), or hex-encoded ('M').
4081 The function creates packet of the form
4082 <HEADER><ADDRESS>,<LENGTH>:<DATA>
4084 where encoding of <DATA> is termined by PACKET_FORMAT.
4086 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
4089 Returns the number of bytes transferred, or 0 (setting errno) for
4090 error. Only transfer a single packet. */
4093 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
4094 const gdb_byte
*myaddr
, int len
,
4095 char packet_format
, int use_length
)
4097 struct remote_state
*rs
= get_remote_state ();
4107 if (packet_format
!= 'X' && packet_format
!= 'M')
4108 internal_error (__FILE__
, __LINE__
,
4109 "remote_write_bytes_aux: bad packet format");
4111 /* Should this be the selected frame? */
4112 gdbarch_remote_translate_xfer_address (current_gdbarch
,
4120 payload_size
= get_memory_write_packet_size ();
4122 /* The packet buffer will be large enough for the payload;
4123 get_memory_packet_size ensures this. */
4126 /* Compute the size of the actual payload by subtracting out the
4127 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
4129 payload_size
-= strlen ("$,:#NN");
4131 /* The comma won't be used. */
4133 header_length
= strlen (header
);
4134 payload_size
-= header_length
;
4135 payload_size
-= hexnumlen (memaddr
);
4137 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
4139 strcat (rs
->buf
, header
);
4140 p
= rs
->buf
+ strlen (header
);
4142 /* Compute a best guess of the number of bytes actually transfered. */
4143 if (packet_format
== 'X')
4145 /* Best guess at number of bytes that will fit. */
4146 todo
= min (len
, payload_size
);
4148 payload_size
-= hexnumlen (todo
);
4149 todo
= min (todo
, payload_size
);
4153 /* Num bytes that will fit. */
4154 todo
= min (len
, payload_size
/ 2);
4156 payload_size
-= hexnumlen (todo
);
4157 todo
= min (todo
, payload_size
/ 2);
4161 internal_error (__FILE__
, __LINE__
,
4162 _("minumum packet size too small to write data"));
4164 /* If we already need another packet, then try to align the end
4165 of this packet to a useful boundary. */
4166 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
4167 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
4169 /* Append "<memaddr>". */
4170 memaddr
= remote_address_masked (memaddr
);
4171 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
4178 /* Append <len>. Retain the location/size of <len>. It may need to
4179 be adjusted once the packet body has been created. */
4181 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
4189 /* Append the packet body. */
4190 if (packet_format
== 'X')
4192 /* Binary mode. Send target system values byte by byte, in
4193 increasing byte addresses. Only escape certain critical
4195 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
4198 /* If not all TODO bytes fit, then we'll need another packet. Make
4199 a second try to keep the end of the packet aligned. Don't do
4200 this if the packet is tiny. */
4201 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
4205 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
4207 if (new_nr_bytes
!= nr_bytes
)
4208 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
4213 p
+= payload_length
;
4214 if (use_length
&& nr_bytes
< todo
)
4216 /* Escape chars have filled up the buffer prematurely,
4217 and we have actually sent fewer bytes than planned.
4218 Fix-up the length field of the packet. Use the same
4219 number of characters as before. */
4220 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
4221 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
4226 /* Normal mode: Send target system values byte by byte, in
4227 increasing byte addresses. Each byte is encoded as a two hex
4229 nr_bytes
= bin2hex (myaddr
, p
, todo
);
4233 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
4234 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4236 if (rs
->buf
[0] == 'E')
4238 /* There is no correspondance between what the remote protocol
4239 uses for errors and errno codes. We would like a cleaner way
4240 of representing errors (big enough to include errno codes,
4241 bfd_error codes, and others). But for now just return EIO. */
4246 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
4247 fewer bytes than we'd planned. */
4251 /* Write memory data directly to the remote machine.
4252 This does not inform the data cache; the data cache uses this.
4253 MEMADDR is the address in the remote memory space.
4254 MYADDR is the address of the buffer in our space.
4255 LEN is the number of bytes.
4257 Returns number of bytes transferred, or 0 (setting errno) for
4258 error. Only transfer a single packet. */
4261 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
4263 char *packet_format
= 0;
4265 /* Check whether the target supports binary download. */
4266 check_binary_download (memaddr
);
4268 switch (remote_protocol_packets
[PACKET_X
].support
)
4271 packet_format
= "X";
4273 case PACKET_DISABLE
:
4274 packet_format
= "M";
4276 case PACKET_SUPPORT_UNKNOWN
:
4277 internal_error (__FILE__
, __LINE__
,
4278 _("remote_write_bytes: bad internal state"));
4280 internal_error (__FILE__
, __LINE__
, _("bad switch"));
4283 return remote_write_bytes_aux (packet_format
,
4284 memaddr
, myaddr
, len
, packet_format
[0], 1);
4287 /* Read memory data directly from the remote machine.
4288 This does not use the data cache; the data cache uses this.
4289 MEMADDR is the address in the remote memory space.
4290 MYADDR is the address of the buffer in our space.
4291 LEN is the number of bytes.
4293 Returns number of bytes transferred, or 0 for error. */
4295 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
4296 remote targets) shouldn't attempt to read the entire buffer.
4297 Instead it should read a single packet worth of data and then
4298 return the byte size of that packet to the caller. The caller (its
4299 caller and its callers caller ;-) already contains code for
4300 handling partial reads. */
4303 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
4305 struct remote_state
*rs
= get_remote_state ();
4306 int max_buf_size
; /* Max size of packet output buffer. */
4309 /* Should this be the selected frame? */
4310 gdbarch_remote_translate_xfer_address (current_gdbarch
,
4318 max_buf_size
= get_memory_read_packet_size ();
4319 /* The packet buffer will be large enough for the payload;
4320 get_memory_packet_size ensures this. */
4329 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
4331 /* construct "m"<memaddr>","<len>" */
4332 /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */
4333 memaddr
= remote_address_masked (memaddr
);
4336 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
4338 p
+= hexnumstr (p
, (ULONGEST
) todo
);
4342 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4344 if (rs
->buf
[0] == 'E'
4345 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
4346 && rs
->buf
[3] == '\0')
4348 /* There is no correspondance between what the remote
4349 protocol uses for errors and errno codes. We would like
4350 a cleaner way of representing errors (big enough to
4351 include errno codes, bfd_error codes, and others). But
4352 for now just return EIO. */
4357 /* Reply describes memory byte by byte,
4358 each byte encoded as two hex characters. */
4361 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
4363 /* Reply is short. This means that we were able to read
4364 only part of what we wanted to. */
4365 return i
+ (origlen
- len
);
4374 /* Read or write LEN bytes from inferior memory at MEMADDR,
4375 transferring to or from debugger address BUFFER. Write to inferior
4376 if SHOULD_WRITE is nonzero. Returns length of data written or
4377 read; 0 for error. TARGET is unused. */
4380 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
4381 int should_write
, struct mem_attrib
*attrib
,
4382 struct target_ops
*target
)
4387 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
4389 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
4394 /* Sends a packet with content determined by the printf format string
4395 FORMAT and the remaining arguments, then gets the reply. Returns
4396 whether the packet was a success, a failure, or unknown. */
4399 remote_send_printf (const char *format
, ...)
4401 struct remote_state
*rs
= get_remote_state ();
4402 int max_size
= get_remote_packet_size ();
4405 va_start (ap
, format
);
4408 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
4409 internal_error (__FILE__
, __LINE__
, "Too long remote packet.");
4411 if (putpkt (rs
->buf
) < 0)
4412 error (_("Communication problem with target."));
4415 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4417 return packet_check_result (rs
->buf
);
4421 restore_remote_timeout (void *p
)
4423 int value
= *(int *)p
;
4424 remote_timeout
= value
;
4427 /* Flash writing can take quite some time. We'll set
4428 effectively infinite timeout for flash operations.
4429 In future, we'll need to decide on a better approach. */
4430 static const int remote_flash_timeout
= 1000;
4433 remote_flash_erase (struct target_ops
*ops
,
4434 ULONGEST address
, LONGEST length
)
4436 int saved_remote_timeout
= remote_timeout
;
4437 enum packet_result ret
;
4439 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
4440 &saved_remote_timeout
);
4441 remote_timeout
= remote_flash_timeout
;
4443 ret
= remote_send_printf ("vFlashErase:%s,%s",
4448 case PACKET_UNKNOWN
:
4449 error (_("Remote target does not support flash erase"));
4451 error (_("Error erasing flash with vFlashErase packet"));
4456 do_cleanups (back_to
);
4460 remote_flash_write (struct target_ops
*ops
,
4461 ULONGEST address
, LONGEST length
,
4462 const gdb_byte
*data
)
4464 int saved_remote_timeout
= remote_timeout
;
4466 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
4467 &saved_remote_timeout
);
4469 remote_timeout
= remote_flash_timeout
;
4470 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
4471 do_cleanups (back_to
);
4477 remote_flash_done (struct target_ops
*ops
)
4479 int saved_remote_timeout
= remote_timeout
;
4481 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
4482 &saved_remote_timeout
);
4484 remote_timeout
= remote_flash_timeout
;
4485 ret
= remote_send_printf ("vFlashDone");
4486 do_cleanups (back_to
);
4490 case PACKET_UNKNOWN
:
4491 error (_("Remote target does not support vFlashDone"));
4493 error (_("Error finishing flash operation"));
4500 remote_files_info (struct target_ops
*ignore
)
4502 puts_filtered ("Debugging a target over a serial line.\n");
4505 /* Stuff for dealing with the packets which are part of this protocol.
4506 See comment at top of file for details. */
4508 /* Read a single character from the remote end. */
4511 readchar (int timeout
)
4515 ch
= serial_readchar (remote_desc
, timeout
);
4520 switch ((enum serial_rc
) ch
)
4523 target_mourn_inferior ();
4524 error (_("Remote connection closed"));
4527 perror_with_name (_("Remote communication error"));
4529 case SERIAL_TIMEOUT
:
4535 /* Send the command in *BUF to the remote machine, and read the reply
4536 into *BUF. Report an error if we get an error reply. Resize
4537 *BUF using xrealloc if necessary to hold the result, and update
4541 remote_send (char **buf
,
4545 getpkt (buf
, sizeof_buf
, 0);
4547 if ((*buf
)[0] == 'E')
4548 error (_("Remote failure reply: %s"), *buf
);
4551 /* Display a null-terminated packet on stdout, for debugging, using C
4555 print_packet (char *buf
)
4557 puts_filtered ("\"");
4558 fputstr_filtered (buf
, '"', gdb_stdout
);
4559 puts_filtered ("\"");
4565 return putpkt_binary (buf
, strlen (buf
));
4568 /* Send a packet to the remote machine, with error checking. The data
4569 of the packet is in BUF. The string in BUF can be at most
4570 get_remote_packet_size () - 5 to account for the $, # and checksum,
4571 and for a possible /0 if we are debugging (remote_debug) and want
4572 to print the sent packet as a string. */
4575 putpkt_binary (char *buf
, int cnt
)
4578 unsigned char csum
= 0;
4579 char *buf2
= alloca (cnt
+ 6);
4585 /* Copy the packet into buffer BUF2, encapsulating it
4586 and giving it a checksum. */
4591 for (i
= 0; i
< cnt
; i
++)
4597 *p
++ = tohex ((csum
>> 4) & 0xf);
4598 *p
++ = tohex (csum
& 0xf);
4600 /* Send it over and over until we get a positive ack. */
4604 int started_error_output
= 0;
4609 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
4610 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
4611 fprintf_unfiltered (gdb_stdlog
, "...");
4612 gdb_flush (gdb_stdlog
);
4614 if (serial_write (remote_desc
, buf2
, p
- buf2
))
4615 perror_with_name (_("putpkt: write failed"));
4617 /* Read until either a timeout occurs (-2) or '+' is read. */
4620 ch
= readchar (remote_timeout
);
4628 case SERIAL_TIMEOUT
:
4630 if (started_error_output
)
4632 putchar_unfiltered ('\n');
4633 started_error_output
= 0;
4642 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
4646 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
4647 case SERIAL_TIMEOUT
:
4651 break; /* Retransmit buffer. */
4655 fprintf_unfiltered (gdb_stdlog
,
4656 "Packet instead of Ack, ignoring it\n");
4657 /* It's probably an old response sent because an ACK
4658 was lost. Gobble up the packet and ack it so it
4659 doesn't get retransmitted when we resend this
4662 serial_write (remote_desc
, "+", 1);
4663 continue; /* Now, go look for +. */
4668 if (!started_error_output
)
4670 started_error_output
= 1;
4671 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
4673 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
4677 break; /* Here to retransmit. */
4681 /* This is wrong. If doing a long backtrace, the user should be
4682 able to get out next time we call QUIT, without anything as
4683 violent as interrupt_query. If we want to provide a way out of
4684 here without getting to the next QUIT, it should be based on
4685 hitting ^C twice as in remote_wait. */
4695 /* Come here after finding the start of a frame when we expected an
4696 ack. Do our best to discard the rest of this packet. */
4705 c
= readchar (remote_timeout
);
4708 case SERIAL_TIMEOUT
:
4709 /* Nothing we can do. */
4712 /* Discard the two bytes of checksum and stop. */
4713 c
= readchar (remote_timeout
);
4715 c
= readchar (remote_timeout
);
4718 case '*': /* Run length encoding. */
4719 /* Discard the repeat count. */
4720 c
= readchar (remote_timeout
);
4725 /* A regular character. */
4731 /* Come here after finding the start of the frame. Collect the rest
4732 into *BUF, verifying the checksum, length, and handling run-length
4733 compression. NUL terminate the buffer. If there is not enough room,
4734 expand *BUF using xrealloc.
4736 Returns -1 on error, number of characters in buffer (ignoring the
4737 trailing NULL) on success. (could be extended to return one of the
4738 SERIAL status indications). */
4741 read_frame (char **buf_p
,
4754 c
= readchar (remote_timeout
);
4757 case SERIAL_TIMEOUT
:
4759 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
4763 fputs_filtered ("Saw new packet start in middle of old one\n",
4765 return -1; /* Start a new packet, count retries. */
4768 unsigned char pktcsum
;
4774 check_0
= readchar (remote_timeout
);
4776 check_1
= readchar (remote_timeout
);
4778 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
4781 fputs_filtered ("Timeout in checksum, retrying\n",
4785 else if (check_0
< 0 || check_1
< 0)
4788 fputs_filtered ("Communication error in checksum\n",
4793 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
4794 if (csum
== pktcsum
)
4799 fprintf_filtered (gdb_stdlog
,
4800 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4802 fputstrn_filtered (buf
, bc
, 0, gdb_stdlog
);
4803 fputs_filtered ("\n", gdb_stdlog
);
4805 /* Number of characters in buffer ignoring trailing
4809 case '*': /* Run length encoding. */
4814 c
= readchar (remote_timeout
);
4816 repeat
= c
- ' ' + 3; /* Compute repeat count. */
4818 /* The character before ``*'' is repeated. */
4820 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
4822 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
4824 /* Make some more room in the buffer. */
4825 *sizeof_buf
+= repeat
;
4826 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
4830 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
4836 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
4840 if (bc
>= *sizeof_buf
- 1)
4842 /* Make some more room in the buffer. */
4844 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
4855 /* Read a packet from the remote machine, with error checking, and
4856 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4857 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4858 rather than timing out; this is used (in synchronous mode) to wait
4859 for a target that is is executing user code to stop. */
4860 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4861 don't have to change all the calls to getpkt to deal with the
4862 return value, because at the moment I don't know what the right
4863 thing to do it for those. */
4871 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
4875 /* Read a packet from the remote machine, with error checking, and
4876 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4877 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4878 rather than timing out; this is used (in synchronous mode) to wait
4879 for a target that is is executing user code to stop. If FOREVER ==
4880 0, this function is allowed to time out gracefully and return an
4881 indication of this to the caller. Otherwise return the number
4884 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
4891 strcpy (*buf
, "timeout");
4895 timeout
= watchdog
> 0 ? watchdog
: -1;
4899 timeout
= remote_timeout
;
4903 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
4905 /* This can loop forever if the remote side sends us characters
4906 continuously, but if it pauses, we'll get a zero from
4907 readchar because of timeout. Then we'll count that as a
4910 /* Note that we will only wait forever prior to the start of a
4911 packet. After that, we expect characters to arrive at a
4912 brisk pace. They should show up within remote_timeout
4917 c
= readchar (timeout
);
4919 if (c
== SERIAL_TIMEOUT
)
4921 if (forever
) /* Watchdog went off? Kill the target. */
4924 target_mourn_inferior ();
4925 error (_("Watchdog has expired. Target detached."));
4928 fputs_filtered ("Timed out.\n", gdb_stdlog
);
4934 /* We've found the start of a packet, now collect the data. */
4936 val
= read_frame (buf
, sizeof_buf
);
4942 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
4943 fputstrn_unfiltered (*buf
, val
, 0, gdb_stdlog
);
4944 fprintf_unfiltered (gdb_stdlog
, "\n");
4946 serial_write (remote_desc
, "+", 1);
4950 /* Try the whole thing again. */
4952 serial_write (remote_desc
, "-", 1);
4955 /* We have tried hard enough, and just can't receive the packet.
4958 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
4959 serial_write (remote_desc
, "+", 1);
4966 /* For some mysterious reason, wait_for_inferior calls kill instead of
4967 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4971 target_mourn_inferior ();
4975 /* Use catch_errors so the user can quit from gdb even when we aren't on
4976 speaking terms with the remote system. */
4977 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
4979 /* Don't wait for it to die. I'm not really sure it matters whether
4980 we do or not. For the existing stubs, kill is a noop. */
4981 target_mourn_inferior ();
4984 /* Async version of remote_kill. */
4986 remote_async_kill (void)
4988 /* Unregister the file descriptor from the event loop. */
4989 if (target_is_async_p ())
4990 serial_async (remote_desc
, NULL
, 0);
4992 /* For some mysterious reason, wait_for_inferior calls kill instead of
4993 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4997 target_mourn_inferior ();
5001 /* Use catch_errors so the user can quit from gdb even when we
5002 aren't on speaking terms with the remote system. */
5003 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
5005 /* Don't wait for it to die. I'm not really sure it matters whether
5006 we do or not. For the existing stubs, kill is a noop. */
5007 target_mourn_inferior ();
5013 remote_mourn_1 (&remote_ops
);
5017 remote_async_mourn (void)
5019 remote_mourn_1 (&remote_async_ops
);
5023 extended_remote_mourn (void)
5025 /* We do _not_ want to mourn the target like this; this will
5026 remove the extended remote target from the target stack,
5027 and the next time the user says "run" it'll fail.
5029 FIXME: What is the right thing to do here? */
5031 remote_mourn_1 (&extended_remote_ops
);
5035 /* Worker function for remote_mourn. */
5037 remote_mourn_1 (struct target_ops
*target
)
5039 unpush_target (target
);
5040 generic_mourn_inferior ();
5043 /* In the extended protocol we want to be able to do things like
5044 "run" and have them basically work as expected. So we need
5045 a special create_inferior function.
5047 FIXME: One day add support for changing the exec file
5048 we're debugging, arguments and an environment. */
5051 extended_remote_create_inferior (char *exec_file
, char *args
,
5052 char **env
, int from_tty
)
5054 /* Rip out the breakpoints; we'll reinsert them after restarting
5055 the remote server. */
5056 remove_breakpoints ();
5058 /* Now restart the remote server. */
5059 extended_remote_restart ();
5061 /* NOTE: We don't need to recheck for a target description here; but
5062 if we gain the ability to switch the remote executable we may
5063 need to, if for instance we are running a process which requested
5064 different emulated hardware from the operating system. A
5065 concrete example of this is ARM GNU/Linux, where some binaries
5066 will have a legacy FPA coprocessor emulated and others may have
5067 access to a hardware VFP unit. */
5069 /* Now put the breakpoints back in. This way we're safe if the
5070 restart function works via a unix fork on the remote side. */
5071 insert_breakpoints ();
5073 /* Clean up from the last time we were running. */
5074 clear_proceed_status ();
5077 /* Async version of extended_remote_create_inferior. */
5079 extended_remote_async_create_inferior (char *exec_file
, char *args
,
5080 char **env
, int from_tty
)
5082 /* Rip out the breakpoints; we'll reinsert them after restarting
5083 the remote server. */
5084 remove_breakpoints ();
5086 /* If running asynchronously, register the target file descriptor
5087 with the event loop. */
5088 if (target_can_async_p ())
5089 target_async (inferior_event_handler
, 0);
5091 /* Now restart the remote server. */
5092 extended_remote_restart ();
5094 /* NOTE: We don't need to recheck for a target description here; but
5095 if we gain the ability to switch the remote executable we may
5096 need to, if for instance we are running a process which requested
5097 different emulated hardware from the operating system. A
5098 concrete example of this is ARM GNU/Linux, where some binaries
5099 will have a legacy FPA coprocessor emulated and others may have
5100 access to a hardware VFP unit. */
5102 /* Now put the breakpoints back in. This way we're safe if the
5103 restart function works via a unix fork on the remote side. */
5104 insert_breakpoints ();
5106 /* Clean up from the last time we were running. */
5107 clear_proceed_status ();
5111 /* Insert a breakpoint. On targets that have software breakpoint
5112 support, we ask the remote target to do the work; on targets
5113 which don't, we insert a traditional memory breakpoint. */
5116 remote_insert_breakpoint (struct bp_target_info
*bp_tgt
)
5118 CORE_ADDR addr
= bp_tgt
->placed_address
;
5119 struct remote_state
*rs
= get_remote_state ();
5121 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
5122 If it succeeds, then set the support to PACKET_ENABLE. If it
5123 fails, and the user has explicitly requested the Z support then
5124 report an error, otherwise, mark it disabled and go on. */
5126 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
5133 BREAKPOINT_FROM_PC (&bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
5134 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
5135 p
+= hexnumstr (p
, addr
);
5136 sprintf (p
, ",%d", bp_tgt
->placed_size
);
5139 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5141 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
5147 case PACKET_UNKNOWN
:
5152 return memory_insert_breakpoint (bp_tgt
);
5156 remote_remove_breakpoint (struct bp_target_info
*bp_tgt
)
5158 CORE_ADDR addr
= bp_tgt
->placed_address
;
5159 struct remote_state
*rs
= get_remote_state ();
5162 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
5170 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
5171 p
+= hexnumstr (p
, addr
);
5172 sprintf (p
, ",%d", bp_tgt
->placed_size
);
5175 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5177 return (rs
->buf
[0] == 'E');
5180 return memory_remove_breakpoint (bp_tgt
);
5184 watchpoint_to_Z_packet (int type
)
5189 return Z_PACKET_WRITE_WP
;
5192 return Z_PACKET_READ_WP
;
5195 return Z_PACKET_ACCESS_WP
;
5198 internal_error (__FILE__
, __LINE__
,
5199 _("hw_bp_to_z: bad watchpoint type %d"), type
);
5204 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
5206 struct remote_state
*rs
= get_remote_state ();
5208 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
5210 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
5213 sprintf (rs
->buf
, "Z%x,", packet
);
5214 p
= strchr (rs
->buf
, '\0');
5215 addr
= remote_address_masked (addr
);
5216 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5217 sprintf (p
, ",%x", len
);
5220 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5222 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
5225 case PACKET_UNKNOWN
:
5230 internal_error (__FILE__
, __LINE__
,
5231 _("remote_insert_watchpoint: reached end of function"));
5236 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
5238 struct remote_state
*rs
= get_remote_state ();
5240 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
5242 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
5245 sprintf (rs
->buf
, "z%x,", packet
);
5246 p
= strchr (rs
->buf
, '\0');
5247 addr
= remote_address_masked (addr
);
5248 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5249 sprintf (p
, ",%x", len
);
5251 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5253 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
5256 case PACKET_UNKNOWN
:
5261 internal_error (__FILE__
, __LINE__
,
5262 _("remote_remove_watchpoint: reached end of function"));
5266 int remote_hw_watchpoint_limit
= -1;
5267 int remote_hw_breakpoint_limit
= -1;
5270 remote_check_watch_resources (int type
, int cnt
, int ot
)
5272 if (type
== bp_hardware_breakpoint
)
5274 if (remote_hw_breakpoint_limit
== 0)
5276 else if (remote_hw_breakpoint_limit
< 0)
5278 else if (cnt
<= remote_hw_breakpoint_limit
)
5283 if (remote_hw_watchpoint_limit
== 0)
5285 else if (remote_hw_watchpoint_limit
< 0)
5289 else if (cnt
<= remote_hw_watchpoint_limit
)
5296 remote_stopped_by_watchpoint (void)
5298 return remote_stopped_by_watchpoint_p
;
5301 extern int stepped_after_stopped_by_watchpoint
;
5304 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
5307 if (remote_stopped_by_watchpoint ()
5308 || stepped_after_stopped_by_watchpoint
)
5310 *addr_p
= remote_watch_data_address
;
5319 remote_insert_hw_breakpoint (struct bp_target_info
*bp_tgt
)
5322 struct remote_state
*rs
= get_remote_state ();
5325 /* The length field should be set to the size of a breakpoint
5326 instruction, even though we aren't inserting one ourselves. */
5328 BREAKPOINT_FROM_PC (&bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
5330 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
5337 addr
= remote_address_masked (bp_tgt
->placed_address
);
5338 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5339 sprintf (p
, ",%x", bp_tgt
->placed_size
);
5342 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5344 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
5347 case PACKET_UNKNOWN
:
5352 internal_error (__FILE__
, __LINE__
,
5353 _("remote_insert_hw_breakpoint: reached end of function"));
5358 remote_remove_hw_breakpoint (struct bp_target_info
*bp_tgt
)
5361 struct remote_state
*rs
= get_remote_state ();
5364 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
5371 addr
= remote_address_masked (bp_tgt
->placed_address
);
5372 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5373 sprintf (p
, ",%x", bp_tgt
->placed_size
);
5376 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5378 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
5381 case PACKET_UNKNOWN
:
5386 internal_error (__FILE__
, __LINE__
,
5387 _("remote_remove_hw_breakpoint: reached end of function"));
5390 /* Some targets are only capable of doing downloads, and afterwards
5391 they switch to the remote serial protocol. This function provides
5392 a clean way to get from the download target to the remote target.
5393 It's basically just a wrapper so that we don't have to expose any
5394 of the internal workings of remote.c.
5396 Prior to calling this routine, you should shutdown the current
5397 target code, else you will get the "A program is being debugged
5398 already..." message. Usually a call to pop_target() suffices. */
5401 push_remote_target (char *name
, int from_tty
)
5403 printf_filtered (_("Switching to remote protocol\n"));
5404 remote_open (name
, from_tty
);
5407 /* Table used by the crc32 function to calcuate the checksum. */
5409 static unsigned long crc32_table
[256] =
5412 static unsigned long
5413 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
5415 if (!crc32_table
[1])
5417 /* Initialize the CRC table and the decoding table. */
5421 for (i
= 0; i
< 256; i
++)
5423 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
5424 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
5431 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
5437 /* compare-sections command
5439 With no arguments, compares each loadable section in the exec bfd
5440 with the same memory range on the target, and reports mismatches.
5441 Useful for verifying the image on the target against the exec file.
5442 Depends on the target understanding the new "qCRC:" request. */
5444 /* FIXME: cagney/1999-10-26: This command should be broken down into a
5445 target method (target verify memory) and generic version of the
5446 actual command. This will allow other high-level code (especially
5447 generic_load()) to make use of this target functionality. */
5450 compare_sections_command (char *args
, int from_tty
)
5452 struct remote_state
*rs
= get_remote_state ();
5454 unsigned long host_crc
, target_crc
;
5455 extern bfd
*exec_bfd
;
5456 struct cleanup
*old_chain
;
5459 const char *sectname
;
5466 error (_("command cannot be used without an exec file"));
5467 if (!current_target
.to_shortname
||
5468 strcmp (current_target
.to_shortname
, "remote") != 0)
5469 error (_("command can only be used with remote target"));
5471 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
5473 if (!(s
->flags
& SEC_LOAD
))
5474 continue; /* skip non-loadable section */
5476 size
= bfd_get_section_size (s
);
5478 continue; /* skip zero-length section */
5480 sectname
= bfd_get_section_name (exec_bfd
, s
);
5481 if (args
&& strcmp (args
, sectname
) != 0)
5482 continue; /* not the section selected by user */
5484 matched
= 1; /* do this section */
5486 /* FIXME: assumes lma can fit into long. */
5487 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
5488 (long) lma
, (long) size
);
5491 /* Be clever; compute the host_crc before waiting for target
5493 sectdata
= xmalloc (size
);
5494 old_chain
= make_cleanup (xfree
, sectdata
);
5495 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
5496 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
5498 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5499 if (rs
->buf
[0] == 'E')
5500 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
5501 sectname
, paddr (lma
), paddr (lma
+ size
));
5502 if (rs
->buf
[0] != 'C')
5503 error (_("remote target does not support this operation"));
5505 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
5506 target_crc
= target_crc
* 16 + fromhex (*tmp
);
5508 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
5509 sectname
, paddr (lma
), paddr (lma
+ size
));
5510 if (host_crc
== target_crc
)
5511 printf_filtered ("matched.\n");
5514 printf_filtered ("MIS-MATCHED!\n");
5518 do_cleanups (old_chain
);
5521 warning (_("One or more sections of the remote executable does not match\n\
5522 the loaded file\n"));
5523 if (args
&& !matched
)
5524 printf_filtered (_("No loaded section named '%s'.\n"), args
);
5527 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
5528 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
5529 number of bytes read is returned, or 0 for EOF, or -1 for error.
5530 The number of bytes read may be less than LEN without indicating an
5531 EOF. PACKET is checked and updated to indicate whether the remote
5532 target supports this object. */
5535 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
5537 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
5538 struct packet_config
*packet
)
5540 static char *finished_object
;
5541 static char *finished_annex
;
5542 static ULONGEST finished_offset
;
5544 struct remote_state
*rs
= get_remote_state ();
5545 unsigned int total
= 0;
5546 LONGEST i
, n
, packet_len
;
5548 if (packet
->support
== PACKET_DISABLE
)
5551 /* Check whether we've cached an end-of-object packet that matches
5553 if (finished_object
)
5555 if (strcmp (object_name
, finished_object
) == 0
5556 && strcmp (annex
? annex
: "", finished_annex
) == 0
5557 && offset
== finished_offset
)
5560 /* Otherwise, we're now reading something different. Discard
5562 xfree (finished_object
);
5563 xfree (finished_annex
);
5564 finished_object
= NULL
;
5565 finished_annex
= NULL
;
5568 /* Request only enough to fit in a single packet. The actual data
5569 may not, since we don't know how much of it will need to be escaped;
5570 the target is free to respond with slightly less data. We subtract
5571 five to account for the response type and the protocol frame. */
5572 n
= min (get_remote_packet_size () - 5, len
);
5573 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
5574 object_name
, annex
? annex
: "",
5575 phex_nz (offset
, sizeof offset
),
5576 phex_nz (n
, sizeof n
));
5577 i
= putpkt (rs
->buf
);
5582 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
5583 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
5586 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
5587 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
5589 /* 'm' means there is (or at least might be) more data after this
5590 batch. That does not make sense unless there's at least one byte
5591 of data in this reply. */
5592 if (rs
->buf
[0] == 'm' && packet_len
== 1)
5593 error (_("Remote qXfer reply contained no data."));
5595 /* Got some data. */
5596 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
5598 /* 'l' is an EOF marker, possibly including a final block of data,
5599 or possibly empty. Record it to bypass the next read, if one is
5601 if (rs
->buf
[0] == 'l')
5603 finished_object
= xstrdup (object_name
);
5604 finished_annex
= xstrdup (annex
? annex
: "");
5605 finished_offset
= offset
+ i
;
5612 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
5613 const char *annex
, gdb_byte
*readbuf
,
5614 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
5616 struct remote_state
*rs
= get_remote_state ();
5621 /* Handle memory using the standard memory routines. */
5622 if (object
== TARGET_OBJECT_MEMORY
)
5627 if (writebuf
!= NULL
)
5628 xfered
= remote_write_bytes (offset
, writebuf
, len
);
5630 xfered
= remote_read_bytes (offset
, readbuf
, len
);
5634 else if (xfered
== 0 && errno
== 0)
5640 /* Only handle flash writes. */
5641 if (writebuf
!= NULL
)
5647 case TARGET_OBJECT_FLASH
:
5648 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
5652 else if (xfered
== 0 && errno
== 0)
5662 /* Map pre-existing objects onto letters. DO NOT do this for new
5663 objects!!! Instead specify new query packets. */
5666 case TARGET_OBJECT_AVR
:
5670 case TARGET_OBJECT_AUXV
:
5671 gdb_assert (annex
== NULL
);
5672 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
5673 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
5675 case TARGET_OBJECT_AVAILABLE_FEATURES
:
5676 return remote_read_qxfer
5677 (ops
, "features", annex
, readbuf
, offset
, len
,
5678 &remote_protocol_packets
[PACKET_qXfer_features
]);
5680 case TARGET_OBJECT_MEMORY_MAP
:
5681 gdb_assert (annex
== NULL
);
5682 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
5683 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
5689 /* Note: a zero OFFSET and LEN can be used to query the minimum
5691 if (offset
== 0 && len
== 0)
5692 return (get_remote_packet_size ());
5693 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
5694 large enough let the caller deal with it. */
5695 if (len
< get_remote_packet_size ())
5697 len
= get_remote_packet_size ();
5699 /* Except for querying the minimum buffer size, target must be open. */
5701 error (_("remote query is only available after target open"));
5703 gdb_assert (annex
!= NULL
);
5704 gdb_assert (readbuf
!= NULL
);
5710 /* We used one buffer char for the remote protocol q command and
5711 another for the query type. As the remote protocol encapsulation
5712 uses 4 chars plus one extra in case we are debugging
5713 (remote_debug), we have PBUFZIZ - 7 left to pack the query
5716 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
5718 /* Bad caller may have sent forbidden characters. */
5719 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
5724 gdb_assert (annex
[i
] == '\0');
5726 i
= putpkt (rs
->buf
);
5730 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5731 strcpy ((char *) readbuf
, rs
->buf
);
5733 return strlen ((char *) readbuf
);
5737 remote_rcmd (char *command
,
5738 struct ui_file
*outbuf
)
5740 struct remote_state
*rs
= get_remote_state ();
5744 error (_("remote rcmd is only available after target open"));
5746 /* Send a NULL command across as an empty command. */
5747 if (command
== NULL
)
5750 /* The query prefix. */
5751 strcpy (rs
->buf
, "qRcmd,");
5752 p
= strchr (rs
->buf
, '\0');
5754 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/) > get_remote_packet_size ())
5755 error (_("\"monitor\" command ``%s'' is too long."), command
);
5757 /* Encode the actual command. */
5758 bin2hex ((gdb_byte
*) command
, p
, 0);
5760 if (putpkt (rs
->buf
) < 0)
5761 error (_("Communication problem with target."));
5763 /* get/display the response */
5768 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
5770 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5773 error (_("Target does not support this command."));
5774 if (buf
[0] == 'O' && buf
[1] != 'K')
5776 remote_console_output (buf
+ 1); /* 'O' message from stub. */
5779 if (strcmp (buf
, "OK") == 0)
5781 if (strlen (buf
) == 3 && buf
[0] == 'E'
5782 && isdigit (buf
[1]) && isdigit (buf
[2]))
5784 error (_("Protocol error with Rcmd"));
5786 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
5788 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
5789 fputc_unfiltered (c
, outbuf
);
5795 static VEC(mem_region_s
) *
5796 remote_memory_map (struct target_ops
*ops
)
5798 VEC(mem_region_s
) *result
= NULL
;
5799 char *text
= target_read_stralloc (¤t_target
,
5800 TARGET_OBJECT_MEMORY_MAP
, NULL
);
5804 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
5805 result
= parse_memory_map (text
);
5806 do_cleanups (back_to
);
5813 packet_command (char *args
, int from_tty
)
5815 struct remote_state
*rs
= get_remote_state ();
5818 error (_("command can only be used with remote target"));
5821 error (_("remote-packet command requires packet text as argument"));
5823 puts_filtered ("sending: ");
5824 print_packet (args
);
5825 puts_filtered ("\n");
5828 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5829 puts_filtered ("received: ");
5830 print_packet (rs
->buf
);
5831 puts_filtered ("\n");
5835 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
5837 static void display_thread_info (struct gdb_ext_thread_info
*info
);
5839 static void threadset_test_cmd (char *cmd
, int tty
);
5841 static void threadalive_test (char *cmd
, int tty
);
5843 static void threadlist_test_cmd (char *cmd
, int tty
);
5845 int get_and_display_threadinfo (threadref
*ref
);
5847 static void threadinfo_test_cmd (char *cmd
, int tty
);
5849 static int thread_display_step (threadref
*ref
, void *context
);
5851 static void threadlist_update_test_cmd (char *cmd
, int tty
);
5853 static void init_remote_threadtests (void);
5855 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
5858 threadset_test_cmd (char *cmd
, int tty
)
5860 int sample_thread
= SAMPLE_THREAD
;
5862 printf_filtered (_("Remote threadset test\n"));
5863 set_thread (sample_thread
, 1);
5868 threadalive_test (char *cmd
, int tty
)
5870 int sample_thread
= SAMPLE_THREAD
;
5872 if (remote_thread_alive (pid_to_ptid (sample_thread
)))
5873 printf_filtered ("PASS: Thread alive test\n");
5875 printf_filtered ("FAIL: Thread alive test\n");
5878 void output_threadid (char *title
, threadref
*ref
);
5881 output_threadid (char *title
, threadref
*ref
)
5885 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
5887 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
5891 threadlist_test_cmd (char *cmd
, int tty
)
5894 threadref nextthread
;
5895 int done
, result_count
;
5896 threadref threadlist
[3];
5898 printf_filtered ("Remote Threadlist test\n");
5899 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
5900 &result_count
, &threadlist
[0]))
5901 printf_filtered ("FAIL: threadlist test\n");
5904 threadref
*scan
= threadlist
;
5905 threadref
*limit
= scan
+ result_count
;
5907 while (scan
< limit
)
5908 output_threadid (" thread ", scan
++);
5913 display_thread_info (struct gdb_ext_thread_info
*info
)
5915 output_threadid ("Threadid: ", &info
->threadid
);
5916 printf_filtered ("Name: %s\n ", info
->shortname
);
5917 printf_filtered ("State: %s\n", info
->display
);
5918 printf_filtered ("other: %s\n\n", info
->more_display
);
5922 get_and_display_threadinfo (threadref
*ref
)
5926 struct gdb_ext_thread_info threadinfo
;
5928 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
5929 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
5930 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
5931 display_thread_info (&threadinfo
);
5936 threadinfo_test_cmd (char *cmd
, int tty
)
5938 int athread
= SAMPLE_THREAD
;
5942 int_to_threadref (&thread
, athread
);
5943 printf_filtered ("Remote Threadinfo test\n");
5944 if (!get_and_display_threadinfo (&thread
))
5945 printf_filtered ("FAIL cannot get thread info\n");
5949 thread_display_step (threadref
*ref
, void *context
)
5951 /* output_threadid(" threadstep ",ref); *//* simple test */
5952 return get_and_display_threadinfo (ref
);
5956 threadlist_update_test_cmd (char *cmd
, int tty
)
5958 printf_filtered ("Remote Threadlist update test\n");
5959 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
5963 init_remote_threadtests (void)
5965 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
5966 Fetch and print the remote list of thread identifiers, one pkt only"));
5967 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
5968 _("Fetch and display info about one thread"));
5969 add_com ("tset", class_obscure
, threadset_test_cmd
,
5970 _("Test setting to a different thread"));
5971 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
5972 _("Iterate through updating all remote thread info"));
5973 add_com ("talive", class_obscure
, threadalive_test
,
5974 _(" Remote thread alive test "));
5979 /* Convert a thread ID to a string. Returns the string in a static
5983 remote_pid_to_str (ptid_t ptid
)
5985 static char buf
[32];
5987 xsnprintf (buf
, sizeof buf
, "Thread %d", ptid_get_pid (ptid
));
5991 /* Get the address of the thread local variable in OBJFILE which is
5992 stored at OFFSET within the thread local storage for thread PTID. */
5995 remote_get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
5997 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
5999 struct remote_state
*rs
= get_remote_state ();
6001 enum packet_result result
;
6003 strcpy (p
, "qGetTLSAddr:");
6005 p
+= hexnumstr (p
, PIDGET (ptid
));
6007 p
+= hexnumstr (p
, offset
);
6009 p
+= hexnumstr (p
, lm
);
6013 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6014 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
6015 if (result
== PACKET_OK
)
6019 unpack_varlen_hex (rs
->buf
, &result
);
6022 else if (result
== PACKET_UNKNOWN
)
6023 throw_error (TLS_GENERIC_ERROR
,
6024 _("Remote target doesn't support qGetTLSAddr packet"));
6026 throw_error (TLS_GENERIC_ERROR
,
6027 _("Remote target failed to process qGetTLSAddr request"));
6030 throw_error (TLS_GENERIC_ERROR
,
6031 _("TLS not supported or disabled on this target"));
6036 /* Support for inferring a target description based on the current
6037 architecture and the size of a 'g' packet. While the 'g' packet
6038 can have any size (since optional registers can be left off the
6039 end), some sizes are easily recognizable given knowledge of the
6040 approximate architecture. */
6042 struct remote_g_packet_guess
6045 const struct target_desc
*tdesc
;
6047 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
6048 DEF_VEC_O(remote_g_packet_guess_s
);
6050 struct remote_g_packet_data
6052 VEC(remote_g_packet_guess_s
) *guesses
;
6055 static struct gdbarch_data
*remote_g_packet_data_handle
;
6058 remote_g_packet_data_init (struct obstack
*obstack
)
6060 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
6064 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
6065 const struct target_desc
*tdesc
)
6067 struct remote_g_packet_data
*data
6068 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
6069 struct remote_g_packet_guess new_guess
, *guess
;
6072 gdb_assert (tdesc
!= NULL
);
6075 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
6077 if (guess
->bytes
== bytes
)
6078 internal_error (__FILE__
, __LINE__
,
6079 "Duplicate g packet description added for size %d",
6082 new_guess
.bytes
= bytes
;
6083 new_guess
.tdesc
= tdesc
;
6084 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
6087 static const struct target_desc
*
6088 remote_read_description (struct target_ops
*target
)
6090 struct remote_g_packet_data
*data
6091 = gdbarch_data (current_gdbarch
, remote_g_packet_data_handle
);
6093 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
6095 struct remote_g_packet_guess
*guess
;
6097 int bytes
= send_g_packet ();
6100 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
6102 if (guess
->bytes
== bytes
)
6103 return guess
->tdesc
;
6105 /* We discard the g packet. A minor optimization would be to
6106 hold on to it, and fill the register cache once we have selected
6107 an architecture, but it's too tricky to do safely. */
6114 init_remote_ops (void)
6116 remote_ops
.to_shortname
= "remote";
6117 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
6119 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
6120 Specify the serial device it is connected to\n\
6121 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
6122 remote_ops
.to_open
= remote_open
;
6123 remote_ops
.to_close
= remote_close
;
6124 remote_ops
.to_detach
= remote_detach
;
6125 remote_ops
.to_disconnect
= remote_disconnect
;
6126 remote_ops
.to_resume
= remote_resume
;
6127 remote_ops
.to_wait
= remote_wait
;
6128 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
6129 remote_ops
.to_store_registers
= remote_store_registers
;
6130 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
6131 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
6132 remote_ops
.to_files_info
= remote_files_info
;
6133 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
6134 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
6135 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
6136 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
6137 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
6138 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
6139 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
6140 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
6141 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
6142 remote_ops
.to_kill
= remote_kill
;
6143 remote_ops
.to_load
= generic_load
;
6144 remote_ops
.to_mourn_inferior
= remote_mourn
;
6145 remote_ops
.to_thread_alive
= remote_thread_alive
;
6146 remote_ops
.to_find_new_threads
= remote_threads_info
;
6147 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
6148 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
6149 remote_ops
.to_stop
= remote_stop
;
6150 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
6151 remote_ops
.to_rcmd
= remote_rcmd
;
6152 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
6153 remote_ops
.to_stratum
= process_stratum
;
6154 remote_ops
.to_has_all_memory
= 1;
6155 remote_ops
.to_has_memory
= 1;
6156 remote_ops
.to_has_stack
= 1;
6157 remote_ops
.to_has_registers
= 1;
6158 remote_ops
.to_has_execution
= 1;
6159 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
6160 remote_ops
.to_magic
= OPS_MAGIC
;
6161 remote_ops
.to_memory_map
= remote_memory_map
;
6162 remote_ops
.to_flash_erase
= remote_flash_erase
;
6163 remote_ops
.to_flash_done
= remote_flash_done
;
6164 remote_ops
.to_read_description
= remote_read_description
;
6167 /* Set up the extended remote vector by making a copy of the standard
6168 remote vector and adding to it. */
6171 init_extended_remote_ops (void)
6173 extended_remote_ops
= remote_ops
;
6175 extended_remote_ops
.to_shortname
= "extended-remote";
6176 extended_remote_ops
.to_longname
=
6177 "Extended remote serial target in gdb-specific protocol";
6178 extended_remote_ops
.to_doc
=
6179 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
6180 Specify the serial device it is connected to (e.g. /dev/ttya).",
6181 extended_remote_ops
.to_open
= extended_remote_open
;
6182 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
6183 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
6187 remote_can_async_p (void)
6189 /* We're async whenever the serial device is. */
6190 return (current_target
.to_async_mask_value
) && serial_can_async_p (remote_desc
);
6194 remote_is_async_p (void)
6196 /* We're async whenever the serial device is. */
6197 return (current_target
.to_async_mask_value
) && serial_is_async_p (remote_desc
);
6200 /* Pass the SERIAL event on and up to the client. One day this code
6201 will be able to delay notifying the client of an event until the
6202 point where an entire packet has been received. */
6204 static void (*async_client_callback
) (enum inferior_event_type event_type
,
6206 static void *async_client_context
;
6207 static serial_event_ftype remote_async_serial_handler
;
6210 remote_async_serial_handler (struct serial
*scb
, void *context
)
6212 /* Don't propogate error information up to the client. Instead let
6213 the client find out about the error by querying the target. */
6214 async_client_callback (INF_REG_EVENT
, async_client_context
);
6218 remote_async (void (*callback
) (enum inferior_event_type event_type
,
6219 void *context
), void *context
)
6221 if (current_target
.to_async_mask_value
== 0)
6222 internal_error (__FILE__
, __LINE__
,
6223 _("Calling remote_async when async is masked"));
6225 if (callback
!= NULL
)
6227 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
6228 async_client_callback
= callback
;
6229 async_client_context
= context
;
6232 serial_async (remote_desc
, NULL
, NULL
);
6235 /* Target async and target extended-async.
6237 This are temporary targets, until it is all tested. Eventually
6238 async support will be incorporated int the usual 'remote'
6242 init_remote_async_ops (void)
6244 remote_async_ops
.to_shortname
= "async";
6245 remote_async_ops
.to_longname
=
6246 "Remote serial target in async version of the gdb-specific protocol";
6247 remote_async_ops
.to_doc
=
6248 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
6249 Specify the serial device it is connected to (e.g. /dev/ttya).";
6250 remote_async_ops
.to_open
= remote_async_open
;
6251 remote_async_ops
.to_close
= remote_close
;
6252 remote_async_ops
.to_detach
= remote_detach
;
6253 remote_async_ops
.to_disconnect
= remote_disconnect
;
6254 remote_async_ops
.to_resume
= remote_async_resume
;
6255 remote_async_ops
.to_wait
= remote_async_wait
;
6256 remote_async_ops
.to_fetch_registers
= remote_fetch_registers
;
6257 remote_async_ops
.to_store_registers
= remote_store_registers
;
6258 remote_async_ops
.to_prepare_to_store
= remote_prepare_to_store
;
6259 remote_async_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
6260 remote_async_ops
.to_files_info
= remote_files_info
;
6261 remote_async_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
6262 remote_async_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
6263 remote_async_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
6264 remote_async_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
6265 remote_async_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
6266 remote_async_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
6267 remote_async_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
6268 remote_async_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
6269 remote_async_ops
.to_stopped_data_address
= remote_stopped_data_address
;
6270 remote_async_ops
.to_terminal_inferior
= remote_async_terminal_inferior
;
6271 remote_async_ops
.to_terminal_ours
= remote_async_terminal_ours
;
6272 remote_async_ops
.to_kill
= remote_async_kill
;
6273 remote_async_ops
.to_load
= generic_load
;
6274 remote_async_ops
.to_mourn_inferior
= remote_async_mourn
;
6275 remote_async_ops
.to_thread_alive
= remote_thread_alive
;
6276 remote_async_ops
.to_find_new_threads
= remote_threads_info
;
6277 remote_async_ops
.to_pid_to_str
= remote_pid_to_str
;
6278 remote_async_ops
.to_extra_thread_info
= remote_threads_extra_info
;
6279 remote_async_ops
.to_stop
= remote_stop
;
6280 remote_async_ops
.to_xfer_partial
= remote_xfer_partial
;
6281 remote_async_ops
.to_rcmd
= remote_rcmd
;
6282 remote_async_ops
.to_stratum
= process_stratum
;
6283 remote_async_ops
.to_has_all_memory
= 1;
6284 remote_async_ops
.to_has_memory
= 1;
6285 remote_async_ops
.to_has_stack
= 1;
6286 remote_async_ops
.to_has_registers
= 1;
6287 remote_async_ops
.to_has_execution
= 1;
6288 remote_async_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
6289 remote_async_ops
.to_can_async_p
= remote_can_async_p
;
6290 remote_async_ops
.to_is_async_p
= remote_is_async_p
;
6291 remote_async_ops
.to_async
= remote_async
;
6292 remote_async_ops
.to_async_mask_value
= 1;
6293 remote_async_ops
.to_magic
= OPS_MAGIC
;
6294 remote_async_ops
.to_memory_map
= remote_memory_map
;
6295 remote_async_ops
.to_flash_erase
= remote_flash_erase
;
6296 remote_async_ops
.to_flash_done
= remote_flash_done
;
6297 remote_ops
.to_read_description
= remote_read_description
;
6300 /* Set up the async extended remote vector by making a copy of the standard
6301 remote vector and adding to it. */
6304 init_extended_async_remote_ops (void)
6306 extended_async_remote_ops
= remote_async_ops
;
6308 extended_async_remote_ops
.to_shortname
= "extended-async";
6309 extended_async_remote_ops
.to_longname
=
6310 "Extended remote serial target in async gdb-specific protocol";
6311 extended_async_remote_ops
.to_doc
=
6312 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
6313 Specify the serial device it is connected to (e.g. /dev/ttya).",
6314 extended_async_remote_ops
.to_open
= extended_remote_async_open
;
6315 extended_async_remote_ops
.to_create_inferior
= extended_remote_async_create_inferior
;
6316 extended_async_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
6320 set_remote_cmd (char *args
, int from_tty
)
6322 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
6326 show_remote_cmd (char *args
, int from_tty
)
6328 /* We can't just use cmd_show_list here, because we want to skip
6329 the redundant "show remote Z-packet" and the legacy aliases. */
6330 struct cleanup
*showlist_chain
;
6331 struct cmd_list_element
*list
= remote_show_cmdlist
;
6333 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
6334 for (; list
!= NULL
; list
= list
->next
)
6335 if (strcmp (list
->name
, "Z-packet") == 0)
6337 else if (list
->type
== not_set_cmd
)
6338 /* Alias commands are exactly like the original, except they
6339 don't have the normal type. */
6343 struct cleanup
*option_chain
6344 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
6345 ui_out_field_string (uiout
, "name", list
->name
);
6346 ui_out_text (uiout
, ": ");
6347 if (list
->type
== show_cmd
)
6348 do_setshow_command ((char *) NULL
, from_tty
, list
);
6350 cmd_func (list
, NULL
, from_tty
);
6351 /* Close the tuple. */
6352 do_cleanups (option_chain
);
6355 /* Close the tuple. */
6356 do_cleanups (showlist_chain
);
6360 build_remote_gdbarch_data (void)
6362 remote_address_size
= TARGET_ADDR_BIT
;
6365 /* Saved pointer to previous owner of the new_objfile event. */
6366 static void (*remote_new_objfile_chain
) (struct objfile
*);
6368 /* Function to be called whenever a new objfile (shlib) is detected. */
6370 remote_new_objfile (struct objfile
*objfile
)
6372 if (remote_desc
!= 0) /* Have a remote connection. */
6374 remote_check_symbols (objfile
);
6376 /* Call predecessor on chain, if any. */
6377 if (remote_new_objfile_chain
)
6378 remote_new_objfile_chain (objfile
);
6382 _initialize_remote (void)
6384 struct remote_state
*rs
;
6386 /* architecture specific data */
6387 remote_gdbarch_data_handle
=
6388 gdbarch_data_register_post_init (init_remote_state
);
6389 remote_g_packet_data_handle
=
6390 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
6392 /* Old tacky stuff. NOTE: This comes after the remote protocol so
6393 that the remote protocol has been initialized. */
6394 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size
);
6395 deprecated_register_gdbarch_swap (NULL
, 0, build_remote_gdbarch_data
);
6397 /* Initialize the per-target state. At the moment there is only one
6398 of these, not one per target. Only one target is active at a
6399 time. The default buffer size is unimportant; it will be expanded
6400 whenever a larger buffer is needed. */
6401 rs
= get_remote_state_raw ();
6403 rs
->buf
= xmalloc (rs
->buf_size
);
6406 add_target (&remote_ops
);
6408 init_extended_remote_ops ();
6409 add_target (&extended_remote_ops
);
6411 init_remote_async_ops ();
6412 add_target (&remote_async_ops
);
6414 init_extended_async_remote_ops ();
6415 add_target (&extended_async_remote_ops
);
6417 /* Hook into new objfile notification. */
6418 remote_new_objfile_chain
= deprecated_target_new_objfile_hook
;
6419 deprecated_target_new_objfile_hook
= remote_new_objfile
;
6422 init_remote_threadtests ();
6425 /* set/show remote ... */
6427 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
6428 Remote protocol specific variables\n\
6429 Configure various remote-protocol specific variables such as\n\
6430 the packets being used"),
6431 &remote_set_cmdlist
, "set remote ",
6432 0 /* allow-unknown */, &setlist
);
6433 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
6434 Remote protocol specific variables\n\
6435 Configure various remote-protocol specific variables such as\n\
6436 the packets being used"),
6437 &remote_show_cmdlist
, "show remote ",
6438 0 /* allow-unknown */, &showlist
);
6440 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
6441 Compare section data on target to the exec file.\n\
6442 Argument is a single section name (default: all loaded sections)."),
6445 add_cmd ("packet", class_maintenance
, packet_command
, _("\
6446 Send an arbitrary packet to a remote target.\n\
6447 maintenance packet TEXT\n\
6448 If GDB is talking to an inferior via the GDB serial protocol, then\n\
6449 this command sends the string TEXT to the inferior, and displays the\n\
6450 response packet. GDB supplies the initial `$' character, and the\n\
6451 terminating `#' character and checksum."),
6454 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
6455 Set whether to send break if interrupted."), _("\
6456 Show whether to send break if interrupted."), _("\
6457 If set, a break, instead of a cntrl-c, is sent to the remote target."),
6458 NULL
, NULL
, /* FIXME: i18n: Whether to send break if interrupted is %s. */
6459 &setlist
, &showlist
);
6461 /* Install commands for configuring memory read/write packets. */
6463 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
6464 Set the maximum number of bytes per memory write packet (deprecated)."),
6466 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
6467 Show the maximum number of bytes per memory write packet (deprecated)."),
6469 add_cmd ("memory-write-packet-size", no_class
,
6470 set_memory_write_packet_size
, _("\
6471 Set the maximum number of bytes per memory-write packet.\n\
6472 Specify the number of bytes in a packet or 0 (zero) for the\n\
6473 default packet size. The actual limit is further reduced\n\
6474 dependent on the target. Specify ``fixed'' to disable the\n\
6475 further restriction and ``limit'' to enable that restriction."),
6476 &remote_set_cmdlist
);
6477 add_cmd ("memory-read-packet-size", no_class
,
6478 set_memory_read_packet_size
, _("\
6479 Set the maximum number of bytes per memory-read packet.\n\
6480 Specify the number of bytes in a packet or 0 (zero) for the\n\
6481 default packet size. The actual limit is further reduced\n\
6482 dependent on the target. Specify ``fixed'' to disable the\n\
6483 further restriction and ``limit'' to enable that restriction."),
6484 &remote_set_cmdlist
);
6485 add_cmd ("memory-write-packet-size", no_class
,
6486 show_memory_write_packet_size
,
6487 _("Show the maximum number of bytes per memory-write packet."),
6488 &remote_show_cmdlist
);
6489 add_cmd ("memory-read-packet-size", no_class
,
6490 show_memory_read_packet_size
,
6491 _("Show the maximum number of bytes per memory-read packet."),
6492 &remote_show_cmdlist
);
6494 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
6495 &remote_hw_watchpoint_limit
, _("\
6496 Set the maximum number of target hardware watchpoints."), _("\
6497 Show the maximum number of target hardware watchpoints."), _("\
6498 Specify a negative limit for unlimited."),
6499 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
6500 &remote_set_cmdlist
, &remote_show_cmdlist
);
6501 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
6502 &remote_hw_breakpoint_limit
, _("\
6503 Set the maximum number of target hardware breakpoints."), _("\
6504 Show the maximum number of target hardware breakpoints."), _("\
6505 Specify a negative limit for unlimited."),
6506 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
6507 &remote_set_cmdlist
, &remote_show_cmdlist
);
6509 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
6510 &remote_address_size
, _("\
6511 Set the maximum size of the address (in bits) in a memory packet."), _("\
6512 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
6514 NULL
, /* FIXME: i18n: */
6515 &setlist
, &showlist
);
6517 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
6518 "X", "binary-download", 1);
6520 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
6521 "vCont", "verbose-resume", 0);
6523 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
6524 "QPassSignals", "pass-signals", 0);
6526 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
6527 "qSymbol", "symbol-lookup", 0);
6529 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
6530 "P", "set-register", 1);
6532 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
6533 "p", "fetch-register", 1);
6535 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
6536 "Z0", "software-breakpoint", 0);
6538 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
6539 "Z1", "hardware-breakpoint", 0);
6541 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
6542 "Z2", "write-watchpoint", 0);
6544 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
6545 "Z3", "read-watchpoint", 0);
6547 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
6548 "Z4", "access-watchpoint", 0);
6550 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
6551 "qXfer:auxv:read", "read-aux-vector", 0);
6553 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
6554 "qXfer:features:read", "target-features", 0);
6556 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
6557 "qXfer:memory-map:read", "memory-map", 0);
6559 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
6560 "qGetTLSAddr", "get-thread-local-storage-address",
6563 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
6564 "qSupported", "supported-packets", 0);
6566 /* Keep the old ``set remote Z-packet ...'' working. Each individual
6567 Z sub-packet has its own set and show commands, but users may
6568 have sets to this variable in their .gdbinit files (or in their
6570 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
6571 &remote_Z_packet_detect
, _("\
6572 Set use of remote protocol `Z' packets"), _("\
6573 Show use of remote protocol `Z' packets "), _("\
6574 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
6576 set_remote_protocol_Z_packet_cmd
,
6577 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
6578 &remote_set_cmdlist
, &remote_show_cmdlist
);
6580 /* Eventually initialize fileio. See fileio.c */
6581 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);