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 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 is auto-detected, currently %s.\n"),
1046 config
->name
, support
);
1048 case AUTO_BOOLEAN_TRUE
:
1049 case AUTO_BOOLEAN_FALSE
:
1050 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1051 config
->name
, support
);
1057 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
1058 const char *title
, int legacy
)
1064 config
->name
= name
;
1065 config
->title
= title
;
1066 config
->detect
= AUTO_BOOLEAN_AUTO
;
1067 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1068 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1070 show_doc
= xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
1072 /* set/show TITLE-packet {auto,on,off} */
1073 cmd_name
= xstrprintf ("%s-packet", title
);
1074 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
1075 &config
->detect
, set_doc
, show_doc
, NULL
, /* help_doc */
1076 set_remote_protocol_packet_cmd
,
1077 show_remote_protocol_packet_cmd
,
1078 &remote_set_cmdlist
, &remote_show_cmdlist
);
1079 /* The command code copies the documentation strings. */
1082 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1087 legacy_name
= xstrprintf ("%s-packet", name
);
1088 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1089 &remote_set_cmdlist
);
1090 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1091 &remote_show_cmdlist
);
1095 static enum packet_result
1096 packet_check_result (const char *buf
)
1100 /* The stub recognized the packet request. Check that the
1101 operation succeeded. */
1103 && isxdigit (buf
[1]) && isxdigit (buf
[2])
1105 /* "Enn" - definitly an error. */
1106 return PACKET_ERROR
;
1108 /* Always treat "E." as an error. This will be used for
1109 more verbose error messages, such as E.memtypes. */
1110 if (buf
[0] == 'E' && buf
[1] == '.')
1111 return PACKET_ERROR
;
1113 /* The packet may or may not be OK. Just assume it is. */
1117 /* The stub does not support the packet. */
1118 return PACKET_UNKNOWN
;
1121 static enum packet_result
1122 packet_ok (const char *buf
, struct packet_config
*config
)
1124 enum packet_result result
;
1126 result
= packet_check_result (buf
);
1131 /* The stub recognized the packet request. */
1132 switch (config
->support
)
1134 case PACKET_SUPPORT_UNKNOWN
:
1136 fprintf_unfiltered (gdb_stdlog
,
1137 "Packet %s (%s) is supported\n",
1138 config
->name
, config
->title
);
1139 config
->support
= PACKET_ENABLE
;
1141 case PACKET_DISABLE
:
1142 internal_error (__FILE__
, __LINE__
,
1143 _("packet_ok: attempt to use a disabled packet"));
1149 case PACKET_UNKNOWN
:
1150 /* The stub does not support the packet. */
1151 switch (config
->support
)
1154 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
1155 /* If the stub previously indicated that the packet was
1156 supported then there is a protocol error.. */
1157 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1158 config
->name
, config
->title
);
1160 /* The user set it wrong. */
1161 error (_("Enabled packet %s (%s) not recognized by stub"),
1162 config
->name
, config
->title
);
1164 case PACKET_SUPPORT_UNKNOWN
:
1166 fprintf_unfiltered (gdb_stdlog
,
1167 "Packet %s (%s) is NOT supported\n",
1168 config
->name
, config
->title
);
1169 config
->support
= PACKET_DISABLE
;
1171 case PACKET_DISABLE
:
1193 PACKET_vFile_pwrite
,
1195 PACKET_vFile_unlink
,
1197 PACKET_qXfer_features
,
1198 PACKET_qXfer_libraries
,
1199 PACKET_qXfer_memory_map
,
1200 PACKET_qXfer_spu_read
,
1201 PACKET_qXfer_spu_write
,
1202 PACKET_qXfer_osdata
,
1203 PACKET_qXfer_threads
,
1204 PACKET_qXfer_statictrace_read
,
1208 PACKET_QPassSignals
,
1209 PACKET_qSearch_memory
,
1212 PACKET_QStartNoAckMode
,
1214 PACKET_qXfer_siginfo_read
,
1215 PACKET_qXfer_siginfo_write
,
1217 PACKET_ConditionalTracepoints
,
1218 PACKET_FastTracepoints
,
1219 PACKET_StaticTracepoints
,
1222 PACKET_TracepointSource
,
1227 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
1230 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
1231 struct cmd_list_element
*c
)
1233 struct packet_config
*packet
;
1235 for (packet
= remote_protocol_packets
;
1236 packet
< &remote_protocol_packets
[PACKET_MAX
];
1239 if (&packet
->detect
== c
->var
)
1241 update_packet_config (packet
);
1245 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1250 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
1251 struct cmd_list_element
*c
,
1254 struct packet_config
*packet
;
1256 for (packet
= remote_protocol_packets
;
1257 packet
< &remote_protocol_packets
[PACKET_MAX
];
1260 if (&packet
->detect
== c
->var
)
1262 show_packet_config_cmd (packet
);
1266 internal_error (__FILE__
, __LINE__
, "Could not find config for %s",
1270 /* Should we try one of the 'Z' requests? */
1274 Z_PACKET_SOFTWARE_BP
,
1275 Z_PACKET_HARDWARE_BP
,
1282 /* For compatibility with older distributions. Provide a ``set remote
1283 Z-packet ...'' command that updates all the Z packet types. */
1285 static enum auto_boolean remote_Z_packet_detect
;
1288 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1289 struct cmd_list_element
*c
)
1293 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1295 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1296 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1301 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1302 struct cmd_list_element
*c
,
1307 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1309 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1313 /* Should we try the 'ThreadInfo' query packet?
1315 This variable (NOT available to the user: auto-detect only!)
1316 determines whether GDB will use the new, simpler "ThreadInfo"
1317 query or the older, more complex syntax for thread queries.
1318 This is an auto-detect variable (set to true at each connect,
1319 and set to false when the target fails to recognize it). */
1321 static int use_threadinfo_query
;
1322 static int use_threadextra_query
;
1324 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1325 static struct async_signal_handler
*sigint_remote_twice_token
;
1326 static struct async_signal_handler
*sigint_remote_token
;
1329 /* Asynchronous signal handle registered as event loop source for
1330 when we have pending events ready to be passed to the core. */
1332 static struct async_event_handler
*remote_async_inferior_event_token
;
1334 /* Asynchronous signal handle registered as event loop source for when
1335 the remote sent us a %Stop notification. The registered callback
1336 will do a vStopped sequence to pull the rest of the events out of
1337 the remote side into our event queue. */
1339 static struct async_event_handler
*remote_async_get_pending_events_token
;
1342 static ptid_t magic_null_ptid
;
1343 static ptid_t not_sent_ptid
;
1344 static ptid_t any_thread_ptid
;
1346 /* These are the threads which we last sent to the remote system. The
1347 TID member will be -1 for all or -2 for not sent yet. */
1349 static ptid_t general_thread
;
1350 static ptid_t continue_thread
;
1352 /* Find out if the stub attached to PID (and hence GDB should offer to
1353 detach instead of killing it when bailing out). */
1356 remote_query_attached (int pid
)
1358 struct remote_state
*rs
= get_remote_state ();
1360 if (remote_protocol_packets
[PACKET_qAttached
].support
== PACKET_DISABLE
)
1363 if (remote_multi_process_p (rs
))
1364 sprintf (rs
->buf
, "qAttached:%x", pid
);
1366 sprintf (rs
->buf
, "qAttached");
1369 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1371 switch (packet_ok (rs
->buf
,
1372 &remote_protocol_packets
[PACKET_qAttached
]))
1375 if (strcmp (rs
->buf
, "1") == 0)
1379 warning (_("Remote failure reply: %s"), rs
->buf
);
1381 case PACKET_UNKNOWN
:
1388 /* Add PID to GDB's inferior table. Since we can be connected to a
1389 remote system before before knowing about any inferior, mark the
1390 target with execution when we find the first inferior. If ATTACHED
1391 is 1, then we had just attached to this inferior. If it is 0, then
1392 we just created this inferior. If it is -1, then try querying the
1393 remote stub to find out if it had attached to the inferior or
1396 static struct inferior
*
1397 remote_add_inferior (int pid
, int attached
)
1399 struct inferior
*inf
;
1401 /* Check whether this process we're learning about is to be
1402 considered attached, or if is to be considered to have been
1403 spawned by the stub. */
1405 attached
= remote_query_attached (pid
);
1407 if (gdbarch_has_global_solist (target_gdbarch
))
1409 /* If the target shares code across all inferiors, then every
1410 attach adds a new inferior. */
1411 inf
= add_inferior (pid
);
1413 /* ... and every inferior is bound to the same program space.
1414 However, each inferior may still have its own address
1416 inf
->aspace
= maybe_new_address_space ();
1417 inf
->pspace
= current_program_space
;
1421 /* In the traditional debugging scenario, there's a 1-1 match
1422 between program/address spaces. We simply bind the inferior
1423 to the program space's address space. */
1424 inf
= current_inferior ();
1425 inferior_appeared (inf
, pid
);
1428 inf
->attach_flag
= attached
;
1433 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1434 according to RUNNING. */
1437 remote_add_thread (ptid_t ptid
, int running
)
1441 set_executing (ptid
, running
);
1442 set_running (ptid
, running
);
1445 /* Come here when we learn about a thread id from the remote target.
1446 It may be the first time we hear about such thread, so take the
1447 opportunity to add it to GDB's thread list. In case this is the
1448 first time we're noticing its corresponding inferior, add it to
1449 GDB's inferior list as well. */
1452 remote_notice_new_inferior (ptid_t currthread
, int running
)
1454 /* If this is a new thread, add it to GDB's thread list.
1455 If we leave it up to WFI to do this, bad things will happen. */
1457 if (in_thread_list (currthread
) && is_exited (currthread
))
1459 /* We're seeing an event on a thread id we knew had exited.
1460 This has to be a new thread reusing the old id. Add it. */
1461 remote_add_thread (currthread
, running
);
1465 if (!in_thread_list (currthread
))
1467 struct inferior
*inf
= NULL
;
1468 int pid
= ptid_get_pid (currthread
);
1470 if (ptid_is_pid (inferior_ptid
)
1471 && pid
== ptid_get_pid (inferior_ptid
))
1473 /* inferior_ptid has no thread member yet. This can happen
1474 with the vAttach -> remote_wait,"TAAthread:" path if the
1475 stub doesn't support qC. This is the first stop reported
1476 after an attach, so this is the main thread. Update the
1477 ptid in the thread list. */
1478 if (in_thread_list (pid_to_ptid (pid
)))
1479 thread_change_ptid (inferior_ptid
, currthread
);
1482 remote_add_thread (currthread
, running
);
1483 inferior_ptid
= currthread
;
1488 if (ptid_equal (magic_null_ptid
, inferior_ptid
))
1490 /* inferior_ptid is not set yet. This can happen with the
1491 vRun -> remote_wait,"TAAthread:" path if the stub
1492 doesn't support qC. This is the first stop reported
1493 after an attach, so this is the main thread. Update the
1494 ptid in the thread list. */
1495 thread_change_ptid (inferior_ptid
, currthread
);
1499 /* When connecting to a target remote, or to a target
1500 extended-remote which already was debugging an inferior, we
1501 may not know about it yet. Add it before adding its child
1502 thread, so notifications are emitted in a sensible order. */
1503 if (!in_inferior_list (ptid_get_pid (currthread
)))
1504 inf
= remote_add_inferior (ptid_get_pid (currthread
), -1);
1506 /* This is really a new thread. Add it. */
1507 remote_add_thread (currthread
, running
);
1509 /* If we found a new inferior, let the common code do whatever
1510 it needs to with it (e.g., read shared libraries, insert
1513 notice_new_inferior (currthread
, running
, 0);
1517 /* Return the private thread data, creating it if necessary. */
1519 struct private_thread_info
*
1520 demand_private_info (ptid_t ptid
)
1522 struct thread_info
*info
= find_thread_ptid (ptid
);
1528 info
->private = xmalloc (sizeof (*(info
->private)));
1529 info
->private_dtor
= free_private_thread_info
;
1530 info
->private->core
= -1;
1531 info
->private->extra
= 0;
1534 return info
->private;
1537 /* Call this function as a result of
1538 1) A halt indication (T packet) containing a thread id
1539 2) A direct query of currthread
1540 3) Successful execution of set thread
1544 record_currthread (ptid_t currthread
)
1546 general_thread
= currthread
;
1549 static char *last_pass_packet
;
1551 /* If 'QPassSignals' is supported, tell the remote stub what signals
1552 it can simply pass through to the inferior without reporting. */
1555 remote_pass_signals (void)
1557 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1559 char *pass_packet
, *p
;
1560 int numsigs
= (int) TARGET_SIGNAL_LAST
;
1563 gdb_assert (numsigs
< 256);
1564 for (i
= 0; i
< numsigs
; i
++)
1566 if (signal_stop_state (i
) == 0
1567 && signal_print_state (i
) == 0
1568 && signal_pass_state (i
) == 1)
1571 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1572 strcpy (pass_packet
, "QPassSignals:");
1573 p
= pass_packet
+ strlen (pass_packet
);
1574 for (i
= 0; i
< numsigs
; i
++)
1576 if (signal_stop_state (i
) == 0
1577 && signal_print_state (i
) == 0
1578 && signal_pass_state (i
) == 1)
1581 *p
++ = tohex (i
>> 4);
1582 *p
++ = tohex (i
& 15);
1591 if (!last_pass_packet
|| strcmp (last_pass_packet
, pass_packet
))
1593 struct remote_state
*rs
= get_remote_state ();
1594 char *buf
= rs
->buf
;
1596 putpkt (pass_packet
);
1597 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1598 packet_ok (buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1599 if (last_pass_packet
)
1600 xfree (last_pass_packet
);
1601 last_pass_packet
= pass_packet
;
1604 xfree (pass_packet
);
1609 remote_notice_signals (ptid_t ptid
)
1611 /* Update the remote on signals to silently pass, if they've
1613 remote_pass_signals ();
1616 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1617 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1618 thread. If GEN is set, set the general thread, if not, then set
1619 the step/continue thread. */
1621 set_thread (struct ptid ptid
, int gen
)
1623 struct remote_state
*rs
= get_remote_state ();
1624 ptid_t state
= gen
? general_thread
: continue_thread
;
1625 char *buf
= rs
->buf
;
1626 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1628 if (ptid_equal (state
, ptid
))
1632 *buf
++ = gen
? 'g' : 'c';
1633 if (ptid_equal (ptid
, magic_null_ptid
))
1634 xsnprintf (buf
, endbuf
- buf
, "0");
1635 else if (ptid_equal (ptid
, any_thread_ptid
))
1636 xsnprintf (buf
, endbuf
- buf
, "0");
1637 else if (ptid_equal (ptid
, minus_one_ptid
))
1638 xsnprintf (buf
, endbuf
- buf
, "-1");
1640 write_ptid (buf
, endbuf
, ptid
);
1642 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1644 general_thread
= ptid
;
1646 continue_thread
= ptid
;
1650 set_general_thread (struct ptid ptid
)
1652 set_thread (ptid
, 1);
1656 set_continue_thread (struct ptid ptid
)
1658 set_thread (ptid
, 0);
1661 /* Change the remote current process. Which thread within the process
1662 ends up selected isn't important, as long as it is the same process
1663 as what INFERIOR_PTID points to.
1665 This comes from that fact that there is no explicit notion of
1666 "selected process" in the protocol. The selected process for
1667 general operations is the process the selected general thread
1671 set_general_process (void)
1673 struct remote_state
*rs
= get_remote_state ();
1675 /* If the remote can't handle multiple processes, don't bother. */
1676 if (!remote_multi_process_p (rs
))
1679 /* We only need to change the remote current thread if it's pointing
1680 at some other process. */
1681 if (ptid_get_pid (general_thread
) != ptid_get_pid (inferior_ptid
))
1682 set_general_thread (inferior_ptid
);
1686 /* Return nonzero if the thread PTID is still alive on the remote
1690 remote_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1692 struct remote_state
*rs
= get_remote_state ();
1695 if (ptid_equal (ptid
, magic_null_ptid
))
1696 /* The main thread is always alive. */
1699 if (ptid_get_pid (ptid
) != 0 && ptid_get_tid (ptid
) == 0)
1700 /* The main thread is always alive. This can happen after a
1701 vAttach, if the remote side doesn't support
1706 endp
= rs
->buf
+ get_remote_packet_size ();
1709 write_ptid (p
, endp
, ptid
);
1712 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1713 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1716 /* About these extended threadlist and threadinfo packets. They are
1717 variable length packets but, the fields within them are often fixed
1718 length. They are redundent enough to send over UDP as is the
1719 remote protocol in general. There is a matching unit test module
1722 #define OPAQUETHREADBYTES 8
1724 /* a 64 bit opaque identifier */
1725 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1727 /* WARNING: This threadref data structure comes from the remote O.S.,
1728 libstub protocol encoding, and remote.c. it is not particularly
1731 /* Right now, the internal structure is int. We want it to be bigger.
1735 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1737 /* gdb_ext_thread_info is an internal GDB data structure which is
1738 equivalent to the reply of the remote threadinfo packet. */
1740 struct gdb_ext_thread_info
1742 threadref threadid
; /* External form of thread reference. */
1743 int active
; /* Has state interesting to GDB?
1745 char display
[256]; /* Brief state display, name,
1746 blocked/suspended. */
1747 char shortname
[32]; /* To be used to name threads. */
1748 char more_display
[256]; /* Long info, statistics, queue depth,
1752 /* The volume of remote transfers can be limited by submitting
1753 a mask containing bits specifying the desired information.
1754 Use a union of these values as the 'selection' parameter to
1755 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) >= get_remote_packet_size ())
2349 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
2351 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
2353 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2355 if (*rs
->buf
== '\0')
2359 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
2362 if (!threadmatch (&echo_nextthread
, nextthread
))
2364 /* FIXME: This is a good reason to drop the packet. */
2365 /* Possably, there is a duplicate response. */
2367 retransmit immediatly - race conditions
2368 retransmit after timeout - yes
2370 wait for packet, then exit
2372 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2373 return 0; /* I choose simply exiting. */
2375 if (*result_count
<= 0)
2379 warning (_("RMT ERROR : failed to get remote thread list."));
2382 return result
; /* break; */
2384 if (*result_count
> result_limit
)
2387 warning (_("RMT ERROR: threadlist response longer than requested."));
2393 /* This is the interface between remote and threads, remotes upper
2396 /* remote_find_new_threads retrieves the thread list and for each
2397 thread in the list, looks up the thread in GDB's internal list,
2398 adding the thread if it does not already exist. This involves
2399 getting partial thread lists from the remote target so, polling the
2400 quit_flag is required. */
2403 /* About this many threadisds fit in a packet. */
2405 #define MAXTHREADLISTRESULTS 32
2408 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2411 int done
, i
, result_count
;
2415 static threadref nextthread
;
2416 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
2421 if (loopcount
++ > looplimit
)
2424 warning (_("Remote fetch threadlist -infinite loop-."));
2427 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
2428 &done
, &result_count
, resultthreadlist
))
2433 /* Clear for later iterations. */
2435 /* Setup to resume next batch of thread references, set nextthread. */
2436 if (result_count
>= 1)
2437 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
2439 while (result_count
--)
2440 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
2447 remote_newthread_step (threadref
*ref
, void *context
)
2449 int pid
= ptid_get_pid (inferior_ptid
);
2450 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2452 if (!in_thread_list (ptid
))
2454 return 1; /* continue iterator */
2457 #define CRAZY_MAX_THREADS 1000
2460 remote_current_thread (ptid_t oldpid
)
2462 struct remote_state
*rs
= get_remote_state ();
2465 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2466 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2467 return read_ptid (&rs
->buf
[2], NULL
);
2472 /* Find new threads for info threads command.
2473 * Original version, using John Metzler's thread protocol.
2477 remote_find_new_threads (void)
2479 remote_threadlist_iterator (remote_newthread_step
, 0,
2483 #if defined(HAVE_LIBEXPAT)
2485 typedef struct thread_item
2491 DEF_VEC_O(thread_item_t
);
2493 struct threads_parsing_context
2495 VEC (thread_item_t
) *items
;
2499 start_thread (struct gdb_xml_parser
*parser
,
2500 const struct gdb_xml_element
*element
,
2501 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
2503 struct threads_parsing_context
*data
= user_data
;
2505 struct thread_item item
;
2508 id
= VEC_index (gdb_xml_value_s
, attributes
, 0)->value
;
2509 item
.ptid
= read_ptid (id
, NULL
);
2511 if (VEC_length (gdb_xml_value_s
, attributes
) > 1)
2512 item
.core
= *(ULONGEST
*) VEC_index (gdb_xml_value_s
, attributes
, 1)->value
;
2518 VEC_safe_push (thread_item_t
, data
->items
, &item
);
2522 end_thread (struct gdb_xml_parser
*parser
,
2523 const struct gdb_xml_element
*element
,
2524 void *user_data
, const char *body_text
)
2526 struct threads_parsing_context
*data
= user_data
;
2528 if (body_text
&& *body_text
)
2529 VEC_last (thread_item_t
, data
->items
)->extra
= xstrdup (body_text
);
2532 const struct gdb_xml_attribute thread_attributes
[] = {
2533 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
2534 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
2535 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
2538 const struct gdb_xml_element thread_children
[] = {
2539 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2542 const struct gdb_xml_element threads_children
[] = {
2543 { "thread", thread_attributes
, thread_children
,
2544 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
2545 start_thread
, end_thread
},
2546 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2549 const struct gdb_xml_element threads_elements
[] = {
2550 { "threads", NULL
, threads_children
,
2551 GDB_XML_EF_NONE
, NULL
, NULL
},
2552 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2555 /* Discard the contents of the constructed thread info context. */
2558 clear_threads_parsing_context (void *p
)
2560 struct threads_parsing_context
*context
= p
;
2562 struct thread_item
*item
;
2564 for (i
= 0; VEC_iterate (thread_item_t
, context
->items
, i
, item
); ++i
)
2565 xfree (item
->extra
);
2567 VEC_free (thread_item_t
, context
->items
);
2573 * Find all threads for info threads command.
2574 * Uses new thread protocol contributed by Cisco.
2575 * Falls back and attempts to use the older method (above)
2576 * if the target doesn't respond to the new method.
2580 remote_threads_info (struct target_ops
*ops
)
2582 struct remote_state
*rs
= get_remote_state ();
2586 if (remote_desc
== 0) /* paranoia */
2587 error (_("Command can only be used when connected to the remote target."));
2589 #if defined(HAVE_LIBEXPAT)
2590 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2592 char *xml
= target_read_stralloc (¤t_target
,
2593 TARGET_OBJECT_THREADS
, NULL
);
2595 struct cleanup
*back_to
= make_cleanup (xfree
, xml
);
2598 struct gdb_xml_parser
*parser
;
2599 struct threads_parsing_context context
;
2600 struct cleanup
*clear_parsing_context
;
2603 /* Note: this parser cleanup is already guarded by BACK_TO
2605 parser
= gdb_xml_create_parser_and_cleanup (_("threads"),
2609 gdb_xml_use_dtd (parser
, "threads.dtd");
2611 clear_parsing_context
2612 = make_cleanup (clear_threads_parsing_context
, &context
);
2614 if (gdb_xml_parse (parser
, xml
) == 0)
2617 struct thread_item
*item
;
2619 for (i
= 0; VEC_iterate (thread_item_t
, context
.items
, i
, item
); ++i
)
2621 if (!ptid_equal (item
->ptid
, null_ptid
))
2623 struct private_thread_info
*info
;
2624 /* In non-stop mode, we assume new found threads
2625 are running until proven otherwise with a
2626 stop reply. In all-stop, we can only get
2627 here if all threads are stopped. */
2628 int running
= non_stop
? 1 : 0;
2630 remote_notice_new_inferior (item
->ptid
, running
);
2632 info
= demand_private_info (item
->ptid
);
2633 info
->core
= item
->core
;
2634 info
->extra
= item
->extra
;
2640 do_cleanups (clear_parsing_context
);
2643 do_cleanups (back_to
);
2648 if (use_threadinfo_query
)
2650 putpkt ("qfThreadInfo");
2651 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2653 if (bufp
[0] != '\0') /* q packet recognized */
2655 while (*bufp
++ == 'm') /* reply contains one or more TID */
2659 new_thread
= read_ptid (bufp
, &bufp
);
2660 if (!ptid_equal (new_thread
, null_ptid
))
2662 /* In non-stop mode, we assume new found threads
2663 are running until proven otherwise with a
2664 stop reply. In all-stop, we can only get
2665 here if all threads are stopped. */
2666 int running
= non_stop
? 1 : 0;
2668 remote_notice_new_inferior (new_thread
, running
);
2671 while (*bufp
++ == ','); /* comma-separated list */
2672 putpkt ("qsThreadInfo");
2673 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2680 /* Only qfThreadInfo is supported in non-stop mode. */
2684 /* Else fall back to old method based on jmetzler protocol. */
2685 use_threadinfo_query
= 0;
2686 remote_find_new_threads ();
2691 * Collect a descriptive string about the given thread.
2692 * The target may say anything it wants to about the thread
2693 * (typically info about its blocked / runnable state, name, etc.).
2694 * This string will appear in the info threads display.
2696 * Optional: targets are not required to implement this function.
2700 remote_threads_extra_info (struct thread_info
*tp
)
2702 struct remote_state
*rs
= get_remote_state ();
2706 struct gdb_ext_thread_info threadinfo
;
2707 static char display_buf
[100]; /* arbitrary... */
2708 int n
= 0; /* position in display_buf */
2710 if (remote_desc
== 0) /* paranoia */
2711 internal_error (__FILE__
, __LINE__
,
2712 _("remote_threads_extra_info"));
2714 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2715 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2716 /* This is the main thread which was added by GDB. The remote
2717 server doesn't know about it. */
2720 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2722 struct thread_info
*info
= find_thread_ptid (tp
->ptid
);
2724 if (info
&& info
->private)
2725 return info
->private->extra
;
2730 if (use_threadextra_query
)
2733 char *endb
= rs
->buf
+ get_remote_packet_size ();
2735 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2737 write_ptid (b
, endb
, tp
->ptid
);
2740 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2741 if (rs
->buf
[0] != 0)
2743 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2744 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2745 display_buf
[result
] = '\0';
2750 /* If the above query fails, fall back to the old method. */
2751 use_threadextra_query
= 0;
2752 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2753 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2754 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2755 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2756 if (threadinfo
.active
)
2758 if (*threadinfo
.shortname
)
2759 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2760 " Name: %s,", threadinfo
.shortname
);
2761 if (*threadinfo
.display
)
2762 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2763 " State: %s,", threadinfo
.display
);
2764 if (*threadinfo
.more_display
)
2765 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2766 " Priority: %s", threadinfo
.more_display
);
2770 /* For purely cosmetic reasons, clear up trailing commas. */
2771 if (',' == display_buf
[n
-1])
2772 display_buf
[n
-1] = ' ';
2781 remote_static_tracepoint_marker_at (CORE_ADDR addr
,
2782 struct static_tracepoint_marker
*marker
)
2784 struct remote_state
*rs
= get_remote_state ();
2787 sprintf (p
, "qTSTMat:");
2789 p
+= hexnumstr (p
, addr
);
2791 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2795 error (_("Remote failure reply: %s"), p
);
2799 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2807 free_current_marker (void *arg
)
2809 struct static_tracepoint_marker
**marker_p
= arg
;
2811 if (*marker_p
!= NULL
)
2813 release_static_tracepoint_marker (*marker_p
);
2820 static VEC(static_tracepoint_marker_p
) *
2821 remote_static_tracepoint_markers_by_strid (const char *strid
)
2823 struct remote_state
*rs
= get_remote_state ();
2824 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
2825 struct static_tracepoint_marker
*marker
= NULL
;
2826 struct cleanup
*old_chain
;
2829 /* Ask for a first packet of static tracepoint marker
2832 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2835 error (_("Remote failure reply: %s"), p
);
2837 old_chain
= make_cleanup (free_current_marker
, &marker
);
2842 marker
= XCNEW (struct static_tracepoint_marker
);
2846 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2848 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
2850 VEC_safe_push (static_tracepoint_marker_p
,
2856 release_static_tracepoint_marker (marker
);
2857 memset (marker
, 0, sizeof (*marker
));
2860 while (*p
++ == ','); /* comma-separated list */
2861 /* Ask for another packet of static tracepoint definition. */
2863 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2867 do_cleanups (old_chain
);
2872 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2875 remote_get_ada_task_ptid (long lwp
, long thread
)
2877 return ptid_build (ptid_get_pid (inferior_ptid
), 0, lwp
);
2881 /* Restart the remote side; this is an extended protocol operation. */
2884 extended_remote_restart (void)
2886 struct remote_state
*rs
= get_remote_state ();
2888 /* Send the restart command; for reasons I don't understand the
2889 remote side really expects a number after the "R". */
2890 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2893 remote_fileio_reset ();
2896 /* Clean up connection to a remote debugger. */
2899 remote_close (int quitting
)
2901 if (remote_desc
== NULL
)
2902 return; /* already closed */
2904 /* Make sure we leave stdin registered in the event loop, and we
2905 don't leave the async SIGINT signal handler installed. */
2906 remote_terminal_ours ();
2908 serial_close (remote_desc
);
2911 /* We don't have a connection to the remote stub anymore. Get rid
2912 of all the inferiors and their threads we were controlling. */
2913 discard_all_inferiors ();
2914 inferior_ptid
= null_ptid
;
2916 /* We're no longer interested in any of these events. */
2917 discard_pending_stop_replies (-1);
2919 if (remote_async_inferior_event_token
)
2920 delete_async_event_handler (&remote_async_inferior_event_token
);
2921 if (remote_async_get_pending_events_token
)
2922 delete_async_event_handler (&remote_async_get_pending_events_token
);
2925 /* Query the remote side for the text, data and bss offsets. */
2930 struct remote_state
*rs
= get_remote_state ();
2933 int lose
, num_segments
= 0, do_sections
, do_segments
;
2934 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
2935 struct section_offsets
*offs
;
2936 struct symfile_segment_data
*data
;
2938 if (symfile_objfile
== NULL
)
2941 putpkt ("qOffsets");
2942 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2945 if (buf
[0] == '\000')
2946 return; /* Return silently. Stub doesn't support
2950 warning (_("Remote failure reply: %s"), buf
);
2954 /* Pick up each field in turn. This used to be done with scanf, but
2955 scanf will make trouble if CORE_ADDR size doesn't match
2956 conversion directives correctly. The following code will work
2957 with any size of CORE_ADDR. */
2958 text_addr
= data_addr
= bss_addr
= 0;
2962 if (strncmp (ptr
, "Text=", 5) == 0)
2965 /* Don't use strtol, could lose on big values. */
2966 while (*ptr
&& *ptr
!= ';')
2967 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2969 if (strncmp (ptr
, ";Data=", 6) == 0)
2972 while (*ptr
&& *ptr
!= ';')
2973 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
2978 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
2981 while (*ptr
&& *ptr
!= ';')
2982 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
2984 if (bss_addr
!= data_addr
)
2985 warning (_("Target reported unsupported offsets: %s"), buf
);
2990 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
2993 /* Don't use strtol, could lose on big values. */
2994 while (*ptr
&& *ptr
!= ';')
2995 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
2998 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
3001 while (*ptr
&& *ptr
!= ';')
3002 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
3010 error (_("Malformed response to offset query, %s"), buf
);
3011 else if (*ptr
!= '\0')
3012 warning (_("Target reported unsupported offsets: %s"), buf
);
3014 offs
= ((struct section_offsets
*)
3015 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
3016 memcpy (offs
, symfile_objfile
->section_offsets
,
3017 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
3019 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
3020 do_segments
= (data
!= NULL
);
3021 do_sections
= num_segments
== 0;
3023 if (num_segments
> 0)
3025 segments
[0] = text_addr
;
3026 segments
[1] = data_addr
;
3028 /* If we have two segments, we can still try to relocate everything
3029 by assuming that the .text and .data offsets apply to the whole
3030 text and data segments. Convert the offsets given in the packet
3031 to base addresses for symfile_map_offsets_to_segments. */
3032 else if (data
&& data
->num_segments
== 2)
3034 segments
[0] = data
->segment_bases
[0] + text_addr
;
3035 segments
[1] = data
->segment_bases
[1] + data_addr
;
3038 /* If the object file has only one segment, assume that it is text
3039 rather than data; main programs with no writable data are rare,
3040 but programs with no code are useless. Of course the code might
3041 have ended up in the data segment... to detect that we would need
3042 the permissions here. */
3043 else if (data
&& data
->num_segments
== 1)
3045 segments
[0] = data
->segment_bases
[0] + text_addr
;
3048 /* There's no way to relocate by segment. */
3054 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
3055 offs
, num_segments
, segments
);
3057 if (ret
== 0 && !do_sections
)
3058 error (_("Can not handle qOffsets TextSeg response with this symbol file"));
3065 free_symfile_segment_data (data
);
3069 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
3071 /* This is a temporary kludge to force data and bss to use the same offsets
3072 because that's what nlmconv does now. The real solution requires changes
3073 to the stub and remote.c that I don't have time to do right now. */
3075 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
3076 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
3079 objfile_relocate (symfile_objfile
, offs
);
3082 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3083 threads we know are stopped already. This is used during the
3084 initial remote connection in non-stop mode --- threads that are
3085 reported as already being stopped are left stopped. */
3088 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
3090 /* If we have a stop reply for this thread, it must be stopped. */
3091 if (peek_stop_reply (thread
->ptid
))
3092 set_stop_requested (thread
->ptid
, 1);
3097 /* Stub for catch_exception. */
3099 struct start_remote_args
3103 /* The current target. */
3104 struct target_ops
*target
;
3106 /* Non-zero if this is an extended-remote target. */
3110 /* Send interrupt_sequence to remote target. */
3112 send_interrupt_sequence ()
3114 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
3115 serial_write (remote_desc
, "\x03", 1);
3116 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
3117 serial_send_break (remote_desc
);
3118 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
3120 serial_send_break (remote_desc
);
3121 serial_write (remote_desc
, "g", 1);
3124 internal_error (__FILE__
, __LINE__
,
3125 _("Invalid value for interrupt_sequence_mode: %s."),
3126 interrupt_sequence_mode
);
3130 remote_start_remote (struct ui_out
*uiout
, void *opaque
)
3132 struct start_remote_args
*args
= opaque
;
3133 struct remote_state
*rs
= get_remote_state ();
3134 struct packet_config
*noack_config
;
3135 char *wait_status
= NULL
;
3137 immediate_quit
++; /* Allow user to interrupt it. */
3139 /* Ack any packet which the remote side has already sent. */
3140 serial_write (remote_desc
, "+", 1);
3142 if (interrupt_on_connect
)
3143 send_interrupt_sequence ();
3145 /* The first packet we send to the target is the optional "supported
3146 packets" request. If the target can answer this, it will tell us
3147 which later probes to skip. */
3148 remote_query_supported ();
3150 /* If the stub wants to get a QAllow, compose one and send it. */
3151 if (remote_protocol_packets
[PACKET_QAllow
].support
!= PACKET_DISABLE
)
3152 remote_set_permissions ();
3154 /* Next, we possibly activate noack mode.
3156 If the QStartNoAckMode packet configuration is set to AUTO,
3157 enable noack mode if the stub reported a wish for it with
3160 If set to TRUE, then enable noack mode even if the stub didn't
3161 report it in qSupported. If the stub doesn't reply OK, the
3162 session ends with an error.
3164 If FALSE, then don't activate noack mode, regardless of what the
3165 stub claimed should be the default with qSupported. */
3167 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
3169 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
3170 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
3171 && noack_config
->support
== PACKET_ENABLE
))
3173 putpkt ("QStartNoAckMode");
3174 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3175 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
3179 if (args
->extended_p
)
3181 /* Tell the remote that we are using the extended protocol. */
3183 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3186 /* Next, if the target can specify a description, read it. We do
3187 this before anything involving memory or registers. */
3188 target_find_description ();
3190 /* Next, now that we know something about the target, update the
3191 address spaces in the program spaces. */
3192 update_address_spaces ();
3194 /* On OSs where the list of libraries is global to all
3195 processes, we fetch them early. */
3196 if (gdbarch_has_global_solist (target_gdbarch
))
3197 solib_add (NULL
, args
->from_tty
, args
->target
, auto_solib_add
);
3201 if (!rs
->non_stop_aware
)
3202 error (_("Non-stop mode requested, but remote does not support non-stop"));
3204 putpkt ("QNonStop:1");
3205 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3207 if (strcmp (rs
->buf
, "OK") != 0)
3208 error ("Remote refused setting non-stop mode with: %s", rs
->buf
);
3210 /* Find about threads and processes the stub is already
3211 controlling. We default to adding them in the running state.
3212 The '?' query below will then tell us about which threads are
3214 remote_threads_info (args
->target
);
3216 else if (rs
->non_stop_aware
)
3218 /* Don't assume that the stub can operate in all-stop mode.
3219 Request it explicitely. */
3220 putpkt ("QNonStop:0");
3221 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3223 if (strcmp (rs
->buf
, "OK") != 0)
3224 error ("Remote refused setting all-stop mode with: %s", rs
->buf
);
3227 /* Check whether the target is running now. */
3229 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3233 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
3235 if (!args
->extended_p
)
3236 error (_("The target is not running (try extended-remote?)"));
3238 /* We're connected, but not running. Drop out before we
3239 call start_remote. */
3244 /* Save the reply for later. */
3245 wait_status
= alloca (strlen (rs
->buf
) + 1);
3246 strcpy (wait_status
, rs
->buf
);
3249 /* Let the stub know that we want it to return the thread. */
3250 set_continue_thread (minus_one_ptid
);
3252 /* Without this, some commands which require an active target
3253 (such as kill) won't work. This variable serves (at least)
3254 double duty as both the pid of the target process (if it has
3255 such), and as a flag indicating that a target is active.
3256 These functions should be split out into seperate variables,
3257 especially since GDB will someday have a notion of debugging
3258 several processes. */
3259 inferior_ptid
= magic_null_ptid
;
3261 /* Now, if we have thread information, update inferior_ptid. */
3262 inferior_ptid
= remote_current_thread (inferior_ptid
);
3264 remote_add_inferior (ptid_get_pid (inferior_ptid
), -1);
3266 /* Always add the main thread. */
3267 add_thread_silent (inferior_ptid
);
3269 get_offsets (); /* Get text, data & bss offsets. */
3271 /* If we could not find a description using qXfer, and we know
3272 how to do it some other way, try again. This is not
3273 supported for non-stop; it could be, but it is tricky if
3274 there are no stopped threads when we connect. */
3275 if (remote_read_description_p (args
->target
)
3276 && gdbarch_target_desc (target_gdbarch
) == NULL
)
3278 target_clear_description ();
3279 target_find_description ();
3282 /* Use the previously fetched status. */
3283 gdb_assert (wait_status
!= NULL
);
3284 strcpy (rs
->buf
, wait_status
);
3285 rs
->cached_wait_status
= 1;
3288 start_remote (args
->from_tty
); /* Initialize gdb process mechanisms. */
3292 /* Clear WFI global state. Do this before finding about new
3293 threads and inferiors, and setting the current inferior.
3294 Otherwise we would clear the proceed status of the current
3295 inferior when we want its stop_soon state to be preserved
3296 (see notice_new_inferior). */
3297 init_wait_for_inferior ();
3299 /* In non-stop, we will either get an "OK", meaning that there
3300 are no stopped threads at this time; or, a regular stop
3301 reply. In the latter case, there may be more than one thread
3302 stopped --- we pull them all out using the vStopped
3304 if (strcmp (rs
->buf
, "OK") != 0)
3306 struct stop_reply
*stop_reply
;
3307 struct cleanup
*old_chain
;
3309 stop_reply
= stop_reply_xmalloc ();
3310 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
3312 remote_parse_stop_reply (rs
->buf
, stop_reply
);
3313 discard_cleanups (old_chain
);
3315 /* get_pending_stop_replies acks this one, and gets the rest
3317 pending_stop_reply
= stop_reply
;
3318 remote_get_pending_stop_replies ();
3320 /* Make sure that threads that were stopped remain
3322 iterate_over_threads (set_stop_requested_callback
, NULL
);
3325 if (target_can_async_p ())
3326 target_async (inferior_event_handler
, 0);
3328 if (thread_count () == 0)
3330 if (!args
->extended_p
)
3331 error (_("The target is not running (try extended-remote?)"));
3333 /* We're connected, but not running. Drop out before we
3334 call start_remote. */
3338 /* Let the stub know that we want it to return the thread. */
3340 /* Force the stub to choose a thread. */
3341 set_general_thread (null_ptid
);
3344 inferior_ptid
= remote_current_thread (minus_one_ptid
);
3345 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
3346 error (_("remote didn't report the current thread in non-stop mode"));
3348 get_offsets (); /* Get text, data & bss offsets. */
3350 /* In non-stop mode, any cached wait status will be stored in
3351 the stop reply queue. */
3352 gdb_assert (wait_status
== NULL
);
3354 /* Update the remote on signals to silently pass, or more
3355 importantly, which to not ignore, in case a previous session
3356 had set some different set of signals to be ignored. */
3357 remote_pass_signals ();
3360 /* If we connected to a live target, do some additional setup. */
3361 if (target_has_execution
)
3363 if (exec_bfd
) /* No use without an exec file. */
3364 remote_check_symbols (symfile_objfile
);
3367 /* Possibly the target has been engaged in a trace run started
3368 previously; find out where things are at. */
3369 if (remote_get_trace_status (current_trace_status ()) != -1)
3371 struct uploaded_tp
*uploaded_tps
= NULL
;
3372 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
3374 if (current_trace_status ()->running
)
3375 printf_filtered (_("Trace is already running on the target.\n"));
3377 /* Get trace state variables first, they may be checked when
3378 parsing uploaded commands. */
3380 remote_upload_trace_state_variables (&uploaded_tsvs
);
3382 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
3384 remote_upload_tracepoints (&uploaded_tps
);
3386 merge_uploaded_tracepoints (&uploaded_tps
);
3389 /* If breakpoints are global, insert them now. */
3390 if (gdbarch_has_global_breakpoints (target_gdbarch
)
3391 && breakpoints_always_inserted_mode ())
3392 insert_breakpoints ();
3395 /* Open a connection to a remote debugger.
3396 NAME is the filename used for communication. */
3399 remote_open (char *name
, int from_tty
)
3401 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
3404 /* Open a connection to a remote debugger using the extended
3405 remote gdb protocol. NAME is the filename used for communication. */
3408 extended_remote_open (char *name
, int from_tty
)
3410 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
3413 /* Generic code for opening a connection to a remote target. */
3416 init_all_packet_configs (void)
3420 for (i
= 0; i
< PACKET_MAX
; i
++)
3421 update_packet_config (&remote_protocol_packets
[i
]);
3424 /* Symbol look-up. */
3427 remote_check_symbols (struct objfile
*objfile
)
3429 struct remote_state
*rs
= get_remote_state ();
3430 char *msg
, *reply
, *tmp
;
3431 struct minimal_symbol
*sym
;
3434 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
3437 /* Make sure the remote is pointing at the right process. */
3438 set_general_process ();
3440 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3441 because we need both at the same time. */
3442 msg
= alloca (get_remote_packet_size ());
3444 /* Invite target to request symbol lookups. */
3446 putpkt ("qSymbol::");
3447 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3448 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
3451 while (strncmp (reply
, "qSymbol:", 8) == 0)
3454 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
3456 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
3458 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
3461 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
3462 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
3464 /* If this is a function address, return the start of code
3465 instead of any data function descriptor. */
3466 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
3470 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
3471 phex_nz (sym_addr
, addr_size
), &reply
[8]);
3475 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3480 static struct serial
*
3481 remote_serial_open (char *name
)
3483 static int udp_warning
= 0;
3485 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3486 of in ser-tcp.c, because it is the remote protocol assuming that the
3487 serial connection is reliable and not the serial connection promising
3489 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
3492 The remote protocol may be unreliable over UDP.\n\
3493 Some events may be lost, rendering further debugging impossible."));
3497 return serial_open (name
);
3500 /* Inform the target of our permission settings. The permission flags
3501 work without this, but if the target knows the settings, it can do
3502 a couple things. First, it can add its own check, to catch cases
3503 that somehow manage to get by the permissions checks in target
3504 methods. Second, if the target is wired to disallow particular
3505 settings (for instance, a system in the field that is not set up to
3506 be able to stop at a breakpoint), it can object to any unavailable
3510 remote_set_permissions (void)
3512 struct remote_state
*rs
= get_remote_state ();
3514 sprintf (rs
->buf
, "QAllow:"
3515 "WriteReg:%x;WriteMem:%x;"
3516 "InsertBreak:%x;InsertTrace:%x;"
3517 "InsertFastTrace:%x;Stop:%x",
3518 may_write_registers
, may_write_memory
,
3519 may_insert_breakpoints
, may_insert_tracepoints
,
3520 may_insert_fast_tracepoints
, may_stop
);
3522 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3524 /* If the target didn't like the packet, warn the user. Do not try
3525 to undo the user's settings, that would just be maddening. */
3526 if (strcmp (rs
->buf
, "OK") != 0)
3527 warning ("Remote refused setting permissions with: %s", rs
->buf
);
3530 /* This type describes each known response to the qSupported
3532 struct protocol_feature
3534 /* The name of this protocol feature. */
3537 /* The default for this protocol feature. */
3538 enum packet_support default_support
;
3540 /* The function to call when this feature is reported, or after
3541 qSupported processing if the feature is not supported.
3542 The first argument points to this structure. The second
3543 argument indicates whether the packet requested support be
3544 enabled, disabled, or probed (or the default, if this function
3545 is being called at the end of processing and this feature was
3546 not reported). The third argument may be NULL; if not NULL, it
3547 is a NUL-terminated string taken from the packet following
3548 this feature's name and an equals sign. */
3549 void (*func
) (const struct protocol_feature
*, enum packet_support
,
3552 /* The corresponding packet for this feature. Only used if
3553 FUNC is remote_supported_packet. */
3558 remote_supported_packet (const struct protocol_feature
*feature
,
3559 enum packet_support support
,
3560 const char *argument
)
3564 warning (_("Remote qSupported response supplied an unexpected value for"
3565 " \"%s\"."), feature
->name
);
3569 if (remote_protocol_packets
[feature
->packet
].support
3570 == PACKET_SUPPORT_UNKNOWN
)
3571 remote_protocol_packets
[feature
->packet
].support
= support
;
3575 remote_packet_size (const struct protocol_feature
*feature
,
3576 enum packet_support support
, const char *value
)
3578 struct remote_state
*rs
= get_remote_state ();
3583 if (support
!= PACKET_ENABLE
)
3586 if (value
== NULL
|| *value
== '\0')
3588 warning (_("Remote target reported \"%s\" without a size."),
3594 packet_size
= strtol (value
, &value_end
, 16);
3595 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
3597 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3598 feature
->name
, value
);
3602 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
3604 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3605 packet_size
, MAX_REMOTE_PACKET_SIZE
);
3606 packet_size
= MAX_REMOTE_PACKET_SIZE
;
3609 /* Record the new maximum packet size. */
3610 rs
->explicit_packet_size
= packet_size
;
3614 remote_multi_process_feature (const struct protocol_feature
*feature
,
3615 enum packet_support support
, const char *value
)
3617 struct remote_state
*rs
= get_remote_state ();
3619 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
3623 remote_non_stop_feature (const struct protocol_feature
*feature
,
3624 enum packet_support support
, const char *value
)
3626 struct remote_state
*rs
= get_remote_state ();
3628 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
3632 remote_cond_tracepoint_feature (const struct protocol_feature
*feature
,
3633 enum packet_support support
,
3636 struct remote_state
*rs
= get_remote_state ();
3638 rs
->cond_tracepoints
= (support
== PACKET_ENABLE
);
3642 remote_fast_tracepoint_feature (const struct protocol_feature
*feature
,
3643 enum packet_support support
,
3646 struct remote_state
*rs
= get_remote_state ();
3648 rs
->fast_tracepoints
= (support
== PACKET_ENABLE
);
3652 remote_static_tracepoint_feature (const struct protocol_feature
*feature
,
3653 enum packet_support support
,
3656 struct remote_state
*rs
= get_remote_state ();
3658 rs
->static_tracepoints
= (support
== PACKET_ENABLE
);
3662 remote_disconnected_tracing_feature (const struct protocol_feature
*feature
,
3663 enum packet_support support
,
3666 struct remote_state
*rs
= get_remote_state ();
3668 rs
->disconnected_tracing
= (support
== PACKET_ENABLE
);
3671 static struct protocol_feature remote_protocol_features
[] = {
3672 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
3673 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
3674 PACKET_qXfer_auxv
},
3675 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
3676 PACKET_qXfer_features
},
3677 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
3678 PACKET_qXfer_libraries
},
3679 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
3680 PACKET_qXfer_memory_map
},
3681 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
3682 PACKET_qXfer_spu_read
},
3683 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
3684 PACKET_qXfer_spu_write
},
3685 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
3686 PACKET_qXfer_osdata
},
3687 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
3688 PACKET_qXfer_threads
},
3689 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
3690 PACKET_QPassSignals
},
3691 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
3692 PACKET_QStartNoAckMode
},
3693 { "multiprocess", PACKET_DISABLE
, remote_multi_process_feature
, -1 },
3694 { "QNonStop", PACKET_DISABLE
, remote_non_stop_feature
, -1 },
3695 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
3696 PACKET_qXfer_siginfo_read
},
3697 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
3698 PACKET_qXfer_siginfo_write
},
3699 { "ConditionalTracepoints", PACKET_DISABLE
, remote_cond_tracepoint_feature
,
3700 PACKET_ConditionalTracepoints
},
3701 { "FastTracepoints", PACKET_DISABLE
, remote_fast_tracepoint_feature
,
3702 PACKET_FastTracepoints
},
3703 { "StaticTracepoints", PACKET_DISABLE
, remote_static_tracepoint_feature
,
3704 PACKET_StaticTracepoints
},
3705 { "DisconnectedTracing", PACKET_DISABLE
, remote_disconnected_tracing_feature
,
3707 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
3709 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
3711 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
3712 PACKET_TracepointSource
},
3713 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
3717 static char *remote_support_xml
;
3719 /* Register string appended to "xmlRegisters=" in qSupported query. */
3722 register_remote_support_xml (const char *xml
)
3724 #if defined(HAVE_LIBEXPAT)
3725 if (remote_support_xml
== NULL
)
3726 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
3729 char *copy
= xstrdup (remote_support_xml
+ 13);
3730 char *p
= strtok (copy
, ",");
3734 if (strcmp (p
, xml
) == 0)
3741 while ((p
= strtok (NULL
, ",")) != NULL
);
3744 remote_support_xml
= reconcat (remote_support_xml
,
3745 remote_support_xml
, ",", xml
,
3752 remote_query_supported_append (char *msg
, const char *append
)
3755 return reconcat (msg
, msg
, ";", append
, (char *) NULL
);
3757 return xstrdup (append
);
3761 remote_query_supported (void)
3763 struct remote_state
*rs
= get_remote_state ();
3766 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
3768 /* The packet support flags are handled differently for this packet
3769 than for most others. We treat an error, a disabled packet, and
3770 an empty response identically: any features which must be reported
3771 to be used will be automatically disabled. An empty buffer
3772 accomplishes this, since that is also the representation for a list
3773 containing no features. */
3776 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
3779 struct cleanup
*old_chain
= make_cleanup (free_current_contents
, &q
);
3782 q
= remote_query_supported_append (q
, "multiprocess+");
3784 if (remote_support_xml
)
3785 q
= remote_query_supported_append (q
, remote_support_xml
);
3787 q
= remote_query_supported_append (q
, "qRelocInsn+");
3789 q
= reconcat (q
, "qSupported:", q
, (char *) NULL
);
3792 do_cleanups (old_chain
);
3794 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3796 /* If an error occured, warn, but do not return - just reset the
3797 buffer to empty and go on to disable features. */
3798 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
3801 warning (_("Remote failure reply: %s"), rs
->buf
);
3806 memset (seen
, 0, sizeof (seen
));
3811 enum packet_support is_supported
;
3812 char *p
, *end
, *name_end
, *value
;
3814 /* First separate out this item from the rest of the packet. If
3815 there's another item after this, we overwrite the separator
3816 (terminated strings are much easier to work with). */
3818 end
= strchr (p
, ';');
3821 end
= p
+ strlen (p
);
3831 warning (_("empty item in \"qSupported\" response"));
3836 name_end
= strchr (p
, '=');
3839 /* This is a name=value entry. */
3840 is_supported
= PACKET_ENABLE
;
3841 value
= name_end
+ 1;
3850 is_supported
= PACKET_ENABLE
;
3854 is_supported
= PACKET_DISABLE
;
3858 is_supported
= PACKET_SUPPORT_UNKNOWN
;
3862 warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p
);
3868 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3869 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
3871 const struct protocol_feature
*feature
;
3874 feature
= &remote_protocol_features
[i
];
3875 feature
->func (feature
, is_supported
, value
);
3880 /* If we increased the packet size, make sure to increase the global
3881 buffer size also. We delay this until after parsing the entire
3882 qSupported packet, because this is the same buffer we were
3884 if (rs
->buf_size
< rs
->explicit_packet_size
)
3886 rs
->buf_size
= rs
->explicit_packet_size
;
3887 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
3890 /* Handle the defaults for unmentioned features. */
3891 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
3894 const struct protocol_feature
*feature
;
3896 feature
= &remote_protocol_features
[i
];
3897 feature
->func (feature
, feature
->default_support
, NULL
);
3903 remote_open_1 (char *name
, int from_tty
, struct target_ops
*target
, int extended_p
)
3905 struct remote_state
*rs
= get_remote_state ();
3908 error (_("To open a remote debug connection, you need to specify what\n"
3909 "serial device is attached to the remote system\n"
3910 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
3912 /* See FIXME above. */
3913 if (!target_async_permitted
)
3914 wait_forever_enabled_p
= 1;
3916 /* If we're connected to a running target, target_preopen will kill it.
3917 But if we're connected to a target system with no running process,
3918 then we will still be connected when it returns. Ask this question
3919 first, before target_preopen has a chance to kill anything. */
3920 if (remote_desc
!= NULL
&& !have_inferiors ())
3923 || query (_("Already connected to a remote target. Disconnect? ")))
3926 error (_("Still connected."));
3929 target_preopen (from_tty
);
3931 unpush_target (target
);
3933 /* This time without a query. If we were connected to an
3934 extended-remote target and target_preopen killed the running
3935 process, we may still be connected. If we are starting "target
3936 remote" now, the extended-remote target will not have been
3937 removed by unpush_target. */
3938 if (remote_desc
!= NULL
&& !have_inferiors ())
3941 /* Make sure we send the passed signals list the next time we resume. */
3942 xfree (last_pass_packet
);
3943 last_pass_packet
= NULL
;
3945 remote_fileio_reset ();
3946 reopen_exec_file ();
3949 remote_desc
= remote_serial_open (name
);
3951 perror_with_name (name
);
3953 if (baud_rate
!= -1)
3955 if (serial_setbaudrate (remote_desc
, baud_rate
))
3957 /* The requested speed could not be set. Error out to
3958 top level after closing remote_desc. Take care to
3959 set remote_desc to NULL to avoid closing remote_desc
3961 serial_close (remote_desc
);
3963 perror_with_name (name
);
3967 serial_raw (remote_desc
);
3969 /* If there is something sitting in the buffer we might take it as a
3970 response to a command, which would be bad. */
3971 serial_flush_input (remote_desc
);
3975 puts_filtered ("Remote debugging using ");
3976 puts_filtered (name
);
3977 puts_filtered ("\n");
3979 push_target (target
); /* Switch to using remote target now. */
3981 /* Register extra event sources in the event loop. */
3982 remote_async_inferior_event_token
3983 = create_async_event_handler (remote_async_inferior_event_handler
,
3985 remote_async_get_pending_events_token
3986 = create_async_event_handler (remote_async_get_pending_events_handler
,
3989 /* Reset the target state; these things will be queried either by
3990 remote_query_supported or as they are needed. */
3991 init_all_packet_configs ();
3992 rs
->cached_wait_status
= 0;
3993 rs
->explicit_packet_size
= 0;
3995 rs
->multi_process_aware
= 0;
3996 rs
->extended
= extended_p
;
3997 rs
->non_stop_aware
= 0;
3998 rs
->waiting_for_stop_reply
= 0;
3999 rs
->ctrlc_pending_p
= 0;
4001 general_thread
= not_sent_ptid
;
4002 continue_thread
= not_sent_ptid
;
4004 /* Probe for ability to use "ThreadInfo" query, as required. */
4005 use_threadinfo_query
= 1;
4006 use_threadextra_query
= 1;
4008 if (target_async_permitted
)
4010 /* With this target we start out by owning the terminal. */
4011 remote_async_terminal_ours_p
= 1;
4013 /* FIXME: cagney/1999-09-23: During the initial connection it is
4014 assumed that the target is already ready and able to respond to
4015 requests. Unfortunately remote_start_remote() eventually calls
4016 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4017 around this. Eventually a mechanism that allows
4018 wait_for_inferior() to expect/get timeouts will be
4020 wait_forever_enabled_p
= 0;
4023 /* First delete any symbols previously loaded from shared libraries. */
4024 no_shared_libraries (NULL
, 0);
4027 init_thread_list ();
4029 /* Start the remote connection. If error() or QUIT, discard this
4030 target (we'd otherwise be in an inconsistent state) and then
4031 propogate the error on up the exception chain. This ensures that
4032 the caller doesn't stumble along blindly assuming that the
4033 function succeeded. The CLI doesn't have this problem but other
4034 UI's, such as MI do.
4036 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4037 this function should return an error indication letting the
4038 caller restore the previous state. Unfortunately the command
4039 ``target remote'' is directly wired to this function making that
4040 impossible. On a positive note, the CLI side of this problem has
4041 been fixed - the function set_cmd_context() makes it possible for
4042 all the ``target ....'' commands to share a common callback
4043 function. See cli-dump.c. */
4045 struct gdb_exception ex
;
4046 struct start_remote_args args
;
4048 args
.from_tty
= from_tty
;
4049 args
.target
= target
;
4050 args
.extended_p
= extended_p
;
4052 ex
= catch_exception (uiout
, remote_start_remote
, &args
, RETURN_MASK_ALL
);
4055 /* Pop the partially set up target - unless something else did
4056 already before throwing the exception. */
4057 if (remote_desc
!= NULL
)
4059 if (target_async_permitted
)
4060 wait_forever_enabled_p
= 1;
4061 throw_exception (ex
);
4065 if (target_async_permitted
)
4066 wait_forever_enabled_p
= 1;
4069 /* This takes a program previously attached to and detaches it. After
4070 this is done, GDB can be used to debug some other program. We
4071 better not have left any breakpoints in the target program or it'll
4072 die when it hits one. */
4075 remote_detach_1 (char *args
, int from_tty
, int extended
)
4077 int pid
= ptid_get_pid (inferior_ptid
);
4078 struct remote_state
*rs
= get_remote_state ();
4081 error (_("Argument given to \"detach\" when remotely debugging."));
4083 if (!target_has_execution
)
4084 error (_("No process to detach from."));
4086 /* Tell the remote target to detach. */
4087 if (remote_multi_process_p (rs
))
4088 sprintf (rs
->buf
, "D;%x", pid
);
4090 strcpy (rs
->buf
, "D");
4093 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4095 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
4097 else if (rs
->buf
[0] == '\0')
4098 error (_("Remote doesn't know how to detach"));
4100 error (_("Can't detach process."));
4104 if (remote_multi_process_p (rs
))
4105 printf_filtered (_("Detached from remote %s.\n"),
4106 target_pid_to_str (pid_to_ptid (pid
)));
4110 puts_filtered (_("Detached from remote process.\n"));
4112 puts_filtered (_("Ending remote debugging.\n"));
4116 discard_pending_stop_replies (pid
);
4117 target_mourn_inferior ();
4121 remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
4123 remote_detach_1 (args
, from_tty
, 0);
4127 extended_remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
4129 remote_detach_1 (args
, from_tty
, 1);
4132 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4135 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
4138 error (_("Argument given to \"disconnect\" when remotely debugging."));
4140 /* Make sure we unpush even the extended remote targets; mourn
4141 won't do it. So call remote_mourn_1 directly instead of
4142 target_mourn_inferior. */
4143 remote_mourn_1 (target
);
4146 puts_filtered ("Ending remote debugging.\n");
4149 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4150 be chatty about it. */
4153 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
4155 struct remote_state
*rs
= get_remote_state ();
4157 char *wait_status
= NULL
;
4159 pid
= parse_pid_to_attach (args
);
4161 /* Remote PID can be freely equal to getpid, do not check it here the same
4162 way as in other targets. */
4164 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4165 error (_("This target does not support attaching to a process"));
4167 sprintf (rs
->buf
, "vAttach;%x", pid
);
4169 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4171 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
4174 printf_unfiltered (_("Attached to %s\n"),
4175 target_pid_to_str (pid_to_ptid (pid
)));
4179 /* Save the reply for later. */
4180 wait_status
= alloca (strlen (rs
->buf
) + 1);
4181 strcpy (wait_status
, rs
->buf
);
4183 else if (strcmp (rs
->buf
, "OK") != 0)
4184 error (_("Attaching to %s failed with: %s"),
4185 target_pid_to_str (pid_to_ptid (pid
)),
4188 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4189 error (_("This target does not support attaching to a process"));
4191 error (_("Attaching to %s failed"),
4192 target_pid_to_str (pid_to_ptid (pid
)));
4194 set_current_inferior (remote_add_inferior (pid
, 1));
4196 inferior_ptid
= pid_to_ptid (pid
);
4200 struct thread_info
*thread
;
4202 /* Get list of threads. */
4203 remote_threads_info (target
);
4205 thread
= first_thread_of_process (pid
);
4207 inferior_ptid
= thread
->ptid
;
4209 inferior_ptid
= pid_to_ptid (pid
);
4211 /* Invalidate our notion of the remote current thread. */
4212 record_currthread (minus_one_ptid
);
4216 /* Now, if we have thread information, update inferior_ptid. */
4217 inferior_ptid
= remote_current_thread (inferior_ptid
);
4219 /* Add the main thread to the thread list. */
4220 add_thread_silent (inferior_ptid
);
4223 /* Next, if the target can specify a description, read it. We do
4224 this before anything involving memory or registers. */
4225 target_find_description ();
4229 /* Use the previously fetched status. */
4230 gdb_assert (wait_status
!= NULL
);
4232 if (target_can_async_p ())
4234 struct stop_reply
*stop_reply
;
4235 struct cleanup
*old_chain
;
4237 stop_reply
= stop_reply_xmalloc ();
4238 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4239 remote_parse_stop_reply (wait_status
, stop_reply
);
4240 discard_cleanups (old_chain
);
4241 push_stop_reply (stop_reply
);
4243 target_async (inferior_event_handler
, 0);
4247 gdb_assert (wait_status
!= NULL
);
4248 strcpy (rs
->buf
, wait_status
);
4249 rs
->cached_wait_status
= 1;
4253 gdb_assert (wait_status
== NULL
);
4257 extended_remote_attach (struct target_ops
*ops
, char *args
, int from_tty
)
4259 extended_remote_attach_1 (ops
, args
, from_tty
);
4262 /* Convert hex digit A to a number. */
4267 if (a
>= '0' && a
<= '9')
4269 else if (a
>= 'a' && a
<= 'f')
4270 return a
- 'a' + 10;
4271 else if (a
>= 'A' && a
<= 'F')
4272 return a
- 'A' + 10;
4274 error (_("Reply contains invalid hex digit %d"), a
);
4278 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
4282 for (i
= 0; i
< count
; i
++)
4284 if (hex
[0] == 0 || hex
[1] == 0)
4286 /* Hex string is short, or of uneven length.
4287 Return the count that has been converted so far. */
4290 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
4296 /* Convert number NIB to a hex digit. */
4304 return 'a' + nib
- 10;
4308 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
4312 /* May use a length, or a nul-terminated string as input. */
4314 count
= strlen ((char *) bin
);
4316 for (i
= 0; i
< count
; i
++)
4318 *hex
++ = tohex ((*bin
>> 4) & 0xf);
4319 *hex
++ = tohex (*bin
++ & 0xf);
4325 /* Check for the availability of vCont. This function should also check
4329 remote_vcont_probe (struct remote_state
*rs
)
4333 strcpy (rs
->buf
, "vCont?");
4335 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4338 /* Make sure that the features we assume are supported. */
4339 if (strncmp (buf
, "vCont", 5) == 0)
4342 int support_s
, support_S
, support_c
, support_C
;
4348 rs
->support_vCont_t
= 0;
4349 while (p
&& *p
== ';')
4352 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4354 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4356 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4358 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4360 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4361 rs
->support_vCont_t
= 1;
4363 p
= strchr (p
, ';');
4366 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4367 BUF will make packet_ok disable the packet. */
4368 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
4372 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
4375 /* Helper function for building "vCont" resumptions. Write a
4376 resumption to P. ENDP points to one-passed-the-end of the buffer
4377 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4378 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4379 resumed thread should be single-stepped and/or signalled. If PTID
4380 equals minus_one_ptid, then all threads are resumed; if PTID
4381 represents a process, then all threads of the process are resumed;
4382 the thread to be stepped and/or signalled is given in the global
4386 append_resumption (char *p
, char *endp
,
4387 ptid_t ptid
, int step
, enum target_signal siggnal
)
4389 struct remote_state
*rs
= get_remote_state ();
4391 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
4392 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
4394 p
+= xsnprintf (p
, endp
- p
, ";s");
4395 else if (siggnal
!= TARGET_SIGNAL_0
)
4396 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
4398 p
+= xsnprintf (p
, endp
- p
, ";c");
4400 if (remote_multi_process_p (rs
) && ptid_is_pid (ptid
))
4404 /* All (-1) threads of process. */
4405 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4407 p
+= xsnprintf (p
, endp
- p
, ":");
4408 p
= write_ptid (p
, endp
, nptid
);
4410 else if (!ptid_equal (ptid
, minus_one_ptid
))
4412 p
+= xsnprintf (p
, endp
- p
, ":");
4413 p
= write_ptid (p
, endp
, ptid
);
4420 currently_stepping_callback (struct thread_info
*tp
, void *data
)
4422 return currently_stepping (tp
);
4425 /* Resume the remote inferior by using a "vCont" packet. The thread
4426 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4427 resumed thread should be single-stepped and/or signalled. If PTID
4428 equals minus_one_ptid, then all threads are resumed; the thread to
4429 be stepped and/or signalled is given in the global INFERIOR_PTID.
4430 This function returns non-zero iff it resumes the inferior.
4432 This function issues a strict subset of all possible vCont commands at the
4436 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
4438 struct remote_state
*rs
= get_remote_state ();
4442 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4443 remote_vcont_probe (rs
);
4445 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
4449 endp
= rs
->buf
+ get_remote_packet_size ();
4451 /* If we could generate a wider range of packets, we'd have to worry
4452 about overflowing BUF. Should there be a generic
4453 "multi-part-packet" packet? */
4455 p
+= xsnprintf (p
, endp
- p
, "vCont");
4457 if (ptid_equal (ptid
, magic_null_ptid
))
4459 /* MAGIC_NULL_PTID means that we don't have any active threads,
4460 so we don't have any TID numbers the inferior will
4461 understand. Make sure to only send forms that do not specify
4463 p
= append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
4465 else if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
4467 struct thread_info
*tp
;
4469 /* Resume all threads (of all processes, or of a single
4470 process), with preference for INFERIOR_PTID. This assumes
4471 inferior_ptid belongs to the set of all threads we are about
4473 if (step
|| siggnal
!= TARGET_SIGNAL_0
)
4475 /* Step inferior_ptid, with or without signal. */
4476 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
4479 tp
= iterate_over_threads (currently_stepping_callback
, NULL
);
4480 if (tp
&& !ptid_equal (tp
->ptid
, inferior_ptid
))
4482 p
= append_resumption (p
, endp
, tp
->ptid
, 1, TARGET_SIGNAL_0
);
4485 /* And continue others without a signal. */
4486 p
= append_resumption (p
, endp
, ptid
, /*step=*/ 0, TARGET_SIGNAL_0
);
4490 /* Scheduler locking; resume only PTID. */
4491 p
= append_resumption (p
, endp
, ptid
, step
, siggnal
);
4494 gdb_assert (strlen (rs
->buf
) < get_remote_packet_size ());
4499 /* In non-stop, the stub replies to vCont with "OK". The stop
4500 reply will be reported asynchronously by means of a `%Stop'
4502 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4503 if (strcmp (rs
->buf
, "OK") != 0)
4504 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
);
4510 /* Tell the remote machine to resume. */
4512 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
4514 static int last_sent_step
;
4517 remote_resume (struct target_ops
*ops
,
4518 ptid_t ptid
, int step
, enum target_signal siggnal
)
4520 struct remote_state
*rs
= get_remote_state ();
4523 last_sent_signal
= siggnal
;
4524 last_sent_step
= step
;
4526 /* Update the inferior on signals to silently pass, if they've changed. */
4527 remote_pass_signals ();
4529 /* The vCont packet doesn't need to specify threads via Hc. */
4530 /* No reverse support (yet) for vCont. */
4531 if (execution_direction
!= EXEC_REVERSE
)
4532 if (remote_vcont_resume (ptid
, step
, siggnal
))
4535 /* All other supported resume packets do use Hc, so set the continue
4537 if (ptid_equal (ptid
, minus_one_ptid
))
4538 set_continue_thread (any_thread_ptid
);
4540 set_continue_thread (ptid
);
4543 if (execution_direction
== EXEC_REVERSE
)
4545 /* We don't pass signals to the target in reverse exec mode. */
4546 if (info_verbose
&& siggnal
!= TARGET_SIGNAL_0
)
4547 warning (" - Can't pass signal %d to target in reverse: ignored.\n",
4551 && remote_protocol_packets
[PACKET_bs
].support
== PACKET_DISABLE
)
4552 error (_("Remote reverse-step not supported."));
4554 && remote_protocol_packets
[PACKET_bc
].support
== PACKET_DISABLE
)
4555 error (_("Remote reverse-continue not supported."));
4557 strcpy (buf
, step
? "bs" : "bc");
4559 else if (siggnal
!= TARGET_SIGNAL_0
)
4561 buf
[0] = step
? 'S' : 'C';
4562 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
4563 buf
[2] = tohex (((int) siggnal
) & 0xf);
4567 strcpy (buf
, step
? "s" : "c");
4572 /* We are about to start executing the inferior, let's register it
4573 with the event loop. NOTE: this is the one place where all the
4574 execution commands end up. We could alternatively do this in each
4575 of the execution commands in infcmd.c. */
4576 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4577 into infcmd.c in order to allow inferior function calls to work
4578 NOT asynchronously. */
4579 if (target_can_async_p ())
4580 target_async (inferior_event_handler
, 0);
4582 /* We've just told the target to resume. The remote server will
4583 wait for the inferior to stop, and then send a stop reply. In
4584 the mean time, we can't start another command/query ourselves
4585 because the stub wouldn't be ready to process it. This applies
4586 only to the base all-stop protocol, however. In non-stop (which
4587 only supports vCont), the stub replies with an "OK", and is
4588 immediate able to process further serial input. */
4590 rs
->waiting_for_stop_reply
= 1;
4594 /* Set up the signal handler for SIGINT, while the target is
4595 executing, ovewriting the 'regular' SIGINT signal handler. */
4597 initialize_sigint_signal_handler (void)
4599 signal (SIGINT
, handle_remote_sigint
);
4602 /* Signal handler for SIGINT, while the target is executing. */
4604 handle_remote_sigint (int sig
)
4606 signal (sig
, handle_remote_sigint_twice
);
4607 mark_async_signal_handler_wrapper (sigint_remote_token
);
4610 /* Signal handler for SIGINT, installed after SIGINT has already been
4611 sent once. It will take effect the second time that the user sends
4614 handle_remote_sigint_twice (int sig
)
4616 signal (sig
, handle_remote_sigint
);
4617 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
4620 /* Perform the real interruption of the target execution, in response
4623 async_remote_interrupt (gdb_client_data arg
)
4626 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
4628 target_stop (inferior_ptid
);
4631 /* Perform interrupt, if the first attempt did not succeed. Just give
4632 up on the target alltogether. */
4634 async_remote_interrupt_twice (gdb_client_data arg
)
4637 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
4642 /* Reinstall the usual SIGINT handlers, after the target has
4645 cleanup_sigint_signal_handler (void *dummy
)
4647 signal (SIGINT
, handle_sigint
);
4650 /* Send ^C to target to halt it. Target will respond, and send us a
4652 static void (*ofunc
) (int);
4654 /* The command line interface's stop routine. This function is installed
4655 as a signal handler for SIGINT. The first time a user requests a
4656 stop, we call remote_stop to send a break or ^C. If there is no
4657 response from the target (it didn't stop when the user requested it),
4658 we ask the user if he'd like to detach from the target. */
4660 remote_interrupt (int signo
)
4662 /* If this doesn't work, try more severe steps. */
4663 signal (signo
, remote_interrupt_twice
);
4665 gdb_call_async_signal_handler (sigint_remote_token
, 1);
4668 /* The user typed ^C twice. */
4671 remote_interrupt_twice (int signo
)
4673 signal (signo
, ofunc
);
4674 gdb_call_async_signal_handler (sigint_remote_twice_token
, 1);
4675 signal (signo
, remote_interrupt
);
4678 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
4679 thread, all threads of a remote process, or all threads of all
4683 remote_stop_ns (ptid_t ptid
)
4685 struct remote_state
*rs
= get_remote_state ();
4687 char *endp
= rs
->buf
+ get_remote_packet_size ();
4689 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4690 remote_vcont_probe (rs
);
4692 if (!rs
->support_vCont_t
)
4693 error (_("Remote server does not support stopping threads"));
4695 if (ptid_equal (ptid
, minus_one_ptid
)
4696 || (!remote_multi_process_p (rs
) && ptid_is_pid (ptid
)))
4697 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
4702 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
4704 if (ptid_is_pid (ptid
))
4705 /* All (-1) threads of process. */
4706 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4709 /* Small optimization: if we already have a stop reply for
4710 this thread, no use in telling the stub we want this
4712 if (peek_stop_reply (ptid
))
4718 p
= write_ptid (p
, endp
, nptid
);
4721 /* In non-stop, we get an immediate OK reply. The stop reply will
4722 come in asynchronously by notification. */
4724 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4725 if (strcmp (rs
->buf
, "OK") != 0)
4726 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
), rs
->buf
);
4729 /* All-stop version of target_stop. Sends a break or a ^C to stop the
4730 remote target. It is undefined which thread of which process
4731 reports the stop. */
4734 remote_stop_as (ptid_t ptid
)
4736 struct remote_state
*rs
= get_remote_state ();
4738 rs
->ctrlc_pending_p
= 1;
4740 /* If the inferior is stopped already, but the core didn't know
4741 about it yet, just ignore the request. The cached wait status
4742 will be collected in remote_wait. */
4743 if (rs
->cached_wait_status
)
4746 /* Send interrupt_sequence to remote target. */
4747 send_interrupt_sequence ();
4750 /* This is the generic stop called via the target vector. When a target
4751 interrupt is requested, either by the command line or the GUI, we
4752 will eventually end up here. */
4755 remote_stop (ptid_t ptid
)
4758 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
4761 remote_stop_ns (ptid
);
4763 remote_stop_as (ptid
);
4766 /* Ask the user what to do when an interrupt is received. */
4769 interrupt_query (void)
4771 target_terminal_ours ();
4773 if (target_can_async_p ())
4775 signal (SIGINT
, handle_sigint
);
4776 deprecated_throw_reason (RETURN_QUIT
);
4780 if (query (_("Interrupted while waiting for the program.\n\
4781 Give up (and stop debugging it)? ")))
4784 deprecated_throw_reason (RETURN_QUIT
);
4788 target_terminal_inferior ();
4791 /* Enable/disable target terminal ownership. Most targets can use
4792 terminal groups to control terminal ownership. Remote targets are
4793 different in that explicit transfer of ownership to/from GDB/target
4797 remote_terminal_inferior (void)
4799 if (!target_async_permitted
)
4800 /* Nothing to do. */
4803 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
4804 idempotent. The event-loop GDB talking to an asynchronous target
4805 with a synchronous command calls this function from both
4806 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
4807 transfer the terminal to the target when it shouldn't this guard
4809 if (!remote_async_terminal_ours_p
)
4811 delete_file_handler (input_fd
);
4812 remote_async_terminal_ours_p
= 0;
4813 initialize_sigint_signal_handler ();
4814 /* NOTE: At this point we could also register our selves as the
4815 recipient of all input. Any characters typed could then be
4816 passed on down to the target. */
4820 remote_terminal_ours (void)
4822 if (!target_async_permitted
)
4823 /* Nothing to do. */
4826 /* See FIXME in remote_terminal_inferior. */
4827 if (remote_async_terminal_ours_p
)
4829 cleanup_sigint_signal_handler (NULL
);
4830 add_file_handler (input_fd
, stdin_event_handler
, 0);
4831 remote_async_terminal_ours_p
= 1;
4835 remote_console_output (char *msg
)
4839 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
4842 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
4846 fputs_unfiltered (tb
, gdb_stdtarg
);
4848 gdb_flush (gdb_stdtarg
);
4851 typedef struct cached_reg
4854 gdb_byte data
[MAX_REGISTER_SIZE
];
4857 DEF_VEC_O(cached_reg_t
);
4861 struct stop_reply
*next
;
4865 struct target_waitstatus ws
;
4867 VEC(cached_reg_t
) *regcache
;
4869 int stopped_by_watchpoint_p
;
4870 CORE_ADDR watch_data_address
;
4878 /* The list of already fetched and acknowledged stop events. */
4879 static struct stop_reply
*stop_reply_queue
;
4881 static struct stop_reply
*
4882 stop_reply_xmalloc (void)
4884 struct stop_reply
*r
= XMALLOC (struct stop_reply
);
4891 stop_reply_xfree (struct stop_reply
*r
)
4895 VEC_free (cached_reg_t
, r
->regcache
);
4900 /* Discard all pending stop replies of inferior PID. If PID is -1,
4901 discard everything. */
4904 discard_pending_stop_replies (int pid
)
4906 struct stop_reply
*prev
= NULL
, *reply
, *next
;
4908 /* Discard the in-flight notification. */
4909 if (pending_stop_reply
!= NULL
4911 || ptid_get_pid (pending_stop_reply
->ptid
) == pid
))
4913 stop_reply_xfree (pending_stop_reply
);
4914 pending_stop_reply
= NULL
;
4917 /* Discard the stop replies we have already pulled with
4919 for (reply
= stop_reply_queue
; reply
; reply
= next
)
4923 || ptid_get_pid (reply
->ptid
) == pid
)
4925 if (reply
== stop_reply_queue
)
4926 stop_reply_queue
= reply
->next
;
4928 prev
->next
= reply
->next
;
4930 stop_reply_xfree (reply
);
4937 /* Cleanup wrapper. */
4940 do_stop_reply_xfree (void *arg
)
4942 struct stop_reply
*r
= arg
;
4944 stop_reply_xfree (r
);
4947 /* Look for a queued stop reply belonging to PTID. If one is found,
4948 remove it from the queue, and return it. Returns NULL if none is
4949 found. If there are still queued events left to process, tell the
4950 event loop to get back to target_wait soon. */
4952 static struct stop_reply
*
4953 queued_stop_reply (ptid_t ptid
)
4955 struct stop_reply
*it
;
4956 struct stop_reply
**it_link
;
4958 it
= stop_reply_queue
;
4959 it_link
= &stop_reply_queue
;
4962 if (ptid_match (it
->ptid
, ptid
))
4964 *it_link
= it
->next
;
4969 it_link
= &it
->next
;
4973 if (stop_reply_queue
)
4974 /* There's still at least an event left. */
4975 mark_async_event_handler (remote_async_inferior_event_token
);
4980 /* Push a fully parsed stop reply in the stop reply queue. Since we
4981 know that we now have at least one queued event left to pass to the
4982 core side, tell the event loop to get back to target_wait soon. */
4985 push_stop_reply (struct stop_reply
*new_event
)
4987 struct stop_reply
*event
;
4989 if (stop_reply_queue
)
4991 for (event
= stop_reply_queue
;
4992 event
&& event
->next
;
4993 event
= event
->next
)
4996 event
->next
= new_event
;
4999 stop_reply_queue
= new_event
;
5001 mark_async_event_handler (remote_async_inferior_event_token
);
5004 /* Returns true if we have a stop reply for PTID. */
5007 peek_stop_reply (ptid_t ptid
)
5009 struct stop_reply
*it
;
5011 for (it
= stop_reply_queue
; it
; it
= it
->next
)
5012 if (ptid_equal (ptid
, it
->ptid
))
5014 if (it
->ws
.kind
== TARGET_WAITKIND_STOPPED
)
5021 /* Parse the stop reply in BUF. Either the function succeeds, and the
5022 result is stored in EVENT, or throws an error. */
5025 remote_parse_stop_reply (char *buf
, struct stop_reply
*event
)
5027 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5031 event
->ptid
= null_ptid
;
5032 event
->ws
.kind
= TARGET_WAITKIND_IGNORE
;
5033 event
->ws
.value
.integer
= 0;
5034 event
->solibs_changed
= 0;
5035 event
->replay_event
= 0;
5036 event
->stopped_by_watchpoint_p
= 0;
5037 event
->regcache
= NULL
;
5042 case 'T': /* Status with PC, SP, FP, ... */
5043 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5044 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5046 n... = register number
5047 r... = register contents
5050 p
= &buf
[3]; /* after Txx */
5058 /* If the packet contains a register number, save it in
5059 pnum and set p1 to point to the character following it.
5060 Otherwise p1 points to p. */
5062 /* If this packet is an awatch packet, don't parse the 'a'
5063 as a register number. */
5065 if (strncmp (p
, "awatch", strlen("awatch")) != 0
5066 && strncmp (p
, "core", strlen ("core") != 0))
5068 /* Read the ``P'' register number. */
5069 pnum
= strtol (p
, &p_temp
, 16);
5075 if (p1
== p
) /* No register number present here. */
5077 p1
= strchr (p
, ':');
5079 error (_("Malformed packet(a) (missing colon): %s\n\
5082 if (strncmp (p
, "thread", p1
- p
) == 0)
5083 event
->ptid
= read_ptid (++p1
, &p
);
5084 else if ((strncmp (p
, "watch", p1
- p
) == 0)
5085 || (strncmp (p
, "rwatch", p1
- p
) == 0)
5086 || (strncmp (p
, "awatch", p1
- p
) == 0))
5088 event
->stopped_by_watchpoint_p
= 1;
5089 p
= unpack_varlen_hex (++p1
, &addr
);
5090 event
->watch_data_address
= (CORE_ADDR
) addr
;
5092 else if (strncmp (p
, "library", p1
- p
) == 0)
5096 while (*p_temp
&& *p_temp
!= ';')
5099 event
->solibs_changed
= 1;
5102 else if (strncmp (p
, "replaylog", p1
- p
) == 0)
5104 /* NO_HISTORY event.
5105 p1 will indicate "begin" or "end", but
5106 it makes no difference for now, so ignore it. */
5107 event
->replay_event
= 1;
5108 p_temp
= strchr (p1
+ 1, ';');
5112 else if (strncmp (p
, "core", p1
- p
) == 0)
5116 p
= unpack_varlen_hex (++p1
, &c
);
5121 /* Silently skip unknown optional info. */
5122 p_temp
= strchr (p1
+ 1, ';');
5129 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
5130 cached_reg_t cached_reg
;
5135 error (_("Malformed packet(b) (missing colon): %s\n\
5141 error (_("Remote sent bad register number %s: %s\n\
5143 hex_string (pnum
), p
, buf
);
5145 cached_reg
.num
= reg
->regnum
;
5147 fieldsize
= hex2bin (p
, cached_reg
.data
,
5148 register_size (target_gdbarch
,
5151 if (fieldsize
< register_size (target_gdbarch
,
5153 warning (_("Remote reply is too short: %s"), buf
);
5155 VEC_safe_push (cached_reg_t
, event
->regcache
, &cached_reg
);
5159 error (_("Remote register badly formatted: %s\nhere: %s"),
5164 case 'S': /* Old style status, just signal only. */
5165 if (event
->solibs_changed
)
5166 event
->ws
.kind
= TARGET_WAITKIND_LOADED
;
5167 else if (event
->replay_event
)
5168 event
->ws
.kind
= TARGET_WAITKIND_NO_HISTORY
;
5171 event
->ws
.kind
= TARGET_WAITKIND_STOPPED
;
5172 event
->ws
.value
.sig
= (enum target_signal
)
5173 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
5176 case 'W': /* Target exited. */
5183 /* GDB used to accept only 2 hex chars here. Stubs should
5184 only send more if they detect GDB supports multi-process
5186 p
= unpack_varlen_hex (&buf
[1], &value
);
5190 /* The remote process exited. */
5191 event
->ws
.kind
= TARGET_WAITKIND_EXITED
;
5192 event
->ws
.value
.integer
= value
;
5196 /* The remote process exited with a signal. */
5197 event
->ws
.kind
= TARGET_WAITKIND_SIGNALLED
;
5198 event
->ws
.value
.sig
= (enum target_signal
) value
;
5201 /* If no process is specified, assume inferior_ptid. */
5202 pid
= ptid_get_pid (inferior_ptid
);
5211 else if (strncmp (p
,
5212 "process:", sizeof ("process:") - 1) == 0)
5216 p
+= sizeof ("process:") - 1;
5217 unpack_varlen_hex (p
, &upid
);
5221 error (_("unknown stop reply packet: %s"), buf
);
5224 error (_("unknown stop reply packet: %s"), buf
);
5225 event
->ptid
= pid_to_ptid (pid
);
5230 if (non_stop
&& ptid_equal (event
->ptid
, null_ptid
))
5231 error (_("No process or thread specified in stop reply: %s"), buf
);
5234 /* When the stub wants to tell GDB about a new stop reply, it sends a
5235 stop notification (%Stop). Those can come it at any time, hence,
5236 we have to make sure that any pending putpkt/getpkt sequence we're
5237 making is finished, before querying the stub for more events with
5238 vStopped. E.g., if we started a vStopped sequence immediatelly
5239 upon receiving the %Stop notification, something like this could
5247 1.6) <-- (registers reply to step #1.3)
5249 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5252 To solve this, whenever we parse a %Stop notification sucessfully,
5253 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5254 doing whatever we were doing:
5260 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5261 2.5) <-- (registers reply to step #2.3)
5263 Eventualy after step #2.5, we return to the event loop, which
5264 notices there's an event on the
5265 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5266 associated callback --- the function below. At this point, we're
5267 always safe to start a vStopped sequence. :
5270 2.7) <-- T05 thread:2
5276 remote_get_pending_stop_replies (void)
5278 struct remote_state
*rs
= get_remote_state ();
5280 if (pending_stop_reply
)
5283 putpkt ("vStopped");
5285 /* Now we can rely on it. */
5286 push_stop_reply (pending_stop_reply
);
5287 pending_stop_reply
= NULL
;
5291 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5292 if (strcmp (rs
->buf
, "OK") == 0)
5296 struct cleanup
*old_chain
;
5297 struct stop_reply
*stop_reply
= stop_reply_xmalloc ();
5299 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
5300 remote_parse_stop_reply (rs
->buf
, stop_reply
);
5303 putpkt ("vStopped");
5305 if (stop_reply
->ws
.kind
!= TARGET_WAITKIND_IGNORE
)
5307 /* Now we can rely on it. */
5308 discard_cleanups (old_chain
);
5309 push_stop_reply (stop_reply
);
5312 /* We got an unknown stop reply. */
5313 do_cleanups (old_chain
);
5320 /* Called when it is decided that STOP_REPLY holds the info of the
5321 event that is to be returned to the core. This function always
5322 destroys STOP_REPLY. */
5325 process_stop_reply (struct stop_reply
*stop_reply
,
5326 struct target_waitstatus
*status
)
5330 *status
= stop_reply
->ws
;
5331 ptid
= stop_reply
->ptid
;
5333 /* If no thread/process was reported by the stub, assume the current
5335 if (ptid_equal (ptid
, null_ptid
))
5336 ptid
= inferior_ptid
;
5338 if (status
->kind
!= TARGET_WAITKIND_EXITED
5339 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5341 /* Expedited registers. */
5342 if (stop_reply
->regcache
)
5344 struct regcache
*regcache
5345 = get_thread_arch_regcache (ptid
, target_gdbarch
);
5350 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
5352 regcache_raw_supply (regcache
, reg
->num
, reg
->data
);
5353 VEC_free (cached_reg_t
, stop_reply
->regcache
);
5356 remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
5357 remote_watch_data_address
= stop_reply
->watch_data_address
;
5359 remote_notice_new_inferior (ptid
, 0);
5360 demand_private_info (ptid
)->core
= stop_reply
->core
;
5363 stop_reply_xfree (stop_reply
);
5367 /* The non-stop mode version of target_wait. */
5370 remote_wait_ns (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5372 struct remote_state
*rs
= get_remote_state ();
5373 struct stop_reply
*stop_reply
;
5376 /* If in non-stop mode, get out of getpkt even if a
5377 notification is received. */
5379 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5386 case 'E': /* Error of some sort. */
5387 /* We're out of sync with the target now. Did it continue
5388 or not? We can't tell which thread it was in non-stop,
5389 so just ignore this. */
5390 warning (_("Remote failure reply: %s"), rs
->buf
);
5392 case 'O': /* Console output. */
5393 remote_console_output (rs
->buf
+ 1);
5396 warning (_("Invalid remote reply: %s"), rs
->buf
);
5400 /* Acknowledge a pending stop reply that may have arrived in the
5402 if (pending_stop_reply
!= NULL
)
5403 remote_get_pending_stop_replies ();
5405 /* If indeed we noticed a stop reply, we're done. */
5406 stop_reply
= queued_stop_reply (ptid
);
5407 if (stop_reply
!= NULL
)
5408 return process_stop_reply (stop_reply
, status
);
5410 /* Still no event. If we're just polling for an event, then
5411 return to the event loop. */
5412 if (options
& TARGET_WNOHANG
)
5414 status
->kind
= TARGET_WAITKIND_IGNORE
;
5415 return minus_one_ptid
;
5418 /* Otherwise do a blocking wait. */
5419 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5424 /* Wait until the remote machine stops, then return, storing status in
5425 STATUS just as `wait' would. */
5428 remote_wait_as (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5430 struct remote_state
*rs
= get_remote_state ();
5431 ptid_t event_ptid
= null_ptid
;
5433 struct stop_reply
*stop_reply
;
5437 status
->kind
= TARGET_WAITKIND_IGNORE
;
5438 status
->value
.integer
= 0;
5440 stop_reply
= queued_stop_reply (ptid
);
5441 if (stop_reply
!= NULL
)
5442 return process_stop_reply (stop_reply
, status
);
5444 if (rs
->cached_wait_status
)
5445 /* Use the cached wait status, but only once. */
5446 rs
->cached_wait_status
= 0;
5451 if (!target_is_async_p ())
5453 ofunc
= signal (SIGINT
, remote_interrupt
);
5454 /* If the user hit C-c before this packet, or between packets,
5455 pretend that it was hit right here. */
5459 remote_interrupt (SIGINT
);
5463 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5464 _never_ wait for ever -> test on target_is_async_p().
5465 However, before we do that we need to ensure that the caller
5466 knows how to take the target into/out of async mode. */
5467 ret
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
5468 if (!target_is_async_p ())
5469 signal (SIGINT
, ofunc
);
5474 remote_stopped_by_watchpoint_p
= 0;
5476 /* We got something. */
5477 rs
->waiting_for_stop_reply
= 0;
5479 /* Assume that the target has acknowledged Ctrl-C unless we receive
5480 an 'F' or 'O' packet. */
5481 if (buf
[0] != 'F' && buf
[0] != 'O')
5482 rs
->ctrlc_pending_p
= 0;
5486 case 'E': /* Error of some sort. */
5487 /* We're out of sync with the target now. Did it continue or
5488 not? Not is more likely, so report a stop. */
5489 warning (_("Remote failure reply: %s"), buf
);
5490 status
->kind
= TARGET_WAITKIND_STOPPED
;
5491 status
->value
.sig
= TARGET_SIGNAL_0
;
5493 case 'F': /* File-I/O request. */
5494 remote_fileio_request (buf
, rs
->ctrlc_pending_p
);
5495 rs
->ctrlc_pending_p
= 0;
5497 case 'T': case 'S': case 'X': case 'W':
5499 struct stop_reply
*stop_reply
;
5500 struct cleanup
*old_chain
;
5502 stop_reply
= stop_reply_xmalloc ();
5503 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
5504 remote_parse_stop_reply (buf
, stop_reply
);
5505 discard_cleanups (old_chain
);
5506 event_ptid
= process_stop_reply (stop_reply
, status
);
5509 case 'O': /* Console output. */
5510 remote_console_output (buf
+ 1);
5512 /* The target didn't really stop; keep waiting. */
5513 rs
->waiting_for_stop_reply
= 1;
5517 if (last_sent_signal
!= TARGET_SIGNAL_0
)
5519 /* Zero length reply means that we tried 'S' or 'C' and the
5520 remote system doesn't support it. */
5521 target_terminal_ours_for_output ();
5523 ("Can't send signals to this remote system. %s not sent.\n",
5524 target_signal_to_name (last_sent_signal
));
5525 last_sent_signal
= TARGET_SIGNAL_0
;
5526 target_terminal_inferior ();
5528 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
5529 putpkt ((char *) buf
);
5531 /* We just told the target to resume, so a stop reply is in
5533 rs
->waiting_for_stop_reply
= 1;
5536 /* else fallthrough */
5538 warning (_("Invalid remote reply: %s"), buf
);
5540 rs
->waiting_for_stop_reply
= 1;
5544 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
5546 /* Nothing interesting happened. If we're doing a non-blocking
5547 poll, we're done. Otherwise, go back to waiting. */
5548 if (options
& TARGET_WNOHANG
)
5549 return minus_one_ptid
;
5553 else if (status
->kind
!= TARGET_WAITKIND_EXITED
5554 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5556 if (!ptid_equal (event_ptid
, null_ptid
))
5557 record_currthread (event_ptid
);
5559 event_ptid
= inferior_ptid
;
5562 /* A process exit. Invalidate our notion of current thread. */
5563 record_currthread (minus_one_ptid
);
5568 /* Wait until the remote machine stops, then return, storing status in
5569 STATUS just as `wait' would. */
5572 remote_wait (struct target_ops
*ops
,
5573 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5578 event_ptid
= remote_wait_ns (ptid
, status
, options
);
5580 event_ptid
= remote_wait_as (ptid
, status
, options
);
5582 if (target_can_async_p ())
5584 /* If there are are events left in the queue tell the event loop
5586 if (stop_reply_queue
)
5587 mark_async_event_handler (remote_async_inferior_event_token
);
5593 /* Fetch a single register using a 'p' packet. */
5596 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
5598 struct remote_state
*rs
= get_remote_state ();
5600 char regp
[MAX_REGISTER_SIZE
];
5603 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
5606 if (reg
->pnum
== -1)
5611 p
+= hexnumstr (p
, reg
->pnum
);
5614 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5618 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
5622 case PACKET_UNKNOWN
:
5625 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
5626 gdbarch_register_name (get_regcache_arch (regcache
),
5631 /* If this register is unfetchable, tell the regcache. */
5634 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
5638 /* Otherwise, parse and supply the value. */
5644 error (_("fetch_register_using_p: early buf termination"));
5646 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
5649 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
5653 /* Fetch the registers included in the target's 'g' packet. */
5656 send_g_packet (void)
5658 struct remote_state
*rs
= get_remote_state ();
5661 sprintf (rs
->buf
, "g");
5662 remote_send (&rs
->buf
, &rs
->buf_size
);
5664 /* We can get out of synch in various cases. If the first character
5665 in the buffer is not a hex character, assume that has happened
5666 and try to fetch another packet to read. */
5667 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
5668 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
5669 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
5670 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
5673 fprintf_unfiltered (gdb_stdlog
,
5674 "Bad register packet; fetching a new packet\n");
5675 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5678 buf_len
= strlen (rs
->buf
);
5680 /* Sanity check the received packet. */
5681 if (buf_len
% 2 != 0)
5682 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
5688 process_g_packet (struct regcache
*regcache
)
5690 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5691 struct remote_state
*rs
= get_remote_state ();
5692 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5697 buf_len
= strlen (rs
->buf
);
5699 /* Further sanity checks, with knowledge of the architecture. */
5700 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
5701 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
5703 /* Save the size of the packet sent to us by the target. It is used
5704 as a heuristic when determining the max size of packets that the
5705 target can safely receive. */
5706 if (rsa
->actual_register_packet_size
== 0)
5707 rsa
->actual_register_packet_size
= buf_len
;
5709 /* If this is smaller than we guessed the 'g' packet would be,
5710 update our records. A 'g' reply that doesn't include a register's
5711 value implies either that the register is not available, or that
5712 the 'p' packet must be used. */
5713 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
5715 rsa
->sizeof_g_packet
= buf_len
/ 2;
5717 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
5719 if (rsa
->regs
[i
].pnum
== -1)
5722 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
5723 rsa
->regs
[i
].in_g_packet
= 0;
5725 rsa
->regs
[i
].in_g_packet
= 1;
5729 regs
= alloca (rsa
->sizeof_g_packet
);
5731 /* Unimplemented registers read as all bits zero. */
5732 memset (regs
, 0, rsa
->sizeof_g_packet
);
5734 /* Reply describes registers byte by byte, each byte encoded as two
5735 hex characters. Suck them all up, then supply them to the
5736 register cacheing/storage mechanism. */
5739 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
5741 if (p
[0] == 0 || p
[1] == 0)
5742 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
5743 internal_error (__FILE__
, __LINE__
,
5744 "unexpected end of 'g' packet reply");
5746 if (p
[0] == 'x' && p
[1] == 'x')
5747 regs
[i
] = 0; /* 'x' */
5749 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
5753 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
5755 struct packet_reg
*r
= &rsa
->regs
[i
];
5759 if (r
->offset
* 2 >= strlen (rs
->buf
))
5760 /* This shouldn't happen - we adjusted in_g_packet above. */
5761 internal_error (__FILE__
, __LINE__
,
5762 "unexpected end of 'g' packet reply");
5763 else if (rs
->buf
[r
->offset
* 2] == 'x')
5765 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
5766 /* The register isn't available, mark it as such (at
5767 the same time setting the value to zero). */
5768 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
5771 regcache_raw_supply (regcache
, r
->regnum
,
5778 fetch_registers_using_g (struct regcache
*regcache
)
5781 process_g_packet (regcache
);
5785 remote_fetch_registers (struct target_ops
*ops
,
5786 struct regcache
*regcache
, int regnum
)
5788 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5791 set_general_thread (inferior_ptid
);
5795 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5797 gdb_assert (reg
!= NULL
);
5799 /* If this register might be in the 'g' packet, try that first -
5800 we are likely to read more than one register. If this is the
5801 first 'g' packet, we might be overly optimistic about its
5802 contents, so fall back to 'p'. */
5803 if (reg
->in_g_packet
)
5805 fetch_registers_using_g (regcache
);
5806 if (reg
->in_g_packet
)
5810 if (fetch_register_using_p (regcache
, reg
))
5813 /* This register is not available. */
5814 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
5819 fetch_registers_using_g (regcache
);
5821 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5822 if (!rsa
->regs
[i
].in_g_packet
)
5823 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
5825 /* This register is not available. */
5826 regcache_raw_supply (regcache
, i
, NULL
);
5830 /* Prepare to store registers. Since we may send them all (using a
5831 'G' request), we have to read out the ones we don't want to change
5835 remote_prepare_to_store (struct regcache
*regcache
)
5837 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5839 gdb_byte buf
[MAX_REGISTER_SIZE
];
5841 /* Make sure the entire registers array is valid. */
5842 switch (remote_protocol_packets
[PACKET_P
].support
)
5844 case PACKET_DISABLE
:
5845 case PACKET_SUPPORT_UNKNOWN
:
5846 /* Make sure all the necessary registers are cached. */
5847 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5848 if (rsa
->regs
[i
].in_g_packet
)
5849 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
5856 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
5857 packet was not recognized. */
5860 store_register_using_P (const struct regcache
*regcache
,
5861 struct packet_reg
*reg
)
5863 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5864 struct remote_state
*rs
= get_remote_state ();
5865 /* Try storing a single register. */
5866 char *buf
= rs
->buf
;
5867 gdb_byte regp
[MAX_REGISTER_SIZE
];
5870 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
5873 if (reg
->pnum
== -1)
5876 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
5877 p
= buf
+ strlen (buf
);
5878 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
5879 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
5881 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5883 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
5888 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
5889 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
);
5890 case PACKET_UNKNOWN
:
5893 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
5897 /* Store register REGNUM, or all registers if REGNUM == -1, from the
5898 contents of the register cache buffer. FIXME: ignores errors. */
5901 store_registers_using_G (const struct regcache
*regcache
)
5903 struct remote_state
*rs
= get_remote_state ();
5904 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5908 /* Extract all the registers in the regcache copying them into a
5913 regs
= alloca (rsa
->sizeof_g_packet
);
5914 memset (regs
, 0, rsa
->sizeof_g_packet
);
5915 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5917 struct packet_reg
*r
= &rsa
->regs
[i
];
5920 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
5924 /* Command describes registers byte by byte,
5925 each byte encoded as two hex characters. */
5928 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
5930 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
5932 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5933 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
5934 error (_("Could not write registers; remote failure reply '%s'"),
5938 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
5939 of the register cache buffer. FIXME: ignores errors. */
5942 remote_store_registers (struct target_ops
*ops
,
5943 struct regcache
*regcache
, int regnum
)
5945 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5948 set_general_thread (inferior_ptid
);
5952 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
5954 gdb_assert (reg
!= NULL
);
5956 /* Always prefer to store registers using the 'P' packet if
5957 possible; we often change only a small number of registers.
5958 Sometimes we change a larger number; we'd need help from a
5959 higher layer to know to use 'G'. */
5960 if (store_register_using_P (regcache
, reg
))
5963 /* For now, don't complain if we have no way to write the
5964 register. GDB loses track of unavailable registers too
5965 easily. Some day, this may be an error. We don't have
5966 any way to read the register, either... */
5967 if (!reg
->in_g_packet
)
5970 store_registers_using_G (regcache
);
5974 store_registers_using_G (regcache
);
5976 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
5977 if (!rsa
->regs
[i
].in_g_packet
)
5978 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
5979 /* See above for why we do not issue an error here. */
5984 /* Return the number of hex digits in num. */
5987 hexnumlen (ULONGEST num
)
5991 for (i
= 0; num
!= 0; i
++)
5997 /* Set BUF to the minimum number of hex digits representing NUM. */
6000 hexnumstr (char *buf
, ULONGEST num
)
6002 int len
= hexnumlen (num
);
6004 return hexnumnstr (buf
, num
, len
);
6008 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6011 hexnumnstr (char *buf
, ULONGEST num
, int width
)
6017 for (i
= width
- 1; i
>= 0; i
--)
6019 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
6026 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6029 remote_address_masked (CORE_ADDR addr
)
6031 int address_size
= remote_address_size
;
6033 /* If "remoteaddresssize" was not set, default to target address size. */
6035 address_size
= gdbarch_addr_bit (target_gdbarch
);
6037 if (address_size
> 0
6038 && address_size
< (sizeof (ULONGEST
) * 8))
6040 /* Only create a mask when that mask can safely be constructed
6041 in a ULONGEST variable. */
6044 mask
= (mask
<< address_size
) - 1;
6050 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
6051 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
6052 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
6053 (which may be more than *OUT_LEN due to escape characters). The
6054 total number of bytes in the output buffer will be at most
6058 remote_escape_output (const gdb_byte
*buffer
, int len
,
6059 gdb_byte
*out_buf
, int *out_len
,
6062 int input_index
, output_index
;
6065 for (input_index
= 0; input_index
< len
; input_index
++)
6067 gdb_byte b
= buffer
[input_index
];
6069 if (b
== '$' || b
== '#' || b
== '}')
6071 /* These must be escaped. */
6072 if (output_index
+ 2 > out_maxlen
)
6074 out_buf
[output_index
++] = '}';
6075 out_buf
[output_index
++] = b
^ 0x20;
6079 if (output_index
+ 1 > out_maxlen
)
6081 out_buf
[output_index
++] = b
;
6085 *out_len
= input_index
;
6086 return output_index
;
6089 /* Convert BUFFER, escaped data LEN bytes long, into binary data
6090 in OUT_BUF. Return the number of bytes written to OUT_BUF.
6091 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
6093 This function reverses remote_escape_output. It allows more
6094 escaped characters than that function does, in particular because
6095 '*' must be escaped to avoid the run-length encoding processing
6096 in reading packets. */
6099 remote_unescape_input (const gdb_byte
*buffer
, int len
,
6100 gdb_byte
*out_buf
, int out_maxlen
)
6102 int input_index
, output_index
;
6107 for (input_index
= 0; input_index
< len
; input_index
++)
6109 gdb_byte b
= buffer
[input_index
];
6111 if (output_index
+ 1 > out_maxlen
)
6113 warning (_("Received too much data from remote target;"
6114 " ignoring overflow."));
6115 return output_index
;
6120 out_buf
[output_index
++] = b
^ 0x20;
6126 out_buf
[output_index
++] = b
;
6130 error (_("Unmatched escape character in target response."));
6132 return output_index
;
6135 /* Determine whether the remote target supports binary downloading.
6136 This is accomplished by sending a no-op memory write of zero length
6137 to the target at the specified address. It does not suffice to send
6138 the whole packet, since many stubs strip the eighth bit and
6139 subsequently compute a wrong checksum, which causes real havoc with
6142 NOTE: This can still lose if the serial line is not eight-bit
6143 clean. In cases like this, the user should clear "remote
6147 check_binary_download (CORE_ADDR addr
)
6149 struct remote_state
*rs
= get_remote_state ();
6151 switch (remote_protocol_packets
[PACKET_X
].support
)
6153 case PACKET_DISABLE
:
6157 case PACKET_SUPPORT_UNKNOWN
:
6163 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6165 p
+= hexnumstr (p
, (ULONGEST
) 0);
6169 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6170 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6172 if (rs
->buf
[0] == '\0')
6175 fprintf_unfiltered (gdb_stdlog
,
6176 "binary downloading NOT suppported by target\n");
6177 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
6182 fprintf_unfiltered (gdb_stdlog
,
6183 "binary downloading suppported by target\n");
6184 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
6191 /* Write memory data directly to the remote machine.
6192 This does not inform the data cache; the data cache uses this.
6193 HEADER is the starting part of the packet.
6194 MEMADDR is the address in the remote memory space.
6195 MYADDR is the address of the buffer in our space.
6196 LEN is the number of bytes.
6197 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6198 should send data as binary ('X'), or hex-encoded ('M').
6200 The function creates packet of the form
6201 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6203 where encoding of <DATA> is termined by PACKET_FORMAT.
6205 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6208 Returns the number of bytes transferred, or 0 (setting errno) for
6209 error. Only transfer a single packet. */
6212 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
6213 const gdb_byte
*myaddr
, int len
,
6214 char packet_format
, int use_length
)
6216 struct remote_state
*rs
= get_remote_state ();
6226 if (packet_format
!= 'X' && packet_format
!= 'M')
6227 internal_error (__FILE__
, __LINE__
,
6228 "remote_write_bytes_aux: bad packet format");
6233 payload_size
= get_memory_write_packet_size ();
6235 /* The packet buffer will be large enough for the payload;
6236 get_memory_packet_size ensures this. */
6239 /* Compute the size of the actual payload by subtracting out the
6240 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
6242 payload_size
-= strlen ("$,:#NN");
6244 /* The comma won't be used. */
6246 header_length
= strlen (header
);
6247 payload_size
-= header_length
;
6248 payload_size
-= hexnumlen (memaddr
);
6250 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6252 strcat (rs
->buf
, header
);
6253 p
= rs
->buf
+ strlen (header
);
6255 /* Compute a best guess of the number of bytes actually transfered. */
6256 if (packet_format
== 'X')
6258 /* Best guess at number of bytes that will fit. */
6259 todo
= min (len
, payload_size
);
6261 payload_size
-= hexnumlen (todo
);
6262 todo
= min (todo
, payload_size
);
6266 /* Num bytes that will fit. */
6267 todo
= min (len
, payload_size
/ 2);
6269 payload_size
-= hexnumlen (todo
);
6270 todo
= min (todo
, payload_size
/ 2);
6274 internal_error (__FILE__
, __LINE__
,
6275 _("minumum packet size too small to write data"));
6277 /* If we already need another packet, then try to align the end
6278 of this packet to a useful boundary. */
6279 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
6280 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
6282 /* Append "<memaddr>". */
6283 memaddr
= remote_address_masked (memaddr
);
6284 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6291 /* Append <len>. Retain the location/size of <len>. It may need to
6292 be adjusted once the packet body has been created. */
6294 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
6302 /* Append the packet body. */
6303 if (packet_format
== 'X')
6305 /* Binary mode. Send target system values byte by byte, in
6306 increasing byte addresses. Only escape certain critical
6308 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
6311 /* If not all TODO bytes fit, then we'll need another packet. Make
6312 a second try to keep the end of the packet aligned. Don't do
6313 this if the packet is tiny. */
6314 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
6318 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
6320 if (new_nr_bytes
!= nr_bytes
)
6321 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
6326 p
+= payload_length
;
6327 if (use_length
&& nr_bytes
< todo
)
6329 /* Escape chars have filled up the buffer prematurely,
6330 and we have actually sent fewer bytes than planned.
6331 Fix-up the length field of the packet. Use the same
6332 number of characters as before. */
6333 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
6334 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
6339 /* Normal mode: Send target system values byte by byte, in
6340 increasing byte addresses. Each byte is encoded as a two hex
6342 nr_bytes
= bin2hex (myaddr
, p
, todo
);
6346 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6347 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6349 if (rs
->buf
[0] == 'E')
6351 /* There is no correspondance between what the remote protocol
6352 uses for errors and errno codes. We would like a cleaner way
6353 of representing errors (big enough to include errno codes,
6354 bfd_error codes, and others). But for now just return EIO. */
6359 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6360 fewer bytes than we'd planned. */
6364 /* Write memory data directly to the remote machine.
6365 This does not inform the data cache; the data cache uses this.
6366 MEMADDR is the address in the remote memory space.
6367 MYADDR is the address of the buffer in our space.
6368 LEN is the number of bytes.
6370 Returns number of bytes transferred, or 0 (setting errno) for
6371 error. Only transfer a single packet. */
6374 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
6376 char *packet_format
= 0;
6378 /* Check whether the target supports binary download. */
6379 check_binary_download (memaddr
);
6381 switch (remote_protocol_packets
[PACKET_X
].support
)
6384 packet_format
= "X";
6386 case PACKET_DISABLE
:
6387 packet_format
= "M";
6389 case PACKET_SUPPORT_UNKNOWN
:
6390 internal_error (__FILE__
, __LINE__
,
6391 _("remote_write_bytes: bad internal state"));
6393 internal_error (__FILE__
, __LINE__
, _("bad switch"));
6396 return remote_write_bytes_aux (packet_format
,
6397 memaddr
, myaddr
, len
, packet_format
[0], 1);
6400 /* Read memory data directly from the remote machine.
6401 This does not use the data cache; the data cache uses this.
6402 MEMADDR is the address in the remote memory space.
6403 MYADDR is the address of the buffer in our space.
6404 LEN is the number of bytes.
6406 Returns number of bytes transferred, or 0 for error. */
6408 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
6409 remote targets) shouldn't attempt to read the entire buffer.
6410 Instead it should read a single packet worth of data and then
6411 return the byte size of that packet to the caller. The caller (its
6412 caller and its callers caller ;-) already contains code for
6413 handling partial reads. */
6416 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
6418 struct remote_state
*rs
= get_remote_state ();
6419 int max_buf_size
; /* Max size of packet output buffer. */
6425 max_buf_size
= get_memory_read_packet_size ();
6426 /* The packet buffer will be large enough for the payload;
6427 get_memory_packet_size ensures this. */
6436 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
6438 /* construct "m"<memaddr>","<len>" */
6439 /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */
6440 memaddr
= remote_address_masked (memaddr
);
6443 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6445 p
+= hexnumstr (p
, (ULONGEST
) todo
);
6449 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6451 if (rs
->buf
[0] == 'E'
6452 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
6453 && rs
->buf
[3] == '\0')
6455 /* There is no correspondance between what the remote
6456 protocol uses for errors and errno codes. We would like
6457 a cleaner way of representing errors (big enough to
6458 include errno codes, bfd_error codes, and others). But
6459 for now just return EIO. */
6464 /* Reply describes memory byte by byte,
6465 each byte encoded as two hex characters. */
6468 if ((i
= hex2bin (p
, myaddr
, todo
)) < todo
)
6470 /* Reply is short. This means that we were able to read
6471 only part of what we wanted to. */
6472 return i
+ (origlen
- len
);
6482 /* Remote notification handler. */
6485 handle_notification (char *buf
, size_t length
)
6487 if (strncmp (buf
, "Stop:", 5) == 0)
6489 if (pending_stop_reply
)
6491 /* We've already parsed the in-flight stop-reply, but the
6492 stub for some reason thought we didn't, possibly due to
6493 timeout on its side. Just ignore it. */
6495 fprintf_unfiltered (gdb_stdlog
, "ignoring resent notification\n");
6499 struct cleanup
*old_chain
;
6500 struct stop_reply
*reply
= stop_reply_xmalloc ();
6502 old_chain
= make_cleanup (do_stop_reply_xfree
, reply
);
6504 remote_parse_stop_reply (buf
+ 5, reply
);
6506 discard_cleanups (old_chain
);
6508 /* Be careful to only set it after parsing, since an error
6509 may be thrown then. */
6510 pending_stop_reply
= reply
;
6512 /* Notify the event loop there's a stop reply to acknowledge
6513 and that there may be more events to fetch. */
6514 mark_async_event_handler (remote_async_get_pending_events_token
);
6517 fprintf_unfiltered (gdb_stdlog
, "stop notification captured\n");
6521 /* We ignore notifications we don't recognize, for compatibility
6522 with newer stubs. */
6527 /* Read or write LEN bytes from inferior memory at MEMADDR,
6528 transferring to or from debugger address BUFFER. Write to inferior
6529 if SHOULD_WRITE is nonzero. Returns length of data written or
6530 read; 0 for error. TARGET is unused. */
6533 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
6534 int should_write
, struct mem_attrib
*attrib
,
6535 struct target_ops
*target
)
6539 set_general_thread (inferior_ptid
);
6542 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
6544 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
6549 /* Sends a packet with content determined by the printf format string
6550 FORMAT and the remaining arguments, then gets the reply. Returns
6551 whether the packet was a success, a failure, or unknown. */
6553 static enum packet_result
6554 remote_send_printf (const char *format
, ...)
6556 struct remote_state
*rs
= get_remote_state ();
6557 int max_size
= get_remote_packet_size ();
6560 va_start (ap
, format
);
6563 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
6564 internal_error (__FILE__
, __LINE__
, "Too long remote packet.");
6566 if (putpkt (rs
->buf
) < 0)
6567 error (_("Communication problem with target."));
6570 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6572 return packet_check_result (rs
->buf
);
6576 restore_remote_timeout (void *p
)
6578 int value
= *(int *)p
;
6580 remote_timeout
= value
;
6583 /* Flash writing can take quite some time. We'll set
6584 effectively infinite timeout for flash operations.
6585 In future, we'll need to decide on a better approach. */
6586 static const int remote_flash_timeout
= 1000;
6589 remote_flash_erase (struct target_ops
*ops
,
6590 ULONGEST address
, LONGEST length
)
6592 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
6593 int saved_remote_timeout
= remote_timeout
;
6594 enum packet_result ret
;
6595 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6596 &saved_remote_timeout
);
6598 remote_timeout
= remote_flash_timeout
;
6600 ret
= remote_send_printf ("vFlashErase:%s,%s",
6601 phex (address
, addr_size
),
6605 case PACKET_UNKNOWN
:
6606 error (_("Remote target does not support flash erase"));
6608 error (_("Error erasing flash with vFlashErase packet"));
6613 do_cleanups (back_to
);
6617 remote_flash_write (struct target_ops
*ops
,
6618 ULONGEST address
, LONGEST length
,
6619 const gdb_byte
*data
)
6621 int saved_remote_timeout
= remote_timeout
;
6623 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6624 &saved_remote_timeout
);
6626 remote_timeout
= remote_flash_timeout
;
6627 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
6628 do_cleanups (back_to
);
6634 remote_flash_done (struct target_ops
*ops
)
6636 int saved_remote_timeout
= remote_timeout
;
6638 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6639 &saved_remote_timeout
);
6641 remote_timeout
= remote_flash_timeout
;
6642 ret
= remote_send_printf ("vFlashDone");
6643 do_cleanups (back_to
);
6647 case PACKET_UNKNOWN
:
6648 error (_("Remote target does not support vFlashDone"));
6650 error (_("Error finishing flash operation"));
6657 remote_files_info (struct target_ops
*ignore
)
6659 puts_filtered ("Debugging a target over a serial line.\n");
6662 /* Stuff for dealing with the packets which are part of this protocol.
6663 See comment at top of file for details. */
6665 /* Read a single character from the remote end. */
6668 readchar (int timeout
)
6672 ch
= serial_readchar (remote_desc
, timeout
);
6677 switch ((enum serial_rc
) ch
)
6681 error (_("Remote connection closed"));
6685 perror_with_name (_("Remote communication error. Target disconnected."));
6687 case SERIAL_TIMEOUT
:
6693 /* Send the command in *BUF to the remote machine, and read the reply
6694 into *BUF. Report an error if we get an error reply. Resize
6695 *BUF using xrealloc if necessary to hold the result, and update
6699 remote_send (char **buf
,
6703 getpkt (buf
, sizeof_buf
, 0);
6705 if ((*buf
)[0] == 'E')
6706 error (_("Remote failure reply: %s"), *buf
);
6709 /* Return a pointer to an xmalloc'ed string representing an escaped
6710 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
6711 etc. The caller is responsible for releasing the returned
6715 escape_buffer (const char *buf
, int n
)
6717 struct cleanup
*old_chain
;
6718 struct ui_file
*stb
;
6721 stb
= mem_fileopen ();
6722 old_chain
= make_cleanup_ui_file_delete (stb
);
6724 fputstrn_unfiltered (buf
, n
, 0, stb
);
6725 str
= ui_file_xstrdup (stb
, NULL
);
6726 do_cleanups (old_chain
);
6730 /* Display a null-terminated packet on stdout, for debugging, using C
6734 print_packet (char *buf
)
6736 puts_filtered ("\"");
6737 fputstr_filtered (buf
, '"', gdb_stdout
);
6738 puts_filtered ("\"");
6744 return putpkt_binary (buf
, strlen (buf
));
6747 /* Send a packet to the remote machine, with error checking. The data
6748 of the packet is in BUF. The string in BUF can be at most
6749 get_remote_packet_size () - 5 to account for the $, # and checksum,
6750 and for a possible /0 if we are debugging (remote_debug) and want
6751 to print the sent packet as a string. */
6754 putpkt_binary (char *buf
, int cnt
)
6756 struct remote_state
*rs
= get_remote_state ();
6758 unsigned char csum
= 0;
6759 char *buf2
= alloca (cnt
+ 6);
6765 /* Catch cases like trying to read memory or listing threads while
6766 we're waiting for a stop reply. The remote server wouldn't be
6767 ready to handle this request, so we'd hang and timeout. We don't
6768 have to worry about this in synchronous mode, because in that
6769 case it's not possible to issue a command while the target is
6770 running. This is not a problem in non-stop mode, because in that
6771 case, the stub is always ready to process serial input. */
6772 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
6773 error (_("Cannot execute this command while the target is running."));
6775 /* We're sending out a new packet. Make sure we don't look at a
6776 stale cached response. */
6777 rs
->cached_wait_status
= 0;
6779 /* Copy the packet into buffer BUF2, encapsulating it
6780 and giving it a checksum. */
6785 for (i
= 0; i
< cnt
; i
++)
6791 *p
++ = tohex ((csum
>> 4) & 0xf);
6792 *p
++ = tohex (csum
& 0xf);
6794 /* Send it over and over until we get a positive ack. */
6798 int started_error_output
= 0;
6802 struct cleanup
*old_chain
;
6806 str
= escape_buffer (buf2
, p
- buf2
);
6807 old_chain
= make_cleanup (xfree
, str
);
6808 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", str
);
6809 gdb_flush (gdb_stdlog
);
6810 do_cleanups (old_chain
);
6812 if (serial_write (remote_desc
, buf2
, p
- buf2
))
6813 perror_with_name (_("putpkt: write failed"));
6815 /* If this is a no acks version of the remote protocol, send the
6816 packet and move on. */
6820 /* Read until either a timeout occurs (-2) or '+' is read.
6821 Handle any notification that arrives in the mean time. */
6824 ch
= readchar (remote_timeout
);
6832 case SERIAL_TIMEOUT
:
6835 if (started_error_output
)
6837 putchar_unfiltered ('\n');
6838 started_error_output
= 0;
6847 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
6851 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
6852 case SERIAL_TIMEOUT
:
6856 break; /* Retransmit buffer. */
6860 fprintf_unfiltered (gdb_stdlog
,
6861 "Packet instead of Ack, ignoring it\n");
6862 /* It's probably an old response sent because an ACK
6863 was lost. Gobble up the packet and ack it so it
6864 doesn't get retransmitted when we resend this
6867 serial_write (remote_desc
, "+", 1);
6868 continue; /* Now, go look for +. */
6875 /* If we got a notification, handle it, and go back to looking
6877 /* We've found the start of a notification. Now
6878 collect the data. */
6879 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
6884 struct cleanup
*old_chain
;
6887 str
= escape_buffer (rs
->buf
, val
);
6888 old_chain
= make_cleanup (xfree
, str
);
6889 fprintf_unfiltered (gdb_stdlog
,
6890 " Notification received: %s\n",
6892 do_cleanups (old_chain
);
6894 handle_notification (rs
->buf
, val
);
6895 /* We're in sync now, rewait for the ack. */
6902 if (!started_error_output
)
6904 started_error_output
= 1;
6905 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6907 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6908 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
6917 if (!started_error_output
)
6919 started_error_output
= 1;
6920 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
6922 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
6926 break; /* Here to retransmit. */
6930 /* This is wrong. If doing a long backtrace, the user should be
6931 able to get out next time we call QUIT, without anything as
6932 violent as interrupt_query. If we want to provide a way out of
6933 here without getting to the next QUIT, it should be based on
6934 hitting ^C twice as in remote_wait. */
6945 /* Come here after finding the start of a frame when we expected an
6946 ack. Do our best to discard the rest of this packet. */
6955 c
= readchar (remote_timeout
);
6958 case SERIAL_TIMEOUT
:
6959 /* Nothing we can do. */
6962 /* Discard the two bytes of checksum and stop. */
6963 c
= readchar (remote_timeout
);
6965 c
= readchar (remote_timeout
);
6968 case '*': /* Run length encoding. */
6969 /* Discard the repeat count. */
6970 c
= readchar (remote_timeout
);
6975 /* A regular character. */
6981 /* Come here after finding the start of the frame. Collect the rest
6982 into *BUF, verifying the checksum, length, and handling run-length
6983 compression. NUL terminate the buffer. If there is not enough room,
6984 expand *BUF using xrealloc.
6986 Returns -1 on error, number of characters in buffer (ignoring the
6987 trailing NULL) on success. (could be extended to return one of the
6988 SERIAL status indications). */
6991 read_frame (char **buf_p
,
6998 struct remote_state
*rs
= get_remote_state ();
7005 c
= readchar (remote_timeout
);
7008 case SERIAL_TIMEOUT
:
7010 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
7014 fputs_filtered ("Saw new packet start in middle of old one\n",
7016 return -1; /* Start a new packet, count retries. */
7019 unsigned char pktcsum
;
7025 check_0
= readchar (remote_timeout
);
7027 check_1
= readchar (remote_timeout
);
7029 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
7032 fputs_filtered ("Timeout in checksum, retrying\n",
7036 else if (check_0
< 0 || check_1
< 0)
7039 fputs_filtered ("Communication error in checksum\n",
7044 /* Don't recompute the checksum; with no ack packets we
7045 don't have any way to indicate a packet retransmission
7050 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
7051 if (csum
== pktcsum
)
7056 struct cleanup
*old_chain
;
7059 str
= escape_buffer (buf
, bc
);
7060 old_chain
= make_cleanup (xfree
, str
);
7061 fprintf_unfiltered (gdb_stdlog
,
7063 Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
7064 pktcsum
, csum
, str
);
7065 do_cleanups (old_chain
);
7067 /* Number of characters in buffer ignoring trailing
7071 case '*': /* Run length encoding. */
7076 c
= readchar (remote_timeout
);
7078 repeat
= c
- ' ' + 3; /* Compute repeat count. */
7080 /* The character before ``*'' is repeated. */
7082 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
7084 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
7086 /* Make some more room in the buffer. */
7087 *sizeof_buf
+= repeat
;
7088 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7092 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
7098 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
7102 if (bc
>= *sizeof_buf
- 1)
7104 /* Make some more room in the buffer. */
7106 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7117 /* Read a packet from the remote machine, with error checking, and
7118 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7119 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7120 rather than timing out; this is used (in synchronous mode) to wait
7121 for a target that is is executing user code to stop. */
7122 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7123 don't have to change all the calls to getpkt to deal with the
7124 return value, because at the moment I don't know what the right
7125 thing to do it for those. */
7133 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
7137 /* Read a packet from the remote machine, with error checking, and
7138 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7139 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7140 rather than timing out; this is used (in synchronous mode) to wait
7141 for a target that is is executing user code to stop. If FOREVER ==
7142 0, this function is allowed to time out gracefully and return an
7143 indication of this to the caller. Otherwise return the number of
7144 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7145 enough reason to return to the caller. */
7148 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
7149 int expecting_notif
)
7151 struct remote_state
*rs
= get_remote_state ();
7157 /* We're reading a new response. Make sure we don't look at a
7158 previously cached response. */
7159 rs
->cached_wait_status
= 0;
7161 strcpy (*buf
, "timeout");
7164 timeout
= watchdog
> 0 ? watchdog
: -1;
7165 else if (expecting_notif
)
7166 timeout
= 0; /* There should already be a char in the buffer. If
7169 timeout
= remote_timeout
;
7173 /* Process any number of notifications, and then return when
7177 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
7179 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
7181 /* This can loop forever if the remote side sends us
7182 characters continuously, but if it pauses, we'll get
7183 SERIAL_TIMEOUT from readchar because of timeout. Then
7184 we'll count that as a retry.
7186 Note that even when forever is set, we will only wait
7187 forever prior to the start of a packet. After that, we
7188 expect characters to arrive at a brisk pace. They should
7189 show up within remote_timeout intervals. */
7191 c
= readchar (timeout
);
7192 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
7194 if (c
== SERIAL_TIMEOUT
)
7196 if (expecting_notif
)
7197 return -1; /* Don't complain, it's normal to not get
7198 anything in this case. */
7200 if (forever
) /* Watchdog went off? Kill the target. */
7204 error (_("Watchdog timeout has expired. Target detached."));
7207 fputs_filtered ("Timed out.\n", gdb_stdlog
);
7211 /* We've found the start of a packet or notification.
7212 Now collect the data. */
7213 val
= read_frame (buf
, sizeof_buf
);
7218 serial_write (remote_desc
, "-", 1);
7221 if (tries
> MAX_TRIES
)
7223 /* We have tried hard enough, and just can't receive the
7224 packet/notification. Give up. */
7225 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7227 /* Skip the ack char if we're in no-ack mode. */
7228 if (!rs
->noack_mode
)
7229 serial_write (remote_desc
, "+", 1);
7233 /* If we got an ordinary packet, return that to our caller. */
7238 struct cleanup
*old_chain
;
7241 str
= escape_buffer (*buf
, val
);
7242 old_chain
= make_cleanup (xfree
, str
);
7243 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", str
);
7244 do_cleanups (old_chain
);
7247 /* Skip the ack char if we're in no-ack mode. */
7248 if (!rs
->noack_mode
)
7249 serial_write (remote_desc
, "+", 1);
7253 /* If we got a notification, handle it, and go back to looking
7257 gdb_assert (c
== '%');
7261 struct cleanup
*old_chain
;
7264 str
= escape_buffer (*buf
, val
);
7265 old_chain
= make_cleanup (xfree
, str
);
7266 fprintf_unfiltered (gdb_stdlog
,
7267 " Notification received: %s\n",
7269 do_cleanups (old_chain
);
7272 handle_notification (*buf
, val
);
7274 /* Notifications require no acknowledgement. */
7276 if (expecting_notif
)
7283 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
7285 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0);
7289 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
)
7291 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1);
7296 remote_kill (struct target_ops
*ops
)
7298 /* Use catch_errors so the user can quit from gdb even when we
7299 aren't on speaking terms with the remote system. */
7300 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
7302 /* Don't wait for it to die. I'm not really sure it matters whether
7303 we do or not. For the existing stubs, kill is a noop. */
7304 target_mourn_inferior ();
7308 remote_vkill (int pid
, struct remote_state
*rs
)
7310 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7313 /* Tell the remote target to detach. */
7314 sprintf (rs
->buf
, "vKill;%x", pid
);
7316 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7318 if (packet_ok (rs
->buf
,
7319 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
7321 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7328 extended_remote_kill (struct target_ops
*ops
)
7331 int pid
= ptid_get_pid (inferior_ptid
);
7332 struct remote_state
*rs
= get_remote_state ();
7334 res
= remote_vkill (pid
, rs
);
7335 if (res
== -1 && !remote_multi_process_p (rs
))
7337 /* Don't try 'k' on a multi-process aware stub -- it has no way
7338 to specify the pid. */
7342 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7343 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
7346 /* Don't wait for it to die. I'm not really sure it matters whether
7347 we do or not. For the existing stubs, kill is a noop. */
7353 error (_("Can't kill process"));
7355 target_mourn_inferior ();
7359 remote_mourn (struct target_ops
*ops
)
7361 remote_mourn_1 (ops
);
7364 /* Worker function for remote_mourn. */
7366 remote_mourn_1 (struct target_ops
*target
)
7368 unpush_target (target
);
7370 /* remote_close takes care of doing most of the clean up. */
7371 generic_mourn_inferior ();
7375 extended_remote_mourn_1 (struct target_ops
*target
)
7377 struct remote_state
*rs
= get_remote_state ();
7379 /* In case we got here due to an error, but we're going to stay
7381 rs
->waiting_for_stop_reply
= 0;
7383 /* We're no longer interested in these events. */
7384 discard_pending_stop_replies (ptid_get_pid (inferior_ptid
));
7386 /* If the current general thread belonged to the process we just
7387 detached from or has exited, the remote side current general
7388 thread becomes undefined. Considering a case like this:
7390 - We just got here due to a detach.
7391 - The process that we're detaching from happens to immediately
7392 report a global breakpoint being hit in non-stop mode, in the
7393 same thread we had selected before.
7394 - GDB attaches to this process again.
7395 - This event happens to be the next event we handle.
7397 GDB would consider that the current general thread didn't need to
7398 be set on the stub side (with Hg), since for all it knew,
7399 GENERAL_THREAD hadn't changed.
7401 Notice that although in all-stop mode, the remote server always
7402 sets the current thread to the thread reporting the stop event,
7403 that doesn't happen in non-stop mode; in non-stop, the stub *must
7404 not* change the current thread when reporting a breakpoint hit,
7405 due to the decoupling of event reporting and event handling.
7407 To keep things simple, we always invalidate our notion of the
7409 record_currthread (minus_one_ptid
);
7411 /* Unlike "target remote", we do not want to unpush the target; then
7412 the next time the user says "run", we won't be connected. */
7414 /* Call common code to mark the inferior as not running. */
7415 generic_mourn_inferior ();
7417 if (!have_inferiors ())
7419 if (!remote_multi_process_p (rs
))
7421 /* Check whether the target is running now - some remote stubs
7422 automatically restart after kill. */
7424 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7426 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
7428 /* Assume that the target has been restarted. Set inferior_ptid
7429 so that bits of core GDB realizes there's something here, e.g.,
7430 so that the user can say "kill" again. */
7431 inferior_ptid
= magic_null_ptid
;
7438 extended_remote_mourn (struct target_ops
*ops
)
7440 extended_remote_mourn_1 (ops
);
7444 extended_remote_run (char *args
)
7446 struct remote_state
*rs
= get_remote_state ();
7449 /* If the user has disabled vRun support, or we have detected that
7450 support is not available, do not try it. */
7451 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7454 strcpy (rs
->buf
, "vRun;");
7455 len
= strlen (rs
->buf
);
7457 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
7458 error (_("Remote file name too long for run packet"));
7459 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
, 0);
7461 gdb_assert (args
!= NULL
);
7464 struct cleanup
*back_to
;
7468 argv
= gdb_buildargv (args
);
7469 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
7470 for (i
= 0; argv
[i
] != NULL
; i
++)
7472 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
7473 error (_("Argument list too long for run packet"));
7474 rs
->buf
[len
++] = ';';
7475 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
, 0);
7477 do_cleanups (back_to
);
7480 rs
->buf
[len
++] = '\0';
7483 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7485 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
7487 /* We have a wait response; we don't need it, though. All is well. */
7490 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7491 /* It wasn't disabled before, but it is now. */
7495 if (remote_exec_file
[0] == '\0')
7496 error (_("Running the default executable on the remote target failed; "
7497 "try \"set remote exec-file\"?"));
7499 error (_("Running \"%s\" on the remote target failed"),
7504 /* In the extended protocol we want to be able to do things like
7505 "run" and have them basically work as expected. So we need
7506 a special create_inferior function. We support changing the
7507 executable file and the command line arguments, but not the
7511 extended_remote_create_inferior_1 (char *exec_file
, char *args
,
7512 char **env
, int from_tty
)
7514 /* If running asynchronously, register the target file descriptor
7515 with the event loop. */
7516 if (target_can_async_p ())
7517 target_async (inferior_event_handler
, 0);
7519 /* Now restart the remote server. */
7520 if (extended_remote_run (args
) == -1)
7522 /* vRun was not supported. Fail if we need it to do what the
7524 if (remote_exec_file
[0])
7525 error (_("Remote target does not support \"set remote exec-file\""));
7527 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7529 /* Fall back to "R". */
7530 extended_remote_restart ();
7533 if (!have_inferiors ())
7535 /* Clean up from the last time we ran, before we mark the target
7536 running again. This will mark breakpoints uninserted, and
7537 get_offsets may insert breakpoints. */
7538 init_thread_list ();
7539 init_wait_for_inferior ();
7542 /* Now mark the inferior as running before we do anything else. */
7543 inferior_ptid
= magic_null_ptid
;
7545 /* Now, if we have thread information, update inferior_ptid. */
7546 inferior_ptid
= remote_current_thread (inferior_ptid
);
7548 remote_add_inferior (ptid_get_pid (inferior_ptid
), 0);
7549 add_thread_silent (inferior_ptid
);
7551 /* Get updated offsets, if the stub uses qOffsets. */
7556 extended_remote_create_inferior (struct target_ops
*ops
,
7557 char *exec_file
, char *args
,
7558 char **env
, int from_tty
)
7560 extended_remote_create_inferior_1 (exec_file
, args
, env
, from_tty
);
7564 /* Insert a breakpoint. On targets that have software breakpoint
7565 support, we ask the remote target to do the work; on targets
7566 which don't, we insert a traditional memory breakpoint. */
7569 remote_insert_breakpoint (struct gdbarch
*gdbarch
,
7570 struct bp_target_info
*bp_tgt
)
7572 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
7573 If it succeeds, then set the support to PACKET_ENABLE. If it
7574 fails, and the user has explicitly requested the Z support then
7575 report an error, otherwise, mark it disabled and go on. */
7577 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
7579 CORE_ADDR addr
= bp_tgt
->placed_address
;
7580 struct remote_state
*rs
;
7584 gdbarch_remote_breakpoint_from_pc (gdbarch
, &addr
, &bpsize
);
7586 rs
= get_remote_state ();
7592 addr
= (ULONGEST
) remote_address_masked (addr
);
7593 p
+= hexnumstr (p
, addr
);
7594 sprintf (p
, ",%d", bpsize
);
7597 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7599 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
7604 bp_tgt
->placed_address
= addr
;
7605 bp_tgt
->placed_size
= bpsize
;
7607 case PACKET_UNKNOWN
:
7612 return memory_insert_breakpoint (gdbarch
, bp_tgt
);
7616 remote_remove_breakpoint (struct gdbarch
*gdbarch
,
7617 struct bp_target_info
*bp_tgt
)
7619 CORE_ADDR addr
= bp_tgt
->placed_address
;
7620 struct remote_state
*rs
= get_remote_state ();
7622 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
7630 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
7631 p
+= hexnumstr (p
, addr
);
7632 sprintf (p
, ",%d", bp_tgt
->placed_size
);
7635 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7637 return (rs
->buf
[0] == 'E');
7640 return memory_remove_breakpoint (gdbarch
, bp_tgt
);
7644 watchpoint_to_Z_packet (int type
)
7649 return Z_PACKET_WRITE_WP
;
7652 return Z_PACKET_READ_WP
;
7655 return Z_PACKET_ACCESS_WP
;
7658 internal_error (__FILE__
, __LINE__
,
7659 _("hw_bp_to_z: bad watchpoint type %d"), type
);
7664 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
,
7665 struct expression
*cond
)
7667 struct remote_state
*rs
= get_remote_state ();
7669 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
7671 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
7674 sprintf (rs
->buf
, "Z%x,", packet
);
7675 p
= strchr (rs
->buf
, '\0');
7676 addr
= remote_address_masked (addr
);
7677 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7678 sprintf (p
, ",%x", len
);
7681 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7683 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
7687 case PACKET_UNKNOWN
:
7692 internal_error (__FILE__
, __LINE__
,
7693 _("remote_insert_watchpoint: reached end of function"));
7698 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
,
7699 struct expression
*cond
)
7701 struct remote_state
*rs
= get_remote_state ();
7703 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
7705 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
7708 sprintf (rs
->buf
, "z%x,", packet
);
7709 p
= strchr (rs
->buf
, '\0');
7710 addr
= remote_address_masked (addr
);
7711 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7712 sprintf (p
, ",%x", len
);
7714 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7716 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
7719 case PACKET_UNKNOWN
:
7724 internal_error (__FILE__
, __LINE__
,
7725 _("remote_remove_watchpoint: reached end of function"));
7729 int remote_hw_watchpoint_limit
= -1;
7730 int remote_hw_breakpoint_limit
= -1;
7733 remote_check_watch_resources (int type
, int cnt
, int ot
)
7735 if (type
== bp_hardware_breakpoint
)
7737 if (remote_hw_breakpoint_limit
== 0)
7739 else if (remote_hw_breakpoint_limit
< 0)
7741 else if (cnt
<= remote_hw_breakpoint_limit
)
7746 if (remote_hw_watchpoint_limit
== 0)
7748 else if (remote_hw_watchpoint_limit
< 0)
7752 else if (cnt
<= remote_hw_watchpoint_limit
)
7759 remote_stopped_by_watchpoint (void)
7761 return remote_stopped_by_watchpoint_p
;
7765 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
7769 if (remote_stopped_by_watchpoint ())
7771 *addr_p
= remote_watch_data_address
;
7780 remote_insert_hw_breakpoint (struct gdbarch
*gdbarch
,
7781 struct bp_target_info
*bp_tgt
)
7784 struct remote_state
*rs
;
7787 /* The length field should be set to the size of a breakpoint
7788 instruction, even though we aren't inserting one ourselves. */
7790 gdbarch_remote_breakpoint_from_pc
7791 (gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
7793 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
7796 rs
= get_remote_state ();
7803 addr
= remote_address_masked (bp_tgt
->placed_address
);
7804 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7805 sprintf (p
, ",%x", bp_tgt
->placed_size
);
7808 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7810 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
7813 case PACKET_UNKNOWN
:
7818 internal_error (__FILE__
, __LINE__
,
7819 _("remote_insert_hw_breakpoint: reached end of function"));
7824 remote_remove_hw_breakpoint (struct gdbarch
*gdbarch
,
7825 struct bp_target_info
*bp_tgt
)
7828 struct remote_state
*rs
= get_remote_state ();
7831 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
7838 addr
= remote_address_masked (bp_tgt
->placed_address
);
7839 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7840 sprintf (p
, ",%x", bp_tgt
->placed_size
);
7843 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7845 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
7848 case PACKET_UNKNOWN
:
7853 internal_error (__FILE__
, __LINE__
,
7854 _("remote_remove_hw_breakpoint: reached end of function"));
7857 /* Table used by the crc32 function to calcuate the checksum. */
7859 static unsigned long crc32_table
[256] =
7862 static unsigned long
7863 crc32 (const unsigned char *buf
, int len
, unsigned int crc
)
7865 if (!crc32_table
[1])
7867 /* Initialize the CRC table and the decoding table. */
7871 for (i
= 0; i
< 256; i
++)
7873 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
7874 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
7881 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
7887 /* Verify memory using the "qCRC:" request. */
7890 remote_verify_memory (struct target_ops
*ops
,
7891 const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
7893 struct remote_state
*rs
= get_remote_state ();
7894 unsigned long host_crc
, target_crc
;
7897 /* FIXME: assumes lma can fit into long. */
7898 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
7899 (long) lma
, (long) size
);
7902 /* Be clever; compute the host_crc before waiting for target
7904 host_crc
= crc32 (data
, size
, 0xffffffff);
7906 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7907 if (rs
->buf
[0] == 'E')
7910 if (rs
->buf
[0] != 'C')
7911 error (_("remote target does not support this operation"));
7913 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
7914 target_crc
= target_crc
* 16 + fromhex (*tmp
);
7916 return (host_crc
== target_crc
);
7919 /* compare-sections command
7921 With no arguments, compares each loadable section in the exec bfd
7922 with the same memory range on the target, and reports mismatches.
7923 Useful for verifying the image on the target against the exec file. */
7926 compare_sections_command (char *args
, int from_tty
)
7929 struct cleanup
*old_chain
;
7931 const char *sectname
;
7939 error (_("command cannot be used without an exec file"));
7941 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
7943 if (!(s
->flags
& SEC_LOAD
))
7944 continue; /* skip non-loadable section */
7946 size
= bfd_get_section_size (s
);
7948 continue; /* skip zero-length section */
7950 sectname
= bfd_get_section_name (exec_bfd
, s
);
7951 if (args
&& strcmp (args
, sectname
) != 0)
7952 continue; /* not the section selected by user */
7954 matched
= 1; /* do this section */
7957 sectdata
= xmalloc (size
);
7958 old_chain
= make_cleanup (xfree
, sectdata
);
7959 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
7961 res
= target_verify_memory (sectdata
, lma
, size
);
7964 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
7965 paddress (target_gdbarch
, lma
),
7966 paddress (target_gdbarch
, lma
+ size
));
7968 printf_filtered ("Section %s, range %s -- %s: ", sectname
,
7969 paddress (target_gdbarch
, lma
),
7970 paddress (target_gdbarch
, lma
+ size
));
7972 printf_filtered ("matched.\n");
7975 printf_filtered ("MIS-MATCHED!\n");
7979 do_cleanups (old_chain
);
7982 warning (_("One or more sections of the remote executable does not match\n\
7983 the loaded file\n"));
7984 if (args
&& !matched
)
7985 printf_filtered (_("No loaded section named '%s'.\n"), args
);
7988 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
7989 into remote target. The number of bytes written to the remote
7990 target is returned, or -1 for error. */
7993 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
7994 const char *annex
, const gdb_byte
*writebuf
,
7995 ULONGEST offset
, LONGEST len
,
7996 struct packet_config
*packet
)
8000 struct remote_state
*rs
= get_remote_state ();
8001 int max_size
= get_memory_write_packet_size ();
8003 if (packet
->support
== PACKET_DISABLE
)
8006 /* Insert header. */
8007 i
= snprintf (rs
->buf
, max_size
,
8008 "qXfer:%s:write:%s:%s:",
8009 object_name
, annex
? annex
: "",
8010 phex_nz (offset
, sizeof offset
));
8011 max_size
-= (i
+ 1);
8013 /* Escape as much data as fits into rs->buf. */
8014 buf_len
= remote_escape_output
8015 (writebuf
, len
, (rs
->buf
+ i
), &max_size
, max_size
);
8017 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
8018 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8019 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8022 unpack_varlen_hex (rs
->buf
, &n
);
8026 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8027 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8028 number of bytes read is returned, or 0 for EOF, or -1 for error.
8029 The number of bytes read may be less than LEN without indicating an
8030 EOF. PACKET is checked and updated to indicate whether the remote
8031 target supports this object. */
8034 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
8036 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
8037 struct packet_config
*packet
)
8039 static char *finished_object
;
8040 static char *finished_annex
;
8041 static ULONGEST finished_offset
;
8043 struct remote_state
*rs
= get_remote_state ();
8044 LONGEST i
, n
, packet_len
;
8046 if (packet
->support
== PACKET_DISABLE
)
8049 /* Check whether we've cached an end-of-object packet that matches
8051 if (finished_object
)
8053 if (strcmp (object_name
, finished_object
) == 0
8054 && strcmp (annex
? annex
: "", finished_annex
) == 0
8055 && offset
== finished_offset
)
8058 /* Otherwise, we're now reading something different. Discard
8060 xfree (finished_object
);
8061 xfree (finished_annex
);
8062 finished_object
= NULL
;
8063 finished_annex
= NULL
;
8066 /* Request only enough to fit in a single packet. The actual data
8067 may not, since we don't know how much of it will need to be escaped;
8068 the target is free to respond with slightly less data. We subtract
8069 five to account for the response type and the protocol frame. */
8070 n
= min (get_remote_packet_size () - 5, len
);
8071 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8072 object_name
, annex
? annex
: "",
8073 phex_nz (offset
, sizeof offset
),
8074 phex_nz (n
, sizeof n
));
8075 i
= putpkt (rs
->buf
);
8080 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
8081 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8084 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
8085 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
8087 /* 'm' means there is (or at least might be) more data after this
8088 batch. That does not make sense unless there's at least one byte
8089 of data in this reply. */
8090 if (rs
->buf
[0] == 'm' && packet_len
== 1)
8091 error (_("Remote qXfer reply contained no data."));
8093 /* Got some data. */
8094 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
8096 /* 'l' is an EOF marker, possibly including a final block of data,
8097 or possibly empty. If we have the final block of a non-empty
8098 object, record this fact to bypass a subsequent partial read. */
8099 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
8101 finished_object
= xstrdup (object_name
);
8102 finished_annex
= xstrdup (annex
? annex
: "");
8103 finished_offset
= offset
+ i
;
8110 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
8111 const char *annex
, gdb_byte
*readbuf
,
8112 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
8114 struct remote_state
*rs
;
8119 set_general_thread (inferior_ptid
);
8121 rs
= get_remote_state ();
8123 /* Handle memory using the standard memory routines. */
8124 if (object
== TARGET_OBJECT_MEMORY
)
8130 /* If the remote target is connected but not running, we should
8131 pass this request down to a lower stratum (e.g. the executable
8133 if (!target_has_execution
)
8136 if (writebuf
!= NULL
)
8137 xfered
= remote_write_bytes (offset
, writebuf
, len
);
8139 xfered
= remote_read_bytes (offset
, readbuf
, len
);
8143 else if (xfered
== 0 && errno
== 0)
8149 /* Handle SPU memory using qxfer packets. */
8150 if (object
== TARGET_OBJECT_SPU
)
8153 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
8154 &remote_protocol_packets
8155 [PACKET_qXfer_spu_read
]);
8157 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
8158 &remote_protocol_packets
8159 [PACKET_qXfer_spu_write
]);
8162 /* Handle extra signal info using qxfer packets. */
8163 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
8166 return remote_read_qxfer (ops
, "siginfo", annex
, readbuf
, offset
, len
,
8167 &remote_protocol_packets
8168 [PACKET_qXfer_siginfo_read
]);
8170 return remote_write_qxfer (ops
, "siginfo", annex
, 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
, readbuf
, offset
, len
,
8179 &remote_protocol_packets
8180 [PACKET_qXfer_statictrace_read
]);
8185 /* Only handle flash writes. */
8186 if (writebuf
!= NULL
)
8192 case TARGET_OBJECT_FLASH
:
8193 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
8197 else if (xfered
== 0 && errno
== 0)
8207 /* Map pre-existing objects onto letters. DO NOT do this for new
8208 objects!!! Instead specify new query packets. */
8211 case TARGET_OBJECT_AVR
:
8215 case TARGET_OBJECT_AUXV
:
8216 gdb_assert (annex
== NULL
);
8217 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
8218 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
8220 case TARGET_OBJECT_AVAILABLE_FEATURES
:
8221 return remote_read_qxfer
8222 (ops
, "features", annex
, readbuf
, offset
, len
,
8223 &remote_protocol_packets
[PACKET_qXfer_features
]);
8225 case TARGET_OBJECT_LIBRARIES
:
8226 return remote_read_qxfer
8227 (ops
, "libraries", annex
, readbuf
, offset
, len
,
8228 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
8230 case TARGET_OBJECT_MEMORY_MAP
:
8231 gdb_assert (annex
== NULL
);
8232 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
8233 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
8235 case TARGET_OBJECT_OSDATA
:
8236 /* Should only get here if we're connected. */
8237 gdb_assert (remote_desc
);
8238 return remote_read_qxfer
8239 (ops
, "osdata", annex
, readbuf
, offset
, len
,
8240 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
8242 case TARGET_OBJECT_THREADS
:
8243 gdb_assert (annex
== NULL
);
8244 return remote_read_qxfer (ops
, "threads", annex
, readbuf
, offset
, len
,
8245 &remote_protocol_packets
[PACKET_qXfer_threads
]);
8251 /* Note: a zero OFFSET and LEN can be used to query the minimum
8253 if (offset
== 0 && len
== 0)
8254 return (get_remote_packet_size ());
8255 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8256 large enough let the caller deal with it. */
8257 if (len
< get_remote_packet_size ())
8259 len
= get_remote_packet_size ();
8261 /* Except for querying the minimum buffer size, target must be open. */
8263 error (_("remote query is only available after target open"));
8265 gdb_assert (annex
!= NULL
);
8266 gdb_assert (readbuf
!= NULL
);
8272 /* We used one buffer char for the remote protocol q command and
8273 another for the query type. As the remote protocol encapsulation
8274 uses 4 chars plus one extra in case we are debugging
8275 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8278 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
8280 /* Bad caller may have sent forbidden characters. */
8281 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
8286 gdb_assert (annex
[i
] == '\0');
8288 i
= putpkt (rs
->buf
);
8292 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8293 strcpy ((char *) readbuf
, rs
->buf
);
8295 return strlen ((char *) readbuf
);
8299 remote_search_memory (struct target_ops
* ops
,
8300 CORE_ADDR start_addr
, ULONGEST search_space_len
,
8301 const gdb_byte
*pattern
, ULONGEST pattern_len
,
8302 CORE_ADDR
*found_addrp
)
8304 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
8305 struct remote_state
*rs
= get_remote_state ();
8306 int max_size
= get_memory_write_packet_size ();
8307 struct packet_config
*packet
=
8308 &remote_protocol_packets
[PACKET_qSearch_memory
];
8309 /* number of packet bytes used to encode the pattern,
8310 this could be more than PATTERN_LEN due to escape characters */
8311 int escaped_pattern_len
;
8312 /* amount of pattern that was encodable in the packet */
8313 int used_pattern_len
;
8316 ULONGEST found_addr
;
8318 /* Don't go to the target if we don't have to.
8319 This is done before checking packet->support to avoid the possibility that
8320 a success for this edge case means the facility works in general. */
8321 if (pattern_len
> search_space_len
)
8323 if (pattern_len
== 0)
8325 *found_addrp
= start_addr
;
8329 /* If we already know the packet isn't supported, fall back to the simple
8330 way of searching memory. */
8332 if (packet
->support
== PACKET_DISABLE
)
8334 /* Target doesn't provided special support, fall back and use the
8335 standard support (copy memory and do the search here). */
8336 return simple_search_memory (ops
, start_addr
, search_space_len
,
8337 pattern
, pattern_len
, found_addrp
);
8340 /* Insert header. */
8341 i
= snprintf (rs
->buf
, max_size
,
8342 "qSearch:memory:%s;%s;",
8343 phex_nz (start_addr
, addr_size
),
8344 phex_nz (search_space_len
, sizeof (search_space_len
)));
8345 max_size
-= (i
+ 1);
8347 /* Escape as much data as fits into rs->buf. */
8348 escaped_pattern_len
=
8349 remote_escape_output (pattern
, pattern_len
, (rs
->buf
+ i
),
8350 &used_pattern_len
, max_size
);
8352 /* Bail if the pattern is too large. */
8353 if (used_pattern_len
!= pattern_len
)
8354 error ("Pattern is too large to transmit to remote target.");
8356 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
8357 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8358 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8360 /* The request may not have worked because the command is not
8361 supported. If so, fall back to the simple way. */
8362 if (packet
->support
== PACKET_DISABLE
)
8364 return simple_search_memory (ops
, start_addr
, search_space_len
,
8365 pattern
, pattern_len
, found_addrp
);
8370 if (rs
->buf
[0] == '0')
8372 else if (rs
->buf
[0] == '1')
8375 if (rs
->buf
[1] != ',')
8376 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8377 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
8378 *found_addrp
= found_addr
;
8381 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8387 remote_rcmd (char *command
,
8388 struct ui_file
*outbuf
)
8390 struct remote_state
*rs
= get_remote_state ();
8394 error (_("remote rcmd is only available after target open"));
8396 /* Send a NULL command across as an empty command. */
8397 if (command
== NULL
)
8400 /* The query prefix. */
8401 strcpy (rs
->buf
, "qRcmd,");
8402 p
= strchr (rs
->buf
, '\0');
8404 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/) > get_remote_packet_size ())
8405 error (_("\"monitor\" command ``%s'' is too long."), command
);
8407 /* Encode the actual command. */
8408 bin2hex ((gdb_byte
*) command
, p
, 0);
8410 if (putpkt (rs
->buf
) < 0)
8411 error (_("Communication problem with target."));
8413 /* get/display the response */
8418 /* XXX - see also remote_get_noisy_reply(). */
8420 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8423 error (_("Target does not support this command."));
8424 if (buf
[0] == 'O' && buf
[1] != 'K')
8426 remote_console_output (buf
+ 1); /* 'O' message from stub. */
8429 if (strcmp (buf
, "OK") == 0)
8431 if (strlen (buf
) == 3 && buf
[0] == 'E'
8432 && isdigit (buf
[1]) && isdigit (buf
[2]))
8434 error (_("Protocol error with Rcmd"));
8436 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
8438 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
8440 fputc_unfiltered (c
, outbuf
);
8446 static VEC(mem_region_s
) *
8447 remote_memory_map (struct target_ops
*ops
)
8449 VEC(mem_region_s
) *result
= NULL
;
8450 char *text
= target_read_stralloc (¤t_target
,
8451 TARGET_OBJECT_MEMORY_MAP
, NULL
);
8455 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
8457 result
= parse_memory_map (text
);
8458 do_cleanups (back_to
);
8465 packet_command (char *args
, int from_tty
)
8467 struct remote_state
*rs
= get_remote_state ();
8470 error (_("command can only be used with remote target"));
8473 error (_("remote-packet command requires packet text as argument"));
8475 puts_filtered ("sending: ");
8476 print_packet (args
);
8477 puts_filtered ("\n");
8480 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8481 puts_filtered ("received: ");
8482 print_packet (rs
->buf
);
8483 puts_filtered ("\n");
8487 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
8489 static void display_thread_info (struct gdb_ext_thread_info
*info
);
8491 static void threadset_test_cmd (char *cmd
, int tty
);
8493 static void threadalive_test (char *cmd
, int tty
);
8495 static void threadlist_test_cmd (char *cmd
, int tty
);
8497 int get_and_display_threadinfo (threadref
*ref
);
8499 static void threadinfo_test_cmd (char *cmd
, int tty
);
8501 static int thread_display_step (threadref
*ref
, void *context
);
8503 static void threadlist_update_test_cmd (char *cmd
, int tty
);
8505 static void init_remote_threadtests (void);
8507 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
8510 threadset_test_cmd (char *cmd
, int tty
)
8512 int sample_thread
= SAMPLE_THREAD
;
8514 printf_filtered (_("Remote threadset test\n"));
8515 set_general_thread (sample_thread
);
8520 threadalive_test (char *cmd
, int tty
)
8522 int sample_thread
= SAMPLE_THREAD
;
8523 int pid
= ptid_get_pid (inferior_ptid
);
8524 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
8526 if (remote_thread_alive (ptid
))
8527 printf_filtered ("PASS: Thread alive test\n");
8529 printf_filtered ("FAIL: Thread alive test\n");
8532 void output_threadid (char *title
, threadref
*ref
);
8535 output_threadid (char *title
, threadref
*ref
)
8539 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
8541 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
8545 threadlist_test_cmd (char *cmd
, int tty
)
8548 threadref nextthread
;
8549 int done
, result_count
;
8550 threadref threadlist
[3];
8552 printf_filtered ("Remote Threadlist test\n");
8553 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
8554 &result_count
, &threadlist
[0]))
8555 printf_filtered ("FAIL: threadlist test\n");
8558 threadref
*scan
= threadlist
;
8559 threadref
*limit
= scan
+ result_count
;
8561 while (scan
< limit
)
8562 output_threadid (" thread ", scan
++);
8567 display_thread_info (struct gdb_ext_thread_info
*info
)
8569 output_threadid ("Threadid: ", &info
->threadid
);
8570 printf_filtered ("Name: %s\n ", info
->shortname
);
8571 printf_filtered ("State: %s\n", info
->display
);
8572 printf_filtered ("other: %s\n\n", info
->more_display
);
8576 get_and_display_threadinfo (threadref
*ref
)
8580 struct gdb_ext_thread_info threadinfo
;
8582 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
8583 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
8584 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
8585 display_thread_info (&threadinfo
);
8590 threadinfo_test_cmd (char *cmd
, int tty
)
8592 int athread
= SAMPLE_THREAD
;
8596 int_to_threadref (&thread
, athread
);
8597 printf_filtered ("Remote Threadinfo test\n");
8598 if (!get_and_display_threadinfo (&thread
))
8599 printf_filtered ("FAIL cannot get thread info\n");
8603 thread_display_step (threadref
*ref
, void *context
)
8605 /* output_threadid(" threadstep ",ref); *//* simple test */
8606 return get_and_display_threadinfo (ref
);
8610 threadlist_update_test_cmd (char *cmd
, int tty
)
8612 printf_filtered ("Remote Threadlist update test\n");
8613 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
8617 init_remote_threadtests (void)
8619 add_com ("tlist", class_obscure
, threadlist_test_cmd
, _("\
8620 Fetch and print the remote list of thread identifiers, one pkt only"));
8621 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
8622 _("Fetch and display info about one thread"));
8623 add_com ("tset", class_obscure
, threadset_test_cmd
,
8624 _("Test setting to a different thread"));
8625 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
8626 _("Iterate through updating all remote thread info"));
8627 add_com ("talive", class_obscure
, threadalive_test
,
8628 _(" Remote thread alive test "));
8633 /* Convert a thread ID to a string. Returns the string in a static
8637 remote_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
8639 static char buf
[64];
8640 struct remote_state
*rs
= get_remote_state ();
8642 if (ptid_is_pid (ptid
))
8644 /* Printing an inferior target id. */
8646 /* When multi-process extensions are off, there's no way in the
8647 remote protocol to know the remote process id, if there's any
8648 at all. There's one exception --- when we're connected with
8649 target extended-remote, and we manually attached to a process
8650 with "attach PID". We don't record anywhere a flag that
8651 allows us to distinguish that case from the case of
8652 connecting with extended-remote and the stub already being
8653 attached to a process, and reporting yes to qAttached, hence
8654 no smart special casing here. */
8655 if (!remote_multi_process_p (rs
))
8657 xsnprintf (buf
, sizeof buf
, "Remote target");
8661 return normal_pid_to_str (ptid
);
8665 if (ptid_equal (magic_null_ptid
, ptid
))
8666 xsnprintf (buf
, sizeof buf
, "Thread <main>");
8667 else if (remote_multi_process_p (rs
))
8668 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
8669 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
8671 xsnprintf (buf
, sizeof buf
, "Thread %ld",
8672 ptid_get_tid (ptid
));
8677 /* Get the address of the thread local variable in OBJFILE which is
8678 stored at OFFSET within the thread local storage for thread PTID. */
8681 remote_get_thread_local_address (struct target_ops
*ops
,
8682 ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
8684 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
8686 struct remote_state
*rs
= get_remote_state ();
8688 char *endp
= rs
->buf
+ get_remote_packet_size ();
8689 enum packet_result result
;
8691 strcpy (p
, "qGetTLSAddr:");
8693 p
= write_ptid (p
, endp
, ptid
);
8695 p
+= hexnumstr (p
, offset
);
8697 p
+= hexnumstr (p
, lm
);
8701 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8702 result
= packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
8703 if (result
== PACKET_OK
)
8707 unpack_varlen_hex (rs
->buf
, &result
);
8710 else if (result
== PACKET_UNKNOWN
)
8711 throw_error (TLS_GENERIC_ERROR
,
8712 _("Remote target doesn't support qGetTLSAddr packet"));
8714 throw_error (TLS_GENERIC_ERROR
,
8715 _("Remote target failed to process qGetTLSAddr request"));
8718 throw_error (TLS_GENERIC_ERROR
,
8719 _("TLS not supported or disabled on this target"));
8724 /* Provide thread local base, i.e. Thread Information Block address.
8725 Returns 1 if ptid is found and thread_local_base is non zero. */
8728 remote_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
8730 if (remote_protocol_packets
[PACKET_qGetTIBAddr
].support
!= PACKET_DISABLE
)
8732 struct remote_state
*rs
= get_remote_state ();
8734 char *endp
= rs
->buf
+ get_remote_packet_size ();
8735 enum packet_result result
;
8737 strcpy (p
, "qGetTIBAddr:");
8739 p
= write_ptid (p
, endp
, ptid
);
8743 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8744 result
= packet_ok (rs
->buf
,
8745 &remote_protocol_packets
[PACKET_qGetTIBAddr
]);
8746 if (result
== PACKET_OK
)
8750 unpack_varlen_hex (rs
->buf
, &result
);
8752 *addr
= (CORE_ADDR
) result
;
8755 else if (result
== PACKET_UNKNOWN
)
8756 error (_("Remote target doesn't support qGetTIBAddr packet"));
8758 error (_("Remote target failed to process qGetTIBAddr request"));
8761 error (_("qGetTIBAddr not supported or disabled on this target"));
8766 /* Support for inferring a target description based on the current
8767 architecture and the size of a 'g' packet. While the 'g' packet
8768 can have any size (since optional registers can be left off the
8769 end), some sizes are easily recognizable given knowledge of the
8770 approximate architecture. */
8772 struct remote_g_packet_guess
8775 const struct target_desc
*tdesc
;
8777 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
8778 DEF_VEC_O(remote_g_packet_guess_s
);
8780 struct remote_g_packet_data
8782 VEC(remote_g_packet_guess_s
) *guesses
;
8785 static struct gdbarch_data
*remote_g_packet_data_handle
;
8788 remote_g_packet_data_init (struct obstack
*obstack
)
8790 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
8794 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
8795 const struct target_desc
*tdesc
)
8797 struct remote_g_packet_data
*data
8798 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
8799 struct remote_g_packet_guess new_guess
, *guess
;
8802 gdb_assert (tdesc
!= NULL
);
8805 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
8807 if (guess
->bytes
== bytes
)
8808 internal_error (__FILE__
, __LINE__
,
8809 "Duplicate g packet description added for size %d",
8812 new_guess
.bytes
= bytes
;
8813 new_guess
.tdesc
= tdesc
;
8814 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
8817 /* Return 1 if remote_read_description would do anything on this target
8818 and architecture, 0 otherwise. */
8821 remote_read_description_p (struct target_ops
*target
)
8823 struct remote_g_packet_data
*data
8824 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
8826 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
8832 static const struct target_desc
*
8833 remote_read_description (struct target_ops
*target
)
8835 struct remote_g_packet_data
*data
8836 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
8838 /* Do not try this during initial connection, when we do not know
8839 whether there is a running but stopped thread. */
8840 if (!target_has_execution
|| ptid_equal (inferior_ptid
, null_ptid
))
8843 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
8845 struct remote_g_packet_guess
*guess
;
8847 int bytes
= send_g_packet ();
8850 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
8852 if (guess
->bytes
== bytes
)
8853 return guess
->tdesc
;
8855 /* We discard the g packet. A minor optimization would be to
8856 hold on to it, and fill the register cache once we have selected
8857 an architecture, but it's too tricky to do safely. */
8863 /* Remote file transfer support. This is host-initiated I/O, not
8864 target-initiated; for target-initiated, see remote-fileio.c. */
8866 /* If *LEFT is at least the length of STRING, copy STRING to
8867 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8868 decrease *LEFT. Otherwise raise an error. */
8871 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
8873 int len
= strlen (string
);
8876 error (_("Packet too long for target."));
8878 memcpy (*buffer
, string
, len
);
8882 /* NUL-terminate the buffer as a convenience, if there is
8888 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
8889 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8890 decrease *LEFT. Otherwise raise an error. */
8893 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
8896 if (2 * len
> *left
)
8897 error (_("Packet too long for target."));
8899 bin2hex (bytes
, *buffer
, len
);
8903 /* NUL-terminate the buffer as a convenience, if there is
8909 /* If *LEFT is large enough, convert VALUE to hex and add it to
8910 *BUFFER, update *BUFFER to point to the new end of the buffer, and
8911 decrease *LEFT. Otherwise raise an error. */
8914 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
8916 int len
= hexnumlen (value
);
8919 error (_("Packet too long for target."));
8921 hexnumstr (*buffer
, value
);
8925 /* NUL-terminate the buffer as a convenience, if there is
8931 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
8932 value, *REMOTE_ERRNO to the remote error number or zero if none
8933 was included, and *ATTACHMENT to point to the start of the annex
8934 if any. The length of the packet isn't needed here; there may
8935 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
8937 Return 0 if the packet could be parsed, -1 if it could not. If
8938 -1 is returned, the other variables may not be initialized. */
8941 remote_hostio_parse_result (char *buffer
, int *retcode
,
8942 int *remote_errno
, char **attachment
)
8949 if (buffer
[0] != 'F')
8953 *retcode
= strtol (&buffer
[1], &p
, 16);
8954 if (errno
!= 0 || p
== &buffer
[1])
8957 /* Check for ",errno". */
8961 *remote_errno
= strtol (p
+ 1, &p2
, 16);
8962 if (errno
!= 0 || p
+ 1 == p2
)
8967 /* Check for ";attachment". If there is no attachment, the
8968 packet should end here. */
8971 *attachment
= p
+ 1;
8974 else if (*p
== '\0')
8980 /* Send a prepared I/O packet to the target and read its response.
8981 The prepared packet is in the global RS->BUF before this function
8982 is called, and the answer is there when we return.
8984 COMMAND_BYTES is the length of the request to send, which may include
8985 binary data. WHICH_PACKET is the packet configuration to check
8986 before attempting a packet. If an error occurs, *REMOTE_ERRNO
8987 is set to the error number and -1 is returned. Otherwise the value
8988 returned by the function is returned.
8990 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
8991 attachment is expected; an error will be reported if there's a
8992 mismatch. If one is found, *ATTACHMENT will be set to point into
8993 the packet buffer and *ATTACHMENT_LEN will be set to the
8994 attachment's length. */
8997 remote_hostio_send_command (int command_bytes
, int which_packet
,
8998 int *remote_errno
, char **attachment
,
8999 int *attachment_len
)
9001 struct remote_state
*rs
= get_remote_state ();
9002 int ret
, bytes_read
;
9003 char *attachment_tmp
;
9006 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
9008 *remote_errno
= FILEIO_ENOSYS
;
9012 putpkt_binary (rs
->buf
, command_bytes
);
9013 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
9015 /* If it timed out, something is wrong. Don't try to parse the
9019 *remote_errno
= FILEIO_EINVAL
;
9023 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
9026 *remote_errno
= FILEIO_EINVAL
;
9028 case PACKET_UNKNOWN
:
9029 *remote_errno
= FILEIO_ENOSYS
;
9035 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
9038 *remote_errno
= FILEIO_EINVAL
;
9042 /* Make sure we saw an attachment if and only if we expected one. */
9043 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
9044 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
9046 *remote_errno
= FILEIO_EINVAL
;
9050 /* If an attachment was found, it must point into the packet buffer;
9051 work out how many bytes there were. */
9052 if (attachment_tmp
!= NULL
)
9054 *attachment
= attachment_tmp
;
9055 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
9061 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9062 remote file descriptor, or -1 if an error occurs (and set
9066 remote_hostio_open (const char *filename
, int flags
, int mode
,
9069 struct remote_state
*rs
= get_remote_state ();
9071 int left
= get_remote_packet_size () - 1;
9073 remote_buffer_add_string (&p
, &left
, "vFile:open:");
9075 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9077 remote_buffer_add_string (&p
, &left
, ",");
9079 remote_buffer_add_int (&p
, &left
, flags
);
9080 remote_buffer_add_string (&p
, &left
, ",");
9082 remote_buffer_add_int (&p
, &left
, mode
);
9084 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
9085 remote_errno
, NULL
, NULL
);
9088 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9089 Return the number of bytes written, or -1 if an error occurs (and
9090 set *REMOTE_ERRNO). */
9093 remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
9094 ULONGEST offset
, int *remote_errno
)
9096 struct remote_state
*rs
= get_remote_state ();
9098 int left
= get_remote_packet_size ();
9101 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
9103 remote_buffer_add_int (&p
, &left
, fd
);
9104 remote_buffer_add_string (&p
, &left
, ",");
9106 remote_buffer_add_int (&p
, &left
, offset
);
9107 remote_buffer_add_string (&p
, &left
, ",");
9109 p
+= remote_escape_output (write_buf
, len
, p
, &out_len
,
9110 get_remote_packet_size () - (p
- rs
->buf
));
9112 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
9113 remote_errno
, NULL
, NULL
);
9116 /* Read up to LEN bytes FD on the remote target into READ_BUF
9117 Return the number of bytes read, or -1 if an error occurs (and
9118 set *REMOTE_ERRNO). */
9121 remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
9122 ULONGEST offset
, int *remote_errno
)
9124 struct remote_state
*rs
= get_remote_state ();
9127 int left
= get_remote_packet_size ();
9128 int ret
, attachment_len
;
9131 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
9133 remote_buffer_add_int (&p
, &left
, fd
);
9134 remote_buffer_add_string (&p
, &left
, ",");
9136 remote_buffer_add_int (&p
, &left
, len
);
9137 remote_buffer_add_string (&p
, &left
, ",");
9139 remote_buffer_add_int (&p
, &left
, offset
);
9141 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
9142 remote_errno
, &attachment
,
9148 read_len
= remote_unescape_input (attachment
, attachment_len
,
9150 if (read_len
!= ret
)
9151 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
9156 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9157 (and set *REMOTE_ERRNO). */
9160 remote_hostio_close (int fd
, int *remote_errno
)
9162 struct remote_state
*rs
= get_remote_state ();
9164 int left
= get_remote_packet_size () - 1;
9166 remote_buffer_add_string (&p
, &left
, "vFile:close:");
9168 remote_buffer_add_int (&p
, &left
, fd
);
9170 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
9171 remote_errno
, NULL
, NULL
);
9174 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9175 occurs (and set *REMOTE_ERRNO). */
9178 remote_hostio_unlink (const char *filename
, int *remote_errno
)
9180 struct remote_state
*rs
= get_remote_state ();
9182 int left
= get_remote_packet_size () - 1;
9184 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
9186 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9189 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
9190 remote_errno
, NULL
, NULL
);
9194 remote_fileio_errno_to_host (int errnum
)
9218 case FILEIO_ENOTDIR
:
9238 case FILEIO_ENAMETOOLONG
:
9239 return ENAMETOOLONG
;
9245 remote_hostio_error (int errnum
)
9247 int host_error
= remote_fileio_errno_to_host (errnum
);
9249 if (host_error
== -1)
9250 error (_("Unknown remote I/O error %d"), errnum
);
9252 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
9256 remote_hostio_close_cleanup (void *opaque
)
9258 int fd
= *(int *) opaque
;
9261 remote_hostio_close (fd
, &remote_errno
);
9266 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
9268 const char *filename
= bfd_get_filename (abfd
);
9269 int fd
, remote_errno
;
9272 gdb_assert (remote_filename_p (filename
));
9274 fd
= remote_hostio_open (filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
9277 errno
= remote_fileio_errno_to_host (remote_errno
);
9278 bfd_set_error (bfd_error_system_call
);
9282 stream
= xmalloc (sizeof (int));
9288 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
9290 int fd
= *(int *)stream
;
9295 /* Ignore errors on close; these may happen if the remote
9296 connection was already torn down. */
9297 remote_hostio_close (fd
, &remote_errno
);
9303 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
9304 file_ptr nbytes
, file_ptr offset
)
9306 int fd
= *(int *)stream
;
9308 file_ptr pos
, bytes
;
9311 while (nbytes
> pos
)
9313 bytes
= remote_hostio_pread (fd
, (char *)buf
+ pos
, nbytes
- pos
,
9314 offset
+ pos
, &remote_errno
);
9316 /* Success, but no bytes, means end-of-file. */
9320 errno
= remote_fileio_errno_to_host (remote_errno
);
9321 bfd_set_error (bfd_error_system_call
);
9332 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
9334 /* FIXME: We should probably implement remote_hostio_stat. */
9335 sb
->st_size
= INT_MAX
;
9340 remote_filename_p (const char *filename
)
9342 return strncmp (filename
, "remote:", 7) == 0;
9346 remote_bfd_open (const char *remote_file
, const char *target
)
9348 return bfd_openr_iovec (remote_file
, target
,
9349 remote_bfd_iovec_open
, NULL
,
9350 remote_bfd_iovec_pread
,
9351 remote_bfd_iovec_close
,
9352 remote_bfd_iovec_stat
);
9356 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
9358 struct cleanup
*back_to
, *close_cleanup
;
9359 int retcode
, fd
, remote_errno
, bytes
, io_size
;
9362 int bytes_in_buffer
;
9367 error (_("command can only be used with remote target"));
9369 file
= fopen (local_file
, "rb");
9371 perror_with_name (local_file
);
9372 back_to
= make_cleanup_fclose (file
);
9374 fd
= remote_hostio_open (remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
9376 0700, &remote_errno
);
9378 remote_hostio_error (remote_errno
);
9380 /* Send up to this many bytes at once. They won't all fit in the
9381 remote packet limit, so we'll transfer slightly fewer. */
9382 io_size
= get_remote_packet_size ();
9383 buffer
= xmalloc (io_size
);
9384 make_cleanup (xfree
, buffer
);
9386 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
9388 bytes_in_buffer
= 0;
9391 while (bytes_in_buffer
|| !saw_eof
)
9395 bytes
= fread (buffer
+ bytes_in_buffer
, 1, io_size
- bytes_in_buffer
,
9400 error (_("Error reading %s."), local_file
);
9403 /* EOF. Unless there is something still in the
9404 buffer from the last iteration, we are done. */
9406 if (bytes_in_buffer
== 0)
9414 bytes
+= bytes_in_buffer
;
9415 bytes_in_buffer
= 0;
9417 retcode
= remote_hostio_pwrite (fd
, buffer
, bytes
, offset
, &remote_errno
);
9420 remote_hostio_error (remote_errno
);
9421 else if (retcode
== 0)
9422 error (_("Remote write of %d bytes returned 0!"), bytes
);
9423 else if (retcode
< bytes
)
9425 /* Short write. Save the rest of the read data for the next
9427 bytes_in_buffer
= bytes
- retcode
;
9428 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
9434 discard_cleanups (close_cleanup
);
9435 if (remote_hostio_close (fd
, &remote_errno
))
9436 remote_hostio_error (remote_errno
);
9439 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
9440 do_cleanups (back_to
);
9444 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
9446 struct cleanup
*back_to
, *close_cleanup
;
9447 int fd
, remote_errno
, bytes
, io_size
;
9453 error (_("command can only be used with remote target"));
9455 fd
= remote_hostio_open (remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
9457 remote_hostio_error (remote_errno
);
9459 file
= fopen (local_file
, "wb");
9461 perror_with_name (local_file
);
9462 back_to
= make_cleanup_fclose (file
);
9464 /* Send up to this many bytes at once. They won't all fit in the
9465 remote packet limit, so we'll transfer slightly fewer. */
9466 io_size
= get_remote_packet_size ();
9467 buffer
= xmalloc (io_size
);
9468 make_cleanup (xfree
, buffer
);
9470 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
9475 bytes
= remote_hostio_pread (fd
, buffer
, io_size
, offset
, &remote_errno
);
9477 /* Success, but no bytes, means end-of-file. */
9480 remote_hostio_error (remote_errno
);
9484 bytes
= fwrite (buffer
, 1, bytes
, file
);
9486 perror_with_name (local_file
);
9489 discard_cleanups (close_cleanup
);
9490 if (remote_hostio_close (fd
, &remote_errno
))
9491 remote_hostio_error (remote_errno
);
9494 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
9495 do_cleanups (back_to
);
9499 remote_file_delete (const char *remote_file
, int from_tty
)
9501 int retcode
, remote_errno
;
9504 error (_("command can only be used with remote target"));
9506 retcode
= remote_hostio_unlink (remote_file
, &remote_errno
);
9508 remote_hostio_error (remote_errno
);
9511 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
9515 remote_put_command (char *args
, int from_tty
)
9517 struct cleanup
*back_to
;
9521 error_no_arg (_("file to put"));
9523 argv
= gdb_buildargv (args
);
9524 back_to
= make_cleanup_freeargv (argv
);
9525 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
9526 error (_("Invalid parameters to remote put"));
9528 remote_file_put (argv
[0], argv
[1], from_tty
);
9530 do_cleanups (back_to
);
9534 remote_get_command (char *args
, int from_tty
)
9536 struct cleanup
*back_to
;
9540 error_no_arg (_("file to get"));
9542 argv
= gdb_buildargv (args
);
9543 back_to
= make_cleanup_freeargv (argv
);
9544 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
9545 error (_("Invalid parameters to remote get"));
9547 remote_file_get (argv
[0], argv
[1], from_tty
);
9549 do_cleanups (back_to
);
9553 remote_delete_command (char *args
, int from_tty
)
9555 struct cleanup
*back_to
;
9559 error_no_arg (_("file to delete"));
9561 argv
= gdb_buildargv (args
);
9562 back_to
= make_cleanup_freeargv (argv
);
9563 if (argv
[0] == NULL
|| argv
[1] != NULL
)
9564 error (_("Invalid parameters to remote delete"));
9566 remote_file_delete (argv
[0], from_tty
);
9568 do_cleanups (back_to
);
9572 remote_command (char *args
, int from_tty
)
9574 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
9578 remote_can_execute_reverse (void)
9580 if (remote_protocol_packets
[PACKET_bs
].support
== PACKET_ENABLE
9581 || remote_protocol_packets
[PACKET_bc
].support
== PACKET_ENABLE
)
9588 remote_supports_non_stop (void)
9594 remote_supports_multi_process (void)
9596 struct remote_state
*rs
= get_remote_state ();
9598 return remote_multi_process_p (rs
);
9602 remote_supports_cond_tracepoints (void)
9604 struct remote_state
*rs
= get_remote_state ();
9606 return rs
->cond_tracepoints
;
9610 remote_supports_fast_tracepoints (void)
9612 struct remote_state
*rs
= get_remote_state ();
9614 return rs
->fast_tracepoints
;
9618 remote_supports_static_tracepoints (void)
9620 struct remote_state
*rs
= get_remote_state ();
9622 return rs
->static_tracepoints
;
9626 remote_trace_init (void)
9629 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9630 if (strcmp (target_buf
, "OK") != 0)
9631 error (_("Target does not support this command."));
9634 static void free_actions_list (char **actions_list
);
9635 static void free_actions_list_cleanup_wrapper (void *);
9637 free_actions_list_cleanup_wrapper (void *al
)
9639 free_actions_list (al
);
9643 free_actions_list (char **actions_list
)
9647 if (actions_list
== 0)
9650 for (ndx
= 0; actions_list
[ndx
]; ndx
++)
9651 xfree (actions_list
[ndx
]);
9653 xfree (actions_list
);
9656 /* Recursive routine to walk through command list including loops, and
9657 download packets for each command. */
9660 remote_download_command_source (int num
, ULONGEST addr
,
9661 struct command_line
*cmds
)
9663 struct remote_state
*rs
= get_remote_state ();
9664 struct command_line
*cmd
;
9666 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
9668 QUIT
; /* allow user to bail out with ^C */
9669 strcpy (rs
->buf
, "QTDPsrc:");
9670 encode_source_string (num
, addr
, "cmd", cmd
->line
,
9671 rs
->buf
+ strlen (rs
->buf
),
9672 rs
->buf_size
- strlen (rs
->buf
));
9674 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9675 if (strcmp (target_buf
, "OK"))
9676 warning (_("Target does not support source download."));
9678 if (cmd
->control_type
== while_control
9679 || cmd
->control_type
== while_stepping_control
)
9681 remote_download_command_source (num
, addr
, *cmd
->body_list
);
9683 QUIT
; /* allow user to bail out with ^C */
9684 strcpy (rs
->buf
, "QTDPsrc:");
9685 encode_source_string (num
, addr
, "cmd", "end",
9686 rs
->buf
+ strlen (rs
->buf
),
9687 rs
->buf_size
- strlen (rs
->buf
));
9689 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9690 if (strcmp (target_buf
, "OK"))
9691 warning (_("Target does not support source download."));
9697 remote_download_tracepoint (struct breakpoint
*t
)
9699 struct bp_location
*loc
;
9704 char **stepping_actions
;
9706 struct cleanup
*old_chain
= NULL
;
9707 struct agent_expr
*aexpr
;
9708 struct cleanup
*aexpr_chain
= NULL
;
9711 /* Iterate over all the tracepoint locations. It's up to the target to
9712 notice multiple tracepoint packets with the same number but different
9713 addresses, and treat them as multiple locations. */
9714 for (loc
= t
->loc
; loc
; loc
= loc
->next
)
9716 encode_actions (t
, loc
, &tdp_actions
, &stepping_actions
);
9717 old_chain
= make_cleanup (free_actions_list_cleanup_wrapper
,
9719 (void) make_cleanup (free_actions_list_cleanup_wrapper
, stepping_actions
);
9721 tpaddr
= loc
->address
;
9722 sprintf_vma (addrbuf
, tpaddr
);
9723 sprintf (buf
, "QTDP:%x:%s:%c:%lx:%x", t
->number
,
9724 addrbuf
, /* address */
9725 (t
->enable_state
== bp_enabled
? 'E' : 'D'),
9726 t
->step_count
, t
->pass_count
);
9727 /* Fast tracepoints are mostly handled by the target, but we can
9728 tell the target how big of an instruction block should be moved
9730 if (t
->type
== bp_fast_tracepoint
)
9732 /* Only test for support at download time; we may not know
9733 target capabilities at definition time. */
9734 if (remote_supports_fast_tracepoints ())
9738 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch
,
9739 tpaddr
, &isize
, NULL
))
9740 sprintf (buf
+ strlen (buf
), ":F%x", isize
);
9742 /* If it passed validation at definition but fails now,
9743 something is very wrong. */
9744 internal_error (__FILE__
, __LINE__
,
9745 "Fast tracepoint not valid during download");
9748 /* Fast tracepoints are functionally identical to regular
9749 tracepoints, so don't take lack of support as a reason to
9750 give up on the trace run. */
9751 warning (_("Target does not support fast tracepoints, downloading %d as regular tracepoint"), t
->number
);
9753 else if (t
->type
== bp_static_tracepoint
)
9755 /* Only test for support at download time; we may not know
9756 target capabilities at definition time. */
9757 if (remote_supports_static_tracepoints ())
9759 struct static_tracepoint_marker marker
;
9761 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
9764 error (_("Static tracepoint not valid during download"));
9767 /* Fast tracepoints are functionally identical to regular
9768 tracepoints, so don't take lack of support as a reason
9769 to give up on the trace run. */
9770 error (_("Target does not support static tracepoints"));
9772 /* If the tracepoint has a conditional, make it into an agent
9773 expression and append to the definition. */
9776 /* Only test support at download time, we may not know target
9777 capabilities at definition time. */
9778 if (remote_supports_cond_tracepoints ())
9780 aexpr
= gen_eval_for_expr (tpaddr
, loc
->cond
);
9781 aexpr_chain
= make_cleanup_free_agent_expr (aexpr
);
9782 sprintf (buf
+ strlen (buf
), ":X%x,", aexpr
->len
);
9783 pkt
= buf
+ strlen (buf
);
9784 for (ndx
= 0; ndx
< aexpr
->len
; ++ndx
)
9785 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
9787 do_cleanups (aexpr_chain
);
9790 warning (_("Target does not support conditional tracepoints, ignoring tp %d cond"), t
->number
);
9793 if (t
->commands
|| *default_collect
)
9796 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9797 if (strcmp (target_buf
, "OK"))
9798 error (_("Target does not support tracepoints."));
9800 /* do_single_steps (t); */
9803 for (ndx
= 0; tdp_actions
[ndx
]; ndx
++)
9805 QUIT
; /* allow user to bail out with ^C */
9806 sprintf (buf
, "QTDP:-%x:%s:%s%c",
9807 t
->number
, addrbuf
, /* address */
9809 ((tdp_actions
[ndx
+ 1] || stepping_actions
)
9812 remote_get_noisy_reply (&target_buf
,
9814 if (strcmp (target_buf
, "OK"))
9815 error (_("Error on target while setting tracepoints."));
9818 if (stepping_actions
)
9820 for (ndx
= 0; stepping_actions
[ndx
]; ndx
++)
9822 QUIT
; /* allow user to bail out with ^C */
9823 sprintf (buf
, "QTDP:-%x:%s:%s%s%s",
9824 t
->number
, addrbuf
, /* address */
9825 ((ndx
== 0) ? "S" : ""),
9826 stepping_actions
[ndx
],
9827 (stepping_actions
[ndx
+ 1] ? "-" : ""));
9829 remote_get_noisy_reply (&target_buf
,
9831 if (strcmp (target_buf
, "OK"))
9832 error (_("Error on target while setting tracepoints."));
9836 if (remote_protocol_packets
[PACKET_TracepointSource
].support
== PACKET_ENABLE
)
9840 strcpy (buf
, "QTDPsrc:");
9841 encode_source_string (t
->number
, loc
->address
,
9842 "at", t
->addr_string
, buf
+ strlen (buf
),
9843 2048 - strlen (buf
));
9846 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9847 if (strcmp (target_buf
, "OK"))
9848 warning (_("Target does not support source download."));
9852 strcpy (buf
, "QTDPsrc:");
9853 encode_source_string (t
->number
, loc
->address
,
9854 "cond", t
->cond_string
, buf
+ strlen (buf
),
9855 2048 - strlen (buf
));
9857 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9858 if (strcmp (target_buf
, "OK"))
9859 warning (_("Target does not support source download."));
9861 remote_download_command_source (t
->number
, loc
->address
,
9862 breakpoint_commands (t
));
9865 do_cleanups (old_chain
);
9870 remote_download_trace_state_variable (struct trace_state_variable
*tsv
)
9872 struct remote_state
*rs
= get_remote_state ();
9875 sprintf (rs
->buf
, "QTDV:%x:%s:%x:",
9876 tsv
->number
, phex ((ULONGEST
) tsv
->initial_value
, 8), tsv
->builtin
);
9877 p
= rs
->buf
+ strlen (rs
->buf
);
9878 if ((p
- rs
->buf
) + strlen (tsv
->name
) * 2 >= get_remote_packet_size ())
9879 error (_("Trace state variable name too long for tsv definition packet"));
9880 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
->name
), p
, 0);
9883 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9884 if (*target_buf
== '\0')
9885 error (_("Target does not support this command."));
9886 if (strcmp (target_buf
, "OK") != 0)
9887 error (_("Error on target while downloading trace state variable."));
9891 remote_trace_set_readonly_regions (void)
9899 return; /* No information to give. */
9901 strcpy (target_buf
, "QTro");
9902 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
9904 char tmp1
[40], tmp2
[40];
9906 if ((s
->flags
& SEC_LOAD
) == 0 ||
9907 /* (s->flags & SEC_CODE) == 0 || */
9908 (s
->flags
& SEC_READONLY
) == 0)
9913 size
= bfd_get_section_size (s
);
9914 sprintf_vma (tmp1
, lma
);
9915 sprintf_vma (tmp2
, lma
+ size
);
9916 sprintf (target_buf
+ strlen (target_buf
),
9917 ":%s,%s", tmp1
, tmp2
);
9921 putpkt (target_buf
);
9922 getpkt (&target_buf
, &target_buf_size
, 0);
9927 remote_trace_start (void)
9930 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9931 if (*target_buf
== '\0')
9932 error (_("Target does not support this command."));
9933 if (strcmp (target_buf
, "OK") != 0)
9934 error (_("Bogus reply from target: %s"), target_buf
);
9938 remote_get_trace_status (struct trace_status
*ts
)
9941 /* FIXME we need to get register block size some other way */
9942 extern int trace_regblock_size
;
9944 trace_regblock_size
= get_remote_arch_state ()->sizeof_g_packet
;
9946 putpkt ("qTStatus");
9947 p
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9949 /* If the remote target doesn't do tracing, flag it. */
9953 /* We're working with a live target. */
9956 /* Set some defaults. */
9957 ts
->running_known
= 0;
9958 ts
->stop_reason
= trace_stop_reason_unknown
;
9959 ts
->traceframe_count
= -1;
9960 ts
->buffer_free
= 0;
9963 error (_("Bogus trace status reply from target: %s"), target_buf
);
9965 parse_trace_status (p
, ts
);
9971 remote_trace_stop (void)
9974 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
9975 if (*target_buf
== '\0')
9976 error (_("Target does not support this command."));
9977 if (strcmp (target_buf
, "OK") != 0)
9978 error (_("Bogus reply from target: %s"), target_buf
);
9982 remote_trace_find (enum trace_find_type type
, int num
,
9983 ULONGEST addr1
, ULONGEST addr2
,
9986 struct remote_state
*rs
= get_remote_state ();
9988 int target_frameno
= -1, target_tracept
= -1;
9991 strcpy (p
, "QTFrame:");
9992 p
= strchr (p
, '\0');
9996 sprintf (p
, "%x", num
);
9999 sprintf (p
, "pc:%s", phex_nz (addr1
, 0));
10002 sprintf (p
, "tdp:%x", num
);
10005 sprintf (p
, "range:%s:%s", phex_nz (addr1
, 0), phex_nz (addr2
, 0));
10007 case tfind_outside
:
10008 sprintf (p
, "outside:%s:%s", phex_nz (addr1
, 0), phex_nz (addr2
, 0));
10011 error ("Unknown trace find type %d", type
);
10015 reply
= remote_get_noisy_reply (&(rs
->buf
), &sizeof_pkt
);
10016 if (*reply
== '\0')
10017 error (_("Target does not support this command."));
10019 while (reply
&& *reply
)
10024 target_frameno
= (int) strtol (p
, &reply
, 16);
10026 error (_("Unable to parse trace frame number"));
10027 if (target_frameno
== -1)
10032 target_tracept
= (int) strtol (p
, &reply
, 16);
10034 error (_("Unable to parse tracepoint number"));
10036 case 'O': /* "OK"? */
10037 if (reply
[1] == 'K' && reply
[2] == '\0')
10040 error (_("Bogus reply from target: %s"), reply
);
10043 error (_("Bogus reply from target: %s"), reply
);
10046 *tpp
= target_tracept
;
10047 return target_frameno
;
10051 remote_get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
10053 struct remote_state
*rs
= get_remote_state ();
10057 sprintf (rs
->buf
, "qTV:%x", tsvnum
);
10059 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10060 if (reply
&& *reply
)
10064 unpack_varlen_hex (reply
+ 1, &uval
);
10065 *val
= (LONGEST
) uval
;
10073 remote_save_trace_data (const char *filename
)
10075 struct remote_state
*rs
= get_remote_state ();
10079 strcpy (p
, "QTSave:");
10081 if ((p
- rs
->buf
) + strlen (filename
) * 2 >= get_remote_packet_size ())
10082 error (_("Remote file name too long for trace save packet"));
10083 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, 0);
10086 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10087 if (*reply
!= '\0')
10088 error (_("Target does not support this command."));
10089 if (strcmp (reply
, "OK") != 0)
10090 error (_("Bogus reply from target: %s"), reply
);
10094 /* This is basically a memory transfer, but needs to be its own packet
10095 because we don't know how the target actually organizes its trace
10096 memory, plus we want to be able to ask for as much as possible, but
10097 not be unhappy if we don't get as much as we ask for. */
10100 remote_get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
10102 struct remote_state
*rs
= get_remote_state ();
10108 strcpy (p
, "qTBuffer:");
10110 p
+= hexnumstr (p
, offset
);
10112 p
+= hexnumstr (p
, len
);
10116 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10117 if (reply
&& *reply
)
10119 /* 'l' by itself means we're at the end of the buffer and
10120 there is nothing more to get. */
10124 /* Convert the reply into binary. Limit the number of bytes to
10125 convert according to our passed-in buffer size, rather than
10126 what was returned in the packet; if the target is
10127 unexpectedly generous and gives us a bigger reply than we
10128 asked for, we don't want to crash. */
10129 rslt
= hex2bin (target_buf
, buf
, len
);
10133 /* Something went wrong, flag as an error. */
10138 remote_set_disconnected_tracing (int val
)
10140 struct remote_state
*rs
= get_remote_state ();
10142 if (rs
->disconnected_tracing
)
10146 sprintf (rs
->buf
, "QTDisconnected:%x", val
);
10148 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10149 if (*reply
== '\0')
10150 error (_("Target does not support this command."));
10151 if (strcmp (reply
, "OK") != 0)
10152 error (_("Bogus reply from target: %s"), reply
);
10155 warning (_("Target does not support disconnected tracing."));
10159 remote_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
10161 struct thread_info
*info
= find_thread_ptid (ptid
);
10163 if (info
&& info
->private)
10164 return info
->private->core
;
10169 remote_set_circular_trace_buffer (int val
)
10171 struct remote_state
*rs
= get_remote_state ();
10174 sprintf (rs
->buf
, "QTBuffer:circular:%x", val
);
10176 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10177 if (*reply
== '\0')
10178 error (_("Target does not support this command."));
10179 if (strcmp (reply
, "OK") != 0)
10180 error (_("Bogus reply from target: %s"), reply
);
10184 init_remote_ops (void)
10186 remote_ops
.to_shortname
= "remote";
10187 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
10188 remote_ops
.to_doc
=
10189 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10190 Specify the serial device it is connected to\n\
10191 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
10192 remote_ops
.to_open
= remote_open
;
10193 remote_ops
.to_close
= remote_close
;
10194 remote_ops
.to_detach
= remote_detach
;
10195 remote_ops
.to_disconnect
= remote_disconnect
;
10196 remote_ops
.to_resume
= remote_resume
;
10197 remote_ops
.to_wait
= remote_wait
;
10198 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
10199 remote_ops
.to_store_registers
= remote_store_registers
;
10200 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
10201 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
10202 remote_ops
.to_files_info
= remote_files_info
;
10203 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
10204 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
10205 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
10206 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
10207 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
10208 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
10209 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
10210 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
10211 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
10212 remote_ops
.to_kill
= remote_kill
;
10213 remote_ops
.to_load
= generic_load
;
10214 remote_ops
.to_mourn_inferior
= remote_mourn
;
10215 remote_ops
.to_notice_signals
= remote_notice_signals
;
10216 remote_ops
.to_thread_alive
= remote_thread_alive
;
10217 remote_ops
.to_find_new_threads
= remote_threads_info
;
10218 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
10219 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
10220 remote_ops
.to_get_ada_task_ptid
= remote_get_ada_task_ptid
;
10221 remote_ops
.to_stop
= remote_stop
;
10222 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
10223 remote_ops
.to_rcmd
= remote_rcmd
;
10224 remote_ops
.to_log_command
= serial_log_command
;
10225 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
10226 remote_ops
.to_stratum
= process_stratum
;
10227 remote_ops
.to_has_all_memory
= default_child_has_all_memory
;
10228 remote_ops
.to_has_memory
= default_child_has_memory
;
10229 remote_ops
.to_has_stack
= default_child_has_stack
;
10230 remote_ops
.to_has_registers
= default_child_has_registers
;
10231 remote_ops
.to_has_execution
= default_child_has_execution
;
10232 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
10233 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
10234 remote_ops
.to_magic
= OPS_MAGIC
;
10235 remote_ops
.to_memory_map
= remote_memory_map
;
10236 remote_ops
.to_flash_erase
= remote_flash_erase
;
10237 remote_ops
.to_flash_done
= remote_flash_done
;
10238 remote_ops
.to_read_description
= remote_read_description
;
10239 remote_ops
.to_search_memory
= remote_search_memory
;
10240 remote_ops
.to_can_async_p
= remote_can_async_p
;
10241 remote_ops
.to_is_async_p
= remote_is_async_p
;
10242 remote_ops
.to_async
= remote_async
;
10243 remote_ops
.to_async_mask
= remote_async_mask
;
10244 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
10245 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
10246 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
10247 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
10248 remote_ops
.to_trace_init
= remote_trace_init
;
10249 remote_ops
.to_download_tracepoint
= remote_download_tracepoint
;
10250 remote_ops
.to_download_trace_state_variable
= remote_download_trace_state_variable
;
10251 remote_ops
.to_trace_set_readonly_regions
= remote_trace_set_readonly_regions
;
10252 remote_ops
.to_trace_start
= remote_trace_start
;
10253 remote_ops
.to_get_trace_status
= remote_get_trace_status
;
10254 remote_ops
.to_trace_stop
= remote_trace_stop
;
10255 remote_ops
.to_trace_find
= remote_trace_find
;
10256 remote_ops
.to_get_trace_state_variable_value
= remote_get_trace_state_variable_value
;
10257 remote_ops
.to_save_trace_data
= remote_save_trace_data
;
10258 remote_ops
.to_upload_tracepoints
= remote_upload_tracepoints
;
10259 remote_ops
.to_upload_trace_state_variables
= remote_upload_trace_state_variables
;
10260 remote_ops
.to_get_raw_trace_data
= remote_get_raw_trace_data
;
10261 remote_ops
.to_set_disconnected_tracing
= remote_set_disconnected_tracing
;
10262 remote_ops
.to_set_circular_trace_buffer
= remote_set_circular_trace_buffer
;
10263 remote_ops
.to_core_of_thread
= remote_core_of_thread
;
10264 remote_ops
.to_verify_memory
= remote_verify_memory
;
10265 remote_ops
.to_get_tib_address
= remote_get_tib_address
;
10266 remote_ops
.to_set_permissions
= remote_set_permissions
;
10267 remote_ops
.to_static_tracepoint_marker_at
10268 = remote_static_tracepoint_marker_at
;
10269 remote_ops
.to_static_tracepoint_markers_by_strid
10270 = remote_static_tracepoint_markers_by_strid
;
10273 /* Set up the extended remote vector by making a copy of the standard
10274 remote vector and adding to it. */
10277 init_extended_remote_ops (void)
10279 extended_remote_ops
= remote_ops
;
10281 extended_remote_ops
.to_shortname
= "extended-remote";
10282 extended_remote_ops
.to_longname
=
10283 "Extended remote serial target in gdb-specific protocol";
10284 extended_remote_ops
.to_doc
=
10285 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10286 Specify the serial device it is connected to (e.g. /dev/ttya).";
10287 extended_remote_ops
.to_open
= extended_remote_open
;
10288 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
10289 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
10290 extended_remote_ops
.to_detach
= extended_remote_detach
;
10291 extended_remote_ops
.to_attach
= extended_remote_attach
;
10292 extended_remote_ops
.to_kill
= extended_remote_kill
;
10296 remote_can_async_p (void)
10298 if (!target_async_permitted
)
10299 /* We only enable async when the user specifically asks for it. */
10302 /* We're async whenever the serial device is. */
10303 return remote_async_mask_value
&& serial_can_async_p (remote_desc
);
10307 remote_is_async_p (void)
10309 if (!target_async_permitted
)
10310 /* We only enable async when the user specifically asks for it. */
10313 /* We're async whenever the serial device is. */
10314 return remote_async_mask_value
&& serial_is_async_p (remote_desc
);
10317 /* Pass the SERIAL event on and up to the client. One day this code
10318 will be able to delay notifying the client of an event until the
10319 point where an entire packet has been received. */
10321 static void (*async_client_callback
) (enum inferior_event_type event_type
,
10323 static void *async_client_context
;
10324 static serial_event_ftype remote_async_serial_handler
;
10327 remote_async_serial_handler (struct serial
*scb
, void *context
)
10329 /* Don't propogate error information up to the client. Instead let
10330 the client find out about the error by querying the target. */
10331 async_client_callback (INF_REG_EVENT
, async_client_context
);
10335 remote_async_inferior_event_handler (gdb_client_data data
)
10337 inferior_event_handler (INF_REG_EVENT
, NULL
);
10341 remote_async_get_pending_events_handler (gdb_client_data data
)
10343 remote_get_pending_stop_replies ();
10347 remote_async (void (*callback
) (enum inferior_event_type event_type
,
10348 void *context
), void *context
)
10350 if (remote_async_mask_value
== 0)
10351 internal_error (__FILE__
, __LINE__
,
10352 _("Calling remote_async when async is masked"));
10354 if (callback
!= NULL
)
10356 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
10357 async_client_callback
= callback
;
10358 async_client_context
= context
;
10361 serial_async (remote_desc
, NULL
, NULL
);
10365 remote_async_mask (int new_mask
)
10367 int curr_mask
= remote_async_mask_value
;
10369 remote_async_mask_value
= new_mask
;
10374 set_remote_cmd (char *args
, int from_tty
)
10376 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
10380 show_remote_cmd (char *args
, int from_tty
)
10382 /* We can't just use cmd_show_list here, because we want to skip
10383 the redundant "show remote Z-packet" and the legacy aliases. */
10384 struct cleanup
*showlist_chain
;
10385 struct cmd_list_element
*list
= remote_show_cmdlist
;
10387 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
10388 for (; list
!= NULL
; list
= list
->next
)
10389 if (strcmp (list
->name
, "Z-packet") == 0)
10391 else if (list
->type
== not_set_cmd
)
10392 /* Alias commands are exactly like the original, except they
10393 don't have the normal type. */
10397 struct cleanup
*option_chain
10398 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
10400 ui_out_field_string (uiout
, "name", list
->name
);
10401 ui_out_text (uiout
, ": ");
10402 if (list
->type
== show_cmd
)
10403 do_setshow_command ((char *) NULL
, from_tty
, list
);
10405 cmd_func (list
, NULL
, from_tty
);
10406 /* Close the tuple. */
10407 do_cleanups (option_chain
);
10410 /* Close the tuple. */
10411 do_cleanups (showlist_chain
);
10415 /* Function to be called whenever a new objfile (shlib) is detected. */
10417 remote_new_objfile (struct objfile
*objfile
)
10419 if (remote_desc
!= 0) /* Have a remote connection. */
10420 remote_check_symbols (objfile
);
10423 /* Pull all the tracepoints defined on the target and create local
10424 data structures representing them. We don't want to create real
10425 tracepoints yet, we don't want to mess up the user's existing
10429 remote_upload_tracepoints (struct uploaded_tp
**utpp
)
10431 struct remote_state
*rs
= get_remote_state ();
10434 /* Ask for a first packet of tracepoint definition. */
10436 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10438 while (*p
&& *p
!= 'l')
10440 parse_tracepoint_definition (p
, utpp
);
10441 /* Ask for another packet of tracepoint definition. */
10443 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10450 remote_upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
10452 struct remote_state
*rs
= get_remote_state ();
10455 /* Ask for a first packet of variable definition. */
10457 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10459 while (*p
&& *p
!= 'l')
10461 parse_tsv_definition (p
, utsvp
);
10462 /* Ask for another packet of variable definition. */
10464 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10471 _initialize_remote (void)
10473 struct remote_state
*rs
;
10474 struct cmd_list_element
*cmd
;
10477 /* architecture specific data */
10478 remote_gdbarch_data_handle
=
10479 gdbarch_data_register_post_init (init_remote_state
);
10480 remote_g_packet_data_handle
=
10481 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
10483 /* Initialize the per-target state. At the moment there is only one
10484 of these, not one per target. Only one target is active at a
10485 time. The default buffer size is unimportant; it will be expanded
10486 whenever a larger buffer is needed. */
10487 rs
= get_remote_state_raw ();
10488 rs
->buf_size
= 400;
10489 rs
->buf
= xmalloc (rs
->buf_size
);
10491 init_remote_ops ();
10492 add_target (&remote_ops
);
10494 init_extended_remote_ops ();
10495 add_target (&extended_remote_ops
);
10497 /* Hook into new objfile notification. */
10498 observer_attach_new_objfile (remote_new_objfile
);
10500 /* Set up signal handlers. */
10501 sigint_remote_token
=
10502 create_async_signal_handler (async_remote_interrupt
, NULL
);
10503 sigint_remote_twice_token
=
10504 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
10507 init_remote_threadtests ();
10510 /* set/show remote ... */
10512 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
10513 Remote protocol specific variables\n\
10514 Configure various remote-protocol specific variables such as\n\
10515 the packets being used"),
10516 &remote_set_cmdlist
, "set remote ",
10517 0 /* allow-unknown */, &setlist
);
10518 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
10519 Remote protocol specific variables\n\
10520 Configure various remote-protocol specific variables such as\n\
10521 the packets being used"),
10522 &remote_show_cmdlist
, "show remote ",
10523 0 /* allow-unknown */, &showlist
);
10525 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
10526 Compare section data on target to the exec file.\n\
10527 Argument is a single section name (default: all loaded sections)."),
10530 add_cmd ("packet", class_maintenance
, packet_command
, _("\
10531 Send an arbitrary packet to a remote target.\n\
10532 maintenance packet TEXT\n\
10533 If GDB is talking to an inferior via the GDB serial protocol, then\n\
10534 this command sends the string TEXT to the inferior, and displays the\n\
10535 response packet. GDB supplies the initial `$' character, and the\n\
10536 terminating `#' character and checksum."),
10539 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
10540 Set whether to send break if interrupted."), _("\
10541 Show whether to send break if interrupted."), _("\
10542 If set, a break, instead of a cntrl-c, is sent to the remote target."),
10543 set_remotebreak
, show_remotebreak
,
10544 &setlist
, &showlist
);
10545 cmd_name
= "remotebreak";
10546 cmd
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
10547 deprecate_cmd (cmd
, "set remote interrupt-sequence");
10548 cmd_name
= "remotebreak"; /* needed because lookup_cmd updates the pointer */
10549 cmd
= lookup_cmd (&cmd_name
, showlist
, "", -1, 1);
10550 deprecate_cmd (cmd
, "show remote interrupt-sequence");
10552 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
10553 interrupt_sequence_modes
, &interrupt_sequence_mode
, _("\
10554 Set interrupt sequence to remote target."), _("\
10555 Show interrupt sequence to remote target."), _("\
10556 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
10557 NULL
, show_interrupt_sequence
,
10558 &remote_set_cmdlist
,
10559 &remote_show_cmdlist
);
10561 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
10562 &interrupt_on_connect
, _("\
10563 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
10564 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
10565 If set, interrupt sequence is sent to remote target."),
10567 &remote_set_cmdlist
, &remote_show_cmdlist
);
10569 /* Install commands for configuring memory read/write packets. */
10571 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
10572 Set the maximum number of bytes per memory write packet (deprecated)."),
10574 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
10575 Show the maximum number of bytes per memory write packet (deprecated)."),
10577 add_cmd ("memory-write-packet-size", no_class
,
10578 set_memory_write_packet_size
, _("\
10579 Set the maximum number of bytes per memory-write packet.\n\
10580 Specify the number of bytes in a packet or 0 (zero) for the\n\
10581 default packet size. The actual limit is further reduced\n\
10582 dependent on the target. Specify ``fixed'' to disable the\n\
10583 further restriction and ``limit'' to enable that restriction."),
10584 &remote_set_cmdlist
);
10585 add_cmd ("memory-read-packet-size", no_class
,
10586 set_memory_read_packet_size
, _("\
10587 Set the maximum number of bytes per memory-read packet.\n\
10588 Specify the number of bytes in a packet or 0 (zero) for the\n\
10589 default packet size. The actual limit is further reduced\n\
10590 dependent on the target. Specify ``fixed'' to disable the\n\
10591 further restriction and ``limit'' to enable that restriction."),
10592 &remote_set_cmdlist
);
10593 add_cmd ("memory-write-packet-size", no_class
,
10594 show_memory_write_packet_size
,
10595 _("Show the maximum number of bytes per memory-write packet."),
10596 &remote_show_cmdlist
);
10597 add_cmd ("memory-read-packet-size", no_class
,
10598 show_memory_read_packet_size
,
10599 _("Show the maximum number of bytes per memory-read packet."),
10600 &remote_show_cmdlist
);
10602 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
10603 &remote_hw_watchpoint_limit
, _("\
10604 Set the maximum number of target hardware watchpoints."), _("\
10605 Show the maximum number of target hardware watchpoints."), _("\
10606 Specify a negative limit for unlimited."),
10607 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
10608 &remote_set_cmdlist
, &remote_show_cmdlist
);
10609 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
10610 &remote_hw_breakpoint_limit
, _("\
10611 Set the maximum number of target hardware breakpoints."), _("\
10612 Show the maximum number of target hardware breakpoints."), _("\
10613 Specify a negative limit for unlimited."),
10614 NULL
, NULL
, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
10615 &remote_set_cmdlist
, &remote_show_cmdlist
);
10617 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
10618 &remote_address_size
, _("\
10619 Set the maximum size of the address (in bits) in a memory packet."), _("\
10620 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
10622 NULL
, /* FIXME: i18n: */
10623 &setlist
, &showlist
);
10625 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
10626 "X", "binary-download", 1);
10628 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
10629 "vCont", "verbose-resume", 0);
10631 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
10632 "QPassSignals", "pass-signals", 0);
10634 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
10635 "qSymbol", "symbol-lookup", 0);
10637 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
10638 "P", "set-register", 1);
10640 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
10641 "p", "fetch-register", 1);
10643 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
10644 "Z0", "software-breakpoint", 0);
10646 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
10647 "Z1", "hardware-breakpoint", 0);
10649 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
10650 "Z2", "write-watchpoint", 0);
10652 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
10653 "Z3", "read-watchpoint", 0);
10655 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
10656 "Z4", "access-watchpoint", 0);
10658 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
10659 "qXfer:auxv:read", "read-aux-vector", 0);
10661 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
10662 "qXfer:features:read", "target-features", 0);
10664 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
10665 "qXfer:libraries:read", "library-info", 0);
10667 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
10668 "qXfer:memory-map:read", "memory-map", 0);
10670 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
10671 "qXfer:spu:read", "read-spu-object", 0);
10673 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
10674 "qXfer:spu:write", "write-spu-object", 0);
10676 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
10677 "qXfer:osdata:read", "osdata", 0);
10679 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_threads
],
10680 "qXfer:threads:read", "threads", 0);
10682 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_read
],
10683 "qXfer:siginfo:read", "read-siginfo-object", 0);
10685 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_write
],
10686 "qXfer:siginfo:write", "write-siginfo-object", 0);
10688 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
10689 "qGetTLSAddr", "get-thread-local-storage-address",
10692 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTIBAddr
],
10693 "qGetTIBAddr", "get-thread-information-block-address",
10696 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bc
],
10697 "bc", "reverse-continue", 0);
10699 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bs
],
10700 "bs", "reverse-step", 0);
10702 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
10703 "qSupported", "supported-packets", 0);
10705 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
10706 "qSearch:memory", "search-memory", 0);
10708 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
10709 "vFile:open", "hostio-open", 0);
10711 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
10712 "vFile:pread", "hostio-pread", 0);
10714 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
10715 "vFile:pwrite", "hostio-pwrite", 0);
10717 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
10718 "vFile:close", "hostio-close", 0);
10720 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
10721 "vFile:unlink", "hostio-unlink", 0);
10723 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
10724 "vAttach", "attach", 0);
10726 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
10729 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
10730 "QStartNoAckMode", "noack", 0);
10732 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
10733 "vKill", "kill", 0);
10735 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qAttached
],
10736 "qAttached", "query-attached", 0);
10738 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalTracepoints
],
10739 "ConditionalTracepoints", "conditional-tracepoints", 0);
10740 add_packet_config_cmd (&remote_protocol_packets
[PACKET_FastTracepoints
],
10741 "FastTracepoints", "fast-tracepoints", 0);
10743 add_packet_config_cmd (&remote_protocol_packets
[PACKET_TracepointSource
],
10744 "TracepointSource", "TracepointSource", 0);
10746 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAllow
],
10747 "QAllow", "allow", 0);
10749 add_packet_config_cmd (&remote_protocol_packets
[PACKET_StaticTracepoints
],
10750 "StaticTracepoints", "static-tracepoints", 0);
10752 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_statictrace_read
],
10753 "qXfer:statictrace:read", "read-sdata-object", 0);
10755 /* Keep the old ``set remote Z-packet ...'' working. Each individual
10756 Z sub-packet has its own set and show commands, but users may
10757 have sets to this variable in their .gdbinit files (or in their
10759 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
10760 &remote_Z_packet_detect
, _("\
10761 Set use of remote protocol `Z' packets"), _("\
10762 Show use of remote protocol `Z' packets "), _("\
10763 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
10765 set_remote_protocol_Z_packet_cmd
,
10766 show_remote_protocol_Z_packet_cmd
, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
10767 &remote_set_cmdlist
, &remote_show_cmdlist
);
10769 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
10770 Manipulate files on the remote system\n\
10771 Transfer files to and from the remote target system."),
10772 &remote_cmdlist
, "remote ",
10773 0 /* allow-unknown */, &cmdlist
);
10775 add_cmd ("put", class_files
, remote_put_command
,
10776 _("Copy a local file to the remote system."),
10779 add_cmd ("get", class_files
, remote_get_command
,
10780 _("Copy a remote file to the local system."),
10783 add_cmd ("delete", class_files
, remote_delete_command
,
10784 _("Delete a remote file."),
10787 remote_exec_file
= xstrdup ("");
10788 add_setshow_string_noescape_cmd ("exec-file", class_files
,
10789 &remote_exec_file
, _("\
10790 Set the remote pathname for \"run\""), _("\
10791 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
10792 &remote_set_cmdlist
, &remote_show_cmdlist
);
10794 /* Eventually initialize fileio. See fileio.c */
10795 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
10797 /* Take advantage of the fact that the LWP field is not used, to tag
10798 special ptids with it set to != 0. */
10799 magic_null_ptid
= ptid_build (42000, 1, -1);
10800 not_sent_ptid
= ptid_build (42000, 1, -2);
10801 any_thread_ptid
= ptid_build (42000, 1, 0);
10803 target_buf_size
= 2048;
10804 target_buf
= xmalloc (target_buf_size
);