1 /* Remote File-I/O communications
3 Copyright (C) 2003, 2005, 2006, 2007, 2008, 2009, 2010
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"
37 #include <sys/cygwin.h> /* For cygwin_conv_to_full_posix_path. */
46 #define FIO_FD_INVALID -1
47 #define FIO_FD_CONSOLE_IN -2
48 #define FIO_FD_CONSOLE_OUT -3
50 static int remote_fio_system_call_allowed
= 0;
52 static struct async_signal_handler
*sigint_fileio_token
;
55 remote_fileio_init_fd_map (void)
59 if (!remote_fio_data
.fd_map
)
61 remote_fio_data
.fd_map
= (int *) xmalloc (10 * sizeof (int));
62 remote_fio_data
.fd_map_size
= 10;
63 remote_fio_data
.fd_map
[0] = FIO_FD_CONSOLE_IN
;
64 remote_fio_data
.fd_map
[1] = FIO_FD_CONSOLE_OUT
;
65 remote_fio_data
.fd_map
[2] = FIO_FD_CONSOLE_OUT
;
66 for (i
= 3; i
< 10; ++i
)
67 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
73 remote_fileio_resize_fd_map (void)
75 int i
= remote_fio_data
.fd_map_size
;
77 if (!remote_fio_data
.fd_map
)
78 return remote_fileio_init_fd_map ();
79 remote_fio_data
.fd_map_size
+= 10;
80 remote_fio_data
.fd_map
=
81 (int *) xrealloc (remote_fio_data
.fd_map
,
82 remote_fio_data
.fd_map_size
* sizeof (int));
83 for (; i
< remote_fio_data
.fd_map_size
; i
++)
84 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
85 return remote_fio_data
.fd_map_size
- 10;
89 remote_fileio_next_free_fd (void)
93 for (i
= 0; i
< remote_fio_data
.fd_map_size
; ++i
)
94 if (remote_fio_data
.fd_map
[i
] == FIO_FD_INVALID
)
96 return remote_fileio_resize_fd_map ();
100 remote_fileio_fd_to_targetfd (int fd
)
102 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_mode_to_target (mode_t mode
)
202 tmode
|= FILEIO_S_IFREG
;
204 tmode
|= FILEIO_S_IFDIR
;
206 tmode
|= FILEIO_S_IFCHR
;
208 tmode
|= FILEIO_S_IRUSR
;
210 tmode
|= FILEIO_S_IWUSR
;
212 tmode
|= FILEIO_S_IXUSR
;
215 tmode
|= FILEIO_S_IRGRP
;
219 tmode
|= FILEIO_S_IWGRP
;
223 tmode
|= FILEIO_S_IXGRP
;
226 tmode
|= FILEIO_S_IROTH
;
229 tmode
|= FILEIO_S_IWOTH
;
233 tmode
|= FILEIO_S_IXOTH
;
239 remote_fileio_errno_to_target (int error
)
246 return FILEIO_ENOENT
;
254 return FILEIO_EACCES
;
256 return FILEIO_EFAULT
;
260 return FILEIO_EEXIST
;
262 return FILEIO_ENODEV
;
264 return FILEIO_ENOTDIR
;
266 return FILEIO_EISDIR
;
268 return FILEIO_EINVAL
;
270 return FILEIO_ENFILE
;
272 return FILEIO_EMFILE
;
276 return FILEIO_ENOSPC
;
278 return FILEIO_ESPIPE
;
282 return FILEIO_ENOSYS
;
284 return FILEIO_ENAMETOOLONG
;
286 return FILEIO_EUNKNOWN
;
290 remote_fileio_seek_flag_to_host (long num
, int *flag
)
296 case FILEIO_SEEK_SET
:
299 case FILEIO_SEEK_CUR
:
302 case FILEIO_SEEK_END
:
312 remote_fileio_extract_long (char **buf
, LONGEST
*retlong
)
317 if (!buf
|| !*buf
|| !**buf
|| !retlong
)
319 c
= strchr (*buf
, ',');
323 c
= strchr (*buf
, '\0');
324 while (strchr ("+-", **buf
))
330 for (*retlong
= 0; **buf
; ++*buf
)
333 if (**buf
>= '0' && **buf
<= '9')
334 *retlong
+= **buf
- '0';
335 else if (**buf
>= 'a' && **buf
<= 'f')
336 *retlong
+= **buf
- 'a' + 10;
337 else if (**buf
>= 'A' && **buf
<= 'F')
338 *retlong
+= **buf
- 'A' + 10;
348 remote_fileio_extract_int (char **buf
, long *retint
)
355 ret
= remote_fileio_extract_long (buf
, &retlong
);
357 *retint
= (long) retlong
;
362 remote_fileio_extract_ptr_w_len (char **buf
, CORE_ADDR
*ptrval
, int *length
)
367 if (!buf
|| !*buf
|| !**buf
|| !ptrval
|| !length
)
369 c
= strchr (*buf
, '/');
373 if (remote_fileio_extract_long (buf
, &retlong
))
375 *ptrval
= (CORE_ADDR
) retlong
;
377 if (remote_fileio_extract_long (buf
, &retlong
))
379 *length
= (int) retlong
;
383 /* Convert to big endian */
385 remote_fileio_to_be (LONGEST num
, char *buf
, int bytes
)
389 for (i
= 0; i
< bytes
; ++i
)
390 buf
[i
] = (num
>> (8 * (bytes
- i
- 1))) & 0xff;
394 remote_fileio_to_fio_uint (long num
, fio_uint_t fnum
)
396 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
400 remote_fileio_to_fio_mode (mode_t num
, fio_mode_t fnum
)
402 remote_fileio_to_be (remote_fileio_mode_to_target(num
), (char *) fnum
, 4);
406 remote_fileio_to_fio_time (time_t num
, fio_time_t fnum
)
408 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
412 remote_fileio_to_fio_long (LONGEST num
, fio_long_t fnum
)
414 remote_fileio_to_be (num
, (char *) fnum
, 8);
418 remote_fileio_to_fio_ulong (LONGEST num
, fio_ulong_t fnum
)
420 remote_fileio_to_be (num
, (char *) fnum
, 8);
424 remote_fileio_to_fio_stat (struct stat
*st
, struct fio_stat
*fst
)
428 /* `st_dev' is set in the calling function */
429 remote_fileio_to_fio_uint ((long) st
->st_ino
, fst
->fst_ino
);
430 remote_fileio_to_fio_mode (st
->st_mode
, fst
->fst_mode
);
431 remote_fileio_to_fio_uint ((long) st
->st_nlink
, fst
->fst_nlink
);
432 remote_fileio_to_fio_uint ((long) st
->st_uid
, fst
->fst_uid
);
433 remote_fileio_to_fio_uint ((long) st
->st_gid
, fst
->fst_gid
);
434 remote_fileio_to_fio_uint ((long) st
->st_rdev
, fst
->fst_rdev
);
435 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_size
, fst
->fst_size
);
436 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
437 blksize
= st
->st_blksize
;
441 remote_fileio_to_fio_ulong (blksize
, fst
->fst_blksize
);
442 #if HAVE_STRUCT_STAT_ST_BLOCKS
443 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_blocks
, fst
->fst_blocks
);
445 /* FIXME: This is correct for DJGPP, but other systems that don't
446 have st_blocks, if any, might prefer 512 instead of st_blksize.
447 (eliz, 30-12-2003) */
448 remote_fileio_to_fio_ulong (((LONGEST
) st
->st_size
+ blksize
- 1)
452 remote_fileio_to_fio_time (st
->st_atime
, fst
->fst_atime
);
453 remote_fileio_to_fio_time (st
->st_mtime
, fst
->fst_mtime
);
454 remote_fileio_to_fio_time (st
->st_ctime
, fst
->fst_ctime
);
458 remote_fileio_to_fio_timeval (struct timeval
*tv
, struct fio_timeval
*ftv
)
460 remote_fileio_to_fio_time (tv
->tv_sec
, ftv
->ftv_sec
);
461 remote_fileio_to_fio_long (tv
->tv_usec
, ftv
->ftv_usec
);
464 static int remote_fio_ctrl_c_flag
= 0;
465 static int remote_fio_no_longjmp
= 0;
467 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
468 static struct sigaction remote_fio_sa
;
469 static struct sigaction remote_fio_osa
;
471 static void (*remote_fio_ofunc
)(int);
475 remote_fileio_sig_init (void)
477 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
478 remote_fio_sa
.sa_handler
= SIG_IGN
;
479 sigemptyset (&remote_fio_sa
.sa_mask
);
480 remote_fio_sa
.sa_flags
= 0;
481 sigaction (SIGINT
, &remote_fio_sa
, &remote_fio_osa
);
483 remote_fio_ofunc
= signal (SIGINT
, SIG_IGN
);
488 remote_fileio_sig_set (void (*sigint_func
)(int))
490 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
491 remote_fio_sa
.sa_handler
= sigint_func
;
492 sigemptyset (&remote_fio_sa
.sa_mask
);
493 remote_fio_sa
.sa_flags
= 0;
494 sigaction (SIGINT
, &remote_fio_sa
, NULL
);
496 signal (SIGINT
, sigint_func
);
501 remote_fileio_sig_exit (void)
503 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
504 sigaction (SIGINT
, &remote_fio_osa
, NULL
);
506 signal (SIGINT
, remote_fio_ofunc
);
511 async_remote_fileio_interrupt (gdb_client_data arg
)
513 deprecated_throw_reason (RETURN_QUIT
);
517 remote_fileio_ctrl_c_signal_handler (int signo
)
519 remote_fileio_sig_set (SIG_IGN
);
520 remote_fio_ctrl_c_flag
= 1;
521 if (!remote_fio_no_longjmp
)
522 gdb_call_async_signal_handler (sigint_fileio_token
, 1);
523 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
527 remote_fileio_reply (int retcode
, int error
)
531 remote_fileio_sig_set (SIG_IGN
);
538 sprintf (buf
+ strlen (buf
), "%x", retcode
);
539 if (error
|| remote_fio_ctrl_c_flag
)
541 if (error
&& remote_fio_ctrl_c_flag
)
542 error
= FILEIO_EINTR
;
548 sprintf (buf
+ strlen (buf
), ",%x", error
);
549 if (remote_fio_ctrl_c_flag
)
552 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
557 remote_fileio_ioerror (void)
559 remote_fileio_reply (-1, FILEIO_EIO
);
563 remote_fileio_badfd (void)
565 remote_fileio_reply (-1, FILEIO_EBADF
);
569 remote_fileio_return_errno (int retcode
)
571 remote_fileio_reply (retcode
,
572 retcode
< 0 ? remote_fileio_errno_to_target (errno
) : 0);
576 remote_fileio_return_success (int retcode
)
578 remote_fileio_reply (retcode
, 0);
581 /* Wrapper function for remote_write_bytes() which has the disadvantage to
582 write only one packet, regardless of the requested number of bytes to
583 transfer. This wrapper calls remote_write_bytes() as often as needed. */
585 remote_fileio_write_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
587 int ret
= 0, written
;
589 while (len
> 0 && (written
= remote_write_bytes (memaddr
, myaddr
, len
)) > 0)
600 remote_fileio_func_open (char *buf
)
603 int length
, retlength
;
610 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
611 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
613 remote_fileio_ioerror ();
616 /* 2. Parameter: open flags */
617 if (remote_fileio_extract_int (&buf
, &num
))
619 remote_fileio_ioerror ();
622 flags
= remote_fileio_oflags_to_host (num
);
623 /* 3. Parameter: open mode */
624 if (remote_fileio_extract_int (&buf
, &num
))
626 remote_fileio_ioerror ();
629 mode
= remote_fileio_mode_to_host (num
, 1);
631 /* Request pathname using 'm' packet */
632 pathname
= alloca (length
);
633 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
634 if (retlength
!= length
)
636 remote_fileio_ioerror ();
640 /* Check if pathname exists and is not a regular file or directory. If so,
641 return an appropriate error code. Same for trying to open directories
643 if (!stat (pathname
, &st
))
645 if (!S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
647 remote_fileio_reply (-1, FILEIO_ENODEV
);
650 if (S_ISDIR (st
.st_mode
)
651 && ((flags
& O_WRONLY
) == O_WRONLY
|| (flags
& O_RDWR
) == O_RDWR
))
653 remote_fileio_reply (-1, FILEIO_EISDIR
);
658 remote_fio_no_longjmp
= 1;
659 fd
= open (pathname
, flags
, mode
);
662 remote_fileio_return_errno (-1);
666 fd
= remote_fileio_fd_to_targetfd (fd
);
667 remote_fileio_return_success (fd
);
671 remote_fileio_func_close (char *buf
)
676 /* Parameter: file descriptor */
677 if (remote_fileio_extract_int (&buf
, &num
))
679 remote_fileio_ioerror ();
682 fd
= remote_fileio_map_fd ((int) num
);
683 if (fd
== FIO_FD_INVALID
)
685 remote_fileio_badfd ();
689 remote_fio_no_longjmp
= 1;
690 if (fd
!= FIO_FD_CONSOLE_IN
&& fd
!= FIO_FD_CONSOLE_OUT
&& close (fd
))
691 remote_fileio_return_errno (-1);
692 remote_fileio_close_target_fd ((int) num
);
693 remote_fileio_return_success (0);
697 remote_fileio_func_read (char *buf
)
702 int fd
, ret
, retlength
;
705 off_t old_offset
, new_offset
;
707 /* 1. Parameter: file descriptor */
708 if (remote_fileio_extract_int (&buf
, &target_fd
))
710 remote_fileio_ioerror ();
713 fd
= remote_fileio_map_fd ((int) target_fd
);
714 if (fd
== FIO_FD_INVALID
)
716 remote_fileio_badfd ();
719 /* 2. Parameter: buffer pointer */
720 if (remote_fileio_extract_long (&buf
, &lnum
))
722 remote_fileio_ioerror ();
725 ptrval
= (CORE_ADDR
) lnum
;
726 /* 3. Parameter: buffer length */
727 if (remote_fileio_extract_int (&buf
, &num
))
729 remote_fileio_ioerror ();
732 length
= (size_t) num
;
736 case FIO_FD_CONSOLE_OUT
:
737 remote_fileio_badfd ();
739 case FIO_FD_CONSOLE_IN
:
741 static char *remaining_buf
= NULL
;
742 static int remaining_length
= 0;
744 buffer
= (gdb_byte
*) xmalloc (16384);
747 remote_fio_no_longjmp
= 1;
748 if (remaining_length
> length
)
750 memcpy (buffer
, remaining_buf
, length
);
751 memmove (remaining_buf
, remaining_buf
+ length
,
752 remaining_length
- length
);
753 remaining_length
-= length
;
758 memcpy (buffer
, remaining_buf
, remaining_length
);
759 xfree (remaining_buf
);
760 remaining_buf
= NULL
;
761 ret
= remaining_length
;
766 /* Windows (at least XP and Server 2003) has difficulty
767 with large reads from consoles. If a handle is
768 backed by a real console device, overly large reads
769 from the handle will fail and set errno == ENOMEM.
770 On a Windows Server 2003 system where I tested,
771 reading 26608 bytes from the console was OK, but
772 anything above 26609 bytes would fail. The limit has
773 been observed to vary on different systems. So, we
774 limit this read to something smaller than that - by a
775 safe margin, in case the limit depends on system
776 resources or version. */
777 ret
= ui_file_read (gdb_stdtargin
, (char *) buffer
, 16383);
778 remote_fio_no_longjmp
= 1;
779 if (ret
> 0 && (size_t)ret
> length
)
781 remaining_buf
= (char *) xmalloc (ret
- length
);
782 remaining_length
= ret
- length
;
783 memcpy (remaining_buf
, buffer
+ length
, remaining_length
);
790 buffer
= (gdb_byte
*) xmalloc (length
);
791 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
792 for read() to return -1 even if "some" bytes have been read. It
793 has been corrected in SUSv2 but that doesn't help us much...
794 Therefore a complete solution must check how many bytes have been
795 read on EINTR to return a more reliable value to the target */
796 old_offset
= lseek (fd
, 0, SEEK_CUR
);
797 remote_fio_no_longjmp
= 1;
798 ret
= read (fd
, buffer
, length
);
799 if (ret
< 0 && errno
== EINTR
)
801 new_offset
= lseek (fd
, 0, SEEK_CUR
);
802 /* If some data has been read, return the number of bytes read.
803 The Ctrl-C flag is set in remote_fileio_reply() anyway */
804 if (old_offset
!= new_offset
)
805 ret
= new_offset
- old_offset
;
812 retlength
= remote_fileio_write_bytes (ptrval
, buffer
, ret
);
813 if (retlength
!= ret
)
814 ret
= -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
818 remote_fileio_return_errno (-1);
820 remote_fileio_return_success (ret
);
826 remote_fileio_func_write (char *buf
)
831 int fd
, ret
, retlength
;
835 /* 1. Parameter: file descriptor */
836 if (remote_fileio_extract_int (&buf
, &target_fd
))
838 remote_fileio_ioerror ();
841 fd
= remote_fileio_map_fd ((int) target_fd
);
842 if (fd
== FIO_FD_INVALID
)
844 remote_fileio_badfd ();
847 /* 2. Parameter: buffer pointer */
848 if (remote_fileio_extract_long (&buf
, &lnum
))
850 remote_fileio_ioerror ();
853 ptrval
= (CORE_ADDR
) lnum
;
854 /* 3. Parameter: buffer length */
855 if (remote_fileio_extract_int (&buf
, &num
))
857 remote_fileio_ioerror ();
860 length
= (size_t) num
;
862 buffer
= (gdb_byte
*) xmalloc (length
);
863 retlength
= remote_read_bytes (ptrval
, buffer
, length
);
864 if (retlength
!= length
)
867 remote_fileio_ioerror ();
871 remote_fio_no_longjmp
= 1;
874 case FIO_FD_CONSOLE_IN
:
875 remote_fileio_badfd ();
878 case FIO_FD_CONSOLE_OUT
:
879 ui_file_write (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
,
880 (char *) buffer
, length
);
881 gdb_flush (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
);
885 ret
= write (fd
, buffer
, length
);
886 if (ret
< 0 && errno
== EACCES
)
887 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a R/O file.*/
892 remote_fileio_return_errno (-1);
894 remote_fileio_return_success (ret
);
900 remote_fileio_func_lseek (char *buf
)
907 /* 1. Parameter: file descriptor */
908 if (remote_fileio_extract_int (&buf
, &num
))
910 remote_fileio_ioerror ();
913 fd
= remote_fileio_map_fd ((int) num
);
914 if (fd
== FIO_FD_INVALID
)
916 remote_fileio_badfd ();
919 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
921 remote_fileio_reply (-1, FILEIO_ESPIPE
);
925 /* 2. Parameter: offset */
926 if (remote_fileio_extract_long (&buf
, &lnum
))
928 remote_fileio_ioerror ();
931 offset
= (off_t
) lnum
;
932 /* 3. Parameter: flag */
933 if (remote_fileio_extract_int (&buf
, &num
))
935 remote_fileio_ioerror ();
938 if (remote_fileio_seek_flag_to_host (num
, &flag
))
940 remote_fileio_reply (-1, FILEIO_EINVAL
);
944 remote_fio_no_longjmp
= 1;
945 ret
= lseek (fd
, offset
, flag
);
947 if (ret
== (off_t
) -1)
948 remote_fileio_return_errno (-1);
950 remote_fileio_return_success (ret
);
954 remote_fileio_func_rename (char *buf
)
956 CORE_ADDR old_ptr
, new_ptr
;
957 int old_len
, new_len
, retlength
;
958 char *oldpath
, *newpath
;
960 struct stat ost
, nst
;
962 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
963 if (remote_fileio_extract_ptr_w_len (&buf
, &old_ptr
, &old_len
))
965 remote_fileio_ioerror ();
969 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
970 if (remote_fileio_extract_ptr_w_len (&buf
, &new_ptr
, &new_len
))
972 remote_fileio_ioerror ();
976 /* Request oldpath using 'm' packet */
977 oldpath
= alloca (old_len
);
978 retlength
= remote_read_bytes (old_ptr
, (gdb_byte
*) oldpath
, old_len
);
979 if (retlength
!= old_len
)
981 remote_fileio_ioerror ();
985 /* Request newpath using 'm' packet */
986 newpath
= alloca (new_len
);
987 retlength
= remote_read_bytes (new_ptr
, (gdb_byte
*) newpath
, new_len
);
988 if (retlength
!= new_len
)
990 remote_fileio_ioerror ();
994 /* Only operate on regular files and directories */
995 of
= stat (oldpath
, &ost
);
996 nf
= stat (newpath
, &nst
);
997 if ((!of
&& !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
998 || (!nf
&& !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
1000 remote_fileio_reply (-1, FILEIO_EACCES
);
1004 remote_fio_no_longjmp
= 1;
1005 ret
= rename (oldpath
, newpath
);
1009 /* Special case: newpath is a non-empty directory. Some systems
1010 return ENOTEMPTY, some return EEXIST. We coerce that to be
1012 if (errno
== ENOTEMPTY
)
1015 /* Workaround some Cygwin problems with correct errnos. */
1016 if (errno
== EACCES
)
1018 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
1020 if (S_ISREG (ost
.st_mode
))
1024 char oldfullpath
[PATH_MAX
];
1025 char newfullpath
[PATH_MAX
];
1028 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, oldpath
, oldfullpath
,
1030 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, newpath
, newfullpath
,
1032 len
= strlen (oldfullpath
);
1033 if (newfullpath
[len
] == '/'
1034 && !strncmp (oldfullpath
, newfullpath
, len
))
1043 remote_fileio_return_errno (-1);
1046 remote_fileio_return_success (ret
);
1050 remote_fileio_func_unlink (char *buf
)
1053 int length
, retlength
;
1058 /* Parameter: Ptr to pathname / length incl. trailing zero */
1059 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1061 remote_fileio_ioerror ();
1064 /* Request pathname using 'm' packet */
1065 pathname
= alloca (length
);
1066 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
1067 if (retlength
!= length
)
1069 remote_fileio_ioerror ();
1073 /* Only operate on regular files (and directories, which allows to return
1074 the correct return code) */
1075 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1077 remote_fileio_reply (-1, FILEIO_ENODEV
);
1081 remote_fio_no_longjmp
= 1;
1082 ret
= unlink (pathname
);
1085 remote_fileio_return_errno (-1);
1087 remote_fileio_return_success (ret
);
1091 remote_fileio_func_stat (char *buf
)
1093 CORE_ADDR statptr
, nameptr
;
1094 int ret
, namelength
, retlength
;
1098 struct fio_stat fst
;
1100 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1101 if (remote_fileio_extract_ptr_w_len (&buf
, &nameptr
, &namelength
))
1103 remote_fileio_ioerror ();
1107 /* 2. Parameter: Ptr to struct stat */
1108 if (remote_fileio_extract_long (&buf
, &lnum
))
1110 remote_fileio_ioerror ();
1113 statptr
= (CORE_ADDR
) lnum
;
1115 /* Request pathname using 'm' packet */
1116 pathname
= alloca (namelength
);
1117 retlength
= remote_read_bytes (nameptr
, (gdb_byte
*) pathname
, namelength
);
1118 if (retlength
!= namelength
)
1120 remote_fileio_ioerror ();
1124 remote_fio_no_longjmp
= 1;
1125 ret
= stat (pathname
, &st
);
1129 remote_fileio_return_errno (-1);
1132 /* Only operate on regular files and directories */
1133 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1135 remote_fileio_reply (-1, FILEIO_EACCES
);
1140 remote_fileio_to_fio_stat (&st
, &fst
);
1141 remote_fileio_to_fio_uint (0, fst
.fst_dev
);
1143 retlength
= remote_fileio_write_bytes (statptr
,
1144 (gdb_byte
*) &fst
, sizeof fst
);
1145 if (retlength
!= sizeof fst
)
1147 remote_fileio_return_errno (-1);
1151 remote_fileio_return_success (ret
);
1155 remote_fileio_func_fstat (char *buf
)
1158 int fd
, ret
, retlength
;
1162 struct fio_stat fst
;
1165 /* 1. Parameter: file descriptor */
1166 if (remote_fileio_extract_int (&buf
, &target_fd
))
1168 remote_fileio_ioerror ();
1171 fd
= remote_fileio_map_fd ((int) target_fd
);
1172 if (fd
== FIO_FD_INVALID
)
1174 remote_fileio_badfd ();
1177 /* 2. Parameter: Ptr to struct stat */
1178 if (remote_fileio_extract_long (&buf
, &lnum
))
1180 remote_fileio_ioerror ();
1183 ptrval
= (CORE_ADDR
) lnum
;
1185 remote_fio_no_longjmp
= 1;
1186 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
1188 remote_fileio_to_fio_uint (1, fst
.fst_dev
);
1189 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
1192 st
.st_uid
= getuid ();
1197 st
.st_gid
= getgid ();
1203 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1204 st
.st_blksize
= 512;
1206 #if HAVE_STRUCT_STAT_ST_BLOCKS
1209 if (!gettimeofday (&tv
, NULL
))
1210 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
1212 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
1216 ret
= fstat (fd
, &st
);
1220 remote_fileio_return_errno (-1);
1225 remote_fileio_to_fio_stat (&st
, &fst
);
1227 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &fst
, sizeof fst
);
1228 if (retlength
!= sizeof fst
)
1230 remote_fileio_return_errno (-1);
1234 remote_fileio_return_success (ret
);
1238 remote_fileio_func_gettimeofday (char *buf
)
1244 struct fio_timeval ftv
;
1246 /* 1. Parameter: struct timeval pointer */
1247 if (remote_fileio_extract_long (&buf
, &lnum
))
1249 remote_fileio_ioerror ();
1252 ptrval
= (CORE_ADDR
) lnum
;
1253 /* 2. Parameter: some pointer value... */
1254 if (remote_fileio_extract_long (&buf
, &lnum
))
1256 remote_fileio_ioerror ();
1259 /* ...which has to be NULL */
1262 remote_fileio_reply (-1, FILEIO_EINVAL
);
1266 remote_fio_no_longjmp
= 1;
1267 ret
= gettimeofday (&tv
, NULL
);
1271 remote_fileio_return_errno (-1);
1277 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1279 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &ftv
, sizeof ftv
);
1280 if (retlength
!= sizeof ftv
)
1282 remote_fileio_return_errno (-1);
1286 remote_fileio_return_success (ret
);
1290 remote_fileio_func_isatty (char *buf
)
1295 /* Parameter: file descriptor */
1296 if (remote_fileio_extract_int (&buf
, &target_fd
))
1298 remote_fileio_ioerror ();
1301 remote_fio_no_longjmp
= 1;
1302 fd
= remote_fileio_map_fd ((int) target_fd
);
1303 remote_fileio_return_success (fd
== FIO_FD_CONSOLE_IN
||
1304 fd
== FIO_FD_CONSOLE_OUT
? 1 : 0);
1308 remote_fileio_func_system (char *buf
)
1311 int ret
, length
, retlength
;
1312 char *cmdline
= NULL
;
1314 /* Parameter: Ptr to commandline / length incl. trailing zero */
1315 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1317 remote_fileio_ioerror ();
1323 /* Request commandline using 'm' packet */
1324 cmdline
= alloca (length
);
1325 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) cmdline
, length
);
1326 if (retlength
!= length
)
1328 remote_fileio_ioerror ();
1333 /* Check if system(3) has been explicitely allowed using the
1334 `set remote system-call-allowed 1' command. If length is 0,
1335 indicating a NULL parameter to the system call, return zero to
1336 indicate a shell is not available. Otherwise fail with EPERM. */
1337 if (!remote_fio_system_call_allowed
)
1340 remote_fileio_return_success (0);
1342 remote_fileio_reply (-1, FILEIO_EPERM
);
1346 remote_fio_no_longjmp
= 1;
1347 ret
= system (cmdline
);
1350 remote_fileio_return_success (ret
);
1352 remote_fileio_return_errno (-1);
1354 remote_fileio_return_success (WEXITSTATUS (ret
));
1359 void (*func
)(char *);
1360 } remote_fio_func_map
[] = {
1361 { "open", remote_fileio_func_open
},
1362 { "close", remote_fileio_func_close
},
1363 { "read", remote_fileio_func_read
},
1364 { "write", remote_fileio_func_write
},
1365 { "lseek", remote_fileio_func_lseek
},
1366 { "rename", remote_fileio_func_rename
},
1367 { "unlink", remote_fileio_func_unlink
},
1368 { "stat", remote_fileio_func_stat
},
1369 { "fstat", remote_fileio_func_fstat
},
1370 { "gettimeofday", remote_fileio_func_gettimeofday
},
1371 { "isatty", remote_fileio_func_isatty
},
1372 { "system", remote_fileio_func_system
},
1377 do_remote_fileio_request (struct ui_out
*uiout
, void *buf_arg
)
1379 char *buf
= buf_arg
;
1383 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
1385 c
= strchr (++buf
, ',');
1389 c
= strchr (buf
, '\0');
1390 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1391 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1393 if (!remote_fio_func_map
[idx
].name
) /* ERROR: No such function. */
1394 return RETURN_ERROR
;
1395 remote_fio_func_map
[idx
].func (c
);
1399 /* Close any open descriptors, and reinitialize the file mapping. */
1402 remote_fileio_reset (void)
1406 for (ix
= 0; ix
!= remote_fio_data
.fd_map_size
; ix
++)
1408 int fd
= remote_fio_data
.fd_map
[ix
];
1413 if (remote_fio_data
.fd_map
)
1415 xfree (remote_fio_data
.fd_map
);
1416 remote_fio_data
.fd_map
= NULL
;
1417 remote_fio_data
.fd_map_size
= 0;
1421 /* Handle a file I/O request. BUF points to the packet containing the
1422 request. CTRLC_PENDING_P should be nonzero if the target has not
1423 acknowledged the Ctrl-C sent asynchronously earlier. */
1426 remote_fileio_request (char *buf
, int ctrlc_pending_p
)
1430 remote_fileio_sig_init ();
1432 if (ctrlc_pending_p
)
1434 /* If the target hasn't responded to the Ctrl-C sent
1435 asynchronously earlier, take this opportunity to send the
1436 Ctrl-C synchronously. */
1437 remote_fio_ctrl_c_flag
= 1;
1438 remote_fio_no_longjmp
= 0;
1439 remote_fileio_reply (-1, FILEIO_EINTR
);
1443 remote_fio_ctrl_c_flag
= 0;
1444 remote_fio_no_longjmp
= 0;
1446 ex
= catch_exceptions (uiout
, do_remote_fileio_request
, (void *)buf
,
1451 remote_fileio_reply (-1, FILEIO_ENOSYS
);
1454 remote_fileio_reply (-1, FILEIO_EINTR
);
1461 remote_fileio_sig_exit ();
1465 set_system_call_allowed (char *args
, int from_tty
)
1470 int val
= strtoul (args
, &arg_end
, 10);
1471 if (*args
&& *arg_end
== '\0')
1473 remote_fio_system_call_allowed
= !!val
;
1477 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1481 show_system_call_allowed (char *args
, int from_tty
)
1484 error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args
);
1485 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1486 remote_fio_system_call_allowed
? "" : "not ");
1490 initialize_remote_fileio (struct cmd_list_element
*remote_set_cmdlist
,
1491 struct cmd_list_element
*remote_show_cmdlist
)
1493 sigint_fileio_token
=
1494 create_async_signal_handler (async_remote_fileio_interrupt
, NULL
);
1496 add_cmd ("system-call-allowed", no_class
,
1497 set_system_call_allowed
,
1498 _("Set if the host system(3) call is allowed for the target."),
1499 &remote_set_cmdlist
);
1500 add_cmd ("system-call-allowed", no_class
,
1501 show_system_call_allowed
,
1502 _("Show if the host system(3) call is allowed for the target."),
1503 &remote_show_cmdlist
);