1 /* Remote target communications for serial-line targets in custom GDB protocol
3 Copyright (C) 1988-2014 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. */
29 #include "exceptions.h"
31 /*#include "terminal.h" */
34 #include "gdb-stabs.h"
35 #include "gdbthread.h"
37 #include "remote-notif.h"
40 #include "gdb_assert.h"
43 #include "cli/cli-decode.h"
44 #include "cli/cli-setshow.h"
45 #include "target-descriptions.h"
47 #include "filestuff.h"
52 #include "event-loop.h"
53 #include "event-top.h"
59 #include "gdbcore.h" /* for exec_bfd */
61 #include "remote-fileio.h"
62 #include "gdb/fileio.h"
64 #include "xml-support.h"
66 #include "memory-map.h"
68 #include "tracepoint.h"
74 /* Temp hacks for tracepoint encoding migration. */
75 static char *target_buf
;
76 static long target_buf_size
;
78 /* The size to align memory write packets, when practical. The protocol
79 does not guarantee any alignment, and gdb will generate short
80 writes and unaligned writes, but even as a best-effort attempt this
81 can improve bulk transfers. For instance, if a write is misaligned
82 relative to the target's data bus, the stub may need to make an extra
83 round trip fetching data from the target. This doesn't make a
84 huge difference, but it's easy to do, so we try to be helpful.
86 The alignment chosen is arbitrary; usually data bus width is
87 important here, not the possibly larger cache line size. */
88 enum { REMOTE_ALIGN_WRITES
= 16 };
90 /* Prototypes for local functions. */
91 static void async_cleanup_sigint_signal_handler (void *dummy
);
92 static int getpkt_sane (char **buf
, long *sizeof_buf
, int forever
);
93 static int getpkt_or_notif_sane (char **buf
, long *sizeof_buf
,
94 int forever
, int *is_notif
);
96 static void async_handle_remote_sigint (int);
97 static void async_handle_remote_sigint_twice (int);
99 static void remote_files_info (struct target_ops
*ignore
);
101 static void remote_prepare_to_store (struct target_ops
*self
,
102 struct regcache
*regcache
);
104 static void remote_open (char *name
, int from_tty
);
106 static void extended_remote_open (char *name
, int from_tty
);
108 static void remote_open_1 (char *, int, struct target_ops
*, int extended_p
);
110 static void remote_close (struct target_ops
*self
);
112 static void remote_mourn (struct target_ops
*ops
);
114 static void extended_remote_restart (void);
116 static void extended_remote_mourn (struct target_ops
*);
118 static void remote_mourn_1 (struct target_ops
*);
120 static void remote_send (char **buf
, long *sizeof_buf_p
);
122 static int readchar (int timeout
);
124 static void remote_serial_write (const char *str
, int len
);
126 static void remote_kill (struct target_ops
*ops
);
128 static int remote_can_async_p (struct target_ops
*);
130 static int remote_is_async_p (struct target_ops
*);
132 static void remote_async (struct target_ops
*ops
,
133 void (*callback
) (enum inferior_event_type event_type
,
137 static void sync_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 (struct target_ops
*self
, ptid_t
);
158 static int stubhex (int ch
);
160 static int hexnumstr (char *, ULONGEST
);
162 static int hexnumnstr (char *, ULONGEST
, int);
164 static CORE_ADDR
remote_address_masked (CORE_ADDR
);
166 static void print_packet (char *);
168 static void compare_sections_command (char *, int);
170 static void packet_command (char *, int);
172 static int stub_unpack_int (char *buff
, int fieldlength
);
174 static ptid_t
remote_current_thread (ptid_t oldptid
);
176 static void remote_find_new_threads (void);
178 static int putpkt_binary (char *buf
, int cnt
);
180 static void check_binary_download (CORE_ADDR addr
);
182 struct packet_config
;
184 static void show_packet_config_cmd (struct packet_config
*config
);
186 static void update_packet_config (struct packet_config
*config
);
188 static void set_remote_protocol_packet_cmd (char *args
, int from_tty
,
189 struct cmd_list_element
*c
);
191 static void show_remote_protocol_packet_cmd (struct ui_file
*file
,
193 struct cmd_list_element
*c
,
196 static char *write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
);
197 static ptid_t
read_ptid (char *buf
, char **obuf
);
199 static void remote_set_permissions (struct target_ops
*self
);
202 static int remote_get_trace_status (struct target_ops
*self
,
203 struct trace_status
*ts
);
205 static int remote_upload_tracepoints (struct target_ops
*self
,
206 struct uploaded_tp
**utpp
);
208 static int remote_upload_trace_state_variables (struct target_ops
*self
,
209 struct uploaded_tsv
**utsvp
);
211 static void remote_query_supported (void);
213 static void remote_check_symbols (void);
215 void _initialize_remote (void);
218 static void stop_reply_xfree (struct stop_reply
*);
219 static void remote_parse_stop_reply (char *, struct stop_reply
*);
220 static void push_stop_reply (struct stop_reply
*);
221 static void discard_pending_stop_replies_in_queue (struct remote_state
*);
222 static int peek_stop_reply (ptid_t ptid
);
224 static void remote_async_inferior_event_handler (gdb_client_data
);
226 static void remote_terminal_ours (struct target_ops
*self
);
228 static int remote_read_description_p (struct target_ops
*target
);
230 static void remote_console_output (char *msg
);
232 static int remote_supports_cond_breakpoints (struct target_ops
*self
);
234 static int remote_can_run_breakpoint_commands (struct target_ops
*self
);
238 static struct cmd_list_element
*remote_cmdlist
;
240 /* For "set remote" and "show remote". */
242 static struct cmd_list_element
*remote_set_cmdlist
;
243 static struct cmd_list_element
*remote_show_cmdlist
;
245 /* Stub vCont actions support.
247 Each field is a boolean flag indicating whether the stub reports
248 support for the corresponding action. */
250 struct vCont_action_support
259 /* Controls whether GDB is willing to use range stepping. */
261 static int use_range_stepping
= 1;
263 #define OPAQUETHREADBYTES 8
265 /* a 64 bit opaque identifier */
266 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
268 /* About this many threadisds fit in a packet. */
270 #define MAXTHREADLISTRESULTS 32
272 /* Description of the remote protocol state for the currently
273 connected target. This is per-target state, and independent of the
274 selected architecture. */
278 /* A buffer to use for incoming packets, and its current size. The
279 buffer is grown dynamically for larger incoming packets.
280 Outgoing packets may also be constructed in this buffer.
281 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
282 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
287 /* True if we're going through initial connection setup (finding out
288 about the remote side's threads, relocating symbols, etc.). */
291 /* If we negotiated packet size explicitly (and thus can bypass
292 heuristics for the largest packet size that will not overflow
293 a buffer in the stub), this will be set to that packet size.
294 Otherwise zero, meaning to use the guessed size. */
295 long explicit_packet_size
;
297 /* remote_wait is normally called when the target is running and
298 waits for a stop reply packet. But sometimes we need to call it
299 when the target is already stopped. We can send a "?" packet
300 and have remote_wait read the response. Or, if we already have
301 the response, we can stash it in BUF and tell remote_wait to
302 skip calling getpkt. This flag is set when BUF contains a
303 stop reply packet and the target is not waiting. */
304 int cached_wait_status
;
306 /* True, if in no ack mode. That is, neither GDB nor the stub will
307 expect acks from each other. The connection is assumed to be
311 /* True if we're connected in extended remote mode. */
314 /* True if the stub reported support for multi-process
316 int multi_process_aware
;
318 /* True if we resumed the target and we're waiting for the target to
319 stop. In the mean time, we can't start another command/query.
320 The remote server wouldn't be ready to process it, so we'd
321 timeout waiting for a reply that would never come and eventually
322 we'd close the connection. This can happen in asynchronous mode
323 because we allow GDB commands while the target is running. */
324 int waiting_for_stop_reply
;
326 /* True if the stub reports support for non-stop mode. */
329 /* The status of the stub support for the various vCont actions. */
330 struct vCont_action_support supports_vCont
;
332 /* True if the stub reports support for conditional tracepoints. */
333 int cond_tracepoints
;
335 /* True if the stub reports support for target-side breakpoint
337 int cond_breakpoints
;
339 /* True if the stub reports support for target-side breakpoint
341 int breakpoint_commands
;
343 /* True if the stub reports support for fast tracepoints. */
344 int fast_tracepoints
;
346 /* True if the stub reports support for static tracepoints. */
347 int static_tracepoints
;
349 /* True if the stub reports support for installing tracepoint while
351 int install_in_trace
;
353 /* True if the stub can continue running a trace while GDB is
355 int disconnected_tracing
;
357 /* True if the stub reports support for enabling and disabling
358 tracepoints while a trace experiment is running. */
359 int enable_disable_tracepoints
;
361 /* True if the stub can collect strings using tracenz bytecode. */
364 /* True if the stub supports qXfer:libraries-svr4:read with a
366 int augmented_libraries_svr4_read
;
368 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
369 responded to that. */
372 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
373 remote_open knows that we don't have a file open when the program
375 struct serial
*remote_desc
;
377 /* These are the threads which we last sent to the remote system. The
378 TID member will be -1 for all or -2 for not sent yet. */
379 ptid_t general_thread
;
380 ptid_t continue_thread
;
382 /* This is the traceframe which we last selected on the remote system.
383 It will be -1 if no traceframe is selected. */
384 int remote_traceframe_number
;
386 char *last_pass_packet
;
388 /* The last QProgramSignals packet sent to the target. We bypass
389 sending a new program signals list down to the target if the new
390 packet is exactly the same as the last we sent. IOW, we only let
391 the target know about program signals list changes. */
392 char *last_program_signals_packet
;
394 enum gdb_signal last_sent_signal
;
398 char *finished_object
;
399 char *finished_annex
;
400 ULONGEST finished_offset
;
402 /* Should we try the 'ThreadInfo' query packet?
404 This variable (NOT available to the user: auto-detect only!)
405 determines whether GDB will use the new, simpler "ThreadInfo"
406 query or the older, more complex syntax for thread queries.
407 This is an auto-detect variable (set to true at each connect,
408 and set to false when the target fails to recognize it). */
409 int use_threadinfo_query
;
410 int use_threadextra_query
;
412 void (*async_client_callback
) (enum inferior_event_type event_type
,
414 void *async_client_context
;
416 /* This is set to the data address of the access causing the target
417 to stop for a watchpoint. */
418 CORE_ADDR remote_watch_data_address
;
420 /* This is non-zero if target stopped for a watchpoint. */
421 int remote_stopped_by_watchpoint_p
;
423 threadref echo_nextthread
;
424 threadref nextthread
;
425 threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
427 /* The state of remote notification. */
428 struct remote_notif_state
*notif_state
;
431 /* Private data that we'll store in (struct thread_info)->private. */
432 struct private_thread_info
439 free_private_thread_info (struct private_thread_info
*info
)
445 /* Returns true if the multi-process extensions are in effect. */
447 remote_multi_process_p (struct remote_state
*rs
)
449 return rs
->multi_process_aware
;
452 /* This data could be associated with a target, but we do not always
453 have access to the current target when we need it, so for now it is
454 static. This will be fine for as long as only one target is in use
456 static struct remote_state
*remote_state
;
458 static struct remote_state
*
459 get_remote_state_raw (void)
464 /* Allocate a new struct remote_state with xmalloc, initialize it, and
467 static struct remote_state
*
468 new_remote_state (void)
470 struct remote_state
*result
= XCNEW (struct remote_state
);
472 /* The default buffer size is unimportant; it will be expanded
473 whenever a larger buffer is needed. */
474 result
->buf_size
= 400;
475 result
->buf
= xmalloc (result
->buf_size
);
476 result
->remote_traceframe_number
= -1;
477 result
->last_sent_signal
= GDB_SIGNAL_0
;
482 /* Description of the remote protocol for a given architecture. */
486 long offset
; /* Offset into G packet. */
487 long regnum
; /* GDB's internal register number. */
488 LONGEST pnum
; /* Remote protocol register number. */
489 int in_g_packet
; /* Always part of G packet. */
490 /* long size in bytes; == register_size (target_gdbarch (), regnum);
492 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
496 struct remote_arch_state
498 /* Description of the remote protocol registers. */
499 long sizeof_g_packet
;
501 /* Description of the remote protocol registers indexed by REGNUM
502 (making an array gdbarch_num_regs in size). */
503 struct packet_reg
*regs
;
505 /* This is the size (in chars) of the first response to the ``g''
506 packet. It is used as a heuristic when determining the maximum
507 size of memory-read and memory-write packets. A target will
508 typically only reserve a buffer large enough to hold the ``g''
509 packet. The size does not include packet overhead (headers and
511 long actual_register_packet_size
;
513 /* This is the maximum size (in chars) of a non read/write packet.
514 It is also used as a cap on the size of read/write packets. */
515 long remote_packet_size
;
518 /* Utility: generate error from an incoming stub packet. */
520 trace_error (char *buf
)
523 return; /* not an error msg */
526 case '1': /* malformed packet error */
527 if (*++buf
== '0') /* general case: */
528 error (_("remote.c: error in outgoing packet."));
530 error (_("remote.c: error in outgoing packet at field #%ld."),
531 strtol (buf
, NULL
, 16));
533 error (_("Target returns error code '%s'."), buf
);
537 /* Utility: wait for reply from stub, while accepting "O" packets. */
539 remote_get_noisy_reply (char **buf_p
,
542 do /* Loop on reply from remote stub. */
546 QUIT
; /* Allow user to bail out with ^C. */
547 getpkt (buf_p
, sizeof_buf
, 0);
551 else if (strncmp (buf
, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
554 CORE_ADDR from
, to
, org_to
;
556 int adjusted_size
= 0;
557 volatile struct gdb_exception ex
;
559 p
= buf
+ strlen ("qRelocInsn:");
560 pp
= unpack_varlen_hex (p
, &ul
);
562 error (_("invalid qRelocInsn packet: %s"), buf
);
566 unpack_varlen_hex (p
, &ul
);
571 TRY_CATCH (ex
, RETURN_MASK_ALL
)
573 gdbarch_relocate_instruction (target_gdbarch (), &to
, from
);
577 adjusted_size
= to
- org_to
;
579 xsnprintf (buf
, *sizeof_buf
, "qRelocInsn:%x", adjusted_size
);
582 else if (ex
.reason
< 0 && ex
.error
== MEMORY_ERROR
)
584 /* Propagate memory errors silently back to the target.
585 The stub may have limited the range of addresses we
586 can write to, for example. */
591 /* Something unexpectedly bad happened. Be verbose so
592 we can tell what, and propagate the error back to the
593 stub, so it doesn't get stuck waiting for a
595 exception_fprintf (gdb_stderr
, ex
,
596 _("warning: relocating instruction: "));
600 else if (buf
[0] == 'O' && buf
[1] != 'K')
601 remote_console_output (buf
+ 1); /* 'O' message from stub */
603 return buf
; /* Here's the actual reply. */
608 /* Handle for retreving the remote protocol data from gdbarch. */
609 static struct gdbarch_data
*remote_gdbarch_data_handle
;
611 static struct remote_arch_state
*
612 get_remote_arch_state (void)
614 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle
);
617 /* Fetch the global remote target state. */
619 static struct remote_state
*
620 get_remote_state (void)
622 /* Make sure that the remote architecture state has been
623 initialized, because doing so might reallocate rs->buf. Any
624 function which calls getpkt also needs to be mindful of changes
625 to rs->buf, but this call limits the number of places which run
627 get_remote_arch_state ();
629 return get_remote_state_raw ();
633 compare_pnums (const void *lhs_
, const void *rhs_
)
635 const struct packet_reg
* const *lhs
= lhs_
;
636 const struct packet_reg
* const *rhs
= rhs_
;
638 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
640 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
647 map_regcache_remote_table (struct gdbarch
*gdbarch
, struct packet_reg
*regs
)
649 int regnum
, num_remote_regs
, offset
;
650 struct packet_reg
**remote_regs
;
652 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
654 struct packet_reg
*r
= ®s
[regnum
];
656 if (register_size (gdbarch
, regnum
) == 0)
657 /* Do not try to fetch zero-sized (placeholder) registers. */
660 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
665 /* Define the g/G packet format as the contents of each register
666 with a remote protocol number, in order of ascending protocol
669 remote_regs
= alloca (gdbarch_num_regs (gdbarch
)
670 * sizeof (struct packet_reg
*));
671 for (num_remote_regs
= 0, regnum
= 0;
672 regnum
< gdbarch_num_regs (gdbarch
);
674 if (regs
[regnum
].pnum
!= -1)
675 remote_regs
[num_remote_regs
++] = ®s
[regnum
];
677 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
680 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
682 remote_regs
[regnum
]->in_g_packet
= 1;
683 remote_regs
[regnum
]->offset
= offset
;
684 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
690 /* Given the architecture described by GDBARCH, return the remote
691 protocol register's number and the register's offset in the g/G
692 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
693 If the target does not have a mapping for REGNUM, return false,
694 otherwise, return true. */
697 remote_register_number_and_offset (struct gdbarch
*gdbarch
, int regnum
,
698 int *pnum
, int *poffset
)
701 struct packet_reg
*regs
;
702 struct cleanup
*old_chain
;
704 gdb_assert (regnum
< gdbarch_num_regs (gdbarch
));
706 regs
= xcalloc (gdbarch_num_regs (gdbarch
), sizeof (struct packet_reg
));
707 old_chain
= make_cleanup (xfree
, regs
);
709 sizeof_g_packet
= map_regcache_remote_table (gdbarch
, regs
);
711 *pnum
= regs
[regnum
].pnum
;
712 *poffset
= regs
[regnum
].offset
;
714 do_cleanups (old_chain
);
720 init_remote_state (struct gdbarch
*gdbarch
)
722 struct remote_state
*rs
= get_remote_state_raw ();
723 struct remote_arch_state
*rsa
;
725 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
727 /* Use the architecture to build a regnum<->pnum table, which will be
728 1:1 unless a feature set specifies otherwise. */
729 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
,
730 gdbarch_num_regs (gdbarch
),
733 /* Record the maximum possible size of the g packet - it may turn out
735 rsa
->sizeof_g_packet
= map_regcache_remote_table (gdbarch
, rsa
->regs
);
737 /* Default maximum number of characters in a packet body. Many
738 remote stubs have a hardwired buffer size of 400 bytes
739 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
740 as the maximum packet-size to ensure that the packet and an extra
741 NUL character can always fit in the buffer. This stops GDB
742 trashing stubs that try to squeeze an extra NUL into what is
743 already a full buffer (As of 1999-12-04 that was most stubs). */
744 rsa
->remote_packet_size
= 400 - 1;
746 /* This one is filled in when a ``g'' packet is received. */
747 rsa
->actual_register_packet_size
= 0;
749 /* Should rsa->sizeof_g_packet needs more space than the
750 default, adjust the size accordingly. Remember that each byte is
751 encoded as two characters. 32 is the overhead for the packet
752 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
753 (``$NN:G...#NN'') is a better guess, the below has been padded a
755 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
756 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
758 /* Make sure that the packet buffer is plenty big enough for
759 this architecture. */
760 if (rs
->buf_size
< rsa
->remote_packet_size
)
762 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
763 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
769 /* Return the current allowed size of a remote packet. This is
770 inferred from the current architecture, and should be used to
771 limit the length of outgoing packets. */
773 get_remote_packet_size (void)
775 struct remote_state
*rs
= get_remote_state ();
776 struct remote_arch_state
*rsa
= get_remote_arch_state ();
778 if (rs
->explicit_packet_size
)
779 return rs
->explicit_packet_size
;
781 return rsa
->remote_packet_size
;
784 static struct packet_reg
*
785 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
787 if (regnum
< 0 && regnum
>= gdbarch_num_regs (target_gdbarch ()))
791 struct packet_reg
*r
= &rsa
->regs
[regnum
];
793 gdb_assert (r
->regnum
== regnum
);
798 static struct packet_reg
*
799 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
803 for (i
= 0; i
< gdbarch_num_regs (target_gdbarch ()); i
++)
805 struct packet_reg
*r
= &rsa
->regs
[i
];
813 static struct target_ops remote_ops
;
815 static struct target_ops extended_remote_ops
;
817 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
818 ``forever'' still use the normal timeout mechanism. This is
819 currently used by the ASYNC code to guarentee that target reads
820 during the initial connect always time-out. Once getpkt has been
821 modified to return a timeout indication and, in turn
822 remote_wait()/wait_for_inferior() have gained a timeout parameter
824 static int wait_forever_enabled_p
= 1;
826 /* Allow the user to specify what sequence to send to the remote
827 when he requests a program interruption: Although ^C is usually
828 what remote systems expect (this is the default, here), it is
829 sometimes preferable to send a break. On other systems such
830 as the Linux kernel, a break followed by g, which is Magic SysRq g
831 is required in order to interrupt the execution. */
832 const char interrupt_sequence_control_c
[] = "Ctrl-C";
833 const char interrupt_sequence_break
[] = "BREAK";
834 const char interrupt_sequence_break_g
[] = "BREAK-g";
835 static const char *const interrupt_sequence_modes
[] =
837 interrupt_sequence_control_c
,
838 interrupt_sequence_break
,
839 interrupt_sequence_break_g
,
842 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
845 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
846 struct cmd_list_element
*c
,
849 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
850 fprintf_filtered (file
,
851 _("Send the ASCII ETX character (Ctrl-c) "
852 "to the remote target to interrupt the "
853 "execution of the program.\n"));
854 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
855 fprintf_filtered (file
,
856 _("send a break signal to the remote target "
857 "to interrupt the execution of the program.\n"));
858 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
859 fprintf_filtered (file
,
860 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
861 "the remote target to interrupt the execution "
862 "of Linux kernel.\n"));
864 internal_error (__FILE__
, __LINE__
,
865 _("Invalid value for interrupt_sequence_mode: %s."),
866 interrupt_sequence_mode
);
869 /* This boolean variable specifies whether interrupt_sequence is sent
870 to the remote target when gdb connects to it.
871 This is mostly needed when you debug the Linux kernel: The Linux kernel
872 expects BREAK g which is Magic SysRq g for connecting gdb. */
873 static int interrupt_on_connect
= 0;
875 /* This variable is used to implement the "set/show remotebreak" commands.
876 Since these commands are now deprecated in favor of "set/show remote
877 interrupt-sequence", it no longer has any effect on the code. */
878 static int remote_break
;
881 set_remotebreak (char *args
, int from_tty
, struct cmd_list_element
*c
)
884 interrupt_sequence_mode
= interrupt_sequence_break
;
886 interrupt_sequence_mode
= interrupt_sequence_control_c
;
890 show_remotebreak (struct ui_file
*file
, int from_tty
,
891 struct cmd_list_element
*c
,
896 /* This variable sets the number of bits in an address that are to be
897 sent in a memory ("M" or "m") packet. Normally, after stripping
898 leading zeros, the entire address would be sent. This variable
899 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
900 initial implementation of remote.c restricted the address sent in
901 memory packets to ``host::sizeof long'' bytes - (typically 32
902 bits). Consequently, for 64 bit targets, the upper 32 bits of an
903 address was never sent. Since fixing this bug may cause a break in
904 some remote targets this variable is principly provided to
905 facilitate backward compatibility. */
907 static unsigned int remote_address_size
;
909 /* Temporary to track who currently owns the terminal. See
910 remote_terminal_* for more details. */
912 static int remote_async_terminal_ours_p
;
914 /* The executable file to use for "run" on the remote side. */
916 static char *remote_exec_file
= "";
919 /* User configurable variables for the number of characters in a
920 memory read/write packet. MIN (rsa->remote_packet_size,
921 rsa->sizeof_g_packet) is the default. Some targets need smaller
922 values (fifo overruns, et.al.) and some users need larger values
923 (speed up transfers). The variables ``preferred_*'' (the user
924 request), ``current_*'' (what was actually set) and ``forced_*''
925 (Positive - a soft limit, negative - a hard limit). */
927 struct memory_packet_config
934 /* Compute the current size of a read/write packet. Since this makes
935 use of ``actual_register_packet_size'' the computation is dynamic. */
938 get_memory_packet_size (struct memory_packet_config
*config
)
940 struct remote_state
*rs
= get_remote_state ();
941 struct remote_arch_state
*rsa
= get_remote_arch_state ();
943 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
944 law?) that some hosts don't cope very well with large alloca()
945 calls. Eventually the alloca() code will be replaced by calls to
946 xmalloc() and make_cleanups() allowing this restriction to either
947 be lifted or removed. */
948 #ifndef MAX_REMOTE_PACKET_SIZE
949 #define MAX_REMOTE_PACKET_SIZE 16384
951 /* NOTE: 20 ensures we can write at least one byte. */
952 #ifndef MIN_REMOTE_PACKET_SIZE
953 #define MIN_REMOTE_PACKET_SIZE 20
958 if (config
->size
<= 0)
959 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
961 what_they_get
= config
->size
;
965 what_they_get
= get_remote_packet_size ();
966 /* Limit the packet to the size specified by the user. */
968 && what_they_get
> config
->size
)
969 what_they_get
= config
->size
;
971 /* Limit it to the size of the targets ``g'' response unless we have
972 permission from the stub to use a larger packet size. */
973 if (rs
->explicit_packet_size
== 0
974 && rsa
->actual_register_packet_size
> 0
975 && what_they_get
> rsa
->actual_register_packet_size
)
976 what_they_get
= rsa
->actual_register_packet_size
;
978 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
979 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
980 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
981 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
983 /* Make sure there is room in the global buffer for this packet
984 (including its trailing NUL byte). */
985 if (rs
->buf_size
< what_they_get
+ 1)
987 rs
->buf_size
= 2 * what_they_get
;
988 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
991 return what_they_get
;
994 /* Update the size of a read/write packet. If they user wants
995 something really big then do a sanity check. */
998 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
1000 int fixed_p
= config
->fixed_p
;
1001 long size
= config
->size
;
1004 error (_("Argument required (integer, `fixed' or `limited')."));
1005 else if (strcmp (args
, "hard") == 0
1006 || strcmp (args
, "fixed") == 0)
1008 else if (strcmp (args
, "soft") == 0
1009 || strcmp (args
, "limit") == 0)
1015 size
= strtoul (args
, &end
, 0);
1017 error (_("Invalid %s (bad syntax)."), config
->name
);
1019 /* Instead of explicitly capping the size of a packet to
1020 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
1021 instead allowed to set the size to something arbitrarily
1023 if (size
> MAX_REMOTE_PACKET_SIZE
)
1024 error (_("Invalid %s (too large)."), config
->name
);
1028 if (fixed_p
&& !config
->fixed_p
)
1030 if (! query (_("The target may not be able to correctly handle a %s\n"
1031 "of %ld bytes. Change the packet size? "),
1032 config
->name
, size
))
1033 error (_("Packet size not changed."));
1035 /* Update the config. */
1036 config
->fixed_p
= fixed_p
;
1037 config
->size
= size
;
1041 show_memory_packet_size (struct memory_packet_config
*config
)
1043 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
1044 if (config
->fixed_p
)
1045 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
1046 get_memory_packet_size (config
));
1048 printf_filtered (_("Packets are limited to %ld bytes.\n"),
1049 get_memory_packet_size (config
));
1052 static struct memory_packet_config memory_write_packet_config
=
1054 "memory-write-packet-size",
1058 set_memory_write_packet_size (char *args
, int from_tty
)
1060 set_memory_packet_size (args
, &memory_write_packet_config
);
1064 show_memory_write_packet_size (char *args
, int from_tty
)
1066 show_memory_packet_size (&memory_write_packet_config
);
1070 get_memory_write_packet_size (void)
1072 return get_memory_packet_size (&memory_write_packet_config
);
1075 static struct memory_packet_config memory_read_packet_config
=
1077 "memory-read-packet-size",
1081 set_memory_read_packet_size (char *args
, int from_tty
)
1083 set_memory_packet_size (args
, &memory_read_packet_config
);
1087 show_memory_read_packet_size (char *args
, int from_tty
)
1089 show_memory_packet_size (&memory_read_packet_config
);
1093 get_memory_read_packet_size (void)
1095 long size
= get_memory_packet_size (&memory_read_packet_config
);
1097 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1098 extra buffer size argument before the memory read size can be
1099 increased beyond this. */
1100 if (size
> get_remote_packet_size ())
1101 size
= get_remote_packet_size ();
1106 /* Generic configuration support for packets the stub optionally
1107 supports. Allows the user to specify the use of the packet as well
1108 as allowing GDB to auto-detect support in the remote stub. */
1112 PACKET_SUPPORT_UNKNOWN
= 0,
1117 struct packet_config
1121 enum auto_boolean detect
;
1122 enum packet_support support
;
1125 /* Analyze a packet's return value and update the packet config
1136 update_packet_config (struct packet_config
*config
)
1138 switch (config
->detect
)
1140 case AUTO_BOOLEAN_TRUE
:
1141 config
->support
= PACKET_ENABLE
;
1143 case AUTO_BOOLEAN_FALSE
:
1144 config
->support
= PACKET_DISABLE
;
1146 case AUTO_BOOLEAN_AUTO
:
1147 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1153 show_packet_config_cmd (struct packet_config
*config
)
1155 char *support
= "internal-error";
1157 switch (config
->support
)
1160 support
= "enabled";
1162 case PACKET_DISABLE
:
1163 support
= "disabled";
1165 case PACKET_SUPPORT_UNKNOWN
:
1166 support
= "unknown";
1169 switch (config
->detect
)
1171 case AUTO_BOOLEAN_AUTO
:
1172 printf_filtered (_("Support for the `%s' packet "
1173 "is auto-detected, currently %s.\n"),
1174 config
->name
, support
);
1176 case AUTO_BOOLEAN_TRUE
:
1177 case AUTO_BOOLEAN_FALSE
:
1178 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1179 config
->name
, support
);
1185 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
1186 const char *title
, int legacy
)
1192 config
->name
= name
;
1193 config
->title
= title
;
1194 config
->detect
= AUTO_BOOLEAN_AUTO
;
1195 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1196 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1198 show_doc
= xstrprintf ("Show current use of remote "
1199 "protocol `%s' (%s) packet",
1201 /* set/show TITLE-packet {auto,on,off} */
1202 cmd_name
= xstrprintf ("%s-packet", title
);
1203 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
1204 &config
->detect
, set_doc
,
1205 show_doc
, NULL
, /* help_doc */
1206 set_remote_protocol_packet_cmd
,
1207 show_remote_protocol_packet_cmd
,
1208 &remote_set_cmdlist
, &remote_show_cmdlist
);
1209 /* The command code copies the documentation strings. */
1212 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1217 legacy_name
= xstrprintf ("%s-packet", name
);
1218 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1219 &remote_set_cmdlist
);
1220 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1221 &remote_show_cmdlist
);
1225 static enum packet_result
1226 packet_check_result (const char *buf
)
1230 /* The stub recognized the packet request. Check that the
1231 operation succeeded. */
1233 && isxdigit (buf
[1]) && isxdigit (buf
[2])
1235 /* "Enn" - definitly an error. */
1236 return PACKET_ERROR
;
1238 /* Always treat "E." as an error. This will be used for
1239 more verbose error messages, such as E.memtypes. */
1240 if (buf
[0] == 'E' && buf
[1] == '.')
1241 return PACKET_ERROR
;
1243 /* The packet may or may not be OK. Just assume it is. */
1247 /* The stub does not support the packet. */
1248 return PACKET_UNKNOWN
;
1251 static enum packet_result
1252 packet_ok (const char *buf
, struct packet_config
*config
)
1254 enum packet_result result
;
1256 result
= packet_check_result (buf
);
1261 /* The stub recognized the packet request. */
1262 switch (config
->support
)
1264 case PACKET_SUPPORT_UNKNOWN
:
1266 fprintf_unfiltered (gdb_stdlog
,
1267 "Packet %s (%s) is supported\n",
1268 config
->name
, config
->title
);
1269 config
->support
= PACKET_ENABLE
;
1271 case PACKET_DISABLE
:
1272 internal_error (__FILE__
, __LINE__
,
1273 _("packet_ok: attempt to use a disabled packet"));
1279 case PACKET_UNKNOWN
:
1280 /* The stub does not support the packet. */
1281 switch (config
->support
)
1284 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
1285 /* If the stub previously indicated that the packet was
1286 supported then there is a protocol error.. */
1287 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1288 config
->name
, config
->title
);
1290 /* The user set it wrong. */
1291 error (_("Enabled packet %s (%s) not recognized by stub"),
1292 config
->name
, config
->title
);
1294 case PACKET_SUPPORT_UNKNOWN
:
1296 fprintf_unfiltered (gdb_stdlog
,
1297 "Packet %s (%s) is NOT supported\n",
1298 config
->name
, config
->title
);
1299 config
->support
= PACKET_DISABLE
;
1301 case PACKET_DISABLE
:
1323 PACKET_vFile_pwrite
,
1325 PACKET_vFile_unlink
,
1326 PACKET_vFile_readlink
,
1328 PACKET_qXfer_features
,
1329 PACKET_qXfer_libraries
,
1330 PACKET_qXfer_libraries_svr4
,
1331 PACKET_qXfer_memory_map
,
1332 PACKET_qXfer_spu_read
,
1333 PACKET_qXfer_spu_write
,
1334 PACKET_qXfer_osdata
,
1335 PACKET_qXfer_threads
,
1336 PACKET_qXfer_statictrace_read
,
1337 PACKET_qXfer_traceframe_info
,
1343 PACKET_QPassSignals
,
1344 PACKET_QProgramSignals
,
1345 PACKET_qSearch_memory
,
1348 PACKET_QStartNoAckMode
,
1350 PACKET_qXfer_siginfo_read
,
1351 PACKET_qXfer_siginfo_write
,
1353 PACKET_ConditionalTracepoints
,
1354 PACKET_ConditionalBreakpoints
,
1355 PACKET_BreakpointCommands
,
1356 PACKET_FastTracepoints
,
1357 PACKET_StaticTracepoints
,
1358 PACKET_InstallInTrace
,
1361 PACKET_TracepointSource
,
1364 PACKET_QDisableRandomization
,
1366 PACKET_QTBuffer_size
,
1369 PACKET_qXfer_btrace
,
1373 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
1376 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
1377 struct cmd_list_element
*c
)
1379 struct packet_config
*packet
;
1381 for (packet
= remote_protocol_packets
;
1382 packet
< &remote_protocol_packets
[PACKET_MAX
];
1385 if (&packet
->detect
== c
->var
)
1387 update_packet_config (packet
);
1391 internal_error (__FILE__
, __LINE__
, _("Could not find config for %s"),
1396 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
1397 struct cmd_list_element
*c
,
1400 struct packet_config
*packet
;
1402 for (packet
= remote_protocol_packets
;
1403 packet
< &remote_protocol_packets
[PACKET_MAX
];
1406 if (&packet
->detect
== c
->var
)
1408 show_packet_config_cmd (packet
);
1412 internal_error (__FILE__
, __LINE__
, _("Could not find config for %s"),
1416 /* Should we try one of the 'Z' requests? */
1420 Z_PACKET_SOFTWARE_BP
,
1421 Z_PACKET_HARDWARE_BP
,
1428 /* For compatibility with older distributions. Provide a ``set remote
1429 Z-packet ...'' command that updates all the Z packet types. */
1431 static enum auto_boolean remote_Z_packet_detect
;
1434 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1435 struct cmd_list_element
*c
)
1439 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1441 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1442 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1447 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1448 struct cmd_list_element
*c
,
1453 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1455 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1459 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1460 static struct async_signal_handler
*async_sigint_remote_twice_token
;
1461 static struct async_signal_handler
*async_sigint_remote_token
;
1464 /* Asynchronous signal handle registered as event loop source for
1465 when we have pending events ready to be passed to the core. */
1467 static struct async_event_handler
*remote_async_inferior_event_token
;
1471 static ptid_t magic_null_ptid
;
1472 static ptid_t not_sent_ptid
;
1473 static ptid_t any_thread_ptid
;
1475 /* Find out if the stub attached to PID (and hence GDB should offer to
1476 detach instead of killing it when bailing out). */
1479 remote_query_attached (int pid
)
1481 struct remote_state
*rs
= get_remote_state ();
1482 size_t size
= get_remote_packet_size ();
1484 if (remote_protocol_packets
[PACKET_qAttached
].support
== PACKET_DISABLE
)
1487 if (remote_multi_process_p (rs
))
1488 xsnprintf (rs
->buf
, size
, "qAttached:%x", pid
);
1490 xsnprintf (rs
->buf
, size
, "qAttached");
1493 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1495 switch (packet_ok (rs
->buf
,
1496 &remote_protocol_packets
[PACKET_qAttached
]))
1499 if (strcmp (rs
->buf
, "1") == 0)
1503 warning (_("Remote failure reply: %s"), rs
->buf
);
1505 case PACKET_UNKNOWN
:
1512 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1513 has been invented by GDB, instead of reported by the target. Since
1514 we can be connected to a remote system before before knowing about
1515 any inferior, mark the target with execution when we find the first
1516 inferior. If ATTACHED is 1, then we had just attached to this
1517 inferior. If it is 0, then we just created this inferior. If it
1518 is -1, then try querying the remote stub to find out if it had
1519 attached to the inferior or not. */
1521 static struct inferior
*
1522 remote_add_inferior (int fake_pid_p
, int pid
, int attached
)
1524 struct inferior
*inf
;
1526 /* Check whether this process we're learning about is to be
1527 considered attached, or if is to be considered to have been
1528 spawned by the stub. */
1530 attached
= remote_query_attached (pid
);
1532 if (gdbarch_has_global_solist (target_gdbarch ()))
1534 /* If the target shares code across all inferiors, then every
1535 attach adds a new inferior. */
1536 inf
= add_inferior (pid
);
1538 /* ... and every inferior is bound to the same program space.
1539 However, each inferior may still have its own address
1541 inf
->aspace
= maybe_new_address_space ();
1542 inf
->pspace
= current_program_space
;
1546 /* In the traditional debugging scenario, there's a 1-1 match
1547 between program/address spaces. We simply bind the inferior
1548 to the program space's address space. */
1549 inf
= current_inferior ();
1550 inferior_appeared (inf
, pid
);
1553 inf
->attach_flag
= attached
;
1554 inf
->fake_pid_p
= fake_pid_p
;
1559 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1560 according to RUNNING. */
1563 remote_add_thread (ptid_t ptid
, int running
)
1565 struct remote_state
*rs
= get_remote_state ();
1567 /* GDB historically didn't pull threads in the initial connection
1568 setup. If the remote target doesn't even have a concept of
1569 threads (e.g., a bare-metal target), even if internally we
1570 consider that a single-threaded target, mentioning a new thread
1571 might be confusing to the user. Be silent then, preserving the
1572 age old behavior. */
1573 if (rs
->starting_up
)
1574 add_thread_silent (ptid
);
1578 set_executing (ptid
, running
);
1579 set_running (ptid
, running
);
1582 /* Come here when we learn about a thread id from the remote target.
1583 It may be the first time we hear about such thread, so take the
1584 opportunity to add it to GDB's thread list. In case this is the
1585 first time we're noticing its corresponding inferior, add it to
1586 GDB's inferior list as well. */
1589 remote_notice_new_inferior (ptid_t currthread
, int running
)
1591 /* If this is a new thread, add it to GDB's thread list.
1592 If we leave it up to WFI to do this, bad things will happen. */
1594 if (in_thread_list (currthread
) && is_exited (currthread
))
1596 /* We're seeing an event on a thread id we knew had exited.
1597 This has to be a new thread reusing the old id. Add it. */
1598 remote_add_thread (currthread
, running
);
1602 if (!in_thread_list (currthread
))
1604 struct inferior
*inf
= NULL
;
1605 int pid
= ptid_get_pid (currthread
);
1607 if (ptid_is_pid (inferior_ptid
)
1608 && pid
== ptid_get_pid (inferior_ptid
))
1610 /* inferior_ptid has no thread member yet. This can happen
1611 with the vAttach -> remote_wait,"TAAthread:" path if the
1612 stub doesn't support qC. This is the first stop reported
1613 after an attach, so this is the main thread. Update the
1614 ptid in the thread list. */
1615 if (in_thread_list (pid_to_ptid (pid
)))
1616 thread_change_ptid (inferior_ptid
, currthread
);
1619 remote_add_thread (currthread
, running
);
1620 inferior_ptid
= currthread
;
1625 if (ptid_equal (magic_null_ptid
, inferior_ptid
))
1627 /* inferior_ptid is not set yet. This can happen with the
1628 vRun -> remote_wait,"TAAthread:" path if the stub
1629 doesn't support qC. This is the first stop reported
1630 after an attach, so this is the main thread. Update the
1631 ptid in the thread list. */
1632 thread_change_ptid (inferior_ptid
, currthread
);
1636 /* When connecting to a target remote, or to a target
1637 extended-remote which already was debugging an inferior, we
1638 may not know about it yet. Add it before adding its child
1639 thread, so notifications are emitted in a sensible order. */
1640 if (!in_inferior_list (ptid_get_pid (currthread
)))
1642 struct remote_state
*rs
= get_remote_state ();
1643 int fake_pid_p
= !remote_multi_process_p (rs
);
1645 inf
= remote_add_inferior (fake_pid_p
,
1646 ptid_get_pid (currthread
), -1);
1649 /* This is really a new thread. Add it. */
1650 remote_add_thread (currthread
, running
);
1652 /* If we found a new inferior, let the common code do whatever
1653 it needs to with it (e.g., read shared libraries, insert
1654 breakpoints), unless we're just setting up an all-stop
1658 struct remote_state
*rs
= get_remote_state ();
1660 if (non_stop
|| !rs
->starting_up
)
1661 notice_new_inferior (currthread
, running
, 0);
1666 /* Return the private thread data, creating it if necessary. */
1668 static struct private_thread_info
*
1669 demand_private_info (ptid_t ptid
)
1671 struct thread_info
*info
= find_thread_ptid (ptid
);
1677 info
->private = xmalloc (sizeof (*(info
->private)));
1678 info
->private_dtor
= free_private_thread_info
;
1679 info
->private->core
= -1;
1680 info
->private->extra
= 0;
1683 return info
->private;
1686 /* Call this function as a result of
1687 1) A halt indication (T packet) containing a thread id
1688 2) A direct query of currthread
1689 3) Successful execution of set thread */
1692 record_currthread (struct remote_state
*rs
, ptid_t currthread
)
1694 rs
->general_thread
= currthread
;
1697 /* If 'QPassSignals' is supported, tell the remote stub what signals
1698 it can simply pass through to the inferior without reporting. */
1701 remote_pass_signals (struct target_ops
*self
,
1702 int numsigs
, unsigned char *pass_signals
)
1704 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1706 char *pass_packet
, *p
;
1708 struct remote_state
*rs
= get_remote_state ();
1710 gdb_assert (numsigs
< 256);
1711 for (i
= 0; i
< numsigs
; i
++)
1713 if (pass_signals
[i
])
1716 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1717 strcpy (pass_packet
, "QPassSignals:");
1718 p
= pass_packet
+ strlen (pass_packet
);
1719 for (i
= 0; i
< numsigs
; i
++)
1721 if (pass_signals
[i
])
1724 *p
++ = tohex (i
>> 4);
1725 *p
++ = tohex (i
& 15);
1734 if (!rs
->last_pass_packet
|| strcmp (rs
->last_pass_packet
, pass_packet
))
1736 putpkt (pass_packet
);
1737 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1738 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1739 if (rs
->last_pass_packet
)
1740 xfree (rs
->last_pass_packet
);
1741 rs
->last_pass_packet
= pass_packet
;
1744 xfree (pass_packet
);
1748 /* If 'QProgramSignals' is supported, tell the remote stub what
1749 signals it should pass through to the inferior when detaching. */
1752 remote_program_signals (struct target_ops
*self
,
1753 int numsigs
, unsigned char *signals
)
1755 if (remote_protocol_packets
[PACKET_QProgramSignals
].support
!= PACKET_DISABLE
)
1759 struct remote_state
*rs
= get_remote_state ();
1761 gdb_assert (numsigs
< 256);
1762 for (i
= 0; i
< numsigs
; i
++)
1767 packet
= xmalloc (count
* 3 + strlen ("QProgramSignals:") + 1);
1768 strcpy (packet
, "QProgramSignals:");
1769 p
= packet
+ strlen (packet
);
1770 for (i
= 0; i
< numsigs
; i
++)
1772 if (signal_pass_state (i
))
1775 *p
++ = tohex (i
>> 4);
1776 *p
++ = tohex (i
& 15);
1785 if (!rs
->last_program_signals_packet
1786 || strcmp (rs
->last_program_signals_packet
, packet
) != 0)
1789 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1790 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_QProgramSignals
]);
1791 xfree (rs
->last_program_signals_packet
);
1792 rs
->last_program_signals_packet
= packet
;
1799 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1800 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1801 thread. If GEN is set, set the general thread, if not, then set
1802 the step/continue thread. */
1804 set_thread (struct ptid ptid
, int gen
)
1806 struct remote_state
*rs
= get_remote_state ();
1807 ptid_t state
= gen
? rs
->general_thread
: rs
->continue_thread
;
1808 char *buf
= rs
->buf
;
1809 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1811 if (ptid_equal (state
, ptid
))
1815 *buf
++ = gen
? 'g' : 'c';
1816 if (ptid_equal (ptid
, magic_null_ptid
))
1817 xsnprintf (buf
, endbuf
- buf
, "0");
1818 else if (ptid_equal (ptid
, any_thread_ptid
))
1819 xsnprintf (buf
, endbuf
- buf
, "0");
1820 else if (ptid_equal (ptid
, minus_one_ptid
))
1821 xsnprintf (buf
, endbuf
- buf
, "-1");
1823 write_ptid (buf
, endbuf
, ptid
);
1825 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1827 rs
->general_thread
= ptid
;
1829 rs
->continue_thread
= ptid
;
1833 set_general_thread (struct ptid ptid
)
1835 set_thread (ptid
, 1);
1839 set_continue_thread (struct ptid ptid
)
1841 set_thread (ptid
, 0);
1844 /* Change the remote current process. Which thread within the process
1845 ends up selected isn't important, as long as it is the same process
1846 as what INFERIOR_PTID points to.
1848 This comes from that fact that there is no explicit notion of
1849 "selected process" in the protocol. The selected process for
1850 general operations is the process the selected general thread
1854 set_general_process (void)
1856 struct remote_state
*rs
= get_remote_state ();
1858 /* If the remote can't handle multiple processes, don't bother. */
1859 if (!rs
->extended
|| !remote_multi_process_p (rs
))
1862 /* We only need to change the remote current thread if it's pointing
1863 at some other process. */
1864 if (ptid_get_pid (rs
->general_thread
) != ptid_get_pid (inferior_ptid
))
1865 set_general_thread (inferior_ptid
);
1869 /* Return nonzero if the thread PTID is still alive on the remote
1873 remote_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1875 struct remote_state
*rs
= get_remote_state ();
1878 if (ptid_equal (ptid
, magic_null_ptid
))
1879 /* The main thread is always alive. */
1882 if (ptid_get_pid (ptid
) != 0 && ptid_get_tid (ptid
) == 0)
1883 /* The main thread is always alive. This can happen after a
1884 vAttach, if the remote side doesn't support
1889 endp
= rs
->buf
+ get_remote_packet_size ();
1892 write_ptid (p
, endp
, ptid
);
1895 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1896 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1899 /* About these extended threadlist and threadinfo packets. They are
1900 variable length packets but, the fields within them are often fixed
1901 length. They are redundent enough to send over UDP as is the
1902 remote protocol in general. There is a matching unit test module
1905 /* WARNING: This threadref data structure comes from the remote O.S.,
1906 libstub protocol encoding, and remote.c. It is not particularly
1909 /* Right now, the internal structure is int. We want it to be bigger.
1910 Plan to fix this. */
1912 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1914 /* gdb_ext_thread_info is an internal GDB data structure which is
1915 equivalent to the reply of the remote threadinfo packet. */
1917 struct gdb_ext_thread_info
1919 threadref threadid
; /* External form of thread reference. */
1920 int active
; /* Has state interesting to GDB?
1922 char display
[256]; /* Brief state display, name,
1923 blocked/suspended. */
1924 char shortname
[32]; /* To be used to name threads. */
1925 char more_display
[256]; /* Long info, statistics, queue depth,
1929 /* The volume of remote transfers can be limited by submitting
1930 a mask containing bits specifying the desired information.
1931 Use a union of these values as the 'selection' parameter to
1932 get_thread_info. FIXME: Make these TAG names more thread specific. */
1934 #define TAG_THREADID 1
1935 #define TAG_EXISTS 2
1936 #define TAG_DISPLAY 4
1937 #define TAG_THREADNAME 8
1938 #define TAG_MOREDISPLAY 16
1940 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1942 static char *unpack_nibble (char *buf
, int *val
);
1944 static char *unpack_byte (char *buf
, int *value
);
1946 static char *pack_int (char *buf
, int value
);
1948 static char *unpack_int (char *buf
, int *value
);
1950 static char *unpack_string (char *src
, char *dest
, int length
);
1952 static char *pack_threadid (char *pkt
, threadref
*id
);
1954 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1956 void int_to_threadref (threadref
*id
, int value
);
1958 static int threadref_to_int (threadref
*ref
);
1960 static void copy_threadref (threadref
*dest
, threadref
*src
);
1962 static int threadmatch (threadref
*dest
, threadref
*src
);
1964 static char *pack_threadinfo_request (char *pkt
, int mode
,
1967 static int remote_unpack_thread_info_response (char *pkt
,
1968 threadref
*expectedref
,
1969 struct gdb_ext_thread_info
1973 static int remote_get_threadinfo (threadref
*threadid
,
1974 int fieldset
, /*TAG mask */
1975 struct gdb_ext_thread_info
*info
);
1977 static char *pack_threadlist_request (char *pkt
, int startflag
,
1979 threadref
*nextthread
);
1981 static int parse_threadlist_response (char *pkt
,
1983 threadref
*original_echo
,
1984 threadref
*resultlist
,
1987 static int remote_get_threadlist (int startflag
,
1988 threadref
*nextthread
,
1992 threadref
*threadlist
);
1994 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1996 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1997 void *context
, int looplimit
);
1999 static int remote_newthread_step (threadref
*ref
, void *context
);
2002 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2003 buffer we're allowed to write to. Returns
2004 BUF+CHARACTERS_WRITTEN. */
2007 write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
2010 struct remote_state
*rs
= get_remote_state ();
2012 if (remote_multi_process_p (rs
))
2014 pid
= ptid_get_pid (ptid
);
2016 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
2018 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
2020 tid
= ptid_get_tid (ptid
);
2022 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
2024 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
2029 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2030 passed the last parsed char. Returns null_ptid on error. */
2033 read_ptid (char *buf
, char **obuf
)
2037 ULONGEST pid
= 0, tid
= 0;
2041 /* Multi-process ptid. */
2042 pp
= unpack_varlen_hex (p
+ 1, &pid
);
2044 error (_("invalid remote ptid: %s"), p
);
2047 pp
= unpack_varlen_hex (p
+ 1, &tid
);
2050 return ptid_build (pid
, 0, tid
);
2053 /* No multi-process. Just a tid. */
2054 pp
= unpack_varlen_hex (p
, &tid
);
2056 /* Since the stub is not sending a process id, then default to
2057 what's in inferior_ptid, unless it's null at this point. If so,
2058 then since there's no way to know the pid of the reported
2059 threads, use the magic number. */
2060 if (ptid_equal (inferior_ptid
, null_ptid
))
2061 pid
= ptid_get_pid (magic_null_ptid
);
2063 pid
= ptid_get_pid (inferior_ptid
);
2067 return ptid_build (pid
, 0, tid
);
2073 if (ch
>= 'a' && ch
<= 'f')
2074 return ch
- 'a' + 10;
2075 if (ch
>= '0' && ch
<= '9')
2077 if (ch
>= 'A' && ch
<= 'F')
2078 return ch
- 'A' + 10;
2083 stub_unpack_int (char *buff
, int fieldlength
)
2090 nibble
= stubhex (*buff
++);
2094 retval
= retval
<< 4;
2100 unpack_nibble (char *buf
, int *val
)
2102 *val
= fromhex (*buf
++);
2107 unpack_byte (char *buf
, int *value
)
2109 *value
= stub_unpack_int (buf
, 2);
2114 pack_int (char *buf
, int value
)
2116 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
2117 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
2118 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
2119 buf
= pack_hex_byte (buf
, (value
& 0xff));
2124 unpack_int (char *buf
, int *value
)
2126 *value
= stub_unpack_int (buf
, 8);
2130 #if 0 /* Currently unused, uncomment when needed. */
2131 static char *pack_string (char *pkt
, char *string
);
2134 pack_string (char *pkt
, char *string
)
2139 len
= strlen (string
);
2141 len
= 200; /* Bigger than most GDB packets, junk??? */
2142 pkt
= pack_hex_byte (pkt
, len
);
2146 if ((ch
== '\0') || (ch
== '#'))
2147 ch
= '*'; /* Protect encapsulation. */
2152 #endif /* 0 (unused) */
2155 unpack_string (char *src
, char *dest
, int length
)
2164 pack_threadid (char *pkt
, threadref
*id
)
2167 unsigned char *altid
;
2169 altid
= (unsigned char *) id
;
2170 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
2172 pkt
= pack_hex_byte (pkt
, *altid
++);
2178 unpack_threadid (char *inbuf
, threadref
*id
)
2181 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
2184 altref
= (char *) id
;
2186 while (inbuf
< limit
)
2188 x
= stubhex (*inbuf
++);
2189 y
= stubhex (*inbuf
++);
2190 *altref
++ = (x
<< 4) | y
;
2195 /* Externally, threadrefs are 64 bits but internally, they are still
2196 ints. This is due to a mismatch of specifications. We would like
2197 to use 64bit thread references internally. This is an adapter
2201 int_to_threadref (threadref
*id
, int value
)
2203 unsigned char *scan
;
2205 scan
= (unsigned char *) id
;
2211 *scan
++ = (value
>> 24) & 0xff;
2212 *scan
++ = (value
>> 16) & 0xff;
2213 *scan
++ = (value
>> 8) & 0xff;
2214 *scan
++ = (value
& 0xff);
2218 threadref_to_int (threadref
*ref
)
2221 unsigned char *scan
;
2227 value
= (value
<< 8) | ((*scan
++) & 0xff);
2232 copy_threadref (threadref
*dest
, threadref
*src
)
2235 unsigned char *csrc
, *cdest
;
2237 csrc
= (unsigned char *) src
;
2238 cdest
= (unsigned char *) dest
;
2245 threadmatch (threadref
*dest
, threadref
*src
)
2247 /* Things are broken right now, so just assume we got a match. */
2249 unsigned char *srcp
, *destp
;
2251 srcp
= (char *) src
;
2252 destp
= (char *) dest
;
2256 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
2263 threadid:1, # always request threadid
2270 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2273 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
2275 *pkt
++ = 'q'; /* Info Query */
2276 *pkt
++ = 'P'; /* process or thread info */
2277 pkt
= pack_int (pkt
, mode
); /* mode */
2278 pkt
= pack_threadid (pkt
, id
); /* threadid */
2279 *pkt
= '\0'; /* terminate */
2283 /* These values tag the fields in a thread info response packet. */
2284 /* Tagging the fields allows us to request specific fields and to
2285 add more fields as time goes by. */
2287 #define TAG_THREADID 1 /* Echo the thread identifier. */
2288 #define TAG_EXISTS 2 /* Is this process defined enough to
2289 fetch registers and its stack? */
2290 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2291 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2292 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2296 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
2297 struct gdb_ext_thread_info
*info
)
2299 struct remote_state
*rs
= get_remote_state ();
2303 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
2306 /* info->threadid = 0; FIXME: implement zero_threadref. */
2308 info
->display
[0] = '\0';
2309 info
->shortname
[0] = '\0';
2310 info
->more_display
[0] = '\0';
2312 /* Assume the characters indicating the packet type have been
2314 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
2315 pkt
= unpack_threadid (pkt
, &ref
);
2318 warning (_("Incomplete response to threadinfo request."));
2319 if (!threadmatch (&ref
, expectedref
))
2320 { /* This is an answer to a different request. */
2321 warning (_("ERROR RMT Thread info mismatch."));
2324 copy_threadref (&info
->threadid
, &ref
);
2326 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2328 /* Packets are terminated with nulls. */
2329 while ((pkt
< limit
) && mask
&& *pkt
)
2331 pkt
= unpack_int (pkt
, &tag
); /* tag */
2332 pkt
= unpack_byte (pkt
, &length
); /* length */
2333 if (!(tag
& mask
)) /* Tags out of synch with mask. */
2335 warning (_("ERROR RMT: threadinfo tag mismatch."));
2339 if (tag
== TAG_THREADID
)
2343 warning (_("ERROR RMT: length of threadid is not 16."));
2347 pkt
= unpack_threadid (pkt
, &ref
);
2348 mask
= mask
& ~TAG_THREADID
;
2351 if (tag
== TAG_EXISTS
)
2353 info
->active
= stub_unpack_int (pkt
, length
);
2355 mask
= mask
& ~(TAG_EXISTS
);
2358 warning (_("ERROR RMT: 'exists' length too long."));
2364 if (tag
== TAG_THREADNAME
)
2366 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
2367 mask
= mask
& ~TAG_THREADNAME
;
2370 if (tag
== TAG_DISPLAY
)
2372 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
2373 mask
= mask
& ~TAG_DISPLAY
;
2376 if (tag
== TAG_MOREDISPLAY
)
2378 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
2379 mask
= mask
& ~TAG_MOREDISPLAY
;
2382 warning (_("ERROR RMT: unknown thread info tag."));
2383 break; /* Not a tag we know about. */
2389 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
2390 struct gdb_ext_thread_info
*info
)
2392 struct remote_state
*rs
= get_remote_state ();
2395 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
2397 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2399 if (rs
->buf
[0] == '\0')
2402 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
2407 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2410 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
2411 threadref
*nextthread
)
2413 *pkt
++ = 'q'; /* info query packet */
2414 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
2415 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
2416 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
2417 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
2422 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2425 parse_threadlist_response (char *pkt
, int result_limit
,
2426 threadref
*original_echo
, threadref
*resultlist
,
2429 struct remote_state
*rs
= get_remote_state ();
2431 int count
, resultcount
, done
;
2434 /* Assume the 'q' and 'M chars have been stripped. */
2435 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
2436 /* done parse past here */
2437 pkt
= unpack_byte (pkt
, &count
); /* count field */
2438 pkt
= unpack_nibble (pkt
, &done
);
2439 /* The first threadid is the argument threadid. */
2440 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
2441 while ((count
-- > 0) && (pkt
< limit
))
2443 pkt
= unpack_threadid (pkt
, resultlist
++);
2444 if (resultcount
++ >= result_limit
)
2453 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
2454 int *done
, int *result_count
, threadref
*threadlist
)
2456 struct remote_state
*rs
= get_remote_state ();
2459 /* Trancate result limit to be smaller than the packet size. */
2460 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
2461 >= get_remote_packet_size ())
2462 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
2464 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
2466 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2468 if (*rs
->buf
== '\0')
2472 parse_threadlist_response (rs
->buf
+ 2, result_limit
,
2473 &rs
->echo_nextthread
, threadlist
, done
);
2475 if (!threadmatch (&rs
->echo_nextthread
, nextthread
))
2477 /* FIXME: This is a good reason to drop the packet. */
2478 /* Possably, there is a duplicate response. */
2480 retransmit immediatly - race conditions
2481 retransmit after timeout - yes
2483 wait for packet, then exit
2485 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2486 return 0; /* I choose simply exiting. */
2488 if (*result_count
<= 0)
2492 warning (_("RMT ERROR : failed to get remote thread list."));
2495 return result
; /* break; */
2497 if (*result_count
> result_limit
)
2500 warning (_("RMT ERROR: threadlist response longer than requested."));
2506 /* This is the interface between remote and threads, remotes upper
2509 /* remote_find_new_threads retrieves the thread list and for each
2510 thread in the list, looks up the thread in GDB's internal list,
2511 adding the thread if it does not already exist. This involves
2512 getting partial thread lists from the remote target so, polling the
2513 quit_flag is required. */
2517 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2520 struct remote_state
*rs
= get_remote_state ();
2521 int done
, i
, result_count
;
2529 if (loopcount
++ > looplimit
)
2532 warning (_("Remote fetch threadlist -infinite loop-."));
2535 if (!remote_get_threadlist (startflag
, &rs
->nextthread
,
2536 MAXTHREADLISTRESULTS
,
2537 &done
, &result_count
, rs
->resultthreadlist
))
2542 /* Clear for later iterations. */
2544 /* Setup to resume next batch of thread references, set nextthread. */
2545 if (result_count
>= 1)
2546 copy_threadref (&rs
->nextthread
,
2547 &rs
->resultthreadlist
[result_count
- 1]);
2549 while (result_count
--)
2550 if (!(result
= (*stepfunction
) (&rs
->resultthreadlist
[i
++], context
)))
2557 remote_newthread_step (threadref
*ref
, void *context
)
2559 int pid
= ptid_get_pid (inferior_ptid
);
2560 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2562 if (!in_thread_list (ptid
))
2564 return 1; /* continue iterator */
2567 #define CRAZY_MAX_THREADS 1000
2570 remote_current_thread (ptid_t oldpid
)
2572 struct remote_state
*rs
= get_remote_state ();
2575 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2576 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2577 return read_ptid (&rs
->buf
[2], NULL
);
2582 /* Find new threads for info threads command.
2583 * Original version, using John Metzler's thread protocol.
2587 remote_find_new_threads (void)
2589 remote_threadlist_iterator (remote_newthread_step
, 0,
2593 #if defined(HAVE_LIBEXPAT)
2595 typedef struct thread_item
2601 DEF_VEC_O(thread_item_t
);
2603 struct threads_parsing_context
2605 VEC (thread_item_t
) *items
;
2609 start_thread (struct gdb_xml_parser
*parser
,
2610 const struct gdb_xml_element
*element
,
2611 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
2613 struct threads_parsing_context
*data
= user_data
;
2615 struct thread_item item
;
2617 struct gdb_xml_value
*attr
;
2619 id
= xml_find_attribute (attributes
, "id")->value
;
2620 item
.ptid
= read_ptid (id
, NULL
);
2622 attr
= xml_find_attribute (attributes
, "core");
2624 item
.core
= *(ULONGEST
*) attr
->value
;
2630 VEC_safe_push (thread_item_t
, data
->items
, &item
);
2634 end_thread (struct gdb_xml_parser
*parser
,
2635 const struct gdb_xml_element
*element
,
2636 void *user_data
, const char *body_text
)
2638 struct threads_parsing_context
*data
= user_data
;
2640 if (body_text
&& *body_text
)
2641 VEC_last (thread_item_t
, data
->items
)->extra
= xstrdup (body_text
);
2644 const struct gdb_xml_attribute thread_attributes
[] = {
2645 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
2646 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
2647 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
2650 const struct gdb_xml_element thread_children
[] = {
2651 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2654 const struct gdb_xml_element threads_children
[] = {
2655 { "thread", thread_attributes
, thread_children
,
2656 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
2657 start_thread
, end_thread
},
2658 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2661 const struct gdb_xml_element threads_elements
[] = {
2662 { "threads", NULL
, threads_children
,
2663 GDB_XML_EF_NONE
, NULL
, NULL
},
2664 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2667 /* Discard the contents of the constructed thread info context. */
2670 clear_threads_parsing_context (void *p
)
2672 struct threads_parsing_context
*context
= p
;
2674 struct thread_item
*item
;
2676 for (i
= 0; VEC_iterate (thread_item_t
, context
->items
, i
, item
); ++i
)
2677 xfree (item
->extra
);
2679 VEC_free (thread_item_t
, context
->items
);
2685 * Find all threads for info threads command.
2686 * Uses new thread protocol contributed by Cisco.
2687 * Falls back and attempts to use the older method (above)
2688 * if the target doesn't respond to the new method.
2692 remote_threads_info (struct target_ops
*ops
)
2694 struct remote_state
*rs
= get_remote_state ();
2698 if (rs
->remote_desc
== 0) /* paranoia */
2699 error (_("Command can only be used when connected to the remote target."));
2701 #if defined(HAVE_LIBEXPAT)
2702 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2704 char *xml
= target_read_stralloc (¤t_target
,
2705 TARGET_OBJECT_THREADS
, NULL
);
2707 struct cleanup
*back_to
= make_cleanup (xfree
, xml
);
2711 struct threads_parsing_context context
;
2713 context
.items
= NULL
;
2714 make_cleanup (clear_threads_parsing_context
, &context
);
2716 if (gdb_xml_parse_quick (_("threads"), "threads.dtd",
2717 threads_elements
, xml
, &context
) == 0)
2720 struct thread_item
*item
;
2723 VEC_iterate (thread_item_t
, context
.items
, i
, item
);
2726 if (!ptid_equal (item
->ptid
, null_ptid
))
2728 struct private_thread_info
*info
;
2729 /* In non-stop mode, we assume new found threads
2730 are running until proven otherwise with a
2731 stop reply. In all-stop, we can only get
2732 here if all threads are stopped. */
2733 int running
= non_stop
? 1 : 0;
2735 remote_notice_new_inferior (item
->ptid
, running
);
2737 info
= demand_private_info (item
->ptid
);
2738 info
->core
= item
->core
;
2739 info
->extra
= item
->extra
;
2746 do_cleanups (back_to
);
2751 if (rs
->use_threadinfo_query
)
2753 putpkt ("qfThreadInfo");
2754 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2756 if (bufp
[0] != '\0') /* q packet recognized */
2758 struct cleanup
*old_chain
;
2761 /* remote_notice_new_inferior (in the loop below) may make
2762 new RSP calls, which clobber rs->buf. Work with a
2764 bufp
= saved_reply
= xstrdup (rs
->buf
);
2765 old_chain
= make_cleanup (free_current_contents
, &saved_reply
);
2767 while (*bufp
++ == 'm') /* reply contains one or more TID */
2771 new_thread
= read_ptid (bufp
, &bufp
);
2772 if (!ptid_equal (new_thread
, null_ptid
))
2774 /* In non-stop mode, we assume new found threads
2775 are running until proven otherwise with a
2776 stop reply. In all-stop, we can only get
2777 here if all threads are stopped. */
2778 int running
= non_stop
? 1 : 0;
2780 remote_notice_new_inferior (new_thread
, running
);
2783 while (*bufp
++ == ','); /* comma-separated list */
2784 free_current_contents (&saved_reply
);
2785 putpkt ("qsThreadInfo");
2786 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2787 bufp
= saved_reply
= xstrdup (rs
->buf
);
2789 do_cleanups (old_chain
);
2794 /* Only qfThreadInfo is supported in non-stop mode. */
2798 /* Else fall back to old method based on jmetzler protocol. */
2799 rs
->use_threadinfo_query
= 0;
2800 remote_find_new_threads ();
2805 * Collect a descriptive string about the given thread.
2806 * The target may say anything it wants to about the thread
2807 * (typically info about its blocked / runnable state, name, etc.).
2808 * This string will appear in the info threads display.
2810 * Optional: targets are not required to implement this function.
2814 remote_threads_extra_info (struct target_ops
*self
, struct thread_info
*tp
)
2816 struct remote_state
*rs
= get_remote_state ();
2820 struct gdb_ext_thread_info threadinfo
;
2821 static char display_buf
[100]; /* arbitrary... */
2822 int n
= 0; /* position in display_buf */
2824 if (rs
->remote_desc
== 0) /* paranoia */
2825 internal_error (__FILE__
, __LINE__
,
2826 _("remote_threads_extra_info"));
2828 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2829 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2830 /* This is the main thread which was added by GDB. The remote
2831 server doesn't know about it. */
2834 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2836 struct thread_info
*info
= find_thread_ptid (tp
->ptid
);
2838 if (info
&& info
->private)
2839 return info
->private->extra
;
2844 if (rs
->use_threadextra_query
)
2847 char *endb
= rs
->buf
+ get_remote_packet_size ();
2849 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2851 write_ptid (b
, endb
, tp
->ptid
);
2854 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2855 if (rs
->buf
[0] != 0)
2857 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2858 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2859 display_buf
[result
] = '\0';
2864 /* If the above query fails, fall back to the old method. */
2865 rs
->use_threadextra_query
= 0;
2866 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2867 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2868 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2869 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2870 if (threadinfo
.active
)
2872 if (*threadinfo
.shortname
)
2873 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2874 " Name: %s,", threadinfo
.shortname
);
2875 if (*threadinfo
.display
)
2876 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2877 " State: %s,", threadinfo
.display
);
2878 if (*threadinfo
.more_display
)
2879 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2880 " Priority: %s", threadinfo
.more_display
);
2884 /* For purely cosmetic reasons, clear up trailing commas. */
2885 if (',' == display_buf
[n
-1])
2886 display_buf
[n
-1] = ' ';
2895 remote_static_tracepoint_marker_at (struct target_ops
*self
, CORE_ADDR addr
,
2896 struct static_tracepoint_marker
*marker
)
2898 struct remote_state
*rs
= get_remote_state ();
2901 xsnprintf (p
, get_remote_packet_size (), "qTSTMat:");
2903 p
+= hexnumstr (p
, addr
);
2905 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2909 error (_("Remote failure reply: %s"), p
);
2913 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2920 static VEC(static_tracepoint_marker_p
) *
2921 remote_static_tracepoint_markers_by_strid (struct target_ops
*self
,
2924 struct remote_state
*rs
= get_remote_state ();
2925 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
2926 struct static_tracepoint_marker
*marker
= NULL
;
2927 struct cleanup
*old_chain
;
2930 /* Ask for a first packet of static tracepoint marker
2933 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2936 error (_("Remote failure reply: %s"), p
);
2938 old_chain
= make_cleanup (free_current_marker
, &marker
);
2943 marker
= XCNEW (struct static_tracepoint_marker
);
2947 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2949 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
2951 VEC_safe_push (static_tracepoint_marker_p
,
2957 release_static_tracepoint_marker (marker
);
2958 memset (marker
, 0, sizeof (*marker
));
2961 while (*p
++ == ','); /* comma-separated list */
2962 /* Ask for another packet of static tracepoint definition. */
2964 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2968 do_cleanups (old_chain
);
2973 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2976 remote_get_ada_task_ptid (struct target_ops
*self
, long lwp
, long thread
)
2978 return ptid_build (ptid_get_pid (inferior_ptid
), 0, lwp
);
2982 /* Restart the remote side; this is an extended protocol operation. */
2985 extended_remote_restart (void)
2987 struct remote_state
*rs
= get_remote_state ();
2989 /* Send the restart command; for reasons I don't understand the
2990 remote side really expects a number after the "R". */
2991 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2994 remote_fileio_reset ();
2997 /* Clean up connection to a remote debugger. */
3000 remote_close (struct target_ops
*self
)
3002 struct remote_state
*rs
= get_remote_state ();
3004 if (rs
->remote_desc
== NULL
)
3005 return; /* already closed */
3007 /* Make sure we leave stdin registered in the event loop, and we
3008 don't leave the async SIGINT signal handler installed. */
3009 remote_terminal_ours (self
);
3011 serial_close (rs
->remote_desc
);
3012 rs
->remote_desc
= NULL
;
3014 /* We don't have a connection to the remote stub anymore. Get rid
3015 of all the inferiors and their threads we were controlling.
3016 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3017 will be unable to find the thread corresponding to (pid, 0, 0). */
3018 inferior_ptid
= null_ptid
;
3019 discard_all_inferiors ();
3021 /* We are closing the remote target, so we should discard
3022 everything of this target. */
3023 discard_pending_stop_replies_in_queue (rs
);
3025 if (remote_async_inferior_event_token
)
3026 delete_async_event_handler (&remote_async_inferior_event_token
);
3028 remote_notif_state_xfree (rs
->notif_state
);
3030 trace_reset_local_state ();
3033 /* Query the remote side for the text, data and bss offsets. */
3038 struct remote_state
*rs
= get_remote_state ();
3041 int lose
, num_segments
= 0, do_sections
, do_segments
;
3042 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
3043 struct section_offsets
*offs
;
3044 struct symfile_segment_data
*data
;
3046 if (symfile_objfile
== NULL
)
3049 putpkt ("qOffsets");
3050 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3053 if (buf
[0] == '\000')
3054 return; /* Return silently. Stub doesn't support
3058 warning (_("Remote failure reply: %s"), buf
);
3062 /* Pick up each field in turn. This used to be done with scanf, but
3063 scanf will make trouble if CORE_ADDR size doesn't match
3064 conversion directives correctly. The following code will work
3065 with any size of CORE_ADDR. */
3066 text_addr
= data_addr
= bss_addr
= 0;
3070 if (strncmp (ptr
, "Text=", 5) == 0)
3073 /* Don't use strtol, could lose on big values. */
3074 while (*ptr
&& *ptr
!= ';')
3075 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
3077 if (strncmp (ptr
, ";Data=", 6) == 0)
3080 while (*ptr
&& *ptr
!= ';')
3081 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
3086 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
3089 while (*ptr
&& *ptr
!= ';')
3090 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
3092 if (bss_addr
!= data_addr
)
3093 warning (_("Target reported unsupported offsets: %s"), buf
);
3098 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
3101 /* Don't use strtol, could lose on big values. */
3102 while (*ptr
&& *ptr
!= ';')
3103 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
3106 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
3109 while (*ptr
&& *ptr
!= ';')
3110 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
3118 error (_("Malformed response to offset query, %s"), buf
);
3119 else if (*ptr
!= '\0')
3120 warning (_("Target reported unsupported offsets: %s"), buf
);
3122 offs
= ((struct section_offsets
*)
3123 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
3124 memcpy (offs
, symfile_objfile
->section_offsets
,
3125 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
3127 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
3128 do_segments
= (data
!= NULL
);
3129 do_sections
= num_segments
== 0;
3131 if (num_segments
> 0)
3133 segments
[0] = text_addr
;
3134 segments
[1] = data_addr
;
3136 /* If we have two segments, we can still try to relocate everything
3137 by assuming that the .text and .data offsets apply to the whole
3138 text and data segments. Convert the offsets given in the packet
3139 to base addresses for symfile_map_offsets_to_segments. */
3140 else if (data
&& data
->num_segments
== 2)
3142 segments
[0] = data
->segment_bases
[0] + text_addr
;
3143 segments
[1] = data
->segment_bases
[1] + data_addr
;
3146 /* If the object file has only one segment, assume that it is text
3147 rather than data; main programs with no writable data are rare,
3148 but programs with no code are useless. Of course the code might
3149 have ended up in the data segment... to detect that we would need
3150 the permissions here. */
3151 else if (data
&& data
->num_segments
== 1)
3153 segments
[0] = data
->segment_bases
[0] + text_addr
;
3156 /* There's no way to relocate by segment. */
3162 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
3163 offs
, num_segments
, segments
);
3165 if (ret
== 0 && !do_sections
)
3166 error (_("Can not handle qOffsets TextSeg "
3167 "response with this symbol file"));
3174 free_symfile_segment_data (data
);
3178 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
3180 /* This is a temporary kludge to force data and bss to use the
3181 same offsets because that's what nlmconv does now. The real
3182 solution requires changes to the stub and remote.c that I
3183 don't have time to do right now. */
3185 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
3186 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
3189 objfile_relocate (symfile_objfile
, offs
);
3192 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3193 threads we know are stopped already. This is used during the
3194 initial remote connection in non-stop mode --- threads that are
3195 reported as already being stopped are left stopped. */
3198 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
3200 /* If we have a stop reply for this thread, it must be stopped. */
3201 if (peek_stop_reply (thread
->ptid
))
3202 set_stop_requested (thread
->ptid
, 1);
3207 /* Send interrupt_sequence to remote target. */
3209 send_interrupt_sequence (void)
3211 struct remote_state
*rs
= get_remote_state ();
3213 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
3214 remote_serial_write ("\x03", 1);
3215 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
3216 serial_send_break (rs
->remote_desc
);
3217 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
3219 serial_send_break (rs
->remote_desc
);
3220 remote_serial_write ("g", 1);
3223 internal_error (__FILE__
, __LINE__
,
3224 _("Invalid value for interrupt_sequence_mode: %s."),
3225 interrupt_sequence_mode
);
3229 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
3230 and extract the PTID. Returns NULL_PTID if not found. */
3233 stop_reply_extract_thread (char *stop_reply
)
3235 if (stop_reply
[0] == 'T' && strlen (stop_reply
) > 3)
3239 /* Txx r:val ; r:val (...) */
3242 /* Look for "register" named "thread". */
3247 p1
= strchr (p
, ':');
3251 if (strncmp (p
, "thread", p1
- p
) == 0)
3252 return read_ptid (++p1
, &p
);
3254 p1
= strchr (p
, ';');
3266 /* Determine the remote side's current thread. If we have a stop
3267 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3268 "thread" register we can extract the current thread from. If not,
3269 ask the remote which is the current thread with qC. The former
3270 method avoids a roundtrip. */
3273 get_current_thread (char *wait_status
)
3277 /* Note we don't use remote_parse_stop_reply as that makes use of
3278 the target architecture, which we haven't yet fully determined at
3280 if (wait_status
!= NULL
)
3281 ptid
= stop_reply_extract_thread (wait_status
);
3282 if (ptid_equal (ptid
, null_ptid
))
3283 ptid
= remote_current_thread (inferior_ptid
);
3288 /* Query the remote target for which is the current thread/process,
3289 add it to our tables, and update INFERIOR_PTID. The caller is
3290 responsible for setting the state such that the remote end is ready
3291 to return the current thread.
3293 This function is called after handling the '?' or 'vRun' packets,
3294 whose response is a stop reply from which we can also try
3295 extracting the thread. If the target doesn't support the explicit
3296 qC query, we infer the current thread from that stop reply, passed
3297 in in WAIT_STATUS, which may be NULL. */
3300 add_current_inferior_and_thread (char *wait_status
)
3302 struct remote_state
*rs
= get_remote_state ();
3304 ptid_t ptid
= null_ptid
;
3306 inferior_ptid
= null_ptid
;
3308 /* Now, if we have thread information, update inferior_ptid. */
3309 ptid
= get_current_thread (wait_status
);
3311 if (!ptid_equal (ptid
, null_ptid
))
3313 if (!remote_multi_process_p (rs
))
3316 inferior_ptid
= ptid
;
3320 /* Without this, some commands which require an active target
3321 (such as kill) won't work. This variable serves (at least)
3322 double duty as both the pid of the target process (if it has
3323 such), and as a flag indicating that a target is active. */
3324 inferior_ptid
= magic_null_ptid
;
3328 remote_add_inferior (fake_pid_p
, ptid_get_pid (inferior_ptid
), -1);
3330 /* Add the main thread. */
3331 add_thread_silent (inferior_ptid
);
3335 remote_start_remote (int from_tty
, struct target_ops
*target
, int extended_p
)
3337 struct remote_state
*rs
= get_remote_state ();
3338 struct packet_config
*noack_config
;
3339 char *wait_status
= NULL
;
3341 immediate_quit
++; /* Allow user to interrupt it. */
3344 if (interrupt_on_connect
)
3345 send_interrupt_sequence ();
3347 /* Ack any packet which the remote side has already sent. */
3348 serial_write (rs
->remote_desc
, "+", 1);
3350 /* Signal other parts that we're going through the initial setup,
3351 and so things may not be stable yet. */
3352 rs
->starting_up
= 1;
3354 /* The first packet we send to the target is the optional "supported
3355 packets" request. If the target can answer this, it will tell us
3356 which later probes to skip. */
3357 remote_query_supported ();
3359 /* If the stub wants to get a QAllow, compose one and send it. */
3360 if (remote_protocol_packets
[PACKET_QAllow
].support
!= PACKET_DISABLE
)
3361 remote_set_permissions (target
);
3363 /* Next, we possibly activate noack mode.
3365 If the QStartNoAckMode packet configuration is set to AUTO,
3366 enable noack mode if the stub reported a wish for it with
3369 If set to TRUE, then enable noack mode even if the stub didn't
3370 report it in qSupported. If the stub doesn't reply OK, the
3371 session ends with an error.
3373 If FALSE, then don't activate noack mode, regardless of what the
3374 stub claimed should be the default with qSupported. */
3376 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
3378 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
3379 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
3380 && noack_config
->support
== PACKET_ENABLE
))
3382 putpkt ("QStartNoAckMode");
3383 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3384 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
3390 /* Tell the remote that we are using the extended protocol. */
3392 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3395 /* Let the target know which signals it is allowed to pass down to
3397 update_signals_program_target ();
3399 /* Next, if the target can specify a description, read it. We do
3400 this before anything involving memory or registers. */
3401 target_find_description ();
3403 /* Next, now that we know something about the target, update the
3404 address spaces in the program spaces. */
3405 update_address_spaces ();
3407 /* On OSs where the list of libraries is global to all
3408 processes, we fetch them early. */
3409 if (gdbarch_has_global_solist (target_gdbarch ()))
3410 solib_add (NULL
, from_tty
, target
, auto_solib_add
);
3414 if (!rs
->non_stop_aware
)
3415 error (_("Non-stop mode requested, but remote "
3416 "does not support non-stop"));
3418 putpkt ("QNonStop:1");
3419 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3421 if (strcmp (rs
->buf
, "OK") != 0)
3422 error (_("Remote refused setting non-stop mode with: %s"), rs
->buf
);
3424 /* Find about threads and processes the stub is already
3425 controlling. We default to adding them in the running state.
3426 The '?' query below will then tell us about which threads are
3428 remote_threads_info (target
);
3430 else if (rs
->non_stop_aware
)
3432 /* Don't assume that the stub can operate in all-stop mode.
3433 Request it explicitly. */
3434 putpkt ("QNonStop:0");
3435 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3437 if (strcmp (rs
->buf
, "OK") != 0)
3438 error (_("Remote refused setting all-stop mode with: %s"), rs
->buf
);
3441 /* Upload TSVs regardless of whether the target is running or not. The
3442 remote stub, such as GDBserver, may have some predefined or builtin
3443 TSVs, even if the target is not running. */
3444 if (remote_get_trace_status (target
, current_trace_status ()) != -1)
3446 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
3448 remote_upload_trace_state_variables (target
, &uploaded_tsvs
);
3449 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
3452 /* Check whether the target is running now. */
3454 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3460 struct inferior
*inf
;
3462 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
3465 error (_("The target is not running (try extended-remote?)"));
3467 /* We're connected, but not running. Drop out before we
3468 call start_remote. */
3469 rs
->starting_up
= 0;
3474 /* Save the reply for later. */
3475 wait_status
= alloca (strlen (rs
->buf
) + 1);
3476 strcpy (wait_status
, rs
->buf
);
3479 /* Fetch thread list. */
3480 target_find_new_threads ();
3482 /* Let the stub know that we want it to return the thread. */
3483 set_continue_thread (minus_one_ptid
);
3485 if (thread_count () == 0)
3487 /* Target has no concept of threads at all. GDB treats
3488 non-threaded target as single-threaded; add a main
3490 add_current_inferior_and_thread (wait_status
);
3494 /* We have thread information; select the thread the target
3495 says should be current. If we're reconnecting to a
3496 multi-threaded program, this will ideally be the thread
3497 that last reported an event before GDB disconnected. */
3498 inferior_ptid
= get_current_thread (wait_status
);
3499 if (ptid_equal (inferior_ptid
, null_ptid
))
3501 /* Odd... The target was able to list threads, but not
3502 tell us which thread was current (no "thread"
3503 register in T stop reply?). Just pick the first
3504 thread in the thread list then. */
3505 inferior_ptid
= thread_list
->ptid
;
3509 /* init_wait_for_inferior should be called before get_offsets in order
3510 to manage `inserted' flag in bp loc in a correct state.
3511 breakpoint_init_inferior, called from init_wait_for_inferior, set
3512 `inserted' flag to 0, while before breakpoint_re_set, called from
3513 start_remote, set `inserted' flag to 1. In the initialization of
3514 inferior, breakpoint_init_inferior should be called first, and then
3515 breakpoint_re_set can be called. If this order is broken, state of
3516 `inserted' flag is wrong, and cause some problems on breakpoint
3518 init_wait_for_inferior ();
3520 get_offsets (); /* Get text, data & bss offsets. */
3522 /* If we could not find a description using qXfer, and we know
3523 how to do it some other way, try again. This is not
3524 supported for non-stop; it could be, but it is tricky if
3525 there are no stopped threads when we connect. */
3526 if (remote_read_description_p (target
)
3527 && gdbarch_target_desc (target_gdbarch ()) == NULL
)
3529 target_clear_description ();
3530 target_find_description ();
3533 /* Use the previously fetched status. */
3534 gdb_assert (wait_status
!= NULL
);
3535 strcpy (rs
->buf
, wait_status
);
3536 rs
->cached_wait_status
= 1;
3539 start_remote (from_tty
); /* Initialize gdb process mechanisms. */
3543 /* Clear WFI global state. Do this before finding about new
3544 threads and inferiors, and setting the current inferior.
3545 Otherwise we would clear the proceed status of the current
3546 inferior when we want its stop_soon state to be preserved
3547 (see notice_new_inferior). */
3548 init_wait_for_inferior ();
3550 /* In non-stop, we will either get an "OK", meaning that there
3551 are no stopped threads at this time; or, a regular stop
3552 reply. In the latter case, there may be more than one thread
3553 stopped --- we pull them all out using the vStopped
3555 if (strcmp (rs
->buf
, "OK") != 0)
3557 struct notif_client
*notif
= ¬if_client_stop
;
3559 /* remote_notif_get_pending_replies acks this one, and gets
3561 rs
->notif_state
->pending_event
[notif_client_stop
.id
]
3562 = remote_notif_parse (notif
, rs
->buf
);
3563 remote_notif_get_pending_events (notif
);
3565 /* Make sure that threads that were stopped remain
3567 iterate_over_threads (set_stop_requested_callback
, NULL
);
3570 if (target_can_async_p ())
3571 target_async (inferior_event_handler
, 0);
3573 if (thread_count () == 0)
3576 error (_("The target is not running (try extended-remote?)"));
3578 /* We're connected, but not running. Drop out before we
3579 call start_remote. */
3580 rs
->starting_up
= 0;
3584 /* Let the stub know that we want it to return the thread. */
3586 /* Force the stub to choose a thread. */
3587 set_general_thread (null_ptid
);
3590 inferior_ptid
= remote_current_thread (minus_one_ptid
);
3591 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
3592 error (_("remote didn't report the current thread in non-stop mode"));
3594 get_offsets (); /* Get text, data & bss offsets. */
3596 /* In non-stop mode, any cached wait status will be stored in
3597 the stop reply queue. */
3598 gdb_assert (wait_status
== NULL
);
3600 /* Report all signals during attach/startup. */
3601 remote_pass_signals (target
, 0, NULL
);
3604 /* If we connected to a live target, do some additional setup. */
3605 if (target_has_execution
)
3607 if (symfile_objfile
) /* No use without a symbol-file. */
3608 remote_check_symbols ();
3611 /* Possibly the target has been engaged in a trace run started
3612 previously; find out where things are at. */
3613 if (remote_get_trace_status (target
, current_trace_status ()) != -1)
3615 struct uploaded_tp
*uploaded_tps
= NULL
;
3617 if (current_trace_status ()->running
)
3618 printf_filtered (_("Trace is already running on the target.\n"));
3620 remote_upload_tracepoints (target
, &uploaded_tps
);
3622 merge_uploaded_tracepoints (&uploaded_tps
);
3625 /* The thread and inferior lists are now synchronized with the
3626 target, our symbols have been relocated, and we're merged the
3627 target's tracepoints with ours. We're done with basic start
3629 rs
->starting_up
= 0;
3631 /* If breakpoints are global, insert them now. */
3632 if (gdbarch_has_global_breakpoints (target_gdbarch ())
3633 && breakpoints_always_inserted_mode ())
3634 insert_breakpoints ();
3637 /* Open a connection to a remote debugger.
3638 NAME is the filename used for communication. */
3641 remote_open (char *name
, int from_tty
)
3643 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
3646 /* Open a connection to a remote debugger using the extended
3647 remote gdb protocol. NAME is the filename used for communication. */
3650 extended_remote_open (char *name
, int from_tty
)
3652 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
3655 /* Generic code for opening a connection to a remote target. */
3658 init_all_packet_configs (void)
3662 for (i
= 0; i
< PACKET_MAX
; i
++)
3663 update_packet_config (&remote_protocol_packets
[i
]);
3666 /* Symbol look-up. */
3669 remote_check_symbols (void)
3671 struct remote_state
*rs
= get_remote_state ();
3672 char *msg
, *reply
, *tmp
;
3673 struct minimal_symbol
*sym
;
3676 /* The remote side has no concept of inferiors that aren't running
3677 yet, it only knows about running processes. If we're connected
3678 but our current inferior is not running, we should not invite the
3679 remote target to request symbol lookups related to its
3680 (unrelated) current process. */
3681 if (!target_has_execution
)
3684 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
3687 /* Make sure the remote is pointing at the right process. Note
3688 there's no way to select "no process". */
3689 set_general_process ();
3691 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3692 because we need both at the same time. */
3693 msg
= alloca (get_remote_packet_size ());
3695 /* Invite target to request symbol lookups. */
3697 putpkt ("qSymbol::");
3698 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3699 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
3702 while (strncmp (reply
, "qSymbol:", 8) == 0)
3705 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
3707 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
3709 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
3712 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
3713 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
3715 /* If this is a function address, return the start of code
3716 instead of any data function descriptor. */
3717 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
3721 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
3722 phex_nz (sym_addr
, addr_size
), &reply
[8]);
3726 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3731 static struct serial
*
3732 remote_serial_open (char *name
)
3734 static int udp_warning
= 0;
3736 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3737 of in ser-tcp.c, because it is the remote protocol assuming that the
3738 serial connection is reliable and not the serial connection promising
3740 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
3742 warning (_("The remote protocol may be unreliable over UDP.\n"
3743 "Some events may be lost, rendering further debugging "
3748 return serial_open (name
);
3751 /* Inform the target of our permission settings. The permission flags
3752 work without this, but if the target knows the settings, it can do
3753 a couple things. First, it can add its own check, to catch cases
3754 that somehow manage to get by the permissions checks in target
3755 methods. Second, if the target is wired to disallow particular
3756 settings (for instance, a system in the field that is not set up to
3757 be able to stop at a breakpoint), it can object to any unavailable
3761 remote_set_permissions (struct target_ops
*self
)
3763 struct remote_state
*rs
= get_remote_state ();
3765 xsnprintf (rs
->buf
, get_remote_packet_size (), "QAllow:"
3766 "WriteReg:%x;WriteMem:%x;"
3767 "InsertBreak:%x;InsertTrace:%x;"
3768 "InsertFastTrace:%x;Stop:%x",
3769 may_write_registers
, may_write_memory
,
3770 may_insert_breakpoints
, may_insert_tracepoints
,
3771 may_insert_fast_tracepoints
, may_stop
);
3773 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3775 /* If the target didn't like the packet, warn the user. Do not try
3776 to undo the user's settings, that would just be maddening. */
3777 if (strcmp (rs
->buf
, "OK") != 0)
3778 warning (_("Remote refused setting permissions with: %s"), rs
->buf
);
3781 /* This type describes each known response to the qSupported
3783 struct protocol_feature
3785 /* The name of this protocol feature. */
3788 /* The default for this protocol feature. */
3789 enum packet_support default_support
;
3791 /* The function to call when this feature is reported, or after
3792 qSupported processing if the feature is not supported.
3793 The first argument points to this structure. The second
3794 argument indicates whether the packet requested support be
3795 enabled, disabled, or probed (or the default, if this function
3796 is being called at the end of processing and this feature was
3797 not reported). The third argument may be NULL; if not NULL, it
3798 is a NUL-terminated string taken from the packet following
3799 this feature's name and an equals sign. */
3800 void (*func
) (const struct protocol_feature
*, enum packet_support
,
3803 /* The corresponding packet for this feature. Only used if
3804 FUNC is remote_supported_packet. */
3809 remote_supported_packet (const struct protocol_feature
*feature
,
3810 enum packet_support support
,
3811 const char *argument
)
3815 warning (_("Remote qSupported response supplied an unexpected value for"
3816 " \"%s\"."), feature
->name
);
3820 if (remote_protocol_packets
[feature
->packet
].support
3821 == PACKET_SUPPORT_UNKNOWN
)
3822 remote_protocol_packets
[feature
->packet
].support
= support
;
3826 remote_packet_size (const struct protocol_feature
*feature
,
3827 enum packet_support support
, const char *value
)
3829 struct remote_state
*rs
= get_remote_state ();
3834 if (support
!= PACKET_ENABLE
)
3837 if (value
== NULL
|| *value
== '\0')
3839 warning (_("Remote target reported \"%s\" without a size."),
3845 packet_size
= strtol (value
, &value_end
, 16);
3846 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
3848 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3849 feature
->name
, value
);
3853 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
3855 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3856 packet_size
, MAX_REMOTE_PACKET_SIZE
);
3857 packet_size
= MAX_REMOTE_PACKET_SIZE
;
3860 /* Record the new maximum packet size. */
3861 rs
->explicit_packet_size
= packet_size
;
3865 remote_multi_process_feature (const struct protocol_feature
*feature
,
3866 enum packet_support support
, const char *value
)
3868 struct remote_state
*rs
= get_remote_state ();
3870 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
3874 remote_non_stop_feature (const struct protocol_feature
*feature
,
3875 enum packet_support support
, const char *value
)
3877 struct remote_state
*rs
= get_remote_state ();
3879 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
3883 remote_cond_tracepoint_feature (const struct protocol_feature
*feature
,
3884 enum packet_support support
,
3887 struct remote_state
*rs
= get_remote_state ();
3889 rs
->cond_tracepoints
= (support
== PACKET_ENABLE
);
3893 remote_cond_breakpoint_feature (const struct protocol_feature
*feature
,
3894 enum packet_support support
,
3897 struct remote_state
*rs
= get_remote_state ();
3899 rs
->cond_breakpoints
= (support
== PACKET_ENABLE
);
3903 remote_breakpoint_commands_feature (const struct protocol_feature
*feature
,
3904 enum packet_support support
,
3907 struct remote_state
*rs
= get_remote_state ();
3909 rs
->breakpoint_commands
= (support
== PACKET_ENABLE
);
3913 remote_fast_tracepoint_feature (const struct protocol_feature
*feature
,
3914 enum packet_support support
,
3917 struct remote_state
*rs
= get_remote_state ();
3919 rs
->fast_tracepoints
= (support
== PACKET_ENABLE
);
3923 remote_static_tracepoint_feature (const struct protocol_feature
*feature
,
3924 enum packet_support support
,
3927 struct remote_state
*rs
= get_remote_state ();
3929 rs
->static_tracepoints
= (support
== PACKET_ENABLE
);
3933 remote_install_in_trace_feature (const struct protocol_feature
*feature
,
3934 enum packet_support support
,
3937 struct remote_state
*rs
= get_remote_state ();
3939 rs
->install_in_trace
= (support
== PACKET_ENABLE
);
3943 remote_disconnected_tracing_feature (const struct protocol_feature
*feature
,
3944 enum packet_support support
,
3947 struct remote_state
*rs
= get_remote_state ();
3949 rs
->disconnected_tracing
= (support
== PACKET_ENABLE
);
3953 remote_enable_disable_tracepoint_feature (const struct protocol_feature
*feature
,
3954 enum packet_support support
,
3957 struct remote_state
*rs
= get_remote_state ();
3959 rs
->enable_disable_tracepoints
= (support
== PACKET_ENABLE
);
3963 remote_string_tracing_feature (const struct protocol_feature
*feature
,
3964 enum packet_support support
,
3967 struct remote_state
*rs
= get_remote_state ();
3969 rs
->string_tracing
= (support
== PACKET_ENABLE
);
3973 remote_augmented_libraries_svr4_read_feature
3974 (const struct protocol_feature
*feature
,
3975 enum packet_support support
, const char *value
)
3977 struct remote_state
*rs
= get_remote_state ();
3979 rs
->augmented_libraries_svr4_read
= (support
== PACKET_ENABLE
);
3982 static const struct protocol_feature remote_protocol_features
[] = {
3983 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
3984 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
3985 PACKET_qXfer_auxv
},
3986 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
3987 PACKET_qXfer_features
},
3988 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
3989 PACKET_qXfer_libraries
},
3990 { "qXfer:libraries-svr4:read", PACKET_DISABLE
, remote_supported_packet
,
3991 PACKET_qXfer_libraries_svr4
},
3992 { "augmented-libraries-svr4-read", PACKET_DISABLE
,
3993 remote_augmented_libraries_svr4_read_feature
, -1 },
3994 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
3995 PACKET_qXfer_memory_map
},
3996 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
3997 PACKET_qXfer_spu_read
},
3998 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
3999 PACKET_qXfer_spu_write
},
4000 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
4001 PACKET_qXfer_osdata
},
4002 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
4003 PACKET_qXfer_threads
},
4004 { "qXfer:traceframe-info:read", PACKET_DISABLE
, remote_supported_packet
,
4005 PACKET_qXfer_traceframe_info
},
4006 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
4007 PACKET_QPassSignals
},
4008 { "QProgramSignals", PACKET_DISABLE
, remote_supported_packet
,
4009 PACKET_QProgramSignals
},
4010 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
4011 PACKET_QStartNoAckMode
},
4012 { "multiprocess", PACKET_DISABLE
, remote_multi_process_feature
, -1 },
4013 { "QNonStop", PACKET_DISABLE
, remote_non_stop_feature
, -1 },
4014 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
4015 PACKET_qXfer_siginfo_read
},
4016 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
4017 PACKET_qXfer_siginfo_write
},
4018 { "ConditionalTracepoints", PACKET_DISABLE
, remote_cond_tracepoint_feature
,
4019 PACKET_ConditionalTracepoints
},
4020 { "ConditionalBreakpoints", PACKET_DISABLE
, remote_cond_breakpoint_feature
,
4021 PACKET_ConditionalBreakpoints
},
4022 { "BreakpointCommands", PACKET_DISABLE
, remote_breakpoint_commands_feature
,
4023 PACKET_BreakpointCommands
},
4024 { "FastTracepoints", PACKET_DISABLE
, remote_fast_tracepoint_feature
,
4025 PACKET_FastTracepoints
},
4026 { "StaticTracepoints", PACKET_DISABLE
, remote_static_tracepoint_feature
,
4027 PACKET_StaticTracepoints
},
4028 {"InstallInTrace", PACKET_DISABLE
, remote_install_in_trace_feature
,
4029 PACKET_InstallInTrace
},
4030 { "DisconnectedTracing", PACKET_DISABLE
, remote_disconnected_tracing_feature
,
4032 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
4034 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
4036 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
4037 PACKET_TracepointSource
},
4038 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
4040 { "EnableDisableTracepoints", PACKET_DISABLE
,
4041 remote_enable_disable_tracepoint_feature
, -1 },
4042 { "qXfer:fdpic:read", PACKET_DISABLE
, remote_supported_packet
,
4043 PACKET_qXfer_fdpic
},
4044 { "qXfer:uib:read", PACKET_DISABLE
, remote_supported_packet
,
4046 { "QDisableRandomization", PACKET_DISABLE
, remote_supported_packet
,
4047 PACKET_QDisableRandomization
},
4048 { "QAgent", PACKET_DISABLE
, remote_supported_packet
, PACKET_QAgent
},
4049 { "QTBuffer:size", PACKET_DISABLE
,
4050 remote_supported_packet
, PACKET_QTBuffer_size
},
4051 { "tracenz", PACKET_DISABLE
,
4052 remote_string_tracing_feature
, -1 },
4053 { "Qbtrace:off", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_off
},
4054 { "Qbtrace:bts", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_bts
},
4055 { "qXfer:btrace:read", PACKET_DISABLE
, remote_supported_packet
,
4056 PACKET_qXfer_btrace
}
4059 static char *remote_support_xml
;
4061 /* Register string appended to "xmlRegisters=" in qSupported query. */
4064 register_remote_support_xml (const char *xml
)
4066 #if defined(HAVE_LIBEXPAT)
4067 if (remote_support_xml
== NULL
)
4068 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
4071 char *copy
= xstrdup (remote_support_xml
+ 13);
4072 char *p
= strtok (copy
, ",");
4076 if (strcmp (p
, xml
) == 0)
4083 while ((p
= strtok (NULL
, ",")) != NULL
);
4086 remote_support_xml
= reconcat (remote_support_xml
,
4087 remote_support_xml
, ",", xml
,
4094 remote_query_supported_append (char *msg
, const char *append
)
4097 return reconcat (msg
, msg
, ";", append
, (char *) NULL
);
4099 return xstrdup (append
);
4103 remote_query_supported (void)
4105 struct remote_state
*rs
= get_remote_state ();
4108 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
4110 /* The packet support flags are handled differently for this packet
4111 than for most others. We treat an error, a disabled packet, and
4112 an empty response identically: any features which must be reported
4113 to be used will be automatically disabled. An empty buffer
4114 accomplishes this, since that is also the representation for a list
4115 containing no features. */
4118 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
4121 struct cleanup
*old_chain
= make_cleanup (free_current_contents
, &q
);
4123 q
= remote_query_supported_append (q
, "multiprocess+");
4125 if (remote_support_xml
)
4126 q
= remote_query_supported_append (q
, remote_support_xml
);
4128 q
= remote_query_supported_append (q
, "qRelocInsn+");
4130 q
= reconcat (q
, "qSupported:", q
, (char *) NULL
);
4133 do_cleanups (old_chain
);
4135 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4137 /* If an error occured, warn, but do not return - just reset the
4138 buffer to empty and go on to disable features. */
4139 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
4142 warning (_("Remote failure reply: %s"), rs
->buf
);
4147 memset (seen
, 0, sizeof (seen
));
4152 enum packet_support is_supported
;
4153 char *p
, *end
, *name_end
, *value
;
4155 /* First separate out this item from the rest of the packet. If
4156 there's another item after this, we overwrite the separator
4157 (terminated strings are much easier to work with). */
4159 end
= strchr (p
, ';');
4162 end
= p
+ strlen (p
);
4172 warning (_("empty item in \"qSupported\" response"));
4177 name_end
= strchr (p
, '=');
4180 /* This is a name=value entry. */
4181 is_supported
= PACKET_ENABLE
;
4182 value
= name_end
+ 1;
4191 is_supported
= PACKET_ENABLE
;
4195 is_supported
= PACKET_DISABLE
;
4199 is_supported
= PACKET_SUPPORT_UNKNOWN
;
4203 warning (_("unrecognized item \"%s\" "
4204 "in \"qSupported\" response"), p
);
4210 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
4211 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
4213 const struct protocol_feature
*feature
;
4216 feature
= &remote_protocol_features
[i
];
4217 feature
->func (feature
, is_supported
, value
);
4222 /* If we increased the packet size, make sure to increase the global
4223 buffer size also. We delay this until after parsing the entire
4224 qSupported packet, because this is the same buffer we were
4226 if (rs
->buf_size
< rs
->explicit_packet_size
)
4228 rs
->buf_size
= rs
->explicit_packet_size
;
4229 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
4232 /* Handle the defaults for unmentioned features. */
4233 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
4236 const struct protocol_feature
*feature
;
4238 feature
= &remote_protocol_features
[i
];
4239 feature
->func (feature
, feature
->default_support
, NULL
);
4243 /* Remove any of the remote.c targets from target stack. Upper targets depend
4244 on it so remove them first. */
4247 remote_unpush_target (void)
4249 pop_all_targets_above (process_stratum
- 1);
4253 remote_open_1 (char *name
, int from_tty
,
4254 struct target_ops
*target
, int extended_p
)
4256 struct remote_state
*rs
= get_remote_state ();
4259 error (_("To open a remote debug connection, you need to specify what\n"
4260 "serial device is attached to the remote system\n"
4261 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
4263 /* See FIXME above. */
4264 if (!target_async_permitted
)
4265 wait_forever_enabled_p
= 1;
4267 /* If we're connected to a running target, target_preopen will kill it.
4268 Ask this question first, before target_preopen has a chance to kill
4270 if (rs
->remote_desc
!= NULL
&& !have_inferiors ())
4273 && !query (_("Already connected to a remote target. Disconnect? ")))
4274 error (_("Still connected."));
4277 /* Here the possibly existing remote target gets unpushed. */
4278 target_preopen (from_tty
);
4280 /* Make sure we send the passed signals list the next time we resume. */
4281 xfree (rs
->last_pass_packet
);
4282 rs
->last_pass_packet
= NULL
;
4284 /* Make sure we send the program signals list the next time we
4286 xfree (rs
->last_program_signals_packet
);
4287 rs
->last_program_signals_packet
= NULL
;
4289 remote_fileio_reset ();
4290 reopen_exec_file ();
4293 rs
->remote_desc
= remote_serial_open (name
);
4294 if (!rs
->remote_desc
)
4295 perror_with_name (name
);
4297 if (baud_rate
!= -1)
4299 if (serial_setbaudrate (rs
->remote_desc
, baud_rate
))
4301 /* The requested speed could not be set. Error out to
4302 top level after closing remote_desc. Take care to
4303 set remote_desc to NULL to avoid closing remote_desc
4305 serial_close (rs
->remote_desc
);
4306 rs
->remote_desc
= NULL
;
4307 perror_with_name (name
);
4311 serial_raw (rs
->remote_desc
);
4313 /* If there is something sitting in the buffer we might take it as a
4314 response to a command, which would be bad. */
4315 serial_flush_input (rs
->remote_desc
);
4319 puts_filtered ("Remote debugging using ");
4320 puts_filtered (name
);
4321 puts_filtered ("\n");
4323 push_target (target
); /* Switch to using remote target now. */
4325 /* Register extra event sources in the event loop. */
4326 remote_async_inferior_event_token
4327 = create_async_event_handler (remote_async_inferior_event_handler
,
4329 rs
->notif_state
= remote_notif_state_allocate ();
4331 /* Reset the target state; these things will be queried either by
4332 remote_query_supported or as they are needed. */
4333 init_all_packet_configs ();
4334 rs
->cached_wait_status
= 0;
4335 rs
->explicit_packet_size
= 0;
4337 rs
->multi_process_aware
= 0;
4338 rs
->extended
= extended_p
;
4339 rs
->non_stop_aware
= 0;
4340 rs
->waiting_for_stop_reply
= 0;
4341 rs
->ctrlc_pending_p
= 0;
4343 rs
->general_thread
= not_sent_ptid
;
4344 rs
->continue_thread
= not_sent_ptid
;
4345 rs
->remote_traceframe_number
= -1;
4347 /* Probe for ability to use "ThreadInfo" query, as required. */
4348 rs
->use_threadinfo_query
= 1;
4349 rs
->use_threadextra_query
= 1;
4351 if (target_async_permitted
)
4353 /* With this target we start out by owning the terminal. */
4354 remote_async_terminal_ours_p
= 1;
4356 /* FIXME: cagney/1999-09-23: During the initial connection it is
4357 assumed that the target is already ready and able to respond to
4358 requests. Unfortunately remote_start_remote() eventually calls
4359 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4360 around this. Eventually a mechanism that allows
4361 wait_for_inferior() to expect/get timeouts will be
4363 wait_forever_enabled_p
= 0;
4366 /* First delete any symbols previously loaded from shared libraries. */
4367 no_shared_libraries (NULL
, 0);
4370 init_thread_list ();
4372 /* Start the remote connection. If error() or QUIT, discard this
4373 target (we'd otherwise be in an inconsistent state) and then
4374 propogate the error on up the exception chain. This ensures that
4375 the caller doesn't stumble along blindly assuming that the
4376 function succeeded. The CLI doesn't have this problem but other
4377 UI's, such as MI do.
4379 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4380 this function should return an error indication letting the
4381 caller restore the previous state. Unfortunately the command
4382 ``target remote'' is directly wired to this function making that
4383 impossible. On a positive note, the CLI side of this problem has
4384 been fixed - the function set_cmd_context() makes it possible for
4385 all the ``target ....'' commands to share a common callback
4386 function. See cli-dump.c. */
4388 volatile struct gdb_exception ex
;
4390 TRY_CATCH (ex
, RETURN_MASK_ALL
)
4392 remote_start_remote (from_tty
, target
, extended_p
);
4396 /* Pop the partially set up target - unless something else did
4397 already before throwing the exception. */
4398 if (rs
->remote_desc
!= NULL
)
4399 remote_unpush_target ();
4400 if (target_async_permitted
)
4401 wait_forever_enabled_p
= 1;
4402 throw_exception (ex
);
4406 if (target_async_permitted
)
4407 wait_forever_enabled_p
= 1;
4410 /* This takes a program previously attached to and detaches it. After
4411 this is done, GDB can be used to debug some other program. We
4412 better not have left any breakpoints in the target program or it'll
4413 die when it hits one. */
4416 remote_detach_1 (const char *args
, int from_tty
, int extended
)
4418 int pid
= ptid_get_pid (inferior_ptid
);
4419 struct remote_state
*rs
= get_remote_state ();
4422 error (_("Argument given to \"detach\" when remotely debugging."));
4424 if (!target_has_execution
)
4425 error (_("No process to detach from."));
4429 char *exec_file
= get_exec_file (0);
4430 if (exec_file
== NULL
)
4432 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
4433 target_pid_to_str (pid_to_ptid (pid
)));
4434 gdb_flush (gdb_stdout
);
4437 /* Tell the remote target to detach. */
4438 if (remote_multi_process_p (rs
))
4439 xsnprintf (rs
->buf
, get_remote_packet_size (), "D;%x", pid
);
4441 strcpy (rs
->buf
, "D");
4444 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4446 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
4448 else if (rs
->buf
[0] == '\0')
4449 error (_("Remote doesn't know how to detach"));
4451 error (_("Can't detach process."));
4453 if (from_tty
&& !extended
)
4454 puts_filtered (_("Ending remote debugging.\n"));
4456 target_mourn_inferior ();
4460 remote_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
4462 remote_detach_1 (args
, from_tty
, 0);
4466 extended_remote_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
4468 remote_detach_1 (args
, from_tty
, 1);
4471 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4474 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
4477 error (_("Argument given to \"disconnect\" when remotely debugging."));
4479 /* Make sure we unpush even the extended remote targets; mourn
4480 won't do it. So call remote_mourn_1 directly instead of
4481 target_mourn_inferior. */
4482 remote_mourn_1 (target
);
4485 puts_filtered ("Ending remote debugging.\n");
4488 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4489 be chatty about it. */
4492 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
4494 struct remote_state
*rs
= get_remote_state ();
4496 char *wait_status
= NULL
;
4498 pid
= parse_pid_to_attach (args
);
4500 /* Remote PID can be freely equal to getpid, do not check it here the same
4501 way as in other targets. */
4503 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4504 error (_("This target does not support attaching to a process"));
4508 char *exec_file
= get_exec_file (0);
4511 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
4512 target_pid_to_str (pid_to_ptid (pid
)));
4514 printf_unfiltered (_("Attaching to %s\n"),
4515 target_pid_to_str (pid_to_ptid (pid
)));
4517 gdb_flush (gdb_stdout
);
4520 xsnprintf (rs
->buf
, get_remote_packet_size (), "vAttach;%x", pid
);
4522 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4524 if (packet_ok (rs
->buf
,
4525 &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
4529 /* Save the reply for later. */
4530 wait_status
= alloca (strlen (rs
->buf
) + 1);
4531 strcpy (wait_status
, rs
->buf
);
4533 else if (strcmp (rs
->buf
, "OK") != 0)
4534 error (_("Attaching to %s failed with: %s"),
4535 target_pid_to_str (pid_to_ptid (pid
)),
4538 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4539 error (_("This target does not support attaching to a process"));
4541 error (_("Attaching to %s failed"),
4542 target_pid_to_str (pid_to_ptid (pid
)));
4544 set_current_inferior (remote_add_inferior (0, pid
, 1));
4546 inferior_ptid
= pid_to_ptid (pid
);
4550 struct thread_info
*thread
;
4552 /* Get list of threads. */
4553 remote_threads_info (target
);
4555 thread
= first_thread_of_process (pid
);
4557 inferior_ptid
= thread
->ptid
;
4559 inferior_ptid
= pid_to_ptid (pid
);
4561 /* Invalidate our notion of the remote current thread. */
4562 record_currthread (rs
, minus_one_ptid
);
4566 /* Now, if we have thread information, update inferior_ptid. */
4567 inferior_ptid
= remote_current_thread (inferior_ptid
);
4569 /* Add the main thread to the thread list. */
4570 add_thread_silent (inferior_ptid
);
4573 /* Next, if the target can specify a description, read it. We do
4574 this before anything involving memory or registers. */
4575 target_find_description ();
4579 /* Use the previously fetched status. */
4580 gdb_assert (wait_status
!= NULL
);
4582 if (target_can_async_p ())
4584 struct notif_event
*reply
4585 = remote_notif_parse (¬if_client_stop
, wait_status
);
4587 push_stop_reply ((struct stop_reply
*) reply
);
4589 target_async (inferior_event_handler
, 0);
4593 gdb_assert (wait_status
!= NULL
);
4594 strcpy (rs
->buf
, wait_status
);
4595 rs
->cached_wait_status
= 1;
4599 gdb_assert (wait_status
== NULL
);
4603 extended_remote_attach (struct target_ops
*ops
, char *args
, int from_tty
)
4605 extended_remote_attach_1 (ops
, args
, from_tty
);
4609 /* Check for the availability of vCont. This function should also check
4613 remote_vcont_probe (struct remote_state
*rs
)
4617 strcpy (rs
->buf
, "vCont?");
4619 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4622 /* Make sure that the features we assume are supported. */
4623 if (strncmp (buf
, "vCont", 5) == 0)
4626 int support_s
, support_S
, support_c
, support_C
;
4632 rs
->supports_vCont
.t
= 0;
4633 rs
->supports_vCont
.r
= 0;
4634 while (p
&& *p
== ';')
4637 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4639 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4641 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4643 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4645 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4646 rs
->supports_vCont
.t
= 1;
4647 else if (*p
== 'r' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4648 rs
->supports_vCont
.r
= 1;
4650 p
= strchr (p
, ';');
4653 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4654 BUF will make packet_ok disable the packet. */
4655 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
4659 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
4662 /* Helper function for building "vCont" resumptions. Write a
4663 resumption to P. ENDP points to one-passed-the-end of the buffer
4664 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4665 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4666 resumed thread should be single-stepped and/or signalled. If PTID
4667 equals minus_one_ptid, then all threads are resumed; if PTID
4668 represents a process, then all threads of the process are resumed;
4669 the thread to be stepped and/or signalled is given in the global
4673 append_resumption (char *p
, char *endp
,
4674 ptid_t ptid
, int step
, enum gdb_signal siggnal
)
4676 struct remote_state
*rs
= get_remote_state ();
4678 if (step
&& siggnal
!= GDB_SIGNAL_0
)
4679 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
4681 /* GDB is willing to range step. */
4682 && use_range_stepping
4683 /* Target supports range stepping. */
4684 && rs
->supports_vCont
.r
4685 /* We don't currently support range stepping multiple
4686 threads with a wildcard (though the protocol allows it,
4687 so stubs shouldn't make an active effort to forbid
4689 && !(remote_multi_process_p (rs
) && ptid_is_pid (ptid
)))
4691 struct thread_info
*tp
;
4693 if (ptid_equal (ptid
, minus_one_ptid
))
4695 /* If we don't know about the target thread's tid, then
4696 we're resuming magic_null_ptid (see caller). */
4697 tp
= find_thread_ptid (magic_null_ptid
);
4700 tp
= find_thread_ptid (ptid
);
4701 gdb_assert (tp
!= NULL
);
4703 if (tp
->control
.may_range_step
)
4705 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
4707 p
+= xsnprintf (p
, endp
- p
, ";r%s,%s",
4708 phex_nz (tp
->control
.step_range_start
,
4710 phex_nz (tp
->control
.step_range_end
,
4714 p
+= xsnprintf (p
, endp
- p
, ";s");
4717 p
+= xsnprintf (p
, endp
- p
, ";s");
4718 else if (siggnal
!= GDB_SIGNAL_0
)
4719 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
4721 p
+= xsnprintf (p
, endp
- p
, ";c");
4723 if (remote_multi_process_p (rs
) && ptid_is_pid (ptid
))
4727 /* All (-1) threads of process. */
4728 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4730 p
+= xsnprintf (p
, endp
- p
, ":");
4731 p
= write_ptid (p
, endp
, nptid
);
4733 else if (!ptid_equal (ptid
, minus_one_ptid
))
4735 p
+= xsnprintf (p
, endp
- p
, ":");
4736 p
= write_ptid (p
, endp
, ptid
);
4742 /* Append a vCont continue-with-signal action for threads that have a
4743 non-zero stop signal. */
4746 append_pending_thread_resumptions (char *p
, char *endp
, ptid_t ptid
)
4748 struct thread_info
*thread
;
4750 ALL_THREADS (thread
)
4751 if (ptid_match (thread
->ptid
, ptid
)
4752 && !ptid_equal (inferior_ptid
, thread
->ptid
)
4753 && thread
->suspend
.stop_signal
!= GDB_SIGNAL_0
4754 && signal_pass_state (thread
->suspend
.stop_signal
))
4756 p
= append_resumption (p
, endp
, thread
->ptid
,
4757 0, thread
->suspend
.stop_signal
);
4758 thread
->suspend
.stop_signal
= GDB_SIGNAL_0
;
4764 /* Resume the remote inferior by using a "vCont" packet. The thread
4765 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4766 resumed thread should be single-stepped and/or signalled. If PTID
4767 equals minus_one_ptid, then all threads are resumed; the thread to
4768 be stepped and/or signalled is given in the global INFERIOR_PTID.
4769 This function returns non-zero iff it resumes the inferior.
4771 This function issues a strict subset of all possible vCont commands at the
4775 remote_vcont_resume (ptid_t ptid
, int step
, enum gdb_signal siggnal
)
4777 struct remote_state
*rs
= get_remote_state ();
4781 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4782 remote_vcont_probe (rs
);
4784 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
4788 endp
= rs
->buf
+ get_remote_packet_size ();
4790 /* If we could generate a wider range of packets, we'd have to worry
4791 about overflowing BUF. Should there be a generic
4792 "multi-part-packet" packet? */
4794 p
+= xsnprintf (p
, endp
- p
, "vCont");
4796 if (ptid_equal (ptid
, magic_null_ptid
))
4798 /* MAGIC_NULL_PTID means that we don't have any active threads,
4799 so we don't have any TID numbers the inferior will
4800 understand. Make sure to only send forms that do not specify
4802 append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
4804 else if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
4806 /* Resume all threads (of all processes, or of a single
4807 process), with preference for INFERIOR_PTID. This assumes
4808 inferior_ptid belongs to the set of all threads we are about
4810 if (step
|| siggnal
!= GDB_SIGNAL_0
)
4812 /* Step inferior_ptid, with or without signal. */
4813 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
4816 /* Also pass down any pending signaled resumption for other
4817 threads not the current. */
4818 p
= append_pending_thread_resumptions (p
, endp
, ptid
);
4820 /* And continue others without a signal. */
4821 append_resumption (p
, endp
, ptid
, /*step=*/ 0, GDB_SIGNAL_0
);
4825 /* Scheduler locking; resume only PTID. */
4826 append_resumption (p
, endp
, ptid
, step
, siggnal
);
4829 gdb_assert (strlen (rs
->buf
) < get_remote_packet_size ());
4834 /* In non-stop, the stub replies to vCont with "OK". The stop
4835 reply will be reported asynchronously by means of a `%Stop'
4837 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4838 if (strcmp (rs
->buf
, "OK") != 0)
4839 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
);
4845 /* Tell the remote machine to resume. */
4848 remote_resume (struct target_ops
*ops
,
4849 ptid_t ptid
, int step
, enum gdb_signal siggnal
)
4851 struct remote_state
*rs
= get_remote_state ();
4854 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
4855 (explained in remote-notif.c:handle_notification) so
4856 remote_notif_process is not called. We need find a place where
4857 it is safe to start a 'vNotif' sequence. It is good to do it
4858 before resuming inferior, because inferior was stopped and no RSP
4859 traffic at that moment. */
4861 remote_notif_process (rs
->notif_state
, ¬if_client_stop
);
4863 rs
->last_sent_signal
= siggnal
;
4864 rs
->last_sent_step
= step
;
4866 /* The vCont packet doesn't need to specify threads via Hc. */
4867 /* No reverse support (yet) for vCont. */
4868 if (execution_direction
!= EXEC_REVERSE
)
4869 if (remote_vcont_resume (ptid
, step
, siggnal
))
4872 /* All other supported resume packets do use Hc, so set the continue
4874 if (ptid_equal (ptid
, minus_one_ptid
))
4875 set_continue_thread (any_thread_ptid
);
4877 set_continue_thread (ptid
);
4880 if (execution_direction
== EXEC_REVERSE
)
4882 /* We don't pass signals to the target in reverse exec mode. */
4883 if (info_verbose
&& siggnal
!= GDB_SIGNAL_0
)
4884 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
4888 && remote_protocol_packets
[PACKET_bs
].support
== PACKET_DISABLE
)
4889 error (_("Remote reverse-step not supported."));
4891 && remote_protocol_packets
[PACKET_bc
].support
== PACKET_DISABLE
)
4892 error (_("Remote reverse-continue not supported."));
4894 strcpy (buf
, step
? "bs" : "bc");
4896 else if (siggnal
!= GDB_SIGNAL_0
)
4898 buf
[0] = step
? 'S' : 'C';
4899 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
4900 buf
[2] = tohex (((int) siggnal
) & 0xf);
4904 strcpy (buf
, step
? "s" : "c");
4909 /* We are about to start executing the inferior, let's register it
4910 with the event loop. NOTE: this is the one place where all the
4911 execution commands end up. We could alternatively do this in each
4912 of the execution commands in infcmd.c. */
4913 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4914 into infcmd.c in order to allow inferior function calls to work
4915 NOT asynchronously. */
4916 if (target_can_async_p ())
4917 target_async (inferior_event_handler
, 0);
4919 /* We've just told the target to resume. The remote server will
4920 wait for the inferior to stop, and then send a stop reply. In
4921 the mean time, we can't start another command/query ourselves
4922 because the stub wouldn't be ready to process it. This applies
4923 only to the base all-stop protocol, however. In non-stop (which
4924 only supports vCont), the stub replies with an "OK", and is
4925 immediate able to process further serial input. */
4927 rs
->waiting_for_stop_reply
= 1;
4931 /* Set up the signal handler for SIGINT, while the target is
4932 executing, ovewriting the 'regular' SIGINT signal handler. */
4934 async_initialize_sigint_signal_handler (void)
4936 signal (SIGINT
, async_handle_remote_sigint
);
4939 /* Signal handler for SIGINT, while the target is executing. */
4941 async_handle_remote_sigint (int sig
)
4943 signal (sig
, async_handle_remote_sigint_twice
);
4944 mark_async_signal_handler (async_sigint_remote_token
);
4947 /* Signal handler for SIGINT, installed after SIGINT has already been
4948 sent once. It will take effect the second time that the user sends
4951 async_handle_remote_sigint_twice (int sig
)
4953 signal (sig
, async_handle_remote_sigint
);
4954 mark_async_signal_handler (async_sigint_remote_twice_token
);
4957 /* Perform the real interruption of the target execution, in response
4960 async_remote_interrupt (gdb_client_data arg
)
4963 fprintf_unfiltered (gdb_stdlog
, "async_remote_interrupt called\n");
4965 target_stop (inferior_ptid
);
4968 /* Perform interrupt, if the first attempt did not succeed. Just give
4969 up on the target alltogether. */
4971 async_remote_interrupt_twice (gdb_client_data arg
)
4974 fprintf_unfiltered (gdb_stdlog
, "async_remote_interrupt_twice called\n");
4979 /* Reinstall the usual SIGINT handlers, after the target has
4982 async_cleanup_sigint_signal_handler (void *dummy
)
4984 signal (SIGINT
, handle_sigint
);
4987 /* Send ^C to target to halt it. Target will respond, and send us a
4989 static void (*ofunc
) (int);
4991 /* The command line interface's stop routine. This function is installed
4992 as a signal handler for SIGINT. The first time a user requests a
4993 stop, we call remote_stop to send a break or ^C. If there is no
4994 response from the target (it didn't stop when the user requested it),
4995 we ask the user if he'd like to detach from the target. */
4997 sync_remote_interrupt (int signo
)
4999 /* If this doesn't work, try more severe steps. */
5000 signal (signo
, sync_remote_interrupt_twice
);
5002 gdb_call_async_signal_handler (async_sigint_remote_token
, 1);
5005 /* The user typed ^C twice. */
5008 sync_remote_interrupt_twice (int signo
)
5010 signal (signo
, ofunc
);
5011 gdb_call_async_signal_handler (async_sigint_remote_twice_token
, 1);
5012 signal (signo
, sync_remote_interrupt
);
5015 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5016 thread, all threads of a remote process, or all threads of all
5020 remote_stop_ns (ptid_t ptid
)
5022 struct remote_state
*rs
= get_remote_state ();
5024 char *endp
= rs
->buf
+ get_remote_packet_size ();
5026 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
5027 remote_vcont_probe (rs
);
5029 if (!rs
->supports_vCont
.t
)
5030 error (_("Remote server does not support stopping threads"));
5032 if (ptid_equal (ptid
, minus_one_ptid
)
5033 || (!remote_multi_process_p (rs
) && ptid_is_pid (ptid
)))
5034 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
5039 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
5041 if (ptid_is_pid (ptid
))
5042 /* All (-1) threads of process. */
5043 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
5046 /* Small optimization: if we already have a stop reply for
5047 this thread, no use in telling the stub we want this
5049 if (peek_stop_reply (ptid
))
5055 write_ptid (p
, endp
, nptid
);
5058 /* In non-stop, we get an immediate OK reply. The stop reply will
5059 come in asynchronously by notification. */
5061 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5062 if (strcmp (rs
->buf
, "OK") != 0)
5063 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
), rs
->buf
);
5066 /* All-stop version of target_stop. Sends a break or a ^C to stop the
5067 remote target. It is undefined which thread of which process
5068 reports the stop. */
5071 remote_stop_as (ptid_t ptid
)
5073 struct remote_state
*rs
= get_remote_state ();
5075 rs
->ctrlc_pending_p
= 1;
5077 /* If the inferior is stopped already, but the core didn't know
5078 about it yet, just ignore the request. The cached wait status
5079 will be collected in remote_wait. */
5080 if (rs
->cached_wait_status
)
5083 /* Send interrupt_sequence to remote target. */
5084 send_interrupt_sequence ();
5087 /* This is the generic stop called via the target vector. When a target
5088 interrupt is requested, either by the command line or the GUI, we
5089 will eventually end up here. */
5092 remote_stop (struct target_ops
*self
, ptid_t ptid
)
5095 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
5098 remote_stop_ns (ptid
);
5100 remote_stop_as (ptid
);
5103 /* Ask the user what to do when an interrupt is received. */
5106 interrupt_query (void)
5108 target_terminal_ours ();
5110 if (target_can_async_p ())
5112 signal (SIGINT
, handle_sigint
);
5117 if (query (_("Interrupted while waiting for the program.\n\
5118 Give up (and stop debugging it)? ")))
5120 remote_unpush_target ();
5125 target_terminal_inferior ();
5128 /* Enable/disable target terminal ownership. Most targets can use
5129 terminal groups to control terminal ownership. Remote targets are
5130 different in that explicit transfer of ownership to/from GDB/target
5134 remote_terminal_inferior (struct target_ops
*self
)
5136 if (!target_async_permitted
)
5137 /* Nothing to do. */
5140 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5141 idempotent. The event-loop GDB talking to an asynchronous target
5142 with a synchronous command calls this function from both
5143 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5144 transfer the terminal to the target when it shouldn't this guard
5146 if (!remote_async_terminal_ours_p
)
5148 delete_file_handler (input_fd
);
5149 remote_async_terminal_ours_p
= 0;
5150 async_initialize_sigint_signal_handler ();
5151 /* NOTE: At this point we could also register our selves as the
5152 recipient of all input. Any characters typed could then be
5153 passed on down to the target. */
5157 remote_terminal_ours (struct target_ops
*self
)
5159 if (!target_async_permitted
)
5160 /* Nothing to do. */
5163 /* See FIXME in remote_terminal_inferior. */
5164 if (remote_async_terminal_ours_p
)
5166 async_cleanup_sigint_signal_handler (NULL
);
5167 add_file_handler (input_fd
, stdin_event_handler
, 0);
5168 remote_async_terminal_ours_p
= 1;
5172 remote_console_output (char *msg
)
5176 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
5179 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
5183 fputs_unfiltered (tb
, gdb_stdtarg
);
5185 gdb_flush (gdb_stdtarg
);
5188 typedef struct cached_reg
5191 gdb_byte data
[MAX_REGISTER_SIZE
];
5194 DEF_VEC_O(cached_reg_t
);
5196 typedef struct stop_reply
5198 struct notif_event base
;
5200 /* The identifier of the thread about this event */
5203 /* The remote state this event is associated with. When the remote
5204 connection, represented by a remote_state object, is closed,
5205 all the associated stop_reply events should be released. */
5206 struct remote_state
*rs
;
5208 struct target_waitstatus ws
;
5210 /* Expedited registers. This makes remote debugging a bit more
5211 efficient for those targets that provide critical registers as
5212 part of their normal status mechanism (as another roundtrip to
5213 fetch them is avoided). */
5214 VEC(cached_reg_t
) *regcache
;
5216 int stopped_by_watchpoint_p
;
5217 CORE_ADDR watch_data_address
;
5222 DECLARE_QUEUE_P (stop_reply_p
);
5223 DEFINE_QUEUE_P (stop_reply_p
);
5224 /* The list of already fetched and acknowledged stop events. This
5225 queue is used for notification Stop, and other notifications
5226 don't need queue for their events, because the notification events
5227 of Stop can't be consumed immediately, so that events should be
5228 queued first, and be consumed by remote_wait_{ns,as} one per
5229 time. Other notifications can consume their events immediately,
5230 so queue is not needed for them. */
5231 static QUEUE (stop_reply_p
) *stop_reply_queue
;
5234 stop_reply_xfree (struct stop_reply
*r
)
5236 notif_event_xfree ((struct notif_event
*) r
);
5240 remote_notif_stop_parse (struct notif_client
*self
, char *buf
,
5241 struct notif_event
*event
)
5243 remote_parse_stop_reply (buf
, (struct stop_reply
*) event
);
5247 remote_notif_stop_ack (struct notif_client
*self
, char *buf
,
5248 struct notif_event
*event
)
5250 struct stop_reply
*stop_reply
= (struct stop_reply
*) event
;
5253 putpkt ((char *) self
->ack_command
);
5255 if (stop_reply
->ws
.kind
== TARGET_WAITKIND_IGNORE
)
5256 /* We got an unknown stop reply. */
5257 error (_("Unknown stop reply"));
5259 push_stop_reply (stop_reply
);
5263 remote_notif_stop_can_get_pending_events (struct notif_client
*self
)
5265 /* We can't get pending events in remote_notif_process for
5266 notification stop, and we have to do this in remote_wait_ns
5267 instead. If we fetch all queued events from stub, remote stub
5268 may exit and we have no chance to process them back in
5270 mark_async_event_handler (remote_async_inferior_event_token
);
5275 stop_reply_dtr (struct notif_event
*event
)
5277 struct stop_reply
*r
= (struct stop_reply
*) event
;
5279 VEC_free (cached_reg_t
, r
->regcache
);
5282 static struct notif_event
*
5283 remote_notif_stop_alloc_reply (void)
5285 struct notif_event
*r
5286 = (struct notif_event
*) XNEW (struct stop_reply
);
5288 r
->dtr
= stop_reply_dtr
;
5293 /* A client of notification Stop. */
5295 struct notif_client notif_client_stop
=
5299 remote_notif_stop_parse
,
5300 remote_notif_stop_ack
,
5301 remote_notif_stop_can_get_pending_events
,
5302 remote_notif_stop_alloc_reply
,
5306 /* A parameter to pass data in and out. */
5308 struct queue_iter_param
5311 struct stop_reply
*output
;
5314 /* Remove stop replies in the queue if its pid is equal to the given
5318 remove_stop_reply_for_inferior (QUEUE (stop_reply_p
) *q
,
5319 QUEUE_ITER (stop_reply_p
) *iter
,
5323 struct queue_iter_param
*param
= data
;
5324 struct inferior
*inf
= param
->input
;
5326 if (ptid_get_pid (event
->ptid
) == inf
->pid
)
5328 stop_reply_xfree (event
);
5329 QUEUE_remove_elem (stop_reply_p
, q
, iter
);
5335 /* Discard all pending stop replies of inferior INF. */
5338 discard_pending_stop_replies (struct inferior
*inf
)
5341 struct queue_iter_param param
;
5342 struct stop_reply
*reply
;
5343 struct remote_state
*rs
= get_remote_state ();
5344 struct remote_notif_state
*rns
= rs
->notif_state
;
5346 /* This function can be notified when an inferior exists. When the
5347 target is not remote, the notification state is NULL. */
5348 if (rs
->remote_desc
== NULL
)
5351 reply
= (struct stop_reply
*) rns
->pending_event
[notif_client_stop
.id
];
5353 /* Discard the in-flight notification. */
5354 if (reply
!= NULL
&& ptid_get_pid (reply
->ptid
) == inf
->pid
)
5356 stop_reply_xfree (reply
);
5357 rns
->pending_event
[notif_client_stop
.id
] = NULL
;
5361 param
.output
= NULL
;
5362 /* Discard the stop replies we have already pulled with
5364 QUEUE_iterate (stop_reply_p
, stop_reply_queue
,
5365 remove_stop_reply_for_inferior
, ¶m
);
5368 /* If its remote state is equal to the given remote state,
5369 remove EVENT from the stop reply queue. */
5372 remove_stop_reply_of_remote_state (QUEUE (stop_reply_p
) *q
,
5373 QUEUE_ITER (stop_reply_p
) *iter
,
5377 struct queue_iter_param
*param
= data
;
5378 struct remote_state
*rs
= param
->input
;
5380 if (event
->rs
== rs
)
5382 stop_reply_xfree (event
);
5383 QUEUE_remove_elem (stop_reply_p
, q
, iter
);
5389 /* Discard the stop replies for RS in stop_reply_queue. */
5392 discard_pending_stop_replies_in_queue (struct remote_state
*rs
)
5394 struct queue_iter_param param
;
5397 param
.output
= NULL
;
5398 /* Discard the stop replies we have already pulled with
5400 QUEUE_iterate (stop_reply_p
, stop_reply_queue
,
5401 remove_stop_reply_of_remote_state
, ¶m
);
5404 /* A parameter to pass data in and out. */
5407 remote_notif_remove_once_on_match (QUEUE (stop_reply_p
) *q
,
5408 QUEUE_ITER (stop_reply_p
) *iter
,
5412 struct queue_iter_param
*param
= data
;
5413 ptid_t
*ptid
= param
->input
;
5415 if (ptid_match (event
->ptid
, *ptid
))
5417 param
->output
= event
;
5418 QUEUE_remove_elem (stop_reply_p
, q
, iter
);
5425 /* Remove the first reply in 'stop_reply_queue' which matches
5428 static struct stop_reply
*
5429 remote_notif_remove_queued_reply (ptid_t ptid
)
5431 struct queue_iter_param param
;
5433 param
.input
= &ptid
;
5434 param
.output
= NULL
;
5436 QUEUE_iterate (stop_reply_p
, stop_reply_queue
,
5437 remote_notif_remove_once_on_match
, ¶m
);
5439 fprintf_unfiltered (gdb_stdlog
,
5440 "notif: discard queued event: 'Stop' in %s\n",
5441 target_pid_to_str (ptid
));
5443 return param
.output
;
5446 /* Look for a queued stop reply belonging to PTID. If one is found,
5447 remove it from the queue, and return it. Returns NULL if none is
5448 found. If there are still queued events left to process, tell the
5449 event loop to get back to target_wait soon. */
5451 static struct stop_reply
*
5452 queued_stop_reply (ptid_t ptid
)
5454 struct stop_reply
*r
= remote_notif_remove_queued_reply (ptid
);
5456 if (!QUEUE_is_empty (stop_reply_p
, stop_reply_queue
))
5457 /* There's still at least an event left. */
5458 mark_async_event_handler (remote_async_inferior_event_token
);
5463 /* Push a fully parsed stop reply in the stop reply queue. Since we
5464 know that we now have at least one queued event left to pass to the
5465 core side, tell the event loop to get back to target_wait soon. */
5468 push_stop_reply (struct stop_reply
*new_event
)
5470 QUEUE_enque (stop_reply_p
, stop_reply_queue
, new_event
);
5473 fprintf_unfiltered (gdb_stdlog
,
5474 "notif: push 'Stop' %s to queue %d\n",
5475 target_pid_to_str (new_event
->ptid
),
5476 QUEUE_length (stop_reply_p
,
5479 mark_async_event_handler (remote_async_inferior_event_token
);
5483 stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p
) *q
,
5484 QUEUE_ITER (stop_reply_p
) *iter
,
5485 struct stop_reply
*event
,
5488 ptid_t
*ptid
= data
;
5490 return !(ptid_equal (*ptid
, event
->ptid
)
5491 && event
->ws
.kind
== TARGET_WAITKIND_STOPPED
);
5494 /* Returns true if we have a stop reply for PTID. */
5497 peek_stop_reply (ptid_t ptid
)
5499 return !QUEUE_iterate (stop_reply_p
, stop_reply_queue
,
5500 stop_reply_match_ptid_and_ws
, &ptid
);
5503 /* Parse the stop reply in BUF. Either the function succeeds, and the
5504 result is stored in EVENT, or throws an error. */
5507 remote_parse_stop_reply (char *buf
, struct stop_reply
*event
)
5509 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5513 event
->ptid
= null_ptid
;
5514 event
->rs
= get_remote_state ();
5515 event
->ws
.kind
= TARGET_WAITKIND_IGNORE
;
5516 event
->ws
.value
.integer
= 0;
5517 event
->stopped_by_watchpoint_p
= 0;
5518 event
->regcache
= NULL
;
5523 case 'T': /* Status with PC, SP, FP, ... */
5524 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5525 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5527 n... = register number
5528 r... = register contents
5531 p
= &buf
[3]; /* after Txx */
5539 /* If the packet contains a register number, save it in
5540 pnum and set p1 to point to the character following it.
5541 Otherwise p1 points to p. */
5543 /* If this packet is an awatch packet, don't parse the 'a'
5544 as a register number. */
5546 if (strncmp (p
, "awatch", strlen("awatch")) != 0
5547 && strncmp (p
, "core", strlen ("core") != 0))
5549 /* Read the ``P'' register number. */
5550 pnum
= strtol (p
, &p_temp
, 16);
5556 if (p1
== p
) /* No register number present here. */
5558 p1
= strchr (p
, ':');
5560 error (_("Malformed packet(a) (missing colon): %s\n\
5563 if (strncmp (p
, "thread", p1
- p
) == 0)
5564 event
->ptid
= read_ptid (++p1
, &p
);
5565 else if ((strncmp (p
, "watch", p1
- p
) == 0)
5566 || (strncmp (p
, "rwatch", p1
- p
) == 0)
5567 || (strncmp (p
, "awatch", p1
- p
) == 0))
5569 event
->stopped_by_watchpoint_p
= 1;
5570 p
= unpack_varlen_hex (++p1
, &addr
);
5571 event
->watch_data_address
= (CORE_ADDR
) addr
;
5573 else if (strncmp (p
, "library", p1
- p
) == 0)
5577 while (*p_temp
&& *p_temp
!= ';')
5580 event
->ws
.kind
= TARGET_WAITKIND_LOADED
;
5583 else if (strncmp (p
, "replaylog", p1
- p
) == 0)
5585 event
->ws
.kind
= TARGET_WAITKIND_NO_HISTORY
;
5586 /* p1 will indicate "begin" or "end", but it makes
5587 no difference for now, so ignore it. */
5588 p_temp
= strchr (p1
+ 1, ';');
5592 else if (strncmp (p
, "core", p1
- p
) == 0)
5596 p
= unpack_varlen_hex (++p1
, &c
);
5601 /* Silently skip unknown optional info. */
5602 p_temp
= strchr (p1
+ 1, ';');
5609 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
5610 cached_reg_t cached_reg
;
5615 error (_("Malformed packet(b) (missing colon): %s\n\
5621 error (_("Remote sent bad register number %s: %s\n\
5623 hex_string (pnum
), p
, buf
);
5625 cached_reg
.num
= reg
->regnum
;
5627 fieldsize
= hex2bin (p
, cached_reg
.data
,
5628 register_size (target_gdbarch (),
5631 if (fieldsize
< register_size (target_gdbarch (),
5633 warning (_("Remote reply is too short: %s"), buf
);
5635 VEC_safe_push (cached_reg_t
, event
->regcache
, &cached_reg
);
5639 error (_("Remote register badly formatted: %s\nhere: %s"),
5644 if (event
->ws
.kind
!= TARGET_WAITKIND_IGNORE
)
5648 case 'S': /* Old style status, just signal only. */
5652 event
->ws
.kind
= TARGET_WAITKIND_STOPPED
;
5653 sig
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
5654 if (GDB_SIGNAL_FIRST
<= sig
&& sig
< GDB_SIGNAL_LAST
)
5655 event
->ws
.value
.sig
= (enum gdb_signal
) sig
;
5657 event
->ws
.value
.sig
= GDB_SIGNAL_UNKNOWN
;
5660 case 'W': /* Target exited. */
5667 /* GDB used to accept only 2 hex chars here. Stubs should
5668 only send more if they detect GDB supports multi-process
5670 p
= unpack_varlen_hex (&buf
[1], &value
);
5674 /* The remote process exited. */
5675 event
->ws
.kind
= TARGET_WAITKIND_EXITED
;
5676 event
->ws
.value
.integer
= value
;
5680 /* The remote process exited with a signal. */
5681 event
->ws
.kind
= TARGET_WAITKIND_SIGNALLED
;
5682 if (GDB_SIGNAL_FIRST
<= value
&& value
< GDB_SIGNAL_LAST
)
5683 event
->ws
.value
.sig
= (enum gdb_signal
) value
;
5685 event
->ws
.value
.sig
= GDB_SIGNAL_UNKNOWN
;
5688 /* If no process is specified, assume inferior_ptid. */
5689 pid
= ptid_get_pid (inferior_ptid
);
5698 else if (strncmp (p
,
5699 "process:", sizeof ("process:") - 1) == 0)
5703 p
+= sizeof ("process:") - 1;
5704 unpack_varlen_hex (p
, &upid
);
5708 error (_("unknown stop reply packet: %s"), buf
);
5711 error (_("unknown stop reply packet: %s"), buf
);
5712 event
->ptid
= pid_to_ptid (pid
);
5717 if (non_stop
&& ptid_equal (event
->ptid
, null_ptid
))
5718 error (_("No process or thread specified in stop reply: %s"), buf
);
5721 /* When the stub wants to tell GDB about a new notification reply, it
5722 sends a notification (%Stop, for example). Those can come it at
5723 any time, hence, we have to make sure that any pending
5724 putpkt/getpkt sequence we're making is finished, before querying
5725 the stub for more events with the corresponding ack command
5726 (vStopped, for example). E.g., if we started a vStopped sequence
5727 immediately upon receiving the notification, something like this
5735 1.6) <-- (registers reply to step #1.3)
5737 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5740 To solve this, whenever we parse a %Stop notification successfully,
5741 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5742 doing whatever we were doing:
5748 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5749 2.5) <-- (registers reply to step #2.3)
5751 Eventualy after step #2.5, we return to the event loop, which
5752 notices there's an event on the
5753 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5754 associated callback --- the function below. At this point, we're
5755 always safe to start a vStopped sequence. :
5758 2.7) <-- T05 thread:2
5764 remote_notif_get_pending_events (struct notif_client
*nc
)
5766 struct remote_state
*rs
= get_remote_state ();
5768 if (rs
->notif_state
->pending_event
[nc
->id
] != NULL
)
5771 fprintf_unfiltered (gdb_stdlog
,
5772 "notif: process: '%s' ack pending event\n",
5776 nc
->ack (nc
, rs
->buf
, rs
->notif_state
->pending_event
[nc
->id
]);
5777 rs
->notif_state
->pending_event
[nc
->id
] = NULL
;
5781 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5782 if (strcmp (rs
->buf
, "OK") == 0)
5785 remote_notif_ack (nc
, rs
->buf
);
5791 fprintf_unfiltered (gdb_stdlog
,
5792 "notif: process: '%s' no pending reply\n",
5797 /* Called when it is decided that STOP_REPLY holds the info of the
5798 event that is to be returned to the core. This function always
5799 destroys STOP_REPLY. */
5802 process_stop_reply (struct stop_reply
*stop_reply
,
5803 struct target_waitstatus
*status
)
5807 *status
= stop_reply
->ws
;
5808 ptid
= stop_reply
->ptid
;
5810 /* If no thread/process was reported by the stub, assume the current
5812 if (ptid_equal (ptid
, null_ptid
))
5813 ptid
= inferior_ptid
;
5815 if (status
->kind
!= TARGET_WAITKIND_EXITED
5816 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5818 struct remote_state
*rs
= get_remote_state ();
5820 /* Expedited registers. */
5821 if (stop_reply
->regcache
)
5823 struct regcache
*regcache
5824 = get_thread_arch_regcache (ptid
, target_gdbarch ());
5829 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
5831 regcache_raw_supply (regcache
, reg
->num
, reg
->data
);
5832 VEC_free (cached_reg_t
, stop_reply
->regcache
);
5835 rs
->remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
5836 rs
->remote_watch_data_address
= stop_reply
->watch_data_address
;
5838 remote_notice_new_inferior (ptid
, 0);
5839 demand_private_info (ptid
)->core
= stop_reply
->core
;
5842 stop_reply_xfree (stop_reply
);
5846 /* The non-stop mode version of target_wait. */
5849 remote_wait_ns (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5851 struct remote_state
*rs
= get_remote_state ();
5852 struct stop_reply
*stop_reply
;
5856 /* If in non-stop mode, get out of getpkt even if a
5857 notification is received. */
5859 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5860 0 /* forever */, &is_notif
);
5863 if (ret
!= -1 && !is_notif
)
5866 case 'E': /* Error of some sort. */
5867 /* We're out of sync with the target now. Did it continue
5868 or not? We can't tell which thread it was in non-stop,
5869 so just ignore this. */
5870 warning (_("Remote failure reply: %s"), rs
->buf
);
5872 case 'O': /* Console output. */
5873 remote_console_output (rs
->buf
+ 1);
5876 warning (_("Invalid remote reply: %s"), rs
->buf
);
5880 /* Acknowledge a pending stop reply that may have arrived in the
5882 if (rs
->notif_state
->pending_event
[notif_client_stop
.id
] != NULL
)
5883 remote_notif_get_pending_events (¬if_client_stop
);
5885 /* If indeed we noticed a stop reply, we're done. */
5886 stop_reply
= queued_stop_reply (ptid
);
5887 if (stop_reply
!= NULL
)
5888 return process_stop_reply (stop_reply
, status
);
5890 /* Still no event. If we're just polling for an event, then
5891 return to the event loop. */
5892 if (options
& TARGET_WNOHANG
)
5894 status
->kind
= TARGET_WAITKIND_IGNORE
;
5895 return minus_one_ptid
;
5898 /* Otherwise do a blocking wait. */
5899 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5900 1 /* forever */, &is_notif
);
5904 /* Wait until the remote machine stops, then return, storing status in
5905 STATUS just as `wait' would. */
5908 remote_wait_as (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5910 struct remote_state
*rs
= get_remote_state ();
5911 ptid_t event_ptid
= null_ptid
;
5913 struct stop_reply
*stop_reply
;
5917 status
->kind
= TARGET_WAITKIND_IGNORE
;
5918 status
->value
.integer
= 0;
5920 stop_reply
= queued_stop_reply (ptid
);
5921 if (stop_reply
!= NULL
)
5922 return process_stop_reply (stop_reply
, status
);
5924 if (rs
->cached_wait_status
)
5925 /* Use the cached wait status, but only once. */
5926 rs
->cached_wait_status
= 0;
5932 if (!target_is_async_p ())
5934 ofunc
= signal (SIGINT
, sync_remote_interrupt
);
5935 /* If the user hit C-c before this packet, or between packets,
5936 pretend that it was hit right here. */
5937 if (check_quit_flag ())
5940 sync_remote_interrupt (SIGINT
);
5944 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5945 _never_ wait for ever -> test on target_is_async_p().
5946 However, before we do that we need to ensure that the caller
5947 knows how to take the target into/out of async mode. */
5948 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5949 wait_forever_enabled_p
, &is_notif
);
5951 if (!target_is_async_p ())
5952 signal (SIGINT
, ofunc
);
5954 /* GDB gets a notification. Return to core as this event is
5956 if (ret
!= -1 && is_notif
)
5957 return minus_one_ptid
;
5962 rs
->remote_stopped_by_watchpoint_p
= 0;
5964 /* We got something. */
5965 rs
->waiting_for_stop_reply
= 0;
5967 /* Assume that the target has acknowledged Ctrl-C unless we receive
5968 an 'F' or 'O' packet. */
5969 if (buf
[0] != 'F' && buf
[0] != 'O')
5970 rs
->ctrlc_pending_p
= 0;
5974 case 'E': /* Error of some sort. */
5975 /* We're out of sync with the target now. Did it continue or
5976 not? Not is more likely, so report a stop. */
5977 warning (_("Remote failure reply: %s"), buf
);
5978 status
->kind
= TARGET_WAITKIND_STOPPED
;
5979 status
->value
.sig
= GDB_SIGNAL_0
;
5981 case 'F': /* File-I/O request. */
5982 remote_fileio_request (buf
, rs
->ctrlc_pending_p
);
5983 rs
->ctrlc_pending_p
= 0;
5985 case 'T': case 'S': case 'X': case 'W':
5987 struct stop_reply
*stop_reply
5988 = (struct stop_reply
*) remote_notif_parse (¬if_client_stop
,
5991 event_ptid
= process_stop_reply (stop_reply
, status
);
5994 case 'O': /* Console output. */
5995 remote_console_output (buf
+ 1);
5997 /* The target didn't really stop; keep waiting. */
5998 rs
->waiting_for_stop_reply
= 1;
6002 if (rs
->last_sent_signal
!= GDB_SIGNAL_0
)
6004 /* Zero length reply means that we tried 'S' or 'C' and the
6005 remote system doesn't support it. */
6006 target_terminal_ours_for_output ();
6008 ("Can't send signals to this remote system. %s not sent.\n",
6009 gdb_signal_to_name (rs
->last_sent_signal
));
6010 rs
->last_sent_signal
= GDB_SIGNAL_0
;
6011 target_terminal_inferior ();
6013 strcpy ((char *) buf
, rs
->last_sent_step
? "s" : "c");
6014 putpkt ((char *) buf
);
6016 /* We just told the target to resume, so a stop reply is in
6018 rs
->waiting_for_stop_reply
= 1;
6021 /* else fallthrough */
6023 warning (_("Invalid remote reply: %s"), buf
);
6025 rs
->waiting_for_stop_reply
= 1;
6029 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
6031 /* Nothing interesting happened. If we're doing a non-blocking
6032 poll, we're done. Otherwise, go back to waiting. */
6033 if (options
& TARGET_WNOHANG
)
6034 return minus_one_ptid
;
6038 else if (status
->kind
!= TARGET_WAITKIND_EXITED
6039 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
6041 if (!ptid_equal (event_ptid
, null_ptid
))
6042 record_currthread (rs
, event_ptid
);
6044 event_ptid
= inferior_ptid
;
6047 /* A process exit. Invalidate our notion of current thread. */
6048 record_currthread (rs
, minus_one_ptid
);
6053 /* Wait until the remote machine stops, then return, storing status in
6054 STATUS just as `wait' would. */
6057 remote_wait (struct target_ops
*ops
,
6058 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
6063 event_ptid
= remote_wait_ns (ptid
, status
, options
);
6065 event_ptid
= remote_wait_as (ptid
, status
, options
);
6067 if (target_can_async_p ())
6069 /* If there are are events left in the queue tell the event loop
6071 if (!QUEUE_is_empty (stop_reply_p
, stop_reply_queue
))
6072 mark_async_event_handler (remote_async_inferior_event_token
);
6078 /* Fetch a single register using a 'p' packet. */
6081 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
6083 struct remote_state
*rs
= get_remote_state ();
6085 char regp
[MAX_REGISTER_SIZE
];
6088 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
6091 if (reg
->pnum
== -1)
6096 p
+= hexnumstr (p
, reg
->pnum
);
6099 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6103 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
6107 case PACKET_UNKNOWN
:
6110 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6111 gdbarch_register_name (get_regcache_arch (regcache
),
6116 /* If this register is unfetchable, tell the regcache. */
6119 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
6123 /* Otherwise, parse and supply the value. */
6129 error (_("fetch_register_using_p: early buf termination"));
6131 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
6134 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
6138 /* Fetch the registers included in the target's 'g' packet. */
6141 send_g_packet (void)
6143 struct remote_state
*rs
= get_remote_state ();
6146 xsnprintf (rs
->buf
, get_remote_packet_size (), "g");
6147 remote_send (&rs
->buf
, &rs
->buf_size
);
6149 /* We can get out of synch in various cases. If the first character
6150 in the buffer is not a hex character, assume that has happened
6151 and try to fetch another packet to read. */
6152 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
6153 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
6154 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
6155 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
6158 fprintf_unfiltered (gdb_stdlog
,
6159 "Bad register packet; fetching a new packet\n");
6160 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6163 buf_len
= strlen (rs
->buf
);
6165 /* Sanity check the received packet. */
6166 if (buf_len
% 2 != 0)
6167 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
6173 process_g_packet (struct regcache
*regcache
)
6175 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
6176 struct remote_state
*rs
= get_remote_state ();
6177 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6182 buf_len
= strlen (rs
->buf
);
6184 /* Further sanity checks, with knowledge of the architecture. */
6185 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
6186 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
6188 /* Save the size of the packet sent to us by the target. It is used
6189 as a heuristic when determining the max size of packets that the
6190 target can safely receive. */
6191 if (rsa
->actual_register_packet_size
== 0)
6192 rsa
->actual_register_packet_size
= buf_len
;
6194 /* If this is smaller than we guessed the 'g' packet would be,
6195 update our records. A 'g' reply that doesn't include a register's
6196 value implies either that the register is not available, or that
6197 the 'p' packet must be used. */
6198 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
6200 rsa
->sizeof_g_packet
= buf_len
/ 2;
6202 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
6204 if (rsa
->regs
[i
].pnum
== -1)
6207 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
6208 rsa
->regs
[i
].in_g_packet
= 0;
6210 rsa
->regs
[i
].in_g_packet
= 1;
6214 regs
= alloca (rsa
->sizeof_g_packet
);
6216 /* Unimplemented registers read as all bits zero. */
6217 memset (regs
, 0, rsa
->sizeof_g_packet
);
6219 /* Reply describes registers byte by byte, each byte encoded as two
6220 hex characters. Suck them all up, then supply them to the
6221 register cacheing/storage mechanism. */
6224 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
6226 if (p
[0] == 0 || p
[1] == 0)
6227 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6228 internal_error (__FILE__
, __LINE__
,
6229 _("unexpected end of 'g' packet reply"));
6231 if (p
[0] == 'x' && p
[1] == 'x')
6232 regs
[i
] = 0; /* 'x' */
6234 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
6238 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
6240 struct packet_reg
*r
= &rsa
->regs
[i
];
6244 if (r
->offset
* 2 >= strlen (rs
->buf
))
6245 /* This shouldn't happen - we adjusted in_g_packet above. */
6246 internal_error (__FILE__
, __LINE__
,
6247 _("unexpected end of 'g' packet reply"));
6248 else if (rs
->buf
[r
->offset
* 2] == 'x')
6250 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
6251 /* The register isn't available, mark it as such (at
6252 the same time setting the value to zero). */
6253 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
6256 regcache_raw_supply (regcache
, r
->regnum
,
6263 fetch_registers_using_g (struct regcache
*regcache
)
6266 process_g_packet (regcache
);
6269 /* Make the remote selected traceframe match GDB's selected
6273 set_remote_traceframe (void)
6276 struct remote_state
*rs
= get_remote_state ();
6278 if (rs
->remote_traceframe_number
== get_traceframe_number ())
6281 /* Avoid recursion, remote_trace_find calls us again. */
6282 rs
->remote_traceframe_number
= get_traceframe_number ();
6284 newnum
= target_trace_find (tfind_number
,
6285 get_traceframe_number (), 0, 0, NULL
);
6287 /* Should not happen. If it does, all bets are off. */
6288 if (newnum
!= get_traceframe_number ())
6289 warning (_("could not set remote traceframe"));
6293 remote_fetch_registers (struct target_ops
*ops
,
6294 struct regcache
*regcache
, int regnum
)
6296 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6299 set_remote_traceframe ();
6300 set_general_thread (inferior_ptid
);
6304 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
6306 gdb_assert (reg
!= NULL
);
6308 /* If this register might be in the 'g' packet, try that first -
6309 we are likely to read more than one register. If this is the
6310 first 'g' packet, we might be overly optimistic about its
6311 contents, so fall back to 'p'. */
6312 if (reg
->in_g_packet
)
6314 fetch_registers_using_g (regcache
);
6315 if (reg
->in_g_packet
)
6319 if (fetch_register_using_p (regcache
, reg
))
6322 /* This register is not available. */
6323 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
6328 fetch_registers_using_g (regcache
);
6330 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6331 if (!rsa
->regs
[i
].in_g_packet
)
6332 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
6334 /* This register is not available. */
6335 regcache_raw_supply (regcache
, i
, NULL
);
6339 /* Prepare to store registers. Since we may send them all (using a
6340 'G' request), we have to read out the ones we don't want to change
6344 remote_prepare_to_store (struct target_ops
*self
, struct regcache
*regcache
)
6346 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6348 gdb_byte buf
[MAX_REGISTER_SIZE
];
6350 /* Make sure the entire registers array is valid. */
6351 switch (remote_protocol_packets
[PACKET_P
].support
)
6353 case PACKET_DISABLE
:
6354 case PACKET_SUPPORT_UNKNOWN
:
6355 /* Make sure all the necessary registers are cached. */
6356 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6357 if (rsa
->regs
[i
].in_g_packet
)
6358 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
6365 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
6366 packet was not recognized. */
6369 store_register_using_P (const struct regcache
*regcache
,
6370 struct packet_reg
*reg
)
6372 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
6373 struct remote_state
*rs
= get_remote_state ();
6374 /* Try storing a single register. */
6375 char *buf
= rs
->buf
;
6376 gdb_byte regp
[MAX_REGISTER_SIZE
];
6379 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
6382 if (reg
->pnum
== -1)
6385 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
6386 p
= buf
+ strlen (buf
);
6387 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
6388 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
6390 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6392 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
6397 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6398 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
);
6399 case PACKET_UNKNOWN
:
6402 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
6406 /* Store register REGNUM, or all registers if REGNUM == -1, from the
6407 contents of the register cache buffer. FIXME: ignores errors. */
6410 store_registers_using_G (const struct regcache
*regcache
)
6412 struct remote_state
*rs
= get_remote_state ();
6413 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6417 /* Extract all the registers in the regcache copying them into a
6422 regs
= alloca (rsa
->sizeof_g_packet
);
6423 memset (regs
, 0, rsa
->sizeof_g_packet
);
6424 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6426 struct packet_reg
*r
= &rsa
->regs
[i
];
6429 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
6433 /* Command describes registers byte by byte,
6434 each byte encoded as two hex characters. */
6437 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6439 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
6441 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6442 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
6443 error (_("Could not write registers; remote failure reply '%s'"),
6447 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6448 of the register cache buffer. FIXME: ignores errors. */
6451 remote_store_registers (struct target_ops
*ops
,
6452 struct regcache
*regcache
, int regnum
)
6454 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6457 set_remote_traceframe ();
6458 set_general_thread (inferior_ptid
);
6462 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
6464 gdb_assert (reg
!= NULL
);
6466 /* Always prefer to store registers using the 'P' packet if
6467 possible; we often change only a small number of registers.
6468 Sometimes we change a larger number; we'd need help from a
6469 higher layer to know to use 'G'. */
6470 if (store_register_using_P (regcache
, reg
))
6473 /* For now, don't complain if we have no way to write the
6474 register. GDB loses track of unavailable registers too
6475 easily. Some day, this may be an error. We don't have
6476 any way to read the register, either... */
6477 if (!reg
->in_g_packet
)
6480 store_registers_using_G (regcache
);
6484 store_registers_using_G (regcache
);
6486 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6487 if (!rsa
->regs
[i
].in_g_packet
)
6488 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
6489 /* See above for why we do not issue an error here. */
6494 /* Return the number of hex digits in num. */
6497 hexnumlen (ULONGEST num
)
6501 for (i
= 0; num
!= 0; i
++)
6507 /* Set BUF to the minimum number of hex digits representing NUM. */
6510 hexnumstr (char *buf
, ULONGEST num
)
6512 int len
= hexnumlen (num
);
6514 return hexnumnstr (buf
, num
, len
);
6518 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6521 hexnumnstr (char *buf
, ULONGEST num
, int width
)
6527 for (i
= width
- 1; i
>= 0; i
--)
6529 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
6536 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6539 remote_address_masked (CORE_ADDR addr
)
6541 unsigned int address_size
= remote_address_size
;
6543 /* If "remoteaddresssize" was not set, default to target address size. */
6545 address_size
= gdbarch_addr_bit (target_gdbarch ());
6547 if (address_size
> 0
6548 && address_size
< (sizeof (ULONGEST
) * 8))
6550 /* Only create a mask when that mask can safely be constructed
6551 in a ULONGEST variable. */
6554 mask
= (mask
<< address_size
) - 1;
6560 /* Determine whether the remote target supports binary downloading.
6561 This is accomplished by sending a no-op memory write of zero length
6562 to the target at the specified address. It does not suffice to send
6563 the whole packet, since many stubs strip the eighth bit and
6564 subsequently compute a wrong checksum, which causes real havoc with
6567 NOTE: This can still lose if the serial line is not eight-bit
6568 clean. In cases like this, the user should clear "remote
6572 check_binary_download (CORE_ADDR addr
)
6574 struct remote_state
*rs
= get_remote_state ();
6576 switch (remote_protocol_packets
[PACKET_X
].support
)
6578 case PACKET_DISABLE
:
6582 case PACKET_SUPPORT_UNKNOWN
:
6588 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6590 p
+= hexnumstr (p
, (ULONGEST
) 0);
6594 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6595 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6597 if (rs
->buf
[0] == '\0')
6600 fprintf_unfiltered (gdb_stdlog
,
6601 "binary downloading NOT "
6602 "supported by target\n");
6603 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
6608 fprintf_unfiltered (gdb_stdlog
,
6609 "binary downloading supported by target\n");
6610 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
6617 /* Write memory data directly to the remote machine.
6618 This does not inform the data cache; the data cache uses this.
6619 HEADER is the starting part of the packet.
6620 MEMADDR is the address in the remote memory space.
6621 MYADDR is the address of the buffer in our space.
6622 LEN is the number of bytes.
6623 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6624 should send data as binary ('X'), or hex-encoded ('M').
6626 The function creates packet of the form
6627 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6629 where encoding of <DATA> is termined by PACKET_FORMAT.
6631 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6634 Return the transferred status, error or OK (an
6635 'enum target_xfer_status' value). Save the number of bytes
6636 transferred in *XFERED_LEN. Only transfer a single packet. */
6638 static enum target_xfer_status
6639 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
6640 const gdb_byte
*myaddr
, ULONGEST len
,
6641 ULONGEST
*xfered_len
, char packet_format
,
6644 struct remote_state
*rs
= get_remote_state ();
6654 if (packet_format
!= 'X' && packet_format
!= 'M')
6655 internal_error (__FILE__
, __LINE__
,
6656 _("remote_write_bytes_aux: bad packet format"));
6659 return TARGET_XFER_EOF
;
6661 payload_size
= get_memory_write_packet_size ();
6663 /* The packet buffer will be large enough for the payload;
6664 get_memory_packet_size ensures this. */
6667 /* Compute the size of the actual payload by subtracting out the
6668 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6670 payload_size
-= strlen ("$,:#NN");
6672 /* The comma won't be used. */
6674 header_length
= strlen (header
);
6675 payload_size
-= header_length
;
6676 payload_size
-= hexnumlen (memaddr
);
6678 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6680 strcat (rs
->buf
, header
);
6681 p
= rs
->buf
+ strlen (header
);
6683 /* Compute a best guess of the number of bytes actually transfered. */
6684 if (packet_format
== 'X')
6686 /* Best guess at number of bytes that will fit. */
6687 todo
= min (len
, payload_size
);
6689 payload_size
-= hexnumlen (todo
);
6690 todo
= min (todo
, payload_size
);
6694 /* Num bytes that will fit. */
6695 todo
= min (len
, payload_size
/ 2);
6697 payload_size
-= hexnumlen (todo
);
6698 todo
= min (todo
, payload_size
/ 2);
6702 internal_error (__FILE__
, __LINE__
,
6703 _("minimum packet size too small to write data"));
6705 /* If we already need another packet, then try to align the end
6706 of this packet to a useful boundary. */
6707 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
6708 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
6710 /* Append "<memaddr>". */
6711 memaddr
= remote_address_masked (memaddr
);
6712 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6719 /* Append <len>. Retain the location/size of <len>. It may need to
6720 be adjusted once the packet body has been created. */
6722 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
6730 /* Append the packet body. */
6731 if (packet_format
== 'X')
6733 /* Binary mode. Send target system values byte by byte, in
6734 increasing byte addresses. Only escape certain critical
6736 payload_length
= remote_escape_output (myaddr
, todo
, (gdb_byte
*) p
,
6737 &nr_bytes
, payload_size
);
6739 /* If not all TODO bytes fit, then we'll need another packet. Make
6740 a second try to keep the end of the packet aligned. Don't do
6741 this if the packet is tiny. */
6742 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
6746 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
6748 if (new_nr_bytes
!= nr_bytes
)
6749 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
6750 (gdb_byte
*) p
, &nr_bytes
,
6754 p
+= payload_length
;
6755 if (use_length
&& nr_bytes
< todo
)
6757 /* Escape chars have filled up the buffer prematurely,
6758 and we have actually sent fewer bytes than planned.
6759 Fix-up the length field of the packet. Use the same
6760 number of characters as before. */
6761 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
6762 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
6767 /* Normal mode: Send target system values byte by byte, in
6768 increasing byte addresses. Each byte is encoded as a two hex
6770 nr_bytes
= bin2hex (myaddr
, p
, todo
);
6774 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6775 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6777 if (rs
->buf
[0] == 'E')
6778 return TARGET_XFER_E_IO
;
6780 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6781 fewer bytes than we'd planned. */
6782 *xfered_len
= (ULONGEST
) nr_bytes
;
6783 return TARGET_XFER_OK
;
6786 /* Write memory data directly to the remote machine.
6787 This does not inform the data cache; the data cache uses this.
6788 MEMADDR is the address in the remote memory space.
6789 MYADDR is the address of the buffer in our space.
6790 LEN is the number of bytes.
6792 Return the transferred status, error or OK (an
6793 'enum target_xfer_status' value). Save the number of bytes
6794 transferred in *XFERED_LEN. Only transfer a single packet. */
6796 static enum target_xfer_status
6797 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ULONGEST len
,
6798 ULONGEST
*xfered_len
)
6800 char *packet_format
= 0;
6802 /* Check whether the target supports binary download. */
6803 check_binary_download (memaddr
);
6805 switch (remote_protocol_packets
[PACKET_X
].support
)
6808 packet_format
= "X";
6810 case PACKET_DISABLE
:
6811 packet_format
= "M";
6813 case PACKET_SUPPORT_UNKNOWN
:
6814 internal_error (__FILE__
, __LINE__
,
6815 _("remote_write_bytes: bad internal state"));
6817 internal_error (__FILE__
, __LINE__
, _("bad switch"));
6820 return remote_write_bytes_aux (packet_format
,
6821 memaddr
, myaddr
, len
, xfered_len
,
6822 packet_format
[0], 1);
6825 /* Read memory data directly from the remote machine.
6826 This does not use the data cache; the data cache uses this.
6827 MEMADDR is the address in the remote memory space.
6828 MYADDR is the address of the buffer in our space.
6829 LEN is the number of bytes.
6831 Return the transferred status, error or OK (an
6832 'enum target_xfer_status' value). Save the number of bytes
6833 transferred in *XFERED_LEN. */
6835 static enum target_xfer_status
6836 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ULONGEST len
,
6837 ULONGEST
*xfered_len
)
6839 struct remote_state
*rs
= get_remote_state ();
6840 int max_buf_size
; /* Max size of packet output buffer. */
6848 max_buf_size
= get_memory_read_packet_size ();
6849 /* The packet buffer will be large enough for the payload;
6850 get_memory_packet_size ensures this. */
6852 /* Number if bytes that will fit. */
6853 todo
= min (len
, max_buf_size
/ 2);
6855 /* Construct "m"<memaddr>","<len>". */
6856 memaddr
= remote_address_masked (memaddr
);
6859 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6861 p
+= hexnumstr (p
, (ULONGEST
) todo
);
6864 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6865 if (rs
->buf
[0] == 'E'
6866 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
6867 && rs
->buf
[3] == '\0')
6868 return TARGET_XFER_E_IO
;
6869 /* Reply describes memory byte by byte, each byte encoded as two hex
6872 i
= hex2bin (p
, myaddr
, todo
);
6873 /* Return what we have. Let higher layers handle partial reads. */
6874 *xfered_len
= (ULONGEST
) i
;
6875 return TARGET_XFER_OK
;
6880 /* Sends a packet with content determined by the printf format string
6881 FORMAT and the remaining arguments, then gets the reply. Returns
6882 whether the packet was a success, a failure, or unknown. */
6884 static enum packet_result
6885 remote_send_printf (const char *format
, ...)
6887 struct remote_state
*rs
= get_remote_state ();
6888 int max_size
= get_remote_packet_size ();
6891 va_start (ap
, format
);
6894 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
6895 internal_error (__FILE__
, __LINE__
, _("Too long remote packet."));
6897 if (putpkt (rs
->buf
) < 0)
6898 error (_("Communication problem with target."));
6901 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6903 return packet_check_result (rs
->buf
);
6907 restore_remote_timeout (void *p
)
6909 int value
= *(int *)p
;
6911 remote_timeout
= value
;
6914 /* Flash writing can take quite some time. We'll set
6915 effectively infinite timeout for flash operations.
6916 In future, we'll need to decide on a better approach. */
6917 static const int remote_flash_timeout
= 1000;
6920 remote_flash_erase (struct target_ops
*ops
,
6921 ULONGEST address
, LONGEST length
)
6923 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
6924 int saved_remote_timeout
= remote_timeout
;
6925 enum packet_result ret
;
6926 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6927 &saved_remote_timeout
);
6929 remote_timeout
= remote_flash_timeout
;
6931 ret
= remote_send_printf ("vFlashErase:%s,%s",
6932 phex (address
, addr_size
),
6936 case PACKET_UNKNOWN
:
6937 error (_("Remote target does not support flash erase"));
6939 error (_("Error erasing flash with vFlashErase packet"));
6944 do_cleanups (back_to
);
6947 static enum target_xfer_status
6948 remote_flash_write (struct target_ops
*ops
, ULONGEST address
,
6949 ULONGEST length
, ULONGEST
*xfered_len
,
6950 const gdb_byte
*data
)
6952 int saved_remote_timeout
= remote_timeout
;
6953 enum target_xfer_status ret
;
6954 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6955 &saved_remote_timeout
);
6957 remote_timeout
= remote_flash_timeout
;
6958 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
,
6960 do_cleanups (back_to
);
6966 remote_flash_done (struct target_ops
*ops
)
6968 int saved_remote_timeout
= remote_timeout
;
6970 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6971 &saved_remote_timeout
);
6973 remote_timeout
= remote_flash_timeout
;
6974 ret
= remote_send_printf ("vFlashDone");
6975 do_cleanups (back_to
);
6979 case PACKET_UNKNOWN
:
6980 error (_("Remote target does not support vFlashDone"));
6982 error (_("Error finishing flash operation"));
6989 remote_files_info (struct target_ops
*ignore
)
6991 puts_filtered ("Debugging a target over a serial line.\n");
6994 /* Stuff for dealing with the packets which are part of this protocol.
6995 See comment at top of file for details. */
6997 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
6998 error to higher layers. Called when a serial error is detected.
6999 The exception message is STRING, followed by a colon and a blank,
7000 the system error message for errno at function entry and final dot
7001 for output compatibility with throw_perror_with_name. */
7004 unpush_and_perror (const char *string
)
7006 int saved_errno
= errno
;
7008 remote_unpush_target ();
7009 throw_error (TARGET_CLOSE_ERROR
, "%s: %s.", string
,
7010 safe_strerror (saved_errno
));
7013 /* Read a single character from the remote end. */
7016 readchar (int timeout
)
7019 struct remote_state
*rs
= get_remote_state ();
7021 ch
= serial_readchar (rs
->remote_desc
, timeout
);
7026 switch ((enum serial_rc
) ch
)
7029 remote_unpush_target ();
7030 throw_error (TARGET_CLOSE_ERROR
, _("Remote connection closed"));
7033 unpush_and_perror (_("Remote communication error. "
7034 "Target disconnected."));
7036 case SERIAL_TIMEOUT
:
7042 /* Wrapper for serial_write that closes the target and throws if
7046 remote_serial_write (const char *str
, int len
)
7048 struct remote_state
*rs
= get_remote_state ();
7050 if (serial_write (rs
->remote_desc
, str
, len
))
7052 unpush_and_perror (_("Remote communication error. "
7053 "Target disconnected."));
7057 /* Send the command in *BUF to the remote machine, and read the reply
7058 into *BUF. Report an error if we get an error reply. Resize
7059 *BUF using xrealloc if necessary to hold the result, and update
7063 remote_send (char **buf
,
7067 getpkt (buf
, sizeof_buf
, 0);
7069 if ((*buf
)[0] == 'E')
7070 error (_("Remote failure reply: %s"), *buf
);
7073 /* Return a pointer to an xmalloc'ed string representing an escaped
7074 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7075 etc. The caller is responsible for releasing the returned
7079 escape_buffer (const char *buf
, int n
)
7081 struct cleanup
*old_chain
;
7082 struct ui_file
*stb
;
7085 stb
= mem_fileopen ();
7086 old_chain
= make_cleanup_ui_file_delete (stb
);
7088 fputstrn_unfiltered (buf
, n
, 0, stb
);
7089 str
= ui_file_xstrdup (stb
, NULL
);
7090 do_cleanups (old_chain
);
7094 /* Display a null-terminated packet on stdout, for debugging, using C
7098 print_packet (char *buf
)
7100 puts_filtered ("\"");
7101 fputstr_filtered (buf
, '"', gdb_stdout
);
7102 puts_filtered ("\"");
7108 return putpkt_binary (buf
, strlen (buf
));
7111 /* Send a packet to the remote machine, with error checking. The data
7112 of the packet is in BUF. The string in BUF can be at most
7113 get_remote_packet_size () - 5 to account for the $, # and checksum,
7114 and for a possible /0 if we are debugging (remote_debug) and want
7115 to print the sent packet as a string. */
7118 putpkt_binary (char *buf
, int cnt
)
7120 struct remote_state
*rs
= get_remote_state ();
7122 unsigned char csum
= 0;
7123 char *buf2
= alloca (cnt
+ 6);
7130 /* Catch cases like trying to read memory or listing threads while
7131 we're waiting for a stop reply. The remote server wouldn't be
7132 ready to handle this request, so we'd hang and timeout. We don't
7133 have to worry about this in synchronous mode, because in that
7134 case it's not possible to issue a command while the target is
7135 running. This is not a problem in non-stop mode, because in that
7136 case, the stub is always ready to process serial input. */
7137 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
7138 error (_("Cannot execute this command while the target is running."));
7140 /* We're sending out a new packet. Make sure we don't look at a
7141 stale cached response. */
7142 rs
->cached_wait_status
= 0;
7144 /* Copy the packet into buffer BUF2, encapsulating it
7145 and giving it a checksum. */
7150 for (i
= 0; i
< cnt
; i
++)
7156 *p
++ = tohex ((csum
>> 4) & 0xf);
7157 *p
++ = tohex (csum
& 0xf);
7159 /* Send it over and over until we get a positive ack. */
7163 int started_error_output
= 0;
7167 struct cleanup
*old_chain
;
7171 str
= escape_buffer (buf2
, p
- buf2
);
7172 old_chain
= make_cleanup (xfree
, str
);
7173 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", str
);
7174 gdb_flush (gdb_stdlog
);
7175 do_cleanups (old_chain
);
7177 remote_serial_write (buf2
, p
- buf2
);
7179 /* If this is a no acks version of the remote protocol, send the
7180 packet and move on. */
7184 /* Read until either a timeout occurs (-2) or '+' is read.
7185 Handle any notification that arrives in the mean time. */
7188 ch
= readchar (remote_timeout
);
7196 case SERIAL_TIMEOUT
:
7199 if (started_error_output
)
7201 putchar_unfiltered ('\n');
7202 started_error_output
= 0;
7211 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
7215 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
7217 case SERIAL_TIMEOUT
:
7221 break; /* Retransmit buffer. */
7225 fprintf_unfiltered (gdb_stdlog
,
7226 "Packet instead of Ack, ignoring it\n");
7227 /* It's probably an old response sent because an ACK
7228 was lost. Gobble up the packet and ack it so it
7229 doesn't get retransmitted when we resend this
7232 remote_serial_write ("+", 1);
7233 continue; /* Now, go look for +. */
7240 /* If we got a notification, handle it, and go back to looking
7242 /* We've found the start of a notification. Now
7243 collect the data. */
7244 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
7249 struct cleanup
*old_chain
;
7252 str
= escape_buffer (rs
->buf
, val
);
7253 old_chain
= make_cleanup (xfree
, str
);
7254 fprintf_unfiltered (gdb_stdlog
,
7255 " Notification received: %s\n",
7257 do_cleanups (old_chain
);
7259 handle_notification (rs
->notif_state
, rs
->buf
);
7260 /* We're in sync now, rewait for the ack. */
7267 if (!started_error_output
)
7269 started_error_output
= 1;
7270 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
7272 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
7273 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
7282 if (!started_error_output
)
7284 started_error_output
= 1;
7285 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
7287 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
7291 break; /* Here to retransmit. */
7295 /* This is wrong. If doing a long backtrace, the user should be
7296 able to get out next time we call QUIT, without anything as
7297 violent as interrupt_query. If we want to provide a way out of
7298 here without getting to the next QUIT, it should be based on
7299 hitting ^C twice as in remote_wait. */
7310 /* Come here after finding the start of a frame when we expected an
7311 ack. Do our best to discard the rest of this packet. */
7320 c
= readchar (remote_timeout
);
7323 case SERIAL_TIMEOUT
:
7324 /* Nothing we can do. */
7327 /* Discard the two bytes of checksum and stop. */
7328 c
= readchar (remote_timeout
);
7330 c
= readchar (remote_timeout
);
7333 case '*': /* Run length encoding. */
7334 /* Discard the repeat count. */
7335 c
= readchar (remote_timeout
);
7340 /* A regular character. */
7346 /* Come here after finding the start of the frame. Collect the rest
7347 into *BUF, verifying the checksum, length, and handling run-length
7348 compression. NUL terminate the buffer. If there is not enough room,
7349 expand *BUF using xrealloc.
7351 Returns -1 on error, number of characters in buffer (ignoring the
7352 trailing NULL) on success. (could be extended to return one of the
7353 SERIAL status indications). */
7356 read_frame (char **buf_p
,
7363 struct remote_state
*rs
= get_remote_state ();
7370 c
= readchar (remote_timeout
);
7373 case SERIAL_TIMEOUT
:
7375 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
7379 fputs_filtered ("Saw new packet start in middle of old one\n",
7381 return -1; /* Start a new packet, count retries. */
7384 unsigned char pktcsum
;
7390 check_0
= readchar (remote_timeout
);
7392 check_1
= readchar (remote_timeout
);
7394 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
7397 fputs_filtered ("Timeout in checksum, retrying\n",
7401 else if (check_0
< 0 || check_1
< 0)
7404 fputs_filtered ("Communication error in checksum\n",
7409 /* Don't recompute the checksum; with no ack packets we
7410 don't have any way to indicate a packet retransmission
7415 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
7416 if (csum
== pktcsum
)
7421 struct cleanup
*old_chain
;
7424 str
= escape_buffer (buf
, bc
);
7425 old_chain
= make_cleanup (xfree
, str
);
7426 fprintf_unfiltered (gdb_stdlog
,
7427 "Bad checksum, sentsum=0x%x, "
7428 "csum=0x%x, buf=%s\n",
7429 pktcsum
, csum
, str
);
7430 do_cleanups (old_chain
);
7432 /* Number of characters in buffer ignoring trailing
7436 case '*': /* Run length encoding. */
7441 c
= readchar (remote_timeout
);
7443 repeat
= c
- ' ' + 3; /* Compute repeat count. */
7445 /* The character before ``*'' is repeated. */
7447 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
7449 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
7451 /* Make some more room in the buffer. */
7452 *sizeof_buf
+= repeat
;
7453 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7457 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
7463 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
7467 if (bc
>= *sizeof_buf
- 1)
7469 /* Make some more room in the buffer. */
7471 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7482 /* Read a packet from the remote machine, with error checking, and
7483 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7484 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7485 rather than timing out; this is used (in synchronous mode) to wait
7486 for a target that is is executing user code to stop. */
7487 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7488 don't have to change all the calls to getpkt to deal with the
7489 return value, because at the moment I don't know what the right
7490 thing to do it for those. */
7498 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
7502 /* Read a packet from the remote machine, with error checking, and
7503 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7504 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7505 rather than timing out; this is used (in synchronous mode) to wait
7506 for a target that is is executing user code to stop. If FOREVER ==
7507 0, this function is allowed to time out gracefully and return an
7508 indication of this to the caller. Otherwise return the number of
7509 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7510 enough reason to return to the caller. *IS_NOTIF is an output
7511 boolean that indicates whether *BUF holds a notification or not
7512 (a regular packet). */
7515 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
7516 int expecting_notif
, int *is_notif
)
7518 struct remote_state
*rs
= get_remote_state ();
7524 /* We're reading a new response. Make sure we don't look at a
7525 previously cached response. */
7526 rs
->cached_wait_status
= 0;
7528 strcpy (*buf
, "timeout");
7531 timeout
= watchdog
> 0 ? watchdog
: -1;
7532 else if (expecting_notif
)
7533 timeout
= 0; /* There should already be a char in the buffer. If
7536 timeout
= remote_timeout
;
7540 /* Process any number of notifications, and then return when
7544 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
7546 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
7548 /* This can loop forever if the remote side sends us
7549 characters continuously, but if it pauses, we'll get
7550 SERIAL_TIMEOUT from readchar because of timeout. Then
7551 we'll count that as a retry.
7553 Note that even when forever is set, we will only wait
7554 forever prior to the start of a packet. After that, we
7555 expect characters to arrive at a brisk pace. They should
7556 show up within remote_timeout intervals. */
7558 c
= readchar (timeout
);
7559 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
7561 if (c
== SERIAL_TIMEOUT
)
7563 if (expecting_notif
)
7564 return -1; /* Don't complain, it's normal to not get
7565 anything in this case. */
7567 if (forever
) /* Watchdog went off? Kill the target. */
7570 remote_unpush_target ();
7571 throw_error (TARGET_CLOSE_ERROR
,
7572 _("Watchdog timeout has expired. "
7573 "Target detached."));
7576 fputs_filtered ("Timed out.\n", gdb_stdlog
);
7580 /* We've found the start of a packet or notification.
7581 Now collect the data. */
7582 val
= read_frame (buf
, sizeof_buf
);
7587 remote_serial_write ("-", 1);
7590 if (tries
> MAX_TRIES
)
7592 /* We have tried hard enough, and just can't receive the
7593 packet/notification. Give up. */
7594 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7596 /* Skip the ack char if we're in no-ack mode. */
7597 if (!rs
->noack_mode
)
7598 remote_serial_write ("+", 1);
7602 /* If we got an ordinary packet, return that to our caller. */
7607 struct cleanup
*old_chain
;
7610 str
= escape_buffer (*buf
, val
);
7611 old_chain
= make_cleanup (xfree
, str
);
7612 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", str
);
7613 do_cleanups (old_chain
);
7616 /* Skip the ack char if we're in no-ack mode. */
7617 if (!rs
->noack_mode
)
7618 remote_serial_write ("+", 1);
7619 if (is_notif
!= NULL
)
7624 /* If we got a notification, handle it, and go back to looking
7628 gdb_assert (c
== '%');
7632 struct cleanup
*old_chain
;
7635 str
= escape_buffer (*buf
, val
);
7636 old_chain
= make_cleanup (xfree
, str
);
7637 fprintf_unfiltered (gdb_stdlog
,
7638 " Notification received: %s\n",
7640 do_cleanups (old_chain
);
7642 if (is_notif
!= NULL
)
7645 handle_notification (rs
->notif_state
, *buf
);
7647 /* Notifications require no acknowledgement. */
7649 if (expecting_notif
)
7656 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
7658 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0, NULL
);
7662 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
,
7665 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1,
7671 remote_kill (struct target_ops
*ops
)
7673 volatile struct gdb_exception ex
;
7675 /* Catch errors so the user can quit from gdb even when we
7676 aren't on speaking terms with the remote system. */
7677 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
7683 if (ex
.error
== TARGET_CLOSE_ERROR
)
7685 /* If we got an (EOF) error that caused the target
7686 to go away, then we're done, that's what we wanted.
7687 "k" is susceptible to cause a premature EOF, given
7688 that the remote server isn't actually required to
7689 reply to "k", and it can happen that it doesn't
7690 even get to reply ACK to the "k". */
7694 /* Otherwise, something went wrong. We didn't actually kill
7695 the target. Just propagate the exception, and let the
7696 user or higher layers decide what to do. */
7697 throw_exception (ex
);
7700 /* We've killed the remote end, we get to mourn it. Since this is
7701 target remote, single-process, mourning the inferior also
7702 unpushes remote_ops. */
7703 target_mourn_inferior ();
7707 remote_vkill (int pid
, struct remote_state
*rs
)
7709 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7712 /* Tell the remote target to detach. */
7713 xsnprintf (rs
->buf
, get_remote_packet_size (), "vKill;%x", pid
);
7715 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7717 if (packet_ok (rs
->buf
,
7718 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
7720 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7727 extended_remote_kill (struct target_ops
*ops
)
7730 int pid
= ptid_get_pid (inferior_ptid
);
7731 struct remote_state
*rs
= get_remote_state ();
7733 res
= remote_vkill (pid
, rs
);
7734 if (res
== -1 && !(rs
->extended
&& remote_multi_process_p (rs
)))
7736 /* Don't try 'k' on a multi-process aware stub -- it has no way
7737 to specify the pid. */
7741 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7742 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
7745 /* Don't wait for it to die. I'm not really sure it matters whether
7746 we do or not. For the existing stubs, kill is a noop. */
7752 error (_("Can't kill process"));
7754 target_mourn_inferior ();
7758 remote_mourn (struct target_ops
*ops
)
7760 remote_mourn_1 (ops
);
7763 /* Worker function for remote_mourn. */
7765 remote_mourn_1 (struct target_ops
*target
)
7767 unpush_target (target
);
7769 /* remote_close takes care of doing most of the clean up. */
7770 generic_mourn_inferior ();
7774 extended_remote_mourn_1 (struct target_ops
*target
)
7776 struct remote_state
*rs
= get_remote_state ();
7778 /* In case we got here due to an error, but we're going to stay
7780 rs
->waiting_for_stop_reply
= 0;
7782 /* If the current general thread belonged to the process we just
7783 detached from or has exited, the remote side current general
7784 thread becomes undefined. Considering a case like this:
7786 - We just got here due to a detach.
7787 - The process that we're detaching from happens to immediately
7788 report a global breakpoint being hit in non-stop mode, in the
7789 same thread we had selected before.
7790 - GDB attaches to this process again.
7791 - This event happens to be the next event we handle.
7793 GDB would consider that the current general thread didn't need to
7794 be set on the stub side (with Hg), since for all it knew,
7795 GENERAL_THREAD hadn't changed.
7797 Notice that although in all-stop mode, the remote server always
7798 sets the current thread to the thread reporting the stop event,
7799 that doesn't happen in non-stop mode; in non-stop, the stub *must
7800 not* change the current thread when reporting a breakpoint hit,
7801 due to the decoupling of event reporting and event handling.
7803 To keep things simple, we always invalidate our notion of the
7805 record_currthread (rs
, minus_one_ptid
);
7807 /* Unlike "target remote", we do not want to unpush the target; then
7808 the next time the user says "run", we won't be connected. */
7810 /* Call common code to mark the inferior as not running. */
7811 generic_mourn_inferior ();
7813 if (!have_inferiors ())
7815 if (!remote_multi_process_p (rs
))
7817 /* Check whether the target is running now - some remote stubs
7818 automatically restart after kill. */
7820 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7822 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
7824 /* Assume that the target has been restarted. Set
7825 inferior_ptid so that bits of core GDB realizes
7826 there's something here, e.g., so that the user can
7827 say "kill" again. */
7828 inferior_ptid
= magic_null_ptid
;
7835 extended_remote_mourn (struct target_ops
*ops
)
7837 extended_remote_mourn_1 (ops
);
7841 extended_remote_supports_disable_randomization (struct target_ops
*self
)
7843 return (remote_protocol_packets
[PACKET_QDisableRandomization
].support
7848 extended_remote_disable_randomization (int val
)
7850 struct remote_state
*rs
= get_remote_state ();
7853 xsnprintf (rs
->buf
, get_remote_packet_size (), "QDisableRandomization:%x",
7856 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
7858 error (_("Target does not support QDisableRandomization."));
7859 if (strcmp (reply
, "OK") != 0)
7860 error (_("Bogus QDisableRandomization reply from target: %s"), reply
);
7864 extended_remote_run (char *args
)
7866 struct remote_state
*rs
= get_remote_state ();
7869 /* If the user has disabled vRun support, or we have detected that
7870 support is not available, do not try it. */
7871 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7874 strcpy (rs
->buf
, "vRun;");
7875 len
= strlen (rs
->buf
);
7877 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
7878 error (_("Remote file name too long for run packet"));
7879 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
,
7880 strlen (remote_exec_file
));
7882 gdb_assert (args
!= NULL
);
7885 struct cleanup
*back_to
;
7889 argv
= gdb_buildargv (args
);
7890 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
7891 for (i
= 0; argv
[i
] != NULL
; i
++)
7893 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
7894 error (_("Argument list too long for run packet"));
7895 rs
->buf
[len
++] = ';';
7896 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
,
7899 do_cleanups (back_to
);
7902 rs
->buf
[len
++] = '\0';
7905 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7907 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
7909 /* We have a wait response. All is well. */
7912 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7913 /* It wasn't disabled before, but it is now. */
7917 if (remote_exec_file
[0] == '\0')
7918 error (_("Running the default executable on the remote target failed; "
7919 "try \"set remote exec-file\"?"));
7921 error (_("Running \"%s\" on the remote target failed"),
7926 /* In the extended protocol we want to be able to do things like
7927 "run" and have them basically work as expected. So we need
7928 a special create_inferior function. We support changing the
7929 executable file and the command line arguments, but not the
7933 extended_remote_create_inferior (struct target_ops
*ops
,
7934 char *exec_file
, char *args
,
7935 char **env
, int from_tty
)
7939 struct remote_state
*rs
= get_remote_state ();
7941 /* If running asynchronously, register the target file descriptor
7942 with the event loop. */
7943 if (target_can_async_p ())
7944 target_async (inferior_event_handler
, 0);
7946 /* Disable address space randomization if requested (and supported). */
7947 if (extended_remote_supports_disable_randomization (ops
))
7948 extended_remote_disable_randomization (disable_randomization
);
7950 /* Now restart the remote server. */
7951 run_worked
= extended_remote_run (args
) != -1;
7954 /* vRun was not supported. Fail if we need it to do what the
7956 if (remote_exec_file
[0])
7957 error (_("Remote target does not support \"set remote exec-file\""));
7959 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7961 /* Fall back to "R". */
7962 extended_remote_restart ();
7965 if (!have_inferiors ())
7967 /* Clean up from the last time we ran, before we mark the target
7968 running again. This will mark breakpoints uninserted, and
7969 get_offsets may insert breakpoints. */
7970 init_thread_list ();
7971 init_wait_for_inferior ();
7974 /* vRun's success return is a stop reply. */
7975 stop_reply
= run_worked
? rs
->buf
: NULL
;
7976 add_current_inferior_and_thread (stop_reply
);
7978 /* Get updated offsets, if the stub uses qOffsets. */
7983 /* Given a location's target info BP_TGT and the packet buffer BUF, output
7984 the list of conditions (in agent expression bytecode format), if any, the
7985 target needs to evaluate. The output is placed into the packet buffer
7986 started from BUF and ended at BUF_END. */
7989 remote_add_target_side_condition (struct gdbarch
*gdbarch
,
7990 struct bp_target_info
*bp_tgt
, char *buf
,
7993 struct agent_expr
*aexpr
= NULL
;
7996 char *buf_start
= buf
;
7998 if (VEC_empty (agent_expr_p
, bp_tgt
->conditions
))
8001 buf
+= strlen (buf
);
8002 xsnprintf (buf
, buf_end
- buf
, "%s", ";");
8005 /* Send conditions to the target and free the vector. */
8007 VEC_iterate (agent_expr_p
, bp_tgt
->conditions
, ix
, aexpr
);
8010 xsnprintf (buf
, buf_end
- buf
, "X%x,", aexpr
->len
);
8011 buf
+= strlen (buf
);
8012 for (i
= 0; i
< aexpr
->len
; ++i
)
8013 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
8020 remote_add_target_side_commands (struct gdbarch
*gdbarch
,
8021 struct bp_target_info
*bp_tgt
, char *buf
)
8023 struct agent_expr
*aexpr
= NULL
;
8026 if (VEC_empty (agent_expr_p
, bp_tgt
->tcommands
))
8029 buf
+= strlen (buf
);
8031 sprintf (buf
, ";cmds:%x,", bp_tgt
->persist
);
8032 buf
+= strlen (buf
);
8034 /* Concatenate all the agent expressions that are commands into the
8037 VEC_iterate (agent_expr_p
, bp_tgt
->tcommands
, ix
, aexpr
);
8040 sprintf (buf
, "X%x,", aexpr
->len
);
8041 buf
+= strlen (buf
);
8042 for (i
= 0; i
< aexpr
->len
; ++i
)
8043 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
8048 /* Insert a breakpoint. On targets that have software breakpoint
8049 support, we ask the remote target to do the work; on targets
8050 which don't, we insert a traditional memory breakpoint. */
8053 remote_insert_breakpoint (struct target_ops
*ops
,
8054 struct gdbarch
*gdbarch
,
8055 struct bp_target_info
*bp_tgt
)
8057 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8058 If it succeeds, then set the support to PACKET_ENABLE. If it
8059 fails, and the user has explicitly requested the Z support then
8060 report an error, otherwise, mark it disabled and go on. */
8062 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
8064 CORE_ADDR addr
= bp_tgt
->placed_address
;
8065 struct remote_state
*rs
;
8068 struct condition_list
*cond
= NULL
;
8070 /* Make sure the remote is pointing at the right process, if
8072 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8073 set_general_process ();
8075 gdbarch_remote_breakpoint_from_pc (gdbarch
, &addr
, &bpsize
);
8077 rs
= get_remote_state ();
8079 endbuf
= rs
->buf
+ get_remote_packet_size ();
8084 addr
= (ULONGEST
) remote_address_masked (addr
);
8085 p
+= hexnumstr (p
, addr
);
8086 xsnprintf (p
, endbuf
- p
, ",%d", bpsize
);
8088 if (remote_supports_cond_breakpoints (ops
))
8089 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
8091 if (remote_can_run_breakpoint_commands (ops
))
8092 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
8095 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8097 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
8102 bp_tgt
->placed_address
= addr
;
8103 bp_tgt
->placed_size
= bpsize
;
8105 case PACKET_UNKNOWN
:
8110 /* If this breakpoint has target-side commands but this stub doesn't
8111 support Z0 packets, throw error. */
8112 if (!VEC_empty (agent_expr_p
, bp_tgt
->tcommands
))
8113 throw_error (NOT_SUPPORTED_ERROR
, _("\
8114 Target doesn't support breakpoints that have target side commands."));
8116 return memory_insert_breakpoint (ops
, gdbarch
, bp_tgt
);
8120 remote_remove_breakpoint (struct target_ops
*ops
,
8121 struct gdbarch
*gdbarch
,
8122 struct bp_target_info
*bp_tgt
)
8124 CORE_ADDR addr
= bp_tgt
->placed_address
;
8125 struct remote_state
*rs
= get_remote_state ();
8127 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
8130 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
8132 /* Make sure the remote is pointing at the right process, if
8134 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8135 set_general_process ();
8141 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
8142 p
+= hexnumstr (p
, addr
);
8143 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->placed_size
);
8146 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8148 return (rs
->buf
[0] == 'E');
8151 return memory_remove_breakpoint (ops
, gdbarch
, bp_tgt
);
8155 watchpoint_to_Z_packet (int type
)
8160 return Z_PACKET_WRITE_WP
;
8163 return Z_PACKET_READ_WP
;
8166 return Z_PACKET_ACCESS_WP
;
8169 internal_error (__FILE__
, __LINE__
,
8170 _("hw_bp_to_z: bad watchpoint type %d"), type
);
8175 remote_insert_watchpoint (struct target_ops
*self
,
8176 CORE_ADDR addr
, int len
, int type
,
8177 struct expression
*cond
)
8179 struct remote_state
*rs
= get_remote_state ();
8180 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
8182 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
8184 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
8187 /* Make sure the remote is pointing at the right process, if
8189 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8190 set_general_process ();
8192 xsnprintf (rs
->buf
, endbuf
- rs
->buf
, "Z%x,", packet
);
8193 p
= strchr (rs
->buf
, '\0');
8194 addr
= remote_address_masked (addr
);
8195 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8196 xsnprintf (p
, endbuf
- p
, ",%x", len
);
8199 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8201 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
8205 case PACKET_UNKNOWN
:
8210 internal_error (__FILE__
, __LINE__
,
8211 _("remote_insert_watchpoint: reached end of function"));
8215 remote_watchpoint_addr_within_range (struct target_ops
*target
, CORE_ADDR addr
,
8216 CORE_ADDR start
, int length
)
8218 CORE_ADDR diff
= remote_address_masked (addr
- start
);
8220 return diff
< length
;
8225 remote_remove_watchpoint (struct target_ops
*self
,
8226 CORE_ADDR addr
, int len
, int type
,
8227 struct expression
*cond
)
8229 struct remote_state
*rs
= get_remote_state ();
8230 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
8232 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
8234 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
8237 /* Make sure the remote is pointing at the right process, if
8239 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8240 set_general_process ();
8242 xsnprintf (rs
->buf
, endbuf
- rs
->buf
, "z%x,", packet
);
8243 p
= strchr (rs
->buf
, '\0');
8244 addr
= remote_address_masked (addr
);
8245 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8246 xsnprintf (p
, endbuf
- p
, ",%x", len
);
8248 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8250 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
8253 case PACKET_UNKNOWN
:
8258 internal_error (__FILE__
, __LINE__
,
8259 _("remote_remove_watchpoint: reached end of function"));
8263 int remote_hw_watchpoint_limit
= -1;
8264 int remote_hw_watchpoint_length_limit
= -1;
8265 int remote_hw_breakpoint_limit
= -1;
8268 remote_region_ok_for_hw_watchpoint (struct target_ops
*self
,
8269 CORE_ADDR addr
, int len
)
8271 if (remote_hw_watchpoint_length_limit
== 0)
8273 else if (remote_hw_watchpoint_length_limit
< 0)
8275 else if (len
<= remote_hw_watchpoint_length_limit
)
8282 remote_check_watch_resources (struct target_ops
*self
,
8283 int type
, int cnt
, int ot
)
8285 if (type
== bp_hardware_breakpoint
)
8287 if (remote_hw_breakpoint_limit
== 0)
8289 else if (remote_hw_breakpoint_limit
< 0)
8291 else if (cnt
<= remote_hw_breakpoint_limit
)
8296 if (remote_hw_watchpoint_limit
== 0)
8298 else if (remote_hw_watchpoint_limit
< 0)
8302 else if (cnt
<= remote_hw_watchpoint_limit
)
8309 remote_stopped_by_watchpoint (struct target_ops
*ops
)
8311 struct remote_state
*rs
= get_remote_state ();
8313 return rs
->remote_stopped_by_watchpoint_p
;
8317 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
8319 struct remote_state
*rs
= get_remote_state ();
8322 if (remote_stopped_by_watchpoint (target
))
8324 *addr_p
= rs
->remote_watch_data_address
;
8333 remote_insert_hw_breakpoint (struct target_ops
*self
, struct gdbarch
*gdbarch
,
8334 struct bp_target_info
*bp_tgt
)
8337 struct remote_state
*rs
;
8341 /* The length field should be set to the size of a breakpoint
8342 instruction, even though we aren't inserting one ourselves. */
8344 gdbarch_remote_breakpoint_from_pc
8345 (gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
8347 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
8350 /* Make sure the remote is pointing at the right process, if
8352 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8353 set_general_process ();
8355 rs
= get_remote_state ();
8357 endbuf
= rs
->buf
+ get_remote_packet_size ();
8363 addr
= remote_address_masked (bp_tgt
->placed_address
);
8364 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8365 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->placed_size
);
8367 if (remote_supports_cond_breakpoints (self
))
8368 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
8370 if (remote_can_run_breakpoint_commands (self
))
8371 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
8374 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8376 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
8379 if (rs
->buf
[1] == '.')
8381 message
= strchr (rs
->buf
+ 2, '.');
8383 error (_("Remote failure reply: %s"), message
+ 1);
8386 case PACKET_UNKNOWN
:
8391 internal_error (__FILE__
, __LINE__
,
8392 _("remote_insert_hw_breakpoint: reached end of function"));
8397 remote_remove_hw_breakpoint (struct target_ops
*self
, struct gdbarch
*gdbarch
,
8398 struct bp_target_info
*bp_tgt
)
8401 struct remote_state
*rs
= get_remote_state ();
8403 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
8405 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
8408 /* Make sure the remote is pointing at the right process, if
8410 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8411 set_general_process ();
8417 addr
= remote_address_masked (bp_tgt
->placed_address
);
8418 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8419 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->placed_size
);
8422 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8424 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
8427 case PACKET_UNKNOWN
:
8432 internal_error (__FILE__
, __LINE__
,
8433 _("remote_remove_hw_breakpoint: reached end of function"));
8436 /* Verify memory using the "qCRC:" request. */
8439 remote_verify_memory (struct target_ops
*ops
,
8440 const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
8442 struct remote_state
*rs
= get_remote_state ();
8443 unsigned long host_crc
, target_crc
;
8446 /* Make sure the remote is pointing at the right process. */
8447 set_general_process ();
8449 /* FIXME: assumes lma can fit into long. */
8450 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
8451 (long) lma
, (long) size
);
8454 /* Be clever; compute the host_crc before waiting for target
8456 host_crc
= xcrc32 (data
, size
, 0xffffffff);
8458 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8459 if (rs
->buf
[0] == 'E')
8462 if (rs
->buf
[0] != 'C')
8463 error (_("remote target does not support this operation"));
8465 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
8466 target_crc
= target_crc
* 16 + fromhex (*tmp
);
8468 return (host_crc
== target_crc
);
8471 /* compare-sections command
8473 With no arguments, compares each loadable section in the exec bfd
8474 with the same memory range on the target, and reports mismatches.
8475 Useful for verifying the image on the target against the exec file. */
8478 compare_sections_command (char *args
, int from_tty
)
8481 struct cleanup
*old_chain
;
8483 const char *sectname
;
8491 error (_("command cannot be used without an exec file"));
8493 /* Make sure the remote is pointing at the right process. */
8494 set_general_process ();
8496 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
8498 if (!(s
->flags
& SEC_LOAD
))
8499 continue; /* Skip non-loadable section. */
8501 size
= bfd_get_section_size (s
);
8503 continue; /* Skip zero-length section. */
8505 sectname
= bfd_get_section_name (exec_bfd
, s
);
8506 if (args
&& strcmp (args
, sectname
) != 0)
8507 continue; /* Not the section selected by user. */
8509 matched
= 1; /* Do this section. */
8512 sectdata
= xmalloc (size
);
8513 old_chain
= make_cleanup (xfree
, sectdata
);
8514 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
8516 res
= target_verify_memory (sectdata
, lma
, size
);
8519 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
8520 paddress (target_gdbarch (), lma
),
8521 paddress (target_gdbarch (), lma
+ size
));
8523 printf_filtered ("Section %s, range %s -- %s: ", sectname
,
8524 paddress (target_gdbarch (), lma
),
8525 paddress (target_gdbarch (), lma
+ size
));
8527 printf_filtered ("matched.\n");
8530 printf_filtered ("MIS-MATCHED!\n");
8534 do_cleanups (old_chain
);
8537 warning (_("One or more sections of the remote executable does not match\n\
8538 the loaded file\n"));
8539 if (args
&& !matched
)
8540 printf_filtered (_("No loaded section named '%s'.\n"), args
);
8543 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
8544 into remote target. The number of bytes written to the remote
8545 target is returned, or -1 for error. */
8547 static enum target_xfer_status
8548 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
8549 const char *annex
, const gdb_byte
*writebuf
,
8550 ULONGEST offset
, LONGEST len
, ULONGEST
*xfered_len
,
8551 struct packet_config
*packet
)
8555 struct remote_state
*rs
= get_remote_state ();
8556 int max_size
= get_memory_write_packet_size ();
8558 if (packet
->support
== PACKET_DISABLE
)
8559 return TARGET_XFER_E_IO
;
8561 /* Insert header. */
8562 i
= snprintf (rs
->buf
, max_size
,
8563 "qXfer:%s:write:%s:%s:",
8564 object_name
, annex
? annex
: "",
8565 phex_nz (offset
, sizeof offset
));
8566 max_size
-= (i
+ 1);
8568 /* Escape as much data as fits into rs->buf. */
8569 buf_len
= remote_escape_output
8570 (writebuf
, len
, (gdb_byte
*) rs
->buf
+ i
, &max_size
, max_size
);
8572 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
8573 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8574 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8575 return TARGET_XFER_E_IO
;
8577 unpack_varlen_hex (rs
->buf
, &n
);
8580 return TARGET_XFER_OK
;
8583 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8584 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8585 number of bytes read is returned, or 0 for EOF, or -1 for error.
8586 The number of bytes read may be less than LEN without indicating an
8587 EOF. PACKET is checked and updated to indicate whether the remote
8588 target supports this object. */
8590 static enum target_xfer_status
8591 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
8593 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
8594 ULONGEST
*xfered_len
,
8595 struct packet_config
*packet
)
8597 struct remote_state
*rs
= get_remote_state ();
8598 LONGEST i
, n
, packet_len
;
8600 if (packet
->support
== PACKET_DISABLE
)
8601 return TARGET_XFER_E_IO
;
8603 /* Check whether we've cached an end-of-object packet that matches
8605 if (rs
->finished_object
)
8607 if (strcmp (object_name
, rs
->finished_object
) == 0
8608 && strcmp (annex
? annex
: "", rs
->finished_annex
) == 0
8609 && offset
== rs
->finished_offset
)
8610 return TARGET_XFER_EOF
;
8613 /* Otherwise, we're now reading something different. Discard
8615 xfree (rs
->finished_object
);
8616 xfree (rs
->finished_annex
);
8617 rs
->finished_object
= NULL
;
8618 rs
->finished_annex
= NULL
;
8621 /* Request only enough to fit in a single packet. The actual data
8622 may not, since we don't know how much of it will need to be escaped;
8623 the target is free to respond with slightly less data. We subtract
8624 five to account for the response type and the protocol frame. */
8625 n
= min (get_remote_packet_size () - 5, len
);
8626 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8627 object_name
, annex
? annex
: "",
8628 phex_nz (offset
, sizeof offset
),
8629 phex_nz (n
, sizeof n
));
8630 i
= putpkt (rs
->buf
);
8632 return TARGET_XFER_E_IO
;
8635 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
8636 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8637 return TARGET_XFER_E_IO
;
8639 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
8640 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
8642 /* 'm' means there is (or at least might be) more data after this
8643 batch. That does not make sense unless there's at least one byte
8644 of data in this reply. */
8645 if (rs
->buf
[0] == 'm' && packet_len
== 1)
8646 error (_("Remote qXfer reply contained no data."));
8648 /* Got some data. */
8649 i
= remote_unescape_input ((gdb_byte
*) rs
->buf
+ 1,
8650 packet_len
- 1, readbuf
, n
);
8652 /* 'l' is an EOF marker, possibly including a final block of data,
8653 or possibly empty. If we have the final block of a non-empty
8654 object, record this fact to bypass a subsequent partial read. */
8655 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
8657 rs
->finished_object
= xstrdup (object_name
);
8658 rs
->finished_annex
= xstrdup (annex
? annex
: "");
8659 rs
->finished_offset
= offset
+ i
;
8663 return TARGET_XFER_EOF
;
8667 return TARGET_XFER_OK
;
8671 static enum target_xfer_status
8672 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
8673 const char *annex
, gdb_byte
*readbuf
,
8674 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
8675 ULONGEST
*xfered_len
)
8677 struct remote_state
*rs
;
8682 set_remote_traceframe ();
8683 set_general_thread (inferior_ptid
);
8685 rs
= get_remote_state ();
8687 /* Handle memory using the standard memory routines. */
8688 if (object
== TARGET_OBJECT_MEMORY
)
8690 /* If the remote target is connected but not running, we should
8691 pass this request down to a lower stratum (e.g. the executable
8693 if (!target_has_execution
)
8694 return TARGET_XFER_EOF
;
8696 if (writebuf
!= NULL
)
8697 return remote_write_bytes (offset
, writebuf
, len
, xfered_len
);
8699 return remote_read_bytes (offset
, readbuf
, len
, xfered_len
);
8702 /* Handle SPU memory using qxfer packets. */
8703 if (object
== TARGET_OBJECT_SPU
)
8706 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
8707 xfered_len
, &remote_protocol_packets
8708 [PACKET_qXfer_spu_read
]);
8710 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
8711 xfered_len
, &remote_protocol_packets
8712 [PACKET_qXfer_spu_write
]);
8715 /* Handle extra signal info using qxfer packets. */
8716 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
8719 return remote_read_qxfer (ops
, "siginfo", annex
, readbuf
, offset
, len
,
8720 xfered_len
, &remote_protocol_packets
8721 [PACKET_qXfer_siginfo_read
]);
8723 return remote_write_qxfer (ops
, "siginfo", annex
,
8724 writebuf
, offset
, len
, xfered_len
,
8725 &remote_protocol_packets
8726 [PACKET_qXfer_siginfo_write
]);
8729 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
8732 return remote_read_qxfer (ops
, "statictrace", annex
,
8733 readbuf
, offset
, len
, xfered_len
,
8734 &remote_protocol_packets
8735 [PACKET_qXfer_statictrace_read
]);
8737 return TARGET_XFER_E_IO
;
8740 /* Only handle flash writes. */
8741 if (writebuf
!= NULL
)
8747 case TARGET_OBJECT_FLASH
:
8748 return remote_flash_write (ops
, offset
, len
, xfered_len
,
8752 return TARGET_XFER_E_IO
;
8756 /* Map pre-existing objects onto letters. DO NOT do this for new
8757 objects!!! Instead specify new query packets. */
8760 case TARGET_OBJECT_AVR
:
8764 case TARGET_OBJECT_AUXV
:
8765 gdb_assert (annex
== NULL
);
8766 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
8768 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
8770 case TARGET_OBJECT_AVAILABLE_FEATURES
:
8771 return remote_read_qxfer
8772 (ops
, "features", annex
, readbuf
, offset
, len
, xfered_len
,
8773 &remote_protocol_packets
[PACKET_qXfer_features
]);
8775 case TARGET_OBJECT_LIBRARIES
:
8776 return remote_read_qxfer
8777 (ops
, "libraries", annex
, readbuf
, offset
, len
, xfered_len
,
8778 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
8780 case TARGET_OBJECT_LIBRARIES_SVR4
:
8781 return remote_read_qxfer
8782 (ops
, "libraries-svr4", annex
, readbuf
, offset
, len
, xfered_len
,
8783 &remote_protocol_packets
[PACKET_qXfer_libraries_svr4
]);
8785 case TARGET_OBJECT_MEMORY_MAP
:
8786 gdb_assert (annex
== NULL
);
8787 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
8789 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
8791 case TARGET_OBJECT_OSDATA
:
8792 /* Should only get here if we're connected. */
8793 gdb_assert (rs
->remote_desc
);
8794 return remote_read_qxfer
8795 (ops
, "osdata", annex
, readbuf
, offset
, len
, xfered_len
,
8796 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
8798 case TARGET_OBJECT_THREADS
:
8799 gdb_assert (annex
== NULL
);
8800 return remote_read_qxfer (ops
, "threads", annex
, readbuf
, offset
, len
,
8802 &remote_protocol_packets
[PACKET_qXfer_threads
]);
8804 case TARGET_OBJECT_TRACEFRAME_INFO
:
8805 gdb_assert (annex
== NULL
);
8806 return remote_read_qxfer
8807 (ops
, "traceframe-info", annex
, readbuf
, offset
, len
, xfered_len
,
8808 &remote_protocol_packets
[PACKET_qXfer_traceframe_info
]);
8810 case TARGET_OBJECT_FDPIC
:
8811 return remote_read_qxfer (ops
, "fdpic", annex
, readbuf
, offset
, len
,
8813 &remote_protocol_packets
[PACKET_qXfer_fdpic
]);
8815 case TARGET_OBJECT_OPENVMS_UIB
:
8816 return remote_read_qxfer (ops
, "uib", annex
, readbuf
, offset
, len
,
8818 &remote_protocol_packets
[PACKET_qXfer_uib
]);
8820 case TARGET_OBJECT_BTRACE
:
8821 return remote_read_qxfer (ops
, "btrace", annex
, readbuf
, offset
, len
,
8823 &remote_protocol_packets
[PACKET_qXfer_btrace
]);
8826 return TARGET_XFER_E_IO
;
8829 /* Note: a zero OFFSET and LEN can be used to query the minimum
8831 if (offset
== 0 && len
== 0)
8832 return (get_remote_packet_size ());
8833 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8834 large enough let the caller deal with it. */
8835 if (len
< get_remote_packet_size ())
8836 return TARGET_XFER_E_IO
;
8837 len
= get_remote_packet_size ();
8839 /* Except for querying the minimum buffer size, target must be open. */
8840 if (!rs
->remote_desc
)
8841 error (_("remote query is only available after target open"));
8843 gdb_assert (annex
!= NULL
);
8844 gdb_assert (readbuf
!= NULL
);
8850 /* We used one buffer char for the remote protocol q command and
8851 another for the query type. As the remote protocol encapsulation
8852 uses 4 chars plus one extra in case we are debugging
8853 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8856 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
8858 /* Bad caller may have sent forbidden characters. */
8859 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
8864 gdb_assert (annex
[i
] == '\0');
8866 i
= putpkt (rs
->buf
);
8868 return TARGET_XFER_E_IO
;
8870 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8871 strcpy ((char *) readbuf
, rs
->buf
);
8873 *xfered_len
= strlen ((char *) readbuf
);
8874 return TARGET_XFER_OK
;
8878 remote_search_memory (struct target_ops
* ops
,
8879 CORE_ADDR start_addr
, ULONGEST search_space_len
,
8880 const gdb_byte
*pattern
, ULONGEST pattern_len
,
8881 CORE_ADDR
*found_addrp
)
8883 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
8884 struct remote_state
*rs
= get_remote_state ();
8885 int max_size
= get_memory_write_packet_size ();
8886 struct packet_config
*packet
=
8887 &remote_protocol_packets
[PACKET_qSearch_memory
];
8888 /* Number of packet bytes used to encode the pattern;
8889 this could be more than PATTERN_LEN due to escape characters. */
8890 int escaped_pattern_len
;
8891 /* Amount of pattern that was encodable in the packet. */
8892 int used_pattern_len
;
8895 ULONGEST found_addr
;
8897 /* Don't go to the target if we don't have to.
8898 This is done before checking packet->support to avoid the possibility that
8899 a success for this edge case means the facility works in general. */
8900 if (pattern_len
> search_space_len
)
8902 if (pattern_len
== 0)
8904 *found_addrp
= start_addr
;
8908 /* If we already know the packet isn't supported, fall back to the simple
8909 way of searching memory. */
8911 if (packet
->support
== PACKET_DISABLE
)
8913 /* Target doesn't provided special support, fall back and use the
8914 standard support (copy memory and do the search here). */
8915 return simple_search_memory (ops
, start_addr
, search_space_len
,
8916 pattern
, pattern_len
, found_addrp
);
8919 /* Make sure the remote is pointing at the right process. */
8920 set_general_process ();
8922 /* Insert header. */
8923 i
= snprintf (rs
->buf
, max_size
,
8924 "qSearch:memory:%s;%s;",
8925 phex_nz (start_addr
, addr_size
),
8926 phex_nz (search_space_len
, sizeof (search_space_len
)));
8927 max_size
-= (i
+ 1);
8929 /* Escape as much data as fits into rs->buf. */
8930 escaped_pattern_len
=
8931 remote_escape_output (pattern
, pattern_len
, (gdb_byte
*) rs
->buf
+ i
,
8932 &used_pattern_len
, max_size
);
8934 /* Bail if the pattern is too large. */
8935 if (used_pattern_len
!= pattern_len
)
8936 error (_("Pattern is too large to transmit to remote target."));
8938 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
8939 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8940 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8942 /* The request may not have worked because the command is not
8943 supported. If so, fall back to the simple way. */
8944 if (packet
->support
== PACKET_DISABLE
)
8946 return simple_search_memory (ops
, start_addr
, search_space_len
,
8947 pattern
, pattern_len
, found_addrp
);
8952 if (rs
->buf
[0] == '0')
8954 else if (rs
->buf
[0] == '1')
8957 if (rs
->buf
[1] != ',')
8958 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8959 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
8960 *found_addrp
= found_addr
;
8963 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8969 remote_rcmd (struct target_ops
*self
, char *command
,
8970 struct ui_file
*outbuf
)
8972 struct remote_state
*rs
= get_remote_state ();
8975 if (!rs
->remote_desc
)
8976 error (_("remote rcmd is only available after target open"));
8978 /* Send a NULL command across as an empty command. */
8979 if (command
== NULL
)
8982 /* The query prefix. */
8983 strcpy (rs
->buf
, "qRcmd,");
8984 p
= strchr (rs
->buf
, '\0');
8986 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/)
8987 > get_remote_packet_size ())
8988 error (_("\"monitor\" command ``%s'' is too long."), command
);
8990 /* Encode the actual command. */
8991 bin2hex ((gdb_byte
*) command
, p
, strlen (command
));
8993 if (putpkt (rs
->buf
) < 0)
8994 error (_("Communication problem with target."));
8996 /* get/display the response */
9001 /* XXX - see also remote_get_noisy_reply(). */
9002 QUIT
; /* Allow user to bail out with ^C. */
9004 if (getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) == -1)
9006 /* Timeout. Continue to (try to) read responses.
9007 This is better than stopping with an error, assuming the stub
9008 is still executing the (long) monitor command.
9009 If needed, the user can interrupt gdb using C-c, obtaining
9010 an effect similar to stop on timeout. */
9015 error (_("Target does not support this command."));
9016 if (buf
[0] == 'O' && buf
[1] != 'K')
9018 remote_console_output (buf
+ 1); /* 'O' message from stub. */
9021 if (strcmp (buf
, "OK") == 0)
9023 if (strlen (buf
) == 3 && buf
[0] == 'E'
9024 && isdigit (buf
[1]) && isdigit (buf
[2]))
9026 error (_("Protocol error with Rcmd"));
9028 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
9030 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
9032 fputc_unfiltered (c
, outbuf
);
9038 static VEC(mem_region_s
) *
9039 remote_memory_map (struct target_ops
*ops
)
9041 VEC(mem_region_s
) *result
= NULL
;
9042 char *text
= target_read_stralloc (¤t_target
,
9043 TARGET_OBJECT_MEMORY_MAP
, NULL
);
9047 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
9049 result
= parse_memory_map (text
);
9050 do_cleanups (back_to
);
9057 packet_command (char *args
, int from_tty
)
9059 struct remote_state
*rs
= get_remote_state ();
9061 if (!rs
->remote_desc
)
9062 error (_("command can only be used with remote target"));
9065 error (_("remote-packet command requires packet text as argument"));
9067 puts_filtered ("sending: ");
9068 print_packet (args
);
9069 puts_filtered ("\n");
9072 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
9073 puts_filtered ("received: ");
9074 print_packet (rs
->buf
);
9075 puts_filtered ("\n");
9079 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
9081 static void display_thread_info (struct gdb_ext_thread_info
*info
);
9083 static void threadset_test_cmd (char *cmd
, int tty
);
9085 static void threadalive_test (char *cmd
, int tty
);
9087 static void threadlist_test_cmd (char *cmd
, int tty
);
9089 int get_and_display_threadinfo (threadref
*ref
);
9091 static void threadinfo_test_cmd (char *cmd
, int tty
);
9093 static int thread_display_step (threadref
*ref
, void *context
);
9095 static void threadlist_update_test_cmd (char *cmd
, int tty
);
9097 static void init_remote_threadtests (void);
9099 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
9102 threadset_test_cmd (char *cmd
, int tty
)
9104 int sample_thread
= SAMPLE_THREAD
;
9106 printf_filtered (_("Remote threadset test\n"));
9107 set_general_thread (sample_thread
);
9112 threadalive_test (char *cmd
, int tty
)
9114 int sample_thread
= SAMPLE_THREAD
;
9115 int pid
= ptid_get_pid (inferior_ptid
);
9116 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
9118 if (remote_thread_alive (ptid
))
9119 printf_filtered ("PASS: Thread alive test\n");
9121 printf_filtered ("FAIL: Thread alive test\n");
9124 void output_threadid (char *title
, threadref
*ref
);
9127 output_threadid (char *title
, threadref
*ref
)
9131 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
9133 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
9137 threadlist_test_cmd (char *cmd
, int tty
)
9140 threadref nextthread
;
9141 int done
, result_count
;
9142 threadref threadlist
[3];
9144 printf_filtered ("Remote Threadlist test\n");
9145 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
9146 &result_count
, &threadlist
[0]))
9147 printf_filtered ("FAIL: threadlist test\n");
9150 threadref
*scan
= threadlist
;
9151 threadref
*limit
= scan
+ result_count
;
9153 while (scan
< limit
)
9154 output_threadid (" thread ", scan
++);
9159 display_thread_info (struct gdb_ext_thread_info
*info
)
9161 output_threadid ("Threadid: ", &info
->threadid
);
9162 printf_filtered ("Name: %s\n ", info
->shortname
);
9163 printf_filtered ("State: %s\n", info
->display
);
9164 printf_filtered ("other: %s\n\n", info
->more_display
);
9168 get_and_display_threadinfo (threadref
*ref
)
9172 struct gdb_ext_thread_info threadinfo
;
9174 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
9175 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
9176 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
9177 display_thread_info (&threadinfo
);
9182 threadinfo_test_cmd (char *cmd
, int tty
)
9184 int athread
= SAMPLE_THREAD
;
9188 int_to_threadref (&thread
, athread
);
9189 printf_filtered ("Remote Threadinfo test\n");
9190 if (!get_and_display_threadinfo (&thread
))
9191 printf_filtered ("FAIL cannot get thread info\n");
9195 thread_display_step (threadref
*ref
, void *context
)
9197 /* output_threadid(" threadstep ",ref); *//* simple test */
9198 return get_and_display_threadinfo (ref
);
9202 threadlist_update_test_cmd (char *cmd
, int tty
)
9204 printf_filtered ("Remote Threadlist update test\n");
9205 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
9209 init_remote_threadtests (void)
9211 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
9212 _("Fetch and print the remote list of "
9213 "thread identifiers, one pkt only"));
9214 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
9215 _("Fetch and display info about one thread"));
9216 add_com ("tset", class_obscure
, threadset_test_cmd
,
9217 _("Test setting to a different thread"));
9218 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
9219 _("Iterate through updating all remote thread info"));
9220 add_com ("talive", class_obscure
, threadalive_test
,
9221 _(" Remote thread alive test "));
9226 /* Convert a thread ID to a string. Returns the string in a static
9230 remote_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
9232 static char buf
[64];
9233 struct remote_state
*rs
= get_remote_state ();
9235 if (ptid_equal (ptid
, null_ptid
))
9236 return normal_pid_to_str (ptid
);
9237 else if (ptid_is_pid (ptid
))
9239 /* Printing an inferior target id. */
9241 /* When multi-process extensions are off, there's no way in the
9242 remote protocol to know the remote process id, if there's any
9243 at all. There's one exception --- when we're connected with
9244 target extended-remote, and we manually attached to a process
9245 with "attach PID". We don't record anywhere a flag that
9246 allows us to distinguish that case from the case of
9247 connecting with extended-remote and the stub already being
9248 attached to a process, and reporting yes to qAttached, hence
9249 no smart special casing here. */
9250 if (!remote_multi_process_p (rs
))
9252 xsnprintf (buf
, sizeof buf
, "Remote target");
9256 return normal_pid_to_str (ptid
);
9260 if (ptid_equal (magic_null_ptid
, ptid
))
9261 xsnprintf (buf
, sizeof buf
, "Thread <main>");
9262 else if (rs
->extended
&& remote_multi_process_p (rs
))
9263 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
9264 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
9266 xsnprintf (buf
, sizeof buf
, "Thread %ld",
9267 ptid_get_tid (ptid
));
9272 /* Get the address of the thread local variable in OBJFILE which is
9273 stored at OFFSET within the thread local storage for thread PTID. */
9276 remote_get_thread_local_address (struct target_ops
*ops
,
9277 ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
9279 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
9281 struct remote_state
*rs
= get_remote_state ();
9283 char *endp
= rs
->buf
+ get_remote_packet_size ();
9284 enum packet_result result
;
9286 strcpy (p
, "qGetTLSAddr:");
9288 p
= write_ptid (p
, endp
, ptid
);
9290 p
+= hexnumstr (p
, offset
);
9292 p
+= hexnumstr (p
, lm
);
9296 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
9297 result
= packet_ok (rs
->buf
,
9298 &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
9299 if (result
== PACKET_OK
)
9303 unpack_varlen_hex (rs
->buf
, &result
);
9306 else if (result
== PACKET_UNKNOWN
)
9307 throw_error (TLS_GENERIC_ERROR
,
9308 _("Remote target doesn't support qGetTLSAddr packet"));
9310 throw_error (TLS_GENERIC_ERROR
,
9311 _("Remote target failed to process qGetTLSAddr request"));
9314 throw_error (TLS_GENERIC_ERROR
,
9315 _("TLS not supported or disabled on this target"));
9320 /* Provide thread local base, i.e. Thread Information Block address.
9321 Returns 1 if ptid is found and thread_local_base is non zero. */
9324 remote_get_tib_address (struct target_ops
*self
, ptid_t ptid
, CORE_ADDR
*addr
)
9326 if (remote_protocol_packets
[PACKET_qGetTIBAddr
].support
!= PACKET_DISABLE
)
9328 struct remote_state
*rs
= get_remote_state ();
9330 char *endp
= rs
->buf
+ get_remote_packet_size ();
9331 enum packet_result result
;
9333 strcpy (p
, "qGetTIBAddr:");
9335 p
= write_ptid (p
, endp
, ptid
);
9339 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
9340 result
= packet_ok (rs
->buf
,
9341 &remote_protocol_packets
[PACKET_qGetTIBAddr
]);
9342 if (result
== PACKET_OK
)
9346 unpack_varlen_hex (rs
->buf
, &result
);
9348 *addr
= (CORE_ADDR
) result
;
9351 else if (result
== PACKET_UNKNOWN
)
9352 error (_("Remote target doesn't support qGetTIBAddr packet"));
9354 error (_("Remote target failed to process qGetTIBAddr request"));
9357 error (_("qGetTIBAddr not supported or disabled on this target"));
9362 /* Support for inferring a target description based on the current
9363 architecture and the size of a 'g' packet. While the 'g' packet
9364 can have any size (since optional registers can be left off the
9365 end), some sizes are easily recognizable given knowledge of the
9366 approximate architecture. */
9368 struct remote_g_packet_guess
9371 const struct target_desc
*tdesc
;
9373 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
9374 DEF_VEC_O(remote_g_packet_guess_s
);
9376 struct remote_g_packet_data
9378 VEC(remote_g_packet_guess_s
) *guesses
;
9381 static struct gdbarch_data
*remote_g_packet_data_handle
;
9384 remote_g_packet_data_init (struct obstack
*obstack
)
9386 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
9390 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
9391 const struct target_desc
*tdesc
)
9393 struct remote_g_packet_data
*data
9394 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
9395 struct remote_g_packet_guess new_guess
, *guess
;
9398 gdb_assert (tdesc
!= NULL
);
9401 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
9403 if (guess
->bytes
== bytes
)
9404 internal_error (__FILE__
, __LINE__
,
9405 _("Duplicate g packet description added for size %d"),
9408 new_guess
.bytes
= bytes
;
9409 new_guess
.tdesc
= tdesc
;
9410 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
9413 /* Return 1 if remote_read_description would do anything on this target
9414 and architecture, 0 otherwise. */
9417 remote_read_description_p (struct target_ops
*target
)
9419 struct remote_g_packet_data
*data
9420 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle
);
9422 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
9428 static const struct target_desc
*
9429 remote_read_description (struct target_ops
*target
)
9431 struct remote_g_packet_data
*data
9432 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle
);
9434 /* Do not try this during initial connection, when we do not know
9435 whether there is a running but stopped thread. */
9436 if (!target_has_execution
|| ptid_equal (inferior_ptid
, null_ptid
))
9439 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
9441 struct remote_g_packet_guess
*guess
;
9443 int bytes
= send_g_packet ();
9446 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
9448 if (guess
->bytes
== bytes
)
9449 return guess
->tdesc
;
9451 /* We discard the g packet. A minor optimization would be to
9452 hold on to it, and fill the register cache once we have selected
9453 an architecture, but it's too tricky to do safely. */
9459 /* Remote file transfer support. This is host-initiated I/O, not
9460 target-initiated; for target-initiated, see remote-fileio.c. */
9462 /* If *LEFT is at least the length of STRING, copy STRING to
9463 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9464 decrease *LEFT. Otherwise raise an error. */
9467 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
9469 int len
= strlen (string
);
9472 error (_("Packet too long for target."));
9474 memcpy (*buffer
, string
, len
);
9478 /* NUL-terminate the buffer as a convenience, if there is
9484 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
9485 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9486 decrease *LEFT. Otherwise raise an error. */
9489 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
9492 if (2 * len
> *left
)
9493 error (_("Packet too long for target."));
9495 bin2hex (bytes
, *buffer
, len
);
9499 /* NUL-terminate the buffer as a convenience, if there is
9505 /* If *LEFT is large enough, convert VALUE to hex and add it to
9506 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9507 decrease *LEFT. Otherwise raise an error. */
9510 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
9512 int len
= hexnumlen (value
);
9515 error (_("Packet too long for target."));
9517 hexnumstr (*buffer
, value
);
9521 /* NUL-terminate the buffer as a convenience, if there is
9527 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
9528 value, *REMOTE_ERRNO to the remote error number or zero if none
9529 was included, and *ATTACHMENT to point to the start of the annex
9530 if any. The length of the packet isn't needed here; there may
9531 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
9533 Return 0 if the packet could be parsed, -1 if it could not. If
9534 -1 is returned, the other variables may not be initialized. */
9537 remote_hostio_parse_result (char *buffer
, int *retcode
,
9538 int *remote_errno
, char **attachment
)
9545 if (buffer
[0] != 'F')
9549 *retcode
= strtol (&buffer
[1], &p
, 16);
9550 if (errno
!= 0 || p
== &buffer
[1])
9553 /* Check for ",errno". */
9557 *remote_errno
= strtol (p
+ 1, &p2
, 16);
9558 if (errno
!= 0 || p
+ 1 == p2
)
9563 /* Check for ";attachment". If there is no attachment, the
9564 packet should end here. */
9567 *attachment
= p
+ 1;
9570 else if (*p
== '\0')
9576 /* Send a prepared I/O packet to the target and read its response.
9577 The prepared packet is in the global RS->BUF before this function
9578 is called, and the answer is there when we return.
9580 COMMAND_BYTES is the length of the request to send, which may include
9581 binary data. WHICH_PACKET is the packet configuration to check
9582 before attempting a packet. If an error occurs, *REMOTE_ERRNO
9583 is set to the error number and -1 is returned. Otherwise the value
9584 returned by the function is returned.
9586 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
9587 attachment is expected; an error will be reported if there's a
9588 mismatch. If one is found, *ATTACHMENT will be set to point into
9589 the packet buffer and *ATTACHMENT_LEN will be set to the
9590 attachment's length. */
9593 remote_hostio_send_command (int command_bytes
, int which_packet
,
9594 int *remote_errno
, char **attachment
,
9595 int *attachment_len
)
9597 struct remote_state
*rs
= get_remote_state ();
9598 int ret
, bytes_read
;
9599 char *attachment_tmp
;
9601 if (!rs
->remote_desc
9602 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
9604 *remote_errno
= FILEIO_ENOSYS
;
9608 putpkt_binary (rs
->buf
, command_bytes
);
9609 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
9611 /* If it timed out, something is wrong. Don't try to parse the
9615 *remote_errno
= FILEIO_EINVAL
;
9619 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
9622 *remote_errno
= FILEIO_EINVAL
;
9624 case PACKET_UNKNOWN
:
9625 *remote_errno
= FILEIO_ENOSYS
;
9631 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
9634 *remote_errno
= FILEIO_EINVAL
;
9638 /* Make sure we saw an attachment if and only if we expected one. */
9639 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
9640 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
9642 *remote_errno
= FILEIO_EINVAL
;
9646 /* If an attachment was found, it must point into the packet buffer;
9647 work out how many bytes there were. */
9648 if (attachment_tmp
!= NULL
)
9650 *attachment
= attachment_tmp
;
9651 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
9657 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9658 remote file descriptor, or -1 if an error occurs (and set
9662 remote_hostio_open (struct target_ops
*self
,
9663 const char *filename
, int flags
, int mode
,
9666 struct remote_state
*rs
= get_remote_state ();
9668 int left
= get_remote_packet_size () - 1;
9670 remote_buffer_add_string (&p
, &left
, "vFile:open:");
9672 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9674 remote_buffer_add_string (&p
, &left
, ",");
9676 remote_buffer_add_int (&p
, &left
, flags
);
9677 remote_buffer_add_string (&p
, &left
, ",");
9679 remote_buffer_add_int (&p
, &left
, mode
);
9681 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
9682 remote_errno
, NULL
, NULL
);
9685 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9686 Return the number of bytes written, or -1 if an error occurs (and
9687 set *REMOTE_ERRNO). */
9690 remote_hostio_pwrite (struct target_ops
*self
,
9691 int fd
, const gdb_byte
*write_buf
, int len
,
9692 ULONGEST offset
, int *remote_errno
)
9694 struct remote_state
*rs
= get_remote_state ();
9696 int left
= get_remote_packet_size ();
9699 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
9701 remote_buffer_add_int (&p
, &left
, fd
);
9702 remote_buffer_add_string (&p
, &left
, ",");
9704 remote_buffer_add_int (&p
, &left
, offset
);
9705 remote_buffer_add_string (&p
, &left
, ",");
9707 p
+= remote_escape_output (write_buf
, len
, (gdb_byte
*) p
, &out_len
,
9708 get_remote_packet_size () - (p
- rs
->buf
));
9710 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
9711 remote_errno
, NULL
, NULL
);
9714 /* Read up to LEN bytes FD on the remote target into READ_BUF
9715 Return the number of bytes read, or -1 if an error occurs (and
9716 set *REMOTE_ERRNO). */
9719 remote_hostio_pread (struct target_ops
*self
,
9720 int fd
, gdb_byte
*read_buf
, int len
,
9721 ULONGEST offset
, int *remote_errno
)
9723 struct remote_state
*rs
= get_remote_state ();
9726 int left
= get_remote_packet_size ();
9727 int ret
, attachment_len
;
9730 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
9732 remote_buffer_add_int (&p
, &left
, fd
);
9733 remote_buffer_add_string (&p
, &left
, ",");
9735 remote_buffer_add_int (&p
, &left
, len
);
9736 remote_buffer_add_string (&p
, &left
, ",");
9738 remote_buffer_add_int (&p
, &left
, offset
);
9740 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
9741 remote_errno
, &attachment
,
9747 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
9749 if (read_len
!= ret
)
9750 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
9755 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9756 (and set *REMOTE_ERRNO). */
9759 remote_hostio_close (struct target_ops
*self
, int fd
, int *remote_errno
)
9761 struct remote_state
*rs
= get_remote_state ();
9763 int left
= get_remote_packet_size () - 1;
9765 remote_buffer_add_string (&p
, &left
, "vFile:close:");
9767 remote_buffer_add_int (&p
, &left
, fd
);
9769 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
9770 remote_errno
, NULL
, NULL
);
9773 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9774 occurs (and set *REMOTE_ERRNO). */
9777 remote_hostio_unlink (struct target_ops
*self
,
9778 const char *filename
, int *remote_errno
)
9780 struct remote_state
*rs
= get_remote_state ();
9782 int left
= get_remote_packet_size () - 1;
9784 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
9786 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9789 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
9790 remote_errno
, NULL
, NULL
);
9793 /* Read value of symbolic link FILENAME on the remote target. Return
9794 a null-terminated string allocated via xmalloc, or NULL if an error
9795 occurs (and set *REMOTE_ERRNO). */
9798 remote_hostio_readlink (struct target_ops
*self
,
9799 const char *filename
, int *remote_errno
)
9801 struct remote_state
*rs
= get_remote_state ();
9804 int left
= get_remote_packet_size ();
9805 int len
, attachment_len
;
9809 remote_buffer_add_string (&p
, &left
, "vFile:readlink:");
9811 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9814 len
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_readlink
,
9815 remote_errno
, &attachment
,
9821 ret
= xmalloc (len
+ 1);
9823 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
9824 (gdb_byte
*) ret
, len
);
9825 if (read_len
!= len
)
9826 error (_("Readlink returned %d, but %d bytes."), len
, read_len
);
9833 remote_fileio_errno_to_host (int errnum
)
9857 case FILEIO_ENOTDIR
:
9877 case FILEIO_ENAMETOOLONG
:
9878 return ENAMETOOLONG
;
9884 remote_hostio_error (int errnum
)
9886 int host_error
= remote_fileio_errno_to_host (errnum
);
9888 if (host_error
== -1)
9889 error (_("Unknown remote I/O error %d"), errnum
);
9891 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
9895 remote_hostio_close_cleanup (void *opaque
)
9897 int fd
= *(int *) opaque
;
9900 remote_hostio_close (find_target_at (process_stratum
), fd
, &remote_errno
);
9905 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
9907 const char *filename
= bfd_get_filename (abfd
);
9908 int fd
, remote_errno
;
9911 gdb_assert (remote_filename_p (filename
));
9913 fd
= remote_hostio_open (find_target_at (process_stratum
),
9914 filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
9917 errno
= remote_fileio_errno_to_host (remote_errno
);
9918 bfd_set_error (bfd_error_system_call
);
9922 stream
= xmalloc (sizeof (int));
9928 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
9930 int fd
= *(int *)stream
;
9935 /* Ignore errors on close; these may happen if the remote
9936 connection was already torn down. */
9937 remote_hostio_close (find_target_at (process_stratum
), fd
, &remote_errno
);
9939 /* Zero means success. */
9944 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
9945 file_ptr nbytes
, file_ptr offset
)
9947 int fd
= *(int *)stream
;
9949 file_ptr pos
, bytes
;
9952 while (nbytes
> pos
)
9954 bytes
= remote_hostio_pread (find_target_at (process_stratum
),
9955 fd
, (gdb_byte
*) buf
+ pos
, nbytes
- pos
,
9956 offset
+ pos
, &remote_errno
);
9958 /* Success, but no bytes, means end-of-file. */
9962 errno
= remote_fileio_errno_to_host (remote_errno
);
9963 bfd_set_error (bfd_error_system_call
);
9974 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
9976 /* FIXME: We should probably implement remote_hostio_stat. */
9977 sb
->st_size
= INT_MAX
;
9982 remote_filename_p (const char *filename
)
9984 return strncmp (filename
,
9985 REMOTE_SYSROOT_PREFIX
,
9986 sizeof (REMOTE_SYSROOT_PREFIX
) - 1) == 0;
9990 remote_bfd_open (const char *remote_file
, const char *target
)
9992 bfd
*abfd
= gdb_bfd_openr_iovec (remote_file
, target
,
9993 remote_bfd_iovec_open
, NULL
,
9994 remote_bfd_iovec_pread
,
9995 remote_bfd_iovec_close
,
9996 remote_bfd_iovec_stat
);
10002 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
10004 struct cleanup
*back_to
, *close_cleanup
;
10005 int retcode
, fd
, remote_errno
, bytes
, io_size
;
10008 int bytes_in_buffer
;
10011 struct remote_state
*rs
= get_remote_state ();
10013 if (!rs
->remote_desc
)
10014 error (_("command can only be used with remote target"));
10016 file
= gdb_fopen_cloexec (local_file
, "rb");
10018 perror_with_name (local_file
);
10019 back_to
= make_cleanup_fclose (file
);
10021 fd
= remote_hostio_open (find_target_at (process_stratum
),
10022 remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
10024 0700, &remote_errno
);
10026 remote_hostio_error (remote_errno
);
10028 /* Send up to this many bytes at once. They won't all fit in the
10029 remote packet limit, so we'll transfer slightly fewer. */
10030 io_size
= get_remote_packet_size ();
10031 buffer
= xmalloc (io_size
);
10032 make_cleanup (xfree
, buffer
);
10034 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
10036 bytes_in_buffer
= 0;
10039 while (bytes_in_buffer
|| !saw_eof
)
10043 bytes
= fread (buffer
+ bytes_in_buffer
, 1,
10044 io_size
- bytes_in_buffer
,
10049 error (_("Error reading %s."), local_file
);
10052 /* EOF. Unless there is something still in the
10053 buffer from the last iteration, we are done. */
10055 if (bytes_in_buffer
== 0)
10063 bytes
+= bytes_in_buffer
;
10064 bytes_in_buffer
= 0;
10066 retcode
= remote_hostio_pwrite (find_target_at (process_stratum
),
10068 offset
, &remote_errno
);
10071 remote_hostio_error (remote_errno
);
10072 else if (retcode
== 0)
10073 error (_("Remote write of %d bytes returned 0!"), bytes
);
10074 else if (retcode
< bytes
)
10076 /* Short write. Save the rest of the read data for the next
10078 bytes_in_buffer
= bytes
- retcode
;
10079 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
10085 discard_cleanups (close_cleanup
);
10086 if (remote_hostio_close (find_target_at (process_stratum
), fd
, &remote_errno
))
10087 remote_hostio_error (remote_errno
);
10090 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
10091 do_cleanups (back_to
);
10095 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
10097 struct cleanup
*back_to
, *close_cleanup
;
10098 int fd
, remote_errno
, bytes
, io_size
;
10102 struct remote_state
*rs
= get_remote_state ();
10104 if (!rs
->remote_desc
)
10105 error (_("command can only be used with remote target"));
10107 fd
= remote_hostio_open (find_target_at (process_stratum
),
10108 remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
10110 remote_hostio_error (remote_errno
);
10112 file
= gdb_fopen_cloexec (local_file
, "wb");
10114 perror_with_name (local_file
);
10115 back_to
= make_cleanup_fclose (file
);
10117 /* Send up to this many bytes at once. They won't all fit in the
10118 remote packet limit, so we'll transfer slightly fewer. */
10119 io_size
= get_remote_packet_size ();
10120 buffer
= xmalloc (io_size
);
10121 make_cleanup (xfree
, buffer
);
10123 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
10128 bytes
= remote_hostio_pread (find_target_at (process_stratum
),
10129 fd
, buffer
, io_size
, offset
, &remote_errno
);
10131 /* Success, but no bytes, means end-of-file. */
10134 remote_hostio_error (remote_errno
);
10138 bytes
= fwrite (buffer
, 1, bytes
, file
);
10140 perror_with_name (local_file
);
10143 discard_cleanups (close_cleanup
);
10144 if (remote_hostio_close (find_target_at (process_stratum
), fd
, &remote_errno
))
10145 remote_hostio_error (remote_errno
);
10148 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
10149 do_cleanups (back_to
);
10153 remote_file_delete (const char *remote_file
, int from_tty
)
10155 int retcode
, remote_errno
;
10156 struct remote_state
*rs
= get_remote_state ();
10158 if (!rs
->remote_desc
)
10159 error (_("command can only be used with remote target"));
10161 retcode
= remote_hostio_unlink (find_target_at (process_stratum
),
10162 remote_file
, &remote_errno
);
10164 remote_hostio_error (remote_errno
);
10167 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
10171 remote_put_command (char *args
, int from_tty
)
10173 struct cleanup
*back_to
;
10177 error_no_arg (_("file to put"));
10179 argv
= gdb_buildargv (args
);
10180 back_to
= make_cleanup_freeargv (argv
);
10181 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
10182 error (_("Invalid parameters to remote put"));
10184 remote_file_put (argv
[0], argv
[1], from_tty
);
10186 do_cleanups (back_to
);
10190 remote_get_command (char *args
, int from_tty
)
10192 struct cleanup
*back_to
;
10196 error_no_arg (_("file to get"));
10198 argv
= gdb_buildargv (args
);
10199 back_to
= make_cleanup_freeargv (argv
);
10200 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
10201 error (_("Invalid parameters to remote get"));
10203 remote_file_get (argv
[0], argv
[1], from_tty
);
10205 do_cleanups (back_to
);
10209 remote_delete_command (char *args
, int from_tty
)
10211 struct cleanup
*back_to
;
10215 error_no_arg (_("file to delete"));
10217 argv
= gdb_buildargv (args
);
10218 back_to
= make_cleanup_freeargv (argv
);
10219 if (argv
[0] == NULL
|| argv
[1] != NULL
)
10220 error (_("Invalid parameters to remote delete"));
10222 remote_file_delete (argv
[0], from_tty
);
10224 do_cleanups (back_to
);
10228 remote_command (char *args
, int from_tty
)
10230 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
10234 remote_can_execute_reverse (struct target_ops
*self
)
10236 if (remote_protocol_packets
[PACKET_bs
].support
== PACKET_ENABLE
10237 || remote_protocol_packets
[PACKET_bc
].support
== PACKET_ENABLE
)
10244 remote_supports_non_stop (struct target_ops
*self
)
10250 remote_supports_disable_randomization (struct target_ops
*self
)
10252 /* Only supported in extended mode. */
10257 remote_supports_multi_process (struct target_ops
*self
)
10259 struct remote_state
*rs
= get_remote_state ();
10261 /* Only extended-remote handles being attached to multiple
10262 processes, even though plain remote can use the multi-process
10263 thread id extensions, so that GDB knows the target process's
10265 return rs
->extended
&& remote_multi_process_p (rs
);
10269 remote_supports_cond_tracepoints (void)
10271 struct remote_state
*rs
= get_remote_state ();
10273 return rs
->cond_tracepoints
;
10277 remote_supports_cond_breakpoints (struct target_ops
*self
)
10279 struct remote_state
*rs
= get_remote_state ();
10281 return rs
->cond_breakpoints
;
10285 remote_supports_fast_tracepoints (void)
10287 struct remote_state
*rs
= get_remote_state ();
10289 return rs
->fast_tracepoints
;
10293 remote_supports_static_tracepoints (void)
10295 struct remote_state
*rs
= get_remote_state ();
10297 return rs
->static_tracepoints
;
10301 remote_supports_install_in_trace (void)
10303 struct remote_state
*rs
= get_remote_state ();
10305 return rs
->install_in_trace
;
10309 remote_supports_enable_disable_tracepoint (struct target_ops
*self
)
10311 struct remote_state
*rs
= get_remote_state ();
10313 return rs
->enable_disable_tracepoints
;
10317 remote_supports_string_tracing (struct target_ops
*self
)
10319 struct remote_state
*rs
= get_remote_state ();
10321 return rs
->string_tracing
;
10325 remote_can_run_breakpoint_commands (struct target_ops
*self
)
10327 struct remote_state
*rs
= get_remote_state ();
10329 return rs
->breakpoint_commands
;
10333 remote_trace_init (struct target_ops
*self
)
10336 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10337 if (strcmp (target_buf
, "OK") != 0)
10338 error (_("Target does not support this command."));
10341 static void free_actions_list (char **actions_list
);
10342 static void free_actions_list_cleanup_wrapper (void *);
10344 free_actions_list_cleanup_wrapper (void *al
)
10346 free_actions_list (al
);
10350 free_actions_list (char **actions_list
)
10354 if (actions_list
== 0)
10357 for (ndx
= 0; actions_list
[ndx
]; ndx
++)
10358 xfree (actions_list
[ndx
]);
10360 xfree (actions_list
);
10363 /* Recursive routine to walk through command list including loops, and
10364 download packets for each command. */
10367 remote_download_command_source (int num
, ULONGEST addr
,
10368 struct command_line
*cmds
)
10370 struct remote_state
*rs
= get_remote_state ();
10371 struct command_line
*cmd
;
10373 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
10375 QUIT
; /* Allow user to bail out with ^C. */
10376 strcpy (rs
->buf
, "QTDPsrc:");
10377 encode_source_string (num
, addr
, "cmd", cmd
->line
,
10378 rs
->buf
+ strlen (rs
->buf
),
10379 rs
->buf_size
- strlen (rs
->buf
));
10381 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10382 if (strcmp (target_buf
, "OK"))
10383 warning (_("Target does not support source download."));
10385 if (cmd
->control_type
== while_control
10386 || cmd
->control_type
== while_stepping_control
)
10388 remote_download_command_source (num
, addr
, *cmd
->body_list
);
10390 QUIT
; /* Allow user to bail out with ^C. */
10391 strcpy (rs
->buf
, "QTDPsrc:");
10392 encode_source_string (num
, addr
, "cmd", "end",
10393 rs
->buf
+ strlen (rs
->buf
),
10394 rs
->buf_size
- strlen (rs
->buf
));
10396 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10397 if (strcmp (target_buf
, "OK"))
10398 warning (_("Target does not support source download."));
10404 remote_download_tracepoint (struct target_ops
*self
, struct bp_location
*loc
)
10406 #define BUF_SIZE 2048
10410 char buf
[BUF_SIZE
];
10411 char **tdp_actions
;
10412 char **stepping_actions
;
10414 struct cleanup
*old_chain
= NULL
;
10415 struct agent_expr
*aexpr
;
10416 struct cleanup
*aexpr_chain
= NULL
;
10418 struct breakpoint
*b
= loc
->owner
;
10419 struct tracepoint
*t
= (struct tracepoint
*) b
;
10421 encode_actions_rsp (loc
, &tdp_actions
, &stepping_actions
);
10422 old_chain
= make_cleanup (free_actions_list_cleanup_wrapper
,
10424 (void) make_cleanup (free_actions_list_cleanup_wrapper
,
10427 tpaddr
= loc
->address
;
10428 sprintf_vma (addrbuf
, tpaddr
);
10429 xsnprintf (buf
, BUF_SIZE
, "QTDP:%x:%s:%c:%lx:%x", b
->number
,
10430 addrbuf
, /* address */
10431 (b
->enable_state
== bp_enabled
? 'E' : 'D'),
10432 t
->step_count
, t
->pass_count
);
10433 /* Fast tracepoints are mostly handled by the target, but we can
10434 tell the target how big of an instruction block should be moved
10436 if (b
->type
== bp_fast_tracepoint
)
10438 /* Only test for support at download time; we may not know
10439 target capabilities at definition time. */
10440 if (remote_supports_fast_tracepoints ())
10444 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
10445 tpaddr
, &isize
, NULL
))
10446 xsnprintf (buf
+ strlen (buf
), BUF_SIZE
- strlen (buf
), ":F%x",
10449 /* If it passed validation at definition but fails now,
10450 something is very wrong. */
10451 internal_error (__FILE__
, __LINE__
,
10452 _("Fast tracepoint not "
10453 "valid during download"));
10456 /* Fast tracepoints are functionally identical to regular
10457 tracepoints, so don't take lack of support as a reason to
10458 give up on the trace run. */
10459 warning (_("Target does not support fast tracepoints, "
10460 "downloading %d as regular tracepoint"), b
->number
);
10462 else if (b
->type
== bp_static_tracepoint
)
10464 /* Only test for support at download time; we may not know
10465 target capabilities at definition time. */
10466 if (remote_supports_static_tracepoints ())
10468 struct static_tracepoint_marker marker
;
10470 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
10471 strcat (buf
, ":S");
10473 error (_("Static tracepoint not valid during download"));
10476 /* Fast tracepoints are functionally identical to regular
10477 tracepoints, so don't take lack of support as a reason
10478 to give up on the trace run. */
10479 error (_("Target does not support static tracepoints"));
10481 /* If the tracepoint has a conditional, make it into an agent
10482 expression and append to the definition. */
10485 /* Only test support at download time, we may not know target
10486 capabilities at definition time. */
10487 if (remote_supports_cond_tracepoints ())
10489 aexpr
= gen_eval_for_expr (tpaddr
, loc
->cond
);
10490 aexpr_chain
= make_cleanup_free_agent_expr (aexpr
);
10491 xsnprintf (buf
+ strlen (buf
), BUF_SIZE
- strlen (buf
), ":X%x,",
10493 pkt
= buf
+ strlen (buf
);
10494 for (ndx
= 0; ndx
< aexpr
->len
; ++ndx
)
10495 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
10497 do_cleanups (aexpr_chain
);
10500 warning (_("Target does not support conditional tracepoints, "
10501 "ignoring tp %d cond"), b
->number
);
10504 if (b
->commands
|| *default_collect
)
10507 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10508 if (strcmp (target_buf
, "OK"))
10509 error (_("Target does not support tracepoints."));
10511 /* do_single_steps (t); */
10514 for (ndx
= 0; tdp_actions
[ndx
]; ndx
++)
10516 QUIT
; /* Allow user to bail out with ^C. */
10517 xsnprintf (buf
, BUF_SIZE
, "QTDP:-%x:%s:%s%c",
10518 b
->number
, addrbuf
, /* address */
10520 ((tdp_actions
[ndx
+ 1] || stepping_actions
)
10523 remote_get_noisy_reply (&target_buf
,
10525 if (strcmp (target_buf
, "OK"))
10526 error (_("Error on target while setting tracepoints."));
10529 if (stepping_actions
)
10531 for (ndx
= 0; stepping_actions
[ndx
]; ndx
++)
10533 QUIT
; /* Allow user to bail out with ^C. */
10534 xsnprintf (buf
, BUF_SIZE
, "QTDP:-%x:%s:%s%s%s",
10535 b
->number
, addrbuf
, /* address */
10536 ((ndx
== 0) ? "S" : ""),
10537 stepping_actions
[ndx
],
10538 (stepping_actions
[ndx
+ 1] ? "-" : ""));
10540 remote_get_noisy_reply (&target_buf
,
10542 if (strcmp (target_buf
, "OK"))
10543 error (_("Error on target while setting tracepoints."));
10547 if (remote_protocol_packets
[PACKET_TracepointSource
].support
10550 if (b
->addr_string
)
10552 strcpy (buf
, "QTDPsrc:");
10553 encode_source_string (b
->number
, loc
->address
,
10554 "at", b
->addr_string
, buf
+ strlen (buf
),
10555 2048 - strlen (buf
));
10558 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10559 if (strcmp (target_buf
, "OK"))
10560 warning (_("Target does not support source download."));
10562 if (b
->cond_string
)
10564 strcpy (buf
, "QTDPsrc:");
10565 encode_source_string (b
->number
, loc
->address
,
10566 "cond", b
->cond_string
, buf
+ strlen (buf
),
10567 2048 - strlen (buf
));
10569 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10570 if (strcmp (target_buf
, "OK"))
10571 warning (_("Target does not support source download."));
10573 remote_download_command_source (b
->number
, loc
->address
,
10574 breakpoint_commands (b
));
10577 do_cleanups (old_chain
);
10581 remote_can_download_tracepoint (struct target_ops
*self
)
10583 struct remote_state
*rs
= get_remote_state ();
10584 struct trace_status
*ts
;
10587 /* Don't try to install tracepoints until we've relocated our
10588 symbols, and fetched and merged the target's tracepoint list with
10590 if (rs
->starting_up
)
10593 ts
= current_trace_status ();
10594 status
= remote_get_trace_status (self
, ts
);
10596 if (status
== -1 || !ts
->running_known
|| !ts
->running
)
10599 /* If we are in a tracing experiment, but remote stub doesn't support
10600 installing tracepoint in trace, we have to return. */
10601 if (!remote_supports_install_in_trace ())
10609 remote_download_trace_state_variable (struct target_ops
*self
,
10610 struct trace_state_variable
*tsv
)
10612 struct remote_state
*rs
= get_remote_state ();
10615 xsnprintf (rs
->buf
, get_remote_packet_size (), "QTDV:%x:%s:%x:",
10616 tsv
->number
, phex ((ULONGEST
) tsv
->initial_value
, 8),
10618 p
= rs
->buf
+ strlen (rs
->buf
);
10619 if ((p
- rs
->buf
) + strlen (tsv
->name
) * 2 >= get_remote_packet_size ())
10620 error (_("Trace state variable name too long for tsv definition packet"));
10621 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
->name
), p
, strlen (tsv
->name
));
10624 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10625 if (*target_buf
== '\0')
10626 error (_("Target does not support this command."));
10627 if (strcmp (target_buf
, "OK") != 0)
10628 error (_("Error on target while downloading trace state variable."));
10632 remote_enable_tracepoint (struct target_ops
*self
,
10633 struct bp_location
*location
)
10635 struct remote_state
*rs
= get_remote_state ();
10638 sprintf_vma (addr_buf
, location
->address
);
10639 xsnprintf (rs
->buf
, get_remote_packet_size (), "QTEnable:%x:%s",
10640 location
->owner
->number
, addr_buf
);
10642 remote_get_noisy_reply (&rs
->buf
, &rs
->buf_size
);
10643 if (*rs
->buf
== '\0')
10644 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
10645 if (strcmp (rs
->buf
, "OK") != 0)
10646 error (_("Error on target while enabling tracepoint."));
10650 remote_disable_tracepoint (struct target_ops
*self
,
10651 struct bp_location
*location
)
10653 struct remote_state
*rs
= get_remote_state ();
10656 sprintf_vma (addr_buf
, location
->address
);
10657 xsnprintf (rs
->buf
, get_remote_packet_size (), "QTDisable:%x:%s",
10658 location
->owner
->number
, addr_buf
);
10660 remote_get_noisy_reply (&rs
->buf
, &rs
->buf_size
);
10661 if (*rs
->buf
== '\0')
10662 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
10663 if (strcmp (rs
->buf
, "OK") != 0)
10664 error (_("Error on target while disabling tracepoint."));
10668 remote_trace_set_readonly_regions (struct target_ops
*self
)
10672 bfd_size_type size
;
10678 return; /* No information to give. */
10680 strcpy (target_buf
, "QTro");
10681 offset
= strlen (target_buf
);
10682 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
10684 char tmp1
[40], tmp2
[40];
10687 if ((s
->flags
& SEC_LOAD
) == 0 ||
10688 /* (s->flags & SEC_CODE) == 0 || */
10689 (s
->flags
& SEC_READONLY
) == 0)
10693 vma
= bfd_get_section_vma (abfd
, s
);
10694 size
= bfd_get_section_size (s
);
10695 sprintf_vma (tmp1
, vma
);
10696 sprintf_vma (tmp2
, vma
+ size
);
10697 sec_length
= 1 + strlen (tmp1
) + 1 + strlen (tmp2
);
10698 if (offset
+ sec_length
+ 1 > target_buf_size
)
10700 if (remote_protocol_packets
[PACKET_qXfer_traceframe_info
].support
10703 Too many sections for read-only sections definition packet."));
10706 xsnprintf (target_buf
+ offset
, target_buf_size
- offset
, ":%s,%s",
10708 offset
+= sec_length
;
10712 putpkt (target_buf
);
10713 getpkt (&target_buf
, &target_buf_size
, 0);
10718 remote_trace_start (struct target_ops
*self
)
10720 putpkt ("QTStart");
10721 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10722 if (*target_buf
== '\0')
10723 error (_("Target does not support this command."));
10724 if (strcmp (target_buf
, "OK") != 0)
10725 error (_("Bogus reply from target: %s"), target_buf
);
10729 remote_get_trace_status (struct target_ops
*self
, struct trace_status
*ts
)
10731 /* Initialize it just to avoid a GCC false warning. */
10733 /* FIXME we need to get register block size some other way. */
10734 extern int trace_regblock_size
;
10735 volatile struct gdb_exception ex
;
10736 enum packet_result result
;
10738 if (remote_protocol_packets
[PACKET_qTStatus
].support
== PACKET_DISABLE
)
10741 trace_regblock_size
= get_remote_arch_state ()->sizeof_g_packet
;
10743 putpkt ("qTStatus");
10745 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
10747 p
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10751 if (ex
.error
!= TARGET_CLOSE_ERROR
)
10753 exception_fprintf (gdb_stderr
, ex
, "qTStatus: ");
10756 throw_exception (ex
);
10759 result
= packet_ok (p
, &remote_protocol_packets
[PACKET_qTStatus
]);
10761 /* If the remote target doesn't do tracing, flag it. */
10762 if (result
== PACKET_UNKNOWN
)
10765 /* We're working with a live target. */
10766 ts
->filename
= NULL
;
10769 error (_("Bogus trace status reply from target: %s"), target_buf
);
10771 /* Function 'parse_trace_status' sets default value of each field of
10772 'ts' at first, so we don't have to do it here. */
10773 parse_trace_status (p
, ts
);
10775 return ts
->running
;
10779 remote_get_tracepoint_status (struct target_ops
*self
, struct breakpoint
*bp
,
10780 struct uploaded_tp
*utp
)
10782 struct remote_state
*rs
= get_remote_state ();
10784 struct bp_location
*loc
;
10785 struct tracepoint
*tp
= (struct tracepoint
*) bp
;
10786 size_t size
= get_remote_packet_size ();
10790 tp
->base
.hit_count
= 0;
10791 tp
->traceframe_usage
= 0;
10792 for (loc
= tp
->base
.loc
; loc
; loc
= loc
->next
)
10794 /* If the tracepoint was never downloaded, don't go asking for
10796 if (tp
->number_on_target
== 0)
10798 xsnprintf (rs
->buf
, size
, "qTP:%x:%s", tp
->number_on_target
,
10799 phex_nz (loc
->address
, 0));
10801 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10802 if (reply
&& *reply
)
10805 parse_tracepoint_status (reply
+ 1, bp
, utp
);
10811 utp
->hit_count
= 0;
10812 utp
->traceframe_usage
= 0;
10813 xsnprintf (rs
->buf
, size
, "qTP:%x:%s", utp
->number
,
10814 phex_nz (utp
->addr
, 0));
10816 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10817 if (reply
&& *reply
)
10820 parse_tracepoint_status (reply
+ 1, bp
, utp
);
10826 remote_trace_stop (struct target_ops
*self
)
10829 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10830 if (*target_buf
== '\0')
10831 error (_("Target does not support this command."));
10832 if (strcmp (target_buf
, "OK") != 0)
10833 error (_("Bogus reply from target: %s"), target_buf
);
10837 remote_trace_find (struct target_ops
*self
,
10838 enum trace_find_type type
, int num
,
10839 CORE_ADDR addr1
, CORE_ADDR addr2
,
10842 struct remote_state
*rs
= get_remote_state ();
10843 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
10845 int target_frameno
= -1, target_tracept
= -1;
10847 /* Lookups other than by absolute frame number depend on the current
10848 trace selected, so make sure it is correct on the remote end
10850 if (type
!= tfind_number
)
10851 set_remote_traceframe ();
10854 strcpy (p
, "QTFrame:");
10855 p
= strchr (p
, '\0');
10859 xsnprintf (p
, endbuf
- p
, "%x", num
);
10862 xsnprintf (p
, endbuf
- p
, "pc:%s", phex_nz (addr1
, 0));
10865 xsnprintf (p
, endbuf
- p
, "tdp:%x", num
);
10868 xsnprintf (p
, endbuf
- p
, "range:%s:%s", phex_nz (addr1
, 0),
10869 phex_nz (addr2
, 0));
10871 case tfind_outside
:
10872 xsnprintf (p
, endbuf
- p
, "outside:%s:%s", phex_nz (addr1
, 0),
10873 phex_nz (addr2
, 0));
10876 error (_("Unknown trace find type %d"), type
);
10880 reply
= remote_get_noisy_reply (&(rs
->buf
), &rs
->buf_size
);
10881 if (*reply
== '\0')
10882 error (_("Target does not support this command."));
10884 while (reply
&& *reply
)
10889 target_frameno
= (int) strtol (p
, &reply
, 16);
10891 error (_("Unable to parse trace frame number"));
10892 /* Don't update our remote traceframe number cache on failure
10893 to select a remote traceframe. */
10894 if (target_frameno
== -1)
10899 target_tracept
= (int) strtol (p
, &reply
, 16);
10901 error (_("Unable to parse tracepoint number"));
10903 case 'O': /* "OK"? */
10904 if (reply
[1] == 'K' && reply
[2] == '\0')
10907 error (_("Bogus reply from target: %s"), reply
);
10910 error (_("Bogus reply from target: %s"), reply
);
10913 *tpp
= target_tracept
;
10915 rs
->remote_traceframe_number
= target_frameno
;
10916 return target_frameno
;
10920 remote_get_trace_state_variable_value (struct target_ops
*self
,
10921 int tsvnum
, LONGEST
*val
)
10923 struct remote_state
*rs
= get_remote_state ();
10927 set_remote_traceframe ();
10929 xsnprintf (rs
->buf
, get_remote_packet_size (), "qTV:%x", tsvnum
);
10931 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10932 if (reply
&& *reply
)
10936 unpack_varlen_hex (reply
+ 1, &uval
);
10937 *val
= (LONGEST
) uval
;
10945 remote_save_trace_data (struct target_ops
*self
, const char *filename
)
10947 struct remote_state
*rs
= get_remote_state ();
10951 strcpy (p
, "QTSave:");
10953 if ((p
- rs
->buf
) + strlen (filename
) * 2 >= get_remote_packet_size ())
10954 error (_("Remote file name too long for trace save packet"));
10955 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, strlen (filename
));
10958 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10959 if (*reply
== '\0')
10960 error (_("Target does not support this command."));
10961 if (strcmp (reply
, "OK") != 0)
10962 error (_("Bogus reply from target: %s"), reply
);
10966 /* This is basically a memory transfer, but needs to be its own packet
10967 because we don't know how the target actually organizes its trace
10968 memory, plus we want to be able to ask for as much as possible, but
10969 not be unhappy if we don't get as much as we ask for. */
10972 remote_get_raw_trace_data (struct target_ops
*self
,
10973 gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
10975 struct remote_state
*rs
= get_remote_state ();
10981 strcpy (p
, "qTBuffer:");
10983 p
+= hexnumstr (p
, offset
);
10985 p
+= hexnumstr (p
, len
);
10989 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10990 if (reply
&& *reply
)
10992 /* 'l' by itself means we're at the end of the buffer and
10993 there is nothing more to get. */
10997 /* Convert the reply into binary. Limit the number of bytes to
10998 convert according to our passed-in buffer size, rather than
10999 what was returned in the packet; if the target is
11000 unexpectedly generous and gives us a bigger reply than we
11001 asked for, we don't want to crash. */
11002 rslt
= hex2bin (target_buf
, buf
, len
);
11006 /* Something went wrong, flag as an error. */
11011 remote_set_disconnected_tracing (struct target_ops
*self
, int val
)
11013 struct remote_state
*rs
= get_remote_state ();
11015 if (rs
->disconnected_tracing
)
11019 xsnprintf (rs
->buf
, get_remote_packet_size (), "QTDisconnected:%x", val
);
11021 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
11022 if (*reply
== '\0')
11023 error (_("Target does not support this command."));
11024 if (strcmp (reply
, "OK") != 0)
11025 error (_("Bogus reply from target: %s"), reply
);
11028 warning (_("Target does not support disconnected tracing."));
11032 remote_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
11034 struct thread_info
*info
= find_thread_ptid (ptid
);
11036 if (info
&& info
->private)
11037 return info
->private->core
;
11042 remote_set_circular_trace_buffer (struct target_ops
*self
, int val
)
11044 struct remote_state
*rs
= get_remote_state ();
11047 xsnprintf (rs
->buf
, get_remote_packet_size (), "QTBuffer:circular:%x", val
);
11049 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
11050 if (*reply
== '\0')
11051 error (_("Target does not support this command."));
11052 if (strcmp (reply
, "OK") != 0)
11053 error (_("Bogus reply from target: %s"), reply
);
11056 static struct traceframe_info
*
11057 remote_traceframe_info (struct target_ops
*self
)
11061 /* If current traceframe is not selected, don't bother the remote
11063 if (get_traceframe_number () < 0)
11066 text
= target_read_stralloc (¤t_target
,
11067 TARGET_OBJECT_TRACEFRAME_INFO
, NULL
);
11070 struct traceframe_info
*info
;
11071 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
11073 info
= parse_traceframe_info (text
);
11074 do_cleanups (back_to
);
11081 /* Handle the qTMinFTPILen packet. Returns the minimum length of
11082 instruction on which a fast tracepoint may be placed. Returns -1
11083 if the packet is not supported, and 0 if the minimum instruction
11084 length is unknown. */
11087 remote_get_min_fast_tracepoint_insn_len (struct target_ops
*self
)
11089 struct remote_state
*rs
= get_remote_state ();
11092 /* If we're not debugging a process yet, the IPA can't be
11094 if (!target_has_execution
)
11097 /* Make sure the remote is pointing at the right process. */
11098 set_general_process ();
11100 xsnprintf (rs
->buf
, get_remote_packet_size (), "qTMinFTPILen");
11102 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
11103 if (*reply
== '\0')
11107 ULONGEST min_insn_len
;
11109 unpack_varlen_hex (reply
, &min_insn_len
);
11111 return (int) min_insn_len
;
11116 remote_set_trace_buffer_size (struct target_ops
*self
, LONGEST val
)
11118 if (remote_protocol_packets
[PACKET_QTBuffer_size
].support
11121 struct remote_state
*rs
= get_remote_state ();
11122 char *buf
= rs
->buf
;
11123 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
11124 enum packet_result result
;
11126 gdb_assert (val
>= 0 || val
== -1);
11127 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTBuffer:size:");
11128 /* Send -1 as literal "-1" to avoid host size dependency. */
11132 buf
+= hexnumstr (buf
, (ULONGEST
) -val
);
11135 buf
+= hexnumstr (buf
, (ULONGEST
) val
);
11138 remote_get_noisy_reply (&rs
->buf
, &rs
->buf_size
);
11139 result
= packet_ok (rs
->buf
,
11140 &remote_protocol_packets
[PACKET_QTBuffer_size
]);
11142 if (result
!= PACKET_OK
)
11143 warning (_("Bogus reply from target: %s"), rs
->buf
);
11148 remote_set_trace_notes (struct target_ops
*self
,
11149 const char *user
, const char *notes
,
11150 const char *stop_notes
)
11152 struct remote_state
*rs
= get_remote_state ();
11154 char *buf
= rs
->buf
;
11155 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
11158 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTNotes:");
11161 buf
+= xsnprintf (buf
, endbuf
- buf
, "user:");
11162 nbytes
= bin2hex ((gdb_byte
*) user
, buf
, strlen (user
));
11168 buf
+= xsnprintf (buf
, endbuf
- buf
, "notes:");
11169 nbytes
= bin2hex ((gdb_byte
*) notes
, buf
, strlen (notes
));
11175 buf
+= xsnprintf (buf
, endbuf
- buf
, "tstop:");
11176 nbytes
= bin2hex ((gdb_byte
*) stop_notes
, buf
, strlen (stop_notes
));
11180 /* Ensure the buffer is terminated. */
11184 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
11185 if (*reply
== '\0')
11188 if (strcmp (reply
, "OK") != 0)
11189 error (_("Bogus reply from target: %s"), reply
);
11195 remote_use_agent (struct target_ops
*self
, int use
)
11197 if (remote_protocol_packets
[PACKET_QAgent
].support
!= PACKET_DISABLE
)
11199 struct remote_state
*rs
= get_remote_state ();
11201 /* If the stub supports QAgent. */
11202 xsnprintf (rs
->buf
, get_remote_packet_size (), "QAgent:%d", use
);
11204 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11206 if (strcmp (rs
->buf
, "OK") == 0)
11217 remote_can_use_agent (struct target_ops
*self
)
11219 return (remote_protocol_packets
[PACKET_QAgent
].support
!= PACKET_DISABLE
);
11222 struct btrace_target_info
11224 /* The ptid of the traced thread. */
11228 /* Check whether the target supports branch tracing. */
11231 remote_supports_btrace (struct target_ops
*self
)
11233 if (remote_protocol_packets
[PACKET_Qbtrace_off
].support
!= PACKET_ENABLE
)
11235 if (remote_protocol_packets
[PACKET_Qbtrace_bts
].support
!= PACKET_ENABLE
)
11237 if (remote_protocol_packets
[PACKET_qXfer_btrace
].support
!= PACKET_ENABLE
)
11243 /* Enable branch tracing. */
11245 static struct btrace_target_info
*
11246 remote_enable_btrace (struct target_ops
*self
, ptid_t ptid
)
11248 struct btrace_target_info
*tinfo
= NULL
;
11249 struct packet_config
*packet
= &remote_protocol_packets
[PACKET_Qbtrace_bts
];
11250 struct remote_state
*rs
= get_remote_state ();
11251 char *buf
= rs
->buf
;
11252 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
11254 if (packet
->support
!= PACKET_ENABLE
)
11255 error (_("Target does not support branch tracing."));
11257 set_general_thread (ptid
);
11259 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s", packet
->name
);
11261 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11263 if (packet_ok (rs
->buf
, packet
) == PACKET_ERROR
)
11265 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
11266 error (_("Could not enable branch tracing for %s: %s"),
11267 target_pid_to_str (ptid
), rs
->buf
+ 2);
11269 error (_("Could not enable branch tracing for %s."),
11270 target_pid_to_str (ptid
));
11273 tinfo
= xzalloc (sizeof (*tinfo
));
11274 tinfo
->ptid
= ptid
;
11279 /* Disable branch tracing. */
11282 remote_disable_btrace (struct target_ops
*self
,
11283 struct btrace_target_info
*tinfo
)
11285 struct packet_config
*packet
= &remote_protocol_packets
[PACKET_Qbtrace_off
];
11286 struct remote_state
*rs
= get_remote_state ();
11287 char *buf
= rs
->buf
;
11288 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
11290 if (packet
->support
!= PACKET_ENABLE
)
11291 error (_("Target does not support branch tracing."));
11293 set_general_thread (tinfo
->ptid
);
11295 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s", packet
->name
);
11297 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11299 if (packet_ok (rs
->buf
, packet
) == PACKET_ERROR
)
11301 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
11302 error (_("Could not disable branch tracing for %s: %s"),
11303 target_pid_to_str (tinfo
->ptid
), rs
->buf
+ 2);
11305 error (_("Could not disable branch tracing for %s."),
11306 target_pid_to_str (tinfo
->ptid
));
11312 /* Teardown branch tracing. */
11315 remote_teardown_btrace (struct target_ops
*self
,
11316 struct btrace_target_info
*tinfo
)
11318 /* We must not talk to the target during teardown. */
11322 /* Read the branch trace. */
11324 static enum btrace_error
11325 remote_read_btrace (struct target_ops
*self
,
11326 VEC (btrace_block_s
) **btrace
,
11327 struct btrace_target_info
*tinfo
,
11328 enum btrace_read_type type
)
11330 struct packet_config
*packet
= &remote_protocol_packets
[PACKET_qXfer_btrace
];
11331 struct remote_state
*rs
= get_remote_state ();
11332 struct cleanup
*cleanup
;
11336 if (packet
->support
!= PACKET_ENABLE
)
11337 error (_("Target does not support branch tracing."));
11339 #if !defined(HAVE_LIBEXPAT)
11340 error (_("Cannot process branch tracing result. XML parsing not supported."));
11345 case BTRACE_READ_ALL
:
11348 case BTRACE_READ_NEW
:
11351 case BTRACE_READ_DELTA
:
11355 internal_error (__FILE__
, __LINE__
,
11356 _("Bad branch tracing read type: %u."),
11357 (unsigned int) type
);
11360 xml
= target_read_stralloc (¤t_target
,
11361 TARGET_OBJECT_BTRACE
, annex
);
11363 return BTRACE_ERR_UNKNOWN
;
11365 cleanup
= make_cleanup (xfree
, xml
);
11366 *btrace
= parse_xml_btrace (xml
);
11367 do_cleanups (cleanup
);
11369 return BTRACE_ERR_NONE
;
11373 remote_augmented_libraries_svr4_read (struct target_ops
*self
)
11375 struct remote_state
*rs
= get_remote_state ();
11377 return rs
->augmented_libraries_svr4_read
;
11380 /* Implementation of to_load. */
11383 remote_load (struct target_ops
*self
, char *name
, int from_tty
)
11385 generic_load (name
, from_tty
);
11389 init_remote_ops (void)
11391 remote_ops
.to_shortname
= "remote";
11392 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
11393 remote_ops
.to_doc
=
11394 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11395 Specify the serial device it is connected to\n\
11396 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
11397 remote_ops
.to_open
= remote_open
;
11398 remote_ops
.to_close
= remote_close
;
11399 remote_ops
.to_detach
= remote_detach
;
11400 remote_ops
.to_disconnect
= remote_disconnect
;
11401 remote_ops
.to_resume
= remote_resume
;
11402 remote_ops
.to_wait
= remote_wait
;
11403 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
11404 remote_ops
.to_store_registers
= remote_store_registers
;
11405 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
11406 remote_ops
.to_files_info
= remote_files_info
;
11407 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
11408 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
11409 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
11410 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
11411 remote_ops
.to_watchpoint_addr_within_range
=
11412 remote_watchpoint_addr_within_range
;
11413 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
11414 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
11415 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
11416 remote_ops
.to_region_ok_for_hw_watchpoint
11417 = remote_region_ok_for_hw_watchpoint
;
11418 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
11419 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
11420 remote_ops
.to_kill
= remote_kill
;
11421 remote_ops
.to_load
= remote_load
;
11422 remote_ops
.to_mourn_inferior
= remote_mourn
;
11423 remote_ops
.to_pass_signals
= remote_pass_signals
;
11424 remote_ops
.to_program_signals
= remote_program_signals
;
11425 remote_ops
.to_thread_alive
= remote_thread_alive
;
11426 remote_ops
.to_find_new_threads
= remote_threads_info
;
11427 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
11428 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
11429 remote_ops
.to_get_ada_task_ptid
= remote_get_ada_task_ptid
;
11430 remote_ops
.to_stop
= remote_stop
;
11431 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
11432 remote_ops
.to_rcmd
= remote_rcmd
;
11433 remote_ops
.to_log_command
= serial_log_command
;
11434 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
11435 remote_ops
.to_stratum
= process_stratum
;
11436 remote_ops
.to_has_all_memory
= default_child_has_all_memory
;
11437 remote_ops
.to_has_memory
= default_child_has_memory
;
11438 remote_ops
.to_has_stack
= default_child_has_stack
;
11439 remote_ops
.to_has_registers
= default_child_has_registers
;
11440 remote_ops
.to_has_execution
= default_child_has_execution
;
11441 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
11442 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
11443 remote_ops
.to_magic
= OPS_MAGIC
;
11444 remote_ops
.to_memory_map
= remote_memory_map
;
11445 remote_ops
.to_flash_erase
= remote_flash_erase
;
11446 remote_ops
.to_flash_done
= remote_flash_done
;
11447 remote_ops
.to_read_description
= remote_read_description
;
11448 remote_ops
.to_search_memory
= remote_search_memory
;
11449 remote_ops
.to_can_async_p
= remote_can_async_p
;
11450 remote_ops
.to_is_async_p
= remote_is_async_p
;
11451 remote_ops
.to_async
= remote_async
;
11452 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
11453 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
11454 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
11455 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
11456 remote_ops
.to_supports_disable_randomization
11457 = remote_supports_disable_randomization
;
11458 remote_ops
.to_fileio_open
= remote_hostio_open
;
11459 remote_ops
.to_fileio_pwrite
= remote_hostio_pwrite
;
11460 remote_ops
.to_fileio_pread
= remote_hostio_pread
;
11461 remote_ops
.to_fileio_close
= remote_hostio_close
;
11462 remote_ops
.to_fileio_unlink
= remote_hostio_unlink
;
11463 remote_ops
.to_fileio_readlink
= remote_hostio_readlink
;
11464 remote_ops
.to_supports_enable_disable_tracepoint
= remote_supports_enable_disable_tracepoint
;
11465 remote_ops
.to_supports_string_tracing
= remote_supports_string_tracing
;
11466 remote_ops
.to_supports_evaluation_of_breakpoint_conditions
= remote_supports_cond_breakpoints
;
11467 remote_ops
.to_can_run_breakpoint_commands
= remote_can_run_breakpoint_commands
;
11468 remote_ops
.to_trace_init
= remote_trace_init
;
11469 remote_ops
.to_download_tracepoint
= remote_download_tracepoint
;
11470 remote_ops
.to_can_download_tracepoint
= remote_can_download_tracepoint
;
11471 remote_ops
.to_download_trace_state_variable
11472 = remote_download_trace_state_variable
;
11473 remote_ops
.to_enable_tracepoint
= remote_enable_tracepoint
;
11474 remote_ops
.to_disable_tracepoint
= remote_disable_tracepoint
;
11475 remote_ops
.to_trace_set_readonly_regions
= remote_trace_set_readonly_regions
;
11476 remote_ops
.to_trace_start
= remote_trace_start
;
11477 remote_ops
.to_get_trace_status
= remote_get_trace_status
;
11478 remote_ops
.to_get_tracepoint_status
= remote_get_tracepoint_status
;
11479 remote_ops
.to_trace_stop
= remote_trace_stop
;
11480 remote_ops
.to_trace_find
= remote_trace_find
;
11481 remote_ops
.to_get_trace_state_variable_value
11482 = remote_get_trace_state_variable_value
;
11483 remote_ops
.to_save_trace_data
= remote_save_trace_data
;
11484 remote_ops
.to_upload_tracepoints
= remote_upload_tracepoints
;
11485 remote_ops
.to_upload_trace_state_variables
11486 = remote_upload_trace_state_variables
;
11487 remote_ops
.to_get_raw_trace_data
= remote_get_raw_trace_data
;
11488 remote_ops
.to_get_min_fast_tracepoint_insn_len
= remote_get_min_fast_tracepoint_insn_len
;
11489 remote_ops
.to_set_disconnected_tracing
= remote_set_disconnected_tracing
;
11490 remote_ops
.to_set_circular_trace_buffer
= remote_set_circular_trace_buffer
;
11491 remote_ops
.to_set_trace_buffer_size
= remote_set_trace_buffer_size
;
11492 remote_ops
.to_set_trace_notes
= remote_set_trace_notes
;
11493 remote_ops
.to_core_of_thread
= remote_core_of_thread
;
11494 remote_ops
.to_verify_memory
= remote_verify_memory
;
11495 remote_ops
.to_get_tib_address
= remote_get_tib_address
;
11496 remote_ops
.to_set_permissions
= remote_set_permissions
;
11497 remote_ops
.to_static_tracepoint_marker_at
11498 = remote_static_tracepoint_marker_at
;
11499 remote_ops
.to_static_tracepoint_markers_by_strid
11500 = remote_static_tracepoint_markers_by_strid
;
11501 remote_ops
.to_traceframe_info
= remote_traceframe_info
;
11502 remote_ops
.to_use_agent
= remote_use_agent
;
11503 remote_ops
.to_can_use_agent
= remote_can_use_agent
;
11504 remote_ops
.to_supports_btrace
= remote_supports_btrace
;
11505 remote_ops
.to_enable_btrace
= remote_enable_btrace
;
11506 remote_ops
.to_disable_btrace
= remote_disable_btrace
;
11507 remote_ops
.to_teardown_btrace
= remote_teardown_btrace
;
11508 remote_ops
.to_read_btrace
= remote_read_btrace
;
11509 remote_ops
.to_augmented_libraries_svr4_read
=
11510 remote_augmented_libraries_svr4_read
;
11513 /* Set up the extended remote vector by making a copy of the standard
11514 remote vector and adding to it. */
11517 init_extended_remote_ops (void)
11519 extended_remote_ops
= remote_ops
;
11521 extended_remote_ops
.to_shortname
= "extended-remote";
11522 extended_remote_ops
.to_longname
=
11523 "Extended remote serial target in gdb-specific protocol";
11524 extended_remote_ops
.to_doc
=
11525 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11526 Specify the serial device it is connected to (e.g. /dev/ttya).";
11527 extended_remote_ops
.to_open
= extended_remote_open
;
11528 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
11529 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
11530 extended_remote_ops
.to_detach
= extended_remote_detach
;
11531 extended_remote_ops
.to_attach
= extended_remote_attach
;
11532 extended_remote_ops
.to_kill
= extended_remote_kill
;
11533 extended_remote_ops
.to_supports_disable_randomization
11534 = extended_remote_supports_disable_randomization
;
11538 remote_can_async_p (struct target_ops
*ops
)
11540 struct remote_state
*rs
= get_remote_state ();
11542 if (!target_async_permitted
)
11543 /* We only enable async when the user specifically asks for it. */
11546 /* We're async whenever the serial device is. */
11547 return serial_can_async_p (rs
->remote_desc
);
11551 remote_is_async_p (struct target_ops
*ops
)
11553 struct remote_state
*rs
= get_remote_state ();
11555 if (!target_async_permitted
)
11556 /* We only enable async when the user specifically asks for it. */
11559 /* We're async whenever the serial device is. */
11560 return serial_is_async_p (rs
->remote_desc
);
11563 /* Pass the SERIAL event on and up to the client. One day this code
11564 will be able to delay notifying the client of an event until the
11565 point where an entire packet has been received. */
11567 static serial_event_ftype remote_async_serial_handler
;
11570 remote_async_serial_handler (struct serial
*scb
, void *context
)
11572 struct remote_state
*rs
= context
;
11574 /* Don't propogate error information up to the client. Instead let
11575 the client find out about the error by querying the target. */
11576 rs
->async_client_callback (INF_REG_EVENT
, rs
->async_client_context
);
11580 remote_async_inferior_event_handler (gdb_client_data data
)
11582 inferior_event_handler (INF_REG_EVENT
, NULL
);
11586 remote_async (struct target_ops
*ops
,
11587 void (*callback
) (enum inferior_event_type event_type
,
11591 struct remote_state
*rs
= get_remote_state ();
11593 if (callback
!= NULL
)
11595 serial_async (rs
->remote_desc
, remote_async_serial_handler
, rs
);
11596 rs
->async_client_callback
= callback
;
11597 rs
->async_client_context
= context
;
11600 serial_async (rs
->remote_desc
, NULL
, NULL
);
11604 set_remote_cmd (char *args
, int from_tty
)
11606 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
11610 show_remote_cmd (char *args
, int from_tty
)
11612 /* We can't just use cmd_show_list here, because we want to skip
11613 the redundant "show remote Z-packet" and the legacy aliases. */
11614 struct cleanup
*showlist_chain
;
11615 struct cmd_list_element
*list
= remote_show_cmdlist
;
11616 struct ui_out
*uiout
= current_uiout
;
11618 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
11619 for (; list
!= NULL
; list
= list
->next
)
11620 if (strcmp (list
->name
, "Z-packet") == 0)
11622 else if (list
->type
== not_set_cmd
)
11623 /* Alias commands are exactly like the original, except they
11624 don't have the normal type. */
11628 struct cleanup
*option_chain
11629 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
11631 ui_out_field_string (uiout
, "name", list
->name
);
11632 ui_out_text (uiout
, ": ");
11633 if (list
->type
== show_cmd
)
11634 do_show_command ((char *) NULL
, from_tty
, list
);
11636 cmd_func (list
, NULL
, from_tty
);
11637 /* Close the tuple. */
11638 do_cleanups (option_chain
);
11641 /* Close the tuple. */
11642 do_cleanups (showlist_chain
);
11646 /* Function to be called whenever a new objfile (shlib) is detected. */
11648 remote_new_objfile (struct objfile
*objfile
)
11650 struct remote_state
*rs
= get_remote_state ();
11652 if (rs
->remote_desc
!= 0) /* Have a remote connection. */
11653 remote_check_symbols ();
11656 /* Pull all the tracepoints defined on the target and create local
11657 data structures representing them. We don't want to create real
11658 tracepoints yet, we don't want to mess up the user's existing
11662 remote_upload_tracepoints (struct target_ops
*self
, struct uploaded_tp
**utpp
)
11664 struct remote_state
*rs
= get_remote_state ();
11667 /* Ask for a first packet of tracepoint definition. */
11669 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11671 while (*p
&& *p
!= 'l')
11673 parse_tracepoint_definition (p
, utpp
);
11674 /* Ask for another packet of tracepoint definition. */
11676 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11683 remote_upload_trace_state_variables (struct target_ops
*self
,
11684 struct uploaded_tsv
**utsvp
)
11686 struct remote_state
*rs
= get_remote_state ();
11689 /* Ask for a first packet of variable definition. */
11691 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11693 while (*p
&& *p
!= 'l')
11695 parse_tsv_definition (p
, utsvp
);
11696 /* Ask for another packet of variable definition. */
11698 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11704 /* The "set/show range-stepping" show hook. */
11707 show_range_stepping (struct ui_file
*file
, int from_tty
,
11708 struct cmd_list_element
*c
,
11711 fprintf_filtered (file
,
11712 _("Debugger's willingness to use range stepping "
11713 "is %s.\n"), value
);
11716 /* The "set/show range-stepping" set hook. */
11719 set_range_stepping (char *ignore_args
, int from_tty
,
11720 struct cmd_list_element
*c
)
11722 struct remote_state
*rs
= get_remote_state ();
11724 /* Whene enabling, check whether range stepping is actually
11725 supported by the target, and warn if not. */
11726 if (use_range_stepping
)
11728 if (rs
->remote_desc
!= NULL
)
11730 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
11731 remote_vcont_probe (rs
);
11733 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_ENABLE
11734 && rs
->supports_vCont
.r
)
11738 warning (_("Range stepping is not supported by the current target"));
11743 _initialize_remote (void)
11745 struct remote_state
*rs
;
11746 struct cmd_list_element
*cmd
;
11747 const char *cmd_name
;
11749 /* architecture specific data */
11750 remote_gdbarch_data_handle
=
11751 gdbarch_data_register_post_init (init_remote_state
);
11752 remote_g_packet_data_handle
=
11753 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
11755 /* Initialize the per-target state. At the moment there is only one
11756 of these, not one per target. Only one target is active at a
11758 remote_state
= new_remote_state ();
11760 init_remote_ops ();
11761 add_target (&remote_ops
);
11763 init_extended_remote_ops ();
11764 add_target (&extended_remote_ops
);
11766 /* Hook into new objfile notification. */
11767 observer_attach_new_objfile (remote_new_objfile
);
11768 /* We're no longer interested in notification events of an inferior
11770 observer_attach_inferior_exit (discard_pending_stop_replies
);
11772 /* Set up signal handlers. */
11773 async_sigint_remote_token
=
11774 create_async_signal_handler (async_remote_interrupt
, NULL
);
11775 async_sigint_remote_twice_token
=
11776 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
11779 init_remote_threadtests ();
11782 stop_reply_queue
= QUEUE_alloc (stop_reply_p
, stop_reply_xfree
);
11783 /* set/show remote ... */
11785 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
11786 Remote protocol specific variables\n\
11787 Configure various remote-protocol specific variables such as\n\
11788 the packets being used"),
11789 &remote_set_cmdlist
, "set remote ",
11790 0 /* allow-unknown */, &setlist
);
11791 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
11792 Remote protocol specific variables\n\
11793 Configure various remote-protocol specific variables such as\n\
11794 the packets being used"),
11795 &remote_show_cmdlist
, "show remote ",
11796 0 /* allow-unknown */, &showlist
);
11798 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
11799 Compare section data on target to the exec file.\n\
11800 Argument is a single section name (default: all loaded sections)."),
11803 add_cmd ("packet", class_maintenance
, packet_command
, _("\
11804 Send an arbitrary packet to a remote target.\n\
11805 maintenance packet TEXT\n\
11806 If GDB is talking to an inferior via the GDB serial protocol, then\n\
11807 this command sends the string TEXT to the inferior, and displays the\n\
11808 response packet. GDB supplies the initial `$' character, and the\n\
11809 terminating `#' character and checksum."),
11812 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
11813 Set whether to send break if interrupted."), _("\
11814 Show whether to send break if interrupted."), _("\
11815 If set, a break, instead of a cntrl-c, is sent to the remote target."),
11816 set_remotebreak
, show_remotebreak
,
11817 &setlist
, &showlist
);
11818 cmd_name
= "remotebreak";
11819 cmd
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
11820 deprecate_cmd (cmd
, "set remote interrupt-sequence");
11821 cmd_name
= "remotebreak"; /* needed because lookup_cmd updates the pointer */
11822 cmd
= lookup_cmd (&cmd_name
, showlist
, "", -1, 1);
11823 deprecate_cmd (cmd
, "show remote interrupt-sequence");
11825 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
11826 interrupt_sequence_modes
, &interrupt_sequence_mode
,
11828 Set interrupt sequence to remote target."), _("\
11829 Show interrupt sequence to remote target."), _("\
11830 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
11831 NULL
, show_interrupt_sequence
,
11832 &remote_set_cmdlist
,
11833 &remote_show_cmdlist
);
11835 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
11836 &interrupt_on_connect
, _("\
11837 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11838 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11839 If set, interrupt sequence is sent to remote target."),
11841 &remote_set_cmdlist
, &remote_show_cmdlist
);
11843 /* Install commands for configuring memory read/write packets. */
11845 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
11846 Set the maximum number of bytes per memory write packet (deprecated)."),
11848 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
11849 Show the maximum number of bytes per memory write packet (deprecated)."),
11851 add_cmd ("memory-write-packet-size", no_class
,
11852 set_memory_write_packet_size
, _("\
11853 Set the maximum number of bytes per memory-write packet.\n\
11854 Specify the number of bytes in a packet or 0 (zero) for the\n\
11855 default packet size. The actual limit is further reduced\n\
11856 dependent on the target. Specify ``fixed'' to disable the\n\
11857 further restriction and ``limit'' to enable that restriction."),
11858 &remote_set_cmdlist
);
11859 add_cmd ("memory-read-packet-size", no_class
,
11860 set_memory_read_packet_size
, _("\
11861 Set the maximum number of bytes per memory-read packet.\n\
11862 Specify the number of bytes in a packet or 0 (zero) for the\n\
11863 default packet size. The actual limit is further reduced\n\
11864 dependent on the target. Specify ``fixed'' to disable the\n\
11865 further restriction and ``limit'' to enable that restriction."),
11866 &remote_set_cmdlist
);
11867 add_cmd ("memory-write-packet-size", no_class
,
11868 show_memory_write_packet_size
,
11869 _("Show the maximum number of bytes per memory-write packet."),
11870 &remote_show_cmdlist
);
11871 add_cmd ("memory-read-packet-size", no_class
,
11872 show_memory_read_packet_size
,
11873 _("Show the maximum number of bytes per memory-read packet."),
11874 &remote_show_cmdlist
);
11876 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
11877 &remote_hw_watchpoint_limit
, _("\
11878 Set the maximum number of target hardware watchpoints."), _("\
11879 Show the maximum number of target hardware watchpoints."), _("\
11880 Specify a negative limit for unlimited."),
11881 NULL
, NULL
, /* FIXME: i18n: The maximum
11882 number of target hardware
11883 watchpoints is %s. */
11884 &remote_set_cmdlist
, &remote_show_cmdlist
);
11885 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class
,
11886 &remote_hw_watchpoint_length_limit
, _("\
11887 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
11888 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
11889 Specify a negative limit for unlimited."),
11890 NULL
, NULL
, /* FIXME: i18n: The maximum
11891 length (in bytes) of a target
11892 hardware watchpoint is %s. */
11893 &remote_set_cmdlist
, &remote_show_cmdlist
);
11894 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
11895 &remote_hw_breakpoint_limit
, _("\
11896 Set the maximum number of target hardware breakpoints."), _("\
11897 Show the maximum number of target hardware breakpoints."), _("\
11898 Specify a negative limit for unlimited."),
11899 NULL
, NULL
, /* FIXME: i18n: The maximum
11900 number of target hardware
11901 breakpoints is %s. */
11902 &remote_set_cmdlist
, &remote_show_cmdlist
);
11904 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure
,
11905 &remote_address_size
, _("\
11906 Set the maximum size of the address (in bits) in a memory packet."), _("\
11907 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
11909 NULL
, /* FIXME: i18n: */
11910 &setlist
, &showlist
);
11912 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
11913 "X", "binary-download", 1);
11915 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
11916 "vCont", "verbose-resume", 0);
11918 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
11919 "QPassSignals", "pass-signals", 0);
11921 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QProgramSignals
],
11922 "QProgramSignals", "program-signals", 0);
11924 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
11925 "qSymbol", "symbol-lookup", 0);
11927 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
11928 "P", "set-register", 1);
11930 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
11931 "p", "fetch-register", 1);
11933 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
11934 "Z0", "software-breakpoint", 0);
11936 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
11937 "Z1", "hardware-breakpoint", 0);
11939 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
11940 "Z2", "write-watchpoint", 0);
11942 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
11943 "Z3", "read-watchpoint", 0);
11945 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
11946 "Z4", "access-watchpoint", 0);
11948 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
11949 "qXfer:auxv:read", "read-aux-vector", 0);
11951 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
11952 "qXfer:features:read", "target-features", 0);
11954 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
11955 "qXfer:libraries:read", "library-info", 0);
11957 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries_svr4
],
11958 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
11960 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
11961 "qXfer:memory-map:read", "memory-map", 0);
11963 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
11964 "qXfer:spu:read", "read-spu-object", 0);
11966 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
11967 "qXfer:spu:write", "write-spu-object", 0);
11969 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
11970 "qXfer:osdata:read", "osdata", 0);
11972 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_threads
],
11973 "qXfer:threads:read", "threads", 0);
11975 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_read
],
11976 "qXfer:siginfo:read", "read-siginfo-object", 0);
11978 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_write
],
11979 "qXfer:siginfo:write", "write-siginfo-object", 0);
11981 add_packet_config_cmd
11982 (&remote_protocol_packets
[PACKET_qXfer_traceframe_info
],
11983 "qXfer:traceframe-info:read", "traceframe-info", 0);
11985 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_uib
],
11986 "qXfer:uib:read", "unwind-info-block", 0);
11988 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
11989 "qGetTLSAddr", "get-thread-local-storage-address",
11992 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTIBAddr
],
11993 "qGetTIBAddr", "get-thread-information-block-address",
11996 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bc
],
11997 "bc", "reverse-continue", 0);
11999 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bs
],
12000 "bs", "reverse-step", 0);
12002 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
12003 "qSupported", "supported-packets", 0);
12005 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
12006 "qSearch:memory", "search-memory", 0);
12008 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qTStatus
],
12009 "qTStatus", "trace-status", 0);
12011 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
12012 "vFile:open", "hostio-open", 0);
12014 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
12015 "vFile:pread", "hostio-pread", 0);
12017 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
12018 "vFile:pwrite", "hostio-pwrite", 0);
12020 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
12021 "vFile:close", "hostio-close", 0);
12023 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
12024 "vFile:unlink", "hostio-unlink", 0);
12026 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_readlink
],
12027 "vFile:readlink", "hostio-readlink", 0);
12029 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
12030 "vAttach", "attach", 0);
12032 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
12035 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
12036 "QStartNoAckMode", "noack", 0);
12038 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
12039 "vKill", "kill", 0);
12041 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qAttached
],
12042 "qAttached", "query-attached", 0);
12044 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalTracepoints
],
12045 "ConditionalTracepoints",
12046 "conditional-tracepoints", 0);
12048 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalBreakpoints
],
12049 "ConditionalBreakpoints",
12050 "conditional-breakpoints", 0);
12052 add_packet_config_cmd (&remote_protocol_packets
[PACKET_BreakpointCommands
],
12053 "BreakpointCommands",
12054 "breakpoint-commands", 0);
12056 add_packet_config_cmd (&remote_protocol_packets
[PACKET_FastTracepoints
],
12057 "FastTracepoints", "fast-tracepoints", 0);
12059 add_packet_config_cmd (&remote_protocol_packets
[PACKET_TracepointSource
],
12060 "TracepointSource", "TracepointSource", 0);
12062 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAllow
],
12063 "QAllow", "allow", 0);
12065 add_packet_config_cmd (&remote_protocol_packets
[PACKET_StaticTracepoints
],
12066 "StaticTracepoints", "static-tracepoints", 0);
12068 add_packet_config_cmd (&remote_protocol_packets
[PACKET_InstallInTrace
],
12069 "InstallInTrace", "install-in-trace", 0);
12071 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_statictrace_read
],
12072 "qXfer:statictrace:read", "read-sdata-object", 0);
12074 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_fdpic
],
12075 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12077 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QDisableRandomization
],
12078 "QDisableRandomization", "disable-randomization", 0);
12080 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAgent
],
12081 "QAgent", "agent", 0);
12083 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QTBuffer_size
],
12084 "QTBuffer:size", "trace-buffer-size", 0);
12086 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_off
],
12087 "Qbtrace:off", "disable-btrace", 0);
12089 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_bts
],
12090 "Qbtrace:bts", "enable-btrace", 0);
12092 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_btrace
],
12093 "qXfer:btrace", "read-btrace", 0);
12095 /* Keep the old ``set remote Z-packet ...'' working. Each individual
12096 Z sub-packet has its own set and show commands, but users may
12097 have sets to this variable in their .gdbinit files (or in their
12099 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
12100 &remote_Z_packet_detect
, _("\
12101 Set use of remote protocol `Z' packets"), _("\
12102 Show use of remote protocol `Z' packets "), _("\
12103 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
12105 set_remote_protocol_Z_packet_cmd
,
12106 show_remote_protocol_Z_packet_cmd
,
12107 /* FIXME: i18n: Use of remote protocol
12108 `Z' packets is %s. */
12109 &remote_set_cmdlist
, &remote_show_cmdlist
);
12111 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
12112 Manipulate files on the remote system\n\
12113 Transfer files to and from the remote target system."),
12114 &remote_cmdlist
, "remote ",
12115 0 /* allow-unknown */, &cmdlist
);
12117 add_cmd ("put", class_files
, remote_put_command
,
12118 _("Copy a local file to the remote system."),
12121 add_cmd ("get", class_files
, remote_get_command
,
12122 _("Copy a remote file to the local system."),
12125 add_cmd ("delete", class_files
, remote_delete_command
,
12126 _("Delete a remote file."),
12129 remote_exec_file
= xstrdup ("");
12130 add_setshow_string_noescape_cmd ("exec-file", class_files
,
12131 &remote_exec_file
, _("\
12132 Set the remote pathname for \"run\""), _("\
12133 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
12134 &remote_set_cmdlist
, &remote_show_cmdlist
);
12136 add_setshow_boolean_cmd ("range-stepping", class_run
,
12137 &use_range_stepping
, _("\
12138 Enable or disable range stepping."), _("\
12139 Show whether target-assisted range stepping is enabled."), _("\
12140 If on, and the target supports it, when stepping a source line, GDB\n\
12141 tells the target to step the corresponding range of addresses itself instead\n\
12142 of issuing multiple single-steps. This speeds up source level\n\
12143 stepping. If off, GDB always issues single-steps, even if range\n\
12144 stepping is supported by the target. The default is on."),
12145 set_range_stepping
,
12146 show_range_stepping
,
12150 /* Eventually initialize fileio. See fileio.c */
12151 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
12153 /* Take advantage of the fact that the LWP field is not used, to tag
12154 special ptids with it set to != 0. */
12155 magic_null_ptid
= ptid_build (42000, 1, -1);
12156 not_sent_ptid
= ptid_build (42000, 1, -2);
12157 any_thread_ptid
= ptid_build (42000, 1, 0);
12159 target_buf_size
= 2048;
12160 target_buf
= xmalloc (target_buf_size
);