1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See the GDB User Guide for details of the GDB remote protocol. */
23 #include "gdb_string.h"
29 #include "exceptions.h"
31 /*#include "terminal.h" */
34 #include "gdb-stabs.h"
35 #include "gdbthread.h"
39 #include "gdb_assert.h"
42 #include "cli/cli-decode.h"
43 #include "cli/cli-setshow.h"
44 #include "target-descriptions.h"
49 #include "event-loop.h"
50 #include "event-top.h"
56 #include "gdbcore.h" /* for exec_bfd */
58 #include "remote-fileio.h"
59 #include "gdb/fileio.h"
61 #include "xml-support.h"
63 #include "memory-map.h"
65 #include "tracepoint.h"
70 /* Temp hacks for tracepoint encoding migration. */
71 static char *target_buf
;
72 static long target_buf_size
;
74 /* The size to align memory write packets, when practical. The protocol
75 does not guarantee any alignment, and gdb will generate short
76 writes and unaligned writes, but even as a best-effort attempt this
77 can improve bulk transfers. For instance, if a write is misaligned
78 relative to the target's data bus, the stub may need to make an extra
79 round trip fetching data from the target. This doesn't make a
80 huge difference, but it's easy to do, so we try to be helpful.
82 The alignment chosen is arbitrary; usually data bus width is
83 important here, not the possibly larger cache line size. */
84 enum { REMOTE_ALIGN_WRITES
= 16 };
86 /* Prototypes for local functions. */
87 static void cleanup_sigint_signal_handler (void *dummy
);
88 static void initialize_sigint_signal_handler (void);
89 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
90 static int getpkt_or_notif_sane (char **buf
, long *sizeof_buf
,
93 static void handle_remote_sigint (int);
94 static void handle_remote_sigint_twice (int);
95 static void async_remote_interrupt (gdb_client_data
);
96 void async_remote_interrupt_twice (gdb_client_data
);
98 static void remote_files_info (struct target_ops
*ignore
);
100 static void remote_prepare_to_store (struct regcache
*regcache
);
102 static void remote_open (char *name
, int from_tty
);
104 static void extended_remote_open (char *name
, int from_tty
);
106 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
);
108 static void remote_close (int quitting
);
110 static void remote_mourn (struct target_ops
*ops
);
112 static void extended_remote_restart (void);
114 static void extended_remote_mourn (struct target_ops
*);
116 static void remote_mourn_1 (struct target_ops
*);
118 static void remote_send (char **buf
, long *sizeof_buf_p
);
120 static int readchar (int timeout
);
122 static void remote_kill (struct target_ops
*ops
);
124 static int tohex (int nib
);
126 static int remote_can_async_p (void);
128 static int remote_is_async_p (void);
130 static void remote_async (void (*callback
) (enum inferior_event_type event_type
,
131 void *context
), void *context
);
133 static void remote_detach (struct target_ops
*ops
, char *args
, int from_tty
);
135 static void remote_interrupt (int signo
);
137 static void remote_interrupt_twice (int signo
);
139 static void interrupt_query (void);
141 static void set_general_thread (struct ptid ptid
);
142 static void set_continue_thread (struct ptid ptid
);
144 static void get_offsets (void);
146 static void skip_frame (void);
148 static long read_frame (char **buf_p
, long *sizeof_buf
);
150 static int hexnumlen (ULONGEST num
);
152 static void init_remote_ops (void);
154 static void init_extended_remote_ops (void);
156 static void remote_stop (ptid_t
);
158 static int ishex (int ch
, int *val
);
160 static int stubhex (int ch
);
162 static int hexnumstr (char *, ULONGEST
);
164 static int hexnumnstr (char *, ULONGEST
, int);
166 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
168 static void print_packet (char *);
170 static void compare_sections_command (char *, int);
172 static void packet_command (char *, int);
174 static int stub_unpack_int (char *buff
, int fieldlength
);
176 static ptid_t
remote_current_thread (ptid_t oldptid
);
178 static void remote_find_new_threads (void);
180 static void record_currthread (ptid_t currthread
);
182 static int fromhex (int a
);
184 extern int hex2bin (const char *hex
, gdb_byte
*bin
, int count
);
186 extern int bin2hex (const gdb_byte
*bin
, char *hex
, int count
);
188 static int putpkt_binary (char *buf
, int cnt
);
190 static void check_binary_download (CORE_ADDR addr
);
192 struct packet_config
;
194 static void show_packet_config_cmd (struct packet_config
*config
);
196 static void update_packet_config (struct packet_config
*config
);
198 static void set_remote_protocol_packet_cmd (char *args
, int from_tty
,
199 struct cmd_list_element
*c
);
201 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
203 struct cmd_list_element
*c
,
206 static char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
207 static ptid_t
read_ptid (char *buf
, char **obuf
);
209 static void remote_set_permissions (void);
212 static int remote_get_trace_status (struct trace_status
*ts
);
214 static int remote_upload_tracepoints (struct uploaded_tp
**utpp
);
216 static int remote_upload_trace_state_variables (struct uploaded_tsv
**utsvp
);
218 static void remote_query_supported (void);
220 static void remote_check_symbols (struct objfile
*objfile
);
222 void _initialize_remote (void);
225 static struct stop_reply
*stop_reply_xmalloc (void);
226 static void stop_reply_xfree (struct stop_reply
*);
227 static void do_stop_reply_xfree (void *arg
);
228 static void remote_parse_stop_reply (char *buf
, struct stop_reply
*);
229 static void push_stop_reply (struct stop_reply
*);
230 static void remote_get_pending_stop_replies (void);
231 static void discard_pending_stop_replies (int pid
);
232 static int peek_stop_reply (ptid_t ptid
);
234 static void remote_async_inferior_event_handler (gdb_client_data
);
235 static void remote_async_get_pending_events_handler (gdb_client_data
);
237 static void remote_terminal_ours (void);
239 static int remote_read_description_p (struct target_ops
*target
);
241 static void remote_console_output (char *msg
);
243 static int remote_supports_cond_breakpoints (void);
245 /* The non-stop remote protocol provisions for one pending stop reply.
246 This is where we keep it until it is acknowledged. */
248 static struct stop_reply
*pending_stop_reply
= NULL
;
252 static struct cmd_list_element
*remote_cmdlist
;
254 /* For "set remote" and "show remote". */
256 static struct cmd_list_element
*remote_set_cmdlist
;
257 static struct cmd_list_element
*remote_show_cmdlist
;
259 /* Description of the remote protocol state for the currently
260 connected target. This is per-target state, and independent of the
261 selected architecture. */
265 /* A buffer to use for incoming packets, and its current size. The
266 buffer is grown dynamically for larger incoming packets.
267 Outgoing packets may also be constructed in this buffer.
268 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
269 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
274 /* If we negotiated packet size explicitly (and thus can bypass
275 heuristics for the largest packet size that will not overflow
276 a buffer in the stub), this will be set to that packet size.
277 Otherwise zero, meaning to use the guessed size. */
278 long explicit_packet_size
;
280 /* remote_wait is normally called when the target is running and
281 waits for a stop reply packet. But sometimes we need to call it
282 when the target is already stopped. We can send a "?" packet
283 and have remote_wait read the response. Or, if we already have
284 the response, we can stash it in BUF and tell remote_wait to
285 skip calling getpkt. This flag is set when BUF contains a
286 stop reply packet and the target is not waiting. */
287 int cached_wait_status
;
289 /* True, if in no ack mode. That is, neither GDB nor the stub will
290 expect acks from each other. The connection is assumed to be
294 /* True if we're connected in extended remote mode. */
297 /* True if the stub reported support for multi-process
299 int multi_process_aware
;
301 /* True if we resumed the target and we're waiting for the target to
302 stop. In the mean time, we can't start another command/query.
303 The remote server wouldn't be ready to process it, so we'd
304 timeout waiting for a reply that would never come and eventually
305 we'd close the connection. This can happen in asynchronous mode
306 because we allow GDB commands while the target is running. */
307 int waiting_for_stop_reply
;
309 /* True if the stub reports support for non-stop mode. */
312 /* True if the stub reports support for vCont;t. */
315 /* True if the stub reports support for conditional tracepoints. */
316 int cond_tracepoints
;
318 /* True if the stub reports support for target-side breakpoint
320 int cond_breakpoints
;
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 reports support for installing tracepoint while
330 int install_in_trace
;
332 /* True if the stub can continue running a trace while GDB is
334 int disconnected_tracing
;
336 /* True if the stub reports support for enabling and disabling
337 tracepoints while a trace experiment is running. */
338 int enable_disable_tracepoints
;
340 /* True if the stub can collect strings using tracenz bytecode. */
343 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
344 responded to that. */
348 /* Private data that we'll store in (struct thread_info)->private. */
349 struct private_thread_info
356 free_private_thread_info (struct private_thread_info
*info
)
362 /* Returns true if the multi-process extensions are in effect. */
364 remote_multi_process_p (struct remote_state
*rs
)
366 return rs
->multi_process_aware
;
369 /* This data could be associated with a target, but we do not always
370 have access to the current target when we need it, so for now it is
371 static. This will be fine for as long as only one target is in use
373 static struct remote_state remote_state
;
375 static struct remote_state
*
376 get_remote_state_raw (void)
378 return &remote_state
;
381 /* Description of the remote protocol for a given architecture. */
385 long offset
; /* Offset into G packet. */
386 long regnum
; /* GDB's internal register number. */
387 LONGEST pnum
; /* Remote protocol register number. */
388 int in_g_packet
; /* Always part of G packet. */
389 /* long size in bytes; == register_size (target_gdbarch, regnum);
391 /* char *name; == gdbarch_register_name (target_gdbarch, regnum);
395 struct remote_arch_state
397 /* Description of the remote protocol registers. */
398 long sizeof_g_packet
;
400 /* Description of the remote protocol registers indexed by REGNUM
401 (making an array gdbarch_num_regs in size). */
402 struct packet_reg
*regs
;
404 /* This is the size (in chars) of the first response to the ``g''
405 packet. It is used as a heuristic when determining the maximum
406 size of memory-read and memory-write packets. A target will
407 typically only reserve a buffer large enough to hold the ``g''
408 packet. The size does not include packet overhead (headers and
410 long actual_register_packet_size
;
412 /* This is the maximum size (in chars) of a non read/write packet.
413 It is also used as a cap on the size of read/write packets. */
414 long remote_packet_size
;
417 long sizeof_pkt
= 2000;
419 /* Utility: generate error from an incoming stub packet. */
421 trace_error (char *buf
)
424 return; /* not an error msg */
427 case '1': /* malformed packet error */
428 if (*++buf
== '0') /* general case: */
429 error (_("remote.c: error in outgoing packet."));
431 error (_("remote.c: error in outgoing packet at field #%ld."),
432 strtol (buf
, NULL
, 16));
434 error (_("trace API error 0x%s."), ++buf
);
436 error (_("Target returns error code '%s'."), buf
);
440 /* Utility: wait for reply from stub, while accepting "O" packets. */
442 remote_get_noisy_reply (char **buf_p
,
445 do /* Loop on reply from remote stub. */
449 QUIT
; /* Allow user to bail out with ^C. */
450 getpkt (buf_p
, sizeof_buf
, 0);
454 else if (strncmp (buf
, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
457 CORE_ADDR from
, to
, org_to
;
459 int adjusted_size
= 0;
460 volatile struct gdb_exception ex
;
462 p
= buf
+ strlen ("qRelocInsn:");
463 pp
= unpack_varlen_hex (p
, &ul
);
465 error (_("invalid qRelocInsn packet: %s"), buf
);
469 unpack_varlen_hex (p
, &ul
);
474 TRY_CATCH (ex
, RETURN_MASK_ALL
)
476 gdbarch_relocate_instruction (target_gdbarch
, &to
, from
);
480 adjusted_size
= to
- org_to
;
482 sprintf (buf
, "qRelocInsn:%x", adjusted_size
);
485 else if (ex
.reason
< 0 && ex
.error
== MEMORY_ERROR
)
487 /* Propagate memory errors silently back to the target.
488 The stub may have limited the range of addresses we
489 can write to, for example. */
494 /* Something unexpectedly bad happened. Be verbose so
495 we can tell what, and propagate the error back to the
496 stub, so it doesn't get stuck waiting for a
498 exception_fprintf (gdb_stderr
, ex
,
499 _("warning: relocating instruction: "));
503 else if (buf
[0] == 'O' && buf
[1] != 'K')
504 remote_console_output (buf
+ 1); /* 'O' message from stub */
506 return buf
; /* Here's the actual reply. */
511 /* Handle for retreving the remote protocol data from gdbarch. */
512 static struct gdbarch_data
*remote_gdbarch_data_handle
;
514 static struct remote_arch_state
*
515 get_remote_arch_state (void)
517 return gdbarch_data (target_gdbarch
, remote_gdbarch_data_handle
);
520 /* Fetch the global remote target state. */
522 static struct remote_state
*
523 get_remote_state (void)
525 /* Make sure that the remote architecture state has been
526 initialized, because doing so might reallocate rs->buf. Any
527 function which calls getpkt also needs to be mindful of changes
528 to rs->buf, but this call limits the number of places which run
530 get_remote_arch_state ();
532 return get_remote_state_raw ();
536 compare_pnums (const void *lhs_
, const void *rhs_
)
538 const struct packet_reg
* const *lhs
= lhs_
;
539 const struct packet_reg
* const *rhs
= rhs_
;
541 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
543 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
550 map_regcache_remote_table (struct gdbarch
*gdbarch
, struct packet_reg
*regs
)
552 int regnum
, num_remote_regs
, offset
;
553 struct packet_reg
**remote_regs
;
555 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
557 struct packet_reg
*r
= ®s
[regnum
];
559 if (register_size (gdbarch
, regnum
) == 0)
560 /* Do not try to fetch zero-sized (placeholder) registers. */
563 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
568 /* Define the g/G packet format as the contents of each register
569 with a remote protocol number, in order of ascending protocol
572 remote_regs
= alloca (gdbarch_num_regs (gdbarch
)
573 * sizeof (struct packet_reg
*));
574 for (num_remote_regs
= 0, regnum
= 0;
575 regnum
< gdbarch_num_regs (gdbarch
);
577 if (regs
[regnum
].pnum
!= -1)
578 remote_regs
[num_remote_regs
++] = ®s
[regnum
];
580 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
583 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
585 remote_regs
[regnum
]->in_g_packet
= 1;
586 remote_regs
[regnum
]->offset
= offset
;
587 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
593 /* Given the architecture described by GDBARCH, return the remote
594 protocol register's number and the register's offset in the g/G
595 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
596 If the target does not have a mapping for REGNUM, return false,
597 otherwise, return true. */
600 remote_register_number_and_offset (struct gdbarch
*gdbarch
, int regnum
,
601 int *pnum
, int *poffset
)
604 struct packet_reg
*regs
;
605 struct cleanup
*old_chain
;
607 gdb_assert (regnum
< gdbarch_num_regs (gdbarch
));
609 regs
= xcalloc (gdbarch_num_regs (gdbarch
), sizeof (struct packet_reg
));
610 old_chain
= make_cleanup (xfree
, regs
);
612 sizeof_g_packet
= map_regcache_remote_table (gdbarch
, regs
);
614 *pnum
= regs
[regnum
].pnum
;
615 *poffset
= regs
[regnum
].offset
;
617 do_cleanups (old_chain
);
623 init_remote_state (struct gdbarch
*gdbarch
)
625 struct remote_state
*rs
= get_remote_state_raw ();
626 struct remote_arch_state
*rsa
;
628 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
630 /* Use the architecture to build a regnum<->pnum table, which will be
631 1:1 unless a feature set specifies otherwise. */
632 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
,
633 gdbarch_num_regs (gdbarch
),
636 /* Record the maximum possible size of the g packet - it may turn out
638 rsa
->sizeof_g_packet
= map_regcache_remote_table (gdbarch
, rsa
->regs
);
640 /* Default maximum number of characters in a packet body. Many
641 remote stubs have a hardwired buffer size of 400 bytes
642 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
643 as the maximum packet-size to ensure that the packet and an extra
644 NUL character can always fit in the buffer. This stops GDB
645 trashing stubs that try to squeeze an extra NUL into what is
646 already a full buffer (As of 1999-12-04 that was most stubs). */
647 rsa
->remote_packet_size
= 400 - 1;
649 /* This one is filled in when a ``g'' packet is received. */
650 rsa
->actual_register_packet_size
= 0;
652 /* Should rsa->sizeof_g_packet needs more space than the
653 default, adjust the size accordingly. Remember that each byte is
654 encoded as two characters. 32 is the overhead for the packet
655 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
656 (``$NN:G...#NN'') is a better guess, the below has been padded a
658 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
659 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
661 /* Make sure that the packet buffer is plenty big enough for
662 this architecture. */
663 if (rs
->buf_size
< rsa
->remote_packet_size
)
665 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
666 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
672 /* Return the current allowed size of a remote packet. This is
673 inferred from the current architecture, and should be used to
674 limit the length of outgoing packets. */
676 get_remote_packet_size (void)
678 struct remote_state
*rs
= get_remote_state ();
679 struct remote_arch_state
*rsa
= get_remote_arch_state ();
681 if (rs
->explicit_packet_size
)
682 return rs
->explicit_packet_size
;
684 return rsa
->remote_packet_size
;
687 static struct packet_reg
*
688 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
690 if (regnum
< 0 && regnum
>= gdbarch_num_regs (target_gdbarch
))
694 struct packet_reg
*r
= &rsa
->regs
[regnum
];
696 gdb_assert (r
->regnum
== regnum
);
701 static struct packet_reg
*
702 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
706 for (i
= 0; i
< gdbarch_num_regs (target_gdbarch
); i
++)
708 struct packet_reg
*r
= &rsa
->regs
[i
];
716 /* FIXME: graces/2002-08-08: These variables should eventually be
717 bound to an instance of the target object (as in gdbarch-tdep()),
718 when such a thing exists. */
720 /* This is set to the data address of the access causing the target
721 to stop for a watchpoint. */
722 static CORE_ADDR remote_watch_data_address
;
724 /* This is non-zero if target stopped for a watchpoint. */
725 static int remote_stopped_by_watchpoint_p
;
727 static struct target_ops remote_ops
;
729 static struct target_ops extended_remote_ops
;
731 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
732 ``forever'' still use the normal timeout mechanism. This is
733 currently used by the ASYNC code to guarentee that target reads
734 during the initial connect always time-out. Once getpkt has been
735 modified to return a timeout indication and, in turn
736 remote_wait()/wait_for_inferior() have gained a timeout parameter
738 static int wait_forever_enabled_p
= 1;
740 /* Allow the user to specify what sequence to send to the remote
741 when he requests a program interruption: Although ^C is usually
742 what remote systems expect (this is the default, here), it is
743 sometimes preferable to send a break. On other systems such
744 as the Linux kernel, a break followed by g, which is Magic SysRq g
745 is required in order to interrupt the execution. */
746 const char interrupt_sequence_control_c
[] = "Ctrl-C";
747 const char interrupt_sequence_break
[] = "BREAK";
748 const char interrupt_sequence_break_g
[] = "BREAK-g";
749 static const char *const interrupt_sequence_modes
[] =
751 interrupt_sequence_control_c
,
752 interrupt_sequence_break
,
753 interrupt_sequence_break_g
,
756 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
759 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
760 struct cmd_list_element
*c
,
763 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
764 fprintf_filtered (file
,
765 _("Send the ASCII ETX character (Ctrl-c) "
766 "to the remote target to interrupt the "
767 "execution of the program.\n"));
768 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
769 fprintf_filtered (file
,
770 _("send a break signal to the remote target "
771 "to interrupt the execution of the program.\n"));
772 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
773 fprintf_filtered (file
,
774 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
775 "the remote target to interrupt the execution "
776 "of Linux kernel.\n"));
778 internal_error (__FILE__
, __LINE__
,
779 _("Invalid value for interrupt_sequence_mode: %s."),
780 interrupt_sequence_mode
);
783 /* This boolean variable specifies whether interrupt_sequence is sent
784 to the remote target when gdb connects to it.
785 This is mostly needed when you debug the Linux kernel: The Linux kernel
786 expects BREAK g which is Magic SysRq g for connecting gdb. */
787 static int interrupt_on_connect
= 0;
789 /* This variable is used to implement the "set/show remotebreak" commands.
790 Since these commands are now deprecated in favor of "set/show remote
791 interrupt-sequence", it no longer has any effect on the code. */
792 static int remote_break
;
795 set_remotebreak (char *args
, int from_tty
, struct cmd_list_element
*c
)
798 interrupt_sequence_mode
= interrupt_sequence_break
;
800 interrupt_sequence_mode
= interrupt_sequence_control_c
;
804 show_remotebreak (struct ui_file
*file
, int from_tty
,
805 struct cmd_list_element
*c
,
810 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
811 remote_open knows that we don't have a file open when the program
813 static struct serial
*remote_desc
= NULL
;
815 /* This variable sets the number of bits in an address that are to be
816 sent in a memory ("M" or "m") packet. Normally, after stripping
817 leading zeros, the entire address would be sent. This variable
818 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
819 initial implementation of remote.c restricted the address sent in
820 memory packets to ``host::sizeof long'' bytes - (typically 32
821 bits). Consequently, for 64 bit targets, the upper 32 bits of an
822 address was never sent. Since fixing this bug may cause a break in
823 some remote targets this variable is principly provided to
824 facilitate backward compatibility. */
826 static int remote_address_size
;
828 /* Temporary to track who currently owns the terminal. See
829 remote_terminal_* for more details. */
831 static int remote_async_terminal_ours_p
;
833 /* The executable file to use for "run" on the remote side. */
835 static char *remote_exec_file
= "";
838 /* User configurable variables for the number of characters in a
839 memory read/write packet. MIN (rsa->remote_packet_size,
840 rsa->sizeof_g_packet) is the default. Some targets need smaller
841 values (fifo overruns, et.al.) and some users need larger values
842 (speed up transfers). The variables ``preferred_*'' (the user
843 request), ``current_*'' (what was actually set) and ``forced_*''
844 (Positive - a soft limit, negative - a hard limit). */
846 struct memory_packet_config
853 /* Compute the current size of a read/write packet. Since this makes
854 use of ``actual_register_packet_size'' the computation is dynamic. */
857 get_memory_packet_size (struct memory_packet_config
*config
)
859 struct remote_state
*rs
= get_remote_state ();
860 struct remote_arch_state
*rsa
= get_remote_arch_state ();
862 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
863 law?) that some hosts don't cope very well with large alloca()
864 calls. Eventually the alloca() code will be replaced by calls to
865 xmalloc() and make_cleanups() allowing this restriction to either
866 be lifted or removed. */
867 #ifndef MAX_REMOTE_PACKET_SIZE
868 #define MAX_REMOTE_PACKET_SIZE 16384
870 /* NOTE: 20 ensures we can write at least one byte. */
871 #ifndef MIN_REMOTE_PACKET_SIZE
872 #define MIN_REMOTE_PACKET_SIZE 20
877 if (config
->size
<= 0)
878 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
880 what_they_get
= config
->size
;
884 what_they_get
= get_remote_packet_size ();
885 /* Limit the packet to the size specified by the user. */
887 && what_they_get
> config
->size
)
888 what_they_get
= config
->size
;
890 /* Limit it to the size of the targets ``g'' response unless we have
891 permission from the stub to use a larger packet size. */
892 if (rs
->explicit_packet_size
== 0
893 && rsa
->actual_register_packet_size
> 0
894 && what_they_get
> rsa
->actual_register_packet_size
)
895 what_they_get
= rsa
->actual_register_packet_size
;
897 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
898 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
899 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
900 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
902 /* Make sure there is room in the global buffer for this packet
903 (including its trailing NUL byte). */
904 if (rs
->buf_size
< what_they_get
+ 1)
906 rs
->buf_size
= 2 * what_they_get
;
907 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
910 return what_they_get
;
913 /* Update the size of a read/write packet. If they user wants
914 something really big then do a sanity check. */
917 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
919 int fixed_p
= config
->fixed_p
;
920 long size
= config
->size
;
923 error (_("Argument required (integer, `fixed' or `limited')."));
924 else if (strcmp (args
, "hard") == 0
925 || strcmp (args
, "fixed") == 0)
927 else if (strcmp (args
, "soft") == 0
928 || strcmp (args
, "limit") == 0)
934 size
= strtoul (args
, &end
, 0);
936 error (_("Invalid %s (bad syntax)."), config
->name
);
938 /* Instead of explicitly capping the size of a packet to
939 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
940 instead allowed to set the size to something arbitrarily
942 if (size
> MAX_REMOTE_PACKET_SIZE
)
943 error (_("Invalid %s (too large)."), config
->name
);
947 if (fixed_p
&& !config
->fixed_p
)
949 if (! query (_("The target may not be able to correctly handle a %s\n"
950 "of %ld bytes. Change the packet size? "),
952 error (_("Packet size not changed."));
954 /* Update the config. */
955 config
->fixed_p
= fixed_p
;
960 show_memory_packet_size (struct memory_packet_config
*config
)
962 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
964 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
965 get_memory_packet_size (config
));
967 printf_filtered (_("Packets are limited to %ld bytes.\n"),
968 get_memory_packet_size (config
));
971 static struct memory_packet_config memory_write_packet_config
=
973 "memory-write-packet-size",
977 set_memory_write_packet_size (char *args
, int from_tty
)
979 set_memory_packet_size (args
, &memory_write_packet_config
);
983 show_memory_write_packet_size (char *args
, int from_tty
)
985 show_memory_packet_size (&memory_write_packet_config
);
989 get_memory_write_packet_size (void)
991 return get_memory_packet_size (&memory_write_packet_config
);
994 static struct memory_packet_config memory_read_packet_config
=
996 "memory-read-packet-size",
1000 set_memory_read_packet_size (char *args
, int from_tty
)
1002 set_memory_packet_size (args
, &memory_read_packet_config
);
1006 show_memory_read_packet_size (char *args
, int from_tty
)
1008 show_memory_packet_size (&memory_read_packet_config
);
1012 get_memory_read_packet_size (void)
1014 long size
= get_memory_packet_size (&memory_read_packet_config
);
1016 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1017 extra buffer size argument before the memory read size can be
1018 increased beyond this. */
1019 if (size
> get_remote_packet_size ())
1020 size
= get_remote_packet_size ();
1025 /* Generic configuration support for packets the stub optionally
1026 supports. Allows the user to specify the use of the packet as well
1027 as allowing GDB to auto-detect support in the remote stub. */
1031 PACKET_SUPPORT_UNKNOWN
= 0,
1036 struct packet_config
1040 enum auto_boolean detect
;
1041 enum packet_support support
;
1044 /* Analyze a packet's return value and update the packet config
1055 update_packet_config (struct packet_config
*config
)
1057 switch (config
->detect
)
1059 case AUTO_BOOLEAN_TRUE
:
1060 config
->support
= PACKET_ENABLE
;
1062 case AUTO_BOOLEAN_FALSE
:
1063 config
->support
= PACKET_DISABLE
;
1065 case AUTO_BOOLEAN_AUTO
:
1066 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1072 show_packet_config_cmd (struct packet_config
*config
)
1074 char *support
= "internal-error";
1076 switch (config
->support
)
1079 support
= "enabled";
1081 case PACKET_DISABLE
:
1082 support
= "disabled";
1084 case PACKET_SUPPORT_UNKNOWN
:
1085 support
= "unknown";
1088 switch (config
->detect
)
1090 case AUTO_BOOLEAN_AUTO
:
1091 printf_filtered (_("Support for the `%s' packet "
1092 "is auto-detected, currently %s.\n"),
1093 config
->name
, support
);
1095 case AUTO_BOOLEAN_TRUE
:
1096 case AUTO_BOOLEAN_FALSE
:
1097 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1098 config
->name
, support
);
1104 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
1105 const char *title
, int legacy
)
1111 config
->name
= name
;
1112 config
->title
= title
;
1113 config
->detect
= AUTO_BOOLEAN_AUTO
;
1114 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1115 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1117 show_doc
= xstrprintf ("Show current use of remote "
1118 "protocol `%s' (%s) packet",
1120 /* set/show TITLE-packet {auto,on,off} */
1121 cmd_name
= xstrprintf ("%s-packet", title
);
1122 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
1123 &config
->detect
, set_doc
,
1124 show_doc
, NULL
, /* help_doc */
1125 set_remote_protocol_packet_cmd
,
1126 show_remote_protocol_packet_cmd
,
1127 &remote_set_cmdlist
, &remote_show_cmdlist
);
1128 /* The command code copies the documentation strings. */
1131 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1136 legacy_name
= xstrprintf ("%s-packet", name
);
1137 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1138 &remote_set_cmdlist
);
1139 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1140 &remote_show_cmdlist
);
1144 static enum packet_result
1145 packet_check_result (const char *buf
)
1149 /* The stub recognized the packet request. Check that the
1150 operation succeeded. */
1152 && isxdigit (buf
[1]) && isxdigit (buf
[2])
1154 /* "Enn" - definitly an error. */
1155 return PACKET_ERROR
;
1157 /* Always treat "E." as an error. This will be used for
1158 more verbose error messages, such as E.memtypes. */
1159 if (buf
[0] == 'E' && buf
[1] == '.')
1160 return PACKET_ERROR
;
1162 /* The packet may or may not be OK. Just assume it is. */
1166 /* The stub does not support the packet. */
1167 return PACKET_UNKNOWN
;
1170 static enum packet_result
1171 packet_ok (const char *buf
, struct packet_config
*config
)
1173 enum packet_result result
;
1175 result
= packet_check_result (buf
);
1180 /* The stub recognized the packet request. */
1181 switch (config
->support
)
1183 case PACKET_SUPPORT_UNKNOWN
:
1185 fprintf_unfiltered (gdb_stdlog
,
1186 "Packet %s (%s) is supported\n",
1187 config
->name
, config
->title
);
1188 config
->support
= PACKET_ENABLE
;
1190 case PACKET_DISABLE
:
1191 internal_error (__FILE__
, __LINE__
,
1192 _("packet_ok: attempt to use a disabled packet"));
1198 case PACKET_UNKNOWN
:
1199 /* The stub does not support the packet. */
1200 switch (config
->support
)
1203 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
1204 /* If the stub previously indicated that the packet was
1205 supported then there is a protocol error.. */
1206 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1207 config
->name
, config
->title
);
1209 /* The user set it wrong. */
1210 error (_("Enabled packet %s (%s) not recognized by stub"),
1211 config
->name
, config
->title
);
1213 case PACKET_SUPPORT_UNKNOWN
:
1215 fprintf_unfiltered (gdb_stdlog
,
1216 "Packet %s (%s) is NOT supported\n",
1217 config
->name
, config
->title
);
1218 config
->support
= PACKET_DISABLE
;
1220 case PACKET_DISABLE
:
1242 PACKET_vFile_pwrite
,
1244 PACKET_vFile_unlink
,
1245 PACKET_vFile_readlink
,
1247 PACKET_qXfer_features
,
1248 PACKET_qXfer_libraries
,
1249 PACKET_qXfer_libraries_svr4
,
1250 PACKET_qXfer_memory_map
,
1251 PACKET_qXfer_spu_read
,
1252 PACKET_qXfer_spu_write
,
1253 PACKET_qXfer_osdata
,
1254 PACKET_qXfer_threads
,
1255 PACKET_qXfer_statictrace_read
,
1256 PACKET_qXfer_traceframe_info
,
1261 PACKET_QPassSignals
,
1262 PACKET_QProgramSignals
,
1263 PACKET_qSearch_memory
,
1266 PACKET_QStartNoAckMode
,
1268 PACKET_qXfer_siginfo_read
,
1269 PACKET_qXfer_siginfo_write
,
1271 PACKET_ConditionalTracepoints
,
1272 PACKET_ConditionalBreakpoints
,
1273 PACKET_FastTracepoints
,
1274 PACKET_StaticTracepoints
,
1275 PACKET_InstallInTrace
,
1278 PACKET_TracepointSource
,
1281 PACKET_QDisableRandomization
,
1286 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
1289 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
1290 struct cmd_list_element
*c
)
1292 struct packet_config
*packet
;
1294 for (packet
= remote_protocol_packets
;
1295 packet
< &remote_protocol_packets
[PACKET_MAX
];
1298 if (&packet
->detect
== c
->var
)
1300 update_packet_config (packet
);
1304 internal_error (__FILE__
, __LINE__
, _("Could not find config for %s"),
1309 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
1310 struct cmd_list_element
*c
,
1313 struct packet_config
*packet
;
1315 for (packet
= remote_protocol_packets
;
1316 packet
< &remote_protocol_packets
[PACKET_MAX
];
1319 if (&packet
->detect
== c
->var
)
1321 show_packet_config_cmd (packet
);
1325 internal_error (__FILE__
, __LINE__
, _("Could not find config for %s"),
1329 /* Should we try one of the 'Z' requests? */
1333 Z_PACKET_SOFTWARE_BP
,
1334 Z_PACKET_HARDWARE_BP
,
1341 /* For compatibility with older distributions. Provide a ``set remote
1342 Z-packet ...'' command that updates all the Z packet types. */
1344 static enum auto_boolean remote_Z_packet_detect
;
1347 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1348 struct cmd_list_element
*c
)
1352 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1354 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1355 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1360 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1361 struct cmd_list_element
*c
,
1366 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1368 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1372 /* Should we try the 'ThreadInfo' query packet?
1374 This variable (NOT available to the user: auto-detect only!)
1375 determines whether GDB will use the new, simpler "ThreadInfo"
1376 query or the older, more complex syntax for thread queries.
1377 This is an auto-detect variable (set to true at each connect,
1378 and set to false when the target fails to recognize it). */
1380 static int use_threadinfo_query
;
1381 static int use_threadextra_query
;
1383 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1384 static struct async_signal_handler
*sigint_remote_twice_token
;
1385 static struct async_signal_handler
*sigint_remote_token
;
1388 /* Asynchronous signal handle registered as event loop source for
1389 when we have pending events ready to be passed to the core. */
1391 static struct async_event_handler
*remote_async_inferior_event_token
;
1393 /* Asynchronous signal handle registered as event loop source for when
1394 the remote sent us a %Stop notification. The registered callback
1395 will do a vStopped sequence to pull the rest of the events out of
1396 the remote side into our event queue. */
1398 static struct async_event_handler
*remote_async_get_pending_events_token
;
1401 static ptid_t magic_null_ptid
;
1402 static ptid_t not_sent_ptid
;
1403 static ptid_t any_thread_ptid
;
1405 /* These are the threads which we last sent to the remote system. The
1406 TID member will be -1 for all or -2 for not sent yet. */
1408 static ptid_t general_thread
;
1409 static ptid_t continue_thread
;
1411 /* This the traceframe which we last selected on the remote system.
1412 It will be -1 if no traceframe is selected. */
1413 static int remote_traceframe_number
= -1;
1415 /* Find out if the stub attached to PID (and hence GDB should offer to
1416 detach instead of killing it when bailing out). */
1419 remote_query_attached (int pid
)
1421 struct remote_state
*rs
= get_remote_state ();
1423 if (remote_protocol_packets
[PACKET_qAttached
].support
== PACKET_DISABLE
)
1426 if (remote_multi_process_p (rs
))
1427 sprintf (rs
->buf
, "qAttached:%x", pid
);
1429 sprintf (rs
->buf
, "qAttached");
1432 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1434 switch (packet_ok (rs
->buf
,
1435 &remote_protocol_packets
[PACKET_qAttached
]))
1438 if (strcmp (rs
->buf
, "1") == 0)
1442 warning (_("Remote failure reply: %s"), rs
->buf
);
1444 case PACKET_UNKNOWN
:
1451 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1452 has been invented by GDB, instead of reported by the target. Since
1453 we can be connected to a remote system before before knowing about
1454 any inferior, mark the target with execution when we find the first
1455 inferior. If ATTACHED is 1, then we had just attached to this
1456 inferior. If it is 0, then we just created this inferior. If it
1457 is -1, then try querying the remote stub to find out if it had
1458 attached to the inferior or not. */
1460 static struct inferior
*
1461 remote_add_inferior (int fake_pid_p
, int pid
, int attached
)
1463 struct inferior
*inf
;
1465 /* Check whether this process we're learning about is to be
1466 considered attached, or if is to be considered to have been
1467 spawned by the stub. */
1469 attached
= remote_query_attached (pid
);
1471 if (gdbarch_has_global_solist (target_gdbarch
))
1473 /* If the target shares code across all inferiors, then every
1474 attach adds a new inferior. */
1475 inf
= add_inferior (pid
);
1477 /* ... and every inferior is bound to the same program space.
1478 However, each inferior may still have its own address
1480 inf
->aspace
= maybe_new_address_space ();
1481 inf
->pspace
= current_program_space
;
1485 /* In the traditional debugging scenario, there's a 1-1 match
1486 between program/address spaces. We simply bind the inferior
1487 to the program space's address space. */
1488 inf
= current_inferior ();
1489 inferior_appeared (inf
, pid
);
1492 inf
->attach_flag
= attached
;
1493 inf
->fake_pid_p
= fake_pid_p
;
1498 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1499 according to RUNNING. */
1502 remote_add_thread (ptid_t ptid
, int running
)
1506 set_executing (ptid
, running
);
1507 set_running (ptid
, running
);
1510 /* Come here when we learn about a thread id from the remote target.
1511 It may be the first time we hear about such thread, so take the
1512 opportunity to add it to GDB's thread list. In case this is the
1513 first time we're noticing its corresponding inferior, add it to
1514 GDB's inferior list as well. */
1517 remote_notice_new_inferior (ptid_t currthread
, int running
)
1519 /* If this is a new thread, add it to GDB's thread list.
1520 If we leave it up to WFI to do this, bad things will happen. */
1522 if (in_thread_list (currthread
) && is_exited (currthread
))
1524 /* We're seeing an event on a thread id we knew had exited.
1525 This has to be a new thread reusing the old id. Add it. */
1526 remote_add_thread (currthread
, running
);
1530 if (!in_thread_list (currthread
))
1532 struct inferior
*inf
= NULL
;
1533 int pid
= ptid_get_pid (currthread
);
1535 if (ptid_is_pid (inferior_ptid
)
1536 && pid
== ptid_get_pid (inferior_ptid
))
1538 /* inferior_ptid has no thread member yet. This can happen
1539 with the vAttach -> remote_wait,"TAAthread:" path if the
1540 stub doesn't support qC. This is the first stop reported
1541 after an attach, so this is the main thread. Update the
1542 ptid in the thread list. */
1543 if (in_thread_list (pid_to_ptid (pid
)))
1544 thread_change_ptid (inferior_ptid
, currthread
);
1547 remote_add_thread (currthread
, running
);
1548 inferior_ptid
= currthread
;
1553 if (ptid_equal (magic_null_ptid
, inferior_ptid
))
1555 /* inferior_ptid is not set yet. This can happen with the
1556 vRun -> remote_wait,"TAAthread:" path if the stub
1557 doesn't support qC. This is the first stop reported
1558 after an attach, so this is the main thread. Update the
1559 ptid in the thread list. */
1560 thread_change_ptid (inferior_ptid
, currthread
);
1564 /* When connecting to a target remote, or to a target
1565 extended-remote which already was debugging an inferior, we
1566 may not know about it yet. Add it before adding its child
1567 thread, so notifications are emitted in a sensible order. */
1568 if (!in_inferior_list (ptid_get_pid (currthread
)))
1570 struct remote_state
*rs
= get_remote_state ();
1571 int fake_pid_p
= !remote_multi_process_p (rs
);
1573 inf
= remote_add_inferior (fake_pid_p
,
1574 ptid_get_pid (currthread
), -1);
1577 /* This is really a new thread. Add it. */
1578 remote_add_thread (currthread
, running
);
1580 /* If we found a new inferior, let the common code do whatever
1581 it needs to with it (e.g., read shared libraries, insert
1584 notice_new_inferior (currthread
, running
, 0);
1588 /* Return the private thread data, creating it if necessary. */
1590 static struct private_thread_info
*
1591 demand_private_info (ptid_t ptid
)
1593 struct thread_info
*info
= find_thread_ptid (ptid
);
1599 info
->private = xmalloc (sizeof (*(info
->private)));
1600 info
->private_dtor
= free_private_thread_info
;
1601 info
->private->core
= -1;
1602 info
->private->extra
= 0;
1605 return info
->private;
1608 /* Call this function as a result of
1609 1) A halt indication (T packet) containing a thread id
1610 2) A direct query of currthread
1611 3) Successful execution of set thread */
1614 record_currthread (ptid_t currthread
)
1616 general_thread
= currthread
;
1619 static char *last_pass_packet
;
1621 /* If 'QPassSignals' is supported, tell the remote stub what signals
1622 it can simply pass through to the inferior without reporting. */
1625 remote_pass_signals (int numsigs
, unsigned char *pass_signals
)
1627 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1629 char *pass_packet
, *p
;
1632 gdb_assert (numsigs
< 256);
1633 for (i
= 0; i
< numsigs
; i
++)
1635 if (pass_signals
[i
])
1638 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1639 strcpy (pass_packet
, "QPassSignals:");
1640 p
= pass_packet
+ strlen (pass_packet
);
1641 for (i
= 0; i
< numsigs
; i
++)
1643 if (pass_signals
[i
])
1646 *p
++ = tohex (i
>> 4);
1647 *p
++ = tohex (i
& 15);
1656 if (!last_pass_packet
|| strcmp (last_pass_packet
, pass_packet
))
1658 struct remote_state
*rs
= get_remote_state ();
1659 char *buf
= rs
->buf
;
1661 putpkt (pass_packet
);
1662 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1663 packet_ok (buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1664 if (last_pass_packet
)
1665 xfree (last_pass_packet
);
1666 last_pass_packet
= pass_packet
;
1669 xfree (pass_packet
);
1673 /* The last QProgramSignals packet sent to the target. We bypass
1674 sending a new program signals list down to the target if the new
1675 packet is exactly the same as the last we sent. IOW, we only let
1676 the target know about program signals list changes. */
1678 static char *last_program_signals_packet
;
1680 /* If 'QProgramSignals' is supported, tell the remote stub what
1681 signals it should pass through to the inferior when detaching. */
1684 remote_program_signals (int numsigs
, unsigned char *signals
)
1686 if (remote_protocol_packets
[PACKET_QProgramSignals
].support
!= PACKET_DISABLE
)
1691 gdb_assert (numsigs
< 256);
1692 for (i
= 0; i
< numsigs
; i
++)
1697 packet
= xmalloc (count
* 3 + strlen ("QProgramSignals:") + 1);
1698 strcpy (packet
, "QProgramSignals:");
1699 p
= packet
+ strlen (packet
);
1700 for (i
= 0; i
< numsigs
; i
++)
1702 if (signal_pass_state (i
))
1705 *p
++ = tohex (i
>> 4);
1706 *p
++ = tohex (i
& 15);
1715 if (!last_program_signals_packet
1716 || strcmp (last_program_signals_packet
, packet
) != 0)
1718 struct remote_state
*rs
= get_remote_state ();
1719 char *buf
= rs
->buf
;
1722 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1723 packet_ok (buf
, &remote_protocol_packets
[PACKET_QProgramSignals
]);
1724 xfree (last_program_signals_packet
);
1725 last_program_signals_packet
= packet
;
1732 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1733 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1734 thread. If GEN is set, set the general thread, if not, then set
1735 the step/continue thread. */
1737 set_thread (struct ptid ptid
, int gen
)
1739 struct remote_state
*rs
= get_remote_state ();
1740 ptid_t state
= gen
? general_thread
: continue_thread
;
1741 char *buf
= rs
->buf
;
1742 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1744 if (ptid_equal (state
, ptid
))
1748 *buf
++ = gen
? 'g' : 'c';
1749 if (ptid_equal (ptid
, magic_null_ptid
))
1750 xsnprintf (buf
, endbuf
- buf
, "0");
1751 else if (ptid_equal (ptid
, any_thread_ptid
))
1752 xsnprintf (buf
, endbuf
- buf
, "0");
1753 else if (ptid_equal (ptid
, minus_one_ptid
))
1754 xsnprintf (buf
, endbuf
- buf
, "-1");
1756 write_ptid (buf
, endbuf
, ptid
);
1758 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1760 general_thread
= ptid
;
1762 continue_thread
= ptid
;
1766 set_general_thread (struct ptid ptid
)
1768 set_thread (ptid
, 1);
1772 set_continue_thread (struct ptid ptid
)
1774 set_thread (ptid
, 0);
1777 /* Change the remote current process. Which thread within the process
1778 ends up selected isn't important, as long as it is the same process
1779 as what INFERIOR_PTID points to.
1781 This comes from that fact that there is no explicit notion of
1782 "selected process" in the protocol. The selected process for
1783 general operations is the process the selected general thread
1787 set_general_process (void)
1789 struct remote_state
*rs
= get_remote_state ();
1791 /* If the remote can't handle multiple processes, don't bother. */
1792 if (!rs
->extended
|| !remote_multi_process_p (rs
))
1795 /* We only need to change the remote current thread if it's pointing
1796 at some other process. */
1797 if (ptid_get_pid (general_thread
) != ptid_get_pid (inferior_ptid
))
1798 set_general_thread (inferior_ptid
);
1802 /* Return nonzero if the thread PTID is still alive on the remote
1806 remote_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1808 struct remote_state
*rs
= get_remote_state ();
1811 if (ptid_equal (ptid
, magic_null_ptid
))
1812 /* The main thread is always alive. */
1815 if (ptid_get_pid (ptid
) != 0 && ptid_get_tid (ptid
) == 0)
1816 /* The main thread is always alive. This can happen after a
1817 vAttach, if the remote side doesn't support
1822 endp
= rs
->buf
+ get_remote_packet_size ();
1825 write_ptid (p
, endp
, ptid
);
1828 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1829 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1832 /* About these extended threadlist and threadinfo packets. They are
1833 variable length packets but, the fields within them are often fixed
1834 length. They are redundent enough to send over UDP as is the
1835 remote protocol in general. There is a matching unit test module
1838 #define OPAQUETHREADBYTES 8
1840 /* a 64 bit opaque identifier */
1841 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
1843 /* WARNING: This threadref data structure comes from the remote O.S.,
1844 libstub protocol encoding, and remote.c. It is not particularly
1847 /* Right now, the internal structure is int. We want it to be bigger.
1848 Plan to fix this. */
1850 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1852 /* gdb_ext_thread_info is an internal GDB data structure which is
1853 equivalent to the reply of the remote threadinfo packet. */
1855 struct gdb_ext_thread_info
1857 threadref threadid
; /* External form of thread reference. */
1858 int active
; /* Has state interesting to GDB?
1860 char display
[256]; /* Brief state display, name,
1861 blocked/suspended. */
1862 char shortname
[32]; /* To be used to name threads. */
1863 char more_display
[256]; /* Long info, statistics, queue depth,
1867 /* The volume of remote transfers can be limited by submitting
1868 a mask containing bits specifying the desired information.
1869 Use a union of these values as the 'selection' parameter to
1870 get_thread_info. FIXME: Make these TAG names more thread specific. */
1872 #define TAG_THREADID 1
1873 #define TAG_EXISTS 2
1874 #define TAG_DISPLAY 4
1875 #define TAG_THREADNAME 8
1876 #define TAG_MOREDISPLAY 16
1878 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1880 char *unpack_varlen_hex (char *buff
, ULONGEST
*result
);
1882 static char *unpack_nibble (char *buf
, int *val
);
1884 static char *pack_nibble (char *buf
, int nibble
);
1886 static char *pack_hex_byte (char *pkt
, int /* unsigned char */ byte
);
1888 static char *unpack_byte (char *buf
, int *value
);
1890 static char *pack_int (char *buf
, int value
);
1892 static char *unpack_int (char *buf
, int *value
);
1894 static char *unpack_string (char *src
, char *dest
, int length
);
1896 static char *pack_threadid (char *pkt
, threadref
*id
);
1898 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1900 void int_to_threadref (threadref
*id
, int value
);
1902 static int threadref_to_int (threadref
*ref
);
1904 static void copy_threadref (threadref
*dest
, threadref
*src
);
1906 static int threadmatch (threadref
*dest
, threadref
*src
);
1908 static char *pack_threadinfo_request (char *pkt
, int mode
,
1911 static int remote_unpack_thread_info_response (char *pkt
,
1912 threadref
*expectedref
,
1913 struct gdb_ext_thread_info
1917 static int remote_get_threadinfo (threadref
*threadid
,
1918 int fieldset
, /*TAG mask */
1919 struct gdb_ext_thread_info
*info
);
1921 static char *pack_threadlist_request (char *pkt
, int startflag
,
1923 threadref
*nextthread
);
1925 static int parse_threadlist_response (char *pkt
,
1927 threadref
*original_echo
,
1928 threadref
*resultlist
,
1931 static int remote_get_threadlist (int startflag
,
1932 threadref
*nextthread
,
1936 threadref
*threadlist
);
1938 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1940 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1941 void *context
, int looplimit
);
1943 static int remote_newthread_step (threadref
*ref
, void *context
);
1946 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
1947 buffer we're allowed to write to. Returns
1948 BUF+CHARACTERS_WRITTEN. */
1951 write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
1954 struct remote_state
*rs
= get_remote_state ();
1956 if (remote_multi_process_p (rs
))
1958 pid
= ptid_get_pid (ptid
);
1960 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
1962 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
1964 tid
= ptid_get_tid (ptid
);
1966 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
1968 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
1973 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
1974 passed the last parsed char. Returns null_ptid on error. */
1977 read_ptid (char *buf
, char **obuf
)
1981 ULONGEST pid
= 0, tid
= 0;
1985 /* Multi-process ptid. */
1986 pp
= unpack_varlen_hex (p
+ 1, &pid
);
1988 error (_("invalid remote ptid: %s"), p
);
1991 pp
= unpack_varlen_hex (p
+ 1, &tid
);
1994 return ptid_build (pid
, 0, tid
);
1997 /* No multi-process. Just a tid. */
1998 pp
= unpack_varlen_hex (p
, &tid
);
2000 /* Since the stub is not sending a process id, then default to
2001 what's in inferior_ptid, unless it's null at this point. If so,
2002 then since there's no way to know the pid of the reported
2003 threads, use the magic number. */
2004 if (ptid_equal (inferior_ptid
, null_ptid
))
2005 pid
= ptid_get_pid (magic_null_ptid
);
2007 pid
= ptid_get_pid (inferior_ptid
);
2011 return ptid_build (pid
, 0, tid
);
2014 /* Encode 64 bits in 16 chars of hex. */
2016 static const char hexchars
[] = "0123456789abcdef";
2019 ishex (int ch
, int *val
)
2021 if ((ch
>= 'a') && (ch
<= 'f'))
2023 *val
= ch
- 'a' + 10;
2026 if ((ch
>= 'A') && (ch
<= 'F'))
2028 *val
= ch
- 'A' + 10;
2031 if ((ch
>= '0') && (ch
<= '9'))
2042 if (ch
>= 'a' && ch
<= 'f')
2043 return ch
- 'a' + 10;
2044 if (ch
>= '0' && ch
<= '9')
2046 if (ch
>= 'A' && ch
<= 'F')
2047 return ch
- 'A' + 10;
2052 stub_unpack_int (char *buff
, int fieldlength
)
2059 nibble
= stubhex (*buff
++);
2063 retval
= retval
<< 4;
2069 unpack_varlen_hex (char *buff
, /* packet to parse */
2073 ULONGEST retval
= 0;
2075 while (ishex (*buff
, &nibble
))
2078 retval
= retval
<< 4;
2079 retval
|= nibble
& 0x0f;
2086 unpack_nibble (char *buf
, int *val
)
2088 *val
= fromhex (*buf
++);
2093 pack_nibble (char *buf
, int nibble
)
2095 *buf
++ = hexchars
[(nibble
& 0x0f)];
2100 pack_hex_byte (char *pkt
, int byte
)
2102 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
2103 *pkt
++ = hexchars
[(byte
& 0xf)];
2108 unpack_byte (char *buf
, int *value
)
2110 *value
= stub_unpack_int (buf
, 2);
2115 pack_int (char *buf
, int value
)
2117 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
2118 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
2119 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
2120 buf
= pack_hex_byte (buf
, (value
& 0xff));
2125 unpack_int (char *buf
, int *value
)
2127 *value
= stub_unpack_int (buf
, 8);
2131 #if 0 /* Currently unused, uncomment when needed. */
2132 static char *pack_string (char *pkt
, char *string
);
2135 pack_string (char *pkt
, char *string
)
2140 len
= strlen (string
);
2142 len
= 200; /* Bigger than most GDB packets, junk??? */
2143 pkt
= pack_hex_byte (pkt
, len
);
2147 if ((ch
== '\0') || (ch
== '#'))
2148 ch
= '*'; /* Protect encapsulation. */
2153 #endif /* 0 (unused) */
2156 unpack_string (char *src
, char *dest
, int length
)
2165 pack_threadid (char *pkt
, threadref
*id
)
2168 unsigned char *altid
;
2170 altid
= (unsigned char *) id
;
2171 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
2173 pkt
= pack_hex_byte (pkt
, *altid
++);
2179 unpack_threadid (char *inbuf
, threadref
*id
)
2182 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
2185 altref
= (char *) id
;
2187 while (inbuf
< limit
)
2189 x
= stubhex (*inbuf
++);
2190 y
= stubhex (*inbuf
++);
2191 *altref
++ = (x
<< 4) | y
;
2196 /* Externally, threadrefs are 64 bits but internally, they are still
2197 ints. This is due to a mismatch of specifications. We would like
2198 to use 64bit thread references internally. This is an adapter
2202 int_to_threadref (threadref
*id
, int value
)
2204 unsigned char *scan
;
2206 scan
= (unsigned char *) id
;
2212 *scan
++ = (value
>> 24) & 0xff;
2213 *scan
++ = (value
>> 16) & 0xff;
2214 *scan
++ = (value
>> 8) & 0xff;
2215 *scan
++ = (value
& 0xff);
2219 threadref_to_int (threadref
*ref
)
2222 unsigned char *scan
;
2228 value
= (value
<< 8) | ((*scan
++) & 0xff);
2233 copy_threadref (threadref
*dest
, threadref
*src
)
2236 unsigned char *csrc
, *cdest
;
2238 csrc
= (unsigned char *) src
;
2239 cdest
= (unsigned char *) dest
;
2246 threadmatch (threadref
*dest
, threadref
*src
)
2248 /* Things are broken right now, so just assume we got a match. */
2250 unsigned char *srcp
, *destp
;
2252 srcp
= (char *) src
;
2253 destp
= (char *) dest
;
2257 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
2264 threadid:1, # always request threadid
2271 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2274 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
2276 *pkt
++ = 'q'; /* Info Query */
2277 *pkt
++ = 'P'; /* process or thread info */
2278 pkt
= pack_int (pkt
, mode
); /* mode */
2279 pkt
= pack_threadid (pkt
, id
); /* threadid */
2280 *pkt
= '\0'; /* terminate */
2284 /* These values tag the fields in a thread info response packet. */
2285 /* Tagging the fields allows us to request specific fields and to
2286 add more fields as time goes by. */
2288 #define TAG_THREADID 1 /* Echo the thread identifier. */
2289 #define TAG_EXISTS 2 /* Is this process defined enough to
2290 fetch registers and its stack? */
2291 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2292 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2293 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2297 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
2298 struct gdb_ext_thread_info
*info
)
2300 struct remote_state
*rs
= get_remote_state ();
2304 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
2307 /* info->threadid = 0; FIXME: implement zero_threadref. */
2309 info
->display
[0] = '\0';
2310 info
->shortname
[0] = '\0';
2311 info
->more_display
[0] = '\0';
2313 /* Assume the characters indicating the packet type have been
2315 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
2316 pkt
= unpack_threadid (pkt
, &ref
);
2319 warning (_("Incomplete response to threadinfo request."));
2320 if (!threadmatch (&ref
, expectedref
))
2321 { /* This is an answer to a different request. */
2322 warning (_("ERROR RMT Thread info mismatch."));
2325 copy_threadref (&info
->threadid
, &ref
);
2327 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2329 /* Packets are terminated with nulls. */
2330 while ((pkt
< limit
) && mask
&& *pkt
)
2332 pkt
= unpack_int (pkt
, &tag
); /* tag */
2333 pkt
= unpack_byte (pkt
, &length
); /* length */
2334 if (!(tag
& mask
)) /* Tags out of synch with mask. */
2336 warning (_("ERROR RMT: threadinfo tag mismatch."));
2340 if (tag
== TAG_THREADID
)
2344 warning (_("ERROR RMT: length of threadid is not 16."));
2348 pkt
= unpack_threadid (pkt
, &ref
);
2349 mask
= mask
& ~TAG_THREADID
;
2352 if (tag
== TAG_EXISTS
)
2354 info
->active
= stub_unpack_int (pkt
, length
);
2356 mask
= mask
& ~(TAG_EXISTS
);
2359 warning (_("ERROR RMT: 'exists' length too long."));
2365 if (tag
== TAG_THREADNAME
)
2367 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
2368 mask
= mask
& ~TAG_THREADNAME
;
2371 if (tag
== TAG_DISPLAY
)
2373 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
2374 mask
= mask
& ~TAG_DISPLAY
;
2377 if (tag
== TAG_MOREDISPLAY
)
2379 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
2380 mask
= mask
& ~TAG_MOREDISPLAY
;
2383 warning (_("ERROR RMT: unknown thread info tag."));
2384 break; /* Not a tag we know about. */
2390 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
2391 struct gdb_ext_thread_info
*info
)
2393 struct remote_state
*rs
= get_remote_state ();
2396 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
2398 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2400 if (rs
->buf
[0] == '\0')
2403 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
2408 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2411 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
2412 threadref
*nextthread
)
2414 *pkt
++ = 'q'; /* info query packet */
2415 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
2416 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
2417 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
2418 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
2423 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2426 parse_threadlist_response (char *pkt
, int result_limit
,
2427 threadref
*original_echo
, threadref
*resultlist
,
2430 struct remote_state
*rs
= get_remote_state ();
2432 int count
, resultcount
, done
;
2435 /* Assume the 'q' and 'M chars have been stripped. */
2436 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
2437 /* done parse past here */
2438 pkt
= unpack_byte (pkt
, &count
); /* count field */
2439 pkt
= unpack_nibble (pkt
, &done
);
2440 /* The first threadid is the argument threadid. */
2441 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
2442 while ((count
-- > 0) && (pkt
< limit
))
2444 pkt
= unpack_threadid (pkt
, resultlist
++);
2445 if (resultcount
++ >= result_limit
)
2454 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
2455 int *done
, int *result_count
, threadref
*threadlist
)
2457 struct remote_state
*rs
= get_remote_state ();
2458 static threadref echo_nextthread
;
2461 /* Trancate result limit to be smaller than the packet size. */
2462 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
2463 >= get_remote_packet_size ())
2464 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
2466 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
2468 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2470 if (*rs
->buf
== '\0')
2474 parse_threadlist_response (rs
->buf
+ 2, result_limit
, &echo_nextthread
,
2477 if (!threadmatch (&echo_nextthread
, nextthread
))
2479 /* FIXME: This is a good reason to drop the packet. */
2480 /* Possably, there is a duplicate response. */
2482 retransmit immediatly - race conditions
2483 retransmit after timeout - yes
2485 wait for packet, then exit
2487 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2488 return 0; /* I choose simply exiting. */
2490 if (*result_count
<= 0)
2494 warning (_("RMT ERROR : failed to get remote thread list."));
2497 return result
; /* break; */
2499 if (*result_count
> result_limit
)
2502 warning (_("RMT ERROR: threadlist response longer than requested."));
2508 /* This is the interface between remote and threads, remotes upper
2511 /* remote_find_new_threads retrieves the thread list and for each
2512 thread in the list, looks up the thread in GDB's internal list,
2513 adding the thread if it does not already exist. This involves
2514 getting partial thread lists from the remote target so, polling the
2515 quit_flag is required. */
2518 /* About this many threadisds fit in a packet. */
2520 #define MAXTHREADLISTRESULTS 32
2523 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2526 int done
, i
, result_count
;
2530 static threadref nextthread
;
2531 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
2536 if (loopcount
++ > looplimit
)
2539 warning (_("Remote fetch threadlist -infinite loop-."));
2542 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
2543 &done
, &result_count
, resultthreadlist
))
2548 /* Clear for later iterations. */
2550 /* Setup to resume next batch of thread references, set nextthread. */
2551 if (result_count
>= 1)
2552 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
2554 while (result_count
--)
2555 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
2562 remote_newthread_step (threadref
*ref
, void *context
)
2564 int pid
= ptid_get_pid (inferior_ptid
);
2565 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2567 if (!in_thread_list (ptid
))
2569 return 1; /* continue iterator */
2572 #define CRAZY_MAX_THREADS 1000
2575 remote_current_thread (ptid_t oldpid
)
2577 struct remote_state
*rs
= get_remote_state ();
2580 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2581 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2582 return read_ptid (&rs
->buf
[2], NULL
);
2587 /* Find new threads for info threads command.
2588 * Original version, using John Metzler's thread protocol.
2592 remote_find_new_threads (void)
2594 remote_threadlist_iterator (remote_newthread_step
, 0,
2598 #if defined(HAVE_LIBEXPAT)
2600 typedef struct thread_item
2606 DEF_VEC_O(thread_item_t
);
2608 struct threads_parsing_context
2610 VEC (thread_item_t
) *items
;
2614 start_thread (struct gdb_xml_parser
*parser
,
2615 const struct gdb_xml_element
*element
,
2616 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
2618 struct threads_parsing_context
*data
= user_data
;
2620 struct thread_item item
;
2622 struct gdb_xml_value
*attr
;
2624 id
= xml_find_attribute (attributes
, "id")->value
;
2625 item
.ptid
= read_ptid (id
, NULL
);
2627 attr
= xml_find_attribute (attributes
, "core");
2629 item
.core
= *(ULONGEST
*) attr
->value
;
2635 VEC_safe_push (thread_item_t
, data
->items
, &item
);
2639 end_thread (struct gdb_xml_parser
*parser
,
2640 const struct gdb_xml_element
*element
,
2641 void *user_data
, const char *body_text
)
2643 struct threads_parsing_context
*data
= user_data
;
2645 if (body_text
&& *body_text
)
2646 VEC_last (thread_item_t
, data
->items
)->extra
= xstrdup (body_text
);
2649 const struct gdb_xml_attribute thread_attributes
[] = {
2650 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
2651 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
2652 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
2655 const struct gdb_xml_element thread_children
[] = {
2656 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2659 const struct gdb_xml_element threads_children
[] = {
2660 { "thread", thread_attributes
, thread_children
,
2661 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
2662 start_thread
, end_thread
},
2663 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2666 const struct gdb_xml_element threads_elements
[] = {
2667 { "threads", NULL
, threads_children
,
2668 GDB_XML_EF_NONE
, NULL
, NULL
},
2669 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2672 /* Discard the contents of the constructed thread info context. */
2675 clear_threads_parsing_context (void *p
)
2677 struct threads_parsing_context
*context
= p
;
2679 struct thread_item
*item
;
2681 for (i
= 0; VEC_iterate (thread_item_t
, context
->items
, i
, item
); ++i
)
2682 xfree (item
->extra
);
2684 VEC_free (thread_item_t
, context
->items
);
2690 * Find all threads for info threads command.
2691 * Uses new thread protocol contributed by Cisco.
2692 * Falls back and attempts to use the older method (above)
2693 * if the target doesn't respond to the new method.
2697 remote_threads_info (struct target_ops
*ops
)
2699 struct remote_state
*rs
= get_remote_state ();
2703 if (remote_desc
== 0) /* paranoia */
2704 error (_("Command can only be used when connected to the remote target."));
2706 #if defined(HAVE_LIBEXPAT)
2707 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2709 char *xml
= target_read_stralloc (¤t_target
,
2710 TARGET_OBJECT_THREADS
, NULL
);
2712 struct cleanup
*back_to
= make_cleanup (xfree
, xml
);
2716 struct threads_parsing_context context
;
2718 context
.items
= NULL
;
2719 make_cleanup (clear_threads_parsing_context
, &context
);
2721 if (gdb_xml_parse_quick (_("threads"), "threads.dtd",
2722 threads_elements
, xml
, &context
) == 0)
2725 struct thread_item
*item
;
2728 VEC_iterate (thread_item_t
, context
.items
, i
, item
);
2731 if (!ptid_equal (item
->ptid
, null_ptid
))
2733 struct private_thread_info
*info
;
2734 /* In non-stop mode, we assume new found threads
2735 are running until proven otherwise with a
2736 stop reply. In all-stop, we can only get
2737 here if all threads are stopped. */
2738 int running
= non_stop
? 1 : 0;
2740 remote_notice_new_inferior (item
->ptid
, running
);
2742 info
= demand_private_info (item
->ptid
);
2743 info
->core
= item
->core
;
2744 info
->extra
= item
->extra
;
2751 do_cleanups (back_to
);
2756 if (use_threadinfo_query
)
2758 putpkt ("qfThreadInfo");
2759 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2761 if (bufp
[0] != '\0') /* q packet recognized */
2763 while (*bufp
++ == 'm') /* reply contains one or more TID */
2767 new_thread
= read_ptid (bufp
, &bufp
);
2768 if (!ptid_equal (new_thread
, null_ptid
))
2770 /* In non-stop mode, we assume new found threads
2771 are running until proven otherwise with a
2772 stop reply. In all-stop, we can only get
2773 here if all threads are stopped. */
2774 int running
= non_stop
? 1 : 0;
2776 remote_notice_new_inferior (new_thread
, running
);
2779 while (*bufp
++ == ','); /* comma-separated list */
2780 putpkt ("qsThreadInfo");
2781 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2788 /* Only qfThreadInfo is supported in non-stop mode. */
2792 /* Else fall back to old method based on jmetzler protocol. */
2793 use_threadinfo_query
= 0;
2794 remote_find_new_threads ();
2799 * Collect a descriptive string about the given thread.
2800 * The target may say anything it wants to about the thread
2801 * (typically info about its blocked / runnable state, name, etc.).
2802 * This string will appear in the info threads display.
2804 * Optional: targets are not required to implement this function.
2808 remote_threads_extra_info (struct thread_info
*tp
)
2810 struct remote_state
*rs
= get_remote_state ();
2814 struct gdb_ext_thread_info threadinfo
;
2815 static char display_buf
[100]; /* arbitrary... */
2816 int n
= 0; /* position in display_buf */
2818 if (remote_desc
== 0) /* paranoia */
2819 internal_error (__FILE__
, __LINE__
,
2820 _("remote_threads_extra_info"));
2822 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2823 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2824 /* This is the main thread which was added by GDB. The remote
2825 server doesn't know about it. */
2828 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2830 struct thread_info
*info
= find_thread_ptid (tp
->ptid
);
2832 if (info
&& info
->private)
2833 return info
->private->extra
;
2838 if (use_threadextra_query
)
2841 char *endb
= rs
->buf
+ get_remote_packet_size ();
2843 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2845 write_ptid (b
, endb
, tp
->ptid
);
2848 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2849 if (rs
->buf
[0] != 0)
2851 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2852 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2853 display_buf
[result
] = '\0';
2858 /* If the above query fails, fall back to the old method. */
2859 use_threadextra_query
= 0;
2860 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2861 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2862 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2863 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2864 if (threadinfo
.active
)
2866 if (*threadinfo
.shortname
)
2867 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2868 " Name: %s,", threadinfo
.shortname
);
2869 if (*threadinfo
.display
)
2870 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2871 " State: %s,", threadinfo
.display
);
2872 if (*threadinfo
.more_display
)
2873 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2874 " Priority: %s", threadinfo
.more_display
);
2878 /* For purely cosmetic reasons, clear up trailing commas. */
2879 if (',' == display_buf
[n
-1])
2880 display_buf
[n
-1] = ' ';
2889 remote_static_tracepoint_marker_at (CORE_ADDR addr
,
2890 struct static_tracepoint_marker
*marker
)
2892 struct remote_state
*rs
= get_remote_state ();
2895 sprintf (p
, "qTSTMat:");
2897 p
+= hexnumstr (p
, addr
);
2899 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2903 error (_("Remote failure reply: %s"), p
);
2907 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2914 static VEC(static_tracepoint_marker_p
) *
2915 remote_static_tracepoint_markers_by_strid (const char *strid
)
2917 struct remote_state
*rs
= get_remote_state ();
2918 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
2919 struct static_tracepoint_marker
*marker
= NULL
;
2920 struct cleanup
*old_chain
;
2923 /* Ask for a first packet of static tracepoint marker
2926 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2929 error (_("Remote failure reply: %s"), p
);
2931 old_chain
= make_cleanup (free_current_marker
, &marker
);
2936 marker
= XCNEW (struct static_tracepoint_marker
);
2940 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2942 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
2944 VEC_safe_push (static_tracepoint_marker_p
,
2950 release_static_tracepoint_marker (marker
);
2951 memset (marker
, 0, sizeof (*marker
));
2954 while (*p
++ == ','); /* comma-separated list */
2955 /* Ask for another packet of static tracepoint definition. */
2957 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2961 do_cleanups (old_chain
);
2966 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2969 remote_get_ada_task_ptid (long lwp
, long thread
)
2971 return ptid_build (ptid_get_pid (inferior_ptid
), 0, lwp
);
2975 /* Restart the remote side; this is an extended protocol operation. */
2978 extended_remote_restart (void)
2980 struct remote_state
*rs
= get_remote_state ();
2982 /* Send the restart command; for reasons I don't understand the
2983 remote side really expects a number after the "R". */
2984 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2987 remote_fileio_reset ();
2990 /* Clean up connection to a remote debugger. */
2993 remote_close (int quitting
)
2995 if (remote_desc
== NULL
)
2996 return; /* already closed */
2998 /* Make sure we leave stdin registered in the event loop, and we
2999 don't leave the async SIGINT signal handler installed. */
3000 remote_terminal_ours ();
3002 serial_close (remote_desc
);
3005 /* We don't have a connection to the remote stub anymore. Get rid
3006 of all the inferiors and their threads we were controlling.
3007 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3008 will be unable to find the thread corresponding to (pid, 0, 0). */
3009 inferior_ptid
= null_ptid
;
3010 discard_all_inferiors ();
3012 /* We're no longer interested in any of these events. */
3013 discard_pending_stop_replies (-1);
3015 if (remote_async_inferior_event_token
)
3016 delete_async_event_handler (&remote_async_inferior_event_token
);
3017 if (remote_async_get_pending_events_token
)
3018 delete_async_event_handler (&remote_async_get_pending_events_token
);
3021 /* Query the remote side for the text, data and bss offsets. */
3026 struct remote_state
*rs
= get_remote_state ();
3029 int lose
, num_segments
= 0, do_sections
, do_segments
;
3030 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
3031 struct section_offsets
*offs
;
3032 struct symfile_segment_data
*data
;
3034 if (symfile_objfile
== NULL
)
3037 putpkt ("qOffsets");
3038 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3041 if (buf
[0] == '\000')
3042 return; /* Return silently. Stub doesn't support
3046 warning (_("Remote failure reply: %s"), buf
);
3050 /* Pick up each field in turn. This used to be done with scanf, but
3051 scanf will make trouble if CORE_ADDR size doesn't match
3052 conversion directives correctly. The following code will work
3053 with any size of CORE_ADDR. */
3054 text_addr
= data_addr
= bss_addr
= 0;
3058 if (strncmp (ptr
, "Text=", 5) == 0)
3061 /* Don't use strtol, could lose on big values. */
3062 while (*ptr
&& *ptr
!= ';')
3063 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
3065 if (strncmp (ptr
, ";Data=", 6) == 0)
3068 while (*ptr
&& *ptr
!= ';')
3069 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
3074 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
3077 while (*ptr
&& *ptr
!= ';')
3078 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
3080 if (bss_addr
!= data_addr
)
3081 warning (_("Target reported unsupported offsets: %s"), buf
);
3086 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
3089 /* Don't use strtol, could lose on big values. */
3090 while (*ptr
&& *ptr
!= ';')
3091 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
3094 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
3097 while (*ptr
&& *ptr
!= ';')
3098 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
3106 error (_("Malformed response to offset query, %s"), buf
);
3107 else if (*ptr
!= '\0')
3108 warning (_("Target reported unsupported offsets: %s"), buf
);
3110 offs
= ((struct section_offsets
*)
3111 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
3112 memcpy (offs
, symfile_objfile
->section_offsets
,
3113 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
3115 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
3116 do_segments
= (data
!= NULL
);
3117 do_sections
= num_segments
== 0;
3119 if (num_segments
> 0)
3121 segments
[0] = text_addr
;
3122 segments
[1] = data_addr
;
3124 /* If we have two segments, we can still try to relocate everything
3125 by assuming that the .text and .data offsets apply to the whole
3126 text and data segments. Convert the offsets given in the packet
3127 to base addresses for symfile_map_offsets_to_segments. */
3128 else if (data
&& data
->num_segments
== 2)
3130 segments
[0] = data
->segment_bases
[0] + text_addr
;
3131 segments
[1] = data
->segment_bases
[1] + data_addr
;
3134 /* If the object file has only one segment, assume that it is text
3135 rather than data; main programs with no writable data are rare,
3136 but programs with no code are useless. Of course the code might
3137 have ended up in the data segment... to detect that we would need
3138 the permissions here. */
3139 else if (data
&& data
->num_segments
== 1)
3141 segments
[0] = data
->segment_bases
[0] + text_addr
;
3144 /* There's no way to relocate by segment. */
3150 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
3151 offs
, num_segments
, segments
);
3153 if (ret
== 0 && !do_sections
)
3154 error (_("Can not handle qOffsets TextSeg "
3155 "response with this symbol file"));
3162 free_symfile_segment_data (data
);
3166 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
3168 /* This is a temporary kludge to force data and bss to use the
3169 same offsets because that's what nlmconv does now. The real
3170 solution requires changes to the stub and remote.c that I
3171 don't have time to do right now. */
3173 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
3174 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
3177 objfile_relocate (symfile_objfile
, offs
);
3180 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3181 threads we know are stopped already. This is used during the
3182 initial remote connection in non-stop mode --- threads that are
3183 reported as already being stopped are left stopped. */
3186 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
3188 /* If we have a stop reply for this thread, it must be stopped. */
3189 if (peek_stop_reply (thread
->ptid
))
3190 set_stop_requested (thread
->ptid
, 1);
3195 /* Send interrupt_sequence to remote target. */
3197 send_interrupt_sequence (void)
3199 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
3200 serial_write (remote_desc
, "\x03", 1);
3201 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
3202 serial_send_break (remote_desc
);
3203 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
3205 serial_send_break (remote_desc
);
3206 serial_write (remote_desc
, "g", 1);
3209 internal_error (__FILE__
, __LINE__
,
3210 _("Invalid value for interrupt_sequence_mode: %s."),
3211 interrupt_sequence_mode
);
3214 /* Query the remote target for which is the current thread/process,
3215 add it to our tables, and update INFERIOR_PTID. The caller is
3216 responsible for setting the state such that the remote end is ready
3217 to return the current thread. */
3220 add_current_inferior_and_thread (void)
3222 struct remote_state
*rs
= get_remote_state ();
3226 inferior_ptid
= null_ptid
;
3228 /* Now, if we have thread information, update inferior_ptid. */
3229 ptid
= remote_current_thread (inferior_ptid
);
3230 if (!ptid_equal (ptid
, null_ptid
))
3232 if (!remote_multi_process_p (rs
))
3235 inferior_ptid
= ptid
;
3239 /* Without this, some commands which require an active target
3240 (such as kill) won't work. This variable serves (at least)
3241 double duty as both the pid of the target process (if it has
3242 such), and as a flag indicating that a target is active. */
3243 inferior_ptid
= magic_null_ptid
;
3247 remote_add_inferior (fake_pid_p
, ptid_get_pid (inferior_ptid
), -1);
3249 /* Add the main thread. */
3250 add_thread_silent (inferior_ptid
);
3254 remote_start_remote (int from_tty
, struct target_ops
*target
, int extended_p
)
3256 struct remote_state
*rs
= get_remote_state ();
3257 struct packet_config
*noack_config
;
3258 char *wait_status
= NULL
;
3260 immediate_quit
++; /* Allow user to interrupt it. */
3262 if (interrupt_on_connect
)
3263 send_interrupt_sequence ();
3265 /* Ack any packet which the remote side has already sent. */
3266 serial_write (remote_desc
, "+", 1);
3268 /* The first packet we send to the target is the optional "supported
3269 packets" request. If the target can answer this, it will tell us
3270 which later probes to skip. */
3271 remote_query_supported ();
3273 /* If the stub wants to get a QAllow, compose one and send it. */
3274 if (remote_protocol_packets
[PACKET_QAllow
].support
!= PACKET_DISABLE
)
3275 remote_set_permissions ();
3277 /* Next, we possibly activate noack mode.
3279 If the QStartNoAckMode packet configuration is set to AUTO,
3280 enable noack mode if the stub reported a wish for it with
3283 If set to TRUE, then enable noack mode even if the stub didn't
3284 report it in qSupported. If the stub doesn't reply OK, the
3285 session ends with an error.
3287 If FALSE, then don't activate noack mode, regardless of what the
3288 stub claimed should be the default with qSupported. */
3290 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
3292 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
3293 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
3294 && noack_config
->support
== PACKET_ENABLE
))
3296 putpkt ("QStartNoAckMode");
3297 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3298 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
3304 /* Tell the remote that we are using the extended protocol. */
3306 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3309 /* Let the target know which signals it is allowed to pass down to
3311 update_signals_program_target ();
3313 /* Next, if the target can specify a description, read it. We do
3314 this before anything involving memory or registers. */
3315 target_find_description ();
3317 /* Next, now that we know something about the target, update the
3318 address spaces in the program spaces. */
3319 update_address_spaces ();
3321 /* On OSs where the list of libraries is global to all
3322 processes, we fetch them early. */
3323 if (gdbarch_has_global_solist (target_gdbarch
))
3324 solib_add (NULL
, from_tty
, target
, auto_solib_add
);
3328 if (!rs
->non_stop_aware
)
3329 error (_("Non-stop mode requested, but remote "
3330 "does not support non-stop"));
3332 putpkt ("QNonStop:1");
3333 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3335 if (strcmp (rs
->buf
, "OK") != 0)
3336 error (_("Remote refused setting non-stop mode with: %s"), rs
->buf
);
3338 /* Find about threads and processes the stub is already
3339 controlling. We default to adding them in the running state.
3340 The '?' query below will then tell us about which threads are
3342 remote_threads_info (target
);
3344 else if (rs
->non_stop_aware
)
3346 /* Don't assume that the stub can operate in all-stop mode.
3347 Request it explicitely. */
3348 putpkt ("QNonStop:0");
3349 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3351 if (strcmp (rs
->buf
, "OK") != 0)
3352 error (_("Remote refused setting all-stop mode with: %s"), rs
->buf
);
3355 /* Check whether the target is running now. */
3357 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3363 struct inferior
*inf
;
3365 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
3368 error (_("The target is not running (try extended-remote?)"));
3370 /* We're connected, but not running. Drop out before we
3371 call start_remote. */
3376 /* Save the reply for later. */
3377 wait_status
= alloca (strlen (rs
->buf
) + 1);
3378 strcpy (wait_status
, rs
->buf
);
3381 /* Let the stub know that we want it to return the thread. */
3382 set_continue_thread (minus_one_ptid
);
3384 add_current_inferior_and_thread ();
3386 /* init_wait_for_inferior should be called before get_offsets in order
3387 to manage `inserted' flag in bp loc in a correct state.
3388 breakpoint_init_inferior, called from init_wait_for_inferior, set
3389 `inserted' flag to 0, while before breakpoint_re_set, called from
3390 start_remote, set `inserted' flag to 1. In the initialization of
3391 inferior, breakpoint_init_inferior should be called first, and then
3392 breakpoint_re_set can be called. If this order is broken, state of
3393 `inserted' flag is wrong, and cause some problems on breakpoint
3395 init_wait_for_inferior ();
3397 get_offsets (); /* Get text, data & bss offsets. */
3399 /* If we could not find a description using qXfer, and we know
3400 how to do it some other way, try again. This is not
3401 supported for non-stop; it could be, but it is tricky if
3402 there are no stopped threads when we connect. */
3403 if (remote_read_description_p (target
)
3404 && gdbarch_target_desc (target_gdbarch
) == NULL
)
3406 target_clear_description ();
3407 target_find_description ();
3410 /* Use the previously fetched status. */
3411 gdb_assert (wait_status
!= NULL
);
3412 strcpy (rs
->buf
, wait_status
);
3413 rs
->cached_wait_status
= 1;
3416 start_remote (from_tty
); /* Initialize gdb process mechanisms. */
3420 /* Clear WFI global state. Do this before finding about new
3421 threads and inferiors, and setting the current inferior.
3422 Otherwise we would clear the proceed status of the current
3423 inferior when we want its stop_soon state to be preserved
3424 (see notice_new_inferior). */
3425 init_wait_for_inferior ();
3427 /* In non-stop, we will either get an "OK", meaning that there
3428 are no stopped threads at this time; or, a regular stop
3429 reply. In the latter case, there may be more than one thread
3430 stopped --- we pull them all out using the vStopped
3432 if (strcmp (rs
->buf
, "OK") != 0)
3434 struct stop_reply
*stop_reply
;
3435 struct cleanup
*old_chain
;
3437 stop_reply
= stop_reply_xmalloc ();
3438 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
3440 remote_parse_stop_reply (rs
->buf
, stop_reply
);
3441 discard_cleanups (old_chain
);
3443 /* get_pending_stop_replies acks this one, and gets the rest
3445 pending_stop_reply
= stop_reply
;
3446 remote_get_pending_stop_replies ();
3448 /* Make sure that threads that were stopped remain
3450 iterate_over_threads (set_stop_requested_callback
, NULL
);
3453 if (target_can_async_p ())
3454 target_async (inferior_event_handler
, 0);
3456 if (thread_count () == 0)
3459 error (_("The target is not running (try extended-remote?)"));
3461 /* We're connected, but not running. Drop out before we
3462 call start_remote. */
3466 /* Let the stub know that we want it to return the thread. */
3468 /* Force the stub to choose a thread. */
3469 set_general_thread (null_ptid
);
3472 inferior_ptid
= remote_current_thread (minus_one_ptid
);
3473 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
3474 error (_("remote didn't report the current thread in non-stop mode"));
3476 get_offsets (); /* Get text, data & bss offsets. */
3478 /* In non-stop mode, any cached wait status will be stored in
3479 the stop reply queue. */
3480 gdb_assert (wait_status
== NULL
);
3482 /* Report all signals during attach/startup. */
3483 remote_pass_signals (0, NULL
);
3486 /* If we connected to a live target, do some additional setup. */
3487 if (target_has_execution
)
3489 if (exec_bfd
) /* No use without an exec file. */
3490 remote_check_symbols (symfile_objfile
);
3493 /* Possibly the target has been engaged in a trace run started
3494 previously; find out where things are at. */
3495 if (remote_get_trace_status (current_trace_status ()) != -1)
3497 struct uploaded_tp
*uploaded_tps
= NULL
;
3498 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
3500 if (current_trace_status ()->running
)
3501 printf_filtered (_("Trace is already running on the target.\n"));
3503 /* Get trace state variables first, they may be checked when
3504 parsing uploaded commands. */
3506 remote_upload_trace_state_variables (&uploaded_tsvs
);
3508 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
3510 remote_upload_tracepoints (&uploaded_tps
);
3512 merge_uploaded_tracepoints (&uploaded_tps
);
3515 /* If breakpoints are global, insert them now. */
3516 if (gdbarch_has_global_breakpoints (target_gdbarch
)
3517 && breakpoints_always_inserted_mode ())
3518 insert_breakpoints ();
3521 /* Open a connection to a remote debugger.
3522 NAME is the filename used for communication. */
3525 remote_open (char *name
, int from_tty
)
3527 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
3530 /* Open a connection to a remote debugger using the extended
3531 remote gdb protocol. NAME is the filename used for communication. */
3534 extended_remote_open (char *name
, int from_tty
)
3536 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
3539 /* Generic code for opening a connection to a remote target. */
3542 init_all_packet_configs (void)
3546 for (i
= 0; i
< PACKET_MAX
; i
++)
3547 update_packet_config (&remote_protocol_packets
[i
]);
3550 /* Symbol look-up. */
3553 remote_check_symbols (struct objfile
*objfile
)
3555 struct remote_state
*rs
= get_remote_state ();
3556 char *msg
, *reply
, *tmp
;
3557 struct minimal_symbol
*sym
;
3560 /* The remote side has no concept of inferiors that aren't running
3561 yet, it only knows about running processes. If we're connected
3562 but our current inferior is not running, we should not invite the
3563 remote target to request symbol lookups related to its
3564 (unrelated) current process. */
3565 if (!target_has_execution
)
3568 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
3571 /* Make sure the remote is pointing at the right process. Note
3572 there's no way to select "no process". */
3573 set_general_process ();
3575 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3576 because we need both at the same time. */
3577 msg
= alloca (get_remote_packet_size ());
3579 /* Invite target to request symbol lookups. */
3581 putpkt ("qSymbol::");
3582 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3583 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
3586 while (strncmp (reply
, "qSymbol:", 8) == 0)
3589 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
3591 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
3593 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
3596 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
3597 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
3599 /* If this is a function address, return the start of code
3600 instead of any data function descriptor. */
3601 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
3605 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
3606 phex_nz (sym_addr
, addr_size
), &reply
[8]);
3610 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3615 static struct serial
*
3616 remote_serial_open (char *name
)
3618 static int udp_warning
= 0;
3620 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3621 of in ser-tcp.c, because it is the remote protocol assuming that the
3622 serial connection is reliable and not the serial connection promising
3624 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
3626 warning (_("The remote protocol may be unreliable over UDP.\n"
3627 "Some events may be lost, rendering further debugging "
3632 return serial_open (name
);
3635 /* Inform the target of our permission settings. The permission flags
3636 work without this, but if the target knows the settings, it can do
3637 a couple things. First, it can add its own check, to catch cases
3638 that somehow manage to get by the permissions checks in target
3639 methods. Second, if the target is wired to disallow particular
3640 settings (for instance, a system in the field that is not set up to
3641 be able to stop at a breakpoint), it can object to any unavailable
3645 remote_set_permissions (void)
3647 struct remote_state
*rs
= get_remote_state ();
3649 sprintf (rs
->buf
, "QAllow:"
3650 "WriteReg:%x;WriteMem:%x;"
3651 "InsertBreak:%x;InsertTrace:%x;"
3652 "InsertFastTrace:%x;Stop:%x",
3653 may_write_registers
, may_write_memory
,
3654 may_insert_breakpoints
, may_insert_tracepoints
,
3655 may_insert_fast_tracepoints
, may_stop
);
3657 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3659 /* If the target didn't like the packet, warn the user. Do not try
3660 to undo the user's settings, that would just be maddening. */
3661 if (strcmp (rs
->buf
, "OK") != 0)
3662 warning (_("Remote refused setting permissions with: %s"), rs
->buf
);
3665 /* This type describes each known response to the qSupported
3667 struct protocol_feature
3669 /* The name of this protocol feature. */
3672 /* The default for this protocol feature. */
3673 enum packet_support default_support
;
3675 /* The function to call when this feature is reported, or after
3676 qSupported processing if the feature is not supported.
3677 The first argument points to this structure. The second
3678 argument indicates whether the packet requested support be
3679 enabled, disabled, or probed (or the default, if this function
3680 is being called at the end of processing and this feature was
3681 not reported). The third argument may be NULL; if not NULL, it
3682 is a NUL-terminated string taken from the packet following
3683 this feature's name and an equals sign. */
3684 void (*func
) (const struct protocol_feature
*, enum packet_support
,
3687 /* The corresponding packet for this feature. Only used if
3688 FUNC is remote_supported_packet. */
3693 remote_supported_packet (const struct protocol_feature
*feature
,
3694 enum packet_support support
,
3695 const char *argument
)
3699 warning (_("Remote qSupported response supplied an unexpected value for"
3700 " \"%s\"."), feature
->name
);
3704 if (remote_protocol_packets
[feature
->packet
].support
3705 == PACKET_SUPPORT_UNKNOWN
)
3706 remote_protocol_packets
[feature
->packet
].support
= support
;
3710 remote_packet_size (const struct protocol_feature
*feature
,
3711 enum packet_support support
, const char *value
)
3713 struct remote_state
*rs
= get_remote_state ();
3718 if (support
!= PACKET_ENABLE
)
3721 if (value
== NULL
|| *value
== '\0')
3723 warning (_("Remote target reported \"%s\" without a size."),
3729 packet_size
= strtol (value
, &value_end
, 16);
3730 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
3732 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3733 feature
->name
, value
);
3737 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
3739 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3740 packet_size
, MAX_REMOTE_PACKET_SIZE
);
3741 packet_size
= MAX_REMOTE_PACKET_SIZE
;
3744 /* Record the new maximum packet size. */
3745 rs
->explicit_packet_size
= packet_size
;
3749 remote_multi_process_feature (const struct protocol_feature
*feature
,
3750 enum packet_support support
, const char *value
)
3752 struct remote_state
*rs
= get_remote_state ();
3754 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
3758 remote_non_stop_feature (const struct protocol_feature
*feature
,
3759 enum packet_support support
, const char *value
)
3761 struct remote_state
*rs
= get_remote_state ();
3763 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
3767 remote_cond_tracepoint_feature (const struct protocol_feature
*feature
,
3768 enum packet_support support
,
3771 struct remote_state
*rs
= get_remote_state ();
3773 rs
->cond_tracepoints
= (support
== PACKET_ENABLE
);
3777 remote_cond_breakpoint_feature (const struct protocol_feature
*feature
,
3778 enum packet_support support
,
3781 struct remote_state
*rs
= get_remote_state ();
3783 rs
->cond_breakpoints
= (support
== PACKET_ENABLE
);
3787 remote_fast_tracepoint_feature (const struct protocol_feature
*feature
,
3788 enum packet_support support
,
3791 struct remote_state
*rs
= get_remote_state ();
3793 rs
->fast_tracepoints
= (support
== PACKET_ENABLE
);
3797 remote_static_tracepoint_feature (const struct protocol_feature
*feature
,
3798 enum packet_support support
,
3801 struct remote_state
*rs
= get_remote_state ();
3803 rs
->static_tracepoints
= (support
== PACKET_ENABLE
);
3807 remote_install_in_trace_feature (const struct protocol_feature
*feature
,
3808 enum packet_support support
,
3811 struct remote_state
*rs
= get_remote_state ();
3813 rs
->install_in_trace
= (support
== PACKET_ENABLE
);
3817 remote_disconnected_tracing_feature (const struct protocol_feature
*feature
,
3818 enum packet_support support
,
3821 struct remote_state
*rs
= get_remote_state ();
3823 rs
->disconnected_tracing
= (support
== PACKET_ENABLE
);
3827 remote_enable_disable_tracepoint_feature (const struct protocol_feature
*feature
,
3828 enum packet_support support
,
3831 struct remote_state
*rs
= get_remote_state ();
3833 rs
->enable_disable_tracepoints
= (support
== PACKET_ENABLE
);
3837 remote_string_tracing_feature (const struct protocol_feature
*feature
,
3838 enum packet_support support
,
3841 struct remote_state
*rs
= get_remote_state ();
3843 rs
->string_tracing
= (support
== PACKET_ENABLE
);
3846 static struct protocol_feature remote_protocol_features
[] = {
3847 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
3848 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
3849 PACKET_qXfer_auxv
},
3850 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
3851 PACKET_qXfer_features
},
3852 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
3853 PACKET_qXfer_libraries
},
3854 { "qXfer:libraries-svr4:read", PACKET_DISABLE
, remote_supported_packet
,
3855 PACKET_qXfer_libraries_svr4
},
3856 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
3857 PACKET_qXfer_memory_map
},
3858 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
3859 PACKET_qXfer_spu_read
},
3860 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
3861 PACKET_qXfer_spu_write
},
3862 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
3863 PACKET_qXfer_osdata
},
3864 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
3865 PACKET_qXfer_threads
},
3866 { "qXfer:traceframe-info:read", PACKET_DISABLE
, remote_supported_packet
,
3867 PACKET_qXfer_traceframe_info
},
3868 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
3869 PACKET_QPassSignals
},
3870 { "QProgramSignals", PACKET_DISABLE
, remote_supported_packet
,
3871 PACKET_QProgramSignals
},
3872 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
3873 PACKET_QStartNoAckMode
},
3874 { "multiprocess", PACKET_DISABLE
, remote_multi_process_feature
, -1 },
3875 { "QNonStop", PACKET_DISABLE
, remote_non_stop_feature
, -1 },
3876 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
3877 PACKET_qXfer_siginfo_read
},
3878 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
3879 PACKET_qXfer_siginfo_write
},
3880 { "ConditionalTracepoints", PACKET_DISABLE
, remote_cond_tracepoint_feature
,
3881 PACKET_ConditionalTracepoints
},
3882 { "ConditionalBreakpoints", PACKET_DISABLE
, remote_cond_breakpoint_feature
,
3883 PACKET_ConditionalBreakpoints
},
3884 { "FastTracepoints", PACKET_DISABLE
, remote_fast_tracepoint_feature
,
3885 PACKET_FastTracepoints
},
3886 { "StaticTracepoints", PACKET_DISABLE
, remote_static_tracepoint_feature
,
3887 PACKET_StaticTracepoints
},
3888 {"InstallInTrace", PACKET_DISABLE
, remote_install_in_trace_feature
,
3889 PACKET_InstallInTrace
},
3890 { "DisconnectedTracing", PACKET_DISABLE
, remote_disconnected_tracing_feature
,
3892 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
3894 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
3896 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
3897 PACKET_TracepointSource
},
3898 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
3900 { "EnableDisableTracepoints", PACKET_DISABLE
,
3901 remote_enable_disable_tracepoint_feature
, -1 },
3902 { "qXfer:fdpic:read", PACKET_DISABLE
, remote_supported_packet
,
3903 PACKET_qXfer_fdpic
},
3904 { "qXfer:uib:read", PACKET_DISABLE
, remote_supported_packet
,
3906 { "QDisableRandomization", PACKET_DISABLE
, remote_supported_packet
,
3907 PACKET_QDisableRandomization
},
3908 { "QAgent", PACKET_DISABLE
, remote_supported_packet
, PACKET_QAgent
},
3909 { "tracenz", PACKET_DISABLE
,
3910 remote_string_tracing_feature
, -1 },
3913 static char *remote_support_xml
;
3915 /* Register string appended to "xmlRegisters=" in qSupported query. */
3918 register_remote_support_xml (const char *xml
)
3920 #if defined(HAVE_LIBEXPAT)
3921 if (remote_support_xml
== NULL
)
3922 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
3925 char *copy
= xstrdup (remote_support_xml
+ 13);
3926 char *p
= strtok (copy
, ",");
3930 if (strcmp (p
, xml
) == 0)
3937 while ((p
= strtok (NULL
, ",")) != NULL
);
3940 remote_support_xml
= reconcat (remote_support_xml
,
3941 remote_support_xml
, ",", xml
,
3948 remote_query_supported_append (char *msg
, const char *append
)
3951 return reconcat (msg
, msg
, ";", append
, (char *) NULL
);
3953 return xstrdup (append
);
3957 remote_query_supported (void)
3959 struct remote_state
*rs
= get_remote_state ();
3962 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
3964 /* The packet support flags are handled differently for this packet
3965 than for most others. We treat an error, a disabled packet, and
3966 an empty response identically: any features which must be reported
3967 to be used will be automatically disabled. An empty buffer
3968 accomplishes this, since that is also the representation for a list
3969 containing no features. */
3972 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
3975 struct cleanup
*old_chain
= make_cleanup (free_current_contents
, &q
);
3977 q
= remote_query_supported_append (q
, "multiprocess+");
3979 if (remote_support_xml
)
3980 q
= remote_query_supported_append (q
, remote_support_xml
);
3982 q
= remote_query_supported_append (q
, "qRelocInsn+");
3984 q
= reconcat (q
, "qSupported:", q
, (char *) NULL
);
3987 do_cleanups (old_chain
);
3989 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3991 /* If an error occured, warn, but do not return - just reset the
3992 buffer to empty and go on to disable features. */
3993 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
3996 warning (_("Remote failure reply: %s"), rs
->buf
);
4001 memset (seen
, 0, sizeof (seen
));
4006 enum packet_support is_supported
;
4007 char *p
, *end
, *name_end
, *value
;
4009 /* First separate out this item from the rest of the packet. If
4010 there's another item after this, we overwrite the separator
4011 (terminated strings are much easier to work with). */
4013 end
= strchr (p
, ';');
4016 end
= p
+ strlen (p
);
4026 warning (_("empty item in \"qSupported\" response"));
4031 name_end
= strchr (p
, '=');
4034 /* This is a name=value entry. */
4035 is_supported
= PACKET_ENABLE
;
4036 value
= name_end
+ 1;
4045 is_supported
= PACKET_ENABLE
;
4049 is_supported
= PACKET_DISABLE
;
4053 is_supported
= PACKET_SUPPORT_UNKNOWN
;
4057 warning (_("unrecognized item \"%s\" "
4058 "in \"qSupported\" response"), p
);
4064 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
4065 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
4067 const struct protocol_feature
*feature
;
4070 feature
= &remote_protocol_features
[i
];
4071 feature
->func (feature
, is_supported
, value
);
4076 /* If we increased the packet size, make sure to increase the global
4077 buffer size also. We delay this until after parsing the entire
4078 qSupported packet, because this is the same buffer we were
4080 if (rs
->buf_size
< rs
->explicit_packet_size
)
4082 rs
->buf_size
= rs
->explicit_packet_size
;
4083 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
4086 /* Handle the defaults for unmentioned features. */
4087 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
4090 const struct protocol_feature
*feature
;
4092 feature
= &remote_protocol_features
[i
];
4093 feature
->func (feature
, feature
->default_support
, NULL
);
4099 remote_open_1 (char *name
, int from_tty
,
4100 struct target_ops
*target
, int extended_p
)
4102 struct remote_state
*rs
= get_remote_state ();
4105 error (_("To open a remote debug connection, you need to specify what\n"
4106 "serial device is attached to the remote system\n"
4107 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
4109 /* See FIXME above. */
4110 if (!target_async_permitted
)
4111 wait_forever_enabled_p
= 1;
4113 /* If we're connected to a running target, target_preopen will kill it.
4114 But if we're connected to a target system with no running process,
4115 then we will still be connected when it returns. Ask this question
4116 first, before target_preopen has a chance to kill anything. */
4117 if (remote_desc
!= NULL
&& !have_inferiors ())
4120 || query (_("Already connected to a remote target. Disconnect? ")))
4123 error (_("Still connected."));
4126 target_preopen (from_tty
);
4128 unpush_target (target
);
4130 /* This time without a query. If we were connected to an
4131 extended-remote target and target_preopen killed the running
4132 process, we may still be connected. If we are starting "target
4133 remote" now, the extended-remote target will not have been
4134 removed by unpush_target. */
4135 if (remote_desc
!= NULL
&& !have_inferiors ())
4138 /* Make sure we send the passed signals list the next time we resume. */
4139 xfree (last_pass_packet
);
4140 last_pass_packet
= NULL
;
4142 /* Make sure we send the program signals list the next time we
4144 xfree (last_program_signals_packet
);
4145 last_program_signals_packet
= NULL
;
4147 remote_fileio_reset ();
4148 reopen_exec_file ();
4151 remote_desc
= remote_serial_open (name
);
4153 perror_with_name (name
);
4155 if (baud_rate
!= -1)
4157 if (serial_setbaudrate (remote_desc
, baud_rate
))
4159 /* The requested speed could not be set. Error out to
4160 top level after closing remote_desc. Take care to
4161 set remote_desc to NULL to avoid closing remote_desc
4163 serial_close (remote_desc
);
4165 perror_with_name (name
);
4169 serial_raw (remote_desc
);
4171 /* If there is something sitting in the buffer we might take it as a
4172 response to a command, which would be bad. */
4173 serial_flush_input (remote_desc
);
4177 puts_filtered ("Remote debugging using ");
4178 puts_filtered (name
);
4179 puts_filtered ("\n");
4181 push_target (target
); /* Switch to using remote target now. */
4183 /* Register extra event sources in the event loop. */
4184 remote_async_inferior_event_token
4185 = create_async_event_handler (remote_async_inferior_event_handler
,
4187 remote_async_get_pending_events_token
4188 = create_async_event_handler (remote_async_get_pending_events_handler
,
4191 /* Reset the target state; these things will be queried either by
4192 remote_query_supported or as they are needed. */
4193 init_all_packet_configs ();
4194 rs
->cached_wait_status
= 0;
4195 rs
->explicit_packet_size
= 0;
4197 rs
->multi_process_aware
= 0;
4198 rs
->extended
= extended_p
;
4199 rs
->non_stop_aware
= 0;
4200 rs
->waiting_for_stop_reply
= 0;
4201 rs
->ctrlc_pending_p
= 0;
4203 general_thread
= not_sent_ptid
;
4204 continue_thread
= not_sent_ptid
;
4205 remote_traceframe_number
= -1;
4207 /* Probe for ability to use "ThreadInfo" query, as required. */
4208 use_threadinfo_query
= 1;
4209 use_threadextra_query
= 1;
4211 if (target_async_permitted
)
4213 /* With this target we start out by owning the terminal. */
4214 remote_async_terminal_ours_p
= 1;
4216 /* FIXME: cagney/1999-09-23: During the initial connection it is
4217 assumed that the target is already ready and able to respond to
4218 requests. Unfortunately remote_start_remote() eventually calls
4219 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4220 around this. Eventually a mechanism that allows
4221 wait_for_inferior() to expect/get timeouts will be
4223 wait_forever_enabled_p
= 0;
4226 /* First delete any symbols previously loaded from shared libraries. */
4227 no_shared_libraries (NULL
, 0);
4230 init_thread_list ();
4232 /* Start the remote connection. If error() or QUIT, discard this
4233 target (we'd otherwise be in an inconsistent state) and then
4234 propogate the error on up the exception chain. This ensures that
4235 the caller doesn't stumble along blindly assuming that the
4236 function succeeded. The CLI doesn't have this problem but other
4237 UI's, such as MI do.
4239 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4240 this function should return an error indication letting the
4241 caller restore the previous state. Unfortunately the command
4242 ``target remote'' is directly wired to this function making that
4243 impossible. On a positive note, the CLI side of this problem has
4244 been fixed - the function set_cmd_context() makes it possible for
4245 all the ``target ....'' commands to share a common callback
4246 function. See cli-dump.c. */
4248 volatile struct gdb_exception ex
;
4250 TRY_CATCH (ex
, RETURN_MASK_ALL
)
4252 remote_start_remote (from_tty
, target
, extended_p
);
4256 /* Pop the partially set up target - unless something else did
4257 already before throwing the exception. */
4258 if (remote_desc
!= NULL
)
4260 if (target_async_permitted
)
4261 wait_forever_enabled_p
= 1;
4262 throw_exception (ex
);
4266 if (target_async_permitted
)
4267 wait_forever_enabled_p
= 1;
4270 /* This takes a program previously attached to and detaches it. After
4271 this is done, GDB can be used to debug some other program. We
4272 better not have left any breakpoints in the target program or it'll
4273 die when it hits one. */
4276 remote_detach_1 (char *args
, int from_tty
, int extended
)
4278 int pid
= ptid_get_pid (inferior_ptid
);
4279 struct remote_state
*rs
= get_remote_state ();
4282 error (_("Argument given to \"detach\" when remotely debugging."));
4284 if (!target_has_execution
)
4285 error (_("No process to detach from."));
4289 char *exec_file
= get_exec_file (0);
4290 if (exec_file
== NULL
)
4292 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
4293 target_pid_to_str (pid_to_ptid (pid
)));
4294 gdb_flush (gdb_stdout
);
4297 /* Tell the remote target to detach. */
4298 if (remote_multi_process_p (rs
))
4299 sprintf (rs
->buf
, "D;%x", pid
);
4301 strcpy (rs
->buf
, "D");
4304 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4306 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
4308 else if (rs
->buf
[0] == '\0')
4309 error (_("Remote doesn't know how to detach"));
4311 error (_("Can't detach process."));
4313 if (from_tty
&& !extended
)
4314 puts_filtered (_("Ending remote debugging.\n"));
4316 discard_pending_stop_replies (pid
);
4317 target_mourn_inferior ();
4321 remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
4323 remote_detach_1 (args
, from_tty
, 0);
4327 extended_remote_detach (struct target_ops
*ops
, char *args
, int from_tty
)
4329 remote_detach_1 (args
, from_tty
, 1);
4332 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4335 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
4338 error (_("Argument given to \"disconnect\" when remotely debugging."));
4340 /* Make sure we unpush even the extended remote targets; mourn
4341 won't do it. So call remote_mourn_1 directly instead of
4342 target_mourn_inferior. */
4343 remote_mourn_1 (target
);
4346 puts_filtered ("Ending remote debugging.\n");
4349 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4350 be chatty about it. */
4353 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
4355 struct remote_state
*rs
= get_remote_state ();
4357 char *wait_status
= NULL
;
4359 pid
= parse_pid_to_attach (args
);
4361 /* Remote PID can be freely equal to getpid, do not check it here the same
4362 way as in other targets. */
4364 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4365 error (_("This target does not support attaching to a process"));
4369 char *exec_file
= get_exec_file (0);
4372 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
4373 target_pid_to_str (pid_to_ptid (pid
)));
4375 printf_unfiltered (_("Attaching to %s\n"),
4376 target_pid_to_str (pid_to_ptid (pid
)));
4378 gdb_flush (gdb_stdout
);
4381 sprintf (rs
->buf
, "vAttach;%x", pid
);
4383 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4385 if (packet_ok (rs
->buf
,
4386 &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
4390 /* Save the reply for later. */
4391 wait_status
= alloca (strlen (rs
->buf
) + 1);
4392 strcpy (wait_status
, rs
->buf
);
4394 else if (strcmp (rs
->buf
, "OK") != 0)
4395 error (_("Attaching to %s failed with: %s"),
4396 target_pid_to_str (pid_to_ptid (pid
)),
4399 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4400 error (_("This target does not support attaching to a process"));
4402 error (_("Attaching to %s failed"),
4403 target_pid_to_str (pid_to_ptid (pid
)));
4405 set_current_inferior (remote_add_inferior (0, pid
, 1));
4407 inferior_ptid
= pid_to_ptid (pid
);
4411 struct thread_info
*thread
;
4413 /* Get list of threads. */
4414 remote_threads_info (target
);
4416 thread
= first_thread_of_process (pid
);
4418 inferior_ptid
= thread
->ptid
;
4420 inferior_ptid
= pid_to_ptid (pid
);
4422 /* Invalidate our notion of the remote current thread. */
4423 record_currthread (minus_one_ptid
);
4427 /* Now, if we have thread information, update inferior_ptid. */
4428 inferior_ptid
= remote_current_thread (inferior_ptid
);
4430 /* Add the main thread to the thread list. */
4431 add_thread_silent (inferior_ptid
);
4434 /* Next, if the target can specify a description, read it. We do
4435 this before anything involving memory or registers. */
4436 target_find_description ();
4440 /* Use the previously fetched status. */
4441 gdb_assert (wait_status
!= NULL
);
4443 if (target_can_async_p ())
4445 struct stop_reply
*stop_reply
;
4446 struct cleanup
*old_chain
;
4448 stop_reply
= stop_reply_xmalloc ();
4449 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
4450 remote_parse_stop_reply (wait_status
, stop_reply
);
4451 discard_cleanups (old_chain
);
4452 push_stop_reply (stop_reply
);
4454 target_async (inferior_event_handler
, 0);
4458 gdb_assert (wait_status
!= NULL
);
4459 strcpy (rs
->buf
, wait_status
);
4460 rs
->cached_wait_status
= 1;
4464 gdb_assert (wait_status
== NULL
);
4468 extended_remote_attach (struct target_ops
*ops
, char *args
, int from_tty
)
4470 extended_remote_attach_1 (ops
, args
, from_tty
);
4473 /* Convert hex digit A to a number. */
4478 if (a
>= '0' && a
<= '9')
4480 else if (a
>= 'a' && a
<= 'f')
4481 return a
- 'a' + 10;
4482 else if (a
>= 'A' && a
<= 'F')
4483 return a
- 'A' + 10;
4485 error (_("Reply contains invalid hex digit %d"), a
);
4489 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
4493 for (i
= 0; i
< count
; i
++)
4495 if (hex
[0] == 0 || hex
[1] == 0)
4497 /* Hex string is short, or of uneven length.
4498 Return the count that has been converted so far. */
4501 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
4507 /* Convert number NIB to a hex digit. */
4515 return 'a' + nib
- 10;
4519 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
4523 /* May use a length, or a nul-terminated string as input. */
4525 count
= strlen ((char *) bin
);
4527 for (i
= 0; i
< count
; i
++)
4529 *hex
++ = tohex ((*bin
>> 4) & 0xf);
4530 *hex
++ = tohex (*bin
++ & 0xf);
4536 /* Check for the availability of vCont. This function should also check
4540 remote_vcont_probe (struct remote_state
*rs
)
4544 strcpy (rs
->buf
, "vCont?");
4546 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4549 /* Make sure that the features we assume are supported. */
4550 if (strncmp (buf
, "vCont", 5) == 0)
4553 int support_s
, support_S
, support_c
, support_C
;
4559 rs
->support_vCont_t
= 0;
4560 while (p
&& *p
== ';')
4563 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4565 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4567 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4569 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4571 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4572 rs
->support_vCont_t
= 1;
4574 p
= strchr (p
, ';');
4577 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4578 BUF will make packet_ok disable the packet. */
4579 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
4583 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
4586 /* Helper function for building "vCont" resumptions. Write a
4587 resumption to P. ENDP points to one-passed-the-end of the buffer
4588 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4589 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4590 resumed thread should be single-stepped and/or signalled. If PTID
4591 equals minus_one_ptid, then all threads are resumed; if PTID
4592 represents a process, then all threads of the process are resumed;
4593 the thread to be stepped and/or signalled is given in the global
4597 append_resumption (char *p
, char *endp
,
4598 ptid_t ptid
, int step
, enum target_signal siggnal
)
4600 struct remote_state
*rs
= get_remote_state ();
4602 if (step
&& siggnal
!= TARGET_SIGNAL_0
)
4603 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
4605 p
+= xsnprintf (p
, endp
- p
, ";s");
4606 else if (siggnal
!= TARGET_SIGNAL_0
)
4607 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
4609 p
+= xsnprintf (p
, endp
- p
, ";c");
4611 if (remote_multi_process_p (rs
) && ptid_is_pid (ptid
))
4615 /* All (-1) threads of process. */
4616 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4618 p
+= xsnprintf (p
, endp
- p
, ":");
4619 p
= write_ptid (p
, endp
, nptid
);
4621 else if (!ptid_equal (ptid
, minus_one_ptid
))
4623 p
+= xsnprintf (p
, endp
- p
, ":");
4624 p
= write_ptid (p
, endp
, ptid
);
4630 /* Resume the remote inferior by using a "vCont" packet. The thread
4631 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4632 resumed thread should be single-stepped and/or signalled. If PTID
4633 equals minus_one_ptid, then all threads are resumed; the thread to
4634 be stepped and/or signalled is given in the global INFERIOR_PTID.
4635 This function returns non-zero iff it resumes the inferior.
4637 This function issues a strict subset of all possible vCont commands at the
4641 remote_vcont_resume (ptid_t ptid
, int step
, enum target_signal siggnal
)
4643 struct remote_state
*rs
= get_remote_state ();
4647 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4648 remote_vcont_probe (rs
);
4650 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
4654 endp
= rs
->buf
+ get_remote_packet_size ();
4656 /* If we could generate a wider range of packets, we'd have to worry
4657 about overflowing BUF. Should there be a generic
4658 "multi-part-packet" packet? */
4660 p
+= xsnprintf (p
, endp
- p
, "vCont");
4662 if (ptid_equal (ptid
, magic_null_ptid
))
4664 /* MAGIC_NULL_PTID means that we don't have any active threads,
4665 so we don't have any TID numbers the inferior will
4666 understand. Make sure to only send forms that do not specify
4668 append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
4670 else if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
4672 /* Resume all threads (of all processes, or of a single
4673 process), with preference for INFERIOR_PTID. This assumes
4674 inferior_ptid belongs to the set of all threads we are about
4676 if (step
|| siggnal
!= TARGET_SIGNAL_0
)
4678 /* Step inferior_ptid, with or without signal. */
4679 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
4682 /* And continue others without a signal. */
4683 append_resumption (p
, endp
, ptid
, /*step=*/ 0, TARGET_SIGNAL_0
);
4687 /* Scheduler locking; resume only PTID. */
4688 append_resumption (p
, endp
, ptid
, step
, siggnal
);
4691 gdb_assert (strlen (rs
->buf
) < get_remote_packet_size ());
4696 /* In non-stop, the stub replies to vCont with "OK". The stop
4697 reply will be reported asynchronously by means of a `%Stop'
4699 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4700 if (strcmp (rs
->buf
, "OK") != 0)
4701 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
);
4707 /* Tell the remote machine to resume. */
4709 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
4711 static int last_sent_step
;
4714 remote_resume (struct target_ops
*ops
,
4715 ptid_t ptid
, int step
, enum target_signal siggnal
)
4717 struct remote_state
*rs
= get_remote_state ();
4720 last_sent_signal
= siggnal
;
4721 last_sent_step
= step
;
4723 /* The vCont packet doesn't need to specify threads via Hc. */
4724 /* No reverse support (yet) for vCont. */
4725 if (execution_direction
!= EXEC_REVERSE
)
4726 if (remote_vcont_resume (ptid
, step
, siggnal
))
4729 /* All other supported resume packets do use Hc, so set the continue
4731 if (ptid_equal (ptid
, minus_one_ptid
))
4732 set_continue_thread (any_thread_ptid
);
4734 set_continue_thread (ptid
);
4737 if (execution_direction
== EXEC_REVERSE
)
4739 /* We don't pass signals to the target in reverse exec mode. */
4740 if (info_verbose
&& siggnal
!= TARGET_SIGNAL_0
)
4741 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
4745 && remote_protocol_packets
[PACKET_bs
].support
== PACKET_DISABLE
)
4746 error (_("Remote reverse-step not supported."));
4748 && remote_protocol_packets
[PACKET_bc
].support
== PACKET_DISABLE
)
4749 error (_("Remote reverse-continue not supported."));
4751 strcpy (buf
, step
? "bs" : "bc");
4753 else if (siggnal
!= TARGET_SIGNAL_0
)
4755 buf
[0] = step
? 'S' : 'C';
4756 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
4757 buf
[2] = tohex (((int) siggnal
) & 0xf);
4761 strcpy (buf
, step
? "s" : "c");
4766 /* We are about to start executing the inferior, let's register it
4767 with the event loop. NOTE: this is the one place where all the
4768 execution commands end up. We could alternatively do this in each
4769 of the execution commands in infcmd.c. */
4770 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4771 into infcmd.c in order to allow inferior function calls to work
4772 NOT asynchronously. */
4773 if (target_can_async_p ())
4774 target_async (inferior_event_handler
, 0);
4776 /* We've just told the target to resume. The remote server will
4777 wait for the inferior to stop, and then send a stop reply. In
4778 the mean time, we can't start another command/query ourselves
4779 because the stub wouldn't be ready to process it. This applies
4780 only to the base all-stop protocol, however. In non-stop (which
4781 only supports vCont), the stub replies with an "OK", and is
4782 immediate able to process further serial input. */
4784 rs
->waiting_for_stop_reply
= 1;
4788 /* Set up the signal handler for SIGINT, while the target is
4789 executing, ovewriting the 'regular' SIGINT signal handler. */
4791 initialize_sigint_signal_handler (void)
4793 signal (SIGINT
, handle_remote_sigint
);
4796 /* Signal handler for SIGINT, while the target is executing. */
4798 handle_remote_sigint (int sig
)
4800 signal (sig
, handle_remote_sigint_twice
);
4801 mark_async_signal_handler_wrapper (sigint_remote_token
);
4804 /* Signal handler for SIGINT, installed after SIGINT has already been
4805 sent once. It will take effect the second time that the user sends
4808 handle_remote_sigint_twice (int sig
)
4810 signal (sig
, handle_remote_sigint
);
4811 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
4814 /* Perform the real interruption of the target execution, in response
4817 async_remote_interrupt (gdb_client_data arg
)
4820 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
4822 target_stop (inferior_ptid
);
4825 /* Perform interrupt, if the first attempt did not succeed. Just give
4826 up on the target alltogether. */
4828 async_remote_interrupt_twice (gdb_client_data arg
)
4831 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
4836 /* Reinstall the usual SIGINT handlers, after the target has
4839 cleanup_sigint_signal_handler (void *dummy
)
4841 signal (SIGINT
, handle_sigint
);
4844 /* Send ^C to target to halt it. Target will respond, and send us a
4846 static void (*ofunc
) (int);
4848 /* The command line interface's stop routine. This function is installed
4849 as a signal handler for SIGINT. The first time a user requests a
4850 stop, we call remote_stop to send a break or ^C. If there is no
4851 response from the target (it didn't stop when the user requested it),
4852 we ask the user if he'd like to detach from the target. */
4854 remote_interrupt (int signo
)
4856 /* If this doesn't work, try more severe steps. */
4857 signal (signo
, remote_interrupt_twice
);
4859 gdb_call_async_signal_handler (sigint_remote_token
, 1);
4862 /* The user typed ^C twice. */
4865 remote_interrupt_twice (int signo
)
4867 signal (signo
, ofunc
);
4868 gdb_call_async_signal_handler (sigint_remote_twice_token
, 1);
4869 signal (signo
, remote_interrupt
);
4872 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
4873 thread, all threads of a remote process, or all threads of all
4877 remote_stop_ns (ptid_t ptid
)
4879 struct remote_state
*rs
= get_remote_state ();
4881 char *endp
= rs
->buf
+ get_remote_packet_size ();
4883 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4884 remote_vcont_probe (rs
);
4886 if (!rs
->support_vCont_t
)
4887 error (_("Remote server does not support stopping threads"));
4889 if (ptid_equal (ptid
, minus_one_ptid
)
4890 || (!remote_multi_process_p (rs
) && ptid_is_pid (ptid
)))
4891 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
4896 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
4898 if (ptid_is_pid (ptid
))
4899 /* All (-1) threads of process. */
4900 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4903 /* Small optimization: if we already have a stop reply for
4904 this thread, no use in telling the stub we want this
4906 if (peek_stop_reply (ptid
))
4912 write_ptid (p
, endp
, nptid
);
4915 /* In non-stop, we get an immediate OK reply. The stop reply will
4916 come in asynchronously by notification. */
4918 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4919 if (strcmp (rs
->buf
, "OK") != 0)
4920 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
), rs
->buf
);
4923 /* All-stop version of target_stop. Sends a break or a ^C to stop the
4924 remote target. It is undefined which thread of which process
4925 reports the stop. */
4928 remote_stop_as (ptid_t ptid
)
4930 struct remote_state
*rs
= get_remote_state ();
4932 rs
->ctrlc_pending_p
= 1;
4934 /* If the inferior is stopped already, but the core didn't know
4935 about it yet, just ignore the request. The cached wait status
4936 will be collected in remote_wait. */
4937 if (rs
->cached_wait_status
)
4940 /* Send interrupt_sequence to remote target. */
4941 send_interrupt_sequence ();
4944 /* This is the generic stop called via the target vector. When a target
4945 interrupt is requested, either by the command line or the GUI, we
4946 will eventually end up here. */
4949 remote_stop (ptid_t ptid
)
4952 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
4955 remote_stop_ns (ptid
);
4957 remote_stop_as (ptid
);
4960 /* Ask the user what to do when an interrupt is received. */
4963 interrupt_query (void)
4965 target_terminal_ours ();
4967 if (target_can_async_p ())
4969 signal (SIGINT
, handle_sigint
);
4970 deprecated_throw_reason (RETURN_QUIT
);
4974 if (query (_("Interrupted while waiting for the program.\n\
4975 Give up (and stop debugging it)? ")))
4978 deprecated_throw_reason (RETURN_QUIT
);
4982 target_terminal_inferior ();
4985 /* Enable/disable target terminal ownership. Most targets can use
4986 terminal groups to control terminal ownership. Remote targets are
4987 different in that explicit transfer of ownership to/from GDB/target
4991 remote_terminal_inferior (void)
4993 if (!target_async_permitted
)
4994 /* Nothing to do. */
4997 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
4998 idempotent. The event-loop GDB talking to an asynchronous target
4999 with a synchronous command calls this function from both
5000 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5001 transfer the terminal to the target when it shouldn't this guard
5003 if (!remote_async_terminal_ours_p
)
5005 delete_file_handler (input_fd
);
5006 remote_async_terminal_ours_p
= 0;
5007 initialize_sigint_signal_handler ();
5008 /* NOTE: At this point we could also register our selves as the
5009 recipient of all input. Any characters typed could then be
5010 passed on down to the target. */
5014 remote_terminal_ours (void)
5016 if (!target_async_permitted
)
5017 /* Nothing to do. */
5020 /* See FIXME in remote_terminal_inferior. */
5021 if (remote_async_terminal_ours_p
)
5023 cleanup_sigint_signal_handler (NULL
);
5024 add_file_handler (input_fd
, stdin_event_handler
, 0);
5025 remote_async_terminal_ours_p
= 1;
5029 remote_console_output (char *msg
)
5033 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
5036 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
5040 fputs_unfiltered (tb
, gdb_stdtarg
);
5042 gdb_flush (gdb_stdtarg
);
5045 typedef struct cached_reg
5048 gdb_byte data
[MAX_REGISTER_SIZE
];
5051 DEF_VEC_O(cached_reg_t
);
5055 struct stop_reply
*next
;
5059 struct target_waitstatus ws
;
5061 /* Expedited registers. This makes remote debugging a bit more
5062 efficient for those targets that provide critical registers as
5063 part of their normal status mechanism (as another roundtrip to
5064 fetch them is avoided). */
5065 VEC(cached_reg_t
) *regcache
;
5067 int stopped_by_watchpoint_p
;
5068 CORE_ADDR watch_data_address
;
5076 /* The list of already fetched and acknowledged stop events. */
5077 static struct stop_reply
*stop_reply_queue
;
5079 static struct stop_reply
*
5080 stop_reply_xmalloc (void)
5082 struct stop_reply
*r
= XMALLOC (struct stop_reply
);
5089 stop_reply_xfree (struct stop_reply
*r
)
5093 VEC_free (cached_reg_t
, r
->regcache
);
5098 /* Discard all pending stop replies of inferior PID. If PID is -1,
5099 discard everything. */
5102 discard_pending_stop_replies (int pid
)
5104 struct stop_reply
*prev
= NULL
, *reply
, *next
;
5106 /* Discard the in-flight notification. */
5107 if (pending_stop_reply
!= NULL
5109 || ptid_get_pid (pending_stop_reply
->ptid
) == pid
))
5111 stop_reply_xfree (pending_stop_reply
);
5112 pending_stop_reply
= NULL
;
5115 /* Discard the stop replies we have already pulled with
5117 for (reply
= stop_reply_queue
; reply
; reply
= next
)
5121 || ptid_get_pid (reply
->ptid
) == pid
)
5123 if (reply
== stop_reply_queue
)
5124 stop_reply_queue
= reply
->next
;
5126 prev
->next
= reply
->next
;
5128 stop_reply_xfree (reply
);
5135 /* Cleanup wrapper. */
5138 do_stop_reply_xfree (void *arg
)
5140 struct stop_reply
*r
= arg
;
5142 stop_reply_xfree (r
);
5145 /* Look for a queued stop reply belonging to PTID. If one is found,
5146 remove it from the queue, and return it. Returns NULL if none is
5147 found. If there are still queued events left to process, tell the
5148 event loop to get back to target_wait soon. */
5150 static struct stop_reply
*
5151 queued_stop_reply (ptid_t ptid
)
5153 struct stop_reply
*it
;
5154 struct stop_reply
**it_link
;
5156 it
= stop_reply_queue
;
5157 it_link
= &stop_reply_queue
;
5160 if (ptid_match (it
->ptid
, ptid
))
5162 *it_link
= it
->next
;
5167 it_link
= &it
->next
;
5171 if (stop_reply_queue
)
5172 /* There's still at least an event left. */
5173 mark_async_event_handler (remote_async_inferior_event_token
);
5178 /* Push a fully parsed stop reply in the stop reply queue. Since we
5179 know that we now have at least one queued event left to pass to the
5180 core side, tell the event loop to get back to target_wait soon. */
5183 push_stop_reply (struct stop_reply
*new_event
)
5185 struct stop_reply
*event
;
5187 if (stop_reply_queue
)
5189 for (event
= stop_reply_queue
;
5190 event
&& event
->next
;
5191 event
= event
->next
)
5194 event
->next
= new_event
;
5197 stop_reply_queue
= new_event
;
5199 mark_async_event_handler (remote_async_inferior_event_token
);
5202 /* Returns true if we have a stop reply for PTID. */
5205 peek_stop_reply (ptid_t ptid
)
5207 struct stop_reply
*it
;
5209 for (it
= stop_reply_queue
; it
; it
= it
->next
)
5210 if (ptid_equal (ptid
, it
->ptid
))
5212 if (it
->ws
.kind
== TARGET_WAITKIND_STOPPED
)
5219 /* Parse the stop reply in BUF. Either the function succeeds, and the
5220 result is stored in EVENT, or throws an error. */
5223 remote_parse_stop_reply (char *buf
, struct stop_reply
*event
)
5225 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5229 event
->ptid
= null_ptid
;
5230 event
->ws
.kind
= TARGET_WAITKIND_IGNORE
;
5231 event
->ws
.value
.integer
= 0;
5232 event
->solibs_changed
= 0;
5233 event
->replay_event
= 0;
5234 event
->stopped_by_watchpoint_p
= 0;
5235 event
->regcache
= NULL
;
5240 case 'T': /* Status with PC, SP, FP, ... */
5241 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5242 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5244 n... = register number
5245 r... = register contents
5248 p
= &buf
[3]; /* after Txx */
5256 /* If the packet contains a register number, save it in
5257 pnum and set p1 to point to the character following it.
5258 Otherwise p1 points to p. */
5260 /* If this packet is an awatch packet, don't parse the 'a'
5261 as a register number. */
5263 if (strncmp (p
, "awatch", strlen("awatch")) != 0
5264 && strncmp (p
, "core", strlen ("core") != 0))
5266 /* Read the ``P'' register number. */
5267 pnum
= strtol (p
, &p_temp
, 16);
5273 if (p1
== p
) /* No register number present here. */
5275 p1
= strchr (p
, ':');
5277 error (_("Malformed packet(a) (missing colon): %s\n\
5280 if (strncmp (p
, "thread", p1
- p
) == 0)
5281 event
->ptid
= read_ptid (++p1
, &p
);
5282 else if ((strncmp (p
, "watch", p1
- p
) == 0)
5283 || (strncmp (p
, "rwatch", p1
- p
) == 0)
5284 || (strncmp (p
, "awatch", p1
- p
) == 0))
5286 event
->stopped_by_watchpoint_p
= 1;
5287 p
= unpack_varlen_hex (++p1
, &addr
);
5288 event
->watch_data_address
= (CORE_ADDR
) addr
;
5290 else if (strncmp (p
, "library", p1
- p
) == 0)
5294 while (*p_temp
&& *p_temp
!= ';')
5297 event
->solibs_changed
= 1;
5300 else if (strncmp (p
, "replaylog", p1
- p
) == 0)
5302 /* NO_HISTORY event.
5303 p1 will indicate "begin" or "end", but
5304 it makes no difference for now, so ignore it. */
5305 event
->replay_event
= 1;
5306 p_temp
= strchr (p1
+ 1, ';');
5310 else if (strncmp (p
, "core", p1
- p
) == 0)
5314 p
= unpack_varlen_hex (++p1
, &c
);
5319 /* Silently skip unknown optional info. */
5320 p_temp
= strchr (p1
+ 1, ';');
5327 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
5328 cached_reg_t cached_reg
;
5333 error (_("Malformed packet(b) (missing colon): %s\n\
5339 error (_("Remote sent bad register number %s: %s\n\
5341 hex_string (pnum
), p
, buf
);
5343 cached_reg
.num
= reg
->regnum
;
5345 fieldsize
= hex2bin (p
, cached_reg
.data
,
5346 register_size (target_gdbarch
,
5349 if (fieldsize
< register_size (target_gdbarch
,
5351 warning (_("Remote reply is too short: %s"), buf
);
5353 VEC_safe_push (cached_reg_t
, event
->regcache
, &cached_reg
);
5357 error (_("Remote register badly formatted: %s\nhere: %s"),
5362 case 'S': /* Old style status, just signal only. */
5363 if (event
->solibs_changed
)
5364 event
->ws
.kind
= TARGET_WAITKIND_LOADED
;
5365 else if (event
->replay_event
)
5366 event
->ws
.kind
= TARGET_WAITKIND_NO_HISTORY
;
5369 event
->ws
.kind
= TARGET_WAITKIND_STOPPED
;
5370 event
->ws
.value
.sig
= (enum target_signal
)
5371 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
5374 case 'W': /* Target exited. */
5381 /* GDB used to accept only 2 hex chars here. Stubs should
5382 only send more if they detect GDB supports multi-process
5384 p
= unpack_varlen_hex (&buf
[1], &value
);
5388 /* The remote process exited. */
5389 event
->ws
.kind
= TARGET_WAITKIND_EXITED
;
5390 event
->ws
.value
.integer
= value
;
5394 /* The remote process exited with a signal. */
5395 event
->ws
.kind
= TARGET_WAITKIND_SIGNALLED
;
5396 event
->ws
.value
.sig
= (enum target_signal
) value
;
5399 /* If no process is specified, assume inferior_ptid. */
5400 pid
= ptid_get_pid (inferior_ptid
);
5409 else if (strncmp (p
,
5410 "process:", sizeof ("process:") - 1) == 0)
5414 p
+= sizeof ("process:") - 1;
5415 unpack_varlen_hex (p
, &upid
);
5419 error (_("unknown stop reply packet: %s"), buf
);
5422 error (_("unknown stop reply packet: %s"), buf
);
5423 event
->ptid
= pid_to_ptid (pid
);
5428 if (non_stop
&& ptid_equal (event
->ptid
, null_ptid
))
5429 error (_("No process or thread specified in stop reply: %s"), buf
);
5432 /* When the stub wants to tell GDB about a new stop reply, it sends a
5433 stop notification (%Stop). Those can come it at any time, hence,
5434 we have to make sure that any pending putpkt/getpkt sequence we're
5435 making is finished, before querying the stub for more events with
5436 vStopped. E.g., if we started a vStopped sequence immediatelly
5437 upon receiving the %Stop notification, something like this could
5445 1.6) <-- (registers reply to step #1.3)
5447 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5450 To solve this, whenever we parse a %Stop notification sucessfully,
5451 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5452 doing whatever we were doing:
5458 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5459 2.5) <-- (registers reply to step #2.3)
5461 Eventualy after step #2.5, we return to the event loop, which
5462 notices there's an event on the
5463 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5464 associated callback --- the function below. At this point, we're
5465 always safe to start a vStopped sequence. :
5468 2.7) <-- T05 thread:2
5474 remote_get_pending_stop_replies (void)
5476 struct remote_state
*rs
= get_remote_state ();
5478 if (pending_stop_reply
)
5481 putpkt ("vStopped");
5483 /* Now we can rely on it. */
5484 push_stop_reply (pending_stop_reply
);
5485 pending_stop_reply
= NULL
;
5489 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5490 if (strcmp (rs
->buf
, "OK") == 0)
5494 struct cleanup
*old_chain
;
5495 struct stop_reply
*stop_reply
= stop_reply_xmalloc ();
5497 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
5498 remote_parse_stop_reply (rs
->buf
, stop_reply
);
5501 putpkt ("vStopped");
5503 if (stop_reply
->ws
.kind
!= TARGET_WAITKIND_IGNORE
)
5505 /* Now we can rely on it. */
5506 discard_cleanups (old_chain
);
5507 push_stop_reply (stop_reply
);
5510 /* We got an unknown stop reply. */
5511 do_cleanups (old_chain
);
5518 /* Called when it is decided that STOP_REPLY holds the info of the
5519 event that is to be returned to the core. This function always
5520 destroys STOP_REPLY. */
5523 process_stop_reply (struct stop_reply
*stop_reply
,
5524 struct target_waitstatus
*status
)
5528 *status
= stop_reply
->ws
;
5529 ptid
= stop_reply
->ptid
;
5531 /* If no thread/process was reported by the stub, assume the current
5533 if (ptid_equal (ptid
, null_ptid
))
5534 ptid
= inferior_ptid
;
5536 if (status
->kind
!= TARGET_WAITKIND_EXITED
5537 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5539 /* Expedited registers. */
5540 if (stop_reply
->regcache
)
5542 struct regcache
*regcache
5543 = get_thread_arch_regcache (ptid
, target_gdbarch
);
5548 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
5550 regcache_raw_supply (regcache
, reg
->num
, reg
->data
);
5551 VEC_free (cached_reg_t
, stop_reply
->regcache
);
5554 remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
5555 remote_watch_data_address
= stop_reply
->watch_data_address
;
5557 remote_notice_new_inferior (ptid
, 0);
5558 demand_private_info (ptid
)->core
= stop_reply
->core
;
5561 stop_reply_xfree (stop_reply
);
5565 /* The non-stop mode version of target_wait. */
5568 remote_wait_ns (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5570 struct remote_state
*rs
= get_remote_state ();
5571 struct stop_reply
*stop_reply
;
5574 /* If in non-stop mode, get out of getpkt even if a
5575 notification is received. */
5577 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5584 case 'E': /* Error of some sort. */
5585 /* We're out of sync with the target now. Did it continue
5586 or not? We can't tell which thread it was in non-stop,
5587 so just ignore this. */
5588 warning (_("Remote failure reply: %s"), rs
->buf
);
5590 case 'O': /* Console output. */
5591 remote_console_output (rs
->buf
+ 1);
5594 warning (_("Invalid remote reply: %s"), rs
->buf
);
5598 /* Acknowledge a pending stop reply that may have arrived in the
5600 if (pending_stop_reply
!= NULL
)
5601 remote_get_pending_stop_replies ();
5603 /* If indeed we noticed a stop reply, we're done. */
5604 stop_reply
= queued_stop_reply (ptid
);
5605 if (stop_reply
!= NULL
)
5606 return process_stop_reply (stop_reply
, status
);
5608 /* Still no event. If we're just polling for an event, then
5609 return to the event loop. */
5610 if (options
& TARGET_WNOHANG
)
5612 status
->kind
= TARGET_WAITKIND_IGNORE
;
5613 return minus_one_ptid
;
5616 /* Otherwise do a blocking wait. */
5617 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5622 /* Wait until the remote machine stops, then return, storing status in
5623 STATUS just as `wait' would. */
5626 remote_wait_as (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5628 struct remote_state
*rs
= get_remote_state ();
5629 ptid_t event_ptid
= null_ptid
;
5631 struct stop_reply
*stop_reply
;
5635 status
->kind
= TARGET_WAITKIND_IGNORE
;
5636 status
->value
.integer
= 0;
5638 stop_reply
= queued_stop_reply (ptid
);
5639 if (stop_reply
!= NULL
)
5640 return process_stop_reply (stop_reply
, status
);
5642 if (rs
->cached_wait_status
)
5643 /* Use the cached wait status, but only once. */
5644 rs
->cached_wait_status
= 0;
5649 if (!target_is_async_p ())
5651 ofunc
= signal (SIGINT
, remote_interrupt
);
5652 /* If the user hit C-c before this packet, or between packets,
5653 pretend that it was hit right here. */
5657 remote_interrupt (SIGINT
);
5661 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5662 _never_ wait for ever -> test on target_is_async_p().
5663 However, before we do that we need to ensure that the caller
5664 knows how to take the target into/out of async mode. */
5665 ret
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, wait_forever_enabled_p
);
5666 if (!target_is_async_p ())
5667 signal (SIGINT
, ofunc
);
5672 remote_stopped_by_watchpoint_p
= 0;
5674 /* We got something. */
5675 rs
->waiting_for_stop_reply
= 0;
5677 /* Assume that the target has acknowledged Ctrl-C unless we receive
5678 an 'F' or 'O' packet. */
5679 if (buf
[0] != 'F' && buf
[0] != 'O')
5680 rs
->ctrlc_pending_p
= 0;
5684 case 'E': /* Error of some sort. */
5685 /* We're out of sync with the target now. Did it continue or
5686 not? Not is more likely, so report a stop. */
5687 warning (_("Remote failure reply: %s"), buf
);
5688 status
->kind
= TARGET_WAITKIND_STOPPED
;
5689 status
->value
.sig
= TARGET_SIGNAL_0
;
5691 case 'F': /* File-I/O request. */
5692 remote_fileio_request (buf
, rs
->ctrlc_pending_p
);
5693 rs
->ctrlc_pending_p
= 0;
5695 case 'T': case 'S': case 'X': case 'W':
5697 struct stop_reply
*stop_reply
;
5698 struct cleanup
*old_chain
;
5700 stop_reply
= stop_reply_xmalloc ();
5701 old_chain
= make_cleanup (do_stop_reply_xfree
, stop_reply
);
5702 remote_parse_stop_reply (buf
, stop_reply
);
5703 discard_cleanups (old_chain
);
5704 event_ptid
= process_stop_reply (stop_reply
, status
);
5707 case 'O': /* Console output. */
5708 remote_console_output (buf
+ 1);
5710 /* The target didn't really stop; keep waiting. */
5711 rs
->waiting_for_stop_reply
= 1;
5715 if (last_sent_signal
!= TARGET_SIGNAL_0
)
5717 /* Zero length reply means that we tried 'S' or 'C' and the
5718 remote system doesn't support it. */
5719 target_terminal_ours_for_output ();
5721 ("Can't send signals to this remote system. %s not sent.\n",
5722 target_signal_to_name (last_sent_signal
));
5723 last_sent_signal
= TARGET_SIGNAL_0
;
5724 target_terminal_inferior ();
5726 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
5727 putpkt ((char *) buf
);
5729 /* We just told the target to resume, so a stop reply is in
5731 rs
->waiting_for_stop_reply
= 1;
5734 /* else fallthrough */
5736 warning (_("Invalid remote reply: %s"), buf
);
5738 rs
->waiting_for_stop_reply
= 1;
5742 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
5744 /* Nothing interesting happened. If we're doing a non-blocking
5745 poll, we're done. Otherwise, go back to waiting. */
5746 if (options
& TARGET_WNOHANG
)
5747 return minus_one_ptid
;
5751 else if (status
->kind
!= TARGET_WAITKIND_EXITED
5752 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5754 if (!ptid_equal (event_ptid
, null_ptid
))
5755 record_currthread (event_ptid
);
5757 event_ptid
= inferior_ptid
;
5760 /* A process exit. Invalidate our notion of current thread. */
5761 record_currthread (minus_one_ptid
);
5766 /* Wait until the remote machine stops, then return, storing status in
5767 STATUS just as `wait' would. */
5770 remote_wait (struct target_ops
*ops
,
5771 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5776 event_ptid
= remote_wait_ns (ptid
, status
, options
);
5778 event_ptid
= remote_wait_as (ptid
, status
, options
);
5780 if (target_can_async_p ())
5782 /* If there are are events left in the queue tell the event loop
5784 if (stop_reply_queue
)
5785 mark_async_event_handler (remote_async_inferior_event_token
);
5791 /* Fetch a single register using a 'p' packet. */
5794 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
5796 struct remote_state
*rs
= get_remote_state ();
5798 char regp
[MAX_REGISTER_SIZE
];
5801 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
5804 if (reg
->pnum
== -1)
5809 p
+= hexnumstr (p
, reg
->pnum
);
5812 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5816 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
5820 case PACKET_UNKNOWN
:
5823 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
5824 gdbarch_register_name (get_regcache_arch (regcache
),
5829 /* If this register is unfetchable, tell the regcache. */
5832 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
5836 /* Otherwise, parse and supply the value. */
5842 error (_("fetch_register_using_p: early buf termination"));
5844 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
5847 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
5851 /* Fetch the registers included in the target's 'g' packet. */
5854 send_g_packet (void)
5856 struct remote_state
*rs
= get_remote_state ();
5859 sprintf (rs
->buf
, "g");
5860 remote_send (&rs
->buf
, &rs
->buf_size
);
5862 /* We can get out of synch in various cases. If the first character
5863 in the buffer is not a hex character, assume that has happened
5864 and try to fetch another packet to read. */
5865 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
5866 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
5867 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
5868 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
5871 fprintf_unfiltered (gdb_stdlog
,
5872 "Bad register packet; fetching a new packet\n");
5873 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5876 buf_len
= strlen (rs
->buf
);
5878 /* Sanity check the received packet. */
5879 if (buf_len
% 2 != 0)
5880 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
5886 process_g_packet (struct regcache
*regcache
)
5888 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
5889 struct remote_state
*rs
= get_remote_state ();
5890 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5895 buf_len
= strlen (rs
->buf
);
5897 /* Further sanity checks, with knowledge of the architecture. */
5898 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
5899 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
5901 /* Save the size of the packet sent to us by the target. It is used
5902 as a heuristic when determining the max size of packets that the
5903 target can safely receive. */
5904 if (rsa
->actual_register_packet_size
== 0)
5905 rsa
->actual_register_packet_size
= buf_len
;
5907 /* If this is smaller than we guessed the 'g' packet would be,
5908 update our records. A 'g' reply that doesn't include a register's
5909 value implies either that the register is not available, or that
5910 the 'p' packet must be used. */
5911 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
5913 rsa
->sizeof_g_packet
= buf_len
/ 2;
5915 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
5917 if (rsa
->regs
[i
].pnum
== -1)
5920 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
5921 rsa
->regs
[i
].in_g_packet
= 0;
5923 rsa
->regs
[i
].in_g_packet
= 1;
5927 regs
= alloca (rsa
->sizeof_g_packet
);
5929 /* Unimplemented registers read as all bits zero. */
5930 memset (regs
, 0, rsa
->sizeof_g_packet
);
5932 /* Reply describes registers byte by byte, each byte encoded as two
5933 hex characters. Suck them all up, then supply them to the
5934 register cacheing/storage mechanism. */
5937 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
5939 if (p
[0] == 0 || p
[1] == 0)
5940 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
5941 internal_error (__FILE__
, __LINE__
,
5942 _("unexpected end of 'g' packet reply"));
5944 if (p
[0] == 'x' && p
[1] == 'x')
5945 regs
[i
] = 0; /* 'x' */
5947 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
5951 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
5953 struct packet_reg
*r
= &rsa
->regs
[i
];
5957 if (r
->offset
* 2 >= strlen (rs
->buf
))
5958 /* This shouldn't happen - we adjusted in_g_packet above. */
5959 internal_error (__FILE__
, __LINE__
,
5960 _("unexpected end of 'g' packet reply"));
5961 else if (rs
->buf
[r
->offset
* 2] == 'x')
5963 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
5964 /* The register isn't available, mark it as such (at
5965 the same time setting the value to zero). */
5966 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
5969 regcache_raw_supply (regcache
, r
->regnum
,
5976 fetch_registers_using_g (struct regcache
*regcache
)
5979 process_g_packet (regcache
);
5982 /* Make the remote selected traceframe match GDB's selected
5986 set_remote_traceframe (void)
5990 if (remote_traceframe_number
== get_traceframe_number ())
5993 /* Avoid recursion, remote_trace_find calls us again. */
5994 remote_traceframe_number
= get_traceframe_number ();
5996 newnum
= target_trace_find (tfind_number
,
5997 get_traceframe_number (), 0, 0, NULL
);
5999 /* Should not happen. If it does, all bets are off. */
6000 if (newnum
!= get_traceframe_number ())
6001 warning (_("could not set remote traceframe"));
6005 remote_fetch_registers (struct target_ops
*ops
,
6006 struct regcache
*regcache
, int regnum
)
6008 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6011 set_remote_traceframe ();
6012 set_general_thread (inferior_ptid
);
6016 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
6018 gdb_assert (reg
!= NULL
);
6020 /* If this register might be in the 'g' packet, try that first -
6021 we are likely to read more than one register. If this is the
6022 first 'g' packet, we might be overly optimistic about its
6023 contents, so fall back to 'p'. */
6024 if (reg
->in_g_packet
)
6026 fetch_registers_using_g (regcache
);
6027 if (reg
->in_g_packet
)
6031 if (fetch_register_using_p (regcache
, reg
))
6034 /* This register is not available. */
6035 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
6040 fetch_registers_using_g (regcache
);
6042 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6043 if (!rsa
->regs
[i
].in_g_packet
)
6044 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
6046 /* This register is not available. */
6047 regcache_raw_supply (regcache
, i
, NULL
);
6051 /* Prepare to store registers. Since we may send them all (using a
6052 'G' request), we have to read out the ones we don't want to change
6056 remote_prepare_to_store (struct regcache
*regcache
)
6058 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6060 gdb_byte buf
[MAX_REGISTER_SIZE
];
6062 /* Make sure the entire registers array is valid. */
6063 switch (remote_protocol_packets
[PACKET_P
].support
)
6065 case PACKET_DISABLE
:
6066 case PACKET_SUPPORT_UNKNOWN
:
6067 /* Make sure all the necessary registers are cached. */
6068 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6069 if (rsa
->regs
[i
].in_g_packet
)
6070 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
6077 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
6078 packet was not recognized. */
6081 store_register_using_P (const struct regcache
*regcache
,
6082 struct packet_reg
*reg
)
6084 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
6085 struct remote_state
*rs
= get_remote_state ();
6086 /* Try storing a single register. */
6087 char *buf
= rs
->buf
;
6088 gdb_byte regp
[MAX_REGISTER_SIZE
];
6091 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
6094 if (reg
->pnum
== -1)
6097 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
6098 p
= buf
+ strlen (buf
);
6099 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
6100 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
6102 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6104 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
6109 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6110 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
);
6111 case PACKET_UNKNOWN
:
6114 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
6118 /* Store register REGNUM, or all registers if REGNUM == -1, from the
6119 contents of the register cache buffer. FIXME: ignores errors. */
6122 store_registers_using_G (const struct regcache
*regcache
)
6124 struct remote_state
*rs
= get_remote_state ();
6125 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6129 /* Extract all the registers in the regcache copying them into a
6134 regs
= alloca (rsa
->sizeof_g_packet
);
6135 memset (regs
, 0, rsa
->sizeof_g_packet
);
6136 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6138 struct packet_reg
*r
= &rsa
->regs
[i
];
6141 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
6145 /* Command describes registers byte by byte,
6146 each byte encoded as two hex characters. */
6149 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6151 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
6153 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6154 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
6155 error (_("Could not write registers; remote failure reply '%s'"),
6159 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6160 of the register cache buffer. FIXME: ignores errors. */
6163 remote_store_registers (struct target_ops
*ops
,
6164 struct regcache
*regcache
, int regnum
)
6166 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6169 set_remote_traceframe ();
6170 set_general_thread (inferior_ptid
);
6174 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
6176 gdb_assert (reg
!= NULL
);
6178 /* Always prefer to store registers using the 'P' packet if
6179 possible; we often change only a small number of registers.
6180 Sometimes we change a larger number; we'd need help from a
6181 higher layer to know to use 'G'. */
6182 if (store_register_using_P (regcache
, reg
))
6185 /* For now, don't complain if we have no way to write the
6186 register. GDB loses track of unavailable registers too
6187 easily. Some day, this may be an error. We don't have
6188 any way to read the register, either... */
6189 if (!reg
->in_g_packet
)
6192 store_registers_using_G (regcache
);
6196 store_registers_using_G (regcache
);
6198 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6199 if (!rsa
->regs
[i
].in_g_packet
)
6200 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
6201 /* See above for why we do not issue an error here. */
6206 /* Return the number of hex digits in num. */
6209 hexnumlen (ULONGEST num
)
6213 for (i
= 0; num
!= 0; i
++)
6219 /* Set BUF to the minimum number of hex digits representing NUM. */
6222 hexnumstr (char *buf
, ULONGEST num
)
6224 int len
= hexnumlen (num
);
6226 return hexnumnstr (buf
, num
, len
);
6230 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6233 hexnumnstr (char *buf
, ULONGEST num
, int width
)
6239 for (i
= width
- 1; i
>= 0; i
--)
6241 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
6248 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6251 remote_address_masked (CORE_ADDR addr
)
6253 int address_size
= remote_address_size
;
6255 /* If "remoteaddresssize" was not set, default to target address size. */
6257 address_size
= gdbarch_addr_bit (target_gdbarch
);
6259 if (address_size
> 0
6260 && address_size
< (sizeof (ULONGEST
) * 8))
6262 /* Only create a mask when that mask can safely be constructed
6263 in a ULONGEST variable. */
6266 mask
= (mask
<< address_size
) - 1;
6272 /* Convert BUFFER, binary data at least LEN bytes long, into escaped
6273 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
6274 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
6275 (which may be more than *OUT_LEN due to escape characters). The
6276 total number of bytes in the output buffer will be at most
6280 remote_escape_output (const gdb_byte
*buffer
, int len
,
6281 gdb_byte
*out_buf
, int *out_len
,
6284 int input_index
, output_index
;
6287 for (input_index
= 0; input_index
< len
; input_index
++)
6289 gdb_byte b
= buffer
[input_index
];
6291 if (b
== '$' || b
== '#' || b
== '}')
6293 /* These must be escaped. */
6294 if (output_index
+ 2 > out_maxlen
)
6296 out_buf
[output_index
++] = '}';
6297 out_buf
[output_index
++] = b
^ 0x20;
6301 if (output_index
+ 1 > out_maxlen
)
6303 out_buf
[output_index
++] = b
;
6307 *out_len
= input_index
;
6308 return output_index
;
6311 /* Convert BUFFER, escaped data LEN bytes long, into binary data
6312 in OUT_BUF. Return the number of bytes written to OUT_BUF.
6313 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
6315 This function reverses remote_escape_output. It allows more
6316 escaped characters than that function does, in particular because
6317 '*' must be escaped to avoid the run-length encoding processing
6318 in reading packets. */
6321 remote_unescape_input (const gdb_byte
*buffer
, int len
,
6322 gdb_byte
*out_buf
, int out_maxlen
)
6324 int input_index
, output_index
;
6329 for (input_index
= 0; input_index
< len
; input_index
++)
6331 gdb_byte b
= buffer
[input_index
];
6333 if (output_index
+ 1 > out_maxlen
)
6335 warning (_("Received too much data from remote target;"
6336 " ignoring overflow."));
6337 return output_index
;
6342 out_buf
[output_index
++] = b
^ 0x20;
6348 out_buf
[output_index
++] = b
;
6352 error (_("Unmatched escape character in target response."));
6354 return output_index
;
6357 /* Determine whether the remote target supports binary downloading.
6358 This is accomplished by sending a no-op memory write of zero length
6359 to the target at the specified address. It does not suffice to send
6360 the whole packet, since many stubs strip the eighth bit and
6361 subsequently compute a wrong checksum, which causes real havoc with
6364 NOTE: This can still lose if the serial line is not eight-bit
6365 clean. In cases like this, the user should clear "remote
6369 check_binary_download (CORE_ADDR addr
)
6371 struct remote_state
*rs
= get_remote_state ();
6373 switch (remote_protocol_packets
[PACKET_X
].support
)
6375 case PACKET_DISABLE
:
6379 case PACKET_SUPPORT_UNKNOWN
:
6385 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6387 p
+= hexnumstr (p
, (ULONGEST
) 0);
6391 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6392 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6394 if (rs
->buf
[0] == '\0')
6397 fprintf_unfiltered (gdb_stdlog
,
6398 "binary downloading NOT "
6399 "supported by target\n");
6400 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
6405 fprintf_unfiltered (gdb_stdlog
,
6406 "binary downloading supported by target\n");
6407 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
6414 /* Write memory data directly to the remote machine.
6415 This does not inform the data cache; the data cache uses this.
6416 HEADER is the starting part of the packet.
6417 MEMADDR is the address in the remote memory space.
6418 MYADDR is the address of the buffer in our space.
6419 LEN is the number of bytes.
6420 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6421 should send data as binary ('X'), or hex-encoded ('M').
6423 The function creates packet of the form
6424 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6426 where encoding of <DATA> is termined by PACKET_FORMAT.
6428 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6431 Returns the number of bytes transferred, or 0 (setting errno) for
6432 error. Only transfer a single packet. */
6435 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
6436 const gdb_byte
*myaddr
, int len
,
6437 char packet_format
, int use_length
)
6439 struct remote_state
*rs
= get_remote_state ();
6449 if (packet_format
!= 'X' && packet_format
!= 'M')
6450 internal_error (__FILE__
, __LINE__
,
6451 _("remote_write_bytes_aux: bad packet format"));
6456 payload_size
= get_memory_write_packet_size ();
6458 /* The packet buffer will be large enough for the payload;
6459 get_memory_packet_size ensures this. */
6462 /* Compute the size of the actual payload by subtracting out the
6463 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6465 payload_size
-= strlen ("$,:#NN");
6467 /* The comma won't be used. */
6469 header_length
= strlen (header
);
6470 payload_size
-= header_length
;
6471 payload_size
-= hexnumlen (memaddr
);
6473 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6475 strcat (rs
->buf
, header
);
6476 p
= rs
->buf
+ strlen (header
);
6478 /* Compute a best guess of the number of bytes actually transfered. */
6479 if (packet_format
== 'X')
6481 /* Best guess at number of bytes that will fit. */
6482 todo
= min (len
, payload_size
);
6484 payload_size
-= hexnumlen (todo
);
6485 todo
= min (todo
, payload_size
);
6489 /* Num bytes that will fit. */
6490 todo
= min (len
, payload_size
/ 2);
6492 payload_size
-= hexnumlen (todo
);
6493 todo
= min (todo
, payload_size
/ 2);
6497 internal_error (__FILE__
, __LINE__
,
6498 _("minimum packet size too small to write data"));
6500 /* If we already need another packet, then try to align the end
6501 of this packet to a useful boundary. */
6502 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
6503 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
6505 /* Append "<memaddr>". */
6506 memaddr
= remote_address_masked (memaddr
);
6507 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6514 /* Append <len>. Retain the location/size of <len>. It may need to
6515 be adjusted once the packet body has been created. */
6517 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
6525 /* Append the packet body. */
6526 if (packet_format
== 'X')
6528 /* Binary mode. Send target system values byte by byte, in
6529 increasing byte addresses. Only escape certain critical
6531 payload_length
= remote_escape_output (myaddr
, todo
, p
, &nr_bytes
,
6534 /* If not all TODO bytes fit, then we'll need another packet. Make
6535 a second try to keep the end of the packet aligned. Don't do
6536 this if the packet is tiny. */
6537 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
6541 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
6543 if (new_nr_bytes
!= nr_bytes
)
6544 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
6549 p
+= payload_length
;
6550 if (use_length
&& nr_bytes
< todo
)
6552 /* Escape chars have filled up the buffer prematurely,
6553 and we have actually sent fewer bytes than planned.
6554 Fix-up the length field of the packet. Use the same
6555 number of characters as before. */
6556 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
6557 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
6562 /* Normal mode: Send target system values byte by byte, in
6563 increasing byte addresses. Each byte is encoded as a two hex
6565 nr_bytes
= bin2hex (myaddr
, p
, todo
);
6569 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6570 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6572 if (rs
->buf
[0] == 'E')
6574 /* There is no correspondance between what the remote protocol
6575 uses for errors and errno codes. We would like a cleaner way
6576 of representing errors (big enough to include errno codes,
6577 bfd_error codes, and others). But for now just return EIO. */
6582 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6583 fewer bytes than we'd planned. */
6587 /* Write memory data directly to the remote machine.
6588 This does not inform the data cache; the data cache uses this.
6589 MEMADDR is the address in the remote memory space.
6590 MYADDR is the address of the buffer in our space.
6591 LEN is the number of bytes.
6593 Returns number of bytes transferred, or 0 (setting errno) for
6594 error. Only transfer a single packet. */
6597 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, int len
)
6599 char *packet_format
= 0;
6601 /* Check whether the target supports binary download. */
6602 check_binary_download (memaddr
);
6604 switch (remote_protocol_packets
[PACKET_X
].support
)
6607 packet_format
= "X";
6609 case PACKET_DISABLE
:
6610 packet_format
= "M";
6612 case PACKET_SUPPORT_UNKNOWN
:
6613 internal_error (__FILE__
, __LINE__
,
6614 _("remote_write_bytes: bad internal state"));
6616 internal_error (__FILE__
, __LINE__
, _("bad switch"));
6619 return remote_write_bytes_aux (packet_format
,
6620 memaddr
, myaddr
, len
, packet_format
[0], 1);
6623 /* Read memory data directly from the remote machine.
6624 This does not use the data cache; the data cache uses this.
6625 MEMADDR is the address in the remote memory space.
6626 MYADDR is the address of the buffer in our space.
6627 LEN is the number of bytes.
6629 Returns number of bytes transferred, or 0 for error. */
6632 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
6634 struct remote_state
*rs
= get_remote_state ();
6635 int max_buf_size
; /* Max size of packet output buffer. */
6643 max_buf_size
= get_memory_read_packet_size ();
6644 /* The packet buffer will be large enough for the payload;
6645 get_memory_packet_size ensures this. */
6647 /* Number if bytes that will fit. */
6648 todo
= min (len
, max_buf_size
/ 2);
6650 /* Construct "m"<memaddr>","<len>". */
6651 memaddr
= remote_address_masked (memaddr
);
6654 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6656 p
+= hexnumstr (p
, (ULONGEST
) todo
);
6659 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6660 if (rs
->buf
[0] == 'E'
6661 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
6662 && rs
->buf
[3] == '\0')
6664 /* There is no correspondance between what the remote protocol
6665 uses for errors and errno codes. We would like a cleaner way
6666 of representing errors (big enough to include errno codes,
6667 bfd_error codes, and others). But for now just return
6672 /* Reply describes memory byte by byte, each byte encoded as two hex
6675 i
= hex2bin (p
, myaddr
, todo
);
6676 /* Return what we have. Let higher layers handle partial reads. */
6681 /* Remote notification handler. */
6684 handle_notification (char *buf
, size_t length
)
6686 if (strncmp (buf
, "Stop:", 5) == 0)
6688 if (pending_stop_reply
)
6690 /* We've already parsed the in-flight stop-reply, but the
6691 stub for some reason thought we didn't, possibly due to
6692 timeout on its side. Just ignore it. */
6694 fprintf_unfiltered (gdb_stdlog
, "ignoring resent notification\n");
6698 struct cleanup
*old_chain
;
6699 struct stop_reply
*reply
= stop_reply_xmalloc ();
6701 old_chain
= make_cleanup (do_stop_reply_xfree
, reply
);
6703 remote_parse_stop_reply (buf
+ 5, reply
);
6705 discard_cleanups (old_chain
);
6707 /* Be careful to only set it after parsing, since an error
6708 may be thrown then. */
6709 pending_stop_reply
= reply
;
6711 /* Notify the event loop there's a stop reply to acknowledge
6712 and that there may be more events to fetch. */
6713 mark_async_event_handler (remote_async_get_pending_events_token
);
6716 fprintf_unfiltered (gdb_stdlog
, "stop notification captured\n");
6720 /* We ignore notifications we don't recognize, for compatibility
6721 with newer stubs. */
6726 /* Read or write LEN bytes from inferior memory at MEMADDR,
6727 transferring to or from debugger address BUFFER. Write to inferior
6728 if SHOULD_WRITE is nonzero. Returns length of data written or
6729 read; 0 for error. TARGET is unused. */
6732 remote_xfer_memory (CORE_ADDR mem_addr
, gdb_byte
*buffer
, int mem_len
,
6733 int should_write
, struct mem_attrib
*attrib
,
6734 struct target_ops
*target
)
6738 set_remote_traceframe ();
6739 set_general_thread (inferior_ptid
);
6742 res
= remote_write_bytes (mem_addr
, buffer
, mem_len
);
6744 res
= remote_read_bytes (mem_addr
, buffer
, mem_len
);
6749 /* Sends a packet with content determined by the printf format string
6750 FORMAT and the remaining arguments, then gets the reply. Returns
6751 whether the packet was a success, a failure, or unknown. */
6753 static enum packet_result
6754 remote_send_printf (const char *format
, ...)
6756 struct remote_state
*rs
= get_remote_state ();
6757 int max_size
= get_remote_packet_size ();
6760 va_start (ap
, format
);
6763 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
6764 internal_error (__FILE__
, __LINE__
, _("Too long remote packet."));
6766 if (putpkt (rs
->buf
) < 0)
6767 error (_("Communication problem with target."));
6770 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6772 return packet_check_result (rs
->buf
);
6776 restore_remote_timeout (void *p
)
6778 int value
= *(int *)p
;
6780 remote_timeout
= value
;
6783 /* Flash writing can take quite some time. We'll set
6784 effectively infinite timeout for flash operations.
6785 In future, we'll need to decide on a better approach. */
6786 static const int remote_flash_timeout
= 1000;
6789 remote_flash_erase (struct target_ops
*ops
,
6790 ULONGEST address
, LONGEST length
)
6792 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
6793 int saved_remote_timeout
= remote_timeout
;
6794 enum packet_result ret
;
6795 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6796 &saved_remote_timeout
);
6798 remote_timeout
= remote_flash_timeout
;
6800 ret
= remote_send_printf ("vFlashErase:%s,%s",
6801 phex (address
, addr_size
),
6805 case PACKET_UNKNOWN
:
6806 error (_("Remote target does not support flash erase"));
6808 error (_("Error erasing flash with vFlashErase packet"));
6813 do_cleanups (back_to
);
6817 remote_flash_write (struct target_ops
*ops
,
6818 ULONGEST address
, LONGEST length
,
6819 const gdb_byte
*data
)
6821 int saved_remote_timeout
= remote_timeout
;
6823 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6824 &saved_remote_timeout
);
6826 remote_timeout
= remote_flash_timeout
;
6827 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
, 'X', 0);
6828 do_cleanups (back_to
);
6834 remote_flash_done (struct target_ops
*ops
)
6836 int saved_remote_timeout
= remote_timeout
;
6838 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6839 &saved_remote_timeout
);
6841 remote_timeout
= remote_flash_timeout
;
6842 ret
= remote_send_printf ("vFlashDone");
6843 do_cleanups (back_to
);
6847 case PACKET_UNKNOWN
:
6848 error (_("Remote target does not support vFlashDone"));
6850 error (_("Error finishing flash operation"));
6857 remote_files_info (struct target_ops
*ignore
)
6859 puts_filtered ("Debugging a target over a serial line.\n");
6862 /* Stuff for dealing with the packets which are part of this protocol.
6863 See comment at top of file for details. */
6865 /* Read a single character from the remote end. */
6868 readchar (int timeout
)
6872 ch
= serial_readchar (remote_desc
, timeout
);
6877 switch ((enum serial_rc
) ch
)
6881 error (_("Remote connection closed"));
6885 perror_with_name (_("Remote communication error. "
6886 "Target disconnected."));
6888 case SERIAL_TIMEOUT
:
6894 /* Send the command in *BUF to the remote machine, and read the reply
6895 into *BUF. Report an error if we get an error reply. Resize
6896 *BUF using xrealloc if necessary to hold the result, and update
6900 remote_send (char **buf
,
6904 getpkt (buf
, sizeof_buf
, 0);
6906 if ((*buf
)[0] == 'E')
6907 error (_("Remote failure reply: %s"), *buf
);
6910 /* Return a pointer to an xmalloc'ed string representing an escaped
6911 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
6912 etc. The caller is responsible for releasing the returned
6916 escape_buffer (const char *buf
, int n
)
6918 struct cleanup
*old_chain
;
6919 struct ui_file
*stb
;
6922 stb
= mem_fileopen ();
6923 old_chain
= make_cleanup_ui_file_delete (stb
);
6925 fputstrn_unfiltered (buf
, n
, 0, stb
);
6926 str
= ui_file_xstrdup (stb
, NULL
);
6927 do_cleanups (old_chain
);
6931 /* Display a null-terminated packet on stdout, for debugging, using C
6935 print_packet (char *buf
)
6937 puts_filtered ("\"");
6938 fputstr_filtered (buf
, '"', gdb_stdout
);
6939 puts_filtered ("\"");
6945 return putpkt_binary (buf
, strlen (buf
));
6948 /* Send a packet to the remote machine, with error checking. The data
6949 of the packet is in BUF. The string in BUF can be at most
6950 get_remote_packet_size () - 5 to account for the $, # and checksum,
6951 and for a possible /0 if we are debugging (remote_debug) and want
6952 to print the sent packet as a string. */
6955 putpkt_binary (char *buf
, int cnt
)
6957 struct remote_state
*rs
= get_remote_state ();
6959 unsigned char csum
= 0;
6960 char *buf2
= alloca (cnt
+ 6);
6966 /* Catch cases like trying to read memory or listing threads while
6967 we're waiting for a stop reply. The remote server wouldn't be
6968 ready to handle this request, so we'd hang and timeout. We don't
6969 have to worry about this in synchronous mode, because in that
6970 case it's not possible to issue a command while the target is
6971 running. This is not a problem in non-stop mode, because in that
6972 case, the stub is always ready to process serial input. */
6973 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
6974 error (_("Cannot execute this command while the target is running."));
6976 /* We're sending out a new packet. Make sure we don't look at a
6977 stale cached response. */
6978 rs
->cached_wait_status
= 0;
6980 /* Copy the packet into buffer BUF2, encapsulating it
6981 and giving it a checksum. */
6986 for (i
= 0; i
< cnt
; i
++)
6992 *p
++ = tohex ((csum
>> 4) & 0xf);
6993 *p
++ = tohex (csum
& 0xf);
6995 /* Send it over and over until we get a positive ack. */
6999 int started_error_output
= 0;
7003 struct cleanup
*old_chain
;
7007 str
= escape_buffer (buf2
, p
- buf2
);
7008 old_chain
= make_cleanup (xfree
, str
);
7009 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", str
);
7010 gdb_flush (gdb_stdlog
);
7011 do_cleanups (old_chain
);
7013 if (serial_write (remote_desc
, buf2
, p
- buf2
))
7014 perror_with_name (_("putpkt: write failed"));
7016 /* If this is a no acks version of the remote protocol, send the
7017 packet and move on. */
7021 /* Read until either a timeout occurs (-2) or '+' is read.
7022 Handle any notification that arrives in the mean time. */
7025 ch
= readchar (remote_timeout
);
7033 case SERIAL_TIMEOUT
:
7036 if (started_error_output
)
7038 putchar_unfiltered ('\n');
7039 started_error_output
= 0;
7048 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
7052 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
7054 case SERIAL_TIMEOUT
:
7058 break; /* Retransmit buffer. */
7062 fprintf_unfiltered (gdb_stdlog
,
7063 "Packet instead of Ack, ignoring it\n");
7064 /* It's probably an old response sent because an ACK
7065 was lost. Gobble up the packet and ack it so it
7066 doesn't get retransmitted when we resend this
7069 serial_write (remote_desc
, "+", 1);
7070 continue; /* Now, go look for +. */
7077 /* If we got a notification, handle it, and go back to looking
7079 /* We've found the start of a notification. Now
7080 collect the data. */
7081 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
7086 struct cleanup
*old_chain
;
7089 str
= escape_buffer (rs
->buf
, val
);
7090 old_chain
= make_cleanup (xfree
, str
);
7091 fprintf_unfiltered (gdb_stdlog
,
7092 " Notification received: %s\n",
7094 do_cleanups (old_chain
);
7096 handle_notification (rs
->buf
, val
);
7097 /* We're in sync now, rewait for the ack. */
7104 if (!started_error_output
)
7106 started_error_output
= 1;
7107 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
7109 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
7110 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
7119 if (!started_error_output
)
7121 started_error_output
= 1;
7122 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
7124 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
7128 break; /* Here to retransmit. */
7132 /* This is wrong. If doing a long backtrace, the user should be
7133 able to get out next time we call QUIT, without anything as
7134 violent as interrupt_query. If we want to provide a way out of
7135 here without getting to the next QUIT, it should be based on
7136 hitting ^C twice as in remote_wait. */
7147 /* Come here after finding the start of a frame when we expected an
7148 ack. Do our best to discard the rest of this packet. */
7157 c
= readchar (remote_timeout
);
7160 case SERIAL_TIMEOUT
:
7161 /* Nothing we can do. */
7164 /* Discard the two bytes of checksum and stop. */
7165 c
= readchar (remote_timeout
);
7167 c
= readchar (remote_timeout
);
7170 case '*': /* Run length encoding. */
7171 /* Discard the repeat count. */
7172 c
= readchar (remote_timeout
);
7177 /* A regular character. */
7183 /* Come here after finding the start of the frame. Collect the rest
7184 into *BUF, verifying the checksum, length, and handling run-length
7185 compression. NUL terminate the buffer. If there is not enough room,
7186 expand *BUF using xrealloc.
7188 Returns -1 on error, number of characters in buffer (ignoring the
7189 trailing NULL) on success. (could be extended to return one of the
7190 SERIAL status indications). */
7193 read_frame (char **buf_p
,
7200 struct remote_state
*rs
= get_remote_state ();
7207 c
= readchar (remote_timeout
);
7210 case SERIAL_TIMEOUT
:
7212 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
7216 fputs_filtered ("Saw new packet start in middle of old one\n",
7218 return -1; /* Start a new packet, count retries. */
7221 unsigned char pktcsum
;
7227 check_0
= readchar (remote_timeout
);
7229 check_1
= readchar (remote_timeout
);
7231 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
7234 fputs_filtered ("Timeout in checksum, retrying\n",
7238 else if (check_0
< 0 || check_1
< 0)
7241 fputs_filtered ("Communication error in checksum\n",
7246 /* Don't recompute the checksum; with no ack packets we
7247 don't have any way to indicate a packet retransmission
7252 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
7253 if (csum
== pktcsum
)
7258 struct cleanup
*old_chain
;
7261 str
= escape_buffer (buf
, bc
);
7262 old_chain
= make_cleanup (xfree
, str
);
7263 fprintf_unfiltered (gdb_stdlog
,
7264 "Bad checksum, sentsum=0x%x, "
7265 "csum=0x%x, buf=%s\n",
7266 pktcsum
, csum
, str
);
7267 do_cleanups (old_chain
);
7269 /* Number of characters in buffer ignoring trailing
7273 case '*': /* Run length encoding. */
7278 c
= readchar (remote_timeout
);
7280 repeat
= c
- ' ' + 3; /* Compute repeat count. */
7282 /* The character before ``*'' is repeated. */
7284 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
7286 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
7288 /* Make some more room in the buffer. */
7289 *sizeof_buf
+= repeat
;
7290 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7294 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
7300 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
7304 if (bc
>= *sizeof_buf
- 1)
7306 /* Make some more room in the buffer. */
7308 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7319 /* Read a packet from the remote machine, with error checking, and
7320 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7321 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7322 rather than timing out; this is used (in synchronous mode) to wait
7323 for a target that is is executing user code to stop. */
7324 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7325 don't have to change all the calls to getpkt to deal with the
7326 return value, because at the moment I don't know what the right
7327 thing to do it for those. */
7335 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
7339 /* Read a packet from the remote machine, with error checking, and
7340 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7341 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7342 rather than timing out; this is used (in synchronous mode) to wait
7343 for a target that is is executing user code to stop. If FOREVER ==
7344 0, this function is allowed to time out gracefully and return an
7345 indication of this to the caller. Otherwise return the number of
7346 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7347 enough reason to return to the caller. */
7350 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
7351 int expecting_notif
)
7353 struct remote_state
*rs
= get_remote_state ();
7359 /* We're reading a new response. Make sure we don't look at a
7360 previously cached response. */
7361 rs
->cached_wait_status
= 0;
7363 strcpy (*buf
, "timeout");
7366 timeout
= watchdog
> 0 ? watchdog
: -1;
7367 else if (expecting_notif
)
7368 timeout
= 0; /* There should already be a char in the buffer. If
7371 timeout
= remote_timeout
;
7375 /* Process any number of notifications, and then return when
7379 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
7381 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
7383 /* This can loop forever if the remote side sends us
7384 characters continuously, but if it pauses, we'll get
7385 SERIAL_TIMEOUT from readchar because of timeout. Then
7386 we'll count that as a retry.
7388 Note that even when forever is set, we will only wait
7389 forever prior to the start of a packet. After that, we
7390 expect characters to arrive at a brisk pace. They should
7391 show up within remote_timeout intervals. */
7393 c
= readchar (timeout
);
7394 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
7396 if (c
== SERIAL_TIMEOUT
)
7398 if (expecting_notif
)
7399 return -1; /* Don't complain, it's normal to not get
7400 anything in this case. */
7402 if (forever
) /* Watchdog went off? Kill the target. */
7406 error (_("Watchdog timeout has expired. Target detached."));
7409 fputs_filtered ("Timed out.\n", gdb_stdlog
);
7413 /* We've found the start of a packet or notification.
7414 Now collect the data. */
7415 val
= read_frame (buf
, sizeof_buf
);
7420 serial_write (remote_desc
, "-", 1);
7423 if (tries
> MAX_TRIES
)
7425 /* We have tried hard enough, and just can't receive the
7426 packet/notification. Give up. */
7427 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7429 /* Skip the ack char if we're in no-ack mode. */
7430 if (!rs
->noack_mode
)
7431 serial_write (remote_desc
, "+", 1);
7435 /* If we got an ordinary packet, return that to our caller. */
7440 struct cleanup
*old_chain
;
7443 str
= escape_buffer (*buf
, val
);
7444 old_chain
= make_cleanup (xfree
, str
);
7445 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", str
);
7446 do_cleanups (old_chain
);
7449 /* Skip the ack char if we're in no-ack mode. */
7450 if (!rs
->noack_mode
)
7451 serial_write (remote_desc
, "+", 1);
7455 /* If we got a notification, handle it, and go back to looking
7459 gdb_assert (c
== '%');
7463 struct cleanup
*old_chain
;
7466 str
= escape_buffer (*buf
, val
);
7467 old_chain
= make_cleanup (xfree
, str
);
7468 fprintf_unfiltered (gdb_stdlog
,
7469 " Notification received: %s\n",
7471 do_cleanups (old_chain
);
7474 handle_notification (*buf
, val
);
7476 /* Notifications require no acknowledgement. */
7478 if (expecting_notif
)
7485 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
7487 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0);
7491 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
)
7493 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1);
7497 /* A helper function that just calls putpkt; for type correctness. */
7500 putpkt_for_catch_errors (void *arg
)
7502 return putpkt (arg
);
7506 remote_kill (struct target_ops
*ops
)
7508 /* Use catch_errors so the user can quit from gdb even when we
7509 aren't on speaking terms with the remote system. */
7510 catch_errors (putpkt_for_catch_errors
, "k", "", RETURN_MASK_ERROR
);
7512 /* Don't wait for it to die. I'm not really sure it matters whether
7513 we do or not. For the existing stubs, kill is a noop. */
7514 target_mourn_inferior ();
7518 remote_vkill (int pid
, struct remote_state
*rs
)
7520 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7523 /* Tell the remote target to detach. */
7524 sprintf (rs
->buf
, "vKill;%x", pid
);
7526 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7528 if (packet_ok (rs
->buf
,
7529 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
7531 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7538 extended_remote_kill (struct target_ops
*ops
)
7541 int pid
= ptid_get_pid (inferior_ptid
);
7542 struct remote_state
*rs
= get_remote_state ();
7544 res
= remote_vkill (pid
, rs
);
7545 if (res
== -1 && !(rs
->extended
&& remote_multi_process_p (rs
)))
7547 /* Don't try 'k' on a multi-process aware stub -- it has no way
7548 to specify the pid. */
7552 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7553 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
7556 /* Don't wait for it to die. I'm not really sure it matters whether
7557 we do or not. For the existing stubs, kill is a noop. */
7563 error (_("Can't kill process"));
7565 target_mourn_inferior ();
7569 remote_mourn (struct target_ops
*ops
)
7571 remote_mourn_1 (ops
);
7574 /* Worker function for remote_mourn. */
7576 remote_mourn_1 (struct target_ops
*target
)
7578 unpush_target (target
);
7580 /* remote_close takes care of doing most of the clean up. */
7581 generic_mourn_inferior ();
7585 extended_remote_mourn_1 (struct target_ops
*target
)
7587 struct remote_state
*rs
= get_remote_state ();
7589 /* In case we got here due to an error, but we're going to stay
7591 rs
->waiting_for_stop_reply
= 0;
7593 /* We're no longer interested in these events. */
7594 discard_pending_stop_replies (ptid_get_pid (inferior_ptid
));
7596 /* If the current general thread belonged to the process we just
7597 detached from or has exited, the remote side current general
7598 thread becomes undefined. Considering a case like this:
7600 - We just got here due to a detach.
7601 - The process that we're detaching from happens to immediately
7602 report a global breakpoint being hit in non-stop mode, in the
7603 same thread we had selected before.
7604 - GDB attaches to this process again.
7605 - This event happens to be the next event we handle.
7607 GDB would consider that the current general thread didn't need to
7608 be set on the stub side (with Hg), since for all it knew,
7609 GENERAL_THREAD hadn't changed.
7611 Notice that although in all-stop mode, the remote server always
7612 sets the current thread to the thread reporting the stop event,
7613 that doesn't happen in non-stop mode; in non-stop, the stub *must
7614 not* change the current thread when reporting a breakpoint hit,
7615 due to the decoupling of event reporting and event handling.
7617 To keep things simple, we always invalidate our notion of the
7619 record_currthread (minus_one_ptid
);
7621 /* Unlike "target remote", we do not want to unpush the target; then
7622 the next time the user says "run", we won't be connected. */
7624 /* Call common code to mark the inferior as not running. */
7625 generic_mourn_inferior ();
7627 if (!have_inferiors ())
7629 if (!remote_multi_process_p (rs
))
7631 /* Check whether the target is running now - some remote stubs
7632 automatically restart after kill. */
7634 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7636 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
7638 /* Assume that the target has been restarted. Set
7639 inferior_ptid so that bits of core GDB realizes
7640 there's something here, e.g., so that the user can
7641 say "kill" again. */
7642 inferior_ptid
= magic_null_ptid
;
7649 extended_remote_mourn (struct target_ops
*ops
)
7651 extended_remote_mourn_1 (ops
);
7655 extended_remote_supports_disable_randomization (void)
7657 return (remote_protocol_packets
[PACKET_QDisableRandomization
].support
7662 extended_remote_disable_randomization (int val
)
7664 struct remote_state
*rs
= get_remote_state ();
7667 sprintf (rs
->buf
, "QDisableRandomization:%x", val
);
7669 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
7671 error (_("Target does not support QDisableRandomization."));
7672 if (strcmp (reply
, "OK") != 0)
7673 error (_("Bogus QDisableRandomization reply from target: %s"), reply
);
7677 extended_remote_run (char *args
)
7679 struct remote_state
*rs
= get_remote_state ();
7682 /* If the user has disabled vRun support, or we have detected that
7683 support is not available, do not try it. */
7684 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7687 strcpy (rs
->buf
, "vRun;");
7688 len
= strlen (rs
->buf
);
7690 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
7691 error (_("Remote file name too long for run packet"));
7692 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
, 0);
7694 gdb_assert (args
!= NULL
);
7697 struct cleanup
*back_to
;
7701 argv
= gdb_buildargv (args
);
7702 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
7703 for (i
= 0; argv
[i
] != NULL
; i
++)
7705 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
7706 error (_("Argument list too long for run packet"));
7707 rs
->buf
[len
++] = ';';
7708 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
, 0);
7710 do_cleanups (back_to
);
7713 rs
->buf
[len
++] = '\0';
7716 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7718 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
7720 /* We have a wait response; we don't need it, though. All is well. */
7723 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7724 /* It wasn't disabled before, but it is now. */
7728 if (remote_exec_file
[0] == '\0')
7729 error (_("Running the default executable on the remote target failed; "
7730 "try \"set remote exec-file\"?"));
7732 error (_("Running \"%s\" on the remote target failed"),
7737 /* In the extended protocol we want to be able to do things like
7738 "run" and have them basically work as expected. So we need
7739 a special create_inferior function. We support changing the
7740 executable file and the command line arguments, but not the
7744 extended_remote_create_inferior_1 (char *exec_file
, char *args
,
7745 char **env
, int from_tty
)
7747 /* If running asynchronously, register the target file descriptor
7748 with the event loop. */
7749 if (target_can_async_p ())
7750 target_async (inferior_event_handler
, 0);
7752 /* Disable address space randomization if requested (and supported). */
7753 if (extended_remote_supports_disable_randomization ())
7754 extended_remote_disable_randomization (disable_randomization
);
7756 /* Now restart the remote server. */
7757 if (extended_remote_run (args
) == -1)
7759 /* vRun was not supported. Fail if we need it to do what the
7761 if (remote_exec_file
[0])
7762 error (_("Remote target does not support \"set remote exec-file\""));
7764 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7766 /* Fall back to "R". */
7767 extended_remote_restart ();
7770 if (!have_inferiors ())
7772 /* Clean up from the last time we ran, before we mark the target
7773 running again. This will mark breakpoints uninserted, and
7774 get_offsets may insert breakpoints. */
7775 init_thread_list ();
7776 init_wait_for_inferior ();
7779 add_current_inferior_and_thread ();
7781 /* Get updated offsets, if the stub uses qOffsets. */
7786 extended_remote_create_inferior (struct target_ops
*ops
,
7787 char *exec_file
, char *args
,
7788 char **env
, int from_tty
)
7790 extended_remote_create_inferior_1 (exec_file
, args
, env
, from_tty
);
7794 /* Given a location's target info BP_TGT and the packet buffer BUF, output
7795 the list of conditions (in agent expression bytecode format), if any, the
7796 target needs to evaluate. The output is placed into the packet buffer
7800 remote_add_target_side_condition (struct gdbarch
*gdbarch
,
7801 struct bp_target_info
*bp_tgt
, char *buf
)
7803 struct agent_expr
*aexpr
= NULL
;
7806 char *buf_start
= buf
;
7808 if (VEC_empty (agent_expr_p
, bp_tgt
->conditions
))
7811 buf
+= strlen (buf
);
7812 sprintf (buf
, "%s", ";");
7815 /* Send conditions to the target and free the vector. */
7817 VEC_iterate (agent_expr_p
, bp_tgt
->conditions
, ix
, aexpr
);
7820 sprintf (buf
, "X%x,", aexpr
->len
);
7821 buf
+= strlen (buf
);
7822 for (i
= 0; i
< aexpr
->len
; ++i
)
7823 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
7827 VEC_free (agent_expr_p
, bp_tgt
->conditions
);
7831 /* Insert a breakpoint. On targets that have software breakpoint
7832 support, we ask the remote target to do the work; on targets
7833 which don't, we insert a traditional memory breakpoint. */
7836 remote_insert_breakpoint (struct gdbarch
*gdbarch
,
7837 struct bp_target_info
*bp_tgt
)
7839 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
7840 If it succeeds, then set the support to PACKET_ENABLE. If it
7841 fails, and the user has explicitly requested the Z support then
7842 report an error, otherwise, mark it disabled and go on. */
7844 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
7846 CORE_ADDR addr
= bp_tgt
->placed_address
;
7847 struct remote_state
*rs
;
7850 struct condition_list
*cond
= NULL
;
7852 gdbarch_remote_breakpoint_from_pc (gdbarch
, &addr
, &bpsize
);
7854 rs
= get_remote_state ();
7860 addr
= (ULONGEST
) remote_address_masked (addr
);
7861 p
+= hexnumstr (p
, addr
);
7862 sprintf (p
, ",%d", bpsize
);
7864 if (remote_supports_cond_breakpoints ())
7865 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
);
7868 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7870 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
7875 bp_tgt
->placed_address
= addr
;
7876 bp_tgt
->placed_size
= bpsize
;
7878 case PACKET_UNKNOWN
:
7883 return memory_insert_breakpoint (gdbarch
, bp_tgt
);
7887 remote_remove_breakpoint (struct gdbarch
*gdbarch
,
7888 struct bp_target_info
*bp_tgt
)
7890 CORE_ADDR addr
= bp_tgt
->placed_address
;
7891 struct remote_state
*rs
= get_remote_state ();
7893 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
7901 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
7902 p
+= hexnumstr (p
, addr
);
7903 sprintf (p
, ",%d", bp_tgt
->placed_size
);
7906 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7908 return (rs
->buf
[0] == 'E');
7911 return memory_remove_breakpoint (gdbarch
, bp_tgt
);
7915 watchpoint_to_Z_packet (int type
)
7920 return Z_PACKET_WRITE_WP
;
7923 return Z_PACKET_READ_WP
;
7926 return Z_PACKET_ACCESS_WP
;
7929 internal_error (__FILE__
, __LINE__
,
7930 _("hw_bp_to_z: bad watchpoint type %d"), type
);
7935 remote_insert_watchpoint (CORE_ADDR addr
, int len
, int type
,
7936 struct expression
*cond
)
7938 struct remote_state
*rs
= get_remote_state ();
7940 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
7942 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
7945 sprintf (rs
->buf
, "Z%x,", packet
);
7946 p
= strchr (rs
->buf
, '\0');
7947 addr
= remote_address_masked (addr
);
7948 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7949 sprintf (p
, ",%x", len
);
7952 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7954 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
7958 case PACKET_UNKNOWN
:
7963 internal_error (__FILE__
, __LINE__
,
7964 _("remote_insert_watchpoint: reached end of function"));
7968 remote_watchpoint_addr_within_range (struct target_ops
*target
, CORE_ADDR addr
,
7969 CORE_ADDR start
, int length
)
7971 CORE_ADDR diff
= remote_address_masked (addr
- start
);
7973 return diff
< length
;
7978 remote_remove_watchpoint (CORE_ADDR addr
, int len
, int type
,
7979 struct expression
*cond
)
7981 struct remote_state
*rs
= get_remote_state ();
7983 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
7985 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
7988 sprintf (rs
->buf
, "z%x,", packet
);
7989 p
= strchr (rs
->buf
, '\0');
7990 addr
= remote_address_masked (addr
);
7991 p
+= hexnumstr (p
, (ULONGEST
) addr
);
7992 sprintf (p
, ",%x", len
);
7994 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7996 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
7999 case PACKET_UNKNOWN
:
8004 internal_error (__FILE__
, __LINE__
,
8005 _("remote_remove_watchpoint: reached end of function"));
8009 int remote_hw_watchpoint_limit
= -1;
8010 int remote_hw_watchpoint_length_limit
= -1;
8011 int remote_hw_breakpoint_limit
= -1;
8014 remote_region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
8016 if (remote_hw_watchpoint_length_limit
== 0)
8018 else if (remote_hw_watchpoint_length_limit
< 0)
8020 else if (len
<= remote_hw_watchpoint_length_limit
)
8027 remote_check_watch_resources (int type
, int cnt
, int ot
)
8029 if (type
== bp_hardware_breakpoint
)
8031 if (remote_hw_breakpoint_limit
== 0)
8033 else if (remote_hw_breakpoint_limit
< 0)
8035 else if (cnt
<= remote_hw_breakpoint_limit
)
8040 if (remote_hw_watchpoint_limit
== 0)
8042 else if (remote_hw_watchpoint_limit
< 0)
8046 else if (cnt
<= remote_hw_watchpoint_limit
)
8053 remote_stopped_by_watchpoint (void)
8055 return remote_stopped_by_watchpoint_p
;
8059 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
8063 if (remote_stopped_by_watchpoint ())
8065 *addr_p
= remote_watch_data_address
;
8074 remote_insert_hw_breakpoint (struct gdbarch
*gdbarch
,
8075 struct bp_target_info
*bp_tgt
)
8078 struct remote_state
*rs
;
8081 /* The length field should be set to the size of a breakpoint
8082 instruction, even though we aren't inserting one ourselves. */
8084 gdbarch_remote_breakpoint_from_pc
8085 (gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
8087 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
8090 rs
= get_remote_state ();
8097 addr
= remote_address_masked (bp_tgt
->placed_address
);
8098 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8099 sprintf (p
, ",%x", bp_tgt
->placed_size
);
8101 if (remote_supports_cond_breakpoints ())
8102 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
);
8105 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8107 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
8110 case PACKET_UNKNOWN
:
8115 internal_error (__FILE__
, __LINE__
,
8116 _("remote_insert_hw_breakpoint: reached end of function"));
8121 remote_remove_hw_breakpoint (struct gdbarch
*gdbarch
,
8122 struct bp_target_info
*bp_tgt
)
8125 struct remote_state
*rs
= get_remote_state ();
8128 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
8135 addr
= remote_address_masked (bp_tgt
->placed_address
);
8136 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8137 sprintf (p
, ",%x", bp_tgt
->placed_size
);
8140 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8142 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
8145 case PACKET_UNKNOWN
:
8150 internal_error (__FILE__
, __LINE__
,
8151 _("remote_remove_hw_breakpoint: reached end of function"));
8154 /* Table used by the crc32 function to calcuate the checksum. */
8156 static unsigned long crc32_table
[256] =
8159 static unsigned long
8160 crc32 (const unsigned char *buf
, int len
, unsigned int crc
)
8162 if (!crc32_table
[1])
8164 /* Initialize the CRC table and the decoding table. */
8168 for (i
= 0; i
< 256; i
++)
8170 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
8171 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
8178 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
8184 /* Verify memory using the "qCRC:" request. */
8187 remote_verify_memory (struct target_ops
*ops
,
8188 const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
8190 struct remote_state
*rs
= get_remote_state ();
8191 unsigned long host_crc
, target_crc
;
8194 /* FIXME: assumes lma can fit into long. */
8195 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
8196 (long) lma
, (long) size
);
8199 /* Be clever; compute the host_crc before waiting for target
8201 host_crc
= crc32 (data
, size
, 0xffffffff);
8203 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8204 if (rs
->buf
[0] == 'E')
8207 if (rs
->buf
[0] != 'C')
8208 error (_("remote target does not support this operation"));
8210 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
8211 target_crc
= target_crc
* 16 + fromhex (*tmp
);
8213 return (host_crc
== target_crc
);
8216 /* compare-sections command
8218 With no arguments, compares each loadable section in the exec bfd
8219 with the same memory range on the target, and reports mismatches.
8220 Useful for verifying the image on the target against the exec file. */
8223 compare_sections_command (char *args
, int from_tty
)
8226 struct cleanup
*old_chain
;
8228 const char *sectname
;
8236 error (_("command cannot be used without an exec file"));
8238 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
8240 if (!(s
->flags
& SEC_LOAD
))
8241 continue; /* Skip non-loadable section. */
8243 size
= bfd_get_section_size (s
);
8245 continue; /* Skip zero-length section. */
8247 sectname
= bfd_get_section_name (exec_bfd
, s
);
8248 if (args
&& strcmp (args
, sectname
) != 0)
8249 continue; /* Not the section selected by user. */
8251 matched
= 1; /* Do this section. */
8254 sectdata
= xmalloc (size
);
8255 old_chain
= make_cleanup (xfree
, sectdata
);
8256 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
8258 res
= target_verify_memory (sectdata
, lma
, size
);
8261 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
8262 paddress (target_gdbarch
, lma
),
8263 paddress (target_gdbarch
, lma
+ size
));
8265 printf_filtered ("Section %s, range %s -- %s: ", sectname
,
8266 paddress (target_gdbarch
, lma
),
8267 paddress (target_gdbarch
, lma
+ size
));
8269 printf_filtered ("matched.\n");
8272 printf_filtered ("MIS-MATCHED!\n");
8276 do_cleanups (old_chain
);
8279 warning (_("One or more sections of the remote executable does not match\n\
8280 the loaded file\n"));
8281 if (args
&& !matched
)
8282 printf_filtered (_("No loaded section named '%s'.\n"), args
);
8285 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
8286 into remote target. The number of bytes written to the remote
8287 target is returned, or -1 for error. */
8290 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
8291 const char *annex
, const gdb_byte
*writebuf
,
8292 ULONGEST offset
, LONGEST len
,
8293 struct packet_config
*packet
)
8297 struct remote_state
*rs
= get_remote_state ();
8298 int max_size
= get_memory_write_packet_size ();
8300 if (packet
->support
== PACKET_DISABLE
)
8303 /* Insert header. */
8304 i
= snprintf (rs
->buf
, max_size
,
8305 "qXfer:%s:write:%s:%s:",
8306 object_name
, annex
? annex
: "",
8307 phex_nz (offset
, sizeof offset
));
8308 max_size
-= (i
+ 1);
8310 /* Escape as much data as fits into rs->buf. */
8311 buf_len
= remote_escape_output
8312 (writebuf
, len
, (rs
->buf
+ i
), &max_size
, max_size
);
8314 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
8315 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8316 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8319 unpack_varlen_hex (rs
->buf
, &n
);
8323 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8324 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8325 number of bytes read is returned, or 0 for EOF, or -1 for error.
8326 The number of bytes read may be less than LEN without indicating an
8327 EOF. PACKET is checked and updated to indicate whether the remote
8328 target supports this object. */
8331 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
8333 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
8334 struct packet_config
*packet
)
8336 static char *finished_object
;
8337 static char *finished_annex
;
8338 static ULONGEST finished_offset
;
8340 struct remote_state
*rs
= get_remote_state ();
8341 LONGEST i
, n
, packet_len
;
8343 if (packet
->support
== PACKET_DISABLE
)
8346 /* Check whether we've cached an end-of-object packet that matches
8348 if (finished_object
)
8350 if (strcmp (object_name
, finished_object
) == 0
8351 && strcmp (annex
? annex
: "", finished_annex
) == 0
8352 && offset
== finished_offset
)
8355 /* Otherwise, we're now reading something different. Discard
8357 xfree (finished_object
);
8358 xfree (finished_annex
);
8359 finished_object
= NULL
;
8360 finished_annex
= NULL
;
8363 /* Request only enough to fit in a single packet. The actual data
8364 may not, since we don't know how much of it will need to be escaped;
8365 the target is free to respond with slightly less data. We subtract
8366 five to account for the response type and the protocol frame. */
8367 n
= min (get_remote_packet_size () - 5, len
);
8368 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8369 object_name
, annex
? annex
: "",
8370 phex_nz (offset
, sizeof offset
),
8371 phex_nz (n
, sizeof n
));
8372 i
= putpkt (rs
->buf
);
8377 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
8378 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8381 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
8382 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
8384 /* 'm' means there is (or at least might be) more data after this
8385 batch. That does not make sense unless there's at least one byte
8386 of data in this reply. */
8387 if (rs
->buf
[0] == 'm' && packet_len
== 1)
8388 error (_("Remote qXfer reply contained no data."));
8390 /* Got some data. */
8391 i
= remote_unescape_input (rs
->buf
+ 1, packet_len
- 1, readbuf
, n
);
8393 /* 'l' is an EOF marker, possibly including a final block of data,
8394 or possibly empty. If we have the final block of a non-empty
8395 object, record this fact to bypass a subsequent partial read. */
8396 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
8398 finished_object
= xstrdup (object_name
);
8399 finished_annex
= xstrdup (annex
? annex
: "");
8400 finished_offset
= offset
+ i
;
8407 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
8408 const char *annex
, gdb_byte
*readbuf
,
8409 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
8411 struct remote_state
*rs
;
8416 set_remote_traceframe ();
8417 set_general_thread (inferior_ptid
);
8419 rs
= get_remote_state ();
8421 /* Handle memory using the standard memory routines. */
8422 if (object
== TARGET_OBJECT_MEMORY
)
8428 /* If the remote target is connected but not running, we should
8429 pass this request down to a lower stratum (e.g. the executable
8431 if (!target_has_execution
)
8434 if (writebuf
!= NULL
)
8435 xfered
= remote_write_bytes (offset
, writebuf
, len
);
8437 xfered
= remote_read_bytes (offset
, readbuf
, len
);
8441 else if (xfered
== 0 && errno
== 0)
8447 /* Handle SPU memory using qxfer packets. */
8448 if (object
== TARGET_OBJECT_SPU
)
8451 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
8452 &remote_protocol_packets
8453 [PACKET_qXfer_spu_read
]);
8455 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
8456 &remote_protocol_packets
8457 [PACKET_qXfer_spu_write
]);
8460 /* Handle extra signal info using qxfer packets. */
8461 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
8464 return remote_read_qxfer (ops
, "siginfo", annex
, readbuf
, offset
, len
,
8465 &remote_protocol_packets
8466 [PACKET_qXfer_siginfo_read
]);
8468 return remote_write_qxfer (ops
, "siginfo", annex
,
8469 writebuf
, offset
, len
,
8470 &remote_protocol_packets
8471 [PACKET_qXfer_siginfo_write
]);
8474 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
8477 return remote_read_qxfer (ops
, "statictrace", annex
,
8478 readbuf
, offset
, len
,
8479 &remote_protocol_packets
8480 [PACKET_qXfer_statictrace_read
]);
8485 /* Only handle flash writes. */
8486 if (writebuf
!= NULL
)
8492 case TARGET_OBJECT_FLASH
:
8493 xfered
= remote_flash_write (ops
, offset
, len
, writebuf
);
8497 else if (xfered
== 0 && errno
== 0)
8507 /* Map pre-existing objects onto letters. DO NOT do this for new
8508 objects!!! Instead specify new query packets. */
8511 case TARGET_OBJECT_AVR
:
8515 case TARGET_OBJECT_AUXV
:
8516 gdb_assert (annex
== NULL
);
8517 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
8518 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
8520 case TARGET_OBJECT_AVAILABLE_FEATURES
:
8521 return remote_read_qxfer
8522 (ops
, "features", annex
, readbuf
, offset
, len
,
8523 &remote_protocol_packets
[PACKET_qXfer_features
]);
8525 case TARGET_OBJECT_LIBRARIES
:
8526 return remote_read_qxfer
8527 (ops
, "libraries", annex
, readbuf
, offset
, len
,
8528 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
8530 case TARGET_OBJECT_LIBRARIES_SVR4
:
8531 return remote_read_qxfer
8532 (ops
, "libraries-svr4", annex
, readbuf
, offset
, len
,
8533 &remote_protocol_packets
[PACKET_qXfer_libraries_svr4
]);
8535 case TARGET_OBJECT_MEMORY_MAP
:
8536 gdb_assert (annex
== NULL
);
8537 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
8538 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
8540 case TARGET_OBJECT_OSDATA
:
8541 /* Should only get here if we're connected. */
8542 gdb_assert (remote_desc
);
8543 return remote_read_qxfer
8544 (ops
, "osdata", annex
, readbuf
, offset
, len
,
8545 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
8547 case TARGET_OBJECT_THREADS
:
8548 gdb_assert (annex
== NULL
);
8549 return remote_read_qxfer (ops
, "threads", annex
, readbuf
, offset
, len
,
8550 &remote_protocol_packets
[PACKET_qXfer_threads
]);
8552 case TARGET_OBJECT_TRACEFRAME_INFO
:
8553 gdb_assert (annex
== NULL
);
8554 return remote_read_qxfer
8555 (ops
, "traceframe-info", annex
, readbuf
, offset
, len
,
8556 &remote_protocol_packets
[PACKET_qXfer_traceframe_info
]);
8558 case TARGET_OBJECT_FDPIC
:
8559 return remote_read_qxfer (ops
, "fdpic", annex
, readbuf
, offset
, len
,
8560 &remote_protocol_packets
[PACKET_qXfer_fdpic
]);
8562 case TARGET_OBJECT_OPENVMS_UIB
:
8563 return remote_read_qxfer (ops
, "uib", annex
, readbuf
, offset
, len
,
8564 &remote_protocol_packets
[PACKET_qXfer_uib
]);
8570 /* Note: a zero OFFSET and LEN can be used to query the minimum
8572 if (offset
== 0 && len
== 0)
8573 return (get_remote_packet_size ());
8574 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8575 large enough let the caller deal with it. */
8576 if (len
< get_remote_packet_size ())
8578 len
= get_remote_packet_size ();
8580 /* Except for querying the minimum buffer size, target must be open. */
8582 error (_("remote query is only available after target open"));
8584 gdb_assert (annex
!= NULL
);
8585 gdb_assert (readbuf
!= NULL
);
8591 /* We used one buffer char for the remote protocol q command and
8592 another for the query type. As the remote protocol encapsulation
8593 uses 4 chars plus one extra in case we are debugging
8594 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8597 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
8599 /* Bad caller may have sent forbidden characters. */
8600 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
8605 gdb_assert (annex
[i
] == '\0');
8607 i
= putpkt (rs
->buf
);
8611 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8612 strcpy ((char *) readbuf
, rs
->buf
);
8614 return strlen ((char *) readbuf
);
8618 remote_search_memory (struct target_ops
* ops
,
8619 CORE_ADDR start_addr
, ULONGEST search_space_len
,
8620 const gdb_byte
*pattern
, ULONGEST pattern_len
,
8621 CORE_ADDR
*found_addrp
)
8623 int addr_size
= gdbarch_addr_bit (target_gdbarch
) / 8;
8624 struct remote_state
*rs
= get_remote_state ();
8625 int max_size
= get_memory_write_packet_size ();
8626 struct packet_config
*packet
=
8627 &remote_protocol_packets
[PACKET_qSearch_memory
];
8628 /* Number of packet bytes used to encode the pattern;
8629 this could be more than PATTERN_LEN due to escape characters. */
8630 int escaped_pattern_len
;
8631 /* Amount of pattern that was encodable in the packet. */
8632 int used_pattern_len
;
8635 ULONGEST found_addr
;
8637 /* Don't go to the target if we don't have to.
8638 This is done before checking packet->support to avoid the possibility that
8639 a success for this edge case means the facility works in general. */
8640 if (pattern_len
> search_space_len
)
8642 if (pattern_len
== 0)
8644 *found_addrp
= start_addr
;
8648 /* If we already know the packet isn't supported, fall back to the simple
8649 way of searching memory. */
8651 if (packet
->support
== PACKET_DISABLE
)
8653 /* Target doesn't provided special support, fall back and use the
8654 standard support (copy memory and do the search here). */
8655 return simple_search_memory (ops
, start_addr
, search_space_len
,
8656 pattern
, pattern_len
, found_addrp
);
8659 /* Insert header. */
8660 i
= snprintf (rs
->buf
, max_size
,
8661 "qSearch:memory:%s;%s;",
8662 phex_nz (start_addr
, addr_size
),
8663 phex_nz (search_space_len
, sizeof (search_space_len
)));
8664 max_size
-= (i
+ 1);
8666 /* Escape as much data as fits into rs->buf. */
8667 escaped_pattern_len
=
8668 remote_escape_output (pattern
, pattern_len
, (rs
->buf
+ i
),
8669 &used_pattern_len
, max_size
);
8671 /* Bail if the pattern is too large. */
8672 if (used_pattern_len
!= pattern_len
)
8673 error (_("Pattern is too large to transmit to remote target."));
8675 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
8676 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8677 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8679 /* The request may not have worked because the command is not
8680 supported. If so, fall back to the simple way. */
8681 if (packet
->support
== PACKET_DISABLE
)
8683 return simple_search_memory (ops
, start_addr
, search_space_len
,
8684 pattern
, pattern_len
, found_addrp
);
8689 if (rs
->buf
[0] == '0')
8691 else if (rs
->buf
[0] == '1')
8694 if (rs
->buf
[1] != ',')
8695 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8696 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
8697 *found_addrp
= found_addr
;
8700 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8706 remote_rcmd (char *command
,
8707 struct ui_file
*outbuf
)
8709 struct remote_state
*rs
= get_remote_state ();
8713 error (_("remote rcmd is only available after target open"));
8715 /* Send a NULL command across as an empty command. */
8716 if (command
== NULL
)
8719 /* The query prefix. */
8720 strcpy (rs
->buf
, "qRcmd,");
8721 p
= strchr (rs
->buf
, '\0');
8723 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/)
8724 > get_remote_packet_size ())
8725 error (_("\"monitor\" command ``%s'' is too long."), command
);
8727 /* Encode the actual command. */
8728 bin2hex ((gdb_byte
*) command
, p
, 0);
8730 if (putpkt (rs
->buf
) < 0)
8731 error (_("Communication problem with target."));
8733 /* get/display the response */
8738 /* XXX - see also remote_get_noisy_reply(). */
8739 QUIT
; /* Allow user to bail out with ^C. */
8741 if (getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) == -1)
8743 /* Timeout. Continue to (try to) read responses.
8744 This is better than stopping with an error, assuming the stub
8745 is still executing the (long) monitor command.
8746 If needed, the user can interrupt gdb using C-c, obtaining
8747 an effect similar to stop on timeout. */
8752 error (_("Target does not support this command."));
8753 if (buf
[0] == 'O' && buf
[1] != 'K')
8755 remote_console_output (buf
+ 1); /* 'O' message from stub. */
8758 if (strcmp (buf
, "OK") == 0)
8760 if (strlen (buf
) == 3 && buf
[0] == 'E'
8761 && isdigit (buf
[1]) && isdigit (buf
[2]))
8763 error (_("Protocol error with Rcmd"));
8765 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
8767 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
8769 fputc_unfiltered (c
, outbuf
);
8775 static VEC(mem_region_s
) *
8776 remote_memory_map (struct target_ops
*ops
)
8778 VEC(mem_region_s
) *result
= NULL
;
8779 char *text
= target_read_stralloc (¤t_target
,
8780 TARGET_OBJECT_MEMORY_MAP
, NULL
);
8784 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
8786 result
= parse_memory_map (text
);
8787 do_cleanups (back_to
);
8794 packet_command (char *args
, int from_tty
)
8796 struct remote_state
*rs
= get_remote_state ();
8799 error (_("command can only be used with remote target"));
8802 error (_("remote-packet command requires packet text as argument"));
8804 puts_filtered ("sending: ");
8805 print_packet (args
);
8806 puts_filtered ("\n");
8809 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8810 puts_filtered ("received: ");
8811 print_packet (rs
->buf
);
8812 puts_filtered ("\n");
8816 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
8818 static void display_thread_info (struct gdb_ext_thread_info
*info
);
8820 static void threadset_test_cmd (char *cmd
, int tty
);
8822 static void threadalive_test (char *cmd
, int tty
);
8824 static void threadlist_test_cmd (char *cmd
, int tty
);
8826 int get_and_display_threadinfo (threadref
*ref
);
8828 static void threadinfo_test_cmd (char *cmd
, int tty
);
8830 static int thread_display_step (threadref
*ref
, void *context
);
8832 static void threadlist_update_test_cmd (char *cmd
, int tty
);
8834 static void init_remote_threadtests (void);
8836 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
8839 threadset_test_cmd (char *cmd
, int tty
)
8841 int sample_thread
= SAMPLE_THREAD
;
8843 printf_filtered (_("Remote threadset test\n"));
8844 set_general_thread (sample_thread
);
8849 threadalive_test (char *cmd
, int tty
)
8851 int sample_thread
= SAMPLE_THREAD
;
8852 int pid
= ptid_get_pid (inferior_ptid
);
8853 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
8855 if (remote_thread_alive (ptid
))
8856 printf_filtered ("PASS: Thread alive test\n");
8858 printf_filtered ("FAIL: Thread alive test\n");
8861 void output_threadid (char *title
, threadref
*ref
);
8864 output_threadid (char *title
, threadref
*ref
)
8868 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
8870 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
8874 threadlist_test_cmd (char *cmd
, int tty
)
8877 threadref nextthread
;
8878 int done
, result_count
;
8879 threadref threadlist
[3];
8881 printf_filtered ("Remote Threadlist test\n");
8882 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
8883 &result_count
, &threadlist
[0]))
8884 printf_filtered ("FAIL: threadlist test\n");
8887 threadref
*scan
= threadlist
;
8888 threadref
*limit
= scan
+ result_count
;
8890 while (scan
< limit
)
8891 output_threadid (" thread ", scan
++);
8896 display_thread_info (struct gdb_ext_thread_info
*info
)
8898 output_threadid ("Threadid: ", &info
->threadid
);
8899 printf_filtered ("Name: %s\n ", info
->shortname
);
8900 printf_filtered ("State: %s\n", info
->display
);
8901 printf_filtered ("other: %s\n\n", info
->more_display
);
8905 get_and_display_threadinfo (threadref
*ref
)
8909 struct gdb_ext_thread_info threadinfo
;
8911 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
8912 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
8913 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
8914 display_thread_info (&threadinfo
);
8919 threadinfo_test_cmd (char *cmd
, int tty
)
8921 int athread
= SAMPLE_THREAD
;
8925 int_to_threadref (&thread
, athread
);
8926 printf_filtered ("Remote Threadinfo test\n");
8927 if (!get_and_display_threadinfo (&thread
))
8928 printf_filtered ("FAIL cannot get thread info\n");
8932 thread_display_step (threadref
*ref
, void *context
)
8934 /* output_threadid(" threadstep ",ref); *//* simple test */
8935 return get_and_display_threadinfo (ref
);
8939 threadlist_update_test_cmd (char *cmd
, int tty
)
8941 printf_filtered ("Remote Threadlist update test\n");
8942 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
8946 init_remote_threadtests (void)
8948 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
8949 _("Fetch and print the remote list of "
8950 "thread identifiers, one pkt only"));
8951 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
8952 _("Fetch and display info about one thread"));
8953 add_com ("tset", class_obscure
, threadset_test_cmd
,
8954 _("Test setting to a different thread"));
8955 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
8956 _("Iterate through updating all remote thread info"));
8957 add_com ("talive", class_obscure
, threadalive_test
,
8958 _(" Remote thread alive test "));
8963 /* Convert a thread ID to a string. Returns the string in a static
8967 remote_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
8969 static char buf
[64];
8970 struct remote_state
*rs
= get_remote_state ();
8972 if (ptid_equal (ptid
, null_ptid
))
8973 return normal_pid_to_str (ptid
);
8974 else if (ptid_is_pid (ptid
))
8976 /* Printing an inferior target id. */
8978 /* When multi-process extensions are off, there's no way in the
8979 remote protocol to know the remote process id, if there's any
8980 at all. There's one exception --- when we're connected with
8981 target extended-remote, and we manually attached to a process
8982 with "attach PID". We don't record anywhere a flag that
8983 allows us to distinguish that case from the case of
8984 connecting with extended-remote and the stub already being
8985 attached to a process, and reporting yes to qAttached, hence
8986 no smart special casing here. */
8987 if (!remote_multi_process_p (rs
))
8989 xsnprintf (buf
, sizeof buf
, "Remote target");
8993 return normal_pid_to_str (ptid
);
8997 if (ptid_equal (magic_null_ptid
, ptid
))
8998 xsnprintf (buf
, sizeof buf
, "Thread <main>");
8999 else if (rs
->extended
&& remote_multi_process_p (rs
))
9000 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
9001 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
9003 xsnprintf (buf
, sizeof buf
, "Thread %ld",
9004 ptid_get_tid (ptid
));
9009 /* Get the address of the thread local variable in OBJFILE which is
9010 stored at OFFSET within the thread local storage for thread PTID. */
9013 remote_get_thread_local_address (struct target_ops
*ops
,
9014 ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
9016 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
9018 struct remote_state
*rs
= get_remote_state ();
9020 char *endp
= rs
->buf
+ get_remote_packet_size ();
9021 enum packet_result result
;
9023 strcpy (p
, "qGetTLSAddr:");
9025 p
= write_ptid (p
, endp
, ptid
);
9027 p
+= hexnumstr (p
, offset
);
9029 p
+= hexnumstr (p
, lm
);
9033 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
9034 result
= packet_ok (rs
->buf
,
9035 &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
9036 if (result
== PACKET_OK
)
9040 unpack_varlen_hex (rs
->buf
, &result
);
9043 else if (result
== PACKET_UNKNOWN
)
9044 throw_error (TLS_GENERIC_ERROR
,
9045 _("Remote target doesn't support qGetTLSAddr packet"));
9047 throw_error (TLS_GENERIC_ERROR
,
9048 _("Remote target failed to process qGetTLSAddr request"));
9051 throw_error (TLS_GENERIC_ERROR
,
9052 _("TLS not supported or disabled on this target"));
9057 /* Provide thread local base, i.e. Thread Information Block address.
9058 Returns 1 if ptid is found and thread_local_base is non zero. */
9061 remote_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
9063 if (remote_protocol_packets
[PACKET_qGetTIBAddr
].support
!= PACKET_DISABLE
)
9065 struct remote_state
*rs
= get_remote_state ();
9067 char *endp
= rs
->buf
+ get_remote_packet_size ();
9068 enum packet_result result
;
9070 strcpy (p
, "qGetTIBAddr:");
9072 p
= write_ptid (p
, endp
, ptid
);
9076 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
9077 result
= packet_ok (rs
->buf
,
9078 &remote_protocol_packets
[PACKET_qGetTIBAddr
]);
9079 if (result
== PACKET_OK
)
9083 unpack_varlen_hex (rs
->buf
, &result
);
9085 *addr
= (CORE_ADDR
) result
;
9088 else if (result
== PACKET_UNKNOWN
)
9089 error (_("Remote target doesn't support qGetTIBAddr packet"));
9091 error (_("Remote target failed to process qGetTIBAddr request"));
9094 error (_("qGetTIBAddr not supported or disabled on this target"));
9099 /* Support for inferring a target description based on the current
9100 architecture and the size of a 'g' packet. While the 'g' packet
9101 can have any size (since optional registers can be left off the
9102 end), some sizes are easily recognizable given knowledge of the
9103 approximate architecture. */
9105 struct remote_g_packet_guess
9108 const struct target_desc
*tdesc
;
9110 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
9111 DEF_VEC_O(remote_g_packet_guess_s
);
9113 struct remote_g_packet_data
9115 VEC(remote_g_packet_guess_s
) *guesses
;
9118 static struct gdbarch_data
*remote_g_packet_data_handle
;
9121 remote_g_packet_data_init (struct obstack
*obstack
)
9123 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
9127 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
9128 const struct target_desc
*tdesc
)
9130 struct remote_g_packet_data
*data
9131 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
9132 struct remote_g_packet_guess new_guess
, *guess
;
9135 gdb_assert (tdesc
!= NULL
);
9138 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
9140 if (guess
->bytes
== bytes
)
9141 internal_error (__FILE__
, __LINE__
,
9142 _("Duplicate g packet description added for size %d"),
9145 new_guess
.bytes
= bytes
;
9146 new_guess
.tdesc
= tdesc
;
9147 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
9150 /* Return 1 if remote_read_description would do anything on this target
9151 and architecture, 0 otherwise. */
9154 remote_read_description_p (struct target_ops
*target
)
9156 struct remote_g_packet_data
*data
9157 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
9159 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
9165 static const struct target_desc
*
9166 remote_read_description (struct target_ops
*target
)
9168 struct remote_g_packet_data
*data
9169 = gdbarch_data (target_gdbarch
, remote_g_packet_data_handle
);
9171 /* Do not try this during initial connection, when we do not know
9172 whether there is a running but stopped thread. */
9173 if (!target_has_execution
|| ptid_equal (inferior_ptid
, null_ptid
))
9176 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
9178 struct remote_g_packet_guess
*guess
;
9180 int bytes
= send_g_packet ();
9183 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
9185 if (guess
->bytes
== bytes
)
9186 return guess
->tdesc
;
9188 /* We discard the g packet. A minor optimization would be to
9189 hold on to it, and fill the register cache once we have selected
9190 an architecture, but it's too tricky to do safely. */
9196 /* Remote file transfer support. This is host-initiated I/O, not
9197 target-initiated; for target-initiated, see remote-fileio.c. */
9199 /* If *LEFT is at least the length of STRING, copy STRING to
9200 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9201 decrease *LEFT. Otherwise raise an error. */
9204 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
9206 int len
= strlen (string
);
9209 error (_("Packet too long for target."));
9211 memcpy (*buffer
, string
, len
);
9215 /* NUL-terminate the buffer as a convenience, if there is
9221 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
9222 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9223 decrease *LEFT. Otherwise raise an error. */
9226 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
9229 if (2 * len
> *left
)
9230 error (_("Packet too long for target."));
9232 bin2hex (bytes
, *buffer
, len
);
9236 /* NUL-terminate the buffer as a convenience, if there is
9242 /* If *LEFT is large enough, convert VALUE to hex and add it to
9243 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9244 decrease *LEFT. Otherwise raise an error. */
9247 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
9249 int len
= hexnumlen (value
);
9252 error (_("Packet too long for target."));
9254 hexnumstr (*buffer
, value
);
9258 /* NUL-terminate the buffer as a convenience, if there is
9264 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
9265 value, *REMOTE_ERRNO to the remote error number or zero if none
9266 was included, and *ATTACHMENT to point to the start of the annex
9267 if any. The length of the packet isn't needed here; there may
9268 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
9270 Return 0 if the packet could be parsed, -1 if it could not. If
9271 -1 is returned, the other variables may not be initialized. */
9274 remote_hostio_parse_result (char *buffer
, int *retcode
,
9275 int *remote_errno
, char **attachment
)
9282 if (buffer
[0] != 'F')
9286 *retcode
= strtol (&buffer
[1], &p
, 16);
9287 if (errno
!= 0 || p
== &buffer
[1])
9290 /* Check for ",errno". */
9294 *remote_errno
= strtol (p
+ 1, &p2
, 16);
9295 if (errno
!= 0 || p
+ 1 == p2
)
9300 /* Check for ";attachment". If there is no attachment, the
9301 packet should end here. */
9304 *attachment
= p
+ 1;
9307 else if (*p
== '\0')
9313 /* Send a prepared I/O packet to the target and read its response.
9314 The prepared packet is in the global RS->BUF before this function
9315 is called, and the answer is there when we return.
9317 COMMAND_BYTES is the length of the request to send, which may include
9318 binary data. WHICH_PACKET is the packet configuration to check
9319 before attempting a packet. If an error occurs, *REMOTE_ERRNO
9320 is set to the error number and -1 is returned. Otherwise the value
9321 returned by the function is returned.
9323 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
9324 attachment is expected; an error will be reported if there's a
9325 mismatch. If one is found, *ATTACHMENT will be set to point into
9326 the packet buffer and *ATTACHMENT_LEN will be set to the
9327 attachment's length. */
9330 remote_hostio_send_command (int command_bytes
, int which_packet
,
9331 int *remote_errno
, char **attachment
,
9332 int *attachment_len
)
9334 struct remote_state
*rs
= get_remote_state ();
9335 int ret
, bytes_read
;
9336 char *attachment_tmp
;
9339 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
9341 *remote_errno
= FILEIO_ENOSYS
;
9345 putpkt_binary (rs
->buf
, command_bytes
);
9346 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
9348 /* If it timed out, something is wrong. Don't try to parse the
9352 *remote_errno
= FILEIO_EINVAL
;
9356 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
9359 *remote_errno
= FILEIO_EINVAL
;
9361 case PACKET_UNKNOWN
:
9362 *remote_errno
= FILEIO_ENOSYS
;
9368 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
9371 *remote_errno
= FILEIO_EINVAL
;
9375 /* Make sure we saw an attachment if and only if we expected one. */
9376 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
9377 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
9379 *remote_errno
= FILEIO_EINVAL
;
9383 /* If an attachment was found, it must point into the packet buffer;
9384 work out how many bytes there were. */
9385 if (attachment_tmp
!= NULL
)
9387 *attachment
= attachment_tmp
;
9388 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
9394 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9395 remote file descriptor, or -1 if an error occurs (and set
9399 remote_hostio_open (const char *filename
, int flags
, int mode
,
9402 struct remote_state
*rs
= get_remote_state ();
9404 int left
= get_remote_packet_size () - 1;
9406 remote_buffer_add_string (&p
, &left
, "vFile:open:");
9408 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9410 remote_buffer_add_string (&p
, &left
, ",");
9412 remote_buffer_add_int (&p
, &left
, flags
);
9413 remote_buffer_add_string (&p
, &left
, ",");
9415 remote_buffer_add_int (&p
, &left
, mode
);
9417 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
9418 remote_errno
, NULL
, NULL
);
9421 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9422 Return the number of bytes written, or -1 if an error occurs (and
9423 set *REMOTE_ERRNO). */
9426 remote_hostio_pwrite (int fd
, const gdb_byte
*write_buf
, int len
,
9427 ULONGEST offset
, int *remote_errno
)
9429 struct remote_state
*rs
= get_remote_state ();
9431 int left
= get_remote_packet_size ();
9434 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
9436 remote_buffer_add_int (&p
, &left
, fd
);
9437 remote_buffer_add_string (&p
, &left
, ",");
9439 remote_buffer_add_int (&p
, &left
, offset
);
9440 remote_buffer_add_string (&p
, &left
, ",");
9442 p
+= remote_escape_output (write_buf
, len
, p
, &out_len
,
9443 get_remote_packet_size () - (p
- rs
->buf
));
9445 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
9446 remote_errno
, NULL
, NULL
);
9449 /* Read up to LEN bytes FD on the remote target into READ_BUF
9450 Return the number of bytes read, or -1 if an error occurs (and
9451 set *REMOTE_ERRNO). */
9454 remote_hostio_pread (int fd
, gdb_byte
*read_buf
, int len
,
9455 ULONGEST offset
, int *remote_errno
)
9457 struct remote_state
*rs
= get_remote_state ();
9460 int left
= get_remote_packet_size ();
9461 int ret
, attachment_len
;
9464 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
9466 remote_buffer_add_int (&p
, &left
, fd
);
9467 remote_buffer_add_string (&p
, &left
, ",");
9469 remote_buffer_add_int (&p
, &left
, len
);
9470 remote_buffer_add_string (&p
, &left
, ",");
9472 remote_buffer_add_int (&p
, &left
, offset
);
9474 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
9475 remote_errno
, &attachment
,
9481 read_len
= remote_unescape_input (attachment
, attachment_len
,
9483 if (read_len
!= ret
)
9484 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
9489 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9490 (and set *REMOTE_ERRNO). */
9493 remote_hostio_close (int fd
, int *remote_errno
)
9495 struct remote_state
*rs
= get_remote_state ();
9497 int left
= get_remote_packet_size () - 1;
9499 remote_buffer_add_string (&p
, &left
, "vFile:close:");
9501 remote_buffer_add_int (&p
, &left
, fd
);
9503 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
9504 remote_errno
, NULL
, NULL
);
9507 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9508 occurs (and set *REMOTE_ERRNO). */
9511 remote_hostio_unlink (const char *filename
, int *remote_errno
)
9513 struct remote_state
*rs
= get_remote_state ();
9515 int left
= get_remote_packet_size () - 1;
9517 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
9519 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9522 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
9523 remote_errno
, NULL
, NULL
);
9526 /* Read value of symbolic link FILENAME on the remote target. Return
9527 a null-terminated string allocated via xmalloc, or NULL if an error
9528 occurs (and set *REMOTE_ERRNO). */
9531 remote_hostio_readlink (const char *filename
, int *remote_errno
)
9533 struct remote_state
*rs
= get_remote_state ();
9536 int left
= get_remote_packet_size ();
9537 int len
, attachment_len
;
9541 remote_buffer_add_string (&p
, &left
, "vFile:readlink:");
9543 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9546 len
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_readlink
,
9547 remote_errno
, &attachment
,
9553 ret
= xmalloc (len
+ 1);
9555 read_len
= remote_unescape_input (attachment
, attachment_len
,
9557 if (read_len
!= len
)
9558 error (_("Readlink returned %d, but %d bytes."), len
, read_len
);
9565 remote_fileio_errno_to_host (int errnum
)
9589 case FILEIO_ENOTDIR
:
9609 case FILEIO_ENAMETOOLONG
:
9610 return ENAMETOOLONG
;
9616 remote_hostio_error (int errnum
)
9618 int host_error
= remote_fileio_errno_to_host (errnum
);
9620 if (host_error
== -1)
9621 error (_("Unknown remote I/O error %d"), errnum
);
9623 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
9627 remote_hostio_close_cleanup (void *opaque
)
9629 int fd
= *(int *) opaque
;
9632 remote_hostio_close (fd
, &remote_errno
);
9637 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
9639 const char *filename
= bfd_get_filename (abfd
);
9640 int fd
, remote_errno
;
9643 gdb_assert (remote_filename_p (filename
));
9645 fd
= remote_hostio_open (filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
9648 errno
= remote_fileio_errno_to_host (remote_errno
);
9649 bfd_set_error (bfd_error_system_call
);
9653 stream
= xmalloc (sizeof (int));
9659 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
9661 int fd
= *(int *)stream
;
9666 /* Ignore errors on close; these may happen if the remote
9667 connection was already torn down. */
9668 remote_hostio_close (fd
, &remote_errno
);
9674 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
9675 file_ptr nbytes
, file_ptr offset
)
9677 int fd
= *(int *)stream
;
9679 file_ptr pos
, bytes
;
9682 while (nbytes
> pos
)
9684 bytes
= remote_hostio_pread (fd
, (char *)buf
+ pos
, nbytes
- pos
,
9685 offset
+ pos
, &remote_errno
);
9687 /* Success, but no bytes, means end-of-file. */
9691 errno
= remote_fileio_errno_to_host (remote_errno
);
9692 bfd_set_error (bfd_error_system_call
);
9703 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
9705 /* FIXME: We should probably implement remote_hostio_stat. */
9706 sb
->st_size
= INT_MAX
;
9711 remote_filename_p (const char *filename
)
9713 return strncmp (filename
, "remote:", 7) == 0;
9717 remote_bfd_open (const char *remote_file
, const char *target
)
9719 return bfd_openr_iovec (remote_file
, target
,
9720 remote_bfd_iovec_open
, NULL
,
9721 remote_bfd_iovec_pread
,
9722 remote_bfd_iovec_close
,
9723 remote_bfd_iovec_stat
);
9727 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
9729 struct cleanup
*back_to
, *close_cleanup
;
9730 int retcode
, fd
, remote_errno
, bytes
, io_size
;
9733 int bytes_in_buffer
;
9738 error (_("command can only be used with remote target"));
9740 file
= fopen (local_file
, "rb");
9742 perror_with_name (local_file
);
9743 back_to
= make_cleanup_fclose (file
);
9745 fd
= remote_hostio_open (remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
9747 0700, &remote_errno
);
9749 remote_hostio_error (remote_errno
);
9751 /* Send up to this many bytes at once. They won't all fit in the
9752 remote packet limit, so we'll transfer slightly fewer. */
9753 io_size
= get_remote_packet_size ();
9754 buffer
= xmalloc (io_size
);
9755 make_cleanup (xfree
, buffer
);
9757 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
9759 bytes_in_buffer
= 0;
9762 while (bytes_in_buffer
|| !saw_eof
)
9766 bytes
= fread (buffer
+ bytes_in_buffer
, 1,
9767 io_size
- bytes_in_buffer
,
9772 error (_("Error reading %s."), local_file
);
9775 /* EOF. Unless there is something still in the
9776 buffer from the last iteration, we are done. */
9778 if (bytes_in_buffer
== 0)
9786 bytes
+= bytes_in_buffer
;
9787 bytes_in_buffer
= 0;
9789 retcode
= remote_hostio_pwrite (fd
, buffer
, bytes
,
9790 offset
, &remote_errno
);
9793 remote_hostio_error (remote_errno
);
9794 else if (retcode
== 0)
9795 error (_("Remote write of %d bytes returned 0!"), bytes
);
9796 else if (retcode
< bytes
)
9798 /* Short write. Save the rest of the read data for the next
9800 bytes_in_buffer
= bytes
- retcode
;
9801 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
9807 discard_cleanups (close_cleanup
);
9808 if (remote_hostio_close (fd
, &remote_errno
))
9809 remote_hostio_error (remote_errno
);
9812 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
9813 do_cleanups (back_to
);
9817 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
9819 struct cleanup
*back_to
, *close_cleanup
;
9820 int fd
, remote_errno
, bytes
, io_size
;
9826 error (_("command can only be used with remote target"));
9828 fd
= remote_hostio_open (remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
9830 remote_hostio_error (remote_errno
);
9832 file
= fopen (local_file
, "wb");
9834 perror_with_name (local_file
);
9835 back_to
= make_cleanup_fclose (file
);
9837 /* Send up to this many bytes at once. They won't all fit in the
9838 remote packet limit, so we'll transfer slightly fewer. */
9839 io_size
= get_remote_packet_size ();
9840 buffer
= xmalloc (io_size
);
9841 make_cleanup (xfree
, buffer
);
9843 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
9848 bytes
= remote_hostio_pread (fd
, buffer
, io_size
, offset
, &remote_errno
);
9850 /* Success, but no bytes, means end-of-file. */
9853 remote_hostio_error (remote_errno
);
9857 bytes
= fwrite (buffer
, 1, bytes
, file
);
9859 perror_with_name (local_file
);
9862 discard_cleanups (close_cleanup
);
9863 if (remote_hostio_close (fd
, &remote_errno
))
9864 remote_hostio_error (remote_errno
);
9867 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
9868 do_cleanups (back_to
);
9872 remote_file_delete (const char *remote_file
, int from_tty
)
9874 int retcode
, remote_errno
;
9877 error (_("command can only be used with remote target"));
9879 retcode
= remote_hostio_unlink (remote_file
, &remote_errno
);
9881 remote_hostio_error (remote_errno
);
9884 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
9888 remote_put_command (char *args
, int from_tty
)
9890 struct cleanup
*back_to
;
9894 error_no_arg (_("file to put"));
9896 argv
= gdb_buildargv (args
);
9897 back_to
= make_cleanup_freeargv (argv
);
9898 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
9899 error (_("Invalid parameters to remote put"));
9901 remote_file_put (argv
[0], argv
[1], from_tty
);
9903 do_cleanups (back_to
);
9907 remote_get_command (char *args
, int from_tty
)
9909 struct cleanup
*back_to
;
9913 error_no_arg (_("file to get"));
9915 argv
= gdb_buildargv (args
);
9916 back_to
= make_cleanup_freeargv (argv
);
9917 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
9918 error (_("Invalid parameters to remote get"));
9920 remote_file_get (argv
[0], argv
[1], from_tty
);
9922 do_cleanups (back_to
);
9926 remote_delete_command (char *args
, int from_tty
)
9928 struct cleanup
*back_to
;
9932 error_no_arg (_("file to delete"));
9934 argv
= gdb_buildargv (args
);
9935 back_to
= make_cleanup_freeargv (argv
);
9936 if (argv
[0] == NULL
|| argv
[1] != NULL
)
9937 error (_("Invalid parameters to remote delete"));
9939 remote_file_delete (argv
[0], from_tty
);
9941 do_cleanups (back_to
);
9945 remote_command (char *args
, int from_tty
)
9947 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
9951 remote_can_execute_reverse (void)
9953 if (remote_protocol_packets
[PACKET_bs
].support
== PACKET_ENABLE
9954 || remote_protocol_packets
[PACKET_bc
].support
== PACKET_ENABLE
)
9961 remote_supports_non_stop (void)
9967 remote_supports_disable_randomization (void)
9969 /* Only supported in extended mode. */
9974 remote_supports_multi_process (void)
9976 struct remote_state
*rs
= get_remote_state ();
9978 /* Only extended-remote handles being attached to multiple
9979 processes, even though plain remote can use the multi-process
9980 thread id extensions, so that GDB knows the target process's
9982 return rs
->extended
&& remote_multi_process_p (rs
);
9986 remote_supports_cond_tracepoints (void)
9988 struct remote_state
*rs
= get_remote_state ();
9990 return rs
->cond_tracepoints
;
9994 remote_supports_cond_breakpoints (void)
9996 struct remote_state
*rs
= get_remote_state ();
9998 return rs
->cond_breakpoints
;
10002 remote_supports_fast_tracepoints (void)
10004 struct remote_state
*rs
= get_remote_state ();
10006 return rs
->fast_tracepoints
;
10010 remote_supports_static_tracepoints (void)
10012 struct remote_state
*rs
= get_remote_state ();
10014 return rs
->static_tracepoints
;
10018 remote_supports_install_in_trace (void)
10020 struct remote_state
*rs
= get_remote_state ();
10022 return rs
->install_in_trace
;
10026 remote_supports_enable_disable_tracepoint (void)
10028 struct remote_state
*rs
= get_remote_state ();
10030 return rs
->enable_disable_tracepoints
;
10034 remote_supports_string_tracing (void)
10036 struct remote_state
*rs
= get_remote_state ();
10038 return rs
->string_tracing
;
10042 remote_trace_init (void)
10045 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10046 if (strcmp (target_buf
, "OK") != 0)
10047 error (_("Target does not support this command."));
10050 static void free_actions_list (char **actions_list
);
10051 static void free_actions_list_cleanup_wrapper (void *);
10053 free_actions_list_cleanup_wrapper (void *al
)
10055 free_actions_list (al
);
10059 free_actions_list (char **actions_list
)
10063 if (actions_list
== 0)
10066 for (ndx
= 0; actions_list
[ndx
]; ndx
++)
10067 xfree (actions_list
[ndx
]);
10069 xfree (actions_list
);
10072 /* Recursive routine to walk through command list including loops, and
10073 download packets for each command. */
10076 remote_download_command_source (int num
, ULONGEST addr
,
10077 struct command_line
*cmds
)
10079 struct remote_state
*rs
= get_remote_state ();
10080 struct command_line
*cmd
;
10082 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
10084 QUIT
; /* Allow user to bail out with ^C. */
10085 strcpy (rs
->buf
, "QTDPsrc:");
10086 encode_source_string (num
, addr
, "cmd", cmd
->line
,
10087 rs
->buf
+ strlen (rs
->buf
),
10088 rs
->buf_size
- strlen (rs
->buf
));
10090 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10091 if (strcmp (target_buf
, "OK"))
10092 warning (_("Target does not support source download."));
10094 if (cmd
->control_type
== while_control
10095 || cmd
->control_type
== while_stepping_control
)
10097 remote_download_command_source (num
, addr
, *cmd
->body_list
);
10099 QUIT
; /* Allow user to bail out with ^C. */
10100 strcpy (rs
->buf
, "QTDPsrc:");
10101 encode_source_string (num
, addr
, "cmd", "end",
10102 rs
->buf
+ strlen (rs
->buf
),
10103 rs
->buf_size
- strlen (rs
->buf
));
10105 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10106 if (strcmp (target_buf
, "OK"))
10107 warning (_("Target does not support source download."));
10113 remote_download_tracepoint (struct bp_location
*loc
)
10119 char **tdp_actions
;
10120 char **stepping_actions
;
10122 struct cleanup
*old_chain
= NULL
;
10123 struct agent_expr
*aexpr
;
10124 struct cleanup
*aexpr_chain
= NULL
;
10126 struct breakpoint
*b
= loc
->owner
;
10127 struct tracepoint
*t
= (struct tracepoint
*) b
;
10129 encode_actions (loc
->owner
, loc
, &tdp_actions
, &stepping_actions
);
10130 old_chain
= make_cleanup (free_actions_list_cleanup_wrapper
,
10132 (void) make_cleanup (free_actions_list_cleanup_wrapper
,
10135 tpaddr
= loc
->address
;
10136 sprintf_vma (addrbuf
, tpaddr
);
10137 sprintf (buf
, "QTDP:%x:%s:%c:%lx:%x", b
->number
,
10138 addrbuf
, /* address */
10139 (b
->enable_state
== bp_enabled
? 'E' : 'D'),
10140 t
->step_count
, t
->pass_count
);
10141 /* Fast tracepoints are mostly handled by the target, but we can
10142 tell the target how big of an instruction block should be moved
10144 if (b
->type
== bp_fast_tracepoint
)
10146 /* Only test for support at download time; we may not know
10147 target capabilities at definition time. */
10148 if (remote_supports_fast_tracepoints ())
10152 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch
,
10153 tpaddr
, &isize
, NULL
))
10154 sprintf (buf
+ strlen (buf
), ":F%x", isize
);
10156 /* If it passed validation at definition but fails now,
10157 something is very wrong. */
10158 internal_error (__FILE__
, __LINE__
,
10159 _("Fast tracepoint not "
10160 "valid during download"));
10163 /* Fast tracepoints are functionally identical to regular
10164 tracepoints, so don't take lack of support as a reason to
10165 give up on the trace run. */
10166 warning (_("Target does not support fast tracepoints, "
10167 "downloading %d as regular tracepoint"), b
->number
);
10169 else if (b
->type
== bp_static_tracepoint
)
10171 /* Only test for support at download time; we may not know
10172 target capabilities at definition time. */
10173 if (remote_supports_static_tracepoints ())
10175 struct static_tracepoint_marker marker
;
10177 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
10178 strcat (buf
, ":S");
10180 error (_("Static tracepoint not valid during download"));
10183 /* Fast tracepoints are functionally identical to regular
10184 tracepoints, so don't take lack of support as a reason
10185 to give up on the trace run. */
10186 error (_("Target does not support static tracepoints"));
10188 /* If the tracepoint has a conditional, make it into an agent
10189 expression and append to the definition. */
10192 /* Only test support at download time, we may not know target
10193 capabilities at definition time. */
10194 if (remote_supports_cond_tracepoints ())
10196 aexpr
= gen_eval_for_expr (tpaddr
, loc
->cond
);
10197 aexpr_chain
= make_cleanup_free_agent_expr (aexpr
);
10198 sprintf (buf
+ strlen (buf
), ":X%x,", aexpr
->len
);
10199 pkt
= buf
+ strlen (buf
);
10200 for (ndx
= 0; ndx
< aexpr
->len
; ++ndx
)
10201 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
10203 do_cleanups (aexpr_chain
);
10206 warning (_("Target does not support conditional tracepoints, "
10207 "ignoring tp %d cond"), b
->number
);
10210 if (b
->commands
|| *default_collect
)
10213 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10214 if (strcmp (target_buf
, "OK"))
10215 error (_("Target does not support tracepoints."));
10217 /* do_single_steps (t); */
10220 for (ndx
= 0; tdp_actions
[ndx
]; ndx
++)
10222 QUIT
; /* Allow user to bail out with ^C. */
10223 sprintf (buf
, "QTDP:-%x:%s:%s%c",
10224 b
->number
, addrbuf
, /* address */
10226 ((tdp_actions
[ndx
+ 1] || stepping_actions
)
10229 remote_get_noisy_reply (&target_buf
,
10231 if (strcmp (target_buf
, "OK"))
10232 error (_("Error on target while setting tracepoints."));
10235 if (stepping_actions
)
10237 for (ndx
= 0; stepping_actions
[ndx
]; ndx
++)
10239 QUIT
; /* Allow user to bail out with ^C. */
10240 sprintf (buf
, "QTDP:-%x:%s:%s%s%s",
10241 b
->number
, addrbuf
, /* address */
10242 ((ndx
== 0) ? "S" : ""),
10243 stepping_actions
[ndx
],
10244 (stepping_actions
[ndx
+ 1] ? "-" : ""));
10246 remote_get_noisy_reply (&target_buf
,
10248 if (strcmp (target_buf
, "OK"))
10249 error (_("Error on target while setting tracepoints."));
10253 if (remote_protocol_packets
[PACKET_TracepointSource
].support
10256 if (b
->addr_string
)
10258 strcpy (buf
, "QTDPsrc:");
10259 encode_source_string (b
->number
, loc
->address
,
10260 "at", b
->addr_string
, buf
+ strlen (buf
),
10261 2048 - strlen (buf
));
10264 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10265 if (strcmp (target_buf
, "OK"))
10266 warning (_("Target does not support source download."));
10268 if (b
->cond_string
)
10270 strcpy (buf
, "QTDPsrc:");
10271 encode_source_string (b
->number
, loc
->address
,
10272 "cond", b
->cond_string
, buf
+ strlen (buf
),
10273 2048 - strlen (buf
));
10275 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10276 if (strcmp (target_buf
, "OK"))
10277 warning (_("Target does not support source download."));
10279 remote_download_command_source (b
->number
, loc
->address
,
10280 breakpoint_commands (b
));
10283 do_cleanups (old_chain
);
10287 remote_can_download_tracepoint (void)
10289 struct trace_status
*ts
= current_trace_status ();
10290 int status
= remote_get_trace_status (ts
);
10292 if (status
== -1 || !ts
->running_known
|| !ts
->running
)
10295 /* If we are in a tracing experiment, but remote stub doesn't support
10296 installing tracepoint in trace, we have to return. */
10297 if (!remote_supports_install_in_trace ())
10305 remote_download_trace_state_variable (struct trace_state_variable
*tsv
)
10307 struct remote_state
*rs
= get_remote_state ();
10310 sprintf (rs
->buf
, "QTDV:%x:%s:%x:",
10311 tsv
->number
, phex ((ULONGEST
) tsv
->initial_value
, 8), tsv
->builtin
);
10312 p
= rs
->buf
+ strlen (rs
->buf
);
10313 if ((p
- rs
->buf
) + strlen (tsv
->name
) * 2 >= get_remote_packet_size ())
10314 error (_("Trace state variable name too long for tsv definition packet"));
10315 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
->name
), p
, 0);
10318 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10319 if (*target_buf
== '\0')
10320 error (_("Target does not support this command."));
10321 if (strcmp (target_buf
, "OK") != 0)
10322 error (_("Error on target while downloading trace state variable."));
10326 remote_enable_tracepoint (struct bp_location
*location
)
10328 struct remote_state
*rs
= get_remote_state ();
10331 sprintf_vma (addr_buf
, location
->address
);
10332 sprintf (rs
->buf
, "QTEnable:%x:%s", location
->owner
->number
, addr_buf
);
10334 remote_get_noisy_reply (&rs
->buf
, &rs
->buf_size
);
10335 if (*rs
->buf
== '\0')
10336 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
10337 if (strcmp (rs
->buf
, "OK") != 0)
10338 error (_("Error on target while enabling tracepoint."));
10342 remote_disable_tracepoint (struct bp_location
*location
)
10344 struct remote_state
*rs
= get_remote_state ();
10347 sprintf_vma (addr_buf
, location
->address
);
10348 sprintf (rs
->buf
, "QTDisable:%x:%s", location
->owner
->number
, addr_buf
);
10350 remote_get_noisy_reply (&rs
->buf
, &rs
->buf_size
);
10351 if (*rs
->buf
== '\0')
10352 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
10353 if (strcmp (rs
->buf
, "OK") != 0)
10354 error (_("Error on target while disabling tracepoint."));
10358 remote_trace_set_readonly_regions (void)
10361 bfd_size_type size
;
10367 return; /* No information to give. */
10369 strcpy (target_buf
, "QTro");
10370 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
10372 char tmp1
[40], tmp2
[40];
10375 if ((s
->flags
& SEC_LOAD
) == 0 ||
10376 /* (s->flags & SEC_CODE) == 0 || */
10377 (s
->flags
& SEC_READONLY
) == 0)
10381 vma
= bfd_get_section_vma (,s
);
10382 size
= bfd_get_section_size (s
);
10383 sprintf_vma (tmp1
, vma
);
10384 sprintf_vma (tmp2
, vma
+ size
);
10385 sec_length
= 1 + strlen (tmp1
) + 1 + strlen (tmp2
);
10386 if (offset
+ sec_length
+ 1 > target_buf_size
)
10388 if (remote_protocol_packets
[PACKET_qXfer_traceframe_info
].support
10391 Too many sections for read-only sections definition packet."));
10394 sprintf (target_buf
+ offset
, ":%s,%s", tmp1
, tmp2
);
10395 offset
+= sec_length
;
10399 putpkt (target_buf
);
10400 getpkt (&target_buf
, &target_buf_size
, 0);
10405 remote_trace_start (void)
10407 putpkt ("QTStart");
10408 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10409 if (*target_buf
== '\0')
10410 error (_("Target does not support this command."));
10411 if (strcmp (target_buf
, "OK") != 0)
10412 error (_("Bogus reply from target: %s"), target_buf
);
10416 remote_get_trace_status (struct trace_status
*ts
)
10418 /* Initialize it just to avoid a GCC false warning. */
10420 /* FIXME we need to get register block size some other way. */
10421 extern int trace_regblock_size
;
10422 volatile struct gdb_exception ex
;
10424 trace_regblock_size
= get_remote_arch_state ()->sizeof_g_packet
;
10426 putpkt ("qTStatus");
10428 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
10430 p
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10434 exception_fprintf (gdb_stderr
, ex
, "qTStatus: ");
10438 /* If the remote target doesn't do tracing, flag it. */
10442 /* We're working with a live target. */
10445 /* Set some defaults. */
10446 ts
->running_known
= 0;
10447 ts
->stop_reason
= trace_stop_reason_unknown
;
10448 ts
->traceframe_count
= -1;
10449 ts
->buffer_free
= 0;
10452 error (_("Bogus trace status reply from target: %s"), target_buf
);
10454 parse_trace_status (p
, ts
);
10456 return ts
->running
;
10460 remote_get_tracepoint_status (struct breakpoint
*bp
,
10461 struct uploaded_tp
*utp
)
10463 struct remote_state
*rs
= get_remote_state ();
10465 struct bp_location
*loc
;
10466 struct tracepoint
*tp
= (struct tracepoint
*) bp
;
10470 tp
->base
.hit_count
= 0;
10471 tp
->traceframe_usage
= 0;
10472 for (loc
= tp
->base
.loc
; loc
; loc
= loc
->next
)
10474 /* If the tracepoint was never downloaded, don't go asking for
10476 if (tp
->number_on_target
== 0)
10478 sprintf (rs
->buf
, "qTP:%x:%s", tp
->number_on_target
,
10479 phex_nz (loc
->address
, 0));
10481 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10482 if (reply
&& *reply
)
10485 parse_tracepoint_status (reply
+ 1, bp
, utp
);
10491 utp
->hit_count
= 0;
10492 utp
->traceframe_usage
= 0;
10493 sprintf (rs
->buf
, "qTP:%x:%s", utp
->number
, phex_nz (utp
->addr
, 0));
10495 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10496 if (reply
&& *reply
)
10499 parse_tracepoint_status (reply
+ 1, bp
, utp
);
10505 remote_trace_stop (void)
10508 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10509 if (*target_buf
== '\0')
10510 error (_("Target does not support this command."));
10511 if (strcmp (target_buf
, "OK") != 0)
10512 error (_("Bogus reply from target: %s"), target_buf
);
10516 remote_trace_find (enum trace_find_type type
, int num
,
10517 ULONGEST addr1
, ULONGEST addr2
,
10520 struct remote_state
*rs
= get_remote_state ();
10522 int target_frameno
= -1, target_tracept
= -1;
10524 /* Lookups other than by absolute frame number depend on the current
10525 trace selected, so make sure it is correct on the remote end
10527 if (type
!= tfind_number
)
10528 set_remote_traceframe ();
10531 strcpy (p
, "QTFrame:");
10532 p
= strchr (p
, '\0');
10536 sprintf (p
, "%x", num
);
10539 sprintf (p
, "pc:%s", phex_nz (addr1
, 0));
10542 sprintf (p
, "tdp:%x", num
);
10545 sprintf (p
, "range:%s:%s", phex_nz (addr1
, 0), phex_nz (addr2
, 0));
10547 case tfind_outside
:
10548 sprintf (p
, "outside:%s:%s", phex_nz (addr1
, 0), phex_nz (addr2
, 0));
10551 error (_("Unknown trace find type %d"), type
);
10555 reply
= remote_get_noisy_reply (&(rs
->buf
), &sizeof_pkt
);
10556 if (*reply
== '\0')
10557 error (_("Target does not support this command."));
10559 while (reply
&& *reply
)
10564 target_frameno
= (int) strtol (p
, &reply
, 16);
10566 error (_("Unable to parse trace frame number"));
10567 /* Don't update our remote traceframe number cache on failure
10568 to select a remote traceframe. */
10569 if (target_frameno
== -1)
10574 target_tracept
= (int) strtol (p
, &reply
, 16);
10576 error (_("Unable to parse tracepoint number"));
10578 case 'O': /* "OK"? */
10579 if (reply
[1] == 'K' && reply
[2] == '\0')
10582 error (_("Bogus reply from target: %s"), reply
);
10585 error (_("Bogus reply from target: %s"), reply
);
10588 *tpp
= target_tracept
;
10590 remote_traceframe_number
= target_frameno
;
10591 return target_frameno
;
10595 remote_get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
10597 struct remote_state
*rs
= get_remote_state ();
10601 set_remote_traceframe ();
10603 sprintf (rs
->buf
, "qTV:%x", tsvnum
);
10605 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10606 if (reply
&& *reply
)
10610 unpack_varlen_hex (reply
+ 1, &uval
);
10611 *val
= (LONGEST
) uval
;
10619 remote_save_trace_data (const char *filename
)
10621 struct remote_state
*rs
= get_remote_state ();
10625 strcpy (p
, "QTSave:");
10627 if ((p
- rs
->buf
) + strlen (filename
) * 2 >= get_remote_packet_size ())
10628 error (_("Remote file name too long for trace save packet"));
10629 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, 0);
10632 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10633 if (*reply
== '\0')
10634 error (_("Target does not support this command."));
10635 if (strcmp (reply
, "OK") != 0)
10636 error (_("Bogus reply from target: %s"), reply
);
10640 /* This is basically a memory transfer, but needs to be its own packet
10641 because we don't know how the target actually organizes its trace
10642 memory, plus we want to be able to ask for as much as possible, but
10643 not be unhappy if we don't get as much as we ask for. */
10646 remote_get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
10648 struct remote_state
*rs
= get_remote_state ();
10654 strcpy (p
, "qTBuffer:");
10656 p
+= hexnumstr (p
, offset
);
10658 p
+= hexnumstr (p
, len
);
10662 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10663 if (reply
&& *reply
)
10665 /* 'l' by itself means we're at the end of the buffer and
10666 there is nothing more to get. */
10670 /* Convert the reply into binary. Limit the number of bytes to
10671 convert according to our passed-in buffer size, rather than
10672 what was returned in the packet; if the target is
10673 unexpectedly generous and gives us a bigger reply than we
10674 asked for, we don't want to crash. */
10675 rslt
= hex2bin (target_buf
, buf
, len
);
10679 /* Something went wrong, flag as an error. */
10684 remote_set_disconnected_tracing (int val
)
10686 struct remote_state
*rs
= get_remote_state ();
10688 if (rs
->disconnected_tracing
)
10692 sprintf (rs
->buf
, "QTDisconnected:%x", val
);
10694 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10695 if (*reply
== '\0')
10696 error (_("Target does not support this command."));
10697 if (strcmp (reply
, "OK") != 0)
10698 error (_("Bogus reply from target: %s"), reply
);
10701 warning (_("Target does not support disconnected tracing."));
10705 remote_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
10707 struct thread_info
*info
= find_thread_ptid (ptid
);
10709 if (info
&& info
->private)
10710 return info
->private->core
;
10715 remote_set_circular_trace_buffer (int val
)
10717 struct remote_state
*rs
= get_remote_state ();
10720 sprintf (rs
->buf
, "QTBuffer:circular:%x", val
);
10722 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10723 if (*reply
== '\0')
10724 error (_("Target does not support this command."));
10725 if (strcmp (reply
, "OK") != 0)
10726 error (_("Bogus reply from target: %s"), reply
);
10729 static struct traceframe_info
*
10730 remote_traceframe_info (void)
10734 text
= target_read_stralloc (¤t_target
,
10735 TARGET_OBJECT_TRACEFRAME_INFO
, NULL
);
10738 struct traceframe_info
*info
;
10739 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
10741 info
= parse_traceframe_info (text
);
10742 do_cleanups (back_to
);
10749 /* Handle the qTMinFTPILen packet. Returns the minimum length of
10750 instruction on which a fast tracepoint may be placed. Returns -1
10751 if the packet is not supported, and 0 if the minimum instruction
10752 length is unknown. */
10755 remote_get_min_fast_tracepoint_insn_len (void)
10757 struct remote_state
*rs
= get_remote_state ();
10760 /* If we're not debugging a process yet, the IPA can't be
10762 if (!target_has_execution
)
10765 /* Make sure the remote is pointing at the right process. */
10766 set_general_process ();
10768 sprintf (rs
->buf
, "qTMinFTPILen");
10770 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10771 if (*reply
== '\0')
10775 ULONGEST min_insn_len
;
10777 unpack_varlen_hex (reply
, &min_insn_len
);
10779 return (int) min_insn_len
;
10784 remote_set_trace_notes (char *user
, char *notes
, char *stop_notes
)
10786 struct remote_state
*rs
= get_remote_state ();
10788 char *buf
= rs
->buf
;
10789 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
10792 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTNotes:");
10795 buf
+= xsnprintf (buf
, endbuf
- buf
, "user:");
10796 nbytes
= bin2hex (user
, buf
, 0);
10802 buf
+= xsnprintf (buf
, endbuf
- buf
, "notes:");
10803 nbytes
= bin2hex (notes
, buf
, 0);
10809 buf
+= xsnprintf (buf
, endbuf
- buf
, "tstop:");
10810 nbytes
= bin2hex (stop_notes
, buf
, 0);
10814 /* Ensure the buffer is terminated. */
10818 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10819 if (*reply
== '\0')
10822 if (strcmp (reply
, "OK") != 0)
10823 error (_("Bogus reply from target: %s"), reply
);
10829 remote_use_agent (int use
)
10831 if (remote_protocol_packets
[PACKET_QAgent
].support
!= PACKET_DISABLE
)
10833 struct remote_state
*rs
= get_remote_state ();
10835 /* If the stub supports QAgent. */
10836 sprintf (rs
->buf
, "QAgent:%d", use
);
10838 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
10840 if (strcmp (rs
->buf
, "OK") == 0)
10851 remote_can_use_agent (void)
10853 return (remote_protocol_packets
[PACKET_QAgent
].support
!= PACKET_DISABLE
);
10857 init_remote_ops (void)
10859 remote_ops
.to_shortname
= "remote";
10860 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
10861 remote_ops
.to_doc
=
10862 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10863 Specify the serial device it is connected to\n\
10864 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
10865 remote_ops
.to_open
= remote_open
;
10866 remote_ops
.to_close
= remote_close
;
10867 remote_ops
.to_detach
= remote_detach
;
10868 remote_ops
.to_disconnect
= remote_disconnect
;
10869 remote_ops
.to_resume
= remote_resume
;
10870 remote_ops
.to_wait
= remote_wait
;
10871 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
10872 remote_ops
.to_store_registers
= remote_store_registers
;
10873 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
10874 remote_ops
.deprecated_xfer_memory
= remote_xfer_memory
;
10875 remote_ops
.to_files_info
= remote_files_info
;
10876 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
10877 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
10878 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
10879 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
10880 remote_ops
.to_watchpoint_addr_within_range
=
10881 remote_watchpoint_addr_within_range
;
10882 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
10883 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
10884 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
10885 remote_ops
.to_region_ok_for_hw_watchpoint
10886 = remote_region_ok_for_hw_watchpoint
;
10887 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
10888 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
10889 remote_ops
.to_kill
= remote_kill
;
10890 remote_ops
.to_load
= generic_load
;
10891 remote_ops
.to_mourn_inferior
= remote_mourn
;
10892 remote_ops
.to_pass_signals
= remote_pass_signals
;
10893 remote_ops
.to_program_signals
= remote_program_signals
;
10894 remote_ops
.to_thread_alive
= remote_thread_alive
;
10895 remote_ops
.to_find_new_threads
= remote_threads_info
;
10896 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
10897 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
10898 remote_ops
.to_get_ada_task_ptid
= remote_get_ada_task_ptid
;
10899 remote_ops
.to_stop
= remote_stop
;
10900 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
10901 remote_ops
.to_rcmd
= remote_rcmd
;
10902 remote_ops
.to_log_command
= serial_log_command
;
10903 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
10904 remote_ops
.to_stratum
= process_stratum
;
10905 remote_ops
.to_has_all_memory
= default_child_has_all_memory
;
10906 remote_ops
.to_has_memory
= default_child_has_memory
;
10907 remote_ops
.to_has_stack
= default_child_has_stack
;
10908 remote_ops
.to_has_registers
= default_child_has_registers
;
10909 remote_ops
.to_has_execution
= default_child_has_execution
;
10910 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
10911 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
10912 remote_ops
.to_magic
= OPS_MAGIC
;
10913 remote_ops
.to_memory_map
= remote_memory_map
;
10914 remote_ops
.to_flash_erase
= remote_flash_erase
;
10915 remote_ops
.to_flash_done
= remote_flash_done
;
10916 remote_ops
.to_read_description
= remote_read_description
;
10917 remote_ops
.to_search_memory
= remote_search_memory
;
10918 remote_ops
.to_can_async_p
= remote_can_async_p
;
10919 remote_ops
.to_is_async_p
= remote_is_async_p
;
10920 remote_ops
.to_async
= remote_async
;
10921 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
10922 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
10923 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
10924 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
10925 remote_ops
.to_supports_disable_randomization
10926 = remote_supports_disable_randomization
;
10927 remote_ops
.to_fileio_open
= remote_hostio_open
;
10928 remote_ops
.to_fileio_pwrite
= remote_hostio_pwrite
;
10929 remote_ops
.to_fileio_pread
= remote_hostio_pread
;
10930 remote_ops
.to_fileio_close
= remote_hostio_close
;
10931 remote_ops
.to_fileio_unlink
= remote_hostio_unlink
;
10932 remote_ops
.to_fileio_readlink
= remote_hostio_readlink
;
10933 remote_ops
.to_supports_enable_disable_tracepoint
= remote_supports_enable_disable_tracepoint
;
10934 remote_ops
.to_supports_string_tracing
= remote_supports_string_tracing
;
10935 remote_ops
.to_supports_evaluation_of_breakpoint_conditions
= remote_supports_cond_breakpoints
;
10936 remote_ops
.to_trace_init
= remote_trace_init
;
10937 remote_ops
.to_download_tracepoint
= remote_download_tracepoint
;
10938 remote_ops
.to_can_download_tracepoint
= remote_can_download_tracepoint
;
10939 remote_ops
.to_download_trace_state_variable
10940 = remote_download_trace_state_variable
;
10941 remote_ops
.to_enable_tracepoint
= remote_enable_tracepoint
;
10942 remote_ops
.to_disable_tracepoint
= remote_disable_tracepoint
;
10943 remote_ops
.to_trace_set_readonly_regions
= remote_trace_set_readonly_regions
;
10944 remote_ops
.to_trace_start
= remote_trace_start
;
10945 remote_ops
.to_get_trace_status
= remote_get_trace_status
;
10946 remote_ops
.to_get_tracepoint_status
= remote_get_tracepoint_status
;
10947 remote_ops
.to_trace_stop
= remote_trace_stop
;
10948 remote_ops
.to_trace_find
= remote_trace_find
;
10949 remote_ops
.to_get_trace_state_variable_value
10950 = remote_get_trace_state_variable_value
;
10951 remote_ops
.to_save_trace_data
= remote_save_trace_data
;
10952 remote_ops
.to_upload_tracepoints
= remote_upload_tracepoints
;
10953 remote_ops
.to_upload_trace_state_variables
10954 = remote_upload_trace_state_variables
;
10955 remote_ops
.to_get_raw_trace_data
= remote_get_raw_trace_data
;
10956 remote_ops
.to_get_min_fast_tracepoint_insn_len
= remote_get_min_fast_tracepoint_insn_len
;
10957 remote_ops
.to_set_disconnected_tracing
= remote_set_disconnected_tracing
;
10958 remote_ops
.to_set_circular_trace_buffer
= remote_set_circular_trace_buffer
;
10959 remote_ops
.to_set_trace_notes
= remote_set_trace_notes
;
10960 remote_ops
.to_core_of_thread
= remote_core_of_thread
;
10961 remote_ops
.to_verify_memory
= remote_verify_memory
;
10962 remote_ops
.to_get_tib_address
= remote_get_tib_address
;
10963 remote_ops
.to_set_permissions
= remote_set_permissions
;
10964 remote_ops
.to_static_tracepoint_marker_at
10965 = remote_static_tracepoint_marker_at
;
10966 remote_ops
.to_static_tracepoint_markers_by_strid
10967 = remote_static_tracepoint_markers_by_strid
;
10968 remote_ops
.to_traceframe_info
= remote_traceframe_info
;
10969 remote_ops
.to_use_agent
= remote_use_agent
;
10970 remote_ops
.to_can_use_agent
= remote_can_use_agent
;
10973 /* Set up the extended remote vector by making a copy of the standard
10974 remote vector and adding to it. */
10977 init_extended_remote_ops (void)
10979 extended_remote_ops
= remote_ops
;
10981 extended_remote_ops
.to_shortname
= "extended-remote";
10982 extended_remote_ops
.to_longname
=
10983 "Extended remote serial target in gdb-specific protocol";
10984 extended_remote_ops
.to_doc
=
10985 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
10986 Specify the serial device it is connected to (e.g. /dev/ttya).";
10987 extended_remote_ops
.to_open
= extended_remote_open
;
10988 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
10989 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
10990 extended_remote_ops
.to_detach
= extended_remote_detach
;
10991 extended_remote_ops
.to_attach
= extended_remote_attach
;
10992 extended_remote_ops
.to_kill
= extended_remote_kill
;
10993 extended_remote_ops
.to_supports_disable_randomization
10994 = extended_remote_supports_disable_randomization
;
10998 remote_can_async_p (void)
11000 if (!target_async_permitted
)
11001 /* We only enable async when the user specifically asks for it. */
11004 /* We're async whenever the serial device is. */
11005 return serial_can_async_p (remote_desc
);
11009 remote_is_async_p (void)
11011 if (!target_async_permitted
)
11012 /* We only enable async when the user specifically asks for it. */
11015 /* We're async whenever the serial device is. */
11016 return serial_is_async_p (remote_desc
);
11019 /* Pass the SERIAL event on and up to the client. One day this code
11020 will be able to delay notifying the client of an event until the
11021 point where an entire packet has been received. */
11023 static void (*async_client_callback
) (enum inferior_event_type event_type
,
11025 static void *async_client_context
;
11026 static serial_event_ftype remote_async_serial_handler
;
11029 remote_async_serial_handler (struct serial
*scb
, void *context
)
11031 /* Don't propogate error information up to the client. Instead let
11032 the client find out about the error by querying the target. */
11033 async_client_callback (INF_REG_EVENT
, async_client_context
);
11037 remote_async_inferior_event_handler (gdb_client_data data
)
11039 inferior_event_handler (INF_REG_EVENT
, NULL
);
11043 remote_async_get_pending_events_handler (gdb_client_data data
)
11045 remote_get_pending_stop_replies ();
11049 remote_async (void (*callback
) (enum inferior_event_type event_type
,
11050 void *context
), void *context
)
11052 if (callback
!= NULL
)
11054 serial_async (remote_desc
, remote_async_serial_handler
, NULL
);
11055 async_client_callback
= callback
;
11056 async_client_context
= context
;
11059 serial_async (remote_desc
, NULL
, NULL
);
11063 set_remote_cmd (char *args
, int from_tty
)
11065 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
11069 show_remote_cmd (char *args
, int from_tty
)
11071 /* We can't just use cmd_show_list here, because we want to skip
11072 the redundant "show remote Z-packet" and the legacy aliases. */
11073 struct cleanup
*showlist_chain
;
11074 struct cmd_list_element
*list
= remote_show_cmdlist
;
11075 struct ui_out
*uiout
= current_uiout
;
11077 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
11078 for (; list
!= NULL
; list
= list
->next
)
11079 if (strcmp (list
->name
, "Z-packet") == 0)
11081 else if (list
->type
== not_set_cmd
)
11082 /* Alias commands are exactly like the original, except they
11083 don't have the normal type. */
11087 struct cleanup
*option_chain
11088 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
11090 ui_out_field_string (uiout
, "name", list
->name
);
11091 ui_out_text (uiout
, ": ");
11092 if (list
->type
== show_cmd
)
11093 do_setshow_command ((char *) NULL
, from_tty
, list
);
11095 cmd_func (list
, NULL
, from_tty
);
11096 /* Close the tuple. */
11097 do_cleanups (option_chain
);
11100 /* Close the tuple. */
11101 do_cleanups (showlist_chain
);
11105 /* Function to be called whenever a new objfile (shlib) is detected. */
11107 remote_new_objfile (struct objfile
*objfile
)
11109 if (remote_desc
!= 0) /* Have a remote connection. */
11110 remote_check_symbols (objfile
);
11113 /* Pull all the tracepoints defined on the target and create local
11114 data structures representing them. We don't want to create real
11115 tracepoints yet, we don't want to mess up the user's existing
11119 remote_upload_tracepoints (struct uploaded_tp
**utpp
)
11121 struct remote_state
*rs
= get_remote_state ();
11124 /* Ask for a first packet of tracepoint definition. */
11126 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11128 while (*p
&& *p
!= 'l')
11130 parse_tracepoint_definition (p
, utpp
);
11131 /* Ask for another packet of tracepoint definition. */
11133 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11140 remote_upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
11142 struct remote_state
*rs
= get_remote_state ();
11145 /* Ask for a first packet of variable definition. */
11147 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11149 while (*p
&& *p
!= 'l')
11151 parse_tsv_definition (p
, utsvp
);
11152 /* Ask for another packet of variable definition. */
11154 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11161 _initialize_remote (void)
11163 struct remote_state
*rs
;
11164 struct cmd_list_element
*cmd
;
11167 /* architecture specific data */
11168 remote_gdbarch_data_handle
=
11169 gdbarch_data_register_post_init (init_remote_state
);
11170 remote_g_packet_data_handle
=
11171 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
11173 /* Initialize the per-target state. At the moment there is only one
11174 of these, not one per target. Only one target is active at a
11175 time. The default buffer size is unimportant; it will be expanded
11176 whenever a larger buffer is needed. */
11177 rs
= get_remote_state_raw ();
11178 rs
->buf_size
= 400;
11179 rs
->buf
= xmalloc (rs
->buf_size
);
11181 init_remote_ops ();
11182 add_target (&remote_ops
);
11184 init_extended_remote_ops ();
11185 add_target (&extended_remote_ops
);
11187 /* Hook into new objfile notification. */
11188 observer_attach_new_objfile (remote_new_objfile
);
11190 /* Set up signal handlers. */
11191 sigint_remote_token
=
11192 create_async_signal_handler (async_remote_interrupt
, NULL
);
11193 sigint_remote_twice_token
=
11194 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
11197 init_remote_threadtests ();
11200 /* set/show remote ... */
11202 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
11203 Remote protocol specific variables\n\
11204 Configure various remote-protocol specific variables such as\n\
11205 the packets being used"),
11206 &remote_set_cmdlist
, "set remote ",
11207 0 /* allow-unknown */, &setlist
);
11208 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
11209 Remote protocol specific variables\n\
11210 Configure various remote-protocol specific variables such as\n\
11211 the packets being used"),
11212 &remote_show_cmdlist
, "show remote ",
11213 0 /* allow-unknown */, &showlist
);
11215 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
11216 Compare section data on target to the exec file.\n\
11217 Argument is a single section name (default: all loaded sections)."),
11220 add_cmd ("packet", class_maintenance
, packet_command
, _("\
11221 Send an arbitrary packet to a remote target.\n\
11222 maintenance packet TEXT\n\
11223 If GDB is talking to an inferior via the GDB serial protocol, then\n\
11224 this command sends the string TEXT to the inferior, and displays the\n\
11225 response packet. GDB supplies the initial `$' character, and the\n\
11226 terminating `#' character and checksum."),
11229 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
11230 Set whether to send break if interrupted."), _("\
11231 Show whether to send break if interrupted."), _("\
11232 If set, a break, instead of a cntrl-c, is sent to the remote target."),
11233 set_remotebreak
, show_remotebreak
,
11234 &setlist
, &showlist
);
11235 cmd_name
= "remotebreak";
11236 cmd
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
11237 deprecate_cmd (cmd
, "set remote interrupt-sequence");
11238 cmd_name
= "remotebreak"; /* needed because lookup_cmd updates the pointer */
11239 cmd
= lookup_cmd (&cmd_name
, showlist
, "", -1, 1);
11240 deprecate_cmd (cmd
, "show remote interrupt-sequence");
11242 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
11243 interrupt_sequence_modes
, &interrupt_sequence_mode
,
11245 Set interrupt sequence to remote target."), _("\
11246 Show interrupt sequence to remote target."), _("\
11247 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
11248 NULL
, show_interrupt_sequence
,
11249 &remote_set_cmdlist
,
11250 &remote_show_cmdlist
);
11252 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
11253 &interrupt_on_connect
, _("\
11254 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11255 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11256 If set, interrupt sequence is sent to remote target."),
11258 &remote_set_cmdlist
, &remote_show_cmdlist
);
11260 /* Install commands for configuring memory read/write packets. */
11262 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
11263 Set the maximum number of bytes per memory write packet (deprecated)."),
11265 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
11266 Show the maximum number of bytes per memory write packet (deprecated)."),
11268 add_cmd ("memory-write-packet-size", no_class
,
11269 set_memory_write_packet_size
, _("\
11270 Set the maximum number of bytes per memory-write packet.\n\
11271 Specify the number of bytes in a packet or 0 (zero) for the\n\
11272 default packet size. The actual limit is further reduced\n\
11273 dependent on the target. Specify ``fixed'' to disable the\n\
11274 further restriction and ``limit'' to enable that restriction."),
11275 &remote_set_cmdlist
);
11276 add_cmd ("memory-read-packet-size", no_class
,
11277 set_memory_read_packet_size
, _("\
11278 Set the maximum number of bytes per memory-read packet.\n\
11279 Specify the number of bytes in a packet or 0 (zero) for the\n\
11280 default packet size. The actual limit is further reduced\n\
11281 dependent on the target. Specify ``fixed'' to disable the\n\
11282 further restriction and ``limit'' to enable that restriction."),
11283 &remote_set_cmdlist
);
11284 add_cmd ("memory-write-packet-size", no_class
,
11285 show_memory_write_packet_size
,
11286 _("Show the maximum number of bytes per memory-write packet."),
11287 &remote_show_cmdlist
);
11288 add_cmd ("memory-read-packet-size", no_class
,
11289 show_memory_read_packet_size
,
11290 _("Show the maximum number of bytes per memory-read packet."),
11291 &remote_show_cmdlist
);
11293 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
11294 &remote_hw_watchpoint_limit
, _("\
11295 Set the maximum number of target hardware watchpoints."), _("\
11296 Show the maximum number of target hardware watchpoints."), _("\
11297 Specify a negative limit for unlimited."),
11298 NULL
, NULL
, /* FIXME: i18n: The maximum
11299 number of target hardware
11300 watchpoints is %s. */
11301 &remote_set_cmdlist
, &remote_show_cmdlist
);
11302 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class
,
11303 &remote_hw_watchpoint_length_limit
, _("\
11304 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
11305 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
11306 Specify a negative limit for unlimited."),
11307 NULL
, NULL
, /* FIXME: i18n: The maximum
11308 length (in bytes) of a target
11309 hardware watchpoint is %s. */
11310 &remote_set_cmdlist
, &remote_show_cmdlist
);
11311 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
11312 &remote_hw_breakpoint_limit
, _("\
11313 Set the maximum number of target hardware breakpoints."), _("\
11314 Show the maximum number of target hardware breakpoints."), _("\
11315 Specify a negative limit for unlimited."),
11316 NULL
, NULL
, /* FIXME: i18n: The maximum
11317 number of target hardware
11318 breakpoints is %s. */
11319 &remote_set_cmdlist
, &remote_show_cmdlist
);
11321 add_setshow_integer_cmd ("remoteaddresssize", class_obscure
,
11322 &remote_address_size
, _("\
11323 Set the maximum size of the address (in bits) in a memory packet."), _("\
11324 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
11326 NULL
, /* FIXME: i18n: */
11327 &setlist
, &showlist
);
11329 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
11330 "X", "binary-download", 1);
11332 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
11333 "vCont", "verbose-resume", 0);
11335 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
11336 "QPassSignals", "pass-signals", 0);
11338 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QProgramSignals
],
11339 "QProgramSignals", "program-signals", 0);
11341 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
11342 "qSymbol", "symbol-lookup", 0);
11344 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
11345 "P", "set-register", 1);
11347 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
11348 "p", "fetch-register", 1);
11350 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
11351 "Z0", "software-breakpoint", 0);
11353 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
11354 "Z1", "hardware-breakpoint", 0);
11356 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
11357 "Z2", "write-watchpoint", 0);
11359 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
11360 "Z3", "read-watchpoint", 0);
11362 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
11363 "Z4", "access-watchpoint", 0);
11365 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
11366 "qXfer:auxv:read", "read-aux-vector", 0);
11368 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
11369 "qXfer:features:read", "target-features", 0);
11371 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
11372 "qXfer:libraries:read", "library-info", 0);
11374 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries_svr4
],
11375 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
11377 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
11378 "qXfer:memory-map:read", "memory-map", 0);
11380 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
11381 "qXfer:spu:read", "read-spu-object", 0);
11383 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
11384 "qXfer:spu:write", "write-spu-object", 0);
11386 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
11387 "qXfer:osdata:read", "osdata", 0);
11389 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_threads
],
11390 "qXfer:threads:read", "threads", 0);
11392 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_read
],
11393 "qXfer:siginfo:read", "read-siginfo-object", 0);
11395 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_write
],
11396 "qXfer:siginfo:write", "write-siginfo-object", 0);
11398 add_packet_config_cmd
11399 (&remote_protocol_packets
[PACKET_qXfer_traceframe_info
],
11400 "qXfer:trace-frame-info:read", "traceframe-info", 0);
11402 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_uib
],
11403 "qXfer:uib:read", "unwind-info-block", 0);
11405 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
11406 "qGetTLSAddr", "get-thread-local-storage-address",
11409 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTIBAddr
],
11410 "qGetTIBAddr", "get-thread-information-block-address",
11413 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bc
],
11414 "bc", "reverse-continue", 0);
11416 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bs
],
11417 "bs", "reverse-step", 0);
11419 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
11420 "qSupported", "supported-packets", 0);
11422 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
11423 "qSearch:memory", "search-memory", 0);
11425 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
11426 "vFile:open", "hostio-open", 0);
11428 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
11429 "vFile:pread", "hostio-pread", 0);
11431 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
11432 "vFile:pwrite", "hostio-pwrite", 0);
11434 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
11435 "vFile:close", "hostio-close", 0);
11437 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
11438 "vFile:unlink", "hostio-unlink", 0);
11440 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_readlink
],
11441 "vFile:readlink", "hostio-readlink", 0);
11443 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
11444 "vAttach", "attach", 0);
11446 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
11449 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
11450 "QStartNoAckMode", "noack", 0);
11452 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
11453 "vKill", "kill", 0);
11455 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qAttached
],
11456 "qAttached", "query-attached", 0);
11458 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalTracepoints
],
11459 "ConditionalTracepoints",
11460 "conditional-tracepoints", 0);
11462 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalBreakpoints
],
11463 "ConditionalBreakpoints",
11464 "conditional-breakpoints", 0);
11466 add_packet_config_cmd (&remote_protocol_packets
[PACKET_FastTracepoints
],
11467 "FastTracepoints", "fast-tracepoints", 0);
11469 add_packet_config_cmd (&remote_protocol_packets
[PACKET_TracepointSource
],
11470 "TracepointSource", "TracepointSource", 0);
11472 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAllow
],
11473 "QAllow", "allow", 0);
11475 add_packet_config_cmd (&remote_protocol_packets
[PACKET_StaticTracepoints
],
11476 "StaticTracepoints", "static-tracepoints", 0);
11478 add_packet_config_cmd (&remote_protocol_packets
[PACKET_InstallInTrace
],
11479 "InstallInTrace", "install-in-trace", 0);
11481 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_statictrace_read
],
11482 "qXfer:statictrace:read", "read-sdata-object", 0);
11484 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_fdpic
],
11485 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
11487 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QDisableRandomization
],
11488 "QDisableRandomization", "disable-randomization", 0);
11490 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAgent
],
11491 "QAgent", "agent", 0);
11493 /* Keep the old ``set remote Z-packet ...'' working. Each individual
11494 Z sub-packet has its own set and show commands, but users may
11495 have sets to this variable in their .gdbinit files (or in their
11497 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
11498 &remote_Z_packet_detect
, _("\
11499 Set use of remote protocol `Z' packets"), _("\
11500 Show use of remote protocol `Z' packets "), _("\
11501 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
11503 set_remote_protocol_Z_packet_cmd
,
11504 show_remote_protocol_Z_packet_cmd
,
11505 /* FIXME: i18n: Use of remote protocol
11506 `Z' packets is %s. */
11507 &remote_set_cmdlist
, &remote_show_cmdlist
);
11509 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
11510 Manipulate files on the remote system\n\
11511 Transfer files to and from the remote target system."),
11512 &remote_cmdlist
, "remote ",
11513 0 /* allow-unknown */, &cmdlist
);
11515 add_cmd ("put", class_files
, remote_put_command
,
11516 _("Copy a local file to the remote system."),
11519 add_cmd ("get", class_files
, remote_get_command
,
11520 _("Copy a remote file to the local system."),
11523 add_cmd ("delete", class_files
, remote_delete_command
,
11524 _("Delete a remote file."),
11527 remote_exec_file
= xstrdup ("");
11528 add_setshow_string_noescape_cmd ("exec-file", class_files
,
11529 &remote_exec_file
, _("\
11530 Set the remote pathname for \"run\""), _("\
11531 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
11532 &remote_set_cmdlist
, &remote_show_cmdlist
);
11534 /* Eventually initialize fileio. See fileio.c */
11535 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
11537 /* Take advantage of the fact that the LWP field is not used, to tag
11538 special ptids with it set to != 0. */
11539 magic_null_ptid
= ptid_build (42000, 1, -1);
11540 not_sent_ptid
= ptid_build (42000, 1, -2);
11541 any_thread_ptid
= ptid_build (42000, 1, 0);
11543 target_buf_size
= 2048;
11544 target_buf
= xmalloc (target_buf_size
);