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. */
36 #include <sys/types.h>
38 #include "sim/callback.h"
39 #include "targ-vals.h"
41 #include "libiberty.h"
51 /* ??? sim_cb_printf should be cb_printf, but until the callback support is
52 broken out of the simulator directory, these are here to not require
54 void sim_cb_printf (host_callback
*, const char *, ...);
55 void sim_cb_eprintf (host_callback
*, const char *, ...);
57 extern CB_TARGET_DEFS_MAP cb_init_syscall_map
[];
58 extern CB_TARGET_DEFS_MAP cb_init_errno_map
[];
59 extern CB_TARGET_DEFS_MAP cb_init_open_map
[];
61 /* Make sure the FD provided is ok. If not, return non-zero
65 fdbad (host_callback
*p
, int fd
)
67 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| p
->fd_buddy
[fd
] < 0)
69 p
->last_errno
= EBADF
;
76 fdmap (host_callback
*p
, int fd
)
82 os_close (host_callback
*p
, int fd
)
87 result
= fdbad (p
, fd
);
90 /* If this file descripter has one or more buddies (originals /
91 duplicates from a dup), just remove it from the circular list. */
92 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
95 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
100 int other
= p
->ispipe
[fd
];
105 /* Closing the read side. */
111 /* Closing the write side. */
116 /* If there was data in the buffer, make a last "now empty"
117 call, then deallocate data. */
118 if (p
->pipe_buffer
[writer
].buffer
!= NULL
)
120 (*p
->pipe_empty
) (p
, reader
, writer
);
121 free (p
->pipe_buffer
[writer
].buffer
);
122 p
->pipe_buffer
[writer
].buffer
= NULL
;
125 /* Clear pipe data for this side. */
126 p
->pipe_buffer
[fd
].size
= 0;
129 /* If this was the first close, mark the other side as the
130 only remaining side. */
131 if (fd
!= abs (other
))
132 p
->ispipe
[abs (other
)] = -other
;
133 p
->fd_buddy
[fd
] = -1;
137 result
= close (fdmap (p
, fd
));
138 p
->last_errno
= errno
;
140 p
->fd_buddy
[fd
] = -1;
146 /* taken from gdb/util.c:notice_quit() - should be in a library */
149 #if defined(__GO32__) || defined (_MSC_VER)
151 os_poll_quit (host_callback
*p
)
153 #if defined(__GO32__)
169 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
173 #if defined (_MSC_VER)
174 /* NB - this will not compile! */
175 int k
= win32pollquit ();
184 #define os_poll_quit 0
185 #endif /* defined(__GO32__) || defined(_MSC_VER) */
188 os_get_errno (host_callback
*p
)
190 return cb_host_to_target_errno (p
, p
->last_errno
);
195 os_isatty (host_callback
*p
, int fd
)
199 result
= fdbad (p
, fd
);
203 result
= isatty (fdmap (p
, fd
));
204 p
->last_errno
= errno
;
209 os_lseek (host_callback
*p
, int fd
, int64_t off
, int way
)
213 result
= fdbad (p
, fd
);
217 result
= lseek (fdmap (p
, fd
), off
, way
);
218 p
->last_errno
= errno
;
223 os_open (host_callback
*p
, const char *name
, int flags
)
226 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
228 if (p
->fd_buddy
[i
] < 0)
230 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
233 p
->last_errno
= errno
;
241 p
->last_errno
= EMFILE
;
246 os_read (host_callback
*p
, int fd
, char *buf
, int len
)
250 result
= fdbad (p
, fd
);
255 int writer
= p
->ispipe
[fd
];
257 /* Can't read from the write-end. */
260 p
->last_errno
= EBADF
;
264 /* Nothing to read if nothing is written. */
265 if (p
->pipe_buffer
[writer
].size
== 0)
268 /* Truncate read request size to buffer size minus what's already
270 if (len
> p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
)
271 len
= p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
;
273 memcpy (buf
, p
->pipe_buffer
[writer
].buffer
+ p
->pipe_buffer
[fd
].size
,
276 /* Account for what we just read. */
277 p
->pipe_buffer
[fd
].size
+= len
;
279 /* If we've read everything, empty and deallocate the buffer and
280 signal buffer-empty to client. (This isn't expected to be a
281 hot path in the simulator, so we don't hold on to the buffer.) */
282 if (p
->pipe_buffer
[fd
].size
== p
->pipe_buffer
[writer
].size
)
284 free (p
->pipe_buffer
[writer
].buffer
);
285 p
->pipe_buffer
[writer
].buffer
= NULL
;
286 p
->pipe_buffer
[fd
].size
= 0;
287 p
->pipe_buffer
[writer
].size
= 0;
288 (*p
->pipe_empty
) (p
, fd
, writer
);
294 result
= read (fdmap (p
, fd
), buf
, len
);
295 p
->last_errno
= errno
;
300 os_read_stdin (host_callback
*p
, char *buf
, int len
)
304 result
= read (0, buf
, len
);
305 p
->last_errno
= errno
;
310 os_write (host_callback
*p
, int fd
, const char *buf
, int len
)
315 result
= fdbad (p
, fd
);
321 int reader
= -p
->ispipe
[fd
];
323 /* Can't write to the read-end. */
326 p
->last_errno
= EBADF
;
330 /* Can't write to pipe with closed read end.
331 FIXME: We should send a SIGPIPE. */
334 p
->last_errno
= EPIPE
;
338 /* As a sanity-check, we bail out it the buffered contents is much
339 larger than the size of the buffer on the host. We don't want
340 to run out of memory in the simulator due to a target program
341 bug if we can help it. Unfortunately, regarding the value that
342 reaches the simulated program, it's no use returning *less*
343 than the requested amount, because cb_syscall loops calling
344 this function until the whole amount is done. */
345 if (p
->pipe_buffer
[fd
].size
+ len
> 10 * PIPE_BUF
)
347 p
->last_errno
= EFBIG
;
351 p
->pipe_buffer
[fd
].buffer
352 = xrealloc (p
->pipe_buffer
[fd
].buffer
, p
->pipe_buffer
[fd
].size
+ len
);
353 memcpy (p
->pipe_buffer
[fd
].buffer
+ p
->pipe_buffer
[fd
].size
,
355 p
->pipe_buffer
[fd
].size
+= len
;
357 (*p
->pipe_nonempty
) (p
, reader
, fd
);
361 real_fd
= fdmap (p
, fd
);
365 result
= write (real_fd
, buf
, len
);
366 p
->last_errno
= errno
;
369 result
= p
->write_stdout (p
, buf
, len
);
372 result
= p
->write_stderr (p
, buf
, len
);
379 os_write_stdout (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
381 return fwrite (buf
, 1, len
, stdout
);
385 os_flush_stdout (host_callback
*p ATTRIBUTE_UNUSED
)
391 os_write_stderr (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
393 return fwrite (buf
, 1, len
, stderr
);
397 os_flush_stderr (host_callback
*p ATTRIBUTE_UNUSED
)
403 os_rename (host_callback
*p
, const char *f1
, const char *f2
)
407 result
= rename (f1
, f2
);
408 p
->last_errno
= errno
;
414 os_system (host_callback
*p
, const char *s
)
419 p
->last_errno
= errno
;
424 os_time (host_callback
*p
)
428 result
= time (NULL
);
429 p
->last_errno
= errno
;
435 os_unlink (host_callback
*p
, const char *f1
)
439 result
= unlink (f1
);
440 p
->last_errno
= errno
;
445 os_stat (host_callback
*p
, const char *file
, struct stat
*buf
)
449 /* ??? There is an issue of when to translate to the target layout.
450 One could do that inside this function, or one could have the
451 caller do it. It's more flexible to let the caller do it, though
452 I'm not sure the flexibility will ever be useful. */
453 result
= stat (file
, buf
);
454 p
->last_errno
= errno
;
459 os_fstat (host_callback
*p
, int fd
, struct stat
*buf
)
468 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
469 time_t t
= (*p
->time
) (p
);
472 /* We have to fake the struct stat contents, since the pipe is
473 made up in the simulator. */
474 memset (buf
, 0, sizeof (*buf
));
476 #ifdef HAVE_STRUCT_STAT_ST_MODE
477 buf
->st_mode
= S_IFIFO
;
480 /* If more accurate tracking than current-time is needed (for
481 example, on GNU/Linux we get accurate numbers), the p->time
482 callback (which may be something other than os_time) should
483 happen for each read and write, and we'd need to keep track of
484 atime, ctime and mtime. */
485 #ifdef HAVE_STRUCT_STAT_ST_ATIME
488 #ifdef HAVE_STRUCT_STAT_ST_CTIME
491 #ifdef HAVE_STRUCT_STAT_ST_MTIME
497 /* ??? There is an issue of when to translate to the target layout.
498 One could do that inside this function, or one could have the
499 caller do it. It's more flexible to let the caller do it, though
500 I'm not sure the flexibility will ever be useful. */
501 result
= fstat (fdmap (p
, fd
), buf
);
502 p
->last_errno
= errno
;
507 os_lstat (host_callback
*p
, const char *file
, struct stat
*buf
)
511 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
513 result
= lstat (file
, buf
);
515 result
= stat (file
, buf
);
517 p
->last_errno
= errno
;
522 os_ftruncate (host_callback
*p
, int fd
, int64_t len
)
526 result
= fdbad (p
, fd
);
529 p
->last_errno
= EINVAL
;
534 #ifdef HAVE_FTRUNCATE
535 result
= ftruncate (fdmap (p
, fd
), len
);
536 p
->last_errno
= errno
;
538 p
->last_errno
= EINVAL
;
545 os_truncate (host_callback
*p
, const char *file
, int64_t len
)
550 result
= truncate (file
, len
);
551 p
->last_errno
= errno
;
554 p
->last_errno
= EINVAL
;
560 os_pipe (host_callback
*p
, int *filedes
)
564 /* We deliberately don't use fd 0. It's probably stdin anyway. */
565 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
569 if (p
->fd_buddy
[i
] < 0)
570 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
571 if (p
->fd_buddy
[j
] < 0)
573 /* Found two free fd:s. Set stat to allocated and mark
582 /* Poison the FD map to make bugs apparent. */
589 p
->last_errno
= EMFILE
;
593 /* Stub functions for pipe support. They should always be overridden in
594 targets using the pipe support, but that's up to the target. */
596 /* Called when the simulator says that the pipe at (reader, writer) is
597 now empty (so the writer should leave its waiting state). */
600 os_pipe_empty (host_callback
*p
, int reader
, int writer
)
604 /* Called when the simulator says the pipe at (reader, writer) is now
605 non-empty (so the writer should wait). */
608 os_pipe_nonempty (host_callback
*p
, int reader
, int writer
)
613 os_shutdown (host_callback
*p
)
616 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
620 /* Zero out all pipe state. Don't call callbacks for non-empty
621 pipes; the target program has likely terminated at this point
622 or we're called at initialization time. */
624 p
->pipe_buffer
[i
].size
= 0;
625 p
->pipe_buffer
[i
].buffer
= NULL
;
627 next
= p
->fd_buddy
[i
];
633 if (j
== MAX_CALLBACK_FDS
)
635 next
= p
->fd_buddy
[j
];
637 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
653 os_init (host_callback
*p
)
658 for (i
= 0; i
< 3; i
++)
661 p
->fd_buddy
[i
] = i
- 1;
663 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
664 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
666 p
->syscall_map
= cb_init_syscall_map
;
667 p
->errno_map
= cb_init_errno_map
;
668 p
->open_map
= cb_init_open_map
;
676 static void ATTRIBUTE_PRINTF (2, 3)
677 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
680 va_start (args
, format
);
682 vfprintf (stdout
, format
, args
);
687 static void ATTRIBUTE_PRINTF (2, 0)
688 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
690 vprintf (format
, args
);
694 static void ATTRIBUTE_PRINTF (2, 0)
695 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
697 vfprintf (stderr
, format
, args
);
701 static void ATTRIBUTE_PRINTF (2, 3) ATTRIBUTE_NORETURN
702 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
705 va_start (args
, format
);
707 vfprintf (stderr
, format
, args
);
708 fprintf (stderr
, "\n");
714 host_callback default_callback
=
749 os_printf_filtered
, /* deprecated */
752 os_evprintf_filtered
,
758 { -1, }, /* fd_buddy */
760 { { 0, 0 }, }, /* pipe_buffer */
768 /* Defaults expected to be overridden at initialization, where needed. */
769 BFD_ENDIAN_UNKNOWN
, /* target_endian */
770 4, /* target_sizeof_int */
775 /* Read in a file describing the target's system call values.
776 E.g. maybe someone will want to use something other than newlib.
777 This assumes that the basic system call recognition and value passing/
778 returning is supported. So maybe some coding/recompilation will be
779 necessary, but not as much.
781 If an error occurs, the existing mapping is not changed. */
784 cb_read_target_syscall_maps (host_callback
*cb
, const char *file
)
786 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
787 const char *stat_map
;
790 if ((f
= fopen (file
, "r")) == NULL
)
793 /* ... read in and parse file ... */
796 return CB_RC_NO_MEM
; /* FIXME:wip */
798 /* Free storage allocated for any existing maps. */
800 free (cb
->syscall_map
);
802 free (cb
->errno_map
);
806 free (cb
->signal_map
);
808 free ((PTR
) cb
->stat_map
);
810 cb
->syscall_map
= syscall_map
;
811 cb
->errno_map
= errno_map
;
812 cb
->open_map
= open_map
;
813 cb
->signal_map
= signal_map
;
814 cb
->stat_map
= stat_map
;
819 /* General utility functions to search a map for a value. */
821 static const CB_TARGET_DEFS_MAP
*
822 cb_target_map_entry (const CB_TARGET_DEFS_MAP map
[], int target_val
)
824 const CB_TARGET_DEFS_MAP
*m
;
826 for (m
= &map
[0]; m
->target_val
!= -1; ++m
)
827 if (m
->target_val
== target_val
)
833 static const CB_TARGET_DEFS_MAP
*
834 cb_host_map_entry (const CB_TARGET_DEFS_MAP map
[], int host_val
)
836 const CB_TARGET_DEFS_MAP
*m
;
838 for (m
= &map
[0]; m
->host_val
!= -1; ++m
)
839 if (m
->host_val
== host_val
)
845 /* Translate the target's version of a syscall number to the host's.
846 This isn't actually the host's version, rather a canonical form.
847 ??? Perhaps this should be renamed to ..._canon_syscall. */
850 cb_target_to_host_syscall (host_callback
*cb
, int target_val
)
852 const CB_TARGET_DEFS_MAP
*m
=
853 cb_target_map_entry (cb
->syscall_map
, target_val
);
855 return m
? m
->host_val
: -1;
858 /* FIXME: sort tables if large.
859 Alternatively, an obvious improvement for errno conversion is
860 to machine generate a function with a large switch(). */
862 /* Translate the host's version of errno to the target's. */
865 cb_host_to_target_errno (host_callback
*cb
, int host_val
)
867 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
869 /* ??? Which error to return in this case is up for grabs.
870 Note that some missing values may have standard alternatives.
871 For now return 0 and require caller to deal with it. */
872 return m
? m
->target_val
: 0;
875 /* Given a set of target bitmasks for the open system call,
876 return the host equivalent.
877 Mapping open flag values is best done by looping so there's no need
878 to machine generate this function. */
881 cb_target_to_host_open (host_callback
*cb
, int target_val
)
884 CB_TARGET_DEFS_MAP
*m
;
886 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
888 switch (m
->target_val
)
890 /* O_RDONLY can be (and usually is) 0 which needs to be treated
892 case TARGET_O_RDONLY
:
893 case TARGET_O_WRONLY
:
895 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
897 host_val
|= m
->host_val
;
898 /* Handle the host/target differentiating between binary and
899 text mode. Only one case is of importance */
900 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
901 host_val
|= O_BINARY
;
905 if ((m
->target_val
& target_val
) == m
->target_val
)
906 host_val
|= m
->host_val
;
914 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
917 ??? The "val" must be as big as target word size. */
920 cb_store_target_endian (host_callback
*cb
, char *p
, int size
, long val
)
922 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
941 /* Translate a host's stat struct into a target's.
942 If HS is NULL, just compute the length of the buffer required,
945 The result is the size of the target's stat struct,
946 or zero if an error occurred during the translation. */
949 cb_host_to_target_stat (host_callback
*cb
, const struct stat
*hs
, void *ts
)
951 const char *m
= cb
->stat_map
;
960 char *q
= strchr (m
, ',');
963 /* FIXME: Use sscanf? */
966 /* FIXME: print error message */
972 /* FIXME: print error message */
980 /* Defined here to avoid emacs indigestion on a lone "else". */
983 else if (strncmp (m, #FLD, q - m) == 0) \
984 cb_store_target_endian (cb, p, size, hs->FLD)
986 #ifdef HAVE_STRUCT_STAT_ST_DEV
989 #ifdef HAVE_STRUCT_STAT_ST_INO
992 #ifdef HAVE_STRUCT_STAT_ST_MODE
995 #ifdef HAVE_STRUCT_STAT_ST_NLINK
998 #ifdef HAVE_STRUCT_STAT_ST_UID
1001 #ifdef HAVE_STRUCT_STAT_ST_GID
1004 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1007 #ifdef HAVE_STRUCT_STAT_ST_SIZE
1010 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1013 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1016 #ifdef HAVE_STRUCT_STAT_ST_ATIME
1019 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1022 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1028 /* Unsupported field, store 0. */
1029 cb_store_target_endian (cb
, p
, size
, 0);
1033 m
= strchr (q
, ':');
1038 return p
- (char *) ts
;
1041 /* Cover functions to the vfprintf callbacks.
1043 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
1044 a larger "remote target subsystem"] with a well defined interface, then
1045 one would think that the subsystem would provide these. However, until
1046 one is allowed to create such a subsystem (with its own source tree
1047 independent of any particular user), such a critter can't exist. Thus
1048 these functions are here for the time being. */
1051 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
1056 p
->vprintf_filtered (p
, fmt
, ap
);
1061 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
1066 p
->evprintf_filtered (p
, fmt
, ap
);
1071 cb_is_stdin (host_callback
*cb
, int fd
)
1073 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 0;
1077 cb_is_stdout (host_callback
*cb
, int fd
)
1079 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 1;
1083 cb_is_stderr (host_callback
*cb
, int fd
)
1085 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 2;
1089 cb_host_str_syscall (host_callback
*cb
, int host_val
)
1091 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->syscall_map
, host_val
);
1093 return m
? m
->name
: NULL
;
1097 cb_host_str_errno (host_callback
*cb
, int host_val
)
1099 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
1101 return m
? m
->name
: NULL
;
1105 cb_host_str_signal (host_callback
*cb
, int host_val
)
1107 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->signal_map
, host_val
);
1109 return m
? m
->name
: NULL
;
1113 cb_target_str_syscall (host_callback
*cb
, int target_val
)
1115 const CB_TARGET_DEFS_MAP
*m
=
1116 cb_target_map_entry (cb
->syscall_map
, target_val
);
1118 return m
? m
->name
: NULL
;
1122 cb_target_str_errno (host_callback
*cb
, int target_val
)
1124 const CB_TARGET_DEFS_MAP
*m
=
1125 cb_target_map_entry (cb
->errno_map
, target_val
);
1127 return m
? m
->name
: NULL
;
1131 cb_target_str_signal (host_callback
*cb
, int target_val
)
1133 const CB_TARGET_DEFS_MAP
*m
=
1134 cb_target_map_entry (cb
->signal_map
, target_val
);
1136 return m
? m
->name
: NULL
;