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"
34 #include "filenames.h"
39 #include <sys/cygwin.h> /* For cygwin_conv_to_full_posix_path. */
40 #include <cygwin/version.h>
41 #if CYGWIN_VERSION_DLL_MAKE_COMBINED(CYGWIN_VERSION_API_MAJOR,CYGWIN_VERSION_API_MINOR) < 181
42 # define CCP_POSIX_TO_WIN_A 0
43 # define CCP_WIN_A_TO_POSIX 2
44 # define cygwin_conv_path(op, from, to, size) \
45 (op == CCP_WIN_A_TO_POSIX) ? \
46 cygwin_conv_to_full_posix_path (from, to) : \
47 cygwin_conv_to_win32_path (from, to)
57 #define FIO_FD_INVALID -1
58 #define FIO_FD_CONSOLE_IN -2
59 #define FIO_FD_CONSOLE_OUT -3
61 static int remote_fio_system_call_allowed = 0;
63 static struct async_signal_handler *sigint_fileio_token;
66 remote_fileio_init_fd_map (void)
70 if (!remote_fio_data.fd_map)
72 remote_fio_data.fd_map = (int *) xmalloc (10 * sizeof (int));
73 remote_fio_data.fd_map_size = 10;
74 remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN;
75 remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT;
76 remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT;
77 for (i = 3; i < 10; ++i)
78 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
84 remote_fileio_resize_fd_map (void)
86 int i = remote_fio_data.fd_map_size;
88 if (!remote_fio_data.fd_map)
89 return remote_fileio_init_fd_map ();
90 remote_fio_data.fd_map_size += 10;
91 remote_fio_data.fd_map =
92 (int *) xrealloc (remote_fio_data.fd_map,
93 remote_fio_data.fd_map_size * sizeof (int));
94 for (; i < remote_fio_data.fd_map_size; i++)
95 remote_fio_data.fd_map[i] = FIO_FD_INVALID;
96 return remote_fio_data.fd_map_size - 10;
100 remote_fileio_next_free_fd (void)
104 for (i = 0; i < remote_fio_data.fd_map_size; ++i)
105 if (remote_fio_data.fd_map[i] == FIO_FD_INVALID)
107 return remote_fileio_resize_fd_map ();
111 remote_fileio_fd_to_targetfd (int fd)
113 int target_fd = remote_fileio_next_free_fd ();
115 remote_fio_data.fd_map[target_fd] = fd;
120 remote_fileio_map_fd (int target_fd)
122 remote_fileio_init_fd_map ();
123 if (target_fd < 0 || target_fd >= remote_fio_data.fd_map_size)
124 return FIO_FD_INVALID;
125 return remote_fio_data.fd_map[target_fd];
129 remote_fileio_close_target_fd (int target_fd)
131 remote_fileio_init_fd_map ();
132 if (target_fd >= 0 && target_fd < remote_fio_data.fd_map_size)
133 remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID;
137 remote_fileio_oflags_to_host (long flags)
141 if (flags & FILEIO_O_CREAT)
143 if (flags & FILEIO_O_EXCL)
145 if (flags & FILEIO_O_TRUNC)
147 if (flags & FILEIO_O_APPEND)
149 if (flags & FILEIO_O_RDONLY)
151 if (flags & FILEIO_O_WRONLY)
153 if (flags & FILEIO_O_RDWR)
155 /* On systems supporting binary and text mode, always open files in
164 remote_fileio_mode_to_host (long mode, int open_call)
170 if (mode & FILEIO_S_IFREG)
172 if (mode & FILEIO_S_IFDIR)
174 if (mode & FILEIO_S_IFCHR)
177 if (mode & FILEIO_S_IRUSR)
179 if (mode & FILEIO_S_IWUSR)
181 if (mode & FILEIO_S_IXUSR)
184 if (mode & FILEIO_S_IRGRP)
188 if (mode & FILEIO_S_IWGRP)
192 if (mode & FILEIO_S_IXGRP)
195 if (mode & FILEIO_S_IROTH)
198 if (mode & FILEIO_S_IWOTH)
202 if (mode & FILEIO_S_IXOTH)
209 remote_fileio_mode_to_target (mode_t mode)
214 tmode |= FILEIO_S_IFREG;
216 tmode |= FILEIO_S_IFDIR;
218 tmode |= FILEIO_S_IFCHR;
220 tmode |= FILEIO_S_IRUSR;
222 tmode |= FILEIO_S_IWUSR;
224 tmode |= FILEIO_S_IXUSR;
227 tmode |= FILEIO_S_IRGRP;
231 tmode |= FILEIO_S_IWGRP;
235 tmode |= FILEIO_S_IXGRP;
238 tmode |= FILEIO_S_IROTH;
241 tmode |= FILEIO_S_IWOTH;
245 tmode |= FILEIO_S_IXOTH;
251 remote_fileio_errno_to_target (int error)
258 return FILEIO_ENOENT;
266 return FILEIO_EACCES;
268 return FILEIO_EFAULT;
272 return FILEIO_EEXIST;
274 return FILEIO_ENODEV;
276 return FILEIO_ENOTDIR;
278 return FILEIO_EISDIR;
280 return FILEIO_EINVAL;
282 return FILEIO_ENFILE;
284 return FILEIO_EMFILE;
288 return FILEIO_ENOSPC;
290 return FILEIO_ESPIPE;
294 return FILEIO_ENOSYS;
296 return FILEIO_ENAMETOOLONG;
298 return FILEIO_EUNKNOWN;
302 remote_fileio_seek_flag_to_host (long num, int *flag)
308 case FILEIO_SEEK_SET:
311 case FILEIO_SEEK_CUR:
314 case FILEIO_SEEK_END:
324 remote_fileio_extract_long (char **buf, LONGEST *retlong)
329 if (!buf || !*buf || !**buf || !retlong)
331 c = strchr (*buf, ',');
335 c = strchr (*buf, '\0');
336 while (strchr ("+-", **buf))
342 for (*retlong = 0; **buf; ++*buf)
345 if (**buf >= '0' && **buf <= '9')
346 *retlong += **buf - '0';
347 else if (**buf >= 'a' && **buf <= 'f')
348 *retlong += **buf - 'a' + 10;
349 else if (**buf >= 'A' && **buf <= 'F')
350 *retlong += **buf - 'A' + 10;
360 remote_fileio_extract_int (char **buf, long *retint)
367 ret = remote_fileio_extract_long (buf, &retlong);
369 *retint = (long) retlong;
374 remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
379 if (!buf || !*buf || !**buf || !ptrval || !length)
381 c = strchr (*buf, '/');
385 if (remote_fileio_extract_long (buf, &retlong))
387 *ptrval = (CORE_ADDR) retlong;
389 if (remote_fileio_extract_long (buf, &retlong))
391 *length = (int) retlong;
395 /* Convert to big endian. */
397 remote_fileio_to_be (LONGEST num, char *buf, int bytes)
401 for (i = 0; i < bytes; ++i)
402 buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
406 remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
408 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
412 remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
414 remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
418 remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
420 remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
424 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
426 remote_fileio_to_be (num, (char *) fnum, 8);
430 remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
432 remote_fileio_to_be (num, (char *) fnum, 8);
436 remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
440 /* `st_dev' is set in the calling function. */
441 remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
442 remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
443 remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
444 remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
445 remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
446 remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
447 remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
448 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
449 blksize = st->st_blksize;
453 remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
454 #if HAVE_STRUCT_STAT_ST_BLOCKS
455 remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
457 /* FIXME: This is correct for DJGPP, but other systems that don't
458 have st_blocks, if any, might prefer 512 instead of st_blksize.
459 (eliz, 30-12-2003) */
460 remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
464 remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
465 remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
466 remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
470 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
472 remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
473 remote_fileio_to_fio_long (tv->tv_usec, ftv->ftv_usec);
476 static int remote_fio_ctrl_c_flag = 0;
477 static int remote_fio_no_longjmp = 0;
479 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
480 static struct sigaction remote_fio_sa;
481 static struct sigaction remote_fio_osa;
483 static void (*remote_fio_ofunc)(int);
487 remote_fileio_sig_init (void)
489 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
490 remote_fio_sa.sa_handler = SIG_IGN;
491 sigemptyset (&remote_fio_sa.sa_mask);
492 remote_fio_sa.sa_flags = 0;
493 sigaction (SIGINT, &remote_fio_sa, &remote_fio_osa);
495 remote_fio_ofunc = signal (SIGINT, SIG_IGN);
500 remote_fileio_sig_set (void (*sigint_func)(int))
502 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
503 remote_fio_sa.sa_handler = sigint_func;
504 sigemptyset (&remote_fio_sa.sa_mask);
505 remote_fio_sa.sa_flags = 0;
506 sigaction (SIGINT, &remote_fio_sa, NULL);
508 signal (SIGINT, sigint_func);
513 remote_fileio_sig_exit (void)
515 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
516 sigaction (SIGINT, &remote_fio_osa, NULL);
518 signal (SIGINT, remote_fio_ofunc);
523 async_remote_fileio_interrupt (gdb_client_data arg)
525 deprecated_throw_reason (RETURN_QUIT);
529 remote_fileio_ctrl_c_signal_handler (int signo)
531 remote_fileio_sig_set (SIG_IGN);
532 remote_fio_ctrl_c_flag = 1;
533 if (!remote_fio_no_longjmp)
534 gdb_call_async_signal_handler (sigint_fileio_token, 1);
535 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
539 remote_fileio_reply (int retcode, int error)
543 remote_fileio_sig_set (SIG_IGN);
550 sprintf (buf + strlen (buf), "%x", retcode);
551 if (error || remote_fio_ctrl_c_flag)
553 if (error && remote_fio_ctrl_c_flag)
554 error = FILEIO_EINTR;
560 sprintf (buf + strlen (buf), ",%x", error);
561 if (remote_fio_ctrl_c_flag)
564 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
569 remote_fileio_ioerror (void)
571 remote_fileio_reply (-1, FILEIO_EIO);
575 remote_fileio_badfd (void)
577 remote_fileio_reply (-1, FILEIO_EBADF);
581 remote_fileio_return_errno (int retcode)
583 remote_fileio_reply (retcode, retcode < 0
584 ? remote_fileio_errno_to_target (errno) : 0);
588 remote_fileio_return_success (int retcode)
590 remote_fileio_reply (retcode, 0);
594 remote_fileio_func_open (char *buf)
604 /* 1. Parameter: Ptr to pathname / length incl. trailing zero. */
605 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
607 remote_fileio_ioerror ();
610 /* 2. Parameter: open flags */
611 if (remote_fileio_extract_int (&buf, &num))
613 remote_fileio_ioerror ();
616 flags = remote_fileio_oflags_to_host (num);
617 /* 3. Parameter: open mode */
618 if (remote_fileio_extract_int (&buf, &num))
620 remote_fileio_ioerror ();
623 mode = remote_fileio_mode_to_host (num, 1);
625 /* Request pathname. */
626 pathname = alloca (length);
627 if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
629 remote_fileio_ioerror ();
633 /* Check if pathname exists and is not a regular file or directory. If so,
634 return an appropriate error code. Same for trying to open directories
636 if (!stat (pathname, &st))
638 if (!S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
640 remote_fileio_reply (-1, FILEIO_ENODEV);
643 if (S_ISDIR (st.st_mode)
644 && ((flags & O_WRONLY) == O_WRONLY || (flags & O_RDWR) == O_RDWR))
646 remote_fileio_reply (-1, FILEIO_EISDIR);
651 remote_fio_no_longjmp = 1;
652 fd = open (pathname, flags, mode);
655 remote_fileio_return_errno (-1);
659 fd = remote_fileio_fd_to_targetfd (fd);
660 remote_fileio_return_success (fd);
664 remote_fileio_func_close (char *buf)
669 /* Parameter: file descriptor */
670 if (remote_fileio_extract_int (&buf, &num))
672 remote_fileio_ioerror ();
675 fd = remote_fileio_map_fd ((int) num);
676 if (fd == FIO_FD_INVALID)
678 remote_fileio_badfd ();
682 remote_fio_no_longjmp = 1;
683 if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd))
684 remote_fileio_return_errno (-1);
685 remote_fileio_close_target_fd ((int) num);
686 remote_fileio_return_success (0);
690 remote_fileio_func_read (char *buf)
695 int fd, ret, retlength;
698 off_t old_offset, new_offset;
700 /* 1. Parameter: file descriptor */
701 if (remote_fileio_extract_int (&buf, &target_fd))
703 remote_fileio_ioerror ();
706 fd = remote_fileio_map_fd ((int) target_fd);
707 if (fd == FIO_FD_INVALID)
709 remote_fileio_badfd ();
712 /* 2. Parameter: buffer pointer */
713 if (remote_fileio_extract_long (&buf, &lnum))
715 remote_fileio_ioerror ();
718 ptrval = (CORE_ADDR) lnum;
719 /* 3. Parameter: buffer length */
720 if (remote_fileio_extract_int (&buf, &num))
722 remote_fileio_ioerror ();
725 length = (size_t) num;
729 case FIO_FD_CONSOLE_OUT:
730 remote_fileio_badfd ();
732 case FIO_FD_CONSOLE_IN:
734 static char *remaining_buf = NULL;
735 static int remaining_length = 0;
737 buffer = (gdb_byte *) xmalloc (16384);
740 remote_fio_no_longjmp = 1;
741 if (remaining_length > length)
743 memcpy (buffer, remaining_buf, length);
744 memmove (remaining_buf, remaining_buf + length,
745 remaining_length - length);
746 remaining_length -= length;
751 memcpy (buffer, remaining_buf, remaining_length);
752 xfree (remaining_buf);
753 remaining_buf = NULL;
754 ret = remaining_length;
759 /* Windows (at least XP and Server 2003) has difficulty
760 with large reads from consoles. If a handle is
761 backed by a real console device, overly large reads
762 from the handle will fail and set errno == ENOMEM.
763 On a Windows Server 2003 system where I tested,
764 reading 26608 bytes from the console was OK, but
765 anything above 26609 bytes would fail. The limit has
766 been observed to vary on different systems. So, we
767 limit this read to something smaller than that - by a
768 safe margin, in case the limit depends on system
769 resources or version. */
770 ret = ui_file_read (gdb_stdtargin, (char *) buffer, 16383);
771 remote_fio_no_longjmp = 1;
772 if (ret > 0 && (size_t)ret > length)
774 remaining_buf = (char *) xmalloc (ret - length);
775 remaining_length = ret - length;
776 memcpy (remaining_buf, buffer + length, remaining_length);
783 buffer = (gdb_byte *) xmalloc (length);
784 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
785 for read() to return -1 even if "some" bytes have been read. It
786 has been corrected in SUSv2 but that doesn't help us much...
787 Therefore a complete solution must check how many bytes have been
788 read on EINTR to return a more reliable value to the target */
789 old_offset = lseek (fd, 0, SEEK_CUR);
790 remote_fio_no_longjmp = 1;
791 ret = read (fd, buffer, length);
792 if (ret < 0 && errno == EINTR)
794 new_offset = lseek (fd, 0, SEEK_CUR);
795 /* If some data has been read, return the number of bytes read.
796 The Ctrl-C flag is set in remote_fileio_reply() anyway. */
797 if (old_offset != new_offset)
798 ret = new_offset - old_offset;
805 errno = target_write_memory (ptrval, buffer, ret);
811 remote_fileio_return_errno (-1);
813 remote_fileio_return_success (ret);
819 remote_fileio_func_write (char *buf)
828 /* 1. Parameter: file descriptor */
829 if (remote_fileio_extract_int (&buf, &target_fd))
831 remote_fileio_ioerror ();
834 fd = remote_fileio_map_fd ((int) target_fd);
835 if (fd == FIO_FD_INVALID)
837 remote_fileio_badfd ();
840 /* 2. Parameter: buffer pointer */
841 if (remote_fileio_extract_long (&buf, &lnum))
843 remote_fileio_ioerror ();
846 ptrval = (CORE_ADDR) lnum;
847 /* 3. Parameter: buffer length */
848 if (remote_fileio_extract_int (&buf, &num))
850 remote_fileio_ioerror ();
853 length = (size_t) num;
855 buffer = (gdb_byte *) xmalloc (length);
856 if (target_read_memory (ptrval, buffer, length) != 0)
859 remote_fileio_ioerror ();
863 remote_fio_no_longjmp = 1;
866 case FIO_FD_CONSOLE_IN:
867 remote_fileio_badfd ();
870 case FIO_FD_CONSOLE_OUT:
871 ui_file_write (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr,
872 (char *) buffer, length);
873 gdb_flush (target_fd == 1 ? gdb_stdtarg : gdb_stdtargerr);
877 ret = write (fd, buffer, length);
878 if (ret < 0 && errno == EACCES)
879 errno = EBADF; /* Cygwin returns EACCESS when writing to a
885 remote_fileio_return_errno (-1);
887 remote_fileio_return_success (ret);
893 remote_fileio_func_lseek (char *buf)
900 /* 1. Parameter: file descriptor */
901 if (remote_fileio_extract_int (&buf, &num))
903 remote_fileio_ioerror ();
906 fd = remote_fileio_map_fd ((int) num);
907 if (fd == FIO_FD_INVALID)
909 remote_fileio_badfd ();
912 else if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
914 remote_fileio_reply (-1, FILEIO_ESPIPE);
918 /* 2. Parameter: offset */
919 if (remote_fileio_extract_long (&buf, &lnum))
921 remote_fileio_ioerror ();
924 offset = (off_t) lnum;
925 /* 3. Parameter: flag */
926 if (remote_fileio_extract_int (&buf, &num))
928 remote_fileio_ioerror ();
931 if (remote_fileio_seek_flag_to_host (num, &flag))
933 remote_fileio_reply (-1, FILEIO_EINVAL);
937 remote_fio_no_longjmp = 1;
938 ret = lseek (fd, offset, flag);
940 if (ret == (off_t) -1)
941 remote_fileio_return_errno (-1);
943 remote_fileio_return_success (ret);
947 remote_fileio_func_rename (char *buf)
949 CORE_ADDR old_ptr, new_ptr;
950 int old_len, new_len;
951 char *oldpath, *newpath;
953 struct stat ost, nst;
955 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
956 if (remote_fileio_extract_ptr_w_len (&buf, &old_ptr, &old_len))
958 remote_fileio_ioerror ();
962 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
963 if (remote_fileio_extract_ptr_w_len (&buf, &new_ptr, &new_len))
965 remote_fileio_ioerror ();
969 /* Request oldpath using 'm' packet */
970 oldpath = alloca (old_len);
971 if (target_read_memory (old_ptr, (gdb_byte *) oldpath, old_len) != 0)
973 remote_fileio_ioerror ();
977 /* Request newpath using 'm' packet */
978 newpath = alloca (new_len);
979 if (target_read_memory (new_ptr, (gdb_byte *) newpath, new_len) != 0)
981 remote_fileio_ioerror ();
985 /* Only operate on regular files and directories. */
986 of = stat (oldpath, &ost);
987 nf = stat (newpath, &nst);
988 if ((!of && !S_ISREG (ost.st_mode) && !S_ISDIR (ost.st_mode))
989 || (!nf && !S_ISREG (nst.st_mode) && !S_ISDIR (nst.st_mode)))
991 remote_fileio_reply (-1, FILEIO_EACCES);
995 remote_fio_no_longjmp = 1;
996 ret = rename (oldpath, newpath);
1000 /* Special case: newpath is a non-empty directory. Some systems
1001 return ENOTEMPTY, some return EEXIST. We coerce that to be
1003 if (errno == ENOTEMPTY)
1006 /* Workaround some Cygwin problems with correct errnos. */
1007 if (errno == EACCES)
1009 if (!of && !nf && S_ISDIR (nst.st_mode))
1011 if (S_ISREG (ost.st_mode))
1015 char oldfullpath[PATH_MAX];
1016 char newfullpath[PATH_MAX];
1019 cygwin_conv_path (CCP_WIN_A_TO_POSIX, oldpath, oldfullpath,
1021 cygwin_conv_path (CCP_WIN_A_TO_POSIX, newpath, newfullpath,
1023 len = strlen (oldfullpath);
1024 if (IS_DIR_SEPARATOR (newfullpath[len])
1025 && !filename_ncmp (oldfullpath, newfullpath, len))
1034 remote_fileio_return_errno (-1);
1037 remote_fileio_return_success (ret);
1041 remote_fileio_func_unlink (char *buf)
1049 /* Parameter: Ptr to pathname / length incl. trailing zero */
1050 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1052 remote_fileio_ioerror ();
1055 /* Request pathname using 'm' packet */
1056 pathname = alloca (length);
1057 if (target_read_memory (ptrval, (gdb_byte *) pathname, length) != 0)
1059 remote_fileio_ioerror ();
1063 /* Only operate on regular files (and directories, which allows to return
1064 the correct return code). */
1065 if (!stat (pathname, &st) && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1067 remote_fileio_reply (-1, FILEIO_ENODEV);
1071 remote_fio_no_longjmp = 1;
1072 ret = unlink (pathname);
1075 remote_fileio_return_errno (-1);
1077 remote_fileio_return_success (ret);
1081 remote_fileio_func_stat (char *buf)
1083 CORE_ADDR statptr, nameptr;
1084 int ret, namelength;
1088 struct fio_stat fst;
1090 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1091 if (remote_fileio_extract_ptr_w_len (&buf, &nameptr, &namelength))
1093 remote_fileio_ioerror ();
1097 /* 2. Parameter: Ptr to struct stat */
1098 if (remote_fileio_extract_long (&buf, &lnum))
1100 remote_fileio_ioerror ();
1103 statptr = (CORE_ADDR) lnum;
1105 /* Request pathname using 'm' packet */
1106 pathname = alloca (namelength);
1107 if (target_read_memory (nameptr, (gdb_byte *) pathname, namelength) != 0)
1109 remote_fileio_ioerror ();
1113 remote_fio_no_longjmp = 1;
1114 ret = stat (pathname, &st);
1118 remote_fileio_return_errno (-1);
1121 /* Only operate on regular files and directories. */
1122 if (!ret && !S_ISREG (st.st_mode) && !S_ISDIR (st.st_mode))
1124 remote_fileio_reply (-1, FILEIO_EACCES);
1129 remote_fileio_to_fio_stat (&st, &fst);
1130 remote_fileio_to_fio_uint (0, fst.fst_dev);
1132 errno = target_write_memory (statptr, (gdb_byte *) &fst, sizeof fst);
1135 remote_fileio_return_errno (-1);
1139 remote_fileio_return_success (ret);
1143 remote_fileio_func_fstat (char *buf)
1146 int fd, ret, retlength;
1150 struct fio_stat fst;
1153 /* 1. Parameter: file descriptor */
1154 if (remote_fileio_extract_int (&buf, &target_fd))
1156 remote_fileio_ioerror ();
1159 fd = remote_fileio_map_fd ((int) target_fd);
1160 if (fd == FIO_FD_INVALID)
1162 remote_fileio_badfd ();
1165 /* 2. Parameter: Ptr to struct stat */
1166 if (remote_fileio_extract_long (&buf, &lnum))
1168 remote_fileio_ioerror ();
1171 ptrval = (CORE_ADDR) lnum;
1173 remote_fio_no_longjmp = 1;
1174 if (fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT)
1176 remote_fileio_to_fio_uint (1, fst.fst_dev);
1177 memset (&st, 0, sizeof (st));
1178 st.st_mode = S_IFCHR | (fd == FIO_FD_CONSOLE_IN ? S_IRUSR : S_IWUSR);
1181 st.st_uid = getuid ();
1184 st.st_gid = getgid ();
1186 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1187 st.st_blksize = 512;
1189 #if HAVE_STRUCT_STAT_ST_BLOCKS
1192 if (!gettimeofday (&tv, NULL))
1193 st.st_atime = st.st_mtime = st.st_ctime = tv.tv_sec;
1195 st.st_atime = st.st_mtime = st.st_ctime = (time_t) 0;
1199 ret = fstat (fd, &st);
1203 remote_fileio_return_errno (-1);
1208 remote_fileio_to_fio_stat (&st, &fst);
1210 errno = target_write_memory (ptrval, (gdb_byte *) &fst, sizeof fst);
1213 remote_fileio_return_errno (-1);
1217 remote_fileio_return_success (ret);
1221 remote_fileio_func_gettimeofday (char *buf)
1227 struct fio_timeval ftv;
1229 /* 1. Parameter: struct timeval pointer */
1230 if (remote_fileio_extract_long (&buf, &lnum))
1232 remote_fileio_ioerror ();
1235 ptrval = (CORE_ADDR) lnum;
1236 /* 2. Parameter: some pointer value... */
1237 if (remote_fileio_extract_long (&buf, &lnum))
1239 remote_fileio_ioerror ();
1242 /* ...which has to be NULL. */
1245 remote_fileio_reply (-1, FILEIO_EINVAL);
1249 remote_fio_no_longjmp = 1;
1250 ret = gettimeofday (&tv, NULL);
1254 remote_fileio_return_errno (-1);
1260 remote_fileio_to_fio_timeval (&tv, &ftv);
1262 errno = target_write_memory (ptrval, (gdb_byte *) &ftv, sizeof ftv);
1265 remote_fileio_return_errno (-1);
1269 remote_fileio_return_success (ret);
1273 remote_fileio_func_isatty (char *buf)
1278 /* Parameter: file descriptor */
1279 if (remote_fileio_extract_int (&buf, &target_fd))
1281 remote_fileio_ioerror ();
1284 remote_fio_no_longjmp = 1;
1285 fd = remote_fileio_map_fd ((int) target_fd);
1286 remote_fileio_return_success (fd == FIO_FD_CONSOLE_IN ||
1287 fd == FIO_FD_CONSOLE_OUT ? 1 : 0);
1291 remote_fileio_func_system (char *buf)
1294 int ret, length, retlength;
1295 char *cmdline = NULL;
1297 /* Parameter: Ptr to commandline / length incl. trailing zero */
1298 if (remote_fileio_extract_ptr_w_len (&buf, &ptrval, &length))
1300 remote_fileio_ioerror ();
1306 /* Request commandline using 'm' packet */
1307 cmdline = alloca (length);
1308 if (target_read_memory (ptrval, (gdb_byte *) cmdline, length) != 0)
1310 remote_fileio_ioerror ();
1315 /* Check if system(3) has been explicitely allowed using the
1316 `set remote system-call-allowed 1' command. If length is 0,
1317 indicating a NULL parameter to the system call, return zero to
1318 indicate a shell is not available. Otherwise fail with EPERM. */
1319 if (!remote_fio_system_call_allowed)
1322 remote_fileio_return_success (0);
1324 remote_fileio_reply (-1, FILEIO_EPERM);
1328 remote_fio_no_longjmp = 1;
1329 ret = system (cmdline);
1332 remote_fileio_return_success (ret);
1334 remote_fileio_return_errno (-1);
1336 remote_fileio_return_success (WEXITSTATUS (ret));
1341 void (*func)(char *);
1342 } remote_fio_func_map[] = {
1343 { "open", remote_fileio_func_open },
1344 { "close", remote_fileio_func_close },
1345 { "read", remote_fileio_func_read },
1346 { "write", remote_fileio_func_write },
1347 { "lseek", remote_fileio_func_lseek },
1348 { "rename", remote_fileio_func_rename },
1349 { "unlink", remote_fileio_func_unlink },
1350 { "stat", remote_fileio_func_stat },
1351 { "fstat", remote_fileio_func_fstat },
1352 { "gettimeofday", remote_fileio_func_gettimeofday },
1353 { "isatty", remote_fileio_func_isatty },
1354 { "system", remote_fileio_func_system },
1359 do_remote_fileio_request (struct ui_out *uiout, void *buf_arg)
1361 char *buf = buf_arg;
1365 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler);
1367 c = strchr (++buf, ',');
1371 c = strchr (buf, '\0');
1372 for (idx = 0; remote_fio_func_map[idx].name; ++idx)
1373 if (!strcmp (remote_fio_func_map[idx].name, buf))
1375 if (!remote_fio_func_map[idx].name) /* ERROR: No such function. */
1376 return RETURN_ERROR;
1377 remote_fio_func_map[idx].func (c);
1381 /* Close any open descriptors, and reinitialize the file mapping. */
1384 remote_fileio_reset (void)
1388 for (ix = 0; ix != remote_fio_data.fd_map_size; ix++)
1390 int fd = remote_fio_data.fd_map[ix];
1395 if (remote_fio_data.fd_map)
1397 xfree (remote_fio_data.fd_map);
1398 remote_fio_data.fd_map = NULL;
1399 remote_fio_data.fd_map_size = 0;
1403 /* Handle a file I/O request. BUF points to the packet containing the
1404 request. CTRLC_PENDING_P should be nonzero if the target has not
1405 acknowledged the Ctrl-C sent asynchronously earlier. */
1408 remote_fileio_request (char *buf, int ctrlc_pending_p)
1412 remote_fileio_sig_init ();
1414 if (ctrlc_pending_p)
1416 /* If the target hasn't responded to the Ctrl-C sent
1417 asynchronously earlier, take this opportunity to send the
1418 Ctrl-C synchronously. */
1419 remote_fio_ctrl_c_flag = 1;
1420 remote_fio_no_longjmp = 0;
1421 remote_fileio_reply (-1, FILEIO_EINTR);
1425 remote_fio_ctrl_c_flag = 0;
1426 remote_fio_no_longjmp = 0;
1428 ex = catch_exceptions (uiout, do_remote_fileio_request, (void *)buf,
1433 remote_fileio_reply (-1, FILEIO_ENOSYS);
1436 remote_fileio_reply (-1, FILEIO_EINTR);
1443 remote_fileio_sig_exit ();
1447 set_system_call_allowed (char *args, int from_tty)
1452 int val = strtoul (args, &arg_end, 10);
1454 if (*args && *arg_end == '\0')
1456 remote_fio_system_call_allowed = !!val;
1460 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1464 show_system_call_allowed (char *args, int from_tty)
1467 error (_("Garbage after \"show remote "
1468 "system-call-allowed\" command: `%s'"), args);
1469 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1470 remote_fio_system_call_allowed ? "" : "not ");
1474 initialize_remote_fileio (struct cmd_list_element *remote_set_cmdlist,
1475 struct cmd_list_element *remote_show_cmdlist)
1477 sigint_fileio_token =
1478 create_async_signal_handler (async_remote_fileio_interrupt, NULL);
1480 add_cmd ("system-call-allowed", no_class,
1481 set_system_call_allowed,
1482 _("Set if the host system(3) call is allowed for the target."),
1483 &remote_set_cmdlist);
1484 add_cmd ("system-call-allowed", no_class,
1485 show_system_call_allowed,
1486 _("Show if the host system(3) call is allowed for the target."),
1487 &remote_show_cmdlist);