gdb/gdbserver/remote-utils.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Remote utility routines for the remote server for GDB.
  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "server.h"
  15. #include "terminal.h"
  16. #include "target.h"
  17. #include "gdbthread.h"
  18. #include "tdesc.h"
  19. #include "dll.h"
  20. #include "rsp-low.h"
  21. #include <ctype.h>
  22. #if HAVE_SYS_IOCTL_H
  23. #include <sys/ioctl.h>
  24. #endif
  25. #if HAVE_SYS_FILE_H
  26. #include <sys/file.h>
  27. #endif
  28. #if HAVE_NETINET_IN_H
  29. #include <netinet/in.h>
  30. #endif
  31. #if HAVE_SYS_SOCKET_H
  32. #include <sys/socket.h>
  33. #endif
  34. #if HAVE_NETDB_H
  35. #include <netdb.h>
  36. #endif
  37. #if HAVE_NETINET_TCP_H
  38. #include <netinet/tcp.h>
  39. #endif
  40. #if HAVE_SYS_IOCTL_H
  41. #include <sys/ioctl.h>
  42. #endif
  43. #if HAVE_SIGNAL_H
  44. #include <signal.h>
  45. #endif
  46. #if HAVE_FCNTL_H
  47. #include <fcntl.h>
  48. #endif
  49. #include <sys/time.h>
  50. #include <unistd.h>
  51. #if HAVE_ARPA_INET_H
  52. #include <arpa/inet.h>
  53. #endif
  54. #include <sys/stat.h>

  55. #if USE_WIN32API
  56. #include <winsock2.h>
  57. #endif

  58. #if __QNX__
  59. #include <sys/iomgr.h>
  60. #endif /* __QNX__ */

  61. #ifndef HAVE_SOCKLEN_T
  62. typedef int socklen_t;
  63. #endif

  64. #ifndef IN_PROCESS_AGENT

  65. #if USE_WIN32API
  66. # define INVALID_DESCRIPTOR INVALID_SOCKET
  67. #else
  68. # define INVALID_DESCRIPTOR -1
  69. #endif

  70. /* Extra value for readchar_callback.  */
  71. enum {
  72.   /* The callback is currently not scheduled.  */
  73.   NOT_SCHEDULED = -1
  74. };

  75. /* Status of the readchar callback.
  76.    Either NOT_SCHEDULED or the callback id.  */
  77. static int readchar_callback = NOT_SCHEDULED;

  78. static int readchar (void);
  79. static void reset_readchar (void);
  80. static void reschedule (void);

  81. /* A cache entry for a successfully looked-up symbol.  */
  82. struct sym_cache
  83. {
  84.   char *name;
  85.   CORE_ADDR addr;
  86.   struct sym_cache *next;
  87. };

  88. int remote_debug = 0;
  89. struct ui_file *gdb_stdlog;

  90. static int remote_is_stdio = 0;

  91. static gdb_fildes_t remote_desc = INVALID_DESCRIPTOR;
  92. static gdb_fildes_t listen_desc = INVALID_DESCRIPTOR;

  93. /* FIXME headerize? */
  94. extern int using_threads;
  95. extern int debug_threads;

  96. /* If true, then GDB has requested noack mode.  */
  97. int noack_mode = 0;
  98. /* If true, then we tell GDB to use noack mode by default.  */
  99. int transport_is_reliable = 0;

  100. #ifdef USE_WIN32API
  101. # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
  102. # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
  103. #endif

  104. int
  105. gdb_connected (void)
  106. {
  107.   return remote_desc != INVALID_DESCRIPTOR;
  108. }

  109. /* Return true if the remote connection is over stdio.  */

  110. int
  111. remote_connection_is_stdio (void)
  112. {
  113.   return remote_is_stdio;
  114. }

  115. static void
  116. enable_async_notification (int fd)
  117. {
  118. #if defined(F_SETFL) && defined (FASYNC)
  119.   int save_fcntl_flags;

  120.   save_fcntl_flags = fcntl (fd, F_GETFL, 0);
  121.   fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC);
  122. #if defined (F_SETOWN)
  123.   fcntl (fd, F_SETOWN, getpid ());
  124. #endif
  125. #endif
  126. }

  127. static int
  128. handle_accept_event (int err, gdb_client_data client_data)
  129. {
  130.   struct sockaddr_in sockaddr;
  131.   socklen_t tmp;

  132.   if (debug_threads)
  133.     debug_printf ("handling possible accept event\n");

  134.   tmp = sizeof (sockaddr);
  135.   remote_desc = accept (listen_desc, (struct sockaddr *) &sockaddr, &tmp);
  136.   if (remote_desc == -1)
  137.     perror_with_name ("Accept failed");

  138.   /* Enable TCP keep alive process. */
  139.   tmp = 1;
  140.   setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE,
  141.               (char *) &tmp, sizeof (tmp));

  142.   /* Tell TCP not to delay small packets.  This greatly speeds up
  143.      interactive response. */
  144.   tmp = 1;
  145.   setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
  146.               (char *) &tmp, sizeof (tmp));

  147. #ifndef USE_WIN32API
  148.   signal (SIGPIPE, SIG_IGN);        /* If we don't do this, then gdbserver simply
  149.                                    exits when the remote side dies.  */
  150. #endif

  151.   if (run_once)
  152.     {
  153. #ifndef USE_WIN32API
  154.       close (listen_desc);                /* No longer need this */
  155. #else
  156.       closesocket (listen_desc);        /* No longer need this */
  157. #endif
  158.     }

  159.   /* Even if !RUN_ONCE no longer notice new connections.  Still keep the
  160.      descriptor open for add_file_handler to wait for a new connection.  */
  161.   delete_file_handler (listen_desc);

  162.   /* Convert IP address to string.  */
  163.   fprintf (stderr, "Remote debugging from host %s\n",
  164.            inet_ntoa (sockaddr.sin_addr));

  165.   enable_async_notification (remote_desc);

  166.   /* Register the event loop handler.  */
  167.   add_file_handler (remote_desc, handle_serial_event, NULL);

  168.   /* We have a new GDB connection now.  If we were disconnected
  169.      tracing, there's a window where the target could report a stop
  170.      event to the event loop, and since we have a connection now, we'd
  171.      try to send vStopped notifications to GDB.  But, don't do that
  172.      until GDB as selected all-stop/non-stop, and has queried the
  173.      threads' status ('?').  */
  174.   target_async (0);

  175.   return 0;
  176. }

  177. /* Prepare for a later connection to a remote debugger.
  178.    NAME is the filename used for communication.  */

  179. void
  180. remote_prepare (char *name)
  181. {
  182.   char *port_str;
  183. #ifdef USE_WIN32API
  184.   static int winsock_initialized;
  185. #endif
  186.   int port;
  187.   struct sockaddr_in sockaddr;
  188.   socklen_t tmp;
  189.   char *port_end;

  190.   remote_is_stdio = 0;
  191.   if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
  192.     {
  193.       /* We need to record fact that we're using stdio sooner than the
  194.          call to remote_open so start_inferior knows the connection is
  195.          via stdio.  */
  196.       remote_is_stdio = 1;
  197.       transport_is_reliable = 1;
  198.       return;
  199.     }

  200.   port_str = strchr (name, ':');
  201.   if (port_str == NULL)
  202.     {
  203.       transport_is_reliable = 0;
  204.       return;
  205.     }

  206.   port = strtoul (port_str + 1, &port_end, 10);
  207.   if (port_str[1] == '\0' || *port_end != '\0')
  208.     error ("Bad port argument: %s", name);

  209. #ifdef USE_WIN32API
  210.   if (!winsock_initialized)
  211.     {
  212.       WSADATA wsad;

  213.       WSAStartup (MAKEWORD (1, 0), &wsad);
  214.       winsock_initialized = 1;
  215.     }
  216. #endif

  217.   listen_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
  218.   if (listen_desc == -1)
  219.     perror_with_name ("Can't open socket");

  220.   /* Allow rapid reuse of this port. */
  221.   tmp = 1;
  222.   setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
  223.               sizeof (tmp));

  224.   sockaddr.sin_family = PF_INET;
  225.   sockaddr.sin_port = htons (port);
  226.   sockaddr.sin_addr.s_addr = INADDR_ANY;

  227.   if (bind (listen_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
  228.       || listen (listen_desc, 1))
  229.     perror_with_name ("Can't bind address");

  230.   transport_is_reliable = 1;
  231. }

  232. /* Open a connection to a remote debugger.
  233.    NAME is the filename used for communication.  */

  234. void
  235. remote_open (char *name)
  236. {
  237.   char *port_str;

  238.   port_str = strchr (name, ':');
  239. #ifdef USE_WIN32API
  240.   if (port_str == NULL)
  241.     error ("Only <host>:<port> is supported on this platform.");
  242. #endif

  243.   if (strcmp (name, STDIO_CONNECTION_NAME) == 0)
  244.     {
  245.       fprintf (stderr, "Remote debugging using stdio\n");

  246.       /* Use stdin as the handle of the connection.
  247.          We only select on reads, for example.  */
  248.       remote_desc = fileno (stdin);

  249.       enable_async_notification (remote_desc);

  250.       /* Register the event loop handler.  */
  251.       add_file_handler (remote_desc, handle_serial_event, NULL);
  252.     }
  253. #ifndef USE_WIN32API
  254.   else if (port_str == NULL)
  255.     {
  256.       struct stat statbuf;

  257.       if (stat (name, &statbuf) == 0
  258.           && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
  259.         remote_desc = open (name, O_RDWR);
  260.       else
  261.         {
  262.           errno = EINVAL;
  263.           remote_desc = -1;
  264.         }

  265.       if (remote_desc < 0)
  266.         perror_with_name ("Could not open remote device");

  267. #ifdef HAVE_TERMIOS
  268.       {
  269.         struct termios termios;
  270.         tcgetattr (remote_desc, &termios);

  271.         termios.c_iflag = 0;
  272.         termios.c_oflag = 0;
  273.         termios.c_lflag = 0;
  274.         termios.c_cflag &= ~(CSIZE | PARENB);
  275.         termios.c_cflag |= CLOCAL | CS8;
  276.         termios.c_cc[VMIN] = 1;
  277.         termios.c_cc[VTIME] = 0;

  278.         tcsetattr (remote_desc, TCSANOW, &termios);
  279.       }
  280. #endif

  281. #ifdef HAVE_TERMIO
  282.       {
  283.         struct termio termio;
  284.         ioctl (remote_desc, TCGETA, &termio);

  285.         termio.c_iflag = 0;
  286.         termio.c_oflag = 0;
  287.         termio.c_lflag = 0;
  288.         termio.c_cflag &= ~(CSIZE | PARENB);
  289.         termio.c_cflag |= CLOCAL | CS8;
  290.         termio.c_cc[VMIN] = 1;
  291.         termio.c_cc[VTIME] = 0;

  292.         ioctl (remote_desc, TCSETA, &termio);
  293.       }
  294. #endif

  295. #ifdef HAVE_SGTTY
  296.       {
  297.         struct sgttyb sg;

  298.         ioctl (remote_desc, TIOCGETP, &sg);
  299.         sg.sg_flags = RAW;
  300.         ioctl (remote_desc, TIOCSETP, &sg);
  301.       }
  302. #endif

  303.       fprintf (stderr, "Remote debugging using %s\n", name);

  304.       enable_async_notification (remote_desc);

  305.       /* Register the event loop handler.  */
  306.       add_file_handler (remote_desc, handle_serial_event, NULL);
  307.     }
  308. #endif /* USE_WIN32API */
  309.   else
  310.     {
  311.       int port;
  312.       socklen_t len;
  313.       struct sockaddr_in sockaddr;

  314.       len = sizeof (sockaddr);
  315.       if (getsockname (listen_desc,
  316.                        (struct sockaddr *) &sockaddr, &len) < 0
  317.           || len < sizeof (sockaddr))
  318.         perror_with_name ("Can't determine port");
  319.       port = ntohs (sockaddr.sin_port);

  320.       fprintf (stderr, "Listening on port %d\n", port);
  321.       fflush (stderr);

  322.       /* Register the event loop handler.  */
  323.       add_file_handler (listen_desc, handle_accept_event, NULL);
  324.     }
  325. }

  326. void
  327. remote_close (void)
  328. {
  329.   delete_file_handler (remote_desc);

  330. #ifdef USE_WIN32API
  331.   closesocket (remote_desc);
  332. #else
  333.   if (! remote_connection_is_stdio ())
  334.     close (remote_desc);
  335. #endif
  336.   remote_desc = INVALID_DESCRIPTOR;

  337.   reset_readchar ();
  338. }

  339. #endif

  340. #ifndef IN_PROCESS_AGENT

  341. void
  342. decode_address (CORE_ADDR *addrp, const char *start, int len)
  343. {
  344.   CORE_ADDR addr;
  345.   char ch;
  346.   int i;

  347.   addr = 0;
  348.   for (i = 0; i < len; i++)
  349.     {
  350.       ch = start[i];
  351.       addr = addr << 4;
  352.       addr = addr | (fromhex (ch) & 0x0f);
  353.     }
  354.   *addrp = addr;
  355. }

  356. const char *
  357. decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)
  358. {
  359.   const char *end;

  360.   end = start;
  361.   while (*end != '\0' && *end != ';')
  362.     end++;

  363.   decode_address (addrp, start, end - start);

  364.   if (*end == ';')
  365.     end++;
  366.   return end;
  367. }

  368. #endif

  369. #ifndef IN_PROCESS_AGENT

  370. /* Look for a sequence of characters which can be run-length encoded.
  371.    If there are any, update *CSUM and *P.  Otherwise, output the
  372.    single character.  Return the number of characters consumed.  */

  373. static int
  374. try_rle (char *buf, int remaining, unsigned char *csum, char **p)
  375. {
  376.   int n;

  377.   /* Always output the character.  */
  378.   *csum += buf[0];
  379.   *(*p)++ = buf[0];

  380.   /* Don't go past '~'.  */
  381.   if (remaining > 97)
  382.     remaining = 97;

  383.   for (n = 1; n < remaining; n++)
  384.     if (buf[n] != buf[0])
  385.       break;

  386.   /* N is the index of the first character not the same as buf[0].
  387.      buf[0] is counted twice, so by decrementing N, we get the number
  388.      of characters the RLE sequence will replace.  */
  389.   n--;

  390.   if (n < 3)
  391.     return 1;

  392.   /* Skip the frame characters.  The manual says to skip '+' and '-'
  393.      also, but there's no reason to.  Unfortunately these two unusable
  394.      characters double the encoded length of a four byte zero
  395.      value.  */
  396.   while (n + 29 == '$' || n + 29 == '#')
  397.     n--;

  398.   *csum += '*';
  399.   *(*p)++ = '*';
  400.   *csum += n + 29;
  401.   *(*p)++ = n + 29;

  402.   return n + 1;
  403. }

  404. #endif

  405. #ifndef IN_PROCESS_AGENT

  406. /* Write a PTID to BUF.  Returns BUF+CHARACTERS_WRITTEN.  */

  407. char *
  408. write_ptid (char *buf, ptid_t ptid)
  409. {
  410.   int pid, tid;

  411.   if (multi_process)
  412.     {
  413.       pid = ptid_get_pid (ptid);
  414.       if (pid < 0)
  415.         buf += sprintf (buf, "p-%x.", -pid);
  416.       else
  417.         buf += sprintf (buf, "p%x.", pid);
  418.     }
  419.   tid = ptid_get_lwp (ptid);
  420.   if (tid < 0)
  421.     buf += sprintf (buf, "-%x", -tid);
  422.   else
  423.     buf += sprintf (buf, "%x", tid);

  424.   return buf;
  425. }

  426. ULONGEST
  427. hex_or_minus_one (char *buf, char **obuf)
  428. {
  429.   ULONGEST ret;

  430.   if (strncmp (buf, "-1", 2) == 0)
  431.     {
  432.       ret = (ULONGEST) -1;
  433.       buf += 2;
  434.     }
  435.   else
  436.     buf = unpack_varlen_hex (buf, &ret);

  437.   if (obuf)
  438.     *obuf = buf;

  439.   return ret;
  440. }

  441. /* Extract a PTID from BUF.  If non-null, OBUF is set to the to one
  442.    passed the last parsed char.  Returns null_ptid on error.  */
  443. ptid_t
  444. read_ptid (char *buf, char **obuf)
  445. {
  446.   char *p = buf;
  447.   char *pp;
  448.   ULONGEST pid = 0, tid = 0;

  449.   if (*p == 'p')
  450.     {
  451.       /* Multi-process ptid.  */
  452.       pp = unpack_varlen_hex (p + 1, &pid);
  453.       if (*pp != '.')
  454.         error ("invalid remote ptid: %s\n", p);

  455.       p = pp + 1;

  456.       tid = hex_or_minus_one (p, &pp);

  457.       if (obuf)
  458.         *obuf = pp;
  459.       return ptid_build (pid, tid, 0);
  460.     }

  461.   /* No multi-process.  Just a tid.  */
  462.   tid = hex_or_minus_one (p, &pp);

  463.   /* Since the stub is not sending a process id, then default to
  464.      what's in the current inferior.  */
  465.   pid = ptid_get_pid (current_ptid);

  466.   if (obuf)
  467.     *obuf = pp;
  468.   return ptid_build (pid, tid, 0);
  469. }

  470. /* Write COUNT bytes in BUF to the client.
  471.    The result is the number of bytes written or -1 if error.
  472.    This may return less than COUNT.  */

  473. static int
  474. write_prim (const void *buf, int count)
  475. {
  476.   if (remote_connection_is_stdio ())
  477.     return write (fileno (stdout), buf, count);
  478.   else
  479.     return write (remote_desc, buf, count);
  480. }

  481. /* Read COUNT bytes from the client and store in BUF.
  482.    The result is the number of bytes read or -1 if error.
  483.    This may return less than COUNT.  */

  484. static int
  485. read_prim (void *buf, int count)
  486. {
  487.   if (remote_connection_is_stdio ())
  488.     return read (fileno (stdin), buf, count);
  489.   else
  490.     return read (remote_desc, buf, count);
  491. }

  492. /* Send a packet to the remote machine, with error checking.
  493.    The data of the packet is in BUF, and the length of the
  494.    packet is in CNT.  Returns >= 0 on success, -1 otherwise.  */

  495. static int
  496. putpkt_binary_1 (char *buf, int cnt, int is_notif)
  497. {
  498.   int i;
  499.   unsigned char csum = 0;
  500.   char *buf2;
  501.   char *p;
  502.   int cc;

  503.   buf2 = xmalloc (strlen ("$") + cnt + strlen ("#nn") + 1);

  504.   /* Copy the packet into buffer BUF2, encapsulating it
  505.      and giving it a checksum.  */

  506.   p = buf2;
  507.   if (is_notif)
  508.     *p++ = '%';
  509.   else
  510.     *p++ = '$';

  511.   for (i = 0; i < cnt;)
  512.     i += try_rle (buf + i, cnt - i, &csum, &p);

  513.   *p++ = '#';
  514.   *p++ = tohex ((csum >> 4) & 0xf);
  515.   *p++ = tohex (csum & 0xf);

  516.   *p = '\0';

  517.   /* Send it over and over until we get a positive ack.  */

  518.   do
  519.     {
  520.       if (write_prim (buf2, p - buf2) != p - buf2)
  521.         {
  522.           perror ("putpkt(write)");
  523.           free (buf2);
  524.           return -1;
  525.         }

  526.       if (noack_mode || is_notif)
  527.         {
  528.           /* Don't expect an ack then.  */
  529.           if (remote_debug)
  530.             {
  531.               if (is_notif)
  532.                 fprintf (stderr, "putpkt (\"%s\"); [notif]\n", buf2);
  533.               else
  534.                 fprintf (stderr, "putpkt (\"%s\"); [noack mode]\n", buf2);
  535.               fflush (stderr);
  536.             }
  537.           break;
  538.         }

  539.       if (remote_debug)
  540.         {
  541.           fprintf (stderr, "putpkt (\"%s\"); [looking for ack]\n", buf2);
  542.           fflush (stderr);
  543.         }

  544.       cc = readchar ();

  545.       if (cc < 0)
  546.         {
  547.           free (buf2);
  548.           return -1;
  549.         }

  550.       if (remote_debug)
  551.         {
  552.           fprintf (stderr, "[received '%c' (0x%x)]\n", cc, cc);
  553.           fflush (stderr);
  554.         }

  555.       /* Check for an input interrupt while we're here.  */
  556.       if (cc == '\003' && current_thread != NULL)
  557.         (*the_target->request_interrupt) ();
  558.     }
  559.   while (cc != '+');

  560.   free (buf2);
  561.   return 1;                        /* Success! */
  562. }

  563. int
  564. putpkt_binary (char *buf, int cnt)
  565. {
  566.   return putpkt_binary_1 (buf, cnt, 0);
  567. }

  568. /* Send a packet to the remote machine, with error checking.  The data
  569.    of the packet is in BUF, and the packet should be a NUL-terminated
  570.    string.  Returns >= 0 on success, -1 otherwise.  */

  571. int
  572. putpkt (char *buf)
  573. {
  574.   return putpkt_binary (buf, strlen (buf));
  575. }

  576. int
  577. putpkt_notif (char *buf)
  578. {
  579.   return putpkt_binary_1 (buf, strlen (buf), 1);
  580. }

  581. /* Come here when we get an input interrupt from the remote side.  This
  582.    interrupt should only be active while we are waiting for the child to do
  583.    something.  Thus this assumes readchar:bufcnt is 0.
  584.    About the only thing that should come through is a ^C, which
  585.    will cause us to request child interruption.  */

  586. static void
  587. input_interrupt (int unused)
  588. {
  589.   fd_set readset;
  590.   struct timeval immediate = { 0, 0 };

  591.   /* Protect against spurious interrupts.  This has been observed to
  592.      be a problem under NetBSD 1.4 and 1.5.  */

  593.   FD_ZERO (&readset);
  594.   FD_SET (remote_desc, &readset);
  595.   if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
  596.     {
  597.       int cc;
  598.       char c = 0;

  599.       cc = read_prim (&c, 1);

  600.       if (cc == 0)
  601.         {
  602.           fprintf (stderr, "client connection closed\n");
  603.           return;
  604.         }
  605.       else if (cc != 1 || c != '\003' || current_thread == NULL)
  606.         {
  607.           fprintf (stderr, "input_interrupt, count = %d c = %d ", cc, c);
  608.           if (isprint (c))
  609.             fprintf (stderr, "('%c')\n", c);
  610.           else
  611.             fprintf (stderr, "('\\x%02x')\n", c & 0xff);
  612.           return;
  613.         }

  614.       (*the_target->request_interrupt) ();
  615.     }
  616. }

  617. /* Check if the remote side sent us an interrupt request (^C).  */
  618. void
  619. check_remote_input_interrupt_request (void)
  620. {
  621.   /* This function may be called before establishing communications,
  622.      therefore we need to validate the remote descriptor.  */

  623.   if (remote_desc == INVALID_DESCRIPTOR)
  624.     return;

  625.   input_interrupt (0);
  626. }

  627. /* Asynchronous I/O support.  SIGIO must be enabled when waiting, in order to
  628.    accept Control-C from the client, and must be disabled when talking to
  629.    the client.  */

  630. static void
  631. unblock_async_io (void)
  632. {
  633. #ifndef USE_WIN32API
  634.   sigset_t sigio_set;

  635.   sigemptyset (&sigio_set);
  636.   sigaddset (&sigio_set, SIGIO);
  637.   sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
  638. #endif
  639. }

  640. #ifdef __QNX__
  641. static void
  642. nto_comctrl (int enable)
  643. {
  644.   struct sigevent event;

  645.   if (enable)
  646.     {
  647.       event.sigev_notify = SIGEV_SIGNAL_THREAD;
  648.       event.sigev_signo = SIGIO;
  649.       event.sigev_code = 0;
  650.       event.sigev_value.sival_ptr = NULL;
  651.       event.sigev_priority = -1;
  652.       ionotify (remote_desc, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT,
  653.                 &event);
  654.     }
  655.   else
  656.     ionotify (remote_desc, _NOTIFY_ACTION_POLL, _NOTIFY_COND_INPUT, NULL);
  657. }
  658. #endif /* __QNX__ */


  659. /* Current state of asynchronous I/O.  */
  660. static int async_io_enabled;

  661. /* Enable asynchronous I/O.  */
  662. void
  663. enable_async_io (void)
  664. {
  665.   if (async_io_enabled)
  666.     return;

  667. #ifndef USE_WIN32API
  668.   signal (SIGIO, input_interrupt);
  669. #endif
  670.   async_io_enabled = 1;
  671. #ifdef __QNX__
  672.   nto_comctrl (1);
  673. #endif /* __QNX__ */
  674. }

  675. /* Disable asynchronous I/O.  */
  676. void
  677. disable_async_io (void)
  678. {
  679.   if (!async_io_enabled)
  680.     return;

  681. #ifndef USE_WIN32API
  682.   signal (SIGIO, SIG_IGN);
  683. #endif
  684.   async_io_enabled = 0;
  685. #ifdef __QNX__
  686.   nto_comctrl (0);
  687. #endif /* __QNX__ */

  688. }

  689. void
  690. initialize_async_io (void)
  691. {
  692.   /* Make sure that async I/O starts disabled.  */
  693.   async_io_enabled = 1;
  694.   disable_async_io ();

  695.   /* Make sure the signal is unblocked.  */
  696.   unblock_async_io ();
  697. }

  698. /* Internal buffer used by readchar.
  699.    These are global to readchar because reschedule_remote needs to be
  700.    able to tell whether the buffer is empty.  */

  701. static unsigned char readchar_buf[BUFSIZ];
  702. static int readchar_bufcnt = 0;
  703. static unsigned char *readchar_bufp;

  704. /* Returns next char from remote GDB.  -1 if error.  */

  705. static int
  706. readchar (void)
  707. {
  708.   int ch;

  709.   if (readchar_bufcnt == 0)
  710.     {
  711.       readchar_bufcnt = read_prim (readchar_buf, sizeof (readchar_buf));

  712.       if (readchar_bufcnt <= 0)
  713.         {
  714.           if (readchar_bufcnt == 0)
  715.             fprintf (stderr, "readchar: Got EOF\n");
  716.           else
  717.             perror ("readchar");

  718.           return -1;
  719.         }

  720.       readchar_bufp = readchar_buf;
  721.     }

  722.   readchar_bufcnt--;
  723.   ch = *readchar_bufp++;
  724.   reschedule ();
  725.   return ch;
  726. }

  727. /* Reset the readchar state machine.  */

  728. static void
  729. reset_readchar (void)
  730. {
  731.   readchar_bufcnt = 0;
  732.   if (readchar_callback != NOT_SCHEDULED)
  733.     {
  734.       delete_callback_event (readchar_callback);
  735.       readchar_callback = NOT_SCHEDULED;
  736.     }
  737. }

  738. /* Process remaining data in readchar_buf.  */

  739. static int
  740. process_remaining (void *context)
  741. {
  742.   int res;

  743.   /* This is a one-shot event.  */
  744.   readchar_callback = NOT_SCHEDULED;

  745.   if (readchar_bufcnt > 0)
  746.     res = handle_serial_event (0, NULL);
  747.   else
  748.     res = 0;

  749.   return res;
  750. }

  751. /* If there is still data in the buffer, queue another event to process it,
  752.    we can't sleep in select yet.  */

  753. static void
  754. reschedule (void)
  755. {
  756.   if (readchar_bufcnt > 0 && readchar_callback == NOT_SCHEDULED)
  757.     readchar_callback = append_callback_event (process_remaining, NULL);
  758. }

  759. /* Read a packet from the remote machine, with error checking,
  760.    and store it in BUF.  Returns length of packet, or negative if error. */

  761. int
  762. getpkt (char *buf)
  763. {
  764.   char *bp;
  765.   unsigned char csum, c1, c2;
  766.   int c;

  767.   while (1)
  768.     {
  769.       csum = 0;

  770.       while (1)
  771.         {
  772.           c = readchar ();
  773.           if (c == '$')
  774.             break;
  775.           if (remote_debug)
  776.             {
  777.               fprintf (stderr, "[getpkt: discarding char '%c']\n", c);
  778.               fflush (stderr);
  779.             }

  780.           if (c < 0)
  781.             return -1;
  782.         }

  783.       bp = buf;
  784.       while (1)
  785.         {
  786.           c = readchar ();
  787.           if (c < 0)
  788.             return -1;
  789.           if (c == '#')
  790.             break;
  791.           *bp++ = c;
  792.           csum += c;
  793.         }
  794.       *bp = 0;

  795.       c1 = fromhex (readchar ());
  796.       c2 = fromhex (readchar ());

  797.       if (csum == (c1 << 4) + c2)
  798.         break;

  799.       if (noack_mode)
  800.         {
  801.           fprintf (stderr,
  802.                    "Bad checksum, sentsum=0x%x, csum=0x%x, "
  803.                    "buf=%s [no-ack-mode, Bad medium?]\n",
  804.                    (c1 << 4) + c2, csum, buf);
  805.           /* Not much we can do, GDB wasn't expecting an ack/nac.  */
  806.           break;
  807.         }

  808.       fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
  809.                (c1 << 4) + c2, csum, buf);
  810.       if (write_prim ("-", 1) != 1)
  811.         return -1;
  812.     }

  813.   if (!noack_mode)
  814.     {
  815.       if (remote_debug)
  816.         {
  817.           fprintf (stderr, "getpkt (\"%s\");  [sending ack] \n", buf);
  818.           fflush (stderr);
  819.         }

  820.       if (write_prim ("+", 1) != 1)
  821.         return -1;

  822.       if (remote_debug)
  823.         {
  824.           fprintf (stderr, "[sent ack]\n");
  825.           fflush (stderr);
  826.         }
  827.     }
  828.   else
  829.     {
  830.       if (remote_debug)
  831.         {
  832.           fprintf (stderr, "getpkt (\"%s\");  [no ack sent] \n", buf);
  833.           fflush (stderr);
  834.         }
  835.     }

  836.   return bp - buf;
  837. }

  838. void
  839. write_ok (char *buf)
  840. {
  841.   buf[0] = 'O';
  842.   buf[1] = 'K';
  843.   buf[2] = '\0';
  844. }

  845. void
  846. write_enn (char *buf)
  847. {
  848.   /* Some day, we should define the meanings of the error codes... */
  849.   buf[0] = 'E';
  850.   buf[1] = '0';
  851.   buf[2] = '1';
  852.   buf[3] = '\0';
  853. }

  854. #endif

  855. #ifndef IN_PROCESS_AGENT

  856. static char *
  857. outreg (struct regcache *regcache, int regno, char *buf)
  858. {
  859.   if ((regno >> 12) != 0)
  860.     *buf++ = tohex ((regno >> 12) & 0xf);
  861.   if ((regno >> 8) != 0)
  862.     *buf++ = tohex ((regno >> 8) & 0xf);
  863.   *buf++ = tohex ((regno >> 4) & 0xf);
  864.   *buf++ = tohex (regno & 0xf);
  865.   *buf++ = ':';
  866.   collect_register_as_string (regcache, regno, buf);
  867.   buf += 2 * register_size (regcache->tdesc, regno);
  868.   *buf++ = ';';

  869.   return buf;
  870. }

  871. void
  872. new_thread_notify (int id)
  873. {
  874.   char own_buf[256];

  875.   /* The `n' response is not yet part of the remote protocol.  Do nothing.  */
  876.   if (1)
  877.     return;

  878.   if (server_waiting == 0)
  879.     return;

  880.   sprintf (own_buf, "n%x", id);
  881.   disable_async_io ();
  882.   putpkt (own_buf);
  883.   enable_async_io ();
  884. }

  885. void
  886. dead_thread_notify (int id)
  887. {
  888.   char own_buf[256];

  889.   /* The `x' response is not yet part of the remote protocol.  Do nothing.  */
  890.   if (1)
  891.     return;

  892.   sprintf (own_buf, "x%x", id);
  893.   disable_async_io ();
  894.   putpkt (own_buf);
  895.   enable_async_io ();
  896. }

  897. void
  898. prepare_resume_reply (char *buf, ptid_t ptid,
  899.                       struct target_waitstatus *status)
  900. {
  901.   if (debug_threads)
  902.     debug_printf ("Writing resume reply for %s:%d\n",
  903.                   target_pid_to_str (ptid), status->kind);

  904.   switch (status->kind)
  905.     {
  906.     case TARGET_WAITKIND_STOPPED:
  907.       {
  908.         struct thread_info *saved_thread;
  909.         const char **regp;
  910.         struct regcache *regcache;

  911.         sprintf (buf, "T%02x", status->value.sig);
  912.         buf += strlen (buf);

  913.         saved_thread = current_thread;

  914.         current_thread = find_thread_ptid (ptid);

  915.         regp = current_target_desc ()->expedite_regs;

  916.         regcache = get_thread_regcache (current_thread, 1);

  917.         if (the_target->stopped_by_watchpoint != NULL
  918.             && (*the_target->stopped_by_watchpoint) ())
  919.           {
  920.             CORE_ADDR addr;
  921.             int i;

  922.             strncpy (buf, "watch:", 6);
  923.             buf += 6;

  924.             addr = (*the_target->stopped_data_address) ();

  925.             /* Convert each byte of the address into two hexadecimal
  926.                chars.  Note that we take sizeof (void *) instead of
  927.                sizeof (addr); this is to avoid sending a 64-bit
  928.                address to a 32-bit GDB.  */
  929.             for (i = sizeof (void *) * 2; i > 0; i--)
  930.               *buf++ = tohex ((addr >> (i - 1) * 4) & 0xf);
  931.             *buf++ = ';';
  932.           }

  933.         while (*regp)
  934.           {
  935.             buf = outreg (regcache, find_regno (regcache->tdesc, *regp), buf);
  936.             regp ++;
  937.           }
  938.         *buf = '\0';

  939.         /* Formerly, if the debugger had not used any thread features
  940.            we would not burden it with a thread status response.  This
  941.            was for the benefit of GDB 4.13 and older.  However, in
  942.            recent GDB versions the check (``if (cont_thread != 0)'')
  943.            does not have the desired effect because of sillyness in
  944.            the way that the remote protocol handles specifying a
  945.            thread.  Since thread support relies on qSymbol support
  946.            anyway, assume GDB can handle threads.  */

  947.         if (using_threads && !disable_packet_Tthread)
  948.           {
  949.             /* This if (1) ought to be unnecessary.  But remote_wait
  950.                in GDB will claim this event belongs to inferior_ptid
  951.                if we do not specify a thread, and there's no way for
  952.                gdbserver to know what inferior_ptid is.  */
  953.             if (1 || !ptid_equal (general_thread, ptid))
  954.               {
  955.                 int core = -1;
  956.                 /* In non-stop, don't change the general thread behind
  957.                    GDB's back.  */
  958.                 if (!non_stop)
  959.                   general_thread = ptid;
  960.                 sprintf (buf, "thread:");
  961.                 buf += strlen (buf);
  962.                 buf = write_ptid (buf, ptid);
  963.                 strcat (buf, ";");
  964.                 buf += strlen (buf);

  965.                 core = target_core_of_thread (ptid);

  966.                 if (core != -1)
  967.                   {
  968.                     sprintf (buf, "core:");
  969.                     buf += strlen (buf);
  970.                     sprintf (buf, "%x", core);
  971.                     strcat (buf, ";");
  972.                     buf += strlen (buf);
  973.                   }
  974.               }
  975.           }

  976.         if (dlls_changed)
  977.           {
  978.             strcpy (buf, "library:;");
  979.             buf += strlen (buf);
  980.             dlls_changed = 0;
  981.           }

  982.         current_thread = saved_thread;
  983.       }
  984.       break;
  985.     case TARGET_WAITKIND_EXITED:
  986.       if (multi_process)
  987.         sprintf (buf, "W%x;process:%x",
  988.                  status->value.integer, ptid_get_pid (ptid));
  989.       else
  990.         sprintf (buf, "W%02x", status->value.integer);
  991.       break;
  992.     case TARGET_WAITKIND_SIGNALLED:
  993.       if (multi_process)
  994.         sprintf (buf, "X%x;process:%x",
  995.                  status->value.sig, ptid_get_pid (ptid));
  996.       else
  997.         sprintf (buf, "X%02x", status->value.sig);
  998.       break;
  999.     default:
  1000.       error ("unhandled waitkind");
  1001.       break;
  1002.     }
  1003. }

  1004. void
  1005. decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
  1006. {
  1007.   int i = 0, j = 0;
  1008.   char ch;
  1009.   *mem_addr_ptr = *len_ptr = 0;

  1010.   while ((ch = from[i++]) != ',')
  1011.     {
  1012.       *mem_addr_ptr = *mem_addr_ptr << 4;
  1013.       *mem_addr_ptr |= fromhex (ch) & 0x0f;
  1014.     }

  1015.   for (j = 0; j < 4; j++)
  1016.     {
  1017.       if ((ch = from[i++]) == 0)
  1018.         break;
  1019.       *len_ptr = *len_ptr << 4;
  1020.       *len_ptr |= fromhex (ch) & 0x0f;
  1021.     }
  1022. }

  1023. void
  1024. decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
  1025.                  unsigned char **to_p)
  1026. {
  1027.   int i = 0;
  1028.   char ch;
  1029.   *mem_addr_ptr = *len_ptr = 0;

  1030.   while ((ch = from[i++]) != ',')
  1031.     {
  1032.       *mem_addr_ptr = *mem_addr_ptr << 4;
  1033.       *mem_addr_ptr |= fromhex (ch) & 0x0f;
  1034.     }

  1035.   while ((ch = from[i++]) != ':')
  1036.     {
  1037.       *len_ptr = *len_ptr << 4;
  1038.       *len_ptr |= fromhex (ch) & 0x0f;
  1039.     }

  1040.   if (*to_p == NULL)
  1041.     *to_p = xmalloc (*len_ptr);

  1042.   hex2bin (&from[i++], *to_p, *len_ptr);
  1043. }

  1044. int
  1045. decode_X_packet (char *from, int packet_len, CORE_ADDR *mem_addr_ptr,
  1046.                  unsigned int *len_ptr, unsigned char **to_p)
  1047. {
  1048.   int i = 0;
  1049.   char ch;
  1050.   *mem_addr_ptr = *len_ptr = 0;

  1051.   while ((ch = from[i++]) != ',')
  1052.     {
  1053.       *mem_addr_ptr = *mem_addr_ptr << 4;
  1054.       *mem_addr_ptr |= fromhex (ch) & 0x0f;
  1055.     }

  1056.   while ((ch = from[i++]) != ':')
  1057.     {
  1058.       *len_ptr = *len_ptr << 4;
  1059.       *len_ptr |= fromhex (ch) & 0x0f;
  1060.     }

  1061.   if (*to_p == NULL)
  1062.     *to_p = xmalloc (*len_ptr);

  1063.   if (remote_unescape_input ((const gdb_byte *) &from[i], packet_len - i,
  1064.                              *to_p, *len_ptr) != *len_ptr)
  1065.     return -1;

  1066.   return 0;
  1067. }

  1068. /* Decode a qXfer write request.  */

  1069. int
  1070. decode_xfer_write (char *buf, int packet_len, CORE_ADDR *offset,
  1071.                    unsigned int *len, unsigned char *data)
  1072. {
  1073.   char ch;
  1074.   char *b = buf;

  1075.   /* Extract the offset.  */
  1076.   *offset = 0;
  1077.   while ((ch = *buf++) != ':')
  1078.     {
  1079.       *offset = *offset << 4;
  1080.       *offset |= fromhex (ch) & 0x0f;
  1081.     }

  1082.   /* Get encoded data.  */
  1083.   packet_len -= buf - b;
  1084.   *len = remote_unescape_input ((const gdb_byte *) buf, packet_len,
  1085.                                 data, packet_len);
  1086.   return 0;
  1087. }

  1088. /* Decode the parameters of a qSearch:memory packet.  */

  1089. int
  1090. decode_search_memory_packet (const char *buf, int packet_len,
  1091.                              CORE_ADDR *start_addrp,
  1092.                              CORE_ADDR *search_space_lenp,
  1093.                              gdb_byte *pattern, unsigned int *pattern_lenp)
  1094. {
  1095.   const char *p = buf;

  1096.   p = decode_address_to_semicolon (start_addrp, p);
  1097.   p = decode_address_to_semicolon (search_space_lenp, p);
  1098.   packet_len -= p - buf;
  1099.   *pattern_lenp = remote_unescape_input ((const gdb_byte *) p, packet_len,
  1100.                                          pattern, packet_len);
  1101.   return 0;
  1102. }

  1103. static void
  1104. free_sym_cache (struct sym_cache *sym)
  1105. {
  1106.   if (sym != NULL)
  1107.     {
  1108.       free (sym->name);
  1109.       free (sym);
  1110.     }
  1111. }

  1112. void
  1113. clear_symbol_cache (struct sym_cache **symcache_p)
  1114. {
  1115.   struct sym_cache *sym, *next;

  1116.   /* Check the cache first.  */
  1117.   for (sym = *symcache_p; sym; sym = next)
  1118.     {
  1119.       next = sym->next;
  1120.       free_sym_cache (sym);
  1121.     }

  1122.   *symcache_p = NULL;
  1123. }

  1124. /* Get the address of NAME, and return it in ADDRP if found.  if
  1125.    MAY_ASK_GDB is false, assume symbol cache misses are failures.
  1126.    Returns 1 if the symbol is found, 0 if it is not, -1 on error.  */

  1127. int
  1128. look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)
  1129. {
  1130.   char own_buf[266], *p, *q;
  1131.   int len;
  1132.   struct sym_cache *sym;
  1133.   struct process_info *proc;

  1134.   proc = current_process ();

  1135.   /* Check the cache first.  */
  1136.   for (sym = proc->symbol_cache; sym; sym = sym->next)
  1137.     if (strcmp (name, sym->name) == 0)
  1138.       {
  1139.         *addrp = sym->addr;
  1140.         return 1;
  1141.       }

  1142.   /* It might not be an appropriate time to look up a symbol,
  1143.      e.g. while we're trying to fetch registers.  */
  1144.   if (!may_ask_gdb)
  1145.     return 0;

  1146.   /* Send the request.  */
  1147.   strcpy (own_buf, "qSymbol:");
  1148.   bin2hex ((const gdb_byte *) name, own_buf + strlen ("qSymbol:"),
  1149.           strlen (name));
  1150.   if (putpkt (own_buf) < 0)
  1151.     return -1;

  1152.   /* FIXME:  Eventually add buffer overflow checking (to getpkt?)  */
  1153.   len = getpkt (own_buf);
  1154.   if (len < 0)
  1155.     return -1;

  1156.   /* We ought to handle pretty much any packet at this point while we
  1157.      wait for the qSymbol "response".  That requires re-entering the
  1158.      main loop.  For now, this is an adequate approximation; allow
  1159.      GDB to read from memory while it figures out the address of the
  1160.      symbol.  */
  1161.   while (own_buf[0] == 'm')
  1162.     {
  1163.       CORE_ADDR mem_addr;
  1164.       unsigned char *mem_buf;
  1165.       unsigned int mem_len;

  1166.       decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
  1167.       mem_buf = xmalloc (mem_len);
  1168.       if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
  1169.         bin2hex (mem_buf, own_buf, mem_len);
  1170.       else
  1171.         write_enn (own_buf);
  1172.       free (mem_buf);
  1173.       if (putpkt (own_buf) < 0)
  1174.         return -1;
  1175.       len = getpkt (own_buf);
  1176.       if (len < 0)
  1177.         return -1;
  1178.     }

  1179.   if (strncmp (own_buf, "qSymbol:", strlen ("qSymbol:")) != 0)
  1180.     {
  1181.       warning ("Malformed response to qSymbol, ignoring: %s\n", own_buf);
  1182.       return -1;
  1183.     }

  1184.   p = own_buf + strlen ("qSymbol:");
  1185.   q = p;
  1186.   while (*q && *q != ':')
  1187.     q++;

  1188.   /* Make sure we found a value for the symbol.  */
  1189.   if (p == q || *q == '\0')
  1190.     return 0;

  1191.   decode_address (addrp, p, q - p);

  1192.   /* Save the symbol in our cache.  */
  1193.   sym = xmalloc (sizeof (*sym));
  1194.   sym->name = xstrdup (name);
  1195.   sym->addr = *addrp;
  1196.   sym->next = proc->symbol_cache;
  1197.   proc->symbol_cache = sym;

  1198.   return 1;
  1199. }

  1200. /* Relocate an instruction to execute at a different address.  OLDLOC
  1201.    is the address in the inferior memory where the instruction to
  1202.    relocate is currently at.  On input, TO points to the destination
  1203.    where we want the instruction to be copied (and possibly adjusted)
  1204.    to.  On output, it points to one past the end of the resulting
  1205.    instruction(s).  The effect of executing the instruction at TO
  1206.    shall be the same as if executing it at OLDLOC.  For example, call
  1207.    instructions that implicitly push the return address on the stack
  1208.    should be adjusted to return to the instruction after OLDLOC;
  1209.    relative branches, and other PC-relative instructions need the
  1210.    offset adjusted; etc.  Returns 0 on success, -1 on failure.  */

  1211. int
  1212. relocate_instruction (CORE_ADDR *to, CORE_ADDR oldloc)
  1213. {
  1214.   char own_buf[266];
  1215.   int len;
  1216.   ULONGEST written = 0;

  1217.   /* Send the request.  */
  1218.   strcpy (own_buf, "qRelocInsn:");
  1219.   sprintf (own_buf, "qRelocInsn:%s;%s", paddress (oldloc),
  1220.            paddress (*to));
  1221.   if (putpkt (own_buf) < 0)
  1222.     return -1;

  1223.   /* FIXME:  Eventually add buffer overflow checking (to getpkt?)  */
  1224.   len = getpkt (own_buf);
  1225.   if (len < 0)
  1226.     return -1;

  1227.   /* We ought to handle pretty much any packet at this point while we
  1228.      wait for the qRelocInsn "response".  That requires re-entering
  1229.      the main loop.  For now, this is an adequate approximation; allow
  1230.      GDB to access memory.  */
  1231.   while (own_buf[0] == 'm' || own_buf[0] == 'M' || own_buf[0] == 'X')
  1232.     {
  1233.       CORE_ADDR mem_addr;
  1234.       unsigned char *mem_buf = NULL;
  1235.       unsigned int mem_len;

  1236.       if (own_buf[0] == 'm')
  1237.         {
  1238.           decode_m_packet (&own_buf[1], &mem_addr, &mem_len);
  1239.           mem_buf = xmalloc (mem_len);
  1240.           if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
  1241.             bin2hex (mem_buf, own_buf, mem_len);
  1242.           else
  1243.             write_enn (own_buf);
  1244.         }
  1245.       else if (own_buf[0] == 'X')
  1246.         {
  1247.           if (decode_X_packet (&own_buf[1], len - 1, &mem_addr,
  1248.                                &mem_len, &mem_buf) < 0
  1249.               || write_inferior_memory (mem_addr, mem_buf, mem_len) != 0)
  1250.             write_enn (own_buf);
  1251.           else
  1252.             write_ok (own_buf);
  1253.         }
  1254.       else
  1255.         {
  1256.           decode_M_packet (&own_buf[1], &mem_addr, &mem_len, &mem_buf);
  1257.           if (write_inferior_memory (mem_addr, mem_buf, mem_len) == 0)
  1258.             write_ok (own_buf);
  1259.           else
  1260.             write_enn (own_buf);
  1261.         }
  1262.       free (mem_buf);
  1263.       if (putpkt (own_buf) < 0)
  1264.         return -1;
  1265.       len = getpkt (own_buf);
  1266.       if (len < 0)
  1267.         return -1;
  1268.     }

  1269.   if (own_buf[0] == 'E')
  1270.     {
  1271.       warning ("An error occurred while relocating an instruction: %s\n",
  1272.                own_buf);
  1273.       return -1;
  1274.     }

  1275.   if (strncmp (own_buf, "qRelocInsn:", strlen ("qRelocInsn:")) != 0)
  1276.     {
  1277.       warning ("Malformed response to qRelocInsn, ignoring: %s\n",
  1278.                own_buf);
  1279.       return -1;
  1280.     }

  1281.   unpack_varlen_hex (own_buf + strlen ("qRelocInsn:"), &written);

  1282.   *to += written;
  1283.   return 0;
  1284. }

  1285. void
  1286. monitor_output (const char *msg)
  1287. {
  1288.   int len = strlen (msg);
  1289.   char *buf = xmalloc (len * 2 + 2);

  1290.   buf[0] = 'O';
  1291.   bin2hex ((const gdb_byte *) msg, buf + 1, len);

  1292.   putpkt (buf);
  1293.   free (buf);
  1294. }

  1295. #endif