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, 2009,
5 2010, 2011 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 "xml-support.h"
65 #include "memory-map.h"
67 #include "tracepoint.h"
71 /* Temp hacks for tracepoint encoding migration. */
72 static char *target_buf
;
73 static long target_buf_size
;
75 encode_actions (struct breakpoint
*t
, struct bp_location
*tloc
,
76 char ***tdp_actions
, char ***stepping_actions
);
78 /* The size to align memory write packets, when practical. The protocol
79 does not guarantee any alignment, and gdb will generate short
80 writes and unaligned writes, but even as a best-effort attempt this
81 can improve bulk transfers. For instance, if a write is misaligned
82 relative to the target's data bus, the stub may need to make an extra
83 round trip fetching data from the target. This doesn't make a
84 huge difference, but it's easy to do, so we try to be helpful.
86 The alignment chosen is arbitrary; usually data bus width is
87 important here, not the possibly larger cache line size. */
88 enum { REMOTE_ALIGN_WRITES
= 16 };
90 /* Prototypes for local functions. */
91 static void cleanup_sigint_signal_handler (void *dummy
);
92 static void initialize_sigint_signal_handler (void);
93 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
94 static int getpkt_or_notif_sane (char **buf
, long *sizeof_buf
,
97 static void handle_remote_sigint (int);
98 static void handle_remote_sigint_twice (int);
99 static void async_remote_interrupt (gdb_client_data
);
100 void async_remote_interrupt_twice (gdb_client_data
);
102 static void remote_files_info (struct target_ops
*ignore
);
104 static void remote_prepare_to_store (struct regcache
*regcache
);
106 static void remote_open (char *name
, int from_tty
);
108 static void extended_remote_open (char *name
, int from_tty
);
110 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
);
112 static void remote_close (int quitting
);
114 static void remote_mourn (struct target_ops
*ops
);
116 static void extended_remote_restart (void);
118 static void extended_remote_mourn (struct target_ops
*);
120 static void remote_mourn_1 (struct target_ops
*);
122 static void remote_send (char **buf
, long *sizeof_buf_p
);
124 static int readchar (int timeout
);
126 static void remote_kill (struct target_ops
*ops
);
128 static int tohex (int nib
);
130 static int remote_can_async_p (void);
132 static int remote_is_async_p (void);
134 static void remote_async (void (*callback
) (enum inferior_event_type event_type
,
135 void *context
), void *context
);
137 static int remote_async_mask (int new_mask
);
139 static void remote_detach (struct target_ops
*ops
, char *args
, int from_tty
);
141 static void remote_interrupt (int signo
);
143 static void remote_interrupt_twice (int signo
);
145 static void interrupt_query (void);
147 static void set_general_thread (struct ptid ptid
);
148 static void set_continue_thread (struct ptid ptid
);
150 static void get_offsets (void);
152 static void skip_frame (void);
154 static long read_frame (char **buf_p
, long *sizeof_buf
);
156 static int hexnumlen (ULONGEST num
);
158 static void init_remote_ops (void);
160 static void init_extended_remote_ops (void);
162 static void remote_stop (ptid_t
);
164 static int ishex (int ch
, int *val
);
166 static int stubhex (int ch
);
168 static int hexnumstr (char *, ULONGEST
);
170 static int hexnumnstr (char *, ULONGEST
, int);
172 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
174 static void print_packet (char *);
176 static void compare_sections_command (char *, int);
178 static void packet_command (char *, int);
180 static int stub_unpack_int (char *buff
, int fieldlength
);
182 static ptid_t
remote_current_thread (ptid_t oldptid
);
184 static void remote_find_new_threads (void);
186 static void record_currthread (ptid_t currthread
);
188 static int fromhex (int a
);
190 extern int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
192 extern int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
194 static int putpkt_binary (char *buf
, int cnt
);
196 static void check_binary_download (CORE_ADDR addr
);
198 struct packet_config
;
200 static void show_packet_config_cmd (struct packet_config
*config
);
202 static void update_packet_config (struct packet_config
*config
);
204 static void set_remote_protocol_packet_cmd (char *args
, int from_tty
,
205 struct cmd_list_element
*c
);
207 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
209 struct cmd_list_element
*c
,
212 static char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
213 static ptid_t
read_ptid (char *buf
, char **obuf
);
215 static void remote_set_permissions (void);
218 static int remote_get_trace_status (struct trace_status
*ts
);
220 static int remote_upload_tracepoints (struct uploaded_tp
**utpp
);
222 static int remote_upload_trace_state_variables (struct uploaded_tsv
**utsvp
);
224 static void remote_query_supported (void);
226 static void remote_check_symbols (struct objfile
*objfile
);
228 void _initialize_remote (void);
231 static struct stop_reply
*stop_reply_xmalloc (void);
232 static void stop_reply_xfree (struct stop_reply
*);
233 static void do_stop_reply_xfree (void *arg
);
234 static void remote_parse_stop_reply (char *buf
, struct stop_reply
*);
235 static void push_stop_reply (struct stop_reply
*);
236 static void remote_get_pending_stop_replies (void);
237 static void discard_pending_stop_replies (int pid
);
238 static int peek_stop_reply (ptid_t ptid
);
240 static void remote_async_inferior_event_handler (gdb_client_data
);
241 static void remote_async_get_pending_events_handler (gdb_client_data
);
243 static void remote_terminal_ours (void);
245 static int remote_read_description_p (struct target_ops
*target
);
247 static void remote_console_output (char *msg
);
249 /* The non-stop remote protocol provisions for one pending stop reply.
250 This is where we keep it until it is acknowledged. */
252 static struct stop_reply
*pending_stop_reply
= NULL
;
256 static struct cmd_list_element
*remote_cmdlist
;
258 /* For "set remote" and "show remote". */
260 static struct cmd_list_element
*remote_set_cmdlist
;
261 static struct cmd_list_element
*remote_show_cmdlist
;
263 /* Description of the remote protocol state for the currently
264 connected target. This is per-target state, and independent of the
265 selected architecture. */
269 /* A buffer to use for incoming packets, and its current size. The
270 buffer is grown dynamically for larger incoming packets.
271 Outgoing packets may also be constructed in this buffer.
272 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
273 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
278 /* If we negotiated packet size explicitly (and thus can bypass
279 heuristics for the largest packet size that will not overflow
280 a buffer in the stub), this will be set to that packet size.
281 Otherwise zero, meaning to use the guessed size. */
282 long explicit_packet_size
;
284 /* remote_wait is normally called when the target is running and
285 waits for a stop reply packet. But sometimes we need to call it
286 when the target is already stopped. We can send a "?" packet
287 and have remote_wait read the response. Or, if we already have
288 the response, we can stash it in BUF and tell remote_wait to
289 skip calling getpkt. This flag is set when BUF contains a
290 stop reply packet and the target is not waiting. */
291 int cached_wait_status
;
293 /* True, if in no ack mode. That is, neither GDB nor the stub will
294 expect acks from each other. The connection is assumed to be
298 /* True if we're connected in extended remote mode. */
301 /* True if the stub reported support for multi-process
303 int multi_process_aware
;
305 /* True if we resumed the target and we're waiting for the target to
306 stop. In the mean time, we can't start another command/query.
307 The remote server wouldn't be ready to process it, so we'd
308 timeout waiting for a reply that would never come and eventually
309 we'd close the connection. This can happen in asynchronous mode
310 because we allow GDB commands while the target is running. */
311 int waiting_for_stop_reply
;
313 /* True if the stub reports support for non-stop mode. */
316 /* True if the stub reports support for vCont;t. */
319 /* True if the stub reports support for conditional tracepoints. */
320 int cond_tracepoints
;
322 /* True if the stub reports support for fast tracepoints. */
323 int fast_tracepoints
;
325 /* True if the stub reports support for static tracepoints. */
326 int static_tracepoints
;
328 /* True if the stub can continue running a trace while GDB is
330 int disconnected_tracing
;
332 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
333 responded to that. */
337 /* Private data that we'll store in (struct thread_info)->private. */
338 struct private_thread_info
345 free_private_thread_info (struct private_thread_info
*info
)
351 /* Returns true if the multi-process extensions are in effect. */
353 remote_multi_process_p (struct remote_state
*rs
)
355 return rs
->extended
&& rs
->multi_process_aware
;
358 /* This data could be associated with a target, but we do not always
359 have access to the current target when we need it, so for now it is
360 static. This will be fine for as long as only one target is in use
362 static struct remote_state remote_state
;
364 static struct remote_state
*
365 get_remote_state_raw (void)
367 return &remote_state
;
370 /* Description of the remote protocol for a given architecture. */
374 long offset
; /* Offset into G packet. */
375 long regnum
; /* GDB's internal register number. */
376 LONGEST pnum
; /* Remote protocol register number. */
377 int in_g_packet
; /* Always part of G packet. */
378 /* long size in bytes; == register_size (target_gdbarch, regnum);
380 /* char *name; == gdbarch_register_name (target_gdbarch, regnum);
384 struct remote_arch_state
386 /* Description of the remote protocol registers. */
387 long sizeof_g_packet
;
389 /* Description of the remote protocol registers indexed by REGNUM
390 (making an array gdbarch_num_regs in size). */
391 struct packet_reg
*regs
;
393 /* This is the size (in chars) of the first response to the ``g''
394 packet. It is used as a heuristic when determining the maximum
395 size of memory-read and memory-write packets. A target will
396 typically only reserve a buffer large enough to hold the ``g''
397 packet. The size does not include packet overhead (headers and
399 long actual_register_packet_size
;
401 /* This is the maximum size (in chars) of a non read/write packet.
402 It is also used as a cap on the size of read/write packets. */
403 long remote_packet_size
;
406 long sizeof_pkt
= 2000;
408 /* Utility: generate error from an incoming stub packet. */
410 trace_error (char *buf
)
413 return; /* not an error msg */
416 case '1': /* malformed packet error */
417 if (*++buf
== '0') /* general case: */
418 error (_("remote.c: error in outgoing packet."));
420 error (_("remote.c: error in outgoing packet at field #%ld."),
421 strtol (buf
, NULL
, 16));
423 error (_("trace API error 0x%s."), ++buf
);
425 error (_("Target returns error code '%s'."), buf
);
429 /* Utility: wait for reply from stub, while accepting "O" packets. */
431 remote_get_noisy_reply (char **buf_p
,
434 do /* Loop on reply from remote stub. */
438 QUIT
; /* Allow user to bail out with ^C. */
439 getpkt (buf_p
, sizeof_buf
, 0);
443 else if (strncmp (buf
, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
446 CORE_ADDR from
, to
, org_to
;
448 int adjusted_size
= 0;
449 volatile struct gdb_exception ex
;
451 p
= buf
+ strlen ("qRelocInsn:");
452 pp
= unpack_varlen_hex (p
, &ul
);
454 error (_("invalid qRelocInsn packet: %s"), buf
);
458 pp
= unpack_varlen_hex (p
, &ul
);
463 TRY_CATCH (ex
, RETURN_MASK_ALL
)
465 gdbarch_relocate_instruction (target_gdbarch
, &to
, from
);
469 adjusted_size
= to
- org_to
;
471 sprintf (buf
, "qRelocInsn:%x", adjusted_size
);
474 else if (ex
.reason
< 0 && ex
.error
== MEMORY_ERROR
)
476 /* Propagate memory errors silently back to the target.
477 The stub may have limited the range of addresses we
478 can write to, for example. */
483 /* Something unexpectedly bad happened. Be verbose so
484 we can tell what, and propagate the error back to the
485 stub, so it doesn't get stuck waiting for a
487 exception_fprintf (gdb_stderr
, ex
,
488 _("warning: relocating instruction: "));
492 else if (buf
[0] == 'O' && buf
[1] != 'K')
493 remote_console_output (buf
+ 1); /* 'O' message from stub */
495 return buf
; /* Here's the actual reply. */
500 /* Handle for retreving the remote protocol data from gdbarch. */
501 static struct gdbarch_data
*remote_gdbarch_data_handle
;
503 static struct remote_arch_state
*
504 get_remote_arch_state (void)
506 return gdbarch_data (target_gdbarch
, remote_gdbarch_data_handle
);
509 /* Fetch the global remote target state. */
511 static struct remote_state
*
512 get_remote_state (void)
514 /* Make sure that the remote architecture state has been
515 initialized, because doing so might reallocate rs->buf. Any
516 function which calls getpkt also needs to be mindful of changes
517 to rs->buf, but this call limits the number of places which run
519 get_remote_arch_state ();
521 return get_remote_state_raw ();
525 compare_pnums (const void *lhs_
, const void *rhs_
)
527 const struct packet_reg
* const *lhs
= lhs_
;
528 const struct packet_reg
* const *rhs
= rhs_
;
530 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
532 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
539 init_remote_state (struct gdbarch
*gdbarch
)
541 int regnum
, num_remote_regs
, offset
;
542 struct remote_state
*rs
= get_remote_state_raw ();
543 struct remote_arch_state
*rsa
;
544 struct packet_reg
**remote_regs
;
546 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
548 /* Use the architecture to build a regnum<->pnum table, which will be
549 1:1 unless a feature set specifies otherwise. */
550 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
,
551 gdbarch_num_regs (gdbarch
),
553 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
555 struct packet_reg
*r
= &rsa
->regs
[regnum
];
557 if (register_size (gdbarch
, regnum
) == 0)
558 /* Do not try to fetch zero-sized (placeholder) registers. */
561 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
566 /* Define the g/G packet format as the contents of each register
567 with a remote protocol number, in order of ascending protocol
570 remote_regs
= alloca (gdbarch_num_regs (gdbarch
)
571 * sizeof (struct packet_reg
*));
572 for (num_remote_regs
= 0, regnum
= 0;
573 regnum
< gdbarch_num_regs (gdbarch
);
575 if (rsa
->regs
[regnum
].pnum
!= -1)
576 remote_regs
[num_remote_regs
++] = &rsa
->regs
[regnum
];
578 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
581 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
583 remote_regs
[regnum
]->in_g_packet
= 1;
584 remote_regs
[regnum
]->offset
= offset
;
585 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
588 /* Record the maximum possible size of the g packet - it may turn out
590 rsa
->sizeof_g_packet
= offset
;
592 /* Default maximum number of characters in a packet body. Many
593 remote stubs have a hardwired buffer size of 400 bytes
594 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
595 as the maximum packet-size to ensure that the packet and an extra
596 NUL character can always fit in the buffer. This stops GDB
597 trashing stubs that try to squeeze an extra NUL into what is
598 already a full buffer (As of 1999-12-04 that was most stubs). */
599 rsa
->remote_packet_size
= 400 - 1;
601 /* This one is filled in when a ``g'' packet is received. */
602 rsa
->actual_register_packet_size
= 0;
604 /* Should rsa->sizeof_g_packet needs more space than the
605 default, adjust the size accordingly. Remember that each byte is
606 encoded as two characters. 32 is the overhead for the packet
607 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
608 (``$NN:G...#NN'') is a better guess, the below has been padded a
610 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
611 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
613 /* Make sure that the packet buffer is plenty big enough for
614 this architecture. */
615 if (rs
->buf_size
< rsa
->remote_packet_size
)
617 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
618 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
624 /* Return the current allowed size of a remote packet. This is
625 inferred from the current architecture, and should be used to
626 limit the length of outgoing packets. */
628 get_remote_packet_size (void)
630 struct remote_state
*rs
= get_remote_state ();
631 struct remote_arch_state
*rsa
= get_remote_arch_state ();
633 if (rs
->explicit_packet_size
)
634 return rs
->explicit_packet_size
;
636 return rsa
->remote_packet_size
;
639 static struct packet_reg
*
640 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
642 if (regnum
< 0 && regnum
>= gdbarch_num_regs (target_gdbarch
))
646 struct packet_reg
*r
= &rsa
->regs
[regnum
];
648 gdb_assert (r
->regnum
== regnum
);
653 static struct packet_reg
*
654 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
658 for (i
= 0; i
< gdbarch_num_regs (target_gdbarch
); i
++)
660 struct packet_reg
*r
= &rsa
->regs
[i
];
668 /* FIXME: graces/2002-08-08: These variables should eventually be
669 bound to an instance of the target object (as in gdbarch-tdep()),
670 when such a thing exists. */
672 /* This is set to the data address of the access causing the target
673 to stop for a watchpoint. */
674 static CORE_ADDR remote_watch_data_address
;
676 /* This is non-zero if target stopped for a watchpoint. */
677 static int remote_stopped_by_watchpoint_p
;
679 static struct target_ops remote_ops
;
681 static struct target_ops extended_remote_ops
;
683 static int remote_async_mask_value
= 1;
685 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
686 ``forever'' still use the normal timeout mechanism. This is
687 currently used by the ASYNC code to guarentee that target reads
688 during the initial connect always time-out. Once getpkt has been
689 modified to return a timeout indication and, in turn
690 remote_wait()/wait_for_inferior() have gained a timeout parameter
692 static int wait_forever_enabled_p
= 1;
694 /* Allow the user to specify what sequence to send to the remote
695 when he requests a program interruption: Although ^C is usually
696 what remote systems expect (this is the default, here), it is
697 sometimes preferable to send a break. On other systems such
698 as the Linux kernel, a break followed by g, which is Magic SysRq g
699 is required in order to interrupt the execution. */
700 const char interrupt_sequence_control_c
[] = "Ctrl-C";
701 const char interrupt_sequence_break
[] = "BREAK";
702 const char interrupt_sequence_break_g
[] = "BREAK-g";
703 static const char *interrupt_sequence_modes
[] =
705 interrupt_sequence_control_c
,
706 interrupt_sequence_break
,
707 interrupt_sequence_break_g
,
710 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
713 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
714 struct cmd_list_element
*c
,
717 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
718 fprintf_filtered (file
,
719 _("Send the ASCII ETX character (Ctrl-c) "
720 "to the remote target to interrupt the "
721 "execution of the program.\n"));
722 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
723 fprintf_filtered (file
,
724 _("send a break signal to the remote target "
725 "to interrupt the execution of the program.\n"));
726 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
727 fprintf_filtered (file
,
728 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
729 "the remote target to interrupt the execution "
730 "of Linux kernel.\n"));
732 internal_error (__FILE__
, __LINE__
,
733 _("Invalid value for interrupt_sequence_mode: %s."),
734 interrupt_sequence_mode
);
737 /* This boolean variable specifies whether interrupt_sequence is sent
738 to the remote target when gdb connects to it.
739 This is mostly needed when you debug the Linux kernel: The Linux kernel
740 expects BREAK g which is Magic SysRq g for connecting gdb. */
741 static int interrupt_on_connect
= 0;
743 /* This variable is used to implement the "set/show remotebreak" commands.
744 Since these commands are now deprecated in favor of "set/show remote
745 interrupt-sequence", it no longer has any effect on the code. */
746 static int remote_break
;
749 set_remotebreak (char *args
, int from_tty
, struct cmd_list_element
*c
)
752 interrupt_sequence_mode
= interrupt_sequence_break
;
754 interrupt_sequence_mode
= interrupt_sequence_control_c
;
758 show_remotebreak (struct ui_file
*file
, int from_tty
,
759 struct cmd_list_element
*c
,
764 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
765 remote_open knows that we don't have a file open when the program
767 static struct serial
*remote_desc
= NULL
;
769 /* This variable sets the number of bits in an address that are to be
770 sent in a memory ("M" or "m") packet. Normally, after stripping
771 leading zeros, the entire address would be sent. This variable
772 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
773 initial implementation of remote.c restricted the address sent in
774 memory packets to ``host::sizeof long'' bytes - (typically 32
775 bits). Consequently, for 64 bit targets, the upper 32 bits of an
776 address was never sent. Since fixing this bug may cause a break in
777 some remote targets this variable is principly provided to
778 facilitate backward compatibility. */
780 static int remote_address_size
;
782 /* Temporary to track who currently owns the terminal. See
783 remote_terminal_* for more details. */
785 static int remote_async_terminal_ours_p
;
787 /* The executable file to use for "run" on the remote side. */
789 static char *remote_exec_file
= "";
792 /* User configurable variables for the number of characters in a
793 memory read/write packet. MIN (rsa->remote_packet_size,
794 rsa->sizeof_g_packet) is the default. Some targets need smaller
795 values (fifo overruns, et.al.) and some users need larger values
796 (speed up transfers). The variables ``preferred_*'' (the user
797 request), ``current_*'' (what was actually set) and ``forced_*''
798 (Positive - a soft limit, negative - a hard limit). */
800 struct memory_packet_config
807 /* Compute the current size of a read/write packet. Since this makes
808 use of ``actual_register_packet_size'' the computation is dynamic. */
811 get_memory_packet_size (struct memory_packet_config
*config
)
813 struct remote_state
*rs
= get_remote_state ();
814 struct remote_arch_state
*rsa
= get_remote_arch_state ();
816 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
817 law?) that some hosts don't cope very well with large alloca()
818 calls. Eventually the alloca() code will be replaced by calls to
819 xmalloc() and make_cleanups() allowing this restriction to either
820 be lifted or removed. */
821 #ifndef MAX_REMOTE_PACKET_SIZE
822 #define MAX_REMOTE_PACKET_SIZE 16384
824 /* NOTE: 20 ensures we can write at least one byte. */
825 #ifndef MIN_REMOTE_PACKET_SIZE
826 #define MIN_REMOTE_PACKET_SIZE 20
831 if (config
->size
<= 0)
832 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
834 what_they_get
= config
->size
;
838 what_they_get
= get_remote_packet_size ();
839 /* Limit the packet to the size specified by the user. */
841 && what_they_get
> config
->size
)
842 what_they_get
= config
->size
;
844 /* Limit it to the size of the targets ``g'' response unless we have
845 permission from the stub to use a larger packet size. */
846 if (rs
->explicit_packet_size
== 0
847 && rsa
->actual_register_packet_size
> 0
848 && what_they_get
> rsa
->actual_register_packet_size
)
849 what_they_get
= rsa
->actual_register_packet_size
;
851 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
852 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
853 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
854 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
856 /* Make sure there is room in the global buffer for this packet
857 (including its trailing NUL byte). */
858 if (rs
->buf_size
< what_they_get
+ 1)
860 rs
->buf_size
= 2 * what_they_get
;
861 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
864 return what_they_get
;
867 /* Update the size of a read/write packet. If they user wants
868 something really big then do a sanity check. */
871 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
873 int fixed_p
= config
->fixed_p
;
874 long size
= config
->size
;
877 error (_("Argument required (integer, `fixed' or `limited')."));
878 else if (strcmp (args
, "hard") == 0
879 || strcmp (args
, "fixed") == 0)
881 else if (strcmp (args
, "soft") == 0
882 || strcmp (args
, "limit") == 0)
888 size
= strtoul (args
, &end
, 0);
890 error (_("Invalid %s (bad syntax)."), config
->name
);
892 /* Instead of explicitly capping the size of a packet to
893 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
894 instead allowed to set the size to something arbitrarily
896 if (size
> MAX_REMOTE_PACKET_SIZE
)
897 error (_("Invalid %s (too large)."), config
->name
);
901 if (fixed_p
&& !config
->fixed_p
)
903 if (! query (_("The target may not be able to correctly handle a %s\n"
904 "of %ld bytes. Change the packet size? "),
906 error (_("Packet size not changed."));
908 /* Update the config. */
909 config
->fixed_p
= fixed_p
;
914 show_memory_packet_size (struct memory_packet_config
*config
)
916 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
918 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
919 get_memory_packet_size (config
));
921 printf_filtered (_("Packets are limited to %ld bytes.\n"),
922 get_memory_packet_size (config
));
925 static struct memory_packet_config memory_write_packet_config
=
927 "memory-write-packet-size",
931 set_memory_write_packet_size (char *args
, int from_tty
)
933 set_memory_packet_size (args
, &memory_write_packet_config
);
937 show_memory_write_packet_size (char *args
, int from_tty
)
939 show_memory_packet_size (&memory_write_packet_config
);
943 get_memory_write_packet_size (void)
945 return get_memory_packet_size (&memory_write_packet_config
);
948 static struct memory_packet_config memory_read_packet_config
=
950 "memory-read-packet-size",
954 set_memory_read_packet_size (char *args
, int from_tty
)
956 set_memory_packet_size (args
, &memory_read_packet_config
);
960 show_memory_read_packet_size (char *args
, int from_tty
)
962 show_memory_packet_size (&memory_read_packet_config
);
966 get_memory_read_packet_size (void)
968 long size
= get_memory_packet_size (&memory_read_packet_config
);
970 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
971 extra buffer size argument before the memory read size can be
972 increased beyond this. */
973 if (size
> get_remote_packet_size ())
974 size
= get_remote_packet_size ();
979 /* Generic configuration support for packets the stub optionally
980 supports. Allows the user to specify the use of the packet as well
981 as allowing GDB to auto-detect support in the remote stub. */
985 PACKET_SUPPORT_UNKNOWN
= 0,
994 enum auto_boolean detect
;
995 enum packet_support support
;
998 /* Analyze a packet's return value and update the packet config
1009 update_packet_config (struct packet_config
*config
)
1011 switch (config
->detect
)
1013 case AUTO_BOOLEAN_TRUE
:
1014 config
->support
= PACKET_ENABLE
;
1016 case AUTO_BOOLEAN_FALSE
:
1017 config
->support
= PACKET_DISABLE
;
1019 case AUTO_BOOLEAN_AUTO
:
1020 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1026 show_packet_config_cmd (struct packet_config
*config
)
1028 char *support
= "internal-error";
1030 switch (config
->support
)
1033 support
= "enabled";
1035 case PACKET_DISABLE
:
1036 support
= "disabled";
1038 case PACKET_SUPPORT_UNKNOWN
:
1039 support
= "unknown";
1042 switch (config
->detect
)
1044 case AUTO_BOOLEAN_AUTO
:
1045 printf_filtered (_("Support for the `%s' packet "
1046 "is auto-detected, currently %s.\n"),
1047 config
->name
, support
);
1049 case AUTO_BOOLEAN_TRUE
:
1050 case AUTO_BOOLEAN_FALSE
:
1051 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1052 config
->name
, support
);
1058 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
1059 const char *title
, int legacy
)
1065 config
->name
= name
;
1066 config
->title
= title
;
1067 config
->detect
= AUTO_BOOLEAN_AUTO
;
1068 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1069 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1071 show_doc
= xstrprintf ("Show current use of remote "
1072 "protocol `%s' (%s) packet",
1074 /* set/show TITLE-packet {auto,on,off} */
1075 cmd_name
= xstrprintf ("%s-packet", title
);
1076 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
1077 &config
->detect
, set_doc
,
1078 show_doc
, NULL
, /* help_doc */
1079 set_remote_protocol_packet_cmd
,
1080 show_remote_protocol_packet_cmd
,
1081 &remote_set_cmdlist
, &remote_show_cmdlist
);
1082 /* The command code copies the documentation strings. */
1085 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1090 legacy_name
= xstrprintf ("%s-packet", name
);
1091 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1092 &remote_set_cmdlist
);
1093 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1094 &remote_show_cmdlist
);
1098 static enum packet_result
1099 packet_check_result (const char *buf
)
1103 /* The stub recognized the packet request. Check that the
1104 operation succeeded. */
1106 && isxdigit (buf
[1]) && isxdigit (buf
[2])
1108 /* "Enn" - definitly an error. */
1109 return PACKET_ERROR
;
1111 /* Always treat "E." as an error. This will be used for
1112 more verbose error messages, such as E.memtypes. */
1113 if (buf
[0] == 'E' && buf
[1] == '.')
1114 return PACKET_ERROR
;
1116 /* The packet may or may not be OK. Just assume it is. */
1120 /* The stub does not support the packet. */
1121 return PACKET_UNKNOWN
;
1124 static enum packet_result
1125 packet_ok (const char *buf
, struct packet_config
*config
)
1127 enum packet_result result
;
1129 result
= packet_check_result (buf
);
1134 /* The stub recognized the packet request. */
1135 switch (config
->support
)
1137 case PACKET_SUPPORT_UNKNOWN
:
1139 fprintf_unfiltered (gdb_stdlog
,
1140 "Packet %s (%s) is supported\n",
1141 config
->name
, config
->title
);
1142 config
->support
= PACKET_ENABLE
;
1144 case PACKET_DISABLE
:
1145 internal_error (__FILE__
, __LINE__
,
1146 _("packet_ok: attempt to use a disabled packet"));
1152 case PACKET_UNKNOWN
:
1153 /* The stub does not support the packet. */
1154 switch (config
->support
)
1157 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
1158 /* If the stub previously indicated that the packet was
1159 supported then there is a protocol error.. */
1160 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1161 config
->name
, config
->title
);
1163 /* The user set it wrong. */
1164 error (_("Enabled packet %s (%s) not recognized by stub"),
1165 config
->name
, config
->title
);
1167 case PACKET_SUPPORT_UNKNOWN
:
1169 fprintf_unfiltered (gdb_stdlog
,
1170 "Packet %s (%s) is NOT supported\n",
1171 config
->name
, config
->title
);
1172 config
->support
= PACKET_DISABLE
;
1174 case PACKET_DISABLE
:
1196 PACKET_vFile_pwrite
,
1198 PACKET_vFile_unlink
,
1200 PACKET_qXfer_features
,
1201 PACKET_qXfer_libraries
,
1202 PACKET_qXfer_memory_map
,
1203 PACKET_qXfer_spu_read
,
1204 PACKET_qXfer_spu_write
,
1205 PACKET_qXfer_osdata
,
1206 PACKET_qXfer_threads
,
1207 PACKET_qXfer_statictrace_read
,
1211 PACKET_QPassSignals
,
1212 PACKET_qSearch_memory
,
1215 PACKET_QStartNoAckMode
,
1217 PACKET_qXfer_siginfo_read
,
1218 PACKET_qXfer_siginfo_write
,
1220 PACKET_ConditionalTracepoints
,
1221 PACKET_FastTracepoints
,
1222 PACKET_StaticTracepoints
,
1225 PACKET_TracepointSource
,
1230 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
1233 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
1234 struct cmd_list_element
*c
)
1236 struct packet_config
*packet
;
1238 for (packet
= remote_protocol_packets
;
1239 packet
< &remote_protocol_packets
[PACKET_MAX
];
1242 if (&packet
->detect
== c
->var
)
1244 update_packet_config (packet
);
1248 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1253 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
1254 struct cmd_list_element
*c
,
1257 struct packet_config
*packet
;
1259 for (packet
= remote_protocol_packets
;
1260 packet
< &remote_protocol_packets
[PACKET_MAX
];
1263 if (&packet
->detect
== c
->var
)
1265 show_packet_config_cmd (packet
);
1269 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1273 /* Should we try one of the 'Z' requests? */
1277 Z_PACKET_SOFTWARE_BP
,
1278 Z_PACKET_HARDWARE_BP
,
1285 /* For compatibility with older distributions. Provide a ``set remote
1286 Z-packet ...'' command that updates all the Z packet types. */
1288 static enum auto_boolean remote_Z_packet_detect
;
1291 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1292 struct cmd_list_element
*c
)
1296 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1298 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1299 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1304 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1305 struct cmd_list_element
*c
,
1310 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1312 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1316 /* Should we try the 'ThreadInfo' query packet?
1318 This variable (NOT available to the user: auto-detect only!)
1319 determines whether GDB will use the new, simpler "ThreadInfo"
1320 query or the older, more complex syntax for thread queries.
1321 This is an auto-detect variable (set to true at each connect,
1322 and set to false when the target fails to recognize it). */
1324 static int use_threadinfo_query
;
1325 static int use_threadextra_query
;
1327 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1328 static struct async_signal_handler
*sigint_remote_twice_token
;
1329 static struct async_signal_handler
*sigint_remote_token
;
1332 /* Asynchronous signal handle registered as event loop source for
1333 when we have pending events ready to be passed to the core. */
1335 static struct async_event_handler
*remote_async_inferior_event_token
;
1337 /* Asynchronous signal handle registered as event loop source for when
1338 the remote sent us a %Stop notification. The registered callback
1339 will do a vStopped sequence to pull the rest of the events out of
1340 the remote side into our event queue. */
1342 static struct async_event_handler
*remote_async_get_pending_events_token
;
1345 static ptid_t magic_null_ptid
;
1346 static ptid_t not_sent_ptid
;
1347 static ptid_t any_thread_ptid
;
1349 /* These are the threads which we last sent to the remote system. The
1350 TID member will be -1 for all or -2 for not sent yet. */
1352 static ptid_t general_thread
;
1353 static ptid_t continue_thread
;
1355 /* Find out if the stub attached to PID (and hence GDB should offer to
1356 detach instead of killing it when bailing out). */
1359 remote_query_attached (int pid
)
1361 struct remote_state
*rs
= get_remote_state ();
1363 if (remote_protocol_packets
[PACKET_qAttached
].support
== PACKET_DISABLE
)
1366 if (remote_multi_process_p (rs
))
1367 sprintf (rs
->buf
, "qAttached:%x", pid
);
1369 sprintf (rs
->buf
, "qAttached");
1372 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1374 switch (packet_ok (rs
->buf
,
1375 &remote_protocol_packets
[PACKET_qAttached
]))
1378 if (strcmp (rs
->buf
, "1") == 0)
1382 warning (_("Remote failure reply: %s"), rs
->buf
);
1384 case PACKET_UNKNOWN
:
1391 /* Add PID to GDB's inferior table. Since we can be connected to a
1392 remote system before before knowing about any inferior, mark the
1393 target with execution when we find the first inferior. If ATTACHED
1394 is 1, then we had just attached to this inferior. If it is 0, then
1395 we just created this inferior. If it is -1, then try querying the
1396 remote stub to find out if it had attached to the inferior or
1399 static struct inferior
*
1400 remote_add_inferior (int pid
, int attached
)
1402 struct inferior
*inf
;
1404 /* Check whether this process we're learning about is to be
1405 considered attached, or if is to be considered to have been
1406 spawned by the stub. */
1408 attached
= remote_query_attached (pid
);
1410 if (gdbarch_has_global_solist (target_gdbarch
))
1412 /* If the target shares code across all inferiors, then every
1413 attach adds a new inferior. */
1414 inf
= add_inferior (pid
);
1416 /* ... and every inferior is bound to the same program space.
1417 However, each inferior may still have its own address
1419 inf
->aspace
= maybe_new_address_space ();
1420 inf
->pspace
= current_program_space
;
1424 /* In the traditional debugging scenario, there's a 1-1 match
1425 between program/address spaces. We simply bind the inferior
1426 to the program space's address space. */
1427 inf
= current_inferior ();
1428 inferior_appeared (inf
, pid
);
1431 inf
->attach_flag
= attached
;
1436 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1437 according to RUNNING. */
1440 remote_add_thread (ptid_t ptid
, int running
)
1444 set_executing (ptid
, running
);
1445 set_running (ptid
, running
);
1448 /* Come here when we learn about a thread id from the remote target.
1449 It may be the first time we hear about such thread, so take the
1450 opportunity to add it to GDB's thread list. In case this is the
1451 first time we're noticing its corresponding inferior, add it to
1452 GDB's inferior list as well. */
1455 remote_notice_new_inferior (ptid_t currthread
, int running
)
1457 /* If this is a new thread, add it to GDB's thread list.
1458 If we leave it up to WFI to do this, bad things will happen. */
1460 if (in_thread_list (currthread
) && is_exited (currthread
))
1462 /* We're seeing an event on a thread id we knew had exited.
1463 This has to be a new thread reusing the old id. Add it. */
1464 remote_add_thread (currthread
, running
);
1468 if (!in_thread_list (currthread
))
1470 struct inferior
*inf
= NULL
;
1471 int pid
= ptid_get_pid (currthread
);
1473 if (ptid_is_pid (inferior_ptid
)
1474 && pid
== ptid_get_pid (inferior_ptid
))
1476 /* inferior_ptid has no thread member yet. This can happen
1477 with the vAttach -> remote_wait,"TAAthread:" path if the
1478 stub doesn't support qC. This is the first stop reported
1479 after an attach, so this is the main thread. Update the
1480 ptid in the thread list. */
1481 if (in_thread_list (pid_to_ptid (pid
)))
1482 thread_change_ptid (inferior_ptid
, currthread
);
1485 remote_add_thread (currthread
, running
);
1486 inferior_ptid
= currthread
;
1491 if (ptid_equal (magic_null_ptid
, inferior_ptid
))
1493 /* inferior_ptid is not set yet. This can happen with the
1494 vRun -> remote_wait,"TAAthread:" path if the stub
1495 doesn't support qC. This is the first stop reported
1496 after an attach, so this is the main thread. Update the
1497 ptid in the thread list. */
1498 thread_change_ptid (inferior_ptid
, currthread
);
1502 /* When connecting to a target remote, or to a target
1503 extended-remote which already was debugging an inferior, we
1504 may not know about it yet. Add it before adding its child
1505 thread, so notifications are emitted in a sensible order. */
1506 if (!in_inferior_list (ptid_get_pid (currthread
)))
1507 inf
= remote_add_inferior (ptid_get_pid (currthread
), -1);
1509 /* This is really a new thread. Add it. */
1510 remote_add_thread (currthread
, running
);
1512 /* If we found a new inferior, let the common code do whatever
1513 it needs to with it (e.g., read shared libraries, insert
1516 notice_new_inferior (currthread
, running
, 0);
1520 /* Return the private thread data, creating it if necessary. */
1522 struct private_thread_info
*
1523 demand_private_info (ptid_t ptid
)
1525 struct thread_info
*info
= find_thread_ptid (ptid
);
1531 info
->private = xmalloc (sizeof (*(info
->private)));
1532 info
->private_dtor
= free_private_thread_info
;
1533 info
->private->core
= -1;
1534 info
->private->extra
= 0;
1537 return info
->private;
1540 /* Call this function as a result of
1541 1) A halt indication (T packet) containing a thread id
1542 2) A direct query of currthread
1543 3) Successful execution of set thread */
1546 record_currthread (ptid_t currthread
)
1548 general_thread
= currthread
;
1551 static char *last_pass_packet
;
1553 /* If 'QPassSignals' is supported, tell the remote stub what signals
1554 it can simply pass through to the inferior without reporting. */
1557 remote_pass_signals (void)
1559 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1561 char *pass_packet
, *p
;
1562 int numsigs
= (int) TARGET_SIGNAL_LAST
;
1565 gdb_assert (numsigs
< 256);
1566 for (i
= 0; i
< numsigs
; i
++)
1568 if (signal_stop_state (i
) == 0
1569 && signal_print_state (i
) == 0
1570 && signal_pass_state (i
) == 1)
1573 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1574 strcpy (pass_packet
, "QPassSignals:");
1575 p
= pass_packet
+ strlen (pass_packet
);
1576 for (i
= 0; i
< numsigs
; i
++)
1578 if (signal_stop_state (i
) == 0
1579 && signal_print_state (i
) == 0
1580 && signal_pass_state (i
) == 1)
1583 *p
++ = tohex (i
>> 4);
1584 *p
++ = tohex (i
& 15);
1593 if (!last_pass_packet
|| strcmp (last_pass_packet
, pass_packet
))
1595 struct remote_state
*rs
= get_remote_state ();
1596 char *buf
= rs
->buf
;
1598 putpkt (pass_packet
);
1599 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1600 packet_ok (buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1601 if (last_pass_packet
)
1602 xfree (last_pass_packet
);
1603 last_pass_packet
= pass_packet
;
1606 xfree (pass_packet
);
1611 remote_notice_signals (ptid_t ptid
)
1613 /* Update the remote on signals to silently pass, if they've
1615 remote_pass_signals ();
1618 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1619 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1620 thread. If GEN is set, set the general thread, if not, then set
1621 the step/continue thread. */
1623 set_thread (struct ptid ptid
, int gen
)
1625 struct remote_state
*rs
= get_remote_state ();
1626 ptid_t state
= gen
? general_thread
: continue_thread
;
1627 char *buf
= rs
->buf
;
1628 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1630 if (ptid_equal (state
, ptid
))
1634 *buf
++ = gen
? 'g' : 'c';
1635 if (ptid_equal (ptid
, magic_null_ptid
))
1636 xsnprintf (buf
, endbuf
- buf
, "0");
1637 else if (ptid_equal (ptid
, any_thread_ptid
))
1638 xsnprintf (buf
, endbuf
- buf
, "0");
1639 else if (ptid_equal (ptid
, minus_one_ptid
))
1640 xsnprintf (buf
, endbuf
- buf
, "-1");
1642 write_ptid (buf
, endbuf
, ptid
);
1644 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1646 general_thread
= ptid
;
1648 continue_thread
= ptid
;
1652 set_general_thread (struct ptid ptid
)
1654 set_thread (ptid
, 1);
1658 set_continue_thread (struct ptid ptid
)
1660 set_thread (ptid
, 0);
1663 /* Change the remote current process. Which thread within the process
1664 ends up selected isn't important, as long as it is the same process
1665 as what INFERIOR_PTID points to.
1667 This comes from that fact that there is no explicit notion of
1668 "selected process" in the protocol. The selected process for
1669 general operations is the process the selected general thread
1673 set_general_process (void)
1675 struct remote_state
*rs
= get_remote_state ();
1677 /* If the remote can't handle multiple processes, don't bother. */
1678 if (!remote_multi_process_p (rs
))
1681 /* We only need to change the remote current thread if it's pointing
1682 at some other process. */
1683 if (ptid_get_pid (general_thread
) != ptid_get_pid (inferior_ptid
))
1684 set_general_thread (inferior_ptid
);
1688 /* Return nonzero if the thread PTID is still alive on the remote
1692 remote_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1694 struct remote_state
*rs
= get_remote_state ();
1697 if (ptid_equal (ptid
, magic_null_ptid
))
1698 /* The main thread is always alive. */
1701 if (ptid_get_pid (ptid
) != 0 && ptid_get_tid (ptid
) == 0)
1702 /* The main thread is always alive. This can happen after a
1703 vAttach, if the remote side doesn't support
1708 endp
= rs
->buf
+ get_remote_packet_size ();
1711 write_ptid (p
, endp
, ptid
);
1714 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1715 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1718 /* About these extended threadlist and threadinfo packets. They are
1719 variable length packets but, the fields within them are often fixed
1720 length. They are redundent enough to send over UDP as is the
1721 remote protocol in general. There is a matching unit test module
1724 #define OPAQUETHREADBYTES 8
1726 /* a 64 bit opaque identifier */
1727 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1729 /* WARNING: This threadref data structure comes from the remote O.S.,
1730 libstub protocol encoding, and remote.c. It is not particularly
1733 /* Right now, the internal structure is int. We want it to be bigger.
1734 Plan to fix this. */
1736 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1738 /* gdb_ext_thread_info is an internal GDB data structure which is
1739 equivalent to the reply of the remote threadinfo packet. */
1741 struct gdb_ext_thread_info
1743 threadref threadid
; /* External form of thread reference. */
1744 int active
; /* Has state interesting to GDB?
1746 char display
[256]; /* Brief state display, name,
1747 blocked/suspended. */
1748 char shortname
[32]; /* To be used to name threads. */
1749 char more_display
[256]; /* Long info, statistics, queue depth,
1753 /* The volume of remote transfers can be limited by submitting
1754 a mask containing bits specifying the desired information.
1755 Use a union of these values as the 'selection' parameter to
1756 get_thread_info. FIXME: Make these TAG names more thread specific. */
1758 #define TAG_THREADID 1
1759 #define TAG_EXISTS 2
1760 #define TAG_DISPLAY 4
1761 #define TAG_THREADNAME 8
1762 #define TAG_MOREDISPLAY 16
1764 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1766 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1768 static char *unpack_nibble (char *buf
, int *val
);
1770 static char *pack_nibble (char *buf
, int nibble
);
1772 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1774 static char *unpack_byte (char *buf
, int *value
);
1776 static char *pack_int (char *buf
, int value
);
1778 static char *unpack_int (char *buf
, int *value
);
1780 static char *unpack_string (char *src
, char *dest
, int length
);
1782 static char *pack_threadid (char *pkt
, threadref
*id
);
1784 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1786 void int_to_threadref (threadref
*id
, int value
);
1788 static int threadref_to_int (threadref
*ref
);
1790 static void copy_threadref (threadref
*dest
, threadref
*src
);
1792 static int threadmatch (threadref
*dest
, threadref
*src
);
1794 static char *pack_threadinfo_request (char *pkt
, int mode
,
1797 static int remote_unpack_thread_info_response (char *pkt
,
1798 threadref
*expectedref
,
1799 struct gdb_ext_thread_info
1803 static int remote_get_threadinfo (threadref
*threadid
,
1804 int fieldset
, /*TAG mask */
1805 struct gdb_ext_thread_info
*info
);
1807 static char *pack_threadlist_request (char *pkt
, int startflag
,
1809 threadref
*nextthread
);
1811 static int parse_threadlist_response (char *pkt
,
1813 threadref
*original_echo
,
1814 threadref
*resultlist
,
1817 static int remote_get_threadlist (int startflag
,
1818 threadref
*nextthread
,
1822 threadref
*threadlist
);
1824 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1826 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1827 void *context
, int looplimit
);
1829 static int remote_newthread_step (threadref
*ref
, void *context
);
1832 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1833 buffer we're allowed to write to. Returns
1834 BUF+CHARACTERS_WRITTEN. */
1837 write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
1840 struct remote_state
*rs
= get_remote_state ();
1842 if (remote_multi_process_p (rs
))
1844 pid
= ptid_get_pid (ptid
);
1846 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
1848 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
1850 tid
= ptid_get_tid (ptid
);
1852 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
1854 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
1859 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
1860 passed the last parsed char. Returns null_ptid on error. */
1863 read_ptid (char *buf
, char **obuf
)
1867 ULONGEST pid
= 0, tid
= 0;
1871 /* Multi-process ptid. */
1872 pp
= unpack_varlen_hex (p
+ 1, &pid
);
1874 error (_("invalid remote ptid: %s\n"), p
);
1877 pp
= unpack_varlen_hex (p
+ 1, &tid
);
1880 return ptid_build (pid
, 0, tid
);
1883 /* No multi-process. Just a tid. */
1884 pp
= unpack_varlen_hex (p
, &tid
);
1886 /* Since the stub is not sending a process id, then default to
1887 what's in inferior_ptid, unless it's null at this point. If so,
1888 then since there's no way to know the pid of the reported
1889 threads, use the magic number. */
1890 if (ptid_equal (inferior_ptid
, null_ptid
))
1891 pid
= ptid_get_pid (magic_null_ptid
);
1893 pid
= ptid_get_pid (inferior_ptid
);
1897 return ptid_build (pid
, 0, tid
);
1900 /* Encode 64 bits in 16 chars of hex. */
1902 static const char hexchars
[] = "0123456789abcdef";
1905 ishex (int ch
, int *val
)
1907 if ((ch
>= 'a') && (ch
<= 'f'))
1909 *val
= ch
- 'a' + 10;
1912 if ((ch
>= 'A') && (ch
<= 'F'))
1914 *val
= ch
- 'A' + 10;
1917 if ((ch
>= '0') && (ch
<= '9'))
1928 if (ch
>= 'a' && ch
<= 'f')
1929 return ch
- 'a' + 10;
1930 if (ch
>= '0' && ch
<= '9')
1932 if (ch
>= 'A' && ch
<= 'F')
1933 return ch
- 'A' + 10;
1938 stub_unpack_int (char *buff
, int fieldlength
)
1945 nibble
= stubhex (*buff
++);
1949 retval
= retval
<< 4;
1955 unpack_varlen_hex (char *buff
, /* packet to parse */
1959 ULONGEST retval
= 0;
1961 while (ishex (*buff
, &nibble
))
1964 retval
= retval
<< 4;
1965 retval
|= nibble
& 0x0f;
1972 unpack_nibble (char *buf
, int *val
)
1974 *val
= fromhex (*buf
++);
1979 pack_nibble (char *buf
, int nibble
)
1981 *buf
++ = hexchars
[(nibble
& 0x0f)];
1986 pack_hex_byte (char *pkt
, int byte
)
1988 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
1989 *pkt
++ = hexchars
[(byte
& 0xf)];
1994 unpack_byte (char *buf
, int *value
)
1996 *value
= stub_unpack_int (buf
, 2);
2001 pack_int (char *buf
, int value
)
2003 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
2004 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
2005 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
2006 buf
= pack_hex_byte (buf
, (value
& 0xff));
2011 unpack_int (char *buf
, int *value
)
2013 *value
= stub_unpack_int (buf
, 8);
2017 #if 0 /* Currently unused, uncomment when needed. */
2018 static char *pack_string (char *pkt
, char *string
);
2021 pack_string (char *pkt
, char *string
)
2026 len
= strlen (string
);
2028 len
= 200; /* Bigger than most GDB packets, junk??? */
2029 pkt
= pack_hex_byte (pkt
, len
);
2033 if ((ch
== '\0') || (ch
== '#'))
2034 ch
= '*'; /* Protect encapsulation. */
2039 #endif /* 0 (unused) */
2042 unpack_string (char *src
, char *dest
, int length
)
2051 pack_threadid (char *pkt
, threadref
*id
)
2054 unsigned char *altid
;
2056 altid
= (unsigned char *) id
;
2057 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
2059 pkt
= pack_hex_byte (pkt
, *altid
++);
2065 unpack_threadid (char *inbuf
, threadref
*id
)
2068 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
2071 altref
= (char *) id
;
2073 while (inbuf
< limit
)
2075 x
= stubhex (*inbuf
++);
2076 y
= stubhex (*inbuf
++);
2077 *altref
++ = (x
<< 4) | y
;
2082 /* Externally, threadrefs are 64 bits but internally, they are still
2083 ints. This is due to a mismatch of specifications. We would like
2084 to use 64bit thread references internally. This is an adapter
2088 int_to_threadref (threadref
*id
, int value
)
2090 unsigned char *scan
;
2092 scan
= (unsigned char *) id
;
2098 *scan
++ = (value
>> 24) & 0xff;
2099 *scan
++ = (value
>> 16) & 0xff;
2100 *scan
++ = (value
>> 8) & 0xff;
2101 *scan
++ = (value
& 0xff);
2105 threadref_to_int (threadref
*ref
)
2108 unsigned char *scan
;
2114 value
= (value
<< 8) | ((*scan
++) & 0xff);
2119 copy_threadref (threadref
*dest
, threadref
*src
)
2122 unsigned char *csrc
, *cdest
;
2124 csrc
= (unsigned char *) src
;
2125 cdest
= (unsigned char *) dest
;
2132 threadmatch (threadref
*dest
, threadref
*src
)
2134 /* Things are broken right now, so just assume we got a match. */
2136 unsigned char *srcp
, *destp
;
2138 srcp
= (char *) src
;
2139 destp
= (char *) dest
;
2143 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
2150 threadid:1, # always request threadid
2157 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2160 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
2162 *pkt
++ = 'q'; /* Info Query */
2163 *pkt
++ = 'P'; /* process or thread info */
2164 pkt
= pack_int (pkt
, mode
); /* mode */
2165 pkt
= pack_threadid (pkt
, id
); /* threadid */
2166 *pkt
= '\0'; /* terminate */
2170 /* These values tag the fields in a thread info response packet. */
2171 /* Tagging the fields allows us to request specific fields and to
2172 add more fields as time goes by. */
2174 #define TAG_THREADID 1 /* Echo the thread identifier. */
2175 #define TAG_EXISTS 2 /* Is this process defined enough to
2176 fetch registers and its stack? */
2177 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2178 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2179 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2183 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
2184 struct gdb_ext_thread_info
*info
)
2186 struct remote_state
*rs
= get_remote_state ();
2190 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
2193 /* info->threadid = 0; FIXME: implement zero_threadref. */
2195 info
->display
[0] = '\0';
2196 info
->shortname
[0] = '\0';
2197 info
->more_display
[0] = '\0';
2199 /* Assume the characters indicating the packet type have been
2201 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
2202 pkt
= unpack_threadid (pkt
, &ref
);
2205 warning (_("Incomplete response to threadinfo request."));
2206 if (!threadmatch (&ref
, expectedref
))
2207 { /* This is an answer to a different request. */
2208 warning (_("ERROR RMT Thread info mismatch."));
2211 copy_threadref (&info
->threadid
, &ref
);
2213 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2215 /* Packets are terminated with nulls. */
2216 while ((pkt
< limit
) && mask
&& *pkt
)
2218 pkt
= unpack_int (pkt
, &tag
); /* tag */
2219 pkt
= unpack_byte (pkt
, &length
); /* length */
2220 if (!(tag
& mask
)) /* Tags out of synch with mask. */
2222 warning (_("ERROR RMT: threadinfo tag mismatch."));
2226 if (tag
== TAG_THREADID
)
2230 warning (_("ERROR RMT: length of threadid is not 16."));
2234 pkt
= unpack_threadid (pkt
, &ref
);
2235 mask
= mask
& ~TAG_THREADID
;
2238 if (tag
== TAG_EXISTS
)
2240 info
->active
= stub_unpack_int (pkt
, length
);
2242 mask
= mask
& ~(TAG_EXISTS
);
2245 warning (_("ERROR RMT: 'exists' length too long."));
2251 if (tag
== TAG_THREADNAME
)
2253 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
2254 mask
= mask
& ~TAG_THREADNAME
;
2257 if (tag
== TAG_DISPLAY
)
2259 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
2260 mask
= mask
& ~TAG_DISPLAY
;
2263 if (tag
== TAG_MOREDISPLAY
)
2265 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
2266 mask
= mask
& ~TAG_MOREDISPLAY
;
2269 warning (_("ERROR RMT: unknown thread info tag."));
2270 break; /* Not a tag we know about. */
2276 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
2277 struct gdb_ext_thread_info
*info
)
2279 struct remote_state
*rs
= get_remote_state ();
2282 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
2284 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2286 if (rs
->buf
[0] == '\0')
2289 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
2294 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2297 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
2298 threadref
*nextthread
)
2300 *pkt
++ = 'q'; /* info query packet */
2301 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
2302 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
2303 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
2304 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
2309 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2312 parse_threadlist_response (char *pkt
, int result_limit
,
2313 threadref
*original_echo
, threadref
*resultlist
,
2316 struct remote_state
*rs
= get_remote_state ();
2318 int count
, resultcount
, done
;
2321 /* Assume the 'q' and 'M chars have been stripped. */
2322 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
2323 /* done parse past here */
2324 pkt
= unpack_byte (pkt
, &count
); /* count field */
2325 pkt
= unpack_nibble (pkt
, &done
);
2326 /* The first threadid is the argument threadid. */
2327 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
2328 while ((count
-- > 0) && (pkt
< limit
))
2330 pkt
= unpack_threadid (pkt
, resultlist
++);
2331 if (resultcount
++ >= result_limit
)
2340 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
2341 int *done
, int *result_count
, threadref
*threadlist
)
2343 struct remote_state
*rs
= get_remote_state ();
2344 static threadref echo_nextthread
;
2347 /* Trancate result limit to be smaller than the packet size. */
2348 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
2349 >= get_remote_packet_size ())
2350 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
2352 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
2354 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2356 if (*rs
->buf
== '\0')
2360 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
2363 if (!threadmatch (&echo_nextthread
, nextthread
))
2365 /* FIXME: This is a good reason to drop the packet. */
2366 /* Possably, there is a duplicate response. */
2368 retransmit immediatly - race conditions
2369 retransmit after timeout - yes
2371 wait for packet, then exit
2373 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2374 return 0; /* I choose simply exiting. */
2376 if (*result_count
<= 0)
2380 warning (_("RMT ERROR : failed to get remote thread list."));
2383 return result
; /* break; */
2385 if (*result_count
> result_limit
)
2388 warning (_("RMT ERROR: threadlist response longer than requested."));
2394 /* This is the interface between remote and threads, remotes upper
2397 /* remote_find_new_threads retrieves the thread list and for each
2398 thread in the list, looks up the thread in GDB's internal list,
2399 adding the thread if it does not already exist. This involves
2400 getting partial thread lists from the remote target so, polling the
2401 quit_flag is required. */
2404 /* About this many threadisds fit in a packet. */
2406 #define MAXTHREADLISTRESULTS 32
2409 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2412 int done
, i
, result_count
;
2416 static threadref nextthread
;
2417 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
2422 if (loopcount
++ > looplimit
)
2425 warning (_("Remote fetch threadlist -infinite loop-."));
2428 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
2429 &done
, &result_count
, resultthreadlist
))
2434 /* Clear for later iterations. */
2436 /* Setup to resume next batch of thread references, set nextthread. */
2437 if (result_count
>= 1)
2438 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
2440 while (result_count
--)
2441 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
2448 remote_newthread_step (threadref
*ref
, void *context
)
2450 int pid
= ptid_get_pid (inferior_ptid
);
2451 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2453 if (!in_thread_list (ptid
))
2455 return 1; /* continue iterator */
2458 #define CRAZY_MAX_THREADS 1000
2461 remote_current_thread (ptid_t oldpid
)
2463 struct remote_state
*rs
= get_remote_state ();
2466 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2467 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2468 return read_ptid (&rs
->buf
[2], NULL
);
2473 /* Find new threads for info threads command.
2474 * Original version, using John Metzler's thread protocol.
2478 remote_find_new_threads (void)
2480 remote_threadlist_iterator (remote_newthread_step
, 0,
2484 #if defined(HAVE_LIBEXPAT)
2486 typedef struct thread_item
2492 DEF_VEC_O(thread_item_t
);
2494 struct threads_parsing_context
2496 VEC (thread_item_t
) *items
;
2500 start_thread (struct gdb_xml_parser
*parser
,
2501 const struct gdb_xml_element
*element
,
2502 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
2504 struct threads_parsing_context
*data
= user_data
;
2506 struct thread_item item
;
2509 id
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
2510 item
.ptid
= read_ptid (id
, NULL
);
2512 if (VEC_length (gdb_xml_value_s
, attributes
) > 1)
2513 item
.core
= *(ULONGEST
*) VEC_index (gdb_xml_value_s
,
2514 attributes
, 1)->value
;
2520 VEC_safe_push (thread_item_t
, data
->items
, &item
);
2524 end_thread (struct gdb_xml_parser
*parser
,
2525 const struct gdb_xml_element
*element
,
2526 void *user_data
, const char *body_text
)
2528 struct threads_parsing_context
*data
= user_data
;
2530 if (body_text
&& *body_text
)
2531 VEC_last (thread_item_t
, data
->items
)->extra
= xstrdup (body_text
);
2534 const struct gdb_xml_attribute thread_attributes
[] = {
2535 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
2536 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
2537 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
2540 const struct gdb_xml_element thread_children
[] = {
2541 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2544 const struct gdb_xml_element threads_children
[] = {
2545 { "thread", thread_attributes
, thread_children
,
2546 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
2547 start_thread
, end_thread
},
2548 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2551 const struct gdb_xml_element threads_elements
[] = {
2552 { "threads", NULL
, threads_children
,
2553 GDB_XML_EF_NONE
, NULL
, NULL
},
2554 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2557 /* Discard the contents of the constructed thread info context. */
2560 clear_threads_parsing_context (void *p
)
2562 struct threads_parsing_context
*context
= p
;
2564 struct thread_item
*item
;
2566 for (i
= 0; VEC_iterate (thread_item_t
, context
->items
, i
, item
); ++i
)
2567 xfree (item
->extra
);
2569 VEC_free (thread_item_t
, context
->items
);
2575 * Find all threads for info threads command.
2576 * Uses new thread protocol contributed by Cisco.
2577 * Falls back and attempts to use the older method (above)
2578 * if the target doesn't respond to the new method.
2582 remote_threads_info (struct target_ops
*ops
)
2584 struct remote_state
*rs
= get_remote_state ();
2588 if (remote_desc
== 0) /* paranoia */
2589 error (_("Command can only be used when connected to the remote target."));
2591 #if defined(HAVE_LIBEXPAT)
2592 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2594 char *xml
= target_read_stralloc (¤t_target
,
2595 TARGET_OBJECT_THREADS
, NULL
);
2597 struct cleanup
*back_to
= make_cleanup (xfree
, xml
);
2600 struct gdb_xml_parser
*parser
;
2601 struct threads_parsing_context context
;
2602 struct cleanup
*clear_parsing_context
;
2605 /* Note: this parser cleanup is already guarded by BACK_TO
2607 parser
= gdb_xml_create_parser_and_cleanup (_("threads"),
2611 gdb_xml_use_dtd (parser
, "threads.dtd");
2613 clear_parsing_context
2614 = make_cleanup (clear_threads_parsing_context
, &context
);
2616 if (gdb_xml_parse (parser
, xml
) == 0)
2619 struct thread_item
*item
;
2622 VEC_iterate (thread_item_t
, context
.items
, i
, item
);
2625 if (!ptid_equal (item
->ptid
, null_ptid
))
2627 struct private_thread_info
*info
;
2628 /* In non-stop mode, we assume new found threads
2629 are running until proven otherwise with a
2630 stop reply. In all-stop, we can only get
2631 here if all threads are stopped. */
2632 int running
= non_stop
? 1 : 0;
2634 remote_notice_new_inferior (item
->ptid
, running
);
2636 info
= demand_private_info (item
->ptid
);
2637 info
->core
= item
->core
;
2638 info
->extra
= item
->extra
;
2644 do_cleanups (clear_parsing_context
);
2647 do_cleanups (back_to
);
2652 if (use_threadinfo_query
)
2654 putpkt ("qfThreadInfo");
2655 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2657 if (bufp
[0] != '\0') /* q packet recognized */
2659 while (*bufp
++ == 'm') /* reply contains one or more TID */
2663 new_thread
= read_ptid (bufp
, &bufp
);
2664 if (!ptid_equal (new_thread
, null_ptid
))
2666 /* In non-stop mode, we assume new found threads
2667 are running until proven otherwise with a
2668 stop reply. In all-stop, we can only get
2669 here if all threads are stopped. */
2670 int running
= non_stop
? 1 : 0;
2672 remote_notice_new_inferior (new_thread
, running
);
2675 while (*bufp
++ == ','); /* comma-separated list */
2676 putpkt ("qsThreadInfo");
2677 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2684 /* Only qfThreadInfo is supported in non-stop mode. */
2688 /* Else fall back to old method based on jmetzler protocol. */
2689 use_threadinfo_query
= 0;
2690 remote_find_new_threads ();
2695 * Collect a descriptive string about the given thread.
2696 * The target may say anything it wants to about the thread
2697 * (typically info about its blocked / runnable state, name, etc.).
2698 * This string will appear in the info threads display.
2700 * Optional: targets are not required to implement this function.
2704 remote_threads_extra_info (struct thread_info
*tp
)
2706 struct remote_state
*rs
= get_remote_state ();
2710 struct gdb_ext_thread_info threadinfo
;
2711 static char display_buf
[100]; /* arbitrary... */
2712 int n
= 0; /* position in display_buf */
2714 if (remote_desc
== 0) /* paranoia */
2715 internal_error (__FILE__
, __LINE__
,
2716 _("remote_threads_extra_info"));
2718 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2719 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2720 /* This is the main thread which was added by GDB. The remote
2721 server doesn't know about it. */
2724 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2726 struct thread_info
*info
= find_thread_ptid (tp
->ptid
);
2728 if (info
&& info
->private)
2729 return info
->private->extra
;
2734 if (use_threadextra_query
)
2737 char *endb
= rs
->buf
+ get_remote_packet_size ();
2739 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2741 write_ptid (b
, endb
, tp
->ptid
);
2744 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2745 if (rs
->buf
[0] != 0)
2747 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2748 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2749 display_buf
[result
] = '\0';
2754 /* If the above query fails, fall back to the old method. */
2755 use_threadextra_query
= 0;
2756 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2757 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2758 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2759 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2760 if (threadinfo
.active
)
2762 if (*threadinfo
.shortname
)
2763 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2764 " Name: %s,", threadinfo
.shortname
);
2765 if (*threadinfo
.display
)
2766 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2767 " State: %s,", threadinfo
.display
);
2768 if (*threadinfo
.more_display
)
2769 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2770 " Priority: %s", threadinfo
.more_display
);
2774 /* For purely cosmetic reasons, clear up trailing commas. */
2775 if (',' == display_buf
[n
-1])
2776 display_buf
[n
-1] = ' ';
2785 remote_static_tracepoint_marker_at (CORE_ADDR addr
,
2786 struct static_tracepoint_marker
*marker
)
2788 struct remote_state
*rs
= get_remote_state ();
2791 sprintf (p
, "qTSTMat:");
2793 p
+= hexnumstr (p
, addr
);
2795 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2799 error (_("Remote failure reply: %s"), p
);
2803 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2811 free_current_marker (void *arg
)
2813 struct static_tracepoint_marker
**marker_p
= arg
;
2815 if (*marker_p
!= NULL
)
2817 release_static_tracepoint_marker (*marker_p
);
2824 static VEC(static_tracepoint_marker_p
) *
2825 remote_static_tracepoint_markers_by_strid (const char *strid
)
2827 struct remote_state
*rs
= get_remote_state ();
2828 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
2829 struct static_tracepoint_marker
*marker
= NULL
;
2830 struct cleanup
*old_chain
;
2833 /* Ask for a first packet of static tracepoint marker
2836 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2839 error (_("Remote failure reply: %s"), p
);
2841 old_chain
= make_cleanup (free_current_marker
, &marker
);
2846 marker
= XCNEW (struct static_tracepoint_marker
);
2850 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2852 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
2854 VEC_safe_push (static_tracepoint_marker_p
,
2860 release_static_tracepoint_marker (marker
);
2861 memset (marker
, 0, sizeof (*marker
));
2864 while (*p
++ == ','); /* comma-separated list */
2865 /* Ask for another packet of static tracepoint definition. */
2867 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2871 do_cleanups (old_chain
);
2876 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2879 remote_get_ada_task_ptid (long lwp
, long thread
)
2881 return ptid_build (ptid_get_pid (inferior_ptid
), 0, lwp
);
2885 /* Restart the remote side; this is an extended protocol operation. */
2888 extended_remote_restart (void)
2890 struct remote_state
*rs
= get_remote_state ();
2892 /* Send the restart command; for reasons I don't understand the
2893 remote side really expects a number after the "R". */
2894 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2897 remote_fileio_reset ();
2900 /* Clean up connection to a remote debugger. */
2903 remote_close (int quitting
)
2905 if (remote_desc
== NULL
)
2906 return; /* already closed */
2908 /* Make sure we leave stdin registered in the event loop, and we
2909 don't leave the async SIGINT signal handler installed. */
2910 remote_terminal_ours ();
2912 serial_close (remote_desc
);
2915 /* We don't have a connection to the remote stub anymore. Get rid
2916 of all the inferiors and their threads we were controlling. */
2917 discard_all_inferiors ();
2918 inferior_ptid
= null_ptid
;
2920 /* We're no longer interested in any of these events. */
2921 discard_pending_stop_replies (-1);
2923 if (remote_async_inferior_event_token
)
2924 delete_async_event_handler (&remote_async_inferior_event_token
);
2925 if (remote_async_get_pending_events_token
)
2926 delete_async_event_handler (&remote_async_get_pending_events_token
);
2929 /* Query the remote side for the text, data and bss offsets. */
2934 struct remote_state
*rs
= get_remote_state ();
2937 int lose
, num_segments
= 0, do_sections
, do_segments
;
2938 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
2939 struct section_offsets
*offs
;
2940 struct symfile_segment_data
*data
;
2942 if (symfile_objfile
== NULL
)
2945 putpkt ("qOffsets");
2946 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2949 if (buf
[0] == '\000')
2950 return; /* Return silently. Stub doesn't support
2954 warning (_("Remote failure reply: %s"), buf
);
2958 /* Pick up each field in turn. This used to be done with scanf, but
2959 scanf will make trouble if CORE_ADDR size doesn't match
2960 conversion directives correctly. The following code will work
2961 with any size of CORE_ADDR. */
2962 text_addr
= data_addr
= bss_addr
= 0;
2966 if (strncmp (ptr
, "Text=", 5) == 0)
2969 /* Don't use strtol, could lose on big values. */
2970 while (*ptr
&& *ptr
!= ';')
2971 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2973 if (strncmp (ptr
, ";Data=", 6) == 0)
2976 while (*ptr
&& *ptr
!= ';')
2977 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2982 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
2985 while (*ptr
&& *ptr
!= ';')
2986 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
2988 if (bss_addr
!= data_addr
)
2989 warning (_("Target reported unsupported offsets: %s"), buf
);
2994 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
2997 /* Don't use strtol, could lose on big values. */
2998 while (*ptr
&& *ptr
!= ';')
2999 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
3002 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
3005 while (*ptr
&& *ptr
!= ';')
3006 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
3014 error (_("Malformed response to offset query, %s"), buf
);
3015 else if (*ptr
!= '\0')
3016 warning (_("Target reported unsupported offsets: %s"), buf
);
3018 offs
= ((struct section_offsets
*)
3019 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
3020 memcpy (offs
, symfile_objfile
->section_offsets
,
3021 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
3023 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
3024 do_segments
= (data
!= NULL
);
3025 do_sections
= num_segments
== 0;
3027 if (num_segments
> 0)
3029 segments
[0] = text_addr
;
3030 segments
[1] = data_addr
;
3032 /* If we have two segments, we can still try to relocate everything
3033 by assuming that the .text and .data offsets apply to the whole
3034 text and data segments. Convert the offsets given in the packet
3035 to base addresses for symfile_map_offsets_to_segments. */
3036 else if (data
&& data
->num_segments
== 2)
3038 segments
[0] = data
->segment_bases
[0] + text_addr
;
3039 segments
[1] = data
->segment_bases
[1] + data_addr
;
3042 /* If the object file has only one segment, assume that it is text
3043 rather than data; main programs with no writable data are rare,
3044 but programs with no code are useless. Of course the code might
3045 have ended up in the data segment... to detect that we would need
3046 the permissions here. */
3047 else if (data
&& data
->num_segments
== 1)
3049 segments
[0] = data
->segment_bases
[0] + text_addr
;
3052 /* There's no way to relocate by segment. */
3058 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
3059 offs
, num_segments
, segments
);
3061 if (ret
== 0 && !do_sections
)
3062 error (_("Can not handle qOffsets TextSeg "
3063 "response with this symbol file"));
3070 free_symfile_segment_data (data
);
3074 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
3076 /* This is a temporary kludge to force data and bss to use the
3077 same offsets because that's what nlmconv does now. The real
3078 solution requires changes to the stub and remote.c that I
3079 don't have time to do right now. */
3081 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
3082 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
3085 objfile_relocate (symfile_objfile
, offs
);
3088 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3089 threads we know are stopped already. This is used during the
3090 initial remote connection in non-stop mode --- threads that are
3091 reported as already being stopped are left stopped. */
3094 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
3096 /* If we have a stop reply for this thread, it must be stopped. */
3097 if (peek_stop_reply (thread
->ptid
))
3098 set_stop_requested (thread
->ptid
, 1);
3103 /* Stub for catch_exception. */
3105 struct start_remote_args
3109 /* The current target. */
3110 struct target_ops
*target
;
3112 /* Non-zero if this is an extended-remote target. */
3116 /* Send interrupt_sequence to remote target. */
3118 send_interrupt_sequence ()
3120 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
3121 serial_write (remote_desc
, "\x03", 1);
3122 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
3123 serial_send_break (remote_desc
);
3124 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
3126 serial_send_break (remote_desc
);
3127 serial_write (remote_desc
, "g", 1);
3130 internal_error (__FILE__
, __LINE__
,
3131 _("Invalid value for interrupt_sequence_mode: %s."),
3132 interrupt_sequence_mode
);
3136 remote_start_remote (struct ui_out
*uiout
, void *opaque
)
3138 struct start_remote_args
*args
= opaque
;
3139 struct remote_state
*rs
= get_remote_state ();
3140 struct packet_config
*noack_config
;
3141 char *wait_status
= NULL
;
3143 immediate_quit
++; /* Allow user to interrupt it. */
3145 /* Ack any packet which the remote side has already sent. */
3146 serial_write (remote_desc
, "+", 1);
3148 if (interrupt_on_connect
)
3149 send_interrupt_sequence ();
3151 /* The first packet we send to the target is the optional "supported
3152 packets" request. If the target can answer this, it will tell us
3153 which later probes to skip. */
3154 remote_query_supported ();
3156 /* If the stub wants to get a QAllow, compose one and send it. */
3157 if (remote_protocol_packets
[PACKET_QAllow
].support
!= PACKET_DISABLE
)
3158 remote_set_permissions ();
3160 /* Next, we possibly activate noack mode.
3162 If the QStartNoAckMode packet configuration is set to AUTO,
3163 enable noack mode if the stub reported a wish for it with
3166 If set to TRUE, then enable noack mode even if the stub didn't
3167 report it in qSupported. If the stub doesn't reply OK, the
3168 session ends with an error.
3170 If FALSE, then don't activate noack mode, regardless of what the
3171 stub claimed should be the default with qSupported. */
3173 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
3175 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
3176 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
3177 && noack_config
->support
== PACKET_ENABLE
))
3179 putpkt ("QStartNoAckMode");
3180 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3181 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
3185 if (args
->extended_p
)
3187 /* Tell the remote that we are using the extended protocol. */
3189 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3192 /* Next, if the target can specify a description, read it. We do
3193 this before anything involving memory or registers. */
3194 target_find_description ();
3196 /* Next, now that we know something about the target, update the
3197 address spaces in the program spaces. */
3198 update_address_spaces ();
3200 /* On OSs where the list of libraries is global to all
3201 processes, we fetch them early. */
3202 if (gdbarch_has_global_solist (target_gdbarch
))
3203 solib_add (NULL
, args
->from_tty
, args
->target
, auto_solib_add
);
3207 if (!rs
->non_stop_aware
)
3208 error (_("Non-stop mode requested, but remote "
3209 "does not support non-stop"));
3211 putpkt ("QNonStop:1");
3212 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3214 if (strcmp (rs
->buf
, "OK") != 0)
3215 error ("Remote refused setting non-stop mode with: %s", rs
->buf
);
3217 /* Find about threads and processes the stub is already
3218 controlling. We default to adding them in the running state.
3219 The '?' query below will then tell us about which threads are
3221 remote_threads_info (args
->target
);
3223 else if (rs
->non_stop_aware
)
3225 /* Don't assume that the stub can operate in all-stop mode.
3226 Request it explicitely. */
3227 putpkt ("QNonStop:0");
3228 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3230 if (strcmp (rs
->buf
, "OK") != 0)
3231 error ("Remote refused setting all-stop mode with: %s", rs
->buf
);
3234 /* Check whether the target is running now. */
3236 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3240 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
3242 if (!args
->extended_p
)
3243 error (_("The target is not running (try extended-remote?)"));
3245 /* We're connected, but not running. Drop out before we
3246 call start_remote. */
3251 /* Save the reply for later. */
3252 wait_status
= alloca (strlen (rs
->buf
) + 1);
3253 strcpy (wait_status
, rs
->buf
);
3256 /* Let the stub know that we want it to return the thread. */
3257 set_continue_thread (minus_one_ptid
);
3259 /* Without this, some commands which require an active target
3260 (such as kill) won't work. This variable serves (at least)
3261 double duty as both the pid of the target process (if it has
3262 such), and as a flag indicating that a target is active.
3263 These functions should be split out into seperate variables,
3264 especially since GDB will someday have a notion of debugging
3265 several processes. */
3266 inferior_ptid
= magic_null_ptid
;
3268 /* Now, if we have thread information, update inferior_ptid. */
3269 inferior_ptid
= remote_current_thread (inferior_ptid
);
3271 remote_add_inferior (ptid_get_pid (inferior_ptid
), -1);
3273 /* Always add the main thread. */
3274 add_thread_silent (inferior_ptid
);
3276 get_offsets (); /* Get text, data & bss offsets. */
3278 /* If we could not find a description using qXfer, and we know
3279 how to do it some other way, try again. This is not
3280 supported for non-stop; it could be, but it is tricky if
3281 there are no stopped threads when we connect. */
3282 if (remote_read_description_p (args
->target
)
3283 && gdbarch_target_desc (target_gdbarch
) == NULL
)
3285 target_clear_description ();
3286 target_find_description ();
3289 /* Use the previously fetched status. */
3290 gdb_assert (wait_status
!= NULL
);
3291 strcpy (rs
->buf
, wait_status
);
3292 rs
->cached_wait_status
= 1;
3295 start_remote (args
->from_tty
); /* Initialize gdb process mechanisms. */
3299 /* Clear WFI global state. Do this before finding about new
3300 threads and inferiors, and setting the current inferior.
3301 Otherwise we would clear the proceed status of the current
3302 inferior when we want its stop_soon state to be preserved
3303 (see notice_new_inferior). */
3304 init_wait_for_inferior ();
3306 /* In non-stop, we will either get an "OK", meaning that there
3307 are no stopped threads at this time; or, a regular stop
3308 reply. In the latter case, there may be more than one thread
3309 stopped --- we pull them all out using the vStopped
3311 if (strcmp (rs
->buf
, "OK") != 0)
3313 struct stop_reply
*stop_reply
;
3314 struct cleanup
*old_chain
;
3316 stop_reply
= stop_reply_xmalloc ();
3317 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
3319 remote_parse_stop_reply (rs
->buf
, stop_reply
);
3320 discard_cleanups (old_chain
);
3322 /* get_pending_stop_replies acks this one, and gets the rest
3324 pending_stop_reply
= stop_reply
;
3325 remote_get_pending_stop_replies ();
3327 /* Make sure that threads that were stopped remain
3329 iterate_over_threads (set_stop_requested_callback
, NULL
);
3332 if (target_can_async_p ())
3333 target_async (inferior_event_handler
, 0);
3335 if (thread_count () == 0)
3337 if (!args
->extended_p
)
3338 error (_("The target is not running (try extended-remote?)"));
3340 /* We're connected, but not running. Drop out before we
3341 call start_remote. */
3345 /* Let the stub know that we want it to return the thread. */
3347 /* Force the stub to choose a thread. */
3348 set_general_thread (null_ptid
);
3351 inferior_ptid
= remote_current_thread (minus_one_ptid
);
3352 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
3353 error (_("remote didn't report the current thread in non-stop mode"));
3355 get_offsets (); /* Get text, data & bss offsets. */
3357 /* In non-stop mode, any cached wait status will be stored in
3358 the stop reply queue. */
3359 gdb_assert (wait_status
== NULL
);
3361 /* Update the remote on signals to silently pass, or more
3362 importantly, which to not ignore, in case a previous session
3363 had set some different set of signals to be ignored. */
3364 remote_pass_signals ();
3367 /* If we connected to a live target, do some additional setup. */
3368 if (target_has_execution
)
3370 if (exec_bfd
) /* No use without an exec file. */
3371 remote_check_symbols (symfile_objfile
);
3374 /* Possibly the target has been engaged in a trace run started
3375 previously; find out where things are at. */
3376 if (remote_get_trace_status (current_trace_status ()) != -1)
3378 struct uploaded_tp
*uploaded_tps
= NULL
;
3379 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
3381 if (current_trace_status ()->running
)
3382 printf_filtered (_("Trace is already running on the target.\n"));
3384 /* Get trace state variables first, they may be checked when
3385 parsing uploaded commands. */
3387 remote_upload_trace_state_variables (&uploaded_tsvs
);
3389 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
3391 remote_upload_tracepoints (&uploaded_tps
);
3393 merge_uploaded_tracepoints (&uploaded_tps
);
3396 /* If breakpoints are global, insert them now. */
3397 if (gdbarch_has_global_breakpoints (target_gdbarch
)
3398 && breakpoints_always_inserted_mode ())
3399 insert_breakpoints ();
3402 /* Open a connection to a remote debugger.
3403 NAME is the filename used for communication. */
3406 remote_open (char *name
, int from_tty
)
3408 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
3411 /* Open a connection to a remote debugger using the extended
3412 remote gdb protocol. NAME is the filename used for communication. */
3415 extended_remote_open (char *name
, int from_tty
)
3417 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
3420 /* Generic code for opening a connection to a remote target. */
3423 init_all_packet_configs (void)
3427 for (i
= 0; i
< PACKET_MAX
; i
++)
3428 update_packet_config (&remote_protocol_packets
[i
]);
3431 /* Symbol look-up. */
3434 remote_check_symbols (struct objfile
*objfile
)
3436 struct remote_state
*rs
= get_remote_state ();
3437 char *msg
, *reply
, *tmp
;
3438 struct minimal_symbol
*sym
;
3441 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
3444 /* Make sure the remote is pointing at the right process. */
3445 set_general_process ();
3447 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3448 because we need both at the same time. */
3449 msg
= alloca (get_remote_packet_size ());
3451 /* Invite target to request symbol lookups. */
3453 putpkt ("qSymbol::");
3454 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3455 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
3458 while (strncmp (reply
, "qSymbol:", 8) == 0)
3461 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
3463 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
3465 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
3468 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
3469 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
3471 /* If this is a function address, return the start of code
3472 instead of any data function descriptor. */
3473 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
3477 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
3478 phex_nz (sym_addr
, addr_size
), &reply
[8]);
3482 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3487 static struct serial
*
3488 remote_serial_open (char *name
)
3490 static int udp_warning
= 0;
3492 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3493 of in ser-tcp.c, because it is the remote protocol assuming that the
3494 serial connection is reliable and not the serial connection promising
3496 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
3498 warning (_("The remote protocol may be unreliable over UDP.\n"
3499 "Some events may be lost, rendering further debugging "
3504 return serial_open (name
);
3507 /* Inform the target of our permission settings. The permission flags
3508 work without this, but if the target knows the settings, it can do
3509 a couple things. First, it can add its own check, to catch cases
3510 that somehow manage to get by the permissions checks in target
3511 methods. Second, if the target is wired to disallow particular
3512 settings (for instance, a system in the field that is not set up to
3513 be able to stop at a breakpoint), it can object to any unavailable
3517 remote_set_permissions (void)
3519 struct remote_state
*rs
= get_remote_state ();
3521 sprintf (rs
->buf
, "QAllow:"
3522 "WriteReg:%x;WriteMem:%x;"
3523 "InsertBreak:%x;InsertTrace:%x;"
3524 "InsertFastTrace:%x;Stop:%x",
3525 may_write_registers
, may_write_memory
,
3526 may_insert_breakpoints
, may_insert_tracepoints
,
3527 may_insert_fast_tracepoints
, may_stop
);
3529 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3531 /* If the target didn't like the packet, warn the user. Do not try
3532 to undo the user's settings, that would just be maddening. */
3533 if (strcmp (rs
->buf
, "OK") != 0)
3534 warning ("Remote refused setting permissions with: %s", rs
->buf
);
3537 /* This type describes each known response to the qSupported
3539 struct protocol_feature
3541 /* The name of this protocol feature. */
3544 /* The default for this protocol feature. */
3545 enum packet_support default_support
;
3547 /* The function to call when this feature is reported, or after
3548 qSupported processing if the feature is not supported.
3549 The first argument points to this structure. The second
3550 argument indicates whether the packet requested support be
3551 enabled, disabled, or probed (or the default, if this function
3552 is being called at the end of processing and this feature was
3553 not reported). The third argument may be NULL; if not NULL, it
3554 is a NUL-terminated string taken from the packet following
3555 this feature's name and an equals sign. */
3556 void (*func
) (const struct protocol_feature
*, enum packet_support
,
3559 /* The corresponding packet for this feature. Only used if
3560 FUNC is remote_supported_packet. */
3565 remote_supported_packet (const struct protocol_feature
*feature
,
3566 enum packet_support support
,
3567 const char *argument
)
3571 warning (_("Remote qSupported response supplied an unexpected value for"
3572 " \"%s\"."), feature
->name
);
3576 if (remote_protocol_packets
[feature
->packet
].support
3577 == PACKET_SUPPORT_UNKNOWN
)
3578 remote_protocol_packets
[feature
->packet
].support
= support
;
3582 remote_packet_size (const struct protocol_feature
*feature
,
3583 enum packet_support support
, const char *value
)
3585 struct remote_state
*rs
= get_remote_state ();
3590 if (support
!= PACKET_ENABLE
)
3593 if (value
== NULL
|| *value
== '\0')
3595 warning (_("Remote target reported \"%s\" without a size."),
3601 packet_size
= strtol (value
, &value_end
, 16);
3602 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
3604 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3605 feature
->name
, value
);
3609 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
3611 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3612 packet_size
, MAX_REMOTE_PACKET_SIZE
);
3613 packet_size
= MAX_REMOTE_PACKET_SIZE
;
3616 /* Record the new maximum packet size. */
3617 rs
->explicit_packet_size
= packet_size
;
3621 remote_multi_process_feature (const struct protocol_feature
*feature
,
3622 enum packet_support support
, const char *value
)
3624 struct remote_state
*rs
= get_remote_state ();
3626 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
3630 remote_non_stop_feature (const struct protocol_feature
*feature
,
3631 enum packet_support support
, const char *value
)
3633 struct remote_state
*rs
= get_remote_state ();
3635 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
3639 remote_cond_tracepoint_feature (const struct protocol_feature
*feature
,
3640 enum packet_support support
,
3643 struct remote_state
*rs
= get_remote_state ();
3645 rs
->cond_tracepoints
= (support
== PACKET_ENABLE
);
3649 remote_fast_tracepoint_feature (const struct protocol_feature
*feature
,
3650 enum packet_support support
,
3653 struct remote_state
*rs
= get_remote_state ();
3655 rs
->fast_tracepoints
= (support
== PACKET_ENABLE
);
3659 remote_static_tracepoint_feature (const struct protocol_feature
*feature
,
3660 enum packet_support support
,
3663 struct remote_state
*rs
= get_remote_state ();
3665 rs
->static_tracepoints
= (support
== PACKET_ENABLE
);
3669 remote_disconnected_tracing_feature (const struct protocol_feature
*feature
,
3670 enum packet_support support
,
3673 struct remote_state
*rs
= get_remote_state ();
3675 rs
->disconnected_tracing
= (support
== PACKET_ENABLE
);
3678 static struct protocol_feature remote_protocol_features
[] = {
3679 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
3680 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
3681 PACKET_qXfer_auxv
},
3682 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
3683 PACKET_qXfer_features
},
3684 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
3685 PACKET_qXfer_libraries
},
3686 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
3687 PACKET_qXfer_memory_map
},
3688 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
3689 PACKET_qXfer_spu_read
},
3690 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
3691 PACKET_qXfer_spu_write
},
3692 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
3693 PACKET_qXfer_osdata
},
3694 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
3695 PACKET_qXfer_threads
},
3696 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
3697 PACKET_QPassSignals
},
3698 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
3699 PACKET_QStartNoAckMode
},
3700 { "multiprocess", PACKET_DISABLE
, remote_multi_process_feature
, -1 },
3701 { "QNonStop", PACKET_DISABLE
, remote_non_stop_feature
, -1 },
3702 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
3703 PACKET_qXfer_siginfo_read
},
3704 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
3705 PACKET_qXfer_siginfo_write
},
3706 { "ConditionalTracepoints", PACKET_DISABLE
, remote_cond_tracepoint_feature
,
3707 PACKET_ConditionalTracepoints
},
3708 { "FastTracepoints", PACKET_DISABLE
, remote_fast_tracepoint_feature
,
3709 PACKET_FastTracepoints
},
3710 { "StaticTracepoints", PACKET_DISABLE
, remote_static_tracepoint_feature
,
3711 PACKET_StaticTracepoints
},
3712 { "DisconnectedTracing", PACKET_DISABLE
, remote_disconnected_tracing_feature
,
3714 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
3716 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
3718 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
3719 PACKET_TracepointSource
},
3720 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
3724 static char *remote_support_xml
;
3726 /* Register string appended to "xmlRegisters=" in qSupported query. */
3729 register_remote_support_xml (const char *xml
)
3731 #if defined(HAVE_LIBEXPAT)
3732 if (remote_support_xml
== NULL
)
3733 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
3736 char *copy
= xstrdup (remote_support_xml
+ 13);
3737 char *p
= strtok (copy
, ",");
3741 if (strcmp (p
, xml
) == 0)
3748 while ((p
= strtok (NULL
, ",")) != NULL
);
3751 remote_support_xml
= reconcat (remote_support_xml
,
3752 remote_support_xml
, ",", xml
,
3759 remote_query_supported_append (char *msg
, const char *append
)
3762 return reconcat (msg
, msg
, ";", append
, (char *) NULL
);
3764 return xstrdup (append
);
3768 remote_query_supported (void)
3770 struct remote_state
*rs
= get_remote_state ();
3773 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
3775 /* The packet support flags are handled differently for this packet
3776 than for most others. We treat an error, a disabled packet, and
3777 an empty response identically: any features which must be reported
3778 to be used will be automatically disabled. An empty buffer
3779 accomplishes this, since that is also the representation for a list
3780 containing no features. */
3783 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
3786 struct cleanup
*old_chain
= make_cleanup (free_current_contents
, &q
);
3789 q
= remote_query_supported_append (q
, "multiprocess+");
3791 if (remote_support_xml
)
3792 q
= remote_query_supported_append (q
, remote_support_xml
);
3794 q
= remote_query_supported_append (q
, "qRelocInsn+");
3796 q
= reconcat (q
, "qSupported:", q
, (char *) NULL
);
3799 do_cleanups (old_chain
);
3801 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3803 /* If an error occured, warn, but do not return - just reset the
3804 buffer to empty and go on to disable features. */
3805 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
3808 warning (_("Remote failure reply: %s"), rs
->buf
);
3813 memset (seen
, 0, sizeof (seen
));
3818 enum packet_support is_supported
;
3819 char *p
, *end
, *name_end
, *value
;
3821 /* First separate out this item from the rest of the packet. If
3822 there's another item after this, we overwrite the separator
3823 (terminated strings are much easier to work with). */
3825 end
= strchr (p
, ';');
3828 end
= p
+ strlen (p
);
3838 warning (_("empty item in \"qSupported\" response"));
3843 name_end
= strchr (p
, '=');
3846 /* This is a name=value entry. */
3847 is_supported
= PACKET_ENABLE
;
3848 value
= name_end
+ 1;
3857 is_supported
= PACKET_ENABLE
;
3861 is_supported
= PACKET_DISABLE
;
3865 is_supported
= PACKET_SUPPORT_UNKNOWN
;
3869 warning (_("unrecognized item \"%s\" "
3870 "in \"qSupported\" response"), p
);
3876 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3877 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
3879 const struct protocol_feature
*feature
;
3882 feature
= &remote_protocol_features
[i
];
3883 feature
->func (feature
, is_supported
, value
);
3888 /* If we increased the packet size, make sure to increase the global
3889 buffer size also. We delay this until after parsing the entire
3890 qSupported packet, because this is the same buffer we were
3892 if (rs
->buf_size
< rs
->explicit_packet_size
)
3894 rs
->buf_size
= rs
->explicit_packet_size
;
3895 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
3898 /* Handle the defaults for unmentioned features. */
3899 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3902 const struct protocol_feature
*feature
;
3904 feature
= &remote_protocol_features
[i
];
3905 feature
->func (feature
, feature
->default_support
, NULL
);
3911 remote_open_1 (char *name
, int from_tty
,
3912 struct target_ops
*target
, int extended_p
)
3914 struct remote_state
*rs
= get_remote_state ();
3917 error (_("To open a remote debug connection, you need to specify what\n"
3918 "serial device is attached to the remote system\n"
3919 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
3921 /* See FIXME above. */
3922 if (!target_async_permitted
)
3923 wait_forever_enabled_p
= 1;
3925 /* If we're connected to a running target, target_preopen will kill it.
3926 But if we're connected to a target system with no running process,
3927 then we will still be connected when it returns. Ask this question
3928 first, before target_preopen has a chance to kill anything. */
3929 if (remote_desc
!= NULL
&& !have_inferiors ())
3932 || query (_("Already connected to a remote target. Disconnect? ")))
3935 error (_("Still connected."));
3938 target_preopen (from_tty
);
3940 unpush_target (target
);
3942 /* This time without a query. If we were connected to an
3943 extended-remote target and target_preopen killed the running
3944 process, we may still be connected. If we are starting "target
3945 remote" now, the extended-remote target will not have been
3946 removed by unpush_target. */
3947 if (remote_desc
!= NULL
&& !have_inferiors ())
3950 /* Make sure we send the passed signals list the next time we resume. */
3951 xfree (last_pass_packet
);
3952 last_pass_packet
= NULL
;
3954 remote_fileio_reset ();
3955 reopen_exec_file ();
3958 remote_desc
= remote_serial_open (name
);
3960 perror_with_name (name
);
3962 if (baud_rate
!= -1)
3964 if (serial_setbaudrate (remote_desc
, baud_rate
))
3966 /* The requested speed could not be set. Error out to
3967 top level after closing remote_desc. Take care to
3968 set remote_desc to NULL to avoid closing remote_desc
3970 serial_close (remote_desc
);
3972 perror_with_name (name
);
3976 serial_raw (remote_desc
);
3978 /* If there is something sitting in the buffer we might take it as a
3979 response to a command, which would be bad. */
3980 serial_flush_input (remote_desc
);
3984 puts_filtered ("Remote debugging using ");
3985 puts_filtered (name
);
3986 puts_filtered ("\n");
3988 push_target (target
); /* Switch to using remote target now. */
3990 /* Register extra event sources in the event loop. */
3991 remote_async_inferior_event_token
3992 = create_async_event_handler (remote_async_inferior_event_handler
,
3994 remote_async_get_pending_events_token
3995 = create_async_event_handler (remote_async_get_pending_events_handler
,
3998 /* Reset the target state; these things will be queried either by
3999 remote_query_supported or as they are needed. */
4000 init_all_packet_configs ();
4001 rs
->cached_wait_status
= 0;
4002 rs
->explicit_packet_size
= 0;
4004 rs
->multi_process_aware
= 0;
4005 rs
->extended
= extended_p
;
4006 rs
->non_stop_aware
= 0;
4007 rs
->waiting_for_stop_reply
= 0;
4008 rs
->ctrlc_pending_p
= 0;
4010 general_thread
= not_sent_ptid
;
4011 continue_thread
= not_sent_ptid
;
4013 /* Probe for ability to use "ThreadInfo" query, as required. */
4014 use_threadinfo_query
= 1;
4015 use_threadextra_query
= 1;
4017 if (target_async_permitted
)
4019 /* With this target we start out by owning the terminal. */
4020 remote_async_terminal_ours_p
= 1;
4022 /* FIXME: cagney/1999-09-23: During the initial connection it is
4023 assumed that the target is already ready and able to respond to
4024 requests. Unfortunately remote_start_remote() eventually calls
4025 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4026 around this. Eventually a mechanism that allows
4027 wait_for_inferior() to expect/get timeouts will be
4029 wait_forever_enabled_p
= 0;
4032 /* First delete any symbols previously loaded from shared libraries. */
4033 no_shared_libraries (NULL
, 0);
4036 init_thread_list ();
4038 /* Start the remote connection. If error() or QUIT, discard this
4039 target (we'd otherwise be in an inconsistent state) and then
4040 propogate the error on up the exception chain. This ensures that
4041 the caller doesn't stumble along blindly assuming that the
4042 function succeeded. The CLI doesn't have this problem but other
4043 UI's, such as MI do.
4045 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4046 this function should return an error indication letting the
4047 caller restore the previous state. Unfortunately the command
4048 ``target remote'' is directly wired to this function making that
4049 impossible. On a positive note, the CLI side of this problem has
4050 been fixed - the function set_cmd_context() makes it possible for
4051 all the ``target ....'' commands to share a common callback
4052 function. See cli-dump.c. */
4054 struct gdb_exception ex
;
4055 struct start_remote_args args
;
4057 args
.from_tty
= from_tty
;
4058 args
.target
= target
;
4059 args
.extended_p
= extended_p
;
4061 ex
= catch_exception (uiout
, remote_start_remote
, &args
, RETURN_MASK_ALL
);
4064 /* Pop the partially set up target - unless something else did
4065 already before throwing the exception. */
4066 if (remote_desc
!= NULL
)
4068 if (target_async_permitted
)
4069 wait_forever_enabled_p
= 1;
4070 throw_exception (ex
);
4074 if (target_async_permitted
)
4075 wait_forever_enabled_p
= 1;
4078 /* This takes a program previously attached to and detaches it. After
4079 this is done, GDB can be used to debug some other program. We
4080 better not have left any breakpoints in the target program or it'll
4081 die when it hits one. */
4084 remote_detach_1 (char *args
, int from_tty
, int extended
)
4086 int pid
= ptid_get_pid (inferior_ptid
);
4087 struct remote_state
*rs
= get_remote_state ();
4090 error (_("Argument given to \"detach\" when remotely debugging."));
4092 if (!target_has_execution
)
4093 error (_("No process to detach from."));
4095 /* Tell the remote target to detach. */
4096 if (remote_multi_process_p (rs
))
4097 sprintf (rs
->buf
, "D;%x", pid
);
4099 strcpy (rs
->buf
, "D");
4102 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4104 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
4106 else if (rs
->buf
[0] == '\0')
4107 error (_("Remote doesn't know how to detach"));
4109 error (_("Can't detach process."));
4113 if (remote_multi_process_p (rs
))
4114 printf_filtered (_("Detached from remote %s.\n"),
4115 target_pid_to_str (pid_to_ptid (pid
)));
4119 puts_filtered (_("Detached from remote process.\n"));
4121 puts_filtered (_("Ending remote debugging.\n"));
4125 discard_pending_stop_replies (pid
);
4126 target_mourn_inferior ();
4130 remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
4132 remote_detach_1 (args
, from_tty
, 0);
4136 extended_remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
4138 remote_detach_1 (args
, from_tty
, 1);
4141 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4144 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
4147 error (_("Argument given to \"disconnect\" when remotely debugging."));
4149 /* Make sure we unpush even the extended remote targets; mourn
4150 won't do it. So call remote_mourn_1 directly instead of
4151 target_mourn_inferior. */
4152 remote_mourn_1 (target
);
4155 puts_filtered ("Ending remote debugging.\n");
4158 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4159 be chatty about it. */
4162 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
4164 struct remote_state
*rs
= get_remote_state ();
4166 char *wait_status
= NULL
;
4168 pid
= parse_pid_to_attach (args
);
4170 /* Remote PID can be freely equal to getpid, do not check it here the same
4171 way as in other targets. */
4173 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4174 error (_("This target does not support attaching to a process"));
4176 sprintf (rs
->buf
, "vAttach;%x", pid
);
4178 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4180 if (packet_ok (rs
->buf
,
4181 &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
4184 printf_unfiltered (_("Attached to %s\n"),
4185 target_pid_to_str (pid_to_ptid (pid
)));
4189 /* Save the reply for later. */
4190 wait_status
= alloca (strlen (rs
->buf
) + 1);
4191 strcpy (wait_status
, rs
->buf
);
4193 else if (strcmp (rs
->buf
, "OK") != 0)
4194 error (_("Attaching to %s failed with: %s"),
4195 target_pid_to_str (pid_to_ptid (pid
)),
4198 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4199 error (_("This target does not support attaching to a process"));
4201 error (_("Attaching to %s failed"),
4202 target_pid_to_str (pid_to_ptid (pid
)));
4204 set_current_inferior (remote_add_inferior (pid
, 1));
4206 inferior_ptid
= pid_to_ptid (pid
);
4210 struct thread_info
*thread
;
4212 /* Get list of threads. */
4213 remote_threads_info (target
);
4215 thread
= first_thread_of_process (pid
);
4217 inferior_ptid
= thread
->ptid
;
4219 inferior_ptid
= pid_to_ptid (pid
);
4221 /* Invalidate our notion of the remote current thread. */
4222 record_currthread (minus_one_ptid
);
4226 /* Now, if we have thread information, update inferior_ptid. */
4227 inferior_ptid
= remote_current_thread (inferior_ptid
);
4229 /* Add the main thread to the thread list. */
4230 add_thread_silent (inferior_ptid
);
4233 /* Next, if the target can specify a description, read it. We do
4234 this before anything involving memory or registers. */
4235 target_find_description ();
4239 /* Use the previously fetched status. */
4240 gdb_assert (wait_status
!= NULL
);
4242 if (target_can_async_p ())
4244 struct stop_reply
*stop_reply
;
4245 struct cleanup
*old_chain
;
4247 stop_reply
= stop_reply_xmalloc ();
4248 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4249 remote_parse_stop_reply (wait_status
, stop_reply
);
4250 discard_cleanups (old_chain
);
4251 push_stop_reply (stop_reply
);
4253 target_async (inferior_event_handler
, 0);
4257 gdb_assert (wait_status
!= NULL
);
4258 strcpy (rs
->buf
, wait_status
);
4259 rs
->cached_wait_status
= 1;
4263 gdb_assert (wait_status
== NULL
);
4267 extended_remote_attach (struct target_ops
*ops
, char *args
, int from_tty
)
4269 extended_remote_attach_1 (ops
, args
, from_tty
);
4272 /* Convert hex digit A to a number. */
4277 if (a
>= '0' && a
<= '9')
4279 else if (a
>= 'a' && a
<= 'f')
4280 return a
- 'a' + 10;
4281 else if (a
>= 'A' && a
<= 'F')
4282 return a
- 'A' + 10;
4284 error (_("Reply contains invalid hex digit %d"), a
);
4288 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
4292 for (i
= 0; i
< count
; i
++)
4294 if (hex
[0] == 0 || hex
[1] == 0)
4296 /* Hex string is short, or of uneven length.
4297 Return the count that has been converted so far. */
4300 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
4306 /* Convert number NIB to a hex digit. */
4314 return 'a' + nib
- 10;
4318 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
4322 /* May use a length, or a nul-terminated string as input. */
4324 count
= strlen ((char *) bin
);
4326 for (i
= 0; i
< count
; i
++)
4328 *hex
++ = tohex ((*bin
>> 4) & 0xf);
4329 *hex
++ = tohex (*bin
++ & 0xf);
4335 /* Check for the availability of vCont. This function should also check
4339 remote_vcont_probe (struct remote_state
*rs
)
4343 strcpy (rs
->buf
, "vCont?");
4345 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4348 /* Make sure that the features we assume are supported. */
4349 if (strncmp (buf
, "vCont", 5) == 0)
4352 int support_s
, support_S
, support_c
, support_C
;
4358 rs
->support_vCont_t
= 0;
4359 while (p
&& *p
== ';')
4362 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4364 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4366 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4368 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4370 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4371 rs
->support_vCont_t
= 1;
4373 p
= strchr (p
, ';');
4376 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4377 BUF will make packet_ok disable the packet. */
4378 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
4382 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
4385 /* Helper function for building "vCont" resumptions. Write a
4386 resumption to P. ENDP points to one-passed-the-end of the buffer
4387 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4388 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4389 resumed thread should be single-stepped and/or signalled. If PTID
4390 equals minus_one_ptid, then all threads are resumed; if PTID
4391 represents a process, then all threads of the process are resumed;
4392 the thread to be stepped and/or signalled is given in the global
4396 append_resumption (char *p
, char *endp
,
4397 ptid_t ptid
, int step
, enum target_signal siggnal
)
4399 struct remote_state
*rs
= get_remote_state ();
4401 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
4402 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
4404 p
+= xsnprintf (p
, endp
- p
, ";s");
4405 else if (siggnal
!= TARGET_SIGNAL_0
)
4406 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
4408 p
+= xsnprintf (p
, endp
- p
, ";c");
4410 if (remote_multi_process_p (rs
) && ptid_is_pid (ptid
))
4414 /* All (-1) threads of process. */
4415 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4417 p
+= xsnprintf (p
, endp
- p
, ":");
4418 p
= write_ptid (p
, endp
, nptid
);
4420 else if (!ptid_equal (ptid
, minus_one_ptid
))
4422 p
+= xsnprintf (p
, endp
- p
, ":");
4423 p
= write_ptid (p
, endp
, ptid
);
4429 /* Resume the remote inferior by using a "vCont" packet. The thread
4430 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4431 resumed thread should be single-stepped and/or signalled. If PTID
4432 equals minus_one_ptid, then all threads are resumed; the thread to
4433 be stepped and/or signalled is given in the global INFERIOR_PTID.
4434 This function returns non-zero iff it resumes the inferior.
4436 This function issues a strict subset of all possible vCont commands at the
4440 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
4442 struct remote_state
*rs
= get_remote_state ();
4446 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4447 remote_vcont_probe (rs
);
4449 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
4453 endp
= rs
->buf
+ get_remote_packet_size ();
4455 /* If we could generate a wider range of packets, we'd have to worry
4456 about overflowing BUF. Should there be a generic
4457 "multi-part-packet" packet? */
4459 p
+= xsnprintf (p
, endp
- p
, "vCont");
4461 if (ptid_equal (ptid
, magic_null_ptid
))
4463 /* MAGIC_NULL_PTID means that we don't have any active threads,
4464 so we don't have any TID numbers the inferior will
4465 understand. Make sure to only send forms that do not specify
4467 p
= append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
4469 else if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
4471 /* Resume all threads (of all processes, or of a single
4472 process), with preference for INFERIOR_PTID. This assumes
4473 inferior_ptid belongs to the set of all threads we are about
4475 if (step
|| siggnal
!= TARGET_SIGNAL_0
)
4477 /* Step inferior_ptid, with or without signal. */
4478 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
4481 /* And continue others without a signal. */
4482 p
= append_resumption (p
, endp
, ptid
, /*step=*/ 0, TARGET_SIGNAL_0
);
4486 /* Scheduler locking; resume only PTID. */
4487 p
= append_resumption (p
, endp
, ptid
, step
, siggnal
);
4490 gdb_assert (strlen (rs
->buf
) < get_remote_packet_size ());
4495 /* In non-stop, the stub replies to vCont with "OK". The stop
4496 reply will be reported asynchronously by means of a `%Stop'
4498 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4499 if (strcmp (rs
->buf
, "OK") != 0)
4500 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
);
4506 /* Tell the remote machine to resume. */
4508 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
4510 static int last_sent_step
;
4513 remote_resume (struct target_ops
*ops
,
4514 ptid_t ptid
, int step
, enum target_signal siggnal
)
4516 struct remote_state
*rs
= get_remote_state ();
4519 last_sent_signal
= siggnal
;
4520 last_sent_step
= step
;
4522 /* Update the inferior on signals to silently pass, if they've changed. */
4523 remote_pass_signals ();
4525 /* The vCont packet doesn't need to specify threads via Hc. */
4526 /* No reverse support (yet) for vCont. */
4527 if (execution_direction
!= EXEC_REVERSE
)
4528 if (remote_vcont_resume (ptid
, step
, siggnal
))
4531 /* All other supported resume packets do use Hc, so set the continue
4533 if (ptid_equal (ptid
, minus_one_ptid
))
4534 set_continue_thread (any_thread_ptid
);
4536 set_continue_thread (ptid
);
4539 if (execution_direction
== EXEC_REVERSE
)
4541 /* We don't pass signals to the target in reverse exec mode. */
4542 if (info_verbose
&& siggnal
!= TARGET_SIGNAL_0
)
4543 warning (" - Can't pass signal %d to target in reverse: ignored.\n",
4547 && remote_protocol_packets
[PACKET_bs
].support
== PACKET_DISABLE
)
4548 error (_("Remote reverse-step not supported."));
4550 && remote_protocol_packets
[PACKET_bc
].support
== PACKET_DISABLE
)
4551 error (_("Remote reverse-continue not supported."));
4553 strcpy (buf
, step
? "bs" : "bc");
4555 else if (siggnal
!= TARGET_SIGNAL_0
)
4557 buf
[0] = step
? 'S' : 'C';
4558 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
4559 buf
[2] = tohex (((int) siggnal
) & 0xf);
4563 strcpy (buf
, step
? "s" : "c");
4568 /* We are about to start executing the inferior, let's register it
4569 with the event loop. NOTE: this is the one place where all the
4570 execution commands end up. We could alternatively do this in each
4571 of the execution commands in infcmd.c. */
4572 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4573 into infcmd.c in order to allow inferior function calls to work
4574 NOT asynchronously. */
4575 if (target_can_async_p ())
4576 target_async (inferior_event_handler
, 0);
4578 /* We've just told the target to resume. The remote server will
4579 wait for the inferior to stop, and then send a stop reply. In
4580 the mean time, we can't start another command/query ourselves
4581 because the stub wouldn't be ready to process it. This applies
4582 only to the base all-stop protocol, however. In non-stop (which
4583 only supports vCont), the stub replies with an "OK", and is
4584 immediate able to process further serial input. */
4586 rs
->waiting_for_stop_reply
= 1;
4590 /* Set up the signal handler for SIGINT, while the target is
4591 executing, ovewriting the 'regular' SIGINT signal handler. */
4593 initialize_sigint_signal_handler (void)
4595 signal (SIGINT
, handle_remote_sigint
);
4598 /* Signal handler for SIGINT, while the target is executing. */
4600 handle_remote_sigint (int sig
)
4602 signal (sig
, handle_remote_sigint_twice
);
4603 mark_async_signal_handler_wrapper (sigint_remote_token
);
4606 /* Signal handler for SIGINT, installed after SIGINT has already been
4607 sent once. It will take effect the second time that the user sends
4610 handle_remote_sigint_twice (int sig
)
4612 signal (sig
, handle_remote_sigint
);
4613 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
4616 /* Perform the real interruption of the target execution, in response
4619 async_remote_interrupt (gdb_client_data arg
)
4622 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
4624 target_stop (inferior_ptid
);
4627 /* Perform interrupt, if the first attempt did not succeed. Just give
4628 up on the target alltogether. */
4630 async_remote_interrupt_twice (gdb_client_data arg
)
4633 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
4638 /* Reinstall the usual SIGINT handlers, after the target has
4641 cleanup_sigint_signal_handler (void *dummy
)
4643 signal (SIGINT
, handle_sigint
);
4646 /* Send ^C to target to halt it. Target will respond, and send us a
4648 static void (*ofunc
) (int);
4650 /* The command line interface's stop routine. This function is installed
4651 as a signal handler for SIGINT. The first time a user requests a
4652 stop, we call remote_stop to send a break or ^C. If there is no
4653 response from the target (it didn't stop when the user requested it),
4654 we ask the user if he'd like to detach from the target. */
4656 remote_interrupt (int signo
)
4658 /* If this doesn't work, try more severe steps. */
4659 signal (signo
, remote_interrupt_twice
);
4661 gdb_call_async_signal_handler (sigint_remote_token
, 1);
4664 /* The user typed ^C twice. */
4667 remote_interrupt_twice (int signo
)
4669 signal (signo
, ofunc
);
4670 gdb_call_async_signal_handler (sigint_remote_twice_token
, 1);
4671 signal (signo
, remote_interrupt
);
4674 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
4675 thread, all threads of a remote process, or all threads of all
4679 remote_stop_ns (ptid_t ptid
)
4681 struct remote_state
*rs
= get_remote_state ();
4683 char *endp
= rs
->buf
+ get_remote_packet_size ();
4685 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4686 remote_vcont_probe (rs
);
4688 if (!rs
->support_vCont_t
)
4689 error (_("Remote server does not support stopping threads"));
4691 if (ptid_equal (ptid
, minus_one_ptid
)
4692 || (!remote_multi_process_p (rs
) && ptid_is_pid (ptid
)))
4693 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
4698 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
4700 if (ptid_is_pid (ptid
))
4701 /* All (-1) threads of process. */
4702 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4705 /* Small optimization: if we already have a stop reply for
4706 this thread, no use in telling the stub we want this
4708 if (peek_stop_reply (ptid
))
4714 p
= write_ptid (p
, endp
, nptid
);
4717 /* In non-stop, we get an immediate OK reply. The stop reply will
4718 come in asynchronously by notification. */
4720 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4721 if (strcmp (rs
->buf
, "OK") != 0)
4722 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
), rs
->buf
);
4725 /* All-stop version of target_stop. Sends a break or a ^C to stop the
4726 remote target. It is undefined which thread of which process
4727 reports the stop. */
4730 remote_stop_as (ptid_t ptid
)
4732 struct remote_state
*rs
= get_remote_state ();
4734 rs
->ctrlc_pending_p
= 1;
4736 /* If the inferior is stopped already, but the core didn't know
4737 about it yet, just ignore the request. The cached wait status
4738 will be collected in remote_wait. */
4739 if (rs
->cached_wait_status
)
4742 /* Send interrupt_sequence to remote target. */
4743 send_interrupt_sequence ();
4746 /* This is the generic stop called via the target vector. When a target
4747 interrupt is requested, either by the command line or the GUI, we
4748 will eventually end up here. */
4751 remote_stop (ptid_t ptid
)
4754 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
4757 remote_stop_ns (ptid
);
4759 remote_stop_as (ptid
);
4762 /* Ask the user what to do when an interrupt is received. */
4765 interrupt_query (void)
4767 target_terminal_ours ();
4769 if (target_can_async_p ())
4771 signal (SIGINT
, handle_sigint
);
4772 deprecated_throw_reason (RETURN_QUIT
);
4776 if (query (_("Interrupted while waiting for the program.\n\
4777 Give up (and stop debugging it)? ")))
4780 deprecated_throw_reason (RETURN_QUIT
);
4784 target_terminal_inferior ();
4787 /* Enable/disable target terminal ownership. Most targets can use
4788 terminal groups to control terminal ownership. Remote targets are
4789 different in that explicit transfer of ownership to/from GDB/target
4793 remote_terminal_inferior (void)
4795 if (!target_async_permitted
)
4796 /* Nothing to do. */
4799 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
4800 idempotent. The event-loop GDB talking to an asynchronous target
4801 with a synchronous command calls this function from both
4802 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
4803 transfer the terminal to the target when it shouldn't this guard
4805 if (!remote_async_terminal_ours_p
)
4807 delete_file_handler (input_fd
);
4808 remote_async_terminal_ours_p
= 0;
4809 initialize_sigint_signal_handler ();
4810 /* NOTE: At this point we could also register our selves as the
4811 recipient of all input. Any characters typed could then be
4812 passed on down to the target. */
4816 remote_terminal_ours (void)
4818 if (!target_async_permitted
)
4819 /* Nothing to do. */
4822 /* See FIXME in remote_terminal_inferior. */
4823 if (remote_async_terminal_ours_p
)
4825 cleanup_sigint_signal_handler (NULL
);
4826 add_file_handler (input_fd
, stdin_event_handler
, 0);
4827 remote_async_terminal_ours_p
= 1;
4831 remote_console_output (char *msg
)
4835 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
4838 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
4842 fputs_unfiltered (tb
, gdb_stdtarg
);
4844 gdb_flush (gdb_stdtarg
);
4847 typedef struct cached_reg
4850 gdb_byte data
[MAX_REGISTER_SIZE
];
4853 DEF_VEC_O(cached_reg_t
);
4857 struct stop_reply
*next
;
4861 struct target_waitstatus ws
;
4863 VEC(cached_reg_t
) *regcache
;
4865 int stopped_by_watchpoint_p
;
4866 CORE_ADDR watch_data_address
;
4874 /* The list of already fetched and acknowledged stop events. */
4875 static struct stop_reply
*stop_reply_queue
;
4877 static struct stop_reply
*
4878 stop_reply_xmalloc (void)
4880 struct stop_reply
*r
= XMALLOC (struct stop_reply
);
4887 stop_reply_xfree (struct stop_reply
*r
)
4891 VEC_free (cached_reg_t
, r
->regcache
);
4896 /* Discard all pending stop replies of inferior PID. If PID is -1,
4897 discard everything. */
4900 discard_pending_stop_replies (int pid
)
4902 struct stop_reply
*prev
= NULL
, *reply
, *next
;
4904 /* Discard the in-flight notification. */
4905 if (pending_stop_reply
!= NULL
4907 || ptid_get_pid (pending_stop_reply
->ptid
) == pid
))
4909 stop_reply_xfree (pending_stop_reply
);
4910 pending_stop_reply
= NULL
;
4913 /* Discard the stop replies we have already pulled with
4915 for (reply
= stop_reply_queue
; reply
; reply
= next
)
4919 || ptid_get_pid (reply
->ptid
) == pid
)
4921 if (reply
== stop_reply_queue
)
4922 stop_reply_queue
= reply
->next
;
4924 prev
->next
= reply
->next
;
4926 stop_reply_xfree (reply
);
4933 /* Cleanup wrapper. */
4936 do_stop_reply_xfree (void *arg
)
4938 struct stop_reply
*r
= arg
;
4940 stop_reply_xfree (r
);
4943 /* Look for a queued stop reply belonging to PTID. If one is found,
4944 remove it from the queue, and return it. Returns NULL if none is
4945 found. If there are still queued events left to process, tell the
4946 event loop to get back to target_wait soon. */
4948 static struct stop_reply
*
4949 queued_stop_reply (ptid_t ptid
)
4951 struct stop_reply
*it
;
4952 struct stop_reply
**it_link
;
4954 it
= stop_reply_queue
;
4955 it_link
= &stop_reply_queue
;
4958 if (ptid_match (it
->ptid
, ptid
))
4960 *it_link
= it
->next
;
4965 it_link
= &it
->next
;
4969 if (stop_reply_queue
)
4970 /* There's still at least an event left. */
4971 mark_async_event_handler (remote_async_inferior_event_token
);
4976 /* Push a fully parsed stop reply in the stop reply queue. Since we
4977 know that we now have at least one queued event left to pass to the
4978 core side, tell the event loop to get back to target_wait soon. */
4981 push_stop_reply (struct stop_reply
*new_event
)
4983 struct stop_reply
*event
;
4985 if (stop_reply_queue
)
4987 for (event
= stop_reply_queue
;
4988 event
&& event
->next
;
4989 event
= event
->next
)
4992 event
->next
= new_event
;
4995 stop_reply_queue
= new_event
;
4997 mark_async_event_handler (remote_async_inferior_event_token
);
5000 /* Returns true if we have a stop reply for PTID. */
5003 peek_stop_reply (ptid_t ptid
)
5005 struct stop_reply
*it
;
5007 for (it
= stop_reply_queue
; it
; it
= it
->next
)
5008 if (ptid_equal (ptid
, it
->ptid
))
5010 if (it
->ws
.kind
== TARGET_WAITKIND_STOPPED
)
5017 /* Parse the stop reply in BUF. Either the function succeeds, and the
5018 result is stored in EVENT, or throws an error. */
5021 remote_parse_stop_reply (char *buf
, struct stop_reply
*event
)
5023 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5027 event
->ptid
= null_ptid
;
5028 event
->ws
.kind
= TARGET_WAITKIND_IGNORE
;
5029 event
->ws
.value
.integer
= 0;
5030 event
->solibs_changed
= 0;
5031 event
->replay_event
= 0;
5032 event
->stopped_by_watchpoint_p
= 0;
5033 event
->regcache
= NULL
;
5038 case 'T': /* Status with PC, SP, FP, ... */
5039 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5040 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5042 n... = register number
5043 r... = register contents
5046 p
= &buf
[3]; /* after Txx */
5054 /* If the packet contains a register number, save it in
5055 pnum and set p1 to point to the character following it.
5056 Otherwise p1 points to p. */
5058 /* If this packet is an awatch packet, don't parse the 'a'
5059 as a register number. */
5061 if (strncmp (p
, "awatch", strlen("awatch")) != 0
5062 && strncmp (p
, "core", strlen ("core") != 0))
5064 /* Read the ``P'' register number. */
5065 pnum
= strtol (p
, &p_temp
, 16);
5071 if (p1
== p
) /* No register number present here. */
5073 p1
= strchr (p
, ':');
5075 error (_("Malformed packet(a) (missing colon): %s\n\
5078 if (strncmp (p
, "thread", p1
- p
) == 0)
5079 event
->ptid
= read_ptid (++p1
, &p
);
5080 else if ((strncmp (p
, "watch", p1
- p
) == 0)
5081 || (strncmp (p
, "rwatch", p1
- p
) == 0)
5082 || (strncmp (p
, "awatch", p1
- p
) == 0))
5084 event
->stopped_by_watchpoint_p
= 1;
5085 p
= unpack_varlen_hex (++p1
, &addr
);
5086 event
->watch_data_address
= (CORE_ADDR
) addr
;
5088 else if (strncmp (p
, "library", p1
- p
) == 0)
5092 while (*p_temp
&& *p_temp
!= ';')
5095 event
->solibs_changed
= 1;
5098 else if (strncmp (p
, "replaylog", p1
- p
) == 0)
5100 /* NO_HISTORY event.
5101 p1 will indicate "begin" or "end", but
5102 it makes no difference for now, so ignore it. */
5103 event
->replay_event
= 1;
5104 p_temp
= strchr (p1
+ 1, ';');
5108 else if (strncmp (p
, "core", p1
- p
) == 0)
5112 p
= unpack_varlen_hex (++p1
, &c
);
5117 /* Silently skip unknown optional info. */
5118 p_temp
= strchr (p1
+ 1, ';');
5125 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
5126 cached_reg_t cached_reg
;
5131 error (_("Malformed packet(b) (missing colon): %s\n\
5137 error (_("Remote sent bad register number %s: %s\n\
5139 hex_string (pnum
), p
, buf
);
5141 cached_reg
.num
= reg
->regnum
;
5143 fieldsize
= hex2bin (p
, cached_reg
.data
,
5144 register_size (target_gdbarch
,
5147 if (fieldsize
< register_size (target_gdbarch
,
5149 warning (_("Remote reply is too short: %s"), buf
);
5151 VEC_safe_push (cached_reg_t
, event
->regcache
, &cached_reg
);
5155 error (_("Remote register badly formatted: %s\nhere: %s"),
5160 case 'S': /* Old style status, just signal only. */
5161 if (event
->solibs_changed
)
5162 event
->ws
.kind
= TARGET_WAITKIND_LOADED
;
5163 else if (event
->replay_event
)
5164 event
->ws
.kind
= TARGET_WAITKIND_NO_HISTORY
;
5167 event
->ws
.kind
= TARGET_WAITKIND_STOPPED
;
5168 event
->ws
.value
.sig
= (enum target_signal
)
5169 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
5172 case 'W': /* Target exited. */
5179 /* GDB used to accept only 2 hex chars here. Stubs should
5180 only send more if they detect GDB supports multi-process
5182 p
= unpack_varlen_hex (&buf
[1], &value
);
5186 /* The remote process exited. */
5187 event
->ws
.kind
= TARGET_WAITKIND_EXITED
;
5188 event
->ws
.value
.integer
= value
;
5192 /* The remote process exited with a signal. */
5193 event
->ws
.kind
= TARGET_WAITKIND_SIGNALLED
;
5194 event
->ws
.value
.sig
= (enum target_signal
) value
;
5197 /* If no process is specified, assume inferior_ptid. */
5198 pid
= ptid_get_pid (inferior_ptid
);
5207 else if (strncmp (p
,
5208 "process:", sizeof ("process:") - 1) == 0)
5212 p
+= sizeof ("process:") - 1;
5213 unpack_varlen_hex (p
, &upid
);
5217 error (_("unknown stop reply packet: %s"), buf
);
5220 error (_("unknown stop reply packet: %s"), buf
);
5221 event
->ptid
= pid_to_ptid (pid
);
5226 if (non_stop
&& ptid_equal (event
->ptid
, null_ptid
))
5227 error (_("No process or thread specified in stop reply: %s"), buf
);
5230 /* When the stub wants to tell GDB about a new stop reply, it sends a
5231 stop notification (%Stop). Those can come it at any time, hence,
5232 we have to make sure that any pending putpkt/getpkt sequence we're
5233 making is finished, before querying the stub for more events with
5234 vStopped. E.g., if we started a vStopped sequence immediatelly
5235 upon receiving the %Stop notification, something like this could
5243 1.6) <-- (registers reply to step #1.3)
5245 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5248 To solve this, whenever we parse a %Stop notification sucessfully,
5249 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5250 doing whatever we were doing:
5256 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5257 2.5) <-- (registers reply to step #2.3)
5259 Eventualy after step #2.5, we return to the event loop, which
5260 notices there's an event on the
5261 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5262 associated callback --- the function below. At this point, we're
5263 always safe to start a vStopped sequence. :
5266 2.7) <-- T05 thread:2
5272 remote_get_pending_stop_replies (void)
5274 struct remote_state
*rs
= get_remote_state ();
5276 if (pending_stop_reply
)
5279 putpkt ("vStopped");
5281 /* Now we can rely on it. */
5282 push_stop_reply (pending_stop_reply
);
5283 pending_stop_reply
= NULL
;
5287 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5288 if (strcmp (rs
->buf
, "OK") == 0)
5292 struct cleanup
*old_chain
;
5293 struct stop_reply
*stop_reply
= stop_reply_xmalloc ();
5295 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
5296 remote_parse_stop_reply (rs
->buf
, stop_reply
);
5299 putpkt ("vStopped");
5301 if (stop_reply
->ws
.kind
!= TARGET_WAITKIND_IGNORE
)
5303 /* Now we can rely on it. */
5304 discard_cleanups (old_chain
);
5305 push_stop_reply (stop_reply
);
5308 /* We got an unknown stop reply. */
5309 do_cleanups (old_chain
);
5316 /* Called when it is decided that STOP_REPLY holds the info of the
5317 event that is to be returned to the core. This function always
5318 destroys STOP_REPLY. */
5321 process_stop_reply (struct stop_reply
*stop_reply
,
5322 struct target_waitstatus
*status
)
5326 *status
= stop_reply
->ws
;
5327 ptid
= stop_reply
->ptid
;
5329 /* If no thread/process was reported by the stub, assume the current
5331 if (ptid_equal (ptid
, null_ptid
))
5332 ptid
= inferior_ptid
;
5334 if (status
->kind
!= TARGET_WAITKIND_EXITED
5335 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5337 /* Expedited registers. */
5338 if (stop_reply
->regcache
)
5340 struct regcache
*regcache
5341 = get_thread_arch_regcache (ptid
, target_gdbarch
);
5346 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
5348 regcache_raw_supply (regcache
, reg
->num
, reg
->data
);
5349 VEC_free (cached_reg_t
, stop_reply
->regcache
);
5352 remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
5353 remote_watch_data_address
= stop_reply
->watch_data_address
;
5355 remote_notice_new_inferior (ptid
, 0);
5356 demand_private_info (ptid
)->core
= stop_reply
->core
;
5359 stop_reply_xfree (stop_reply
);
5363 /* The non-stop mode version of target_wait. */
5366 remote_wait_ns (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5368 struct remote_state
*rs
= get_remote_state ();
5369 struct stop_reply
*stop_reply
;
5372 /* If in non-stop mode, get out of getpkt even if a
5373 notification is received. */
5375 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5382 case 'E': /* Error of some sort. */
5383 /* We're out of sync with the target now. Did it continue
5384 or not? We can't tell which thread it was in non-stop,
5385 so just ignore this. */
5386 warning (_("Remote failure reply: %s"), rs
->buf
);
5388 case 'O': /* Console output. */
5389 remote_console_output (rs
->buf
+ 1);
5392 warning (_("Invalid remote reply: %s"), rs
->buf
);
5396 /* Acknowledge a pending stop reply that may have arrived in the
5398 if (pending_stop_reply
!= NULL
)
5399 remote_get_pending_stop_replies ();
5401 /* If indeed we noticed a stop reply, we're done. */
5402 stop_reply
= queued_stop_reply (ptid
);
5403 if (stop_reply
!= NULL
)
5404 return process_stop_reply (stop_reply
, status
);
5406 /* Still no event. If we're just polling for an event, then
5407 return to the event loop. */
5408 if (options
& TARGET_WNOHANG
)
5410 status
->kind
= TARGET_WAITKIND_IGNORE
;
5411 return minus_one_ptid
;
5414 /* Otherwise do a blocking wait. */
5415 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5420 /* Wait until the remote machine stops, then return, storing status in
5421 STATUS just as `wait' would. */
5424 remote_wait_as (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5426 struct remote_state
*rs
= get_remote_state ();
5427 ptid_t event_ptid
= null_ptid
;
5429 struct stop_reply
*stop_reply
;
5433 status
->kind
= TARGET_WAITKIND_IGNORE
;
5434 status
->value
.integer
= 0;
5436 stop_reply
= queued_stop_reply (ptid
);
5437 if (stop_reply
!= NULL
)
5438 return process_stop_reply (stop_reply
, status
);
5440 if (rs
->cached_wait_status
)
5441 /* Use the cached wait status, but only once. */
5442 rs
->cached_wait_status
= 0;
5447 if (!target_is_async_p ())
5449 ofunc
= signal (SIGINT
, remote_interrupt
);
5450 /* If the user hit C-c before this packet, or between packets,
5451 pretend that it was hit right here. */
5455 remote_interrupt (SIGINT
);
5459 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5460 _never_ wait for ever -> test on target_is_async_p().
5461 However, before we do that we need to ensure that the caller
5462 knows how to take the target into/out of async mode. */
5463 ret
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
5464 if (!target_is_async_p ())
5465 signal (SIGINT
, ofunc
);
5470 remote_stopped_by_watchpoint_p
= 0;
5472 /* We got something. */
5473 rs
->waiting_for_stop_reply
= 0;
5475 /* Assume that the target has acknowledged Ctrl-C unless we receive
5476 an 'F' or 'O' packet. */
5477 if (buf
[0] != 'F' && buf
[0] != 'O')
5478 rs
->ctrlc_pending_p
= 0;
5482 case 'E': /* Error of some sort. */
5483 /* We're out of sync with the target now. Did it continue or
5484 not? Not is more likely, so report a stop. */
5485 warning (_("Remote failure reply: %s"), buf
);
5486 status
->kind
= TARGET_WAITKIND_STOPPED
;
5487 status
->value
.sig
= TARGET_SIGNAL_0
;
5489 case 'F': /* File-I/O request. */
5490 remote_fileio_request (buf
, rs
->ctrlc_pending_p
);
5491 rs
->ctrlc_pending_p
= 0;
5493 case 'T': case 'S': case 'X': case 'W':
5495 struct stop_reply
*stop_reply
;
5496 struct cleanup
*old_chain
;
5498 stop_reply
= stop_reply_xmalloc ();
5499 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
5500 remote_parse_stop_reply (buf
, stop_reply
);
5501 discard_cleanups (old_chain
);
5502 event_ptid
= process_stop_reply (stop_reply
, status
);
5505 case 'O': /* Console output. */
5506 remote_console_output (buf
+ 1);
5508 /* The target didn't really stop; keep waiting. */
5509 rs
->waiting_for_stop_reply
= 1;
5513 if (last_sent_signal
!= TARGET_SIGNAL_0
)
5515 /* Zero length reply means that we tried 'S' or 'C' and the
5516 remote system doesn't support it. */
5517 target_terminal_ours_for_output ();
5519 ("Can't send signals to this remote system. %s not sent.\n",
5520 target_signal_to_name (last_sent_signal
));
5521 last_sent_signal
= TARGET_SIGNAL_0
;
5522 target_terminal_inferior ();
5524 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
5525 putpkt ((char *) buf
);
5527 /* We just told the target to resume, so a stop reply is in
5529 rs
->waiting_for_stop_reply
= 1;
5532 /* else fallthrough */
5534 warning (_("Invalid remote reply: %s"), buf
);
5536 rs
->waiting_for_stop_reply
= 1;
5540 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
5542 /* Nothing interesting happened. If we're doing a non-blocking
5543 poll, we're done. Otherwise, go back to waiting. */
5544 if (options
& TARGET_WNOHANG
)
5545 return minus_one_ptid
;
5549 else if (status
->kind
!= TARGET_WAITKIND_EXITED
5550 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5552 if (!ptid_equal (event_ptid
, null_ptid
))
5553 record_currthread (event_ptid
);
5555 event_ptid
= inferior_ptid
;
5558 /* A process exit. Invalidate our notion of current thread. */
5559 record_currthread (minus_one_ptid
);
5564 /* Wait until the remote machine stops, then return, storing status in
5565 STATUS just as `wait' would. */
5568 remote_wait (struct target_ops
*ops
,
5569 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5574 event_ptid
= remote_wait_ns (ptid
, status
, options
);
5576 event_ptid
= remote_wait_as (ptid
, status
, options
);
5578 if (target_can_async_p ())
5580 /* If there are are events left in the queue tell the event loop
5582 if (stop_reply_queue
)
5583 mark_async_event_handler (remote_async_inferior_event_token
);
5589 /* Fetch a single register using a 'p' packet. */
5592 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
5594 struct remote_state
*rs
= get_remote_state ();
5596 char regp
[MAX_REGISTER_SIZE
];
5599 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
5602 if (reg
->pnum
== -1)
5607 p
+= hexnumstr (p
, reg
->pnum
);
5610 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5614 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
5618 case PACKET_UNKNOWN
:
5621 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
5622 gdbarch_register_name (get_regcache_arch (regcache
),
5627 /* If this register is unfetchable, tell the regcache. */
5630 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
5634 /* Otherwise, parse and supply the value. */
5640 error (_("fetch_register_using_p: early buf termination"));
5642 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
5645 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
5649 /* Fetch the registers included in the target's 'g' packet. */
5652 send_g_packet (void)
5654 struct remote_state
*rs
= get_remote_state ();
5657 sprintf (rs
->buf
, "g");
5658 remote_send (&rs
->buf
, &rs
->buf_size
);
5660 /* We can get out of synch in various cases. If the first character
5661 in the buffer is not a hex character, assume that has happened
5662 and try to fetch another packet to read. */
5663 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
5664 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
5665 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
5666 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
5669 fprintf_unfiltered (gdb_stdlog
,
5670 "Bad register packet; fetching a new packet\n");
5671 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5674 buf_len
= strlen (rs
->buf
);
5676 /* Sanity check the received packet. */
5677 if (buf_len
% 2 != 0)
5678 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
5684 process_g_packet (struct regcache
*regcache
)
5686 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5687 struct remote_state
*rs
= get_remote_state ();
5688 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5693 buf_len
= strlen (rs
->buf
);
5695 /* Further sanity checks, with knowledge of the architecture. */
5696 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
5697 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
5699 /* Save the size of the packet sent to us by the target. It is used
5700 as a heuristic when determining the max size of packets that the
5701 target can safely receive. */
5702 if (rsa
->actual_register_packet_size
== 0)
5703 rsa
->actual_register_packet_size
= buf_len
;
5705 /* If this is smaller than we guessed the 'g' packet would be,
5706 update our records. A 'g' reply that doesn't include a register's
5707 value implies either that the register is not available, or that
5708 the 'p' packet must be used. */
5709 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
5711 rsa
->sizeof_g_packet
= buf_len
/ 2;
5713 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
5715 if (rsa
->regs
[i
].pnum
== -1)
5718 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
5719 rsa
->regs
[i
].in_g_packet
= 0;
5721 rsa
->regs
[i
].in_g_packet
= 1;
5725 regs
= alloca (rsa
->sizeof_g_packet
);
5727 /* Unimplemented registers read as all bits zero. */
5728 memset (regs
, 0, rsa
->sizeof_g_packet
);
5730 /* Reply describes registers byte by byte, each byte encoded as two
5731 hex characters. Suck them all up, then supply them to the
5732 register cacheing/storage mechanism. */
5735 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
5737 if (p
[0] == 0 || p
[1] == 0)
5738 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
5739 internal_error (__FILE__
, __LINE__
,
5740 "unexpected end of 'g' packet reply");
5742 if (p
[0] == 'x' && p
[1] == 'x')
5743 regs
[i
] = 0; /* 'x' */
5745 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
5749 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
5751 struct packet_reg
*r
= &rsa
->regs
[i
];
5755 if (r
->offset
* 2 >= strlen (rs
->buf
))
5756 /* This shouldn't happen - we adjusted in_g_packet above. */
5757 internal_error (__FILE__
, __LINE__
,
5758 "unexpected end of 'g' packet reply");
5759 else if (rs
->buf
[r
->offset
* 2] == 'x')
5761 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
5762 /* The register isn't available, mark it as such (at
5763 the same time setting the value to zero). */
5764 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
5767 regcache_raw_supply (regcache
, r
->regnum
,
5774 fetch_registers_using_g (struct regcache
*regcache
)
5777 process_g_packet (regcache
);
5781 remote_fetch_registers (struct target_ops
*ops
,
5782 struct regcache
*regcache
, int regnum
)
5784 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5787 set_general_thread (inferior_ptid
);
5791 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5793 gdb_assert (reg
!= NULL
);
5795 /* If this register might be in the 'g' packet, try that first -
5796 we are likely to read more than one register. If this is the
5797 first 'g' packet, we might be overly optimistic about its
5798 contents, so fall back to 'p'. */
5799 if (reg
->in_g_packet
)
5801 fetch_registers_using_g (regcache
);
5802 if (reg
->in_g_packet
)
5806 if (fetch_register_using_p (regcache
, reg
))
5809 /* This register is not available. */
5810 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
5815 fetch_registers_using_g (regcache
);
5817 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5818 if (!rsa
->regs
[i
].in_g_packet
)
5819 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
5821 /* This register is not available. */
5822 regcache_raw_supply (regcache
, i
, NULL
);
5826 /* Prepare to store registers. Since we may send them all (using a
5827 'G' request), we have to read out the ones we don't want to change
5831 remote_prepare_to_store (struct regcache
*regcache
)
5833 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5835 gdb_byte buf
[MAX_REGISTER_SIZE
];
5837 /* Make sure the entire registers array is valid. */
5838 switch (remote_protocol_packets
[PACKET_P
].support
)
5840 case PACKET_DISABLE
:
5841 case PACKET_SUPPORT_UNKNOWN
:
5842 /* Make sure all the necessary registers are cached. */
5843 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5844 if (rsa
->regs
[i
].in_g_packet
)
5845 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
5852 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
5853 packet was not recognized. */
5856 store_register_using_P (const struct regcache
*regcache
,
5857 struct packet_reg
*reg
)
5859 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5860 struct remote_state
*rs
= get_remote_state ();
5861 /* Try storing a single register. */
5862 char *buf
= rs
->buf
;
5863 gdb_byte regp
[MAX_REGISTER_SIZE
];
5866 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
5869 if (reg
->pnum
== -1)
5872 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
5873 p
= buf
+ strlen (buf
);
5874 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
5875 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
5877 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5879 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
5884 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
5885 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
);
5886 case PACKET_UNKNOWN
:
5889 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
5893 /* Store register REGNUM, or all registers if REGNUM == -1, from the
5894 contents of the register cache buffer. FIXME: ignores errors. */
5897 store_registers_using_G (const struct regcache
*regcache
)
5899 struct remote_state
*rs
= get_remote_state ();
5900 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5904 /* Extract all the registers in the regcache copying them into a
5909 regs
= alloca (rsa
->sizeof_g_packet
);
5910 memset (regs
, 0, rsa
->sizeof_g_packet
);
5911 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5913 struct packet_reg
*r
= &rsa
->regs
[i
];
5916 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
5920 /* Command describes registers byte by byte,
5921 each byte encoded as two hex characters. */
5924 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
5926 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
5928 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5929 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
5930 error (_("Could not write registers; remote failure reply '%s'"),
5934 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
5935 of the register cache buffer. FIXME: ignores errors. */
5938 remote_store_registers (struct target_ops
*ops
,
5939 struct regcache
*regcache
, int regnum
)
5941 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5944 set_general_thread (inferior_ptid
);
5948 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5950 gdb_assert (reg
!= NULL
);
5952 /* Always prefer to store registers using the 'P' packet if
5953 possible; we often change only a small number of registers.
5954 Sometimes we change a larger number; we'd need help from a
5955 higher layer to know to use 'G'. */
5956 if (store_register_using_P (regcache
, reg
))
5959 /* For now, don't complain if we have no way to write the
5960 register. GDB loses track of unavailable registers too
5961 easily. Some day, this may be an error. We don't have
5962 any way to read the register, either... */
5963 if (!reg
->in_g_packet
)
5966 store_registers_using_G (regcache
);
5970 store_registers_using_G (regcache
);
5972 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5973 if (!rsa
->regs
[i
].in_g_packet
)
5974 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
5975 /* See above for why we do not issue an error here. */
5980 /* Return the number of hex digits in num. */
5983 hexnumlen (ULONGEST num
)
5987 for (i
= 0; num
!= 0; i
++)
5993 /* Set BUF to the minimum number of hex digits representing NUM. */
5996 hexnumstr (char *buf
, ULONGEST num
)
5998 int len
= hexnumlen (num
);
6000 return hexnumnstr (buf
, num
, len
);
6004 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6007 hexnumnstr (char *buf
, ULONGEST num
, int width
)
6013 for (i
= width
- 1; i
>= 0; i
--)
6015 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
6022 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6025 remote_address_masked (CORE_ADDR addr
)
6027 int address_size
= remote_address_size
;
6029 /* If "remoteaddresssize" was not set, default to target address size. */
6031 address_size
= gdbarch_addr_bit (target_gdbarch
);
6033 if (address_size
> 0
6034 && address_size
< (sizeof (ULONGEST
) * 8))
6036 /* Only create a mask when that mask can safely be constructed
6037 in a ULONGEST variable. */
6040 mask
= (mask
<< address_size
) - 1;
6046 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
6047 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
6048 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
6049 (which may be more than *OUT_LEN due to escape characters). The
6050 total number of bytes in the output buffer will be at most
6054 remote_escape_output (const gdb_byte
*buffer
, int len
,
6055 gdb_byte
*out_buf
, int *out_len
,
6058 int input_index
, output_index
;
6061 for (input_index
= 0; input_index
< len
; input_index
++)
6063 gdb_byte b
= buffer
[input_index
];
6065 if (b
== '$' || b
== '#' || b
== '}')
6067 /* These must be escaped. */
6068 if (output_index
+ 2 > out_maxlen
)
6070 out_buf
[output_index
++] = '}';
6071 out_buf
[output_index
++] = b
^ 0x20;
6075 if (output_index
+ 1 > out_maxlen
)
6077 out_buf
[output_index
++] = b
;
6081 *out_len
= input_index
;
6082 return output_index
;
6085 /* Convert BUFFER, escaped data LEN bytes long, into binary data
6086 in OUT_BUF. Return the number of bytes written to OUT_BUF.
6087 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
6089 This function reverses remote_escape_output. It allows more
6090 escaped characters than that function does, in particular because
6091 '*' must be escaped to avoid the run-length encoding processing
6092 in reading packets. */
6095 remote_unescape_input (const gdb_byte
*buffer
, int len
,
6096 gdb_byte
*out_buf
, int out_maxlen
)
6098 int input_index
, output_index
;
6103 for (input_index
= 0; input_index
< len
; input_index
++)
6105 gdb_byte b
= buffer
[input_index
];
6107 if (output_index
+ 1 > out_maxlen
)
6109 warning (_("Received too much data from remote target;"
6110 " ignoring overflow."));
6111 return output_index
;
6116 out_buf
[output_index
++] = b
^ 0x20;
6122 out_buf
[output_index
++] = b
;
6126 error (_("Unmatched escape character in target response."));
6128 return output_index
;
6131 /* Determine whether the remote target supports binary downloading.
6132 This is accomplished by sending a no-op memory write of zero length
6133 to the target at the specified address. It does not suffice to send
6134 the whole packet, since many stubs strip the eighth bit and
6135 subsequently compute a wrong checksum, which causes real havoc with
6138 NOTE: This can still lose if the serial line is not eight-bit
6139 clean. In cases like this, the user should clear "remote
6143 check_binary_download (CORE_ADDR addr
)
6145 struct remote_state
*rs
= get_remote_state ();
6147 switch (remote_protocol_packets
[PACKET_X
].support
)
6149 case PACKET_DISABLE
:
6153 case PACKET_SUPPORT_UNKNOWN
:
6159 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6161 p
+= hexnumstr (p
, (ULONGEST
) 0);
6165 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6166 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6168 if (rs
->buf
[0] == '\0')
6171 fprintf_unfiltered (gdb_stdlog
,
6172 "binary downloading NOT "
6173 "supported by target\n");
6174 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
6179 fprintf_unfiltered (gdb_stdlog
,
6180 "binary downloading suppported by target\n");
6181 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
6188 /* Write memory data directly to the remote machine.
6189 This does not inform the data cache; the data cache uses this.
6190 HEADER is the starting part of the packet.
6191 MEMADDR is the address in the remote memory space.
6192 MYADDR is the address of the buffer in our space.
6193 LEN is the number of bytes.
6194 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6195 should send data as binary ('X'), or hex-encoded ('M').
6197 The function creates packet of the form
6198 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6200 where encoding of <DATA> is termined by PACKET_FORMAT.
6202 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6205 Returns the number of bytes transferred, or 0 (setting errno) for
6206 error. Only transfer a single packet. */
6209 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
6210 const gdb_byte
*myaddr
, int len
,
6211 char packet_format
, int use_length
)
6213 struct remote_state
*rs
= get_remote_state ();
6223 if (packet_format
!= 'X' && packet_format
!= 'M')
6224 internal_error (__FILE__
, __LINE__
,
6225 "remote_write_bytes_aux: bad packet format");
6230 payload_size
= get_memory_write_packet_size ();
6232 /* The packet buffer will be large enough for the payload;
6233 get_memory_packet_size ensures this. */
6236 /* Compute the size of the actual payload by subtracting out the
6237 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6239 payload_size
-= strlen ("$,:#NN");
6241 /* The comma won't be used. */
6243 header_length
= strlen (header
);
6244 payload_size
-= header_length
;
6245 payload_size
-= hexnumlen (memaddr
);
6247 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6249 strcat (rs
->buf
, header
);
6250 p
= rs
->buf
+ strlen (header
);
6252 /* Compute a best guess of the number of bytes actually transfered. */
6253 if (packet_format
== 'X')
6255 /* Best guess at number of bytes that will fit. */
6256 todo
= min (len
, payload_size
);
6258 payload_size
-= hexnumlen (todo
);
6259 todo
= min (todo
, payload_size
);
6263 /* Num bytes that will fit. */
6264 todo
= min (len
, payload_size
/ 2);
6266 payload_size
-= hexnumlen (todo
);
6267 todo
= min (todo
, payload_size
/ 2);
6271 internal_error (__FILE__
, __LINE__
,
6272 _("minumum packet size too small to write data"));
6274 /* If we already need another packet, then try to align the end
6275 of this packet to a useful boundary. */
6276 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
6277 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
6279 /* Append "<memaddr>". */
6280 memaddr
= remote_address_masked (memaddr
);
6281 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6288 /* Append <len>. Retain the location/size of <len>. It may need to
6289 be adjusted once the packet body has been created. */
6291 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
6299 /* Append the packet body. */
6300 if (packet_format
== 'X')
6302 /* Binary mode. Send target system values byte by byte, in
6303 increasing byte addresses. Only escape certain critical
6305 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
6308 /* If not all TODO bytes fit, then we'll need another packet. Make
6309 a second try to keep the end of the packet aligned. Don't do
6310 this if the packet is tiny. */
6311 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
6315 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
6317 if (new_nr_bytes
!= nr_bytes
)
6318 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
6323 p
+= payload_length
;
6324 if (use_length
&& nr_bytes
< todo
)
6326 /* Escape chars have filled up the buffer prematurely,
6327 and we have actually sent fewer bytes than planned.
6328 Fix-up the length field of the packet. Use the same
6329 number of characters as before. */
6330 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
6331 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
6336 /* Normal mode: Send target system values byte by byte, in
6337 increasing byte addresses. Each byte is encoded as a two hex
6339 nr_bytes
= bin2hex (myaddr
, p
, todo
);
6343 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6344 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6346 if (rs
->buf
[0] == 'E')
6348 /* There is no correspondance between what the remote protocol
6349 uses for errors and errno codes. We would like a cleaner way
6350 of representing errors (big enough to include errno codes,
6351 bfd_error codes, and others). But for now just return EIO. */
6356 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6357 fewer bytes than we'd planned. */
6361 /* Write memory data directly to the remote machine.
6362 This does not inform the data cache; the data cache uses this.
6363 MEMADDR is the address in the remote memory space.
6364 MYADDR is the address of the buffer in our space.
6365 LEN is the number of bytes.
6367 Returns number of bytes transferred, or 0 (setting errno) for
6368 error. Only transfer a single packet. */
6371 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
6373 char *packet_format
= 0;
6375 /* Check whether the target supports binary download. */
6376 check_binary_download (memaddr
);
6378 switch (remote_protocol_packets
[PACKET_X
].support
)
6381 packet_format
= "X";
6383 case PACKET_DISABLE
:
6384 packet_format
= "M";
6386 case PACKET_SUPPORT_UNKNOWN
:
6387 internal_error (__FILE__
, __LINE__
,
6388 _("remote_write_bytes: bad internal state"));
6390 internal_error (__FILE__
, __LINE__
, _("bad switch"));
6393 return remote_write_bytes_aux (packet_format
,
6394 memaddr
, myaddr
, len
, packet_format
[0], 1);
6397 /* Read memory data directly from the remote machine.
6398 This does not use the data cache; the data cache uses this.
6399 MEMADDR is the address in the remote memory space.
6400 MYADDR is the address of the buffer in our space.
6401 LEN is the number of bytes.
6403 Returns number of bytes transferred, or 0 for error. */
6405 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
6406 remote targets) shouldn't attempt to read the entire buffer.
6407 Instead it should read a single packet worth of data and then
6408 return the byte size of that packet to the caller. The caller (its
6409 caller and its callers caller ;-) already contains code for
6410 handling partial reads. */
6413 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
6415 struct remote_state
*rs
= get_remote_state ();
6416 int max_buf_size
; /* Max size of packet output buffer. */
6422 max_buf_size
= get_memory_read_packet_size ();
6423 /* The packet buffer will be large enough for the payload;
6424 get_memory_packet_size ensures this. */
6433 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit. */
6435 /* construct "m"<memaddr>","<len>" */
6436 /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */
6437 memaddr
= remote_address_masked (memaddr
);
6440 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6442 p
+= hexnumstr (p
, (ULONGEST
) todo
);
6446 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6448 if (rs
->buf
[0] == 'E'
6449 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
6450 && rs
->buf
[3] == '\0')
6452 /* There is no correspondance between what the remote
6453 protocol uses for errors and errno codes. We would like
6454 a cleaner way of representing errors (big enough to
6455 include errno codes, bfd_error codes, and others). But
6456 for now just return EIO. */
6461 /* Reply describes memory byte by byte,
6462 each byte encoded as two hex characters. */
6465 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
6467 /* Reply is short. This means that we were able to read
6468 only part of what we wanted to. */
6469 return i
+ (origlen
- len
);
6479 /* Remote notification handler. */
6482 handle_notification (char *buf
, size_t length
)
6484 if (strncmp (buf
, "Stop:", 5) == 0)
6486 if (pending_stop_reply
)
6488 /* We've already parsed the in-flight stop-reply, but the
6489 stub for some reason thought we didn't, possibly due to
6490 timeout on its side. Just ignore it. */
6492 fprintf_unfiltered (gdb_stdlog
, "ignoring resent notification\n");
6496 struct cleanup
*old_chain
;
6497 struct stop_reply
*reply
= stop_reply_xmalloc ();
6499 old_chain
= make_cleanup (do_stop_reply_xfree
, reply
);
6501 remote_parse_stop_reply (buf
+ 5, reply
);
6503 discard_cleanups (old_chain
);
6505 /* Be careful to only set it after parsing, since an error
6506 may be thrown then. */
6507 pending_stop_reply
= reply
;
6509 /* Notify the event loop there's a stop reply to acknowledge
6510 and that there may be more events to fetch. */
6511 mark_async_event_handler (remote_async_get_pending_events_token
);
6514 fprintf_unfiltered (gdb_stdlog
, "stop notification captured\n");
6518 /* We ignore notifications we don't recognize, for compatibility
6519 with newer stubs. */
6524 /* Read or write LEN bytes from inferior memory at MEMADDR,
6525 transferring to or from debugger address BUFFER. Write to inferior
6526 if SHOULD_WRITE is nonzero. Returns length of data written or
6527 read; 0 for error. TARGET is unused. */
6530 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
6531 int should_write
, struct mem_attrib
*attrib
,
6532 struct target_ops
*target
)
6536 set_general_thread (inferior_ptid
);
6539 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
6541 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
6546 /* Sends a packet with content determined by the printf format string
6547 FORMAT and the remaining arguments, then gets the reply. Returns
6548 whether the packet was a success, a failure, or unknown. */
6550 static enum packet_result
6551 remote_send_printf (const char *format
, ...)
6553 struct remote_state
*rs
= get_remote_state ();
6554 int max_size
= get_remote_packet_size ();
6557 va_start (ap
, format
);
6560 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
6561 internal_error (__FILE__
, __LINE__
, "Too long remote packet.");
6563 if (putpkt (rs
->buf
) < 0)
6564 error (_("Communication problem with target."));
6567 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6569 return packet_check_result (rs
->buf
);
6573 restore_remote_timeout (void *p
)
6575 int value
= *(int *)p
;
6577 remote_timeout
= value
;
6580 /* Flash writing can take quite some time. We'll set
6581 effectively infinite timeout for flash operations.
6582 In future, we'll need to decide on a better approach. */
6583 static const int remote_flash_timeout
= 1000;
6586 remote_flash_erase (struct target_ops
*ops
,
6587 ULONGEST address
, LONGEST length
)
6589 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
6590 int saved_remote_timeout
= remote_timeout
;
6591 enum packet_result ret
;
6592 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6593 &saved_remote_timeout
);
6595 remote_timeout
= remote_flash_timeout
;
6597 ret
= remote_send_printf ("vFlashErase:%s,%s",
6598 phex (address
, addr_size
),
6602 case PACKET_UNKNOWN
:
6603 error (_("Remote target does not support flash erase"));
6605 error (_("Error erasing flash with vFlashErase packet"));
6610 do_cleanups (back_to
);
6614 remote_flash_write (struct target_ops
*ops
,
6615 ULONGEST address
, LONGEST length
,
6616 const gdb_byte
*data
)
6618 int saved_remote_timeout
= remote_timeout
;
6620 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6621 &saved_remote_timeout
);
6623 remote_timeout
= remote_flash_timeout
;
6624 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
6625 do_cleanups (back_to
);
6631 remote_flash_done (struct target_ops
*ops
)
6633 int saved_remote_timeout
= remote_timeout
;
6635 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6636 &saved_remote_timeout
);
6638 remote_timeout
= remote_flash_timeout
;
6639 ret
= remote_send_printf ("vFlashDone");
6640 do_cleanups (back_to
);
6644 case PACKET_UNKNOWN
:
6645 error (_("Remote target does not support vFlashDone"));
6647 error (_("Error finishing flash operation"));
6654 remote_files_info (struct target_ops
*ignore
)
6656 puts_filtered ("Debugging a target over a serial line.\n");
6659 /* Stuff for dealing with the packets which are part of this protocol.
6660 See comment at top of file for details. */
6662 /* Read a single character from the remote end. */
6665 readchar (int timeout
)
6669 ch
= serial_readchar (remote_desc
, timeout
);
6674 switch ((enum serial_rc
) ch
)
6678 error (_("Remote connection closed"));
6682 perror_with_name (_("Remote communication error. "
6683 "Target disconnected."));
6685 case SERIAL_TIMEOUT
:
6691 /* Send the command in *BUF to the remote machine, and read the reply
6692 into *BUF. Report an error if we get an error reply. Resize
6693 *BUF using xrealloc if necessary to hold the result, and update
6697 remote_send (char **buf
,
6701 getpkt (buf
, sizeof_buf
, 0);
6703 if ((*buf
)[0] == 'E')
6704 error (_("Remote failure reply: %s"), *buf
);
6707 /* Return a pointer to an xmalloc'ed string representing an escaped
6708 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
6709 etc. The caller is responsible for releasing the returned
6713 escape_buffer (const char *buf
, int n
)
6715 struct cleanup
*old_chain
;
6716 struct ui_file
*stb
;
6719 stb
= mem_fileopen ();
6720 old_chain
= make_cleanup_ui_file_delete (stb
);
6722 fputstrn_unfiltered (buf
, n
, 0, stb
);
6723 str
= ui_file_xstrdup (stb
, NULL
);
6724 do_cleanups (old_chain
);
6728 /* Display a null-terminated packet on stdout, for debugging, using C
6732 print_packet (char *buf
)
6734 puts_filtered ("\"");
6735 fputstr_filtered (buf
, '"', gdb_stdout
);
6736 puts_filtered ("\"");
6742 return putpkt_binary (buf
, strlen (buf
));
6745 /* Send a packet to the remote machine, with error checking. The data
6746 of the packet is in BUF. The string in BUF can be at most
6747 get_remote_packet_size () - 5 to account for the $, # and checksum,
6748 and for a possible /0 if we are debugging (remote_debug) and want
6749 to print the sent packet as a string. */
6752 putpkt_binary (char *buf
, int cnt
)
6754 struct remote_state
*rs
= get_remote_state ();
6756 unsigned char csum
= 0;
6757 char *buf2
= alloca (cnt
+ 6);
6763 /* Catch cases like trying to read memory or listing threads while
6764 we're waiting for a stop reply. The remote server wouldn't be
6765 ready to handle this request, so we'd hang and timeout. We don't
6766 have to worry about this in synchronous mode, because in that
6767 case it's not possible to issue a command while the target is
6768 running. This is not a problem in non-stop mode, because in that
6769 case, the stub is always ready to process serial input. */
6770 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
6771 error (_("Cannot execute this command while the target is running."));
6773 /* We're sending out a new packet. Make sure we don't look at a
6774 stale cached response. */
6775 rs
->cached_wait_status
= 0;
6777 /* Copy the packet into buffer BUF2, encapsulating it
6778 and giving it a checksum. */
6783 for (i
= 0; i
< cnt
; i
++)
6789 *p
++ = tohex ((csum
>> 4) & 0xf);
6790 *p
++ = tohex (csum
& 0xf);
6792 /* Send it over and over until we get a positive ack. */
6796 int started_error_output
= 0;
6800 struct cleanup
*old_chain
;
6804 str
= escape_buffer (buf2
, p
- buf2
);
6805 old_chain
= make_cleanup (xfree
, str
);
6806 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", str
);
6807 gdb_flush (gdb_stdlog
);
6808 do_cleanups (old_chain
);
6810 if (serial_write (remote_desc
, buf2
, p
- buf2
))
6811 perror_with_name (_("putpkt: write failed"));
6813 /* If this is a no acks version of the remote protocol, send the
6814 packet and move on. */
6818 /* Read until either a timeout occurs (-2) or '+' is read.
6819 Handle any notification that arrives in the mean time. */
6822 ch
= readchar (remote_timeout
);
6830 case SERIAL_TIMEOUT
:
6833 if (started_error_output
)
6835 putchar_unfiltered ('\n');
6836 started_error_output
= 0;
6845 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
6849 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
6850 case SERIAL_TIMEOUT
:
6854 break; /* Retransmit buffer. */
6858 fprintf_unfiltered (gdb_stdlog
,
6859 "Packet instead of Ack, ignoring it\n");
6860 /* It's probably an old response sent because an ACK
6861 was lost. Gobble up the packet and ack it so it
6862 doesn't get retransmitted when we resend this
6865 serial_write (remote_desc
, "+", 1);
6866 continue; /* Now, go look for +. */
6873 /* If we got a notification, handle it, and go back to looking
6875 /* We've found the start of a notification. Now
6876 collect the data. */
6877 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
6882 struct cleanup
*old_chain
;
6885 str
= escape_buffer (rs
->buf
, val
);
6886 old_chain
= make_cleanup (xfree
, str
);
6887 fprintf_unfiltered (gdb_stdlog
,
6888 " Notification received: %s\n",
6890 do_cleanups (old_chain
);
6892 handle_notification (rs
->buf
, val
);
6893 /* We're in sync now, rewait for the ack. */
6900 if (!started_error_output
)
6902 started_error_output
= 1;
6903 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6905 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6906 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
6915 if (!started_error_output
)
6917 started_error_output
= 1;
6918 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6920 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6924 break; /* Here to retransmit. */
6928 /* This is wrong. If doing a long backtrace, the user should be
6929 able to get out next time we call QUIT, without anything as
6930 violent as interrupt_query. If we want to provide a way out of
6931 here without getting to the next QUIT, it should be based on
6932 hitting ^C twice as in remote_wait. */
6943 /* Come here after finding the start of a frame when we expected an
6944 ack. Do our best to discard the rest of this packet. */
6953 c
= readchar (remote_timeout
);
6956 case SERIAL_TIMEOUT
:
6957 /* Nothing we can do. */
6960 /* Discard the two bytes of checksum and stop. */
6961 c
= readchar (remote_timeout
);
6963 c
= readchar (remote_timeout
);
6966 case '*': /* Run length encoding. */
6967 /* Discard the repeat count. */
6968 c
= readchar (remote_timeout
);
6973 /* A regular character. */
6979 /* Come here after finding the start of the frame. Collect the rest
6980 into *BUF, verifying the checksum, length, and handling run-length
6981 compression. NUL terminate the buffer. If there is not enough room,
6982 expand *BUF using xrealloc.
6984 Returns -1 on error, number of characters in buffer (ignoring the
6985 trailing NULL) on success. (could be extended to return one of the
6986 SERIAL status indications). */
6989 read_frame (char **buf_p
,
6996 struct remote_state
*rs
= get_remote_state ();
7003 c
= readchar (remote_timeout
);
7006 case SERIAL_TIMEOUT
:
7008 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
7012 fputs_filtered ("Saw new packet start in middle of old one\n",
7014 return -1; /* Start a new packet, count retries. */
7017 unsigned char pktcsum
;
7023 check_0
= readchar (remote_timeout
);
7025 check_1
= readchar (remote_timeout
);
7027 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
7030 fputs_filtered ("Timeout in checksum, retrying\n",
7034 else if (check_0
< 0 || check_1
< 0)
7037 fputs_filtered ("Communication error in checksum\n",
7042 /* Don't recompute the checksum; with no ack packets we
7043 don't have any way to indicate a packet retransmission
7048 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
7049 if (csum
== pktcsum
)
7054 struct cleanup
*old_chain
;
7057 str
= escape_buffer (buf
, bc
);
7058 old_chain
= make_cleanup (xfree
, str
);
7059 fprintf_unfiltered (gdb_stdlog
,
7060 "Bad checksum, sentsum=0x%x, "
7061 "csum=0x%x, buf=%s\n",
7062 pktcsum
, csum
, str
);
7063 do_cleanups (old_chain
);
7065 /* Number of characters in buffer ignoring trailing
7069 case '*': /* Run length encoding. */
7074 c
= readchar (remote_timeout
);
7076 repeat
= c
- ' ' + 3; /* Compute repeat count. */
7078 /* The character before ``*'' is repeated. */
7080 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
7082 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
7084 /* Make some more room in the buffer. */
7085 *sizeof_buf
+= repeat
;
7086 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7090 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
7096 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
7100 if (bc
>= *sizeof_buf
- 1)
7102 /* Make some more room in the buffer. */
7104 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7115 /* Read a packet from the remote machine, with error checking, and
7116 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7117 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7118 rather than timing out; this is used (in synchronous mode) to wait
7119 for a target that is is executing user code to stop. */
7120 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7121 don't have to change all the calls to getpkt to deal with the
7122 return value, because at the moment I don't know what the right
7123 thing to do it for those. */
7131 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
7135 /* Read a packet from the remote machine, with error checking, and
7136 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7137 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7138 rather than timing out; this is used (in synchronous mode) to wait
7139 for a target that is is executing user code to stop. If FOREVER ==
7140 0, this function is allowed to time out gracefully and return an
7141 indication of this to the caller. Otherwise return the number of
7142 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7143 enough reason to return to the caller. */
7146 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
7147 int expecting_notif
)
7149 struct remote_state
*rs
= get_remote_state ();
7155 /* We're reading a new response. Make sure we don't look at a
7156 previously cached response. */
7157 rs
->cached_wait_status
= 0;
7159 strcpy (*buf
, "timeout");
7162 timeout
= watchdog
> 0 ? watchdog
: -1;
7163 else if (expecting_notif
)
7164 timeout
= 0; /* There should already be a char in the buffer. If
7167 timeout
= remote_timeout
;
7171 /* Process any number of notifications, and then return when
7175 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
7177 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
7179 /* This can loop forever if the remote side sends us
7180 characters continuously, but if it pauses, we'll get
7181 SERIAL_TIMEOUT from readchar because of timeout. Then
7182 we'll count that as a retry.
7184 Note that even when forever is set, we will only wait
7185 forever prior to the start of a packet. After that, we
7186 expect characters to arrive at a brisk pace. They should
7187 show up within remote_timeout intervals. */
7189 c
= readchar (timeout
);
7190 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
7192 if (c
== SERIAL_TIMEOUT
)
7194 if (expecting_notif
)
7195 return -1; /* Don't complain, it's normal to not get
7196 anything in this case. */
7198 if (forever
) /* Watchdog went off? Kill the target. */
7202 error (_("Watchdog timeout has expired. Target detached."));
7205 fputs_filtered ("Timed out.\n", gdb_stdlog
);
7209 /* We've found the start of a packet or notification.
7210 Now collect the data. */
7211 val
= read_frame (buf
, sizeof_buf
);
7216 serial_write (remote_desc
, "-", 1);
7219 if (tries
> MAX_TRIES
)
7221 /* We have tried hard enough, and just can't receive the
7222 packet/notification. Give up. */
7223 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7225 /* Skip the ack char if we're in no-ack mode. */
7226 if (!rs
->noack_mode
)
7227 serial_write (remote_desc
, "+", 1);
7231 /* If we got an ordinary packet, return that to our caller. */
7236 struct cleanup
*old_chain
;
7239 str
= escape_buffer (*buf
, val
);
7240 old_chain
= make_cleanup (xfree
, str
);
7241 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", str
);
7242 do_cleanups (old_chain
);
7245 /* Skip the ack char if we're in no-ack mode. */
7246 if (!rs
->noack_mode
)
7247 serial_write (remote_desc
, "+", 1);
7251 /* If we got a notification, handle it, and go back to looking
7255 gdb_assert (c
== '%');
7259 struct cleanup
*old_chain
;
7262 str
= escape_buffer (*buf
, val
);
7263 old_chain
= make_cleanup (xfree
, str
);
7264 fprintf_unfiltered (gdb_stdlog
,
7265 " Notification received: %s\n",
7267 do_cleanups (old_chain
);
7270 handle_notification (*buf
, val
);
7272 /* Notifications require no acknowledgement. */
7274 if (expecting_notif
)
7281 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
7283 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0);
7287 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
)
7289 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1);
7294 remote_kill (struct target_ops
*ops
)
7296 /* Use catch_errors so the user can quit from gdb even when we
7297 aren't on speaking terms with the remote system. */
7298 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
7300 /* Don't wait for it to die. I'm not really sure it matters whether
7301 we do or not. For the existing stubs, kill is a noop. */
7302 target_mourn_inferior ();
7306 remote_vkill (int pid
, struct remote_state
*rs
)
7308 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7311 /* Tell the remote target to detach. */
7312 sprintf (rs
->buf
, "vKill;%x", pid
);
7314 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7316 if (packet_ok (rs
->buf
,
7317 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
7319 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7326 extended_remote_kill (struct target_ops
*ops
)
7329 int pid
= ptid_get_pid (inferior_ptid
);
7330 struct remote_state
*rs
= get_remote_state ();
7332 res
= remote_vkill (pid
, rs
);
7333 if (res
== -1 && !remote_multi_process_p (rs
))
7335 /* Don't try 'k' on a multi-process aware stub -- it has no way
7336 to specify the pid. */
7340 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7341 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
7344 /* Don't wait for it to die. I'm not really sure it matters whether
7345 we do or not. For the existing stubs, kill is a noop. */
7351 error (_("Can't kill process"));
7353 target_mourn_inferior ();
7357 remote_mourn (struct target_ops
*ops
)
7359 remote_mourn_1 (ops
);
7362 /* Worker function for remote_mourn. */
7364 remote_mourn_1 (struct target_ops
*target
)
7366 unpush_target (target
);
7368 /* remote_close takes care of doing most of the clean up. */
7369 generic_mourn_inferior ();
7373 extended_remote_mourn_1 (struct target_ops
*target
)
7375 struct remote_state
*rs
= get_remote_state ();
7377 /* In case we got here due to an error, but we're going to stay
7379 rs
->waiting_for_stop_reply
= 0;
7381 /* We're no longer interested in these events. */
7382 discard_pending_stop_replies (ptid_get_pid (inferior_ptid
));
7384 /* If the current general thread belonged to the process we just
7385 detached from or has exited, the remote side current general
7386 thread becomes undefined. Considering a case like this:
7388 - We just got here due to a detach.
7389 - The process that we're detaching from happens to immediately
7390 report a global breakpoint being hit in non-stop mode, in the
7391 same thread we had selected before.
7392 - GDB attaches to this process again.
7393 - This event happens to be the next event we handle.
7395 GDB would consider that the current general thread didn't need to
7396 be set on the stub side (with Hg), since for all it knew,
7397 GENERAL_THREAD hadn't changed.
7399 Notice that although in all-stop mode, the remote server always
7400 sets the current thread to the thread reporting the stop event,
7401 that doesn't happen in non-stop mode; in non-stop, the stub *must
7402 not* change the current thread when reporting a breakpoint hit,
7403 due to the decoupling of event reporting and event handling.
7405 To keep things simple, we always invalidate our notion of the
7407 record_currthread (minus_one_ptid
);
7409 /* Unlike "target remote", we do not want to unpush the target; then
7410 the next time the user says "run", we won't be connected. */
7412 /* Call common code to mark the inferior as not running. */
7413 generic_mourn_inferior ();
7415 if (!have_inferiors ())
7417 if (!remote_multi_process_p (rs
))
7419 /* Check whether the target is running now - some remote stubs
7420 automatically restart after kill. */
7422 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7424 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
7426 /* Assume that the target has been restarted. Set
7427 inferior_ptid so that bits of core GDB realizes
7428 there's something here, e.g., so that the user can
7429 say "kill" again. */
7430 inferior_ptid
= magic_null_ptid
;
7437 extended_remote_mourn (struct target_ops
*ops
)
7439 extended_remote_mourn_1 (ops
);
7443 extended_remote_run (char *args
)
7445 struct remote_state
*rs
= get_remote_state ();
7448 /* If the user has disabled vRun support, or we have detected that
7449 support is not available, do not try it. */
7450 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7453 strcpy (rs
->buf
, "vRun;");
7454 len
= strlen (rs
->buf
);
7456 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
7457 error (_("Remote file name too long for run packet"));
7458 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
, 0);
7460 gdb_assert (args
!= NULL
);
7463 struct cleanup
*back_to
;
7467 argv
= gdb_buildargv (args
);
7468 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
7469 for (i
= 0; argv
[i
] != NULL
; i
++)
7471 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
7472 error (_("Argument list too long for run packet"));
7473 rs
->buf
[len
++] = ';';
7474 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
, 0);
7476 do_cleanups (back_to
);
7479 rs
->buf
[len
++] = '\0';
7482 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7484 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
7486 /* We have a wait response; we don't need it, though. All is well. */
7489 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7490 /* It wasn't disabled before, but it is now. */
7494 if (remote_exec_file
[0] == '\0')
7495 error (_("Running the default executable on the remote target failed; "
7496 "try \"set remote exec-file\"?"));
7498 error (_("Running \"%s\" on the remote target failed"),
7503 /* In the extended protocol we want to be able to do things like
7504 "run" and have them basically work as expected. So we need
7505 a special create_inferior function. We support changing the
7506 executable file and the command line arguments, but not the
7510 extended_remote_create_inferior_1 (char *exec_file
, char *args
,
7511 char **env
, int from_tty
)
7513 /* If running asynchronously, register the target file descriptor
7514 with the event loop. */
7515 if (target_can_async_p ())
7516 target_async (inferior_event_handler
, 0);
7518 /* Now restart the remote server. */
7519 if (extended_remote_run (args
) == -1)
7521 /* vRun was not supported. Fail if we need it to do what the
7523 if (remote_exec_file
[0])
7524 error (_("Remote target does not support \"set remote exec-file\""));
7526 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7528 /* Fall back to "R". */
7529 extended_remote_restart ();
7532 if (!have_inferiors ())
7534 /* Clean up from the last time we ran, before we mark the target
7535 running again. This will mark breakpoints uninserted, and
7536 get_offsets may insert breakpoints. */
7537 init_thread_list ();
7538 init_wait_for_inferior ();
7541 /* Now mark the inferior as running before we do anything else. */
7542 inferior_ptid
= magic_null_ptid
;
7544 /* Now, if we have thread information, update inferior_ptid. */
7545 inferior_ptid
= remote_current_thread (inferior_ptid
);
7547 remote_add_inferior (ptid_get_pid (inferior_ptid
), 0);
7548 add_thread_silent (inferior_ptid
);
7550 /* Get updated offsets, if the stub uses qOffsets. */
7555 extended_remote_create_inferior (struct target_ops
*ops
,
7556 char *exec_file
, char *args
,
7557 char **env
, int from_tty
)
7559 extended_remote_create_inferior_1 (exec_file
, args
, env
, from_tty
);
7563 /* Insert a breakpoint. On targets that have software breakpoint
7564 support, we ask the remote target to do the work; on targets
7565 which don't, we insert a traditional memory breakpoint. */
7568 remote_insert_breakpoint (struct gdbarch
*gdbarch
,
7569 struct bp_target_info
*bp_tgt
)
7571 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
7572 If it succeeds, then set the support to PACKET_ENABLE. If it
7573 fails, and the user has explicitly requested the Z support then
7574 report an error, otherwise, mark it disabled and go on. */
7576 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
7578 CORE_ADDR addr
= bp_tgt
->placed_address
;
7579 struct remote_state
*rs
;
7583 gdbarch_remote_breakpoint_from_pc (gdbarch
, &addr
, &bpsize
);
7585 rs
= get_remote_state ();
7591 addr
= (ULONGEST
) remote_address_masked (addr
);
7592 p
+= hexnumstr (p
, addr
);
7593 sprintf (p
, ",%d", bpsize
);
7596 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7598 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
7603 bp_tgt
->placed_address
= addr
;
7604 bp_tgt
->placed_size
= bpsize
;
7606 case PACKET_UNKNOWN
:
7611 return memory_insert_breakpoint (gdbarch
, bp_tgt
);
7615 remote_remove_breakpoint (struct gdbarch
*gdbarch
,
7616 struct bp_target_info
*bp_tgt
)
7618 CORE_ADDR addr
= bp_tgt
->placed_address
;
7619 struct remote_state
*rs
= get_remote_state ();
7621 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
7629 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
7630 p
+= hexnumstr (p
, addr
);
7631 sprintf (p
, ",%d", bp_tgt
->placed_size
);
7634 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7636 return (rs
->buf
[0] == 'E');
7639 return memory_remove_breakpoint (gdbarch
, bp_tgt
);
7643 watchpoint_to_Z_packet (int type
)
7648 return Z_PACKET_WRITE_WP
;
7651 return Z_PACKET_READ_WP
;
7654 return Z_PACKET_ACCESS_WP
;
7657 internal_error (__FILE__
, __LINE__
,
7658 _("hw_bp_to_z: bad watchpoint type %d"), type
);
7663 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
,
7664 struct expression
*cond
)
7666 struct remote_state
*rs
= get_remote_state ();
7668 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
7670 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
7673 sprintf (rs
->buf
, "Z%x,", packet
);
7674 p
= strchr (rs
->buf
, '\0');
7675 addr
= remote_address_masked (addr
);
7676 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7677 sprintf (p
, ",%x", len
);
7680 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7682 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
7686 case PACKET_UNKNOWN
:
7691 internal_error (__FILE__
, __LINE__
,
7692 _("remote_insert_watchpoint: reached end of function"));
7697 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
,
7698 struct expression
*cond
)
7700 struct remote_state
*rs
= get_remote_state ();
7702 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
7704 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
7707 sprintf (rs
->buf
, "z%x,", packet
);
7708 p
= strchr (rs
->buf
, '\0');
7709 addr
= remote_address_masked (addr
);
7710 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7711 sprintf (p
, ",%x", len
);
7713 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7715 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
7718 case PACKET_UNKNOWN
:
7723 internal_error (__FILE__
, __LINE__
,
7724 _("remote_remove_watchpoint: reached end of function"));
7728 int remote_hw_watchpoint_limit
= -1;
7729 int remote_hw_breakpoint_limit
= -1;
7732 remote_check_watch_resources (int type
, int cnt
, int ot
)
7734 if (type
== bp_hardware_breakpoint
)
7736 if (remote_hw_breakpoint_limit
== 0)
7738 else if (remote_hw_breakpoint_limit
< 0)
7740 else if (cnt
<= remote_hw_breakpoint_limit
)
7745 if (remote_hw_watchpoint_limit
== 0)
7747 else if (remote_hw_watchpoint_limit
< 0)
7751 else if (cnt
<= remote_hw_watchpoint_limit
)
7758 remote_stopped_by_watchpoint (void)
7760 return remote_stopped_by_watchpoint_p
;
7764 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
7768 if (remote_stopped_by_watchpoint ())
7770 *addr_p
= remote_watch_data_address
;
7779 remote_insert_hw_breakpoint (struct gdbarch
*gdbarch
,
7780 struct bp_target_info
*bp_tgt
)
7783 struct remote_state
*rs
;
7786 /* The length field should be set to the size of a breakpoint
7787 instruction, even though we aren't inserting one ourselves. */
7789 gdbarch_remote_breakpoint_from_pc
7790 (gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
7792 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
7795 rs
= get_remote_state ();
7802 addr
= remote_address_masked (bp_tgt
->placed_address
);
7803 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7804 sprintf (p
, ",%x", bp_tgt
->placed_size
);
7807 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7809 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
7812 case PACKET_UNKNOWN
:
7817 internal_error (__FILE__
, __LINE__
,
7818 _("remote_insert_hw_breakpoint: reached end of function"));
7823 remote_remove_hw_breakpoint (struct gdbarch
*gdbarch
,
7824 struct bp_target_info
*bp_tgt
)
7827 struct remote_state
*rs
= get_remote_state ();
7830 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
7837 addr
= remote_address_masked (bp_tgt
->placed_address
);
7838 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7839 sprintf (p
, ",%x", bp_tgt
->placed_size
);
7842 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7844 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
7847 case PACKET_UNKNOWN
:
7852 internal_error (__FILE__
, __LINE__
,
7853 _("remote_remove_hw_breakpoint: reached end of function"));
7856 /* Table used by the crc32 function to calcuate the checksum. */
7858 static unsigned long crc32_table
[256] =
7861 static unsigned long
7862 crc32 (const unsigned char *buf
, int len
, unsigned int crc
)
7864 if (!crc32_table
[1])
7866 /* Initialize the CRC table and the decoding table. */
7870 for (i
= 0; i
< 256; i
++)
7872 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
7873 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
7880 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
7886 /* Verify memory using the "qCRC:" request. */
7889 remote_verify_memory (struct target_ops
*ops
,
7890 const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
7892 struct remote_state
*rs
= get_remote_state ();
7893 unsigned long host_crc
, target_crc
;
7896 /* FIXME: assumes lma can fit into long. */
7897 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
7898 (long) lma
, (long) size
);
7901 /* Be clever; compute the host_crc before waiting for target
7903 host_crc
= crc32 (data
, size
, 0xffffffff);
7905 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7906 if (rs
->buf
[0] == 'E')
7909 if (rs
->buf
[0] != 'C')
7910 error (_("remote target does not support this operation"));
7912 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
7913 target_crc
= target_crc
* 16 + fromhex (*tmp
);
7915 return (host_crc
== target_crc
);
7918 /* compare-sections command
7920 With no arguments, compares each loadable section in the exec bfd
7921 with the same memory range on the target, and reports mismatches.
7922 Useful for verifying the image on the target against the exec file. */
7925 compare_sections_command (char *args
, int from_tty
)
7928 struct cleanup
*old_chain
;
7930 const char *sectname
;
7938 error (_("command cannot be used without an exec file"));
7940 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
7942 if (!(s
->flags
& SEC_LOAD
))
7943 continue; /* Skip non-loadable section. */
7945 size
= bfd_get_section_size (s
);
7947 continue; /* Skip zero-length section. */
7949 sectname
= bfd_get_section_name (exec_bfd
, s
);
7950 if (args
&& strcmp (args
, sectname
) != 0)
7951 continue; /* Not the section selected by user. */
7953 matched
= 1; /* Do this section. */
7956 sectdata
= xmalloc (size
);
7957 old_chain
= make_cleanup (xfree
, sectdata
);
7958 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
7960 res
= target_verify_memory (sectdata
, lma
, size
);
7963 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
7964 paddress (target_gdbarch
, lma
),
7965 paddress (target_gdbarch
, lma
+ size
));
7967 printf_filtered ("Section %s, range %s -- %s: ", sectname
,
7968 paddress (target_gdbarch
, lma
),
7969 paddress (target_gdbarch
, lma
+ size
));
7971 printf_filtered ("matched.\n");
7974 printf_filtered ("MIS-MATCHED!\n");
7978 do_cleanups (old_chain
);
7981 warning (_("One or more sections of the remote executable does not match\n\
7982 the loaded file\n"));
7983 if (args
&& !matched
)
7984 printf_filtered (_("No loaded section named '%s'.\n"), args
);
7987 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
7988 into remote target. The number of bytes written to the remote
7989 target is returned, or -1 for error. */
7992 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
7993 const char *annex
, const gdb_byte
*writebuf
,
7994 ULONGEST offset
, LONGEST len
,
7995 struct packet_config
*packet
)
7999 struct remote_state
*rs
= get_remote_state ();
8000 int max_size
= get_memory_write_packet_size ();
8002 if (packet
->support
== PACKET_DISABLE
)
8005 /* Insert header. */
8006 i
= snprintf (rs
->buf
, max_size
,
8007 "qXfer:%s:write:%s:%s:",
8008 object_name
, annex
? annex
: "",
8009 phex_nz (offset
, sizeof offset
));
8010 max_size
-= (i
+ 1);
8012 /* Escape as much data as fits into rs->buf. */
8013 buf_len
= remote_escape_output
8014 (writebuf
, len
, (rs
->buf
+ i
), &max_size
, max_size
);
8016 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
8017 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8018 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8021 unpack_varlen_hex (rs
->buf
, &n
);
8025 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8026 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8027 number of bytes read is returned, or 0 for EOF, or -1 for error.
8028 The number of bytes read may be less than LEN without indicating an
8029 EOF. PACKET is checked and updated to indicate whether the remote
8030 target supports this object. */
8033 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
8035 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
8036 struct packet_config
*packet
)
8038 static char *finished_object
;
8039 static char *finished_annex
;
8040 static ULONGEST finished_offset
;
8042 struct remote_state
*rs
= get_remote_state ();
8043 LONGEST i
, n
, packet_len
;
8045 if (packet
->support
== PACKET_DISABLE
)
8048 /* Check whether we've cached an end-of-object packet that matches
8050 if (finished_object
)
8052 if (strcmp (object_name
, finished_object
) == 0
8053 && strcmp (annex
? annex
: "", finished_annex
) == 0
8054 && offset
== finished_offset
)
8057 /* Otherwise, we're now reading something different. Discard
8059 xfree (finished_object
);
8060 xfree (finished_annex
);
8061 finished_object
= NULL
;
8062 finished_annex
= NULL
;
8065 /* Request only enough to fit in a single packet. The actual data
8066 may not, since we don't know how much of it will need to be escaped;
8067 the target is free to respond with slightly less data. We subtract
8068 five to account for the response type and the protocol frame. */
8069 n
= min (get_remote_packet_size () - 5, len
);
8070 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8071 object_name
, annex
? annex
: "",
8072 phex_nz (offset
, sizeof offset
),
8073 phex_nz (n
, sizeof n
));
8074 i
= putpkt (rs
->buf
);
8079 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
8080 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8083 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
8084 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
8086 /* 'm' means there is (or at least might be) more data after this
8087 batch. That does not make sense unless there's at least one byte
8088 of data in this reply. */
8089 if (rs
->buf
[0] == 'm' && packet_len
== 1)
8090 error (_("Remote qXfer reply contained no data."));
8092 /* Got some data. */
8093 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
8095 /* 'l' is an EOF marker, possibly including a final block of data,
8096 or possibly empty. If we have the final block of a non-empty
8097 object, record this fact to bypass a subsequent partial read. */
8098 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
8100 finished_object
= xstrdup (object_name
);
8101 finished_annex
= xstrdup (annex
? annex
: "");
8102 finished_offset
= offset
+ i
;
8109 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
8110 const char *annex
, gdb_byte
*readbuf
,
8111 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
8113 struct remote_state
*rs
;
8118 set_general_thread (inferior_ptid
);
8120 rs
= get_remote_state ();
8122 /* Handle memory using the standard memory routines. */
8123 if (object
== TARGET_OBJECT_MEMORY
)
8129 /* If the remote target is connected but not running, we should
8130 pass this request down to a lower stratum (e.g. the executable
8132 if (!target_has_execution
)
8135 if (writebuf
!= NULL
)
8136 xfered
= remote_write_bytes (offset
, writebuf
, len
);
8138 xfered
= remote_read_bytes (offset
, readbuf
, len
);
8142 else if (xfered
== 0 && errno
== 0)
8148 /* Handle SPU memory using qxfer packets. */
8149 if (object
== TARGET_OBJECT_SPU
)
8152 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
8153 &remote_protocol_packets
8154 [PACKET_qXfer_spu_read
]);
8156 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
8157 &remote_protocol_packets
8158 [PACKET_qXfer_spu_write
]);
8161 /* Handle extra signal info using qxfer packets. */
8162 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
8165 return remote_read_qxfer (ops
, "siginfo", annex
, readbuf
, offset
, len
,
8166 &remote_protocol_packets
8167 [PACKET_qXfer_siginfo_read
]);
8169 return remote_write_qxfer (ops
, "siginfo", annex
,
8170 writebuf
, offset
, len
,
8171 &remote_protocol_packets
8172 [PACKET_qXfer_siginfo_write
]);
8175 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
8178 return remote_read_qxfer (ops
, "statictrace", annex
,
8179 readbuf
, offset
, len
,
8180 &remote_protocol_packets
8181 [PACKET_qXfer_statictrace_read
]);
8186 /* Only handle flash writes. */
8187 if (writebuf
!= NULL
)
8193 case TARGET_OBJECT_FLASH
:
8194 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
8198 else if (xfered
== 0 && errno
== 0)
8208 /* Map pre-existing objects onto letters. DO NOT do this for new
8209 objects!!! Instead specify new query packets. */
8212 case TARGET_OBJECT_AVR
:
8216 case TARGET_OBJECT_AUXV
:
8217 gdb_assert (annex
== NULL
);
8218 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
8219 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
8221 case TARGET_OBJECT_AVAILABLE_FEATURES
:
8222 return remote_read_qxfer
8223 (ops
, "features", annex
, readbuf
, offset
, len
,
8224 &remote_protocol_packets
[PACKET_qXfer_features
]);
8226 case TARGET_OBJECT_LIBRARIES
:
8227 return remote_read_qxfer
8228 (ops
, "libraries", annex
, readbuf
, offset
, len
,
8229 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
8231 case TARGET_OBJECT_MEMORY_MAP
:
8232 gdb_assert (annex
== NULL
);
8233 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
8234 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
8236 case TARGET_OBJECT_OSDATA
:
8237 /* Should only get here if we're connected. */
8238 gdb_assert (remote_desc
);
8239 return remote_read_qxfer
8240 (ops
, "osdata", annex
, readbuf
, offset
, len
,
8241 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
8243 case TARGET_OBJECT_THREADS
:
8244 gdb_assert (annex
== NULL
);
8245 return remote_read_qxfer (ops
, "threads", annex
, readbuf
, offset
, len
,
8246 &remote_protocol_packets
[PACKET_qXfer_threads
]);
8252 /* Note: a zero OFFSET and LEN can be used to query the minimum
8254 if (offset
== 0 && len
== 0)
8255 return (get_remote_packet_size ());
8256 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8257 large enough let the caller deal with it. */
8258 if (len
< get_remote_packet_size ())
8260 len
= get_remote_packet_size ();
8262 /* Except for querying the minimum buffer size, target must be open. */
8264 error (_("remote query is only available after target open"));
8266 gdb_assert (annex
!= NULL
);
8267 gdb_assert (readbuf
!= NULL
);
8273 /* We used one buffer char for the remote protocol q command and
8274 another for the query type. As the remote protocol encapsulation
8275 uses 4 chars plus one extra in case we are debugging
8276 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8279 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
8281 /* Bad caller may have sent forbidden characters. */
8282 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
8287 gdb_assert (annex
[i
] == '\0');
8289 i
= putpkt (rs
->buf
);
8293 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8294 strcpy ((char *) readbuf
, rs
->buf
);
8296 return strlen ((char *) readbuf
);
8300 remote_search_memory (struct target_ops
* ops
,
8301 CORE_ADDR start_addr
, ULONGEST search_space_len
,
8302 const gdb_byte
*pattern
, ULONGEST pattern_len
,
8303 CORE_ADDR
*found_addrp
)
8305 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
8306 struct remote_state
*rs
= get_remote_state ();
8307 int max_size
= get_memory_write_packet_size ();
8308 struct packet_config
*packet
=
8309 &remote_protocol_packets
[PACKET_qSearch_memory
];
8310 /* Number of packet bytes used to encode the pattern;
8311 this could be more than PATTERN_LEN due to escape characters. */
8312 int escaped_pattern_len
;
8313 /* Amount of pattern that was encodable in the packet. */
8314 int used_pattern_len
;
8317 ULONGEST found_addr
;
8319 /* Don't go to the target if we don't have to.
8320 This is done before checking packet->support to avoid the possibility that
8321 a success for this edge case means the facility works in general. */
8322 if (pattern_len
> search_space_len
)
8324 if (pattern_len
== 0)
8326 *found_addrp
= start_addr
;
8330 /* If we already know the packet isn't supported, fall back to the simple
8331 way of searching memory. */
8333 if (packet
->support
== PACKET_DISABLE
)
8335 /* Target doesn't provided special support, fall back and use the
8336 standard support (copy memory and do the search here). */
8337 return simple_search_memory (ops
, start_addr
, search_space_len
,
8338 pattern
, pattern_len
, found_addrp
);
8341 /* Insert header. */
8342 i
= snprintf (rs
->buf
, max_size
,
8343 "qSearch:memory:%s;%s;",
8344 phex_nz (start_addr
, addr_size
),
8345 phex_nz (search_space_len
, sizeof (search_space_len
)));
8346 max_size
-= (i
+ 1);
8348 /* Escape as much data as fits into rs->buf. */
8349 escaped_pattern_len
=
8350 remote_escape_output (pattern
, pattern_len
, (rs
->buf
+ i
),
8351 &used_pattern_len
, max_size
);
8353 /* Bail if the pattern is too large. */
8354 if (used_pattern_len
!= pattern_len
)
8355 error ("Pattern is too large to transmit to remote target.");
8357 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
8358 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8359 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8361 /* The request may not have worked because the command is not
8362 supported. If so, fall back to the simple way. */
8363 if (packet
->support
== PACKET_DISABLE
)
8365 return simple_search_memory (ops
, start_addr
, search_space_len
,
8366 pattern
, pattern_len
, found_addrp
);
8371 if (rs
->buf
[0] == '0')
8373 else if (rs
->buf
[0] == '1')
8376 if (rs
->buf
[1] != ',')
8377 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8378 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
8379 *found_addrp
= found_addr
;
8382 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8388 remote_rcmd (char *command
,
8389 struct ui_file
*outbuf
)
8391 struct remote_state
*rs
= get_remote_state ();
8395 error (_("remote rcmd is only available after target open"));
8397 /* Send a NULL command across as an empty command. */
8398 if (command
== NULL
)
8401 /* The query prefix. */
8402 strcpy (rs
->buf
, "qRcmd,");
8403 p
= strchr (rs
->buf
, '\0');
8405 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/)
8406 > get_remote_packet_size ())
8407 error (_("\"monitor\" command ``%s'' is too long."), command
);
8409 /* Encode the actual command. */
8410 bin2hex ((gdb_byte
*) command
, p
, 0);
8412 if (putpkt (rs
->buf
) < 0)
8413 error (_("Communication problem with target."));
8415 /* get/display the response */
8420 /* XXX - see also remote_get_noisy_reply(). */
8422 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8425 error (_("Target does not support this command."));
8426 if (buf
[0] == 'O' && buf
[1] != 'K')
8428 remote_console_output (buf
+ 1); /* 'O' message from stub. */
8431 if (strcmp (buf
, "OK") == 0)
8433 if (strlen (buf
) == 3 && buf
[0] == 'E'
8434 && isdigit (buf
[1]) && isdigit (buf
[2]))
8436 error (_("Protocol error with Rcmd"));
8438 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
8440 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
8442 fputc_unfiltered (c
, outbuf
);
8448 static VEC(mem_region_s
) *
8449 remote_memory_map (struct target_ops
*ops
)
8451 VEC(mem_region_s
) *result
= NULL
;
8452 char *text
= target_read_stralloc (¤t_target
,
8453 TARGET_OBJECT_MEMORY_MAP
, NULL
);
8457 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
8459 result
= parse_memory_map (text
);
8460 do_cleanups (back_to
);
8467 packet_command (char *args
, int from_tty
)
8469 struct remote_state
*rs
= get_remote_state ();
8472 error (_("command can only be used with remote target"));
8475 error (_("remote-packet command requires packet text as argument"));
8477 puts_filtered ("sending: ");
8478 print_packet (args
);
8479 puts_filtered ("\n");
8482 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8483 puts_filtered ("received: ");
8484 print_packet (rs
->buf
);
8485 puts_filtered ("\n");
8489 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
8491 static void display_thread_info (struct gdb_ext_thread_info
*info
);
8493 static void threadset_test_cmd (char *cmd
, int tty
);
8495 static void threadalive_test (char *cmd
, int tty
);
8497 static void threadlist_test_cmd (char *cmd
, int tty
);
8499 int get_and_display_threadinfo (threadref
*ref
);
8501 static void threadinfo_test_cmd (char *cmd
, int tty
);
8503 static int thread_display_step (threadref
*ref
, void *context
);
8505 static void threadlist_update_test_cmd (char *cmd
, int tty
);
8507 static void init_remote_threadtests (void);
8509 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
8512 threadset_test_cmd (char *cmd
, int tty
)
8514 int sample_thread
= SAMPLE_THREAD
;
8516 printf_filtered (_("Remote threadset test\n"));
8517 set_general_thread (sample_thread
);
8522 threadalive_test (char *cmd
, int tty
)
8524 int sample_thread
= SAMPLE_THREAD
;
8525 int pid
= ptid_get_pid (inferior_ptid
);
8526 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
8528 if (remote_thread_alive (ptid
))
8529 printf_filtered ("PASS: Thread alive test\n");
8531 printf_filtered ("FAIL: Thread alive test\n");
8534 void output_threadid (char *title
, threadref
*ref
);
8537 output_threadid (char *title
, threadref
*ref
)
8541 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
8543 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
8547 threadlist_test_cmd (char *cmd
, int tty
)
8550 threadref nextthread
;
8551 int done
, result_count
;
8552 threadref threadlist
[3];
8554 printf_filtered ("Remote Threadlist test\n");
8555 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
8556 &result_count
, &threadlist
[0]))
8557 printf_filtered ("FAIL: threadlist test\n");
8560 threadref
*scan
= threadlist
;
8561 threadref
*limit
= scan
+ result_count
;
8563 while (scan
< limit
)
8564 output_threadid (" thread ", scan
++);
8569 display_thread_info (struct gdb_ext_thread_info
*info
)
8571 output_threadid ("Threadid: ", &info
->threadid
);
8572 printf_filtered ("Name: %s\n ", info
->shortname
);
8573 printf_filtered ("State: %s\n", info
->display
);
8574 printf_filtered ("other: %s\n\n", info
->more_display
);
8578 get_and_display_threadinfo (threadref
*ref
)
8582 struct gdb_ext_thread_info threadinfo
;
8584 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
8585 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
8586 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
8587 display_thread_info (&threadinfo
);
8592 threadinfo_test_cmd (char *cmd
, int tty
)
8594 int athread
= SAMPLE_THREAD
;
8598 int_to_threadref (&thread
, athread
);
8599 printf_filtered ("Remote Threadinfo test\n");
8600 if (!get_and_display_threadinfo (&thread
))
8601 printf_filtered ("FAIL cannot get thread info\n");
8605 thread_display_step (threadref
*ref
, void *context
)
8607 /* output_threadid(" threadstep ",ref); *//* simple test */
8608 return get_and_display_threadinfo (ref
);
8612 threadlist_update_test_cmd (char *cmd
, int tty
)
8614 printf_filtered ("Remote Threadlist update test\n");
8615 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
8619 init_remote_threadtests (void)
8621 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
8622 _("Fetch and print the remote list of "
8623 "thread identifiers, one pkt only"));
8624 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
8625 _("Fetch and display info about one thread"));
8626 add_com ("tset", class_obscure
, threadset_test_cmd
,
8627 _("Test setting to a different thread"));
8628 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
8629 _("Iterate through updating all remote thread info"));
8630 add_com ("talive", class_obscure
, threadalive_test
,
8631 _(" Remote thread alive test "));
8636 /* Convert a thread ID to a string. Returns the string in a static
8640 remote_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
8642 static char buf
[64];
8643 struct remote_state
*rs
= get_remote_state ();
8645 if (ptid_is_pid (ptid
))
8647 /* Printing an inferior target id. */
8649 /* When multi-process extensions are off, there's no way in the
8650 remote protocol to know the remote process id, if there's any
8651 at all. There's one exception --- when we're connected with
8652 target extended-remote, and we manually attached to a process
8653 with "attach PID". We don't record anywhere a flag that
8654 allows us to distinguish that case from the case of
8655 connecting with extended-remote and the stub already being
8656 attached to a process, and reporting yes to qAttached, hence
8657 no smart special casing here. */
8658 if (!remote_multi_process_p (rs
))
8660 xsnprintf (buf
, sizeof buf
, "Remote target");
8664 return normal_pid_to_str (ptid
);
8668 if (ptid_equal (magic_null_ptid
, ptid
))
8669 xsnprintf (buf
, sizeof buf
, "Thread <main>");
8670 else if (remote_multi_process_p (rs
))
8671 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
8672 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
8674 xsnprintf (buf
, sizeof buf
, "Thread %ld",
8675 ptid_get_tid (ptid
));
8680 /* Get the address of the thread local variable in OBJFILE which is
8681 stored at OFFSET within the thread local storage for thread PTID. */
8684 remote_get_thread_local_address (struct target_ops
*ops
,
8685 ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
8687 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
8689 struct remote_state
*rs
= get_remote_state ();
8691 char *endp
= rs
->buf
+ get_remote_packet_size ();
8692 enum packet_result result
;
8694 strcpy (p
, "qGetTLSAddr:");
8696 p
= write_ptid (p
, endp
, ptid
);
8698 p
+= hexnumstr (p
, offset
);
8700 p
+= hexnumstr (p
, lm
);
8704 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8705 result
= packet_ok (rs
->buf
,
8706 &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
8707 if (result
== PACKET_OK
)
8711 unpack_varlen_hex (rs
->buf
, &result
);
8714 else if (result
== PACKET_UNKNOWN
)
8715 throw_error (TLS_GENERIC_ERROR
,
8716 _("Remote target doesn't support qGetTLSAddr packet"));
8718 throw_error (TLS_GENERIC_ERROR
,
8719 _("Remote target failed to process qGetTLSAddr request"));
8722 throw_error (TLS_GENERIC_ERROR
,
8723 _("TLS not supported or disabled on this target"));
8728 /* Provide thread local base, i.e. Thread Information Block address.
8729 Returns 1 if ptid is found and thread_local_base is non zero. */
8732 remote_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
8734 if (remote_protocol_packets
[PACKET_qGetTIBAddr
].support
!= PACKET_DISABLE
)
8736 struct remote_state
*rs
= get_remote_state ();
8738 char *endp
= rs
->buf
+ get_remote_packet_size ();
8739 enum packet_result result
;
8741 strcpy (p
, "qGetTIBAddr:");
8743 p
= write_ptid (p
, endp
, ptid
);
8747 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8748 result
= packet_ok (rs
->buf
,
8749 &remote_protocol_packets
[PACKET_qGetTIBAddr
]);
8750 if (result
== PACKET_OK
)
8754 unpack_varlen_hex (rs
->buf
, &result
);
8756 *addr
= (CORE_ADDR
) result
;
8759 else if (result
== PACKET_UNKNOWN
)
8760 error (_("Remote target doesn't support qGetTIBAddr packet"));
8762 error (_("Remote target failed to process qGetTIBAddr request"));
8765 error (_("qGetTIBAddr not supported or disabled on this target"));
8770 /* Support for inferring a target description based on the current
8771 architecture and the size of a 'g' packet. While the 'g' packet
8772 can have any size (since optional registers can be left off the
8773 end), some sizes are easily recognizable given knowledge of the
8774 approximate architecture. */
8776 struct remote_g_packet_guess
8779 const struct target_desc
*tdesc
;
8781 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
8782 DEF_VEC_O(remote_g_packet_guess_s
);
8784 struct remote_g_packet_data
8786 VEC(remote_g_packet_guess_s
) *guesses
;
8789 static struct gdbarch_data
*remote_g_packet_data_handle
;
8792 remote_g_packet_data_init (struct obstack
*obstack
)
8794 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
8798 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
8799 const struct target_desc
*tdesc
)
8801 struct remote_g_packet_data
*data
8802 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
8803 struct remote_g_packet_guess new_guess
, *guess
;
8806 gdb_assert (tdesc
!= NULL
);
8809 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
8811 if (guess
->bytes
== bytes
)
8812 internal_error (__FILE__
, __LINE__
,
8813 "Duplicate g packet description added for size %d",
8816 new_guess
.bytes
= bytes
;
8817 new_guess
.tdesc
= tdesc
;
8818 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
8821 /* Return 1 if remote_read_description would do anything on this target
8822 and architecture, 0 otherwise. */
8825 remote_read_description_p (struct target_ops
*target
)
8827 struct remote_g_packet_data
*data
8828 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
8830 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
8836 static const struct target_desc
*
8837 remote_read_description (struct target_ops
*target
)
8839 struct remote_g_packet_data
*data
8840 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
8842 /* Do not try this during initial connection, when we do not know
8843 whether there is a running but stopped thread. */
8844 if (!target_has_execution
|| ptid_equal (inferior_ptid
, null_ptid
))
8847 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
8849 struct remote_g_packet_guess
*guess
;
8851 int bytes
= send_g_packet ();
8854 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
8856 if (guess
->bytes
== bytes
)
8857 return guess
->tdesc
;
8859 /* We discard the g packet. A minor optimization would be to
8860 hold on to it, and fill the register cache once we have selected
8861 an architecture, but it's too tricky to do safely. */
8867 /* Remote file transfer support. This is host-initiated I/O, not
8868 target-initiated; for target-initiated, see remote-fileio.c. */
8870 /* If *LEFT is at least the length of STRING, copy STRING to
8871 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8872 decrease *LEFT. Otherwise raise an error. */
8875 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
8877 int len
= strlen (string
);
8880 error (_("Packet too long for target."));
8882 memcpy (*buffer
, string
, len
);
8886 /* NUL-terminate the buffer as a convenience, if there is
8892 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
8893 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8894 decrease *LEFT. Otherwise raise an error. */
8897 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
8900 if (2 * len
> *left
)
8901 error (_("Packet too long for target."));
8903 bin2hex (bytes
, *buffer
, len
);
8907 /* NUL-terminate the buffer as a convenience, if there is
8913 /* If *LEFT is large enough, convert VALUE to hex and add it to
8914 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8915 decrease *LEFT. Otherwise raise an error. */
8918 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
8920 int len
= hexnumlen (value
);
8923 error (_("Packet too long for target."));
8925 hexnumstr (*buffer
, value
);
8929 /* NUL-terminate the buffer as a convenience, if there is
8935 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
8936 value, *REMOTE_ERRNO to the remote error number or zero if none
8937 was included, and *ATTACHMENT to point to the start of the annex
8938 if any. The length of the packet isn't needed here; there may
8939 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
8941 Return 0 if the packet could be parsed, -1 if it could not. If
8942 -1 is returned, the other variables may not be initialized. */
8945 remote_hostio_parse_result (char *buffer
, int *retcode
,
8946 int *remote_errno
, char **attachment
)
8953 if (buffer
[0] != 'F')
8957 *retcode
= strtol (&buffer
[1], &p
, 16);
8958 if (errno
!= 0 || p
== &buffer
[1])
8961 /* Check for ",errno". */
8965 *remote_errno
= strtol (p
+ 1, &p2
, 16);
8966 if (errno
!= 0 || p
+ 1 == p2
)
8971 /* Check for ";attachment". If there is no attachment, the
8972 packet should end here. */
8975 *attachment
= p
+ 1;
8978 else if (*p
== '\0')
8984 /* Send a prepared I/O packet to the target and read its response.
8985 The prepared packet is in the global RS->BUF before this function
8986 is called, and the answer is there when we return.
8988 COMMAND_BYTES is the length of the request to send, which may include
8989 binary data. WHICH_PACKET is the packet configuration to check
8990 before attempting a packet. If an error occurs, *REMOTE_ERRNO
8991 is set to the error number and -1 is returned. Otherwise the value
8992 returned by the function is returned.
8994 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
8995 attachment is expected; an error will be reported if there's a
8996 mismatch. If one is found, *ATTACHMENT will be set to point into
8997 the packet buffer and *ATTACHMENT_LEN will be set to the
8998 attachment's length. */
9001 remote_hostio_send_command (int command_bytes
, int which_packet
,
9002 int *remote_errno
, char **attachment
,
9003 int *attachment_len
)
9005 struct remote_state
*rs
= get_remote_state ();
9006 int ret
, bytes_read
;
9007 char *attachment_tmp
;
9010 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
9012 *remote_errno
= FILEIO_ENOSYS
;
9016 putpkt_binary (rs
->buf
, command_bytes
);
9017 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
9019 /* If it timed out, something is wrong. Don't try to parse the
9023 *remote_errno
= FILEIO_EINVAL
;
9027 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
9030 *remote_errno
= FILEIO_EINVAL
;
9032 case PACKET_UNKNOWN
:
9033 *remote_errno
= FILEIO_ENOSYS
;
9039 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
9042 *remote_errno
= FILEIO_EINVAL
;
9046 /* Make sure we saw an attachment if and only if we expected one. */
9047 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
9048 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
9050 *remote_errno
= FILEIO_EINVAL
;
9054 /* If an attachment was found, it must point into the packet buffer;
9055 work out how many bytes there were. */
9056 if (attachment_tmp
!= NULL
)
9058 *attachment
= attachment_tmp
;
9059 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
9065 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9066 remote file descriptor, or -1 if an error occurs (and set
9070 remote_hostio_open (const char *filename
, int flags
, int mode
,
9073 struct remote_state
*rs
= get_remote_state ();
9075 int left
= get_remote_packet_size () - 1;
9077 remote_buffer_add_string (&p
, &left
, "vFile:open:");
9079 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9081 remote_buffer_add_string (&p
, &left
, ",");
9083 remote_buffer_add_int (&p
, &left
, flags
);
9084 remote_buffer_add_string (&p
, &left
, ",");
9086 remote_buffer_add_int (&p
, &left
, mode
);
9088 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
9089 remote_errno
, NULL
, NULL
);
9092 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9093 Return the number of bytes written, or -1 if an error occurs (and
9094 set *REMOTE_ERRNO). */
9097 remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
9098 ULONGEST offset
, int *remote_errno
)
9100 struct remote_state
*rs
= get_remote_state ();
9102 int left
= get_remote_packet_size ();
9105 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
9107 remote_buffer_add_int (&p
, &left
, fd
);
9108 remote_buffer_add_string (&p
, &left
, ",");
9110 remote_buffer_add_int (&p
, &left
, offset
);
9111 remote_buffer_add_string (&p
, &left
, ",");
9113 p
+= remote_escape_output (write_buf
, len
, p
, &out_len
,
9114 get_remote_packet_size () - (p
- rs
->buf
));
9116 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
9117 remote_errno
, NULL
, NULL
);
9120 /* Read up to LEN bytes FD on the remote target into READ_BUF
9121 Return the number of bytes read, or -1 if an error occurs (and
9122 set *REMOTE_ERRNO). */
9125 remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
9126 ULONGEST offset
, int *remote_errno
)
9128 struct remote_state
*rs
= get_remote_state ();
9131 int left
= get_remote_packet_size ();
9132 int ret
, attachment_len
;
9135 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
9137 remote_buffer_add_int (&p
, &left
, fd
);
9138 remote_buffer_add_string (&p
, &left
, ",");
9140 remote_buffer_add_int (&p
, &left
, len
);
9141 remote_buffer_add_string (&p
, &left
, ",");
9143 remote_buffer_add_int (&p
, &left
, offset
);
9145 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
9146 remote_errno
, &attachment
,
9152 read_len
= remote_unescape_input (attachment
, attachment_len
,
9154 if (read_len
!= ret
)
9155 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
9160 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9161 (and set *REMOTE_ERRNO). */
9164 remote_hostio_close (int fd
, int *remote_errno
)
9166 struct remote_state
*rs
= get_remote_state ();
9168 int left
= get_remote_packet_size () - 1;
9170 remote_buffer_add_string (&p
, &left
, "vFile:close:");
9172 remote_buffer_add_int (&p
, &left
, fd
);
9174 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
9175 remote_errno
, NULL
, NULL
);
9178 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9179 occurs (and set *REMOTE_ERRNO). */
9182 remote_hostio_unlink (const char *filename
, int *remote_errno
)
9184 struct remote_state
*rs
= get_remote_state ();
9186 int left
= get_remote_packet_size () - 1;
9188 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
9190 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9193 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
9194 remote_errno
, NULL
, NULL
);
9198 remote_fileio_errno_to_host (int errnum
)
9222 case FILEIO_ENOTDIR
:
9242 case FILEIO_ENAMETOOLONG
:
9243 return ENAMETOOLONG
;
9249 remote_hostio_error (int errnum
)
9251 int host_error
= remote_fileio_errno_to_host (errnum
);
9253 if (host_error
== -1)
9254 error (_("Unknown remote I/O error %d"), errnum
);
9256 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
9260 remote_hostio_close_cleanup (void *opaque
)
9262 int fd
= *(int *) opaque
;
9265 remote_hostio_close (fd
, &remote_errno
);
9270 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
9272 const char *filename
= bfd_get_filename (abfd
);
9273 int fd
, remote_errno
;
9276 gdb_assert (remote_filename_p (filename
));
9278 fd
= remote_hostio_open (filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
9281 errno
= remote_fileio_errno_to_host (remote_errno
);
9282 bfd_set_error (bfd_error_system_call
);
9286 stream
= xmalloc (sizeof (int));
9292 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
9294 int fd
= *(int *)stream
;
9299 /* Ignore errors on close; these may happen if the remote
9300 connection was already torn down. */
9301 remote_hostio_close (fd
, &remote_errno
);
9307 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
9308 file_ptr nbytes
, file_ptr offset
)
9310 int fd
= *(int *)stream
;
9312 file_ptr pos
, bytes
;
9315 while (nbytes
> pos
)
9317 bytes
= remote_hostio_pread (fd
, (char *)buf
+ pos
, nbytes
- pos
,
9318 offset
+ pos
, &remote_errno
);
9320 /* Success, but no bytes, means end-of-file. */
9324 errno
= remote_fileio_errno_to_host (remote_errno
);
9325 bfd_set_error (bfd_error_system_call
);
9336 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
9338 /* FIXME: We should probably implement remote_hostio_stat. */
9339 sb
->st_size
= INT_MAX
;
9344 remote_filename_p (const char *filename
)
9346 return strncmp (filename
, "remote:", 7) == 0;
9350 remote_bfd_open (const char *remote_file
, const char *target
)
9352 return bfd_openr_iovec (remote_file
, target
,
9353 remote_bfd_iovec_open
, NULL
,
9354 remote_bfd_iovec_pread
,
9355 remote_bfd_iovec_close
,
9356 remote_bfd_iovec_stat
);
9360 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
9362 struct cleanup
*back_to
, *close_cleanup
;
9363 int retcode
, fd
, remote_errno
, bytes
, io_size
;
9366 int bytes_in_buffer
;
9371 error (_("command can only be used with remote target"));
9373 file
= fopen (local_file
, "rb");
9375 perror_with_name (local_file
);
9376 back_to
= make_cleanup_fclose (file
);
9378 fd
= remote_hostio_open (remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
9380 0700, &remote_errno
);
9382 remote_hostio_error (remote_errno
);
9384 /* Send up to this many bytes at once. They won't all fit in the
9385 remote packet limit, so we'll transfer slightly fewer. */
9386 io_size
= get_remote_packet_size ();
9387 buffer
= xmalloc (io_size
);
9388 make_cleanup (xfree
, buffer
);
9390 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
9392 bytes_in_buffer
= 0;
9395 while (bytes_in_buffer
|| !saw_eof
)
9399 bytes
= fread (buffer
+ bytes_in_buffer
, 1,
9400 io_size
- bytes_in_buffer
,
9405 error (_("Error reading %s."), local_file
);
9408 /* EOF. Unless there is something still in the
9409 buffer from the last iteration, we are done. */
9411 if (bytes_in_buffer
== 0)
9419 bytes
+= bytes_in_buffer
;
9420 bytes_in_buffer
= 0;
9422 retcode
= remote_hostio_pwrite (fd
, buffer
, bytes
,
9423 offset
, &remote_errno
);
9426 remote_hostio_error (remote_errno
);
9427 else if (retcode
== 0)
9428 error (_("Remote write of %d bytes returned 0!"), bytes
);
9429 else if (retcode
< bytes
)
9431 /* Short write. Save the rest of the read data for the next
9433 bytes_in_buffer
= bytes
- retcode
;
9434 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
9440 discard_cleanups (close_cleanup
);
9441 if (remote_hostio_close (fd
, &remote_errno
))
9442 remote_hostio_error (remote_errno
);
9445 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
9446 do_cleanups (back_to
);
9450 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
9452 struct cleanup
*back_to
, *close_cleanup
;
9453 int fd
, remote_errno
, bytes
, io_size
;
9459 error (_("command can only be used with remote target"));
9461 fd
= remote_hostio_open (remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
9463 remote_hostio_error (remote_errno
);
9465 file
= fopen (local_file
, "wb");
9467 perror_with_name (local_file
);
9468 back_to
= make_cleanup_fclose (file
);
9470 /* Send up to this many bytes at once. They won't all fit in the
9471 remote packet limit, so we'll transfer slightly fewer. */
9472 io_size
= get_remote_packet_size ();
9473 buffer
= xmalloc (io_size
);
9474 make_cleanup (xfree
, buffer
);
9476 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
9481 bytes
= remote_hostio_pread (fd
, buffer
, io_size
, offset
, &remote_errno
);
9483 /* Success, but no bytes, means end-of-file. */
9486 remote_hostio_error (remote_errno
);
9490 bytes
= fwrite (buffer
, 1, bytes
, file
);
9492 perror_with_name (local_file
);
9495 discard_cleanups (close_cleanup
);
9496 if (remote_hostio_close (fd
, &remote_errno
))
9497 remote_hostio_error (remote_errno
);
9500 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
9501 do_cleanups (back_to
);
9505 remote_file_delete (const char *remote_file
, int from_tty
)
9507 int retcode
, remote_errno
;
9510 error (_("command can only be used with remote target"));
9512 retcode
= remote_hostio_unlink (remote_file
, &remote_errno
);
9514 remote_hostio_error (remote_errno
);
9517 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
9521 remote_put_command (char *args
, int from_tty
)
9523 struct cleanup
*back_to
;
9527 error_no_arg (_("file to put"));
9529 argv
= gdb_buildargv (args
);
9530 back_to
= make_cleanup_freeargv (argv
);
9531 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
9532 error (_("Invalid parameters to remote put"));
9534 remote_file_put (argv
[0], argv
[1], from_tty
);
9536 do_cleanups (back_to
);
9540 remote_get_command (char *args
, int from_tty
)
9542 struct cleanup
*back_to
;
9546 error_no_arg (_("file to get"));
9548 argv
= gdb_buildargv (args
);
9549 back_to
= make_cleanup_freeargv (argv
);
9550 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
9551 error (_("Invalid parameters to remote get"));
9553 remote_file_get (argv
[0], argv
[1], from_tty
);
9555 do_cleanups (back_to
);
9559 remote_delete_command (char *args
, int from_tty
)
9561 struct cleanup
*back_to
;
9565 error_no_arg (_("file to delete"));
9567 argv
= gdb_buildargv (args
);
9568 back_to
= make_cleanup_freeargv (argv
);
9569 if (argv
[0] == NULL
|| argv
[1] != NULL
)
9570 error (_("Invalid parameters to remote delete"));
9572 remote_file_delete (argv
[0], from_tty
);
9574 do_cleanups (back_to
);
9578 remote_command (char *args
, int from_tty
)
9580 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
9584 remote_can_execute_reverse (void)
9586 if (remote_protocol_packets
[PACKET_bs
].support
== PACKET_ENABLE
9587 || remote_protocol_packets
[PACKET_bc
].support
== PACKET_ENABLE
)
9594 remote_supports_non_stop (void)
9600 remote_supports_multi_process (void)
9602 struct remote_state
*rs
= get_remote_state ();
9604 return remote_multi_process_p (rs
);
9608 remote_supports_cond_tracepoints (void)
9610 struct remote_state
*rs
= get_remote_state ();
9612 return rs
->cond_tracepoints
;
9616 remote_supports_fast_tracepoints (void)
9618 struct remote_state
*rs
= get_remote_state ();
9620 return rs
->fast_tracepoints
;
9624 remote_supports_static_tracepoints (void)
9626 struct remote_state
*rs
= get_remote_state ();
9628 return rs
->static_tracepoints
;
9632 remote_trace_init (void)
9635 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9636 if (strcmp (target_buf
, "OK") != 0)
9637 error (_("Target does not support this command."));
9640 static void free_actions_list (char **actions_list
);
9641 static void free_actions_list_cleanup_wrapper (void *);
9643 free_actions_list_cleanup_wrapper (void *al
)
9645 free_actions_list (al
);
9649 free_actions_list (char **actions_list
)
9653 if (actions_list
== 0)
9656 for (ndx
= 0; actions_list
[ndx
]; ndx
++)
9657 xfree (actions_list
[ndx
]);
9659 xfree (actions_list
);
9662 /* Recursive routine to walk through command list including loops, and
9663 download packets for each command. */
9666 remote_download_command_source (int num
, ULONGEST addr
,
9667 struct command_line
*cmds
)
9669 struct remote_state
*rs
= get_remote_state ();
9670 struct command_line
*cmd
;
9672 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
9674 QUIT
; /* Allow user to bail out with ^C. */
9675 strcpy (rs
->buf
, "QTDPsrc:");
9676 encode_source_string (num
, addr
, "cmd", cmd
->line
,
9677 rs
->buf
+ strlen (rs
->buf
),
9678 rs
->buf_size
- strlen (rs
->buf
));
9680 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9681 if (strcmp (target_buf
, "OK"))
9682 warning (_("Target does not support source download."));
9684 if (cmd
->control_type
== while_control
9685 || cmd
->control_type
== while_stepping_control
)
9687 remote_download_command_source (num
, addr
, *cmd
->body_list
);
9689 QUIT
; /* Allow user to bail out with ^C. */
9690 strcpy (rs
->buf
, "QTDPsrc:");
9691 encode_source_string (num
, addr
, "cmd", "end",
9692 rs
->buf
+ strlen (rs
->buf
),
9693 rs
->buf_size
- strlen (rs
->buf
));
9695 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9696 if (strcmp (target_buf
, "OK"))
9697 warning (_("Target does not support source download."));
9703 remote_download_tracepoint (struct breakpoint
*t
)
9705 struct bp_location
*loc
;
9710 char **stepping_actions
;
9712 struct cleanup
*old_chain
= NULL
;
9713 struct agent_expr
*aexpr
;
9714 struct cleanup
*aexpr_chain
= NULL
;
9717 /* Iterate over all the tracepoint locations. It's up to the target to
9718 notice multiple tracepoint packets with the same number but different
9719 addresses, and treat them as multiple locations. */
9720 for (loc
= t
->loc
; loc
; loc
= loc
->next
)
9722 encode_actions (t
, loc
, &tdp_actions
, &stepping_actions
);
9723 old_chain
= make_cleanup (free_actions_list_cleanup_wrapper
,
9725 (void) make_cleanup (free_actions_list_cleanup_wrapper
,
9728 tpaddr
= loc
->address
;
9729 sprintf_vma (addrbuf
, tpaddr
);
9730 sprintf (buf
, "QTDP:%x:%s:%c:%lx:%x", t
->number
,
9731 addrbuf
, /* address */
9732 (t
->enable_state
== bp_enabled
? 'E' : 'D'),
9733 t
->step_count
, t
->pass_count
);
9734 /* Fast tracepoints are mostly handled by the target, but we can
9735 tell the target how big of an instruction block should be moved
9737 if (t
->type
== bp_fast_tracepoint
)
9739 /* Only test for support at download time; we may not know
9740 target capabilities at definition time. */
9741 if (remote_supports_fast_tracepoints ())
9745 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch
,
9746 tpaddr
, &isize
, NULL
))
9747 sprintf (buf
+ strlen (buf
), ":F%x", isize
);
9749 /* If it passed validation at definition but fails now,
9750 something is very wrong. */
9751 internal_error (__FILE__
, __LINE__
,
9752 "Fast tracepoint not valid during download");
9755 /* Fast tracepoints are functionally identical to regular
9756 tracepoints, so don't take lack of support as a reason to
9757 give up on the trace run. */
9758 warning (_("Target does not support fast tracepoints, "
9759 "downloading %d as regular tracepoint"), t
->number
);
9761 else if (t
->type
== bp_static_tracepoint
)
9763 /* Only test for support at download time; we may not know
9764 target capabilities at definition time. */
9765 if (remote_supports_static_tracepoints ())
9767 struct static_tracepoint_marker marker
;
9769 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
9772 error (_("Static tracepoint not valid during download"));
9775 /* Fast tracepoints are functionally identical to regular
9776 tracepoints, so don't take lack of support as a reason
9777 to give up on the trace run. */
9778 error (_("Target does not support static tracepoints"));
9780 /* If the tracepoint has a conditional, make it into an agent
9781 expression and append to the definition. */
9784 /* Only test support at download time, we may not know target
9785 capabilities at definition time. */
9786 if (remote_supports_cond_tracepoints ())
9788 aexpr
= gen_eval_for_expr (tpaddr
, loc
->cond
);
9789 aexpr_chain
= make_cleanup_free_agent_expr (aexpr
);
9790 sprintf (buf
+ strlen (buf
), ":X%x,", aexpr
->len
);
9791 pkt
= buf
+ strlen (buf
);
9792 for (ndx
= 0; ndx
< aexpr
->len
; ++ndx
)
9793 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
9795 do_cleanups (aexpr_chain
);
9798 warning (_("Target does not support conditional tracepoints, "
9799 "ignoring tp %d cond"), t
->number
);
9802 if (t
->commands
|| *default_collect
)
9805 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9806 if (strcmp (target_buf
, "OK"))
9807 error (_("Target does not support tracepoints."));
9809 /* do_single_steps (t); */
9812 for (ndx
= 0; tdp_actions
[ndx
]; ndx
++)
9814 QUIT
; /* Allow user to bail out with ^C. */
9815 sprintf (buf
, "QTDP:-%x:%s:%s%c",
9816 t
->number
, addrbuf
, /* address */
9818 ((tdp_actions
[ndx
+ 1] || stepping_actions
)
9821 remote_get_noisy_reply (&target_buf
,
9823 if (strcmp (target_buf
, "OK"))
9824 error (_("Error on target while setting tracepoints."));
9827 if (stepping_actions
)
9829 for (ndx
= 0; stepping_actions
[ndx
]; ndx
++)
9831 QUIT
; /* Allow user to bail out with ^C. */
9832 sprintf (buf
, "QTDP:-%x:%s:%s%s%s",
9833 t
->number
, addrbuf
, /* address */
9834 ((ndx
== 0) ? "S" : ""),
9835 stepping_actions
[ndx
],
9836 (stepping_actions
[ndx
+ 1] ? "-" : ""));
9838 remote_get_noisy_reply (&target_buf
,
9840 if (strcmp (target_buf
, "OK"))
9841 error (_("Error on target while setting tracepoints."));
9845 if (remote_protocol_packets
[PACKET_TracepointSource
].support
9850 strcpy (buf
, "QTDPsrc:");
9851 encode_source_string (t
->number
, loc
->address
,
9852 "at", t
->addr_string
, buf
+ strlen (buf
),
9853 2048 - strlen (buf
));
9856 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9857 if (strcmp (target_buf
, "OK"))
9858 warning (_("Target does not support source download."));
9862 strcpy (buf
, "QTDPsrc:");
9863 encode_source_string (t
->number
, loc
->address
,
9864 "cond", t
->cond_string
, buf
+ strlen (buf
),
9865 2048 - strlen (buf
));
9867 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9868 if (strcmp (target_buf
, "OK"))
9869 warning (_("Target does not support source download."));
9871 remote_download_command_source (t
->number
, loc
->address
,
9872 breakpoint_commands (t
));
9875 do_cleanups (old_chain
);
9880 remote_download_trace_state_variable (struct trace_state_variable
*tsv
)
9882 struct remote_state
*rs
= get_remote_state ();
9885 sprintf (rs
->buf
, "QTDV:%x:%s:%x:",
9886 tsv
->number
, phex ((ULONGEST
) tsv
->initial_value
, 8), tsv
->builtin
);
9887 p
= rs
->buf
+ strlen (rs
->buf
);
9888 if ((p
- rs
->buf
) + strlen (tsv
->name
) * 2 >= get_remote_packet_size ())
9889 error (_("Trace state variable name too long for tsv definition packet"));
9890 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
->name
), p
, 0);
9893 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9894 if (*target_buf
== '\0')
9895 error (_("Target does not support this command."));
9896 if (strcmp (target_buf
, "OK") != 0)
9897 error (_("Error on target while downloading trace state variable."));
9901 remote_trace_set_readonly_regions (void)
9909 return; /* No information to give. */
9911 strcpy (target_buf
, "QTro");
9912 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
9914 char tmp1
[40], tmp2
[40];
9916 if ((s
->flags
& SEC_LOAD
) == 0 ||
9917 /* (s->flags & SEC_CODE) == 0 || */
9918 (s
->flags
& SEC_READONLY
) == 0)
9922 vma
= bfd_get_section_vma (,s
);
9923 size
= bfd_get_section_size (s
);
9924 sprintf_vma (tmp1
, vma
);
9925 sprintf_vma (tmp2
, vma
+ size
);
9926 sprintf (target_buf
+ strlen (target_buf
),
9927 ":%s,%s", tmp1
, tmp2
);
9931 putpkt (target_buf
);
9932 getpkt (&target_buf
, &target_buf_size
, 0);
9937 remote_trace_start (void)
9940 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9941 if (*target_buf
== '\0')
9942 error (_("Target does not support this command."));
9943 if (strcmp (target_buf
, "OK") != 0)
9944 error (_("Bogus reply from target: %s"), target_buf
);
9948 remote_get_trace_status (struct trace_status
*ts
)
9951 /* FIXME we need to get register block size some other way. */
9952 extern int trace_regblock_size
;
9954 trace_regblock_size
= get_remote_arch_state ()->sizeof_g_packet
;
9956 putpkt ("qTStatus");
9957 p
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9959 /* If the remote target doesn't do tracing, flag it. */
9963 /* We're working with a live target. */
9966 /* Set some defaults. */
9967 ts
->running_known
= 0;
9968 ts
->stop_reason
= trace_stop_reason_unknown
;
9969 ts
->traceframe_count
= -1;
9970 ts
->buffer_free
= 0;
9973 error (_("Bogus trace status reply from target: %s"), target_buf
);
9975 parse_trace_status (p
, ts
);
9981 remote_trace_stop (void)
9984 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9985 if (*target_buf
== '\0')
9986 error (_("Target does not support this command."));
9987 if (strcmp (target_buf
, "OK") != 0)
9988 error (_("Bogus reply from target: %s"), target_buf
);
9992 remote_trace_find (enum trace_find_type type
, int num
,
9993 ULONGEST addr1
, ULONGEST addr2
,
9996 struct remote_state
*rs
= get_remote_state ();
9998 int target_frameno
= -1, target_tracept
= -1;
10001 strcpy (p
, "QTFrame:");
10002 p
= strchr (p
, '\0');
10006 sprintf (p
, "%x", num
);
10009 sprintf (p
, "pc:%s", phex_nz (addr1
, 0));
10012 sprintf (p
, "tdp:%x", num
);
10015 sprintf (p
, "range:%s:%s", phex_nz (addr1
, 0), phex_nz (addr2
, 0));
10017 case tfind_outside
:
10018 sprintf (p
, "outside:%s:%s", phex_nz (addr1
, 0), phex_nz (addr2
, 0));
10021 error ("Unknown trace find type %d", type
);
10025 reply
= remote_get_noisy_reply (&(rs
->buf
), &sizeof_pkt
);
10026 if (*reply
== '\0')
10027 error (_("Target does not support this command."));
10029 while (reply
&& *reply
)
10034 target_frameno
= (int) strtol (p
, &reply
, 16);
10036 error (_("Unable to parse trace frame number"));
10037 if (target_frameno
== -1)
10042 target_tracept
= (int) strtol (p
, &reply
, 16);
10044 error (_("Unable to parse tracepoint number"));
10046 case 'O': /* "OK"? */
10047 if (reply
[1] == 'K' && reply
[2] == '\0')
10050 error (_("Bogus reply from target: %s"), reply
);
10053 error (_("Bogus reply from target: %s"), reply
);
10056 *tpp
= target_tracept
;
10057 return target_frameno
;
10061 remote_get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
10063 struct remote_state
*rs
= get_remote_state ();
10067 sprintf (rs
->buf
, "qTV:%x", tsvnum
);
10069 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10070 if (reply
&& *reply
)
10074 unpack_varlen_hex (reply
+ 1, &uval
);
10075 *val
= (LONGEST
) uval
;
10083 remote_save_trace_data (const char *filename
)
10085 struct remote_state
*rs
= get_remote_state ();
10089 strcpy (p
, "QTSave:");
10091 if ((p
- rs
->buf
) + strlen (filename
) * 2 >= get_remote_packet_size ())
10092 error (_("Remote file name too long for trace save packet"));
10093 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, 0);
10096 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10097 if (*reply
!= '\0')
10098 error (_("Target does not support this command."));
10099 if (strcmp (reply
, "OK") != 0)
10100 error (_("Bogus reply from target: %s"), reply
);
10104 /* This is basically a memory transfer, but needs to be its own packet
10105 because we don't know how the target actually organizes its trace
10106 memory, plus we want to be able to ask for as much as possible, but
10107 not be unhappy if we don't get as much as we ask for. */
10110 remote_get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
10112 struct remote_state
*rs
= get_remote_state ();
10118 strcpy (p
, "qTBuffer:");
10120 p
+= hexnumstr (p
, offset
);
10122 p
+= hexnumstr (p
, len
);
10126 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10127 if (reply
&& *reply
)
10129 /* 'l' by itself means we're at the end of the buffer and
10130 there is nothing more to get. */
10134 /* Convert the reply into binary. Limit the number of bytes to
10135 convert according to our passed-in buffer size, rather than
10136 what was returned in the packet; if the target is
10137 unexpectedly generous and gives us a bigger reply than we
10138 asked for, we don't want to crash. */
10139 rslt
= hex2bin (target_buf
, buf
, len
);
10143 /* Something went wrong, flag as an error. */
10148 remote_set_disconnected_tracing (int val
)
10150 struct remote_state
*rs
= get_remote_state ();
10152 if (rs
->disconnected_tracing
)
10156 sprintf (rs
->buf
, "QTDisconnected:%x", val
);
10158 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10159 if (*reply
== '\0')
10160 error (_("Target does not support this command."));
10161 if (strcmp (reply
, "OK") != 0)
10162 error (_("Bogus reply from target: %s"), reply
);
10165 warning (_("Target does not support disconnected tracing."));
10169 remote_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
10171 struct thread_info
*info
= find_thread_ptid (ptid
);
10173 if (info
&& info
->private)
10174 return info
->private->core
;
10179 remote_set_circular_trace_buffer (int val
)
10181 struct remote_state
*rs
= get_remote_state ();
10184 sprintf (rs
->buf
, "QTBuffer:circular:%x", val
);
10186 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10187 if (*reply
== '\0')
10188 error (_("Target does not support this command."));
10189 if (strcmp (reply
, "OK") != 0)
10190 error (_("Bogus reply from target: %s"), reply
);
10194 init_remote_ops (void)
10196 remote_ops
.to_shortname
= "remote";
10197 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
10198 remote_ops
.to_doc
=
10199 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10200 Specify the serial device it is connected to\n\
10201 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
10202 remote_ops
.to_open
= remote_open
;
10203 remote_ops
.to_close
= remote_close
;
10204 remote_ops
.to_detach
= remote_detach
;
10205 remote_ops
.to_disconnect
= remote_disconnect
;
10206 remote_ops
.to_resume
= remote_resume
;
10207 remote_ops
.to_wait
= remote_wait
;
10208 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
10209 remote_ops
.to_store_registers
= remote_store_registers
;
10210 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
10211 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
10212 remote_ops
.to_files_info
= remote_files_info
;
10213 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
10214 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
10215 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
10216 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
10217 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
10218 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
10219 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
10220 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
10221 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
10222 remote_ops
.to_kill
= remote_kill
;
10223 remote_ops
.to_load
= generic_load
;
10224 remote_ops
.to_mourn_inferior
= remote_mourn
;
10225 remote_ops
.to_notice_signals
= remote_notice_signals
;
10226 remote_ops
.to_thread_alive
= remote_thread_alive
;
10227 remote_ops
.to_find_new_threads
= remote_threads_info
;
10228 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
10229 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
10230 remote_ops
.to_get_ada_task_ptid
= remote_get_ada_task_ptid
;
10231 remote_ops
.to_stop
= remote_stop
;
10232 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
10233 remote_ops
.to_rcmd
= remote_rcmd
;
10234 remote_ops
.to_log_command
= serial_log_command
;
10235 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
10236 remote_ops
.to_stratum
= process_stratum
;
10237 remote_ops
.to_has_all_memory
= default_child_has_all_memory
;
10238 remote_ops
.to_has_memory
= default_child_has_memory
;
10239 remote_ops
.to_has_stack
= default_child_has_stack
;
10240 remote_ops
.to_has_registers
= default_child_has_registers
;
10241 remote_ops
.to_has_execution
= default_child_has_execution
;
10242 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
10243 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
10244 remote_ops
.to_magic
= OPS_MAGIC
;
10245 remote_ops
.to_memory_map
= remote_memory_map
;
10246 remote_ops
.to_flash_erase
= remote_flash_erase
;
10247 remote_ops
.to_flash_done
= remote_flash_done
;
10248 remote_ops
.to_read_description
= remote_read_description
;
10249 remote_ops
.to_search_memory
= remote_search_memory
;
10250 remote_ops
.to_can_async_p
= remote_can_async_p
;
10251 remote_ops
.to_is_async_p
= remote_is_async_p
;
10252 remote_ops
.to_async
= remote_async
;
10253 remote_ops
.to_async_mask
= remote_async_mask
;
10254 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
10255 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
10256 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
10257 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
10258 remote_ops
.to_trace_init
= remote_trace_init
;
10259 remote_ops
.to_download_tracepoint
= remote_download_tracepoint
;
10260 remote_ops
.to_download_trace_state_variable
10261 = remote_download_trace_state_variable
;
10262 remote_ops
.to_trace_set_readonly_regions
= remote_trace_set_readonly_regions
;
10263 remote_ops
.to_trace_start
= remote_trace_start
;
10264 remote_ops
.to_get_trace_status
= remote_get_trace_status
;
10265 remote_ops
.to_trace_stop
= remote_trace_stop
;
10266 remote_ops
.to_trace_find
= remote_trace_find
;
10267 remote_ops
.to_get_trace_state_variable_value
10268 = remote_get_trace_state_variable_value
;
10269 remote_ops
.to_save_trace_data
= remote_save_trace_data
;
10270 remote_ops
.to_upload_tracepoints
= remote_upload_tracepoints
;
10271 remote_ops
.to_upload_trace_state_variables
10272 = remote_upload_trace_state_variables
;
10273 remote_ops
.to_get_raw_trace_data
= remote_get_raw_trace_data
;
10274 remote_ops
.to_set_disconnected_tracing
= remote_set_disconnected_tracing
;
10275 remote_ops
.to_set_circular_trace_buffer
= remote_set_circular_trace_buffer
;
10276 remote_ops
.to_core_of_thread
= remote_core_of_thread
;
10277 remote_ops
.to_verify_memory
= remote_verify_memory
;
10278 remote_ops
.to_get_tib_address
= remote_get_tib_address
;
10279 remote_ops
.to_set_permissions
= remote_set_permissions
;
10280 remote_ops
.to_static_tracepoint_marker_at
10281 = remote_static_tracepoint_marker_at
;
10282 remote_ops
.to_static_tracepoint_markers_by_strid
10283 = remote_static_tracepoint_markers_by_strid
;
10286 /* Set up the extended remote vector by making a copy of the standard
10287 remote vector and adding to it. */
10290 init_extended_remote_ops (void)
10292 extended_remote_ops
= remote_ops
;
10294 extended_remote_ops
.to_shortname
= "extended-remote";
10295 extended_remote_ops
.to_longname
=
10296 "Extended remote serial target in gdb-specific protocol";
10297 extended_remote_ops
.to_doc
=
10298 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10299 Specify the serial device it is connected to (e.g. /dev/ttya).";
10300 extended_remote_ops
.to_open
= extended_remote_open
;
10301 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
10302 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
10303 extended_remote_ops
.to_detach
= extended_remote_detach
;
10304 extended_remote_ops
.to_attach
= extended_remote_attach
;
10305 extended_remote_ops
.to_kill
= extended_remote_kill
;
10309 remote_can_async_p (void)
10311 if (!target_async_permitted
)
10312 /* We only enable async when the user specifically asks for it. */
10315 /* We're async whenever the serial device is. */
10316 return remote_async_mask_value
&& serial_can_async_p (remote_desc
);
10320 remote_is_async_p (void)
10322 if (!target_async_permitted
)
10323 /* We only enable async when the user specifically asks for it. */
10326 /* We're async whenever the serial device is. */
10327 return remote_async_mask_value
&& serial_is_async_p (remote_desc
);
10330 /* Pass the SERIAL event on and up to the client. One day this code
10331 will be able to delay notifying the client of an event until the
10332 point where an entire packet has been received. */
10334 static void (*async_client_callback
) (enum inferior_event_type event_type
,
10336 static void *async_client_context
;
10337 static serial_event_ftype remote_async_serial_handler
;
10340 remote_async_serial_handler (struct serial
*scb
, void *context
)
10342 /* Don't propogate error information up to the client. Instead let
10343 the client find out about the error by querying the target. */
10344 async_client_callback (INF_REG_EVENT
, async_client_context
);
10348 remote_async_inferior_event_handler (gdb_client_data data
)
10350 inferior_event_handler (INF_REG_EVENT
, NULL
);
10354 remote_async_get_pending_events_handler (gdb_client_data data
)
10356 remote_get_pending_stop_replies ();
10360 remote_async (void (*callback
) (enum inferior_event_type event_type
,
10361 void *context
), void *context
)
10363 if (remote_async_mask_value
== 0)
10364 internal_error (__FILE__
, __LINE__
,
10365 _("Calling remote_async when async is masked"));
10367 if (callback
!= NULL
)
10369 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
10370 async_client_callback
= callback
;
10371 async_client_context
= context
;
10374 serial_async (remote_desc
, NULL
, NULL
);
10378 remote_async_mask (int new_mask
)
10380 int curr_mask
= remote_async_mask_value
;
10382 remote_async_mask_value
= new_mask
;
10387 set_remote_cmd (char *args
, int from_tty
)
10389 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
10393 show_remote_cmd (char *args
, int from_tty
)
10395 /* We can't just use cmd_show_list here, because we want to skip
10396 the redundant "show remote Z-packet" and the legacy aliases. */
10397 struct cleanup
*showlist_chain
;
10398 struct cmd_list_element
*list
= remote_show_cmdlist
;
10400 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
10401 for (; list
!= NULL
; list
= list
->next
)
10402 if (strcmp (list
->name
, "Z-packet") == 0)
10404 else if (list
->type
== not_set_cmd
)
10405 /* Alias commands are exactly like the original, except they
10406 don't have the normal type. */
10410 struct cleanup
*option_chain
10411 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
10413 ui_out_field_string (uiout
, "name", list
->name
);
10414 ui_out_text (uiout
, ": ");
10415 if (list
->type
== show_cmd
)
10416 do_setshow_command ((char *) NULL
, from_tty
, list
);
10418 cmd_func (list
, NULL
, from_tty
);
10419 /* Close the tuple. */
10420 do_cleanups (option_chain
);
10423 /* Close the tuple. */
10424 do_cleanups (showlist_chain
);
10428 /* Function to be called whenever a new objfile (shlib) is detected. */
10430 remote_new_objfile (struct objfile
*objfile
)
10432 if (remote_desc
!= 0) /* Have a remote connection. */
10433 remote_check_symbols (objfile
);
10436 /* Pull all the tracepoints defined on the target and create local
10437 data structures representing them. We don't want to create real
10438 tracepoints yet, we don't want to mess up the user's existing
10442 remote_upload_tracepoints (struct uploaded_tp
**utpp
)
10444 struct remote_state
*rs
= get_remote_state ();
10447 /* Ask for a first packet of tracepoint definition. */
10449 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10451 while (*p
&& *p
!= 'l')
10453 parse_tracepoint_definition (p
, utpp
);
10454 /* Ask for another packet of tracepoint definition. */
10456 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10463 remote_upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
10465 struct remote_state
*rs
= get_remote_state ();
10468 /* Ask for a first packet of variable definition. */
10470 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10472 while (*p
&& *p
!= 'l')
10474 parse_tsv_definition (p
, utsvp
);
10475 /* Ask for another packet of variable definition. */
10477 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10484 _initialize_remote (void)
10486 struct remote_state
*rs
;
10487 struct cmd_list_element
*cmd
;
10490 /* architecture specific data */
10491 remote_gdbarch_data_handle
=
10492 gdbarch_data_register_post_init (init_remote_state
);
10493 remote_g_packet_data_handle
=
10494 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
10496 /* Initialize the per-target state. At the moment there is only one
10497 of these, not one per target. Only one target is active at a
10498 time. The default buffer size is unimportant; it will be expanded
10499 whenever a larger buffer is needed. */
10500 rs
= get_remote_state_raw ();
10501 rs
->buf_size
= 400;
10502 rs
->buf
= xmalloc (rs
->buf_size
);
10504 init_remote_ops ();
10505 add_target (&remote_ops
);
10507 init_extended_remote_ops ();
10508 add_target (&extended_remote_ops
);
10510 /* Hook into new objfile notification. */
10511 observer_attach_new_objfile (remote_new_objfile
);
10513 /* Set up signal handlers. */
10514 sigint_remote_token
=
10515 create_async_signal_handler (async_remote_interrupt
, NULL
);
10516 sigint_remote_twice_token
=
10517 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
10520 init_remote_threadtests ();
10523 /* set/show remote ... */
10525 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
10526 Remote protocol specific variables\n\
10527 Configure various remote-protocol specific variables such as\n\
10528 the packets being used"),
10529 &remote_set_cmdlist
, "set remote ",
10530 0 /* allow-unknown */, &setlist
);
10531 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
10532 Remote protocol specific variables\n\
10533 Configure various remote-protocol specific variables such as\n\
10534 the packets being used"),
10535 &remote_show_cmdlist
, "show remote ",
10536 0 /* allow-unknown */, &showlist
);
10538 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
10539 Compare section data on target to the exec file.\n\
10540 Argument is a single section name (default: all loaded sections)."),
10543 add_cmd ("packet", class_maintenance
, packet_command
, _("\
10544 Send an arbitrary packet to a remote target.\n\
10545 maintenance packet TEXT\n\
10546 If GDB is talking to an inferior via the GDB serial protocol, then\n\
10547 this command sends the string TEXT to the inferior, and displays the\n\
10548 response packet. GDB supplies the initial `$' character, and the\n\
10549 terminating `#' character and checksum."),
10552 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
10553 Set whether to send break if interrupted."), _("\
10554 Show whether to send break if interrupted."), _("\
10555 If set, a break, instead of a cntrl-c, is sent to the remote target."),
10556 set_remotebreak
, show_remotebreak
,
10557 &setlist
, &showlist
);
10558 cmd_name
= "remotebreak";
10559 cmd
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
10560 deprecate_cmd (cmd
, "set remote interrupt-sequence");
10561 cmd_name
= "remotebreak"; /* needed because lookup_cmd updates the pointer */
10562 cmd
= lookup_cmd (&cmd_name
, showlist
, "", -1, 1);
10563 deprecate_cmd (cmd
, "show remote interrupt-sequence");
10565 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
10566 interrupt_sequence_modes
, &interrupt_sequence_mode
,
10568 Set interrupt sequence to remote target."), _("\
10569 Show interrupt sequence to remote target."), _("\
10570 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
10571 NULL
, show_interrupt_sequence
,
10572 &remote_set_cmdlist
,
10573 &remote_show_cmdlist
);
10575 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
10576 &interrupt_on_connect
, _("\
10577 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
10578 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
10579 If set, interrupt sequence is sent to remote target."),
10581 &remote_set_cmdlist
, &remote_show_cmdlist
);
10583 /* Install commands for configuring memory read/write packets. */
10585 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
10586 Set the maximum number of bytes per memory write packet (deprecated)."),
10588 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
10589 Show the maximum number of bytes per memory write packet (deprecated)."),
10591 add_cmd ("memory-write-packet-size", no_class
,
10592 set_memory_write_packet_size
, _("\
10593 Set the maximum number of bytes per memory-write packet.\n\
10594 Specify the number of bytes in a packet or 0 (zero) for the\n\
10595 default packet size. The actual limit is further reduced\n\
10596 dependent on the target. Specify ``fixed'' to disable the\n\
10597 further restriction and ``limit'' to enable that restriction."),
10598 &remote_set_cmdlist
);
10599 add_cmd ("memory-read-packet-size", no_class
,
10600 set_memory_read_packet_size
, _("\
10601 Set the maximum number of bytes per memory-read packet.\n\
10602 Specify the number of bytes in a packet or 0 (zero) for the\n\
10603 default packet size. The actual limit is further reduced\n\
10604 dependent on the target. Specify ``fixed'' to disable the\n\
10605 further restriction and ``limit'' to enable that restriction."),
10606 &remote_set_cmdlist
);
10607 add_cmd ("memory-write-packet-size", no_class
,
10608 show_memory_write_packet_size
,
10609 _("Show the maximum number of bytes per memory-write packet."),
10610 &remote_show_cmdlist
);
10611 add_cmd ("memory-read-packet-size", no_class
,
10612 show_memory_read_packet_size
,
10613 _("Show the maximum number of bytes per memory-read packet."),
10614 &remote_show_cmdlist
);
10616 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
10617 &remote_hw_watchpoint_limit
, _("\
10618 Set the maximum number of target hardware watchpoints."), _("\
10619 Show the maximum number of target hardware watchpoints."), _("\
10620 Specify a negative limit for unlimited."),
10621 NULL
, NULL
, /* FIXME: i18n: The maximum
10622 number of target hardware
10623 watchpoints is %s. */
10624 &remote_set_cmdlist
, &remote_show_cmdlist
);
10625 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
10626 &remote_hw_breakpoint_limit
, _("\
10627 Set the maximum number of target hardware breakpoints."), _("\
10628 Show the maximum number of target hardware breakpoints."), _("\
10629 Specify a negative limit for unlimited."),
10630 NULL
, NULL
, /* FIXME: i18n: The maximum
10631 number of target hardware
10632 breakpoints is %s. */
10633 &remote_set_cmdlist
, &remote_show_cmdlist
);
10635 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
10636 &remote_address_size
, _("\
10637 Set the maximum size of the address (in bits) in a memory packet."), _("\
10638 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
10640 NULL
, /* FIXME: i18n: */
10641 &setlist
, &showlist
);
10643 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
10644 "X", "binary-download", 1);
10646 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
10647 "vCont", "verbose-resume", 0);
10649 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
10650 "QPassSignals", "pass-signals", 0);
10652 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
10653 "qSymbol", "symbol-lookup", 0);
10655 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
10656 "P", "set-register", 1);
10658 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
10659 "p", "fetch-register", 1);
10661 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
10662 "Z0", "software-breakpoint", 0);
10664 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
10665 "Z1", "hardware-breakpoint", 0);
10667 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
10668 "Z2", "write-watchpoint", 0);
10670 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
10671 "Z3", "read-watchpoint", 0);
10673 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
10674 "Z4", "access-watchpoint", 0);
10676 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
10677 "qXfer:auxv:read", "read-aux-vector", 0);
10679 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
10680 "qXfer:features:read", "target-features", 0);
10682 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
10683 "qXfer:libraries:read", "library-info", 0);
10685 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
10686 "qXfer:memory-map:read", "memory-map", 0);
10688 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
10689 "qXfer:spu:read", "read-spu-object", 0);
10691 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
10692 "qXfer:spu:write", "write-spu-object", 0);
10694 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
10695 "qXfer:osdata:read", "osdata", 0);
10697 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_threads
],
10698 "qXfer:threads:read", "threads", 0);
10700 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_read
],
10701 "qXfer:siginfo:read", "read-siginfo-object", 0);
10703 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_write
],
10704 "qXfer:siginfo:write", "write-siginfo-object", 0);
10706 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
10707 "qGetTLSAddr", "get-thread-local-storage-address",
10710 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTIBAddr
],
10711 "qGetTIBAddr", "get-thread-information-block-address",
10714 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bc
],
10715 "bc", "reverse-continue", 0);
10717 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bs
],
10718 "bs", "reverse-step", 0);
10720 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
10721 "qSupported", "supported-packets", 0);
10723 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
10724 "qSearch:memory", "search-memory", 0);
10726 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
10727 "vFile:open", "hostio-open", 0);
10729 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
10730 "vFile:pread", "hostio-pread", 0);
10732 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
10733 "vFile:pwrite", "hostio-pwrite", 0);
10735 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
10736 "vFile:close", "hostio-close", 0);
10738 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
10739 "vFile:unlink", "hostio-unlink", 0);
10741 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
10742 "vAttach", "attach", 0);
10744 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
10747 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
10748 "QStartNoAckMode", "noack", 0);
10750 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
10751 "vKill", "kill", 0);
10753 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qAttached
],
10754 "qAttached", "query-attached", 0);
10756 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalTracepoints
],
10757 "ConditionalTracepoints",
10758 "conditional-tracepoints", 0);
10759 add_packet_config_cmd (&remote_protocol_packets
[PACKET_FastTracepoints
],
10760 "FastTracepoints", "fast-tracepoints", 0);
10762 add_packet_config_cmd (&remote_protocol_packets
[PACKET_TracepointSource
],
10763 "TracepointSource", "TracepointSource", 0);
10765 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAllow
],
10766 "QAllow", "allow", 0);
10768 add_packet_config_cmd (&remote_protocol_packets
[PACKET_StaticTracepoints
],
10769 "StaticTracepoints", "static-tracepoints", 0);
10771 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_statictrace_read
],
10772 "qXfer:statictrace:read", "read-sdata-object", 0);
10774 /* Keep the old ``set remote Z-packet ...'' working. Each individual
10775 Z sub-packet has its own set and show commands, but users may
10776 have sets to this variable in their .gdbinit files (or in their
10778 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
10779 &remote_Z_packet_detect
, _("\
10780 Set use of remote protocol `Z' packets"), _("\
10781 Show use of remote protocol `Z' packets "), _("\
10782 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
10784 set_remote_protocol_Z_packet_cmd
,
10785 show_remote_protocol_Z_packet_cmd
,
10786 /* FIXME: i18n: Use of remote protocol
10787 `Z' packets is %s. */
10788 &remote_set_cmdlist
, &remote_show_cmdlist
);
10790 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
10791 Manipulate files on the remote system\n\
10792 Transfer files to and from the remote target system."),
10793 &remote_cmdlist
, "remote ",
10794 0 /* allow-unknown */, &cmdlist
);
10796 add_cmd ("put", class_files
, remote_put_command
,
10797 _("Copy a local file to the remote system."),
10800 add_cmd ("get", class_files
, remote_get_command
,
10801 _("Copy a remote file to the local system."),
10804 add_cmd ("delete", class_files
, remote_delete_command
,
10805 _("Delete a remote file."),
10808 remote_exec_file
= xstrdup ("");
10809 add_setshow_string_noescape_cmd ("exec-file", class_files
,
10810 &remote_exec_file
, _("\
10811 Set the remote pathname for \"run\""), _("\
10812 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
10813 &remote_set_cmdlist
, &remote_show_cmdlist
);
10815 /* Eventually initialize fileio. See fileio.c */
10816 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
10818 /* Take advantage of the fact that the LWP field is not used, to tag
10819 special ptids with it set to != 0. */
10820 magic_null_ptid
= ptid_build (42000, 1, -1);
10821 not_sent_ptid
= ptid_build (42000, 1, -2);
10822 any_thread_ptid
= ptid_build (42000, 1, 0);
10824 target_buf_size
= 2048;
10825 target_buf
= xmalloc (target_buf_size
);