1 /* Remote File-I/O communications
3 Copyright (C) 2003, 2005-2012 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. */
23 #include "gdb_string.h"
26 #include "gdb/fileio.h"
29 #include "exceptions.h"
30 #include "remote-fileio.h"
31 #include "event-loop.h"
33 #include "filenames.h"
38 #include <sys/cygwin.h> /* For cygwin_conv_path. */
47 #define FIO_FD_INVALID -1
48 #define FIO_FD_CONSOLE_IN -2
49 #define FIO_FD_CONSOLE_OUT -3
51 static int remote_fio_system_call_allowed
= 0;
53 static struct async_signal_handler
*sigint_fileio_token
;
56 remote_fileio_init_fd_map (void)
60 if (!remote_fio_data
.fd_map
)
62 remote_fio_data
.fd_map
= (int *) xmalloc (10 * sizeof (int));
63 remote_fio_data
.fd_map_size
= 10;
64 remote_fio_data
.fd_map
[0] = FIO_FD_CONSOLE_IN
;
65 remote_fio_data
.fd_map
[1] = FIO_FD_CONSOLE_OUT
;
66 remote_fio_data
.fd_map
[2] = FIO_FD_CONSOLE_OUT
;
67 for (i
= 3; i
< 10; ++i
)
68 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
74 remote_fileio_resize_fd_map (void)
76 int i
= remote_fio_data
.fd_map_size
;
78 if (!remote_fio_data
.fd_map
)
79 return remote_fileio_init_fd_map ();
80 remote_fio_data
.fd_map_size
+= 10;
81 remote_fio_data
.fd_map
=
82 (int *) xrealloc (remote_fio_data
.fd_map
,
83 remote_fio_data
.fd_map_size
* sizeof (int));
84 for (; i
< remote_fio_data
.fd_map_size
; i
++)
85 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
86 return remote_fio_data
.fd_map_size
- 10;
90 remote_fileio_next_free_fd (void)
94 for (i
= 0; i
< remote_fio_data
.fd_map_size
; ++i
)
95 if (remote_fio_data
.fd_map
[i
] == FIO_FD_INVALID
)
97 return remote_fileio_resize_fd_map ();
101 remote_fileio_fd_to_targetfd (int fd
)
103 int target_fd
= remote_fileio_next_free_fd ();
105 remote_fio_data
.fd_map
[target_fd
] = fd
;
110 remote_fileio_map_fd (int target_fd
)
112 remote_fileio_init_fd_map ();
113 if (target_fd
< 0 || target_fd
>= remote_fio_data
.fd_map_size
)
114 return FIO_FD_INVALID
;
115 return remote_fio_data
.fd_map
[target_fd
];
119 remote_fileio_close_target_fd (int target_fd
)
121 remote_fileio_init_fd_map ();
122 if (target_fd
>= 0 && target_fd
< remote_fio_data
.fd_map_size
)
123 remote_fio_data
.fd_map
[target_fd
] = FIO_FD_INVALID
;
127 remote_fileio_oflags_to_host (long flags
)
131 if (flags
& FILEIO_O_CREAT
)
133 if (flags
& FILEIO_O_EXCL
)
135 if (flags
& FILEIO_O_TRUNC
)
137 if (flags
& FILEIO_O_APPEND
)
139 if (flags
& FILEIO_O_RDONLY
)
141 if (flags
& FILEIO_O_WRONLY
)
143 if (flags
& FILEIO_O_RDWR
)
145 /* On systems supporting binary and text mode, always open files in
154 remote_fileio_mode_to_host (long mode
, int open_call
)
160 if (mode
& FILEIO_S_IFREG
)
162 if (mode
& FILEIO_S_IFDIR
)
164 if (mode
& FILEIO_S_IFCHR
)
167 if (mode
& FILEIO_S_IRUSR
)
169 if (mode
& FILEIO_S_IWUSR
)
171 if (mode
& FILEIO_S_IXUSR
)
174 if (mode
& FILEIO_S_IRGRP
)
178 if (mode
& FILEIO_S_IWGRP
)
182 if (mode
& FILEIO_S_IXGRP
)
185 if (mode
& FILEIO_S_IROTH
)
188 if (mode
& FILEIO_S_IWOTH
)
192 if (mode
& FILEIO_S_IXOTH
)
199 remote_fileio_mode_to_target (mode_t mode
)
204 tmode
|= FILEIO_S_IFREG
;
206 tmode
|= FILEIO_S_IFDIR
;
208 tmode
|= FILEIO_S_IFCHR
;
210 tmode
|= FILEIO_S_IRUSR
;
212 tmode
|= FILEIO_S_IWUSR
;
214 tmode
|= FILEIO_S_IXUSR
;
217 tmode
|= FILEIO_S_IRGRP
;
221 tmode
|= FILEIO_S_IWGRP
;
225 tmode
|= FILEIO_S_IXGRP
;
228 tmode
|= FILEIO_S_IROTH
;
231 tmode
|= FILEIO_S_IWOTH
;
235 tmode
|= FILEIO_S_IXOTH
;
241 remote_fileio_errno_to_target (int error
)
248 return FILEIO_ENOENT
;
256 return FILEIO_EACCES
;
258 return FILEIO_EFAULT
;
262 return FILEIO_EEXIST
;
264 return FILEIO_ENODEV
;
266 return FILEIO_ENOTDIR
;
268 return FILEIO_EISDIR
;
270 return FILEIO_EINVAL
;
272 return FILEIO_ENFILE
;
274 return FILEIO_EMFILE
;
278 return FILEIO_ENOSPC
;
280 return FILEIO_ESPIPE
;
284 return FILEIO_ENOSYS
;
286 return FILEIO_ENAMETOOLONG
;
288 return FILEIO_EUNKNOWN
;
292 remote_fileio_seek_flag_to_host (long num
, int *flag
)
298 case FILEIO_SEEK_SET
:
301 case FILEIO_SEEK_CUR
:
304 case FILEIO_SEEK_END
:
314 remote_fileio_extract_long (char **buf
, LONGEST
*retlong
)
319 if (!buf
|| !*buf
|| !**buf
|| !retlong
)
321 c
= strchr (*buf
, ',');
325 c
= strchr (*buf
, '\0');
326 while (strchr ("+-", **buf
))
332 for (*retlong
= 0; **buf
; ++*buf
)
335 if (**buf
>= '0' && **buf
<= '9')
336 *retlong
+= **buf
- '0';
337 else if (**buf
>= 'a' && **buf
<= 'f')
338 *retlong
+= **buf
- 'a' + 10;
339 else if (**buf
>= 'A' && **buf
<= 'F')
340 *retlong
+= **buf
- 'A' + 10;
350 remote_fileio_extract_int (char **buf
, long *retint
)
357 ret
= remote_fileio_extract_long (buf
, &retlong
);
359 *retint
= (long) retlong
;
364 remote_fileio_extract_ptr_w_len (char **buf
, CORE_ADDR
*ptrval
, int *length
)
369 if (!buf
|| !*buf
|| !**buf
|| !ptrval
|| !length
)
371 c
= strchr (*buf
, '/');
375 if (remote_fileio_extract_long (buf
, &retlong
))
377 *ptrval
= (CORE_ADDR
) retlong
;
379 if (remote_fileio_extract_long (buf
, &retlong
))
381 *length
= (int) retlong
;
385 /* Convert to big endian. */
387 remote_fileio_to_be (LONGEST num
, char *buf
, int bytes
)
391 for (i
= 0; i
< bytes
; ++i
)
392 buf
[i
] = (num
>> (8 * (bytes
- i
- 1))) & 0xff;
396 remote_fileio_to_fio_uint (long num
, fio_uint_t fnum
)
398 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
402 remote_fileio_to_fio_mode (mode_t num
, fio_mode_t fnum
)
404 remote_fileio_to_be (remote_fileio_mode_to_target(num
), (char *) fnum
, 4);
408 remote_fileio_to_fio_time (time_t num
, fio_time_t fnum
)
410 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
414 remote_fileio_to_fio_long (LONGEST num
, fio_long_t fnum
)
416 remote_fileio_to_be (num
, (char *) fnum
, 8);
420 remote_fileio_to_fio_ulong (LONGEST num
, fio_ulong_t fnum
)
422 remote_fileio_to_be (num
, (char *) fnum
, 8);
426 remote_fileio_to_fio_stat (struct stat
*st
, struct fio_stat
*fst
)
430 /* `st_dev' is set in the calling function. */
431 remote_fileio_to_fio_uint ((long) st
->st_ino
, fst
->fst_ino
);
432 remote_fileio_to_fio_mode (st
->st_mode
, fst
->fst_mode
);
433 remote_fileio_to_fio_uint ((long) st
->st_nlink
, fst
->fst_nlink
);
434 remote_fileio_to_fio_uint ((long) st
->st_uid
, fst
->fst_uid
);
435 remote_fileio_to_fio_uint ((long) st
->st_gid
, fst
->fst_gid
);
436 remote_fileio_to_fio_uint ((long) st
->st_rdev
, fst
->fst_rdev
);
437 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_size
, fst
->fst_size
);
438 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
439 blksize
= st
->st_blksize
;
443 remote_fileio_to_fio_ulong (blksize
, fst
->fst_blksize
);
444 #if HAVE_STRUCT_STAT_ST_BLOCKS
445 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_blocks
, fst
->fst_blocks
);
447 /* FIXME: This is correct for DJGPP, but other systems that don't
448 have st_blocks, if any, might prefer 512 instead of st_blksize.
449 (eliz, 30-12-2003) */
450 remote_fileio_to_fio_ulong (((LONGEST
) st
->st_size
+ blksize
- 1)
454 remote_fileio_to_fio_time (st
->st_atime
, fst
->fst_atime
);
455 remote_fileio_to_fio_time (st
->st_mtime
, fst
->fst_mtime
);
456 remote_fileio_to_fio_time (st
->st_ctime
, fst
->fst_ctime
);
460 remote_fileio_to_fio_timeval (struct timeval
*tv
, struct fio_timeval
*ftv
)
462 remote_fileio_to_fio_time (tv
->tv_sec
, ftv
->ftv_sec
);
463 remote_fileio_to_fio_long (tv
->tv_usec
, ftv
->ftv_usec
);
466 static int remote_fio_ctrl_c_flag
= 0;
467 static int remote_fio_no_longjmp
= 0;
469 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
470 static struct sigaction remote_fio_sa
;
471 static struct sigaction remote_fio_osa
;
473 static void (*remote_fio_ofunc
)(int);
477 remote_fileio_sig_init (void)
479 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
480 remote_fio_sa
.sa_handler
= SIG_IGN
;
481 sigemptyset (&remote_fio_sa
.sa_mask
);
482 remote_fio_sa
.sa_flags
= 0;
483 sigaction (SIGINT
, &remote_fio_sa
, &remote_fio_osa
);
485 remote_fio_ofunc
= signal (SIGINT
, SIG_IGN
);
490 remote_fileio_sig_set (void (*sigint_func
)(int))
492 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
493 remote_fio_sa
.sa_handler
= sigint_func
;
494 sigemptyset (&remote_fio_sa
.sa_mask
);
495 remote_fio_sa
.sa_flags
= 0;
496 sigaction (SIGINT
, &remote_fio_sa
, NULL
);
498 signal (SIGINT
, sigint_func
);
503 remote_fileio_sig_exit (void)
505 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
506 sigaction (SIGINT
, &remote_fio_osa
, NULL
);
508 signal (SIGINT
, remote_fio_ofunc
);
513 async_remote_fileio_interrupt (gdb_client_data arg
)
515 deprecated_throw_reason (RETURN_QUIT
);
519 remote_fileio_ctrl_c_signal_handler (int signo
)
521 remote_fileio_sig_set (SIG_IGN
);
522 remote_fio_ctrl_c_flag
= 1;
523 if (!remote_fio_no_longjmp
)
524 gdb_call_async_signal_handler (sigint_fileio_token
, 1);
525 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
529 remote_fileio_reply (int retcode
, int error
)
533 remote_fileio_sig_set (SIG_IGN
);
540 sprintf (buf
+ strlen (buf
), "%x", retcode
);
541 if (error
|| remote_fio_ctrl_c_flag
)
543 if (error
&& remote_fio_ctrl_c_flag
)
544 error
= FILEIO_EINTR
;
550 sprintf (buf
+ strlen (buf
), ",%x", error
);
551 if (remote_fio_ctrl_c_flag
)
554 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
559 remote_fileio_ioerror (void)
561 remote_fileio_reply (-1, FILEIO_EIO
);
565 remote_fileio_badfd (void)
567 remote_fileio_reply (-1, FILEIO_EBADF
);
571 remote_fileio_return_errno (int retcode
)
573 remote_fileio_reply (retcode
, retcode
< 0
574 ? remote_fileio_errno_to_target (errno
) : 0);
578 remote_fileio_return_success (int retcode
)
580 remote_fileio_reply (retcode
, 0);
584 remote_fileio_func_open (char *buf
)
594 /* 1. Parameter: Ptr to pathname / length incl. trailing zero. */
595 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
597 remote_fileio_ioerror ();
600 /* 2. Parameter: open flags */
601 if (remote_fileio_extract_int (&buf
, &num
))
603 remote_fileio_ioerror ();
606 flags
= remote_fileio_oflags_to_host (num
);
607 /* 3. Parameter: open mode */
608 if (remote_fileio_extract_int (&buf
, &num
))
610 remote_fileio_ioerror ();
613 mode
= remote_fileio_mode_to_host (num
, 1);
615 /* Request pathname. */
616 pathname
= alloca (length
);
617 if (target_read_memory (ptrval
, (gdb_byte
*) pathname
, length
) != 0)
619 remote_fileio_ioerror ();
623 /* Check if pathname exists and is not a regular file or directory. If so,
624 return an appropriate error code. Same for trying to open directories
626 if (!stat (pathname
, &st
))
628 if (!S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
630 remote_fileio_reply (-1, FILEIO_ENODEV
);
633 if (S_ISDIR (st
.st_mode
)
634 && ((flags
& O_WRONLY
) == O_WRONLY
|| (flags
& O_RDWR
) == O_RDWR
))
636 remote_fileio_reply (-1, FILEIO_EISDIR
);
641 remote_fio_no_longjmp
= 1;
642 fd
= open (pathname
, flags
, mode
);
645 remote_fileio_return_errno (-1);
649 fd
= remote_fileio_fd_to_targetfd (fd
);
650 remote_fileio_return_success (fd
);
654 remote_fileio_func_close (char *buf
)
659 /* Parameter: file descriptor */
660 if (remote_fileio_extract_int (&buf
, &num
))
662 remote_fileio_ioerror ();
665 fd
= remote_fileio_map_fd ((int) num
);
666 if (fd
== FIO_FD_INVALID
)
668 remote_fileio_badfd ();
672 remote_fio_no_longjmp
= 1;
673 if (fd
!= FIO_FD_CONSOLE_IN
&& fd
!= FIO_FD_CONSOLE_OUT
&& close (fd
))
674 remote_fileio_return_errno (-1);
675 remote_fileio_close_target_fd ((int) num
);
676 remote_fileio_return_success (0);
680 remote_fileio_func_read (char *buf
)
688 off_t old_offset
, new_offset
;
690 /* 1. Parameter: file descriptor */
691 if (remote_fileio_extract_int (&buf
, &target_fd
))
693 remote_fileio_ioerror ();
696 fd
= remote_fileio_map_fd ((int) target_fd
);
697 if (fd
== FIO_FD_INVALID
)
699 remote_fileio_badfd ();
702 /* 2. Parameter: buffer pointer */
703 if (remote_fileio_extract_long (&buf
, &lnum
))
705 remote_fileio_ioerror ();
708 ptrval
= (CORE_ADDR
) lnum
;
709 /* 3. Parameter: buffer length */
710 if (remote_fileio_extract_int (&buf
, &num
))
712 remote_fileio_ioerror ();
715 length
= (size_t) num
;
719 case FIO_FD_CONSOLE_OUT
:
720 remote_fileio_badfd ();
722 case FIO_FD_CONSOLE_IN
:
724 static char *remaining_buf
= NULL
;
725 static int remaining_length
= 0;
727 buffer
= (gdb_byte
*) xmalloc (16384);
730 remote_fio_no_longjmp
= 1;
731 if (remaining_length
> length
)
733 memcpy (buffer
, remaining_buf
, length
);
734 memmove (remaining_buf
, remaining_buf
+ length
,
735 remaining_length
- length
);
736 remaining_length
-= length
;
741 memcpy (buffer
, remaining_buf
, remaining_length
);
742 xfree (remaining_buf
);
743 remaining_buf
= NULL
;
744 ret
= remaining_length
;
749 /* Windows (at least XP and Server 2003) has difficulty
750 with large reads from consoles. If a handle is
751 backed by a real console device, overly large reads
752 from the handle will fail and set errno == ENOMEM.
753 On a Windows Server 2003 system where I tested,
754 reading 26608 bytes from the console was OK, but
755 anything above 26609 bytes would fail. The limit has
756 been observed to vary on different systems. So, we
757 limit this read to something smaller than that - by a
758 safe margin, in case the limit depends on system
759 resources or version. */
760 ret
= ui_file_read (gdb_stdtargin
, (char *) buffer
, 16383);
761 remote_fio_no_longjmp
= 1;
762 if (ret
> 0 && (size_t)ret
> length
)
764 remaining_buf
= (char *) xmalloc (ret
- length
);
765 remaining_length
= ret
- length
;
766 memcpy (remaining_buf
, buffer
+ length
, remaining_length
);
773 buffer
= (gdb_byte
*) xmalloc (length
);
774 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
775 for read() to return -1 even if "some" bytes have been read. It
776 has been corrected in SUSv2 but that doesn't help us much...
777 Therefore a complete solution must check how many bytes have been
778 read on EINTR to return a more reliable value to the target */
779 old_offset
= lseek (fd
, 0, SEEK_CUR
);
780 remote_fio_no_longjmp
= 1;
781 ret
= read (fd
, buffer
, length
);
782 if (ret
< 0 && errno
== EINTR
)
784 new_offset
= lseek (fd
, 0, SEEK_CUR
);
785 /* If some data has been read, return the number of bytes read.
786 The Ctrl-C flag is set in remote_fileio_reply() anyway. */
787 if (old_offset
!= new_offset
)
788 ret
= new_offset
- old_offset
;
795 errno
= target_write_memory (ptrval
, buffer
, ret
);
801 remote_fileio_return_errno (-1);
803 remote_fileio_return_success (ret
);
809 remote_fileio_func_write (char *buf
)
818 /* 1. Parameter: file descriptor */
819 if (remote_fileio_extract_int (&buf
, &target_fd
))
821 remote_fileio_ioerror ();
824 fd
= remote_fileio_map_fd ((int) target_fd
);
825 if (fd
== FIO_FD_INVALID
)
827 remote_fileio_badfd ();
830 /* 2. Parameter: buffer pointer */
831 if (remote_fileio_extract_long (&buf
, &lnum
))
833 remote_fileio_ioerror ();
836 ptrval
= (CORE_ADDR
) lnum
;
837 /* 3. Parameter: buffer length */
838 if (remote_fileio_extract_int (&buf
, &num
))
840 remote_fileio_ioerror ();
843 length
= (size_t) num
;
845 buffer
= (gdb_byte
*) xmalloc (length
);
846 if (target_read_memory (ptrval
, buffer
, length
) != 0)
849 remote_fileio_ioerror ();
853 remote_fio_no_longjmp
= 1;
856 case FIO_FD_CONSOLE_IN
:
857 remote_fileio_badfd ();
860 case FIO_FD_CONSOLE_OUT
:
861 ui_file_write (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
,
862 (char *) buffer
, length
);
863 gdb_flush (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
);
867 ret
= write (fd
, buffer
, length
);
868 if (ret
< 0 && errno
== EACCES
)
869 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a
875 remote_fileio_return_errno (-1);
877 remote_fileio_return_success (ret
);
883 remote_fileio_func_lseek (char *buf
)
890 /* 1. Parameter: file descriptor */
891 if (remote_fileio_extract_int (&buf
, &num
))
893 remote_fileio_ioerror ();
896 fd
= remote_fileio_map_fd ((int) num
);
897 if (fd
== FIO_FD_INVALID
)
899 remote_fileio_badfd ();
902 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
904 remote_fileio_reply (-1, FILEIO_ESPIPE
);
908 /* 2. Parameter: offset */
909 if (remote_fileio_extract_long (&buf
, &lnum
))
911 remote_fileio_ioerror ();
914 offset
= (off_t
) lnum
;
915 /* 3. Parameter: flag */
916 if (remote_fileio_extract_int (&buf
, &num
))
918 remote_fileio_ioerror ();
921 if (remote_fileio_seek_flag_to_host (num
, &flag
))
923 remote_fileio_reply (-1, FILEIO_EINVAL
);
927 remote_fio_no_longjmp
= 1;
928 ret
= lseek (fd
, offset
, flag
);
930 if (ret
== (off_t
) -1)
931 remote_fileio_return_errno (-1);
933 remote_fileio_return_success (ret
);
937 remote_fileio_func_rename (char *buf
)
939 CORE_ADDR old_ptr
, new_ptr
;
940 int old_len
, new_len
;
941 char *oldpath
, *newpath
;
943 struct stat ost
, nst
;
945 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
946 if (remote_fileio_extract_ptr_w_len (&buf
, &old_ptr
, &old_len
))
948 remote_fileio_ioerror ();
952 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
953 if (remote_fileio_extract_ptr_w_len (&buf
, &new_ptr
, &new_len
))
955 remote_fileio_ioerror ();
959 /* Request oldpath using 'm' packet */
960 oldpath
= alloca (old_len
);
961 if (target_read_memory (old_ptr
, (gdb_byte
*) oldpath
, old_len
) != 0)
963 remote_fileio_ioerror ();
967 /* Request newpath using 'm' packet */
968 newpath
= alloca (new_len
);
969 if (target_read_memory (new_ptr
, (gdb_byte
*) newpath
, new_len
) != 0)
971 remote_fileio_ioerror ();
975 /* Only operate on regular files and directories. */
976 of
= stat (oldpath
, &ost
);
977 nf
= stat (newpath
, &nst
);
978 if ((!of
&& !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
979 || (!nf
&& !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
981 remote_fileio_reply (-1, FILEIO_EACCES
);
985 remote_fio_no_longjmp
= 1;
986 ret
= rename (oldpath
, newpath
);
990 /* Special case: newpath is a non-empty directory. Some systems
991 return ENOTEMPTY, some return EEXIST. We coerce that to be
993 if (errno
== ENOTEMPTY
)
996 /* Workaround some Cygwin problems with correct errnos. */
999 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
1001 if (S_ISREG (ost
.st_mode
))
1005 char oldfullpath
[PATH_MAX
];
1006 char newfullpath
[PATH_MAX
];
1009 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, oldpath
, oldfullpath
,
1011 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, newpath
, newfullpath
,
1013 len
= strlen (oldfullpath
);
1014 if (IS_DIR_SEPARATOR (newfullpath
[len
])
1015 && !filename_ncmp (oldfullpath
, newfullpath
, len
))
1024 remote_fileio_return_errno (-1);
1027 remote_fileio_return_success (ret
);
1031 remote_fileio_func_unlink (char *buf
)
1039 /* Parameter: Ptr to pathname / length incl. trailing zero */
1040 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1042 remote_fileio_ioerror ();
1045 /* Request pathname using 'm' packet */
1046 pathname
= alloca (length
);
1047 if (target_read_memory (ptrval
, (gdb_byte
*) pathname
, length
) != 0)
1049 remote_fileio_ioerror ();
1053 /* Only operate on regular files (and directories, which allows to return
1054 the correct return code). */
1055 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1057 remote_fileio_reply (-1, FILEIO_ENODEV
);
1061 remote_fio_no_longjmp
= 1;
1062 ret
= unlink (pathname
);
1065 remote_fileio_return_errno (-1);
1067 remote_fileio_return_success (ret
);
1071 remote_fileio_func_stat (char *buf
)
1073 CORE_ADDR statptr
, nameptr
;
1074 int ret
, namelength
;
1078 struct fio_stat fst
;
1080 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1081 if (remote_fileio_extract_ptr_w_len (&buf
, &nameptr
, &namelength
))
1083 remote_fileio_ioerror ();
1087 /* 2. Parameter: Ptr to struct stat */
1088 if (remote_fileio_extract_long (&buf
, &lnum
))
1090 remote_fileio_ioerror ();
1093 statptr
= (CORE_ADDR
) lnum
;
1095 /* Request pathname using 'm' packet */
1096 pathname
= alloca (namelength
);
1097 if (target_read_memory (nameptr
, (gdb_byte
*) pathname
, namelength
) != 0)
1099 remote_fileio_ioerror ();
1103 remote_fio_no_longjmp
= 1;
1104 ret
= stat (pathname
, &st
);
1108 remote_fileio_return_errno (-1);
1111 /* Only operate on regular files and directories. */
1112 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1114 remote_fileio_reply (-1, FILEIO_EACCES
);
1119 remote_fileio_to_fio_stat (&st
, &fst
);
1120 remote_fileio_to_fio_uint (0, fst
.fst_dev
);
1122 errno
= target_write_memory (statptr
, (gdb_byte
*) &fst
, sizeof fst
);
1125 remote_fileio_return_errno (-1);
1129 remote_fileio_return_success (ret
);
1133 remote_fileio_func_fstat (char *buf
)
1140 struct fio_stat fst
;
1143 /* 1. Parameter: file descriptor */
1144 if (remote_fileio_extract_int (&buf
, &target_fd
))
1146 remote_fileio_ioerror ();
1149 fd
= remote_fileio_map_fd ((int) target_fd
);
1150 if (fd
== FIO_FD_INVALID
)
1152 remote_fileio_badfd ();
1155 /* 2. Parameter: Ptr to struct stat */
1156 if (remote_fileio_extract_long (&buf
, &lnum
))
1158 remote_fileio_ioerror ();
1161 ptrval
= (CORE_ADDR
) lnum
;
1163 remote_fio_no_longjmp
= 1;
1164 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
1166 remote_fileio_to_fio_uint (1, fst
.fst_dev
);
1167 memset (&st
, 0, sizeof (st
));
1168 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
1171 st
.st_uid
= getuid ();
1174 st
.st_gid
= getgid ();
1176 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1177 st
.st_blksize
= 512;
1179 #if HAVE_STRUCT_STAT_ST_BLOCKS
1182 if (!gettimeofday (&tv
, NULL
))
1183 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
1185 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
1189 ret
= fstat (fd
, &st
);
1193 remote_fileio_return_errno (-1);
1198 remote_fileio_to_fio_stat (&st
, &fst
);
1200 errno
= target_write_memory (ptrval
, (gdb_byte
*) &fst
, sizeof fst
);
1203 remote_fileio_return_errno (-1);
1207 remote_fileio_return_success (ret
);
1211 remote_fileio_func_gettimeofday (char *buf
)
1217 struct fio_timeval ftv
;
1219 /* 1. Parameter: struct timeval pointer */
1220 if (remote_fileio_extract_long (&buf
, &lnum
))
1222 remote_fileio_ioerror ();
1225 ptrval
= (CORE_ADDR
) lnum
;
1226 /* 2. Parameter: some pointer value... */
1227 if (remote_fileio_extract_long (&buf
, &lnum
))
1229 remote_fileio_ioerror ();
1232 /* ...which has to be NULL. */
1235 remote_fileio_reply (-1, FILEIO_EINVAL
);
1239 remote_fio_no_longjmp
= 1;
1240 ret
= gettimeofday (&tv
, NULL
);
1244 remote_fileio_return_errno (-1);
1250 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1252 errno
= target_write_memory (ptrval
, (gdb_byte
*) &ftv
, sizeof ftv
);
1255 remote_fileio_return_errno (-1);
1259 remote_fileio_return_success (ret
);
1263 remote_fileio_func_isatty (char *buf
)
1268 /* Parameter: file descriptor */
1269 if (remote_fileio_extract_int (&buf
, &target_fd
))
1271 remote_fileio_ioerror ();
1274 remote_fio_no_longjmp
= 1;
1275 fd
= remote_fileio_map_fd ((int) target_fd
);
1276 remote_fileio_return_success (fd
== FIO_FD_CONSOLE_IN
||
1277 fd
== FIO_FD_CONSOLE_OUT
? 1 : 0);
1281 remote_fileio_func_system (char *buf
)
1285 char *cmdline
= NULL
;
1287 /* Parameter: Ptr to commandline / length incl. trailing zero */
1288 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1290 remote_fileio_ioerror ();
1296 /* Request commandline using 'm' packet */
1297 cmdline
= alloca (length
);
1298 if (target_read_memory (ptrval
, (gdb_byte
*) cmdline
, length
) != 0)
1300 remote_fileio_ioerror ();
1305 /* Check if system(3) has been explicitely allowed using the
1306 `set remote system-call-allowed 1' command. If length is 0,
1307 indicating a NULL parameter to the system call, return zero to
1308 indicate a shell is not available. Otherwise fail with EPERM. */
1309 if (!remote_fio_system_call_allowed
)
1312 remote_fileio_return_success (0);
1314 remote_fileio_reply (-1, FILEIO_EPERM
);
1318 remote_fio_no_longjmp
= 1;
1319 ret
= system (cmdline
);
1322 remote_fileio_return_success (ret
);
1324 remote_fileio_return_errno (-1);
1326 remote_fileio_return_success (WEXITSTATUS (ret
));
1331 void (*func
)(char *);
1332 } remote_fio_func_map
[] = {
1333 { "open", remote_fileio_func_open
},
1334 { "close", remote_fileio_func_close
},
1335 { "read", remote_fileio_func_read
},
1336 { "write", remote_fileio_func_write
},
1337 { "lseek", remote_fileio_func_lseek
},
1338 { "rename", remote_fileio_func_rename
},
1339 { "unlink", remote_fileio_func_unlink
},
1340 { "stat", remote_fileio_func_stat
},
1341 { "fstat", remote_fileio_func_fstat
},
1342 { "gettimeofday", remote_fileio_func_gettimeofday
},
1343 { "isatty", remote_fileio_func_isatty
},
1344 { "system", remote_fileio_func_system
},
1349 do_remote_fileio_request (struct ui_out
*uiout
, void *buf_arg
)
1351 char *buf
= buf_arg
;
1355 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
1357 c
= strchr (++buf
, ',');
1361 c
= strchr (buf
, '\0');
1362 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1363 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1365 if (!remote_fio_func_map
[idx
].name
) /* ERROR: No such function. */
1366 return RETURN_ERROR
;
1367 remote_fio_func_map
[idx
].func (c
);
1371 /* Close any open descriptors, and reinitialize the file mapping. */
1374 remote_fileio_reset (void)
1378 for (ix
= 0; ix
!= remote_fio_data
.fd_map_size
; ix
++)
1380 int fd
= remote_fio_data
.fd_map
[ix
];
1385 if (remote_fio_data
.fd_map
)
1387 xfree (remote_fio_data
.fd_map
);
1388 remote_fio_data
.fd_map
= NULL
;
1389 remote_fio_data
.fd_map_size
= 0;
1393 /* Handle a file I/O request. BUF points to the packet containing the
1394 request. CTRLC_PENDING_P should be nonzero if the target has not
1395 acknowledged the Ctrl-C sent asynchronously earlier. */
1398 remote_fileio_request (char *buf
, int ctrlc_pending_p
)
1402 remote_fileio_sig_init ();
1404 if (ctrlc_pending_p
)
1406 /* If the target hasn't responded to the Ctrl-C sent
1407 asynchronously earlier, take this opportunity to send the
1408 Ctrl-C synchronously. */
1409 remote_fio_ctrl_c_flag
= 1;
1410 remote_fio_no_longjmp
= 0;
1411 remote_fileio_reply (-1, FILEIO_EINTR
);
1415 remote_fio_ctrl_c_flag
= 0;
1416 remote_fio_no_longjmp
= 0;
1418 ex
= catch_exceptions (current_uiout
,
1419 do_remote_fileio_request
, (void *)buf
,
1424 remote_fileio_reply (-1, FILEIO_ENOSYS
);
1427 remote_fileio_reply (-1, FILEIO_EINTR
);
1434 remote_fileio_sig_exit ();
1438 set_system_call_allowed (char *args
, int from_tty
)
1443 int val
= strtoul (args
, &arg_end
, 10);
1445 if (*args
&& *arg_end
== '\0')
1447 remote_fio_system_call_allowed
= !!val
;
1451 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1455 show_system_call_allowed (char *args
, int from_tty
)
1458 error (_("Garbage after \"show remote "
1459 "system-call-allowed\" command: `%s'"), args
);
1460 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1461 remote_fio_system_call_allowed
? "" : "not ");
1465 initialize_remote_fileio (struct cmd_list_element
*remote_set_cmdlist
,
1466 struct cmd_list_element
*remote_show_cmdlist
)
1468 sigint_fileio_token
=
1469 create_async_signal_handler (async_remote_fileio_interrupt
, NULL
);
1471 add_cmd ("system-call-allowed", no_class
,
1472 set_system_call_allowed
,
1473 _("Set if the host system(3) call is allowed for the target."),
1474 &remote_set_cmdlist
);
1475 add_cmd ("system-call-allowed", no_class
,
1476 show_system_call_allowed
,
1477 _("Show if the host system(3) call is allowed for the target."),
1478 &remote_show_cmdlist
);