1 /* Remote target callback routines.
2 Copyright 1995-2023 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. */
39 #include <sys/types.h>
43 #include "libiberty.h"
45 #include "sim/callback.h"
51 extern CB_TARGET_DEFS_MAP cb_init_syscall_map
[];
52 extern CB_TARGET_DEFS_MAP cb_init_errno_map
[];
53 extern CB_TARGET_DEFS_MAP cb_init_signal_map
[];
54 extern CB_TARGET_DEFS_MAP cb_init_open_map
[];
56 /* Make sure the FD provided is ok. If not, return non-zero
60 fdbad (host_callback
*p
, int fd
)
62 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| p
->fd_buddy
[fd
] < 0)
64 p
->last_errno
= EBADF
;
71 fdmap (host_callback
*p
, int fd
)
77 os_close (host_callback
*p
, int fd
)
82 result
= fdbad (p
, fd
);
85 /* If this file descripter has one or more buddies (originals /
86 duplicates from a dup), just remove it from the circular list. */
87 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
90 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
95 int other
= p
->ispipe
[fd
];
100 /* Closing the read side. */
106 /* Closing the write side. */
111 /* If there was data in the buffer, make a last "now empty"
112 call, then deallocate data. */
113 if (p
->pipe_buffer
[writer
].buffer
!= NULL
)
115 (*p
->pipe_empty
) (p
, reader
, writer
);
116 free (p
->pipe_buffer
[writer
].buffer
);
117 p
->pipe_buffer
[writer
].buffer
= NULL
;
120 /* Clear pipe data for this side. */
121 p
->pipe_buffer
[fd
].size
= 0;
124 /* If this was the first close, mark the other side as the
125 only remaining side. */
126 if (fd
!= abs (other
))
127 p
->ispipe
[abs (other
)] = -other
;
128 p
->fd_buddy
[fd
] = -1;
132 result
= close (fdmap (p
, fd
));
133 p
->last_errno
= errno
;
135 p
->fd_buddy
[fd
] = -1;
141 /* taken from gdb/util.c:notice_quit() - should be in a library */
144 #if defined(_MSC_VER)
146 os_poll_quit (host_callback
*p
)
148 /* NB - this will not compile! */
149 int k
= win32pollquit ();
157 #define os_poll_quit 0
158 #endif /* defined(_MSC_VER) */
161 os_get_errno (host_callback
*p
)
163 return cb_host_to_target_errno (p
, p
->last_errno
);
168 os_isatty (host_callback
*p
, int fd
)
172 result
= fdbad (p
, fd
);
176 result
= isatty (fdmap (p
, fd
));
177 p
->last_errno
= errno
;
182 os_lseek (host_callback
*p
, int fd
, int64_t off
, int way
)
186 result
= fdbad (p
, fd
);
190 result
= lseek (fdmap (p
, fd
), off
, way
);
191 p
->last_errno
= errno
;
196 os_open (host_callback
*p
, const char *name
, int flags
)
199 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
201 if (p
->fd_buddy
[i
] < 0)
203 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
206 p
->last_errno
= errno
;
214 p
->last_errno
= EMFILE
;
219 os_read (host_callback
*p
, int fd
, char *buf
, int len
)
223 result
= fdbad (p
, fd
);
228 int writer
= p
->ispipe
[fd
];
230 /* Can't read from the write-end. */
233 p
->last_errno
= EBADF
;
237 /* Nothing to read if nothing is written. */
238 if (p
->pipe_buffer
[writer
].size
== 0)
241 /* Truncate read request size to buffer size minus what's already
243 if (len
> p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
)
244 len
= p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
;
246 memcpy (buf
, p
->pipe_buffer
[writer
].buffer
+ p
->pipe_buffer
[fd
].size
,
249 /* Account for what we just read. */
250 p
->pipe_buffer
[fd
].size
+= len
;
252 /* If we've read everything, empty and deallocate the buffer and
253 signal buffer-empty to client. (This isn't expected to be a
254 hot path in the simulator, so we don't hold on to the buffer.) */
255 if (p
->pipe_buffer
[fd
].size
== p
->pipe_buffer
[writer
].size
)
257 free (p
->pipe_buffer
[writer
].buffer
);
258 p
->pipe_buffer
[writer
].buffer
= NULL
;
259 p
->pipe_buffer
[fd
].size
= 0;
260 p
->pipe_buffer
[writer
].size
= 0;
261 (*p
->pipe_empty
) (p
, fd
, writer
);
267 result
= read (fdmap (p
, fd
), buf
, len
);
268 p
->last_errno
= errno
;
273 os_read_stdin (host_callback
*p
, char *buf
, int len
)
277 result
= read (0, buf
, len
);
278 p
->last_errno
= errno
;
283 os_write (host_callback
*p
, int fd
, const char *buf
, int len
)
288 result
= fdbad (p
, fd
);
294 int reader
= -p
->ispipe
[fd
];
296 /* Can't write to the read-end. */
299 p
->last_errno
= EBADF
;
303 /* Can't write to pipe with closed read end.
304 FIXME: We should send a SIGPIPE. */
307 p
->last_errno
= EPIPE
;
311 /* As a sanity-check, we bail out it the buffered contents is much
312 larger than the size of the buffer on the host. We don't want
313 to run out of memory in the simulator due to a target program
314 bug if we can help it. Unfortunately, regarding the value that
315 reaches the simulated program, it's no use returning *less*
316 than the requested amount, because cb_syscall loops calling
317 this function until the whole amount is done. */
318 if (p
->pipe_buffer
[fd
].size
+ len
> 10 * PIPE_BUF
)
320 p
->last_errno
= EFBIG
;
324 p
->pipe_buffer
[fd
].buffer
325 = xrealloc (p
->pipe_buffer
[fd
].buffer
, p
->pipe_buffer
[fd
].size
+ len
);
326 memcpy (p
->pipe_buffer
[fd
].buffer
+ p
->pipe_buffer
[fd
].size
,
328 p
->pipe_buffer
[fd
].size
+= len
;
330 (*p
->pipe_nonempty
) (p
, reader
, fd
);
334 real_fd
= fdmap (p
, fd
);
338 result
= write (real_fd
, buf
, len
);
339 p
->last_errno
= errno
;
342 result
= p
->write_stdout (p
, buf
, len
);
345 result
= p
->write_stderr (p
, buf
, len
);
352 os_write_stdout (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
354 return fwrite (buf
, 1, len
, stdout
);
358 os_flush_stdout (host_callback
*p ATTRIBUTE_UNUSED
)
364 os_write_stderr (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
366 return fwrite (buf
, 1, len
, stderr
);
370 os_flush_stderr (host_callback
*p ATTRIBUTE_UNUSED
)
376 os_rename (host_callback
*p
, const char *f1
, const char *f2
)
380 result
= rename (f1
, f2
);
381 p
->last_errno
= errno
;
387 os_system (host_callback
*p
, const char *s
)
392 p
->last_errno
= errno
;
397 os_time (host_callback
*p
)
401 result
= time (NULL
);
402 p
->last_errno
= errno
;
408 os_unlink (host_callback
*p
, const char *f1
)
412 result
= unlink (f1
);
413 p
->last_errno
= errno
;
418 os_stat (host_callback
*p
, const char *file
, struct stat
*buf
)
422 /* ??? There is an issue of when to translate to the target layout.
423 One could do that inside this function, or one could have the
424 caller do it. It's more flexible to let the caller do it, though
425 I'm not sure the flexibility will ever be useful. */
426 result
= stat (file
, buf
);
427 p
->last_errno
= errno
;
432 os_fstat (host_callback
*p
, int fd
, struct stat
*buf
)
441 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
442 time_t t
= (*p
->time
) (p
);
445 /* We have to fake the struct stat contents, since the pipe is
446 made up in the simulator. */
447 memset (buf
, 0, sizeof (*buf
));
449 #ifdef HAVE_STRUCT_STAT_ST_MODE
450 buf
->st_mode
= S_IFIFO
;
453 /* If more accurate tracking than current-time is needed (for
454 example, on GNU/Linux we get accurate numbers), the p->time
455 callback (which may be something other than os_time) should
456 happen for each read and write, and we'd need to keep track of
457 atime, ctime and mtime. */
458 #ifdef HAVE_STRUCT_STAT_ST_ATIME
461 #ifdef HAVE_STRUCT_STAT_ST_CTIME
464 #ifdef HAVE_STRUCT_STAT_ST_MTIME
470 /* ??? There is an issue of when to translate to the target layout.
471 One could do that inside this function, or one could have the
472 caller do it. It's more flexible to let the caller do it, though
473 I'm not sure the flexibility will ever be useful. */
474 result
= fstat (fdmap (p
, fd
), buf
);
475 p
->last_errno
= errno
;
480 os_lstat (host_callback
*p
, const char *file
, struct stat
*buf
)
484 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
486 result
= lstat (file
, buf
);
488 result
= stat (file
, buf
);
490 p
->last_errno
= errno
;
495 os_ftruncate (host_callback
*p
, int fd
, int64_t len
)
499 result
= fdbad (p
, fd
);
502 p
->last_errno
= EINVAL
;
507 #ifdef HAVE_FTRUNCATE
508 result
= ftruncate (fdmap (p
, fd
), len
);
509 p
->last_errno
= errno
;
511 p
->last_errno
= EINVAL
;
518 os_truncate (host_callback
*p
, const char *file
, int64_t len
)
523 result
= truncate (file
, len
);
524 p
->last_errno
= errno
;
527 p
->last_errno
= EINVAL
;
533 os_getpid (host_callback
*p
)
538 /* POSIX says getpid always succeeds. */
544 os_kill (host_callback
*p
, int pid
, int signum
)
549 result
= kill (pid
, signum
);
550 p
->last_errno
= errno
;
553 p
->last_errno
= ENOSYS
;
559 os_pipe (host_callback
*p
, int *filedes
)
563 /* We deliberately don't use fd 0. It's probably stdin anyway. */
564 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
568 if (p
->fd_buddy
[i
] < 0)
569 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
570 if (p
->fd_buddy
[j
] < 0)
572 /* Found two free fd:s. Set stat to allocated and mark
581 /* Poison the FD map to make bugs apparent. */
588 p
->last_errno
= EMFILE
;
592 /* Stub functions for pipe support. They should always be overridden in
593 targets using the pipe support, but that's up to the target. */
595 /* Called when the simulator says that the pipe at (reader, writer) is
596 now empty (so the writer should leave its waiting state). */
599 os_pipe_empty (host_callback
*p
, int reader
, int writer
)
603 /* Called when the simulator says the pipe at (reader, writer) is now
604 non-empty (so the writer should wait). */
607 os_pipe_nonempty (host_callback
*p
, int reader
, int writer
)
612 os_shutdown (host_callback
*p
)
615 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
619 /* Zero out all pipe state. Don't call callbacks for non-empty
620 pipes; the target program has likely terminated at this point
621 or we're called at initialization time. */
623 p
->pipe_buffer
[i
].size
= 0;
624 p
->pipe_buffer
[i
].buffer
= NULL
;
626 next
= p
->fd_buddy
[i
];
632 if (j
== MAX_CALLBACK_FDS
)
634 next
= p
->fd_buddy
[j
];
636 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
652 os_init (host_callback
*p
)
657 for (i
= 0; i
< 3; i
++)
660 p
->fd_buddy
[i
] = i
- 1;
662 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
663 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
665 p
->syscall_map
= cb_init_syscall_map
;
666 p
->errno_map
= cb_init_errno_map
;
667 p
->signal_map
= cb_init_signal_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
=
752 os_printf_filtered
, /* deprecated */
755 os_evprintf_filtered
,
761 { -1, }, /* fd_buddy */
763 { { 0, 0 }, }, /* pipe_buffer */
771 /* Defaults expected to be overridden at initialization, where needed. */
772 BFD_ENDIAN_UNKNOWN
, /* target_endian */
775 4, /* target_sizeof_int */
780 /* Read in a file describing the target's system call values.
781 E.g. maybe someone will want to use something other than newlib.
782 This assumes that the basic system call recognition and value passing/
783 returning is supported. So maybe some coding/recompilation will be
784 necessary, but not as much.
786 If an error occurs, the existing mapping is not changed. */
789 cb_read_target_syscall_maps (host_callback
*cb
, const char *file
)
791 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
792 const char *stat_map
;
795 if ((f
= fopen (file
, "r")) == NULL
)
798 /* ... read in and parse file ... */
801 return CB_RC_NO_MEM
; /* FIXME:wip */
803 /* Free storage allocated for any existing maps. */
805 free (cb
->syscall_map
);
807 free (cb
->errno_map
);
811 free (cb
->signal_map
);
813 free ((void *) cb
->stat_map
);
815 cb
->syscall_map
= syscall_map
;
816 cb
->errno_map
= errno_map
;
817 cb
->open_map
= open_map
;
818 cb
->signal_map
= signal_map
;
819 cb
->stat_map
= stat_map
;
824 /* General utility functions to search a map for a value. */
826 static const CB_TARGET_DEFS_MAP
*
827 cb_target_map_entry (const CB_TARGET_DEFS_MAP map
[], int target_val
)
829 const CB_TARGET_DEFS_MAP
*m
;
831 for (m
= &map
[0]; m
->target_val
!= -1; ++m
)
832 if (m
->target_val
== target_val
)
838 static const CB_TARGET_DEFS_MAP
*
839 cb_host_map_entry (const CB_TARGET_DEFS_MAP map
[], int host_val
)
841 const CB_TARGET_DEFS_MAP
*m
;
843 for (m
= &map
[0]; m
->host_val
!= -1; ++m
)
844 if (m
->host_val
== host_val
)
850 /* Translate the target's version of a syscall number to the host's.
851 This isn't actually the host's version, rather a canonical form.
852 ??? Perhaps this should be renamed to ..._canon_syscall. */
855 cb_target_to_host_syscall (host_callback
*cb
, int target_val
)
857 const CB_TARGET_DEFS_MAP
*m
=
858 cb_target_map_entry (cb
->syscall_map
, target_val
);
860 return m
? m
->host_val
: -1;
863 /* FIXME: sort tables if large.
864 Alternatively, an obvious improvement for errno conversion is
865 to machine generate a function with a large switch(). */
867 /* Translate the host's version of errno to the target's. */
870 cb_host_to_target_errno (host_callback
*cb
, int host_val
)
872 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
874 /* ??? Which error to return in this case is up for grabs.
875 Note that some missing values may have standard alternatives.
876 For now return 0 and require caller to deal with it. */
877 return m
? m
->target_val
: 0;
880 /* Given a set of target bitmasks for the open system call,
881 return the host equivalent.
882 Mapping open flag values is best done by looping so there's no need
883 to machine generate this function. */
886 cb_target_to_host_open (host_callback
*cb
, int target_val
)
889 CB_TARGET_DEFS_MAP
*m
;
896 /* O_RDONLY can be (and usually is) 0 which needs to be treated specially. */
897 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
899 if (!strcmp (m
->name
, "O_RDONLY"))
900 o_rdonly
= m
->target_val
;
901 else if (!strcmp (m
->name
, "O_WRONLY"))
902 o_wronly
= m
->target_val
;
903 else if (!strcmp (m
->name
, "O_RDWR"))
904 o_rdwr
= m
->target_val
;
905 else if (!strcmp (m
->name
, "O_BINARY"))
906 o_binary
= m
->target_val
;
908 o_rdwrmask
= o_rdonly
| o_wronly
| o_rdwr
;
910 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
912 if (m
->target_val
== o_rdonly
|| m
->target_val
== o_wronly
913 || m
->target_val
== o_rdwr
)
915 if ((target_val
& o_rdwrmask
) == m
->target_val
)
916 host_val
|= m
->host_val
;
917 /* Handle the host/target differentiating between binary and
918 text mode. Only one case is of importance */
921 host_val
|= O_BINARY
;
926 if ((m
->target_val
& target_val
) == m
->target_val
)
927 host_val
|= m
->host_val
;
934 /* Translate the target's version of a signal number to the host's.
935 This isn't actually the host's version, rather a canonical form.
936 ??? Perhaps this should be renamed to ..._canon_signal. */
939 cb_target_to_host_signal (host_callback
*cb
, int target_val
)
941 const CB_TARGET_DEFS_MAP
*m
=
942 cb_target_map_entry (cb
->signal_map
, target_val
);
944 return m
? m
->host_val
: -1;
947 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
950 ??? The "val" must be as big as target word size. */
953 cb_store_target_endian (host_callback
*cb
, char *p
, int size
, long val
)
955 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
974 /* Translate a host's stat struct into a target's.
975 If HS is NULL, just compute the length of the buffer required,
978 The result is the size of the target's stat struct,
979 or zero if an error occurred during the translation. */
982 cb_host_to_target_stat (host_callback
*cb
, const struct stat
*hs
, void *ts
)
984 const char *m
= cb
->stat_map
;
993 char *q
= strchr (m
, ',');
996 /* FIXME: Use sscanf? */
999 /* FIXME: print error message */
1002 size
= atoi (q
+ 1);
1005 /* FIXME: print error message */
1013 /* Defined here to avoid emacs indigestion on a lone "else". */
1016 else if (strncmp (m, #FLD, q - m) == 0) \
1017 cb_store_target_endian (cb, p, size, hs->FLD)
1019 #ifdef HAVE_STRUCT_STAT_ST_DEV
1022 #ifdef HAVE_STRUCT_STAT_ST_INO
1025 #ifdef HAVE_STRUCT_STAT_ST_MODE
1028 #ifdef HAVE_STRUCT_STAT_ST_NLINK
1031 #ifdef HAVE_STRUCT_STAT_ST_UID
1034 #ifdef HAVE_STRUCT_STAT_ST_GID
1037 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1040 #ifdef HAVE_STRUCT_STAT_ST_SIZE
1043 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1046 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1049 #ifdef HAVE_STRUCT_STAT_ST_ATIME
1052 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1055 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1061 /* Unsupported field, store 0. */
1062 cb_store_target_endian (cb
, p
, size
, 0);
1066 m
= strchr (q
, ':');
1071 return p
- (char *) ts
;
1075 cb_is_stdin (host_callback
*cb
, int fd
)
1077 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 0;
1081 cb_is_stdout (host_callback
*cb
, int fd
)
1083 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 1;
1087 cb_is_stderr (host_callback
*cb
, int fd
)
1089 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 2;
1093 cb_host_str_syscall (host_callback
*cb
, int host_val
)
1095 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->syscall_map
, host_val
);
1097 return m
? m
->name
: NULL
;
1101 cb_host_str_errno (host_callback
*cb
, int host_val
)
1103 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
1105 return m
? m
->name
: NULL
;
1109 cb_host_str_signal (host_callback
*cb
, int host_val
)
1111 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->signal_map
, host_val
);
1113 return m
? m
->name
: NULL
;
1117 cb_target_str_syscall (host_callback
*cb
, int target_val
)
1119 const CB_TARGET_DEFS_MAP
*m
=
1120 cb_target_map_entry (cb
->syscall_map
, target_val
);
1122 return m
? m
->name
: NULL
;
1126 cb_target_str_errno (host_callback
*cb
, int target_val
)
1128 const CB_TARGET_DEFS_MAP
*m
=
1129 cb_target_map_entry (cb
->errno_map
, target_val
);
1131 return m
? m
->name
: NULL
;
1135 cb_target_str_signal (host_callback
*cb
, int target_val
)
1137 const CB_TARGET_DEFS_MAP
*m
=
1138 cb_target_map_entry (cb
->signal_map
, target_val
);
1140 return m
? m
->name
: NULL
;