gdb/gdbserver/hostio.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Host file transfer support for gdbserver.
  2.    Copyright (C) 2007-2015 Free Software Foundation, Inc.

  3.    Contributed by CodeSourcery.

  4.    This file is part of GDB.

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

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

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

  15. #include "server.h"
  16. #include "gdb/fileio.h"
  17. #include "hostio.h"

  18. #include <fcntl.h>
  19. #include <limits.h>
  20. #include <unistd.h>

  21. extern int remote_debug;

  22. struct fd_list
  23. {
  24.   int fd;
  25.   struct fd_list *next;
  26. };

  27. static struct fd_list *open_fds;

  28. static int
  29. safe_fromhex (char a, int *nibble)
  30. {
  31.   if (a >= '0' && a <= '9')
  32.     *nibble = a - '0';
  33.   else if (a >= 'a' && a <= 'f')
  34.     *nibble = a - 'a' + 10;
  35.   else if (a >= 'A' && a <= 'F')
  36.     *nibble = a - 'A' + 10;
  37.   else
  38.     return -1;

  39.   return 0;
  40. }

  41. /* Filenames are hex encoded, so the maximum we can handle is half the
  42.    packet buffer size.  Cap to PATH_MAX, if it is shorter.  */
  43. #if !defined (PATH_MAX) || (PATH_MAX > (PBUFSIZ / 2 + 1))
  44. #  define HOSTIO_PATH_MAX (PBUFSIZ / 2 + 1)
  45. #else
  46. #  define HOSTIO_PATH_MAX PATH_MAX
  47. #endif

  48. static int
  49. require_filename (char **pp, char *filename)
  50. {
  51.   int count;
  52.   char *p;

  53.   p = *pp;
  54.   count = 0;

  55.   while (*p && *p != ',')
  56.     {
  57.       int nib1, nib2;

  58.       /* Don't allow overflow.  */
  59.       if (count >= HOSTIO_PATH_MAX - 1)
  60.         return -1;

  61.       if (safe_fromhex (p[0], &nib1)
  62.           || safe_fromhex (p[1], &nib2))
  63.         return -1;

  64.       filename[count++] = nib1 * 16 + nib2;
  65.       p += 2;
  66.     }

  67.   filename[count] = '\0';
  68.   *pp = p;
  69.   return 0;
  70. }

  71. static int
  72. require_int (char **pp, int *value)
  73. {
  74.   char *p;
  75.   int count;

  76.   p = *pp;
  77.   *value = 0;
  78.   count = 0;

  79.   while (*p && *p != ',')
  80.     {
  81.       int nib;

  82.       /* Don't allow overflow.  */
  83.       if (count >= 7)
  84.         return -1;

  85.       if (safe_fromhex (p[0], &nib))
  86.         return -1;
  87.       *value = *value * 16 + nib;
  88.       p++;
  89.       count++;
  90.     }

  91.   *pp = p;
  92.   return 0;
  93. }

  94. static int
  95. require_data (char *p, int p_len, char **data, int *data_len)
  96. {
  97.   int input_index, output_index, escaped;

  98.   *data = xmalloc (p_len);

  99.   output_index = 0;
  100.   escaped = 0;
  101.   for (input_index = 0; input_index < p_len; input_index++)
  102.     {
  103.       char b = p[input_index];

  104.       if (escaped)
  105.         {
  106.           (*data)[output_index++] = b ^ 0x20;
  107.           escaped = 0;
  108.         }
  109.       else if (b == '}')
  110.         escaped = 1;
  111.       else
  112.         (*data)[output_index++] = b;
  113.     }

  114.   if (escaped)
  115.     {
  116.       free (*data);
  117.       return -1;
  118.     }

  119.   *data_len = output_index;
  120.   return 0;
  121. }

  122. static int
  123. require_comma (char **pp)
  124. {
  125.   if (**pp == ',')
  126.     {
  127.       (*pp)++;
  128.       return 0;
  129.     }
  130.   else
  131.     return -1;
  132. }

  133. static int
  134. require_end (char *p)
  135. {
  136.   if (*p == '\0')
  137.     return 0;
  138.   else
  139.     return -1;
  140. }

  141. static int
  142. require_valid_fd (int fd)
  143. {
  144.   struct fd_list *fd_ptr;

  145.   for (fd_ptr = open_fds; fd_ptr != NULL; fd_ptr = fd_ptr->next)
  146.     if (fd_ptr->fd == fd)
  147.       return 0;

  148.   return -1;
  149. }

  150. /* Fill in own_buf with the last hostio error packet, however it
  151.    suitable for the target.  */
  152. static void
  153. hostio_error (char *own_buf)
  154. {
  155.   the_target->hostio_last_error (own_buf);
  156. }

  157. static void
  158. hostio_packet_error (char *own_buf)
  159. {
  160.   sprintf (own_buf, "F-1,%x", FILEIO_EINVAL);
  161. }

  162. static void
  163. hostio_reply (char *own_buf, int result)
  164. {
  165.   sprintf (own_buf, "F%x", result);
  166. }

  167. static int
  168. hostio_reply_with_data (char *own_buf, char *buffer, int len,
  169.                         int *new_packet_len)
  170. {
  171.   int input_index, output_index, out_maxlen;

  172.   sprintf (own_buf, "F%x;", len);
  173.   output_index = strlen (own_buf);

  174.   out_maxlen = PBUFSIZ;

  175.   for (input_index = 0; input_index < len; input_index++)
  176.     {
  177.       char b = buffer[input_index];

  178.       if (b == '$' || b == '#' || b == '}' || b == '*')
  179.         {
  180.           /* These must be escaped.  */
  181.           if (output_index + 2 > out_maxlen)
  182.             break;
  183.           own_buf[output_index++] = '}';
  184.           own_buf[output_index++] = b ^ 0x20;
  185.         }
  186.       else
  187.         {
  188.           if (output_index + 1 > out_maxlen)
  189.             break;
  190.           own_buf[output_index++] = b;
  191.         }
  192.     }

  193.   *new_packet_len = output_index;
  194.   return input_index;
  195. }

  196. static int
  197. fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
  198. {
  199.   int open_flags = 0;

  200.   if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
  201.     return -1;

  202.   if (fileio_open_flags & FILEIO_O_CREAT)
  203.     open_flags |= O_CREAT;
  204.   if (fileio_open_flags & FILEIO_O_EXCL)
  205.     open_flags |= O_EXCL;
  206.   if (fileio_open_flags & FILEIO_O_TRUNC)
  207.     open_flags |= O_TRUNC;
  208.   if (fileio_open_flags & FILEIO_O_APPEND)
  209.     open_flags |= O_APPEND;
  210.   if (fileio_open_flags & FILEIO_O_RDONLY)
  211.     open_flags |= O_RDONLY;
  212.   if (fileio_open_flags & FILEIO_O_WRONLY)
  213.     open_flags |= O_WRONLY;
  214.   if (fileio_open_flags & FILEIO_O_RDWR)
  215.     open_flags |= O_RDWR;
  216. /* On systems supporting binary and text mode, always open files in
  217.    binary mode. */
  218. #ifdef O_BINARY
  219.   open_flags |= O_BINARY;
  220. #endif

  221.   *open_flags_p = open_flags;
  222.   return 0;
  223. }

  224. static void
  225. handle_open (char *own_buf)
  226. {
  227.   char filename[HOSTIO_PATH_MAX];
  228.   char *p;
  229.   int fileio_flags, mode, flags, fd;
  230.   struct fd_list *new_fd;

  231.   p = own_buf + strlen ("vFile:open:");

  232.   if (require_filename (&p, filename)
  233.       || require_comma (&p)
  234.       || require_int (&p, &fileio_flags)
  235.       || require_comma (&p)
  236.       || require_int (&p, &mode)
  237.       || require_end (p)
  238.       || fileio_open_flags_to_host (fileio_flags, &flags))
  239.     {
  240.       hostio_packet_error (own_buf);
  241.       return;
  242.     }

  243.   /* We do not need to convert MODE, since the fileio protocol
  244.      uses the standard values.  */
  245.   fd = open (filename, flags, mode);

  246.   if (fd == -1)
  247.     {
  248.       hostio_error (own_buf);
  249.       return;
  250.     }

  251.   /* Record the new file descriptor.  */
  252.   new_fd = xmalloc (sizeof (struct fd_list));
  253.   new_fd->fd = fd;
  254.   new_fd->next = open_fds;
  255.   open_fds = new_fd;

  256.   hostio_reply (own_buf, fd);
  257. }

  258. static void
  259. handle_pread (char *own_buf, int *new_packet_len)
  260. {
  261.   int fd, ret, len, offset, bytes_sent;
  262.   char *p, *data;

  263.   p = own_buf + strlen ("vFile:pread:");

  264.   if (require_int (&p, &fd)
  265.       || require_comma (&p)
  266.       || require_valid_fd (fd)
  267.       || require_int (&p, &len)
  268.       || require_comma (&p)
  269.       || require_int (&p, &offset)
  270.       || require_end (p))
  271.     {
  272.       hostio_packet_error (own_buf);
  273.       return;
  274.     }

  275.   data = xmalloc (len);
  276. #ifdef HAVE_PREAD
  277.   ret = pread (fd, data, len, offset);
  278. #else
  279.   ret = -1;
  280. #endif
  281.   /* If we have no pread or it failed for this file, use lseek/read.  */
  282.   if (ret == -1)
  283.     {
  284.       ret = lseek (fd, offset, SEEK_SET);
  285.       if (ret != -1)
  286.         ret = read (fd, data, len);
  287.     }

  288.   if (ret == -1)
  289.     {
  290.       hostio_error (own_buf);
  291.       free (data);
  292.       return;
  293.     }

  294.   bytes_sent = hostio_reply_with_data (own_buf, data, ret, new_packet_len);

  295.   /* If we were using read, and the data did not all fit in the reply,
  296.      we would have to back up using lseek here.  With pread it does
  297.      not matter.  But we still have a problem; the return value in the
  298.      packet might be wrong, so we must fix it.  This time it will
  299.      definitely fit.  */
  300.   if (bytes_sent < ret)
  301.     bytes_sent = hostio_reply_with_data (own_buf, data, bytes_sent,
  302.                                          new_packet_len);

  303.   free (data);
  304. }

  305. static void
  306. handle_pwrite (char *own_buf, int packet_len)
  307. {
  308.   int fd, ret, len, offset;
  309.   char *p, *data;

  310.   p = own_buf + strlen ("vFile:pwrite:");

  311.   if (require_int (&p, &fd)
  312.       || require_comma (&p)
  313.       || require_valid_fd (fd)
  314.       || require_int (&p, &offset)
  315.       || require_comma (&p)
  316.       || require_data (p, packet_len - (p - own_buf), &data, &len))
  317.     {
  318.       hostio_packet_error (own_buf);
  319.       return;
  320.     }

  321. #ifdef HAVE_PWRITE
  322.   ret = pwrite (fd, data, len, offset);
  323. #else
  324.   ret = -1;
  325. #endif
  326.   /* If we have no pwrite or it failed for this file, use lseek/write.  */
  327.   if (ret == -1)
  328.     {
  329.       ret = lseek (fd, offset, SEEK_SET);
  330.       if (ret != -1)
  331.         ret = write (fd, data, len);
  332.     }

  333.   if (ret == -1)
  334.     {
  335.       hostio_error (own_buf);
  336.       free (data);
  337.       return;
  338.     }

  339.   hostio_reply (own_buf, ret);
  340.   free (data);
  341. }

  342. static void
  343. handle_close (char *own_buf)
  344. {
  345.   int fd, ret;
  346.   char *p;
  347.   struct fd_list **open_fd_p, *old_fd;

  348.   p = own_buf + strlen ("vFile:close:");

  349.   if (require_int (&p, &fd)
  350.       || require_valid_fd (fd)
  351.       || require_end (p))
  352.     {
  353.       hostio_packet_error (own_buf);
  354.       return;
  355.     }

  356.   ret = close (fd);

  357.   if (ret == -1)
  358.     {
  359.       hostio_error (own_buf);
  360.       return;
  361.     }

  362.   open_fd_p = &open_fds;
  363.   /* We know that fd is in the list, thanks to require_valid_fd.  */
  364.   while ((*open_fd_p)->fd != fd)
  365.     open_fd_p = &(*open_fd_p)->next;

  366.   old_fd = *open_fd_p;
  367.   *open_fd_p = (*open_fd_p)->next;
  368.   free (old_fd);

  369.   hostio_reply (own_buf, ret);
  370. }

  371. static void
  372. handle_unlink (char *own_buf)
  373. {
  374.   char filename[HOSTIO_PATH_MAX];
  375.   char *p;
  376.   int ret;

  377.   p = own_buf + strlen ("vFile:unlink:");

  378.   if (require_filename (&p, filename)
  379.       || require_end (p))
  380.     {
  381.       hostio_packet_error (own_buf);
  382.       return;
  383.     }

  384.   ret = unlink (filename);

  385.   if (ret == -1)
  386.     {
  387.       hostio_error (own_buf);
  388.       return;
  389.     }

  390.   hostio_reply (own_buf, ret);
  391. }

  392. static void
  393. handle_readlink (char *own_buf, int *new_packet_len)
  394. {
  395.   char filename[HOSTIO_PATH_MAX], linkname[HOSTIO_PATH_MAX];
  396.   char *p;
  397.   int ret, bytes_sent;

  398.   p = own_buf + strlen ("vFile:readlink:");

  399.   if (require_filename (&p, filename)
  400.       || require_end (p))
  401.     {
  402.       hostio_packet_error (own_buf);
  403.       return;
  404.     }

  405.   ret = readlink (filename, linkname, sizeof (linkname) - 1);
  406.   if (ret == -1)
  407.     {
  408.       hostio_error (own_buf);
  409.       return;
  410.     }

  411.   bytes_sent = hostio_reply_with_data (own_buf, linkname, ret, new_packet_len);

  412.   /* If the response does not fit into a single packet, do not attempt
  413.      to return a partial response, but simply fail.  */
  414.   if (bytes_sent < ret)
  415.     sprintf (own_buf, "F-1,%x", FILEIO_ENAMETOOLONG);
  416. }

  417. /* Handle all the 'F' file transfer packets.  */

  418. int
  419. handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
  420. {
  421.   if (strncmp (own_buf, "vFile:open:", 11) == 0)
  422.     handle_open (own_buf);
  423.   else if (strncmp (own_buf, "vFile:pread:", 11) == 0)
  424.     handle_pread (own_buf, new_packet_len);
  425.   else if (strncmp (own_buf, "vFile:pwrite:", 12) == 0)
  426.     handle_pwrite (own_buf, packet_len);
  427.   else if (strncmp (own_buf, "vFile:close:", 12) == 0)
  428.     handle_close (own_buf);
  429.   else if (strncmp (own_buf, "vFile:unlink:", 13) == 0)
  430.     handle_unlink (own_buf);
  431.   else if (strncmp (own_buf, "vFile:readlink:", 15) == 0)
  432.     handle_readlink (own_buf, new_packet_len);
  433.   else
  434.     return 0;

  435.   return 1;
  436. }