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 /* ??? sim_cb_printf should be cb_printf, but until the callback support is
53 broken out of the simulator directory, these are here to not require
55 void sim_cb_printf (host_callback
*, const char *, ...);
56 void sim_cb_eprintf (host_callback
*, const char *, ...);
58 extern CB_TARGET_DEFS_MAP cb_init_syscall_map
[];
59 extern CB_TARGET_DEFS_MAP cb_init_errno_map
[];
60 extern CB_TARGET_DEFS_MAP cb_init_signal_map
[];
61 extern CB_TARGET_DEFS_MAP cb_init_open_map
[];
63 /* Make sure the FD provided is ok. If not, return non-zero
67 fdbad (host_callback
*p
, int fd
)
69 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| p
->fd_buddy
[fd
] < 0)
71 p
->last_errno
= EBADF
;
78 fdmap (host_callback
*p
, int fd
)
84 os_close (host_callback
*p
, int fd
)
89 result
= fdbad (p
, fd
);
92 /* If this file descripter has one or more buddies (originals /
93 duplicates from a dup), just remove it from the circular list. */
94 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
97 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
102 int other
= p
->ispipe
[fd
];
107 /* Closing the read side. */
113 /* Closing the write side. */
118 /* If there was data in the buffer, make a last "now empty"
119 call, then deallocate data. */
120 if (p
->pipe_buffer
[writer
].buffer
!= NULL
)
122 (*p
->pipe_empty
) (p
, reader
, writer
);
123 free (p
->pipe_buffer
[writer
].buffer
);
124 p
->pipe_buffer
[writer
].buffer
= NULL
;
127 /* Clear pipe data for this side. */
128 p
->pipe_buffer
[fd
].size
= 0;
131 /* If this was the first close, mark the other side as the
132 only remaining side. */
133 if (fd
!= abs (other
))
134 p
->ispipe
[abs (other
)] = -other
;
135 p
->fd_buddy
[fd
] = -1;
139 result
= close (fdmap (p
, fd
));
140 p
->last_errno
= errno
;
142 p
->fd_buddy
[fd
] = -1;
148 /* taken from gdb/util.c:notice_quit() - should be in a library */
151 #if defined(__GO32__) || defined (_MSC_VER)
153 os_poll_quit (host_callback
*p
)
155 #if defined(__GO32__)
171 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
175 #if defined (_MSC_VER)
176 /* NB - this will not compile! */
177 int k
= win32pollquit ();
186 #define os_poll_quit 0
187 #endif /* defined(__GO32__) || defined(_MSC_VER) */
190 os_get_errno (host_callback
*p
)
192 return cb_host_to_target_errno (p
, p
->last_errno
);
197 os_isatty (host_callback
*p
, int fd
)
201 result
= fdbad (p
, fd
);
205 result
= isatty (fdmap (p
, fd
));
206 p
->last_errno
= errno
;
211 os_lseek (host_callback
*p
, int fd
, int64_t off
, int way
)
215 result
= fdbad (p
, fd
);
219 result
= lseek (fdmap (p
, fd
), off
, way
);
220 p
->last_errno
= errno
;
225 os_open (host_callback
*p
, const char *name
, int flags
)
228 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
230 if (p
->fd_buddy
[i
] < 0)
232 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
235 p
->last_errno
= errno
;
243 p
->last_errno
= EMFILE
;
248 os_read (host_callback
*p
, int fd
, char *buf
, int len
)
252 result
= fdbad (p
, fd
);
257 int writer
= p
->ispipe
[fd
];
259 /* Can't read from the write-end. */
262 p
->last_errno
= EBADF
;
266 /* Nothing to read if nothing is written. */
267 if (p
->pipe_buffer
[writer
].size
== 0)
270 /* Truncate read request size to buffer size minus what's already
272 if (len
> p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
)
273 len
= p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
;
275 memcpy (buf
, p
->pipe_buffer
[writer
].buffer
+ p
->pipe_buffer
[fd
].size
,
278 /* Account for what we just read. */
279 p
->pipe_buffer
[fd
].size
+= len
;
281 /* If we've read everything, empty and deallocate the buffer and
282 signal buffer-empty to client. (This isn't expected to be a
283 hot path in the simulator, so we don't hold on to the buffer.) */
284 if (p
->pipe_buffer
[fd
].size
== p
->pipe_buffer
[writer
].size
)
286 free (p
->pipe_buffer
[writer
].buffer
);
287 p
->pipe_buffer
[writer
].buffer
= NULL
;
288 p
->pipe_buffer
[fd
].size
= 0;
289 p
->pipe_buffer
[writer
].size
= 0;
290 (*p
->pipe_empty
) (p
, fd
, writer
);
296 result
= read (fdmap (p
, fd
), buf
, len
);
297 p
->last_errno
= errno
;
302 os_read_stdin (host_callback
*p
, char *buf
, int len
)
306 result
= read (0, buf
, len
);
307 p
->last_errno
= errno
;
312 os_write (host_callback
*p
, int fd
, const char *buf
, int len
)
317 result
= fdbad (p
, fd
);
323 int reader
= -p
->ispipe
[fd
];
325 /* Can't write to the read-end. */
328 p
->last_errno
= EBADF
;
332 /* Can't write to pipe with closed read end.
333 FIXME: We should send a SIGPIPE. */
336 p
->last_errno
= EPIPE
;
340 /* As a sanity-check, we bail out it the buffered contents is much
341 larger than the size of the buffer on the host. We don't want
342 to run out of memory in the simulator due to a target program
343 bug if we can help it. Unfortunately, regarding the value that
344 reaches the simulated program, it's no use returning *less*
345 than the requested amount, because cb_syscall loops calling
346 this function until the whole amount is done. */
347 if (p
->pipe_buffer
[fd
].size
+ len
> 10 * PIPE_BUF
)
349 p
->last_errno
= EFBIG
;
353 p
->pipe_buffer
[fd
].buffer
354 = xrealloc (p
->pipe_buffer
[fd
].buffer
, p
->pipe_buffer
[fd
].size
+ len
);
355 memcpy (p
->pipe_buffer
[fd
].buffer
+ p
->pipe_buffer
[fd
].size
,
357 p
->pipe_buffer
[fd
].size
+= len
;
359 (*p
->pipe_nonempty
) (p
, reader
, fd
);
363 real_fd
= fdmap (p
, fd
);
367 result
= write (real_fd
, buf
, len
);
368 p
->last_errno
= errno
;
371 result
= p
->write_stdout (p
, buf
, len
);
374 result
= p
->write_stderr (p
, buf
, len
);
381 os_write_stdout (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
383 return fwrite (buf
, 1, len
, stdout
);
387 os_flush_stdout (host_callback
*p ATTRIBUTE_UNUSED
)
393 os_write_stderr (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
395 return fwrite (buf
, 1, len
, stderr
);
399 os_flush_stderr (host_callback
*p ATTRIBUTE_UNUSED
)
405 os_rename (host_callback
*p
, const char *f1
, const char *f2
)
409 result
= rename (f1
, f2
);
410 p
->last_errno
= errno
;
416 os_system (host_callback
*p
, const char *s
)
421 p
->last_errno
= errno
;
426 os_time (host_callback
*p
)
430 result
= time (NULL
);
431 p
->last_errno
= errno
;
437 os_unlink (host_callback
*p
, const char *f1
)
441 result
= unlink (f1
);
442 p
->last_errno
= errno
;
447 os_stat (host_callback
*p
, const char *file
, struct stat
*buf
)
451 /* ??? There is an issue of when to translate to the target layout.
452 One could do that inside this function, or one could have the
453 caller do it. It's more flexible to let the caller do it, though
454 I'm not sure the flexibility will ever be useful. */
455 result
= stat (file
, buf
);
456 p
->last_errno
= errno
;
461 os_fstat (host_callback
*p
, int fd
, struct stat
*buf
)
470 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
471 time_t t
= (*p
->time
) (p
);
474 /* We have to fake the struct stat contents, since the pipe is
475 made up in the simulator. */
476 memset (buf
, 0, sizeof (*buf
));
478 #ifdef HAVE_STRUCT_STAT_ST_MODE
479 buf
->st_mode
= S_IFIFO
;
482 /* If more accurate tracking than current-time is needed (for
483 example, on GNU/Linux we get accurate numbers), the p->time
484 callback (which may be something other than os_time) should
485 happen for each read and write, and we'd need to keep track of
486 atime, ctime and mtime. */
487 #ifdef HAVE_STRUCT_STAT_ST_ATIME
490 #ifdef HAVE_STRUCT_STAT_ST_CTIME
493 #ifdef HAVE_STRUCT_STAT_ST_MTIME
499 /* ??? There is an issue of when to translate to the target layout.
500 One could do that inside this function, or one could have the
501 caller do it. It's more flexible to let the caller do it, though
502 I'm not sure the flexibility will ever be useful. */
503 result
= fstat (fdmap (p
, fd
), buf
);
504 p
->last_errno
= errno
;
509 os_lstat (host_callback
*p
, const char *file
, struct stat
*buf
)
513 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
515 result
= lstat (file
, buf
);
517 result
= stat (file
, buf
);
519 p
->last_errno
= errno
;
524 os_ftruncate (host_callback
*p
, int fd
, int64_t len
)
528 result
= fdbad (p
, fd
);
531 p
->last_errno
= EINVAL
;
536 #ifdef HAVE_FTRUNCATE
537 result
= ftruncate (fdmap (p
, fd
), len
);
538 p
->last_errno
= errno
;
540 p
->last_errno
= EINVAL
;
547 os_truncate (host_callback
*p
, const char *file
, int64_t len
)
552 result
= truncate (file
, len
);
553 p
->last_errno
= errno
;
556 p
->last_errno
= EINVAL
;
562 os_getpid (host_callback
*p
)
567 /* POSIX says getpid always succeeds. */
573 os_pipe (host_callback
*p
, int *filedes
)
577 /* We deliberately don't use fd 0. It's probably stdin anyway. */
578 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
582 if (p
->fd_buddy
[i
] < 0)
583 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
584 if (p
->fd_buddy
[j
] < 0)
586 /* Found two free fd:s. Set stat to allocated and mark
595 /* Poison the FD map to make bugs apparent. */
602 p
->last_errno
= EMFILE
;
606 /* Stub functions for pipe support. They should always be overridden in
607 targets using the pipe support, but that's up to the target. */
609 /* Called when the simulator says that the pipe at (reader, writer) is
610 now empty (so the writer should leave its waiting state). */
613 os_pipe_empty (host_callback
*p
, int reader
, int writer
)
617 /* Called when the simulator says the pipe at (reader, writer) is now
618 non-empty (so the writer should wait). */
621 os_pipe_nonempty (host_callback
*p
, int reader
, int writer
)
626 os_shutdown (host_callback
*p
)
629 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
633 /* Zero out all pipe state. Don't call callbacks for non-empty
634 pipes; the target program has likely terminated at this point
635 or we're called at initialization time. */
637 p
->pipe_buffer
[i
].size
= 0;
638 p
->pipe_buffer
[i
].buffer
= NULL
;
640 next
= p
->fd_buddy
[i
];
646 if (j
== MAX_CALLBACK_FDS
)
648 next
= p
->fd_buddy
[j
];
650 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
666 os_init (host_callback
*p
)
671 for (i
= 0; i
< 3; i
++)
674 p
->fd_buddy
[i
] = i
- 1;
676 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
677 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
679 p
->syscall_map
= cb_init_syscall_map
;
680 p
->errno_map
= cb_init_errno_map
;
681 p
->signal_map
= cb_init_signal_map
;
682 p
->open_map
= cb_init_open_map
;
690 static void ATTRIBUTE_PRINTF (2, 3)
691 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
694 va_start (args
, format
);
696 vfprintf (stdout
, format
, args
);
701 static void ATTRIBUTE_PRINTF (2, 0)
702 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
704 vprintf (format
, args
);
708 static void ATTRIBUTE_PRINTF (2, 0)
709 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
711 vfprintf (stderr
, format
, args
);
715 static void ATTRIBUTE_PRINTF (2, 3) ATTRIBUTE_NORETURN
716 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
719 va_start (args
, format
);
721 vfprintf (stderr
, format
, args
);
722 fprintf (stderr
, "\n");
728 host_callback default_callback
=
765 os_printf_filtered
, /* deprecated */
768 os_evprintf_filtered
,
774 { -1, }, /* fd_buddy */
776 { { 0, 0 }, }, /* pipe_buffer */
784 /* Defaults expected to be overridden at initialization, where needed. */
785 BFD_ENDIAN_UNKNOWN
, /* target_endian */
786 4, /* target_sizeof_int */
791 /* Read in a file describing the target's system call values.
792 E.g. maybe someone will want to use something other than newlib.
793 This assumes that the basic system call recognition and value passing/
794 returning is supported. So maybe some coding/recompilation will be
795 necessary, but not as much.
797 If an error occurs, the existing mapping is not changed. */
800 cb_read_target_syscall_maps (host_callback
*cb
, const char *file
)
802 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
803 const char *stat_map
;
806 if ((f
= fopen (file
, "r")) == NULL
)
809 /* ... read in and parse file ... */
812 return CB_RC_NO_MEM
; /* FIXME:wip */
814 /* Free storage allocated for any existing maps. */
816 free (cb
->syscall_map
);
818 free (cb
->errno_map
);
822 free (cb
->signal_map
);
824 free ((PTR
) cb
->stat_map
);
826 cb
->syscall_map
= syscall_map
;
827 cb
->errno_map
= errno_map
;
828 cb
->open_map
= open_map
;
829 cb
->signal_map
= signal_map
;
830 cb
->stat_map
= stat_map
;
835 /* General utility functions to search a map for a value. */
837 static const CB_TARGET_DEFS_MAP
*
838 cb_target_map_entry (const CB_TARGET_DEFS_MAP map
[], int target_val
)
840 const CB_TARGET_DEFS_MAP
*m
;
842 for (m
= &map
[0]; m
->target_val
!= -1; ++m
)
843 if (m
->target_val
== target_val
)
849 static const CB_TARGET_DEFS_MAP
*
850 cb_host_map_entry (const CB_TARGET_DEFS_MAP map
[], int host_val
)
852 const CB_TARGET_DEFS_MAP
*m
;
854 for (m
= &map
[0]; m
->host_val
!= -1; ++m
)
855 if (m
->host_val
== host_val
)
861 /* Translate the target's version of a syscall number to the host's.
862 This isn't actually the host's version, rather a canonical form.
863 ??? Perhaps this should be renamed to ..._canon_syscall. */
866 cb_target_to_host_syscall (host_callback
*cb
, int target_val
)
868 const CB_TARGET_DEFS_MAP
*m
=
869 cb_target_map_entry (cb
->syscall_map
, target_val
);
871 return m
? m
->host_val
: -1;
874 /* FIXME: sort tables if large.
875 Alternatively, an obvious improvement for errno conversion is
876 to machine generate a function with a large switch(). */
878 /* Translate the host's version of errno to the target's. */
881 cb_host_to_target_errno (host_callback
*cb
, int host_val
)
883 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
885 /* ??? Which error to return in this case is up for grabs.
886 Note that some missing values may have standard alternatives.
887 For now return 0 and require caller to deal with it. */
888 return m
? m
->target_val
: 0;
891 /* Given a set of target bitmasks for the open system call,
892 return the host equivalent.
893 Mapping open flag values is best done by looping so there's no need
894 to machine generate this function. */
897 cb_target_to_host_open (host_callback
*cb
, int target_val
)
900 CB_TARGET_DEFS_MAP
*m
;
902 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
904 switch (m
->target_val
)
906 /* O_RDONLY can be (and usually is) 0 which needs to be treated
908 case TARGET_O_RDONLY
:
909 case TARGET_O_WRONLY
:
911 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
913 host_val
|= m
->host_val
;
914 /* Handle the host/target differentiating between binary and
915 text mode. Only one case is of importance */
916 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
917 host_val
|= O_BINARY
;
921 if ((m
->target_val
& target_val
) == m
->target_val
)
922 host_val
|= m
->host_val
;
930 /* Translate the target's version of a signal number to the host's.
931 This isn't actually the host's version, rather a canonical form.
932 ??? Perhaps this should be renamed to ..._canon_signal. */
935 cb_target_to_host_signal (host_callback
*cb
, int target_val
)
937 const CB_TARGET_DEFS_MAP
*m
=
938 cb_target_map_entry (cb
->signal_map
, target_val
);
940 return m
? m
->host_val
: -1;
943 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
946 ??? The "val" must be as big as target word size. */
949 cb_store_target_endian (host_callback
*cb
, char *p
, int size
, long val
)
951 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
970 /* Translate a host's stat struct into a target's.
971 If HS is NULL, just compute the length of the buffer required,
974 The result is the size of the target's stat struct,
975 or zero if an error occurred during the translation. */
978 cb_host_to_target_stat (host_callback
*cb
, const struct stat
*hs
, void *ts
)
980 const char *m
= cb
->stat_map
;
989 char *q
= strchr (m
, ',');
992 /* FIXME: Use sscanf? */
995 /* FIXME: print error message */
1001 /* FIXME: print error message */
1009 /* Defined here to avoid emacs indigestion on a lone "else". */
1012 else if (strncmp (m, #FLD, q - m) == 0) \
1013 cb_store_target_endian (cb, p, size, hs->FLD)
1015 #ifdef HAVE_STRUCT_STAT_ST_DEV
1018 #ifdef HAVE_STRUCT_STAT_ST_INO
1021 #ifdef HAVE_STRUCT_STAT_ST_MODE
1024 #ifdef HAVE_STRUCT_STAT_ST_NLINK
1027 #ifdef HAVE_STRUCT_STAT_ST_UID
1030 #ifdef HAVE_STRUCT_STAT_ST_GID
1033 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1036 #ifdef HAVE_STRUCT_STAT_ST_SIZE
1039 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1042 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1045 #ifdef HAVE_STRUCT_STAT_ST_ATIME
1048 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1051 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1057 /* Unsupported field, store 0. */
1058 cb_store_target_endian (cb
, p
, size
, 0);
1062 m
= strchr (q
, ':');
1067 return p
- (char *) ts
;
1070 /* Cover functions to the vfprintf callbacks.
1072 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
1073 a larger "remote target subsystem"] with a well defined interface, then
1074 one would think that the subsystem would provide these. However, until
1075 one is allowed to create such a subsystem (with its own source tree
1076 independent of any particular user), such a critter can't exist. Thus
1077 these functions are here for the time being. */
1080 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
1085 p
->vprintf_filtered (p
, fmt
, ap
);
1090 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
1095 p
->evprintf_filtered (p
, fmt
, ap
);
1100 cb_is_stdin (host_callback
*cb
, int fd
)
1102 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 0;
1106 cb_is_stdout (host_callback
*cb
, int fd
)
1108 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 1;
1112 cb_is_stderr (host_callback
*cb
, int fd
)
1114 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 2;
1118 cb_host_str_syscall (host_callback
*cb
, int host_val
)
1120 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->syscall_map
, host_val
);
1122 return m
? m
->name
: NULL
;
1126 cb_host_str_errno (host_callback
*cb
, int host_val
)
1128 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
1130 return m
? m
->name
: NULL
;
1134 cb_host_str_signal (host_callback
*cb
, int host_val
)
1136 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->signal_map
, host_val
);
1138 return m
? m
->name
: NULL
;
1142 cb_target_str_syscall (host_callback
*cb
, int target_val
)
1144 const CB_TARGET_DEFS_MAP
*m
=
1145 cb_target_map_entry (cb
->syscall_map
, target_val
);
1147 return m
? m
->name
: NULL
;
1151 cb_target_str_errno (host_callback
*cb
, int target_val
)
1153 const CB_TARGET_DEFS_MAP
*m
=
1154 cb_target_map_entry (cb
->errno_map
, target_val
);
1156 return m
? m
->name
: NULL
;
1160 cb_target_str_signal (host_callback
*cb
, int target_val
)
1162 const CB_TARGET_DEFS_MAP
*m
=
1163 cb_target_map_entry (cb
->signal_map
, target_val
);
1165 return m
? m
->name
: NULL
;