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 (void);
202 static int remote_get_trace_status (struct target_ops
*self
,
203 struct trace_status
*ts
);
205 static int remote_upload_tracepoints (struct uploaded_tp
**utpp
);
207 static int remote_upload_trace_state_variables (struct uploaded_tsv
**utsvp
);
209 static void remote_query_supported (void);
211 static void remote_check_symbols (void);
213 void _initialize_remote (void);
216 static void stop_reply_xfree (struct stop_reply
*);
217 static void remote_parse_stop_reply (char *, struct stop_reply
*);
218 static void push_stop_reply (struct stop_reply
*);
219 static void discard_pending_stop_replies_in_queue (struct remote_state
*);
220 static int peek_stop_reply (ptid_t ptid
);
222 static void remote_async_inferior_event_handler (gdb_client_data
);
224 static void remote_terminal_ours (struct target_ops
*self
);
226 static int remote_read_description_p (struct target_ops
*target
);
228 static void remote_console_output (char *msg
);
230 static int remote_supports_cond_breakpoints (struct target_ops
*self
);
232 static int remote_can_run_breakpoint_commands (struct target_ops
*self
);
236 static struct cmd_list_element
*remote_cmdlist
;
238 /* For "set remote" and "show remote". */
240 static struct cmd_list_element
*remote_set_cmdlist
;
241 static struct cmd_list_element
*remote_show_cmdlist
;
243 /* Stub vCont actions support.
245 Each field is a boolean flag indicating whether the stub reports
246 support for the corresponding action. */
248 struct vCont_action_support
257 /* Controls whether GDB is willing to use range stepping. */
259 static int use_range_stepping
= 1;
261 #define OPAQUETHREADBYTES 8
263 /* a 64 bit opaque identifier */
264 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
266 /* About this many threadisds fit in a packet. */
268 #define MAXTHREADLISTRESULTS 32
270 /* Description of the remote protocol state for the currently
271 connected target. This is per-target state, and independent of the
272 selected architecture. */
276 /* A buffer to use for incoming packets, and its current size. The
277 buffer is grown dynamically for larger incoming packets.
278 Outgoing packets may also be constructed in this buffer.
279 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
280 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
285 /* True if we're going through initial connection setup (finding out
286 about the remote side's threads, relocating symbols, etc.). */
289 /* If we negotiated packet size explicitly (and thus can bypass
290 heuristics for the largest packet size that will not overflow
291 a buffer in the stub), this will be set to that packet size.
292 Otherwise zero, meaning to use the guessed size. */
293 long explicit_packet_size
;
295 /* remote_wait is normally called when the target is running and
296 waits for a stop reply packet. But sometimes we need to call it
297 when the target is already stopped. We can send a "?" packet
298 and have remote_wait read the response. Or, if we already have
299 the response, we can stash it in BUF and tell remote_wait to
300 skip calling getpkt. This flag is set when BUF contains a
301 stop reply packet and the target is not waiting. */
302 int cached_wait_status
;
304 /* True, if in no ack mode. That is, neither GDB nor the stub will
305 expect acks from each other. The connection is assumed to be
309 /* True if we're connected in extended remote mode. */
312 /* True if the stub reported support for multi-process
314 int multi_process_aware
;
316 /* True if we resumed the target and we're waiting for the target to
317 stop. In the mean time, we can't start another command/query.
318 The remote server wouldn't be ready to process it, so we'd
319 timeout waiting for a reply that would never come and eventually
320 we'd close the connection. This can happen in asynchronous mode
321 because we allow GDB commands while the target is running. */
322 int waiting_for_stop_reply
;
324 /* True if the stub reports support for non-stop mode. */
327 /* The status of the stub support for the various vCont actions. */
328 struct vCont_action_support supports_vCont
;
330 /* True if the stub reports support for conditional tracepoints. */
331 int cond_tracepoints
;
333 /* True if the stub reports support for target-side breakpoint
335 int cond_breakpoints
;
337 /* True if the stub reports support for target-side breakpoint
339 int breakpoint_commands
;
341 /* True if the stub reports support for fast tracepoints. */
342 int fast_tracepoints
;
344 /* True if the stub reports support for static tracepoints. */
345 int static_tracepoints
;
347 /* True if the stub reports support for installing tracepoint while
349 int install_in_trace
;
351 /* True if the stub can continue running a trace while GDB is
353 int disconnected_tracing
;
355 /* True if the stub reports support for enabling and disabling
356 tracepoints while a trace experiment is running. */
357 int enable_disable_tracepoints
;
359 /* True if the stub can collect strings using tracenz bytecode. */
362 /* True if the stub supports qXfer:libraries-svr4:read with a
364 int augmented_libraries_svr4_read
;
366 /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
367 responded to that. */
370 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
371 remote_open knows that we don't have a file open when the program
373 struct serial
*remote_desc
;
375 /* These are the threads which we last sent to the remote system. The
376 TID member will be -1 for all or -2 for not sent yet. */
377 ptid_t general_thread
;
378 ptid_t continue_thread
;
380 /* This is the traceframe which we last selected on the remote system.
381 It will be -1 if no traceframe is selected. */
382 int remote_traceframe_number
;
384 char *last_pass_packet
;
386 /* The last QProgramSignals packet sent to the target. We bypass
387 sending a new program signals list down to the target if the new
388 packet is exactly the same as the last we sent. IOW, we only let
389 the target know about program signals list changes. */
390 char *last_program_signals_packet
;
392 enum gdb_signal last_sent_signal
;
396 char *finished_object
;
397 char *finished_annex
;
398 ULONGEST finished_offset
;
400 /* Should we try the 'ThreadInfo' query packet?
402 This variable (NOT available to the user: auto-detect only!)
403 determines whether GDB will use the new, simpler "ThreadInfo"
404 query or the older, more complex syntax for thread queries.
405 This is an auto-detect variable (set to true at each connect,
406 and set to false when the target fails to recognize it). */
407 int use_threadinfo_query
;
408 int use_threadextra_query
;
410 void (*async_client_callback
) (enum inferior_event_type event_type
,
412 void *async_client_context
;
414 /* This is set to the data address of the access causing the target
415 to stop for a watchpoint. */
416 CORE_ADDR remote_watch_data_address
;
418 /* This is non-zero if target stopped for a watchpoint. */
419 int remote_stopped_by_watchpoint_p
;
421 threadref echo_nextthread
;
422 threadref nextthread
;
423 threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
425 /* The state of remote notification. */
426 struct remote_notif_state
*notif_state
;
429 /* Private data that we'll store in (struct thread_info)->private. */
430 struct private_thread_info
437 free_private_thread_info (struct private_thread_info
*info
)
443 /* Returns true if the multi-process extensions are in effect. */
445 remote_multi_process_p (struct remote_state
*rs
)
447 return rs
->multi_process_aware
;
450 /* This data could be associated with a target, but we do not always
451 have access to the current target when we need it, so for now it is
452 static. This will be fine for as long as only one target is in use
454 static struct remote_state
*remote_state
;
456 static struct remote_state
*
457 get_remote_state_raw (void)
462 /* Allocate a new struct remote_state with xmalloc, initialize it, and
465 static struct remote_state
*
466 new_remote_state (void)
468 struct remote_state
*result
= XCNEW (struct remote_state
);
470 /* The default buffer size is unimportant; it will be expanded
471 whenever a larger buffer is needed. */
472 result
->buf_size
= 400;
473 result
->buf
= xmalloc (result
->buf_size
);
474 result
->remote_traceframe_number
= -1;
475 result
->last_sent_signal
= GDB_SIGNAL_0
;
480 /* Description of the remote protocol for a given architecture. */
484 long offset
; /* Offset into G packet. */
485 long regnum
; /* GDB's internal register number. */
486 LONGEST pnum
; /* Remote protocol register number. */
487 int in_g_packet
; /* Always part of G packet. */
488 /* long size in bytes; == register_size (target_gdbarch (), regnum);
490 /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
494 struct remote_arch_state
496 /* Description of the remote protocol registers. */
497 long sizeof_g_packet
;
499 /* Description of the remote protocol registers indexed by REGNUM
500 (making an array gdbarch_num_regs in size). */
501 struct packet_reg
*regs
;
503 /* This is the size (in chars) of the first response to the ``g''
504 packet. It is used as a heuristic when determining the maximum
505 size of memory-read and memory-write packets. A target will
506 typically only reserve a buffer large enough to hold the ``g''
507 packet. The size does not include packet overhead (headers and
509 long actual_register_packet_size
;
511 /* This is the maximum size (in chars) of a non read/write packet.
512 It is also used as a cap on the size of read/write packets. */
513 long remote_packet_size
;
516 /* Utility: generate error from an incoming stub packet. */
518 trace_error (char *buf
)
521 return; /* not an error msg */
524 case '1': /* malformed packet error */
525 if (*++buf
== '0') /* general case: */
526 error (_("remote.c: error in outgoing packet."));
528 error (_("remote.c: error in outgoing packet at field #%ld."),
529 strtol (buf
, NULL
, 16));
531 error (_("Target returns error code '%s'."), buf
);
535 /* Utility: wait for reply from stub, while accepting "O" packets. */
537 remote_get_noisy_reply (char **buf_p
,
540 do /* Loop on reply from remote stub. */
544 QUIT
; /* Allow user to bail out with ^C. */
545 getpkt (buf_p
, sizeof_buf
, 0);
549 else if (strncmp (buf
, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
552 CORE_ADDR from
, to
, org_to
;
554 int adjusted_size
= 0;
555 volatile struct gdb_exception ex
;
557 p
= buf
+ strlen ("qRelocInsn:");
558 pp
= unpack_varlen_hex (p
, &ul
);
560 error (_("invalid qRelocInsn packet: %s"), buf
);
564 unpack_varlen_hex (p
, &ul
);
569 TRY_CATCH (ex
, RETURN_MASK_ALL
)
571 gdbarch_relocate_instruction (target_gdbarch (), &to
, from
);
575 adjusted_size
= to
- org_to
;
577 xsnprintf (buf
, *sizeof_buf
, "qRelocInsn:%x", adjusted_size
);
580 else if (ex
.reason
< 0 && ex
.error
== MEMORY_ERROR
)
582 /* Propagate memory errors silently back to the target.
583 The stub may have limited the range of addresses we
584 can write to, for example. */
589 /* Something unexpectedly bad happened. Be verbose so
590 we can tell what, and propagate the error back to the
591 stub, so it doesn't get stuck waiting for a
593 exception_fprintf (gdb_stderr
, ex
,
594 _("warning: relocating instruction: "));
598 else if (buf
[0] == 'O' && buf
[1] != 'K')
599 remote_console_output (buf
+ 1); /* 'O' message from stub */
601 return buf
; /* Here's the actual reply. */
606 /* Handle for retreving the remote protocol data from gdbarch. */
607 static struct gdbarch_data
*remote_gdbarch_data_handle
;
609 static struct remote_arch_state
*
610 get_remote_arch_state (void)
612 return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle
);
615 /* Fetch the global remote target state. */
617 static struct remote_state
*
618 get_remote_state (void)
620 /* Make sure that the remote architecture state has been
621 initialized, because doing so might reallocate rs->buf. Any
622 function which calls getpkt also needs to be mindful of changes
623 to rs->buf, but this call limits the number of places which run
625 get_remote_arch_state ();
627 return get_remote_state_raw ();
631 compare_pnums (const void *lhs_
, const void *rhs_
)
633 const struct packet_reg
* const *lhs
= lhs_
;
634 const struct packet_reg
* const *rhs
= rhs_
;
636 if ((*lhs
)->pnum
< (*rhs
)->pnum
)
638 else if ((*lhs
)->pnum
== (*rhs
)->pnum
)
645 map_regcache_remote_table (struct gdbarch
*gdbarch
, struct packet_reg
*regs
)
647 int regnum
, num_remote_regs
, offset
;
648 struct packet_reg
**remote_regs
;
650 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
652 struct packet_reg
*r
= ®s
[regnum
];
654 if (register_size (gdbarch
, regnum
) == 0)
655 /* Do not try to fetch zero-sized (placeholder) registers. */
658 r
->pnum
= gdbarch_remote_register_number (gdbarch
, regnum
);
663 /* Define the g/G packet format as the contents of each register
664 with a remote protocol number, in order of ascending protocol
667 remote_regs
= alloca (gdbarch_num_regs (gdbarch
)
668 * sizeof (struct packet_reg
*));
669 for (num_remote_regs
= 0, regnum
= 0;
670 regnum
< gdbarch_num_regs (gdbarch
);
672 if (regs
[regnum
].pnum
!= -1)
673 remote_regs
[num_remote_regs
++] = ®s
[regnum
];
675 qsort (remote_regs
, num_remote_regs
, sizeof (struct packet_reg
*),
678 for (regnum
= 0, offset
= 0; regnum
< num_remote_regs
; regnum
++)
680 remote_regs
[regnum
]->in_g_packet
= 1;
681 remote_regs
[regnum
]->offset
= offset
;
682 offset
+= register_size (gdbarch
, remote_regs
[regnum
]->regnum
);
688 /* Given the architecture described by GDBARCH, return the remote
689 protocol register's number and the register's offset in the g/G
690 packets of GDB register REGNUM, in PNUM and POFFSET respectively.
691 If the target does not have a mapping for REGNUM, return false,
692 otherwise, return true. */
695 remote_register_number_and_offset (struct gdbarch
*gdbarch
, int regnum
,
696 int *pnum
, int *poffset
)
699 struct packet_reg
*regs
;
700 struct cleanup
*old_chain
;
702 gdb_assert (regnum
< gdbarch_num_regs (gdbarch
));
704 regs
= xcalloc (gdbarch_num_regs (gdbarch
), sizeof (struct packet_reg
));
705 old_chain
= make_cleanup (xfree
, regs
);
707 sizeof_g_packet
= map_regcache_remote_table (gdbarch
, regs
);
709 *pnum
= regs
[regnum
].pnum
;
710 *poffset
= regs
[regnum
].offset
;
712 do_cleanups (old_chain
);
718 init_remote_state (struct gdbarch
*gdbarch
)
720 struct remote_state
*rs
= get_remote_state_raw ();
721 struct remote_arch_state
*rsa
;
723 rsa
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct remote_arch_state
);
725 /* Use the architecture to build a regnum<->pnum table, which will be
726 1:1 unless a feature set specifies otherwise. */
727 rsa
->regs
= GDBARCH_OBSTACK_CALLOC (gdbarch
,
728 gdbarch_num_regs (gdbarch
),
731 /* Record the maximum possible size of the g packet - it may turn out
733 rsa
->sizeof_g_packet
= map_regcache_remote_table (gdbarch
, rsa
->regs
);
735 /* Default maximum number of characters in a packet body. Many
736 remote stubs have a hardwired buffer size of 400 bytes
737 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
738 as the maximum packet-size to ensure that the packet and an extra
739 NUL character can always fit in the buffer. This stops GDB
740 trashing stubs that try to squeeze an extra NUL into what is
741 already a full buffer (As of 1999-12-04 that was most stubs). */
742 rsa
->remote_packet_size
= 400 - 1;
744 /* This one is filled in when a ``g'' packet is received. */
745 rsa
->actual_register_packet_size
= 0;
747 /* Should rsa->sizeof_g_packet needs more space than the
748 default, adjust the size accordingly. Remember that each byte is
749 encoded as two characters. 32 is the overhead for the packet
750 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
751 (``$NN:G...#NN'') is a better guess, the below has been padded a
753 if (rsa
->sizeof_g_packet
> ((rsa
->remote_packet_size
- 32) / 2))
754 rsa
->remote_packet_size
= (rsa
->sizeof_g_packet
* 2 + 32);
756 /* Make sure that the packet buffer is plenty big enough for
757 this architecture. */
758 if (rs
->buf_size
< rsa
->remote_packet_size
)
760 rs
->buf_size
= 2 * rsa
->remote_packet_size
;
761 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
767 /* Return the current allowed size of a remote packet. This is
768 inferred from the current architecture, and should be used to
769 limit the length of outgoing packets. */
771 get_remote_packet_size (void)
773 struct remote_state
*rs
= get_remote_state ();
774 struct remote_arch_state
*rsa
= get_remote_arch_state ();
776 if (rs
->explicit_packet_size
)
777 return rs
->explicit_packet_size
;
779 return rsa
->remote_packet_size
;
782 static struct packet_reg
*
783 packet_reg_from_regnum (struct remote_arch_state
*rsa
, long regnum
)
785 if (regnum
< 0 && regnum
>= gdbarch_num_regs (target_gdbarch ()))
789 struct packet_reg
*r
= &rsa
->regs
[regnum
];
791 gdb_assert (r
->regnum
== regnum
);
796 static struct packet_reg
*
797 packet_reg_from_pnum (struct remote_arch_state
*rsa
, LONGEST pnum
)
801 for (i
= 0; i
< gdbarch_num_regs (target_gdbarch ()); i
++)
803 struct packet_reg
*r
= &rsa
->regs
[i
];
811 static struct target_ops remote_ops
;
813 static struct target_ops extended_remote_ops
;
815 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
816 ``forever'' still use the normal timeout mechanism. This is
817 currently used by the ASYNC code to guarentee that target reads
818 during the initial connect always time-out. Once getpkt has been
819 modified to return a timeout indication and, in turn
820 remote_wait()/wait_for_inferior() have gained a timeout parameter
822 static int wait_forever_enabled_p
= 1;
824 /* Allow the user to specify what sequence to send to the remote
825 when he requests a program interruption: Although ^C is usually
826 what remote systems expect (this is the default, here), it is
827 sometimes preferable to send a break. On other systems such
828 as the Linux kernel, a break followed by g, which is Magic SysRq g
829 is required in order to interrupt the execution. */
830 const char interrupt_sequence_control_c
[] = "Ctrl-C";
831 const char interrupt_sequence_break
[] = "BREAK";
832 const char interrupt_sequence_break_g
[] = "BREAK-g";
833 static const char *const interrupt_sequence_modes
[] =
835 interrupt_sequence_control_c
,
836 interrupt_sequence_break
,
837 interrupt_sequence_break_g
,
840 static const char *interrupt_sequence_mode
= interrupt_sequence_control_c
;
843 show_interrupt_sequence (struct ui_file
*file
, int from_tty
,
844 struct cmd_list_element
*c
,
847 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
848 fprintf_filtered (file
,
849 _("Send the ASCII ETX character (Ctrl-c) "
850 "to the remote target to interrupt the "
851 "execution of the program.\n"));
852 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
853 fprintf_filtered (file
,
854 _("send a break signal to the remote target "
855 "to interrupt the execution of the program.\n"));
856 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
857 fprintf_filtered (file
,
858 _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
859 "the remote target to interrupt the execution "
860 "of Linux kernel.\n"));
862 internal_error (__FILE__
, __LINE__
,
863 _("Invalid value for interrupt_sequence_mode: %s."),
864 interrupt_sequence_mode
);
867 /* This boolean variable specifies whether interrupt_sequence is sent
868 to the remote target when gdb connects to it.
869 This is mostly needed when you debug the Linux kernel: The Linux kernel
870 expects BREAK g which is Magic SysRq g for connecting gdb. */
871 static int interrupt_on_connect
= 0;
873 /* This variable is used to implement the "set/show remotebreak" commands.
874 Since these commands are now deprecated in favor of "set/show remote
875 interrupt-sequence", it no longer has any effect on the code. */
876 static int remote_break
;
879 set_remotebreak (char *args
, int from_tty
, struct cmd_list_element
*c
)
882 interrupt_sequence_mode
= interrupt_sequence_break
;
884 interrupt_sequence_mode
= interrupt_sequence_control_c
;
888 show_remotebreak (struct ui_file
*file
, int from_tty
,
889 struct cmd_list_element
*c
,
894 /* This variable sets the number of bits in an address that are to be
895 sent in a memory ("M" or "m") packet. Normally, after stripping
896 leading zeros, the entire address would be sent. This variable
897 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
898 initial implementation of remote.c restricted the address sent in
899 memory packets to ``host::sizeof long'' bytes - (typically 32
900 bits). Consequently, for 64 bit targets, the upper 32 bits of an
901 address was never sent. Since fixing this bug may cause a break in
902 some remote targets this variable is principly provided to
903 facilitate backward compatibility. */
905 static unsigned int remote_address_size
;
907 /* Temporary to track who currently owns the terminal. See
908 remote_terminal_* for more details. */
910 static int remote_async_terminal_ours_p
;
912 /* The executable file to use for "run" on the remote side. */
914 static char *remote_exec_file
= "";
917 /* User configurable variables for the number of characters in a
918 memory read/write packet. MIN (rsa->remote_packet_size,
919 rsa->sizeof_g_packet) is the default. Some targets need smaller
920 values (fifo overruns, et.al.) and some users need larger values
921 (speed up transfers). The variables ``preferred_*'' (the user
922 request), ``current_*'' (what was actually set) and ``forced_*''
923 (Positive - a soft limit, negative - a hard limit). */
925 struct memory_packet_config
932 /* Compute the current size of a read/write packet. Since this makes
933 use of ``actual_register_packet_size'' the computation is dynamic. */
936 get_memory_packet_size (struct memory_packet_config
*config
)
938 struct remote_state
*rs
= get_remote_state ();
939 struct remote_arch_state
*rsa
= get_remote_arch_state ();
941 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
942 law?) that some hosts don't cope very well with large alloca()
943 calls. Eventually the alloca() code will be replaced by calls to
944 xmalloc() and make_cleanups() allowing this restriction to either
945 be lifted or removed. */
946 #ifndef MAX_REMOTE_PACKET_SIZE
947 #define MAX_REMOTE_PACKET_SIZE 16384
949 /* NOTE: 20 ensures we can write at least one byte. */
950 #ifndef MIN_REMOTE_PACKET_SIZE
951 #define MIN_REMOTE_PACKET_SIZE 20
956 if (config
->size
<= 0)
957 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
959 what_they_get
= config
->size
;
963 what_they_get
= get_remote_packet_size ();
964 /* Limit the packet to the size specified by the user. */
966 && what_they_get
> config
->size
)
967 what_they_get
= config
->size
;
969 /* Limit it to the size of the targets ``g'' response unless we have
970 permission from the stub to use a larger packet size. */
971 if (rs
->explicit_packet_size
== 0
972 && rsa
->actual_register_packet_size
> 0
973 && what_they_get
> rsa
->actual_register_packet_size
)
974 what_they_get
= rsa
->actual_register_packet_size
;
976 if (what_they_get
> MAX_REMOTE_PACKET_SIZE
)
977 what_they_get
= MAX_REMOTE_PACKET_SIZE
;
978 if (what_they_get
< MIN_REMOTE_PACKET_SIZE
)
979 what_they_get
= MIN_REMOTE_PACKET_SIZE
;
981 /* Make sure there is room in the global buffer for this packet
982 (including its trailing NUL byte). */
983 if (rs
->buf_size
< what_they_get
+ 1)
985 rs
->buf_size
= 2 * what_they_get
;
986 rs
->buf
= xrealloc (rs
->buf
, 2 * what_they_get
);
989 return what_they_get
;
992 /* Update the size of a read/write packet. If they user wants
993 something really big then do a sanity check. */
996 set_memory_packet_size (char *args
, struct memory_packet_config
*config
)
998 int fixed_p
= config
->fixed_p
;
999 long size
= config
->size
;
1002 error (_("Argument required (integer, `fixed' or `limited')."));
1003 else if (strcmp (args
, "hard") == 0
1004 || strcmp (args
, "fixed") == 0)
1006 else if (strcmp (args
, "soft") == 0
1007 || strcmp (args
, "limit") == 0)
1013 size
= strtoul (args
, &end
, 0);
1015 error (_("Invalid %s (bad syntax)."), config
->name
);
1017 /* Instead of explicitly capping the size of a packet to
1018 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
1019 instead allowed to set the size to something arbitrarily
1021 if (size
> MAX_REMOTE_PACKET_SIZE
)
1022 error (_("Invalid %s (too large)."), config
->name
);
1026 if (fixed_p
&& !config
->fixed_p
)
1028 if (! query (_("The target may not be able to correctly handle a %s\n"
1029 "of %ld bytes. Change the packet size? "),
1030 config
->name
, size
))
1031 error (_("Packet size not changed."));
1033 /* Update the config. */
1034 config
->fixed_p
= fixed_p
;
1035 config
->size
= size
;
1039 show_memory_packet_size (struct memory_packet_config
*config
)
1041 printf_filtered (_("The %s is %ld. "), config
->name
, config
->size
);
1042 if (config
->fixed_p
)
1043 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
1044 get_memory_packet_size (config
));
1046 printf_filtered (_("Packets are limited to %ld bytes.\n"),
1047 get_memory_packet_size (config
));
1050 static struct memory_packet_config memory_write_packet_config
=
1052 "memory-write-packet-size",
1056 set_memory_write_packet_size (char *args
, int from_tty
)
1058 set_memory_packet_size (args
, &memory_write_packet_config
);
1062 show_memory_write_packet_size (char *args
, int from_tty
)
1064 show_memory_packet_size (&memory_write_packet_config
);
1068 get_memory_write_packet_size (void)
1070 return get_memory_packet_size (&memory_write_packet_config
);
1073 static struct memory_packet_config memory_read_packet_config
=
1075 "memory-read-packet-size",
1079 set_memory_read_packet_size (char *args
, int from_tty
)
1081 set_memory_packet_size (args
, &memory_read_packet_config
);
1085 show_memory_read_packet_size (char *args
, int from_tty
)
1087 show_memory_packet_size (&memory_read_packet_config
);
1091 get_memory_read_packet_size (void)
1093 long size
= get_memory_packet_size (&memory_read_packet_config
);
1095 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
1096 extra buffer size argument before the memory read size can be
1097 increased beyond this. */
1098 if (size
> get_remote_packet_size ())
1099 size
= get_remote_packet_size ();
1104 /* Generic configuration support for packets the stub optionally
1105 supports. Allows the user to specify the use of the packet as well
1106 as allowing GDB to auto-detect support in the remote stub. */
1110 PACKET_SUPPORT_UNKNOWN
= 0,
1115 struct packet_config
1119 enum auto_boolean detect
;
1120 enum packet_support support
;
1123 /* Analyze a packet's return value and update the packet config
1134 update_packet_config (struct packet_config
*config
)
1136 switch (config
->detect
)
1138 case AUTO_BOOLEAN_TRUE
:
1139 config
->support
= PACKET_ENABLE
;
1141 case AUTO_BOOLEAN_FALSE
:
1142 config
->support
= PACKET_DISABLE
;
1144 case AUTO_BOOLEAN_AUTO
:
1145 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1151 show_packet_config_cmd (struct packet_config
*config
)
1153 char *support
= "internal-error";
1155 switch (config
->support
)
1158 support
= "enabled";
1160 case PACKET_DISABLE
:
1161 support
= "disabled";
1163 case PACKET_SUPPORT_UNKNOWN
:
1164 support
= "unknown";
1167 switch (config
->detect
)
1169 case AUTO_BOOLEAN_AUTO
:
1170 printf_filtered (_("Support for the `%s' packet "
1171 "is auto-detected, currently %s.\n"),
1172 config
->name
, support
);
1174 case AUTO_BOOLEAN_TRUE
:
1175 case AUTO_BOOLEAN_FALSE
:
1176 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
1177 config
->name
, support
);
1183 add_packet_config_cmd (struct packet_config
*config
, const char *name
,
1184 const char *title
, int legacy
)
1190 config
->name
= name
;
1191 config
->title
= title
;
1192 config
->detect
= AUTO_BOOLEAN_AUTO
;
1193 config
->support
= PACKET_SUPPORT_UNKNOWN
;
1194 set_doc
= xstrprintf ("Set use of remote protocol `%s' (%s) packet",
1196 show_doc
= xstrprintf ("Show current use of remote "
1197 "protocol `%s' (%s) packet",
1199 /* set/show TITLE-packet {auto,on,off} */
1200 cmd_name
= xstrprintf ("%s-packet", title
);
1201 add_setshow_auto_boolean_cmd (cmd_name
, class_obscure
,
1202 &config
->detect
, set_doc
,
1203 show_doc
, NULL
, /* help_doc */
1204 set_remote_protocol_packet_cmd
,
1205 show_remote_protocol_packet_cmd
,
1206 &remote_set_cmdlist
, &remote_show_cmdlist
);
1207 /* The command code copies the documentation strings. */
1210 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
1215 legacy_name
= xstrprintf ("%s-packet", name
);
1216 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1217 &remote_set_cmdlist
);
1218 add_alias_cmd (legacy_name
, cmd_name
, class_obscure
, 0,
1219 &remote_show_cmdlist
);
1223 static enum packet_result
1224 packet_check_result (const char *buf
)
1228 /* The stub recognized the packet request. Check that the
1229 operation succeeded. */
1231 && isxdigit (buf
[1]) && isxdigit (buf
[2])
1233 /* "Enn" - definitly an error. */
1234 return PACKET_ERROR
;
1236 /* Always treat "E." as an error. This will be used for
1237 more verbose error messages, such as E.memtypes. */
1238 if (buf
[0] == 'E' && buf
[1] == '.')
1239 return PACKET_ERROR
;
1241 /* The packet may or may not be OK. Just assume it is. */
1245 /* The stub does not support the packet. */
1246 return PACKET_UNKNOWN
;
1249 static enum packet_result
1250 packet_ok (const char *buf
, struct packet_config
*config
)
1252 enum packet_result result
;
1254 result
= packet_check_result (buf
);
1259 /* The stub recognized the packet request. */
1260 switch (config
->support
)
1262 case PACKET_SUPPORT_UNKNOWN
:
1264 fprintf_unfiltered (gdb_stdlog
,
1265 "Packet %s (%s) is supported\n",
1266 config
->name
, config
->title
);
1267 config
->support
= PACKET_ENABLE
;
1269 case PACKET_DISABLE
:
1270 internal_error (__FILE__
, __LINE__
,
1271 _("packet_ok: attempt to use a disabled packet"));
1277 case PACKET_UNKNOWN
:
1278 /* The stub does not support the packet. */
1279 switch (config
->support
)
1282 if (config
->detect
== AUTO_BOOLEAN_AUTO
)
1283 /* If the stub previously indicated that the packet was
1284 supported then there is a protocol error.. */
1285 error (_("Protocol error: %s (%s) conflicting enabled responses."),
1286 config
->name
, config
->title
);
1288 /* The user set it wrong. */
1289 error (_("Enabled packet %s (%s) not recognized by stub"),
1290 config
->name
, config
->title
);
1292 case PACKET_SUPPORT_UNKNOWN
:
1294 fprintf_unfiltered (gdb_stdlog
,
1295 "Packet %s (%s) is NOT supported\n",
1296 config
->name
, config
->title
);
1297 config
->support
= PACKET_DISABLE
;
1299 case PACKET_DISABLE
:
1321 PACKET_vFile_pwrite
,
1323 PACKET_vFile_unlink
,
1324 PACKET_vFile_readlink
,
1326 PACKET_qXfer_features
,
1327 PACKET_qXfer_libraries
,
1328 PACKET_qXfer_libraries_svr4
,
1329 PACKET_qXfer_memory_map
,
1330 PACKET_qXfer_spu_read
,
1331 PACKET_qXfer_spu_write
,
1332 PACKET_qXfer_osdata
,
1333 PACKET_qXfer_threads
,
1334 PACKET_qXfer_statictrace_read
,
1335 PACKET_qXfer_traceframe_info
,
1341 PACKET_QPassSignals
,
1342 PACKET_QProgramSignals
,
1343 PACKET_qSearch_memory
,
1346 PACKET_QStartNoAckMode
,
1348 PACKET_qXfer_siginfo_read
,
1349 PACKET_qXfer_siginfo_write
,
1351 PACKET_ConditionalTracepoints
,
1352 PACKET_ConditionalBreakpoints
,
1353 PACKET_BreakpointCommands
,
1354 PACKET_FastTracepoints
,
1355 PACKET_StaticTracepoints
,
1356 PACKET_InstallInTrace
,
1359 PACKET_TracepointSource
,
1362 PACKET_QDisableRandomization
,
1364 PACKET_QTBuffer_size
,
1367 PACKET_qXfer_btrace
,
1371 static struct packet_config remote_protocol_packets
[PACKET_MAX
];
1374 set_remote_protocol_packet_cmd (char *args
, int from_tty
,
1375 struct cmd_list_element
*c
)
1377 struct packet_config
*packet
;
1379 for (packet
= remote_protocol_packets
;
1380 packet
< &remote_protocol_packets
[PACKET_MAX
];
1383 if (&packet
->detect
== c
->var
)
1385 update_packet_config (packet
);
1389 internal_error (__FILE__
, __LINE__
, _("Could not find config for %s"),
1394 show_remote_protocol_packet_cmd (struct ui_file
*file
, int from_tty
,
1395 struct cmd_list_element
*c
,
1398 struct packet_config
*packet
;
1400 for (packet
= remote_protocol_packets
;
1401 packet
< &remote_protocol_packets
[PACKET_MAX
];
1404 if (&packet
->detect
== c
->var
)
1406 show_packet_config_cmd (packet
);
1410 internal_error (__FILE__
, __LINE__
, _("Could not find config for %s"),
1414 /* Should we try one of the 'Z' requests? */
1418 Z_PACKET_SOFTWARE_BP
,
1419 Z_PACKET_HARDWARE_BP
,
1426 /* For compatibility with older distributions. Provide a ``set remote
1427 Z-packet ...'' command that updates all the Z packet types. */
1429 static enum auto_boolean remote_Z_packet_detect
;
1432 set_remote_protocol_Z_packet_cmd (char *args
, int from_tty
,
1433 struct cmd_list_element
*c
)
1437 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1439 remote_protocol_packets
[PACKET_Z0
+ i
].detect
= remote_Z_packet_detect
;
1440 update_packet_config (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1445 show_remote_protocol_Z_packet_cmd (struct ui_file
*file
, int from_tty
,
1446 struct cmd_list_element
*c
,
1451 for (i
= 0; i
< NR_Z_PACKET_TYPES
; i
++)
1453 show_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
+ i
]);
1457 /* Tokens for use by the asynchronous signal handlers for SIGINT. */
1458 static struct async_signal_handler
*async_sigint_remote_twice_token
;
1459 static struct async_signal_handler
*async_sigint_remote_token
;
1462 /* Asynchronous signal handle registered as event loop source for
1463 when we have pending events ready to be passed to the core. */
1465 static struct async_event_handler
*remote_async_inferior_event_token
;
1469 static ptid_t magic_null_ptid
;
1470 static ptid_t not_sent_ptid
;
1471 static ptid_t any_thread_ptid
;
1473 /* Find out if the stub attached to PID (and hence GDB should offer to
1474 detach instead of killing it when bailing out). */
1477 remote_query_attached (int pid
)
1479 struct remote_state
*rs
= get_remote_state ();
1480 size_t size
= get_remote_packet_size ();
1482 if (remote_protocol_packets
[PACKET_qAttached
].support
== PACKET_DISABLE
)
1485 if (remote_multi_process_p (rs
))
1486 xsnprintf (rs
->buf
, size
, "qAttached:%x", pid
);
1488 xsnprintf (rs
->buf
, size
, "qAttached");
1491 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1493 switch (packet_ok (rs
->buf
,
1494 &remote_protocol_packets
[PACKET_qAttached
]))
1497 if (strcmp (rs
->buf
, "1") == 0)
1501 warning (_("Remote failure reply: %s"), rs
->buf
);
1503 case PACKET_UNKNOWN
:
1510 /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
1511 has been invented by GDB, instead of reported by the target. Since
1512 we can be connected to a remote system before before knowing about
1513 any inferior, mark the target with execution when we find the first
1514 inferior. If ATTACHED is 1, then we had just attached to this
1515 inferior. If it is 0, then we just created this inferior. If it
1516 is -1, then try querying the remote stub to find out if it had
1517 attached to the inferior or not. */
1519 static struct inferior
*
1520 remote_add_inferior (int fake_pid_p
, int pid
, int attached
)
1522 struct inferior
*inf
;
1524 /* Check whether this process we're learning about is to be
1525 considered attached, or if is to be considered to have been
1526 spawned by the stub. */
1528 attached
= remote_query_attached (pid
);
1530 if (gdbarch_has_global_solist (target_gdbarch ()))
1532 /* If the target shares code across all inferiors, then every
1533 attach adds a new inferior. */
1534 inf
= add_inferior (pid
);
1536 /* ... and every inferior is bound to the same program space.
1537 However, each inferior may still have its own address
1539 inf
->aspace
= maybe_new_address_space ();
1540 inf
->pspace
= current_program_space
;
1544 /* In the traditional debugging scenario, there's a 1-1 match
1545 between program/address spaces. We simply bind the inferior
1546 to the program space's address space. */
1547 inf
= current_inferior ();
1548 inferior_appeared (inf
, pid
);
1551 inf
->attach_flag
= attached
;
1552 inf
->fake_pid_p
= fake_pid_p
;
1557 /* Add thread PTID to GDB's thread list. Tag it as executing/running
1558 according to RUNNING. */
1561 remote_add_thread (ptid_t ptid
, int running
)
1563 struct remote_state
*rs
= get_remote_state ();
1565 /* GDB historically didn't pull threads in the initial connection
1566 setup. If the remote target doesn't even have a concept of
1567 threads (e.g., a bare-metal target), even if internally we
1568 consider that a single-threaded target, mentioning a new thread
1569 might be confusing to the user. Be silent then, preserving the
1570 age old behavior. */
1571 if (rs
->starting_up
)
1572 add_thread_silent (ptid
);
1576 set_executing (ptid
, running
);
1577 set_running (ptid
, running
);
1580 /* Come here when we learn about a thread id from the remote target.
1581 It may be the first time we hear about such thread, so take the
1582 opportunity to add it to GDB's thread list. In case this is the
1583 first time we're noticing its corresponding inferior, add it to
1584 GDB's inferior list as well. */
1587 remote_notice_new_inferior (ptid_t currthread
, int running
)
1589 /* If this is a new thread, add it to GDB's thread list.
1590 If we leave it up to WFI to do this, bad things will happen. */
1592 if (in_thread_list (currthread
) && is_exited (currthread
))
1594 /* We're seeing an event on a thread id we knew had exited.
1595 This has to be a new thread reusing the old id. Add it. */
1596 remote_add_thread (currthread
, running
);
1600 if (!in_thread_list (currthread
))
1602 struct inferior
*inf
= NULL
;
1603 int pid
= ptid_get_pid (currthread
);
1605 if (ptid_is_pid (inferior_ptid
)
1606 && pid
== ptid_get_pid (inferior_ptid
))
1608 /* inferior_ptid has no thread member yet. This can happen
1609 with the vAttach -> remote_wait,"TAAthread:" path if the
1610 stub doesn't support qC. This is the first stop reported
1611 after an attach, so this is the main thread. Update the
1612 ptid in the thread list. */
1613 if (in_thread_list (pid_to_ptid (pid
)))
1614 thread_change_ptid (inferior_ptid
, currthread
);
1617 remote_add_thread (currthread
, running
);
1618 inferior_ptid
= currthread
;
1623 if (ptid_equal (magic_null_ptid
, inferior_ptid
))
1625 /* inferior_ptid is not set yet. This can happen with the
1626 vRun -> remote_wait,"TAAthread:" path if the stub
1627 doesn't support qC. This is the first stop reported
1628 after an attach, so this is the main thread. Update the
1629 ptid in the thread list. */
1630 thread_change_ptid (inferior_ptid
, currthread
);
1634 /* When connecting to a target remote, or to a target
1635 extended-remote which already was debugging an inferior, we
1636 may not know about it yet. Add it before adding its child
1637 thread, so notifications are emitted in a sensible order. */
1638 if (!in_inferior_list (ptid_get_pid (currthread
)))
1640 struct remote_state
*rs
= get_remote_state ();
1641 int fake_pid_p
= !remote_multi_process_p (rs
);
1643 inf
= remote_add_inferior (fake_pid_p
,
1644 ptid_get_pid (currthread
), -1);
1647 /* This is really a new thread. Add it. */
1648 remote_add_thread (currthread
, running
);
1650 /* If we found a new inferior, let the common code do whatever
1651 it needs to with it (e.g., read shared libraries, insert
1652 breakpoints), unless we're just setting up an all-stop
1656 struct remote_state
*rs
= get_remote_state ();
1658 if (non_stop
|| !rs
->starting_up
)
1659 notice_new_inferior (currthread
, running
, 0);
1664 /* Return the private thread data, creating it if necessary. */
1666 static struct private_thread_info
*
1667 demand_private_info (ptid_t ptid
)
1669 struct thread_info
*info
= find_thread_ptid (ptid
);
1675 info
->private = xmalloc (sizeof (*(info
->private)));
1676 info
->private_dtor
= free_private_thread_info
;
1677 info
->private->core
= -1;
1678 info
->private->extra
= 0;
1681 return info
->private;
1684 /* Call this function as a result of
1685 1) A halt indication (T packet) containing a thread id
1686 2) A direct query of currthread
1687 3) Successful execution of set thread */
1690 record_currthread (struct remote_state
*rs
, ptid_t currthread
)
1692 rs
->general_thread
= currthread
;
1695 /* If 'QPassSignals' is supported, tell the remote stub what signals
1696 it can simply pass through to the inferior without reporting. */
1699 remote_pass_signals (struct target_ops
*self
,
1700 int numsigs
, unsigned char *pass_signals
)
1702 if (remote_protocol_packets
[PACKET_QPassSignals
].support
!= PACKET_DISABLE
)
1704 char *pass_packet
, *p
;
1706 struct remote_state
*rs
= get_remote_state ();
1708 gdb_assert (numsigs
< 256);
1709 for (i
= 0; i
< numsigs
; i
++)
1711 if (pass_signals
[i
])
1714 pass_packet
= xmalloc (count
* 3 + strlen ("QPassSignals:") + 1);
1715 strcpy (pass_packet
, "QPassSignals:");
1716 p
= pass_packet
+ strlen (pass_packet
);
1717 for (i
= 0; i
< numsigs
; i
++)
1719 if (pass_signals
[i
])
1722 *p
++ = tohex (i
>> 4);
1723 *p
++ = tohex (i
& 15);
1732 if (!rs
->last_pass_packet
|| strcmp (rs
->last_pass_packet
, pass_packet
))
1734 putpkt (pass_packet
);
1735 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1736 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_QPassSignals
]);
1737 if (rs
->last_pass_packet
)
1738 xfree (rs
->last_pass_packet
);
1739 rs
->last_pass_packet
= pass_packet
;
1742 xfree (pass_packet
);
1746 /* If 'QProgramSignals' is supported, tell the remote stub what
1747 signals it should pass through to the inferior when detaching. */
1750 remote_program_signals (struct target_ops
*self
,
1751 int numsigs
, unsigned char *signals
)
1753 if (remote_protocol_packets
[PACKET_QProgramSignals
].support
!= PACKET_DISABLE
)
1757 struct remote_state
*rs
= get_remote_state ();
1759 gdb_assert (numsigs
< 256);
1760 for (i
= 0; i
< numsigs
; i
++)
1765 packet
= xmalloc (count
* 3 + strlen ("QProgramSignals:") + 1);
1766 strcpy (packet
, "QProgramSignals:");
1767 p
= packet
+ strlen (packet
);
1768 for (i
= 0; i
< numsigs
; i
++)
1770 if (signal_pass_state (i
))
1773 *p
++ = tohex (i
>> 4);
1774 *p
++ = tohex (i
& 15);
1783 if (!rs
->last_program_signals_packet
1784 || strcmp (rs
->last_program_signals_packet
, packet
) != 0)
1787 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1788 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_QProgramSignals
]);
1789 xfree (rs
->last_program_signals_packet
);
1790 rs
->last_program_signals_packet
= packet
;
1797 /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
1798 MINUS_ONE_PTID, set the thread to -1, so the stub returns the
1799 thread. If GEN is set, set the general thread, if not, then set
1800 the step/continue thread. */
1802 set_thread (struct ptid ptid
, int gen
)
1804 struct remote_state
*rs
= get_remote_state ();
1805 ptid_t state
= gen
? rs
->general_thread
: rs
->continue_thread
;
1806 char *buf
= rs
->buf
;
1807 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
1809 if (ptid_equal (state
, ptid
))
1813 *buf
++ = gen
? 'g' : 'c';
1814 if (ptid_equal (ptid
, magic_null_ptid
))
1815 xsnprintf (buf
, endbuf
- buf
, "0");
1816 else if (ptid_equal (ptid
, any_thread_ptid
))
1817 xsnprintf (buf
, endbuf
- buf
, "0");
1818 else if (ptid_equal (ptid
, minus_one_ptid
))
1819 xsnprintf (buf
, endbuf
- buf
, "-1");
1821 write_ptid (buf
, endbuf
, ptid
);
1823 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1825 rs
->general_thread
= ptid
;
1827 rs
->continue_thread
= ptid
;
1831 set_general_thread (struct ptid ptid
)
1833 set_thread (ptid
, 1);
1837 set_continue_thread (struct ptid ptid
)
1839 set_thread (ptid
, 0);
1842 /* Change the remote current process. Which thread within the process
1843 ends up selected isn't important, as long as it is the same process
1844 as what INFERIOR_PTID points to.
1846 This comes from that fact that there is no explicit notion of
1847 "selected process" in the protocol. The selected process for
1848 general operations is the process the selected general thread
1852 set_general_process (void)
1854 struct remote_state
*rs
= get_remote_state ();
1856 /* If the remote can't handle multiple processes, don't bother. */
1857 if (!rs
->extended
|| !remote_multi_process_p (rs
))
1860 /* We only need to change the remote current thread if it's pointing
1861 at some other process. */
1862 if (ptid_get_pid (rs
->general_thread
) != ptid_get_pid (inferior_ptid
))
1863 set_general_thread (inferior_ptid
);
1867 /* Return nonzero if the thread PTID is still alive on the remote
1871 remote_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1873 struct remote_state
*rs
= get_remote_state ();
1876 if (ptid_equal (ptid
, magic_null_ptid
))
1877 /* The main thread is always alive. */
1880 if (ptid_get_pid (ptid
) != 0 && ptid_get_tid (ptid
) == 0)
1881 /* The main thread is always alive. This can happen after a
1882 vAttach, if the remote side doesn't support
1887 endp
= rs
->buf
+ get_remote_packet_size ();
1890 write_ptid (p
, endp
, ptid
);
1893 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
1894 return (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K');
1897 /* About these extended threadlist and threadinfo packets. They are
1898 variable length packets but, the fields within them are often fixed
1899 length. They are redundent enough to send over UDP as is the
1900 remote protocol in general. There is a matching unit test module
1903 /* WARNING: This threadref data structure comes from the remote O.S.,
1904 libstub protocol encoding, and remote.c. It is not particularly
1907 /* Right now, the internal structure is int. We want it to be bigger.
1908 Plan to fix this. */
1910 typedef int gdb_threadref
; /* Internal GDB thread reference. */
1912 /* gdb_ext_thread_info is an internal GDB data structure which is
1913 equivalent to the reply of the remote threadinfo packet. */
1915 struct gdb_ext_thread_info
1917 threadref threadid
; /* External form of thread reference. */
1918 int active
; /* Has state interesting to GDB?
1920 char display
[256]; /* Brief state display, name,
1921 blocked/suspended. */
1922 char shortname
[32]; /* To be used to name threads. */
1923 char more_display
[256]; /* Long info, statistics, queue depth,
1927 /* The volume of remote transfers can be limited by submitting
1928 a mask containing bits specifying the desired information.
1929 Use a union of these values as the 'selection' parameter to
1930 get_thread_info. FIXME: Make these TAG names more thread specific. */
1932 #define TAG_THREADID 1
1933 #define TAG_EXISTS 2
1934 #define TAG_DISPLAY 4
1935 #define TAG_THREADNAME 8
1936 #define TAG_MOREDISPLAY 16
1938 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
1940 static char *unpack_nibble (char *buf
, int *val
);
1942 static char *unpack_byte (char *buf
, int *value
);
1944 static char *pack_int (char *buf
, int value
);
1946 static char *unpack_int (char *buf
, int *value
);
1948 static char *unpack_string (char *src
, char *dest
, int length
);
1950 static char *pack_threadid (char *pkt
, threadref
*id
);
1952 static char *unpack_threadid (char *inbuf
, threadref
*id
);
1954 void int_to_threadref (threadref
*id
, int value
);
1956 static int threadref_to_int (threadref
*ref
);
1958 static void copy_threadref (threadref
*dest
, threadref
*src
);
1960 static int threadmatch (threadref
*dest
, threadref
*src
);
1962 static char *pack_threadinfo_request (char *pkt
, int mode
,
1965 static int remote_unpack_thread_info_response (char *pkt
,
1966 threadref
*expectedref
,
1967 struct gdb_ext_thread_info
1971 static int remote_get_threadinfo (threadref
*threadid
,
1972 int fieldset
, /*TAG mask */
1973 struct gdb_ext_thread_info
*info
);
1975 static char *pack_threadlist_request (char *pkt
, int startflag
,
1977 threadref
*nextthread
);
1979 static int parse_threadlist_response (char *pkt
,
1981 threadref
*original_echo
,
1982 threadref
*resultlist
,
1985 static int remote_get_threadlist (int startflag
,
1986 threadref
*nextthread
,
1990 threadref
*threadlist
);
1992 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
1994 static int remote_threadlist_iterator (rmt_thread_action stepfunction
,
1995 void *context
, int looplimit
);
1997 static int remote_newthread_step (threadref
*ref
, void *context
);
2000 /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
2001 buffer we're allowed to write to. Returns
2002 BUF+CHARACTERS_WRITTEN. */
2005 write_ptid (char *buf
, const char *endbuf
, ptid_t ptid
)
2008 struct remote_state
*rs
= get_remote_state ();
2010 if (remote_multi_process_p (rs
))
2012 pid
= ptid_get_pid (ptid
);
2014 buf
+= xsnprintf (buf
, endbuf
- buf
, "p-%x.", -pid
);
2016 buf
+= xsnprintf (buf
, endbuf
- buf
, "p%x.", pid
);
2018 tid
= ptid_get_tid (ptid
);
2020 buf
+= xsnprintf (buf
, endbuf
- buf
, "-%x", -tid
);
2022 buf
+= xsnprintf (buf
, endbuf
- buf
, "%x", tid
);
2027 /* Extract a PTID from BUF. If non-null, OBUF is set to the to one
2028 passed the last parsed char. Returns null_ptid on error. */
2031 read_ptid (char *buf
, char **obuf
)
2035 ULONGEST pid
= 0, tid
= 0;
2039 /* Multi-process ptid. */
2040 pp
= unpack_varlen_hex (p
+ 1, &pid
);
2042 error (_("invalid remote ptid: %s"), p
);
2045 pp
= unpack_varlen_hex (p
+ 1, &tid
);
2048 return ptid_build (pid
, 0, tid
);
2051 /* No multi-process. Just a tid. */
2052 pp
= unpack_varlen_hex (p
, &tid
);
2054 /* Since the stub is not sending a process id, then default to
2055 what's in inferior_ptid, unless it's null at this point. If so,
2056 then since there's no way to know the pid of the reported
2057 threads, use the magic number. */
2058 if (ptid_equal (inferior_ptid
, null_ptid
))
2059 pid
= ptid_get_pid (magic_null_ptid
);
2061 pid
= ptid_get_pid (inferior_ptid
);
2065 return ptid_build (pid
, 0, tid
);
2071 if (ch
>= 'a' && ch
<= 'f')
2072 return ch
- 'a' + 10;
2073 if (ch
>= '0' && ch
<= '9')
2075 if (ch
>= 'A' && ch
<= 'F')
2076 return ch
- 'A' + 10;
2081 stub_unpack_int (char *buff
, int fieldlength
)
2088 nibble
= stubhex (*buff
++);
2092 retval
= retval
<< 4;
2098 unpack_nibble (char *buf
, int *val
)
2100 *val
= fromhex (*buf
++);
2105 unpack_byte (char *buf
, int *value
)
2107 *value
= stub_unpack_int (buf
, 2);
2112 pack_int (char *buf
, int value
)
2114 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
2115 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
2116 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
2117 buf
= pack_hex_byte (buf
, (value
& 0xff));
2122 unpack_int (char *buf
, int *value
)
2124 *value
= stub_unpack_int (buf
, 8);
2128 #if 0 /* Currently unused, uncomment when needed. */
2129 static char *pack_string (char *pkt
, char *string
);
2132 pack_string (char *pkt
, char *string
)
2137 len
= strlen (string
);
2139 len
= 200; /* Bigger than most GDB packets, junk??? */
2140 pkt
= pack_hex_byte (pkt
, len
);
2144 if ((ch
== '\0') || (ch
== '#'))
2145 ch
= '*'; /* Protect encapsulation. */
2150 #endif /* 0 (unused) */
2153 unpack_string (char *src
, char *dest
, int length
)
2162 pack_threadid (char *pkt
, threadref
*id
)
2165 unsigned char *altid
;
2167 altid
= (unsigned char *) id
;
2168 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
2170 pkt
= pack_hex_byte (pkt
, *altid
++);
2176 unpack_threadid (char *inbuf
, threadref
*id
)
2179 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
2182 altref
= (char *) id
;
2184 while (inbuf
< limit
)
2186 x
= stubhex (*inbuf
++);
2187 y
= stubhex (*inbuf
++);
2188 *altref
++ = (x
<< 4) | y
;
2193 /* Externally, threadrefs are 64 bits but internally, they are still
2194 ints. This is due to a mismatch of specifications. We would like
2195 to use 64bit thread references internally. This is an adapter
2199 int_to_threadref (threadref
*id
, int value
)
2201 unsigned char *scan
;
2203 scan
= (unsigned char *) id
;
2209 *scan
++ = (value
>> 24) & 0xff;
2210 *scan
++ = (value
>> 16) & 0xff;
2211 *scan
++ = (value
>> 8) & 0xff;
2212 *scan
++ = (value
& 0xff);
2216 threadref_to_int (threadref
*ref
)
2219 unsigned char *scan
;
2225 value
= (value
<< 8) | ((*scan
++) & 0xff);
2230 copy_threadref (threadref
*dest
, threadref
*src
)
2233 unsigned char *csrc
, *cdest
;
2235 csrc
= (unsigned char *) src
;
2236 cdest
= (unsigned char *) dest
;
2243 threadmatch (threadref
*dest
, threadref
*src
)
2245 /* Things are broken right now, so just assume we got a match. */
2247 unsigned char *srcp
, *destp
;
2249 srcp
= (char *) src
;
2250 destp
= (char *) dest
;
2254 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
2261 threadid:1, # always request threadid
2268 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
2271 pack_threadinfo_request (char *pkt
, int mode
, threadref
*id
)
2273 *pkt
++ = 'q'; /* Info Query */
2274 *pkt
++ = 'P'; /* process or thread info */
2275 pkt
= pack_int (pkt
, mode
); /* mode */
2276 pkt
= pack_threadid (pkt
, id
); /* threadid */
2277 *pkt
= '\0'; /* terminate */
2281 /* These values tag the fields in a thread info response packet. */
2282 /* Tagging the fields allows us to request specific fields and to
2283 add more fields as time goes by. */
2285 #define TAG_THREADID 1 /* Echo the thread identifier. */
2286 #define TAG_EXISTS 2 /* Is this process defined enough to
2287 fetch registers and its stack? */
2288 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
2289 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
2290 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
2294 remote_unpack_thread_info_response (char *pkt
, threadref
*expectedref
,
2295 struct gdb_ext_thread_info
*info
)
2297 struct remote_state
*rs
= get_remote_state ();
2301 char *limit
= pkt
+ rs
->buf_size
; /* Plausible parsing limit. */
2304 /* info->threadid = 0; FIXME: implement zero_threadref. */
2306 info
->display
[0] = '\0';
2307 info
->shortname
[0] = '\0';
2308 info
->more_display
[0] = '\0';
2310 /* Assume the characters indicating the packet type have been
2312 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
2313 pkt
= unpack_threadid (pkt
, &ref
);
2316 warning (_("Incomplete response to threadinfo request."));
2317 if (!threadmatch (&ref
, expectedref
))
2318 { /* This is an answer to a different request. */
2319 warning (_("ERROR RMT Thread info mismatch."));
2322 copy_threadref (&info
->threadid
, &ref
);
2324 /* Loop on tagged fields , try to bail if somthing goes wrong. */
2326 /* Packets are terminated with nulls. */
2327 while ((pkt
< limit
) && mask
&& *pkt
)
2329 pkt
= unpack_int (pkt
, &tag
); /* tag */
2330 pkt
= unpack_byte (pkt
, &length
); /* length */
2331 if (!(tag
& mask
)) /* Tags out of synch with mask. */
2333 warning (_("ERROR RMT: threadinfo tag mismatch."));
2337 if (tag
== TAG_THREADID
)
2341 warning (_("ERROR RMT: length of threadid is not 16."));
2345 pkt
= unpack_threadid (pkt
, &ref
);
2346 mask
= mask
& ~TAG_THREADID
;
2349 if (tag
== TAG_EXISTS
)
2351 info
->active
= stub_unpack_int (pkt
, length
);
2353 mask
= mask
& ~(TAG_EXISTS
);
2356 warning (_("ERROR RMT: 'exists' length too long."));
2362 if (tag
== TAG_THREADNAME
)
2364 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
2365 mask
= mask
& ~TAG_THREADNAME
;
2368 if (tag
== TAG_DISPLAY
)
2370 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
2371 mask
= mask
& ~TAG_DISPLAY
;
2374 if (tag
== TAG_MOREDISPLAY
)
2376 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
2377 mask
= mask
& ~TAG_MOREDISPLAY
;
2380 warning (_("ERROR RMT: unknown thread info tag."));
2381 break; /* Not a tag we know about. */
2387 remote_get_threadinfo (threadref
*threadid
, int fieldset
, /* TAG mask */
2388 struct gdb_ext_thread_info
*info
)
2390 struct remote_state
*rs
= get_remote_state ();
2393 pack_threadinfo_request (rs
->buf
, fieldset
, threadid
);
2395 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2397 if (rs
->buf
[0] == '\0')
2400 result
= remote_unpack_thread_info_response (rs
->buf
+ 2,
2405 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
2408 pack_threadlist_request (char *pkt
, int startflag
, int threadcount
,
2409 threadref
*nextthread
)
2411 *pkt
++ = 'q'; /* info query packet */
2412 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
2413 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
2414 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
2415 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
2420 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
2423 parse_threadlist_response (char *pkt
, int result_limit
,
2424 threadref
*original_echo
, threadref
*resultlist
,
2427 struct remote_state
*rs
= get_remote_state ();
2429 int count
, resultcount
, done
;
2432 /* Assume the 'q' and 'M chars have been stripped. */
2433 limit
= pkt
+ (rs
->buf_size
- BUF_THREAD_ID_SIZE
);
2434 /* done parse past here */
2435 pkt
= unpack_byte (pkt
, &count
); /* count field */
2436 pkt
= unpack_nibble (pkt
, &done
);
2437 /* The first threadid is the argument threadid. */
2438 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
2439 while ((count
-- > 0) && (pkt
< limit
))
2441 pkt
= unpack_threadid (pkt
, resultlist
++);
2442 if (resultcount
++ >= result_limit
)
2451 remote_get_threadlist (int startflag
, threadref
*nextthread
, int result_limit
,
2452 int *done
, int *result_count
, threadref
*threadlist
)
2454 struct remote_state
*rs
= get_remote_state ();
2457 /* Trancate result limit to be smaller than the packet size. */
2458 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10)
2459 >= get_remote_packet_size ())
2460 result_limit
= (get_remote_packet_size () / BUF_THREAD_ID_SIZE
) - 2;
2462 pack_threadlist_request (rs
->buf
, startflag
, result_limit
, nextthread
);
2464 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2466 if (*rs
->buf
== '\0')
2470 parse_threadlist_response (rs
->buf
+ 2, result_limit
,
2471 &rs
->echo_nextthread
, threadlist
, done
);
2473 if (!threadmatch (&rs
->echo_nextthread
, nextthread
))
2475 /* FIXME: This is a good reason to drop the packet. */
2476 /* Possably, there is a duplicate response. */
2478 retransmit immediatly - race conditions
2479 retransmit after timeout - yes
2481 wait for packet, then exit
2483 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
2484 return 0; /* I choose simply exiting. */
2486 if (*result_count
<= 0)
2490 warning (_("RMT ERROR : failed to get remote thread list."));
2493 return result
; /* break; */
2495 if (*result_count
> result_limit
)
2498 warning (_("RMT ERROR: threadlist response longer than requested."));
2504 /* This is the interface between remote and threads, remotes upper
2507 /* remote_find_new_threads retrieves the thread list and for each
2508 thread in the list, looks up the thread in GDB's internal list,
2509 adding the thread if it does not already exist. This involves
2510 getting partial thread lists from the remote target so, polling the
2511 quit_flag is required. */
2515 remote_threadlist_iterator (rmt_thread_action stepfunction
, void *context
,
2518 struct remote_state
*rs
= get_remote_state ();
2519 int done
, i
, result_count
;
2527 if (loopcount
++ > looplimit
)
2530 warning (_("Remote fetch threadlist -infinite loop-."));
2533 if (!remote_get_threadlist (startflag
, &rs
->nextthread
,
2534 MAXTHREADLISTRESULTS
,
2535 &done
, &result_count
, rs
->resultthreadlist
))
2540 /* Clear for later iterations. */
2542 /* Setup to resume next batch of thread references, set nextthread. */
2543 if (result_count
>= 1)
2544 copy_threadref (&rs
->nextthread
,
2545 &rs
->resultthreadlist
[result_count
- 1]);
2547 while (result_count
--)
2548 if (!(result
= (*stepfunction
) (&rs
->resultthreadlist
[i
++], context
)))
2555 remote_newthread_step (threadref
*ref
, void *context
)
2557 int pid
= ptid_get_pid (inferior_ptid
);
2558 ptid_t ptid
= ptid_build (pid
, 0, threadref_to_int (ref
));
2560 if (!in_thread_list (ptid
))
2562 return 1; /* continue iterator */
2565 #define CRAZY_MAX_THREADS 1000
2568 remote_current_thread (ptid_t oldpid
)
2570 struct remote_state
*rs
= get_remote_state ();
2573 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2574 if (rs
->buf
[0] == 'Q' && rs
->buf
[1] == 'C')
2575 return read_ptid (&rs
->buf
[2], NULL
);
2580 /* Find new threads for info threads command.
2581 * Original version, using John Metzler's thread protocol.
2585 remote_find_new_threads (void)
2587 remote_threadlist_iterator (remote_newthread_step
, 0,
2591 #if defined(HAVE_LIBEXPAT)
2593 typedef struct thread_item
2599 DEF_VEC_O(thread_item_t
);
2601 struct threads_parsing_context
2603 VEC (thread_item_t
) *items
;
2607 start_thread (struct gdb_xml_parser
*parser
,
2608 const struct gdb_xml_element
*element
,
2609 void *user_data
, VEC(gdb_xml_value_s
) *attributes
)
2611 struct threads_parsing_context
*data
= user_data
;
2613 struct thread_item item
;
2615 struct gdb_xml_value
*attr
;
2617 id
= xml_find_attribute (attributes
, "id")->value
;
2618 item
.ptid
= read_ptid (id
, NULL
);
2620 attr
= xml_find_attribute (attributes
, "core");
2622 item
.core
= *(ULONGEST
*) attr
->value
;
2628 VEC_safe_push (thread_item_t
, data
->items
, &item
);
2632 end_thread (struct gdb_xml_parser
*parser
,
2633 const struct gdb_xml_element
*element
,
2634 void *user_data
, const char *body_text
)
2636 struct threads_parsing_context
*data
= user_data
;
2638 if (body_text
&& *body_text
)
2639 VEC_last (thread_item_t
, data
->items
)->extra
= xstrdup (body_text
);
2642 const struct gdb_xml_attribute thread_attributes
[] = {
2643 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
2644 { "core", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
2645 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
2648 const struct gdb_xml_element thread_children
[] = {
2649 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2652 const struct gdb_xml_element threads_children
[] = {
2653 { "thread", thread_attributes
, thread_children
,
2654 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
2655 start_thread
, end_thread
},
2656 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2659 const struct gdb_xml_element threads_elements
[] = {
2660 { "threads", NULL
, threads_children
,
2661 GDB_XML_EF_NONE
, NULL
, NULL
},
2662 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
2665 /* Discard the contents of the constructed thread info context. */
2668 clear_threads_parsing_context (void *p
)
2670 struct threads_parsing_context
*context
= p
;
2672 struct thread_item
*item
;
2674 for (i
= 0; VEC_iterate (thread_item_t
, context
->items
, i
, item
); ++i
)
2675 xfree (item
->extra
);
2677 VEC_free (thread_item_t
, context
->items
);
2683 * Find all threads for info threads command.
2684 * Uses new thread protocol contributed by Cisco.
2685 * Falls back and attempts to use the older method (above)
2686 * if the target doesn't respond to the new method.
2690 remote_threads_info (struct target_ops
*ops
)
2692 struct remote_state
*rs
= get_remote_state ();
2696 if (rs
->remote_desc
== 0) /* paranoia */
2697 error (_("Command can only be used when connected to the remote target."));
2699 #if defined(HAVE_LIBEXPAT)
2700 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2702 char *xml
= target_read_stralloc (¤t_target
,
2703 TARGET_OBJECT_THREADS
, NULL
);
2705 struct cleanup
*back_to
= make_cleanup (xfree
, xml
);
2709 struct threads_parsing_context context
;
2711 context
.items
= NULL
;
2712 make_cleanup (clear_threads_parsing_context
, &context
);
2714 if (gdb_xml_parse_quick (_("threads"), "threads.dtd",
2715 threads_elements
, xml
, &context
) == 0)
2718 struct thread_item
*item
;
2721 VEC_iterate (thread_item_t
, context
.items
, i
, item
);
2724 if (!ptid_equal (item
->ptid
, null_ptid
))
2726 struct private_thread_info
*info
;
2727 /* In non-stop mode, we assume new found threads
2728 are running until proven otherwise with a
2729 stop reply. In all-stop, we can only get
2730 here if all threads are stopped. */
2731 int running
= non_stop
? 1 : 0;
2733 remote_notice_new_inferior (item
->ptid
, running
);
2735 info
= demand_private_info (item
->ptid
);
2736 info
->core
= item
->core
;
2737 info
->extra
= item
->extra
;
2744 do_cleanups (back_to
);
2749 if (rs
->use_threadinfo_query
)
2751 putpkt ("qfThreadInfo");
2752 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2754 if (bufp
[0] != '\0') /* q packet recognized */
2756 struct cleanup
*old_chain
;
2759 /* remote_notice_new_inferior (in the loop below) may make
2760 new RSP calls, which clobber rs->buf. Work with a
2762 bufp
= saved_reply
= xstrdup (rs
->buf
);
2763 old_chain
= make_cleanup (free_current_contents
, &saved_reply
);
2765 while (*bufp
++ == 'm') /* reply contains one or more TID */
2769 new_thread
= read_ptid (bufp
, &bufp
);
2770 if (!ptid_equal (new_thread
, null_ptid
))
2772 /* In non-stop mode, we assume new found threads
2773 are running until proven otherwise with a
2774 stop reply. In all-stop, we can only get
2775 here if all threads are stopped. */
2776 int running
= non_stop
? 1 : 0;
2778 remote_notice_new_inferior (new_thread
, running
);
2781 while (*bufp
++ == ','); /* comma-separated list */
2782 free_current_contents (&saved_reply
);
2783 putpkt ("qsThreadInfo");
2784 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2785 bufp
= saved_reply
= xstrdup (rs
->buf
);
2787 do_cleanups (old_chain
);
2792 /* Only qfThreadInfo is supported in non-stop mode. */
2796 /* Else fall back to old method based on jmetzler protocol. */
2797 rs
->use_threadinfo_query
= 0;
2798 remote_find_new_threads ();
2803 * Collect a descriptive string about the given thread.
2804 * The target may say anything it wants to about the thread
2805 * (typically info about its blocked / runnable state, name, etc.).
2806 * This string will appear in the info threads display.
2808 * Optional: targets are not required to implement this function.
2812 remote_threads_extra_info (struct target_ops
*self
, struct thread_info
*tp
)
2814 struct remote_state
*rs
= get_remote_state ();
2818 struct gdb_ext_thread_info threadinfo
;
2819 static char display_buf
[100]; /* arbitrary... */
2820 int n
= 0; /* position in display_buf */
2822 if (rs
->remote_desc
== 0) /* paranoia */
2823 internal_error (__FILE__
, __LINE__
,
2824 _("remote_threads_extra_info"));
2826 if (ptid_equal (tp
->ptid
, magic_null_ptid
)
2827 || (ptid_get_pid (tp
->ptid
) != 0 && ptid_get_tid (tp
->ptid
) == 0))
2828 /* This is the main thread which was added by GDB. The remote
2829 server doesn't know about it. */
2832 if (remote_protocol_packets
[PACKET_qXfer_threads
].support
== PACKET_ENABLE
)
2834 struct thread_info
*info
= find_thread_ptid (tp
->ptid
);
2836 if (info
&& info
->private)
2837 return info
->private->extra
;
2842 if (rs
->use_threadextra_query
)
2845 char *endb
= rs
->buf
+ get_remote_packet_size ();
2847 xsnprintf (b
, endb
- b
, "qThreadExtraInfo,");
2849 write_ptid (b
, endb
, tp
->ptid
);
2852 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2853 if (rs
->buf
[0] != 0)
2855 n
= min (strlen (rs
->buf
) / 2, sizeof (display_buf
));
2856 result
= hex2bin (rs
->buf
, (gdb_byte
*) display_buf
, n
);
2857 display_buf
[result
] = '\0';
2862 /* If the above query fails, fall back to the old method. */
2863 rs
->use_threadextra_query
= 0;
2864 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
2865 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
2866 int_to_threadref (&id
, ptid_get_tid (tp
->ptid
));
2867 if (remote_get_threadinfo (&id
, set
, &threadinfo
))
2868 if (threadinfo
.active
)
2870 if (*threadinfo
.shortname
)
2871 n
+= xsnprintf (&display_buf
[0], sizeof (display_buf
) - n
,
2872 " Name: %s,", threadinfo
.shortname
);
2873 if (*threadinfo
.display
)
2874 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2875 " State: %s,", threadinfo
.display
);
2876 if (*threadinfo
.more_display
)
2877 n
+= xsnprintf (&display_buf
[n
], sizeof (display_buf
) - n
,
2878 " Priority: %s", threadinfo
.more_display
);
2882 /* For purely cosmetic reasons, clear up trailing commas. */
2883 if (',' == display_buf
[n
-1])
2884 display_buf
[n
-1] = ' ';
2893 remote_static_tracepoint_marker_at (CORE_ADDR addr
,
2894 struct static_tracepoint_marker
*marker
)
2896 struct remote_state
*rs
= get_remote_state ();
2899 xsnprintf (p
, get_remote_packet_size (), "qTSTMat:");
2901 p
+= hexnumstr (p
, addr
);
2903 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2907 error (_("Remote failure reply: %s"), p
);
2911 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2918 static VEC(static_tracepoint_marker_p
) *
2919 remote_static_tracepoint_markers_by_strid (const char *strid
)
2921 struct remote_state
*rs
= get_remote_state ();
2922 VEC(static_tracepoint_marker_p
) *markers
= NULL
;
2923 struct static_tracepoint_marker
*marker
= NULL
;
2924 struct cleanup
*old_chain
;
2927 /* Ask for a first packet of static tracepoint marker
2930 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2933 error (_("Remote failure reply: %s"), p
);
2935 old_chain
= make_cleanup (free_current_marker
, &marker
);
2940 marker
= XCNEW (struct static_tracepoint_marker
);
2944 parse_static_tracepoint_marker_definition (p
, &p
, marker
);
2946 if (strid
== NULL
|| strcmp (strid
, marker
->str_id
) == 0)
2948 VEC_safe_push (static_tracepoint_marker_p
,
2954 release_static_tracepoint_marker (marker
);
2955 memset (marker
, 0, sizeof (*marker
));
2958 while (*p
++ == ','); /* comma-separated list */
2959 /* Ask for another packet of static tracepoint definition. */
2961 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
2965 do_cleanups (old_chain
);
2970 /* Implement the to_get_ada_task_ptid function for the remote targets. */
2973 remote_get_ada_task_ptid (struct target_ops
*self
, long lwp
, long thread
)
2975 return ptid_build (ptid_get_pid (inferior_ptid
), 0, lwp
);
2979 /* Restart the remote side; this is an extended protocol operation. */
2982 extended_remote_restart (void)
2984 struct remote_state
*rs
= get_remote_state ();
2986 /* Send the restart command; for reasons I don't understand the
2987 remote side really expects a number after the "R". */
2988 xsnprintf (rs
->buf
, get_remote_packet_size (), "R%x", 0);
2991 remote_fileio_reset ();
2994 /* Clean up connection to a remote debugger. */
2997 remote_close (struct target_ops
*self
)
2999 struct remote_state
*rs
= get_remote_state ();
3001 if (rs
->remote_desc
== NULL
)
3002 return; /* already closed */
3004 /* Make sure we leave stdin registered in the event loop, and we
3005 don't leave the async SIGINT signal handler installed. */
3006 remote_terminal_ours (self
);
3008 serial_close (rs
->remote_desc
);
3009 rs
->remote_desc
= NULL
;
3011 /* We don't have a connection to the remote stub anymore. Get rid
3012 of all the inferiors and their threads we were controlling.
3013 Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3014 will be unable to find the thread corresponding to (pid, 0, 0). */
3015 inferior_ptid
= null_ptid
;
3016 discard_all_inferiors ();
3018 /* We are closing the remote target, so we should discard
3019 everything of this target. */
3020 discard_pending_stop_replies_in_queue (rs
);
3022 if (remote_async_inferior_event_token
)
3023 delete_async_event_handler (&remote_async_inferior_event_token
);
3025 remote_notif_state_xfree (rs
->notif_state
);
3027 trace_reset_local_state ();
3030 /* Query the remote side for the text, data and bss offsets. */
3035 struct remote_state
*rs
= get_remote_state ();
3038 int lose
, num_segments
= 0, do_sections
, do_segments
;
3039 CORE_ADDR text_addr
, data_addr
, bss_addr
, segments
[2];
3040 struct section_offsets
*offs
;
3041 struct symfile_segment_data
*data
;
3043 if (symfile_objfile
== NULL
)
3046 putpkt ("qOffsets");
3047 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3050 if (buf
[0] == '\000')
3051 return; /* Return silently. Stub doesn't support
3055 warning (_("Remote failure reply: %s"), buf
);
3059 /* Pick up each field in turn. This used to be done with scanf, but
3060 scanf will make trouble if CORE_ADDR size doesn't match
3061 conversion directives correctly. The following code will work
3062 with any size of CORE_ADDR. */
3063 text_addr
= data_addr
= bss_addr
= 0;
3067 if (strncmp (ptr
, "Text=", 5) == 0)
3070 /* Don't use strtol, could lose on big values. */
3071 while (*ptr
&& *ptr
!= ';')
3072 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
3074 if (strncmp (ptr
, ";Data=", 6) == 0)
3077 while (*ptr
&& *ptr
!= ';')
3078 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
3083 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
3086 while (*ptr
&& *ptr
!= ';')
3087 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
3089 if (bss_addr
!= data_addr
)
3090 warning (_("Target reported unsupported offsets: %s"), buf
);
3095 else if (strncmp (ptr
, "TextSeg=", 8) == 0)
3098 /* Don't use strtol, could lose on big values. */
3099 while (*ptr
&& *ptr
!= ';')
3100 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
3103 if (strncmp (ptr
, ";DataSeg=", 9) == 0)
3106 while (*ptr
&& *ptr
!= ';')
3107 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
3115 error (_("Malformed response to offset query, %s"), buf
);
3116 else if (*ptr
!= '\0')
3117 warning (_("Target reported unsupported offsets: %s"), buf
);
3119 offs
= ((struct section_offsets
*)
3120 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
)));
3121 memcpy (offs
, symfile_objfile
->section_offsets
,
3122 SIZEOF_N_SECTION_OFFSETS (symfile_objfile
->num_sections
));
3124 data
= get_symfile_segment_data (symfile_objfile
->obfd
);
3125 do_segments
= (data
!= NULL
);
3126 do_sections
= num_segments
== 0;
3128 if (num_segments
> 0)
3130 segments
[0] = text_addr
;
3131 segments
[1] = data_addr
;
3133 /* If we have two segments, we can still try to relocate everything
3134 by assuming that the .text and .data offsets apply to the whole
3135 text and data segments. Convert the offsets given in the packet
3136 to base addresses for symfile_map_offsets_to_segments. */
3137 else if (data
&& data
->num_segments
== 2)
3139 segments
[0] = data
->segment_bases
[0] + text_addr
;
3140 segments
[1] = data
->segment_bases
[1] + data_addr
;
3143 /* If the object file has only one segment, assume that it is text
3144 rather than data; main programs with no writable data are rare,
3145 but programs with no code are useless. Of course the code might
3146 have ended up in the data segment... to detect that we would need
3147 the permissions here. */
3148 else if (data
&& data
->num_segments
== 1)
3150 segments
[0] = data
->segment_bases
[0] + text_addr
;
3153 /* There's no way to relocate by segment. */
3159 int ret
= symfile_map_offsets_to_segments (symfile_objfile
->obfd
, data
,
3160 offs
, num_segments
, segments
);
3162 if (ret
== 0 && !do_sections
)
3163 error (_("Can not handle qOffsets TextSeg "
3164 "response with this symbol file"));
3171 free_symfile_segment_data (data
);
3175 offs
->offsets
[SECT_OFF_TEXT (symfile_objfile
)] = text_addr
;
3177 /* This is a temporary kludge to force data and bss to use the
3178 same offsets because that's what nlmconv does now. The real
3179 solution requires changes to the stub and remote.c that I
3180 don't have time to do right now. */
3182 offs
->offsets
[SECT_OFF_DATA (symfile_objfile
)] = data_addr
;
3183 offs
->offsets
[SECT_OFF_BSS (symfile_objfile
)] = data_addr
;
3186 objfile_relocate (symfile_objfile
, offs
);
3189 /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in
3190 threads we know are stopped already. This is used during the
3191 initial remote connection in non-stop mode --- threads that are
3192 reported as already being stopped are left stopped. */
3195 set_stop_requested_callback (struct thread_info
*thread
, void *data
)
3197 /* If we have a stop reply for this thread, it must be stopped. */
3198 if (peek_stop_reply (thread
->ptid
))
3199 set_stop_requested (thread
->ptid
, 1);
3204 /* Send interrupt_sequence to remote target. */
3206 send_interrupt_sequence (void)
3208 struct remote_state
*rs
= get_remote_state ();
3210 if (interrupt_sequence_mode
== interrupt_sequence_control_c
)
3211 remote_serial_write ("\x03", 1);
3212 else if (interrupt_sequence_mode
== interrupt_sequence_break
)
3213 serial_send_break (rs
->remote_desc
);
3214 else if (interrupt_sequence_mode
== interrupt_sequence_break_g
)
3216 serial_send_break (rs
->remote_desc
);
3217 remote_serial_write ("g", 1);
3220 internal_error (__FILE__
, __LINE__
,
3221 _("Invalid value for interrupt_sequence_mode: %s."),
3222 interrupt_sequence_mode
);
3226 /* If STOP_REPLY is a T stop reply, look for the "thread" register,
3227 and extract the PTID. Returns NULL_PTID if not found. */
3230 stop_reply_extract_thread (char *stop_reply
)
3232 if (stop_reply
[0] == 'T' && strlen (stop_reply
) > 3)
3236 /* Txx r:val ; r:val (...) */
3239 /* Look for "register" named "thread". */
3244 p1
= strchr (p
, ':');
3248 if (strncmp (p
, "thread", p1
- p
) == 0)
3249 return read_ptid (++p1
, &p
);
3251 p1
= strchr (p
, ';');
3263 /* Determine the remote side's current thread. If we have a stop
3264 reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
3265 "thread" register we can extract the current thread from. If not,
3266 ask the remote which is the current thread with qC. The former
3267 method avoids a roundtrip. */
3270 get_current_thread (char *wait_status
)
3274 /* Note we don't use remote_parse_stop_reply as that makes use of
3275 the target architecture, which we haven't yet fully determined at
3277 if (wait_status
!= NULL
)
3278 ptid
= stop_reply_extract_thread (wait_status
);
3279 if (ptid_equal (ptid
, null_ptid
))
3280 ptid
= remote_current_thread (inferior_ptid
);
3285 /* Query the remote target for which is the current thread/process,
3286 add it to our tables, and update INFERIOR_PTID. The caller is
3287 responsible for setting the state such that the remote end is ready
3288 to return the current thread.
3290 This function is called after handling the '?' or 'vRun' packets,
3291 whose response is a stop reply from which we can also try
3292 extracting the thread. If the target doesn't support the explicit
3293 qC query, we infer the current thread from that stop reply, passed
3294 in in WAIT_STATUS, which may be NULL. */
3297 add_current_inferior_and_thread (char *wait_status
)
3299 struct remote_state
*rs
= get_remote_state ();
3301 ptid_t ptid
= null_ptid
;
3303 inferior_ptid
= null_ptid
;
3305 /* Now, if we have thread information, update inferior_ptid. */
3306 ptid
= get_current_thread (wait_status
);
3308 if (!ptid_equal (ptid
, null_ptid
))
3310 if (!remote_multi_process_p (rs
))
3313 inferior_ptid
= ptid
;
3317 /* Without this, some commands which require an active target
3318 (such as kill) won't work. This variable serves (at least)
3319 double duty as both the pid of the target process (if it has
3320 such), and as a flag indicating that a target is active. */
3321 inferior_ptid
= magic_null_ptid
;
3325 remote_add_inferior (fake_pid_p
, ptid_get_pid (inferior_ptid
), -1);
3327 /* Add the main thread. */
3328 add_thread_silent (inferior_ptid
);
3332 remote_start_remote (int from_tty
, struct target_ops
*target
, int extended_p
)
3334 struct remote_state
*rs
= get_remote_state ();
3335 struct packet_config
*noack_config
;
3336 char *wait_status
= NULL
;
3338 immediate_quit
++; /* Allow user to interrupt it. */
3341 if (interrupt_on_connect
)
3342 send_interrupt_sequence ();
3344 /* Ack any packet which the remote side has already sent. */
3345 serial_write (rs
->remote_desc
, "+", 1);
3347 /* Signal other parts that we're going through the initial setup,
3348 and so things may not be stable yet. */
3349 rs
->starting_up
= 1;
3351 /* The first packet we send to the target is the optional "supported
3352 packets" request. If the target can answer this, it will tell us
3353 which later probes to skip. */
3354 remote_query_supported ();
3356 /* If the stub wants to get a QAllow, compose one and send it. */
3357 if (remote_protocol_packets
[PACKET_QAllow
].support
!= PACKET_DISABLE
)
3358 remote_set_permissions ();
3360 /* Next, we possibly activate noack mode.
3362 If the QStartNoAckMode packet configuration is set to AUTO,
3363 enable noack mode if the stub reported a wish for it with
3366 If set to TRUE, then enable noack mode even if the stub didn't
3367 report it in qSupported. If the stub doesn't reply OK, the
3368 session ends with an error.
3370 If FALSE, then don't activate noack mode, regardless of what the
3371 stub claimed should be the default with qSupported. */
3373 noack_config
= &remote_protocol_packets
[PACKET_QStartNoAckMode
];
3375 if (noack_config
->detect
== AUTO_BOOLEAN_TRUE
3376 || (noack_config
->detect
== AUTO_BOOLEAN_AUTO
3377 && noack_config
->support
== PACKET_ENABLE
))
3379 putpkt ("QStartNoAckMode");
3380 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3381 if (packet_ok (rs
->buf
, noack_config
) == PACKET_OK
)
3387 /* Tell the remote that we are using the extended protocol. */
3389 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3392 /* Let the target know which signals it is allowed to pass down to
3394 update_signals_program_target ();
3396 /* Next, if the target can specify a description, read it. We do
3397 this before anything involving memory or registers. */
3398 target_find_description ();
3400 /* Next, now that we know something about the target, update the
3401 address spaces in the program spaces. */
3402 update_address_spaces ();
3404 /* On OSs where the list of libraries is global to all
3405 processes, we fetch them early. */
3406 if (gdbarch_has_global_solist (target_gdbarch ()))
3407 solib_add (NULL
, from_tty
, target
, auto_solib_add
);
3411 if (!rs
->non_stop_aware
)
3412 error (_("Non-stop mode requested, but remote "
3413 "does not support non-stop"));
3415 putpkt ("QNonStop:1");
3416 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3418 if (strcmp (rs
->buf
, "OK") != 0)
3419 error (_("Remote refused setting non-stop mode with: %s"), rs
->buf
);
3421 /* Find about threads and processes the stub is already
3422 controlling. We default to adding them in the running state.
3423 The '?' query below will then tell us about which threads are
3425 remote_threads_info (target
);
3427 else if (rs
->non_stop_aware
)
3429 /* Don't assume that the stub can operate in all-stop mode.
3430 Request it explicitly. */
3431 putpkt ("QNonStop:0");
3432 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3434 if (strcmp (rs
->buf
, "OK") != 0)
3435 error (_("Remote refused setting all-stop mode with: %s"), rs
->buf
);
3438 /* Upload TSVs regardless of whether the target is running or not. The
3439 remote stub, such as GDBserver, may have some predefined or builtin
3440 TSVs, even if the target is not running. */
3441 if (remote_get_trace_status (target
, current_trace_status ()) != -1)
3443 struct uploaded_tsv
*uploaded_tsvs
= NULL
;
3445 remote_upload_trace_state_variables (&uploaded_tsvs
);
3446 merge_uploaded_trace_state_variables (&uploaded_tsvs
);
3449 /* Check whether the target is running now. */
3451 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3457 struct inferior
*inf
;
3459 if (rs
->buf
[0] == 'W' || rs
->buf
[0] == 'X')
3462 error (_("The target is not running (try extended-remote?)"));
3464 /* We're connected, but not running. Drop out before we
3465 call start_remote. */
3466 rs
->starting_up
= 0;
3471 /* Save the reply for later. */
3472 wait_status
= alloca (strlen (rs
->buf
) + 1);
3473 strcpy (wait_status
, rs
->buf
);
3476 /* Fetch thread list. */
3477 target_find_new_threads ();
3479 /* Let the stub know that we want it to return the thread. */
3480 set_continue_thread (minus_one_ptid
);
3482 if (thread_count () == 0)
3484 /* Target has no concept of threads at all. GDB treats
3485 non-threaded target as single-threaded; add a main
3487 add_current_inferior_and_thread (wait_status
);
3491 /* We have thread information; select the thread the target
3492 says should be current. If we're reconnecting to a
3493 multi-threaded program, this will ideally be the thread
3494 that last reported an event before GDB disconnected. */
3495 inferior_ptid
= get_current_thread (wait_status
);
3496 if (ptid_equal (inferior_ptid
, null_ptid
))
3498 /* Odd... The target was able to list threads, but not
3499 tell us which thread was current (no "thread"
3500 register in T stop reply?). Just pick the first
3501 thread in the thread list then. */
3502 inferior_ptid
= thread_list
->ptid
;
3506 /* init_wait_for_inferior should be called before get_offsets in order
3507 to manage `inserted' flag in bp loc in a correct state.
3508 breakpoint_init_inferior, called from init_wait_for_inferior, set
3509 `inserted' flag to 0, while before breakpoint_re_set, called from
3510 start_remote, set `inserted' flag to 1. In the initialization of
3511 inferior, breakpoint_init_inferior should be called first, and then
3512 breakpoint_re_set can be called. If this order is broken, state of
3513 `inserted' flag is wrong, and cause some problems on breakpoint
3515 init_wait_for_inferior ();
3517 get_offsets (); /* Get text, data & bss offsets. */
3519 /* If we could not find a description using qXfer, and we know
3520 how to do it some other way, try again. This is not
3521 supported for non-stop; it could be, but it is tricky if
3522 there are no stopped threads when we connect. */
3523 if (remote_read_description_p (target
)
3524 && gdbarch_target_desc (target_gdbarch ()) == NULL
)
3526 target_clear_description ();
3527 target_find_description ();
3530 /* Use the previously fetched status. */
3531 gdb_assert (wait_status
!= NULL
);
3532 strcpy (rs
->buf
, wait_status
);
3533 rs
->cached_wait_status
= 1;
3536 start_remote (from_tty
); /* Initialize gdb process mechanisms. */
3540 /* Clear WFI global state. Do this before finding about new
3541 threads and inferiors, and setting the current inferior.
3542 Otherwise we would clear the proceed status of the current
3543 inferior when we want its stop_soon state to be preserved
3544 (see notice_new_inferior). */
3545 init_wait_for_inferior ();
3547 /* In non-stop, we will either get an "OK", meaning that there
3548 are no stopped threads at this time; or, a regular stop
3549 reply. In the latter case, there may be more than one thread
3550 stopped --- we pull them all out using the vStopped
3552 if (strcmp (rs
->buf
, "OK") != 0)
3554 struct notif_client
*notif
= ¬if_client_stop
;
3556 /* remote_notif_get_pending_replies acks this one, and gets
3558 rs
->notif_state
->pending_event
[notif_client_stop
.id
]
3559 = remote_notif_parse (notif
, rs
->buf
);
3560 remote_notif_get_pending_events (notif
);
3562 /* Make sure that threads that were stopped remain
3564 iterate_over_threads (set_stop_requested_callback
, NULL
);
3567 if (target_can_async_p ())
3568 target_async (inferior_event_handler
, 0);
3570 if (thread_count () == 0)
3573 error (_("The target is not running (try extended-remote?)"));
3575 /* We're connected, but not running. Drop out before we
3576 call start_remote. */
3577 rs
->starting_up
= 0;
3581 /* Let the stub know that we want it to return the thread. */
3583 /* Force the stub to choose a thread. */
3584 set_general_thread (null_ptid
);
3587 inferior_ptid
= remote_current_thread (minus_one_ptid
);
3588 if (ptid_equal (inferior_ptid
, minus_one_ptid
))
3589 error (_("remote didn't report the current thread in non-stop mode"));
3591 get_offsets (); /* Get text, data & bss offsets. */
3593 /* In non-stop mode, any cached wait status will be stored in
3594 the stop reply queue. */
3595 gdb_assert (wait_status
== NULL
);
3597 /* Report all signals during attach/startup. */
3598 remote_pass_signals (target
, 0, NULL
);
3601 /* If we connected to a live target, do some additional setup. */
3602 if (target_has_execution
)
3604 if (symfile_objfile
) /* No use without a symbol-file. */
3605 remote_check_symbols ();
3608 /* Possibly the target has been engaged in a trace run started
3609 previously; find out where things are at. */
3610 if (remote_get_trace_status (target
, current_trace_status ()) != -1)
3612 struct uploaded_tp
*uploaded_tps
= NULL
;
3614 if (current_trace_status ()->running
)
3615 printf_filtered (_("Trace is already running on the target.\n"));
3617 remote_upload_tracepoints (&uploaded_tps
);
3619 merge_uploaded_tracepoints (&uploaded_tps
);
3622 /* The thread and inferior lists are now synchronized with the
3623 target, our symbols have been relocated, and we're merged the
3624 target's tracepoints with ours. We're done with basic start
3626 rs
->starting_up
= 0;
3628 /* If breakpoints are global, insert them now. */
3629 if (gdbarch_has_global_breakpoints (target_gdbarch ())
3630 && breakpoints_always_inserted_mode ())
3631 insert_breakpoints ();
3634 /* Open a connection to a remote debugger.
3635 NAME is the filename used for communication. */
3638 remote_open (char *name
, int from_tty
)
3640 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
3643 /* Open a connection to a remote debugger using the extended
3644 remote gdb protocol. NAME is the filename used for communication. */
3647 extended_remote_open (char *name
, int from_tty
)
3649 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */);
3652 /* Generic code for opening a connection to a remote target. */
3655 init_all_packet_configs (void)
3659 for (i
= 0; i
< PACKET_MAX
; i
++)
3660 update_packet_config (&remote_protocol_packets
[i
]);
3663 /* Symbol look-up. */
3666 remote_check_symbols (void)
3668 struct remote_state
*rs
= get_remote_state ();
3669 char *msg
, *reply
, *tmp
;
3670 struct minimal_symbol
*sym
;
3673 /* The remote side has no concept of inferiors that aren't running
3674 yet, it only knows about running processes. If we're connected
3675 but our current inferior is not running, we should not invite the
3676 remote target to request symbol lookups related to its
3677 (unrelated) current process. */
3678 if (!target_has_execution
)
3681 if (remote_protocol_packets
[PACKET_qSymbol
].support
== PACKET_DISABLE
)
3684 /* Make sure the remote is pointing at the right process. Note
3685 there's no way to select "no process". */
3686 set_general_process ();
3688 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3689 because we need both at the same time. */
3690 msg
= alloca (get_remote_packet_size ());
3692 /* Invite target to request symbol lookups. */
3694 putpkt ("qSymbol::");
3695 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3696 packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSymbol
]);
3699 while (strncmp (reply
, "qSymbol:", 8) == 0)
3702 end
= hex2bin (tmp
, (gdb_byte
*) msg
, strlen (tmp
) / 2);
3704 sym
= lookup_minimal_symbol (msg
, NULL
, NULL
);
3706 xsnprintf (msg
, get_remote_packet_size (), "qSymbol::%s", &reply
[8]);
3709 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
3710 CORE_ADDR sym_addr
= SYMBOL_VALUE_ADDRESS (sym
);
3712 /* If this is a function address, return the start of code
3713 instead of any data function descriptor. */
3714 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
3718 xsnprintf (msg
, get_remote_packet_size (), "qSymbol:%s:%s",
3719 phex_nz (sym_addr
, addr_size
), &reply
[8]);
3723 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3728 static struct serial
*
3729 remote_serial_open (char *name
)
3731 static int udp_warning
= 0;
3733 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3734 of in ser-tcp.c, because it is the remote protocol assuming that the
3735 serial connection is reliable and not the serial connection promising
3737 if (!udp_warning
&& strncmp (name
, "udp:", 4) == 0)
3739 warning (_("The remote protocol may be unreliable over UDP.\n"
3740 "Some events may be lost, rendering further debugging "
3745 return serial_open (name
);
3748 /* Inform the target of our permission settings. The permission flags
3749 work without this, but if the target knows the settings, it can do
3750 a couple things. First, it can add its own check, to catch cases
3751 that somehow manage to get by the permissions checks in target
3752 methods. Second, if the target is wired to disallow particular
3753 settings (for instance, a system in the field that is not set up to
3754 be able to stop at a breakpoint), it can object to any unavailable
3758 remote_set_permissions (void)
3760 struct remote_state
*rs
= get_remote_state ();
3762 xsnprintf (rs
->buf
, get_remote_packet_size (), "QAllow:"
3763 "WriteReg:%x;WriteMem:%x;"
3764 "InsertBreak:%x;InsertTrace:%x;"
3765 "InsertFastTrace:%x;Stop:%x",
3766 may_write_registers
, may_write_memory
,
3767 may_insert_breakpoints
, may_insert_tracepoints
,
3768 may_insert_fast_tracepoints
, may_stop
);
3770 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
3772 /* If the target didn't like the packet, warn the user. Do not try
3773 to undo the user's settings, that would just be maddening. */
3774 if (strcmp (rs
->buf
, "OK") != 0)
3775 warning (_("Remote refused setting permissions with: %s"), rs
->buf
);
3778 /* This type describes each known response to the qSupported
3780 struct protocol_feature
3782 /* The name of this protocol feature. */
3785 /* The default for this protocol feature. */
3786 enum packet_support default_support
;
3788 /* The function to call when this feature is reported, or after
3789 qSupported processing if the feature is not supported.
3790 The first argument points to this structure. The second
3791 argument indicates whether the packet requested support be
3792 enabled, disabled, or probed (or the default, if this function
3793 is being called at the end of processing and this feature was
3794 not reported). The third argument may be NULL; if not NULL, it
3795 is a NUL-terminated string taken from the packet following
3796 this feature's name and an equals sign. */
3797 void (*func
) (const struct protocol_feature
*, enum packet_support
,
3800 /* The corresponding packet for this feature. Only used if
3801 FUNC is remote_supported_packet. */
3806 remote_supported_packet (const struct protocol_feature
*feature
,
3807 enum packet_support support
,
3808 const char *argument
)
3812 warning (_("Remote qSupported response supplied an unexpected value for"
3813 " \"%s\"."), feature
->name
);
3817 if (remote_protocol_packets
[feature
->packet
].support
3818 == PACKET_SUPPORT_UNKNOWN
)
3819 remote_protocol_packets
[feature
->packet
].support
= support
;
3823 remote_packet_size (const struct protocol_feature
*feature
,
3824 enum packet_support support
, const char *value
)
3826 struct remote_state
*rs
= get_remote_state ();
3831 if (support
!= PACKET_ENABLE
)
3834 if (value
== NULL
|| *value
== '\0')
3836 warning (_("Remote target reported \"%s\" without a size."),
3842 packet_size
= strtol (value
, &value_end
, 16);
3843 if (errno
!= 0 || *value_end
!= '\0' || packet_size
< 0)
3845 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3846 feature
->name
, value
);
3850 if (packet_size
> MAX_REMOTE_PACKET_SIZE
)
3852 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3853 packet_size
, MAX_REMOTE_PACKET_SIZE
);
3854 packet_size
= MAX_REMOTE_PACKET_SIZE
;
3857 /* Record the new maximum packet size. */
3858 rs
->explicit_packet_size
= packet_size
;
3862 remote_multi_process_feature (const struct protocol_feature
*feature
,
3863 enum packet_support support
, const char *value
)
3865 struct remote_state
*rs
= get_remote_state ();
3867 rs
->multi_process_aware
= (support
== PACKET_ENABLE
);
3871 remote_non_stop_feature (const struct protocol_feature
*feature
,
3872 enum packet_support support
, const char *value
)
3874 struct remote_state
*rs
= get_remote_state ();
3876 rs
->non_stop_aware
= (support
== PACKET_ENABLE
);
3880 remote_cond_tracepoint_feature (const struct protocol_feature
*feature
,
3881 enum packet_support support
,
3884 struct remote_state
*rs
= get_remote_state ();
3886 rs
->cond_tracepoints
= (support
== PACKET_ENABLE
);
3890 remote_cond_breakpoint_feature (const struct protocol_feature
*feature
,
3891 enum packet_support support
,
3894 struct remote_state
*rs
= get_remote_state ();
3896 rs
->cond_breakpoints
= (support
== PACKET_ENABLE
);
3900 remote_breakpoint_commands_feature (const struct protocol_feature
*feature
,
3901 enum packet_support support
,
3904 struct remote_state
*rs
= get_remote_state ();
3906 rs
->breakpoint_commands
= (support
== PACKET_ENABLE
);
3910 remote_fast_tracepoint_feature (const struct protocol_feature
*feature
,
3911 enum packet_support support
,
3914 struct remote_state
*rs
= get_remote_state ();
3916 rs
->fast_tracepoints
= (support
== PACKET_ENABLE
);
3920 remote_static_tracepoint_feature (const struct protocol_feature
*feature
,
3921 enum packet_support support
,
3924 struct remote_state
*rs
= get_remote_state ();
3926 rs
->static_tracepoints
= (support
== PACKET_ENABLE
);
3930 remote_install_in_trace_feature (const struct protocol_feature
*feature
,
3931 enum packet_support support
,
3934 struct remote_state
*rs
= get_remote_state ();
3936 rs
->install_in_trace
= (support
== PACKET_ENABLE
);
3940 remote_disconnected_tracing_feature (const struct protocol_feature
*feature
,
3941 enum packet_support support
,
3944 struct remote_state
*rs
= get_remote_state ();
3946 rs
->disconnected_tracing
= (support
== PACKET_ENABLE
);
3950 remote_enable_disable_tracepoint_feature (const struct protocol_feature
*feature
,
3951 enum packet_support support
,
3954 struct remote_state
*rs
= get_remote_state ();
3956 rs
->enable_disable_tracepoints
= (support
== PACKET_ENABLE
);
3960 remote_string_tracing_feature (const struct protocol_feature
*feature
,
3961 enum packet_support support
,
3964 struct remote_state
*rs
= get_remote_state ();
3966 rs
->string_tracing
= (support
== PACKET_ENABLE
);
3970 remote_augmented_libraries_svr4_read_feature
3971 (const struct protocol_feature
*feature
,
3972 enum packet_support support
, const char *value
)
3974 struct remote_state
*rs
= get_remote_state ();
3976 rs
->augmented_libraries_svr4_read
= (support
== PACKET_ENABLE
);
3979 static const struct protocol_feature remote_protocol_features
[] = {
3980 { "PacketSize", PACKET_DISABLE
, remote_packet_size
, -1 },
3981 { "qXfer:auxv:read", PACKET_DISABLE
, remote_supported_packet
,
3982 PACKET_qXfer_auxv
},
3983 { "qXfer:features:read", PACKET_DISABLE
, remote_supported_packet
,
3984 PACKET_qXfer_features
},
3985 { "qXfer:libraries:read", PACKET_DISABLE
, remote_supported_packet
,
3986 PACKET_qXfer_libraries
},
3987 { "qXfer:libraries-svr4:read", PACKET_DISABLE
, remote_supported_packet
,
3988 PACKET_qXfer_libraries_svr4
},
3989 { "augmented-libraries-svr4-read", PACKET_DISABLE
,
3990 remote_augmented_libraries_svr4_read_feature
, -1 },
3991 { "qXfer:memory-map:read", PACKET_DISABLE
, remote_supported_packet
,
3992 PACKET_qXfer_memory_map
},
3993 { "qXfer:spu:read", PACKET_DISABLE
, remote_supported_packet
,
3994 PACKET_qXfer_spu_read
},
3995 { "qXfer:spu:write", PACKET_DISABLE
, remote_supported_packet
,
3996 PACKET_qXfer_spu_write
},
3997 { "qXfer:osdata:read", PACKET_DISABLE
, remote_supported_packet
,
3998 PACKET_qXfer_osdata
},
3999 { "qXfer:threads:read", PACKET_DISABLE
, remote_supported_packet
,
4000 PACKET_qXfer_threads
},
4001 { "qXfer:traceframe-info:read", PACKET_DISABLE
, remote_supported_packet
,
4002 PACKET_qXfer_traceframe_info
},
4003 { "QPassSignals", PACKET_DISABLE
, remote_supported_packet
,
4004 PACKET_QPassSignals
},
4005 { "QProgramSignals", PACKET_DISABLE
, remote_supported_packet
,
4006 PACKET_QProgramSignals
},
4007 { "QStartNoAckMode", PACKET_DISABLE
, remote_supported_packet
,
4008 PACKET_QStartNoAckMode
},
4009 { "multiprocess", PACKET_DISABLE
, remote_multi_process_feature
, -1 },
4010 { "QNonStop", PACKET_DISABLE
, remote_non_stop_feature
, -1 },
4011 { "qXfer:siginfo:read", PACKET_DISABLE
, remote_supported_packet
,
4012 PACKET_qXfer_siginfo_read
},
4013 { "qXfer:siginfo:write", PACKET_DISABLE
, remote_supported_packet
,
4014 PACKET_qXfer_siginfo_write
},
4015 { "ConditionalTracepoints", PACKET_DISABLE
, remote_cond_tracepoint_feature
,
4016 PACKET_ConditionalTracepoints
},
4017 { "ConditionalBreakpoints", PACKET_DISABLE
, remote_cond_breakpoint_feature
,
4018 PACKET_ConditionalBreakpoints
},
4019 { "BreakpointCommands", PACKET_DISABLE
, remote_breakpoint_commands_feature
,
4020 PACKET_BreakpointCommands
},
4021 { "FastTracepoints", PACKET_DISABLE
, remote_fast_tracepoint_feature
,
4022 PACKET_FastTracepoints
},
4023 { "StaticTracepoints", PACKET_DISABLE
, remote_static_tracepoint_feature
,
4024 PACKET_StaticTracepoints
},
4025 {"InstallInTrace", PACKET_DISABLE
, remote_install_in_trace_feature
,
4026 PACKET_InstallInTrace
},
4027 { "DisconnectedTracing", PACKET_DISABLE
, remote_disconnected_tracing_feature
,
4029 { "ReverseContinue", PACKET_DISABLE
, remote_supported_packet
,
4031 { "ReverseStep", PACKET_DISABLE
, remote_supported_packet
,
4033 { "TracepointSource", PACKET_DISABLE
, remote_supported_packet
,
4034 PACKET_TracepointSource
},
4035 { "QAllow", PACKET_DISABLE
, remote_supported_packet
,
4037 { "EnableDisableTracepoints", PACKET_DISABLE
,
4038 remote_enable_disable_tracepoint_feature
, -1 },
4039 { "qXfer:fdpic:read", PACKET_DISABLE
, remote_supported_packet
,
4040 PACKET_qXfer_fdpic
},
4041 { "qXfer:uib:read", PACKET_DISABLE
, remote_supported_packet
,
4043 { "QDisableRandomization", PACKET_DISABLE
, remote_supported_packet
,
4044 PACKET_QDisableRandomization
},
4045 { "QAgent", PACKET_DISABLE
, remote_supported_packet
, PACKET_QAgent
},
4046 { "QTBuffer:size", PACKET_DISABLE
,
4047 remote_supported_packet
, PACKET_QTBuffer_size
},
4048 { "tracenz", PACKET_DISABLE
,
4049 remote_string_tracing_feature
, -1 },
4050 { "Qbtrace:off", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_off
},
4051 { "Qbtrace:bts", PACKET_DISABLE
, remote_supported_packet
, PACKET_Qbtrace_bts
},
4052 { "qXfer:btrace:read", PACKET_DISABLE
, remote_supported_packet
,
4053 PACKET_qXfer_btrace
}
4056 static char *remote_support_xml
;
4058 /* Register string appended to "xmlRegisters=" in qSupported query. */
4061 register_remote_support_xml (const char *xml
)
4063 #if defined(HAVE_LIBEXPAT)
4064 if (remote_support_xml
== NULL
)
4065 remote_support_xml
= concat ("xmlRegisters=", xml
, (char *) NULL
);
4068 char *copy
= xstrdup (remote_support_xml
+ 13);
4069 char *p
= strtok (copy
, ",");
4073 if (strcmp (p
, xml
) == 0)
4080 while ((p
= strtok (NULL
, ",")) != NULL
);
4083 remote_support_xml
= reconcat (remote_support_xml
,
4084 remote_support_xml
, ",", xml
,
4091 remote_query_supported_append (char *msg
, const char *append
)
4094 return reconcat (msg
, msg
, ";", append
, (char *) NULL
);
4096 return xstrdup (append
);
4100 remote_query_supported (void)
4102 struct remote_state
*rs
= get_remote_state ();
4105 unsigned char seen
[ARRAY_SIZE (remote_protocol_features
)];
4107 /* The packet support flags are handled differently for this packet
4108 than for most others. We treat an error, a disabled packet, and
4109 an empty response identically: any features which must be reported
4110 to be used will be automatically disabled. An empty buffer
4111 accomplishes this, since that is also the representation for a list
4112 containing no features. */
4115 if (remote_protocol_packets
[PACKET_qSupported
].support
!= PACKET_DISABLE
)
4118 struct cleanup
*old_chain
= make_cleanup (free_current_contents
, &q
);
4120 q
= remote_query_supported_append (q
, "multiprocess+");
4122 if (remote_support_xml
)
4123 q
= remote_query_supported_append (q
, remote_support_xml
);
4125 q
= remote_query_supported_append (q
, "qRelocInsn+");
4127 q
= reconcat (q
, "qSupported:", q
, (char *) NULL
);
4130 do_cleanups (old_chain
);
4132 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4134 /* If an error occured, warn, but do not return - just reset the
4135 buffer to empty and go on to disable features. */
4136 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_qSupported
])
4139 warning (_("Remote failure reply: %s"), rs
->buf
);
4144 memset (seen
, 0, sizeof (seen
));
4149 enum packet_support is_supported
;
4150 char *p
, *end
, *name_end
, *value
;
4152 /* First separate out this item from the rest of the packet. If
4153 there's another item after this, we overwrite the separator
4154 (terminated strings are much easier to work with). */
4156 end
= strchr (p
, ';');
4159 end
= p
+ strlen (p
);
4169 warning (_("empty item in \"qSupported\" response"));
4174 name_end
= strchr (p
, '=');
4177 /* This is a name=value entry. */
4178 is_supported
= PACKET_ENABLE
;
4179 value
= name_end
+ 1;
4188 is_supported
= PACKET_ENABLE
;
4192 is_supported
= PACKET_DISABLE
;
4196 is_supported
= PACKET_SUPPORT_UNKNOWN
;
4200 warning (_("unrecognized item \"%s\" "
4201 "in \"qSupported\" response"), p
);
4207 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
4208 if (strcmp (remote_protocol_features
[i
].name
, p
) == 0)
4210 const struct protocol_feature
*feature
;
4213 feature
= &remote_protocol_features
[i
];
4214 feature
->func (feature
, is_supported
, value
);
4219 /* If we increased the packet size, make sure to increase the global
4220 buffer size also. We delay this until after parsing the entire
4221 qSupported packet, because this is the same buffer we were
4223 if (rs
->buf_size
< rs
->explicit_packet_size
)
4225 rs
->buf_size
= rs
->explicit_packet_size
;
4226 rs
->buf
= xrealloc (rs
->buf
, rs
->buf_size
);
4229 /* Handle the defaults for unmentioned features. */
4230 for (i
= 0; i
< ARRAY_SIZE (remote_protocol_features
); i
++)
4233 const struct protocol_feature
*feature
;
4235 feature
= &remote_protocol_features
[i
];
4236 feature
->func (feature
, feature
->default_support
, NULL
);
4240 /* Remove any of the remote.c targets from target stack. Upper targets depend
4241 on it so remove them first. */
4244 remote_unpush_target (void)
4246 pop_all_targets_above (process_stratum
- 1);
4250 remote_open_1 (char *name
, int from_tty
,
4251 struct target_ops
*target
, int extended_p
)
4253 struct remote_state
*rs
= get_remote_state ();
4256 error (_("To open a remote debug connection, you need to specify what\n"
4257 "serial device is attached to the remote system\n"
4258 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
4260 /* See FIXME above. */
4261 if (!target_async_permitted
)
4262 wait_forever_enabled_p
= 1;
4264 /* If we're connected to a running target, target_preopen will kill it.
4265 Ask this question first, before target_preopen has a chance to kill
4267 if (rs
->remote_desc
!= NULL
&& !have_inferiors ())
4270 && !query (_("Already connected to a remote target. Disconnect? ")))
4271 error (_("Still connected."));
4274 /* Here the possibly existing remote target gets unpushed. */
4275 target_preopen (from_tty
);
4277 /* Make sure we send the passed signals list the next time we resume. */
4278 xfree (rs
->last_pass_packet
);
4279 rs
->last_pass_packet
= NULL
;
4281 /* Make sure we send the program signals list the next time we
4283 xfree (rs
->last_program_signals_packet
);
4284 rs
->last_program_signals_packet
= NULL
;
4286 remote_fileio_reset ();
4287 reopen_exec_file ();
4290 rs
->remote_desc
= remote_serial_open (name
);
4291 if (!rs
->remote_desc
)
4292 perror_with_name (name
);
4294 if (baud_rate
!= -1)
4296 if (serial_setbaudrate (rs
->remote_desc
, baud_rate
))
4298 /* The requested speed could not be set. Error out to
4299 top level after closing remote_desc. Take care to
4300 set remote_desc to NULL to avoid closing remote_desc
4302 serial_close (rs
->remote_desc
);
4303 rs
->remote_desc
= NULL
;
4304 perror_with_name (name
);
4308 serial_raw (rs
->remote_desc
);
4310 /* If there is something sitting in the buffer we might take it as a
4311 response to a command, which would be bad. */
4312 serial_flush_input (rs
->remote_desc
);
4316 puts_filtered ("Remote debugging using ");
4317 puts_filtered (name
);
4318 puts_filtered ("\n");
4320 push_target (target
); /* Switch to using remote target now. */
4322 /* Register extra event sources in the event loop. */
4323 remote_async_inferior_event_token
4324 = create_async_event_handler (remote_async_inferior_event_handler
,
4326 rs
->notif_state
= remote_notif_state_allocate ();
4328 /* Reset the target state; these things will be queried either by
4329 remote_query_supported or as they are needed. */
4330 init_all_packet_configs ();
4331 rs
->cached_wait_status
= 0;
4332 rs
->explicit_packet_size
= 0;
4334 rs
->multi_process_aware
= 0;
4335 rs
->extended
= extended_p
;
4336 rs
->non_stop_aware
= 0;
4337 rs
->waiting_for_stop_reply
= 0;
4338 rs
->ctrlc_pending_p
= 0;
4340 rs
->general_thread
= not_sent_ptid
;
4341 rs
->continue_thread
= not_sent_ptid
;
4342 rs
->remote_traceframe_number
= -1;
4344 /* Probe for ability to use "ThreadInfo" query, as required. */
4345 rs
->use_threadinfo_query
= 1;
4346 rs
->use_threadextra_query
= 1;
4348 if (target_async_permitted
)
4350 /* With this target we start out by owning the terminal. */
4351 remote_async_terminal_ours_p
= 1;
4353 /* FIXME: cagney/1999-09-23: During the initial connection it is
4354 assumed that the target is already ready and able to respond to
4355 requests. Unfortunately remote_start_remote() eventually calls
4356 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
4357 around this. Eventually a mechanism that allows
4358 wait_for_inferior() to expect/get timeouts will be
4360 wait_forever_enabled_p
= 0;
4363 /* First delete any symbols previously loaded from shared libraries. */
4364 no_shared_libraries (NULL
, 0);
4367 init_thread_list ();
4369 /* Start the remote connection. If error() or QUIT, discard this
4370 target (we'd otherwise be in an inconsistent state) and then
4371 propogate the error on up the exception chain. This ensures that
4372 the caller doesn't stumble along blindly assuming that the
4373 function succeeded. The CLI doesn't have this problem but other
4374 UI's, such as MI do.
4376 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4377 this function should return an error indication letting the
4378 caller restore the previous state. Unfortunately the command
4379 ``target remote'' is directly wired to this function making that
4380 impossible. On a positive note, the CLI side of this problem has
4381 been fixed - the function set_cmd_context() makes it possible for
4382 all the ``target ....'' commands to share a common callback
4383 function. See cli-dump.c. */
4385 volatile struct gdb_exception ex
;
4387 TRY_CATCH (ex
, RETURN_MASK_ALL
)
4389 remote_start_remote (from_tty
, target
, extended_p
);
4393 /* Pop the partially set up target - unless something else did
4394 already before throwing the exception. */
4395 if (rs
->remote_desc
!= NULL
)
4396 remote_unpush_target ();
4397 if (target_async_permitted
)
4398 wait_forever_enabled_p
= 1;
4399 throw_exception (ex
);
4403 if (target_async_permitted
)
4404 wait_forever_enabled_p
= 1;
4407 /* This takes a program previously attached to and detaches it. After
4408 this is done, GDB can be used to debug some other program. We
4409 better not have left any breakpoints in the target program or it'll
4410 die when it hits one. */
4413 remote_detach_1 (const char *args
, int from_tty
, int extended
)
4415 int pid
= ptid_get_pid (inferior_ptid
);
4416 struct remote_state
*rs
= get_remote_state ();
4419 error (_("Argument given to \"detach\" when remotely debugging."));
4421 if (!target_has_execution
)
4422 error (_("No process to detach from."));
4426 char *exec_file
= get_exec_file (0);
4427 if (exec_file
== NULL
)
4429 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file
,
4430 target_pid_to_str (pid_to_ptid (pid
)));
4431 gdb_flush (gdb_stdout
);
4434 /* Tell the remote target to detach. */
4435 if (remote_multi_process_p (rs
))
4436 xsnprintf (rs
->buf
, get_remote_packet_size (), "D;%x", pid
);
4438 strcpy (rs
->buf
, "D");
4441 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4443 if (rs
->buf
[0] == 'O' && rs
->buf
[1] == 'K')
4445 else if (rs
->buf
[0] == '\0')
4446 error (_("Remote doesn't know how to detach"));
4448 error (_("Can't detach process."));
4450 if (from_tty
&& !extended
)
4451 puts_filtered (_("Ending remote debugging.\n"));
4453 target_mourn_inferior ();
4457 remote_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
4459 remote_detach_1 (args
, from_tty
, 0);
4463 extended_remote_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
4465 remote_detach_1 (args
, from_tty
, 1);
4468 /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4471 remote_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
4474 error (_("Argument given to \"disconnect\" when remotely debugging."));
4476 /* Make sure we unpush even the extended remote targets; mourn
4477 won't do it. So call remote_mourn_1 directly instead of
4478 target_mourn_inferior. */
4479 remote_mourn_1 (target
);
4482 puts_filtered ("Ending remote debugging.\n");
4485 /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4486 be chatty about it. */
4489 extended_remote_attach_1 (struct target_ops
*target
, char *args
, int from_tty
)
4491 struct remote_state
*rs
= get_remote_state ();
4493 char *wait_status
= NULL
;
4495 pid
= parse_pid_to_attach (args
);
4497 /* Remote PID can be freely equal to getpid, do not check it here the same
4498 way as in other targets. */
4500 if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4501 error (_("This target does not support attaching to a process"));
4505 char *exec_file
= get_exec_file (0);
4508 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file
,
4509 target_pid_to_str (pid_to_ptid (pid
)));
4511 printf_unfiltered (_("Attaching to %s\n"),
4512 target_pid_to_str (pid_to_ptid (pid
)));
4514 gdb_flush (gdb_stdout
);
4517 xsnprintf (rs
->buf
, get_remote_packet_size (), "vAttach;%x", pid
);
4519 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4521 if (packet_ok (rs
->buf
,
4522 &remote_protocol_packets
[PACKET_vAttach
]) == PACKET_OK
)
4526 /* Save the reply for later. */
4527 wait_status
= alloca (strlen (rs
->buf
) + 1);
4528 strcpy (wait_status
, rs
->buf
);
4530 else if (strcmp (rs
->buf
, "OK") != 0)
4531 error (_("Attaching to %s failed with: %s"),
4532 target_pid_to_str (pid_to_ptid (pid
)),
4535 else if (remote_protocol_packets
[PACKET_vAttach
].support
== PACKET_DISABLE
)
4536 error (_("This target does not support attaching to a process"));
4538 error (_("Attaching to %s failed"),
4539 target_pid_to_str (pid_to_ptid (pid
)));
4541 set_current_inferior (remote_add_inferior (0, pid
, 1));
4543 inferior_ptid
= pid_to_ptid (pid
);
4547 struct thread_info
*thread
;
4549 /* Get list of threads. */
4550 remote_threads_info (target
);
4552 thread
= first_thread_of_process (pid
);
4554 inferior_ptid
= thread
->ptid
;
4556 inferior_ptid
= pid_to_ptid (pid
);
4558 /* Invalidate our notion of the remote current thread. */
4559 record_currthread (rs
, minus_one_ptid
);
4563 /* Now, if we have thread information, update inferior_ptid. */
4564 inferior_ptid
= remote_current_thread (inferior_ptid
);
4566 /* Add the main thread to the thread list. */
4567 add_thread_silent (inferior_ptid
);
4570 /* Next, if the target can specify a description, read it. We do
4571 this before anything involving memory or registers. */
4572 target_find_description ();
4576 /* Use the previously fetched status. */
4577 gdb_assert (wait_status
!= NULL
);
4579 if (target_can_async_p ())
4581 struct notif_event
*reply
4582 = remote_notif_parse (¬if_client_stop
, wait_status
);
4584 push_stop_reply ((struct stop_reply
*) reply
);
4586 target_async (inferior_event_handler
, 0);
4590 gdb_assert (wait_status
!= NULL
);
4591 strcpy (rs
->buf
, wait_status
);
4592 rs
->cached_wait_status
= 1;
4596 gdb_assert (wait_status
== NULL
);
4600 extended_remote_attach (struct target_ops
*ops
, char *args
, int from_tty
)
4602 extended_remote_attach_1 (ops
, args
, from_tty
);
4606 /* Check for the availability of vCont. This function should also check
4610 remote_vcont_probe (struct remote_state
*rs
)
4614 strcpy (rs
->buf
, "vCont?");
4616 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4619 /* Make sure that the features we assume are supported. */
4620 if (strncmp (buf
, "vCont", 5) == 0)
4623 int support_s
, support_S
, support_c
, support_C
;
4629 rs
->supports_vCont
.t
= 0;
4630 rs
->supports_vCont
.r
= 0;
4631 while (p
&& *p
== ';')
4634 if (*p
== 's' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4636 else if (*p
== 'S' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4638 else if (*p
== 'c' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4640 else if (*p
== 'C' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4642 else if (*p
== 't' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4643 rs
->supports_vCont
.t
= 1;
4644 else if (*p
== 'r' && (*(p
+ 1) == ';' || *(p
+ 1) == 0))
4645 rs
->supports_vCont
.r
= 1;
4647 p
= strchr (p
, ';');
4650 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4651 BUF will make packet_ok disable the packet. */
4652 if (!support_s
|| !support_S
|| !support_c
|| !support_C
)
4656 packet_ok (buf
, &remote_protocol_packets
[PACKET_vCont
]);
4659 /* Helper function for building "vCont" resumptions. Write a
4660 resumption to P. ENDP points to one-passed-the-end of the buffer
4661 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4662 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4663 resumed thread should be single-stepped and/or signalled. If PTID
4664 equals minus_one_ptid, then all threads are resumed; if PTID
4665 represents a process, then all threads of the process are resumed;
4666 the thread to be stepped and/or signalled is given in the global
4670 append_resumption (char *p
, char *endp
,
4671 ptid_t ptid
, int step
, enum gdb_signal siggnal
)
4673 struct remote_state
*rs
= get_remote_state ();
4675 if (step
&& siggnal
!= GDB_SIGNAL_0
)
4676 p
+= xsnprintf (p
, endp
- p
, ";S%02x", siggnal
);
4678 /* GDB is willing to range step. */
4679 && use_range_stepping
4680 /* Target supports range stepping. */
4681 && rs
->supports_vCont
.r
4682 /* We don't currently support range stepping multiple
4683 threads with a wildcard (though the protocol allows it,
4684 so stubs shouldn't make an active effort to forbid
4686 && !(remote_multi_process_p (rs
) && ptid_is_pid (ptid
)))
4688 struct thread_info
*tp
;
4690 if (ptid_equal (ptid
, minus_one_ptid
))
4692 /* If we don't know about the target thread's tid, then
4693 we're resuming magic_null_ptid (see caller). */
4694 tp
= find_thread_ptid (magic_null_ptid
);
4697 tp
= find_thread_ptid (ptid
);
4698 gdb_assert (tp
!= NULL
);
4700 if (tp
->control
.may_range_step
)
4702 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
4704 p
+= xsnprintf (p
, endp
- p
, ";r%s,%s",
4705 phex_nz (tp
->control
.step_range_start
,
4707 phex_nz (tp
->control
.step_range_end
,
4711 p
+= xsnprintf (p
, endp
- p
, ";s");
4714 p
+= xsnprintf (p
, endp
- p
, ";s");
4715 else if (siggnal
!= GDB_SIGNAL_0
)
4716 p
+= xsnprintf (p
, endp
- p
, ";C%02x", siggnal
);
4718 p
+= xsnprintf (p
, endp
- p
, ";c");
4720 if (remote_multi_process_p (rs
) && ptid_is_pid (ptid
))
4724 /* All (-1) threads of process. */
4725 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
4727 p
+= xsnprintf (p
, endp
- p
, ":");
4728 p
= write_ptid (p
, endp
, nptid
);
4730 else if (!ptid_equal (ptid
, minus_one_ptid
))
4732 p
+= xsnprintf (p
, endp
- p
, ":");
4733 p
= write_ptid (p
, endp
, ptid
);
4739 /* Append a vCont continue-with-signal action for threads that have a
4740 non-zero stop signal. */
4743 append_pending_thread_resumptions (char *p
, char *endp
, ptid_t ptid
)
4745 struct thread_info
*thread
;
4747 ALL_THREADS (thread
)
4748 if (ptid_match (thread
->ptid
, ptid
)
4749 && !ptid_equal (inferior_ptid
, thread
->ptid
)
4750 && thread
->suspend
.stop_signal
!= GDB_SIGNAL_0
4751 && signal_pass_state (thread
->suspend
.stop_signal
))
4753 p
= append_resumption (p
, endp
, thread
->ptid
,
4754 0, thread
->suspend
.stop_signal
);
4755 thread
->suspend
.stop_signal
= GDB_SIGNAL_0
;
4761 /* Resume the remote inferior by using a "vCont" packet. The thread
4762 to be resumed is PTID; STEP and SIGGNAL indicate whether the
4763 resumed thread should be single-stepped and/or signalled. If PTID
4764 equals minus_one_ptid, then all threads are resumed; the thread to
4765 be stepped and/or signalled is given in the global INFERIOR_PTID.
4766 This function returns non-zero iff it resumes the inferior.
4768 This function issues a strict subset of all possible vCont commands at the
4772 remote_vcont_resume (ptid_t ptid
, int step
, enum gdb_signal siggnal
)
4774 struct remote_state
*rs
= get_remote_state ();
4778 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
4779 remote_vcont_probe (rs
);
4781 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_DISABLE
)
4785 endp
= rs
->buf
+ get_remote_packet_size ();
4787 /* If we could generate a wider range of packets, we'd have to worry
4788 about overflowing BUF. Should there be a generic
4789 "multi-part-packet" packet? */
4791 p
+= xsnprintf (p
, endp
- p
, "vCont");
4793 if (ptid_equal (ptid
, magic_null_ptid
))
4795 /* MAGIC_NULL_PTID means that we don't have any active threads,
4796 so we don't have any TID numbers the inferior will
4797 understand. Make sure to only send forms that do not specify
4799 append_resumption (p
, endp
, minus_one_ptid
, step
, siggnal
);
4801 else if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
4803 /* Resume all threads (of all processes, or of a single
4804 process), with preference for INFERIOR_PTID. This assumes
4805 inferior_ptid belongs to the set of all threads we are about
4807 if (step
|| siggnal
!= GDB_SIGNAL_0
)
4809 /* Step inferior_ptid, with or without signal. */
4810 p
= append_resumption (p
, endp
, inferior_ptid
, step
, siggnal
);
4813 /* Also pass down any pending signaled resumption for other
4814 threads not the current. */
4815 p
= append_pending_thread_resumptions (p
, endp
, ptid
);
4817 /* And continue others without a signal. */
4818 append_resumption (p
, endp
, ptid
, /*step=*/ 0, GDB_SIGNAL_0
);
4822 /* Scheduler locking; resume only PTID. */
4823 append_resumption (p
, endp
, ptid
, step
, siggnal
);
4826 gdb_assert (strlen (rs
->buf
) < get_remote_packet_size ());
4831 /* In non-stop, the stub replies to vCont with "OK". The stop
4832 reply will be reported asynchronously by means of a `%Stop'
4834 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
4835 if (strcmp (rs
->buf
, "OK") != 0)
4836 error (_("Unexpected vCont reply in non-stop mode: %s"), rs
->buf
);
4842 /* Tell the remote machine to resume. */
4845 remote_resume (struct target_ops
*ops
,
4846 ptid_t ptid
, int step
, enum gdb_signal siggnal
)
4848 struct remote_state
*rs
= get_remote_state ();
4851 /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
4852 (explained in remote-notif.c:handle_notification) so
4853 remote_notif_process is not called. We need find a place where
4854 it is safe to start a 'vNotif' sequence. It is good to do it
4855 before resuming inferior, because inferior was stopped and no RSP
4856 traffic at that moment. */
4858 remote_notif_process (rs
->notif_state
, ¬if_client_stop
);
4860 rs
->last_sent_signal
= siggnal
;
4861 rs
->last_sent_step
= step
;
4863 /* The vCont packet doesn't need to specify threads via Hc. */
4864 /* No reverse support (yet) for vCont. */
4865 if (execution_direction
!= EXEC_REVERSE
)
4866 if (remote_vcont_resume (ptid
, step
, siggnal
))
4869 /* All other supported resume packets do use Hc, so set the continue
4871 if (ptid_equal (ptid
, minus_one_ptid
))
4872 set_continue_thread (any_thread_ptid
);
4874 set_continue_thread (ptid
);
4877 if (execution_direction
== EXEC_REVERSE
)
4879 /* We don't pass signals to the target in reverse exec mode. */
4880 if (info_verbose
&& siggnal
!= GDB_SIGNAL_0
)
4881 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
4885 && remote_protocol_packets
[PACKET_bs
].support
== PACKET_DISABLE
)
4886 error (_("Remote reverse-step not supported."));
4888 && remote_protocol_packets
[PACKET_bc
].support
== PACKET_DISABLE
)
4889 error (_("Remote reverse-continue not supported."));
4891 strcpy (buf
, step
? "bs" : "bc");
4893 else if (siggnal
!= GDB_SIGNAL_0
)
4895 buf
[0] = step
? 'S' : 'C';
4896 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
4897 buf
[2] = tohex (((int) siggnal
) & 0xf);
4901 strcpy (buf
, step
? "s" : "c");
4906 /* We are about to start executing the inferior, let's register it
4907 with the event loop. NOTE: this is the one place where all the
4908 execution commands end up. We could alternatively do this in each
4909 of the execution commands in infcmd.c. */
4910 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4911 into infcmd.c in order to allow inferior function calls to work
4912 NOT asynchronously. */
4913 if (target_can_async_p ())
4914 target_async (inferior_event_handler
, 0);
4916 /* We've just told the target to resume. The remote server will
4917 wait for the inferior to stop, and then send a stop reply. In
4918 the mean time, we can't start another command/query ourselves
4919 because the stub wouldn't be ready to process it. This applies
4920 only to the base all-stop protocol, however. In non-stop (which
4921 only supports vCont), the stub replies with an "OK", and is
4922 immediate able to process further serial input. */
4924 rs
->waiting_for_stop_reply
= 1;
4928 /* Set up the signal handler for SIGINT, while the target is
4929 executing, ovewriting the 'regular' SIGINT signal handler. */
4931 async_initialize_sigint_signal_handler (void)
4933 signal (SIGINT
, async_handle_remote_sigint
);
4936 /* Signal handler for SIGINT, while the target is executing. */
4938 async_handle_remote_sigint (int sig
)
4940 signal (sig
, async_handle_remote_sigint_twice
);
4941 mark_async_signal_handler (async_sigint_remote_token
);
4944 /* Signal handler for SIGINT, installed after SIGINT has already been
4945 sent once. It will take effect the second time that the user sends
4948 async_handle_remote_sigint_twice (int sig
)
4950 signal (sig
, async_handle_remote_sigint
);
4951 mark_async_signal_handler (async_sigint_remote_twice_token
);
4954 /* Perform the real interruption of the target execution, in response
4957 async_remote_interrupt (gdb_client_data arg
)
4960 fprintf_unfiltered (gdb_stdlog
, "async_remote_interrupt called\n");
4962 target_stop (inferior_ptid
);
4965 /* Perform interrupt, if the first attempt did not succeed. Just give
4966 up on the target alltogether. */
4968 async_remote_interrupt_twice (gdb_client_data arg
)
4971 fprintf_unfiltered (gdb_stdlog
, "async_remote_interrupt_twice called\n");
4976 /* Reinstall the usual SIGINT handlers, after the target has
4979 async_cleanup_sigint_signal_handler (void *dummy
)
4981 signal (SIGINT
, handle_sigint
);
4984 /* Send ^C to target to halt it. Target will respond, and send us a
4986 static void (*ofunc
) (int);
4988 /* The command line interface's stop routine. This function is installed
4989 as a signal handler for SIGINT. The first time a user requests a
4990 stop, we call remote_stop to send a break or ^C. If there is no
4991 response from the target (it didn't stop when the user requested it),
4992 we ask the user if he'd like to detach from the target. */
4994 sync_remote_interrupt (int signo
)
4996 /* If this doesn't work, try more severe steps. */
4997 signal (signo
, sync_remote_interrupt_twice
);
4999 gdb_call_async_signal_handler (async_sigint_remote_token
, 1);
5002 /* The user typed ^C twice. */
5005 sync_remote_interrupt_twice (int signo
)
5007 signal (signo
, ofunc
);
5008 gdb_call_async_signal_handler (async_sigint_remote_twice_token
, 1);
5009 signal (signo
, sync_remote_interrupt
);
5012 /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
5013 thread, all threads of a remote process, or all threads of all
5017 remote_stop_ns (ptid_t ptid
)
5019 struct remote_state
*rs
= get_remote_state ();
5021 char *endp
= rs
->buf
+ get_remote_packet_size ();
5023 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
5024 remote_vcont_probe (rs
);
5026 if (!rs
->supports_vCont
.t
)
5027 error (_("Remote server does not support stopping threads"));
5029 if (ptid_equal (ptid
, minus_one_ptid
)
5030 || (!remote_multi_process_p (rs
) && ptid_is_pid (ptid
)))
5031 p
+= xsnprintf (p
, endp
- p
, "vCont;t");
5036 p
+= xsnprintf (p
, endp
- p
, "vCont;t:");
5038 if (ptid_is_pid (ptid
))
5039 /* All (-1) threads of process. */
5040 nptid
= ptid_build (ptid_get_pid (ptid
), 0, -1);
5043 /* Small optimization: if we already have a stop reply for
5044 this thread, no use in telling the stub we want this
5046 if (peek_stop_reply (ptid
))
5052 write_ptid (p
, endp
, nptid
);
5055 /* In non-stop, we get an immediate OK reply. The stop reply will
5056 come in asynchronously by notification. */
5058 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5059 if (strcmp (rs
->buf
, "OK") != 0)
5060 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid
), rs
->buf
);
5063 /* All-stop version of target_stop. Sends a break or a ^C to stop the
5064 remote target. It is undefined which thread of which process
5065 reports the stop. */
5068 remote_stop_as (ptid_t ptid
)
5070 struct remote_state
*rs
= get_remote_state ();
5072 rs
->ctrlc_pending_p
= 1;
5074 /* If the inferior is stopped already, but the core didn't know
5075 about it yet, just ignore the request. The cached wait status
5076 will be collected in remote_wait. */
5077 if (rs
->cached_wait_status
)
5080 /* Send interrupt_sequence to remote target. */
5081 send_interrupt_sequence ();
5084 /* This is the generic stop called via the target vector. When a target
5085 interrupt is requested, either by the command line or the GUI, we
5086 will eventually end up here. */
5089 remote_stop (struct target_ops
*self
, ptid_t ptid
)
5092 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
5095 remote_stop_ns (ptid
);
5097 remote_stop_as (ptid
);
5100 /* Ask the user what to do when an interrupt is received. */
5103 interrupt_query (void)
5105 target_terminal_ours ();
5107 if (target_can_async_p ())
5109 signal (SIGINT
, handle_sigint
);
5114 if (query (_("Interrupted while waiting for the program.\n\
5115 Give up (and stop debugging it)? ")))
5117 remote_unpush_target ();
5122 target_terminal_inferior ();
5125 /* Enable/disable target terminal ownership. Most targets can use
5126 terminal groups to control terminal ownership. Remote targets are
5127 different in that explicit transfer of ownership to/from GDB/target
5131 remote_terminal_inferior (struct target_ops
*self
)
5133 if (!target_async_permitted
)
5134 /* Nothing to do. */
5137 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5138 idempotent. The event-loop GDB talking to an asynchronous target
5139 with a synchronous command calls this function from both
5140 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5141 transfer the terminal to the target when it shouldn't this guard
5143 if (!remote_async_terminal_ours_p
)
5145 delete_file_handler (input_fd
);
5146 remote_async_terminal_ours_p
= 0;
5147 async_initialize_sigint_signal_handler ();
5148 /* NOTE: At this point we could also register our selves as the
5149 recipient of all input. Any characters typed could then be
5150 passed on down to the target. */
5154 remote_terminal_ours (struct target_ops
*self
)
5156 if (!target_async_permitted
)
5157 /* Nothing to do. */
5160 /* See FIXME in remote_terminal_inferior. */
5161 if (remote_async_terminal_ours_p
)
5163 async_cleanup_sigint_signal_handler (NULL
);
5164 add_file_handler (input_fd
, stdin_event_handler
, 0);
5165 remote_async_terminal_ours_p
= 1;
5169 remote_console_output (char *msg
)
5173 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
5176 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
5180 fputs_unfiltered (tb
, gdb_stdtarg
);
5182 gdb_flush (gdb_stdtarg
);
5185 typedef struct cached_reg
5188 gdb_byte data
[MAX_REGISTER_SIZE
];
5191 DEF_VEC_O(cached_reg_t
);
5193 typedef struct stop_reply
5195 struct notif_event base
;
5197 /* The identifier of the thread about this event */
5200 /* The remote state this event is associated with. When the remote
5201 connection, represented by a remote_state object, is closed,
5202 all the associated stop_reply events should be released. */
5203 struct remote_state
*rs
;
5205 struct target_waitstatus ws
;
5207 /* Expedited registers. This makes remote debugging a bit more
5208 efficient for those targets that provide critical registers as
5209 part of their normal status mechanism (as another roundtrip to
5210 fetch them is avoided). */
5211 VEC(cached_reg_t
) *regcache
;
5213 int stopped_by_watchpoint_p
;
5214 CORE_ADDR watch_data_address
;
5219 DECLARE_QUEUE_P (stop_reply_p
);
5220 DEFINE_QUEUE_P (stop_reply_p
);
5221 /* The list of already fetched and acknowledged stop events. This
5222 queue is used for notification Stop, and other notifications
5223 don't need queue for their events, because the notification events
5224 of Stop can't be consumed immediately, so that events should be
5225 queued first, and be consumed by remote_wait_{ns,as} one per
5226 time. Other notifications can consume their events immediately,
5227 so queue is not needed for them. */
5228 static QUEUE (stop_reply_p
) *stop_reply_queue
;
5231 stop_reply_xfree (struct stop_reply
*r
)
5233 notif_event_xfree ((struct notif_event
*) r
);
5237 remote_notif_stop_parse (struct notif_client
*self
, char *buf
,
5238 struct notif_event
*event
)
5240 remote_parse_stop_reply (buf
, (struct stop_reply
*) event
);
5244 remote_notif_stop_ack (struct notif_client
*self
, char *buf
,
5245 struct notif_event
*event
)
5247 struct stop_reply
*stop_reply
= (struct stop_reply
*) event
;
5250 putpkt ((char *) self
->ack_command
);
5252 if (stop_reply
->ws
.kind
== TARGET_WAITKIND_IGNORE
)
5253 /* We got an unknown stop reply. */
5254 error (_("Unknown stop reply"));
5256 push_stop_reply (stop_reply
);
5260 remote_notif_stop_can_get_pending_events (struct notif_client
*self
)
5262 /* We can't get pending events in remote_notif_process for
5263 notification stop, and we have to do this in remote_wait_ns
5264 instead. If we fetch all queued events from stub, remote stub
5265 may exit and we have no chance to process them back in
5267 mark_async_event_handler (remote_async_inferior_event_token
);
5272 stop_reply_dtr (struct notif_event
*event
)
5274 struct stop_reply
*r
= (struct stop_reply
*) event
;
5276 VEC_free (cached_reg_t
, r
->regcache
);
5279 static struct notif_event
*
5280 remote_notif_stop_alloc_reply (void)
5282 struct notif_event
*r
5283 = (struct notif_event
*) XNEW (struct stop_reply
);
5285 r
->dtr
= stop_reply_dtr
;
5290 /* A client of notification Stop. */
5292 struct notif_client notif_client_stop
=
5296 remote_notif_stop_parse
,
5297 remote_notif_stop_ack
,
5298 remote_notif_stop_can_get_pending_events
,
5299 remote_notif_stop_alloc_reply
,
5303 /* A parameter to pass data in and out. */
5305 struct queue_iter_param
5308 struct stop_reply
*output
;
5311 /* Remove stop replies in the queue if its pid is equal to the given
5315 remove_stop_reply_for_inferior (QUEUE (stop_reply_p
) *q
,
5316 QUEUE_ITER (stop_reply_p
) *iter
,
5320 struct queue_iter_param
*param
= data
;
5321 struct inferior
*inf
= param
->input
;
5323 if (ptid_get_pid (event
->ptid
) == inf
->pid
)
5325 stop_reply_xfree (event
);
5326 QUEUE_remove_elem (stop_reply_p
, q
, iter
);
5332 /* Discard all pending stop replies of inferior INF. */
5335 discard_pending_stop_replies (struct inferior
*inf
)
5338 struct queue_iter_param param
;
5339 struct stop_reply
*reply
;
5340 struct remote_state
*rs
= get_remote_state ();
5341 struct remote_notif_state
*rns
= rs
->notif_state
;
5343 /* This function can be notified when an inferior exists. When the
5344 target is not remote, the notification state is NULL. */
5345 if (rs
->remote_desc
== NULL
)
5348 reply
= (struct stop_reply
*) rns
->pending_event
[notif_client_stop
.id
];
5350 /* Discard the in-flight notification. */
5351 if (reply
!= NULL
&& ptid_get_pid (reply
->ptid
) == inf
->pid
)
5353 stop_reply_xfree (reply
);
5354 rns
->pending_event
[notif_client_stop
.id
] = NULL
;
5358 param
.output
= NULL
;
5359 /* Discard the stop replies we have already pulled with
5361 QUEUE_iterate (stop_reply_p
, stop_reply_queue
,
5362 remove_stop_reply_for_inferior
, ¶m
);
5365 /* If its remote state is equal to the given remote state,
5366 remove EVENT from the stop reply queue. */
5369 remove_stop_reply_of_remote_state (QUEUE (stop_reply_p
) *q
,
5370 QUEUE_ITER (stop_reply_p
) *iter
,
5374 struct queue_iter_param
*param
= data
;
5375 struct remote_state
*rs
= param
->input
;
5377 if (event
->rs
== rs
)
5379 stop_reply_xfree (event
);
5380 QUEUE_remove_elem (stop_reply_p
, q
, iter
);
5386 /* Discard the stop replies for RS in stop_reply_queue. */
5389 discard_pending_stop_replies_in_queue (struct remote_state
*rs
)
5391 struct queue_iter_param param
;
5394 param
.output
= NULL
;
5395 /* Discard the stop replies we have already pulled with
5397 QUEUE_iterate (stop_reply_p
, stop_reply_queue
,
5398 remove_stop_reply_of_remote_state
, ¶m
);
5401 /* A parameter to pass data in and out. */
5404 remote_notif_remove_once_on_match (QUEUE (stop_reply_p
) *q
,
5405 QUEUE_ITER (stop_reply_p
) *iter
,
5409 struct queue_iter_param
*param
= data
;
5410 ptid_t
*ptid
= param
->input
;
5412 if (ptid_match (event
->ptid
, *ptid
))
5414 param
->output
= event
;
5415 QUEUE_remove_elem (stop_reply_p
, q
, iter
);
5422 /* Remove the first reply in 'stop_reply_queue' which matches
5425 static struct stop_reply
*
5426 remote_notif_remove_queued_reply (ptid_t ptid
)
5428 struct queue_iter_param param
;
5430 param
.input
= &ptid
;
5431 param
.output
= NULL
;
5433 QUEUE_iterate (stop_reply_p
, stop_reply_queue
,
5434 remote_notif_remove_once_on_match
, ¶m
);
5436 fprintf_unfiltered (gdb_stdlog
,
5437 "notif: discard queued event: 'Stop' in %s\n",
5438 target_pid_to_str (ptid
));
5440 return param
.output
;
5443 /* Look for a queued stop reply belonging to PTID. If one is found,
5444 remove it from the queue, and return it. Returns NULL if none is
5445 found. If there are still queued events left to process, tell the
5446 event loop to get back to target_wait soon. */
5448 static struct stop_reply
*
5449 queued_stop_reply (ptid_t ptid
)
5451 struct stop_reply
*r
= remote_notif_remove_queued_reply (ptid
);
5453 if (!QUEUE_is_empty (stop_reply_p
, stop_reply_queue
))
5454 /* There's still at least an event left. */
5455 mark_async_event_handler (remote_async_inferior_event_token
);
5460 /* Push a fully parsed stop reply in the stop reply queue. Since we
5461 know that we now have at least one queued event left to pass to the
5462 core side, tell the event loop to get back to target_wait soon. */
5465 push_stop_reply (struct stop_reply
*new_event
)
5467 QUEUE_enque (stop_reply_p
, stop_reply_queue
, new_event
);
5470 fprintf_unfiltered (gdb_stdlog
,
5471 "notif: push 'Stop' %s to queue %d\n",
5472 target_pid_to_str (new_event
->ptid
),
5473 QUEUE_length (stop_reply_p
,
5476 mark_async_event_handler (remote_async_inferior_event_token
);
5480 stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p
) *q
,
5481 QUEUE_ITER (stop_reply_p
) *iter
,
5482 struct stop_reply
*event
,
5485 ptid_t
*ptid
= data
;
5487 return !(ptid_equal (*ptid
, event
->ptid
)
5488 && event
->ws
.kind
== TARGET_WAITKIND_STOPPED
);
5491 /* Returns true if we have a stop reply for PTID. */
5494 peek_stop_reply (ptid_t ptid
)
5496 return !QUEUE_iterate (stop_reply_p
, stop_reply_queue
,
5497 stop_reply_match_ptid_and_ws
, &ptid
);
5500 /* Parse the stop reply in BUF. Either the function succeeds, and the
5501 result is stored in EVENT, or throws an error. */
5504 remote_parse_stop_reply (char *buf
, struct stop_reply
*event
)
5506 struct remote_arch_state
*rsa
= get_remote_arch_state ();
5510 event
->ptid
= null_ptid
;
5511 event
->rs
= get_remote_state ();
5512 event
->ws
.kind
= TARGET_WAITKIND_IGNORE
;
5513 event
->ws
.value
.integer
= 0;
5514 event
->stopped_by_watchpoint_p
= 0;
5515 event
->regcache
= NULL
;
5520 case 'T': /* Status with PC, SP, FP, ... */
5521 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5522 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5524 n... = register number
5525 r... = register contents
5528 p
= &buf
[3]; /* after Txx */
5536 /* If the packet contains a register number, save it in
5537 pnum and set p1 to point to the character following it.
5538 Otherwise p1 points to p. */
5540 /* If this packet is an awatch packet, don't parse the 'a'
5541 as a register number. */
5543 if (strncmp (p
, "awatch", strlen("awatch")) != 0
5544 && strncmp (p
, "core", strlen ("core") != 0))
5546 /* Read the ``P'' register number. */
5547 pnum
= strtol (p
, &p_temp
, 16);
5553 if (p1
== p
) /* No register number present here. */
5555 p1
= strchr (p
, ':');
5557 error (_("Malformed packet(a) (missing colon): %s\n\
5560 if (strncmp (p
, "thread", p1
- p
) == 0)
5561 event
->ptid
= read_ptid (++p1
, &p
);
5562 else if ((strncmp (p
, "watch", p1
- p
) == 0)
5563 || (strncmp (p
, "rwatch", p1
- p
) == 0)
5564 || (strncmp (p
, "awatch", p1
- p
) == 0))
5566 event
->stopped_by_watchpoint_p
= 1;
5567 p
= unpack_varlen_hex (++p1
, &addr
);
5568 event
->watch_data_address
= (CORE_ADDR
) addr
;
5570 else if (strncmp (p
, "library", p1
- p
) == 0)
5574 while (*p_temp
&& *p_temp
!= ';')
5577 event
->ws
.kind
= TARGET_WAITKIND_LOADED
;
5580 else if (strncmp (p
, "replaylog", p1
- p
) == 0)
5582 event
->ws
.kind
= TARGET_WAITKIND_NO_HISTORY
;
5583 /* p1 will indicate "begin" or "end", but it makes
5584 no difference for now, so ignore it. */
5585 p_temp
= strchr (p1
+ 1, ';');
5589 else if (strncmp (p
, "core", p1
- p
) == 0)
5593 p
= unpack_varlen_hex (++p1
, &c
);
5598 /* Silently skip unknown optional info. */
5599 p_temp
= strchr (p1
+ 1, ';');
5606 struct packet_reg
*reg
= packet_reg_from_pnum (rsa
, pnum
);
5607 cached_reg_t cached_reg
;
5612 error (_("Malformed packet(b) (missing colon): %s\n\
5618 error (_("Remote sent bad register number %s: %s\n\
5620 hex_string (pnum
), p
, buf
);
5622 cached_reg
.num
= reg
->regnum
;
5624 fieldsize
= hex2bin (p
, cached_reg
.data
,
5625 register_size (target_gdbarch (),
5628 if (fieldsize
< register_size (target_gdbarch (),
5630 warning (_("Remote reply is too short: %s"), buf
);
5632 VEC_safe_push (cached_reg_t
, event
->regcache
, &cached_reg
);
5636 error (_("Remote register badly formatted: %s\nhere: %s"),
5641 if (event
->ws
.kind
!= TARGET_WAITKIND_IGNORE
)
5645 case 'S': /* Old style status, just signal only. */
5649 event
->ws
.kind
= TARGET_WAITKIND_STOPPED
;
5650 sig
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
5651 if (GDB_SIGNAL_FIRST
<= sig
&& sig
< GDB_SIGNAL_LAST
)
5652 event
->ws
.value
.sig
= (enum gdb_signal
) sig
;
5654 event
->ws
.value
.sig
= GDB_SIGNAL_UNKNOWN
;
5657 case 'W': /* Target exited. */
5664 /* GDB used to accept only 2 hex chars here. Stubs should
5665 only send more if they detect GDB supports multi-process
5667 p
= unpack_varlen_hex (&buf
[1], &value
);
5671 /* The remote process exited. */
5672 event
->ws
.kind
= TARGET_WAITKIND_EXITED
;
5673 event
->ws
.value
.integer
= value
;
5677 /* The remote process exited with a signal. */
5678 event
->ws
.kind
= TARGET_WAITKIND_SIGNALLED
;
5679 if (GDB_SIGNAL_FIRST
<= value
&& value
< GDB_SIGNAL_LAST
)
5680 event
->ws
.value
.sig
= (enum gdb_signal
) value
;
5682 event
->ws
.value
.sig
= GDB_SIGNAL_UNKNOWN
;
5685 /* If no process is specified, assume inferior_ptid. */
5686 pid
= ptid_get_pid (inferior_ptid
);
5695 else if (strncmp (p
,
5696 "process:", sizeof ("process:") - 1) == 0)
5700 p
+= sizeof ("process:") - 1;
5701 unpack_varlen_hex (p
, &upid
);
5705 error (_("unknown stop reply packet: %s"), buf
);
5708 error (_("unknown stop reply packet: %s"), buf
);
5709 event
->ptid
= pid_to_ptid (pid
);
5714 if (non_stop
&& ptid_equal (event
->ptid
, null_ptid
))
5715 error (_("No process or thread specified in stop reply: %s"), buf
);
5718 /* When the stub wants to tell GDB about a new notification reply, it
5719 sends a notification (%Stop, for example). Those can come it at
5720 any time, hence, we have to make sure that any pending
5721 putpkt/getpkt sequence we're making is finished, before querying
5722 the stub for more events with the corresponding ack command
5723 (vStopped, for example). E.g., if we started a vStopped sequence
5724 immediately upon receiving the notification, something like this
5732 1.6) <-- (registers reply to step #1.3)
5734 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5737 To solve this, whenever we parse a %Stop notification successfully,
5738 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5739 doing whatever we were doing:
5745 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5746 2.5) <-- (registers reply to step #2.3)
5748 Eventualy after step #2.5, we return to the event loop, which
5749 notices there's an event on the
5750 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5751 associated callback --- the function below. At this point, we're
5752 always safe to start a vStopped sequence. :
5755 2.7) <-- T05 thread:2
5761 remote_notif_get_pending_events (struct notif_client
*nc
)
5763 struct remote_state
*rs
= get_remote_state ();
5765 if (rs
->notif_state
->pending_event
[nc
->id
] != NULL
)
5768 fprintf_unfiltered (gdb_stdlog
,
5769 "notif: process: '%s' ack pending event\n",
5773 nc
->ack (nc
, rs
->buf
, rs
->notif_state
->pending_event
[nc
->id
]);
5774 rs
->notif_state
->pending_event
[nc
->id
] = NULL
;
5778 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
5779 if (strcmp (rs
->buf
, "OK") == 0)
5782 remote_notif_ack (nc
, rs
->buf
);
5788 fprintf_unfiltered (gdb_stdlog
,
5789 "notif: process: '%s' no pending reply\n",
5794 /* Called when it is decided that STOP_REPLY holds the info of the
5795 event that is to be returned to the core. This function always
5796 destroys STOP_REPLY. */
5799 process_stop_reply (struct stop_reply
*stop_reply
,
5800 struct target_waitstatus
*status
)
5804 *status
= stop_reply
->ws
;
5805 ptid
= stop_reply
->ptid
;
5807 /* If no thread/process was reported by the stub, assume the current
5809 if (ptid_equal (ptid
, null_ptid
))
5810 ptid
= inferior_ptid
;
5812 if (status
->kind
!= TARGET_WAITKIND_EXITED
5813 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
5815 struct remote_state
*rs
= get_remote_state ();
5817 /* Expedited registers. */
5818 if (stop_reply
->regcache
)
5820 struct regcache
*regcache
5821 = get_thread_arch_regcache (ptid
, target_gdbarch ());
5826 VEC_iterate(cached_reg_t
, stop_reply
->regcache
, ix
, reg
);
5828 regcache_raw_supply (regcache
, reg
->num
, reg
->data
);
5829 VEC_free (cached_reg_t
, stop_reply
->regcache
);
5832 rs
->remote_stopped_by_watchpoint_p
= stop_reply
->stopped_by_watchpoint_p
;
5833 rs
->remote_watch_data_address
= stop_reply
->watch_data_address
;
5835 remote_notice_new_inferior (ptid
, 0);
5836 demand_private_info (ptid
)->core
= stop_reply
->core
;
5839 stop_reply_xfree (stop_reply
);
5843 /* The non-stop mode version of target_wait. */
5846 remote_wait_ns (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5848 struct remote_state
*rs
= get_remote_state ();
5849 struct stop_reply
*stop_reply
;
5853 /* If in non-stop mode, get out of getpkt even if a
5854 notification is received. */
5856 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5857 0 /* forever */, &is_notif
);
5860 if (ret
!= -1 && !is_notif
)
5863 case 'E': /* Error of some sort. */
5864 /* We're out of sync with the target now. Did it continue
5865 or not? We can't tell which thread it was in non-stop,
5866 so just ignore this. */
5867 warning (_("Remote failure reply: %s"), rs
->buf
);
5869 case 'O': /* Console output. */
5870 remote_console_output (rs
->buf
+ 1);
5873 warning (_("Invalid remote reply: %s"), rs
->buf
);
5877 /* Acknowledge a pending stop reply that may have arrived in the
5879 if (rs
->notif_state
->pending_event
[notif_client_stop
.id
] != NULL
)
5880 remote_notif_get_pending_events (¬if_client_stop
);
5882 /* If indeed we noticed a stop reply, we're done. */
5883 stop_reply
= queued_stop_reply (ptid
);
5884 if (stop_reply
!= NULL
)
5885 return process_stop_reply (stop_reply
, status
);
5887 /* Still no event. If we're just polling for an event, then
5888 return to the event loop. */
5889 if (options
& TARGET_WNOHANG
)
5891 status
->kind
= TARGET_WAITKIND_IGNORE
;
5892 return minus_one_ptid
;
5895 /* Otherwise do a blocking wait. */
5896 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5897 1 /* forever */, &is_notif
);
5901 /* Wait until the remote machine stops, then return, storing status in
5902 STATUS just as `wait' would. */
5905 remote_wait_as (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
5907 struct remote_state
*rs
= get_remote_state ();
5908 ptid_t event_ptid
= null_ptid
;
5910 struct stop_reply
*stop_reply
;
5914 status
->kind
= TARGET_WAITKIND_IGNORE
;
5915 status
->value
.integer
= 0;
5917 stop_reply
= queued_stop_reply (ptid
);
5918 if (stop_reply
!= NULL
)
5919 return process_stop_reply (stop_reply
, status
);
5921 if (rs
->cached_wait_status
)
5922 /* Use the cached wait status, but only once. */
5923 rs
->cached_wait_status
= 0;
5929 if (!target_is_async_p ())
5931 ofunc
= signal (SIGINT
, sync_remote_interrupt
);
5932 /* If the user hit C-c before this packet, or between packets,
5933 pretend that it was hit right here. */
5934 if (check_quit_flag ())
5937 sync_remote_interrupt (SIGINT
);
5941 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5942 _never_ wait for ever -> test on target_is_async_p().
5943 However, before we do that we need to ensure that the caller
5944 knows how to take the target into/out of async mode. */
5945 ret
= getpkt_or_notif_sane (&rs
->buf
, &rs
->buf_size
,
5946 wait_forever_enabled_p
, &is_notif
);
5948 if (!target_is_async_p ())
5949 signal (SIGINT
, ofunc
);
5951 /* GDB gets a notification. Return to core as this event is
5953 if (ret
!= -1 && is_notif
)
5954 return minus_one_ptid
;
5959 rs
->remote_stopped_by_watchpoint_p
= 0;
5961 /* We got something. */
5962 rs
->waiting_for_stop_reply
= 0;
5964 /* Assume that the target has acknowledged Ctrl-C unless we receive
5965 an 'F' or 'O' packet. */
5966 if (buf
[0] != 'F' && buf
[0] != 'O')
5967 rs
->ctrlc_pending_p
= 0;
5971 case 'E': /* Error of some sort. */
5972 /* We're out of sync with the target now. Did it continue or
5973 not? Not is more likely, so report a stop. */
5974 warning (_("Remote failure reply: %s"), buf
);
5975 status
->kind
= TARGET_WAITKIND_STOPPED
;
5976 status
->value
.sig
= GDB_SIGNAL_0
;
5978 case 'F': /* File-I/O request. */
5979 remote_fileio_request (buf
, rs
->ctrlc_pending_p
);
5980 rs
->ctrlc_pending_p
= 0;
5982 case 'T': case 'S': case 'X': case 'W':
5984 struct stop_reply
*stop_reply
5985 = (struct stop_reply
*) remote_notif_parse (¬if_client_stop
,
5988 event_ptid
= process_stop_reply (stop_reply
, status
);
5991 case 'O': /* Console output. */
5992 remote_console_output (buf
+ 1);
5994 /* The target didn't really stop; keep waiting. */
5995 rs
->waiting_for_stop_reply
= 1;
5999 if (rs
->last_sent_signal
!= GDB_SIGNAL_0
)
6001 /* Zero length reply means that we tried 'S' or 'C' and the
6002 remote system doesn't support it. */
6003 target_terminal_ours_for_output ();
6005 ("Can't send signals to this remote system. %s not sent.\n",
6006 gdb_signal_to_name (rs
->last_sent_signal
));
6007 rs
->last_sent_signal
= GDB_SIGNAL_0
;
6008 target_terminal_inferior ();
6010 strcpy ((char *) buf
, rs
->last_sent_step
? "s" : "c");
6011 putpkt ((char *) buf
);
6013 /* We just told the target to resume, so a stop reply is in
6015 rs
->waiting_for_stop_reply
= 1;
6018 /* else fallthrough */
6020 warning (_("Invalid remote reply: %s"), buf
);
6022 rs
->waiting_for_stop_reply
= 1;
6026 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
6028 /* Nothing interesting happened. If we're doing a non-blocking
6029 poll, we're done. Otherwise, go back to waiting. */
6030 if (options
& TARGET_WNOHANG
)
6031 return minus_one_ptid
;
6035 else if (status
->kind
!= TARGET_WAITKIND_EXITED
6036 && status
->kind
!= TARGET_WAITKIND_SIGNALLED
)
6038 if (!ptid_equal (event_ptid
, null_ptid
))
6039 record_currthread (rs
, event_ptid
);
6041 event_ptid
= inferior_ptid
;
6044 /* A process exit. Invalidate our notion of current thread. */
6045 record_currthread (rs
, minus_one_ptid
);
6050 /* Wait until the remote machine stops, then return, storing status in
6051 STATUS just as `wait' would. */
6054 remote_wait (struct target_ops
*ops
,
6055 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
6060 event_ptid
= remote_wait_ns (ptid
, status
, options
);
6062 event_ptid
= remote_wait_as (ptid
, status
, options
);
6064 if (target_can_async_p ())
6066 /* If there are are events left in the queue tell the event loop
6068 if (!QUEUE_is_empty (stop_reply_p
, stop_reply_queue
))
6069 mark_async_event_handler (remote_async_inferior_event_token
);
6075 /* Fetch a single register using a 'p' packet. */
6078 fetch_register_using_p (struct regcache
*regcache
, struct packet_reg
*reg
)
6080 struct remote_state
*rs
= get_remote_state ();
6082 char regp
[MAX_REGISTER_SIZE
];
6085 if (remote_protocol_packets
[PACKET_p
].support
== PACKET_DISABLE
)
6088 if (reg
->pnum
== -1)
6093 p
+= hexnumstr (p
, reg
->pnum
);
6096 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6100 switch (packet_ok (buf
, &remote_protocol_packets
[PACKET_p
]))
6104 case PACKET_UNKNOWN
:
6107 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
6108 gdbarch_register_name (get_regcache_arch (regcache
),
6113 /* If this register is unfetchable, tell the regcache. */
6116 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
6120 /* Otherwise, parse and supply the value. */
6126 error (_("fetch_register_using_p: early buf termination"));
6128 regp
[i
++] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
6131 regcache_raw_supply (regcache
, reg
->regnum
, regp
);
6135 /* Fetch the registers included in the target's 'g' packet. */
6138 send_g_packet (void)
6140 struct remote_state
*rs
= get_remote_state ();
6143 xsnprintf (rs
->buf
, get_remote_packet_size (), "g");
6144 remote_send (&rs
->buf
, &rs
->buf_size
);
6146 /* We can get out of synch in various cases. If the first character
6147 in the buffer is not a hex character, assume that has happened
6148 and try to fetch another packet to read. */
6149 while ((rs
->buf
[0] < '0' || rs
->buf
[0] > '9')
6150 && (rs
->buf
[0] < 'A' || rs
->buf
[0] > 'F')
6151 && (rs
->buf
[0] < 'a' || rs
->buf
[0] > 'f')
6152 && rs
->buf
[0] != 'x') /* New: unavailable register value. */
6155 fprintf_unfiltered (gdb_stdlog
,
6156 "Bad register packet; fetching a new packet\n");
6157 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6160 buf_len
= strlen (rs
->buf
);
6162 /* Sanity check the received packet. */
6163 if (buf_len
% 2 != 0)
6164 error (_("Remote 'g' packet reply is of odd length: %s"), rs
->buf
);
6170 process_g_packet (struct regcache
*regcache
)
6172 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
6173 struct remote_state
*rs
= get_remote_state ();
6174 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6179 buf_len
= strlen (rs
->buf
);
6181 /* Further sanity checks, with knowledge of the architecture. */
6182 if (buf_len
> 2 * rsa
->sizeof_g_packet
)
6183 error (_("Remote 'g' packet reply is too long: %s"), rs
->buf
);
6185 /* Save the size of the packet sent to us by the target. It is used
6186 as a heuristic when determining the max size of packets that the
6187 target can safely receive. */
6188 if (rsa
->actual_register_packet_size
== 0)
6189 rsa
->actual_register_packet_size
= buf_len
;
6191 /* If this is smaller than we guessed the 'g' packet would be,
6192 update our records. A 'g' reply that doesn't include a register's
6193 value implies either that the register is not available, or that
6194 the 'p' packet must be used. */
6195 if (buf_len
< 2 * rsa
->sizeof_g_packet
)
6197 rsa
->sizeof_g_packet
= buf_len
/ 2;
6199 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
6201 if (rsa
->regs
[i
].pnum
== -1)
6204 if (rsa
->regs
[i
].offset
>= rsa
->sizeof_g_packet
)
6205 rsa
->regs
[i
].in_g_packet
= 0;
6207 rsa
->regs
[i
].in_g_packet
= 1;
6211 regs
= alloca (rsa
->sizeof_g_packet
);
6213 /* Unimplemented registers read as all bits zero. */
6214 memset (regs
, 0, rsa
->sizeof_g_packet
);
6216 /* Reply describes registers byte by byte, each byte encoded as two
6217 hex characters. Suck them all up, then supply them to the
6218 register cacheing/storage mechanism. */
6221 for (i
= 0; i
< rsa
->sizeof_g_packet
; i
++)
6223 if (p
[0] == 0 || p
[1] == 0)
6224 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
6225 internal_error (__FILE__
, __LINE__
,
6226 _("unexpected end of 'g' packet reply"));
6228 if (p
[0] == 'x' && p
[1] == 'x')
6229 regs
[i
] = 0; /* 'x' */
6231 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
6235 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
6237 struct packet_reg
*r
= &rsa
->regs
[i
];
6241 if (r
->offset
* 2 >= strlen (rs
->buf
))
6242 /* This shouldn't happen - we adjusted in_g_packet above. */
6243 internal_error (__FILE__
, __LINE__
,
6244 _("unexpected end of 'g' packet reply"));
6245 else if (rs
->buf
[r
->offset
* 2] == 'x')
6247 gdb_assert (r
->offset
* 2 < strlen (rs
->buf
));
6248 /* The register isn't available, mark it as such (at
6249 the same time setting the value to zero). */
6250 regcache_raw_supply (regcache
, r
->regnum
, NULL
);
6253 regcache_raw_supply (regcache
, r
->regnum
,
6260 fetch_registers_using_g (struct regcache
*regcache
)
6263 process_g_packet (regcache
);
6266 /* Make the remote selected traceframe match GDB's selected
6270 set_remote_traceframe (void)
6273 struct remote_state
*rs
= get_remote_state ();
6275 if (rs
->remote_traceframe_number
== get_traceframe_number ())
6278 /* Avoid recursion, remote_trace_find calls us again. */
6279 rs
->remote_traceframe_number
= get_traceframe_number ();
6281 newnum
= target_trace_find (tfind_number
,
6282 get_traceframe_number (), 0, 0, NULL
);
6284 /* Should not happen. If it does, all bets are off. */
6285 if (newnum
!= get_traceframe_number ())
6286 warning (_("could not set remote traceframe"));
6290 remote_fetch_registers (struct target_ops
*ops
,
6291 struct regcache
*regcache
, int regnum
)
6293 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6296 set_remote_traceframe ();
6297 set_general_thread (inferior_ptid
);
6301 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
6303 gdb_assert (reg
!= NULL
);
6305 /* If this register might be in the 'g' packet, try that first -
6306 we are likely to read more than one register. If this is the
6307 first 'g' packet, we might be overly optimistic about its
6308 contents, so fall back to 'p'. */
6309 if (reg
->in_g_packet
)
6311 fetch_registers_using_g (regcache
);
6312 if (reg
->in_g_packet
)
6316 if (fetch_register_using_p (regcache
, reg
))
6319 /* This register is not available. */
6320 regcache_raw_supply (regcache
, reg
->regnum
, NULL
);
6325 fetch_registers_using_g (regcache
);
6327 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6328 if (!rsa
->regs
[i
].in_g_packet
)
6329 if (!fetch_register_using_p (regcache
, &rsa
->regs
[i
]))
6331 /* This register is not available. */
6332 regcache_raw_supply (regcache
, i
, NULL
);
6336 /* Prepare to store registers. Since we may send them all (using a
6337 'G' request), we have to read out the ones we don't want to change
6341 remote_prepare_to_store (struct target_ops
*self
, struct regcache
*regcache
)
6343 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6345 gdb_byte buf
[MAX_REGISTER_SIZE
];
6347 /* Make sure the entire registers array is valid. */
6348 switch (remote_protocol_packets
[PACKET_P
].support
)
6350 case PACKET_DISABLE
:
6351 case PACKET_SUPPORT_UNKNOWN
:
6352 /* Make sure all the necessary registers are cached. */
6353 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6354 if (rsa
->regs
[i
].in_g_packet
)
6355 regcache_raw_read (regcache
, rsa
->regs
[i
].regnum
, buf
);
6362 /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
6363 packet was not recognized. */
6366 store_register_using_P (const struct regcache
*regcache
,
6367 struct packet_reg
*reg
)
6369 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
6370 struct remote_state
*rs
= get_remote_state ();
6371 /* Try storing a single register. */
6372 char *buf
= rs
->buf
;
6373 gdb_byte regp
[MAX_REGISTER_SIZE
];
6376 if (remote_protocol_packets
[PACKET_P
].support
== PACKET_DISABLE
)
6379 if (reg
->pnum
== -1)
6382 xsnprintf (buf
, get_remote_packet_size (), "P%s=", phex_nz (reg
->pnum
, 0));
6383 p
= buf
+ strlen (buf
);
6384 regcache_raw_collect (regcache
, reg
->regnum
, regp
);
6385 bin2hex (regp
, p
, register_size (gdbarch
, reg
->regnum
));
6387 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6389 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_P
]))
6394 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6395 gdbarch_register_name (gdbarch
, reg
->regnum
), rs
->buf
);
6396 case PACKET_UNKNOWN
:
6399 internal_error (__FILE__
, __LINE__
, _("Bad result from packet_ok"));
6403 /* Store register REGNUM, or all registers if REGNUM == -1, from the
6404 contents of the register cache buffer. FIXME: ignores errors. */
6407 store_registers_using_G (const struct regcache
*regcache
)
6409 struct remote_state
*rs
= get_remote_state ();
6410 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6414 /* Extract all the registers in the regcache copying them into a
6419 regs
= alloca (rsa
->sizeof_g_packet
);
6420 memset (regs
, 0, rsa
->sizeof_g_packet
);
6421 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6423 struct packet_reg
*r
= &rsa
->regs
[i
];
6426 regcache_raw_collect (regcache
, r
->regnum
, regs
+ r
->offset
);
6430 /* Command describes registers byte by byte,
6431 each byte encoded as two hex characters. */
6434 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6436 bin2hex (regs
, p
, rsa
->sizeof_g_packet
);
6438 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6439 if (packet_check_result (rs
->buf
) == PACKET_ERROR
)
6440 error (_("Could not write registers; remote failure reply '%s'"),
6444 /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6445 of the register cache buffer. FIXME: ignores errors. */
6448 remote_store_registers (struct target_ops
*ops
,
6449 struct regcache
*regcache
, int regnum
)
6451 struct remote_arch_state
*rsa
= get_remote_arch_state ();
6454 set_remote_traceframe ();
6455 set_general_thread (inferior_ptid
);
6459 struct packet_reg
*reg
= packet_reg_from_regnum (rsa
, regnum
);
6461 gdb_assert (reg
!= NULL
);
6463 /* Always prefer to store registers using the 'P' packet if
6464 possible; we often change only a small number of registers.
6465 Sometimes we change a larger number; we'd need help from a
6466 higher layer to know to use 'G'. */
6467 if (store_register_using_P (regcache
, reg
))
6470 /* For now, don't complain if we have no way to write the
6471 register. GDB loses track of unavailable registers too
6472 easily. Some day, this may be an error. We don't have
6473 any way to read the register, either... */
6474 if (!reg
->in_g_packet
)
6477 store_registers_using_G (regcache
);
6481 store_registers_using_G (regcache
);
6483 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
6484 if (!rsa
->regs
[i
].in_g_packet
)
6485 if (!store_register_using_P (regcache
, &rsa
->regs
[i
]))
6486 /* See above for why we do not issue an error here. */
6491 /* Return the number of hex digits in num. */
6494 hexnumlen (ULONGEST num
)
6498 for (i
= 0; num
!= 0; i
++)
6504 /* Set BUF to the minimum number of hex digits representing NUM. */
6507 hexnumstr (char *buf
, ULONGEST num
)
6509 int len
= hexnumlen (num
);
6511 return hexnumnstr (buf
, num
, len
);
6515 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
6518 hexnumnstr (char *buf
, ULONGEST num
, int width
)
6524 for (i
= width
- 1; i
>= 0; i
--)
6526 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
6533 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
6536 remote_address_masked (CORE_ADDR addr
)
6538 unsigned int address_size
= remote_address_size
;
6540 /* If "remoteaddresssize" was not set, default to target address size. */
6542 address_size
= gdbarch_addr_bit (target_gdbarch ());
6544 if (address_size
> 0
6545 && address_size
< (sizeof (ULONGEST
) * 8))
6547 /* Only create a mask when that mask can safely be constructed
6548 in a ULONGEST variable. */
6551 mask
= (mask
<< address_size
) - 1;
6557 /* Determine whether the remote target supports binary downloading.
6558 This is accomplished by sending a no-op memory write of zero length
6559 to the target at the specified address. It does not suffice to send
6560 the whole packet, since many stubs strip the eighth bit and
6561 subsequently compute a wrong checksum, which causes real havoc with
6564 NOTE: This can still lose if the serial line is not eight-bit
6565 clean. In cases like this, the user should clear "remote
6569 check_binary_download (CORE_ADDR addr
)
6571 struct remote_state
*rs
= get_remote_state ();
6573 switch (remote_protocol_packets
[PACKET_X
].support
)
6575 case PACKET_DISABLE
:
6579 case PACKET_SUPPORT_UNKNOWN
:
6585 p
+= hexnumstr (p
, (ULONGEST
) addr
);
6587 p
+= hexnumstr (p
, (ULONGEST
) 0);
6591 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6592 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6594 if (rs
->buf
[0] == '\0')
6597 fprintf_unfiltered (gdb_stdlog
,
6598 "binary downloading NOT "
6599 "supported by target\n");
6600 remote_protocol_packets
[PACKET_X
].support
= PACKET_DISABLE
;
6605 fprintf_unfiltered (gdb_stdlog
,
6606 "binary downloading supported by target\n");
6607 remote_protocol_packets
[PACKET_X
].support
= PACKET_ENABLE
;
6614 /* Write memory data directly to the remote machine.
6615 This does not inform the data cache; the data cache uses this.
6616 HEADER is the starting part of the packet.
6617 MEMADDR is the address in the remote memory space.
6618 MYADDR is the address of the buffer in our space.
6619 LEN is the number of bytes.
6620 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6621 should send data as binary ('X'), or hex-encoded ('M').
6623 The function creates packet of the form
6624 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6626 where encoding of <DATA> is termined by PACKET_FORMAT.
6628 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6631 Return the transferred status, error or OK (an
6632 'enum target_xfer_status' value). Save the number of bytes
6633 transferred in *XFERED_LEN. Only transfer a single packet. */
6635 static enum target_xfer_status
6636 remote_write_bytes_aux (const char *header
, CORE_ADDR memaddr
,
6637 const gdb_byte
*myaddr
, ULONGEST len
,
6638 ULONGEST
*xfered_len
, char packet_format
,
6641 struct remote_state
*rs
= get_remote_state ();
6651 if (packet_format
!= 'X' && packet_format
!= 'M')
6652 internal_error (__FILE__
, __LINE__
,
6653 _("remote_write_bytes_aux: bad packet format"));
6656 return TARGET_XFER_EOF
;
6658 payload_size
= get_memory_write_packet_size ();
6660 /* The packet buffer will be large enough for the payload;
6661 get_memory_packet_size ensures this. */
6664 /* Compute the size of the actual payload by subtracting out the
6665 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6667 payload_size
-= strlen ("$,:#NN");
6669 /* The comma won't be used. */
6671 header_length
= strlen (header
);
6672 payload_size
-= header_length
;
6673 payload_size
-= hexnumlen (memaddr
);
6675 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
6677 strcat (rs
->buf
, header
);
6678 p
= rs
->buf
+ strlen (header
);
6680 /* Compute a best guess of the number of bytes actually transfered. */
6681 if (packet_format
== 'X')
6683 /* Best guess at number of bytes that will fit. */
6684 todo
= min (len
, payload_size
);
6686 payload_size
-= hexnumlen (todo
);
6687 todo
= min (todo
, payload_size
);
6691 /* Num bytes that will fit. */
6692 todo
= min (len
, payload_size
/ 2);
6694 payload_size
-= hexnumlen (todo
);
6695 todo
= min (todo
, payload_size
/ 2);
6699 internal_error (__FILE__
, __LINE__
,
6700 _("minimum packet size too small to write data"));
6702 /* If we already need another packet, then try to align the end
6703 of this packet to a useful boundary. */
6704 if (todo
> 2 * REMOTE_ALIGN_WRITES
&& todo
< len
)
6705 todo
= ((memaddr
+ todo
) & ~(REMOTE_ALIGN_WRITES
- 1)) - memaddr
;
6707 /* Append "<memaddr>". */
6708 memaddr
= remote_address_masked (memaddr
);
6709 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6716 /* Append <len>. Retain the location/size of <len>. It may need to
6717 be adjusted once the packet body has been created. */
6719 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
6727 /* Append the packet body. */
6728 if (packet_format
== 'X')
6730 /* Binary mode. Send target system values byte by byte, in
6731 increasing byte addresses. Only escape certain critical
6733 payload_length
= remote_escape_output (myaddr
, todo
, (gdb_byte
*) p
,
6734 &nr_bytes
, payload_size
);
6736 /* If not all TODO bytes fit, then we'll need another packet. Make
6737 a second try to keep the end of the packet aligned. Don't do
6738 this if the packet is tiny. */
6739 if (nr_bytes
< todo
&& nr_bytes
> 2 * REMOTE_ALIGN_WRITES
)
6743 new_nr_bytes
= (((memaddr
+ nr_bytes
) & ~(REMOTE_ALIGN_WRITES
- 1))
6745 if (new_nr_bytes
!= nr_bytes
)
6746 payload_length
= remote_escape_output (myaddr
, new_nr_bytes
,
6747 (gdb_byte
*) p
, &nr_bytes
,
6751 p
+= payload_length
;
6752 if (use_length
&& nr_bytes
< todo
)
6754 /* Escape chars have filled up the buffer prematurely,
6755 and we have actually sent fewer bytes than planned.
6756 Fix-up the length field of the packet. Use the same
6757 number of characters as before. */
6758 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
6759 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
6764 /* Normal mode: Send target system values byte by byte, in
6765 increasing byte addresses. Each byte is encoded as a two hex
6767 nr_bytes
= bin2hex (myaddr
, p
, todo
);
6771 putpkt_binary (rs
->buf
, (int) (p
- rs
->buf
));
6772 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6774 if (rs
->buf
[0] == 'E')
6775 return TARGET_XFER_E_IO
;
6777 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6778 fewer bytes than we'd planned. */
6779 *xfered_len
= (ULONGEST
) nr_bytes
;
6780 return TARGET_XFER_OK
;
6783 /* Write memory data directly to the remote machine.
6784 This does not inform the data cache; the data cache uses this.
6785 MEMADDR is the address in the remote memory space.
6786 MYADDR is the address of the buffer in our space.
6787 LEN is the number of bytes.
6789 Return the transferred status, error or OK (an
6790 'enum target_xfer_status' value). Save the number of bytes
6791 transferred in *XFERED_LEN. Only transfer a single packet. */
6793 static enum target_xfer_status
6794 remote_write_bytes (CORE_ADDR memaddr
, const gdb_byte
*myaddr
, ULONGEST len
,
6795 ULONGEST
*xfered_len
)
6797 char *packet_format
= 0;
6799 /* Check whether the target supports binary download. */
6800 check_binary_download (memaddr
);
6802 switch (remote_protocol_packets
[PACKET_X
].support
)
6805 packet_format
= "X";
6807 case PACKET_DISABLE
:
6808 packet_format
= "M";
6810 case PACKET_SUPPORT_UNKNOWN
:
6811 internal_error (__FILE__
, __LINE__
,
6812 _("remote_write_bytes: bad internal state"));
6814 internal_error (__FILE__
, __LINE__
, _("bad switch"));
6817 return remote_write_bytes_aux (packet_format
,
6818 memaddr
, myaddr
, len
, xfered_len
,
6819 packet_format
[0], 1);
6822 /* Read memory data directly from the remote machine.
6823 This does not use the data cache; the data cache uses this.
6824 MEMADDR is the address in the remote memory space.
6825 MYADDR is the address of the buffer in our space.
6826 LEN is the number of bytes.
6828 Return the transferred status, error or OK (an
6829 'enum target_xfer_status' value). Save the number of bytes
6830 transferred in *XFERED_LEN. */
6832 static enum target_xfer_status
6833 remote_read_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, ULONGEST len
,
6834 ULONGEST
*xfered_len
)
6836 struct remote_state
*rs
= get_remote_state ();
6837 int max_buf_size
; /* Max size of packet output buffer. */
6845 max_buf_size
= get_memory_read_packet_size ();
6846 /* The packet buffer will be large enough for the payload;
6847 get_memory_packet_size ensures this. */
6849 /* Number if bytes that will fit. */
6850 todo
= min (len
, max_buf_size
/ 2);
6852 /* Construct "m"<memaddr>","<len>". */
6853 memaddr
= remote_address_masked (memaddr
);
6856 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
6858 p
+= hexnumstr (p
, (ULONGEST
) todo
);
6861 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6862 if (rs
->buf
[0] == 'E'
6863 && isxdigit (rs
->buf
[1]) && isxdigit (rs
->buf
[2])
6864 && rs
->buf
[3] == '\0')
6865 return TARGET_XFER_E_IO
;
6866 /* Reply describes memory byte by byte, each byte encoded as two hex
6869 i
= hex2bin (p
, myaddr
, todo
);
6870 /* Return what we have. Let higher layers handle partial reads. */
6871 *xfered_len
= (ULONGEST
) i
;
6872 return TARGET_XFER_OK
;
6877 /* Sends a packet with content determined by the printf format string
6878 FORMAT and the remaining arguments, then gets the reply. Returns
6879 whether the packet was a success, a failure, or unknown. */
6881 static enum packet_result
6882 remote_send_printf (const char *format
, ...)
6884 struct remote_state
*rs
= get_remote_state ();
6885 int max_size
= get_remote_packet_size ();
6888 va_start (ap
, format
);
6891 if (vsnprintf (rs
->buf
, max_size
, format
, ap
) >= max_size
)
6892 internal_error (__FILE__
, __LINE__
, _("Too long remote packet."));
6894 if (putpkt (rs
->buf
) < 0)
6895 error (_("Communication problem with target."));
6898 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
6900 return packet_check_result (rs
->buf
);
6904 restore_remote_timeout (void *p
)
6906 int value
= *(int *)p
;
6908 remote_timeout
= value
;
6911 /* Flash writing can take quite some time. We'll set
6912 effectively infinite timeout for flash operations.
6913 In future, we'll need to decide on a better approach. */
6914 static const int remote_flash_timeout
= 1000;
6917 remote_flash_erase (struct target_ops
*ops
,
6918 ULONGEST address
, LONGEST length
)
6920 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
6921 int saved_remote_timeout
= remote_timeout
;
6922 enum packet_result ret
;
6923 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6924 &saved_remote_timeout
);
6926 remote_timeout
= remote_flash_timeout
;
6928 ret
= remote_send_printf ("vFlashErase:%s,%s",
6929 phex (address
, addr_size
),
6933 case PACKET_UNKNOWN
:
6934 error (_("Remote target does not support flash erase"));
6936 error (_("Error erasing flash with vFlashErase packet"));
6941 do_cleanups (back_to
);
6944 static enum target_xfer_status
6945 remote_flash_write (struct target_ops
*ops
, ULONGEST address
,
6946 ULONGEST length
, ULONGEST
*xfered_len
,
6947 const gdb_byte
*data
)
6949 int saved_remote_timeout
= remote_timeout
;
6950 enum target_xfer_status ret
;
6951 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6952 &saved_remote_timeout
);
6954 remote_timeout
= remote_flash_timeout
;
6955 ret
= remote_write_bytes_aux ("vFlashWrite:", address
, data
, length
,
6957 do_cleanups (back_to
);
6963 remote_flash_done (struct target_ops
*ops
)
6965 int saved_remote_timeout
= remote_timeout
;
6967 struct cleanup
*back_to
= make_cleanup (restore_remote_timeout
,
6968 &saved_remote_timeout
);
6970 remote_timeout
= remote_flash_timeout
;
6971 ret
= remote_send_printf ("vFlashDone");
6972 do_cleanups (back_to
);
6976 case PACKET_UNKNOWN
:
6977 error (_("Remote target does not support vFlashDone"));
6979 error (_("Error finishing flash operation"));
6986 remote_files_info (struct target_ops
*ignore
)
6988 puts_filtered ("Debugging a target over a serial line.\n");
6991 /* Stuff for dealing with the packets which are part of this protocol.
6992 See comment at top of file for details. */
6994 /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
6995 error to higher layers. Called when a serial error is detected.
6996 The exception message is STRING, followed by a colon and a blank,
6997 the system error message for errno at function entry and final dot
6998 for output compatibility with throw_perror_with_name. */
7001 unpush_and_perror (const char *string
)
7003 int saved_errno
= errno
;
7005 remote_unpush_target ();
7006 throw_error (TARGET_CLOSE_ERROR
, "%s: %s.", string
,
7007 safe_strerror (saved_errno
));
7010 /* Read a single character from the remote end. */
7013 readchar (int timeout
)
7016 struct remote_state
*rs
= get_remote_state ();
7018 ch
= serial_readchar (rs
->remote_desc
, timeout
);
7023 switch ((enum serial_rc
) ch
)
7026 remote_unpush_target ();
7027 throw_error (TARGET_CLOSE_ERROR
, _("Remote connection closed"));
7030 unpush_and_perror (_("Remote communication error. "
7031 "Target disconnected."));
7033 case SERIAL_TIMEOUT
:
7039 /* Wrapper for serial_write that closes the target and throws if
7043 remote_serial_write (const char *str
, int len
)
7045 struct remote_state
*rs
= get_remote_state ();
7047 if (serial_write (rs
->remote_desc
, str
, len
))
7049 unpush_and_perror (_("Remote communication error. "
7050 "Target disconnected."));
7054 /* Send the command in *BUF to the remote machine, and read the reply
7055 into *BUF. Report an error if we get an error reply. Resize
7056 *BUF using xrealloc if necessary to hold the result, and update
7060 remote_send (char **buf
,
7064 getpkt (buf
, sizeof_buf
, 0);
7066 if ((*buf
)[0] == 'E')
7067 error (_("Remote failure reply: %s"), *buf
);
7070 /* Return a pointer to an xmalloc'ed string representing an escaped
7071 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
7072 etc. The caller is responsible for releasing the returned
7076 escape_buffer (const char *buf
, int n
)
7078 struct cleanup
*old_chain
;
7079 struct ui_file
*stb
;
7082 stb
= mem_fileopen ();
7083 old_chain
= make_cleanup_ui_file_delete (stb
);
7085 fputstrn_unfiltered (buf
, n
, 0, stb
);
7086 str
= ui_file_xstrdup (stb
, NULL
);
7087 do_cleanups (old_chain
);
7091 /* Display a null-terminated packet on stdout, for debugging, using C
7095 print_packet (char *buf
)
7097 puts_filtered ("\"");
7098 fputstr_filtered (buf
, '"', gdb_stdout
);
7099 puts_filtered ("\"");
7105 return putpkt_binary (buf
, strlen (buf
));
7108 /* Send a packet to the remote machine, with error checking. The data
7109 of the packet is in BUF. The string in BUF can be at most
7110 get_remote_packet_size () - 5 to account for the $, # and checksum,
7111 and for a possible /0 if we are debugging (remote_debug) and want
7112 to print the sent packet as a string. */
7115 putpkt_binary (char *buf
, int cnt
)
7117 struct remote_state
*rs
= get_remote_state ();
7119 unsigned char csum
= 0;
7120 char *buf2
= alloca (cnt
+ 6);
7127 /* Catch cases like trying to read memory or listing threads while
7128 we're waiting for a stop reply. The remote server wouldn't be
7129 ready to handle this request, so we'd hang and timeout. We don't
7130 have to worry about this in synchronous mode, because in that
7131 case it's not possible to issue a command while the target is
7132 running. This is not a problem in non-stop mode, because in that
7133 case, the stub is always ready to process serial input. */
7134 if (!non_stop
&& target_can_async_p () && rs
->waiting_for_stop_reply
)
7135 error (_("Cannot execute this command while the target is running."));
7137 /* We're sending out a new packet. Make sure we don't look at a
7138 stale cached response. */
7139 rs
->cached_wait_status
= 0;
7141 /* Copy the packet into buffer BUF2, encapsulating it
7142 and giving it a checksum. */
7147 for (i
= 0; i
< cnt
; i
++)
7153 *p
++ = tohex ((csum
>> 4) & 0xf);
7154 *p
++ = tohex (csum
& 0xf);
7156 /* Send it over and over until we get a positive ack. */
7160 int started_error_output
= 0;
7164 struct cleanup
*old_chain
;
7168 str
= escape_buffer (buf2
, p
- buf2
);
7169 old_chain
= make_cleanup (xfree
, str
);
7170 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", str
);
7171 gdb_flush (gdb_stdlog
);
7172 do_cleanups (old_chain
);
7174 remote_serial_write (buf2
, p
- buf2
);
7176 /* If this is a no acks version of the remote protocol, send the
7177 packet and move on. */
7181 /* Read until either a timeout occurs (-2) or '+' is read.
7182 Handle any notification that arrives in the mean time. */
7185 ch
= readchar (remote_timeout
);
7193 case SERIAL_TIMEOUT
:
7196 if (started_error_output
)
7198 putchar_unfiltered ('\n');
7199 started_error_output
= 0;
7208 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
7212 fprintf_unfiltered (gdb_stdlog
, "Nak\n");
7214 case SERIAL_TIMEOUT
:
7218 break; /* Retransmit buffer. */
7222 fprintf_unfiltered (gdb_stdlog
,
7223 "Packet instead of Ack, ignoring it\n");
7224 /* It's probably an old response sent because an ACK
7225 was lost. Gobble up the packet and ack it so it
7226 doesn't get retransmitted when we resend this
7229 remote_serial_write ("+", 1);
7230 continue; /* Now, go look for +. */
7237 /* If we got a notification, handle it, and go back to looking
7239 /* We've found the start of a notification. Now
7240 collect the data. */
7241 val
= read_frame (&rs
->buf
, &rs
->buf_size
);
7246 struct cleanup
*old_chain
;
7249 str
= escape_buffer (rs
->buf
, val
);
7250 old_chain
= make_cleanup (xfree
, str
);
7251 fprintf_unfiltered (gdb_stdlog
,
7252 " Notification received: %s\n",
7254 do_cleanups (old_chain
);
7256 handle_notification (rs
->notif_state
, rs
->buf
);
7257 /* We're in sync now, rewait for the ack. */
7264 if (!started_error_output
)
7266 started_error_output
= 1;
7267 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
7269 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
7270 fprintf_unfiltered (gdb_stdlog
, "%s", rs
->buf
);
7279 if (!started_error_output
)
7281 started_error_output
= 1;
7282 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
7284 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
7288 break; /* Here to retransmit. */
7292 /* This is wrong. If doing a long backtrace, the user should be
7293 able to get out next time we call QUIT, without anything as
7294 violent as interrupt_query. If we want to provide a way out of
7295 here without getting to the next QUIT, it should be based on
7296 hitting ^C twice as in remote_wait. */
7307 /* Come here after finding the start of a frame when we expected an
7308 ack. Do our best to discard the rest of this packet. */
7317 c
= readchar (remote_timeout
);
7320 case SERIAL_TIMEOUT
:
7321 /* Nothing we can do. */
7324 /* Discard the two bytes of checksum and stop. */
7325 c
= readchar (remote_timeout
);
7327 c
= readchar (remote_timeout
);
7330 case '*': /* Run length encoding. */
7331 /* Discard the repeat count. */
7332 c
= readchar (remote_timeout
);
7337 /* A regular character. */
7343 /* Come here after finding the start of the frame. Collect the rest
7344 into *BUF, verifying the checksum, length, and handling run-length
7345 compression. NUL terminate the buffer. If there is not enough room,
7346 expand *BUF using xrealloc.
7348 Returns -1 on error, number of characters in buffer (ignoring the
7349 trailing NULL) on success. (could be extended to return one of the
7350 SERIAL status indications). */
7353 read_frame (char **buf_p
,
7360 struct remote_state
*rs
= get_remote_state ();
7367 c
= readchar (remote_timeout
);
7370 case SERIAL_TIMEOUT
:
7372 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
7376 fputs_filtered ("Saw new packet start in middle of old one\n",
7378 return -1; /* Start a new packet, count retries. */
7381 unsigned char pktcsum
;
7387 check_0
= readchar (remote_timeout
);
7389 check_1
= readchar (remote_timeout
);
7391 if (check_0
== SERIAL_TIMEOUT
|| check_1
== SERIAL_TIMEOUT
)
7394 fputs_filtered ("Timeout in checksum, retrying\n",
7398 else if (check_0
< 0 || check_1
< 0)
7401 fputs_filtered ("Communication error in checksum\n",
7406 /* Don't recompute the checksum; with no ack packets we
7407 don't have any way to indicate a packet retransmission
7412 pktcsum
= (fromhex (check_0
) << 4) | fromhex (check_1
);
7413 if (csum
== pktcsum
)
7418 struct cleanup
*old_chain
;
7421 str
= escape_buffer (buf
, bc
);
7422 old_chain
= make_cleanup (xfree
, str
);
7423 fprintf_unfiltered (gdb_stdlog
,
7424 "Bad checksum, sentsum=0x%x, "
7425 "csum=0x%x, buf=%s\n",
7426 pktcsum
, csum
, str
);
7427 do_cleanups (old_chain
);
7429 /* Number of characters in buffer ignoring trailing
7433 case '*': /* Run length encoding. */
7438 c
= readchar (remote_timeout
);
7440 repeat
= c
- ' ' + 3; /* Compute repeat count. */
7442 /* The character before ``*'' is repeated. */
7444 if (repeat
> 0 && repeat
<= 255 && bc
> 0)
7446 if (bc
+ repeat
- 1 >= *sizeof_buf
- 1)
7448 /* Make some more room in the buffer. */
7449 *sizeof_buf
+= repeat
;
7450 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7454 memset (&buf
[bc
], buf
[bc
- 1], repeat
);
7460 printf_filtered (_("Invalid run length encoding: %s\n"), buf
);
7464 if (bc
>= *sizeof_buf
- 1)
7466 /* Make some more room in the buffer. */
7468 *buf_p
= xrealloc (*buf_p
, *sizeof_buf
);
7479 /* Read a packet from the remote machine, with error checking, and
7480 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7481 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7482 rather than timing out; this is used (in synchronous mode) to wait
7483 for a target that is is executing user code to stop. */
7484 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7485 don't have to change all the calls to getpkt to deal with the
7486 return value, because at the moment I don't know what the right
7487 thing to do it for those. */
7495 timed_out
= getpkt_sane (buf
, sizeof_buf
, forever
);
7499 /* Read a packet from the remote machine, with error checking, and
7500 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7501 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7502 rather than timing out; this is used (in synchronous mode) to wait
7503 for a target that is is executing user code to stop. If FOREVER ==
7504 0, this function is allowed to time out gracefully and return an
7505 indication of this to the caller. Otherwise return the number of
7506 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7507 enough reason to return to the caller. *IS_NOTIF is an output
7508 boolean that indicates whether *BUF holds a notification or not
7509 (a regular packet). */
7512 getpkt_or_notif_sane_1 (char **buf
, long *sizeof_buf
, int forever
,
7513 int expecting_notif
, int *is_notif
)
7515 struct remote_state
*rs
= get_remote_state ();
7521 /* We're reading a new response. Make sure we don't look at a
7522 previously cached response. */
7523 rs
->cached_wait_status
= 0;
7525 strcpy (*buf
, "timeout");
7528 timeout
= watchdog
> 0 ? watchdog
: -1;
7529 else if (expecting_notif
)
7530 timeout
= 0; /* There should already be a char in the buffer. If
7533 timeout
= remote_timeout
;
7537 /* Process any number of notifications, and then return when
7541 /* If we get a timeout or bad checksum, retry up to MAX_TRIES
7543 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
7545 /* This can loop forever if the remote side sends us
7546 characters continuously, but if it pauses, we'll get
7547 SERIAL_TIMEOUT from readchar because of timeout. Then
7548 we'll count that as a retry.
7550 Note that even when forever is set, we will only wait
7551 forever prior to the start of a packet. After that, we
7552 expect characters to arrive at a brisk pace. They should
7553 show up within remote_timeout intervals. */
7555 c
= readchar (timeout
);
7556 while (c
!= SERIAL_TIMEOUT
&& c
!= '$' && c
!= '%');
7558 if (c
== SERIAL_TIMEOUT
)
7560 if (expecting_notif
)
7561 return -1; /* Don't complain, it's normal to not get
7562 anything in this case. */
7564 if (forever
) /* Watchdog went off? Kill the target. */
7567 remote_unpush_target ();
7568 throw_error (TARGET_CLOSE_ERROR
,
7569 _("Watchdog timeout has expired. "
7570 "Target detached."));
7573 fputs_filtered ("Timed out.\n", gdb_stdlog
);
7577 /* We've found the start of a packet or notification.
7578 Now collect the data. */
7579 val
= read_frame (buf
, sizeof_buf
);
7584 remote_serial_write ("-", 1);
7587 if (tries
> MAX_TRIES
)
7589 /* We have tried hard enough, and just can't receive the
7590 packet/notification. Give up. */
7591 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
7593 /* Skip the ack char if we're in no-ack mode. */
7594 if (!rs
->noack_mode
)
7595 remote_serial_write ("+", 1);
7599 /* If we got an ordinary packet, return that to our caller. */
7604 struct cleanup
*old_chain
;
7607 str
= escape_buffer (*buf
, val
);
7608 old_chain
= make_cleanup (xfree
, str
);
7609 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", str
);
7610 do_cleanups (old_chain
);
7613 /* Skip the ack char if we're in no-ack mode. */
7614 if (!rs
->noack_mode
)
7615 remote_serial_write ("+", 1);
7616 if (is_notif
!= NULL
)
7621 /* If we got a notification, handle it, and go back to looking
7625 gdb_assert (c
== '%');
7629 struct cleanup
*old_chain
;
7632 str
= escape_buffer (*buf
, val
);
7633 old_chain
= make_cleanup (xfree
, str
);
7634 fprintf_unfiltered (gdb_stdlog
,
7635 " Notification received: %s\n",
7637 do_cleanups (old_chain
);
7639 if (is_notif
!= NULL
)
7642 handle_notification (rs
->notif_state
, *buf
);
7644 /* Notifications require no acknowledgement. */
7646 if (expecting_notif
)
7653 getpkt_sane (char **buf
, long *sizeof_buf
, int forever
)
7655 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 0, NULL
);
7659 getpkt_or_notif_sane (char **buf
, long *sizeof_buf
, int forever
,
7662 return getpkt_or_notif_sane_1 (buf
, sizeof_buf
, forever
, 1,
7668 remote_kill (struct target_ops
*ops
)
7670 volatile struct gdb_exception ex
;
7672 /* Catch errors so the user can quit from gdb even when we
7673 aren't on speaking terms with the remote system. */
7674 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
7680 if (ex
.error
== TARGET_CLOSE_ERROR
)
7682 /* If we got an (EOF) error that caused the target
7683 to go away, then we're done, that's what we wanted.
7684 "k" is susceptible to cause a premature EOF, given
7685 that the remote server isn't actually required to
7686 reply to "k", and it can happen that it doesn't
7687 even get to reply ACK to the "k". */
7691 /* Otherwise, something went wrong. We didn't actually kill
7692 the target. Just propagate the exception, and let the
7693 user or higher layers decide what to do. */
7694 throw_exception (ex
);
7697 /* We've killed the remote end, we get to mourn it. Since this is
7698 target remote, single-process, mourning the inferior also
7699 unpushes remote_ops. */
7700 target_mourn_inferior ();
7704 remote_vkill (int pid
, struct remote_state
*rs
)
7706 if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7709 /* Tell the remote target to detach. */
7710 xsnprintf (rs
->buf
, get_remote_packet_size (), "vKill;%x", pid
);
7712 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7714 if (packet_ok (rs
->buf
,
7715 &remote_protocol_packets
[PACKET_vKill
]) == PACKET_OK
)
7717 else if (remote_protocol_packets
[PACKET_vKill
].support
== PACKET_DISABLE
)
7724 extended_remote_kill (struct target_ops
*ops
)
7727 int pid
= ptid_get_pid (inferior_ptid
);
7728 struct remote_state
*rs
= get_remote_state ();
7730 res
= remote_vkill (pid
, rs
);
7731 if (res
== -1 && !(rs
->extended
&& remote_multi_process_p (rs
)))
7733 /* Don't try 'k' on a multi-process aware stub -- it has no way
7734 to specify the pid. */
7738 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7739 if (rs
->buf
[0] != 'O' || rs
->buf
[0] != 'K')
7742 /* Don't wait for it to die. I'm not really sure it matters whether
7743 we do or not. For the existing stubs, kill is a noop. */
7749 error (_("Can't kill process"));
7751 target_mourn_inferior ();
7755 remote_mourn (struct target_ops
*ops
)
7757 remote_mourn_1 (ops
);
7760 /* Worker function for remote_mourn. */
7762 remote_mourn_1 (struct target_ops
*target
)
7764 unpush_target (target
);
7766 /* remote_close takes care of doing most of the clean up. */
7767 generic_mourn_inferior ();
7771 extended_remote_mourn_1 (struct target_ops
*target
)
7773 struct remote_state
*rs
= get_remote_state ();
7775 /* In case we got here due to an error, but we're going to stay
7777 rs
->waiting_for_stop_reply
= 0;
7779 /* If the current general thread belonged to the process we just
7780 detached from or has exited, the remote side current general
7781 thread becomes undefined. Considering a case like this:
7783 - We just got here due to a detach.
7784 - The process that we're detaching from happens to immediately
7785 report a global breakpoint being hit in non-stop mode, in the
7786 same thread we had selected before.
7787 - GDB attaches to this process again.
7788 - This event happens to be the next event we handle.
7790 GDB would consider that the current general thread didn't need to
7791 be set on the stub side (with Hg), since for all it knew,
7792 GENERAL_THREAD hadn't changed.
7794 Notice that although in all-stop mode, the remote server always
7795 sets the current thread to the thread reporting the stop event,
7796 that doesn't happen in non-stop mode; in non-stop, the stub *must
7797 not* change the current thread when reporting a breakpoint hit,
7798 due to the decoupling of event reporting and event handling.
7800 To keep things simple, we always invalidate our notion of the
7802 record_currthread (rs
, minus_one_ptid
);
7804 /* Unlike "target remote", we do not want to unpush the target; then
7805 the next time the user says "run", we won't be connected. */
7807 /* Call common code to mark the inferior as not running. */
7808 generic_mourn_inferior ();
7810 if (!have_inferiors ())
7812 if (!remote_multi_process_p (rs
))
7814 /* Check whether the target is running now - some remote stubs
7815 automatically restart after kill. */
7817 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7819 if (rs
->buf
[0] == 'S' || rs
->buf
[0] == 'T')
7821 /* Assume that the target has been restarted. Set
7822 inferior_ptid so that bits of core GDB realizes
7823 there's something here, e.g., so that the user can
7824 say "kill" again. */
7825 inferior_ptid
= magic_null_ptid
;
7832 extended_remote_mourn (struct target_ops
*ops
)
7834 extended_remote_mourn_1 (ops
);
7838 extended_remote_supports_disable_randomization (struct target_ops
*self
)
7840 return (remote_protocol_packets
[PACKET_QDisableRandomization
].support
7845 extended_remote_disable_randomization (int val
)
7847 struct remote_state
*rs
= get_remote_state ();
7850 xsnprintf (rs
->buf
, get_remote_packet_size (), "QDisableRandomization:%x",
7853 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
7855 error (_("Target does not support QDisableRandomization."));
7856 if (strcmp (reply
, "OK") != 0)
7857 error (_("Bogus QDisableRandomization reply from target: %s"), reply
);
7861 extended_remote_run (char *args
)
7863 struct remote_state
*rs
= get_remote_state ();
7866 /* If the user has disabled vRun support, or we have detected that
7867 support is not available, do not try it. */
7868 if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7871 strcpy (rs
->buf
, "vRun;");
7872 len
= strlen (rs
->buf
);
7874 if (strlen (remote_exec_file
) * 2 + len
>= get_remote_packet_size ())
7875 error (_("Remote file name too long for run packet"));
7876 len
+= 2 * bin2hex ((gdb_byte
*) remote_exec_file
, rs
->buf
+ len
,
7877 strlen (remote_exec_file
));
7879 gdb_assert (args
!= NULL
);
7882 struct cleanup
*back_to
;
7886 argv
= gdb_buildargv (args
);
7887 back_to
= make_cleanup ((void (*) (void *)) freeargv
, argv
);
7888 for (i
= 0; argv
[i
] != NULL
; i
++)
7890 if (strlen (argv
[i
]) * 2 + 1 + len
>= get_remote_packet_size ())
7891 error (_("Argument list too long for run packet"));
7892 rs
->buf
[len
++] = ';';
7893 len
+= 2 * bin2hex ((gdb_byte
*) argv
[i
], rs
->buf
+ len
,
7896 do_cleanups (back_to
);
7899 rs
->buf
[len
++] = '\0';
7902 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
7904 if (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_vRun
]) == PACKET_OK
)
7906 /* We have a wait response. All is well. */
7909 else if (remote_protocol_packets
[PACKET_vRun
].support
== PACKET_DISABLE
)
7910 /* It wasn't disabled before, but it is now. */
7914 if (remote_exec_file
[0] == '\0')
7915 error (_("Running the default executable on the remote target failed; "
7916 "try \"set remote exec-file\"?"));
7918 error (_("Running \"%s\" on the remote target failed"),
7923 /* In the extended protocol we want to be able to do things like
7924 "run" and have them basically work as expected. So we need
7925 a special create_inferior function. We support changing the
7926 executable file and the command line arguments, but not the
7930 extended_remote_create_inferior (struct target_ops
*ops
,
7931 char *exec_file
, char *args
,
7932 char **env
, int from_tty
)
7936 struct remote_state
*rs
= get_remote_state ();
7938 /* If running asynchronously, register the target file descriptor
7939 with the event loop. */
7940 if (target_can_async_p ())
7941 target_async (inferior_event_handler
, 0);
7943 /* Disable address space randomization if requested (and supported). */
7944 if (extended_remote_supports_disable_randomization (ops
))
7945 extended_remote_disable_randomization (disable_randomization
);
7947 /* Now restart the remote server. */
7948 run_worked
= extended_remote_run (args
) != -1;
7951 /* vRun was not supported. Fail if we need it to do what the
7953 if (remote_exec_file
[0])
7954 error (_("Remote target does not support \"set remote exec-file\""));
7956 error (_("Remote target does not support \"set args\" or run <ARGS>"));
7958 /* Fall back to "R". */
7959 extended_remote_restart ();
7962 if (!have_inferiors ())
7964 /* Clean up from the last time we ran, before we mark the target
7965 running again. This will mark breakpoints uninserted, and
7966 get_offsets may insert breakpoints. */
7967 init_thread_list ();
7968 init_wait_for_inferior ();
7971 /* vRun's success return is a stop reply. */
7972 stop_reply
= run_worked
? rs
->buf
: NULL
;
7973 add_current_inferior_and_thread (stop_reply
);
7975 /* Get updated offsets, if the stub uses qOffsets. */
7980 /* Given a location's target info BP_TGT and the packet buffer BUF, output
7981 the list of conditions (in agent expression bytecode format), if any, the
7982 target needs to evaluate. The output is placed into the packet buffer
7983 started from BUF and ended at BUF_END. */
7986 remote_add_target_side_condition (struct gdbarch
*gdbarch
,
7987 struct bp_target_info
*bp_tgt
, char *buf
,
7990 struct agent_expr
*aexpr
= NULL
;
7993 char *buf_start
= buf
;
7995 if (VEC_empty (agent_expr_p
, bp_tgt
->conditions
))
7998 buf
+= strlen (buf
);
7999 xsnprintf (buf
, buf_end
- buf
, "%s", ";");
8002 /* Send conditions to the target and free the vector. */
8004 VEC_iterate (agent_expr_p
, bp_tgt
->conditions
, ix
, aexpr
);
8007 xsnprintf (buf
, buf_end
- buf
, "X%x,", aexpr
->len
);
8008 buf
+= strlen (buf
);
8009 for (i
= 0; i
< aexpr
->len
; ++i
)
8010 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
8017 remote_add_target_side_commands (struct gdbarch
*gdbarch
,
8018 struct bp_target_info
*bp_tgt
, char *buf
)
8020 struct agent_expr
*aexpr
= NULL
;
8023 if (VEC_empty (agent_expr_p
, bp_tgt
->tcommands
))
8026 buf
+= strlen (buf
);
8028 sprintf (buf
, ";cmds:%x,", bp_tgt
->persist
);
8029 buf
+= strlen (buf
);
8031 /* Concatenate all the agent expressions that are commands into the
8034 VEC_iterate (agent_expr_p
, bp_tgt
->tcommands
, ix
, aexpr
);
8037 sprintf (buf
, "X%x,", aexpr
->len
);
8038 buf
+= strlen (buf
);
8039 for (i
= 0; i
< aexpr
->len
; ++i
)
8040 buf
= pack_hex_byte (buf
, aexpr
->buf
[i
]);
8045 /* Insert a breakpoint. On targets that have software breakpoint
8046 support, we ask the remote target to do the work; on targets
8047 which don't, we insert a traditional memory breakpoint. */
8050 remote_insert_breakpoint (struct target_ops
*ops
,
8051 struct gdbarch
*gdbarch
,
8052 struct bp_target_info
*bp_tgt
)
8054 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
8055 If it succeeds, then set the support to PACKET_ENABLE. If it
8056 fails, and the user has explicitly requested the Z support then
8057 report an error, otherwise, mark it disabled and go on. */
8059 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
8061 CORE_ADDR addr
= bp_tgt
->placed_address
;
8062 struct remote_state
*rs
;
8065 struct condition_list
*cond
= NULL
;
8067 /* Make sure the remote is pointing at the right process, if
8069 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8070 set_general_process ();
8072 gdbarch_remote_breakpoint_from_pc (gdbarch
, &addr
, &bpsize
);
8074 rs
= get_remote_state ();
8076 endbuf
= rs
->buf
+ get_remote_packet_size ();
8081 addr
= (ULONGEST
) remote_address_masked (addr
);
8082 p
+= hexnumstr (p
, addr
);
8083 xsnprintf (p
, endbuf
- p
, ",%d", bpsize
);
8085 if (remote_supports_cond_breakpoints (ops
))
8086 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
8088 if (remote_can_run_breakpoint_commands (ops
))
8089 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
8092 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8094 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
]))
8099 bp_tgt
->placed_address
= addr
;
8100 bp_tgt
->placed_size
= bpsize
;
8102 case PACKET_UNKNOWN
:
8107 /* If this breakpoint has target-side commands but this stub doesn't
8108 support Z0 packets, throw error. */
8109 if (!VEC_empty (agent_expr_p
, bp_tgt
->tcommands
))
8110 throw_error (NOT_SUPPORTED_ERROR
, _("\
8111 Target doesn't support breakpoints that have target side commands."));
8113 return memory_insert_breakpoint (ops
, gdbarch
, bp_tgt
);
8117 remote_remove_breakpoint (struct target_ops
*ops
,
8118 struct gdbarch
*gdbarch
,
8119 struct bp_target_info
*bp_tgt
)
8121 CORE_ADDR addr
= bp_tgt
->placed_address
;
8122 struct remote_state
*rs
= get_remote_state ();
8124 if (remote_protocol_packets
[PACKET_Z0
].support
!= PACKET_DISABLE
)
8127 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
8129 /* Make sure the remote is pointing at the right process, if
8131 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8132 set_general_process ();
8138 addr
= (ULONGEST
) remote_address_masked (bp_tgt
->placed_address
);
8139 p
+= hexnumstr (p
, addr
);
8140 xsnprintf (p
, endbuf
- p
, ",%d", bp_tgt
->placed_size
);
8143 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8145 return (rs
->buf
[0] == 'E');
8148 return memory_remove_breakpoint (ops
, gdbarch
, bp_tgt
);
8152 watchpoint_to_Z_packet (int type
)
8157 return Z_PACKET_WRITE_WP
;
8160 return Z_PACKET_READ_WP
;
8163 return Z_PACKET_ACCESS_WP
;
8166 internal_error (__FILE__
, __LINE__
,
8167 _("hw_bp_to_z: bad watchpoint type %d"), type
);
8172 remote_insert_watchpoint (struct target_ops
*self
,
8173 CORE_ADDR addr
, int len
, int type
,
8174 struct expression
*cond
)
8176 struct remote_state
*rs
= get_remote_state ();
8177 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
8179 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
8181 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
8184 /* Make sure the remote is pointing at the right process, if
8186 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8187 set_general_process ();
8189 xsnprintf (rs
->buf
, endbuf
- rs
->buf
, "Z%x,", packet
);
8190 p
= strchr (rs
->buf
, '\0');
8191 addr
= remote_address_masked (addr
);
8192 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8193 xsnprintf (p
, endbuf
- p
, ",%x", len
);
8196 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8198 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
8202 case PACKET_UNKNOWN
:
8207 internal_error (__FILE__
, __LINE__
,
8208 _("remote_insert_watchpoint: reached end of function"));
8212 remote_watchpoint_addr_within_range (struct target_ops
*target
, CORE_ADDR addr
,
8213 CORE_ADDR start
, int length
)
8215 CORE_ADDR diff
= remote_address_masked (addr
- start
);
8217 return diff
< length
;
8222 remote_remove_watchpoint (struct target_ops
*self
,
8223 CORE_ADDR addr
, int len
, int type
,
8224 struct expression
*cond
)
8226 struct remote_state
*rs
= get_remote_state ();
8227 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
8229 enum Z_packet_type packet
= watchpoint_to_Z_packet (type
);
8231 if (remote_protocol_packets
[PACKET_Z0
+ packet
].support
== PACKET_DISABLE
)
8234 /* Make sure the remote is pointing at the right process, if
8236 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8237 set_general_process ();
8239 xsnprintf (rs
->buf
, endbuf
- rs
->buf
, "z%x,", packet
);
8240 p
= strchr (rs
->buf
, '\0');
8241 addr
= remote_address_masked (addr
);
8242 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8243 xsnprintf (p
, endbuf
- p
, ",%x", len
);
8245 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8247 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z0
+ packet
]))
8250 case PACKET_UNKNOWN
:
8255 internal_error (__FILE__
, __LINE__
,
8256 _("remote_remove_watchpoint: reached end of function"));
8260 int remote_hw_watchpoint_limit
= -1;
8261 int remote_hw_watchpoint_length_limit
= -1;
8262 int remote_hw_breakpoint_limit
= -1;
8265 remote_region_ok_for_hw_watchpoint (struct target_ops
*self
,
8266 CORE_ADDR addr
, int len
)
8268 if (remote_hw_watchpoint_length_limit
== 0)
8270 else if (remote_hw_watchpoint_length_limit
< 0)
8272 else if (len
<= remote_hw_watchpoint_length_limit
)
8279 remote_check_watch_resources (struct target_ops
*self
,
8280 int type
, int cnt
, int ot
)
8282 if (type
== bp_hardware_breakpoint
)
8284 if (remote_hw_breakpoint_limit
== 0)
8286 else if (remote_hw_breakpoint_limit
< 0)
8288 else if (cnt
<= remote_hw_breakpoint_limit
)
8293 if (remote_hw_watchpoint_limit
== 0)
8295 else if (remote_hw_watchpoint_limit
< 0)
8299 else if (cnt
<= remote_hw_watchpoint_limit
)
8306 remote_stopped_by_watchpoint (struct target_ops
*ops
)
8308 struct remote_state
*rs
= get_remote_state ();
8310 return rs
->remote_stopped_by_watchpoint_p
;
8314 remote_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
8316 struct remote_state
*rs
= get_remote_state ();
8319 if (remote_stopped_by_watchpoint (target
))
8321 *addr_p
= rs
->remote_watch_data_address
;
8330 remote_insert_hw_breakpoint (struct target_ops
*self
, struct gdbarch
*gdbarch
,
8331 struct bp_target_info
*bp_tgt
)
8334 struct remote_state
*rs
;
8338 /* The length field should be set to the size of a breakpoint
8339 instruction, even though we aren't inserting one ourselves. */
8341 gdbarch_remote_breakpoint_from_pc
8342 (gdbarch
, &bp_tgt
->placed_address
, &bp_tgt
->placed_size
);
8344 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
8347 /* Make sure the remote is pointing at the right process, if
8349 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8350 set_general_process ();
8352 rs
= get_remote_state ();
8354 endbuf
= rs
->buf
+ get_remote_packet_size ();
8360 addr
= remote_address_masked (bp_tgt
->placed_address
);
8361 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8362 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->placed_size
);
8364 if (remote_supports_cond_breakpoints (self
))
8365 remote_add_target_side_condition (gdbarch
, bp_tgt
, p
, endbuf
);
8367 if (remote_can_run_breakpoint_commands (self
))
8368 remote_add_target_side_commands (gdbarch
, bp_tgt
, p
);
8371 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8373 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
8376 if (rs
->buf
[1] == '.')
8378 message
= strchr (rs
->buf
+ 2, '.');
8380 error (_("Remote failure reply: %s"), message
+ 1);
8383 case PACKET_UNKNOWN
:
8388 internal_error (__FILE__
, __LINE__
,
8389 _("remote_insert_hw_breakpoint: reached end of function"));
8394 remote_remove_hw_breakpoint (struct target_ops
*self
, struct gdbarch
*gdbarch
,
8395 struct bp_target_info
*bp_tgt
)
8398 struct remote_state
*rs
= get_remote_state ();
8400 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
8402 if (remote_protocol_packets
[PACKET_Z1
].support
== PACKET_DISABLE
)
8405 /* Make sure the remote is pointing at the right process, if
8407 if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
8408 set_general_process ();
8414 addr
= remote_address_masked (bp_tgt
->placed_address
);
8415 p
+= hexnumstr (p
, (ULONGEST
) addr
);
8416 xsnprintf (p
, endbuf
- p
, ",%x", bp_tgt
->placed_size
);
8419 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8421 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[PACKET_Z1
]))
8424 case PACKET_UNKNOWN
:
8429 internal_error (__FILE__
, __LINE__
,
8430 _("remote_remove_hw_breakpoint: reached end of function"));
8433 /* Verify memory using the "qCRC:" request. */
8436 remote_verify_memory (struct target_ops
*ops
,
8437 const gdb_byte
*data
, CORE_ADDR lma
, ULONGEST size
)
8439 struct remote_state
*rs
= get_remote_state ();
8440 unsigned long host_crc
, target_crc
;
8443 /* Make sure the remote is pointing at the right process. */
8444 set_general_process ();
8446 /* FIXME: assumes lma can fit into long. */
8447 xsnprintf (rs
->buf
, get_remote_packet_size (), "qCRC:%lx,%lx",
8448 (long) lma
, (long) size
);
8451 /* Be clever; compute the host_crc before waiting for target
8453 host_crc
= xcrc32 (data
, size
, 0xffffffff);
8455 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8456 if (rs
->buf
[0] == 'E')
8459 if (rs
->buf
[0] != 'C')
8460 error (_("remote target does not support this operation"));
8462 for (target_crc
= 0, tmp
= &rs
->buf
[1]; *tmp
; tmp
++)
8463 target_crc
= target_crc
* 16 + fromhex (*tmp
);
8465 return (host_crc
== target_crc
);
8468 /* compare-sections command
8470 With no arguments, compares each loadable section in the exec bfd
8471 with the same memory range on the target, and reports mismatches.
8472 Useful for verifying the image on the target against the exec file. */
8475 compare_sections_command (char *args
, int from_tty
)
8478 struct cleanup
*old_chain
;
8480 const char *sectname
;
8488 error (_("command cannot be used without an exec file"));
8490 /* Make sure the remote is pointing at the right process. */
8491 set_general_process ();
8493 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
8495 if (!(s
->flags
& SEC_LOAD
))
8496 continue; /* Skip non-loadable section. */
8498 size
= bfd_get_section_size (s
);
8500 continue; /* Skip zero-length section. */
8502 sectname
= bfd_get_section_name (exec_bfd
, s
);
8503 if (args
&& strcmp (args
, sectname
) != 0)
8504 continue; /* Not the section selected by user. */
8506 matched
= 1; /* Do this section. */
8509 sectdata
= xmalloc (size
);
8510 old_chain
= make_cleanup (xfree
, sectdata
);
8511 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
8513 res
= target_verify_memory (sectdata
, lma
, size
);
8516 error (_("target memory fault, section %s, range %s -- %s"), sectname
,
8517 paddress (target_gdbarch (), lma
),
8518 paddress (target_gdbarch (), lma
+ size
));
8520 printf_filtered ("Section %s, range %s -- %s: ", sectname
,
8521 paddress (target_gdbarch (), lma
),
8522 paddress (target_gdbarch (), lma
+ size
));
8524 printf_filtered ("matched.\n");
8527 printf_filtered ("MIS-MATCHED!\n");
8531 do_cleanups (old_chain
);
8534 warning (_("One or more sections of the remote executable does not match\n\
8535 the loaded file\n"));
8536 if (args
&& !matched
)
8537 printf_filtered (_("No loaded section named '%s'.\n"), args
);
8540 /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
8541 into remote target. The number of bytes written to the remote
8542 target is returned, or -1 for error. */
8544 static enum target_xfer_status
8545 remote_write_qxfer (struct target_ops
*ops
, const char *object_name
,
8546 const char *annex
, const gdb_byte
*writebuf
,
8547 ULONGEST offset
, LONGEST len
, ULONGEST
*xfered_len
,
8548 struct packet_config
*packet
)
8552 struct remote_state
*rs
= get_remote_state ();
8553 int max_size
= get_memory_write_packet_size ();
8555 if (packet
->support
== PACKET_DISABLE
)
8556 return TARGET_XFER_E_IO
;
8558 /* Insert header. */
8559 i
= snprintf (rs
->buf
, max_size
,
8560 "qXfer:%s:write:%s:%s:",
8561 object_name
, annex
? annex
: "",
8562 phex_nz (offset
, sizeof offset
));
8563 max_size
-= (i
+ 1);
8565 /* Escape as much data as fits into rs->buf. */
8566 buf_len
= remote_escape_output
8567 (writebuf
, len
, (gdb_byte
*) rs
->buf
+ i
, &max_size
, max_size
);
8569 if (putpkt_binary (rs
->buf
, i
+ buf_len
) < 0
8570 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8571 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8572 return TARGET_XFER_E_IO
;
8574 unpack_varlen_hex (rs
->buf
, &n
);
8577 return TARGET_XFER_OK
;
8580 /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8581 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8582 number of bytes read is returned, or 0 for EOF, or -1 for error.
8583 The number of bytes read may be less than LEN without indicating an
8584 EOF. PACKET is checked and updated to indicate whether the remote
8585 target supports this object. */
8587 static enum target_xfer_status
8588 remote_read_qxfer (struct target_ops
*ops
, const char *object_name
,
8590 gdb_byte
*readbuf
, ULONGEST offset
, LONGEST len
,
8591 ULONGEST
*xfered_len
,
8592 struct packet_config
*packet
)
8594 struct remote_state
*rs
= get_remote_state ();
8595 LONGEST i
, n
, packet_len
;
8597 if (packet
->support
== PACKET_DISABLE
)
8598 return TARGET_XFER_E_IO
;
8600 /* Check whether we've cached an end-of-object packet that matches
8602 if (rs
->finished_object
)
8604 if (strcmp (object_name
, rs
->finished_object
) == 0
8605 && strcmp (annex
? annex
: "", rs
->finished_annex
) == 0
8606 && offset
== rs
->finished_offset
)
8607 return TARGET_XFER_EOF
;
8610 /* Otherwise, we're now reading something different. Discard
8612 xfree (rs
->finished_object
);
8613 xfree (rs
->finished_annex
);
8614 rs
->finished_object
= NULL
;
8615 rs
->finished_annex
= NULL
;
8618 /* Request only enough to fit in a single packet. The actual data
8619 may not, since we don't know how much of it will need to be escaped;
8620 the target is free to respond with slightly less data. We subtract
8621 five to account for the response type and the protocol frame. */
8622 n
= min (get_remote_packet_size () - 5, len
);
8623 snprintf (rs
->buf
, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8624 object_name
, annex
? annex
: "",
8625 phex_nz (offset
, sizeof offset
),
8626 phex_nz (n
, sizeof n
));
8627 i
= putpkt (rs
->buf
);
8629 return TARGET_XFER_E_IO
;
8632 packet_len
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
8633 if (packet_len
< 0 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8634 return TARGET_XFER_E_IO
;
8636 if (rs
->buf
[0] != 'l' && rs
->buf
[0] != 'm')
8637 error (_("Unknown remote qXfer reply: %s"), rs
->buf
);
8639 /* 'm' means there is (or at least might be) more data after this
8640 batch. That does not make sense unless there's at least one byte
8641 of data in this reply. */
8642 if (rs
->buf
[0] == 'm' && packet_len
== 1)
8643 error (_("Remote qXfer reply contained no data."));
8645 /* Got some data. */
8646 i
= remote_unescape_input ((gdb_byte
*) rs
->buf
+ 1,
8647 packet_len
- 1, readbuf
, n
);
8649 /* 'l' is an EOF marker, possibly including a final block of data,
8650 or possibly empty. If we have the final block of a non-empty
8651 object, record this fact to bypass a subsequent partial read. */
8652 if (rs
->buf
[0] == 'l' && offset
+ i
> 0)
8654 rs
->finished_object
= xstrdup (object_name
);
8655 rs
->finished_annex
= xstrdup (annex
? annex
: "");
8656 rs
->finished_offset
= offset
+ i
;
8660 return TARGET_XFER_EOF
;
8664 return TARGET_XFER_OK
;
8668 static enum target_xfer_status
8669 remote_xfer_partial (struct target_ops
*ops
, enum target_object object
,
8670 const char *annex
, gdb_byte
*readbuf
,
8671 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
8672 ULONGEST
*xfered_len
)
8674 struct remote_state
*rs
;
8679 set_remote_traceframe ();
8680 set_general_thread (inferior_ptid
);
8682 rs
= get_remote_state ();
8684 /* Handle memory using the standard memory routines. */
8685 if (object
== TARGET_OBJECT_MEMORY
)
8687 /* If the remote target is connected but not running, we should
8688 pass this request down to a lower stratum (e.g. the executable
8690 if (!target_has_execution
)
8691 return TARGET_XFER_EOF
;
8693 if (writebuf
!= NULL
)
8694 return remote_write_bytes (offset
, writebuf
, len
, xfered_len
);
8696 return remote_read_bytes (offset
, readbuf
, len
, xfered_len
);
8699 /* Handle SPU memory using qxfer packets. */
8700 if (object
== TARGET_OBJECT_SPU
)
8703 return remote_read_qxfer (ops
, "spu", annex
, readbuf
, offset
, len
,
8704 xfered_len
, &remote_protocol_packets
8705 [PACKET_qXfer_spu_read
]);
8707 return remote_write_qxfer (ops
, "spu", annex
, writebuf
, offset
, len
,
8708 xfered_len
, &remote_protocol_packets
8709 [PACKET_qXfer_spu_write
]);
8712 /* Handle extra signal info using qxfer packets. */
8713 if (object
== TARGET_OBJECT_SIGNAL_INFO
)
8716 return remote_read_qxfer (ops
, "siginfo", annex
, readbuf
, offset
, len
,
8717 xfered_len
, &remote_protocol_packets
8718 [PACKET_qXfer_siginfo_read
]);
8720 return remote_write_qxfer (ops
, "siginfo", annex
,
8721 writebuf
, offset
, len
, xfered_len
,
8722 &remote_protocol_packets
8723 [PACKET_qXfer_siginfo_write
]);
8726 if (object
== TARGET_OBJECT_STATIC_TRACE_DATA
)
8729 return remote_read_qxfer (ops
, "statictrace", annex
,
8730 readbuf
, offset
, len
, xfered_len
,
8731 &remote_protocol_packets
8732 [PACKET_qXfer_statictrace_read
]);
8734 return TARGET_XFER_E_IO
;
8737 /* Only handle flash writes. */
8738 if (writebuf
!= NULL
)
8744 case TARGET_OBJECT_FLASH
:
8745 return remote_flash_write (ops
, offset
, len
, xfered_len
,
8749 return TARGET_XFER_E_IO
;
8753 /* Map pre-existing objects onto letters. DO NOT do this for new
8754 objects!!! Instead specify new query packets. */
8757 case TARGET_OBJECT_AVR
:
8761 case TARGET_OBJECT_AUXV
:
8762 gdb_assert (annex
== NULL
);
8763 return remote_read_qxfer (ops
, "auxv", annex
, readbuf
, offset
, len
,
8765 &remote_protocol_packets
[PACKET_qXfer_auxv
]);
8767 case TARGET_OBJECT_AVAILABLE_FEATURES
:
8768 return remote_read_qxfer
8769 (ops
, "features", annex
, readbuf
, offset
, len
, xfered_len
,
8770 &remote_protocol_packets
[PACKET_qXfer_features
]);
8772 case TARGET_OBJECT_LIBRARIES
:
8773 return remote_read_qxfer
8774 (ops
, "libraries", annex
, readbuf
, offset
, len
, xfered_len
,
8775 &remote_protocol_packets
[PACKET_qXfer_libraries
]);
8777 case TARGET_OBJECT_LIBRARIES_SVR4
:
8778 return remote_read_qxfer
8779 (ops
, "libraries-svr4", annex
, readbuf
, offset
, len
, xfered_len
,
8780 &remote_protocol_packets
[PACKET_qXfer_libraries_svr4
]);
8782 case TARGET_OBJECT_MEMORY_MAP
:
8783 gdb_assert (annex
== NULL
);
8784 return remote_read_qxfer (ops
, "memory-map", annex
, readbuf
, offset
, len
,
8786 &remote_protocol_packets
[PACKET_qXfer_memory_map
]);
8788 case TARGET_OBJECT_OSDATA
:
8789 /* Should only get here if we're connected. */
8790 gdb_assert (rs
->remote_desc
);
8791 return remote_read_qxfer
8792 (ops
, "osdata", annex
, readbuf
, offset
, len
, xfered_len
,
8793 &remote_protocol_packets
[PACKET_qXfer_osdata
]);
8795 case TARGET_OBJECT_THREADS
:
8796 gdb_assert (annex
== NULL
);
8797 return remote_read_qxfer (ops
, "threads", annex
, readbuf
, offset
, len
,
8799 &remote_protocol_packets
[PACKET_qXfer_threads
]);
8801 case TARGET_OBJECT_TRACEFRAME_INFO
:
8802 gdb_assert (annex
== NULL
);
8803 return remote_read_qxfer
8804 (ops
, "traceframe-info", annex
, readbuf
, offset
, len
, xfered_len
,
8805 &remote_protocol_packets
[PACKET_qXfer_traceframe_info
]);
8807 case TARGET_OBJECT_FDPIC
:
8808 return remote_read_qxfer (ops
, "fdpic", annex
, readbuf
, offset
, len
,
8810 &remote_protocol_packets
[PACKET_qXfer_fdpic
]);
8812 case TARGET_OBJECT_OPENVMS_UIB
:
8813 return remote_read_qxfer (ops
, "uib", annex
, readbuf
, offset
, len
,
8815 &remote_protocol_packets
[PACKET_qXfer_uib
]);
8817 case TARGET_OBJECT_BTRACE
:
8818 return remote_read_qxfer (ops
, "btrace", annex
, readbuf
, offset
, len
,
8820 &remote_protocol_packets
[PACKET_qXfer_btrace
]);
8823 return TARGET_XFER_E_IO
;
8826 /* Note: a zero OFFSET and LEN can be used to query the minimum
8828 if (offset
== 0 && len
== 0)
8829 return (get_remote_packet_size ());
8830 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
8831 large enough let the caller deal with it. */
8832 if (len
< get_remote_packet_size ())
8833 return TARGET_XFER_E_IO
;
8834 len
= get_remote_packet_size ();
8836 /* Except for querying the minimum buffer size, target must be open. */
8837 if (!rs
->remote_desc
)
8838 error (_("remote query is only available after target open"));
8840 gdb_assert (annex
!= NULL
);
8841 gdb_assert (readbuf
!= NULL
);
8847 /* We used one buffer char for the remote protocol q command and
8848 another for the query type. As the remote protocol encapsulation
8849 uses 4 chars plus one extra in case we are debugging
8850 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8853 while (annex
[i
] && (i
< (get_remote_packet_size () - 8)))
8855 /* Bad caller may have sent forbidden characters. */
8856 gdb_assert (isprint (annex
[i
]) && annex
[i
] != '$' && annex
[i
] != '#');
8861 gdb_assert (annex
[i
] == '\0');
8863 i
= putpkt (rs
->buf
);
8865 return TARGET_XFER_E_IO
;
8867 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
8868 strcpy ((char *) readbuf
, rs
->buf
);
8870 *xfered_len
= strlen ((char *) readbuf
);
8871 return TARGET_XFER_OK
;
8875 remote_search_memory (struct target_ops
* ops
,
8876 CORE_ADDR start_addr
, ULONGEST search_space_len
,
8877 const gdb_byte
*pattern
, ULONGEST pattern_len
,
8878 CORE_ADDR
*found_addrp
)
8880 int addr_size
= gdbarch_addr_bit (target_gdbarch ()) / 8;
8881 struct remote_state
*rs
= get_remote_state ();
8882 int max_size
= get_memory_write_packet_size ();
8883 struct packet_config
*packet
=
8884 &remote_protocol_packets
[PACKET_qSearch_memory
];
8885 /* Number of packet bytes used to encode the pattern;
8886 this could be more than PATTERN_LEN due to escape characters. */
8887 int escaped_pattern_len
;
8888 /* Amount of pattern that was encodable in the packet. */
8889 int used_pattern_len
;
8892 ULONGEST found_addr
;
8894 /* Don't go to the target if we don't have to.
8895 This is done before checking packet->support to avoid the possibility that
8896 a success for this edge case means the facility works in general. */
8897 if (pattern_len
> search_space_len
)
8899 if (pattern_len
== 0)
8901 *found_addrp
= start_addr
;
8905 /* If we already know the packet isn't supported, fall back to the simple
8906 way of searching memory. */
8908 if (packet
->support
== PACKET_DISABLE
)
8910 /* Target doesn't provided special support, fall back and use the
8911 standard support (copy memory and do the search here). */
8912 return simple_search_memory (ops
, start_addr
, search_space_len
,
8913 pattern
, pattern_len
, found_addrp
);
8916 /* Make sure the remote is pointing at the right process. */
8917 set_general_process ();
8919 /* Insert header. */
8920 i
= snprintf (rs
->buf
, max_size
,
8921 "qSearch:memory:%s;%s;",
8922 phex_nz (start_addr
, addr_size
),
8923 phex_nz (search_space_len
, sizeof (search_space_len
)));
8924 max_size
-= (i
+ 1);
8926 /* Escape as much data as fits into rs->buf. */
8927 escaped_pattern_len
=
8928 remote_escape_output (pattern
, pattern_len
, (gdb_byte
*) rs
->buf
+ i
,
8929 &used_pattern_len
, max_size
);
8931 /* Bail if the pattern is too large. */
8932 if (used_pattern_len
!= pattern_len
)
8933 error (_("Pattern is too large to transmit to remote target."));
8935 if (putpkt_binary (rs
->buf
, i
+ escaped_pattern_len
) < 0
8936 || getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) < 0
8937 || packet_ok (rs
->buf
, packet
) != PACKET_OK
)
8939 /* The request may not have worked because the command is not
8940 supported. If so, fall back to the simple way. */
8941 if (packet
->support
== PACKET_DISABLE
)
8943 return simple_search_memory (ops
, start_addr
, search_space_len
,
8944 pattern
, pattern_len
, found_addrp
);
8949 if (rs
->buf
[0] == '0')
8951 else if (rs
->buf
[0] == '1')
8954 if (rs
->buf
[1] != ',')
8955 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8956 unpack_varlen_hex (rs
->buf
+ 2, &found_addr
);
8957 *found_addrp
= found_addr
;
8960 error (_("Unknown qSearch:memory reply: %s"), rs
->buf
);
8966 remote_rcmd (struct target_ops
*self
, char *command
,
8967 struct ui_file
*outbuf
)
8969 struct remote_state
*rs
= get_remote_state ();
8972 if (!rs
->remote_desc
)
8973 error (_("remote rcmd is only available after target open"));
8975 /* Send a NULL command across as an empty command. */
8976 if (command
== NULL
)
8979 /* The query prefix. */
8980 strcpy (rs
->buf
, "qRcmd,");
8981 p
= strchr (rs
->buf
, '\0');
8983 if ((strlen (rs
->buf
) + strlen (command
) * 2 + 8/*misc*/)
8984 > get_remote_packet_size ())
8985 error (_("\"monitor\" command ``%s'' is too long."), command
);
8987 /* Encode the actual command. */
8988 bin2hex ((gdb_byte
*) command
, p
, strlen (command
));
8990 if (putpkt (rs
->buf
) < 0)
8991 error (_("Communication problem with target."));
8993 /* get/display the response */
8998 /* XXX - see also remote_get_noisy_reply(). */
8999 QUIT
; /* Allow user to bail out with ^C. */
9001 if (getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0) == -1)
9003 /* Timeout. Continue to (try to) read responses.
9004 This is better than stopping with an error, assuming the stub
9005 is still executing the (long) monitor command.
9006 If needed, the user can interrupt gdb using C-c, obtaining
9007 an effect similar to stop on timeout. */
9012 error (_("Target does not support this command."));
9013 if (buf
[0] == 'O' && buf
[1] != 'K')
9015 remote_console_output (buf
+ 1); /* 'O' message from stub. */
9018 if (strcmp (buf
, "OK") == 0)
9020 if (strlen (buf
) == 3 && buf
[0] == 'E'
9021 && isdigit (buf
[1]) && isdigit (buf
[2]))
9023 error (_("Protocol error with Rcmd"));
9025 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
9027 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
9029 fputc_unfiltered (c
, outbuf
);
9035 static VEC(mem_region_s
) *
9036 remote_memory_map (struct target_ops
*ops
)
9038 VEC(mem_region_s
) *result
= NULL
;
9039 char *text
= target_read_stralloc (¤t_target
,
9040 TARGET_OBJECT_MEMORY_MAP
, NULL
);
9044 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
9046 result
= parse_memory_map (text
);
9047 do_cleanups (back_to
);
9054 packet_command (char *args
, int from_tty
)
9056 struct remote_state
*rs
= get_remote_state ();
9058 if (!rs
->remote_desc
)
9059 error (_("command can only be used with remote target"));
9062 error (_("remote-packet command requires packet text as argument"));
9064 puts_filtered ("sending: ");
9065 print_packet (args
);
9066 puts_filtered ("\n");
9069 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
9070 puts_filtered ("received: ");
9071 print_packet (rs
->buf
);
9072 puts_filtered ("\n");
9076 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
9078 static void display_thread_info (struct gdb_ext_thread_info
*info
);
9080 static void threadset_test_cmd (char *cmd
, int tty
);
9082 static void threadalive_test (char *cmd
, int tty
);
9084 static void threadlist_test_cmd (char *cmd
, int tty
);
9086 int get_and_display_threadinfo (threadref
*ref
);
9088 static void threadinfo_test_cmd (char *cmd
, int tty
);
9090 static int thread_display_step (threadref
*ref
, void *context
);
9092 static void threadlist_update_test_cmd (char *cmd
, int tty
);
9094 static void init_remote_threadtests (void);
9096 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
9099 threadset_test_cmd (char *cmd
, int tty
)
9101 int sample_thread
= SAMPLE_THREAD
;
9103 printf_filtered (_("Remote threadset test\n"));
9104 set_general_thread (sample_thread
);
9109 threadalive_test (char *cmd
, int tty
)
9111 int sample_thread
= SAMPLE_THREAD
;
9112 int pid
= ptid_get_pid (inferior_ptid
);
9113 ptid_t ptid
= ptid_build (pid
, 0, sample_thread
);
9115 if (remote_thread_alive (ptid
))
9116 printf_filtered ("PASS: Thread alive test\n");
9118 printf_filtered ("FAIL: Thread alive test\n");
9121 void output_threadid (char *title
, threadref
*ref
);
9124 output_threadid (char *title
, threadref
*ref
)
9128 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex. */
9130 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
9134 threadlist_test_cmd (char *cmd
, int tty
)
9137 threadref nextthread
;
9138 int done
, result_count
;
9139 threadref threadlist
[3];
9141 printf_filtered ("Remote Threadlist test\n");
9142 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
9143 &result_count
, &threadlist
[0]))
9144 printf_filtered ("FAIL: threadlist test\n");
9147 threadref
*scan
= threadlist
;
9148 threadref
*limit
= scan
+ result_count
;
9150 while (scan
< limit
)
9151 output_threadid (" thread ", scan
++);
9156 display_thread_info (struct gdb_ext_thread_info
*info
)
9158 output_threadid ("Threadid: ", &info
->threadid
);
9159 printf_filtered ("Name: %s\n ", info
->shortname
);
9160 printf_filtered ("State: %s\n", info
->display
);
9161 printf_filtered ("other: %s\n\n", info
->more_display
);
9165 get_and_display_threadinfo (threadref
*ref
)
9169 struct gdb_ext_thread_info threadinfo
;
9171 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
9172 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
9173 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
9174 display_thread_info (&threadinfo
);
9179 threadinfo_test_cmd (char *cmd
, int tty
)
9181 int athread
= SAMPLE_THREAD
;
9185 int_to_threadref (&thread
, athread
);
9186 printf_filtered ("Remote Threadinfo test\n");
9187 if (!get_and_display_threadinfo (&thread
))
9188 printf_filtered ("FAIL cannot get thread info\n");
9192 thread_display_step (threadref
*ref
, void *context
)
9194 /* output_threadid(" threadstep ",ref); *//* simple test */
9195 return get_and_display_threadinfo (ref
);
9199 threadlist_update_test_cmd (char *cmd
, int tty
)
9201 printf_filtered ("Remote Threadlist update test\n");
9202 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
9206 init_remote_threadtests (void)
9208 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
9209 _("Fetch and print the remote list of "
9210 "thread identifiers, one pkt only"));
9211 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
9212 _("Fetch and display info about one thread"));
9213 add_com ("tset", class_obscure
, threadset_test_cmd
,
9214 _("Test setting to a different thread"));
9215 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
9216 _("Iterate through updating all remote thread info"));
9217 add_com ("talive", class_obscure
, threadalive_test
,
9218 _(" Remote thread alive test "));
9223 /* Convert a thread ID to a string. Returns the string in a static
9227 remote_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
9229 static char buf
[64];
9230 struct remote_state
*rs
= get_remote_state ();
9232 if (ptid_equal (ptid
, null_ptid
))
9233 return normal_pid_to_str (ptid
);
9234 else if (ptid_is_pid (ptid
))
9236 /* Printing an inferior target id. */
9238 /* When multi-process extensions are off, there's no way in the
9239 remote protocol to know the remote process id, if there's any
9240 at all. There's one exception --- when we're connected with
9241 target extended-remote, and we manually attached to a process
9242 with "attach PID". We don't record anywhere a flag that
9243 allows us to distinguish that case from the case of
9244 connecting with extended-remote and the stub already being
9245 attached to a process, and reporting yes to qAttached, hence
9246 no smart special casing here. */
9247 if (!remote_multi_process_p (rs
))
9249 xsnprintf (buf
, sizeof buf
, "Remote target");
9253 return normal_pid_to_str (ptid
);
9257 if (ptid_equal (magic_null_ptid
, ptid
))
9258 xsnprintf (buf
, sizeof buf
, "Thread <main>");
9259 else if (rs
->extended
&& remote_multi_process_p (rs
))
9260 xsnprintf (buf
, sizeof buf
, "Thread %d.%ld",
9261 ptid_get_pid (ptid
), ptid_get_tid (ptid
));
9263 xsnprintf (buf
, sizeof buf
, "Thread %ld",
9264 ptid_get_tid (ptid
));
9269 /* Get the address of the thread local variable in OBJFILE which is
9270 stored at OFFSET within the thread local storage for thread PTID. */
9273 remote_get_thread_local_address (struct target_ops
*ops
,
9274 ptid_t ptid
, CORE_ADDR lm
, CORE_ADDR offset
)
9276 if (remote_protocol_packets
[PACKET_qGetTLSAddr
].support
!= PACKET_DISABLE
)
9278 struct remote_state
*rs
= get_remote_state ();
9280 char *endp
= rs
->buf
+ get_remote_packet_size ();
9281 enum packet_result result
;
9283 strcpy (p
, "qGetTLSAddr:");
9285 p
= write_ptid (p
, endp
, ptid
);
9287 p
+= hexnumstr (p
, offset
);
9289 p
+= hexnumstr (p
, lm
);
9293 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
9294 result
= packet_ok (rs
->buf
,
9295 &remote_protocol_packets
[PACKET_qGetTLSAddr
]);
9296 if (result
== PACKET_OK
)
9300 unpack_varlen_hex (rs
->buf
, &result
);
9303 else if (result
== PACKET_UNKNOWN
)
9304 throw_error (TLS_GENERIC_ERROR
,
9305 _("Remote target doesn't support qGetTLSAddr packet"));
9307 throw_error (TLS_GENERIC_ERROR
,
9308 _("Remote target failed to process qGetTLSAddr request"));
9311 throw_error (TLS_GENERIC_ERROR
,
9312 _("TLS not supported or disabled on this target"));
9317 /* Provide thread local base, i.e. Thread Information Block address.
9318 Returns 1 if ptid is found and thread_local_base is non zero. */
9321 remote_get_tib_address (ptid_t ptid
, CORE_ADDR
*addr
)
9323 if (remote_protocol_packets
[PACKET_qGetTIBAddr
].support
!= PACKET_DISABLE
)
9325 struct remote_state
*rs
= get_remote_state ();
9327 char *endp
= rs
->buf
+ get_remote_packet_size ();
9328 enum packet_result result
;
9330 strcpy (p
, "qGetTIBAddr:");
9332 p
= write_ptid (p
, endp
, ptid
);
9336 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
9337 result
= packet_ok (rs
->buf
,
9338 &remote_protocol_packets
[PACKET_qGetTIBAddr
]);
9339 if (result
== PACKET_OK
)
9343 unpack_varlen_hex (rs
->buf
, &result
);
9345 *addr
= (CORE_ADDR
) result
;
9348 else if (result
== PACKET_UNKNOWN
)
9349 error (_("Remote target doesn't support qGetTIBAddr packet"));
9351 error (_("Remote target failed to process qGetTIBAddr request"));
9354 error (_("qGetTIBAddr not supported or disabled on this target"));
9359 /* Support for inferring a target description based on the current
9360 architecture and the size of a 'g' packet. While the 'g' packet
9361 can have any size (since optional registers can be left off the
9362 end), some sizes are easily recognizable given knowledge of the
9363 approximate architecture. */
9365 struct remote_g_packet_guess
9368 const struct target_desc
*tdesc
;
9370 typedef struct remote_g_packet_guess remote_g_packet_guess_s
;
9371 DEF_VEC_O(remote_g_packet_guess_s
);
9373 struct remote_g_packet_data
9375 VEC(remote_g_packet_guess_s
) *guesses
;
9378 static struct gdbarch_data
*remote_g_packet_data_handle
;
9381 remote_g_packet_data_init (struct obstack
*obstack
)
9383 return OBSTACK_ZALLOC (obstack
, struct remote_g_packet_data
);
9387 register_remote_g_packet_guess (struct gdbarch
*gdbarch
, int bytes
,
9388 const struct target_desc
*tdesc
)
9390 struct remote_g_packet_data
*data
9391 = gdbarch_data (gdbarch
, remote_g_packet_data_handle
);
9392 struct remote_g_packet_guess new_guess
, *guess
;
9395 gdb_assert (tdesc
!= NULL
);
9398 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
9400 if (guess
->bytes
== bytes
)
9401 internal_error (__FILE__
, __LINE__
,
9402 _("Duplicate g packet description added for size %d"),
9405 new_guess
.bytes
= bytes
;
9406 new_guess
.tdesc
= tdesc
;
9407 VEC_safe_push (remote_g_packet_guess_s
, data
->guesses
, &new_guess
);
9410 /* Return 1 if remote_read_description would do anything on this target
9411 and architecture, 0 otherwise. */
9414 remote_read_description_p (struct target_ops
*target
)
9416 struct remote_g_packet_data
*data
9417 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle
);
9419 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
9425 static const struct target_desc
*
9426 remote_read_description (struct target_ops
*target
)
9428 struct remote_g_packet_data
*data
9429 = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle
);
9431 /* Do not try this during initial connection, when we do not know
9432 whether there is a running but stopped thread. */
9433 if (!target_has_execution
|| ptid_equal (inferior_ptid
, null_ptid
))
9436 if (!VEC_empty (remote_g_packet_guess_s
, data
->guesses
))
9438 struct remote_g_packet_guess
*guess
;
9440 int bytes
= send_g_packet ();
9443 VEC_iterate (remote_g_packet_guess_s
, data
->guesses
, ix
, guess
);
9445 if (guess
->bytes
== bytes
)
9446 return guess
->tdesc
;
9448 /* We discard the g packet. A minor optimization would be to
9449 hold on to it, and fill the register cache once we have selected
9450 an architecture, but it's too tricky to do safely. */
9456 /* Remote file transfer support. This is host-initiated I/O, not
9457 target-initiated; for target-initiated, see remote-fileio.c. */
9459 /* If *LEFT is at least the length of STRING, copy STRING to
9460 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9461 decrease *LEFT. Otherwise raise an error. */
9464 remote_buffer_add_string (char **buffer
, int *left
, char *string
)
9466 int len
= strlen (string
);
9469 error (_("Packet too long for target."));
9471 memcpy (*buffer
, string
, len
);
9475 /* NUL-terminate the buffer as a convenience, if there is
9481 /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
9482 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9483 decrease *LEFT. Otherwise raise an error. */
9486 remote_buffer_add_bytes (char **buffer
, int *left
, const gdb_byte
*bytes
,
9489 if (2 * len
> *left
)
9490 error (_("Packet too long for target."));
9492 bin2hex (bytes
, *buffer
, len
);
9496 /* NUL-terminate the buffer as a convenience, if there is
9502 /* If *LEFT is large enough, convert VALUE to hex and add it to
9503 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9504 decrease *LEFT. Otherwise raise an error. */
9507 remote_buffer_add_int (char **buffer
, int *left
, ULONGEST value
)
9509 int len
= hexnumlen (value
);
9512 error (_("Packet too long for target."));
9514 hexnumstr (*buffer
, value
);
9518 /* NUL-terminate the buffer as a convenience, if there is
9524 /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
9525 value, *REMOTE_ERRNO to the remote error number or zero if none
9526 was included, and *ATTACHMENT to point to the start of the annex
9527 if any. The length of the packet isn't needed here; there may
9528 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
9530 Return 0 if the packet could be parsed, -1 if it could not. If
9531 -1 is returned, the other variables may not be initialized. */
9534 remote_hostio_parse_result (char *buffer
, int *retcode
,
9535 int *remote_errno
, char **attachment
)
9542 if (buffer
[0] != 'F')
9546 *retcode
= strtol (&buffer
[1], &p
, 16);
9547 if (errno
!= 0 || p
== &buffer
[1])
9550 /* Check for ",errno". */
9554 *remote_errno
= strtol (p
+ 1, &p2
, 16);
9555 if (errno
!= 0 || p
+ 1 == p2
)
9560 /* Check for ";attachment". If there is no attachment, the
9561 packet should end here. */
9564 *attachment
= p
+ 1;
9567 else if (*p
== '\0')
9573 /* Send a prepared I/O packet to the target and read its response.
9574 The prepared packet is in the global RS->BUF before this function
9575 is called, and the answer is there when we return.
9577 COMMAND_BYTES is the length of the request to send, which may include
9578 binary data. WHICH_PACKET is the packet configuration to check
9579 before attempting a packet. If an error occurs, *REMOTE_ERRNO
9580 is set to the error number and -1 is returned. Otherwise the value
9581 returned by the function is returned.
9583 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
9584 attachment is expected; an error will be reported if there's a
9585 mismatch. If one is found, *ATTACHMENT will be set to point into
9586 the packet buffer and *ATTACHMENT_LEN will be set to the
9587 attachment's length. */
9590 remote_hostio_send_command (int command_bytes
, int which_packet
,
9591 int *remote_errno
, char **attachment
,
9592 int *attachment_len
)
9594 struct remote_state
*rs
= get_remote_state ();
9595 int ret
, bytes_read
;
9596 char *attachment_tmp
;
9598 if (!rs
->remote_desc
9599 || remote_protocol_packets
[which_packet
].support
== PACKET_DISABLE
)
9601 *remote_errno
= FILEIO_ENOSYS
;
9605 putpkt_binary (rs
->buf
, command_bytes
);
9606 bytes_read
= getpkt_sane (&rs
->buf
, &rs
->buf_size
, 0);
9608 /* If it timed out, something is wrong. Don't try to parse the
9612 *remote_errno
= FILEIO_EINVAL
;
9616 switch (packet_ok (rs
->buf
, &remote_protocol_packets
[which_packet
]))
9619 *remote_errno
= FILEIO_EINVAL
;
9621 case PACKET_UNKNOWN
:
9622 *remote_errno
= FILEIO_ENOSYS
;
9628 if (remote_hostio_parse_result (rs
->buf
, &ret
, remote_errno
,
9631 *remote_errno
= FILEIO_EINVAL
;
9635 /* Make sure we saw an attachment if and only if we expected one. */
9636 if ((attachment_tmp
== NULL
&& attachment
!= NULL
)
9637 || (attachment_tmp
!= NULL
&& attachment
== NULL
))
9639 *remote_errno
= FILEIO_EINVAL
;
9643 /* If an attachment was found, it must point into the packet buffer;
9644 work out how many bytes there were. */
9645 if (attachment_tmp
!= NULL
)
9647 *attachment
= attachment_tmp
;
9648 *attachment_len
= bytes_read
- (*attachment
- rs
->buf
);
9654 /* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9655 remote file descriptor, or -1 if an error occurs (and set
9659 remote_hostio_open (struct target_ops
*self
,
9660 const char *filename
, int flags
, int mode
,
9663 struct remote_state
*rs
= get_remote_state ();
9665 int left
= get_remote_packet_size () - 1;
9667 remote_buffer_add_string (&p
, &left
, "vFile:open:");
9669 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9671 remote_buffer_add_string (&p
, &left
, ",");
9673 remote_buffer_add_int (&p
, &left
, flags
);
9674 remote_buffer_add_string (&p
, &left
, ",");
9676 remote_buffer_add_int (&p
, &left
, mode
);
9678 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_open
,
9679 remote_errno
, NULL
, NULL
);
9682 /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9683 Return the number of bytes written, or -1 if an error occurs (and
9684 set *REMOTE_ERRNO). */
9687 remote_hostio_pwrite (struct target_ops
*self
,
9688 int fd
, const gdb_byte
*write_buf
, int len
,
9689 ULONGEST offset
, int *remote_errno
)
9691 struct remote_state
*rs
= get_remote_state ();
9693 int left
= get_remote_packet_size ();
9696 remote_buffer_add_string (&p
, &left
, "vFile:pwrite:");
9698 remote_buffer_add_int (&p
, &left
, fd
);
9699 remote_buffer_add_string (&p
, &left
, ",");
9701 remote_buffer_add_int (&p
, &left
, offset
);
9702 remote_buffer_add_string (&p
, &left
, ",");
9704 p
+= remote_escape_output (write_buf
, len
, (gdb_byte
*) p
, &out_len
,
9705 get_remote_packet_size () - (p
- rs
->buf
));
9707 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pwrite
,
9708 remote_errno
, NULL
, NULL
);
9711 /* Read up to LEN bytes FD on the remote target into READ_BUF
9712 Return the number of bytes read, or -1 if an error occurs (and
9713 set *REMOTE_ERRNO). */
9716 remote_hostio_pread (struct target_ops
*self
,
9717 int fd
, gdb_byte
*read_buf
, int len
,
9718 ULONGEST offset
, int *remote_errno
)
9720 struct remote_state
*rs
= get_remote_state ();
9723 int left
= get_remote_packet_size ();
9724 int ret
, attachment_len
;
9727 remote_buffer_add_string (&p
, &left
, "vFile:pread:");
9729 remote_buffer_add_int (&p
, &left
, fd
);
9730 remote_buffer_add_string (&p
, &left
, ",");
9732 remote_buffer_add_int (&p
, &left
, len
);
9733 remote_buffer_add_string (&p
, &left
, ",");
9735 remote_buffer_add_int (&p
, &left
, offset
);
9737 ret
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_pread
,
9738 remote_errno
, &attachment
,
9744 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
9746 if (read_len
!= ret
)
9747 error (_("Read returned %d, but %d bytes."), ret
, (int) read_len
);
9752 /* Close FD on the remote target. Return 0, or -1 if an error occurs
9753 (and set *REMOTE_ERRNO). */
9756 remote_hostio_close (struct target_ops
*self
, int fd
, int *remote_errno
)
9758 struct remote_state
*rs
= get_remote_state ();
9760 int left
= get_remote_packet_size () - 1;
9762 remote_buffer_add_string (&p
, &left
, "vFile:close:");
9764 remote_buffer_add_int (&p
, &left
, fd
);
9766 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_close
,
9767 remote_errno
, NULL
, NULL
);
9770 /* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9771 occurs (and set *REMOTE_ERRNO). */
9774 remote_hostio_unlink (struct target_ops
*self
,
9775 const char *filename
, int *remote_errno
)
9777 struct remote_state
*rs
= get_remote_state ();
9779 int left
= get_remote_packet_size () - 1;
9781 remote_buffer_add_string (&p
, &left
, "vFile:unlink:");
9783 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9786 return remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_unlink
,
9787 remote_errno
, NULL
, NULL
);
9790 /* Read value of symbolic link FILENAME on the remote target. Return
9791 a null-terminated string allocated via xmalloc, or NULL if an error
9792 occurs (and set *REMOTE_ERRNO). */
9795 remote_hostio_readlink (struct target_ops
*self
,
9796 const char *filename
, int *remote_errno
)
9798 struct remote_state
*rs
= get_remote_state ();
9801 int left
= get_remote_packet_size ();
9802 int len
, attachment_len
;
9806 remote_buffer_add_string (&p
, &left
, "vFile:readlink:");
9808 remote_buffer_add_bytes (&p
, &left
, (const gdb_byte
*) filename
,
9811 len
= remote_hostio_send_command (p
- rs
->buf
, PACKET_vFile_readlink
,
9812 remote_errno
, &attachment
,
9818 ret
= xmalloc (len
+ 1);
9820 read_len
= remote_unescape_input ((gdb_byte
*) attachment
, attachment_len
,
9821 (gdb_byte
*) ret
, len
);
9822 if (read_len
!= len
)
9823 error (_("Readlink returned %d, but %d bytes."), len
, read_len
);
9830 remote_fileio_errno_to_host (int errnum
)
9854 case FILEIO_ENOTDIR
:
9874 case FILEIO_ENAMETOOLONG
:
9875 return ENAMETOOLONG
;
9881 remote_hostio_error (int errnum
)
9883 int host_error
= remote_fileio_errno_to_host (errnum
);
9885 if (host_error
== -1)
9886 error (_("Unknown remote I/O error %d"), errnum
);
9888 error (_("Remote I/O error: %s"), safe_strerror (host_error
));
9892 remote_hostio_close_cleanup (void *opaque
)
9894 int fd
= *(int *) opaque
;
9897 remote_hostio_close (find_target_at (process_stratum
), fd
, &remote_errno
);
9902 remote_bfd_iovec_open (struct bfd
*abfd
, void *open_closure
)
9904 const char *filename
= bfd_get_filename (abfd
);
9905 int fd
, remote_errno
;
9908 gdb_assert (remote_filename_p (filename
));
9910 fd
= remote_hostio_open (find_target_at (process_stratum
),
9911 filename
+ 7, FILEIO_O_RDONLY
, 0, &remote_errno
);
9914 errno
= remote_fileio_errno_to_host (remote_errno
);
9915 bfd_set_error (bfd_error_system_call
);
9919 stream
= xmalloc (sizeof (int));
9925 remote_bfd_iovec_close (struct bfd
*abfd
, void *stream
)
9927 int fd
= *(int *)stream
;
9932 /* Ignore errors on close; these may happen if the remote
9933 connection was already torn down. */
9934 remote_hostio_close (find_target_at (process_stratum
), fd
, &remote_errno
);
9936 /* Zero means success. */
9941 remote_bfd_iovec_pread (struct bfd
*abfd
, void *stream
, void *buf
,
9942 file_ptr nbytes
, file_ptr offset
)
9944 int fd
= *(int *)stream
;
9946 file_ptr pos
, bytes
;
9949 while (nbytes
> pos
)
9951 bytes
= remote_hostio_pread (find_target_at (process_stratum
),
9952 fd
, (gdb_byte
*) buf
+ pos
, nbytes
- pos
,
9953 offset
+ pos
, &remote_errno
);
9955 /* Success, but no bytes, means end-of-file. */
9959 errno
= remote_fileio_errno_to_host (remote_errno
);
9960 bfd_set_error (bfd_error_system_call
);
9971 remote_bfd_iovec_stat (struct bfd
*abfd
, void *stream
, struct stat
*sb
)
9973 /* FIXME: We should probably implement remote_hostio_stat. */
9974 sb
->st_size
= INT_MAX
;
9979 remote_filename_p (const char *filename
)
9981 return strncmp (filename
,
9982 REMOTE_SYSROOT_PREFIX
,
9983 sizeof (REMOTE_SYSROOT_PREFIX
) - 1) == 0;
9987 remote_bfd_open (const char *remote_file
, const char *target
)
9989 bfd
*abfd
= gdb_bfd_openr_iovec (remote_file
, target
,
9990 remote_bfd_iovec_open
, NULL
,
9991 remote_bfd_iovec_pread
,
9992 remote_bfd_iovec_close
,
9993 remote_bfd_iovec_stat
);
9999 remote_file_put (const char *local_file
, const char *remote_file
, int from_tty
)
10001 struct cleanup
*back_to
, *close_cleanup
;
10002 int retcode
, fd
, remote_errno
, bytes
, io_size
;
10005 int bytes_in_buffer
;
10008 struct remote_state
*rs
= get_remote_state ();
10010 if (!rs
->remote_desc
)
10011 error (_("command can only be used with remote target"));
10013 file
= gdb_fopen_cloexec (local_file
, "rb");
10015 perror_with_name (local_file
);
10016 back_to
= make_cleanup_fclose (file
);
10018 fd
= remote_hostio_open (find_target_at (process_stratum
),
10019 remote_file
, (FILEIO_O_WRONLY
| FILEIO_O_CREAT
10021 0700, &remote_errno
);
10023 remote_hostio_error (remote_errno
);
10025 /* Send up to this many bytes at once. They won't all fit in the
10026 remote packet limit, so we'll transfer slightly fewer. */
10027 io_size
= get_remote_packet_size ();
10028 buffer
= xmalloc (io_size
);
10029 make_cleanup (xfree
, buffer
);
10031 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
10033 bytes_in_buffer
= 0;
10036 while (bytes_in_buffer
|| !saw_eof
)
10040 bytes
= fread (buffer
+ bytes_in_buffer
, 1,
10041 io_size
- bytes_in_buffer
,
10046 error (_("Error reading %s."), local_file
);
10049 /* EOF. Unless there is something still in the
10050 buffer from the last iteration, we are done. */
10052 if (bytes_in_buffer
== 0)
10060 bytes
+= bytes_in_buffer
;
10061 bytes_in_buffer
= 0;
10063 retcode
= remote_hostio_pwrite (find_target_at (process_stratum
),
10065 offset
, &remote_errno
);
10068 remote_hostio_error (remote_errno
);
10069 else if (retcode
== 0)
10070 error (_("Remote write of %d bytes returned 0!"), bytes
);
10071 else if (retcode
< bytes
)
10073 /* Short write. Save the rest of the read data for the next
10075 bytes_in_buffer
= bytes
- retcode
;
10076 memmove (buffer
, buffer
+ retcode
, bytes_in_buffer
);
10082 discard_cleanups (close_cleanup
);
10083 if (remote_hostio_close (find_target_at (process_stratum
), fd
, &remote_errno
))
10084 remote_hostio_error (remote_errno
);
10087 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file
);
10088 do_cleanups (back_to
);
10092 remote_file_get (const char *remote_file
, const char *local_file
, int from_tty
)
10094 struct cleanup
*back_to
, *close_cleanup
;
10095 int fd
, remote_errno
, bytes
, io_size
;
10099 struct remote_state
*rs
= get_remote_state ();
10101 if (!rs
->remote_desc
)
10102 error (_("command can only be used with remote target"));
10104 fd
= remote_hostio_open (find_target_at (process_stratum
),
10105 remote_file
, FILEIO_O_RDONLY
, 0, &remote_errno
);
10107 remote_hostio_error (remote_errno
);
10109 file
= gdb_fopen_cloexec (local_file
, "wb");
10111 perror_with_name (local_file
);
10112 back_to
= make_cleanup_fclose (file
);
10114 /* Send up to this many bytes at once. They won't all fit in the
10115 remote packet limit, so we'll transfer slightly fewer. */
10116 io_size
= get_remote_packet_size ();
10117 buffer
= xmalloc (io_size
);
10118 make_cleanup (xfree
, buffer
);
10120 close_cleanup
= make_cleanup (remote_hostio_close_cleanup
, &fd
);
10125 bytes
= remote_hostio_pread (find_target_at (process_stratum
),
10126 fd
, buffer
, io_size
, offset
, &remote_errno
);
10128 /* Success, but no bytes, means end-of-file. */
10131 remote_hostio_error (remote_errno
);
10135 bytes
= fwrite (buffer
, 1, bytes
, file
);
10137 perror_with_name (local_file
);
10140 discard_cleanups (close_cleanup
);
10141 if (remote_hostio_close (find_target_at (process_stratum
), fd
, &remote_errno
))
10142 remote_hostio_error (remote_errno
);
10145 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file
);
10146 do_cleanups (back_to
);
10150 remote_file_delete (const char *remote_file
, int from_tty
)
10152 int retcode
, remote_errno
;
10153 struct remote_state
*rs
= get_remote_state ();
10155 if (!rs
->remote_desc
)
10156 error (_("command can only be used with remote target"));
10158 retcode
= remote_hostio_unlink (find_target_at (process_stratum
),
10159 remote_file
, &remote_errno
);
10161 remote_hostio_error (remote_errno
);
10164 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file
);
10168 remote_put_command (char *args
, int from_tty
)
10170 struct cleanup
*back_to
;
10174 error_no_arg (_("file to put"));
10176 argv
= gdb_buildargv (args
);
10177 back_to
= make_cleanup_freeargv (argv
);
10178 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
10179 error (_("Invalid parameters to remote put"));
10181 remote_file_put (argv
[0], argv
[1], from_tty
);
10183 do_cleanups (back_to
);
10187 remote_get_command (char *args
, int from_tty
)
10189 struct cleanup
*back_to
;
10193 error_no_arg (_("file to get"));
10195 argv
= gdb_buildargv (args
);
10196 back_to
= make_cleanup_freeargv (argv
);
10197 if (argv
[0] == NULL
|| argv
[1] == NULL
|| argv
[2] != NULL
)
10198 error (_("Invalid parameters to remote get"));
10200 remote_file_get (argv
[0], argv
[1], from_tty
);
10202 do_cleanups (back_to
);
10206 remote_delete_command (char *args
, int from_tty
)
10208 struct cleanup
*back_to
;
10212 error_no_arg (_("file to delete"));
10214 argv
= gdb_buildargv (args
);
10215 back_to
= make_cleanup_freeargv (argv
);
10216 if (argv
[0] == NULL
|| argv
[1] != NULL
)
10217 error (_("Invalid parameters to remote delete"));
10219 remote_file_delete (argv
[0], from_tty
);
10221 do_cleanups (back_to
);
10225 remote_command (char *args
, int from_tty
)
10227 help_list (remote_cmdlist
, "remote ", -1, gdb_stdout
);
10231 remote_can_execute_reverse (struct target_ops
*self
)
10233 if (remote_protocol_packets
[PACKET_bs
].support
== PACKET_ENABLE
10234 || remote_protocol_packets
[PACKET_bc
].support
== PACKET_ENABLE
)
10241 remote_supports_non_stop (struct target_ops
*self
)
10247 remote_supports_disable_randomization (struct target_ops
*self
)
10249 /* Only supported in extended mode. */
10254 remote_supports_multi_process (struct target_ops
*self
)
10256 struct remote_state
*rs
= get_remote_state ();
10258 /* Only extended-remote handles being attached to multiple
10259 processes, even though plain remote can use the multi-process
10260 thread id extensions, so that GDB knows the target process's
10262 return rs
->extended
&& remote_multi_process_p (rs
);
10266 remote_supports_cond_tracepoints (void)
10268 struct remote_state
*rs
= get_remote_state ();
10270 return rs
->cond_tracepoints
;
10274 remote_supports_cond_breakpoints (struct target_ops
*self
)
10276 struct remote_state
*rs
= get_remote_state ();
10278 return rs
->cond_breakpoints
;
10282 remote_supports_fast_tracepoints (void)
10284 struct remote_state
*rs
= get_remote_state ();
10286 return rs
->fast_tracepoints
;
10290 remote_supports_static_tracepoints (void)
10292 struct remote_state
*rs
= get_remote_state ();
10294 return rs
->static_tracepoints
;
10298 remote_supports_install_in_trace (void)
10300 struct remote_state
*rs
= get_remote_state ();
10302 return rs
->install_in_trace
;
10306 remote_supports_enable_disable_tracepoint (struct target_ops
*self
)
10308 struct remote_state
*rs
= get_remote_state ();
10310 return rs
->enable_disable_tracepoints
;
10314 remote_supports_string_tracing (struct target_ops
*self
)
10316 struct remote_state
*rs
= get_remote_state ();
10318 return rs
->string_tracing
;
10322 remote_can_run_breakpoint_commands (struct target_ops
*self
)
10324 struct remote_state
*rs
= get_remote_state ();
10326 return rs
->breakpoint_commands
;
10330 remote_trace_init (struct target_ops
*self
)
10333 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10334 if (strcmp (target_buf
, "OK") != 0)
10335 error (_("Target does not support this command."));
10338 static void free_actions_list (char **actions_list
);
10339 static void free_actions_list_cleanup_wrapper (void *);
10341 free_actions_list_cleanup_wrapper (void *al
)
10343 free_actions_list (al
);
10347 free_actions_list (char **actions_list
)
10351 if (actions_list
== 0)
10354 for (ndx
= 0; actions_list
[ndx
]; ndx
++)
10355 xfree (actions_list
[ndx
]);
10357 xfree (actions_list
);
10360 /* Recursive routine to walk through command list including loops, and
10361 download packets for each command. */
10364 remote_download_command_source (int num
, ULONGEST addr
,
10365 struct command_line
*cmds
)
10367 struct remote_state
*rs
= get_remote_state ();
10368 struct command_line
*cmd
;
10370 for (cmd
= cmds
; cmd
; cmd
= cmd
->next
)
10372 QUIT
; /* Allow user to bail out with ^C. */
10373 strcpy (rs
->buf
, "QTDPsrc:");
10374 encode_source_string (num
, addr
, "cmd", cmd
->line
,
10375 rs
->buf
+ strlen (rs
->buf
),
10376 rs
->buf_size
- strlen (rs
->buf
));
10378 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10379 if (strcmp (target_buf
, "OK"))
10380 warning (_("Target does not support source download."));
10382 if (cmd
->control_type
== while_control
10383 || cmd
->control_type
== while_stepping_control
)
10385 remote_download_command_source (num
, addr
, *cmd
->body_list
);
10387 QUIT
; /* Allow user to bail out with ^C. */
10388 strcpy (rs
->buf
, "QTDPsrc:");
10389 encode_source_string (num
, addr
, "cmd", "end",
10390 rs
->buf
+ strlen (rs
->buf
),
10391 rs
->buf_size
- strlen (rs
->buf
));
10393 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10394 if (strcmp (target_buf
, "OK"))
10395 warning (_("Target does not support source download."));
10401 remote_download_tracepoint (struct target_ops
*self
, struct bp_location
*loc
)
10403 #define BUF_SIZE 2048
10407 char buf
[BUF_SIZE
];
10408 char **tdp_actions
;
10409 char **stepping_actions
;
10411 struct cleanup
*old_chain
= NULL
;
10412 struct agent_expr
*aexpr
;
10413 struct cleanup
*aexpr_chain
= NULL
;
10415 struct breakpoint
*b
= loc
->owner
;
10416 struct tracepoint
*t
= (struct tracepoint
*) b
;
10418 encode_actions_rsp (loc
, &tdp_actions
, &stepping_actions
);
10419 old_chain
= make_cleanup (free_actions_list_cleanup_wrapper
,
10421 (void) make_cleanup (free_actions_list_cleanup_wrapper
,
10424 tpaddr
= loc
->address
;
10425 sprintf_vma (addrbuf
, tpaddr
);
10426 xsnprintf (buf
, BUF_SIZE
, "QTDP:%x:%s:%c:%lx:%x", b
->number
,
10427 addrbuf
, /* address */
10428 (b
->enable_state
== bp_enabled
? 'E' : 'D'),
10429 t
->step_count
, t
->pass_count
);
10430 /* Fast tracepoints are mostly handled by the target, but we can
10431 tell the target how big of an instruction block should be moved
10433 if (b
->type
== bp_fast_tracepoint
)
10435 /* Only test for support at download time; we may not know
10436 target capabilities at definition time. */
10437 if (remote_supports_fast_tracepoints ())
10441 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
10442 tpaddr
, &isize
, NULL
))
10443 xsnprintf (buf
+ strlen (buf
), BUF_SIZE
- strlen (buf
), ":F%x",
10446 /* If it passed validation at definition but fails now,
10447 something is very wrong. */
10448 internal_error (__FILE__
, __LINE__
,
10449 _("Fast tracepoint not "
10450 "valid during download"));
10453 /* Fast tracepoints are functionally identical to regular
10454 tracepoints, so don't take lack of support as a reason to
10455 give up on the trace run. */
10456 warning (_("Target does not support fast tracepoints, "
10457 "downloading %d as regular tracepoint"), b
->number
);
10459 else if (b
->type
== bp_static_tracepoint
)
10461 /* Only test for support at download time; we may not know
10462 target capabilities at definition time. */
10463 if (remote_supports_static_tracepoints ())
10465 struct static_tracepoint_marker marker
;
10467 if (target_static_tracepoint_marker_at (tpaddr
, &marker
))
10468 strcat (buf
, ":S");
10470 error (_("Static tracepoint not valid during download"));
10473 /* Fast tracepoints are functionally identical to regular
10474 tracepoints, so don't take lack of support as a reason
10475 to give up on the trace run. */
10476 error (_("Target does not support static tracepoints"));
10478 /* If the tracepoint has a conditional, make it into an agent
10479 expression and append to the definition. */
10482 /* Only test support at download time, we may not know target
10483 capabilities at definition time. */
10484 if (remote_supports_cond_tracepoints ())
10486 aexpr
= gen_eval_for_expr (tpaddr
, loc
->cond
);
10487 aexpr_chain
= make_cleanup_free_agent_expr (aexpr
);
10488 xsnprintf (buf
+ strlen (buf
), BUF_SIZE
- strlen (buf
), ":X%x,",
10490 pkt
= buf
+ strlen (buf
);
10491 for (ndx
= 0; ndx
< aexpr
->len
; ++ndx
)
10492 pkt
= pack_hex_byte (pkt
, aexpr
->buf
[ndx
]);
10494 do_cleanups (aexpr_chain
);
10497 warning (_("Target does not support conditional tracepoints, "
10498 "ignoring tp %d cond"), b
->number
);
10501 if (b
->commands
|| *default_collect
)
10504 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10505 if (strcmp (target_buf
, "OK"))
10506 error (_("Target does not support tracepoints."));
10508 /* do_single_steps (t); */
10511 for (ndx
= 0; tdp_actions
[ndx
]; ndx
++)
10513 QUIT
; /* Allow user to bail out with ^C. */
10514 xsnprintf (buf
, BUF_SIZE
, "QTDP:-%x:%s:%s%c",
10515 b
->number
, addrbuf
, /* address */
10517 ((tdp_actions
[ndx
+ 1] || stepping_actions
)
10520 remote_get_noisy_reply (&target_buf
,
10522 if (strcmp (target_buf
, "OK"))
10523 error (_("Error on target while setting tracepoints."));
10526 if (stepping_actions
)
10528 for (ndx
= 0; stepping_actions
[ndx
]; ndx
++)
10530 QUIT
; /* Allow user to bail out with ^C. */
10531 xsnprintf (buf
, BUF_SIZE
, "QTDP:-%x:%s:%s%s%s",
10532 b
->number
, addrbuf
, /* address */
10533 ((ndx
== 0) ? "S" : ""),
10534 stepping_actions
[ndx
],
10535 (stepping_actions
[ndx
+ 1] ? "-" : ""));
10537 remote_get_noisy_reply (&target_buf
,
10539 if (strcmp (target_buf
, "OK"))
10540 error (_("Error on target while setting tracepoints."));
10544 if (remote_protocol_packets
[PACKET_TracepointSource
].support
10547 if (b
->addr_string
)
10549 strcpy (buf
, "QTDPsrc:");
10550 encode_source_string (b
->number
, loc
->address
,
10551 "at", b
->addr_string
, buf
+ strlen (buf
),
10552 2048 - strlen (buf
));
10555 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10556 if (strcmp (target_buf
, "OK"))
10557 warning (_("Target does not support source download."));
10559 if (b
->cond_string
)
10561 strcpy (buf
, "QTDPsrc:");
10562 encode_source_string (b
->number
, loc
->address
,
10563 "cond", b
->cond_string
, buf
+ strlen (buf
),
10564 2048 - strlen (buf
));
10566 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10567 if (strcmp (target_buf
, "OK"))
10568 warning (_("Target does not support source download."));
10570 remote_download_command_source (b
->number
, loc
->address
,
10571 breakpoint_commands (b
));
10574 do_cleanups (old_chain
);
10578 remote_can_download_tracepoint (struct target_ops
*self
)
10580 struct remote_state
*rs
= get_remote_state ();
10581 struct trace_status
*ts
;
10584 /* Don't try to install tracepoints until we've relocated our
10585 symbols, and fetched and merged the target's tracepoint list with
10587 if (rs
->starting_up
)
10590 ts
= current_trace_status ();
10591 status
= remote_get_trace_status (self
, ts
);
10593 if (status
== -1 || !ts
->running_known
|| !ts
->running
)
10596 /* If we are in a tracing experiment, but remote stub doesn't support
10597 installing tracepoint in trace, we have to return. */
10598 if (!remote_supports_install_in_trace ())
10606 remote_download_trace_state_variable (struct target_ops
*self
,
10607 struct trace_state_variable
*tsv
)
10609 struct remote_state
*rs
= get_remote_state ();
10612 xsnprintf (rs
->buf
, get_remote_packet_size (), "QTDV:%x:%s:%x:",
10613 tsv
->number
, phex ((ULONGEST
) tsv
->initial_value
, 8),
10615 p
= rs
->buf
+ strlen (rs
->buf
);
10616 if ((p
- rs
->buf
) + strlen (tsv
->name
) * 2 >= get_remote_packet_size ())
10617 error (_("Trace state variable name too long for tsv definition packet"));
10618 p
+= 2 * bin2hex ((gdb_byte
*) (tsv
->name
), p
, strlen (tsv
->name
));
10621 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10622 if (*target_buf
== '\0')
10623 error (_("Target does not support this command."));
10624 if (strcmp (target_buf
, "OK") != 0)
10625 error (_("Error on target while downloading trace state variable."));
10629 remote_enable_tracepoint (struct target_ops
*self
,
10630 struct bp_location
*location
)
10632 struct remote_state
*rs
= get_remote_state ();
10635 sprintf_vma (addr_buf
, location
->address
);
10636 xsnprintf (rs
->buf
, get_remote_packet_size (), "QTEnable:%x:%s",
10637 location
->owner
->number
, addr_buf
);
10639 remote_get_noisy_reply (&rs
->buf
, &rs
->buf_size
);
10640 if (*rs
->buf
== '\0')
10641 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
10642 if (strcmp (rs
->buf
, "OK") != 0)
10643 error (_("Error on target while enabling tracepoint."));
10647 remote_disable_tracepoint (struct target_ops
*self
,
10648 struct bp_location
*location
)
10650 struct remote_state
*rs
= get_remote_state ();
10653 sprintf_vma (addr_buf
, location
->address
);
10654 xsnprintf (rs
->buf
, get_remote_packet_size (), "QTDisable:%x:%s",
10655 location
->owner
->number
, addr_buf
);
10657 remote_get_noisy_reply (&rs
->buf
, &rs
->buf_size
);
10658 if (*rs
->buf
== '\0')
10659 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
10660 if (strcmp (rs
->buf
, "OK") != 0)
10661 error (_("Error on target while disabling tracepoint."));
10665 remote_trace_set_readonly_regions (struct target_ops
*self
)
10669 bfd_size_type size
;
10675 return; /* No information to give. */
10677 strcpy (target_buf
, "QTro");
10678 offset
= strlen (target_buf
);
10679 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
10681 char tmp1
[40], tmp2
[40];
10684 if ((s
->flags
& SEC_LOAD
) == 0 ||
10685 /* (s->flags & SEC_CODE) == 0 || */
10686 (s
->flags
& SEC_READONLY
) == 0)
10690 vma
= bfd_get_section_vma (abfd
, s
);
10691 size
= bfd_get_section_size (s
);
10692 sprintf_vma (tmp1
, vma
);
10693 sprintf_vma (tmp2
, vma
+ size
);
10694 sec_length
= 1 + strlen (tmp1
) + 1 + strlen (tmp2
);
10695 if (offset
+ sec_length
+ 1 > target_buf_size
)
10697 if (remote_protocol_packets
[PACKET_qXfer_traceframe_info
].support
10700 Too many sections for read-only sections definition packet."));
10703 xsnprintf (target_buf
+ offset
, target_buf_size
- offset
, ":%s,%s",
10705 offset
+= sec_length
;
10709 putpkt (target_buf
);
10710 getpkt (&target_buf
, &target_buf_size
, 0);
10715 remote_trace_start (struct target_ops
*self
)
10717 putpkt ("QTStart");
10718 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10719 if (*target_buf
== '\0')
10720 error (_("Target does not support this command."));
10721 if (strcmp (target_buf
, "OK") != 0)
10722 error (_("Bogus reply from target: %s"), target_buf
);
10726 remote_get_trace_status (struct target_ops
*self
, struct trace_status
*ts
)
10728 /* Initialize it just to avoid a GCC false warning. */
10730 /* FIXME we need to get register block size some other way. */
10731 extern int trace_regblock_size
;
10732 volatile struct gdb_exception ex
;
10733 enum packet_result result
;
10735 if (remote_protocol_packets
[PACKET_qTStatus
].support
== PACKET_DISABLE
)
10738 trace_regblock_size
= get_remote_arch_state ()->sizeof_g_packet
;
10740 putpkt ("qTStatus");
10742 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
10744 p
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10748 if (ex
.error
!= TARGET_CLOSE_ERROR
)
10750 exception_fprintf (gdb_stderr
, ex
, "qTStatus: ");
10753 throw_exception (ex
);
10756 result
= packet_ok (p
, &remote_protocol_packets
[PACKET_qTStatus
]);
10758 /* If the remote target doesn't do tracing, flag it. */
10759 if (result
== PACKET_UNKNOWN
)
10762 /* We're working with a live target. */
10763 ts
->filename
= NULL
;
10766 error (_("Bogus trace status reply from target: %s"), target_buf
);
10768 /* Function 'parse_trace_status' sets default value of each field of
10769 'ts' at first, so we don't have to do it here. */
10770 parse_trace_status (p
, ts
);
10772 return ts
->running
;
10776 remote_get_tracepoint_status (struct target_ops
*self
, struct breakpoint
*bp
,
10777 struct uploaded_tp
*utp
)
10779 struct remote_state
*rs
= get_remote_state ();
10781 struct bp_location
*loc
;
10782 struct tracepoint
*tp
= (struct tracepoint
*) bp
;
10783 size_t size
= get_remote_packet_size ();
10787 tp
->base
.hit_count
= 0;
10788 tp
->traceframe_usage
= 0;
10789 for (loc
= tp
->base
.loc
; loc
; loc
= loc
->next
)
10791 /* If the tracepoint was never downloaded, don't go asking for
10793 if (tp
->number_on_target
== 0)
10795 xsnprintf (rs
->buf
, size
, "qTP:%x:%s", tp
->number_on_target
,
10796 phex_nz (loc
->address
, 0));
10798 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10799 if (reply
&& *reply
)
10802 parse_tracepoint_status (reply
+ 1, bp
, utp
);
10808 utp
->hit_count
= 0;
10809 utp
->traceframe_usage
= 0;
10810 xsnprintf (rs
->buf
, size
, "qTP:%x:%s", utp
->number
,
10811 phex_nz (utp
->addr
, 0));
10813 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10814 if (reply
&& *reply
)
10817 parse_tracepoint_status (reply
+ 1, bp
, utp
);
10823 remote_trace_stop (struct target_ops
*self
)
10826 remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10827 if (*target_buf
== '\0')
10828 error (_("Target does not support this command."));
10829 if (strcmp (target_buf
, "OK") != 0)
10830 error (_("Bogus reply from target: %s"), target_buf
);
10834 remote_trace_find (struct target_ops
*self
,
10835 enum trace_find_type type
, int num
,
10836 CORE_ADDR addr1
, CORE_ADDR addr2
,
10839 struct remote_state
*rs
= get_remote_state ();
10840 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
10842 int target_frameno
= -1, target_tracept
= -1;
10844 /* Lookups other than by absolute frame number depend on the current
10845 trace selected, so make sure it is correct on the remote end
10847 if (type
!= tfind_number
)
10848 set_remote_traceframe ();
10851 strcpy (p
, "QTFrame:");
10852 p
= strchr (p
, '\0');
10856 xsnprintf (p
, endbuf
- p
, "%x", num
);
10859 xsnprintf (p
, endbuf
- p
, "pc:%s", phex_nz (addr1
, 0));
10862 xsnprintf (p
, endbuf
- p
, "tdp:%x", num
);
10865 xsnprintf (p
, endbuf
- p
, "range:%s:%s", phex_nz (addr1
, 0),
10866 phex_nz (addr2
, 0));
10868 case tfind_outside
:
10869 xsnprintf (p
, endbuf
- p
, "outside:%s:%s", phex_nz (addr1
, 0),
10870 phex_nz (addr2
, 0));
10873 error (_("Unknown trace find type %d"), type
);
10877 reply
= remote_get_noisy_reply (&(rs
->buf
), &rs
->buf_size
);
10878 if (*reply
== '\0')
10879 error (_("Target does not support this command."));
10881 while (reply
&& *reply
)
10886 target_frameno
= (int) strtol (p
, &reply
, 16);
10888 error (_("Unable to parse trace frame number"));
10889 /* Don't update our remote traceframe number cache on failure
10890 to select a remote traceframe. */
10891 if (target_frameno
== -1)
10896 target_tracept
= (int) strtol (p
, &reply
, 16);
10898 error (_("Unable to parse tracepoint number"));
10900 case 'O': /* "OK"? */
10901 if (reply
[1] == 'K' && reply
[2] == '\0')
10904 error (_("Bogus reply from target: %s"), reply
);
10907 error (_("Bogus reply from target: %s"), reply
);
10910 *tpp
= target_tracept
;
10912 rs
->remote_traceframe_number
= target_frameno
;
10913 return target_frameno
;
10917 remote_get_trace_state_variable_value (int tsvnum
, LONGEST
*val
)
10919 struct remote_state
*rs
= get_remote_state ();
10923 set_remote_traceframe ();
10925 xsnprintf (rs
->buf
, get_remote_packet_size (), "qTV:%x", tsvnum
);
10927 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10928 if (reply
&& *reply
)
10932 unpack_varlen_hex (reply
+ 1, &uval
);
10933 *val
= (LONGEST
) uval
;
10941 remote_save_trace_data (const char *filename
)
10943 struct remote_state
*rs
= get_remote_state ();
10947 strcpy (p
, "QTSave:");
10949 if ((p
- rs
->buf
) + strlen (filename
) * 2 >= get_remote_packet_size ())
10950 error (_("Remote file name too long for trace save packet"));
10951 p
+= 2 * bin2hex ((gdb_byte
*) filename
, p
, strlen (filename
));
10954 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10955 if (*reply
== '\0')
10956 error (_("Target does not support this command."));
10957 if (strcmp (reply
, "OK") != 0)
10958 error (_("Bogus reply from target: %s"), reply
);
10962 /* This is basically a memory transfer, but needs to be its own packet
10963 because we don't know how the target actually organizes its trace
10964 memory, plus we want to be able to ask for as much as possible, but
10965 not be unhappy if we don't get as much as we ask for. */
10968 remote_get_raw_trace_data (gdb_byte
*buf
, ULONGEST offset
, LONGEST len
)
10970 struct remote_state
*rs
= get_remote_state ();
10976 strcpy (p
, "qTBuffer:");
10978 p
+= hexnumstr (p
, offset
);
10980 p
+= hexnumstr (p
, len
);
10984 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
10985 if (reply
&& *reply
)
10987 /* 'l' by itself means we're at the end of the buffer and
10988 there is nothing more to get. */
10992 /* Convert the reply into binary. Limit the number of bytes to
10993 convert according to our passed-in buffer size, rather than
10994 what was returned in the packet; if the target is
10995 unexpectedly generous and gives us a bigger reply than we
10996 asked for, we don't want to crash. */
10997 rslt
= hex2bin (target_buf
, buf
, len
);
11001 /* Something went wrong, flag as an error. */
11006 remote_set_disconnected_tracing (int val
)
11008 struct remote_state
*rs
= get_remote_state ();
11010 if (rs
->disconnected_tracing
)
11014 xsnprintf (rs
->buf
, get_remote_packet_size (), "QTDisconnected:%x", val
);
11016 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
11017 if (*reply
== '\0')
11018 error (_("Target does not support this command."));
11019 if (strcmp (reply
, "OK") != 0)
11020 error (_("Bogus reply from target: %s"), reply
);
11023 warning (_("Target does not support disconnected tracing."));
11027 remote_core_of_thread (struct target_ops
*ops
, ptid_t ptid
)
11029 struct thread_info
*info
= find_thread_ptid (ptid
);
11031 if (info
&& info
->private)
11032 return info
->private->core
;
11037 remote_set_circular_trace_buffer (int val
)
11039 struct remote_state
*rs
= get_remote_state ();
11042 xsnprintf (rs
->buf
, get_remote_packet_size (), "QTBuffer:circular:%x", val
);
11044 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
11045 if (*reply
== '\0')
11046 error (_("Target does not support this command."));
11047 if (strcmp (reply
, "OK") != 0)
11048 error (_("Bogus reply from target: %s"), reply
);
11051 static struct traceframe_info
*
11052 remote_traceframe_info (void)
11056 /* If current traceframe is not selected, don't bother the remote
11058 if (get_traceframe_number () < 0)
11061 text
= target_read_stralloc (¤t_target
,
11062 TARGET_OBJECT_TRACEFRAME_INFO
, NULL
);
11065 struct traceframe_info
*info
;
11066 struct cleanup
*back_to
= make_cleanup (xfree
, text
);
11068 info
= parse_traceframe_info (text
);
11069 do_cleanups (back_to
);
11076 /* Handle the qTMinFTPILen packet. Returns the minimum length of
11077 instruction on which a fast tracepoint may be placed. Returns -1
11078 if the packet is not supported, and 0 if the minimum instruction
11079 length is unknown. */
11082 remote_get_min_fast_tracepoint_insn_len (void)
11084 struct remote_state
*rs
= get_remote_state ();
11087 /* If we're not debugging a process yet, the IPA can't be
11089 if (!target_has_execution
)
11092 /* Make sure the remote is pointing at the right process. */
11093 set_general_process ();
11095 xsnprintf (rs
->buf
, get_remote_packet_size (), "qTMinFTPILen");
11097 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
11098 if (*reply
== '\0')
11102 ULONGEST min_insn_len
;
11104 unpack_varlen_hex (reply
, &min_insn_len
);
11106 return (int) min_insn_len
;
11111 remote_set_trace_buffer_size (LONGEST val
)
11113 if (remote_protocol_packets
[PACKET_QTBuffer_size
].support
11116 struct remote_state
*rs
= get_remote_state ();
11117 char *buf
= rs
->buf
;
11118 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
11119 enum packet_result result
;
11121 gdb_assert (val
>= 0 || val
== -1);
11122 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTBuffer:size:");
11123 /* Send -1 as literal "-1" to avoid host size dependency. */
11127 buf
+= hexnumstr (buf
, (ULONGEST
) -val
);
11130 buf
+= hexnumstr (buf
, (ULONGEST
) val
);
11133 remote_get_noisy_reply (&rs
->buf
, &rs
->buf_size
);
11134 result
= packet_ok (rs
->buf
,
11135 &remote_protocol_packets
[PACKET_QTBuffer_size
]);
11137 if (result
!= PACKET_OK
)
11138 warning (_("Bogus reply from target: %s"), rs
->buf
);
11143 remote_set_trace_notes (const char *user
, const char *notes
,
11144 const char *stop_notes
)
11146 struct remote_state
*rs
= get_remote_state ();
11148 char *buf
= rs
->buf
;
11149 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
11152 buf
+= xsnprintf (buf
, endbuf
- buf
, "QTNotes:");
11155 buf
+= xsnprintf (buf
, endbuf
- buf
, "user:");
11156 nbytes
= bin2hex ((gdb_byte
*) user
, buf
, strlen (user
));
11162 buf
+= xsnprintf (buf
, endbuf
- buf
, "notes:");
11163 nbytes
= bin2hex ((gdb_byte
*) notes
, buf
, strlen (notes
));
11169 buf
+= xsnprintf (buf
, endbuf
- buf
, "tstop:");
11170 nbytes
= bin2hex ((gdb_byte
*) stop_notes
, buf
, strlen (stop_notes
));
11174 /* Ensure the buffer is terminated. */
11178 reply
= remote_get_noisy_reply (&target_buf
, &target_buf_size
);
11179 if (*reply
== '\0')
11182 if (strcmp (reply
, "OK") != 0)
11183 error (_("Bogus reply from target: %s"), reply
);
11189 remote_use_agent (int use
)
11191 if (remote_protocol_packets
[PACKET_QAgent
].support
!= PACKET_DISABLE
)
11193 struct remote_state
*rs
= get_remote_state ();
11195 /* If the stub supports QAgent. */
11196 xsnprintf (rs
->buf
, get_remote_packet_size (), "QAgent:%d", use
);
11198 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11200 if (strcmp (rs
->buf
, "OK") == 0)
11211 remote_can_use_agent (void)
11213 return (remote_protocol_packets
[PACKET_QAgent
].support
!= PACKET_DISABLE
);
11216 struct btrace_target_info
11218 /* The ptid of the traced thread. */
11222 /* Check whether the target supports branch tracing. */
11225 remote_supports_btrace (struct target_ops
*self
)
11227 if (remote_protocol_packets
[PACKET_Qbtrace_off
].support
!= PACKET_ENABLE
)
11229 if (remote_protocol_packets
[PACKET_Qbtrace_bts
].support
!= PACKET_ENABLE
)
11231 if (remote_protocol_packets
[PACKET_qXfer_btrace
].support
!= PACKET_ENABLE
)
11237 /* Enable branch tracing. */
11239 static struct btrace_target_info
*
11240 remote_enable_btrace (ptid_t ptid
)
11242 struct btrace_target_info
*tinfo
= NULL
;
11243 struct packet_config
*packet
= &remote_protocol_packets
[PACKET_Qbtrace_bts
];
11244 struct remote_state
*rs
= get_remote_state ();
11245 char *buf
= rs
->buf
;
11246 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
11248 if (packet
->support
!= PACKET_ENABLE
)
11249 error (_("Target does not support branch tracing."));
11251 set_general_thread (ptid
);
11253 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s", packet
->name
);
11255 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11257 if (packet_ok (rs
->buf
, packet
) == PACKET_ERROR
)
11259 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
11260 error (_("Could not enable branch tracing for %s: %s"),
11261 target_pid_to_str (ptid
), rs
->buf
+ 2);
11263 error (_("Could not enable branch tracing for %s."),
11264 target_pid_to_str (ptid
));
11267 tinfo
= xzalloc (sizeof (*tinfo
));
11268 tinfo
->ptid
= ptid
;
11273 /* Disable branch tracing. */
11276 remote_disable_btrace (struct btrace_target_info
*tinfo
)
11278 struct packet_config
*packet
= &remote_protocol_packets
[PACKET_Qbtrace_off
];
11279 struct remote_state
*rs
= get_remote_state ();
11280 char *buf
= rs
->buf
;
11281 char *endbuf
= rs
->buf
+ get_remote_packet_size ();
11283 if (packet
->support
!= PACKET_ENABLE
)
11284 error (_("Target does not support branch tracing."));
11286 set_general_thread (tinfo
->ptid
);
11288 buf
+= xsnprintf (buf
, endbuf
- buf
, "%s", packet
->name
);
11290 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11292 if (packet_ok (rs
->buf
, packet
) == PACKET_ERROR
)
11294 if (rs
->buf
[0] == 'E' && rs
->buf
[1] == '.')
11295 error (_("Could not disable branch tracing for %s: %s"),
11296 target_pid_to_str (tinfo
->ptid
), rs
->buf
+ 2);
11298 error (_("Could not disable branch tracing for %s."),
11299 target_pid_to_str (tinfo
->ptid
));
11305 /* Teardown branch tracing. */
11308 remote_teardown_btrace (struct btrace_target_info
*tinfo
)
11310 /* We must not talk to the target during teardown. */
11314 /* Read the branch trace. */
11316 static enum btrace_error
11317 remote_read_btrace (VEC (btrace_block_s
) **btrace
,
11318 struct btrace_target_info
*tinfo
,
11319 enum btrace_read_type type
)
11321 struct packet_config
*packet
= &remote_protocol_packets
[PACKET_qXfer_btrace
];
11322 struct remote_state
*rs
= get_remote_state ();
11323 struct cleanup
*cleanup
;
11327 if (packet
->support
!= PACKET_ENABLE
)
11328 error (_("Target does not support branch tracing."));
11330 #if !defined(HAVE_LIBEXPAT)
11331 error (_("Cannot process branch tracing result. XML parsing not supported."));
11336 case BTRACE_READ_ALL
:
11339 case BTRACE_READ_NEW
:
11342 case BTRACE_READ_DELTA
:
11346 internal_error (__FILE__
, __LINE__
,
11347 _("Bad branch tracing read type: %u."),
11348 (unsigned int) type
);
11351 xml
= target_read_stralloc (¤t_target
,
11352 TARGET_OBJECT_BTRACE
, annex
);
11354 return BTRACE_ERR_UNKNOWN
;
11356 cleanup
= make_cleanup (xfree
, xml
);
11357 *btrace
= parse_xml_btrace (xml
);
11358 do_cleanups (cleanup
);
11360 return BTRACE_ERR_NONE
;
11364 remote_augmented_libraries_svr4_read (void)
11366 struct remote_state
*rs
= get_remote_state ();
11368 return rs
->augmented_libraries_svr4_read
;
11371 /* Implementation of to_load. */
11374 remote_load (struct target_ops
*self
, char *name
, int from_tty
)
11376 generic_load (name
, from_tty
);
11380 init_remote_ops (void)
11382 remote_ops
.to_shortname
= "remote";
11383 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
11384 remote_ops
.to_doc
=
11385 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11386 Specify the serial device it is connected to\n\
11387 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
11388 remote_ops
.to_open
= remote_open
;
11389 remote_ops
.to_close
= remote_close
;
11390 remote_ops
.to_detach
= remote_detach
;
11391 remote_ops
.to_disconnect
= remote_disconnect
;
11392 remote_ops
.to_resume
= remote_resume
;
11393 remote_ops
.to_wait
= remote_wait
;
11394 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
11395 remote_ops
.to_store_registers
= remote_store_registers
;
11396 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
11397 remote_ops
.to_files_info
= remote_files_info
;
11398 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
11399 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
11400 remote_ops
.to_stopped_by_watchpoint
= remote_stopped_by_watchpoint
;
11401 remote_ops
.to_stopped_data_address
= remote_stopped_data_address
;
11402 remote_ops
.to_watchpoint_addr_within_range
=
11403 remote_watchpoint_addr_within_range
;
11404 remote_ops
.to_can_use_hw_breakpoint
= remote_check_watch_resources
;
11405 remote_ops
.to_insert_hw_breakpoint
= remote_insert_hw_breakpoint
;
11406 remote_ops
.to_remove_hw_breakpoint
= remote_remove_hw_breakpoint
;
11407 remote_ops
.to_region_ok_for_hw_watchpoint
11408 = remote_region_ok_for_hw_watchpoint
;
11409 remote_ops
.to_insert_watchpoint
= remote_insert_watchpoint
;
11410 remote_ops
.to_remove_watchpoint
= remote_remove_watchpoint
;
11411 remote_ops
.to_kill
= remote_kill
;
11412 remote_ops
.to_load
= remote_load
;
11413 remote_ops
.to_mourn_inferior
= remote_mourn
;
11414 remote_ops
.to_pass_signals
= remote_pass_signals
;
11415 remote_ops
.to_program_signals
= remote_program_signals
;
11416 remote_ops
.to_thread_alive
= remote_thread_alive
;
11417 remote_ops
.to_find_new_threads
= remote_threads_info
;
11418 remote_ops
.to_pid_to_str
= remote_pid_to_str
;
11419 remote_ops
.to_extra_thread_info
= remote_threads_extra_info
;
11420 remote_ops
.to_get_ada_task_ptid
= remote_get_ada_task_ptid
;
11421 remote_ops
.to_stop
= remote_stop
;
11422 remote_ops
.to_xfer_partial
= remote_xfer_partial
;
11423 remote_ops
.to_rcmd
= remote_rcmd
;
11424 remote_ops
.to_log_command
= serial_log_command
;
11425 remote_ops
.to_get_thread_local_address
= remote_get_thread_local_address
;
11426 remote_ops
.to_stratum
= process_stratum
;
11427 remote_ops
.to_has_all_memory
= default_child_has_all_memory
;
11428 remote_ops
.to_has_memory
= default_child_has_memory
;
11429 remote_ops
.to_has_stack
= default_child_has_stack
;
11430 remote_ops
.to_has_registers
= default_child_has_registers
;
11431 remote_ops
.to_has_execution
= default_child_has_execution
;
11432 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
11433 remote_ops
.to_can_execute_reverse
= remote_can_execute_reverse
;
11434 remote_ops
.to_magic
= OPS_MAGIC
;
11435 remote_ops
.to_memory_map
= remote_memory_map
;
11436 remote_ops
.to_flash_erase
= remote_flash_erase
;
11437 remote_ops
.to_flash_done
= remote_flash_done
;
11438 remote_ops
.to_read_description
= remote_read_description
;
11439 remote_ops
.to_search_memory
= remote_search_memory
;
11440 remote_ops
.to_can_async_p
= remote_can_async_p
;
11441 remote_ops
.to_is_async_p
= remote_is_async_p
;
11442 remote_ops
.to_async
= remote_async
;
11443 remote_ops
.to_terminal_inferior
= remote_terminal_inferior
;
11444 remote_ops
.to_terminal_ours
= remote_terminal_ours
;
11445 remote_ops
.to_supports_non_stop
= remote_supports_non_stop
;
11446 remote_ops
.to_supports_multi_process
= remote_supports_multi_process
;
11447 remote_ops
.to_supports_disable_randomization
11448 = remote_supports_disable_randomization
;
11449 remote_ops
.to_fileio_open
= remote_hostio_open
;
11450 remote_ops
.to_fileio_pwrite
= remote_hostio_pwrite
;
11451 remote_ops
.to_fileio_pread
= remote_hostio_pread
;
11452 remote_ops
.to_fileio_close
= remote_hostio_close
;
11453 remote_ops
.to_fileio_unlink
= remote_hostio_unlink
;
11454 remote_ops
.to_fileio_readlink
= remote_hostio_readlink
;
11455 remote_ops
.to_supports_enable_disable_tracepoint
= remote_supports_enable_disable_tracepoint
;
11456 remote_ops
.to_supports_string_tracing
= remote_supports_string_tracing
;
11457 remote_ops
.to_supports_evaluation_of_breakpoint_conditions
= remote_supports_cond_breakpoints
;
11458 remote_ops
.to_can_run_breakpoint_commands
= remote_can_run_breakpoint_commands
;
11459 remote_ops
.to_trace_init
= remote_trace_init
;
11460 remote_ops
.to_download_tracepoint
= remote_download_tracepoint
;
11461 remote_ops
.to_can_download_tracepoint
= remote_can_download_tracepoint
;
11462 remote_ops
.to_download_trace_state_variable
11463 = remote_download_trace_state_variable
;
11464 remote_ops
.to_enable_tracepoint
= remote_enable_tracepoint
;
11465 remote_ops
.to_disable_tracepoint
= remote_disable_tracepoint
;
11466 remote_ops
.to_trace_set_readonly_regions
= remote_trace_set_readonly_regions
;
11467 remote_ops
.to_trace_start
= remote_trace_start
;
11468 remote_ops
.to_get_trace_status
= remote_get_trace_status
;
11469 remote_ops
.to_get_tracepoint_status
= remote_get_tracepoint_status
;
11470 remote_ops
.to_trace_stop
= remote_trace_stop
;
11471 remote_ops
.to_trace_find
= remote_trace_find
;
11472 remote_ops
.to_get_trace_state_variable_value
11473 = remote_get_trace_state_variable_value
;
11474 remote_ops
.to_save_trace_data
= remote_save_trace_data
;
11475 remote_ops
.to_upload_tracepoints
= remote_upload_tracepoints
;
11476 remote_ops
.to_upload_trace_state_variables
11477 = remote_upload_trace_state_variables
;
11478 remote_ops
.to_get_raw_trace_data
= remote_get_raw_trace_data
;
11479 remote_ops
.to_get_min_fast_tracepoint_insn_len
= remote_get_min_fast_tracepoint_insn_len
;
11480 remote_ops
.to_set_disconnected_tracing
= remote_set_disconnected_tracing
;
11481 remote_ops
.to_set_circular_trace_buffer
= remote_set_circular_trace_buffer
;
11482 remote_ops
.to_set_trace_buffer_size
= remote_set_trace_buffer_size
;
11483 remote_ops
.to_set_trace_notes
= remote_set_trace_notes
;
11484 remote_ops
.to_core_of_thread
= remote_core_of_thread
;
11485 remote_ops
.to_verify_memory
= remote_verify_memory
;
11486 remote_ops
.to_get_tib_address
= remote_get_tib_address
;
11487 remote_ops
.to_set_permissions
= remote_set_permissions
;
11488 remote_ops
.to_static_tracepoint_marker_at
11489 = remote_static_tracepoint_marker_at
;
11490 remote_ops
.to_static_tracepoint_markers_by_strid
11491 = remote_static_tracepoint_markers_by_strid
;
11492 remote_ops
.to_traceframe_info
= remote_traceframe_info
;
11493 remote_ops
.to_use_agent
= remote_use_agent
;
11494 remote_ops
.to_can_use_agent
= remote_can_use_agent
;
11495 remote_ops
.to_supports_btrace
= remote_supports_btrace
;
11496 remote_ops
.to_enable_btrace
= remote_enable_btrace
;
11497 remote_ops
.to_disable_btrace
= remote_disable_btrace
;
11498 remote_ops
.to_teardown_btrace
= remote_teardown_btrace
;
11499 remote_ops
.to_read_btrace
= remote_read_btrace
;
11500 remote_ops
.to_augmented_libraries_svr4_read
=
11501 remote_augmented_libraries_svr4_read
;
11504 /* Set up the extended remote vector by making a copy of the standard
11505 remote vector and adding to it. */
11508 init_extended_remote_ops (void)
11510 extended_remote_ops
= remote_ops
;
11512 extended_remote_ops
.to_shortname
= "extended-remote";
11513 extended_remote_ops
.to_longname
=
11514 "Extended remote serial target in gdb-specific protocol";
11515 extended_remote_ops
.to_doc
=
11516 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
11517 Specify the serial device it is connected to (e.g. /dev/ttya).";
11518 extended_remote_ops
.to_open
= extended_remote_open
;
11519 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
11520 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
11521 extended_remote_ops
.to_detach
= extended_remote_detach
;
11522 extended_remote_ops
.to_attach
= extended_remote_attach
;
11523 extended_remote_ops
.to_kill
= extended_remote_kill
;
11524 extended_remote_ops
.to_supports_disable_randomization
11525 = extended_remote_supports_disable_randomization
;
11529 remote_can_async_p (struct target_ops
*ops
)
11531 struct remote_state
*rs
= get_remote_state ();
11533 if (!target_async_permitted
)
11534 /* We only enable async when the user specifically asks for it. */
11537 /* We're async whenever the serial device is. */
11538 return serial_can_async_p (rs
->remote_desc
);
11542 remote_is_async_p (struct target_ops
*ops
)
11544 struct remote_state
*rs
= get_remote_state ();
11546 if (!target_async_permitted
)
11547 /* We only enable async when the user specifically asks for it. */
11550 /* We're async whenever the serial device is. */
11551 return serial_is_async_p (rs
->remote_desc
);
11554 /* Pass the SERIAL event on and up to the client. One day this code
11555 will be able to delay notifying the client of an event until the
11556 point where an entire packet has been received. */
11558 static serial_event_ftype remote_async_serial_handler
;
11561 remote_async_serial_handler (struct serial
*scb
, void *context
)
11563 struct remote_state
*rs
= context
;
11565 /* Don't propogate error information up to the client. Instead let
11566 the client find out about the error by querying the target. */
11567 rs
->async_client_callback (INF_REG_EVENT
, rs
->async_client_context
);
11571 remote_async_inferior_event_handler (gdb_client_data data
)
11573 inferior_event_handler (INF_REG_EVENT
, NULL
);
11577 remote_async (struct target_ops
*ops
,
11578 void (*callback
) (enum inferior_event_type event_type
,
11582 struct remote_state
*rs
= get_remote_state ();
11584 if (callback
!= NULL
)
11586 serial_async (rs
->remote_desc
, remote_async_serial_handler
, rs
);
11587 rs
->async_client_callback
= callback
;
11588 rs
->async_client_context
= context
;
11591 serial_async (rs
->remote_desc
, NULL
, NULL
);
11595 set_remote_cmd (char *args
, int from_tty
)
11597 help_list (remote_set_cmdlist
, "set remote ", -1, gdb_stdout
);
11601 show_remote_cmd (char *args
, int from_tty
)
11603 /* We can't just use cmd_show_list here, because we want to skip
11604 the redundant "show remote Z-packet" and the legacy aliases. */
11605 struct cleanup
*showlist_chain
;
11606 struct cmd_list_element
*list
= remote_show_cmdlist
;
11607 struct ui_out
*uiout
= current_uiout
;
11609 showlist_chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, "showlist");
11610 for (; list
!= NULL
; list
= list
->next
)
11611 if (strcmp (list
->name
, "Z-packet") == 0)
11613 else if (list
->type
== not_set_cmd
)
11614 /* Alias commands are exactly like the original, except they
11615 don't have the normal type. */
11619 struct cleanup
*option_chain
11620 = make_cleanup_ui_out_tuple_begin_end (uiout
, "option");
11622 ui_out_field_string (uiout
, "name", list
->name
);
11623 ui_out_text (uiout
, ": ");
11624 if (list
->type
== show_cmd
)
11625 do_show_command ((char *) NULL
, from_tty
, list
);
11627 cmd_func (list
, NULL
, from_tty
);
11628 /* Close the tuple. */
11629 do_cleanups (option_chain
);
11632 /* Close the tuple. */
11633 do_cleanups (showlist_chain
);
11637 /* Function to be called whenever a new objfile (shlib) is detected. */
11639 remote_new_objfile (struct objfile
*objfile
)
11641 struct remote_state
*rs
= get_remote_state ();
11643 if (rs
->remote_desc
!= 0) /* Have a remote connection. */
11644 remote_check_symbols ();
11647 /* Pull all the tracepoints defined on the target and create local
11648 data structures representing them. We don't want to create real
11649 tracepoints yet, we don't want to mess up the user's existing
11653 remote_upload_tracepoints (struct uploaded_tp
**utpp
)
11655 struct remote_state
*rs
= get_remote_state ();
11658 /* Ask for a first packet of tracepoint definition. */
11660 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11662 while (*p
&& *p
!= 'l')
11664 parse_tracepoint_definition (p
, utpp
);
11665 /* Ask for another packet of tracepoint definition. */
11667 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11674 remote_upload_trace_state_variables (struct uploaded_tsv
**utsvp
)
11676 struct remote_state
*rs
= get_remote_state ();
11679 /* Ask for a first packet of variable definition. */
11681 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11683 while (*p
&& *p
!= 'l')
11685 parse_tsv_definition (p
, utsvp
);
11686 /* Ask for another packet of variable definition. */
11688 getpkt (&rs
->buf
, &rs
->buf_size
, 0);
11694 /* The "set/show range-stepping" show hook. */
11697 show_range_stepping (struct ui_file
*file
, int from_tty
,
11698 struct cmd_list_element
*c
,
11701 fprintf_filtered (file
,
11702 _("Debugger's willingness to use range stepping "
11703 "is %s.\n"), value
);
11706 /* The "set/show range-stepping" set hook. */
11709 set_range_stepping (char *ignore_args
, int from_tty
,
11710 struct cmd_list_element
*c
)
11712 struct remote_state
*rs
= get_remote_state ();
11714 /* Whene enabling, check whether range stepping is actually
11715 supported by the target, and warn if not. */
11716 if (use_range_stepping
)
11718 if (rs
->remote_desc
!= NULL
)
11720 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_SUPPORT_UNKNOWN
)
11721 remote_vcont_probe (rs
);
11723 if (remote_protocol_packets
[PACKET_vCont
].support
== PACKET_ENABLE
11724 && rs
->supports_vCont
.r
)
11728 warning (_("Range stepping is not supported by the current target"));
11733 _initialize_remote (void)
11735 struct remote_state
*rs
;
11736 struct cmd_list_element
*cmd
;
11737 const char *cmd_name
;
11739 /* architecture specific data */
11740 remote_gdbarch_data_handle
=
11741 gdbarch_data_register_post_init (init_remote_state
);
11742 remote_g_packet_data_handle
=
11743 gdbarch_data_register_pre_init (remote_g_packet_data_init
);
11745 /* Initialize the per-target state. At the moment there is only one
11746 of these, not one per target. Only one target is active at a
11748 remote_state
= new_remote_state ();
11750 init_remote_ops ();
11751 add_target (&remote_ops
);
11753 init_extended_remote_ops ();
11754 add_target (&extended_remote_ops
);
11756 /* Hook into new objfile notification. */
11757 observer_attach_new_objfile (remote_new_objfile
);
11758 /* We're no longer interested in notification events of an inferior
11760 observer_attach_inferior_exit (discard_pending_stop_replies
);
11762 /* Set up signal handlers. */
11763 async_sigint_remote_token
=
11764 create_async_signal_handler (async_remote_interrupt
, NULL
);
11765 async_sigint_remote_twice_token
=
11766 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
11769 init_remote_threadtests ();
11772 stop_reply_queue
= QUEUE_alloc (stop_reply_p
, stop_reply_xfree
);
11773 /* set/show remote ... */
11775 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, _("\
11776 Remote protocol specific variables\n\
11777 Configure various remote-protocol specific variables such as\n\
11778 the packets being used"),
11779 &remote_set_cmdlist
, "set remote ",
11780 0 /* allow-unknown */, &setlist
);
11781 add_prefix_cmd ("remote", class_maintenance
, show_remote_cmd
, _("\
11782 Remote protocol specific variables\n\
11783 Configure various remote-protocol specific variables such as\n\
11784 the packets being used"),
11785 &remote_show_cmdlist
, "show remote ",
11786 0 /* allow-unknown */, &showlist
);
11788 add_cmd ("compare-sections", class_obscure
, compare_sections_command
, _("\
11789 Compare section data on target to the exec file.\n\
11790 Argument is a single section name (default: all loaded sections)."),
11793 add_cmd ("packet", class_maintenance
, packet_command
, _("\
11794 Send an arbitrary packet to a remote target.\n\
11795 maintenance packet TEXT\n\
11796 If GDB is talking to an inferior via the GDB serial protocol, then\n\
11797 this command sends the string TEXT to the inferior, and displays the\n\
11798 response packet. GDB supplies the initial `$' character, and the\n\
11799 terminating `#' character and checksum."),
11802 add_setshow_boolean_cmd ("remotebreak", no_class
, &remote_break
, _("\
11803 Set whether to send break if interrupted."), _("\
11804 Show whether to send break if interrupted."), _("\
11805 If set, a break, instead of a cntrl-c, is sent to the remote target."),
11806 set_remotebreak
, show_remotebreak
,
11807 &setlist
, &showlist
);
11808 cmd_name
= "remotebreak";
11809 cmd
= lookup_cmd (&cmd_name
, setlist
, "", -1, 1);
11810 deprecate_cmd (cmd
, "set remote interrupt-sequence");
11811 cmd_name
= "remotebreak"; /* needed because lookup_cmd updates the pointer */
11812 cmd
= lookup_cmd (&cmd_name
, showlist
, "", -1, 1);
11813 deprecate_cmd (cmd
, "show remote interrupt-sequence");
11815 add_setshow_enum_cmd ("interrupt-sequence", class_support
,
11816 interrupt_sequence_modes
, &interrupt_sequence_mode
,
11818 Set interrupt sequence to remote target."), _("\
11819 Show interrupt sequence to remote target."), _("\
11820 Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
11821 NULL
, show_interrupt_sequence
,
11822 &remote_set_cmdlist
,
11823 &remote_show_cmdlist
);
11825 add_setshow_boolean_cmd ("interrupt-on-connect", class_support
,
11826 &interrupt_on_connect
, _("\
11827 Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11828 Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11829 If set, interrupt sequence is sent to remote target."),
11831 &remote_set_cmdlist
, &remote_show_cmdlist
);
11833 /* Install commands for configuring memory read/write packets. */
11835 add_cmd ("remotewritesize", no_class
, set_memory_write_packet_size
, _("\
11836 Set the maximum number of bytes per memory write packet (deprecated)."),
11838 add_cmd ("remotewritesize", no_class
, show_memory_write_packet_size
, _("\
11839 Show the maximum number of bytes per memory write packet (deprecated)."),
11841 add_cmd ("memory-write-packet-size", no_class
,
11842 set_memory_write_packet_size
, _("\
11843 Set the maximum number of bytes per memory-write packet.\n\
11844 Specify the number of bytes in a packet or 0 (zero) for the\n\
11845 default packet size. The actual limit is further reduced\n\
11846 dependent on the target. Specify ``fixed'' to disable the\n\
11847 further restriction and ``limit'' to enable that restriction."),
11848 &remote_set_cmdlist
);
11849 add_cmd ("memory-read-packet-size", no_class
,
11850 set_memory_read_packet_size
, _("\
11851 Set the maximum number of bytes per memory-read packet.\n\
11852 Specify the number of bytes in a packet or 0 (zero) for the\n\
11853 default packet size. The actual limit is further reduced\n\
11854 dependent on the target. Specify ``fixed'' to disable the\n\
11855 further restriction and ``limit'' to enable that restriction."),
11856 &remote_set_cmdlist
);
11857 add_cmd ("memory-write-packet-size", no_class
,
11858 show_memory_write_packet_size
,
11859 _("Show the maximum number of bytes per memory-write packet."),
11860 &remote_show_cmdlist
);
11861 add_cmd ("memory-read-packet-size", no_class
,
11862 show_memory_read_packet_size
,
11863 _("Show the maximum number of bytes per memory-read packet."),
11864 &remote_show_cmdlist
);
11866 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class
,
11867 &remote_hw_watchpoint_limit
, _("\
11868 Set the maximum number of target hardware watchpoints."), _("\
11869 Show the maximum number of target hardware watchpoints."), _("\
11870 Specify a negative limit for unlimited."),
11871 NULL
, NULL
, /* FIXME: i18n: The maximum
11872 number of target hardware
11873 watchpoints is %s. */
11874 &remote_set_cmdlist
, &remote_show_cmdlist
);
11875 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class
,
11876 &remote_hw_watchpoint_length_limit
, _("\
11877 Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
11878 Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
11879 Specify a negative limit for unlimited."),
11880 NULL
, NULL
, /* FIXME: i18n: The maximum
11881 length (in bytes) of a target
11882 hardware watchpoint is %s. */
11883 &remote_set_cmdlist
, &remote_show_cmdlist
);
11884 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class
,
11885 &remote_hw_breakpoint_limit
, _("\
11886 Set the maximum number of target hardware breakpoints."), _("\
11887 Show the maximum number of target hardware breakpoints."), _("\
11888 Specify a negative limit for unlimited."),
11889 NULL
, NULL
, /* FIXME: i18n: The maximum
11890 number of target hardware
11891 breakpoints is %s. */
11892 &remote_set_cmdlist
, &remote_show_cmdlist
);
11894 add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure
,
11895 &remote_address_size
, _("\
11896 Set the maximum size of the address (in bits) in a memory packet."), _("\
11897 Show the maximum size of the address (in bits) in a memory packet."), NULL
,
11899 NULL
, /* FIXME: i18n: */
11900 &setlist
, &showlist
);
11902 add_packet_config_cmd (&remote_protocol_packets
[PACKET_X
],
11903 "X", "binary-download", 1);
11905 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vCont
],
11906 "vCont", "verbose-resume", 0);
11908 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QPassSignals
],
11909 "QPassSignals", "pass-signals", 0);
11911 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QProgramSignals
],
11912 "QProgramSignals", "program-signals", 0);
11914 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSymbol
],
11915 "qSymbol", "symbol-lookup", 0);
11917 add_packet_config_cmd (&remote_protocol_packets
[PACKET_P
],
11918 "P", "set-register", 1);
11920 add_packet_config_cmd (&remote_protocol_packets
[PACKET_p
],
11921 "p", "fetch-register", 1);
11923 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z0
],
11924 "Z0", "software-breakpoint", 0);
11926 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z1
],
11927 "Z1", "hardware-breakpoint", 0);
11929 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z2
],
11930 "Z2", "write-watchpoint", 0);
11932 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z3
],
11933 "Z3", "read-watchpoint", 0);
11935 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Z4
],
11936 "Z4", "access-watchpoint", 0);
11938 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_auxv
],
11939 "qXfer:auxv:read", "read-aux-vector", 0);
11941 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_features
],
11942 "qXfer:features:read", "target-features", 0);
11944 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries
],
11945 "qXfer:libraries:read", "library-info", 0);
11947 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_libraries_svr4
],
11948 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
11950 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_memory_map
],
11951 "qXfer:memory-map:read", "memory-map", 0);
11953 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_read
],
11954 "qXfer:spu:read", "read-spu-object", 0);
11956 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_spu_write
],
11957 "qXfer:spu:write", "write-spu-object", 0);
11959 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_osdata
],
11960 "qXfer:osdata:read", "osdata", 0);
11962 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_threads
],
11963 "qXfer:threads:read", "threads", 0);
11965 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_read
],
11966 "qXfer:siginfo:read", "read-siginfo-object", 0);
11968 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_siginfo_write
],
11969 "qXfer:siginfo:write", "write-siginfo-object", 0);
11971 add_packet_config_cmd
11972 (&remote_protocol_packets
[PACKET_qXfer_traceframe_info
],
11973 "qXfer:traceframe-info:read", "traceframe-info", 0);
11975 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_uib
],
11976 "qXfer:uib:read", "unwind-info-block", 0);
11978 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTLSAddr
],
11979 "qGetTLSAddr", "get-thread-local-storage-address",
11982 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qGetTIBAddr
],
11983 "qGetTIBAddr", "get-thread-information-block-address",
11986 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bc
],
11987 "bc", "reverse-continue", 0);
11989 add_packet_config_cmd (&remote_protocol_packets
[PACKET_bs
],
11990 "bs", "reverse-step", 0);
11992 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSupported
],
11993 "qSupported", "supported-packets", 0);
11995 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qSearch_memory
],
11996 "qSearch:memory", "search-memory", 0);
11998 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qTStatus
],
11999 "qTStatus", "trace-status", 0);
12001 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_open
],
12002 "vFile:open", "hostio-open", 0);
12004 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pread
],
12005 "vFile:pread", "hostio-pread", 0);
12007 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_pwrite
],
12008 "vFile:pwrite", "hostio-pwrite", 0);
12010 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_close
],
12011 "vFile:close", "hostio-close", 0);
12013 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_unlink
],
12014 "vFile:unlink", "hostio-unlink", 0);
12016 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vFile_readlink
],
12017 "vFile:readlink", "hostio-readlink", 0);
12019 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vAttach
],
12020 "vAttach", "attach", 0);
12022 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vRun
],
12025 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QStartNoAckMode
],
12026 "QStartNoAckMode", "noack", 0);
12028 add_packet_config_cmd (&remote_protocol_packets
[PACKET_vKill
],
12029 "vKill", "kill", 0);
12031 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qAttached
],
12032 "qAttached", "query-attached", 0);
12034 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalTracepoints
],
12035 "ConditionalTracepoints",
12036 "conditional-tracepoints", 0);
12038 add_packet_config_cmd (&remote_protocol_packets
[PACKET_ConditionalBreakpoints
],
12039 "ConditionalBreakpoints",
12040 "conditional-breakpoints", 0);
12042 add_packet_config_cmd (&remote_protocol_packets
[PACKET_BreakpointCommands
],
12043 "BreakpointCommands",
12044 "breakpoint-commands", 0);
12046 add_packet_config_cmd (&remote_protocol_packets
[PACKET_FastTracepoints
],
12047 "FastTracepoints", "fast-tracepoints", 0);
12049 add_packet_config_cmd (&remote_protocol_packets
[PACKET_TracepointSource
],
12050 "TracepointSource", "TracepointSource", 0);
12052 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAllow
],
12053 "QAllow", "allow", 0);
12055 add_packet_config_cmd (&remote_protocol_packets
[PACKET_StaticTracepoints
],
12056 "StaticTracepoints", "static-tracepoints", 0);
12058 add_packet_config_cmd (&remote_protocol_packets
[PACKET_InstallInTrace
],
12059 "InstallInTrace", "install-in-trace", 0);
12061 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_statictrace_read
],
12062 "qXfer:statictrace:read", "read-sdata-object", 0);
12064 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_fdpic
],
12065 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
12067 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QDisableRandomization
],
12068 "QDisableRandomization", "disable-randomization", 0);
12070 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QAgent
],
12071 "QAgent", "agent", 0);
12073 add_packet_config_cmd (&remote_protocol_packets
[PACKET_QTBuffer_size
],
12074 "QTBuffer:size", "trace-buffer-size", 0);
12076 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_off
],
12077 "Qbtrace:off", "disable-btrace", 0);
12079 add_packet_config_cmd (&remote_protocol_packets
[PACKET_Qbtrace_bts
],
12080 "Qbtrace:bts", "enable-btrace", 0);
12082 add_packet_config_cmd (&remote_protocol_packets
[PACKET_qXfer_btrace
],
12083 "qXfer:btrace", "read-btrace", 0);
12085 /* Keep the old ``set remote Z-packet ...'' working. Each individual
12086 Z sub-packet has its own set and show commands, but users may
12087 have sets to this variable in their .gdbinit files (or in their
12089 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure
,
12090 &remote_Z_packet_detect
, _("\
12091 Set use of remote protocol `Z' packets"), _("\
12092 Show use of remote protocol `Z' packets "), _("\
12093 When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
12095 set_remote_protocol_Z_packet_cmd
,
12096 show_remote_protocol_Z_packet_cmd
,
12097 /* FIXME: i18n: Use of remote protocol
12098 `Z' packets is %s. */
12099 &remote_set_cmdlist
, &remote_show_cmdlist
);
12101 add_prefix_cmd ("remote", class_files
, remote_command
, _("\
12102 Manipulate files on the remote system\n\
12103 Transfer files to and from the remote target system."),
12104 &remote_cmdlist
, "remote ",
12105 0 /* allow-unknown */, &cmdlist
);
12107 add_cmd ("put", class_files
, remote_put_command
,
12108 _("Copy a local file to the remote system."),
12111 add_cmd ("get", class_files
, remote_get_command
,
12112 _("Copy a remote file to the local system."),
12115 add_cmd ("delete", class_files
, remote_delete_command
,
12116 _("Delete a remote file."),
12119 remote_exec_file
= xstrdup ("");
12120 add_setshow_string_noescape_cmd ("exec-file", class_files
,
12121 &remote_exec_file
, _("\
12122 Set the remote pathname for \"run\""), _("\
12123 Show the remote pathname for \"run\""), NULL
, NULL
, NULL
,
12124 &remote_set_cmdlist
, &remote_show_cmdlist
);
12126 add_setshow_boolean_cmd ("range-stepping", class_run
,
12127 &use_range_stepping
, _("\
12128 Enable or disable range stepping."), _("\
12129 Show whether target-assisted range stepping is enabled."), _("\
12130 If on, and the target supports it, when stepping a source line, GDB\n\
12131 tells the target to step the corresponding range of addresses itself instead\n\
12132 of issuing multiple single-steps. This speeds up source level\n\
12133 stepping. If off, GDB always issues single-steps, even if range\n\
12134 stepping is supported by the target. The default is on."),
12135 set_range_stepping
,
12136 show_range_stepping
,
12140 /* Eventually initialize fileio. See fileio.c */
12141 initialize_remote_fileio (remote_set_cmdlist
, remote_show_cmdlist
);
12143 /* Take advantage of the fact that the LWP field is not used, to tag
12144 special ptids with it set to != 0. */
12145 magic_null_ptid
= ptid_build (42000, 1, -1);
12146 not_sent_ptid
= ptid_build (42000, 1, -2);
12147 any_thread_ptid
= ptid_build (42000, 1, 0);
12149 target_buf_size
= 2048;
12150 target_buf
= xmalloc (target_buf_size
);