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. */
38 #include <cygwin/version.h>
39 #if CYGWIN_VERSION_DLL_MAKE_COMBINED(CYGWIN_VERSION_API_MAJOR,CYGWIN_VERSION_API_MINOR) < 181
40 # define CCP_POSIX_TO_WIN_A 0
41 # define CCP_WIN_A_TO_POSIX 2
42 # define cygwin_conv_path(op, from, to, size) \
43 (op == CCP_WIN_A_TO_POSIX) ? \
44 cygwin_conv_to_full_posix_path (from, to) : \
45 cygwin_conv_to_win32_path (from, to)
55 #define FIO_FD_INVALID -1
56 #define FIO_FD_CONSOLE_IN -2
57 #define FIO_FD_CONSOLE_OUT -3
59 static int remote_fio_system_call_allowed
= 0;
61 static struct async_signal_handler
*sigint_fileio_token
;
64 remote_fileio_init_fd_map (void)
68 if (!remote_fio_data
.fd_map
)
70 remote_fio_data
.fd_map
= (int *) xmalloc (10 * sizeof (int));
71 remote_fio_data
.fd_map_size
= 10;
72 remote_fio_data
.fd_map
[0] = FIO_FD_CONSOLE_IN
;
73 remote_fio_data
.fd_map
[1] = FIO_FD_CONSOLE_OUT
;
74 remote_fio_data
.fd_map
[2] = FIO_FD_CONSOLE_OUT
;
75 for (i
= 3; i
< 10; ++i
)
76 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
82 remote_fileio_resize_fd_map (void)
84 int i
= remote_fio_data
.fd_map_size
;
86 if (!remote_fio_data
.fd_map
)
87 return remote_fileio_init_fd_map ();
88 remote_fio_data
.fd_map_size
+= 10;
89 remote_fio_data
.fd_map
=
90 (int *) xrealloc (remote_fio_data
.fd_map
,
91 remote_fio_data
.fd_map_size
* sizeof (int));
92 for (; i
< remote_fio_data
.fd_map_size
; i
++)
93 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
94 return remote_fio_data
.fd_map_size
- 10;
98 remote_fileio_next_free_fd (void)
102 for (i
= 0; i
< remote_fio_data
.fd_map_size
; ++i
)
103 if (remote_fio_data
.fd_map
[i
] == FIO_FD_INVALID
)
105 return remote_fileio_resize_fd_map ();
109 remote_fileio_fd_to_targetfd (int fd
)
111 int target_fd
= remote_fileio_next_free_fd ();
112 remote_fio_data
.fd_map
[target_fd
] = fd
;
117 remote_fileio_map_fd (int target_fd
)
119 remote_fileio_init_fd_map ();
120 if (target_fd
< 0 || target_fd
>= remote_fio_data
.fd_map_size
)
121 return FIO_FD_INVALID
;
122 return remote_fio_data
.fd_map
[target_fd
];
126 remote_fileio_close_target_fd (int target_fd
)
128 remote_fileio_init_fd_map ();
129 if (target_fd
>= 0 && target_fd
< remote_fio_data
.fd_map_size
)
130 remote_fio_data
.fd_map
[target_fd
] = FIO_FD_INVALID
;
134 remote_fileio_oflags_to_host (long flags
)
138 if (flags
& FILEIO_O_CREAT
)
140 if (flags
& FILEIO_O_EXCL
)
142 if (flags
& FILEIO_O_TRUNC
)
144 if (flags
& FILEIO_O_APPEND
)
146 if (flags
& FILEIO_O_RDONLY
)
148 if (flags
& FILEIO_O_WRONLY
)
150 if (flags
& FILEIO_O_RDWR
)
152 /* On systems supporting binary and text mode, always open files in
161 remote_fileio_mode_to_host (long mode
, int open_call
)
167 if (mode
& FILEIO_S_IFREG
)
169 if (mode
& FILEIO_S_IFDIR
)
171 if (mode
& FILEIO_S_IFCHR
)
174 if (mode
& FILEIO_S_IRUSR
)
176 if (mode
& FILEIO_S_IWUSR
)
178 if (mode
& FILEIO_S_IXUSR
)
181 if (mode
& FILEIO_S_IRGRP
)
185 if (mode
& FILEIO_S_IWGRP
)
189 if (mode
& FILEIO_S_IXGRP
)
192 if (mode
& FILEIO_S_IROTH
)
195 if (mode
& FILEIO_S_IWOTH
)
199 if (mode
& FILEIO_S_IXOTH
)
206 remote_fileio_mode_to_target (mode_t mode
)
211 tmode
|= FILEIO_S_IFREG
;
213 tmode
|= FILEIO_S_IFDIR
;
215 tmode
|= FILEIO_S_IFCHR
;
217 tmode
|= FILEIO_S_IRUSR
;
219 tmode
|= FILEIO_S_IWUSR
;
221 tmode
|= FILEIO_S_IXUSR
;
224 tmode
|= FILEIO_S_IRGRP
;
228 tmode
|= FILEIO_S_IWGRP
;
232 tmode
|= FILEIO_S_IXGRP
;
235 tmode
|= FILEIO_S_IROTH
;
238 tmode
|= FILEIO_S_IWOTH
;
242 tmode
|= FILEIO_S_IXOTH
;
248 remote_fileio_errno_to_target (int error
)
255 return FILEIO_ENOENT
;
263 return FILEIO_EACCES
;
265 return FILEIO_EFAULT
;
269 return FILEIO_EEXIST
;
271 return FILEIO_ENODEV
;
273 return FILEIO_ENOTDIR
;
275 return FILEIO_EISDIR
;
277 return FILEIO_EINVAL
;
279 return FILEIO_ENFILE
;
281 return FILEIO_EMFILE
;
285 return FILEIO_ENOSPC
;
287 return FILEIO_ESPIPE
;
291 return FILEIO_ENOSYS
;
293 return FILEIO_ENAMETOOLONG
;
295 return FILEIO_EUNKNOWN
;
299 remote_fileio_seek_flag_to_host (long num
, int *flag
)
305 case FILEIO_SEEK_SET
:
308 case FILEIO_SEEK_CUR
:
311 case FILEIO_SEEK_END
:
321 remote_fileio_extract_long (char **buf
, LONGEST
*retlong
)
326 if (!buf
|| !*buf
|| !**buf
|| !retlong
)
328 c
= strchr (*buf
, ',');
332 c
= strchr (*buf
, '\0');
333 while (strchr ("+-", **buf
))
339 for (*retlong
= 0; **buf
; ++*buf
)
342 if (**buf
>= '0' && **buf
<= '9')
343 *retlong
+= **buf
- '0';
344 else if (**buf
>= 'a' && **buf
<= 'f')
345 *retlong
+= **buf
- 'a' + 10;
346 else if (**buf
>= 'A' && **buf
<= 'F')
347 *retlong
+= **buf
- 'A' + 10;
357 remote_fileio_extract_int (char **buf
, long *retint
)
364 ret
= remote_fileio_extract_long (buf
, &retlong
);
366 *retint
= (long) retlong
;
371 remote_fileio_extract_ptr_w_len (char **buf
, CORE_ADDR
*ptrval
, int *length
)
376 if (!buf
|| !*buf
|| !**buf
|| !ptrval
|| !length
)
378 c
= strchr (*buf
, '/');
382 if (remote_fileio_extract_long (buf
, &retlong
))
384 *ptrval
= (CORE_ADDR
) retlong
;
386 if (remote_fileio_extract_long (buf
, &retlong
))
388 *length
= (int) retlong
;
392 /* Convert to big endian */
394 remote_fileio_to_be (LONGEST num
, char *buf
, int bytes
)
398 for (i
= 0; i
< bytes
; ++i
)
399 buf
[i
] = (num
>> (8 * (bytes
- i
- 1))) & 0xff;
403 remote_fileio_to_fio_uint (long num
, fio_uint_t fnum
)
405 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
409 remote_fileio_to_fio_mode (mode_t num
, fio_mode_t fnum
)
411 remote_fileio_to_be (remote_fileio_mode_to_target(num
), (char *) fnum
, 4);
415 remote_fileio_to_fio_time (time_t num
, fio_time_t fnum
)
417 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
421 remote_fileio_to_fio_long (LONGEST num
, fio_long_t fnum
)
423 remote_fileio_to_be (num
, (char *) fnum
, 8);
427 remote_fileio_to_fio_ulong (LONGEST num
, fio_ulong_t fnum
)
429 remote_fileio_to_be (num
, (char *) fnum
, 8);
433 remote_fileio_to_fio_stat (struct stat
*st
, struct fio_stat
*fst
)
437 /* `st_dev' is set in the calling function */
438 remote_fileio_to_fio_uint ((long) st
->st_ino
, fst
->fst_ino
);
439 remote_fileio_to_fio_mode (st
->st_mode
, fst
->fst_mode
);
440 remote_fileio_to_fio_uint ((long) st
->st_nlink
, fst
->fst_nlink
);
441 remote_fileio_to_fio_uint ((long) st
->st_uid
, fst
->fst_uid
);
442 remote_fileio_to_fio_uint ((long) st
->st_gid
, fst
->fst_gid
);
443 remote_fileio_to_fio_uint ((long) st
->st_rdev
, fst
->fst_rdev
);
444 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_size
, fst
->fst_size
);
445 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
446 blksize
= st
->st_blksize
;
450 remote_fileio_to_fio_ulong (blksize
, fst
->fst_blksize
);
451 #if HAVE_STRUCT_STAT_ST_BLOCKS
452 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_blocks
, fst
->fst_blocks
);
454 /* FIXME: This is correct for DJGPP, but other systems that don't
455 have st_blocks, if any, might prefer 512 instead of st_blksize.
456 (eliz, 30-12-2003) */
457 remote_fileio_to_fio_ulong (((LONGEST
) st
->st_size
+ blksize
- 1)
461 remote_fileio_to_fio_time (st
->st_atime
, fst
->fst_atime
);
462 remote_fileio_to_fio_time (st
->st_mtime
, fst
->fst_mtime
);
463 remote_fileio_to_fio_time (st
->st_ctime
, fst
->fst_ctime
);
467 remote_fileio_to_fio_timeval (struct timeval
*tv
, struct fio_timeval
*ftv
)
469 remote_fileio_to_fio_time (tv
->tv_sec
, ftv
->ftv_sec
);
470 remote_fileio_to_fio_long (tv
->tv_usec
, ftv
->ftv_usec
);
473 static int remote_fio_ctrl_c_flag
= 0;
474 static int remote_fio_no_longjmp
= 0;
476 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
477 static struct sigaction remote_fio_sa
;
478 static struct sigaction remote_fio_osa
;
480 static void (*remote_fio_ofunc
)(int);
484 remote_fileio_sig_init (void)
486 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
487 remote_fio_sa
.sa_handler
= SIG_IGN
;
488 sigemptyset (&remote_fio_sa
.sa_mask
);
489 remote_fio_sa
.sa_flags
= 0;
490 sigaction (SIGINT
, &remote_fio_sa
, &remote_fio_osa
);
492 remote_fio_ofunc
= signal (SIGINT
, SIG_IGN
);
497 remote_fileio_sig_set (void (*sigint_func
)(int))
499 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
500 remote_fio_sa
.sa_handler
= sigint_func
;
501 sigemptyset (&remote_fio_sa
.sa_mask
);
502 remote_fio_sa
.sa_flags
= 0;
503 sigaction (SIGINT
, &remote_fio_sa
, NULL
);
505 signal (SIGINT
, sigint_func
);
510 remote_fileio_sig_exit (void)
512 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
513 sigaction (SIGINT
, &remote_fio_osa
, NULL
);
515 signal (SIGINT
, remote_fio_ofunc
);
520 async_remote_fileio_interrupt (gdb_client_data arg
)
522 deprecated_throw_reason (RETURN_QUIT
);
526 remote_fileio_ctrl_c_signal_handler (int signo
)
528 remote_fileio_sig_set (SIG_IGN
);
529 remote_fio_ctrl_c_flag
= 1;
530 if (!remote_fio_no_longjmp
)
531 gdb_call_async_signal_handler (sigint_fileio_token
, 1);
532 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
536 remote_fileio_reply (int retcode
, int error
)
540 remote_fileio_sig_set (SIG_IGN
);
547 sprintf (buf
+ strlen (buf
), "%x", retcode
);
548 if (error
|| remote_fio_ctrl_c_flag
)
550 if (error
&& remote_fio_ctrl_c_flag
)
551 error
= FILEIO_EINTR
;
557 sprintf (buf
+ strlen (buf
), ",%x", error
);
558 if (remote_fio_ctrl_c_flag
)
561 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
566 remote_fileio_ioerror (void)
568 remote_fileio_reply (-1, FILEIO_EIO
);
572 remote_fileio_badfd (void)
574 remote_fileio_reply (-1, FILEIO_EBADF
);
578 remote_fileio_return_errno (int retcode
)
580 remote_fileio_reply (retcode
,
581 retcode
< 0 ? remote_fileio_errno_to_target (errno
) : 0);
585 remote_fileio_return_success (int retcode
)
587 remote_fileio_reply (retcode
, 0);
590 /* Wrapper function for remote_write_bytes() which has the disadvantage to
591 write only one packet, regardless of the requested number of bytes to
592 transfer. This wrapper calls remote_write_bytes() as often as needed. */
594 remote_fileio_write_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
596 int ret
= 0, written
;
598 while (len
> 0 && (written
= remote_write_bytes (memaddr
, myaddr
, len
)) > 0)
609 remote_fileio_func_open (char *buf
)
612 int length
, retlength
;
619 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
620 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
622 remote_fileio_ioerror ();
625 /* 2. Parameter: open flags */
626 if (remote_fileio_extract_int (&buf
, &num
))
628 remote_fileio_ioerror ();
631 flags
= remote_fileio_oflags_to_host (num
);
632 /* 3. Parameter: open mode */
633 if (remote_fileio_extract_int (&buf
, &num
))
635 remote_fileio_ioerror ();
638 mode
= remote_fileio_mode_to_host (num
, 1);
640 /* Request pathname using 'm' packet */
641 pathname
= alloca (length
);
642 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
643 if (retlength
!= length
)
645 remote_fileio_ioerror ();
649 /* Check if pathname exists and is not a regular file or directory. If so,
650 return an appropriate error code. Same for trying to open directories
652 if (!stat (pathname
, &st
))
654 if (!S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
656 remote_fileio_reply (-1, FILEIO_ENODEV
);
659 if (S_ISDIR (st
.st_mode
)
660 && ((flags
& O_WRONLY
) == O_WRONLY
|| (flags
& O_RDWR
) == O_RDWR
))
662 remote_fileio_reply (-1, FILEIO_EISDIR
);
667 remote_fio_no_longjmp
= 1;
668 fd
= open (pathname
, flags
, mode
);
671 remote_fileio_return_errno (-1);
675 fd
= remote_fileio_fd_to_targetfd (fd
);
676 remote_fileio_return_success (fd
);
680 remote_fileio_func_close (char *buf
)
685 /* Parameter: file descriptor */
686 if (remote_fileio_extract_int (&buf
, &num
))
688 remote_fileio_ioerror ();
691 fd
= remote_fileio_map_fd ((int) num
);
692 if (fd
== FIO_FD_INVALID
)
694 remote_fileio_badfd ();
698 remote_fio_no_longjmp
= 1;
699 if (fd
!= FIO_FD_CONSOLE_IN
&& fd
!= FIO_FD_CONSOLE_OUT
&& close (fd
))
700 remote_fileio_return_errno (-1);
701 remote_fileio_close_target_fd ((int) num
);
702 remote_fileio_return_success (0);
706 remote_fileio_func_read (char *buf
)
711 int fd
, ret
, retlength
;
714 off_t old_offset
, new_offset
;
716 /* 1. Parameter: file descriptor */
717 if (remote_fileio_extract_int (&buf
, &target_fd
))
719 remote_fileio_ioerror ();
722 fd
= remote_fileio_map_fd ((int) target_fd
);
723 if (fd
== FIO_FD_INVALID
)
725 remote_fileio_badfd ();
728 /* 2. Parameter: buffer pointer */
729 if (remote_fileio_extract_long (&buf
, &lnum
))
731 remote_fileio_ioerror ();
734 ptrval
= (CORE_ADDR
) lnum
;
735 /* 3. Parameter: buffer length */
736 if (remote_fileio_extract_int (&buf
, &num
))
738 remote_fileio_ioerror ();
741 length
= (size_t) num
;
745 case FIO_FD_CONSOLE_OUT
:
746 remote_fileio_badfd ();
748 case FIO_FD_CONSOLE_IN
:
750 static char *remaining_buf
= NULL
;
751 static int remaining_length
= 0;
753 buffer
= (gdb_byte
*) xmalloc (16384);
756 remote_fio_no_longjmp
= 1;
757 if (remaining_length
> length
)
759 memcpy (buffer
, remaining_buf
, length
);
760 memmove (remaining_buf
, remaining_buf
+ length
,
761 remaining_length
- length
);
762 remaining_length
-= length
;
767 memcpy (buffer
, remaining_buf
, remaining_length
);
768 xfree (remaining_buf
);
769 remaining_buf
= NULL
;
770 ret
= remaining_length
;
775 /* Windows (at least XP and Server 2003) has difficulty
776 with large reads from consoles. If a handle is
777 backed by a real console device, overly large reads
778 from the handle will fail and set errno == ENOMEM.
779 On a Windows Server 2003 system where I tested,
780 reading 26608 bytes from the console was OK, but
781 anything above 26609 bytes would fail. The limit has
782 been observed to vary on different systems. So, we
783 limit this read to something smaller than that - by a
784 safe margin, in case the limit depends on system
785 resources or version. */
786 ret
= ui_file_read (gdb_stdtargin
, (char *) buffer
, 16383);
787 remote_fio_no_longjmp
= 1;
788 if (ret
> 0 && (size_t)ret
> length
)
790 remaining_buf
= (char *) xmalloc (ret
- length
);
791 remaining_length
= ret
- length
;
792 memcpy (remaining_buf
, buffer
+ length
, remaining_length
);
799 buffer
= (gdb_byte
*) xmalloc (length
);
800 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
801 for read() to return -1 even if "some" bytes have been read. It
802 has been corrected in SUSv2 but that doesn't help us much...
803 Therefore a complete solution must check how many bytes have been
804 read on EINTR to return a more reliable value to the target */
805 old_offset
= lseek (fd
, 0, SEEK_CUR
);
806 remote_fio_no_longjmp
= 1;
807 ret
= read (fd
, buffer
, length
);
808 if (ret
< 0 && errno
== EINTR
)
810 new_offset
= lseek (fd
, 0, SEEK_CUR
);
811 /* If some data has been read, return the number of bytes read.
812 The Ctrl-C flag is set in remote_fileio_reply() anyway */
813 if (old_offset
!= new_offset
)
814 ret
= new_offset
- old_offset
;
821 retlength
= remote_fileio_write_bytes (ptrval
, buffer
, ret
);
822 if (retlength
!= ret
)
823 ret
= -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
827 remote_fileio_return_errno (-1);
829 remote_fileio_return_success (ret
);
835 remote_fileio_func_write (char *buf
)
840 int fd
, ret
, retlength
;
844 /* 1. Parameter: file descriptor */
845 if (remote_fileio_extract_int (&buf
, &target_fd
))
847 remote_fileio_ioerror ();
850 fd
= remote_fileio_map_fd ((int) target_fd
);
851 if (fd
== FIO_FD_INVALID
)
853 remote_fileio_badfd ();
856 /* 2. Parameter: buffer pointer */
857 if (remote_fileio_extract_long (&buf
, &lnum
))
859 remote_fileio_ioerror ();
862 ptrval
= (CORE_ADDR
) lnum
;
863 /* 3. Parameter: buffer length */
864 if (remote_fileio_extract_int (&buf
, &num
))
866 remote_fileio_ioerror ();
869 length
= (size_t) num
;
871 buffer
= (gdb_byte
*) xmalloc (length
);
872 retlength
= remote_read_bytes (ptrval
, buffer
, length
);
873 if (retlength
!= length
)
876 remote_fileio_ioerror ();
880 remote_fio_no_longjmp
= 1;
883 case FIO_FD_CONSOLE_IN
:
884 remote_fileio_badfd ();
887 case FIO_FD_CONSOLE_OUT
:
888 ui_file_write (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
,
889 (char *) buffer
, length
);
890 gdb_flush (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
);
894 ret
= write (fd
, buffer
, length
);
895 if (ret
< 0 && errno
== EACCES
)
896 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a R/O file.*/
901 remote_fileio_return_errno (-1);
903 remote_fileio_return_success (ret
);
909 remote_fileio_func_lseek (char *buf
)
916 /* 1. Parameter: file descriptor */
917 if (remote_fileio_extract_int (&buf
, &num
))
919 remote_fileio_ioerror ();
922 fd
= remote_fileio_map_fd ((int) num
);
923 if (fd
== FIO_FD_INVALID
)
925 remote_fileio_badfd ();
928 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
930 remote_fileio_reply (-1, FILEIO_ESPIPE
);
934 /* 2. Parameter: offset */
935 if (remote_fileio_extract_long (&buf
, &lnum
))
937 remote_fileio_ioerror ();
940 offset
= (off_t
) lnum
;
941 /* 3. Parameter: flag */
942 if (remote_fileio_extract_int (&buf
, &num
))
944 remote_fileio_ioerror ();
947 if (remote_fileio_seek_flag_to_host (num
, &flag
))
949 remote_fileio_reply (-1, FILEIO_EINVAL
);
953 remote_fio_no_longjmp
= 1;
954 ret
= lseek (fd
, offset
, flag
);
956 if (ret
== (off_t
) -1)
957 remote_fileio_return_errno (-1);
959 remote_fileio_return_success (ret
);
963 remote_fileio_func_rename (char *buf
)
965 CORE_ADDR old_ptr
, new_ptr
;
966 int old_len
, new_len
, retlength
;
967 char *oldpath
, *newpath
;
969 struct stat ost
, nst
;
971 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
972 if (remote_fileio_extract_ptr_w_len (&buf
, &old_ptr
, &old_len
))
974 remote_fileio_ioerror ();
978 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
979 if (remote_fileio_extract_ptr_w_len (&buf
, &new_ptr
, &new_len
))
981 remote_fileio_ioerror ();
985 /* Request oldpath using 'm' packet */
986 oldpath
= alloca (old_len
);
987 retlength
= remote_read_bytes (old_ptr
, (gdb_byte
*) oldpath
, old_len
);
988 if (retlength
!= old_len
)
990 remote_fileio_ioerror ();
994 /* Request newpath using 'm' packet */
995 newpath
= alloca (new_len
);
996 retlength
= remote_read_bytes (new_ptr
, (gdb_byte
*) newpath
, new_len
);
997 if (retlength
!= new_len
)
999 remote_fileio_ioerror ();
1003 /* Only operate on regular files and directories */
1004 of
= stat (oldpath
, &ost
);
1005 nf
= stat (newpath
, &nst
);
1006 if ((!of
&& !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
1007 || (!nf
&& !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
1009 remote_fileio_reply (-1, FILEIO_EACCES
);
1013 remote_fio_no_longjmp
= 1;
1014 ret
= rename (oldpath
, newpath
);
1018 /* Special case: newpath is a non-empty directory. Some systems
1019 return ENOTEMPTY, some return EEXIST. We coerce that to be
1021 if (errno
== ENOTEMPTY
)
1024 /* Workaround some Cygwin problems with correct errnos. */
1025 if (errno
== EACCES
)
1027 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
1029 if (S_ISREG (ost
.st_mode
))
1033 char oldfullpath
[PATH_MAX
];
1034 char newfullpath
[PATH_MAX
];
1037 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, oldpath
, oldfullpath
,
1039 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, newpath
, newfullpath
,
1041 len
= strlen (oldfullpath
);
1042 if (newfullpath
[len
] == '/'
1043 && !strncmp (oldfullpath
, newfullpath
, len
))
1052 remote_fileio_return_errno (-1);
1055 remote_fileio_return_success (ret
);
1059 remote_fileio_func_unlink (char *buf
)
1062 int length
, retlength
;
1067 /* Parameter: Ptr to pathname / length incl. trailing zero */
1068 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1070 remote_fileio_ioerror ();
1073 /* Request pathname using 'm' packet */
1074 pathname
= alloca (length
);
1075 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
1076 if (retlength
!= length
)
1078 remote_fileio_ioerror ();
1082 /* Only operate on regular files (and directories, which allows to return
1083 the correct return code) */
1084 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1086 remote_fileio_reply (-1, FILEIO_ENODEV
);
1090 remote_fio_no_longjmp
= 1;
1091 ret
= unlink (pathname
);
1094 remote_fileio_return_errno (-1);
1096 remote_fileio_return_success (ret
);
1100 remote_fileio_func_stat (char *buf
)
1102 CORE_ADDR statptr
, nameptr
;
1103 int ret
, namelength
, retlength
;
1107 struct fio_stat fst
;
1109 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1110 if (remote_fileio_extract_ptr_w_len (&buf
, &nameptr
, &namelength
))
1112 remote_fileio_ioerror ();
1116 /* 2. Parameter: Ptr to struct stat */
1117 if (remote_fileio_extract_long (&buf
, &lnum
))
1119 remote_fileio_ioerror ();
1122 statptr
= (CORE_ADDR
) lnum
;
1124 /* Request pathname using 'm' packet */
1125 pathname
= alloca (namelength
);
1126 retlength
= remote_read_bytes (nameptr
, (gdb_byte
*) pathname
, namelength
);
1127 if (retlength
!= namelength
)
1129 remote_fileio_ioerror ();
1133 remote_fio_no_longjmp
= 1;
1134 ret
= stat (pathname
, &st
);
1138 remote_fileio_return_errno (-1);
1141 /* Only operate on regular files and directories */
1142 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1144 remote_fileio_reply (-1, FILEIO_EACCES
);
1149 remote_fileio_to_fio_stat (&st
, &fst
);
1150 remote_fileio_to_fio_uint (0, fst
.fst_dev
);
1152 retlength
= remote_fileio_write_bytes (statptr
,
1153 (gdb_byte
*) &fst
, sizeof fst
);
1154 if (retlength
!= sizeof fst
)
1156 remote_fileio_return_errno (-1);
1160 remote_fileio_return_success (ret
);
1164 remote_fileio_func_fstat (char *buf
)
1167 int fd
, ret
, retlength
;
1171 struct fio_stat fst
;
1174 /* 1. Parameter: file descriptor */
1175 if (remote_fileio_extract_int (&buf
, &target_fd
))
1177 remote_fileio_ioerror ();
1180 fd
= remote_fileio_map_fd ((int) target_fd
);
1181 if (fd
== FIO_FD_INVALID
)
1183 remote_fileio_badfd ();
1186 /* 2. Parameter: Ptr to struct stat */
1187 if (remote_fileio_extract_long (&buf
, &lnum
))
1189 remote_fileio_ioerror ();
1192 ptrval
= (CORE_ADDR
) lnum
;
1194 remote_fio_no_longjmp
= 1;
1195 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
1197 remote_fileio_to_fio_uint (1, fst
.fst_dev
);
1198 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
1201 st
.st_uid
= getuid ();
1206 st
.st_gid
= getgid ();
1212 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1213 st
.st_blksize
= 512;
1215 #if HAVE_STRUCT_STAT_ST_BLOCKS
1218 if (!gettimeofday (&tv
, NULL
))
1219 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
1221 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
1225 ret
= fstat (fd
, &st
);
1229 remote_fileio_return_errno (-1);
1234 remote_fileio_to_fio_stat (&st
, &fst
);
1236 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &fst
, sizeof fst
);
1237 if (retlength
!= sizeof fst
)
1239 remote_fileio_return_errno (-1);
1243 remote_fileio_return_success (ret
);
1247 remote_fileio_func_gettimeofday (char *buf
)
1253 struct fio_timeval ftv
;
1255 /* 1. Parameter: struct timeval pointer */
1256 if (remote_fileio_extract_long (&buf
, &lnum
))
1258 remote_fileio_ioerror ();
1261 ptrval
= (CORE_ADDR
) lnum
;
1262 /* 2. Parameter: some pointer value... */
1263 if (remote_fileio_extract_long (&buf
, &lnum
))
1265 remote_fileio_ioerror ();
1268 /* ...which has to be NULL */
1271 remote_fileio_reply (-1, FILEIO_EINVAL
);
1275 remote_fio_no_longjmp
= 1;
1276 ret
= gettimeofday (&tv
, NULL
);
1280 remote_fileio_return_errno (-1);
1286 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1288 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &ftv
, sizeof ftv
);
1289 if (retlength
!= sizeof ftv
)
1291 remote_fileio_return_errno (-1);
1295 remote_fileio_return_success (ret
);
1299 remote_fileio_func_isatty (char *buf
)
1304 /* Parameter: file descriptor */
1305 if (remote_fileio_extract_int (&buf
, &target_fd
))
1307 remote_fileio_ioerror ();
1310 remote_fio_no_longjmp
= 1;
1311 fd
= remote_fileio_map_fd ((int) target_fd
);
1312 remote_fileio_return_success (fd
== FIO_FD_CONSOLE_IN
||
1313 fd
== FIO_FD_CONSOLE_OUT
? 1 : 0);
1317 remote_fileio_func_system (char *buf
)
1320 int ret
, length
, retlength
;
1321 char *cmdline
= NULL
;
1323 /* Parameter: Ptr to commandline / length incl. trailing zero */
1324 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1326 remote_fileio_ioerror ();
1332 /* Request commandline using 'm' packet */
1333 cmdline
= alloca (length
);
1334 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) cmdline
, length
);
1335 if (retlength
!= length
)
1337 remote_fileio_ioerror ();
1342 /* Check if system(3) has been explicitely allowed using the
1343 `set remote system-call-allowed 1' command. If length is 0,
1344 indicating a NULL parameter to the system call, return zero to
1345 indicate a shell is not available. Otherwise fail with EPERM. */
1346 if (!remote_fio_system_call_allowed
)
1349 remote_fileio_return_success (0);
1351 remote_fileio_reply (-1, FILEIO_EPERM
);
1355 remote_fio_no_longjmp
= 1;
1356 ret
= system (cmdline
);
1359 remote_fileio_return_success (ret
);
1361 remote_fileio_return_errno (-1);
1363 remote_fileio_return_success (WEXITSTATUS (ret
));
1368 void (*func
)(char *);
1369 } remote_fio_func_map
[] = {
1370 { "open", remote_fileio_func_open
},
1371 { "close", remote_fileio_func_close
},
1372 { "read", remote_fileio_func_read
},
1373 { "write", remote_fileio_func_write
},
1374 { "lseek", remote_fileio_func_lseek
},
1375 { "rename", remote_fileio_func_rename
},
1376 { "unlink", remote_fileio_func_unlink
},
1377 { "stat", remote_fileio_func_stat
},
1378 { "fstat", remote_fileio_func_fstat
},
1379 { "gettimeofday", remote_fileio_func_gettimeofday
},
1380 { "isatty", remote_fileio_func_isatty
},
1381 { "system", remote_fileio_func_system
},
1386 do_remote_fileio_request (struct ui_out
*uiout
, void *buf_arg
)
1388 char *buf
= buf_arg
;
1392 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
1394 c
= strchr (++buf
, ',');
1398 c
= strchr (buf
, '\0');
1399 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1400 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1402 if (!remote_fio_func_map
[idx
].name
) /* ERROR: No such function. */
1403 return RETURN_ERROR
;
1404 remote_fio_func_map
[idx
].func (c
);
1408 /* Close any open descriptors, and reinitialize the file mapping. */
1411 remote_fileio_reset (void)
1415 for (ix
= 0; ix
!= remote_fio_data
.fd_map_size
; ix
++)
1417 int fd
= remote_fio_data
.fd_map
[ix
];
1422 if (remote_fio_data
.fd_map
)
1424 xfree (remote_fio_data
.fd_map
);
1425 remote_fio_data
.fd_map
= NULL
;
1426 remote_fio_data
.fd_map_size
= 0;
1430 /* Handle a file I/O request. BUF points to the packet containing the
1431 request. CTRLC_PENDING_P should be nonzero if the target has not
1432 acknowledged the Ctrl-C sent asynchronously earlier. */
1435 remote_fileio_request (char *buf
, int ctrlc_pending_p
)
1439 remote_fileio_sig_init ();
1441 if (ctrlc_pending_p
)
1443 /* If the target hasn't responded to the Ctrl-C sent
1444 asynchronously earlier, take this opportunity to send the
1445 Ctrl-C synchronously. */
1446 remote_fio_ctrl_c_flag
= 1;
1447 remote_fio_no_longjmp
= 0;
1448 remote_fileio_reply (-1, FILEIO_EINTR
);
1452 remote_fio_ctrl_c_flag
= 0;
1453 remote_fio_no_longjmp
= 0;
1455 ex
= catch_exceptions (uiout
, do_remote_fileio_request
, (void *)buf
,
1460 remote_fileio_reply (-1, FILEIO_ENOSYS
);
1463 remote_fileio_reply (-1, FILEIO_EINTR
);
1470 remote_fileio_sig_exit ();
1474 set_system_call_allowed (char *args
, int from_tty
)
1479 int val
= strtoul (args
, &arg_end
, 10);
1480 if (*args
&& *arg_end
== '\0')
1482 remote_fio_system_call_allowed
= !!val
;
1486 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1490 show_system_call_allowed (char *args
, int from_tty
)
1493 error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args
);
1494 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1495 remote_fio_system_call_allowed
? "" : "not ");
1499 initialize_remote_fileio (struct cmd_list_element
*remote_set_cmdlist
,
1500 struct cmd_list_element
*remote_show_cmdlist
)
1502 sigint_fileio_token
=
1503 create_async_signal_handler (async_remote_fileio_interrupt
, NULL
);
1505 add_cmd ("system-call-allowed", no_class
,
1506 set_system_call_allowed
,
1507 _("Set if the host system(3) call is allowed for the target."),
1508 &remote_set_cmdlist
);
1509 add_cmd ("system-call-allowed", no_class
,
1510 show_system_call_allowed
,
1511 _("Show if the host system(3) call is allowed for the target."),
1512 &remote_show_cmdlist
);