| /* Remote target communications for serial-line targets in custom GDB protocol |
| |
| Copyright (C) 1988-2016 Free Software Foundation, Inc. |
| |
| This file is part of GDB. |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation; either version 3 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| |
| /* See the GDB User Guide for details of the GDB remote protocol. */ |
| |
| #include "defs.h" |
| #include <ctype.h> |
| #include <fcntl.h> |
| #include "inferior.h" |
| #include "infrun.h" |
| #include "bfd.h" |
| #include "symfile.h" |
| #include "target.h" |
| /*#include "terminal.h" */ |
| #include "gdbcmd.h" |
| #include "objfiles.h" |
| #include "gdb-stabs.h" |
| #include "gdbthread.h" |
| #include "remote.h" |
| #include "remote-notif.h" |
| #include "regcache.h" |
| #include "value.h" |
| #include "observer.h" |
| #include "solib.h" |
| #include "cli/cli-decode.h" |
| #include "cli/cli-setshow.h" |
| #include "target-descriptions.h" |
| #include "gdb_bfd.h" |
| #include "filestuff.h" |
| #include "rsp-low.h" |
| #include "disasm.h" |
| #include "location.h" |
| |
| #include "gdb_sys_time.h" |
| |
| #include "event-loop.h" |
| #include "event-top.h" |
| #include "inf-loop.h" |
| |
| #include <signal.h> |
| #include "serial.h" |
| |
| #include "gdbcore.h" /* for exec_bfd */ |
| |
| #include "remote-fileio.h" |
| #include "gdb/fileio.h" |
| #include <sys/stat.h> |
| #include "xml-support.h" |
| |
| #include "memory-map.h" |
| |
| #include "tracepoint.h" |
| #include "ax.h" |
| #include "ax-gdb.h" |
| #include "agent.h" |
| #include "btrace.h" |
| |
| /* Temp hacks for tracepoint encoding migration. */ |
| static char *target_buf; |
| static long target_buf_size; |
| |
| /* Per-program-space data key. */ |
| static const struct program_space_data *remote_pspace_data; |
| |
| /* The variable registered as the control variable used by the |
| remote exec-file commands. While the remote exec-file setting is |
| per-program-space, the set/show machinery uses this as the |
| location of the remote exec-file value. */ |
| static char *remote_exec_file_var; |
| |
| /* The size to align memory write packets, when practical. The protocol |
| does not guarantee any alignment, and gdb will generate short |
| writes and unaligned writes, but even as a best-effort attempt this |
| can improve bulk transfers. For instance, if a write is misaligned |
| relative to the target's data bus, the stub may need to make an extra |
| round trip fetching data from the target. This doesn't make a |
| huge difference, but it's easy to do, so we try to be helpful. |
| |
| The alignment chosen is arbitrary; usually data bus width is |
| important here, not the possibly larger cache line size. */ |
| enum { REMOTE_ALIGN_WRITES = 16 }; |
| |
| /* Prototypes for local functions. */ |
| static int getpkt_sane (char **buf, long *sizeof_buf, int forever); |
| static int getpkt_or_notif_sane (char **buf, long *sizeof_buf, |
| int forever, int *is_notif); |
| |
| static void remote_files_info (struct target_ops *ignore); |
| |
| static void remote_prepare_to_store (struct target_ops *self, |
| struct regcache *regcache); |
| |
| static void remote_open_1 (const char *, int, struct target_ops *, |
| int extended_p); |
| |
| static void remote_close (struct target_ops *self); |
| |
| struct remote_state; |
| |
| static int remote_vkill (int pid, struct remote_state *rs); |
| |
| static void remote_kill_k (void); |
| |
| static void remote_mourn (struct target_ops *ops); |
| |
| static void extended_remote_restart (void); |
| |
| static void remote_send (char **buf, long *sizeof_buf_p); |
| |
| static int readchar (int timeout); |
| |
| static void remote_serial_write (const char *str, int len); |
| |
| static void remote_kill (struct target_ops *ops); |
| |
| static int remote_can_async_p (struct target_ops *); |
| |
| static int remote_is_async_p (struct target_ops *); |
| |
| static void remote_async (struct target_ops *ops, int enable); |
| |
| static void remote_thread_events (struct target_ops *ops, int enable); |
| |
| static void interrupt_query (void); |
| |
| static void set_general_thread (struct ptid ptid); |
| static void set_continue_thread (struct ptid ptid); |
| |
| static void get_offsets (void); |
| |
| static void skip_frame (void); |
| |
| static long read_frame (char **buf_p, long *sizeof_buf); |
| |
| static int hexnumlen (ULONGEST num); |
| |
| static void init_remote_ops (void); |
| |
| static void init_extended_remote_ops (void); |
| |
| static void remote_stop (struct target_ops *self, ptid_t); |
| |
| static int stubhex (int ch); |
| |
| static int hexnumstr (char *, ULONGEST); |
| |
| static int hexnumnstr (char *, ULONGEST, int); |
| |
| static CORE_ADDR remote_address_masked (CORE_ADDR); |
| |
| static void print_packet (const char *); |
| |
| static void compare_sections_command (char *, int); |
| |
| static void packet_command (char *, int); |
| |
| static int stub_unpack_int (char *buff, int fieldlength); |
| |
| static ptid_t remote_current_thread (ptid_t oldptid); |
| |
| static int putpkt_binary (const char *buf, int cnt); |
| |
| static void check_binary_download (CORE_ADDR addr); |
| |
| struct packet_config; |
| |
| static void show_packet_config_cmd (struct packet_config *config); |
| |
| static void show_remote_protocol_packet_cmd (struct ui_file *file, |
| int from_tty, |
| struct cmd_list_element *c, |
| const char *value); |
| |
| static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid); |
| static ptid_t read_ptid (char *buf, char **obuf); |
| |
| static void remote_set_permissions (struct target_ops *self); |
| |
| static int remote_get_trace_status (struct target_ops *self, |
| struct trace_status *ts); |
| |
| static int remote_upload_tracepoints (struct target_ops *self, |
| struct uploaded_tp **utpp); |
| |
| static int remote_upload_trace_state_variables (struct target_ops *self, |
| struct uploaded_tsv **utsvp); |
| |
| static void remote_query_supported (void); |
| |
| static void remote_check_symbols (void); |
| |
| void _initialize_remote (void); |
| |
| struct stop_reply; |
| static void stop_reply_xfree (struct stop_reply *); |
| static void remote_parse_stop_reply (char *, struct stop_reply *); |
| static void push_stop_reply (struct stop_reply *); |
| static void discard_pending_stop_replies_in_queue (struct remote_state *); |
| static int peek_stop_reply (ptid_t ptid); |
| |
| struct threads_listing_context; |
| static void remove_new_fork_children (struct threads_listing_context *); |
| |
| static void remote_async_inferior_event_handler (gdb_client_data); |
| |
| static void remote_terminal_ours (struct target_ops *self); |
| |
| static int remote_read_description_p (struct target_ops *target); |
| |
| static void remote_console_output (char *msg); |
| |
| static int remote_supports_cond_breakpoints (struct target_ops *self); |
| |
| static int remote_can_run_breakpoint_commands (struct target_ops *self); |
| |
| static void remote_btrace_reset (void); |
| |
| static int stop_reply_queue_length (void); |
| |
| static void readahead_cache_invalidate (void); |
| |
| static void remote_unpush_and_throw (void); |
| |
| /* For "remote". */ |
| |
| static struct cmd_list_element *remote_cmdlist; |
| |
| /* For "set remote" and "show remote". */ |
| |
| static struct cmd_list_element *remote_set_cmdlist; |
| static struct cmd_list_element *remote_show_cmdlist; |
| |
| /* Stub vCont actions support. |
| |
| Each field is a boolean flag indicating whether the stub reports |
| support for the corresponding action. */ |
| |
| struct vCont_action_support |
| { |
| /* vCont;t */ |
| int t; |
| |
| /* vCont;r */ |
| int r; |
| |
| /* vCont;s */ |
| int s; |
| |
| /* vCont;S */ |
| int S; |
| }; |
| |
| /* Controls whether GDB is willing to use range stepping. */ |
| |
| static int use_range_stepping = 1; |
| |
| #define OPAQUETHREADBYTES 8 |
| |
| /* a 64 bit opaque identifier */ |
| typedef unsigned char threadref[OPAQUETHREADBYTES]; |
| |
| /* About this many threadisds fit in a packet. */ |
| |
| #define MAXTHREADLISTRESULTS 32 |
| |
| /* Data for the vFile:pread readahead cache. */ |
| |
| struct readahead_cache |
| { |
| /* The file descriptor for the file that is being cached. -1 if the |
| cache is invalid. */ |
| int fd; |
| |
| /* The offset into the file that the cache buffer corresponds |
| to. */ |
| ULONGEST offset; |
| |
| /* The buffer holding the cache contents. */ |
| gdb_byte *buf; |
| /* The buffer's size. We try to read as much as fits into a packet |
| at a time. */ |
| size_t bufsize; |
| |
| /* Cache hit and miss counters. */ |
| ULONGEST hit_count; |
| ULONGEST miss_count; |
| }; |
| |
| /* Description of the remote protocol state for the currently |
| connected target. This is per-target state, and independent of the |
| selected architecture. */ |
| |
| struct remote_state |
| { |
| /* A buffer to use for incoming packets, and its current size. The |
| buffer is grown dynamically for larger incoming packets. |
| Outgoing packets may also be constructed in this buffer. |
| BUF_SIZE is always at least REMOTE_PACKET_SIZE; |
| REMOTE_PACKET_SIZE should be used to limit the length of outgoing |
| packets. */ |
| char *buf; |
| long buf_size; |
| |
| /* True if we're going through initial connection setup (finding out |
| about the remote side's threads, relocating symbols, etc.). */ |
| int starting_up; |
| |
| /* If we negotiated packet size explicitly (and thus can bypass |
| heuristics for the largest packet size that will not overflow |
| a buffer in the stub), this will be set to that packet size. |
| Otherwise zero, meaning to use the guessed size. */ |
| long explicit_packet_size; |
| |
| /* remote_wait is normally called when the target is running and |
| waits for a stop reply packet. But sometimes we need to call it |
| when the target is already stopped. We can send a "?" packet |
| and have remote_wait read the response. Or, if we already have |
| the response, we can stash it in BUF and tell remote_wait to |
| skip calling getpkt. This flag is set when BUF contains a |
| stop reply packet and the target is not waiting. */ |
| int cached_wait_status; |
| |
| /* True, if in no ack mode. That is, neither GDB nor the stub will |
| expect acks from each other. The connection is assumed to be |
| reliable. */ |
| int noack_mode; |
| |
| /* True if we're connected in extended remote mode. */ |
| int extended; |
| |
| /* True if we resumed the target and we're waiting for the target to |
| stop. In the mean time, we can't start another command/query. |
| The remote server wouldn't be ready to process it, so we'd |
| timeout waiting for a reply that would never come and eventually |
| we'd close the connection. This can happen in asynchronous mode |
| because we allow GDB commands while the target is running. */ |
| int waiting_for_stop_reply; |
| |
| /* The status of the stub support for the various vCont actions. */ |
| struct vCont_action_support supports_vCont; |
| |
| /* Nonzero if the user has pressed Ctrl-C, but the target hasn't |
| responded to that. */ |
| int ctrlc_pending_p; |
| |
| /* True if we saw a Ctrl-C while reading or writing from/to the |
| remote descriptor. At that point it is not safe to send a remote |
| interrupt packet, so we instead remember we saw the Ctrl-C and |
| process it once we're done with sending/receiving the current |
| packet, which should be shortly. If however that takes too long, |
| and the user presses Ctrl-C again, we offer to disconnect. */ |
| int got_ctrlc_during_io; |
| |
| /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
| remote_open knows that we don't have a file open when the program |
| starts. */ |
| struct serial *remote_desc; |
| |
| /* These are the threads which we last sent to the remote system. The |
| TID member will be -1 for all or -2 for not sent yet. */ |
| ptid_t general_thread; |
| ptid_t continue_thread; |
| |
| /* This is the traceframe which we last selected on the remote system. |
| It will be -1 if no traceframe is selected. */ |
| int remote_traceframe_number; |
| |
| char *last_pass_packet; |
| |
| /* The last QProgramSignals packet sent to the target. We bypass |
| sending a new program signals list down to the target if the new |
| packet is exactly the same as the last we sent. IOW, we only let |
| the target know about program signals list changes. */ |
| char *last_program_signals_packet; |
| |
| enum gdb_signal last_sent_signal; |
| |
| int last_sent_step; |
| |
| /* The execution direction of the last resume we got. */ |
| enum exec_direction_kind last_resume_exec_dir; |
| |
| char *finished_object; |
| char *finished_annex; |
| ULONGEST finished_offset; |
| |
| /* Should we try the 'ThreadInfo' query packet? |
| |
| This variable (NOT available to the user: auto-detect only!) |
| determines whether GDB will use the new, simpler "ThreadInfo" |
| query or the older, more complex syntax for thread queries. |
| This is an auto-detect variable (set to true at each connect, |
| and set to false when the target fails to recognize it). */ |
| int use_threadinfo_query; |
| int use_threadextra_query; |
| |
| threadref echo_nextthread; |
| threadref nextthread; |
| threadref resultthreadlist[MAXTHREADLISTRESULTS]; |
| |
| /* The state of remote notification. */ |
| struct remote_notif_state *notif_state; |
| |
| /* The branch trace configuration. */ |
| struct btrace_config btrace_config; |
| |
| /* The argument to the last "vFile:setfs:" packet we sent, used |
| to avoid sending repeated unnecessary "vFile:setfs:" packets. |
| Initialized to -1 to indicate that no "vFile:setfs:" packet |
| has yet been sent. */ |
| int fs_pid; |
| |
| /* A readahead cache for vFile:pread. Often, reading a binary |
| involves a sequence of small reads. E.g., when parsing an ELF |
| file. A readahead cache helps mostly the case of remote |
| debugging on a connection with higher latency, due to the |
| request/reply nature of the RSP. We only cache data for a single |
| file descriptor at a time. */ |
| struct readahead_cache readahead_cache; |
| }; |
| |
| /* Private data that we'll store in (struct thread_info)->private. */ |
| struct private_thread_info |
| { |
| char *extra; |
| char *name; |
| int core; |
| |
| /* Whether the target stopped for a breakpoint/watchpoint. */ |
| enum target_stop_reason stop_reason; |
| |
| /* This is set to the data address of the access causing the target |
| to stop for a watchpoint. */ |
| CORE_ADDR watch_data_address; |
| }; |
| |
| static void |
| free_private_thread_info (struct private_thread_info *info) |
| { |
| xfree (info->extra); |
| xfree (info->name); |
| xfree (info); |
| } |
| |
| /* This data could be associated with a target, but we do not always |
| have access to the current target when we need it, so for now it is |
| static. This will be fine for as long as only one target is in use |
| at a time. */ |
| static struct remote_state *remote_state; |
| |
| static struct remote_state * |
| get_remote_state_raw (void) |
| { |
| return remote_state; |
| } |
| |
| /* Allocate a new struct remote_state with xmalloc, initialize it, and |
| return it. */ |
| |
| static struct remote_state * |
| new_remote_state (void) |
| { |
| struct remote_state *result = XCNEW (struct remote_state); |
| |
| /* The default buffer size is unimportant; it will be expanded |
| whenever a larger buffer is needed. */ |
| result->buf_size = 400; |
| result->buf = (char *) xmalloc (result->buf_size); |
| result->remote_traceframe_number = -1; |
| result->last_sent_signal = GDB_SIGNAL_0; |
| result->last_resume_exec_dir = EXEC_FORWARD; |
| result->fs_pid = -1; |
| |
| return result; |
| } |
| |
| /* Description of the remote protocol for a given architecture. */ |
| |
| struct packet_reg |
| { |
| long offset; /* Offset into G packet. */ |
| long regnum; /* GDB's internal register number. */ |
| LONGEST pnum; /* Remote protocol register number. */ |
| int in_g_packet; /* Always part of G packet. */ |
| /* long size in bytes; == register_size (target_gdbarch (), regnum); |
| at present. */ |
| /* char *name; == gdbarch_register_name (target_gdbarch (), regnum); |
| at present. */ |
| }; |
| |
| struct remote_arch_state |
| { |
| /* Description of the remote protocol registers. */ |
| long sizeof_g_packet; |
| |
| /* Description of the remote protocol registers indexed by REGNUM |
| (making an array gdbarch_num_regs in size). */ |
| struct packet_reg *regs; |
| |
| /* This is the size (in chars) of the first response to the ``g'' |
| packet. It is used as a heuristic when determining the maximum |
| size of memory-read and memory-write packets. A target will |
| typically only reserve a buffer large enough to hold the ``g'' |
| packet. The size does not include packet overhead (headers and |
| trailers). */ |
| long actual_register_packet_size; |
| |
| /* This is the maximum size (in chars) of a non read/write packet. |
| It is also used as a cap on the size of read/write packets. */ |
| long remote_packet_size; |
| }; |
| |
| /* Utility: generate error from an incoming stub packet. */ |
| static void |
| trace_error (char *buf) |
| { |
| if (*buf++ != 'E') |
| return; /* not an error msg */ |
| switch (*buf) |
| { |
| case '1': /* malformed packet error */ |
| if (*++buf == '0') /* general case: */ |
| error (_("remote.c: error in outgoing packet.")); |
| else |
| error (_("remote.c: error in outgoing packet at field #%ld."), |
| strtol (buf, NULL, 16)); |
| default: |
| error (_("Target returns error code '%s'."), buf); |
| } |
| } |
| |
| /* Utility: wait for reply from stub, while accepting "O" packets. */ |
| static char * |
| remote_get_noisy_reply (char **buf_p, |
| long *sizeof_buf) |
| { |
| do /* Loop on reply from remote stub. */ |
| { |
| char *buf; |
| |
| QUIT; /* Allow user to bail out with ^C. */ |
| getpkt (buf_p, sizeof_buf, 0); |
| buf = *buf_p; |
| if (buf[0] == 'E') |
| trace_error (buf); |
| else if (startswith (buf, "qRelocInsn:")) |
| { |
| ULONGEST ul; |
| CORE_ADDR from, to, org_to; |
| char *p, *pp; |
| int adjusted_size = 0; |
| int relocated = 0; |
| |
| p = buf + strlen ("qRelocInsn:"); |
| pp = unpack_varlen_hex (p, &ul); |
| if (*pp != ';') |
| error (_("invalid qRelocInsn packet: %s"), buf); |
| from = ul; |
| |
| p = pp + 1; |
| unpack_varlen_hex (p, &ul); |
| to = ul; |
| |
| org_to = to; |
| |
| TRY |
| { |
| gdbarch_relocate_instruction (target_gdbarch (), &to, from); |
| relocated = 1; |
| } |
| CATCH (ex, RETURN_MASK_ALL) |
| { |
| if (ex.error == MEMORY_ERROR) |
| { |
| /* Propagate memory errors silently back to the |
| target. The stub may have limited the range of |
| addresses we can write to, for example. */ |
| } |
| else |
| { |
| /* Something unexpectedly bad happened. Be verbose |
| so we can tell what, and propagate the error back |
| to the stub, so it doesn't get stuck waiting for |
| a response. */ |
| exception_fprintf (gdb_stderr, ex, |
| _("warning: relocating instruction: ")); |
| } |
| putpkt ("E01"); |
| } |
| END_CATCH |
| |
| if (relocated) |
| { |
| adjusted_size = to - org_to; |
| |
| xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size); |
| putpkt (buf); |
| } |
| } |
| else if (buf[0] == 'O' && buf[1] != 'K') |
| remote_console_output (buf + 1); /* 'O' message from stub */ |
| else |
| return buf; /* Here's the actual reply. */ |
| } |
| while (1); |
| } |
| |
| /* Handle for retreving the remote protocol data from gdbarch. */ |
| static struct gdbarch_data *remote_gdbarch_data_handle; |
| |
| static struct remote_arch_state * |
| get_remote_arch_state (void) |
| { |
| gdb_assert (target_gdbarch () != NULL); |
| return ((struct remote_arch_state *) |
| gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle)); |
| } |
| |
| /* Fetch the global remote target state. */ |
| |
| static struct remote_state * |
| get_remote_state (void) |
| { |
| /* Make sure that the remote architecture state has been |
| initialized, because doing so might reallocate rs->buf. Any |
| function which calls getpkt also needs to be mindful of changes |
| to rs->buf, but this call limits the number of places which run |
| into trouble. */ |
| get_remote_arch_state (); |
| |
| return get_remote_state_raw (); |
| } |
| |
| /* Cleanup routine for the remote module's pspace data. */ |
| |
| static void |
| remote_pspace_data_cleanup (struct program_space *pspace, void *arg) |
| { |
| char *remote_exec_file = (char *) arg; |
| |
| xfree (remote_exec_file); |
| } |
| |
| /* Fetch the remote exec-file from the current program space. */ |
| |
| static const char * |
| get_remote_exec_file (void) |
| { |
| char *remote_exec_file; |
| |
| remote_exec_file |
| = (char *) program_space_data (current_program_space, |
| remote_pspace_data); |
| if (remote_exec_file == NULL) |
| return ""; |
| |
| return remote_exec_file; |
| } |
| |
| /* Set the remote exec file for PSPACE. */ |
| |
| static void |
| set_pspace_remote_exec_file (struct program_space *pspace, |
| char *remote_exec_file) |
| { |
| char *old_file = (char *) program_space_data (pspace, remote_pspace_data); |
| |
| xfree (old_file); |
| set_program_space_data (pspace, remote_pspace_data, |
| xstrdup (remote_exec_file)); |
| } |
| |
| /* The "set/show remote exec-file" set command hook. */ |
| |
| static void |
| set_remote_exec_file (char *ignored, int from_tty, |
| struct cmd_list_element *c) |
| { |
| gdb_assert (remote_exec_file_var != NULL); |
| set_pspace_remote_exec_file (current_program_space, remote_exec_file_var); |
| } |
| |
| /* The "set/show remote exec-file" show command hook. */ |
| |
| static void |
| show_remote_exec_file (struct ui_file *file, int from_tty, |
| struct cmd_list_element *cmd, const char *value) |
| { |
| fprintf_filtered (file, "%s\n", remote_exec_file_var); |
| } |
| |
| static int |
| compare_pnums (const void *lhs_, const void *rhs_) |
| { |
| const struct packet_reg * const *lhs |
| = (const struct packet_reg * const *) lhs_; |
| const struct packet_reg * const *rhs |
| = (const struct packet_reg * const *) rhs_; |
| |
| if ((*lhs)->pnum < (*rhs)->pnum) |
| return -1; |
| else if ((*lhs)->pnum == (*rhs)->pnum) |
| return 0; |
| else |
| return 1; |
| } |
| |
| static int |
| map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs) |
| { |
| int regnum, num_remote_regs, offset; |
| struct packet_reg **remote_regs; |
| |
| for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++) |
| { |
| struct packet_reg *r = ®s[regnum]; |
| |
| if (register_size (gdbarch, regnum) == 0) |
| /* Do not try to fetch zero-sized (placeholder) registers. */ |
| r->pnum = -1; |
| else |
| r->pnum = gdbarch_remote_register_number (gdbarch, regnum); |
| |
| r->regnum = regnum; |
| } |
| |
| /* Define the g/G packet format as the contents of each register |
| with a remote protocol number, in order of ascending protocol |
| number. */ |
| |
| remote_regs = XALLOCAVEC (struct packet_reg *, gdbarch_num_regs (gdbarch)); |
| for (num_remote_regs = 0, regnum = 0; |
| regnum < gdbarch_num_regs (gdbarch); |
| regnum++) |
| if (regs[regnum].pnum != -1) |
| remote_regs[num_remote_regs++] = ®s[regnum]; |
| |
| qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *), |
| compare_pnums); |
| |
| for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++) |
| { |
| remote_regs[regnum]->in_g_packet = 1; |
| remote_regs[regnum]->offset = offset; |
| offset += register_size (gdbarch, remote_regs[regnum]->regnum); |
| } |
| |
| return offset; |
| } |
| |
| /* Given the architecture described by GDBARCH, return the remote |
| protocol register's number and the register's offset in the g/G |
| packets of GDB register REGNUM, in PNUM and POFFSET respectively. |
| If the target does not have a mapping for REGNUM, return false, |
| otherwise, return true. */ |
| |
| int |
| remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum, |
| int *pnum, int *poffset) |
| { |
| struct packet_reg *regs; |
| struct cleanup *old_chain; |
| |
| gdb_assert (regnum < gdbarch_num_regs (gdbarch)); |
| |
| regs = XCNEWVEC (struct packet_reg, gdbarch_num_regs (gdbarch)); |
| old_chain = make_cleanup (xfree, regs); |
| |
| map_regcache_remote_table (gdbarch, regs); |
| |
| *pnum = regs[regnum].pnum; |
| *poffset = regs[regnum].offset; |
| |
| do_cleanups (old_chain); |
| |
| return *pnum != -1; |
| } |
| |
| static void * |
| init_remote_state (struct gdbarch *gdbarch) |
| { |
| struct remote_state *rs = get_remote_state_raw (); |
| struct remote_arch_state *rsa; |
| |
| rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state); |
| |
| /* Use the architecture to build a regnum<->pnum table, which will be |
| 1:1 unless a feature set specifies otherwise. */ |
| rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, |
| gdbarch_num_regs (gdbarch), |
| struct packet_reg); |
| |
| /* Record the maximum possible size of the g packet - it may turn out |
| to be smaller. */ |
| rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs); |
| |
| /* Default maximum number of characters in a packet body. Many |
| remote stubs have a hardwired buffer size of 400 bytes |
| (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used |
| as the maximum packet-size to ensure that the packet and an extra |
| NUL character can always fit in the buffer. This stops GDB |
| trashing stubs that try to squeeze an extra NUL into what is |
| already a full buffer (As of 1999-12-04 that was most stubs). */ |
| rsa->remote_packet_size = 400 - 1; |
| |
| /* This one is filled in when a ``g'' packet is received. */ |
| rsa->actual_register_packet_size = 0; |
| |
| /* Should rsa->sizeof_g_packet needs more space than the |
| default, adjust the size accordingly. Remember that each byte is |
| encoded as two characters. 32 is the overhead for the packet |
| header / footer. NOTE: cagney/1999-10-26: I suspect that 8 |
| (``$NN:G...#NN'') is a better guess, the below has been padded a |
| little. */ |
| if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2)) |
| rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32); |
| |
| /* Make sure that the packet buffer is plenty big enough for |
| this architecture. */ |
| if (rs->buf_size < rsa->remote_packet_size) |
| { |
| rs->buf_size = 2 * rsa->remote_packet_size; |
| rs->buf = (char *) xrealloc (rs->buf, rs->buf_size); |
| } |
| |
| return rsa; |
| } |
| |
| /* Return the current allowed size of a remote packet. This is |
| inferred from the current architecture, and should be used to |
| limit the length of outgoing packets. */ |
| static long |
| get_remote_packet_size (void) |
| { |
| struct remote_state *rs = get_remote_state (); |
| struct remote_arch_state *rsa = get_remote_arch_state (); |
| |
| if (rs->explicit_packet_size) |
| return rs->explicit_packet_size; |
| |
| return rsa->remote_packet_size; |
| } |
| |
| static struct packet_reg * |
| packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum) |
| { |
| if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ())) |
| return NULL; |
| else |
| { |
| struct packet_reg *r = &rsa->regs[regnum]; |
| |
| gdb_assert (r->regnum == regnum); |
| return r; |
| } |
| } |
| |
| static struct packet_reg * |
| packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum) |
| { |
| int i; |
| |
| for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++) |
| { |
| struct packet_reg *r = &rsa->regs[i]; |
| |
| if (r->pnum == pnum) |
| return r; |
| } |
| return NULL; |
| } |
| |
| static struct target_ops remote_ops; |
| |
| static struct target_ops extended_remote_ops; |
| |
| /* FIXME: cagney/1999-09-23: Even though getpkt was called with |
| ``forever'' still use the normal timeout mechanism. This is |
| currently used by the ASYNC code to guarentee that target reads |
| during the initial connect always time-out. Once getpkt has been |
| modified to return a timeout indication and, in turn |
| remote_wait()/wait_for_inferior() have gained a timeout parameter |
| this can go away. */ |
| static int wait_forever_enabled_p = 1; |
| |
| /* Allow the user to specify what sequence to send to the remote |
| when he requests a program interruption: Although ^C is usually |
| what remote systems expect (this is the default, here), it is |
| sometimes preferable to send a break. On other systems such |
| as the Linux kernel, a break followed by g, which is Magic SysRq g |
| is required in order to interrupt the execution. */ |
| const char interrupt_sequence_control_c[] = "Ctrl-C"; |
| const char interrupt_sequence_break[] = "BREAK"; |
| const char interrupt_sequence_break_g[] = "BREAK-g"; |
| static const char *const interrupt_sequence_modes[] = |
| { |
| interrupt_sequence_control_c, |
| interrupt_sequence_break, |
| interrupt_sequence_break_g, |
| NULL |
| }; |
| static const char *interrupt_sequence_mode = interrupt_sequence_control_c; |
| |
| static void |
| show_interrupt_sequence (struct ui_file *file, int from_tty, |
| struct cmd_list_element *c, |
| const char *value) |
| { |
| if (interrupt_sequence_mode == interrupt_sequence_control_c) |
| fprintf_filtered (file, |
| _("Send the ASCII ETX character (Ctrl-c) " |
| "to the remote target to interrupt the " |
| "execution of the program.\n")); |
| else if (interrupt_sequence_mode == interrupt_sequence_break) |
| fprintf_filtered (file, |
| _("send a break signal to the remote target " |
| "to interrupt the execution of the program.\n")); |
| else if (interrupt_sequence_mode == interrupt_sequence_break_g) |
| fprintf_filtered (file, |
| _("Send a break signal and 'g' a.k.a. Magic SysRq g to " |
| "the remote target to interrupt the execution " |
| "of Linux kernel.\n")); |
| else |
| internal_error (__FILE__, __LINE__, |
| _("Invalid value for interrupt_sequence_mode: %s."), |
| interrupt_sequence_mode); |
| } |
| |
| /* This boolean variable specifies whether interrupt_sequence is sent |
| to the remote target when gdb connects to it. |
| This is mostly needed when you debug the Linux kernel: The Linux kernel |
| expects BREAK g which is Magic SysRq g for connecting gdb. */ |
| static int interrupt_on_connect = 0; |
| |
| /* This variable is used to implement the "set/show remotebreak" commands. |
| Since these commands are now deprecated in favor of "set/show remote |
| interrupt-sequence", it no longer has any effect on the code. */ |
| static int remote_break; |
| |
| static void |
| set_remotebreak (char *args, int from_tty, struct cmd_list_element *c) |
| { |
| if (remote_break) |
| interrupt_sequence_mode = interrupt_sequence_break; |
| else |
| interrupt_sequence_mode = interrupt_sequence_control_c; |
| } |
| |
| static void |
| show_remotebreak (struct ui_file *file, int from_tty, |
| struct cmd_list_element *c, |
| const char *value) |
| { |
| } |
| |
| /* This variable sets the number of bits in an address that are to be |
| sent in a memory ("M" or "m") packet. Normally, after stripping |
| leading zeros, the entire address would be sent. This variable |
| restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The |
| initial implementation of remote.c restricted the address sent in |
| memory packets to ``host::sizeof long'' bytes - (typically 32 |
| bits). Consequently, for 64 bit targets, the upper 32 bits of an |
| address was never sent. Since fixing this bug may cause a break in |
| some remote targets this variable is principly provided to |
| facilitate backward compatibility. */ |
| |
| static unsigned int remote_address_size; |
| |
| /* Temporary to track who currently owns the terminal. See |
| remote_terminal_* for more details. */ |
| |
| static int remote_async_terminal_ours_p; |
| |
| |
| /* User configurable variables for the number of characters in a |
| memory read/write packet. MIN (rsa->remote_packet_size, |
| rsa->sizeof_g_packet) is the default. Some targets need smaller |
| values (fifo overruns, et.al.) and some users need larger values |
| (speed up transfers). The variables ``preferred_*'' (the user |
| request), ``current_*'' (what was actually set) and ``forced_*'' |
| (Positive - a soft limit, negative - a hard limit). */ |
| |
| struct memory_packet_config |
| { |
| char *name; |
| long size; |
| int fixed_p; |
| }; |
| |
| /* The default max memory-write-packet-size. The 16k is historical. |
| (It came from older GDB's using alloca for buffers and the |
| knowledge (folklore?) that some hosts don't cope very well with |
| large alloca calls.) */ |
| #define DEFAULT_MAX_MEMORY_PACKET_SIZE 16384 |
| |
| /* The minimum remote packet size for memory transfers. Ensures we |
| can write at least one byte. */ |
| #define MIN_MEMORY_PACKET_SIZE 20 |
| |
| /* Compute the current size of a read/write packet. Since this makes |
| use of ``actual_register_packet_size'' the computation is dynamic. */ |
| |
| static long |
| get_memory_packet_size (struct memory_packet_config *config) |
| { |
| struct remote_state *rs = get_remote_state (); |
| struct remote_arch_state *rsa = get_remote_arch_state (); |
| |
| long what_they_get; |
| if (config->fixed_p) |
| { |
| if (config->size <= 0) |
| what_they_get = DEFAULT_MAX_MEMORY_PACKET_SIZE; |
| else |
| what_they_get = config->size; |
| } |
| else |
| { |
| what_they_get = get_remote_packet_size (); |
| /* Limit the packet to the size specified by the user. */ |
| if (config->size > 0 |
| && what_they_get > config->size) |
| what_they_get = config->size; |
| |
| /* Limit it to the size of the targets ``g'' response unless we have |
| permission from the stub to use a larger packet size. */ |
| if (rs->explicit_packet_size == 0 |
| && rsa->actual_register_packet_size > 0 |
| && what_they_get > rsa->actual_register_packet_size) |
| what_they_get = rsa->actual_register_packet_size; |
| } |
| if (what_they_get < MIN_MEMORY_PACKET_SIZE) |
| what_they_get = MIN_MEMORY_PACKET_SIZE; |
| |
| /* Make sure there is room in the global buffer for this packet |
| (including its trailing NUL byte). */ |
| if (rs->buf_size < what_they_get + 1) |
| { |
| rs->buf_size = 2 * what_they_get; |
| rs->buf = (char *) xrealloc (rs->buf, 2 * what_they_get); |
| } |
| |
| return what_they_get; |
| } |
| |
| /* Update the size of a read/write packet. If they user wants |
| something really big then do a sanity check. */ |
| |
| static void |
| set_memory_packet_size (char *args, struct memory_packet_config *config) |
| { |
| int fixed_p = config->fixed_p; |
| long size = config->size; |
| |
| if (args == NULL) |
| error (_("Argument required (integer, `fixed' or `limited').")); |
| else if (strcmp (args, "hard") == 0 |
| || strcmp (args, "fixed") == 0) |
| fixed_p = 1; |
| else if (strcmp (args, "soft") == 0 |
| || strcmp (args, "limit") == 0) |
| fixed_p = 0; |
| else |
| { |
| char *end; |
| |
| size = strtoul (args, &end, 0); |
| if (args == end) |
| error (_("Invalid %s (bad syntax)."), config->name); |
| |
| /* Instead of explicitly capping the size of a packet to or |
| disallowing it, the user is allowed to set the size to |
| something arbitrarily large. */ |
| } |
| |
| /* So that the query shows the correct value. */ |
| if (size <= 0) |
| size = DEFAULT_MAX_MEMORY_PACKET_SIZE; |
| |
| /* Extra checks? */ |
| if (fixed_p && !config->fixed_p) |
| { |
| if (! query (_("The target may not be able to correctly handle a %s\n" |
| "of %ld bytes. Change the packet size? "), |
| config->name, size)) |
| error (_("Packet size not changed.")); |
| } |
| /* Update the config. */ |
| config->fixed_p = fixed_p; |
| config->size = size; |
| } |
| |
| static void |
| show_memory_packet_size (struct memory_packet_config *config) |
| { |
| printf_filtered (_("The %s is %ld. "), config->name, config->size); |
| if (config->fixed_p) |
| printf_filtered (_("Packets are fixed at %ld bytes.\n"), |
| get_memory_packet_size (config)); |
| else |
| printf_filtered (_("Packets are limited to %ld bytes.\n"), |
| get_memory_packet_size (config)); |
| } |
| |
| static struct memory_packet_config memory_write_packet_config = |
| { |
| "memory-write-packet-size", |
| }; |
| |
| static void |
| set_memory_write_packet_size (char *args, int from_tty) |
| { |
| set_memory_packet_size (args, &memory_write_packet_config); |
| } |
| |
| static void |
| show_memory_write_packet_size (char *args, int from_tty) |
| { |
| show_memory_packet_size (&memory_write_packet_config); |
| } |
| |
| static long |
| get_memory_write_packet_size (void) |
| { |
| return get_memory_packet_size (&memory_write_packet_config); |
| } |
| |
| static struct memory_packet_config memory_read_packet_config = |
| { |
| "memory-read-packet-size", |
| }; |
| |
| static void |
| set_memory_read_packet_size (char *args, int from_tty) |
| { |
| set_memory_packet_size (args, &memory_read_packet_config); |
| } |
| |
| static void |
| show_memory_read_packet_size (char *args, int from_tty) |
| { |
| show_memory_packet_size (&memory_read_packet_config); |
| } |
| |
| static long |
| get_memory_read_packet_size (void) |
| { |
| long size = get_memory_packet_size (&memory_read_packet_config); |
| |
| /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an |
| extra buffer size argument before the memory read size can be |
| increased beyond this. */ |
| if (size > get_remote_packet_size ()) |
| size = get_remote_packet_size (); |
| return size; |
| } |
| |
| |
| /* Generic configuration support for packets the stub optionally |
| supports. Allows the user to specify the use of the packet as well |
| as allowing GDB to auto-detect support in the remote stub. */ |
| |
| enum packet_support |
| { |
| PACKET_SUPPORT_UNKNOWN = 0, |
| PACKET_ENABLE, |
| PACKET_DISABLE |
| }; |
| |
| struct packet_config |
| { |
| const char *name; |
| const char *title; |
| |
| /* If auto, GDB auto-detects support for this packet or feature, |
| either through qSupported, or by trying the packet and looking |
| at the response. If true, GDB assumes the target supports this |
| packet. If false, the packet is disabled. Configs that don't |
| have an associated command always have this set to auto. */ |
| enum auto_boolean detect; |
| |
| /* Does the target support this packet? */ |
| enum packet_support support; |
| }; |
| |
| /* Analyze a packet's return value and update the packet config |
| accordingly. */ |
| |
| enum packet_result |
| { |
| PACKET_ERROR, |
| PACKET_OK, |
| PACKET_UNKNOWN |
| }; |
| |
| static enum packet_support packet_config_support (struct packet_config *config); |
| static enum packet_support packet_support (int packet); |
| |
| static void |
| show_packet_config_cmd (struct packet_config *config) |
| { |
| char *support = "internal-error"; |
| |
| switch (packet_config_support (config)) |
| { |
| case PACKET_ENABLE: |
| support = "enabled"; |
| break; |
| case PACKET_DISABLE: |
| support = "disabled"; |
| break; |
| case PACKET_SUPPORT_UNKNOWN: |
| support = "unknown"; |
| break; |
| } |
| switch (config->detect) |
| { |
| case AUTO_BOOLEAN_AUTO: |
| printf_filtered (_("Support for the `%s' packet " |
| "is auto-detected, currently %s.\n"), |
| config->name, support); |
| break; |
| case AUTO_BOOLEAN_TRUE: |
| case AUTO_BOOLEAN_FALSE: |
| printf_filtered (_("Support for the `%s' packet is currently %s.\n"), |
| config->name, support); |
| break; |
| } |
| } |
| |
| static void |
| add_packet_config_cmd (struct packet_config *config, const char *name, |
| const char *title, int legacy) |
| { |
| char *set_doc; |
| char *show_doc; |
| char *cmd_name; |
| |
| config->name = name; |
| config->title = title; |
| set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet", |
| name, title); |
| show_doc = xstrprintf ("Show current use of remote " |
| "protocol `%s' (%s) packet", |
| name, title); |
| /* set/show TITLE-packet {auto,on,off} */ |
| cmd_name = xstrprintf ("%s-packet", title); |
| add_setshow_auto_boolean_cmd (cmd_name, class_obscure, |
| &config->detect, set_doc, |
| show_doc, NULL, /* help_doc */ |
| NULL, |
| show_remote_protocol_packet_cmd, |
| &remote_set_cmdlist, &remote_show_cmdlist); |
| /* The command code copies the documentation strings. */ |
| xfree (set_doc); |
| xfree (show_doc); |
| /* set/show remote NAME-packet {auto,on,off} -- legacy. */ |
| if (legacy) |
| { |
| char *legacy_name; |
| |
| legacy_name = xstrprintf ("%s-packet", name); |
| add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
| &remote_set_cmdlist); |
| add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
| &remote_show_cmdlist); |
| } |
| } |
| |
| static enum packet_result |
| packet_check_result (const char *buf) |
| { |
| if (buf[0] != '\0') |
| { |
| /* The stub recognized the packet request. Check that the |
| operation succeeded. */ |
| if (buf[0] == 'E' |
| && isxdigit (buf[1]) && isxdigit (buf[2]) |
| && buf[3] == '\0') |
| /* "Enn" - definitly an error. */ |
| return PACKET_ERROR; |
| |
| /* Always treat "E." as an error. This will be used for |
| more verbose error messages, such as E.memtypes. */ |
| if (buf[0] == 'E' && buf[1] == '.') |
| return PACKET_ERROR; |
| |
| /* The packet may or may not be OK. Just assume it is. */ |
| return PACKET_OK; |
| } |
| else |
| /* The stub does not support the packet. */ |
| return PACKET_UNKNOWN; |
| } |
| |
| static enum packet_result |
| packet_ok (const char *buf, struct packet_config *config) |
| { |
| enum packet_result result; |
| |
| if (config->detect != AUTO_BOOLEAN_TRUE |
| && config->support == PACKET_DISABLE) |
| internal_error (__FILE__, __LINE__, |
| _("packet_ok: attempt to use a disabled packet")); |
| |
| result = packet_check_result (buf); |
| switch (result) |
| { |
| case PACKET_OK: |
| case PACKET_ERROR: |
| /* The stub recognized the packet request. */ |
| if (config->support == PACKET_SUPPORT_UNKNOWN) |
| { |
| if (remote_debug) |
| fprintf_unfiltered (gdb_stdlog, |
| "Packet %s (%s) is supported\n", |
| config->name, config->title); |
| config->support = PACKET_ENABLE; |
| } |
| break; |
| case PACKET_UNKNOWN: |
| /* The stub does not support the packet. */ |
| if (config->detect == AUTO_BOOLEAN_AUTO |
| && config->support == PACKET_ENABLE) |
| { |
| /* If the stub previously indicated that the packet was |
| supported then there is a protocol error. */ |
| error (_("Protocol error: %s (%s) conflicting enabled responses."), |
| config->name, config->title); |
| } |
| else if (config->detect == AUTO_BOOLEAN_TRUE) |
| { |
| /* The user set it wrong. */ |
| error (_("Enabled packet %s (%s) not recognized by stub"), |
| config->name, config->title); |
| } |
| |
| if (remote_debug) |
| fprintf_unfiltered (gdb_stdlog, |
| "Packet %s (%s) is NOT supported\n", |
| config->name, config->title); |
| config->support = PACKET_DISABLE; |
| break; |
| } |
| |
| return result; |
| } |
| |
| enum { |
| PACKET_vCont = 0, |
| PACKET_X, |
| PACKET_qSymbol, |
| PACKET_P, |
| PACKET_p, |
| PACKET_Z0, |
| PACKET_Z1, |
| PACKET_Z2, |
| PACKET_Z3, |
| PACKET_Z4, |
| PACKET_vFile_setfs, |
| PACKET_vFile_open, |
| PACKET_vFile_pread, |
| PACKET_vFile_pwrite, |
| PACKET_vFile_close, |
| PACKET_vFile_unlink, |
| PACKET_vFile_readlink, |
| PACKET_vFile_fstat, |
| PACKET_qXfer_auxv, |
| PACKET_qXfer_features, |
| PACKET_qXfer_exec_file, |
| PACKET_qXfer_libraries, |
| PACKET_qXfer_libraries_svr4, |
| PACKET_qXfer_memory_map, |
| PACKET_qXfer_spu_read, |
| PACKET_qXfer_spu_write, |
| PACKET_qXfer_osdata, |
| PACKET_qXfer_threads, |
| PACKET_qXfer_statictrace_read, |
| PACKET_qXfer_traceframe_info, |
| PACKET_qXfer_uib, |
| PACKET_qGetTIBAddr, |
| PACKET_qGetTLSAddr, |
| PACKET_qSupported, |
| PACKET_qTStatus, |
| PACKET_QPassSignals, |
| PACKET_QCatchSyscalls, |
| PACKET_QProgramSignals, |
| PACKET_qCRC, |
| PACKET_qSearch_memory, |
| PACKET_vAttach, |
| PACKET_vRun, |
| PACKET_QStartNoAckMode, |
| PACKET_vKill, |
| PACKET_qXfer_siginfo_read, |
| PACKET_qXfer_siginfo_write, |
| PACKET_qAttached, |
| |
| /* Support for conditional tracepoints. */ |
| PACKET_ConditionalTracepoints, |
| |
| /* Support for target-side breakpoint conditions. */ |
| PACKET_ConditionalBreakpoints, |
| |
| /* Support for target-side breakpoint commands. */ |
| PACKET_BreakpointCommands, |
| |
| /* Support for fast tracepoints. */ |
| PACKET_FastTracepoints, |
| |
| /* Support for static tracepoints. */ |
| PACKET_StaticTracepoints, |
| |
| /* Support for installing tracepoints while a trace experiment is |
| running. */ |
| PACKET_InstallInTrace, |
| |
| PACKET_bc, |
| PACKET_bs, |
| PACKET_TracepointSource, |
| PACKET_QAllow, |
| PACKET_qXfer_fdpic, |
| PACKET_QDisableRandomization, |
| PACKET_QAgent, |
| PACKET_QTBuffer_size, |
| PACKET_Qbtrace_off, |
| PACKET_Qbtrace_bts, |
| PACKET_Qbtrace_pt, |
| PACKET_qXfer_btrace, |
| |
| /* Support for the QNonStop packet. */ |
| PACKET_QNonStop, |
| |
| /* Support for the QThreadEvents packet. */ |
| PACKET_QThreadEvents, |
| |
| /* Support for multi-process extensions. */ |
| PACKET_multiprocess_feature, |
| |
| /* Support for enabling and disabling tracepoints while a trace |
| experiment is running. */ |
| PACKET_EnableDisableTracepoints_feature, |
| |
| /* Support for collecting strings using the tracenz bytecode. */ |
| PACKET_tracenz_feature, |
| |
| /* Support for continuing to run a trace experiment while GDB is |
| disconnected. */ |
| PACKET_DisconnectedTracing_feature, |
| |
| /* Support for qXfer:libraries-svr4:read with a non-empty annex. */ |
| PACKET_augmented_libraries_svr4_read_feature, |
| |
| /* Support for the qXfer:btrace-conf:read packet. */ |
| PACKET_qXfer_btrace_conf, |
| |
| /* Support for the Qbtrace-conf:bts:size packet. */ |
| PACKET_Qbtrace_conf_bts_size, |
| |
| /* Support for swbreak+ feature. */ |
| PACKET_swbreak_feature, |
| |
| /* Support for hwbreak+ feature. */ |
| PACKET_hwbreak_feature, |
| |
| /* Support for fork events. */ |
| PACKET_fork_event_feature, |
| |
| /* Support for vfork events. */ |
| PACKET_vfork_event_feature, |
| |
| /* Support for the Qbtrace-conf:pt:size packet. */ |
| PACKET_Qbtrace_conf_pt_size, |
| |
| /* Support for exec events. */ |
| PACKET_exec_event_feature, |
| |
| /* Support for query supported vCont actions. */ |
| PACKET_vContSupported, |
| |
| /* Support remote CTRL-C. */ |
| PACKET_vCtrlC, |
| |
| /* Support TARGET_WAITKIND_NO_RESUMED. */ |
| PACKET_no_resumed, |
| |
| PACKET_MAX |
| }; |
| |
| static struct packet_config remote_protocol_packets[PACKET_MAX]; |
| |
| /* Returns the packet's corresponding "set remote foo-packet" command |
| state. See struct packet_config for more details. */ |
| |
| static enum auto_boolean |
| packet_set_cmd_state (int packet) |
| { |
| return remote_protocol_packets[packet].detect; |
| } |
| |
| /* Returns whether a given packet or feature is supported. This takes |
| into account the state of the corresponding "set remote foo-packet" |
| command, which may be used to bypass auto-detection. */ |
| |
| static enum packet_support |
| packet_config_support (struct packet_config *config) |
| { |
| switch (config->detect) |
| { |
| case AUTO_BOOLEAN_TRUE: |
| return PACKET_ENABLE; |
| case AUTO_BOOLEAN_FALSE: |
| return PACKET_DISABLE; |
| case AUTO_BOOLEAN_AUTO: |
| return config->support; |
| default: |
| gdb_assert_not_reached (_("bad switch")); |
| } |
| } |
| |
| /* Same as packet_config_support, but takes the packet's enum value as |
| argument. */ |
| |
| static enum packet_support |
| packet_support (int packet) |
| { |
| struct packet_config *config = &remote_protocol_packets[packet]; |
| |
| return packet_config_support (config); |
| } |
| |
| static void |
| show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty, |
| struct cmd_list_element *c, |
| const char *value) |
| { |
| struct packet_config *packet; |
| |
| for (packet = remote_protocol_packets; |
| packet < &remote_protocol_packets[PACKET_MAX]; |
| packet++) |
| { |
| if (&packet->detect == c->var) |
| { |
| show_packet_config_cmd (packet); |
| return; |
| } |
| } |
| internal_error (__FILE__, __LINE__, _("Could not find config for %s"), |
| c->name); |
| } |
| |
| /* Should we try one of the 'Z' requests? */ |
| |
| enum Z_packet_type |
| { |
| Z_PACKET_SOFTWARE_BP, |
| Z_PACKET_HARDWARE_BP, |
| Z_PACKET_WRITE_WP, |
| Z_PACKET_READ_WP, |
| Z_PACKET_ACCESS_WP, |
| NR_Z_PACKET_TYPES |
| }; |
| |
| /* For compatibility with older distributions. Provide a ``set remote |
| Z-packet ...'' command that updates all the Z packet types. */ |
| |
| static enum auto_boolean remote_Z_packet_detect; |
| |
| static void |
| set_remote_protocol_Z_packet_cmd (char *args, int from_tty, |
| struct cmd_list_element *c) |
| { |
| int i; |
| |
| for (i = 0; i < NR_Z_PACKET_TYPES; i++) |
| remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect; |
| } |
| |
| static void |
| show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty, |
| struct cmd_list_element *c, |
| const char *value) |
| { |
| int i; |
| |
| for (i = 0; i < NR_Z_PACKET_TYPES; i++) |
| { |
| show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]); |
| } |
| } |
| |
| /* Returns true if the multi-process extensions are in effect. */ |
| |
| static int |
| remote_multi_process_p (struct remote_state *rs) |
| { |
| return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE; |
| } |
| |
| /* Returns true if fork events are supported. */ |
| |
| static int |
| remote_fork_event_p (struct remote_state *rs) |
| { |
| return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE; |
| } |
| |
| /* Returns true if vfork events are supported. */ |
| |
| static int |
| remote_vfork_event_p (struct remote_state *rs) |
| { |
| return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE; |
| } |
| |
| /* Returns true if exec events are supported. */ |
| |
| static int |
| remote_exec_event_p (struct remote_state *rs) |
| { |
| return packet_support (PACKET_exec_event_feature) == PACKET_ENABLE; |
| } |
| |
| /* Insert fork catchpoint target routine. If fork events are enabled |
| then return success, nothing more to do. */ |
| |
| static int |
| remote_insert_fork_catchpoint (struct target_ops *ops, int pid) |
| { |
| struct remote_state *rs = get_remote_state (); |
| |
| return !remote_fork_event_p (rs); |
| } |
| |
| /* Remove fork catchpoint target routine. Nothing to do, just |
| return success. */ |
| |
| static int |
| remote_remove_fork_catchpoint (struct target_ops *ops, int pid) |
| { |
| return 0; |
| } |
| |
| /* Insert vfork catchpoint target routine. If vfork events are enabled |
| then return success, nothing more to do. */ |
| |
| static int |
| remote_insert_vfork_catchpoint (struct target_ops *ops, int pid) |
| { |
| struct remote_state *rs = get_remote_state (); |
| |
| return !remote_vfork_event_p (rs); |
| } |
| |
| /* Remove vfork catchpoint target routine. Nothing to do, just |
| return success. */ |
| |
| static int |
| remote_remove_vfork_catchpoint (struct target_ops *ops, int pid) |
| { |
| return 0; |
| } |
| |
| /* Insert exec catchpoint target routine. If exec events are |
| enabled, just return success. */ |
| |
| static int |
| remote_insert_exec_catchpoint (struct target_ops *ops, int pid) |
| { |
| struct remote_state *rs = get_remote_state (); |
| |
| return !remote_exec_event_p (rs); |
| } |
| |
| /* Remove exec catchpoint target routine. Nothing to do, just |
| return success. */ |
| |
| static int |
| remote_remove_exec_catchpoint (struct target_ops *ops, int pid) |
| { |
| return 0; |
| } |
| |
| |
| /* Asynchronous signal handle registered as event loop source for |
| when we have pending events ready to be passed to the core. */ |
| |
| static struct async_event_handler *remote_async_inferior_event_token; |
| |
| |
| |
| static ptid_t magic_null_ptid; |
| static ptid_t not_sent_ptid; |
| static ptid_t any_thread_ptid; |
| |
| /* Find out if the stub attached to PID (and hence GDB should offer to |
| detach instead of killing it when bailing out). */ |
| |
| static int |
| remote_query_attached (int pid) |
| { |
| struct remote_state *rs = get_remote_state (); |
| size_t size = get_remote_packet_size (); |
| |
| if (packet_support (PACKET_qAttached) == PACKET_DISABLE) |
| return 0; |
| |
| if (remote_multi_process_p (rs)) |
| xsnprintf (rs->buf, size, "qAttached:%x", pid); |
| else |
| xsnprintf (rs->buf, size, "qAttached"); |
| |
| putpkt (rs->buf); |
| getpkt (&rs->buf, &rs->buf_size, 0); |
| |
| switch (packet_ok (rs->buf, |
| &remote_protocol_packets[PACKET_qAttached])) |
| { |
| case PACKET_OK: |
| if (strcmp (rs->buf, "1") == 0) |
| return 1; |
| break; |
| case PACKET_ERROR: |
| warning (_("Remote failure reply: %s"), rs->buf); |
| break; |
| case PACKET_UNKNOWN: |
| break; |
| } |
| |
| return 0; |
| } |
| |
| /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID |
| has been invented by GDB, instead of reported by the target. Since |
| we can be connected to a remote system before before knowing about |
| any inferior, mark the target with execution when we find the first |
| inferior. If ATTACHED is 1, then we had just attached to this |
| inferior. If it is 0, then we just created this inferior. If it |
| is -1, then try querying the remote stub to find out if it had |
| attached to the inferior or not. If TRY_OPEN_EXEC is true then |
| attempt to open this inferior's executable as the main executable |
| if no main executable is open already. */ |
| |
| static struct inferior * |
| remote_add_inferior (int fake_pid_p, int pid, int attached, |
| int try_open_exec) |
| { |
| struct inferior *inf; |
| |
| /* Check whether this process we're learning about is to be |
| considered attached, or if is to be considered to have been |
| spawned by the stub. */ |
| if (attached == -1) |
| attached = remote_query_attached (pid); |
| |
| if (gdbarch_has_global_solist (target_gdbarch ())) |
| { |
| /* If the target shares code across all inferiors, then every |
| attach adds a new inferior. */ |
| inf = add_inferior (pid); |
| |
| /* ... and every inferior is bound to the same program space. |
| However, each inferior may still have its own address |
| space. */ |
| inf->aspace = maybe_new_address_space (); |
| inf->pspace = current_program_space; |
| } |
| else |
| { |
| /* In the traditional debugging scenario, there's a 1-1 match |
| between program/address spaces. We simply bind the inferior |
| to the program space's address space. */ |
| inf = current_inferior (); |
| inferior_appeared (inf, pid); |
| } |
| |
| inf->attach_flag = attached; |
| inf->fake_pid_p = fake_pid_p; |
| |
| /* If no main executable is currently open then attempt to |
| open the file that was executed to create this inferior. */ |
| if (try_open_exec && get_exec_file (0) == NULL) |
| exec_file_locate_attach (pid, 1); |
| |
| return inf; |
| } |
| |
| /* Add thread PTID to GDB's thread list. Tag it as executing/running |
| according to RUNNING. */ |
| |
| static void |
| remote_add_thread (ptid_t ptid, int running, int executing) |
| { |
| struct remote_state *rs = get_remote_state (); |
| |
| /* GDB historically didn't pull threads in the initial connection |
| setup. If the remote target doesn't even have a concept of |
| threads (e.g., a bare-metal target), even if internally we |
| consider that a single-threaded target, mentioning a new thread |
| might be confusing to the user. Be silent then, preserving the |
| age old behavior. */ |
| if (rs->starting_up) |
| add_thread_silent (ptid); |
| else |
| add_thread (ptid); |
| |
| set_executing (ptid, executing); |
| set_running (ptid, running); |
| } |
| |
| /* Come here when we learn about a thread id from the remote target. |
| It may be the first time we hear about such thread, so take the |
| opportunity to add it to GDB's thread list. In case this is the |
| first time we're noticing its corresponding inferior, add it to |
| GDB's inferior list as well. EXECUTING indicates whether the |
| thread is (internally) executing or stopped. */ |
| |
| static void |
| remote_notice_new_inferior (ptid_t currthread, int executing) |
| { |
| /* In non-stop mode, we assume new found threads are (externally) |
| running until proven otherwise with a stop reply. In all-stop, |
| we can only get here if all threads are stopped. */ |
| int running = target_is_non_stop_p () ? 1 : 0; |
| |
| /* If this is a new thread, add it to GDB's thread list. |
| If we leave it up to WFI to do this, bad things will happen. */ |
| |
| if (in_thread_list (currthread) && is_exited (currthread)) |
| { |
| /* We're seeing an event on a thread id we knew had exited. |
| This has to be a new thread reusing the old id. Add it. */ |
| remote_add_thread (currthread, running, executing); |
| return; |
| } |
| |
| if (!in_thread_list (currthread)) |
| { |
| struct inferior *inf = NULL; |
| int pid = ptid_get_pid (currthread); |
| |
| if (ptid_is_pid (inferior_ptid) |
| && pid == ptid_get_pid (inferior_ptid)) |
| { |
| /* inferior_ptid has no thread member yet. This can happen |
| with the vAttach -> remote_wait,"TAAthread:" path if the |
| stub doesn't support qC. This is the first stop reported |
| after an attach, so this is the main thread. Update the |
| ptid in the thread list. */ |
| if (in_thread_list (pid_to_ptid (pid))) |
| thread_change_ptid (inferior_ptid, currthread); |
| else |
| { |
| remote_add_thread (currthread, running, executing); |
| inferior_ptid = currthread; |
| } |
| return; |
| } |
| |
| if (ptid_equal (magic_null_ptid, inferior_ptid)) |
| { |
| /* inferior_ptid is not set yet. This can happen with the |
| vRun -> remote_wait,"TAAthread:" path if the stub |
| doesn't support qC. This is the first stop reported |
| after an attach, so this is the main thread. Update the |
| ptid in the thread list. */ |
| thread_change_ptid (inferior_ptid, currthread); |
| return; |
| } |
| |
| /* When connecting to a target remote, or to a target |
| extended-remote which already was debugging an inferior, we |
| may not know about it yet. Add it before adding its child |
| thread, so notifications are emitted in a sensible order. */ |
| if (!in_inferior_list (ptid_get_pid (currthread))) |
| { |
| struct remote_state *rs = get_remote_state (); |
| int fake_pid_p = !remote_multi_process_p (rs); |
| |
| inf = remote_add_inferior (fake_pid_p, |
| ptid_get_pid (currthread), -1, 1); |
| } |
| |
| /* This is really a new thread. Add it. */ |
| remote_add_thread (currthread, running, executing); |
| |
| /* If we found a new inferior, let the common code do whatever |
| it needs to with it (e.g., read shared libraries, insert |
| breakpoints), unless we're just setting up an all-stop |
| connection. */ |
| if (inf != NULL) |
| { |
| struct remote_state *rs = get_remote_state (); |
| |
| if (!rs->starting_up) |
| notice_new_inferior (currthread, executing, 0); |
| } |
| } |
| } |
| |
| /* Return the private thread data, creating it if necessary. */ |
| |
| static struct private_thread_info * |
| demand_private_info (ptid_t ptid) |
| { |
| struct thread_info *info = find_thread_ptid (ptid); |
| |
| gdb_assert (info); |
| |
| if (!info->priv) |
| { |
| info->priv = XNEW (struct private_thread_info); |
| info->private_dtor = free_private_thread_info; |
| info->priv->core = -1; |
| info->priv->extra = NULL; |
| info->priv->name = NULL; |
| } |
| |
| return info->priv; |
| } |
| |
| /* Call this function as a result of |
| 1) A halt indication (T packet) containing a thread id |
| 2) A direct query of currthread |
| 3) Successful execution of set thread */ |
| |
| static void |
| record_currthread (struct remote_state *rs, ptid_t currthread) |
| { |
| rs->general_thread = currthread; |
| } |
| |
| /* If 'QPassSignals' is supported, tell the remote stub what signals |
| it can simply pass through to the inferior without reporting. */ |
| |
| static void |
| remote_pass_signals (struct target_ops *self, |
| int numsigs, unsigned char *pass_signals) |
| { |
| if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE) |
| { |
| char *pass_packet, *p; |
| int count = 0, i; |
| struct remote_state *rs = get_remote_state (); |
| |
| gdb_assert (numsigs < 256); |
| for (i = 0; i < numsigs; i++) |
| { |
| if (pass_signals[i]) |
| count++; |
| } |
| pass_packet = (char *) xmalloc (count * 3 + strlen ("QPassSignals:") + 1); |
| strcpy (pass_packet, "QPassSignals:"); |
| p = pass_packet + strlen (pass_packet); |
| for (i = 0; i < numsigs; i++) |
| { |
| if (pass_signals[i]) |
| { |
| if (i >= 16) |
| *p++ = tohex (i >> 4); |
| *p++ = tohex (i & 15); |
| if (count) |
| *p++ = ';'; |
| else |
| break; |
| count--; |
| } |
| } |
| *p = 0; |
| if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet)) |
| { |
| putpkt (pass_packet); |
| getpkt (&rs->buf, &rs->buf_size, 0); |
| packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]); |
| if (rs->last_pass_packet) |
| xfree (rs->last_pass_packet); |
| rs->last_pass_packet = pass_packet; |
| } |
| else |
| xfree (pass_packet); |
| } |
| } |
| |
| /* If 'QCatchSyscalls' is supported, tell the remote stub |
| to report syscalls to GDB. */ |
| |
| static int |
| remote_set_syscall_catchpoint (struct target_ops *self, |
| int pid, int needed, int any_count, |
| int table_size, int *table) |
| { |
| char *catch_packet; |
| enum packet_result result; |
| int n_sysno = 0; |
| |
| if (packet_support (PACKET_QCatchSyscalls) == PACKET_DISABLE) |
| { |
| /* Not supported. */ |
| return 1; |
| } |
| |
| if (needed && !any_count) |
| { |
| int i; |
| |
| /* Count how many syscalls are to be caught (table[sysno] != 0). */ |
| for (i = 0; i < table_size; i++) |
| { |
| if (table[i] != 0) |
| n_sysno++; |
| } |
| } |
| |
| if (remote_debug) |
| { |
| fprintf_unfiltered (gdb_stdlog, |
| "remote_set_syscall_catchpoint " |
| "pid %d needed %d any_count %d n_sysno %d\n", |
| pid, needed, any_count, n_sysno); |
| } |
| |
| if (needed) |
| { |
| /* Prepare a packet with the sysno list, assuming max 8+1 |
| characters for a sysno. If the resulting packet size is too |
| big, fallback on the non-selective packet. */ |
| const int maxpktsz = strlen ("QCatchSyscalls:1") + n_sysno * 9 + 1; |
| |
| catch_packet = (char *) xmalloc (maxpktsz); |
| strcpy (catch_packet, "QCatchSyscalls:1"); |
| if (!any_count) |
| { |
| int i; |
| char *p; |
| |
| p = catch_packet; |
| p += strlen (p); |
| |
| /* Add in catch_packet each syscall to be caught (table[i] != 0). */ |
| for (i = 0; i < table_size; i++) |
| { |
| if (table[i] != 0) |
| p += xsnprintf (p, catch_packet + maxpktsz - p, ";%x", i); |
| } |
| } |
| if (strlen (catch_packet) > get_remote_packet_size ()) |
| { |
| /* catch_packet too big. Fallback to less efficient |
| non selective mode, with GDB doing the filtering. */ |
| catch_packet[sizeof ("QCatchSyscalls:1") - 1] = 0; |
| } |
| } |
| else |
| catch_packet = xstrdup ("QCatchSyscalls:0"); |
| |
| { |
| struct cleanup *old_chain = make_cleanup (xfree, catch_packet); |
| struct remote_state *rs = get_remote_state (); |
| |
| putpkt (catch_packet); |
| getpkt (&rs->buf, &rs->buf_size, 0); |
| result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_QCatchSyscalls]); |
| do_cleanups (old_chain); |
| if (result == PACKET_OK) |
| return 0; |
| else |
| return -1; |
| } |
| } |
| |
| /* If 'QProgramSignals' is supported, tell the remote stub what |
| signals it should pass through to the inferior when detaching. */ |
| |
| static void |
| remote_program_signals (struct target_ops *self, |
| int numsigs, unsigned char *signals) |
| { |
| if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE) |
| { |
| char *packet, *p; |
| int count = 0, i; |
| struct remote_state *rs = get_remote_state (); |
| |
| gdb_assert (numsigs < 256); |
| for (i = 0; i < numsigs; i++) |
| { |
| if (signals[i]) |
| count++; |
| } |
| packet = (char *) xmalloc (count * 3 + strlen ("QProgramSignals:") + 1); |
| strcpy (packet, "QProgramSignals:"); |
| p = packet + strlen (packet); |
| for (i = 0; i < numsigs; i++) |
| { |
| if (signal_pass_state (i)) |
| { |
| if (i >= 16) |
| *p++ = tohex (i >> 4); |
| *p++ = tohex (i & 15); |
| if (count) |
| *p++ = ';'; |
| else |
| break; |
| count--; |
| } |
| } |
| *p = 0; |
| if (!rs->last_program_signals_packet |
| || strcmp (rs->last_program_signals_packet, packet) != 0) |
| { |
| putpkt (packet); |
| getpkt (&rs->buf, &rs->buf_size, 0); |
| packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]); |
| xfree (rs->last_program_signals_packet); |
| rs->last_program_signals_packet = packet; |
| } |
| else |
| xfree (packet); |
| } |
| } |
| |
| /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is |
| MINUS_ONE_PTID, set the thread to -1, so the stub returns the |
| thread. If GEN is set, set the general thread, if not, then set |
| the step/continue thread. */ |
| static void |
| set_thread (struct ptid ptid, int gen) |
| { |
| struct remote_state *rs = get_remote_state (); |
| ptid_t state = gen ? rs->general_thread : rs->continue_thread; |
| char *buf = rs->buf; |
| char *endbuf = rs->buf + get_remote_packet_size (); |
| |
| if (ptid_equal (state, ptid)) |
| return; |
| |
| *buf++ = 'H'; |
| *buf++ = gen ? 'g' : 'c'; |
| if (ptid_equal (ptid, magic_null_ptid)) |
| xsnprintf (buf, endbuf - buf, "0"); |
| else if (ptid_equal (ptid, any_thread_ptid)) |
| xsnprintf (buf, endbuf - buf, "0"); |
| else if (ptid_equal (ptid, minus_one_ptid)) |
| xsnprintf (buf, endbuf - buf, "-1"); |
| else |
| write_ptid (buf, endbuf, ptid); |
| putpkt (rs->buf); |
| getpkt (&rs->buf, &rs->buf_size, 0); |
| if (gen) |
| rs->general_thread = ptid; |
| else |
| rs->continue_thread = ptid; |
| } |
| |
| static void |
| set_general_thread (struct ptid ptid) |
| { |
| set_thread (ptid, 1); |
| } |
| |
| static void |
| set_continue_thread (struct ptid ptid) |
| { |
| set_thread (ptid, 0); |
| } |
| |
| /* Change the remote current process. Which thread within the process |
| ends up selected isn't important, as long as it is the same process |
| as what INFERIOR_PTID points to. |
| |
| This comes from that fact that there is no explicit notion of |
| "selected process" in the protocol. The selected process for |
| general operations is the process the selected general thread |
| belongs to. */ |
| |
| static void |
| set_general_process (void) |
| { |
| struct remote_state *rs = get_remote_state (); |
| |
| /* If the remote can't handle multiple processes, don't bother. */ |
| if (!remote_multi_process_p (rs)) |
| return; |
| |
| /* We only need to change the remote current thread if it's pointing |
| at some other process. */ |
| if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid)) |
| set_general_thread (inferior_ptid); |
| } |
| |
| |
| /* Return nonzero if this is the main thread that we made up ourselves |
| to model non-threaded targets as single-threaded. */ |
| |
| static int |
| remote_thread_always_alive (struct target_ops *ops, ptid_t ptid) |
| { |
| if (ptid_equal (ptid, magic_null_ptid)) |
| /* The main thread is always alive. */ |
| return 1; |
| |
| if (ptid_get_pid (ptid) != 0 && ptid_get_lwp (ptid) == 0) |
| /* The main thread is always alive. This can happen after a |
| vAttach, if the remote side doesn't support |
| multi-threading. */ |
| return 1; |
| |
| return 0; |
| } |
| |
| /* Return nonzero if the thread PTID is still alive on the remote |
| system. */ |
| |
| static int |
| remote_thread_alive (struct target_ops *ops, ptid_t ptid) |
| { |
| struct remote_state *rs = get_remote_state (); |
| char *p, *endp; |
| |
| /* Check if this is a thread that we made up ourselves to model |
| non-threaded targets as single-threaded. */ |
| if (remote_thread_always_alive (ops, ptid)) |
| return 1; |
| |
| p = rs->buf; |
| endp = rs->buf + get_remote_packet_size (); |
| |
| *p++ = 'T'; |
| write_ptid (p, endp, ptid); |
| |
| putpkt (rs->buf); |
| getpkt (&rs->buf, &rs->buf_size, 0); |
| return (rs->buf[0] == 'O' && rs->buf[1] == 'K'); |
| } |
| |
| /* Return a pointer to a thread name if we know it and NULL otherwise. |
| The thread_info object owns the memory for the name. */ |
| |
| static const char * |
| remote_thread_name (struct target_ops *ops, struct thread_info *info) |
| { |
| if (info->priv != NULL) |
| return info->priv->name; |
| |
| return NULL; |
| } |
| |
| /* About these extended threadlist and threadinfo packets. They are |
| variable length packets but, the fields within them are often fixed |
| length. They are redundent enough to send over UDP as is the |
| remote protocol in general. There is a matching unit test module |
| in libstub. */ |
| |
| /* WARNING: This threadref data structure comes from the remote O.S., |
| libstub protocol encoding, and remote.c. It is not particularly |
| changable. */ |
| |
| /* Right now, the internal structure is int. We want it to be bigger. |
| Plan to fix this. */ |
| |
| typedef int gdb_threadref; /* Internal GDB thread reference. */ |
| |
| /* gdb_ext_thread_info is an internal GDB data structure which is |
| equivalent to the reply of the remote threadinfo packet. */ |
| |
| struct gdb_ext_thread_info |
| { |
| threadref threadid; /* External form of thread reference. */ |
| int active; /* Has state interesting to GDB? |
| regs, stack. */ |
| char display[256]; /* Brief state display, name, |
| blocked/suspended. */ |
| char shortname[32]; /* To be used to name threads. */ |
| char more_display[256]; /* Long info, statistics, queue depth, |
| whatever. */ |
| }; |
| |
| /* The volume of remote transfers can be limited by submitting |
| a mask containing bits specifying the desired information. |
| Use a union of these values as the 'selection' parameter to |
| get_thread_info. FIXME: Make these TAG names more thread specific. */ |
| |
| #define TAG_THREADID 1 |
| #define TAG_EXISTS 2 |
| #define TAG_DISPLAY 4 |
| #define TAG_THREADNAME 8 |
| #define TAG_MOREDISPLAY 16 |
| |
| #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2) |
| |
| static char *unpack_nibble (char *buf, int *val); |
| |
| static char *unpack_byte (char *buf, int *value); |
| |
| static char *pack_int (char *buf, int value); |
| |
| static char *unpack_int (char *buf, int *value); |
| |
| static char *unpack_string (char *src, char *dest, int length); |
| |
| static char *pack_threadid (char *pkt, threadref *id); |
| |
| static char *unpack_threadid (char *inbuf, threadref *id); |
| |
| void int_to_threadref (threadref *id, int value); |
| |
| static int threadref_to_int (threadref *ref); |
| |
| static void copy_threadref (threadref *dest, threadref *src); |
| |
| static int threadmatch (threadref *dest, threadref *src); |
| |
| static char *pack_threadinfo_request (char *pkt, int mode, |
| threadref *id); |
| |
| static int remote_unpack_thread_info_response (char *pkt, |
| threadref *expectedref, |
| struct gdb_ext_thread_info |
| *info); |
| |
| |
| static int remote_get_threadinfo (threadref *threadid, |
| int fieldset, /*TAG mask */ |
| struct gdb_ext_thread_info *info); |
| |
| static char *pack_threadlist_request (char *pkt, int startflag, |
| int threadcount, |
| threadref *nextthread); |
| |
| static int parse_threadlist_response (char *pkt, |
| int result_limit, |
| threadref *original_echo, |
| threadref *resultlist, |
| int *doneflag); |
| |
| static int remote_get_threadlist (int startflag, |
| threadref *nextthread, |
| int result_limit, |
| int *done, |
| int *result_count, |
| threadref *threadlist); |
| |
| typedef int (*rmt_thread_action) (threadref *ref, void *context); |
| |
| static int remote_threadlist_iterator (rmt_thread_action stepfunction, |
| void *context, int looplimit); |
| |
| static int remote_newthread_step (threadref *ref, void *context); |
| |
| |
| /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the |
| buffer we're allowed to write to. Returns |
| BUF+CHARACTERS_WRITTEN. */ |
| |
| static char * |
| write_ptid (char *buf, const char *endbuf, ptid_t ptid) |
| { |
| int pid, tid; |
| struct remote_state *rs = get_remote_state (); |
| |
| if (remote_multi_process_p (rs)) |
| { |
| pid = ptid_get_pid (ptid); |
| if (pid < 0) |
| buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid); |
| else |
| buf += xsnprintf (buf, endbuf - buf, "p%x.", pid); |
| } |
| tid = ptid_get_lwp (ptid); |
| if (tid < 0) |
| buf += xsnprintf (buf, endbuf - buf, "-%x", -tid); |
| else |
| buf += xsnprintf (buf, endbuf - buf, "%x", tid); |
| |
| return buf; |
| } |
| |
| /* Extract a PTID from BUF. If non-null, OBUF is set to the to one |
| passed the last parsed char. Returns null_ptid on error. */ |
| |
| static ptid_t |
| read_ptid (char *buf, char **obuf) |
| { |
| char *p = buf; |
| char *pp; |
| ULONGEST pid = 0, tid = 0; |
| |
| if (*p == 'p') |
| { |
| /* Multi-process ptid. */ |
| pp = unpack_varlen_hex (p + 1, &pid); |
| if (*pp != '.') |
| error (_("invalid remote ptid: %s"), p); |
| |
| p = pp; |
| pp = unpack_varlen_hex (p + 1, &tid); |
| if (obuf) |
| *obuf = pp; |
| return ptid_build (pid, tid, 0); |
| } |
| |
| /* No multi-process. Just a tid. */ |
| pp = unpack_varlen_hex (p, &tid); |
| |
| /* Return null_ptid when no thread id is found. */ |
| if (p == pp) |
| { |
| if (obuf) |
| *obuf = pp; |
| return null_ptid; |
| } |
| |
| /* Since the stub is not sending a process id, then default to |
| what's in inferior_ptid, unless it's null at this point. If so, |
| then since there's no way to know the pid of the reported |
| threads, use the magic number. */ |
| if (ptid_equal (inferior_ptid, null_ptid)) |
| pid = ptid_get_pid (magic_null_ptid); |
| else |
| pid = ptid_get_pid (inferior_ptid); |
| |
| if (obuf) |
| *obuf = pp; |
| return ptid_build (pid, tid, 0); |
| } |
| |
| static int |
| stubhex (int ch) |
| { |
| if (ch >= 'a' && ch <= 'f') |
| return ch - 'a' + 10; |
| if (ch >= '0' && ch <= '9') |
| return ch - '0'; |
| if (ch >= 'A' && ch <= 'F') |
| return ch - 'A' + 10; |
| return -1; |
| } |
| |
| static int |
| stub_unpack_int (char *buff, int fieldlength) |
| { |
| int nibble; |
| int retval = 0; |
| |
| while (fieldlength) |
| { |
| nibble = stubhex (*buff++); |
| retval |= nibble; |
| fieldlength--; |
| if (fieldlength) |
| retval = retval << 4; |
| } |
| return retval; |
| } |
| |
| static char * |
| unpack_nibble (char *buf, int *val) |
| { |
| *val = fromhex (*buf++); |
| return buf; |
| } |
| |
| static char * |
| unpack_byte (char *buf, int *value) |
| { |
| *value = stub_unpack_int (buf, 2); |
| return buf + 2; |
| } |
| |
| static char * |
| pack_int (char *buf, int value) |
| { |
| buf = pack_hex_byte (buf, (value >> 24) & 0xff); |
| buf = pack_hex_byte (buf, (value >> 16) & 0xff); |
| buf = pack_hex_byte (buf, (value >> 8) & 0x0ff); |
| buf = pack_hex_byte (buf, (value & 0xff)); |
| return buf; |
| } |
| |
| static char * |
| unpack_int (char *buf, int *value) |
| { |
| *value = stub_unpack_int (buf, 8); |
| return buf + 8; |
| } |
| |
| #if 0 /* Currently unused, uncomment when needed. */ |
| static char *pack_string (char *pkt, char *string); |
| |
| static char * |
| pack_string (char *pkt, char *string) |
| { |
| char ch; |
| int len; |
| |
| len = strlen (string); |
| if (len > 200) |
| len = 200; /* Bigger than most GDB packets, junk??? */ |
| pkt = pack_hex_byte (pkt, len); |
| while (len-- > 0) |
| { |
| ch = *string++; |
| if ((ch == '\0') || (ch == '#')) |
| ch = '*'; /* Protect encapsulation. */ |
| *pkt++ = ch; |
| } |
| return pkt; |
| } |
| #endif /* 0 (unused) */ |
| |
| static char * |
| unpack_string (char *src, char *dest, int length) |
| { |
| while (length--) |
| *dest++ = *src++; |
| *dest = '\0'; |
| return src; |
| } |
| |
| static char * |
| pack_threadid (char *pkt, threadref *id) |
| { |
| char *limit; |
| unsigned char *altid; |
| |
| altid = (unsigned char *) id; |
| limit = pkt + BUF_THREAD_ID_SIZE; |
| while (pkt < limit) |
| pkt = pack_hex_byte (pkt, *altid++); |
| return pkt; |
| } |
| |
| |
| static char * |
| unpack_threadid (char *inbuf, threadref *id) |
| { |
| char *altref; |
| char *limit = inbuf + BUF_THREAD_ID_SIZE; |
| int x, y; |
| |
| altref = (char *) id; |
| |
| while (inbuf < limit) |
| { |
| x = stubhex (*inbuf++); |
| y = stubhex (*inbuf++); |
| *altref++ = (x << 4) | y; |
| } |
| return inbuf; |
| } |
| |
| /* Externally, threadrefs are 64 bits but internally, they are still |
| ints. This is due to a mismatch of specifications. We would like |
| to use 64bit thread references internally. This is an adapter |
| function. */ |
| |
| void |
| int_to_threadref (threadref *id, int value) |
| { |
| unsigned char *scan; |
| |
| scan = (unsigned char *) id; |
| { |
| int i = 4; |
| while (i--) |
| *scan++ = 0; |
| } |
| *scan++ = (value >> 24) & 0xff; |
| *scan++ = (value >> 16) & 0xff; |
| *scan++ = (value >> 8) & 0xff; |
| *scan++ = (value & 0xff); |
| } |
| |
| static int |
| threadref_to_int (threadref *ref) |
| { |
| int i, value = 0; |
| unsigned char *scan; |
| |
| scan = *ref; |
| scan += 4; |
| i = 4; |
| while (i-- > 0) |
| value = (value << 8) | ((*scan++) & 0xff); |
| return value; |
| } |
| |
| static void |
| copy_threadref (threadref *dest, threadref *src) |
| { |
| int i; |
| unsigned char *csrc, *cdest; |
| |
| csrc = (unsigned char *) src; |
| cdest = (unsigned char *) dest; |
| i = 8; |
| while (i--) |
| *cdest++ = *csrc++; |
| } |
| |
| static int |
| threadmatch (threadref *dest, threadref *src) |
| { |
| /* Things are broken right now, so just assume we got a match. */ |
| #if 0 |
| unsigned char *srcp, *destp; |
| int i, result; |
| srcp = (char *) src; |
| destp = (char *) dest; |
| |
| result = 1; |
| while (i-- > 0) |
| result &= (*srcp++ == *destp++) ? 1 : 0; |
| return result; |
| #endif |
| return 1; |
| } |
| |
| /* |
| threadid:1, # always request threadid |
| context_exists:2, |
| display:4, |
| unique_name:8, |
| more_display:16 |
| */ |
| |
| /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */ |
| |
| static char * |
| pack_threadinfo_request (char *pkt, int mode, threadref *id) |
| { |
| *pkt++ = 'q'; /* Info Query */ |
| *pkt++ = 'P'; /* process or thread info */ |
| pkt = pack_int (pkt, mode); /* mode */ |
| pkt = pack_threadid (pkt, id); /* threadid */ |
| *pkt = '\0'; /* terminate */ |
| return pkt; |
| } |
| |
| /* These values tag the fields in a thread info response packet. */ |
| /* Tagging the fields allows us to request specific fields and to |
| add more fields as time goes by. */ |
| |
| #define TAG_THREADID 1 /* Echo the thread identifier. */ |
| #define TAG_EXISTS 2 /* Is this process defined enough to |
| fetch registers and its stack? */ |
| #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */ |
| #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */ |
| #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about |
| the process. */ |
| |
| static int |
| remote_unpack_thread_info_response (char *pkt, threadref *expectedref, |
| struct gdb_ext_thread_info *info) |
| { |
| struct remote_state *rs = get_remote_state (); |
| int mask, length; |
| int tag; |
| threadref ref; |
| char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */ |
| int retval = 1; |
| |
| /* info->threadid = 0; FIXME: implement zero_threadref. */ |
| info->active = 0; |
| info->display[0] = '\0'; |
| info->shortname[0] = '\0'; |
| info->more_display[0] = '\0'; |
| |
| /* Assume the characters indicating the packet type have been |
| stripped. */ |
| pkt = unpack_int (pkt, &mask); /* arg mask */ |
| pkt = unpack_threadid (pkt, &ref); |
| |
| if (mask == 0) |
| warning (_("Incomplete response to threadinfo request.")); |
| if (!threadmatch (&ref, expectedref)) |
| { /* This is an answer to a different request. */ |
| warning (_("ERROR RMT Thread info mismatch.")); |
| return 0; |
| } |
| copy_threadref (&info->threadid, &ref); |
| |
| /* Loop on tagged fields , try to bail if somthing goes wrong. */ |
| |
| /* Packets are terminated with nulls. */ |
| while ((pkt < limit) && mask && *pkt) |
| { |
| pkt = unpack_int (pkt, &tag); /* tag */ |
| pkt = unpack_byte (pkt, &length); /* length */ |
| if (!(tag & mask)) /* Tags out of synch with mask. */ |
| { |
| warning (_("ERROR RMT: threadinfo tag mismatch.")); |
| retval = 0; |
| break; |
| } |
| if (tag == TAG_THREADID) |
| { |
| if (length != 16) |
| { |
| warning (_("ERROR RMT: length of threadid is not 16.")); |
| retval = 0; |
| break; |
| } |
| pkt = unpack_threadid (pkt, &ref); |
| mask = mask & ~TAG_THREADID; |
| continue; |
| } |
| if (tag == TAG_EXISTS) |
| { |
| info->active = stub_unpack_int (pkt, length); |
| pkt += length; |
| mask = mask & ~(TAG_EXISTS); |
| if (length > 8) |
| { |
| warning (_("ERROR RMT: 'exists' length too long.")); |
| retval = 0; |
| break; |
| } |
| continue; |
| } |
| if (tag == TAG_THREADNAME) |
| { |
| pkt = unpack_string (pkt, &info->shortname[0], length); |
| mask = mask & ~TAG_THREADNAME; |
| continue; |
| } |
| if (tag == TAG_DISPLAY) |
| { |
| pkt = unpack_string (pkt, &info->display[0], length); |
| mask = mask & ~TAG_DISPLAY; |
| continue; |
| } |
| if (tag == TAG_MOREDISPLAY) |
| { |
| pkt = unpack_string (pkt, &info->more_display[0], length); |
| mask = mask & ~TAG_MOREDISPLAY; |
| continue; |
| } |
| warning (_("ERROR RMT: unknown thread info tag.")); |
| break; /* Not a tag we know about. */ |
| } |
| return retval; |
| } |
| |
| static int |
| remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */ |
| struct gdb_ext_thread_info *info) |
| { |
| struct remote_state *rs = get_remote_state (); |
| int result; |
| |
| pack_threadinfo_request (rs->buf, fieldset, threadid); |
| putpkt (rs->buf); |
| getpkt (&rs->buf, &rs->buf_size, 0); |
| |
| if (rs->buf[0] == '\0') |
| return 0; |
| |
| result = remote_unpack_thread_info_response (rs->buf + 2, |
| threadid, info); |
| return result; |
| } |
| |
| /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */ |
| |
| static char * |
| pack_threadlist_request (char *pkt, int startflag, int threadcount, |
| threadref *nextthread) |
| { |
| *pkt++ = 'q'; /* info query packet */ |
| *pkt++ = 'L'; /* Process LIST or threadLIST request */ |
| pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */ |
| pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */ |
| pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */ |
| *pkt = '\0'; |
| return pkt; |
| } |
| |
| /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */ |
| |
| static int |
| parse_threadlist_response (char *pkt, int result_limit, |
| threadref *original_echo, threadref *resultlist, |
| int *doneflag) |
| { |
| struct remote_state *rs = get_remote_state (); |
| char *limit; |
| int count, resultcount, done; |
| |
| resultcount = 0; |
| /* Assume the 'q' and 'M chars have been stripped. */ |
| limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE); |
| /* done parse past here */ |
| pkt = unpack_byte (pkt, &count); /* count field */ |
| pkt = unpack_nibble (pkt, &done); |
| /* The first threadid is the argument threadid. */ |
| pkt = unpack_threadid (pkt, original_echo); /* should match query packet */ |
| while ((count-- > 0) && (pkt < limit)) |
| { |
| pkt = unpack_threadid (pkt, resultlist++); |
| if (resultcount++ >= result_limit) |
| break; |
| } |
| if (doneflag) |
| *doneflag = done; |
| return resultcount; |
| } |
| |
| /* Fetch the next batch of threads from the remote. Returns -1 if the |
| qL packet is not supported, 0 on error and 1 on success. */ |
| |
| static int |
| remote_get_threadlist (int startflag, threadref *nextthread, int result_limit, |
| int *done, int *result_count, threadref *threadlist) |
| { |
| struct remote_state *rs = get_remote_state (); |
| int result = 1; |
| |
| /* Trancate result limit to be smaller than the packet size. */ |
| if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) |
| >= get_remote_packet_size ()) |
| result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2; |
| |
| pack_threadlist_request (rs->buf, startflag, result_limit, nextthread); |
| putpkt (rs->buf); |
| getpkt (&rs->buf, &rs->buf_size, 0); |
| if (*rs->buf == '\0') |
| { |
| /* Packet not supported. */ |
| return -1; |
| } |
| |
| *result_count = |
| parse_threadlist_response (rs->buf + 2, result_limit, |
| &rs->echo_nextthread, threadlist, done); |
| |
| if (!threadmatch (&rs->echo_nextthread, nextthread)) |
| { |
| /* FIXME: This is a good reason to drop the packet. */ |
| /* Possably, there is a duplicate response. */ |
| /* Possabilities : |
| retransmit immediatly - race conditions |
| retransmit after timeout - yes |
| exit |
| wait for packet, then exit |
| */ |
| warning (_("HMM: threadlist did not echo arg thread, dropping it.")); |
| return 0; /* I choose simply exiting. */ |
| } |
| if (*result_count <= 0) |
| { |
| if (*done != 1) |
| { |
| warning (_("RMT ERROR : failed to get remote thread list.")); |
| result = 0; |
| } |
| return result; /* break; */ |
| } |
| if (*result_count > result_limit) |
| { |
| *result_count = 0; |
| warning (_("RMT ERROR: threadlist response longer than requested.")); |
| return 0; |
| } |
| return result; |
| } |
| |
| /* Fetch the list of remote threads, with the qL packet, and call |
| STEPFUNCTION for each thread found. Stops iterating and returns 1 |
| if STEPFUNCTION returns true. Stops iterating and returns 0 if the |
| STEPFUNCTION returns false. If the packet is not supported, |
| returns -1. */ |
| |
| static int |
| remote_threadlist_iterator (rmt_thread_action stepfunction, void *context, |
| int looplimit) |
| { |
| struct remote_state *rs = get_remote_state (); |
| int done, i, result_count; |
| int startflag = 1; |
| int result = 1; |
| int loopcount = 0; |
| |
| done = 0; |
| while (!done) |
| { |
| if (loopcount++ > looplimit) |
| { |
| result = 0; |
| warning (_("Remote fetch threadlist -infinite loop-.")); |
| break; |
| } |
| result = remote_get_threadlist (startflag, &rs->nextthread, |
| MAXTHREADLISTRESULTS, |
| &done, &result_count, |
| rs->resultthreadlist); |
| if (result <= 0) |
| break; |
| /* Clear for later iterations. */ |
| startflag = 0; |
| /* Setup to resume next batch of thread references, set nextthread. */ |
| if (result_count >= 1) |
| copy_threadref (&rs->nextthread, |
| &rs->resultthreadlist[result_count - 1]); |
| i = 0; |
| while (result_count--) |
| { |
| if (!(*stepfunction) (&rs->resultthreadlist[i++], context)) |
| { |
| result = 0; |
| break; |
| } |
| } |
| } |
| return result; |
| } |
| |
| /* A thread found on the remote target. */ |
| |
| typedef struct thread_item |
| { |
| /* The thread's PTID. */ |
| ptid_t ptid; |
| |
| /* The thread's extra info. May be NULL. */ |
| char *extra; |
| |
| /* The thread's name. May be NULL. */ |
| char *name; |
| |
| /* The core the thread was running on. -1 if not known. */ |
| int core; |
| } thread_item_t; |
| DEF_VEC_O(thread_item_t); |
| |
| /* Context passed around to the various methods listing remote |
| threads. As new threads are found, they're added to the ITEMS |
| vector. */ |
| |
| struct threads_listing_context |
| { |
| /* The threads found on the remote target. */ |
| VEC (thread_item_t) *items; |
| }; |
| |