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, 2008
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 3 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, see <http://www.gnu.org/licenses/>. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
31 #include "exceptions.h"
33 /*#include "terminal.h" */
36 #include "gdb-stabs.h"
37 #include "gdbthread.h"
41 #include "gdb_assert.h"
44 #include "cli/cli-decode.h"
45 #include "cli/cli-setshow.h"
46 #include "target-descriptions.h"
51 #include "event-loop.h"
52 #include "event-top.h"
58 #include "gdbcore.h" /* for exec_bfd */
60 #include "remote-fileio.h"
61 #include "gdb/fileio.h"
63 #include "memory-map.h"
65 /* The size to align memory write packets, when practical. The protocol
66 does not guarantee any alignment, and gdb will generate short
67 writes and unaligned writes, but even as a best-effort attempt this
68 can improve bulk transfers. For instance, if a write is misaligned
69 relative to the target's data bus, the stub may need to make an extra
70 round trip fetching data from the target. This doesn't make a
71 huge difference, but it's easy to do, so we try to be helpful.
73 The alignment chosen is arbitrary; usually data bus width is
74 important here, not the possibly larger cache line size. */
75 enum { REMOTE_ALIGN_WRITES
= 16 };
77 /* Prototypes for local functions. */
78 static void cleanup_sigint_signal_handler (void *dummy
);
79 static void initialize_sigint_signal_handler (void);
80 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
82 static void handle_remote_sigint (int);
83 static void handle_remote_sigint_twice (int);
84 static void async_remote_interrupt (gdb_client_data
);
85 void async_remote_interrupt_twice (gdb_client_data
);
87 static void remote_files_info (struct target_ops
*ignore
);
89 static void remote_prepare_to_store (struct regcache
*regcache
);
91 static void remote_fetch_registers (struct regcache
*regcache
, int regno
);
93 static void remote_resume (ptid_t ptid
, int step
,
94 enum target_signal siggnal
);
95 static void remote_open (char *name
, int from_tty
);
97 static void extended_remote_open (char *name
, int from_tty
);
99 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
);
101 static void remote_close (int quitting
);
103 static void remote_store_registers (struct regcache
*regcache
, int regno
);
105 static void remote_mourn (void);
107 static void extended_remote_restart (void);
109 static void extended_remote_mourn (void);
111 static void remote_mourn_1 (struct target_ops
*);
113 static void remote_send (char **buf
, long *sizeof_buf_p
);
115 static int readchar (int timeout
);
117 static ptid_t
remote_wait (ptid_t ptid
,
118 struct target_waitstatus
*status
);
120 static void remote_kill (void);
122 static int tohex (int nib
);
124 static int remote_can_async_p (void);
126 static int remote_is_async_p (void);
128 static void remote_async (void (*callback
) (enum inferior_event_type event_type
,
129 void *context
), void *context
);
131 static int remote_async_mask (int new_mask
);
133 static void remote_detach (char *args
, int from_tty
);
135 static void remote_interrupt (int signo
);
137 static void remote_interrupt_twice (int signo
);
139 static void interrupt_query (void);
141 static void set_general_thread (struct ptid ptid
);
142 static void set_continue_thread (struct ptid ptid
);
144 static int remote_thread_alive (ptid_t
);
146 static void get_offsets (void);
148 static void skip_frame (void);
150 static long read_frame (char **buf_p
, long *sizeof_buf
);
152 static int hexnumlen (ULONGEST num
);
154 static void init_remote_ops (void);
156 static void init_extended_remote_ops (void);
158 static void remote_stop (void);
160 static int ishex (int ch
, int *val
);
162 static int stubhex (int ch
);
164 static int hexnumstr (char *, ULONGEST
);
166 static int hexnumnstr (char *, ULONGEST
, int);
168 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
170 static void print_packet (char *);
172 static unsigned long crc32 (unsigned char *, int, unsigned int);
174 static void compare_sections_command (char *, int);
176 static void packet_command (char *, int);
178 static int stub_unpack_int (char *buff
, int fieldlength
);
180 static ptid_t
remote_current_thread (ptid_t oldptid
);
182 static void remote_find_new_threads (void);
184 static void record_currthread (ptid_t currthread
);
186 static int fromhex (int a
);
188 static int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
190 static int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
192 static int putpkt_binary (char *buf
, int cnt
);
194 static void check_binary_download (CORE_ADDR addr
);
196 struct packet_config
;
198 static void show_packet_config_cmd (struct packet_config
*config
);
200 static void update_packet_config (struct packet_config
*config
);
202 static void set_remote_protocol_packet_cmd (char *args
, int from_tty
,
203 struct cmd_list_element
*c
);
205 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
207 struct cmd_list_element
*c
,
210 void _initialize_remote (void);
212 /* Controls if async mode is permitted. */
213 static int remote_async_permitted
= 0;
215 static int remote_async_permitted_set
= 0;
218 set_maintenance_remote_async_permitted (char *args
, int from_tty
,
219 struct cmd_list_element
*c
)
221 if (target_has_execution
)
223 remote_async_permitted_set
= remote_async_permitted
; /* revert */
224 error (_("Cannot change this setting while the inferior is running."));
227 remote_async_permitted
= remote_async_permitted_set
;
231 show_maintenance_remote_async_permitted (struct ui_file
*file
, int from_tty
,
232 struct cmd_list_element
*c
, const char *value
)
234 fprintf_filtered (file
, _("\
235 Controlling the remote inferior in asynchronous mode is %s.\n"),
241 static struct cmd_list_element
*remote_cmdlist
;
243 /* For "set remote" and "show remote". */
245 static struct cmd_list_element
*remote_set_cmdlist
;
246 static struct cmd_list_element
*remote_show_cmdlist
;
248 /* Description of the remote protocol state for the currently
249 connected target. This is per-target state, and independent of the
250 selected architecture. */
254 /* A buffer to use for incoming packets, and its current size. The
255 buffer is grown dynamically for larger incoming packets.
256 Outgoing packets may also be constructed in this buffer.
257 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
258 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
263 /* If we negotiated packet size explicitly (and thus can bypass
264 heuristics for the largest packet size that will not overflow
265 a buffer in the stub), this will be set to that packet size.
266 Otherwise zero, meaning to use the guessed size. */
267 long explicit_packet_size
;
269 /* remote_wait is normally called when the target is running and
270 waits for a stop reply packet. But sometimes we need to call it
271 when the target is already stopped. We can send a "?" packet
272 and have remote_wait read the response. Or, if we already have
273 the response, we can stash it in BUF and tell remote_wait to
274 skip calling getpkt. This flag is set when BUF contains a
275 stop reply packet and the target is not waiting. */
276 int cached_wait_status
;
279 /* This data could be associated with a target, but we do not always
280 have access to the current target when we need it, so for now it is
281 static. This will be fine for as long as only one target is in use
283 static struct remote_state remote_state
;
285 static struct remote_state
*
286 get_remote_state_raw (void)
288 return &remote_state
;
291 /* Description of the remote protocol for a given architecture. */
295 long offset
; /* Offset into G packet. */
296 long regnum
; /* GDB's internal register number. */
297 LONGEST pnum
; /* Remote protocol register number. */
298 int in_g_packet
; /* Always part of G packet. */
299 /* long size in bytes; == register_size (current_gdbarch, regnum);
301 /* char *name; == gdbarch_register_name (current_gdbarch, regnum);
305 struct remote_arch_state
307 /* Description of the remote protocol registers. */
308 long sizeof_g_packet
;
310 /* Description of the remote protocol registers indexed by REGNUM
311 (making an array gdbarch_num_regs in size). */
312 struct packet_reg
*regs
;
314 /* This is the size (in chars) of the first response to the ``g''
315 packet. It is used as a heuristic when determining the maximum
316 size of memory-read and memory-write packets. A target will
317 typically only reserve a buffer large enough to hold the ``g''
318 packet. The size does not include packet overhead (headers and
320 long actual_register_packet_size
;
322 /* This is the maximum size (in chars) of a non read/write packet.
323 It is also used as a cap on the size of read/write packets. */
324 long remote_packet_size
;
328 /* Handle for retreving the remote protocol data from gdbarch. */
329 static struct gdbarch_data
*remote_gdbarch_data_handle
;
331 static struct remote_arch_state
*
332 get_remote_arch_state (void)
334 return gdbarch_data (current_gdbarch
, remote_gdbarch_data_handle
);
337 /* Fetch the global remote target state. */
339 static struct remote_state
*
340 get_remote_state (void)
342 /* Make sure that the remote architecture state has been
343 initialized, because doing so might reallocate rs->buf. Any
344 function which calls getpkt also needs to be mindful of changes
345 to rs->buf, but this call limits the number of places which run
347 get_remote_arch_state ();
349 return get_remote_state_raw ();
353 compare_pnums (const void *lhs_
, const void *rhs_
)
355 const struct packet_reg
* const *lhs
= lhs_
;
356 const struct packet_reg
* const *rhs
= rhs_
;
358 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
360 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
367 init_remote_state (struct gdbarch
*gdbarch
)
369 int regnum
, num_remote_regs
, offset
;
370 struct remote_state
*rs
= get_remote_state_raw ();
371 struct remote_arch_state
*rsa
;
372 struct packet_reg
**remote_regs
;
374 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
376 /* Use the architecture to build a regnum<->pnum table, which will be
377 1:1 unless a feature set specifies otherwise. */
378 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
,
379 gdbarch_num_regs (gdbarch
),
381 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
383 struct packet_reg
*r
= &rsa
->regs
[regnum
];
385 if (register_size (gdbarch
, regnum
) == 0)
386 /* Do not try to fetch zero-sized (placeholder) registers. */
389 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
394 /* Define the g/G packet format as the contents of each register
395 with a remote protocol number, in order of ascending protocol
398 remote_regs
= alloca (gdbarch_num_regs (gdbarch
)
399 * sizeof (struct packet_reg
*));
400 for (num_remote_regs
= 0, regnum
= 0;
401 regnum
< gdbarch_num_regs (gdbarch
);
403 if (rsa
->regs
[regnum
].pnum
!= -1)
404 remote_regs
[num_remote_regs
++] = &rsa
->regs
[regnum
];
406 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
409 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
411 remote_regs
[regnum
]->in_g_packet
= 1;
412 remote_regs
[regnum
]->offset
= offset
;
413 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
416 /* Record the maximum possible size of the g packet - it may turn out
418 rsa
->sizeof_g_packet
= offset
;
420 /* Default maximum number of characters in a packet body. Many
421 remote stubs have a hardwired buffer size of 400 bytes
422 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
423 as the maximum packet-size to ensure that the packet and an extra
424 NUL character can always fit in the buffer. This stops GDB
425 trashing stubs that try to squeeze an extra NUL into what is
426 already a full buffer (As of 1999-12-04 that was most stubs). */
427 rsa
->remote_packet_size
= 400 - 1;
429 /* This one is filled in when a ``g'' packet is received. */
430 rsa
->actual_register_packet_size
= 0;
432 /* Should rsa->sizeof_g_packet needs more space than the
433 default, adjust the size accordingly. Remember that each byte is
434 encoded as two characters. 32 is the overhead for the packet
435 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
436 (``$NN:G...#NN'') is a better guess, the below has been padded a
438 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
439 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
441 /* Make sure that the packet buffer is plenty big enough for
442 this architecture. */
443 if (rs
->buf_size
< rsa
->remote_packet_size
)
445 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
446 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
452 /* Return the current allowed size of a remote packet. This is
453 inferred from the current architecture, and should be used to
454 limit the length of outgoing packets. */
456 get_remote_packet_size (void)
458 struct remote_state
*rs
= get_remote_state ();
459 struct remote_arch_state
*rsa
= get_remote_arch_state ();
461 if (rs
->explicit_packet_size
)
462 return rs
->explicit_packet_size
;
464 return rsa
->remote_packet_size
;
467 static struct packet_reg
*
468 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
470 if (regnum
< 0 && regnum
>= gdbarch_num_regs (current_gdbarch
))
474 struct packet_reg
*r
= &rsa
->regs
[regnum
];
475 gdb_assert (r
->regnum
== regnum
);
480 static struct packet_reg
*
481 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
484 for (i
= 0; i
< gdbarch_num_regs (current_gdbarch
); i
++)
486 struct packet_reg
*r
= &rsa
->regs
[i
];
493 /* FIXME: graces/2002-08-08: These variables should eventually be
494 bound to an instance of the target object (as in gdbarch-tdep()),
495 when such a thing exists. */
497 /* This is set to the data address of the access causing the target
498 to stop for a watchpoint. */
499 static CORE_ADDR remote_watch_data_address
;
501 /* This is non-zero if target stopped for a watchpoint. */
502 static int remote_stopped_by_watchpoint_p
;
504 static struct target_ops remote_ops
;
506 static struct target_ops extended_remote_ops
;
508 static int remote_async_mask_value
= 1;
510 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
511 ``forever'' still use the normal timeout mechanism. This is
512 currently used by the ASYNC code to guarentee that target reads
513 during the initial connect always time-out. Once getpkt has been
514 modified to return a timeout indication and, in turn
515 remote_wait()/wait_for_inferior() have gained a timeout parameter
517 static int wait_forever_enabled_p
= 1;
520 /* This variable chooses whether to send a ^C or a break when the user
521 requests program interruption. Although ^C is usually what remote
522 systems expect, and that is the default here, sometimes a break is
523 preferable instead. */
525 static int remote_break
;
527 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
528 remote_open knows that we don't have a file open when the program
530 static struct serial
*remote_desc
= NULL
;
532 /* This variable sets the number of bits in an address that are to be
533 sent in a memory ("M" or "m") packet. Normally, after stripping
534 leading zeros, the entire address would be sent. This variable
535 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
536 initial implementation of remote.c restricted the address sent in
537 memory packets to ``host::sizeof long'' bytes - (typically 32
538 bits). Consequently, for 64 bit targets, the upper 32 bits of an
539 address was never sent. Since fixing this bug may cause a break in
540 some remote targets this variable is principly provided to
541 facilitate backward compatibility. */
543 static int remote_address_size
;
545 /* Temporary to track who currently owns the terminal. See
546 remote_terminal_* for more details. */
548 static int remote_async_terminal_ours_p
;
550 /* The executable file to use for "run" on the remote side. */
552 static char *remote_exec_file
= "";
555 /* User configurable variables for the number of characters in a
556 memory read/write packet. MIN (rsa->remote_packet_size,
557 rsa->sizeof_g_packet) is the default. Some targets need smaller
558 values (fifo overruns, et.al.) and some users need larger values
559 (speed up transfers). The variables ``preferred_*'' (the user
560 request), ``current_*'' (what was actually set) and ``forced_*''
561 (Positive - a soft limit, negative - a hard limit). */
563 struct memory_packet_config
570 /* Compute the current size of a read/write packet. Since this makes
571 use of ``actual_register_packet_size'' the computation is dynamic. */
574 get_memory_packet_size (struct memory_packet_config
*config
)
576 struct remote_state
*rs
= get_remote_state ();
577 struct remote_arch_state
*rsa
= get_remote_arch_state ();
579 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
580 law?) that some hosts don't cope very well with large alloca()
581 calls. Eventually the alloca() code will be replaced by calls to
582 xmalloc() and make_cleanups() allowing this restriction to either
583 be lifted or removed. */
584 #ifndef MAX_REMOTE_PACKET_SIZE
585 #define MAX_REMOTE_PACKET_SIZE 16384
587 /* NOTE: 20 ensures we can write at least one byte. */
588 #ifndef MIN_REMOTE_PACKET_SIZE
589 #define MIN_REMOTE_PACKET_SIZE 20
594 if (config
->size
<= 0)
595 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
597 what_they_get
= config
->size
;
601 what_they_get
= get_remote_packet_size ();
602 /* Limit the packet to the size specified by the user. */
604 && what_they_get
> config
->size
)
605 what_they_get
= config
->size
;
607 /* Limit it to the size of the targets ``g'' response unless we have
608 permission from the stub to use a larger packet size. */
609 if (rs
->explicit_packet_size
== 0
610 && rsa
->actual_register_packet_size
> 0
611 && what_they_get
> rsa
->actual_register_packet_size
)
612 what_they_get
= rsa
->actual_register_packet_size
;
614 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
615 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
616 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
617 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
619 /* Make sure there is room in the global buffer for this packet
620 (including its trailing NUL byte). */
621 if (rs
->buf_size
< what_they_get
+ 1)
623 rs
->buf_size
= 2 * what_they_get
;
624 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
627 return what_they_get
;
630 /* Update the size of a read/write packet. If they user wants
631 something really big then do a sanity check. */
634 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
636 int fixed_p
= config
->fixed_p
;
637 long size
= config
->size
;
639 error (_("Argument required (integer, `fixed' or `limited')."));
640 else if (strcmp (args
, "hard") == 0
641 || strcmp (args
, "fixed") == 0)
643 else if (strcmp (args
, "soft") == 0
644 || strcmp (args
, "limit") == 0)
649 size
= strtoul (args
, &end
, 0);
651 error (_("Invalid %s (bad syntax)."), config
->name
);
653 /* Instead of explicitly capping the size of a packet to
654 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
655 instead allowed to set the size to something arbitrarily
657 if (size
> MAX_REMOTE_PACKET_SIZE
)
658 error (_("Invalid %s (too large)."), config
->name
);
662 if (fixed_p
&& !config
->fixed_p
)
664 if (! query (_("The target may not be able to correctly handle a %s\n"
665 "of %ld bytes. Change the packet size? "),
667 error (_("Packet size not changed."));
669 /* Update the config. */
670 config
->fixed_p
= fixed_p
;
675 show_memory_packet_size (struct memory_packet_config
*config
)
677 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
679 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
680 get_memory_packet_size (config
));
682 printf_filtered (_("Packets are limited to %ld bytes.\n"),
683 get_memory_packet_size (config
));
686 static struct memory_packet_config memory_write_packet_config
=
688 "memory-write-packet-size",
692 set_memory_write_packet_size (char *args
, int from_tty
)
694 set_memory_packet_size (args
, &memory_write_packet_config
);
698 show_memory_write_packet_size (char *args
, int from_tty
)
700 show_memory_packet_size (&memory_write_packet_config
);
704 get_memory_write_packet_size (void)
706 return get_memory_packet_size (&memory_write_packet_config
);
709 static struct memory_packet_config memory_read_packet_config
=
711 "memory-read-packet-size",
715 set_memory_read_packet_size (char *args
, int from_tty
)
717 set_memory_packet_size (args
, &memory_read_packet_config
);
721 show_memory_read_packet_size (char *args
, int from_tty
)
723 show_memory_packet_size (&memory_read_packet_config
);
727 get_memory_read_packet_size (void)
729 long size
= get_memory_packet_size (&memory_read_packet_config
);
730 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
731 extra buffer size argument before the memory read size can be
732 increased beyond this. */
733 if (size
> get_remote_packet_size ())
734 size
= get_remote_packet_size ();
739 /* Generic configuration support for packets the stub optionally
740 supports. Allows the user to specify the use of the packet as well
741 as allowing GDB to auto-detect support in the remote stub. */
745 PACKET_SUPPORT_UNKNOWN
= 0,
754 enum auto_boolean detect
;
755 enum packet_support support
;
758 /* Analyze a packet's return value and update the packet config
769 update_packet_config (struct packet_config
*config
)
771 switch (config
->detect
)
773 case AUTO_BOOLEAN_TRUE
:
774 config
->support
= PACKET_ENABLE
;
776 case AUTO_BOOLEAN_FALSE
:
777 config
->support
= PACKET_DISABLE
;
779 case AUTO_BOOLEAN_AUTO
:
780 config
->support
= PACKET_SUPPORT_UNKNOWN
;
786 show_packet_config_cmd (struct packet_config
*config
)
788 char *support
= "internal-error";
789 switch (config
->support
)
795 support
= "disabled";
797 case PACKET_SUPPORT_UNKNOWN
:
801 switch (config
->detect
)
803 case AUTO_BOOLEAN_AUTO
:
804 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
805 config
->name
, support
);
807 case AUTO_BOOLEAN_TRUE
:
808 case AUTO_BOOLEAN_FALSE
:
809 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
810 config
->name
, support
);
816 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
817 const char *title
, int legacy
)
824 config
->title
= title
;
825 config
->detect
= AUTO_BOOLEAN_AUTO
;
826 config
->support
= PACKET_SUPPORT_UNKNOWN
;
827 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
829 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
831 /* set/show TITLE-packet {auto,on,off} */
832 cmd_name
= xstrprintf ("%s-packet", title
);
833 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
834 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
835 set_remote_protocol_packet_cmd
,
836 show_remote_protocol_packet_cmd
,
837 &remote_set_cmdlist
, &remote_show_cmdlist
);
838 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
842 legacy_name
= xstrprintf ("%s-packet", name
);
843 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
844 &remote_set_cmdlist
);
845 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
846 &remote_show_cmdlist
);
850 static enum packet_result
851 packet_check_result (const char *buf
)
855 /* The stub recognized the packet request. Check that the
856 operation succeeded. */
858 && isxdigit (buf
[1]) && isxdigit (buf
[2])
860 /* "Enn" - definitly an error. */
863 /* Always treat "E." as an error. This will be used for
864 more verbose error messages, such as E.memtypes. */
865 if (buf
[0] == 'E' && buf
[1] == '.')
868 /* The packet may or may not be OK. Just assume it is. */
872 /* The stub does not support the packet. */
873 return PACKET_UNKNOWN
;
876 static enum packet_result
877 packet_ok (const char *buf
, struct packet_config
*config
)
879 enum packet_result result
;
881 result
= packet_check_result (buf
);
886 /* The stub recognized the packet request. */
887 switch (config
->support
)
889 case PACKET_SUPPORT_UNKNOWN
:
891 fprintf_unfiltered (gdb_stdlog
,
892 "Packet %s (%s) is supported\n",
893 config
->name
, config
->title
);
894 config
->support
= PACKET_ENABLE
;
897 internal_error (__FILE__
, __LINE__
,
898 _("packet_ok: attempt to use a disabled packet"));
905 /* The stub does not support the packet. */
906 switch (config
->support
)
909 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
910 /* If the stub previously indicated that the packet was
911 supported then there is a protocol error.. */
912 error (_("Protocol error: %s (%s) conflicting enabled responses."),
913 config
->name
, config
->title
);
915 /* The user set it wrong. */
916 error (_("Enabled packet %s (%s) not recognized by stub"),
917 config
->name
, config
->title
);
919 case PACKET_SUPPORT_UNKNOWN
:
921 fprintf_unfiltered (gdb_stdlog
,
922 "Packet %s (%s) is NOT supported\n",
923 config
->name
, config
->title
);
924 config
->support
= PACKET_DISABLE
;
952 PACKET_qXfer_features
,
953 PACKET_qXfer_libraries
,
954 PACKET_qXfer_memory_map
,
955 PACKET_qXfer_spu_read
,
956 PACKET_qXfer_spu_write
,
960 PACKET_qSearch_memory
,
966 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
969 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
970 struct cmd_list_element
*c
)
972 struct packet_config
*packet
;
974 for (packet
= remote_protocol_packets
;
975 packet
< &remote_protocol_packets
[PACKET_MAX
];
978 if (&packet
->detect
== c
->var
)
980 update_packet_config (packet
);
984 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
989 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
990 struct cmd_list_element
*c
,
993 struct packet_config
*packet
;
995 for (packet
= remote_protocol_packets
;
996 packet
< &remote_protocol_packets
[PACKET_MAX
];
999 if (&packet
->detect
== c
->var
)
1001 show_packet_config_cmd (packet
);
1005 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1009 /* Should we try one of the 'Z' requests? */
1013 Z_PACKET_SOFTWARE_BP
,
1014 Z_PACKET_HARDWARE_BP
,
1021 /* For compatibility with older distributions. Provide a ``set remote
1022 Z-packet ...'' command that updates all the Z packet types. */
1024 static enum auto_boolean remote_Z_packet_detect
;
1027 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1028 struct cmd_list_element
*c
)
1031 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1033 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1034 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1039 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1040 struct cmd_list_element
*c
,
1044 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1046 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1050 /* Should we try the 'ThreadInfo' query packet?
1052 This variable (NOT available to the user: auto-detect only!)
1053 determines whether GDB will use the new, simpler "ThreadInfo"
1054 query or the older, more complex syntax for thread queries.
1055 This is an auto-detect variable (set to true at each connect,
1056 and set to false when the target fails to recognize it). */
1058 static int use_threadinfo_query
;
1059 static int use_threadextra_query
;
1061 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1062 static struct async_signal_handler
*sigint_remote_twice_token
;
1063 static struct async_signal_handler
*sigint_remote_token
;
1067 static ptid_t magic_null_ptid
;
1068 static ptid_t not_sent_ptid
;
1069 static ptid_t any_thread_ptid
;
1071 /* These are the threads which we last sent to the remote system. The
1072 TID member will be -1 for all or -2 for not sent yet. */
1074 static ptid_t general_thread
;
1075 static ptid_t continue_thread
;
1078 /* Call this function as a result of
1079 1) A halt indication (T packet) containing a thread id
1080 2) A direct query of currthread
1081 3) Successful execution of set thread
1085 record_currthread (ptid_t currthread
)
1087 general_thread
= currthread
;
1089 /* If this is a new thread, add it to GDB's thread list.
1090 If we leave it up to WFI to do this, bad things will happen. */
1091 if (!in_thread_list (currthread
))
1092 add_thread (currthread
);
1095 static char *last_pass_packet
;
1097 /* If 'QPassSignals' is supported, tell the remote stub what signals
1098 it can simply pass through to the inferior without reporting. */
1101 remote_pass_signals (void)
1103 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1105 char *pass_packet
, *p
;
1106 int numsigs
= (int) TARGET_SIGNAL_LAST
;
1109 gdb_assert (numsigs
< 256);
1110 for (i
= 0; i
< numsigs
; i
++)
1112 if (signal_stop_state (i
) == 0
1113 && signal_print_state (i
) == 0
1114 && signal_pass_state (i
) == 1)
1117 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1118 strcpy (pass_packet
, "QPassSignals:");
1119 p
= pass_packet
+ strlen (pass_packet
);
1120 for (i
= 0; i
< numsigs
; i
++)
1122 if (signal_stop_state (i
) == 0
1123 && signal_print_state (i
) == 0
1124 && signal_pass_state (i
) == 1)
1127 *p
++ = tohex (i
>> 4);
1128 *p
++ = tohex (i
& 15);
1137 if (!last_pass_packet
|| strcmp (last_pass_packet
, pass_packet
))
1139 struct remote_state
*rs
= get_remote_state ();
1140 char *buf
= rs
->buf
;
1142 putpkt (pass_packet
);
1143 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1144 packet_ok (buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1145 if (last_pass_packet
)
1146 xfree (last_pass_packet
);
1147 last_pass_packet
= pass_packet
;
1150 xfree (pass_packet
);
1154 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1155 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1156 thread. If GEN is set, set the general thread, if not, then set
1157 the step/continue thread. */
1159 set_thread (struct ptid ptid
, int gen
)
1161 struct remote_state
*rs
= get_remote_state ();
1162 ptid_t state
= gen
? general_thread
: continue_thread
;
1163 char *buf
= rs
->buf
;
1164 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1166 if (ptid_equal (state
, ptid
))
1170 *buf
++ = gen
? 'g' : 'c';
1171 if (ptid_equal (ptid
, magic_null_ptid
))
1172 xsnprintf (buf
, endbuf
- buf
, "0");
1173 else if (ptid_equal (ptid
, any_thread_ptid
))
1174 xsnprintf (buf
, endbuf
- buf
, "0");
1175 else if (ptid_equal (ptid
, minus_one_ptid
))
1176 xsnprintf (buf
, endbuf
- buf
, "-1");
1179 int tid
= ptid_get_tid (ptid
);
1181 xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
1183 xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
1186 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1188 general_thread
= ptid
;
1190 continue_thread
= ptid
;
1194 set_general_thread (struct ptid ptid
)
1196 set_thread (ptid
, 1);
1200 set_continue_thread (struct ptid ptid
)
1202 set_thread (ptid
, 0);
1206 /* Return nonzero if the thread PTID is still alive on the remote
1210 remote_thread_alive (ptid_t ptid
)
1212 struct remote_state
*rs
= get_remote_state ();
1213 int tid
= ptid_get_tid (ptid
);
1216 xsnprintf (rs
->buf
, get_remote_packet_size (), "T-%08x", -tid
);
1218 xsnprintf (rs
->buf
, get_remote_packet_size (), "T%08x", tid
);
1220 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1221 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1224 /* About these extended threadlist and threadinfo packets. They are
1225 variable length packets but, the fields within them are often fixed
1226 length. They are redundent enough to send over UDP as is the
1227 remote protocol in general. There is a matching unit test module
1230 #define OPAQUETHREADBYTES 8
1232 /* a 64 bit opaque identifier */
1233 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1235 /* WARNING: This threadref data structure comes from the remote O.S.,
1236 libstub protocol encoding, and remote.c. it is not particularly
1239 /* Right now, the internal structure is int. We want it to be bigger.
1243 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1245 /* gdb_ext_thread_info is an internal GDB data structure which is
1246 equivalent to the reply of the remote threadinfo packet. */
1248 struct gdb_ext_thread_info
1250 threadref threadid
; /* External form of thread reference. */
1251 int active
; /* Has state interesting to GDB?
1253 char display
[256]; /* Brief state display, name,
1254 blocked/suspended. */
1255 char shortname
[32]; /* To be used to name threads. */
1256 char more_display
[256]; /* Long info, statistics, queue depth,
1260 /* The volume of remote transfers can be limited by submitting
1261 a mask containing bits specifying the desired information.
1262 Use a union of these values as the 'selection' parameter to
1263 get_thread_info. FIXME: Make these TAG names more thread specific.
1266 #define TAG_THREADID 1
1267 #define TAG_EXISTS 2
1268 #define TAG_DISPLAY 4
1269 #define TAG_THREADNAME 8
1270 #define TAG_MOREDISPLAY 16
1272 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1274 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1276 static char *unpack_nibble (char *buf
, int *val
);
1278 static char *pack_nibble (char *buf
, int nibble
);
1280 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1282 static char *unpack_byte (char *buf
, int *value
);
1284 static char *pack_int (char *buf
, int value
);
1286 static char *unpack_int (char *buf
, int *value
);
1288 static char *unpack_string (char *src
, char *dest
, int length
);
1290 static char *pack_threadid (char *pkt
, threadref
*id
);
1292 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1294 void int_to_threadref (threadref
*id
, int value
);
1296 static int threadref_to_int (threadref
*ref
);
1298 static void copy_threadref (threadref
*dest
, threadref
*src
);
1300 static int threadmatch (threadref
*dest
, threadref
*src
);
1302 static char *pack_threadinfo_request (char *pkt
, int mode
,
1305 static int remote_unpack_thread_info_response (char *pkt
,
1306 threadref
*expectedref
,
1307 struct gdb_ext_thread_info
1311 static int remote_get_threadinfo (threadref
*threadid
,
1312 int fieldset
, /*TAG mask */
1313 struct gdb_ext_thread_info
*info
);
1315 static char *pack_threadlist_request (char *pkt
, int startflag
,
1317 threadref
*nextthread
);
1319 static int parse_threadlist_response (char *pkt
,
1321 threadref
*original_echo
,
1322 threadref
*resultlist
,
1325 static int remote_get_threadlist (int startflag
,
1326 threadref
*nextthread
,
1330 threadref
*threadlist
);
1332 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1334 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1335 void *context
, int looplimit
);
1337 static int remote_newthread_step (threadref
*ref
, void *context
);
1339 /* Encode 64 bits in 16 chars of hex. */
1341 static const char hexchars
[] = "0123456789abcdef";
1344 ishex (int ch
, int *val
)
1346 if ((ch
>= 'a') && (ch
<= 'f'))
1348 *val
= ch
- 'a' + 10;
1351 if ((ch
>= 'A') && (ch
<= 'F'))
1353 *val
= ch
- 'A' + 10;
1356 if ((ch
>= '0') && (ch
<= '9'))
1367 if (ch
>= 'a' && ch
<= 'f')
1368 return ch
- 'a' + 10;
1369 if (ch
>= '0' && ch
<= '9')
1371 if (ch
>= 'A' && ch
<= 'F')
1372 return ch
- 'A' + 10;
1377 stub_unpack_int (char *buff
, int fieldlength
)
1384 nibble
= stubhex (*buff
++);
1388 retval
= retval
<< 4;
1394 unpack_varlen_hex (char *buff
, /* packet to parse */
1398 ULONGEST retval
= 0;
1400 while (ishex (*buff
, &nibble
))
1403 retval
= retval
<< 4;
1404 retval
|= nibble
& 0x0f;
1411 unpack_nibble (char *buf
, int *val
)
1413 *val
= fromhex (*buf
++);
1418 pack_nibble (char *buf
, int nibble
)
1420 *buf
++ = hexchars
[(nibble
& 0x0f)];
1425 pack_hex_byte (char *pkt
, int byte
)
1427 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1428 *pkt
++ = hexchars
[(byte
& 0xf)];
1433 unpack_byte (char *buf
, int *value
)
1435 *value
= stub_unpack_int (buf
, 2);
1440 pack_int (char *buf
, int value
)
1442 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
1443 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
1444 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
1445 buf
= pack_hex_byte (buf
, (value
& 0xff));
1450 unpack_int (char *buf
, int *value
)
1452 *value
= stub_unpack_int (buf
, 8);
1456 #if 0 /* Currently unused, uncomment when needed. */
1457 static char *pack_string (char *pkt
, char *string
);
1460 pack_string (char *pkt
, char *string
)
1465 len
= strlen (string
);
1467 len
= 200; /* Bigger than most GDB packets, junk??? */
1468 pkt
= pack_hex_byte (pkt
, len
);
1472 if ((ch
== '\0') || (ch
== '#'))
1473 ch
= '*'; /* Protect encapsulation. */
1478 #endif /* 0 (unused) */
1481 unpack_string (char *src
, char *dest
, int length
)
1490 pack_threadid (char *pkt
, threadref
*id
)
1493 unsigned char *altid
;
1495 altid
= (unsigned char *) id
;
1496 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
1498 pkt
= pack_hex_byte (pkt
, *altid
++);
1504 unpack_threadid (char *inbuf
, threadref
*id
)
1507 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
1510 altref
= (char *) id
;
1512 while (inbuf
< limit
)
1514 x
= stubhex (*inbuf
++);
1515 y
= stubhex (*inbuf
++);
1516 *altref
++ = (x
<< 4) | y
;
1521 /* Externally, threadrefs are 64 bits but internally, they are still
1522 ints. This is due to a mismatch of specifications. We would like
1523 to use 64bit thread references internally. This is an adapter
1527 int_to_threadref (threadref
*id
, int value
)
1529 unsigned char *scan
;
1531 scan
= (unsigned char *) id
;
1537 *scan
++ = (value
>> 24) & 0xff;
1538 *scan
++ = (value
>> 16) & 0xff;
1539 *scan
++ = (value
>> 8) & 0xff;
1540 *scan
++ = (value
& 0xff);
1544 threadref_to_int (threadref
*ref
)
1547 unsigned char *scan
;
1553 value
= (value
<< 8) | ((*scan
++) & 0xff);
1558 copy_threadref (threadref
*dest
, threadref
*src
)
1561 unsigned char *csrc
, *cdest
;
1563 csrc
= (unsigned char *) src
;
1564 cdest
= (unsigned char *) dest
;
1571 threadmatch (threadref
*dest
, threadref
*src
)
1573 /* Things are broken right now, so just assume we got a match. */
1575 unsigned char *srcp
, *destp
;
1577 srcp
= (char *) src
;
1578 destp
= (char *) dest
;
1582 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1589 threadid:1, # always request threadid
1596 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1599 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
1601 *pkt
++ = 'q'; /* Info Query */
1602 *pkt
++ = 'P'; /* process or thread info */
1603 pkt
= pack_int (pkt
, mode
); /* mode */
1604 pkt
= pack_threadid (pkt
, id
); /* threadid */
1605 *pkt
= '\0'; /* terminate */
1609 /* These values tag the fields in a thread info response packet. */
1610 /* Tagging the fields allows us to request specific fields and to
1611 add more fields as time goes by. */
1613 #define TAG_THREADID 1 /* Echo the thread identifier. */
1614 #define TAG_EXISTS 2 /* Is this process defined enough to
1615 fetch registers and its stack? */
1616 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1617 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
1618 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1622 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
1623 struct gdb_ext_thread_info
*info
)
1625 struct remote_state
*rs
= get_remote_state ();
1629 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
1632 /* info->threadid = 0; FIXME: implement zero_threadref. */
1634 info
->display
[0] = '\0';
1635 info
->shortname
[0] = '\0';
1636 info
->more_display
[0] = '\0';
1638 /* Assume the characters indicating the packet type have been
1640 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1641 pkt
= unpack_threadid (pkt
, &ref
);
1644 warning (_("Incomplete response to threadinfo request."));
1645 if (!threadmatch (&ref
, expectedref
))
1646 { /* This is an answer to a different request. */
1647 warning (_("ERROR RMT Thread info mismatch."));
1650 copy_threadref (&info
->threadid
, &ref
);
1652 /* Loop on tagged fields , try to bail if somthing goes wrong. */
1654 /* Packets are terminated with nulls. */
1655 while ((pkt
< limit
) && mask
&& *pkt
)
1657 pkt
= unpack_int (pkt
, &tag
); /* tag */
1658 pkt
= unpack_byte (pkt
, &length
); /* length */
1659 if (!(tag
& mask
)) /* Tags out of synch with mask. */
1661 warning (_("ERROR RMT: threadinfo tag mismatch."));
1665 if (tag
== TAG_THREADID
)
1669 warning (_("ERROR RMT: length of threadid is not 16."));
1673 pkt
= unpack_threadid (pkt
, &ref
);
1674 mask
= mask
& ~TAG_THREADID
;
1677 if (tag
== TAG_EXISTS
)
1679 info
->active
= stub_unpack_int (pkt
, length
);
1681 mask
= mask
& ~(TAG_EXISTS
);
1684 warning (_("ERROR RMT: 'exists' length too long."));
1690 if (tag
== TAG_THREADNAME
)
1692 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1693 mask
= mask
& ~TAG_THREADNAME
;
1696 if (tag
== TAG_DISPLAY
)
1698 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1699 mask
= mask
& ~TAG_DISPLAY
;
1702 if (tag
== TAG_MOREDISPLAY
)
1704 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1705 mask
= mask
& ~TAG_MOREDISPLAY
;
1708 warning (_("ERROR RMT: unknown thread info tag."));
1709 break; /* Not a tag we know about. */
1715 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
1716 struct gdb_ext_thread_info
*info
)
1718 struct remote_state
*rs
= get_remote_state ();
1721 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
1723 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1724 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
1729 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1732 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
1733 threadref
*nextthread
)
1735 *pkt
++ = 'q'; /* info query packet */
1736 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1737 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1738 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1739 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1744 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1747 parse_threadlist_response (char *pkt
, int result_limit
,
1748 threadref
*original_echo
, threadref
*resultlist
,
1751 struct remote_state
*rs
= get_remote_state ();
1753 int count
, resultcount
, done
;
1756 /* Assume the 'q' and 'M chars have been stripped. */
1757 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
1758 /* done parse past here */
1759 pkt
= unpack_byte (pkt
, &count
); /* count field */
1760 pkt
= unpack_nibble (pkt
, &done
);
1761 /* The first threadid is the argument threadid. */
1762 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1763 while ((count
-- > 0) && (pkt
< limit
))
1765 pkt
= unpack_threadid (pkt
, resultlist
++);
1766 if (resultcount
++ >= result_limit
)
1775 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
1776 int *done
, int *result_count
, threadref
*threadlist
)
1778 struct remote_state
*rs
= get_remote_state ();
1779 static threadref echo_nextthread
;
1782 /* Trancate result limit to be smaller than the packet size. */
1783 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= get_remote_packet_size ())
1784 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
1786 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
1788 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1790 if (*rs
->buf
== '\0')
1794 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
1797 if (!threadmatch (&echo_nextthread
, nextthread
))
1799 /* FIXME: This is a good reason to drop the packet. */
1800 /* Possably, there is a duplicate response. */
1802 retransmit immediatly - race conditions
1803 retransmit after timeout - yes
1805 wait for packet, then exit
1807 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
1808 return 0; /* I choose simply exiting. */
1810 if (*result_count
<= 0)
1814 warning (_("RMT ERROR : failed to get remote thread list."));
1817 return result
; /* break; */
1819 if (*result_count
> result_limit
)
1822 warning (_("RMT ERROR: threadlist response longer than requested."));
1828 /* This is the interface between remote and threads, remotes upper
1831 /* remote_find_new_threads retrieves the thread list and for each
1832 thread in the list, looks up the thread in GDB's internal list,
1833 adding the thread if it does not already exist. This involves
1834 getting partial thread lists from the remote target so, polling the
1835 quit_flag is required. */
1838 /* About this many threadisds fit in a packet. */
1840 #define MAXTHREADLISTRESULTS 32
1843 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
1846 int done
, i
, result_count
;
1850 static threadref nextthread
;
1851 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1856 if (loopcount
++ > looplimit
)
1859 warning (_("Remote fetch threadlist -infinite loop-."));
1862 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1863 &done
, &result_count
, resultthreadlist
))
1868 /* Clear for later iterations. */
1870 /* Setup to resume next batch of thread references, set nextthread. */
1871 if (result_count
>= 1)
1872 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1874 while (result_count
--)
1875 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1882 remote_newthread_step (threadref
*ref
, void *context
)
1884 int pid
= ptid_get_pid (inferior_ptid
);
1885 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
1887 if (!in_thread_list (ptid
))
1889 return 1; /* continue iterator */
1892 #define CRAZY_MAX_THREADS 1000
1895 remote_current_thread (ptid_t oldpid
)
1897 struct remote_state
*rs
= get_remote_state ();
1903 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1904 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
1906 /* Use strtoul here, so we'll correctly parse values whose
1907 highest bit is set. The protocol carries them as a simple
1908 series of hex digits; in the absence of a sign, strtol will
1909 see such values as positive numbers out of range for signed
1910 'long', and return LONG_MAX to indicate an overflow. */
1911 tid
= strtoul (&rs
->buf
[2], NULL
, 16);
1912 pid
= ptid_get_pid (oldpid
);
1913 return ptid_build (pid
, 0, tid
);
1919 /* Find new threads for info threads command.
1920 * Original version, using John Metzler's thread protocol.
1924 remote_find_new_threads (void)
1926 remote_threadlist_iterator (remote_newthread_step
, 0,
1928 if (ptid_equal (inferior_ptid
, magic_null_ptid
))
1929 /* We don't know the current thread yet. Query it. */
1930 inferior_ptid
= remote_current_thread (inferior_ptid
);
1934 * Find all threads for info threads command.
1935 * Uses new thread protocol contributed by Cisco.
1936 * Falls back and attempts to use the older method (above)
1937 * if the target doesn't respond to the new method.
1941 remote_threads_info (void)
1943 struct remote_state
*rs
= get_remote_state ();
1949 if (remote_desc
== 0) /* paranoia */
1950 error (_("Command can only be used when connected to the remote target."));
1952 if (use_threadinfo_query
)
1954 putpkt ("qfThreadInfo");
1955 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1957 if (bufp
[0] != '\0') /* q packet recognized */
1959 while (*bufp
++ == 'm') /* reply contains one or more TID */
1963 /* Use strtoul here, so we'll correctly parse values
1964 whose highest bit is set. The protocol carries
1965 them as a simple series of hex digits; in the
1966 absence of a sign, strtol will see such values as
1967 positive numbers out of range for signed 'long',
1968 and return LONG_MAX to indicate an overflow. */
1969 tid
= strtoul (bufp
, &bufp
, 16);
1970 pid
= ptid_get_pid (inferior_ptid
);
1971 new_thread
= ptid_build (pid
, 0, tid
);
1972 if (tid
!= 0 && !in_thread_list (new_thread
))
1973 add_thread (new_thread
);
1975 while (*bufp
++ == ','); /* comma-separated list */
1976 putpkt ("qsThreadInfo");
1977 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1984 /* Else fall back to old method based on jmetzler protocol. */
1985 use_threadinfo_query
= 0;
1986 remote_find_new_threads ();
1991 * Collect a descriptive string about the given thread.
1992 * The target may say anything it wants to about the thread
1993 * (typically info about its blocked / runnable state, name, etc.).
1994 * This string will appear in the info threads display.
1996 * Optional: targets are not required to implement this function.
2000 remote_threads_extra_info (struct thread_info
*tp
)
2002 struct remote_state
*rs
= get_remote_state ();
2006 struct gdb_ext_thread_info threadinfo
;
2007 static char display_buf
[100]; /* arbitrary... */
2008 int n
= 0; /* position in display_buf */
2010 if (remote_desc
== 0) /* paranoia */
2011 internal_error (__FILE__
, __LINE__
,
2012 _("remote_threads_extra_info"));
2014 if (use_threadextra_query
)
2016 xsnprintf (rs
->buf
, get_remote_packet_size (), "qThreadExtraInfo,%lx",
2017 ptid_get_tid (tp
->ptid
));
2019 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2020 if (rs
->buf
[0] != 0)
2022 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2023 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2024 display_buf
[result
] = '\0';
2029 /* If the above query fails, fall back to the old method. */
2030 use_threadextra_query
= 0;
2031 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2032 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2033 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2034 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2035 if (threadinfo
.active
)
2037 if (*threadinfo
.shortname
)
2038 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2039 " Name: %s,", threadinfo
.shortname
);
2040 if (*threadinfo
.display
)
2041 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2042 " State: %s,", threadinfo
.display
);
2043 if (*threadinfo
.more_display
)
2044 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2045 " Priority: %s", threadinfo
.more_display
);
2049 /* For purely cosmetic reasons, clear up trailing commas. */
2050 if (',' == display_buf
[n
-1])
2051 display_buf
[n
-1] = ' ';
2059 /* Restart the remote side; this is an extended protocol operation. */
2062 extended_remote_restart (void)
2064 struct remote_state
*rs
= get_remote_state ();
2066 /* Send the restart command; for reasons I don't understand the
2067 remote side really expects a number after the "R". */
2068 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2071 remote_fileio_reset ();
2074 /* Clean up connection to a remote debugger. */
2077 remote_close (int quitting
)
2080 serial_close (remote_desc
);
2084 /* Query the remote side for the text, data and bss offsets. */
2089 struct remote_state
*rs
= get_remote_state ();
2092 int lose
, num_segments
= 0, do_sections
, do_segments
;
2093 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
2094 struct section_offsets
*offs
;
2095 struct symfile_segment_data
*data
;
2097 if (symfile_objfile
== NULL
)
2100 putpkt ("qOffsets");
2101 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2104 if (buf
[0] == '\000')
2105 return; /* Return silently. Stub doesn't support
2109 warning (_("Remote failure reply: %s"), buf
);
2113 /* Pick up each field in turn. This used to be done with scanf, but
2114 scanf will make trouble if CORE_ADDR size doesn't match
2115 conversion directives correctly. The following code will work
2116 with any size of CORE_ADDR. */
2117 text_addr
= data_addr
= bss_addr
= 0;
2121 if (strncmp (ptr
, "Text=", 5) == 0)
2124 /* Don't use strtol, could lose on big values. */
2125 while (*ptr
&& *ptr
!= ';')
2126 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2128 if (strncmp (ptr
, ";Data=", 6) == 0)
2131 while (*ptr
&& *ptr
!= ';')
2132 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2137 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
2140 while (*ptr
&& *ptr
!= ';')
2141 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
2143 if (bss_addr
!= data_addr
)
2144 warning (_("Target reported unsupported offsets: %s"), buf
);
2149 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
2152 /* Don't use strtol, could lose on big values. */
2153 while (*ptr
&& *ptr
!= ';')
2154 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2157 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
2160 while (*ptr
&& *ptr
!= ';')
2161 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2169 error (_("Malformed response to offset query, %s"), buf
);
2170 else if (*ptr
!= '\0')
2171 warning (_("Target reported unsupported offsets: %s"), buf
);
2173 offs
= ((struct section_offsets
*)
2174 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
2175 memcpy (offs
, symfile_objfile
->section_offsets
,
2176 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
2178 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
2179 do_segments
= (data
!= NULL
);
2180 do_sections
= num_segments
== 0;
2182 if (num_segments
> 0)
2184 segments
[0] = text_addr
;
2185 segments
[1] = data_addr
;
2187 /* If we have two segments, we can still try to relocate everything
2188 by assuming that the .text and .data offsets apply to the whole
2189 text and data segments. Convert the offsets given in the packet
2190 to base addresses for symfile_map_offsets_to_segments. */
2191 else if (data
&& data
->num_segments
== 2)
2193 segments
[0] = data
->segment_bases
[0] + text_addr
;
2194 segments
[1] = data
->segment_bases
[1] + data_addr
;
2197 /* If the object file has only one segment, assume that it is text
2198 rather than data; main programs with no writable data are rare,
2199 but programs with no code are useless. Of course the code might
2200 have ended up in the data segment... to detect that we would need
2201 the permissions here. */
2202 else if (data
&& data
->num_segments
== 1)
2204 segments
[0] = data
->segment_bases
[0] + text_addr
;
2207 /* There's no way to relocate by segment. */
2213 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
2214 offs
, num_segments
, segments
);
2216 if (ret
== 0 && !do_sections
)
2217 error (_("Can not handle qOffsets TextSeg response with this symbol file"));
2224 free_symfile_segment_data (data
);
2228 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
2230 /* This is a temporary kludge to force data and bss to use the same offsets
2231 because that's what nlmconv does now. The real solution requires changes
2232 to the stub and remote.c that I don't have time to do right now. */
2234 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
2235 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
2238 objfile_relocate (symfile_objfile
, offs
);
2241 /* Stub for catch_exception. */
2243 struct start_remote_args
2247 /* The current target. */
2248 struct target_ops
*target
;
2250 /* Non-zero if this is an extended-remote target. */
2255 remote_start_remote (struct ui_out
*uiout
, void *opaque
)
2257 struct remote_state
*rs
= get_remote_state ();
2258 struct start_remote_args
*args
= opaque
;
2259 char *wait_status
= NULL
;
2261 immediate_quit
++; /* Allow user to interrupt it. */
2263 /* Ack any packet which the remote side has already sent. */
2264 serial_write (remote_desc
, "+", 1);
2266 /* Check whether the target is running now. */
2268 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2270 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
2272 if (args
->extended_p
)
2274 /* We're connected, but not running. Drop out before we
2275 call start_remote. */
2276 target_mark_exited (args
->target
);
2280 error (_("The target is not running (try extended-remote?)"));
2284 if (args
->extended_p
)
2285 target_mark_running (args
->target
);
2287 /* Save the reply for later. */
2288 wait_status
= alloca (strlen (rs
->buf
) + 1);
2289 strcpy (wait_status
, rs
->buf
);
2292 /* Let the stub know that we want it to return the thread. */
2293 set_continue_thread (minus_one_ptid
);
2295 /* Without this, some commands which require an active target
2296 (such as kill) won't work. This variable serves (at least)
2297 double duty as both the pid of the target process (if it has
2298 such), and as a flag indicating that a target is active.
2299 These functions should be split out into seperate variables,
2300 especially since GDB will someday have a notion of debugging
2301 several processes. */
2302 inferior_ptid
= magic_null_ptid
;
2304 /* Now, if we have thread information, update inferior_ptid. */
2305 inferior_ptid
= remote_current_thread (inferior_ptid
);
2307 get_offsets (); /* Get text, data & bss offsets. */
2309 /* Use the previously fetched status. */
2310 gdb_assert (wait_status
!= NULL
);
2311 strcpy (rs
->buf
, wait_status
);
2312 rs
->cached_wait_status
= 1;
2315 start_remote (args
->from_tty
); /* Initialize gdb process mechanisms. */
2318 /* Open a connection to a remote debugger.
2319 NAME is the filename used for communication. */
2322 remote_open (char *name
, int from_tty
)
2324 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
2327 /* Open a connection to a remote debugger using the extended
2328 remote gdb protocol. NAME is the filename used for communication. */
2331 extended_remote_open (char *name
, int from_tty
)
2333 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
2336 /* Generic code for opening a connection to a remote target. */
2339 init_all_packet_configs (void)
2342 for (i
= 0; i
< PACKET_MAX
; i
++)
2343 update_packet_config (&remote_protocol_packets
[i
]);
2346 /* Symbol look-up. */
2349 remote_check_symbols (struct objfile
*objfile
)
2351 struct remote_state
*rs
= get_remote_state ();
2352 char *msg
, *reply
, *tmp
;
2353 struct minimal_symbol
*sym
;
2356 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
2359 /* Allocate a message buffer. We can't reuse the input buffer in RS,
2360 because we need both at the same time. */
2361 msg
= alloca (get_remote_packet_size ());
2363 /* Invite target to request symbol lookups. */
2365 putpkt ("qSymbol::");
2366 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2367 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
2370 while (strncmp (reply
, "qSymbol:", 8) == 0)
2373 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
2375 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
2377 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
2380 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
2382 /* If this is a function address, return the start of code
2383 instead of any data function descriptor. */
2384 sym_addr
= gdbarch_convert_from_func_ptr_addr (current_gdbarch
,
2388 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
2389 paddr_nz (sym_addr
), &reply
[8]);
2393 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2398 static struct serial
*
2399 remote_serial_open (char *name
)
2401 static int udp_warning
= 0;
2403 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2404 of in ser-tcp.c, because it is the remote protocol assuming that the
2405 serial connection is reliable and not the serial connection promising
2407 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
2410 The remote protocol may be unreliable over UDP.\n\
2411 Some events may be lost, rendering further debugging impossible."));
2415 return serial_open (name
);
2418 /* This type describes each known response to the qSupported
2420 struct protocol_feature
2422 /* The name of this protocol feature. */
2425 /* The default for this protocol feature. */
2426 enum packet_support default_support
;
2428 /* The function to call when this feature is reported, or after
2429 qSupported processing if the feature is not supported.
2430 The first argument points to this structure. The second
2431 argument indicates whether the packet requested support be
2432 enabled, disabled, or probed (or the default, if this function
2433 is being called at the end of processing and this feature was
2434 not reported). The third argument may be NULL; if not NULL, it
2435 is a NUL-terminated string taken from the packet following
2436 this feature's name and an equals sign. */
2437 void (*func
) (const struct protocol_feature
*, enum packet_support
,
2440 /* The corresponding packet for this feature. Only used if
2441 FUNC is remote_supported_packet. */
2446 remote_supported_packet (const struct protocol_feature
*feature
,
2447 enum packet_support support
,
2448 const char *argument
)
2452 warning (_("Remote qSupported response supplied an unexpected value for"
2453 " \"%s\"."), feature
->name
);
2457 if (remote_protocol_packets
[feature
->packet
].support
2458 == PACKET_SUPPORT_UNKNOWN
)
2459 remote_protocol_packets
[feature
->packet
].support
= support
;
2463 remote_packet_size (const struct protocol_feature
*feature
,
2464 enum packet_support support
, const char *value
)
2466 struct remote_state
*rs
= get_remote_state ();
2471 if (support
!= PACKET_ENABLE
)
2474 if (value
== NULL
|| *value
== '\0')
2476 warning (_("Remote target reported \"%s\" without a size."),
2482 packet_size
= strtol (value
, &value_end
, 16);
2483 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
2485 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
2486 feature
->name
, value
);
2490 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
2492 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
2493 packet_size
, MAX_REMOTE_PACKET_SIZE
);
2494 packet_size
= MAX_REMOTE_PACKET_SIZE
;
2497 /* Record the new maximum packet size. */
2498 rs
->explicit_packet_size
= packet_size
;
2501 static struct protocol_feature remote_protocol_features
[] = {
2502 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
2503 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
2504 PACKET_qXfer_auxv
},
2505 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
2506 PACKET_qXfer_features
},
2507 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
2508 PACKET_qXfer_libraries
},
2509 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
2510 PACKET_qXfer_memory_map
},
2511 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
2512 PACKET_qXfer_spu_read
},
2513 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
2514 PACKET_qXfer_spu_write
},
2515 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
2516 PACKET_QPassSignals
},
2520 remote_query_supported (void)
2522 struct remote_state
*rs
= get_remote_state ();
2525 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
2527 /* The packet support flags are handled differently for this packet
2528 than for most others. We treat an error, a disabled packet, and
2529 an empty response identically: any features which must be reported
2530 to be used will be automatically disabled. An empty buffer
2531 accomplishes this, since that is also the representation for a list
2532 containing no features. */
2535 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
2537 putpkt ("qSupported");
2538 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2540 /* If an error occured, warn, but do not return - just reset the
2541 buffer to empty and go on to disable features. */
2542 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
2545 warning (_("Remote failure reply: %s"), rs
->buf
);
2550 memset (seen
, 0, sizeof (seen
));
2555 enum packet_support is_supported
;
2556 char *p
, *end
, *name_end
, *value
;
2558 /* First separate out this item from the rest of the packet. If
2559 there's another item after this, we overwrite the separator
2560 (terminated strings are much easier to work with). */
2562 end
= strchr (p
, ';');
2565 end
= p
+ strlen (p
);
2575 warning (_("empty item in \"qSupported\" response"));
2580 name_end
= strchr (p
, '=');
2583 /* This is a name=value entry. */
2584 is_supported
= PACKET_ENABLE
;
2585 value
= name_end
+ 1;
2594 is_supported
= PACKET_ENABLE
;
2598 is_supported
= PACKET_DISABLE
;
2602 is_supported
= PACKET_SUPPORT_UNKNOWN
;
2606 warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p
);
2612 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
2613 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
2615 const struct protocol_feature
*feature
;
2618 feature
= &remote_protocol_features
[i
];
2619 feature
->func (feature
, is_supported
, value
);
2624 /* If we increased the packet size, make sure to increase the global
2625 buffer size also. We delay this until after parsing the entire
2626 qSupported packet, because this is the same buffer we were
2628 if (rs
->buf_size
< rs
->explicit_packet_size
)
2630 rs
->buf_size
= rs
->explicit_packet_size
;
2631 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
2634 /* Handle the defaults for unmentioned features. */
2635 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
2638 const struct protocol_feature
*feature
;
2640 feature
= &remote_protocol_features
[i
];
2641 feature
->func (feature
, feature
->default_support
, NULL
);
2647 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
, int extended_p
)
2649 struct remote_state
*rs
= get_remote_state ();
2651 error (_("To open a remote debug connection, you need to specify what\n"
2652 "serial device is attached to the remote system\n"
2653 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
2655 /* See FIXME above. */
2656 if (!remote_async_permitted
)
2657 wait_forever_enabled_p
= 1;
2659 /* If we're connected to a running target, target_preopen will kill it.
2660 But if we're connected to a target system with no running process,
2661 then we will still be connected when it returns. Ask this question
2662 first, before target_preopen has a chance to kill anything. */
2663 if (remote_desc
!= NULL
&& !target_has_execution
)
2666 || query (_("Already connected to a remote target. Disconnect? ")))
2669 error (_("Still connected."));
2672 target_preopen (from_tty
);
2674 unpush_target (target
);
2676 /* This time without a query. If we were connected to an
2677 extended-remote target and target_preopen killed the running
2678 process, we may still be connected. If we are starting "target
2679 remote" now, the extended-remote target will not have been
2680 removed by unpush_target. */
2681 if (remote_desc
!= NULL
&& !target_has_execution
)
2684 /* Make sure we send the passed signals list the next time we resume. */
2685 xfree (last_pass_packet
);
2686 last_pass_packet
= NULL
;
2688 remote_fileio_reset ();
2689 reopen_exec_file ();
2692 remote_desc
= remote_serial_open (name
);
2694 perror_with_name (name
);
2696 if (baud_rate
!= -1)
2698 if (serial_setbaudrate (remote_desc
, baud_rate
))
2700 /* The requested speed could not be set. Error out to
2701 top level after closing remote_desc. Take care to
2702 set remote_desc to NULL to avoid closing remote_desc
2704 serial_close (remote_desc
);
2706 perror_with_name (name
);
2710 serial_raw (remote_desc
);
2712 /* If there is something sitting in the buffer we might take it as a
2713 response to a command, which would be bad. */
2714 serial_flush_input (remote_desc
);
2718 puts_filtered ("Remote debugging using ");
2719 puts_filtered (name
);
2720 puts_filtered ("\n");
2722 push_target (target
); /* Switch to using remote target now. */
2724 /* Assume that the target is running, unless we learn otherwise. */
2725 target_mark_running (target
);
2727 /* Reset the target state; these things will be queried either by
2728 remote_query_supported or as they are needed. */
2729 init_all_packet_configs ();
2730 rs
->explicit_packet_size
= 0;
2732 general_thread
= not_sent_ptid
;
2733 continue_thread
= not_sent_ptid
;
2735 /* Probe for ability to use "ThreadInfo" query, as required. */
2736 use_threadinfo_query
= 1;
2737 use_threadextra_query
= 1;
2739 /* The first packet we send to the target is the optional "supported
2740 packets" request. If the target can answer this, it will tell us
2741 which later probes to skip. */
2742 remote_query_supported ();
2744 /* Next, if the target can specify a description, read it. We do
2745 this before anything involving memory or registers. */
2746 target_find_description ();
2748 if (remote_async_permitted
)
2750 /* With this target we start out by owning the terminal. */
2751 remote_async_terminal_ours_p
= 1;
2753 /* FIXME: cagney/1999-09-23: During the initial connection it is
2754 assumed that the target is already ready and able to respond to
2755 requests. Unfortunately remote_start_remote() eventually calls
2756 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2757 around this. Eventually a mechanism that allows
2758 wait_for_inferior() to expect/get timeouts will be
2760 wait_forever_enabled_p
= 0;
2763 /* First delete any symbols previously loaded from shared libraries. */
2764 no_shared_libraries (NULL
, 0);
2766 /* Start the remote connection. If error() or QUIT, discard this
2767 target (we'd otherwise be in an inconsistent state) and then
2768 propogate the error on up the exception chain. This ensures that
2769 the caller doesn't stumble along blindly assuming that the
2770 function succeeded. The CLI doesn't have this problem but other
2771 UI's, such as MI do.
2773 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2774 this function should return an error indication letting the
2775 caller restore the previous state. Unfortunately the command
2776 ``target remote'' is directly wired to this function making that
2777 impossible. On a positive note, the CLI side of this problem has
2778 been fixed - the function set_cmd_context() makes it possible for
2779 all the ``target ....'' commands to share a common callback
2780 function. See cli-dump.c. */
2782 struct gdb_exception ex
;
2783 struct start_remote_args args
;
2785 args
.from_tty
= from_tty
;
2786 args
.target
= target
;
2787 args
.extended_p
= extended_p
;
2789 ex
= catch_exception (uiout
, remote_start_remote
, &args
, RETURN_MASK_ALL
);
2793 if (remote_async_permitted
)
2794 wait_forever_enabled_p
= 1;
2795 throw_exception (ex
);
2799 if (remote_async_permitted
)
2800 wait_forever_enabled_p
= 1;
2804 /* Tell the remote that we are using the extended protocol. */
2806 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2809 /* If we connected to a live target, do some additional setup. */
2810 if (target_has_execution
)
2812 if (exec_bfd
) /* No use without an exec file. */
2813 remote_check_symbols (symfile_objfile
);
2817 /* This takes a program previously attached to and detaches it. After
2818 this is done, GDB can be used to debug some other program. We
2819 better not have left any breakpoints in the target program or it'll
2820 die when it hits one. */
2823 remote_detach_1 (char *args
, int from_tty
, int extended
)
2825 struct remote_state
*rs
= get_remote_state ();
2828 error (_("Argument given to \"detach\" when remotely debugging."));
2830 if (!target_has_execution
)
2831 error (_("No process to detach from."));
2833 /* Tell the remote target to detach. */
2834 strcpy (rs
->buf
, "D");
2836 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2838 if (rs
->buf
[0] == 'E')
2839 error (_("Can't detach process."));
2841 /* Unregister the file descriptor from the event loop. */
2842 if (target_is_async_p ())
2843 serial_async (remote_desc
, NULL
, 0);
2845 target_mourn_inferior ();
2849 puts_filtered ("Detached from remote process.\n");
2851 puts_filtered ("Ending remote debugging.\n");
2856 remote_detach (char *args
, int from_tty
)
2858 remote_detach_1 (args
, from_tty
, 0);
2862 extended_remote_detach (char *args
, int from_tty
)
2864 remote_detach_1 (args
, from_tty
, 1);
2867 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2870 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
2873 error (_("Argument given to \"disconnect\" when remotely debugging."));
2875 /* Unregister the file descriptor from the event loop. */
2876 if (target_is_async_p ())
2877 serial_async (remote_desc
, NULL
, 0);
2879 /* Make sure we unpush even the extended remote targets; mourn
2880 won't do it. So call remote_mourn_1 directly instead of
2881 target_mourn_inferior. */
2882 remote_mourn_1 (target
);
2885 puts_filtered ("Ending remote debugging.\n");
2888 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
2889 be chatty about it. */
2892 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
2894 struct remote_state
*rs
= get_remote_state ();
2897 char *wait_status
= NULL
;
2900 error_no_arg (_("process-id to attach"));
2903 pid
= strtol (args
, &dummy
, 0);
2904 /* Some targets don't set errno on errors, grrr! */
2905 if (pid
== 0 && args
== dummy
)
2906 error (_("Illegal process-id: %s."), args
);
2908 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
2909 error (_("This target does not support attaching to a process"));
2911 sprintf (rs
->buf
, "vAttach;%x", pid
);
2913 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2915 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
2918 printf_unfiltered (_("Attached to %s\n"),
2919 target_pid_to_str (pid_to_ptid (pid
)));
2921 /* Save the reply for later. */
2922 wait_status
= alloca (strlen (rs
->buf
) + 1);
2923 strcpy (wait_status
, rs
->buf
);
2925 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
2926 error (_("This target does not support attaching to a process"));
2928 error (_("Attaching to %s failed"),
2929 target_pid_to_str (pid_to_ptid (pid
)));
2931 target_mark_running (target
);
2932 inferior_ptid
= pid_to_ptid (pid
);
2934 /* Now, if we have thread information, update inferior_ptid. */
2935 inferior_ptid
= remote_current_thread (inferior_ptid
);
2939 /* Next, if the target can specify a description, read it. We do
2940 this before anything involving memory or registers. */
2941 target_find_description ();
2943 /* Use the previously fetched status. */
2944 gdb_assert (wait_status
!= NULL
);
2945 strcpy (rs
->buf
, wait_status
);
2946 rs
->cached_wait_status
= 1;
2950 extended_remote_attach (char *args
, int from_tty
)
2952 extended_remote_attach_1 (&extended_remote_ops
, args
, from_tty
);
2955 /* Convert hex digit A to a number. */
2960 if (a
>= '0' && a
<= '9')
2962 else if (a
>= 'a' && a
<= 'f')
2963 return a
- 'a' + 10;
2964 else if (a
>= 'A' && a
<= 'F')
2965 return a
- 'A' + 10;
2967 error (_("Reply contains invalid hex digit %d"), a
);
2971 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
2975 for (i
= 0; i
< count
; i
++)
2977 if (hex
[0] == 0 || hex
[1] == 0)
2979 /* Hex string is short, or of uneven length.
2980 Return the count that has been converted so far. */
2983 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
2989 /* Convert number NIB to a hex digit. */
2997 return 'a' + nib
- 10;
3001 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
3004 /* May use a length, or a nul-terminated string as input. */
3006 count
= strlen ((char *) bin
);
3008 for (i
= 0; i
< count
; i
++)
3010 *hex
++ = tohex ((*bin
>> 4) & 0xf);
3011 *hex
++ = tohex (*bin
++ & 0xf);
3017 /* Check for the availability of vCont. This function should also check
3021 remote_vcont_probe (struct remote_state
*rs
)
3025 strcpy (rs
->buf
, "vCont?");
3027 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3030 /* Make sure that the features we assume are supported. */
3031 if (strncmp (buf
, "vCont", 5) == 0)
3034 int support_s
, support_S
, support_c
, support_C
;
3040 while (p
&& *p
== ';')
3043 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3045 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3047 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3049 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
3052 p
= strchr (p
, ';');
3055 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
3056 BUF will make packet_ok disable the packet. */
3057 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
3061 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
3064 /* Resume the remote inferior by using a "vCont" packet. The thread
3065 to be resumed is PTID; STEP and SIGGNAL indicate whether the
3066 resumed thread should be single-stepped and/or signalled. If PTID
3067 equals minus_one_ptid, then all threads are resumed; the thread to
3068 be stepped and/or signalled is given in the global INFERIOR_PTID.
3069 This function returns non-zero iff it resumes the inferior.
3071 This function issues a strict subset of all possible vCont commands at the
3075 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
3077 struct remote_state
*rs
= get_remote_state ();
3079 struct cleanup
*old_cleanup
;
3081 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
3082 remote_vcont_probe (rs
);
3084 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
3087 /* If we could generate a wider range of packets, we'd have to worry
3088 about overflowing BUF. Should there be a generic
3089 "multi-part-packet" packet? */
3091 if (ptid_equal (ptid
, magic_null_ptid
))
3093 /* MAGIC_NULL_PTID means that we don't have any active threads,
3094 so we don't have any TID numbers the inferior will
3095 understand. Make sure to only send forms that do not specify
3097 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3098 outbuf
= xstrprintf ("vCont;S%02x", siggnal
);
3100 outbuf
= xstrprintf ("vCont;s");
3101 else if (siggnal
!= TARGET_SIGNAL_0
)
3102 outbuf
= xstrprintf ("vCont;C%02x", siggnal
);
3104 outbuf
= xstrprintf ("vCont;c");
3106 else if (ptid_equal (ptid
, minus_one_ptid
))
3108 /* Resume all threads, with preference for INFERIOR_PTID. */
3109 int tid
= ptid_get_tid (inferior_ptid
);
3110 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3111 outbuf
= xstrprintf ("vCont;S%02x:%x;c", siggnal
, tid
);
3113 outbuf
= xstrprintf ("vCont;s:%x;c", tid
);
3114 else if (siggnal
!= TARGET_SIGNAL_0
)
3115 outbuf
= xstrprintf ("vCont;C%02x:%x;c", siggnal
, tid
);
3117 outbuf
= xstrprintf ("vCont;c");
3121 /* Scheduler locking; resume only PTID. */
3122 int tid
= ptid_get_tid (ptid
);
3123 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
3124 outbuf
= xstrprintf ("vCont;S%02x:%x", siggnal
, tid
);
3126 outbuf
= xstrprintf ("vCont;s:%x", tid
);
3127 else if (siggnal
!= TARGET_SIGNAL_0
)
3128 outbuf
= xstrprintf ("vCont;C%02x:%x", siggnal
, tid
);
3130 outbuf
= xstrprintf ("vCont;c:%x", tid
);
3133 gdb_assert (outbuf
&& strlen (outbuf
) < get_remote_packet_size ());
3134 old_cleanup
= make_cleanup (xfree
, outbuf
);
3138 do_cleanups (old_cleanup
);
3143 /* Tell the remote machine to resume. */
3145 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
3147 static int last_sent_step
;
3150 remote_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
3152 struct remote_state
*rs
= get_remote_state ();
3155 last_sent_signal
= siggnal
;
3156 last_sent_step
= step
;
3158 /* Update the inferior on signals to silently pass, if they've changed. */
3159 remote_pass_signals ();
3161 /* The vCont packet doesn't need to specify threads via Hc. */
3162 if (remote_vcont_resume (ptid
, step
, siggnal
))
3165 /* All other supported resume packets do use Hc, so set the continue
3167 if (ptid_equal (ptid
, minus_one_ptid
))
3168 set_continue_thread (any_thread_ptid
);
3170 set_continue_thread (ptid
);
3173 if (siggnal
!= TARGET_SIGNAL_0
)
3175 buf
[0] = step
? 'S' : 'C';
3176 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
3177 buf
[2] = tohex (((int) siggnal
) & 0xf);
3181 strcpy (buf
, step
? "s" : "c");
3186 /* We are about to start executing the inferior, let's register it
3187 with the event loop. NOTE: this is the one place where all the
3188 execution commands end up. We could alternatively do this in each
3189 of the execution commands in infcmd.c. */
3190 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
3191 into infcmd.c in order to allow inferior function calls to work
3192 NOT asynchronously. */
3193 if (target_can_async_p ())
3194 target_async (inferior_event_handler
, 0);
3195 /* Tell the world that the target is now executing. */
3196 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
3197 this? Instead, should the client of target just assume (for
3198 async targets) that the target is going to start executing? Is
3199 this information already found in the continuation block? */
3200 if (target_is_async_p ())
3201 target_executing
= 1;
3205 /* Set up the signal handler for SIGINT, while the target is
3206 executing, ovewriting the 'regular' SIGINT signal handler. */
3208 initialize_sigint_signal_handler (void)
3210 signal (SIGINT
, handle_remote_sigint
);
3213 /* Signal handler for SIGINT, while the target is executing. */
3215 handle_remote_sigint (int sig
)
3217 signal (sig
, handle_remote_sigint_twice
);
3218 mark_async_signal_handler_wrapper (sigint_remote_token
);
3221 /* Signal handler for SIGINT, installed after SIGINT has already been
3222 sent once. It will take effect the second time that the user sends
3225 handle_remote_sigint_twice (int sig
)
3227 signal (sig
, handle_remote_sigint
);
3228 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
3231 /* Perform the real interruption of the target execution, in response
3234 async_remote_interrupt (gdb_client_data arg
)
3237 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
3242 /* Perform interrupt, if the first attempt did not succeed. Just give
3243 up on the target alltogether. */
3245 async_remote_interrupt_twice (gdb_client_data arg
)
3248 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
3253 /* Reinstall the usual SIGINT handlers, after the target has
3256 cleanup_sigint_signal_handler (void *dummy
)
3258 signal (SIGINT
, handle_sigint
);
3261 /* Send ^C to target to halt it. Target will respond, and send us a
3263 static void (*ofunc
) (int);
3265 /* The command line interface's stop routine. This function is installed
3266 as a signal handler for SIGINT. The first time a user requests a
3267 stop, we call remote_stop to send a break or ^C. If there is no
3268 response from the target (it didn't stop when the user requested it),
3269 we ask the user if he'd like to detach from the target. */
3271 remote_interrupt (int signo
)
3273 /* If this doesn't work, try more severe steps. */
3274 signal (signo
, remote_interrupt_twice
);
3276 gdb_call_async_signal_handler (sigint_remote_token
, 1);
3279 /* The user typed ^C twice. */
3282 remote_interrupt_twice (int signo
)
3284 signal (signo
, ofunc
);
3285 gdb_call_async_signal_handler (sigint_remote_twice_token
, 1);
3286 signal (signo
, remote_interrupt
);
3289 /* This is the generic stop called via the target vector. When a target
3290 interrupt is requested, either by the command line or the GUI, we
3291 will eventually end up here. */
3295 /* Send a break or a ^C, depending on user preference. */
3297 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
3300 serial_send_break (remote_desc
);
3302 serial_write (remote_desc
, "\003", 1);
3305 /* Ask the user what to do when an interrupt is received. */
3308 interrupt_query (void)
3310 target_terminal_ours ();
3312 if (query ("Interrupted while waiting for the program.\n\
3313 Give up (and stop debugging it)? "))
3315 target_mourn_inferior ();
3316 signal (SIGINT
, handle_sigint
);
3317 deprecated_throw_reason (RETURN_QUIT
);
3320 target_terminal_inferior ();
3323 /* Enable/disable target terminal ownership. Most targets can use
3324 terminal groups to control terminal ownership. Remote targets are
3325 different in that explicit transfer of ownership to/from GDB/target
3329 remote_terminal_inferior (void)
3331 if (!remote_async_permitted
)
3332 /* Nothing to do. */
3335 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
3336 sync_execution here. This function should only be called when
3337 GDB is resuming the inferior in the forground. A background
3338 resume (``run&'') should leave GDB in control of the terminal and
3339 consequently should not call this code. */
3340 if (!sync_execution
)
3342 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
3343 calls target_terminal_*() idenpotent. The event-loop GDB talking
3344 to an asynchronous target with a synchronous command calls this
3345 function from both event-top.c and infrun.c/infcmd.c. Once GDB
3346 stops trying to transfer the terminal to the target when it
3347 shouldn't this guard can go away. */
3348 if (!remote_async_terminal_ours_p
)
3350 delete_file_handler (input_fd
);
3351 remote_async_terminal_ours_p
= 0;
3352 initialize_sigint_signal_handler ();
3353 /* NOTE: At this point we could also register our selves as the
3354 recipient of all input. Any characters typed could then be
3355 passed on down to the target. */
3359 remote_terminal_ours (void)
3361 if (!remote_async_permitted
)
3362 /* Nothing to do. */
3365 /* See FIXME in remote_terminal_inferior. */
3366 if (!sync_execution
)
3368 /* See FIXME in remote_terminal_inferior. */
3369 if (remote_async_terminal_ours_p
)
3371 cleanup_sigint_signal_handler (NULL
);
3372 add_file_handler (input_fd
, stdin_event_handler
, 0);
3373 remote_async_terminal_ours_p
= 1;
3377 remote_console_output (char *msg
)
3381 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
3384 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
3387 fputs_unfiltered (tb
, gdb_stdtarg
);
3389 gdb_flush (gdb_stdtarg
);
3392 /* Wait until the remote machine stops, then return,
3393 storing status in STATUS just as `wait' would. */
3396 remote_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3398 struct remote_state
*rs
= get_remote_state ();
3399 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3400 ULONGEST thread_num
= -1;
3401 ULONGEST process_num
= -1;
3403 int solibs_changed
= 0;
3405 status
->kind
= TARGET_WAITKIND_EXITED
;
3406 status
->value
.integer
= 0;
3412 if (rs
->cached_wait_status
)
3413 /* Use the cached wait status, but only once. */
3414 rs
->cached_wait_status
= 0;
3417 if (!target_is_async_p ())
3419 ofunc
= signal (SIGINT
, remote_interrupt
);
3420 /* If the user hit C-c before this packet, or between packets,
3421 pretend that it was hit right here. */
3425 remote_interrupt (SIGINT
);
3428 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3429 _never_ wait for ever -> test on target_is_async_p().
3430 However, before we do that we need to ensure that the caller
3431 knows how to take the target into/out of async mode. */
3432 getpkt (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
3433 if (!target_is_async_p ())
3434 signal (SIGINT
, ofunc
);
3439 remote_stopped_by_watchpoint_p
= 0;
3443 case 'E': /* Error of some sort. */
3444 /* We're out of sync with the target now. Did it continue or not?
3445 Not is more likely, so report a stop. */
3446 warning (_("Remote failure reply: %s"), buf
);
3447 status
->kind
= TARGET_WAITKIND_STOPPED
;
3448 status
->value
.sig
= TARGET_SIGNAL_0
;
3450 case 'F': /* File-I/O request. */
3451 remote_fileio_request (buf
);
3453 case 'T': /* Status with PC, SP, FP, ... */
3455 gdb_byte regs
[MAX_REGISTER_SIZE
];
3457 /* Expedited reply, containing Signal, {regno, reg} repeat. */
3458 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
3460 n... = register number
3461 r... = register contents
3463 p
= &buf
[3]; /* after Txx */
3472 /* If the packet contains a register number, save it
3473 in pnum and set p1 to point to the character
3474 following it. Otherwise p1 points to p. */
3476 /* If this packet is an awatch packet, don't parse the
3477 'a' as a register number. */
3479 if (strncmp (p
, "awatch", strlen("awatch")) != 0)
3481 /* Read the ``P'' register number. */
3482 pnum
= strtol (p
, &p_temp
, 16);
3488 if (p1
== p
) /* No register number present here. */
3490 p1
= strchr (p
, ':');
3492 error (_("Malformed packet(a) (missing colon): %s\n\
3495 if (strncmp (p
, "thread", p1
- p
) == 0)
3497 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
3500 else if ((strncmp (p
, "watch", p1
- p
) == 0)
3501 || (strncmp (p
, "rwatch", p1
- p
) == 0)
3502 || (strncmp (p
, "awatch", p1
- p
) == 0))
3504 remote_stopped_by_watchpoint_p
= 1;
3505 p
= unpack_varlen_hex (++p1
, &addr
);
3506 remote_watch_data_address
= (CORE_ADDR
)addr
;
3508 else if (strncmp (p
, "library", p1
- p
) == 0)
3512 while (*p_temp
&& *p_temp
!= ';')
3520 /* Silently skip unknown optional info. */
3521 p_temp
= strchr (p1
+ 1, ';');
3528 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
3532 error (_("Malformed packet(b) (missing colon): %s\n\
3538 error (_("Remote sent bad register number %s: %s\n\
3540 phex_nz (pnum
, 0), p
, buf
);
3542 fieldsize
= hex2bin (p
, regs
,
3543 register_size (current_gdbarch
,
3546 if (fieldsize
< register_size (current_gdbarch
,
3548 warning (_("Remote reply is too short: %s"), buf
);
3549 regcache_raw_supply (get_current_regcache (),
3554 error (_("Remote register badly formatted: %s\nhere: %s"),
3560 case 'S': /* Old style status, just signal only. */
3562 status
->kind
= TARGET_WAITKIND_LOADED
;
3565 status
->kind
= TARGET_WAITKIND_STOPPED
;
3566 status
->value
.sig
= (enum target_signal
)
3567 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3570 case 'W': /* Target exited. */
3572 /* The remote process exited. */
3573 status
->kind
= TARGET_WAITKIND_EXITED
;
3574 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
3578 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
3579 status
->value
.sig
= (enum target_signal
)
3580 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
3583 case 'O': /* Console output. */
3584 remote_console_output (buf
+ 1);
3585 if (target_can_async_p ())
3587 /* Return immediately to the event loop. The event loop
3588 will still be waiting on the inferior afterwards. */
3589 status
->kind
= TARGET_WAITKIND_IGNORE
;
3595 if (last_sent_signal
!= TARGET_SIGNAL_0
)
3597 /* Zero length reply means that we tried 'S' or 'C' and
3598 the remote system doesn't support it. */
3599 target_terminal_ours_for_output ();
3601 ("Can't send signals to this remote system. %s not sent.\n",
3602 target_signal_to_name (last_sent_signal
));
3603 last_sent_signal
= TARGET_SIGNAL_0
;
3604 target_terminal_inferior ();
3606 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
3607 putpkt ((char *) buf
);
3610 /* else fallthrough */
3612 warning (_("Invalid remote reply: %s"), buf
);
3617 if (thread_num
!= -1)
3620 ptid
= ptid_build (ptid_get_pid (inferior_ptid
), 0, thread_num
);
3621 record_currthread (ptid
);
3625 return inferior_ptid
;
3628 /* Fetch a single register using a 'p' packet. */
3631 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
3633 struct remote_state
*rs
= get_remote_state ();
3635 char regp
[MAX_REGISTER_SIZE
];
3638 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
3641 if (reg
->pnum
== -1)
3646 p
+= hexnumstr (p
, reg
->pnum
);
3648 remote_send (&rs
->buf
, &rs
->buf_size
);
3652 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
3656 case PACKET_UNKNOWN
:
3659 error (_("Could not fetch register \"%s\""),
3660 gdbarch_register_name (get_regcache_arch (regcache
), reg
->regnum
));
3663 /* If this register is unfetchable, tell the regcache. */
3666 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
3670 /* Otherwise, parse and supply the value. */
3676 error (_("fetch_register_using_p: early buf termination"));
3678 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3681 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
3685 /* Fetch the registers included in the target's 'g' packet. */
3688 send_g_packet (void)
3690 struct remote_state
*rs
= get_remote_state ();
3695 sprintf (rs
->buf
, "g");
3696 remote_send (&rs
->buf
, &rs
->buf_size
);
3698 /* We can get out of synch in various cases. If the first character
3699 in the buffer is not a hex character, assume that has happened
3700 and try to fetch another packet to read. */
3701 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
3702 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
3703 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
3704 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
3707 fprintf_unfiltered (gdb_stdlog
,
3708 "Bad register packet; fetching a new packet\n");
3709 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3712 buf_len
= strlen (rs
->buf
);
3714 /* Sanity check the received packet. */
3715 if (buf_len
% 2 != 0)
3716 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
3722 process_g_packet (struct regcache
*regcache
)
3724 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3725 struct remote_state
*rs
= get_remote_state ();
3726 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3731 buf_len
= strlen (rs
->buf
);
3733 /* Further sanity checks, with knowledge of the architecture. */
3734 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
3735 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
3737 /* Save the size of the packet sent to us by the target. It is used
3738 as a heuristic when determining the max size of packets that the
3739 target can safely receive. */
3740 if (rsa
->actual_register_packet_size
== 0)
3741 rsa
->actual_register_packet_size
= buf_len
;
3743 /* If this is smaller than we guessed the 'g' packet would be,
3744 update our records. A 'g' reply that doesn't include a register's
3745 value implies either that the register is not available, or that
3746 the 'p' packet must be used. */
3747 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
3749 rsa
->sizeof_g_packet
= buf_len
/ 2;
3751 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
3753 if (rsa
->regs
[i
].pnum
== -1)
3756 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
3757 rsa
->regs
[i
].in_g_packet
= 0;
3759 rsa
->regs
[i
].in_g_packet
= 1;
3763 regs
= alloca (rsa
->sizeof_g_packet
);
3765 /* Unimplemented registers read as all bits zero. */
3766 memset (regs
, 0, rsa
->sizeof_g_packet
);
3768 /* Reply describes registers byte by byte, each byte encoded as two
3769 hex characters. Suck them all up, then supply them to the
3770 register cacheing/storage mechanism. */
3773 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
3775 if (p
[0] == 0 || p
[1] == 0)
3776 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
3777 internal_error (__FILE__
, __LINE__
,
3778 "unexpected end of 'g' packet reply");
3780 if (p
[0] == 'x' && p
[1] == 'x')
3781 regs
[i
] = 0; /* 'x' */
3783 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3789 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
3791 struct packet_reg
*r
= &rsa
->regs
[i
];
3794 if (r
->offset
* 2 >= strlen (rs
->buf
))
3795 /* This shouldn't happen - we adjusted in_g_packet above. */
3796 internal_error (__FILE__
, __LINE__
,
3797 "unexpected end of 'g' packet reply");
3798 else if (rs
->buf
[r
->offset
* 2] == 'x')
3800 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
3801 /* The register isn't available, mark it as such (at
3802 the same time setting the value to zero). */
3803 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
3806 regcache_raw_supply (regcache
, r
->regnum
,
3814 fetch_registers_using_g (struct regcache
*regcache
)
3817 process_g_packet (regcache
);
3821 remote_fetch_registers (struct regcache
*regcache
, int regnum
)
3823 struct remote_state
*rs
= get_remote_state ();
3824 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3827 set_general_thread (inferior_ptid
);
3831 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
3832 gdb_assert (reg
!= NULL
);
3834 /* If this register might be in the 'g' packet, try that first -
3835 we are likely to read more than one register. If this is the
3836 first 'g' packet, we might be overly optimistic about its
3837 contents, so fall back to 'p'. */
3838 if (reg
->in_g_packet
)
3840 fetch_registers_using_g (regcache
);
3841 if (reg
->in_g_packet
)
3845 if (fetch_register_using_p (regcache
, reg
))
3848 /* This register is not available. */
3849 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
3854 fetch_registers_using_g (regcache
);
3856 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
3857 if (!rsa
->regs
[i
].in_g_packet
)
3858 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
3860 /* This register is not available. */
3861 regcache_raw_supply (regcache
, i
, NULL
);
3865 /* Prepare to store registers. Since we may send them all (using a
3866 'G' request), we have to read out the ones we don't want to change
3870 remote_prepare_to_store (struct regcache
*regcache
)
3872 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3874 gdb_byte buf
[MAX_REGISTER_SIZE
];
3876 /* Make sure the entire registers array is valid. */
3877 switch (remote_protocol_packets
[PACKET_P
].support
)
3879 case PACKET_DISABLE
:
3880 case PACKET_SUPPORT_UNKNOWN
:
3881 /* Make sure all the necessary registers are cached. */
3882 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
3883 if (rsa
->regs
[i
].in_g_packet
)
3884 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
3891 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
3892 packet was not recognized. */
3895 store_register_using_P (const struct regcache
*regcache
, struct packet_reg
*reg
)
3897 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3898 struct remote_state
*rs
= get_remote_state ();
3899 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3900 /* Try storing a single register. */
3901 char *buf
= rs
->buf
;
3902 gdb_byte regp
[MAX_REGISTER_SIZE
];
3905 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
3908 if (reg
->pnum
== -1)
3911 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
3912 p
= buf
+ strlen (buf
);
3913 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
3914 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
3915 remote_send (&rs
->buf
, &rs
->buf_size
);
3917 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
3922 error (_("Could not write register \"%s\""),
3923 gdbarch_register_name (gdbarch
, reg
->regnum
));
3924 case PACKET_UNKNOWN
:
3927 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
3931 /* Store register REGNUM, or all registers if REGNUM == -1, from the
3932 contents of the register cache buffer. FIXME: ignores errors. */
3935 store_registers_using_G (const struct regcache
*regcache
)
3937 struct remote_state
*rs
= get_remote_state ();
3938 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3942 /* Extract all the registers in the regcache copying them into a
3946 regs
= alloca (rsa
->sizeof_g_packet
);
3947 memset (regs
, 0, rsa
->sizeof_g_packet
);
3948 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
3950 struct packet_reg
*r
= &rsa
->regs
[i
];
3952 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
3956 /* Command describes registers byte by byte,
3957 each byte encoded as two hex characters. */
3960 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
3962 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
3963 remote_send (&rs
->buf
, &rs
->buf_size
);
3966 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
3967 of the register cache buffer. FIXME: ignores errors. */
3970 remote_store_registers (struct regcache
*regcache
, int regnum
)
3972 struct remote_state
*rs
= get_remote_state ();
3973 struct remote_arch_state
*rsa
= get_remote_arch_state ();
3976 set_general_thread (inferior_ptid
);
3980 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
3981 gdb_assert (reg
!= NULL
);
3983 /* Always prefer to store registers using the 'P' packet if
3984 possible; we often change only a small number of registers.
3985 Sometimes we change a larger number; we'd need help from a
3986 higher layer to know to use 'G'. */
3987 if (store_register_using_P (regcache
, reg
))
3990 /* For now, don't complain if we have no way to write the
3991 register. GDB loses track of unavailable registers too
3992 easily. Some day, this may be an error. We don't have
3993 any way to read the register, either... */
3994 if (!reg
->in_g_packet
)
3997 store_registers_using_G (regcache
);
4001 store_registers_using_G (regcache
);
4003 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
4004 if (!rsa
->regs
[i
].in_g_packet
)
4005 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
4006 /* See above for why we do not issue an error here. */
4011 /* Return the number of hex digits in num. */
4014 hexnumlen (ULONGEST num
)
4018 for (i
= 0; num
!= 0; i
++)
4024 /* Set BUF to the minimum number of hex digits representing NUM. */
4027 hexnumstr (char *buf
, ULONGEST num
)
4029 int len
= hexnumlen (num
);
4030 return hexnumnstr (buf
, num
, len
);
4034 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
4037 hexnumnstr (char *buf
, ULONGEST num
, int width
)
4043 for (i
= width
- 1; i
>= 0; i
--)
4045 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
4052 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
4055 remote_address_masked (CORE_ADDR addr
)
4057 int address_size
= remote_address_size
;
4058 /* If "remoteaddresssize" was not set, default to target address size. */
4060 address_size
= gdbarch_addr_bit (current_gdbarch
);
4062 if (address_size
> 0
4063 && address_size
< (sizeof (ULONGEST
) * 8))
4065 /* Only create a mask when that mask can safely be constructed
4066 in a ULONGEST variable. */
4068 mask
= (mask
<< address_size
) - 1;
4074 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
4075 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
4076 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
4077 (which may be more than *OUT_LEN due to escape characters). The
4078 total number of bytes in the output buffer will be at most
4082 remote_escape_output (const gdb_byte
*buffer
, int len
,
4083 gdb_byte
*out_buf
, int *out_len
,
4086 int input_index
, output_index
;
4089 for (input_index
= 0; input_index
< len
; input_index
++)
4091 gdb_byte b
= buffer
[input_index
];
4093 if (b
== '$' || b
== '#' || b
== '}')
4095 /* These must be escaped. */
4096 if (output_index
+ 2 > out_maxlen
)
4098 out_buf
[output_index
++] = '}';
4099 out_buf
[output_index
++] = b
^ 0x20;
4103 if (output_index
+ 1 > out_maxlen
)
4105 out_buf
[output_index
++] = b
;
4109 *out_len
= input_index
;
4110 return output_index
;
4113 /* Convert BUFFER, escaped data LEN bytes long, into binary data
4114 in OUT_BUF. Return the number of bytes written to OUT_BUF.
4115 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
4117 This function reverses remote_escape_output. It allows more
4118 escaped characters than that function does, in particular because
4119 '*' must be escaped to avoid the run-length encoding processing
4120 in reading packets. */
4123 remote_unescape_input (const gdb_byte
*buffer
, int len
,
4124 gdb_byte
*out_buf
, int out_maxlen
)
4126 int input_index
, output_index
;
4131 for (input_index
= 0; input_index
< len
; input_index
++)
4133 gdb_byte b
= buffer
[input_index
];
4135 if (output_index
+ 1 > out_maxlen
)
4137 warning (_("Received too much data from remote target;"
4138 " ignoring overflow."));
4139 return output_index
;
4144 out_buf
[output_index
++] = b
^ 0x20;
4150 out_buf
[output_index
++] = b
;
4154 error (_("Unmatched escape character in target response."));
4156 return output_index
;
4159 /* Determine whether the remote target supports binary downloading.
4160 This is accomplished by sending a no-op memory write of zero length
4161 to the target at the specified address. It does not suffice to send
4162 the whole packet, since many stubs strip the eighth bit and
4163 subsequently compute a wrong checksum, which causes real havoc with
4166 NOTE: This can still lose if the serial line is not eight-bit
4167 clean. In cases like this, the user should clear "remote
4171 check_binary_download (CORE_ADDR addr
)
4173 struct remote_state
*rs
= get_remote_state ();
4175 switch (remote_protocol_packets
[PACKET_X
].support
)
4177 case PACKET_DISABLE
:
4181 case PACKET_SUPPORT_UNKNOWN
:
4187 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4189 p
+= hexnumstr (p
, (ULONGEST
) 0);
4193 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
4194 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4196 if (rs
->buf
[0] == '\0')
4199 fprintf_unfiltered (gdb_stdlog
,
4200 "binary downloading NOT suppported by target\n");
4201 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
4206 fprintf_unfiltered (gdb_stdlog
,
4207 "binary downloading suppported by target\n");
4208 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
4215 /* Write memory data directly to the remote machine.
4216 This does not inform the data cache; the data cache uses this.
4217 HEADER is the starting part of the packet.
4218 MEMADDR is the address in the remote memory space.
4219 MYADDR is the address of the buffer in our space.
4220 LEN is the number of bytes.
4221 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
4222 should send data as binary ('X'), or hex-encoded ('M').
4224 The function creates packet of the form
4225 <HEADER><ADDRESS>,<LENGTH>:<DATA>
4227 where encoding of <DATA> is termined by PACKET_FORMAT.
4229 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
4232 Returns the number of bytes transferred, or 0 (setting errno) for
4233 error. Only transfer a single packet. */
4236 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
4237 const gdb_byte
*myaddr
, int len
,
4238 char packet_format
, int use_length
)
4240 struct remote_state
*rs
= get_remote_state ();
4250 if (packet_format
!= 'X' && packet_format
!= 'M')
4251 internal_error (__FILE__
, __LINE__
,
4252 "remote_write_bytes_aux: bad packet format");
4257 payload_size
= get_memory_write_packet_size ();
4259 /* The packet buffer will be large enough for the payload;
4260 get_memory_packet_size ensures this. */
4263 /* Compute the size of the actual payload by subtracting out the
4264 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
4266 payload_size
-= strlen ("$,:#NN");
4268 /* The comma won't be used. */
4270 header_length
= strlen (header
);
4271 payload_size
-= header_length
;
4272 payload_size
-= hexnumlen (memaddr
);
4274 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
4276 strcat (rs
->buf
, header
);
4277 p
= rs
->buf
+ strlen (header
);
4279 /* Compute a best guess of the number of bytes actually transfered. */
4280 if (packet_format
== 'X')
4282 /* Best guess at number of bytes that will fit. */
4283 todo
= min (len
, payload_size
);
4285 payload_size
-= hexnumlen (todo
);
4286 todo
= min (todo
, payload_size
);
4290 /* Num bytes that will fit. */
4291 todo
= min (len
, payload_size
/ 2);
4293 payload_size
-= hexnumlen (todo
);
4294 todo
= min (todo
, payload_size
/ 2);
4298 internal_error (__FILE__
, __LINE__
,
4299 _("minumum packet size too small to write data"));
4301 /* If we already need another packet, then try to align the end
4302 of this packet to a useful boundary. */
4303 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
4304 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
4306 /* Append "<memaddr>". */
4307 memaddr
= remote_address_masked (memaddr
);
4308 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
4315 /* Append <len>. Retain the location/size of <len>. It may need to
4316 be adjusted once the packet body has been created. */
4318 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
4326 /* Append the packet body. */
4327 if (packet_format
== 'X')
4329 /* Binary mode. Send target system values byte by byte, in
4330 increasing byte addresses. Only escape certain critical
4332 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
4335 /* If not all TODO bytes fit, then we'll need another packet. Make
4336 a second try to keep the end of the packet aligned. Don't do
4337 this if the packet is tiny. */
4338 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
4342 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
4344 if (new_nr_bytes
!= nr_bytes
)
4345 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
4350 p
+= payload_length
;
4351 if (use_length
&& nr_bytes
< todo
)
4353 /* Escape chars have filled up the buffer prematurely,
4354 and we have actually sent fewer bytes than planned.
4355 Fix-up the length field of the packet. Use the same
4356 number of characters as before. */
4357 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
4358 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
4363 /* Normal mode: Send target system values byte by byte, in
4364 increasing byte addresses. Each byte is encoded as a two hex
4366 nr_bytes
= bin2hex (myaddr
, p
, todo
);
4370 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
4371 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4373 if (rs
->buf
[0] == 'E')
4375 /* There is no correspondance between what the remote protocol
4376 uses for errors and errno codes. We would like a cleaner way
4377 of representing errors (big enough to include errno codes,
4378 bfd_error codes, and others). But for now just return EIO. */
4383 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
4384 fewer bytes than we'd planned. */
4388 /* Write memory data directly to the remote machine.
4389 This does not inform the data cache; the data cache uses this.
4390 MEMADDR is the address in the remote memory space.
4391 MYADDR is the address of the buffer in our space.
4392 LEN is the number of bytes.
4394 Returns number of bytes transferred, or 0 (setting errno) for
4395 error. Only transfer a single packet. */
4398 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
4400 char *packet_format
= 0;
4402 /* Check whether the target supports binary download. */
4403 check_binary_download (memaddr
);
4405 switch (remote_protocol_packets
[PACKET_X
].support
)
4408 packet_format
= "X";
4410 case PACKET_DISABLE
:
4411 packet_format
= "M";
4413 case PACKET_SUPPORT_UNKNOWN
:
4414 internal_error (__FILE__
, __LINE__
,
4415 _("remote_write_bytes: bad internal state"));
4417 internal_error (__FILE__
, __LINE__
, _("bad switch"));
4420 return remote_write_bytes_aux (packet_format
,
4421 memaddr
, myaddr
, len
, packet_format
[0], 1);
4424 /* Read memory data directly from the remote machine.
4425 This does not use the data cache; the data cache uses this.
4426 MEMADDR is the address in the remote memory space.
4427 MYADDR is the address of the buffer in our space.
4428 LEN is the number of bytes.
4430 Returns number of bytes transferred, or 0 for error. */
4432 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
4433 remote targets) shouldn't attempt to read the entire buffer.
4434 Instead it should read a single packet worth of data and then
4435 return the byte size of that packet to the caller. The caller (its
4436 caller and its callers caller ;-) already contains code for
4437 handling partial reads. */
4440 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
4442 struct remote_state
*rs
= get_remote_state ();
4443 int max_buf_size
; /* Max size of packet output buffer. */
4449 max_buf_size
= get_memory_read_packet_size ();
4450 /* The packet buffer will be large enough for the payload;
4451 get_memory_packet_size ensures this. */
4460 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
4462 /* construct "m"<memaddr>","<len>" */
4463 /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */
4464 memaddr
= remote_address_masked (memaddr
);
4467 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
4469 p
+= hexnumstr (p
, (ULONGEST
) todo
);
4473 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4475 if (rs
->buf
[0] == 'E'
4476 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
4477 && rs
->buf
[3] == '\0')
4479 /* There is no correspondance between what the remote
4480 protocol uses for errors and errno codes. We would like
4481 a cleaner way of representing errors (big enough to
4482 include errno codes, bfd_error codes, and others). But
4483 for now just return EIO. */
4488 /* Reply describes memory byte by byte,
4489 each byte encoded as two hex characters. */
4492 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
4494 /* Reply is short. This means that we were able to read
4495 only part of what we wanted to. */
4496 return i
+ (origlen
- len
);
4505 /* Read or write LEN bytes from inferior memory at MEMADDR,
4506 transferring to or from debugger address BUFFER. Write to inferior
4507 if SHOULD_WRITE is nonzero. Returns length of data written or
4508 read; 0 for error. TARGET is unused. */
4511 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
4512 int should_write
, struct mem_attrib
*attrib
,
4513 struct target_ops
*target
)
4518 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
4520 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
4525 /* Sends a packet with content determined by the printf format string
4526 FORMAT and the remaining arguments, then gets the reply. Returns
4527 whether the packet was a success, a failure, or unknown. */
4530 remote_send_printf (const char *format
, ...)
4532 struct remote_state
*rs
= get_remote_state ();
4533 int max_size
= get_remote_packet_size ();
4536 va_start (ap
, format
);
4539 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
4540 internal_error (__FILE__
, __LINE__
, "Too long remote packet.");
4542 if (putpkt (rs
->buf
) < 0)
4543 error (_("Communication problem with target."));
4546 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4548 return packet_check_result (rs
->buf
);
4552 restore_remote_timeout (void *p
)
4554 int value
= *(int *)p
;
4555 remote_timeout
= value
;
4558 /* Flash writing can take quite some time. We'll set
4559 effectively infinite timeout for flash operations.
4560 In future, we'll need to decide on a better approach. */
4561 static const int remote_flash_timeout
= 1000;
4564 remote_flash_erase (struct target_ops
*ops
,
4565 ULONGEST address
, LONGEST length
)
4567 int saved_remote_timeout
= remote_timeout
;
4568 enum packet_result ret
;
4570 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
4571 &saved_remote_timeout
);
4572 remote_timeout
= remote_flash_timeout
;
4574 ret
= remote_send_printf ("vFlashErase:%s,%s",
4579 case PACKET_UNKNOWN
:
4580 error (_("Remote target does not support flash erase"));
4582 error (_("Error erasing flash with vFlashErase packet"));
4587 do_cleanups (back_to
);
4591 remote_flash_write (struct target_ops
*ops
,
4592 ULONGEST address
, LONGEST length
,
4593 const gdb_byte
*data
)
4595 int saved_remote_timeout
= remote_timeout
;
4597 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
4598 &saved_remote_timeout
);
4600 remote_timeout
= remote_flash_timeout
;
4601 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
4602 do_cleanups (back_to
);
4608 remote_flash_done (struct target_ops
*ops
)
4610 int saved_remote_timeout
= remote_timeout
;
4612 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
4613 &saved_remote_timeout
);
4615 remote_timeout
= remote_flash_timeout
;
4616 ret
= remote_send_printf ("vFlashDone");
4617 do_cleanups (back_to
);
4621 case PACKET_UNKNOWN
:
4622 error (_("Remote target does not support vFlashDone"));
4624 error (_("Error finishing flash operation"));
4631 remote_files_info (struct target_ops
*ignore
)
4633 puts_filtered ("Debugging a target over a serial line.\n");
4636 /* Stuff for dealing with the packets which are part of this protocol.
4637 See comment at top of file for details. */
4639 /* Read a single character from the remote end. */
4642 readchar (int timeout
)
4646 ch
= serial_readchar (remote_desc
, timeout
);
4651 switch ((enum serial_rc
) ch
)
4654 target_mourn_inferior ();
4655 error (_("Remote connection closed"));
4658 perror_with_name (_("Remote communication error"));
4660 case SERIAL_TIMEOUT
:
4666 /* Send the command in *BUF to the remote machine, and read the reply
4667 into *BUF. Report an error if we get an error reply. Resize
4668 *BUF using xrealloc if necessary to hold the result, and update
4672 remote_send (char **buf
,
4676 getpkt (buf
, sizeof_buf
, 0);
4678 if ((*buf
)[0] == 'E')
4679 error (_("Remote failure reply: %s"), *buf
);
4682 /* Display a null-terminated packet on stdout, for debugging, using C
4686 print_packet (char *buf
)
4688 puts_filtered ("\"");
4689 fputstr_filtered (buf
, '"', gdb_stdout
);
4690 puts_filtered ("\"");
4696 return putpkt_binary (buf
, strlen (buf
));
4699 /* Send a packet to the remote machine, with error checking. The data
4700 of the packet is in BUF. The string in BUF can be at most
4701 get_remote_packet_size () - 5 to account for the $, # and checksum,
4702 and for a possible /0 if we are debugging (remote_debug) and want
4703 to print the sent packet as a string. */
4706 putpkt_binary (char *buf
, int cnt
)
4708 struct remote_state
*rs
= get_remote_state ();
4710 unsigned char csum
= 0;
4711 char *buf2
= alloca (cnt
+ 6);
4717 /* We're sending out a new packet. Make sure we don't look at a
4718 stale cached response. */
4719 rs
->cached_wait_status
= 0;
4721 /* Copy the packet into buffer BUF2, encapsulating it
4722 and giving it a checksum. */
4727 for (i
= 0; i
< cnt
; i
++)
4733 *p
++ = tohex ((csum
>> 4) & 0xf);
4734 *p
++ = tohex (csum
& 0xf);
4736 /* Send it over and over until we get a positive ack. */
4740 int started_error_output
= 0;
4745 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
4746 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
4747 fprintf_unfiltered (gdb_stdlog
, "...");
4748 gdb_flush (gdb_stdlog
);
4750 if (serial_write (remote_desc
, buf2
, p
- buf2
))
4751 perror_with_name (_("putpkt: write failed"));
4753 /* Read until either a timeout occurs (-2) or '+' is read. */
4756 ch
= readchar (remote_timeout
);
4764 case SERIAL_TIMEOUT
:
4766 if (started_error_output
)
4768 putchar_unfiltered ('\n');
4769 started_error_output
= 0;
4778 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
4782 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
4783 case SERIAL_TIMEOUT
:
4787 break; /* Retransmit buffer. */
4791 fprintf_unfiltered (gdb_stdlog
,
4792 "Packet instead of Ack, ignoring it\n");
4793 /* It's probably an old response sent because an ACK
4794 was lost. Gobble up the packet and ack it so it
4795 doesn't get retransmitted when we resend this
4798 serial_write (remote_desc
, "+", 1);
4799 continue; /* Now, go look for +. */
4804 if (!started_error_output
)
4806 started_error_output
= 1;
4807 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
4809 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
4813 break; /* Here to retransmit. */
4817 /* This is wrong. If doing a long backtrace, the user should be
4818 able to get out next time we call QUIT, without anything as
4819 violent as interrupt_query. If we want to provide a way out of
4820 here without getting to the next QUIT, it should be based on
4821 hitting ^C twice as in remote_wait. */
4831 /* Come here after finding the start of a frame when we expected an
4832 ack. Do our best to discard the rest of this packet. */
4841 c
= readchar (remote_timeout
);
4844 case SERIAL_TIMEOUT
:
4845 /* Nothing we can do. */
4848 /* Discard the two bytes of checksum and stop. */
4849 c
= readchar (remote_timeout
);
4851 c
= readchar (remote_timeout
);
4854 case '*': /* Run length encoding. */
4855 /* Discard the repeat count. */
4856 c
= readchar (remote_timeout
);
4861 /* A regular character. */
4867 /* Come here after finding the start of the frame. Collect the rest
4868 into *BUF, verifying the checksum, length, and handling run-length
4869 compression. NUL terminate the buffer. If there is not enough room,
4870 expand *BUF using xrealloc.
4872 Returns -1 on error, number of characters in buffer (ignoring the
4873 trailing NULL) on success. (could be extended to return one of the
4874 SERIAL status indications). */
4877 read_frame (char **buf_p
,
4890 c
= readchar (remote_timeout
);
4893 case SERIAL_TIMEOUT
:
4895 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
4899 fputs_filtered ("Saw new packet start in middle of old one\n",
4901 return -1; /* Start a new packet, count retries. */
4904 unsigned char pktcsum
;
4910 check_0
= readchar (remote_timeout
);
4912 check_1
= readchar (remote_timeout
);
4914 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
4917 fputs_filtered ("Timeout in checksum, retrying\n",
4921 else if (check_0
< 0 || check_1
< 0)
4924 fputs_filtered ("Communication error in checksum\n",
4929 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
4930 if (csum
== pktcsum
)
4935 fprintf_filtered (gdb_stdlog
,
4936 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4938 fputstrn_filtered (buf
, bc
, 0, gdb_stdlog
);
4939 fputs_filtered ("\n", gdb_stdlog
);
4941 /* Number of characters in buffer ignoring trailing
4945 case '*': /* Run length encoding. */
4950 c
= readchar (remote_timeout
);
4952 repeat
= c
- ' ' + 3; /* Compute repeat count. */
4954 /* The character before ``*'' is repeated. */
4956 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
4958 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
4960 /* Make some more room in the buffer. */
4961 *sizeof_buf
+= repeat
;
4962 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
4966 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
4972 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
4976 if (bc
>= *sizeof_buf
- 1)
4978 /* Make some more room in the buffer. */
4980 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
4991 /* Read a packet from the remote machine, with error checking, and
4992 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4993 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4994 rather than timing out; this is used (in synchronous mode) to wait
4995 for a target that is is executing user code to stop. */
4996 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4997 don't have to change all the calls to getpkt to deal with the
4998 return value, because at the moment I don't know what the right
4999 thing to do it for those. */
5007 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
5011 /* Read a packet from the remote machine, with error checking, and
5012 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
5013 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
5014 rather than timing out; this is used (in synchronous mode) to wait
5015 for a target that is is executing user code to stop. If FOREVER ==
5016 0, this function is allowed to time out gracefully and return an
5017 indication of this to the caller. Otherwise return the number
5020 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
5022 struct remote_state
*rs
= get_remote_state ();
5028 /* We're reading a new response. Make sure we don't look at a
5029 previously cached response. */
5030 rs
->cached_wait_status
= 0;
5032 strcpy (*buf
, "timeout");
5036 timeout
= watchdog
> 0 ? watchdog
: -1;
5040 timeout
= remote_timeout
;
5044 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
5046 /* This can loop forever if the remote side sends us characters
5047 continuously, but if it pauses, we'll get a zero from
5048 readchar because of timeout. Then we'll count that as a
5051 /* Note that we will only wait forever prior to the start of a
5052 packet. After that, we expect characters to arrive at a
5053 brisk pace. They should show up within remote_timeout
5058 c
= readchar (timeout
);
5060 if (c
== SERIAL_TIMEOUT
)
5062 if (forever
) /* Watchdog went off? Kill the target. */
5065 target_mourn_inferior ();
5066 error (_("Watchdog timeout has expired. Target detached."));
5069 fputs_filtered ("Timed out.\n", gdb_stdlog
);
5075 /* We've found the start of a packet, now collect the data. */
5077 val
= read_frame (buf
, sizeof_buf
);
5083 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
5084 fputstrn_unfiltered (*buf
, val
, 0, gdb_stdlog
);
5085 fprintf_unfiltered (gdb_stdlog
, "\n");
5087 serial_write (remote_desc
, "+", 1);
5091 /* Try the whole thing again. */
5093 serial_write (remote_desc
, "-", 1);
5096 /* We have tried hard enough, and just can't receive the packet.
5099 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
5100 serial_write (remote_desc
, "+", 1);
5107 /* Unregister the file descriptor from the event loop. */
5108 if (target_is_async_p ())
5109 serial_async (remote_desc
, NULL
, 0);
5111 /* Use catch_errors so the user can quit from gdb even when we
5112 aren't on speaking terms with the remote system. */
5113 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
5115 /* Don't wait for it to die. I'm not really sure it matters whether
5116 we do or not. For the existing stubs, kill is a noop. */
5117 target_mourn_inferior ();
5123 remote_mourn_1 (&remote_ops
);
5126 /* Worker function for remote_mourn. */
5128 remote_mourn_1 (struct target_ops
*target
)
5130 unpush_target (target
);
5131 generic_mourn_inferior ();
5135 extended_remote_mourn_1 (struct target_ops
*target
)
5137 struct remote_state
*rs
= get_remote_state ();
5139 /* Unlike "target remote", we do not want to unpush the target; then
5140 the next time the user says "run", we won't be connected. */
5142 /* Call common code to mark the inferior as not running. */
5143 generic_mourn_inferior ();
5145 /* Check whether the target is running now - some remote stubs
5146 automatically restart after kill. */
5148 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5150 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
5152 /* Assume that the target has been restarted. Set inferior_ptid
5153 so that bits of core GDB realizes there's something here, e.g.,
5154 so that the user can say "kill" again. */
5155 inferior_ptid
= magic_null_ptid
;
5159 /* Mark this (still pushed) target as not executable until we
5161 target_mark_exited (target
);
5166 extended_remote_mourn (void)
5168 extended_remote_mourn_1 (&extended_remote_ops
);
5172 extended_remote_run (char *args
)
5174 struct remote_state
*rs
= get_remote_state ();
5178 /* If the user has disabled vRun support, or we have detected that
5179 support is not available, do not try it. */
5180 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
5183 strcpy (rs
->buf
, "vRun;");
5184 len
= strlen (rs
->buf
);
5186 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
5187 error (_("Remote file name too long for run packet"));
5188 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
, 0);
5192 struct cleanup
*back_to
;
5196 argv
= buildargv (args
);
5197 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
5198 for (i
= 0; argv
[i
] != NULL
; i
++)
5200 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
5201 error (_("Argument list too long for run packet"));
5202 rs
->buf
[len
++] = ';';
5203 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
, 0);
5205 do_cleanups (back_to
);
5208 rs
->buf
[len
++] = '\0';
5211 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5213 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
5215 /* We have a wait response; we don't need it, though. All is well. */
5218 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
5219 /* It wasn't disabled before, but it is now. */
5223 if (remote_exec_file
[0] == '\0')
5224 error (_("Running the default executable on the remote target failed; "
5225 "try \"set remote exec-file\"?"));
5227 error (_("Running \"%s\" on the remote target failed"),
5232 /* In the extended protocol we want to be able to do things like
5233 "run" and have them basically work as expected. So we need
5234 a special create_inferior function. We support changing the
5235 executable file and the command line arguments, but not the
5239 extended_remote_create_inferior_1 (char *exec_file
, char *args
,
5240 char **env
, int from_tty
)
5242 /* If running asynchronously, register the target file descriptor
5243 with the event loop. */
5244 if (target_can_async_p ())
5245 target_async (inferior_event_handler
, 0);
5247 /* Now restart the remote server. */
5248 if (extended_remote_run (args
) == -1)
5250 /* vRun was not supported. Fail if we need it to do what the
5252 if (remote_exec_file
[0])
5253 error (_("Remote target does not support \"set remote exec-file\""));
5255 error (_("Remote target does not support \"set args\" or run <ARGS>"));
5257 /* Fall back to "R". */
5258 extended_remote_restart ();
5261 /* Clean up from the last time we ran, before we mark the target
5262 running again. This will mark breakpoints uninserted, and
5263 get_offsets may insert breakpoints. */
5264 init_thread_list ();
5265 init_wait_for_inferior ();
5267 /* Now mark the inferior as running before we do anything else. */
5269 inferior_ptid
= magic_null_ptid
;
5270 target_mark_running (&extended_remote_ops
);
5272 /* Get updated offsets, if the stub uses qOffsets. */
5277 extended_remote_create_inferior (char *exec_file
, char *args
,
5278 char **env
, int from_tty
)
5280 extended_remote_create_inferior_1 (exec_file
, args
, env
, from_tty
);
5284 /* Insert a breakpoint. On targets that have software breakpoint
5285 support, we ask the remote target to do the work; on targets
5286 which don't, we insert a traditional memory breakpoint. */
5289 remote_insert_breakpoint (struct bp_target_info
*bp_tgt
)
5291 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
5292 If it succeeds, then set the support to PACKET_ENABLE. If it
5293 fails, and the user has explicitly requested the Z support then
5294 report an error, otherwise, mark it disabled and go on. */
5296 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
5299 struct remote_state
*rs
;
5302 gdbarch_breakpoint_from_pc
5303 (current_gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
5305 rs
= get_remote_state ();
5311 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
5312 p
+= hexnumstr (p
, addr
);
5313 sprintf (p
, ",%d", bp_tgt
->placed_size
);
5316 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5318 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
5324 case PACKET_UNKNOWN
:
5329 return memory_insert_breakpoint (bp_tgt
);
5333 remote_remove_breakpoint (struct bp_target_info
*bp_tgt
)
5335 CORE_ADDR addr
= bp_tgt
->placed_address
;
5336 struct remote_state
*rs
= get_remote_state ();
5339 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
5347 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
5348 p
+= hexnumstr (p
, addr
);
5349 sprintf (p
, ",%d", bp_tgt
->placed_size
);
5352 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5354 return (rs
->buf
[0] == 'E');
5357 return memory_remove_breakpoint (bp_tgt
);
5361 watchpoint_to_Z_packet (int type
)
5366 return Z_PACKET_WRITE_WP
;
5369 return Z_PACKET_READ_WP
;
5372 return Z_PACKET_ACCESS_WP
;
5375 internal_error (__FILE__
, __LINE__
,
5376 _("hw_bp_to_z: bad watchpoint type %d"), type
);
5381 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
5383 struct remote_state
*rs
= get_remote_state ();
5385 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
5387 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
5390 sprintf (rs
->buf
, "Z%x,", packet
);
5391 p
= strchr (rs
->buf
, '\0');
5392 addr
= remote_address_masked (addr
);
5393 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5394 sprintf (p
, ",%x", len
);
5397 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5399 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
5402 case PACKET_UNKNOWN
:
5407 internal_error (__FILE__
, __LINE__
,
5408 _("remote_insert_watchpoint: reached end of function"));
5413 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
5415 struct remote_state
*rs
= get_remote_state ();
5417 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
5419 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
5422 sprintf (rs
->buf
, "z%x,", packet
);
5423 p
= strchr (rs
->buf
, '\0');
5424 addr
= remote_address_masked (addr
);
5425 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5426 sprintf (p
, ",%x", len
);
5428 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5430 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
5433 case PACKET_UNKNOWN
:
5438 internal_error (__FILE__
, __LINE__
,
5439 _("remote_remove_watchpoint: reached end of function"));
5443 int remote_hw_watchpoint_limit
= -1;
5444 int remote_hw_breakpoint_limit
= -1;
5447 remote_check_watch_resources (int type
, int cnt
, int ot
)
5449 if (type
== bp_hardware_breakpoint
)
5451 if (remote_hw_breakpoint_limit
== 0)
5453 else if (remote_hw_breakpoint_limit
< 0)
5455 else if (cnt
<= remote_hw_breakpoint_limit
)
5460 if (remote_hw_watchpoint_limit
== 0)
5462 else if (remote_hw_watchpoint_limit
< 0)
5466 else if (cnt
<= remote_hw_watchpoint_limit
)
5473 remote_stopped_by_watchpoint (void)
5475 return remote_stopped_by_watchpoint_p
;
5479 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
5482 if (remote_stopped_by_watchpoint ())
5484 *addr_p
= remote_watch_data_address
;
5493 remote_insert_hw_breakpoint (struct bp_target_info
*bp_tgt
)
5496 struct remote_state
*rs
;
5499 /* The length field should be set to the size of a breakpoint
5500 instruction, even though we aren't inserting one ourselves. */
5502 gdbarch_breakpoint_from_pc
5503 (current_gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
5505 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
5508 rs
= get_remote_state ();
5515 addr
= remote_address_masked (bp_tgt
->placed_address
);
5516 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5517 sprintf (p
, ",%x", bp_tgt
->placed_size
);
5520 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5522 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
5525 case PACKET_UNKNOWN
:
5530 internal_error (__FILE__
, __LINE__
,
5531 _("remote_insert_hw_breakpoint: reached end of function"));
5536 remote_remove_hw_breakpoint (struct bp_target_info
*bp_tgt
)
5539 struct remote_state
*rs
= get_remote_state ();
5542 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
5549 addr
= remote_address_masked (bp_tgt
->placed_address
);
5550 p
+= hexnumstr (p
, (ULONGEST
) addr
);
5551 sprintf (p
, ",%x", bp_tgt
->placed_size
);
5554 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5556 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
5559 case PACKET_UNKNOWN
:
5564 internal_error (__FILE__
, __LINE__
,
5565 _("remote_remove_hw_breakpoint: reached end of function"));
5568 /* Some targets are only capable of doing downloads, and afterwards
5569 they switch to the remote serial protocol. This function provides
5570 a clean way to get from the download target to the remote target.
5571 It's basically just a wrapper so that we don't have to expose any
5572 of the internal workings of remote.c.
5574 Prior to calling this routine, you should shutdown the current
5575 target code, else you will get the "A program is being debugged
5576 already..." message. Usually a call to pop_target() suffices. */
5579 push_remote_target (char *name
, int from_tty
)
5581 printf_filtered (_("Switching to remote protocol\n"));
5582 remote_open (name
, from_tty
);
5585 /* Table used by the crc32 function to calcuate the checksum. */
5587 static unsigned long crc32_table
[256] =
5590 static unsigned long
5591 crc32 (unsigned char *buf
, int len
, unsigned int crc
)
5593 if (!crc32_table
[1])
5595 /* Initialize the CRC table and the decoding table. */
5599 for (i
= 0; i
< 256; i
++)
5601 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
5602 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
5609 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
5615 /* compare-sections command
5617 With no arguments, compares each loadable section in the exec bfd
5618 with the same memory range on the target, and reports mismatches.
5619 Useful for verifying the image on the target against the exec file.
5620 Depends on the target understanding the new "qCRC:" request. */
5622 /* FIXME: cagney/1999-10-26: This command should be broken down into a
5623 target method (target verify memory) and generic version of the
5624 actual command. This will allow other high-level code (especially
5625 generic_load()) to make use of this target functionality. */
5628 compare_sections_command (char *args
, int from_tty
)
5630 struct remote_state
*rs
= get_remote_state ();
5632 unsigned long host_crc
, target_crc
;
5633 extern bfd
*exec_bfd
;
5634 struct cleanup
*old_chain
;
5637 const char *sectname
;
5644 error (_("command cannot be used without an exec file"));
5645 if (!current_target
.to_shortname
||
5646 strcmp (current_target
.to_shortname
, "remote") != 0)
5647 error (_("command can only be used with remote target"));
5649 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
5651 if (!(s
->flags
& SEC_LOAD
))
5652 continue; /* skip non-loadable section */
5654 size
= bfd_get_section_size (s
);
5656 continue; /* skip zero-length section */
5658 sectname
= bfd_get_section_name (exec_bfd
, s
);
5659 if (args
&& strcmp (args
, sectname
) != 0)
5660 continue; /* not the section selected by user */
5662 matched
= 1; /* do this section */
5664 /* FIXME: assumes lma can fit into long. */
5665 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
5666 (long) lma
, (long) size
);
5669 /* Be clever; compute the host_crc before waiting for target
5671 sectdata
= xmalloc (size
);
5672 old_chain
= make_cleanup (xfree
, sectdata
);
5673 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
5674 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
5676 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5677 if (rs
->buf
[0] == 'E')
5678 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
5679 sectname
, paddr (lma
), paddr (lma
+ size
));
5680 if (rs
->buf
[0] != 'C')
5681 error (_("remote target does not support this operation"));
5683 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
5684 target_crc
= target_crc
* 16 + fromhex (*tmp
);
5686 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
5687 sectname
, paddr (lma
), paddr (lma
+ size
));
5688 if (host_crc
== target_crc
)
5689 printf_filtered ("matched.\n");
5692 printf_filtered ("MIS-MATCHED!\n");
5696 do_cleanups (old_chain
);
5699 warning (_("One or more sections of the remote executable does not match\n\
5700 the loaded file\n"));
5701 if (args
&& !matched
)
5702 printf_filtered (_("No loaded section named '%s'.\n"), args
);
5705 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
5706 into remote target. The number of bytes written to the remote
5707 target is returned, or -1 for error. */
5710 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
5711 const char *annex
, const gdb_byte
*writebuf
,
5712 ULONGEST offset
, LONGEST len
,
5713 struct packet_config
*packet
)
5718 struct remote_state
*rs
= get_remote_state ();
5719 int max_size
= get_memory_write_packet_size ();
5721 if (packet
->support
== PACKET_DISABLE
)
5724 /* Insert header. */
5725 i
= snprintf (rs
->buf
, max_size
,
5726 "qXfer:%s:write:%s:%s:",
5727 object_name
, annex
? annex
: "",
5728 phex_nz (offset
, sizeof offset
));
5729 max_size
-= (i
+ 1);
5731 /* Escape as much data as fits into rs->buf. */
5732 buf_len
= remote_escape_output
5733 (writebuf
, len
, (rs
->buf
+ i
), &max_size
, max_size
);
5735 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
5736 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
5737 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
5740 unpack_varlen_hex (rs
->buf
, &n
);
5744 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
5745 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
5746 number of bytes read is returned, or 0 for EOF, or -1 for error.
5747 The number of bytes read may be less than LEN without indicating an
5748 EOF. PACKET is checked and updated to indicate whether the remote
5749 target supports this object. */
5752 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
5754 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
5755 struct packet_config
*packet
)
5757 static char *finished_object
;
5758 static char *finished_annex
;
5759 static ULONGEST finished_offset
;
5761 struct remote_state
*rs
= get_remote_state ();
5762 unsigned int total
= 0;
5763 LONGEST i
, n
, packet_len
;
5765 if (packet
->support
== PACKET_DISABLE
)
5768 /* Check whether we've cached an end-of-object packet that matches
5770 if (finished_object
)
5772 if (strcmp (object_name
, finished_object
) == 0
5773 && strcmp (annex
? annex
: "", finished_annex
) == 0
5774 && offset
== finished_offset
)
5777 /* Otherwise, we're now reading something different. Discard
5779 xfree (finished_object
);
5780 xfree (finished_annex
);
5781 finished_object
= NULL
;
5782 finished_annex
= NULL
;
5785 /* Request only enough to fit in a single packet. The actual data
5786 may not, since we don't know how much of it will need to be escaped;
5787 the target is free to respond with slightly less data. We subtract
5788 five to account for the response type and the protocol frame. */
5789 n
= min (get_remote_packet_size () - 5, len
);
5790 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
5791 object_name
, annex
? annex
: "",
5792 phex_nz (offset
, sizeof offset
),
5793 phex_nz (n
, sizeof n
));
5794 i
= putpkt (rs
->buf
);
5799 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
5800 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
5803 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
5804 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
5806 /* 'm' means there is (or at least might be) more data after this
5807 batch. That does not make sense unless there's at least one byte
5808 of data in this reply. */
5809 if (rs
->buf
[0] == 'm' && packet_len
== 1)
5810 error (_("Remote qXfer reply contained no data."));
5812 /* Got some data. */
5813 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
5815 /* 'l' is an EOF marker, possibly including a final block of data,
5816 or possibly empty. If we have the final block of a non-empty
5817 object, record this fact to bypass a subsequent partial read. */
5818 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
5820 finished_object
= xstrdup (object_name
);
5821 finished_annex
= xstrdup (annex
? annex
: "");
5822 finished_offset
= offset
+ i
;
5829 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
5830 const char *annex
, gdb_byte
*readbuf
,
5831 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
5833 struct remote_state
*rs
= get_remote_state ();
5838 /* Handle memory using the standard memory routines. */
5839 if (object
== TARGET_OBJECT_MEMORY
)
5844 /* If the remote target is connected but not running, we should
5845 pass this request down to a lower stratum (e.g. the executable
5847 if (!target_has_execution
)
5850 if (writebuf
!= NULL
)
5851 xfered
= remote_write_bytes (offset
, writebuf
, len
);
5853 xfered
= remote_read_bytes (offset
, readbuf
, len
);
5857 else if (xfered
== 0 && errno
== 0)
5863 /* Handle SPU memory using qxfer packets. */
5864 if (object
== TARGET_OBJECT_SPU
)
5867 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
5868 &remote_protocol_packets
5869 [PACKET_qXfer_spu_read
]);
5871 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
5872 &remote_protocol_packets
5873 [PACKET_qXfer_spu_write
]);
5876 /* Only handle flash writes. */
5877 if (writebuf
!= NULL
)
5883 case TARGET_OBJECT_FLASH
:
5884 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
5888 else if (xfered
== 0 && errno
== 0)
5898 /* Map pre-existing objects onto letters. DO NOT do this for new
5899 objects!!! Instead specify new query packets. */
5902 case TARGET_OBJECT_AVR
:
5906 case TARGET_OBJECT_AUXV
:
5907 gdb_assert (annex
== NULL
);
5908 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
5909 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
5911 case TARGET_OBJECT_AVAILABLE_FEATURES
:
5912 return remote_read_qxfer
5913 (ops
, "features", annex
, readbuf
, offset
, len
,
5914 &remote_protocol_packets
[PACKET_qXfer_features
]);
5916 case TARGET_OBJECT_LIBRARIES
:
5917 return remote_read_qxfer
5918 (ops
, "libraries", annex
, readbuf
, offset
, len
,
5919 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
5921 case TARGET_OBJECT_MEMORY_MAP
:
5922 gdb_assert (annex
== NULL
);
5923 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
5924 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
5930 /* Note: a zero OFFSET and LEN can be used to query the minimum
5932 if (offset
== 0 && len
== 0)
5933 return (get_remote_packet_size ());
5934 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
5935 large enough let the caller deal with it. */
5936 if (len
< get_remote_packet_size ())
5938 len
= get_remote_packet_size ();
5940 /* Except for querying the minimum buffer size, target must be open. */
5942 error (_("remote query is only available after target open"));
5944 gdb_assert (annex
!= NULL
);
5945 gdb_assert (readbuf
!= NULL
);
5951 /* We used one buffer char for the remote protocol q command and
5952 another for the query type. As the remote protocol encapsulation
5953 uses 4 chars plus one extra in case we are debugging
5954 (remote_debug), we have PBUFZIZ - 7 left to pack the query
5957 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
5959 /* Bad caller may have sent forbidden characters. */
5960 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
5965 gdb_assert (annex
[i
] == '\0');
5967 i
= putpkt (rs
->buf
);
5971 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5972 strcpy ((char *) readbuf
, rs
->buf
);
5974 return strlen ((char *) readbuf
);
5978 remote_search_memory (struct target_ops
* ops
,
5979 CORE_ADDR start_addr
, ULONGEST search_space_len
,
5980 const gdb_byte
*pattern
, ULONGEST pattern_len
,
5981 CORE_ADDR
*found_addrp
)
5983 struct remote_state
*rs
= get_remote_state ();
5984 int max_size
= get_memory_write_packet_size ();
5985 struct packet_config
*packet
=
5986 &remote_protocol_packets
[PACKET_qSearch_memory
];
5987 /* number of packet bytes used to encode the pattern,
5988 this could be more than PATTERN_LEN due to escape characters */
5989 int escaped_pattern_len
;
5990 /* amount of pattern that was encodable in the packet */
5991 int used_pattern_len
;
5994 ULONGEST found_addr
;
5996 /* Don't go to the target if we don't have to.
5997 This is done before checking packet->support to avoid the possibility that
5998 a success for this edge case means the facility works in general. */
5999 if (pattern_len
> search_space_len
)
6001 if (pattern_len
== 0)
6003 *found_addrp
= start_addr
;
6007 /* If we already know the packet isn't supported, fall back to the simple
6008 way of searching memory. */
6010 if (packet
->support
== PACKET_DISABLE
)
6012 /* Target doesn't provided special support, fall back and use the
6013 standard support (copy memory and do the search here). */
6014 return simple_search_memory (ops
, start_addr
, search_space_len
,
6015 pattern
, pattern_len
, found_addrp
);
6018 /* Insert header. */
6019 i
= snprintf (rs
->buf
, max_size
,
6020 "qSearch:memory:%s;%s;",
6021 paddr_nz (start_addr
),
6022 phex_nz (search_space_len
, sizeof (search_space_len
)));
6023 max_size
-= (i
+ 1);
6025 /* Escape as much data as fits into rs->buf. */
6026 escaped_pattern_len
=
6027 remote_escape_output (pattern
, pattern_len
, (rs
->buf
+ i
),
6028 &used_pattern_len
, max_size
);
6030 /* Bail if the pattern is too large. */
6031 if (used_pattern_len
!= pattern_len
)
6032 error ("Pattern is too large to transmit to remote target.");
6034 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
6035 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
6036 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
6038 /* The request may not have worked because the command is not
6039 supported. If so, fall back to the simple way. */
6040 if (packet
->support
== PACKET_DISABLE
)
6042 return simple_search_memory (ops
, start_addr
, search_space_len
,
6043 pattern
, pattern_len
, found_addrp
);
6048 if (rs
->buf
[0] == '0')
6050 else if (rs
->buf
[0] == '1')
6053 if (rs
->buf
[1] != ',')
6054 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
6055 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
6056 *found_addrp
= found_addr
;
6059 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
6065 remote_rcmd (char *command
,
6066 struct ui_file
*outbuf
)
6068 struct remote_state
*rs
= get_remote_state ();
6072 error (_("remote rcmd is only available after target open"));
6074 /* Send a NULL command across as an empty command. */
6075 if (command
== NULL
)
6078 /* The query prefix. */
6079 strcpy (rs
->buf
, "qRcmd,");
6080 p
= strchr (rs
->buf
, '\0');
6082 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/) > get_remote_packet_size ())
6083 error (_("\"monitor\" command ``%s'' is too long."), command
);
6085 /* Encode the actual command. */
6086 bin2hex ((gdb_byte
*) command
, p
, 0);
6088 if (putpkt (rs
->buf
) < 0)
6089 error (_("Communication problem with target."));
6091 /* get/display the response */
6096 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
6098 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6101 error (_("Target does not support this command."));
6102 if (buf
[0] == 'O' && buf
[1] != 'K')
6104 remote_console_output (buf
+ 1); /* 'O' message from stub. */
6107 if (strcmp (buf
, "OK") == 0)
6109 if (strlen (buf
) == 3 && buf
[0] == 'E'
6110 && isdigit (buf
[1]) && isdigit (buf
[2]))
6112 error (_("Protocol error with Rcmd"));
6114 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
6116 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
6117 fputc_unfiltered (c
, outbuf
);
6123 static VEC(mem_region_s
) *
6124 remote_memory_map (struct target_ops
*ops
)
6126 VEC(mem_region_s
) *result
= NULL
;
6127 char *text
= target_read_stralloc (¤t_target
,
6128 TARGET_OBJECT_MEMORY_MAP
, NULL
);
6132 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
6133 result
= parse_memory_map (text
);
6134 do_cleanups (back_to
);
6141 packet_command (char *args
, int from_tty
)
6143 struct remote_state
*rs
= get_remote_state ();
6146 error (_("command can only be used with remote target"));
6149 error (_("remote-packet command requires packet text as argument"));
6151 puts_filtered ("sending: ");
6152 print_packet (args
);
6153 puts_filtered ("\n");
6156 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6157 puts_filtered ("received: ");
6158 print_packet (rs
->buf
);
6159 puts_filtered ("\n");
6163 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
6165 static void display_thread_info (struct gdb_ext_thread_info
*info
);
6167 static void threadset_test_cmd (char *cmd
, int tty
);
6169 static void threadalive_test (char *cmd
, int tty
);
6171 static void threadlist_test_cmd (char *cmd
, int tty
);
6173 int get_and_display_threadinfo (threadref
*ref
);
6175 static void threadinfo_test_cmd (char *cmd
, int tty
);
6177 static int thread_display_step (threadref
*ref
, void *context
);
6179 static void threadlist_update_test_cmd (char *cmd
, int tty
);
6181 static void init_remote_threadtests (void);
6183 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
6186 threadset_test_cmd (char *cmd
, int tty
)
6188 int sample_thread
= SAMPLE_THREAD
;
6190 printf_filtered (_("Remote threadset test\n"));
6191 set_general_thread (sample_thread
);
6196 threadalive_test (char *cmd
, int tty
)
6198 int sample_thread
= SAMPLE_THREAD
;
6199 int pid
= ptid_get_pid (inferior_ptid
);
6200 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
6202 if (remote_thread_alive (ptid
))
6203 printf_filtered ("PASS: Thread alive test\n");
6205 printf_filtered ("FAIL: Thread alive test\n");
6208 void output_threadid (char *title
, threadref
*ref
);
6211 output_threadid (char *title
, threadref
*ref
)
6215 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
6217 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
6221 threadlist_test_cmd (char *cmd
, int tty
)
6224 threadref nextthread
;
6225 int done
, result_count
;
6226 threadref threadlist
[3];
6228 printf_filtered ("Remote Threadlist test\n");
6229 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
6230 &result_count
, &threadlist
[0]))
6231 printf_filtered ("FAIL: threadlist test\n");
6234 threadref
*scan
= threadlist
;
6235 threadref
*limit
= scan
+ result_count
;
6237 while (scan
< limit
)
6238 output_threadid (" thread ", scan
++);
6243 display_thread_info (struct gdb_ext_thread_info
*info
)
6245 output_threadid ("Threadid: ", &info
->threadid
);
6246 printf_filtered ("Name: %s\n ", info
->shortname
);
6247 printf_filtered ("State: %s\n", info
->display
);
6248 printf_filtered ("other: %s\n\n", info
->more_display
);
6252 get_and_display_threadinfo (threadref
*ref
)
6256 struct gdb_ext_thread_info threadinfo
;
6258 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
6259 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
6260 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
6261 display_thread_info (&threadinfo
);
6266 threadinfo_test_cmd (char *cmd
, int tty
)
6268 int athread
= SAMPLE_THREAD
;
6272 int_to_threadref (&thread
, athread
);
6273 printf_filtered ("Remote Threadinfo test\n");
6274 if (!get_and_display_threadinfo (&thread
))
6275 printf_filtered ("FAIL cannot get thread info\n");
6279 thread_display_step (threadref
*ref
, void *context
)
6281 /* output_threadid(" threadstep ",ref); *//* simple test */
6282 return get_and_display_threadinfo (ref
);
6286 threadlist_update_test_cmd (char *cmd
, int tty
)
6288 printf_filtered ("Remote Threadlist update test\n");
6289 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
6293 init_remote_threadtests (void)
6295 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
6296 Fetch and print the remote list of thread identifiers, one pkt only"));
6297 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
6298 _("Fetch and display info about one thread"));
6299 add_com ("tset", class_obscure
, threadset_test_cmd
,
6300 _("Test setting to a different thread"));
6301 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
6302 _("Iterate through updating all remote thread info"));
6303 add_com ("talive", class_obscure
, threadalive_test
,
6304 _(" Remote thread alive test "));
6309 /* Convert a thread ID to a string. Returns the string in a static
6313 remote_pid_to_str (ptid_t ptid
)
6315 static char buf
[64];
6317 if (ptid_equal (magic_null_ptid
, ptid
))
6319 xsnprintf (buf
, sizeof buf
, "Thread <main>");
6322 else if (ptid_get_tid (ptid
) != 0)
6324 xsnprintf (buf
, sizeof buf
, "Thread %ld",
6325 ptid_get_tid (ptid
));
6329 return normal_pid_to_str (ptid
);
6332 /* Get the address of the thread local variable in OBJFILE which is
6333 stored at OFFSET within the thread local storage for thread PTID. */
6336 remote_get_thread_local_address (ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
6338 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
6340 struct remote_state
*rs
= get_remote_state ();
6342 enum packet_result result
;
6344 strcpy (p
, "qGetTLSAddr:");
6346 p
+= hexnumstr (p
, ptid_get_tid (ptid
));
6348 p
+= hexnumstr (p
, offset
);
6350 p
+= hexnumstr (p
, lm
);
6354 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6355 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
6356 if (result
== PACKET_OK
)
6360 unpack_varlen_hex (rs
->buf
, &result
);
6363 else if (result
== PACKET_UNKNOWN
)
6364 throw_error (TLS_GENERIC_ERROR
,
6365 _("Remote target doesn't support qGetTLSAddr packet"));
6367 throw_error (TLS_GENERIC_ERROR
,
6368 _("Remote target failed to process qGetTLSAddr request"));
6371 throw_error (TLS_GENERIC_ERROR
,
6372 _("TLS not supported or disabled on this target"));
6377 /* Support for inferring a target description based on the current
6378 architecture and the size of a 'g' packet. While the 'g' packet
6379 can have any size (since optional registers can be left off the
6380 end), some sizes are easily recognizable given knowledge of the
6381 approximate architecture. */
6383 struct remote_g_packet_guess
6386 const struct target_desc
*tdesc
;
6388 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
6389 DEF_VEC_O(remote_g_packet_guess_s
);
6391 struct remote_g_packet_data
6393 VEC(remote_g_packet_guess_s
) *guesses
;
6396 static struct gdbarch_data
*remote_g_packet_data_handle
;
6399 remote_g_packet_data_init (struct obstack
*obstack
)
6401 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
6405 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
6406 const struct target_desc
*tdesc
)
6408 struct remote_g_packet_data
*data
6409 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
6410 struct remote_g_packet_guess new_guess
, *guess
;
6413 gdb_assert (tdesc
!= NULL
);
6416 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
6418 if (guess
->bytes
== bytes
)
6419 internal_error (__FILE__
, __LINE__
,
6420 "Duplicate g packet description added for size %d",
6423 new_guess
.bytes
= bytes
;
6424 new_guess
.tdesc
= tdesc
;
6425 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
6428 static const struct target_desc
*
6429 remote_read_description (struct target_ops
*target
)
6431 struct remote_g_packet_data
*data
6432 = gdbarch_data (current_gdbarch
, remote_g_packet_data_handle
);
6434 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
6436 struct remote_g_packet_guess
*guess
;
6438 int bytes
= send_g_packet ();
6441 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
6443 if (guess
->bytes
== bytes
)
6444 return guess
->tdesc
;
6446 /* We discard the g packet. A minor optimization would be to
6447 hold on to it, and fill the register cache once we have selected
6448 an architecture, but it's too tricky to do safely. */
6454 /* Remote file transfer support. This is host-initiated I/O, not
6455 target-initiated; for target-initiated, see remote-fileio.c. */
6457 /* If *LEFT is at least the length of STRING, copy STRING to
6458 *BUFFER, update *BUFFER to point to the new end of the buffer, and
6459 decrease *LEFT. Otherwise raise an error. */
6462 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
6464 int len
= strlen (string
);
6467 error (_("Packet too long for target."));
6469 memcpy (*buffer
, string
, len
);
6473 /* NUL-terminate the buffer as a convenience, if there is
6479 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
6480 *BUFFER, update *BUFFER to point to the new end of the buffer, and
6481 decrease *LEFT. Otherwise raise an error. */
6484 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
6487 if (2 * len
> *left
)
6488 error (_("Packet too long for target."));
6490 bin2hex (bytes
, *buffer
, len
);
6494 /* NUL-terminate the buffer as a convenience, if there is
6500 /* If *LEFT is large enough, convert VALUE to hex and add it to
6501 *BUFFER, update *BUFFER to point to the new end of the buffer, and
6502 decrease *LEFT. Otherwise raise an error. */
6505 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
6507 int len
= hexnumlen (value
);
6510 error (_("Packet too long for target."));
6512 hexnumstr (*buffer
, value
);
6516 /* NUL-terminate the buffer as a convenience, if there is
6522 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
6523 value, *REMOTE_ERRNO to the remote error number or zero if none
6524 was included, and *ATTACHMENT to point to the start of the annex
6525 if any. The length of the packet isn't needed here; there may
6526 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
6528 Return 0 if the packet could be parsed, -1 if it could not. If
6529 -1 is returned, the other variables may not be initialized. */
6532 remote_hostio_parse_result (char *buffer
, int *retcode
,
6533 int *remote_errno
, char **attachment
)
6540 if (buffer
[0] != 'F')
6544 *retcode
= strtol (&buffer
[1], &p
, 16);
6545 if (errno
!= 0 || p
== &buffer
[1])
6548 /* Check for ",errno". */
6552 *remote_errno
= strtol (p
+ 1, &p2
, 16);
6553 if (errno
!= 0 || p
+ 1 == p2
)
6558 /* Check for ";attachment". If there is no attachment, the
6559 packet should end here. */
6562 *attachment
= p
+ 1;
6565 else if (*p
== '\0')
6571 /* Send a prepared I/O packet to the target and read its response.
6572 The prepared packet is in the global RS->BUF before this function
6573 is called, and the answer is there when we return.
6575 COMMAND_BYTES is the length of the request to send, which may include
6576 binary data. WHICH_PACKET is the packet configuration to check
6577 before attempting a packet. If an error occurs, *REMOTE_ERRNO
6578 is set to the error number and -1 is returned. Otherwise the value
6579 returned by the function is returned.
6581 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
6582 attachment is expected; an error will be reported if there's a
6583 mismatch. If one is found, *ATTACHMENT will be set to point into
6584 the packet buffer and *ATTACHMENT_LEN will be set to the
6585 attachment's length. */
6588 remote_hostio_send_command (int command_bytes
, int which_packet
,
6589 int *remote_errno
, char **attachment
,
6590 int *attachment_len
)
6592 struct remote_state
*rs
= get_remote_state ();
6593 int ret
, bytes_read
;
6594 char *attachment_tmp
;
6596 if (remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
6598 *remote_errno
= FILEIO_ENOSYS
;
6602 putpkt_binary (rs
->buf
, command_bytes
);
6603 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
6605 /* If it timed out, something is wrong. Don't try to parse the
6609 *remote_errno
= FILEIO_EINVAL
;
6613 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
6616 *remote_errno
= FILEIO_EINVAL
;
6618 case PACKET_UNKNOWN
:
6619 *remote_errno
= FILEIO_ENOSYS
;
6625 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
6628 *remote_errno
= FILEIO_EINVAL
;
6632 /* Make sure we saw an attachment if and only if we expected one. */
6633 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
6634 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
6636 *remote_errno
= FILEIO_EINVAL
;
6640 /* If an attachment was found, it must point into the packet buffer;
6641 work out how many bytes there were. */
6642 if (attachment_tmp
!= NULL
)
6644 *attachment
= attachment_tmp
;
6645 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
6651 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
6652 remote file descriptor, or -1 if an error occurs (and set
6656 remote_hostio_open (const char *filename
, int flags
, int mode
,
6659 struct remote_state
*rs
= get_remote_state ();
6661 int left
= get_remote_packet_size () - 1;
6663 remote_buffer_add_string (&p
, &left
, "vFile:open:");
6665 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
6667 remote_buffer_add_string (&p
, &left
, ",");
6669 remote_buffer_add_int (&p
, &left
, flags
);
6670 remote_buffer_add_string (&p
, &left
, ",");
6672 remote_buffer_add_int (&p
, &left
, mode
);
6674 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
6675 remote_errno
, NULL
, NULL
);
6678 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
6679 Return the number of bytes written, or -1 if an error occurs (and
6680 set *REMOTE_ERRNO). */
6683 remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
6684 ULONGEST offset
, int *remote_errno
)
6686 struct remote_state
*rs
= get_remote_state ();
6688 int left
= get_remote_packet_size ();
6691 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
6693 remote_buffer_add_int (&p
, &left
, fd
);
6694 remote_buffer_add_string (&p
, &left
, ",");
6696 remote_buffer_add_int (&p
, &left
, offset
);
6697 remote_buffer_add_string (&p
, &left
, ",");
6699 p
+= remote_escape_output (write_buf
, len
, p
, &out_len
,
6700 get_remote_packet_size () - (p
- rs
->buf
));
6702 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
6703 remote_errno
, NULL
, NULL
);
6706 /* Read up to LEN bytes FD on the remote target into READ_BUF
6707 Return the number of bytes read, or -1 if an error occurs (and
6708 set *REMOTE_ERRNO). */
6711 remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
6712 ULONGEST offset
, int *remote_errno
)
6714 struct remote_state
*rs
= get_remote_state ();
6717 int left
= get_remote_packet_size ();
6718 int ret
, attachment_len
;
6721 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
6723 remote_buffer_add_int (&p
, &left
, fd
);
6724 remote_buffer_add_string (&p
, &left
, ",");
6726 remote_buffer_add_int (&p
, &left
, len
);
6727 remote_buffer_add_string (&p
, &left
, ",");
6729 remote_buffer_add_int (&p
, &left
, offset
);
6731 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
6732 remote_errno
, &attachment
,
6738 read_len
= remote_unescape_input (attachment
, attachment_len
,
6740 if (read_len
!= ret
)
6741 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
6746 /* Close FD on the remote target. Return 0, or -1 if an error occurs
6747 (and set *REMOTE_ERRNO). */
6750 remote_hostio_close (int fd
, int *remote_errno
)
6752 struct remote_state
*rs
= get_remote_state ();
6754 int left
= get_remote_packet_size () - 1;
6756 remote_buffer_add_string (&p
, &left
, "vFile:close:");
6758 remote_buffer_add_int (&p
, &left
, fd
);
6760 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
6761 remote_errno
, NULL
, NULL
);
6764 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
6765 occurs (and set *REMOTE_ERRNO). */
6768 remote_hostio_unlink (const char *filename
, int *remote_errno
)
6770 struct remote_state
*rs
= get_remote_state ();
6772 int left
= get_remote_packet_size () - 1;
6774 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
6776 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
6779 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
6780 remote_errno
, NULL
, NULL
);
6784 remote_fileio_errno_to_host (int errnum
)
6808 case FILEIO_ENOTDIR
:
6828 case FILEIO_ENAMETOOLONG
:
6829 return ENAMETOOLONG
;
6835 remote_hostio_error (int errnum
)
6837 int host_error
= remote_fileio_errno_to_host (errnum
);
6839 if (host_error
== -1)
6840 error (_("Unknown remote I/O error %d"), errnum
);
6842 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
6846 fclose_cleanup (void *file
)
6852 remote_hostio_close_cleanup (void *opaque
)
6854 int fd
= *(int *) opaque
;
6857 remote_hostio_close (fd
, &remote_errno
);
6861 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
6863 struct cleanup
*back_to
, *close_cleanup
;
6864 int retcode
, fd
, remote_errno
, bytes
, io_size
;
6867 int bytes_in_buffer
;
6872 error (_("command can only be used with remote target"));
6874 file
= fopen (local_file
, "rb");
6876 perror_with_name (local_file
);
6877 back_to
= make_cleanup (fclose_cleanup
, file
);
6879 fd
= remote_hostio_open (remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
6881 0700, &remote_errno
);
6883 remote_hostio_error (remote_errno
);
6885 /* Send up to this many bytes at once. They won't all fit in the
6886 remote packet limit, so we'll transfer slightly fewer. */
6887 io_size
= get_remote_packet_size ();
6888 buffer
= xmalloc (io_size
);
6889 make_cleanup (xfree
, buffer
);
6891 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
6893 bytes_in_buffer
= 0;
6896 while (bytes_in_buffer
|| !saw_eof
)
6900 bytes
= fread (buffer
+ bytes_in_buffer
, 1, io_size
- bytes_in_buffer
,
6905 error (_("Error reading %s."), local_file
);
6908 /* EOF. Unless there is something still in the
6909 buffer from the last iteration, we are done. */
6911 if (bytes_in_buffer
== 0)
6919 bytes
+= bytes_in_buffer
;
6920 bytes_in_buffer
= 0;
6922 retcode
= remote_hostio_pwrite (fd
, buffer
, bytes
, offset
, &remote_errno
);
6925 remote_hostio_error (remote_errno
);
6926 else if (retcode
== 0)
6927 error (_("Remote write of %d bytes returned 0!"), bytes
);
6928 else if (retcode
< bytes
)
6930 /* Short write. Save the rest of the read data for the next
6932 bytes_in_buffer
= bytes
- retcode
;
6933 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
6939 discard_cleanups (close_cleanup
);
6940 if (remote_hostio_close (fd
, &remote_errno
))
6941 remote_hostio_error (remote_errno
);
6944 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
6945 do_cleanups (back_to
);
6949 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
6951 struct cleanup
*back_to
, *close_cleanup
;
6952 int retcode
, fd
, remote_errno
, bytes
, io_size
;
6958 error (_("command can only be used with remote target"));
6960 fd
= remote_hostio_open (remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
6962 remote_hostio_error (remote_errno
);
6964 file
= fopen (local_file
, "wb");
6966 perror_with_name (local_file
);
6967 back_to
= make_cleanup (fclose_cleanup
, file
);
6969 /* Send up to this many bytes at once. They won't all fit in the
6970 remote packet limit, so we'll transfer slightly fewer. */
6971 io_size
= get_remote_packet_size ();
6972 buffer
= xmalloc (io_size
);
6973 make_cleanup (xfree
, buffer
);
6975 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
6980 bytes
= remote_hostio_pread (fd
, buffer
, io_size
, offset
, &remote_errno
);
6982 /* Success, but no bytes, means end-of-file. */
6985 remote_hostio_error (remote_errno
);
6989 bytes
= fwrite (buffer
, 1, bytes
, file
);
6991 perror_with_name (local_file
);
6994 discard_cleanups (close_cleanup
);
6995 if (remote_hostio_close (fd
, &remote_errno
))
6996 remote_hostio_error (remote_errno
);
6999 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
7000 do_cleanups (back_to
);
7004 remote_file_delete (const char *remote_file
, int from_tty
)
7006 int retcode
, remote_errno
;
7009 error (_("command can only be used with remote target"));
7011 retcode
= remote_hostio_unlink (remote_file
, &remote_errno
);
7013 remote_hostio_error (remote_errno
);
7016 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
7020 remote_put_command (char *args
, int from_tty
)
7022 struct cleanup
*back_to
;
7025 argv
= buildargv (args
);
7028 back_to
= make_cleanup_freeargv (argv
);
7029 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
7030 error (_("Invalid parameters to remote put"));
7032 remote_file_put (argv
[0], argv
[1], from_tty
);
7034 do_cleanups (back_to
);
7038 remote_get_command (char *args
, int from_tty
)
7040 struct cleanup
*back_to
;
7043 argv
= buildargv (args
);
7046 back_to
= make_cleanup_freeargv (argv
);
7047 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
7048 error (_("Invalid parameters to remote get"));
7050 remote_file_get (argv
[0], argv
[1], from_tty
);
7052 do_cleanups (back_to
);
7056 remote_delete_command (char *args
, int from_tty
)
7058 struct cleanup
*back_to
;
7061 argv
= buildargv (args
);
7064 back_to
= make_cleanup_freeargv (argv
);
7065 if (argv
[0] == NULL
|| argv
[1] != NULL
)
7066 error (_("Invalid parameters to remote delete"));
7068 remote_file_delete (argv
[0], from_tty
);
7070 do_cleanups (back_to
);
7074 remote_command (char *args
, int from_tty
)
7076 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
7080 init_remote_ops (void)
7082 remote_ops
.to_shortname
= "remote";
7083 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
7085 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
7086 Specify the serial device it is connected to\n\
7087 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
7088 remote_ops
.to_open
= remote_open
;
7089 remote_ops
.to_close
= remote_close
;
7090 remote_ops
.to_detach
= remote_detach
;
7091 remote_ops
.to_disconnect
= remote_disconnect
;
7092 remote_ops
.to_resume
= remote_resume
;
7093 remote_ops
.to_wait
= remote_wait
;
7094 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
7095 remote_ops
.to_store_registers
= remote_store_registers
;
7096 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
7097 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
7098 remote_ops
.to_files_info
= remote_files_info
;
7099 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
7100 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
7101 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
7102 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
7103 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
7104 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
7105 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
7106 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
7107 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
7108 remote_ops
.to_kill
= remote_kill
;
7109 remote_ops
.to_load
= generic_load
;
7110 remote_ops
.to_mourn_inferior
= remote_mourn
;
7111 remote_ops
.to_thread_alive
= remote_thread_alive
;
7112 remote_ops
.to_find_new_threads
= remote_threads_info
;
7113 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
7114 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
7115 remote_ops
.to_stop
= remote_stop
;
7116 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
7117 remote_ops
.to_rcmd
= remote_rcmd
;
7118 remote_ops
.to_log_command
= serial_log_command
;
7119 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
7120 remote_ops
.to_stratum
= process_stratum
;
7121 remote_ops
.to_has_all_memory
= 1;
7122 remote_ops
.to_has_memory
= 1;
7123 remote_ops
.to_has_stack
= 1;
7124 remote_ops
.to_has_registers
= 1;
7125 remote_ops
.to_has_execution
= 1;
7126 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
7127 remote_ops
.to_magic
= OPS_MAGIC
;
7128 remote_ops
.to_memory_map
= remote_memory_map
;
7129 remote_ops
.to_flash_erase
= remote_flash_erase
;
7130 remote_ops
.to_flash_done
= remote_flash_done
;
7131 remote_ops
.to_read_description
= remote_read_description
;
7132 remote_ops
.to_search_memory
= remote_search_memory
;
7133 remote_ops
.to_can_async_p
= remote_can_async_p
;
7134 remote_ops
.to_is_async_p
= remote_is_async_p
;
7135 remote_ops
.to_async
= remote_async
;
7136 remote_ops
.to_async_mask
= remote_async_mask
;
7137 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
7138 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
7141 /* Set up the extended remote vector by making a copy of the standard
7142 remote vector and adding to it. */
7145 init_extended_remote_ops (void)
7147 extended_remote_ops
= remote_ops
;
7149 extended_remote_ops
.to_shortname
= "extended-remote";
7150 extended_remote_ops
.to_longname
=
7151 "Extended remote serial target in gdb-specific protocol";
7152 extended_remote_ops
.to_doc
=
7153 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
7154 Specify the serial device it is connected to (e.g. /dev/ttya).";
7155 extended_remote_ops
.to_open
= extended_remote_open
;
7156 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
7157 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
7158 extended_remote_ops
.to_detach
= extended_remote_detach
;
7159 extended_remote_ops
.to_attach
= extended_remote_attach
;
7163 remote_can_async_p (void)
7165 if (!remote_async_permitted
)
7166 /* We only enable async when the user specifically asks for it. */
7169 /* We're async whenever the serial device is. */
7170 return remote_async_mask_value
&& serial_can_async_p (remote_desc
);
7174 remote_is_async_p (void)
7176 if (!remote_async_permitted
)
7177 /* We only enable async when the user specifically asks for it. */
7180 /* We're async whenever the serial device is. */
7181 return remote_async_mask_value
&& serial_is_async_p (remote_desc
);
7184 /* Pass the SERIAL event on and up to the client. One day this code
7185 will be able to delay notifying the client of an event until the
7186 point where an entire packet has been received. */
7188 static void (*async_client_callback
) (enum inferior_event_type event_type
,
7190 static void *async_client_context
;
7191 static serial_event_ftype remote_async_serial_handler
;
7194 remote_async_serial_handler (struct serial
*scb
, void *context
)
7196 /* Don't propogate error information up to the client. Instead let
7197 the client find out about the error by querying the target. */
7198 async_client_callback (INF_REG_EVENT
, async_client_context
);
7202 remote_async (void (*callback
) (enum inferior_event_type event_type
,
7203 void *context
), void *context
)
7205 if (remote_async_mask_value
== 0)
7206 internal_error (__FILE__
, __LINE__
,
7207 _("Calling remote_async when async is masked"));
7209 if (callback
!= NULL
)
7211 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
7212 async_client_callback
= callback
;
7213 async_client_context
= context
;
7216 serial_async (remote_desc
, NULL
, NULL
);
7220 remote_async_mask (int new_mask
)
7222 int curr_mask
= remote_async_mask_value
;
7223 remote_async_mask_value
= new_mask
;
7228 set_remote_cmd (char *args
, int from_tty
)
7230 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
7234 show_remote_cmd (char *args
, int from_tty
)
7236 /* We can't just use cmd_show_list here, because we want to skip
7237 the redundant "show remote Z-packet" and the legacy aliases. */
7238 struct cleanup
*showlist_chain
;
7239 struct cmd_list_element
*list
= remote_show_cmdlist
;
7241 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
7242 for (; list
!= NULL
; list
= list
->next
)
7243 if (strcmp (list
->name
, "Z-packet") == 0)
7245 else if (list
->type
== not_set_cmd
)
7246 /* Alias commands are exactly like the original, except they
7247 don't have the normal type. */
7251 struct cleanup
*option_chain
7252 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
7253 ui_out_field_string (uiout
, "name", list
->name
);
7254 ui_out_text (uiout
, ": ");
7255 if (list
->type
== show_cmd
)
7256 do_setshow_command ((char *) NULL
, from_tty
, list
);
7258 cmd_func (list
, NULL
, from_tty
);
7259 /* Close the tuple. */
7260 do_cleanups (option_chain
);
7263 /* Close the tuple. */
7264 do_cleanups (showlist_chain
);
7268 /* Function to be called whenever a new objfile (shlib) is detected. */
7270 remote_new_objfile (struct objfile
*objfile
)
7272 if (remote_desc
!= 0) /* Have a remote connection. */
7273 remote_check_symbols (objfile
);
7277 _initialize_remote (void)
7279 struct remote_state
*rs
;
7281 /* architecture specific data */
7282 remote_gdbarch_data_handle
=
7283 gdbarch_data_register_post_init (init_remote_state
);
7284 remote_g_packet_data_handle
=
7285 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
7287 /* Initialize the per-target state. At the moment there is only one
7288 of these, not one per target. Only one target is active at a
7289 time. The default buffer size is unimportant; it will be expanded
7290 whenever a larger buffer is needed. */
7291 rs
= get_remote_state_raw ();
7293 rs
->buf
= xmalloc (rs
->buf_size
);
7296 add_target (&remote_ops
);
7298 init_extended_remote_ops ();
7299 add_target (&extended_remote_ops
);
7301 /* Hook into new objfile notification. */
7302 observer_attach_new_objfile (remote_new_objfile
);
7304 /* Set up signal handlers. */
7305 sigint_remote_token
=
7306 create_async_signal_handler (async_remote_interrupt
, NULL
);
7307 sigint_remote_twice_token
=
7308 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
7311 init_remote_threadtests ();
7314 /* set/show remote ... */
7316 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
7317 Remote protocol specific variables\n\
7318 Configure various remote-protocol specific variables such as\n\
7319 the packets being used"),
7320 &remote_set_cmdlist
, "set remote ",
7321 0 /* allow-unknown */, &setlist
);
7322 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
7323 Remote protocol specific variables\n\
7324 Configure various remote-protocol specific variables such as\n\
7325 the packets being used"),
7326 &remote_show_cmdlist
, "show remote ",
7327 0 /* allow-unknown */, &showlist
);
7329 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
7330 Compare section data on target to the exec file.\n\
7331 Argument is a single section name (default: all loaded sections)."),
7334 add_cmd ("packet", class_maintenance
, packet_command
, _("\
7335 Send an arbitrary packet to a remote target.\n\
7336 maintenance packet TEXT\n\
7337 If GDB is talking to an inferior via the GDB serial protocol, then\n\
7338 this command sends the string TEXT to the inferior, and displays the\n\
7339 response packet. GDB supplies the initial `$' character, and the\n\
7340 terminating `#' character and checksum."),
7343 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
7344 Set whether to send break if interrupted."), _("\
7345 Show whether to send break if interrupted."), _("\
7346 If set, a break, instead of a cntrl-c, is sent to the remote target."),
7347 NULL
, NULL
, /* FIXME: i18n: Whether to send break if interrupted is %s. */
7348 &setlist
, &showlist
);
7350 /* Install commands for configuring memory read/write packets. */
7352 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
7353 Set the maximum number of bytes per memory write packet (deprecated)."),
7355 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
7356 Show the maximum number of bytes per memory write packet (deprecated)."),
7358 add_cmd ("memory-write-packet-size", no_class
,
7359 set_memory_write_packet_size
, _("\
7360 Set the maximum number of bytes per memory-write packet.\n\
7361 Specify the number of bytes in a packet or 0 (zero) for the\n\
7362 default packet size. The actual limit is further reduced\n\
7363 dependent on the target. Specify ``fixed'' to disable the\n\
7364 further restriction and ``limit'' to enable that restriction."),
7365 &remote_set_cmdlist
);
7366 add_cmd ("memory-read-packet-size", no_class
,
7367 set_memory_read_packet_size
, _("\
7368 Set the maximum number of bytes per memory-read packet.\n\
7369 Specify the number of bytes in a packet or 0 (zero) for the\n\
7370 default packet size. The actual limit is further reduced\n\
7371 dependent on the target. Specify ``fixed'' to disable the\n\
7372 further restriction and ``limit'' to enable that restriction."),
7373 &remote_set_cmdlist
);
7374 add_cmd ("memory-write-packet-size", no_class
,
7375 show_memory_write_packet_size
,
7376 _("Show the maximum number of bytes per memory-write packet."),
7377 &remote_show_cmdlist
);
7378 add_cmd ("memory-read-packet-size", no_class
,
7379 show_memory_read_packet_size
,
7380 _("Show the maximum number of bytes per memory-read packet."),
7381 &remote_show_cmdlist
);
7383 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
7384 &remote_hw_watchpoint_limit
, _("\
7385 Set the maximum number of target hardware watchpoints."), _("\
7386 Show the maximum number of target hardware watchpoints."), _("\
7387 Specify a negative limit for unlimited."),
7388 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
7389 &remote_set_cmdlist
, &remote_show_cmdlist
);
7390 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
7391 &remote_hw_breakpoint_limit
, _("\
7392 Set the maximum number of target hardware breakpoints."), _("\
7393 Show the maximum number of target hardware breakpoints."), _("\
7394 Specify a negative limit for unlimited."),
7395 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
7396 &remote_set_cmdlist
, &remote_show_cmdlist
);
7398 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
7399 &remote_address_size
, _("\
7400 Set the maximum size of the address (in bits) in a memory packet."), _("\
7401 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
7403 NULL
, /* FIXME: i18n: */
7404 &setlist
, &showlist
);
7406 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
7407 "X", "binary-download", 1);
7409 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
7410 "vCont", "verbose-resume", 0);
7412 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
7413 "QPassSignals", "pass-signals", 0);
7415 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
7416 "qSymbol", "symbol-lookup", 0);
7418 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
7419 "P", "set-register", 1);
7421 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
7422 "p", "fetch-register", 1);
7424 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
7425 "Z0", "software-breakpoint", 0);
7427 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
7428 "Z1", "hardware-breakpoint", 0);
7430 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
7431 "Z2", "write-watchpoint", 0);
7433 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
7434 "Z3", "read-watchpoint", 0);
7436 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
7437 "Z4", "access-watchpoint", 0);
7439 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
7440 "qXfer:auxv:read", "read-aux-vector", 0);
7442 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
7443 "qXfer:features:read", "target-features", 0);
7445 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
7446 "qXfer:libraries:read", "library-info", 0);
7448 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
7449 "qXfer:memory-map:read", "memory-map", 0);
7451 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
7452 "qXfer:spu:read", "read-spu-object", 0);
7454 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
7455 "qXfer:spu:write", "write-spu-object", 0);
7457 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
7458 "qGetTLSAddr", "get-thread-local-storage-address",
7461 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
7462 "qSupported", "supported-packets", 0);
7464 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
7465 "qSearch:memory", "search-memory", 0);
7467 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
7468 "vFile:open", "hostio-open", 0);
7470 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
7471 "vFile:pread", "hostio-pread", 0);
7473 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
7474 "vFile:pwrite", "hostio-pwrite", 0);
7476 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
7477 "vFile:close", "hostio-close", 0);
7479 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
7480 "vFile:unlink", "hostio-unlink", 0);
7482 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
7483 "vAttach", "attach", 0);
7485 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
7488 /* Keep the old ``set remote Z-packet ...'' working. Each individual
7489 Z sub-packet has its own set and show commands, but users may
7490 have sets to this variable in their .gdbinit files (or in their
7492 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
7493 &remote_Z_packet_detect
, _("\
7494 Set use of remote protocol `Z' packets"), _("\
7495 Show use of remote protocol `Z' packets "), _("\
7496 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7498 set_remote_protocol_Z_packet_cmd
,
7499 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
7500 &remote_set_cmdlist
, &remote_show_cmdlist
);
7502 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
7503 Manipulate files on the remote system\n\
7504 Transfer files to and from the remote target system."),
7505 &remote_cmdlist
, "remote ",
7506 0 /* allow-unknown */, &cmdlist
);
7508 add_cmd ("put", class_files
, remote_put_command
,
7509 _("Copy a local file to the remote system."),
7512 add_cmd ("get", class_files
, remote_get_command
,
7513 _("Copy a remote file to the local system."),
7516 add_cmd ("delete", class_files
, remote_delete_command
,
7517 _("Delete a remote file."),
7520 remote_exec_file
= xstrdup ("");
7521 add_setshow_string_noescape_cmd ("exec-file", class_files
,
7522 &remote_exec_file
, _("\
7523 Set the remote pathname for \"run\""), _("\
7524 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
7525 &remote_set_cmdlist
, &remote_show_cmdlist
);
7527 add_setshow_boolean_cmd ("remote-async", class_maintenance
,
7528 &remote_async_permitted_set
, _("\
7529 Set whether gdb controls the remote inferior in asynchronous mode."), _("\
7530 Show whether gdb controls the remote inferior in asynchronous mode."), _("\
7531 Tells gdb whether to control the remote inferior in asynchronous mode."),
7532 set_maintenance_remote_async_permitted
,
7533 show_maintenance_remote_async_permitted
,
7534 &maintenance_set_cmdlist
,
7535 &maintenance_show_cmdlist
);
7538 /* Eventually initialize fileio. See fileio.c */
7539 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
7541 /* Take advantage of the fact that the LWP field is not used, to tag
7542 special ptids with it set to != 0. */
7543 magic_null_ptid
= ptid_build (0, 1, -1);
7544 not_sent_ptid
= ptid_build (0, 1, -2);
7545 any_thread_ptid
= ptid_build (0, 1, 0);