1 /* Remote File-I/O communications
3 Copyright (C) 2003-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* See the GDB User Guide for details of the GDB remote protocol. */
24 #include "gdbsupport/gdb_wait.h"
26 #include "remote-fileio.h"
27 #include "gdbsupport/event-loop.h"
29 #include "filenames.h"
30 #include "gdbsupport/filestuff.h"
33 #include "gdbsupport/gdb_sys_time.h"
35 #include <sys/cygwin.h>
44 #define FIO_FD_INVALID -1
45 #define FIO_FD_CONSOLE_IN -2
46 #define FIO_FD_CONSOLE_OUT -3
48 static int remote_fio_system_call_allowed
= 0;
51 remote_fileio_init_fd_map (void)
55 if (!remote_fio_data
.fd_map
)
57 remote_fio_data
.fd_map
= XNEWVEC (int, 10);
58 remote_fio_data
.fd_map_size
= 10;
59 remote_fio_data
.fd_map
[0] = FIO_FD_CONSOLE_IN
;
60 remote_fio_data
.fd_map
[1] = FIO_FD_CONSOLE_OUT
;
61 remote_fio_data
.fd_map
[2] = FIO_FD_CONSOLE_OUT
;
62 for (i
= 3; i
< 10; ++i
)
63 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
69 remote_fileio_resize_fd_map (void)
71 int i
= remote_fio_data
.fd_map_size
;
73 if (!remote_fio_data
.fd_map
)
74 return remote_fileio_init_fd_map ();
75 remote_fio_data
.fd_map_size
+= 10;
76 remote_fio_data
.fd_map
=
77 (int *) xrealloc (remote_fio_data
.fd_map
,
78 remote_fio_data
.fd_map_size
* sizeof (int));
79 for (; i
< remote_fio_data
.fd_map_size
; i
++)
80 remote_fio_data
.fd_map
[i
] = FIO_FD_INVALID
;
81 return remote_fio_data
.fd_map_size
- 10;
85 remote_fileio_next_free_fd (void)
89 for (i
= 0; i
< remote_fio_data
.fd_map_size
; ++i
)
90 if (remote_fio_data
.fd_map
[i
] == FIO_FD_INVALID
)
92 return remote_fileio_resize_fd_map ();
96 remote_fileio_fd_to_targetfd (int fd
)
98 int target_fd
= remote_fileio_next_free_fd ();
100 remote_fio_data
.fd_map
[target_fd
] = fd
;
105 remote_fileio_map_fd (int target_fd
)
107 remote_fileio_init_fd_map ();
108 if (target_fd
< 0 || target_fd
>= remote_fio_data
.fd_map_size
)
109 return FIO_FD_INVALID
;
110 return remote_fio_data
.fd_map
[target_fd
];
114 remote_fileio_close_target_fd (int target_fd
)
116 remote_fileio_init_fd_map ();
117 if (target_fd
>= 0 && target_fd
< remote_fio_data
.fd_map_size
)
118 remote_fio_data
.fd_map
[target_fd
] = FIO_FD_INVALID
;
122 remote_fileio_oflags_to_host (long flags
)
126 if (flags
& FILEIO_O_CREAT
)
128 if (flags
& FILEIO_O_EXCL
)
130 if (flags
& FILEIO_O_TRUNC
)
132 if (flags
& FILEIO_O_APPEND
)
134 if (flags
& FILEIO_O_RDONLY
)
136 if (flags
& FILEIO_O_WRONLY
)
138 if (flags
& FILEIO_O_RDWR
)
140 /* On systems supporting binary and text mode, always open files in
149 remote_fileio_mode_to_host (long mode
, int open_call
)
155 if (mode
& FILEIO_S_IFREG
)
157 if (mode
& FILEIO_S_IFDIR
)
159 if (mode
& FILEIO_S_IFCHR
)
162 if (mode
& FILEIO_S_IRUSR
)
164 if (mode
& FILEIO_S_IWUSR
)
166 if (mode
& FILEIO_S_IXUSR
)
169 if (mode
& FILEIO_S_IRGRP
)
173 if (mode
& FILEIO_S_IWGRP
)
177 if (mode
& FILEIO_S_IXGRP
)
180 if (mode
& FILEIO_S_IROTH
)
183 if (mode
& FILEIO_S_IWOTH
)
187 if (mode
& FILEIO_S_IXOTH
)
194 remote_fileio_seek_flag_to_host (long num
, int *flag
)
200 case FILEIO_SEEK_SET
:
203 case FILEIO_SEEK_CUR
:
206 case FILEIO_SEEK_END
:
216 remote_fileio_extract_long (char **buf
, LONGEST
*retlong
)
221 if (!buf
|| !*buf
|| !**buf
|| !retlong
)
223 c
= strchr (*buf
, ',');
227 c
= strchr (*buf
, '\0');
228 while (strchr ("+-", **buf
))
234 for (*retlong
= 0; **buf
; ++*buf
)
237 if (**buf
>= '0' && **buf
<= '9')
238 *retlong
+= **buf
- '0';
239 else if (**buf
>= 'a' && **buf
<= 'f')
240 *retlong
+= **buf
- 'a' + 10;
241 else if (**buf
>= 'A' && **buf
<= 'F')
242 *retlong
+= **buf
- 'A' + 10;
252 remote_fileio_extract_int (char **buf
, long *retint
)
259 ret
= remote_fileio_extract_long (buf
, &retlong
);
261 *retint
= (long) retlong
;
266 remote_fileio_extract_ptr_w_len (char **buf
, CORE_ADDR
*ptrval
, int *length
)
271 if (!buf
|| !*buf
|| !**buf
|| !ptrval
|| !length
)
273 c
= strchr (*buf
, '/');
277 if (remote_fileio_extract_long (buf
, &retlong
))
279 *ptrval
= (CORE_ADDR
) retlong
;
281 if (remote_fileio_extract_long (buf
, &retlong
))
283 *length
= (int) retlong
;
288 remote_fileio_to_fio_long (LONGEST num
, fio_long_t fnum
)
290 host_to_bigendian (num
, (char *) fnum
, 8);
294 remote_fileio_to_fio_timeval (struct timeval
*tv
, struct fio_timeval
*ftv
)
296 host_to_fileio_time (tv
->tv_sec
, ftv
->ftv_sec
);
297 remote_fileio_to_fio_long (tv
->tv_usec
, ftv
->ftv_usec
);
300 /* The quit handler originally installed. */
301 static quit_handler_ftype
*remote_fileio_o_quit_handler
;
303 /* What to do on a QUIT call while handling a file I/O request. We
304 throw a quit exception, which is caught by remote_fileio_request
305 and translated to an EINTR reply back to the target. */
308 remote_fileio_quit_handler (void)
310 if (check_quit_flag ())
315 remote_fileio_reply (remote_target
*remote
, int retcode
, int error
)
318 int ctrl_c
= check_quit_flag ();
326 sprintf (buf
+ strlen (buf
), "%x", retcode
);
330 error
= FILEIO_EINTR
;
336 sprintf (buf
+ strlen (buf
), ",%x", error
);
340 quit_handler
= remote_fileio_o_quit_handler
;
341 putpkt (remote
, buf
);
345 remote_fileio_ioerror (remote_target
*remote
)
347 remote_fileio_reply (remote
, -1, FILEIO_EIO
);
351 remote_fileio_badfd (remote_target
*remote
)
353 remote_fileio_reply (remote
, -1, FILEIO_EBADF
);
357 remote_fileio_return_errno (remote_target
*remote
, int retcode
)
359 remote_fileio_reply (remote
, retcode
, retcode
< 0
360 ? host_to_fileio_error (errno
) : 0);
364 remote_fileio_return_success (remote_target
*remote
, int retcode
)
366 remote_fileio_reply (remote
, retcode
, 0);
370 remote_fileio_func_open (remote_target
*remote
, char *buf
)
380 /* 1. Parameter: Ptr to pathname / length incl. trailing zero. */
381 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
383 remote_fileio_ioerror (remote
);
386 /* 2. Parameter: open flags */
387 if (remote_fileio_extract_int (&buf
, &num
))
389 remote_fileio_ioerror (remote
);
392 flags
= remote_fileio_oflags_to_host (num
);
393 /* 3. Parameter: open mode */
394 if (remote_fileio_extract_int (&buf
, &num
))
396 remote_fileio_ioerror (remote
);
399 mode
= remote_fileio_mode_to_host (num
, 1);
401 /* Request pathname. */
402 pathname
= (char *) alloca (length
);
403 if (target_read_memory (ptrval
, (gdb_byte
*) pathname
, length
) != 0)
405 remote_fileio_ioerror (remote
);
409 /* Check if pathname exists and is not a regular file or directory. If so,
410 return an appropriate error code. Same for trying to open directories
412 if (!stat (pathname
, &st
))
414 if (!S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
416 remote_fileio_reply (remote
, -1, FILEIO_ENODEV
);
419 if (S_ISDIR (st
.st_mode
)
420 && ((flags
& O_WRONLY
) == O_WRONLY
|| (flags
& O_RDWR
) == O_RDWR
))
422 remote_fileio_reply (remote
, -1, FILEIO_EISDIR
);
427 fd
= gdb_open_cloexec (pathname
, flags
, mode
).release ();
430 remote_fileio_return_errno (remote
, -1);
434 fd
= remote_fileio_fd_to_targetfd (fd
);
435 remote_fileio_return_success (remote
, fd
);
439 remote_fileio_func_close (remote_target
*remote
, char *buf
)
444 /* Parameter: file descriptor */
445 if (remote_fileio_extract_int (&buf
, &num
))
447 remote_fileio_ioerror (remote
);
450 fd
= remote_fileio_map_fd ((int) num
);
451 if (fd
== FIO_FD_INVALID
)
453 remote_fileio_badfd (remote
);
457 if (fd
!= FIO_FD_CONSOLE_IN
&& fd
!= FIO_FD_CONSOLE_OUT
&& close (fd
))
458 remote_fileio_return_errno (remote
, -1);
459 remote_fileio_close_target_fd ((int) num
);
460 remote_fileio_return_success (remote
, 0);
464 remote_fileio_func_read (remote_target
*remote
, char *buf
)
472 off_t old_offset
, new_offset
;
474 /* 1. Parameter: file descriptor */
475 if (remote_fileio_extract_int (&buf
, &target_fd
))
477 remote_fileio_ioerror (remote
);
480 fd
= remote_fileio_map_fd ((int) target_fd
);
481 if (fd
== FIO_FD_INVALID
)
483 remote_fileio_badfd (remote
);
486 /* 2. Parameter: buffer pointer */
487 if (remote_fileio_extract_long (&buf
, &lnum
))
489 remote_fileio_ioerror (remote
);
492 ptrval
= (CORE_ADDR
) lnum
;
493 /* 3. Parameter: buffer length */
494 if (remote_fileio_extract_int (&buf
, &num
))
496 remote_fileio_ioerror (remote
);
499 length
= (size_t) num
;
503 case FIO_FD_CONSOLE_OUT
:
504 remote_fileio_badfd (remote
);
506 case FIO_FD_CONSOLE_IN
:
508 static char *remaining_buf
= NULL
;
509 static int remaining_length
= 0;
511 buffer
= (gdb_byte
*) xmalloc (16384);
514 if (remaining_length
> length
)
516 memcpy (buffer
, remaining_buf
, length
);
517 memmove (remaining_buf
, remaining_buf
+ length
,
518 remaining_length
- length
);
519 remaining_length
-= length
;
524 memcpy (buffer
, remaining_buf
, remaining_length
);
525 xfree (remaining_buf
);
526 remaining_buf
= NULL
;
527 ret
= remaining_length
;
532 /* Windows (at least XP and Server 2003) has difficulty
533 with large reads from consoles. If a handle is
534 backed by a real console device, overly large reads
535 from the handle will fail and set errno == ENOMEM.
536 On a Windows Server 2003 system where I tested,
537 reading 26608 bytes from the console was OK, but
538 anything above 26609 bytes would fail. The limit has
539 been observed to vary on different systems. So, we
540 limit this read to something smaller than that - by a
541 safe margin, in case the limit depends on system
542 resources or version. */
543 ret
= gdb_stdtargin
->read ((char *) buffer
, 16383);
544 if (ret
> 0 && (size_t)ret
> length
)
546 remaining_buf
= (char *) xmalloc (ret
- length
);
547 remaining_length
= ret
- length
;
548 memcpy (remaining_buf
, buffer
+ length
, remaining_length
);
555 buffer
= (gdb_byte
*) xmalloc (length
);
556 /* POSIX defines EINTR behaviour of read in a weird way. It's allowed
557 for read() to return -1 even if "some" bytes have been read. It
558 has been corrected in SUSv2 but that doesn't help us much...
559 Therefore a complete solution must check how many bytes have been
560 read on EINTR to return a more reliable value to the target */
561 old_offset
= lseek (fd
, 0, SEEK_CUR
);
562 ret
= read (fd
, buffer
, length
);
563 if (ret
< 0 && errno
== EINTR
)
565 new_offset
= lseek (fd
, 0, SEEK_CUR
);
566 /* If some data has been read, return the number of bytes read.
567 The Ctrl-C flag is set in remote_fileio_reply() anyway. */
568 if (old_offset
!= new_offset
)
569 ret
= new_offset
- old_offset
;
576 errno
= target_write_memory (ptrval
, buffer
, ret
);
582 remote_fileio_return_errno (remote
, -1);
584 remote_fileio_return_success (remote
, ret
);
590 remote_fileio_func_write (remote_target
*remote
, char *buf
)
599 /* 1. Parameter: file descriptor */
600 if (remote_fileio_extract_int (&buf
, &target_fd
))
602 remote_fileio_ioerror (remote
);
605 fd
= remote_fileio_map_fd ((int) target_fd
);
606 if (fd
== FIO_FD_INVALID
)
608 remote_fileio_badfd (remote
);
611 /* 2. Parameter: buffer pointer */
612 if (remote_fileio_extract_long (&buf
, &lnum
))
614 remote_fileio_ioerror (remote
);
617 ptrval
= (CORE_ADDR
) lnum
;
618 /* 3. Parameter: buffer length */
619 if (remote_fileio_extract_int (&buf
, &num
))
621 remote_fileio_ioerror (remote
);
624 length
= (size_t) num
;
626 buffer
= (gdb_byte
*) xmalloc (length
);
627 if (target_read_memory (ptrval
, buffer
, length
) != 0)
630 remote_fileio_ioerror (remote
);
636 case FIO_FD_CONSOLE_IN
:
637 remote_fileio_badfd (remote
);
640 case FIO_FD_CONSOLE_OUT
:
642 ui_file
*file
= target_fd
== 1 ? gdb_stdtarg
: gdb_stdtargerr
;
643 file
->write ((char *) buffer
, length
);
649 ret
= write (fd
, buffer
, length
);
650 if (ret
< 0 && errno
== EACCES
)
651 errno
= EBADF
; /* Cygwin returns EACCESS when writing to a
657 remote_fileio_return_errno (remote
, -1);
659 remote_fileio_return_success (remote
, ret
);
665 remote_fileio_func_lseek (remote_target
*remote
, char *buf
)
672 /* 1. Parameter: file descriptor */
673 if (remote_fileio_extract_int (&buf
, &num
))
675 remote_fileio_ioerror (remote
);
678 fd
= remote_fileio_map_fd ((int) num
);
679 if (fd
== FIO_FD_INVALID
)
681 remote_fileio_badfd (remote
);
684 else if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
686 remote_fileio_reply (remote
, -1, FILEIO_ESPIPE
);
690 /* 2. Parameter: offset */
691 if (remote_fileio_extract_long (&buf
, &lnum
))
693 remote_fileio_ioerror (remote
);
696 offset
= (off_t
) lnum
;
697 /* 3. Parameter: flag */
698 if (remote_fileio_extract_int (&buf
, &num
))
700 remote_fileio_ioerror (remote
);
703 if (remote_fileio_seek_flag_to_host (num
, &flag
))
705 remote_fileio_reply (remote
, -1, FILEIO_EINVAL
);
709 ret
= lseek (fd
, offset
, flag
);
711 if (ret
== (off_t
) -1)
712 remote_fileio_return_errno (remote
, -1);
714 remote_fileio_return_success (remote
, ret
);
718 remote_fileio_func_rename (remote_target
*remote
, char *buf
)
720 CORE_ADDR old_ptr
, new_ptr
;
721 int old_len
, new_len
;
722 char *oldpath
, *newpath
;
724 struct stat ost
, nst
;
726 /* 1. Parameter: Ptr to oldpath / length incl. trailing zero */
727 if (remote_fileio_extract_ptr_w_len (&buf
, &old_ptr
, &old_len
))
729 remote_fileio_ioerror (remote
);
733 /* 2. Parameter: Ptr to newpath / length incl. trailing zero */
734 if (remote_fileio_extract_ptr_w_len (&buf
, &new_ptr
, &new_len
))
736 remote_fileio_ioerror (remote
);
740 /* Request oldpath using 'm' packet */
741 oldpath
= (char *) alloca (old_len
);
742 if (target_read_memory (old_ptr
, (gdb_byte
*) oldpath
, old_len
) != 0)
744 remote_fileio_ioerror (remote
);
748 /* Request newpath using 'm' packet */
749 newpath
= (char *) alloca (new_len
);
750 if (target_read_memory (new_ptr
, (gdb_byte
*) newpath
, new_len
) != 0)
752 remote_fileio_ioerror (remote
);
756 /* Only operate on regular files and directories. */
757 of
= stat (oldpath
, &ost
);
758 nf
= stat (newpath
, &nst
);
759 if ((!of
&& !S_ISREG (ost
.st_mode
) && !S_ISDIR (ost
.st_mode
))
760 || (!nf
&& !S_ISREG (nst
.st_mode
) && !S_ISDIR (nst
.st_mode
)))
762 remote_fileio_reply (remote
, -1, FILEIO_EACCES
);
766 ret
= rename (oldpath
, newpath
);
770 /* Special case: newpath is a non-empty directory. Some systems
771 return ENOTEMPTY, some return EEXIST. We coerce that to be
773 if (errno
== ENOTEMPTY
)
776 /* Workaround some Cygwin problems with correct errnos. */
779 if (!of
&& !nf
&& S_ISDIR (nst
.st_mode
))
781 if (S_ISREG (ost
.st_mode
))
785 char oldfullpath
[PATH_MAX
];
786 char newfullpath
[PATH_MAX
];
789 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, oldpath
, oldfullpath
,
791 cygwin_conv_path (CCP_WIN_A_TO_POSIX
, newpath
, newfullpath
,
793 len
= strlen (oldfullpath
);
794 if (IS_DIR_SEPARATOR (newfullpath
[len
])
795 && !filename_ncmp (oldfullpath
, newfullpath
, len
))
804 remote_fileio_return_errno (remote
, -1);
807 remote_fileio_return_success (remote
, ret
);
811 remote_fileio_func_unlink (remote_target
*remote
, char *buf
)
819 /* Parameter: Ptr to pathname / length incl. trailing zero */
820 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
822 remote_fileio_ioerror (remote
);
825 /* Request pathname using 'm' packet */
826 pathname
= (char *) alloca (length
);
827 if (target_read_memory (ptrval
, (gdb_byte
*) pathname
, length
) != 0)
829 remote_fileio_ioerror (remote
);
833 /* Only operate on regular files (and directories, which allows to return
834 the correct return code). */
835 if (!stat (pathname
, &st
) && !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
837 remote_fileio_reply (remote
, -1, FILEIO_ENODEV
);
841 ret
= unlink (pathname
);
844 remote_fileio_return_errno (remote
, -1);
846 remote_fileio_return_success (remote
, ret
);
850 remote_fileio_func_stat (remote_target
*remote
, char *buf
)
852 CORE_ADDR statptr
, nameptr
;
859 /* 1. Parameter: Ptr to pathname / length incl. trailing zero */
860 if (remote_fileio_extract_ptr_w_len (&buf
, &nameptr
, &namelength
))
862 remote_fileio_ioerror (remote
);
866 /* 2. Parameter: Ptr to struct stat */
867 if (remote_fileio_extract_long (&buf
, &lnum
))
869 remote_fileio_ioerror (remote
);
872 statptr
= (CORE_ADDR
) lnum
;
874 /* Request pathname using 'm' packet */
875 pathname
= (char *) alloca (namelength
);
876 if (target_read_memory (nameptr
, (gdb_byte
*) pathname
, namelength
) != 0)
878 remote_fileio_ioerror (remote
);
882 ret
= stat (pathname
, &st
);
886 remote_fileio_return_errno (remote
, -1);
889 /* Only operate on regular files and directories. */
890 if (!ret
&& !S_ISREG (st
.st_mode
) && !S_ISDIR (st
.st_mode
))
892 remote_fileio_reply (remote
, -1, FILEIO_EACCES
);
897 host_to_fileio_stat (&st
, &fst
);
898 host_to_fileio_uint (0, fst
.fst_dev
);
900 errno
= target_write_memory (statptr
, (gdb_byte
*) &fst
, sizeof fst
);
903 remote_fileio_return_errno (remote
, -1);
907 remote_fileio_return_success (remote
, ret
);
911 remote_fileio_func_fstat (remote_target
*remote
, char *buf
)
921 /* 1. Parameter: file descriptor */
922 if (remote_fileio_extract_int (&buf
, &target_fd
))
924 remote_fileio_ioerror (remote
);
927 fd
= remote_fileio_map_fd ((int) target_fd
);
928 if (fd
== FIO_FD_INVALID
)
930 remote_fileio_badfd (remote
);
933 /* 2. Parameter: Ptr to struct stat */
934 if (remote_fileio_extract_long (&buf
, &lnum
))
936 remote_fileio_ioerror (remote
);
939 ptrval
= (CORE_ADDR
) lnum
;
941 if (fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
)
943 host_to_fileio_uint (1, fst
.fst_dev
);
944 memset (&st
, 0, sizeof (st
));
945 st
.st_mode
= S_IFCHR
| (fd
== FIO_FD_CONSOLE_IN
? S_IRUSR
: S_IWUSR
);
948 st
.st_uid
= getuid ();
951 st
.st_gid
= getgid ();
953 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
956 #if HAVE_STRUCT_STAT_ST_BLOCKS
959 if (!gettimeofday (&tv
, NULL
))
960 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= tv
.tv_sec
;
962 st
.st_atime
= st
.st_mtime
= st
.st_ctime
= (time_t) 0;
966 ret
= fstat (fd
, &st
);
970 remote_fileio_return_errno (remote
, -1);
975 host_to_fileio_stat (&st
, &fst
);
977 errno
= target_write_memory (ptrval
, (gdb_byte
*) &fst
, sizeof fst
);
980 remote_fileio_return_errno (remote
, -1);
984 remote_fileio_return_success (remote
, ret
);
988 remote_fileio_func_gettimeofday (remote_target
*remote
, char *buf
)
994 struct fio_timeval ftv
;
996 /* 1. Parameter: struct timeval pointer */
997 if (remote_fileio_extract_long (&buf
, &lnum
))
999 remote_fileio_ioerror (remote
);
1002 ptrval
= (CORE_ADDR
) lnum
;
1003 /* 2. Parameter: some pointer value... */
1004 if (remote_fileio_extract_long (&buf
, &lnum
))
1006 remote_fileio_ioerror (remote
);
1009 /* ...which has to be NULL. */
1012 remote_fileio_reply (remote
, -1, FILEIO_EINVAL
);
1016 ret
= gettimeofday (&tv
, NULL
);
1020 remote_fileio_return_errno (remote
, -1);
1026 remote_fileio_to_fio_timeval (&tv
, &ftv
);
1028 errno
= target_write_memory (ptrval
, (gdb_byte
*) &ftv
, sizeof ftv
);
1031 remote_fileio_return_errno (remote
, -1);
1035 remote_fileio_return_success (remote
, ret
);
1039 remote_fileio_func_isatty (remote_target
*remote
, char *buf
)
1044 /* Parameter: file descriptor */
1045 if (remote_fileio_extract_int (&buf
, &target_fd
))
1047 remote_fileio_ioerror (remote
);
1050 fd
= remote_fileio_map_fd ((int) target_fd
);
1051 int ret
= fd
== FIO_FD_CONSOLE_IN
|| fd
== FIO_FD_CONSOLE_OUT
? 1 : 0;
1052 remote_fileio_return_success (remote
, ret
);
1056 remote_fileio_func_system (remote_target
*remote
, char *buf
)
1060 char *cmdline
= NULL
;
1062 /* Parameter: Ptr to commandline / length incl. trailing zero */
1063 if (remote_fileio_extract_ptr_w_len (&buf
, &ptrval
, &length
))
1065 remote_fileio_ioerror (remote
);
1071 /* Request commandline using 'm' packet */
1072 cmdline
= (char *) alloca (length
);
1073 if (target_read_memory (ptrval
, (gdb_byte
*) cmdline
, length
) != 0)
1075 remote_fileio_ioerror (remote
);
1080 /* Check if system(3) has been explicitly allowed using the
1081 `set remote system-call-allowed 1' command. If length is 0,
1082 indicating a NULL parameter to the system call, return zero to
1083 indicate a shell is not available. Otherwise fail with EPERM. */
1084 if (!remote_fio_system_call_allowed
)
1087 remote_fileio_return_success (remote
, 0);
1089 remote_fileio_reply (remote
, -1, FILEIO_EPERM
);
1093 ret
= system (cmdline
);
1096 remote_fileio_return_success (remote
, ret
);
1098 remote_fileio_return_errno (remote
, -1);
1100 remote_fileio_return_success (remote
, WEXITSTATUS (ret
));
1105 void (*func
)(remote_target
*remote
, char *);
1106 } remote_fio_func_map
[] = {
1107 { "open", remote_fileio_func_open
},
1108 { "close", remote_fileio_func_close
},
1109 { "read", remote_fileio_func_read
},
1110 { "write", remote_fileio_func_write
},
1111 { "lseek", remote_fileio_func_lseek
},
1112 { "rename", remote_fileio_func_rename
},
1113 { "unlink", remote_fileio_func_unlink
},
1114 { "stat", remote_fileio_func_stat
},
1115 { "fstat", remote_fileio_func_fstat
},
1116 { "gettimeofday", remote_fileio_func_gettimeofday
},
1117 { "isatty", remote_fileio_func_isatty
},
1118 { "system", remote_fileio_func_system
},
1123 do_remote_fileio_request (remote_target
*remote
, char *buf
)
1128 quit_handler
= remote_fileio_quit_handler
;
1130 c
= strchr (++buf
, ',');
1134 c
= strchr (buf
, '\0');
1135 for (idx
= 0; remote_fio_func_map
[idx
].name
; ++idx
)
1136 if (!strcmp (remote_fio_func_map
[idx
].name
, buf
))
1138 if (!remote_fio_func_map
[idx
].name
)
1139 remote_fileio_reply (remote
, -1, FILEIO_ENOSYS
);
1141 remote_fio_func_map
[idx
].func (remote
, c
);
1144 /* Close any open descriptors, and reinitialize the file mapping. */
1147 remote_fileio_reset (void)
1151 for (ix
= 0; ix
!= remote_fio_data
.fd_map_size
; ix
++)
1153 int fd
= remote_fio_data
.fd_map
[ix
];
1158 if (remote_fio_data
.fd_map
)
1160 xfree (remote_fio_data
.fd_map
);
1161 remote_fio_data
.fd_map
= NULL
;
1162 remote_fio_data
.fd_map_size
= 0;
1166 /* Handle a file I/O request. BUF points to the packet containing the
1167 request. CTRLC_PENDING_P should be nonzero if the target has not
1168 acknowledged the Ctrl-C sent asynchronously earlier. */
1171 remote_fileio_request (remote_target
*remote
, char *buf
, int ctrlc_pending_p
)
1173 /* Save the previous quit handler, so we can restore it. No need
1174 for a cleanup since we catch all exceptions below. Note that the
1175 quit handler is also restored by remote_fileio_reply just before
1176 pushing a packet. */
1177 remote_fileio_o_quit_handler
= quit_handler
;
1179 if (ctrlc_pending_p
)
1181 /* If the target hasn't responded to the Ctrl-C sent
1182 asynchronously earlier, take this opportunity to send the
1183 Ctrl-C synchronously. */
1185 remote_fileio_reply (remote
, -1, FILEIO_EINTR
);
1191 do_remote_fileio_request (remote
, buf
);
1193 catch (const gdb_exception_forced_quit
&ex
)
1197 catch (const gdb_exception_quit
&ex
)
1199 remote_fileio_reply (remote
, -1, FILEIO_EINTR
);
1201 catch (const gdb_exception
&ex
)
1203 remote_fileio_reply (remote
, -1, FILEIO_EIO
);
1207 quit_handler
= remote_fileio_o_quit_handler
;
1211 /* Unpack an fio_uint_t. */
1214 remote_fileio_to_host_uint (fio_uint_t fnum
)
1216 return extract_unsigned_integer ((gdb_byte
*) fnum
, 4,
1220 /* Unpack an fio_ulong_t. */
1223 remote_fileio_to_host_ulong (fio_ulong_t fnum
)
1225 return extract_unsigned_integer ((gdb_byte
*) fnum
, 8,
1229 /* Unpack an fio_mode_t. */
1232 remote_fileio_to_host_mode (fio_mode_t fnum
)
1234 return remote_fileio_mode_to_host (remote_fileio_to_host_uint (fnum
),
1238 /* Unpack an fio_time_t. */
1241 remote_fileio_to_host_time (fio_time_t fnum
)
1243 return remote_fileio_to_host_uint (fnum
);
1247 /* See remote-fileio.h. */
1250 remote_fileio_to_host_stat (struct fio_stat
*fst
, struct stat
*st
)
1252 memset (st
, 0, sizeof (struct stat
));
1254 st
->st_dev
= remote_fileio_to_host_uint (fst
->fst_dev
);
1255 st
->st_ino
= remote_fileio_to_host_uint (fst
->fst_ino
);
1256 st
->st_mode
= remote_fileio_to_host_mode (fst
->fst_mode
);
1257 st
->st_nlink
= remote_fileio_to_host_uint (fst
->fst_nlink
);
1258 st
->st_uid
= remote_fileio_to_host_uint (fst
->fst_uid
);
1259 st
->st_gid
= remote_fileio_to_host_uint (fst
->fst_gid
);
1260 st
->st_rdev
= remote_fileio_to_host_uint (fst
->fst_rdev
);
1261 st
->st_size
= remote_fileio_to_host_ulong (fst
->fst_size
);
1262 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1263 st
->st_blksize
= remote_fileio_to_host_ulong (fst
->fst_blksize
);
1265 #if HAVE_STRUCT_STAT_ST_BLOCKS
1266 st
->st_blocks
= remote_fileio_to_host_ulong (fst
->fst_blocks
);
1268 st
->st_atime
= remote_fileio_to_host_time (fst
->fst_atime
);
1269 st
->st_mtime
= remote_fileio_to_host_time (fst
->fst_mtime
);
1270 st
->st_ctime
= remote_fileio_to_host_time (fst
->fst_ctime
);
1275 set_system_call_allowed (const char *args
, int from_tty
)
1280 int val
= strtoul (args
, &arg_end
, 10);
1282 if (*args
&& *arg_end
== '\0')
1284 remote_fio_system_call_allowed
= !!val
;
1288 error (_("Illegal argument for \"set remote system-call-allowed\" command"));
1292 show_system_call_allowed (const char *args
, int from_tty
)
1295 error (_("Garbage after \"show remote "
1296 "system-call-allowed\" command: `%s'"), args
);
1297 gdb_printf ("Calling host system(3) call from target is %sallowed\n",
1298 remote_fio_system_call_allowed
? "" : "not ");
1302 initialize_remote_fileio (struct cmd_list_element
**remote_set_cmdlist
,
1303 struct cmd_list_element
**remote_show_cmdlist
)
1305 add_cmd ("system-call-allowed", no_class
,
1306 set_system_call_allowed
,
1307 _("Set if the host system(3) call is allowed for the target."),
1308 remote_set_cmdlist
);
1309 add_cmd ("system-call-allowed", no_class
,
1310 show_system_call_allowed
,
1311 _("Show if the host system(3) call is allowed for the target."),
1312 remote_show_cmdlist
);