xref: /dflybsd-src/contrib/gdb-7/gdb/remote.c (revision accba8701d70140ad7c156331c3ad42b80c0570d)
15796c8dcSSimon Schubert /* Remote target communications for serial-line targets in custom GDB protocol
25796c8dcSSimon Schubert 
3ef5ccd6cSJohn Marino    Copyright (C) 1988-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert /* See the GDB User Guide for details of the GDB remote protocol.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "gdb_string.h"
245796c8dcSSimon Schubert #include <ctype.h>
255796c8dcSSimon Schubert #include <fcntl.h>
265796c8dcSSimon Schubert #include "inferior.h"
275796c8dcSSimon Schubert #include "bfd.h"
285796c8dcSSimon Schubert #include "symfile.h"
295796c8dcSSimon Schubert #include "exceptions.h"
305796c8dcSSimon Schubert #include "target.h"
315796c8dcSSimon Schubert /*#include "terminal.h" */
325796c8dcSSimon Schubert #include "gdbcmd.h"
335796c8dcSSimon Schubert #include "objfiles.h"
345796c8dcSSimon Schubert #include "gdb-stabs.h"
355796c8dcSSimon Schubert #include "gdbthread.h"
365796c8dcSSimon Schubert #include "remote.h"
37ef5ccd6cSJohn Marino #include "remote-notif.h"
385796c8dcSSimon Schubert #include "regcache.h"
395796c8dcSSimon Schubert #include "value.h"
405796c8dcSSimon Schubert #include "gdb_assert.h"
415796c8dcSSimon Schubert #include "observer.h"
425796c8dcSSimon Schubert #include "solib.h"
435796c8dcSSimon Schubert #include "cli/cli-decode.h"
445796c8dcSSimon Schubert #include "cli/cli-setshow.h"
455796c8dcSSimon Schubert #include "target-descriptions.h"
46ef5ccd6cSJohn Marino #include "gdb_bfd.h"
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert #include <ctype.h>
495796c8dcSSimon Schubert #include <sys/time.h>
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert #include "event-loop.h"
525796c8dcSSimon Schubert #include "event-top.h"
535796c8dcSSimon Schubert #include "inf-loop.h"
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert #include <signal.h>
565796c8dcSSimon Schubert #include "serial.h"
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert #include "gdbcore.h" /* for exec_bfd */
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert #include "remote-fileio.h"
615796c8dcSSimon Schubert #include "gdb/fileio.h"
625796c8dcSSimon Schubert #include "gdb_stat.h"
63cf7f2e2dSJohn Marino #include "xml-support.h"
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert #include "memory-map.h"
665796c8dcSSimon Schubert 
67cf7f2e2dSJohn Marino #include "tracepoint.h"
68cf7f2e2dSJohn Marino #include "ax.h"
69cf7f2e2dSJohn Marino #include "ax-gdb.h"
70ef5ccd6cSJohn Marino #include "agent.h"
71ef5ccd6cSJohn Marino #include "btrace.h"
72cf7f2e2dSJohn Marino 
73c50c785cSJohn Marino /* Temp hacks for tracepoint encoding migration.  */
74cf7f2e2dSJohn Marino static char *target_buf;
75cf7f2e2dSJohn Marino static long target_buf_size;
76cf7f2e2dSJohn Marino 
775796c8dcSSimon Schubert /* The size to align memory write packets, when practical.  The protocol
785796c8dcSSimon Schubert    does not guarantee any alignment, and gdb will generate short
795796c8dcSSimon Schubert    writes and unaligned writes, but even as a best-effort attempt this
805796c8dcSSimon Schubert    can improve bulk transfers.  For instance, if a write is misaligned
815796c8dcSSimon Schubert    relative to the target's data bus, the stub may need to make an extra
825796c8dcSSimon Schubert    round trip fetching data from the target.  This doesn't make a
835796c8dcSSimon Schubert    huge difference, but it's easy to do, so we try to be helpful.
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert    The alignment chosen is arbitrary; usually data bus width is
865796c8dcSSimon Schubert    important here, not the possibly larger cache line size.  */
875796c8dcSSimon Schubert enum { REMOTE_ALIGN_WRITES = 16 };
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert /* Prototypes for local functions.  */
905796c8dcSSimon Schubert static void cleanup_sigint_signal_handler (void *dummy);
915796c8dcSSimon Schubert static void initialize_sigint_signal_handler (void);
925796c8dcSSimon Schubert static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
935796c8dcSSimon Schubert static int getpkt_or_notif_sane (char **buf, long *sizeof_buf,
94ef5ccd6cSJohn Marino 				 int forever, int *is_notif);
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert static void handle_remote_sigint (int);
975796c8dcSSimon Schubert static void handle_remote_sigint_twice (int);
985796c8dcSSimon Schubert static void async_remote_interrupt (gdb_client_data);
995796c8dcSSimon Schubert void async_remote_interrupt_twice (gdb_client_data);
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert static void remote_files_info (struct target_ops *ignore);
1025796c8dcSSimon Schubert 
1035796c8dcSSimon Schubert static void remote_prepare_to_store (struct regcache *regcache);
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert static void remote_open (char *name, int from_tty);
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert static void extended_remote_open (char *name, int from_tty);
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert static void remote_open_1 (char *, int, struct target_ops *, int extended_p);
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert static void remote_close (int quitting);
1125796c8dcSSimon Schubert 
1135796c8dcSSimon Schubert static void remote_mourn (struct target_ops *ops);
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert static void extended_remote_restart (void);
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert static void extended_remote_mourn (struct target_ops *);
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert static void remote_mourn_1 (struct target_ops *);
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert static void remote_send (char **buf, long *sizeof_buf_p);
1225796c8dcSSimon Schubert 
1235796c8dcSSimon Schubert static int readchar (int timeout);
1245796c8dcSSimon Schubert 
125ef5ccd6cSJohn Marino static void remote_serial_write (const char *str, int len);
126ef5ccd6cSJohn Marino 
1275796c8dcSSimon Schubert static void remote_kill (struct target_ops *ops);
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert static int tohex (int nib);
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert static int remote_can_async_p (void);
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert static int remote_is_async_p (void);
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert static void remote_async (void (*callback) (enum inferior_event_type event_type,
1365796c8dcSSimon Schubert 					    void *context), void *context);
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert static void remote_detach (struct target_ops *ops, char *args, int from_tty);
1395796c8dcSSimon Schubert 
1405796c8dcSSimon Schubert static void remote_interrupt (int signo);
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert static void remote_interrupt_twice (int signo);
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert static void interrupt_query (void);
1455796c8dcSSimon Schubert 
1465796c8dcSSimon Schubert static void set_general_thread (struct ptid ptid);
1475796c8dcSSimon Schubert static void set_continue_thread (struct ptid ptid);
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert static void get_offsets (void);
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert static void skip_frame (void);
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert static long read_frame (char **buf_p, long *sizeof_buf);
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert static int hexnumlen (ULONGEST num);
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert static void init_remote_ops (void);
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert static void init_extended_remote_ops (void);
1605796c8dcSSimon Schubert 
1615796c8dcSSimon Schubert static void remote_stop (ptid_t);
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert static int ishex (int ch, int *val);
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert static int stubhex (int ch);
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert static int hexnumstr (char *, ULONGEST);
1685796c8dcSSimon Schubert 
1695796c8dcSSimon Schubert static int hexnumnstr (char *, ULONGEST, int);
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert static CORE_ADDR remote_address_masked (CORE_ADDR);
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert static void print_packet (char *);
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert static void compare_sections_command (char *, int);
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert static void packet_command (char *, int);
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert static int stub_unpack_int (char *buff, int fieldlength);
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert static ptid_t remote_current_thread (ptid_t oldptid);
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert static void remote_find_new_threads (void);
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert static void record_currthread (ptid_t currthread);
1865796c8dcSSimon Schubert 
1875796c8dcSSimon Schubert static int fromhex (int a);
1885796c8dcSSimon Schubert 
1895796c8dcSSimon Schubert static int putpkt_binary (char *buf, int cnt);
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert static void check_binary_download (CORE_ADDR addr);
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert struct packet_config;
1945796c8dcSSimon Schubert 
1955796c8dcSSimon Schubert static void show_packet_config_cmd (struct packet_config *config);
1965796c8dcSSimon Schubert 
1975796c8dcSSimon Schubert static void update_packet_config (struct packet_config *config);
1985796c8dcSSimon Schubert 
1995796c8dcSSimon Schubert static void set_remote_protocol_packet_cmd (char *args, int from_tty,
2005796c8dcSSimon Schubert 					    struct cmd_list_element *c);
2015796c8dcSSimon Schubert 
2025796c8dcSSimon Schubert static void show_remote_protocol_packet_cmd (struct ui_file *file,
2035796c8dcSSimon Schubert 					     int from_tty,
2045796c8dcSSimon Schubert 					     struct cmd_list_element *c,
2055796c8dcSSimon Schubert 					     const char *value);
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
2085796c8dcSSimon Schubert static ptid_t read_ptid (char *buf, char **obuf);
2095796c8dcSSimon Schubert 
210cf7f2e2dSJohn Marino static void remote_set_permissions (void);
211cf7f2e2dSJohn Marino 
212cf7f2e2dSJohn Marino struct remote_state;
213cf7f2e2dSJohn Marino static int remote_get_trace_status (struct trace_status *ts);
214cf7f2e2dSJohn Marino 
215cf7f2e2dSJohn Marino static int remote_upload_tracepoints (struct uploaded_tp **utpp);
216cf7f2e2dSJohn Marino 
217cf7f2e2dSJohn Marino static int remote_upload_trace_state_variables (struct uploaded_tsv **utsvp);
218cf7f2e2dSJohn Marino 
2195796c8dcSSimon Schubert static void remote_query_supported (void);
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert static void remote_check_symbols (struct objfile *objfile);
2225796c8dcSSimon Schubert 
2235796c8dcSSimon Schubert void _initialize_remote (void);
2245796c8dcSSimon Schubert 
2255796c8dcSSimon Schubert struct stop_reply;
2265796c8dcSSimon Schubert static void stop_reply_xfree (struct stop_reply *);
227ef5ccd6cSJohn Marino static void remote_parse_stop_reply (char *, struct stop_reply *);
2285796c8dcSSimon Schubert static void push_stop_reply (struct stop_reply *);
229ef5ccd6cSJohn Marino static void discard_pending_stop_replies (struct inferior *);
2305796c8dcSSimon Schubert static int peek_stop_reply (ptid_t ptid);
2315796c8dcSSimon Schubert 
2325796c8dcSSimon Schubert static void remote_async_inferior_event_handler (gdb_client_data);
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert static void remote_terminal_ours (void);
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert static int remote_read_description_p (struct target_ops *target);
2375796c8dcSSimon Schubert 
238cf7f2e2dSJohn Marino static void remote_console_output (char *msg);
239cf7f2e2dSJohn Marino 
240ef5ccd6cSJohn Marino static int remote_supports_cond_breakpoints (void);
2415796c8dcSSimon Schubert 
242ef5ccd6cSJohn Marino static int remote_can_run_breakpoint_commands (void);
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert /* For "remote".  */
2455796c8dcSSimon Schubert 
2465796c8dcSSimon Schubert static struct cmd_list_element *remote_cmdlist;
2475796c8dcSSimon Schubert 
2485796c8dcSSimon Schubert /* For "set remote" and "show remote".  */
2495796c8dcSSimon Schubert 
2505796c8dcSSimon Schubert static struct cmd_list_element *remote_set_cmdlist;
2515796c8dcSSimon Schubert static struct cmd_list_element *remote_show_cmdlist;
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert /* Description of the remote protocol state for the currently
2545796c8dcSSimon Schubert    connected target.  This is per-target state, and independent of the
2555796c8dcSSimon Schubert    selected architecture.  */
2565796c8dcSSimon Schubert 
2575796c8dcSSimon Schubert struct remote_state
2585796c8dcSSimon Schubert {
2595796c8dcSSimon Schubert   /* A buffer to use for incoming packets, and its current size.  The
2605796c8dcSSimon Schubert      buffer is grown dynamically for larger incoming packets.
2615796c8dcSSimon Schubert      Outgoing packets may also be constructed in this buffer.
2625796c8dcSSimon Schubert      BUF_SIZE is always at least REMOTE_PACKET_SIZE;
2635796c8dcSSimon Schubert      REMOTE_PACKET_SIZE should be used to limit the length of outgoing
2645796c8dcSSimon Schubert      packets.  */
2655796c8dcSSimon Schubert   char *buf;
2665796c8dcSSimon Schubert   long buf_size;
2675796c8dcSSimon Schubert 
268a45ae5f8SJohn Marino   /* True if we're going through initial connection setup (finding out
269a45ae5f8SJohn Marino      about the remote side's threads, relocating symbols, etc.).  */
270a45ae5f8SJohn Marino   int starting_up;
271a45ae5f8SJohn Marino 
2725796c8dcSSimon Schubert   /* If we negotiated packet size explicitly (and thus can bypass
2735796c8dcSSimon Schubert      heuristics for the largest packet size that will not overflow
2745796c8dcSSimon Schubert      a buffer in the stub), this will be set to that packet size.
2755796c8dcSSimon Schubert      Otherwise zero, meaning to use the guessed size.  */
2765796c8dcSSimon Schubert   long explicit_packet_size;
2775796c8dcSSimon Schubert 
2785796c8dcSSimon Schubert   /* remote_wait is normally called when the target is running and
2795796c8dcSSimon Schubert      waits for a stop reply packet.  But sometimes we need to call it
2805796c8dcSSimon Schubert      when the target is already stopped.  We can send a "?" packet
2815796c8dcSSimon Schubert      and have remote_wait read the response.  Or, if we already have
2825796c8dcSSimon Schubert      the response, we can stash it in BUF and tell remote_wait to
2835796c8dcSSimon Schubert      skip calling getpkt.  This flag is set when BUF contains a
2845796c8dcSSimon Schubert      stop reply packet and the target is not waiting.  */
2855796c8dcSSimon Schubert   int cached_wait_status;
2865796c8dcSSimon Schubert 
2875796c8dcSSimon Schubert   /* True, if in no ack mode.  That is, neither GDB nor the stub will
2885796c8dcSSimon Schubert      expect acks from each other.  The connection is assumed to be
2895796c8dcSSimon Schubert      reliable.  */
2905796c8dcSSimon Schubert   int noack_mode;
2915796c8dcSSimon Schubert 
2925796c8dcSSimon Schubert   /* True if we're connected in extended remote mode.  */
2935796c8dcSSimon Schubert   int extended;
2945796c8dcSSimon Schubert 
2955796c8dcSSimon Schubert   /* True if the stub reported support for multi-process
2965796c8dcSSimon Schubert      extensions.  */
2975796c8dcSSimon Schubert   int multi_process_aware;
2985796c8dcSSimon Schubert 
2995796c8dcSSimon Schubert   /* True if we resumed the target and we're waiting for the target to
3005796c8dcSSimon Schubert      stop.  In the mean time, we can't start another command/query.
3015796c8dcSSimon Schubert      The remote server wouldn't be ready to process it, so we'd
3025796c8dcSSimon Schubert      timeout waiting for a reply that would never come and eventually
3035796c8dcSSimon Schubert      we'd close the connection.  This can happen in asynchronous mode
3045796c8dcSSimon Schubert      because we allow GDB commands while the target is running.  */
3055796c8dcSSimon Schubert   int waiting_for_stop_reply;
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert   /* True if the stub reports support for non-stop mode.  */
3085796c8dcSSimon Schubert   int non_stop_aware;
3095796c8dcSSimon Schubert 
3105796c8dcSSimon Schubert   /* True if the stub reports support for vCont;t.  */
3115796c8dcSSimon Schubert   int support_vCont_t;
3125796c8dcSSimon Schubert 
3135796c8dcSSimon Schubert   /* True if the stub reports support for conditional tracepoints.  */
3145796c8dcSSimon Schubert   int cond_tracepoints;
315cf7f2e2dSJohn Marino 
316ef5ccd6cSJohn Marino   /* True if the stub reports support for target-side breakpoint
317ef5ccd6cSJohn Marino      conditions.  */
318ef5ccd6cSJohn Marino   int cond_breakpoints;
319ef5ccd6cSJohn Marino 
320ef5ccd6cSJohn Marino   /* True if the stub reports support for target-side breakpoint
321ef5ccd6cSJohn Marino      commands.  */
322ef5ccd6cSJohn Marino   int breakpoint_commands;
323ef5ccd6cSJohn Marino 
324cf7f2e2dSJohn Marino   /* True if the stub reports support for fast tracepoints.  */
325cf7f2e2dSJohn Marino   int fast_tracepoints;
326cf7f2e2dSJohn Marino 
327cf7f2e2dSJohn Marino   /* True if the stub reports support for static tracepoints.  */
328cf7f2e2dSJohn Marino   int static_tracepoints;
329cf7f2e2dSJohn Marino 
330a45ae5f8SJohn Marino   /* True if the stub reports support for installing tracepoint while
331a45ae5f8SJohn Marino      tracing.  */
332a45ae5f8SJohn Marino   int install_in_trace;
333a45ae5f8SJohn Marino 
334cf7f2e2dSJohn Marino   /* True if the stub can continue running a trace while GDB is
335cf7f2e2dSJohn Marino      disconnected.  */
336cf7f2e2dSJohn Marino   int disconnected_tracing;
337cf7f2e2dSJohn Marino 
338a45ae5f8SJohn Marino   /* True if the stub reports support for enabling and disabling
339a45ae5f8SJohn Marino      tracepoints while a trace experiment is running.  */
340a45ae5f8SJohn Marino   int enable_disable_tracepoints;
341a45ae5f8SJohn Marino 
342a45ae5f8SJohn Marino   /* True if the stub can collect strings using tracenz bytecode.  */
343a45ae5f8SJohn Marino   int string_tracing;
344a45ae5f8SJohn Marino 
345cf7f2e2dSJohn Marino   /* Nonzero if the user has pressed Ctrl-C, but the target hasn't
346cf7f2e2dSJohn Marino      responded to that.  */
347cf7f2e2dSJohn Marino   int ctrlc_pending_p;
3485796c8dcSSimon Schubert };
3495796c8dcSSimon Schubert 
350cf7f2e2dSJohn Marino /* Private data that we'll store in (struct thread_info)->private.  */
351cf7f2e2dSJohn Marino struct private_thread_info
352cf7f2e2dSJohn Marino {
353cf7f2e2dSJohn Marino   char *extra;
354cf7f2e2dSJohn Marino   int core;
355cf7f2e2dSJohn Marino };
356cf7f2e2dSJohn Marino 
357cf7f2e2dSJohn Marino static void
free_private_thread_info(struct private_thread_info * info)358cf7f2e2dSJohn Marino free_private_thread_info (struct private_thread_info *info)
359cf7f2e2dSJohn Marino {
360cf7f2e2dSJohn Marino   xfree (info->extra);
361cf7f2e2dSJohn Marino   xfree (info);
362cf7f2e2dSJohn Marino }
363cf7f2e2dSJohn Marino 
3645796c8dcSSimon Schubert /* Returns true if the multi-process extensions are in effect.  */
3655796c8dcSSimon Schubert static int
remote_multi_process_p(struct remote_state * rs)3665796c8dcSSimon Schubert remote_multi_process_p (struct remote_state *rs)
3675796c8dcSSimon Schubert {
368ef5ccd6cSJohn Marino   return rs->multi_process_aware;
3695796c8dcSSimon Schubert }
3705796c8dcSSimon Schubert 
3715796c8dcSSimon Schubert /* This data could be associated with a target, but we do not always
3725796c8dcSSimon Schubert    have access to the current target when we need it, so for now it is
3735796c8dcSSimon Schubert    static.  This will be fine for as long as only one target is in use
3745796c8dcSSimon Schubert    at a time.  */
3755796c8dcSSimon Schubert static struct remote_state remote_state;
3765796c8dcSSimon Schubert 
3775796c8dcSSimon Schubert static struct remote_state *
get_remote_state_raw(void)3785796c8dcSSimon Schubert get_remote_state_raw (void)
3795796c8dcSSimon Schubert {
3805796c8dcSSimon Schubert   return &remote_state;
3815796c8dcSSimon Schubert }
3825796c8dcSSimon Schubert 
3835796c8dcSSimon Schubert /* Description of the remote protocol for a given architecture.  */
3845796c8dcSSimon Schubert 
3855796c8dcSSimon Schubert struct packet_reg
3865796c8dcSSimon Schubert {
3875796c8dcSSimon Schubert   long offset; /* Offset into G packet.  */
3885796c8dcSSimon Schubert   long regnum; /* GDB's internal register number.  */
3895796c8dcSSimon Schubert   LONGEST pnum; /* Remote protocol register number.  */
3905796c8dcSSimon Schubert   int in_g_packet; /* Always part of G packet.  */
391ef5ccd6cSJohn Marino   /* long size in bytes;  == register_size (target_gdbarch (), regnum);
3925796c8dcSSimon Schubert      at present.  */
393ef5ccd6cSJohn Marino   /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
3945796c8dcSSimon Schubert      at present.  */
3955796c8dcSSimon Schubert };
3965796c8dcSSimon Schubert 
3975796c8dcSSimon Schubert struct remote_arch_state
3985796c8dcSSimon Schubert {
3995796c8dcSSimon Schubert   /* Description of the remote protocol registers.  */
4005796c8dcSSimon Schubert   long sizeof_g_packet;
4015796c8dcSSimon Schubert 
4025796c8dcSSimon Schubert   /* Description of the remote protocol registers indexed by REGNUM
4035796c8dcSSimon Schubert      (making an array gdbarch_num_regs in size).  */
4045796c8dcSSimon Schubert   struct packet_reg *regs;
4055796c8dcSSimon Schubert 
4065796c8dcSSimon Schubert   /* This is the size (in chars) of the first response to the ``g''
4075796c8dcSSimon Schubert      packet.  It is used as a heuristic when determining the maximum
4085796c8dcSSimon Schubert      size of memory-read and memory-write packets.  A target will
4095796c8dcSSimon Schubert      typically only reserve a buffer large enough to hold the ``g''
4105796c8dcSSimon Schubert      packet.  The size does not include packet overhead (headers and
4115796c8dcSSimon Schubert      trailers).  */
4125796c8dcSSimon Schubert   long actual_register_packet_size;
4135796c8dcSSimon Schubert 
4145796c8dcSSimon Schubert   /* This is the maximum size (in chars) of a non read/write packet.
4155796c8dcSSimon Schubert      It is also used as a cap on the size of read/write packets.  */
4165796c8dcSSimon Schubert   long remote_packet_size;
4175796c8dcSSimon Schubert };
4185796c8dcSSimon Schubert 
419cf7f2e2dSJohn Marino long sizeof_pkt = 2000;
420cf7f2e2dSJohn Marino 
421cf7f2e2dSJohn Marino /* Utility: generate error from an incoming stub packet.  */
422cf7f2e2dSJohn Marino static void
trace_error(char * buf)423cf7f2e2dSJohn Marino trace_error (char *buf)
424cf7f2e2dSJohn Marino {
425cf7f2e2dSJohn Marino   if (*buf++ != 'E')
426cf7f2e2dSJohn Marino     return;			/* not an error msg */
427cf7f2e2dSJohn Marino   switch (*buf)
428cf7f2e2dSJohn Marino     {
429cf7f2e2dSJohn Marino     case '1':			/* malformed packet error */
430cf7f2e2dSJohn Marino       if (*++buf == '0')	/*   general case: */
431cf7f2e2dSJohn Marino 	error (_("remote.c: error in outgoing packet."));
432cf7f2e2dSJohn Marino       else
433cf7f2e2dSJohn Marino 	error (_("remote.c: error in outgoing packet at field #%ld."),
434cf7f2e2dSJohn Marino 	       strtol (buf, NULL, 16));
435cf7f2e2dSJohn Marino     default:
436cf7f2e2dSJohn Marino       error (_("Target returns error code '%s'."), buf);
437cf7f2e2dSJohn Marino     }
438cf7f2e2dSJohn Marino }
439cf7f2e2dSJohn Marino 
440cf7f2e2dSJohn Marino /* Utility: wait for reply from stub, while accepting "O" packets.  */
441cf7f2e2dSJohn Marino static char *
remote_get_noisy_reply(char ** buf_p,long * sizeof_buf)442cf7f2e2dSJohn Marino remote_get_noisy_reply (char **buf_p,
443cf7f2e2dSJohn Marino 			long *sizeof_buf)
444cf7f2e2dSJohn Marino {
445cf7f2e2dSJohn Marino   do				/* Loop on reply from remote stub.  */
446cf7f2e2dSJohn Marino     {
447cf7f2e2dSJohn Marino       char *buf;
448cf7f2e2dSJohn Marino 
449c50c785cSJohn Marino       QUIT;			/* Allow user to bail out with ^C.  */
450cf7f2e2dSJohn Marino       getpkt (buf_p, sizeof_buf, 0);
451cf7f2e2dSJohn Marino       buf = *buf_p;
452cf7f2e2dSJohn Marino       if (buf[0] == 'E')
453cf7f2e2dSJohn Marino 	trace_error (buf);
454cf7f2e2dSJohn Marino       else if (strncmp (buf, "qRelocInsn:", strlen ("qRelocInsn:")) == 0)
455cf7f2e2dSJohn Marino 	{
456cf7f2e2dSJohn Marino 	  ULONGEST ul;
457cf7f2e2dSJohn Marino 	  CORE_ADDR from, to, org_to;
458cf7f2e2dSJohn Marino 	  char *p, *pp;
459cf7f2e2dSJohn Marino 	  int adjusted_size = 0;
460cf7f2e2dSJohn Marino 	  volatile struct gdb_exception ex;
461cf7f2e2dSJohn Marino 
462cf7f2e2dSJohn Marino 	  p = buf + strlen ("qRelocInsn:");
463cf7f2e2dSJohn Marino 	  pp = unpack_varlen_hex (p, &ul);
464cf7f2e2dSJohn Marino 	  if (*pp != ';')
465cf7f2e2dSJohn Marino 	    error (_("invalid qRelocInsn packet: %s"), buf);
466cf7f2e2dSJohn Marino 	  from = ul;
467cf7f2e2dSJohn Marino 
468cf7f2e2dSJohn Marino 	  p = pp + 1;
469c50c785cSJohn Marino 	  unpack_varlen_hex (p, &ul);
470cf7f2e2dSJohn Marino 	  to = ul;
471cf7f2e2dSJohn Marino 
472cf7f2e2dSJohn Marino 	  org_to = to;
473cf7f2e2dSJohn Marino 
474cf7f2e2dSJohn Marino 	  TRY_CATCH (ex, RETURN_MASK_ALL)
475cf7f2e2dSJohn Marino 	    {
476ef5ccd6cSJohn Marino 	      gdbarch_relocate_instruction (target_gdbarch (), &to, from);
477cf7f2e2dSJohn Marino 	    }
478cf7f2e2dSJohn Marino 	  if (ex.reason >= 0)
479cf7f2e2dSJohn Marino 	    {
480cf7f2e2dSJohn Marino 	      adjusted_size = to - org_to;
481cf7f2e2dSJohn Marino 
482ef5ccd6cSJohn Marino 	      xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size);
483cf7f2e2dSJohn Marino 	      putpkt (buf);
484cf7f2e2dSJohn Marino 	    }
485cf7f2e2dSJohn Marino 	  else if (ex.reason < 0 && ex.error == MEMORY_ERROR)
486cf7f2e2dSJohn Marino 	    {
487cf7f2e2dSJohn Marino 	      /* Propagate memory errors silently back to the target.
488cf7f2e2dSJohn Marino 		 The stub may have limited the range of addresses we
489cf7f2e2dSJohn Marino 		 can write to, for example.  */
490cf7f2e2dSJohn Marino 	      putpkt ("E01");
491cf7f2e2dSJohn Marino 	    }
492cf7f2e2dSJohn Marino 	  else
493cf7f2e2dSJohn Marino 	    {
494cf7f2e2dSJohn Marino 	      /* Something unexpectedly bad happened.  Be verbose so
495cf7f2e2dSJohn Marino 		 we can tell what, and propagate the error back to the
496cf7f2e2dSJohn Marino 		 stub, so it doesn't get stuck waiting for a
497cf7f2e2dSJohn Marino 		 response.  */
498cf7f2e2dSJohn Marino 	      exception_fprintf (gdb_stderr, ex,
499cf7f2e2dSJohn Marino 				 _("warning: relocating instruction: "));
500cf7f2e2dSJohn Marino 	      putpkt ("E01");
501cf7f2e2dSJohn Marino 	    }
502cf7f2e2dSJohn Marino 	}
503cf7f2e2dSJohn Marino       else if (buf[0] == 'O' && buf[1] != 'K')
504cf7f2e2dSJohn Marino 	remote_console_output (buf + 1);	/* 'O' message from stub */
505cf7f2e2dSJohn Marino       else
506c50c785cSJohn Marino 	return buf;		/* Here's the actual reply.  */
507cf7f2e2dSJohn Marino     }
508cf7f2e2dSJohn Marino   while (1);
509cf7f2e2dSJohn Marino }
5105796c8dcSSimon Schubert 
5115796c8dcSSimon Schubert /* Handle for retreving the remote protocol data from gdbarch.  */
5125796c8dcSSimon Schubert static struct gdbarch_data *remote_gdbarch_data_handle;
5135796c8dcSSimon Schubert 
5145796c8dcSSimon Schubert static struct remote_arch_state *
get_remote_arch_state(void)5155796c8dcSSimon Schubert get_remote_arch_state (void)
5165796c8dcSSimon Schubert {
517ef5ccd6cSJohn Marino   return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle);
5185796c8dcSSimon Schubert }
5195796c8dcSSimon Schubert 
5205796c8dcSSimon Schubert /* Fetch the global remote target state.  */
5215796c8dcSSimon Schubert 
5225796c8dcSSimon Schubert static struct remote_state *
get_remote_state(void)5235796c8dcSSimon Schubert get_remote_state (void)
5245796c8dcSSimon Schubert {
5255796c8dcSSimon Schubert   /* Make sure that the remote architecture state has been
5265796c8dcSSimon Schubert      initialized, because doing so might reallocate rs->buf.  Any
5275796c8dcSSimon Schubert      function which calls getpkt also needs to be mindful of changes
5285796c8dcSSimon Schubert      to rs->buf, but this call limits the number of places which run
5295796c8dcSSimon Schubert      into trouble.  */
5305796c8dcSSimon Schubert   get_remote_arch_state ();
5315796c8dcSSimon Schubert 
5325796c8dcSSimon Schubert   return get_remote_state_raw ();
5335796c8dcSSimon Schubert }
5345796c8dcSSimon Schubert 
5355796c8dcSSimon Schubert static int
compare_pnums(const void * lhs_,const void * rhs_)5365796c8dcSSimon Schubert compare_pnums (const void *lhs_, const void *rhs_)
5375796c8dcSSimon Schubert {
5385796c8dcSSimon Schubert   const struct packet_reg * const *lhs = lhs_;
5395796c8dcSSimon Schubert   const struct packet_reg * const *rhs = rhs_;
5405796c8dcSSimon Schubert 
5415796c8dcSSimon Schubert   if ((*lhs)->pnum < (*rhs)->pnum)
5425796c8dcSSimon Schubert     return -1;
5435796c8dcSSimon Schubert   else if ((*lhs)->pnum == (*rhs)->pnum)
5445796c8dcSSimon Schubert     return 0;
5455796c8dcSSimon Schubert   else
5465796c8dcSSimon Schubert     return 1;
5475796c8dcSSimon Schubert }
5485796c8dcSSimon Schubert 
549a45ae5f8SJohn Marino static int
map_regcache_remote_table(struct gdbarch * gdbarch,struct packet_reg * regs)550a45ae5f8SJohn Marino map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
5515796c8dcSSimon Schubert {
5525796c8dcSSimon Schubert   int regnum, num_remote_regs, offset;
5535796c8dcSSimon Schubert   struct packet_reg **remote_regs;
5545796c8dcSSimon Schubert 
5555796c8dcSSimon Schubert   for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
5565796c8dcSSimon Schubert     {
557a45ae5f8SJohn Marino       struct packet_reg *r = &regs[regnum];
5585796c8dcSSimon Schubert 
5595796c8dcSSimon Schubert       if (register_size (gdbarch, regnum) == 0)
5605796c8dcSSimon Schubert 	/* Do not try to fetch zero-sized (placeholder) registers.  */
5615796c8dcSSimon Schubert 	r->pnum = -1;
5625796c8dcSSimon Schubert       else
5635796c8dcSSimon Schubert 	r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
5645796c8dcSSimon Schubert 
5655796c8dcSSimon Schubert       r->regnum = regnum;
5665796c8dcSSimon Schubert     }
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert   /* Define the g/G packet format as the contents of each register
5695796c8dcSSimon Schubert      with a remote protocol number, in order of ascending protocol
5705796c8dcSSimon Schubert      number.  */
5715796c8dcSSimon Schubert 
5725796c8dcSSimon Schubert   remote_regs = alloca (gdbarch_num_regs (gdbarch)
5735796c8dcSSimon Schubert 			* sizeof (struct packet_reg *));
5745796c8dcSSimon Schubert   for (num_remote_regs = 0, regnum = 0;
5755796c8dcSSimon Schubert        regnum < gdbarch_num_regs (gdbarch);
5765796c8dcSSimon Schubert        regnum++)
577a45ae5f8SJohn Marino     if (regs[regnum].pnum != -1)
578a45ae5f8SJohn Marino       remote_regs[num_remote_regs++] = &regs[regnum];
5795796c8dcSSimon Schubert 
5805796c8dcSSimon Schubert   qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
5815796c8dcSSimon Schubert 	 compare_pnums);
5825796c8dcSSimon Schubert 
5835796c8dcSSimon Schubert   for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
5845796c8dcSSimon Schubert     {
5855796c8dcSSimon Schubert       remote_regs[regnum]->in_g_packet = 1;
5865796c8dcSSimon Schubert       remote_regs[regnum]->offset = offset;
5875796c8dcSSimon Schubert       offset += register_size (gdbarch, remote_regs[regnum]->regnum);
5885796c8dcSSimon Schubert     }
5895796c8dcSSimon Schubert 
590a45ae5f8SJohn Marino   return offset;
591a45ae5f8SJohn Marino }
592a45ae5f8SJohn Marino 
593a45ae5f8SJohn Marino /* Given the architecture described by GDBARCH, return the remote
594a45ae5f8SJohn Marino    protocol register's number and the register's offset in the g/G
595a45ae5f8SJohn Marino    packets of GDB register REGNUM, in PNUM and POFFSET respectively.
596a45ae5f8SJohn Marino    If the target does not have a mapping for REGNUM, return false,
597a45ae5f8SJohn Marino    otherwise, return true.  */
598a45ae5f8SJohn Marino 
599a45ae5f8SJohn Marino int
remote_register_number_and_offset(struct gdbarch * gdbarch,int regnum,int * pnum,int * poffset)600a45ae5f8SJohn Marino remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
601a45ae5f8SJohn Marino 				   int *pnum, int *poffset)
602a45ae5f8SJohn Marino {
603a45ae5f8SJohn Marino   int sizeof_g_packet;
604a45ae5f8SJohn Marino   struct packet_reg *regs;
605a45ae5f8SJohn Marino   struct cleanup *old_chain;
606a45ae5f8SJohn Marino 
607a45ae5f8SJohn Marino   gdb_assert (regnum < gdbarch_num_regs (gdbarch));
608a45ae5f8SJohn Marino 
609a45ae5f8SJohn Marino   regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg));
610a45ae5f8SJohn Marino   old_chain = make_cleanup (xfree, regs);
611a45ae5f8SJohn Marino 
612a45ae5f8SJohn Marino   sizeof_g_packet = map_regcache_remote_table (gdbarch, regs);
613a45ae5f8SJohn Marino 
614a45ae5f8SJohn Marino   *pnum = regs[regnum].pnum;
615a45ae5f8SJohn Marino   *poffset = regs[regnum].offset;
616a45ae5f8SJohn Marino 
617a45ae5f8SJohn Marino   do_cleanups (old_chain);
618a45ae5f8SJohn Marino 
619a45ae5f8SJohn Marino   return *pnum != -1;
620a45ae5f8SJohn Marino }
621a45ae5f8SJohn Marino 
622a45ae5f8SJohn Marino static void *
init_remote_state(struct gdbarch * gdbarch)623a45ae5f8SJohn Marino init_remote_state (struct gdbarch *gdbarch)
624a45ae5f8SJohn Marino {
625a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state_raw ();
626a45ae5f8SJohn Marino   struct remote_arch_state *rsa;
627a45ae5f8SJohn Marino 
628a45ae5f8SJohn Marino   rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
629a45ae5f8SJohn Marino 
630a45ae5f8SJohn Marino   /* Use the architecture to build a regnum<->pnum table, which will be
631a45ae5f8SJohn Marino      1:1 unless a feature set specifies otherwise.  */
632a45ae5f8SJohn Marino   rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch,
633a45ae5f8SJohn Marino 				      gdbarch_num_regs (gdbarch),
634a45ae5f8SJohn Marino 				      struct packet_reg);
635a45ae5f8SJohn Marino 
6365796c8dcSSimon Schubert   /* Record the maximum possible size of the g packet - it may turn out
6375796c8dcSSimon Schubert      to be smaller.  */
638a45ae5f8SJohn Marino   rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs);
6395796c8dcSSimon Schubert 
6405796c8dcSSimon Schubert   /* Default maximum number of characters in a packet body.  Many
6415796c8dcSSimon Schubert      remote stubs have a hardwired buffer size of 400 bytes
6425796c8dcSSimon Schubert      (c.f. BUFMAX in m68k-stub.c and i386-stub.c).  BUFMAX-1 is used
6435796c8dcSSimon Schubert      as the maximum packet-size to ensure that the packet and an extra
6445796c8dcSSimon Schubert      NUL character can always fit in the buffer.  This stops GDB
6455796c8dcSSimon Schubert      trashing stubs that try to squeeze an extra NUL into what is
6465796c8dcSSimon Schubert      already a full buffer (As of 1999-12-04 that was most stubs).  */
6475796c8dcSSimon Schubert   rsa->remote_packet_size = 400 - 1;
6485796c8dcSSimon Schubert 
6495796c8dcSSimon Schubert   /* This one is filled in when a ``g'' packet is received.  */
6505796c8dcSSimon Schubert   rsa->actual_register_packet_size = 0;
6515796c8dcSSimon Schubert 
6525796c8dcSSimon Schubert   /* Should rsa->sizeof_g_packet needs more space than the
6535796c8dcSSimon Schubert      default, adjust the size accordingly.  Remember that each byte is
6545796c8dcSSimon Schubert      encoded as two characters.  32 is the overhead for the packet
6555796c8dcSSimon Schubert      header / footer.  NOTE: cagney/1999-10-26: I suspect that 8
6565796c8dcSSimon Schubert      (``$NN:G...#NN'') is a better guess, the below has been padded a
6575796c8dcSSimon Schubert      little.  */
6585796c8dcSSimon Schubert   if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
6595796c8dcSSimon Schubert     rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
6605796c8dcSSimon Schubert 
6615796c8dcSSimon Schubert   /* Make sure that the packet buffer is plenty big enough for
6625796c8dcSSimon Schubert      this architecture.  */
6635796c8dcSSimon Schubert   if (rs->buf_size < rsa->remote_packet_size)
6645796c8dcSSimon Schubert     {
6655796c8dcSSimon Schubert       rs->buf_size = 2 * rsa->remote_packet_size;
6665796c8dcSSimon Schubert       rs->buf = xrealloc (rs->buf, rs->buf_size);
6675796c8dcSSimon Schubert     }
6685796c8dcSSimon Schubert 
6695796c8dcSSimon Schubert   return rsa;
6705796c8dcSSimon Schubert }
6715796c8dcSSimon Schubert 
6725796c8dcSSimon Schubert /* Return the current allowed size of a remote packet.  This is
6735796c8dcSSimon Schubert    inferred from the current architecture, and should be used to
6745796c8dcSSimon Schubert    limit the length of outgoing packets.  */
6755796c8dcSSimon Schubert static long
get_remote_packet_size(void)6765796c8dcSSimon Schubert get_remote_packet_size (void)
6775796c8dcSSimon Schubert {
6785796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
6795796c8dcSSimon Schubert   struct remote_arch_state *rsa = get_remote_arch_state ();
6805796c8dcSSimon Schubert 
6815796c8dcSSimon Schubert   if (rs->explicit_packet_size)
6825796c8dcSSimon Schubert     return rs->explicit_packet_size;
6835796c8dcSSimon Schubert 
6845796c8dcSSimon Schubert   return rsa->remote_packet_size;
6855796c8dcSSimon Schubert }
6865796c8dcSSimon Schubert 
6875796c8dcSSimon Schubert static struct packet_reg *
packet_reg_from_regnum(struct remote_arch_state * rsa,long regnum)6885796c8dcSSimon Schubert packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
6895796c8dcSSimon Schubert {
690ef5ccd6cSJohn Marino   if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ()))
6915796c8dcSSimon Schubert     return NULL;
6925796c8dcSSimon Schubert   else
6935796c8dcSSimon Schubert     {
6945796c8dcSSimon Schubert       struct packet_reg *r = &rsa->regs[regnum];
695cf7f2e2dSJohn Marino 
6965796c8dcSSimon Schubert       gdb_assert (r->regnum == regnum);
6975796c8dcSSimon Schubert       return r;
6985796c8dcSSimon Schubert     }
6995796c8dcSSimon Schubert }
7005796c8dcSSimon Schubert 
7015796c8dcSSimon Schubert static struct packet_reg *
packet_reg_from_pnum(struct remote_arch_state * rsa,LONGEST pnum)7025796c8dcSSimon Schubert packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
7035796c8dcSSimon Schubert {
7045796c8dcSSimon Schubert   int i;
705cf7f2e2dSJohn Marino 
706ef5ccd6cSJohn Marino   for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
7075796c8dcSSimon Schubert     {
7085796c8dcSSimon Schubert       struct packet_reg *r = &rsa->regs[i];
709cf7f2e2dSJohn Marino 
7105796c8dcSSimon Schubert       if (r->pnum == pnum)
7115796c8dcSSimon Schubert 	return r;
7125796c8dcSSimon Schubert     }
7135796c8dcSSimon Schubert   return NULL;
7145796c8dcSSimon Schubert }
7155796c8dcSSimon Schubert 
7165796c8dcSSimon Schubert /* FIXME: graces/2002-08-08: These variables should eventually be
7175796c8dcSSimon Schubert    bound to an instance of the target object (as in gdbarch-tdep()),
7185796c8dcSSimon Schubert    when such a thing exists.  */
7195796c8dcSSimon Schubert 
7205796c8dcSSimon Schubert /* This is set to the data address of the access causing the target
7215796c8dcSSimon Schubert    to stop for a watchpoint.  */
7225796c8dcSSimon Schubert static CORE_ADDR remote_watch_data_address;
7235796c8dcSSimon Schubert 
7245796c8dcSSimon Schubert /* This is non-zero if target stopped for a watchpoint.  */
7255796c8dcSSimon Schubert static int remote_stopped_by_watchpoint_p;
7265796c8dcSSimon Schubert 
7275796c8dcSSimon Schubert static struct target_ops remote_ops;
7285796c8dcSSimon Schubert 
7295796c8dcSSimon Schubert static struct target_ops extended_remote_ops;
7305796c8dcSSimon Schubert 
7315796c8dcSSimon Schubert /* FIXME: cagney/1999-09-23: Even though getpkt was called with
7325796c8dcSSimon Schubert    ``forever'' still use the normal timeout mechanism.  This is
7335796c8dcSSimon Schubert    currently used by the ASYNC code to guarentee that target reads
7345796c8dcSSimon Schubert    during the initial connect always time-out.  Once getpkt has been
7355796c8dcSSimon Schubert    modified to return a timeout indication and, in turn
7365796c8dcSSimon Schubert    remote_wait()/wait_for_inferior() have gained a timeout parameter
7375796c8dcSSimon Schubert    this can go away.  */
7385796c8dcSSimon Schubert static int wait_forever_enabled_p = 1;
7395796c8dcSSimon Schubert 
740cf7f2e2dSJohn Marino /* Allow the user to specify what sequence to send to the remote
741cf7f2e2dSJohn Marino    when he requests a program interruption: Although ^C is usually
742cf7f2e2dSJohn Marino    what remote systems expect (this is the default, here), it is
743cf7f2e2dSJohn Marino    sometimes preferable to send a break.  On other systems such
744cf7f2e2dSJohn Marino    as the Linux kernel, a break followed by g, which is Magic SysRq g
745cf7f2e2dSJohn Marino    is required in order to interrupt the execution.  */
746cf7f2e2dSJohn Marino const char interrupt_sequence_control_c[] = "Ctrl-C";
747cf7f2e2dSJohn Marino const char interrupt_sequence_break[] = "BREAK";
748cf7f2e2dSJohn Marino const char interrupt_sequence_break_g[] = "BREAK-g";
749ef5ccd6cSJohn Marino static const char *const interrupt_sequence_modes[] =
750cf7f2e2dSJohn Marino   {
751cf7f2e2dSJohn Marino     interrupt_sequence_control_c,
752cf7f2e2dSJohn Marino     interrupt_sequence_break,
753cf7f2e2dSJohn Marino     interrupt_sequence_break_g,
754cf7f2e2dSJohn Marino     NULL
755cf7f2e2dSJohn Marino   };
756cf7f2e2dSJohn Marino static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
7575796c8dcSSimon Schubert 
758cf7f2e2dSJohn Marino static void
show_interrupt_sequence(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)759cf7f2e2dSJohn Marino show_interrupt_sequence (struct ui_file *file, int from_tty,
760cf7f2e2dSJohn Marino 			 struct cmd_list_element *c,
761cf7f2e2dSJohn Marino 			 const char *value)
762cf7f2e2dSJohn Marino {
763cf7f2e2dSJohn Marino   if (interrupt_sequence_mode == interrupt_sequence_control_c)
764cf7f2e2dSJohn Marino     fprintf_filtered (file,
765cf7f2e2dSJohn Marino 		      _("Send the ASCII ETX character (Ctrl-c) "
766cf7f2e2dSJohn Marino 			"to the remote target to interrupt the "
767cf7f2e2dSJohn Marino 			"execution of the program.\n"));
768cf7f2e2dSJohn Marino   else if (interrupt_sequence_mode == interrupt_sequence_break)
769cf7f2e2dSJohn Marino     fprintf_filtered (file,
770cf7f2e2dSJohn Marino 		      _("send a break signal to the remote target "
771cf7f2e2dSJohn Marino 			"to interrupt the execution of the program.\n"));
772cf7f2e2dSJohn Marino   else if (interrupt_sequence_mode == interrupt_sequence_break_g)
773cf7f2e2dSJohn Marino     fprintf_filtered (file,
774cf7f2e2dSJohn Marino 		      _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
775cf7f2e2dSJohn Marino 			"the remote target to interrupt the execution "
776cf7f2e2dSJohn Marino 			"of Linux kernel.\n"));
777cf7f2e2dSJohn Marino   else
778cf7f2e2dSJohn Marino     internal_error (__FILE__, __LINE__,
779cf7f2e2dSJohn Marino 		    _("Invalid value for interrupt_sequence_mode: %s."),
780cf7f2e2dSJohn Marino 		    interrupt_sequence_mode);
781cf7f2e2dSJohn Marino }
7825796c8dcSSimon Schubert 
783cf7f2e2dSJohn Marino /* This boolean variable specifies whether interrupt_sequence is sent
784cf7f2e2dSJohn Marino    to the remote target when gdb connects to it.
785cf7f2e2dSJohn Marino    This is mostly needed when you debug the Linux kernel: The Linux kernel
786cf7f2e2dSJohn Marino    expects BREAK g which is Magic SysRq g for connecting gdb.  */
787cf7f2e2dSJohn Marino static int interrupt_on_connect = 0;
788cf7f2e2dSJohn Marino 
789cf7f2e2dSJohn Marino /* This variable is used to implement the "set/show remotebreak" commands.
790cf7f2e2dSJohn Marino    Since these commands are now deprecated in favor of "set/show remote
791cf7f2e2dSJohn Marino    interrupt-sequence", it no longer has any effect on the code.  */
7925796c8dcSSimon Schubert static int remote_break;
7935796c8dcSSimon Schubert 
794cf7f2e2dSJohn Marino static void
set_remotebreak(char * args,int from_tty,struct cmd_list_element * c)795cf7f2e2dSJohn Marino set_remotebreak (char *args, int from_tty, struct cmd_list_element *c)
796cf7f2e2dSJohn Marino {
797cf7f2e2dSJohn Marino   if (remote_break)
798cf7f2e2dSJohn Marino     interrupt_sequence_mode = interrupt_sequence_break;
799cf7f2e2dSJohn Marino   else
800cf7f2e2dSJohn Marino     interrupt_sequence_mode = interrupt_sequence_control_c;
801cf7f2e2dSJohn Marino }
802cf7f2e2dSJohn Marino 
803cf7f2e2dSJohn Marino static void
show_remotebreak(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)804cf7f2e2dSJohn Marino show_remotebreak (struct ui_file *file, int from_tty,
805cf7f2e2dSJohn Marino 		  struct cmd_list_element *c,
806cf7f2e2dSJohn Marino 		  const char *value)
807cf7f2e2dSJohn Marino {
808cf7f2e2dSJohn Marino }
809cf7f2e2dSJohn Marino 
8105796c8dcSSimon Schubert /* Descriptor for I/O to remote machine.  Initialize it to NULL so that
8115796c8dcSSimon Schubert    remote_open knows that we don't have a file open when the program
8125796c8dcSSimon Schubert    starts.  */
8135796c8dcSSimon Schubert static struct serial *remote_desc = NULL;
8145796c8dcSSimon Schubert 
8155796c8dcSSimon Schubert /* This variable sets the number of bits in an address that are to be
8165796c8dcSSimon Schubert    sent in a memory ("M" or "m") packet.  Normally, after stripping
8175796c8dcSSimon Schubert    leading zeros, the entire address would be sent.  This variable
8185796c8dcSSimon Schubert    restricts the address to REMOTE_ADDRESS_SIZE bits.  HISTORY: The
8195796c8dcSSimon Schubert    initial implementation of remote.c restricted the address sent in
8205796c8dcSSimon Schubert    memory packets to ``host::sizeof long'' bytes - (typically 32
8215796c8dcSSimon Schubert    bits).  Consequently, for 64 bit targets, the upper 32 bits of an
8225796c8dcSSimon Schubert    address was never sent.  Since fixing this bug may cause a break in
8235796c8dcSSimon Schubert    some remote targets this variable is principly provided to
8245796c8dcSSimon Schubert    facilitate backward compatibility.  */
8255796c8dcSSimon Schubert 
826ef5ccd6cSJohn Marino static unsigned int remote_address_size;
8275796c8dcSSimon Schubert 
8285796c8dcSSimon Schubert /* Temporary to track who currently owns the terminal.  See
8295796c8dcSSimon Schubert    remote_terminal_* for more details.  */
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert static int remote_async_terminal_ours_p;
8325796c8dcSSimon Schubert 
8335796c8dcSSimon Schubert /* The executable file to use for "run" on the remote side.  */
8345796c8dcSSimon Schubert 
8355796c8dcSSimon Schubert static char *remote_exec_file = "";
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert 
8385796c8dcSSimon Schubert /* User configurable variables for the number of characters in a
8395796c8dcSSimon Schubert    memory read/write packet.  MIN (rsa->remote_packet_size,
8405796c8dcSSimon Schubert    rsa->sizeof_g_packet) is the default.  Some targets need smaller
8415796c8dcSSimon Schubert    values (fifo overruns, et.al.) and some users need larger values
8425796c8dcSSimon Schubert    (speed up transfers).  The variables ``preferred_*'' (the user
8435796c8dcSSimon Schubert    request), ``current_*'' (what was actually set) and ``forced_*''
8445796c8dcSSimon Schubert    (Positive - a soft limit, negative - a hard limit).  */
8455796c8dcSSimon Schubert 
8465796c8dcSSimon Schubert struct memory_packet_config
8475796c8dcSSimon Schubert {
8485796c8dcSSimon Schubert   char *name;
8495796c8dcSSimon Schubert   long size;
8505796c8dcSSimon Schubert   int fixed_p;
8515796c8dcSSimon Schubert };
8525796c8dcSSimon Schubert 
8535796c8dcSSimon Schubert /* Compute the current size of a read/write packet.  Since this makes
8545796c8dcSSimon Schubert    use of ``actual_register_packet_size'' the computation is dynamic.  */
8555796c8dcSSimon Schubert 
8565796c8dcSSimon Schubert static long
get_memory_packet_size(struct memory_packet_config * config)8575796c8dcSSimon Schubert get_memory_packet_size (struct memory_packet_config *config)
8585796c8dcSSimon Schubert {
8595796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
8605796c8dcSSimon Schubert   struct remote_arch_state *rsa = get_remote_arch_state ();
8615796c8dcSSimon Schubert 
8625796c8dcSSimon Schubert   /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
8635796c8dcSSimon Schubert      law?) that some hosts don't cope very well with large alloca()
8645796c8dcSSimon Schubert      calls.  Eventually the alloca() code will be replaced by calls to
8655796c8dcSSimon Schubert      xmalloc() and make_cleanups() allowing this restriction to either
8665796c8dcSSimon Schubert      be lifted or removed.  */
8675796c8dcSSimon Schubert #ifndef MAX_REMOTE_PACKET_SIZE
8685796c8dcSSimon Schubert #define MAX_REMOTE_PACKET_SIZE 16384
8695796c8dcSSimon Schubert #endif
8705796c8dcSSimon Schubert   /* NOTE: 20 ensures we can write at least one byte.  */
8715796c8dcSSimon Schubert #ifndef MIN_REMOTE_PACKET_SIZE
8725796c8dcSSimon Schubert #define MIN_REMOTE_PACKET_SIZE 20
8735796c8dcSSimon Schubert #endif
8745796c8dcSSimon Schubert   long what_they_get;
8755796c8dcSSimon Schubert   if (config->fixed_p)
8765796c8dcSSimon Schubert     {
8775796c8dcSSimon Schubert       if (config->size <= 0)
8785796c8dcSSimon Schubert 	what_they_get = MAX_REMOTE_PACKET_SIZE;
8795796c8dcSSimon Schubert       else
8805796c8dcSSimon Schubert 	what_they_get = config->size;
8815796c8dcSSimon Schubert     }
8825796c8dcSSimon Schubert   else
8835796c8dcSSimon Schubert     {
8845796c8dcSSimon Schubert       what_they_get = get_remote_packet_size ();
8855796c8dcSSimon Schubert       /* Limit the packet to the size specified by the user.  */
8865796c8dcSSimon Schubert       if (config->size > 0
8875796c8dcSSimon Schubert 	  && what_they_get > config->size)
8885796c8dcSSimon Schubert 	what_they_get = config->size;
8895796c8dcSSimon Schubert 
8905796c8dcSSimon Schubert       /* Limit it to the size of the targets ``g'' response unless we have
8915796c8dcSSimon Schubert 	 permission from the stub to use a larger packet size.  */
8925796c8dcSSimon Schubert       if (rs->explicit_packet_size == 0
8935796c8dcSSimon Schubert 	  && rsa->actual_register_packet_size > 0
8945796c8dcSSimon Schubert 	  && what_they_get > rsa->actual_register_packet_size)
8955796c8dcSSimon Schubert 	what_they_get = rsa->actual_register_packet_size;
8965796c8dcSSimon Schubert     }
8975796c8dcSSimon Schubert   if (what_they_get > MAX_REMOTE_PACKET_SIZE)
8985796c8dcSSimon Schubert     what_they_get = MAX_REMOTE_PACKET_SIZE;
8995796c8dcSSimon Schubert   if (what_they_get < MIN_REMOTE_PACKET_SIZE)
9005796c8dcSSimon Schubert     what_they_get = MIN_REMOTE_PACKET_SIZE;
9015796c8dcSSimon Schubert 
9025796c8dcSSimon Schubert   /* Make sure there is room in the global buffer for this packet
9035796c8dcSSimon Schubert      (including its trailing NUL byte).  */
9045796c8dcSSimon Schubert   if (rs->buf_size < what_they_get + 1)
9055796c8dcSSimon Schubert     {
9065796c8dcSSimon Schubert       rs->buf_size = 2 * what_they_get;
9075796c8dcSSimon Schubert       rs->buf = xrealloc (rs->buf, 2 * what_they_get);
9085796c8dcSSimon Schubert     }
9095796c8dcSSimon Schubert 
9105796c8dcSSimon Schubert   return what_they_get;
9115796c8dcSSimon Schubert }
9125796c8dcSSimon Schubert 
9135796c8dcSSimon Schubert /* Update the size of a read/write packet.  If they user wants
9145796c8dcSSimon Schubert    something really big then do a sanity check.  */
9155796c8dcSSimon Schubert 
9165796c8dcSSimon Schubert static void
set_memory_packet_size(char * args,struct memory_packet_config * config)9175796c8dcSSimon Schubert set_memory_packet_size (char *args, struct memory_packet_config *config)
9185796c8dcSSimon Schubert {
9195796c8dcSSimon Schubert   int fixed_p = config->fixed_p;
9205796c8dcSSimon Schubert   long size = config->size;
921cf7f2e2dSJohn Marino 
9225796c8dcSSimon Schubert   if (args == NULL)
9235796c8dcSSimon Schubert     error (_("Argument required (integer, `fixed' or `limited')."));
9245796c8dcSSimon Schubert   else if (strcmp (args, "hard") == 0
9255796c8dcSSimon Schubert       || strcmp (args, "fixed") == 0)
9265796c8dcSSimon Schubert     fixed_p = 1;
9275796c8dcSSimon Schubert   else if (strcmp (args, "soft") == 0
9285796c8dcSSimon Schubert 	   || strcmp (args, "limit") == 0)
9295796c8dcSSimon Schubert     fixed_p = 0;
9305796c8dcSSimon Schubert   else
9315796c8dcSSimon Schubert     {
9325796c8dcSSimon Schubert       char *end;
933cf7f2e2dSJohn Marino 
9345796c8dcSSimon Schubert       size = strtoul (args, &end, 0);
9355796c8dcSSimon Schubert       if (args == end)
9365796c8dcSSimon Schubert 	error (_("Invalid %s (bad syntax)."), config->name);
9375796c8dcSSimon Schubert #if 0
9385796c8dcSSimon Schubert       /* Instead of explicitly capping the size of a packet to
9395796c8dcSSimon Schubert          MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
9405796c8dcSSimon Schubert          instead allowed to set the size to something arbitrarily
9415796c8dcSSimon Schubert          large.  */
9425796c8dcSSimon Schubert       if (size > MAX_REMOTE_PACKET_SIZE)
9435796c8dcSSimon Schubert 	error (_("Invalid %s (too large)."), config->name);
9445796c8dcSSimon Schubert #endif
9455796c8dcSSimon Schubert     }
9465796c8dcSSimon Schubert   /* Extra checks?  */
9475796c8dcSSimon Schubert   if (fixed_p && !config->fixed_p)
9485796c8dcSSimon Schubert     {
9495796c8dcSSimon Schubert       if (! query (_("The target may not be able to correctly handle a %s\n"
9505796c8dcSSimon Schubert 		   "of %ld bytes. Change the packet size? "),
9515796c8dcSSimon Schubert 		   config->name, size))
9525796c8dcSSimon Schubert 	error (_("Packet size not changed."));
9535796c8dcSSimon Schubert     }
9545796c8dcSSimon Schubert   /* Update the config.  */
9555796c8dcSSimon Schubert   config->fixed_p = fixed_p;
9565796c8dcSSimon Schubert   config->size = size;
9575796c8dcSSimon Schubert }
9585796c8dcSSimon Schubert 
9595796c8dcSSimon Schubert static void
show_memory_packet_size(struct memory_packet_config * config)9605796c8dcSSimon Schubert show_memory_packet_size (struct memory_packet_config *config)
9615796c8dcSSimon Schubert {
9625796c8dcSSimon Schubert   printf_filtered (_("The %s is %ld. "), config->name, config->size);
9635796c8dcSSimon Schubert   if (config->fixed_p)
9645796c8dcSSimon Schubert     printf_filtered (_("Packets are fixed at %ld bytes.\n"),
9655796c8dcSSimon Schubert 		     get_memory_packet_size (config));
9665796c8dcSSimon Schubert   else
9675796c8dcSSimon Schubert     printf_filtered (_("Packets are limited to %ld bytes.\n"),
9685796c8dcSSimon Schubert 		     get_memory_packet_size (config));
9695796c8dcSSimon Schubert }
9705796c8dcSSimon Schubert 
9715796c8dcSSimon Schubert static struct memory_packet_config memory_write_packet_config =
9725796c8dcSSimon Schubert {
9735796c8dcSSimon Schubert   "memory-write-packet-size",
9745796c8dcSSimon Schubert };
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert static void
set_memory_write_packet_size(char * args,int from_tty)9775796c8dcSSimon Schubert set_memory_write_packet_size (char *args, int from_tty)
9785796c8dcSSimon Schubert {
9795796c8dcSSimon Schubert   set_memory_packet_size (args, &memory_write_packet_config);
9805796c8dcSSimon Schubert }
9815796c8dcSSimon Schubert 
9825796c8dcSSimon Schubert static void
show_memory_write_packet_size(char * args,int from_tty)9835796c8dcSSimon Schubert show_memory_write_packet_size (char *args, int from_tty)
9845796c8dcSSimon Schubert {
9855796c8dcSSimon Schubert   show_memory_packet_size (&memory_write_packet_config);
9865796c8dcSSimon Schubert }
9875796c8dcSSimon Schubert 
9885796c8dcSSimon Schubert static long
get_memory_write_packet_size(void)9895796c8dcSSimon Schubert get_memory_write_packet_size (void)
9905796c8dcSSimon Schubert {
9915796c8dcSSimon Schubert   return get_memory_packet_size (&memory_write_packet_config);
9925796c8dcSSimon Schubert }
9935796c8dcSSimon Schubert 
9945796c8dcSSimon Schubert static struct memory_packet_config memory_read_packet_config =
9955796c8dcSSimon Schubert {
9965796c8dcSSimon Schubert   "memory-read-packet-size",
9975796c8dcSSimon Schubert };
9985796c8dcSSimon Schubert 
9995796c8dcSSimon Schubert static void
set_memory_read_packet_size(char * args,int from_tty)10005796c8dcSSimon Schubert set_memory_read_packet_size (char *args, int from_tty)
10015796c8dcSSimon Schubert {
10025796c8dcSSimon Schubert   set_memory_packet_size (args, &memory_read_packet_config);
10035796c8dcSSimon Schubert }
10045796c8dcSSimon Schubert 
10055796c8dcSSimon Schubert static void
show_memory_read_packet_size(char * args,int from_tty)10065796c8dcSSimon Schubert show_memory_read_packet_size (char *args, int from_tty)
10075796c8dcSSimon Schubert {
10085796c8dcSSimon Schubert   show_memory_packet_size (&memory_read_packet_config);
10095796c8dcSSimon Schubert }
10105796c8dcSSimon Schubert 
10115796c8dcSSimon Schubert static long
get_memory_read_packet_size(void)10125796c8dcSSimon Schubert get_memory_read_packet_size (void)
10135796c8dcSSimon Schubert {
10145796c8dcSSimon Schubert   long size = get_memory_packet_size (&memory_read_packet_config);
1015cf7f2e2dSJohn Marino 
10165796c8dcSSimon Schubert   /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
10175796c8dcSSimon Schubert      extra buffer size argument before the memory read size can be
10185796c8dcSSimon Schubert      increased beyond this.  */
10195796c8dcSSimon Schubert   if (size > get_remote_packet_size ())
10205796c8dcSSimon Schubert     size = get_remote_packet_size ();
10215796c8dcSSimon Schubert   return size;
10225796c8dcSSimon Schubert }
10235796c8dcSSimon Schubert 
10245796c8dcSSimon Schubert 
10255796c8dcSSimon Schubert /* Generic configuration support for packets the stub optionally
10265796c8dcSSimon Schubert    supports.  Allows the user to specify the use of the packet as well
10275796c8dcSSimon Schubert    as allowing GDB to auto-detect support in the remote stub.  */
10285796c8dcSSimon Schubert 
10295796c8dcSSimon Schubert enum packet_support
10305796c8dcSSimon Schubert   {
10315796c8dcSSimon Schubert     PACKET_SUPPORT_UNKNOWN = 0,
10325796c8dcSSimon Schubert     PACKET_ENABLE,
10335796c8dcSSimon Schubert     PACKET_DISABLE
10345796c8dcSSimon Schubert   };
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert struct packet_config
10375796c8dcSSimon Schubert   {
10385796c8dcSSimon Schubert     const char *name;
10395796c8dcSSimon Schubert     const char *title;
10405796c8dcSSimon Schubert     enum auto_boolean detect;
10415796c8dcSSimon Schubert     enum packet_support support;
10425796c8dcSSimon Schubert   };
10435796c8dcSSimon Schubert 
10445796c8dcSSimon Schubert /* Analyze a packet's return value and update the packet config
10455796c8dcSSimon Schubert    accordingly.  */
10465796c8dcSSimon Schubert 
10475796c8dcSSimon Schubert enum packet_result
10485796c8dcSSimon Schubert {
10495796c8dcSSimon Schubert   PACKET_ERROR,
10505796c8dcSSimon Schubert   PACKET_OK,
10515796c8dcSSimon Schubert   PACKET_UNKNOWN
10525796c8dcSSimon Schubert };
10535796c8dcSSimon Schubert 
10545796c8dcSSimon Schubert static void
update_packet_config(struct packet_config * config)10555796c8dcSSimon Schubert update_packet_config (struct packet_config *config)
10565796c8dcSSimon Schubert {
10575796c8dcSSimon Schubert   switch (config->detect)
10585796c8dcSSimon Schubert     {
10595796c8dcSSimon Schubert     case AUTO_BOOLEAN_TRUE:
10605796c8dcSSimon Schubert       config->support = PACKET_ENABLE;
10615796c8dcSSimon Schubert       break;
10625796c8dcSSimon Schubert     case AUTO_BOOLEAN_FALSE:
10635796c8dcSSimon Schubert       config->support = PACKET_DISABLE;
10645796c8dcSSimon Schubert       break;
10655796c8dcSSimon Schubert     case AUTO_BOOLEAN_AUTO:
10665796c8dcSSimon Schubert       config->support = PACKET_SUPPORT_UNKNOWN;
10675796c8dcSSimon Schubert       break;
10685796c8dcSSimon Schubert     }
10695796c8dcSSimon Schubert }
10705796c8dcSSimon Schubert 
10715796c8dcSSimon Schubert static void
show_packet_config_cmd(struct packet_config * config)10725796c8dcSSimon Schubert show_packet_config_cmd (struct packet_config *config)
10735796c8dcSSimon Schubert {
10745796c8dcSSimon Schubert   char *support = "internal-error";
1075cf7f2e2dSJohn Marino 
10765796c8dcSSimon Schubert   switch (config->support)
10775796c8dcSSimon Schubert     {
10785796c8dcSSimon Schubert     case PACKET_ENABLE:
10795796c8dcSSimon Schubert       support = "enabled";
10805796c8dcSSimon Schubert       break;
10815796c8dcSSimon Schubert     case PACKET_DISABLE:
10825796c8dcSSimon Schubert       support = "disabled";
10835796c8dcSSimon Schubert       break;
10845796c8dcSSimon Schubert     case PACKET_SUPPORT_UNKNOWN:
10855796c8dcSSimon Schubert       support = "unknown";
10865796c8dcSSimon Schubert       break;
10875796c8dcSSimon Schubert     }
10885796c8dcSSimon Schubert   switch (config->detect)
10895796c8dcSSimon Schubert     {
10905796c8dcSSimon Schubert     case AUTO_BOOLEAN_AUTO:
1091c50c785cSJohn Marino       printf_filtered (_("Support for the `%s' packet "
1092c50c785cSJohn Marino 			 "is auto-detected, currently %s.\n"),
10935796c8dcSSimon Schubert 		       config->name, support);
10945796c8dcSSimon Schubert       break;
10955796c8dcSSimon Schubert     case AUTO_BOOLEAN_TRUE:
10965796c8dcSSimon Schubert     case AUTO_BOOLEAN_FALSE:
10975796c8dcSSimon Schubert       printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
10985796c8dcSSimon Schubert 		       config->name, support);
10995796c8dcSSimon Schubert       break;
11005796c8dcSSimon Schubert     }
11015796c8dcSSimon Schubert }
11025796c8dcSSimon Schubert 
11035796c8dcSSimon Schubert static void
add_packet_config_cmd(struct packet_config * config,const char * name,const char * title,int legacy)11045796c8dcSSimon Schubert add_packet_config_cmd (struct packet_config *config, const char *name,
11055796c8dcSSimon Schubert 		       const char *title, int legacy)
11065796c8dcSSimon Schubert {
11075796c8dcSSimon Schubert   char *set_doc;
11085796c8dcSSimon Schubert   char *show_doc;
11095796c8dcSSimon Schubert   char *cmd_name;
11105796c8dcSSimon Schubert 
11115796c8dcSSimon Schubert   config->name = name;
11125796c8dcSSimon Schubert   config->title = title;
11135796c8dcSSimon Schubert   config->detect = AUTO_BOOLEAN_AUTO;
11145796c8dcSSimon Schubert   config->support = PACKET_SUPPORT_UNKNOWN;
11155796c8dcSSimon Schubert   set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
11165796c8dcSSimon Schubert 			name, title);
1117c50c785cSJohn Marino   show_doc = xstrprintf ("Show current use of remote "
1118c50c785cSJohn Marino 			 "protocol `%s' (%s) packet",
11195796c8dcSSimon Schubert 			 name, title);
11205796c8dcSSimon Schubert   /* set/show TITLE-packet {auto,on,off} */
11215796c8dcSSimon Schubert   cmd_name = xstrprintf ("%s-packet", title);
11225796c8dcSSimon Schubert   add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
1123c50c785cSJohn Marino 				&config->detect, set_doc,
1124c50c785cSJohn Marino 				show_doc, NULL, /* help_doc */
11255796c8dcSSimon Schubert 				set_remote_protocol_packet_cmd,
11265796c8dcSSimon Schubert 				show_remote_protocol_packet_cmd,
11275796c8dcSSimon Schubert 				&remote_set_cmdlist, &remote_show_cmdlist);
11285796c8dcSSimon Schubert   /* The command code copies the documentation strings.  */
11295796c8dcSSimon Schubert   xfree (set_doc);
11305796c8dcSSimon Schubert   xfree (show_doc);
11315796c8dcSSimon Schubert   /* set/show remote NAME-packet {auto,on,off} -- legacy.  */
11325796c8dcSSimon Schubert   if (legacy)
11335796c8dcSSimon Schubert     {
11345796c8dcSSimon Schubert       char *legacy_name;
1135cf7f2e2dSJohn Marino 
11365796c8dcSSimon Schubert       legacy_name = xstrprintf ("%s-packet", name);
11375796c8dcSSimon Schubert       add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
11385796c8dcSSimon Schubert 		     &remote_set_cmdlist);
11395796c8dcSSimon Schubert       add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
11405796c8dcSSimon Schubert 		     &remote_show_cmdlist);
11415796c8dcSSimon Schubert     }
11425796c8dcSSimon Schubert }
11435796c8dcSSimon Schubert 
11445796c8dcSSimon Schubert static enum packet_result
packet_check_result(const char * buf)11455796c8dcSSimon Schubert packet_check_result (const char *buf)
11465796c8dcSSimon Schubert {
11475796c8dcSSimon Schubert   if (buf[0] != '\0')
11485796c8dcSSimon Schubert     {
11495796c8dcSSimon Schubert       /* The stub recognized the packet request.  Check that the
11505796c8dcSSimon Schubert 	 operation succeeded.  */
11515796c8dcSSimon Schubert       if (buf[0] == 'E'
11525796c8dcSSimon Schubert 	  && isxdigit (buf[1]) && isxdigit (buf[2])
11535796c8dcSSimon Schubert 	  && buf[3] == '\0')
11545796c8dcSSimon Schubert 	/* "Enn"  - definitly an error.  */
11555796c8dcSSimon Schubert 	return PACKET_ERROR;
11565796c8dcSSimon Schubert 
11575796c8dcSSimon Schubert       /* Always treat "E." as an error.  This will be used for
11585796c8dcSSimon Schubert 	 more verbose error messages, such as E.memtypes.  */
11595796c8dcSSimon Schubert       if (buf[0] == 'E' && buf[1] == '.')
11605796c8dcSSimon Schubert 	return PACKET_ERROR;
11615796c8dcSSimon Schubert 
11625796c8dcSSimon Schubert       /* The packet may or may not be OK.  Just assume it is.  */
11635796c8dcSSimon Schubert       return PACKET_OK;
11645796c8dcSSimon Schubert     }
11655796c8dcSSimon Schubert   else
11665796c8dcSSimon Schubert     /* The stub does not support the packet.  */
11675796c8dcSSimon Schubert     return PACKET_UNKNOWN;
11685796c8dcSSimon Schubert }
11695796c8dcSSimon Schubert 
11705796c8dcSSimon Schubert static enum packet_result
packet_ok(const char * buf,struct packet_config * config)11715796c8dcSSimon Schubert packet_ok (const char *buf, struct packet_config *config)
11725796c8dcSSimon Schubert {
11735796c8dcSSimon Schubert   enum packet_result result;
11745796c8dcSSimon Schubert 
11755796c8dcSSimon Schubert   result = packet_check_result (buf);
11765796c8dcSSimon Schubert   switch (result)
11775796c8dcSSimon Schubert     {
11785796c8dcSSimon Schubert     case PACKET_OK:
11795796c8dcSSimon Schubert     case PACKET_ERROR:
11805796c8dcSSimon Schubert       /* The stub recognized the packet request.  */
11815796c8dcSSimon Schubert       switch (config->support)
11825796c8dcSSimon Schubert 	{
11835796c8dcSSimon Schubert 	case PACKET_SUPPORT_UNKNOWN:
11845796c8dcSSimon Schubert 	  if (remote_debug)
11855796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog,
11865796c8dcSSimon Schubert 				    "Packet %s (%s) is supported\n",
11875796c8dcSSimon Schubert 				    config->name, config->title);
11885796c8dcSSimon Schubert 	  config->support = PACKET_ENABLE;
11895796c8dcSSimon Schubert 	  break;
11905796c8dcSSimon Schubert 	case PACKET_DISABLE:
11915796c8dcSSimon Schubert 	  internal_error (__FILE__, __LINE__,
11925796c8dcSSimon Schubert 			  _("packet_ok: attempt to use a disabled packet"));
11935796c8dcSSimon Schubert 	  break;
11945796c8dcSSimon Schubert 	case PACKET_ENABLE:
11955796c8dcSSimon Schubert 	  break;
11965796c8dcSSimon Schubert 	}
11975796c8dcSSimon Schubert       break;
11985796c8dcSSimon Schubert     case PACKET_UNKNOWN:
11995796c8dcSSimon Schubert       /* The stub does not support the packet.  */
12005796c8dcSSimon Schubert       switch (config->support)
12015796c8dcSSimon Schubert 	{
12025796c8dcSSimon Schubert 	case PACKET_ENABLE:
12035796c8dcSSimon Schubert 	  if (config->detect == AUTO_BOOLEAN_AUTO)
12045796c8dcSSimon Schubert 	    /* If the stub previously indicated that the packet was
12055796c8dcSSimon Schubert 	       supported then there is a protocol error..  */
12065796c8dcSSimon Schubert 	    error (_("Protocol error: %s (%s) conflicting enabled responses."),
12075796c8dcSSimon Schubert 		   config->name, config->title);
12085796c8dcSSimon Schubert 	  else
12095796c8dcSSimon Schubert 	    /* The user set it wrong.  */
12105796c8dcSSimon Schubert 	    error (_("Enabled packet %s (%s) not recognized by stub"),
12115796c8dcSSimon Schubert 		   config->name, config->title);
12125796c8dcSSimon Schubert 	  break;
12135796c8dcSSimon Schubert 	case PACKET_SUPPORT_UNKNOWN:
12145796c8dcSSimon Schubert 	  if (remote_debug)
12155796c8dcSSimon Schubert 	    fprintf_unfiltered (gdb_stdlog,
12165796c8dcSSimon Schubert 				"Packet %s (%s) is NOT supported\n",
12175796c8dcSSimon Schubert 				config->name, config->title);
12185796c8dcSSimon Schubert 	  config->support = PACKET_DISABLE;
12195796c8dcSSimon Schubert 	  break;
12205796c8dcSSimon Schubert 	case PACKET_DISABLE:
12215796c8dcSSimon Schubert 	  break;
12225796c8dcSSimon Schubert 	}
12235796c8dcSSimon Schubert       break;
12245796c8dcSSimon Schubert     }
12255796c8dcSSimon Schubert 
12265796c8dcSSimon Schubert   return result;
12275796c8dcSSimon Schubert }
12285796c8dcSSimon Schubert 
12295796c8dcSSimon Schubert enum {
12305796c8dcSSimon Schubert   PACKET_vCont = 0,
12315796c8dcSSimon Schubert   PACKET_X,
12325796c8dcSSimon Schubert   PACKET_qSymbol,
12335796c8dcSSimon Schubert   PACKET_P,
12345796c8dcSSimon Schubert   PACKET_p,
12355796c8dcSSimon Schubert   PACKET_Z0,
12365796c8dcSSimon Schubert   PACKET_Z1,
12375796c8dcSSimon Schubert   PACKET_Z2,
12385796c8dcSSimon Schubert   PACKET_Z3,
12395796c8dcSSimon Schubert   PACKET_Z4,
12405796c8dcSSimon Schubert   PACKET_vFile_open,
12415796c8dcSSimon Schubert   PACKET_vFile_pread,
12425796c8dcSSimon Schubert   PACKET_vFile_pwrite,
12435796c8dcSSimon Schubert   PACKET_vFile_close,
12445796c8dcSSimon Schubert   PACKET_vFile_unlink,
1245ef5ccd6cSJohn Marino   PACKET_vFile_readlink,
12465796c8dcSSimon Schubert   PACKET_qXfer_auxv,
12475796c8dcSSimon Schubert   PACKET_qXfer_features,
12485796c8dcSSimon Schubert   PACKET_qXfer_libraries,
1249a45ae5f8SJohn Marino   PACKET_qXfer_libraries_svr4,
12505796c8dcSSimon Schubert   PACKET_qXfer_memory_map,
12515796c8dcSSimon Schubert   PACKET_qXfer_spu_read,
12525796c8dcSSimon Schubert   PACKET_qXfer_spu_write,
12535796c8dcSSimon Schubert   PACKET_qXfer_osdata,
1254cf7f2e2dSJohn Marino   PACKET_qXfer_threads,
1255cf7f2e2dSJohn Marino   PACKET_qXfer_statictrace_read,
1256c50c785cSJohn Marino   PACKET_qXfer_traceframe_info,
1257ef5ccd6cSJohn Marino   PACKET_qXfer_uib,
1258cf7f2e2dSJohn Marino   PACKET_qGetTIBAddr,
12595796c8dcSSimon Schubert   PACKET_qGetTLSAddr,
12605796c8dcSSimon Schubert   PACKET_qSupported,
12615796c8dcSSimon Schubert   PACKET_QPassSignals,
1262ef5ccd6cSJohn Marino   PACKET_QProgramSignals,
12635796c8dcSSimon Schubert   PACKET_qSearch_memory,
12645796c8dcSSimon Schubert   PACKET_vAttach,
12655796c8dcSSimon Schubert   PACKET_vRun,
12665796c8dcSSimon Schubert   PACKET_QStartNoAckMode,
12675796c8dcSSimon Schubert   PACKET_vKill,
12685796c8dcSSimon Schubert   PACKET_qXfer_siginfo_read,
12695796c8dcSSimon Schubert   PACKET_qXfer_siginfo_write,
12705796c8dcSSimon Schubert   PACKET_qAttached,
12715796c8dcSSimon Schubert   PACKET_ConditionalTracepoints,
1272ef5ccd6cSJohn Marino   PACKET_ConditionalBreakpoints,
1273ef5ccd6cSJohn Marino   PACKET_BreakpointCommands,
1274cf7f2e2dSJohn Marino   PACKET_FastTracepoints,
1275cf7f2e2dSJohn Marino   PACKET_StaticTracepoints,
1276a45ae5f8SJohn Marino   PACKET_InstallInTrace,
12775796c8dcSSimon Schubert   PACKET_bc,
12785796c8dcSSimon Schubert   PACKET_bs,
1279cf7f2e2dSJohn Marino   PACKET_TracepointSource,
1280cf7f2e2dSJohn Marino   PACKET_QAllow,
1281a45ae5f8SJohn Marino   PACKET_qXfer_fdpic,
1282a45ae5f8SJohn Marino   PACKET_QDisableRandomization,
1283ef5ccd6cSJohn Marino   PACKET_QAgent,
1284ef5ccd6cSJohn Marino   PACKET_QTBuffer_size,
1285ef5ccd6cSJohn Marino   PACKET_Qbtrace_off,
1286ef5ccd6cSJohn Marino   PACKET_Qbtrace_bts,
1287ef5ccd6cSJohn Marino   PACKET_qXfer_btrace,
12885796c8dcSSimon Schubert   PACKET_MAX
12895796c8dcSSimon Schubert };
12905796c8dcSSimon Schubert 
12915796c8dcSSimon Schubert static struct packet_config remote_protocol_packets[PACKET_MAX];
12925796c8dcSSimon Schubert 
12935796c8dcSSimon Schubert static void
set_remote_protocol_packet_cmd(char * args,int from_tty,struct cmd_list_element * c)12945796c8dcSSimon Schubert set_remote_protocol_packet_cmd (char *args, int from_tty,
12955796c8dcSSimon Schubert 				struct cmd_list_element *c)
12965796c8dcSSimon Schubert {
12975796c8dcSSimon Schubert   struct packet_config *packet;
12985796c8dcSSimon Schubert 
12995796c8dcSSimon Schubert   for (packet = remote_protocol_packets;
13005796c8dcSSimon Schubert        packet < &remote_protocol_packets[PACKET_MAX];
13015796c8dcSSimon Schubert        packet++)
13025796c8dcSSimon Schubert     {
13035796c8dcSSimon Schubert       if (&packet->detect == c->var)
13045796c8dcSSimon Schubert 	{
13055796c8dcSSimon Schubert 	  update_packet_config (packet);
13065796c8dcSSimon Schubert 	  return;
13075796c8dcSSimon Schubert 	}
13085796c8dcSSimon Schubert     }
1309c50c785cSJohn Marino   internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
13105796c8dcSSimon Schubert 		  c->name);
13115796c8dcSSimon Schubert }
13125796c8dcSSimon Schubert 
13135796c8dcSSimon Schubert static void
show_remote_protocol_packet_cmd(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)13145796c8dcSSimon Schubert show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
13155796c8dcSSimon Schubert 				 struct cmd_list_element *c,
13165796c8dcSSimon Schubert 				 const char *value)
13175796c8dcSSimon Schubert {
13185796c8dcSSimon Schubert   struct packet_config *packet;
13195796c8dcSSimon Schubert 
13205796c8dcSSimon Schubert   for (packet = remote_protocol_packets;
13215796c8dcSSimon Schubert        packet < &remote_protocol_packets[PACKET_MAX];
13225796c8dcSSimon Schubert        packet++)
13235796c8dcSSimon Schubert     {
13245796c8dcSSimon Schubert       if (&packet->detect == c->var)
13255796c8dcSSimon Schubert 	{
13265796c8dcSSimon Schubert 	  show_packet_config_cmd (packet);
13275796c8dcSSimon Schubert 	  return;
13285796c8dcSSimon Schubert 	}
13295796c8dcSSimon Schubert     }
1330c50c785cSJohn Marino   internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
13315796c8dcSSimon Schubert 		  c->name);
13325796c8dcSSimon Schubert }
13335796c8dcSSimon Schubert 
13345796c8dcSSimon Schubert /* Should we try one of the 'Z' requests?  */
13355796c8dcSSimon Schubert 
13365796c8dcSSimon Schubert enum Z_packet_type
13375796c8dcSSimon Schubert {
13385796c8dcSSimon Schubert   Z_PACKET_SOFTWARE_BP,
13395796c8dcSSimon Schubert   Z_PACKET_HARDWARE_BP,
13405796c8dcSSimon Schubert   Z_PACKET_WRITE_WP,
13415796c8dcSSimon Schubert   Z_PACKET_READ_WP,
13425796c8dcSSimon Schubert   Z_PACKET_ACCESS_WP,
13435796c8dcSSimon Schubert   NR_Z_PACKET_TYPES
13445796c8dcSSimon Schubert };
13455796c8dcSSimon Schubert 
13465796c8dcSSimon Schubert /* For compatibility with older distributions.  Provide a ``set remote
13475796c8dcSSimon Schubert    Z-packet ...'' command that updates all the Z packet types.  */
13485796c8dcSSimon Schubert 
13495796c8dcSSimon Schubert static enum auto_boolean remote_Z_packet_detect;
13505796c8dcSSimon Schubert 
13515796c8dcSSimon Schubert static void
set_remote_protocol_Z_packet_cmd(char * args,int from_tty,struct cmd_list_element * c)13525796c8dcSSimon Schubert set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
13535796c8dcSSimon Schubert 				  struct cmd_list_element *c)
13545796c8dcSSimon Schubert {
13555796c8dcSSimon Schubert   int i;
1356cf7f2e2dSJohn Marino 
13575796c8dcSSimon Schubert   for (i = 0; i < NR_Z_PACKET_TYPES; i++)
13585796c8dcSSimon Schubert     {
13595796c8dcSSimon Schubert       remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
13605796c8dcSSimon Schubert       update_packet_config (&remote_protocol_packets[PACKET_Z0 + i]);
13615796c8dcSSimon Schubert     }
13625796c8dcSSimon Schubert }
13635796c8dcSSimon Schubert 
13645796c8dcSSimon Schubert static void
show_remote_protocol_Z_packet_cmd(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)13655796c8dcSSimon Schubert show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
13665796c8dcSSimon Schubert 				   struct cmd_list_element *c,
13675796c8dcSSimon Schubert 				   const char *value)
13685796c8dcSSimon Schubert {
13695796c8dcSSimon Schubert   int i;
1370cf7f2e2dSJohn Marino 
13715796c8dcSSimon Schubert   for (i = 0; i < NR_Z_PACKET_TYPES; i++)
13725796c8dcSSimon Schubert     {
13735796c8dcSSimon Schubert       show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
13745796c8dcSSimon Schubert     }
13755796c8dcSSimon Schubert }
13765796c8dcSSimon Schubert 
13775796c8dcSSimon Schubert /* Should we try the 'ThreadInfo' query packet?
13785796c8dcSSimon Schubert 
13795796c8dcSSimon Schubert    This variable (NOT available to the user: auto-detect only!)
13805796c8dcSSimon Schubert    determines whether GDB will use the new, simpler "ThreadInfo"
13815796c8dcSSimon Schubert    query or the older, more complex syntax for thread queries.
13825796c8dcSSimon Schubert    This is an auto-detect variable (set to true at each connect,
13835796c8dcSSimon Schubert    and set to false when the target fails to recognize it).  */
13845796c8dcSSimon Schubert 
13855796c8dcSSimon Schubert static int use_threadinfo_query;
13865796c8dcSSimon Schubert static int use_threadextra_query;
13875796c8dcSSimon Schubert 
13885796c8dcSSimon Schubert /* Tokens for use by the asynchronous signal handlers for SIGINT.  */
13895796c8dcSSimon Schubert static struct async_signal_handler *sigint_remote_twice_token;
13905796c8dcSSimon Schubert static struct async_signal_handler *sigint_remote_token;
13915796c8dcSSimon Schubert 
13925796c8dcSSimon Schubert 
13935796c8dcSSimon Schubert /* Asynchronous signal handle registered as event loop source for
13945796c8dcSSimon Schubert    when we have pending events ready to be passed to the core.  */
13955796c8dcSSimon Schubert 
13965796c8dcSSimon Schubert static struct async_event_handler *remote_async_inferior_event_token;
13975796c8dcSSimon Schubert 
13985796c8dcSSimon Schubert 
13995796c8dcSSimon Schubert 
14005796c8dcSSimon Schubert static ptid_t magic_null_ptid;
14015796c8dcSSimon Schubert static ptid_t not_sent_ptid;
14025796c8dcSSimon Schubert static ptid_t any_thread_ptid;
14035796c8dcSSimon Schubert 
14045796c8dcSSimon Schubert /* These are the threads which we last sent to the remote system.  The
14055796c8dcSSimon Schubert    TID member will be -1 for all or -2 for not sent yet.  */
14065796c8dcSSimon Schubert 
14075796c8dcSSimon Schubert static ptid_t general_thread;
14085796c8dcSSimon Schubert static ptid_t continue_thread;
14095796c8dcSSimon Schubert 
1410ef5ccd6cSJohn Marino /* This is the traceframe which we last selected on the remote system.
1411c50c785cSJohn Marino    It will be -1 if no traceframe is selected.  */
1412c50c785cSJohn Marino static int remote_traceframe_number = -1;
1413c50c785cSJohn Marino 
14145796c8dcSSimon Schubert /* Find out if the stub attached to PID (and hence GDB should offer to
14155796c8dcSSimon Schubert    detach instead of killing it when bailing out).  */
14165796c8dcSSimon Schubert 
14175796c8dcSSimon Schubert static int
remote_query_attached(int pid)14185796c8dcSSimon Schubert remote_query_attached (int pid)
14195796c8dcSSimon Schubert {
14205796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
1421ef5ccd6cSJohn Marino   size_t size = get_remote_packet_size ();
14225796c8dcSSimon Schubert 
14235796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_qAttached].support == PACKET_DISABLE)
14245796c8dcSSimon Schubert     return 0;
14255796c8dcSSimon Schubert 
14265796c8dcSSimon Schubert   if (remote_multi_process_p (rs))
1427ef5ccd6cSJohn Marino     xsnprintf (rs->buf, size, "qAttached:%x", pid);
14285796c8dcSSimon Schubert   else
1429ef5ccd6cSJohn Marino     xsnprintf (rs->buf, size, "qAttached");
14305796c8dcSSimon Schubert 
14315796c8dcSSimon Schubert   putpkt (rs->buf);
14325796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
14335796c8dcSSimon Schubert 
14345796c8dcSSimon Schubert   switch (packet_ok (rs->buf,
14355796c8dcSSimon Schubert 		     &remote_protocol_packets[PACKET_qAttached]))
14365796c8dcSSimon Schubert     {
14375796c8dcSSimon Schubert     case PACKET_OK:
14385796c8dcSSimon Schubert       if (strcmp (rs->buf, "1") == 0)
14395796c8dcSSimon Schubert 	return 1;
14405796c8dcSSimon Schubert       break;
14415796c8dcSSimon Schubert     case PACKET_ERROR:
14425796c8dcSSimon Schubert       warning (_("Remote failure reply: %s"), rs->buf);
14435796c8dcSSimon Schubert       break;
14445796c8dcSSimon Schubert     case PACKET_UNKNOWN:
14455796c8dcSSimon Schubert       break;
14465796c8dcSSimon Schubert     }
14475796c8dcSSimon Schubert 
14485796c8dcSSimon Schubert   return 0;
14495796c8dcSSimon Schubert }
14505796c8dcSSimon Schubert 
1451ef5ccd6cSJohn Marino /* Add PID to GDB's inferior table.  If FAKE_PID_P is true, then PID
1452ef5ccd6cSJohn Marino    has been invented by GDB, instead of reported by the target.  Since
1453ef5ccd6cSJohn Marino    we can be connected to a remote system before before knowing about
1454ef5ccd6cSJohn Marino    any inferior, mark the target with execution when we find the first
1455ef5ccd6cSJohn Marino    inferior.  If ATTACHED is 1, then we had just attached to this
1456ef5ccd6cSJohn Marino    inferior.  If it is 0, then we just created this inferior.  If it
1457ef5ccd6cSJohn Marino    is -1, then try querying the remote stub to find out if it had
1458ef5ccd6cSJohn Marino    attached to the inferior or not.  */
14595796c8dcSSimon Schubert 
14605796c8dcSSimon Schubert static struct inferior *
remote_add_inferior(int fake_pid_p,int pid,int attached)1461ef5ccd6cSJohn Marino remote_add_inferior (int fake_pid_p, int pid, int attached)
14625796c8dcSSimon Schubert {
14635796c8dcSSimon Schubert   struct inferior *inf;
14645796c8dcSSimon Schubert 
14655796c8dcSSimon Schubert   /* Check whether this process we're learning about is to be
14665796c8dcSSimon Schubert      considered attached, or if is to be considered to have been
14675796c8dcSSimon Schubert      spawned by the stub.  */
14685796c8dcSSimon Schubert   if (attached == -1)
14695796c8dcSSimon Schubert     attached = remote_query_attached (pid);
14705796c8dcSSimon Schubert 
1471ef5ccd6cSJohn Marino   if (gdbarch_has_global_solist (target_gdbarch ()))
1472cf7f2e2dSJohn Marino     {
1473cf7f2e2dSJohn Marino       /* If the target shares code across all inferiors, then every
1474cf7f2e2dSJohn Marino 	 attach adds a new inferior.  */
14755796c8dcSSimon Schubert       inf = add_inferior (pid);
14765796c8dcSSimon Schubert 
1477cf7f2e2dSJohn Marino       /* ... and every inferior is bound to the same program space.
1478cf7f2e2dSJohn Marino 	 However, each inferior may still have its own address
1479cf7f2e2dSJohn Marino 	 space.  */
1480cf7f2e2dSJohn Marino       inf->aspace = maybe_new_address_space ();
1481cf7f2e2dSJohn Marino       inf->pspace = current_program_space;
1482cf7f2e2dSJohn Marino     }
1483cf7f2e2dSJohn Marino   else
1484cf7f2e2dSJohn Marino     {
1485cf7f2e2dSJohn Marino       /* In the traditional debugging scenario, there's a 1-1 match
1486cf7f2e2dSJohn Marino 	 between program/address spaces.  We simply bind the inferior
1487cf7f2e2dSJohn Marino 	 to the program space's address space.  */
1488cf7f2e2dSJohn Marino       inf = current_inferior ();
1489cf7f2e2dSJohn Marino       inferior_appeared (inf, pid);
1490cf7f2e2dSJohn Marino     }
1491cf7f2e2dSJohn Marino 
14925796c8dcSSimon Schubert   inf->attach_flag = attached;
1493ef5ccd6cSJohn Marino   inf->fake_pid_p = fake_pid_p;
14945796c8dcSSimon Schubert 
14955796c8dcSSimon Schubert   return inf;
14965796c8dcSSimon Schubert }
14975796c8dcSSimon Schubert 
14985796c8dcSSimon Schubert /* Add thread PTID to GDB's thread list.  Tag it as executing/running
14995796c8dcSSimon Schubert    according to RUNNING.  */
15005796c8dcSSimon Schubert 
15015796c8dcSSimon Schubert static void
remote_add_thread(ptid_t ptid,int running)15025796c8dcSSimon Schubert remote_add_thread (ptid_t ptid, int running)
15035796c8dcSSimon Schubert {
15045796c8dcSSimon Schubert   add_thread (ptid);
15055796c8dcSSimon Schubert 
15065796c8dcSSimon Schubert   set_executing (ptid, running);
15075796c8dcSSimon Schubert   set_running (ptid, running);
15085796c8dcSSimon Schubert }
15095796c8dcSSimon Schubert 
15105796c8dcSSimon Schubert /* Come here when we learn about a thread id from the remote target.
15115796c8dcSSimon Schubert    It may be the first time we hear about such thread, so take the
15125796c8dcSSimon Schubert    opportunity to add it to GDB's thread list.  In case this is the
15135796c8dcSSimon Schubert    first time we're noticing its corresponding inferior, add it to
15145796c8dcSSimon Schubert    GDB's inferior list as well.  */
15155796c8dcSSimon Schubert 
15165796c8dcSSimon Schubert static void
remote_notice_new_inferior(ptid_t currthread,int running)15175796c8dcSSimon Schubert remote_notice_new_inferior (ptid_t currthread, int running)
15185796c8dcSSimon Schubert {
15195796c8dcSSimon Schubert   /* If this is a new thread, add it to GDB's thread list.
15205796c8dcSSimon Schubert      If we leave it up to WFI to do this, bad things will happen.  */
15215796c8dcSSimon Schubert 
15225796c8dcSSimon Schubert   if (in_thread_list (currthread) && is_exited (currthread))
15235796c8dcSSimon Schubert     {
15245796c8dcSSimon Schubert       /* We're seeing an event on a thread id we knew had exited.
15255796c8dcSSimon Schubert 	 This has to be a new thread reusing the old id.  Add it.  */
15265796c8dcSSimon Schubert       remote_add_thread (currthread, running);
15275796c8dcSSimon Schubert       return;
15285796c8dcSSimon Schubert     }
15295796c8dcSSimon Schubert 
15305796c8dcSSimon Schubert   if (!in_thread_list (currthread))
15315796c8dcSSimon Schubert     {
15325796c8dcSSimon Schubert       struct inferior *inf = NULL;
15335796c8dcSSimon Schubert       int pid = ptid_get_pid (currthread);
15345796c8dcSSimon Schubert 
15355796c8dcSSimon Schubert       if (ptid_is_pid (inferior_ptid)
15365796c8dcSSimon Schubert 	  && pid == ptid_get_pid (inferior_ptid))
15375796c8dcSSimon Schubert 	{
15385796c8dcSSimon Schubert 	  /* inferior_ptid has no thread member yet.  This can happen
15395796c8dcSSimon Schubert 	     with the vAttach -> remote_wait,"TAAthread:" path if the
15405796c8dcSSimon Schubert 	     stub doesn't support qC.  This is the first stop reported
15415796c8dcSSimon Schubert 	     after an attach, so this is the main thread.  Update the
15425796c8dcSSimon Schubert 	     ptid in the thread list.  */
15435796c8dcSSimon Schubert 	  if (in_thread_list (pid_to_ptid (pid)))
15445796c8dcSSimon Schubert 	    thread_change_ptid (inferior_ptid, currthread);
15455796c8dcSSimon Schubert 	  else
15465796c8dcSSimon Schubert 	    {
15475796c8dcSSimon Schubert 	      remote_add_thread (currthread, running);
15485796c8dcSSimon Schubert 	      inferior_ptid = currthread;
15495796c8dcSSimon Schubert 	    }
15505796c8dcSSimon Schubert 	  return;
15515796c8dcSSimon Schubert 	}
15525796c8dcSSimon Schubert 
15535796c8dcSSimon Schubert       if (ptid_equal (magic_null_ptid, inferior_ptid))
15545796c8dcSSimon Schubert 	{
15555796c8dcSSimon Schubert 	  /* inferior_ptid is not set yet.  This can happen with the
15565796c8dcSSimon Schubert 	     vRun -> remote_wait,"TAAthread:" path if the stub
15575796c8dcSSimon Schubert 	     doesn't support qC.  This is the first stop reported
15585796c8dcSSimon Schubert 	     after an attach, so this is the main thread.  Update the
15595796c8dcSSimon Schubert 	     ptid in the thread list.  */
15605796c8dcSSimon Schubert 	  thread_change_ptid (inferior_ptid, currthread);
15615796c8dcSSimon Schubert 	  return;
15625796c8dcSSimon Schubert 	}
15635796c8dcSSimon Schubert 
15645796c8dcSSimon Schubert       /* When connecting to a target remote, or to a target
15655796c8dcSSimon Schubert 	 extended-remote which already was debugging an inferior, we
15665796c8dcSSimon Schubert 	 may not know about it yet.  Add it before adding its child
15675796c8dcSSimon Schubert 	 thread, so notifications are emitted in a sensible order.  */
15685796c8dcSSimon Schubert       if (!in_inferior_list (ptid_get_pid (currthread)))
1569ef5ccd6cSJohn Marino 	{
1570ef5ccd6cSJohn Marino 	  struct remote_state *rs = get_remote_state ();
1571ef5ccd6cSJohn Marino 	  int fake_pid_p = !remote_multi_process_p (rs);
1572ef5ccd6cSJohn Marino 
1573ef5ccd6cSJohn Marino 	  inf = remote_add_inferior (fake_pid_p,
1574ef5ccd6cSJohn Marino 				     ptid_get_pid (currthread), -1);
1575ef5ccd6cSJohn Marino 	}
15765796c8dcSSimon Schubert 
15775796c8dcSSimon Schubert       /* This is really a new thread.  Add it.  */
15785796c8dcSSimon Schubert       remote_add_thread (currthread, running);
15795796c8dcSSimon Schubert 
15805796c8dcSSimon Schubert       /* If we found a new inferior, let the common code do whatever
15815796c8dcSSimon Schubert 	 it needs to with it (e.g., read shared libraries, insert
15825796c8dcSSimon Schubert 	 breakpoints).  */
15835796c8dcSSimon Schubert       if (inf != NULL)
15845796c8dcSSimon Schubert 	notice_new_inferior (currthread, running, 0);
15855796c8dcSSimon Schubert     }
15865796c8dcSSimon Schubert }
15875796c8dcSSimon Schubert 
1588cf7f2e2dSJohn Marino /* Return the private thread data, creating it if necessary.  */
1589cf7f2e2dSJohn Marino 
1590ef5ccd6cSJohn Marino static struct private_thread_info *
demand_private_info(ptid_t ptid)1591cf7f2e2dSJohn Marino demand_private_info (ptid_t ptid)
1592cf7f2e2dSJohn Marino {
1593cf7f2e2dSJohn Marino   struct thread_info *info = find_thread_ptid (ptid);
1594cf7f2e2dSJohn Marino 
1595cf7f2e2dSJohn Marino   gdb_assert (info);
1596cf7f2e2dSJohn Marino 
1597cf7f2e2dSJohn Marino   if (!info->private)
1598cf7f2e2dSJohn Marino     {
1599cf7f2e2dSJohn Marino       info->private = xmalloc (sizeof (*(info->private)));
1600cf7f2e2dSJohn Marino       info->private_dtor = free_private_thread_info;
1601cf7f2e2dSJohn Marino       info->private->core = -1;
1602cf7f2e2dSJohn Marino       info->private->extra = 0;
1603cf7f2e2dSJohn Marino     }
1604cf7f2e2dSJohn Marino 
1605cf7f2e2dSJohn Marino   return info->private;
1606cf7f2e2dSJohn Marino }
1607cf7f2e2dSJohn Marino 
16085796c8dcSSimon Schubert /* Call this function as a result of
16095796c8dcSSimon Schubert    1) A halt indication (T packet) containing a thread id
16105796c8dcSSimon Schubert    2) A direct query of currthread
1611c50c785cSJohn Marino    3) Successful execution of set thread */
16125796c8dcSSimon Schubert 
16135796c8dcSSimon Schubert static void
record_currthread(ptid_t currthread)16145796c8dcSSimon Schubert record_currthread (ptid_t currthread)
16155796c8dcSSimon Schubert {
16165796c8dcSSimon Schubert   general_thread = currthread;
16175796c8dcSSimon Schubert }
16185796c8dcSSimon Schubert 
16195796c8dcSSimon Schubert static char *last_pass_packet;
16205796c8dcSSimon Schubert 
16215796c8dcSSimon Schubert /* If 'QPassSignals' is supported, tell the remote stub what signals
16225796c8dcSSimon Schubert    it can simply pass through to the inferior without reporting.  */
16235796c8dcSSimon Schubert 
16245796c8dcSSimon Schubert static void
remote_pass_signals(int numsigs,unsigned char * pass_signals)1625a45ae5f8SJohn Marino remote_pass_signals (int numsigs, unsigned char *pass_signals)
16265796c8dcSSimon Schubert {
16275796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_QPassSignals].support != PACKET_DISABLE)
16285796c8dcSSimon Schubert     {
16295796c8dcSSimon Schubert       char *pass_packet, *p;
16305796c8dcSSimon Schubert       int count = 0, i;
16315796c8dcSSimon Schubert 
16325796c8dcSSimon Schubert       gdb_assert (numsigs < 256);
16335796c8dcSSimon Schubert       for (i = 0; i < numsigs; i++)
16345796c8dcSSimon Schubert 	{
1635a45ae5f8SJohn Marino 	  if (pass_signals[i])
16365796c8dcSSimon Schubert 	    count++;
16375796c8dcSSimon Schubert 	}
16385796c8dcSSimon Schubert       pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
16395796c8dcSSimon Schubert       strcpy (pass_packet, "QPassSignals:");
16405796c8dcSSimon Schubert       p = pass_packet + strlen (pass_packet);
16415796c8dcSSimon Schubert       for (i = 0; i < numsigs; i++)
16425796c8dcSSimon Schubert 	{
1643a45ae5f8SJohn Marino 	  if (pass_signals[i])
16445796c8dcSSimon Schubert 	    {
16455796c8dcSSimon Schubert 	      if (i >= 16)
16465796c8dcSSimon Schubert 		*p++ = tohex (i >> 4);
16475796c8dcSSimon Schubert 	      *p++ = tohex (i & 15);
16485796c8dcSSimon Schubert 	      if (count)
16495796c8dcSSimon Schubert 		*p++ = ';';
16505796c8dcSSimon Schubert 	      else
16515796c8dcSSimon Schubert 		break;
16525796c8dcSSimon Schubert 	      count--;
16535796c8dcSSimon Schubert 	    }
16545796c8dcSSimon Schubert 	}
16555796c8dcSSimon Schubert       *p = 0;
16565796c8dcSSimon Schubert       if (!last_pass_packet || strcmp (last_pass_packet, pass_packet))
16575796c8dcSSimon Schubert 	{
16585796c8dcSSimon Schubert 	  struct remote_state *rs = get_remote_state ();
16595796c8dcSSimon Schubert 	  char *buf = rs->buf;
16605796c8dcSSimon Schubert 
16615796c8dcSSimon Schubert 	  putpkt (pass_packet);
16625796c8dcSSimon Schubert 	  getpkt (&rs->buf, &rs->buf_size, 0);
16635796c8dcSSimon Schubert 	  packet_ok (buf, &remote_protocol_packets[PACKET_QPassSignals]);
16645796c8dcSSimon Schubert 	  if (last_pass_packet)
16655796c8dcSSimon Schubert 	    xfree (last_pass_packet);
16665796c8dcSSimon Schubert 	  last_pass_packet = pass_packet;
16675796c8dcSSimon Schubert 	}
16685796c8dcSSimon Schubert       else
16695796c8dcSSimon Schubert 	xfree (pass_packet);
16705796c8dcSSimon Schubert     }
16715796c8dcSSimon Schubert }
16725796c8dcSSimon Schubert 
1673ef5ccd6cSJohn Marino /* The last QProgramSignals packet sent to the target.  We bypass
1674ef5ccd6cSJohn Marino    sending a new program signals list down to the target if the new
1675ef5ccd6cSJohn Marino    packet is exactly the same as the last we sent.  IOW, we only let
1676ef5ccd6cSJohn Marino    the target know about program signals list changes.  */
1677ef5ccd6cSJohn Marino 
1678ef5ccd6cSJohn Marino static char *last_program_signals_packet;
1679ef5ccd6cSJohn Marino 
1680ef5ccd6cSJohn Marino /* If 'QProgramSignals' is supported, tell the remote stub what
1681ef5ccd6cSJohn Marino    signals it should pass through to the inferior when detaching.  */
1682ef5ccd6cSJohn Marino 
1683ef5ccd6cSJohn Marino static void
remote_program_signals(int numsigs,unsigned char * signals)1684ef5ccd6cSJohn Marino remote_program_signals (int numsigs, unsigned char *signals)
1685ef5ccd6cSJohn Marino {
1686ef5ccd6cSJohn Marino   if (remote_protocol_packets[PACKET_QProgramSignals].support != PACKET_DISABLE)
1687ef5ccd6cSJohn Marino     {
1688ef5ccd6cSJohn Marino       char *packet, *p;
1689ef5ccd6cSJohn Marino       int count = 0, i;
1690ef5ccd6cSJohn Marino 
1691ef5ccd6cSJohn Marino       gdb_assert (numsigs < 256);
1692ef5ccd6cSJohn Marino       for (i = 0; i < numsigs; i++)
1693ef5ccd6cSJohn Marino 	{
1694ef5ccd6cSJohn Marino 	  if (signals[i])
1695ef5ccd6cSJohn Marino 	    count++;
1696ef5ccd6cSJohn Marino 	}
1697ef5ccd6cSJohn Marino       packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
1698ef5ccd6cSJohn Marino       strcpy (packet, "QProgramSignals:");
1699ef5ccd6cSJohn Marino       p = packet + strlen (packet);
1700ef5ccd6cSJohn Marino       for (i = 0; i < numsigs; i++)
1701ef5ccd6cSJohn Marino 	{
1702ef5ccd6cSJohn Marino 	  if (signal_pass_state (i))
1703ef5ccd6cSJohn Marino 	    {
1704ef5ccd6cSJohn Marino 	      if (i >= 16)
1705ef5ccd6cSJohn Marino 		*p++ = tohex (i >> 4);
1706ef5ccd6cSJohn Marino 	      *p++ = tohex (i & 15);
1707ef5ccd6cSJohn Marino 	      if (count)
1708ef5ccd6cSJohn Marino 		*p++ = ';';
1709ef5ccd6cSJohn Marino 	      else
1710ef5ccd6cSJohn Marino 		break;
1711ef5ccd6cSJohn Marino 	      count--;
1712ef5ccd6cSJohn Marino 	    }
1713ef5ccd6cSJohn Marino 	}
1714ef5ccd6cSJohn Marino       *p = 0;
1715ef5ccd6cSJohn Marino       if (!last_program_signals_packet
1716ef5ccd6cSJohn Marino 	  || strcmp (last_program_signals_packet, packet) != 0)
1717ef5ccd6cSJohn Marino 	{
1718ef5ccd6cSJohn Marino 	  struct remote_state *rs = get_remote_state ();
1719ef5ccd6cSJohn Marino 	  char *buf = rs->buf;
1720ef5ccd6cSJohn Marino 
1721ef5ccd6cSJohn Marino 	  putpkt (packet);
1722ef5ccd6cSJohn Marino 	  getpkt (&rs->buf, &rs->buf_size, 0);
1723ef5ccd6cSJohn Marino 	  packet_ok (buf, &remote_protocol_packets[PACKET_QProgramSignals]);
1724ef5ccd6cSJohn Marino 	  xfree (last_program_signals_packet);
1725ef5ccd6cSJohn Marino 	  last_program_signals_packet = packet;
1726ef5ccd6cSJohn Marino 	}
1727ef5ccd6cSJohn Marino       else
1728ef5ccd6cSJohn Marino 	xfree (packet);
1729ef5ccd6cSJohn Marino     }
1730ef5ccd6cSJohn Marino }
1731ef5ccd6cSJohn Marino 
17325796c8dcSSimon Schubert /* If PTID is MAGIC_NULL_PTID, don't set any thread.  If PTID is
17335796c8dcSSimon Schubert    MINUS_ONE_PTID, set the thread to -1, so the stub returns the
17345796c8dcSSimon Schubert    thread.  If GEN is set, set the general thread, if not, then set
17355796c8dcSSimon Schubert    the step/continue thread.  */
17365796c8dcSSimon Schubert static void
set_thread(struct ptid ptid,int gen)17375796c8dcSSimon Schubert set_thread (struct ptid ptid, int gen)
17385796c8dcSSimon Schubert {
17395796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
17405796c8dcSSimon Schubert   ptid_t state = gen ? general_thread : continue_thread;
17415796c8dcSSimon Schubert   char *buf = rs->buf;
17425796c8dcSSimon Schubert   char *endbuf = rs->buf + get_remote_packet_size ();
17435796c8dcSSimon Schubert 
17445796c8dcSSimon Schubert   if (ptid_equal (state, ptid))
17455796c8dcSSimon Schubert     return;
17465796c8dcSSimon Schubert 
17475796c8dcSSimon Schubert   *buf++ = 'H';
17485796c8dcSSimon Schubert   *buf++ = gen ? 'g' : 'c';
17495796c8dcSSimon Schubert   if (ptid_equal (ptid, magic_null_ptid))
17505796c8dcSSimon Schubert     xsnprintf (buf, endbuf - buf, "0");
17515796c8dcSSimon Schubert   else if (ptid_equal (ptid, any_thread_ptid))
17525796c8dcSSimon Schubert     xsnprintf (buf, endbuf - buf, "0");
17535796c8dcSSimon Schubert   else if (ptid_equal (ptid, minus_one_ptid))
17545796c8dcSSimon Schubert     xsnprintf (buf, endbuf - buf, "-1");
17555796c8dcSSimon Schubert   else
17565796c8dcSSimon Schubert     write_ptid (buf, endbuf, ptid);
17575796c8dcSSimon Schubert   putpkt (rs->buf);
17585796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
17595796c8dcSSimon Schubert   if (gen)
17605796c8dcSSimon Schubert     general_thread = ptid;
17615796c8dcSSimon Schubert   else
17625796c8dcSSimon Schubert     continue_thread = ptid;
17635796c8dcSSimon Schubert }
17645796c8dcSSimon Schubert 
17655796c8dcSSimon Schubert static void
set_general_thread(struct ptid ptid)17665796c8dcSSimon Schubert set_general_thread (struct ptid ptid)
17675796c8dcSSimon Schubert {
17685796c8dcSSimon Schubert   set_thread (ptid, 1);
17695796c8dcSSimon Schubert }
17705796c8dcSSimon Schubert 
17715796c8dcSSimon Schubert static void
set_continue_thread(struct ptid ptid)17725796c8dcSSimon Schubert set_continue_thread (struct ptid ptid)
17735796c8dcSSimon Schubert {
17745796c8dcSSimon Schubert   set_thread (ptid, 0);
17755796c8dcSSimon Schubert }
17765796c8dcSSimon Schubert 
17775796c8dcSSimon Schubert /* Change the remote current process.  Which thread within the process
17785796c8dcSSimon Schubert    ends up selected isn't important, as long as it is the same process
17795796c8dcSSimon Schubert    as what INFERIOR_PTID points to.
17805796c8dcSSimon Schubert 
17815796c8dcSSimon Schubert    This comes from that fact that there is no explicit notion of
17825796c8dcSSimon Schubert    "selected process" in the protocol.  The selected process for
17835796c8dcSSimon Schubert    general operations is the process the selected general thread
17845796c8dcSSimon Schubert    belongs to.  */
17855796c8dcSSimon Schubert 
17865796c8dcSSimon Schubert static void
set_general_process(void)17875796c8dcSSimon Schubert set_general_process (void)
17885796c8dcSSimon Schubert {
17895796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
17905796c8dcSSimon Schubert 
17915796c8dcSSimon Schubert   /* If the remote can't handle multiple processes, don't bother.  */
1792ef5ccd6cSJohn Marino   if (!rs->extended || !remote_multi_process_p (rs))
17935796c8dcSSimon Schubert     return;
17945796c8dcSSimon Schubert 
17955796c8dcSSimon Schubert   /* We only need to change the remote current thread if it's pointing
17965796c8dcSSimon Schubert      at some other process.  */
17975796c8dcSSimon Schubert   if (ptid_get_pid (general_thread) != ptid_get_pid (inferior_ptid))
17985796c8dcSSimon Schubert     set_general_thread (inferior_ptid);
17995796c8dcSSimon Schubert }
18005796c8dcSSimon Schubert 
18015796c8dcSSimon Schubert 
18025796c8dcSSimon Schubert /*  Return nonzero if the thread PTID is still alive on the remote
18035796c8dcSSimon Schubert     system.  */
18045796c8dcSSimon Schubert 
18055796c8dcSSimon Schubert static int
remote_thread_alive(struct target_ops * ops,ptid_t ptid)18065796c8dcSSimon Schubert remote_thread_alive (struct target_ops *ops, ptid_t ptid)
18075796c8dcSSimon Schubert {
18085796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
18095796c8dcSSimon Schubert   char *p, *endp;
18105796c8dcSSimon Schubert 
18115796c8dcSSimon Schubert   if (ptid_equal (ptid, magic_null_ptid))
18125796c8dcSSimon Schubert     /* The main thread is always alive.  */
18135796c8dcSSimon Schubert     return 1;
18145796c8dcSSimon Schubert 
18155796c8dcSSimon Schubert   if (ptid_get_pid (ptid) != 0 && ptid_get_tid (ptid) == 0)
18165796c8dcSSimon Schubert     /* The main thread is always alive.  This can happen after a
18175796c8dcSSimon Schubert        vAttach, if the remote side doesn't support
18185796c8dcSSimon Schubert        multi-threading.  */
18195796c8dcSSimon Schubert     return 1;
18205796c8dcSSimon Schubert 
18215796c8dcSSimon Schubert   p = rs->buf;
18225796c8dcSSimon Schubert   endp = rs->buf + get_remote_packet_size ();
18235796c8dcSSimon Schubert 
18245796c8dcSSimon Schubert   *p++ = 'T';
18255796c8dcSSimon Schubert   write_ptid (p, endp, ptid);
18265796c8dcSSimon Schubert 
18275796c8dcSSimon Schubert   putpkt (rs->buf);
18285796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
18295796c8dcSSimon Schubert   return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
18305796c8dcSSimon Schubert }
18315796c8dcSSimon Schubert 
18325796c8dcSSimon Schubert /* About these extended threadlist and threadinfo packets.  They are
18335796c8dcSSimon Schubert    variable length packets but, the fields within them are often fixed
18345796c8dcSSimon Schubert    length.  They are redundent enough to send over UDP as is the
18355796c8dcSSimon Schubert    remote protocol in general.  There is a matching unit test module
18365796c8dcSSimon Schubert    in libstub.  */
18375796c8dcSSimon Schubert 
18385796c8dcSSimon Schubert #define OPAQUETHREADBYTES 8
18395796c8dcSSimon Schubert 
18405796c8dcSSimon Schubert /* a 64 bit opaque identifier */
18415796c8dcSSimon Schubert typedef unsigned char threadref[OPAQUETHREADBYTES];
18425796c8dcSSimon Schubert 
18435796c8dcSSimon Schubert /* WARNING: This threadref data structure comes from the remote O.S.,
1844c50c785cSJohn Marino    libstub protocol encoding, and remote.c.  It is not particularly
18455796c8dcSSimon Schubert    changable.  */
18465796c8dcSSimon Schubert 
18475796c8dcSSimon Schubert /* Right now, the internal structure is int. We want it to be bigger.
1848c50c785cSJohn Marino    Plan to fix this.  */
18495796c8dcSSimon Schubert 
18505796c8dcSSimon Schubert typedef int gdb_threadref;	/* Internal GDB thread reference.  */
18515796c8dcSSimon Schubert 
18525796c8dcSSimon Schubert /* gdb_ext_thread_info is an internal GDB data structure which is
18535796c8dcSSimon Schubert    equivalent to the reply of the remote threadinfo packet.  */
18545796c8dcSSimon Schubert 
18555796c8dcSSimon Schubert struct gdb_ext_thread_info
18565796c8dcSSimon Schubert   {
18575796c8dcSSimon Schubert     threadref threadid;		/* External form of thread reference.  */
18585796c8dcSSimon Schubert     int active;			/* Has state interesting to GDB?
18595796c8dcSSimon Schubert 				   regs, stack.  */
18605796c8dcSSimon Schubert     char display[256];		/* Brief state display, name,
18615796c8dcSSimon Schubert 				   blocked/suspended.  */
18625796c8dcSSimon Schubert     char shortname[32];		/* To be used to name threads.  */
18635796c8dcSSimon Schubert     char more_display[256];	/* Long info, statistics, queue depth,
18645796c8dcSSimon Schubert 				   whatever.  */
18655796c8dcSSimon Schubert   };
18665796c8dcSSimon Schubert 
18675796c8dcSSimon Schubert /* The volume of remote transfers can be limited by submitting
18685796c8dcSSimon Schubert    a mask containing bits specifying the desired information.
18695796c8dcSSimon Schubert    Use a union of these values as the 'selection' parameter to
1870c50c785cSJohn Marino    get_thread_info.  FIXME: Make these TAG names more thread specific.  */
18715796c8dcSSimon Schubert 
18725796c8dcSSimon Schubert #define TAG_THREADID 1
18735796c8dcSSimon Schubert #define TAG_EXISTS 2
18745796c8dcSSimon Schubert #define TAG_DISPLAY 4
18755796c8dcSSimon Schubert #define TAG_THREADNAME 8
18765796c8dcSSimon Schubert #define TAG_MOREDISPLAY 16
18775796c8dcSSimon Schubert 
18785796c8dcSSimon Schubert #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
18795796c8dcSSimon Schubert 
18805796c8dcSSimon Schubert char *unpack_varlen_hex (char *buff, ULONGEST *result);
18815796c8dcSSimon Schubert 
18825796c8dcSSimon Schubert static char *unpack_nibble (char *buf, int *val);
18835796c8dcSSimon Schubert 
18845796c8dcSSimon Schubert static char *pack_nibble (char *buf, int nibble);
18855796c8dcSSimon Schubert 
18865796c8dcSSimon Schubert static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte);
18875796c8dcSSimon Schubert 
18885796c8dcSSimon Schubert static char *unpack_byte (char *buf, int *value);
18895796c8dcSSimon Schubert 
18905796c8dcSSimon Schubert static char *pack_int (char *buf, int value);
18915796c8dcSSimon Schubert 
18925796c8dcSSimon Schubert static char *unpack_int (char *buf, int *value);
18935796c8dcSSimon Schubert 
18945796c8dcSSimon Schubert static char *unpack_string (char *src, char *dest, int length);
18955796c8dcSSimon Schubert 
18965796c8dcSSimon Schubert static char *pack_threadid (char *pkt, threadref *id);
18975796c8dcSSimon Schubert 
18985796c8dcSSimon Schubert static char *unpack_threadid (char *inbuf, threadref *id);
18995796c8dcSSimon Schubert 
19005796c8dcSSimon Schubert void int_to_threadref (threadref *id, int value);
19015796c8dcSSimon Schubert 
19025796c8dcSSimon Schubert static int threadref_to_int (threadref *ref);
19035796c8dcSSimon Schubert 
19045796c8dcSSimon Schubert static void copy_threadref (threadref *dest, threadref *src);
19055796c8dcSSimon Schubert 
19065796c8dcSSimon Schubert static int threadmatch (threadref *dest, threadref *src);
19075796c8dcSSimon Schubert 
19085796c8dcSSimon Schubert static char *pack_threadinfo_request (char *pkt, int mode,
19095796c8dcSSimon Schubert 				      threadref *id);
19105796c8dcSSimon Schubert 
19115796c8dcSSimon Schubert static int remote_unpack_thread_info_response (char *pkt,
19125796c8dcSSimon Schubert 					       threadref *expectedref,
19135796c8dcSSimon Schubert 					       struct gdb_ext_thread_info
19145796c8dcSSimon Schubert 					       *info);
19155796c8dcSSimon Schubert 
19165796c8dcSSimon Schubert 
19175796c8dcSSimon Schubert static int remote_get_threadinfo (threadref *threadid,
19185796c8dcSSimon Schubert 				  int fieldset,	/*TAG mask */
19195796c8dcSSimon Schubert 				  struct gdb_ext_thread_info *info);
19205796c8dcSSimon Schubert 
19215796c8dcSSimon Schubert static char *pack_threadlist_request (char *pkt, int startflag,
19225796c8dcSSimon Schubert 				      int threadcount,
19235796c8dcSSimon Schubert 				      threadref *nextthread);
19245796c8dcSSimon Schubert 
19255796c8dcSSimon Schubert static int parse_threadlist_response (char *pkt,
19265796c8dcSSimon Schubert 				      int result_limit,
19275796c8dcSSimon Schubert 				      threadref *original_echo,
19285796c8dcSSimon Schubert 				      threadref *resultlist,
19295796c8dcSSimon Schubert 				      int *doneflag);
19305796c8dcSSimon Schubert 
19315796c8dcSSimon Schubert static int remote_get_threadlist (int startflag,
19325796c8dcSSimon Schubert 				  threadref *nextthread,
19335796c8dcSSimon Schubert 				  int result_limit,
19345796c8dcSSimon Schubert 				  int *done,
19355796c8dcSSimon Schubert 				  int *result_count,
19365796c8dcSSimon Schubert 				  threadref *threadlist);
19375796c8dcSSimon Schubert 
19385796c8dcSSimon Schubert typedef int (*rmt_thread_action) (threadref *ref, void *context);
19395796c8dcSSimon Schubert 
19405796c8dcSSimon Schubert static int remote_threadlist_iterator (rmt_thread_action stepfunction,
19415796c8dcSSimon Schubert 				       void *context, int looplimit);
19425796c8dcSSimon Schubert 
19435796c8dcSSimon Schubert static int remote_newthread_step (threadref *ref, void *context);
19445796c8dcSSimon Schubert 
19455796c8dcSSimon Schubert 
19465796c8dcSSimon Schubert /* Write a PTID to BUF.  ENDBUF points to one-passed-the-end of the
19475796c8dcSSimon Schubert    buffer we're allowed to write to.  Returns
19485796c8dcSSimon Schubert    BUF+CHARACTERS_WRITTEN.  */
19495796c8dcSSimon Schubert 
19505796c8dcSSimon Schubert static char *
write_ptid(char * buf,const char * endbuf,ptid_t ptid)19515796c8dcSSimon Schubert write_ptid (char *buf, const char *endbuf, ptid_t ptid)
19525796c8dcSSimon Schubert {
19535796c8dcSSimon Schubert   int pid, tid;
19545796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
19555796c8dcSSimon Schubert 
19565796c8dcSSimon Schubert   if (remote_multi_process_p (rs))
19575796c8dcSSimon Schubert     {
19585796c8dcSSimon Schubert       pid = ptid_get_pid (ptid);
19595796c8dcSSimon Schubert       if (pid < 0)
19605796c8dcSSimon Schubert 	buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
19615796c8dcSSimon Schubert       else
19625796c8dcSSimon Schubert 	buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
19635796c8dcSSimon Schubert     }
19645796c8dcSSimon Schubert   tid = ptid_get_tid (ptid);
19655796c8dcSSimon Schubert   if (tid < 0)
19665796c8dcSSimon Schubert     buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
19675796c8dcSSimon Schubert   else
19685796c8dcSSimon Schubert     buf += xsnprintf (buf, endbuf - buf, "%x", tid);
19695796c8dcSSimon Schubert 
19705796c8dcSSimon Schubert   return buf;
19715796c8dcSSimon Schubert }
19725796c8dcSSimon Schubert 
19735796c8dcSSimon Schubert /* Extract a PTID from BUF.  If non-null, OBUF is set to the to one
19745796c8dcSSimon Schubert    passed the last parsed char.  Returns null_ptid on error.  */
19755796c8dcSSimon Schubert 
19765796c8dcSSimon Schubert static ptid_t
read_ptid(char * buf,char ** obuf)19775796c8dcSSimon Schubert read_ptid (char *buf, char **obuf)
19785796c8dcSSimon Schubert {
19795796c8dcSSimon Schubert   char *p = buf;
19805796c8dcSSimon Schubert   char *pp;
19815796c8dcSSimon Schubert   ULONGEST pid = 0, tid = 0;
19825796c8dcSSimon Schubert 
19835796c8dcSSimon Schubert   if (*p == 'p')
19845796c8dcSSimon Schubert     {
19855796c8dcSSimon Schubert       /* Multi-process ptid.  */
19865796c8dcSSimon Schubert       pp = unpack_varlen_hex (p + 1, &pid);
19875796c8dcSSimon Schubert       if (*pp != '.')
1988c50c785cSJohn Marino 	error (_("invalid remote ptid: %s"), p);
19895796c8dcSSimon Schubert 
19905796c8dcSSimon Schubert       p = pp;
19915796c8dcSSimon Schubert       pp = unpack_varlen_hex (p + 1, &tid);
19925796c8dcSSimon Schubert       if (obuf)
19935796c8dcSSimon Schubert 	*obuf = pp;
19945796c8dcSSimon Schubert       return ptid_build (pid, 0, tid);
19955796c8dcSSimon Schubert     }
19965796c8dcSSimon Schubert 
19975796c8dcSSimon Schubert   /* No multi-process.  Just a tid.  */
19985796c8dcSSimon Schubert   pp = unpack_varlen_hex (p, &tid);
19995796c8dcSSimon Schubert 
20005796c8dcSSimon Schubert   /* Since the stub is not sending a process id, then default to
20015796c8dcSSimon Schubert      what's in inferior_ptid, unless it's null at this point.  If so,
20025796c8dcSSimon Schubert      then since there's no way to know the pid of the reported
20035796c8dcSSimon Schubert      threads, use the magic number.  */
20045796c8dcSSimon Schubert   if (ptid_equal (inferior_ptid, null_ptid))
20055796c8dcSSimon Schubert     pid = ptid_get_pid (magic_null_ptid);
20065796c8dcSSimon Schubert   else
20075796c8dcSSimon Schubert     pid = ptid_get_pid (inferior_ptid);
20085796c8dcSSimon Schubert 
20095796c8dcSSimon Schubert   if (obuf)
20105796c8dcSSimon Schubert     *obuf = pp;
20115796c8dcSSimon Schubert   return ptid_build (pid, 0, tid);
20125796c8dcSSimon Schubert }
20135796c8dcSSimon Schubert 
20145796c8dcSSimon Schubert /* Encode 64 bits in 16 chars of hex.  */
20155796c8dcSSimon Schubert 
20165796c8dcSSimon Schubert static const char hexchars[] = "0123456789abcdef";
20175796c8dcSSimon Schubert 
20185796c8dcSSimon Schubert static int
ishex(int ch,int * val)20195796c8dcSSimon Schubert ishex (int ch, int *val)
20205796c8dcSSimon Schubert {
20215796c8dcSSimon Schubert   if ((ch >= 'a') && (ch <= 'f'))
20225796c8dcSSimon Schubert     {
20235796c8dcSSimon Schubert       *val = ch - 'a' + 10;
20245796c8dcSSimon Schubert       return 1;
20255796c8dcSSimon Schubert     }
20265796c8dcSSimon Schubert   if ((ch >= 'A') && (ch <= 'F'))
20275796c8dcSSimon Schubert     {
20285796c8dcSSimon Schubert       *val = ch - 'A' + 10;
20295796c8dcSSimon Schubert       return 1;
20305796c8dcSSimon Schubert     }
20315796c8dcSSimon Schubert   if ((ch >= '0') && (ch <= '9'))
20325796c8dcSSimon Schubert     {
20335796c8dcSSimon Schubert       *val = ch - '0';
20345796c8dcSSimon Schubert       return 1;
20355796c8dcSSimon Schubert     }
20365796c8dcSSimon Schubert   return 0;
20375796c8dcSSimon Schubert }
20385796c8dcSSimon Schubert 
20395796c8dcSSimon Schubert static int
stubhex(int ch)20405796c8dcSSimon Schubert stubhex (int ch)
20415796c8dcSSimon Schubert {
20425796c8dcSSimon Schubert   if (ch >= 'a' && ch <= 'f')
20435796c8dcSSimon Schubert     return ch - 'a' + 10;
20445796c8dcSSimon Schubert   if (ch >= '0' && ch <= '9')
20455796c8dcSSimon Schubert     return ch - '0';
20465796c8dcSSimon Schubert   if (ch >= 'A' && ch <= 'F')
20475796c8dcSSimon Schubert     return ch - 'A' + 10;
20485796c8dcSSimon Schubert   return -1;
20495796c8dcSSimon Schubert }
20505796c8dcSSimon Schubert 
20515796c8dcSSimon Schubert static int
stub_unpack_int(char * buff,int fieldlength)20525796c8dcSSimon Schubert stub_unpack_int (char *buff, int fieldlength)
20535796c8dcSSimon Schubert {
20545796c8dcSSimon Schubert   int nibble;
20555796c8dcSSimon Schubert   int retval = 0;
20565796c8dcSSimon Schubert 
20575796c8dcSSimon Schubert   while (fieldlength)
20585796c8dcSSimon Schubert     {
20595796c8dcSSimon Schubert       nibble = stubhex (*buff++);
20605796c8dcSSimon Schubert       retval |= nibble;
20615796c8dcSSimon Schubert       fieldlength--;
20625796c8dcSSimon Schubert       if (fieldlength)
20635796c8dcSSimon Schubert 	retval = retval << 4;
20645796c8dcSSimon Schubert     }
20655796c8dcSSimon Schubert   return retval;
20665796c8dcSSimon Schubert }
20675796c8dcSSimon Schubert 
20685796c8dcSSimon Schubert char *
unpack_varlen_hex(char * buff,ULONGEST * result)20695796c8dcSSimon Schubert unpack_varlen_hex (char *buff,	/* packet to parse */
20705796c8dcSSimon Schubert 		   ULONGEST *result)
20715796c8dcSSimon Schubert {
20725796c8dcSSimon Schubert   int nibble;
20735796c8dcSSimon Schubert   ULONGEST retval = 0;
20745796c8dcSSimon Schubert 
20755796c8dcSSimon Schubert   while (ishex (*buff, &nibble))
20765796c8dcSSimon Schubert     {
20775796c8dcSSimon Schubert       buff++;
20785796c8dcSSimon Schubert       retval = retval << 4;
20795796c8dcSSimon Schubert       retval |= nibble & 0x0f;
20805796c8dcSSimon Schubert     }
20815796c8dcSSimon Schubert   *result = retval;
20825796c8dcSSimon Schubert   return buff;
20835796c8dcSSimon Schubert }
20845796c8dcSSimon Schubert 
20855796c8dcSSimon Schubert static char *
unpack_nibble(char * buf,int * val)20865796c8dcSSimon Schubert unpack_nibble (char *buf, int *val)
20875796c8dcSSimon Schubert {
20885796c8dcSSimon Schubert   *val = fromhex (*buf++);
20895796c8dcSSimon Schubert   return buf;
20905796c8dcSSimon Schubert }
20915796c8dcSSimon Schubert 
20925796c8dcSSimon Schubert static char *
pack_nibble(char * buf,int nibble)20935796c8dcSSimon Schubert pack_nibble (char *buf, int nibble)
20945796c8dcSSimon Schubert {
20955796c8dcSSimon Schubert   *buf++ = hexchars[(nibble & 0x0f)];
20965796c8dcSSimon Schubert   return buf;
20975796c8dcSSimon Schubert }
20985796c8dcSSimon Schubert 
20995796c8dcSSimon Schubert static char *
pack_hex_byte(char * pkt,int byte)21005796c8dcSSimon Schubert pack_hex_byte (char *pkt, int byte)
21015796c8dcSSimon Schubert {
21025796c8dcSSimon Schubert   *pkt++ = hexchars[(byte >> 4) & 0xf];
21035796c8dcSSimon Schubert   *pkt++ = hexchars[(byte & 0xf)];
21045796c8dcSSimon Schubert   return pkt;
21055796c8dcSSimon Schubert }
21065796c8dcSSimon Schubert 
21075796c8dcSSimon Schubert static char *
unpack_byte(char * buf,int * value)21085796c8dcSSimon Schubert unpack_byte (char *buf, int *value)
21095796c8dcSSimon Schubert {
21105796c8dcSSimon Schubert   *value = stub_unpack_int (buf, 2);
21115796c8dcSSimon Schubert   return buf + 2;
21125796c8dcSSimon Schubert }
21135796c8dcSSimon Schubert 
21145796c8dcSSimon Schubert static char *
pack_int(char * buf,int value)21155796c8dcSSimon Schubert pack_int (char *buf, int value)
21165796c8dcSSimon Schubert {
21175796c8dcSSimon Schubert   buf = pack_hex_byte (buf, (value >> 24) & 0xff);
21185796c8dcSSimon Schubert   buf = pack_hex_byte (buf, (value >> 16) & 0xff);
21195796c8dcSSimon Schubert   buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
21205796c8dcSSimon Schubert   buf = pack_hex_byte (buf, (value & 0xff));
21215796c8dcSSimon Schubert   return buf;
21225796c8dcSSimon Schubert }
21235796c8dcSSimon Schubert 
21245796c8dcSSimon Schubert static char *
unpack_int(char * buf,int * value)21255796c8dcSSimon Schubert unpack_int (char *buf, int *value)
21265796c8dcSSimon Schubert {
21275796c8dcSSimon Schubert   *value = stub_unpack_int (buf, 8);
21285796c8dcSSimon Schubert   return buf + 8;
21295796c8dcSSimon Schubert }
21305796c8dcSSimon Schubert 
21315796c8dcSSimon Schubert #if 0			/* Currently unused, uncomment when needed.  */
21325796c8dcSSimon Schubert static char *pack_string (char *pkt, char *string);
21335796c8dcSSimon Schubert 
21345796c8dcSSimon Schubert static char *
21355796c8dcSSimon Schubert pack_string (char *pkt, char *string)
21365796c8dcSSimon Schubert {
21375796c8dcSSimon Schubert   char ch;
21385796c8dcSSimon Schubert   int len;
21395796c8dcSSimon Schubert 
21405796c8dcSSimon Schubert   len = strlen (string);
21415796c8dcSSimon Schubert   if (len > 200)
21425796c8dcSSimon Schubert     len = 200;		/* Bigger than most GDB packets, junk???  */
21435796c8dcSSimon Schubert   pkt = pack_hex_byte (pkt, len);
21445796c8dcSSimon Schubert   while (len-- > 0)
21455796c8dcSSimon Schubert     {
21465796c8dcSSimon Schubert       ch = *string++;
21475796c8dcSSimon Schubert       if ((ch == '\0') || (ch == '#'))
21485796c8dcSSimon Schubert 	ch = '*';		/* Protect encapsulation.  */
21495796c8dcSSimon Schubert       *pkt++ = ch;
21505796c8dcSSimon Schubert     }
21515796c8dcSSimon Schubert   return pkt;
21525796c8dcSSimon Schubert }
21535796c8dcSSimon Schubert #endif /* 0 (unused) */
21545796c8dcSSimon Schubert 
21555796c8dcSSimon Schubert static char *
unpack_string(char * src,char * dest,int length)21565796c8dcSSimon Schubert unpack_string (char *src, char *dest, int length)
21575796c8dcSSimon Schubert {
21585796c8dcSSimon Schubert   while (length--)
21595796c8dcSSimon Schubert     *dest++ = *src++;
21605796c8dcSSimon Schubert   *dest = '\0';
21615796c8dcSSimon Schubert   return src;
21625796c8dcSSimon Schubert }
21635796c8dcSSimon Schubert 
21645796c8dcSSimon Schubert static char *
pack_threadid(char * pkt,threadref * id)21655796c8dcSSimon Schubert pack_threadid (char *pkt, threadref *id)
21665796c8dcSSimon Schubert {
21675796c8dcSSimon Schubert   char *limit;
21685796c8dcSSimon Schubert   unsigned char *altid;
21695796c8dcSSimon Schubert 
21705796c8dcSSimon Schubert   altid = (unsigned char *) id;
21715796c8dcSSimon Schubert   limit = pkt + BUF_THREAD_ID_SIZE;
21725796c8dcSSimon Schubert   while (pkt < limit)
21735796c8dcSSimon Schubert     pkt = pack_hex_byte (pkt, *altid++);
21745796c8dcSSimon Schubert   return pkt;
21755796c8dcSSimon Schubert }
21765796c8dcSSimon Schubert 
21775796c8dcSSimon Schubert 
21785796c8dcSSimon Schubert static char *
unpack_threadid(char * inbuf,threadref * id)21795796c8dcSSimon Schubert unpack_threadid (char *inbuf, threadref *id)
21805796c8dcSSimon Schubert {
21815796c8dcSSimon Schubert   char *altref;
21825796c8dcSSimon Schubert   char *limit = inbuf + BUF_THREAD_ID_SIZE;
21835796c8dcSSimon Schubert   int x, y;
21845796c8dcSSimon Schubert 
21855796c8dcSSimon Schubert   altref = (char *) id;
21865796c8dcSSimon Schubert 
21875796c8dcSSimon Schubert   while (inbuf < limit)
21885796c8dcSSimon Schubert     {
21895796c8dcSSimon Schubert       x = stubhex (*inbuf++);
21905796c8dcSSimon Schubert       y = stubhex (*inbuf++);
21915796c8dcSSimon Schubert       *altref++ = (x << 4) | y;
21925796c8dcSSimon Schubert     }
21935796c8dcSSimon Schubert   return inbuf;
21945796c8dcSSimon Schubert }
21955796c8dcSSimon Schubert 
21965796c8dcSSimon Schubert /* Externally, threadrefs are 64 bits but internally, they are still
21975796c8dcSSimon Schubert    ints.  This is due to a mismatch of specifications.  We would like
21985796c8dcSSimon Schubert    to use 64bit thread references internally.  This is an adapter
21995796c8dcSSimon Schubert    function.  */
22005796c8dcSSimon Schubert 
22015796c8dcSSimon Schubert void
int_to_threadref(threadref * id,int value)22025796c8dcSSimon Schubert int_to_threadref (threadref *id, int value)
22035796c8dcSSimon Schubert {
22045796c8dcSSimon Schubert   unsigned char *scan;
22055796c8dcSSimon Schubert 
22065796c8dcSSimon Schubert   scan = (unsigned char *) id;
22075796c8dcSSimon Schubert   {
22085796c8dcSSimon Schubert     int i = 4;
22095796c8dcSSimon Schubert     while (i--)
22105796c8dcSSimon Schubert       *scan++ = 0;
22115796c8dcSSimon Schubert   }
22125796c8dcSSimon Schubert   *scan++ = (value >> 24) & 0xff;
22135796c8dcSSimon Schubert   *scan++ = (value >> 16) & 0xff;
22145796c8dcSSimon Schubert   *scan++ = (value >> 8) & 0xff;
22155796c8dcSSimon Schubert   *scan++ = (value & 0xff);
22165796c8dcSSimon Schubert }
22175796c8dcSSimon Schubert 
22185796c8dcSSimon Schubert static int
threadref_to_int(threadref * ref)22195796c8dcSSimon Schubert threadref_to_int (threadref *ref)
22205796c8dcSSimon Schubert {
22215796c8dcSSimon Schubert   int i, value = 0;
22225796c8dcSSimon Schubert   unsigned char *scan;
22235796c8dcSSimon Schubert 
22245796c8dcSSimon Schubert   scan = *ref;
22255796c8dcSSimon Schubert   scan += 4;
22265796c8dcSSimon Schubert   i = 4;
22275796c8dcSSimon Schubert   while (i-- > 0)
22285796c8dcSSimon Schubert     value = (value << 8) | ((*scan++) & 0xff);
22295796c8dcSSimon Schubert   return value;
22305796c8dcSSimon Schubert }
22315796c8dcSSimon Schubert 
22325796c8dcSSimon Schubert static void
copy_threadref(threadref * dest,threadref * src)22335796c8dcSSimon Schubert copy_threadref (threadref *dest, threadref *src)
22345796c8dcSSimon Schubert {
22355796c8dcSSimon Schubert   int i;
22365796c8dcSSimon Schubert   unsigned char *csrc, *cdest;
22375796c8dcSSimon Schubert 
22385796c8dcSSimon Schubert   csrc = (unsigned char *) src;
22395796c8dcSSimon Schubert   cdest = (unsigned char *) dest;
22405796c8dcSSimon Schubert   i = 8;
22415796c8dcSSimon Schubert   while (i--)
22425796c8dcSSimon Schubert     *cdest++ = *csrc++;
22435796c8dcSSimon Schubert }
22445796c8dcSSimon Schubert 
22455796c8dcSSimon Schubert static int
threadmatch(threadref * dest,threadref * src)22465796c8dcSSimon Schubert threadmatch (threadref *dest, threadref *src)
22475796c8dcSSimon Schubert {
22485796c8dcSSimon Schubert   /* Things are broken right now, so just assume we got a match.  */
22495796c8dcSSimon Schubert #if 0
22505796c8dcSSimon Schubert   unsigned char *srcp, *destp;
22515796c8dcSSimon Schubert   int i, result;
22525796c8dcSSimon Schubert   srcp = (char *) src;
22535796c8dcSSimon Schubert   destp = (char *) dest;
22545796c8dcSSimon Schubert 
22555796c8dcSSimon Schubert   result = 1;
22565796c8dcSSimon Schubert   while (i-- > 0)
22575796c8dcSSimon Schubert     result &= (*srcp++ == *destp++) ? 1 : 0;
22585796c8dcSSimon Schubert   return result;
22595796c8dcSSimon Schubert #endif
22605796c8dcSSimon Schubert   return 1;
22615796c8dcSSimon Schubert }
22625796c8dcSSimon Schubert 
22635796c8dcSSimon Schubert /*
22645796c8dcSSimon Schubert    threadid:1,        # always request threadid
22655796c8dcSSimon Schubert    context_exists:2,
22665796c8dcSSimon Schubert    display:4,
22675796c8dcSSimon Schubert    unique_name:8,
22685796c8dcSSimon Schubert    more_display:16
22695796c8dcSSimon Schubert  */
22705796c8dcSSimon Schubert 
22715796c8dcSSimon Schubert /* Encoding:  'Q':8,'P':8,mask:32,threadid:64 */
22725796c8dcSSimon Schubert 
22735796c8dcSSimon Schubert static char *
pack_threadinfo_request(char * pkt,int mode,threadref * id)22745796c8dcSSimon Schubert pack_threadinfo_request (char *pkt, int mode, threadref *id)
22755796c8dcSSimon Schubert {
22765796c8dcSSimon Schubert   *pkt++ = 'q';				/* Info Query */
22775796c8dcSSimon Schubert   *pkt++ = 'P';				/* process or thread info */
22785796c8dcSSimon Schubert   pkt = pack_int (pkt, mode);		/* mode */
22795796c8dcSSimon Schubert   pkt = pack_threadid (pkt, id);	/* threadid */
22805796c8dcSSimon Schubert   *pkt = '\0';				/* terminate */
22815796c8dcSSimon Schubert   return pkt;
22825796c8dcSSimon Schubert }
22835796c8dcSSimon Schubert 
22845796c8dcSSimon Schubert /* These values tag the fields in a thread info response packet.  */
22855796c8dcSSimon Schubert /* Tagging the fields allows us to request specific fields and to
22865796c8dcSSimon Schubert    add more fields as time goes by.  */
22875796c8dcSSimon Schubert 
22885796c8dcSSimon Schubert #define TAG_THREADID 1		/* Echo the thread identifier.  */
22895796c8dcSSimon Schubert #define TAG_EXISTS 2		/* Is this process defined enough to
22905796c8dcSSimon Schubert 				   fetch registers and its stack?  */
22915796c8dcSSimon Schubert #define TAG_DISPLAY 4		/* A short thing maybe to put on a window */
22925796c8dcSSimon Schubert #define TAG_THREADNAME 8	/* string, maps 1-to-1 with a thread is.  */
22935796c8dcSSimon Schubert #define TAG_MOREDISPLAY 16	/* Whatever the kernel wants to say about
22945796c8dcSSimon Schubert 				   the process.  */
22955796c8dcSSimon Schubert 
22965796c8dcSSimon Schubert static int
remote_unpack_thread_info_response(char * pkt,threadref * expectedref,struct gdb_ext_thread_info * info)22975796c8dcSSimon Schubert remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
22985796c8dcSSimon Schubert 				    struct gdb_ext_thread_info *info)
22995796c8dcSSimon Schubert {
23005796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
23015796c8dcSSimon Schubert   int mask, length;
23025796c8dcSSimon Schubert   int tag;
23035796c8dcSSimon Schubert   threadref ref;
23045796c8dcSSimon Schubert   char *limit = pkt + rs->buf_size; /* Plausible parsing limit.  */
23055796c8dcSSimon Schubert   int retval = 1;
23065796c8dcSSimon Schubert 
23075796c8dcSSimon Schubert   /* info->threadid = 0; FIXME: implement zero_threadref.  */
23085796c8dcSSimon Schubert   info->active = 0;
23095796c8dcSSimon Schubert   info->display[0] = '\0';
23105796c8dcSSimon Schubert   info->shortname[0] = '\0';
23115796c8dcSSimon Schubert   info->more_display[0] = '\0';
23125796c8dcSSimon Schubert 
23135796c8dcSSimon Schubert   /* Assume the characters indicating the packet type have been
23145796c8dcSSimon Schubert      stripped.  */
23155796c8dcSSimon Schubert   pkt = unpack_int (pkt, &mask);	/* arg mask */
23165796c8dcSSimon Schubert   pkt = unpack_threadid (pkt, &ref);
23175796c8dcSSimon Schubert 
23185796c8dcSSimon Schubert   if (mask == 0)
23195796c8dcSSimon Schubert     warning (_("Incomplete response to threadinfo request."));
23205796c8dcSSimon Schubert   if (!threadmatch (&ref, expectedref))
23215796c8dcSSimon Schubert     {			/* This is an answer to a different request.  */
23225796c8dcSSimon Schubert       warning (_("ERROR RMT Thread info mismatch."));
23235796c8dcSSimon Schubert       return 0;
23245796c8dcSSimon Schubert     }
23255796c8dcSSimon Schubert   copy_threadref (&info->threadid, &ref);
23265796c8dcSSimon Schubert 
23275796c8dcSSimon Schubert   /* Loop on tagged fields , try to bail if somthing goes wrong.  */
23285796c8dcSSimon Schubert 
23295796c8dcSSimon Schubert   /* Packets are terminated with nulls.  */
23305796c8dcSSimon Schubert   while ((pkt < limit) && mask && *pkt)
23315796c8dcSSimon Schubert     {
23325796c8dcSSimon Schubert       pkt = unpack_int (pkt, &tag);	/* tag */
23335796c8dcSSimon Schubert       pkt = unpack_byte (pkt, &length);	/* length */
23345796c8dcSSimon Schubert       if (!(tag & mask))		/* Tags out of synch with mask.  */
23355796c8dcSSimon Schubert 	{
23365796c8dcSSimon Schubert 	  warning (_("ERROR RMT: threadinfo tag mismatch."));
23375796c8dcSSimon Schubert 	  retval = 0;
23385796c8dcSSimon Schubert 	  break;
23395796c8dcSSimon Schubert 	}
23405796c8dcSSimon Schubert       if (tag == TAG_THREADID)
23415796c8dcSSimon Schubert 	{
23425796c8dcSSimon Schubert 	  if (length != 16)
23435796c8dcSSimon Schubert 	    {
23445796c8dcSSimon Schubert 	      warning (_("ERROR RMT: length of threadid is not 16."));
23455796c8dcSSimon Schubert 	      retval = 0;
23465796c8dcSSimon Schubert 	      break;
23475796c8dcSSimon Schubert 	    }
23485796c8dcSSimon Schubert 	  pkt = unpack_threadid (pkt, &ref);
23495796c8dcSSimon Schubert 	  mask = mask & ~TAG_THREADID;
23505796c8dcSSimon Schubert 	  continue;
23515796c8dcSSimon Schubert 	}
23525796c8dcSSimon Schubert       if (tag == TAG_EXISTS)
23535796c8dcSSimon Schubert 	{
23545796c8dcSSimon Schubert 	  info->active = stub_unpack_int (pkt, length);
23555796c8dcSSimon Schubert 	  pkt += length;
23565796c8dcSSimon Schubert 	  mask = mask & ~(TAG_EXISTS);
23575796c8dcSSimon Schubert 	  if (length > 8)
23585796c8dcSSimon Schubert 	    {
23595796c8dcSSimon Schubert 	      warning (_("ERROR RMT: 'exists' length too long."));
23605796c8dcSSimon Schubert 	      retval = 0;
23615796c8dcSSimon Schubert 	      break;
23625796c8dcSSimon Schubert 	    }
23635796c8dcSSimon Schubert 	  continue;
23645796c8dcSSimon Schubert 	}
23655796c8dcSSimon Schubert       if (tag == TAG_THREADNAME)
23665796c8dcSSimon Schubert 	{
23675796c8dcSSimon Schubert 	  pkt = unpack_string (pkt, &info->shortname[0], length);
23685796c8dcSSimon Schubert 	  mask = mask & ~TAG_THREADNAME;
23695796c8dcSSimon Schubert 	  continue;
23705796c8dcSSimon Schubert 	}
23715796c8dcSSimon Schubert       if (tag == TAG_DISPLAY)
23725796c8dcSSimon Schubert 	{
23735796c8dcSSimon Schubert 	  pkt = unpack_string (pkt, &info->display[0], length);
23745796c8dcSSimon Schubert 	  mask = mask & ~TAG_DISPLAY;
23755796c8dcSSimon Schubert 	  continue;
23765796c8dcSSimon Schubert 	}
23775796c8dcSSimon Schubert       if (tag == TAG_MOREDISPLAY)
23785796c8dcSSimon Schubert 	{
23795796c8dcSSimon Schubert 	  pkt = unpack_string (pkt, &info->more_display[0], length);
23805796c8dcSSimon Schubert 	  mask = mask & ~TAG_MOREDISPLAY;
23815796c8dcSSimon Schubert 	  continue;
23825796c8dcSSimon Schubert 	}
23835796c8dcSSimon Schubert       warning (_("ERROR RMT: unknown thread info tag."));
23845796c8dcSSimon Schubert       break;			/* Not a tag we know about.  */
23855796c8dcSSimon Schubert     }
23865796c8dcSSimon Schubert   return retval;
23875796c8dcSSimon Schubert }
23885796c8dcSSimon Schubert 
23895796c8dcSSimon Schubert static int
remote_get_threadinfo(threadref * threadid,int fieldset,struct gdb_ext_thread_info * info)23905796c8dcSSimon Schubert remote_get_threadinfo (threadref *threadid, int fieldset,	/* TAG mask */
23915796c8dcSSimon Schubert 		       struct gdb_ext_thread_info *info)
23925796c8dcSSimon Schubert {
23935796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
23945796c8dcSSimon Schubert   int result;
23955796c8dcSSimon Schubert 
23965796c8dcSSimon Schubert   pack_threadinfo_request (rs->buf, fieldset, threadid);
23975796c8dcSSimon Schubert   putpkt (rs->buf);
23985796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
23995796c8dcSSimon Schubert 
24005796c8dcSSimon Schubert   if (rs->buf[0] == '\0')
24015796c8dcSSimon Schubert     return 0;
24025796c8dcSSimon Schubert 
24035796c8dcSSimon Schubert   result = remote_unpack_thread_info_response (rs->buf + 2,
24045796c8dcSSimon Schubert 					       threadid, info);
24055796c8dcSSimon Schubert   return result;
24065796c8dcSSimon Schubert }
24075796c8dcSSimon Schubert 
24085796c8dcSSimon Schubert /*    Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32   */
24095796c8dcSSimon Schubert 
24105796c8dcSSimon Schubert static char *
pack_threadlist_request(char * pkt,int startflag,int threadcount,threadref * nextthread)24115796c8dcSSimon Schubert pack_threadlist_request (char *pkt, int startflag, int threadcount,
24125796c8dcSSimon Schubert 			 threadref *nextthread)
24135796c8dcSSimon Schubert {
24145796c8dcSSimon Schubert   *pkt++ = 'q';			/* info query packet */
24155796c8dcSSimon Schubert   *pkt++ = 'L';			/* Process LIST or threadLIST request */
24165796c8dcSSimon Schubert   pkt = pack_nibble (pkt, startflag);		/* initflag 1 bytes */
24175796c8dcSSimon Schubert   pkt = pack_hex_byte (pkt, threadcount);	/* threadcount 2 bytes */
24185796c8dcSSimon Schubert   pkt = pack_threadid (pkt, nextthread);	/* 64 bit thread identifier */
24195796c8dcSSimon Schubert   *pkt = '\0';
24205796c8dcSSimon Schubert   return pkt;
24215796c8dcSSimon Schubert }
24225796c8dcSSimon Schubert 
24235796c8dcSSimon Schubert /* Encoding:   'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
24245796c8dcSSimon Schubert 
24255796c8dcSSimon Schubert static int
parse_threadlist_response(char * pkt,int result_limit,threadref * original_echo,threadref * resultlist,int * doneflag)24265796c8dcSSimon Schubert parse_threadlist_response (char *pkt, int result_limit,
24275796c8dcSSimon Schubert 			   threadref *original_echo, threadref *resultlist,
24285796c8dcSSimon Schubert 			   int *doneflag)
24295796c8dcSSimon Schubert {
24305796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
24315796c8dcSSimon Schubert   char *limit;
24325796c8dcSSimon Schubert   int count, resultcount, done;
24335796c8dcSSimon Schubert 
24345796c8dcSSimon Schubert   resultcount = 0;
24355796c8dcSSimon Schubert   /* Assume the 'q' and 'M chars have been stripped.  */
24365796c8dcSSimon Schubert   limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
24375796c8dcSSimon Schubert   /* done parse past here */
24385796c8dcSSimon Schubert   pkt = unpack_byte (pkt, &count);	/* count field */
24395796c8dcSSimon Schubert   pkt = unpack_nibble (pkt, &done);
24405796c8dcSSimon Schubert   /* The first threadid is the argument threadid.  */
24415796c8dcSSimon Schubert   pkt = unpack_threadid (pkt, original_echo);	/* should match query packet */
24425796c8dcSSimon Schubert   while ((count-- > 0) && (pkt < limit))
24435796c8dcSSimon Schubert     {
24445796c8dcSSimon Schubert       pkt = unpack_threadid (pkt, resultlist++);
24455796c8dcSSimon Schubert       if (resultcount++ >= result_limit)
24465796c8dcSSimon Schubert 	break;
24475796c8dcSSimon Schubert     }
24485796c8dcSSimon Schubert   if (doneflag)
24495796c8dcSSimon Schubert     *doneflag = done;
24505796c8dcSSimon Schubert   return resultcount;
24515796c8dcSSimon Schubert }
24525796c8dcSSimon Schubert 
24535796c8dcSSimon Schubert static int
remote_get_threadlist(int startflag,threadref * nextthread,int result_limit,int * done,int * result_count,threadref * threadlist)24545796c8dcSSimon Schubert remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
24555796c8dcSSimon Schubert 		       int *done, int *result_count, threadref *threadlist)
24565796c8dcSSimon Schubert {
24575796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
24585796c8dcSSimon Schubert   static threadref echo_nextthread;
24595796c8dcSSimon Schubert   int result = 1;
24605796c8dcSSimon Schubert 
24615796c8dcSSimon Schubert   /* Trancate result limit to be smaller than the packet size.  */
2462c50c785cSJohn Marino   if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
2463c50c785cSJohn Marino       >= get_remote_packet_size ())
24645796c8dcSSimon Schubert     result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
24655796c8dcSSimon Schubert 
24665796c8dcSSimon Schubert   pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
24675796c8dcSSimon Schubert   putpkt (rs->buf);
24685796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
24695796c8dcSSimon Schubert 
24705796c8dcSSimon Schubert   if (*rs->buf == '\0')
2471c50c785cSJohn Marino     return 0;
24725796c8dcSSimon Schubert   else
24735796c8dcSSimon Schubert     *result_count =
24745796c8dcSSimon Schubert       parse_threadlist_response (rs->buf + 2, result_limit, &echo_nextthread,
24755796c8dcSSimon Schubert                                  threadlist, done);
24765796c8dcSSimon Schubert 
24775796c8dcSSimon Schubert   if (!threadmatch (&echo_nextthread, nextthread))
24785796c8dcSSimon Schubert     {
24795796c8dcSSimon Schubert       /* FIXME: This is a good reason to drop the packet.  */
24805796c8dcSSimon Schubert       /* Possably, there is a duplicate response.  */
24815796c8dcSSimon Schubert       /* Possabilities :
24825796c8dcSSimon Schubert          retransmit immediatly - race conditions
24835796c8dcSSimon Schubert          retransmit after timeout - yes
24845796c8dcSSimon Schubert          exit
24855796c8dcSSimon Schubert          wait for packet, then exit
24865796c8dcSSimon Schubert        */
24875796c8dcSSimon Schubert       warning (_("HMM: threadlist did not echo arg thread, dropping it."));
24885796c8dcSSimon Schubert       return 0;			/* I choose simply exiting.  */
24895796c8dcSSimon Schubert     }
24905796c8dcSSimon Schubert   if (*result_count <= 0)
24915796c8dcSSimon Schubert     {
24925796c8dcSSimon Schubert       if (*done != 1)
24935796c8dcSSimon Schubert 	{
24945796c8dcSSimon Schubert 	  warning (_("RMT ERROR : failed to get remote thread list."));
24955796c8dcSSimon Schubert 	  result = 0;
24965796c8dcSSimon Schubert 	}
24975796c8dcSSimon Schubert       return result;		/* break; */
24985796c8dcSSimon Schubert     }
24995796c8dcSSimon Schubert   if (*result_count > result_limit)
25005796c8dcSSimon Schubert     {
25015796c8dcSSimon Schubert       *result_count = 0;
25025796c8dcSSimon Schubert       warning (_("RMT ERROR: threadlist response longer than requested."));
25035796c8dcSSimon Schubert       return 0;
25045796c8dcSSimon Schubert     }
25055796c8dcSSimon Schubert   return result;
25065796c8dcSSimon Schubert }
25075796c8dcSSimon Schubert 
25085796c8dcSSimon Schubert /* This is the interface between remote and threads, remotes upper
25095796c8dcSSimon Schubert    interface.  */
25105796c8dcSSimon Schubert 
25115796c8dcSSimon Schubert /* remote_find_new_threads retrieves the thread list and for each
25125796c8dcSSimon Schubert    thread in the list, looks up the thread in GDB's internal list,
25135796c8dcSSimon Schubert    adding the thread if it does not already exist.  This involves
25145796c8dcSSimon Schubert    getting partial thread lists from the remote target so, polling the
25155796c8dcSSimon Schubert    quit_flag is required.  */
25165796c8dcSSimon Schubert 
25175796c8dcSSimon Schubert 
25185796c8dcSSimon Schubert /* About this many threadisds fit in a packet.  */
25195796c8dcSSimon Schubert 
25205796c8dcSSimon Schubert #define MAXTHREADLISTRESULTS 32
25215796c8dcSSimon Schubert 
25225796c8dcSSimon Schubert static int
remote_threadlist_iterator(rmt_thread_action stepfunction,void * context,int looplimit)25235796c8dcSSimon Schubert remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
25245796c8dcSSimon Schubert 			    int looplimit)
25255796c8dcSSimon Schubert {
25265796c8dcSSimon Schubert   int done, i, result_count;
25275796c8dcSSimon Schubert   int startflag = 1;
25285796c8dcSSimon Schubert   int result = 1;
25295796c8dcSSimon Schubert   int loopcount = 0;
25305796c8dcSSimon Schubert   static threadref nextthread;
25315796c8dcSSimon Schubert   static threadref resultthreadlist[MAXTHREADLISTRESULTS];
25325796c8dcSSimon Schubert 
25335796c8dcSSimon Schubert   done = 0;
25345796c8dcSSimon Schubert   while (!done)
25355796c8dcSSimon Schubert     {
25365796c8dcSSimon Schubert       if (loopcount++ > looplimit)
25375796c8dcSSimon Schubert 	{
25385796c8dcSSimon Schubert 	  result = 0;
25395796c8dcSSimon Schubert 	  warning (_("Remote fetch threadlist -infinite loop-."));
25405796c8dcSSimon Schubert 	  break;
25415796c8dcSSimon Schubert 	}
25425796c8dcSSimon Schubert       if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
25435796c8dcSSimon Schubert 				  &done, &result_count, resultthreadlist))
25445796c8dcSSimon Schubert 	{
25455796c8dcSSimon Schubert 	  result = 0;
25465796c8dcSSimon Schubert 	  break;
25475796c8dcSSimon Schubert 	}
25485796c8dcSSimon Schubert       /* Clear for later iterations.  */
25495796c8dcSSimon Schubert       startflag = 0;
25505796c8dcSSimon Schubert       /* Setup to resume next batch of thread references, set nextthread.  */
25515796c8dcSSimon Schubert       if (result_count >= 1)
25525796c8dcSSimon Schubert 	copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
25535796c8dcSSimon Schubert       i = 0;
25545796c8dcSSimon Schubert       while (result_count--)
25555796c8dcSSimon Schubert 	if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
25565796c8dcSSimon Schubert 	  break;
25575796c8dcSSimon Schubert     }
25585796c8dcSSimon Schubert   return result;
25595796c8dcSSimon Schubert }
25605796c8dcSSimon Schubert 
25615796c8dcSSimon Schubert static int
remote_newthread_step(threadref * ref,void * context)25625796c8dcSSimon Schubert remote_newthread_step (threadref *ref, void *context)
25635796c8dcSSimon Schubert {
25645796c8dcSSimon Schubert   int pid = ptid_get_pid (inferior_ptid);
25655796c8dcSSimon Schubert   ptid_t ptid = ptid_build (pid, 0, threadref_to_int (ref));
25665796c8dcSSimon Schubert 
25675796c8dcSSimon Schubert   if (!in_thread_list (ptid))
25685796c8dcSSimon Schubert     add_thread (ptid);
25695796c8dcSSimon Schubert   return 1;			/* continue iterator */
25705796c8dcSSimon Schubert }
25715796c8dcSSimon Schubert 
25725796c8dcSSimon Schubert #define CRAZY_MAX_THREADS 1000
25735796c8dcSSimon Schubert 
25745796c8dcSSimon Schubert static ptid_t
remote_current_thread(ptid_t oldpid)25755796c8dcSSimon Schubert remote_current_thread (ptid_t oldpid)
25765796c8dcSSimon Schubert {
25775796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
25785796c8dcSSimon Schubert 
25795796c8dcSSimon Schubert   putpkt ("qC");
25805796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
25815796c8dcSSimon Schubert   if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
25825796c8dcSSimon Schubert     return read_ptid (&rs->buf[2], NULL);
25835796c8dcSSimon Schubert   else
25845796c8dcSSimon Schubert     return oldpid;
25855796c8dcSSimon Schubert }
25865796c8dcSSimon Schubert 
25875796c8dcSSimon Schubert /* Find new threads for info threads command.
25885796c8dcSSimon Schubert  * Original version, using John Metzler's thread protocol.
25895796c8dcSSimon Schubert  */
25905796c8dcSSimon Schubert 
25915796c8dcSSimon Schubert static void
remote_find_new_threads(void)25925796c8dcSSimon Schubert remote_find_new_threads (void)
25935796c8dcSSimon Schubert {
25945796c8dcSSimon Schubert   remote_threadlist_iterator (remote_newthread_step, 0,
25955796c8dcSSimon Schubert 			      CRAZY_MAX_THREADS);
25965796c8dcSSimon Schubert }
25975796c8dcSSimon Schubert 
2598cf7f2e2dSJohn Marino #if defined(HAVE_LIBEXPAT)
2599cf7f2e2dSJohn Marino 
2600cf7f2e2dSJohn Marino typedef struct thread_item
2601cf7f2e2dSJohn Marino {
2602cf7f2e2dSJohn Marino   ptid_t ptid;
2603cf7f2e2dSJohn Marino   char *extra;
2604cf7f2e2dSJohn Marino   int core;
2605cf7f2e2dSJohn Marino } thread_item_t;
2606cf7f2e2dSJohn Marino DEF_VEC_O(thread_item_t);
2607cf7f2e2dSJohn Marino 
2608cf7f2e2dSJohn Marino struct threads_parsing_context
2609cf7f2e2dSJohn Marino {
2610cf7f2e2dSJohn Marino   VEC (thread_item_t) *items;
2611cf7f2e2dSJohn Marino };
2612cf7f2e2dSJohn Marino 
2613cf7f2e2dSJohn Marino static void
start_thread(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,VEC (gdb_xml_value_s)* attributes)2614cf7f2e2dSJohn Marino start_thread (struct gdb_xml_parser *parser,
2615cf7f2e2dSJohn Marino 	      const struct gdb_xml_element *element,
2616cf7f2e2dSJohn Marino 	      void *user_data, VEC(gdb_xml_value_s) *attributes)
2617cf7f2e2dSJohn Marino {
2618cf7f2e2dSJohn Marino   struct threads_parsing_context *data = user_data;
2619cf7f2e2dSJohn Marino 
2620cf7f2e2dSJohn Marino   struct thread_item item;
2621cf7f2e2dSJohn Marino   char *id;
2622c50c785cSJohn Marino   struct gdb_xml_value *attr;
2623cf7f2e2dSJohn Marino 
2624c50c785cSJohn Marino   id = xml_find_attribute (attributes, "id")->value;
2625cf7f2e2dSJohn Marino   item.ptid = read_ptid (id, NULL);
2626cf7f2e2dSJohn Marino 
2627c50c785cSJohn Marino   attr = xml_find_attribute (attributes, "core");
2628c50c785cSJohn Marino   if (attr != NULL)
2629c50c785cSJohn Marino     item.core = *(ULONGEST *) attr->value;
2630cf7f2e2dSJohn Marino   else
2631cf7f2e2dSJohn Marino     item.core = -1;
2632cf7f2e2dSJohn Marino 
2633cf7f2e2dSJohn Marino   item.extra = 0;
2634cf7f2e2dSJohn Marino 
2635cf7f2e2dSJohn Marino   VEC_safe_push (thread_item_t, data->items, &item);
2636cf7f2e2dSJohn Marino }
2637cf7f2e2dSJohn Marino 
2638cf7f2e2dSJohn Marino static void
end_thread(struct gdb_xml_parser * parser,const struct gdb_xml_element * element,void * user_data,const char * body_text)2639cf7f2e2dSJohn Marino end_thread (struct gdb_xml_parser *parser,
2640cf7f2e2dSJohn Marino 	    const struct gdb_xml_element *element,
2641cf7f2e2dSJohn Marino 	    void *user_data, const char *body_text)
2642cf7f2e2dSJohn Marino {
2643cf7f2e2dSJohn Marino   struct threads_parsing_context *data = user_data;
2644cf7f2e2dSJohn Marino 
2645cf7f2e2dSJohn Marino   if (body_text && *body_text)
2646cf7f2e2dSJohn Marino     VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text);
2647cf7f2e2dSJohn Marino }
2648cf7f2e2dSJohn Marino 
2649cf7f2e2dSJohn Marino const struct gdb_xml_attribute thread_attributes[] = {
2650cf7f2e2dSJohn Marino   { "id", GDB_XML_AF_NONE, NULL, NULL },
2651cf7f2e2dSJohn Marino   { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2652cf7f2e2dSJohn Marino   { NULL, GDB_XML_AF_NONE, NULL, NULL }
2653cf7f2e2dSJohn Marino };
2654cf7f2e2dSJohn Marino 
2655cf7f2e2dSJohn Marino const struct gdb_xml_element thread_children[] = {
2656cf7f2e2dSJohn Marino   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2657cf7f2e2dSJohn Marino };
2658cf7f2e2dSJohn Marino 
2659cf7f2e2dSJohn Marino const struct gdb_xml_element threads_children[] = {
2660cf7f2e2dSJohn Marino   { "thread", thread_attributes, thread_children,
2661cf7f2e2dSJohn Marino     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
2662cf7f2e2dSJohn Marino     start_thread, end_thread },
2663cf7f2e2dSJohn Marino   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2664cf7f2e2dSJohn Marino };
2665cf7f2e2dSJohn Marino 
2666cf7f2e2dSJohn Marino const struct gdb_xml_element threads_elements[] = {
2667cf7f2e2dSJohn Marino   { "threads", NULL, threads_children,
2668cf7f2e2dSJohn Marino     GDB_XML_EF_NONE, NULL, NULL },
2669cf7f2e2dSJohn Marino   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2670cf7f2e2dSJohn Marino };
2671cf7f2e2dSJohn Marino 
2672cf7f2e2dSJohn Marino /* Discard the contents of the constructed thread info context.  */
2673cf7f2e2dSJohn Marino 
2674cf7f2e2dSJohn Marino static void
clear_threads_parsing_context(void * p)2675cf7f2e2dSJohn Marino clear_threads_parsing_context (void *p)
2676cf7f2e2dSJohn Marino {
2677cf7f2e2dSJohn Marino   struct threads_parsing_context *context = p;
2678cf7f2e2dSJohn Marino   int i;
2679cf7f2e2dSJohn Marino   struct thread_item *item;
2680cf7f2e2dSJohn Marino 
2681cf7f2e2dSJohn Marino   for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i)
2682cf7f2e2dSJohn Marino     xfree (item->extra);
2683cf7f2e2dSJohn Marino 
2684cf7f2e2dSJohn Marino   VEC_free (thread_item_t, context->items);
2685cf7f2e2dSJohn Marino }
2686cf7f2e2dSJohn Marino 
2687cf7f2e2dSJohn Marino #endif
2688cf7f2e2dSJohn Marino 
26895796c8dcSSimon Schubert /*
26905796c8dcSSimon Schubert  * Find all threads for info threads command.
26915796c8dcSSimon Schubert  * Uses new thread protocol contributed by Cisco.
26925796c8dcSSimon Schubert  * Falls back and attempts to use the older method (above)
26935796c8dcSSimon Schubert  * if the target doesn't respond to the new method.
26945796c8dcSSimon Schubert  */
26955796c8dcSSimon Schubert 
26965796c8dcSSimon Schubert static void
remote_threads_info(struct target_ops * ops)26975796c8dcSSimon Schubert remote_threads_info (struct target_ops *ops)
26985796c8dcSSimon Schubert {
26995796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
27005796c8dcSSimon Schubert   char *bufp;
27015796c8dcSSimon Schubert   ptid_t new_thread;
27025796c8dcSSimon Schubert 
27035796c8dcSSimon Schubert   if (remote_desc == 0)		/* paranoia */
27045796c8dcSSimon Schubert     error (_("Command can only be used when connected to the remote target."));
27055796c8dcSSimon Schubert 
2706cf7f2e2dSJohn Marino #if defined(HAVE_LIBEXPAT)
2707cf7f2e2dSJohn Marino   if (remote_protocol_packets[PACKET_qXfer_threads].support == PACKET_ENABLE)
2708cf7f2e2dSJohn Marino     {
2709cf7f2e2dSJohn Marino       char *xml = target_read_stralloc (&current_target,
2710cf7f2e2dSJohn Marino 					 TARGET_OBJECT_THREADS, NULL);
2711cf7f2e2dSJohn Marino 
2712cf7f2e2dSJohn Marino       struct cleanup *back_to = make_cleanup (xfree, xml);
2713c50c785cSJohn Marino 
2714cf7f2e2dSJohn Marino       if (xml && *xml)
2715cf7f2e2dSJohn Marino 	{
2716cf7f2e2dSJohn Marino 	  struct threads_parsing_context context;
2717cf7f2e2dSJohn Marino 
2718c50c785cSJohn Marino 	  context.items = NULL;
2719c50c785cSJohn Marino 	  make_cleanup (clear_threads_parsing_context, &context);
2720cf7f2e2dSJohn Marino 
2721c50c785cSJohn Marino 	  if (gdb_xml_parse_quick (_("threads"), "threads.dtd",
2722c50c785cSJohn Marino 				   threads_elements, xml, &context) == 0)
2723cf7f2e2dSJohn Marino 	    {
2724cf7f2e2dSJohn Marino 	      int i;
2725cf7f2e2dSJohn Marino 	      struct thread_item *item;
2726cf7f2e2dSJohn Marino 
2727c50c785cSJohn Marino 	      for (i = 0;
2728c50c785cSJohn Marino 		   VEC_iterate (thread_item_t, context.items, i, item);
2729c50c785cSJohn Marino 		   ++i)
2730cf7f2e2dSJohn Marino 		{
2731cf7f2e2dSJohn Marino 		  if (!ptid_equal (item->ptid, null_ptid))
2732cf7f2e2dSJohn Marino 		    {
2733cf7f2e2dSJohn Marino 		      struct private_thread_info *info;
2734cf7f2e2dSJohn Marino 		      /* In non-stop mode, we assume new found threads
2735cf7f2e2dSJohn Marino 			 are running until proven otherwise with a
2736cf7f2e2dSJohn Marino 			 stop reply.  In all-stop, we can only get
2737cf7f2e2dSJohn Marino 			 here if all threads are stopped.  */
2738cf7f2e2dSJohn Marino 		      int running = non_stop ? 1 : 0;
2739cf7f2e2dSJohn Marino 
2740cf7f2e2dSJohn Marino 		      remote_notice_new_inferior (item->ptid, running);
2741cf7f2e2dSJohn Marino 
2742cf7f2e2dSJohn Marino 		      info = demand_private_info (item->ptid);
2743cf7f2e2dSJohn Marino 		      info->core = item->core;
2744cf7f2e2dSJohn Marino 		      info->extra = item->extra;
2745cf7f2e2dSJohn Marino 		      item->extra = NULL;
2746cf7f2e2dSJohn Marino 		    }
2747cf7f2e2dSJohn Marino 		}
2748cf7f2e2dSJohn Marino 	    }
2749cf7f2e2dSJohn Marino 	}
2750cf7f2e2dSJohn Marino 
2751cf7f2e2dSJohn Marino       do_cleanups (back_to);
2752cf7f2e2dSJohn Marino       return;
2753cf7f2e2dSJohn Marino     }
2754cf7f2e2dSJohn Marino #endif
2755cf7f2e2dSJohn Marino 
27565796c8dcSSimon Schubert   if (use_threadinfo_query)
27575796c8dcSSimon Schubert     {
27585796c8dcSSimon Schubert       putpkt ("qfThreadInfo");
27595796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
27605796c8dcSSimon Schubert       bufp = rs->buf;
27615796c8dcSSimon Schubert       if (bufp[0] != '\0')		/* q packet recognized */
27625796c8dcSSimon Schubert 	{
2763ef5ccd6cSJohn Marino 	  struct cleanup *old_chain;
2764ef5ccd6cSJohn Marino 	  char *saved_reply;
2765ef5ccd6cSJohn Marino 
2766ef5ccd6cSJohn Marino 	  /* remote_notice_new_inferior (in the loop below) may make
2767ef5ccd6cSJohn Marino 	     new RSP calls, which clobber rs->buf.  Work with a
2768ef5ccd6cSJohn Marino 	     copy.  */
2769ef5ccd6cSJohn Marino 	  bufp = saved_reply = xstrdup (rs->buf);
2770ef5ccd6cSJohn Marino 	  old_chain = make_cleanup (free_current_contents, &saved_reply);
2771ef5ccd6cSJohn Marino 
27725796c8dcSSimon Schubert 	  while (*bufp++ == 'm')	/* reply contains one or more TID */
27735796c8dcSSimon Schubert 	    {
27745796c8dcSSimon Schubert 	      do
27755796c8dcSSimon Schubert 		{
27765796c8dcSSimon Schubert 		  new_thread = read_ptid (bufp, &bufp);
27775796c8dcSSimon Schubert 		  if (!ptid_equal (new_thread, null_ptid))
27785796c8dcSSimon Schubert 		    {
27795796c8dcSSimon Schubert 		      /* In non-stop mode, we assume new found threads
27805796c8dcSSimon Schubert 			 are running until proven otherwise with a
27815796c8dcSSimon Schubert 			 stop reply.  In all-stop, we can only get
27825796c8dcSSimon Schubert 			 here if all threads are stopped.  */
27835796c8dcSSimon Schubert 		      int running = non_stop ? 1 : 0;
27845796c8dcSSimon Schubert 
27855796c8dcSSimon Schubert 		      remote_notice_new_inferior (new_thread, running);
27865796c8dcSSimon Schubert 		    }
27875796c8dcSSimon Schubert 		}
27885796c8dcSSimon Schubert 	      while (*bufp++ == ',');	/* comma-separated list */
2789ef5ccd6cSJohn Marino 	      free_current_contents (&saved_reply);
27905796c8dcSSimon Schubert 	      putpkt ("qsThreadInfo");
27915796c8dcSSimon Schubert 	      getpkt (&rs->buf, &rs->buf_size, 0);
2792ef5ccd6cSJohn Marino 	      bufp = saved_reply = xstrdup (rs->buf);
27935796c8dcSSimon Schubert 	    }
2794ef5ccd6cSJohn Marino 	  do_cleanups (old_chain);
27955796c8dcSSimon Schubert 	  return;	/* done */
27965796c8dcSSimon Schubert 	}
27975796c8dcSSimon Schubert     }
27985796c8dcSSimon Schubert 
27995796c8dcSSimon Schubert   /* Only qfThreadInfo is supported in non-stop mode.  */
28005796c8dcSSimon Schubert   if (non_stop)
28015796c8dcSSimon Schubert     return;
28025796c8dcSSimon Schubert 
28035796c8dcSSimon Schubert   /* Else fall back to old method based on jmetzler protocol.  */
28045796c8dcSSimon Schubert   use_threadinfo_query = 0;
28055796c8dcSSimon Schubert   remote_find_new_threads ();
28065796c8dcSSimon Schubert   return;
28075796c8dcSSimon Schubert }
28085796c8dcSSimon Schubert 
28095796c8dcSSimon Schubert /*
28105796c8dcSSimon Schubert  * Collect a descriptive string about the given thread.
28115796c8dcSSimon Schubert  * The target may say anything it wants to about the thread
28125796c8dcSSimon Schubert  * (typically info about its blocked / runnable state, name, etc.).
28135796c8dcSSimon Schubert  * This string will appear in the info threads display.
28145796c8dcSSimon Schubert  *
28155796c8dcSSimon Schubert  * Optional: targets are not required to implement this function.
28165796c8dcSSimon Schubert  */
28175796c8dcSSimon Schubert 
28185796c8dcSSimon Schubert static char *
remote_threads_extra_info(struct thread_info * tp)28195796c8dcSSimon Schubert remote_threads_extra_info (struct thread_info *tp)
28205796c8dcSSimon Schubert {
28215796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
28225796c8dcSSimon Schubert   int result;
28235796c8dcSSimon Schubert   int set;
28245796c8dcSSimon Schubert   threadref id;
28255796c8dcSSimon Schubert   struct gdb_ext_thread_info threadinfo;
28265796c8dcSSimon Schubert   static char display_buf[100];	/* arbitrary...  */
28275796c8dcSSimon Schubert   int n = 0;                    /* position in display_buf */
28285796c8dcSSimon Schubert 
28295796c8dcSSimon Schubert   if (remote_desc == 0)		/* paranoia */
28305796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
28315796c8dcSSimon Schubert 		    _("remote_threads_extra_info"));
28325796c8dcSSimon Schubert 
28335796c8dcSSimon Schubert   if (ptid_equal (tp->ptid, magic_null_ptid)
28345796c8dcSSimon Schubert       || (ptid_get_pid (tp->ptid) != 0 && ptid_get_tid (tp->ptid) == 0))
28355796c8dcSSimon Schubert     /* This is the main thread which was added by GDB.  The remote
28365796c8dcSSimon Schubert        server doesn't know about it.  */
28375796c8dcSSimon Schubert     return NULL;
28385796c8dcSSimon Schubert 
2839cf7f2e2dSJohn Marino   if (remote_protocol_packets[PACKET_qXfer_threads].support == PACKET_ENABLE)
2840cf7f2e2dSJohn Marino     {
2841cf7f2e2dSJohn Marino       struct thread_info *info = find_thread_ptid (tp->ptid);
2842cf7f2e2dSJohn Marino 
2843cf7f2e2dSJohn Marino       if (info && info->private)
2844cf7f2e2dSJohn Marino 	return info->private->extra;
2845cf7f2e2dSJohn Marino       else
2846cf7f2e2dSJohn Marino 	return NULL;
2847cf7f2e2dSJohn Marino     }
2848cf7f2e2dSJohn Marino 
28495796c8dcSSimon Schubert   if (use_threadextra_query)
28505796c8dcSSimon Schubert     {
28515796c8dcSSimon Schubert       char *b = rs->buf;
28525796c8dcSSimon Schubert       char *endb = rs->buf + get_remote_packet_size ();
28535796c8dcSSimon Schubert 
28545796c8dcSSimon Schubert       xsnprintf (b, endb - b, "qThreadExtraInfo,");
28555796c8dcSSimon Schubert       b += strlen (b);
28565796c8dcSSimon Schubert       write_ptid (b, endb, tp->ptid);
28575796c8dcSSimon Schubert 
28585796c8dcSSimon Schubert       putpkt (rs->buf);
28595796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
28605796c8dcSSimon Schubert       if (rs->buf[0] != 0)
28615796c8dcSSimon Schubert 	{
28625796c8dcSSimon Schubert 	  n = min (strlen (rs->buf) / 2, sizeof (display_buf));
28635796c8dcSSimon Schubert 	  result = hex2bin (rs->buf, (gdb_byte *) display_buf, n);
28645796c8dcSSimon Schubert 	  display_buf [result] = '\0';
28655796c8dcSSimon Schubert 	  return display_buf;
28665796c8dcSSimon Schubert 	}
28675796c8dcSSimon Schubert     }
28685796c8dcSSimon Schubert 
28695796c8dcSSimon Schubert   /* If the above query fails, fall back to the old method.  */
28705796c8dcSSimon Schubert   use_threadextra_query = 0;
28715796c8dcSSimon Schubert   set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
28725796c8dcSSimon Schubert     | TAG_MOREDISPLAY | TAG_DISPLAY;
28735796c8dcSSimon Schubert   int_to_threadref (&id, ptid_get_tid (tp->ptid));
28745796c8dcSSimon Schubert   if (remote_get_threadinfo (&id, set, &threadinfo))
28755796c8dcSSimon Schubert     if (threadinfo.active)
28765796c8dcSSimon Schubert       {
28775796c8dcSSimon Schubert 	if (*threadinfo.shortname)
28785796c8dcSSimon Schubert 	  n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
28795796c8dcSSimon Schubert 			  " Name: %s,", threadinfo.shortname);
28805796c8dcSSimon Schubert 	if (*threadinfo.display)
28815796c8dcSSimon Schubert 	  n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
28825796c8dcSSimon Schubert 			  " State: %s,", threadinfo.display);
28835796c8dcSSimon Schubert 	if (*threadinfo.more_display)
28845796c8dcSSimon Schubert 	  n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
28855796c8dcSSimon Schubert 			  " Priority: %s", threadinfo.more_display);
28865796c8dcSSimon Schubert 
28875796c8dcSSimon Schubert 	if (n > 0)
28885796c8dcSSimon Schubert 	  {
28895796c8dcSSimon Schubert 	    /* For purely cosmetic reasons, clear up trailing commas.  */
28905796c8dcSSimon Schubert 	    if (',' == display_buf[n-1])
28915796c8dcSSimon Schubert 	      display_buf[n-1] = ' ';
28925796c8dcSSimon Schubert 	    return display_buf;
28935796c8dcSSimon Schubert 	  }
28945796c8dcSSimon Schubert       }
28955796c8dcSSimon Schubert   return NULL;
28965796c8dcSSimon Schubert }
28975796c8dcSSimon Schubert 
28985796c8dcSSimon Schubert 
2899cf7f2e2dSJohn Marino static int
remote_static_tracepoint_marker_at(CORE_ADDR addr,struct static_tracepoint_marker * marker)2900cf7f2e2dSJohn Marino remote_static_tracepoint_marker_at (CORE_ADDR addr,
2901cf7f2e2dSJohn Marino 				    struct static_tracepoint_marker *marker)
2902cf7f2e2dSJohn Marino {
2903cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
2904cf7f2e2dSJohn Marino   char *p = rs->buf;
2905cf7f2e2dSJohn Marino 
2906ef5ccd6cSJohn Marino   xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
2907cf7f2e2dSJohn Marino   p += strlen (p);
2908cf7f2e2dSJohn Marino   p += hexnumstr (p, addr);
2909cf7f2e2dSJohn Marino   putpkt (rs->buf);
2910cf7f2e2dSJohn Marino   getpkt (&rs->buf, &rs->buf_size, 0);
2911cf7f2e2dSJohn Marino   p = rs->buf;
2912cf7f2e2dSJohn Marino 
2913cf7f2e2dSJohn Marino   if (*p == 'E')
2914cf7f2e2dSJohn Marino     error (_("Remote failure reply: %s"), p);
2915cf7f2e2dSJohn Marino 
2916cf7f2e2dSJohn Marino   if (*p++ == 'm')
2917cf7f2e2dSJohn Marino     {
2918cf7f2e2dSJohn Marino       parse_static_tracepoint_marker_definition (p, &p, marker);
2919cf7f2e2dSJohn Marino       return 1;
2920cf7f2e2dSJohn Marino     }
2921cf7f2e2dSJohn Marino 
2922cf7f2e2dSJohn Marino   return 0;
2923cf7f2e2dSJohn Marino }
2924cf7f2e2dSJohn Marino 
VEC(static_tracepoint_marker_p)2925cf7f2e2dSJohn Marino static VEC(static_tracepoint_marker_p) *
2926cf7f2e2dSJohn Marino remote_static_tracepoint_markers_by_strid (const char *strid)
2927cf7f2e2dSJohn Marino {
2928cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
2929cf7f2e2dSJohn Marino   VEC(static_tracepoint_marker_p) *markers = NULL;
2930cf7f2e2dSJohn Marino   struct static_tracepoint_marker *marker = NULL;
2931cf7f2e2dSJohn Marino   struct cleanup *old_chain;
2932cf7f2e2dSJohn Marino   char *p;
2933cf7f2e2dSJohn Marino 
2934cf7f2e2dSJohn Marino   /* Ask for a first packet of static tracepoint marker
2935cf7f2e2dSJohn Marino      definition.  */
2936cf7f2e2dSJohn Marino   putpkt ("qTfSTM");
2937cf7f2e2dSJohn Marino   getpkt (&rs->buf, &rs->buf_size, 0);
2938cf7f2e2dSJohn Marino   p = rs->buf;
2939cf7f2e2dSJohn Marino   if (*p == 'E')
2940cf7f2e2dSJohn Marino     error (_("Remote failure reply: %s"), p);
2941cf7f2e2dSJohn Marino 
2942cf7f2e2dSJohn Marino   old_chain = make_cleanup (free_current_marker, &marker);
2943cf7f2e2dSJohn Marino 
2944cf7f2e2dSJohn Marino   while (*p++ == 'm')
2945cf7f2e2dSJohn Marino     {
2946cf7f2e2dSJohn Marino       if (marker == NULL)
2947cf7f2e2dSJohn Marino 	marker = XCNEW (struct static_tracepoint_marker);
2948cf7f2e2dSJohn Marino 
2949cf7f2e2dSJohn Marino       do
2950cf7f2e2dSJohn Marino 	{
2951cf7f2e2dSJohn Marino 	  parse_static_tracepoint_marker_definition (p, &p, marker);
2952cf7f2e2dSJohn Marino 
2953cf7f2e2dSJohn Marino 	  if (strid == NULL || strcmp (strid, marker->str_id) == 0)
2954cf7f2e2dSJohn Marino 	    {
2955cf7f2e2dSJohn Marino 	      VEC_safe_push (static_tracepoint_marker_p,
2956cf7f2e2dSJohn Marino 			     markers, marker);
2957cf7f2e2dSJohn Marino 	      marker = NULL;
2958cf7f2e2dSJohn Marino 	    }
2959cf7f2e2dSJohn Marino 	  else
2960cf7f2e2dSJohn Marino 	    {
2961cf7f2e2dSJohn Marino 	      release_static_tracepoint_marker (marker);
2962cf7f2e2dSJohn Marino 	      memset (marker, 0, sizeof (*marker));
2963cf7f2e2dSJohn Marino 	    }
2964cf7f2e2dSJohn Marino 	}
2965cf7f2e2dSJohn Marino       while (*p++ == ',');	/* comma-separated list */
2966cf7f2e2dSJohn Marino       /* Ask for another packet of static tracepoint definition.  */
2967cf7f2e2dSJohn Marino       putpkt ("qTsSTM");
2968cf7f2e2dSJohn Marino       getpkt (&rs->buf, &rs->buf_size, 0);
2969cf7f2e2dSJohn Marino       p = rs->buf;
2970cf7f2e2dSJohn Marino     }
2971cf7f2e2dSJohn Marino 
2972cf7f2e2dSJohn Marino   do_cleanups (old_chain);
2973cf7f2e2dSJohn Marino   return markers;
2974cf7f2e2dSJohn Marino }
2975cf7f2e2dSJohn Marino 
2976cf7f2e2dSJohn Marino 
2977cf7f2e2dSJohn Marino /* Implement the to_get_ada_task_ptid function for the remote targets.  */
2978cf7f2e2dSJohn Marino 
2979cf7f2e2dSJohn Marino static ptid_t
remote_get_ada_task_ptid(long lwp,long thread)2980cf7f2e2dSJohn Marino remote_get_ada_task_ptid (long lwp, long thread)
2981cf7f2e2dSJohn Marino {
2982cf7f2e2dSJohn Marino   return ptid_build (ptid_get_pid (inferior_ptid), 0, lwp);
2983cf7f2e2dSJohn Marino }
2984cf7f2e2dSJohn Marino 
2985cf7f2e2dSJohn Marino 
29865796c8dcSSimon Schubert /* Restart the remote side; this is an extended protocol operation.  */
29875796c8dcSSimon Schubert 
29885796c8dcSSimon Schubert static void
extended_remote_restart(void)29895796c8dcSSimon Schubert extended_remote_restart (void)
29905796c8dcSSimon Schubert {
29915796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
29925796c8dcSSimon Schubert 
29935796c8dcSSimon Schubert   /* Send the restart command; for reasons I don't understand the
29945796c8dcSSimon Schubert      remote side really expects a number after the "R".  */
29955796c8dcSSimon Schubert   xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
29965796c8dcSSimon Schubert   putpkt (rs->buf);
29975796c8dcSSimon Schubert 
29985796c8dcSSimon Schubert   remote_fileio_reset ();
29995796c8dcSSimon Schubert }
30005796c8dcSSimon Schubert 
30015796c8dcSSimon Schubert /* Clean up connection to a remote debugger.  */
30025796c8dcSSimon Schubert 
30035796c8dcSSimon Schubert static void
remote_close(int quitting)30045796c8dcSSimon Schubert remote_close (int quitting)
30055796c8dcSSimon Schubert {
30065796c8dcSSimon Schubert   if (remote_desc == NULL)
30075796c8dcSSimon Schubert     return; /* already closed */
30085796c8dcSSimon Schubert 
30095796c8dcSSimon Schubert   /* Make sure we leave stdin registered in the event loop, and we
30105796c8dcSSimon Schubert      don't leave the async SIGINT signal handler installed.  */
30115796c8dcSSimon Schubert   remote_terminal_ours ();
30125796c8dcSSimon Schubert 
30135796c8dcSSimon Schubert   serial_close (remote_desc);
30145796c8dcSSimon Schubert   remote_desc = NULL;
30155796c8dcSSimon Schubert 
30165796c8dcSSimon Schubert   /* We don't have a connection to the remote stub anymore.  Get rid
3017c50c785cSJohn Marino      of all the inferiors and their threads we were controlling.
3018c50c785cSJohn Marino      Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame
3019c50c785cSJohn Marino      will be unable to find the thread corresponding to (pid, 0, 0).  */
3020cf7f2e2dSJohn Marino   inferior_ptid = null_ptid;
3021c50c785cSJohn Marino   discard_all_inferiors ();
30225796c8dcSSimon Schubert 
3023ef5ccd6cSJohn Marino   /* Stop replies may from inferiors which are still unknown to GDB.
3024ef5ccd6cSJohn Marino      We are closing the remote target, so we should discard
3025ef5ccd6cSJohn Marino      everything, including the stop replies from GDB-unknown
3026ef5ccd6cSJohn Marino      inferiors.  */
3027ef5ccd6cSJohn Marino   discard_pending_stop_replies (NULL);
30285796c8dcSSimon Schubert 
30295796c8dcSSimon Schubert   if (remote_async_inferior_event_token)
30305796c8dcSSimon Schubert     delete_async_event_handler (&remote_async_inferior_event_token);
3031ef5ccd6cSJohn Marino 
3032ef5ccd6cSJohn Marino   remote_notif_unregister_async_event_handler ();
30335796c8dcSSimon Schubert }
30345796c8dcSSimon Schubert 
30355796c8dcSSimon Schubert /* Query the remote side for the text, data and bss offsets.  */
30365796c8dcSSimon Schubert 
30375796c8dcSSimon Schubert static void
get_offsets(void)30385796c8dcSSimon Schubert get_offsets (void)
30395796c8dcSSimon Schubert {
30405796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
30415796c8dcSSimon Schubert   char *buf;
30425796c8dcSSimon Schubert   char *ptr;
30435796c8dcSSimon Schubert   int lose, num_segments = 0, do_sections, do_segments;
30445796c8dcSSimon Schubert   CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
30455796c8dcSSimon Schubert   struct section_offsets *offs;
30465796c8dcSSimon Schubert   struct symfile_segment_data *data;
30475796c8dcSSimon Schubert 
30485796c8dcSSimon Schubert   if (symfile_objfile == NULL)
30495796c8dcSSimon Schubert     return;
30505796c8dcSSimon Schubert 
30515796c8dcSSimon Schubert   putpkt ("qOffsets");
30525796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
30535796c8dcSSimon Schubert   buf = rs->buf;
30545796c8dcSSimon Schubert 
30555796c8dcSSimon Schubert   if (buf[0] == '\000')
30565796c8dcSSimon Schubert     return;			/* Return silently.  Stub doesn't support
30575796c8dcSSimon Schubert 				   this command.  */
30585796c8dcSSimon Schubert   if (buf[0] == 'E')
30595796c8dcSSimon Schubert     {
30605796c8dcSSimon Schubert       warning (_("Remote failure reply: %s"), buf);
30615796c8dcSSimon Schubert       return;
30625796c8dcSSimon Schubert     }
30635796c8dcSSimon Schubert 
30645796c8dcSSimon Schubert   /* Pick up each field in turn.  This used to be done with scanf, but
30655796c8dcSSimon Schubert      scanf will make trouble if CORE_ADDR size doesn't match
30665796c8dcSSimon Schubert      conversion directives correctly.  The following code will work
30675796c8dcSSimon Schubert      with any size of CORE_ADDR.  */
30685796c8dcSSimon Schubert   text_addr = data_addr = bss_addr = 0;
30695796c8dcSSimon Schubert   ptr = buf;
30705796c8dcSSimon Schubert   lose = 0;
30715796c8dcSSimon Schubert 
30725796c8dcSSimon Schubert   if (strncmp (ptr, "Text=", 5) == 0)
30735796c8dcSSimon Schubert     {
30745796c8dcSSimon Schubert       ptr += 5;
30755796c8dcSSimon Schubert       /* Don't use strtol, could lose on big values.  */
30765796c8dcSSimon Schubert       while (*ptr && *ptr != ';')
30775796c8dcSSimon Schubert 	text_addr = (text_addr << 4) + fromhex (*ptr++);
30785796c8dcSSimon Schubert 
30795796c8dcSSimon Schubert       if (strncmp (ptr, ";Data=", 6) == 0)
30805796c8dcSSimon Schubert 	{
30815796c8dcSSimon Schubert 	  ptr += 6;
30825796c8dcSSimon Schubert 	  while (*ptr && *ptr != ';')
30835796c8dcSSimon Schubert 	    data_addr = (data_addr << 4) + fromhex (*ptr++);
30845796c8dcSSimon Schubert 	}
30855796c8dcSSimon Schubert       else
30865796c8dcSSimon Schubert 	lose = 1;
30875796c8dcSSimon Schubert 
30885796c8dcSSimon Schubert       if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
30895796c8dcSSimon Schubert 	{
30905796c8dcSSimon Schubert 	  ptr += 5;
30915796c8dcSSimon Schubert 	  while (*ptr && *ptr != ';')
30925796c8dcSSimon Schubert 	    bss_addr = (bss_addr << 4) + fromhex (*ptr++);
30935796c8dcSSimon Schubert 
30945796c8dcSSimon Schubert 	  if (bss_addr != data_addr)
30955796c8dcSSimon Schubert 	    warning (_("Target reported unsupported offsets: %s"), buf);
30965796c8dcSSimon Schubert 	}
30975796c8dcSSimon Schubert       else
30985796c8dcSSimon Schubert 	lose = 1;
30995796c8dcSSimon Schubert     }
31005796c8dcSSimon Schubert   else if (strncmp (ptr, "TextSeg=", 8) == 0)
31015796c8dcSSimon Schubert     {
31025796c8dcSSimon Schubert       ptr += 8;
31035796c8dcSSimon Schubert       /* Don't use strtol, could lose on big values.  */
31045796c8dcSSimon Schubert       while (*ptr && *ptr != ';')
31055796c8dcSSimon Schubert 	text_addr = (text_addr << 4) + fromhex (*ptr++);
31065796c8dcSSimon Schubert       num_segments = 1;
31075796c8dcSSimon Schubert 
31085796c8dcSSimon Schubert       if (strncmp (ptr, ";DataSeg=", 9) == 0)
31095796c8dcSSimon Schubert 	{
31105796c8dcSSimon Schubert 	  ptr += 9;
31115796c8dcSSimon Schubert 	  while (*ptr && *ptr != ';')
31125796c8dcSSimon Schubert 	    data_addr = (data_addr << 4) + fromhex (*ptr++);
31135796c8dcSSimon Schubert 	  num_segments++;
31145796c8dcSSimon Schubert 	}
31155796c8dcSSimon Schubert     }
31165796c8dcSSimon Schubert   else
31175796c8dcSSimon Schubert     lose = 1;
31185796c8dcSSimon Schubert 
31195796c8dcSSimon Schubert   if (lose)
31205796c8dcSSimon Schubert     error (_("Malformed response to offset query, %s"), buf);
31215796c8dcSSimon Schubert   else if (*ptr != '\0')
31225796c8dcSSimon Schubert     warning (_("Target reported unsupported offsets: %s"), buf);
31235796c8dcSSimon Schubert 
31245796c8dcSSimon Schubert   offs = ((struct section_offsets *)
31255796c8dcSSimon Schubert 	  alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
31265796c8dcSSimon Schubert   memcpy (offs, symfile_objfile->section_offsets,
31275796c8dcSSimon Schubert 	  SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
31285796c8dcSSimon Schubert 
31295796c8dcSSimon Schubert   data = get_symfile_segment_data (symfile_objfile->obfd);
31305796c8dcSSimon Schubert   do_segments = (data != NULL);
31315796c8dcSSimon Schubert   do_sections = num_segments == 0;
31325796c8dcSSimon Schubert 
31335796c8dcSSimon Schubert   if (num_segments > 0)
31345796c8dcSSimon Schubert     {
31355796c8dcSSimon Schubert       segments[0] = text_addr;
31365796c8dcSSimon Schubert       segments[1] = data_addr;
31375796c8dcSSimon Schubert     }
31385796c8dcSSimon Schubert   /* If we have two segments, we can still try to relocate everything
31395796c8dcSSimon Schubert      by assuming that the .text and .data offsets apply to the whole
31405796c8dcSSimon Schubert      text and data segments.  Convert the offsets given in the packet
31415796c8dcSSimon Schubert      to base addresses for symfile_map_offsets_to_segments.  */
31425796c8dcSSimon Schubert   else if (data && data->num_segments == 2)
31435796c8dcSSimon Schubert     {
31445796c8dcSSimon Schubert       segments[0] = data->segment_bases[0] + text_addr;
31455796c8dcSSimon Schubert       segments[1] = data->segment_bases[1] + data_addr;
31465796c8dcSSimon Schubert       num_segments = 2;
31475796c8dcSSimon Schubert     }
31485796c8dcSSimon Schubert   /* If the object file has only one segment, assume that it is text
31495796c8dcSSimon Schubert      rather than data; main programs with no writable data are rare,
31505796c8dcSSimon Schubert      but programs with no code are useless.  Of course the code might
31515796c8dcSSimon Schubert      have ended up in the data segment... to detect that we would need
31525796c8dcSSimon Schubert      the permissions here.  */
31535796c8dcSSimon Schubert   else if (data && data->num_segments == 1)
31545796c8dcSSimon Schubert     {
31555796c8dcSSimon Schubert       segments[0] = data->segment_bases[0] + text_addr;
31565796c8dcSSimon Schubert       num_segments = 1;
31575796c8dcSSimon Schubert     }
31585796c8dcSSimon Schubert   /* There's no way to relocate by segment.  */
31595796c8dcSSimon Schubert   else
31605796c8dcSSimon Schubert     do_segments = 0;
31615796c8dcSSimon Schubert 
31625796c8dcSSimon Schubert   if (do_segments)
31635796c8dcSSimon Schubert     {
31645796c8dcSSimon Schubert       int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
31655796c8dcSSimon Schubert 						 offs, num_segments, segments);
31665796c8dcSSimon Schubert 
31675796c8dcSSimon Schubert       if (ret == 0 && !do_sections)
3168c50c785cSJohn Marino 	error (_("Can not handle qOffsets TextSeg "
3169c50c785cSJohn Marino 		 "response with this symbol file"));
31705796c8dcSSimon Schubert 
31715796c8dcSSimon Schubert       if (ret > 0)
31725796c8dcSSimon Schubert 	do_sections = 0;
31735796c8dcSSimon Schubert     }
31745796c8dcSSimon Schubert 
31755796c8dcSSimon Schubert   if (data)
31765796c8dcSSimon Schubert     free_symfile_segment_data (data);
31775796c8dcSSimon Schubert 
31785796c8dcSSimon Schubert   if (do_sections)
31795796c8dcSSimon Schubert     {
31805796c8dcSSimon Schubert       offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
31815796c8dcSSimon Schubert 
3182c50c785cSJohn Marino       /* This is a temporary kludge to force data and bss to use the
3183c50c785cSJohn Marino 	 same offsets because that's what nlmconv does now.  The real
3184c50c785cSJohn Marino 	 solution requires changes to the stub and remote.c that I
3185c50c785cSJohn Marino 	 don't have time to do right now.  */
31865796c8dcSSimon Schubert 
31875796c8dcSSimon Schubert       offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
31885796c8dcSSimon Schubert       offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
31895796c8dcSSimon Schubert     }
31905796c8dcSSimon Schubert 
31915796c8dcSSimon Schubert   objfile_relocate (symfile_objfile, offs);
31925796c8dcSSimon Schubert }
31935796c8dcSSimon Schubert 
31945796c8dcSSimon Schubert /* Callback for iterate_over_threads.  Set the STOP_REQUESTED flags in
31955796c8dcSSimon Schubert    threads we know are stopped already.  This is used during the
31965796c8dcSSimon Schubert    initial remote connection in non-stop mode --- threads that are
31975796c8dcSSimon Schubert    reported as already being stopped are left stopped.  */
31985796c8dcSSimon Schubert 
31995796c8dcSSimon Schubert static int
set_stop_requested_callback(struct thread_info * thread,void * data)32005796c8dcSSimon Schubert set_stop_requested_callback (struct thread_info *thread, void *data)
32015796c8dcSSimon Schubert {
32025796c8dcSSimon Schubert   /* If we have a stop reply for this thread, it must be stopped.  */
32035796c8dcSSimon Schubert   if (peek_stop_reply (thread->ptid))
32045796c8dcSSimon Schubert     set_stop_requested (thread->ptid, 1);
32055796c8dcSSimon Schubert 
32065796c8dcSSimon Schubert   return 0;
32075796c8dcSSimon Schubert }
32085796c8dcSSimon Schubert 
3209cf7f2e2dSJohn Marino /* Send interrupt_sequence to remote target.  */
3210cf7f2e2dSJohn Marino static void
send_interrupt_sequence(void)3211c50c785cSJohn Marino send_interrupt_sequence (void)
3212cf7f2e2dSJohn Marino {
3213cf7f2e2dSJohn Marino   if (interrupt_sequence_mode == interrupt_sequence_control_c)
3214ef5ccd6cSJohn Marino     remote_serial_write ("\x03", 1);
3215cf7f2e2dSJohn Marino   else if (interrupt_sequence_mode == interrupt_sequence_break)
3216cf7f2e2dSJohn Marino     serial_send_break (remote_desc);
3217cf7f2e2dSJohn Marino   else if (interrupt_sequence_mode == interrupt_sequence_break_g)
3218cf7f2e2dSJohn Marino     {
3219cf7f2e2dSJohn Marino       serial_send_break (remote_desc);
3220ef5ccd6cSJohn Marino       remote_serial_write ("g", 1);
3221cf7f2e2dSJohn Marino     }
3222cf7f2e2dSJohn Marino   else
3223cf7f2e2dSJohn Marino     internal_error (__FILE__, __LINE__,
3224cf7f2e2dSJohn Marino 		    _("Invalid value for interrupt_sequence_mode: %s."),
3225cf7f2e2dSJohn Marino 		    interrupt_sequence_mode);
3226cf7f2e2dSJohn Marino }
3227cf7f2e2dSJohn Marino 
3228ef5ccd6cSJohn Marino 
3229ef5ccd6cSJohn Marino /* If STOP_REPLY is a T stop reply, look for the "thread" register,
3230ef5ccd6cSJohn Marino    and extract the PTID.  Returns NULL_PTID if not found.  */
3231ef5ccd6cSJohn Marino 
3232ef5ccd6cSJohn Marino static ptid_t
stop_reply_extract_thread(char * stop_reply)3233ef5ccd6cSJohn Marino stop_reply_extract_thread (char *stop_reply)
3234ef5ccd6cSJohn Marino {
3235ef5ccd6cSJohn Marino   if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
3236ef5ccd6cSJohn Marino     {
3237ef5ccd6cSJohn Marino       char *p;
3238ef5ccd6cSJohn Marino 
3239ef5ccd6cSJohn Marino       /* Txx r:val ; r:val (...)  */
3240ef5ccd6cSJohn Marino       p = &stop_reply[3];
3241ef5ccd6cSJohn Marino 
3242ef5ccd6cSJohn Marino       /* Look for "register" named "thread".  */
3243ef5ccd6cSJohn Marino       while (*p != '\0')
3244ef5ccd6cSJohn Marino 	{
3245ef5ccd6cSJohn Marino 	  char *p1;
3246ef5ccd6cSJohn Marino 
3247ef5ccd6cSJohn Marino 	  p1 = strchr (p, ':');
3248ef5ccd6cSJohn Marino 	  if (p1 == NULL)
3249ef5ccd6cSJohn Marino 	    return null_ptid;
3250ef5ccd6cSJohn Marino 
3251ef5ccd6cSJohn Marino 	  if (strncmp (p, "thread", p1 - p) == 0)
3252ef5ccd6cSJohn Marino 	    return read_ptid (++p1, &p);
3253ef5ccd6cSJohn Marino 
3254ef5ccd6cSJohn Marino 	  p1 = strchr (p, ';');
3255ef5ccd6cSJohn Marino 	  if (p1 == NULL)
3256ef5ccd6cSJohn Marino 	    return null_ptid;
3257ef5ccd6cSJohn Marino 	  p1++;
3258ef5ccd6cSJohn Marino 
3259ef5ccd6cSJohn Marino 	  p = p1;
3260ef5ccd6cSJohn Marino 	}
3261ef5ccd6cSJohn Marino     }
3262ef5ccd6cSJohn Marino 
3263ef5ccd6cSJohn Marino   return null_ptid;
3264ef5ccd6cSJohn Marino }
3265ef5ccd6cSJohn Marino 
3266ef5ccd6cSJohn Marino /* Query the remote target for which is the current thread/process,
3267ef5ccd6cSJohn Marino    add it to our tables, and update INFERIOR_PTID.  The caller is
3268ef5ccd6cSJohn Marino    responsible for setting the state such that the remote end is ready
3269ef5ccd6cSJohn Marino    to return the current thread.
3270ef5ccd6cSJohn Marino 
3271ef5ccd6cSJohn Marino    This function is called after handling the '?' or 'vRun' packets,
3272ef5ccd6cSJohn Marino    whose response is a stop reply from which we can also try
3273ef5ccd6cSJohn Marino    extracting the thread.  If the target doesn't support the explicit
3274ef5ccd6cSJohn Marino    qC query, we infer the current thread from that stop reply, passed
3275ef5ccd6cSJohn Marino    in in WAIT_STATUS, which may be NULL.  */
3276ef5ccd6cSJohn Marino 
3277ef5ccd6cSJohn Marino static void
add_current_inferior_and_thread(char * wait_status)3278ef5ccd6cSJohn Marino add_current_inferior_and_thread (char *wait_status)
3279ef5ccd6cSJohn Marino {
3280ef5ccd6cSJohn Marino   struct remote_state *rs = get_remote_state ();
3281ef5ccd6cSJohn Marino   int fake_pid_p = 0;
3282ef5ccd6cSJohn Marino   ptid_t ptid = null_ptid;
3283ef5ccd6cSJohn Marino 
3284ef5ccd6cSJohn Marino   inferior_ptid = null_ptid;
3285ef5ccd6cSJohn Marino 
3286ef5ccd6cSJohn Marino   /* Now, if we have thread information, update inferior_ptid.  First
3287ef5ccd6cSJohn Marino      if we have a stop reply handy, maybe it's a T stop reply with a
3288ef5ccd6cSJohn Marino      "thread" register we can extract the current thread from.  If
3289ef5ccd6cSJohn Marino      not, ask the remote which is the current thread, with qC.  The
3290ef5ccd6cSJohn Marino      former method avoids a roundtrip.  Note we don't use
3291ef5ccd6cSJohn Marino      remote_parse_stop_reply as that makes use of the target
3292ef5ccd6cSJohn Marino      architecture, which we haven't yet fully determined at this
3293ef5ccd6cSJohn Marino      point.  */
3294ef5ccd6cSJohn Marino   if (wait_status != NULL)
3295ef5ccd6cSJohn Marino     ptid = stop_reply_extract_thread (wait_status);
3296ef5ccd6cSJohn Marino   if (ptid_equal (ptid, null_ptid))
3297ef5ccd6cSJohn Marino     ptid = remote_current_thread (inferior_ptid);
3298ef5ccd6cSJohn Marino 
3299ef5ccd6cSJohn Marino   if (!ptid_equal (ptid, null_ptid))
3300ef5ccd6cSJohn Marino     {
3301ef5ccd6cSJohn Marino       if (!remote_multi_process_p (rs))
3302ef5ccd6cSJohn Marino 	fake_pid_p = 1;
3303ef5ccd6cSJohn Marino 
3304ef5ccd6cSJohn Marino       inferior_ptid = ptid;
3305ef5ccd6cSJohn Marino     }
3306ef5ccd6cSJohn Marino   else
3307ef5ccd6cSJohn Marino     {
3308ef5ccd6cSJohn Marino       /* Without this, some commands which require an active target
3309ef5ccd6cSJohn Marino 	 (such as kill) won't work.  This variable serves (at least)
3310ef5ccd6cSJohn Marino 	 double duty as both the pid of the target process (if it has
3311ef5ccd6cSJohn Marino 	 such), and as a flag indicating that a target is active.  */
3312ef5ccd6cSJohn Marino       inferior_ptid = magic_null_ptid;
3313ef5ccd6cSJohn Marino       fake_pid_p = 1;
3314ef5ccd6cSJohn Marino     }
3315ef5ccd6cSJohn Marino 
3316ef5ccd6cSJohn Marino   remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1);
3317ef5ccd6cSJohn Marino 
3318ef5ccd6cSJohn Marino   /* Add the main thread.  */
3319ef5ccd6cSJohn Marino   add_thread_silent (inferior_ptid);
3320ef5ccd6cSJohn Marino }
3321ef5ccd6cSJohn Marino 
33225796c8dcSSimon Schubert static void
remote_start_remote(int from_tty,struct target_ops * target,int extended_p)3323a45ae5f8SJohn Marino remote_start_remote (int from_tty, struct target_ops *target, int extended_p)
33245796c8dcSSimon Schubert {
33255796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
33265796c8dcSSimon Schubert   struct packet_config *noack_config;
33275796c8dcSSimon Schubert   char *wait_status = NULL;
33285796c8dcSSimon Schubert 
33295796c8dcSSimon Schubert   immediate_quit++;		/* Allow user to interrupt it.  */
3330ef5ccd6cSJohn Marino   QUIT;
33315796c8dcSSimon Schubert 
3332cf7f2e2dSJohn Marino   if (interrupt_on_connect)
3333cf7f2e2dSJohn Marino     send_interrupt_sequence ();
3334cf7f2e2dSJohn Marino 
3335c50c785cSJohn Marino   /* Ack any packet which the remote side has already sent.  */
3336c50c785cSJohn Marino   serial_write (remote_desc, "+", 1);
3337c50c785cSJohn Marino 
3338a45ae5f8SJohn Marino   /* Signal other parts that we're going through the initial setup,
3339a45ae5f8SJohn Marino      and so things may not be stable yet.  */
3340a45ae5f8SJohn Marino   rs->starting_up = 1;
3341a45ae5f8SJohn Marino 
33425796c8dcSSimon Schubert   /* The first packet we send to the target is the optional "supported
33435796c8dcSSimon Schubert      packets" request.  If the target can answer this, it will tell us
33445796c8dcSSimon Schubert      which later probes to skip.  */
33455796c8dcSSimon Schubert   remote_query_supported ();
33465796c8dcSSimon Schubert 
3347cf7f2e2dSJohn Marino   /* If the stub wants to get a QAllow, compose one and send it.  */
3348cf7f2e2dSJohn Marino   if (remote_protocol_packets[PACKET_QAllow].support != PACKET_DISABLE)
3349cf7f2e2dSJohn Marino     remote_set_permissions ();
3350cf7f2e2dSJohn Marino 
33515796c8dcSSimon Schubert   /* Next, we possibly activate noack mode.
33525796c8dcSSimon Schubert 
33535796c8dcSSimon Schubert      If the QStartNoAckMode packet configuration is set to AUTO,
33545796c8dcSSimon Schubert      enable noack mode if the stub reported a wish for it with
33555796c8dcSSimon Schubert      qSupported.
33565796c8dcSSimon Schubert 
33575796c8dcSSimon Schubert      If set to TRUE, then enable noack mode even if the stub didn't
33585796c8dcSSimon Schubert      report it in qSupported.  If the stub doesn't reply OK, the
33595796c8dcSSimon Schubert      session ends with an error.
33605796c8dcSSimon Schubert 
33615796c8dcSSimon Schubert      If FALSE, then don't activate noack mode, regardless of what the
33625796c8dcSSimon Schubert      stub claimed should be the default with qSupported.  */
33635796c8dcSSimon Schubert 
33645796c8dcSSimon Schubert   noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
33655796c8dcSSimon Schubert 
33665796c8dcSSimon Schubert   if (noack_config->detect == AUTO_BOOLEAN_TRUE
33675796c8dcSSimon Schubert       || (noack_config->detect == AUTO_BOOLEAN_AUTO
33685796c8dcSSimon Schubert 	  && noack_config->support == PACKET_ENABLE))
33695796c8dcSSimon Schubert     {
33705796c8dcSSimon Schubert       putpkt ("QStartNoAckMode");
33715796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
33725796c8dcSSimon Schubert       if (packet_ok (rs->buf, noack_config) == PACKET_OK)
33735796c8dcSSimon Schubert 	rs->noack_mode = 1;
33745796c8dcSSimon Schubert     }
33755796c8dcSSimon Schubert 
3376a45ae5f8SJohn Marino   if (extended_p)
33775796c8dcSSimon Schubert     {
33785796c8dcSSimon Schubert       /* Tell the remote that we are using the extended protocol.  */
33795796c8dcSSimon Schubert       putpkt ("!");
33805796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
33815796c8dcSSimon Schubert     }
33825796c8dcSSimon Schubert 
3383ef5ccd6cSJohn Marino   /* Let the target know which signals it is allowed to pass down to
3384ef5ccd6cSJohn Marino      the program.  */
3385ef5ccd6cSJohn Marino   update_signals_program_target ();
3386ef5ccd6cSJohn Marino 
33875796c8dcSSimon Schubert   /* Next, if the target can specify a description, read it.  We do
33885796c8dcSSimon Schubert      this before anything involving memory or registers.  */
33895796c8dcSSimon Schubert   target_find_description ();
33905796c8dcSSimon Schubert 
3391cf7f2e2dSJohn Marino   /* Next, now that we know something about the target, update the
3392cf7f2e2dSJohn Marino      address spaces in the program spaces.  */
3393cf7f2e2dSJohn Marino   update_address_spaces ();
3394cf7f2e2dSJohn Marino 
33955796c8dcSSimon Schubert   /* On OSs where the list of libraries is global to all
33965796c8dcSSimon Schubert      processes, we fetch them early.  */
3397ef5ccd6cSJohn Marino   if (gdbarch_has_global_solist (target_gdbarch ()))
3398a45ae5f8SJohn Marino     solib_add (NULL, from_tty, target, auto_solib_add);
33995796c8dcSSimon Schubert 
34005796c8dcSSimon Schubert   if (non_stop)
34015796c8dcSSimon Schubert     {
34025796c8dcSSimon Schubert       if (!rs->non_stop_aware)
3403c50c785cSJohn Marino 	error (_("Non-stop mode requested, but remote "
3404c50c785cSJohn Marino 		 "does not support non-stop"));
34055796c8dcSSimon Schubert 
34065796c8dcSSimon Schubert       putpkt ("QNonStop:1");
34075796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
34085796c8dcSSimon Schubert 
34095796c8dcSSimon Schubert       if (strcmp (rs->buf, "OK") != 0)
3410c50c785cSJohn Marino 	error (_("Remote refused setting non-stop mode with: %s"), rs->buf);
34115796c8dcSSimon Schubert 
34125796c8dcSSimon Schubert       /* Find about threads and processes the stub is already
34135796c8dcSSimon Schubert 	 controlling.  We default to adding them in the running state.
34145796c8dcSSimon Schubert 	 The '?' query below will then tell us about which threads are
34155796c8dcSSimon Schubert 	 stopped.  */
3416a45ae5f8SJohn Marino       remote_threads_info (target);
34175796c8dcSSimon Schubert     }
34185796c8dcSSimon Schubert   else if (rs->non_stop_aware)
34195796c8dcSSimon Schubert     {
34205796c8dcSSimon Schubert       /* Don't assume that the stub can operate in all-stop mode.
3421ef5ccd6cSJohn Marino 	 Request it explicitly.  */
34225796c8dcSSimon Schubert       putpkt ("QNonStop:0");
34235796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
34245796c8dcSSimon Schubert 
34255796c8dcSSimon Schubert       if (strcmp (rs->buf, "OK") != 0)
3426c50c785cSJohn Marino 	error (_("Remote refused setting all-stop mode with: %s"), rs->buf);
34275796c8dcSSimon Schubert     }
34285796c8dcSSimon Schubert 
34295796c8dcSSimon Schubert   /* Check whether the target is running now.  */
34305796c8dcSSimon Schubert   putpkt ("?");
34315796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
34325796c8dcSSimon Schubert 
34335796c8dcSSimon Schubert   if (!non_stop)
34345796c8dcSSimon Schubert     {
3435ef5ccd6cSJohn Marino       ptid_t ptid;
3436ef5ccd6cSJohn Marino       int fake_pid_p = 0;
3437ef5ccd6cSJohn Marino       struct inferior *inf;
3438ef5ccd6cSJohn Marino 
34395796c8dcSSimon Schubert       if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
34405796c8dcSSimon Schubert 	{
3441a45ae5f8SJohn Marino 	  if (!extended_p)
34425796c8dcSSimon Schubert 	    error (_("The target is not running (try extended-remote?)"));
34435796c8dcSSimon Schubert 
34445796c8dcSSimon Schubert 	  /* We're connected, but not running.  Drop out before we
34455796c8dcSSimon Schubert 	     call start_remote.  */
3446a45ae5f8SJohn Marino 	  rs->starting_up = 0;
34475796c8dcSSimon Schubert 	  return;
34485796c8dcSSimon Schubert 	}
34495796c8dcSSimon Schubert       else
34505796c8dcSSimon Schubert 	{
34515796c8dcSSimon Schubert 	  /* Save the reply for later.  */
34525796c8dcSSimon Schubert 	  wait_status = alloca (strlen (rs->buf) + 1);
34535796c8dcSSimon Schubert 	  strcpy (wait_status, rs->buf);
34545796c8dcSSimon Schubert 	}
34555796c8dcSSimon Schubert 
34565796c8dcSSimon Schubert       /* Let the stub know that we want it to return the thread.  */
34575796c8dcSSimon Schubert       set_continue_thread (minus_one_ptid);
34585796c8dcSSimon Schubert 
3459ef5ccd6cSJohn Marino       add_current_inferior_and_thread (wait_status);
34605796c8dcSSimon Schubert 
3461a45ae5f8SJohn Marino       /* init_wait_for_inferior should be called before get_offsets in order
3462a45ae5f8SJohn Marino 	 to manage `inserted' flag in bp loc in a correct state.
3463a45ae5f8SJohn Marino 	 breakpoint_init_inferior, called from init_wait_for_inferior, set
3464a45ae5f8SJohn Marino 	 `inserted' flag to 0, while before breakpoint_re_set, called from
3465a45ae5f8SJohn Marino 	 start_remote, set `inserted' flag to 1.  In the initialization of
3466a45ae5f8SJohn Marino 	 inferior, breakpoint_init_inferior should be called first, and then
3467a45ae5f8SJohn Marino 	 breakpoint_re_set can be called.  If this order is broken, state of
3468a45ae5f8SJohn Marino 	 `inserted' flag is wrong, and cause some problems on breakpoint
3469a45ae5f8SJohn Marino 	 manipulation.  */
3470a45ae5f8SJohn Marino       init_wait_for_inferior ();
3471a45ae5f8SJohn Marino 
34725796c8dcSSimon Schubert       get_offsets ();		/* Get text, data & bss offsets.  */
34735796c8dcSSimon Schubert 
34745796c8dcSSimon Schubert       /* If we could not find a description using qXfer, and we know
34755796c8dcSSimon Schubert 	 how to do it some other way, try again.  This is not
34765796c8dcSSimon Schubert 	 supported for non-stop; it could be, but it is tricky if
34775796c8dcSSimon Schubert 	 there are no stopped threads when we connect.  */
3478a45ae5f8SJohn Marino       if (remote_read_description_p (target)
3479ef5ccd6cSJohn Marino 	  && gdbarch_target_desc (target_gdbarch ()) == NULL)
34805796c8dcSSimon Schubert 	{
34815796c8dcSSimon Schubert 	  target_clear_description ();
34825796c8dcSSimon Schubert 	  target_find_description ();
34835796c8dcSSimon Schubert 	}
34845796c8dcSSimon Schubert 
34855796c8dcSSimon Schubert       /* Use the previously fetched status.  */
34865796c8dcSSimon Schubert       gdb_assert (wait_status != NULL);
34875796c8dcSSimon Schubert       strcpy (rs->buf, wait_status);
34885796c8dcSSimon Schubert       rs->cached_wait_status = 1;
34895796c8dcSSimon Schubert 
34905796c8dcSSimon Schubert       immediate_quit--;
3491a45ae5f8SJohn Marino       start_remote (from_tty); /* Initialize gdb process mechanisms.  */
34925796c8dcSSimon Schubert     }
34935796c8dcSSimon Schubert   else
34945796c8dcSSimon Schubert     {
34955796c8dcSSimon Schubert       /* Clear WFI global state.  Do this before finding about new
34965796c8dcSSimon Schubert 	 threads and inferiors, and setting the current inferior.
34975796c8dcSSimon Schubert 	 Otherwise we would clear the proceed status of the current
34985796c8dcSSimon Schubert 	 inferior when we want its stop_soon state to be preserved
34995796c8dcSSimon Schubert 	 (see notice_new_inferior).  */
35005796c8dcSSimon Schubert       init_wait_for_inferior ();
35015796c8dcSSimon Schubert 
35025796c8dcSSimon Schubert       /* In non-stop, we will either get an "OK", meaning that there
35035796c8dcSSimon Schubert 	 are no stopped threads at this time; or, a regular stop
35045796c8dcSSimon Schubert 	 reply.  In the latter case, there may be more than one thread
35055796c8dcSSimon Schubert 	 stopped --- we pull them all out using the vStopped
35065796c8dcSSimon Schubert 	 mechanism.  */
35075796c8dcSSimon Schubert       if (strcmp (rs->buf, "OK") != 0)
35085796c8dcSSimon Schubert 	{
3509ef5ccd6cSJohn Marino 	  struct notif_client *notif = &notif_client_stop;
35105796c8dcSSimon Schubert 
3511ef5ccd6cSJohn Marino 	  /* remote_notif_get_pending_replies acks this one, and gets
3512ef5ccd6cSJohn Marino 	     the rest out.  */
3513ef5ccd6cSJohn Marino 	  notif_client_stop.pending_event
3514ef5ccd6cSJohn Marino 	    = remote_notif_parse (notif, rs->buf);
3515ef5ccd6cSJohn Marino 	  remote_notif_get_pending_events (notif);
35165796c8dcSSimon Schubert 
35175796c8dcSSimon Schubert 	  /* Make sure that threads that were stopped remain
35185796c8dcSSimon Schubert 	     stopped.  */
35195796c8dcSSimon Schubert 	  iterate_over_threads (set_stop_requested_callback, NULL);
35205796c8dcSSimon Schubert 	}
35215796c8dcSSimon Schubert 
35225796c8dcSSimon Schubert       if (target_can_async_p ())
35235796c8dcSSimon Schubert 	target_async (inferior_event_handler, 0);
35245796c8dcSSimon Schubert 
35255796c8dcSSimon Schubert       if (thread_count () == 0)
35265796c8dcSSimon Schubert 	{
3527a45ae5f8SJohn Marino 	  if (!extended_p)
35285796c8dcSSimon Schubert 	    error (_("The target is not running (try extended-remote?)"));
35295796c8dcSSimon Schubert 
35305796c8dcSSimon Schubert 	  /* We're connected, but not running.  Drop out before we
35315796c8dcSSimon Schubert 	     call start_remote.  */
3532a45ae5f8SJohn Marino 	  rs->starting_up = 0;
35335796c8dcSSimon Schubert 	  return;
35345796c8dcSSimon Schubert 	}
35355796c8dcSSimon Schubert 
35365796c8dcSSimon Schubert       /* Let the stub know that we want it to return the thread.  */
35375796c8dcSSimon Schubert 
35385796c8dcSSimon Schubert       /* Force the stub to choose a thread.  */
35395796c8dcSSimon Schubert       set_general_thread (null_ptid);
35405796c8dcSSimon Schubert 
35415796c8dcSSimon Schubert       /* Query it.  */
35425796c8dcSSimon Schubert       inferior_ptid = remote_current_thread (minus_one_ptid);
35435796c8dcSSimon Schubert       if (ptid_equal (inferior_ptid, minus_one_ptid))
35445796c8dcSSimon Schubert 	error (_("remote didn't report the current thread in non-stop mode"));
35455796c8dcSSimon Schubert 
35465796c8dcSSimon Schubert       get_offsets ();		/* Get text, data & bss offsets.  */
35475796c8dcSSimon Schubert 
35485796c8dcSSimon Schubert       /* In non-stop mode, any cached wait status will be stored in
35495796c8dcSSimon Schubert 	 the stop reply queue.  */
35505796c8dcSSimon Schubert       gdb_assert (wait_status == NULL);
3551cf7f2e2dSJohn Marino 
3552a45ae5f8SJohn Marino       /* Report all signals during attach/startup.  */
3553a45ae5f8SJohn Marino       remote_pass_signals (0, NULL);
35545796c8dcSSimon Schubert     }
35555796c8dcSSimon Schubert 
35565796c8dcSSimon Schubert   /* If we connected to a live target, do some additional setup.  */
35575796c8dcSSimon Schubert   if (target_has_execution)
35585796c8dcSSimon Schubert     {
35595796c8dcSSimon Schubert       if (exec_bfd) 	/* No use without an exec file.  */
35605796c8dcSSimon Schubert 	remote_check_symbols (symfile_objfile);
35615796c8dcSSimon Schubert     }
35625796c8dcSSimon Schubert 
3563cf7f2e2dSJohn Marino   /* Possibly the target has been engaged in a trace run started
3564cf7f2e2dSJohn Marino      previously; find out where things are at.  */
3565cf7f2e2dSJohn Marino   if (remote_get_trace_status (current_trace_status ()) != -1)
3566cf7f2e2dSJohn Marino     {
3567cf7f2e2dSJohn Marino       struct uploaded_tp *uploaded_tps = NULL;
3568cf7f2e2dSJohn Marino       struct uploaded_tsv *uploaded_tsvs = NULL;
3569cf7f2e2dSJohn Marino 
3570cf7f2e2dSJohn Marino       if (current_trace_status ()->running)
3571cf7f2e2dSJohn Marino 	printf_filtered (_("Trace is already running on the target.\n"));
3572cf7f2e2dSJohn Marino 
3573cf7f2e2dSJohn Marino       /* Get trace state variables first, they may be checked when
3574cf7f2e2dSJohn Marino 	 parsing uploaded commands.  */
3575cf7f2e2dSJohn Marino 
3576cf7f2e2dSJohn Marino       remote_upload_trace_state_variables (&uploaded_tsvs);
3577cf7f2e2dSJohn Marino 
3578cf7f2e2dSJohn Marino       merge_uploaded_trace_state_variables (&uploaded_tsvs);
3579cf7f2e2dSJohn Marino 
3580cf7f2e2dSJohn Marino       remote_upload_tracepoints (&uploaded_tps);
3581cf7f2e2dSJohn Marino 
3582cf7f2e2dSJohn Marino       merge_uploaded_tracepoints (&uploaded_tps);
3583cf7f2e2dSJohn Marino     }
3584cf7f2e2dSJohn Marino 
3585a45ae5f8SJohn Marino   /* The thread and inferior lists are now synchronized with the
3586a45ae5f8SJohn Marino      target, our symbols have been relocated, and we're merged the
3587a45ae5f8SJohn Marino      target's tracepoints with ours.  We're done with basic start
3588a45ae5f8SJohn Marino      up.  */
3589a45ae5f8SJohn Marino   rs->starting_up = 0;
3590a45ae5f8SJohn Marino 
35915796c8dcSSimon Schubert   /* If breakpoints are global, insert them now.  */
3592ef5ccd6cSJohn Marino   if (gdbarch_has_global_breakpoints (target_gdbarch ())
35935796c8dcSSimon Schubert       && breakpoints_always_inserted_mode ())
35945796c8dcSSimon Schubert     insert_breakpoints ();
35955796c8dcSSimon Schubert }
35965796c8dcSSimon Schubert 
35975796c8dcSSimon Schubert /* Open a connection to a remote debugger.
35985796c8dcSSimon Schubert    NAME is the filename used for communication.  */
35995796c8dcSSimon Schubert 
36005796c8dcSSimon Schubert static void
remote_open(char * name,int from_tty)36015796c8dcSSimon Schubert remote_open (char *name, int from_tty)
36025796c8dcSSimon Schubert {
36035796c8dcSSimon Schubert   remote_open_1 (name, from_tty, &remote_ops, 0);
36045796c8dcSSimon Schubert }
36055796c8dcSSimon Schubert 
36065796c8dcSSimon Schubert /* Open a connection to a remote debugger using the extended
36075796c8dcSSimon Schubert    remote gdb protocol.  NAME is the filename used for communication.  */
36085796c8dcSSimon Schubert 
36095796c8dcSSimon Schubert static void
extended_remote_open(char * name,int from_tty)36105796c8dcSSimon Schubert extended_remote_open (char *name, int from_tty)
36115796c8dcSSimon Schubert {
36125796c8dcSSimon Schubert   remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
36135796c8dcSSimon Schubert }
36145796c8dcSSimon Schubert 
36155796c8dcSSimon Schubert /* Generic code for opening a connection to a remote target.  */
36165796c8dcSSimon Schubert 
36175796c8dcSSimon Schubert static void
init_all_packet_configs(void)36185796c8dcSSimon Schubert init_all_packet_configs (void)
36195796c8dcSSimon Schubert {
36205796c8dcSSimon Schubert   int i;
3621cf7f2e2dSJohn Marino 
36225796c8dcSSimon Schubert   for (i = 0; i < PACKET_MAX; i++)
36235796c8dcSSimon Schubert     update_packet_config (&remote_protocol_packets[i]);
36245796c8dcSSimon Schubert }
36255796c8dcSSimon Schubert 
36265796c8dcSSimon Schubert /* Symbol look-up.  */
36275796c8dcSSimon Schubert 
36285796c8dcSSimon Schubert static void
remote_check_symbols(struct objfile * objfile)36295796c8dcSSimon Schubert remote_check_symbols (struct objfile *objfile)
36305796c8dcSSimon Schubert {
36315796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
36325796c8dcSSimon Schubert   char *msg, *reply, *tmp;
36335796c8dcSSimon Schubert   struct minimal_symbol *sym;
36345796c8dcSSimon Schubert   int end;
36355796c8dcSSimon Schubert 
3636c50c785cSJohn Marino   /* The remote side has no concept of inferiors that aren't running
3637c50c785cSJohn Marino      yet, it only knows about running processes.  If we're connected
3638c50c785cSJohn Marino      but our current inferior is not running, we should not invite the
3639c50c785cSJohn Marino      remote target to request symbol lookups related to its
3640c50c785cSJohn Marino      (unrelated) current process.  */
3641c50c785cSJohn Marino   if (!target_has_execution)
3642c50c785cSJohn Marino     return;
3643c50c785cSJohn Marino 
36445796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE)
36455796c8dcSSimon Schubert     return;
36465796c8dcSSimon Schubert 
3647c50c785cSJohn Marino   /* Make sure the remote is pointing at the right process.  Note
3648c50c785cSJohn Marino      there's no way to select "no process".  */
36495796c8dcSSimon Schubert   set_general_process ();
36505796c8dcSSimon Schubert 
36515796c8dcSSimon Schubert   /* Allocate a message buffer.  We can't reuse the input buffer in RS,
36525796c8dcSSimon Schubert      because we need both at the same time.  */
36535796c8dcSSimon Schubert   msg = alloca (get_remote_packet_size ());
36545796c8dcSSimon Schubert 
36555796c8dcSSimon Schubert   /* Invite target to request symbol lookups.  */
36565796c8dcSSimon Schubert 
36575796c8dcSSimon Schubert   putpkt ("qSymbol::");
36585796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
36595796c8dcSSimon Schubert   packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
36605796c8dcSSimon Schubert   reply = rs->buf;
36615796c8dcSSimon Schubert 
36625796c8dcSSimon Schubert   while (strncmp (reply, "qSymbol:", 8) == 0)
36635796c8dcSSimon Schubert     {
36645796c8dcSSimon Schubert       tmp = &reply[8];
36655796c8dcSSimon Schubert       end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
36665796c8dcSSimon Schubert       msg[end] = '\0';
36675796c8dcSSimon Schubert       sym = lookup_minimal_symbol (msg, NULL, NULL);
36685796c8dcSSimon Schubert       if (sym == NULL)
36695796c8dcSSimon Schubert 	xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
36705796c8dcSSimon Schubert       else
36715796c8dcSSimon Schubert 	{
3672ef5ccd6cSJohn Marino 	  int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
36735796c8dcSSimon Schubert 	  CORE_ADDR sym_addr = SYMBOL_VALUE_ADDRESS (sym);
36745796c8dcSSimon Schubert 
36755796c8dcSSimon Schubert 	  /* If this is a function address, return the start of code
36765796c8dcSSimon Schubert 	     instead of any data function descriptor.  */
3677ef5ccd6cSJohn Marino 	  sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
36785796c8dcSSimon Schubert 							 sym_addr,
36795796c8dcSSimon Schubert 							 &current_target);
36805796c8dcSSimon Schubert 
36815796c8dcSSimon Schubert 	  xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
36825796c8dcSSimon Schubert 		     phex_nz (sym_addr, addr_size), &reply[8]);
36835796c8dcSSimon Schubert 	}
36845796c8dcSSimon Schubert 
36855796c8dcSSimon Schubert       putpkt (msg);
36865796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
36875796c8dcSSimon Schubert       reply = rs->buf;
36885796c8dcSSimon Schubert     }
36895796c8dcSSimon Schubert }
36905796c8dcSSimon Schubert 
36915796c8dcSSimon Schubert static struct serial *
remote_serial_open(char * name)36925796c8dcSSimon Schubert remote_serial_open (char *name)
36935796c8dcSSimon Schubert {
36945796c8dcSSimon Schubert   static int udp_warning = 0;
36955796c8dcSSimon Schubert 
36965796c8dcSSimon Schubert   /* FIXME: Parsing NAME here is a hack.  But we want to warn here instead
36975796c8dcSSimon Schubert      of in ser-tcp.c, because it is the remote protocol assuming that the
36985796c8dcSSimon Schubert      serial connection is reliable and not the serial connection promising
36995796c8dcSSimon Schubert      to be.  */
37005796c8dcSSimon Schubert   if (!udp_warning && strncmp (name, "udp:", 4) == 0)
37015796c8dcSSimon Schubert     {
3702c50c785cSJohn Marino       warning (_("The remote protocol may be unreliable over UDP.\n"
3703c50c785cSJohn Marino 		 "Some events may be lost, rendering further debugging "
3704c50c785cSJohn Marino 		 "impossible."));
37055796c8dcSSimon Schubert       udp_warning = 1;
37065796c8dcSSimon Schubert     }
37075796c8dcSSimon Schubert 
37085796c8dcSSimon Schubert   return serial_open (name);
37095796c8dcSSimon Schubert }
37105796c8dcSSimon Schubert 
3711cf7f2e2dSJohn Marino /* Inform the target of our permission settings.  The permission flags
3712cf7f2e2dSJohn Marino    work without this, but if the target knows the settings, it can do
3713cf7f2e2dSJohn Marino    a couple things.  First, it can add its own check, to catch cases
3714cf7f2e2dSJohn Marino    that somehow manage to get by the permissions checks in target
3715cf7f2e2dSJohn Marino    methods.  Second, if the target is wired to disallow particular
3716cf7f2e2dSJohn Marino    settings (for instance, a system in the field that is not set up to
3717cf7f2e2dSJohn Marino    be able to stop at a breakpoint), it can object to any unavailable
3718cf7f2e2dSJohn Marino    permissions.  */
3719cf7f2e2dSJohn Marino 
3720cf7f2e2dSJohn Marino void
remote_set_permissions(void)3721cf7f2e2dSJohn Marino remote_set_permissions (void)
3722cf7f2e2dSJohn Marino {
3723cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
3724cf7f2e2dSJohn Marino 
3725ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3726cf7f2e2dSJohn Marino 	     "WriteReg:%x;WriteMem:%x;"
3727cf7f2e2dSJohn Marino 	     "InsertBreak:%x;InsertTrace:%x;"
3728cf7f2e2dSJohn Marino 	     "InsertFastTrace:%x;Stop:%x",
3729cf7f2e2dSJohn Marino 	     may_write_registers, may_write_memory,
3730cf7f2e2dSJohn Marino 	     may_insert_breakpoints, may_insert_tracepoints,
3731cf7f2e2dSJohn Marino 	     may_insert_fast_tracepoints, may_stop);
3732cf7f2e2dSJohn Marino   putpkt (rs->buf);
3733cf7f2e2dSJohn Marino   getpkt (&rs->buf, &rs->buf_size, 0);
3734cf7f2e2dSJohn Marino 
3735cf7f2e2dSJohn Marino   /* If the target didn't like the packet, warn the user.  Do not try
3736cf7f2e2dSJohn Marino      to undo the user's settings, that would just be maddening.  */
3737cf7f2e2dSJohn Marino   if (strcmp (rs->buf, "OK") != 0)
3738c50c785cSJohn Marino     warning (_("Remote refused setting permissions with: %s"), rs->buf);
3739cf7f2e2dSJohn Marino }
3740cf7f2e2dSJohn Marino 
37415796c8dcSSimon Schubert /* This type describes each known response to the qSupported
37425796c8dcSSimon Schubert    packet.  */
37435796c8dcSSimon Schubert struct protocol_feature
37445796c8dcSSimon Schubert {
37455796c8dcSSimon Schubert   /* The name of this protocol feature.  */
37465796c8dcSSimon Schubert   const char *name;
37475796c8dcSSimon Schubert 
37485796c8dcSSimon Schubert   /* The default for this protocol feature.  */
37495796c8dcSSimon Schubert   enum packet_support default_support;
37505796c8dcSSimon Schubert 
37515796c8dcSSimon Schubert   /* The function to call when this feature is reported, or after
37525796c8dcSSimon Schubert      qSupported processing if the feature is not supported.
37535796c8dcSSimon Schubert      The first argument points to this structure.  The second
37545796c8dcSSimon Schubert      argument indicates whether the packet requested support be
37555796c8dcSSimon Schubert      enabled, disabled, or probed (or the default, if this function
37565796c8dcSSimon Schubert      is being called at the end of processing and this feature was
37575796c8dcSSimon Schubert      not reported).  The third argument may be NULL; if not NULL, it
37585796c8dcSSimon Schubert      is a NUL-terminated string taken from the packet following
37595796c8dcSSimon Schubert      this feature's name and an equals sign.  */
37605796c8dcSSimon Schubert   void (*func) (const struct protocol_feature *, enum packet_support,
37615796c8dcSSimon Schubert 		const char *);
37625796c8dcSSimon Schubert 
37635796c8dcSSimon Schubert   /* The corresponding packet for this feature.  Only used if
37645796c8dcSSimon Schubert      FUNC is remote_supported_packet.  */
37655796c8dcSSimon Schubert   int packet;
37665796c8dcSSimon Schubert };
37675796c8dcSSimon Schubert 
37685796c8dcSSimon Schubert static void
remote_supported_packet(const struct protocol_feature * feature,enum packet_support support,const char * argument)37695796c8dcSSimon Schubert remote_supported_packet (const struct protocol_feature *feature,
37705796c8dcSSimon Schubert 			 enum packet_support support,
37715796c8dcSSimon Schubert 			 const char *argument)
37725796c8dcSSimon Schubert {
37735796c8dcSSimon Schubert   if (argument)
37745796c8dcSSimon Schubert     {
37755796c8dcSSimon Schubert       warning (_("Remote qSupported response supplied an unexpected value for"
37765796c8dcSSimon Schubert 		 " \"%s\"."), feature->name);
37775796c8dcSSimon Schubert       return;
37785796c8dcSSimon Schubert     }
37795796c8dcSSimon Schubert 
37805796c8dcSSimon Schubert   if (remote_protocol_packets[feature->packet].support
37815796c8dcSSimon Schubert       == PACKET_SUPPORT_UNKNOWN)
37825796c8dcSSimon Schubert     remote_protocol_packets[feature->packet].support = support;
37835796c8dcSSimon Schubert }
37845796c8dcSSimon Schubert 
37855796c8dcSSimon Schubert static void
remote_packet_size(const struct protocol_feature * feature,enum packet_support support,const char * value)37865796c8dcSSimon Schubert remote_packet_size (const struct protocol_feature *feature,
37875796c8dcSSimon Schubert 		    enum packet_support support, const char *value)
37885796c8dcSSimon Schubert {
37895796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
37905796c8dcSSimon Schubert 
37915796c8dcSSimon Schubert   int packet_size;
37925796c8dcSSimon Schubert   char *value_end;
37935796c8dcSSimon Schubert 
37945796c8dcSSimon Schubert   if (support != PACKET_ENABLE)
37955796c8dcSSimon Schubert     return;
37965796c8dcSSimon Schubert 
37975796c8dcSSimon Schubert   if (value == NULL || *value == '\0')
37985796c8dcSSimon Schubert     {
37995796c8dcSSimon Schubert       warning (_("Remote target reported \"%s\" without a size."),
38005796c8dcSSimon Schubert 	       feature->name);
38015796c8dcSSimon Schubert       return;
38025796c8dcSSimon Schubert     }
38035796c8dcSSimon Schubert 
38045796c8dcSSimon Schubert   errno = 0;
38055796c8dcSSimon Schubert   packet_size = strtol (value, &value_end, 16);
38065796c8dcSSimon Schubert   if (errno != 0 || *value_end != '\0' || packet_size < 0)
38075796c8dcSSimon Schubert     {
38085796c8dcSSimon Schubert       warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
38095796c8dcSSimon Schubert 	       feature->name, value);
38105796c8dcSSimon Schubert       return;
38115796c8dcSSimon Schubert     }
38125796c8dcSSimon Schubert 
38135796c8dcSSimon Schubert   if (packet_size > MAX_REMOTE_PACKET_SIZE)
38145796c8dcSSimon Schubert     {
38155796c8dcSSimon Schubert       warning (_("limiting remote suggested packet size (%d bytes) to %d"),
38165796c8dcSSimon Schubert 	       packet_size, MAX_REMOTE_PACKET_SIZE);
38175796c8dcSSimon Schubert       packet_size = MAX_REMOTE_PACKET_SIZE;
38185796c8dcSSimon Schubert     }
38195796c8dcSSimon Schubert 
38205796c8dcSSimon Schubert   /* Record the new maximum packet size.  */
38215796c8dcSSimon Schubert   rs->explicit_packet_size = packet_size;
38225796c8dcSSimon Schubert }
38235796c8dcSSimon Schubert 
38245796c8dcSSimon Schubert static void
remote_multi_process_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)38255796c8dcSSimon Schubert remote_multi_process_feature (const struct protocol_feature *feature,
38265796c8dcSSimon Schubert 			      enum packet_support support, const char *value)
38275796c8dcSSimon Schubert {
38285796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
3829cf7f2e2dSJohn Marino 
38305796c8dcSSimon Schubert   rs->multi_process_aware = (support == PACKET_ENABLE);
38315796c8dcSSimon Schubert }
38325796c8dcSSimon Schubert 
38335796c8dcSSimon Schubert static void
remote_non_stop_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)38345796c8dcSSimon Schubert remote_non_stop_feature (const struct protocol_feature *feature,
38355796c8dcSSimon Schubert 			      enum packet_support support, const char *value)
38365796c8dcSSimon Schubert {
38375796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
3838cf7f2e2dSJohn Marino 
38395796c8dcSSimon Schubert   rs->non_stop_aware = (support == PACKET_ENABLE);
38405796c8dcSSimon Schubert }
38415796c8dcSSimon Schubert 
38425796c8dcSSimon Schubert static void
remote_cond_tracepoint_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)38435796c8dcSSimon Schubert remote_cond_tracepoint_feature (const struct protocol_feature *feature,
38445796c8dcSSimon Schubert 				       enum packet_support support,
38455796c8dcSSimon Schubert 				       const char *value)
38465796c8dcSSimon Schubert {
38475796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
3848cf7f2e2dSJohn Marino 
38495796c8dcSSimon Schubert   rs->cond_tracepoints = (support == PACKET_ENABLE);
38505796c8dcSSimon Schubert }
38515796c8dcSSimon Schubert 
3852cf7f2e2dSJohn Marino static void
remote_cond_breakpoint_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)3853ef5ccd6cSJohn Marino remote_cond_breakpoint_feature (const struct protocol_feature *feature,
3854ef5ccd6cSJohn Marino 				enum packet_support support,
3855ef5ccd6cSJohn Marino 				const char *value)
3856ef5ccd6cSJohn Marino {
3857ef5ccd6cSJohn Marino   struct remote_state *rs = get_remote_state ();
3858ef5ccd6cSJohn Marino 
3859ef5ccd6cSJohn Marino   rs->cond_breakpoints = (support == PACKET_ENABLE);
3860ef5ccd6cSJohn Marino }
3861ef5ccd6cSJohn Marino 
3862ef5ccd6cSJohn Marino static void
remote_breakpoint_commands_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)3863ef5ccd6cSJohn Marino remote_breakpoint_commands_feature (const struct protocol_feature *feature,
3864ef5ccd6cSJohn Marino 				    enum packet_support support,
3865ef5ccd6cSJohn Marino 				    const char *value)
3866ef5ccd6cSJohn Marino {
3867ef5ccd6cSJohn Marino   struct remote_state *rs = get_remote_state ();
3868ef5ccd6cSJohn Marino 
3869ef5ccd6cSJohn Marino   rs->breakpoint_commands = (support == PACKET_ENABLE);
3870ef5ccd6cSJohn Marino }
3871ef5ccd6cSJohn Marino 
3872ef5ccd6cSJohn Marino static void
remote_fast_tracepoint_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)3873cf7f2e2dSJohn Marino remote_fast_tracepoint_feature (const struct protocol_feature *feature,
3874cf7f2e2dSJohn Marino 				enum packet_support support,
3875cf7f2e2dSJohn Marino 				const char *value)
3876cf7f2e2dSJohn Marino {
3877cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
3878cf7f2e2dSJohn Marino 
3879cf7f2e2dSJohn Marino   rs->fast_tracepoints = (support == PACKET_ENABLE);
3880cf7f2e2dSJohn Marino }
3881cf7f2e2dSJohn Marino 
3882cf7f2e2dSJohn Marino static void
remote_static_tracepoint_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)3883cf7f2e2dSJohn Marino remote_static_tracepoint_feature (const struct protocol_feature *feature,
3884cf7f2e2dSJohn Marino 				  enum packet_support support,
3885cf7f2e2dSJohn Marino 				  const char *value)
3886cf7f2e2dSJohn Marino {
3887cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
3888cf7f2e2dSJohn Marino 
3889cf7f2e2dSJohn Marino   rs->static_tracepoints = (support == PACKET_ENABLE);
3890cf7f2e2dSJohn Marino }
3891cf7f2e2dSJohn Marino 
3892cf7f2e2dSJohn Marino static void
remote_install_in_trace_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)3893a45ae5f8SJohn Marino remote_install_in_trace_feature (const struct protocol_feature *feature,
3894a45ae5f8SJohn Marino 				 enum packet_support support,
3895a45ae5f8SJohn Marino 				 const char *value)
3896a45ae5f8SJohn Marino {
3897a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
3898a45ae5f8SJohn Marino 
3899a45ae5f8SJohn Marino   rs->install_in_trace = (support == PACKET_ENABLE);
3900a45ae5f8SJohn Marino }
3901a45ae5f8SJohn Marino 
3902a45ae5f8SJohn Marino static void
remote_disconnected_tracing_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)3903cf7f2e2dSJohn Marino remote_disconnected_tracing_feature (const struct protocol_feature *feature,
3904cf7f2e2dSJohn Marino 				     enum packet_support support,
3905cf7f2e2dSJohn Marino 				     const char *value)
3906cf7f2e2dSJohn Marino {
3907cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
3908cf7f2e2dSJohn Marino 
3909cf7f2e2dSJohn Marino   rs->disconnected_tracing = (support == PACKET_ENABLE);
3910cf7f2e2dSJohn Marino }
3911cf7f2e2dSJohn Marino 
3912a45ae5f8SJohn Marino static void
remote_enable_disable_tracepoint_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)3913a45ae5f8SJohn Marino remote_enable_disable_tracepoint_feature (const struct protocol_feature *feature,
3914a45ae5f8SJohn Marino 					  enum packet_support support,
3915a45ae5f8SJohn Marino 					  const char *value)
3916a45ae5f8SJohn Marino {
3917a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
3918a45ae5f8SJohn Marino 
3919a45ae5f8SJohn Marino   rs->enable_disable_tracepoints = (support == PACKET_ENABLE);
3920a45ae5f8SJohn Marino }
3921a45ae5f8SJohn Marino 
3922a45ae5f8SJohn Marino static void
remote_string_tracing_feature(const struct protocol_feature * feature,enum packet_support support,const char * value)3923a45ae5f8SJohn Marino remote_string_tracing_feature (const struct protocol_feature *feature,
3924a45ae5f8SJohn Marino 			       enum packet_support support,
3925a45ae5f8SJohn Marino 			       const char *value)
3926a45ae5f8SJohn Marino {
3927a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
3928a45ae5f8SJohn Marino 
3929a45ae5f8SJohn Marino   rs->string_tracing = (support == PACKET_ENABLE);
3930a45ae5f8SJohn Marino }
3931a45ae5f8SJohn Marino 
39325796c8dcSSimon Schubert static struct protocol_feature remote_protocol_features[] = {
39335796c8dcSSimon Schubert   { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
39345796c8dcSSimon Schubert   { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
39355796c8dcSSimon Schubert     PACKET_qXfer_auxv },
39365796c8dcSSimon Schubert   { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
39375796c8dcSSimon Schubert     PACKET_qXfer_features },
39385796c8dcSSimon Schubert   { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
39395796c8dcSSimon Schubert     PACKET_qXfer_libraries },
3940a45ae5f8SJohn Marino   { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
3941a45ae5f8SJohn Marino     PACKET_qXfer_libraries_svr4 },
39425796c8dcSSimon Schubert   { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
39435796c8dcSSimon Schubert     PACKET_qXfer_memory_map },
39445796c8dcSSimon Schubert   { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
39455796c8dcSSimon Schubert     PACKET_qXfer_spu_read },
39465796c8dcSSimon Schubert   { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
39475796c8dcSSimon Schubert     PACKET_qXfer_spu_write },
39485796c8dcSSimon Schubert   { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
39495796c8dcSSimon Schubert     PACKET_qXfer_osdata },
3950cf7f2e2dSJohn Marino   { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
3951cf7f2e2dSJohn Marino     PACKET_qXfer_threads },
3952c50c785cSJohn Marino   { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
3953c50c785cSJohn Marino     PACKET_qXfer_traceframe_info },
39545796c8dcSSimon Schubert   { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
39555796c8dcSSimon Schubert     PACKET_QPassSignals },
3956ef5ccd6cSJohn Marino   { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
3957ef5ccd6cSJohn Marino     PACKET_QProgramSignals },
39585796c8dcSSimon Schubert   { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
39595796c8dcSSimon Schubert     PACKET_QStartNoAckMode },
39605796c8dcSSimon Schubert   { "multiprocess", PACKET_DISABLE, remote_multi_process_feature, -1 },
39615796c8dcSSimon Schubert   { "QNonStop", PACKET_DISABLE, remote_non_stop_feature, -1 },
39625796c8dcSSimon Schubert   { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
39635796c8dcSSimon Schubert     PACKET_qXfer_siginfo_read },
39645796c8dcSSimon Schubert   { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
39655796c8dcSSimon Schubert     PACKET_qXfer_siginfo_write },
39665796c8dcSSimon Schubert   { "ConditionalTracepoints", PACKET_DISABLE, remote_cond_tracepoint_feature,
39675796c8dcSSimon Schubert     PACKET_ConditionalTracepoints },
3968ef5ccd6cSJohn Marino   { "ConditionalBreakpoints", PACKET_DISABLE, remote_cond_breakpoint_feature,
3969ef5ccd6cSJohn Marino     PACKET_ConditionalBreakpoints },
3970ef5ccd6cSJohn Marino   { "BreakpointCommands", PACKET_DISABLE, remote_breakpoint_commands_feature,
3971ef5ccd6cSJohn Marino     PACKET_BreakpointCommands },
3972cf7f2e2dSJohn Marino   { "FastTracepoints", PACKET_DISABLE, remote_fast_tracepoint_feature,
3973cf7f2e2dSJohn Marino     PACKET_FastTracepoints },
3974cf7f2e2dSJohn Marino   { "StaticTracepoints", PACKET_DISABLE, remote_static_tracepoint_feature,
3975cf7f2e2dSJohn Marino     PACKET_StaticTracepoints },
3976a45ae5f8SJohn Marino   {"InstallInTrace", PACKET_DISABLE, remote_install_in_trace_feature,
3977a45ae5f8SJohn Marino    PACKET_InstallInTrace},
3978cf7f2e2dSJohn Marino   { "DisconnectedTracing", PACKET_DISABLE, remote_disconnected_tracing_feature,
3979cf7f2e2dSJohn Marino     -1 },
39805796c8dcSSimon Schubert   { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
39815796c8dcSSimon Schubert     PACKET_bc },
39825796c8dcSSimon Schubert   { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
39835796c8dcSSimon Schubert     PACKET_bs },
3984cf7f2e2dSJohn Marino   { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
3985cf7f2e2dSJohn Marino     PACKET_TracepointSource },
3986cf7f2e2dSJohn Marino   { "QAllow", PACKET_DISABLE, remote_supported_packet,
3987cf7f2e2dSJohn Marino     PACKET_QAllow },
3988a45ae5f8SJohn Marino   { "EnableDisableTracepoints", PACKET_DISABLE,
3989a45ae5f8SJohn Marino     remote_enable_disable_tracepoint_feature, -1 },
3990a45ae5f8SJohn Marino   { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
3991a45ae5f8SJohn Marino     PACKET_qXfer_fdpic },
3992ef5ccd6cSJohn Marino   { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
3993ef5ccd6cSJohn Marino     PACKET_qXfer_uib },
3994a45ae5f8SJohn Marino   { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
3995a45ae5f8SJohn Marino     PACKET_QDisableRandomization },
3996ef5ccd6cSJohn Marino   { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
3997ef5ccd6cSJohn Marino   { "QTBuffer:size", PACKET_DISABLE,
3998ef5ccd6cSJohn Marino     remote_supported_packet, PACKET_QTBuffer_size},
3999a45ae5f8SJohn Marino   { "tracenz", PACKET_DISABLE,
4000a45ae5f8SJohn Marino     remote_string_tracing_feature, -1 },
4001ef5ccd6cSJohn Marino   { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
4002ef5ccd6cSJohn Marino   { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
4003ef5ccd6cSJohn Marino   { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
4004ef5ccd6cSJohn Marino     PACKET_qXfer_btrace }
40055796c8dcSSimon Schubert };
40065796c8dcSSimon Schubert 
4007cf7f2e2dSJohn Marino static char *remote_support_xml;
4008cf7f2e2dSJohn Marino 
4009cf7f2e2dSJohn Marino /* Register string appended to "xmlRegisters=" in qSupported query.  */
4010cf7f2e2dSJohn Marino 
4011cf7f2e2dSJohn Marino void
register_remote_support_xml(const char * xml)4012cf7f2e2dSJohn Marino register_remote_support_xml (const char *xml)
4013cf7f2e2dSJohn Marino {
4014cf7f2e2dSJohn Marino #if defined(HAVE_LIBEXPAT)
4015cf7f2e2dSJohn Marino   if (remote_support_xml == NULL)
4016cf7f2e2dSJohn Marino     remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
4017cf7f2e2dSJohn Marino   else
4018cf7f2e2dSJohn Marino     {
4019cf7f2e2dSJohn Marino       char *copy = xstrdup (remote_support_xml + 13);
4020cf7f2e2dSJohn Marino       char *p = strtok (copy, ",");
4021cf7f2e2dSJohn Marino 
4022cf7f2e2dSJohn Marino       do
4023cf7f2e2dSJohn Marino 	{
4024cf7f2e2dSJohn Marino 	  if (strcmp (p, xml) == 0)
4025cf7f2e2dSJohn Marino 	    {
4026cf7f2e2dSJohn Marino 	      /* already there */
4027cf7f2e2dSJohn Marino 	      xfree (copy);
4028cf7f2e2dSJohn Marino 	      return;
4029cf7f2e2dSJohn Marino 	    }
4030cf7f2e2dSJohn Marino 	}
4031cf7f2e2dSJohn Marino       while ((p = strtok (NULL, ",")) != NULL);
4032cf7f2e2dSJohn Marino       xfree (copy);
4033cf7f2e2dSJohn Marino 
4034cf7f2e2dSJohn Marino       remote_support_xml = reconcat (remote_support_xml,
4035cf7f2e2dSJohn Marino 				     remote_support_xml, ",", xml,
4036cf7f2e2dSJohn Marino 				     (char *) NULL);
4037cf7f2e2dSJohn Marino     }
4038cf7f2e2dSJohn Marino #endif
4039cf7f2e2dSJohn Marino }
4040cf7f2e2dSJohn Marino 
4041cf7f2e2dSJohn Marino static char *
remote_query_supported_append(char * msg,const char * append)4042cf7f2e2dSJohn Marino remote_query_supported_append (char *msg, const char *append)
4043cf7f2e2dSJohn Marino {
4044cf7f2e2dSJohn Marino   if (msg)
4045cf7f2e2dSJohn Marino     return reconcat (msg, msg, ";", append, (char *) NULL);
4046cf7f2e2dSJohn Marino   else
4047cf7f2e2dSJohn Marino     return xstrdup (append);
4048cf7f2e2dSJohn Marino }
4049cf7f2e2dSJohn Marino 
40505796c8dcSSimon Schubert static void
remote_query_supported(void)40515796c8dcSSimon Schubert remote_query_supported (void)
40525796c8dcSSimon Schubert {
40535796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
40545796c8dcSSimon Schubert   char *next;
40555796c8dcSSimon Schubert   int i;
40565796c8dcSSimon Schubert   unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
40575796c8dcSSimon Schubert 
40585796c8dcSSimon Schubert   /* The packet support flags are handled differently for this packet
40595796c8dcSSimon Schubert      than for most others.  We treat an error, a disabled packet, and
40605796c8dcSSimon Schubert      an empty response identically: any features which must be reported
40615796c8dcSSimon Schubert      to be used will be automatically disabled.  An empty buffer
40625796c8dcSSimon Schubert      accomplishes this, since that is also the representation for a list
40635796c8dcSSimon Schubert      containing no features.  */
40645796c8dcSSimon Schubert 
40655796c8dcSSimon Schubert   rs->buf[0] = 0;
40665796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE)
40675796c8dcSSimon Schubert     {
4068cf7f2e2dSJohn Marino       char *q = NULL;
4069cf7f2e2dSJohn Marino       struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
4070cf7f2e2dSJohn Marino 
4071cf7f2e2dSJohn Marino       q = remote_query_supported_append (q, "multiprocess+");
4072cf7f2e2dSJohn Marino 
4073cf7f2e2dSJohn Marino       if (remote_support_xml)
4074cf7f2e2dSJohn Marino 	q = remote_query_supported_append (q, remote_support_xml);
4075cf7f2e2dSJohn Marino 
4076cf7f2e2dSJohn Marino       q = remote_query_supported_append (q, "qRelocInsn+");
4077cf7f2e2dSJohn Marino 
4078cf7f2e2dSJohn Marino       q = reconcat (q, "qSupported:", q, (char *) NULL);
4079cf7f2e2dSJohn Marino       putpkt (q);
4080cf7f2e2dSJohn Marino 
4081cf7f2e2dSJohn Marino       do_cleanups (old_chain);
40825796c8dcSSimon Schubert 
40835796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
40845796c8dcSSimon Schubert 
40855796c8dcSSimon Schubert       /* If an error occured, warn, but do not return - just reset the
40865796c8dcSSimon Schubert 	 buffer to empty and go on to disable features.  */
40875796c8dcSSimon Schubert       if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
40885796c8dcSSimon Schubert 	  == PACKET_ERROR)
40895796c8dcSSimon Schubert 	{
40905796c8dcSSimon Schubert 	  warning (_("Remote failure reply: %s"), rs->buf);
40915796c8dcSSimon Schubert 	  rs->buf[0] = 0;
40925796c8dcSSimon Schubert 	}
40935796c8dcSSimon Schubert     }
40945796c8dcSSimon Schubert 
40955796c8dcSSimon Schubert   memset (seen, 0, sizeof (seen));
40965796c8dcSSimon Schubert 
40975796c8dcSSimon Schubert   next = rs->buf;
40985796c8dcSSimon Schubert   while (*next)
40995796c8dcSSimon Schubert     {
41005796c8dcSSimon Schubert       enum packet_support is_supported;
41015796c8dcSSimon Schubert       char *p, *end, *name_end, *value;
41025796c8dcSSimon Schubert 
41035796c8dcSSimon Schubert       /* First separate out this item from the rest of the packet.  If
41045796c8dcSSimon Schubert 	 there's another item after this, we overwrite the separator
41055796c8dcSSimon Schubert 	 (terminated strings are much easier to work with).  */
41065796c8dcSSimon Schubert       p = next;
41075796c8dcSSimon Schubert       end = strchr (p, ';');
41085796c8dcSSimon Schubert       if (end == NULL)
41095796c8dcSSimon Schubert 	{
41105796c8dcSSimon Schubert 	  end = p + strlen (p);
41115796c8dcSSimon Schubert 	  next = end;
41125796c8dcSSimon Schubert 	}
41135796c8dcSSimon Schubert       else
41145796c8dcSSimon Schubert 	{
41155796c8dcSSimon Schubert 	  *end = '\0';
41165796c8dcSSimon Schubert 	  next = end + 1;
41175796c8dcSSimon Schubert 
41185796c8dcSSimon Schubert 	  if (end == p)
41195796c8dcSSimon Schubert 	    {
41205796c8dcSSimon Schubert 	      warning (_("empty item in \"qSupported\" response"));
41215796c8dcSSimon Schubert 	      continue;
41225796c8dcSSimon Schubert 	    }
41235796c8dcSSimon Schubert 	}
41245796c8dcSSimon Schubert 
41255796c8dcSSimon Schubert       name_end = strchr (p, '=');
41265796c8dcSSimon Schubert       if (name_end)
41275796c8dcSSimon Schubert 	{
41285796c8dcSSimon Schubert 	  /* This is a name=value entry.  */
41295796c8dcSSimon Schubert 	  is_supported = PACKET_ENABLE;
41305796c8dcSSimon Schubert 	  value = name_end + 1;
41315796c8dcSSimon Schubert 	  *name_end = '\0';
41325796c8dcSSimon Schubert 	}
41335796c8dcSSimon Schubert       else
41345796c8dcSSimon Schubert 	{
41355796c8dcSSimon Schubert 	  value = NULL;
41365796c8dcSSimon Schubert 	  switch (end[-1])
41375796c8dcSSimon Schubert 	    {
41385796c8dcSSimon Schubert 	    case '+':
41395796c8dcSSimon Schubert 	      is_supported = PACKET_ENABLE;
41405796c8dcSSimon Schubert 	      break;
41415796c8dcSSimon Schubert 
41425796c8dcSSimon Schubert 	    case '-':
41435796c8dcSSimon Schubert 	      is_supported = PACKET_DISABLE;
41445796c8dcSSimon Schubert 	      break;
41455796c8dcSSimon Schubert 
41465796c8dcSSimon Schubert 	    case '?':
41475796c8dcSSimon Schubert 	      is_supported = PACKET_SUPPORT_UNKNOWN;
41485796c8dcSSimon Schubert 	      break;
41495796c8dcSSimon Schubert 
41505796c8dcSSimon Schubert 	    default:
4151c50c785cSJohn Marino 	      warning (_("unrecognized item \"%s\" "
4152c50c785cSJohn Marino 			 "in \"qSupported\" response"), p);
41535796c8dcSSimon Schubert 	      continue;
41545796c8dcSSimon Schubert 	    }
41555796c8dcSSimon Schubert 	  end[-1] = '\0';
41565796c8dcSSimon Schubert 	}
41575796c8dcSSimon Schubert 
41585796c8dcSSimon Schubert       for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
41595796c8dcSSimon Schubert 	if (strcmp (remote_protocol_features[i].name, p) == 0)
41605796c8dcSSimon Schubert 	  {
41615796c8dcSSimon Schubert 	    const struct protocol_feature *feature;
41625796c8dcSSimon Schubert 
41635796c8dcSSimon Schubert 	    seen[i] = 1;
41645796c8dcSSimon Schubert 	    feature = &remote_protocol_features[i];
41655796c8dcSSimon Schubert 	    feature->func (feature, is_supported, value);
41665796c8dcSSimon Schubert 	    break;
41675796c8dcSSimon Schubert 	  }
41685796c8dcSSimon Schubert     }
41695796c8dcSSimon Schubert 
41705796c8dcSSimon Schubert   /* If we increased the packet size, make sure to increase the global
41715796c8dcSSimon Schubert      buffer size also.  We delay this until after parsing the entire
41725796c8dcSSimon Schubert      qSupported packet, because this is the same buffer we were
41735796c8dcSSimon Schubert      parsing.  */
41745796c8dcSSimon Schubert   if (rs->buf_size < rs->explicit_packet_size)
41755796c8dcSSimon Schubert     {
41765796c8dcSSimon Schubert       rs->buf_size = rs->explicit_packet_size;
41775796c8dcSSimon Schubert       rs->buf = xrealloc (rs->buf, rs->buf_size);
41785796c8dcSSimon Schubert     }
41795796c8dcSSimon Schubert 
41805796c8dcSSimon Schubert   /* Handle the defaults for unmentioned features.  */
41815796c8dcSSimon Schubert   for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
41825796c8dcSSimon Schubert     if (!seen[i])
41835796c8dcSSimon Schubert       {
41845796c8dcSSimon Schubert 	const struct protocol_feature *feature;
41855796c8dcSSimon Schubert 
41865796c8dcSSimon Schubert 	feature = &remote_protocol_features[i];
41875796c8dcSSimon Schubert 	feature->func (feature, feature->default_support, NULL);
41885796c8dcSSimon Schubert       }
41895796c8dcSSimon Schubert }
41905796c8dcSSimon Schubert 
4191ef5ccd6cSJohn Marino /* Remove any of the remote.c targets from target stack.  Upper targets depend
4192ef5ccd6cSJohn Marino    on it so remove them first.  */
4193ef5ccd6cSJohn Marino 
4194ef5ccd6cSJohn Marino static void
remote_unpush_target(void)4195ef5ccd6cSJohn Marino remote_unpush_target (void)
4196ef5ccd6cSJohn Marino {
4197ef5ccd6cSJohn Marino   pop_all_targets_above (process_stratum - 1, 0);
4198ef5ccd6cSJohn Marino }
41995796c8dcSSimon Schubert 
42005796c8dcSSimon Schubert static void
remote_open_1(char * name,int from_tty,struct target_ops * target,int extended_p)4201c50c785cSJohn Marino remote_open_1 (char *name, int from_tty,
4202c50c785cSJohn Marino 	       struct target_ops *target, int extended_p)
42035796c8dcSSimon Schubert {
42045796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
42055796c8dcSSimon Schubert 
42065796c8dcSSimon Schubert   if (name == 0)
42075796c8dcSSimon Schubert     error (_("To open a remote debug connection, you need to specify what\n"
42085796c8dcSSimon Schubert 	   "serial device is attached to the remote system\n"
42095796c8dcSSimon Schubert 	   "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
42105796c8dcSSimon Schubert 
42115796c8dcSSimon Schubert   /* See FIXME above.  */
42125796c8dcSSimon Schubert   if (!target_async_permitted)
42135796c8dcSSimon Schubert     wait_forever_enabled_p = 1;
42145796c8dcSSimon Schubert 
42155796c8dcSSimon Schubert   /* If we're connected to a running target, target_preopen will kill it.
4216ef5ccd6cSJohn Marino      Ask this question first, before target_preopen has a chance to kill
4217ef5ccd6cSJohn Marino      anything.  */
42185796c8dcSSimon Schubert   if (remote_desc != NULL && !have_inferiors ())
42195796c8dcSSimon Schubert     {
4220ef5ccd6cSJohn Marino       if (from_tty
4221ef5ccd6cSJohn Marino 	  && !query (_("Already connected to a remote target.  Disconnect? ")))
42225796c8dcSSimon Schubert 	error (_("Still connected."));
42235796c8dcSSimon Schubert     }
42245796c8dcSSimon Schubert 
4225ef5ccd6cSJohn Marino   /* Here the possibly existing remote target gets unpushed.  */
42265796c8dcSSimon Schubert   target_preopen (from_tty);
42275796c8dcSSimon Schubert 
42285796c8dcSSimon Schubert   /* Make sure we send the passed signals list the next time we resume.  */
42295796c8dcSSimon Schubert   xfree (last_pass_packet);
42305796c8dcSSimon Schubert   last_pass_packet = NULL;
42315796c8dcSSimon Schubert 
4232ef5ccd6cSJohn Marino   /* Make sure we send the program signals list the next time we
4233ef5ccd6cSJohn Marino      resume.  */
4234ef5ccd6cSJohn Marino   xfree (last_program_signals_packet);
4235ef5ccd6cSJohn Marino   last_program_signals_packet = NULL;
4236ef5ccd6cSJohn Marino 
42375796c8dcSSimon Schubert   remote_fileio_reset ();
42385796c8dcSSimon Schubert   reopen_exec_file ();
42395796c8dcSSimon Schubert   reread_symbols ();
42405796c8dcSSimon Schubert 
42415796c8dcSSimon Schubert   remote_desc = remote_serial_open (name);
42425796c8dcSSimon Schubert   if (!remote_desc)
42435796c8dcSSimon Schubert     perror_with_name (name);
42445796c8dcSSimon Schubert 
42455796c8dcSSimon Schubert   if (baud_rate != -1)
42465796c8dcSSimon Schubert     {
42475796c8dcSSimon Schubert       if (serial_setbaudrate (remote_desc, baud_rate))
42485796c8dcSSimon Schubert 	{
42495796c8dcSSimon Schubert 	  /* The requested speed could not be set.  Error out to
42505796c8dcSSimon Schubert 	     top level after closing remote_desc.  Take care to
42515796c8dcSSimon Schubert 	     set remote_desc to NULL to avoid closing remote_desc
42525796c8dcSSimon Schubert 	     more than once.  */
42535796c8dcSSimon Schubert 	  serial_close (remote_desc);
42545796c8dcSSimon Schubert 	  remote_desc = NULL;
42555796c8dcSSimon Schubert 	  perror_with_name (name);
42565796c8dcSSimon Schubert 	}
42575796c8dcSSimon Schubert     }
42585796c8dcSSimon Schubert 
42595796c8dcSSimon Schubert   serial_raw (remote_desc);
42605796c8dcSSimon Schubert 
42615796c8dcSSimon Schubert   /* If there is something sitting in the buffer we might take it as a
42625796c8dcSSimon Schubert      response to a command, which would be bad.  */
42635796c8dcSSimon Schubert   serial_flush_input (remote_desc);
42645796c8dcSSimon Schubert 
42655796c8dcSSimon Schubert   if (from_tty)
42665796c8dcSSimon Schubert     {
42675796c8dcSSimon Schubert       puts_filtered ("Remote debugging using ");
42685796c8dcSSimon Schubert       puts_filtered (name);
42695796c8dcSSimon Schubert       puts_filtered ("\n");
42705796c8dcSSimon Schubert     }
42715796c8dcSSimon Schubert   push_target (target);		/* Switch to using remote target now.  */
42725796c8dcSSimon Schubert 
42735796c8dcSSimon Schubert   /* Register extra event sources in the event loop.  */
42745796c8dcSSimon Schubert   remote_async_inferior_event_token
42755796c8dcSSimon Schubert     = create_async_event_handler (remote_async_inferior_event_handler,
42765796c8dcSSimon Schubert 				  NULL);
4277ef5ccd6cSJohn Marino   remote_notif_register_async_event_handler ();
42785796c8dcSSimon Schubert 
42795796c8dcSSimon Schubert   /* Reset the target state; these things will be queried either by
42805796c8dcSSimon Schubert      remote_query_supported or as they are needed.  */
42815796c8dcSSimon Schubert   init_all_packet_configs ();
42825796c8dcSSimon Schubert   rs->cached_wait_status = 0;
42835796c8dcSSimon Schubert   rs->explicit_packet_size = 0;
42845796c8dcSSimon Schubert   rs->noack_mode = 0;
42855796c8dcSSimon Schubert   rs->multi_process_aware = 0;
42865796c8dcSSimon Schubert   rs->extended = extended_p;
42875796c8dcSSimon Schubert   rs->non_stop_aware = 0;
42885796c8dcSSimon Schubert   rs->waiting_for_stop_reply = 0;
4289cf7f2e2dSJohn Marino   rs->ctrlc_pending_p = 0;
42905796c8dcSSimon Schubert 
42915796c8dcSSimon Schubert   general_thread = not_sent_ptid;
42925796c8dcSSimon Schubert   continue_thread = not_sent_ptid;
4293c50c785cSJohn Marino   remote_traceframe_number = -1;
42945796c8dcSSimon Schubert 
42955796c8dcSSimon Schubert   /* Probe for ability to use "ThreadInfo" query, as required.  */
42965796c8dcSSimon Schubert   use_threadinfo_query = 1;
42975796c8dcSSimon Schubert   use_threadextra_query = 1;
42985796c8dcSSimon Schubert 
42995796c8dcSSimon Schubert   if (target_async_permitted)
43005796c8dcSSimon Schubert     {
43015796c8dcSSimon Schubert       /* With this target we start out by owning the terminal.  */
43025796c8dcSSimon Schubert       remote_async_terminal_ours_p = 1;
43035796c8dcSSimon Schubert 
43045796c8dcSSimon Schubert       /* FIXME: cagney/1999-09-23: During the initial connection it is
43055796c8dcSSimon Schubert 	 assumed that the target is already ready and able to respond to
43065796c8dcSSimon Schubert 	 requests.  Unfortunately remote_start_remote() eventually calls
43075796c8dcSSimon Schubert 	 wait_for_inferior() with no timeout.  wait_forever_enabled_p gets
43085796c8dcSSimon Schubert 	 around this.  Eventually a mechanism that allows
43095796c8dcSSimon Schubert 	 wait_for_inferior() to expect/get timeouts will be
43105796c8dcSSimon Schubert 	 implemented.  */
43115796c8dcSSimon Schubert       wait_forever_enabled_p = 0;
43125796c8dcSSimon Schubert     }
43135796c8dcSSimon Schubert 
43145796c8dcSSimon Schubert   /* First delete any symbols previously loaded from shared libraries.  */
43155796c8dcSSimon Schubert   no_shared_libraries (NULL, 0);
43165796c8dcSSimon Schubert 
43175796c8dcSSimon Schubert   /* Start afresh.  */
43185796c8dcSSimon Schubert   init_thread_list ();
43195796c8dcSSimon Schubert 
43205796c8dcSSimon Schubert   /* Start the remote connection.  If error() or QUIT, discard this
43215796c8dcSSimon Schubert      target (we'd otherwise be in an inconsistent state) and then
43225796c8dcSSimon Schubert      propogate the error on up the exception chain.  This ensures that
43235796c8dcSSimon Schubert      the caller doesn't stumble along blindly assuming that the
43245796c8dcSSimon Schubert      function succeeded.  The CLI doesn't have this problem but other
43255796c8dcSSimon Schubert      UI's, such as MI do.
43265796c8dcSSimon Schubert 
43275796c8dcSSimon Schubert      FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
43285796c8dcSSimon Schubert      this function should return an error indication letting the
43295796c8dcSSimon Schubert      caller restore the previous state.  Unfortunately the command
43305796c8dcSSimon Schubert      ``target remote'' is directly wired to this function making that
43315796c8dcSSimon Schubert      impossible.  On a positive note, the CLI side of this problem has
43325796c8dcSSimon Schubert      been fixed - the function set_cmd_context() makes it possible for
43335796c8dcSSimon Schubert      all the ``target ....'' commands to share a common callback
43345796c8dcSSimon Schubert      function.  See cli-dump.c.  */
43355796c8dcSSimon Schubert   {
4336a45ae5f8SJohn Marino     volatile struct gdb_exception ex;
43375796c8dcSSimon Schubert 
4338a45ae5f8SJohn Marino     TRY_CATCH (ex, RETURN_MASK_ALL)
4339a45ae5f8SJohn Marino       {
4340a45ae5f8SJohn Marino 	remote_start_remote (from_tty, target, extended_p);
4341a45ae5f8SJohn Marino       }
43425796c8dcSSimon Schubert     if (ex.reason < 0)
43435796c8dcSSimon Schubert       {
43445796c8dcSSimon Schubert 	/* Pop the partially set up target - unless something else did
43455796c8dcSSimon Schubert 	   already before throwing the exception.  */
43465796c8dcSSimon Schubert 	if (remote_desc != NULL)
4347ef5ccd6cSJohn Marino 	  remote_unpush_target ();
43485796c8dcSSimon Schubert 	if (target_async_permitted)
43495796c8dcSSimon Schubert 	  wait_forever_enabled_p = 1;
43505796c8dcSSimon Schubert 	throw_exception (ex);
43515796c8dcSSimon Schubert       }
43525796c8dcSSimon Schubert   }
43535796c8dcSSimon Schubert 
43545796c8dcSSimon Schubert   if (target_async_permitted)
43555796c8dcSSimon Schubert     wait_forever_enabled_p = 1;
43565796c8dcSSimon Schubert }
43575796c8dcSSimon Schubert 
43585796c8dcSSimon Schubert /* This takes a program previously attached to and detaches it.  After
43595796c8dcSSimon Schubert    this is done, GDB can be used to debug some other program.  We
43605796c8dcSSimon Schubert    better not have left any breakpoints in the target program or it'll
43615796c8dcSSimon Schubert    die when it hits one.  */
43625796c8dcSSimon Schubert 
43635796c8dcSSimon Schubert static void
remote_detach_1(char * args,int from_tty,int extended)43645796c8dcSSimon Schubert remote_detach_1 (char *args, int from_tty, int extended)
43655796c8dcSSimon Schubert {
43665796c8dcSSimon Schubert   int pid = ptid_get_pid (inferior_ptid);
43675796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
43685796c8dcSSimon Schubert 
43695796c8dcSSimon Schubert   if (args)
43705796c8dcSSimon Schubert     error (_("Argument given to \"detach\" when remotely debugging."));
43715796c8dcSSimon Schubert 
43725796c8dcSSimon Schubert   if (!target_has_execution)
43735796c8dcSSimon Schubert     error (_("No process to detach from."));
43745796c8dcSSimon Schubert 
4375ef5ccd6cSJohn Marino   if (from_tty)
4376ef5ccd6cSJohn Marino     {
4377ef5ccd6cSJohn Marino       char *exec_file = get_exec_file (0);
4378ef5ccd6cSJohn Marino       if (exec_file == NULL)
4379ef5ccd6cSJohn Marino 	exec_file = "";
4380ef5ccd6cSJohn Marino       printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4381ef5ccd6cSJohn Marino 			 target_pid_to_str (pid_to_ptid (pid)));
4382ef5ccd6cSJohn Marino       gdb_flush (gdb_stdout);
4383ef5ccd6cSJohn Marino     }
4384ef5ccd6cSJohn Marino 
43855796c8dcSSimon Schubert   /* Tell the remote target to detach.  */
43865796c8dcSSimon Schubert   if (remote_multi_process_p (rs))
4387ef5ccd6cSJohn Marino     xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
43885796c8dcSSimon Schubert   else
43895796c8dcSSimon Schubert     strcpy (rs->buf, "D");
43905796c8dcSSimon Schubert 
43915796c8dcSSimon Schubert   putpkt (rs->buf);
43925796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
43935796c8dcSSimon Schubert 
43945796c8dcSSimon Schubert   if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
43955796c8dcSSimon Schubert     ;
43965796c8dcSSimon Schubert   else if (rs->buf[0] == '\0')
43975796c8dcSSimon Schubert     error (_("Remote doesn't know how to detach"));
43985796c8dcSSimon Schubert   else
43995796c8dcSSimon Schubert     error (_("Can't detach process."));
44005796c8dcSSimon Schubert 
4401ef5ccd6cSJohn Marino   if (from_tty && !extended)
44025796c8dcSSimon Schubert     puts_filtered (_("Ending remote debugging.\n"));
44035796c8dcSSimon Schubert 
44045796c8dcSSimon Schubert   target_mourn_inferior ();
44055796c8dcSSimon Schubert }
44065796c8dcSSimon Schubert 
44075796c8dcSSimon Schubert static void
remote_detach(struct target_ops * ops,char * args,int from_tty)44085796c8dcSSimon Schubert remote_detach (struct target_ops *ops, char *args, int from_tty)
44095796c8dcSSimon Schubert {
44105796c8dcSSimon Schubert   remote_detach_1 (args, from_tty, 0);
44115796c8dcSSimon Schubert }
44125796c8dcSSimon Schubert 
44135796c8dcSSimon Schubert static void
extended_remote_detach(struct target_ops * ops,char * args,int from_tty)44145796c8dcSSimon Schubert extended_remote_detach (struct target_ops *ops, char *args, int from_tty)
44155796c8dcSSimon Schubert {
44165796c8dcSSimon Schubert   remote_detach_1 (args, from_tty, 1);
44175796c8dcSSimon Schubert }
44185796c8dcSSimon Schubert 
44195796c8dcSSimon Schubert /* Same as remote_detach, but don't send the "D" packet; just disconnect.  */
44205796c8dcSSimon Schubert 
44215796c8dcSSimon Schubert static void
remote_disconnect(struct target_ops * target,char * args,int from_tty)44225796c8dcSSimon Schubert remote_disconnect (struct target_ops *target, char *args, int from_tty)
44235796c8dcSSimon Schubert {
44245796c8dcSSimon Schubert   if (args)
44255796c8dcSSimon Schubert     error (_("Argument given to \"disconnect\" when remotely debugging."));
44265796c8dcSSimon Schubert 
44275796c8dcSSimon Schubert   /* Make sure we unpush even the extended remote targets; mourn
44285796c8dcSSimon Schubert      won't do it.  So call remote_mourn_1 directly instead of
44295796c8dcSSimon Schubert      target_mourn_inferior.  */
44305796c8dcSSimon Schubert   remote_mourn_1 (target);
44315796c8dcSSimon Schubert 
44325796c8dcSSimon Schubert   if (from_tty)
44335796c8dcSSimon Schubert     puts_filtered ("Ending remote debugging.\n");
44345796c8dcSSimon Schubert }
44355796c8dcSSimon Schubert 
44365796c8dcSSimon Schubert /* Attach to the process specified by ARGS.  If FROM_TTY is non-zero,
44375796c8dcSSimon Schubert    be chatty about it.  */
44385796c8dcSSimon Schubert 
44395796c8dcSSimon Schubert static void
extended_remote_attach_1(struct target_ops * target,char * args,int from_tty)44405796c8dcSSimon Schubert extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty)
44415796c8dcSSimon Schubert {
44425796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
44435796c8dcSSimon Schubert   int pid;
44445796c8dcSSimon Schubert   char *wait_status = NULL;
44455796c8dcSSimon Schubert 
4446cf7f2e2dSJohn Marino   pid = parse_pid_to_attach (args);
44475796c8dcSSimon Schubert 
4448cf7f2e2dSJohn Marino   /* Remote PID can be freely equal to getpid, do not check it here the same
4449cf7f2e2dSJohn Marino      way as in other targets.  */
44505796c8dcSSimon Schubert 
44515796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
44525796c8dcSSimon Schubert     error (_("This target does not support attaching to a process"));
44535796c8dcSSimon Schubert 
4454ef5ccd6cSJohn Marino   if (from_tty)
4455ef5ccd6cSJohn Marino     {
4456ef5ccd6cSJohn Marino       char *exec_file = get_exec_file (0);
4457ef5ccd6cSJohn Marino 
4458ef5ccd6cSJohn Marino       if (exec_file)
4459ef5ccd6cSJohn Marino 	printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4460ef5ccd6cSJohn Marino 			   target_pid_to_str (pid_to_ptid (pid)));
4461ef5ccd6cSJohn Marino       else
4462ef5ccd6cSJohn Marino 	printf_unfiltered (_("Attaching to %s\n"),
4463ef5ccd6cSJohn Marino 			   target_pid_to_str (pid_to_ptid (pid)));
4464ef5ccd6cSJohn Marino 
4465ef5ccd6cSJohn Marino       gdb_flush (gdb_stdout);
4466ef5ccd6cSJohn Marino     }
4467ef5ccd6cSJohn Marino 
4468ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
44695796c8dcSSimon Schubert   putpkt (rs->buf);
44705796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
44715796c8dcSSimon Schubert 
4472c50c785cSJohn Marino   if (packet_ok (rs->buf,
4473c50c785cSJohn Marino 		 &remote_protocol_packets[PACKET_vAttach]) == PACKET_OK)
44745796c8dcSSimon Schubert     {
44755796c8dcSSimon Schubert       if (!non_stop)
44765796c8dcSSimon Schubert 	{
44775796c8dcSSimon Schubert 	  /* Save the reply for later.  */
44785796c8dcSSimon Schubert 	  wait_status = alloca (strlen (rs->buf) + 1);
44795796c8dcSSimon Schubert 	  strcpy (wait_status, rs->buf);
44805796c8dcSSimon Schubert 	}
44815796c8dcSSimon Schubert       else if (strcmp (rs->buf, "OK") != 0)
44825796c8dcSSimon Schubert 	error (_("Attaching to %s failed with: %s"),
44835796c8dcSSimon Schubert 	       target_pid_to_str (pid_to_ptid (pid)),
44845796c8dcSSimon Schubert 	       rs->buf);
44855796c8dcSSimon Schubert     }
44865796c8dcSSimon Schubert   else if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
44875796c8dcSSimon Schubert     error (_("This target does not support attaching to a process"));
44885796c8dcSSimon Schubert   else
44895796c8dcSSimon Schubert     error (_("Attaching to %s failed"),
44905796c8dcSSimon Schubert 	   target_pid_to_str (pid_to_ptid (pid)));
44915796c8dcSSimon Schubert 
4492ef5ccd6cSJohn Marino   set_current_inferior (remote_add_inferior (0, pid, 1));
44935796c8dcSSimon Schubert 
44945796c8dcSSimon Schubert   inferior_ptid = pid_to_ptid (pid);
44955796c8dcSSimon Schubert 
44965796c8dcSSimon Schubert   if (non_stop)
44975796c8dcSSimon Schubert     {
44985796c8dcSSimon Schubert       struct thread_info *thread;
44995796c8dcSSimon Schubert 
45005796c8dcSSimon Schubert       /* Get list of threads.  */
45015796c8dcSSimon Schubert       remote_threads_info (target);
45025796c8dcSSimon Schubert 
45035796c8dcSSimon Schubert       thread = first_thread_of_process (pid);
45045796c8dcSSimon Schubert       if (thread)
45055796c8dcSSimon Schubert 	inferior_ptid = thread->ptid;
45065796c8dcSSimon Schubert       else
45075796c8dcSSimon Schubert 	inferior_ptid = pid_to_ptid (pid);
45085796c8dcSSimon Schubert 
45095796c8dcSSimon Schubert       /* Invalidate our notion of the remote current thread.  */
45105796c8dcSSimon Schubert       record_currthread (minus_one_ptid);
45115796c8dcSSimon Schubert     }
45125796c8dcSSimon Schubert   else
45135796c8dcSSimon Schubert     {
45145796c8dcSSimon Schubert       /* Now, if we have thread information, update inferior_ptid.  */
45155796c8dcSSimon Schubert       inferior_ptid = remote_current_thread (inferior_ptid);
45165796c8dcSSimon Schubert 
45175796c8dcSSimon Schubert       /* Add the main thread to the thread list.  */
45185796c8dcSSimon Schubert       add_thread_silent (inferior_ptid);
45195796c8dcSSimon Schubert     }
45205796c8dcSSimon Schubert 
45215796c8dcSSimon Schubert   /* Next, if the target can specify a description, read it.  We do
45225796c8dcSSimon Schubert      this before anything involving memory or registers.  */
45235796c8dcSSimon Schubert   target_find_description ();
45245796c8dcSSimon Schubert 
45255796c8dcSSimon Schubert   if (!non_stop)
45265796c8dcSSimon Schubert     {
45275796c8dcSSimon Schubert       /* Use the previously fetched status.  */
45285796c8dcSSimon Schubert       gdb_assert (wait_status != NULL);
45295796c8dcSSimon Schubert 
45305796c8dcSSimon Schubert       if (target_can_async_p ())
45315796c8dcSSimon Schubert 	{
4532ef5ccd6cSJohn Marino 	  struct notif_event *reply
4533ef5ccd6cSJohn Marino 	    =  remote_notif_parse (&notif_client_stop, wait_status);
45345796c8dcSSimon Schubert 
4535ef5ccd6cSJohn Marino 	  push_stop_reply ((struct stop_reply *) reply);
45365796c8dcSSimon Schubert 
45375796c8dcSSimon Schubert 	  target_async (inferior_event_handler, 0);
45385796c8dcSSimon Schubert 	}
45395796c8dcSSimon Schubert       else
45405796c8dcSSimon Schubert 	{
45415796c8dcSSimon Schubert 	  gdb_assert (wait_status != NULL);
45425796c8dcSSimon Schubert 	  strcpy (rs->buf, wait_status);
45435796c8dcSSimon Schubert 	  rs->cached_wait_status = 1;
45445796c8dcSSimon Schubert 	}
45455796c8dcSSimon Schubert     }
45465796c8dcSSimon Schubert   else
45475796c8dcSSimon Schubert     gdb_assert (wait_status == NULL);
45485796c8dcSSimon Schubert }
45495796c8dcSSimon Schubert 
45505796c8dcSSimon Schubert static void
extended_remote_attach(struct target_ops * ops,char * args,int from_tty)45515796c8dcSSimon Schubert extended_remote_attach (struct target_ops *ops, char *args, int from_tty)
45525796c8dcSSimon Schubert {
45535796c8dcSSimon Schubert   extended_remote_attach_1 (ops, args, from_tty);
45545796c8dcSSimon Schubert }
45555796c8dcSSimon Schubert 
45565796c8dcSSimon Schubert /* Convert hex digit A to a number.  */
45575796c8dcSSimon Schubert 
45585796c8dcSSimon Schubert static int
fromhex(int a)45595796c8dcSSimon Schubert fromhex (int a)
45605796c8dcSSimon Schubert {
45615796c8dcSSimon Schubert   if (a >= '0' && a <= '9')
45625796c8dcSSimon Schubert     return a - '0';
45635796c8dcSSimon Schubert   else if (a >= 'a' && a <= 'f')
45645796c8dcSSimon Schubert     return a - 'a' + 10;
45655796c8dcSSimon Schubert   else if (a >= 'A' && a <= 'F')
45665796c8dcSSimon Schubert     return a - 'A' + 10;
45675796c8dcSSimon Schubert   else
45685796c8dcSSimon Schubert     error (_("Reply contains invalid hex digit %d"), a);
45695796c8dcSSimon Schubert }
45705796c8dcSSimon Schubert 
4571cf7f2e2dSJohn Marino int
hex2bin(const char * hex,gdb_byte * bin,int count)45725796c8dcSSimon Schubert hex2bin (const char *hex, gdb_byte *bin, int count)
45735796c8dcSSimon Schubert {
45745796c8dcSSimon Schubert   int i;
45755796c8dcSSimon Schubert 
45765796c8dcSSimon Schubert   for (i = 0; i < count; i++)
45775796c8dcSSimon Schubert     {
45785796c8dcSSimon Schubert       if (hex[0] == 0 || hex[1] == 0)
45795796c8dcSSimon Schubert 	{
45805796c8dcSSimon Schubert 	  /* Hex string is short, or of uneven length.
45815796c8dcSSimon Schubert 	     Return the count that has been converted so far.  */
45825796c8dcSSimon Schubert 	  return i;
45835796c8dcSSimon Schubert 	}
45845796c8dcSSimon Schubert       *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
45855796c8dcSSimon Schubert       hex += 2;
45865796c8dcSSimon Schubert     }
45875796c8dcSSimon Schubert   return i;
45885796c8dcSSimon Schubert }
45895796c8dcSSimon Schubert 
45905796c8dcSSimon Schubert /* Convert number NIB to a hex digit.  */
45915796c8dcSSimon Schubert 
45925796c8dcSSimon Schubert static int
tohex(int nib)45935796c8dcSSimon Schubert tohex (int nib)
45945796c8dcSSimon Schubert {
45955796c8dcSSimon Schubert   if (nib < 10)
45965796c8dcSSimon Schubert     return '0' + nib;
45975796c8dcSSimon Schubert   else
45985796c8dcSSimon Schubert     return 'a' + nib - 10;
45995796c8dcSSimon Schubert }
46005796c8dcSSimon Schubert 
4601cf7f2e2dSJohn Marino int
bin2hex(const gdb_byte * bin,char * hex,int count)46025796c8dcSSimon Schubert bin2hex (const gdb_byte *bin, char *hex, int count)
46035796c8dcSSimon Schubert {
46045796c8dcSSimon Schubert   int i;
4605cf7f2e2dSJohn Marino 
46065796c8dcSSimon Schubert   /* May use a length, or a nul-terminated string as input.  */
46075796c8dcSSimon Schubert   if (count == 0)
46085796c8dcSSimon Schubert     count = strlen ((char *) bin);
46095796c8dcSSimon Schubert 
46105796c8dcSSimon Schubert   for (i = 0; i < count; i++)
46115796c8dcSSimon Schubert     {
46125796c8dcSSimon Schubert       *hex++ = tohex ((*bin >> 4) & 0xf);
46135796c8dcSSimon Schubert       *hex++ = tohex (*bin++ & 0xf);
46145796c8dcSSimon Schubert     }
46155796c8dcSSimon Schubert   *hex = 0;
46165796c8dcSSimon Schubert   return i;
46175796c8dcSSimon Schubert }
46185796c8dcSSimon Schubert 
46195796c8dcSSimon Schubert /* Check for the availability of vCont.  This function should also check
46205796c8dcSSimon Schubert    the response.  */
46215796c8dcSSimon Schubert 
46225796c8dcSSimon Schubert static void
remote_vcont_probe(struct remote_state * rs)46235796c8dcSSimon Schubert remote_vcont_probe (struct remote_state *rs)
46245796c8dcSSimon Schubert {
46255796c8dcSSimon Schubert   char *buf;
46265796c8dcSSimon Schubert 
46275796c8dcSSimon Schubert   strcpy (rs->buf, "vCont?");
46285796c8dcSSimon Schubert   putpkt (rs->buf);
46295796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
46305796c8dcSSimon Schubert   buf = rs->buf;
46315796c8dcSSimon Schubert 
46325796c8dcSSimon Schubert   /* Make sure that the features we assume are supported.  */
46335796c8dcSSimon Schubert   if (strncmp (buf, "vCont", 5) == 0)
46345796c8dcSSimon Schubert     {
46355796c8dcSSimon Schubert       char *p = &buf[5];
46365796c8dcSSimon Schubert       int support_s, support_S, support_c, support_C;
46375796c8dcSSimon Schubert 
46385796c8dcSSimon Schubert       support_s = 0;
46395796c8dcSSimon Schubert       support_S = 0;
46405796c8dcSSimon Schubert       support_c = 0;
46415796c8dcSSimon Schubert       support_C = 0;
46425796c8dcSSimon Schubert       rs->support_vCont_t = 0;
46435796c8dcSSimon Schubert       while (p && *p == ';')
46445796c8dcSSimon Schubert 	{
46455796c8dcSSimon Schubert 	  p++;
46465796c8dcSSimon Schubert 	  if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
46475796c8dcSSimon Schubert 	    support_s = 1;
46485796c8dcSSimon Schubert 	  else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
46495796c8dcSSimon Schubert 	    support_S = 1;
46505796c8dcSSimon Schubert 	  else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
46515796c8dcSSimon Schubert 	    support_c = 1;
46525796c8dcSSimon Schubert 	  else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
46535796c8dcSSimon Schubert 	    support_C = 1;
46545796c8dcSSimon Schubert 	  else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
46555796c8dcSSimon Schubert 	    rs->support_vCont_t = 1;
46565796c8dcSSimon Schubert 
46575796c8dcSSimon Schubert 	  p = strchr (p, ';');
46585796c8dcSSimon Schubert 	}
46595796c8dcSSimon Schubert 
46605796c8dcSSimon Schubert       /* If s, S, c, and C are not all supported, we can't use vCont.  Clearing
46615796c8dcSSimon Schubert          BUF will make packet_ok disable the packet.  */
46625796c8dcSSimon Schubert       if (!support_s || !support_S || !support_c || !support_C)
46635796c8dcSSimon Schubert 	buf[0] = 0;
46645796c8dcSSimon Schubert     }
46655796c8dcSSimon Schubert 
46665796c8dcSSimon Schubert   packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
46675796c8dcSSimon Schubert }
46685796c8dcSSimon Schubert 
46695796c8dcSSimon Schubert /* Helper function for building "vCont" resumptions.  Write a
46705796c8dcSSimon Schubert    resumption to P.  ENDP points to one-passed-the-end of the buffer
46715796c8dcSSimon Schubert    we're allowed to write to.  Returns BUF+CHARACTERS_WRITTEN.  The
46725796c8dcSSimon Schubert    thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
46735796c8dcSSimon Schubert    resumed thread should be single-stepped and/or signalled.  If PTID
46745796c8dcSSimon Schubert    equals minus_one_ptid, then all threads are resumed; if PTID
46755796c8dcSSimon Schubert    represents a process, then all threads of the process are resumed;
46765796c8dcSSimon Schubert    the thread to be stepped and/or signalled is given in the global
46775796c8dcSSimon Schubert    INFERIOR_PTID.  */
46785796c8dcSSimon Schubert 
46795796c8dcSSimon Schubert static char *
append_resumption(char * p,char * endp,ptid_t ptid,int step,enum gdb_signal siggnal)46805796c8dcSSimon Schubert append_resumption (char *p, char *endp,
4681ef5ccd6cSJohn Marino 		   ptid_t ptid, int step, enum gdb_signal siggnal)
46825796c8dcSSimon Schubert {
46835796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
46845796c8dcSSimon Schubert 
4685ef5ccd6cSJohn Marino   if (step && siggnal != GDB_SIGNAL_0)
46865796c8dcSSimon Schubert     p += xsnprintf (p, endp - p, ";S%02x", siggnal);
46875796c8dcSSimon Schubert   else if (step)
46885796c8dcSSimon Schubert     p += xsnprintf (p, endp - p, ";s");
4689ef5ccd6cSJohn Marino   else if (siggnal != GDB_SIGNAL_0)
46905796c8dcSSimon Schubert     p += xsnprintf (p, endp - p, ";C%02x", siggnal);
46915796c8dcSSimon Schubert   else
46925796c8dcSSimon Schubert     p += xsnprintf (p, endp - p, ";c");
46935796c8dcSSimon Schubert 
46945796c8dcSSimon Schubert   if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
46955796c8dcSSimon Schubert     {
46965796c8dcSSimon Schubert       ptid_t nptid;
46975796c8dcSSimon Schubert 
46985796c8dcSSimon Schubert       /* All (-1) threads of process.  */
46995796c8dcSSimon Schubert       nptid = ptid_build (ptid_get_pid (ptid), 0, -1);
47005796c8dcSSimon Schubert 
47015796c8dcSSimon Schubert       p += xsnprintf (p, endp - p, ":");
47025796c8dcSSimon Schubert       p = write_ptid (p, endp, nptid);
47035796c8dcSSimon Schubert     }
47045796c8dcSSimon Schubert   else if (!ptid_equal (ptid, minus_one_ptid))
47055796c8dcSSimon Schubert     {
47065796c8dcSSimon Schubert       p += xsnprintf (p, endp - p, ":");
47075796c8dcSSimon Schubert       p = write_ptid (p, endp, ptid);
47085796c8dcSSimon Schubert     }
47095796c8dcSSimon Schubert 
47105796c8dcSSimon Schubert   return p;
47115796c8dcSSimon Schubert }
47125796c8dcSSimon Schubert 
4713ef5ccd6cSJohn Marino /* Append a vCont continue-with-signal action for threads that have a
4714ef5ccd6cSJohn Marino    non-zero stop signal.  */
4715ef5ccd6cSJohn Marino 
4716ef5ccd6cSJohn Marino static char *
append_pending_thread_resumptions(char * p,char * endp,ptid_t ptid)4717ef5ccd6cSJohn Marino append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid)
4718ef5ccd6cSJohn Marino {
4719ef5ccd6cSJohn Marino   struct thread_info *thread;
4720ef5ccd6cSJohn Marino 
4721ef5ccd6cSJohn Marino   ALL_THREADS (thread)
4722ef5ccd6cSJohn Marino     if (ptid_match (thread->ptid, ptid)
4723ef5ccd6cSJohn Marino 	&& !ptid_equal (inferior_ptid, thread->ptid)
4724ef5ccd6cSJohn Marino 	&& thread->suspend.stop_signal != GDB_SIGNAL_0
4725ef5ccd6cSJohn Marino 	&& signal_pass_state (thread->suspend.stop_signal))
4726ef5ccd6cSJohn Marino       {
4727ef5ccd6cSJohn Marino 	p = append_resumption (p, endp, thread->ptid,
4728ef5ccd6cSJohn Marino 			       0, thread->suspend.stop_signal);
4729ef5ccd6cSJohn Marino 	thread->suspend.stop_signal = GDB_SIGNAL_0;
4730ef5ccd6cSJohn Marino       }
4731ef5ccd6cSJohn Marino 
4732ef5ccd6cSJohn Marino   return p;
4733ef5ccd6cSJohn Marino }
4734ef5ccd6cSJohn Marino 
47355796c8dcSSimon Schubert /* Resume the remote inferior by using a "vCont" packet.  The thread
47365796c8dcSSimon Schubert    to be resumed is PTID; STEP and SIGGNAL indicate whether the
47375796c8dcSSimon Schubert    resumed thread should be single-stepped and/or signalled.  If PTID
47385796c8dcSSimon Schubert    equals minus_one_ptid, then all threads are resumed; the thread to
47395796c8dcSSimon Schubert    be stepped and/or signalled is given in the global INFERIOR_PTID.
47405796c8dcSSimon Schubert    This function returns non-zero iff it resumes the inferior.
47415796c8dcSSimon Schubert 
47425796c8dcSSimon Schubert    This function issues a strict subset of all possible vCont commands at the
47435796c8dcSSimon Schubert    moment.  */
47445796c8dcSSimon Schubert 
47455796c8dcSSimon Schubert static int
remote_vcont_resume(ptid_t ptid,int step,enum gdb_signal siggnal)4746ef5ccd6cSJohn Marino remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal)
47475796c8dcSSimon Schubert {
47485796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
47495796c8dcSSimon Schubert   char *p;
47505796c8dcSSimon Schubert   char *endp;
47515796c8dcSSimon Schubert 
47525796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
47535796c8dcSSimon Schubert     remote_vcont_probe (rs);
47545796c8dcSSimon Schubert 
47555796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE)
47565796c8dcSSimon Schubert     return 0;
47575796c8dcSSimon Schubert 
47585796c8dcSSimon Schubert   p = rs->buf;
47595796c8dcSSimon Schubert   endp = rs->buf + get_remote_packet_size ();
47605796c8dcSSimon Schubert 
47615796c8dcSSimon Schubert   /* If we could generate a wider range of packets, we'd have to worry
47625796c8dcSSimon Schubert      about overflowing BUF.  Should there be a generic
47635796c8dcSSimon Schubert      "multi-part-packet" packet?  */
47645796c8dcSSimon Schubert 
47655796c8dcSSimon Schubert   p += xsnprintf (p, endp - p, "vCont");
47665796c8dcSSimon Schubert 
47675796c8dcSSimon Schubert   if (ptid_equal (ptid, magic_null_ptid))
47685796c8dcSSimon Schubert     {
47695796c8dcSSimon Schubert       /* MAGIC_NULL_PTID means that we don't have any active threads,
47705796c8dcSSimon Schubert 	 so we don't have any TID numbers the inferior will
47715796c8dcSSimon Schubert 	 understand.  Make sure to only send forms that do not specify
47725796c8dcSSimon Schubert 	 a TID.  */
4773c50c785cSJohn Marino       append_resumption (p, endp, minus_one_ptid, step, siggnal);
47745796c8dcSSimon Schubert     }
47755796c8dcSSimon Schubert   else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
47765796c8dcSSimon Schubert     {
47775796c8dcSSimon Schubert       /* Resume all threads (of all processes, or of a single
47785796c8dcSSimon Schubert 	 process), with preference for INFERIOR_PTID.  This assumes
47795796c8dcSSimon Schubert 	 inferior_ptid belongs to the set of all threads we are about
47805796c8dcSSimon Schubert 	 to resume.  */
4781ef5ccd6cSJohn Marino       if (step || siggnal != GDB_SIGNAL_0)
47825796c8dcSSimon Schubert 	{
47835796c8dcSSimon Schubert 	  /* Step inferior_ptid, with or without signal.  */
47845796c8dcSSimon Schubert 	  p = append_resumption (p, endp, inferior_ptid, step, siggnal);
47855796c8dcSSimon Schubert 	}
47865796c8dcSSimon Schubert 
4787ef5ccd6cSJohn Marino       /* Also pass down any pending signaled resumption for other
4788ef5ccd6cSJohn Marino 	 threads not the current.  */
4789ef5ccd6cSJohn Marino       p = append_pending_thread_resumptions (p, endp, ptid);
4790ef5ccd6cSJohn Marino 
47915796c8dcSSimon Schubert       /* And continue others without a signal.  */
4792ef5ccd6cSJohn Marino       append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
47935796c8dcSSimon Schubert     }
47945796c8dcSSimon Schubert   else
47955796c8dcSSimon Schubert     {
47965796c8dcSSimon Schubert       /* Scheduler locking; resume only PTID.  */
4797c50c785cSJohn Marino       append_resumption (p, endp, ptid, step, siggnal);
47985796c8dcSSimon Schubert     }
47995796c8dcSSimon Schubert 
48005796c8dcSSimon Schubert   gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
48015796c8dcSSimon Schubert   putpkt (rs->buf);
48025796c8dcSSimon Schubert 
48035796c8dcSSimon Schubert   if (non_stop)
48045796c8dcSSimon Schubert     {
48055796c8dcSSimon Schubert       /* In non-stop, the stub replies to vCont with "OK".  The stop
48065796c8dcSSimon Schubert 	 reply will be reported asynchronously by means of a `%Stop'
48075796c8dcSSimon Schubert 	 notification.  */
48085796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
48095796c8dcSSimon Schubert       if (strcmp (rs->buf, "OK") != 0)
48105796c8dcSSimon Schubert 	error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
48115796c8dcSSimon Schubert     }
48125796c8dcSSimon Schubert 
48135796c8dcSSimon Schubert   return 1;
48145796c8dcSSimon Schubert }
48155796c8dcSSimon Schubert 
48165796c8dcSSimon Schubert /* Tell the remote machine to resume.  */
48175796c8dcSSimon Schubert 
4818ef5ccd6cSJohn Marino static enum gdb_signal last_sent_signal = GDB_SIGNAL_0;
48195796c8dcSSimon Schubert 
48205796c8dcSSimon Schubert static int last_sent_step;
48215796c8dcSSimon Schubert 
48225796c8dcSSimon Schubert static void
remote_resume(struct target_ops * ops,ptid_t ptid,int step,enum gdb_signal siggnal)48235796c8dcSSimon Schubert remote_resume (struct target_ops *ops,
4824ef5ccd6cSJohn Marino 	       ptid_t ptid, int step, enum gdb_signal siggnal)
48255796c8dcSSimon Schubert {
48265796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
48275796c8dcSSimon Schubert   char *buf;
48285796c8dcSSimon Schubert 
4829ef5ccd6cSJohn Marino   /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
4830ef5ccd6cSJohn Marino      (explained in remote-notif.c:handle_notification) so
4831ef5ccd6cSJohn Marino      remote_notif_process is not called.  We need find a place where
4832ef5ccd6cSJohn Marino      it is safe to start a 'vNotif' sequence.  It is good to do it
4833ef5ccd6cSJohn Marino      before resuming inferior, because inferior was stopped and no RSP
4834ef5ccd6cSJohn Marino      traffic at that moment.  */
4835ef5ccd6cSJohn Marino   if (!non_stop)
4836ef5ccd6cSJohn Marino     remote_notif_process (&notif_client_stop);
4837ef5ccd6cSJohn Marino 
48385796c8dcSSimon Schubert   last_sent_signal = siggnal;
48395796c8dcSSimon Schubert   last_sent_step = step;
48405796c8dcSSimon Schubert 
48415796c8dcSSimon Schubert   /* The vCont packet doesn't need to specify threads via Hc.  */
48425796c8dcSSimon Schubert   /* No reverse support (yet) for vCont.  */
48435796c8dcSSimon Schubert   if (execution_direction != EXEC_REVERSE)
48445796c8dcSSimon Schubert     if (remote_vcont_resume (ptid, step, siggnal))
48455796c8dcSSimon Schubert       goto done;
48465796c8dcSSimon Schubert 
48475796c8dcSSimon Schubert   /* All other supported resume packets do use Hc, so set the continue
48485796c8dcSSimon Schubert      thread.  */
48495796c8dcSSimon Schubert   if (ptid_equal (ptid, minus_one_ptid))
48505796c8dcSSimon Schubert     set_continue_thread (any_thread_ptid);
48515796c8dcSSimon Schubert   else
48525796c8dcSSimon Schubert     set_continue_thread (ptid);
48535796c8dcSSimon Schubert 
48545796c8dcSSimon Schubert   buf = rs->buf;
48555796c8dcSSimon Schubert   if (execution_direction == EXEC_REVERSE)
48565796c8dcSSimon Schubert     {
48575796c8dcSSimon Schubert       /* We don't pass signals to the target in reverse exec mode.  */
4858ef5ccd6cSJohn Marino       if (info_verbose && siggnal != GDB_SIGNAL_0)
4859c50c785cSJohn Marino 	warning (_(" - Can't pass signal %d to target in reverse: ignored."),
48605796c8dcSSimon Schubert 		 siggnal);
48615796c8dcSSimon Schubert 
48625796c8dcSSimon Schubert       if (step
48635796c8dcSSimon Schubert 	  && remote_protocol_packets[PACKET_bs].support == PACKET_DISABLE)
48645796c8dcSSimon Schubert 	error (_("Remote reverse-step not supported."));
48655796c8dcSSimon Schubert       if (!step
48665796c8dcSSimon Schubert 	  && remote_protocol_packets[PACKET_bc].support == PACKET_DISABLE)
48675796c8dcSSimon Schubert 	error (_("Remote reverse-continue not supported."));
48685796c8dcSSimon Schubert 
48695796c8dcSSimon Schubert       strcpy (buf, step ? "bs" : "bc");
48705796c8dcSSimon Schubert     }
4871ef5ccd6cSJohn Marino   else if (siggnal != GDB_SIGNAL_0)
48725796c8dcSSimon Schubert     {
48735796c8dcSSimon Schubert       buf[0] = step ? 'S' : 'C';
48745796c8dcSSimon Schubert       buf[1] = tohex (((int) siggnal >> 4) & 0xf);
48755796c8dcSSimon Schubert       buf[2] = tohex (((int) siggnal) & 0xf);
48765796c8dcSSimon Schubert       buf[3] = '\0';
48775796c8dcSSimon Schubert     }
48785796c8dcSSimon Schubert   else
48795796c8dcSSimon Schubert     strcpy (buf, step ? "s" : "c");
48805796c8dcSSimon Schubert 
48815796c8dcSSimon Schubert   putpkt (buf);
48825796c8dcSSimon Schubert 
48835796c8dcSSimon Schubert  done:
48845796c8dcSSimon Schubert   /* We are about to start executing the inferior, let's register it
48855796c8dcSSimon Schubert      with the event loop.  NOTE: this is the one place where all the
48865796c8dcSSimon Schubert      execution commands end up.  We could alternatively do this in each
48875796c8dcSSimon Schubert      of the execution commands in infcmd.c.  */
48885796c8dcSSimon Schubert   /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
48895796c8dcSSimon Schubert      into infcmd.c in order to allow inferior function calls to work
48905796c8dcSSimon Schubert      NOT asynchronously.  */
48915796c8dcSSimon Schubert   if (target_can_async_p ())
48925796c8dcSSimon Schubert     target_async (inferior_event_handler, 0);
48935796c8dcSSimon Schubert 
48945796c8dcSSimon Schubert   /* We've just told the target to resume.  The remote server will
48955796c8dcSSimon Schubert      wait for the inferior to stop, and then send a stop reply.  In
48965796c8dcSSimon Schubert      the mean time, we can't start another command/query ourselves
48975796c8dcSSimon Schubert      because the stub wouldn't be ready to process it.  This applies
48985796c8dcSSimon Schubert      only to the base all-stop protocol, however.  In non-stop (which
48995796c8dcSSimon Schubert      only supports vCont), the stub replies with an "OK", and is
49005796c8dcSSimon Schubert      immediate able to process further serial input.  */
49015796c8dcSSimon Schubert   if (!non_stop)
49025796c8dcSSimon Schubert     rs->waiting_for_stop_reply = 1;
49035796c8dcSSimon Schubert }
49045796c8dcSSimon Schubert 
49055796c8dcSSimon Schubert 
49065796c8dcSSimon Schubert /* Set up the signal handler for SIGINT, while the target is
49075796c8dcSSimon Schubert    executing, ovewriting the 'regular' SIGINT signal handler.  */
49085796c8dcSSimon Schubert static void
initialize_sigint_signal_handler(void)49095796c8dcSSimon Schubert initialize_sigint_signal_handler (void)
49105796c8dcSSimon Schubert {
49115796c8dcSSimon Schubert   signal (SIGINT, handle_remote_sigint);
49125796c8dcSSimon Schubert }
49135796c8dcSSimon Schubert 
49145796c8dcSSimon Schubert /* Signal handler for SIGINT, while the target is executing.  */
49155796c8dcSSimon Schubert static void
handle_remote_sigint(int sig)49165796c8dcSSimon Schubert handle_remote_sigint (int sig)
49175796c8dcSSimon Schubert {
49185796c8dcSSimon Schubert   signal (sig, handle_remote_sigint_twice);
4919ef5ccd6cSJohn Marino   mark_async_signal_handler (sigint_remote_token);
49205796c8dcSSimon Schubert }
49215796c8dcSSimon Schubert 
49225796c8dcSSimon Schubert /* Signal handler for SIGINT, installed after SIGINT has already been
49235796c8dcSSimon Schubert    sent once.  It will take effect the second time that the user sends
49245796c8dcSSimon Schubert    a ^C.  */
49255796c8dcSSimon Schubert static void
handle_remote_sigint_twice(int sig)49265796c8dcSSimon Schubert handle_remote_sigint_twice (int sig)
49275796c8dcSSimon Schubert {
49285796c8dcSSimon Schubert   signal (sig, handle_remote_sigint);
4929ef5ccd6cSJohn Marino   mark_async_signal_handler (sigint_remote_twice_token);
49305796c8dcSSimon Schubert }
49315796c8dcSSimon Schubert 
49325796c8dcSSimon Schubert /* Perform the real interruption of the target execution, in response
49335796c8dcSSimon Schubert    to a ^C.  */
49345796c8dcSSimon Schubert static void
async_remote_interrupt(gdb_client_data arg)49355796c8dcSSimon Schubert async_remote_interrupt (gdb_client_data arg)
49365796c8dcSSimon Schubert {
49375796c8dcSSimon Schubert   if (remote_debug)
4938ef5ccd6cSJohn Marino     fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n");
49395796c8dcSSimon Schubert 
49405796c8dcSSimon Schubert   target_stop (inferior_ptid);
49415796c8dcSSimon Schubert }
49425796c8dcSSimon Schubert 
49435796c8dcSSimon Schubert /* Perform interrupt, if the first attempt did not succeed.  Just give
49445796c8dcSSimon Schubert    up on the target alltogether.  */
49455796c8dcSSimon Schubert void
async_remote_interrupt_twice(gdb_client_data arg)49465796c8dcSSimon Schubert async_remote_interrupt_twice (gdb_client_data arg)
49475796c8dcSSimon Schubert {
49485796c8dcSSimon Schubert   if (remote_debug)
4949ef5ccd6cSJohn Marino     fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n");
49505796c8dcSSimon Schubert 
49515796c8dcSSimon Schubert   interrupt_query ();
49525796c8dcSSimon Schubert }
49535796c8dcSSimon Schubert 
49545796c8dcSSimon Schubert /* Reinstall the usual SIGINT handlers, after the target has
49555796c8dcSSimon Schubert    stopped.  */
49565796c8dcSSimon Schubert static void
cleanup_sigint_signal_handler(void * dummy)49575796c8dcSSimon Schubert cleanup_sigint_signal_handler (void *dummy)
49585796c8dcSSimon Schubert {
49595796c8dcSSimon Schubert   signal (SIGINT, handle_sigint);
49605796c8dcSSimon Schubert }
49615796c8dcSSimon Schubert 
49625796c8dcSSimon Schubert /* Send ^C to target to halt it.  Target will respond, and send us a
49635796c8dcSSimon Schubert    packet.  */
49645796c8dcSSimon Schubert static void (*ofunc) (int);
49655796c8dcSSimon Schubert 
49665796c8dcSSimon Schubert /* The command line interface's stop routine.  This function is installed
49675796c8dcSSimon Schubert    as a signal handler for SIGINT.  The first time a user requests a
49685796c8dcSSimon Schubert    stop, we call remote_stop to send a break or ^C.  If there is no
49695796c8dcSSimon Schubert    response from the target (it didn't stop when the user requested it),
49705796c8dcSSimon Schubert    we ask the user if he'd like to detach from the target.  */
49715796c8dcSSimon Schubert static void
remote_interrupt(int signo)49725796c8dcSSimon Schubert remote_interrupt (int signo)
49735796c8dcSSimon Schubert {
49745796c8dcSSimon Schubert   /* If this doesn't work, try more severe steps.  */
49755796c8dcSSimon Schubert   signal (signo, remote_interrupt_twice);
49765796c8dcSSimon Schubert 
49775796c8dcSSimon Schubert   gdb_call_async_signal_handler (sigint_remote_token, 1);
49785796c8dcSSimon Schubert }
49795796c8dcSSimon Schubert 
49805796c8dcSSimon Schubert /* The user typed ^C twice.  */
49815796c8dcSSimon Schubert 
49825796c8dcSSimon Schubert static void
remote_interrupt_twice(int signo)49835796c8dcSSimon Schubert remote_interrupt_twice (int signo)
49845796c8dcSSimon Schubert {
49855796c8dcSSimon Schubert   signal (signo, ofunc);
49865796c8dcSSimon Schubert   gdb_call_async_signal_handler (sigint_remote_twice_token, 1);
49875796c8dcSSimon Schubert   signal (signo, remote_interrupt);
49885796c8dcSSimon Schubert }
49895796c8dcSSimon Schubert 
49905796c8dcSSimon Schubert /* Non-stop version of target_stop.  Uses `vCont;t' to stop a remote
49915796c8dcSSimon Schubert    thread, all threads of a remote process, or all threads of all
49925796c8dcSSimon Schubert    processes.  */
49935796c8dcSSimon Schubert 
49945796c8dcSSimon Schubert static void
remote_stop_ns(ptid_t ptid)49955796c8dcSSimon Schubert remote_stop_ns (ptid_t ptid)
49965796c8dcSSimon Schubert {
49975796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
49985796c8dcSSimon Schubert   char *p = rs->buf;
49995796c8dcSSimon Schubert   char *endp = rs->buf + get_remote_packet_size ();
50005796c8dcSSimon Schubert 
50015796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
50025796c8dcSSimon Schubert     remote_vcont_probe (rs);
50035796c8dcSSimon Schubert 
50045796c8dcSSimon Schubert   if (!rs->support_vCont_t)
50055796c8dcSSimon Schubert     error (_("Remote server does not support stopping threads"));
50065796c8dcSSimon Schubert 
50075796c8dcSSimon Schubert   if (ptid_equal (ptid, minus_one_ptid)
50085796c8dcSSimon Schubert       || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
50095796c8dcSSimon Schubert     p += xsnprintf (p, endp - p, "vCont;t");
50105796c8dcSSimon Schubert   else
50115796c8dcSSimon Schubert     {
50125796c8dcSSimon Schubert       ptid_t nptid;
50135796c8dcSSimon Schubert 
50145796c8dcSSimon Schubert       p += xsnprintf (p, endp - p, "vCont;t:");
50155796c8dcSSimon Schubert 
50165796c8dcSSimon Schubert       if (ptid_is_pid (ptid))
50175796c8dcSSimon Schubert 	  /* All (-1) threads of process.  */
50185796c8dcSSimon Schubert 	nptid = ptid_build (ptid_get_pid (ptid), 0, -1);
50195796c8dcSSimon Schubert       else
50205796c8dcSSimon Schubert 	{
50215796c8dcSSimon Schubert 	  /* Small optimization: if we already have a stop reply for
50225796c8dcSSimon Schubert 	     this thread, no use in telling the stub we want this
50235796c8dcSSimon Schubert 	     stopped.  */
50245796c8dcSSimon Schubert 	  if (peek_stop_reply (ptid))
50255796c8dcSSimon Schubert 	    return;
50265796c8dcSSimon Schubert 
50275796c8dcSSimon Schubert 	  nptid = ptid;
50285796c8dcSSimon Schubert 	}
50295796c8dcSSimon Schubert 
5030c50c785cSJohn Marino       write_ptid (p, endp, nptid);
50315796c8dcSSimon Schubert     }
50325796c8dcSSimon Schubert 
50335796c8dcSSimon Schubert   /* In non-stop, we get an immediate OK reply.  The stop reply will
50345796c8dcSSimon Schubert      come in asynchronously by notification.  */
50355796c8dcSSimon Schubert   putpkt (rs->buf);
50365796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
50375796c8dcSSimon Schubert   if (strcmp (rs->buf, "OK") != 0)
50385796c8dcSSimon Schubert     error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
50395796c8dcSSimon Schubert }
50405796c8dcSSimon Schubert 
50415796c8dcSSimon Schubert /* All-stop version of target_stop.  Sends a break or a ^C to stop the
50425796c8dcSSimon Schubert    remote target.  It is undefined which thread of which process
50435796c8dcSSimon Schubert    reports the stop.  */
50445796c8dcSSimon Schubert 
50455796c8dcSSimon Schubert static void
remote_stop_as(ptid_t ptid)50465796c8dcSSimon Schubert remote_stop_as (ptid_t ptid)
50475796c8dcSSimon Schubert {
50485796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
50495796c8dcSSimon Schubert 
5050cf7f2e2dSJohn Marino   rs->ctrlc_pending_p = 1;
5051cf7f2e2dSJohn Marino 
50525796c8dcSSimon Schubert   /* If the inferior is stopped already, but the core didn't know
50535796c8dcSSimon Schubert      about it yet, just ignore the request.  The cached wait status
50545796c8dcSSimon Schubert      will be collected in remote_wait.  */
50555796c8dcSSimon Schubert   if (rs->cached_wait_status)
50565796c8dcSSimon Schubert     return;
50575796c8dcSSimon Schubert 
5058cf7f2e2dSJohn Marino   /* Send interrupt_sequence to remote target.  */
5059cf7f2e2dSJohn Marino   send_interrupt_sequence ();
50605796c8dcSSimon Schubert }
50615796c8dcSSimon Schubert 
50625796c8dcSSimon Schubert /* This is the generic stop called via the target vector.  When a target
50635796c8dcSSimon Schubert    interrupt is requested, either by the command line or the GUI, we
50645796c8dcSSimon Schubert    will eventually end up here.  */
50655796c8dcSSimon Schubert 
50665796c8dcSSimon Schubert static void
remote_stop(ptid_t ptid)50675796c8dcSSimon Schubert remote_stop (ptid_t ptid)
50685796c8dcSSimon Schubert {
50695796c8dcSSimon Schubert   if (remote_debug)
50705796c8dcSSimon Schubert     fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
50715796c8dcSSimon Schubert 
50725796c8dcSSimon Schubert   if (non_stop)
50735796c8dcSSimon Schubert     remote_stop_ns (ptid);
50745796c8dcSSimon Schubert   else
50755796c8dcSSimon Schubert     remote_stop_as (ptid);
50765796c8dcSSimon Schubert }
50775796c8dcSSimon Schubert 
50785796c8dcSSimon Schubert /* Ask the user what to do when an interrupt is received.  */
50795796c8dcSSimon Schubert 
50805796c8dcSSimon Schubert static void
interrupt_query(void)50815796c8dcSSimon Schubert interrupt_query (void)
50825796c8dcSSimon Schubert {
50835796c8dcSSimon Schubert   target_terminal_ours ();
50845796c8dcSSimon Schubert 
50855796c8dcSSimon Schubert   if (target_can_async_p ())
50865796c8dcSSimon Schubert     {
50875796c8dcSSimon Schubert       signal (SIGINT, handle_sigint);
50885796c8dcSSimon Schubert       deprecated_throw_reason (RETURN_QUIT);
50895796c8dcSSimon Schubert     }
50905796c8dcSSimon Schubert   else
50915796c8dcSSimon Schubert     {
50925796c8dcSSimon Schubert       if (query (_("Interrupted while waiting for the program.\n\
50935796c8dcSSimon Schubert Give up (and stop debugging it)? ")))
50945796c8dcSSimon Schubert 	{
5095ef5ccd6cSJohn Marino 	  remote_unpush_target ();
50965796c8dcSSimon Schubert 	  deprecated_throw_reason (RETURN_QUIT);
50975796c8dcSSimon Schubert 	}
50985796c8dcSSimon Schubert     }
50995796c8dcSSimon Schubert 
51005796c8dcSSimon Schubert   target_terminal_inferior ();
51015796c8dcSSimon Schubert }
51025796c8dcSSimon Schubert 
51035796c8dcSSimon Schubert /* Enable/disable target terminal ownership.  Most targets can use
51045796c8dcSSimon Schubert    terminal groups to control terminal ownership.  Remote targets are
51055796c8dcSSimon Schubert    different in that explicit transfer of ownership to/from GDB/target
51065796c8dcSSimon Schubert    is required.  */
51075796c8dcSSimon Schubert 
51085796c8dcSSimon Schubert static void
remote_terminal_inferior(void)51095796c8dcSSimon Schubert remote_terminal_inferior (void)
51105796c8dcSSimon Schubert {
51115796c8dcSSimon Schubert   if (!target_async_permitted)
51125796c8dcSSimon Schubert     /* Nothing to do.  */
51135796c8dcSSimon Schubert     return;
51145796c8dcSSimon Schubert 
51155796c8dcSSimon Schubert   /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
51165796c8dcSSimon Schubert      idempotent.  The event-loop GDB talking to an asynchronous target
51175796c8dcSSimon Schubert      with a synchronous command calls this function from both
51185796c8dcSSimon Schubert      event-top.c and infrun.c/infcmd.c.  Once GDB stops trying to
51195796c8dcSSimon Schubert      transfer the terminal to the target when it shouldn't this guard
51205796c8dcSSimon Schubert      can go away.  */
51215796c8dcSSimon Schubert   if (!remote_async_terminal_ours_p)
51225796c8dcSSimon Schubert     return;
51235796c8dcSSimon Schubert   delete_file_handler (input_fd);
51245796c8dcSSimon Schubert   remote_async_terminal_ours_p = 0;
51255796c8dcSSimon Schubert   initialize_sigint_signal_handler ();
51265796c8dcSSimon Schubert   /* NOTE: At this point we could also register our selves as the
51275796c8dcSSimon Schubert      recipient of all input.  Any characters typed could then be
51285796c8dcSSimon Schubert      passed on down to the target.  */
51295796c8dcSSimon Schubert }
51305796c8dcSSimon Schubert 
51315796c8dcSSimon Schubert static void
remote_terminal_ours(void)51325796c8dcSSimon Schubert remote_terminal_ours (void)
51335796c8dcSSimon Schubert {
51345796c8dcSSimon Schubert   if (!target_async_permitted)
51355796c8dcSSimon Schubert     /* Nothing to do.  */
51365796c8dcSSimon Schubert     return;
51375796c8dcSSimon Schubert 
51385796c8dcSSimon Schubert   /* See FIXME in remote_terminal_inferior.  */
51395796c8dcSSimon Schubert   if (remote_async_terminal_ours_p)
51405796c8dcSSimon Schubert     return;
51415796c8dcSSimon Schubert   cleanup_sigint_signal_handler (NULL);
51425796c8dcSSimon Schubert   add_file_handler (input_fd, stdin_event_handler, 0);
51435796c8dcSSimon Schubert   remote_async_terminal_ours_p = 1;
51445796c8dcSSimon Schubert }
51455796c8dcSSimon Schubert 
5146cf7f2e2dSJohn Marino static void
remote_console_output(char * msg)51475796c8dcSSimon Schubert remote_console_output (char *msg)
51485796c8dcSSimon Schubert {
51495796c8dcSSimon Schubert   char *p;
51505796c8dcSSimon Schubert 
51515796c8dcSSimon Schubert   for (p = msg; p[0] && p[1]; p += 2)
51525796c8dcSSimon Schubert     {
51535796c8dcSSimon Schubert       char tb[2];
51545796c8dcSSimon Schubert       char c = fromhex (p[0]) * 16 + fromhex (p[1]);
5155cf7f2e2dSJohn Marino 
51565796c8dcSSimon Schubert       tb[0] = c;
51575796c8dcSSimon Schubert       tb[1] = 0;
51585796c8dcSSimon Schubert       fputs_unfiltered (tb, gdb_stdtarg);
51595796c8dcSSimon Schubert     }
51605796c8dcSSimon Schubert   gdb_flush (gdb_stdtarg);
51615796c8dcSSimon Schubert }
51625796c8dcSSimon Schubert 
51635796c8dcSSimon Schubert typedef struct cached_reg
51645796c8dcSSimon Schubert {
51655796c8dcSSimon Schubert   int num;
51665796c8dcSSimon Schubert   gdb_byte data[MAX_REGISTER_SIZE];
51675796c8dcSSimon Schubert } cached_reg_t;
51685796c8dcSSimon Schubert 
51695796c8dcSSimon Schubert DEF_VEC_O(cached_reg_t);
51705796c8dcSSimon Schubert 
5171ef5ccd6cSJohn Marino typedef struct stop_reply
51725796c8dcSSimon Schubert {
5173ef5ccd6cSJohn Marino   struct notif_event base;
51745796c8dcSSimon Schubert 
5175ef5ccd6cSJohn Marino   /* The identifier of the thread about this event  */
51765796c8dcSSimon Schubert   ptid_t ptid;
51775796c8dcSSimon Schubert 
51785796c8dcSSimon Schubert   struct target_waitstatus ws;
51795796c8dcSSimon Schubert 
5180a45ae5f8SJohn Marino   /* Expedited registers.  This makes remote debugging a bit more
5181a45ae5f8SJohn Marino      efficient for those targets that provide critical registers as
5182a45ae5f8SJohn Marino      part of their normal status mechanism (as another roundtrip to
5183a45ae5f8SJohn Marino      fetch them is avoided).  */
51845796c8dcSSimon Schubert   VEC(cached_reg_t) *regcache;
51855796c8dcSSimon Schubert 
51865796c8dcSSimon Schubert   int stopped_by_watchpoint_p;
51875796c8dcSSimon Schubert   CORE_ADDR watch_data_address;
51885796c8dcSSimon Schubert 
51895796c8dcSSimon Schubert   int solibs_changed;
51905796c8dcSSimon Schubert   int replay_event;
5191cf7f2e2dSJohn Marino 
5192cf7f2e2dSJohn Marino   int core;
5193ef5ccd6cSJohn Marino } *stop_reply_p;
51945796c8dcSSimon Schubert 
5195ef5ccd6cSJohn Marino DECLARE_QUEUE_P (stop_reply_p);
5196ef5ccd6cSJohn Marino DEFINE_QUEUE_P (stop_reply_p);
5197ef5ccd6cSJohn Marino /* The list of already fetched and acknowledged stop events.  This
5198ef5ccd6cSJohn Marino    queue is used for notification Stop, and other notifications
5199ef5ccd6cSJohn Marino    don't need queue for their events, because the notification events
5200ef5ccd6cSJohn Marino    of Stop can't be consumed immediately, so that events should be
5201ef5ccd6cSJohn Marino    queued first, and be consumed by remote_wait_{ns,as} one per
5202ef5ccd6cSJohn Marino    time.  Other notifications can consume their events immediately,
5203ef5ccd6cSJohn Marino    so queue is not needed for them.  */
QUEUE(stop_reply_p)5204ef5ccd6cSJohn Marino static QUEUE (stop_reply_p) *stop_reply_queue;
52055796c8dcSSimon Schubert 
52065796c8dcSSimon Schubert static void
52075796c8dcSSimon Schubert stop_reply_xfree (struct stop_reply *r)
52085796c8dcSSimon Schubert {
52095796c8dcSSimon Schubert   if (r != NULL)
52105796c8dcSSimon Schubert     {
52115796c8dcSSimon Schubert       VEC_free (cached_reg_t, r->regcache);
52125796c8dcSSimon Schubert       xfree (r);
52135796c8dcSSimon Schubert     }
52145796c8dcSSimon Schubert }
52155796c8dcSSimon Schubert 
5216ef5ccd6cSJohn Marino static void
remote_notif_stop_parse(struct notif_client * self,char * buf,struct notif_event * event)5217ef5ccd6cSJohn Marino remote_notif_stop_parse (struct notif_client *self, char *buf,
5218ef5ccd6cSJohn Marino 			 struct notif_event *event)
5219ef5ccd6cSJohn Marino {
5220ef5ccd6cSJohn Marino   remote_parse_stop_reply (buf, (struct stop_reply *) event);
5221ef5ccd6cSJohn Marino }
5222ef5ccd6cSJohn Marino 
5223ef5ccd6cSJohn Marino static void
remote_notif_stop_ack(struct notif_client * self,char * buf,struct notif_event * event)5224ef5ccd6cSJohn Marino remote_notif_stop_ack (struct notif_client *self, char *buf,
5225ef5ccd6cSJohn Marino 		       struct notif_event *event)
5226ef5ccd6cSJohn Marino {
5227ef5ccd6cSJohn Marino   struct stop_reply *stop_reply = (struct stop_reply *) event;
5228ef5ccd6cSJohn Marino 
5229ef5ccd6cSJohn Marino   /* acknowledge */
5230ef5ccd6cSJohn Marino   putpkt ((char *) self->ack_command);
5231ef5ccd6cSJohn Marino 
5232ef5ccd6cSJohn Marino   if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE)
5233ef5ccd6cSJohn Marino       /* We got an unknown stop reply.  */
5234ef5ccd6cSJohn Marino       error (_("Unknown stop reply"));
5235ef5ccd6cSJohn Marino 
5236ef5ccd6cSJohn Marino   push_stop_reply (stop_reply);
5237ef5ccd6cSJohn Marino }
5238ef5ccd6cSJohn Marino 
5239ef5ccd6cSJohn Marino static int
remote_notif_stop_can_get_pending_events(struct notif_client * self)5240ef5ccd6cSJohn Marino remote_notif_stop_can_get_pending_events (struct notif_client *self)
5241ef5ccd6cSJohn Marino {
5242ef5ccd6cSJohn Marino   /* We can't get pending events in remote_notif_process for
5243ef5ccd6cSJohn Marino      notification stop, and we have to do this in remote_wait_ns
5244ef5ccd6cSJohn Marino      instead.  If we fetch all queued events from stub, remote stub
5245ef5ccd6cSJohn Marino      may exit and we have no chance to process them back in
5246ef5ccd6cSJohn Marino      remote_wait_ns.  */
5247ef5ccd6cSJohn Marino   mark_async_event_handler (remote_async_inferior_event_token);
5248ef5ccd6cSJohn Marino   return 0;
5249ef5ccd6cSJohn Marino }
5250ef5ccd6cSJohn Marino 
5251ef5ccd6cSJohn Marino static void
stop_reply_dtr(struct notif_event * event)5252ef5ccd6cSJohn Marino stop_reply_dtr (struct notif_event *event)
5253ef5ccd6cSJohn Marino {
5254ef5ccd6cSJohn Marino   struct stop_reply *r = (struct stop_reply *) event;
5255ef5ccd6cSJohn Marino 
5256ef5ccd6cSJohn Marino   VEC_free (cached_reg_t, r->regcache);
5257ef5ccd6cSJohn Marino }
5258ef5ccd6cSJohn Marino 
5259ef5ccd6cSJohn Marino static struct notif_event *
remote_notif_stop_alloc_reply(void)5260ef5ccd6cSJohn Marino remote_notif_stop_alloc_reply (void)
5261ef5ccd6cSJohn Marino {
5262ef5ccd6cSJohn Marino   struct notif_event *r
5263ef5ccd6cSJohn Marino     = (struct notif_event *) XMALLOC (struct stop_reply);
5264ef5ccd6cSJohn Marino 
5265ef5ccd6cSJohn Marino   r->dtr = stop_reply_dtr;
5266ef5ccd6cSJohn Marino 
5267ef5ccd6cSJohn Marino   return r;
5268ef5ccd6cSJohn Marino }
5269ef5ccd6cSJohn Marino 
5270ef5ccd6cSJohn Marino /* A client of notification Stop.  */
5271ef5ccd6cSJohn Marino 
5272ef5ccd6cSJohn Marino struct notif_client notif_client_stop =
5273ef5ccd6cSJohn Marino {
5274ef5ccd6cSJohn Marino   "Stop",
5275ef5ccd6cSJohn Marino   "vStopped",
5276ef5ccd6cSJohn Marino   remote_notif_stop_parse,
5277ef5ccd6cSJohn Marino   remote_notif_stop_ack,
5278ef5ccd6cSJohn Marino   remote_notif_stop_can_get_pending_events,
5279ef5ccd6cSJohn Marino   remote_notif_stop_alloc_reply,
5280ef5ccd6cSJohn Marino   NULL,
5281ef5ccd6cSJohn Marino };
5282ef5ccd6cSJohn Marino 
5283ef5ccd6cSJohn Marino /* A parameter to pass data in and out.  */
5284ef5ccd6cSJohn Marino 
5285ef5ccd6cSJohn Marino struct queue_iter_param
5286ef5ccd6cSJohn Marino {
5287ef5ccd6cSJohn Marino   void *input;
5288ef5ccd6cSJohn Marino   struct stop_reply *output;
5289ef5ccd6cSJohn Marino };
5290ef5ccd6cSJohn Marino 
5291ef5ccd6cSJohn Marino /* Remove all queue elements meet the condition it checks.  */
5292ef5ccd6cSJohn Marino 
5293ef5ccd6cSJohn Marino static int
remote_notif_remove_all(QUEUE (stop_reply_p)* q,QUEUE_ITER (stop_reply_p)* iter,stop_reply_p event,void * data)5294ef5ccd6cSJohn Marino remote_notif_remove_all (QUEUE (stop_reply_p) *q,
5295ef5ccd6cSJohn Marino 			 QUEUE_ITER (stop_reply_p) *iter,
5296ef5ccd6cSJohn Marino 			 stop_reply_p event,
5297ef5ccd6cSJohn Marino 			 void *data)
5298ef5ccd6cSJohn Marino {
5299ef5ccd6cSJohn Marino   struct queue_iter_param *param = data;
5300ef5ccd6cSJohn Marino   struct inferior *inf = param->input;
5301ef5ccd6cSJohn Marino 
5302ef5ccd6cSJohn Marino   if (inf == NULL || ptid_get_pid (event->ptid) == inf->pid)
5303ef5ccd6cSJohn Marino     {
5304ef5ccd6cSJohn Marino       stop_reply_xfree (event);
5305ef5ccd6cSJohn Marino       QUEUE_remove_elem (stop_reply_p, q, iter);
5306ef5ccd6cSJohn Marino     }
5307ef5ccd6cSJohn Marino 
5308ef5ccd6cSJohn Marino   return 1;
5309ef5ccd6cSJohn Marino }
5310ef5ccd6cSJohn Marino 
5311ef5ccd6cSJohn Marino /* Discard all pending stop replies of inferior INF.  If INF is NULL,
53125796c8dcSSimon Schubert    discard everything.  */
53135796c8dcSSimon Schubert 
53145796c8dcSSimon Schubert static void
discard_pending_stop_replies(struct inferior * inf)5315ef5ccd6cSJohn Marino discard_pending_stop_replies (struct inferior *inf)
53165796c8dcSSimon Schubert {
5317ef5ccd6cSJohn Marino   int i;
5318ef5ccd6cSJohn Marino   struct queue_iter_param param;
5319ef5ccd6cSJohn Marino   struct stop_reply *reply
5320ef5ccd6cSJohn Marino     = (struct stop_reply *) notif_client_stop.pending_event;
53215796c8dcSSimon Schubert 
53225796c8dcSSimon Schubert   /* Discard the in-flight notification.  */
5323ef5ccd6cSJohn Marino   if (reply != NULL
5324ef5ccd6cSJohn Marino       && (inf == NULL
5325ef5ccd6cSJohn Marino 	  || ptid_get_pid (reply->ptid) == inf->pid))
53265796c8dcSSimon Schubert     {
5327ef5ccd6cSJohn Marino       stop_reply_xfree (reply);
5328ef5ccd6cSJohn Marino       notif_client_stop.pending_event = NULL;
53295796c8dcSSimon Schubert     }
53305796c8dcSSimon Schubert 
5331ef5ccd6cSJohn Marino   param.input = inf;
5332ef5ccd6cSJohn Marino   param.output = NULL;
53335796c8dcSSimon Schubert   /* Discard the stop replies we have already pulled with
53345796c8dcSSimon Schubert      vStopped.  */
5335ef5ccd6cSJohn Marino   QUEUE_iterate (stop_reply_p, stop_reply_queue,
5336ef5ccd6cSJohn Marino 		 remote_notif_remove_all, &param);
53375796c8dcSSimon Schubert }
53385796c8dcSSimon Schubert 
5339ef5ccd6cSJohn Marino /* A parameter to pass data in and out.  */
53405796c8dcSSimon Schubert 
5341ef5ccd6cSJohn Marino static int
remote_notif_remove_once_on_match(QUEUE (stop_reply_p)* q,QUEUE_ITER (stop_reply_p)* iter,stop_reply_p event,void * data)5342ef5ccd6cSJohn Marino remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q,
5343ef5ccd6cSJohn Marino 				   QUEUE_ITER (stop_reply_p) *iter,
5344ef5ccd6cSJohn Marino 				   stop_reply_p event,
5345ef5ccd6cSJohn Marino 				   void *data)
53465796c8dcSSimon Schubert {
5347ef5ccd6cSJohn Marino   struct queue_iter_param *param = data;
5348ef5ccd6cSJohn Marino   ptid_t *ptid = param->input;
5349cf7f2e2dSJohn Marino 
5350ef5ccd6cSJohn Marino   if (ptid_match (event->ptid, *ptid))
5351ef5ccd6cSJohn Marino     {
5352ef5ccd6cSJohn Marino       param->output = event;
5353ef5ccd6cSJohn Marino       QUEUE_remove_elem (stop_reply_p, q, iter);
5354ef5ccd6cSJohn Marino       return 0;
5355ef5ccd6cSJohn Marino     }
5356ef5ccd6cSJohn Marino 
5357ef5ccd6cSJohn Marino   return 1;
5358ef5ccd6cSJohn Marino }
5359ef5ccd6cSJohn Marino 
5360ef5ccd6cSJohn Marino /* Remove the first reply in 'stop_reply_queue' which matches
5361ef5ccd6cSJohn Marino    PTID.  */
5362ef5ccd6cSJohn Marino 
5363ef5ccd6cSJohn Marino static struct stop_reply *
remote_notif_remove_queued_reply(ptid_t ptid)5364ef5ccd6cSJohn Marino remote_notif_remove_queued_reply (ptid_t ptid)
5365ef5ccd6cSJohn Marino {
5366ef5ccd6cSJohn Marino   struct queue_iter_param param;
5367ef5ccd6cSJohn Marino 
5368ef5ccd6cSJohn Marino   param.input = &ptid;
5369ef5ccd6cSJohn Marino   param.output = NULL;
5370ef5ccd6cSJohn Marino 
5371ef5ccd6cSJohn Marino   QUEUE_iterate (stop_reply_p, stop_reply_queue,
5372ef5ccd6cSJohn Marino 		 remote_notif_remove_once_on_match, &param);
5373ef5ccd6cSJohn Marino   if (notif_debug)
5374ef5ccd6cSJohn Marino     fprintf_unfiltered (gdb_stdlog,
5375ef5ccd6cSJohn Marino 			"notif: discard queued event: 'Stop' in %s\n",
5376ef5ccd6cSJohn Marino 			target_pid_to_str (ptid));
5377ef5ccd6cSJohn Marino 
5378ef5ccd6cSJohn Marino   return param.output;
53795796c8dcSSimon Schubert }
53805796c8dcSSimon Schubert 
53815796c8dcSSimon Schubert /* Look for a queued stop reply belonging to PTID.  If one is found,
53825796c8dcSSimon Schubert    remove it from the queue, and return it.  Returns NULL if none is
53835796c8dcSSimon Schubert    found.  If there are still queued events left to process, tell the
53845796c8dcSSimon Schubert    event loop to get back to target_wait soon.  */
53855796c8dcSSimon Schubert 
53865796c8dcSSimon Schubert static struct stop_reply *
queued_stop_reply(ptid_t ptid)53875796c8dcSSimon Schubert queued_stop_reply (ptid_t ptid)
53885796c8dcSSimon Schubert {
5389ef5ccd6cSJohn Marino   struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
53905796c8dcSSimon Schubert 
5391ef5ccd6cSJohn Marino   if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
53925796c8dcSSimon Schubert     /* There's still at least an event left.  */
53935796c8dcSSimon Schubert     mark_async_event_handler (remote_async_inferior_event_token);
53945796c8dcSSimon Schubert 
5395ef5ccd6cSJohn Marino   return r;
53965796c8dcSSimon Schubert }
53975796c8dcSSimon Schubert 
53985796c8dcSSimon Schubert /* Push a fully parsed stop reply in the stop reply queue.  Since we
53995796c8dcSSimon Schubert    know that we now have at least one queued event left to pass to the
54005796c8dcSSimon Schubert    core side, tell the event loop to get back to target_wait soon.  */
54015796c8dcSSimon Schubert 
54025796c8dcSSimon Schubert static void
push_stop_reply(struct stop_reply * new_event)54035796c8dcSSimon Schubert push_stop_reply (struct stop_reply *new_event)
54045796c8dcSSimon Schubert {
5405ef5ccd6cSJohn Marino   QUEUE_enque (stop_reply_p, stop_reply_queue, new_event);
54065796c8dcSSimon Schubert 
5407ef5ccd6cSJohn Marino   if (notif_debug)
5408ef5ccd6cSJohn Marino     fprintf_unfiltered (gdb_stdlog,
5409ef5ccd6cSJohn Marino 			"notif: push 'Stop' %s to queue %d\n",
5410ef5ccd6cSJohn Marino 			target_pid_to_str (new_event->ptid),
5411ef5ccd6cSJohn Marino 			QUEUE_length (stop_reply_p,
5412ef5ccd6cSJohn Marino 				      stop_reply_queue));
54135796c8dcSSimon Schubert 
54145796c8dcSSimon Schubert   mark_async_event_handler (remote_async_inferior_event_token);
54155796c8dcSSimon Schubert }
54165796c8dcSSimon Schubert 
5417ef5ccd6cSJohn Marino static int
stop_reply_match_ptid_and_ws(QUEUE (stop_reply_p)* q,QUEUE_ITER (stop_reply_p)* iter,struct stop_reply * event,void * data)5418ef5ccd6cSJohn Marino stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q,
5419ef5ccd6cSJohn Marino 			      QUEUE_ITER (stop_reply_p) *iter,
5420ef5ccd6cSJohn Marino 			      struct stop_reply *event,
5421ef5ccd6cSJohn Marino 			      void *data)
5422ef5ccd6cSJohn Marino {
5423ef5ccd6cSJohn Marino   ptid_t *ptid = data;
5424ef5ccd6cSJohn Marino 
5425ef5ccd6cSJohn Marino   return !(ptid_equal (*ptid, event->ptid)
5426ef5ccd6cSJohn Marino 	   && event->ws.kind == TARGET_WAITKIND_STOPPED);
5427ef5ccd6cSJohn Marino }
5428ef5ccd6cSJohn Marino 
54295796c8dcSSimon Schubert /* Returns true if we have a stop reply for PTID.  */
54305796c8dcSSimon Schubert 
54315796c8dcSSimon Schubert static int
peek_stop_reply(ptid_t ptid)54325796c8dcSSimon Schubert peek_stop_reply (ptid_t ptid)
54335796c8dcSSimon Schubert {
5434ef5ccd6cSJohn Marino   return !QUEUE_iterate (stop_reply_p, stop_reply_queue,
5435ef5ccd6cSJohn Marino 			 stop_reply_match_ptid_and_ws, &ptid);
54365796c8dcSSimon Schubert }
54375796c8dcSSimon Schubert 
54385796c8dcSSimon Schubert /* Parse the stop reply in BUF.  Either the function succeeds, and the
54395796c8dcSSimon Schubert    result is stored in EVENT, or throws an error.  */
54405796c8dcSSimon Schubert 
54415796c8dcSSimon Schubert static void
remote_parse_stop_reply(char * buf,struct stop_reply * event)54425796c8dcSSimon Schubert remote_parse_stop_reply (char *buf, struct stop_reply *event)
54435796c8dcSSimon Schubert {
54445796c8dcSSimon Schubert   struct remote_arch_state *rsa = get_remote_arch_state ();
54455796c8dcSSimon Schubert   ULONGEST addr;
54465796c8dcSSimon Schubert   char *p;
54475796c8dcSSimon Schubert 
54485796c8dcSSimon Schubert   event->ptid = null_ptid;
54495796c8dcSSimon Schubert   event->ws.kind = TARGET_WAITKIND_IGNORE;
54505796c8dcSSimon Schubert   event->ws.value.integer = 0;
54515796c8dcSSimon Schubert   event->solibs_changed = 0;
54525796c8dcSSimon Schubert   event->replay_event = 0;
54535796c8dcSSimon Schubert   event->stopped_by_watchpoint_p = 0;
54545796c8dcSSimon Schubert   event->regcache = NULL;
5455cf7f2e2dSJohn Marino   event->core = -1;
54565796c8dcSSimon Schubert 
54575796c8dcSSimon Schubert   switch (buf[0])
54585796c8dcSSimon Schubert     {
54595796c8dcSSimon Schubert     case 'T':		/* Status with PC, SP, FP, ...	*/
54605796c8dcSSimon Schubert       /* Expedited reply, containing Signal, {regno, reg} repeat.  */
54615796c8dcSSimon Schubert       /*  format is:  'Tssn...:r...;n...:r...;n...:r...;#cc', where
54625796c8dcSSimon Schubert 	    ss = signal number
54635796c8dcSSimon Schubert 	    n... = register number
54645796c8dcSSimon Schubert 	    r... = register contents
54655796c8dcSSimon Schubert       */
54665796c8dcSSimon Schubert 
54675796c8dcSSimon Schubert       p = &buf[3];	/* after Txx */
54685796c8dcSSimon Schubert       while (*p)
54695796c8dcSSimon Schubert 	{
54705796c8dcSSimon Schubert 	  char *p1;
54715796c8dcSSimon Schubert 	  char *p_temp;
54725796c8dcSSimon Schubert 	  int fieldsize;
54735796c8dcSSimon Schubert 	  LONGEST pnum = 0;
54745796c8dcSSimon Schubert 
54755796c8dcSSimon Schubert 	  /* If the packet contains a register number, save it in
54765796c8dcSSimon Schubert 	     pnum and set p1 to point to the character following it.
54775796c8dcSSimon Schubert 	     Otherwise p1 points to p.  */
54785796c8dcSSimon Schubert 
54795796c8dcSSimon Schubert 	  /* If this packet is an awatch packet, don't parse the 'a'
54805796c8dcSSimon Schubert 	     as a register number.  */
54815796c8dcSSimon Schubert 
5482cf7f2e2dSJohn Marino 	  if (strncmp (p, "awatch", strlen("awatch")) != 0
5483cf7f2e2dSJohn Marino 	      && strncmp (p, "core", strlen ("core") != 0))
54845796c8dcSSimon Schubert 	    {
54855796c8dcSSimon Schubert 	      /* Read the ``P'' register number.  */
54865796c8dcSSimon Schubert 	      pnum = strtol (p, &p_temp, 16);
54875796c8dcSSimon Schubert 	      p1 = p_temp;
54885796c8dcSSimon Schubert 	    }
54895796c8dcSSimon Schubert 	  else
54905796c8dcSSimon Schubert 	    p1 = p;
54915796c8dcSSimon Schubert 
54925796c8dcSSimon Schubert 	  if (p1 == p)	/* No register number present here.  */
54935796c8dcSSimon Schubert 	    {
54945796c8dcSSimon Schubert 	      p1 = strchr (p, ':');
54955796c8dcSSimon Schubert 	      if (p1 == NULL)
54965796c8dcSSimon Schubert 		error (_("Malformed packet(a) (missing colon): %s\n\
54975796c8dcSSimon Schubert Packet: '%s'\n"),
54985796c8dcSSimon Schubert 		       p, buf);
54995796c8dcSSimon Schubert 	      if (strncmp (p, "thread", p1 - p) == 0)
55005796c8dcSSimon Schubert 		event->ptid = read_ptid (++p1, &p);
55015796c8dcSSimon Schubert 	      else if ((strncmp (p, "watch", p1 - p) == 0)
55025796c8dcSSimon Schubert 		       || (strncmp (p, "rwatch", p1 - p) == 0)
55035796c8dcSSimon Schubert 		       || (strncmp (p, "awatch", p1 - p) == 0))
55045796c8dcSSimon Schubert 		{
55055796c8dcSSimon Schubert 		  event->stopped_by_watchpoint_p = 1;
55065796c8dcSSimon Schubert 		  p = unpack_varlen_hex (++p1, &addr);
55075796c8dcSSimon Schubert 		  event->watch_data_address = (CORE_ADDR) addr;
55085796c8dcSSimon Schubert 		}
55095796c8dcSSimon Schubert 	      else if (strncmp (p, "library", p1 - p) == 0)
55105796c8dcSSimon Schubert 		{
55115796c8dcSSimon Schubert 		  p1++;
55125796c8dcSSimon Schubert 		  p_temp = p1;
55135796c8dcSSimon Schubert 		  while (*p_temp && *p_temp != ';')
55145796c8dcSSimon Schubert 		    p_temp++;
55155796c8dcSSimon Schubert 
55165796c8dcSSimon Schubert 		  event->solibs_changed = 1;
55175796c8dcSSimon Schubert 		  p = p_temp;
55185796c8dcSSimon Schubert 		}
55195796c8dcSSimon Schubert 	      else if (strncmp (p, "replaylog", p1 - p) == 0)
55205796c8dcSSimon Schubert 		{
55215796c8dcSSimon Schubert 		  /* NO_HISTORY event.
55225796c8dcSSimon Schubert 		     p1 will indicate "begin" or "end", but
55235796c8dcSSimon Schubert 		     it makes no difference for now, so ignore it.  */
55245796c8dcSSimon Schubert 		  event->replay_event = 1;
55255796c8dcSSimon Schubert 		  p_temp = strchr (p1 + 1, ';');
55265796c8dcSSimon Schubert 		  if (p_temp)
55275796c8dcSSimon Schubert 		    p = p_temp;
55285796c8dcSSimon Schubert 		}
5529cf7f2e2dSJohn Marino 	      else if (strncmp (p, "core", p1 - p) == 0)
5530cf7f2e2dSJohn Marino 		{
5531cf7f2e2dSJohn Marino 		  ULONGEST c;
5532cf7f2e2dSJohn Marino 
5533cf7f2e2dSJohn Marino 		  p = unpack_varlen_hex (++p1, &c);
5534cf7f2e2dSJohn Marino 		  event->core = c;
5535cf7f2e2dSJohn Marino 		}
55365796c8dcSSimon Schubert 	      else
55375796c8dcSSimon Schubert 		{
55385796c8dcSSimon Schubert 		  /* Silently skip unknown optional info.  */
55395796c8dcSSimon Schubert 		  p_temp = strchr (p1 + 1, ';');
55405796c8dcSSimon Schubert 		  if (p_temp)
55415796c8dcSSimon Schubert 		    p = p_temp;
55425796c8dcSSimon Schubert 		}
55435796c8dcSSimon Schubert 	    }
55445796c8dcSSimon Schubert 	  else
55455796c8dcSSimon Schubert 	    {
55465796c8dcSSimon Schubert 	      struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
55475796c8dcSSimon Schubert 	      cached_reg_t cached_reg;
55485796c8dcSSimon Schubert 
55495796c8dcSSimon Schubert 	      p = p1;
55505796c8dcSSimon Schubert 
55515796c8dcSSimon Schubert 	      if (*p != ':')
55525796c8dcSSimon Schubert 		error (_("Malformed packet(b) (missing colon): %s\n\
55535796c8dcSSimon Schubert Packet: '%s'\n"),
55545796c8dcSSimon Schubert 		       p, buf);
55555796c8dcSSimon Schubert 	      ++p;
55565796c8dcSSimon Schubert 
55575796c8dcSSimon Schubert 	      if (reg == NULL)
55585796c8dcSSimon Schubert 		error (_("Remote sent bad register number %s: %s\n\
55595796c8dcSSimon Schubert Packet: '%s'\n"),
5560cf7f2e2dSJohn Marino 		       hex_string (pnum), p, buf);
55615796c8dcSSimon Schubert 
55625796c8dcSSimon Schubert 	      cached_reg.num = reg->regnum;
55635796c8dcSSimon Schubert 
55645796c8dcSSimon Schubert 	      fieldsize = hex2bin (p, cached_reg.data,
5565ef5ccd6cSJohn Marino 				   register_size (target_gdbarch (),
55665796c8dcSSimon Schubert 						  reg->regnum));
55675796c8dcSSimon Schubert 	      p += 2 * fieldsize;
5568ef5ccd6cSJohn Marino 	      if (fieldsize < register_size (target_gdbarch (),
55695796c8dcSSimon Schubert 					     reg->regnum))
55705796c8dcSSimon Schubert 		warning (_("Remote reply is too short: %s"), buf);
55715796c8dcSSimon Schubert 
55725796c8dcSSimon Schubert 	      VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
55735796c8dcSSimon Schubert 	    }
55745796c8dcSSimon Schubert 
55755796c8dcSSimon Schubert 	  if (*p != ';')
55765796c8dcSSimon Schubert 	    error (_("Remote register badly formatted: %s\nhere: %s"),
55775796c8dcSSimon Schubert 		   buf, p);
55785796c8dcSSimon Schubert 	  ++p;
55795796c8dcSSimon Schubert 	}
55805796c8dcSSimon Schubert       /* fall through */
55815796c8dcSSimon Schubert     case 'S':		/* Old style status, just signal only.  */
55825796c8dcSSimon Schubert       if (event->solibs_changed)
55835796c8dcSSimon Schubert 	event->ws.kind = TARGET_WAITKIND_LOADED;
55845796c8dcSSimon Schubert       else if (event->replay_event)
55855796c8dcSSimon Schubert 	event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
55865796c8dcSSimon Schubert       else
55875796c8dcSSimon Schubert 	{
55885796c8dcSSimon Schubert 	  event->ws.kind = TARGET_WAITKIND_STOPPED;
5589ef5ccd6cSJohn Marino 	  event->ws.value.sig = (enum gdb_signal)
55905796c8dcSSimon Schubert 	    (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
55915796c8dcSSimon Schubert 	}
55925796c8dcSSimon Schubert       break;
55935796c8dcSSimon Schubert     case 'W':		/* Target exited.  */
55945796c8dcSSimon Schubert     case 'X':
55955796c8dcSSimon Schubert       {
55965796c8dcSSimon Schubert 	char *p;
55975796c8dcSSimon Schubert 	int pid;
55985796c8dcSSimon Schubert 	ULONGEST value;
55995796c8dcSSimon Schubert 
56005796c8dcSSimon Schubert 	/* GDB used to accept only 2 hex chars here.  Stubs should
56015796c8dcSSimon Schubert 	   only send more if they detect GDB supports multi-process
56025796c8dcSSimon Schubert 	   support.  */
56035796c8dcSSimon Schubert 	p = unpack_varlen_hex (&buf[1], &value);
56045796c8dcSSimon Schubert 
56055796c8dcSSimon Schubert 	if (buf[0] == 'W')
56065796c8dcSSimon Schubert 	  {
56075796c8dcSSimon Schubert 	    /* The remote process exited.  */
56085796c8dcSSimon Schubert 	    event->ws.kind = TARGET_WAITKIND_EXITED;
56095796c8dcSSimon Schubert 	    event->ws.value.integer = value;
56105796c8dcSSimon Schubert 	  }
56115796c8dcSSimon Schubert 	else
56125796c8dcSSimon Schubert 	  {
56135796c8dcSSimon Schubert 	    /* The remote process exited with a signal.  */
56145796c8dcSSimon Schubert 	    event->ws.kind = TARGET_WAITKIND_SIGNALLED;
5615ef5ccd6cSJohn Marino 	    event->ws.value.sig = (enum gdb_signal) value;
56165796c8dcSSimon Schubert 	  }
56175796c8dcSSimon Schubert 
56185796c8dcSSimon Schubert 	/* If no process is specified, assume inferior_ptid.  */
56195796c8dcSSimon Schubert 	pid = ptid_get_pid (inferior_ptid);
56205796c8dcSSimon Schubert 	if (*p == '\0')
56215796c8dcSSimon Schubert 	  ;
56225796c8dcSSimon Schubert 	else if (*p == ';')
56235796c8dcSSimon Schubert 	  {
56245796c8dcSSimon Schubert 	    p++;
56255796c8dcSSimon Schubert 
5626*accba870Szrj 	    if (*p == '\0')
56275796c8dcSSimon Schubert 	      ;
56285796c8dcSSimon Schubert 	    else if (strncmp (p,
56295796c8dcSSimon Schubert 			      "process:", sizeof ("process:") - 1) == 0)
56305796c8dcSSimon Schubert 	      {
56315796c8dcSSimon Schubert 		ULONGEST upid;
5632cf7f2e2dSJohn Marino 
56335796c8dcSSimon Schubert 		p += sizeof ("process:") - 1;
56345796c8dcSSimon Schubert 		unpack_varlen_hex (p, &upid);
56355796c8dcSSimon Schubert 		pid = upid;
56365796c8dcSSimon Schubert 	      }
56375796c8dcSSimon Schubert 	    else
56385796c8dcSSimon Schubert 	      error (_("unknown stop reply packet: %s"), buf);
56395796c8dcSSimon Schubert 	  }
56405796c8dcSSimon Schubert 	else
56415796c8dcSSimon Schubert 	  error (_("unknown stop reply packet: %s"), buf);
56425796c8dcSSimon Schubert 	event->ptid = pid_to_ptid (pid);
56435796c8dcSSimon Schubert       }
56445796c8dcSSimon Schubert       break;
56455796c8dcSSimon Schubert     }
56465796c8dcSSimon Schubert 
56475796c8dcSSimon Schubert   if (non_stop && ptid_equal (event->ptid, null_ptid))
56485796c8dcSSimon Schubert     error (_("No process or thread specified in stop reply: %s"), buf);
56495796c8dcSSimon Schubert }
56505796c8dcSSimon Schubert 
5651ef5ccd6cSJohn Marino /* When the stub wants to tell GDB about a new notification reply, it
5652ef5ccd6cSJohn Marino    sends a notification (%Stop, for example).  Those can come it at
5653ef5ccd6cSJohn Marino    any time, hence, we have to make sure that any pending
5654ef5ccd6cSJohn Marino    putpkt/getpkt sequence we're making is finished, before querying
5655ef5ccd6cSJohn Marino    the stub for more events with the corresponding ack command
5656ef5ccd6cSJohn Marino    (vStopped, for example).  E.g., if we started a vStopped sequence
5657ef5ccd6cSJohn Marino    immediately upon receiving the notification, something like this
5658ef5ccd6cSJohn Marino    could happen:
56595796c8dcSSimon Schubert 
56605796c8dcSSimon Schubert     1.1) --> Hg 1
56615796c8dcSSimon Schubert     1.2) <-- OK
56625796c8dcSSimon Schubert     1.3) --> g
56635796c8dcSSimon Schubert     1.4) <-- %Stop
56645796c8dcSSimon Schubert     1.5) --> vStopped
56655796c8dcSSimon Schubert     1.6) <-- (registers reply to step #1.3)
56665796c8dcSSimon Schubert 
56675796c8dcSSimon Schubert    Obviously, the reply in step #1.6 would be unexpected to a vStopped
56685796c8dcSSimon Schubert    query.
56695796c8dcSSimon Schubert 
5670ef5ccd6cSJohn Marino    To solve this, whenever we parse a %Stop notification successfully,
56715796c8dcSSimon Schubert    we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
56725796c8dcSSimon Schubert    doing whatever we were doing:
56735796c8dcSSimon Schubert 
56745796c8dcSSimon Schubert     2.1) --> Hg 1
56755796c8dcSSimon Schubert     2.2) <-- OK
56765796c8dcSSimon Schubert     2.3) --> g
56775796c8dcSSimon Schubert     2.4) <-- %Stop
56785796c8dcSSimon Schubert       <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
56795796c8dcSSimon Schubert     2.5) <-- (registers reply to step #2.3)
56805796c8dcSSimon Schubert 
56815796c8dcSSimon Schubert    Eventualy after step #2.5, we return to the event loop, which
56825796c8dcSSimon Schubert    notices there's an event on the
56835796c8dcSSimon Schubert    REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
56845796c8dcSSimon Schubert    associated callback --- the function below.  At this point, we're
56855796c8dcSSimon Schubert    always safe to start a vStopped sequence. :
56865796c8dcSSimon Schubert 
56875796c8dcSSimon Schubert     2.6) --> vStopped
56885796c8dcSSimon Schubert     2.7) <-- T05 thread:2
56895796c8dcSSimon Schubert     2.8) --> vStopped
56905796c8dcSSimon Schubert     2.9) --> OK
56915796c8dcSSimon Schubert */
56925796c8dcSSimon Schubert 
5693ef5ccd6cSJohn Marino void
remote_notif_get_pending_events(struct notif_client * nc)5694ef5ccd6cSJohn Marino remote_notif_get_pending_events (struct notif_client *nc)
56955796c8dcSSimon Schubert {
56965796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
56975796c8dcSSimon Schubert 
5698ef5ccd6cSJohn Marino   if (nc->pending_event)
56995796c8dcSSimon Schubert     {
5700ef5ccd6cSJohn Marino       if (notif_debug)
5701ef5ccd6cSJohn Marino 	fprintf_unfiltered (gdb_stdlog,
5702ef5ccd6cSJohn Marino 			    "notif: process: '%s' ack pending event\n",
5703ef5ccd6cSJohn Marino 			    nc->name);
57045796c8dcSSimon Schubert 
5705ef5ccd6cSJohn Marino       /* acknowledge */
5706ef5ccd6cSJohn Marino       nc->ack (nc, rs->buf, nc->pending_event);
5707ef5ccd6cSJohn Marino       nc->pending_event = NULL;
57085796c8dcSSimon Schubert 
57095796c8dcSSimon Schubert       while (1)
57105796c8dcSSimon Schubert 	{
57115796c8dcSSimon Schubert 	  getpkt (&rs->buf, &rs->buf_size, 0);
57125796c8dcSSimon Schubert 	  if (strcmp (rs->buf, "OK") == 0)
57135796c8dcSSimon Schubert 	    break;
57145796c8dcSSimon Schubert 	  else
5715ef5ccd6cSJohn Marino 	    remote_notif_ack (nc, rs->buf);
5716ef5ccd6cSJohn Marino 	}
57175796c8dcSSimon Schubert     }
57185796c8dcSSimon Schubert   else
5719ef5ccd6cSJohn Marino     {
5720ef5ccd6cSJohn Marino       if (notif_debug)
5721ef5ccd6cSJohn Marino 	fprintf_unfiltered (gdb_stdlog,
5722ef5ccd6cSJohn Marino 			    "notif: process: '%s' no pending reply\n",
5723ef5ccd6cSJohn Marino 			    nc->name);
57245796c8dcSSimon Schubert     }
57255796c8dcSSimon Schubert }
57265796c8dcSSimon Schubert 
57275796c8dcSSimon Schubert /* Called when it is decided that STOP_REPLY holds the info of the
57285796c8dcSSimon Schubert    event that is to be returned to the core.  This function always
57295796c8dcSSimon Schubert    destroys STOP_REPLY.  */
57305796c8dcSSimon Schubert 
57315796c8dcSSimon Schubert static ptid_t
process_stop_reply(struct stop_reply * stop_reply,struct target_waitstatus * status)57325796c8dcSSimon Schubert process_stop_reply (struct stop_reply *stop_reply,
57335796c8dcSSimon Schubert 		    struct target_waitstatus *status)
57345796c8dcSSimon Schubert {
57355796c8dcSSimon Schubert   ptid_t ptid;
57365796c8dcSSimon Schubert 
57375796c8dcSSimon Schubert   *status = stop_reply->ws;
57385796c8dcSSimon Schubert   ptid = stop_reply->ptid;
57395796c8dcSSimon Schubert 
57405796c8dcSSimon Schubert   /* If no thread/process was reported by the stub, assume the current
57415796c8dcSSimon Schubert      inferior.  */
57425796c8dcSSimon Schubert   if (ptid_equal (ptid, null_ptid))
57435796c8dcSSimon Schubert     ptid = inferior_ptid;
57445796c8dcSSimon Schubert 
57455796c8dcSSimon Schubert   if (status->kind != TARGET_WAITKIND_EXITED
57465796c8dcSSimon Schubert       && status->kind != TARGET_WAITKIND_SIGNALLED)
57475796c8dcSSimon Schubert     {
57485796c8dcSSimon Schubert       /* Expedited registers.  */
57495796c8dcSSimon Schubert       if (stop_reply->regcache)
57505796c8dcSSimon Schubert 	{
57515796c8dcSSimon Schubert 	  struct regcache *regcache
5752ef5ccd6cSJohn Marino 	    = get_thread_arch_regcache (ptid, target_gdbarch ());
57535796c8dcSSimon Schubert 	  cached_reg_t *reg;
57545796c8dcSSimon Schubert 	  int ix;
57555796c8dcSSimon Schubert 
57565796c8dcSSimon Schubert 	  for (ix = 0;
57575796c8dcSSimon Schubert 	       VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
57585796c8dcSSimon Schubert 	       ix++)
57595796c8dcSSimon Schubert 	    regcache_raw_supply (regcache, reg->num, reg->data);
57605796c8dcSSimon Schubert 	  VEC_free (cached_reg_t, stop_reply->regcache);
57615796c8dcSSimon Schubert 	}
57625796c8dcSSimon Schubert 
57635796c8dcSSimon Schubert       remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p;
57645796c8dcSSimon Schubert       remote_watch_data_address = stop_reply->watch_data_address;
57655796c8dcSSimon Schubert 
57665796c8dcSSimon Schubert       remote_notice_new_inferior (ptid, 0);
5767cf7f2e2dSJohn Marino       demand_private_info (ptid)->core = stop_reply->core;
57685796c8dcSSimon Schubert     }
57695796c8dcSSimon Schubert 
57705796c8dcSSimon Schubert   stop_reply_xfree (stop_reply);
57715796c8dcSSimon Schubert   return ptid;
57725796c8dcSSimon Schubert }
57735796c8dcSSimon Schubert 
57745796c8dcSSimon Schubert /* The non-stop mode version of target_wait.  */
57755796c8dcSSimon Schubert 
57765796c8dcSSimon Schubert static ptid_t
remote_wait_ns(ptid_t ptid,struct target_waitstatus * status,int options)57775796c8dcSSimon Schubert remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
57785796c8dcSSimon Schubert {
57795796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
57805796c8dcSSimon Schubert   struct stop_reply *stop_reply;
57815796c8dcSSimon Schubert   int ret;
5782ef5ccd6cSJohn Marino   int is_notif = 0;
57835796c8dcSSimon Schubert 
57845796c8dcSSimon Schubert   /* If in non-stop mode, get out of getpkt even if a
57855796c8dcSSimon Schubert      notification is received.	*/
57865796c8dcSSimon Schubert 
57875796c8dcSSimon Schubert   ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5788ef5ccd6cSJohn Marino 			      0 /* forever */, &is_notif);
57895796c8dcSSimon Schubert   while (1)
57905796c8dcSSimon Schubert     {
5791ef5ccd6cSJohn Marino       if (ret != -1 && !is_notif)
57925796c8dcSSimon Schubert 	switch (rs->buf[0])
57935796c8dcSSimon Schubert 	  {
57945796c8dcSSimon Schubert 	  case 'E':		/* Error of some sort.	*/
57955796c8dcSSimon Schubert 	    /* We're out of sync with the target now.  Did it continue
57965796c8dcSSimon Schubert 	       or not?  We can't tell which thread it was in non-stop,
57975796c8dcSSimon Schubert 	       so just ignore this.  */
57985796c8dcSSimon Schubert 	    warning (_("Remote failure reply: %s"), rs->buf);
57995796c8dcSSimon Schubert 	    break;
58005796c8dcSSimon Schubert 	  case 'O':		/* Console output.  */
58015796c8dcSSimon Schubert 	    remote_console_output (rs->buf + 1);
58025796c8dcSSimon Schubert 	    break;
58035796c8dcSSimon Schubert 	  default:
58045796c8dcSSimon Schubert 	    warning (_("Invalid remote reply: %s"), rs->buf);
58055796c8dcSSimon Schubert 	    break;
58065796c8dcSSimon Schubert 	  }
58075796c8dcSSimon Schubert 
58085796c8dcSSimon Schubert       /* Acknowledge a pending stop reply that may have arrived in the
58095796c8dcSSimon Schubert 	 mean time.  */
5810ef5ccd6cSJohn Marino       if (notif_client_stop.pending_event != NULL)
5811ef5ccd6cSJohn Marino 	remote_notif_get_pending_events (&notif_client_stop);
58125796c8dcSSimon Schubert 
58135796c8dcSSimon Schubert       /* If indeed we noticed a stop reply, we're done.  */
58145796c8dcSSimon Schubert       stop_reply = queued_stop_reply (ptid);
58155796c8dcSSimon Schubert       if (stop_reply != NULL)
58165796c8dcSSimon Schubert 	return process_stop_reply (stop_reply, status);
58175796c8dcSSimon Schubert 
58185796c8dcSSimon Schubert       /* Still no event.  If we're just polling for an event, then
58195796c8dcSSimon Schubert 	 return to the event loop.  */
58205796c8dcSSimon Schubert       if (options & TARGET_WNOHANG)
58215796c8dcSSimon Schubert 	{
58225796c8dcSSimon Schubert 	  status->kind = TARGET_WAITKIND_IGNORE;
58235796c8dcSSimon Schubert 	  return minus_one_ptid;
58245796c8dcSSimon Schubert 	}
58255796c8dcSSimon Schubert 
58265796c8dcSSimon Schubert       /* Otherwise do a blocking wait.  */
58275796c8dcSSimon Schubert       ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5828ef5ccd6cSJohn Marino 				  1 /* forever */, &is_notif);
58295796c8dcSSimon Schubert     }
58305796c8dcSSimon Schubert }
58315796c8dcSSimon Schubert 
58325796c8dcSSimon Schubert /* Wait until the remote machine stops, then return, storing status in
58335796c8dcSSimon Schubert    STATUS just as `wait' would.  */
58345796c8dcSSimon Schubert 
58355796c8dcSSimon Schubert static ptid_t
remote_wait_as(ptid_t ptid,struct target_waitstatus * status,int options)58365796c8dcSSimon Schubert remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
58375796c8dcSSimon Schubert {
58385796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
58395796c8dcSSimon Schubert   ptid_t event_ptid = null_ptid;
58405796c8dcSSimon Schubert   char *buf;
58415796c8dcSSimon Schubert   struct stop_reply *stop_reply;
58425796c8dcSSimon Schubert 
58435796c8dcSSimon Schubert  again:
58445796c8dcSSimon Schubert 
58455796c8dcSSimon Schubert   status->kind = TARGET_WAITKIND_IGNORE;
58465796c8dcSSimon Schubert   status->value.integer = 0;
58475796c8dcSSimon Schubert 
58485796c8dcSSimon Schubert   stop_reply = queued_stop_reply (ptid);
58495796c8dcSSimon Schubert   if (stop_reply != NULL)
58505796c8dcSSimon Schubert     return process_stop_reply (stop_reply, status);
58515796c8dcSSimon Schubert 
58525796c8dcSSimon Schubert   if (rs->cached_wait_status)
58535796c8dcSSimon Schubert     /* Use the cached wait status, but only once.  */
58545796c8dcSSimon Schubert     rs->cached_wait_status = 0;
58555796c8dcSSimon Schubert   else
58565796c8dcSSimon Schubert     {
58575796c8dcSSimon Schubert       int ret;
5858ef5ccd6cSJohn Marino       int is_notif;
58595796c8dcSSimon Schubert 
58605796c8dcSSimon Schubert       if (!target_is_async_p ())
58615796c8dcSSimon Schubert 	{
58625796c8dcSSimon Schubert 	  ofunc = signal (SIGINT, remote_interrupt);
58635796c8dcSSimon Schubert 	  /* If the user hit C-c before this packet, or between packets,
58645796c8dcSSimon Schubert 	     pretend that it was hit right here.  */
5865ef5ccd6cSJohn Marino 	  if (check_quit_flag ())
58665796c8dcSSimon Schubert 	    {
5867ef5ccd6cSJohn Marino 	      clear_quit_flag ();
58685796c8dcSSimon Schubert 	      remote_interrupt (SIGINT);
58695796c8dcSSimon Schubert 	    }
58705796c8dcSSimon Schubert 	}
58715796c8dcSSimon Schubert 
58725796c8dcSSimon Schubert       /* FIXME: cagney/1999-09-27: If we're in async mode we should
58735796c8dcSSimon Schubert 	 _never_ wait for ever -> test on target_is_async_p().
58745796c8dcSSimon Schubert 	 However, before we do that we need to ensure that the caller
58755796c8dcSSimon Schubert 	 knows how to take the target into/out of async mode.  */
5876ef5ccd6cSJohn Marino       ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5877ef5ccd6cSJohn Marino 				  wait_forever_enabled_p, &is_notif);
5878ef5ccd6cSJohn Marino 
5879ef5ccd6cSJohn Marino       /* GDB gets a notification.  Return to core as this event is
5880ef5ccd6cSJohn Marino 	 not interesting.  */
5881ef5ccd6cSJohn Marino       if (ret != -1 && is_notif)
5882ef5ccd6cSJohn Marino 	return minus_one_ptid;
5883ef5ccd6cSJohn Marino 
58845796c8dcSSimon Schubert       if (!target_is_async_p ())
58855796c8dcSSimon Schubert 	signal (SIGINT, ofunc);
58865796c8dcSSimon Schubert     }
58875796c8dcSSimon Schubert 
58885796c8dcSSimon Schubert   buf = rs->buf;
58895796c8dcSSimon Schubert 
58905796c8dcSSimon Schubert   remote_stopped_by_watchpoint_p = 0;
58915796c8dcSSimon Schubert 
58925796c8dcSSimon Schubert   /* We got something.  */
58935796c8dcSSimon Schubert   rs->waiting_for_stop_reply = 0;
58945796c8dcSSimon Schubert 
5895cf7f2e2dSJohn Marino   /* Assume that the target has acknowledged Ctrl-C unless we receive
5896cf7f2e2dSJohn Marino      an 'F' or 'O' packet.  */
5897cf7f2e2dSJohn Marino   if (buf[0] != 'F' && buf[0] != 'O')
5898cf7f2e2dSJohn Marino     rs->ctrlc_pending_p = 0;
5899cf7f2e2dSJohn Marino 
59005796c8dcSSimon Schubert   switch (buf[0])
59015796c8dcSSimon Schubert     {
59025796c8dcSSimon Schubert     case 'E':		/* Error of some sort.	*/
59035796c8dcSSimon Schubert       /* We're out of sync with the target now.  Did it continue or
59045796c8dcSSimon Schubert 	 not?  Not is more likely, so report a stop.  */
59055796c8dcSSimon Schubert       warning (_("Remote failure reply: %s"), buf);
59065796c8dcSSimon Schubert       status->kind = TARGET_WAITKIND_STOPPED;
5907ef5ccd6cSJohn Marino       status->value.sig = GDB_SIGNAL_0;
59085796c8dcSSimon Schubert       break;
59095796c8dcSSimon Schubert     case 'F':		/* File-I/O request.  */
5910cf7f2e2dSJohn Marino       remote_fileio_request (buf, rs->ctrlc_pending_p);
5911cf7f2e2dSJohn Marino       rs->ctrlc_pending_p = 0;
59125796c8dcSSimon Schubert       break;
59135796c8dcSSimon Schubert     case 'T': case 'S': case 'X': case 'W':
59145796c8dcSSimon Schubert       {
5915ef5ccd6cSJohn Marino 	struct stop_reply *stop_reply
5916ef5ccd6cSJohn Marino 	  = (struct stop_reply *) remote_notif_parse (&notif_client_stop,
5917ef5ccd6cSJohn Marino 						      rs->buf);
59185796c8dcSSimon Schubert 
59195796c8dcSSimon Schubert 	event_ptid = process_stop_reply (stop_reply, status);
59205796c8dcSSimon Schubert 	break;
59215796c8dcSSimon Schubert       }
59225796c8dcSSimon Schubert     case 'O':		/* Console output.  */
59235796c8dcSSimon Schubert       remote_console_output (buf + 1);
59245796c8dcSSimon Schubert 
59255796c8dcSSimon Schubert       /* The target didn't really stop; keep waiting.  */
59265796c8dcSSimon Schubert       rs->waiting_for_stop_reply = 1;
59275796c8dcSSimon Schubert 
59285796c8dcSSimon Schubert       break;
59295796c8dcSSimon Schubert     case '\0':
5930ef5ccd6cSJohn Marino       if (last_sent_signal != GDB_SIGNAL_0)
59315796c8dcSSimon Schubert 	{
59325796c8dcSSimon Schubert 	  /* Zero length reply means that we tried 'S' or 'C' and the
59335796c8dcSSimon Schubert 	     remote system doesn't support it.  */
59345796c8dcSSimon Schubert 	  target_terminal_ours_for_output ();
59355796c8dcSSimon Schubert 	  printf_filtered
59365796c8dcSSimon Schubert 	    ("Can't send signals to this remote system.  %s not sent.\n",
5937ef5ccd6cSJohn Marino 	     gdb_signal_to_name (last_sent_signal));
5938ef5ccd6cSJohn Marino 	  last_sent_signal = GDB_SIGNAL_0;
59395796c8dcSSimon Schubert 	  target_terminal_inferior ();
59405796c8dcSSimon Schubert 
59415796c8dcSSimon Schubert 	  strcpy ((char *) buf, last_sent_step ? "s" : "c");
59425796c8dcSSimon Schubert 	  putpkt ((char *) buf);
59435796c8dcSSimon Schubert 
59445796c8dcSSimon Schubert 	  /* We just told the target to resume, so a stop reply is in
59455796c8dcSSimon Schubert 	     order.  */
59465796c8dcSSimon Schubert 	  rs->waiting_for_stop_reply = 1;
59475796c8dcSSimon Schubert 	  break;
59485796c8dcSSimon Schubert 	}
59495796c8dcSSimon Schubert       /* else fallthrough */
59505796c8dcSSimon Schubert     default:
59515796c8dcSSimon Schubert       warning (_("Invalid remote reply: %s"), buf);
59525796c8dcSSimon Schubert       /* Keep waiting.  */
59535796c8dcSSimon Schubert       rs->waiting_for_stop_reply = 1;
59545796c8dcSSimon Schubert       break;
59555796c8dcSSimon Schubert     }
59565796c8dcSSimon Schubert 
59575796c8dcSSimon Schubert   if (status->kind == TARGET_WAITKIND_IGNORE)
59585796c8dcSSimon Schubert     {
59595796c8dcSSimon Schubert       /* Nothing interesting happened.  If we're doing a non-blocking
59605796c8dcSSimon Schubert 	 poll, we're done.  Otherwise, go back to waiting.  */
59615796c8dcSSimon Schubert       if (options & TARGET_WNOHANG)
59625796c8dcSSimon Schubert 	return minus_one_ptid;
59635796c8dcSSimon Schubert       else
59645796c8dcSSimon Schubert 	goto again;
59655796c8dcSSimon Schubert     }
59665796c8dcSSimon Schubert   else if (status->kind != TARGET_WAITKIND_EXITED
59675796c8dcSSimon Schubert 	   && status->kind != TARGET_WAITKIND_SIGNALLED)
59685796c8dcSSimon Schubert     {
59695796c8dcSSimon Schubert       if (!ptid_equal (event_ptid, null_ptid))
59705796c8dcSSimon Schubert 	record_currthread (event_ptid);
59715796c8dcSSimon Schubert       else
59725796c8dcSSimon Schubert 	event_ptid = inferior_ptid;
59735796c8dcSSimon Schubert     }
59745796c8dcSSimon Schubert   else
59755796c8dcSSimon Schubert     /* A process exit.  Invalidate our notion of current thread.  */
59765796c8dcSSimon Schubert     record_currthread (minus_one_ptid);
59775796c8dcSSimon Schubert 
59785796c8dcSSimon Schubert   return event_ptid;
59795796c8dcSSimon Schubert }
59805796c8dcSSimon Schubert 
59815796c8dcSSimon Schubert /* Wait until the remote machine stops, then return, storing status in
59825796c8dcSSimon Schubert    STATUS just as `wait' would.  */
59835796c8dcSSimon Schubert 
59845796c8dcSSimon Schubert static ptid_t
remote_wait(struct target_ops * ops,ptid_t ptid,struct target_waitstatus * status,int options)59855796c8dcSSimon Schubert remote_wait (struct target_ops *ops,
59865796c8dcSSimon Schubert 	     ptid_t ptid, struct target_waitstatus *status, int options)
59875796c8dcSSimon Schubert {
59885796c8dcSSimon Schubert   ptid_t event_ptid;
59895796c8dcSSimon Schubert 
59905796c8dcSSimon Schubert   if (non_stop)
59915796c8dcSSimon Schubert     event_ptid = remote_wait_ns (ptid, status, options);
59925796c8dcSSimon Schubert   else
59935796c8dcSSimon Schubert     event_ptid = remote_wait_as (ptid, status, options);
59945796c8dcSSimon Schubert 
59955796c8dcSSimon Schubert   if (target_can_async_p ())
59965796c8dcSSimon Schubert     {
59975796c8dcSSimon Schubert       /* If there are are events left in the queue tell the event loop
59985796c8dcSSimon Schubert 	 to return here.  */
5999ef5ccd6cSJohn Marino       if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue))
60005796c8dcSSimon Schubert 	mark_async_event_handler (remote_async_inferior_event_token);
60015796c8dcSSimon Schubert     }
60025796c8dcSSimon Schubert 
60035796c8dcSSimon Schubert   return event_ptid;
60045796c8dcSSimon Schubert }
60055796c8dcSSimon Schubert 
60065796c8dcSSimon Schubert /* Fetch a single register using a 'p' packet.  */
60075796c8dcSSimon Schubert 
60085796c8dcSSimon Schubert static int
fetch_register_using_p(struct regcache * regcache,struct packet_reg * reg)60095796c8dcSSimon Schubert fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
60105796c8dcSSimon Schubert {
60115796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
60125796c8dcSSimon Schubert   char *buf, *p;
60135796c8dcSSimon Schubert   char regp[MAX_REGISTER_SIZE];
60145796c8dcSSimon Schubert   int i;
60155796c8dcSSimon Schubert 
60165796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_p].support == PACKET_DISABLE)
60175796c8dcSSimon Schubert     return 0;
60185796c8dcSSimon Schubert 
60195796c8dcSSimon Schubert   if (reg->pnum == -1)
60205796c8dcSSimon Schubert     return 0;
60215796c8dcSSimon Schubert 
60225796c8dcSSimon Schubert   p = rs->buf;
60235796c8dcSSimon Schubert   *p++ = 'p';
60245796c8dcSSimon Schubert   p += hexnumstr (p, reg->pnum);
60255796c8dcSSimon Schubert   *p++ = '\0';
60265796c8dcSSimon Schubert   putpkt (rs->buf);
60275796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
60285796c8dcSSimon Schubert 
60295796c8dcSSimon Schubert   buf = rs->buf;
60305796c8dcSSimon Schubert 
60315796c8dcSSimon Schubert   switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
60325796c8dcSSimon Schubert     {
60335796c8dcSSimon Schubert     case PACKET_OK:
60345796c8dcSSimon Schubert       break;
60355796c8dcSSimon Schubert     case PACKET_UNKNOWN:
60365796c8dcSSimon Schubert       return 0;
60375796c8dcSSimon Schubert     case PACKET_ERROR:
60385796c8dcSSimon Schubert       error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
60395796c8dcSSimon Schubert 	     gdbarch_register_name (get_regcache_arch (regcache),
60405796c8dcSSimon Schubert 				    reg->regnum),
60415796c8dcSSimon Schubert 	     buf);
60425796c8dcSSimon Schubert     }
60435796c8dcSSimon Schubert 
60445796c8dcSSimon Schubert   /* If this register is unfetchable, tell the regcache.  */
60455796c8dcSSimon Schubert   if (buf[0] == 'x')
60465796c8dcSSimon Schubert     {
60475796c8dcSSimon Schubert       regcache_raw_supply (regcache, reg->regnum, NULL);
60485796c8dcSSimon Schubert       return 1;
60495796c8dcSSimon Schubert     }
60505796c8dcSSimon Schubert 
60515796c8dcSSimon Schubert   /* Otherwise, parse and supply the value.  */
60525796c8dcSSimon Schubert   p = buf;
60535796c8dcSSimon Schubert   i = 0;
60545796c8dcSSimon Schubert   while (p[0] != 0)
60555796c8dcSSimon Schubert     {
60565796c8dcSSimon Schubert       if (p[1] == 0)
60575796c8dcSSimon Schubert 	error (_("fetch_register_using_p: early buf termination"));
60585796c8dcSSimon Schubert 
60595796c8dcSSimon Schubert       regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
60605796c8dcSSimon Schubert       p += 2;
60615796c8dcSSimon Schubert     }
60625796c8dcSSimon Schubert   regcache_raw_supply (regcache, reg->regnum, regp);
60635796c8dcSSimon Schubert   return 1;
60645796c8dcSSimon Schubert }
60655796c8dcSSimon Schubert 
60665796c8dcSSimon Schubert /* Fetch the registers included in the target's 'g' packet.  */
60675796c8dcSSimon Schubert 
60685796c8dcSSimon Schubert static int
send_g_packet(void)60695796c8dcSSimon Schubert send_g_packet (void)
60705796c8dcSSimon Schubert {
60715796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
60725796c8dcSSimon Schubert   int buf_len;
60735796c8dcSSimon Schubert 
6074ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "g");
60755796c8dcSSimon Schubert   remote_send (&rs->buf, &rs->buf_size);
60765796c8dcSSimon Schubert 
60775796c8dcSSimon Schubert   /* We can get out of synch in various cases.  If the first character
60785796c8dcSSimon Schubert      in the buffer is not a hex character, assume that has happened
60795796c8dcSSimon Schubert      and try to fetch another packet to read.  */
60805796c8dcSSimon Schubert   while ((rs->buf[0] < '0' || rs->buf[0] > '9')
60815796c8dcSSimon Schubert 	 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
60825796c8dcSSimon Schubert 	 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
60835796c8dcSSimon Schubert 	 && rs->buf[0] != 'x')	/* New: unavailable register value.  */
60845796c8dcSSimon Schubert     {
60855796c8dcSSimon Schubert       if (remote_debug)
60865796c8dcSSimon Schubert 	fprintf_unfiltered (gdb_stdlog,
60875796c8dcSSimon Schubert 			    "Bad register packet; fetching a new packet\n");
60885796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
60895796c8dcSSimon Schubert     }
60905796c8dcSSimon Schubert 
60915796c8dcSSimon Schubert   buf_len = strlen (rs->buf);
60925796c8dcSSimon Schubert 
60935796c8dcSSimon Schubert   /* Sanity check the received packet.  */
60945796c8dcSSimon Schubert   if (buf_len % 2 != 0)
60955796c8dcSSimon Schubert     error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
60965796c8dcSSimon Schubert 
60975796c8dcSSimon Schubert   return buf_len / 2;
60985796c8dcSSimon Schubert }
60995796c8dcSSimon Schubert 
61005796c8dcSSimon Schubert static void
process_g_packet(struct regcache * regcache)61015796c8dcSSimon Schubert process_g_packet (struct regcache *regcache)
61025796c8dcSSimon Schubert {
61035796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_regcache_arch (regcache);
61045796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
61055796c8dcSSimon Schubert   struct remote_arch_state *rsa = get_remote_arch_state ();
61065796c8dcSSimon Schubert   int i, buf_len;
61075796c8dcSSimon Schubert   char *p;
61085796c8dcSSimon Schubert   char *regs;
61095796c8dcSSimon Schubert 
61105796c8dcSSimon Schubert   buf_len = strlen (rs->buf);
61115796c8dcSSimon Schubert 
61125796c8dcSSimon Schubert   /* Further sanity checks, with knowledge of the architecture.  */
61135796c8dcSSimon Schubert   if (buf_len > 2 * rsa->sizeof_g_packet)
61145796c8dcSSimon Schubert     error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
61155796c8dcSSimon Schubert 
61165796c8dcSSimon Schubert   /* Save the size of the packet sent to us by the target.  It is used
61175796c8dcSSimon Schubert      as a heuristic when determining the max size of packets that the
61185796c8dcSSimon Schubert      target can safely receive.  */
61195796c8dcSSimon Schubert   if (rsa->actual_register_packet_size == 0)
61205796c8dcSSimon Schubert     rsa->actual_register_packet_size = buf_len;
61215796c8dcSSimon Schubert 
61225796c8dcSSimon Schubert   /* If this is smaller than we guessed the 'g' packet would be,
61235796c8dcSSimon Schubert      update our records.  A 'g' reply that doesn't include a register's
61245796c8dcSSimon Schubert      value implies either that the register is not available, or that
61255796c8dcSSimon Schubert      the 'p' packet must be used.  */
61265796c8dcSSimon Schubert   if (buf_len < 2 * rsa->sizeof_g_packet)
61275796c8dcSSimon Schubert     {
61285796c8dcSSimon Schubert       rsa->sizeof_g_packet = buf_len / 2;
61295796c8dcSSimon Schubert 
61305796c8dcSSimon Schubert       for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
61315796c8dcSSimon Schubert 	{
61325796c8dcSSimon Schubert 	  if (rsa->regs[i].pnum == -1)
61335796c8dcSSimon Schubert 	    continue;
61345796c8dcSSimon Schubert 
61355796c8dcSSimon Schubert 	  if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
61365796c8dcSSimon Schubert 	    rsa->regs[i].in_g_packet = 0;
61375796c8dcSSimon Schubert 	  else
61385796c8dcSSimon Schubert 	    rsa->regs[i].in_g_packet = 1;
61395796c8dcSSimon Schubert 	}
61405796c8dcSSimon Schubert     }
61415796c8dcSSimon Schubert 
61425796c8dcSSimon Schubert   regs = alloca (rsa->sizeof_g_packet);
61435796c8dcSSimon Schubert 
61445796c8dcSSimon Schubert   /* Unimplemented registers read as all bits zero.  */
61455796c8dcSSimon Schubert   memset (regs, 0, rsa->sizeof_g_packet);
61465796c8dcSSimon Schubert 
61475796c8dcSSimon Schubert   /* Reply describes registers byte by byte, each byte encoded as two
61485796c8dcSSimon Schubert      hex characters.  Suck them all up, then supply them to the
61495796c8dcSSimon Schubert      register cacheing/storage mechanism.  */
61505796c8dcSSimon Schubert 
61515796c8dcSSimon Schubert   p = rs->buf;
61525796c8dcSSimon Schubert   for (i = 0; i < rsa->sizeof_g_packet; i++)
61535796c8dcSSimon Schubert     {
61545796c8dcSSimon Schubert       if (p[0] == 0 || p[1] == 0)
61555796c8dcSSimon Schubert 	/* This shouldn't happen - we adjusted sizeof_g_packet above.  */
61565796c8dcSSimon Schubert 	internal_error (__FILE__, __LINE__,
6157c50c785cSJohn Marino 			_("unexpected end of 'g' packet reply"));
61585796c8dcSSimon Schubert 
61595796c8dcSSimon Schubert       if (p[0] == 'x' && p[1] == 'x')
61605796c8dcSSimon Schubert 	regs[i] = 0;		/* 'x' */
61615796c8dcSSimon Schubert       else
61625796c8dcSSimon Schubert 	regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
61635796c8dcSSimon Schubert       p += 2;
61645796c8dcSSimon Schubert     }
61655796c8dcSSimon Schubert 
61665796c8dcSSimon Schubert   for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
61675796c8dcSSimon Schubert     {
61685796c8dcSSimon Schubert       struct packet_reg *r = &rsa->regs[i];
6169cf7f2e2dSJohn Marino 
61705796c8dcSSimon Schubert       if (r->in_g_packet)
61715796c8dcSSimon Schubert 	{
61725796c8dcSSimon Schubert 	  if (r->offset * 2 >= strlen (rs->buf))
61735796c8dcSSimon Schubert 	    /* This shouldn't happen - we adjusted in_g_packet above.  */
61745796c8dcSSimon Schubert 	    internal_error (__FILE__, __LINE__,
6175c50c785cSJohn Marino 			    _("unexpected end of 'g' packet reply"));
61765796c8dcSSimon Schubert 	  else if (rs->buf[r->offset * 2] == 'x')
61775796c8dcSSimon Schubert 	    {
61785796c8dcSSimon Schubert 	      gdb_assert (r->offset * 2 < strlen (rs->buf));
61795796c8dcSSimon Schubert 	      /* The register isn't available, mark it as such (at
61805796c8dcSSimon Schubert 		 the same time setting the value to zero).  */
61815796c8dcSSimon Schubert 	      regcache_raw_supply (regcache, r->regnum, NULL);
61825796c8dcSSimon Schubert 	    }
61835796c8dcSSimon Schubert 	  else
61845796c8dcSSimon Schubert 	    regcache_raw_supply (regcache, r->regnum,
61855796c8dcSSimon Schubert 				 regs + r->offset);
61865796c8dcSSimon Schubert 	}
61875796c8dcSSimon Schubert     }
61885796c8dcSSimon Schubert }
61895796c8dcSSimon Schubert 
61905796c8dcSSimon Schubert static void
fetch_registers_using_g(struct regcache * regcache)61915796c8dcSSimon Schubert fetch_registers_using_g (struct regcache *regcache)
61925796c8dcSSimon Schubert {
61935796c8dcSSimon Schubert   send_g_packet ();
61945796c8dcSSimon Schubert   process_g_packet (regcache);
61955796c8dcSSimon Schubert }
61965796c8dcSSimon Schubert 
6197c50c785cSJohn Marino /* Make the remote selected traceframe match GDB's selected
6198c50c785cSJohn Marino    traceframe.  */
6199c50c785cSJohn Marino 
6200c50c785cSJohn Marino static void
set_remote_traceframe(void)6201c50c785cSJohn Marino set_remote_traceframe (void)
6202c50c785cSJohn Marino {
6203c50c785cSJohn Marino   int newnum;
6204c50c785cSJohn Marino 
6205c50c785cSJohn Marino   if (remote_traceframe_number == get_traceframe_number ())
6206c50c785cSJohn Marino     return;
6207c50c785cSJohn Marino 
6208c50c785cSJohn Marino   /* Avoid recursion, remote_trace_find calls us again.  */
6209c50c785cSJohn Marino   remote_traceframe_number = get_traceframe_number ();
6210c50c785cSJohn Marino 
6211c50c785cSJohn Marino   newnum = target_trace_find (tfind_number,
6212c50c785cSJohn Marino 			      get_traceframe_number (), 0, 0, NULL);
6213c50c785cSJohn Marino 
6214c50c785cSJohn Marino   /* Should not happen.  If it does, all bets are off.  */
6215c50c785cSJohn Marino   if (newnum != get_traceframe_number ())
6216c50c785cSJohn Marino     warning (_("could not set remote traceframe"));
6217c50c785cSJohn Marino }
6218c50c785cSJohn Marino 
62195796c8dcSSimon Schubert static void
remote_fetch_registers(struct target_ops * ops,struct regcache * regcache,int regnum)62205796c8dcSSimon Schubert remote_fetch_registers (struct target_ops *ops,
62215796c8dcSSimon Schubert 			struct regcache *regcache, int regnum)
62225796c8dcSSimon Schubert {
62235796c8dcSSimon Schubert   struct remote_arch_state *rsa = get_remote_arch_state ();
62245796c8dcSSimon Schubert   int i;
62255796c8dcSSimon Schubert 
6226c50c785cSJohn Marino   set_remote_traceframe ();
62275796c8dcSSimon Schubert   set_general_thread (inferior_ptid);
62285796c8dcSSimon Schubert 
62295796c8dcSSimon Schubert   if (regnum >= 0)
62305796c8dcSSimon Schubert     {
62315796c8dcSSimon Schubert       struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6232cf7f2e2dSJohn Marino 
62335796c8dcSSimon Schubert       gdb_assert (reg != NULL);
62345796c8dcSSimon Schubert 
62355796c8dcSSimon Schubert       /* If this register might be in the 'g' packet, try that first -
62365796c8dcSSimon Schubert 	 we are likely to read more than one register.  If this is the
62375796c8dcSSimon Schubert 	 first 'g' packet, we might be overly optimistic about its
62385796c8dcSSimon Schubert 	 contents, so fall back to 'p'.  */
62395796c8dcSSimon Schubert       if (reg->in_g_packet)
62405796c8dcSSimon Schubert 	{
62415796c8dcSSimon Schubert 	  fetch_registers_using_g (regcache);
62425796c8dcSSimon Schubert 	  if (reg->in_g_packet)
62435796c8dcSSimon Schubert 	    return;
62445796c8dcSSimon Schubert 	}
62455796c8dcSSimon Schubert 
62465796c8dcSSimon Schubert       if (fetch_register_using_p (regcache, reg))
62475796c8dcSSimon Schubert 	return;
62485796c8dcSSimon Schubert 
62495796c8dcSSimon Schubert       /* This register is not available.  */
62505796c8dcSSimon Schubert       regcache_raw_supply (regcache, reg->regnum, NULL);
62515796c8dcSSimon Schubert 
62525796c8dcSSimon Schubert       return;
62535796c8dcSSimon Schubert     }
62545796c8dcSSimon Schubert 
62555796c8dcSSimon Schubert   fetch_registers_using_g (regcache);
62565796c8dcSSimon Schubert 
62575796c8dcSSimon Schubert   for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
62585796c8dcSSimon Schubert     if (!rsa->regs[i].in_g_packet)
62595796c8dcSSimon Schubert       if (!fetch_register_using_p (regcache, &rsa->regs[i]))
62605796c8dcSSimon Schubert 	{
62615796c8dcSSimon Schubert 	  /* This register is not available.  */
62625796c8dcSSimon Schubert 	  regcache_raw_supply (regcache, i, NULL);
62635796c8dcSSimon Schubert 	}
62645796c8dcSSimon Schubert }
62655796c8dcSSimon Schubert 
62665796c8dcSSimon Schubert /* Prepare to store registers.  Since we may send them all (using a
62675796c8dcSSimon Schubert    'G' request), we have to read out the ones we don't want to change
62685796c8dcSSimon Schubert    first.  */
62695796c8dcSSimon Schubert 
62705796c8dcSSimon Schubert static void
remote_prepare_to_store(struct regcache * regcache)62715796c8dcSSimon Schubert remote_prepare_to_store (struct regcache *regcache)
62725796c8dcSSimon Schubert {
62735796c8dcSSimon Schubert   struct remote_arch_state *rsa = get_remote_arch_state ();
62745796c8dcSSimon Schubert   int i;
62755796c8dcSSimon Schubert   gdb_byte buf[MAX_REGISTER_SIZE];
62765796c8dcSSimon Schubert 
62775796c8dcSSimon Schubert   /* Make sure the entire registers array is valid.  */
62785796c8dcSSimon Schubert   switch (remote_protocol_packets[PACKET_P].support)
62795796c8dcSSimon Schubert     {
62805796c8dcSSimon Schubert     case PACKET_DISABLE:
62815796c8dcSSimon Schubert     case PACKET_SUPPORT_UNKNOWN:
62825796c8dcSSimon Schubert       /* Make sure all the necessary registers are cached.  */
62835796c8dcSSimon Schubert       for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
62845796c8dcSSimon Schubert 	if (rsa->regs[i].in_g_packet)
62855796c8dcSSimon Schubert 	  regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
62865796c8dcSSimon Schubert       break;
62875796c8dcSSimon Schubert     case PACKET_ENABLE:
62885796c8dcSSimon Schubert       break;
62895796c8dcSSimon Schubert     }
62905796c8dcSSimon Schubert }
62915796c8dcSSimon Schubert 
62925796c8dcSSimon Schubert /* Helper: Attempt to store REGNUM using the P packet.  Return fail IFF
62935796c8dcSSimon Schubert    packet was not recognized.  */
62945796c8dcSSimon Schubert 
62955796c8dcSSimon Schubert static int
store_register_using_P(const struct regcache * regcache,struct packet_reg * reg)62965796c8dcSSimon Schubert store_register_using_P (const struct regcache *regcache,
62975796c8dcSSimon Schubert 			struct packet_reg *reg)
62985796c8dcSSimon Schubert {
62995796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_regcache_arch (regcache);
63005796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
63015796c8dcSSimon Schubert   /* Try storing a single register.  */
63025796c8dcSSimon Schubert   char *buf = rs->buf;
63035796c8dcSSimon Schubert   gdb_byte regp[MAX_REGISTER_SIZE];
63045796c8dcSSimon Schubert   char *p;
63055796c8dcSSimon Schubert 
63065796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_P].support == PACKET_DISABLE)
63075796c8dcSSimon Schubert     return 0;
63085796c8dcSSimon Schubert 
63095796c8dcSSimon Schubert   if (reg->pnum == -1)
63105796c8dcSSimon Schubert     return 0;
63115796c8dcSSimon Schubert 
63125796c8dcSSimon Schubert   xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
63135796c8dcSSimon Schubert   p = buf + strlen (buf);
63145796c8dcSSimon Schubert   regcache_raw_collect (regcache, reg->regnum, regp);
63155796c8dcSSimon Schubert   bin2hex (regp, p, register_size (gdbarch, reg->regnum));
63165796c8dcSSimon Schubert   putpkt (rs->buf);
63175796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
63185796c8dcSSimon Schubert 
63195796c8dcSSimon Schubert   switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
63205796c8dcSSimon Schubert     {
63215796c8dcSSimon Schubert     case PACKET_OK:
63225796c8dcSSimon Schubert       return 1;
63235796c8dcSSimon Schubert     case PACKET_ERROR:
63245796c8dcSSimon Schubert       error (_("Could not write register \"%s\"; remote failure reply '%s'"),
63255796c8dcSSimon Schubert 	     gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
63265796c8dcSSimon Schubert     case PACKET_UNKNOWN:
63275796c8dcSSimon Schubert       return 0;
63285796c8dcSSimon Schubert     default:
63295796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
63305796c8dcSSimon Schubert     }
63315796c8dcSSimon Schubert }
63325796c8dcSSimon Schubert 
63335796c8dcSSimon Schubert /* Store register REGNUM, or all registers if REGNUM == -1, from the
63345796c8dcSSimon Schubert    contents of the register cache buffer.  FIXME: ignores errors.  */
63355796c8dcSSimon Schubert 
63365796c8dcSSimon Schubert static void
store_registers_using_G(const struct regcache * regcache)63375796c8dcSSimon Schubert store_registers_using_G (const struct regcache *regcache)
63385796c8dcSSimon Schubert {
63395796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
63405796c8dcSSimon Schubert   struct remote_arch_state *rsa = get_remote_arch_state ();
63415796c8dcSSimon Schubert   gdb_byte *regs;
63425796c8dcSSimon Schubert   char *p;
63435796c8dcSSimon Schubert 
63445796c8dcSSimon Schubert   /* Extract all the registers in the regcache copying them into a
63455796c8dcSSimon Schubert      local buffer.  */
63465796c8dcSSimon Schubert   {
63475796c8dcSSimon Schubert     int i;
6348cf7f2e2dSJohn Marino 
63495796c8dcSSimon Schubert     regs = alloca (rsa->sizeof_g_packet);
63505796c8dcSSimon Schubert     memset (regs, 0, rsa->sizeof_g_packet);
63515796c8dcSSimon Schubert     for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
63525796c8dcSSimon Schubert       {
63535796c8dcSSimon Schubert 	struct packet_reg *r = &rsa->regs[i];
6354cf7f2e2dSJohn Marino 
63555796c8dcSSimon Schubert 	if (r->in_g_packet)
63565796c8dcSSimon Schubert 	  regcache_raw_collect (regcache, r->regnum, regs + r->offset);
63575796c8dcSSimon Schubert       }
63585796c8dcSSimon Schubert   }
63595796c8dcSSimon Schubert 
63605796c8dcSSimon Schubert   /* Command describes registers byte by byte,
63615796c8dcSSimon Schubert      each byte encoded as two hex characters.  */
63625796c8dcSSimon Schubert   p = rs->buf;
63635796c8dcSSimon Schubert   *p++ = 'G';
63645796c8dcSSimon Schubert   /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
63655796c8dcSSimon Schubert      updated.  */
63665796c8dcSSimon Schubert   bin2hex (regs, p, rsa->sizeof_g_packet);
63675796c8dcSSimon Schubert   putpkt (rs->buf);
63685796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
63695796c8dcSSimon Schubert   if (packet_check_result (rs->buf) == PACKET_ERROR)
63705796c8dcSSimon Schubert     error (_("Could not write registers; remote failure reply '%s'"),
63715796c8dcSSimon Schubert 	   rs->buf);
63725796c8dcSSimon Schubert }
63735796c8dcSSimon Schubert 
63745796c8dcSSimon Schubert /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
63755796c8dcSSimon Schubert    of the register cache buffer.  FIXME: ignores errors.  */
63765796c8dcSSimon Schubert 
63775796c8dcSSimon Schubert static void
remote_store_registers(struct target_ops * ops,struct regcache * regcache,int regnum)63785796c8dcSSimon Schubert remote_store_registers (struct target_ops *ops,
63795796c8dcSSimon Schubert 			struct regcache *regcache, int regnum)
63805796c8dcSSimon Schubert {
63815796c8dcSSimon Schubert   struct remote_arch_state *rsa = get_remote_arch_state ();
63825796c8dcSSimon Schubert   int i;
63835796c8dcSSimon Schubert 
6384c50c785cSJohn Marino   set_remote_traceframe ();
63855796c8dcSSimon Schubert   set_general_thread (inferior_ptid);
63865796c8dcSSimon Schubert 
63875796c8dcSSimon Schubert   if (regnum >= 0)
63885796c8dcSSimon Schubert     {
63895796c8dcSSimon Schubert       struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
6390cf7f2e2dSJohn Marino 
63915796c8dcSSimon Schubert       gdb_assert (reg != NULL);
63925796c8dcSSimon Schubert 
63935796c8dcSSimon Schubert       /* Always prefer to store registers using the 'P' packet if
63945796c8dcSSimon Schubert 	 possible; we often change only a small number of registers.
63955796c8dcSSimon Schubert 	 Sometimes we change a larger number; we'd need help from a
63965796c8dcSSimon Schubert 	 higher layer to know to use 'G'.  */
63975796c8dcSSimon Schubert       if (store_register_using_P (regcache, reg))
63985796c8dcSSimon Schubert 	return;
63995796c8dcSSimon Schubert 
64005796c8dcSSimon Schubert       /* For now, don't complain if we have no way to write the
64015796c8dcSSimon Schubert 	 register.  GDB loses track of unavailable registers too
64025796c8dcSSimon Schubert 	 easily.  Some day, this may be an error.  We don't have
64035796c8dcSSimon Schubert 	 any way to read the register, either...  */
64045796c8dcSSimon Schubert       if (!reg->in_g_packet)
64055796c8dcSSimon Schubert 	return;
64065796c8dcSSimon Schubert 
64075796c8dcSSimon Schubert       store_registers_using_G (regcache);
64085796c8dcSSimon Schubert       return;
64095796c8dcSSimon Schubert     }
64105796c8dcSSimon Schubert 
64115796c8dcSSimon Schubert   store_registers_using_G (regcache);
64125796c8dcSSimon Schubert 
64135796c8dcSSimon Schubert   for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
64145796c8dcSSimon Schubert     if (!rsa->regs[i].in_g_packet)
64155796c8dcSSimon Schubert       if (!store_register_using_P (regcache, &rsa->regs[i]))
64165796c8dcSSimon Schubert 	/* See above for why we do not issue an error here.  */
64175796c8dcSSimon Schubert 	continue;
64185796c8dcSSimon Schubert }
64195796c8dcSSimon Schubert 
64205796c8dcSSimon Schubert 
64215796c8dcSSimon Schubert /* Return the number of hex digits in num.  */
64225796c8dcSSimon Schubert 
64235796c8dcSSimon Schubert static int
hexnumlen(ULONGEST num)64245796c8dcSSimon Schubert hexnumlen (ULONGEST num)
64255796c8dcSSimon Schubert {
64265796c8dcSSimon Schubert   int i;
64275796c8dcSSimon Schubert 
64285796c8dcSSimon Schubert   for (i = 0; num != 0; i++)
64295796c8dcSSimon Schubert     num >>= 4;
64305796c8dcSSimon Schubert 
64315796c8dcSSimon Schubert   return max (i, 1);
64325796c8dcSSimon Schubert }
64335796c8dcSSimon Schubert 
64345796c8dcSSimon Schubert /* Set BUF to the minimum number of hex digits representing NUM.  */
64355796c8dcSSimon Schubert 
64365796c8dcSSimon Schubert static int
hexnumstr(char * buf,ULONGEST num)64375796c8dcSSimon Schubert hexnumstr (char *buf, ULONGEST num)
64385796c8dcSSimon Schubert {
64395796c8dcSSimon Schubert   int len = hexnumlen (num);
6440cf7f2e2dSJohn Marino 
64415796c8dcSSimon Schubert   return hexnumnstr (buf, num, len);
64425796c8dcSSimon Schubert }
64435796c8dcSSimon Schubert 
64445796c8dcSSimon Schubert 
64455796c8dcSSimon Schubert /* Set BUF to the hex digits representing NUM, padded to WIDTH characters.  */
64465796c8dcSSimon Schubert 
64475796c8dcSSimon Schubert static int
hexnumnstr(char * buf,ULONGEST num,int width)64485796c8dcSSimon Schubert hexnumnstr (char *buf, ULONGEST num, int width)
64495796c8dcSSimon Schubert {
64505796c8dcSSimon Schubert   int i;
64515796c8dcSSimon Schubert 
64525796c8dcSSimon Schubert   buf[width] = '\0';
64535796c8dcSSimon Schubert 
64545796c8dcSSimon Schubert   for (i = width - 1; i >= 0; i--)
64555796c8dcSSimon Schubert     {
64565796c8dcSSimon Schubert       buf[i] = "0123456789abcdef"[(num & 0xf)];
64575796c8dcSSimon Schubert       num >>= 4;
64585796c8dcSSimon Schubert     }
64595796c8dcSSimon Schubert 
64605796c8dcSSimon Schubert   return width;
64615796c8dcSSimon Schubert }
64625796c8dcSSimon Schubert 
64635796c8dcSSimon Schubert /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits.  */
64645796c8dcSSimon Schubert 
64655796c8dcSSimon Schubert static CORE_ADDR
remote_address_masked(CORE_ADDR addr)64665796c8dcSSimon Schubert remote_address_masked (CORE_ADDR addr)
64675796c8dcSSimon Schubert {
6468ef5ccd6cSJohn Marino   unsigned int address_size = remote_address_size;
6469cf7f2e2dSJohn Marino 
64705796c8dcSSimon Schubert   /* If "remoteaddresssize" was not set, default to target address size.  */
64715796c8dcSSimon Schubert   if (!address_size)
6472ef5ccd6cSJohn Marino     address_size = gdbarch_addr_bit (target_gdbarch ());
64735796c8dcSSimon Schubert 
64745796c8dcSSimon Schubert   if (address_size > 0
64755796c8dcSSimon Schubert       && address_size < (sizeof (ULONGEST) * 8))
64765796c8dcSSimon Schubert     {
64775796c8dcSSimon Schubert       /* Only create a mask when that mask can safely be constructed
64785796c8dcSSimon Schubert          in a ULONGEST variable.  */
64795796c8dcSSimon Schubert       ULONGEST mask = 1;
6480cf7f2e2dSJohn Marino 
64815796c8dcSSimon Schubert       mask = (mask << address_size) - 1;
64825796c8dcSSimon Schubert       addr &= mask;
64835796c8dcSSimon Schubert     }
64845796c8dcSSimon Schubert   return addr;
64855796c8dcSSimon Schubert }
64865796c8dcSSimon Schubert 
64875796c8dcSSimon Schubert /* Convert BUFFER, binary data at least LEN bytes long, into escaped
64885796c8dcSSimon Schubert    binary data in OUT_BUF.  Set *OUT_LEN to the length of the data
64895796c8dcSSimon Schubert    encoded in OUT_BUF, and return the number of bytes in OUT_BUF
64905796c8dcSSimon Schubert    (which may be more than *OUT_LEN due to escape characters).  The
64915796c8dcSSimon Schubert    total number of bytes in the output buffer will be at most
64925796c8dcSSimon Schubert    OUT_MAXLEN.  */
64935796c8dcSSimon Schubert 
64945796c8dcSSimon Schubert static int
remote_escape_output(const gdb_byte * buffer,int len,gdb_byte * out_buf,int * out_len,int out_maxlen)64955796c8dcSSimon Schubert remote_escape_output (const gdb_byte *buffer, int len,
64965796c8dcSSimon Schubert 		      gdb_byte *out_buf, int *out_len,
64975796c8dcSSimon Schubert 		      int out_maxlen)
64985796c8dcSSimon Schubert {
64995796c8dcSSimon Schubert   int input_index, output_index;
65005796c8dcSSimon Schubert 
65015796c8dcSSimon Schubert   output_index = 0;
65025796c8dcSSimon Schubert   for (input_index = 0; input_index < len; input_index++)
65035796c8dcSSimon Schubert     {
65045796c8dcSSimon Schubert       gdb_byte b = buffer[input_index];
65055796c8dcSSimon Schubert 
65065796c8dcSSimon Schubert       if (b == '$' || b == '#' || b == '}')
65075796c8dcSSimon Schubert 	{
65085796c8dcSSimon Schubert 	  /* These must be escaped.  */
65095796c8dcSSimon Schubert 	  if (output_index + 2 > out_maxlen)
65105796c8dcSSimon Schubert 	    break;
65115796c8dcSSimon Schubert 	  out_buf[output_index++] = '}';
65125796c8dcSSimon Schubert 	  out_buf[output_index++] = b ^ 0x20;
65135796c8dcSSimon Schubert 	}
65145796c8dcSSimon Schubert       else
65155796c8dcSSimon Schubert 	{
65165796c8dcSSimon Schubert 	  if (output_index + 1 > out_maxlen)
65175796c8dcSSimon Schubert 	    break;
65185796c8dcSSimon Schubert 	  out_buf[output_index++] = b;
65195796c8dcSSimon Schubert 	}
65205796c8dcSSimon Schubert     }
65215796c8dcSSimon Schubert 
65225796c8dcSSimon Schubert   *out_len = input_index;
65235796c8dcSSimon Schubert   return output_index;
65245796c8dcSSimon Schubert }
65255796c8dcSSimon Schubert 
65265796c8dcSSimon Schubert /* Convert BUFFER, escaped data LEN bytes long, into binary data
65275796c8dcSSimon Schubert    in OUT_BUF.  Return the number of bytes written to OUT_BUF.
65285796c8dcSSimon Schubert    Raise an error if the total number of bytes exceeds OUT_MAXLEN.
65295796c8dcSSimon Schubert 
65305796c8dcSSimon Schubert    This function reverses remote_escape_output.  It allows more
65315796c8dcSSimon Schubert    escaped characters than that function does, in particular because
65325796c8dcSSimon Schubert    '*' must be escaped to avoid the run-length encoding processing
65335796c8dcSSimon Schubert    in reading packets.  */
65345796c8dcSSimon Schubert 
65355796c8dcSSimon Schubert static int
remote_unescape_input(const gdb_byte * buffer,int len,gdb_byte * out_buf,int out_maxlen)65365796c8dcSSimon Schubert remote_unescape_input (const gdb_byte *buffer, int len,
65375796c8dcSSimon Schubert 		       gdb_byte *out_buf, int out_maxlen)
65385796c8dcSSimon Schubert {
65395796c8dcSSimon Schubert   int input_index, output_index;
65405796c8dcSSimon Schubert   int escaped;
65415796c8dcSSimon Schubert 
65425796c8dcSSimon Schubert   output_index = 0;
65435796c8dcSSimon Schubert   escaped = 0;
65445796c8dcSSimon Schubert   for (input_index = 0; input_index < len; input_index++)
65455796c8dcSSimon Schubert     {
65465796c8dcSSimon Schubert       gdb_byte b = buffer[input_index];
65475796c8dcSSimon Schubert 
65485796c8dcSSimon Schubert       if (output_index + 1 > out_maxlen)
65495796c8dcSSimon Schubert 	{
65505796c8dcSSimon Schubert 	  warning (_("Received too much data from remote target;"
65515796c8dcSSimon Schubert 		     " ignoring overflow."));
65525796c8dcSSimon Schubert 	  return output_index;
65535796c8dcSSimon Schubert 	}
65545796c8dcSSimon Schubert 
65555796c8dcSSimon Schubert       if (escaped)
65565796c8dcSSimon Schubert 	{
65575796c8dcSSimon Schubert 	  out_buf[output_index++] = b ^ 0x20;
65585796c8dcSSimon Schubert 	  escaped = 0;
65595796c8dcSSimon Schubert 	}
65605796c8dcSSimon Schubert       else if (b == '}')
65615796c8dcSSimon Schubert 	escaped = 1;
65625796c8dcSSimon Schubert       else
65635796c8dcSSimon Schubert 	out_buf[output_index++] = b;
65645796c8dcSSimon Schubert     }
65655796c8dcSSimon Schubert 
65665796c8dcSSimon Schubert   if (escaped)
65675796c8dcSSimon Schubert     error (_("Unmatched escape character in target response."));
65685796c8dcSSimon Schubert 
65695796c8dcSSimon Schubert   return output_index;
65705796c8dcSSimon Schubert }
65715796c8dcSSimon Schubert 
65725796c8dcSSimon Schubert /* Determine whether the remote target supports binary downloading.
65735796c8dcSSimon Schubert    This is accomplished by sending a no-op memory write of zero length
65745796c8dcSSimon Schubert    to the target at the specified address. It does not suffice to send
65755796c8dcSSimon Schubert    the whole packet, since many stubs strip the eighth bit and
65765796c8dcSSimon Schubert    subsequently compute a wrong checksum, which causes real havoc with
65775796c8dcSSimon Schubert    remote_write_bytes.
65785796c8dcSSimon Schubert 
65795796c8dcSSimon Schubert    NOTE: This can still lose if the serial line is not eight-bit
65805796c8dcSSimon Schubert    clean.  In cases like this, the user should clear "remote
65815796c8dcSSimon Schubert    X-packet".  */
65825796c8dcSSimon Schubert 
65835796c8dcSSimon Schubert static void
check_binary_download(CORE_ADDR addr)65845796c8dcSSimon Schubert check_binary_download (CORE_ADDR addr)
65855796c8dcSSimon Schubert {
65865796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
65875796c8dcSSimon Schubert 
65885796c8dcSSimon Schubert   switch (remote_protocol_packets[PACKET_X].support)
65895796c8dcSSimon Schubert     {
65905796c8dcSSimon Schubert     case PACKET_DISABLE:
65915796c8dcSSimon Schubert       break;
65925796c8dcSSimon Schubert     case PACKET_ENABLE:
65935796c8dcSSimon Schubert       break;
65945796c8dcSSimon Schubert     case PACKET_SUPPORT_UNKNOWN:
65955796c8dcSSimon Schubert       {
65965796c8dcSSimon Schubert 	char *p;
65975796c8dcSSimon Schubert 
65985796c8dcSSimon Schubert 	p = rs->buf;
65995796c8dcSSimon Schubert 	*p++ = 'X';
66005796c8dcSSimon Schubert 	p += hexnumstr (p, (ULONGEST) addr);
66015796c8dcSSimon Schubert 	*p++ = ',';
66025796c8dcSSimon Schubert 	p += hexnumstr (p, (ULONGEST) 0);
66035796c8dcSSimon Schubert 	*p++ = ':';
66045796c8dcSSimon Schubert 	*p = '\0';
66055796c8dcSSimon Schubert 
66065796c8dcSSimon Schubert 	putpkt_binary (rs->buf, (int) (p - rs->buf));
66075796c8dcSSimon Schubert 	getpkt (&rs->buf, &rs->buf_size, 0);
66085796c8dcSSimon Schubert 
66095796c8dcSSimon Schubert 	if (rs->buf[0] == '\0')
66105796c8dcSSimon Schubert 	  {
66115796c8dcSSimon Schubert 	    if (remote_debug)
66125796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog,
6613c50c785cSJohn Marino 				  "binary downloading NOT "
6614c50c785cSJohn Marino 				  "supported by target\n");
66155796c8dcSSimon Schubert 	    remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
66165796c8dcSSimon Schubert 	  }
66175796c8dcSSimon Schubert 	else
66185796c8dcSSimon Schubert 	  {
66195796c8dcSSimon Schubert 	    if (remote_debug)
66205796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog,
6621a45ae5f8SJohn Marino 				  "binary downloading supported by target\n");
66225796c8dcSSimon Schubert 	    remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
66235796c8dcSSimon Schubert 	  }
66245796c8dcSSimon Schubert 	break;
66255796c8dcSSimon Schubert       }
66265796c8dcSSimon Schubert     }
66275796c8dcSSimon Schubert }
66285796c8dcSSimon Schubert 
66295796c8dcSSimon Schubert /* Write memory data directly to the remote machine.
66305796c8dcSSimon Schubert    This does not inform the data cache; the data cache uses this.
66315796c8dcSSimon Schubert    HEADER is the starting part of the packet.
66325796c8dcSSimon Schubert    MEMADDR is the address in the remote memory space.
66335796c8dcSSimon Schubert    MYADDR is the address of the buffer in our space.
66345796c8dcSSimon Schubert    LEN is the number of bytes.
66355796c8dcSSimon Schubert    PACKET_FORMAT should be either 'X' or 'M', and indicates if we
66365796c8dcSSimon Schubert    should send data as binary ('X'), or hex-encoded ('M').
66375796c8dcSSimon Schubert 
66385796c8dcSSimon Schubert    The function creates packet of the form
66395796c8dcSSimon Schubert        <HEADER><ADDRESS>,<LENGTH>:<DATA>
66405796c8dcSSimon Schubert 
66415796c8dcSSimon Schubert    where encoding of <DATA> is termined by PACKET_FORMAT.
66425796c8dcSSimon Schubert 
66435796c8dcSSimon Schubert    If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
66445796c8dcSSimon Schubert    are omitted.
66455796c8dcSSimon Schubert 
66465796c8dcSSimon Schubert    Returns the number of bytes transferred, or 0 (setting errno) for
66475796c8dcSSimon Schubert    error.  Only transfer a single packet.  */
66485796c8dcSSimon Schubert 
66495796c8dcSSimon Schubert static int
remote_write_bytes_aux(const char * header,CORE_ADDR memaddr,const gdb_byte * myaddr,ssize_t len,char packet_format,int use_length)66505796c8dcSSimon Schubert remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
6651ef5ccd6cSJohn Marino 			const gdb_byte *myaddr, ssize_t len,
66525796c8dcSSimon Schubert 			char packet_format, int use_length)
66535796c8dcSSimon Schubert {
66545796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
66555796c8dcSSimon Schubert   char *p;
66565796c8dcSSimon Schubert   char *plen = NULL;
66575796c8dcSSimon Schubert   int plenlen = 0;
66585796c8dcSSimon Schubert   int todo;
66595796c8dcSSimon Schubert   int nr_bytes;
66605796c8dcSSimon Schubert   int payload_size;
66615796c8dcSSimon Schubert   int payload_length;
66625796c8dcSSimon Schubert   int header_length;
66635796c8dcSSimon Schubert 
66645796c8dcSSimon Schubert   if (packet_format != 'X' && packet_format != 'M')
66655796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
6666c50c785cSJohn Marino 		    _("remote_write_bytes_aux: bad packet format"));
66675796c8dcSSimon Schubert 
66685796c8dcSSimon Schubert   if (len <= 0)
66695796c8dcSSimon Schubert     return 0;
66705796c8dcSSimon Schubert 
66715796c8dcSSimon Schubert   payload_size = get_memory_write_packet_size ();
66725796c8dcSSimon Schubert 
66735796c8dcSSimon Schubert   /* The packet buffer will be large enough for the payload;
66745796c8dcSSimon Schubert      get_memory_packet_size ensures this.  */
66755796c8dcSSimon Schubert   rs->buf[0] = '\0';
66765796c8dcSSimon Schubert 
66775796c8dcSSimon Schubert   /* Compute the size of the actual payload by subtracting out the
6678c50c785cSJohn Marino      packet header and footer overhead: "$M<memaddr>,<len>:...#nn".  */
6679c50c785cSJohn Marino 
66805796c8dcSSimon Schubert   payload_size -= strlen ("$,:#NN");
66815796c8dcSSimon Schubert   if (!use_length)
66825796c8dcSSimon Schubert     /* The comma won't be used.  */
66835796c8dcSSimon Schubert     payload_size += 1;
66845796c8dcSSimon Schubert   header_length = strlen (header);
66855796c8dcSSimon Schubert   payload_size -= header_length;
66865796c8dcSSimon Schubert   payload_size -= hexnumlen (memaddr);
66875796c8dcSSimon Schubert 
66885796c8dcSSimon Schubert   /* Construct the packet excluding the data: "<header><memaddr>,<len>:".  */
66895796c8dcSSimon Schubert 
66905796c8dcSSimon Schubert   strcat (rs->buf, header);
66915796c8dcSSimon Schubert   p = rs->buf + strlen (header);
66925796c8dcSSimon Schubert 
66935796c8dcSSimon Schubert   /* Compute a best guess of the number of bytes actually transfered.  */
66945796c8dcSSimon Schubert   if (packet_format == 'X')
66955796c8dcSSimon Schubert     {
66965796c8dcSSimon Schubert       /* Best guess at number of bytes that will fit.  */
66975796c8dcSSimon Schubert       todo = min (len, payload_size);
66985796c8dcSSimon Schubert       if (use_length)
66995796c8dcSSimon Schubert 	payload_size -= hexnumlen (todo);
67005796c8dcSSimon Schubert       todo = min (todo, payload_size);
67015796c8dcSSimon Schubert     }
67025796c8dcSSimon Schubert   else
67035796c8dcSSimon Schubert     {
67045796c8dcSSimon Schubert       /* Num bytes that will fit.  */
67055796c8dcSSimon Schubert       todo = min (len, payload_size / 2);
67065796c8dcSSimon Schubert       if (use_length)
67075796c8dcSSimon Schubert 	payload_size -= hexnumlen (todo);
67085796c8dcSSimon Schubert       todo = min (todo, payload_size / 2);
67095796c8dcSSimon Schubert     }
67105796c8dcSSimon Schubert 
67115796c8dcSSimon Schubert   if (todo <= 0)
67125796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
6713a45ae5f8SJohn Marino 		    _("minimum packet size too small to write data"));
67145796c8dcSSimon Schubert 
67155796c8dcSSimon Schubert   /* If we already need another packet, then try to align the end
67165796c8dcSSimon Schubert      of this packet to a useful boundary.  */
67175796c8dcSSimon Schubert   if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len)
67185796c8dcSSimon Schubert     todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
67195796c8dcSSimon Schubert 
67205796c8dcSSimon Schubert   /* Append "<memaddr>".  */
67215796c8dcSSimon Schubert   memaddr = remote_address_masked (memaddr);
67225796c8dcSSimon Schubert   p += hexnumstr (p, (ULONGEST) memaddr);
67235796c8dcSSimon Schubert 
67245796c8dcSSimon Schubert   if (use_length)
67255796c8dcSSimon Schubert     {
67265796c8dcSSimon Schubert       /* Append ",".  */
67275796c8dcSSimon Schubert       *p++ = ',';
67285796c8dcSSimon Schubert 
67295796c8dcSSimon Schubert       /* Append <len>.  Retain the location/size of <len>.  It may need to
67305796c8dcSSimon Schubert 	 be adjusted once the packet body has been created.  */
67315796c8dcSSimon Schubert       plen = p;
67325796c8dcSSimon Schubert       plenlen = hexnumstr (p, (ULONGEST) todo);
67335796c8dcSSimon Schubert       p += plenlen;
67345796c8dcSSimon Schubert     }
67355796c8dcSSimon Schubert 
67365796c8dcSSimon Schubert   /* Append ":".  */
67375796c8dcSSimon Schubert   *p++ = ':';
67385796c8dcSSimon Schubert   *p = '\0';
67395796c8dcSSimon Schubert 
67405796c8dcSSimon Schubert   /* Append the packet body.  */
67415796c8dcSSimon Schubert   if (packet_format == 'X')
67425796c8dcSSimon Schubert     {
67435796c8dcSSimon Schubert       /* Binary mode.  Send target system values byte by byte, in
67445796c8dcSSimon Schubert 	 increasing byte addresses.  Only escape certain critical
67455796c8dcSSimon Schubert 	 characters.  */
67465796c8dcSSimon Schubert       payload_length = remote_escape_output (myaddr, todo, p, &nr_bytes,
67475796c8dcSSimon Schubert 					     payload_size);
67485796c8dcSSimon Schubert 
67495796c8dcSSimon Schubert       /* If not all TODO bytes fit, then we'll need another packet.  Make
67505796c8dcSSimon Schubert 	 a second try to keep the end of the packet aligned.  Don't do
67515796c8dcSSimon Schubert 	 this if the packet is tiny.  */
67525796c8dcSSimon Schubert       if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES)
67535796c8dcSSimon Schubert 	{
67545796c8dcSSimon Schubert 	  int new_nr_bytes;
67555796c8dcSSimon Schubert 
67565796c8dcSSimon Schubert 	  new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1))
67575796c8dcSSimon Schubert 			  - memaddr);
67585796c8dcSSimon Schubert 	  if (new_nr_bytes != nr_bytes)
67595796c8dcSSimon Schubert 	    payload_length = remote_escape_output (myaddr, new_nr_bytes,
67605796c8dcSSimon Schubert 						   p, &nr_bytes,
67615796c8dcSSimon Schubert 						   payload_size);
67625796c8dcSSimon Schubert 	}
67635796c8dcSSimon Schubert 
67645796c8dcSSimon Schubert       p += payload_length;
67655796c8dcSSimon Schubert       if (use_length && nr_bytes < todo)
67665796c8dcSSimon Schubert 	{
67675796c8dcSSimon Schubert 	  /* Escape chars have filled up the buffer prematurely,
67685796c8dcSSimon Schubert 	     and we have actually sent fewer bytes than planned.
67695796c8dcSSimon Schubert 	     Fix-up the length field of the packet.  Use the same
67705796c8dcSSimon Schubert 	     number of characters as before.  */
67715796c8dcSSimon Schubert 	  plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
67725796c8dcSSimon Schubert 	  *plen = ':';  /* overwrite \0 from hexnumnstr() */
67735796c8dcSSimon Schubert 	}
67745796c8dcSSimon Schubert     }
67755796c8dcSSimon Schubert   else
67765796c8dcSSimon Schubert     {
67775796c8dcSSimon Schubert       /* Normal mode: Send target system values byte by byte, in
67785796c8dcSSimon Schubert 	 increasing byte addresses.  Each byte is encoded as a two hex
67795796c8dcSSimon Schubert 	 value.  */
67805796c8dcSSimon Schubert       nr_bytes = bin2hex (myaddr, p, todo);
67815796c8dcSSimon Schubert       p += 2 * nr_bytes;
67825796c8dcSSimon Schubert     }
67835796c8dcSSimon Schubert 
67845796c8dcSSimon Schubert   putpkt_binary (rs->buf, (int) (p - rs->buf));
67855796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
67865796c8dcSSimon Schubert 
67875796c8dcSSimon Schubert   if (rs->buf[0] == 'E')
67885796c8dcSSimon Schubert     {
67895796c8dcSSimon Schubert       /* There is no correspondance between what the remote protocol
67905796c8dcSSimon Schubert 	 uses for errors and errno codes.  We would like a cleaner way
67915796c8dcSSimon Schubert 	 of representing errors (big enough to include errno codes,
67925796c8dcSSimon Schubert 	 bfd_error codes, and others).  But for now just return EIO.  */
67935796c8dcSSimon Schubert       errno = EIO;
67945796c8dcSSimon Schubert       return 0;
67955796c8dcSSimon Schubert     }
67965796c8dcSSimon Schubert 
67975796c8dcSSimon Schubert   /* Return NR_BYTES, not TODO, in case escape chars caused us to send
67985796c8dcSSimon Schubert      fewer bytes than we'd planned.  */
67995796c8dcSSimon Schubert   return nr_bytes;
68005796c8dcSSimon Schubert }
68015796c8dcSSimon Schubert 
68025796c8dcSSimon Schubert /* Write memory data directly to the remote machine.
68035796c8dcSSimon Schubert    This does not inform the data cache; the data cache uses this.
68045796c8dcSSimon Schubert    MEMADDR is the address in the remote memory space.
68055796c8dcSSimon Schubert    MYADDR is the address of the buffer in our space.
68065796c8dcSSimon Schubert    LEN is the number of bytes.
68075796c8dcSSimon Schubert 
68085796c8dcSSimon Schubert    Returns number of bytes transferred, or 0 (setting errno) for
68095796c8dcSSimon Schubert    error.  Only transfer a single packet.  */
68105796c8dcSSimon Schubert 
6811c50c785cSJohn Marino static int
remote_write_bytes(CORE_ADDR memaddr,const gdb_byte * myaddr,ssize_t len)6812ef5ccd6cSJohn Marino remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
68135796c8dcSSimon Schubert {
68145796c8dcSSimon Schubert   char *packet_format = 0;
68155796c8dcSSimon Schubert 
68165796c8dcSSimon Schubert   /* Check whether the target supports binary download.  */
68175796c8dcSSimon Schubert   check_binary_download (memaddr);
68185796c8dcSSimon Schubert 
68195796c8dcSSimon Schubert   switch (remote_protocol_packets[PACKET_X].support)
68205796c8dcSSimon Schubert     {
68215796c8dcSSimon Schubert     case PACKET_ENABLE:
68225796c8dcSSimon Schubert       packet_format = "X";
68235796c8dcSSimon Schubert       break;
68245796c8dcSSimon Schubert     case PACKET_DISABLE:
68255796c8dcSSimon Schubert       packet_format = "M";
68265796c8dcSSimon Schubert       break;
68275796c8dcSSimon Schubert     case PACKET_SUPPORT_UNKNOWN:
68285796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__,
68295796c8dcSSimon Schubert 		      _("remote_write_bytes: bad internal state"));
68305796c8dcSSimon Schubert     default:
68315796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__, _("bad switch"));
68325796c8dcSSimon Schubert     }
68335796c8dcSSimon Schubert 
68345796c8dcSSimon Schubert   return remote_write_bytes_aux (packet_format,
68355796c8dcSSimon Schubert 				 memaddr, myaddr, len, packet_format[0], 1);
68365796c8dcSSimon Schubert }
68375796c8dcSSimon Schubert 
68385796c8dcSSimon Schubert /* Read memory data directly from the remote machine.
68395796c8dcSSimon Schubert    This does not use the data cache; the data cache uses this.
68405796c8dcSSimon Schubert    MEMADDR is the address in the remote memory space.
68415796c8dcSSimon Schubert    MYADDR is the address of the buffer in our space.
68425796c8dcSSimon Schubert    LEN is the number of bytes.
68435796c8dcSSimon Schubert 
68445796c8dcSSimon Schubert    Returns number of bytes transferred, or 0 for error.  */
68455796c8dcSSimon Schubert 
6846c50c785cSJohn Marino static int
remote_read_bytes(CORE_ADDR memaddr,gdb_byte * myaddr,int len)68475796c8dcSSimon Schubert remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
68485796c8dcSSimon Schubert {
68495796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
68505796c8dcSSimon Schubert   int max_buf_size;		/* Max size of packet output buffer.  */
6851c50c785cSJohn Marino   char *p;
6852c50c785cSJohn Marino   int todo;
6853c50c785cSJohn Marino   int i;
68545796c8dcSSimon Schubert 
68555796c8dcSSimon Schubert   if (len <= 0)
68565796c8dcSSimon Schubert     return 0;
68575796c8dcSSimon Schubert 
68585796c8dcSSimon Schubert   max_buf_size = get_memory_read_packet_size ();
68595796c8dcSSimon Schubert   /* The packet buffer will be large enough for the payload;
68605796c8dcSSimon Schubert      get_memory_packet_size ensures this.  */
68615796c8dcSSimon Schubert 
6862c50c785cSJohn Marino   /* Number if bytes that will fit.  */
6863c50c785cSJohn Marino   todo = min (len, max_buf_size / 2);
68645796c8dcSSimon Schubert 
6865c50c785cSJohn Marino   /* Construct "m"<memaddr>","<len>".  */
68665796c8dcSSimon Schubert   memaddr = remote_address_masked (memaddr);
68675796c8dcSSimon Schubert   p = rs->buf;
68685796c8dcSSimon Schubert   *p++ = 'm';
68695796c8dcSSimon Schubert   p += hexnumstr (p, (ULONGEST) memaddr);
68705796c8dcSSimon Schubert   *p++ = ',';
68715796c8dcSSimon Schubert   p += hexnumstr (p, (ULONGEST) todo);
68725796c8dcSSimon Schubert   *p = '\0';
68735796c8dcSSimon Schubert   putpkt (rs->buf);
68745796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
68755796c8dcSSimon Schubert   if (rs->buf[0] == 'E'
68765796c8dcSSimon Schubert       && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
68775796c8dcSSimon Schubert       && rs->buf[3] == '\0')
68785796c8dcSSimon Schubert     {
6879c50c785cSJohn Marino       /* There is no correspondance between what the remote protocol
6880c50c785cSJohn Marino 	 uses for errors and errno codes.  We would like a cleaner way
6881c50c785cSJohn Marino 	 of representing errors (big enough to include errno codes,
6882c50c785cSJohn Marino 	 bfd_error codes, and others).  But for now just return
6883c50c785cSJohn Marino 	 EIO.  */
68845796c8dcSSimon Schubert       errno = EIO;
68855796c8dcSSimon Schubert       return 0;
68865796c8dcSSimon Schubert     }
6887c50c785cSJohn Marino   /* Reply describes memory byte by byte, each byte encoded as two hex
6888c50c785cSJohn Marino      characters.  */
68895796c8dcSSimon Schubert   p = rs->buf;
6890c50c785cSJohn Marino   i = hex2bin (p, myaddr, todo);
6891c50c785cSJohn Marino   /* Return what we have.  Let higher layers handle partial reads.  */
6892c50c785cSJohn Marino   return i;
68935796c8dcSSimon Schubert }
68945796c8dcSSimon Schubert 
68955796c8dcSSimon Schubert 
68965796c8dcSSimon Schubert /* Read or write LEN bytes from inferior memory at MEMADDR,
68975796c8dcSSimon Schubert    transferring to or from debugger address BUFFER.  Write to inferior
68985796c8dcSSimon Schubert    if SHOULD_WRITE is nonzero.  Returns length of data written or
68995796c8dcSSimon Schubert    read; 0 for error.  TARGET is unused.  */
69005796c8dcSSimon Schubert 
69015796c8dcSSimon Schubert static int
remote_xfer_memory(CORE_ADDR mem_addr,gdb_byte * buffer,int mem_len,int should_write,struct mem_attrib * attrib,struct target_ops * target)69025796c8dcSSimon Schubert remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len,
69035796c8dcSSimon Schubert 		    int should_write, struct mem_attrib *attrib,
69045796c8dcSSimon Schubert 		    struct target_ops *target)
69055796c8dcSSimon Schubert {
69065796c8dcSSimon Schubert   int res;
69075796c8dcSSimon Schubert 
6908c50c785cSJohn Marino   set_remote_traceframe ();
69095796c8dcSSimon Schubert   set_general_thread (inferior_ptid);
69105796c8dcSSimon Schubert 
69115796c8dcSSimon Schubert   if (should_write)
69125796c8dcSSimon Schubert     res = remote_write_bytes (mem_addr, buffer, mem_len);
69135796c8dcSSimon Schubert   else
69145796c8dcSSimon Schubert     res = remote_read_bytes (mem_addr, buffer, mem_len);
69155796c8dcSSimon Schubert 
69165796c8dcSSimon Schubert   return res;
69175796c8dcSSimon Schubert }
69185796c8dcSSimon Schubert 
69195796c8dcSSimon Schubert /* Sends a packet with content determined by the printf format string
69205796c8dcSSimon Schubert    FORMAT and the remaining arguments, then gets the reply.  Returns
69215796c8dcSSimon Schubert    whether the packet was a success, a failure, or unknown.  */
69225796c8dcSSimon Schubert 
69235796c8dcSSimon Schubert static enum packet_result
remote_send_printf(const char * format,...)69245796c8dcSSimon Schubert remote_send_printf (const char *format, ...)
69255796c8dcSSimon Schubert {
69265796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
69275796c8dcSSimon Schubert   int max_size = get_remote_packet_size ();
69285796c8dcSSimon Schubert   va_list ap;
6929cf7f2e2dSJohn Marino 
69305796c8dcSSimon Schubert   va_start (ap, format);
69315796c8dcSSimon Schubert 
69325796c8dcSSimon Schubert   rs->buf[0] = '\0';
69335796c8dcSSimon Schubert   if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
6934c50c785cSJohn Marino     internal_error (__FILE__, __LINE__, _("Too long remote packet."));
69355796c8dcSSimon Schubert 
69365796c8dcSSimon Schubert   if (putpkt (rs->buf) < 0)
69375796c8dcSSimon Schubert     error (_("Communication problem with target."));
69385796c8dcSSimon Schubert 
69395796c8dcSSimon Schubert   rs->buf[0] = '\0';
69405796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
69415796c8dcSSimon Schubert 
69425796c8dcSSimon Schubert   return packet_check_result (rs->buf);
69435796c8dcSSimon Schubert }
69445796c8dcSSimon Schubert 
69455796c8dcSSimon Schubert static void
restore_remote_timeout(void * p)69465796c8dcSSimon Schubert restore_remote_timeout (void *p)
69475796c8dcSSimon Schubert {
69485796c8dcSSimon Schubert   int value = *(int *)p;
6949cf7f2e2dSJohn Marino 
69505796c8dcSSimon Schubert   remote_timeout = value;
69515796c8dcSSimon Schubert }
69525796c8dcSSimon Schubert 
69535796c8dcSSimon Schubert /* Flash writing can take quite some time.  We'll set
69545796c8dcSSimon Schubert    effectively infinite timeout for flash operations.
69555796c8dcSSimon Schubert    In future, we'll need to decide on a better approach.  */
69565796c8dcSSimon Schubert static const int remote_flash_timeout = 1000;
69575796c8dcSSimon Schubert 
69585796c8dcSSimon Schubert static void
remote_flash_erase(struct target_ops * ops,ULONGEST address,LONGEST length)69595796c8dcSSimon Schubert remote_flash_erase (struct target_ops *ops,
69605796c8dcSSimon Schubert                     ULONGEST address, LONGEST length)
69615796c8dcSSimon Schubert {
6962ef5ccd6cSJohn Marino   int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
69635796c8dcSSimon Schubert   int saved_remote_timeout = remote_timeout;
69645796c8dcSSimon Schubert   enum packet_result ret;
69655796c8dcSSimon Schubert   struct cleanup *back_to = make_cleanup (restore_remote_timeout,
69665796c8dcSSimon Schubert                                           &saved_remote_timeout);
6967cf7f2e2dSJohn Marino 
69685796c8dcSSimon Schubert   remote_timeout = remote_flash_timeout;
69695796c8dcSSimon Schubert 
69705796c8dcSSimon Schubert   ret = remote_send_printf ("vFlashErase:%s,%s",
69715796c8dcSSimon Schubert 			    phex (address, addr_size),
69725796c8dcSSimon Schubert 			    phex (length, 4));
69735796c8dcSSimon Schubert   switch (ret)
69745796c8dcSSimon Schubert     {
69755796c8dcSSimon Schubert     case PACKET_UNKNOWN:
69765796c8dcSSimon Schubert       error (_("Remote target does not support flash erase"));
69775796c8dcSSimon Schubert     case PACKET_ERROR:
69785796c8dcSSimon Schubert       error (_("Error erasing flash with vFlashErase packet"));
69795796c8dcSSimon Schubert     default:
69805796c8dcSSimon Schubert       break;
69815796c8dcSSimon Schubert     }
69825796c8dcSSimon Schubert 
69835796c8dcSSimon Schubert   do_cleanups (back_to);
69845796c8dcSSimon Schubert }
69855796c8dcSSimon Schubert 
69865796c8dcSSimon Schubert static LONGEST
remote_flash_write(struct target_ops * ops,ULONGEST address,LONGEST length,const gdb_byte * data)69875796c8dcSSimon Schubert remote_flash_write (struct target_ops *ops,
69885796c8dcSSimon Schubert                     ULONGEST address, LONGEST length,
69895796c8dcSSimon Schubert                     const gdb_byte *data)
69905796c8dcSSimon Schubert {
69915796c8dcSSimon Schubert   int saved_remote_timeout = remote_timeout;
69925796c8dcSSimon Schubert   int ret;
69935796c8dcSSimon Schubert   struct cleanup *back_to = make_cleanup (restore_remote_timeout,
69945796c8dcSSimon Schubert                                           &saved_remote_timeout);
69955796c8dcSSimon Schubert 
69965796c8dcSSimon Schubert   remote_timeout = remote_flash_timeout;
69975796c8dcSSimon Schubert   ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 'X', 0);
69985796c8dcSSimon Schubert   do_cleanups (back_to);
69995796c8dcSSimon Schubert 
70005796c8dcSSimon Schubert   return ret;
70015796c8dcSSimon Schubert }
70025796c8dcSSimon Schubert 
70035796c8dcSSimon Schubert static void
remote_flash_done(struct target_ops * ops)70045796c8dcSSimon Schubert remote_flash_done (struct target_ops *ops)
70055796c8dcSSimon Schubert {
70065796c8dcSSimon Schubert   int saved_remote_timeout = remote_timeout;
70075796c8dcSSimon Schubert   int ret;
70085796c8dcSSimon Schubert   struct cleanup *back_to = make_cleanup (restore_remote_timeout,
70095796c8dcSSimon Schubert                                           &saved_remote_timeout);
70105796c8dcSSimon Schubert 
70115796c8dcSSimon Schubert   remote_timeout = remote_flash_timeout;
70125796c8dcSSimon Schubert   ret = remote_send_printf ("vFlashDone");
70135796c8dcSSimon Schubert   do_cleanups (back_to);
70145796c8dcSSimon Schubert 
70155796c8dcSSimon Schubert   switch (ret)
70165796c8dcSSimon Schubert     {
70175796c8dcSSimon Schubert     case PACKET_UNKNOWN:
70185796c8dcSSimon Schubert       error (_("Remote target does not support vFlashDone"));
70195796c8dcSSimon Schubert     case PACKET_ERROR:
70205796c8dcSSimon Schubert       error (_("Error finishing flash operation"));
70215796c8dcSSimon Schubert     default:
70225796c8dcSSimon Schubert       break;
70235796c8dcSSimon Schubert     }
70245796c8dcSSimon Schubert }
70255796c8dcSSimon Schubert 
70265796c8dcSSimon Schubert static void
remote_files_info(struct target_ops * ignore)70275796c8dcSSimon Schubert remote_files_info (struct target_ops *ignore)
70285796c8dcSSimon Schubert {
70295796c8dcSSimon Schubert   puts_filtered ("Debugging a target over a serial line.\n");
70305796c8dcSSimon Schubert }
70315796c8dcSSimon Schubert 
70325796c8dcSSimon Schubert /* Stuff for dealing with the packets which are part of this protocol.
70335796c8dcSSimon Schubert    See comment at top of file for details.  */
70345796c8dcSSimon Schubert 
70355796c8dcSSimon Schubert /* Read a single character from the remote end.  */
70365796c8dcSSimon Schubert 
70375796c8dcSSimon Schubert static int
readchar(int timeout)70385796c8dcSSimon Schubert readchar (int timeout)
70395796c8dcSSimon Schubert {
70405796c8dcSSimon Schubert   int ch;
70415796c8dcSSimon Schubert 
70425796c8dcSSimon Schubert   ch = serial_readchar (remote_desc, timeout);
70435796c8dcSSimon Schubert 
70445796c8dcSSimon Schubert   if (ch >= 0)
70455796c8dcSSimon Schubert     return ch;
70465796c8dcSSimon Schubert 
70475796c8dcSSimon Schubert   switch ((enum serial_rc) ch)
70485796c8dcSSimon Schubert     {
70495796c8dcSSimon Schubert     case SERIAL_EOF:
7050ef5ccd6cSJohn Marino       remote_unpush_target ();
7051ef5ccd6cSJohn Marino       throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
70525796c8dcSSimon Schubert       /* no return */
70535796c8dcSSimon Schubert     case SERIAL_ERROR:
7054ef5ccd6cSJohn Marino       remote_unpush_target ();
7055ef5ccd6cSJohn Marino       throw_perror_with_name (TARGET_CLOSE_ERROR,
7056ef5ccd6cSJohn Marino 			      _("Remote communication error.  "
7057c50c785cSJohn Marino 				"Target disconnected."));
70585796c8dcSSimon Schubert       /* no return */
70595796c8dcSSimon Schubert     case SERIAL_TIMEOUT:
70605796c8dcSSimon Schubert       break;
70615796c8dcSSimon Schubert     }
70625796c8dcSSimon Schubert   return ch;
70635796c8dcSSimon Schubert }
70645796c8dcSSimon Schubert 
7065ef5ccd6cSJohn Marino /* Wrapper for serial_write that closes the target and throws if
7066ef5ccd6cSJohn Marino    writing fails.  */
7067ef5ccd6cSJohn Marino 
7068ef5ccd6cSJohn Marino static void
remote_serial_write(const char * str,int len)7069ef5ccd6cSJohn Marino remote_serial_write (const char *str, int len)
7070ef5ccd6cSJohn Marino {
7071ef5ccd6cSJohn Marino   if (serial_write (remote_desc, str, len))
7072ef5ccd6cSJohn Marino     {
7073ef5ccd6cSJohn Marino       remote_unpush_target ();
7074ef5ccd6cSJohn Marino       throw_perror_with_name (TARGET_CLOSE_ERROR,
7075ef5ccd6cSJohn Marino 			      _("Remote communication error.  "
7076ef5ccd6cSJohn Marino 				"Target disconnected."));
7077ef5ccd6cSJohn Marino     }
7078ef5ccd6cSJohn Marino }
7079ef5ccd6cSJohn Marino 
70805796c8dcSSimon Schubert /* Send the command in *BUF to the remote machine, and read the reply
70815796c8dcSSimon Schubert    into *BUF.  Report an error if we get an error reply.  Resize
70825796c8dcSSimon Schubert    *BUF using xrealloc if necessary to hold the result, and update
70835796c8dcSSimon Schubert    *SIZEOF_BUF.  */
70845796c8dcSSimon Schubert 
70855796c8dcSSimon Schubert static void
remote_send(char ** buf,long * sizeof_buf)70865796c8dcSSimon Schubert remote_send (char **buf,
70875796c8dcSSimon Schubert 	     long *sizeof_buf)
70885796c8dcSSimon Schubert {
70895796c8dcSSimon Schubert   putpkt (*buf);
70905796c8dcSSimon Schubert   getpkt (buf, sizeof_buf, 0);
70915796c8dcSSimon Schubert 
70925796c8dcSSimon Schubert   if ((*buf)[0] == 'E')
70935796c8dcSSimon Schubert     error (_("Remote failure reply: %s"), *buf);
70945796c8dcSSimon Schubert }
70955796c8dcSSimon Schubert 
70965796c8dcSSimon Schubert /* Return a pointer to an xmalloc'ed string representing an escaped
70975796c8dcSSimon Schubert    version of BUF, of len N.  E.g. \n is converted to \\n, \t to \\t,
70985796c8dcSSimon Schubert    etc.  The caller is responsible for releasing the returned
70995796c8dcSSimon Schubert    memory.  */
71005796c8dcSSimon Schubert 
71015796c8dcSSimon Schubert static char *
escape_buffer(const char * buf,int n)71025796c8dcSSimon Schubert escape_buffer (const char *buf, int n)
71035796c8dcSSimon Schubert {
71045796c8dcSSimon Schubert   struct cleanup *old_chain;
71055796c8dcSSimon Schubert   struct ui_file *stb;
71065796c8dcSSimon Schubert   char *str;
71075796c8dcSSimon Schubert 
71085796c8dcSSimon Schubert   stb = mem_fileopen ();
71095796c8dcSSimon Schubert   old_chain = make_cleanup_ui_file_delete (stb);
71105796c8dcSSimon Schubert 
71115796c8dcSSimon Schubert   fputstrn_unfiltered (buf, n, 0, stb);
71125796c8dcSSimon Schubert   str = ui_file_xstrdup (stb, NULL);
71135796c8dcSSimon Schubert   do_cleanups (old_chain);
71145796c8dcSSimon Schubert   return str;
71155796c8dcSSimon Schubert }
71165796c8dcSSimon Schubert 
71175796c8dcSSimon Schubert /* Display a null-terminated packet on stdout, for debugging, using C
71185796c8dcSSimon Schubert    string notation.  */
71195796c8dcSSimon Schubert 
71205796c8dcSSimon Schubert static void
print_packet(char * buf)71215796c8dcSSimon Schubert print_packet (char *buf)
71225796c8dcSSimon Schubert {
71235796c8dcSSimon Schubert   puts_filtered ("\"");
71245796c8dcSSimon Schubert   fputstr_filtered (buf, '"', gdb_stdout);
71255796c8dcSSimon Schubert   puts_filtered ("\"");
71265796c8dcSSimon Schubert }
71275796c8dcSSimon Schubert 
71285796c8dcSSimon Schubert int
putpkt(char * buf)71295796c8dcSSimon Schubert putpkt (char *buf)
71305796c8dcSSimon Schubert {
71315796c8dcSSimon Schubert   return putpkt_binary (buf, strlen (buf));
71325796c8dcSSimon Schubert }
71335796c8dcSSimon Schubert 
71345796c8dcSSimon Schubert /* Send a packet to the remote machine, with error checking.  The data
71355796c8dcSSimon Schubert    of the packet is in BUF.  The string in BUF can be at most
71365796c8dcSSimon Schubert    get_remote_packet_size () - 5 to account for the $, # and checksum,
71375796c8dcSSimon Schubert    and for a possible /0 if we are debugging (remote_debug) and want
71385796c8dcSSimon Schubert    to print the sent packet as a string.  */
71395796c8dcSSimon Schubert 
71405796c8dcSSimon Schubert static int
putpkt_binary(char * buf,int cnt)71415796c8dcSSimon Schubert putpkt_binary (char *buf, int cnt)
71425796c8dcSSimon Schubert {
71435796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
71445796c8dcSSimon Schubert   int i;
71455796c8dcSSimon Schubert   unsigned char csum = 0;
71465796c8dcSSimon Schubert   char *buf2 = alloca (cnt + 6);
71475796c8dcSSimon Schubert 
71485796c8dcSSimon Schubert   int ch;
71495796c8dcSSimon Schubert   int tcount = 0;
71505796c8dcSSimon Schubert   char *p;
7151ef5ccd6cSJohn Marino   char *message;
71525796c8dcSSimon Schubert 
71535796c8dcSSimon Schubert   /* Catch cases like trying to read memory or listing threads while
71545796c8dcSSimon Schubert      we're waiting for a stop reply.  The remote server wouldn't be
71555796c8dcSSimon Schubert      ready to handle this request, so we'd hang and timeout.  We don't
71565796c8dcSSimon Schubert      have to worry about this in synchronous mode, because in that
71575796c8dcSSimon Schubert      case it's not possible to issue a command while the target is
71585796c8dcSSimon Schubert      running.  This is not a problem in non-stop mode, because in that
71595796c8dcSSimon Schubert      case, the stub is always ready to process serial input.  */
71605796c8dcSSimon Schubert   if (!non_stop && target_can_async_p () && rs->waiting_for_stop_reply)
71615796c8dcSSimon Schubert     error (_("Cannot execute this command while the target is running."));
71625796c8dcSSimon Schubert 
71635796c8dcSSimon Schubert   /* We're sending out a new packet.  Make sure we don't look at a
71645796c8dcSSimon Schubert      stale cached response.  */
71655796c8dcSSimon Schubert   rs->cached_wait_status = 0;
71665796c8dcSSimon Schubert 
71675796c8dcSSimon Schubert   /* Copy the packet into buffer BUF2, encapsulating it
71685796c8dcSSimon Schubert      and giving it a checksum.  */
71695796c8dcSSimon Schubert 
71705796c8dcSSimon Schubert   p = buf2;
71715796c8dcSSimon Schubert   *p++ = '$';
71725796c8dcSSimon Schubert 
71735796c8dcSSimon Schubert   for (i = 0; i < cnt; i++)
71745796c8dcSSimon Schubert     {
71755796c8dcSSimon Schubert       csum += buf[i];
71765796c8dcSSimon Schubert       *p++ = buf[i];
71775796c8dcSSimon Schubert     }
71785796c8dcSSimon Schubert   *p++ = '#';
71795796c8dcSSimon Schubert   *p++ = tohex ((csum >> 4) & 0xf);
71805796c8dcSSimon Schubert   *p++ = tohex (csum & 0xf);
71815796c8dcSSimon Schubert 
71825796c8dcSSimon Schubert   /* Send it over and over until we get a positive ack.  */
71835796c8dcSSimon Schubert 
71845796c8dcSSimon Schubert   while (1)
71855796c8dcSSimon Schubert     {
71865796c8dcSSimon Schubert       int started_error_output = 0;
71875796c8dcSSimon Schubert 
71885796c8dcSSimon Schubert       if (remote_debug)
71895796c8dcSSimon Schubert 	{
71905796c8dcSSimon Schubert 	  struct cleanup *old_chain;
71915796c8dcSSimon Schubert 	  char *str;
71925796c8dcSSimon Schubert 
71935796c8dcSSimon Schubert 	  *p = '\0';
71945796c8dcSSimon Schubert 	  str = escape_buffer (buf2, p - buf2);
71955796c8dcSSimon Schubert 	  old_chain = make_cleanup (xfree, str);
71965796c8dcSSimon Schubert 	  fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
71975796c8dcSSimon Schubert 	  gdb_flush (gdb_stdlog);
71985796c8dcSSimon Schubert 	  do_cleanups (old_chain);
71995796c8dcSSimon Schubert 	}
7200ef5ccd6cSJohn Marino       remote_serial_write (buf2, p - buf2);
72015796c8dcSSimon Schubert 
72025796c8dcSSimon Schubert       /* If this is a no acks version of the remote protocol, send the
72035796c8dcSSimon Schubert 	 packet and move on.  */
72045796c8dcSSimon Schubert       if (rs->noack_mode)
72055796c8dcSSimon Schubert         break;
72065796c8dcSSimon Schubert 
72075796c8dcSSimon Schubert       /* Read until either a timeout occurs (-2) or '+' is read.
72085796c8dcSSimon Schubert 	 Handle any notification that arrives in the mean time.  */
72095796c8dcSSimon Schubert       while (1)
72105796c8dcSSimon Schubert 	{
72115796c8dcSSimon Schubert 	  ch = readchar (remote_timeout);
72125796c8dcSSimon Schubert 
72135796c8dcSSimon Schubert 	  if (remote_debug)
72145796c8dcSSimon Schubert 	    {
72155796c8dcSSimon Schubert 	      switch (ch)
72165796c8dcSSimon Schubert 		{
72175796c8dcSSimon Schubert 		case '+':
72185796c8dcSSimon Schubert 		case '-':
72195796c8dcSSimon Schubert 		case SERIAL_TIMEOUT:
72205796c8dcSSimon Schubert 		case '$':
72215796c8dcSSimon Schubert 		case '%':
72225796c8dcSSimon Schubert 		  if (started_error_output)
72235796c8dcSSimon Schubert 		    {
72245796c8dcSSimon Schubert 		      putchar_unfiltered ('\n');
72255796c8dcSSimon Schubert 		      started_error_output = 0;
72265796c8dcSSimon Schubert 		    }
72275796c8dcSSimon Schubert 		}
72285796c8dcSSimon Schubert 	    }
72295796c8dcSSimon Schubert 
72305796c8dcSSimon Schubert 	  switch (ch)
72315796c8dcSSimon Schubert 	    {
72325796c8dcSSimon Schubert 	    case '+':
72335796c8dcSSimon Schubert 	      if (remote_debug)
72345796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, "Ack\n");
72355796c8dcSSimon Schubert 	      return 1;
72365796c8dcSSimon Schubert 	    case '-':
72375796c8dcSSimon Schubert 	      if (remote_debug)
72385796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog, "Nak\n");
7239c50c785cSJohn Marino 	      /* FALLTHROUGH */
72405796c8dcSSimon Schubert 	    case SERIAL_TIMEOUT:
72415796c8dcSSimon Schubert 	      tcount++;
72425796c8dcSSimon Schubert 	      if (tcount > 3)
72435796c8dcSSimon Schubert 		return 0;
72445796c8dcSSimon Schubert 	      break;		/* Retransmit buffer.  */
72455796c8dcSSimon Schubert 	    case '$':
72465796c8dcSSimon Schubert 	      {
72475796c8dcSSimon Schubert 	        if (remote_debug)
72485796c8dcSSimon Schubert 		  fprintf_unfiltered (gdb_stdlog,
72495796c8dcSSimon Schubert 				      "Packet instead of Ack, ignoring it\n");
72505796c8dcSSimon Schubert 		/* It's probably an old response sent because an ACK
72515796c8dcSSimon Schubert 		   was lost.  Gobble up the packet and ack it so it
72525796c8dcSSimon Schubert 		   doesn't get retransmitted when we resend this
72535796c8dcSSimon Schubert 		   packet.  */
72545796c8dcSSimon Schubert 		skip_frame ();
7255ef5ccd6cSJohn Marino 		remote_serial_write ("+", 1);
72565796c8dcSSimon Schubert 		continue;	/* Now, go look for +.  */
72575796c8dcSSimon Schubert 	      }
72585796c8dcSSimon Schubert 
72595796c8dcSSimon Schubert 	    case '%':
72605796c8dcSSimon Schubert 	      {
72615796c8dcSSimon Schubert 		int val;
72625796c8dcSSimon Schubert 
72635796c8dcSSimon Schubert 		/* If we got a notification, handle it, and go back to looking
72645796c8dcSSimon Schubert 		   for an ack.  */
72655796c8dcSSimon Schubert 		/* We've found the start of a notification.  Now
72665796c8dcSSimon Schubert 		   collect the data.  */
72675796c8dcSSimon Schubert 		val = read_frame (&rs->buf, &rs->buf_size);
72685796c8dcSSimon Schubert 		if (val >= 0)
72695796c8dcSSimon Schubert 		  {
72705796c8dcSSimon Schubert 		    if (remote_debug)
72715796c8dcSSimon Schubert 		      {
72725796c8dcSSimon Schubert 			struct cleanup *old_chain;
72735796c8dcSSimon Schubert 			char *str;
72745796c8dcSSimon Schubert 
72755796c8dcSSimon Schubert 			str = escape_buffer (rs->buf, val);
72765796c8dcSSimon Schubert 			old_chain = make_cleanup (xfree, str);
72775796c8dcSSimon Schubert 			fprintf_unfiltered (gdb_stdlog,
72785796c8dcSSimon Schubert 					    "  Notification received: %s\n",
72795796c8dcSSimon Schubert 					    str);
72805796c8dcSSimon Schubert 			do_cleanups (old_chain);
72815796c8dcSSimon Schubert 		      }
7282ef5ccd6cSJohn Marino 		    handle_notification (rs->buf);
72835796c8dcSSimon Schubert 		    /* We're in sync now, rewait for the ack.  */
72845796c8dcSSimon Schubert 		    tcount = 0;
72855796c8dcSSimon Schubert 		  }
72865796c8dcSSimon Schubert 		else
72875796c8dcSSimon Schubert 		  {
72885796c8dcSSimon Schubert 		    if (remote_debug)
72895796c8dcSSimon Schubert 		      {
72905796c8dcSSimon Schubert 			if (!started_error_output)
72915796c8dcSSimon Schubert 			  {
72925796c8dcSSimon Schubert 			    started_error_output = 1;
72935796c8dcSSimon Schubert 			    fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
72945796c8dcSSimon Schubert 			  }
72955796c8dcSSimon Schubert 			fputc_unfiltered (ch & 0177, gdb_stdlog);
72965796c8dcSSimon Schubert 			fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
72975796c8dcSSimon Schubert 		      }
72985796c8dcSSimon Schubert 		  }
72995796c8dcSSimon Schubert 		continue;
73005796c8dcSSimon Schubert 	      }
73015796c8dcSSimon Schubert 	      /* fall-through */
73025796c8dcSSimon Schubert 	    default:
73035796c8dcSSimon Schubert 	      if (remote_debug)
73045796c8dcSSimon Schubert 		{
73055796c8dcSSimon Schubert 		  if (!started_error_output)
73065796c8dcSSimon Schubert 		    {
73075796c8dcSSimon Schubert 		      started_error_output = 1;
73085796c8dcSSimon Schubert 		      fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
73095796c8dcSSimon Schubert 		    }
73105796c8dcSSimon Schubert 		  fputc_unfiltered (ch & 0177, gdb_stdlog);
73115796c8dcSSimon Schubert 		}
73125796c8dcSSimon Schubert 	      continue;
73135796c8dcSSimon Schubert 	    }
73145796c8dcSSimon Schubert 	  break;		/* Here to retransmit.  */
73155796c8dcSSimon Schubert 	}
73165796c8dcSSimon Schubert 
73175796c8dcSSimon Schubert #if 0
73185796c8dcSSimon Schubert       /* This is wrong.  If doing a long backtrace, the user should be
73195796c8dcSSimon Schubert          able to get out next time we call QUIT, without anything as
73205796c8dcSSimon Schubert          violent as interrupt_query.  If we want to provide a way out of
73215796c8dcSSimon Schubert          here without getting to the next QUIT, it should be based on
73225796c8dcSSimon Schubert          hitting ^C twice as in remote_wait.  */
73235796c8dcSSimon Schubert       if (quit_flag)
73245796c8dcSSimon Schubert 	{
73255796c8dcSSimon Schubert 	  quit_flag = 0;
73265796c8dcSSimon Schubert 	  interrupt_query ();
73275796c8dcSSimon Schubert 	}
73285796c8dcSSimon Schubert #endif
73295796c8dcSSimon Schubert     }
73305796c8dcSSimon Schubert   return 0;
73315796c8dcSSimon Schubert }
73325796c8dcSSimon Schubert 
73335796c8dcSSimon Schubert /* Come here after finding the start of a frame when we expected an
73345796c8dcSSimon Schubert    ack.  Do our best to discard the rest of this packet.  */
73355796c8dcSSimon Schubert 
73365796c8dcSSimon Schubert static void
skip_frame(void)73375796c8dcSSimon Schubert skip_frame (void)
73385796c8dcSSimon Schubert {
73395796c8dcSSimon Schubert   int c;
73405796c8dcSSimon Schubert 
73415796c8dcSSimon Schubert   while (1)
73425796c8dcSSimon Schubert     {
73435796c8dcSSimon Schubert       c = readchar (remote_timeout);
73445796c8dcSSimon Schubert       switch (c)
73455796c8dcSSimon Schubert 	{
73465796c8dcSSimon Schubert 	case SERIAL_TIMEOUT:
73475796c8dcSSimon Schubert 	  /* Nothing we can do.  */
73485796c8dcSSimon Schubert 	  return;
73495796c8dcSSimon Schubert 	case '#':
73505796c8dcSSimon Schubert 	  /* Discard the two bytes of checksum and stop.  */
73515796c8dcSSimon Schubert 	  c = readchar (remote_timeout);
73525796c8dcSSimon Schubert 	  if (c >= 0)
73535796c8dcSSimon Schubert 	    c = readchar (remote_timeout);
73545796c8dcSSimon Schubert 
73555796c8dcSSimon Schubert 	  return;
73565796c8dcSSimon Schubert 	case '*':		/* Run length encoding.  */
73575796c8dcSSimon Schubert 	  /* Discard the repeat count.  */
73585796c8dcSSimon Schubert 	  c = readchar (remote_timeout);
73595796c8dcSSimon Schubert 	  if (c < 0)
73605796c8dcSSimon Schubert 	    return;
73615796c8dcSSimon Schubert 	  break;
73625796c8dcSSimon Schubert 	default:
73635796c8dcSSimon Schubert 	  /* A regular character.  */
73645796c8dcSSimon Schubert 	  break;
73655796c8dcSSimon Schubert 	}
73665796c8dcSSimon Schubert     }
73675796c8dcSSimon Schubert }
73685796c8dcSSimon Schubert 
73695796c8dcSSimon Schubert /* Come here after finding the start of the frame.  Collect the rest
73705796c8dcSSimon Schubert    into *BUF, verifying the checksum, length, and handling run-length
73715796c8dcSSimon Schubert    compression.  NUL terminate the buffer.  If there is not enough room,
73725796c8dcSSimon Schubert    expand *BUF using xrealloc.
73735796c8dcSSimon Schubert 
73745796c8dcSSimon Schubert    Returns -1 on error, number of characters in buffer (ignoring the
73755796c8dcSSimon Schubert    trailing NULL) on success. (could be extended to return one of the
73765796c8dcSSimon Schubert    SERIAL status indications).  */
73775796c8dcSSimon Schubert 
73785796c8dcSSimon Schubert static long
read_frame(char ** buf_p,long * sizeof_buf)73795796c8dcSSimon Schubert read_frame (char **buf_p,
73805796c8dcSSimon Schubert 	    long *sizeof_buf)
73815796c8dcSSimon Schubert {
73825796c8dcSSimon Schubert   unsigned char csum;
73835796c8dcSSimon Schubert   long bc;
73845796c8dcSSimon Schubert   int c;
73855796c8dcSSimon Schubert   char *buf = *buf_p;
73865796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
73875796c8dcSSimon Schubert 
73885796c8dcSSimon Schubert   csum = 0;
73895796c8dcSSimon Schubert   bc = 0;
73905796c8dcSSimon Schubert 
73915796c8dcSSimon Schubert   while (1)
73925796c8dcSSimon Schubert     {
73935796c8dcSSimon Schubert       c = readchar (remote_timeout);
73945796c8dcSSimon Schubert       switch (c)
73955796c8dcSSimon Schubert 	{
73965796c8dcSSimon Schubert 	case SERIAL_TIMEOUT:
73975796c8dcSSimon Schubert 	  if (remote_debug)
73985796c8dcSSimon Schubert 	    fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
73995796c8dcSSimon Schubert 	  return -1;
74005796c8dcSSimon Schubert 	case '$':
74015796c8dcSSimon Schubert 	  if (remote_debug)
74025796c8dcSSimon Schubert 	    fputs_filtered ("Saw new packet start in middle of old one\n",
74035796c8dcSSimon Schubert 			    gdb_stdlog);
74045796c8dcSSimon Schubert 	  return -1;		/* Start a new packet, count retries.  */
74055796c8dcSSimon Schubert 	case '#':
74065796c8dcSSimon Schubert 	  {
74075796c8dcSSimon Schubert 	    unsigned char pktcsum;
74085796c8dcSSimon Schubert 	    int check_0 = 0;
74095796c8dcSSimon Schubert 	    int check_1 = 0;
74105796c8dcSSimon Schubert 
74115796c8dcSSimon Schubert 	    buf[bc] = '\0';
74125796c8dcSSimon Schubert 
74135796c8dcSSimon Schubert 	    check_0 = readchar (remote_timeout);
74145796c8dcSSimon Schubert 	    if (check_0 >= 0)
74155796c8dcSSimon Schubert 	      check_1 = readchar (remote_timeout);
74165796c8dcSSimon Schubert 
74175796c8dcSSimon Schubert 	    if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
74185796c8dcSSimon Schubert 	      {
74195796c8dcSSimon Schubert 		if (remote_debug)
74205796c8dcSSimon Schubert 		  fputs_filtered ("Timeout in checksum, retrying\n",
74215796c8dcSSimon Schubert 				  gdb_stdlog);
74225796c8dcSSimon Schubert 		return -1;
74235796c8dcSSimon Schubert 	      }
74245796c8dcSSimon Schubert 	    else if (check_0 < 0 || check_1 < 0)
74255796c8dcSSimon Schubert 	      {
74265796c8dcSSimon Schubert 		if (remote_debug)
74275796c8dcSSimon Schubert 		  fputs_filtered ("Communication error in checksum\n",
74285796c8dcSSimon Schubert 				  gdb_stdlog);
74295796c8dcSSimon Schubert 		return -1;
74305796c8dcSSimon Schubert 	      }
74315796c8dcSSimon Schubert 
74325796c8dcSSimon Schubert 	    /* Don't recompute the checksum; with no ack packets we
74335796c8dcSSimon Schubert 	       don't have any way to indicate a packet retransmission
74345796c8dcSSimon Schubert 	       is necessary.  */
74355796c8dcSSimon Schubert 	    if (rs->noack_mode)
74365796c8dcSSimon Schubert 	      return bc;
74375796c8dcSSimon Schubert 
74385796c8dcSSimon Schubert 	    pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
74395796c8dcSSimon Schubert 	    if (csum == pktcsum)
74405796c8dcSSimon Schubert               return bc;
74415796c8dcSSimon Schubert 
74425796c8dcSSimon Schubert 	    if (remote_debug)
74435796c8dcSSimon Schubert 	      {
74445796c8dcSSimon Schubert 		struct cleanup *old_chain;
74455796c8dcSSimon Schubert 		char *str;
74465796c8dcSSimon Schubert 
74475796c8dcSSimon Schubert 		str = escape_buffer (buf, bc);
74485796c8dcSSimon Schubert 		old_chain = make_cleanup (xfree, str);
74495796c8dcSSimon Schubert 		fprintf_unfiltered (gdb_stdlog,
7450c50c785cSJohn Marino 				    "Bad checksum, sentsum=0x%x, "
7451c50c785cSJohn Marino 				    "csum=0x%x, buf=%s\n",
74525796c8dcSSimon Schubert 				    pktcsum, csum, str);
74535796c8dcSSimon Schubert 		do_cleanups (old_chain);
74545796c8dcSSimon Schubert 	      }
74555796c8dcSSimon Schubert 	    /* Number of characters in buffer ignoring trailing
74565796c8dcSSimon Schubert                NULL.  */
74575796c8dcSSimon Schubert 	    return -1;
74585796c8dcSSimon Schubert 	  }
74595796c8dcSSimon Schubert 	case '*':		/* Run length encoding.  */
74605796c8dcSSimon Schubert           {
74615796c8dcSSimon Schubert 	    int repeat;
74625796c8dcSSimon Schubert 
7463cf7f2e2dSJohn Marino  	    csum += c;
74645796c8dcSSimon Schubert 	    c = readchar (remote_timeout);
74655796c8dcSSimon Schubert 	    csum += c;
74665796c8dcSSimon Schubert 	    repeat = c - ' ' + 3;	/* Compute repeat count.  */
74675796c8dcSSimon Schubert 
74685796c8dcSSimon Schubert 	    /* The character before ``*'' is repeated.  */
74695796c8dcSSimon Schubert 
74705796c8dcSSimon Schubert 	    if (repeat > 0 && repeat <= 255 && bc > 0)
74715796c8dcSSimon Schubert 	      {
74725796c8dcSSimon Schubert 		if (bc + repeat - 1 >= *sizeof_buf - 1)
74735796c8dcSSimon Schubert 		  {
74745796c8dcSSimon Schubert 		    /* Make some more room in the buffer.  */
74755796c8dcSSimon Schubert 		    *sizeof_buf += repeat;
74765796c8dcSSimon Schubert 		    *buf_p = xrealloc (*buf_p, *sizeof_buf);
74775796c8dcSSimon Schubert 		    buf = *buf_p;
74785796c8dcSSimon Schubert 		  }
74795796c8dcSSimon Schubert 
74805796c8dcSSimon Schubert 		memset (&buf[bc], buf[bc - 1], repeat);
74815796c8dcSSimon Schubert 		bc += repeat;
74825796c8dcSSimon Schubert 		continue;
74835796c8dcSSimon Schubert 	      }
74845796c8dcSSimon Schubert 
74855796c8dcSSimon Schubert 	    buf[bc] = '\0';
74865796c8dcSSimon Schubert 	    printf_filtered (_("Invalid run length encoding: %s\n"), buf);
74875796c8dcSSimon Schubert 	    return -1;
74885796c8dcSSimon Schubert 	  }
74895796c8dcSSimon Schubert 	default:
74905796c8dcSSimon Schubert 	  if (bc >= *sizeof_buf - 1)
74915796c8dcSSimon Schubert 	    {
74925796c8dcSSimon Schubert 	      /* Make some more room in the buffer.  */
74935796c8dcSSimon Schubert 	      *sizeof_buf *= 2;
74945796c8dcSSimon Schubert 	      *buf_p = xrealloc (*buf_p, *sizeof_buf);
74955796c8dcSSimon Schubert 	      buf = *buf_p;
74965796c8dcSSimon Schubert 	    }
74975796c8dcSSimon Schubert 
74985796c8dcSSimon Schubert 	  buf[bc++] = c;
74995796c8dcSSimon Schubert 	  csum += c;
75005796c8dcSSimon Schubert 	  continue;
75015796c8dcSSimon Schubert 	}
75025796c8dcSSimon Schubert     }
75035796c8dcSSimon Schubert }
75045796c8dcSSimon Schubert 
75055796c8dcSSimon Schubert /* Read a packet from the remote machine, with error checking, and
75065796c8dcSSimon Schubert    store it in *BUF.  Resize *BUF using xrealloc if necessary to hold
75075796c8dcSSimon Schubert    the result, and update *SIZEOF_BUF.  If FOREVER, wait forever
75085796c8dcSSimon Schubert    rather than timing out; this is used (in synchronous mode) to wait
75095796c8dcSSimon Schubert    for a target that is is executing user code to stop.  */
75105796c8dcSSimon Schubert /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
75115796c8dcSSimon Schubert    don't have to change all the calls to getpkt to deal with the
75125796c8dcSSimon Schubert    return value, because at the moment I don't know what the right
75135796c8dcSSimon Schubert    thing to do it for those.  */
75145796c8dcSSimon Schubert void
getpkt(char ** buf,long * sizeof_buf,int forever)75155796c8dcSSimon Schubert getpkt (char **buf,
75165796c8dcSSimon Schubert 	long *sizeof_buf,
75175796c8dcSSimon Schubert 	int forever)
75185796c8dcSSimon Schubert {
75195796c8dcSSimon Schubert   int timed_out;
75205796c8dcSSimon Schubert 
75215796c8dcSSimon Schubert   timed_out = getpkt_sane (buf, sizeof_buf, forever);
75225796c8dcSSimon Schubert }
75235796c8dcSSimon Schubert 
75245796c8dcSSimon Schubert 
75255796c8dcSSimon Schubert /* Read a packet from the remote machine, with error checking, and
75265796c8dcSSimon Schubert    store it in *BUF.  Resize *BUF using xrealloc if necessary to hold
75275796c8dcSSimon Schubert    the result, and update *SIZEOF_BUF.  If FOREVER, wait forever
75285796c8dcSSimon Schubert    rather than timing out; this is used (in synchronous mode) to wait
75295796c8dcSSimon Schubert    for a target that is is executing user code to stop.  If FOREVER ==
75305796c8dcSSimon Schubert    0, this function is allowed to time out gracefully and return an
75315796c8dcSSimon Schubert    indication of this to the caller.  Otherwise return the number of
75325796c8dcSSimon Schubert    bytes read.  If EXPECTING_NOTIF, consider receiving a notification
7533ef5ccd6cSJohn Marino    enough reason to return to the caller.  *IS_NOTIF is an output
7534ef5ccd6cSJohn Marino    boolean that indicates whether *BUF holds a notification or not
7535ef5ccd6cSJohn Marino    (a regular packet).  */
75365796c8dcSSimon Schubert 
75375796c8dcSSimon Schubert static int
getpkt_or_notif_sane_1(char ** buf,long * sizeof_buf,int forever,int expecting_notif,int * is_notif)75385796c8dcSSimon Schubert getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
7539ef5ccd6cSJohn Marino 			int expecting_notif, int *is_notif)
75405796c8dcSSimon Schubert {
75415796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
75425796c8dcSSimon Schubert   int c;
75435796c8dcSSimon Schubert   int tries;
75445796c8dcSSimon Schubert   int timeout;
75455796c8dcSSimon Schubert   int val = -1;
75465796c8dcSSimon Schubert 
75475796c8dcSSimon Schubert   /* We're reading a new response.  Make sure we don't look at a
75485796c8dcSSimon Schubert      previously cached response.  */
75495796c8dcSSimon Schubert   rs->cached_wait_status = 0;
75505796c8dcSSimon Schubert 
75515796c8dcSSimon Schubert   strcpy (*buf, "timeout");
75525796c8dcSSimon Schubert 
75535796c8dcSSimon Schubert   if (forever)
75545796c8dcSSimon Schubert     timeout = watchdog > 0 ? watchdog : -1;
75555796c8dcSSimon Schubert   else if (expecting_notif)
75565796c8dcSSimon Schubert     timeout = 0; /* There should already be a char in the buffer.  If
75575796c8dcSSimon Schubert 		    not, bail out.  */
75585796c8dcSSimon Schubert   else
75595796c8dcSSimon Schubert     timeout = remote_timeout;
75605796c8dcSSimon Schubert 
75615796c8dcSSimon Schubert #define MAX_TRIES 3
75625796c8dcSSimon Schubert 
75635796c8dcSSimon Schubert   /* Process any number of notifications, and then return when
75645796c8dcSSimon Schubert      we get a packet.  */
75655796c8dcSSimon Schubert   for (;;)
75665796c8dcSSimon Schubert     {
75675796c8dcSSimon Schubert       /* If we get a timeout or bad checksm, retry up to MAX_TRIES
75685796c8dcSSimon Schubert 	 times.  */
75695796c8dcSSimon Schubert       for (tries = 1; tries <= MAX_TRIES; tries++)
75705796c8dcSSimon Schubert 	{
75715796c8dcSSimon Schubert 	  /* This can loop forever if the remote side sends us
75725796c8dcSSimon Schubert 	     characters continuously, but if it pauses, we'll get
75735796c8dcSSimon Schubert 	     SERIAL_TIMEOUT from readchar because of timeout.  Then
75745796c8dcSSimon Schubert 	     we'll count that as a retry.
75755796c8dcSSimon Schubert 
75765796c8dcSSimon Schubert 	     Note that even when forever is set, we will only wait
75775796c8dcSSimon Schubert 	     forever prior to the start of a packet.  After that, we
75785796c8dcSSimon Schubert 	     expect characters to arrive at a brisk pace.  They should
75795796c8dcSSimon Schubert 	     show up within remote_timeout intervals.  */
75805796c8dcSSimon Schubert 	  do
75815796c8dcSSimon Schubert 	    c = readchar (timeout);
75825796c8dcSSimon Schubert 	  while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
75835796c8dcSSimon Schubert 
75845796c8dcSSimon Schubert 	  if (c == SERIAL_TIMEOUT)
75855796c8dcSSimon Schubert 	    {
75865796c8dcSSimon Schubert 	      if (expecting_notif)
75875796c8dcSSimon Schubert 		return -1; /* Don't complain, it's normal to not get
75885796c8dcSSimon Schubert 			      anything in this case.  */
75895796c8dcSSimon Schubert 
75905796c8dcSSimon Schubert 	      if (forever)	/* Watchdog went off?  Kill the target.  */
75915796c8dcSSimon Schubert 		{
75925796c8dcSSimon Schubert 		  QUIT;
7593ef5ccd6cSJohn Marino 		  remote_unpush_target ();
7594ef5ccd6cSJohn Marino 		  throw_error (TARGET_CLOSE_ERROR,
7595ef5ccd6cSJohn Marino 			       _("Watchdog timeout has expired.  "
7596ef5ccd6cSJohn Marino 				 "Target detached."));
75975796c8dcSSimon Schubert 		}
75985796c8dcSSimon Schubert 	      if (remote_debug)
75995796c8dcSSimon Schubert 		fputs_filtered ("Timed out.\n", gdb_stdlog);
76005796c8dcSSimon Schubert 	    }
76015796c8dcSSimon Schubert 	  else
76025796c8dcSSimon Schubert 	    {
76035796c8dcSSimon Schubert 	      /* We've found the start of a packet or notification.
76045796c8dcSSimon Schubert 		 Now collect the data.  */
76055796c8dcSSimon Schubert 	      val = read_frame (buf, sizeof_buf);
76065796c8dcSSimon Schubert 	      if (val >= 0)
76075796c8dcSSimon Schubert 		break;
76085796c8dcSSimon Schubert 	    }
76095796c8dcSSimon Schubert 
7610ef5ccd6cSJohn Marino 	  remote_serial_write ("-", 1);
76115796c8dcSSimon Schubert 	}
76125796c8dcSSimon Schubert 
76135796c8dcSSimon Schubert       if (tries > MAX_TRIES)
76145796c8dcSSimon Schubert 	{
76155796c8dcSSimon Schubert 	  /* We have tried hard enough, and just can't receive the
76165796c8dcSSimon Schubert 	     packet/notification.  Give up.  */
76175796c8dcSSimon Schubert 	  printf_unfiltered (_("Ignoring packet error, continuing...\n"));
76185796c8dcSSimon Schubert 
76195796c8dcSSimon Schubert 	  /* Skip the ack char if we're in no-ack mode.  */
76205796c8dcSSimon Schubert 	  if (!rs->noack_mode)
7621ef5ccd6cSJohn Marino 	    remote_serial_write ("+", 1);
76225796c8dcSSimon Schubert 	  return -1;
76235796c8dcSSimon Schubert 	}
76245796c8dcSSimon Schubert 
76255796c8dcSSimon Schubert       /* If we got an ordinary packet, return that to our caller.  */
76265796c8dcSSimon Schubert       if (c == '$')
76275796c8dcSSimon Schubert 	{
76285796c8dcSSimon Schubert 	  if (remote_debug)
76295796c8dcSSimon Schubert 	    {
76305796c8dcSSimon Schubert 	     struct cleanup *old_chain;
76315796c8dcSSimon Schubert 	     char *str;
76325796c8dcSSimon Schubert 
76335796c8dcSSimon Schubert 	     str = escape_buffer (*buf, val);
76345796c8dcSSimon Schubert 	     old_chain = make_cleanup (xfree, str);
76355796c8dcSSimon Schubert 	     fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
76365796c8dcSSimon Schubert 	     do_cleanups (old_chain);
76375796c8dcSSimon Schubert 	    }
76385796c8dcSSimon Schubert 
76395796c8dcSSimon Schubert 	  /* Skip the ack char if we're in no-ack mode.  */
76405796c8dcSSimon Schubert 	  if (!rs->noack_mode)
7641ef5ccd6cSJohn Marino 	    remote_serial_write ("+", 1);
7642ef5ccd6cSJohn Marino 	  if (is_notif != NULL)
7643ef5ccd6cSJohn Marino 	    *is_notif = 0;
76445796c8dcSSimon Schubert 	  return val;
76455796c8dcSSimon Schubert 	}
76465796c8dcSSimon Schubert 
76475796c8dcSSimon Schubert        /* If we got a notification, handle it, and go back to looking
76485796c8dcSSimon Schubert 	 for a packet.  */
76495796c8dcSSimon Schubert       else
76505796c8dcSSimon Schubert 	{
76515796c8dcSSimon Schubert 	  gdb_assert (c == '%');
76525796c8dcSSimon Schubert 
76535796c8dcSSimon Schubert 	  if (remote_debug)
76545796c8dcSSimon Schubert 	    {
76555796c8dcSSimon Schubert 	      struct cleanup *old_chain;
76565796c8dcSSimon Schubert 	      char *str;
76575796c8dcSSimon Schubert 
76585796c8dcSSimon Schubert 	      str = escape_buffer (*buf, val);
76595796c8dcSSimon Schubert 	      old_chain = make_cleanup (xfree, str);
76605796c8dcSSimon Schubert 	      fprintf_unfiltered (gdb_stdlog,
76615796c8dcSSimon Schubert 				  "  Notification received: %s\n",
76625796c8dcSSimon Schubert 				  str);
76635796c8dcSSimon Schubert 	      do_cleanups (old_chain);
76645796c8dcSSimon Schubert 	    }
7665ef5ccd6cSJohn Marino 	  if (is_notif != NULL)
7666ef5ccd6cSJohn Marino 	    *is_notif = 1;
76675796c8dcSSimon Schubert 
7668ef5ccd6cSJohn Marino 	  handle_notification (*buf);
76695796c8dcSSimon Schubert 
76705796c8dcSSimon Schubert 	  /* Notifications require no acknowledgement.  */
76715796c8dcSSimon Schubert 
76725796c8dcSSimon Schubert 	  if (expecting_notif)
7673ef5ccd6cSJohn Marino 	    return val;
76745796c8dcSSimon Schubert 	}
76755796c8dcSSimon Schubert     }
76765796c8dcSSimon Schubert }
76775796c8dcSSimon Schubert 
76785796c8dcSSimon Schubert static int
getpkt_sane(char ** buf,long * sizeof_buf,int forever)76795796c8dcSSimon Schubert getpkt_sane (char **buf, long *sizeof_buf, int forever)
76805796c8dcSSimon Schubert {
7681ef5ccd6cSJohn Marino   return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL);
76825796c8dcSSimon Schubert }
76835796c8dcSSimon Schubert 
76845796c8dcSSimon Schubert static int
getpkt_or_notif_sane(char ** buf,long * sizeof_buf,int forever,int * is_notif)7685ef5ccd6cSJohn Marino getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever,
7686ef5ccd6cSJohn Marino 		      int *is_notif)
76875796c8dcSSimon Schubert {
7688ef5ccd6cSJohn Marino   return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1,
7689ef5ccd6cSJohn Marino 				 is_notif);
76905796c8dcSSimon Schubert }
76915796c8dcSSimon Schubert 
76925796c8dcSSimon Schubert 
7693a45ae5f8SJohn Marino /* A helper function that just calls putpkt; for type correctness.  */
7694a45ae5f8SJohn Marino 
7695a45ae5f8SJohn Marino static int
putpkt_for_catch_errors(void * arg)7696a45ae5f8SJohn Marino putpkt_for_catch_errors (void *arg)
7697a45ae5f8SJohn Marino {
7698a45ae5f8SJohn Marino   return putpkt (arg);
7699a45ae5f8SJohn Marino }
7700a45ae5f8SJohn Marino 
77015796c8dcSSimon Schubert static void
remote_kill(struct target_ops * ops)77025796c8dcSSimon Schubert remote_kill (struct target_ops *ops)
77035796c8dcSSimon Schubert {
77045796c8dcSSimon Schubert   /* Use catch_errors so the user can quit from gdb even when we
77055796c8dcSSimon Schubert      aren't on speaking terms with the remote system.  */
7706a45ae5f8SJohn Marino   catch_errors (putpkt_for_catch_errors, "k", "", RETURN_MASK_ERROR);
77075796c8dcSSimon Schubert 
77085796c8dcSSimon Schubert   /* Don't wait for it to die.  I'm not really sure it matters whether
77095796c8dcSSimon Schubert      we do or not.  For the existing stubs, kill is a noop.  */
77105796c8dcSSimon Schubert   target_mourn_inferior ();
77115796c8dcSSimon Schubert }
77125796c8dcSSimon Schubert 
77135796c8dcSSimon Schubert static int
remote_vkill(int pid,struct remote_state * rs)77145796c8dcSSimon Schubert remote_vkill (int pid, struct remote_state *rs)
77155796c8dcSSimon Schubert {
77165796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE)
77175796c8dcSSimon Schubert     return -1;
77185796c8dcSSimon Schubert 
77195796c8dcSSimon Schubert   /* Tell the remote target to detach.  */
7720ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
77215796c8dcSSimon Schubert   putpkt (rs->buf);
77225796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
77235796c8dcSSimon Schubert 
77245796c8dcSSimon Schubert   if (packet_ok (rs->buf,
77255796c8dcSSimon Schubert 		 &remote_protocol_packets[PACKET_vKill]) == PACKET_OK)
77265796c8dcSSimon Schubert     return 0;
77275796c8dcSSimon Schubert   else if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE)
77285796c8dcSSimon Schubert     return -1;
77295796c8dcSSimon Schubert   else
77305796c8dcSSimon Schubert     return 1;
77315796c8dcSSimon Schubert }
77325796c8dcSSimon Schubert 
77335796c8dcSSimon Schubert static void
extended_remote_kill(struct target_ops * ops)77345796c8dcSSimon Schubert extended_remote_kill (struct target_ops *ops)
77355796c8dcSSimon Schubert {
77365796c8dcSSimon Schubert   int res;
77375796c8dcSSimon Schubert   int pid = ptid_get_pid (inferior_ptid);
77385796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
77395796c8dcSSimon Schubert 
77405796c8dcSSimon Schubert   res = remote_vkill (pid, rs);
7741ef5ccd6cSJohn Marino   if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
77425796c8dcSSimon Schubert     {
77435796c8dcSSimon Schubert       /* Don't try 'k' on a multi-process aware stub -- it has no way
77445796c8dcSSimon Schubert 	 to specify the pid.  */
77455796c8dcSSimon Schubert 
77465796c8dcSSimon Schubert       putpkt ("k");
77475796c8dcSSimon Schubert #if 0
77485796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
77495796c8dcSSimon Schubert       if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
77505796c8dcSSimon Schubert 	res = 1;
77515796c8dcSSimon Schubert #else
77525796c8dcSSimon Schubert       /* Don't wait for it to die.  I'm not really sure it matters whether
77535796c8dcSSimon Schubert 	 we do or not.  For the existing stubs, kill is a noop.  */
77545796c8dcSSimon Schubert       res = 0;
77555796c8dcSSimon Schubert #endif
77565796c8dcSSimon Schubert     }
77575796c8dcSSimon Schubert 
77585796c8dcSSimon Schubert   if (res != 0)
77595796c8dcSSimon Schubert     error (_("Can't kill process"));
77605796c8dcSSimon Schubert 
77615796c8dcSSimon Schubert   target_mourn_inferior ();
77625796c8dcSSimon Schubert }
77635796c8dcSSimon Schubert 
77645796c8dcSSimon Schubert static void
remote_mourn(struct target_ops * ops)77655796c8dcSSimon Schubert remote_mourn (struct target_ops *ops)
77665796c8dcSSimon Schubert {
77675796c8dcSSimon Schubert   remote_mourn_1 (ops);
77685796c8dcSSimon Schubert }
77695796c8dcSSimon Schubert 
77705796c8dcSSimon Schubert /* Worker function for remote_mourn.  */
77715796c8dcSSimon Schubert static void
remote_mourn_1(struct target_ops * target)77725796c8dcSSimon Schubert remote_mourn_1 (struct target_ops *target)
77735796c8dcSSimon Schubert {
77745796c8dcSSimon Schubert   unpush_target (target);
77755796c8dcSSimon Schubert 
77765796c8dcSSimon Schubert   /* remote_close takes care of doing most of the clean up.  */
77775796c8dcSSimon Schubert   generic_mourn_inferior ();
77785796c8dcSSimon Schubert }
77795796c8dcSSimon Schubert 
77805796c8dcSSimon Schubert static void
extended_remote_mourn_1(struct target_ops * target)77815796c8dcSSimon Schubert extended_remote_mourn_1 (struct target_ops *target)
77825796c8dcSSimon Schubert {
77835796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
77845796c8dcSSimon Schubert 
77855796c8dcSSimon Schubert   /* In case we got here due to an error, but we're going to stay
77865796c8dcSSimon Schubert      connected.  */
77875796c8dcSSimon Schubert   rs->waiting_for_stop_reply = 0;
77885796c8dcSSimon Schubert 
77895796c8dcSSimon Schubert   /* If the current general thread belonged to the process we just
77905796c8dcSSimon Schubert      detached from or has exited, the remote side current general
77915796c8dcSSimon Schubert      thread becomes undefined.  Considering a case like this:
77925796c8dcSSimon Schubert 
77935796c8dcSSimon Schubert      - We just got here due to a detach.
77945796c8dcSSimon Schubert      - The process that we're detaching from happens to immediately
77955796c8dcSSimon Schubert        report a global breakpoint being hit in non-stop mode, in the
77965796c8dcSSimon Schubert        same thread we had selected before.
77975796c8dcSSimon Schubert      - GDB attaches to this process again.
77985796c8dcSSimon Schubert      - This event happens to be the next event we handle.
77995796c8dcSSimon Schubert 
78005796c8dcSSimon Schubert      GDB would consider that the current general thread didn't need to
78015796c8dcSSimon Schubert      be set on the stub side (with Hg), since for all it knew,
78025796c8dcSSimon Schubert      GENERAL_THREAD hadn't changed.
78035796c8dcSSimon Schubert 
78045796c8dcSSimon Schubert      Notice that although in all-stop mode, the remote server always
78055796c8dcSSimon Schubert      sets the current thread to the thread reporting the stop event,
78065796c8dcSSimon Schubert      that doesn't happen in non-stop mode; in non-stop, the stub *must
78075796c8dcSSimon Schubert      not* change the current thread when reporting a breakpoint hit,
78085796c8dcSSimon Schubert      due to the decoupling of event reporting and event handling.
78095796c8dcSSimon Schubert 
78105796c8dcSSimon Schubert      To keep things simple, we always invalidate our notion of the
78115796c8dcSSimon Schubert      current thread.  */
78125796c8dcSSimon Schubert   record_currthread (minus_one_ptid);
78135796c8dcSSimon Schubert 
78145796c8dcSSimon Schubert   /* Unlike "target remote", we do not want to unpush the target; then
78155796c8dcSSimon Schubert      the next time the user says "run", we won't be connected.  */
78165796c8dcSSimon Schubert 
78175796c8dcSSimon Schubert   /* Call common code to mark the inferior as not running.	*/
78185796c8dcSSimon Schubert   generic_mourn_inferior ();
78195796c8dcSSimon Schubert 
78205796c8dcSSimon Schubert   if (!have_inferiors ())
78215796c8dcSSimon Schubert     {
78225796c8dcSSimon Schubert       if (!remote_multi_process_p (rs))
78235796c8dcSSimon Schubert 	{
78245796c8dcSSimon Schubert 	  /* Check whether the target is running now - some remote stubs
78255796c8dcSSimon Schubert 	     automatically restart after kill.	*/
78265796c8dcSSimon Schubert 	  putpkt ("?");
78275796c8dcSSimon Schubert 	  getpkt (&rs->buf, &rs->buf_size, 0);
78285796c8dcSSimon Schubert 
78295796c8dcSSimon Schubert 	  if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
78305796c8dcSSimon Schubert 	    {
7831c50c785cSJohn Marino 	      /* Assume that the target has been restarted.  Set
7832c50c785cSJohn Marino 		 inferior_ptid so that bits of core GDB realizes
7833c50c785cSJohn Marino 		 there's something here, e.g., so that the user can
7834c50c785cSJohn Marino 		 say "kill" again.  */
78355796c8dcSSimon Schubert 	      inferior_ptid = magic_null_ptid;
78365796c8dcSSimon Schubert 	    }
78375796c8dcSSimon Schubert 	}
78385796c8dcSSimon Schubert     }
78395796c8dcSSimon Schubert }
78405796c8dcSSimon Schubert 
78415796c8dcSSimon Schubert static void
extended_remote_mourn(struct target_ops * ops)78425796c8dcSSimon Schubert extended_remote_mourn (struct target_ops *ops)
78435796c8dcSSimon Schubert {
78445796c8dcSSimon Schubert   extended_remote_mourn_1 (ops);
78455796c8dcSSimon Schubert }
78465796c8dcSSimon Schubert 
78475796c8dcSSimon Schubert static int
extended_remote_supports_disable_randomization(void)7848a45ae5f8SJohn Marino extended_remote_supports_disable_randomization (void)
7849a45ae5f8SJohn Marino {
7850a45ae5f8SJohn Marino   return (remote_protocol_packets[PACKET_QDisableRandomization].support
7851a45ae5f8SJohn Marino 	  == PACKET_ENABLE);
7852a45ae5f8SJohn Marino }
7853a45ae5f8SJohn Marino 
7854a45ae5f8SJohn Marino static void
extended_remote_disable_randomization(int val)7855a45ae5f8SJohn Marino extended_remote_disable_randomization (int val)
7856a45ae5f8SJohn Marino {
7857a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
7858a45ae5f8SJohn Marino   char *reply;
7859a45ae5f8SJohn Marino 
7860ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
7861ef5ccd6cSJohn Marino 	     val);
7862a45ae5f8SJohn Marino   putpkt (rs->buf);
7863a45ae5f8SJohn Marino   reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
7864a45ae5f8SJohn Marino   if (*reply == '\0')
7865a45ae5f8SJohn Marino     error (_("Target does not support QDisableRandomization."));
7866a45ae5f8SJohn Marino   if (strcmp (reply, "OK") != 0)
7867a45ae5f8SJohn Marino     error (_("Bogus QDisableRandomization reply from target: %s"), reply);
7868a45ae5f8SJohn Marino }
7869a45ae5f8SJohn Marino 
7870a45ae5f8SJohn Marino static int
extended_remote_run(char * args)78715796c8dcSSimon Schubert extended_remote_run (char *args)
78725796c8dcSSimon Schubert {
78735796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
78745796c8dcSSimon Schubert   int len;
78755796c8dcSSimon Schubert 
78765796c8dcSSimon Schubert   /* If the user has disabled vRun support, or we have detected that
78775796c8dcSSimon Schubert      support is not available, do not try it.  */
78785796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE)
78795796c8dcSSimon Schubert     return -1;
78805796c8dcSSimon Schubert 
78815796c8dcSSimon Schubert   strcpy (rs->buf, "vRun;");
78825796c8dcSSimon Schubert   len = strlen (rs->buf);
78835796c8dcSSimon Schubert 
78845796c8dcSSimon Schubert   if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
78855796c8dcSSimon Schubert     error (_("Remote file name too long for run packet"));
78865796c8dcSSimon Schubert   len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len, 0);
78875796c8dcSSimon Schubert 
78885796c8dcSSimon Schubert   gdb_assert (args != NULL);
78895796c8dcSSimon Schubert   if (*args)
78905796c8dcSSimon Schubert     {
78915796c8dcSSimon Schubert       struct cleanup *back_to;
78925796c8dcSSimon Schubert       int i;
78935796c8dcSSimon Schubert       char **argv;
78945796c8dcSSimon Schubert 
78955796c8dcSSimon Schubert       argv = gdb_buildargv (args);
78965796c8dcSSimon Schubert       back_to = make_cleanup ((void (*) (void *)) freeargv, argv);
78975796c8dcSSimon Schubert       for (i = 0; argv[i] != NULL; i++)
78985796c8dcSSimon Schubert 	{
78995796c8dcSSimon Schubert 	  if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
79005796c8dcSSimon Schubert 	    error (_("Argument list too long for run packet"));
79015796c8dcSSimon Schubert 	  rs->buf[len++] = ';';
79025796c8dcSSimon Schubert 	  len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len, 0);
79035796c8dcSSimon Schubert 	}
79045796c8dcSSimon Schubert       do_cleanups (back_to);
79055796c8dcSSimon Schubert     }
79065796c8dcSSimon Schubert 
79075796c8dcSSimon Schubert   rs->buf[len++] = '\0';
79085796c8dcSSimon Schubert 
79095796c8dcSSimon Schubert   putpkt (rs->buf);
79105796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
79115796c8dcSSimon Schubert 
79125796c8dcSSimon Schubert   if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]) == PACKET_OK)
79135796c8dcSSimon Schubert     {
7914ef5ccd6cSJohn Marino       /* We have a wait response.  All is well.  */
79155796c8dcSSimon Schubert       return 0;
79165796c8dcSSimon Schubert     }
79175796c8dcSSimon Schubert   else if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE)
79185796c8dcSSimon Schubert     /* It wasn't disabled before, but it is now.  */
79195796c8dcSSimon Schubert     return -1;
79205796c8dcSSimon Schubert   else
79215796c8dcSSimon Schubert     {
79225796c8dcSSimon Schubert       if (remote_exec_file[0] == '\0')
79235796c8dcSSimon Schubert 	error (_("Running the default executable on the remote target failed; "
79245796c8dcSSimon Schubert 		 "try \"set remote exec-file\"?"));
79255796c8dcSSimon Schubert       else
79265796c8dcSSimon Schubert 	error (_("Running \"%s\" on the remote target failed"),
79275796c8dcSSimon Schubert 	       remote_exec_file);
79285796c8dcSSimon Schubert     }
79295796c8dcSSimon Schubert }
79305796c8dcSSimon Schubert 
79315796c8dcSSimon Schubert /* In the extended protocol we want to be able to do things like
79325796c8dcSSimon Schubert    "run" and have them basically work as expected.  So we need
79335796c8dcSSimon Schubert    a special create_inferior function.  We support changing the
79345796c8dcSSimon Schubert    executable file and the command line arguments, but not the
79355796c8dcSSimon Schubert    environment.  */
79365796c8dcSSimon Schubert 
79375796c8dcSSimon Schubert static void
extended_remote_create_inferior_1(char * exec_file,char * args,char ** env,int from_tty)79385796c8dcSSimon Schubert extended_remote_create_inferior_1 (char *exec_file, char *args,
79395796c8dcSSimon Schubert 				   char **env, int from_tty)
79405796c8dcSSimon Schubert {
7941ef5ccd6cSJohn Marino   int run_worked;
7942ef5ccd6cSJohn Marino   char *stop_reply;
7943ef5ccd6cSJohn Marino   struct remote_state *rs = get_remote_state ();
7944ef5ccd6cSJohn Marino 
79455796c8dcSSimon Schubert   /* If running asynchronously, register the target file descriptor
79465796c8dcSSimon Schubert      with the event loop.  */
79475796c8dcSSimon Schubert   if (target_can_async_p ())
79485796c8dcSSimon Schubert     target_async (inferior_event_handler, 0);
79495796c8dcSSimon Schubert 
7950a45ae5f8SJohn Marino   /* Disable address space randomization if requested (and supported).  */
7951a45ae5f8SJohn Marino   if (extended_remote_supports_disable_randomization ())
7952a45ae5f8SJohn Marino     extended_remote_disable_randomization (disable_randomization);
7953a45ae5f8SJohn Marino 
79545796c8dcSSimon Schubert   /* Now restart the remote server.  */
7955ef5ccd6cSJohn Marino   run_worked = extended_remote_run (args) != -1;
7956ef5ccd6cSJohn Marino   if (!run_worked)
79575796c8dcSSimon Schubert     {
79585796c8dcSSimon Schubert       /* vRun was not supported.  Fail if we need it to do what the
79595796c8dcSSimon Schubert 	 user requested.  */
79605796c8dcSSimon Schubert       if (remote_exec_file[0])
79615796c8dcSSimon Schubert 	error (_("Remote target does not support \"set remote exec-file\""));
79625796c8dcSSimon Schubert       if (args[0])
79635796c8dcSSimon Schubert 	error (_("Remote target does not support \"set args\" or run <ARGS>"));
79645796c8dcSSimon Schubert 
79655796c8dcSSimon Schubert       /* Fall back to "R".  */
79665796c8dcSSimon Schubert       extended_remote_restart ();
79675796c8dcSSimon Schubert     }
79685796c8dcSSimon Schubert 
7969cf7f2e2dSJohn Marino   if (!have_inferiors ())
7970cf7f2e2dSJohn Marino     {
79715796c8dcSSimon Schubert       /* Clean up from the last time we ran, before we mark the target
79725796c8dcSSimon Schubert 	 running again.  This will mark breakpoints uninserted, and
79735796c8dcSSimon Schubert 	 get_offsets may insert breakpoints.  */
79745796c8dcSSimon Schubert       init_thread_list ();
79755796c8dcSSimon Schubert       init_wait_for_inferior ();
7976cf7f2e2dSJohn Marino     }
79775796c8dcSSimon Schubert 
7978ef5ccd6cSJohn Marino   /* vRun's success return is a stop reply.  */
7979ef5ccd6cSJohn Marino   stop_reply = run_worked ? rs->buf : NULL;
7980ef5ccd6cSJohn Marino   add_current_inferior_and_thread (stop_reply);
79815796c8dcSSimon Schubert 
79825796c8dcSSimon Schubert   /* Get updated offsets, if the stub uses qOffsets.  */
79835796c8dcSSimon Schubert   get_offsets ();
79845796c8dcSSimon Schubert }
79855796c8dcSSimon Schubert 
79865796c8dcSSimon Schubert static void
extended_remote_create_inferior(struct target_ops * ops,char * exec_file,char * args,char ** env,int from_tty)79875796c8dcSSimon Schubert extended_remote_create_inferior (struct target_ops *ops,
79885796c8dcSSimon Schubert 				 char *exec_file, char *args,
79895796c8dcSSimon Schubert 				 char **env, int from_tty)
79905796c8dcSSimon Schubert {
79915796c8dcSSimon Schubert   extended_remote_create_inferior_1 (exec_file, args, env, from_tty);
79925796c8dcSSimon Schubert }
79935796c8dcSSimon Schubert 
79945796c8dcSSimon Schubert 
7995ef5ccd6cSJohn Marino /* Given a location's target info BP_TGT and the packet buffer BUF,  output
7996ef5ccd6cSJohn Marino    the list of conditions (in agent expression bytecode format), if any, the
7997ef5ccd6cSJohn Marino    target needs to evaluate.  The output is placed into the packet buffer
7998ef5ccd6cSJohn Marino    started from BUF and ended at BUF_END.  */
7999ef5ccd6cSJohn Marino 
8000ef5ccd6cSJohn Marino static int
remote_add_target_side_condition(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt,char * buf,char * buf_end)8001ef5ccd6cSJohn Marino remote_add_target_side_condition (struct gdbarch *gdbarch,
8002ef5ccd6cSJohn Marino 				  struct bp_target_info *bp_tgt, char *buf,
8003ef5ccd6cSJohn Marino 				  char *buf_end)
8004ef5ccd6cSJohn Marino {
8005ef5ccd6cSJohn Marino   struct agent_expr *aexpr = NULL;
8006ef5ccd6cSJohn Marino   int i, ix;
8007ef5ccd6cSJohn Marino   char *pkt;
8008ef5ccd6cSJohn Marino   char *buf_start = buf;
8009ef5ccd6cSJohn Marino 
8010ef5ccd6cSJohn Marino   if (VEC_empty (agent_expr_p, bp_tgt->conditions))
8011ef5ccd6cSJohn Marino     return 0;
8012ef5ccd6cSJohn Marino 
8013ef5ccd6cSJohn Marino   buf += strlen (buf);
8014ef5ccd6cSJohn Marino   xsnprintf (buf, buf_end - buf, "%s", ";");
8015ef5ccd6cSJohn Marino   buf++;
8016ef5ccd6cSJohn Marino 
8017ef5ccd6cSJohn Marino   /* Send conditions to the target and free the vector.  */
8018ef5ccd6cSJohn Marino   for (ix = 0;
8019ef5ccd6cSJohn Marino        VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
8020ef5ccd6cSJohn Marino        ix++)
8021ef5ccd6cSJohn Marino     {
8022ef5ccd6cSJohn Marino       xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
8023ef5ccd6cSJohn Marino       buf += strlen (buf);
8024ef5ccd6cSJohn Marino       for (i = 0; i < aexpr->len; ++i)
8025ef5ccd6cSJohn Marino 	buf = pack_hex_byte (buf, aexpr->buf[i]);
8026ef5ccd6cSJohn Marino       *buf = '\0';
8027ef5ccd6cSJohn Marino     }
8028ef5ccd6cSJohn Marino 
8029ef5ccd6cSJohn Marino   VEC_free (agent_expr_p, bp_tgt->conditions);
8030ef5ccd6cSJohn Marino   return 0;
8031ef5ccd6cSJohn Marino }
8032ef5ccd6cSJohn Marino 
8033ef5ccd6cSJohn Marino static void
remote_add_target_side_commands(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt,char * buf)8034ef5ccd6cSJohn Marino remote_add_target_side_commands (struct gdbarch *gdbarch,
8035ef5ccd6cSJohn Marino 				 struct bp_target_info *bp_tgt, char *buf)
8036ef5ccd6cSJohn Marino {
8037ef5ccd6cSJohn Marino   struct agent_expr *aexpr = NULL;
8038ef5ccd6cSJohn Marino   int i, ix;
8039ef5ccd6cSJohn Marino 
8040ef5ccd6cSJohn Marino   if (VEC_empty (agent_expr_p, bp_tgt->tcommands))
8041ef5ccd6cSJohn Marino     return;
8042ef5ccd6cSJohn Marino 
8043ef5ccd6cSJohn Marino   buf += strlen (buf);
8044ef5ccd6cSJohn Marino 
8045ef5ccd6cSJohn Marino   sprintf (buf, ";cmds:%x,", bp_tgt->persist);
8046ef5ccd6cSJohn Marino   buf += strlen (buf);
8047ef5ccd6cSJohn Marino 
8048ef5ccd6cSJohn Marino   /* Concatenate all the agent expressions that are commands into the
8049ef5ccd6cSJohn Marino      cmds parameter.  */
8050ef5ccd6cSJohn Marino   for (ix = 0;
8051ef5ccd6cSJohn Marino        VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr);
8052ef5ccd6cSJohn Marino        ix++)
8053ef5ccd6cSJohn Marino     {
8054ef5ccd6cSJohn Marino       sprintf (buf, "X%x,", aexpr->len);
8055ef5ccd6cSJohn Marino       buf += strlen (buf);
8056ef5ccd6cSJohn Marino       for (i = 0; i < aexpr->len; ++i)
8057ef5ccd6cSJohn Marino 	buf = pack_hex_byte (buf, aexpr->buf[i]);
8058ef5ccd6cSJohn Marino       *buf = '\0';
8059ef5ccd6cSJohn Marino     }
8060ef5ccd6cSJohn Marino 
8061ef5ccd6cSJohn Marino   VEC_free (agent_expr_p, bp_tgt->tcommands);
8062ef5ccd6cSJohn Marino }
8063ef5ccd6cSJohn Marino 
80645796c8dcSSimon Schubert /* Insert a breakpoint.  On targets that have software breakpoint
80655796c8dcSSimon Schubert    support, we ask the remote target to do the work; on targets
80665796c8dcSSimon Schubert    which don't, we insert a traditional memory breakpoint.  */
80675796c8dcSSimon Schubert 
80685796c8dcSSimon Schubert static int
remote_insert_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)80695796c8dcSSimon Schubert remote_insert_breakpoint (struct gdbarch *gdbarch,
80705796c8dcSSimon Schubert 			  struct bp_target_info *bp_tgt)
80715796c8dcSSimon Schubert {
80725796c8dcSSimon Schubert   /* Try the "Z" s/w breakpoint packet if it is not already disabled.
80735796c8dcSSimon Schubert      If it succeeds, then set the support to PACKET_ENABLE.  If it
80745796c8dcSSimon Schubert      fails, and the user has explicitly requested the Z support then
80755796c8dcSSimon Schubert      report an error, otherwise, mark it disabled and go on.  */
80765796c8dcSSimon Schubert 
80775796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
80785796c8dcSSimon Schubert     {
80795796c8dcSSimon Schubert       CORE_ADDR addr = bp_tgt->placed_address;
80805796c8dcSSimon Schubert       struct remote_state *rs;
8081ef5ccd6cSJohn Marino       char *p, *endbuf;
80825796c8dcSSimon Schubert       int bpsize;
8083ef5ccd6cSJohn Marino       struct condition_list *cond = NULL;
80845796c8dcSSimon Schubert 
8085cf7f2e2dSJohn Marino       gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
80865796c8dcSSimon Schubert 
80875796c8dcSSimon Schubert       rs = get_remote_state ();
80885796c8dcSSimon Schubert       p = rs->buf;
8089ef5ccd6cSJohn Marino       endbuf = rs->buf + get_remote_packet_size ();
80905796c8dcSSimon Schubert 
80915796c8dcSSimon Schubert       *(p++) = 'Z';
80925796c8dcSSimon Schubert       *(p++) = '0';
80935796c8dcSSimon Schubert       *(p++) = ',';
80945796c8dcSSimon Schubert       addr = (ULONGEST) remote_address_masked (addr);
80955796c8dcSSimon Schubert       p += hexnumstr (p, addr);
8096ef5ccd6cSJohn Marino       xsnprintf (p, endbuf - p, ",%d", bpsize);
8097ef5ccd6cSJohn Marino 
8098ef5ccd6cSJohn Marino       if (remote_supports_cond_breakpoints ())
8099ef5ccd6cSJohn Marino 	remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8100ef5ccd6cSJohn Marino 
8101ef5ccd6cSJohn Marino       if (remote_can_run_breakpoint_commands ())
8102ef5ccd6cSJohn Marino 	remote_add_target_side_commands (gdbarch, bp_tgt, p);
81035796c8dcSSimon Schubert 
81045796c8dcSSimon Schubert       putpkt (rs->buf);
81055796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
81065796c8dcSSimon Schubert 
81075796c8dcSSimon Schubert       switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
81085796c8dcSSimon Schubert 	{
81095796c8dcSSimon Schubert 	case PACKET_ERROR:
81105796c8dcSSimon Schubert 	  return -1;
81115796c8dcSSimon Schubert 	case PACKET_OK:
81125796c8dcSSimon Schubert 	  bp_tgt->placed_address = addr;
81135796c8dcSSimon Schubert 	  bp_tgt->placed_size = bpsize;
81145796c8dcSSimon Schubert 	  return 0;
81155796c8dcSSimon Schubert 	case PACKET_UNKNOWN:
81165796c8dcSSimon Schubert 	  break;
81175796c8dcSSimon Schubert 	}
81185796c8dcSSimon Schubert     }
81195796c8dcSSimon Schubert 
81205796c8dcSSimon Schubert   return memory_insert_breakpoint (gdbarch, bp_tgt);
81215796c8dcSSimon Schubert }
81225796c8dcSSimon Schubert 
81235796c8dcSSimon Schubert static int
remote_remove_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)81245796c8dcSSimon Schubert remote_remove_breakpoint (struct gdbarch *gdbarch,
81255796c8dcSSimon Schubert 			  struct bp_target_info *bp_tgt)
81265796c8dcSSimon Schubert {
81275796c8dcSSimon Schubert   CORE_ADDR addr = bp_tgt->placed_address;
81285796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
81295796c8dcSSimon Schubert 
81305796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
81315796c8dcSSimon Schubert     {
81325796c8dcSSimon Schubert       char *p = rs->buf;
8133ef5ccd6cSJohn Marino       char *endbuf = rs->buf + get_remote_packet_size ();
81345796c8dcSSimon Schubert 
81355796c8dcSSimon Schubert       *(p++) = 'z';
81365796c8dcSSimon Schubert       *(p++) = '0';
81375796c8dcSSimon Schubert       *(p++) = ',';
81385796c8dcSSimon Schubert 
81395796c8dcSSimon Schubert       addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
81405796c8dcSSimon Schubert       p += hexnumstr (p, addr);
8141ef5ccd6cSJohn Marino       xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
81425796c8dcSSimon Schubert 
81435796c8dcSSimon Schubert       putpkt (rs->buf);
81445796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
81455796c8dcSSimon Schubert 
81465796c8dcSSimon Schubert       return (rs->buf[0] == 'E');
81475796c8dcSSimon Schubert     }
81485796c8dcSSimon Schubert 
81495796c8dcSSimon Schubert   return memory_remove_breakpoint (gdbarch, bp_tgt);
81505796c8dcSSimon Schubert }
81515796c8dcSSimon Schubert 
81525796c8dcSSimon Schubert static int
watchpoint_to_Z_packet(int type)81535796c8dcSSimon Schubert watchpoint_to_Z_packet (int type)
81545796c8dcSSimon Schubert {
81555796c8dcSSimon Schubert   switch (type)
81565796c8dcSSimon Schubert     {
81575796c8dcSSimon Schubert     case hw_write:
81585796c8dcSSimon Schubert       return Z_PACKET_WRITE_WP;
81595796c8dcSSimon Schubert       break;
81605796c8dcSSimon Schubert     case hw_read:
81615796c8dcSSimon Schubert       return Z_PACKET_READ_WP;
81625796c8dcSSimon Schubert       break;
81635796c8dcSSimon Schubert     case hw_access:
81645796c8dcSSimon Schubert       return Z_PACKET_ACCESS_WP;
81655796c8dcSSimon Schubert       break;
81665796c8dcSSimon Schubert     default:
81675796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__,
81685796c8dcSSimon Schubert 		      _("hw_bp_to_z: bad watchpoint type %d"), type);
81695796c8dcSSimon Schubert     }
81705796c8dcSSimon Schubert }
81715796c8dcSSimon Schubert 
81725796c8dcSSimon Schubert static int
remote_insert_watchpoint(CORE_ADDR addr,int len,int type,struct expression * cond)8173cf7f2e2dSJohn Marino remote_insert_watchpoint (CORE_ADDR addr, int len, int type,
8174cf7f2e2dSJohn Marino 			  struct expression *cond)
81755796c8dcSSimon Schubert {
81765796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
8177ef5ccd6cSJohn Marino   char *endbuf = rs->buf + get_remote_packet_size ();
81785796c8dcSSimon Schubert   char *p;
81795796c8dcSSimon Schubert   enum Z_packet_type packet = watchpoint_to_Z_packet (type);
81805796c8dcSSimon Schubert 
81815796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
8182cf7f2e2dSJohn Marino     return 1;
81835796c8dcSSimon Schubert 
8184ef5ccd6cSJohn Marino   xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
81855796c8dcSSimon Schubert   p = strchr (rs->buf, '\0');
81865796c8dcSSimon Schubert   addr = remote_address_masked (addr);
81875796c8dcSSimon Schubert   p += hexnumstr (p, (ULONGEST) addr);
8188ef5ccd6cSJohn Marino   xsnprintf (p, endbuf - p, ",%x", len);
81895796c8dcSSimon Schubert 
81905796c8dcSSimon Schubert   putpkt (rs->buf);
81915796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
81925796c8dcSSimon Schubert 
81935796c8dcSSimon Schubert   switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
81945796c8dcSSimon Schubert     {
81955796c8dcSSimon Schubert     case PACKET_ERROR:
81965796c8dcSSimon Schubert       return -1;
8197cf7f2e2dSJohn Marino     case PACKET_UNKNOWN:
8198cf7f2e2dSJohn Marino       return 1;
81995796c8dcSSimon Schubert     case PACKET_OK:
82005796c8dcSSimon Schubert       return 0;
82015796c8dcSSimon Schubert     }
82025796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
82035796c8dcSSimon Schubert 		  _("remote_insert_watchpoint: reached end of function"));
82045796c8dcSSimon Schubert }
82055796c8dcSSimon Schubert 
8206ef5ccd6cSJohn Marino static int
remote_watchpoint_addr_within_range(struct target_ops * target,CORE_ADDR addr,CORE_ADDR start,int length)8207ef5ccd6cSJohn Marino remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
8208ef5ccd6cSJohn Marino 				     CORE_ADDR start, int length)
8209ef5ccd6cSJohn Marino {
8210ef5ccd6cSJohn Marino   CORE_ADDR diff = remote_address_masked (addr - start);
8211ef5ccd6cSJohn Marino 
8212ef5ccd6cSJohn Marino   return diff < length;
8213ef5ccd6cSJohn Marino }
8214ef5ccd6cSJohn Marino 
82155796c8dcSSimon Schubert 
82165796c8dcSSimon Schubert static int
remote_remove_watchpoint(CORE_ADDR addr,int len,int type,struct expression * cond)8217cf7f2e2dSJohn Marino remote_remove_watchpoint (CORE_ADDR addr, int len, int type,
8218cf7f2e2dSJohn Marino 			  struct expression *cond)
82195796c8dcSSimon Schubert {
82205796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
8221ef5ccd6cSJohn Marino   char *endbuf = rs->buf + get_remote_packet_size ();
82225796c8dcSSimon Schubert   char *p;
82235796c8dcSSimon Schubert   enum Z_packet_type packet = watchpoint_to_Z_packet (type);
82245796c8dcSSimon Schubert 
82255796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
82265796c8dcSSimon Schubert     return -1;
82275796c8dcSSimon Schubert 
8228ef5ccd6cSJohn Marino   xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
82295796c8dcSSimon Schubert   p = strchr (rs->buf, '\0');
82305796c8dcSSimon Schubert   addr = remote_address_masked (addr);
82315796c8dcSSimon Schubert   p += hexnumstr (p, (ULONGEST) addr);
8232ef5ccd6cSJohn Marino   xsnprintf (p, endbuf - p, ",%x", len);
82335796c8dcSSimon Schubert   putpkt (rs->buf);
82345796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
82355796c8dcSSimon Schubert 
82365796c8dcSSimon Schubert   switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
82375796c8dcSSimon Schubert     {
82385796c8dcSSimon Schubert     case PACKET_ERROR:
82395796c8dcSSimon Schubert     case PACKET_UNKNOWN:
82405796c8dcSSimon Schubert       return -1;
82415796c8dcSSimon Schubert     case PACKET_OK:
82425796c8dcSSimon Schubert       return 0;
82435796c8dcSSimon Schubert     }
82445796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
82455796c8dcSSimon Schubert 		  _("remote_remove_watchpoint: reached end of function"));
82465796c8dcSSimon Schubert }
82475796c8dcSSimon Schubert 
82485796c8dcSSimon Schubert 
82495796c8dcSSimon Schubert int remote_hw_watchpoint_limit = -1;
8250a45ae5f8SJohn Marino int remote_hw_watchpoint_length_limit = -1;
82515796c8dcSSimon Schubert int remote_hw_breakpoint_limit = -1;
82525796c8dcSSimon Schubert 
82535796c8dcSSimon Schubert static int
remote_region_ok_for_hw_watchpoint(CORE_ADDR addr,int len)8254a45ae5f8SJohn Marino remote_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
8255a45ae5f8SJohn Marino {
8256a45ae5f8SJohn Marino   if (remote_hw_watchpoint_length_limit == 0)
8257a45ae5f8SJohn Marino     return 0;
8258a45ae5f8SJohn Marino   else if (remote_hw_watchpoint_length_limit < 0)
8259a45ae5f8SJohn Marino     return 1;
8260a45ae5f8SJohn Marino   else if (len <= remote_hw_watchpoint_length_limit)
8261a45ae5f8SJohn Marino     return 1;
8262a45ae5f8SJohn Marino   else
8263a45ae5f8SJohn Marino     return 0;
8264a45ae5f8SJohn Marino }
8265a45ae5f8SJohn Marino 
8266a45ae5f8SJohn Marino static int
remote_check_watch_resources(int type,int cnt,int ot)82675796c8dcSSimon Schubert remote_check_watch_resources (int type, int cnt, int ot)
82685796c8dcSSimon Schubert {
82695796c8dcSSimon Schubert   if (type == bp_hardware_breakpoint)
82705796c8dcSSimon Schubert     {
82715796c8dcSSimon Schubert       if (remote_hw_breakpoint_limit == 0)
82725796c8dcSSimon Schubert 	return 0;
82735796c8dcSSimon Schubert       else if (remote_hw_breakpoint_limit < 0)
82745796c8dcSSimon Schubert 	return 1;
82755796c8dcSSimon Schubert       else if (cnt <= remote_hw_breakpoint_limit)
82765796c8dcSSimon Schubert 	return 1;
82775796c8dcSSimon Schubert     }
82785796c8dcSSimon Schubert   else
82795796c8dcSSimon Schubert     {
82805796c8dcSSimon Schubert       if (remote_hw_watchpoint_limit == 0)
82815796c8dcSSimon Schubert 	return 0;
82825796c8dcSSimon Schubert       else if (remote_hw_watchpoint_limit < 0)
82835796c8dcSSimon Schubert 	return 1;
82845796c8dcSSimon Schubert       else if (ot)
82855796c8dcSSimon Schubert 	return -1;
82865796c8dcSSimon Schubert       else if (cnt <= remote_hw_watchpoint_limit)
82875796c8dcSSimon Schubert 	return 1;
82885796c8dcSSimon Schubert     }
82895796c8dcSSimon Schubert   return -1;
82905796c8dcSSimon Schubert }
82915796c8dcSSimon Schubert 
82925796c8dcSSimon Schubert static int
remote_stopped_by_watchpoint(void)82935796c8dcSSimon Schubert remote_stopped_by_watchpoint (void)
82945796c8dcSSimon Schubert {
82955796c8dcSSimon Schubert   return remote_stopped_by_watchpoint_p;
82965796c8dcSSimon Schubert }
82975796c8dcSSimon Schubert 
82985796c8dcSSimon Schubert static int
remote_stopped_data_address(struct target_ops * target,CORE_ADDR * addr_p)82995796c8dcSSimon Schubert remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
83005796c8dcSSimon Schubert {
83015796c8dcSSimon Schubert   int rc = 0;
8302cf7f2e2dSJohn Marino 
83035796c8dcSSimon Schubert   if (remote_stopped_by_watchpoint ())
83045796c8dcSSimon Schubert     {
83055796c8dcSSimon Schubert       *addr_p = remote_watch_data_address;
83065796c8dcSSimon Schubert       rc = 1;
83075796c8dcSSimon Schubert     }
83085796c8dcSSimon Schubert 
83095796c8dcSSimon Schubert   return rc;
83105796c8dcSSimon Schubert }
83115796c8dcSSimon Schubert 
83125796c8dcSSimon Schubert 
83135796c8dcSSimon Schubert static int
remote_insert_hw_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)83145796c8dcSSimon Schubert remote_insert_hw_breakpoint (struct gdbarch *gdbarch,
83155796c8dcSSimon Schubert 			     struct bp_target_info *bp_tgt)
83165796c8dcSSimon Schubert {
83175796c8dcSSimon Schubert   CORE_ADDR addr;
83185796c8dcSSimon Schubert   struct remote_state *rs;
8319ef5ccd6cSJohn Marino   char *p, *endbuf;
8320ef5ccd6cSJohn Marino   char *message;
83215796c8dcSSimon Schubert 
83225796c8dcSSimon Schubert   /* The length field should be set to the size of a breakpoint
83235796c8dcSSimon Schubert      instruction, even though we aren't inserting one ourselves.  */
83245796c8dcSSimon Schubert 
8325cf7f2e2dSJohn Marino   gdbarch_remote_breakpoint_from_pc
83265796c8dcSSimon Schubert     (gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size);
83275796c8dcSSimon Schubert 
83285796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
83295796c8dcSSimon Schubert     return -1;
83305796c8dcSSimon Schubert 
83315796c8dcSSimon Schubert   rs = get_remote_state ();
83325796c8dcSSimon Schubert   p = rs->buf;
8333ef5ccd6cSJohn Marino   endbuf = rs->buf + get_remote_packet_size ();
83345796c8dcSSimon Schubert 
83355796c8dcSSimon Schubert   *(p++) = 'Z';
83365796c8dcSSimon Schubert   *(p++) = '1';
83375796c8dcSSimon Schubert   *(p++) = ',';
83385796c8dcSSimon Schubert 
83395796c8dcSSimon Schubert   addr = remote_address_masked (bp_tgt->placed_address);
83405796c8dcSSimon Schubert   p += hexnumstr (p, (ULONGEST) addr);
8341ef5ccd6cSJohn Marino   xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
8342ef5ccd6cSJohn Marino 
8343ef5ccd6cSJohn Marino   if (remote_supports_cond_breakpoints ())
8344ef5ccd6cSJohn Marino     remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
8345ef5ccd6cSJohn Marino 
8346ef5ccd6cSJohn Marino   if (remote_can_run_breakpoint_commands ())
8347ef5ccd6cSJohn Marino     remote_add_target_side_commands (gdbarch, bp_tgt, p);
83485796c8dcSSimon Schubert 
83495796c8dcSSimon Schubert   putpkt (rs->buf);
83505796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
83515796c8dcSSimon Schubert 
83525796c8dcSSimon Schubert   switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
83535796c8dcSSimon Schubert     {
83545796c8dcSSimon Schubert     case PACKET_ERROR:
8355ef5ccd6cSJohn Marino       if (rs->buf[1] == '.')
8356ef5ccd6cSJohn Marino         {
8357ef5ccd6cSJohn Marino           message = strchr (rs->buf + 2, '.');
8358ef5ccd6cSJohn Marino           if (message)
8359ef5ccd6cSJohn Marino             error (_("Remote failure reply: %s"), message + 1);
8360ef5ccd6cSJohn Marino         }
8361ef5ccd6cSJohn Marino       return -1;
83625796c8dcSSimon Schubert     case PACKET_UNKNOWN:
83635796c8dcSSimon Schubert       return -1;
83645796c8dcSSimon Schubert     case PACKET_OK:
83655796c8dcSSimon Schubert       return 0;
83665796c8dcSSimon Schubert     }
83675796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
83685796c8dcSSimon Schubert 		  _("remote_insert_hw_breakpoint: reached end of function"));
83695796c8dcSSimon Schubert }
83705796c8dcSSimon Schubert 
83715796c8dcSSimon Schubert 
83725796c8dcSSimon Schubert static int
remote_remove_hw_breakpoint(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)83735796c8dcSSimon Schubert remote_remove_hw_breakpoint (struct gdbarch *gdbarch,
83745796c8dcSSimon Schubert 			     struct bp_target_info *bp_tgt)
83755796c8dcSSimon Schubert {
83765796c8dcSSimon Schubert   CORE_ADDR addr;
83775796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
83785796c8dcSSimon Schubert   char *p = rs->buf;
8379ef5ccd6cSJohn Marino   char *endbuf = rs->buf + get_remote_packet_size ();
83805796c8dcSSimon Schubert 
83815796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
83825796c8dcSSimon Schubert     return -1;
83835796c8dcSSimon Schubert 
83845796c8dcSSimon Schubert   *(p++) = 'z';
83855796c8dcSSimon Schubert   *(p++) = '1';
83865796c8dcSSimon Schubert   *(p++) = ',';
83875796c8dcSSimon Schubert 
83885796c8dcSSimon Schubert   addr = remote_address_masked (bp_tgt->placed_address);
83895796c8dcSSimon Schubert   p += hexnumstr (p, (ULONGEST) addr);
8390ef5ccd6cSJohn Marino   xsnprintf (p, endbuf  - p, ",%x", bp_tgt->placed_size);
83915796c8dcSSimon Schubert 
83925796c8dcSSimon Schubert   putpkt (rs->buf);
83935796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
83945796c8dcSSimon Schubert 
83955796c8dcSSimon Schubert   switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
83965796c8dcSSimon Schubert     {
83975796c8dcSSimon Schubert     case PACKET_ERROR:
83985796c8dcSSimon Schubert     case PACKET_UNKNOWN:
83995796c8dcSSimon Schubert       return -1;
84005796c8dcSSimon Schubert     case PACKET_OK:
84015796c8dcSSimon Schubert       return 0;
84025796c8dcSSimon Schubert     }
84035796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
84045796c8dcSSimon Schubert 		  _("remote_remove_hw_breakpoint: reached end of function"));
84055796c8dcSSimon Schubert }
84065796c8dcSSimon Schubert 
84075796c8dcSSimon Schubert /* Table used by the crc32 function to calcuate the checksum.  */
84085796c8dcSSimon Schubert 
84095796c8dcSSimon Schubert static unsigned long crc32_table[256] =
84105796c8dcSSimon Schubert {0, 0};
84115796c8dcSSimon Schubert 
84125796c8dcSSimon Schubert static unsigned long
crc32(const unsigned char * buf,int len,unsigned int crc)8413cf7f2e2dSJohn Marino crc32 (const unsigned char *buf, int len, unsigned int crc)
84145796c8dcSSimon Schubert {
84155796c8dcSSimon Schubert   if (!crc32_table[1])
84165796c8dcSSimon Schubert     {
84175796c8dcSSimon Schubert       /* Initialize the CRC table and the decoding table.  */
84185796c8dcSSimon Schubert       int i, j;
84195796c8dcSSimon Schubert       unsigned int c;
84205796c8dcSSimon Schubert 
84215796c8dcSSimon Schubert       for (i = 0; i < 256; i++)
84225796c8dcSSimon Schubert 	{
84235796c8dcSSimon Schubert 	  for (c = i << 24, j = 8; j > 0; --j)
84245796c8dcSSimon Schubert 	    c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
84255796c8dcSSimon Schubert 	  crc32_table[i] = c;
84265796c8dcSSimon Schubert 	}
84275796c8dcSSimon Schubert     }
84285796c8dcSSimon Schubert 
84295796c8dcSSimon Schubert   while (len--)
84305796c8dcSSimon Schubert     {
84315796c8dcSSimon Schubert       crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
84325796c8dcSSimon Schubert       buf++;
84335796c8dcSSimon Schubert     }
84345796c8dcSSimon Schubert   return crc;
84355796c8dcSSimon Schubert }
84365796c8dcSSimon Schubert 
8437cf7f2e2dSJohn Marino /* Verify memory using the "qCRC:" request.  */
8438cf7f2e2dSJohn Marino 
8439cf7f2e2dSJohn Marino static int
remote_verify_memory(struct target_ops * ops,const gdb_byte * data,CORE_ADDR lma,ULONGEST size)8440cf7f2e2dSJohn Marino remote_verify_memory (struct target_ops *ops,
8441cf7f2e2dSJohn Marino 		      const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
8442cf7f2e2dSJohn Marino {
8443cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
8444cf7f2e2dSJohn Marino   unsigned long host_crc, target_crc;
8445cf7f2e2dSJohn Marino   char *tmp;
8446cf7f2e2dSJohn Marino 
8447cf7f2e2dSJohn Marino   /* FIXME: assumes lma can fit into long.  */
8448cf7f2e2dSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
8449cf7f2e2dSJohn Marino 	     (long) lma, (long) size);
8450cf7f2e2dSJohn Marino   putpkt (rs->buf);
8451cf7f2e2dSJohn Marino 
8452cf7f2e2dSJohn Marino   /* Be clever; compute the host_crc before waiting for target
8453cf7f2e2dSJohn Marino      reply.  */
8454cf7f2e2dSJohn Marino   host_crc = crc32 (data, size, 0xffffffff);
8455cf7f2e2dSJohn Marino 
8456cf7f2e2dSJohn Marino   getpkt (&rs->buf, &rs->buf_size, 0);
8457cf7f2e2dSJohn Marino   if (rs->buf[0] == 'E')
8458cf7f2e2dSJohn Marino     return -1;
8459cf7f2e2dSJohn Marino 
8460cf7f2e2dSJohn Marino   if (rs->buf[0] != 'C')
8461cf7f2e2dSJohn Marino     error (_("remote target does not support this operation"));
8462cf7f2e2dSJohn Marino 
8463cf7f2e2dSJohn Marino   for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
8464cf7f2e2dSJohn Marino     target_crc = target_crc * 16 + fromhex (*tmp);
8465cf7f2e2dSJohn Marino 
8466cf7f2e2dSJohn Marino   return (host_crc == target_crc);
8467cf7f2e2dSJohn Marino }
8468cf7f2e2dSJohn Marino 
84695796c8dcSSimon Schubert /* compare-sections command
84705796c8dcSSimon Schubert 
84715796c8dcSSimon Schubert    With no arguments, compares each loadable section in the exec bfd
84725796c8dcSSimon Schubert    with the same memory range on the target, and reports mismatches.
8473cf7f2e2dSJohn Marino    Useful for verifying the image on the target against the exec file.  */
84745796c8dcSSimon Schubert 
84755796c8dcSSimon Schubert static void
compare_sections_command(char * args,int from_tty)84765796c8dcSSimon Schubert compare_sections_command (char *args, int from_tty)
84775796c8dcSSimon Schubert {
84785796c8dcSSimon Schubert   asection *s;
84795796c8dcSSimon Schubert   struct cleanup *old_chain;
84805796c8dcSSimon Schubert   char *sectdata;
84815796c8dcSSimon Schubert   const char *sectname;
84825796c8dcSSimon Schubert   bfd_size_type size;
84835796c8dcSSimon Schubert   bfd_vma lma;
84845796c8dcSSimon Schubert   int matched = 0;
84855796c8dcSSimon Schubert   int mismatched = 0;
8486cf7f2e2dSJohn Marino   int res;
84875796c8dcSSimon Schubert 
84885796c8dcSSimon Schubert   if (!exec_bfd)
84895796c8dcSSimon Schubert     error (_("command cannot be used without an exec file"));
84905796c8dcSSimon Schubert 
84915796c8dcSSimon Schubert   for (s = exec_bfd->sections; s; s = s->next)
84925796c8dcSSimon Schubert     {
84935796c8dcSSimon Schubert       if (!(s->flags & SEC_LOAD))
8494c50c785cSJohn Marino 	continue;		/* Skip non-loadable section.  */
84955796c8dcSSimon Schubert 
84965796c8dcSSimon Schubert       size = bfd_get_section_size (s);
84975796c8dcSSimon Schubert       if (size == 0)
8498c50c785cSJohn Marino 	continue;		/* Skip zero-length section.  */
84995796c8dcSSimon Schubert 
85005796c8dcSSimon Schubert       sectname = bfd_get_section_name (exec_bfd, s);
85015796c8dcSSimon Schubert       if (args && strcmp (args, sectname) != 0)
8502c50c785cSJohn Marino 	continue;		/* Not the section selected by user.  */
85035796c8dcSSimon Schubert 
8504c50c785cSJohn Marino       matched = 1;		/* Do this section.  */
85055796c8dcSSimon Schubert       lma = s->lma;
85065796c8dcSSimon Schubert 
85075796c8dcSSimon Schubert       sectdata = xmalloc (size);
85085796c8dcSSimon Schubert       old_chain = make_cleanup (xfree, sectdata);
85095796c8dcSSimon Schubert       bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
85105796c8dcSSimon Schubert 
8511cf7f2e2dSJohn Marino       res = target_verify_memory (sectdata, lma, size);
8512cf7f2e2dSJohn Marino 
8513cf7f2e2dSJohn Marino       if (res == -1)
85145796c8dcSSimon Schubert 	error (_("target memory fault, section %s, range %s -- %s"), sectname,
8515ef5ccd6cSJohn Marino 	       paddress (target_gdbarch (), lma),
8516ef5ccd6cSJohn Marino 	       paddress (target_gdbarch (), lma + size));
85175796c8dcSSimon Schubert 
85185796c8dcSSimon Schubert       printf_filtered ("Section %s, range %s -- %s: ", sectname,
8519ef5ccd6cSJohn Marino 		       paddress (target_gdbarch (), lma),
8520ef5ccd6cSJohn Marino 		       paddress (target_gdbarch (), lma + size));
8521cf7f2e2dSJohn Marino       if (res)
85225796c8dcSSimon Schubert 	printf_filtered ("matched.\n");
85235796c8dcSSimon Schubert       else
85245796c8dcSSimon Schubert 	{
85255796c8dcSSimon Schubert 	  printf_filtered ("MIS-MATCHED!\n");
85265796c8dcSSimon Schubert 	  mismatched++;
85275796c8dcSSimon Schubert 	}
85285796c8dcSSimon Schubert 
85295796c8dcSSimon Schubert       do_cleanups (old_chain);
85305796c8dcSSimon Schubert     }
85315796c8dcSSimon Schubert   if (mismatched > 0)
85325796c8dcSSimon Schubert     warning (_("One or more sections of the remote executable does not match\n\
85335796c8dcSSimon Schubert the loaded file\n"));
85345796c8dcSSimon Schubert   if (args && !matched)
85355796c8dcSSimon Schubert     printf_filtered (_("No loaded section named '%s'.\n"), args);
85365796c8dcSSimon Schubert }
85375796c8dcSSimon Schubert 
85385796c8dcSSimon Schubert /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
85395796c8dcSSimon Schubert    into remote target.  The number of bytes written to the remote
85405796c8dcSSimon Schubert    target is returned, or -1 for error.  */
85415796c8dcSSimon Schubert 
85425796c8dcSSimon Schubert static LONGEST
remote_write_qxfer(struct target_ops * ops,const char * object_name,const char * annex,const gdb_byte * writebuf,ULONGEST offset,LONGEST len,struct packet_config * packet)85435796c8dcSSimon Schubert remote_write_qxfer (struct target_ops *ops, const char *object_name,
85445796c8dcSSimon Schubert                     const char *annex, const gdb_byte *writebuf,
85455796c8dcSSimon Schubert                     ULONGEST offset, LONGEST len,
85465796c8dcSSimon Schubert                     struct packet_config *packet)
85475796c8dcSSimon Schubert {
85485796c8dcSSimon Schubert   int i, buf_len;
85495796c8dcSSimon Schubert   ULONGEST n;
85505796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
85515796c8dcSSimon Schubert   int max_size = get_memory_write_packet_size ();
85525796c8dcSSimon Schubert 
85535796c8dcSSimon Schubert   if (packet->support == PACKET_DISABLE)
85545796c8dcSSimon Schubert     return -1;
85555796c8dcSSimon Schubert 
85565796c8dcSSimon Schubert   /* Insert header.  */
85575796c8dcSSimon Schubert   i = snprintf (rs->buf, max_size,
85585796c8dcSSimon Schubert 		"qXfer:%s:write:%s:%s:",
85595796c8dcSSimon Schubert 		object_name, annex ? annex : "",
85605796c8dcSSimon Schubert 		phex_nz (offset, sizeof offset));
85615796c8dcSSimon Schubert   max_size -= (i + 1);
85625796c8dcSSimon Schubert 
85635796c8dcSSimon Schubert   /* Escape as much data as fits into rs->buf.  */
85645796c8dcSSimon Schubert   buf_len = remote_escape_output
85655796c8dcSSimon Schubert     (writebuf, len, (rs->buf + i), &max_size, max_size);
85665796c8dcSSimon Schubert 
85675796c8dcSSimon Schubert   if (putpkt_binary (rs->buf, i + buf_len) < 0
85685796c8dcSSimon Schubert       || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
85695796c8dcSSimon Schubert       || packet_ok (rs->buf, packet) != PACKET_OK)
85705796c8dcSSimon Schubert     return -1;
85715796c8dcSSimon Schubert 
85725796c8dcSSimon Schubert   unpack_varlen_hex (rs->buf, &n);
85735796c8dcSSimon Schubert   return n;
85745796c8dcSSimon Schubert }
85755796c8dcSSimon Schubert 
85765796c8dcSSimon Schubert /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
85775796c8dcSSimon Schubert    Data at OFFSET, of up to LEN bytes, is read into READBUF; the
85785796c8dcSSimon Schubert    number of bytes read is returned, or 0 for EOF, or -1 for error.
85795796c8dcSSimon Schubert    The number of bytes read may be less than LEN without indicating an
85805796c8dcSSimon Schubert    EOF.  PACKET is checked and updated to indicate whether the remote
85815796c8dcSSimon Schubert    target supports this object.  */
85825796c8dcSSimon Schubert 
85835796c8dcSSimon Schubert static LONGEST
remote_read_qxfer(struct target_ops * ops,const char * object_name,const char * annex,gdb_byte * readbuf,ULONGEST offset,LONGEST len,struct packet_config * packet)85845796c8dcSSimon Schubert remote_read_qxfer (struct target_ops *ops, const char *object_name,
85855796c8dcSSimon Schubert 		   const char *annex,
85865796c8dcSSimon Schubert 		   gdb_byte *readbuf, ULONGEST offset, LONGEST len,
85875796c8dcSSimon Schubert 		   struct packet_config *packet)
85885796c8dcSSimon Schubert {
85895796c8dcSSimon Schubert   static char *finished_object;
85905796c8dcSSimon Schubert   static char *finished_annex;
85915796c8dcSSimon Schubert   static ULONGEST finished_offset;
85925796c8dcSSimon Schubert 
85935796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
85945796c8dcSSimon Schubert   LONGEST i, n, packet_len;
85955796c8dcSSimon Schubert 
85965796c8dcSSimon Schubert   if (packet->support == PACKET_DISABLE)
85975796c8dcSSimon Schubert     return -1;
85985796c8dcSSimon Schubert 
85995796c8dcSSimon Schubert   /* Check whether we've cached an end-of-object packet that matches
86005796c8dcSSimon Schubert      this request.  */
86015796c8dcSSimon Schubert   if (finished_object)
86025796c8dcSSimon Schubert     {
86035796c8dcSSimon Schubert       if (strcmp (object_name, finished_object) == 0
86045796c8dcSSimon Schubert 	  && strcmp (annex ? annex : "", finished_annex) == 0
86055796c8dcSSimon Schubert 	  && offset == finished_offset)
86065796c8dcSSimon Schubert 	return 0;
86075796c8dcSSimon Schubert 
86085796c8dcSSimon Schubert       /* Otherwise, we're now reading something different.  Discard
86095796c8dcSSimon Schubert 	 the cache.  */
86105796c8dcSSimon Schubert       xfree (finished_object);
86115796c8dcSSimon Schubert       xfree (finished_annex);
86125796c8dcSSimon Schubert       finished_object = NULL;
86135796c8dcSSimon Schubert       finished_annex = NULL;
86145796c8dcSSimon Schubert     }
86155796c8dcSSimon Schubert 
86165796c8dcSSimon Schubert   /* Request only enough to fit in a single packet.  The actual data
86175796c8dcSSimon Schubert      may not, since we don't know how much of it will need to be escaped;
86185796c8dcSSimon Schubert      the target is free to respond with slightly less data.  We subtract
86195796c8dcSSimon Schubert      five to account for the response type and the protocol frame.  */
86205796c8dcSSimon Schubert   n = min (get_remote_packet_size () - 5, len);
86215796c8dcSSimon Schubert   snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
86225796c8dcSSimon Schubert 	    object_name, annex ? annex : "",
86235796c8dcSSimon Schubert 	    phex_nz (offset, sizeof offset),
86245796c8dcSSimon Schubert 	    phex_nz (n, sizeof n));
86255796c8dcSSimon Schubert   i = putpkt (rs->buf);
86265796c8dcSSimon Schubert   if (i < 0)
86275796c8dcSSimon Schubert     return -1;
86285796c8dcSSimon Schubert 
86295796c8dcSSimon Schubert   rs->buf[0] = '\0';
86305796c8dcSSimon Schubert   packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
86315796c8dcSSimon Schubert   if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
86325796c8dcSSimon Schubert     return -1;
86335796c8dcSSimon Schubert 
86345796c8dcSSimon Schubert   if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
86355796c8dcSSimon Schubert     error (_("Unknown remote qXfer reply: %s"), rs->buf);
86365796c8dcSSimon Schubert 
86375796c8dcSSimon Schubert   /* 'm' means there is (or at least might be) more data after this
86385796c8dcSSimon Schubert      batch.  That does not make sense unless there's at least one byte
86395796c8dcSSimon Schubert      of data in this reply.  */
86405796c8dcSSimon Schubert   if (rs->buf[0] == 'm' && packet_len == 1)
86415796c8dcSSimon Schubert     error (_("Remote qXfer reply contained no data."));
86425796c8dcSSimon Schubert 
86435796c8dcSSimon Schubert   /* Got some data.  */
86445796c8dcSSimon Schubert   i = remote_unescape_input (rs->buf + 1, packet_len - 1, readbuf, n);
86455796c8dcSSimon Schubert 
86465796c8dcSSimon Schubert   /* 'l' is an EOF marker, possibly including a final block of data,
86475796c8dcSSimon Schubert      or possibly empty.  If we have the final block of a non-empty
86485796c8dcSSimon Schubert      object, record this fact to bypass a subsequent partial read.  */
86495796c8dcSSimon Schubert   if (rs->buf[0] == 'l' && offset + i > 0)
86505796c8dcSSimon Schubert     {
86515796c8dcSSimon Schubert       finished_object = xstrdup (object_name);
86525796c8dcSSimon Schubert       finished_annex = xstrdup (annex ? annex : "");
86535796c8dcSSimon Schubert       finished_offset = offset + i;
86545796c8dcSSimon Schubert     }
86555796c8dcSSimon Schubert 
86565796c8dcSSimon Schubert   return i;
86575796c8dcSSimon Schubert }
86585796c8dcSSimon Schubert 
86595796c8dcSSimon Schubert static LONGEST
remote_xfer_partial(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte * readbuf,const gdb_byte * writebuf,ULONGEST offset,LONGEST len)86605796c8dcSSimon Schubert remote_xfer_partial (struct target_ops *ops, enum target_object object,
86615796c8dcSSimon Schubert 		     const char *annex, gdb_byte *readbuf,
86625796c8dcSSimon Schubert 		     const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
86635796c8dcSSimon Schubert {
86645796c8dcSSimon Schubert   struct remote_state *rs;
86655796c8dcSSimon Schubert   int i;
86665796c8dcSSimon Schubert   char *p2;
86675796c8dcSSimon Schubert   char query_type;
86685796c8dcSSimon Schubert 
8669c50c785cSJohn Marino   set_remote_traceframe ();
86705796c8dcSSimon Schubert   set_general_thread (inferior_ptid);
86715796c8dcSSimon Schubert 
86725796c8dcSSimon Schubert   rs = get_remote_state ();
86735796c8dcSSimon Schubert 
86745796c8dcSSimon Schubert   /* Handle memory using the standard memory routines.  */
86755796c8dcSSimon Schubert   if (object == TARGET_OBJECT_MEMORY)
86765796c8dcSSimon Schubert     {
86775796c8dcSSimon Schubert       int xfered;
8678cf7f2e2dSJohn Marino 
86795796c8dcSSimon Schubert       errno = 0;
86805796c8dcSSimon Schubert 
86815796c8dcSSimon Schubert       /* If the remote target is connected but not running, we should
86825796c8dcSSimon Schubert 	 pass this request down to a lower stratum (e.g. the executable
86835796c8dcSSimon Schubert 	 file).  */
86845796c8dcSSimon Schubert       if (!target_has_execution)
86855796c8dcSSimon Schubert 	return 0;
86865796c8dcSSimon Schubert 
86875796c8dcSSimon Schubert       if (writebuf != NULL)
86885796c8dcSSimon Schubert 	xfered = remote_write_bytes (offset, writebuf, len);
86895796c8dcSSimon Schubert       else
86905796c8dcSSimon Schubert 	xfered = remote_read_bytes (offset, readbuf, len);
86915796c8dcSSimon Schubert 
86925796c8dcSSimon Schubert       if (xfered > 0)
86935796c8dcSSimon Schubert 	return xfered;
86945796c8dcSSimon Schubert       else if (xfered == 0 && errno == 0)
86955796c8dcSSimon Schubert 	return 0;
86965796c8dcSSimon Schubert       else
86975796c8dcSSimon Schubert 	return -1;
86985796c8dcSSimon Schubert     }
86995796c8dcSSimon Schubert 
87005796c8dcSSimon Schubert   /* Handle SPU memory using qxfer packets.  */
87015796c8dcSSimon Schubert   if (object == TARGET_OBJECT_SPU)
87025796c8dcSSimon Schubert     {
87035796c8dcSSimon Schubert       if (readbuf)
87045796c8dcSSimon Schubert 	return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
87055796c8dcSSimon Schubert 				  &remote_protocol_packets
87065796c8dcSSimon Schubert 				    [PACKET_qXfer_spu_read]);
87075796c8dcSSimon Schubert       else
87085796c8dcSSimon Schubert 	return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
87095796c8dcSSimon Schubert 				   &remote_protocol_packets
87105796c8dcSSimon Schubert 				     [PACKET_qXfer_spu_write]);
87115796c8dcSSimon Schubert     }
87125796c8dcSSimon Schubert 
87135796c8dcSSimon Schubert   /* Handle extra signal info using qxfer packets.  */
87145796c8dcSSimon Schubert   if (object == TARGET_OBJECT_SIGNAL_INFO)
87155796c8dcSSimon Schubert     {
87165796c8dcSSimon Schubert       if (readbuf)
87175796c8dcSSimon Schubert 	return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
87185796c8dcSSimon Schubert 				  &remote_protocol_packets
87195796c8dcSSimon Schubert 				  [PACKET_qXfer_siginfo_read]);
87205796c8dcSSimon Schubert       else
8721c50c785cSJohn Marino 	return remote_write_qxfer (ops, "siginfo", annex,
8722c50c785cSJohn Marino 				   writebuf, offset, len,
87235796c8dcSSimon Schubert 				   &remote_protocol_packets
87245796c8dcSSimon Schubert 				   [PACKET_qXfer_siginfo_write]);
87255796c8dcSSimon Schubert     }
87265796c8dcSSimon Schubert 
8727cf7f2e2dSJohn Marino   if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
8728cf7f2e2dSJohn Marino     {
8729cf7f2e2dSJohn Marino       if (readbuf)
8730c50c785cSJohn Marino 	return remote_read_qxfer (ops, "statictrace", annex,
8731c50c785cSJohn Marino 				  readbuf, offset, len,
8732cf7f2e2dSJohn Marino 				  &remote_protocol_packets
8733cf7f2e2dSJohn Marino 				  [PACKET_qXfer_statictrace_read]);
8734cf7f2e2dSJohn Marino       else
8735cf7f2e2dSJohn Marino 	return -1;
8736cf7f2e2dSJohn Marino     }
8737cf7f2e2dSJohn Marino 
87385796c8dcSSimon Schubert   /* Only handle flash writes.  */
87395796c8dcSSimon Schubert   if (writebuf != NULL)
87405796c8dcSSimon Schubert     {
87415796c8dcSSimon Schubert       LONGEST xfered;
87425796c8dcSSimon Schubert 
87435796c8dcSSimon Schubert       switch (object)
87445796c8dcSSimon Schubert 	{
87455796c8dcSSimon Schubert 	case TARGET_OBJECT_FLASH:
87465796c8dcSSimon Schubert 	  xfered = remote_flash_write (ops, offset, len, writebuf);
87475796c8dcSSimon Schubert 
87485796c8dcSSimon Schubert 	  if (xfered > 0)
87495796c8dcSSimon Schubert 	    return xfered;
87505796c8dcSSimon Schubert 	  else if (xfered == 0 && errno == 0)
87515796c8dcSSimon Schubert 	    return 0;
87525796c8dcSSimon Schubert 	  else
87535796c8dcSSimon Schubert 	    return -1;
87545796c8dcSSimon Schubert 
87555796c8dcSSimon Schubert 	default:
87565796c8dcSSimon Schubert 	  return -1;
87575796c8dcSSimon Schubert 	}
87585796c8dcSSimon Schubert     }
87595796c8dcSSimon Schubert 
87605796c8dcSSimon Schubert   /* Map pre-existing objects onto letters.  DO NOT do this for new
87615796c8dcSSimon Schubert      objects!!!  Instead specify new query packets.  */
87625796c8dcSSimon Schubert   switch (object)
87635796c8dcSSimon Schubert     {
87645796c8dcSSimon Schubert     case TARGET_OBJECT_AVR:
87655796c8dcSSimon Schubert       query_type = 'R';
87665796c8dcSSimon Schubert       break;
87675796c8dcSSimon Schubert 
87685796c8dcSSimon Schubert     case TARGET_OBJECT_AUXV:
87695796c8dcSSimon Schubert       gdb_assert (annex == NULL);
87705796c8dcSSimon Schubert       return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
87715796c8dcSSimon Schubert 				&remote_protocol_packets[PACKET_qXfer_auxv]);
87725796c8dcSSimon Schubert 
87735796c8dcSSimon Schubert     case TARGET_OBJECT_AVAILABLE_FEATURES:
87745796c8dcSSimon Schubert       return remote_read_qxfer
87755796c8dcSSimon Schubert 	(ops, "features", annex, readbuf, offset, len,
87765796c8dcSSimon Schubert 	 &remote_protocol_packets[PACKET_qXfer_features]);
87775796c8dcSSimon Schubert 
87785796c8dcSSimon Schubert     case TARGET_OBJECT_LIBRARIES:
87795796c8dcSSimon Schubert       return remote_read_qxfer
87805796c8dcSSimon Schubert 	(ops, "libraries", annex, readbuf, offset, len,
87815796c8dcSSimon Schubert 	 &remote_protocol_packets[PACKET_qXfer_libraries]);
87825796c8dcSSimon Schubert 
8783a45ae5f8SJohn Marino     case TARGET_OBJECT_LIBRARIES_SVR4:
8784a45ae5f8SJohn Marino       return remote_read_qxfer
8785a45ae5f8SJohn Marino 	(ops, "libraries-svr4", annex, readbuf, offset, len,
8786a45ae5f8SJohn Marino 	 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
8787a45ae5f8SJohn Marino 
87885796c8dcSSimon Schubert     case TARGET_OBJECT_MEMORY_MAP:
87895796c8dcSSimon Schubert       gdb_assert (annex == NULL);
87905796c8dcSSimon Schubert       return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
87915796c8dcSSimon Schubert 				&remote_protocol_packets[PACKET_qXfer_memory_map]);
87925796c8dcSSimon Schubert 
87935796c8dcSSimon Schubert     case TARGET_OBJECT_OSDATA:
87945796c8dcSSimon Schubert       /* Should only get here if we're connected.  */
87955796c8dcSSimon Schubert       gdb_assert (remote_desc);
87965796c8dcSSimon Schubert       return remote_read_qxfer
87975796c8dcSSimon Schubert        (ops, "osdata", annex, readbuf, offset, len,
87985796c8dcSSimon Schubert         &remote_protocol_packets[PACKET_qXfer_osdata]);
87995796c8dcSSimon Schubert 
8800cf7f2e2dSJohn Marino     case TARGET_OBJECT_THREADS:
8801cf7f2e2dSJohn Marino       gdb_assert (annex == NULL);
8802cf7f2e2dSJohn Marino       return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
8803cf7f2e2dSJohn Marino 				&remote_protocol_packets[PACKET_qXfer_threads]);
8804cf7f2e2dSJohn Marino 
8805c50c785cSJohn Marino     case TARGET_OBJECT_TRACEFRAME_INFO:
8806c50c785cSJohn Marino       gdb_assert (annex == NULL);
8807c50c785cSJohn Marino       return remote_read_qxfer
8808c50c785cSJohn Marino 	(ops, "traceframe-info", annex, readbuf, offset, len,
8809c50c785cSJohn Marino 	 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
8810a45ae5f8SJohn Marino 
8811a45ae5f8SJohn Marino     case TARGET_OBJECT_FDPIC:
8812a45ae5f8SJohn Marino       return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
8813a45ae5f8SJohn Marino 				&remote_protocol_packets[PACKET_qXfer_fdpic]);
8814ef5ccd6cSJohn Marino 
8815ef5ccd6cSJohn Marino     case TARGET_OBJECT_OPENVMS_UIB:
8816ef5ccd6cSJohn Marino       return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
8817ef5ccd6cSJohn Marino 				&remote_protocol_packets[PACKET_qXfer_uib]);
8818ef5ccd6cSJohn Marino 
8819ef5ccd6cSJohn Marino     case TARGET_OBJECT_BTRACE:
8820ef5ccd6cSJohn Marino       return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len,
8821ef5ccd6cSJohn Marino         &remote_protocol_packets[PACKET_qXfer_btrace]);
8822ef5ccd6cSJohn Marino 
88235796c8dcSSimon Schubert     default:
88245796c8dcSSimon Schubert       return -1;
88255796c8dcSSimon Schubert     }
88265796c8dcSSimon Schubert 
88275796c8dcSSimon Schubert   /* Note: a zero OFFSET and LEN can be used to query the minimum
88285796c8dcSSimon Schubert      buffer size.  */
88295796c8dcSSimon Schubert   if (offset == 0 && len == 0)
88305796c8dcSSimon Schubert     return (get_remote_packet_size ());
88315796c8dcSSimon Schubert   /* Minimum outbuf size is get_remote_packet_size ().  If LEN is not
88325796c8dcSSimon Schubert      large enough let the caller deal with it.  */
88335796c8dcSSimon Schubert   if (len < get_remote_packet_size ())
88345796c8dcSSimon Schubert     return -1;
88355796c8dcSSimon Schubert   len = get_remote_packet_size ();
88365796c8dcSSimon Schubert 
88375796c8dcSSimon Schubert   /* Except for querying the minimum buffer size, target must be open.  */
88385796c8dcSSimon Schubert   if (!remote_desc)
88395796c8dcSSimon Schubert     error (_("remote query is only available after target open"));
88405796c8dcSSimon Schubert 
88415796c8dcSSimon Schubert   gdb_assert (annex != NULL);
88425796c8dcSSimon Schubert   gdb_assert (readbuf != NULL);
88435796c8dcSSimon Schubert 
88445796c8dcSSimon Schubert   p2 = rs->buf;
88455796c8dcSSimon Schubert   *p2++ = 'q';
88465796c8dcSSimon Schubert   *p2++ = query_type;
88475796c8dcSSimon Schubert 
88485796c8dcSSimon Schubert   /* We used one buffer char for the remote protocol q command and
88495796c8dcSSimon Schubert      another for the query type.  As the remote protocol encapsulation
88505796c8dcSSimon Schubert      uses 4 chars plus one extra in case we are debugging
88515796c8dcSSimon Schubert      (remote_debug), we have PBUFZIZ - 7 left to pack the query
88525796c8dcSSimon Schubert      string.  */
88535796c8dcSSimon Schubert   i = 0;
88545796c8dcSSimon Schubert   while (annex[i] && (i < (get_remote_packet_size () - 8)))
88555796c8dcSSimon Schubert     {
88565796c8dcSSimon Schubert       /* Bad caller may have sent forbidden characters.  */
88575796c8dcSSimon Schubert       gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
88585796c8dcSSimon Schubert       *p2++ = annex[i];
88595796c8dcSSimon Schubert       i++;
88605796c8dcSSimon Schubert     }
88615796c8dcSSimon Schubert   *p2 = '\0';
88625796c8dcSSimon Schubert   gdb_assert (annex[i] == '\0');
88635796c8dcSSimon Schubert 
88645796c8dcSSimon Schubert   i = putpkt (rs->buf);
88655796c8dcSSimon Schubert   if (i < 0)
88665796c8dcSSimon Schubert     return i;
88675796c8dcSSimon Schubert 
88685796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
88695796c8dcSSimon Schubert   strcpy ((char *) readbuf, rs->buf);
88705796c8dcSSimon Schubert 
88715796c8dcSSimon Schubert   return strlen ((char *) readbuf);
88725796c8dcSSimon Schubert }
88735796c8dcSSimon Schubert 
88745796c8dcSSimon Schubert static int
remote_search_memory(struct target_ops * ops,CORE_ADDR start_addr,ULONGEST search_space_len,const gdb_byte * pattern,ULONGEST pattern_len,CORE_ADDR * found_addrp)88755796c8dcSSimon Schubert remote_search_memory (struct target_ops* ops,
88765796c8dcSSimon Schubert 		      CORE_ADDR start_addr, ULONGEST search_space_len,
88775796c8dcSSimon Schubert 		      const gdb_byte *pattern, ULONGEST pattern_len,
88785796c8dcSSimon Schubert 		      CORE_ADDR *found_addrp)
88795796c8dcSSimon Schubert {
8880ef5ccd6cSJohn Marino   int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
88815796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
88825796c8dcSSimon Schubert   int max_size = get_memory_write_packet_size ();
88835796c8dcSSimon Schubert   struct packet_config *packet =
88845796c8dcSSimon Schubert     &remote_protocol_packets[PACKET_qSearch_memory];
8885c50c785cSJohn Marino   /* Number of packet bytes used to encode the pattern;
8886c50c785cSJohn Marino      this could be more than PATTERN_LEN due to escape characters.  */
88875796c8dcSSimon Schubert   int escaped_pattern_len;
8888c50c785cSJohn Marino   /* Amount of pattern that was encodable in the packet.  */
88895796c8dcSSimon Schubert   int used_pattern_len;
88905796c8dcSSimon Schubert   int i;
88915796c8dcSSimon Schubert   int found;
88925796c8dcSSimon Schubert   ULONGEST found_addr;
88935796c8dcSSimon Schubert 
88945796c8dcSSimon Schubert   /* Don't go to the target if we don't have to.
88955796c8dcSSimon Schubert      This is done before checking packet->support to avoid the possibility that
88965796c8dcSSimon Schubert      a success for this edge case means the facility works in general.  */
88975796c8dcSSimon Schubert   if (pattern_len > search_space_len)
88985796c8dcSSimon Schubert     return 0;
88995796c8dcSSimon Schubert   if (pattern_len == 0)
89005796c8dcSSimon Schubert     {
89015796c8dcSSimon Schubert       *found_addrp = start_addr;
89025796c8dcSSimon Schubert       return 1;
89035796c8dcSSimon Schubert     }
89045796c8dcSSimon Schubert 
89055796c8dcSSimon Schubert   /* If we already know the packet isn't supported, fall back to the simple
89065796c8dcSSimon Schubert      way of searching memory.  */
89075796c8dcSSimon Schubert 
89085796c8dcSSimon Schubert   if (packet->support == PACKET_DISABLE)
89095796c8dcSSimon Schubert     {
89105796c8dcSSimon Schubert       /* Target doesn't provided special support, fall back and use the
89115796c8dcSSimon Schubert 	 standard support (copy memory and do the search here).  */
89125796c8dcSSimon Schubert       return simple_search_memory (ops, start_addr, search_space_len,
89135796c8dcSSimon Schubert 				   pattern, pattern_len, found_addrp);
89145796c8dcSSimon Schubert     }
89155796c8dcSSimon Schubert 
89165796c8dcSSimon Schubert   /* Insert header.  */
89175796c8dcSSimon Schubert   i = snprintf (rs->buf, max_size,
89185796c8dcSSimon Schubert 		"qSearch:memory:%s;%s;",
89195796c8dcSSimon Schubert 		phex_nz (start_addr, addr_size),
89205796c8dcSSimon Schubert 		phex_nz (search_space_len, sizeof (search_space_len)));
89215796c8dcSSimon Schubert   max_size -= (i + 1);
89225796c8dcSSimon Schubert 
89235796c8dcSSimon Schubert   /* Escape as much data as fits into rs->buf.  */
89245796c8dcSSimon Schubert   escaped_pattern_len =
89255796c8dcSSimon Schubert     remote_escape_output (pattern, pattern_len, (rs->buf + i),
89265796c8dcSSimon Schubert 			  &used_pattern_len, max_size);
89275796c8dcSSimon Schubert 
89285796c8dcSSimon Schubert   /* Bail if the pattern is too large.  */
89295796c8dcSSimon Schubert   if (used_pattern_len != pattern_len)
8930c50c785cSJohn Marino     error (_("Pattern is too large to transmit to remote target."));
89315796c8dcSSimon Schubert 
89325796c8dcSSimon Schubert   if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
89335796c8dcSSimon Schubert       || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
89345796c8dcSSimon Schubert       || packet_ok (rs->buf, packet) != PACKET_OK)
89355796c8dcSSimon Schubert     {
89365796c8dcSSimon Schubert       /* The request may not have worked because the command is not
89375796c8dcSSimon Schubert 	 supported.  If so, fall back to the simple way.  */
89385796c8dcSSimon Schubert       if (packet->support == PACKET_DISABLE)
89395796c8dcSSimon Schubert 	{
89405796c8dcSSimon Schubert 	  return simple_search_memory (ops, start_addr, search_space_len,
89415796c8dcSSimon Schubert 				       pattern, pattern_len, found_addrp);
89425796c8dcSSimon Schubert 	}
89435796c8dcSSimon Schubert       return -1;
89445796c8dcSSimon Schubert     }
89455796c8dcSSimon Schubert 
89465796c8dcSSimon Schubert   if (rs->buf[0] == '0')
89475796c8dcSSimon Schubert     found = 0;
89485796c8dcSSimon Schubert   else if (rs->buf[0] == '1')
89495796c8dcSSimon Schubert     {
89505796c8dcSSimon Schubert       found = 1;
89515796c8dcSSimon Schubert       if (rs->buf[1] != ',')
89525796c8dcSSimon Schubert 	error (_("Unknown qSearch:memory reply: %s"), rs->buf);
89535796c8dcSSimon Schubert       unpack_varlen_hex (rs->buf + 2, &found_addr);
89545796c8dcSSimon Schubert       *found_addrp = found_addr;
89555796c8dcSSimon Schubert     }
89565796c8dcSSimon Schubert   else
89575796c8dcSSimon Schubert     error (_("Unknown qSearch:memory reply: %s"), rs->buf);
89585796c8dcSSimon Schubert 
89595796c8dcSSimon Schubert   return found;
89605796c8dcSSimon Schubert }
89615796c8dcSSimon Schubert 
89625796c8dcSSimon Schubert static void
remote_rcmd(char * command,struct ui_file * outbuf)89635796c8dcSSimon Schubert remote_rcmd (char *command,
89645796c8dcSSimon Schubert 	     struct ui_file *outbuf)
89655796c8dcSSimon Schubert {
89665796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
89675796c8dcSSimon Schubert   char *p = rs->buf;
89685796c8dcSSimon Schubert 
89695796c8dcSSimon Schubert   if (!remote_desc)
89705796c8dcSSimon Schubert     error (_("remote rcmd is only available after target open"));
89715796c8dcSSimon Schubert 
89725796c8dcSSimon Schubert   /* Send a NULL command across as an empty command.  */
89735796c8dcSSimon Schubert   if (command == NULL)
89745796c8dcSSimon Schubert     command = "";
89755796c8dcSSimon Schubert 
89765796c8dcSSimon Schubert   /* The query prefix.  */
89775796c8dcSSimon Schubert   strcpy (rs->buf, "qRcmd,");
89785796c8dcSSimon Schubert   p = strchr (rs->buf, '\0');
89795796c8dcSSimon Schubert 
8980c50c785cSJohn Marino   if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
8981c50c785cSJohn Marino       > get_remote_packet_size ())
89825796c8dcSSimon Schubert     error (_("\"monitor\" command ``%s'' is too long."), command);
89835796c8dcSSimon Schubert 
89845796c8dcSSimon Schubert   /* Encode the actual command.  */
89855796c8dcSSimon Schubert   bin2hex ((gdb_byte *) command, p, 0);
89865796c8dcSSimon Schubert 
89875796c8dcSSimon Schubert   if (putpkt (rs->buf) < 0)
89885796c8dcSSimon Schubert     error (_("Communication problem with target."));
89895796c8dcSSimon Schubert 
89905796c8dcSSimon Schubert   /* get/display the response */
89915796c8dcSSimon Schubert   while (1)
89925796c8dcSSimon Schubert     {
89935796c8dcSSimon Schubert       char *buf;
89945796c8dcSSimon Schubert 
8995cf7f2e2dSJohn Marino       /* XXX - see also remote_get_noisy_reply().  */
8996ef5ccd6cSJohn Marino       QUIT;			/* Allow user to bail out with ^C.  */
89975796c8dcSSimon Schubert       rs->buf[0] = '\0';
8998ef5ccd6cSJohn Marino       if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
8999ef5ccd6cSJohn Marino         {
9000ef5ccd6cSJohn Marino           /* Timeout.  Continue to (try to) read responses.
9001ef5ccd6cSJohn Marino              This is better than stopping with an error, assuming the stub
9002ef5ccd6cSJohn Marino              is still executing the (long) monitor command.
9003ef5ccd6cSJohn Marino              If needed, the user can interrupt gdb using C-c, obtaining
9004ef5ccd6cSJohn Marino              an effect similar to stop on timeout.  */
9005ef5ccd6cSJohn Marino           continue;
9006ef5ccd6cSJohn Marino         }
90075796c8dcSSimon Schubert       buf = rs->buf;
90085796c8dcSSimon Schubert       if (buf[0] == '\0')
90095796c8dcSSimon Schubert 	error (_("Target does not support this command."));
90105796c8dcSSimon Schubert       if (buf[0] == 'O' && buf[1] != 'K')
90115796c8dcSSimon Schubert 	{
90125796c8dcSSimon Schubert 	  remote_console_output (buf + 1); /* 'O' message from stub.  */
90135796c8dcSSimon Schubert 	  continue;
90145796c8dcSSimon Schubert 	}
90155796c8dcSSimon Schubert       if (strcmp (buf, "OK") == 0)
90165796c8dcSSimon Schubert 	break;
90175796c8dcSSimon Schubert       if (strlen (buf) == 3 && buf[0] == 'E'
90185796c8dcSSimon Schubert 	  && isdigit (buf[1]) && isdigit (buf[2]))
90195796c8dcSSimon Schubert 	{
90205796c8dcSSimon Schubert 	  error (_("Protocol error with Rcmd"));
90215796c8dcSSimon Schubert 	}
90225796c8dcSSimon Schubert       for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
90235796c8dcSSimon Schubert 	{
90245796c8dcSSimon Schubert 	  char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
9025cf7f2e2dSJohn Marino 
90265796c8dcSSimon Schubert 	  fputc_unfiltered (c, outbuf);
90275796c8dcSSimon Schubert 	}
90285796c8dcSSimon Schubert       break;
90295796c8dcSSimon Schubert     }
90305796c8dcSSimon Schubert }
90315796c8dcSSimon Schubert 
VEC(mem_region_s)90325796c8dcSSimon Schubert static VEC(mem_region_s) *
90335796c8dcSSimon Schubert remote_memory_map (struct target_ops *ops)
90345796c8dcSSimon Schubert {
90355796c8dcSSimon Schubert   VEC(mem_region_s) *result = NULL;
90365796c8dcSSimon Schubert   char *text = target_read_stralloc (&current_target,
90375796c8dcSSimon Schubert 				     TARGET_OBJECT_MEMORY_MAP, NULL);
90385796c8dcSSimon Schubert 
90395796c8dcSSimon Schubert   if (text)
90405796c8dcSSimon Schubert     {
90415796c8dcSSimon Schubert       struct cleanup *back_to = make_cleanup (xfree, text);
9042cf7f2e2dSJohn Marino 
90435796c8dcSSimon Schubert       result = parse_memory_map (text);
90445796c8dcSSimon Schubert       do_cleanups (back_to);
90455796c8dcSSimon Schubert     }
90465796c8dcSSimon Schubert 
90475796c8dcSSimon Schubert   return result;
90485796c8dcSSimon Schubert }
90495796c8dcSSimon Schubert 
90505796c8dcSSimon Schubert static void
packet_command(char * args,int from_tty)90515796c8dcSSimon Schubert packet_command (char *args, int from_tty)
90525796c8dcSSimon Schubert {
90535796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
90545796c8dcSSimon Schubert 
90555796c8dcSSimon Schubert   if (!remote_desc)
90565796c8dcSSimon Schubert     error (_("command can only be used with remote target"));
90575796c8dcSSimon Schubert 
90585796c8dcSSimon Schubert   if (!args)
90595796c8dcSSimon Schubert     error (_("remote-packet command requires packet text as argument"));
90605796c8dcSSimon Schubert 
90615796c8dcSSimon Schubert   puts_filtered ("sending: ");
90625796c8dcSSimon Schubert   print_packet (args);
90635796c8dcSSimon Schubert   puts_filtered ("\n");
90645796c8dcSSimon Schubert   putpkt (args);
90655796c8dcSSimon Schubert 
90665796c8dcSSimon Schubert   getpkt (&rs->buf, &rs->buf_size, 0);
90675796c8dcSSimon Schubert   puts_filtered ("received: ");
90685796c8dcSSimon Schubert   print_packet (rs->buf);
90695796c8dcSSimon Schubert   puts_filtered ("\n");
90705796c8dcSSimon Schubert }
90715796c8dcSSimon Schubert 
90725796c8dcSSimon Schubert #if 0
90735796c8dcSSimon Schubert /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
90745796c8dcSSimon Schubert 
90755796c8dcSSimon Schubert static void display_thread_info (struct gdb_ext_thread_info *info);
90765796c8dcSSimon Schubert 
90775796c8dcSSimon Schubert static void threadset_test_cmd (char *cmd, int tty);
90785796c8dcSSimon Schubert 
90795796c8dcSSimon Schubert static void threadalive_test (char *cmd, int tty);
90805796c8dcSSimon Schubert 
90815796c8dcSSimon Schubert static void threadlist_test_cmd (char *cmd, int tty);
90825796c8dcSSimon Schubert 
90835796c8dcSSimon Schubert int get_and_display_threadinfo (threadref *ref);
90845796c8dcSSimon Schubert 
90855796c8dcSSimon Schubert static void threadinfo_test_cmd (char *cmd, int tty);
90865796c8dcSSimon Schubert 
90875796c8dcSSimon Schubert static int thread_display_step (threadref *ref, void *context);
90885796c8dcSSimon Schubert 
90895796c8dcSSimon Schubert static void threadlist_update_test_cmd (char *cmd, int tty);
90905796c8dcSSimon Schubert 
90915796c8dcSSimon Schubert static void init_remote_threadtests (void);
90925796c8dcSSimon Schubert 
90935796c8dcSSimon Schubert #define SAMPLE_THREAD  0x05060708	/* Truncated 64 bit threadid.  */
90945796c8dcSSimon Schubert 
90955796c8dcSSimon Schubert static void
90965796c8dcSSimon Schubert threadset_test_cmd (char *cmd, int tty)
90975796c8dcSSimon Schubert {
90985796c8dcSSimon Schubert   int sample_thread = SAMPLE_THREAD;
90995796c8dcSSimon Schubert 
91005796c8dcSSimon Schubert   printf_filtered (_("Remote threadset test\n"));
91015796c8dcSSimon Schubert   set_general_thread (sample_thread);
91025796c8dcSSimon Schubert }
91035796c8dcSSimon Schubert 
91045796c8dcSSimon Schubert 
91055796c8dcSSimon Schubert static void
91065796c8dcSSimon Schubert threadalive_test (char *cmd, int tty)
91075796c8dcSSimon Schubert {
91085796c8dcSSimon Schubert   int sample_thread = SAMPLE_THREAD;
91095796c8dcSSimon Schubert   int pid = ptid_get_pid (inferior_ptid);
91105796c8dcSSimon Schubert   ptid_t ptid = ptid_build (pid, 0, sample_thread);
91115796c8dcSSimon Schubert 
91125796c8dcSSimon Schubert   if (remote_thread_alive (ptid))
91135796c8dcSSimon Schubert     printf_filtered ("PASS: Thread alive test\n");
91145796c8dcSSimon Schubert   else
91155796c8dcSSimon Schubert     printf_filtered ("FAIL: Thread alive test\n");
91165796c8dcSSimon Schubert }
91175796c8dcSSimon Schubert 
91185796c8dcSSimon Schubert void output_threadid (char *title, threadref *ref);
91195796c8dcSSimon Schubert 
91205796c8dcSSimon Schubert void
91215796c8dcSSimon Schubert output_threadid (char *title, threadref *ref)
91225796c8dcSSimon Schubert {
91235796c8dcSSimon Schubert   char hexid[20];
91245796c8dcSSimon Schubert 
91255796c8dcSSimon Schubert   pack_threadid (&hexid[0], ref);	/* Convert threead id into hex.  */
91265796c8dcSSimon Schubert   hexid[16] = 0;
91275796c8dcSSimon Schubert   printf_filtered ("%s  %s\n", title, (&hexid[0]));
91285796c8dcSSimon Schubert }
91295796c8dcSSimon Schubert 
91305796c8dcSSimon Schubert static void
91315796c8dcSSimon Schubert threadlist_test_cmd (char *cmd, int tty)
91325796c8dcSSimon Schubert {
91335796c8dcSSimon Schubert   int startflag = 1;
91345796c8dcSSimon Schubert   threadref nextthread;
91355796c8dcSSimon Schubert   int done, result_count;
91365796c8dcSSimon Schubert   threadref threadlist[3];
91375796c8dcSSimon Schubert 
91385796c8dcSSimon Schubert   printf_filtered ("Remote Threadlist test\n");
91395796c8dcSSimon Schubert   if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
91405796c8dcSSimon Schubert 			      &result_count, &threadlist[0]))
91415796c8dcSSimon Schubert     printf_filtered ("FAIL: threadlist test\n");
91425796c8dcSSimon Schubert   else
91435796c8dcSSimon Schubert     {
91445796c8dcSSimon Schubert       threadref *scan = threadlist;
91455796c8dcSSimon Schubert       threadref *limit = scan + result_count;
91465796c8dcSSimon Schubert 
91475796c8dcSSimon Schubert       while (scan < limit)
91485796c8dcSSimon Schubert 	output_threadid (" thread ", scan++);
91495796c8dcSSimon Schubert     }
91505796c8dcSSimon Schubert }
91515796c8dcSSimon Schubert 
91525796c8dcSSimon Schubert void
91535796c8dcSSimon Schubert display_thread_info (struct gdb_ext_thread_info *info)
91545796c8dcSSimon Schubert {
91555796c8dcSSimon Schubert   output_threadid ("Threadid: ", &info->threadid);
91565796c8dcSSimon Schubert   printf_filtered ("Name: %s\n ", info->shortname);
91575796c8dcSSimon Schubert   printf_filtered ("State: %s\n", info->display);
91585796c8dcSSimon Schubert   printf_filtered ("other: %s\n\n", info->more_display);
91595796c8dcSSimon Schubert }
91605796c8dcSSimon Schubert 
91615796c8dcSSimon Schubert int
91625796c8dcSSimon Schubert get_and_display_threadinfo (threadref *ref)
91635796c8dcSSimon Schubert {
91645796c8dcSSimon Schubert   int result;
91655796c8dcSSimon Schubert   int set;
91665796c8dcSSimon Schubert   struct gdb_ext_thread_info threadinfo;
91675796c8dcSSimon Schubert 
91685796c8dcSSimon Schubert   set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
91695796c8dcSSimon Schubert     | TAG_MOREDISPLAY | TAG_DISPLAY;
91705796c8dcSSimon Schubert   if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
91715796c8dcSSimon Schubert     display_thread_info (&threadinfo);
91725796c8dcSSimon Schubert   return result;
91735796c8dcSSimon Schubert }
91745796c8dcSSimon Schubert 
91755796c8dcSSimon Schubert static void
91765796c8dcSSimon Schubert threadinfo_test_cmd (char *cmd, int tty)
91775796c8dcSSimon Schubert {
91785796c8dcSSimon Schubert   int athread = SAMPLE_THREAD;
91795796c8dcSSimon Schubert   threadref thread;
91805796c8dcSSimon Schubert   int set;
91815796c8dcSSimon Schubert 
91825796c8dcSSimon Schubert   int_to_threadref (&thread, athread);
91835796c8dcSSimon Schubert   printf_filtered ("Remote Threadinfo test\n");
91845796c8dcSSimon Schubert   if (!get_and_display_threadinfo (&thread))
91855796c8dcSSimon Schubert     printf_filtered ("FAIL cannot get thread info\n");
91865796c8dcSSimon Schubert }
91875796c8dcSSimon Schubert 
91885796c8dcSSimon Schubert static int
91895796c8dcSSimon Schubert thread_display_step (threadref *ref, void *context)
91905796c8dcSSimon Schubert {
91915796c8dcSSimon Schubert   /* output_threadid(" threadstep ",ref); *//* simple test */
91925796c8dcSSimon Schubert   return get_and_display_threadinfo (ref);
91935796c8dcSSimon Schubert }
91945796c8dcSSimon Schubert 
91955796c8dcSSimon Schubert static void
91965796c8dcSSimon Schubert threadlist_update_test_cmd (char *cmd, int tty)
91975796c8dcSSimon Schubert {
91985796c8dcSSimon Schubert   printf_filtered ("Remote Threadlist update test\n");
91995796c8dcSSimon Schubert   remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
92005796c8dcSSimon Schubert }
92015796c8dcSSimon Schubert 
92025796c8dcSSimon Schubert static void
92035796c8dcSSimon Schubert init_remote_threadtests (void)
92045796c8dcSSimon Schubert {
9205c50c785cSJohn Marino   add_com ("tlist", class_obscure, threadlist_test_cmd,
9206c50c785cSJohn Marino 	   _("Fetch and print the remote list of "
9207c50c785cSJohn Marino 	     "thread identifiers, one pkt only"));
92085796c8dcSSimon Schubert   add_com ("tinfo", class_obscure, threadinfo_test_cmd,
92095796c8dcSSimon Schubert 	   _("Fetch and display info about one thread"));
92105796c8dcSSimon Schubert   add_com ("tset", class_obscure, threadset_test_cmd,
92115796c8dcSSimon Schubert 	   _("Test setting to a different thread"));
92125796c8dcSSimon Schubert   add_com ("tupd", class_obscure, threadlist_update_test_cmd,
92135796c8dcSSimon Schubert 	   _("Iterate through updating all remote thread info"));
92145796c8dcSSimon Schubert   add_com ("talive", class_obscure, threadalive_test,
92155796c8dcSSimon Schubert 	   _(" Remote thread alive test "));
92165796c8dcSSimon Schubert }
92175796c8dcSSimon Schubert 
92185796c8dcSSimon Schubert #endif /* 0 */
92195796c8dcSSimon Schubert 
92205796c8dcSSimon Schubert /* Convert a thread ID to a string.  Returns the string in a static
92215796c8dcSSimon Schubert    buffer.  */
92225796c8dcSSimon Schubert 
92235796c8dcSSimon Schubert static char *
remote_pid_to_str(struct target_ops * ops,ptid_t ptid)92245796c8dcSSimon Schubert remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
92255796c8dcSSimon Schubert {
92265796c8dcSSimon Schubert   static char buf[64];
92275796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
92285796c8dcSSimon Schubert 
9229ef5ccd6cSJohn Marino   if (ptid_equal (ptid, null_ptid))
9230ef5ccd6cSJohn Marino     return normal_pid_to_str (ptid);
9231ef5ccd6cSJohn Marino   else if (ptid_is_pid (ptid))
92325796c8dcSSimon Schubert     {
92335796c8dcSSimon Schubert       /* Printing an inferior target id.  */
92345796c8dcSSimon Schubert 
92355796c8dcSSimon Schubert       /* When multi-process extensions are off, there's no way in the
92365796c8dcSSimon Schubert 	 remote protocol to know the remote process id, if there's any
92375796c8dcSSimon Schubert 	 at all.  There's one exception --- when we're connected with
92385796c8dcSSimon Schubert 	 target extended-remote, and we manually attached to a process
92395796c8dcSSimon Schubert 	 with "attach PID".  We don't record anywhere a flag that
92405796c8dcSSimon Schubert 	 allows us to distinguish that case from the case of
92415796c8dcSSimon Schubert 	 connecting with extended-remote and the stub already being
92425796c8dcSSimon Schubert 	 attached to a process, and reporting yes to qAttached, hence
92435796c8dcSSimon Schubert 	 no smart special casing here.  */
92445796c8dcSSimon Schubert       if (!remote_multi_process_p (rs))
92455796c8dcSSimon Schubert 	{
92465796c8dcSSimon Schubert 	  xsnprintf (buf, sizeof buf, "Remote target");
92475796c8dcSSimon Schubert 	  return buf;
92485796c8dcSSimon Schubert 	}
92495796c8dcSSimon Schubert 
92505796c8dcSSimon Schubert       return normal_pid_to_str (ptid);
92515796c8dcSSimon Schubert     }
92525796c8dcSSimon Schubert   else
92535796c8dcSSimon Schubert     {
92545796c8dcSSimon Schubert       if (ptid_equal (magic_null_ptid, ptid))
92555796c8dcSSimon Schubert 	xsnprintf (buf, sizeof buf, "Thread <main>");
9256ef5ccd6cSJohn Marino       else if (rs->extended && remote_multi_process_p (rs))
92575796c8dcSSimon Schubert 	xsnprintf (buf, sizeof buf, "Thread %d.%ld",
92585796c8dcSSimon Schubert 		   ptid_get_pid (ptid), ptid_get_tid (ptid));
92595796c8dcSSimon Schubert       else
92605796c8dcSSimon Schubert 	xsnprintf (buf, sizeof buf, "Thread %ld",
92615796c8dcSSimon Schubert 		   ptid_get_tid (ptid));
92625796c8dcSSimon Schubert       return buf;
92635796c8dcSSimon Schubert     }
92645796c8dcSSimon Schubert }
92655796c8dcSSimon Schubert 
92665796c8dcSSimon Schubert /* Get the address of the thread local variable in OBJFILE which is
92675796c8dcSSimon Schubert    stored at OFFSET within the thread local storage for thread PTID.  */
92685796c8dcSSimon Schubert 
92695796c8dcSSimon Schubert static CORE_ADDR
remote_get_thread_local_address(struct target_ops * ops,ptid_t ptid,CORE_ADDR lm,CORE_ADDR offset)92705796c8dcSSimon Schubert remote_get_thread_local_address (struct target_ops *ops,
92715796c8dcSSimon Schubert 				 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
92725796c8dcSSimon Schubert {
92735796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE)
92745796c8dcSSimon Schubert     {
92755796c8dcSSimon Schubert       struct remote_state *rs = get_remote_state ();
92765796c8dcSSimon Schubert       char *p = rs->buf;
92775796c8dcSSimon Schubert       char *endp = rs->buf + get_remote_packet_size ();
92785796c8dcSSimon Schubert       enum packet_result result;
92795796c8dcSSimon Schubert 
92805796c8dcSSimon Schubert       strcpy (p, "qGetTLSAddr:");
92815796c8dcSSimon Schubert       p += strlen (p);
92825796c8dcSSimon Schubert       p = write_ptid (p, endp, ptid);
92835796c8dcSSimon Schubert       *p++ = ',';
92845796c8dcSSimon Schubert       p += hexnumstr (p, offset);
92855796c8dcSSimon Schubert       *p++ = ',';
92865796c8dcSSimon Schubert       p += hexnumstr (p, lm);
92875796c8dcSSimon Schubert       *p++ = '\0';
92885796c8dcSSimon Schubert 
92895796c8dcSSimon Schubert       putpkt (rs->buf);
92905796c8dcSSimon Schubert       getpkt (&rs->buf, &rs->buf_size, 0);
9291c50c785cSJohn Marino       result = packet_ok (rs->buf,
9292c50c785cSJohn Marino 			  &remote_protocol_packets[PACKET_qGetTLSAddr]);
92935796c8dcSSimon Schubert       if (result == PACKET_OK)
92945796c8dcSSimon Schubert 	{
92955796c8dcSSimon Schubert 	  ULONGEST result;
92965796c8dcSSimon Schubert 
92975796c8dcSSimon Schubert 	  unpack_varlen_hex (rs->buf, &result);
92985796c8dcSSimon Schubert 	  return result;
92995796c8dcSSimon Schubert 	}
93005796c8dcSSimon Schubert       else if (result == PACKET_UNKNOWN)
93015796c8dcSSimon Schubert 	throw_error (TLS_GENERIC_ERROR,
93025796c8dcSSimon Schubert 		     _("Remote target doesn't support qGetTLSAddr packet"));
93035796c8dcSSimon Schubert       else
93045796c8dcSSimon Schubert 	throw_error (TLS_GENERIC_ERROR,
93055796c8dcSSimon Schubert 		     _("Remote target failed to process qGetTLSAddr request"));
93065796c8dcSSimon Schubert     }
93075796c8dcSSimon Schubert   else
93085796c8dcSSimon Schubert     throw_error (TLS_GENERIC_ERROR,
93095796c8dcSSimon Schubert 		 _("TLS not supported or disabled on this target"));
93105796c8dcSSimon Schubert   /* Not reached.  */
93115796c8dcSSimon Schubert   return 0;
93125796c8dcSSimon Schubert }
93135796c8dcSSimon Schubert 
9314cf7f2e2dSJohn Marino /* Provide thread local base, i.e. Thread Information Block address.
9315cf7f2e2dSJohn Marino    Returns 1 if ptid is found and thread_local_base is non zero.  */
9316cf7f2e2dSJohn Marino 
9317ef5ccd6cSJohn Marino static int
remote_get_tib_address(ptid_t ptid,CORE_ADDR * addr)9318cf7f2e2dSJohn Marino remote_get_tib_address (ptid_t ptid, CORE_ADDR *addr)
9319cf7f2e2dSJohn Marino {
9320cf7f2e2dSJohn Marino   if (remote_protocol_packets[PACKET_qGetTIBAddr].support != PACKET_DISABLE)
9321cf7f2e2dSJohn Marino     {
9322cf7f2e2dSJohn Marino       struct remote_state *rs = get_remote_state ();
9323cf7f2e2dSJohn Marino       char *p = rs->buf;
9324cf7f2e2dSJohn Marino       char *endp = rs->buf + get_remote_packet_size ();
9325cf7f2e2dSJohn Marino       enum packet_result result;
9326cf7f2e2dSJohn Marino 
9327cf7f2e2dSJohn Marino       strcpy (p, "qGetTIBAddr:");
9328cf7f2e2dSJohn Marino       p += strlen (p);
9329cf7f2e2dSJohn Marino       p = write_ptid (p, endp, ptid);
9330cf7f2e2dSJohn Marino       *p++ = '\0';
9331cf7f2e2dSJohn Marino 
9332cf7f2e2dSJohn Marino       putpkt (rs->buf);
9333cf7f2e2dSJohn Marino       getpkt (&rs->buf, &rs->buf_size, 0);
9334cf7f2e2dSJohn Marino       result = packet_ok (rs->buf,
9335cf7f2e2dSJohn Marino 			  &remote_protocol_packets[PACKET_qGetTIBAddr]);
9336cf7f2e2dSJohn Marino       if (result == PACKET_OK)
9337cf7f2e2dSJohn Marino 	{
9338cf7f2e2dSJohn Marino 	  ULONGEST result;
9339cf7f2e2dSJohn Marino 
9340cf7f2e2dSJohn Marino 	  unpack_varlen_hex (rs->buf, &result);
9341cf7f2e2dSJohn Marino 	  if (addr)
9342cf7f2e2dSJohn Marino 	    *addr = (CORE_ADDR) result;
9343cf7f2e2dSJohn Marino 	  return 1;
9344cf7f2e2dSJohn Marino 	}
9345cf7f2e2dSJohn Marino       else if (result == PACKET_UNKNOWN)
9346cf7f2e2dSJohn Marino 	error (_("Remote target doesn't support qGetTIBAddr packet"));
9347cf7f2e2dSJohn Marino       else
9348cf7f2e2dSJohn Marino 	error (_("Remote target failed to process qGetTIBAddr request"));
9349cf7f2e2dSJohn Marino     }
9350cf7f2e2dSJohn Marino   else
9351cf7f2e2dSJohn Marino     error (_("qGetTIBAddr not supported or disabled on this target"));
9352cf7f2e2dSJohn Marino   /* Not reached.  */
9353cf7f2e2dSJohn Marino   return 0;
9354cf7f2e2dSJohn Marino }
9355cf7f2e2dSJohn Marino 
93565796c8dcSSimon Schubert /* Support for inferring a target description based on the current
93575796c8dcSSimon Schubert    architecture and the size of a 'g' packet.  While the 'g' packet
93585796c8dcSSimon Schubert    can have any size (since optional registers can be left off the
93595796c8dcSSimon Schubert    end), some sizes are easily recognizable given knowledge of the
93605796c8dcSSimon Schubert    approximate architecture.  */
93615796c8dcSSimon Schubert 
93625796c8dcSSimon Schubert struct remote_g_packet_guess
93635796c8dcSSimon Schubert {
93645796c8dcSSimon Schubert   int bytes;
93655796c8dcSSimon Schubert   const struct target_desc *tdesc;
93665796c8dcSSimon Schubert };
93675796c8dcSSimon Schubert typedef struct remote_g_packet_guess remote_g_packet_guess_s;
93685796c8dcSSimon Schubert DEF_VEC_O(remote_g_packet_guess_s);
93695796c8dcSSimon Schubert 
93705796c8dcSSimon Schubert struct remote_g_packet_data
93715796c8dcSSimon Schubert {
93725796c8dcSSimon Schubert   VEC(remote_g_packet_guess_s) *guesses;
93735796c8dcSSimon Schubert };
93745796c8dcSSimon Schubert 
93755796c8dcSSimon Schubert static struct gdbarch_data *remote_g_packet_data_handle;
93765796c8dcSSimon Schubert 
93775796c8dcSSimon Schubert static void *
remote_g_packet_data_init(struct obstack * obstack)93785796c8dcSSimon Schubert remote_g_packet_data_init (struct obstack *obstack)
93795796c8dcSSimon Schubert {
93805796c8dcSSimon Schubert   return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
93815796c8dcSSimon Schubert }
93825796c8dcSSimon Schubert 
93835796c8dcSSimon Schubert void
register_remote_g_packet_guess(struct gdbarch * gdbarch,int bytes,const struct target_desc * tdesc)93845796c8dcSSimon Schubert register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
93855796c8dcSSimon Schubert 				const struct target_desc *tdesc)
93865796c8dcSSimon Schubert {
93875796c8dcSSimon Schubert   struct remote_g_packet_data *data
93885796c8dcSSimon Schubert     = gdbarch_data (gdbarch, remote_g_packet_data_handle);
93895796c8dcSSimon Schubert   struct remote_g_packet_guess new_guess, *guess;
93905796c8dcSSimon Schubert   int ix;
93915796c8dcSSimon Schubert 
93925796c8dcSSimon Schubert   gdb_assert (tdesc != NULL);
93935796c8dcSSimon Schubert 
93945796c8dcSSimon Schubert   for (ix = 0;
93955796c8dcSSimon Schubert        VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
93965796c8dcSSimon Schubert        ix++)
93975796c8dcSSimon Schubert     if (guess->bytes == bytes)
93985796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__,
9399c50c785cSJohn Marino 		      _("Duplicate g packet description added for size %d"),
94005796c8dcSSimon Schubert 		      bytes);
94015796c8dcSSimon Schubert 
94025796c8dcSSimon Schubert   new_guess.bytes = bytes;
94035796c8dcSSimon Schubert   new_guess.tdesc = tdesc;
94045796c8dcSSimon Schubert   VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
94055796c8dcSSimon Schubert }
94065796c8dcSSimon Schubert 
94075796c8dcSSimon Schubert /* Return 1 if remote_read_description would do anything on this target
94085796c8dcSSimon Schubert    and architecture, 0 otherwise.  */
94095796c8dcSSimon Schubert 
94105796c8dcSSimon Schubert static int
remote_read_description_p(struct target_ops * target)94115796c8dcSSimon Schubert remote_read_description_p (struct target_ops *target)
94125796c8dcSSimon Schubert {
94135796c8dcSSimon Schubert   struct remote_g_packet_data *data
9414ef5ccd6cSJohn Marino     = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
94155796c8dcSSimon Schubert 
94165796c8dcSSimon Schubert   if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
94175796c8dcSSimon Schubert     return 1;
94185796c8dcSSimon Schubert 
94195796c8dcSSimon Schubert   return 0;
94205796c8dcSSimon Schubert }
94215796c8dcSSimon Schubert 
94225796c8dcSSimon Schubert static const struct target_desc *
remote_read_description(struct target_ops * target)94235796c8dcSSimon Schubert remote_read_description (struct target_ops *target)
94245796c8dcSSimon Schubert {
94255796c8dcSSimon Schubert   struct remote_g_packet_data *data
9426ef5ccd6cSJohn Marino     = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle);
94275796c8dcSSimon Schubert 
94285796c8dcSSimon Schubert   /* Do not try this during initial connection, when we do not know
94295796c8dcSSimon Schubert      whether there is a running but stopped thread.  */
94305796c8dcSSimon Schubert   if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
94315796c8dcSSimon Schubert     return NULL;
94325796c8dcSSimon Schubert 
94335796c8dcSSimon Schubert   if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
94345796c8dcSSimon Schubert     {
94355796c8dcSSimon Schubert       struct remote_g_packet_guess *guess;
94365796c8dcSSimon Schubert       int ix;
94375796c8dcSSimon Schubert       int bytes = send_g_packet ();
94385796c8dcSSimon Schubert 
94395796c8dcSSimon Schubert       for (ix = 0;
94405796c8dcSSimon Schubert 	   VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
94415796c8dcSSimon Schubert 	   ix++)
94425796c8dcSSimon Schubert 	if (guess->bytes == bytes)
94435796c8dcSSimon Schubert 	  return guess->tdesc;
94445796c8dcSSimon Schubert 
94455796c8dcSSimon Schubert       /* We discard the g packet.  A minor optimization would be to
94465796c8dcSSimon Schubert 	 hold on to it, and fill the register cache once we have selected
94475796c8dcSSimon Schubert 	 an architecture, but it's too tricky to do safely.  */
94485796c8dcSSimon Schubert     }
94495796c8dcSSimon Schubert 
94505796c8dcSSimon Schubert   return NULL;
94515796c8dcSSimon Schubert }
94525796c8dcSSimon Schubert 
94535796c8dcSSimon Schubert /* Remote file transfer support.  This is host-initiated I/O, not
94545796c8dcSSimon Schubert    target-initiated; for target-initiated, see remote-fileio.c.  */
94555796c8dcSSimon Schubert 
94565796c8dcSSimon Schubert /* If *LEFT is at least the length of STRING, copy STRING to
94575796c8dcSSimon Schubert    *BUFFER, update *BUFFER to point to the new end of the buffer, and
94585796c8dcSSimon Schubert    decrease *LEFT.  Otherwise raise an error.  */
94595796c8dcSSimon Schubert 
94605796c8dcSSimon Schubert static void
remote_buffer_add_string(char ** buffer,int * left,char * string)94615796c8dcSSimon Schubert remote_buffer_add_string (char **buffer, int *left, char *string)
94625796c8dcSSimon Schubert {
94635796c8dcSSimon Schubert   int len = strlen (string);
94645796c8dcSSimon Schubert 
94655796c8dcSSimon Schubert   if (len > *left)
94665796c8dcSSimon Schubert     error (_("Packet too long for target."));
94675796c8dcSSimon Schubert 
94685796c8dcSSimon Schubert   memcpy (*buffer, string, len);
94695796c8dcSSimon Schubert   *buffer += len;
94705796c8dcSSimon Schubert   *left -= len;
94715796c8dcSSimon Schubert 
94725796c8dcSSimon Schubert   /* NUL-terminate the buffer as a convenience, if there is
94735796c8dcSSimon Schubert      room.  */
94745796c8dcSSimon Schubert   if (*left)
94755796c8dcSSimon Schubert     **buffer = '\0';
94765796c8dcSSimon Schubert }
94775796c8dcSSimon Schubert 
94785796c8dcSSimon Schubert /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
94795796c8dcSSimon Schubert    *BUFFER, update *BUFFER to point to the new end of the buffer, and
94805796c8dcSSimon Schubert    decrease *LEFT.  Otherwise raise an error.  */
94815796c8dcSSimon Schubert 
94825796c8dcSSimon Schubert static void
remote_buffer_add_bytes(char ** buffer,int * left,const gdb_byte * bytes,int len)94835796c8dcSSimon Schubert remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
94845796c8dcSSimon Schubert 			 int len)
94855796c8dcSSimon Schubert {
94865796c8dcSSimon Schubert   if (2 * len > *left)
94875796c8dcSSimon Schubert     error (_("Packet too long for target."));
94885796c8dcSSimon Schubert 
94895796c8dcSSimon Schubert   bin2hex (bytes, *buffer, len);
94905796c8dcSSimon Schubert   *buffer += 2 * len;
94915796c8dcSSimon Schubert   *left -= 2 * len;
94925796c8dcSSimon Schubert 
94935796c8dcSSimon Schubert   /* NUL-terminate the buffer as a convenience, if there is
94945796c8dcSSimon Schubert      room.  */
94955796c8dcSSimon Schubert   if (*left)
94965796c8dcSSimon Schubert     **buffer = '\0';
94975796c8dcSSimon Schubert }
94985796c8dcSSimon Schubert 
94995796c8dcSSimon Schubert /* If *LEFT is large enough, convert VALUE to hex and add it to
95005796c8dcSSimon Schubert    *BUFFER, update *BUFFER to point to the new end of the buffer, and
95015796c8dcSSimon Schubert    decrease *LEFT.  Otherwise raise an error.  */
95025796c8dcSSimon Schubert 
95035796c8dcSSimon Schubert static void
remote_buffer_add_int(char ** buffer,int * left,ULONGEST value)95045796c8dcSSimon Schubert remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
95055796c8dcSSimon Schubert {
95065796c8dcSSimon Schubert   int len = hexnumlen (value);
95075796c8dcSSimon Schubert 
95085796c8dcSSimon Schubert   if (len > *left)
95095796c8dcSSimon Schubert     error (_("Packet too long for target."));
95105796c8dcSSimon Schubert 
95115796c8dcSSimon Schubert   hexnumstr (*buffer, value);
95125796c8dcSSimon Schubert   *buffer += len;
95135796c8dcSSimon Schubert   *left -= len;
95145796c8dcSSimon Schubert 
95155796c8dcSSimon Schubert   /* NUL-terminate the buffer as a convenience, if there is
95165796c8dcSSimon Schubert      room.  */
95175796c8dcSSimon Schubert   if (*left)
95185796c8dcSSimon Schubert     **buffer = '\0';
95195796c8dcSSimon Schubert }
95205796c8dcSSimon Schubert 
95215796c8dcSSimon Schubert /* Parse an I/O result packet from BUFFER.  Set RETCODE to the return
95225796c8dcSSimon Schubert    value, *REMOTE_ERRNO to the remote error number or zero if none
95235796c8dcSSimon Schubert    was included, and *ATTACHMENT to point to the start of the annex
95245796c8dcSSimon Schubert    if any.  The length of the packet isn't needed here; there may
95255796c8dcSSimon Schubert    be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
95265796c8dcSSimon Schubert 
95275796c8dcSSimon Schubert    Return 0 if the packet could be parsed, -1 if it could not.  If
95285796c8dcSSimon Schubert    -1 is returned, the other variables may not be initialized.  */
95295796c8dcSSimon Schubert 
95305796c8dcSSimon Schubert static int
remote_hostio_parse_result(char * buffer,int * retcode,int * remote_errno,char ** attachment)95315796c8dcSSimon Schubert remote_hostio_parse_result (char *buffer, int *retcode,
95325796c8dcSSimon Schubert 			    int *remote_errno, char **attachment)
95335796c8dcSSimon Schubert {
95345796c8dcSSimon Schubert   char *p, *p2;
95355796c8dcSSimon Schubert 
95365796c8dcSSimon Schubert   *remote_errno = 0;
95375796c8dcSSimon Schubert   *attachment = NULL;
95385796c8dcSSimon Schubert 
95395796c8dcSSimon Schubert   if (buffer[0] != 'F')
95405796c8dcSSimon Schubert     return -1;
95415796c8dcSSimon Schubert 
95425796c8dcSSimon Schubert   errno = 0;
95435796c8dcSSimon Schubert   *retcode = strtol (&buffer[1], &p, 16);
95445796c8dcSSimon Schubert   if (errno != 0 || p == &buffer[1])
95455796c8dcSSimon Schubert     return -1;
95465796c8dcSSimon Schubert 
95475796c8dcSSimon Schubert   /* Check for ",errno".  */
95485796c8dcSSimon Schubert   if (*p == ',')
95495796c8dcSSimon Schubert     {
95505796c8dcSSimon Schubert       errno = 0;
95515796c8dcSSimon Schubert       *remote_errno = strtol (p + 1, &p2, 16);
95525796c8dcSSimon Schubert       if (errno != 0 || p + 1 == p2)
95535796c8dcSSimon Schubert 	return -1;
95545796c8dcSSimon Schubert       p = p2;
95555796c8dcSSimon Schubert     }
95565796c8dcSSimon Schubert 
95575796c8dcSSimon Schubert   /* Check for ";attachment".  If there is no attachment, the
95585796c8dcSSimon Schubert      packet should end here.  */
95595796c8dcSSimon Schubert   if (*p == ';')
95605796c8dcSSimon Schubert     {
95615796c8dcSSimon Schubert       *attachment = p + 1;
95625796c8dcSSimon Schubert       return 0;
95635796c8dcSSimon Schubert     }
95645796c8dcSSimon Schubert   else if (*p == '\0')
95655796c8dcSSimon Schubert     return 0;
95665796c8dcSSimon Schubert   else
95675796c8dcSSimon Schubert     return -1;
95685796c8dcSSimon Schubert }
95695796c8dcSSimon Schubert 
95705796c8dcSSimon Schubert /* Send a prepared I/O packet to the target and read its response.
95715796c8dcSSimon Schubert    The prepared packet is in the global RS->BUF before this function
95725796c8dcSSimon Schubert    is called, and the answer is there when we return.
95735796c8dcSSimon Schubert 
95745796c8dcSSimon Schubert    COMMAND_BYTES is the length of the request to send, which may include
95755796c8dcSSimon Schubert    binary data.  WHICH_PACKET is the packet configuration to check
95765796c8dcSSimon Schubert    before attempting a packet.  If an error occurs, *REMOTE_ERRNO
95775796c8dcSSimon Schubert    is set to the error number and -1 is returned.  Otherwise the value
95785796c8dcSSimon Schubert    returned by the function is returned.
95795796c8dcSSimon Schubert 
95805796c8dcSSimon Schubert    ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
95815796c8dcSSimon Schubert    attachment is expected; an error will be reported if there's a
95825796c8dcSSimon Schubert    mismatch.  If one is found, *ATTACHMENT will be set to point into
95835796c8dcSSimon Schubert    the packet buffer and *ATTACHMENT_LEN will be set to the
95845796c8dcSSimon Schubert    attachment's length.  */
95855796c8dcSSimon Schubert 
95865796c8dcSSimon Schubert static int
remote_hostio_send_command(int command_bytes,int which_packet,int * remote_errno,char ** attachment,int * attachment_len)95875796c8dcSSimon Schubert remote_hostio_send_command (int command_bytes, int which_packet,
95885796c8dcSSimon Schubert 			    int *remote_errno, char **attachment,
95895796c8dcSSimon Schubert 			    int *attachment_len)
95905796c8dcSSimon Schubert {
95915796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
95925796c8dcSSimon Schubert   int ret, bytes_read;
95935796c8dcSSimon Schubert   char *attachment_tmp;
95945796c8dcSSimon Schubert 
95955796c8dcSSimon Schubert   if (!remote_desc
95965796c8dcSSimon Schubert       || remote_protocol_packets[which_packet].support == PACKET_DISABLE)
95975796c8dcSSimon Schubert     {
95985796c8dcSSimon Schubert       *remote_errno = FILEIO_ENOSYS;
95995796c8dcSSimon Schubert       return -1;
96005796c8dcSSimon Schubert     }
96015796c8dcSSimon Schubert 
96025796c8dcSSimon Schubert   putpkt_binary (rs->buf, command_bytes);
96035796c8dcSSimon Schubert   bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
96045796c8dcSSimon Schubert 
96055796c8dcSSimon Schubert   /* If it timed out, something is wrong.  Don't try to parse the
96065796c8dcSSimon Schubert      buffer.  */
96075796c8dcSSimon Schubert   if (bytes_read < 0)
96085796c8dcSSimon Schubert     {
96095796c8dcSSimon Schubert       *remote_errno = FILEIO_EINVAL;
96105796c8dcSSimon Schubert       return -1;
96115796c8dcSSimon Schubert     }
96125796c8dcSSimon Schubert 
96135796c8dcSSimon Schubert   switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
96145796c8dcSSimon Schubert     {
96155796c8dcSSimon Schubert     case PACKET_ERROR:
96165796c8dcSSimon Schubert       *remote_errno = FILEIO_EINVAL;
96175796c8dcSSimon Schubert       return -1;
96185796c8dcSSimon Schubert     case PACKET_UNKNOWN:
96195796c8dcSSimon Schubert       *remote_errno = FILEIO_ENOSYS;
96205796c8dcSSimon Schubert       return -1;
96215796c8dcSSimon Schubert     case PACKET_OK:
96225796c8dcSSimon Schubert       break;
96235796c8dcSSimon Schubert     }
96245796c8dcSSimon Schubert 
96255796c8dcSSimon Schubert   if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
96265796c8dcSSimon Schubert 				  &attachment_tmp))
96275796c8dcSSimon Schubert     {
96285796c8dcSSimon Schubert       *remote_errno = FILEIO_EINVAL;
96295796c8dcSSimon Schubert       return -1;
96305796c8dcSSimon Schubert     }
96315796c8dcSSimon Schubert 
96325796c8dcSSimon Schubert   /* Make sure we saw an attachment if and only if we expected one.  */
96335796c8dcSSimon Schubert   if ((attachment_tmp == NULL && attachment != NULL)
96345796c8dcSSimon Schubert       || (attachment_tmp != NULL && attachment == NULL))
96355796c8dcSSimon Schubert     {
96365796c8dcSSimon Schubert       *remote_errno = FILEIO_EINVAL;
96375796c8dcSSimon Schubert       return -1;
96385796c8dcSSimon Schubert     }
96395796c8dcSSimon Schubert 
96405796c8dcSSimon Schubert   /* If an attachment was found, it must point into the packet buffer;
96415796c8dcSSimon Schubert      work out how many bytes there were.  */
96425796c8dcSSimon Schubert   if (attachment_tmp != NULL)
96435796c8dcSSimon Schubert     {
96445796c8dcSSimon Schubert       *attachment = attachment_tmp;
96455796c8dcSSimon Schubert       *attachment_len = bytes_read - (*attachment - rs->buf);
96465796c8dcSSimon Schubert     }
96475796c8dcSSimon Schubert 
96485796c8dcSSimon Schubert   return ret;
96495796c8dcSSimon Schubert }
96505796c8dcSSimon Schubert 
96515796c8dcSSimon Schubert /* Open FILENAME on the remote target, using FLAGS and MODE.  Return a
96525796c8dcSSimon Schubert    remote file descriptor, or -1 if an error occurs (and set
96535796c8dcSSimon Schubert    *REMOTE_ERRNO).  */
96545796c8dcSSimon Schubert 
96555796c8dcSSimon Schubert static int
remote_hostio_open(const char * filename,int flags,int mode,int * remote_errno)96565796c8dcSSimon Schubert remote_hostio_open (const char *filename, int flags, int mode,
96575796c8dcSSimon Schubert 		    int *remote_errno)
96585796c8dcSSimon Schubert {
96595796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
96605796c8dcSSimon Schubert   char *p = rs->buf;
96615796c8dcSSimon Schubert   int left = get_remote_packet_size () - 1;
96625796c8dcSSimon Schubert 
96635796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, "vFile:open:");
96645796c8dcSSimon Schubert 
96655796c8dcSSimon Schubert   remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
96665796c8dcSSimon Schubert 			   strlen (filename));
96675796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, ",");
96685796c8dcSSimon Schubert 
96695796c8dcSSimon Schubert   remote_buffer_add_int (&p, &left, flags);
96705796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, ",");
96715796c8dcSSimon Schubert 
96725796c8dcSSimon Schubert   remote_buffer_add_int (&p, &left, mode);
96735796c8dcSSimon Schubert 
96745796c8dcSSimon Schubert   return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
96755796c8dcSSimon Schubert 				     remote_errno, NULL, NULL);
96765796c8dcSSimon Schubert }
96775796c8dcSSimon Schubert 
96785796c8dcSSimon Schubert /* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
96795796c8dcSSimon Schubert    Return the number of bytes written, or -1 if an error occurs (and
96805796c8dcSSimon Schubert    set *REMOTE_ERRNO).  */
96815796c8dcSSimon Schubert 
96825796c8dcSSimon Schubert static int
remote_hostio_pwrite(int fd,const gdb_byte * write_buf,int len,ULONGEST offset,int * remote_errno)96835796c8dcSSimon Schubert remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
96845796c8dcSSimon Schubert 		      ULONGEST offset, int *remote_errno)
96855796c8dcSSimon Schubert {
96865796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
96875796c8dcSSimon Schubert   char *p = rs->buf;
96885796c8dcSSimon Schubert   int left = get_remote_packet_size ();
96895796c8dcSSimon Schubert   int out_len;
96905796c8dcSSimon Schubert 
96915796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, "vFile:pwrite:");
96925796c8dcSSimon Schubert 
96935796c8dcSSimon Schubert   remote_buffer_add_int (&p, &left, fd);
96945796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, ",");
96955796c8dcSSimon Schubert 
96965796c8dcSSimon Schubert   remote_buffer_add_int (&p, &left, offset);
96975796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, ",");
96985796c8dcSSimon Schubert 
96995796c8dcSSimon Schubert   p += remote_escape_output (write_buf, len, p, &out_len,
97005796c8dcSSimon Schubert 			     get_remote_packet_size () - (p - rs->buf));
97015796c8dcSSimon Schubert 
97025796c8dcSSimon Schubert   return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
97035796c8dcSSimon Schubert 				     remote_errno, NULL, NULL);
97045796c8dcSSimon Schubert }
97055796c8dcSSimon Schubert 
97065796c8dcSSimon Schubert /* Read up to LEN bytes FD on the remote target into READ_BUF
97075796c8dcSSimon Schubert    Return the number of bytes read, or -1 if an error occurs (and
97085796c8dcSSimon Schubert    set *REMOTE_ERRNO).  */
97095796c8dcSSimon Schubert 
97105796c8dcSSimon Schubert static int
remote_hostio_pread(int fd,gdb_byte * read_buf,int len,ULONGEST offset,int * remote_errno)97115796c8dcSSimon Schubert remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
97125796c8dcSSimon Schubert 		     ULONGEST offset, int *remote_errno)
97135796c8dcSSimon Schubert {
97145796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
97155796c8dcSSimon Schubert   char *p = rs->buf;
97165796c8dcSSimon Schubert   char *attachment;
97175796c8dcSSimon Schubert   int left = get_remote_packet_size ();
97185796c8dcSSimon Schubert   int ret, attachment_len;
97195796c8dcSSimon Schubert   int read_len;
97205796c8dcSSimon Schubert 
97215796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, "vFile:pread:");
97225796c8dcSSimon Schubert 
97235796c8dcSSimon Schubert   remote_buffer_add_int (&p, &left, fd);
97245796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, ",");
97255796c8dcSSimon Schubert 
97265796c8dcSSimon Schubert   remote_buffer_add_int (&p, &left, len);
97275796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, ",");
97285796c8dcSSimon Schubert 
97295796c8dcSSimon Schubert   remote_buffer_add_int (&p, &left, offset);
97305796c8dcSSimon Schubert 
97315796c8dcSSimon Schubert   ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
97325796c8dcSSimon Schubert 				    remote_errno, &attachment,
97335796c8dcSSimon Schubert 				    &attachment_len);
97345796c8dcSSimon Schubert 
97355796c8dcSSimon Schubert   if (ret < 0)
97365796c8dcSSimon Schubert     return ret;
97375796c8dcSSimon Schubert 
97385796c8dcSSimon Schubert   read_len = remote_unescape_input (attachment, attachment_len,
97395796c8dcSSimon Schubert 				    read_buf, len);
97405796c8dcSSimon Schubert   if (read_len != ret)
97415796c8dcSSimon Schubert     error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
97425796c8dcSSimon Schubert 
97435796c8dcSSimon Schubert   return ret;
97445796c8dcSSimon Schubert }
97455796c8dcSSimon Schubert 
97465796c8dcSSimon Schubert /* Close FD on the remote target.  Return 0, or -1 if an error occurs
97475796c8dcSSimon Schubert    (and set *REMOTE_ERRNO).  */
97485796c8dcSSimon Schubert 
97495796c8dcSSimon Schubert static int
remote_hostio_close(int fd,int * remote_errno)97505796c8dcSSimon Schubert remote_hostio_close (int fd, int *remote_errno)
97515796c8dcSSimon Schubert {
97525796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
97535796c8dcSSimon Schubert   char *p = rs->buf;
97545796c8dcSSimon Schubert   int left = get_remote_packet_size () - 1;
97555796c8dcSSimon Schubert 
97565796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, "vFile:close:");
97575796c8dcSSimon Schubert 
97585796c8dcSSimon Schubert   remote_buffer_add_int (&p, &left, fd);
97595796c8dcSSimon Schubert 
97605796c8dcSSimon Schubert   return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
97615796c8dcSSimon Schubert 				     remote_errno, NULL, NULL);
97625796c8dcSSimon Schubert }
97635796c8dcSSimon Schubert 
97645796c8dcSSimon Schubert /* Unlink FILENAME on the remote target.  Return 0, or -1 if an error
97655796c8dcSSimon Schubert    occurs (and set *REMOTE_ERRNO).  */
97665796c8dcSSimon Schubert 
97675796c8dcSSimon Schubert static int
remote_hostio_unlink(const char * filename,int * remote_errno)97685796c8dcSSimon Schubert remote_hostio_unlink (const char *filename, int *remote_errno)
97695796c8dcSSimon Schubert {
97705796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
97715796c8dcSSimon Schubert   char *p = rs->buf;
97725796c8dcSSimon Schubert   int left = get_remote_packet_size () - 1;
97735796c8dcSSimon Schubert 
97745796c8dcSSimon Schubert   remote_buffer_add_string (&p, &left, "vFile:unlink:");
97755796c8dcSSimon Schubert 
97765796c8dcSSimon Schubert   remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
97775796c8dcSSimon Schubert 			   strlen (filename));
97785796c8dcSSimon Schubert 
97795796c8dcSSimon Schubert   return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
97805796c8dcSSimon Schubert 				     remote_errno, NULL, NULL);
97815796c8dcSSimon Schubert }
97825796c8dcSSimon Schubert 
9783ef5ccd6cSJohn Marino /* Read value of symbolic link FILENAME on the remote target.  Return
9784ef5ccd6cSJohn Marino    a null-terminated string allocated via xmalloc, or NULL if an error
9785ef5ccd6cSJohn Marino    occurs (and set *REMOTE_ERRNO).  */
9786ef5ccd6cSJohn Marino 
9787ef5ccd6cSJohn Marino static char *
remote_hostio_readlink(const char * filename,int * remote_errno)9788ef5ccd6cSJohn Marino remote_hostio_readlink (const char *filename, int *remote_errno)
9789ef5ccd6cSJohn Marino {
9790ef5ccd6cSJohn Marino   struct remote_state *rs = get_remote_state ();
9791ef5ccd6cSJohn Marino   char *p = rs->buf;
9792ef5ccd6cSJohn Marino   char *attachment;
9793ef5ccd6cSJohn Marino   int left = get_remote_packet_size ();
9794ef5ccd6cSJohn Marino   int len, attachment_len;
9795ef5ccd6cSJohn Marino   int read_len;
9796ef5ccd6cSJohn Marino   char *ret;
9797ef5ccd6cSJohn Marino 
9798ef5ccd6cSJohn Marino   remote_buffer_add_string (&p, &left, "vFile:readlink:");
9799ef5ccd6cSJohn Marino 
9800ef5ccd6cSJohn Marino   remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9801ef5ccd6cSJohn Marino 			   strlen (filename));
9802ef5ccd6cSJohn Marino 
9803ef5ccd6cSJohn Marino   len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
9804ef5ccd6cSJohn Marino 				    remote_errno, &attachment,
9805ef5ccd6cSJohn Marino 				    &attachment_len);
9806ef5ccd6cSJohn Marino 
9807ef5ccd6cSJohn Marino   if (len < 0)
9808ef5ccd6cSJohn Marino     return NULL;
9809ef5ccd6cSJohn Marino 
9810ef5ccd6cSJohn Marino   ret = xmalloc (len + 1);
9811ef5ccd6cSJohn Marino 
9812ef5ccd6cSJohn Marino   read_len = remote_unescape_input (attachment, attachment_len,
9813ef5ccd6cSJohn Marino 				    ret, len);
9814ef5ccd6cSJohn Marino   if (read_len != len)
9815ef5ccd6cSJohn Marino     error (_("Readlink returned %d, but %d bytes."), len, read_len);
9816ef5ccd6cSJohn Marino 
9817ef5ccd6cSJohn Marino   ret[len] = '\0';
9818ef5ccd6cSJohn Marino   return ret;
9819ef5ccd6cSJohn Marino }
9820ef5ccd6cSJohn Marino 
98215796c8dcSSimon Schubert static int
remote_fileio_errno_to_host(int errnum)98225796c8dcSSimon Schubert remote_fileio_errno_to_host (int errnum)
98235796c8dcSSimon Schubert {
98245796c8dcSSimon Schubert   switch (errnum)
98255796c8dcSSimon Schubert     {
98265796c8dcSSimon Schubert       case FILEIO_EPERM:
98275796c8dcSSimon Schubert         return EPERM;
98285796c8dcSSimon Schubert       case FILEIO_ENOENT:
98295796c8dcSSimon Schubert         return ENOENT;
98305796c8dcSSimon Schubert       case FILEIO_EINTR:
98315796c8dcSSimon Schubert         return EINTR;
98325796c8dcSSimon Schubert       case FILEIO_EIO:
98335796c8dcSSimon Schubert         return EIO;
98345796c8dcSSimon Schubert       case FILEIO_EBADF:
98355796c8dcSSimon Schubert         return EBADF;
98365796c8dcSSimon Schubert       case FILEIO_EACCES:
98375796c8dcSSimon Schubert         return EACCES;
98385796c8dcSSimon Schubert       case FILEIO_EFAULT:
98395796c8dcSSimon Schubert         return EFAULT;
98405796c8dcSSimon Schubert       case FILEIO_EBUSY:
98415796c8dcSSimon Schubert         return EBUSY;
98425796c8dcSSimon Schubert       case FILEIO_EEXIST:
98435796c8dcSSimon Schubert         return EEXIST;
98445796c8dcSSimon Schubert       case FILEIO_ENODEV:
98455796c8dcSSimon Schubert         return ENODEV;
98465796c8dcSSimon Schubert       case FILEIO_ENOTDIR:
98475796c8dcSSimon Schubert         return ENOTDIR;
98485796c8dcSSimon Schubert       case FILEIO_EISDIR:
98495796c8dcSSimon Schubert         return EISDIR;
98505796c8dcSSimon Schubert       case FILEIO_EINVAL:
98515796c8dcSSimon Schubert         return EINVAL;
98525796c8dcSSimon Schubert       case FILEIO_ENFILE:
98535796c8dcSSimon Schubert         return ENFILE;
98545796c8dcSSimon Schubert       case FILEIO_EMFILE:
98555796c8dcSSimon Schubert         return EMFILE;
98565796c8dcSSimon Schubert       case FILEIO_EFBIG:
98575796c8dcSSimon Schubert         return EFBIG;
98585796c8dcSSimon Schubert       case FILEIO_ENOSPC:
98595796c8dcSSimon Schubert         return ENOSPC;
98605796c8dcSSimon Schubert       case FILEIO_ESPIPE:
98615796c8dcSSimon Schubert         return ESPIPE;
98625796c8dcSSimon Schubert       case FILEIO_EROFS:
98635796c8dcSSimon Schubert         return EROFS;
98645796c8dcSSimon Schubert       case FILEIO_ENOSYS:
98655796c8dcSSimon Schubert         return ENOSYS;
98665796c8dcSSimon Schubert       case FILEIO_ENAMETOOLONG:
98675796c8dcSSimon Schubert         return ENAMETOOLONG;
98685796c8dcSSimon Schubert     }
98695796c8dcSSimon Schubert   return -1;
98705796c8dcSSimon Schubert }
98715796c8dcSSimon Schubert 
98725796c8dcSSimon Schubert static char *
remote_hostio_error(int errnum)98735796c8dcSSimon Schubert remote_hostio_error (int errnum)
98745796c8dcSSimon Schubert {
98755796c8dcSSimon Schubert   int host_error = remote_fileio_errno_to_host (errnum);
98765796c8dcSSimon Schubert 
98775796c8dcSSimon Schubert   if (host_error == -1)
98785796c8dcSSimon Schubert     error (_("Unknown remote I/O error %d"), errnum);
98795796c8dcSSimon Schubert   else
98805796c8dcSSimon Schubert     error (_("Remote I/O error: %s"), safe_strerror (host_error));
98815796c8dcSSimon Schubert }
98825796c8dcSSimon Schubert 
98835796c8dcSSimon Schubert static void
remote_hostio_close_cleanup(void * opaque)98845796c8dcSSimon Schubert remote_hostio_close_cleanup (void *opaque)
98855796c8dcSSimon Schubert {
98865796c8dcSSimon Schubert   int fd = *(int *) opaque;
98875796c8dcSSimon Schubert   int remote_errno;
98885796c8dcSSimon Schubert 
98895796c8dcSSimon Schubert   remote_hostio_close (fd, &remote_errno);
98905796c8dcSSimon Schubert }
98915796c8dcSSimon Schubert 
98925796c8dcSSimon Schubert 
98935796c8dcSSimon Schubert static void *
remote_bfd_iovec_open(struct bfd * abfd,void * open_closure)98945796c8dcSSimon Schubert remote_bfd_iovec_open (struct bfd *abfd, void *open_closure)
98955796c8dcSSimon Schubert {
98965796c8dcSSimon Schubert   const char *filename = bfd_get_filename (abfd);
98975796c8dcSSimon Schubert   int fd, remote_errno;
98985796c8dcSSimon Schubert   int *stream;
98995796c8dcSSimon Schubert 
99005796c8dcSSimon Schubert   gdb_assert (remote_filename_p (filename));
99015796c8dcSSimon Schubert 
99025796c8dcSSimon Schubert   fd = remote_hostio_open (filename + 7, FILEIO_O_RDONLY, 0, &remote_errno);
99035796c8dcSSimon Schubert   if (fd == -1)
99045796c8dcSSimon Schubert     {
99055796c8dcSSimon Schubert       errno = remote_fileio_errno_to_host (remote_errno);
99065796c8dcSSimon Schubert       bfd_set_error (bfd_error_system_call);
99075796c8dcSSimon Schubert       return NULL;
99085796c8dcSSimon Schubert     }
99095796c8dcSSimon Schubert 
99105796c8dcSSimon Schubert   stream = xmalloc (sizeof (int));
99115796c8dcSSimon Schubert   *stream = fd;
99125796c8dcSSimon Schubert   return stream;
99135796c8dcSSimon Schubert }
99145796c8dcSSimon Schubert 
99155796c8dcSSimon Schubert static int
remote_bfd_iovec_close(struct bfd * abfd,void * stream)99165796c8dcSSimon Schubert remote_bfd_iovec_close (struct bfd *abfd, void *stream)
99175796c8dcSSimon Schubert {
99185796c8dcSSimon Schubert   int fd = *(int *)stream;
99195796c8dcSSimon Schubert   int remote_errno;
99205796c8dcSSimon Schubert 
99215796c8dcSSimon Schubert   xfree (stream);
99225796c8dcSSimon Schubert 
99235796c8dcSSimon Schubert   /* Ignore errors on close; these may happen if the remote
99245796c8dcSSimon Schubert      connection was already torn down.  */
99255796c8dcSSimon Schubert   remote_hostio_close (fd, &remote_errno);
99265796c8dcSSimon Schubert 
9927ef5ccd6cSJohn Marino   /* Zero means success.  */
9928ef5ccd6cSJohn Marino   return 0;
99295796c8dcSSimon Schubert }
99305796c8dcSSimon Schubert 
99315796c8dcSSimon Schubert static file_ptr
remote_bfd_iovec_pread(struct bfd * abfd,void * stream,void * buf,file_ptr nbytes,file_ptr offset)99325796c8dcSSimon Schubert remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
99335796c8dcSSimon Schubert 			file_ptr nbytes, file_ptr offset)
99345796c8dcSSimon Schubert {
99355796c8dcSSimon Schubert   int fd = *(int *)stream;
99365796c8dcSSimon Schubert   int remote_errno;
99375796c8dcSSimon Schubert   file_ptr pos, bytes;
99385796c8dcSSimon Schubert 
99395796c8dcSSimon Schubert   pos = 0;
99405796c8dcSSimon Schubert   while (nbytes > pos)
99415796c8dcSSimon Schubert     {
99425796c8dcSSimon Schubert       bytes = remote_hostio_pread (fd, (char *)buf + pos, nbytes - pos,
99435796c8dcSSimon Schubert 				   offset + pos, &remote_errno);
99445796c8dcSSimon Schubert       if (bytes == 0)
99455796c8dcSSimon Schubert         /* Success, but no bytes, means end-of-file.  */
99465796c8dcSSimon Schubert         break;
99475796c8dcSSimon Schubert       if (bytes == -1)
99485796c8dcSSimon Schubert 	{
99495796c8dcSSimon Schubert 	  errno = remote_fileio_errno_to_host (remote_errno);
99505796c8dcSSimon Schubert 	  bfd_set_error (bfd_error_system_call);
99515796c8dcSSimon Schubert 	  return -1;
99525796c8dcSSimon Schubert 	}
99535796c8dcSSimon Schubert 
99545796c8dcSSimon Schubert       pos += bytes;
99555796c8dcSSimon Schubert     }
99565796c8dcSSimon Schubert 
99575796c8dcSSimon Schubert   return pos;
99585796c8dcSSimon Schubert }
99595796c8dcSSimon Schubert 
99605796c8dcSSimon Schubert static int
remote_bfd_iovec_stat(struct bfd * abfd,void * stream,struct stat * sb)99615796c8dcSSimon Schubert remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
99625796c8dcSSimon Schubert {
99635796c8dcSSimon Schubert   /* FIXME: We should probably implement remote_hostio_stat.  */
99645796c8dcSSimon Schubert   sb->st_size = INT_MAX;
99655796c8dcSSimon Schubert   return 0;
99665796c8dcSSimon Schubert }
99675796c8dcSSimon Schubert 
99685796c8dcSSimon Schubert int
remote_filename_p(const char * filename)99695796c8dcSSimon Schubert remote_filename_p (const char *filename)
99705796c8dcSSimon Schubert {
99715796c8dcSSimon Schubert   return strncmp (filename, "remote:", 7) == 0;
99725796c8dcSSimon Schubert }
99735796c8dcSSimon Schubert 
99745796c8dcSSimon Schubert bfd *
remote_bfd_open(const char * remote_file,const char * target)99755796c8dcSSimon Schubert remote_bfd_open (const char *remote_file, const char *target)
99765796c8dcSSimon Schubert {
9977ef5ccd6cSJohn Marino   bfd *abfd = gdb_bfd_openr_iovec (remote_file, target,
99785796c8dcSSimon Schubert 				   remote_bfd_iovec_open, NULL,
99795796c8dcSSimon Schubert 				   remote_bfd_iovec_pread,
99805796c8dcSSimon Schubert 				   remote_bfd_iovec_close,
99815796c8dcSSimon Schubert 				   remote_bfd_iovec_stat);
9982ef5ccd6cSJohn Marino 
9983ef5ccd6cSJohn Marino   return abfd;
99845796c8dcSSimon Schubert }
99855796c8dcSSimon Schubert 
99865796c8dcSSimon Schubert void
remote_file_put(const char * local_file,const char * remote_file,int from_tty)99875796c8dcSSimon Schubert remote_file_put (const char *local_file, const char *remote_file, int from_tty)
99885796c8dcSSimon Schubert {
99895796c8dcSSimon Schubert   struct cleanup *back_to, *close_cleanup;
99905796c8dcSSimon Schubert   int retcode, fd, remote_errno, bytes, io_size;
99915796c8dcSSimon Schubert   FILE *file;
99925796c8dcSSimon Schubert   gdb_byte *buffer;
99935796c8dcSSimon Schubert   int bytes_in_buffer;
99945796c8dcSSimon Schubert   int saw_eof;
99955796c8dcSSimon Schubert   ULONGEST offset;
99965796c8dcSSimon Schubert 
99975796c8dcSSimon Schubert   if (!remote_desc)
99985796c8dcSSimon Schubert     error (_("command can only be used with remote target"));
99995796c8dcSSimon Schubert 
100005796c8dcSSimon Schubert   file = fopen (local_file, "rb");
100015796c8dcSSimon Schubert   if (file == NULL)
100025796c8dcSSimon Schubert     perror_with_name (local_file);
100035796c8dcSSimon Schubert   back_to = make_cleanup_fclose (file);
100045796c8dcSSimon Schubert 
100055796c8dcSSimon Schubert   fd = remote_hostio_open (remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
100065796c8dcSSimon Schubert 					 | FILEIO_O_TRUNC),
100075796c8dcSSimon Schubert 			   0700, &remote_errno);
100085796c8dcSSimon Schubert   if (fd == -1)
100095796c8dcSSimon Schubert     remote_hostio_error (remote_errno);
100105796c8dcSSimon Schubert 
100115796c8dcSSimon Schubert   /* Send up to this many bytes at once.  They won't all fit in the
100125796c8dcSSimon Schubert      remote packet limit, so we'll transfer slightly fewer.  */
100135796c8dcSSimon Schubert   io_size = get_remote_packet_size ();
100145796c8dcSSimon Schubert   buffer = xmalloc (io_size);
100155796c8dcSSimon Schubert   make_cleanup (xfree, buffer);
100165796c8dcSSimon Schubert 
100175796c8dcSSimon Schubert   close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
100185796c8dcSSimon Schubert 
100195796c8dcSSimon Schubert   bytes_in_buffer = 0;
100205796c8dcSSimon Schubert   saw_eof = 0;
100215796c8dcSSimon Schubert   offset = 0;
100225796c8dcSSimon Schubert   while (bytes_in_buffer || !saw_eof)
100235796c8dcSSimon Schubert     {
100245796c8dcSSimon Schubert       if (!saw_eof)
100255796c8dcSSimon Schubert 	{
10026c50c785cSJohn Marino 	  bytes = fread (buffer + bytes_in_buffer, 1,
10027c50c785cSJohn Marino 			 io_size - bytes_in_buffer,
100285796c8dcSSimon Schubert 			 file);
100295796c8dcSSimon Schubert 	  if (bytes == 0)
100305796c8dcSSimon Schubert 	    {
100315796c8dcSSimon Schubert 	      if (ferror (file))
100325796c8dcSSimon Schubert 		error (_("Error reading %s."), local_file);
100335796c8dcSSimon Schubert 	      else
100345796c8dcSSimon Schubert 		{
100355796c8dcSSimon Schubert 		  /* EOF.  Unless there is something still in the
100365796c8dcSSimon Schubert 		     buffer from the last iteration, we are done.  */
100375796c8dcSSimon Schubert 		  saw_eof = 1;
100385796c8dcSSimon Schubert 		  if (bytes_in_buffer == 0)
100395796c8dcSSimon Schubert 		    break;
100405796c8dcSSimon Schubert 		}
100415796c8dcSSimon Schubert 	    }
100425796c8dcSSimon Schubert 	}
100435796c8dcSSimon Schubert       else
100445796c8dcSSimon Schubert 	bytes = 0;
100455796c8dcSSimon Schubert 
100465796c8dcSSimon Schubert       bytes += bytes_in_buffer;
100475796c8dcSSimon Schubert       bytes_in_buffer = 0;
100485796c8dcSSimon Schubert 
10049c50c785cSJohn Marino       retcode = remote_hostio_pwrite (fd, buffer, bytes,
10050c50c785cSJohn Marino 				      offset, &remote_errno);
100515796c8dcSSimon Schubert 
100525796c8dcSSimon Schubert       if (retcode < 0)
100535796c8dcSSimon Schubert 	remote_hostio_error (remote_errno);
100545796c8dcSSimon Schubert       else if (retcode == 0)
100555796c8dcSSimon Schubert 	error (_("Remote write of %d bytes returned 0!"), bytes);
100565796c8dcSSimon Schubert       else if (retcode < bytes)
100575796c8dcSSimon Schubert 	{
100585796c8dcSSimon Schubert 	  /* Short write.  Save the rest of the read data for the next
100595796c8dcSSimon Schubert 	     write.  */
100605796c8dcSSimon Schubert 	  bytes_in_buffer = bytes - retcode;
100615796c8dcSSimon Schubert 	  memmove (buffer, buffer + retcode, bytes_in_buffer);
100625796c8dcSSimon Schubert 	}
100635796c8dcSSimon Schubert 
100645796c8dcSSimon Schubert       offset += retcode;
100655796c8dcSSimon Schubert     }
100665796c8dcSSimon Schubert 
100675796c8dcSSimon Schubert   discard_cleanups (close_cleanup);
100685796c8dcSSimon Schubert   if (remote_hostio_close (fd, &remote_errno))
100695796c8dcSSimon Schubert     remote_hostio_error (remote_errno);
100705796c8dcSSimon Schubert 
100715796c8dcSSimon Schubert   if (from_tty)
100725796c8dcSSimon Schubert     printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
100735796c8dcSSimon Schubert   do_cleanups (back_to);
100745796c8dcSSimon Schubert }
100755796c8dcSSimon Schubert 
100765796c8dcSSimon Schubert void
remote_file_get(const char * remote_file,const char * local_file,int from_tty)100775796c8dcSSimon Schubert remote_file_get (const char *remote_file, const char *local_file, int from_tty)
100785796c8dcSSimon Schubert {
100795796c8dcSSimon Schubert   struct cleanup *back_to, *close_cleanup;
100805796c8dcSSimon Schubert   int fd, remote_errno, bytes, io_size;
100815796c8dcSSimon Schubert   FILE *file;
100825796c8dcSSimon Schubert   gdb_byte *buffer;
100835796c8dcSSimon Schubert   ULONGEST offset;
100845796c8dcSSimon Schubert 
100855796c8dcSSimon Schubert   if (!remote_desc)
100865796c8dcSSimon Schubert     error (_("command can only be used with remote target"));
100875796c8dcSSimon Schubert 
100885796c8dcSSimon Schubert   fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
100895796c8dcSSimon Schubert   if (fd == -1)
100905796c8dcSSimon Schubert     remote_hostio_error (remote_errno);
100915796c8dcSSimon Schubert 
100925796c8dcSSimon Schubert   file = fopen (local_file, "wb");
100935796c8dcSSimon Schubert   if (file == NULL)
100945796c8dcSSimon Schubert     perror_with_name (local_file);
100955796c8dcSSimon Schubert   back_to = make_cleanup_fclose (file);
100965796c8dcSSimon Schubert 
100975796c8dcSSimon Schubert   /* Send up to this many bytes at once.  They won't all fit in the
100985796c8dcSSimon Schubert      remote packet limit, so we'll transfer slightly fewer.  */
100995796c8dcSSimon Schubert   io_size = get_remote_packet_size ();
101005796c8dcSSimon Schubert   buffer = xmalloc (io_size);
101015796c8dcSSimon Schubert   make_cleanup (xfree, buffer);
101025796c8dcSSimon Schubert 
101035796c8dcSSimon Schubert   close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
101045796c8dcSSimon Schubert 
101055796c8dcSSimon Schubert   offset = 0;
101065796c8dcSSimon Schubert   while (1)
101075796c8dcSSimon Schubert     {
101085796c8dcSSimon Schubert       bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_errno);
101095796c8dcSSimon Schubert       if (bytes == 0)
101105796c8dcSSimon Schubert 	/* Success, but no bytes, means end-of-file.  */
101115796c8dcSSimon Schubert 	break;
101125796c8dcSSimon Schubert       if (bytes == -1)
101135796c8dcSSimon Schubert 	remote_hostio_error (remote_errno);
101145796c8dcSSimon Schubert 
101155796c8dcSSimon Schubert       offset += bytes;
101165796c8dcSSimon Schubert 
101175796c8dcSSimon Schubert       bytes = fwrite (buffer, 1, bytes, file);
101185796c8dcSSimon Schubert       if (bytes == 0)
101195796c8dcSSimon Schubert 	perror_with_name (local_file);
101205796c8dcSSimon Schubert     }
101215796c8dcSSimon Schubert 
101225796c8dcSSimon Schubert   discard_cleanups (close_cleanup);
101235796c8dcSSimon Schubert   if (remote_hostio_close (fd, &remote_errno))
101245796c8dcSSimon Schubert     remote_hostio_error (remote_errno);
101255796c8dcSSimon Schubert 
101265796c8dcSSimon Schubert   if (from_tty)
101275796c8dcSSimon Schubert     printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
101285796c8dcSSimon Schubert   do_cleanups (back_to);
101295796c8dcSSimon Schubert }
101305796c8dcSSimon Schubert 
101315796c8dcSSimon Schubert void
remote_file_delete(const char * remote_file,int from_tty)101325796c8dcSSimon Schubert remote_file_delete (const char *remote_file, int from_tty)
101335796c8dcSSimon Schubert {
101345796c8dcSSimon Schubert   int retcode, remote_errno;
101355796c8dcSSimon Schubert 
101365796c8dcSSimon Schubert   if (!remote_desc)
101375796c8dcSSimon Schubert     error (_("command can only be used with remote target"));
101385796c8dcSSimon Schubert 
101395796c8dcSSimon Schubert   retcode = remote_hostio_unlink (remote_file, &remote_errno);
101405796c8dcSSimon Schubert   if (retcode == -1)
101415796c8dcSSimon Schubert     remote_hostio_error (remote_errno);
101425796c8dcSSimon Schubert 
101435796c8dcSSimon Schubert   if (from_tty)
101445796c8dcSSimon Schubert     printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
101455796c8dcSSimon Schubert }
101465796c8dcSSimon Schubert 
101475796c8dcSSimon Schubert static void
remote_put_command(char * args,int from_tty)101485796c8dcSSimon Schubert remote_put_command (char *args, int from_tty)
101495796c8dcSSimon Schubert {
101505796c8dcSSimon Schubert   struct cleanup *back_to;
101515796c8dcSSimon Schubert   char **argv;
101525796c8dcSSimon Schubert 
101535796c8dcSSimon Schubert   if (args == NULL)
101545796c8dcSSimon Schubert     error_no_arg (_("file to put"));
101555796c8dcSSimon Schubert 
101565796c8dcSSimon Schubert   argv = gdb_buildargv (args);
101575796c8dcSSimon Schubert   back_to = make_cleanup_freeargv (argv);
101585796c8dcSSimon Schubert   if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
101595796c8dcSSimon Schubert     error (_("Invalid parameters to remote put"));
101605796c8dcSSimon Schubert 
101615796c8dcSSimon Schubert   remote_file_put (argv[0], argv[1], from_tty);
101625796c8dcSSimon Schubert 
101635796c8dcSSimon Schubert   do_cleanups (back_to);
101645796c8dcSSimon Schubert }
101655796c8dcSSimon Schubert 
101665796c8dcSSimon Schubert static void
remote_get_command(char * args,int from_tty)101675796c8dcSSimon Schubert remote_get_command (char *args, int from_tty)
101685796c8dcSSimon Schubert {
101695796c8dcSSimon Schubert   struct cleanup *back_to;
101705796c8dcSSimon Schubert   char **argv;
101715796c8dcSSimon Schubert 
101725796c8dcSSimon Schubert   if (args == NULL)
101735796c8dcSSimon Schubert     error_no_arg (_("file to get"));
101745796c8dcSSimon Schubert 
101755796c8dcSSimon Schubert   argv = gdb_buildargv (args);
101765796c8dcSSimon Schubert   back_to = make_cleanup_freeargv (argv);
101775796c8dcSSimon Schubert   if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
101785796c8dcSSimon Schubert     error (_("Invalid parameters to remote get"));
101795796c8dcSSimon Schubert 
101805796c8dcSSimon Schubert   remote_file_get (argv[0], argv[1], from_tty);
101815796c8dcSSimon Schubert 
101825796c8dcSSimon Schubert   do_cleanups (back_to);
101835796c8dcSSimon Schubert }
101845796c8dcSSimon Schubert 
101855796c8dcSSimon Schubert static void
remote_delete_command(char * args,int from_tty)101865796c8dcSSimon Schubert remote_delete_command (char *args, int from_tty)
101875796c8dcSSimon Schubert {
101885796c8dcSSimon Schubert   struct cleanup *back_to;
101895796c8dcSSimon Schubert   char **argv;
101905796c8dcSSimon Schubert 
101915796c8dcSSimon Schubert   if (args == NULL)
101925796c8dcSSimon Schubert     error_no_arg (_("file to delete"));
101935796c8dcSSimon Schubert 
101945796c8dcSSimon Schubert   argv = gdb_buildargv (args);
101955796c8dcSSimon Schubert   back_to = make_cleanup_freeargv (argv);
101965796c8dcSSimon Schubert   if (argv[0] == NULL || argv[1] != NULL)
101975796c8dcSSimon Schubert     error (_("Invalid parameters to remote delete"));
101985796c8dcSSimon Schubert 
101995796c8dcSSimon Schubert   remote_file_delete (argv[0], from_tty);
102005796c8dcSSimon Schubert 
102015796c8dcSSimon Schubert   do_cleanups (back_to);
102025796c8dcSSimon Schubert }
102035796c8dcSSimon Schubert 
102045796c8dcSSimon Schubert static void
remote_command(char * args,int from_tty)102055796c8dcSSimon Schubert remote_command (char *args, int from_tty)
102065796c8dcSSimon Schubert {
102075796c8dcSSimon Schubert   help_list (remote_cmdlist, "remote ", -1, gdb_stdout);
102085796c8dcSSimon Schubert }
102095796c8dcSSimon Schubert 
102105796c8dcSSimon Schubert static int
remote_can_execute_reverse(void)102115796c8dcSSimon Schubert remote_can_execute_reverse (void)
102125796c8dcSSimon Schubert {
102135796c8dcSSimon Schubert   if (remote_protocol_packets[PACKET_bs].support == PACKET_ENABLE
102145796c8dcSSimon Schubert       || remote_protocol_packets[PACKET_bc].support == PACKET_ENABLE)
102155796c8dcSSimon Schubert     return 1;
102165796c8dcSSimon Schubert   else
102175796c8dcSSimon Schubert     return 0;
102185796c8dcSSimon Schubert }
102195796c8dcSSimon Schubert 
102205796c8dcSSimon Schubert static int
remote_supports_non_stop(void)102215796c8dcSSimon Schubert remote_supports_non_stop (void)
102225796c8dcSSimon Schubert {
102235796c8dcSSimon Schubert   return 1;
102245796c8dcSSimon Schubert }
102255796c8dcSSimon Schubert 
102265796c8dcSSimon Schubert static int
remote_supports_disable_randomization(void)10227a45ae5f8SJohn Marino remote_supports_disable_randomization (void)
10228a45ae5f8SJohn Marino {
10229a45ae5f8SJohn Marino   /* Only supported in extended mode.  */
10230a45ae5f8SJohn Marino   return 0;
10231a45ae5f8SJohn Marino }
10232a45ae5f8SJohn Marino 
10233a45ae5f8SJohn Marino static int
remote_supports_multi_process(void)102345796c8dcSSimon Schubert remote_supports_multi_process (void)
102355796c8dcSSimon Schubert {
102365796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
10237cf7f2e2dSJohn Marino 
10238ef5ccd6cSJohn Marino   /* Only extended-remote handles being attached to multiple
10239ef5ccd6cSJohn Marino      processes, even though plain remote can use the multi-process
10240ef5ccd6cSJohn Marino      thread id extensions, so that GDB knows the target process's
10241ef5ccd6cSJohn Marino      PID.  */
10242ef5ccd6cSJohn Marino   return rs->extended && remote_multi_process_p (rs);
102435796c8dcSSimon Schubert }
102445796c8dcSSimon Schubert 
10245ef5ccd6cSJohn Marino static int
remote_supports_cond_tracepoints(void)102465796c8dcSSimon Schubert remote_supports_cond_tracepoints (void)
102475796c8dcSSimon Schubert {
102485796c8dcSSimon Schubert   struct remote_state *rs = get_remote_state ();
10249cf7f2e2dSJohn Marino 
102505796c8dcSSimon Schubert   return rs->cond_tracepoints;
102515796c8dcSSimon Schubert }
102525796c8dcSSimon Schubert 
10253ef5ccd6cSJohn Marino static int
remote_supports_cond_breakpoints(void)10254ef5ccd6cSJohn Marino remote_supports_cond_breakpoints (void)
10255ef5ccd6cSJohn Marino {
10256ef5ccd6cSJohn Marino   struct remote_state *rs = get_remote_state ();
10257ef5ccd6cSJohn Marino 
10258ef5ccd6cSJohn Marino   return rs->cond_breakpoints;
10259ef5ccd6cSJohn Marino }
10260ef5ccd6cSJohn Marino 
10261ef5ccd6cSJohn Marino static int
remote_supports_fast_tracepoints(void)10262cf7f2e2dSJohn Marino remote_supports_fast_tracepoints (void)
10263cf7f2e2dSJohn Marino {
10264cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
10265cf7f2e2dSJohn Marino 
10266cf7f2e2dSJohn Marino   return rs->fast_tracepoints;
10267cf7f2e2dSJohn Marino }
10268cf7f2e2dSJohn Marino 
10269cf7f2e2dSJohn Marino static int
remote_supports_static_tracepoints(void)10270cf7f2e2dSJohn Marino remote_supports_static_tracepoints (void)
10271cf7f2e2dSJohn Marino {
10272cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
10273cf7f2e2dSJohn Marino 
10274cf7f2e2dSJohn Marino   return rs->static_tracepoints;
10275cf7f2e2dSJohn Marino }
10276cf7f2e2dSJohn Marino 
10277a45ae5f8SJohn Marino static int
remote_supports_install_in_trace(void)10278a45ae5f8SJohn Marino remote_supports_install_in_trace (void)
10279a45ae5f8SJohn Marino {
10280a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
10281a45ae5f8SJohn Marino 
10282a45ae5f8SJohn Marino   return rs->install_in_trace;
10283a45ae5f8SJohn Marino }
10284a45ae5f8SJohn Marino 
10285a45ae5f8SJohn Marino static int
remote_supports_enable_disable_tracepoint(void)10286a45ae5f8SJohn Marino remote_supports_enable_disable_tracepoint (void)
10287a45ae5f8SJohn Marino {
10288a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
10289a45ae5f8SJohn Marino 
10290a45ae5f8SJohn Marino   return rs->enable_disable_tracepoints;
10291a45ae5f8SJohn Marino }
10292a45ae5f8SJohn Marino 
10293a45ae5f8SJohn Marino static int
remote_supports_string_tracing(void)10294a45ae5f8SJohn Marino remote_supports_string_tracing (void)
10295a45ae5f8SJohn Marino {
10296a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
10297a45ae5f8SJohn Marino 
10298a45ae5f8SJohn Marino   return rs->string_tracing;
10299a45ae5f8SJohn Marino }
10300a45ae5f8SJohn Marino 
10301ef5ccd6cSJohn Marino static int
remote_can_run_breakpoint_commands(void)10302ef5ccd6cSJohn Marino remote_can_run_breakpoint_commands (void)
10303ef5ccd6cSJohn Marino {
10304ef5ccd6cSJohn Marino   struct remote_state *rs = get_remote_state ();
10305ef5ccd6cSJohn Marino 
10306ef5ccd6cSJohn Marino   return rs->breakpoint_commands;
10307ef5ccd6cSJohn Marino }
10308ef5ccd6cSJohn Marino 
10309cf7f2e2dSJohn Marino static void
remote_trace_init(void)10310cf7f2e2dSJohn Marino remote_trace_init (void)
10311cf7f2e2dSJohn Marino {
10312cf7f2e2dSJohn Marino   putpkt ("QTinit");
10313cf7f2e2dSJohn Marino   remote_get_noisy_reply (&target_buf, &target_buf_size);
10314cf7f2e2dSJohn Marino   if (strcmp (target_buf, "OK") != 0)
10315cf7f2e2dSJohn Marino     error (_("Target does not support this command."));
10316cf7f2e2dSJohn Marino }
10317cf7f2e2dSJohn Marino 
10318cf7f2e2dSJohn Marino static void free_actions_list (char **actions_list);
10319cf7f2e2dSJohn Marino static void free_actions_list_cleanup_wrapper (void *);
10320cf7f2e2dSJohn Marino static void
free_actions_list_cleanup_wrapper(void * al)10321cf7f2e2dSJohn Marino free_actions_list_cleanup_wrapper (void *al)
10322cf7f2e2dSJohn Marino {
10323cf7f2e2dSJohn Marino   free_actions_list (al);
10324cf7f2e2dSJohn Marino }
10325cf7f2e2dSJohn Marino 
10326cf7f2e2dSJohn Marino static void
free_actions_list(char ** actions_list)10327cf7f2e2dSJohn Marino free_actions_list (char **actions_list)
10328cf7f2e2dSJohn Marino {
10329cf7f2e2dSJohn Marino   int ndx;
10330cf7f2e2dSJohn Marino 
10331cf7f2e2dSJohn Marino   if (actions_list == 0)
10332cf7f2e2dSJohn Marino     return;
10333cf7f2e2dSJohn Marino 
10334cf7f2e2dSJohn Marino   for (ndx = 0; actions_list[ndx]; ndx++)
10335cf7f2e2dSJohn Marino     xfree (actions_list[ndx]);
10336cf7f2e2dSJohn Marino 
10337cf7f2e2dSJohn Marino   xfree (actions_list);
10338cf7f2e2dSJohn Marino }
10339cf7f2e2dSJohn Marino 
10340cf7f2e2dSJohn Marino /* Recursive routine to walk through command list including loops, and
10341cf7f2e2dSJohn Marino    download packets for each command.  */
10342cf7f2e2dSJohn Marino 
10343cf7f2e2dSJohn Marino static void
remote_download_command_source(int num,ULONGEST addr,struct command_line * cmds)10344cf7f2e2dSJohn Marino remote_download_command_source (int num, ULONGEST addr,
10345cf7f2e2dSJohn Marino 				struct command_line *cmds)
10346cf7f2e2dSJohn Marino {
10347cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
10348cf7f2e2dSJohn Marino   struct command_line *cmd;
10349cf7f2e2dSJohn Marino 
10350cf7f2e2dSJohn Marino   for (cmd = cmds; cmd; cmd = cmd->next)
10351cf7f2e2dSJohn Marino     {
10352c50c785cSJohn Marino       QUIT;	/* Allow user to bail out with ^C.  */
10353cf7f2e2dSJohn Marino       strcpy (rs->buf, "QTDPsrc:");
10354cf7f2e2dSJohn Marino       encode_source_string (num, addr, "cmd", cmd->line,
10355cf7f2e2dSJohn Marino 			    rs->buf + strlen (rs->buf),
10356cf7f2e2dSJohn Marino 			    rs->buf_size - strlen (rs->buf));
10357cf7f2e2dSJohn Marino       putpkt (rs->buf);
10358cf7f2e2dSJohn Marino       remote_get_noisy_reply (&target_buf, &target_buf_size);
10359cf7f2e2dSJohn Marino       if (strcmp (target_buf, "OK"))
10360cf7f2e2dSJohn Marino 	warning (_("Target does not support source download."));
10361cf7f2e2dSJohn Marino 
10362cf7f2e2dSJohn Marino       if (cmd->control_type == while_control
10363cf7f2e2dSJohn Marino 	  || cmd->control_type == while_stepping_control)
10364cf7f2e2dSJohn Marino 	{
10365cf7f2e2dSJohn Marino 	  remote_download_command_source (num, addr, *cmd->body_list);
10366cf7f2e2dSJohn Marino 
10367c50c785cSJohn Marino 	  QUIT;	/* Allow user to bail out with ^C.  */
10368cf7f2e2dSJohn Marino 	  strcpy (rs->buf, "QTDPsrc:");
10369cf7f2e2dSJohn Marino 	  encode_source_string (num, addr, "cmd", "end",
10370cf7f2e2dSJohn Marino 				rs->buf + strlen (rs->buf),
10371cf7f2e2dSJohn Marino 				rs->buf_size - strlen (rs->buf));
10372cf7f2e2dSJohn Marino 	  putpkt (rs->buf);
10373cf7f2e2dSJohn Marino 	  remote_get_noisy_reply (&target_buf, &target_buf_size);
10374cf7f2e2dSJohn Marino 	  if (strcmp (target_buf, "OK"))
10375cf7f2e2dSJohn Marino 	    warning (_("Target does not support source download."));
10376cf7f2e2dSJohn Marino 	}
10377cf7f2e2dSJohn Marino     }
10378cf7f2e2dSJohn Marino }
10379cf7f2e2dSJohn Marino 
10380cf7f2e2dSJohn Marino static void
remote_download_tracepoint(struct bp_location * loc)10381a45ae5f8SJohn Marino remote_download_tracepoint (struct bp_location *loc)
10382cf7f2e2dSJohn Marino {
10383ef5ccd6cSJohn Marino #define BUF_SIZE 2048
10384a45ae5f8SJohn Marino 
10385cf7f2e2dSJohn Marino   CORE_ADDR tpaddr;
10386cf7f2e2dSJohn Marino   char addrbuf[40];
10387ef5ccd6cSJohn Marino   char buf[BUF_SIZE];
10388cf7f2e2dSJohn Marino   char **tdp_actions;
10389cf7f2e2dSJohn Marino   char **stepping_actions;
10390cf7f2e2dSJohn Marino   int ndx;
10391cf7f2e2dSJohn Marino   struct cleanup *old_chain = NULL;
10392cf7f2e2dSJohn Marino   struct agent_expr *aexpr;
10393cf7f2e2dSJohn Marino   struct cleanup *aexpr_chain = NULL;
10394cf7f2e2dSJohn Marino   char *pkt;
10395a45ae5f8SJohn Marino   struct breakpoint *b = loc->owner;
10396a45ae5f8SJohn Marino   struct tracepoint *t = (struct tracepoint *) b;
10397cf7f2e2dSJohn Marino 
10398a45ae5f8SJohn Marino   encode_actions (loc->owner, loc, &tdp_actions, &stepping_actions);
10399cf7f2e2dSJohn Marino   old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
10400cf7f2e2dSJohn Marino 			    tdp_actions);
10401c50c785cSJohn Marino   (void) make_cleanup (free_actions_list_cleanup_wrapper,
10402c50c785cSJohn Marino 		       stepping_actions);
10403cf7f2e2dSJohn Marino 
10404cf7f2e2dSJohn Marino   tpaddr = loc->address;
10405cf7f2e2dSJohn Marino   sprintf_vma (addrbuf, tpaddr);
10406ef5ccd6cSJohn Marino   xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
10407cf7f2e2dSJohn Marino 	     addrbuf, /* address */
10408a45ae5f8SJohn Marino 	     (b->enable_state == bp_enabled ? 'E' : 'D'),
10409cf7f2e2dSJohn Marino 	     t->step_count, t->pass_count);
10410cf7f2e2dSJohn Marino   /* Fast tracepoints are mostly handled by the target, but we can
10411cf7f2e2dSJohn Marino      tell the target how big of an instruction block should be moved
10412cf7f2e2dSJohn Marino      around.  */
10413a45ae5f8SJohn Marino   if (b->type == bp_fast_tracepoint)
10414cf7f2e2dSJohn Marino     {
10415cf7f2e2dSJohn Marino       /* Only test for support at download time; we may not know
10416cf7f2e2dSJohn Marino 	 target capabilities at definition time.  */
10417cf7f2e2dSJohn Marino       if (remote_supports_fast_tracepoints ())
10418cf7f2e2dSJohn Marino 	{
10419cf7f2e2dSJohn Marino 	  int isize;
10420cf7f2e2dSJohn Marino 
10421ef5ccd6cSJohn Marino 	  if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (),
10422cf7f2e2dSJohn Marino 						tpaddr, &isize, NULL))
10423ef5ccd6cSJohn Marino 	    xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
10424ef5ccd6cSJohn Marino 		       isize);
10425cf7f2e2dSJohn Marino 	  else
10426cf7f2e2dSJohn Marino 	    /* If it passed validation at definition but fails now,
10427cf7f2e2dSJohn Marino 	       something is very wrong.  */
10428cf7f2e2dSJohn Marino 	    internal_error (__FILE__, __LINE__,
10429c50c785cSJohn Marino 			    _("Fast tracepoint not "
10430c50c785cSJohn Marino 			      "valid during download"));
10431cf7f2e2dSJohn Marino 	}
10432cf7f2e2dSJohn Marino       else
10433cf7f2e2dSJohn Marino 	/* Fast tracepoints are functionally identical to regular
10434cf7f2e2dSJohn Marino 	   tracepoints, so don't take lack of support as a reason to
10435cf7f2e2dSJohn Marino 	   give up on the trace run.  */
10436c50c785cSJohn Marino 	warning (_("Target does not support fast tracepoints, "
10437a45ae5f8SJohn Marino 		   "downloading %d as regular tracepoint"), b->number);
10438cf7f2e2dSJohn Marino     }
10439a45ae5f8SJohn Marino   else if (b->type == bp_static_tracepoint)
10440cf7f2e2dSJohn Marino     {
10441cf7f2e2dSJohn Marino       /* Only test for support at download time; we may not know
10442cf7f2e2dSJohn Marino 	 target capabilities at definition time.  */
10443cf7f2e2dSJohn Marino       if (remote_supports_static_tracepoints ())
10444cf7f2e2dSJohn Marino 	{
10445cf7f2e2dSJohn Marino 	  struct static_tracepoint_marker marker;
10446cf7f2e2dSJohn Marino 
10447cf7f2e2dSJohn Marino 	  if (target_static_tracepoint_marker_at (tpaddr, &marker))
10448cf7f2e2dSJohn Marino 	    strcat (buf, ":S");
10449cf7f2e2dSJohn Marino 	  else
10450c50c785cSJohn Marino 	    error (_("Static tracepoint not valid during download"));
10451cf7f2e2dSJohn Marino 	}
10452cf7f2e2dSJohn Marino       else
10453cf7f2e2dSJohn Marino 	/* Fast tracepoints are functionally identical to regular
10454cf7f2e2dSJohn Marino 	   tracepoints, so don't take lack of support as a reason
10455cf7f2e2dSJohn Marino 	   to give up on the trace run.  */
10456cf7f2e2dSJohn Marino 	error (_("Target does not support static tracepoints"));
10457cf7f2e2dSJohn Marino     }
10458cf7f2e2dSJohn Marino   /* If the tracepoint has a conditional, make it into an agent
10459cf7f2e2dSJohn Marino      expression and append to the definition.  */
10460cf7f2e2dSJohn Marino   if (loc->cond)
10461cf7f2e2dSJohn Marino     {
10462cf7f2e2dSJohn Marino       /* Only test support at download time, we may not know target
10463cf7f2e2dSJohn Marino 	 capabilities at definition time.  */
10464cf7f2e2dSJohn Marino       if (remote_supports_cond_tracepoints ())
10465cf7f2e2dSJohn Marino 	{
10466cf7f2e2dSJohn Marino 	  aexpr = gen_eval_for_expr (tpaddr, loc->cond);
10467cf7f2e2dSJohn Marino 	  aexpr_chain = make_cleanup_free_agent_expr (aexpr);
10468ef5ccd6cSJohn Marino 	  xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
10469ef5ccd6cSJohn Marino 		     aexpr->len);
10470cf7f2e2dSJohn Marino 	  pkt = buf + strlen (buf);
10471cf7f2e2dSJohn Marino 	  for (ndx = 0; ndx < aexpr->len; ++ndx)
10472cf7f2e2dSJohn Marino 	    pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
10473cf7f2e2dSJohn Marino 	  *pkt = '\0';
10474cf7f2e2dSJohn Marino 	  do_cleanups (aexpr_chain);
10475cf7f2e2dSJohn Marino 	}
10476cf7f2e2dSJohn Marino       else
10477c50c785cSJohn Marino 	warning (_("Target does not support conditional tracepoints, "
10478a45ae5f8SJohn Marino 		   "ignoring tp %d cond"), b->number);
10479cf7f2e2dSJohn Marino     }
10480cf7f2e2dSJohn Marino 
10481a45ae5f8SJohn Marino   if (b->commands || *default_collect)
10482cf7f2e2dSJohn Marino     strcat (buf, "-");
10483cf7f2e2dSJohn Marino   putpkt (buf);
10484cf7f2e2dSJohn Marino   remote_get_noisy_reply (&target_buf, &target_buf_size);
10485cf7f2e2dSJohn Marino   if (strcmp (target_buf, "OK"))
10486cf7f2e2dSJohn Marino     error (_("Target does not support tracepoints."));
10487cf7f2e2dSJohn Marino 
10488cf7f2e2dSJohn Marino   /* do_single_steps (t); */
10489cf7f2e2dSJohn Marino   if (tdp_actions)
10490cf7f2e2dSJohn Marino     {
10491cf7f2e2dSJohn Marino       for (ndx = 0; tdp_actions[ndx]; ndx++)
10492cf7f2e2dSJohn Marino 	{
10493c50c785cSJohn Marino 	  QUIT;	/* Allow user to bail out with ^C.  */
10494ef5ccd6cSJohn Marino 	  xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
10495a45ae5f8SJohn Marino 		     b->number, addrbuf, /* address */
10496cf7f2e2dSJohn Marino 		     tdp_actions[ndx],
10497cf7f2e2dSJohn Marino 		     ((tdp_actions[ndx + 1] || stepping_actions)
10498cf7f2e2dSJohn Marino 		      ? '-' : 0));
10499cf7f2e2dSJohn Marino 	  putpkt (buf);
10500cf7f2e2dSJohn Marino 	  remote_get_noisy_reply (&target_buf,
10501cf7f2e2dSJohn Marino 				  &target_buf_size);
10502cf7f2e2dSJohn Marino 	  if (strcmp (target_buf, "OK"))
10503cf7f2e2dSJohn Marino 	    error (_("Error on target while setting tracepoints."));
10504cf7f2e2dSJohn Marino 	}
10505cf7f2e2dSJohn Marino     }
10506cf7f2e2dSJohn Marino   if (stepping_actions)
10507cf7f2e2dSJohn Marino     {
10508cf7f2e2dSJohn Marino       for (ndx = 0; stepping_actions[ndx]; ndx++)
10509cf7f2e2dSJohn Marino 	{
10510c50c785cSJohn Marino 	  QUIT;	/* Allow user to bail out with ^C.  */
10511ef5ccd6cSJohn Marino 	  xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
10512a45ae5f8SJohn Marino 		     b->number, addrbuf, /* address */
10513cf7f2e2dSJohn Marino 		     ((ndx == 0) ? "S" : ""),
10514cf7f2e2dSJohn Marino 		     stepping_actions[ndx],
10515cf7f2e2dSJohn Marino 		     (stepping_actions[ndx + 1] ? "-" : ""));
10516cf7f2e2dSJohn Marino 	  putpkt (buf);
10517cf7f2e2dSJohn Marino 	  remote_get_noisy_reply (&target_buf,
10518cf7f2e2dSJohn Marino 				  &target_buf_size);
10519cf7f2e2dSJohn Marino 	  if (strcmp (target_buf, "OK"))
10520cf7f2e2dSJohn Marino 	    error (_("Error on target while setting tracepoints."));
10521cf7f2e2dSJohn Marino 	}
10522cf7f2e2dSJohn Marino     }
10523cf7f2e2dSJohn Marino 
10524c50c785cSJohn Marino   if (remote_protocol_packets[PACKET_TracepointSource].support
10525c50c785cSJohn Marino       == PACKET_ENABLE)
10526cf7f2e2dSJohn Marino     {
10527a45ae5f8SJohn Marino       if (b->addr_string)
10528cf7f2e2dSJohn Marino 	{
10529cf7f2e2dSJohn Marino 	  strcpy (buf, "QTDPsrc:");
10530a45ae5f8SJohn Marino 	  encode_source_string (b->number, loc->address,
10531a45ae5f8SJohn Marino 				"at", b->addr_string, buf + strlen (buf),
10532cf7f2e2dSJohn Marino 				2048 - strlen (buf));
10533cf7f2e2dSJohn Marino 
10534cf7f2e2dSJohn Marino 	  putpkt (buf);
10535cf7f2e2dSJohn Marino 	  remote_get_noisy_reply (&target_buf, &target_buf_size);
10536cf7f2e2dSJohn Marino 	  if (strcmp (target_buf, "OK"))
10537cf7f2e2dSJohn Marino 	    warning (_("Target does not support source download."));
10538cf7f2e2dSJohn Marino 	}
10539a45ae5f8SJohn Marino       if (b->cond_string)
10540cf7f2e2dSJohn Marino 	{
10541cf7f2e2dSJohn Marino 	  strcpy (buf, "QTDPsrc:");
10542a45ae5f8SJohn Marino 	  encode_source_string (b->number, loc->address,
10543a45ae5f8SJohn Marino 				"cond", b->cond_string, buf + strlen (buf),
10544cf7f2e2dSJohn Marino 				2048 - strlen (buf));
10545cf7f2e2dSJohn Marino 	  putpkt (buf);
10546cf7f2e2dSJohn Marino 	  remote_get_noisy_reply (&target_buf, &target_buf_size);
10547cf7f2e2dSJohn Marino 	  if (strcmp (target_buf, "OK"))
10548cf7f2e2dSJohn Marino 	    warning (_("Target does not support source download."));
10549cf7f2e2dSJohn Marino 	}
10550a45ae5f8SJohn Marino       remote_download_command_source (b->number, loc->address,
10551a45ae5f8SJohn Marino 				      breakpoint_commands (b));
10552cf7f2e2dSJohn Marino     }
10553cf7f2e2dSJohn Marino 
10554cf7f2e2dSJohn Marino   do_cleanups (old_chain);
10555cf7f2e2dSJohn Marino }
10556a45ae5f8SJohn Marino 
10557a45ae5f8SJohn Marino static int
remote_can_download_tracepoint(void)10558a45ae5f8SJohn Marino remote_can_download_tracepoint (void)
10559a45ae5f8SJohn Marino {
10560a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
10561a45ae5f8SJohn Marino   struct trace_status *ts;
10562a45ae5f8SJohn Marino   int status;
10563a45ae5f8SJohn Marino 
10564a45ae5f8SJohn Marino   /* Don't try to install tracepoints until we've relocated our
10565a45ae5f8SJohn Marino      symbols, and fetched and merged the target's tracepoint list with
10566a45ae5f8SJohn Marino      ours.  */
10567a45ae5f8SJohn Marino   if (rs->starting_up)
10568a45ae5f8SJohn Marino     return 0;
10569a45ae5f8SJohn Marino 
10570a45ae5f8SJohn Marino   ts = current_trace_status ();
10571a45ae5f8SJohn Marino   status = remote_get_trace_status (ts);
10572a45ae5f8SJohn Marino 
10573a45ae5f8SJohn Marino   if (status == -1 || !ts->running_known || !ts->running)
10574a45ae5f8SJohn Marino     return 0;
10575a45ae5f8SJohn Marino 
10576a45ae5f8SJohn Marino   /* If we are in a tracing experiment, but remote stub doesn't support
10577a45ae5f8SJohn Marino      installing tracepoint in trace, we have to return.  */
10578a45ae5f8SJohn Marino   if (!remote_supports_install_in_trace ())
10579a45ae5f8SJohn Marino     return 0;
10580a45ae5f8SJohn Marino 
10581a45ae5f8SJohn Marino   return 1;
10582cf7f2e2dSJohn Marino }
10583cf7f2e2dSJohn Marino 
10584a45ae5f8SJohn Marino 
10585cf7f2e2dSJohn Marino static void
remote_download_trace_state_variable(struct trace_state_variable * tsv)10586cf7f2e2dSJohn Marino remote_download_trace_state_variable (struct trace_state_variable *tsv)
10587cf7f2e2dSJohn Marino {
10588cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
10589cf7f2e2dSJohn Marino   char *p;
10590cf7f2e2dSJohn Marino 
10591ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
10592ef5ccd6cSJohn Marino 	     tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
10593ef5ccd6cSJohn Marino 	     tsv->builtin);
10594cf7f2e2dSJohn Marino   p = rs->buf + strlen (rs->buf);
10595cf7f2e2dSJohn Marino   if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
10596cf7f2e2dSJohn Marino     error (_("Trace state variable name too long for tsv definition packet"));
10597cf7f2e2dSJohn Marino   p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, 0);
10598cf7f2e2dSJohn Marino   *p++ = '\0';
10599cf7f2e2dSJohn Marino   putpkt (rs->buf);
10600cf7f2e2dSJohn Marino   remote_get_noisy_reply (&target_buf, &target_buf_size);
10601cf7f2e2dSJohn Marino   if (*target_buf == '\0')
10602cf7f2e2dSJohn Marino     error (_("Target does not support this command."));
10603cf7f2e2dSJohn Marino   if (strcmp (target_buf, "OK") != 0)
10604cf7f2e2dSJohn Marino     error (_("Error on target while downloading trace state variable."));
10605cf7f2e2dSJohn Marino }
10606cf7f2e2dSJohn Marino 
10607cf7f2e2dSJohn Marino static void
remote_enable_tracepoint(struct bp_location * location)10608a45ae5f8SJohn Marino remote_enable_tracepoint (struct bp_location *location)
10609a45ae5f8SJohn Marino {
10610a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
10611a45ae5f8SJohn Marino   char addr_buf[40];
10612a45ae5f8SJohn Marino 
10613a45ae5f8SJohn Marino   sprintf_vma (addr_buf, location->address);
10614ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
10615ef5ccd6cSJohn Marino 	     location->owner->number, addr_buf);
10616a45ae5f8SJohn Marino   putpkt (rs->buf);
10617a45ae5f8SJohn Marino   remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10618a45ae5f8SJohn Marino   if (*rs->buf == '\0')
10619a45ae5f8SJohn Marino     error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
10620a45ae5f8SJohn Marino   if (strcmp (rs->buf, "OK") != 0)
10621a45ae5f8SJohn Marino     error (_("Error on target while enabling tracepoint."));
10622a45ae5f8SJohn Marino }
10623a45ae5f8SJohn Marino 
10624a45ae5f8SJohn Marino static void
remote_disable_tracepoint(struct bp_location * location)10625a45ae5f8SJohn Marino remote_disable_tracepoint (struct bp_location *location)
10626a45ae5f8SJohn Marino {
10627a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
10628a45ae5f8SJohn Marino   char addr_buf[40];
10629a45ae5f8SJohn Marino 
10630a45ae5f8SJohn Marino   sprintf_vma (addr_buf, location->address);
10631ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
10632ef5ccd6cSJohn Marino 	     location->owner->number, addr_buf);
10633a45ae5f8SJohn Marino   putpkt (rs->buf);
10634a45ae5f8SJohn Marino   remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10635a45ae5f8SJohn Marino   if (*rs->buf == '\0')
10636a45ae5f8SJohn Marino     error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
10637a45ae5f8SJohn Marino   if (strcmp (rs->buf, "OK") != 0)
10638a45ae5f8SJohn Marino     error (_("Error on target while disabling tracepoint."));
10639a45ae5f8SJohn Marino }
10640a45ae5f8SJohn Marino 
10641a45ae5f8SJohn Marino static void
remote_trace_set_readonly_regions(void)10642cf7f2e2dSJohn Marino remote_trace_set_readonly_regions (void)
10643cf7f2e2dSJohn Marino {
10644cf7f2e2dSJohn Marino   asection *s;
10645ef5ccd6cSJohn Marino   bfd *abfd = NULL;
10646cf7f2e2dSJohn Marino   bfd_size_type size;
10647c50c785cSJohn Marino   bfd_vma vma;
10648cf7f2e2dSJohn Marino   int anysecs = 0;
10649c50c785cSJohn Marino   int offset = 0;
10650cf7f2e2dSJohn Marino 
10651cf7f2e2dSJohn Marino   if (!exec_bfd)
10652cf7f2e2dSJohn Marino     return;			/* No information to give.  */
10653cf7f2e2dSJohn Marino 
10654cf7f2e2dSJohn Marino   strcpy (target_buf, "QTro");
10655ef5ccd6cSJohn Marino   offset = strlen (target_buf);
10656cf7f2e2dSJohn Marino   for (s = exec_bfd->sections; s; s = s->next)
10657cf7f2e2dSJohn Marino     {
10658cf7f2e2dSJohn Marino       char tmp1[40], tmp2[40];
10659c50c785cSJohn Marino       int sec_length;
10660cf7f2e2dSJohn Marino 
10661cf7f2e2dSJohn Marino       if ((s->flags & SEC_LOAD) == 0 ||
10662cf7f2e2dSJohn Marino       /*  (s->flags & SEC_CODE) == 0 || */
10663cf7f2e2dSJohn Marino 	  (s->flags & SEC_READONLY) == 0)
10664cf7f2e2dSJohn Marino 	continue;
10665cf7f2e2dSJohn Marino 
10666cf7f2e2dSJohn Marino       anysecs = 1;
10667ef5ccd6cSJohn Marino       vma = bfd_get_section_vma (abfd, s);
10668cf7f2e2dSJohn Marino       size = bfd_get_section_size (s);
10669c50c785cSJohn Marino       sprintf_vma (tmp1, vma);
10670c50c785cSJohn Marino       sprintf_vma (tmp2, vma + size);
10671c50c785cSJohn Marino       sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
10672c50c785cSJohn Marino       if (offset + sec_length + 1 > target_buf_size)
10673c50c785cSJohn Marino 	{
10674c50c785cSJohn Marino 	  if (remote_protocol_packets[PACKET_qXfer_traceframe_info].support
10675c50c785cSJohn Marino 	      != PACKET_ENABLE)
10676c50c785cSJohn Marino 	    warning (_("\
10677c50c785cSJohn Marino Too many sections for read-only sections definition packet."));
10678c50c785cSJohn Marino 	  break;
10679c50c785cSJohn Marino 	}
10680ef5ccd6cSJohn Marino       xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
10681ef5ccd6cSJohn Marino 		 tmp1, tmp2);
10682c50c785cSJohn Marino       offset += sec_length;
10683cf7f2e2dSJohn Marino     }
10684cf7f2e2dSJohn Marino   if (anysecs)
10685cf7f2e2dSJohn Marino     {
10686cf7f2e2dSJohn Marino       putpkt (target_buf);
10687cf7f2e2dSJohn Marino       getpkt (&target_buf, &target_buf_size, 0);
10688cf7f2e2dSJohn Marino     }
10689cf7f2e2dSJohn Marino }
10690cf7f2e2dSJohn Marino 
10691cf7f2e2dSJohn Marino static void
remote_trace_start(void)10692cf7f2e2dSJohn Marino remote_trace_start (void)
10693cf7f2e2dSJohn Marino {
10694cf7f2e2dSJohn Marino   putpkt ("QTStart");
10695cf7f2e2dSJohn Marino   remote_get_noisy_reply (&target_buf, &target_buf_size);
10696cf7f2e2dSJohn Marino   if (*target_buf == '\0')
10697cf7f2e2dSJohn Marino     error (_("Target does not support this command."));
10698cf7f2e2dSJohn Marino   if (strcmp (target_buf, "OK") != 0)
10699cf7f2e2dSJohn Marino     error (_("Bogus reply from target: %s"), target_buf);
10700cf7f2e2dSJohn Marino }
10701cf7f2e2dSJohn Marino 
10702cf7f2e2dSJohn Marino static int
remote_get_trace_status(struct trace_status * ts)10703cf7f2e2dSJohn Marino remote_get_trace_status (struct trace_status *ts)
10704cf7f2e2dSJohn Marino {
10705c50c785cSJohn Marino   /* Initialize it just to avoid a GCC false warning.  */
10706c50c785cSJohn Marino   char *p = NULL;
10707c50c785cSJohn Marino   /* FIXME we need to get register block size some other way.  */
10708cf7f2e2dSJohn Marino   extern int trace_regblock_size;
10709c50c785cSJohn Marino   volatile struct gdb_exception ex;
10710cf7f2e2dSJohn Marino 
10711cf7f2e2dSJohn Marino   trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
10712cf7f2e2dSJohn Marino 
10713cf7f2e2dSJohn Marino   putpkt ("qTStatus");
10714c50c785cSJohn Marino 
10715c50c785cSJohn Marino   TRY_CATCH (ex, RETURN_MASK_ERROR)
10716c50c785cSJohn Marino     {
10717cf7f2e2dSJohn Marino       p = remote_get_noisy_reply (&target_buf, &target_buf_size);
10718c50c785cSJohn Marino     }
10719c50c785cSJohn Marino   if (ex.reason < 0)
10720c50c785cSJohn Marino     {
10721ef5ccd6cSJohn Marino       if (ex.error != TARGET_CLOSE_ERROR)
10722ef5ccd6cSJohn Marino 	{
10723c50c785cSJohn Marino 	  exception_fprintf (gdb_stderr, ex, "qTStatus: ");
10724c50c785cSJohn Marino 	  return -1;
10725c50c785cSJohn Marino 	}
10726ef5ccd6cSJohn Marino       throw_exception (ex);
10727ef5ccd6cSJohn Marino     }
10728cf7f2e2dSJohn Marino 
10729cf7f2e2dSJohn Marino   /* If the remote target doesn't do tracing, flag it.  */
10730cf7f2e2dSJohn Marino   if (*p == '\0')
10731cf7f2e2dSJohn Marino     return -1;
10732cf7f2e2dSJohn Marino 
10733cf7f2e2dSJohn Marino   /* We're working with a live target.  */
10734ef5ccd6cSJohn Marino   ts->filename = NULL;
10735cf7f2e2dSJohn Marino 
10736cf7f2e2dSJohn Marino   if (*p++ != 'T')
10737cf7f2e2dSJohn Marino     error (_("Bogus trace status reply from target: %s"), target_buf);
10738cf7f2e2dSJohn Marino 
10739ef5ccd6cSJohn Marino   /* Function 'parse_trace_status' sets default value of each field of
10740ef5ccd6cSJohn Marino      'ts' at first, so we don't have to do it here.  */
10741cf7f2e2dSJohn Marino   parse_trace_status (p, ts);
10742cf7f2e2dSJohn Marino 
10743cf7f2e2dSJohn Marino   return ts->running;
10744cf7f2e2dSJohn Marino }
10745cf7f2e2dSJohn Marino 
10746ef5ccd6cSJohn Marino static void
remote_get_tracepoint_status(struct breakpoint * bp,struct uploaded_tp * utp)10747a45ae5f8SJohn Marino remote_get_tracepoint_status (struct breakpoint *bp,
10748a45ae5f8SJohn Marino 			      struct uploaded_tp *utp)
10749a45ae5f8SJohn Marino {
10750a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
10751a45ae5f8SJohn Marino   char *reply;
10752a45ae5f8SJohn Marino   struct bp_location *loc;
10753a45ae5f8SJohn Marino   struct tracepoint *tp = (struct tracepoint *) bp;
10754ef5ccd6cSJohn Marino   size_t size = get_remote_packet_size ();
10755a45ae5f8SJohn Marino 
10756a45ae5f8SJohn Marino   if (tp)
10757a45ae5f8SJohn Marino     {
10758a45ae5f8SJohn Marino       tp->base.hit_count = 0;
10759a45ae5f8SJohn Marino       tp->traceframe_usage = 0;
10760a45ae5f8SJohn Marino       for (loc = tp->base.loc; loc; loc = loc->next)
10761a45ae5f8SJohn Marino 	{
10762a45ae5f8SJohn Marino 	  /* If the tracepoint was never downloaded, don't go asking for
10763a45ae5f8SJohn Marino 	     any status.  */
10764a45ae5f8SJohn Marino 	  if (tp->number_on_target == 0)
10765a45ae5f8SJohn Marino 	    continue;
10766ef5ccd6cSJohn Marino 	  xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
10767a45ae5f8SJohn Marino 		     phex_nz (loc->address, 0));
10768a45ae5f8SJohn Marino 	  putpkt (rs->buf);
10769a45ae5f8SJohn Marino 	  reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10770a45ae5f8SJohn Marino 	  if (reply && *reply)
10771a45ae5f8SJohn Marino 	    {
10772a45ae5f8SJohn Marino 	      if (*reply == 'V')
10773a45ae5f8SJohn Marino 		parse_tracepoint_status (reply + 1, bp, utp);
10774a45ae5f8SJohn Marino 	    }
10775a45ae5f8SJohn Marino 	}
10776a45ae5f8SJohn Marino     }
10777a45ae5f8SJohn Marino   else if (utp)
10778a45ae5f8SJohn Marino     {
10779a45ae5f8SJohn Marino       utp->hit_count = 0;
10780a45ae5f8SJohn Marino       utp->traceframe_usage = 0;
10781ef5ccd6cSJohn Marino       xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
10782ef5ccd6cSJohn Marino 		 phex_nz (utp->addr, 0));
10783a45ae5f8SJohn Marino       putpkt (rs->buf);
10784a45ae5f8SJohn Marino       reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10785a45ae5f8SJohn Marino       if (reply && *reply)
10786a45ae5f8SJohn Marino 	{
10787a45ae5f8SJohn Marino 	  if (*reply == 'V')
10788a45ae5f8SJohn Marino 	    parse_tracepoint_status (reply + 1, bp, utp);
10789a45ae5f8SJohn Marino 	}
10790a45ae5f8SJohn Marino     }
10791a45ae5f8SJohn Marino }
10792a45ae5f8SJohn Marino 
10793cf7f2e2dSJohn Marino static void
remote_trace_stop(void)10794cf7f2e2dSJohn Marino remote_trace_stop (void)
10795cf7f2e2dSJohn Marino {
10796cf7f2e2dSJohn Marino   putpkt ("QTStop");
10797cf7f2e2dSJohn Marino   remote_get_noisy_reply (&target_buf, &target_buf_size);
10798cf7f2e2dSJohn Marino   if (*target_buf == '\0')
10799cf7f2e2dSJohn Marino     error (_("Target does not support this command."));
10800cf7f2e2dSJohn Marino   if (strcmp (target_buf, "OK") != 0)
10801cf7f2e2dSJohn Marino     error (_("Bogus reply from target: %s"), target_buf);
10802cf7f2e2dSJohn Marino }
10803cf7f2e2dSJohn Marino 
10804cf7f2e2dSJohn Marino static int
remote_trace_find(enum trace_find_type type,int num,ULONGEST addr1,ULONGEST addr2,int * tpp)10805cf7f2e2dSJohn Marino remote_trace_find (enum trace_find_type type, int num,
10806cf7f2e2dSJohn Marino 		   ULONGEST addr1, ULONGEST addr2,
10807cf7f2e2dSJohn Marino 		   int *tpp)
10808cf7f2e2dSJohn Marino {
10809cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
10810ef5ccd6cSJohn Marino   char *endbuf = rs->buf + get_remote_packet_size ();
10811cf7f2e2dSJohn Marino   char *p, *reply;
10812cf7f2e2dSJohn Marino   int target_frameno = -1, target_tracept = -1;
10813cf7f2e2dSJohn Marino 
10814c50c785cSJohn Marino   /* Lookups other than by absolute frame number depend on the current
10815c50c785cSJohn Marino      trace selected, so make sure it is correct on the remote end
10816c50c785cSJohn Marino      first.  */
10817c50c785cSJohn Marino   if (type != tfind_number)
10818c50c785cSJohn Marino     set_remote_traceframe ();
10819c50c785cSJohn Marino 
10820cf7f2e2dSJohn Marino   p = rs->buf;
10821cf7f2e2dSJohn Marino   strcpy (p, "QTFrame:");
10822cf7f2e2dSJohn Marino   p = strchr (p, '\0');
10823cf7f2e2dSJohn Marino   switch (type)
10824cf7f2e2dSJohn Marino     {
10825cf7f2e2dSJohn Marino     case tfind_number:
10826ef5ccd6cSJohn Marino       xsnprintf (p, endbuf - p, "%x", num);
10827cf7f2e2dSJohn Marino       break;
10828cf7f2e2dSJohn Marino     case tfind_pc:
10829ef5ccd6cSJohn Marino       xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
10830cf7f2e2dSJohn Marino       break;
10831cf7f2e2dSJohn Marino     case tfind_tp:
10832ef5ccd6cSJohn Marino       xsnprintf (p, endbuf - p, "tdp:%x", num);
10833cf7f2e2dSJohn Marino       break;
10834cf7f2e2dSJohn Marino     case tfind_range:
10835ef5ccd6cSJohn Marino       xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
10836ef5ccd6cSJohn Marino 		 phex_nz (addr2, 0));
10837cf7f2e2dSJohn Marino       break;
10838cf7f2e2dSJohn Marino     case tfind_outside:
10839ef5ccd6cSJohn Marino       xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
10840ef5ccd6cSJohn Marino 		 phex_nz (addr2, 0));
10841cf7f2e2dSJohn Marino       break;
10842cf7f2e2dSJohn Marino     default:
10843c50c785cSJohn Marino       error (_("Unknown trace find type %d"), type);
10844cf7f2e2dSJohn Marino     }
10845cf7f2e2dSJohn Marino 
10846cf7f2e2dSJohn Marino   putpkt (rs->buf);
10847cf7f2e2dSJohn Marino   reply = remote_get_noisy_reply (&(rs->buf), &sizeof_pkt);
10848cf7f2e2dSJohn Marino   if (*reply == '\0')
10849cf7f2e2dSJohn Marino     error (_("Target does not support this command."));
10850cf7f2e2dSJohn Marino 
10851cf7f2e2dSJohn Marino   while (reply && *reply)
10852cf7f2e2dSJohn Marino     switch (*reply)
10853cf7f2e2dSJohn Marino       {
10854cf7f2e2dSJohn Marino       case 'F':
10855cf7f2e2dSJohn Marino 	p = ++reply;
10856cf7f2e2dSJohn Marino 	target_frameno = (int) strtol (p, &reply, 16);
10857cf7f2e2dSJohn Marino 	if (reply == p)
10858cf7f2e2dSJohn Marino 	  error (_("Unable to parse trace frame number"));
10859c50c785cSJohn Marino 	/* Don't update our remote traceframe number cache on failure
10860c50c785cSJohn Marino 	   to select a remote traceframe.  */
10861cf7f2e2dSJohn Marino 	if (target_frameno == -1)
10862cf7f2e2dSJohn Marino 	  return -1;
10863cf7f2e2dSJohn Marino 	break;
10864cf7f2e2dSJohn Marino       case 'T':
10865cf7f2e2dSJohn Marino 	p = ++reply;
10866cf7f2e2dSJohn Marino 	target_tracept = (int) strtol (p, &reply, 16);
10867cf7f2e2dSJohn Marino 	if (reply == p)
10868cf7f2e2dSJohn Marino 	  error (_("Unable to parse tracepoint number"));
10869cf7f2e2dSJohn Marino 	break;
10870cf7f2e2dSJohn Marino       case 'O':		/* "OK"? */
10871cf7f2e2dSJohn Marino 	if (reply[1] == 'K' && reply[2] == '\0')
10872cf7f2e2dSJohn Marino 	  reply += 2;
10873cf7f2e2dSJohn Marino 	else
10874cf7f2e2dSJohn Marino 	  error (_("Bogus reply from target: %s"), reply);
10875cf7f2e2dSJohn Marino 	break;
10876cf7f2e2dSJohn Marino       default:
10877cf7f2e2dSJohn Marino 	error (_("Bogus reply from target: %s"), reply);
10878cf7f2e2dSJohn Marino       }
10879cf7f2e2dSJohn Marino   if (tpp)
10880cf7f2e2dSJohn Marino     *tpp = target_tracept;
10881c50c785cSJohn Marino 
10882c50c785cSJohn Marino   remote_traceframe_number = target_frameno;
10883cf7f2e2dSJohn Marino   return target_frameno;
10884cf7f2e2dSJohn Marino }
10885cf7f2e2dSJohn Marino 
10886cf7f2e2dSJohn Marino static int
remote_get_trace_state_variable_value(int tsvnum,LONGEST * val)10887cf7f2e2dSJohn Marino remote_get_trace_state_variable_value (int tsvnum, LONGEST *val)
10888cf7f2e2dSJohn Marino {
10889cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
10890cf7f2e2dSJohn Marino   char *reply;
10891cf7f2e2dSJohn Marino   ULONGEST uval;
10892cf7f2e2dSJohn Marino 
10893c50c785cSJohn Marino   set_remote_traceframe ();
10894c50c785cSJohn Marino 
10895ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
10896cf7f2e2dSJohn Marino   putpkt (rs->buf);
10897cf7f2e2dSJohn Marino   reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10898cf7f2e2dSJohn Marino   if (reply && *reply)
10899cf7f2e2dSJohn Marino     {
10900cf7f2e2dSJohn Marino       if (*reply == 'V')
10901cf7f2e2dSJohn Marino 	{
10902cf7f2e2dSJohn Marino 	  unpack_varlen_hex (reply + 1, &uval);
10903cf7f2e2dSJohn Marino 	  *val = (LONGEST) uval;
10904cf7f2e2dSJohn Marino 	  return 1;
10905cf7f2e2dSJohn Marino 	}
10906cf7f2e2dSJohn Marino     }
10907cf7f2e2dSJohn Marino   return 0;
10908cf7f2e2dSJohn Marino }
10909cf7f2e2dSJohn Marino 
10910cf7f2e2dSJohn Marino static int
remote_save_trace_data(const char * filename)10911cf7f2e2dSJohn Marino remote_save_trace_data (const char *filename)
10912cf7f2e2dSJohn Marino {
10913cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
10914cf7f2e2dSJohn Marino   char *p, *reply;
10915cf7f2e2dSJohn Marino 
10916cf7f2e2dSJohn Marino   p = rs->buf;
10917cf7f2e2dSJohn Marino   strcpy (p, "QTSave:");
10918cf7f2e2dSJohn Marino   p += strlen (p);
10919cf7f2e2dSJohn Marino   if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
10920cf7f2e2dSJohn Marino     error (_("Remote file name too long for trace save packet"));
10921cf7f2e2dSJohn Marino   p += 2 * bin2hex ((gdb_byte *) filename, p, 0);
10922cf7f2e2dSJohn Marino   *p++ = '\0';
10923cf7f2e2dSJohn Marino   putpkt (rs->buf);
10924cf7f2e2dSJohn Marino   reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10925a45ae5f8SJohn Marino   if (*reply == '\0')
10926cf7f2e2dSJohn Marino     error (_("Target does not support this command."));
10927cf7f2e2dSJohn Marino   if (strcmp (reply, "OK") != 0)
10928cf7f2e2dSJohn Marino     error (_("Bogus reply from target: %s"), reply);
10929cf7f2e2dSJohn Marino   return 0;
10930cf7f2e2dSJohn Marino }
10931cf7f2e2dSJohn Marino 
10932cf7f2e2dSJohn Marino /* This is basically a memory transfer, but needs to be its own packet
10933cf7f2e2dSJohn Marino    because we don't know how the target actually organizes its trace
10934cf7f2e2dSJohn Marino    memory, plus we want to be able to ask for as much as possible, but
10935cf7f2e2dSJohn Marino    not be unhappy if we don't get as much as we ask for.  */
10936cf7f2e2dSJohn Marino 
10937cf7f2e2dSJohn Marino static LONGEST
remote_get_raw_trace_data(gdb_byte * buf,ULONGEST offset,LONGEST len)10938cf7f2e2dSJohn Marino remote_get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len)
10939cf7f2e2dSJohn Marino {
10940cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
10941cf7f2e2dSJohn Marino   char *reply;
10942cf7f2e2dSJohn Marino   char *p;
10943cf7f2e2dSJohn Marino   int rslt;
10944cf7f2e2dSJohn Marino 
10945cf7f2e2dSJohn Marino   p = rs->buf;
10946cf7f2e2dSJohn Marino   strcpy (p, "qTBuffer:");
10947cf7f2e2dSJohn Marino   p += strlen (p);
10948cf7f2e2dSJohn Marino   p += hexnumstr (p, offset);
10949cf7f2e2dSJohn Marino   *p++ = ',';
10950cf7f2e2dSJohn Marino   p += hexnumstr (p, len);
10951cf7f2e2dSJohn Marino   *p++ = '\0';
10952cf7f2e2dSJohn Marino 
10953cf7f2e2dSJohn Marino   putpkt (rs->buf);
10954cf7f2e2dSJohn Marino   reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10955cf7f2e2dSJohn Marino   if (reply && *reply)
10956cf7f2e2dSJohn Marino     {
10957cf7f2e2dSJohn Marino       /* 'l' by itself means we're at the end of the buffer and
10958cf7f2e2dSJohn Marino 	 there is nothing more to get.  */
10959cf7f2e2dSJohn Marino       if (*reply == 'l')
10960cf7f2e2dSJohn Marino 	return 0;
10961cf7f2e2dSJohn Marino 
10962cf7f2e2dSJohn Marino       /* Convert the reply into binary.  Limit the number of bytes to
10963cf7f2e2dSJohn Marino 	 convert according to our passed-in buffer size, rather than
10964cf7f2e2dSJohn Marino 	 what was returned in the packet; if the target is
10965cf7f2e2dSJohn Marino 	 unexpectedly generous and gives us a bigger reply than we
10966cf7f2e2dSJohn Marino 	 asked for, we don't want to crash.  */
10967cf7f2e2dSJohn Marino       rslt = hex2bin (target_buf, buf, len);
10968cf7f2e2dSJohn Marino       return rslt;
10969cf7f2e2dSJohn Marino     }
10970cf7f2e2dSJohn Marino 
10971cf7f2e2dSJohn Marino   /* Something went wrong, flag as an error.  */
10972cf7f2e2dSJohn Marino   return -1;
10973cf7f2e2dSJohn Marino }
10974cf7f2e2dSJohn Marino 
10975cf7f2e2dSJohn Marino static void
remote_set_disconnected_tracing(int val)10976cf7f2e2dSJohn Marino remote_set_disconnected_tracing (int val)
10977cf7f2e2dSJohn Marino {
10978cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
10979cf7f2e2dSJohn Marino 
10980cf7f2e2dSJohn Marino   if (rs->disconnected_tracing)
10981cf7f2e2dSJohn Marino     {
10982cf7f2e2dSJohn Marino       char *reply;
10983cf7f2e2dSJohn Marino 
10984ef5ccd6cSJohn Marino       xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
10985cf7f2e2dSJohn Marino       putpkt (rs->buf);
10986cf7f2e2dSJohn Marino       reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10987cf7f2e2dSJohn Marino       if (*reply == '\0')
10988cf7f2e2dSJohn Marino 	error (_("Target does not support this command."));
10989cf7f2e2dSJohn Marino       if (strcmp (reply, "OK") != 0)
10990cf7f2e2dSJohn Marino         error (_("Bogus reply from target: %s"), reply);
10991cf7f2e2dSJohn Marino     }
10992cf7f2e2dSJohn Marino   else if (val)
10993cf7f2e2dSJohn Marino     warning (_("Target does not support disconnected tracing."));
10994cf7f2e2dSJohn Marino }
10995cf7f2e2dSJohn Marino 
10996cf7f2e2dSJohn Marino static int
remote_core_of_thread(struct target_ops * ops,ptid_t ptid)10997cf7f2e2dSJohn Marino remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
10998cf7f2e2dSJohn Marino {
10999cf7f2e2dSJohn Marino   struct thread_info *info = find_thread_ptid (ptid);
11000cf7f2e2dSJohn Marino 
11001cf7f2e2dSJohn Marino   if (info && info->private)
11002cf7f2e2dSJohn Marino     return info->private->core;
11003cf7f2e2dSJohn Marino   return -1;
11004cf7f2e2dSJohn Marino }
11005cf7f2e2dSJohn Marino 
11006cf7f2e2dSJohn Marino static void
remote_set_circular_trace_buffer(int val)11007cf7f2e2dSJohn Marino remote_set_circular_trace_buffer (int val)
11008cf7f2e2dSJohn Marino {
11009cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
11010cf7f2e2dSJohn Marino   char *reply;
11011cf7f2e2dSJohn Marino 
11012ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
11013cf7f2e2dSJohn Marino   putpkt (rs->buf);
11014cf7f2e2dSJohn Marino   reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11015cf7f2e2dSJohn Marino   if (*reply == '\0')
11016cf7f2e2dSJohn Marino     error (_("Target does not support this command."));
11017cf7f2e2dSJohn Marino   if (strcmp (reply, "OK") != 0)
11018cf7f2e2dSJohn Marino     error (_("Bogus reply from target: %s"), reply);
11019cf7f2e2dSJohn Marino }
11020cf7f2e2dSJohn Marino 
11021c50c785cSJohn Marino static struct traceframe_info *
remote_traceframe_info(void)11022c50c785cSJohn Marino remote_traceframe_info (void)
11023c50c785cSJohn Marino {
11024c50c785cSJohn Marino   char *text;
11025c50c785cSJohn Marino 
11026c50c785cSJohn Marino   text = target_read_stralloc (&current_target,
11027c50c785cSJohn Marino 			       TARGET_OBJECT_TRACEFRAME_INFO, NULL);
11028c50c785cSJohn Marino   if (text != NULL)
11029c50c785cSJohn Marino     {
11030c50c785cSJohn Marino       struct traceframe_info *info;
11031c50c785cSJohn Marino       struct cleanup *back_to = make_cleanup (xfree, text);
11032c50c785cSJohn Marino 
11033c50c785cSJohn Marino       info = parse_traceframe_info (text);
11034c50c785cSJohn Marino       do_cleanups (back_to);
11035c50c785cSJohn Marino       return info;
11036c50c785cSJohn Marino     }
11037c50c785cSJohn Marino 
11038c50c785cSJohn Marino   return NULL;
11039c50c785cSJohn Marino }
11040c50c785cSJohn Marino 
11041a45ae5f8SJohn Marino /* Handle the qTMinFTPILen packet.  Returns the minimum length of
11042a45ae5f8SJohn Marino    instruction on which a fast tracepoint may be placed.  Returns -1
11043a45ae5f8SJohn Marino    if the packet is not supported, and 0 if the minimum instruction
11044a45ae5f8SJohn Marino    length is unknown.  */
11045a45ae5f8SJohn Marino 
11046a45ae5f8SJohn Marino static int
remote_get_min_fast_tracepoint_insn_len(void)11047a45ae5f8SJohn Marino remote_get_min_fast_tracepoint_insn_len (void)
11048a45ae5f8SJohn Marino {
11049a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
11050a45ae5f8SJohn Marino   char *reply;
11051a45ae5f8SJohn Marino 
11052ef5ccd6cSJohn Marino   /* If we're not debugging a process yet, the IPA can't be
11053ef5ccd6cSJohn Marino      loaded.  */
11054ef5ccd6cSJohn Marino   if (!target_has_execution)
11055ef5ccd6cSJohn Marino     return 0;
11056ef5ccd6cSJohn Marino 
11057ef5ccd6cSJohn Marino   /* Make sure the remote is pointing at the right process.  */
11058ef5ccd6cSJohn Marino   set_general_process ();
11059ef5ccd6cSJohn Marino 
11060ef5ccd6cSJohn Marino   xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
11061a45ae5f8SJohn Marino   putpkt (rs->buf);
11062a45ae5f8SJohn Marino   reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11063a45ae5f8SJohn Marino   if (*reply == '\0')
11064a45ae5f8SJohn Marino     return -1;
11065a45ae5f8SJohn Marino   else
11066a45ae5f8SJohn Marino     {
11067a45ae5f8SJohn Marino       ULONGEST min_insn_len;
11068a45ae5f8SJohn Marino 
11069a45ae5f8SJohn Marino       unpack_varlen_hex (reply, &min_insn_len);
11070a45ae5f8SJohn Marino 
11071a45ae5f8SJohn Marino       return (int) min_insn_len;
11072a45ae5f8SJohn Marino     }
11073a45ae5f8SJohn Marino }
11074a45ae5f8SJohn Marino 
11075ef5ccd6cSJohn Marino static void
remote_set_trace_buffer_size(LONGEST val)11076ef5ccd6cSJohn Marino remote_set_trace_buffer_size (LONGEST val)
11077ef5ccd6cSJohn Marino {
11078ef5ccd6cSJohn Marino   if (remote_protocol_packets[PACKET_QTBuffer_size].support
11079ef5ccd6cSJohn Marino       != PACKET_DISABLE)
11080ef5ccd6cSJohn Marino     {
11081ef5ccd6cSJohn Marino       struct remote_state *rs = get_remote_state ();
11082ef5ccd6cSJohn Marino       char *buf = rs->buf;
11083ef5ccd6cSJohn Marino       char *endbuf = rs->buf + get_remote_packet_size ();
11084ef5ccd6cSJohn Marino       enum packet_result result;
11085ef5ccd6cSJohn Marino 
11086ef5ccd6cSJohn Marino       gdb_assert (val >= 0 || val == -1);
11087ef5ccd6cSJohn Marino       buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
11088ef5ccd6cSJohn Marino       /* Send -1 as literal "-1" to avoid host size dependency.  */
11089ef5ccd6cSJohn Marino       if (val < 0)
11090ef5ccd6cSJohn Marino 	{
11091ef5ccd6cSJohn Marino 	  *buf++ = '-';
11092ef5ccd6cSJohn Marino           buf += hexnumstr (buf, (ULONGEST) -val);
11093ef5ccd6cSJohn Marino 	}
11094ef5ccd6cSJohn Marino       else
11095ef5ccd6cSJohn Marino 	buf += hexnumstr (buf, (ULONGEST) val);
11096ef5ccd6cSJohn Marino 
11097ef5ccd6cSJohn Marino       putpkt (rs->buf);
11098ef5ccd6cSJohn Marino       remote_get_noisy_reply (&rs->buf, &rs->buf_size);
11099ef5ccd6cSJohn Marino       result = packet_ok (rs->buf,
11100ef5ccd6cSJohn Marino 		  &remote_protocol_packets[PACKET_QTBuffer_size]);
11101ef5ccd6cSJohn Marino 
11102ef5ccd6cSJohn Marino       if (result != PACKET_OK)
11103ef5ccd6cSJohn Marino 	warning (_("Bogus reply from target: %s"), rs->buf);
11104ef5ccd6cSJohn Marino     }
11105ef5ccd6cSJohn Marino }
11106ef5ccd6cSJohn Marino 
11107a45ae5f8SJohn Marino static int
remote_set_trace_notes(char * user,char * notes,char * stop_notes)11108a45ae5f8SJohn Marino remote_set_trace_notes (char *user, char *notes, char *stop_notes)
11109a45ae5f8SJohn Marino {
11110a45ae5f8SJohn Marino   struct remote_state *rs = get_remote_state ();
11111a45ae5f8SJohn Marino   char *reply;
11112a45ae5f8SJohn Marino   char *buf = rs->buf;
11113a45ae5f8SJohn Marino   char *endbuf = rs->buf + get_remote_packet_size ();
11114a45ae5f8SJohn Marino   int nbytes;
11115a45ae5f8SJohn Marino 
11116a45ae5f8SJohn Marino   buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
11117a45ae5f8SJohn Marino   if (user)
11118a45ae5f8SJohn Marino     {
11119a45ae5f8SJohn Marino       buf += xsnprintf (buf, endbuf - buf, "user:");
11120a45ae5f8SJohn Marino       nbytes = bin2hex (user, buf, 0);
11121a45ae5f8SJohn Marino       buf += 2 * nbytes;
11122a45ae5f8SJohn Marino       *buf++ = ';';
11123a45ae5f8SJohn Marino     }
11124a45ae5f8SJohn Marino   if (notes)
11125a45ae5f8SJohn Marino     {
11126a45ae5f8SJohn Marino       buf += xsnprintf (buf, endbuf - buf, "notes:");
11127a45ae5f8SJohn Marino       nbytes = bin2hex (notes, buf, 0);
11128a45ae5f8SJohn Marino       buf += 2 * nbytes;
11129a45ae5f8SJohn Marino       *buf++ = ';';
11130a45ae5f8SJohn Marino     }
11131a45ae5f8SJohn Marino   if (stop_notes)
11132a45ae5f8SJohn Marino     {
11133a45ae5f8SJohn Marino       buf += xsnprintf (buf, endbuf - buf, "tstop:");
11134a45ae5f8SJohn Marino       nbytes = bin2hex (stop_notes, buf, 0);
11135a45ae5f8SJohn Marino       buf += 2 * nbytes;
11136a45ae5f8SJohn Marino       *buf++ = ';';
11137a45ae5f8SJohn Marino     }
11138a45ae5f8SJohn Marino   /* Ensure the buffer is terminated.  */
11139a45ae5f8SJohn Marino   *buf = '\0';
11140a45ae5f8SJohn Marino 
11141a45ae5f8SJohn Marino   putpkt (rs->buf);
11142a45ae5f8SJohn Marino   reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
11143a45ae5f8SJohn Marino   if (*reply == '\0')
11144a45ae5f8SJohn Marino     return 0;
11145a45ae5f8SJohn Marino 
11146a45ae5f8SJohn Marino   if (strcmp (reply, "OK") != 0)
11147a45ae5f8SJohn Marino     error (_("Bogus reply from target: %s"), reply);
11148a45ae5f8SJohn Marino 
11149a45ae5f8SJohn Marino   return 1;
11150a45ae5f8SJohn Marino }
11151a45ae5f8SJohn Marino 
11152ef5ccd6cSJohn Marino static int
remote_use_agent(int use)11153ef5ccd6cSJohn Marino remote_use_agent (int use)
11154ef5ccd6cSJohn Marino {
11155ef5ccd6cSJohn Marino   if (remote_protocol_packets[PACKET_QAgent].support != PACKET_DISABLE)
11156ef5ccd6cSJohn Marino     {
11157ef5ccd6cSJohn Marino       struct remote_state *rs = get_remote_state ();
11158ef5ccd6cSJohn Marino 
11159ef5ccd6cSJohn Marino       /* If the stub supports QAgent.  */
11160ef5ccd6cSJohn Marino       xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
11161ef5ccd6cSJohn Marino       putpkt (rs->buf);
11162ef5ccd6cSJohn Marino       getpkt (&rs->buf, &rs->buf_size, 0);
11163ef5ccd6cSJohn Marino 
11164ef5ccd6cSJohn Marino       if (strcmp (rs->buf, "OK") == 0)
11165ef5ccd6cSJohn Marino 	{
11166ef5ccd6cSJohn Marino 	  use_agent = use;
11167ef5ccd6cSJohn Marino 	  return 1;
11168ef5ccd6cSJohn Marino 	}
11169ef5ccd6cSJohn Marino     }
11170ef5ccd6cSJohn Marino 
11171ef5ccd6cSJohn Marino   return 0;
11172ef5ccd6cSJohn Marino }
11173ef5ccd6cSJohn Marino 
11174ef5ccd6cSJohn Marino static int
remote_can_use_agent(void)11175ef5ccd6cSJohn Marino remote_can_use_agent (void)
11176ef5ccd6cSJohn Marino {
11177ef5ccd6cSJohn Marino   return (remote_protocol_packets[PACKET_QAgent].support != PACKET_DISABLE);
11178ef5ccd6cSJohn Marino }
11179ef5ccd6cSJohn Marino 
11180ef5ccd6cSJohn Marino struct btrace_target_info
11181ef5ccd6cSJohn Marino {
11182ef5ccd6cSJohn Marino   /* The ptid of the traced thread.  */
11183ef5ccd6cSJohn Marino   ptid_t ptid;
11184ef5ccd6cSJohn Marino };
11185ef5ccd6cSJohn Marino 
11186ef5ccd6cSJohn Marino /* Check whether the target supports branch tracing.  */
11187ef5ccd6cSJohn Marino 
11188ef5ccd6cSJohn Marino static int
remote_supports_btrace(void)11189ef5ccd6cSJohn Marino remote_supports_btrace (void)
11190ef5ccd6cSJohn Marino {
11191ef5ccd6cSJohn Marino   if (remote_protocol_packets[PACKET_Qbtrace_off].support != PACKET_ENABLE)
11192ef5ccd6cSJohn Marino     return 0;
11193ef5ccd6cSJohn Marino   if (remote_protocol_packets[PACKET_Qbtrace_bts].support != PACKET_ENABLE)
11194ef5ccd6cSJohn Marino     return 0;
11195ef5ccd6cSJohn Marino   if (remote_protocol_packets[PACKET_qXfer_btrace].support != PACKET_ENABLE)
11196ef5ccd6cSJohn Marino     return 0;
11197ef5ccd6cSJohn Marino 
11198ef5ccd6cSJohn Marino   return 1;
11199ef5ccd6cSJohn Marino }
11200ef5ccd6cSJohn Marino 
11201ef5ccd6cSJohn Marino /* Enable branch tracing.  */
11202ef5ccd6cSJohn Marino 
11203ef5ccd6cSJohn Marino static struct btrace_target_info *
remote_enable_btrace(ptid_t ptid)11204ef5ccd6cSJohn Marino remote_enable_btrace (ptid_t ptid)
11205ef5ccd6cSJohn Marino {
11206ef5ccd6cSJohn Marino   struct btrace_target_info *tinfo = NULL;
11207ef5ccd6cSJohn Marino   struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
11208ef5ccd6cSJohn Marino   struct remote_state *rs = get_remote_state ();
11209ef5ccd6cSJohn Marino   char *buf = rs->buf;
11210ef5ccd6cSJohn Marino   char *endbuf = rs->buf + get_remote_packet_size ();
11211ef5ccd6cSJohn Marino 
11212ef5ccd6cSJohn Marino   if (packet->support != PACKET_ENABLE)
11213ef5ccd6cSJohn Marino     error (_("Target does not support branch tracing."));
11214ef5ccd6cSJohn Marino 
11215ef5ccd6cSJohn Marino   set_general_thread (ptid);
11216ef5ccd6cSJohn Marino 
11217ef5ccd6cSJohn Marino   buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11218ef5ccd6cSJohn Marino   putpkt (rs->buf);
11219ef5ccd6cSJohn Marino   getpkt (&rs->buf, &rs->buf_size, 0);
11220ef5ccd6cSJohn Marino 
11221ef5ccd6cSJohn Marino   if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11222ef5ccd6cSJohn Marino     {
11223ef5ccd6cSJohn Marino       if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11224ef5ccd6cSJohn Marino 	error (_("Could not enable branch tracing for %s: %s"),
11225ef5ccd6cSJohn Marino 	       target_pid_to_str (ptid), rs->buf + 2);
11226ef5ccd6cSJohn Marino       else
11227ef5ccd6cSJohn Marino 	error (_("Could not enable branch tracing for %s."),
11228ef5ccd6cSJohn Marino 	       target_pid_to_str (ptid));
11229ef5ccd6cSJohn Marino     }
11230ef5ccd6cSJohn Marino 
11231ef5ccd6cSJohn Marino   tinfo = xzalloc (sizeof (*tinfo));
11232ef5ccd6cSJohn Marino   tinfo->ptid = ptid;
11233ef5ccd6cSJohn Marino 
11234ef5ccd6cSJohn Marino   return tinfo;
11235ef5ccd6cSJohn Marino }
11236ef5ccd6cSJohn Marino 
11237ef5ccd6cSJohn Marino /* Disable branch tracing.  */
11238ef5ccd6cSJohn Marino 
11239ef5ccd6cSJohn Marino static void
remote_disable_btrace(struct btrace_target_info * tinfo)11240ef5ccd6cSJohn Marino remote_disable_btrace (struct btrace_target_info *tinfo)
11241ef5ccd6cSJohn Marino {
11242ef5ccd6cSJohn Marino   struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
11243ef5ccd6cSJohn Marino   struct remote_state *rs = get_remote_state ();
11244ef5ccd6cSJohn Marino   char *buf = rs->buf;
11245ef5ccd6cSJohn Marino   char *endbuf = rs->buf + get_remote_packet_size ();
11246ef5ccd6cSJohn Marino 
11247ef5ccd6cSJohn Marino   if (packet->support != PACKET_ENABLE)
11248ef5ccd6cSJohn Marino     error (_("Target does not support branch tracing."));
11249ef5ccd6cSJohn Marino 
11250ef5ccd6cSJohn Marino   set_general_thread (tinfo->ptid);
11251ef5ccd6cSJohn Marino 
11252ef5ccd6cSJohn Marino   buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
11253ef5ccd6cSJohn Marino   putpkt (rs->buf);
11254ef5ccd6cSJohn Marino   getpkt (&rs->buf, &rs->buf_size, 0);
11255ef5ccd6cSJohn Marino 
11256ef5ccd6cSJohn Marino   if (packet_ok (rs->buf, packet) == PACKET_ERROR)
11257ef5ccd6cSJohn Marino     {
11258ef5ccd6cSJohn Marino       if (rs->buf[0] == 'E' && rs->buf[1] == '.')
11259ef5ccd6cSJohn Marino 	error (_("Could not disable branch tracing for %s: %s"),
11260ef5ccd6cSJohn Marino 	       target_pid_to_str (tinfo->ptid), rs->buf + 2);
11261ef5ccd6cSJohn Marino       else
11262ef5ccd6cSJohn Marino 	error (_("Could not disable branch tracing for %s."),
11263ef5ccd6cSJohn Marino 	       target_pid_to_str (tinfo->ptid));
11264ef5ccd6cSJohn Marino     }
11265ef5ccd6cSJohn Marino 
11266ef5ccd6cSJohn Marino   xfree (tinfo);
11267ef5ccd6cSJohn Marino }
11268ef5ccd6cSJohn Marino 
11269ef5ccd6cSJohn Marino /* Teardown branch tracing.  */
11270ef5ccd6cSJohn Marino 
11271ef5ccd6cSJohn Marino static void
remote_teardown_btrace(struct btrace_target_info * tinfo)11272ef5ccd6cSJohn Marino remote_teardown_btrace (struct btrace_target_info *tinfo)
11273ef5ccd6cSJohn Marino {
11274ef5ccd6cSJohn Marino   /* We must not talk to the target during teardown.  */
11275ef5ccd6cSJohn Marino   xfree (tinfo);
11276ef5ccd6cSJohn Marino }
11277ef5ccd6cSJohn Marino 
11278ef5ccd6cSJohn Marino /* Read the branch trace.  */
11279ef5ccd6cSJohn Marino 
VEC(btrace_block_s)11280ef5ccd6cSJohn Marino static VEC (btrace_block_s) *
11281ef5ccd6cSJohn Marino remote_read_btrace (struct btrace_target_info *tinfo,
11282ef5ccd6cSJohn Marino 		    enum btrace_read_type type)
11283ef5ccd6cSJohn Marino {
11284ef5ccd6cSJohn Marino   struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
11285ef5ccd6cSJohn Marino   struct remote_state *rs = get_remote_state ();
11286ef5ccd6cSJohn Marino   VEC (btrace_block_s) *btrace = NULL;
11287ef5ccd6cSJohn Marino   const char *annex;
11288ef5ccd6cSJohn Marino   char *xml;
11289ef5ccd6cSJohn Marino 
11290ef5ccd6cSJohn Marino   if (packet->support != PACKET_ENABLE)
11291ef5ccd6cSJohn Marino     error (_("Target does not support branch tracing."));
11292ef5ccd6cSJohn Marino 
11293ef5ccd6cSJohn Marino #if !defined(HAVE_LIBEXPAT)
11294ef5ccd6cSJohn Marino   error (_("Cannot process branch tracing result. XML parsing not supported."));
11295ef5ccd6cSJohn Marino #endif
11296ef5ccd6cSJohn Marino 
11297ef5ccd6cSJohn Marino   switch (type)
11298ef5ccd6cSJohn Marino     {
11299ef5ccd6cSJohn Marino     case btrace_read_all:
11300ef5ccd6cSJohn Marino       annex = "all";
11301ef5ccd6cSJohn Marino       break;
11302ef5ccd6cSJohn Marino     case btrace_read_new:
11303ef5ccd6cSJohn Marino       annex = "new";
11304ef5ccd6cSJohn Marino       break;
11305ef5ccd6cSJohn Marino     default:
11306ef5ccd6cSJohn Marino       internal_error (__FILE__, __LINE__,
11307ef5ccd6cSJohn Marino 		      _("Bad branch tracing read type: %u."),
11308ef5ccd6cSJohn Marino 		      (unsigned int) type);
11309ef5ccd6cSJohn Marino     }
11310ef5ccd6cSJohn Marino 
11311ef5ccd6cSJohn Marino   xml = target_read_stralloc (&current_target,
11312ef5ccd6cSJohn Marino                               TARGET_OBJECT_BTRACE, annex);
11313ef5ccd6cSJohn Marino   if (xml != NULL)
11314ef5ccd6cSJohn Marino     {
11315ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup (xfree, xml);
11316ef5ccd6cSJohn Marino 
11317ef5ccd6cSJohn Marino       btrace = parse_xml_btrace (xml);
11318ef5ccd6cSJohn Marino       do_cleanups (cleanup);
11319ef5ccd6cSJohn Marino     }
11320ef5ccd6cSJohn Marino 
11321ef5ccd6cSJohn Marino   return btrace;
11322ef5ccd6cSJohn Marino }
11323ef5ccd6cSJohn Marino 
113245796c8dcSSimon Schubert static void
init_remote_ops(void)113255796c8dcSSimon Schubert init_remote_ops (void)
113265796c8dcSSimon Schubert {
113275796c8dcSSimon Schubert   remote_ops.to_shortname = "remote";
113285796c8dcSSimon Schubert   remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
113295796c8dcSSimon Schubert   remote_ops.to_doc =
113305796c8dcSSimon Schubert     "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
113315796c8dcSSimon Schubert Specify the serial device it is connected to\n\
113325796c8dcSSimon Schubert (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
113335796c8dcSSimon Schubert   remote_ops.to_open = remote_open;
113345796c8dcSSimon Schubert   remote_ops.to_close = remote_close;
113355796c8dcSSimon Schubert   remote_ops.to_detach = remote_detach;
113365796c8dcSSimon Schubert   remote_ops.to_disconnect = remote_disconnect;
113375796c8dcSSimon Schubert   remote_ops.to_resume = remote_resume;
113385796c8dcSSimon Schubert   remote_ops.to_wait = remote_wait;
113395796c8dcSSimon Schubert   remote_ops.to_fetch_registers = remote_fetch_registers;
113405796c8dcSSimon Schubert   remote_ops.to_store_registers = remote_store_registers;
113415796c8dcSSimon Schubert   remote_ops.to_prepare_to_store = remote_prepare_to_store;
113425796c8dcSSimon Schubert   remote_ops.deprecated_xfer_memory = remote_xfer_memory;
113435796c8dcSSimon Schubert   remote_ops.to_files_info = remote_files_info;
113445796c8dcSSimon Schubert   remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
113455796c8dcSSimon Schubert   remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
113465796c8dcSSimon Schubert   remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
113475796c8dcSSimon Schubert   remote_ops.to_stopped_data_address = remote_stopped_data_address;
11348ef5ccd6cSJohn Marino   remote_ops.to_watchpoint_addr_within_range =
11349ef5ccd6cSJohn Marino     remote_watchpoint_addr_within_range;
113505796c8dcSSimon Schubert   remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
113515796c8dcSSimon Schubert   remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
113525796c8dcSSimon Schubert   remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
11353a45ae5f8SJohn Marino   remote_ops.to_region_ok_for_hw_watchpoint
11354a45ae5f8SJohn Marino      = remote_region_ok_for_hw_watchpoint;
113555796c8dcSSimon Schubert   remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
113565796c8dcSSimon Schubert   remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
113575796c8dcSSimon Schubert   remote_ops.to_kill = remote_kill;
113585796c8dcSSimon Schubert   remote_ops.to_load = generic_load;
113595796c8dcSSimon Schubert   remote_ops.to_mourn_inferior = remote_mourn;
11360a45ae5f8SJohn Marino   remote_ops.to_pass_signals = remote_pass_signals;
11361ef5ccd6cSJohn Marino   remote_ops.to_program_signals = remote_program_signals;
113625796c8dcSSimon Schubert   remote_ops.to_thread_alive = remote_thread_alive;
113635796c8dcSSimon Schubert   remote_ops.to_find_new_threads = remote_threads_info;
113645796c8dcSSimon Schubert   remote_ops.to_pid_to_str = remote_pid_to_str;
113655796c8dcSSimon Schubert   remote_ops.to_extra_thread_info = remote_threads_extra_info;
11366cf7f2e2dSJohn Marino   remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
113675796c8dcSSimon Schubert   remote_ops.to_stop = remote_stop;
113685796c8dcSSimon Schubert   remote_ops.to_xfer_partial = remote_xfer_partial;
113695796c8dcSSimon Schubert   remote_ops.to_rcmd = remote_rcmd;
113705796c8dcSSimon Schubert   remote_ops.to_log_command = serial_log_command;
113715796c8dcSSimon Schubert   remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
113725796c8dcSSimon Schubert   remote_ops.to_stratum = process_stratum;
113735796c8dcSSimon Schubert   remote_ops.to_has_all_memory = default_child_has_all_memory;
113745796c8dcSSimon Schubert   remote_ops.to_has_memory = default_child_has_memory;
113755796c8dcSSimon Schubert   remote_ops.to_has_stack = default_child_has_stack;
113765796c8dcSSimon Schubert   remote_ops.to_has_registers = default_child_has_registers;
113775796c8dcSSimon Schubert   remote_ops.to_has_execution = default_child_has_execution;
113785796c8dcSSimon Schubert   remote_ops.to_has_thread_control = tc_schedlock;    /* can lock scheduler */
113795796c8dcSSimon Schubert   remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
113805796c8dcSSimon Schubert   remote_ops.to_magic = OPS_MAGIC;
113815796c8dcSSimon Schubert   remote_ops.to_memory_map = remote_memory_map;
113825796c8dcSSimon Schubert   remote_ops.to_flash_erase = remote_flash_erase;
113835796c8dcSSimon Schubert   remote_ops.to_flash_done = remote_flash_done;
113845796c8dcSSimon Schubert   remote_ops.to_read_description = remote_read_description;
113855796c8dcSSimon Schubert   remote_ops.to_search_memory = remote_search_memory;
113865796c8dcSSimon Schubert   remote_ops.to_can_async_p = remote_can_async_p;
113875796c8dcSSimon Schubert   remote_ops.to_is_async_p = remote_is_async_p;
113885796c8dcSSimon Schubert   remote_ops.to_async = remote_async;
113895796c8dcSSimon Schubert   remote_ops.to_terminal_inferior = remote_terminal_inferior;
113905796c8dcSSimon Schubert   remote_ops.to_terminal_ours = remote_terminal_ours;
113915796c8dcSSimon Schubert   remote_ops.to_supports_non_stop = remote_supports_non_stop;
113925796c8dcSSimon Schubert   remote_ops.to_supports_multi_process = remote_supports_multi_process;
11393a45ae5f8SJohn Marino   remote_ops.to_supports_disable_randomization
11394a45ae5f8SJohn Marino     = remote_supports_disable_randomization;
11395ef5ccd6cSJohn Marino   remote_ops.to_fileio_open = remote_hostio_open;
11396ef5ccd6cSJohn Marino   remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
11397ef5ccd6cSJohn Marino   remote_ops.to_fileio_pread = remote_hostio_pread;
11398ef5ccd6cSJohn Marino   remote_ops.to_fileio_close = remote_hostio_close;
11399ef5ccd6cSJohn Marino   remote_ops.to_fileio_unlink = remote_hostio_unlink;
11400ef5ccd6cSJohn Marino   remote_ops.to_fileio_readlink = remote_hostio_readlink;
11401a45ae5f8SJohn Marino   remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
11402a45ae5f8SJohn Marino   remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
11403ef5ccd6cSJohn Marino   remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
11404ef5ccd6cSJohn Marino   remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands;
11405cf7f2e2dSJohn Marino   remote_ops.to_trace_init = remote_trace_init;
11406cf7f2e2dSJohn Marino   remote_ops.to_download_tracepoint = remote_download_tracepoint;
11407a45ae5f8SJohn Marino   remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
11408c50c785cSJohn Marino   remote_ops.to_download_trace_state_variable
11409c50c785cSJohn Marino     = remote_download_trace_state_variable;
11410a45ae5f8SJohn Marino   remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
11411a45ae5f8SJohn Marino   remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
11412cf7f2e2dSJohn Marino   remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
11413cf7f2e2dSJohn Marino   remote_ops.to_trace_start = remote_trace_start;
11414cf7f2e2dSJohn Marino   remote_ops.to_get_trace_status = remote_get_trace_status;
11415a45ae5f8SJohn Marino   remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
11416cf7f2e2dSJohn Marino   remote_ops.to_trace_stop = remote_trace_stop;
11417cf7f2e2dSJohn Marino   remote_ops.to_trace_find = remote_trace_find;
11418c50c785cSJohn Marino   remote_ops.to_get_trace_state_variable_value
11419c50c785cSJohn Marino     = remote_get_trace_state_variable_value;
11420cf7f2e2dSJohn Marino   remote_ops.to_save_trace_data = remote_save_trace_data;
11421cf7f2e2dSJohn Marino   remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
11422c50c785cSJohn Marino   remote_ops.to_upload_trace_state_variables
11423c50c785cSJohn Marino     = remote_upload_trace_state_variables;
11424cf7f2e2dSJohn Marino   remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
11425a45ae5f8SJohn Marino   remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
11426cf7f2e2dSJohn Marino   remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
11427cf7f2e2dSJohn Marino   remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
11428ef5ccd6cSJohn Marino   remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size;
11429a45ae5f8SJohn Marino   remote_ops.to_set_trace_notes = remote_set_trace_notes;
11430cf7f2e2dSJohn Marino   remote_ops.to_core_of_thread = remote_core_of_thread;
11431cf7f2e2dSJohn Marino   remote_ops.to_verify_memory = remote_verify_memory;
11432cf7f2e2dSJohn Marino   remote_ops.to_get_tib_address = remote_get_tib_address;
11433cf7f2e2dSJohn Marino   remote_ops.to_set_permissions = remote_set_permissions;
11434cf7f2e2dSJohn Marino   remote_ops.to_static_tracepoint_marker_at
11435cf7f2e2dSJohn Marino     = remote_static_tracepoint_marker_at;
11436cf7f2e2dSJohn Marino   remote_ops.to_static_tracepoint_markers_by_strid
11437cf7f2e2dSJohn Marino     = remote_static_tracepoint_markers_by_strid;
11438c50c785cSJohn Marino   remote_ops.to_traceframe_info = remote_traceframe_info;
11439ef5ccd6cSJohn Marino   remote_ops.to_use_agent = remote_use_agent;
11440ef5ccd6cSJohn Marino   remote_ops.to_can_use_agent = remote_can_use_agent;
11441ef5ccd6cSJohn Marino   remote_ops.to_supports_btrace = remote_supports_btrace;
11442ef5ccd6cSJohn Marino   remote_ops.to_enable_btrace = remote_enable_btrace;
11443ef5ccd6cSJohn Marino   remote_ops.to_disable_btrace = remote_disable_btrace;
11444ef5ccd6cSJohn Marino   remote_ops.to_teardown_btrace = remote_teardown_btrace;
11445ef5ccd6cSJohn Marino   remote_ops.to_read_btrace = remote_read_btrace;
114465796c8dcSSimon Schubert }
114475796c8dcSSimon Schubert 
114485796c8dcSSimon Schubert /* Set up the extended remote vector by making a copy of the standard
114495796c8dcSSimon Schubert    remote vector and adding to it.  */
114505796c8dcSSimon Schubert 
114515796c8dcSSimon Schubert static void
init_extended_remote_ops(void)114525796c8dcSSimon Schubert init_extended_remote_ops (void)
114535796c8dcSSimon Schubert {
114545796c8dcSSimon Schubert   extended_remote_ops = remote_ops;
114555796c8dcSSimon Schubert 
114565796c8dcSSimon Schubert   extended_remote_ops.to_shortname = "extended-remote";
114575796c8dcSSimon Schubert   extended_remote_ops.to_longname =
114585796c8dcSSimon Schubert     "Extended remote serial target in gdb-specific protocol";
114595796c8dcSSimon Schubert   extended_remote_ops.to_doc =
114605796c8dcSSimon Schubert     "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
114615796c8dcSSimon Schubert Specify the serial device it is connected to (e.g. /dev/ttya).";
114625796c8dcSSimon Schubert   extended_remote_ops.to_open = extended_remote_open;
114635796c8dcSSimon Schubert   extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
114645796c8dcSSimon Schubert   extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
114655796c8dcSSimon Schubert   extended_remote_ops.to_detach = extended_remote_detach;
114665796c8dcSSimon Schubert   extended_remote_ops.to_attach = extended_remote_attach;
114675796c8dcSSimon Schubert   extended_remote_ops.to_kill = extended_remote_kill;
11468a45ae5f8SJohn Marino   extended_remote_ops.to_supports_disable_randomization
11469a45ae5f8SJohn Marino     = extended_remote_supports_disable_randomization;
114705796c8dcSSimon Schubert }
114715796c8dcSSimon Schubert 
114725796c8dcSSimon Schubert static int
remote_can_async_p(void)114735796c8dcSSimon Schubert remote_can_async_p (void)
114745796c8dcSSimon Schubert {
114755796c8dcSSimon Schubert   if (!target_async_permitted)
114765796c8dcSSimon Schubert     /* We only enable async when the user specifically asks for it.  */
114775796c8dcSSimon Schubert     return 0;
114785796c8dcSSimon Schubert 
114795796c8dcSSimon Schubert   /* We're async whenever the serial device is.  */
11480a45ae5f8SJohn Marino   return serial_can_async_p (remote_desc);
114815796c8dcSSimon Schubert }
114825796c8dcSSimon Schubert 
114835796c8dcSSimon Schubert static int
remote_is_async_p(void)114845796c8dcSSimon Schubert remote_is_async_p (void)
114855796c8dcSSimon Schubert {
114865796c8dcSSimon Schubert   if (!target_async_permitted)
114875796c8dcSSimon Schubert     /* We only enable async when the user specifically asks for it.  */
114885796c8dcSSimon Schubert     return 0;
114895796c8dcSSimon Schubert 
114905796c8dcSSimon Schubert   /* We're async whenever the serial device is.  */
11491a45ae5f8SJohn Marino   return serial_is_async_p (remote_desc);
114925796c8dcSSimon Schubert }
114935796c8dcSSimon Schubert 
114945796c8dcSSimon Schubert /* Pass the SERIAL event on and up to the client.  One day this code
114955796c8dcSSimon Schubert    will be able to delay notifying the client of an event until the
114965796c8dcSSimon Schubert    point where an entire packet has been received.  */
114975796c8dcSSimon Schubert 
114985796c8dcSSimon Schubert static void (*async_client_callback) (enum inferior_event_type event_type,
114995796c8dcSSimon Schubert 				      void *context);
115005796c8dcSSimon Schubert static void *async_client_context;
115015796c8dcSSimon Schubert static serial_event_ftype remote_async_serial_handler;
115025796c8dcSSimon Schubert 
115035796c8dcSSimon Schubert static void
remote_async_serial_handler(struct serial * scb,void * context)115045796c8dcSSimon Schubert remote_async_serial_handler (struct serial *scb, void *context)
115055796c8dcSSimon Schubert {
115065796c8dcSSimon Schubert   /* Don't propogate error information up to the client.  Instead let
115075796c8dcSSimon Schubert      the client find out about the error by querying the target.  */
115085796c8dcSSimon Schubert   async_client_callback (INF_REG_EVENT, async_client_context);
115095796c8dcSSimon Schubert }
115105796c8dcSSimon Schubert 
115115796c8dcSSimon Schubert static void
remote_async_inferior_event_handler(gdb_client_data data)115125796c8dcSSimon Schubert remote_async_inferior_event_handler (gdb_client_data data)
115135796c8dcSSimon Schubert {
115145796c8dcSSimon Schubert   inferior_event_handler (INF_REG_EVENT, NULL);
115155796c8dcSSimon Schubert }
115165796c8dcSSimon Schubert 
115175796c8dcSSimon Schubert static void
remote_async(void (* callback)(enum inferior_event_type event_type,void * context),void * context)115185796c8dcSSimon Schubert remote_async (void (*callback) (enum inferior_event_type event_type,
115195796c8dcSSimon Schubert 				void *context), void *context)
115205796c8dcSSimon Schubert {
115215796c8dcSSimon Schubert   if (callback != NULL)
115225796c8dcSSimon Schubert     {
115235796c8dcSSimon Schubert       serial_async (remote_desc, remote_async_serial_handler, NULL);
115245796c8dcSSimon Schubert       async_client_callback = callback;
115255796c8dcSSimon Schubert       async_client_context = context;
115265796c8dcSSimon Schubert     }
115275796c8dcSSimon Schubert   else
115285796c8dcSSimon Schubert     serial_async (remote_desc, NULL, NULL);
115295796c8dcSSimon Schubert }
115305796c8dcSSimon Schubert 
115315796c8dcSSimon Schubert static void
set_remote_cmd(char * args,int from_tty)115325796c8dcSSimon Schubert set_remote_cmd (char *args, int from_tty)
115335796c8dcSSimon Schubert {
115345796c8dcSSimon Schubert   help_list (remote_set_cmdlist, "set remote ", -1, gdb_stdout);
115355796c8dcSSimon Schubert }
115365796c8dcSSimon Schubert 
115375796c8dcSSimon Schubert static void
show_remote_cmd(char * args,int from_tty)115385796c8dcSSimon Schubert show_remote_cmd (char *args, int from_tty)
115395796c8dcSSimon Schubert {
115405796c8dcSSimon Schubert   /* We can't just use cmd_show_list here, because we want to skip
115415796c8dcSSimon Schubert      the redundant "show remote Z-packet" and the legacy aliases.  */
115425796c8dcSSimon Schubert   struct cleanup *showlist_chain;
115435796c8dcSSimon Schubert   struct cmd_list_element *list = remote_show_cmdlist;
11544a45ae5f8SJohn Marino   struct ui_out *uiout = current_uiout;
115455796c8dcSSimon Schubert 
115465796c8dcSSimon Schubert   showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
115475796c8dcSSimon Schubert   for (; list != NULL; list = list->next)
115485796c8dcSSimon Schubert     if (strcmp (list->name, "Z-packet") == 0)
115495796c8dcSSimon Schubert       continue;
115505796c8dcSSimon Schubert     else if (list->type == not_set_cmd)
115515796c8dcSSimon Schubert       /* Alias commands are exactly like the original, except they
115525796c8dcSSimon Schubert 	 don't have the normal type.  */
115535796c8dcSSimon Schubert       continue;
115545796c8dcSSimon Schubert     else
115555796c8dcSSimon Schubert       {
115565796c8dcSSimon Schubert 	struct cleanup *option_chain
115575796c8dcSSimon Schubert 	  = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
11558cf7f2e2dSJohn Marino 
115595796c8dcSSimon Schubert 	ui_out_field_string (uiout, "name", list->name);
115605796c8dcSSimon Schubert 	ui_out_text (uiout, ":  ");
115615796c8dcSSimon Schubert 	if (list->type == show_cmd)
11562ef5ccd6cSJohn Marino 	  do_show_command ((char *) NULL, from_tty, list);
115635796c8dcSSimon Schubert 	else
115645796c8dcSSimon Schubert 	  cmd_func (list, NULL, from_tty);
115655796c8dcSSimon Schubert 	/* Close the tuple.  */
115665796c8dcSSimon Schubert 	do_cleanups (option_chain);
115675796c8dcSSimon Schubert       }
115685796c8dcSSimon Schubert 
115695796c8dcSSimon Schubert   /* Close the tuple.  */
115705796c8dcSSimon Schubert   do_cleanups (showlist_chain);
115715796c8dcSSimon Schubert }
115725796c8dcSSimon Schubert 
115735796c8dcSSimon Schubert 
115745796c8dcSSimon Schubert /* Function to be called whenever a new objfile (shlib) is detected.  */
115755796c8dcSSimon Schubert static void
remote_new_objfile(struct objfile * objfile)115765796c8dcSSimon Schubert remote_new_objfile (struct objfile *objfile)
115775796c8dcSSimon Schubert {
115785796c8dcSSimon Schubert   if (remote_desc != 0)		/* Have a remote connection.  */
115795796c8dcSSimon Schubert     remote_check_symbols (objfile);
115805796c8dcSSimon Schubert }
115815796c8dcSSimon Schubert 
11582cf7f2e2dSJohn Marino /* Pull all the tracepoints defined on the target and create local
11583cf7f2e2dSJohn Marino    data structures representing them.  We don't want to create real
11584cf7f2e2dSJohn Marino    tracepoints yet, we don't want to mess up the user's existing
11585cf7f2e2dSJohn Marino    collection.  */
11586cf7f2e2dSJohn Marino 
11587cf7f2e2dSJohn Marino static int
remote_upload_tracepoints(struct uploaded_tp ** utpp)11588cf7f2e2dSJohn Marino remote_upload_tracepoints (struct uploaded_tp **utpp)
11589cf7f2e2dSJohn Marino {
11590cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
11591cf7f2e2dSJohn Marino   char *p;
11592cf7f2e2dSJohn Marino 
11593cf7f2e2dSJohn Marino   /* Ask for a first packet of tracepoint definition.  */
11594cf7f2e2dSJohn Marino   putpkt ("qTfP");
11595cf7f2e2dSJohn Marino   getpkt (&rs->buf, &rs->buf_size, 0);
11596cf7f2e2dSJohn Marino   p = rs->buf;
11597cf7f2e2dSJohn Marino   while (*p && *p != 'l')
11598cf7f2e2dSJohn Marino     {
11599cf7f2e2dSJohn Marino       parse_tracepoint_definition (p, utpp);
11600cf7f2e2dSJohn Marino       /* Ask for another packet of tracepoint definition.  */
11601cf7f2e2dSJohn Marino       putpkt ("qTsP");
11602cf7f2e2dSJohn Marino       getpkt (&rs->buf, &rs->buf_size, 0);
11603cf7f2e2dSJohn Marino       p = rs->buf;
11604cf7f2e2dSJohn Marino     }
11605cf7f2e2dSJohn Marino   return 0;
11606cf7f2e2dSJohn Marino }
11607cf7f2e2dSJohn Marino 
11608cf7f2e2dSJohn Marino static int
remote_upload_trace_state_variables(struct uploaded_tsv ** utsvp)11609cf7f2e2dSJohn Marino remote_upload_trace_state_variables (struct uploaded_tsv **utsvp)
11610cf7f2e2dSJohn Marino {
11611cf7f2e2dSJohn Marino   struct remote_state *rs = get_remote_state ();
11612cf7f2e2dSJohn Marino   char *p;
11613cf7f2e2dSJohn Marino 
11614cf7f2e2dSJohn Marino   /* Ask for a first packet of variable definition.  */
11615cf7f2e2dSJohn Marino   putpkt ("qTfV");
11616cf7f2e2dSJohn Marino   getpkt (&rs->buf, &rs->buf_size, 0);
11617cf7f2e2dSJohn Marino   p = rs->buf;
11618cf7f2e2dSJohn Marino   while (*p && *p != 'l')
11619cf7f2e2dSJohn Marino     {
11620cf7f2e2dSJohn Marino       parse_tsv_definition (p, utsvp);
11621cf7f2e2dSJohn Marino       /* Ask for another packet of variable definition.  */
11622cf7f2e2dSJohn Marino       putpkt ("qTsV");
11623cf7f2e2dSJohn Marino       getpkt (&rs->buf, &rs->buf_size, 0);
11624cf7f2e2dSJohn Marino       p = rs->buf;
11625cf7f2e2dSJohn Marino     }
11626cf7f2e2dSJohn Marino   return 0;
11627cf7f2e2dSJohn Marino }
11628cf7f2e2dSJohn Marino 
116295796c8dcSSimon Schubert void
_initialize_remote(void)116305796c8dcSSimon Schubert _initialize_remote (void)
116315796c8dcSSimon Schubert {
116325796c8dcSSimon Schubert   struct remote_state *rs;
11633cf7f2e2dSJohn Marino   struct cmd_list_element *cmd;
11634cf7f2e2dSJohn Marino   char *cmd_name;
116355796c8dcSSimon Schubert 
116365796c8dcSSimon Schubert   /* architecture specific data */
116375796c8dcSSimon Schubert   remote_gdbarch_data_handle =
116385796c8dcSSimon Schubert     gdbarch_data_register_post_init (init_remote_state);
116395796c8dcSSimon Schubert   remote_g_packet_data_handle =
116405796c8dcSSimon Schubert     gdbarch_data_register_pre_init (remote_g_packet_data_init);
116415796c8dcSSimon Schubert 
116425796c8dcSSimon Schubert   /* Initialize the per-target state.  At the moment there is only one
116435796c8dcSSimon Schubert      of these, not one per target.  Only one target is active at a
116445796c8dcSSimon Schubert      time.  The default buffer size is unimportant; it will be expanded
116455796c8dcSSimon Schubert      whenever a larger buffer is needed.  */
116465796c8dcSSimon Schubert   rs = get_remote_state_raw ();
116475796c8dcSSimon Schubert   rs->buf_size = 400;
116485796c8dcSSimon Schubert   rs->buf = xmalloc (rs->buf_size);
116495796c8dcSSimon Schubert 
116505796c8dcSSimon Schubert   init_remote_ops ();
116515796c8dcSSimon Schubert   add_target (&remote_ops);
116525796c8dcSSimon Schubert 
116535796c8dcSSimon Schubert   init_extended_remote_ops ();
116545796c8dcSSimon Schubert   add_target (&extended_remote_ops);
116555796c8dcSSimon Schubert 
116565796c8dcSSimon Schubert   /* Hook into new objfile notification.  */
116575796c8dcSSimon Schubert   observer_attach_new_objfile (remote_new_objfile);
11658ef5ccd6cSJohn Marino   /* We're no longer interested in notification events of an inferior
11659ef5ccd6cSJohn Marino      when it exits.  */
11660ef5ccd6cSJohn Marino   observer_attach_inferior_exit (discard_pending_stop_replies);
116615796c8dcSSimon Schubert 
116625796c8dcSSimon Schubert   /* Set up signal handlers.  */
116635796c8dcSSimon Schubert   sigint_remote_token =
116645796c8dcSSimon Schubert     create_async_signal_handler (async_remote_interrupt, NULL);
116655796c8dcSSimon Schubert   sigint_remote_twice_token =
11666a45ae5f8SJohn Marino     create_async_signal_handler (async_remote_interrupt_twice, NULL);
116675796c8dcSSimon Schubert 
116685796c8dcSSimon Schubert #if 0
116695796c8dcSSimon Schubert   init_remote_threadtests ();
116705796c8dcSSimon Schubert #endif
116715796c8dcSSimon Schubert 
11672ef5ccd6cSJohn Marino   stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree);
116735796c8dcSSimon Schubert   /* set/show remote ...  */
116745796c8dcSSimon Schubert 
116755796c8dcSSimon Schubert   add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
116765796c8dcSSimon Schubert Remote protocol specific variables\n\
116775796c8dcSSimon Schubert Configure various remote-protocol specific variables such as\n\
116785796c8dcSSimon Schubert the packets being used"),
116795796c8dcSSimon Schubert 		  &remote_set_cmdlist, "set remote ",
116805796c8dcSSimon Schubert 		  0 /* allow-unknown */, &setlist);
116815796c8dcSSimon Schubert   add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
116825796c8dcSSimon Schubert Remote protocol specific variables\n\
116835796c8dcSSimon Schubert Configure various remote-protocol specific variables such as\n\
116845796c8dcSSimon Schubert the packets being used"),
116855796c8dcSSimon Schubert 		  &remote_show_cmdlist, "show remote ",
116865796c8dcSSimon Schubert 		  0 /* allow-unknown */, &showlist);
116875796c8dcSSimon Schubert 
116885796c8dcSSimon Schubert   add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
116895796c8dcSSimon Schubert Compare section data on target to the exec file.\n\
116905796c8dcSSimon Schubert Argument is a single section name (default: all loaded sections)."),
116915796c8dcSSimon Schubert 	   &cmdlist);
116925796c8dcSSimon Schubert 
116935796c8dcSSimon Schubert   add_cmd ("packet", class_maintenance, packet_command, _("\
116945796c8dcSSimon Schubert Send an arbitrary packet to a remote target.\n\
116955796c8dcSSimon Schubert    maintenance packet TEXT\n\
116965796c8dcSSimon Schubert If GDB is talking to an inferior via the GDB serial protocol, then\n\
116975796c8dcSSimon Schubert this command sends the string TEXT to the inferior, and displays the\n\
116985796c8dcSSimon Schubert response packet.  GDB supplies the initial `$' character, and the\n\
116995796c8dcSSimon Schubert terminating `#' character and checksum."),
117005796c8dcSSimon Schubert 	   &maintenancelist);
117015796c8dcSSimon Schubert 
117025796c8dcSSimon Schubert   add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
117035796c8dcSSimon Schubert Set whether to send break if interrupted."), _("\
117045796c8dcSSimon Schubert Show whether to send break if interrupted."), _("\
117055796c8dcSSimon Schubert If set, a break, instead of a cntrl-c, is sent to the remote target."),
11706cf7f2e2dSJohn Marino 			   set_remotebreak, show_remotebreak,
117075796c8dcSSimon Schubert 			   &setlist, &showlist);
11708cf7f2e2dSJohn Marino   cmd_name = "remotebreak";
11709cf7f2e2dSJohn Marino   cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
11710cf7f2e2dSJohn Marino   deprecate_cmd (cmd, "set remote interrupt-sequence");
11711cf7f2e2dSJohn Marino   cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
11712cf7f2e2dSJohn Marino   cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
11713cf7f2e2dSJohn Marino   deprecate_cmd (cmd, "show remote interrupt-sequence");
11714cf7f2e2dSJohn Marino 
11715cf7f2e2dSJohn Marino   add_setshow_enum_cmd ("interrupt-sequence", class_support,
11716c50c785cSJohn Marino 			interrupt_sequence_modes, &interrupt_sequence_mode,
11717c50c785cSJohn Marino 			_("\
11718cf7f2e2dSJohn Marino Set interrupt sequence to remote target."), _("\
11719cf7f2e2dSJohn Marino Show interrupt sequence to remote target."), _("\
11720cf7f2e2dSJohn Marino Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
11721cf7f2e2dSJohn Marino 			NULL, show_interrupt_sequence,
11722cf7f2e2dSJohn Marino 			&remote_set_cmdlist,
11723cf7f2e2dSJohn Marino 			&remote_show_cmdlist);
11724cf7f2e2dSJohn Marino 
11725cf7f2e2dSJohn Marino   add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
11726cf7f2e2dSJohn Marino 			   &interrupt_on_connect, _("\
11727cf7f2e2dSJohn Marino Set whether interrupt-sequence is sent to remote target when gdb connects to."), _("		\
11728cf7f2e2dSJohn Marino Show whether interrupt-sequence is sent to remote target when gdb connects to."), _("		\
11729cf7f2e2dSJohn Marino If set, interrupt sequence is sent to remote target."),
11730cf7f2e2dSJohn Marino 			   NULL, NULL,
11731cf7f2e2dSJohn Marino 			   &remote_set_cmdlist, &remote_show_cmdlist);
117325796c8dcSSimon Schubert 
117335796c8dcSSimon Schubert   /* Install commands for configuring memory read/write packets.  */
117345796c8dcSSimon Schubert 
117355796c8dcSSimon Schubert   add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
117365796c8dcSSimon Schubert Set the maximum number of bytes per memory write packet (deprecated)."),
117375796c8dcSSimon Schubert 	   &setlist);
117385796c8dcSSimon Schubert   add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
117395796c8dcSSimon Schubert Show the maximum number of bytes per memory write packet (deprecated)."),
117405796c8dcSSimon Schubert 	   &showlist);
117415796c8dcSSimon Schubert   add_cmd ("memory-write-packet-size", no_class,
117425796c8dcSSimon Schubert 	   set_memory_write_packet_size, _("\
117435796c8dcSSimon Schubert Set the maximum number of bytes per memory-write packet.\n\
117445796c8dcSSimon Schubert Specify the number of bytes in a packet or 0 (zero) for the\n\
117455796c8dcSSimon Schubert default packet size.  The actual limit is further reduced\n\
117465796c8dcSSimon Schubert dependent on the target.  Specify ``fixed'' to disable the\n\
117475796c8dcSSimon Schubert further restriction and ``limit'' to enable that restriction."),
117485796c8dcSSimon Schubert 	   &remote_set_cmdlist);
117495796c8dcSSimon Schubert   add_cmd ("memory-read-packet-size", no_class,
117505796c8dcSSimon Schubert 	   set_memory_read_packet_size, _("\
117515796c8dcSSimon Schubert Set the maximum number of bytes per memory-read packet.\n\
117525796c8dcSSimon Schubert Specify the number of bytes in a packet or 0 (zero) for the\n\
117535796c8dcSSimon Schubert default packet size.  The actual limit is further reduced\n\
117545796c8dcSSimon Schubert dependent on the target.  Specify ``fixed'' to disable the\n\
117555796c8dcSSimon Schubert further restriction and ``limit'' to enable that restriction."),
117565796c8dcSSimon Schubert 	   &remote_set_cmdlist);
117575796c8dcSSimon Schubert   add_cmd ("memory-write-packet-size", no_class,
117585796c8dcSSimon Schubert 	   show_memory_write_packet_size,
117595796c8dcSSimon Schubert 	   _("Show the maximum number of bytes per memory-write packet."),
117605796c8dcSSimon Schubert 	   &remote_show_cmdlist);
117615796c8dcSSimon Schubert   add_cmd ("memory-read-packet-size", no_class,
117625796c8dcSSimon Schubert 	   show_memory_read_packet_size,
117635796c8dcSSimon Schubert 	   _("Show the maximum number of bytes per memory-read packet."),
117645796c8dcSSimon Schubert 	   &remote_show_cmdlist);
117655796c8dcSSimon Schubert 
117665796c8dcSSimon Schubert   add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
117675796c8dcSSimon Schubert 			    &remote_hw_watchpoint_limit, _("\
117685796c8dcSSimon Schubert Set the maximum number of target hardware watchpoints."), _("\
117695796c8dcSSimon Schubert Show the maximum number of target hardware watchpoints."), _("\
117705796c8dcSSimon Schubert Specify a negative limit for unlimited."),
11771c50c785cSJohn Marino 			    NULL, NULL, /* FIXME: i18n: The maximum
11772c50c785cSJohn Marino 					   number of target hardware
11773c50c785cSJohn Marino 					   watchpoints is %s.  */
117745796c8dcSSimon Schubert 			    &remote_set_cmdlist, &remote_show_cmdlist);
11775a45ae5f8SJohn Marino   add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
11776a45ae5f8SJohn Marino 			    &remote_hw_watchpoint_length_limit, _("\
11777a45ae5f8SJohn Marino Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
11778a45ae5f8SJohn Marino Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
11779a45ae5f8SJohn Marino Specify a negative limit for unlimited."),
11780a45ae5f8SJohn Marino 			    NULL, NULL, /* FIXME: i18n: The maximum
11781a45ae5f8SJohn Marino                                            length (in bytes) of a target
11782a45ae5f8SJohn Marino                                            hardware watchpoint is %s.  */
11783a45ae5f8SJohn Marino 			    &remote_set_cmdlist, &remote_show_cmdlist);
117845796c8dcSSimon Schubert   add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
117855796c8dcSSimon Schubert 			    &remote_hw_breakpoint_limit, _("\
117865796c8dcSSimon Schubert Set the maximum number of target hardware breakpoints."), _("\
117875796c8dcSSimon Schubert Show the maximum number of target hardware breakpoints."), _("\
117885796c8dcSSimon Schubert Specify a negative limit for unlimited."),
11789c50c785cSJohn Marino 			    NULL, NULL, /* FIXME: i18n: The maximum
11790c50c785cSJohn Marino 					   number of target hardware
11791c50c785cSJohn Marino 					   breakpoints is %s.  */
117925796c8dcSSimon Schubert 			    &remote_set_cmdlist, &remote_show_cmdlist);
117935796c8dcSSimon Schubert 
11794ef5ccd6cSJohn Marino   add_setshow_uinteger_cmd ("remoteaddresssize", class_obscure,
117955796c8dcSSimon Schubert 			    &remote_address_size, _("\
117965796c8dcSSimon Schubert Set the maximum size of the address (in bits) in a memory packet."), _("\
117975796c8dcSSimon Schubert Show the maximum size of the address (in bits) in a memory packet."), NULL,
117985796c8dcSSimon Schubert 			    NULL,
117995796c8dcSSimon Schubert 			    NULL, /* FIXME: i18n: */
118005796c8dcSSimon Schubert 			    &setlist, &showlist);
118015796c8dcSSimon Schubert 
118025796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
118035796c8dcSSimon Schubert 			 "X", "binary-download", 1);
118045796c8dcSSimon Schubert 
118055796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
118065796c8dcSSimon Schubert 			 "vCont", "verbose-resume", 0);
118075796c8dcSSimon Schubert 
118085796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
118095796c8dcSSimon Schubert 			 "QPassSignals", "pass-signals", 0);
118105796c8dcSSimon Schubert 
11811ef5ccd6cSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
11812ef5ccd6cSJohn Marino 			 "QProgramSignals", "program-signals", 0);
11813ef5ccd6cSJohn Marino 
118145796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
118155796c8dcSSimon Schubert 			 "qSymbol", "symbol-lookup", 0);
118165796c8dcSSimon Schubert 
118175796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
118185796c8dcSSimon Schubert 			 "P", "set-register", 1);
118195796c8dcSSimon Schubert 
118205796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
118215796c8dcSSimon Schubert 			 "p", "fetch-register", 1);
118225796c8dcSSimon Schubert 
118235796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
118245796c8dcSSimon Schubert 			 "Z0", "software-breakpoint", 0);
118255796c8dcSSimon Schubert 
118265796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
118275796c8dcSSimon Schubert 			 "Z1", "hardware-breakpoint", 0);
118285796c8dcSSimon Schubert 
118295796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
118305796c8dcSSimon Schubert 			 "Z2", "write-watchpoint", 0);
118315796c8dcSSimon Schubert 
118325796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
118335796c8dcSSimon Schubert 			 "Z3", "read-watchpoint", 0);
118345796c8dcSSimon Schubert 
118355796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
118365796c8dcSSimon Schubert 			 "Z4", "access-watchpoint", 0);
118375796c8dcSSimon Schubert 
118385796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
118395796c8dcSSimon Schubert 			 "qXfer:auxv:read", "read-aux-vector", 0);
118405796c8dcSSimon Schubert 
118415796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
118425796c8dcSSimon Schubert 			 "qXfer:features:read", "target-features", 0);
118435796c8dcSSimon Schubert 
118445796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
118455796c8dcSSimon Schubert 			 "qXfer:libraries:read", "library-info", 0);
118465796c8dcSSimon Schubert 
11847a45ae5f8SJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
11848a45ae5f8SJohn Marino 			 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
11849a45ae5f8SJohn Marino 
118505796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
118515796c8dcSSimon Schubert 			 "qXfer:memory-map:read", "memory-map", 0);
118525796c8dcSSimon Schubert 
118535796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
118545796c8dcSSimon Schubert                          "qXfer:spu:read", "read-spu-object", 0);
118555796c8dcSSimon Schubert 
118565796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
118575796c8dcSSimon Schubert                          "qXfer:spu:write", "write-spu-object", 0);
118585796c8dcSSimon Schubert 
118595796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
118605796c8dcSSimon Schubert                         "qXfer:osdata:read", "osdata", 0);
118615796c8dcSSimon Schubert 
11862cf7f2e2dSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
11863cf7f2e2dSJohn Marino 			 "qXfer:threads:read", "threads", 0);
11864cf7f2e2dSJohn Marino 
118655796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
118665796c8dcSSimon Schubert                          "qXfer:siginfo:read", "read-siginfo-object", 0);
118675796c8dcSSimon Schubert 
118685796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
118695796c8dcSSimon Schubert                          "qXfer:siginfo:write", "write-siginfo-object", 0);
118705796c8dcSSimon Schubert 
11871c50c785cSJohn Marino   add_packet_config_cmd
11872c50c785cSJohn Marino     (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
11873c50c785cSJohn Marino      "qXfer:trace-frame-info:read", "traceframe-info", 0);
11874c50c785cSJohn Marino 
11875ef5ccd6cSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
11876ef5ccd6cSJohn Marino 			 "qXfer:uib:read", "unwind-info-block", 0);
11877ef5ccd6cSJohn Marino 
118785796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
118795796c8dcSSimon Schubert 			 "qGetTLSAddr", "get-thread-local-storage-address",
118805796c8dcSSimon Schubert 			 0);
118815796c8dcSSimon Schubert 
11882cf7f2e2dSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
11883cf7f2e2dSJohn Marino 			 "qGetTIBAddr", "get-thread-information-block-address",
11884cf7f2e2dSJohn Marino 			 0);
11885cf7f2e2dSJohn Marino 
118865796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
118875796c8dcSSimon Schubert 			 "bc", "reverse-continue", 0);
118885796c8dcSSimon Schubert 
118895796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
118905796c8dcSSimon Schubert 			 "bs", "reverse-step", 0);
118915796c8dcSSimon Schubert 
118925796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
118935796c8dcSSimon Schubert 			 "qSupported", "supported-packets", 0);
118945796c8dcSSimon Schubert 
118955796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
118965796c8dcSSimon Schubert 			 "qSearch:memory", "search-memory", 0);
118975796c8dcSSimon Schubert 
118985796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
118995796c8dcSSimon Schubert 			 "vFile:open", "hostio-open", 0);
119005796c8dcSSimon Schubert 
119015796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
119025796c8dcSSimon Schubert 			 "vFile:pread", "hostio-pread", 0);
119035796c8dcSSimon Schubert 
119045796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
119055796c8dcSSimon Schubert 			 "vFile:pwrite", "hostio-pwrite", 0);
119065796c8dcSSimon Schubert 
119075796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
119085796c8dcSSimon Schubert 			 "vFile:close", "hostio-close", 0);
119095796c8dcSSimon Schubert 
119105796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
119115796c8dcSSimon Schubert 			 "vFile:unlink", "hostio-unlink", 0);
119125796c8dcSSimon Schubert 
11913ef5ccd6cSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
11914ef5ccd6cSJohn Marino 			 "vFile:readlink", "hostio-readlink", 0);
11915ef5ccd6cSJohn Marino 
119165796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
119175796c8dcSSimon Schubert 			 "vAttach", "attach", 0);
119185796c8dcSSimon Schubert 
119195796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
119205796c8dcSSimon Schubert 			 "vRun", "run", 0);
119215796c8dcSSimon Schubert 
119225796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
119235796c8dcSSimon Schubert 			 "QStartNoAckMode", "noack", 0);
119245796c8dcSSimon Schubert 
119255796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
119265796c8dcSSimon Schubert 			 "vKill", "kill", 0);
119275796c8dcSSimon Schubert 
119285796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
119295796c8dcSSimon Schubert 			 "qAttached", "query-attached", 0);
119305796c8dcSSimon Schubert 
119315796c8dcSSimon Schubert   add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
11932c50c785cSJohn Marino 			 "ConditionalTracepoints",
11933c50c785cSJohn Marino 			 "conditional-tracepoints", 0);
11934ef5ccd6cSJohn Marino 
11935ef5ccd6cSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
11936ef5ccd6cSJohn Marino 			 "ConditionalBreakpoints",
11937ef5ccd6cSJohn Marino 			 "conditional-breakpoints", 0);
11938ef5ccd6cSJohn Marino 
11939ef5ccd6cSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
11940ef5ccd6cSJohn Marino 			 "BreakpointCommands",
11941ef5ccd6cSJohn Marino 			 "breakpoint-commands", 0);
11942ef5ccd6cSJohn Marino 
11943cf7f2e2dSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
11944cf7f2e2dSJohn Marino 			 "FastTracepoints", "fast-tracepoints", 0);
11945cf7f2e2dSJohn Marino 
11946cf7f2e2dSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
11947cf7f2e2dSJohn Marino 			 "TracepointSource", "TracepointSource", 0);
11948cf7f2e2dSJohn Marino 
11949cf7f2e2dSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
11950cf7f2e2dSJohn Marino 			 "QAllow", "allow", 0);
11951cf7f2e2dSJohn Marino 
11952cf7f2e2dSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
11953cf7f2e2dSJohn Marino 			 "StaticTracepoints", "static-tracepoints", 0);
11954cf7f2e2dSJohn Marino 
11955a45ae5f8SJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
11956a45ae5f8SJohn Marino 			 "InstallInTrace", "install-in-trace", 0);
11957a45ae5f8SJohn Marino 
11958cf7f2e2dSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
11959cf7f2e2dSJohn Marino                          "qXfer:statictrace:read", "read-sdata-object", 0);
119605796c8dcSSimon Schubert 
11961a45ae5f8SJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
11962a45ae5f8SJohn Marino 			 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
11963a45ae5f8SJohn Marino 
11964a45ae5f8SJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
11965a45ae5f8SJohn Marino 			 "QDisableRandomization", "disable-randomization", 0);
11966a45ae5f8SJohn Marino 
11967ef5ccd6cSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
11968ef5ccd6cSJohn Marino 			 "QAgent", "agent", 0);
11969ef5ccd6cSJohn Marino 
11970ef5ccd6cSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
11971ef5ccd6cSJohn Marino 			 "QTBuffer:size", "trace-buffer-size", 0);
11972ef5ccd6cSJohn Marino 
11973ef5ccd6cSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
11974ef5ccd6cSJohn Marino        "Qbtrace:off", "disable-btrace", 0);
11975ef5ccd6cSJohn Marino 
11976ef5ccd6cSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
11977ef5ccd6cSJohn Marino        "Qbtrace:bts", "enable-btrace", 0);
11978ef5ccd6cSJohn Marino 
11979ef5ccd6cSJohn Marino   add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
11980ef5ccd6cSJohn Marino        "qXfer:btrace", "read-btrace", 0);
11981ef5ccd6cSJohn Marino 
119825796c8dcSSimon Schubert   /* Keep the old ``set remote Z-packet ...'' working.  Each individual
119835796c8dcSSimon Schubert      Z sub-packet has its own set and show commands, but users may
119845796c8dcSSimon Schubert      have sets to this variable in their .gdbinit files (or in their
119855796c8dcSSimon Schubert      documentation).  */
119865796c8dcSSimon Schubert   add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
119875796c8dcSSimon Schubert 				&remote_Z_packet_detect, _("\
119885796c8dcSSimon Schubert Set use of remote protocol `Z' packets"), _("\
119895796c8dcSSimon Schubert Show use of remote protocol `Z' packets "), _("\
119905796c8dcSSimon Schubert When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
119915796c8dcSSimon Schubert packets."),
119925796c8dcSSimon Schubert 				set_remote_protocol_Z_packet_cmd,
11993c50c785cSJohn Marino 				show_remote_protocol_Z_packet_cmd,
11994c50c785cSJohn Marino 				/* FIXME: i18n: Use of remote protocol
11995c50c785cSJohn Marino 				   `Z' packets is %s.  */
119965796c8dcSSimon Schubert 				&remote_set_cmdlist, &remote_show_cmdlist);
119975796c8dcSSimon Schubert 
119985796c8dcSSimon Schubert   add_prefix_cmd ("remote", class_files, remote_command, _("\
119995796c8dcSSimon Schubert Manipulate files on the remote system\n\
120005796c8dcSSimon Schubert Transfer files to and from the remote target system."),
120015796c8dcSSimon Schubert 		  &remote_cmdlist, "remote ",
120025796c8dcSSimon Schubert 		  0 /* allow-unknown */, &cmdlist);
120035796c8dcSSimon Schubert 
120045796c8dcSSimon Schubert   add_cmd ("put", class_files, remote_put_command,
120055796c8dcSSimon Schubert 	   _("Copy a local file to the remote system."),
120065796c8dcSSimon Schubert 	   &remote_cmdlist);
120075796c8dcSSimon Schubert 
120085796c8dcSSimon Schubert   add_cmd ("get", class_files, remote_get_command,
120095796c8dcSSimon Schubert 	   _("Copy a remote file to the local system."),
120105796c8dcSSimon Schubert 	   &remote_cmdlist);
120115796c8dcSSimon Schubert 
120125796c8dcSSimon Schubert   add_cmd ("delete", class_files, remote_delete_command,
120135796c8dcSSimon Schubert 	   _("Delete a remote file."),
120145796c8dcSSimon Schubert 	   &remote_cmdlist);
120155796c8dcSSimon Schubert 
120165796c8dcSSimon Schubert   remote_exec_file = xstrdup ("");
120175796c8dcSSimon Schubert   add_setshow_string_noescape_cmd ("exec-file", class_files,
120185796c8dcSSimon Schubert 				   &remote_exec_file, _("\
120195796c8dcSSimon Schubert Set the remote pathname for \"run\""), _("\
120205796c8dcSSimon Schubert Show the remote pathname for \"run\""), NULL, NULL, NULL,
120215796c8dcSSimon Schubert 				   &remote_set_cmdlist, &remote_show_cmdlist);
120225796c8dcSSimon Schubert 
120235796c8dcSSimon Schubert   /* Eventually initialize fileio.  See fileio.c */
120245796c8dcSSimon Schubert   initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
120255796c8dcSSimon Schubert 
120265796c8dcSSimon Schubert   /* Take advantage of the fact that the LWP field is not used, to tag
120275796c8dcSSimon Schubert      special ptids with it set to != 0.  */
120285796c8dcSSimon Schubert   magic_null_ptid = ptid_build (42000, 1, -1);
120295796c8dcSSimon Schubert   not_sent_ptid = ptid_build (42000, 1, -2);
120305796c8dcSSimon Schubert   any_thread_ptid = ptid_build (42000, 1, 0);
12031cf7f2e2dSJohn Marino 
12032cf7f2e2dSJohn Marino   target_buf_size = 2048;
12033cf7f2e2dSJohn Marino   target_buf = xmalloc (target_buf_size);
120345796c8dcSSimon Schubert }
12035cf7f2e2dSJohn Marino 
12036