1 /* Remote target callback routines.
2 Copyright 1995-2014 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
PARAMS ((host_callback
*, const char *, ...));
66 void sim_cb_eprintf
PARAMS ((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 extern int system
PARAMS ((const char *));
74 static int os_init
PARAMS ((host_callback
*));
75 static int os_shutdown
PARAMS ((host_callback
*));
76 static int os_unlink
PARAMS ((host_callback
*, const char *));
77 static long os_time
PARAMS ((host_callback
*, long *));
78 static int os_system
PARAMS ((host_callback
*, const char *));
79 static int os_rename
PARAMS ((host_callback
*, const char *, const char *));
80 static int os_write_stdout
PARAMS ((host_callback
*, const char *, int));
81 static void os_flush_stdout
PARAMS ((host_callback
*));
82 static int os_write_stderr
PARAMS ((host_callback
*, const char *, int));
83 static void os_flush_stderr
PARAMS ((host_callback
*));
84 static int os_write
PARAMS ((host_callback
*, int, const char *, int));
85 static int os_read_stdin
PARAMS ((host_callback
*, char *, int));
86 static int os_read
PARAMS ((host_callback
*, int, char *, int));
87 static int os_open
PARAMS ((host_callback
*, const char *, int));
88 static int os_lseek
PARAMS ((host_callback
*, int, long, int));
89 static int os_isatty
PARAMS ((host_callback
*, int));
90 static int os_get_errno
PARAMS ((host_callback
*));
91 static int os_close
PARAMS ((host_callback
*, int));
92 static void os_vprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
93 static void os_evprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
94 static void os_error
PARAMS ((host_callback
*, const char *, ...))
96 __attribute__ ((__noreturn__
))
99 static int fdmap
PARAMS ((host_callback
*, int));
100 static int fdbad
PARAMS ((host_callback
*, int));
101 static int wrap
PARAMS ((host_callback
*, int));
103 /* Set the callback copy of errno from what we see now. */
110 p
->last_errno
= errno
;
114 /* Make sure the FD provided is ok. If not, return non-zero
122 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| p
->fd_buddy
[fd
] < 0)
124 p
->last_errno
= EBADF
;
146 result
= fdbad (p
, fd
);
149 /* If this file descripter has one or more buddies (originals /
150 duplicates from a dup), just remove it from the circular list. */
151 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
154 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
159 int other
= p
->ispipe
[fd
];
164 /* Closing the read side. */
170 /* Closing the write side. */
175 /* If there was data in the buffer, make a last "now empty"
176 call, then deallocate data. */
177 if (p
->pipe_buffer
[writer
].buffer
!= NULL
)
179 (*p
->pipe_empty
) (p
, reader
, writer
);
180 free (p
->pipe_buffer
[writer
].buffer
);
181 p
->pipe_buffer
[writer
].buffer
= NULL
;
184 /* Clear pipe data for this side. */
185 p
->pipe_buffer
[fd
].size
= 0;
188 /* If this was the first close, mark the other side as the
189 only remaining side. */
190 if (fd
!= abs (other
))
191 p
->ispipe
[abs (other
)] = -other
;
192 p
->fd_buddy
[fd
] = -1;
196 result
= wrap (p
, close (fdmap (p
, fd
)));
198 p
->fd_buddy
[fd
] = -1;
204 /* taken from gdb/util.c:notice_quit() - should be in a library */
207 #if defined(__GO32__) || defined (_MSC_VER)
212 #if defined(__GO32__)
228 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
232 #if defined (_MSC_VER)
233 /* NB - this will not compile! */
234 int k
= win32pollquit ();
243 #define os_poll_quit 0
244 #endif /* defined(__GO32__) || defined(_MSC_VER) */
250 return cb_host_to_target_errno (p
, p
->last_errno
);
261 result
= fdbad (p
, fd
);
264 result
= wrap (p
, isatty (fdmap (p
, fd
)));
270 os_lseek (p
, fd
, off
, way
)
278 result
= fdbad (p
, fd
);
281 result
= wrap (p
, lseek (fdmap (p
, fd
), off
, way
));
286 os_open (p
, name
, flags
)
292 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
294 if (p
->fd_buddy
[i
] < 0)
296 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
299 p
->last_errno
= errno
;
307 p
->last_errno
= EMFILE
;
312 os_read (p
, fd
, buf
, len
)
320 result
= fdbad (p
, fd
);
325 int writer
= p
->ispipe
[fd
];
327 /* Can't read from the write-end. */
330 p
->last_errno
= EBADF
;
334 /* Nothing to read if nothing is written. */
335 if (p
->pipe_buffer
[writer
].size
== 0)
338 /* Truncate read request size to buffer size minus what's already
340 if (len
> p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
)
341 len
= p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
;
343 memcpy (buf
, p
->pipe_buffer
[writer
].buffer
+ p
->pipe_buffer
[fd
].size
,
346 /* Account for what we just read. */
347 p
->pipe_buffer
[fd
].size
+= len
;
349 /* If we've read everything, empty and deallocate the buffer and
350 signal buffer-empty to client. (This isn't expected to be a
351 hot path in the simulator, so we don't hold on to the buffer.) */
352 if (p
->pipe_buffer
[fd
].size
== p
->pipe_buffer
[writer
].size
)
354 free (p
->pipe_buffer
[writer
].buffer
);
355 p
->pipe_buffer
[writer
].buffer
= NULL
;
356 p
->pipe_buffer
[fd
].size
= 0;
357 p
->pipe_buffer
[writer
].size
= 0;
358 (*p
->pipe_empty
) (p
, fd
, writer
);
364 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
369 os_read_stdin (p
, buf
, len
)
374 return wrap (p
, read (0, buf
, len
));
378 os_write (p
, fd
, buf
, len
)
387 result
= fdbad (p
, fd
);
393 int reader
= -p
->ispipe
[fd
];
395 /* Can't write to the read-end. */
398 p
->last_errno
= EBADF
;
402 /* Can't write to pipe with closed read end.
403 FIXME: We should send a SIGPIPE. */
406 p
->last_errno
= EPIPE
;
410 /* As a sanity-check, we bail out it the buffered contents is much
411 larger than the size of the buffer on the host. We don't want
412 to run out of memory in the simulator due to a target program
413 bug if we can help it. Unfortunately, regarding the value that
414 reaches the simulated program, it's no use returning *less*
415 than the requested amount, because cb_syscall loops calling
416 this function until the whole amount is done. */
417 if (p
->pipe_buffer
[fd
].size
+ len
> 10 * PIPE_BUF
)
419 p
->last_errno
= EFBIG
;
423 p
->pipe_buffer
[fd
].buffer
424 = xrealloc (p
->pipe_buffer
[fd
].buffer
, p
->pipe_buffer
[fd
].size
+ len
);
425 memcpy (p
->pipe_buffer
[fd
].buffer
+ p
->pipe_buffer
[fd
].size
,
427 p
->pipe_buffer
[fd
].size
+= len
;
429 (*p
->pipe_nonempty
) (p
, reader
, fd
);
433 real_fd
= fdmap (p
, fd
);
437 result
= wrap (p
, write (real_fd
, buf
, len
));
440 result
= p
->write_stdout (p
, buf
, len
);
443 result
= p
->write_stderr (p
, buf
, len
);
450 os_write_stdout (p
, buf
, len
)
451 host_callback
*p ATTRIBUTE_UNUSED
;
455 return fwrite (buf
, 1, len
, stdout
);
460 host_callback
*p ATTRIBUTE_UNUSED
;
466 os_write_stderr (p
, buf
, len
)
467 host_callback
*p ATTRIBUTE_UNUSED
;
471 return fwrite (buf
, 1, len
, stderr
);
476 host_callback
*p ATTRIBUTE_UNUSED
;
482 os_rename (p
, f1
, f2
)
487 return wrap (p
, rename (f1
, f2
));
496 return wrap (p
, system (s
));
504 return wrap (p
, time (t
));
513 return wrap (p
, unlink (f1
));
517 os_stat (p
, file
, buf
)
522 /* ??? There is an issue of when to translate to the target layout.
523 One could do that inside this function, or one could have the
524 caller do it. It's more flexible to let the caller do it, though
525 I'm not sure the flexibility will ever be useful. */
526 return wrap (p
, stat (file
, buf
));
530 os_fstat (p
, fd
, buf
)
540 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
541 time_t t
= (*p
->time
) (p
, NULL
);
544 /* We have to fake the struct stat contents, since the pipe is
545 made up in the simulator. */
546 memset (buf
, 0, sizeof (*buf
));
548 #ifdef HAVE_STRUCT_STAT_ST_MODE
549 buf
->st_mode
= S_IFIFO
;
552 /* If more accurate tracking than current-time is needed (for
553 example, on GNU/Linux we get accurate numbers), the p->time
554 callback (which may be something other than os_time) should
555 happen for each read and write, and we'd need to keep track of
556 atime, ctime and mtime. */
557 #ifdef HAVE_STRUCT_STAT_ST_ATIME
560 #ifdef HAVE_STRUCT_STAT_ST_CTIME
563 #ifdef HAVE_STRUCT_STAT_ST_MTIME
569 /* ??? There is an issue of when to translate to the target layout.
570 One could do that inside this function, or one could have the
571 caller do it. It's more flexible to let the caller do it, though
572 I'm not sure the flexibility will ever be useful. */
573 return wrap (p
, fstat (fdmap (p
, fd
), buf
));
577 os_lstat (p
, file
, buf
)
582 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
584 return wrap (p
, lstat (file
, buf
));
586 return wrap (p
, stat (file
, buf
));
591 os_ftruncate (p
, fd
, len
)
598 result
= fdbad (p
, fd
);
601 p
->last_errno
= EINVAL
;
606 #ifdef HAVE_FTRUNCATE
607 result
= wrap (p
, ftruncate (fdmap (p
, fd
), len
));
609 p
->last_errno
= EINVAL
;
616 os_truncate (p
, file
, len
)
622 return wrap (p
, truncate (file
, len
));
624 p
->last_errno
= EINVAL
;
636 /* We deliberately don't use fd 0. It's probably stdin anyway. */
637 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
641 if (p
->fd_buddy
[i
] < 0)
642 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
643 if (p
->fd_buddy
[j
] < 0)
645 /* Found two free fd:s. Set stat to allocated and mark
654 /* Poison the FD map to make bugs apparent. */
661 p
->last_errno
= EMFILE
;
665 /* Stub functions for pipe support. They should always be overridden in
666 targets using the pipe support, but that's up to the target. */
668 /* Called when the simulator says that the pipe at (reader, writer) is
669 now empty (so the writer should leave its waiting state). */
672 os_pipe_empty (p
, reader
, writer
)
679 /* Called when the simulator says the pipe at (reader, writer) is now
680 non-empty (so the writer should wait). */
683 os_pipe_nonempty (p
, reader
, writer
)
695 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
699 /* Zero out all pipe state. Don't call callbacks for non-empty
700 pipes; the target program has likely terminated at this point
701 or we're called at initialization time. */
703 p
->pipe_buffer
[i
].size
= 0;
704 p
->pipe_buffer
[i
].buffer
= NULL
;
706 next
= p
->fd_buddy
[i
];
712 if (j
== MAX_CALLBACK_FDS
)
714 next
= p
->fd_buddy
[j
];
716 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
738 for (i
= 0; i
< 3; i
++)
741 p
->fd_buddy
[i
] = i
- 1;
743 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
744 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
746 p
->syscall_map
= cb_init_syscall_map
;
747 p
->errno_map
= cb_init_errno_map
;
748 p
->open_map
= cb_init_open_map
;
757 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
760 va_start (args
, format
);
762 vfprintf (stdout
, format
, args
);
768 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
770 vprintf (format
, args
);
775 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
777 vfprintf (stderr
, format
, args
);
782 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
785 va_start (args
, format
);
787 vfprintf (stderr
, format
, args
);
788 fprintf (stderr
, "\n");
794 host_callback default_callback
=
829 os_printf_filtered
, /* deprecated */
832 os_evprintf_filtered
,
838 { -1, }, /* fd_buddy */
840 { { 0, 0 }, }, /* pipe_buffer */
848 /* Defaults expected to be overridden at initialization, where needed. */
849 BFD_ENDIAN_UNKNOWN
, /* target_endian */
850 4, /* target_sizeof_int */
855 /* Read in a file describing the target's system call values.
856 E.g. maybe someone will want to use something other than newlib.
857 This assumes that the basic system call recognition and value passing/
858 returning is supported. So maybe some coding/recompilation will be
859 necessary, but not as much.
861 If an error occurs, the existing mapping is not changed. */
864 cb_read_target_syscall_maps (cb
, file
)
868 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
869 const char *stat_map
;
872 if ((f
= fopen (file
, "r")) == NULL
)
875 /* ... read in and parse file ... */
878 return CB_RC_NO_MEM
; /* FIXME:wip */
880 /* Free storage allocated for any existing maps. */
882 free (cb
->syscall_map
);
884 free (cb
->errno_map
);
888 free (cb
->signal_map
);
890 free ((PTR
) cb
->stat_map
);
892 cb
->syscall_map
= syscall_map
;
893 cb
->errno_map
= errno_map
;
894 cb
->open_map
= open_map
;
895 cb
->signal_map
= signal_map
;
896 cb
->stat_map
= stat_map
;
901 /* Translate the target's version of a syscall number to the host's.
902 This isn't actually the host's version, rather a canonical form.
903 ??? Perhaps this should be renamed to ..._canon_syscall. */
906 cb_target_to_host_syscall (cb
, target_val
)
910 CB_TARGET_DEFS_MAP
*m
;
912 for (m
= &cb
->syscall_map
[0]; m
->target_val
!= -1; ++m
)
913 if (m
->target_val
== target_val
)
919 /* FIXME: sort tables if large.
920 Alternatively, an obvious improvement for errno conversion is
921 to machine generate a function with a large switch(). */
923 /* Translate the host's version of errno to the target's. */
926 cb_host_to_target_errno (cb
, host_val
)
930 CB_TARGET_DEFS_MAP
*m
;
932 for (m
= &cb
->errno_map
[0]; m
->host_val
; ++m
)
933 if (m
->host_val
== host_val
)
934 return m
->target_val
;
936 /* ??? Which error to return in this case is up for grabs.
937 Note that some missing values may have standard alternatives.
938 For now return 0 and require caller to deal with it. */
942 /* Given a set of target bitmasks for the open system call,
943 return the host equivalent.
944 Mapping open flag values is best done by looping so there's no need
945 to machine generate this function. */
948 cb_target_to_host_open (cb
, target_val
)
953 CB_TARGET_DEFS_MAP
*m
;
955 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
957 switch (m
->target_val
)
959 /* O_RDONLY can be (and usually is) 0 which needs to be treated
961 case TARGET_O_RDONLY
:
962 case TARGET_O_WRONLY
:
964 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
966 host_val
|= m
->host_val
;
967 /* Handle the host/target differentiating between binary and
968 text mode. Only one case is of importance */
969 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
970 host_val
|= O_BINARY
;
974 if ((m
->target_val
& target_val
) == m
->target_val
)
975 host_val
|= m
->host_val
;
983 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
987 cb_store_target_endian (cb
, p
, size
, val
)
991 long val
; /* ??? must be as big as target word size */
993 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
1012 /* Translate a host's stat struct into a target's.
1013 If HS is NULL, just compute the length of the buffer required,
1016 The result is the size of the target's stat struct,
1017 or zero if an error occurred during the translation. */
1020 cb_host_to_target_stat (cb
, hs
, ts
)
1022 const struct stat
*hs
;
1025 const char *m
= cb
->stat_map
;
1034 char *q
= strchr (m
, ',');
1037 /* FIXME: Use sscanf? */
1040 /* FIXME: print error message */
1043 size
= atoi (q
+ 1);
1046 /* FIXME: print error message */
1054 /* Defined here to avoid emacs indigestion on a lone "else". */
1057 else if (strncmp (m, #FLD, q - m) == 0) \
1058 cb_store_target_endian (cb, p, size, hs->FLD)
1060 #ifdef HAVE_STRUCT_STAT_ST_DEV
1063 #ifdef HAVE_STRUCT_STAT_ST_INO
1066 #ifdef HAVE_STRUCT_STAT_ST_MODE
1069 #ifdef HAVE_STRUCT_STAT_ST_NLINK
1072 #ifdef HAVE_STRUCT_STAT_ST_UID
1075 #ifdef HAVE_STRUCT_STAT_ST_GID
1078 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1081 #ifdef HAVE_STRUCT_STAT_ST_SIZE
1084 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1087 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1090 #ifdef HAVE_STRUCT_STAT_ST_ATIME
1093 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1096 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1102 /* Unsupported field, store 0. */
1103 cb_store_target_endian (cb
, p
, size
, 0);
1107 m
= strchr (q
, ':');
1112 return p
- (char *) ts
;
1115 /* Cover functions to the vfprintf callbacks.
1117 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
1118 a larger "remote target subsystem"] with a well defined interface, then
1119 one would think that the subsystem would provide these. However, until
1120 one is allowed to create such a subsystem (with its own source tree
1121 independent of any particular user), such a critter can't exist. Thus
1122 these functions are here for the time being. */
1125 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
1130 p
->vprintf_filtered (p
, fmt
, ap
);
1135 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
1140 p
->evprintf_filtered (p
, fmt
, ap
);
1145 cb_is_stdin (host_callback
*cb
, int fd
)
1147 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 0;
1151 cb_is_stdout (host_callback
*cb
, int fd
)
1153 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 1;
1157 cb_is_stderr (host_callback
*cb
, int fd
)
1159 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 2;