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_getpid (host_callback
*p
)
565 /* POSIX says getpid always succeeds. */
571 os_pipe (host_callback
*p
, int *filedes
)
575 /* We deliberately don't use fd 0. It's probably stdin anyway. */
576 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
580 if (p
->fd_buddy
[i
] < 0)
581 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
582 if (p
->fd_buddy
[j
] < 0)
584 /* Found two free fd:s. Set stat to allocated and mark
593 /* Poison the FD map to make bugs apparent. */
600 p
->last_errno
= EMFILE
;
604 /* Stub functions for pipe support. They should always be overridden in
605 targets using the pipe support, but that's up to the target. */
607 /* Called when the simulator says that the pipe at (reader, writer) is
608 now empty (so the writer should leave its waiting state). */
611 os_pipe_empty (host_callback
*p
, int reader
, int writer
)
615 /* Called when the simulator says the pipe at (reader, writer) is now
616 non-empty (so the writer should wait). */
619 os_pipe_nonempty (host_callback
*p
, int reader
, int writer
)
624 os_shutdown (host_callback
*p
)
627 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
631 /* Zero out all pipe state. Don't call callbacks for non-empty
632 pipes; the target program has likely terminated at this point
633 or we're called at initialization time. */
635 p
->pipe_buffer
[i
].size
= 0;
636 p
->pipe_buffer
[i
].buffer
= NULL
;
638 next
= p
->fd_buddy
[i
];
644 if (j
== MAX_CALLBACK_FDS
)
646 next
= p
->fd_buddy
[j
];
648 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
664 os_init (host_callback
*p
)
669 for (i
= 0; i
< 3; i
++)
672 p
->fd_buddy
[i
] = i
- 1;
674 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
675 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
677 p
->syscall_map
= cb_init_syscall_map
;
678 p
->errno_map
= cb_init_errno_map
;
679 p
->open_map
= cb_init_open_map
;
687 static void ATTRIBUTE_PRINTF (2, 3)
688 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
691 va_start (args
, format
);
693 vfprintf (stdout
, format
, args
);
698 static void ATTRIBUTE_PRINTF (2, 0)
699 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
701 vprintf (format
, args
);
705 static void ATTRIBUTE_PRINTF (2, 0)
706 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
708 vfprintf (stderr
, format
, args
);
712 static void ATTRIBUTE_PRINTF (2, 3) ATTRIBUTE_NORETURN
713 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
716 va_start (args
, format
);
718 vfprintf (stderr
, format
, args
);
719 fprintf (stderr
, "\n");
725 host_callback default_callback
=
762 os_printf_filtered
, /* deprecated */
765 os_evprintf_filtered
,
771 { -1, }, /* fd_buddy */
773 { { 0, 0 }, }, /* pipe_buffer */
781 /* Defaults expected to be overridden at initialization, where needed. */
782 BFD_ENDIAN_UNKNOWN
, /* target_endian */
783 4, /* target_sizeof_int */
788 /* Read in a file describing the target's system call values.
789 E.g. maybe someone will want to use something other than newlib.
790 This assumes that the basic system call recognition and value passing/
791 returning is supported. So maybe some coding/recompilation will be
792 necessary, but not as much.
794 If an error occurs, the existing mapping is not changed. */
797 cb_read_target_syscall_maps (host_callback
*cb
, const char *file
)
799 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
800 const char *stat_map
;
803 if ((f
= fopen (file
, "r")) == NULL
)
806 /* ... read in and parse file ... */
809 return CB_RC_NO_MEM
; /* FIXME:wip */
811 /* Free storage allocated for any existing maps. */
813 free (cb
->syscall_map
);
815 free (cb
->errno_map
);
819 free (cb
->signal_map
);
821 free ((PTR
) cb
->stat_map
);
823 cb
->syscall_map
= syscall_map
;
824 cb
->errno_map
= errno_map
;
825 cb
->open_map
= open_map
;
826 cb
->signal_map
= signal_map
;
827 cb
->stat_map
= stat_map
;
832 /* General utility functions to search a map for a value. */
834 static const CB_TARGET_DEFS_MAP
*
835 cb_target_map_entry (const CB_TARGET_DEFS_MAP map
[], int target_val
)
837 const CB_TARGET_DEFS_MAP
*m
;
839 for (m
= &map
[0]; m
->target_val
!= -1; ++m
)
840 if (m
->target_val
== target_val
)
846 static const CB_TARGET_DEFS_MAP
*
847 cb_host_map_entry (const CB_TARGET_DEFS_MAP map
[], int host_val
)
849 const CB_TARGET_DEFS_MAP
*m
;
851 for (m
= &map
[0]; m
->host_val
!= -1; ++m
)
852 if (m
->host_val
== host_val
)
858 /* Translate the target's version of a syscall number to the host's.
859 This isn't actually the host's version, rather a canonical form.
860 ??? Perhaps this should be renamed to ..._canon_syscall. */
863 cb_target_to_host_syscall (host_callback
*cb
, int target_val
)
865 const CB_TARGET_DEFS_MAP
*m
=
866 cb_target_map_entry (cb
->syscall_map
, target_val
);
868 return m
? m
->host_val
: -1;
871 /* FIXME: sort tables if large.
872 Alternatively, an obvious improvement for errno conversion is
873 to machine generate a function with a large switch(). */
875 /* Translate the host's version of errno to the target's. */
878 cb_host_to_target_errno (host_callback
*cb
, int host_val
)
880 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
882 /* ??? Which error to return in this case is up for grabs.
883 Note that some missing values may have standard alternatives.
884 For now return 0 and require caller to deal with it. */
885 return m
? m
->target_val
: 0;
888 /* Given a set of target bitmasks for the open system call,
889 return the host equivalent.
890 Mapping open flag values is best done by looping so there's no need
891 to machine generate this function. */
894 cb_target_to_host_open (host_callback
*cb
, int target_val
)
897 CB_TARGET_DEFS_MAP
*m
;
899 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
901 switch (m
->target_val
)
903 /* O_RDONLY can be (and usually is) 0 which needs to be treated
905 case TARGET_O_RDONLY
:
906 case TARGET_O_WRONLY
:
908 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
910 host_val
|= m
->host_val
;
911 /* Handle the host/target differentiating between binary and
912 text mode. Only one case is of importance */
913 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
914 host_val
|= O_BINARY
;
918 if ((m
->target_val
& target_val
) == m
->target_val
)
919 host_val
|= m
->host_val
;
927 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
930 ??? The "val" must be as big as target word size. */
933 cb_store_target_endian (host_callback
*cb
, char *p
, int size
, long val
)
935 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
954 /* Translate a host's stat struct into a target's.
955 If HS is NULL, just compute the length of the buffer required,
958 The result is the size of the target's stat struct,
959 or zero if an error occurred during the translation. */
962 cb_host_to_target_stat (host_callback
*cb
, const struct stat
*hs
, void *ts
)
964 const char *m
= cb
->stat_map
;
973 char *q
= strchr (m
, ',');
976 /* FIXME: Use sscanf? */
979 /* FIXME: print error message */
985 /* FIXME: print error message */
993 /* Defined here to avoid emacs indigestion on a lone "else". */
996 else if (strncmp (m, #FLD, q - m) == 0) \
997 cb_store_target_endian (cb, p, size, hs->FLD)
999 #ifdef HAVE_STRUCT_STAT_ST_DEV
1002 #ifdef HAVE_STRUCT_STAT_ST_INO
1005 #ifdef HAVE_STRUCT_STAT_ST_MODE
1008 #ifdef HAVE_STRUCT_STAT_ST_NLINK
1011 #ifdef HAVE_STRUCT_STAT_ST_UID
1014 #ifdef HAVE_STRUCT_STAT_ST_GID
1017 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1020 #ifdef HAVE_STRUCT_STAT_ST_SIZE
1023 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1026 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1029 #ifdef HAVE_STRUCT_STAT_ST_ATIME
1032 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1035 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1041 /* Unsupported field, store 0. */
1042 cb_store_target_endian (cb
, p
, size
, 0);
1046 m
= strchr (q
, ':');
1051 return p
- (char *) ts
;
1054 /* Cover functions to the vfprintf callbacks.
1056 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
1057 a larger "remote target subsystem"] with a well defined interface, then
1058 one would think that the subsystem would provide these. However, until
1059 one is allowed to create such a subsystem (with its own source tree
1060 independent of any particular user), such a critter can't exist. Thus
1061 these functions are here for the time being. */
1064 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
1069 p
->vprintf_filtered (p
, fmt
, ap
);
1074 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
1079 p
->evprintf_filtered (p
, fmt
, ap
);
1084 cb_is_stdin (host_callback
*cb
, int fd
)
1086 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 0;
1090 cb_is_stdout (host_callback
*cb
, int fd
)
1092 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 1;
1096 cb_is_stderr (host_callback
*cb
, int fd
)
1098 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 2;
1102 cb_host_str_syscall (host_callback
*cb
, int host_val
)
1104 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->syscall_map
, host_val
);
1106 return m
? m
->name
: NULL
;
1110 cb_host_str_errno (host_callback
*cb
, int host_val
)
1112 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
1114 return m
? m
->name
: NULL
;
1118 cb_host_str_signal (host_callback
*cb
, int host_val
)
1120 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->signal_map
, host_val
);
1122 return m
? m
->name
: NULL
;
1126 cb_target_str_syscall (host_callback
*cb
, int target_val
)
1128 const CB_TARGET_DEFS_MAP
*m
=
1129 cb_target_map_entry (cb
->syscall_map
, target_val
);
1131 return m
? m
->name
: NULL
;
1135 cb_target_str_errno (host_callback
*cb
, int target_val
)
1137 const CB_TARGET_DEFS_MAP
*m
=
1138 cb_target_map_entry (cb
->errno_map
, target_val
);
1140 return m
? m
->name
: NULL
;
1144 cb_target_str_signal (host_callback
*cb
, int target_val
)
1146 const CB_TARGET_DEFS_MAP
*m
=
1147 cb_target_map_entry (cb
->signal_map
, target_val
);
1149 return m
? m
->name
: NULL
;