1 /* Remote File-I/O communications
3 Copyright (C) 2003-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See the GDB User Guide for details of the GDB remote protocol. */
27 #include "remote-fileio.h"
28 #include "event-loop.h"
30 #include "filenames.h"
31 #include "filestuff.h"
36 #include <sys/cygwin.h> /* For cygwin_conv_path. */
45 #define FIO_FD_INVALID -1
46 #define FIO_FD_CONSOLE_IN -2
47 #define FIO_FD_CONSOLE_OUT -3
49 static int remote_fio_system_call_allowed
= 0;
51 static struct async_signal_handler
*sigint_fileio_token
;
54 remote_fileio_init_fd_map (void)
58 if (!remote_fio_data
.fd_map
)
60 remote_fio_data
.fd_map
= (int *) xmalloc (10 * sizeof (int));
61 remote_fio_data
.fd_map_size
= 10;
62 remote_fio_data
.fd_map
[0] = FIO_FD_CONSOLE_IN
;
63 remote_fio_data
.fd_map
[1] = FIO_FD_CONSOLE_OUT
;
64 remote_fio_data
.fd_map
[2] = FIO_FD_CONSOLE_OUT
;
65 for (i
= 3; i
< 10; ++i
)
66 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
72 remote_fileio_resize_fd_map (void)
74 int i
= remote_fio_data
.fd_map_size
;
76 if (!remote_fio_data
.fd_map
)
77 return remote_fileio_init_fd_map ();
78 remote_fio_data
.fd_map_size
+= 10;
79 remote_fio_data
.fd_map
=
80 (int *) xrealloc (remote_fio_data
.fd_map
,
81 remote_fio_data
.fd_map_size
* sizeof (int));
82 for (; i
< remote_fio_data
.fd_map_size
; i
++)
83 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
84 return remote_fio_data
.fd_map_size
- 10;
88 remote_fileio_next_free_fd (void)
92 for (i
= 0; i
< remote_fio_data
.fd_map_size
; ++i
)
93 if (remote_fio_data
.fd_map
[i
] == FIO_FD_INVALID
)
95 return remote_fileio_resize_fd_map ();
99 remote_fileio_fd_to_targetfd (int fd
)
101 int target_fd
= remote_fileio_next_free_fd ();
103 remote_fio_data
.fd_map
[target_fd
] = fd
;
108 remote_fileio_map_fd (int target_fd
)
110 remote_fileio_init_fd_map ();
111 if (target_fd
< 0 || target_fd
>= remote_fio_data
.fd_map_size
)
112 return FIO_FD_INVALID
;
113 return remote_fio_data
.fd_map
[target_fd
];
117 remote_fileio_close_target_fd (int target_fd
)
119 remote_fileio_init_fd_map ();
120 if (target_fd
>= 0 && target_fd
< remote_fio_data
.fd_map_size
)
121 remote_fio_data
.fd_map
[target_fd
] = FIO_FD_INVALID
;
125 remote_fileio_oflags_to_host (long flags
)
129 if (flags
& FILEIO_O_CREAT
)
131 if (flags
& FILEIO_O_EXCL
)
133 if (flags
& FILEIO_O_TRUNC
)
135 if (flags
& FILEIO_O_APPEND
)
137 if (flags
& FILEIO_O_RDONLY
)
139 if (flags
& FILEIO_O_WRONLY
)
141 if (flags
& FILEIO_O_RDWR
)
143 /* On systems supporting binary and text mode, always open files in
152 remote_fileio_mode_to_host (long mode
, int open_call
)
158 if (mode
& FILEIO_S_IFREG
)
160 if (mode
& FILEIO_S_IFDIR
)
162 if (mode
& FILEIO_S_IFCHR
)
165 if (mode
& FILEIO_S_IRUSR
)
167 if (mode
& FILEIO_S_IWUSR
)
169 if (mode
& FILEIO_S_IXUSR
)
172 if (mode
& FILEIO_S_IRGRP
)
176 if (mode
& FILEIO_S_IWGRP
)
180 if (mode
& FILEIO_S_IXGRP
)
183 if (mode
& FILEIO_S_IROTH
)
186 if (mode
& FILEIO_S_IWOTH
)
190 if (mode
& FILEIO_S_IXOTH
)
197 remote_fileio_errno_to_target (int error
)
204 return FILEIO_ENOENT
;
212 return FILEIO_EACCES
;
214 return FILEIO_EFAULT
;
218 return FILEIO_EEXIST
;
220 return FILEIO_ENODEV
;
222 return FILEIO_ENOTDIR
;
224 return FILEIO_EISDIR
;
226 return FILEIO_EINVAL
;
228 return FILEIO_ENFILE
;
230 return FILEIO_EMFILE
;
234 return FILEIO_ENOSPC
;
236 return FILEIO_ESPIPE
;
240 return FILEIO_ENOSYS
;
242 return FILEIO_ENAMETOOLONG
;
244 return FILEIO_EUNKNOWN
;
248 remote_fileio_seek_flag_to_host (long num
, int *flag
)
254 case FILEIO_SEEK_SET
:
257 case FILEIO_SEEK_CUR
:
260 case FILEIO_SEEK_END
:
270 remote_fileio_extract_long (char **buf
, LONGEST
*retlong
)
275 if (!buf
|| !*buf
|| !**buf
|| !retlong
)
277 c
= strchr (*buf
, ',');
281 c
= strchr (*buf
, '\0');
282 while (strchr ("+-", **buf
))
288 for (*retlong
= 0; **buf
; ++*buf
)
291 if (**buf
>= '0' && **buf
<= '9')
292 *retlong
+= **buf
- '0';
293 else if (**buf
>= 'a' && **buf
<= 'f')
294 *retlong
+= **buf
- 'a' + 10;
295 else if (**buf
>= 'A' && **buf
<= 'F')
296 *retlong
+= **buf
- 'A' + 10;
306 remote_fileio_extract_int (char **buf
, long *retint
)
313 ret
= remote_fileio_extract_long (buf
, &retlong
);
315 *retint
= (long) retlong
;
320 remote_fileio_extract_ptr_w_len (char **buf
, CORE_ADDR
*ptrval
, int *length
)
325 if (!buf
|| !*buf
|| !**buf
|| !ptrval
|| !length
)
327 c
= strchr (*buf
, '/');
331 if (remote_fileio_extract_long (buf
, &retlong
))
333 *ptrval
= (CORE_ADDR
) retlong
;
335 if (remote_fileio_extract_long (buf
, &retlong
))
337 *length
= (int) retlong
;
342 remote_fileio_to_fio_long (LONGEST num
, fio_long_t fnum
)
344 remote_fileio_to_be (num
, (char *) fnum
, 8);
348 remote_fileio_to_fio_timeval (struct timeval
*tv
, struct fio_timeval
*ftv
)
350 remote_fileio_to_fio_time (tv
->tv_sec
, ftv
->ftv_sec
);
351 remote_fileio_to_fio_long (tv
->tv_usec
, ftv
->ftv_usec
);
354 static int remote_fio_ctrl_c_flag
= 0;
355 static int remote_fio_no_longjmp
= 0;
357 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
358 static struct sigaction remote_fio_sa
;
359 static struct sigaction remote_fio_osa
;
361 static void (*remote_fio_ofunc
)(int);
365 remote_fileio_sig_init (void)
367 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
368 remote_fio_sa
.sa_handler
= SIG_IGN
;
369 sigemptyset (&remote_fio_sa
.sa_mask
);
370 remote_fio_sa
.sa_flags
= 0;
371 sigaction (SIGINT
, &remote_fio_sa
, &remote_fio_osa
);
373 remote_fio_ofunc
= signal (SIGINT
, SIG_IGN
);
378 remote_fileio_sig_set (void (*sigint_func
)(int))
380 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
381 remote_fio_sa
.sa_handler
= sigint_func
;
382 sigemptyset (&remote_fio_sa
.sa_mask
);
383 remote_fio_sa
.sa_flags
= 0;
384 sigaction (SIGINT
, &remote_fio_sa
, NULL
);
386 signal (SIGINT
, sigint_func
);
391 remote_fileio_sig_exit (void)
393 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
394 sigaction (SIGINT
, &remote_fio_osa
, NULL
);
396 signal (SIGINT
, remote_fio_ofunc
);
401 async_remote_fileio_interrupt (gdb_client_data arg
)
407 remote_fileio_ctrl_c_signal_handler (int signo
)
409 remote_fileio_sig_set (SIG_IGN
);
410 remote_fio_ctrl_c_flag
= 1;
411 if (!remote_fio_no_longjmp
)
412 gdb_call_async_signal_handler (sigint_fileio_token
, 1);
413 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
417 remote_fileio_reply (int retcode
, int error
)
421 remote_fileio_sig_set (SIG_IGN
);
428 sprintf (buf
+ strlen (buf
), "%x", retcode
);
429 if (error
|| remote_fio_ctrl_c_flag
)
431 if (error
&& remote_fio_ctrl_c_flag
)
432 error
= FILEIO_EINTR
;
438 sprintf (buf
+ strlen (buf
), ",%x", error
);
439 if (remote_fio_ctrl_c_flag
)
442 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
447 remote_fileio_ioerror (void)
449 remote_fileio_reply (-1, FILEIO_EIO
);
453 remote_fileio_badfd (void)
455 remote_fileio_reply (-1, FILEIO_EBADF
);
459 remote_fileio_return_errno (int retcode
)
461 remote_fileio_reply (retcode
, retcode
< 0
462 ? remote_fileio_errno_to_target (errno
) : 0);
466 remote_fileio_return_success (int retcode
)
468 remote_fileio_reply (retcode
, 0);
472 remote_fileio_func_open (char *buf
)
482 /* 1. Parameter: Ptr to pathname / length incl. trailing zero. */
483 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
485 remote_fileio_ioerror ();
488 /* 2. Parameter: open flags */
489 if (remote_fileio_extract_int (&buf
, &num
))
491 remote_fileio_ioerror ();
494 flags
= remote_fileio_oflags_to_host (num
);
495 /* 3. Parameter: open mode */
496 if (remote_fileio_extract_int (&buf
, &num
))
498 remote_fileio_ioerror ();
501 mode
= remote_fileio_mode_to_host (num
, 1);
503 /* Request pathname. */
504 pathname
= alloca (length
);
505 if (target_read_memory (ptrval
, (gdb_byte
*) pathname
, length
) != 0)
507 remote_fileio_ioerror ();
511 /* Check if pathname exists and is not a regular file or directory. If so,
512 return an appropriate error code. Same for trying to open directories
514 if (!stat (pathname
, &st
))
516 if (!S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
518 remote_fileio_reply (-1, FILEIO_ENODEV
);
521 if (S_ISDIR (st
.st_mode
)
522 && ((flags
& O_WRONLY
) == O_WRONLY
|| (flags
& O_RDWR
) == O_RDWR
))
524 remote_fileio_reply (-1, FILEIO_EISDIR
);
529 remote_fio_no_longjmp
= 1;
530 fd
= gdb_open_cloexec (pathname
, flags
, mode
);
533 remote_fileio_return_errno (-1);
537 fd
= remote_fileio_fd_to_targetfd (fd
);
538 remote_fileio_return_success (fd
);
542 remote_fileio_func_close (char *buf
)
547 /* Parameter: file descriptor */
548 if (remote_fileio_extract_int (&buf
, &num
))
550 remote_fileio_ioerror ();
553 fd
= remote_fileio_map_fd ((int) num
);
554 if (fd
== FIO_FD_INVALID
)
556 remote_fileio_badfd ();
560 remote_fio_no_longjmp
= 1;
561 if (fd
!= FIO_FD_CONSOLE_IN
&& fd
!= FIO_FD_CONSOLE_OUT
&& close (fd
))
562 remote_fileio_return_errno (-1);
563 remote_fileio_close_target_fd ((int) num
);
564 remote_fileio_return_success (0);
568 remote_fileio_func_read (char *buf
)
576 off_t old_offset
, new_offset
;
578 /* 1. Parameter: file descriptor */
579 if (remote_fileio_extract_int (&buf
, &target_fd
))
581 remote_fileio_ioerror ();
584 fd
= remote_fileio_map_fd ((int) target_fd
);
585 if (fd
== FIO_FD_INVALID
)
587 remote_fileio_badfd ();
590 /* 2. Parameter: buffer pointer */
591 if (remote_fileio_extract_long (&buf
, &lnum
))
593 remote_fileio_ioerror ();
596 ptrval
= (CORE_ADDR
) lnum
;
597 /* 3. Parameter: buffer length */
598 if (remote_fileio_extract_int (&buf
, &num
))
600 remote_fileio_ioerror ();
603 length
= (size_t) num
;
607 case FIO_FD_CONSOLE_OUT
:
608 remote_fileio_badfd ();
610 case FIO_FD_CONSOLE_IN
:
612 static char *remaining_buf
= NULL
;
613 static int remaining_length
= 0;
615 buffer
= (gdb_byte
*) xmalloc (16384);
618 remote_fio_no_longjmp
= 1;
619 if (remaining_length
> length
)
621 memcpy (buffer
, remaining_buf
, length
);
622 memmove (remaining_buf
, remaining_buf
+ length
,
623 remaining_length
- length
);
624 remaining_length
-= length
;
629 memcpy (buffer
, remaining_buf
, remaining_length
);
630 xfree (remaining_buf
);
631 remaining_buf
= NULL
;
632 ret
= remaining_length
;
637 /* Windows (at least XP and Server 2003) has difficulty
638 with large reads from consoles. If a handle is
639 backed by a real console device, overly large reads
640 from the handle will fail and set errno == ENOMEM.
641 On a Windows Server 2003 system where I tested,
642 reading 26608 bytes from the console was OK, but
643 anything above 26609 bytes would fail. The limit has
644 been observed to vary on different systems. So, we
645 limit this read to something smaller than that - by a
646 safe margin, in case the limit depends on system
647 resources or version. */
648 ret
= ui_file_read (gdb_stdtargin
, (char *) buffer
, 16383);
649 remote_fio_no_longjmp
= 1;
650 if (ret
> 0 && (size_t)ret
> length
)
652 remaining_buf
= (char *) xmalloc (ret
- length
);
653 remaining_length
= ret
- length
;
654 memcpy (remaining_buf
, buffer
+ length
, remaining_length
);
661 buffer
= (gdb_byte
*) xmalloc (length
);
662 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
663 for read() to return -1 even if "some" bytes have been read. It
664 has been corrected in SUSv2 but that doesn't help us much...
665 Therefore a complete solution must check how many bytes have been
666 read on EINTR to return a more reliable value to the target */
667 old_offset
= lseek (fd
, 0, SEEK_CUR
);
668 remote_fio_no_longjmp
= 1;
669 ret
= read (fd
, buffer
, length
);
670 if (ret
< 0 && errno
== EINTR
)
672 new_offset
= lseek (fd
, 0, SEEK_CUR
);
673 /* If some data has been read, return the number of bytes read.
674 The Ctrl-C flag is set in remote_fileio_reply() anyway. */
675 if (old_offset
!= new_offset
)
676 ret
= new_offset
- old_offset
;
683 errno
= target_write_memory (ptrval
, buffer
, ret
);
689 remote_fileio_return_errno (-1);
691 remote_fileio_return_success (ret
);
697 remote_fileio_func_write (char *buf
)
706 /* 1. Parameter: file descriptor */
707 if (remote_fileio_extract_int (&buf
, &target_fd
))
709 remote_fileio_ioerror ();
712 fd
= remote_fileio_map_fd ((int) target_fd
);
713 if (fd
== FIO_FD_INVALID
)
715 remote_fileio_badfd ();
718 /* 2. Parameter: buffer pointer */
719 if (remote_fileio_extract_long (&buf
, &lnum
))
721 remote_fileio_ioerror ();
724 ptrval
= (CORE_ADDR
) lnum
;
725 /* 3. Parameter: buffer length */
726 if (remote_fileio_extract_int (&buf
, &num
))
728 remote_fileio_ioerror ();
731 length
= (size_t) num
;
733 buffer
= (gdb_byte
*) xmalloc (length
);
734 if (target_read_memory (ptrval
, buffer
, length
) != 0)
737 remote_fileio_ioerror ();
741 remote_fio_no_longjmp
= 1;
744 case FIO_FD_CONSOLE_IN
:
745 remote_fileio_badfd ();
748 case FIO_FD_CONSOLE_OUT
:
749 ui_file_write (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
,
750 (char *) buffer
, length
);
751 gdb_flush (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
);
755 ret
= write (fd
, buffer
, length
);
756 if (ret
< 0 && errno
== EACCES
)
757 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a
763 remote_fileio_return_errno (-1);
765 remote_fileio_return_success (ret
);
771 remote_fileio_func_lseek (char *buf
)
778 /* 1. Parameter: file descriptor */
779 if (remote_fileio_extract_int (&buf
, &num
))
781 remote_fileio_ioerror ();
784 fd
= remote_fileio_map_fd ((int) num
);
785 if (fd
== FIO_FD_INVALID
)
787 remote_fileio_badfd ();
790 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
792 remote_fileio_reply (-1, FILEIO_ESPIPE
);
796 /* 2. Parameter: offset */
797 if (remote_fileio_extract_long (&buf
, &lnum
))
799 remote_fileio_ioerror ();
802 offset
= (off_t
) lnum
;
803 /* 3. Parameter: flag */
804 if (remote_fileio_extract_int (&buf
, &num
))
806 remote_fileio_ioerror ();
809 if (remote_fileio_seek_flag_to_host (num
, &flag
))
811 remote_fileio_reply (-1, FILEIO_EINVAL
);
815 remote_fio_no_longjmp
= 1;
816 ret
= lseek (fd
, offset
, flag
);
818 if (ret
== (off_t
) -1)
819 remote_fileio_return_errno (-1);
821 remote_fileio_return_success (ret
);
825 remote_fileio_func_rename (char *buf
)
827 CORE_ADDR old_ptr
, new_ptr
;
828 int old_len
, new_len
;
829 char *oldpath
, *newpath
;
831 struct stat ost
, nst
;
833 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
834 if (remote_fileio_extract_ptr_w_len (&buf
, &old_ptr
, &old_len
))
836 remote_fileio_ioerror ();
840 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
841 if (remote_fileio_extract_ptr_w_len (&buf
, &new_ptr
, &new_len
))
843 remote_fileio_ioerror ();
847 /* Request oldpath using 'm' packet */
848 oldpath
= alloca (old_len
);
849 if (target_read_memory (old_ptr
, (gdb_byte
*) oldpath
, old_len
) != 0)
851 remote_fileio_ioerror ();
855 /* Request newpath using 'm' packet */
856 newpath
= alloca (new_len
);
857 if (target_read_memory (new_ptr
, (gdb_byte
*) newpath
, new_len
) != 0)
859 remote_fileio_ioerror ();
863 /* Only operate on regular files and directories. */
864 of
= stat (oldpath
, &ost
);
865 nf
= stat (newpath
, &nst
);
866 if ((!of
&& !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
867 || (!nf
&& !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
869 remote_fileio_reply (-1, FILEIO_EACCES
);
873 remote_fio_no_longjmp
= 1;
874 ret
= rename (oldpath
, newpath
);
878 /* Special case: newpath is a non-empty directory. Some systems
879 return ENOTEMPTY, some return EEXIST. We coerce that to be
881 if (errno
== ENOTEMPTY
)
884 /* Workaround some Cygwin problems with correct errnos. */
887 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
889 if (S_ISREG (ost
.st_mode
))
893 char oldfullpath
[PATH_MAX
];
894 char newfullpath
[PATH_MAX
];
897 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, oldpath
, oldfullpath
,
899 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, newpath
, newfullpath
,
901 len
= strlen (oldfullpath
);
902 if (IS_DIR_SEPARATOR (newfullpath
[len
])
903 && !filename_ncmp (oldfullpath
, newfullpath
, len
))
912 remote_fileio_return_errno (-1);
915 remote_fileio_return_success (ret
);
919 remote_fileio_func_unlink (char *buf
)
927 /* Parameter: Ptr to pathname / length incl. trailing zero */
928 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
930 remote_fileio_ioerror ();
933 /* Request pathname using 'm' packet */
934 pathname
= alloca (length
);
935 if (target_read_memory (ptrval
, (gdb_byte
*) pathname
, length
) != 0)
937 remote_fileio_ioerror ();
941 /* Only operate on regular files (and directories, which allows to return
942 the correct return code). */
943 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
945 remote_fileio_reply (-1, FILEIO_ENODEV
);
949 remote_fio_no_longjmp
= 1;
950 ret
= unlink (pathname
);
953 remote_fileio_return_errno (-1);
955 remote_fileio_return_success (ret
);
959 remote_fileio_func_stat (char *buf
)
961 CORE_ADDR statptr
, nameptr
;
968 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
969 if (remote_fileio_extract_ptr_w_len (&buf
, &nameptr
, &namelength
))
971 remote_fileio_ioerror ();
975 /* 2. Parameter: Ptr to struct stat */
976 if (remote_fileio_extract_long (&buf
, &lnum
))
978 remote_fileio_ioerror ();
981 statptr
= (CORE_ADDR
) lnum
;
983 /* Request pathname using 'm' packet */
984 pathname
= alloca (namelength
);
985 if (target_read_memory (nameptr
, (gdb_byte
*) pathname
, namelength
) != 0)
987 remote_fileio_ioerror ();
991 remote_fio_no_longjmp
= 1;
992 ret
= stat (pathname
, &st
);
996 remote_fileio_return_errno (-1);
999 /* Only operate on regular files and directories. */
1000 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1002 remote_fileio_reply (-1, FILEIO_EACCES
);
1007 remote_fileio_to_fio_stat (&st
, &fst
);
1008 remote_fileio_to_fio_uint (0, fst
.fst_dev
);
1010 errno
= target_write_memory (statptr
, (gdb_byte
*) &fst
, sizeof fst
);
1013 remote_fileio_return_errno (-1);
1017 remote_fileio_return_success (ret
);
1021 remote_fileio_func_fstat (char *buf
)
1028 struct fio_stat fst
;
1031 /* 1. Parameter: file descriptor */
1032 if (remote_fileio_extract_int (&buf
, &target_fd
))
1034 remote_fileio_ioerror ();
1037 fd
= remote_fileio_map_fd ((int) target_fd
);
1038 if (fd
== FIO_FD_INVALID
)
1040 remote_fileio_badfd ();
1043 /* 2. Parameter: Ptr to struct stat */
1044 if (remote_fileio_extract_long (&buf
, &lnum
))
1046 remote_fileio_ioerror ();
1049 ptrval
= (CORE_ADDR
) lnum
;
1051 remote_fio_no_longjmp
= 1;
1052 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
1054 remote_fileio_to_fio_uint (1, fst
.fst_dev
);
1055 memset (&st
, 0, sizeof (st
));
1056 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
1059 st
.st_uid
= getuid ();
1062 st
.st_gid
= getgid ();
1064 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1065 st
.st_blksize
= 512;
1067 #if HAVE_STRUCT_STAT_ST_BLOCKS
1070 if (!gettimeofday (&tv
, NULL
))
1071 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
1073 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
1077 ret
= fstat (fd
, &st
);
1081 remote_fileio_return_errno (-1);
1086 remote_fileio_to_fio_stat (&st
, &fst
);
1088 errno
= target_write_memory (ptrval
, (gdb_byte
*) &fst
, sizeof fst
);
1091 remote_fileio_return_errno (-1);
1095 remote_fileio_return_success (ret
);
1099 remote_fileio_func_gettimeofday (char *buf
)
1105 struct fio_timeval ftv
;
1107 /* 1. Parameter: struct timeval pointer */
1108 if (remote_fileio_extract_long (&buf
, &lnum
))
1110 remote_fileio_ioerror ();
1113 ptrval
= (CORE_ADDR
) lnum
;
1114 /* 2. Parameter: some pointer value... */
1115 if (remote_fileio_extract_long (&buf
, &lnum
))
1117 remote_fileio_ioerror ();
1120 /* ...which has to be NULL. */
1123 remote_fileio_reply (-1, FILEIO_EINVAL
);
1127 remote_fio_no_longjmp
= 1;
1128 ret
= gettimeofday (&tv
, NULL
);
1132 remote_fileio_return_errno (-1);
1138 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1140 errno
= target_write_memory (ptrval
, (gdb_byte
*) &ftv
, sizeof ftv
);
1143 remote_fileio_return_errno (-1);
1147 remote_fileio_return_success (ret
);
1151 remote_fileio_func_isatty (char *buf
)
1156 /* Parameter: file descriptor */
1157 if (remote_fileio_extract_int (&buf
, &target_fd
))
1159 remote_fileio_ioerror ();
1162 remote_fio_no_longjmp
= 1;
1163 fd
= remote_fileio_map_fd ((int) target_fd
);
1164 remote_fileio_return_success (fd
== FIO_FD_CONSOLE_IN
||
1165 fd
== FIO_FD_CONSOLE_OUT
? 1 : 0);
1169 remote_fileio_func_system (char *buf
)
1173 char *cmdline
= NULL
;
1175 /* Parameter: Ptr to commandline / length incl. trailing zero */
1176 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1178 remote_fileio_ioerror ();
1184 /* Request commandline using 'm' packet */
1185 cmdline
= alloca (length
);
1186 if (target_read_memory (ptrval
, (gdb_byte
*) cmdline
, length
) != 0)
1188 remote_fileio_ioerror ();
1193 /* Check if system(3) has been explicitely allowed using the
1194 `set remote system-call-allowed 1' command. If length is 0,
1195 indicating a NULL parameter to the system call, return zero to
1196 indicate a shell is not available. Otherwise fail with EPERM. */
1197 if (!remote_fio_system_call_allowed
)
1200 remote_fileio_return_success (0);
1202 remote_fileio_reply (-1, FILEIO_EPERM
);
1206 remote_fio_no_longjmp
= 1;
1207 ret
= system (cmdline
);
1210 remote_fileio_return_success (ret
);
1212 remote_fileio_return_errno (-1);
1214 remote_fileio_return_success (WEXITSTATUS (ret
));
1219 void (*func
)(char *);
1220 } remote_fio_func_map
[] = {
1221 { "open", remote_fileio_func_open
},
1222 { "close", remote_fileio_func_close
},
1223 { "read", remote_fileio_func_read
},
1224 { "write", remote_fileio_func_write
},
1225 { "lseek", remote_fileio_func_lseek
},
1226 { "rename", remote_fileio_func_rename
},
1227 { "unlink", remote_fileio_func_unlink
},
1228 { "stat", remote_fileio_func_stat
},
1229 { "fstat", remote_fileio_func_fstat
},
1230 { "gettimeofday", remote_fileio_func_gettimeofday
},
1231 { "isatty", remote_fileio_func_isatty
},
1232 { "system", remote_fileio_func_system
},
1237 do_remote_fileio_request (struct ui_out
*uiout
, void *buf_arg
)
1239 char *buf
= buf_arg
;
1243 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
1245 c
= strchr (++buf
, ',');
1249 c
= strchr (buf
, '\0');
1250 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1251 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1253 if (!remote_fio_func_map
[idx
].name
) /* ERROR: No such function. */
1254 return RETURN_ERROR
;
1255 remote_fio_func_map
[idx
].func (c
);
1259 /* Close any open descriptors, and reinitialize the file mapping. */
1262 remote_fileio_reset (void)
1266 for (ix
= 0; ix
!= remote_fio_data
.fd_map_size
; ix
++)
1268 int fd
= remote_fio_data
.fd_map
[ix
];
1273 if (remote_fio_data
.fd_map
)
1275 xfree (remote_fio_data
.fd_map
);
1276 remote_fio_data
.fd_map
= NULL
;
1277 remote_fio_data
.fd_map_size
= 0;
1281 /* Handle a file I/O request. BUF points to the packet containing the
1282 request. CTRLC_PENDING_P should be nonzero if the target has not
1283 acknowledged the Ctrl-C sent asynchronously earlier. */
1286 remote_fileio_request (char *buf
, int ctrlc_pending_p
)
1290 remote_fileio_sig_init ();
1292 if (ctrlc_pending_p
)
1294 /* If the target hasn't responded to the Ctrl-C sent
1295 asynchronously earlier, take this opportunity to send the
1296 Ctrl-C synchronously. */
1297 remote_fio_ctrl_c_flag
= 1;
1298 remote_fio_no_longjmp
= 0;
1299 remote_fileio_reply (-1, FILEIO_EINTR
);
1303 remote_fio_ctrl_c_flag
= 0;
1304 remote_fio_no_longjmp
= 0;
1306 ex
= catch_exceptions (current_uiout
,
1307 do_remote_fileio_request
, (void *)buf
,
1312 remote_fileio_reply (-1, FILEIO_ENOSYS
);
1315 remote_fileio_reply (-1, FILEIO_EINTR
);
1322 remote_fileio_sig_exit ();
1326 set_system_call_allowed (char *args
, int from_tty
)
1331 int val
= strtoul (args
, &arg_end
, 10);
1333 if (*args
&& *arg_end
== '\0')
1335 remote_fio_system_call_allowed
= !!val
;
1339 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1343 show_system_call_allowed (char *args
, int from_tty
)
1346 error (_("Garbage after \"show remote "
1347 "system-call-allowed\" command: `%s'"), args
);
1348 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1349 remote_fio_system_call_allowed
? "" : "not ");
1353 initialize_remote_fileio (struct cmd_list_element
*remote_set_cmdlist
,
1354 struct cmd_list_element
*remote_show_cmdlist
)
1356 sigint_fileio_token
=
1357 create_async_signal_handler (async_remote_fileio_interrupt
, NULL
);
1359 add_cmd ("system-call-allowed", no_class
,
1360 set_system_call_allowed
,
1361 _("Set if the host system(3) call is allowed for the target."),
1362 &remote_set_cmdlist
);
1363 add_cmd ("system-call-allowed", no_class
,
1364 show_system_call_allowed
,
1365 _("Show if the host system(3) call is allowed for the target."),
1366 &remote_show_cmdlist
);