Home | History | Annotate | Line # | Download | only in gdbserver
      1      1.1  christos /* Main code for remote server for GDB.
      2  1.1.1.3  christos    Copyright (C) 1989-2024 Free Software Foundation, Inc.
      3      1.1  christos 
      4      1.1  christos    This file is part of GDB.
      5      1.1  christos 
      6      1.1  christos    This program is free software; you can redistribute it and/or modify
      7      1.1  christos    it under the terms of the GNU General Public License as published by
      8      1.1  christos    the Free Software Foundation; either version 3 of the License, or
      9      1.1  christos    (at your option) any later version.
     10      1.1  christos 
     11      1.1  christos    This program is distributed in the hope that it will be useful,
     12      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14      1.1  christos    GNU General Public License for more details.
     15      1.1  christos 
     16      1.1  christos    You should have received a copy of the GNU General Public License
     17      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18      1.1  christos 
     19      1.1  christos #include "gdbthread.h"
     20      1.1  christos #include "gdbsupport/agent.h"
     21      1.1  christos #include "notif.h"
     22      1.1  christos #include "tdesc.h"
     23      1.1  christos #include "gdbsupport/rsp-low.h"
     24      1.1  christos #include "gdbsupport/signals-state-save-restore.h"
     25      1.1  christos #include <ctype.h>
     26      1.1  christos #include <unistd.h>
     27      1.1  christos #if HAVE_SIGNAL_H
     28      1.1  christos #include <signal.h>
     29      1.1  christos #endif
     30      1.1  christos #include "gdbsupport/gdb_vecs.h"
     31      1.1  christos #include "gdbsupport/gdb_wait.h"
     32      1.1  christos #include "gdbsupport/btrace-common.h"
     33      1.1  christos #include "gdbsupport/filestuff.h"
     34      1.1  christos #include "tracepoint.h"
     35      1.1  christos #include "dll.h"
     36      1.1  christos #include "hostio.h"
     37      1.1  christos #include <vector>
     38  1.1.1.3  christos #include <unordered_map>
     39      1.1  christos #include "gdbsupport/common-inferior.h"
     40      1.1  christos #include "gdbsupport/job-control.h"
     41      1.1  christos #include "gdbsupport/environ.h"
     42      1.1  christos #include "filenames.h"
     43      1.1  christos #include "gdbsupport/pathstuff.h"
     44      1.1  christos #ifdef USE_XML
     45      1.1  christos #include "xml-builtin.h"
     46      1.1  christos #endif
     47      1.1  christos 
     48      1.1  christos #include "gdbsupport/selftest.h"
     49      1.1  christos #include "gdbsupport/scope-exit.h"
     50      1.1  christos #include "gdbsupport/gdb_select.h"
     51      1.1  christos #include "gdbsupport/scoped_restore.h"
     52  1.1.1.2  christos #include "gdbsupport/search.h"
     53      1.1  christos 
     54  1.1.1.3  christos /* PBUFSIZ must also be at least as big as IPA_CMD_BUF_SIZE, because
     55  1.1.1.3  christos    the client state data is passed directly to some agent
     56  1.1.1.3  christos    functions.  */
     57  1.1.1.3  christos static_assert (PBUFSIZ >= IPA_CMD_BUF_SIZE);
     58  1.1.1.3  christos 
     59      1.1  christos #define require_running_or_return(BUF)		\
     60      1.1  christos   if (!target_running ())			\
     61      1.1  christos     {						\
     62      1.1  christos       write_enn (BUF);				\
     63      1.1  christos       return;					\
     64      1.1  christos     }
     65      1.1  christos 
     66      1.1  christos #define require_running_or_break(BUF)		\
     67      1.1  christos   if (!target_running ())			\
     68      1.1  christos     {						\
     69      1.1  christos       write_enn (BUF);				\
     70      1.1  christos       break;					\
     71      1.1  christos     }
     72      1.1  christos 
     73      1.1  christos /* The environment to pass to the inferior when creating it.  */
     74      1.1  christos 
     75      1.1  christos static gdb_environ our_environ;
     76      1.1  christos 
     77      1.1  christos bool server_waiting;
     78      1.1  christos 
     79      1.1  christos static bool extended_protocol;
     80      1.1  christos static bool response_needed;
     81      1.1  christos static bool exit_requested;
     82      1.1  christos 
     83      1.1  christos /* --once: Exit after the first connection has closed.  */
     84      1.1  christos bool run_once;
     85      1.1  christos 
     86      1.1  christos /* Whether to report TARGET_WAITKIND_NO_RESUMED events.  */
     87      1.1  christos static bool report_no_resumed;
     88      1.1  christos 
     89      1.1  christos /* The event loop checks this to decide whether to continue accepting
     90      1.1  christos    events.  */
     91      1.1  christos static bool keep_processing_events = true;
     92      1.1  christos 
     93      1.1  christos bool non_stop;
     94      1.1  christos 
     95      1.1  christos static struct {
     96      1.1  christos   /* Set the PROGRAM_PATH.  Here we adjust the path of the provided
     97      1.1  christos      binary if needed.  */
     98  1.1.1.2  christos   void set (const char *path)
     99      1.1  christos   {
    100  1.1.1.2  christos     m_path = path;
    101      1.1  christos 
    102      1.1  christos     /* Make sure we're using the absolute path of the inferior when
    103      1.1  christos        creating it.  */
    104  1.1.1.2  christos     if (!contains_dir_separator (m_path.c_str ()))
    105      1.1  christos       {
    106      1.1  christos 	int reg_file_errno;
    107      1.1  christos 
    108      1.1  christos 	/* Check if the file is in our CWD.  If it is, then we prefix
    109      1.1  christos 	   its name with CURRENT_DIRECTORY.  Otherwise, we leave the
    110      1.1  christos 	   name as-is because we'll try searching for it in $PATH.  */
    111  1.1.1.2  christos 	if (is_regular_file (m_path.c_str (), &reg_file_errno))
    112  1.1.1.2  christos 	  m_path = gdb_abspath (m_path.c_str ());
    113      1.1  christos       }
    114      1.1  christos   }
    115      1.1  christos 
    116      1.1  christos   /* Return the PROGRAM_PATH.  */
    117  1.1.1.2  christos   const char *get ()
    118  1.1.1.2  christos   { return m_path.empty () ? nullptr : m_path.c_str (); }
    119      1.1  christos 
    120      1.1  christos private:
    121      1.1  christos   /* The program name, adjusted if needed.  */
    122  1.1.1.2  christos   std::string m_path;
    123      1.1  christos } program_path;
    124      1.1  christos static std::vector<char *> program_args;
    125      1.1  christos static std::string wrapper_argv;
    126      1.1  christos 
    127      1.1  christos /* The PID of the originally created or attached inferior.  Used to
    128      1.1  christos    send signals to the process when GDB sends us an asynchronous interrupt
    129      1.1  christos    (user hitting Control-C in the client), and to wait for the child to exit
    130      1.1  christos    when no longer debugging it.  */
    131      1.1  christos 
    132      1.1  christos unsigned long signal_pid;
    133      1.1  christos 
    134      1.1  christos /* Set if you want to disable optional thread related packets support
    135      1.1  christos    in gdbserver, for the sake of testing GDB against stubs that don't
    136      1.1  christos    support them.  */
    137      1.1  christos bool disable_packet_vCont;
    138      1.1  christos bool disable_packet_Tthread;
    139      1.1  christos bool disable_packet_qC;
    140      1.1  christos bool disable_packet_qfThreadInfo;
    141      1.1  christos bool disable_packet_T;
    142      1.1  christos 
    143      1.1  christos static unsigned char *mem_buf;
    144      1.1  christos 
    145      1.1  christos /* A sub-class of 'struct notif_event' for stop, holding information
    146      1.1  christos    relative to a single stop reply.  We keep a queue of these to
    147      1.1  christos    push to GDB in non-stop mode.  */
    148      1.1  christos 
    149      1.1  christos struct vstop_notif : public notif_event
    150      1.1  christos {
    151      1.1  christos   /* Thread or process that got the event.  */
    152      1.1  christos   ptid_t ptid;
    153      1.1  christos 
    154      1.1  christos   /* Event info.  */
    155      1.1  christos   struct target_waitstatus status;
    156      1.1  christos };
    157      1.1  christos 
    158      1.1  christos /* The current btrace configuration.  This is gdbserver's mirror of GDB's
    159      1.1  christos    btrace configuration.  */
    160      1.1  christos static struct btrace_config current_btrace_conf;
    161      1.1  christos 
    162      1.1  christos /* The client remote protocol state. */
    163      1.1  christos 
    164      1.1  christos static client_state g_client_state;
    165      1.1  christos 
    166      1.1  christos client_state &
    167      1.1  christos get_client_state ()
    168      1.1  christos {
    169      1.1  christos   client_state &cs = g_client_state;
    170      1.1  christos   return cs;
    171      1.1  christos }
    172      1.1  christos 
    173      1.1  christos 
    174      1.1  christos /* Put a stop reply to the stop reply queue.  */
    175      1.1  christos 
    176      1.1  christos static void
    177  1.1.1.2  christos queue_stop_reply (ptid_t ptid, const target_waitstatus &status)
    178      1.1  christos {
    179      1.1  christos   struct vstop_notif *new_notif = new struct vstop_notif;
    180      1.1  christos 
    181      1.1  christos   new_notif->ptid = ptid;
    182  1.1.1.2  christos   new_notif->status = status;
    183      1.1  christos 
    184      1.1  christos   notif_event_enque (&notif_stop, new_notif);
    185      1.1  christos }
    186      1.1  christos 
    187      1.1  christos static bool
    188      1.1  christos remove_all_on_match_ptid (struct notif_event *event, ptid_t filter_ptid)
    189      1.1  christos {
    190      1.1  christos   struct vstop_notif *vstop_event = (struct vstop_notif *) event;
    191      1.1  christos 
    192      1.1  christos   return vstop_event->ptid.matches (filter_ptid);
    193      1.1  christos }
    194      1.1  christos 
    195      1.1  christos /* See server.h.  */
    196      1.1  christos 
    197      1.1  christos void
    198      1.1  christos discard_queued_stop_replies (ptid_t ptid)
    199      1.1  christos {
    200      1.1  christos   std::list<notif_event *>::iterator iter, next, end;
    201      1.1  christos   end = notif_stop.queue.end ();
    202      1.1  christos   for (iter = notif_stop.queue.begin (); iter != end; iter = next)
    203      1.1  christos     {
    204      1.1  christos       next = iter;
    205      1.1  christos       ++next;
    206      1.1  christos 
    207  1.1.1.2  christos       if (iter == notif_stop.queue.begin ())
    208  1.1.1.2  christos 	{
    209  1.1.1.2  christos 	  /* The head of the list contains the notification that was
    210  1.1.1.2  christos 	     already sent to GDB.  So we can't remove it, otherwise
    211  1.1.1.2  christos 	     when GDB sends the vStopped, it would ack the _next_
    212  1.1.1.2  christos 	     notification, which hadn't been sent yet!  */
    213  1.1.1.2  christos 	  continue;
    214  1.1.1.2  christos 	}
    215  1.1.1.2  christos 
    216      1.1  christos       if (remove_all_on_match_ptid (*iter, ptid))
    217      1.1  christos 	{
    218      1.1  christos 	  delete *iter;
    219      1.1  christos 	  notif_stop.queue.erase (iter);
    220      1.1  christos 	}
    221      1.1  christos     }
    222      1.1  christos }
    223      1.1  christos 
    224      1.1  christos static void
    225      1.1  christos vstop_notif_reply (struct notif_event *event, char *own_buf)
    226      1.1  christos {
    227      1.1  christos   struct vstop_notif *vstop = (struct vstop_notif *) event;
    228      1.1  christos 
    229  1.1.1.2  christos   prepare_resume_reply (own_buf, vstop->ptid, vstop->status);
    230      1.1  christos }
    231      1.1  christos 
    232      1.1  christos /* Helper for in_queued_stop_replies.  */
    233      1.1  christos 
    234      1.1  christos static bool
    235      1.1  christos in_queued_stop_replies_ptid (struct notif_event *event, ptid_t filter_ptid)
    236      1.1  christos {
    237      1.1  christos   struct vstop_notif *vstop_event = (struct vstop_notif *) event;
    238      1.1  christos 
    239      1.1  christos   if (vstop_event->ptid.matches (filter_ptid))
    240      1.1  christos     return true;
    241      1.1  christos 
    242      1.1  christos   /* Don't resume fork children that GDB does not know about yet.  */
    243  1.1.1.2  christos   if ((vstop_event->status.kind () == TARGET_WAITKIND_FORKED
    244  1.1.1.3  christos        || vstop_event->status.kind () == TARGET_WAITKIND_VFORKED
    245  1.1.1.3  christos        || vstop_event->status.kind () == TARGET_WAITKIND_THREAD_CLONED)
    246  1.1.1.2  christos       && vstop_event->status.child_ptid ().matches (filter_ptid))
    247      1.1  christos     return true;
    248      1.1  christos 
    249      1.1  christos   return false;
    250      1.1  christos }
    251      1.1  christos 
    252      1.1  christos /* See server.h.  */
    253      1.1  christos 
    254      1.1  christos int
    255      1.1  christos in_queued_stop_replies (ptid_t ptid)
    256      1.1  christos {
    257      1.1  christos   for (notif_event *event : notif_stop.queue)
    258      1.1  christos     {
    259      1.1  christos       if (in_queued_stop_replies_ptid (event, ptid))
    260      1.1  christos 	return true;
    261      1.1  christos     }
    262      1.1  christos 
    263      1.1  christos   return false;
    264      1.1  christos }
    265      1.1  christos 
    266      1.1  christos struct notif_server notif_stop =
    267      1.1  christos {
    268      1.1  christos   "vStopped", "Stop", {}, vstop_notif_reply,
    269      1.1  christos };
    270      1.1  christos 
    271      1.1  christos static int
    272      1.1  christos target_running (void)
    273      1.1  christos {
    274      1.1  christos   return get_first_thread () != NULL;
    275      1.1  christos }
    276      1.1  christos 
    277      1.1  christos /* See gdbsupport/common-inferior.h.  */
    278      1.1  christos 
    279      1.1  christos const char *
    280      1.1  christos get_exec_wrapper ()
    281      1.1  christos {
    282      1.1  christos   return !wrapper_argv.empty () ? wrapper_argv.c_str () : NULL;
    283      1.1  christos }
    284      1.1  christos 
    285      1.1  christos /* See gdbsupport/common-inferior.h.  */
    286      1.1  christos 
    287      1.1  christos const char *
    288      1.1  christos get_exec_file (int err)
    289      1.1  christos {
    290      1.1  christos   if (err && program_path.get () == NULL)
    291      1.1  christos     error (_("No executable file specified."));
    292      1.1  christos 
    293      1.1  christos   return program_path.get ();
    294      1.1  christos }
    295      1.1  christos 
    296      1.1  christos /* See server.h.  */
    297      1.1  christos 
    298      1.1  christos gdb_environ *
    299      1.1  christos get_environ ()
    300      1.1  christos {
    301      1.1  christos   return &our_environ;
    302      1.1  christos }
    303      1.1  christos 
    304      1.1  christos static int
    305      1.1  christos attach_inferior (int pid)
    306      1.1  christos {
    307      1.1  christos   client_state &cs = get_client_state ();
    308      1.1  christos   /* myattach should return -1 if attaching is unsupported,
    309      1.1  christos      0 if it succeeded, and call error() otherwise.  */
    310      1.1  christos 
    311      1.1  christos   if (find_process_pid (pid) != nullptr)
    312      1.1  christos     error ("Already attached to process %d\n", pid);
    313      1.1  christos 
    314      1.1  christos   if (myattach (pid) != 0)
    315      1.1  christos     return -1;
    316      1.1  christos 
    317      1.1  christos   fprintf (stderr, "Attached; pid = %d\n", pid);
    318      1.1  christos   fflush (stderr);
    319      1.1  christos 
    320      1.1  christos   /* FIXME - It may be that we should get the SIGNAL_PID from the
    321      1.1  christos      attach function, so that it can be the main thread instead of
    322      1.1  christos      whichever we were told to attach to.  */
    323      1.1  christos   signal_pid = pid;
    324      1.1  christos 
    325      1.1  christos   if (!non_stop)
    326      1.1  christos     {
    327      1.1  christos       cs.last_ptid = mywait (ptid_t (pid), &cs.last_status, 0, 0);
    328      1.1  christos 
    329      1.1  christos       /* GDB knows to ignore the first SIGSTOP after attaching to a running
    330      1.1  christos 	 process using the "attach" command, but this is different; it's
    331      1.1  christos 	 just using "target remote".  Pretend it's just starting up.  */
    332  1.1.1.2  christos       if (cs.last_status.kind () == TARGET_WAITKIND_STOPPED
    333  1.1.1.2  christos 	  && cs.last_status.sig () == GDB_SIGNAL_STOP)
    334  1.1.1.2  christos 	cs.last_status.set_stopped (GDB_SIGNAL_TRAP);
    335      1.1  christos 
    336      1.1  christos       current_thread->last_resume_kind = resume_stop;
    337      1.1  christos       current_thread->last_status = cs.last_status;
    338      1.1  christos     }
    339      1.1  christos 
    340      1.1  christos   return 0;
    341      1.1  christos }
    342      1.1  christos 
    343      1.1  christos /* Decode a qXfer read request.  Return 0 if everything looks OK,
    344      1.1  christos    or -1 otherwise.  */
    345      1.1  christos 
    346      1.1  christos static int
    347      1.1  christos decode_xfer_read (char *buf, CORE_ADDR *ofs, unsigned int *len)
    348      1.1  christos {
    349      1.1  christos   /* After the read marker and annex, qXfer looks like a
    350      1.1  christos      traditional 'm' packet.  */
    351      1.1  christos   decode_m_packet (buf, ofs, len);
    352      1.1  christos 
    353      1.1  christos   return 0;
    354      1.1  christos }
    355      1.1  christos 
    356      1.1  christos static int
    357      1.1  christos decode_xfer (char *buf, char **object, char **rw, char **annex, char **offset)
    358      1.1  christos {
    359      1.1  christos   /* Extract and NUL-terminate the object.  */
    360      1.1  christos   *object = buf;
    361      1.1  christos   while (*buf && *buf != ':')
    362      1.1  christos     buf++;
    363      1.1  christos   if (*buf == '\0')
    364      1.1  christos     return -1;
    365      1.1  christos   *buf++ = 0;
    366      1.1  christos 
    367      1.1  christos   /* Extract and NUL-terminate the read/write action.  */
    368      1.1  christos   *rw = buf;
    369      1.1  christos   while (*buf && *buf != ':')
    370      1.1  christos     buf++;
    371      1.1  christos   if (*buf == '\0')
    372      1.1  christos     return -1;
    373      1.1  christos   *buf++ = 0;
    374      1.1  christos 
    375      1.1  christos   /* Extract and NUL-terminate the annex.  */
    376      1.1  christos   *annex = buf;
    377      1.1  christos   while (*buf && *buf != ':')
    378      1.1  christos     buf++;
    379      1.1  christos   if (*buf == '\0')
    380      1.1  christos     return -1;
    381      1.1  christos   *buf++ = 0;
    382      1.1  christos 
    383      1.1  christos   *offset = buf;
    384      1.1  christos   return 0;
    385      1.1  christos }
    386      1.1  christos 
    387      1.1  christos /* Write the response to a successful qXfer read.  Returns the
    388      1.1  christos    length of the (binary) data stored in BUF, corresponding
    389      1.1  christos    to as much of DATA/LEN as we could fit.  IS_MORE controls
    390      1.1  christos    the first character of the response.  */
    391      1.1  christos static int
    392      1.1  christos write_qxfer_response (char *buf, const gdb_byte *data, int len, int is_more)
    393      1.1  christos {
    394      1.1  christos   int out_len;
    395      1.1  christos 
    396      1.1  christos   if (is_more)
    397      1.1  christos     buf[0] = 'm';
    398      1.1  christos   else
    399      1.1  christos     buf[0] = 'l';
    400      1.1  christos 
    401      1.1  christos   return remote_escape_output (data, len, 1, (unsigned char *) buf + 1,
    402      1.1  christos 			       &out_len, PBUFSIZ - 2) + 1;
    403      1.1  christos }
    404      1.1  christos 
    405      1.1  christos /* Handle btrace enabling in BTS format.  */
    406      1.1  christos 
    407      1.1  christos static void
    408      1.1  christos handle_btrace_enable_bts (struct thread_info *thread)
    409      1.1  christos {
    410      1.1  christos   if (thread->btrace != NULL)
    411      1.1  christos     error (_("Btrace already enabled."));
    412      1.1  christos 
    413      1.1  christos   current_btrace_conf.format = BTRACE_FORMAT_BTS;
    414  1.1.1.2  christos   thread->btrace = target_enable_btrace (thread, &current_btrace_conf);
    415      1.1  christos }
    416      1.1  christos 
    417      1.1  christos /* Handle btrace enabling in Intel Processor Trace format.  */
    418      1.1  christos 
    419      1.1  christos static void
    420      1.1  christos handle_btrace_enable_pt (struct thread_info *thread)
    421      1.1  christos {
    422      1.1  christos   if (thread->btrace != NULL)
    423      1.1  christos     error (_("Btrace already enabled."));
    424      1.1  christos 
    425      1.1  christos   current_btrace_conf.format = BTRACE_FORMAT_PT;
    426  1.1.1.2  christos   thread->btrace = target_enable_btrace (thread, &current_btrace_conf);
    427      1.1  christos }
    428      1.1  christos 
    429      1.1  christos /* Handle btrace disabling.  */
    430      1.1  christos 
    431      1.1  christos static void
    432      1.1  christos handle_btrace_disable (struct thread_info *thread)
    433      1.1  christos {
    434      1.1  christos 
    435      1.1  christos   if (thread->btrace == NULL)
    436      1.1  christos     error (_("Branch tracing not enabled."));
    437      1.1  christos 
    438      1.1  christos   if (target_disable_btrace (thread->btrace) != 0)
    439      1.1  christos     error (_("Could not disable branch tracing."));
    440      1.1  christos 
    441      1.1  christos   thread->btrace = NULL;
    442      1.1  christos }
    443      1.1  christos 
    444      1.1  christos /* Handle the "Qbtrace" packet.  */
    445      1.1  christos 
    446      1.1  christos static int
    447      1.1  christos handle_btrace_general_set (char *own_buf)
    448      1.1  christos {
    449      1.1  christos   client_state &cs = get_client_state ();
    450      1.1  christos   struct thread_info *thread;
    451      1.1  christos   char *op;
    452      1.1  christos 
    453      1.1  christos   if (!startswith (own_buf, "Qbtrace:"))
    454      1.1  christos     return 0;
    455      1.1  christos 
    456      1.1  christos   op = own_buf + strlen ("Qbtrace:");
    457      1.1  christos 
    458      1.1  christos   if (cs.general_thread == null_ptid
    459      1.1  christos       || cs.general_thread == minus_one_ptid)
    460      1.1  christos     {
    461      1.1  christos       strcpy (own_buf, "E.Must select a single thread.");
    462      1.1  christos       return -1;
    463      1.1  christos     }
    464      1.1  christos 
    465      1.1  christos   thread = find_thread_ptid (cs.general_thread);
    466      1.1  christos   if (thread == NULL)
    467      1.1  christos     {
    468      1.1  christos       strcpy (own_buf, "E.No such thread.");
    469      1.1  christos       return -1;
    470      1.1  christos     }
    471      1.1  christos 
    472      1.1  christos   try
    473      1.1  christos     {
    474      1.1  christos       if (strcmp (op, "bts") == 0)
    475      1.1  christos 	handle_btrace_enable_bts (thread);
    476      1.1  christos       else if (strcmp (op, "pt") == 0)
    477      1.1  christos 	handle_btrace_enable_pt (thread);
    478      1.1  christos       else if (strcmp (op, "off") == 0)
    479      1.1  christos 	handle_btrace_disable (thread);
    480      1.1  christos       else
    481      1.1  christos 	error (_("Bad Qbtrace operation.  Use bts, pt, or off."));
    482      1.1  christos 
    483      1.1  christos       write_ok (own_buf);
    484      1.1  christos     }
    485      1.1  christos   catch (const gdb_exception_error &exception)
    486      1.1  christos     {
    487      1.1  christos       sprintf (own_buf, "E.%s", exception.what ());
    488      1.1  christos     }
    489      1.1  christos 
    490      1.1  christos   return 1;
    491      1.1  christos }
    492      1.1  christos 
    493      1.1  christos /* Handle the "Qbtrace-conf" packet.  */
    494      1.1  christos 
    495      1.1  christos static int
    496      1.1  christos handle_btrace_conf_general_set (char *own_buf)
    497      1.1  christos {
    498      1.1  christos   client_state &cs = get_client_state ();
    499      1.1  christos   struct thread_info *thread;
    500      1.1  christos   char *op;
    501      1.1  christos 
    502      1.1  christos   if (!startswith (own_buf, "Qbtrace-conf:"))
    503      1.1  christos     return 0;
    504      1.1  christos 
    505      1.1  christos   op = own_buf + strlen ("Qbtrace-conf:");
    506      1.1  christos 
    507      1.1  christos   if (cs.general_thread == null_ptid
    508      1.1  christos       || cs.general_thread == minus_one_ptid)
    509      1.1  christos     {
    510      1.1  christos       strcpy (own_buf, "E.Must select a single thread.");
    511      1.1  christos       return -1;
    512      1.1  christos     }
    513      1.1  christos 
    514      1.1  christos   thread = find_thread_ptid (cs.general_thread);
    515      1.1  christos   if (thread == NULL)
    516      1.1  christos     {
    517      1.1  christos       strcpy (own_buf, "E.No such thread.");
    518      1.1  christos       return -1;
    519      1.1  christos     }
    520      1.1  christos 
    521      1.1  christos   if (startswith (op, "bts:size="))
    522      1.1  christos     {
    523      1.1  christos       unsigned long size;
    524      1.1  christos       char *endp = NULL;
    525      1.1  christos 
    526      1.1  christos       errno = 0;
    527      1.1  christos       size = strtoul (op + strlen ("bts:size="), &endp, 16);
    528      1.1  christos       if (endp == NULL || *endp != 0 || errno != 0 || size > UINT_MAX)
    529      1.1  christos 	{
    530      1.1  christos 	  strcpy (own_buf, "E.Bad size value.");
    531      1.1  christos 	  return -1;
    532      1.1  christos 	}
    533      1.1  christos 
    534      1.1  christos       current_btrace_conf.bts.size = (unsigned int) size;
    535      1.1  christos     }
    536      1.1  christos   else if (strncmp (op, "pt:size=", strlen ("pt:size=")) == 0)
    537      1.1  christos     {
    538      1.1  christos       unsigned long size;
    539      1.1  christos       char *endp = NULL;
    540      1.1  christos 
    541      1.1  christos       errno = 0;
    542      1.1  christos       size = strtoul (op + strlen ("pt:size="), &endp, 16);
    543      1.1  christos       if (endp == NULL || *endp != 0 || errno != 0 || size > UINT_MAX)
    544      1.1  christos 	{
    545      1.1  christos 	  strcpy (own_buf, "E.Bad size value.");
    546      1.1  christos 	  return -1;
    547      1.1  christos 	}
    548      1.1  christos 
    549      1.1  christos       current_btrace_conf.pt.size = (unsigned int) size;
    550      1.1  christos     }
    551      1.1  christos   else
    552      1.1  christos     {
    553      1.1  christos       strcpy (own_buf, "E.Bad Qbtrace configuration option.");
    554      1.1  christos       return -1;
    555      1.1  christos     }
    556      1.1  christos 
    557      1.1  christos   write_ok (own_buf);
    558      1.1  christos   return 1;
    559      1.1  christos }
    560      1.1  christos 
    561  1.1.1.2  christos /* Create the qMemTags packet reply given TAGS.
    562  1.1.1.2  christos 
    563  1.1.1.2  christos    Returns true if parsing succeeded and false otherwise.  */
    564  1.1.1.2  christos 
    565  1.1.1.2  christos static bool
    566  1.1.1.2  christos create_fetch_memtags_reply (char *reply, const gdb::byte_vector &tags)
    567  1.1.1.2  christos {
    568  1.1.1.2  christos   /* It is an error to pass a zero-sized tag vector.  */
    569  1.1.1.2  christos   gdb_assert (tags.size () != 0);
    570  1.1.1.2  christos 
    571  1.1.1.2  christos   std::string packet ("m");
    572  1.1.1.2  christos 
    573  1.1.1.2  christos   /* Write the tag data.  */
    574  1.1.1.2  christos   packet += bin2hex (tags.data (), tags.size ());
    575  1.1.1.2  christos 
    576  1.1.1.2  christos   /* Check if the reply is too big for the packet to handle.  */
    577  1.1.1.2  christos   if (PBUFSIZ < packet.size ())
    578  1.1.1.2  christos     return false;
    579  1.1.1.2  christos 
    580  1.1.1.2  christos   strcpy (reply, packet.c_str ());
    581  1.1.1.2  christos   return true;
    582  1.1.1.2  christos }
    583  1.1.1.2  christos 
    584  1.1.1.2  christos /* Parse the QMemTags request into ADDR, LEN and TAGS.
    585  1.1.1.2  christos 
    586  1.1.1.2  christos    Returns true if parsing succeeded and false otherwise.  */
    587  1.1.1.2  christos 
    588  1.1.1.2  christos static bool
    589  1.1.1.2  christos parse_store_memtags_request (char *request, CORE_ADDR *addr, size_t *len,
    590  1.1.1.2  christos 			     gdb::byte_vector &tags, int *type)
    591  1.1.1.2  christos {
    592  1.1.1.2  christos   gdb_assert (startswith (request, "QMemTags:"));
    593  1.1.1.2  christos 
    594  1.1.1.2  christos   const char *p = request + strlen ("QMemTags:");
    595  1.1.1.2  christos 
    596  1.1.1.2  christos   /* Read address and length.  */
    597  1.1.1.2  christos   unsigned int length = 0;
    598  1.1.1.2  christos   p = decode_m_packet_params (p, addr, &length, ':');
    599  1.1.1.2  christos   *len = length;
    600  1.1.1.2  christos 
    601  1.1.1.2  christos   /* Read the tag type.  */
    602  1.1.1.2  christos   ULONGEST tag_type = 0;
    603  1.1.1.2  christos   p = unpack_varlen_hex (p, &tag_type);
    604  1.1.1.2  christos   *type = (int) tag_type;
    605  1.1.1.2  christos 
    606  1.1.1.2  christos   /* Make sure there is a colon after the type.  */
    607  1.1.1.2  christos   if (*p != ':')
    608  1.1.1.2  christos     return false;
    609  1.1.1.2  christos 
    610  1.1.1.2  christos   /* Skip the colon.  */
    611  1.1.1.2  christos   p++;
    612  1.1.1.2  christos 
    613  1.1.1.2  christos   /* Read the tag data.  */
    614  1.1.1.2  christos   tags = hex2bin (p);
    615  1.1.1.2  christos 
    616  1.1.1.2  christos   return true;
    617  1.1.1.2  christos }
    618  1.1.1.2  christos 
    619  1.1.1.3  christos /* Parse thread options starting at *P and return them.  On exit,
    620  1.1.1.3  christos    advance *P past the options.  */
    621  1.1.1.3  christos 
    622  1.1.1.3  christos static gdb_thread_options
    623  1.1.1.3  christos parse_gdb_thread_options (const char **p)
    624  1.1.1.3  christos {
    625  1.1.1.3  christos   ULONGEST options = 0;
    626  1.1.1.3  christos   *p = unpack_varlen_hex (*p, &options);
    627  1.1.1.3  christos   return (gdb_thread_option) options;
    628  1.1.1.3  christos }
    629  1.1.1.3  christos 
    630      1.1  christos /* Handle all of the extended 'Q' packets.  */
    631      1.1  christos 
    632      1.1  christos static void
    633      1.1  christos handle_general_set (char *own_buf)
    634      1.1  christos {
    635      1.1  christos   client_state &cs = get_client_state ();
    636      1.1  christos   if (startswith (own_buf, "QPassSignals:"))
    637      1.1  christos     {
    638      1.1  christos       int numsigs = (int) GDB_SIGNAL_LAST, i;
    639      1.1  christos       const char *p = own_buf + strlen ("QPassSignals:");
    640      1.1  christos       CORE_ADDR cursig;
    641      1.1  christos 
    642      1.1  christos       p = decode_address_to_semicolon (&cursig, p);
    643      1.1  christos       for (i = 0; i < numsigs; i++)
    644      1.1  christos 	{
    645      1.1  christos 	  if (i == cursig)
    646      1.1  christos 	    {
    647      1.1  christos 	      cs.pass_signals[i] = 1;
    648      1.1  christos 	      if (*p == '\0')
    649      1.1  christos 		/* Keep looping, to clear the remaining signals.  */
    650      1.1  christos 		cursig = -1;
    651      1.1  christos 	      else
    652      1.1  christos 		p = decode_address_to_semicolon (&cursig, p);
    653      1.1  christos 	    }
    654      1.1  christos 	  else
    655      1.1  christos 	    cs.pass_signals[i] = 0;
    656      1.1  christos 	}
    657      1.1  christos       strcpy (own_buf, "OK");
    658      1.1  christos       return;
    659      1.1  christos     }
    660      1.1  christos 
    661      1.1  christos   if (startswith (own_buf, "QProgramSignals:"))
    662      1.1  christos     {
    663      1.1  christos       int numsigs = (int) GDB_SIGNAL_LAST, i;
    664      1.1  christos       const char *p = own_buf + strlen ("QProgramSignals:");
    665      1.1  christos       CORE_ADDR cursig;
    666      1.1  christos 
    667      1.1  christos       cs.program_signals_p = 1;
    668      1.1  christos 
    669      1.1  christos       p = decode_address_to_semicolon (&cursig, p);
    670      1.1  christos       for (i = 0; i < numsigs; i++)
    671      1.1  christos 	{
    672      1.1  christos 	  if (i == cursig)
    673      1.1  christos 	    {
    674      1.1  christos 	      cs.program_signals[i] = 1;
    675      1.1  christos 	      if (*p == '\0')
    676      1.1  christos 		/* Keep looping, to clear the remaining signals.  */
    677      1.1  christos 		cursig = -1;
    678      1.1  christos 	      else
    679      1.1  christos 		p = decode_address_to_semicolon (&cursig, p);
    680      1.1  christos 	    }
    681      1.1  christos 	  else
    682      1.1  christos 	    cs.program_signals[i] = 0;
    683      1.1  christos 	}
    684      1.1  christos       strcpy (own_buf, "OK");
    685      1.1  christos       return;
    686      1.1  christos     }
    687      1.1  christos 
    688      1.1  christos   if (startswith (own_buf, "QCatchSyscalls:"))
    689      1.1  christos     {
    690      1.1  christos       const char *p = own_buf + sizeof ("QCatchSyscalls:") - 1;
    691      1.1  christos       int enabled = -1;
    692      1.1  christos       CORE_ADDR sysno;
    693      1.1  christos       struct process_info *process;
    694      1.1  christos 
    695      1.1  christos       if (!target_running () || !target_supports_catch_syscall ())
    696      1.1  christos 	{
    697      1.1  christos 	  write_enn (own_buf);
    698      1.1  christos 	  return;
    699      1.1  christos 	}
    700      1.1  christos 
    701      1.1  christos       if (strcmp (p, "0") == 0)
    702      1.1  christos 	enabled = 0;
    703      1.1  christos       else if (p[0] == '1' && (p[1] == ';' || p[1] == '\0'))
    704      1.1  christos 	enabled = 1;
    705      1.1  christos       else
    706      1.1  christos 	{
    707      1.1  christos 	  fprintf (stderr, "Unknown catch-syscalls mode requested: %s\n",
    708      1.1  christos 		   own_buf);
    709      1.1  christos 	  write_enn (own_buf);
    710      1.1  christos 	  return;
    711      1.1  christos 	}
    712      1.1  christos 
    713      1.1  christos       process = current_process ();
    714      1.1  christos       process->syscalls_to_catch.clear ();
    715      1.1  christos 
    716      1.1  christos       if (enabled)
    717      1.1  christos 	{
    718      1.1  christos 	  p += 1;
    719      1.1  christos 	  if (*p == ';')
    720      1.1  christos 	    {
    721      1.1  christos 	      p += 1;
    722      1.1  christos 	      while (*p != '\0')
    723      1.1  christos 		{
    724      1.1  christos 		  p = decode_address_to_semicolon (&sysno, p);
    725      1.1  christos 		  process->syscalls_to_catch.push_back (sysno);
    726      1.1  christos 		}
    727      1.1  christos 	    }
    728      1.1  christos 	  else
    729      1.1  christos 	    process->syscalls_to_catch.push_back (ANY_SYSCALL);
    730      1.1  christos 	}
    731      1.1  christos 
    732      1.1  christos       write_ok (own_buf);
    733      1.1  christos       return;
    734      1.1  christos     }
    735      1.1  christos 
    736      1.1  christos   if (strcmp (own_buf, "QEnvironmentReset") == 0)
    737      1.1  christos     {
    738      1.1  christos       our_environ = gdb_environ::from_host_environ ();
    739      1.1  christos 
    740      1.1  christos       write_ok (own_buf);
    741      1.1  christos       return;
    742      1.1  christos     }
    743      1.1  christos 
    744      1.1  christos   if (startswith (own_buf, "QEnvironmentHexEncoded:"))
    745      1.1  christos     {
    746      1.1  christos       const char *p = own_buf + sizeof ("QEnvironmentHexEncoded:") - 1;
    747      1.1  christos       /* The final form of the environment variable.  FINAL_VAR will
    748      1.1  christos 	 hold the 'VAR=VALUE' format.  */
    749      1.1  christos       std::string final_var = hex2str (p);
    750      1.1  christos       std::string var_name, var_value;
    751      1.1  christos 
    752  1.1.1.2  christos       remote_debug_printf ("[QEnvironmentHexEncoded received '%s']", p);
    753  1.1.1.2  christos       remote_debug_printf ("[Environment variable to be set: '%s']",
    754  1.1.1.2  christos 			   final_var.c_str ());
    755      1.1  christos 
    756      1.1  christos       size_t pos = final_var.find ('=');
    757      1.1  christos       if (pos == std::string::npos)
    758      1.1  christos 	{
    759      1.1  christos 	  warning (_("Unexpected format for environment variable: '%s'"),
    760      1.1  christos 		   final_var.c_str ());
    761      1.1  christos 	  write_enn (own_buf);
    762      1.1  christos 	  return;
    763      1.1  christos 	}
    764      1.1  christos 
    765      1.1  christos       var_name = final_var.substr (0, pos);
    766      1.1  christos       var_value = final_var.substr (pos + 1, std::string::npos);
    767      1.1  christos 
    768      1.1  christos       our_environ.set (var_name.c_str (), var_value.c_str ());
    769      1.1  christos 
    770      1.1  christos       write_ok (own_buf);
    771      1.1  christos       return;
    772      1.1  christos     }
    773      1.1  christos 
    774      1.1  christos   if (startswith (own_buf, "QEnvironmentUnset:"))
    775      1.1  christos     {
    776      1.1  christos       const char *p = own_buf + sizeof ("QEnvironmentUnset:") - 1;
    777      1.1  christos       std::string varname = hex2str (p);
    778      1.1  christos 
    779  1.1.1.2  christos       remote_debug_printf ("[QEnvironmentUnset received '%s']", p);
    780  1.1.1.2  christos       remote_debug_printf ("[Environment variable to be unset: '%s']",
    781  1.1.1.2  christos 			   varname.c_str ());
    782      1.1  christos 
    783      1.1  christos       our_environ.unset (varname.c_str ());
    784      1.1  christos 
    785      1.1  christos       write_ok (own_buf);
    786      1.1  christos       return;
    787      1.1  christos     }
    788      1.1  christos 
    789      1.1  christos   if (strcmp (own_buf, "QStartNoAckMode") == 0)
    790      1.1  christos     {
    791  1.1.1.2  christos       remote_debug_printf ("[noack mode enabled]");
    792      1.1  christos 
    793      1.1  christos       cs.noack_mode = 1;
    794      1.1  christos       write_ok (own_buf);
    795      1.1  christos       return;
    796      1.1  christos     }
    797      1.1  christos 
    798      1.1  christos   if (startswith (own_buf, "QNonStop:"))
    799      1.1  christos     {
    800      1.1  christos       char *mode = own_buf + 9;
    801      1.1  christos       int req = -1;
    802      1.1  christos       const char *req_str;
    803      1.1  christos 
    804      1.1  christos       if (strcmp (mode, "0") == 0)
    805      1.1  christos 	req = 0;
    806      1.1  christos       else if (strcmp (mode, "1") == 0)
    807      1.1  christos 	req = 1;
    808      1.1  christos       else
    809      1.1  christos 	{
    810      1.1  christos 	  /* We don't know what this mode is, so complain to
    811      1.1  christos 	     GDB.  */
    812      1.1  christos 	  fprintf (stderr, "Unknown non-stop mode requested: %s\n",
    813      1.1  christos 		   own_buf);
    814      1.1  christos 	  write_enn (own_buf);
    815      1.1  christos 	  return;
    816      1.1  christos 	}
    817      1.1  christos 
    818      1.1  christos       req_str = req ? "non-stop" : "all-stop";
    819      1.1  christos       if (the_target->start_non_stop (req == 1) != 0)
    820      1.1  christos 	{
    821      1.1  christos 	  fprintf (stderr, "Setting %s mode failed\n", req_str);
    822      1.1  christos 	  write_enn (own_buf);
    823      1.1  christos 	  return;
    824      1.1  christos 	}
    825      1.1  christos 
    826      1.1  christos       non_stop = (req != 0);
    827      1.1  christos 
    828  1.1.1.2  christos       remote_debug_printf ("[%s mode enabled]", req_str);
    829      1.1  christos 
    830      1.1  christos       write_ok (own_buf);
    831      1.1  christos       return;
    832      1.1  christos     }
    833      1.1  christos 
    834      1.1  christos   if (startswith (own_buf, "QDisableRandomization:"))
    835      1.1  christos     {
    836      1.1  christos       char *packet = own_buf + strlen ("QDisableRandomization:");
    837      1.1  christos       ULONGEST setting;
    838      1.1  christos 
    839      1.1  christos       unpack_varlen_hex (packet, &setting);
    840      1.1  christos       cs.disable_randomization = setting;
    841      1.1  christos 
    842  1.1.1.2  christos       remote_debug_printf (cs.disable_randomization
    843  1.1.1.2  christos 			   ? "[address space randomization disabled]"
    844  1.1.1.2  christos 			       : "[address space randomization enabled]");
    845      1.1  christos 
    846      1.1  christos       write_ok (own_buf);
    847      1.1  christos       return;
    848      1.1  christos     }
    849      1.1  christos 
    850      1.1  christos   if (target_supports_tracepoints ()
    851      1.1  christos       && handle_tracepoint_general_set (own_buf))
    852      1.1  christos     return;
    853      1.1  christos 
    854      1.1  christos   if (startswith (own_buf, "QAgent:"))
    855      1.1  christos     {
    856      1.1  christos       char *mode = own_buf + strlen ("QAgent:");
    857      1.1  christos       int req = 0;
    858      1.1  christos 
    859      1.1  christos       if (strcmp (mode, "0") == 0)
    860      1.1  christos 	req = 0;
    861      1.1  christos       else if (strcmp (mode, "1") == 0)
    862      1.1  christos 	req = 1;
    863      1.1  christos       else
    864      1.1  christos 	{
    865      1.1  christos 	  /* We don't know what this value is, so complain to GDB.  */
    866      1.1  christos 	  sprintf (own_buf, "E.Unknown QAgent value");
    867      1.1  christos 	  return;
    868      1.1  christos 	}
    869      1.1  christos 
    870      1.1  christos       /* Update the flag.  */
    871      1.1  christos       use_agent = req;
    872  1.1.1.2  christos       remote_debug_printf ("[%s agent]", req ? "Enable" : "Disable");
    873      1.1  christos       write_ok (own_buf);
    874      1.1  christos       return;
    875      1.1  christos     }
    876      1.1  christos 
    877      1.1  christos   if (handle_btrace_general_set (own_buf))
    878      1.1  christos     return;
    879      1.1  christos 
    880      1.1  christos   if (handle_btrace_conf_general_set (own_buf))
    881      1.1  christos     return;
    882      1.1  christos 
    883      1.1  christos   if (startswith (own_buf, "QThreadEvents:"))
    884      1.1  christos     {
    885      1.1  christos       char *mode = own_buf + strlen ("QThreadEvents:");
    886      1.1  christos       enum tribool req = TRIBOOL_UNKNOWN;
    887      1.1  christos 
    888      1.1  christos       if (strcmp (mode, "0") == 0)
    889      1.1  christos 	req = TRIBOOL_FALSE;
    890      1.1  christos       else if (strcmp (mode, "1") == 0)
    891      1.1  christos 	req = TRIBOOL_TRUE;
    892      1.1  christos       else
    893      1.1  christos 	{
    894      1.1  christos 	  /* We don't know what this mode is, so complain to GDB.  */
    895  1.1.1.2  christos 	  std::string err
    896  1.1.1.2  christos 	    = string_printf ("E.Unknown thread-events mode requested: %s\n",
    897  1.1.1.2  christos 			     mode);
    898  1.1.1.2  christos 	  strcpy (own_buf, err.c_str ());
    899      1.1  christos 	  return;
    900      1.1  christos 	}
    901      1.1  christos 
    902      1.1  christos       cs.report_thread_events = (req == TRIBOOL_TRUE);
    903      1.1  christos 
    904  1.1.1.2  christos       remote_debug_printf ("[thread events are now %s]\n",
    905  1.1.1.2  christos 			   cs.report_thread_events ? "enabled" : "disabled");
    906      1.1  christos 
    907      1.1  christos       write_ok (own_buf);
    908      1.1  christos       return;
    909      1.1  christos     }
    910      1.1  christos 
    911  1.1.1.3  christos   if (startswith (own_buf, "QThreadOptions;"))
    912  1.1.1.3  christos     {
    913  1.1.1.3  christos       const char *p = own_buf + strlen ("QThreadOptions");
    914  1.1.1.3  christos 
    915  1.1.1.3  christos       gdb_thread_options supported_options = target_supported_thread_options ();
    916  1.1.1.3  christos       if (supported_options == 0)
    917  1.1.1.3  christos 	{
    918  1.1.1.3  christos 	  /* Something went wrong -- we don't support any option, but
    919  1.1.1.3  christos 	     GDB sent the packet anyway.  */
    920  1.1.1.3  christos 	  write_enn (own_buf);
    921  1.1.1.3  christos 	  return;
    922  1.1.1.3  christos 	}
    923  1.1.1.3  christos 
    924  1.1.1.3  christos       /* We could store the options directly in thread->thread_options
    925  1.1.1.3  christos 	 without this map, but that would mean that a QThreadOptions
    926  1.1.1.3  christos 	 packet with a wildcard like "QThreadOptions;0;3:TID" would
    927  1.1.1.3  christos 	 result in the debug logs showing:
    928  1.1.1.3  christos 
    929  1.1.1.3  christos 	   [options for TID are now 0x0]
    930  1.1.1.3  christos 	   [options for TID are now 0x3]
    931  1.1.1.3  christos 
    932  1.1.1.3  christos 	 It's nicer if we only print the final options for each TID,
    933  1.1.1.3  christos 	 and if we only print about it if the options changed compared
    934  1.1.1.3  christos 	 to the options that were previously set on the thread.  */
    935  1.1.1.3  christos       std::unordered_map<thread_info *, gdb_thread_options> set_options;
    936  1.1.1.3  christos 
    937  1.1.1.3  christos       while (*p != '\0')
    938  1.1.1.3  christos 	{
    939  1.1.1.3  christos 	  if (p[0] != ';')
    940  1.1.1.3  christos 	    {
    941  1.1.1.3  christos 	      write_enn (own_buf);
    942  1.1.1.3  christos 	      return;
    943  1.1.1.3  christos 	    }
    944  1.1.1.3  christos 	  p++;
    945  1.1.1.3  christos 
    946  1.1.1.3  christos 	  /* Read the options.  */
    947  1.1.1.3  christos 
    948  1.1.1.3  christos 	  gdb_thread_options options = parse_gdb_thread_options (&p);
    949  1.1.1.3  christos 
    950  1.1.1.3  christos 	  if ((options & ~supported_options) != 0)
    951  1.1.1.3  christos 	    {
    952  1.1.1.3  christos #if 0
    953  1.1.1.3  christos 	      // XXX: see undefined
    954  1.1.1.3  christos 	      /* GDB asked for an unknown or unsupported option, so
    955  1.1.1.3  christos 		 error out.  */
    956  1.1.1.3  christos 	      std::string err
    957  1.1.1.3  christos 		= string_printf ("E.Unknown thread options requested: %s\n",
    958  1.1.1.3  christos 				 to_string (options).c_str ());
    959  1.1.1.3  christos 	      strcpy (own_buf, err.c_str ());
    960  1.1.1.3  christos #else
    961  1.1.1.3  christos 	      strcpy (own_buf, "unsuppported option");
    962  1.1.1.3  christos #endif
    963  1.1.1.3  christos 	      return;
    964  1.1.1.3  christos 	    }
    965  1.1.1.3  christos 
    966  1.1.1.3  christos 	  ptid_t ptid;
    967  1.1.1.3  christos 
    968  1.1.1.3  christos 	  if (p[0] == ';' || p[0] == '\0')
    969  1.1.1.3  christos 	    ptid = minus_one_ptid;
    970  1.1.1.3  christos 	  else if (p[0] == ':')
    971  1.1.1.3  christos 	    {
    972  1.1.1.3  christos 	      const char *q;
    973  1.1.1.3  christos 
    974  1.1.1.3  christos 	      ptid = read_ptid (p + 1, &q);
    975  1.1.1.3  christos 
    976  1.1.1.3  christos 	      if (p == q)
    977  1.1.1.3  christos 		{
    978  1.1.1.3  christos 		  write_enn (own_buf);
    979  1.1.1.3  christos 		  return;
    980  1.1.1.3  christos 		}
    981  1.1.1.3  christos 	      p = q;
    982  1.1.1.3  christos 	      if (p[0] != ';' && p[0] != '\0')
    983  1.1.1.3  christos 		{
    984  1.1.1.3  christos 		  write_enn (own_buf);
    985  1.1.1.3  christos 		  return;
    986  1.1.1.3  christos 		}
    987  1.1.1.3  christos 	    }
    988  1.1.1.3  christos 	  else
    989  1.1.1.3  christos 	    {
    990  1.1.1.3  christos 	      write_enn (own_buf);
    991  1.1.1.3  christos 	      return;
    992  1.1.1.3  christos 	    }
    993  1.1.1.3  christos 
    994  1.1.1.3  christos 	  /* Convert PID.-1 => PID.0 for ptid.matches.  */
    995  1.1.1.3  christos 	  if (ptid.lwp () == -1)
    996  1.1.1.3  christos 	    ptid = ptid_t (ptid.pid ());
    997  1.1.1.3  christos 
    998  1.1.1.3  christos 	  for_each_thread ([&] (thread_info *thread)
    999  1.1.1.3  christos 	    {
   1000  1.1.1.3  christos 	      if (ptid_of (thread).matches (ptid))
   1001  1.1.1.3  christos 		set_options[thread] = options;
   1002  1.1.1.3  christos 	    });
   1003  1.1.1.3  christos 	}
   1004  1.1.1.3  christos 
   1005  1.1.1.3  christos       for (const auto &iter : set_options)
   1006  1.1.1.3  christos 	{
   1007  1.1.1.3  christos 	  thread_info *thread = iter.first;
   1008  1.1.1.3  christos 	  gdb_thread_options options = iter.second;
   1009  1.1.1.3  christos 
   1010  1.1.1.3  christos 	  if (thread->thread_options != options)
   1011  1.1.1.3  christos 	    {
   1012  1.1.1.3  christos #if 0
   1013  1.1.1.3  christos 	      // XXX: undefined reference to
   1014  1.1.1.3  christos 	      // `to_string[abi:cxx11](enum_flags<gdb_thread_option>)'
   1015  1.1.1.3  christos 	      threads_debug_printf ("[options for %s are now %s]\n",
   1016  1.1.1.3  christos 				    target_pid_to_str (ptid_of (thread)).c_str (),
   1017  1.1.1.3  christos 				    to_string (options).c_str ());
   1018  1.1.1.3  christos #endif
   1019  1.1.1.3  christos 
   1020  1.1.1.3  christos 	      thread->thread_options = options;
   1021  1.1.1.3  christos 	    }
   1022  1.1.1.3  christos 	}
   1023  1.1.1.3  christos 
   1024  1.1.1.3  christos       write_ok (own_buf);
   1025  1.1.1.3  christos       return;
   1026  1.1.1.3  christos     }
   1027  1.1.1.3  christos 
   1028      1.1  christos   if (startswith (own_buf, "QStartupWithShell:"))
   1029      1.1  christos     {
   1030      1.1  christos       const char *value = own_buf + strlen ("QStartupWithShell:");
   1031      1.1  christos 
   1032      1.1  christos       if (strcmp (value, "1") == 0)
   1033      1.1  christos 	startup_with_shell = true;
   1034      1.1  christos       else if (strcmp (value, "0") == 0)
   1035      1.1  christos 	startup_with_shell = false;
   1036      1.1  christos       else
   1037      1.1  christos 	{
   1038      1.1  christos 	  /* Unknown value.  */
   1039      1.1  christos 	  fprintf (stderr, "Unknown value to startup-with-shell: %s\n",
   1040      1.1  christos 		   own_buf);
   1041      1.1  christos 	  write_enn (own_buf);
   1042      1.1  christos 	  return;
   1043      1.1  christos 	}
   1044      1.1  christos 
   1045  1.1.1.2  christos       remote_debug_printf ("[Inferior will %s started with shell]",
   1046  1.1.1.2  christos 			   startup_with_shell ? "be" : "not be");
   1047      1.1  christos 
   1048      1.1  christos       write_ok (own_buf);
   1049      1.1  christos       return;
   1050      1.1  christos     }
   1051      1.1  christos 
   1052      1.1  christos   if (startswith (own_buf, "QSetWorkingDir:"))
   1053      1.1  christos     {
   1054      1.1  christos       const char *p = own_buf + strlen ("QSetWorkingDir:");
   1055      1.1  christos 
   1056      1.1  christos       if (*p != '\0')
   1057      1.1  christos 	{
   1058      1.1  christos 	  std::string path = hex2str (p);
   1059      1.1  christos 
   1060  1.1.1.2  christos 	  remote_debug_printf ("[Set the inferior's current directory to %s]",
   1061  1.1.1.2  christos 			       path.c_str ());
   1062      1.1  christos 
   1063  1.1.1.2  christos 	  set_inferior_cwd (std::move (path));
   1064      1.1  christos 	}
   1065      1.1  christos       else
   1066      1.1  christos 	{
   1067      1.1  christos 	  /* An empty argument means that we should clear out any
   1068      1.1  christos 	     previously set cwd for the inferior.  */
   1069  1.1.1.2  christos 	  set_inferior_cwd ("");
   1070      1.1  christos 
   1071  1.1.1.2  christos 	  remote_debug_printf ("[Unset the inferior's current directory; will "
   1072  1.1.1.2  christos 			       "use gdbserver's cwd]");
   1073      1.1  christos 	}
   1074      1.1  christos       write_ok (own_buf);
   1075      1.1  christos 
   1076      1.1  christos       return;
   1077      1.1  christos     }
   1078      1.1  christos 
   1079  1.1.1.2  christos 
   1080  1.1.1.2  christos   /* Handle store memory tags packets.  */
   1081  1.1.1.2  christos   if (startswith (own_buf, "QMemTags:")
   1082  1.1.1.2  christos       && target_supports_memory_tagging ())
   1083  1.1.1.2  christos     {
   1084  1.1.1.2  christos       gdb::byte_vector tags;
   1085  1.1.1.2  christos       CORE_ADDR addr = 0;
   1086  1.1.1.2  christos       size_t len = 0;
   1087  1.1.1.2  christos       int type = 0;
   1088  1.1.1.2  christos 
   1089  1.1.1.2  christos       require_running_or_return (own_buf);
   1090  1.1.1.2  christos 
   1091  1.1.1.2  christos       bool ret = parse_store_memtags_request (own_buf, &addr, &len, tags,
   1092  1.1.1.2  christos 					     &type);
   1093  1.1.1.2  christos 
   1094  1.1.1.2  christos       if (ret)
   1095  1.1.1.2  christos 	ret = the_target->store_memtags (addr, len, tags, type);
   1096  1.1.1.2  christos 
   1097  1.1.1.2  christos       if (!ret)
   1098  1.1.1.2  christos 	write_enn (own_buf);
   1099  1.1.1.2  christos       else
   1100  1.1.1.2  christos 	write_ok (own_buf);
   1101  1.1.1.2  christos 
   1102  1.1.1.2  christos       return;
   1103  1.1.1.2  christos     }
   1104  1.1.1.2  christos 
   1105      1.1  christos   /* Otherwise we didn't know what packet it was.  Say we didn't
   1106      1.1  christos      understand it.  */
   1107      1.1  christos   own_buf[0] = 0;
   1108      1.1  christos }
   1109      1.1  christos 
   1110      1.1  christos static const char *
   1111      1.1  christos get_features_xml (const char *annex)
   1112      1.1  christos {
   1113      1.1  christos   const struct target_desc *desc = current_target_desc ();
   1114      1.1  christos 
   1115      1.1  christos   /* `desc->xmltarget' defines what to return when looking for the
   1116      1.1  christos      "target.xml" file.  Its contents can either be verbatim XML code
   1117      1.1  christos      (prefixed with a '@') or else the name of the actual XML file to
   1118      1.1  christos      be used in place of "target.xml".
   1119      1.1  christos 
   1120      1.1  christos      This variable is set up from the auto-generated
   1121      1.1  christos      init_registers_... routine for the current target.  */
   1122      1.1  christos 
   1123      1.1  christos   if (strcmp (annex, "target.xml") == 0)
   1124      1.1  christos     {
   1125      1.1  christos       const char *ret = tdesc_get_features_xml (desc);
   1126      1.1  christos 
   1127      1.1  christos       if (*ret == '@')
   1128      1.1  christos 	return ret + 1;
   1129      1.1  christos       else
   1130      1.1  christos 	annex = ret;
   1131      1.1  christos     }
   1132      1.1  christos 
   1133      1.1  christos #ifdef USE_XML
   1134      1.1  christos   {
   1135      1.1  christos     int i;
   1136      1.1  christos 
   1137      1.1  christos     /* Look for the annex.  */
   1138      1.1  christos     for (i = 0; xml_builtin[i][0] != NULL; i++)
   1139      1.1  christos       if (strcmp (annex, xml_builtin[i][0]) == 0)
   1140      1.1  christos 	break;
   1141      1.1  christos 
   1142      1.1  christos     if (xml_builtin[i][0] != NULL)
   1143      1.1  christos       return xml_builtin[i][1];
   1144      1.1  christos   }
   1145      1.1  christos #endif
   1146      1.1  christos 
   1147      1.1  christos   return NULL;
   1148      1.1  christos }
   1149      1.1  christos 
   1150      1.1  christos static void
   1151      1.1  christos monitor_show_help (void)
   1152      1.1  christos {
   1153      1.1  christos   monitor_output ("The following monitor commands are supported:\n");
   1154  1.1.1.3  christos   monitor_output ("  set debug on\n");
   1155      1.1  christos   monitor_output ("    Enable general debugging messages\n");
   1156  1.1.1.3  christos   monitor_output ("  set debug off\n");
   1157  1.1.1.3  christos   monitor_output ("    Disable all debugging messages\n");
   1158  1.1.1.3  christos   monitor_output ("  set debug COMPONENT <off|on>\n");
   1159  1.1.1.3  christos   monitor_output ("    Enable debugging messages for COMPONENT, which is\n");
   1160  1.1.1.3  christos   monitor_output ("    one of: all, threads, remote, event-loop.\n");
   1161      1.1  christos   monitor_output ("  set debug-hw-points <0|1>\n");
   1162      1.1  christos   monitor_output ("    Enable h/w breakpoint/watchpoint debugging messages\n");
   1163      1.1  christos   monitor_output ("  set debug-format option1[,option2,...]\n");
   1164      1.1  christos   monitor_output ("    Add additional information to debugging messages\n");
   1165  1.1.1.3  christos   monitor_output ("    Options: all, none, timestamp\n");
   1166      1.1  christos   monitor_output ("  exit\n");
   1167      1.1  christos   monitor_output ("    Quit GDBserver\n");
   1168      1.1  christos }
   1169      1.1  christos 
   1170      1.1  christos /* Read trace frame or inferior memory.  Returns the number of bytes
   1171      1.1  christos    actually read, zero when no further transfer is possible, and -1 on
   1172      1.1  christos    error.  Return of a positive value smaller than LEN does not
   1173      1.1  christos    indicate there's no more to be read, only the end of the transfer.
   1174      1.1  christos    E.g., when GDB reads memory from a traceframe, a first request may
   1175      1.1  christos    be served from a memory block that does not cover the whole request
   1176      1.1  christos    length.  A following request gets the rest served from either
   1177      1.1  christos    another block (of the same traceframe) or from the read-only
   1178      1.1  christos    regions.  */
   1179      1.1  christos 
   1180      1.1  christos static int
   1181      1.1  christos gdb_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
   1182      1.1  christos {
   1183      1.1  christos   client_state &cs = get_client_state ();
   1184      1.1  christos   int res;
   1185      1.1  christos 
   1186      1.1  christos   if (cs.current_traceframe >= 0)
   1187      1.1  christos     {
   1188      1.1  christos       ULONGEST nbytes;
   1189      1.1  christos       ULONGEST length = len;
   1190      1.1  christos 
   1191      1.1  christos       if (traceframe_read_mem (cs.current_traceframe,
   1192      1.1  christos 			       memaddr, myaddr, len, &nbytes))
   1193      1.1  christos 	return -1;
   1194      1.1  christos       /* Data read from trace buffer, we're done.  */
   1195      1.1  christos       if (nbytes > 0)
   1196      1.1  christos 	return nbytes;
   1197      1.1  christos       if (!in_readonly_region (memaddr, length))
   1198      1.1  christos 	return -1;
   1199      1.1  christos       /* Otherwise we have a valid readonly case, fall through.  */
   1200      1.1  christos       /* (assume no half-trace half-real blocks for now) */
   1201      1.1  christos     }
   1202      1.1  christos 
   1203  1.1.1.2  christos   if (set_desired_process ())
   1204  1.1.1.2  christos     res = read_inferior_memory (memaddr, myaddr, len);
   1205      1.1  christos   else
   1206  1.1.1.2  christos     res = 1;
   1207  1.1.1.2  christos 
   1208  1.1.1.2  christos   return res == 0 ? len : -1;
   1209      1.1  christos }
   1210      1.1  christos 
   1211      1.1  christos /* Write trace frame or inferior memory.  Actually, writing to trace
   1212      1.1  christos    frames is forbidden.  */
   1213      1.1  christos 
   1214      1.1  christos static int
   1215      1.1  christos gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
   1216      1.1  christos {
   1217      1.1  christos   client_state &cs = get_client_state ();
   1218      1.1  christos   if (cs.current_traceframe >= 0)
   1219      1.1  christos     return EIO;
   1220      1.1  christos   else
   1221      1.1  christos     {
   1222      1.1  christos       int ret;
   1223      1.1  christos 
   1224  1.1.1.2  christos       if (set_desired_process ())
   1225  1.1.1.2  christos 	ret = target_write_memory (memaddr, myaddr, len);
   1226      1.1  christos       else
   1227  1.1.1.2  christos 	ret = EIO;
   1228  1.1.1.2  christos       return ret;
   1229      1.1  christos     }
   1230      1.1  christos }
   1231      1.1  christos 
   1232      1.1  christos /* Handle qSearch:memory packets.  */
   1233      1.1  christos 
   1234      1.1  christos static void
   1235      1.1  christos handle_search_memory (char *own_buf, int packet_len)
   1236      1.1  christos {
   1237      1.1  christos   CORE_ADDR start_addr;
   1238      1.1  christos   CORE_ADDR search_space_len;
   1239      1.1  christos   gdb_byte *pattern;
   1240      1.1  christos   unsigned int pattern_len;
   1241      1.1  christos   int found;
   1242      1.1  christos   CORE_ADDR found_addr;
   1243      1.1  christos   int cmd_name_len = sizeof ("qSearch:memory:") - 1;
   1244      1.1  christos 
   1245      1.1  christos   pattern = (gdb_byte *) malloc (packet_len);
   1246      1.1  christos   if (pattern == NULL)
   1247  1.1.1.2  christos     error ("Unable to allocate memory to perform the search");
   1248  1.1.1.2  christos 
   1249      1.1  christos   if (decode_search_memory_packet (own_buf + cmd_name_len,
   1250      1.1  christos 				   packet_len - cmd_name_len,
   1251      1.1  christos 				   &start_addr, &search_space_len,
   1252      1.1  christos 				   pattern, &pattern_len) < 0)
   1253      1.1  christos     {
   1254      1.1  christos       free (pattern);
   1255      1.1  christos       error ("Error in parsing qSearch:memory packet");
   1256      1.1  christos     }
   1257      1.1  christos 
   1258  1.1.1.2  christos   auto read_memory = [] (CORE_ADDR addr, gdb_byte *result, size_t len)
   1259      1.1  christos     {
   1260  1.1.1.2  christos       return gdb_read_memory (addr, result, len) == len;
   1261  1.1.1.2  christos     };
   1262      1.1  christos 
   1263  1.1.1.2  christos   found = simple_search_memory (read_memory, start_addr, search_space_len,
   1264  1.1.1.2  christos 				pattern, pattern_len, &found_addr);
   1265      1.1  christos 
   1266      1.1  christos   if (found > 0)
   1267      1.1  christos     sprintf (own_buf, "1,%lx", (long) found_addr);
   1268      1.1  christos   else if (found == 0)
   1269      1.1  christos     strcpy (own_buf, "0");
   1270      1.1  christos   else
   1271      1.1  christos     strcpy (own_buf, "E00");
   1272      1.1  christos 
   1273      1.1  christos   free (pattern);
   1274      1.1  christos }
   1275      1.1  christos 
   1276      1.1  christos /* Handle the "D" packet.  */
   1277      1.1  christos 
   1278      1.1  christos static void
   1279      1.1  christos handle_detach (char *own_buf)
   1280      1.1  christos {
   1281      1.1  christos   client_state &cs = get_client_state ();
   1282      1.1  christos 
   1283      1.1  christos   process_info *process;
   1284      1.1  christos 
   1285      1.1  christos   if (cs.multi_process)
   1286      1.1  christos     {
   1287      1.1  christos       /* skip 'D;' */
   1288      1.1  christos       int pid = strtol (&own_buf[2], NULL, 16);
   1289      1.1  christos 
   1290      1.1  christos       process = find_process_pid (pid);
   1291      1.1  christos     }
   1292      1.1  christos   else
   1293      1.1  christos     {
   1294      1.1  christos       process = (current_thread != nullptr
   1295      1.1  christos 		 ? get_thread_process (current_thread)
   1296      1.1  christos 		 : nullptr);
   1297      1.1  christos     }
   1298      1.1  christos 
   1299      1.1  christos   if (process == NULL)
   1300      1.1  christos     {
   1301      1.1  christos       write_enn (own_buf);
   1302      1.1  christos       return;
   1303      1.1  christos     }
   1304      1.1  christos 
   1305      1.1  christos   if ((tracing && disconnected_tracing) || any_persistent_commands (process))
   1306      1.1  christos     {
   1307      1.1  christos       if (tracing && disconnected_tracing)
   1308      1.1  christos 	fprintf (stderr,
   1309      1.1  christos 		 "Disconnected tracing in effect, "
   1310      1.1  christos 		 "leaving gdbserver attached to the process\n");
   1311      1.1  christos 
   1312      1.1  christos       if (any_persistent_commands (process))
   1313      1.1  christos 	fprintf (stderr,
   1314      1.1  christos 		 "Persistent commands are present, "
   1315      1.1  christos 		 "leaving gdbserver attached to the process\n");
   1316      1.1  christos 
   1317      1.1  christos       /* Make sure we're in non-stop/async mode, so we we can both
   1318      1.1  christos 	 wait for an async socket accept, and handle async target
   1319      1.1  christos 	 events simultaneously.  There's also no point either in
   1320      1.1  christos 	 having the target stop all threads, when we're going to
   1321      1.1  christos 	 pass signals down without informing GDB.  */
   1322      1.1  christos       if (!non_stop)
   1323      1.1  christos 	{
   1324  1.1.1.2  christos 	  threads_debug_printf ("Forcing non-stop mode");
   1325      1.1  christos 
   1326      1.1  christos 	  non_stop = true;
   1327      1.1  christos 	  the_target->start_non_stop (true);
   1328      1.1  christos 	}
   1329      1.1  christos 
   1330      1.1  christos       process->gdb_detached = 1;
   1331      1.1  christos 
   1332      1.1  christos       /* Detaching implicitly resumes all threads.  */
   1333      1.1  christos       target_continue_no_signal (minus_one_ptid);
   1334      1.1  christos 
   1335      1.1  christos       write_ok (own_buf);
   1336      1.1  christos       return;
   1337      1.1  christos     }
   1338      1.1  christos 
   1339      1.1  christos   fprintf (stderr, "Detaching from process %d\n", process->pid);
   1340      1.1  christos   stop_tracing ();
   1341      1.1  christos 
   1342      1.1  christos   /* We'll need this after PROCESS has been destroyed.  */
   1343      1.1  christos   int pid = process->pid;
   1344      1.1  christos 
   1345  1.1.1.2  christos   /* If this process has an unreported fork child, that child is not known to
   1346  1.1.1.2  christos      GDB, so GDB won't take care of detaching it.  We must do it here.
   1347  1.1.1.2  christos 
   1348  1.1.1.2  christos      Here, we specifically don't want to use "safe iteration", as detaching
   1349  1.1.1.2  christos      another process might delete the next thread in the iteration, which is
   1350  1.1.1.2  christos      the one saved by the safe iterator.  We will never delete the currently
   1351  1.1.1.2  christos      iterated on thread, so standard iteration should be safe.  */
   1352  1.1.1.2  christos   for (thread_info *thread : all_threads)
   1353  1.1.1.2  christos     {
   1354  1.1.1.2  christos       /* Only threads that are of the process we are detaching.  */
   1355  1.1.1.2  christos       if (thread->id.pid () != pid)
   1356  1.1.1.2  christos 	continue;
   1357  1.1.1.2  christos 
   1358  1.1.1.2  christos       /* Only threads that have a pending fork event.  */
   1359  1.1.1.3  christos       target_waitkind kind;
   1360  1.1.1.3  christos       thread_info *child = target_thread_pending_child (thread, &kind);
   1361  1.1.1.3  christos       if (child == nullptr || kind == TARGET_WAITKIND_THREAD_CLONED)
   1362  1.1.1.2  christos 	continue;
   1363  1.1.1.2  christos 
   1364  1.1.1.2  christos       process_info *fork_child_process = get_thread_process (child);
   1365  1.1.1.2  christos       gdb_assert (fork_child_process != nullptr);
   1366  1.1.1.2  christos 
   1367  1.1.1.2  christos       int fork_child_pid = fork_child_process->pid;
   1368  1.1.1.2  christos 
   1369  1.1.1.2  christos       if (detach_inferior (fork_child_process) != 0)
   1370  1.1.1.2  christos 	warning (_("Failed to detach fork child %s, child of %s"),
   1371  1.1.1.2  christos 		 target_pid_to_str (ptid_t (fork_child_pid)).c_str (),
   1372  1.1.1.2  christos 		 target_pid_to_str (thread->id).c_str ());
   1373  1.1.1.2  christos     }
   1374  1.1.1.2  christos 
   1375      1.1  christos   if (detach_inferior (process) != 0)
   1376      1.1  christos     write_enn (own_buf);
   1377      1.1  christos   else
   1378      1.1  christos     {
   1379      1.1  christos       discard_queued_stop_replies (ptid_t (pid));
   1380      1.1  christos       write_ok (own_buf);
   1381      1.1  christos 
   1382      1.1  christos       if (extended_protocol || target_running ())
   1383      1.1  christos 	{
   1384      1.1  christos 	  /* There is still at least one inferior remaining or
   1385      1.1  christos 	     we are in extended mode, so don't terminate gdbserver,
   1386      1.1  christos 	     and instead treat this like a normal program exit.  */
   1387  1.1.1.2  christos 	  cs.last_status.set_exited (0);
   1388      1.1  christos 	  cs.last_ptid = ptid_t (pid);
   1389      1.1  christos 
   1390  1.1.1.2  christos 	  switch_to_thread (nullptr);
   1391      1.1  christos 	}
   1392      1.1  christos       else
   1393      1.1  christos 	{
   1394      1.1  christos 	  putpkt (own_buf);
   1395      1.1  christos 	  remote_close ();
   1396      1.1  christos 
   1397      1.1  christos 	  /* If we are attached, then we can exit.  Otherwise, we
   1398      1.1  christos 	     need to hang around doing nothing, until the child is
   1399      1.1  christos 	     gone.  */
   1400      1.1  christos 	  join_inferior (pid);
   1401      1.1  christos 	  exit (0);
   1402      1.1  christos 	}
   1403      1.1  christos     }
   1404      1.1  christos }
   1405      1.1  christos 
   1406      1.1  christos /* Parse options to --debug-format= and "monitor set debug-format".
   1407      1.1  christos    ARG is the text after "--debug-format=" or "monitor set debug-format".
   1408      1.1  christos    IS_MONITOR is non-zero if we're invoked via "monitor set debug-format".
   1409      1.1  christos    This triggers calls to monitor_output.
   1410      1.1  christos    The result is an empty string if all options were parsed ok, otherwise an
   1411      1.1  christos    error message which the caller must free.
   1412      1.1  christos 
   1413      1.1  christos    N.B. These commands affect all debug format settings, they are not
   1414      1.1  christos    cumulative.  If a format is not specified, it is turned off.
   1415      1.1  christos    However, we don't go to extra trouble with things like
   1416      1.1  christos    "monitor set debug-format all,none,timestamp".
   1417      1.1  christos    Instead we just parse them one at a time, in order.
   1418      1.1  christos 
   1419      1.1  christos    The syntax for "monitor set debug" we support here is not identical
   1420      1.1  christos    to gdb's "set debug foo on|off" because we also use this function to
   1421      1.1  christos    parse "--debug-format=foo,bar".  */
   1422      1.1  christos 
   1423      1.1  christos static std::string
   1424      1.1  christos parse_debug_format_options (const char *arg, int is_monitor)
   1425      1.1  christos {
   1426      1.1  christos   /* First turn all debug format options off.  */
   1427      1.1  christos   debug_timestamp = 0;
   1428      1.1  christos 
   1429      1.1  christos   /* First remove leading spaces, for "monitor set debug-format".  */
   1430      1.1  christos   while (isspace (*arg))
   1431      1.1  christos     ++arg;
   1432      1.1  christos 
   1433      1.1  christos   std::vector<gdb::unique_xmalloc_ptr<char>> options
   1434      1.1  christos     = delim_string_to_char_ptr_vec (arg, ',');
   1435      1.1  christos 
   1436      1.1  christos   for (const gdb::unique_xmalloc_ptr<char> &option : options)
   1437      1.1  christos     {
   1438      1.1  christos       if (strcmp (option.get (), "all") == 0)
   1439      1.1  christos 	{
   1440      1.1  christos 	  debug_timestamp = 1;
   1441      1.1  christos 	  if (is_monitor)
   1442      1.1  christos 	    monitor_output ("All extra debug format options enabled.\n");
   1443      1.1  christos 	}
   1444      1.1  christos       else if (strcmp (option.get (), "none") == 0)
   1445      1.1  christos 	{
   1446      1.1  christos 	  debug_timestamp = 0;
   1447      1.1  christos 	  if (is_monitor)
   1448      1.1  christos 	    monitor_output ("All extra debug format options disabled.\n");
   1449      1.1  christos 	}
   1450      1.1  christos       else if (strcmp (option.get (), "timestamp") == 0)
   1451      1.1  christos 	{
   1452      1.1  christos 	  debug_timestamp = 1;
   1453      1.1  christos 	  if (is_monitor)
   1454      1.1  christos 	    monitor_output ("Timestamps will be added to debug output.\n");
   1455      1.1  christos 	}
   1456      1.1  christos       else if (*option == '\0')
   1457      1.1  christos 	{
   1458      1.1  christos 	  /* An empty option, e.g., "--debug-format=foo,,bar", is ignored.  */
   1459      1.1  christos 	  continue;
   1460      1.1  christos 	}
   1461      1.1  christos       else
   1462      1.1  christos 	return string_printf ("Unknown debug-format argument: \"%s\"\n",
   1463      1.1  christos 			      option.get ());
   1464      1.1  christos     }
   1465      1.1  christos 
   1466      1.1  christos   return std::string ();
   1467      1.1  christos }
   1468      1.1  christos 
   1469  1.1.1.3  christos /* A wrapper to enable, or disable a debug flag.  These are debug flags
   1470  1.1.1.3  christos    that control the debug output from gdbserver, that developers might
   1471  1.1.1.3  christos    want, this is not something most end users will need.  */
   1472  1.1.1.3  christos 
   1473  1.1.1.3  christos struct debug_opt
   1474  1.1.1.3  christos {
   1475  1.1.1.3  christos   /* NAME is the name of this debug option, this should be a simple string
   1476  1.1.1.3  christos      containing no whitespace, starting with a letter from isalpha(), and
   1477  1.1.1.3  christos      contain only isalnum() characters and '_' underscore and '-' hyphen.
   1478  1.1.1.3  christos 
   1479  1.1.1.3  christos      SETTER is a callback function used to set the debug variable.  This
   1480  1.1.1.3  christos      callback will be passed true to enable the debug setting, or false to
   1481  1.1.1.3  christos      disable the debug setting.  */
   1482  1.1.1.3  christos   debug_opt (const char *name, std::function<void (bool)> setter)
   1483  1.1.1.3  christos     : m_name (name),
   1484  1.1.1.3  christos       m_setter (setter)
   1485  1.1.1.3  christos   {
   1486  1.1.1.3  christos     gdb_assert (isalpha (*name));
   1487  1.1.1.3  christos   }
   1488  1.1.1.3  christos 
   1489  1.1.1.3  christos   /* Called to enable or disable the debug setting.  */
   1490  1.1.1.3  christos   void set (bool enable) const
   1491  1.1.1.3  christos   {
   1492  1.1.1.3  christos     m_setter (enable);
   1493  1.1.1.3  christos   }
   1494  1.1.1.3  christos 
   1495  1.1.1.3  christos   /* Return the name of this debug option.  */
   1496  1.1.1.3  christos   const char *name () const
   1497  1.1.1.3  christos   { return m_name; }
   1498  1.1.1.3  christos 
   1499  1.1.1.3  christos private:
   1500  1.1.1.3  christos   /* The name of this debug option.  */
   1501  1.1.1.3  christos   const char *m_name;
   1502  1.1.1.3  christos 
   1503  1.1.1.3  christos   /* The callback to update the debug setting.  */
   1504  1.1.1.3  christos   std::function<void (bool)> m_setter;
   1505  1.1.1.3  christos };
   1506  1.1.1.3  christos 
   1507  1.1.1.3  christos /* The set of all debug options that gdbserver supports.  These are the
   1508  1.1.1.3  christos    options that can be passed to the command line '--debug=...' flag, or to
   1509  1.1.1.3  christos    the monitor command 'monitor set debug ...'.  */
   1510  1.1.1.3  christos 
   1511  1.1.1.3  christos static std::vector<debug_opt> all_debug_opt {
   1512  1.1.1.3  christos   {"threads", [] (bool enable)
   1513  1.1.1.3  christos   {
   1514  1.1.1.3  christos     debug_threads = enable;
   1515  1.1.1.3  christos   }},
   1516  1.1.1.3  christos   {"remote", [] (bool enable)
   1517  1.1.1.3  christos   {
   1518  1.1.1.3  christos     remote_debug = enable;
   1519  1.1.1.3  christos   }},
   1520  1.1.1.3  christos   {"event-loop", [] (bool enable)
   1521  1.1.1.3  christos   {
   1522  1.1.1.3  christos     debug_event_loop = (enable ? debug_event_loop_kind::ALL
   1523  1.1.1.3  christos 			: debug_event_loop_kind::OFF);
   1524  1.1.1.3  christos   }}
   1525  1.1.1.3  christos };
   1526  1.1.1.3  christos 
   1527  1.1.1.3  christos /* Parse the options to --debug=...
   1528  1.1.1.3  christos 
   1529  1.1.1.3  christos    OPTIONS is the string of debug components which should be enabled (or
   1530  1.1.1.3  christos    disabled), and must not be nullptr.  An empty OPTIONS string is valid,
   1531  1.1.1.3  christos    in which case a default set of debug components will be enabled.
   1532  1.1.1.3  christos 
   1533  1.1.1.3  christos    An unknown, or otherwise invalid debug component will result in an
   1534  1.1.1.3  christos    exception being thrown.
   1535  1.1.1.3  christos 
   1536  1.1.1.3  christos    OPTIONS can consist of multiple debug component names separated by a
   1537  1.1.1.3  christos    comma.  Debugging for each component will be turned on.  The special
   1538  1.1.1.3  christos    component 'all' can be used to enable debugging for all components.
   1539  1.1.1.3  christos 
   1540  1.1.1.3  christos    A component can also be prefixed with '-' to disable debugging of that
   1541  1.1.1.3  christos    component, so a user might use: '--debug=all,-remote', to enable all
   1542  1.1.1.3  christos    debugging, except for the remote (protocol) component.  Components are
   1543  1.1.1.3  christos    processed left to write in the OPTIONS list.  */
   1544      1.1  christos 
   1545      1.1  christos static void
   1546  1.1.1.3  christos parse_debug_options (const char *options)
   1547      1.1  christos {
   1548  1.1.1.3  christos   gdb_assert (options != nullptr);
   1549  1.1.1.3  christos 
   1550  1.1.1.3  christos   /* Empty options means the "default" set.  This exists mostly for
   1551  1.1.1.3  christos      backwards compatibility with gdbserver's legacy behaviour.  */
   1552  1.1.1.3  christos   if (*options == '\0')
   1553  1.1.1.3  christos     options = "+threads";
   1554  1.1.1.3  christos 
   1555  1.1.1.3  christos   while (*options != '\0')
   1556      1.1  christos     {
   1557  1.1.1.3  christos       const char *end = strchrnul (options, ',');
   1558  1.1.1.3  christos 
   1559  1.1.1.3  christos       bool enable = *options != '-';
   1560  1.1.1.3  christos       if (*options == '-' || *options == '+')
   1561  1.1.1.3  christos 	++options;
   1562  1.1.1.3  christos 
   1563  1.1.1.3  christos       std::string opt (options, end - options);
   1564  1.1.1.3  christos 
   1565  1.1.1.3  christos       if (opt.size () == 0)
   1566  1.1.1.3  christos 	error ("invalid empty debug option");
   1567  1.1.1.3  christos 
   1568  1.1.1.3  christos       bool is_opt_all = opt == "all";
   1569  1.1.1.3  christos 
   1570  1.1.1.3  christos       bool found = false;
   1571  1.1.1.3  christos       for (const auto &debug_opt : all_debug_opt)
   1572  1.1.1.3  christos 	if (is_opt_all || opt == debug_opt.name ())
   1573  1.1.1.3  christos 	  {
   1574  1.1.1.3  christos 	    debug_opt.set (enable);
   1575  1.1.1.3  christos 	    found = true;
   1576  1.1.1.3  christos 	    if (!is_opt_all)
   1577  1.1.1.3  christos 	      break;
   1578  1.1.1.3  christos 	  }
   1579  1.1.1.3  christos 
   1580  1.1.1.3  christos       if (!found)
   1581  1.1.1.3  christos 	error ("unknown debug option '%s'", opt.c_str ());
   1582  1.1.1.3  christos 
   1583  1.1.1.3  christos       options = (*end == ',') ? end + 1 : end;
   1584      1.1  christos     }
   1585  1.1.1.3  christos }
   1586  1.1.1.3  christos 
   1587  1.1.1.3  christos /* Called from the 'monitor' command handler, to handle general 'set debug'
   1588  1.1.1.3  christos    monitor commands with one of the formats:
   1589  1.1.1.3  christos 
   1590  1.1.1.3  christos      set debug COMPONENT VALUE
   1591  1.1.1.3  christos      set debug VALUE
   1592  1.1.1.3  christos 
   1593  1.1.1.3  christos    In both of these command formats VALUE can be 'on', 'off', '1', or '0'
   1594  1.1.1.3  christos    with 1/0 being equivalent to on/off respectively.
   1595  1.1.1.3  christos 
   1596  1.1.1.3  christos    In the no-COMPONENT version of the command, if VALUE is 'on' (or '1')
   1597  1.1.1.3  christos    then the component 'threads' is assumed, this is for backward
   1598  1.1.1.3  christos    compatibility, but maybe in the future we might find a better "default"
   1599  1.1.1.3  christos    set of debug flags to enable.
   1600  1.1.1.3  christos 
   1601  1.1.1.3  christos    In the no-COMPONENT version of the command, if VALUE is 'off' (or '0')
   1602  1.1.1.3  christos    then all debugging is turned off.
   1603  1.1.1.3  christos 
   1604  1.1.1.3  christos    Otherwise, COMPONENT must be one of the known debug components, and that
   1605  1.1.1.3  christos    component is either enabled or disabled as appropriate.
   1606  1.1.1.3  christos 
   1607  1.1.1.3  christos    The string MON contains either 'COMPONENT VALUE' or just the 'VALUE' for
   1608  1.1.1.3  christos    the second command format, the 'set debug ' has been stripped off
   1609  1.1.1.3  christos    already.
   1610  1.1.1.3  christos 
   1611  1.1.1.3  christos    Return a string containing an error message if something goes wrong,
   1612  1.1.1.3  christos    this error can be returned as part of the monitor command output.  If
   1613  1.1.1.3  christos    everything goes correctly then the debug global will have been updated,
   1614  1.1.1.3  christos    and an empty string is returned.  */
   1615  1.1.1.3  christos 
   1616  1.1.1.3  christos static std::string
   1617  1.1.1.3  christos handle_general_monitor_debug (const char *mon)
   1618  1.1.1.3  christos {
   1619  1.1.1.3  christos   mon = skip_spaces (mon);
   1620  1.1.1.3  christos 
   1621  1.1.1.3  christos   if (*mon == '\0')
   1622  1.1.1.3  christos     return "No debug component name found.\n";
   1623  1.1.1.3  christos 
   1624  1.1.1.3  christos   /* Find the first word within MON.  This is either the component name,
   1625  1.1.1.3  christos      or the value if no component has been given.  */
   1626  1.1.1.3  christos   const char *end = skip_to_space (mon);
   1627  1.1.1.3  christos   std::string component (mon, end - mon);
   1628  1.1.1.3  christos   if (component.find (',') != component.npos || component[0] == '-'
   1629  1.1.1.3  christos       || component[0] == '+')
   1630  1.1.1.3  christos     return "Invalid character found in debug component name.\n";
   1631  1.1.1.3  christos 
   1632  1.1.1.3  christos   /* In ACTION_STR we create a string that will be passed to the
   1633  1.1.1.3  christos      parse_debug_options string.  This will be either '+COMPONENT' or
   1634  1.1.1.3  christos      '-COMPONENT' depending on whether we want to enable or disable
   1635  1.1.1.3  christos      COMPONENT.  */
   1636  1.1.1.3  christos   std::string action_str;
   1637  1.1.1.3  christos 
   1638  1.1.1.3  christos   /* If parse_debug_options succeeds, then MSG will be returned to the user
   1639  1.1.1.3  christos      as the output of the monitor command.  */
   1640  1.1.1.3  christos   std::string msg;
   1641  1.1.1.3  christos 
   1642  1.1.1.3  christos   /* Check for 'set debug off', this disables all debug output.  */
   1643  1.1.1.3  christos   if (component == "0" || component == "off")
   1644  1.1.1.3  christos     {
   1645  1.1.1.3  christos       if (*skip_spaces (end) != '\0')
   1646  1.1.1.3  christos 	return string_printf
   1647  1.1.1.3  christos 	  ("Junk '%s' found at end of 'set debug %s' command.\n",
   1648  1.1.1.3  christos 	   skip_spaces (end), std::string (mon, end - mon).c_str ());
   1649  1.1.1.3  christos 
   1650  1.1.1.3  christos       action_str = "-all";
   1651  1.1.1.3  christos       msg = "All debug output disabled.\n";
   1652  1.1.1.3  christos     }
   1653  1.1.1.3  christos   /* Check for 'set debug on', this disables a general set of debug.  */
   1654  1.1.1.3  christos   else if (component == "1" || component == "on")
   1655  1.1.1.3  christos     {
   1656  1.1.1.3  christos       if (*skip_spaces (end) != '\0')
   1657  1.1.1.3  christos 	return string_printf
   1658  1.1.1.3  christos 	  ("Junk '%s' found at end of 'set debug %s' command.\n",
   1659  1.1.1.3  christos 	   skip_spaces (end), std::string (mon, end - mon).c_str ());
   1660  1.1.1.3  christos 
   1661  1.1.1.3  christos       action_str = "+threads";
   1662  1.1.1.3  christos       msg = "General debug output enabled.\n";
   1663      1.1  christos     }
   1664  1.1.1.3  christos   /* Otherwise we should have 'set debug COMPONENT VALUE'.  Extract the two
   1665  1.1.1.3  christos      parts and validate.  */
   1666  1.1.1.3  christos   else
   1667      1.1  christos     {
   1668  1.1.1.3  christos       /* Figure out the value the user passed.  */
   1669  1.1.1.3  christos       const char *value_start = skip_spaces (end);
   1670  1.1.1.3  christos       if (*value_start == '\0')
   1671  1.1.1.3  christos 	return string_printf ("Missing value for 'set debug %s' command.\n",
   1672  1.1.1.3  christos 			      mon);
   1673  1.1.1.3  christos 
   1674  1.1.1.3  christos       const char *after_value = skip_to_space (value_start);
   1675  1.1.1.3  christos       if (*skip_spaces (after_value) != '\0')
   1676  1.1.1.3  christos 	return string_printf
   1677  1.1.1.3  christos 	  ("Junk '%s' found at end of 'set debug %s' command.\n",
   1678  1.1.1.3  christos 	   skip_spaces (after_value),
   1679  1.1.1.3  christos 	   std::string (mon, after_value - mon).c_str ());
   1680  1.1.1.3  christos 
   1681  1.1.1.3  christos       std::string value (value_start, after_value - value_start);
   1682  1.1.1.3  christos 
   1683  1.1.1.3  christos       /* Check VALUE to see if we are enabling, or disabling.  */
   1684  1.1.1.3  christos       bool enable;
   1685  1.1.1.3  christos       if (value == "0" || value == "off")
   1686  1.1.1.3  christos 	enable = false;
   1687  1.1.1.3  christos       else if (value == "1" || value == "on")
   1688  1.1.1.3  christos 	enable = true;
   1689  1.1.1.3  christos       else
   1690  1.1.1.3  christos 	return string_printf ("Invalid value '%s' for 'set debug %s'.\n",
   1691  1.1.1.3  christos 			      value.c_str (),
   1692  1.1.1.3  christos 			      std::string (mon, end - mon).c_str ());
   1693  1.1.1.3  christos 
   1694  1.1.1.3  christos       action_str = std::string (enable ? "+" : "-") + component;
   1695  1.1.1.3  christos       msg = string_printf ("Debug output for '%s' %s.\n", component.c_str (),
   1696  1.1.1.3  christos 			   enable ? "enabled" : "disabled");
   1697      1.1  christos     }
   1698  1.1.1.3  christos 
   1699  1.1.1.3  christos   gdb_assert (!msg.empty ());
   1700  1.1.1.3  christos   gdb_assert (!action_str.empty ());
   1701  1.1.1.3  christos 
   1702  1.1.1.3  christos   try
   1703      1.1  christos     {
   1704  1.1.1.3  christos       parse_debug_options (action_str.c_str ());
   1705  1.1.1.3  christos       monitor_output (msg.c_str ());
   1706      1.1  christos     }
   1707  1.1.1.3  christos   catch (const gdb_exception_error &exception)
   1708      1.1  christos     {
   1709  1.1.1.3  christos       return string_printf ("Error: %s\n", exception.what ());
   1710      1.1  christos     }
   1711  1.1.1.3  christos 
   1712  1.1.1.3  christos   return {};
   1713  1.1.1.3  christos }
   1714  1.1.1.3  christos 
   1715  1.1.1.3  christos /* Handle monitor commands not handled by target-specific handlers.  */
   1716  1.1.1.3  christos 
   1717  1.1.1.3  christos static void
   1718  1.1.1.3  christos handle_monitor_command (char *mon, char *own_buf)
   1719  1.1.1.3  christos {
   1720  1.1.1.3  christos   if (startswith (mon, "set debug "))
   1721      1.1  christos     {
   1722  1.1.1.3  christos       std::string error_msg
   1723  1.1.1.3  christos 	= handle_general_monitor_debug (mon + sizeof ("set debug ") - 1);
   1724  1.1.1.3  christos 
   1725  1.1.1.3  christos       if (!error_msg.empty ())
   1726  1.1.1.3  christos 	{
   1727  1.1.1.3  christos 	  monitor_output (error_msg.c_str ());
   1728  1.1.1.3  christos 	  monitor_show_help ();
   1729  1.1.1.3  christos 	  write_enn (own_buf);
   1730  1.1.1.3  christos 	}
   1731      1.1  christos     }
   1732  1.1.1.3  christos   else if (strcmp (mon, "set debug-hw-points 1") == 0)
   1733  1.1.1.2  christos     {
   1734  1.1.1.3  christos       show_debug_regs = 1;
   1735  1.1.1.3  christos       monitor_output ("H/W point debugging output enabled.\n");
   1736  1.1.1.2  christos     }
   1737  1.1.1.3  christos   else if (strcmp (mon, "set debug-hw-points 0") == 0)
   1738  1.1.1.2  christos     {
   1739  1.1.1.3  christos       show_debug_regs = 0;
   1740  1.1.1.3  christos       monitor_output ("H/W point debugging output disabled.\n");
   1741  1.1.1.2  christos     }
   1742      1.1  christos   else if (startswith (mon, "set debug-format "))
   1743      1.1  christos     {
   1744      1.1  christos       std::string error_msg
   1745      1.1  christos 	= parse_debug_format_options (mon + sizeof ("set debug-format ") - 1,
   1746      1.1  christos 				      1);
   1747      1.1  christos 
   1748      1.1  christos       if (!error_msg.empty ())
   1749      1.1  christos 	{
   1750      1.1  christos 	  monitor_output (error_msg.c_str ());
   1751      1.1  christos 	  monitor_show_help ();
   1752      1.1  christos 	  write_enn (own_buf);
   1753      1.1  christos 	}
   1754      1.1  christos     }
   1755      1.1  christos   else if (strcmp (mon, "set debug-file") == 0)
   1756      1.1  christos     debug_set_output (nullptr);
   1757      1.1  christos   else if (startswith (mon, "set debug-file "))
   1758      1.1  christos     debug_set_output (mon + sizeof ("set debug-file ") - 1);
   1759      1.1  christos   else if (strcmp (mon, "help") == 0)
   1760      1.1  christos     monitor_show_help ();
   1761      1.1  christos   else if (strcmp (mon, "exit") == 0)
   1762      1.1  christos     exit_requested = true;
   1763      1.1  christos   else
   1764      1.1  christos     {
   1765      1.1  christos       monitor_output ("Unknown monitor command.\n\n");
   1766      1.1  christos       monitor_show_help ();
   1767      1.1  christos       write_enn (own_buf);
   1768      1.1  christos     }
   1769      1.1  christos }
   1770      1.1  christos 
   1771      1.1  christos /* Associates a callback with each supported qXfer'able object.  */
   1772      1.1  christos 
   1773      1.1  christos struct qxfer
   1774      1.1  christos {
   1775      1.1  christos   /* The object this handler handles.  */
   1776      1.1  christos   const char *object;
   1777      1.1  christos 
   1778      1.1  christos   /* Request that the target transfer up to LEN 8-bit bytes of the
   1779      1.1  christos      target's OBJECT.  The OFFSET, for a seekable object, specifies
   1780      1.1  christos      the starting point.  The ANNEX can be used to provide additional
   1781      1.1  christos      data-specific information to the target.
   1782      1.1  christos 
   1783      1.1  christos      Return the number of bytes actually transfered, zero when no
   1784      1.1  christos      further transfer is possible, -1 on error, -2 when the transfer
   1785      1.1  christos      is not supported, and -3 on a verbose error message that should
   1786      1.1  christos      be preserved.  Return of a positive value smaller than LEN does
   1787      1.1  christos      not indicate the end of the object, only the end of the transfer.
   1788      1.1  christos 
   1789      1.1  christos      One, and only one, of readbuf or writebuf must be non-NULL.  */
   1790      1.1  christos   int (*xfer) (const char *annex,
   1791      1.1  christos 	       gdb_byte *readbuf, const gdb_byte *writebuf,
   1792      1.1  christos 	       ULONGEST offset, LONGEST len);
   1793      1.1  christos };
   1794      1.1  christos 
   1795      1.1  christos /* Handle qXfer:auxv:read.  */
   1796      1.1  christos 
   1797      1.1  christos static int
   1798      1.1  christos handle_qxfer_auxv (const char *annex,
   1799      1.1  christos 		   gdb_byte *readbuf, const gdb_byte *writebuf,
   1800      1.1  christos 		   ULONGEST offset, LONGEST len)
   1801      1.1  christos {
   1802      1.1  christos   if (!the_target->supports_read_auxv () || writebuf != NULL)
   1803      1.1  christos     return -2;
   1804      1.1  christos 
   1805      1.1  christos   if (annex[0] != '\0' || current_thread == NULL)
   1806      1.1  christos     return -1;
   1807      1.1  christos 
   1808  1.1.1.3  christos   return the_target->read_auxv (current_thread->id.pid (), offset, readbuf,
   1809  1.1.1.3  christos 				len);
   1810      1.1  christos }
   1811      1.1  christos 
   1812      1.1  christos /* Handle qXfer:exec-file:read.  */
   1813      1.1  christos 
   1814      1.1  christos static int
   1815      1.1  christos handle_qxfer_exec_file (const char *annex,
   1816      1.1  christos 			gdb_byte *readbuf, const gdb_byte *writebuf,
   1817      1.1  christos 			ULONGEST offset, LONGEST len)
   1818      1.1  christos {
   1819      1.1  christos   ULONGEST pid;
   1820      1.1  christos   int total_len;
   1821      1.1  christos 
   1822      1.1  christos   if (!the_target->supports_pid_to_exec_file () || writebuf != NULL)
   1823      1.1  christos     return -2;
   1824      1.1  christos 
   1825      1.1  christos   if (annex[0] == '\0')
   1826      1.1  christos     {
   1827      1.1  christos       if (current_thread == NULL)
   1828      1.1  christos 	return -1;
   1829      1.1  christos 
   1830      1.1  christos       pid = pid_of (current_thread);
   1831      1.1  christos     }
   1832      1.1  christos   else
   1833      1.1  christos     {
   1834      1.1  christos       annex = unpack_varlen_hex (annex, &pid);
   1835      1.1  christos       if (annex[0] != '\0')
   1836      1.1  christos 	return -1;
   1837      1.1  christos     }
   1838      1.1  christos 
   1839      1.1  christos   if (pid <= 0)
   1840      1.1  christos     return -1;
   1841      1.1  christos 
   1842  1.1.1.2  christos   const char *file = the_target->pid_to_exec_file (pid);
   1843      1.1  christos   if (file == NULL)
   1844      1.1  christos     return -1;
   1845      1.1  christos 
   1846      1.1  christos   total_len = strlen (file);
   1847      1.1  christos 
   1848      1.1  christos   if (offset > total_len)
   1849      1.1  christos     return -1;
   1850      1.1  christos 
   1851      1.1  christos   if (offset + len > total_len)
   1852      1.1  christos     len = total_len - offset;
   1853      1.1  christos 
   1854      1.1  christos   memcpy (readbuf, file + offset, len);
   1855      1.1  christos   return len;
   1856      1.1  christos }
   1857      1.1  christos 
   1858      1.1  christos /* Handle qXfer:features:read.  */
   1859      1.1  christos 
   1860      1.1  christos static int
   1861      1.1  christos handle_qxfer_features (const char *annex,
   1862      1.1  christos 		       gdb_byte *readbuf, const gdb_byte *writebuf,
   1863      1.1  christos 		       ULONGEST offset, LONGEST len)
   1864      1.1  christos {
   1865      1.1  christos   const char *document;
   1866      1.1  christos   size_t total_len;
   1867      1.1  christos 
   1868      1.1  christos   if (writebuf != NULL)
   1869      1.1  christos     return -2;
   1870      1.1  christos 
   1871      1.1  christos   if (!target_running ())
   1872      1.1  christos     return -1;
   1873      1.1  christos 
   1874      1.1  christos   /* Grab the correct annex.  */
   1875      1.1  christos   document = get_features_xml (annex);
   1876      1.1  christos   if (document == NULL)
   1877      1.1  christos     return -1;
   1878      1.1  christos 
   1879      1.1  christos   total_len = strlen (document);
   1880      1.1  christos 
   1881      1.1  christos   if (offset > total_len)
   1882      1.1  christos     return -1;
   1883      1.1  christos 
   1884      1.1  christos   if (offset + len > total_len)
   1885      1.1  christos     len = total_len - offset;
   1886      1.1  christos 
   1887      1.1  christos   memcpy (readbuf, document + offset, len);
   1888      1.1  christos   return len;
   1889      1.1  christos }
   1890      1.1  christos 
   1891      1.1  christos /* Handle qXfer:libraries:read.  */
   1892      1.1  christos 
   1893      1.1  christos static int
   1894      1.1  christos handle_qxfer_libraries (const char *annex,
   1895      1.1  christos 			gdb_byte *readbuf, const gdb_byte *writebuf,
   1896      1.1  christos 			ULONGEST offset, LONGEST len)
   1897      1.1  christos {
   1898      1.1  christos   if (writebuf != NULL)
   1899      1.1  christos     return -2;
   1900      1.1  christos 
   1901      1.1  christos   if (annex[0] != '\0' || current_thread == NULL)
   1902      1.1  christos     return -1;
   1903      1.1  christos 
   1904      1.1  christos   std::string document = "<library-list version=\"1.0\">\n";
   1905      1.1  christos 
   1906  1.1.1.2  christos   process_info *proc = current_process ();
   1907  1.1.1.2  christos   for (const dll_info &dll : proc->all_dlls)
   1908      1.1  christos     document += string_printf
   1909      1.1  christos       ("  <library name=\"%s\"><segment address=\"0x%s\"/></library>\n",
   1910      1.1  christos        dll.name.c_str (), paddress (dll.base_addr));
   1911      1.1  christos 
   1912      1.1  christos   document += "</library-list>\n";
   1913      1.1  christos 
   1914      1.1  christos   if (offset > document.length ())
   1915      1.1  christos     return -1;
   1916      1.1  christos 
   1917      1.1  christos   if (offset + len > document.length ())
   1918      1.1  christos     len = document.length () - offset;
   1919      1.1  christos 
   1920      1.1  christos   memcpy (readbuf, &document[offset], len);
   1921      1.1  christos 
   1922      1.1  christos   return len;
   1923      1.1  christos }
   1924      1.1  christos 
   1925      1.1  christos /* Handle qXfer:libraries-svr4:read.  */
   1926      1.1  christos 
   1927      1.1  christos static int
   1928      1.1  christos handle_qxfer_libraries_svr4 (const char *annex,
   1929      1.1  christos 			     gdb_byte *readbuf, const gdb_byte *writebuf,
   1930      1.1  christos 			     ULONGEST offset, LONGEST len)
   1931      1.1  christos {
   1932      1.1  christos   if (writebuf != NULL)
   1933      1.1  christos     return -2;
   1934      1.1  christos 
   1935      1.1  christos   if (current_thread == NULL
   1936      1.1  christos       || !the_target->supports_qxfer_libraries_svr4 ())
   1937      1.1  christos     return -1;
   1938      1.1  christos 
   1939      1.1  christos   return the_target->qxfer_libraries_svr4 (annex, readbuf, writebuf,
   1940      1.1  christos 					   offset, len);
   1941      1.1  christos }
   1942      1.1  christos 
   1943      1.1  christos /* Handle qXfer:osadata:read.  */
   1944      1.1  christos 
   1945      1.1  christos static int
   1946      1.1  christos handle_qxfer_osdata (const char *annex,
   1947      1.1  christos 		     gdb_byte *readbuf, const gdb_byte *writebuf,
   1948      1.1  christos 		     ULONGEST offset, LONGEST len)
   1949      1.1  christos {
   1950      1.1  christos   if (!the_target->supports_qxfer_osdata () || writebuf != NULL)
   1951      1.1  christos     return -2;
   1952      1.1  christos 
   1953      1.1  christos   return the_target->qxfer_osdata (annex, readbuf, NULL, offset, len);
   1954      1.1  christos }
   1955      1.1  christos 
   1956      1.1  christos /* Handle qXfer:siginfo:read and qXfer:siginfo:write.  */
   1957      1.1  christos 
   1958      1.1  christos static int
   1959      1.1  christos handle_qxfer_siginfo (const char *annex,
   1960      1.1  christos 		      gdb_byte *readbuf, const gdb_byte *writebuf,
   1961      1.1  christos 		      ULONGEST offset, LONGEST len)
   1962      1.1  christos {
   1963      1.1  christos   if (!the_target->supports_qxfer_siginfo ())
   1964      1.1  christos     return -2;
   1965      1.1  christos 
   1966      1.1  christos   if (annex[0] != '\0' || current_thread == NULL)
   1967      1.1  christos     return -1;
   1968      1.1  christos 
   1969      1.1  christos   return the_target->qxfer_siginfo (annex, readbuf, writebuf, offset, len);
   1970      1.1  christos }
   1971      1.1  christos 
   1972      1.1  christos /* Handle qXfer:statictrace:read.  */
   1973      1.1  christos 
   1974      1.1  christos static int
   1975      1.1  christos handle_qxfer_statictrace (const char *annex,
   1976      1.1  christos 			  gdb_byte *readbuf, const gdb_byte *writebuf,
   1977      1.1  christos 			  ULONGEST offset, LONGEST len)
   1978      1.1  christos {
   1979      1.1  christos   client_state &cs = get_client_state ();
   1980      1.1  christos   ULONGEST nbytes;
   1981      1.1  christos 
   1982      1.1  christos   if (writebuf != NULL)
   1983      1.1  christos     return -2;
   1984      1.1  christos 
   1985      1.1  christos   if (annex[0] != '\0' || current_thread == NULL
   1986      1.1  christos       || cs.current_traceframe == -1)
   1987      1.1  christos     return -1;
   1988      1.1  christos 
   1989      1.1  christos   if (traceframe_read_sdata (cs.current_traceframe, offset,
   1990      1.1  christos 			     readbuf, len, &nbytes))
   1991      1.1  christos     return -1;
   1992      1.1  christos   return nbytes;
   1993      1.1  christos }
   1994      1.1  christos 
   1995      1.1  christos /* Helper for handle_qxfer_threads_proper.
   1996      1.1  christos    Emit the XML to describe the thread of INF.  */
   1997      1.1  christos 
   1998      1.1  christos static void
   1999  1.1.1.3  christos handle_qxfer_threads_worker (thread_info *thread, std::string *buffer)
   2000      1.1  christos {
   2001      1.1  christos   ptid_t ptid = ptid_of (thread);
   2002      1.1  christos   char ptid_s[100];
   2003      1.1  christos   int core = target_core_of_thread (ptid);
   2004      1.1  christos   char core_s[21];
   2005      1.1  christos   const char *name = target_thread_name (ptid);
   2006      1.1  christos   int handle_len;
   2007      1.1  christos   gdb_byte *handle;
   2008      1.1  christos   bool handle_status = target_thread_handle (ptid, &handle, &handle_len);
   2009      1.1  christos 
   2010  1.1.1.3  christos   /* If this is a (v)fork/clone child (has a (v)fork/clone parent),
   2011  1.1.1.3  christos      GDB does not yet know about this thread, and must not know about
   2012  1.1.1.3  christos      it until it gets the corresponding (v)fork/clone event.  Exclude
   2013  1.1.1.3  christos      this thread from the list.  */
   2014  1.1.1.2  christos   if (target_thread_pending_parent (thread) != nullptr)
   2015  1.1.1.2  christos     return;
   2016  1.1.1.2  christos 
   2017      1.1  christos   write_ptid (ptid_s, ptid);
   2018      1.1  christos 
   2019  1.1.1.3  christos   string_xml_appendf (*buffer, "<thread id=\"%s\"", ptid_s);
   2020      1.1  christos 
   2021      1.1  christos   if (core != -1)
   2022      1.1  christos     {
   2023      1.1  christos       sprintf (core_s, "%d", core);
   2024  1.1.1.3  christos       string_xml_appendf (*buffer, " core=\"%s\"", core_s);
   2025      1.1  christos     }
   2026      1.1  christos 
   2027      1.1  christos   if (name != NULL)
   2028  1.1.1.3  christos     string_xml_appendf (*buffer, " name=\"%s\"", name);
   2029      1.1  christos 
   2030      1.1  christos   if (handle_status)
   2031      1.1  christos     {
   2032      1.1  christos       char *handle_s = (char *) alloca (handle_len * 2 + 1);
   2033      1.1  christos       bin2hex (handle, handle_s, handle_len);
   2034  1.1.1.3  christos       string_xml_appendf (*buffer, " handle=\"%s\"", handle_s);
   2035      1.1  christos     }
   2036      1.1  christos 
   2037  1.1.1.3  christos   string_xml_appendf (*buffer, "/>\n");
   2038      1.1  christos }
   2039      1.1  christos 
   2040      1.1  christos /* Helper for handle_qxfer_threads.  Return true on success, false
   2041      1.1  christos    otherwise.  */
   2042      1.1  christos 
   2043      1.1  christos static bool
   2044  1.1.1.3  christos handle_qxfer_threads_proper (std::string *buffer)
   2045      1.1  christos {
   2046  1.1.1.3  christos   *buffer += "<threads>\n";
   2047      1.1  christos 
   2048  1.1.1.2  christos   /* The target may need to access memory and registers (e.g. via
   2049  1.1.1.2  christos      libthread_db) to fetch thread properties.  Even if don't need to
   2050  1.1.1.2  christos      stop threads to access memory, we still will need to be able to
   2051  1.1.1.2  christos      access registers, and other ptrace accesses like
   2052  1.1.1.2  christos      PTRACE_GET_THREAD_AREA that require a paused thread.  Pause all
   2053  1.1.1.2  christos      threads here, so that we pause each thread at most once for all
   2054  1.1.1.2  christos      accesses.  */
   2055  1.1.1.2  christos   if (non_stop)
   2056  1.1.1.2  christos     target_pause_all (true);
   2057      1.1  christos 
   2058  1.1.1.2  christos   for_each_thread ([&] (thread_info *thread)
   2059  1.1.1.2  christos     {
   2060  1.1.1.2  christos       handle_qxfer_threads_worker (thread, buffer);
   2061      1.1  christos     });
   2062      1.1  christos 
   2063  1.1.1.2  christos   if (non_stop)
   2064  1.1.1.2  christos     target_unpause_all (true);
   2065  1.1.1.2  christos 
   2066  1.1.1.3  christos   *buffer += "</threads>\n";
   2067  1.1.1.2  christos   return true;
   2068      1.1  christos }
   2069      1.1  christos 
   2070      1.1  christos /* Handle qXfer:threads:read.  */
   2071      1.1  christos 
   2072      1.1  christos static int
   2073      1.1  christos handle_qxfer_threads (const char *annex,
   2074      1.1  christos 		      gdb_byte *readbuf, const gdb_byte *writebuf,
   2075      1.1  christos 		      ULONGEST offset, LONGEST len)
   2076      1.1  christos {
   2077  1.1.1.3  christos   static std::string result;
   2078      1.1  christos 
   2079      1.1  christos   if (writebuf != NULL)
   2080      1.1  christos     return -2;
   2081      1.1  christos 
   2082      1.1  christos   if (annex[0] != '\0')
   2083      1.1  christos     return -1;
   2084      1.1  christos 
   2085      1.1  christos   if (offset == 0)
   2086      1.1  christos     {
   2087      1.1  christos       /* When asked for data at offset 0, generate everything and store into
   2088      1.1  christos 	 'result'.  Successive reads will be served off 'result'.  */
   2089  1.1.1.3  christos       result.clear ();
   2090      1.1  christos 
   2091  1.1.1.3  christos       bool res = handle_qxfer_threads_proper (&result);
   2092      1.1  christos 
   2093      1.1  christos       if (!res)
   2094      1.1  christos 	return -1;
   2095      1.1  christos     }
   2096      1.1  christos 
   2097  1.1.1.3  christos   if (offset >= result.length ())
   2098      1.1  christos     {
   2099      1.1  christos       /* We're out of data.  */
   2100  1.1.1.3  christos       result.clear ();
   2101      1.1  christos       return 0;
   2102      1.1  christos     }
   2103      1.1  christos 
   2104  1.1.1.3  christos   if (len > result.length () - offset)
   2105  1.1.1.3  christos     len = result.length () - offset;
   2106      1.1  christos 
   2107  1.1.1.3  christos   memcpy (readbuf, result.c_str () + offset, len);
   2108      1.1  christos 
   2109      1.1  christos   return len;
   2110      1.1  christos }
   2111      1.1  christos 
   2112      1.1  christos /* Handle qXfer:traceframe-info:read.  */
   2113      1.1  christos 
   2114      1.1  christos static int
   2115      1.1  christos handle_qxfer_traceframe_info (const char *annex,
   2116      1.1  christos 			      gdb_byte *readbuf, const gdb_byte *writebuf,
   2117      1.1  christos 			      ULONGEST offset, LONGEST len)
   2118      1.1  christos {
   2119      1.1  christos   client_state &cs = get_client_state ();
   2120  1.1.1.3  christos   static std::string result;
   2121      1.1  christos 
   2122      1.1  christos   if (writebuf != NULL)
   2123      1.1  christos     return -2;
   2124      1.1  christos 
   2125      1.1  christos   if (!target_running () || annex[0] != '\0' || cs.current_traceframe == -1)
   2126      1.1  christos     return -1;
   2127      1.1  christos 
   2128      1.1  christos   if (offset == 0)
   2129      1.1  christos     {
   2130      1.1  christos       /* When asked for data at offset 0, generate everything and
   2131      1.1  christos 	 store into 'result'.  Successive reads will be served off
   2132      1.1  christos 	 'result'.  */
   2133  1.1.1.3  christos       result.clear ();
   2134      1.1  christos 
   2135  1.1.1.3  christos       traceframe_read_info (cs.current_traceframe, &result);
   2136      1.1  christos     }
   2137      1.1  christos 
   2138  1.1.1.3  christos   if (offset >= result.length ())
   2139      1.1  christos     {
   2140      1.1  christos       /* We're out of data.  */
   2141  1.1.1.3  christos       result.clear ();
   2142      1.1  christos       return 0;
   2143      1.1  christos     }
   2144      1.1  christos 
   2145  1.1.1.3  christos   if (len > result.length () - offset)
   2146  1.1.1.3  christos     len = result.length () - offset;
   2147      1.1  christos 
   2148  1.1.1.3  christos   memcpy (readbuf, result.c_str () + offset, len);
   2149      1.1  christos   return len;
   2150      1.1  christos }
   2151      1.1  christos 
   2152      1.1  christos /* Handle qXfer:fdpic:read.  */
   2153      1.1  christos 
   2154      1.1  christos static int
   2155      1.1  christos handle_qxfer_fdpic (const char *annex, gdb_byte *readbuf,
   2156      1.1  christos 		    const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
   2157      1.1  christos {
   2158      1.1  christos   if (!the_target->supports_read_loadmap ())
   2159      1.1  christos     return -2;
   2160      1.1  christos 
   2161      1.1  christos   if (current_thread == NULL)
   2162      1.1  christos     return -1;
   2163      1.1  christos 
   2164      1.1  christos   return the_target->read_loadmap (annex, offset, readbuf, len);
   2165      1.1  christos }
   2166      1.1  christos 
   2167      1.1  christos /* Handle qXfer:btrace:read.  */
   2168      1.1  christos 
   2169      1.1  christos static int
   2170      1.1  christos handle_qxfer_btrace (const char *annex,
   2171      1.1  christos 		     gdb_byte *readbuf, const gdb_byte *writebuf,
   2172      1.1  christos 		     ULONGEST offset, LONGEST len)
   2173      1.1  christos {
   2174      1.1  christos   client_state &cs = get_client_state ();
   2175  1.1.1.3  christos   static std::string cache;
   2176      1.1  christos   struct thread_info *thread;
   2177      1.1  christos   enum btrace_read_type type;
   2178      1.1  christos   int result;
   2179      1.1  christos 
   2180      1.1  christos   if (writebuf != NULL)
   2181      1.1  christos     return -2;
   2182      1.1  christos 
   2183      1.1  christos   if (cs.general_thread == null_ptid
   2184      1.1  christos       || cs.general_thread == minus_one_ptid)
   2185      1.1  christos     {
   2186      1.1  christos       strcpy (cs.own_buf, "E.Must select a single thread.");
   2187      1.1  christos       return -3;
   2188      1.1  christos     }
   2189      1.1  christos 
   2190      1.1  christos   thread = find_thread_ptid (cs.general_thread);
   2191      1.1  christos   if (thread == NULL)
   2192      1.1  christos     {
   2193      1.1  christos       strcpy (cs.own_buf, "E.No such thread.");
   2194      1.1  christos       return -3;
   2195      1.1  christos     }
   2196      1.1  christos 
   2197      1.1  christos   if (thread->btrace == NULL)
   2198      1.1  christos     {
   2199      1.1  christos       strcpy (cs.own_buf, "E.Btrace not enabled.");
   2200      1.1  christos       return -3;
   2201      1.1  christos     }
   2202      1.1  christos 
   2203      1.1  christos   if (strcmp (annex, "all") == 0)
   2204      1.1  christos     type = BTRACE_READ_ALL;
   2205      1.1  christos   else if (strcmp (annex, "new") == 0)
   2206      1.1  christos     type = BTRACE_READ_NEW;
   2207      1.1  christos   else if (strcmp (annex, "delta") == 0)
   2208      1.1  christos     type = BTRACE_READ_DELTA;
   2209      1.1  christos   else
   2210      1.1  christos     {
   2211      1.1  christos       strcpy (cs.own_buf, "E.Bad annex.");
   2212      1.1  christos       return -3;
   2213      1.1  christos     }
   2214      1.1  christos 
   2215      1.1  christos   if (offset == 0)
   2216      1.1  christos     {
   2217  1.1.1.3  christos       cache.clear ();
   2218      1.1  christos 
   2219      1.1  christos       try
   2220      1.1  christos 	{
   2221      1.1  christos 	  result = target_read_btrace (thread->btrace, &cache, type);
   2222      1.1  christos 	  if (result != 0)
   2223  1.1.1.3  christos 	    memcpy (cs.own_buf, cache.c_str (), cache.length ());
   2224      1.1  christos 	}
   2225      1.1  christos       catch (const gdb_exception_error &exception)
   2226      1.1  christos 	{
   2227      1.1  christos 	  sprintf (cs.own_buf, "E.%s", exception.what ());
   2228      1.1  christos 	  result = -1;
   2229      1.1  christos 	}
   2230      1.1  christos 
   2231      1.1  christos       if (result != 0)
   2232      1.1  christos 	return -3;
   2233      1.1  christos     }
   2234  1.1.1.3  christos   else if (offset > cache.length ())
   2235      1.1  christos     {
   2236  1.1.1.3  christos       cache.clear ();
   2237      1.1  christos       return -3;
   2238      1.1  christos     }
   2239      1.1  christos 
   2240  1.1.1.3  christos   if (len > cache.length () - offset)
   2241  1.1.1.3  christos     len = cache.length () - offset;
   2242      1.1  christos 
   2243  1.1.1.3  christos   memcpy (readbuf, cache.c_str () + offset, len);
   2244      1.1  christos 
   2245      1.1  christos   return len;
   2246      1.1  christos }
   2247      1.1  christos 
   2248      1.1  christos /* Handle qXfer:btrace-conf:read.  */
   2249      1.1  christos 
   2250      1.1  christos static int
   2251      1.1  christos handle_qxfer_btrace_conf (const char *annex,
   2252      1.1  christos 			  gdb_byte *readbuf, const gdb_byte *writebuf,
   2253      1.1  christos 			  ULONGEST offset, LONGEST len)
   2254      1.1  christos {
   2255      1.1  christos   client_state &cs = get_client_state ();
   2256  1.1.1.3  christos   static std::string cache;
   2257      1.1  christos   struct thread_info *thread;
   2258      1.1  christos   int result;
   2259      1.1  christos 
   2260      1.1  christos   if (writebuf != NULL)
   2261      1.1  christos     return -2;
   2262      1.1  christos 
   2263      1.1  christos   if (annex[0] != '\0')
   2264      1.1  christos     return -1;
   2265      1.1  christos 
   2266      1.1  christos   if (cs.general_thread == null_ptid
   2267      1.1  christos       || cs.general_thread == minus_one_ptid)
   2268      1.1  christos     {
   2269      1.1  christos       strcpy (cs.own_buf, "E.Must select a single thread.");
   2270      1.1  christos       return -3;
   2271      1.1  christos     }
   2272      1.1  christos 
   2273      1.1  christos   thread = find_thread_ptid (cs.general_thread);
   2274      1.1  christos   if (thread == NULL)
   2275      1.1  christos     {
   2276      1.1  christos       strcpy (cs.own_buf, "E.No such thread.");
   2277      1.1  christos       return -3;
   2278      1.1  christos     }
   2279      1.1  christos 
   2280      1.1  christos   if (thread->btrace == NULL)
   2281      1.1  christos     {
   2282      1.1  christos       strcpy (cs.own_buf, "E.Btrace not enabled.");
   2283      1.1  christos       return -3;
   2284      1.1  christos     }
   2285      1.1  christos 
   2286      1.1  christos   if (offset == 0)
   2287      1.1  christos     {
   2288  1.1.1.3  christos       cache.clear ();
   2289      1.1  christos 
   2290      1.1  christos       try
   2291      1.1  christos 	{
   2292      1.1  christos 	  result = target_read_btrace_conf (thread->btrace, &cache);
   2293      1.1  christos 	  if (result != 0)
   2294  1.1.1.3  christos 	    memcpy (cs.own_buf, cache.c_str (), cache.length ());
   2295      1.1  christos 	}
   2296      1.1  christos       catch (const gdb_exception_error &exception)
   2297      1.1  christos 	{
   2298      1.1  christos 	  sprintf (cs.own_buf, "E.%s", exception.what ());
   2299      1.1  christos 	  result = -1;
   2300      1.1  christos 	}
   2301      1.1  christos 
   2302      1.1  christos       if (result != 0)
   2303      1.1  christos 	return -3;
   2304      1.1  christos     }
   2305  1.1.1.3  christos   else if (offset > cache.length ())
   2306      1.1  christos     {
   2307  1.1.1.3  christos       cache.clear ();
   2308      1.1  christos       return -3;
   2309      1.1  christos     }
   2310      1.1  christos 
   2311  1.1.1.3  christos   if (len > cache.length () - offset)
   2312  1.1.1.3  christos     len = cache.length () - offset;
   2313      1.1  christos 
   2314  1.1.1.3  christos   memcpy (readbuf, cache.c_str () + offset, len);
   2315      1.1  christos 
   2316      1.1  christos   return len;
   2317      1.1  christos }
   2318      1.1  christos 
   2319      1.1  christos static const struct qxfer qxfer_packets[] =
   2320      1.1  christos   {
   2321      1.1  christos     { "auxv", handle_qxfer_auxv },
   2322      1.1  christos     { "btrace", handle_qxfer_btrace },
   2323      1.1  christos     { "btrace-conf", handle_qxfer_btrace_conf },
   2324      1.1  christos     { "exec-file", handle_qxfer_exec_file},
   2325      1.1  christos     { "fdpic", handle_qxfer_fdpic},
   2326      1.1  christos     { "features", handle_qxfer_features },
   2327      1.1  christos     { "libraries", handle_qxfer_libraries },
   2328      1.1  christos     { "libraries-svr4", handle_qxfer_libraries_svr4 },
   2329      1.1  christos     { "osdata", handle_qxfer_osdata },
   2330      1.1  christos     { "siginfo", handle_qxfer_siginfo },
   2331      1.1  christos     { "statictrace", handle_qxfer_statictrace },
   2332      1.1  christos     { "threads", handle_qxfer_threads },
   2333      1.1  christos     { "traceframe-info", handle_qxfer_traceframe_info },
   2334      1.1  christos   };
   2335      1.1  christos 
   2336      1.1  christos static int
   2337      1.1  christos handle_qxfer (char *own_buf, int packet_len, int *new_packet_len_p)
   2338      1.1  christos {
   2339      1.1  christos   int i;
   2340      1.1  christos   char *object;
   2341      1.1  christos   char *rw;
   2342      1.1  christos   char *annex;
   2343      1.1  christos   char *offset;
   2344      1.1  christos 
   2345      1.1  christos   if (!startswith (own_buf, "qXfer:"))
   2346      1.1  christos     return 0;
   2347      1.1  christos 
   2348      1.1  christos   /* Grab the object, r/w and annex.  */
   2349      1.1  christos   if (decode_xfer (own_buf + 6, &object, &rw, &annex, &offset) < 0)
   2350      1.1  christos     {
   2351      1.1  christos       write_enn (own_buf);
   2352      1.1  christos       return 1;
   2353      1.1  christos     }
   2354      1.1  christos 
   2355      1.1  christos   for (i = 0;
   2356      1.1  christos        i < sizeof (qxfer_packets) / sizeof (qxfer_packets[0]);
   2357      1.1  christos        i++)
   2358      1.1  christos     {
   2359      1.1  christos       const struct qxfer *q = &qxfer_packets[i];
   2360      1.1  christos 
   2361      1.1  christos       if (strcmp (object, q->object) == 0)
   2362      1.1  christos 	{
   2363      1.1  christos 	  if (strcmp (rw, "read") == 0)
   2364      1.1  christos 	    {
   2365      1.1  christos 	      unsigned char *data;
   2366      1.1  christos 	      int n;
   2367      1.1  christos 	      CORE_ADDR ofs;
   2368      1.1  christos 	      unsigned int len;
   2369      1.1  christos 
   2370      1.1  christos 	      /* Grab the offset and length.  */
   2371      1.1  christos 	      if (decode_xfer_read (offset, &ofs, &len) < 0)
   2372      1.1  christos 		{
   2373      1.1  christos 		  write_enn (own_buf);
   2374      1.1  christos 		  return 1;
   2375      1.1  christos 		}
   2376      1.1  christos 
   2377      1.1  christos 	      /* Read one extra byte, as an indicator of whether there is
   2378      1.1  christos 		 more.  */
   2379      1.1  christos 	      if (len > PBUFSIZ - 2)
   2380      1.1  christos 		len = PBUFSIZ - 2;
   2381      1.1  christos 	      data = (unsigned char *) malloc (len + 1);
   2382      1.1  christos 	      if (data == NULL)
   2383      1.1  christos 		{
   2384      1.1  christos 		  write_enn (own_buf);
   2385      1.1  christos 		  return 1;
   2386      1.1  christos 		}
   2387      1.1  christos 	      n = (*q->xfer) (annex, data, NULL, ofs, len + 1);
   2388      1.1  christos 	      if (n == -2)
   2389      1.1  christos 		{
   2390      1.1  christos 		  free (data);
   2391      1.1  christos 		  return 0;
   2392      1.1  christos 		}
   2393      1.1  christos 	      else if (n == -3)
   2394      1.1  christos 		{
   2395      1.1  christos 		  /* Preserve error message.  */
   2396      1.1  christos 		}
   2397      1.1  christos 	      else if (n < 0)
   2398      1.1  christos 		write_enn (own_buf);
   2399      1.1  christos 	      else if (n > len)
   2400      1.1  christos 		*new_packet_len_p = write_qxfer_response (own_buf, data, len, 1);
   2401      1.1  christos 	      else
   2402      1.1  christos 		*new_packet_len_p = write_qxfer_response (own_buf, data, n, 0);
   2403      1.1  christos 
   2404      1.1  christos 	      free (data);
   2405      1.1  christos 	      return 1;
   2406      1.1  christos 	    }
   2407      1.1  christos 	  else if (strcmp (rw, "write") == 0)
   2408      1.1  christos 	    {
   2409      1.1  christos 	      int n;
   2410      1.1  christos 	      unsigned int len;
   2411      1.1  christos 	      CORE_ADDR ofs;
   2412      1.1  christos 	      unsigned char *data;
   2413      1.1  christos 
   2414      1.1  christos 	      strcpy (own_buf, "E00");
   2415      1.1  christos 	      data = (unsigned char *) malloc (packet_len - (offset - own_buf));
   2416      1.1  christos 	      if (data == NULL)
   2417      1.1  christos 		{
   2418      1.1  christos 		  write_enn (own_buf);
   2419      1.1  christos 		  return 1;
   2420      1.1  christos 		}
   2421      1.1  christos 	      if (decode_xfer_write (offset, packet_len - (offset - own_buf),
   2422      1.1  christos 				     &ofs, &len, data) < 0)
   2423      1.1  christos 		{
   2424      1.1  christos 		  free (data);
   2425      1.1  christos 		  write_enn (own_buf);
   2426      1.1  christos 		  return 1;
   2427      1.1  christos 		}
   2428      1.1  christos 
   2429      1.1  christos 	      n = (*q->xfer) (annex, NULL, data, ofs, len);
   2430      1.1  christos 	      if (n == -2)
   2431      1.1  christos 		{
   2432      1.1  christos 		  free (data);
   2433      1.1  christos 		  return 0;
   2434      1.1  christos 		}
   2435      1.1  christos 	      else if (n == -3)
   2436      1.1  christos 		{
   2437      1.1  christos 		  /* Preserve error message.  */
   2438      1.1  christos 		}
   2439      1.1  christos 	      else if (n < 0)
   2440      1.1  christos 		write_enn (own_buf);
   2441      1.1  christos 	      else
   2442      1.1  christos 		sprintf (own_buf, "%x", n);
   2443      1.1  christos 
   2444      1.1  christos 	      free (data);
   2445      1.1  christos 	      return 1;
   2446      1.1  christos 	    }
   2447      1.1  christos 
   2448      1.1  christos 	  return 0;
   2449      1.1  christos 	}
   2450      1.1  christos     }
   2451      1.1  christos 
   2452      1.1  christos   return 0;
   2453      1.1  christos }
   2454      1.1  christos 
   2455      1.1  christos /* Compute 32 bit CRC from inferior memory.
   2456      1.1  christos 
   2457      1.1  christos    On success, return 32 bit CRC.
   2458      1.1  christos    On failure, return (unsigned long long) -1.  */
   2459      1.1  christos 
   2460      1.1  christos static unsigned long long
   2461      1.1  christos crc32 (CORE_ADDR base, int len, unsigned int crc)
   2462      1.1  christos {
   2463      1.1  christos   while (len--)
   2464      1.1  christos     {
   2465      1.1  christos       unsigned char byte = 0;
   2466      1.1  christos 
   2467      1.1  christos       /* Return failure if memory read fails.  */
   2468      1.1  christos       if (read_inferior_memory (base, &byte, 1) != 0)
   2469      1.1  christos 	return (unsigned long long) -1;
   2470      1.1  christos 
   2471      1.1  christos       crc = xcrc32 (&byte, 1, crc);
   2472      1.1  christos       base++;
   2473      1.1  christos     }
   2474      1.1  christos   return (unsigned long long) crc;
   2475      1.1  christos }
   2476      1.1  christos 
   2477  1.1.1.2  christos /* Parse the qMemTags packet request into ADDR and LEN.  */
   2478  1.1.1.2  christos 
   2479  1.1.1.2  christos static void
   2480  1.1.1.2  christos parse_fetch_memtags_request (char *request, CORE_ADDR *addr, size_t *len,
   2481  1.1.1.2  christos 			     int *type)
   2482  1.1.1.2  christos {
   2483  1.1.1.2  christos   gdb_assert (startswith (request, "qMemTags:"));
   2484  1.1.1.2  christos 
   2485  1.1.1.2  christos   const char *p = request + strlen ("qMemTags:");
   2486  1.1.1.2  christos 
   2487  1.1.1.2  christos   /* Read address and length.  */
   2488  1.1.1.2  christos   unsigned int length = 0;
   2489  1.1.1.2  christos   p = decode_m_packet_params (p, addr, &length, ':');
   2490  1.1.1.2  christos   *len = length;
   2491  1.1.1.2  christos 
   2492  1.1.1.2  christos   /* Read the tag type.  */
   2493  1.1.1.2  christos   ULONGEST tag_type = 0;
   2494  1.1.1.2  christos   p = unpack_varlen_hex (p, &tag_type);
   2495  1.1.1.2  christos   *type = (int) tag_type;
   2496  1.1.1.2  christos }
   2497  1.1.1.2  christos 
   2498      1.1  christos /* Add supported btrace packets to BUF.  */
   2499      1.1  christos 
   2500      1.1  christos static void
   2501      1.1  christos supported_btrace_packets (char *buf)
   2502      1.1  christos {
   2503      1.1  christos   strcat (buf, ";Qbtrace:bts+");
   2504      1.1  christos   strcat (buf, ";Qbtrace-conf:bts:size+");
   2505      1.1  christos   strcat (buf, ";Qbtrace:pt+");
   2506      1.1  christos   strcat (buf, ";Qbtrace-conf:pt:size+");
   2507      1.1  christos   strcat (buf, ";Qbtrace:off+");
   2508      1.1  christos   strcat (buf, ";qXfer:btrace:read+");
   2509      1.1  christos   strcat (buf, ";qXfer:btrace-conf:read+");
   2510      1.1  christos }
   2511      1.1  christos 
   2512      1.1  christos /* Handle all of the extended 'q' packets.  */
   2513      1.1  christos 
   2514      1.1  christos static void
   2515      1.1  christos handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
   2516      1.1  christos {
   2517      1.1  christos   client_state &cs = get_client_state ();
   2518      1.1  christos   static std::list<thread_info *>::const_iterator thread_iter;
   2519      1.1  christos 
   2520      1.1  christos   /* Reply the current thread id.  */
   2521      1.1  christos   if (strcmp ("qC", own_buf) == 0 && !disable_packet_qC)
   2522      1.1  christos     {
   2523      1.1  christos       ptid_t ptid;
   2524      1.1  christos       require_running_or_return (own_buf);
   2525      1.1  christos 
   2526      1.1  christos       if (cs.general_thread != null_ptid && cs.general_thread != minus_one_ptid)
   2527      1.1  christos 	ptid = cs.general_thread;
   2528      1.1  christos       else
   2529      1.1  christos 	{
   2530      1.1  christos 	  thread_iter = all_threads.begin ();
   2531      1.1  christos 	  ptid = (*thread_iter)->id;
   2532      1.1  christos 	}
   2533      1.1  christos 
   2534      1.1  christos       sprintf (own_buf, "QC");
   2535      1.1  christos       own_buf += 2;
   2536      1.1  christos       write_ptid (own_buf, ptid);
   2537      1.1  christos       return;
   2538      1.1  christos     }
   2539      1.1  christos 
   2540      1.1  christos   if (strcmp ("qSymbol::", own_buf) == 0)
   2541      1.1  christos     {
   2542  1.1.1.2  christos       scoped_restore_current_thread restore_thread;
   2543      1.1  christos 
   2544      1.1  christos       /* For qSymbol, GDB only changes the current thread if the
   2545      1.1  christos 	 previous current thread was of a different process.  So if
   2546      1.1  christos 	 the previous thread is gone, we need to pick another one of
   2547      1.1  christos 	 the same process.  This can happen e.g., if we followed an
   2548      1.1  christos 	 exec in a non-leader thread.  */
   2549      1.1  christos       if (current_thread == NULL)
   2550      1.1  christos 	{
   2551  1.1.1.2  christos 	  thread_info *any_thread
   2552      1.1  christos 	    = find_any_thread_of_pid (cs.general_thread.pid ());
   2553  1.1.1.2  christos 	  switch_to_thread (any_thread);
   2554      1.1  christos 
   2555      1.1  christos 	  /* Just in case, if we didn't find a thread, then bail out
   2556      1.1  christos 	     instead of crashing.  */
   2557      1.1  christos 	  if (current_thread == NULL)
   2558      1.1  christos 	    {
   2559      1.1  christos 	      write_enn (own_buf);
   2560      1.1  christos 	      return;
   2561      1.1  christos 	    }
   2562      1.1  christos 	}
   2563      1.1  christos 
   2564      1.1  christos       /* GDB is suggesting new symbols have been loaded.  This may
   2565      1.1  christos 	 mean a new shared library has been detected as loaded, so
   2566      1.1  christos 	 take the opportunity to check if breakpoints we think are
   2567      1.1  christos 	 inserted, still are.  Note that it isn't guaranteed that
   2568      1.1  christos 	 we'll see this when a shared library is loaded, and nor will
   2569      1.1  christos 	 we see this for unloads (although breakpoints in unloaded
   2570      1.1  christos 	 libraries shouldn't trigger), as GDB may not find symbols for
   2571      1.1  christos 	 the library at all.  We also re-validate breakpoints when we
   2572      1.1  christos 	 see a second GDB breakpoint for the same address, and or when
   2573      1.1  christos 	 we access breakpoint shadows.  */
   2574      1.1  christos       validate_breakpoints ();
   2575      1.1  christos 
   2576      1.1  christos       if (target_supports_tracepoints ())
   2577      1.1  christos 	tracepoint_look_up_symbols ();
   2578      1.1  christos 
   2579      1.1  christos       if (current_thread != NULL)
   2580      1.1  christos 	the_target->look_up_symbols ();
   2581      1.1  christos 
   2582      1.1  christos       strcpy (own_buf, "OK");
   2583      1.1  christos       return;
   2584      1.1  christos     }
   2585      1.1  christos 
   2586      1.1  christos   if (!disable_packet_qfThreadInfo)
   2587      1.1  christos     {
   2588      1.1  christos       if (strcmp ("qfThreadInfo", own_buf) == 0)
   2589      1.1  christos 	{
   2590      1.1  christos 	  require_running_or_return (own_buf);
   2591      1.1  christos 	  thread_iter = all_threads.begin ();
   2592      1.1  christos 
   2593      1.1  christos 	  *own_buf++ = 'm';
   2594      1.1  christos 	  ptid_t ptid = (*thread_iter)->id;
   2595      1.1  christos 	  write_ptid (own_buf, ptid);
   2596      1.1  christos 	  thread_iter++;
   2597      1.1  christos 	  return;
   2598      1.1  christos 	}
   2599      1.1  christos 
   2600      1.1  christos       if (strcmp ("qsThreadInfo", own_buf) == 0)
   2601      1.1  christos 	{
   2602      1.1  christos 	  require_running_or_return (own_buf);
   2603      1.1  christos 	  if (thread_iter != all_threads.end ())
   2604      1.1  christos 	    {
   2605      1.1  christos 	      *own_buf++ = 'm';
   2606      1.1  christos 	      ptid_t ptid = (*thread_iter)->id;
   2607      1.1  christos 	      write_ptid (own_buf, ptid);
   2608      1.1  christos 	      thread_iter++;
   2609      1.1  christos 	      return;
   2610      1.1  christos 	    }
   2611      1.1  christos 	  else
   2612      1.1  christos 	    {
   2613      1.1  christos 	      sprintf (own_buf, "l");
   2614      1.1  christos 	      return;
   2615      1.1  christos 	    }
   2616      1.1  christos 	}
   2617      1.1  christos     }
   2618      1.1  christos 
   2619      1.1  christos   if (the_target->supports_read_offsets ()
   2620      1.1  christos       && strcmp ("qOffsets", own_buf) == 0)
   2621      1.1  christos     {
   2622      1.1  christos       CORE_ADDR text, data;
   2623      1.1  christos 
   2624      1.1  christos       require_running_or_return (own_buf);
   2625      1.1  christos       if (the_target->read_offsets (&text, &data))
   2626      1.1  christos 	sprintf (own_buf, "Text=%lX;Data=%lX;Bss=%lX",
   2627      1.1  christos 		 (long)text, (long)data, (long)data);
   2628      1.1  christos       else
   2629      1.1  christos 	write_enn (own_buf);
   2630      1.1  christos 
   2631      1.1  christos       return;
   2632      1.1  christos     }
   2633      1.1  christos 
   2634      1.1  christos   /* Protocol features query.  */
   2635      1.1  christos   if (startswith (own_buf, "qSupported")
   2636      1.1  christos       && (own_buf[10] == ':' || own_buf[10] == '\0'))
   2637      1.1  christos     {
   2638      1.1  christos       char *p = &own_buf[10];
   2639      1.1  christos       int gdb_supports_qRelocInsn = 0;
   2640      1.1  christos 
   2641      1.1  christos       /* Process each feature being provided by GDB.  The first
   2642      1.1  christos 	 feature will follow a ':', and latter features will follow
   2643      1.1  christos 	 ';'.  */
   2644      1.1  christos       if (*p == ':')
   2645      1.1  christos 	{
   2646      1.1  christos 	  std::vector<std::string> qsupported;
   2647      1.1  christos 	  std::vector<const char *> unknowns;
   2648      1.1  christos 
   2649      1.1  christos 	  /* Two passes, to avoid nested strtok calls in
   2650      1.1  christos 	     target_process_qsupported.  */
   2651      1.1  christos 	  char *saveptr;
   2652      1.1  christos 	  for (p = strtok_r (p + 1, ";", &saveptr);
   2653      1.1  christos 	       p != NULL;
   2654      1.1  christos 	       p = strtok_r (NULL, ";", &saveptr))
   2655      1.1  christos 	    qsupported.emplace_back (p);
   2656      1.1  christos 
   2657      1.1  christos 	  for (const std::string &feature : qsupported)
   2658      1.1  christos 	    {
   2659      1.1  christos 	      if (feature == "multiprocess+")
   2660      1.1  christos 		{
   2661      1.1  christos 		  /* GDB supports and wants multi-process support if
   2662      1.1  christos 		     possible.  */
   2663      1.1  christos 		  if (target_supports_multi_process ())
   2664      1.1  christos 		    cs.multi_process = 1;
   2665      1.1  christos 		}
   2666      1.1  christos 	      else if (feature == "qRelocInsn+")
   2667      1.1  christos 		{
   2668      1.1  christos 		  /* GDB supports relocate instruction requests.  */
   2669      1.1  christos 		  gdb_supports_qRelocInsn = 1;
   2670      1.1  christos 		}
   2671      1.1  christos 	      else if (feature == "swbreak+")
   2672      1.1  christos 		{
   2673      1.1  christos 		  /* GDB wants us to report whether a trap is caused
   2674      1.1  christos 		     by a software breakpoint and for us to handle PC
   2675      1.1  christos 		     adjustment if necessary on this target.  */
   2676      1.1  christos 		  if (target_supports_stopped_by_sw_breakpoint ())
   2677      1.1  christos 		    cs.swbreak_feature = 1;
   2678      1.1  christos 		}
   2679      1.1  christos 	      else if (feature == "hwbreak+")
   2680      1.1  christos 		{
   2681      1.1  christos 		  /* GDB wants us to report whether a trap is caused
   2682      1.1  christos 		     by a hardware breakpoint.  */
   2683      1.1  christos 		  if (target_supports_stopped_by_hw_breakpoint ())
   2684      1.1  christos 		    cs.hwbreak_feature = 1;
   2685      1.1  christos 		}
   2686      1.1  christos 	      else if (feature == "fork-events+")
   2687      1.1  christos 		{
   2688      1.1  christos 		  /* GDB supports and wants fork events if possible.  */
   2689      1.1  christos 		  if (target_supports_fork_events ())
   2690      1.1  christos 		    cs.report_fork_events = 1;
   2691      1.1  christos 		}
   2692      1.1  christos 	      else if (feature == "vfork-events+")
   2693      1.1  christos 		{
   2694      1.1  christos 		  /* GDB supports and wants vfork events if possible.  */
   2695      1.1  christos 		  if (target_supports_vfork_events ())
   2696      1.1  christos 		    cs.report_vfork_events = 1;
   2697      1.1  christos 		}
   2698      1.1  christos 	      else if (feature == "exec-events+")
   2699      1.1  christos 		{
   2700      1.1  christos 		  /* GDB supports and wants exec events if possible.  */
   2701      1.1  christos 		  if (target_supports_exec_events ())
   2702      1.1  christos 		    cs.report_exec_events = 1;
   2703      1.1  christos 		}
   2704      1.1  christos 	      else if (feature == "vContSupported+")
   2705      1.1  christos 		cs.vCont_supported = 1;
   2706      1.1  christos 	      else if (feature == "QThreadEvents+")
   2707      1.1  christos 		;
   2708  1.1.1.3  christos 	      else if (feature == "QThreadOptions+")
   2709  1.1.1.3  christos 		;
   2710      1.1  christos 	      else if (feature == "no-resumed+")
   2711      1.1  christos 		{
   2712      1.1  christos 		  /* GDB supports and wants TARGET_WAITKIND_NO_RESUMED
   2713      1.1  christos 		     events.  */
   2714      1.1  christos 		  report_no_resumed = true;
   2715      1.1  christos 		}
   2716  1.1.1.2  christos 	      else if (feature == "memory-tagging+")
   2717  1.1.1.2  christos 		{
   2718  1.1.1.2  christos 		  /* GDB supports memory tagging features.  */
   2719  1.1.1.2  christos 		  if (target_supports_memory_tagging ())
   2720  1.1.1.2  christos 		    cs.memory_tagging_feature = true;
   2721  1.1.1.2  christos 		}
   2722      1.1  christos 	      else
   2723      1.1  christos 		{
   2724      1.1  christos 		  /* Move the unknown features all together.  */
   2725      1.1  christos 		  unknowns.push_back (feature.c_str ());
   2726      1.1  christos 		}
   2727      1.1  christos 	    }
   2728      1.1  christos 
   2729      1.1  christos 	  /* Give the target backend a chance to process the unknown
   2730      1.1  christos 	     features.  */
   2731      1.1  christos 	  target_process_qsupported (unknowns);
   2732      1.1  christos 	}
   2733      1.1  christos 
   2734      1.1  christos       sprintf (own_buf,
   2735      1.1  christos 	       "PacketSize=%x;QPassSignals+;QProgramSignals+;"
   2736      1.1  christos 	       "QStartupWithShell+;QEnvironmentHexEncoded+;"
   2737      1.1  christos 	       "QEnvironmentReset+;QEnvironmentUnset+;"
   2738      1.1  christos 	       "QSetWorkingDir+",
   2739      1.1  christos 	       PBUFSIZ - 1);
   2740      1.1  christos 
   2741      1.1  christos       if (target_supports_catch_syscall ())
   2742      1.1  christos 	strcat (own_buf, ";QCatchSyscalls+");
   2743      1.1  christos 
   2744      1.1  christos       if (the_target->supports_qxfer_libraries_svr4 ())
   2745      1.1  christos 	strcat (own_buf, ";qXfer:libraries-svr4:read+"
   2746      1.1  christos 		";augmented-libraries-svr4-read+");
   2747      1.1  christos       else
   2748      1.1  christos 	{
   2749      1.1  christos 	  /* We do not have any hook to indicate whether the non-SVR4 target
   2750      1.1  christos 	     backend supports qXfer:libraries:read, so always report it.  */
   2751      1.1  christos 	  strcat (own_buf, ";qXfer:libraries:read+");
   2752      1.1  christos 	}
   2753      1.1  christos 
   2754      1.1  christos       if (the_target->supports_read_auxv ())
   2755      1.1  christos 	strcat (own_buf, ";qXfer:auxv:read+");
   2756      1.1  christos 
   2757      1.1  christos       if (the_target->supports_qxfer_siginfo ())
   2758      1.1  christos 	strcat (own_buf, ";qXfer:siginfo:read+;qXfer:siginfo:write+");
   2759      1.1  christos 
   2760      1.1  christos       if (the_target->supports_read_loadmap ())
   2761      1.1  christos 	strcat (own_buf, ";qXfer:fdpic:read+");
   2762      1.1  christos 
   2763      1.1  christos       /* We always report qXfer:features:read, as targets may
   2764      1.1  christos 	 install XML files on a subsequent call to arch_setup.
   2765      1.1  christos 	 If we reported to GDB on startup that we don't support
   2766      1.1  christos 	 qXfer:feature:read at all, we will never be re-queried.  */
   2767      1.1  christos       strcat (own_buf, ";qXfer:features:read+");
   2768      1.1  christos 
   2769      1.1  christos       if (cs.transport_is_reliable)
   2770      1.1  christos 	strcat (own_buf, ";QStartNoAckMode+");
   2771      1.1  christos 
   2772      1.1  christos       if (the_target->supports_qxfer_osdata ())
   2773      1.1  christos 	strcat (own_buf, ";qXfer:osdata:read+");
   2774      1.1  christos 
   2775      1.1  christos       if (target_supports_multi_process ())
   2776      1.1  christos 	strcat (own_buf, ";multiprocess+");
   2777      1.1  christos 
   2778      1.1  christos       if (target_supports_fork_events ())
   2779      1.1  christos 	strcat (own_buf, ";fork-events+");
   2780      1.1  christos 
   2781      1.1  christos       if (target_supports_vfork_events ())
   2782      1.1  christos 	strcat (own_buf, ";vfork-events+");
   2783      1.1  christos 
   2784      1.1  christos       if (target_supports_exec_events ())
   2785      1.1  christos 	strcat (own_buf, ";exec-events+");
   2786      1.1  christos 
   2787      1.1  christos       if (target_supports_non_stop ())
   2788      1.1  christos 	strcat (own_buf, ";QNonStop+");
   2789      1.1  christos 
   2790      1.1  christos       if (target_supports_disable_randomization ())
   2791      1.1  christos 	strcat (own_buf, ";QDisableRandomization+");
   2792      1.1  christos 
   2793      1.1  christos       strcat (own_buf, ";qXfer:threads:read+");
   2794      1.1  christos 
   2795      1.1  christos       if (target_supports_tracepoints ())
   2796      1.1  christos 	{
   2797      1.1  christos 	  strcat (own_buf, ";ConditionalTracepoints+");
   2798      1.1  christos 	  strcat (own_buf, ";TraceStateVariables+");
   2799      1.1  christos 	  strcat (own_buf, ";TracepointSource+");
   2800      1.1  christos 	  strcat (own_buf, ";DisconnectedTracing+");
   2801      1.1  christos 	  if (gdb_supports_qRelocInsn && target_supports_fast_tracepoints ())
   2802      1.1  christos 	    strcat (own_buf, ";FastTracepoints+");
   2803      1.1  christos 	  strcat (own_buf, ";StaticTracepoints+");
   2804      1.1  christos 	  strcat (own_buf, ";InstallInTrace+");
   2805      1.1  christos 	  strcat (own_buf, ";qXfer:statictrace:read+");
   2806      1.1  christos 	  strcat (own_buf, ";qXfer:traceframe-info:read+");
   2807      1.1  christos 	  strcat (own_buf, ";EnableDisableTracepoints+");
   2808      1.1  christos 	  strcat (own_buf, ";QTBuffer:size+");
   2809      1.1  christos 	  strcat (own_buf, ";tracenz+");
   2810      1.1  christos 	}
   2811      1.1  christos 
   2812      1.1  christos       if (target_supports_hardware_single_step ()
   2813      1.1  christos 	  || target_supports_software_single_step () )
   2814      1.1  christos 	{
   2815      1.1  christos 	  strcat (own_buf, ";ConditionalBreakpoints+");
   2816      1.1  christos 	}
   2817      1.1  christos       strcat (own_buf, ";BreakpointCommands+");
   2818      1.1  christos 
   2819      1.1  christos       if (target_supports_agent ())
   2820      1.1  christos 	strcat (own_buf, ";QAgent+");
   2821      1.1  christos 
   2822  1.1.1.2  christos       if (the_target->supports_btrace ())
   2823  1.1.1.2  christos 	supported_btrace_packets (own_buf);
   2824      1.1  christos 
   2825      1.1  christos       if (target_supports_stopped_by_sw_breakpoint ())
   2826      1.1  christos 	strcat (own_buf, ";swbreak+");
   2827      1.1  christos 
   2828      1.1  christos       if (target_supports_stopped_by_hw_breakpoint ())
   2829      1.1  christos 	strcat (own_buf, ";hwbreak+");
   2830      1.1  christos 
   2831      1.1  christos       if (the_target->supports_pid_to_exec_file ())
   2832      1.1  christos 	strcat (own_buf, ";qXfer:exec-file:read+");
   2833      1.1  christos 
   2834      1.1  christos       strcat (own_buf, ";vContSupported+");
   2835      1.1  christos 
   2836  1.1.1.3  christos       gdb_thread_options supported_options = target_supported_thread_options ();
   2837  1.1.1.3  christos       if (supported_options != 0)
   2838  1.1.1.3  christos 	{
   2839  1.1.1.3  christos 	  char *end_buf = own_buf + strlen (own_buf);
   2840  1.1.1.3  christos 	  sprintf (end_buf, ";QThreadOptions=%s",
   2841  1.1.1.3  christos 		   phex_nz (supported_options, sizeof (supported_options)));
   2842  1.1.1.3  christos 	}
   2843  1.1.1.3  christos 
   2844      1.1  christos       strcat (own_buf, ";QThreadEvents+");
   2845      1.1  christos 
   2846      1.1  christos       strcat (own_buf, ";no-resumed+");
   2847      1.1  christos 
   2848  1.1.1.2  christos       if (target_supports_memory_tagging ())
   2849  1.1.1.2  christos 	strcat (own_buf, ";memory-tagging+");
   2850  1.1.1.2  christos 
   2851      1.1  christos       /* Reinitialize components as needed for the new connection.  */
   2852      1.1  christos       hostio_handle_new_gdb_connection ();
   2853      1.1  christos       target_handle_new_gdb_connection ();
   2854      1.1  christos 
   2855      1.1  christos       return;
   2856      1.1  christos     }
   2857      1.1  christos 
   2858      1.1  christos   /* Thread-local storage support.  */
   2859      1.1  christos   if (the_target->supports_get_tls_address ()
   2860      1.1  christos       && startswith (own_buf, "qGetTLSAddr:"))
   2861      1.1  christos     {
   2862      1.1  christos       char *p = own_buf + 12;
   2863      1.1  christos       CORE_ADDR parts[2], address = 0;
   2864      1.1  christos       int i, err;
   2865      1.1  christos       ptid_t ptid = null_ptid;
   2866      1.1  christos 
   2867      1.1  christos       require_running_or_return (own_buf);
   2868      1.1  christos 
   2869      1.1  christos       for (i = 0; i < 3; i++)
   2870      1.1  christos 	{
   2871      1.1  christos 	  char *p2;
   2872      1.1  christos 	  int len;
   2873      1.1  christos 
   2874      1.1  christos 	  if (p == NULL)
   2875      1.1  christos 	    break;
   2876      1.1  christos 
   2877      1.1  christos 	  p2 = strchr (p, ',');
   2878      1.1  christos 	  if (p2)
   2879      1.1  christos 	    {
   2880      1.1  christos 	      len = p2 - p;
   2881      1.1  christos 	      p2++;
   2882      1.1  christos 	    }
   2883      1.1  christos 	  else
   2884      1.1  christos 	    {
   2885      1.1  christos 	      len = strlen (p);
   2886      1.1  christos 	      p2 = NULL;
   2887      1.1  christos 	    }
   2888      1.1  christos 
   2889      1.1  christos 	  if (i == 0)
   2890      1.1  christos 	    ptid = read_ptid (p, NULL);
   2891      1.1  christos 	  else
   2892      1.1  christos 	    decode_address (&parts[i - 1], p, len);
   2893      1.1  christos 	  p = p2;
   2894      1.1  christos 	}
   2895      1.1  christos 
   2896      1.1  christos       if (p != NULL || i < 3)
   2897      1.1  christos 	err = 1;
   2898      1.1  christos       else
   2899      1.1  christos 	{
   2900      1.1  christos 	  struct thread_info *thread = find_thread_ptid (ptid);
   2901      1.1  christos 
   2902      1.1  christos 	  if (thread == NULL)
   2903      1.1  christos 	    err = 2;
   2904      1.1  christos 	  else
   2905      1.1  christos 	    err = the_target->get_tls_address (thread, parts[0], parts[1],
   2906      1.1  christos 					       &address);
   2907      1.1  christos 	}
   2908      1.1  christos 
   2909      1.1  christos       if (err == 0)
   2910      1.1  christos 	{
   2911      1.1  christos 	  strcpy (own_buf, paddress(address));
   2912      1.1  christos 	  return;
   2913      1.1  christos 	}
   2914      1.1  christos       else if (err > 0)
   2915      1.1  christos 	{
   2916      1.1  christos 	  write_enn (own_buf);
   2917      1.1  christos 	  return;
   2918      1.1  christos 	}
   2919      1.1  christos 
   2920      1.1  christos       /* Otherwise, pretend we do not understand this packet.  */
   2921      1.1  christos     }
   2922      1.1  christos 
   2923      1.1  christos   /* Windows OS Thread Information Block address support.  */
   2924      1.1  christos   if (the_target->supports_get_tib_address ()
   2925      1.1  christos       && startswith (own_buf, "qGetTIBAddr:"))
   2926      1.1  christos     {
   2927      1.1  christos       const char *annex;
   2928      1.1  christos       int n;
   2929      1.1  christos       CORE_ADDR tlb;
   2930      1.1  christos       ptid_t ptid = read_ptid (own_buf + 12, &annex);
   2931      1.1  christos 
   2932      1.1  christos       n = the_target->get_tib_address (ptid, &tlb);
   2933      1.1  christos       if (n == 1)
   2934      1.1  christos 	{
   2935      1.1  christos 	  strcpy (own_buf, paddress(tlb));
   2936      1.1  christos 	  return;
   2937      1.1  christos 	}
   2938      1.1  christos       else if (n == 0)
   2939      1.1  christos 	{
   2940      1.1  christos 	  write_enn (own_buf);
   2941      1.1  christos 	  return;
   2942      1.1  christos 	}
   2943      1.1  christos       return;
   2944      1.1  christos     }
   2945      1.1  christos 
   2946      1.1  christos   /* Handle "monitor" commands.  */
   2947      1.1  christos   if (startswith (own_buf, "qRcmd,"))
   2948      1.1  christos     {
   2949      1.1  christos       char *mon = (char *) malloc (PBUFSIZ);
   2950      1.1  christos       int len = strlen (own_buf + 6);
   2951      1.1  christos 
   2952      1.1  christos       if (mon == NULL)
   2953      1.1  christos 	{
   2954      1.1  christos 	  write_enn (own_buf);
   2955      1.1  christos 	  return;
   2956      1.1  christos 	}
   2957      1.1  christos 
   2958      1.1  christos       if ((len % 2) != 0
   2959      1.1  christos 	  || hex2bin (own_buf + 6, (gdb_byte *) mon, len / 2) != len / 2)
   2960      1.1  christos 	{
   2961      1.1  christos 	  write_enn (own_buf);
   2962      1.1  christos 	  free (mon);
   2963      1.1  christos 	  return;
   2964      1.1  christos 	}
   2965      1.1  christos       mon[len / 2] = '\0';
   2966      1.1  christos 
   2967      1.1  christos       write_ok (own_buf);
   2968      1.1  christos 
   2969      1.1  christos       if (the_target->handle_monitor_command (mon) == 0)
   2970      1.1  christos 	/* Default processing.  */
   2971      1.1  christos 	handle_monitor_command (mon, own_buf);
   2972      1.1  christos 
   2973      1.1  christos       free (mon);
   2974      1.1  christos       return;
   2975      1.1  christos     }
   2976      1.1  christos 
   2977      1.1  christos   if (startswith (own_buf, "qSearch:memory:"))
   2978      1.1  christos     {
   2979      1.1  christos       require_running_or_return (own_buf);
   2980      1.1  christos       handle_search_memory (own_buf, packet_len);
   2981      1.1  christos       return;
   2982      1.1  christos     }
   2983      1.1  christos 
   2984      1.1  christos   if (strcmp (own_buf, "qAttached") == 0
   2985      1.1  christos       || startswith (own_buf, "qAttached:"))
   2986      1.1  christos     {
   2987      1.1  christos       struct process_info *process;
   2988      1.1  christos 
   2989      1.1  christos       if (own_buf[sizeof ("qAttached") - 1])
   2990      1.1  christos 	{
   2991      1.1  christos 	  int pid = strtoul (own_buf + sizeof ("qAttached:") - 1, NULL, 16);
   2992      1.1  christos 	  process = find_process_pid (pid);
   2993      1.1  christos 	}
   2994      1.1  christos       else
   2995      1.1  christos 	{
   2996      1.1  christos 	  require_running_or_return (own_buf);
   2997      1.1  christos 	  process = current_process ();
   2998      1.1  christos 	}
   2999      1.1  christos 
   3000      1.1  christos       if (process == NULL)
   3001      1.1  christos 	{
   3002      1.1  christos 	  write_enn (own_buf);
   3003      1.1  christos 	  return;
   3004      1.1  christos 	}
   3005      1.1  christos 
   3006      1.1  christos       strcpy (own_buf, process->attached ? "1" : "0");
   3007      1.1  christos       return;
   3008      1.1  christos     }
   3009      1.1  christos 
   3010      1.1  christos   if (startswith (own_buf, "qCRC:"))
   3011      1.1  christos     {
   3012      1.1  christos       /* CRC check (compare-section).  */
   3013      1.1  christos       const char *comma;
   3014      1.1  christos       ULONGEST base;
   3015      1.1  christos       int len;
   3016      1.1  christos       unsigned long long crc;
   3017      1.1  christos 
   3018      1.1  christos       require_running_or_return (own_buf);
   3019      1.1  christos       comma = unpack_varlen_hex (own_buf + 5, &base);
   3020      1.1  christos       if (*comma++ != ',')
   3021      1.1  christos 	{
   3022      1.1  christos 	  write_enn (own_buf);
   3023      1.1  christos 	  return;
   3024      1.1  christos 	}
   3025      1.1  christos       len = strtoul (comma, NULL, 16);
   3026      1.1  christos       crc = crc32 (base, len, 0xffffffff);
   3027      1.1  christos       /* Check for memory failure.  */
   3028      1.1  christos       if (crc == (unsigned long long) -1)
   3029      1.1  christos 	{
   3030      1.1  christos 	  write_enn (own_buf);
   3031      1.1  christos 	  return;
   3032      1.1  christos 	}
   3033      1.1  christos       sprintf (own_buf, "C%lx", (unsigned long) crc);
   3034      1.1  christos       return;
   3035      1.1  christos     }
   3036      1.1  christos 
   3037      1.1  christos   if (handle_qxfer (own_buf, packet_len, new_packet_len_p))
   3038      1.1  christos     return;
   3039      1.1  christos 
   3040      1.1  christos   if (target_supports_tracepoints () && handle_tracepoint_query (own_buf))
   3041      1.1  christos     return;
   3042      1.1  christos 
   3043  1.1.1.2  christos   /* Handle fetch memory tags packets.  */
   3044  1.1.1.2  christos   if (startswith (own_buf, "qMemTags:")
   3045  1.1.1.2  christos       && target_supports_memory_tagging ())
   3046  1.1.1.2  christos     {
   3047  1.1.1.2  christos       gdb::byte_vector tags;
   3048  1.1.1.2  christos       CORE_ADDR addr = 0;
   3049  1.1.1.2  christos       size_t len = 0;
   3050  1.1.1.2  christos       int type = 0;
   3051  1.1.1.2  christos 
   3052  1.1.1.2  christos       require_running_or_return (own_buf);
   3053  1.1.1.2  christos 
   3054  1.1.1.2  christos       parse_fetch_memtags_request (own_buf, &addr, &len, &type);
   3055  1.1.1.2  christos 
   3056  1.1.1.2  christos       bool ret = the_target->fetch_memtags (addr, len, tags, type);
   3057  1.1.1.2  christos 
   3058  1.1.1.2  christos       if (ret)
   3059  1.1.1.2  christos 	ret = create_fetch_memtags_reply (own_buf, tags);
   3060  1.1.1.2  christos 
   3061  1.1.1.2  christos       if (!ret)
   3062  1.1.1.2  christos 	write_enn (own_buf);
   3063  1.1.1.2  christos 
   3064  1.1.1.2  christos       *new_packet_len_p = strlen (own_buf);
   3065  1.1.1.2  christos       return;
   3066  1.1.1.2  christos     }
   3067  1.1.1.2  christos 
   3068      1.1  christos   /* Otherwise we didn't know what packet it was.  Say we didn't
   3069      1.1  christos      understand it.  */
   3070      1.1  christos   own_buf[0] = 0;
   3071      1.1  christos }
   3072      1.1  christos 
   3073      1.1  christos static void gdb_wants_all_threads_stopped (void);
   3074      1.1  christos static void resume (struct thread_resume *actions, size_t n);
   3075      1.1  christos 
   3076      1.1  christos /* The callback that is passed to visit_actioned_threads.  */
   3077      1.1  christos typedef int (visit_actioned_threads_callback_ftype)
   3078      1.1  christos   (const struct thread_resume *, struct thread_info *);
   3079      1.1  christos 
   3080      1.1  christos /* Call CALLBACK for any thread to which ACTIONS applies to.  Returns
   3081      1.1  christos    true if CALLBACK returns true.  Returns false if no matching thread
   3082      1.1  christos    is found or CALLBACK results false.
   3083      1.1  christos    Note: This function is itself a callback for find_thread.  */
   3084      1.1  christos 
   3085      1.1  christos static bool
   3086      1.1  christos visit_actioned_threads (thread_info *thread,
   3087      1.1  christos 			const struct thread_resume *actions,
   3088      1.1  christos 			size_t num_actions,
   3089      1.1  christos 			visit_actioned_threads_callback_ftype *callback)
   3090      1.1  christos {
   3091      1.1  christos   for (size_t i = 0; i < num_actions; i++)
   3092      1.1  christos     {
   3093      1.1  christos       const struct thread_resume *action = &actions[i];
   3094      1.1  christos 
   3095      1.1  christos       if (action->thread == minus_one_ptid
   3096      1.1  christos 	  || action->thread == thread->id
   3097      1.1  christos 	  || ((action->thread.pid ()
   3098      1.1  christos 	       == thread->id.pid ())
   3099      1.1  christos 	      && action->thread.lwp () == -1))
   3100      1.1  christos 	{
   3101      1.1  christos 	  if ((*callback) (action, thread))
   3102      1.1  christos 	    return true;
   3103      1.1  christos 	}
   3104      1.1  christos     }
   3105      1.1  christos 
   3106      1.1  christos   return false;
   3107      1.1  christos }
   3108      1.1  christos 
   3109      1.1  christos /* Callback for visit_actioned_threads.  If the thread has a pending
   3110      1.1  christos    status to report, report it now.  */
   3111      1.1  christos 
   3112      1.1  christos static int
   3113      1.1  christos handle_pending_status (const struct thread_resume *resumption,
   3114      1.1  christos 		       struct thread_info *thread)
   3115      1.1  christos {
   3116      1.1  christos   client_state &cs = get_client_state ();
   3117      1.1  christos   if (thread->status_pending_p)
   3118      1.1  christos     {
   3119      1.1  christos       thread->status_pending_p = 0;
   3120      1.1  christos 
   3121      1.1  christos       cs.last_status = thread->last_status;
   3122      1.1  christos       cs.last_ptid = thread->id;
   3123  1.1.1.2  christos       prepare_resume_reply (cs.own_buf, cs.last_ptid, cs.last_status);
   3124      1.1  christos       return 1;
   3125      1.1  christos     }
   3126      1.1  christos   return 0;
   3127      1.1  christos }
   3128      1.1  christos 
   3129      1.1  christos /* Parse vCont packets.  */
   3130      1.1  christos static void
   3131      1.1  christos handle_v_cont (char *own_buf)
   3132      1.1  christos {
   3133      1.1  christos   const char *p;
   3134      1.1  christos   int n = 0, i = 0;
   3135      1.1  christos   struct thread_resume *resume_info;
   3136      1.1  christos   struct thread_resume default_action { null_ptid };
   3137      1.1  christos 
   3138      1.1  christos   /* Count the number of semicolons in the packet.  There should be one
   3139      1.1  christos      for every action.  */
   3140      1.1  christos   p = &own_buf[5];
   3141      1.1  christos   while (p)
   3142      1.1  christos     {
   3143      1.1  christos       n++;
   3144      1.1  christos       p++;
   3145      1.1  christos       p = strchr (p, ';');
   3146      1.1  christos     }
   3147      1.1  christos 
   3148      1.1  christos   resume_info = (struct thread_resume *) malloc (n * sizeof (resume_info[0]));
   3149      1.1  christos   if (resume_info == NULL)
   3150      1.1  christos     goto err;
   3151      1.1  christos 
   3152      1.1  christos   p = &own_buf[5];
   3153      1.1  christos   while (*p)
   3154      1.1  christos     {
   3155      1.1  christos       p++;
   3156      1.1  christos 
   3157      1.1  christos       memset (&resume_info[i], 0, sizeof resume_info[i]);
   3158      1.1  christos 
   3159      1.1  christos       if (p[0] == 's' || p[0] == 'S')
   3160      1.1  christos 	resume_info[i].kind = resume_step;
   3161      1.1  christos       else if (p[0] == 'r')
   3162      1.1  christos 	resume_info[i].kind = resume_step;
   3163      1.1  christos       else if (p[0] == 'c' || p[0] == 'C')
   3164      1.1  christos 	resume_info[i].kind = resume_continue;
   3165      1.1  christos       else if (p[0] == 't')
   3166      1.1  christos 	resume_info[i].kind = resume_stop;
   3167      1.1  christos       else
   3168      1.1  christos 	goto err;
   3169      1.1  christos 
   3170      1.1  christos       if (p[0] == 'S' || p[0] == 'C')
   3171      1.1  christos 	{
   3172      1.1  christos 	  char *q;
   3173      1.1  christos 	  int sig = strtol (p + 1, &q, 16);
   3174      1.1  christos 	  if (p == q)
   3175      1.1  christos 	    goto err;
   3176      1.1  christos 	  p = q;
   3177      1.1  christos 
   3178      1.1  christos 	  if (!gdb_signal_to_host_p ((enum gdb_signal) sig))
   3179      1.1  christos 	    goto err;
   3180      1.1  christos 	  resume_info[i].sig = gdb_signal_to_host ((enum gdb_signal) sig);
   3181      1.1  christos 	}
   3182      1.1  christos       else if (p[0] == 'r')
   3183      1.1  christos 	{
   3184      1.1  christos 	  ULONGEST addr;
   3185      1.1  christos 
   3186      1.1  christos 	  p = unpack_varlen_hex (p + 1, &addr);
   3187      1.1  christos 	  resume_info[i].step_range_start = addr;
   3188      1.1  christos 
   3189      1.1  christos 	  if (*p != ',')
   3190      1.1  christos 	    goto err;
   3191      1.1  christos 
   3192      1.1  christos 	  p = unpack_varlen_hex (p + 1, &addr);
   3193      1.1  christos 	  resume_info[i].step_range_end = addr;
   3194      1.1  christos 	}
   3195      1.1  christos       else
   3196      1.1  christos 	{
   3197      1.1  christos 	  p = p + 1;
   3198      1.1  christos 	}
   3199      1.1  christos 
   3200      1.1  christos       if (p[0] == 0)
   3201      1.1  christos 	{
   3202      1.1  christos 	  resume_info[i].thread = minus_one_ptid;
   3203      1.1  christos 	  default_action = resume_info[i];
   3204      1.1  christos 
   3205      1.1  christos 	  /* Note: we don't increment i here, we'll overwrite this entry
   3206      1.1  christos 	     the next time through.  */
   3207      1.1  christos 	}
   3208      1.1  christos       else if (p[0] == ':')
   3209      1.1  christos 	{
   3210      1.1  christos 	  const char *q;
   3211      1.1  christos 	  ptid_t ptid = read_ptid (p + 1, &q);
   3212      1.1  christos 
   3213      1.1  christos 	  if (p == q)
   3214      1.1  christos 	    goto err;
   3215      1.1  christos 	  p = q;
   3216      1.1  christos 	  if (p[0] != ';' && p[0] != 0)
   3217      1.1  christos 	    goto err;
   3218      1.1  christos 
   3219      1.1  christos 	  resume_info[i].thread = ptid;
   3220      1.1  christos 
   3221      1.1  christos 	  i++;
   3222      1.1  christos 	}
   3223      1.1  christos     }
   3224      1.1  christos 
   3225      1.1  christos   if (i < n)
   3226      1.1  christos     resume_info[i] = default_action;
   3227      1.1  christos 
   3228      1.1  christos   resume (resume_info, n);
   3229      1.1  christos   free (resume_info);
   3230      1.1  christos   return;
   3231      1.1  christos 
   3232      1.1  christos err:
   3233      1.1  christos   write_enn (own_buf);
   3234      1.1  christos   free (resume_info);
   3235      1.1  christos   return;
   3236      1.1  christos }
   3237      1.1  christos 
   3238      1.1  christos /* Resume target with ACTIONS, an array of NUM_ACTIONS elements.  */
   3239      1.1  christos 
   3240      1.1  christos static void
   3241      1.1  christos resume (struct thread_resume *actions, size_t num_actions)
   3242      1.1  christos {
   3243      1.1  christos   client_state &cs = get_client_state ();
   3244      1.1  christos   if (!non_stop)
   3245      1.1  christos     {
   3246      1.1  christos       /* Check if among the threads that GDB wants actioned, there's
   3247      1.1  christos 	 one with a pending status to report.  If so, skip actually
   3248      1.1  christos 	 resuming/stopping and report the pending event
   3249      1.1  christos 	 immediately.  */
   3250      1.1  christos 
   3251      1.1  christos       thread_info *thread_with_status = find_thread ([&] (thread_info *thread)
   3252      1.1  christos 	{
   3253      1.1  christos 	  return visit_actioned_threads (thread, actions, num_actions,
   3254      1.1  christos 					 handle_pending_status);
   3255      1.1  christos 	});
   3256      1.1  christos 
   3257      1.1  christos       if (thread_with_status != NULL)
   3258      1.1  christos 	return;
   3259      1.1  christos 
   3260      1.1  christos       enable_async_io ();
   3261      1.1  christos     }
   3262      1.1  christos 
   3263      1.1  christos   the_target->resume (actions, num_actions);
   3264      1.1  christos 
   3265      1.1  christos   if (non_stop)
   3266      1.1  christos     write_ok (cs.own_buf);
   3267      1.1  christos   else
   3268      1.1  christos     {
   3269      1.1  christos       cs.last_ptid = mywait (minus_one_ptid, &cs.last_status, 0, 1);
   3270      1.1  christos 
   3271  1.1.1.2  christos       if (cs.last_status.kind () == TARGET_WAITKIND_NO_RESUMED
   3272      1.1  christos 	  && !report_no_resumed)
   3273      1.1  christos 	{
   3274      1.1  christos 	  /* The client does not support this stop reply.  At least
   3275      1.1  christos 	     return error.  */
   3276      1.1  christos 	  sprintf (cs.own_buf, "E.No unwaited-for children left.");
   3277      1.1  christos 	  disable_async_io ();
   3278      1.1  christos 	  return;
   3279      1.1  christos 	}
   3280      1.1  christos 
   3281  1.1.1.2  christos       if (cs.last_status.kind () != TARGET_WAITKIND_EXITED
   3282  1.1.1.2  christos 	  && cs.last_status.kind () != TARGET_WAITKIND_SIGNALLED
   3283  1.1.1.3  christos 	  && cs.last_status.kind () != TARGET_WAITKIND_THREAD_EXITED
   3284  1.1.1.2  christos 	  && cs.last_status.kind () != TARGET_WAITKIND_NO_RESUMED)
   3285      1.1  christos 	current_thread->last_status = cs.last_status;
   3286      1.1  christos 
   3287      1.1  christos       /* From the client's perspective, all-stop mode always stops all
   3288      1.1  christos 	 threads implicitly (and the target backend has already done
   3289      1.1  christos 	 so by now).  Tag all threads as "want-stopped", so we don't
   3290      1.1  christos 	 resume them implicitly without the client telling us to.  */
   3291      1.1  christos       gdb_wants_all_threads_stopped ();
   3292  1.1.1.2  christos       prepare_resume_reply (cs.own_buf, cs.last_ptid, cs.last_status);
   3293      1.1  christos       disable_async_io ();
   3294      1.1  christos 
   3295  1.1.1.2  christos       if (cs.last_status.kind () == TARGET_WAITKIND_EXITED
   3296  1.1.1.2  christos 	  || cs.last_status.kind () == TARGET_WAITKIND_SIGNALLED)
   3297  1.1.1.2  christos 	target_mourn_inferior (cs.last_ptid);
   3298      1.1  christos     }
   3299      1.1  christos }
   3300      1.1  christos 
   3301  1.1.1.2  christos /* Attach to a new program.  */
   3302  1.1.1.2  christos static void
   3303      1.1  christos handle_v_attach (char *own_buf)
   3304      1.1  christos {
   3305      1.1  christos   client_state &cs = get_client_state ();
   3306      1.1  christos 
   3307  1.1.1.3  christos   int pid = strtol (own_buf + 8, NULL, 16);
   3308      1.1  christos 
   3309  1.1.1.3  christos   try
   3310  1.1.1.3  christos     {
   3311  1.1.1.3  christos       if (attach_inferior (pid) == 0)
   3312      1.1  christos 	{
   3313  1.1.1.3  christos 	  /* Don't report shared library events after attaching, even if
   3314  1.1.1.3  christos 	     some libraries are preloaded.  GDB will always poll the
   3315  1.1.1.3  christos 	     library list.  Avoids the "stopped by shared library event"
   3316  1.1.1.3  christos 	     notice on the GDB side.  */
   3317  1.1.1.3  christos 	  current_process ()->dlls_changed = false;
   3318  1.1.1.3  christos 
   3319  1.1.1.3  christos 	  if (non_stop)
   3320  1.1.1.3  christos 	    {
   3321  1.1.1.3  christos 	      /* In non-stop, we don't send a resume reply.  Stop events
   3322  1.1.1.3  christos 		 will follow up using the normal notification
   3323  1.1.1.3  christos 		 mechanism.  */
   3324  1.1.1.3  christos 	      write_ok (own_buf);
   3325  1.1.1.3  christos 	    }
   3326  1.1.1.3  christos 	  else
   3327  1.1.1.3  christos 	    prepare_resume_reply (own_buf, cs.last_ptid, cs.last_status);
   3328      1.1  christos 	}
   3329      1.1  christos       else
   3330  1.1.1.3  christos 	{
   3331  1.1.1.3  christos 	  /* Not supported.  */
   3332  1.1.1.3  christos 	  own_buf[0] = 0;
   3333  1.1.1.3  christos 	}
   3334      1.1  christos     }
   3335  1.1.1.3  christos   catch (const gdb_exception_error &exception)
   3336  1.1.1.3  christos     {
   3337  1.1.1.3  christos       sprintf (own_buf, "E.%s", exception.what ());
   3338  1.1.1.3  christos     }
   3339  1.1.1.3  christos }
   3340  1.1.1.3  christos 
   3341  1.1.1.3  christos /* Decode an argument from the vRun packet buffer.  PTR points to the
   3342  1.1.1.3  christos    first hex-encoded character in the buffer, and LEN is the number of
   3343  1.1.1.3  christos    characters to read from the packet buffer.
   3344  1.1.1.3  christos 
   3345  1.1.1.3  christos    If the argument decoding is successful, return a buffer containing the
   3346  1.1.1.3  christos    decoded argument, including a null terminator at the end.
   3347  1.1.1.3  christos 
   3348  1.1.1.3  christos    If the argument decoding fails for any reason, return nullptr.  */
   3349  1.1.1.3  christos 
   3350  1.1.1.3  christos static gdb::unique_xmalloc_ptr<char>
   3351  1.1.1.3  christos decode_v_run_arg (const char *ptr, size_t len)
   3352  1.1.1.3  christos {
   3353  1.1.1.3  christos   /* Two hex characters are required for each decoded byte.  */
   3354  1.1.1.3  christos   if (len % 2 != 0)
   3355  1.1.1.3  christos     return nullptr;
   3356  1.1.1.3  christos 
   3357  1.1.1.3  christos   /* The length in bytes needed for the decoded argument.  */
   3358  1.1.1.3  christos   len /= 2;
   3359  1.1.1.3  christos 
   3360  1.1.1.3  christos   /* Buffer to decode the argument into.  The '+ 1' is for the null
   3361  1.1.1.3  christos      terminator we will add.  */
   3362  1.1.1.3  christos   char *arg = (char *) xmalloc (len + 1);
   3363  1.1.1.3  christos 
   3364  1.1.1.3  christos   /* Decode the argument from the packet and add a null terminator.  We do
   3365  1.1.1.3  christos      this within a try block as invalid characters within the PTR buffer
   3366  1.1.1.3  christos      will cause hex2bin to throw an exception.  Our caller relies on us
   3367  1.1.1.3  christos      returning nullptr in order to clean up some memory allocations.  */
   3368  1.1.1.3  christos   try
   3369  1.1.1.3  christos     {
   3370  1.1.1.3  christos       hex2bin (ptr, (gdb_byte *) arg, len);
   3371  1.1.1.3  christos       arg[len] = '\0';
   3372  1.1.1.3  christos     }
   3373  1.1.1.3  christos   catch (const gdb_exception_error &exception)
   3374  1.1.1.3  christos     {
   3375  1.1.1.3  christos       return nullptr;
   3376  1.1.1.3  christos     }
   3377  1.1.1.3  christos 
   3378  1.1.1.3  christos   return gdb::unique_xmalloc_ptr<char> (arg);
   3379      1.1  christos }
   3380      1.1  christos 
   3381  1.1.1.2  christos /* Run a new program.  */
   3382  1.1.1.2  christos static void
   3383      1.1  christos handle_v_run (char *own_buf)
   3384      1.1  christos {
   3385      1.1  christos   client_state &cs = get_client_state ();
   3386      1.1  christos   char *p, *next_p;
   3387      1.1  christos   std::vector<char *> new_argv;
   3388  1.1.1.3  christos   gdb::unique_xmalloc_ptr<char> new_program_name;
   3389  1.1.1.2  christos   int i;
   3390      1.1  christos 
   3391  1.1.1.3  christos   for (i = 0, p = own_buf + strlen ("vRun;");
   3392  1.1.1.3  christos        /* Exit condition is at the end of the loop.  */;
   3393  1.1.1.3  christos        p = next_p + 1, ++i)
   3394      1.1  christos     {
   3395      1.1  christos       next_p = strchr (p, ';');
   3396      1.1  christos       if (next_p == NULL)
   3397      1.1  christos 	next_p = p + strlen (p);
   3398      1.1  christos 
   3399      1.1  christos       if (i == 0 && p == next_p)
   3400      1.1  christos 	{
   3401      1.1  christos 	  /* No program specified.  */
   3402  1.1.1.3  christos 	  gdb_assert (new_program_name == nullptr);
   3403      1.1  christos 	}
   3404      1.1  christos       else if (p == next_p)
   3405      1.1  christos 	{
   3406      1.1  christos 	  /* Empty argument.  */
   3407      1.1  christos 	  new_argv.push_back (xstrdup (""));
   3408      1.1  christos 	}
   3409      1.1  christos       else
   3410      1.1  christos 	{
   3411  1.1.1.3  christos 	  /* The length of the argument string in the packet.  */
   3412  1.1.1.3  christos 	  size_t len = next_p - p;
   3413      1.1  christos 
   3414  1.1.1.3  christos 	  gdb::unique_xmalloc_ptr<char> arg = decode_v_run_arg (p, len);
   3415  1.1.1.3  christos 	  if (arg == nullptr)
   3416      1.1  christos 	    {
   3417  1.1.1.3  christos 	      write_enn (own_buf);
   3418  1.1.1.3  christos 	      free_vector_argv (new_argv);
   3419  1.1.1.3  christos 	      return;
   3420      1.1  christos 	    }
   3421      1.1  christos 
   3422      1.1  christos 	  if (i == 0)
   3423  1.1.1.3  christos 	    new_program_name = std::move (arg);
   3424      1.1  christos 	  else
   3425  1.1.1.3  christos 	    new_argv.push_back (arg.release ());
   3426      1.1  christos 	}
   3427  1.1.1.3  christos       if (*next_p == '\0')
   3428  1.1.1.3  christos 	break;
   3429      1.1  christos     }
   3430      1.1  christos 
   3431  1.1.1.3  christos   if (new_program_name == nullptr)
   3432      1.1  christos     {
   3433      1.1  christos       /* GDB didn't specify a program to run.  Use the program from the
   3434      1.1  christos 	 last run with the new argument list.  */
   3435  1.1.1.3  christos       if (program_path.get () == nullptr)
   3436      1.1  christos 	{
   3437      1.1  christos 	  write_enn (own_buf);
   3438      1.1  christos 	  free_vector_argv (new_argv);
   3439  1.1.1.2  christos 	  return;
   3440      1.1  christos 	}
   3441      1.1  christos     }
   3442      1.1  christos   else
   3443  1.1.1.3  christos     program_path.set (new_program_name.get ());
   3444      1.1  christos 
   3445      1.1  christos   /* Free the old argv and install the new one.  */
   3446      1.1  christos   free_vector_argv (program_args);
   3447      1.1  christos   program_args = new_argv;
   3448      1.1  christos 
   3449  1.1.1.3  christos   try
   3450  1.1.1.3  christos     {
   3451  1.1.1.3  christos       target_create_inferior (program_path.get (), program_args);
   3452  1.1.1.3  christos     }
   3453  1.1.1.3  christos   catch (const gdb_exception_error &exception)
   3454  1.1.1.3  christos     {
   3455  1.1.1.3  christos       sprintf (own_buf, "E.%s", exception.what ());
   3456  1.1.1.3  christos       return;
   3457  1.1.1.3  christos     }
   3458      1.1  christos 
   3459  1.1.1.2  christos   if (cs.last_status.kind () == TARGET_WAITKIND_STOPPED)
   3460      1.1  christos     {
   3461  1.1.1.2  christos       prepare_resume_reply (own_buf, cs.last_ptid, cs.last_status);
   3462      1.1  christos 
   3463      1.1  christos       /* In non-stop, sending a resume reply doesn't set the general
   3464      1.1  christos 	 thread, but GDB assumes a vRun sets it (this is so GDB can
   3465      1.1  christos 	 query which is the main thread of the new inferior.  */
   3466      1.1  christos       if (non_stop)
   3467      1.1  christos 	cs.general_thread = cs.last_ptid;
   3468      1.1  christos     }
   3469      1.1  christos   else
   3470  1.1.1.2  christos     write_enn (own_buf);
   3471      1.1  christos }
   3472      1.1  christos 
   3473  1.1.1.2  christos /* Kill process.  */
   3474  1.1.1.2  christos static void
   3475      1.1  christos handle_v_kill (char *own_buf)
   3476      1.1  christos {
   3477      1.1  christos   client_state &cs = get_client_state ();
   3478      1.1  christos   int pid;
   3479      1.1  christos   char *p = &own_buf[6];
   3480      1.1  christos   if (cs.multi_process)
   3481      1.1  christos     pid = strtol (p, NULL, 16);
   3482      1.1  christos   else
   3483      1.1  christos     pid = signal_pid;
   3484      1.1  christos 
   3485      1.1  christos   process_info *proc = find_process_pid (pid);
   3486      1.1  christos 
   3487      1.1  christos   if (proc != nullptr && kill_inferior (proc) == 0)
   3488      1.1  christos     {
   3489  1.1.1.2  christos       cs.last_status.set_signalled (GDB_SIGNAL_KILL);
   3490      1.1  christos       cs.last_ptid = ptid_t (pid);
   3491      1.1  christos       discard_queued_stop_replies (cs.last_ptid);
   3492      1.1  christos       write_ok (own_buf);
   3493      1.1  christos     }
   3494      1.1  christos   else
   3495  1.1.1.2  christos     write_enn (own_buf);
   3496      1.1  christos }
   3497      1.1  christos 
   3498      1.1  christos /* Handle all of the extended 'v' packets.  */
   3499      1.1  christos void
   3500      1.1  christos handle_v_requests (char *own_buf, int packet_len, int *new_packet_len)
   3501      1.1  christos {
   3502      1.1  christos   client_state &cs = get_client_state ();
   3503      1.1  christos   if (!disable_packet_vCont)
   3504      1.1  christos     {
   3505      1.1  christos       if (strcmp (own_buf, "vCtrlC") == 0)
   3506      1.1  christos 	{
   3507      1.1  christos 	  the_target->request_interrupt ();
   3508      1.1  christos 	  write_ok (own_buf);
   3509      1.1  christos 	  return;
   3510      1.1  christos 	}
   3511      1.1  christos 
   3512      1.1  christos       if (startswith (own_buf, "vCont;"))
   3513      1.1  christos 	{
   3514      1.1  christos 	  handle_v_cont (own_buf);
   3515      1.1  christos 	  return;
   3516      1.1  christos 	}
   3517      1.1  christos 
   3518      1.1  christos       if (startswith (own_buf, "vCont?"))
   3519      1.1  christos 	{
   3520      1.1  christos 	  strcpy (own_buf, "vCont;c;C;t");
   3521      1.1  christos 
   3522      1.1  christos 	  if (target_supports_hardware_single_step ()
   3523      1.1  christos 	      || target_supports_software_single_step ()
   3524      1.1  christos 	      || !cs.vCont_supported)
   3525      1.1  christos 	    {
   3526      1.1  christos 	      /* If target supports single step either by hardware or by
   3527      1.1  christos 		 software, add actions s and S to the list of supported
   3528      1.1  christos 		 actions.  On the other hand, if GDB doesn't request the
   3529      1.1  christos 		 supported vCont actions in qSupported packet, add s and
   3530      1.1  christos 		 S to the list too.  */
   3531      1.1  christos 	      own_buf = own_buf + strlen (own_buf);
   3532      1.1  christos 	      strcpy (own_buf, ";s;S");
   3533      1.1  christos 	    }
   3534      1.1  christos 
   3535      1.1  christos 	  if (target_supports_range_stepping ())
   3536      1.1  christos 	    {
   3537      1.1  christos 	      own_buf = own_buf + strlen (own_buf);
   3538      1.1  christos 	      strcpy (own_buf, ";r");
   3539      1.1  christos 	    }
   3540      1.1  christos 	  return;
   3541      1.1  christos 	}
   3542      1.1  christos     }
   3543      1.1  christos 
   3544      1.1  christos   if (startswith (own_buf, "vFile:")
   3545      1.1  christos       && handle_vFile (own_buf, packet_len, new_packet_len))
   3546      1.1  christos     return;
   3547      1.1  christos 
   3548      1.1  christos   if (startswith (own_buf, "vAttach;"))
   3549      1.1  christos     {
   3550      1.1  christos       if ((!extended_protocol || !cs.multi_process) && target_running ())
   3551      1.1  christos 	{
   3552      1.1  christos 	  fprintf (stderr, "Already debugging a process\n");
   3553      1.1  christos 	  write_enn (own_buf);
   3554      1.1  christos 	  return;
   3555      1.1  christos 	}
   3556      1.1  christos       handle_v_attach (own_buf);
   3557      1.1  christos       return;
   3558      1.1  christos     }
   3559      1.1  christos 
   3560      1.1  christos   if (startswith (own_buf, "vRun;"))
   3561      1.1  christos     {
   3562      1.1  christos       if ((!extended_protocol || !cs.multi_process) && target_running ())
   3563      1.1  christos 	{
   3564      1.1  christos 	  fprintf (stderr, "Already debugging a process\n");
   3565      1.1  christos 	  write_enn (own_buf);
   3566      1.1  christos 	  return;
   3567      1.1  christos 	}
   3568      1.1  christos       handle_v_run (own_buf);
   3569      1.1  christos       return;
   3570      1.1  christos     }
   3571      1.1  christos 
   3572      1.1  christos   if (startswith (own_buf, "vKill;"))
   3573      1.1  christos     {
   3574      1.1  christos       if (!target_running ())
   3575      1.1  christos 	{
   3576      1.1  christos 	  fprintf (stderr, "No process to kill\n");
   3577      1.1  christos 	  write_enn (own_buf);
   3578      1.1  christos 	  return;
   3579      1.1  christos 	}
   3580      1.1  christos       handle_v_kill (own_buf);
   3581      1.1  christos       return;
   3582      1.1  christos     }
   3583      1.1  christos 
   3584      1.1  christos   if (handle_notif_ack (own_buf, packet_len))
   3585      1.1  christos     return;
   3586      1.1  christos 
   3587      1.1  christos   /* Otherwise we didn't know what packet it was.  Say we didn't
   3588      1.1  christos      understand it.  */
   3589      1.1  christos   own_buf[0] = 0;
   3590      1.1  christos   return;
   3591      1.1  christos }
   3592      1.1  christos 
   3593      1.1  christos /* Resume thread and wait for another event.  In non-stop mode,
   3594  1.1.1.3  christos    don't really wait here, but return immediately to the event
   3595      1.1  christos    loop.  */
   3596      1.1  christos static void
   3597      1.1  christos myresume (char *own_buf, int step, int sig)
   3598      1.1  christos {
   3599      1.1  christos   client_state &cs = get_client_state ();
   3600      1.1  christos   struct thread_resume resume_info[2];
   3601      1.1  christos   int n = 0;
   3602      1.1  christos   int valid_cont_thread;
   3603      1.1  christos 
   3604      1.1  christos   valid_cont_thread = (cs.cont_thread != null_ptid
   3605      1.1  christos 			 && cs.cont_thread != minus_one_ptid);
   3606      1.1  christos 
   3607      1.1  christos   if (step || sig || valid_cont_thread)
   3608      1.1  christos     {
   3609      1.1  christos       resume_info[0].thread = current_ptid;
   3610      1.1  christos       if (step)
   3611      1.1  christos 	resume_info[0].kind = resume_step;
   3612      1.1  christos       else
   3613      1.1  christos 	resume_info[0].kind = resume_continue;
   3614      1.1  christos       resume_info[0].sig = sig;
   3615      1.1  christos       n++;
   3616      1.1  christos     }
   3617      1.1  christos 
   3618      1.1  christos   if (!valid_cont_thread)
   3619      1.1  christos     {
   3620      1.1  christos       resume_info[n].thread = minus_one_ptid;
   3621      1.1  christos       resume_info[n].kind = resume_continue;
   3622      1.1  christos       resume_info[n].sig = 0;
   3623      1.1  christos       n++;
   3624      1.1  christos     }
   3625      1.1  christos 
   3626      1.1  christos   resume (resume_info, n);
   3627      1.1  christos }
   3628      1.1  christos 
   3629      1.1  christos /* Callback for for_each_thread.  Make a new stop reply for each
   3630      1.1  christos    stopped thread.  */
   3631      1.1  christos 
   3632      1.1  christos static void
   3633      1.1  christos queue_stop_reply_callback (thread_info *thread)
   3634      1.1  christos {
   3635      1.1  christos   /* For now, assume targets that don't have this callback also don't
   3636      1.1  christos      manage the thread's last_status field.  */
   3637      1.1  christos   if (!the_target->supports_thread_stopped ())
   3638      1.1  christos     {
   3639      1.1  christos       struct vstop_notif *new_notif = new struct vstop_notif;
   3640      1.1  christos 
   3641      1.1  christos       new_notif->ptid = thread->id;
   3642      1.1  christos       new_notif->status = thread->last_status;
   3643      1.1  christos       /* Pass the last stop reply back to GDB, but don't notify
   3644      1.1  christos 	 yet.  */
   3645      1.1  christos       notif_event_enque (&notif_stop, new_notif);
   3646      1.1  christos     }
   3647      1.1  christos   else
   3648      1.1  christos     {
   3649      1.1  christos       if (target_thread_stopped (thread))
   3650      1.1  christos 	{
   3651  1.1.1.2  christos 	  threads_debug_printf
   3652  1.1.1.2  christos 	    ("Reporting thread %s as already stopped with %s",
   3653  1.1.1.2  christos 	     target_pid_to_str (thread->id).c_str (),
   3654  1.1.1.2  christos 	     thread->last_status.to_string ().c_str ());
   3655      1.1  christos 
   3656  1.1.1.2  christos 	  gdb_assert (thread->last_status.kind () != TARGET_WAITKIND_IGNORE);
   3657      1.1  christos 
   3658      1.1  christos 	  /* Pass the last stop reply back to GDB, but don't notify
   3659      1.1  christos 	     yet.  */
   3660  1.1.1.2  christos 	  queue_stop_reply (thread->id, thread->last_status);
   3661      1.1  christos 	}
   3662      1.1  christos     }
   3663      1.1  christos }
   3664      1.1  christos 
   3665      1.1  christos /* Set this inferior threads's state as "want-stopped".  We won't
   3666      1.1  christos    resume this thread until the client gives us another action for
   3667      1.1  christos    it.  */
   3668      1.1  christos 
   3669      1.1  christos static void
   3670      1.1  christos gdb_wants_thread_stopped (thread_info *thread)
   3671      1.1  christos {
   3672      1.1  christos   thread->last_resume_kind = resume_stop;
   3673      1.1  christos 
   3674  1.1.1.2  christos   if (thread->last_status.kind () == TARGET_WAITKIND_IGNORE)
   3675      1.1  christos     {
   3676      1.1  christos       /* Most threads are stopped implicitly (all-stop); tag that with
   3677      1.1  christos 	 signal 0.  */
   3678  1.1.1.2  christos       thread->last_status.set_stopped (GDB_SIGNAL_0);
   3679      1.1  christos     }
   3680      1.1  christos }
   3681      1.1  christos 
   3682      1.1  christos /* Set all threads' states as "want-stopped".  */
   3683      1.1  christos 
   3684      1.1  christos static void
   3685      1.1  christos gdb_wants_all_threads_stopped (void)
   3686      1.1  christos {
   3687      1.1  christos   for_each_thread (gdb_wants_thread_stopped);
   3688      1.1  christos }
   3689      1.1  christos 
   3690      1.1  christos /* Callback for for_each_thread.  If the thread is stopped with an
   3691      1.1  christos    interesting event, mark it as having a pending event.  */
   3692      1.1  christos 
   3693      1.1  christos static void
   3694      1.1  christos set_pending_status_callback (thread_info *thread)
   3695      1.1  christos {
   3696  1.1.1.2  christos   if (thread->last_status.kind () != TARGET_WAITKIND_STOPPED
   3697  1.1.1.2  christos       || (thread->last_status.sig () != GDB_SIGNAL_0
   3698      1.1  christos 	  /* A breakpoint, watchpoint or finished step from a previous
   3699      1.1  christos 	     GDB run isn't considered interesting for a new GDB run.
   3700      1.1  christos 	     If we left those pending, the new GDB could consider them
   3701      1.1  christos 	     random SIGTRAPs.  This leaves out real async traps.  We'd
   3702      1.1  christos 	     have to peek into the (target-specific) siginfo to
   3703      1.1  christos 	     distinguish those.  */
   3704  1.1.1.2  christos 	  && thread->last_status.sig () != GDB_SIGNAL_TRAP))
   3705      1.1  christos     thread->status_pending_p = 1;
   3706      1.1  christos }
   3707      1.1  christos 
   3708      1.1  christos /* Status handler for the '?' packet.  */
   3709      1.1  christos 
   3710      1.1  christos static void
   3711      1.1  christos handle_status (char *own_buf)
   3712      1.1  christos {
   3713      1.1  christos   client_state &cs = get_client_state ();
   3714      1.1  christos 
   3715      1.1  christos   /* GDB is connected, don't forward events to the target anymore.  */
   3716      1.1  christos   for_each_process ([] (process_info *process) {
   3717      1.1  christos     process->gdb_detached = 0;
   3718      1.1  christos   });
   3719      1.1  christos 
   3720      1.1  christos   /* In non-stop mode, we must send a stop reply for each stopped
   3721      1.1  christos      thread.  In all-stop mode, just send one for the first stopped
   3722      1.1  christos      thread we find.  */
   3723      1.1  christos 
   3724      1.1  christos   if (non_stop)
   3725      1.1  christos     {
   3726      1.1  christos       for_each_thread (queue_stop_reply_callback);
   3727      1.1  christos 
   3728      1.1  christos       /* The first is sent immediatly.  OK is sent if there is no
   3729      1.1  christos 	 stopped thread, which is the same handling of the vStopped
   3730      1.1  christos 	 packet (by design).  */
   3731      1.1  christos       notif_write_event (&notif_stop, cs.own_buf);
   3732      1.1  christos     }
   3733      1.1  christos   else
   3734      1.1  christos     {
   3735      1.1  christos       thread_info *thread = NULL;
   3736      1.1  christos 
   3737      1.1  christos       target_pause_all (false);
   3738      1.1  christos       target_stabilize_threads ();
   3739      1.1  christos       gdb_wants_all_threads_stopped ();
   3740      1.1  christos 
   3741      1.1  christos       /* We can only report one status, but we might be coming out of
   3742      1.1  christos 	 non-stop -- if more than one thread is stopped with
   3743      1.1  christos 	 interesting events, leave events for the threads we're not
   3744      1.1  christos 	 reporting now pending.  They'll be reported the next time the
   3745      1.1  christos 	 threads are resumed.  Start by marking all interesting events
   3746      1.1  christos 	 as pending.  */
   3747      1.1  christos       for_each_thread (set_pending_status_callback);
   3748      1.1  christos 
   3749      1.1  christos       /* Prefer the last thread that reported an event to GDB (even if
   3750      1.1  christos 	 that was a GDB_SIGNAL_TRAP).  */
   3751  1.1.1.2  christos       if (cs.last_status.kind () != TARGET_WAITKIND_IGNORE
   3752  1.1.1.2  christos 	  && cs.last_status.kind () != TARGET_WAITKIND_EXITED
   3753  1.1.1.2  christos 	  && cs.last_status.kind () != TARGET_WAITKIND_SIGNALLED)
   3754      1.1  christos 	thread = find_thread_ptid (cs.last_ptid);
   3755      1.1  christos 
   3756      1.1  christos       /* If the last event thread is not found for some reason, look
   3757      1.1  christos 	 for some other thread that might have an event to report.  */
   3758      1.1  christos       if (thread == NULL)
   3759      1.1  christos 	thread = find_thread ([] (thread_info *thr_arg)
   3760      1.1  christos 	  {
   3761      1.1  christos 	    return thr_arg->status_pending_p;
   3762      1.1  christos 	  });
   3763      1.1  christos 
   3764      1.1  christos       /* If we're still out of luck, simply pick the first thread in
   3765      1.1  christos 	 the thread list.  */
   3766      1.1  christos       if (thread == NULL)
   3767      1.1  christos 	thread = get_first_thread ();
   3768      1.1  christos 
   3769      1.1  christos       if (thread != NULL)
   3770      1.1  christos 	{
   3771      1.1  christos 	  struct thread_info *tp = (struct thread_info *) thread;
   3772      1.1  christos 
   3773      1.1  christos 	  /* We're reporting this event, so it's no longer
   3774      1.1  christos 	     pending.  */
   3775      1.1  christos 	  tp->status_pending_p = 0;
   3776      1.1  christos 
   3777      1.1  christos 	  /* GDB assumes the current thread is the thread we're
   3778      1.1  christos 	     reporting the status for.  */
   3779      1.1  christos 	  cs.general_thread = thread->id;
   3780      1.1  christos 	  set_desired_thread ();
   3781      1.1  christos 
   3782  1.1.1.2  christos 	  gdb_assert (tp->last_status.kind () != TARGET_WAITKIND_IGNORE);
   3783  1.1.1.2  christos 	  prepare_resume_reply (own_buf, tp->id, tp->last_status);
   3784      1.1  christos 	}
   3785      1.1  christos       else
   3786      1.1  christos 	strcpy (own_buf, "W00");
   3787      1.1  christos     }
   3788      1.1  christos }
   3789      1.1  christos 
   3790      1.1  christos static void
   3791      1.1  christos gdbserver_version (void)
   3792      1.1  christos {
   3793      1.1  christos   printf ("GNU gdbserver %s%s\n"
   3794  1.1.1.3  christos 	  "Copyright (C) 2024 Free Software Foundation, Inc.\n"
   3795      1.1  christos 	  "gdbserver is free software, covered by the "
   3796      1.1  christos 	  "GNU General Public License.\n"
   3797      1.1  christos 	  "This gdbserver was configured as \"%s\"\n",
   3798      1.1  christos 	  PKGVERSION, version, host_name);
   3799      1.1  christos }
   3800      1.1  christos 
   3801      1.1  christos static void
   3802      1.1  christos gdbserver_usage (FILE *stream)
   3803      1.1  christos {
   3804      1.1  christos   fprintf (stream, "Usage:\tgdbserver [OPTIONS] COMM PROG [ARGS ...]\n"
   3805      1.1  christos 	   "\tgdbserver [OPTIONS] --attach COMM PID\n"
   3806      1.1  christos 	   "\tgdbserver [OPTIONS] --multi COMM\n"
   3807      1.1  christos 	   "\n"
   3808      1.1  christos 	   "COMM may either be a tty device (for serial debugging),\n"
   3809      1.1  christos 	   "HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
   3810      1.1  christos 	   "stdin/stdout of gdbserver.\n"
   3811      1.1  christos 	   "PROG is the executable program.  ARGS are arguments passed to inferior.\n"
   3812      1.1  christos 	   "PID is the process ID to attach to, when --attach is specified.\n"
   3813      1.1  christos 	   "\n"
   3814      1.1  christos 	   "Operating modes:\n"
   3815      1.1  christos 	   "\n"
   3816      1.1  christos 	   "  --attach              Attach to running process PID.\n"
   3817      1.1  christos 	   "  --multi               Start server without a specific program, and\n"
   3818      1.1  christos 	   "                        only quit when explicitly commanded.\n"
   3819      1.1  christos 	   "  --once                Exit after the first connection has closed.\n"
   3820      1.1  christos 	   "  --help                Print this message and then exit.\n"
   3821      1.1  christos 	   "  --version             Display version information and exit.\n"
   3822      1.1  christos 	   "\n"
   3823      1.1  christos 	   "Other options:\n"
   3824      1.1  christos 	   "\n"
   3825      1.1  christos 	   "  --wrapper WRAPPER --  Run WRAPPER to start new programs.\n"
   3826      1.1  christos 	   "  --disable-randomization\n"
   3827      1.1  christos 	   "                        Run PROG with address space randomization disabled.\n"
   3828      1.1  christos 	   "  --no-disable-randomization\n"
   3829      1.1  christos 	   "                        Don't disable address space randomization when\n"
   3830      1.1  christos 	   "                        starting PROG.\n"
   3831      1.1  christos 	   "  --startup-with-shell\n"
   3832      1.1  christos 	   "                        Start PROG using a shell.  I.e., execs a shell that\n"
   3833      1.1  christos 	   "                        then execs PROG.  (default)\n"
   3834      1.1  christos 	   "  --no-startup-with-shell\n"
   3835      1.1  christos 	   "                        Exec PROG directly instead of using a shell.\n"
   3836      1.1  christos 	   "                        Disables argument globbing and variable substitution\n"
   3837      1.1  christos 	   "                        on UNIX-like systems.\n"
   3838      1.1  christos 	   "\n"
   3839      1.1  christos 	   "Debug options:\n"
   3840      1.1  christos 	   "\n"
   3841  1.1.1.3  christos 	   "  --debug[=OPT1,OPT2,...]\n"
   3842  1.1.1.3  christos 	   "                        Enable debugging output.\n"
   3843  1.1.1.3  christos 	   "                          Options:\n"
   3844  1.1.1.3  christos 	   "                            all, threads, event-loop, remote\n"
   3845  1.1.1.3  christos 	   "                          With no options, 'threads' is assumed.\n"
   3846  1.1.1.3  christos 	   "                          Prefix an option with '-' to disable\n"
   3847  1.1.1.3  christos 	   "                          debugging of that component.\n"
   3848      1.1  christos 	   "  --debug-format=OPT1[,OPT2,...]\n"
   3849      1.1  christos 	   "                        Specify extra content in debugging output.\n"
   3850      1.1  christos 	   "                          Options:\n"
   3851      1.1  christos 	   "                            all\n"
   3852      1.1  christos 	   "                            none\n"
   3853      1.1  christos 	   "                            timestamp\n"
   3854      1.1  christos 	   "  --disable-packet=OPT1[,OPT2,...]\n"
   3855      1.1  christos 	   "                        Disable support for RSP packets or features.\n"
   3856      1.1  christos 	   "                          Options:\n"
   3857  1.1.1.2  christos 	   "                            vCont, T, Tthread, qC, qfThreadInfo and \n"
   3858      1.1  christos 	   "                            threads (disable all threading packets).\n"
   3859      1.1  christos 	   "\n"
   3860      1.1  christos 	   "For more information, consult the GDB manual (available as on-line \n"
   3861      1.1  christos 	   "info or a printed manual).\n");
   3862      1.1  christos   if (REPORT_BUGS_TO[0] && stream == stdout)
   3863      1.1  christos     fprintf (stream, "Report bugs to \"%s\".\n", REPORT_BUGS_TO);
   3864      1.1  christos }
   3865      1.1  christos 
   3866      1.1  christos static void
   3867      1.1  christos gdbserver_show_disableable (FILE *stream)
   3868      1.1  christos {
   3869      1.1  christos   fprintf (stream, "Disableable packets:\n"
   3870      1.1  christos 	   "  vCont       \tAll vCont packets\n"
   3871      1.1  christos 	   "  qC          \tQuerying the current thread\n"
   3872      1.1  christos 	   "  qfThreadInfo\tThread listing\n"
   3873      1.1  christos 	   "  Tthread     \tPassing the thread specifier in the "
   3874      1.1  christos 	   "T stop reply packet\n"
   3875  1.1.1.2  christos 	   "  threads     \tAll of the above\n"
   3876  1.1.1.2  christos 	   "  T           \tAll 'T' packets\n");
   3877      1.1  christos }
   3878      1.1  christos 
   3879      1.1  christos /* Start up the event loop.  This is the entry point to the event
   3880      1.1  christos    loop.  */
   3881      1.1  christos 
   3882      1.1  christos static void
   3883      1.1  christos start_event_loop ()
   3884      1.1  christos {
   3885      1.1  christos   /* Loop until there is nothing to do.  This is the entry point to
   3886      1.1  christos      the event loop engine.  If nothing is ready at this time, wait
   3887      1.1  christos      for something to happen (via wait_for_event), then process it.
   3888      1.1  christos      Return when there are no longer event sources to wait for.  */
   3889      1.1  christos 
   3890      1.1  christos   keep_processing_events = true;
   3891      1.1  christos   while (keep_processing_events)
   3892      1.1  christos     {
   3893      1.1  christos       /* Any events already waiting in the queue?  */
   3894      1.1  christos       int res = gdb_do_one_event ();
   3895      1.1  christos 
   3896      1.1  christos       /* Was there an error?  */
   3897      1.1  christos       if (res == -1)
   3898      1.1  christos 	break;
   3899      1.1  christos     }
   3900      1.1  christos 
   3901      1.1  christos   /* We are done with the event loop.  There are no more event sources
   3902      1.1  christos      to listen to.  So we exit gdbserver.  */
   3903      1.1  christos }
   3904      1.1  christos 
   3905      1.1  christos static void
   3906      1.1  christos kill_inferior_callback (process_info *process)
   3907      1.1  christos {
   3908      1.1  christos   kill_inferior (process);
   3909      1.1  christos   discard_queued_stop_replies (ptid_t (process->pid));
   3910      1.1  christos }
   3911      1.1  christos 
   3912      1.1  christos /* Call this when exiting gdbserver with possible inferiors that need
   3913      1.1  christos    to be killed or detached from.  */
   3914      1.1  christos 
   3915      1.1  christos static void
   3916      1.1  christos detach_or_kill_for_exit (void)
   3917      1.1  christos {
   3918      1.1  christos   /* First print a list of the inferiors we will be killing/detaching.
   3919      1.1  christos      This is to assist the user, for example, in case the inferior unexpectedly
   3920      1.1  christos      dies after we exit: did we screw up or did the inferior exit on its own?
   3921      1.1  christos      Having this info will save some head-scratching.  */
   3922      1.1  christos 
   3923      1.1  christos   if (have_started_inferiors_p ())
   3924      1.1  christos     {
   3925      1.1  christos       fprintf (stderr, "Killing process(es):");
   3926      1.1  christos 
   3927      1.1  christos       for_each_process ([] (process_info *process) {
   3928      1.1  christos 	if (!process->attached)
   3929      1.1  christos 	  fprintf (stderr, " %d", process->pid);
   3930      1.1  christos       });
   3931      1.1  christos 
   3932      1.1  christos       fprintf (stderr, "\n");
   3933      1.1  christos     }
   3934      1.1  christos   if (have_attached_inferiors_p ())
   3935      1.1  christos     {
   3936      1.1  christos       fprintf (stderr, "Detaching process(es):");
   3937      1.1  christos 
   3938      1.1  christos       for_each_process ([] (process_info *process) {
   3939      1.1  christos 	if (process->attached)
   3940      1.1  christos 	  fprintf (stderr, " %d", process->pid);
   3941      1.1  christos       });
   3942      1.1  christos 
   3943      1.1  christos       fprintf (stderr, "\n");
   3944      1.1  christos     }
   3945      1.1  christos 
   3946      1.1  christos   /* Now we can kill or detach the inferiors.  */
   3947      1.1  christos   for_each_process ([] (process_info *process) {
   3948      1.1  christos     int pid = process->pid;
   3949      1.1  christos 
   3950      1.1  christos     if (process->attached)
   3951      1.1  christos       detach_inferior (process);
   3952      1.1  christos     else
   3953      1.1  christos       kill_inferior (process);
   3954      1.1  christos 
   3955      1.1  christos     discard_queued_stop_replies (ptid_t (pid));
   3956      1.1  christos   });
   3957      1.1  christos }
   3958      1.1  christos 
   3959      1.1  christos /* Value that will be passed to exit(3) when gdbserver exits.  */
   3960      1.1  christos static int exit_code;
   3961      1.1  christos 
   3962      1.1  christos /* Wrapper for detach_or_kill_for_exit that catches and prints
   3963      1.1  christos    errors.  */
   3964      1.1  christos 
   3965      1.1  christos static void
   3966      1.1  christos detach_or_kill_for_exit_cleanup ()
   3967      1.1  christos {
   3968      1.1  christos   try
   3969      1.1  christos     {
   3970      1.1  christos       detach_or_kill_for_exit ();
   3971      1.1  christos     }
   3972      1.1  christos   catch (const gdb_exception &exception)
   3973      1.1  christos     {
   3974      1.1  christos       fflush (stdout);
   3975      1.1  christos       fprintf (stderr, "Detach or kill failed: %s\n",
   3976      1.1  christos 	       exception.what ());
   3977      1.1  christos       exit_code = 1;
   3978      1.1  christos     }
   3979      1.1  christos }
   3980      1.1  christos 
   3981  1.1.1.2  christos #if GDB_SELF_TEST
   3982  1.1.1.2  christos 
   3983  1.1.1.2  christos namespace selftests {
   3984  1.1.1.2  christos 
   3985  1.1.1.2  christos static void
   3986  1.1.1.2  christos test_memory_tagging_functions (void)
   3987  1.1.1.2  christos {
   3988  1.1.1.2  christos   /* Setup testing.  */
   3989  1.1.1.2  christos   gdb::char_vector packet;
   3990  1.1.1.2  christos   gdb::byte_vector tags, bv;
   3991  1.1.1.2  christos   std::string expected;
   3992  1.1.1.2  christos   packet.resize (32000);
   3993  1.1.1.2  christos   CORE_ADDR addr;
   3994  1.1.1.2  christos   size_t len;
   3995  1.1.1.2  christos   int type;
   3996  1.1.1.2  christos 
   3997  1.1.1.2  christos   /* Test parsing a qMemTags request.  */
   3998  1.1.1.2  christos 
   3999  1.1.1.2  christos   /* Valid request, addr, len and type updated.  */
   4000  1.1.1.2  christos   addr = 0xff;
   4001  1.1.1.2  christos   len = 255;
   4002  1.1.1.2  christos   type = 255;
   4003  1.1.1.2  christos   strcpy (packet.data (), "qMemTags:0,0:0");
   4004  1.1.1.2  christos   parse_fetch_memtags_request (packet.data (), &addr, &len, &type);
   4005  1.1.1.2  christos   SELF_CHECK (addr == 0 && len == 0 && type == 0);
   4006  1.1.1.2  christos 
   4007  1.1.1.2  christos   /* Valid request, addr, len and type updated.  */
   4008  1.1.1.2  christos   addr = 0;
   4009  1.1.1.2  christos   len = 0;
   4010  1.1.1.2  christos   type = 0;
   4011  1.1.1.2  christos   strcpy (packet.data (), "qMemTags:deadbeef,ff:5");
   4012  1.1.1.2  christos   parse_fetch_memtags_request (packet.data (), &addr, &len, &type);
   4013  1.1.1.2  christos   SELF_CHECK (addr == 0xdeadbeef && len == 255 && type == 5);
   4014  1.1.1.2  christos 
   4015  1.1.1.2  christos   /* Test creating a qMemTags reply.  */
   4016  1.1.1.2  christos 
   4017  1.1.1.2  christos   /* Non-empty tag data.  */
   4018  1.1.1.2  christos   bv.resize (0);
   4019  1.1.1.2  christos 
   4020  1.1.1.2  christos   for (int i = 0; i < 5; i++)
   4021  1.1.1.2  christos     bv.push_back (i);
   4022  1.1.1.2  christos 
   4023  1.1.1.2  christos   expected = "m0001020304";
   4024  1.1.1.2  christos   SELF_CHECK (create_fetch_memtags_reply (packet.data (), bv) == true);
   4025  1.1.1.2  christos   SELF_CHECK (strcmp (packet.data (), expected.c_str ()) == 0);
   4026  1.1.1.2  christos 
   4027  1.1.1.2  christos   /* Test parsing a QMemTags request.  */
   4028  1.1.1.2  christos 
   4029  1.1.1.2  christos   /* Valid request and empty tag data: addr, len, type and tags updated.  */
   4030  1.1.1.2  christos   addr = 0xff;
   4031  1.1.1.2  christos   len = 255;
   4032  1.1.1.2  christos   type = 255;
   4033  1.1.1.2  christos   tags.resize (5);
   4034  1.1.1.2  christos   strcpy (packet.data (), "QMemTags:0,0:0:");
   4035  1.1.1.2  christos   SELF_CHECK (parse_store_memtags_request (packet.data (),
   4036  1.1.1.2  christos 					   &addr, &len, tags, &type) == true);
   4037  1.1.1.2  christos   SELF_CHECK (addr == 0 && len == 0 && type == 0 && tags.size () == 0);
   4038  1.1.1.2  christos 
   4039  1.1.1.2  christos   /* Valid request and non-empty tag data: addr, len, type
   4040  1.1.1.2  christos      and tags updated.  */
   4041  1.1.1.2  christos   addr = 0;
   4042  1.1.1.2  christos   len = 0;
   4043  1.1.1.2  christos   type = 0;
   4044  1.1.1.2  christos   tags.resize (0);
   4045  1.1.1.2  christos   strcpy (packet.data (),
   4046  1.1.1.2  christos 	  "QMemTags:deadbeef,ff:5:0001020304");
   4047  1.1.1.2  christos   SELF_CHECK (parse_store_memtags_request (packet.data (), &addr, &len, tags,
   4048  1.1.1.2  christos 					   &type) == true);
   4049  1.1.1.2  christos   SELF_CHECK (addr == 0xdeadbeef && len == 255 && type == 5
   4050  1.1.1.2  christos 	      && tags.size () == 5);
   4051  1.1.1.2  christos }
   4052  1.1.1.2  christos 
   4053  1.1.1.2  christos } // namespace selftests
   4054  1.1.1.2  christos #endif /* GDB_SELF_TEST */
   4055  1.1.1.2  christos 
   4056      1.1  christos /* Main function.  This is called by the real "main" function,
   4057      1.1  christos    wrapped in a TRY_CATCH that handles any uncaught exceptions.  */
   4058      1.1  christos 
   4059      1.1  christos static void ATTRIBUTE_NORETURN
   4060      1.1  christos captured_main (int argc, char *argv[])
   4061      1.1  christos {
   4062      1.1  christos   int bad_attach;
   4063      1.1  christos   int pid;
   4064      1.1  christos   char *arg_end;
   4065      1.1  christos   const char *port = NULL;
   4066      1.1  christos   char **next_arg = &argv[1];
   4067      1.1  christos   volatile int multi_mode = 0;
   4068      1.1  christos   volatile int attach = 0;
   4069      1.1  christos   int was_running;
   4070      1.1  christos   bool selftest = false;
   4071      1.1  christos #if GDB_SELF_TEST
   4072      1.1  christos   std::vector<const char *> selftest_filters;
   4073  1.1.1.2  christos 
   4074  1.1.1.2  christos   selftests::register_test ("remote_memory_tagging",
   4075  1.1.1.2  christos 			    selftests::test_memory_tagging_functions);
   4076      1.1  christos #endif
   4077      1.1  christos 
   4078      1.1  christos   current_directory = getcwd (NULL, 0);
   4079      1.1  christos   client_state &cs = get_client_state ();
   4080      1.1  christos 
   4081      1.1  christos   if (current_directory == NULL)
   4082      1.1  christos     {
   4083      1.1  christos       error (_("Could not find current working directory: %s"),
   4084      1.1  christos 	     safe_strerror (errno));
   4085      1.1  christos     }
   4086      1.1  christos 
   4087      1.1  christos   while (*next_arg != NULL && **next_arg == '-')
   4088      1.1  christos     {
   4089      1.1  christos       if (strcmp (*next_arg, "--version") == 0)
   4090      1.1  christos 	{
   4091      1.1  christos 	  gdbserver_version ();
   4092      1.1  christos 	  exit (0);
   4093      1.1  christos 	}
   4094      1.1  christos       else if (strcmp (*next_arg, "--help") == 0)
   4095      1.1  christos 	{
   4096      1.1  christos 	  gdbserver_usage (stdout);
   4097      1.1  christos 	  exit (0);
   4098      1.1  christos 	}
   4099      1.1  christos       else if (strcmp (*next_arg, "--attach") == 0)
   4100      1.1  christos 	attach = 1;
   4101      1.1  christos       else if (strcmp (*next_arg, "--multi") == 0)
   4102      1.1  christos 	multi_mode = 1;
   4103      1.1  christos       else if (strcmp (*next_arg, "--wrapper") == 0)
   4104      1.1  christos 	{
   4105      1.1  christos 	  char **tmp;
   4106      1.1  christos 
   4107      1.1  christos 	  next_arg++;
   4108      1.1  christos 
   4109      1.1  christos 	  tmp = next_arg;
   4110      1.1  christos 	  while (*next_arg != NULL && strcmp (*next_arg, "--") != 0)
   4111      1.1  christos 	    {
   4112      1.1  christos 	      wrapper_argv += *next_arg;
   4113      1.1  christos 	      wrapper_argv += ' ';
   4114      1.1  christos 	      next_arg++;
   4115      1.1  christos 	    }
   4116      1.1  christos 
   4117      1.1  christos 	  if (!wrapper_argv.empty ())
   4118      1.1  christos 	    {
   4119      1.1  christos 	      /* Erase the last whitespace.  */
   4120      1.1  christos 	      wrapper_argv.erase (wrapper_argv.end () - 1);
   4121      1.1  christos 	    }
   4122      1.1  christos 
   4123      1.1  christos 	  if (next_arg == tmp || *next_arg == NULL)
   4124      1.1  christos 	    {
   4125      1.1  christos 	      gdbserver_usage (stderr);
   4126      1.1  christos 	      exit (1);
   4127      1.1  christos 	    }
   4128      1.1  christos 
   4129      1.1  christos 	  /* Consume the "--".  */
   4130      1.1  christos 	  *next_arg = NULL;
   4131      1.1  christos 	}
   4132  1.1.1.3  christos       else if (startswith (*next_arg, "--debug="))
   4133  1.1.1.3  christos 	{
   4134  1.1.1.3  christos 	  try
   4135  1.1.1.3  christos 	    {
   4136  1.1.1.3  christos 	      parse_debug_options ((*next_arg) + sizeof ("--debug=") - 1);
   4137  1.1.1.3  christos 	    }
   4138  1.1.1.3  christos 	  catch (const gdb_exception_error &exception)
   4139  1.1.1.3  christos 	    {
   4140  1.1.1.3  christos 	      fflush (stdout);
   4141  1.1.1.3  christos 	      fprintf (stderr, "gdbserver: %s\n", exception.what ());
   4142  1.1.1.3  christos 	      exit (1);
   4143  1.1.1.3  christos 	    }
   4144  1.1.1.3  christos 	}
   4145      1.1  christos       else if (strcmp (*next_arg, "--debug") == 0)
   4146  1.1.1.3  christos 	{
   4147  1.1.1.3  christos 	  try
   4148  1.1.1.3  christos 	    {
   4149  1.1.1.3  christos 	      parse_debug_options ("");
   4150  1.1.1.3  christos 	    }
   4151  1.1.1.3  christos 	  catch (const gdb_exception_error &exception)
   4152  1.1.1.3  christos 	    {
   4153  1.1.1.3  christos 	      fflush (stdout);
   4154  1.1.1.3  christos 	      fprintf (stderr, "gdbserver: %s\n", exception.what ());
   4155  1.1.1.3  christos 	      exit (1);
   4156  1.1.1.3  christos 	    }
   4157  1.1.1.3  christos 	}
   4158      1.1  christos       else if (startswith (*next_arg, "--debug-format="))
   4159      1.1  christos 	{
   4160      1.1  christos 	  std::string error_msg
   4161      1.1  christos 	    = parse_debug_format_options ((*next_arg)
   4162      1.1  christos 					  + sizeof ("--debug-format=") - 1, 0);
   4163      1.1  christos 
   4164      1.1  christos 	  if (!error_msg.empty ())
   4165      1.1  christos 	    {
   4166      1.1  christos 	      fprintf (stderr, "%s", error_msg.c_str ());
   4167      1.1  christos 	      exit (1);
   4168      1.1  christos 	    }
   4169      1.1  christos 	}
   4170      1.1  christos       else if (startswith (*next_arg, "--debug-file="))
   4171      1.1  christos 	debug_set_output ((*next_arg) + sizeof ("--debug-file=") -1);
   4172      1.1  christos       else if (strcmp (*next_arg, "--disable-packet") == 0)
   4173      1.1  christos 	{
   4174      1.1  christos 	  gdbserver_show_disableable (stdout);
   4175      1.1  christos 	  exit (0);
   4176      1.1  christos 	}
   4177      1.1  christos       else if (startswith (*next_arg, "--disable-packet="))
   4178      1.1  christos 	{
   4179      1.1  christos 	  char *packets = *next_arg += sizeof ("--disable-packet=") - 1;
   4180      1.1  christos 	  char *saveptr;
   4181      1.1  christos 	  for (char *tok = strtok_r (packets, ",", &saveptr);
   4182      1.1  christos 	       tok != NULL;
   4183      1.1  christos 	       tok = strtok_r (NULL, ",", &saveptr))
   4184      1.1  christos 	    {
   4185      1.1  christos 	      if (strcmp ("vCont", tok) == 0)
   4186      1.1  christos 		disable_packet_vCont = true;
   4187      1.1  christos 	      else if (strcmp ("Tthread", tok) == 0)
   4188      1.1  christos 		disable_packet_Tthread = true;
   4189      1.1  christos 	      else if (strcmp ("qC", tok) == 0)
   4190      1.1  christos 		disable_packet_qC = true;
   4191      1.1  christos 	      else if (strcmp ("qfThreadInfo", tok) == 0)
   4192      1.1  christos 		disable_packet_qfThreadInfo = true;
   4193      1.1  christos 	      else if (strcmp ("T", tok) == 0)
   4194      1.1  christos 		disable_packet_T = true;
   4195      1.1  christos 	      else if (strcmp ("threads", tok) == 0)
   4196      1.1  christos 		{
   4197      1.1  christos 		  disable_packet_vCont = true;
   4198      1.1  christos 		  disable_packet_Tthread = true;
   4199      1.1  christos 		  disable_packet_qC = true;
   4200      1.1  christos 		  disable_packet_qfThreadInfo = true;
   4201      1.1  christos 		}
   4202      1.1  christos 	      else
   4203      1.1  christos 		{
   4204      1.1  christos 		  fprintf (stderr, "Don't know how to disable \"%s\".\n\n",
   4205      1.1  christos 			   tok);
   4206      1.1  christos 		  gdbserver_show_disableable (stderr);
   4207      1.1  christos 		  exit (1);
   4208      1.1  christos 		}
   4209      1.1  christos 	    }
   4210      1.1  christos 	}
   4211      1.1  christos       else if (strcmp (*next_arg, "-") == 0)
   4212      1.1  christos 	{
   4213      1.1  christos 	  /* "-" specifies a stdio connection and is a form of port
   4214      1.1  christos 	     specification.  */
   4215      1.1  christos 	  port = STDIO_CONNECTION_NAME;
   4216      1.1  christos 	  next_arg++;
   4217      1.1  christos 	  break;
   4218      1.1  christos 	}
   4219      1.1  christos       else if (strcmp (*next_arg, "--disable-randomization") == 0)
   4220      1.1  christos 	cs.disable_randomization = 1;
   4221      1.1  christos       else if (strcmp (*next_arg, "--no-disable-randomization") == 0)
   4222      1.1  christos 	cs.disable_randomization = 0;
   4223      1.1  christos       else if (strcmp (*next_arg, "--startup-with-shell") == 0)
   4224      1.1  christos 	startup_with_shell = true;
   4225      1.1  christos       else if (strcmp (*next_arg, "--no-startup-with-shell") == 0)
   4226      1.1  christos 	startup_with_shell = false;
   4227      1.1  christos       else if (strcmp (*next_arg, "--once") == 0)
   4228      1.1  christos 	run_once = true;
   4229      1.1  christos       else if (strcmp (*next_arg, "--selftest") == 0)
   4230      1.1  christos 	selftest = true;
   4231      1.1  christos       else if (startswith (*next_arg, "--selftest="))
   4232      1.1  christos 	{
   4233      1.1  christos 	  selftest = true;
   4234      1.1  christos 
   4235      1.1  christos #if GDB_SELF_TEST
   4236      1.1  christos 	  const char *filter = *next_arg + strlen ("--selftest=");
   4237      1.1  christos 	  if (*filter == '\0')
   4238      1.1  christos 	    {
   4239      1.1  christos 	      fprintf (stderr, _("Error: selftest filter is empty.\n"));
   4240      1.1  christos 	      exit (1);
   4241      1.1  christos 	    }
   4242      1.1  christos 
   4243      1.1  christos 	  selftest_filters.push_back (filter);
   4244      1.1  christos #endif
   4245      1.1  christos 	}
   4246      1.1  christos       else
   4247      1.1  christos 	{
   4248      1.1  christos 	  fprintf (stderr, "Unknown argument: %s\n", *next_arg);
   4249      1.1  christos 	  exit (1);
   4250      1.1  christos 	}
   4251      1.1  christos 
   4252      1.1  christos       next_arg++;
   4253      1.1  christos       continue;
   4254      1.1  christos     }
   4255      1.1  christos 
   4256      1.1  christos   if (port == NULL)
   4257      1.1  christos     {
   4258      1.1  christos       port = *next_arg;
   4259      1.1  christos       next_arg++;
   4260      1.1  christos     }
   4261      1.1  christos   if ((port == NULL || (!attach && !multi_mode && *next_arg == NULL))
   4262      1.1  christos        && !selftest)
   4263      1.1  christos     {
   4264      1.1  christos       gdbserver_usage (stderr);
   4265      1.1  christos       exit (1);
   4266      1.1  christos     }
   4267      1.1  christos 
   4268      1.1  christos   /* Remember stdio descriptors.  LISTEN_DESC must not be listed, it will be
   4269      1.1  christos      opened by remote_prepare.  */
   4270      1.1  christos   notice_open_fds ();
   4271      1.1  christos 
   4272      1.1  christos   save_original_signals_state (false);
   4273      1.1  christos 
   4274      1.1  christos   /* We need to know whether the remote connection is stdio before
   4275      1.1  christos      starting the inferior.  Inferiors created in this scenario have
   4276      1.1  christos      stdin,stdout redirected.  So do this here before we call
   4277      1.1  christos      start_inferior.  */
   4278      1.1  christos   if (port != NULL)
   4279      1.1  christos     remote_prepare (port);
   4280      1.1  christos 
   4281      1.1  christos   bad_attach = 0;
   4282      1.1  christos   pid = 0;
   4283      1.1  christos 
   4284      1.1  christos   /* --attach used to come after PORT, so allow it there for
   4285      1.1  christos        compatibility.  */
   4286      1.1  christos   if (*next_arg != NULL && strcmp (*next_arg, "--attach") == 0)
   4287      1.1  christos     {
   4288      1.1  christos       attach = 1;
   4289      1.1  christos       next_arg++;
   4290      1.1  christos     }
   4291      1.1  christos 
   4292      1.1  christos   if (attach
   4293      1.1  christos       && (*next_arg == NULL
   4294      1.1  christos 	  || (*next_arg)[0] == '\0'
   4295      1.1  christos 	  || (pid = strtoul (*next_arg, &arg_end, 0)) == 0
   4296      1.1  christos 	  || *arg_end != '\0'
   4297      1.1  christos 	  || next_arg[1] != NULL))
   4298      1.1  christos     bad_attach = 1;
   4299      1.1  christos 
   4300      1.1  christos   if (bad_attach)
   4301      1.1  christos     {
   4302      1.1  christos       gdbserver_usage (stderr);
   4303      1.1  christos       exit (1);
   4304      1.1  christos     }
   4305      1.1  christos 
   4306      1.1  christos   /* Gather information about the environment.  */
   4307      1.1  christos   our_environ = gdb_environ::from_host_environ ();
   4308      1.1  christos 
   4309      1.1  christos   initialize_async_io ();
   4310      1.1  christos   initialize_low ();
   4311      1.1  christos   have_job_control ();
   4312      1.1  christos   if (target_supports_tracepoints ())
   4313      1.1  christos     initialize_tracepoint ();
   4314      1.1  christos 
   4315      1.1  christos   mem_buf = (unsigned char *) xmalloc (PBUFSIZ);
   4316      1.1  christos 
   4317      1.1  christos   if (selftest)
   4318      1.1  christos     {
   4319      1.1  christos #if GDB_SELF_TEST
   4320      1.1  christos       selftests::run_tests (selftest_filters);
   4321      1.1  christos #else
   4322      1.1  christos       printf (_("Selftests have been disabled for this build.\n"));
   4323      1.1  christos #endif
   4324      1.1  christos       throw_quit ("Quit");
   4325      1.1  christos     }
   4326      1.1  christos 
   4327      1.1  christos   if (pid == 0 && *next_arg != NULL)
   4328      1.1  christos     {
   4329      1.1  christos       int i, n;
   4330      1.1  christos 
   4331      1.1  christos       n = argc - (next_arg - argv);
   4332  1.1.1.2  christos       program_path.set (next_arg[0]);
   4333      1.1  christos       for (i = 1; i < n; i++)
   4334      1.1  christos 	program_args.push_back (xstrdup (next_arg[i]));
   4335      1.1  christos 
   4336      1.1  christos       /* Wait till we are at first instruction in program.  */
   4337      1.1  christos       target_create_inferior (program_path.get (), program_args);
   4338      1.1  christos 
   4339      1.1  christos       /* We are now (hopefully) stopped at the first instruction of
   4340      1.1  christos 	 the target process.  This assumes that the target process was
   4341      1.1  christos 	 successfully created.  */
   4342      1.1  christos     }
   4343      1.1  christos   else if (pid != 0)
   4344      1.1  christos     {
   4345      1.1  christos       if (attach_inferior (pid) == -1)
   4346      1.1  christos 	error ("Attaching not supported on this target");
   4347      1.1  christos 
   4348      1.1  christos       /* Otherwise succeeded.  */
   4349      1.1  christos     }
   4350      1.1  christos   else
   4351      1.1  christos     {
   4352  1.1.1.2  christos       cs.last_status.set_exited (0);
   4353      1.1  christos       cs.last_ptid = minus_one_ptid;
   4354      1.1  christos     }
   4355      1.1  christos 
   4356      1.1  christos   SCOPE_EXIT { detach_or_kill_for_exit_cleanup (); };
   4357      1.1  christos 
   4358      1.1  christos   /* Don't report shared library events on the initial connection,
   4359      1.1  christos      even if some libraries are preloaded.  Avoids the "stopped by
   4360      1.1  christos      shared library event" notice on gdb side.  */
   4361  1.1.1.2  christos   if (current_thread != nullptr)
   4362  1.1.1.2  christos     current_process ()->dlls_changed = false;
   4363      1.1  christos 
   4364  1.1.1.2  christos   if (cs.last_status.kind () == TARGET_WAITKIND_EXITED
   4365  1.1.1.2  christos       || cs.last_status.kind () == TARGET_WAITKIND_SIGNALLED)
   4366      1.1  christos     was_running = 0;
   4367      1.1  christos   else
   4368      1.1  christos     was_running = 1;
   4369      1.1  christos 
   4370      1.1  christos   if (!was_running && !multi_mode)
   4371      1.1  christos     error ("No program to debug");
   4372      1.1  christos 
   4373      1.1  christos   while (1)
   4374      1.1  christos     {
   4375      1.1  christos       cs.noack_mode = 0;
   4376      1.1  christos       cs.multi_process = 0;
   4377      1.1  christos       cs.report_fork_events = 0;
   4378      1.1  christos       cs.report_vfork_events = 0;
   4379      1.1  christos       cs.report_exec_events = 0;
   4380      1.1  christos       /* Be sure we're out of tfind mode.  */
   4381      1.1  christos       cs.current_traceframe = -1;
   4382      1.1  christos       cs.cont_thread = null_ptid;
   4383      1.1  christos       cs.swbreak_feature = 0;
   4384      1.1  christos       cs.hwbreak_feature = 0;
   4385      1.1  christos       cs.vCont_supported = 0;
   4386  1.1.1.2  christos       cs.memory_tagging_feature = false;
   4387      1.1  christos 
   4388      1.1  christos       remote_open (port);
   4389      1.1  christos 
   4390      1.1  christos       try
   4391      1.1  christos 	{
   4392      1.1  christos 	  /* Wait for events.  This will return when all event sources
   4393      1.1  christos 	     are removed from the event loop.  */
   4394      1.1  christos 	  start_event_loop ();
   4395      1.1  christos 
   4396      1.1  christos 	  /* If an exit was requested (using the "monitor exit"
   4397      1.1  christos 	     command), terminate now.  */
   4398      1.1  christos 	  if (exit_requested)
   4399      1.1  christos 	    throw_quit ("Quit");
   4400      1.1  christos 
   4401      1.1  christos 	  /* The only other way to get here is for getpkt to fail:
   4402      1.1  christos 
   4403      1.1  christos 	      - If --once was specified, we're done.
   4404      1.1  christos 
   4405      1.1  christos 	      - If not in extended-remote mode, and we're no longer
   4406  1.1.1.2  christos 		debugging anything, simply exit: GDB has disconnected
   4407  1.1.1.2  christos 		after processing the last process exit.
   4408      1.1  christos 
   4409      1.1  christos 	      - Otherwise, close the connection and reopen it at the
   4410  1.1.1.2  christos 		top of the loop.  */
   4411      1.1  christos 	  if (run_once || (!extended_protocol && !target_running ()))
   4412      1.1  christos 	    throw_quit ("Quit");
   4413      1.1  christos 
   4414      1.1  christos 	  fprintf (stderr,
   4415      1.1  christos 		   "Remote side has terminated connection.  "
   4416      1.1  christos 		   "GDBserver will reopen the connection.\n");
   4417      1.1  christos 
   4418      1.1  christos 	  /* Get rid of any pending statuses.  An eventual reconnection
   4419      1.1  christos 	     (by the same GDB instance or another) will refresh all its
   4420      1.1  christos 	     state from scratch.  */
   4421      1.1  christos 	  discard_queued_stop_replies (minus_one_ptid);
   4422      1.1  christos 	  for_each_thread ([] (thread_info *thread)
   4423      1.1  christos 	    {
   4424      1.1  christos 	      thread->status_pending_p = 0;
   4425      1.1  christos 	    });
   4426      1.1  christos 
   4427      1.1  christos 	  if (tracing)
   4428      1.1  christos 	    {
   4429      1.1  christos 	      if (disconnected_tracing)
   4430      1.1  christos 		{
   4431      1.1  christos 		  /* Try to enable non-stop/async mode, so we we can
   4432      1.1  christos 		     both wait for an async socket accept, and handle
   4433      1.1  christos 		     async target events simultaneously.  There's also
   4434      1.1  christos 		     no point either in having the target always stop
   4435      1.1  christos 		     all threads, when we're going to pass signals
   4436      1.1  christos 		     down without informing GDB.  */
   4437      1.1  christos 		  if (!non_stop)
   4438      1.1  christos 		    {
   4439      1.1  christos 		      if (the_target->start_non_stop (true))
   4440      1.1  christos 			non_stop = 1;
   4441      1.1  christos 
   4442      1.1  christos 		      /* Detaching implicitly resumes all threads;
   4443      1.1  christos 			 simply disconnecting does not.  */
   4444      1.1  christos 		    }
   4445      1.1  christos 		}
   4446      1.1  christos 	      else
   4447      1.1  christos 		{
   4448      1.1  christos 		  fprintf (stderr,
   4449      1.1  christos 			   "Disconnected tracing disabled; "
   4450      1.1  christos 			   "stopping trace run.\n");
   4451      1.1  christos 		  stop_tracing ();
   4452      1.1  christos 		}
   4453      1.1  christos 	    }
   4454      1.1  christos 	}
   4455      1.1  christos       catch (const gdb_exception_error &exception)
   4456      1.1  christos 	{
   4457      1.1  christos 	  fflush (stdout);
   4458      1.1  christos 	  fprintf (stderr, "gdbserver: %s\n", exception.what ());
   4459      1.1  christos 
   4460      1.1  christos 	  if (response_needed)
   4461      1.1  christos 	    {
   4462      1.1  christos 	      write_enn (cs.own_buf);
   4463      1.1  christos 	      putpkt (cs.own_buf);
   4464      1.1  christos 	    }
   4465      1.1  christos 
   4466      1.1  christos 	  if (run_once)
   4467      1.1  christos 	    throw_quit ("Quit");
   4468      1.1  christos 	}
   4469      1.1  christos     }
   4470      1.1  christos }
   4471      1.1  christos 
   4472      1.1  christos /* Main function.  */
   4473      1.1  christos 
   4474      1.1  christos int
   4475      1.1  christos main (int argc, char *argv[])
   4476      1.1  christos {
   4477  1.1.1.3  christos   setlocale (LC_CTYPE, "");
   4478      1.1  christos 
   4479      1.1  christos   try
   4480      1.1  christos     {
   4481      1.1  christos       captured_main (argc, argv);
   4482      1.1  christos     }
   4483      1.1  christos   catch (const gdb_exception &exception)
   4484      1.1  christos     {
   4485      1.1  christos       if (exception.reason == RETURN_ERROR)
   4486      1.1  christos 	{
   4487      1.1  christos 	  fflush (stdout);
   4488      1.1  christos 	  fprintf (stderr, "%s\n", exception.what ());
   4489      1.1  christos 	  fprintf (stderr, "Exiting\n");
   4490      1.1  christos 	  exit_code = 1;
   4491      1.1  christos 	}
   4492      1.1  christos 
   4493      1.1  christos       exit (exit_code);
   4494      1.1  christos     }
   4495      1.1  christos 
   4496      1.1  christos   gdb_assert_not_reached ("captured_main should never return");
   4497      1.1  christos }
   4498      1.1  christos 
   4499      1.1  christos /* Process options coming from Z packets for a breakpoint.  PACKET is
   4500      1.1  christos    the packet buffer.  *PACKET is updated to point to the first char
   4501      1.1  christos    after the last processed option.  */
   4502      1.1  christos 
   4503      1.1  christos static void
   4504      1.1  christos process_point_options (struct gdb_breakpoint *bp, const char **packet)
   4505      1.1  christos {
   4506      1.1  christos   const char *dataptr = *packet;
   4507      1.1  christos   int persist;
   4508      1.1  christos 
   4509      1.1  christos   /* Check if data has the correct format.  */
   4510      1.1  christos   if (*dataptr != ';')
   4511      1.1  christos     return;
   4512      1.1  christos 
   4513      1.1  christos   dataptr++;
   4514      1.1  christos 
   4515      1.1  christos   while (*dataptr)
   4516      1.1  christos     {
   4517      1.1  christos       if (*dataptr == ';')
   4518      1.1  christos 	++dataptr;
   4519      1.1  christos 
   4520      1.1  christos       if (*dataptr == 'X')
   4521      1.1  christos 	{
   4522      1.1  christos 	  /* Conditional expression.  */
   4523  1.1.1.2  christos 	  threads_debug_printf ("Found breakpoint condition.");
   4524      1.1  christos 	  if (!add_breakpoint_condition (bp, &dataptr))
   4525      1.1  christos 	    dataptr = strchrnul (dataptr, ';');
   4526      1.1  christos 	}
   4527      1.1  christos       else if (startswith (dataptr, "cmds:"))
   4528      1.1  christos 	{
   4529      1.1  christos 	  dataptr += strlen ("cmds:");
   4530  1.1.1.2  christos 	  threads_debug_printf ("Found breakpoint commands %s.", dataptr);
   4531      1.1  christos 	  persist = (*dataptr == '1');
   4532      1.1  christos 	  dataptr += 2;
   4533      1.1  christos 	  if (add_breakpoint_commands (bp, &dataptr, persist))
   4534      1.1  christos 	    dataptr = strchrnul (dataptr, ';');
   4535      1.1  christos 	}
   4536      1.1  christos       else
   4537      1.1  christos 	{
   4538      1.1  christos 	  fprintf (stderr, "Unknown token %c, ignoring.\n",
   4539      1.1  christos 		   *dataptr);
   4540      1.1  christos 	  /* Skip tokens until we find one that we recognize.  */
   4541      1.1  christos 	  dataptr = strchrnul (dataptr, ';');
   4542      1.1  christos 	}
   4543      1.1  christos     }
   4544      1.1  christos   *packet = dataptr;
   4545      1.1  christos }
   4546      1.1  christos 
   4547      1.1  christos /* Event loop callback that handles a serial event.  The first byte in
   4548      1.1  christos    the serial buffer gets us here.  We expect characters to arrive at
   4549      1.1  christos    a brisk pace, so we read the rest of the packet with a blocking
   4550      1.1  christos    getpkt call.  */
   4551      1.1  christos 
   4552      1.1  christos static int
   4553      1.1  christos process_serial_event (void)
   4554      1.1  christos {
   4555      1.1  christos   client_state &cs = get_client_state ();
   4556      1.1  christos   int signal;
   4557      1.1  christos   unsigned int len;
   4558      1.1  christos   CORE_ADDR mem_addr;
   4559      1.1  christos   unsigned char sig;
   4560      1.1  christos   int packet_len;
   4561      1.1  christos   int new_packet_len = -1;
   4562      1.1  christos 
   4563      1.1  christos   disable_async_io ();
   4564      1.1  christos 
   4565      1.1  christos   response_needed = false;
   4566      1.1  christos   packet_len = getpkt (cs.own_buf);
   4567      1.1  christos   if (packet_len <= 0)
   4568      1.1  christos     {
   4569      1.1  christos       remote_close ();
   4570      1.1  christos       /* Force an event loop break.  */
   4571      1.1  christos       return -1;
   4572      1.1  christos     }
   4573      1.1  christos   response_needed = true;
   4574      1.1  christos 
   4575      1.1  christos   char ch = cs.own_buf[0];
   4576      1.1  christos   switch (ch)
   4577      1.1  christos     {
   4578      1.1  christos     case 'q':
   4579      1.1  christos       handle_query (cs.own_buf, packet_len, &new_packet_len);
   4580      1.1  christos       break;
   4581      1.1  christos     case 'Q':
   4582      1.1  christos       handle_general_set (cs.own_buf);
   4583      1.1  christos       break;
   4584      1.1  christos     case 'D':
   4585      1.1  christos       handle_detach (cs.own_buf);
   4586      1.1  christos       break;
   4587      1.1  christos     case '!':
   4588      1.1  christos       extended_protocol = true;
   4589      1.1  christos       write_ok (cs.own_buf);
   4590      1.1  christos       break;
   4591      1.1  christos     case '?':
   4592      1.1  christos       handle_status (cs.own_buf);
   4593      1.1  christos       break;
   4594      1.1  christos     case 'H':
   4595      1.1  christos       if (cs.own_buf[1] == 'c' || cs.own_buf[1] == 'g' || cs.own_buf[1] == 's')
   4596      1.1  christos 	{
   4597      1.1  christos 	  require_running_or_break (cs.own_buf);
   4598      1.1  christos 
   4599      1.1  christos 	  ptid_t thread_id = read_ptid (&cs.own_buf[2], NULL);
   4600      1.1  christos 
   4601      1.1  christos 	  if (thread_id == null_ptid || thread_id == minus_one_ptid)
   4602      1.1  christos 	    thread_id = null_ptid;
   4603      1.1  christos 	  else if (thread_id.is_pid ())
   4604      1.1  christos 	    {
   4605      1.1  christos 	      /* The ptid represents a pid.  */
   4606      1.1  christos 	      thread_info *thread = find_any_thread_of_pid (thread_id.pid ());
   4607      1.1  christos 
   4608      1.1  christos 	      if (thread == NULL)
   4609      1.1  christos 		{
   4610      1.1  christos 		  write_enn (cs.own_buf);
   4611      1.1  christos 		  break;
   4612      1.1  christos 		}
   4613      1.1  christos 
   4614      1.1  christos 	      thread_id = thread->id;
   4615      1.1  christos 	    }
   4616      1.1  christos 	  else
   4617      1.1  christos 	    {
   4618      1.1  christos 	      /* The ptid represents a lwp/tid.  */
   4619      1.1  christos 	      if (find_thread_ptid (thread_id) == NULL)
   4620      1.1  christos 		{
   4621      1.1  christos 		  write_enn (cs.own_buf);
   4622      1.1  christos 		  break;
   4623      1.1  christos 		}
   4624      1.1  christos 	    }
   4625      1.1  christos 
   4626      1.1  christos 	  if (cs.own_buf[1] == 'g')
   4627      1.1  christos 	    {
   4628      1.1  christos 	      if (thread_id == null_ptid)
   4629      1.1  christos 		{
   4630      1.1  christos 		  /* GDB is telling us to choose any thread.  Check if
   4631      1.1  christos 		     the currently selected thread is still valid. If
   4632      1.1  christos 		     it is not, select the first available.  */
   4633      1.1  christos 		  thread_info *thread = find_thread_ptid (cs.general_thread);
   4634      1.1  christos 		  if (thread == NULL)
   4635      1.1  christos 		    thread = get_first_thread ();
   4636      1.1  christos 		  thread_id = thread->id;
   4637      1.1  christos 		}
   4638      1.1  christos 
   4639      1.1  christos 	      cs.general_thread = thread_id;
   4640      1.1  christos 	      set_desired_thread ();
   4641      1.1  christos 	      gdb_assert (current_thread != NULL);
   4642      1.1  christos 	    }
   4643      1.1  christos 	  else if (cs.own_buf[1] == 'c')
   4644      1.1  christos 	    cs.cont_thread = thread_id;
   4645      1.1  christos 
   4646      1.1  christos 	  write_ok (cs.own_buf);
   4647      1.1  christos 	}
   4648      1.1  christos       else
   4649      1.1  christos 	{
   4650      1.1  christos 	  /* Silently ignore it so that gdb can extend the protocol
   4651      1.1  christos 	     without compatibility headaches.  */
   4652      1.1  christos 	  cs.own_buf[0] = '\0';
   4653      1.1  christos 	}
   4654      1.1  christos       break;
   4655      1.1  christos     case 'g':
   4656      1.1  christos       require_running_or_break (cs.own_buf);
   4657      1.1  christos       if (cs.current_traceframe >= 0)
   4658      1.1  christos 	{
   4659      1.1  christos 	  struct regcache *regcache
   4660      1.1  christos 	    = new_register_cache (current_target_desc ());
   4661      1.1  christos 
   4662      1.1  christos 	  if (fetch_traceframe_registers (cs.current_traceframe,
   4663      1.1  christos 					  regcache, -1) == 0)
   4664      1.1  christos 	    registers_to_string (regcache, cs.own_buf);
   4665      1.1  christos 	  else
   4666      1.1  christos 	    write_enn (cs.own_buf);
   4667      1.1  christos 	  free_register_cache (regcache);
   4668      1.1  christos 	}
   4669      1.1  christos       else
   4670      1.1  christos 	{
   4671      1.1  christos 	  struct regcache *regcache;
   4672      1.1  christos 
   4673      1.1  christos 	  if (!set_desired_thread ())
   4674      1.1  christos 	    write_enn (cs.own_buf);
   4675      1.1  christos 	  else
   4676      1.1  christos 	    {
   4677      1.1  christos 	      regcache = get_thread_regcache (current_thread, 1);
   4678      1.1  christos 	      registers_to_string (regcache, cs.own_buf);
   4679      1.1  christos 	    }
   4680      1.1  christos 	}
   4681      1.1  christos       break;
   4682      1.1  christos     case 'G':
   4683      1.1  christos       require_running_or_break (cs.own_buf);
   4684      1.1  christos       if (cs.current_traceframe >= 0)
   4685      1.1  christos 	write_enn (cs.own_buf);
   4686      1.1  christos       else
   4687      1.1  christos 	{
   4688      1.1  christos 	  struct regcache *regcache;
   4689      1.1  christos 
   4690      1.1  christos 	  if (!set_desired_thread ())
   4691      1.1  christos 	    write_enn (cs.own_buf);
   4692      1.1  christos 	  else
   4693      1.1  christos 	    {
   4694      1.1  christos 	      regcache = get_thread_regcache (current_thread, 1);
   4695      1.1  christos 	      registers_from_string (regcache, &cs.own_buf[1]);
   4696      1.1  christos 	      write_ok (cs.own_buf);
   4697      1.1  christos 	    }
   4698      1.1  christos 	}
   4699      1.1  christos       break;
   4700      1.1  christos     case 'm':
   4701      1.1  christos       {
   4702      1.1  christos 	require_running_or_break (cs.own_buf);
   4703      1.1  christos 	decode_m_packet (&cs.own_buf[1], &mem_addr, &len);
   4704      1.1  christos 	int res = gdb_read_memory (mem_addr, mem_buf, len);
   4705      1.1  christos 	if (res < 0)
   4706      1.1  christos 	  write_enn (cs.own_buf);
   4707      1.1  christos 	else
   4708      1.1  christos 	  bin2hex (mem_buf, cs.own_buf, res);
   4709      1.1  christos       }
   4710      1.1  christos       break;
   4711      1.1  christos     case 'M':
   4712      1.1  christos       require_running_or_break (cs.own_buf);
   4713      1.1  christos       decode_M_packet (&cs.own_buf[1], &mem_addr, &len, &mem_buf);
   4714      1.1  christos       if (gdb_write_memory (mem_addr, mem_buf, len) == 0)
   4715      1.1  christos 	write_ok (cs.own_buf);
   4716      1.1  christos       else
   4717      1.1  christos 	write_enn (cs.own_buf);
   4718      1.1  christos       break;
   4719      1.1  christos     case 'X':
   4720      1.1  christos       require_running_or_break (cs.own_buf);
   4721      1.1  christos       if (decode_X_packet (&cs.own_buf[1], packet_len - 1,
   4722      1.1  christos 			   &mem_addr, &len, &mem_buf) < 0
   4723      1.1  christos 	  || gdb_write_memory (mem_addr, mem_buf, len) != 0)
   4724      1.1  christos 	write_enn (cs.own_buf);
   4725      1.1  christos       else
   4726      1.1  christos 	write_ok (cs.own_buf);
   4727      1.1  christos       break;
   4728      1.1  christos     case 'C':
   4729      1.1  christos       require_running_or_break (cs.own_buf);
   4730      1.1  christos       hex2bin (cs.own_buf + 1, &sig, 1);
   4731      1.1  christos       if (gdb_signal_to_host_p ((enum gdb_signal) sig))
   4732      1.1  christos 	signal = gdb_signal_to_host ((enum gdb_signal) sig);
   4733      1.1  christos       else
   4734      1.1  christos 	signal = 0;
   4735      1.1  christos       myresume (cs.own_buf, 0, signal);
   4736      1.1  christos       break;
   4737      1.1  christos     case 'S':
   4738      1.1  christos       require_running_or_break (cs.own_buf);
   4739      1.1  christos       hex2bin (cs.own_buf + 1, &sig, 1);
   4740      1.1  christos       if (gdb_signal_to_host_p ((enum gdb_signal) sig))
   4741      1.1  christos 	signal = gdb_signal_to_host ((enum gdb_signal) sig);
   4742      1.1  christos       else
   4743      1.1  christos 	signal = 0;
   4744      1.1  christos       myresume (cs.own_buf, 1, signal);
   4745      1.1  christos       break;
   4746      1.1  christos     case 'c':
   4747      1.1  christos       require_running_or_break (cs.own_buf);
   4748      1.1  christos       signal = 0;
   4749      1.1  christos       myresume (cs.own_buf, 0, signal);
   4750      1.1  christos       break;
   4751      1.1  christos     case 's':
   4752      1.1  christos       require_running_or_break (cs.own_buf);
   4753      1.1  christos       signal = 0;
   4754      1.1  christos       myresume (cs.own_buf, 1, signal);
   4755      1.1  christos       break;
   4756      1.1  christos     case 'Z':  /* insert_ ... */
   4757      1.1  christos       /* Fallthrough.  */
   4758      1.1  christos     case 'z':  /* remove_ ... */
   4759      1.1  christos       {
   4760      1.1  christos 	char *dataptr;
   4761      1.1  christos 	ULONGEST addr;
   4762      1.1  christos 	int kind;
   4763      1.1  christos 	char type = cs.own_buf[1];
   4764      1.1  christos 	int res;
   4765      1.1  christos 	const int insert = ch == 'Z';
   4766      1.1  christos 	const char *p = &cs.own_buf[3];
   4767      1.1  christos 
   4768      1.1  christos 	p = unpack_varlen_hex (p, &addr);
   4769      1.1  christos 	kind = strtol (p + 1, &dataptr, 16);
   4770      1.1  christos 
   4771      1.1  christos 	if (insert)
   4772      1.1  christos 	  {
   4773      1.1  christos 	    struct gdb_breakpoint *bp;
   4774      1.1  christos 
   4775      1.1  christos 	    bp = set_gdb_breakpoint (type, addr, kind, &res);
   4776      1.1  christos 	    if (bp != NULL)
   4777      1.1  christos 	      {
   4778      1.1  christos 		res = 0;
   4779      1.1  christos 
   4780      1.1  christos 		/* GDB may have sent us a list of *point parameters to
   4781      1.1  christos 		   be evaluated on the target's side.  Read such list
   4782      1.1  christos 		   here.  If we already have a list of parameters, GDB
   4783      1.1  christos 		   is telling us to drop that list and use this one
   4784      1.1  christos 		   instead.  */
   4785      1.1  christos 		clear_breakpoint_conditions_and_commands (bp);
   4786      1.1  christos 		const char *options = dataptr;
   4787      1.1  christos 		process_point_options (bp, &options);
   4788      1.1  christos 	      }
   4789      1.1  christos 	  }
   4790      1.1  christos 	else
   4791      1.1  christos 	  res = delete_gdb_breakpoint (type, addr, kind);
   4792      1.1  christos 
   4793      1.1  christos 	if (res == 0)
   4794      1.1  christos 	  write_ok (cs.own_buf);
   4795      1.1  christos 	else if (res == 1)
   4796      1.1  christos 	  /* Unsupported.  */
   4797      1.1  christos 	  cs.own_buf[0] = '\0';
   4798      1.1  christos 	else
   4799      1.1  christos 	  write_enn (cs.own_buf);
   4800      1.1  christos 	break;
   4801      1.1  christos       }
   4802      1.1  christos     case 'k':
   4803      1.1  christos       response_needed = false;
   4804      1.1  christos       if (!target_running ())
   4805      1.1  christos 	/* The packet we received doesn't make sense - but we can't
   4806      1.1  christos 	   reply to it, either.  */
   4807      1.1  christos 	return 0;
   4808      1.1  christos 
   4809      1.1  christos       fprintf (stderr, "Killing all inferiors\n");
   4810      1.1  christos 
   4811      1.1  christos       for_each_process (kill_inferior_callback);
   4812      1.1  christos 
   4813      1.1  christos       /* When using the extended protocol, we wait with no program
   4814      1.1  christos 	 running.  The traditional protocol will exit instead.  */
   4815      1.1  christos       if (extended_protocol)
   4816      1.1  christos 	{
   4817  1.1.1.2  christos 	  cs.last_status.set_exited (GDB_SIGNAL_KILL);
   4818      1.1  christos 	  return 0;
   4819      1.1  christos 	}
   4820      1.1  christos       else
   4821      1.1  christos 	exit (0);
   4822      1.1  christos 
   4823      1.1  christos     case 'T':
   4824      1.1  christos       {
   4825      1.1  christos 	require_running_or_break (cs.own_buf);
   4826      1.1  christos 
   4827      1.1  christos 	ptid_t thread_id = read_ptid (&cs.own_buf[1], NULL);
   4828      1.1  christos 	if (find_thread_ptid (thread_id) == NULL)
   4829      1.1  christos 	  {
   4830      1.1  christos 	    write_enn (cs.own_buf);
   4831      1.1  christos 	    break;
   4832      1.1  christos 	  }
   4833      1.1  christos 
   4834      1.1  christos 	if (mythread_alive (thread_id))
   4835      1.1  christos 	  write_ok (cs.own_buf);
   4836      1.1  christos 	else
   4837      1.1  christos 	  write_enn (cs.own_buf);
   4838      1.1  christos       }
   4839      1.1  christos       break;
   4840      1.1  christos     case 'R':
   4841      1.1  christos       response_needed = false;
   4842      1.1  christos 
   4843      1.1  christos       /* Restarting the inferior is only supported in the extended
   4844      1.1  christos 	 protocol.  */
   4845      1.1  christos       if (extended_protocol)
   4846      1.1  christos 	{
   4847      1.1  christos 	  if (target_running ())
   4848      1.1  christos 	    for_each_process (kill_inferior_callback);
   4849      1.1  christos 
   4850      1.1  christos 	  fprintf (stderr, "GDBserver restarting\n");
   4851      1.1  christos 
   4852      1.1  christos 	  /* Wait till we are at 1st instruction in prog.  */
   4853      1.1  christos 	  if (program_path.get () != NULL)
   4854      1.1  christos 	    {
   4855      1.1  christos 	      target_create_inferior (program_path.get (), program_args);
   4856      1.1  christos 
   4857  1.1.1.2  christos 	      if (cs.last_status.kind () == TARGET_WAITKIND_STOPPED)
   4858      1.1  christos 		{
   4859      1.1  christos 		  /* Stopped at the first instruction of the target
   4860      1.1  christos 		     process.  */
   4861      1.1  christos 		  cs.general_thread = cs.last_ptid;
   4862      1.1  christos 		}
   4863      1.1  christos 	      else
   4864      1.1  christos 		{
   4865      1.1  christos 		  /* Something went wrong.  */
   4866      1.1  christos 		  cs.general_thread = null_ptid;
   4867      1.1  christos 		}
   4868      1.1  christos 	    }
   4869      1.1  christos 	  else
   4870      1.1  christos 	    {
   4871  1.1.1.2  christos 	      cs.last_status.set_exited (GDB_SIGNAL_KILL);
   4872      1.1  christos 	    }
   4873      1.1  christos 	  return 0;
   4874      1.1  christos 	}
   4875      1.1  christos       else
   4876      1.1  christos 	{
   4877      1.1  christos 	  /* It is a request we don't understand.  Respond with an
   4878      1.1  christos 	     empty packet so that gdb knows that we don't support this
   4879      1.1  christos 	     request.  */
   4880      1.1  christos 	  cs.own_buf[0] = '\0';
   4881      1.1  christos 	  break;
   4882      1.1  christos 	}
   4883      1.1  christos     case 'v':
   4884      1.1  christos       /* Extended (long) request.  */
   4885      1.1  christos       handle_v_requests (cs.own_buf, packet_len, &new_packet_len);
   4886      1.1  christos       break;
   4887      1.1  christos 
   4888      1.1  christos     default:
   4889      1.1  christos       /* It is a request we don't understand.  Respond with an empty
   4890      1.1  christos 	 packet so that gdb knows that we don't support this
   4891      1.1  christos 	 request.  */
   4892      1.1  christos       cs.own_buf[0] = '\0';
   4893      1.1  christos       break;
   4894      1.1  christos     }
   4895      1.1  christos 
   4896      1.1  christos   if (new_packet_len != -1)
   4897      1.1  christos     putpkt_binary (cs.own_buf, new_packet_len);
   4898      1.1  christos   else
   4899      1.1  christos     putpkt (cs.own_buf);
   4900      1.1  christos 
   4901      1.1  christos   response_needed = false;
   4902      1.1  christos 
   4903      1.1  christos   if (exit_requested)
   4904      1.1  christos     return -1;
   4905      1.1  christos 
   4906      1.1  christos   return 0;
   4907      1.1  christos }
   4908      1.1  christos 
   4909      1.1  christos /* Event-loop callback for serial events.  */
   4910      1.1  christos 
   4911      1.1  christos void
   4912      1.1  christos handle_serial_event (int err, gdb_client_data client_data)
   4913      1.1  christos {
   4914  1.1.1.2  christos   threads_debug_printf ("handling possible serial event");
   4915      1.1  christos 
   4916      1.1  christos   /* Really handle it.  */
   4917      1.1  christos   if (process_serial_event () < 0)
   4918      1.1  christos     {
   4919      1.1  christos       keep_processing_events = false;
   4920      1.1  christos       return;
   4921      1.1  christos     }
   4922      1.1  christos 
   4923      1.1  christos   /* Be sure to not change the selected thread behind GDB's back.
   4924      1.1  christos      Important in the non-stop mode asynchronous protocol.  */
   4925      1.1  christos   set_desired_thread ();
   4926      1.1  christos }
   4927      1.1  christos 
   4928      1.1  christos /* Push a stop notification on the notification queue.  */
   4929      1.1  christos 
   4930      1.1  christos static void
   4931  1.1.1.2  christos push_stop_notification (ptid_t ptid, const target_waitstatus &status)
   4932      1.1  christos {
   4933      1.1  christos   struct vstop_notif *vstop_notif = new struct vstop_notif;
   4934      1.1  christos 
   4935  1.1.1.2  christos   vstop_notif->status = status;
   4936      1.1  christos   vstop_notif->ptid = ptid;
   4937      1.1  christos   /* Push Stop notification.  */
   4938      1.1  christos   notif_push (&notif_stop, vstop_notif);
   4939      1.1  christos }
   4940      1.1  christos 
   4941      1.1  christos /* Event-loop callback for target events.  */
   4942      1.1  christos 
   4943      1.1  christos void
   4944      1.1  christos handle_target_event (int err, gdb_client_data client_data)
   4945      1.1  christos {
   4946      1.1  christos   client_state &cs = get_client_state ();
   4947  1.1.1.2  christos   threads_debug_printf ("handling possible target event");
   4948      1.1  christos 
   4949      1.1  christos   cs.last_ptid = mywait (minus_one_ptid, &cs.last_status,
   4950      1.1  christos 		      TARGET_WNOHANG, 1);
   4951      1.1  christos 
   4952  1.1.1.2  christos   if (cs.last_status.kind () == TARGET_WAITKIND_NO_RESUMED)
   4953      1.1  christos     {
   4954      1.1  christos       if (gdb_connected () && report_no_resumed)
   4955  1.1.1.2  christos 	push_stop_notification (null_ptid, cs.last_status);
   4956      1.1  christos     }
   4957  1.1.1.2  christos   else if (cs.last_status.kind () != TARGET_WAITKIND_IGNORE)
   4958      1.1  christos     {
   4959      1.1  christos       int pid = cs.last_ptid.pid ();
   4960      1.1  christos       struct process_info *process = find_process_pid (pid);
   4961      1.1  christos       int forward_event = !gdb_connected () || process->gdb_detached;
   4962      1.1  christos 
   4963  1.1.1.2  christos       if (cs.last_status.kind () == TARGET_WAITKIND_EXITED
   4964  1.1.1.2  christos 	  || cs.last_status.kind () == TARGET_WAITKIND_SIGNALLED)
   4965      1.1  christos 	{
   4966      1.1  christos 	  mark_breakpoints_out (process);
   4967      1.1  christos 	  target_mourn_inferior (cs.last_ptid);
   4968      1.1  christos 	}
   4969  1.1.1.2  christos       else if (cs.last_status.kind () == TARGET_WAITKIND_THREAD_EXITED)
   4970      1.1  christos 	;
   4971      1.1  christos       else
   4972      1.1  christos 	{
   4973      1.1  christos 	  /* We're reporting this thread as stopped.  Update its
   4974      1.1  christos 	     "want-stopped" state to what the client wants, until it
   4975      1.1  christos 	     gets a new resume action.  */
   4976      1.1  christos 	  current_thread->last_resume_kind = resume_stop;
   4977      1.1  christos 	  current_thread->last_status = cs.last_status;
   4978      1.1  christos 	}
   4979      1.1  christos 
   4980      1.1  christos       if (forward_event)
   4981      1.1  christos 	{
   4982      1.1  christos 	  if (!target_running ())
   4983      1.1  christos 	    {
   4984      1.1  christos 	      /* The last process exited.  We're done.  */
   4985      1.1  christos 	      exit (0);
   4986      1.1  christos 	    }
   4987      1.1  christos 
   4988  1.1.1.2  christos 	  if (cs.last_status.kind () == TARGET_WAITKIND_EXITED
   4989  1.1.1.2  christos 	      || cs.last_status.kind () == TARGET_WAITKIND_SIGNALLED
   4990  1.1.1.2  christos 	      || cs.last_status.kind () == TARGET_WAITKIND_THREAD_EXITED)
   4991      1.1  christos 	    ;
   4992      1.1  christos 	  else
   4993      1.1  christos 	    {
   4994      1.1  christos 	      /* A thread stopped with a signal, but gdb isn't
   4995      1.1  christos 		 connected to handle it.  Pass it down to the
   4996      1.1  christos 		 inferior, as if it wasn't being traced.  */
   4997      1.1  christos 	      enum gdb_signal signal;
   4998      1.1  christos 
   4999  1.1.1.2  christos 	      threads_debug_printf ("GDB not connected; forwarding event %d for"
   5000  1.1.1.2  christos 				    " [%s]",
   5001  1.1.1.2  christos 				    (int) cs.last_status.kind (),
   5002  1.1.1.2  christos 				    target_pid_to_str (cs.last_ptid).c_str ());
   5003      1.1  christos 
   5004  1.1.1.2  christos 	      if (cs.last_status.kind () == TARGET_WAITKIND_STOPPED)
   5005  1.1.1.2  christos 		signal = cs.last_status.sig ();
   5006      1.1  christos 	      else
   5007      1.1  christos 		signal = GDB_SIGNAL_0;
   5008      1.1  christos 	      target_continue (cs.last_ptid, signal);
   5009      1.1  christos 	    }
   5010      1.1  christos 	}
   5011      1.1  christos       else
   5012  1.1.1.3  christos 	{
   5013  1.1.1.3  christos 	  push_stop_notification (cs.last_ptid, cs.last_status);
   5014  1.1.1.3  christos 
   5015  1.1.1.3  christos 	  if (cs.last_status.kind () == TARGET_WAITKIND_THREAD_EXITED
   5016  1.1.1.3  christos 	      && !target_any_resumed ())
   5017  1.1.1.3  christos 	    {
   5018  1.1.1.3  christos 	      target_waitstatus ws;
   5019  1.1.1.3  christos 	      ws.set_no_resumed ();
   5020  1.1.1.3  christos 	      push_stop_notification (null_ptid, ws);
   5021  1.1.1.3  christos 	    }
   5022  1.1.1.3  christos 	}
   5023      1.1  christos     }
   5024      1.1  christos 
   5025      1.1  christos   /* Be sure to not change the selected thread behind GDB's back.
   5026      1.1  christos      Important in the non-stop mode asynchronous protocol.  */
   5027      1.1  christos   set_desired_thread ();
   5028      1.1  christos }
   5029      1.1  christos 
   5030      1.1  christos /* See gdbsupport/event-loop.h.  */
   5031      1.1  christos 
   5032      1.1  christos int
   5033      1.1  christos invoke_async_signal_handlers ()
   5034      1.1  christos {
   5035      1.1  christos   return 0;
   5036      1.1  christos }
   5037      1.1  christos 
   5038      1.1  christos /* See gdbsupport/event-loop.h.  */
   5039      1.1  christos 
   5040      1.1  christos int
   5041      1.1  christos check_async_event_handlers ()
   5042      1.1  christos {
   5043      1.1  christos   return 0;
   5044      1.1  christos }
   5045      1.1  christos 
   5046      1.1  christos /* See gdbsupport/errors.h  */
   5047      1.1  christos 
   5048      1.1  christos void
   5049      1.1  christos flush_streams ()
   5050      1.1  christos {
   5051      1.1  christos   fflush (stdout);
   5052      1.1  christos   fflush (stderr);
   5053      1.1  christos }
   5054      1.1  christos 
   5055      1.1  christos /* See gdbsupport/gdb_select.h.  */
   5056      1.1  christos 
   5057      1.1  christos int
   5058      1.1  christos gdb_select (int n, fd_set *readfds, fd_set *writefds,
   5059      1.1  christos 	    fd_set *exceptfds, struct timeval *timeout)
   5060      1.1  christos {
   5061      1.1  christos   return select (n, readfds, writefds, exceptfds, timeout);
   5062      1.1  christos }
   5063      1.1  christos 
   5064      1.1  christos #if GDB_SELF_TEST
   5065      1.1  christos namespace selftests
   5066      1.1  christos {
   5067      1.1  christos 
   5068      1.1  christos void
   5069      1.1  christos reset ()
   5070      1.1  christos {}
   5071      1.1  christos 
   5072      1.1  christos } // namespace selftests
   5073      1.1  christos #endif /* GDB_SELF_TEST */
   5074