1 /* Remote File-I/O communications
3 Copyright (C) 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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
, retcode
< 0
582 ? 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
825 remote_fileio_write_bytes(). */
829 remote_fileio_return_errno (-1);
831 remote_fileio_return_success (ret
);
837 remote_fileio_func_write (char *buf
)
842 int fd
, ret
, retlength
;
846 /* 1. Parameter: file descriptor */
847 if (remote_fileio_extract_int (&buf
, &target_fd
))
849 remote_fileio_ioerror ();
852 fd
= remote_fileio_map_fd ((int) target_fd
);
853 if (fd
== FIO_FD_INVALID
)
855 remote_fileio_badfd ();
858 /* 2. Parameter: buffer pointer */
859 if (remote_fileio_extract_long (&buf
, &lnum
))
861 remote_fileio_ioerror ();
864 ptrval
= (CORE_ADDR
) lnum
;
865 /* 3. Parameter: buffer length */
866 if (remote_fileio_extract_int (&buf
, &num
))
868 remote_fileio_ioerror ();
871 length
= (size_t) num
;
873 buffer
= (gdb_byte
*) xmalloc (length
);
874 retlength
= remote_read_bytes (ptrval
, buffer
, length
);
875 if (retlength
!= length
)
878 remote_fileio_ioerror ();
882 remote_fio_no_longjmp
= 1;
885 case FIO_FD_CONSOLE_IN
:
886 remote_fileio_badfd ();
889 case FIO_FD_CONSOLE_OUT
:
890 ui_file_write (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
,
891 (char *) buffer
, length
);
892 gdb_flush (target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
);
896 ret
= write (fd
, buffer
, length
);
897 if (ret
< 0 && errno
== EACCES
)
898 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a
904 remote_fileio_return_errno (-1);
906 remote_fileio_return_success (ret
);
912 remote_fileio_func_lseek (char *buf
)
919 /* 1. Parameter: file descriptor */
920 if (remote_fileio_extract_int (&buf
, &num
))
922 remote_fileio_ioerror ();
925 fd
= remote_fileio_map_fd ((int) num
);
926 if (fd
== FIO_FD_INVALID
)
928 remote_fileio_badfd ();
931 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
933 remote_fileio_reply (-1, FILEIO_ESPIPE
);
937 /* 2. Parameter: offset */
938 if (remote_fileio_extract_long (&buf
, &lnum
))
940 remote_fileio_ioerror ();
943 offset
= (off_t
) lnum
;
944 /* 3. Parameter: flag */
945 if (remote_fileio_extract_int (&buf
, &num
))
947 remote_fileio_ioerror ();
950 if (remote_fileio_seek_flag_to_host (num
, &flag
))
952 remote_fileio_reply (-1, FILEIO_EINVAL
);
956 remote_fio_no_longjmp
= 1;
957 ret
= lseek (fd
, offset
, flag
);
959 if (ret
== (off_t
) -1)
960 remote_fileio_return_errno (-1);
962 remote_fileio_return_success (ret
);
966 remote_fileio_func_rename (char *buf
)
968 CORE_ADDR old_ptr
, new_ptr
;
969 int old_len
, new_len
, retlength
;
970 char *oldpath
, *newpath
;
972 struct stat ost
, nst
;
974 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
975 if (remote_fileio_extract_ptr_w_len (&buf
, &old_ptr
, &old_len
))
977 remote_fileio_ioerror ();
981 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
982 if (remote_fileio_extract_ptr_w_len (&buf
, &new_ptr
, &new_len
))
984 remote_fileio_ioerror ();
988 /* Request oldpath using 'm' packet */
989 oldpath
= alloca (old_len
);
990 retlength
= remote_read_bytes (old_ptr
, (gdb_byte
*) oldpath
, old_len
);
991 if (retlength
!= old_len
)
993 remote_fileio_ioerror ();
997 /* Request newpath using 'm' packet */
998 newpath
= alloca (new_len
);
999 retlength
= remote_read_bytes (new_ptr
, (gdb_byte
*) newpath
, new_len
);
1000 if (retlength
!= new_len
)
1002 remote_fileio_ioerror ();
1006 /* Only operate on regular files and directories. */
1007 of
= stat (oldpath
, &ost
);
1008 nf
= stat (newpath
, &nst
);
1009 if ((!of
&& !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
1010 || (!nf
&& !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
1012 remote_fileio_reply (-1, FILEIO_EACCES
);
1016 remote_fio_no_longjmp
= 1;
1017 ret
= rename (oldpath
, newpath
);
1021 /* Special case: newpath is a non-empty directory. Some systems
1022 return ENOTEMPTY, some return EEXIST. We coerce that to be
1024 if (errno
== ENOTEMPTY
)
1027 /* Workaround some Cygwin problems with correct errnos. */
1028 if (errno
== EACCES
)
1030 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
1032 if (S_ISREG (ost
.st_mode
))
1036 char oldfullpath
[PATH_MAX
];
1037 char newfullpath
[PATH_MAX
];
1040 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, oldpath
, oldfullpath
,
1042 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, newpath
, newfullpath
,
1044 len
= strlen (oldfullpath
);
1045 if (newfullpath
[len
] == '/'
1046 && !strncmp (oldfullpath
, newfullpath
, len
))
1055 remote_fileio_return_errno (-1);
1058 remote_fileio_return_success (ret
);
1062 remote_fileio_func_unlink (char *buf
)
1065 int length
, retlength
;
1070 /* Parameter: Ptr to pathname / length incl. trailing zero */
1071 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1073 remote_fileio_ioerror ();
1076 /* Request pathname using 'm' packet */
1077 pathname
= alloca (length
);
1078 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) pathname
, length
);
1079 if (retlength
!= length
)
1081 remote_fileio_ioerror ();
1085 /* Only operate on regular files (and directories, which allows to return
1086 the correct return code). */
1087 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1089 remote_fileio_reply (-1, FILEIO_ENODEV
);
1093 remote_fio_no_longjmp
= 1;
1094 ret
= unlink (pathname
);
1097 remote_fileio_return_errno (-1);
1099 remote_fileio_return_success (ret
);
1103 remote_fileio_func_stat (char *buf
)
1105 CORE_ADDR statptr
, nameptr
;
1106 int ret
, namelength
, retlength
;
1110 struct fio_stat fst
;
1112 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
1113 if (remote_fileio_extract_ptr_w_len (&buf
, &nameptr
, &namelength
))
1115 remote_fileio_ioerror ();
1119 /* 2. Parameter: Ptr to struct stat */
1120 if (remote_fileio_extract_long (&buf
, &lnum
))
1122 remote_fileio_ioerror ();
1125 statptr
= (CORE_ADDR
) lnum
;
1127 /* Request pathname using 'm' packet */
1128 pathname
= alloca (namelength
);
1129 retlength
= remote_read_bytes (nameptr
, (gdb_byte
*) pathname
, namelength
);
1130 if (retlength
!= namelength
)
1132 remote_fileio_ioerror ();
1136 remote_fio_no_longjmp
= 1;
1137 ret
= stat (pathname
, &st
);
1141 remote_fileio_return_errno (-1);
1144 /* Only operate on regular files and directories. */
1145 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
1147 remote_fileio_reply (-1, FILEIO_EACCES
);
1152 remote_fileio_to_fio_stat (&st
, &fst
);
1153 remote_fileio_to_fio_uint (0, fst
.fst_dev
);
1155 retlength
= remote_fileio_write_bytes (statptr
,
1156 (gdb_byte
*) &fst
, sizeof fst
);
1157 if (retlength
!= sizeof fst
)
1159 remote_fileio_return_errno (-1);
1163 remote_fileio_return_success (ret
);
1167 remote_fileio_func_fstat (char *buf
)
1170 int fd
, ret
, retlength
;
1174 struct fio_stat fst
;
1177 /* 1. Parameter: file descriptor */
1178 if (remote_fileio_extract_int (&buf
, &target_fd
))
1180 remote_fileio_ioerror ();
1183 fd
= remote_fileio_map_fd ((int) target_fd
);
1184 if (fd
== FIO_FD_INVALID
)
1186 remote_fileio_badfd ();
1189 /* 2. Parameter: Ptr to struct stat */
1190 if (remote_fileio_extract_long (&buf
, &lnum
))
1192 remote_fileio_ioerror ();
1195 ptrval
= (CORE_ADDR
) lnum
;
1197 remote_fio_no_longjmp
= 1;
1198 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
1200 remote_fileio_to_fio_uint (1, fst
.fst_dev
);
1201 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
1204 st
.st_uid
= getuid ();
1209 st
.st_gid
= getgid ();
1215 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1216 st
.st_blksize
= 512;
1218 #if HAVE_STRUCT_STAT_ST_BLOCKS
1221 if (!gettimeofday (&tv
, NULL
))
1222 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
1224 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
1228 ret
= fstat (fd
, &st
);
1232 remote_fileio_return_errno (-1);
1237 remote_fileio_to_fio_stat (&st
, &fst
);
1239 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &fst
,
1241 if (retlength
!= sizeof fst
)
1243 remote_fileio_return_errno (-1);
1247 remote_fileio_return_success (ret
);
1251 remote_fileio_func_gettimeofday (char *buf
)
1257 struct fio_timeval ftv
;
1259 /* 1. Parameter: struct timeval pointer */
1260 if (remote_fileio_extract_long (&buf
, &lnum
))
1262 remote_fileio_ioerror ();
1265 ptrval
= (CORE_ADDR
) lnum
;
1266 /* 2. Parameter: some pointer value... */
1267 if (remote_fileio_extract_long (&buf
, &lnum
))
1269 remote_fileio_ioerror ();
1272 /* ...which has to be NULL. */
1275 remote_fileio_reply (-1, FILEIO_EINVAL
);
1279 remote_fio_no_longjmp
= 1;
1280 ret
= gettimeofday (&tv
, NULL
);
1284 remote_fileio_return_errno (-1);
1290 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1292 retlength
= remote_fileio_write_bytes (ptrval
, (gdb_byte
*) &ftv
,
1294 if (retlength
!= sizeof ftv
)
1296 remote_fileio_return_errno (-1);
1300 remote_fileio_return_success (ret
);
1304 remote_fileio_func_isatty (char *buf
)
1309 /* Parameter: file descriptor */
1310 if (remote_fileio_extract_int (&buf
, &target_fd
))
1312 remote_fileio_ioerror ();
1315 remote_fio_no_longjmp
= 1;
1316 fd
= remote_fileio_map_fd ((int) target_fd
);
1317 remote_fileio_return_success (fd
== FIO_FD_CONSOLE_IN
||
1318 fd
== FIO_FD_CONSOLE_OUT
? 1 : 0);
1322 remote_fileio_func_system (char *buf
)
1325 int ret
, length
, retlength
;
1326 char *cmdline
= NULL
;
1328 /* Parameter: Ptr to commandline / length incl. trailing zero */
1329 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1331 remote_fileio_ioerror ();
1337 /* Request commandline using 'm' packet */
1338 cmdline
= alloca (length
);
1339 retlength
= remote_read_bytes (ptrval
, (gdb_byte
*) cmdline
, length
);
1340 if (retlength
!= length
)
1342 remote_fileio_ioerror ();
1347 /* Check if system(3) has been explicitely allowed using the
1348 `set remote system-call-allowed 1' command. If length is 0,
1349 indicating a NULL parameter to the system call, return zero to
1350 indicate a shell is not available. Otherwise fail with EPERM. */
1351 if (!remote_fio_system_call_allowed
)
1354 remote_fileio_return_success (0);
1356 remote_fileio_reply (-1, FILEIO_EPERM
);
1360 remote_fio_no_longjmp
= 1;
1361 ret
= system (cmdline
);
1364 remote_fileio_return_success (ret
);
1366 remote_fileio_return_errno (-1);
1368 remote_fileio_return_success (WEXITSTATUS (ret
));
1373 void (*func
)(char *);
1374 } remote_fio_func_map
[] = {
1375 { "open", remote_fileio_func_open
},
1376 { "close", remote_fileio_func_close
},
1377 { "read", remote_fileio_func_read
},
1378 { "write", remote_fileio_func_write
},
1379 { "lseek", remote_fileio_func_lseek
},
1380 { "rename", remote_fileio_func_rename
},
1381 { "unlink", remote_fileio_func_unlink
},
1382 { "stat", remote_fileio_func_stat
},
1383 { "fstat", remote_fileio_func_fstat
},
1384 { "gettimeofday", remote_fileio_func_gettimeofday
},
1385 { "isatty", remote_fileio_func_isatty
},
1386 { "system", remote_fileio_func_system
},
1391 do_remote_fileio_request (struct ui_out
*uiout
, void *buf_arg
)
1393 char *buf
= buf_arg
;
1397 remote_fileio_sig_set (remote_fileio_ctrl_c_signal_handler
);
1399 c
= strchr (++buf
, ',');
1403 c
= strchr (buf
, '\0');
1404 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1405 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1407 if (!remote_fio_func_map
[idx
].name
) /* ERROR: No such function. */
1408 return RETURN_ERROR
;
1409 remote_fio_func_map
[idx
].func (c
);
1413 /* Close any open descriptors, and reinitialize the file mapping. */
1416 remote_fileio_reset (void)
1420 for (ix
= 0; ix
!= remote_fio_data
.fd_map_size
; ix
++)
1422 int fd
= remote_fio_data
.fd_map
[ix
];
1427 if (remote_fio_data
.fd_map
)
1429 xfree (remote_fio_data
.fd_map
);
1430 remote_fio_data
.fd_map
= NULL
;
1431 remote_fio_data
.fd_map_size
= 0;
1435 /* Handle a file I/O request. BUF points to the packet containing the
1436 request. CTRLC_PENDING_P should be nonzero if the target has not
1437 acknowledged the Ctrl-C sent asynchronously earlier. */
1440 remote_fileio_request (char *buf
, int ctrlc_pending_p
)
1444 remote_fileio_sig_init ();
1446 if (ctrlc_pending_p
)
1448 /* If the target hasn't responded to the Ctrl-C sent
1449 asynchronously earlier, take this opportunity to send the
1450 Ctrl-C synchronously. */
1451 remote_fio_ctrl_c_flag
= 1;
1452 remote_fio_no_longjmp
= 0;
1453 remote_fileio_reply (-1, FILEIO_EINTR
);
1457 remote_fio_ctrl_c_flag
= 0;
1458 remote_fio_no_longjmp
= 0;
1460 ex
= catch_exceptions (uiout
, do_remote_fileio_request
, (void *)buf
,
1465 remote_fileio_reply (-1, FILEIO_ENOSYS
);
1468 remote_fileio_reply (-1, FILEIO_EINTR
);
1475 remote_fileio_sig_exit ();
1479 set_system_call_allowed (char *args
, int from_tty
)
1484 int val
= strtoul (args
, &arg_end
, 10);
1486 if (*args
&& *arg_end
== '\0')
1488 remote_fio_system_call_allowed
= !!val
;
1492 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1496 show_system_call_allowed (char *args
, int from_tty
)
1499 error (_("Garbage after \"show remote "
1500 "system-call-allowed\" command: `%s'"), args
);
1501 printf_unfiltered ("Calling host system(3) call from target is %sallowed\n",
1502 remote_fio_system_call_allowed
? "" : "not ");
1506 initialize_remote_fileio (struct cmd_list_element
*remote_set_cmdlist
,
1507 struct cmd_list_element
*remote_show_cmdlist
)
1509 sigint_fileio_token
=
1510 create_async_signal_handler (async_remote_fileio_interrupt
, NULL
);
1512 add_cmd ("system-call-allowed", no_class
,
1513 set_system_call_allowed
,
1514 _("Set if the host system(3) call is allowed for the target."),
1515 &remote_set_cmdlist
);
1516 add_cmd ("system-call-allowed", no_class
,
1517 show_system_call_allowed
,
1518 _("Show if the host system(3) call is allowed for the target."),
1519 &remote_show_cmdlist
);