1 /* Remote target callback routines.
2 Copyright 1995-2021 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
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 /* This file provides a standard way for targets to talk to the host OS
23 /* This must come before any other includes. */
37 #include <sys/types.h>
39 #include "sim/callback.h"
40 #include "targ-vals.h"
42 #include "libiberty.h"
52 extern CB_TARGET_DEFS_MAP cb_init_syscall_map
[];
53 extern CB_TARGET_DEFS_MAP cb_init_errno_map
[];
54 extern CB_TARGET_DEFS_MAP cb_init_signal_map
[];
55 extern CB_TARGET_DEFS_MAP cb_init_open_map
[];
57 /* Make sure the FD provided is ok. If not, return non-zero
61 fdbad (host_callback
*p
, int fd
)
63 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| p
->fd_buddy
[fd
] < 0)
65 p
->last_errno
= EBADF
;
72 fdmap (host_callback
*p
, int fd
)
78 os_close (host_callback
*p
, int fd
)
83 result
= fdbad (p
, fd
);
86 /* If this file descripter has one or more buddies (originals /
87 duplicates from a dup), just remove it from the circular list. */
88 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
91 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
96 int other
= p
->ispipe
[fd
];
101 /* Closing the read side. */
107 /* Closing the write side. */
112 /* If there was data in the buffer, make a last "now empty"
113 call, then deallocate data. */
114 if (p
->pipe_buffer
[writer
].buffer
!= NULL
)
116 (*p
->pipe_empty
) (p
, reader
, writer
);
117 free (p
->pipe_buffer
[writer
].buffer
);
118 p
->pipe_buffer
[writer
].buffer
= NULL
;
121 /* Clear pipe data for this side. */
122 p
->pipe_buffer
[fd
].size
= 0;
125 /* If this was the first close, mark the other side as the
126 only remaining side. */
127 if (fd
!= abs (other
))
128 p
->ispipe
[abs (other
)] = -other
;
129 p
->fd_buddy
[fd
] = -1;
133 result
= close (fdmap (p
, fd
));
134 p
->last_errno
= errno
;
136 p
->fd_buddy
[fd
] = -1;
142 /* taken from gdb/util.c:notice_quit() - should be in a library */
145 #if defined(_MSC_VER)
147 os_poll_quit (host_callback
*p
)
149 /* NB - this will not compile! */
150 int k
= win32pollquit ();
158 #define os_poll_quit 0
159 #endif /* defined(_MSC_VER) */
162 os_get_errno (host_callback
*p
)
164 return cb_host_to_target_errno (p
, p
->last_errno
);
169 os_isatty (host_callback
*p
, int fd
)
173 result
= fdbad (p
, fd
);
177 result
= isatty (fdmap (p
, fd
));
178 p
->last_errno
= errno
;
183 os_lseek (host_callback
*p
, int fd
, int64_t off
, int way
)
187 result
= fdbad (p
, fd
);
191 result
= lseek (fdmap (p
, fd
), off
, way
);
192 p
->last_errno
= errno
;
197 os_open (host_callback
*p
, const char *name
, int flags
)
200 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
202 if (p
->fd_buddy
[i
] < 0)
204 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
207 p
->last_errno
= errno
;
215 p
->last_errno
= EMFILE
;
220 os_read (host_callback
*p
, int fd
, char *buf
, int len
)
224 result
= fdbad (p
, fd
);
229 int writer
= p
->ispipe
[fd
];
231 /* Can't read from the write-end. */
234 p
->last_errno
= EBADF
;
238 /* Nothing to read if nothing is written. */
239 if (p
->pipe_buffer
[writer
].size
== 0)
242 /* Truncate read request size to buffer size minus what's already
244 if (len
> p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
)
245 len
= p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
;
247 memcpy (buf
, p
->pipe_buffer
[writer
].buffer
+ p
->pipe_buffer
[fd
].size
,
250 /* Account for what we just read. */
251 p
->pipe_buffer
[fd
].size
+= len
;
253 /* If we've read everything, empty and deallocate the buffer and
254 signal buffer-empty to client. (This isn't expected to be a
255 hot path in the simulator, so we don't hold on to the buffer.) */
256 if (p
->pipe_buffer
[fd
].size
== p
->pipe_buffer
[writer
].size
)
258 free (p
->pipe_buffer
[writer
].buffer
);
259 p
->pipe_buffer
[writer
].buffer
= NULL
;
260 p
->pipe_buffer
[fd
].size
= 0;
261 p
->pipe_buffer
[writer
].size
= 0;
262 (*p
->pipe_empty
) (p
, fd
, writer
);
268 result
= read (fdmap (p
, fd
), buf
, len
);
269 p
->last_errno
= errno
;
274 os_read_stdin (host_callback
*p
, char *buf
, int len
)
278 result
= read (0, buf
, len
);
279 p
->last_errno
= errno
;
284 os_write (host_callback
*p
, int fd
, const char *buf
, int len
)
289 result
= fdbad (p
, fd
);
295 int reader
= -p
->ispipe
[fd
];
297 /* Can't write to the read-end. */
300 p
->last_errno
= EBADF
;
304 /* Can't write to pipe with closed read end.
305 FIXME: We should send a SIGPIPE. */
308 p
->last_errno
= EPIPE
;
312 /* As a sanity-check, we bail out it the buffered contents is much
313 larger than the size of the buffer on the host. We don't want
314 to run out of memory in the simulator due to a target program
315 bug if we can help it. Unfortunately, regarding the value that
316 reaches the simulated program, it's no use returning *less*
317 than the requested amount, because cb_syscall loops calling
318 this function until the whole amount is done. */
319 if (p
->pipe_buffer
[fd
].size
+ len
> 10 * PIPE_BUF
)
321 p
->last_errno
= EFBIG
;
325 p
->pipe_buffer
[fd
].buffer
326 = xrealloc (p
->pipe_buffer
[fd
].buffer
, p
->pipe_buffer
[fd
].size
+ len
);
327 memcpy (p
->pipe_buffer
[fd
].buffer
+ p
->pipe_buffer
[fd
].size
,
329 p
->pipe_buffer
[fd
].size
+= len
;
331 (*p
->pipe_nonempty
) (p
, reader
, fd
);
335 real_fd
= fdmap (p
, fd
);
339 result
= write (real_fd
, buf
, len
);
340 p
->last_errno
= errno
;
343 result
= p
->write_stdout (p
, buf
, len
);
346 result
= p
->write_stderr (p
, buf
, len
);
353 os_write_stdout (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
355 return fwrite (buf
, 1, len
, stdout
);
359 os_flush_stdout (host_callback
*p ATTRIBUTE_UNUSED
)
365 os_write_stderr (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
367 return fwrite (buf
, 1, len
, stderr
);
371 os_flush_stderr (host_callback
*p ATTRIBUTE_UNUSED
)
377 os_rename (host_callback
*p
, const char *f1
, const char *f2
)
381 result
= rename (f1
, f2
);
382 p
->last_errno
= errno
;
388 os_system (host_callback
*p
, const char *s
)
393 p
->last_errno
= errno
;
398 os_time (host_callback
*p
)
402 result
= time (NULL
);
403 p
->last_errno
= errno
;
409 os_unlink (host_callback
*p
, const char *f1
)
413 result
= unlink (f1
);
414 p
->last_errno
= errno
;
419 os_stat (host_callback
*p
, const char *file
, struct stat
*buf
)
423 /* ??? There is an issue of when to translate to the target layout.
424 One could do that inside this function, or one could have the
425 caller do it. It's more flexible to let the caller do it, though
426 I'm not sure the flexibility will ever be useful. */
427 result
= stat (file
, buf
);
428 p
->last_errno
= errno
;
433 os_fstat (host_callback
*p
, int fd
, struct stat
*buf
)
442 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
443 time_t t
= (*p
->time
) (p
);
446 /* We have to fake the struct stat contents, since the pipe is
447 made up in the simulator. */
448 memset (buf
, 0, sizeof (*buf
));
450 #ifdef HAVE_STRUCT_STAT_ST_MODE
451 buf
->st_mode
= S_IFIFO
;
454 /* If more accurate tracking than current-time is needed (for
455 example, on GNU/Linux we get accurate numbers), the p->time
456 callback (which may be something other than os_time) should
457 happen for each read and write, and we'd need to keep track of
458 atime, ctime and mtime. */
459 #ifdef HAVE_STRUCT_STAT_ST_ATIME
462 #ifdef HAVE_STRUCT_STAT_ST_CTIME
465 #ifdef HAVE_STRUCT_STAT_ST_MTIME
471 /* ??? There is an issue of when to translate to the target layout.
472 One could do that inside this function, or one could have the
473 caller do it. It's more flexible to let the caller do it, though
474 I'm not sure the flexibility will ever be useful. */
475 result
= fstat (fdmap (p
, fd
), buf
);
476 p
->last_errno
= errno
;
481 os_lstat (host_callback
*p
, const char *file
, struct stat
*buf
)
485 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
487 result
= lstat (file
, buf
);
489 result
= stat (file
, buf
);
491 p
->last_errno
= errno
;
496 os_ftruncate (host_callback
*p
, int fd
, int64_t len
)
500 result
= fdbad (p
, fd
);
503 p
->last_errno
= EINVAL
;
508 #ifdef HAVE_FTRUNCATE
509 result
= ftruncate (fdmap (p
, fd
), len
);
510 p
->last_errno
= errno
;
512 p
->last_errno
= EINVAL
;
519 os_truncate (host_callback
*p
, const char *file
, int64_t len
)
524 result
= truncate (file
, len
);
525 p
->last_errno
= errno
;
528 p
->last_errno
= EINVAL
;
534 os_getpid (host_callback
*p
)
539 /* POSIX says getpid always succeeds. */
545 os_kill (host_callback
*p
, int pid
, int signum
)
549 result
= kill (pid
, signum
);
550 p
->last_errno
= errno
;
555 os_pipe (host_callback
*p
, int *filedes
)
559 /* We deliberately don't use fd 0. It's probably stdin anyway. */
560 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
564 if (p
->fd_buddy
[i
] < 0)
565 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
566 if (p
->fd_buddy
[j
] < 0)
568 /* Found two free fd:s. Set stat to allocated and mark
577 /* Poison the FD map to make bugs apparent. */
584 p
->last_errno
= EMFILE
;
588 /* Stub functions for pipe support. They should always be overridden in
589 targets using the pipe support, but that's up to the target. */
591 /* Called when the simulator says that the pipe at (reader, writer) is
592 now empty (so the writer should leave its waiting state). */
595 os_pipe_empty (host_callback
*p
, int reader
, int writer
)
599 /* Called when the simulator says the pipe at (reader, writer) is now
600 non-empty (so the writer should wait). */
603 os_pipe_nonempty (host_callback
*p
, int reader
, int writer
)
608 os_shutdown (host_callback
*p
)
611 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
615 /* Zero out all pipe state. Don't call callbacks for non-empty
616 pipes; the target program has likely terminated at this point
617 or we're called at initialization time. */
619 p
->pipe_buffer
[i
].size
= 0;
620 p
->pipe_buffer
[i
].buffer
= NULL
;
622 next
= p
->fd_buddy
[i
];
628 if (j
== MAX_CALLBACK_FDS
)
630 next
= p
->fd_buddy
[j
];
632 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
648 os_init (host_callback
*p
)
653 for (i
= 0; i
< 3; i
++)
656 p
->fd_buddy
[i
] = i
- 1;
658 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
659 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
661 p
->syscall_map
= cb_init_syscall_map
;
662 p
->errno_map
= cb_init_errno_map
;
663 p
->signal_map
= cb_init_signal_map
;
664 p
->open_map
= cb_init_open_map
;
672 static void ATTRIBUTE_PRINTF (2, 3)
673 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
676 va_start (args
, format
);
678 vfprintf (stdout
, format
, args
);
683 static void ATTRIBUTE_PRINTF (2, 0)
684 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
686 vprintf (format
, args
);
690 static void ATTRIBUTE_PRINTF (2, 0)
691 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
693 vfprintf (stderr
, format
, args
);
697 static void ATTRIBUTE_PRINTF (2, 3) ATTRIBUTE_NORETURN
698 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
701 va_start (args
, format
);
703 vfprintf (stderr
, format
, args
);
704 fprintf (stderr
, "\n");
710 host_callback default_callback
=
748 os_printf_filtered
, /* deprecated */
751 os_evprintf_filtered
,
757 { -1, }, /* fd_buddy */
759 { { 0, 0 }, }, /* pipe_buffer */
767 /* Defaults expected to be overridden at initialization, where needed. */
768 BFD_ENDIAN_UNKNOWN
, /* target_endian */
769 4, /* target_sizeof_int */
774 /* Read in a file describing the target's system call values.
775 E.g. maybe someone will want to use something other than newlib.
776 This assumes that the basic system call recognition and value passing/
777 returning is supported. So maybe some coding/recompilation will be
778 necessary, but not as much.
780 If an error occurs, the existing mapping is not changed. */
783 cb_read_target_syscall_maps (host_callback
*cb
, const char *file
)
785 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
786 const char *stat_map
;
789 if ((f
= fopen (file
, "r")) == NULL
)
792 /* ... read in and parse file ... */
795 return CB_RC_NO_MEM
; /* FIXME:wip */
797 /* Free storage allocated for any existing maps. */
799 free (cb
->syscall_map
);
801 free (cb
->errno_map
);
805 free (cb
->signal_map
);
807 free ((PTR
) cb
->stat_map
);
809 cb
->syscall_map
= syscall_map
;
810 cb
->errno_map
= errno_map
;
811 cb
->open_map
= open_map
;
812 cb
->signal_map
= signal_map
;
813 cb
->stat_map
= stat_map
;
818 /* General utility functions to search a map for a value. */
820 static const CB_TARGET_DEFS_MAP
*
821 cb_target_map_entry (const CB_TARGET_DEFS_MAP map
[], int target_val
)
823 const CB_TARGET_DEFS_MAP
*m
;
825 for (m
= &map
[0]; m
->target_val
!= -1; ++m
)
826 if (m
->target_val
== target_val
)
832 static const CB_TARGET_DEFS_MAP
*
833 cb_host_map_entry (const CB_TARGET_DEFS_MAP map
[], int host_val
)
835 const CB_TARGET_DEFS_MAP
*m
;
837 for (m
= &map
[0]; m
->host_val
!= -1; ++m
)
838 if (m
->host_val
== host_val
)
844 /* Translate the target's version of a syscall number to the host's.
845 This isn't actually the host's version, rather a canonical form.
846 ??? Perhaps this should be renamed to ..._canon_syscall. */
849 cb_target_to_host_syscall (host_callback
*cb
, int target_val
)
851 const CB_TARGET_DEFS_MAP
*m
=
852 cb_target_map_entry (cb
->syscall_map
, target_val
);
854 return m
? m
->host_val
: -1;
857 /* FIXME: sort tables if large.
858 Alternatively, an obvious improvement for errno conversion is
859 to machine generate a function with a large switch(). */
861 /* Translate the host's version of errno to the target's. */
864 cb_host_to_target_errno (host_callback
*cb
, int host_val
)
866 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
868 /* ??? Which error to return in this case is up for grabs.
869 Note that some missing values may have standard alternatives.
870 For now return 0 and require caller to deal with it. */
871 return m
? m
->target_val
: 0;
874 /* Given a set of target bitmasks for the open system call,
875 return the host equivalent.
876 Mapping open flag values is best done by looping so there's no need
877 to machine generate this function. */
880 cb_target_to_host_open (host_callback
*cb
, int target_val
)
883 CB_TARGET_DEFS_MAP
*m
;
885 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
887 switch (m
->target_val
)
889 /* O_RDONLY can be (and usually is) 0 which needs to be treated
891 case TARGET_O_RDONLY
:
892 case TARGET_O_WRONLY
:
894 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
896 host_val
|= m
->host_val
;
897 /* Handle the host/target differentiating between binary and
898 text mode. Only one case is of importance */
899 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
900 host_val
|= O_BINARY
;
904 if ((m
->target_val
& target_val
) == m
->target_val
)
905 host_val
|= m
->host_val
;
913 /* Translate the target's version of a signal number to the host's.
914 This isn't actually the host's version, rather a canonical form.
915 ??? Perhaps this should be renamed to ..._canon_signal. */
918 cb_target_to_host_signal (host_callback
*cb
, int target_val
)
920 const CB_TARGET_DEFS_MAP
*m
=
921 cb_target_map_entry (cb
->signal_map
, target_val
);
923 return m
? m
->host_val
: -1;
926 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
929 ??? The "val" must be as big as target word size. */
932 cb_store_target_endian (host_callback
*cb
, char *p
, int size
, long val
)
934 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
953 /* Translate a host's stat struct into a target's.
954 If HS is NULL, just compute the length of the buffer required,
957 The result is the size of the target's stat struct,
958 or zero if an error occurred during the translation. */
961 cb_host_to_target_stat (host_callback
*cb
, const struct stat
*hs
, void *ts
)
963 const char *m
= cb
->stat_map
;
972 char *q
= strchr (m
, ',');
975 /* FIXME: Use sscanf? */
978 /* FIXME: print error message */
984 /* FIXME: print error message */
992 /* Defined here to avoid emacs indigestion on a lone "else". */
995 else if (strncmp (m, #FLD, q - m) == 0) \
996 cb_store_target_endian (cb, p, size, hs->FLD)
998 #ifdef HAVE_STRUCT_STAT_ST_DEV
1001 #ifdef HAVE_STRUCT_STAT_ST_INO
1004 #ifdef HAVE_STRUCT_STAT_ST_MODE
1007 #ifdef HAVE_STRUCT_STAT_ST_NLINK
1010 #ifdef HAVE_STRUCT_STAT_ST_UID
1013 #ifdef HAVE_STRUCT_STAT_ST_GID
1016 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1019 #ifdef HAVE_STRUCT_STAT_ST_SIZE
1022 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1025 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1028 #ifdef HAVE_STRUCT_STAT_ST_ATIME
1031 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1034 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1040 /* Unsupported field, store 0. */
1041 cb_store_target_endian (cb
, p
, size
, 0);
1045 m
= strchr (q
, ':');
1050 return p
- (char *) ts
;
1054 cb_is_stdin (host_callback
*cb
, int fd
)
1056 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 0;
1060 cb_is_stdout (host_callback
*cb
, int fd
)
1062 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 1;
1066 cb_is_stderr (host_callback
*cb
, int fd
)
1068 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 2;
1072 cb_host_str_syscall (host_callback
*cb
, int host_val
)
1074 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->syscall_map
, host_val
);
1076 return m
? m
->name
: NULL
;
1080 cb_host_str_errno (host_callback
*cb
, int host_val
)
1082 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
1084 return m
? m
->name
: NULL
;
1088 cb_host_str_signal (host_callback
*cb
, int host_val
)
1090 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->signal_map
, host_val
);
1092 return m
? m
->name
: NULL
;
1096 cb_target_str_syscall (host_callback
*cb
, int target_val
)
1098 const CB_TARGET_DEFS_MAP
*m
=
1099 cb_target_map_entry (cb
->syscall_map
, target_val
);
1101 return m
? m
->name
: NULL
;
1105 cb_target_str_errno (host_callback
*cb
, int target_val
)
1107 const CB_TARGET_DEFS_MAP
*m
=
1108 cb_target_map_entry (cb
->errno_map
, target_val
);
1110 return m
? m
->name
: NULL
;
1114 cb_target_str_signal (host_callback
*cb
, int target_val
)
1116 const CB_TARGET_DEFS_MAP
*m
=
1117 cb_target_map_entry (cb
->signal_map
, target_val
);
1119 return m
? m
->name
: NULL
;