OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [gdb/] [gdbserver/] [hostio.c] - Diff between revs 157 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 157 Rev 816
/* Host file transfer support for gdbserver.
/* Host file transfer support for gdbserver.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
 
 
   Contributed by CodeSourcery.
   Contributed by CodeSourcery.
 
 
   This file is part of GDB.
   This file is part of GDB.
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.  */
   Boston, MA 02110-1301, USA.  */
 
 
#include "server.h"
#include "server.h"
#include "gdb/fileio.h"
#include "gdb/fileio.h"
 
 
#include <fcntl.h>
#include <fcntl.h>
#include <limits.h>
#include <limits.h>
#include <unistd.h>
#include <unistd.h>
 
 
extern int remote_debug;
extern int remote_debug;
 
 
struct fd_list
struct fd_list
{
{
  int fd;
  int fd;
  struct fd_list *next;
  struct fd_list *next;
};
};
 
 
static struct fd_list *open_fds;
static struct fd_list *open_fds;
 
 
static int
static int
safe_fromhex (char a, int *nibble)
safe_fromhex (char a, int *nibble)
{
{
  if (a >= '0' && a <= '9')
  if (a >= '0' && a <= '9')
    *nibble = a - '0';
    *nibble = a - '0';
  else if (a >= 'a' && a <= 'f')
  else if (a >= 'a' && a <= 'f')
    *nibble = a - 'a' + 10;
    *nibble = a - 'a' + 10;
  else if (a >= 'A' && a <= 'F')
  else if (a >= 'A' && a <= 'F')
    *nibble = a - 'A' + 10;
    *nibble = a - 'A' + 10;
  else
  else
    return -1;
    return -1;
 
 
  return 0;
  return 0;
}
}
 
 
static int
static int
require_filename (char **pp, char *filename)
require_filename (char **pp, char *filename)
{
{
  int count;
  int count;
  char *p;
  char *p;
 
 
  p = *pp;
  p = *pp;
  count = 0;
  count = 0;
 
 
  while (*p && *p != ',')
  while (*p && *p != ',')
    {
    {
      int nib1, nib2;
      int nib1, nib2;
 
 
      /* Don't allow overflow.  */
      /* Don't allow overflow.  */
      if (count >= PATH_MAX - 1)
      if (count >= PATH_MAX - 1)
        return -1;
        return -1;
 
 
      if (safe_fromhex (p[0], &nib1)
      if (safe_fromhex (p[0], &nib1)
          || safe_fromhex (p[1], &nib2))
          || safe_fromhex (p[1], &nib2))
        return -1;
        return -1;
 
 
      filename[count++] = nib1 * 16 + nib2;
      filename[count++] = nib1 * 16 + nib2;
      p += 2;
      p += 2;
    }
    }
 
 
  filename[count] = '\0';
  filename[count] = '\0';
  *pp = p;
  *pp = p;
  return 0;
  return 0;
}
}
 
 
static int
static int
require_int (char **pp, int *value)
require_int (char **pp, int *value)
{
{
  char *p;
  char *p;
  int count;
  int count;
 
 
  p = *pp;
  p = *pp;
  *value = 0;
  *value = 0;
  count = 0;
  count = 0;
 
 
  while (*p && *p != ',')
  while (*p && *p != ',')
    {
    {
      int nib;
      int nib;
 
 
      /* Don't allow overflow.  */
      /* Don't allow overflow.  */
      if (count >= 7)
      if (count >= 7)
        return -1;
        return -1;
 
 
      if (safe_fromhex (p[0], &nib))
      if (safe_fromhex (p[0], &nib))
        return -1;
        return -1;
      *value = *value * 16 + nib;
      *value = *value * 16 + nib;
      p++;
      p++;
      count++;
      count++;
    }
    }
 
 
  *pp = p;
  *pp = p;
  return 0;
  return 0;
}
}
 
 
static int
static int
require_data (char *p, int p_len, char **data, int *data_len)
require_data (char *p, int p_len, char **data, int *data_len)
{
{
  int input_index, output_index, escaped;
  int input_index, output_index, escaped;
 
 
  *data = malloc (p_len);
  *data = malloc (p_len);
 
 
  output_index = 0;
  output_index = 0;
  escaped = 0;
  escaped = 0;
  for (input_index = 0; input_index < p_len; input_index++)
  for (input_index = 0; input_index < p_len; input_index++)
    {
    {
      char b = p[input_index];
      char b = p[input_index];
 
 
      if (escaped)
      if (escaped)
        {
        {
          (*data)[output_index++] = b ^ 0x20;
          (*data)[output_index++] = b ^ 0x20;
          escaped = 0;
          escaped = 0;
        }
        }
      else if (b == '}')
      else if (b == '}')
        escaped = 1;
        escaped = 1;
      else
      else
        (*data)[output_index++] = b;
        (*data)[output_index++] = b;
    }
    }
 
 
  if (escaped)
  if (escaped)
    return -1;
    return -1;
 
 
  *data_len = output_index;
  *data_len = output_index;
  return 0;
  return 0;
}
}
 
 
static int
static int
require_comma (char **pp)
require_comma (char **pp)
{
{
  if (**pp == ',')
  if (**pp == ',')
    {
    {
      (*pp)++;
      (*pp)++;
      return 0;
      return 0;
    }
    }
  else
  else
    return -1;
    return -1;
}
}
 
 
static int
static int
require_end (char *p)
require_end (char *p)
{
{
  if (*p == '\0')
  if (*p == '\0')
    return 0;
    return 0;
  else
  else
    return -1;
    return -1;
}
}
 
 
static int
static int
require_valid_fd (int fd)
require_valid_fd (int fd)
{
{
  struct fd_list *fd_ptr;
  struct fd_list *fd_ptr;
 
 
  for (fd_ptr = open_fds; fd_ptr != NULL; fd_ptr = fd_ptr->next)
  for (fd_ptr = open_fds; fd_ptr != NULL; fd_ptr = fd_ptr->next)
    if (fd_ptr->fd == fd)
    if (fd_ptr->fd == fd)
      return 0;
      return 0;
 
 
  return -1;
  return -1;
}
}
 
 
/* Fill in own_buf with the last hostio error packet, however it
/* Fill in own_buf with the last hostio error packet, however it
   suitable for the target.  */
   suitable for the target.  */
static void
static void
hostio_error (char *own_buf)
hostio_error (char *own_buf)
{
{
  the_target->hostio_last_error (own_buf);
  the_target->hostio_last_error (own_buf);
}
}
 
 
static void
static void
hostio_packet_error (char *own_buf)
hostio_packet_error (char *own_buf)
{
{
  sprintf (own_buf, "F-1,%x", FILEIO_EINVAL);
  sprintf (own_buf, "F-1,%x", FILEIO_EINVAL);
}
}
 
 
static void
static void
hostio_reply (char *own_buf, int result)
hostio_reply (char *own_buf, int result)
{
{
  sprintf (own_buf, "F%x", result);
  sprintf (own_buf, "F%x", result);
}
}
 
 
static int
static int
hostio_reply_with_data (char *own_buf, char *buffer, int len,
hostio_reply_with_data (char *own_buf, char *buffer, int len,
                        int *new_packet_len)
                        int *new_packet_len)
{
{
  int input_index, output_index, out_maxlen;
  int input_index, output_index, out_maxlen;
 
 
  sprintf (own_buf, "F%x;", len);
  sprintf (own_buf, "F%x;", len);
  output_index = strlen (own_buf);
  output_index = strlen (own_buf);
 
 
  out_maxlen = PBUFSIZ;
  out_maxlen = PBUFSIZ;
 
 
  for (input_index = 0; input_index < len; input_index++)
  for (input_index = 0; input_index < len; input_index++)
    {
    {
      char b = buffer[input_index];
      char b = buffer[input_index];
 
 
      if (b == '$' || b == '#' || b == '}' || b == '*')
      if (b == '$' || b == '#' || b == '}' || b == '*')
        {
        {
          /* These must be escaped.  */
          /* These must be escaped.  */
          if (output_index + 2 > out_maxlen)
          if (output_index + 2 > out_maxlen)
            break;
            break;
          own_buf[output_index++] = '}';
          own_buf[output_index++] = '}';
          own_buf[output_index++] = b ^ 0x20;
          own_buf[output_index++] = b ^ 0x20;
        }
        }
      else
      else
        {
        {
          if (output_index + 1 > out_maxlen)
          if (output_index + 1 > out_maxlen)
            break;
            break;
          own_buf[output_index++] = b;
          own_buf[output_index++] = b;
        }
        }
    }
    }
 
 
  *new_packet_len = output_index;
  *new_packet_len = output_index;
  return input_index;
  return input_index;
}
}
 
 
static int
static int
fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
{
{
  int open_flags = 0;
  int open_flags = 0;
 
 
  if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
  if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
    return -1;
    return -1;
 
 
  if (fileio_open_flags & FILEIO_O_CREAT)
  if (fileio_open_flags & FILEIO_O_CREAT)
    open_flags |= O_CREAT;
    open_flags |= O_CREAT;
  if (fileio_open_flags & FILEIO_O_EXCL)
  if (fileio_open_flags & FILEIO_O_EXCL)
    open_flags |= O_EXCL;
    open_flags |= O_EXCL;
  if (fileio_open_flags & FILEIO_O_TRUNC)
  if (fileio_open_flags & FILEIO_O_TRUNC)
    open_flags |= O_TRUNC;
    open_flags |= O_TRUNC;
  if (fileio_open_flags & FILEIO_O_APPEND)
  if (fileio_open_flags & FILEIO_O_APPEND)
    open_flags |= O_APPEND;
    open_flags |= O_APPEND;
  if (fileio_open_flags & FILEIO_O_RDONLY)
  if (fileio_open_flags & FILEIO_O_RDONLY)
    open_flags |= O_RDONLY;
    open_flags |= O_RDONLY;
  if (fileio_open_flags & FILEIO_O_WRONLY)
  if (fileio_open_flags & FILEIO_O_WRONLY)
    open_flags |= O_WRONLY;
    open_flags |= O_WRONLY;
  if (fileio_open_flags & FILEIO_O_RDWR)
  if (fileio_open_flags & FILEIO_O_RDWR)
    open_flags |= O_RDWR;
    open_flags |= O_RDWR;
/* On systems supporting binary and text mode, always open files in
/* On systems supporting binary and text mode, always open files in
   binary mode. */
   binary mode. */
#ifdef O_BINARY
#ifdef O_BINARY
  open_flags |= O_BINARY;
  open_flags |= O_BINARY;
#endif
#endif
 
 
  *open_flags_p = open_flags;
  *open_flags_p = open_flags;
  return 0;
  return 0;
}
}
 
 
static void
static void
handle_open (char *own_buf)
handle_open (char *own_buf)
{
{
  char filename[PATH_MAX];
  char filename[PATH_MAX];
  char *p;
  char *p;
  int fileio_flags, mode, flags, fd;
  int fileio_flags, mode, flags, fd;
  struct fd_list *new_fd;
  struct fd_list *new_fd;
 
 
  p = own_buf + strlen ("vFile:open:");
  p = own_buf + strlen ("vFile:open:");
 
 
  if (require_filename (&p, filename)
  if (require_filename (&p, filename)
      || require_comma (&p)
      || require_comma (&p)
      || require_int (&p, &fileio_flags)
      || require_int (&p, &fileio_flags)
      || require_comma (&p)
      || require_comma (&p)
      || require_int (&p, &mode)
      || require_int (&p, &mode)
      || require_end (p)
      || require_end (p)
      || fileio_open_flags_to_host (fileio_flags, &flags))
      || fileio_open_flags_to_host (fileio_flags, &flags))
    {
    {
      hostio_packet_error (own_buf);
      hostio_packet_error (own_buf);
      return;
      return;
    }
    }
 
 
  /* We do not need to convert MODE, since the fileio protocol
  /* We do not need to convert MODE, since the fileio protocol
     uses the standard values.  */
     uses the standard values.  */
  fd = open (filename, flags, mode);
  fd = open (filename, flags, mode);
 
 
  if (fd == -1)
  if (fd == -1)
    {
    {
      hostio_error (own_buf);
      hostio_error (own_buf);
      return;
      return;
    }
    }
 
 
  /* Record the new file descriptor.  */
  /* Record the new file descriptor.  */
  new_fd = malloc (sizeof (struct fd_list));
  new_fd = malloc (sizeof (struct fd_list));
  new_fd->fd = fd;
  new_fd->fd = fd;
  new_fd->next = open_fds;
  new_fd->next = open_fds;
  open_fds = new_fd;
  open_fds = new_fd;
 
 
  hostio_reply (own_buf, fd);
  hostio_reply (own_buf, fd);
}
}
 
 
static void
static void
handle_pread (char *own_buf, int *new_packet_len)
handle_pread (char *own_buf, int *new_packet_len)
{
{
  int fd, ret, len, offset, bytes_sent;
  int fd, ret, len, offset, bytes_sent;
  char *p, *data;
  char *p, *data;
 
 
  p = own_buf + strlen ("vFile:pread:");
  p = own_buf + strlen ("vFile:pread:");
 
 
  if (require_int (&p, &fd)
  if (require_int (&p, &fd)
      || require_comma (&p)
      || require_comma (&p)
      || require_valid_fd (fd)
      || require_valid_fd (fd)
      || require_int (&p, &len)
      || require_int (&p, &len)
      || require_comma (&p)
      || require_comma (&p)
      || require_int (&p, &offset)
      || require_int (&p, &offset)
      || require_end (p))
      || require_end (p))
    {
    {
      hostio_packet_error (own_buf);
      hostio_packet_error (own_buf);
      return;
      return;
    }
    }
 
 
  data = malloc (len);
  data = malloc (len);
#ifdef HAVE_PREAD
#ifdef HAVE_PREAD
  ret = pread (fd, data, len, offset);
  ret = pread (fd, data, len, offset);
#else
#else
  ret = lseek (fd, offset, SEEK_SET);
  ret = lseek (fd, offset, SEEK_SET);
  if (ret != -1)
  if (ret != -1)
    ret = read (fd, data, len);
    ret = read (fd, data, len);
#endif
#endif
 
 
  if (ret == -1)
  if (ret == -1)
    {
    {
      hostio_error (own_buf);
      hostio_error (own_buf);
      free (data);
      free (data);
      return;
      return;
    }
    }
 
 
  bytes_sent = hostio_reply_with_data (own_buf, data, ret, new_packet_len);
  bytes_sent = hostio_reply_with_data (own_buf, data, ret, new_packet_len);
 
 
  /* If we were using read, and the data did not all fit in the reply,
  /* If we were using read, and the data did not all fit in the reply,
     we would have to back up using lseek here.  With pread it does
     we would have to back up using lseek here.  With pread it does
     not matter.  But we still have a problem; the return value in the
     not matter.  But we still have a problem; the return value in the
     packet might be wrong, so we must fix it.  This time it will
     packet might be wrong, so we must fix it.  This time it will
     definitely fit.  */
     definitely fit.  */
  if (bytes_sent < ret)
  if (bytes_sent < ret)
    bytes_sent = hostio_reply_with_data (own_buf, data, bytes_sent,
    bytes_sent = hostio_reply_with_data (own_buf, data, bytes_sent,
                                         new_packet_len);
                                         new_packet_len);
 
 
  free (data);
  free (data);
}
}
 
 
static void
static void
handle_pwrite (char *own_buf, int packet_len)
handle_pwrite (char *own_buf, int packet_len)
{
{
  int fd, ret, len, offset;
  int fd, ret, len, offset;
  char *p, *data;
  char *p, *data;
 
 
  p = own_buf + strlen ("vFile:pwrite:");
  p = own_buf + strlen ("vFile:pwrite:");
 
 
  if (require_int (&p, &fd)
  if (require_int (&p, &fd)
      || require_comma (&p)
      || require_comma (&p)
      || require_valid_fd (fd)
      || require_valid_fd (fd)
      || require_int (&p, &offset)
      || require_int (&p, &offset)
      || require_comma (&p)
      || require_comma (&p)
      || require_data (p, packet_len - (p - own_buf), &data, &len))
      || require_data (p, packet_len - (p - own_buf), &data, &len))
    {
    {
      hostio_packet_error (own_buf);
      hostio_packet_error (own_buf);
      return;
      return;
    }
    }
 
 
#ifdef HAVE_PWRITE
#ifdef HAVE_PWRITE
  ret = pwrite (fd, data, len, offset);
  ret = pwrite (fd, data, len, offset);
#else
#else
  ret = lseek (fd, offset, SEEK_SET);
  ret = lseek (fd, offset, SEEK_SET);
  if (ret != -1)
  if (ret != -1)
    ret = write (fd, data, len);
    ret = write (fd, data, len);
#endif
#endif
 
 
  if (ret == -1)
  if (ret == -1)
    {
    {
      hostio_error (own_buf);
      hostio_error (own_buf);
      free (data);
      free (data);
      return;
      return;
    }
    }
 
 
  hostio_reply (own_buf, ret);
  hostio_reply (own_buf, ret);
  free (data);
  free (data);
}
}
 
 
static void
static void
handle_close (char *own_buf)
handle_close (char *own_buf)
{
{
  int fd, ret;
  int fd, ret;
  char *p;
  char *p;
  struct fd_list **open_fd_p, *old_fd;
  struct fd_list **open_fd_p, *old_fd;
 
 
  p = own_buf + strlen ("vFile:close:");
  p = own_buf + strlen ("vFile:close:");
 
 
  if (require_int (&p, &fd)
  if (require_int (&p, &fd)
      || require_valid_fd (fd)
      || require_valid_fd (fd)
      || require_end (p))
      || require_end (p))
    {
    {
      hostio_packet_error (own_buf);
      hostio_packet_error (own_buf);
      return;
      return;
    }
    }
 
 
  ret = close (fd);
  ret = close (fd);
 
 
  if (ret == -1)
  if (ret == -1)
    {
    {
      hostio_error (own_buf);
      hostio_error (own_buf);
      return;
      return;
    }
    }
 
 
  open_fd_p = &open_fds;
  open_fd_p = &open_fds;
  while (*open_fd_p && (*open_fd_p)->fd != fd)
  while (*open_fd_p && (*open_fd_p)->fd != fd)
    open_fd_p = &(*open_fd_p)->next;
    open_fd_p = &(*open_fd_p)->next;
 
 
  old_fd = *open_fd_p;
  old_fd = *open_fd_p;
  *open_fd_p = (*open_fd_p)->next;
  *open_fd_p = (*open_fd_p)->next;
  free (old_fd);
  free (old_fd);
 
 
  hostio_reply (own_buf, ret);
  hostio_reply (own_buf, ret);
}
}
 
 
static void
static void
handle_unlink (char *own_buf)
handle_unlink (char *own_buf)
{
{
  char filename[PATH_MAX];
  char filename[PATH_MAX];
  char *p;
  char *p;
  int ret;
  int ret;
 
 
  p = own_buf + strlen ("vFile:unlink:");
  p = own_buf + strlen ("vFile:unlink:");
 
 
  if (require_filename (&p, filename)
  if (require_filename (&p, filename)
      || require_end (p))
      || require_end (p))
    {
    {
      hostio_packet_error (own_buf);
      hostio_packet_error (own_buf);
      return;
      return;
    }
    }
 
 
  ret = unlink (filename);
  ret = unlink (filename);
 
 
  if (ret == -1)
  if (ret == -1)
    {
    {
      hostio_error (own_buf);
      hostio_error (own_buf);
      return;
      return;
    }
    }
 
 
  hostio_reply (own_buf, ret);
  hostio_reply (own_buf, ret);
}
}
 
 
/* Handle all the 'F' file transfer packets.  */
/* Handle all the 'F' file transfer packets.  */
 
 
int
int
handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
{
{
  if (strncmp (own_buf, "vFile:open:", 11) == 0)
  if (strncmp (own_buf, "vFile:open:", 11) == 0)
    handle_open (own_buf);
    handle_open (own_buf);
  else if (strncmp (own_buf, "vFile:pread:", 11) == 0)
  else if (strncmp (own_buf, "vFile:pread:", 11) == 0)
    handle_pread (own_buf, new_packet_len);
    handle_pread (own_buf, new_packet_len);
  else if (strncmp (own_buf, "vFile:pwrite:", 12) == 0)
  else if (strncmp (own_buf, "vFile:pwrite:", 12) == 0)
    handle_pwrite (own_buf, packet_len);
    handle_pwrite (own_buf, packet_len);
  else if (strncmp (own_buf, "vFile:close:", 12) == 0)
  else if (strncmp (own_buf, "vFile:close:", 12) == 0)
    handle_close (own_buf);
    handle_close (own_buf);
  else if (strncmp (own_buf, "vFile:unlink:", 13) == 0)
  else if (strncmp (own_buf, "vFile:unlink:", 13) == 0)
    handle_unlink (own_buf);
    handle_unlink (own_buf);
  else
  else
    return 0;
    return 0;
 
 
  return 1;
  return 1;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.