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 ();
113 remote_fio_data
.fd_map
[target_fd
] = fd
;
118 remote_fileio_map_fd (int target_fd
)
120 remote_fileio_init_fd_map ();
121 if (target_fd
< 0 || target_fd
>= remote_fio_data
.fd_map_size
)
122 return FIO_FD_INVALID
;
123 return remote_fio_data
.fd_map
[target_fd
];
127 remote_fileio_close_target_fd (int target_fd
)
129 remote_fileio_init_fd_map ();
130 if (target_fd
>= 0 && target_fd
< remote_fio_data
.fd_map_size
)
131 remote_fio_data
.fd_map
[target_fd
] = FIO_FD_INVALID
;
135 remote_fileio_oflags_to_host (long flags
)
139 if (flags
& FILEIO_O_CREAT
)
141 if (flags
& FILEIO_O_EXCL
)
143 if (flags
& FILEIO_O_TRUNC
)
145 if (flags
& FILEIO_O_APPEND
)
147 if (flags
& FILEIO_O_RDONLY
)
149 if (flags
& FILEIO_O_WRONLY
)
151 if (flags
& FILEIO_O_RDWR
)
153 /* On systems supporting binary and text mode, always open files in
162 remote_fileio_mode_to_host (long mode
, int open_call
)
168 if (mode
& FILEIO_S_IFREG
)
170 if (mode
& FILEIO_S_IFDIR
)
172 if (mode
& FILEIO_S_IFCHR
)
175 if (mode
& FILEIO_S_IRUSR
)
177 if (mode
& FILEIO_S_IWUSR
)
179 if (mode
& FILEIO_S_IXUSR
)
182 if (mode
& FILEIO_S_IRGRP
)
186 if (mode
& FILEIO_S_IWGRP
)
190 if (mode
& FILEIO_S_IXGRP
)
193 if (mode
& FILEIO_S_IROTH
)
196 if (mode
& FILEIO_S_IWOTH
)
200 if (mode
& FILEIO_S_IXOTH
)
207 remote_fileio_mode_to_target (mode_t mode
)
212 tmode
|= FILEIO_S_IFREG
;
214 tmode
|= FILEIO_S_IFDIR
;
216 tmode
|= FILEIO_S_IFCHR
;
218 tmode
|= FILEIO_S_IRUSR
;
220 tmode
|= FILEIO_S_IWUSR
;
222 tmode
|= FILEIO_S_IXUSR
;
225 tmode
|= FILEIO_S_IRGRP
;
229 tmode
|= FILEIO_S_IWGRP
;
233 tmode
|= FILEIO_S_IXGRP
;
236 tmode
|= FILEIO_S_IROTH
;
239 tmode
|= FILEIO_S_IWOTH
;
243 tmode
|= FILEIO_S_IXOTH
;
249 remote_fileio_errno_to_target (int error
)
256 return FILEIO_ENOENT
;
264 return FILEIO_EACCES
;
266 return FILEIO_EFAULT
;
270 return FILEIO_EEXIST
;
272 return FILEIO_ENODEV
;
274 return FILEIO_ENOTDIR
;
276 return FILEIO_EISDIR
;
278 return FILEIO_EINVAL
;
280 return FILEIO_ENFILE
;
282 return FILEIO_EMFILE
;
286 return FILEIO_ENOSPC
;
288 return FILEIO_ESPIPE
;
292 return FILEIO_ENOSYS
;
294 return FILEIO_ENAMETOOLONG
;
296 return FILEIO_EUNKNOWN
;
300 remote_fileio_seek_flag_to_host (long num
, int *flag
)
306 case FILEIO_SEEK_SET
:
309 case FILEIO_SEEK_CUR
:
312 case FILEIO_SEEK_END
:
322 remote_fileio_extract_long (char **buf
, LONGEST
*retlong
)
327 if (!buf
|| !*buf
|| !**buf
|| !retlong
)
329 c
= strchr (*buf
, ',');
333 c
= strchr (*buf
, '\0');
334 while (strchr ("+-", **buf
))
340 for (*retlong
= 0; **buf
; ++*buf
)
343 if (**buf
>= '0' && **buf
<= '9')
344 *retlong
+= **buf
- '0';
345 else if (**buf
>= 'a' && **buf
<= 'f')
346 *retlong
+= **buf
- 'a' + 10;
347 else if (**buf
>= 'A' && **buf
<= 'F')
348 *retlong
+= **buf
- 'A' + 10;
358 remote_fileio_extract_int (char **buf
, long *retint
)
365 ret
= remote_fileio_extract_long (buf
, &retlong
);
367 *retint
= (long) retlong
;
372 remote_fileio_extract_ptr_w_len (char **buf
, CORE_ADDR
*ptrval
, int *length
)
377 if (!buf
|| !*buf
|| !**buf
|| !ptrval
|| !length
)
379 c
= strchr (*buf
, '/');
383 if (remote_fileio_extract_long (buf
, &retlong
))
385 *ptrval
= (CORE_ADDR
) retlong
;
387 if (remote_fileio_extract_long (buf
, &retlong
))
389 *length
= (int) retlong
;
393 /* Convert to big endian */
395 remote_fileio_to_be (LONGEST num
, char *buf
, int bytes
)
399 for (i
= 0; i
< bytes
; ++i
)
400 buf
[i
] = (num
>> (8 * (bytes
- i
- 1))) & 0xff;
404 remote_fileio_to_fio_uint (long num
, fio_uint_t fnum
)
406 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
410 remote_fileio_to_fio_mode (mode_t num
, fio_mode_t fnum
)
412 remote_fileio_to_be (remote_fileio_mode_to_target(num
), (char *) fnum
, 4);
416 remote_fileio_to_fio_time (time_t num
, fio_time_t fnum
)
418 remote_fileio_to_be ((LONGEST
) num
, (char *) fnum
, 4);
422 remote_fileio_to_fio_long (LONGEST num
, fio_long_t fnum
)
424 remote_fileio_to_be (num
, (char *) fnum
, 8);
428 remote_fileio_to_fio_ulong (LONGEST num
, fio_ulong_t fnum
)
430 remote_fileio_to_be (num
, (char *) fnum
, 8);
434 remote_fileio_to_fio_stat (struct stat
*st
, struct fio_stat
*fst
)
438 /* `st_dev' is set in the calling function */
439 remote_fileio_to_fio_uint ((long) st
->st_ino
, fst
->fst_ino
);
440 remote_fileio_to_fio_mode (st
->st_mode
, fst
->fst_mode
);
441 remote_fileio_to_fio_uint ((long) st
->st_nlink
, fst
->fst_nlink
);
442 remote_fileio_to_fio_uint ((long) st
->st_uid
, fst
->fst_uid
);
443 remote_fileio_to_fio_uint ((long) st
->st_gid
, fst
->fst_gid
);
444 remote_fileio_to_fio_uint ((long) st
->st_rdev
, fst
->fst_rdev
);
445 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_size
, fst
->fst_size
);
446 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
447 blksize
= st
->st_blksize
;
451 remote_fileio_to_fio_ulong (blksize
, fst
->fst_blksize
);
452 #if HAVE_STRUCT_STAT_ST_BLOCKS
453 remote_fileio_to_fio_ulong ((LONGEST
) st
->st_blocks
, fst
->fst_blocks
);
455 /* FIXME: This is correct for DJGPP, but other systems that don't
456 have st_blocks, if any, might prefer 512 instead of st_blksize.
457 (eliz, 30-12-2003) */
458 remote_fileio_to_fio_ulong (((LONGEST
) st
->st_size
+ blksize
- 1)
462 remote_fileio_to_fio_time (st
->st_atime
, fst
->fst_atime
);
463 remote_fileio_to_fio_time (st
->st_mtime
, fst
->fst_mtime
);
464 remote_fileio_to_fio_time (st
->st_ctime
, fst
->fst_ctime
);
468 remote_fileio_to_fio_timeval (struct timeval
*tv
, struct fio_timeval
*ftv
)
470 remote_fileio_to_fio_time (tv
->tv_sec
, ftv
->ftv_sec
);
471 remote_fileio_to_fio_long (tv
->tv_usec
, ftv
->ftv_usec
);
474 static int remote_fio_ctrl_c_flag
= 0;
475 static int remote_fio_no_longjmp
= 0;
477 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
478 static struct sigaction remote_fio_sa
;
479 static struct sigaction remote_fio_osa
;
481 static void (*remote_fio_ofunc
)(int);
485 remote_fileio_sig_init (void)
487 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
488 remote_fio_sa
.sa_handler
= SIG_IGN
;
489 sigemptyset (&remote_fio_sa
.sa_mask
);
490 remote_fio_sa
.sa_flags
= 0;
491 sigaction (SIGINT
, &remote_fio_sa
, &remote_fio_osa
);
493 remote_fio_ofunc
= signal (SIGINT
, SIG_IGN
);
498 remote_fileio_sig_set (void (*sigint_func
)(int))
500 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
501 remote_fio_sa
.sa_handler
= sigint_func
;
502 sigemptyset (&remote_fio_sa
.sa_mask
);
503 remote_fio_sa
.sa_flags
= 0;
504 sigaction (SIGINT
, &remote_fio_sa
, NULL
);
506 signal (SIGINT
, sigint_func
);
511 remote_fileio_sig_exit (void)
513 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
514 sigaction (SIGINT
, &remote_fio_osa
, NULL
);
516 signal (SIGINT
, remote_fio_ofunc
);
521 async_remote_fileio_interrupt (gdb_client_data arg
)
523 deprecated_throw_reason (RETURN_QUIT
);
527 remote_fileio_ctrl_c_signal_handler (int signo
)
529 remote_fileio_sig_set (SIG_IGN
);
530 remote_fio_ctrl_c_flag
= 1;
531 if (!remote_fio_no_longjmp
)
532 gdb_call_async_signal_handler (sigint_fileio_token
, 1);
533 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
537 remote_fileio_reply (int retcode
, int error
)
541 remote_fileio_sig_set (SIG_IGN
);
548 sprintf (buf
+ strlen (buf
), "%x", retcode
);
549 if (error
|| remote_fio_ctrl_c_flag
)
551 if (error
&& remote_fio_ctrl_c_flag
)
552 error
= FILEIO_EINTR
;
558 sprintf (buf
+ strlen (buf
), ",%x", error
);
559 if (remote_fio_ctrl_c_flag
)
562 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
567 remote_fileio_ioerror (void)
569 remote_fileio_reply (-1, FILEIO_EIO
);
573 remote_fileio_badfd (void)
575 remote_fileio_reply (-1, FILEIO_EBADF
);
579 remote_fileio_return_errno (int retcode
)
581 remote_fileio_reply (retcode
,
582 retcode
< 0 ? remote_fileio_errno_to_target (errno
) : 0);
586 remote_fileio_return_success (int retcode
)
588 remote_fileio_reply (retcode
, 0);
591 /* Wrapper function for remote_write_bytes() which has the disadvantage to
592 write only one packet, regardless of the requested number of bytes to
593 transfer. This wrapper calls remote_write_bytes() as often as needed. */
595 remote_fileio_write_bytes (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
)
597 int ret
= 0, written
;
599 while (len
> 0 && (written
= remote_write_bytes (memaddr
, myaddr
, len
)) > 0)
610 remote_fileio_func_open (char *buf
)
613 int length
, retlength
;
620 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
621 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
623 remote_fileio_ioerror ();
626 /* 2. Parameter: open flags */
627 if (remote_fileio_extract_int (&buf
, &num
))
629 remote_fileio_ioerror ();
632 flags
= remote_fileio_oflags_to_host (num
);
633 /* 3. Parameter: open mode */
634 if (remote_fileio_extract_int (&buf
, &num
))
636 remote_fileio_ioerror ();
639 mode
= remote_fileio_mode_to_host (num
, 1);
641 /* Request pathname using 'm' packet */
642 pathname
= alloca (length
);
643 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
644 if (retlength
!= length
)
646 remote_fileio_ioerror ();
650 /* Check if pathname exists and is not a regular file or directory. If so,
651 return an appropriate error code. Same for trying to open directories
653 if (!stat (pathname
, &st
))
655 if (!S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
657 remote_fileio_reply (-1, FILEIO_ENODEV
);
660 if (S_ISDIR (st
.st_mode
)
661 && ((flags
& O_WRONLY
) == O_WRONLY
|| (flags
& O_RDWR
) == O_RDWR
))
663 remote_fileio_reply (-1, FILEIO_EISDIR
);
668 remote_fio_no_longjmp
= 1;
669 fd
= open (pathname
, flags
, mode
);
672 remote_fileio_return_errno (-1);
676 fd
= remote_fileio_fd_to_targetfd (fd
);
677 remote_fileio_return_success (fd
);
681 remote_fileio_func_close (char *buf
)
686 /* Parameter: file descriptor */
687 if (remote_fileio_extract_int (&buf
, &num
))
689 remote_fileio_ioerror ();
692 fd
= remote_fileio_map_fd ((int) num
);
693 if (fd
== FIO_FD_INVALID
)
695 remote_fileio_badfd ();
699 remote_fio_no_longjmp
= 1;
700 if (fd
!= FIO_FD_CONSOLE_IN
&& fd
!= FIO_FD_CONSOLE_OUT
&& close (fd
))
701 remote_fileio_return_errno (-1);
702 remote_fileio_close_target_fd ((int) num
);
703 remote_fileio_return_success (0);
707 remote_fileio_func_read (char *buf
)
712 int fd
, ret
, retlength
;
715 off_t old_offset
, new_offset
;
717 /* 1. Parameter: file descriptor */
718 if (remote_fileio_extract_int (&buf
, &target_fd
))
720 remote_fileio_ioerror ();
723 fd
= remote_fileio_map_fd ((int) target_fd
);
724 if (fd
== FIO_FD_INVALID
)
726 remote_fileio_badfd ();
729 /* 2. Parameter: buffer pointer */
730 if (remote_fileio_extract_long (&buf
, &lnum
))
732 remote_fileio_ioerror ();
735 ptrval
= (CORE_ADDR
) lnum
;
736 /* 3. Parameter: buffer length */
737 if (remote_fileio_extract_int (&buf
, &num
))
739 remote_fileio_ioerror ();
742 length
= (size_t) num
;
746 case FIO_FD_CONSOLE_OUT
:
747 remote_fileio_badfd ();
749 case FIO_FD_CONSOLE_IN
:
751 static char *remaining_buf
= NULL
;
752 static int remaining_length
= 0;
754 buffer
= (gdb_byte
*) xmalloc (16384);
757 remote_fio_no_longjmp
= 1;
758 if (remaining_length
> length
)
760 memcpy (buffer
, remaining_buf
, length
);
761 memmove (remaining_buf
, remaining_buf
+ length
,
762 remaining_length
- length
);
763 remaining_length
-= length
;
768 memcpy (buffer
, remaining_buf
, remaining_length
);
769 xfree (remaining_buf
);
770 remaining_buf
= NULL
;
771 ret
= remaining_length
;
776 /* Windows (at least XP and Server 2003) has difficulty
777 with large reads from consoles. If a handle is
778 backed by a real console device, overly large reads
779 from the handle will fail and set errno == ENOMEM.
780 On a Windows Server 2003 system where I tested,
781 reading 26608 bytes from the console was OK, but
782 anything above 26609 bytes would fail. The limit has
783 been observed to vary on different systems. So, we
784 limit this read to something smaller than that - by a
785 safe margin, in case the limit depends on system
786 resources or version. */
787 ret
= ui_file_read (gdb_stdtargin
, (char *) buffer
, 16383);
788 remote_fio_no_longjmp
= 1;
789 if (ret
> 0 && (size_t)ret
> length
)
791 remaining_buf
= (char *) xmalloc (ret
- length
);
792 remaining_length
= ret
- length
;
793 memcpy (remaining_buf
, buffer
+ length
, remaining_length
);
800 buffer
= (gdb_byte
*) xmalloc (length
);
801 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
802 for read() to return -1 even if "some" bytes have been read. It
803 has been corrected in SUSv2 but that doesn't help us much...
804 Therefore a complete solution must check how many bytes have been
805 read on EINTR to return a more reliable value to the target */
806 old_offset
= lseek (fd
, 0, SEEK_CUR
);
807 remote_fio_no_longjmp
= 1;
808 ret
= read (fd
, buffer
, length
);
809 if (ret
< 0 && errno
== EINTR
)
811 new_offset
= lseek (fd
, 0, SEEK_CUR
);
812 /* If some data has been read, return the number of bytes read.
813 The Ctrl-C flag is set in remote_fileio_reply() anyway */
814 if (old_offset
!= new_offset
)
815 ret
= new_offset
- old_offset
;
822 retlength
= remote_fileio_write_bytes (ptrval
, buffer
, ret
);
823 if (retlength
!= ret
)
824 ret
= -1; /* errno has been set to EIO in remote_fileio_write_bytes() */
828 remote_fileio_return_errno (-1);
830 remote_fileio_return_success (ret
);
836 remote_fileio_func_write (char *buf
)
841 int fd
, ret
, retlength
;
845 /* 1. Parameter: file descriptor */
846 if (remote_fileio_extract_int (&buf
, &target_fd
))
848 remote_fileio_ioerror ();
851 fd
= remote_fileio_map_fd ((int) target_fd
);
852 if (fd
== FIO_FD_INVALID
)
854 remote_fileio_badfd ();
857 /* 2. Parameter: buffer pointer */
858 if (remote_fileio_extract_long (&buf
, &lnum
))
860 remote_fileio_ioerror ();
863 ptrval
= (CORE_ADDR
) lnum
;
864 /* 3. Parameter: buffer length */
865 if (remote_fileio_extract_int (&buf
, &num
))
867 remote_fileio_ioerror ();
870 length
= (size_t) num
;
872 buffer
= (gdb_byte
*) xmalloc (length
);
873 retlength
= remote_read_bytes (ptrval
, buffer
, length
);
874 if (retlength
!= length
)
877 remote_fileio_ioerror ();
881 remote_fio_no_longjmp
= 1;
884 case FIO_FD_CONSOLE_IN
:
885 remote_fileio_badfd ();
888 case FIO_FD_CONSOLE_OUT
:
889 ui_file_write (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
,
890 (char *) buffer
, length
);
891 gdb_flush (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
);
895 ret
= write (fd
, buffer
, length
);
896 if (ret
< 0 && errno
== EACCES
)
897 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a R/O file.*/
902 remote_fileio_return_errno (-1);
904 remote_fileio_return_success (ret
);
910 remote_fileio_func_lseek (char *buf
)
917 /* 1. Parameter: file descriptor */
918 if (remote_fileio_extract_int (&buf
, &num
))
920 remote_fileio_ioerror ();
923 fd
= remote_fileio_map_fd ((int) num
);
924 if (fd
== FIO_FD_INVALID
)
926 remote_fileio_badfd ();
929 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
931 remote_fileio_reply (-1, FILEIO_ESPIPE
);
935 /* 2. Parameter: offset */
936 if (remote_fileio_extract_long (&buf
, &lnum
))
938 remote_fileio_ioerror ();
941 offset
= (off_t
) lnum
;
942 /* 3. Parameter: flag */
943 if (remote_fileio_extract_int (&buf
, &num
))
945 remote_fileio_ioerror ();
948 if (remote_fileio_seek_flag_to_host (num
, &flag
))
950 remote_fileio_reply (-1, FILEIO_EINVAL
);
954 remote_fio_no_longjmp
= 1;
955 ret
= lseek (fd
, offset
, flag
);
957 if (ret
== (off_t
) -1)
958 remote_fileio_return_errno (-1);
960 remote_fileio_return_success (ret
);
964 remote_fileio_func_rename (char *buf
)
966 CORE_ADDR old_ptr
, new_ptr
;
967 int old_len
, new_len
, retlength
;
968 char *oldpath
, *newpath
;
970 struct stat ost
, nst
;
972 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
973 if (remote_fileio_extract_ptr_w_len (&buf
, &old_ptr
, &old_len
))
975 remote_fileio_ioerror ();
979 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
980 if (remote_fileio_extract_ptr_w_len (&buf
, &new_ptr
, &new_len
))
982 remote_fileio_ioerror ();
986 /* Request oldpath using 'm' packet */
987 oldpath
= alloca (old_len
);
988 retlength
= remote_read_bytes (old_ptr
, (gdb_byte
*) oldpath
, old_len
);
989 if (retlength
!= old_len
)
991 remote_fileio_ioerror ();
995 /* Request newpath using 'm' packet */
996 newpath
= alloca (new_len
);
997 retlength
= remote_read_bytes (new_ptr
, (gdb_byte
*) newpath
, new_len
);
998 if (retlength
!= new_len
)
1000 remote_fileio_ioerror ();
1004 /* Only operate on regular files and directories */
1005 of
= stat (oldpath
, &ost
);
1006 nf
= stat (newpath
, &nst
);
1007 if ((!of
&& !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
1008 || (!nf
&& !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
1010 remote_fileio_reply (-1, FILEIO_EACCES
);
1014 remote_fio_no_longjmp
= 1;
1015 ret
= rename (oldpath
, newpath
);
1019 /* Special case: newpath is a non-empty directory. Some systems
1020 return ENOTEMPTY, some return EEXIST. We coerce that to be
1022 if (errno
== ENOTEMPTY
)
1025 /* Workaround some Cygwin problems with correct errnos. */
1026 if (errno
== EACCES
)
1028 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
1030 if (S_ISREG (ost
.st_mode
))
1034 char oldfullpath
[PATH_MAX
];
1035 char newfullpath
[PATH_MAX
];
1038 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, oldpath
, oldfullpath
,
1040 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, newpath
, newfullpath
,
1042 len
= strlen (oldfullpath
);
1043 if (newfullpath
[len
] == '/'
1044 && !strncmp (oldfullpath
, newfullpath
, len
))
1053 remote_fileio_return_errno (-1);
1056 remote_fileio_return_success (ret
);
1060 remote_fileio_func_unlink (char *buf
)
1063 int length
, retlength
;
1068 /* Parameter: Ptr to pathname / length incl. trailing zero */
1069 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1071 remote_fileio_ioerror ();
1074 /* Request pathname using 'm' packet */
1075 pathname
= alloca (length
);
1076 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
1077 if (retlength
!= length
)
1079 remote_fileio_ioerror ();
1083 /* Only operate on regular files (and directories, which allows to return
1084 the correct return code) */
1085 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1087 remote_fileio_reply (-1, FILEIO_ENODEV
);
1091 remote_fio_no_longjmp
= 1;
1092 ret
= unlink (pathname
);
1095 remote_fileio_return_errno (-1);
1097 remote_fileio_return_success (ret
);
1101 remote_fileio_func_stat (char *buf
)
1103 CORE_ADDR statptr
, nameptr
;
1104 int ret
, namelength
, retlength
;
1108 struct fio_stat fst
;
1110 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1111 if (remote_fileio_extract_ptr_w_len (&buf
, &nameptr
, &namelength
))
1113 remote_fileio_ioerror ();
1117 /* 2. Parameter: Ptr to struct stat */
1118 if (remote_fileio_extract_long (&buf
, &lnum
))
1120 remote_fileio_ioerror ();
1123 statptr
= (CORE_ADDR
) lnum
;
1125 /* Request pathname using 'm' packet */
1126 pathname
= alloca (namelength
);
1127 retlength
= remote_read_bytes (nameptr
, (gdb_byte
*) pathname
, namelength
);
1128 if (retlength
!= namelength
)
1130 remote_fileio_ioerror ();
1134 remote_fio_no_longjmp
= 1;
1135 ret
= stat (pathname
, &st
);
1139 remote_fileio_return_errno (-1);
1142 /* Only operate on regular files and directories */
1143 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1145 remote_fileio_reply (-1, FILEIO_EACCES
);
1150 remote_fileio_to_fio_stat (&st
, &fst
);
1151 remote_fileio_to_fio_uint (0, fst
.fst_dev
);
1153 retlength
= remote_fileio_write_bytes (statptr
,
1154 (gdb_byte
*) &fst
, sizeof fst
);
1155 if (retlength
!= sizeof fst
)
1157 remote_fileio_return_errno (-1);
1161 remote_fileio_return_success (ret
);
1165 remote_fileio_func_fstat (char *buf
)
1168 int fd
, ret
, retlength
;
1172 struct fio_stat fst
;
1175 /* 1. Parameter: file descriptor */
1176 if (remote_fileio_extract_int (&buf
, &target_fd
))
1178 remote_fileio_ioerror ();
1181 fd
= remote_fileio_map_fd ((int) target_fd
);
1182 if (fd
== FIO_FD_INVALID
)
1184 remote_fileio_badfd ();
1187 /* 2. Parameter: Ptr to struct stat */
1188 if (remote_fileio_extract_long (&buf
, &lnum
))
1190 remote_fileio_ioerror ();
1193 ptrval
= (CORE_ADDR
) lnum
;
1195 remote_fio_no_longjmp
= 1;
1196 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
1198 remote_fileio_to_fio_uint (1, fst
.fst_dev
);
1199 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
1202 st
.st_uid
= getuid ();
1207 st
.st_gid
= getgid ();
1213 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1214 st
.st_blksize
= 512;
1216 #if HAVE_STRUCT_STAT_ST_BLOCKS
1219 if (!gettimeofday (&tv
, NULL
))
1220 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
1222 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
1226 ret
= fstat (fd
, &st
);
1230 remote_fileio_return_errno (-1);
1235 remote_fileio_to_fio_stat (&st
, &fst
);
1237 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &fst
, sizeof fst
);
1238 if (retlength
!= sizeof fst
)
1240 remote_fileio_return_errno (-1);
1244 remote_fileio_return_success (ret
);
1248 remote_fileio_func_gettimeofday (char *buf
)
1254 struct fio_timeval ftv
;
1256 /* 1. Parameter: struct timeval pointer */
1257 if (remote_fileio_extract_long (&buf
, &lnum
))
1259 remote_fileio_ioerror ();
1262 ptrval
= (CORE_ADDR
) lnum
;
1263 /* 2. Parameter: some pointer value... */
1264 if (remote_fileio_extract_long (&buf
, &lnum
))
1266 remote_fileio_ioerror ();
1269 /* ...which has to be NULL */
1272 remote_fileio_reply (-1, FILEIO_EINVAL
);
1276 remote_fio_no_longjmp
= 1;
1277 ret
= gettimeofday (&tv
, NULL
);
1281 remote_fileio_return_errno (-1);
1287 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1289 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &ftv
, sizeof ftv
);
1290 if (retlength
!= sizeof ftv
)
1292 remote_fileio_return_errno (-1);
1296 remote_fileio_return_success (ret
);
1300 remote_fileio_func_isatty (char *buf
)
1305 /* Parameter: file descriptor */
1306 if (remote_fileio_extract_int (&buf
, &target_fd
))
1308 remote_fileio_ioerror ();
1311 remote_fio_no_longjmp
= 1;
1312 fd
= remote_fileio_map_fd ((int) target_fd
);
1313 remote_fileio_return_success (fd
== FIO_FD_CONSOLE_IN
||
1314 fd
== FIO_FD_CONSOLE_OUT
? 1 : 0);
1318 remote_fileio_func_system (char *buf
)
1321 int ret
, length
, retlength
;
1322 char *cmdline
= NULL
;
1324 /* Parameter: Ptr to commandline / length incl. trailing zero */
1325 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1327 remote_fileio_ioerror ();
1333 /* Request commandline using 'm' packet */
1334 cmdline
= alloca (length
);
1335 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) cmdline
, length
);
1336 if (retlength
!= length
)
1338 remote_fileio_ioerror ();
1343 /* Check if system(3) has been explicitely allowed using the
1344 `set remote system-call-allowed 1' command. If length is 0,
1345 indicating a NULL parameter to the system call, return zero to
1346 indicate a shell is not available. Otherwise fail with EPERM. */
1347 if (!remote_fio_system_call_allowed
)
1350 remote_fileio_return_success (0);
1352 remote_fileio_reply (-1, FILEIO_EPERM
);
1356 remote_fio_no_longjmp
= 1;
1357 ret
= system (cmdline
);
1360 remote_fileio_return_success (ret
);
1362 remote_fileio_return_errno (-1);
1364 remote_fileio_return_success (WEXITSTATUS (ret
));
1369 void (*func
)(char *);
1370 } remote_fio_func_map
[] = {
1371 { "open", remote_fileio_func_open
},
1372 { "close", remote_fileio_func_close
},
1373 { "read", remote_fileio_func_read
},
1374 { "write", remote_fileio_func_write
},
1375 { "lseek", remote_fileio_func_lseek
},
1376 { "rename", remote_fileio_func_rename
},
1377 { "unlink", remote_fileio_func_unlink
},
1378 { "stat", remote_fileio_func_stat
},
1379 { "fstat", remote_fileio_func_fstat
},
1380 { "gettimeofday", remote_fileio_func_gettimeofday
},
1381 { "isatty", remote_fileio_func_isatty
},
1382 { "system", remote_fileio_func_system
},
1387 do_remote_fileio_request (struct ui_out
*uiout
, void *buf_arg
)
1389 char *buf
= buf_arg
;
1393 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
1395 c
= strchr (++buf
, ',');
1399 c
= strchr (buf
, '\0');
1400 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1401 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1403 if (!remote_fio_func_map
[idx
].name
) /* ERROR: No such function. */
1404 return RETURN_ERROR
;
1405 remote_fio_func_map
[idx
].func (c
);
1409 /* Close any open descriptors, and reinitialize the file mapping. */
1412 remote_fileio_reset (void)
1416 for (ix
= 0; ix
!= remote_fio_data
.fd_map_size
; ix
++)
1418 int fd
= remote_fio_data
.fd_map
[ix
];
1423 if (remote_fio_data
.fd_map
)
1425 xfree (remote_fio_data
.fd_map
);
1426 remote_fio_data
.fd_map
= NULL
;
1427 remote_fio_data
.fd_map_size
= 0;
1431 /* Handle a file I/O request. BUF points to the packet containing the
1432 request. CTRLC_PENDING_P should be nonzero if the target has not
1433 acknowledged the Ctrl-C sent asynchronously earlier. */
1436 remote_fileio_request (char *buf
, int ctrlc_pending_p
)
1440 remote_fileio_sig_init ();
1442 if (ctrlc_pending_p
)
1444 /* If the target hasn't responded to the Ctrl-C sent
1445 asynchronously earlier, take this opportunity to send the
1446 Ctrl-C synchronously. */
1447 remote_fio_ctrl_c_flag
= 1;
1448 remote_fio_no_longjmp
= 0;
1449 remote_fileio_reply (-1, FILEIO_EINTR
);
1453 remote_fio_ctrl_c_flag
= 0;
1454 remote_fio_no_longjmp
= 0;
1456 ex
= catch_exceptions (uiout
, do_remote_fileio_request
, (void *)buf
,
1461 remote_fileio_reply (-1, FILEIO_ENOSYS
);
1464 remote_fileio_reply (-1, FILEIO_EINTR
);
1471 remote_fileio_sig_exit ();
1475 set_system_call_allowed (char *args
, int from_tty
)
1480 int val
= strtoul (args
, &arg_end
, 10);
1482 if (*args
&& *arg_end
== '\0')
1484 remote_fio_system_call_allowed
= !!val
;
1488 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1492 show_system_call_allowed (char *args
, int from_tty
)
1495 error (_("Garbage after \"show remote system-call-allowed\" command: `%s'"), args
);
1496 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1497 remote_fio_system_call_allowed
? "" : "not ");
1501 initialize_remote_fileio (struct cmd_list_element
*remote_set_cmdlist
,
1502 struct cmd_list_element
*remote_show_cmdlist
)
1504 sigint_fileio_token
=
1505 create_async_signal_handler (async_remote_fileio_interrupt
, NULL
);
1507 add_cmd ("system-call-allowed", no_class
,
1508 set_system_call_allowed
,
1509 _("Set if the host system(3) call is allowed for the target."),
1510 &remote_set_cmdlist
);
1511 add_cmd ("system-call-allowed", no_class
,
1512 show_system_call_allowed
,
1513 _("Show if the host system(3) call is allowed for the target."),
1514 &remote_show_cmdlist
);