1 /* Remote target callback routines.
2 Copyright 1995-2019 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
46 #include <sys/types.h>
48 #include "gdb/callback.h"
49 #include "targ-vals.h"
51 #include "libiberty.h"
61 /* ??? sim_cb_printf should be cb_printf, but until the callback support is
62 broken out of the simulator directory, these are here to not require
64 void sim_cb_printf (host_callback
*, const char *, ...);
65 void sim_cb_eprintf (host_callback
*, const char *, ...);
67 extern CB_TARGET_DEFS_MAP cb_init_syscall_map
[];
68 extern CB_TARGET_DEFS_MAP cb_init_errno_map
[];
69 extern CB_TARGET_DEFS_MAP cb_init_open_map
[];
71 /* Set the callback copy of errno from what we see now. */
74 wrap (host_callback
*p
, int val
)
76 p
->last_errno
= errno
;
80 /* Make sure the FD provided is ok. If not, return non-zero
84 fdbad (host_callback
*p
, int fd
)
86 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| p
->fd_buddy
[fd
] < 0)
88 p
->last_errno
= EBADF
;
95 fdmap (host_callback
*p
, int fd
)
101 os_close (host_callback
*p
, int fd
)
106 result
= fdbad (p
, fd
);
109 /* If this file descripter has one or more buddies (originals /
110 duplicates from a dup), just remove it from the circular list. */
111 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
114 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
119 int other
= p
->ispipe
[fd
];
124 /* Closing the read side. */
130 /* Closing the write side. */
135 /* If there was data in the buffer, make a last "now empty"
136 call, then deallocate data. */
137 if (p
->pipe_buffer
[writer
].buffer
!= NULL
)
139 (*p
->pipe_empty
) (p
, reader
, writer
);
140 free (p
->pipe_buffer
[writer
].buffer
);
141 p
->pipe_buffer
[writer
].buffer
= NULL
;
144 /* Clear pipe data for this side. */
145 p
->pipe_buffer
[fd
].size
= 0;
148 /* If this was the first close, mark the other side as the
149 only remaining side. */
150 if (fd
!= abs (other
))
151 p
->ispipe
[abs (other
)] = -other
;
152 p
->fd_buddy
[fd
] = -1;
156 result
= wrap (p
, close (fdmap (p
, fd
)));
158 p
->fd_buddy
[fd
] = -1;
164 /* taken from gdb/util.c:notice_quit() - should be in a library */
167 #if defined(__GO32__) || defined (_MSC_VER)
169 os_poll_quit (host_callback
*p
)
171 #if defined(__GO32__)
187 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
191 #if defined (_MSC_VER)
192 /* NB - this will not compile! */
193 int k
= win32pollquit ();
202 #define os_poll_quit 0
203 #endif /* defined(__GO32__) || defined(_MSC_VER) */
206 os_get_errno (host_callback
*p
)
208 return cb_host_to_target_errno (p
, p
->last_errno
);
213 os_isatty (host_callback
*p
, int fd
)
217 result
= fdbad (p
, fd
);
220 result
= wrap (p
, isatty (fdmap (p
, fd
)));
226 os_lseek (host_callback
*p
, int fd
, long off
, int way
)
230 result
= fdbad (p
, fd
);
233 result
= wrap (p
, lseek (fdmap (p
, fd
), off
, way
));
238 os_open (host_callback
*p
, const char *name
, int flags
)
241 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
243 if (p
->fd_buddy
[i
] < 0)
245 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
248 p
->last_errno
= errno
;
256 p
->last_errno
= EMFILE
;
261 os_read (host_callback
*p
, int fd
, char *buf
, int len
)
265 result
= fdbad (p
, fd
);
270 int writer
= p
->ispipe
[fd
];
272 /* Can't read from the write-end. */
275 p
->last_errno
= EBADF
;
279 /* Nothing to read if nothing is written. */
280 if (p
->pipe_buffer
[writer
].size
== 0)
283 /* Truncate read request size to buffer size minus what's already
285 if (len
> p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
)
286 len
= p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
;
288 memcpy (buf
, p
->pipe_buffer
[writer
].buffer
+ p
->pipe_buffer
[fd
].size
,
291 /* Account for what we just read. */
292 p
->pipe_buffer
[fd
].size
+= len
;
294 /* If we've read everything, empty and deallocate the buffer and
295 signal buffer-empty to client. (This isn't expected to be a
296 hot path in the simulator, so we don't hold on to the buffer.) */
297 if (p
->pipe_buffer
[fd
].size
== p
->pipe_buffer
[writer
].size
)
299 free (p
->pipe_buffer
[writer
].buffer
);
300 p
->pipe_buffer
[writer
].buffer
= NULL
;
301 p
->pipe_buffer
[fd
].size
= 0;
302 p
->pipe_buffer
[writer
].size
= 0;
303 (*p
->pipe_empty
) (p
, fd
, writer
);
309 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
314 os_read_stdin (host_callback
*p
, char *buf
, int len
)
316 return wrap (p
, read (0, buf
, len
));
320 os_write (host_callback
*p
, int fd
, const char *buf
, int len
)
325 result
= fdbad (p
, fd
);
331 int reader
= -p
->ispipe
[fd
];
333 /* Can't write to the read-end. */
336 p
->last_errno
= EBADF
;
340 /* Can't write to pipe with closed read end.
341 FIXME: We should send a SIGPIPE. */
344 p
->last_errno
= EPIPE
;
348 /* As a sanity-check, we bail out it the buffered contents is much
349 larger than the size of the buffer on the host. We don't want
350 to run out of memory in the simulator due to a target program
351 bug if we can help it. Unfortunately, regarding the value that
352 reaches the simulated program, it's no use returning *less*
353 than the requested amount, because cb_syscall loops calling
354 this function until the whole amount is done. */
355 if (p
->pipe_buffer
[fd
].size
+ len
> 10 * PIPE_BUF
)
357 p
->last_errno
= EFBIG
;
361 p
->pipe_buffer
[fd
].buffer
362 = xrealloc (p
->pipe_buffer
[fd
].buffer
, p
->pipe_buffer
[fd
].size
+ len
);
363 memcpy (p
->pipe_buffer
[fd
].buffer
+ p
->pipe_buffer
[fd
].size
,
365 p
->pipe_buffer
[fd
].size
+= len
;
367 (*p
->pipe_nonempty
) (p
, reader
, fd
);
371 real_fd
= fdmap (p
, fd
);
375 result
= wrap (p
, write (real_fd
, buf
, len
));
378 result
= p
->write_stdout (p
, buf
, len
);
381 result
= p
->write_stderr (p
, buf
, len
);
388 os_write_stdout (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
390 return fwrite (buf
, 1, len
, stdout
);
394 os_flush_stdout (host_callback
*p ATTRIBUTE_UNUSED
)
400 os_write_stderr (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
402 return fwrite (buf
, 1, len
, stderr
);
406 os_flush_stderr (host_callback
*p ATTRIBUTE_UNUSED
)
412 os_rename (host_callback
*p
, const char *f1
, const char *f2
)
414 return wrap (p
, rename (f1
, f2
));
419 os_system (host_callback
*p
, const char *s
)
421 return wrap (p
, system (s
));
425 os_time (host_callback
*p
, long *t
)
427 return wrap (p
, time (t
));
432 os_unlink (host_callback
*p
, const char *f1
)
434 return wrap (p
, unlink (f1
));
438 os_stat (host_callback
*p
, const char *file
, struct stat
*buf
)
440 /* ??? There is an issue of when to translate to the target layout.
441 One could do that inside this function, or one could have the
442 caller do it. It's more flexible to let the caller do it, though
443 I'm not sure the flexibility will ever be useful. */
444 return wrap (p
, stat (file
, buf
));
448 os_fstat (host_callback
*p
, int fd
, struct stat
*buf
)
455 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
456 time_t t
= (*p
->time
) (p
, NULL
);
459 /* We have to fake the struct stat contents, since the pipe is
460 made up in the simulator. */
461 memset (buf
, 0, sizeof (*buf
));
463 #ifdef HAVE_STRUCT_STAT_ST_MODE
464 buf
->st_mode
= S_IFIFO
;
467 /* If more accurate tracking than current-time is needed (for
468 example, on GNU/Linux we get accurate numbers), the p->time
469 callback (which may be something other than os_time) should
470 happen for each read and write, and we'd need to keep track of
471 atime, ctime and mtime. */
472 #ifdef HAVE_STRUCT_STAT_ST_ATIME
475 #ifdef HAVE_STRUCT_STAT_ST_CTIME
478 #ifdef HAVE_STRUCT_STAT_ST_MTIME
484 /* ??? There is an issue of when to translate to the target layout.
485 One could do that inside this function, or one could have the
486 caller do it. It's more flexible to let the caller do it, though
487 I'm not sure the flexibility will ever be useful. */
488 return wrap (p
, fstat (fdmap (p
, fd
), buf
));
492 os_lstat (host_callback
*p
, const char *file
, struct stat
*buf
)
494 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
496 return wrap (p
, lstat (file
, buf
));
498 return wrap (p
, stat (file
, buf
));
503 os_ftruncate (host_callback
*p
, int fd
, long len
)
507 result
= fdbad (p
, fd
);
510 p
->last_errno
= EINVAL
;
515 #ifdef HAVE_FTRUNCATE
516 result
= wrap (p
, ftruncate (fdmap (p
, fd
), len
));
518 p
->last_errno
= EINVAL
;
525 os_truncate (host_callback
*p
, const char *file
, long len
)
528 return wrap (p
, truncate (file
, len
));
530 p
->last_errno
= EINVAL
;
536 os_pipe (host_callback
*p
, int *filedes
)
540 /* We deliberately don't use fd 0. It's probably stdin anyway. */
541 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
545 if (p
->fd_buddy
[i
] < 0)
546 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
547 if (p
->fd_buddy
[j
] < 0)
549 /* Found two free fd:s. Set stat to allocated and mark
558 /* Poison the FD map to make bugs apparent. */
565 p
->last_errno
= EMFILE
;
569 /* Stub functions for pipe support. They should always be overridden in
570 targets using the pipe support, but that's up to the target. */
572 /* Called when the simulator says that the pipe at (reader, writer) is
573 now empty (so the writer should leave its waiting state). */
576 os_pipe_empty (host_callback
*p
, int reader
, int writer
)
580 /* Called when the simulator says the pipe at (reader, writer) is now
581 non-empty (so the writer should wait). */
584 os_pipe_nonempty (host_callback
*p
, int reader
, int writer
)
589 os_shutdown (host_callback
*p
)
592 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
596 /* Zero out all pipe state. Don't call callbacks for non-empty
597 pipes; the target program has likely terminated at this point
598 or we're called at initialization time. */
600 p
->pipe_buffer
[i
].size
= 0;
601 p
->pipe_buffer
[i
].buffer
= NULL
;
603 next
= p
->fd_buddy
[i
];
609 if (j
== MAX_CALLBACK_FDS
)
611 next
= p
->fd_buddy
[j
];
613 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
629 os_init (host_callback
*p
)
634 for (i
= 0; i
< 3; i
++)
637 p
->fd_buddy
[i
] = i
- 1;
639 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
640 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
642 p
->syscall_map
= cb_init_syscall_map
;
643 p
->errno_map
= cb_init_errno_map
;
644 p
->open_map
= cb_init_open_map
;
653 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
656 va_start (args
, format
);
658 vfprintf (stdout
, format
, args
);
664 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
666 vprintf (format
, args
);
671 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
673 vfprintf (stderr
, format
, args
);
678 __attribute__ ((__noreturn__
))
681 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
684 va_start (args
, format
);
686 vfprintf (stderr
, format
, args
);
687 fprintf (stderr
, "\n");
693 host_callback default_callback
=
728 os_printf_filtered
, /* deprecated */
731 os_evprintf_filtered
,
737 { -1, }, /* fd_buddy */
739 { { 0, 0 }, }, /* pipe_buffer */
747 /* Defaults expected to be overridden at initialization, where needed. */
748 BFD_ENDIAN_UNKNOWN
, /* target_endian */
749 4, /* target_sizeof_int */
754 /* Read in a file describing the target's system call values.
755 E.g. maybe someone will want to use something other than newlib.
756 This assumes that the basic system call recognition and value passing/
757 returning is supported. So maybe some coding/recompilation will be
758 necessary, but not as much.
760 If an error occurs, the existing mapping is not changed. */
763 cb_read_target_syscall_maps (host_callback
*cb
, const char *file
)
765 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
766 const char *stat_map
;
769 if ((f
= fopen (file
, "r")) == NULL
)
772 /* ... read in and parse file ... */
775 return CB_RC_NO_MEM
; /* FIXME:wip */
777 /* Free storage allocated for any existing maps. */
779 free (cb
->syscall_map
);
781 free (cb
->errno_map
);
785 free (cb
->signal_map
);
787 free ((PTR
) cb
->stat_map
);
789 cb
->syscall_map
= syscall_map
;
790 cb
->errno_map
= errno_map
;
791 cb
->open_map
= open_map
;
792 cb
->signal_map
= signal_map
;
793 cb
->stat_map
= stat_map
;
798 /* General utility functions to search a map for a value. */
800 static const CB_TARGET_DEFS_MAP
*
801 cb_target_map_entry (const CB_TARGET_DEFS_MAP map
[], int target_val
)
803 const CB_TARGET_DEFS_MAP
*m
;
805 for (m
= &map
[0]; m
->target_val
!= -1; ++m
)
806 if (m
->target_val
== target_val
)
812 static const CB_TARGET_DEFS_MAP
*
813 cb_host_map_entry (const CB_TARGET_DEFS_MAP map
[], int host_val
)
815 const CB_TARGET_DEFS_MAP
*m
;
817 for (m
= &map
[0]; m
->host_val
!= -1; ++m
)
818 if (m
->host_val
== host_val
)
824 /* Translate the target's version of a syscall number to the host's.
825 This isn't actually the host's version, rather a canonical form.
826 ??? Perhaps this should be renamed to ..._canon_syscall. */
829 cb_target_to_host_syscall (host_callback
*cb
, int target_val
)
831 const CB_TARGET_DEFS_MAP
*m
=
832 cb_target_map_entry (cb
->syscall_map
, target_val
);
834 return m
? m
->host_val
: -1;
837 /* FIXME: sort tables if large.
838 Alternatively, an obvious improvement for errno conversion is
839 to machine generate a function with a large switch(). */
841 /* Translate the host's version of errno to the target's. */
844 cb_host_to_target_errno (host_callback
*cb
, int host_val
)
846 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
848 /* ??? Which error to return in this case is up for grabs.
849 Note that some missing values may have standard alternatives.
850 For now return 0 and require caller to deal with it. */
851 return m
? m
->target_val
: 0;
854 /* Given a set of target bitmasks for the open system call,
855 return the host equivalent.
856 Mapping open flag values is best done by looping so there's no need
857 to machine generate this function. */
860 cb_target_to_host_open (host_callback
*cb
, int target_val
)
863 CB_TARGET_DEFS_MAP
*m
;
865 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
867 switch (m
->target_val
)
869 /* O_RDONLY can be (and usually is) 0 which needs to be treated
871 case TARGET_O_RDONLY
:
872 case TARGET_O_WRONLY
:
874 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
876 host_val
|= m
->host_val
;
877 /* Handle the host/target differentiating between binary and
878 text mode. Only one case is of importance */
879 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
880 host_val
|= O_BINARY
;
884 if ((m
->target_val
& target_val
) == m
->target_val
)
885 host_val
|= m
->host_val
;
893 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
896 ??? The "val" must be as big as target word size. */
899 cb_store_target_endian (host_callback
*cb
, char *p
, int size
, long val
)
901 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
920 /* Translate a host's stat struct into a target's.
921 If HS is NULL, just compute the length of the buffer required,
924 The result is the size of the target's stat struct,
925 or zero if an error occurred during the translation. */
928 cb_host_to_target_stat (host_callback
*cb
, const struct stat
*hs
, PTR ts
)
930 const char *m
= cb
->stat_map
;
939 char *q
= strchr (m
, ',');
942 /* FIXME: Use sscanf? */
945 /* FIXME: print error message */
951 /* FIXME: print error message */
959 /* Defined here to avoid emacs indigestion on a lone "else". */
962 else if (strncmp (m, #FLD, q - m) == 0) \
963 cb_store_target_endian (cb, p, size, hs->FLD)
965 #ifdef HAVE_STRUCT_STAT_ST_DEV
968 #ifdef HAVE_STRUCT_STAT_ST_INO
971 #ifdef HAVE_STRUCT_STAT_ST_MODE
974 #ifdef HAVE_STRUCT_STAT_ST_NLINK
977 #ifdef HAVE_STRUCT_STAT_ST_UID
980 #ifdef HAVE_STRUCT_STAT_ST_GID
983 #ifdef HAVE_STRUCT_STAT_ST_RDEV
986 #ifdef HAVE_STRUCT_STAT_ST_SIZE
989 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
992 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
995 #ifdef HAVE_STRUCT_STAT_ST_ATIME
998 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1001 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1007 /* Unsupported field, store 0. */
1008 cb_store_target_endian (cb
, p
, size
, 0);
1012 m
= strchr (q
, ':');
1017 return p
- (char *) ts
;
1020 /* Cover functions to the vfprintf callbacks.
1022 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
1023 a larger "remote target subsystem"] with a well defined interface, then
1024 one would think that the subsystem would provide these. However, until
1025 one is allowed to create such a subsystem (with its own source tree
1026 independent of any particular user), such a critter can't exist. Thus
1027 these functions are here for the time being. */
1030 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
1035 p
->vprintf_filtered (p
, fmt
, ap
);
1040 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
1045 p
->evprintf_filtered (p
, fmt
, ap
);
1050 cb_is_stdin (host_callback
*cb
, int fd
)
1052 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 0;
1056 cb_is_stdout (host_callback
*cb
, int fd
)
1058 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 1;
1062 cb_is_stderr (host_callback
*cb
, int fd
)
1064 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 2;
1068 cb_host_str_syscall (host_callback
*cb
, int host_val
)
1070 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->syscall_map
, host_val
);
1072 return m
? m
->name
: NULL
;
1076 cb_host_str_errno (host_callback
*cb
, int host_val
)
1078 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
1080 return m
? m
->name
: NULL
;
1084 cb_host_str_signal (host_callback
*cb
, int host_val
)
1086 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->signal_map
, host_val
);
1088 return m
? m
->name
: NULL
;
1092 cb_target_str_syscall (host_callback
*cb
, int target_val
)
1094 const CB_TARGET_DEFS_MAP
*m
=
1095 cb_target_map_entry (cb
->syscall_map
, target_val
);
1097 return m
? m
->name
: NULL
;
1101 cb_target_str_errno (host_callback
*cb
, int target_val
)
1103 const CB_TARGET_DEFS_MAP
*m
=
1104 cb_target_map_entry (cb
->errno_map
, target_val
);
1106 return m
? m
->name
: NULL
;
1110 cb_target_str_signal (host_callback
*cb
, int target_val
)
1112 const CB_TARGET_DEFS_MAP
*m
=
1113 cb_target_map_entry (cb
->signal_map
, target_val
);
1115 return m
? m
->name
: NULL
;