1 /* Remote target callback routines.
2 Copyright 1995-2015 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
47 #include <sys/types.h>
49 #include "gdb/callback.h"
50 #include "targ-vals.h"
52 #include "libiberty.h"
62 /* ??? sim_cb_printf should be cb_printf, but until the callback support is
63 broken out of the simulator directory, these are here to not require
65 void sim_cb_printf (host_callback
*, const char *, ...);
66 void sim_cb_eprintf (host_callback
*, const char *, ...);
68 extern CB_TARGET_DEFS_MAP cb_init_syscall_map
[];
69 extern CB_TARGET_DEFS_MAP cb_init_errno_map
[];
70 extern CB_TARGET_DEFS_MAP cb_init_open_map
[];
72 /* Set the callback copy of errno from what we see now. */
75 wrap (host_callback
*p
, int val
)
77 p
->last_errno
= errno
;
81 /* Make sure the FD provided is ok. If not, return non-zero
85 fdbad (host_callback
*p
, int fd
)
87 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| p
->fd_buddy
[fd
] < 0)
89 p
->last_errno
= EBADF
;
96 fdmap (host_callback
*p
, int fd
)
102 os_close (host_callback
*p
, int fd
)
107 result
= fdbad (p
, fd
);
110 /* If this file descripter has one or more buddies (originals /
111 duplicates from a dup), just remove it from the circular list. */
112 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
115 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
120 int other
= p
->ispipe
[fd
];
125 /* Closing the read side. */
131 /* Closing the write side. */
136 /* If there was data in the buffer, make a last "now empty"
137 call, then deallocate data. */
138 if (p
->pipe_buffer
[writer
].buffer
!= NULL
)
140 (*p
->pipe_empty
) (p
, reader
, writer
);
141 free (p
->pipe_buffer
[writer
].buffer
);
142 p
->pipe_buffer
[writer
].buffer
= NULL
;
145 /* Clear pipe data for this side. */
146 p
->pipe_buffer
[fd
].size
= 0;
149 /* If this was the first close, mark the other side as the
150 only remaining side. */
151 if (fd
!= abs (other
))
152 p
->ispipe
[abs (other
)] = -other
;
153 p
->fd_buddy
[fd
] = -1;
157 result
= wrap (p
, close (fdmap (p
, fd
)));
159 p
->fd_buddy
[fd
] = -1;
165 /* taken from gdb/util.c:notice_quit() - should be in a library */
168 #if defined(__GO32__) || defined (_MSC_VER)
170 os_poll_quit (host_callback
*p
)
172 #if defined(__GO32__)
188 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
192 #if defined (_MSC_VER)
193 /* NB - this will not compile! */
194 int k
= win32pollquit ();
203 #define os_poll_quit 0
204 #endif /* defined(__GO32__) || defined(_MSC_VER) */
207 os_get_errno (host_callback
*p
)
209 return cb_host_to_target_errno (p
, p
->last_errno
);
214 os_isatty (host_callback
*p
, int fd
)
218 result
= fdbad (p
, fd
);
221 result
= wrap (p
, isatty (fdmap (p
, fd
)));
227 os_lseek (host_callback
*p
, int fd
, long off
, int way
)
231 result
= fdbad (p
, fd
);
234 result
= wrap (p
, lseek (fdmap (p
, fd
), off
, way
));
239 os_open (host_callback
*p
, const char *name
, int flags
)
242 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
244 if (p
->fd_buddy
[i
] < 0)
246 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
249 p
->last_errno
= errno
;
257 p
->last_errno
= EMFILE
;
262 os_read (host_callback
*p
, int fd
, char *buf
, int len
)
266 result
= fdbad (p
, fd
);
271 int writer
= p
->ispipe
[fd
];
273 /* Can't read from the write-end. */
276 p
->last_errno
= EBADF
;
280 /* Nothing to read if nothing is written. */
281 if (p
->pipe_buffer
[writer
].size
== 0)
284 /* Truncate read request size to buffer size minus what's already
286 if (len
> p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
)
287 len
= p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
;
289 memcpy (buf
, p
->pipe_buffer
[writer
].buffer
+ p
->pipe_buffer
[fd
].size
,
292 /* Account for what we just read. */
293 p
->pipe_buffer
[fd
].size
+= len
;
295 /* If we've read everything, empty and deallocate the buffer and
296 signal buffer-empty to client. (This isn't expected to be a
297 hot path in the simulator, so we don't hold on to the buffer.) */
298 if (p
->pipe_buffer
[fd
].size
== p
->pipe_buffer
[writer
].size
)
300 free (p
->pipe_buffer
[writer
].buffer
);
301 p
->pipe_buffer
[writer
].buffer
= NULL
;
302 p
->pipe_buffer
[fd
].size
= 0;
303 p
->pipe_buffer
[writer
].size
= 0;
304 (*p
->pipe_empty
) (p
, fd
, writer
);
310 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
315 os_read_stdin (host_callback
*p
, char *buf
, int len
)
317 return wrap (p
, read (0, buf
, len
));
321 os_write (host_callback
*p
, int fd
, const char *buf
, int len
)
326 result
= fdbad (p
, fd
);
332 int reader
= -p
->ispipe
[fd
];
334 /* Can't write to the read-end. */
337 p
->last_errno
= EBADF
;
341 /* Can't write to pipe with closed read end.
342 FIXME: We should send a SIGPIPE. */
345 p
->last_errno
= EPIPE
;
349 /* As a sanity-check, we bail out it the buffered contents is much
350 larger than the size of the buffer on the host. We don't want
351 to run out of memory in the simulator due to a target program
352 bug if we can help it. Unfortunately, regarding the value that
353 reaches the simulated program, it's no use returning *less*
354 than the requested amount, because cb_syscall loops calling
355 this function until the whole amount is done. */
356 if (p
->pipe_buffer
[fd
].size
+ len
> 10 * PIPE_BUF
)
358 p
->last_errno
= EFBIG
;
362 p
->pipe_buffer
[fd
].buffer
363 = xrealloc (p
->pipe_buffer
[fd
].buffer
, p
->pipe_buffer
[fd
].size
+ len
);
364 memcpy (p
->pipe_buffer
[fd
].buffer
+ p
->pipe_buffer
[fd
].size
,
366 p
->pipe_buffer
[fd
].size
+= len
;
368 (*p
->pipe_nonempty
) (p
, reader
, fd
);
372 real_fd
= fdmap (p
, fd
);
376 result
= wrap (p
, write (real_fd
, buf
, len
));
379 result
= p
->write_stdout (p
, buf
, len
);
382 result
= p
->write_stderr (p
, buf
, len
);
389 os_write_stdout (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
391 return fwrite (buf
, 1, len
, stdout
);
395 os_flush_stdout (host_callback
*p ATTRIBUTE_UNUSED
)
401 os_write_stderr (host_callback
*p ATTRIBUTE_UNUSED
, const char *buf
, int len
)
403 return fwrite (buf
, 1, len
, stderr
);
407 os_flush_stderr (host_callback
*p ATTRIBUTE_UNUSED
)
413 os_rename (host_callback
*p
, const char *f1
, const char *f2
)
415 return wrap (p
, rename (f1
, f2
));
420 os_system (host_callback
*p
, const char *s
)
422 return wrap (p
, system (s
));
426 os_time (host_callback
*p
, long *t
)
428 return wrap (p
, time (t
));
433 os_unlink (host_callback
*p
, const char *f1
)
435 return wrap (p
, unlink (f1
));
439 os_stat (host_callback
*p
, const char *file
, struct stat
*buf
)
441 /* ??? There is an issue of when to translate to the target layout.
442 One could do that inside this function, or one could have the
443 caller do it. It's more flexible to let the caller do it, though
444 I'm not sure the flexibility will ever be useful. */
445 return wrap (p
, stat (file
, buf
));
449 os_fstat (host_callback
*p
, int fd
, struct stat
*buf
)
456 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
457 time_t t
= (*p
->time
) (p
, NULL
);
460 /* We have to fake the struct stat contents, since the pipe is
461 made up in the simulator. */
462 memset (buf
, 0, sizeof (*buf
));
464 #ifdef HAVE_STRUCT_STAT_ST_MODE
465 buf
->st_mode
= S_IFIFO
;
468 /* If more accurate tracking than current-time is needed (for
469 example, on GNU/Linux we get accurate numbers), the p->time
470 callback (which may be something other than os_time) should
471 happen for each read and write, and we'd need to keep track of
472 atime, ctime and mtime. */
473 #ifdef HAVE_STRUCT_STAT_ST_ATIME
476 #ifdef HAVE_STRUCT_STAT_ST_CTIME
479 #ifdef HAVE_STRUCT_STAT_ST_MTIME
485 /* ??? There is an issue of when to translate to the target layout.
486 One could do that inside this function, or one could have the
487 caller do it. It's more flexible to let the caller do it, though
488 I'm not sure the flexibility will ever be useful. */
489 return wrap (p
, fstat (fdmap (p
, fd
), buf
));
493 os_lstat (host_callback
*p
, const char *file
, struct stat
*buf
)
495 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
497 return wrap (p
, lstat (file
, buf
));
499 return wrap (p
, stat (file
, buf
));
504 os_ftruncate (host_callback
*p
, int fd
, long len
)
508 result
= fdbad (p
, fd
);
511 p
->last_errno
= EINVAL
;
516 #ifdef HAVE_FTRUNCATE
517 result
= wrap (p
, ftruncate (fdmap (p
, fd
), len
));
519 p
->last_errno
= EINVAL
;
526 os_truncate (host_callback
*p
, const char *file
, long len
)
529 return wrap (p
, truncate (file
, len
));
531 p
->last_errno
= EINVAL
;
537 os_pipe (host_callback
*p
, int *filedes
)
541 /* We deliberately don't use fd 0. It's probably stdin anyway. */
542 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
546 if (p
->fd_buddy
[i
] < 0)
547 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
548 if (p
->fd_buddy
[j
] < 0)
550 /* Found two free fd:s. Set stat to allocated and mark
559 /* Poison the FD map to make bugs apparent. */
566 p
->last_errno
= EMFILE
;
570 /* Stub functions for pipe support. They should always be overridden in
571 targets using the pipe support, but that's up to the target. */
573 /* Called when the simulator says that the pipe at (reader, writer) is
574 now empty (so the writer should leave its waiting state). */
577 os_pipe_empty (host_callback
*p
, int reader
, int writer
)
581 /* Called when the simulator says the pipe at (reader, writer) is now
582 non-empty (so the writer should wait). */
585 os_pipe_nonempty (host_callback
*p
, int reader
, int writer
)
590 os_shutdown (host_callback
*p
)
593 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
597 /* Zero out all pipe state. Don't call callbacks for non-empty
598 pipes; the target program has likely terminated at this point
599 or we're called at initialization time. */
601 p
->pipe_buffer
[i
].size
= 0;
602 p
->pipe_buffer
[i
].buffer
= NULL
;
604 next
= p
->fd_buddy
[i
];
610 if (j
== MAX_CALLBACK_FDS
)
612 next
= p
->fd_buddy
[j
];
614 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
630 os_init (host_callback
*p
)
635 for (i
= 0; i
< 3; i
++)
638 p
->fd_buddy
[i
] = i
- 1;
640 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
641 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
643 p
->syscall_map
= cb_init_syscall_map
;
644 p
->errno_map
= cb_init_errno_map
;
645 p
->open_map
= cb_init_open_map
;
654 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
657 va_start (args
, format
);
659 vfprintf (stdout
, format
, args
);
665 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
667 vprintf (format
, args
);
672 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
674 vfprintf (stderr
, format
, args
);
679 __attribute__ ((__noreturn__
))
682 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
685 va_start (args
, format
);
687 vfprintf (stderr
, format
, args
);
688 fprintf (stderr
, "\n");
694 host_callback default_callback
=
729 os_printf_filtered
, /* deprecated */
732 os_evprintf_filtered
,
738 { -1, }, /* fd_buddy */
740 { { 0, 0 }, }, /* pipe_buffer */
748 /* Defaults expected to be overridden at initialization, where needed. */
749 BFD_ENDIAN_UNKNOWN
, /* target_endian */
750 4, /* target_sizeof_int */
755 /* Read in a file describing the target's system call values.
756 E.g. maybe someone will want to use something other than newlib.
757 This assumes that the basic system call recognition and value passing/
758 returning is supported. So maybe some coding/recompilation will be
759 necessary, but not as much.
761 If an error occurs, the existing mapping is not changed. */
764 cb_read_target_syscall_maps (host_callback
*cb
, const char *file
)
766 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
767 const char *stat_map
;
770 if ((f
= fopen (file
, "r")) == NULL
)
773 /* ... read in and parse file ... */
776 return CB_RC_NO_MEM
; /* FIXME:wip */
778 /* Free storage allocated for any existing maps. */
780 free (cb
->syscall_map
);
782 free (cb
->errno_map
);
786 free (cb
->signal_map
);
788 free ((PTR
) cb
->stat_map
);
790 cb
->syscall_map
= syscall_map
;
791 cb
->errno_map
= errno_map
;
792 cb
->open_map
= open_map
;
793 cb
->signal_map
= signal_map
;
794 cb
->stat_map
= stat_map
;
799 /* General utility functions to search a map for a value. */
801 static const CB_TARGET_DEFS_MAP
*
802 cb_target_map_entry (const CB_TARGET_DEFS_MAP map
[], int target_val
)
804 const CB_TARGET_DEFS_MAP
*m
;
806 for (m
= &map
[0]; m
->target_val
!= -1; ++m
)
807 if (m
->target_val
== target_val
)
813 static const CB_TARGET_DEFS_MAP
*
814 cb_host_map_entry (const CB_TARGET_DEFS_MAP map
[], int host_val
)
816 const CB_TARGET_DEFS_MAP
*m
;
818 for (m
= &map
[0]; m
->host_val
!= -1; ++m
)
819 if (m
->host_val
== host_val
)
825 /* Translate the target's version of a syscall number to the host's.
826 This isn't actually the host's version, rather a canonical form.
827 ??? Perhaps this should be renamed to ..._canon_syscall. */
830 cb_target_to_host_syscall (host_callback
*cb
, int target_val
)
832 const CB_TARGET_DEFS_MAP
*m
=
833 cb_target_map_entry (cb
->syscall_map
, target_val
);
835 return m
? m
->host_val
: -1;
838 /* FIXME: sort tables if large.
839 Alternatively, an obvious improvement for errno conversion is
840 to machine generate a function with a large switch(). */
842 /* Translate the host's version of errno to the target's. */
845 cb_host_to_target_errno (host_callback
*cb
, int host_val
)
847 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
849 /* ??? Which error to return in this case is up for grabs.
850 Note that some missing values may have standard alternatives.
851 For now return 0 and require caller to deal with it. */
852 return m
? m
->target_val
: 0;
855 /* Given a set of target bitmasks for the open system call,
856 return the host equivalent.
857 Mapping open flag values is best done by looping so there's no need
858 to machine generate this function. */
861 cb_target_to_host_open (host_callback
*cb
, int target_val
)
864 CB_TARGET_DEFS_MAP
*m
;
866 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
868 switch (m
->target_val
)
870 /* O_RDONLY can be (and usually is) 0 which needs to be treated
872 case TARGET_O_RDONLY
:
873 case TARGET_O_WRONLY
:
875 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
877 host_val
|= m
->host_val
;
878 /* Handle the host/target differentiating between binary and
879 text mode. Only one case is of importance */
880 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
881 host_val
|= O_BINARY
;
885 if ((m
->target_val
& target_val
) == m
->target_val
)
886 host_val
|= m
->host_val
;
894 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
897 ??? The "val" must be as big as target word size. */
900 cb_store_target_endian (host_callback
*cb
, char *p
, int size
, long val
)
902 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
921 /* Translate a host's stat struct into a target's.
922 If HS is NULL, just compute the length of the buffer required,
925 The result is the size of the target's stat struct,
926 or zero if an error occurred during the translation. */
929 cb_host_to_target_stat (host_callback
*cb
, const struct stat
*hs
, PTR ts
)
931 const char *m
= cb
->stat_map
;
940 char *q
= strchr (m
, ',');
943 /* FIXME: Use sscanf? */
946 /* FIXME: print error message */
952 /* FIXME: print error message */
960 /* Defined here to avoid emacs indigestion on a lone "else". */
963 else if (strncmp (m, #FLD, q - m) == 0) \
964 cb_store_target_endian (cb, p, size, hs->FLD)
966 #ifdef HAVE_STRUCT_STAT_ST_DEV
969 #ifdef HAVE_STRUCT_STAT_ST_INO
972 #ifdef HAVE_STRUCT_STAT_ST_MODE
975 #ifdef HAVE_STRUCT_STAT_ST_NLINK
978 #ifdef HAVE_STRUCT_STAT_ST_UID
981 #ifdef HAVE_STRUCT_STAT_ST_GID
984 #ifdef HAVE_STRUCT_STAT_ST_RDEV
987 #ifdef HAVE_STRUCT_STAT_ST_SIZE
990 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
993 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
996 #ifdef HAVE_STRUCT_STAT_ST_ATIME
999 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1002 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1008 /* Unsupported field, store 0. */
1009 cb_store_target_endian (cb
, p
, size
, 0);
1013 m
= strchr (q
, ':');
1018 return p
- (char *) ts
;
1021 /* Cover functions to the vfprintf callbacks.
1023 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
1024 a larger "remote target subsystem"] with a well defined interface, then
1025 one would think that the subsystem would provide these. However, until
1026 one is allowed to create such a subsystem (with its own source tree
1027 independent of any particular user), such a critter can't exist. Thus
1028 these functions are here for the time being. */
1031 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
1036 p
->vprintf_filtered (p
, fmt
, ap
);
1041 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
1046 p
->evprintf_filtered (p
, fmt
, ap
);
1051 cb_is_stdin (host_callback
*cb
, int fd
)
1053 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 0;
1057 cb_is_stdout (host_callback
*cb
, int fd
)
1059 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 1;
1063 cb_is_stderr (host_callback
*cb
, int fd
)
1065 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 2;
1069 cb_host_str_syscall (host_callback
*cb
, int host_val
)
1071 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->syscall_map
, host_val
);
1073 return m
? m
->name
: NULL
;
1077 cb_host_str_errno (host_callback
*cb
, int host_val
)
1079 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->errno_map
, host_val
);
1081 return m
? m
->name
: NULL
;
1085 cb_host_str_signal (host_callback
*cb
, int host_val
)
1087 const CB_TARGET_DEFS_MAP
*m
= cb_host_map_entry (cb
->signal_map
, host_val
);
1089 return m
? m
->name
: NULL
;
1093 cb_target_str_syscall (host_callback
*cb
, int target_val
)
1095 const CB_TARGET_DEFS_MAP
*m
=
1096 cb_target_map_entry (cb
->syscall_map
, target_val
);
1098 return m
? m
->name
: NULL
;
1102 cb_target_str_errno (host_callback
*cb
, int target_val
)
1104 const CB_TARGET_DEFS_MAP
*m
=
1105 cb_target_map_entry (cb
->errno_map
, target_val
);
1107 return m
? m
->name
: NULL
;
1111 cb_target_str_signal (host_callback
*cb
, int target_val
)
1113 const CB_TARGET_DEFS_MAP
*m
=
1114 cb_target_map_entry (cb
->signal_map
, target_val
);
1116 return m
? m
->name
: NULL
;