1 /* Remote File-I/O communications
3 Copyright (C) 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* See the GDB User Guide for details of the GDB remote protocol. */
24 #include "gdb_string.h"
27 #include "gdb/fileio.h"
30 #include "exceptions.h"
31 #include "remote-fileio.h"
32 #include "event-loop.h"
38 #include <sys/cygwin.h> /* For cygwin_conv_to_full_posix_path. */
39 #include <cygwin/version.h>
40 #if CYGWIN_VERSION_DLL_MAKE_COMBINED(CYGWIN_VERSION_API_MAJOR,CYGWIN_VERSION_API_MINOR) < 181
41 # define CCP_POSIX_TO_WIN_A 0
42 # define CCP_WIN_A_TO_POSIX 2
43 # define cygwin_conv_path(op, from, to, size) \
44 (op == CCP_WIN_A_TO_POSIX) ? \
45 cygwin_conv_to_full_posix_path (from, to) : \
46 cygwin_conv_to_win32_path (from, to)
56 #define FIO_FD_INVALID -1
57 #define FIO_FD_CONSOLE_IN -2
58 #define FIO_FD_CONSOLE_OUT -3
60 static int remote_fio_system_call_allowed = 0;
62 static struct async_signal_handler *sigint_fileio_token;
65 remote_fileio_init_fd_map (void)
69 if (!remote_fio_data.fd_map)
71 remote_fio_data.fd_map = (int *) xmalloc (10 * sizeof (int));
72 remote_fio_data.fd_map_size = 10;
73 remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN;
74 remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT;
75 remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT;
76 for (i = 3; i < 10; ++i)
77 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
83 remote_fileio_resize_fd_map (void)
85 int i = remote_fio_data.fd_map_size;
87 if (!remote_fio_data.fd_map)
88 return remote_fileio_init_fd_map ();
89 remote_fio_data.fd_map_size += 10;
90 remote_fio_data.fd_map =
91 (int *) xrealloc (remote_fio_data.fd_map,
92 remote_fio_data.fd_map_size * sizeof (int));
93 for (; i < remote_fio_data.fd_map_size; i++)
94 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
95 return remote_fio_data.fd_map_size - 10;
99 remote_fileio_next_free_fd (void)
103 for (i = 0; i < remote_fio_data.fd_map_size; ++i)
104 if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
106 return remote_fileio_resize_fd_map ();
110 remote_fileio_fd_to_targetfd (int fd)
112 int target_fd = remote_fileio_next_free_fd ();
114 remote_fio_data.fd_map[target_fd] = fd;
119 remote_fileio_map_fd (int target_fd)
121 remote_fileio_init_fd_map ();
122 if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
123 return FIO_FD_INVALID;
124 return remote_fio_data.fd_map[target_fd];
128 remote_fileio_close_target_fd (int target_fd)
130 remote_fileio_init_fd_map ();
131 if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
132 remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
136 remote_fileio_oflags_to_host (long flags)
140 if (flags & FILEIO_O_CREAT)
142 if (flags & FILEIO_O_EXCL)
144 if (flags & FILEIO_O_TRUNC)
146 if (flags & FILEIO_O_APPEND)
148 if (flags & FILEIO_O_RDONLY)
150 if (flags & FILEIO_O_WRONLY)
152 if (flags & FILEIO_O_RDWR)
154 /* On systems supporting binary and text mode, always open files in
163 remote_fileio_mode_to_host (long mode, int open_call)
169 if (mode & FILEIO_S_IFREG)
171 if (mode & FILEIO_S_IFDIR)
173 if (mode & FILEIO_S_IFCHR)
176 if (mode & FILEIO_S_IRUSR)
178 if (mode & FILEIO_S_IWUSR)
180 if (mode & FILEIO_S_IXUSR)
183 if (mode & FILEIO_S_IRGRP)
187 if (mode & FILEIO_S_IWGRP)
191 if (mode & FILEIO_S_IXGRP)
194 if (mode & FILEIO_S_IROTH)
197 if (mode & FILEIO_S_IWOTH)
201 if (mode & FILEIO_S_IXOTH)
208 remote_fileio_mode_to_target (mode_t mode)
213 tmode |= FILEIO_S_IFREG;
215 tmode |= FILEIO_S_IFDIR;
217 tmode |= FILEIO_S_IFCHR;
219 tmode |= FILEIO_S_IRUSR;
221 tmode |= FILEIO_S_IWUSR;
223 tmode |= FILEIO_S_IXUSR;
226 tmode |= FILEIO_S_IRGRP;
230 tmode |= FILEIO_S_IWGRP;
234 tmode |= FILEIO_S_IXGRP;
237 tmode |= FILEIO_S_IROTH;
240 tmode |= FILEIO_S_IWOTH;
244 tmode |= FILEIO_S_IXOTH;
250 remote_fileio_errno_to_target (int error)
257 return FILEIO_ENOENT;
265 return FILEIO_EACCES;
267 return FILEIO_EFAULT;
271 return FILEIO_EEXIST;
273 return FILEIO_ENODEV;
275 return FILEIO_ENOTDIR;
277 return FILEIO_EISDIR;
279 return FILEIO_EINVAL;
281 return FILEIO_ENFILE;
283 return FILEIO_EMFILE;
287 return FILEIO_ENOSPC;
289 return FILEIO_ESPIPE;
293 return FILEIO_ENOSYS;
295 return FILEIO_ENAMETOOLONG;
297 return FILEIO_EUNKNOWN;
301 remote_fileio_seek_flag_to_host (long num, int *flag)
307 case FILEIO_SEEK_SET:
310 case FILEIO_SEEK_CUR:
313 case FILEIO_SEEK_END:
323 remote_fileio_extract_long (char **buf, LONGEST *retlong)
328 if (!buf || !*buf || !**buf || !retlong)
330 c = strchr (*buf, ',');
334 c = strchr (*buf, '\0');
335 while (strchr ("+-", **buf))
341 for (*retlong = 0; **buf; ++*buf)
344 if (**buf >= '0' && **buf <= '9')
345 *retlong += **buf - '0';
346 else if (**buf >= 'a' && **buf <= 'f')
347 *retlong += **buf - 'a' + 10;
348 else if (**buf >= 'A' && **buf <= 'F')
349 *retlong += **buf - 'A' + 10;
359 remote_fileio_extract_int (char **buf, long *retint)
366 ret = remote_fileio_extract_long (buf, &retlong);
368 *retint = (long) retlong;
373 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
378 if (!buf || !*buf || !**buf || !ptrval || !length)
380 c = strchr (*buf, '/');
384 if (remote_fileio_extract_long (buf, &retlong))
386 *ptrval = (CORE_ADDR) retlong;
388 if (remote_fileio_extract_long (buf, &retlong))
390 *length = (int) retlong;
394 /* Convert to big endian. */
396 remote_fileio_to_be (LONGEST num, char *buf, int bytes)
400 for (i = 0; i < bytes; ++i)
401 buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
405 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
407 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
411 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
413 remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
417 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
419 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
423 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
425 remote_fileio_to_be (num, (char *) fnum, 8);
429 remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
431 remote_fileio_to_be (num, (char *) fnum, 8);
435 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
439 /* `st_dev' is set in the calling function. */
440 remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
441 remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
442 remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
443 remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
444 remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
445 remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
446 remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
447 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
448 blksize = st->st_blksize;
452 remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
453 #if HAVE_STRUCT_STAT_ST_BLOCKS
454 remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
456 /* FIXME: This is correct for DJGPP, but other systems that don't
457 have st_blocks, if any, might prefer 512 instead of st_blksize.
458 (eliz, 30-12-2003) */
459 remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
463 remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
464 remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
465 remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
469 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
471 remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
472 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
475 static int remote_fio_ctrl_c_flag = 0;
476 static int remote_fio_no_longjmp = 0;
478 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
479 static struct sigaction remote_fio_sa;
480 static struct sigaction remote_fio_osa;
482 static void (*remote_fio_ofunc)(int);
486 remote_fileio_sig_init (void)
488 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
489 remote_fio_sa.sa_handler = SIG_IGN;
490 sigemptyset (&remote_fio_sa.sa_mask);
491 remote_fio_sa.sa_flags = 0;
492 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
494 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
499 remote_fileio_sig_set (void (*sigint_func)(int))
501 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
502 remote_fio_sa.sa_handler = sigint_func;
503 sigemptyset (&remote_fio_sa.sa_mask);
504 remote_fio_sa.sa_flags = 0;
505 sigaction (SIGINT, &remote_fio_sa, NULL);
507 signal (SIGINT, sigint_func);
512 remote_fileio_sig_exit (void)
514 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
515 sigaction (SIGINT, &remote_fio_osa, NULL);
517 signal (SIGINT, remote_fio_ofunc);
522 async_remote_fileio_interrupt (gdb_client_data arg)
524 deprecated_throw_reason (RETURN_QUIT);
528 remote_fileio_ctrl_c_signal_handler (int signo)
530 remote_fileio_sig_set (SIG_IGN);
531 remote_fio_ctrl_c_flag = 1;
532 if (!remote_fio_no_longjmp)
533 gdb_call_async_signal_handler (sigint_fileio_token, 1);
534 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
538 remote_fileio_reply (int retcode, int error)
542 remote_fileio_sig_set (SIG_IGN);
549 sprintf (buf + strlen (buf), "%x", retcode);
550 if (error || remote_fio_ctrl_c_flag)
552 if (error && remote_fio_ctrl_c_flag)
553 error = FILEIO_EINTR;
559 sprintf (buf + strlen (buf), ",%x", error);
560 if (remote_fio_ctrl_c_flag)
563 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
568 remote_fileio_ioerror (void)
570 remote_fileio_reply (-1, FILEIO_EIO);
574 remote_fileio_badfd (void)
576 remote_fileio_reply (-1, FILEIO_EBADF);
580 remote_fileio_return_errno (int retcode)
582 remote_fileio_reply (retcode, retcode < 0
583 ? remote_fileio_errno_to_target (errno) : 0);
587 remote_fileio_return_success (int retcode)
589 remote_fileio_reply (retcode, 0);
593 remote_fileio_func_open (char *buf)
603 /* 1. Parameter: Ptr to pathname / length incl. trailing zero. */
604 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
606 remote_fileio_ioerror ();
609 /* 2. Parameter: open flags */
610 if (remote_fileio_extract_int (&buf, &num))
612 remote_fileio_ioerror ();
615 flags = remote_fileio_oflags_to_host (num);
616 /* 3. Parameter: open mode */
617 if (remote_fileio_extract_int (&buf, &num))
619 remote_fileio_ioerror ();
622 mode = remote_fileio_mode_to_host (num, 1);
624 /* Request pathname. */
625 pathname = alloca (length);
626 if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
628 remote_fileio_ioerror ();
632 /* Check if pathname exists and is not a regular file or directory. If so,
633 return an appropriate error code. Same for trying to open directories
635 if (!stat (pathname, &st))
637 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
639 remote_fileio_reply (-1, FILEIO_ENODEV);
642 if (S_ISDIR (st.st_mode)
643 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
645 remote_fileio_reply (-1, FILEIO_EISDIR);
650 remote_fio_no_longjmp = 1;
651 fd = open (pathname, flags, mode);
654 remote_fileio_return_errno (-1);
658 fd = remote_fileio_fd_to_targetfd (fd);
659 remote_fileio_return_success (fd);
663 remote_fileio_func_close (char *buf)
668 /* Parameter: file descriptor */
669 if (remote_fileio_extract_int (&buf, &num))
671 remote_fileio_ioerror ();
674 fd = remote_fileio_map_fd ((int) num);
675 if (fd == FIO_FD_INVALID)
677 remote_fileio_badfd ();
681 remote_fio_no_longjmp = 1;
682 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
683 remote_fileio_return_errno (-1);
684 remote_fileio_close_target_fd ((int) num);
685 remote_fileio_return_success (0);
689 remote_fileio_func_read (char *buf)
694 int fd, ret, retlength;
697 off_t old_offset, new_offset;
699 /* 1. Parameter: file descriptor */
700 if (remote_fileio_extract_int (&buf, &target_fd))
702 remote_fileio_ioerror ();
705 fd = remote_fileio_map_fd ((int) target_fd);
706 if (fd == FIO_FD_INVALID)
708 remote_fileio_badfd ();
711 /* 2. Parameter: buffer pointer */
712 if (remote_fileio_extract_long (&buf, &lnum))
714 remote_fileio_ioerror ();
717 ptrval = (CORE_ADDR) lnum;
718 /* 3. Parameter: buffer length */
719 if (remote_fileio_extract_int (&buf, &num))
721 remote_fileio_ioerror ();
724 length = (size_t) num;
728 case FIO_FD_CONSOLE_OUT:
729 remote_fileio_badfd ();
731 case FIO_FD_CONSOLE_IN:
733 static char *remaining_buf = NULL;
734 static int remaining_length = 0;
736 buffer = (gdb_byte *) xmalloc (16384);
739 remote_fio_no_longjmp = 1;
740 if (remaining_length > length)
742 memcpy (buffer, remaining_buf, length);
743 memmove (remaining_buf, remaining_buf + length,
744 remaining_length - length);
745 remaining_length -= length;
750 memcpy (buffer, remaining_buf, remaining_length);
751 xfree (remaining_buf);
752 remaining_buf = NULL;
753 ret = remaining_length;
758 /* Windows (at least XP and Server 2003) has difficulty
759 with large reads from consoles. If a handle is
760 backed by a real console device, overly large reads
761 from the handle will fail and set errno == ENOMEM.
762 On a Windows Server 2003 system where I tested,
763 reading 26608 bytes from the console was OK, but
764 anything above 26609 bytes would fail. The limit has
765 been observed to vary on different systems. So, we
766 limit this read to something smaller than that - by a
767 safe margin, in case the limit depends on system
768 resources or version. */
769 ret = ui_file_read (gdb_stdtargin, (char *) buffer, 16383);
770 remote_fio_no_longjmp = 1;
771 if (ret > 0 && (size_t)ret > length)
773 remaining_buf = (char *) xmalloc (ret - length);
774 remaining_length = ret - length;
775 memcpy (remaining_buf, buffer + length, remaining_length);
782 buffer = (gdb_byte *) xmalloc (length);
783 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
784 for read() to return -1 even if "some" bytes have been read. It
785 has been corrected in SUSv2 but that doesn't help us much...
786 Therefore a complete solution must check how many bytes have been
787 read on EINTR to return a more reliable value to the target */
788 old_offset = lseek (fd, 0, SEEK_CUR);
789 remote_fio_no_longjmp = 1;
790 ret = read (fd, buffer, length);
791 if (ret < 0 && errno == EINTR)
793 new_offset = lseek (fd, 0, SEEK_CUR);
794 /* If some data has been read, return the number of bytes read.
795 The Ctrl-C flag is set in remote_fileio_reply() anyway. */
796 if (old_offset != new_offset)
797 ret = new_offset - old_offset;
804 errno = target_write_memory (ptrval, buffer, ret);
810 remote_fileio_return_errno (-1);
812 remote_fileio_return_success (ret);
818 remote_fileio_func_write (char *buf)
827 /* 1. Parameter: file descriptor */
828 if (remote_fileio_extract_int (&buf, &target_fd))
830 remote_fileio_ioerror ();
833 fd = remote_fileio_map_fd ((int) target_fd);
834 if (fd == FIO_FD_INVALID)
836 remote_fileio_badfd ();
839 /* 2. Parameter: buffer pointer */
840 if (remote_fileio_extract_long (&buf, &lnum))
842 remote_fileio_ioerror ();
845 ptrval = (CORE_ADDR) lnum;
846 /* 3. Parameter: buffer length */
847 if (remote_fileio_extract_int (&buf, &num))
849 remote_fileio_ioerror ();
852 length = (size_t) num;
854 buffer = (gdb_byte *) xmalloc (length);
855 if (target_read_memory (ptrval, buffer, length) != 0)
858 remote_fileio_ioerror ();
862 remote_fio_no_longjmp = 1;
865 case FIO_FD_CONSOLE_IN:
866 remote_fileio_badfd ();
869 case FIO_FD_CONSOLE_OUT:
870 ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr,
871 (char *) buffer, length);
872 gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
876 ret = write (fd, buffer, length);
877 if (ret < 0 && errno == EACCES)
878 errno = EBADF; /* Cygwin returns EACCESS when writing to a
884 remote_fileio_return_errno (-1);
886 remote_fileio_return_success (ret);
892 remote_fileio_func_lseek (char *buf)
899 /* 1. Parameter: file descriptor */
900 if (remote_fileio_extract_int (&buf, &num))
902 remote_fileio_ioerror ();
905 fd = remote_fileio_map_fd ((int) num);
906 if (fd == FIO_FD_INVALID)
908 remote_fileio_badfd ();
911 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
913 remote_fileio_reply (-1, FILEIO_ESPIPE);
917 /* 2. Parameter: offset */
918 if (remote_fileio_extract_long (&buf, &lnum))
920 remote_fileio_ioerror ();
923 offset = (off_t) lnum;
924 /* 3. Parameter: flag */
925 if (remote_fileio_extract_int (&buf, &num))
927 remote_fileio_ioerror ();
930 if (remote_fileio_seek_flag_to_host (num, &flag))
932 remote_fileio_reply (-1, FILEIO_EINVAL);
936 remote_fio_no_longjmp = 1;
937 ret = lseek (fd, offset, flag);
939 if (ret == (off_t) -1)
940 remote_fileio_return_errno (-1);
942 remote_fileio_return_success (ret);
946 remote_fileio_func_rename (char *buf)
948 CORE_ADDR old_ptr, new_ptr;
949 int old_len, new_len;
950 char *oldpath, *newpath;
952 struct stat ost, nst;
954 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
955 if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
957 remote_fileio_ioerror ();
961 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
962 if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
964 remote_fileio_ioerror ();
968 /* Request oldpath using 'm' packet */
969 oldpath = alloca (old_len);
970 if (target_read_memory (old_ptr, (gdb_byte *) oldpath, old_len) != 0)
972 remote_fileio_ioerror ();
976 /* Request newpath using 'm' packet */
977 newpath = alloca (new_len);
978 if (target_read_memory (new_ptr, (gdb_byte *) newpath, new_len) != 0)
980 remote_fileio_ioerror ();
984 /* Only operate on regular files and directories. */
985 of = stat (oldpath, &ost);
986 nf = stat (newpath, &nst);
987 if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
988 || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
990 remote_fileio_reply (-1, FILEIO_EACCES);
994 remote_fio_no_longjmp = 1;
995 ret = rename (oldpath, newpath);
999 /* Special case: newpath is a non-empty directory. Some systems
1000 return ENOTEMPTY, some return EEXIST. We coerce that to be
1002 if (errno == ENOTEMPTY)
1005 /* Workaround some Cygwin problems with correct errnos. */
1006 if (errno == EACCES)
1008 if (!of && !nf && S_ISDIR (nst.st_mode))
1010 if (S_ISREG (ost.st_mode))
1014 char oldfullpath[PATH_MAX];
1015 char newfullpath[PATH_MAX];
1018 cygwin_conv_path (CCP_WIN_A_TO_POSIX, oldpath, oldfullpath,
1020 cygwin_conv_path (CCP_WIN_A_TO_POSIX, newpath, newfullpath,
1022 len = strlen (oldfullpath);
1023 if (newfullpath[len] == '/'
1024 && !strncmp (oldfullpath, newfullpath, len))
1033 remote_fileio_return_errno (-1);
1036 remote_fileio_return_success (ret);
1040 remote_fileio_func_unlink (char *buf)
1048 /* Parameter: Ptr to pathname / length incl. trailing zero */
1049 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1051 remote_fileio_ioerror ();
1054 /* Request pathname using 'm' packet */
1055 pathname = alloca (length);
1056 if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
1058 remote_fileio_ioerror ();
1062 /* Only operate on regular files (and directories, which allows to return
1063 the correct return code). */
1064 if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1066 remote_fileio_reply (-1, FILEIO_ENODEV);
1070 remote_fio_no_longjmp = 1;
1071 ret = unlink (pathname);
1074 remote_fileio_return_errno (-1);
1076 remote_fileio_return_success (ret);
1080 remote_fileio_func_stat (char *buf)
1082 CORE_ADDR statptr, nameptr;
1083 int ret, namelength;
1087 struct fio_stat fst;
1089 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1090 if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
1092 remote_fileio_ioerror ();
1096 /* 2. Parameter: Ptr to struct stat */
1097 if (remote_fileio_extract_long (&buf, &lnum))
1099 remote_fileio_ioerror ();
1102 statptr = (CORE_ADDR) lnum;
1104 /* Request pathname using 'm' packet */
1105 pathname = alloca (namelength);
1106 if (target_read_memory (nameptr, (gdb_byte *) pathname, namelength) != 0)
1108 remote_fileio_ioerror ();
1112 remote_fio_no_longjmp = 1;
1113 ret = stat (pathname, &st);
1117 remote_fileio_return_errno (-1);
1120 /* Only operate on regular files and directories. */
1121 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1123 remote_fileio_reply (-1, FILEIO_EACCES);
1128 remote_fileio_to_fio_stat (&st, &fst);
1129 remote_fileio_to_fio_uint (0, fst.fst_dev);
1131 errno = target_write_memory (statptr, (gdb_byte *) &fst, sizeof fst);
1134 remote_fileio_return_errno (-1);
1138 remote_fileio_return_success (ret);
1142 remote_fileio_func_fstat (char *buf)
1145 int fd, ret, retlength;
1149 struct fio_stat fst;
1152 /* 1. Parameter: file descriptor */
1153 if (remote_fileio_extract_int (&buf, &target_fd))
1155 remote_fileio_ioerror ();
1158 fd = remote_fileio_map_fd ((int) target_fd);
1159 if (fd == FIO_FD_INVALID)
1161 remote_fileio_badfd ();
1164 /* 2. Parameter: Ptr to struct stat */
1165 if (remote_fileio_extract_long (&buf, &lnum))
1167 remote_fileio_ioerror ();
1170 ptrval = (CORE_ADDR) lnum;
1172 remote_fio_no_longjmp = 1;
1173 if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1175 remote_fileio_to_fio_uint (1, fst.fst_dev);
1176 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1179 st.st_uid = getuid ();
1184 st.st_gid = getgid ();
1190 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1191 st.st_blksize = 512;
1193 #if HAVE_STRUCT_STAT_ST_BLOCKS
1196 if (!gettimeofday (&tv, NULL))
1197 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1199 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1203 ret = fstat (fd, &st);
1207 remote_fileio_return_errno (-1);
1212 remote_fileio_to_fio_stat (&st, &fst);
1214 errno = target_write_memory (ptrval, (gdb_byte *) &fst, sizeof fst);
1217 remote_fileio_return_errno (-1);
1221 remote_fileio_return_success (ret);
1225 remote_fileio_func_gettimeofday (char *buf)
1231 struct fio_timeval ftv;
1233 /* 1. Parameter: struct timeval pointer */
1234 if (remote_fileio_extract_long (&buf, &lnum))
1236 remote_fileio_ioerror ();
1239 ptrval = (CORE_ADDR) lnum;
1240 /* 2. Parameter: some pointer value... */
1241 if (remote_fileio_extract_long (&buf, &lnum))
1243 remote_fileio_ioerror ();
1246 /* ...which has to be NULL. */
1249 remote_fileio_reply (-1, FILEIO_EINVAL);
1253 remote_fio_no_longjmp = 1;
1254 ret = gettimeofday (&tv, NULL);
1258 remote_fileio_return_errno (-1);
1264 remote_fileio_to_fio_timeval (&tv, &ftv);
1266 errno = target_write_memory (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1269 remote_fileio_return_errno (-1);
1273 remote_fileio_return_success (ret);
1277 remote_fileio_func_isatty (char *buf)
1282 /* Parameter: file descriptor */
1283 if (remote_fileio_extract_int (&buf, &target_fd))
1285 remote_fileio_ioerror ();
1288 remote_fio_no_longjmp = 1;
1289 fd = remote_fileio_map_fd ((int) target_fd);
1290 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1291 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1295 remote_fileio_func_system (char *buf)
1298 int ret, length, retlength;
1299 char *cmdline = NULL;
1301 /* Parameter: Ptr to commandline / length incl. trailing zero */
1302 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1304 remote_fileio_ioerror ();
1310 /* Request commandline using 'm' packet */
1311 cmdline = alloca (length);
1312 if (target_read_memory (ptrval, (gdb_byte *) cmdline, length) != 0)
1314 remote_fileio_ioerror ();
1319 /* Check if system(3) has been explicitely allowed using the
1320 `set remote system-call-allowed 1' command. If length is 0,
1321 indicating a NULL parameter to the system call, return zero to
1322 indicate a shell is not available. Otherwise fail with EPERM. */
1323 if (!remote_fio_system_call_allowed)
1326 remote_fileio_return_success (0);
1328 remote_fileio_reply (-1, FILEIO_EPERM);
1332 remote_fio_no_longjmp = 1;
1333 ret = system (cmdline);
1336 remote_fileio_return_success (ret);
1338 remote_fileio_return_errno (-1);
1340 remote_fileio_return_success (WEXITSTATUS (ret));
1345 void (*func)(char *);
1346 } remote_fio_func_map[] = {
1347 { "open", remote_fileio_func_open },
1348 { "close", remote_fileio_func_close },
1349 { "read", remote_fileio_func_read },
1350 { "write", remote_fileio_func_write },
1351 { "lseek", remote_fileio_func_lseek },
1352 { "rename", remote_fileio_func_rename },
1353 { "unlink", remote_fileio_func_unlink },
1354 { "stat", remote_fileio_func_stat },
1355 { "fstat", remote_fileio_func_fstat },
1356 { "gettimeofday", remote_fileio_func_gettimeofday },
1357 { "isatty", remote_fileio_func_isatty },
1358 { "system", remote_fileio_func_system },
1363 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1365 char *buf = buf_arg;
1369 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1371 c = strchr (++buf, ',');
1375 c = strchr (buf, '\0');
1376 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1377 if (!strcmp (remote_fio_func_map[idx].name, buf))
1379 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1380 return RETURN_ERROR;
1381 remote_fio_func_map[idx].func (c);
1385 /* Close any open descriptors, and reinitialize the file mapping. */
1388 remote_fileio_reset (void)
1392 for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
1394 int fd = remote_fio_data.fd_map[ix];
1399 if (remote_fio_data.fd_map)
1401 xfree (remote_fio_data.fd_map);
1402 remote_fio_data.fd_map = NULL;
1403 remote_fio_data.fd_map_size = 0;
1407 /* Handle a file I/O request. BUF points to the packet containing the
1408 request. CTRLC_PENDING_P should be nonzero if the target has not
1409 acknowledged the Ctrl-C sent asynchronously earlier. */
1412 remote_fileio_request (char *buf, int ctrlc_pending_p)
1416 remote_fileio_sig_init ();
1418 if (ctrlc_pending_p)
1420 /* If the target hasn't responded to the Ctrl-C sent
1421 asynchronously earlier, take this opportunity to send the
1422 Ctrl-C synchronously. */
1423 remote_fio_ctrl_c_flag = 1;
1424 remote_fio_no_longjmp = 0;
1425 remote_fileio_reply (-1, FILEIO_EINTR);
1429 remote_fio_ctrl_c_flag = 0;
1430 remote_fio_no_longjmp = 0;
1432 ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1437 remote_fileio_reply (-1, FILEIO_ENOSYS);
1440 remote_fileio_reply (-1, FILEIO_EINTR);
1447 remote_fileio_sig_exit ();
1451 set_system_call_allowed (char *args, int from_tty)
1456 int val = strtoul (args, &arg_end, 10);
1458 if (*args && *arg_end == '\0')
1460 remote_fio_system_call_allowed = !!val;
1464 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1468 show_system_call_allowed (char *args, int from_tty)
1471 error (_("Garbage after \"show remote "
1472 "system-call-allowed\" command: `%s'"), args);
1473 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1474 remote_fio_system_call_allowed ? "" : "not ");
1478 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1479 struct cmd_list_element *remote_show_cmdlist)
1481 sigint_fileio_token =
1482 create_async_signal_handler (async_remote_fileio_interrupt, NULL);
1484 add_cmd ("system-call-allowed", no_class,
1485 set_system_call_allowed,
1486 _("Set if the host system(3) call is allowed for the target."),
1487 &remote_set_cmdlist);
1488 add_cmd ("system-call-allowed", no_class,
1489 show_system_call_allowed,
1490 _("Show if the host system(3) call is allowed for the target."),
1491 &remote_show_cmdlist);