1 /* Remote target callback routines.
2 Copyright 1995, 1996, 1997, 2000, 2002, 2003, 2004, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Solutions.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* 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 *, ...));
95 static int fdmap
PARAMS ((host_callback
*, int));
96 static int fdbad
PARAMS ((host_callback
*, int));
97 static int wrap
PARAMS ((host_callback
*, int));
99 /* Set the callback copy of errno from what we see now. */
106 p
->last_errno
= errno
;
110 /* Make sure the FD provided is ok. If not, return non-zero
118 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| p
->fd_buddy
[fd
] < 0)
120 p
->last_errno
= EINVAL
;
142 result
= fdbad (p
, fd
);
145 /* If this file descripter has one or more buddies (originals /
146 duplicates from a dup), just remove it from the circular list. */
147 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
150 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
155 int other
= p
->ispipe
[fd
];
160 /* Closing the read side. */
166 /* Closing the write side. */
171 /* If there was data in the buffer, make a last "now empty"
172 call, then deallocate data. */
173 if (p
->pipe_buffer
[writer
].buffer
!= NULL
)
175 (*p
->pipe_empty
) (p
, reader
, writer
);
176 free (p
->pipe_buffer
[writer
].buffer
);
177 p
->pipe_buffer
[writer
].buffer
= NULL
;
180 /* Clear pipe data for this side. */
181 p
->pipe_buffer
[fd
].size
= 0;
184 /* If this was the first close, mark the other side as the
185 only remaining side. */
186 if (fd
!= abs (other
))
187 p
->ispipe
[abs (other
)] = -other
;
188 p
->fd_buddy
[fd
] = -1;
192 result
= wrap (p
, close (fdmap (p
, fd
)));
194 p
->fd_buddy
[fd
] = -1;
200 /* taken from gdb/util.c:notice_quit() - should be in a library */
203 #if defined(__GO32__) || defined (_MSC_VER)
208 #if defined(__GO32__)
224 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
228 #if defined (_MSC_VER)
229 /* NB - this will not compile! */
230 int k
= win32pollquit();
239 #define os_poll_quit 0
240 #endif /* defined(__GO32__) || defined(_MSC_VER) */
246 return cb_host_to_target_errno (p
, p
->last_errno
);
257 result
= fdbad (p
, fd
);
260 result
= wrap (p
, isatty (fdmap (p
, fd
)));
266 os_lseek (p
, fd
, off
, way
)
274 result
= fdbad (p
, fd
);
277 result
= lseek (fdmap (p
, fd
), off
, way
);
282 os_open (p
, name
, flags
)
288 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
290 if (p
->fd_buddy
[i
] < 0)
292 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
295 p
->last_errno
= errno
;
303 p
->last_errno
= EMFILE
;
308 os_read (p
, fd
, buf
, len
)
316 result
= fdbad (p
, fd
);
321 int writer
= p
->ispipe
[fd
];
323 /* Can't read from the write-end. */
326 p
->last_errno
= EBADF
;
330 /* Nothing to read if nothing is written. */
331 if (p
->pipe_buffer
[writer
].size
== 0)
334 /* Truncate read request size to buffer size minus what's already
336 if (len
> p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
)
337 len
= p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
;
339 memcpy (buf
, p
->pipe_buffer
[writer
].buffer
+ p
->pipe_buffer
[fd
].size
,
342 /* Account for what we just read. */
343 p
->pipe_buffer
[fd
].size
+= len
;
345 /* If we've read everything, empty and deallocate the buffer and
346 signal buffer-empty to client. (This isn't expected to be a
347 hot path in the simulator, so we don't hold on to the buffer.) */
348 if (p
->pipe_buffer
[fd
].size
== p
->pipe_buffer
[writer
].size
)
350 free (p
->pipe_buffer
[writer
].buffer
);
351 p
->pipe_buffer
[writer
].buffer
= NULL
;
352 p
->pipe_buffer
[fd
].size
= 0;
353 p
->pipe_buffer
[writer
].size
= 0;
354 (*p
->pipe_empty
) (p
, fd
, writer
);
360 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
365 os_read_stdin (p
, buf
, len
)
370 return wrap (p
, read (0, buf
, len
));
374 os_write (p
, fd
, buf
, len
)
383 result
= fdbad (p
, fd
);
389 int reader
= -p
->ispipe
[fd
];
391 /* Can't write to the read-end. */
394 p
->last_errno
= EBADF
;
398 /* Can't write to pipe with closed read end.
399 FIXME: We should send a SIGPIPE. */
402 p
->last_errno
= EPIPE
;
406 /* As a sanity-check, we bail out it the buffered contents is much
407 larger than the size of the buffer on the host. We don't want
408 to run out of memory in the simulator due to a target program
409 bug if we can help it. Unfortunately, regarding the value that
410 reaches the simulated program, it's no use returning *less*
411 than the requested amount, because cb_syscall loops calling
412 this function until the whole amount is done. */
413 if (p
->pipe_buffer
[fd
].size
+ len
> 10 * PIPE_BUF
)
415 p
->last_errno
= EFBIG
;
419 p
->pipe_buffer
[fd
].buffer
420 = xrealloc (p
->pipe_buffer
[fd
].buffer
, p
->pipe_buffer
[fd
].size
+ len
);
421 memcpy (p
->pipe_buffer
[fd
].buffer
+ p
->pipe_buffer
[fd
].size
,
423 p
->pipe_buffer
[fd
].size
+= len
;
425 (*p
->pipe_nonempty
) (p
, reader
, fd
);
429 real_fd
= fdmap (p
, fd
);
433 result
= wrap (p
, write (real_fd
, buf
, len
));
436 result
= p
->write_stdout (p
, buf
, len
);
439 result
= p
->write_stderr (p
, buf
, len
);
446 os_write_stdout (p
, buf
, len
)
447 host_callback
*p ATTRIBUTE_UNUSED
;
451 return fwrite (buf
, 1, len
, stdout
);
456 host_callback
*p ATTRIBUTE_UNUSED
;
462 os_write_stderr (p
, buf
, len
)
463 host_callback
*p ATTRIBUTE_UNUSED
;
467 return fwrite (buf
, 1, len
, stderr
);
472 host_callback
*p ATTRIBUTE_UNUSED
;
478 os_rename (p
, f1
, f2
)
483 return wrap (p
, rename (f1
, f2
));
492 return wrap (p
, system (s
));
500 return wrap (p
, time (t
));
509 return wrap (p
, unlink (f1
));
513 os_stat (p
, file
, buf
)
518 /* ??? There is an issue of when to translate to the target layout.
519 One could do that inside this function, or one could have the
520 caller do it. It's more flexible to let the caller do it, though
521 I'm not sure the flexibility will ever be useful. */
522 return wrap (p
, stat (file
, buf
));
526 os_fstat (p
, fd
, buf
)
536 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
537 time_t t
= (*p
->time
) (p
, NULL
);
540 /* We have to fake the struct stat contents, since the pipe is
541 made up in the simulator. */
542 memset (buf
, 0, sizeof (*buf
));
544 #ifdef HAVE_STRUCT_STAT_ST_MODE
545 buf
->st_mode
= S_IFIFO
;
548 /* If more accurate tracking than current-time is needed (for
549 example, on GNU/Linux we get accurate numbers), the p->time
550 callback (which may be something other than os_time) should
551 happen for each read and write, and we'd need to keep track of
552 atime, ctime and mtime. */
553 #ifdef HAVE_STRUCT_STAT_ST_ATIME
556 #ifdef HAVE_STRUCT_STAT_ST_CTIME
559 #ifdef HAVE_STRUCT_STAT_ST_MTIME
565 /* ??? There is an issue of when to translate to the target layout.
566 One could do that inside this function, or one could have the
567 caller do it. It's more flexible to let the caller do it, though
568 I'm not sure the flexibility will ever be useful. */
569 return wrap (p
, fstat (fdmap (p
, fd
), buf
));
573 os_lstat (p
, file
, buf
)
578 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
580 return wrap (p
, lstat (file
, buf
));
582 return wrap (p
, stat (file
, buf
));
587 os_ftruncate (p
, fd
, len
)
594 result
= fdbad (p
, fd
);
597 p
->last_errno
= EINVAL
;
602 #ifdef HAVE_FTRUNCATE
603 result
= wrap (p
, ftruncate (fdmap (p
, fd
), len
));
605 p
->last_errno
= EINVAL
;
612 os_truncate (p
, file
, len
)
618 return wrap (p
, truncate (file
, len
));
620 p
->last_errno
= EINVAL
;
632 /* We deliberately don't use fd 0. It's probably stdin anyway. */
633 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
637 if (p
->fd_buddy
[i
] < 0)
638 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
639 if (p
->fd_buddy
[j
] < 0)
641 /* Found two free fd:s. Set stat to allocated and mark
650 /* Poison the FD map to make bugs apparent. */
657 p
->last_errno
= EMFILE
;
661 /* Stub functions for pipe support. They should always be overridden in
662 targets using the pipe support, but that's up to the target. */
664 /* Called when the simulator says that the pipe at (reader, writer) is
665 now empty (so the writer should leave its waiting state). */
668 os_pipe_empty (p
, reader
, writer
)
675 /* Called when the simulator says the pipe at (reader, writer) is now
676 non-empty (so the writer should wait). */
679 os_pipe_nonempty (p
, reader
, writer
)
691 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
695 /* Zero out all pipe state. Don't call callbacks for non-empty
696 pipes; the target program has likely terminated at this point
697 or we're called at initialization time. */
699 p
->pipe_buffer
[i
].size
= 0;
700 p
->pipe_buffer
[i
].buffer
= NULL
;
702 next
= p
->fd_buddy
[i
];
708 if (j
== MAX_CALLBACK_FDS
)
710 next
= p
->fd_buddy
[j
];
712 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
734 for (i
= 0; i
< 3; i
++)
737 p
->fd_buddy
[i
] = i
- 1;
739 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
740 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
742 p
->syscall_map
= cb_init_syscall_map
;
743 p
->errno_map
= cb_init_errno_map
;
744 p
->open_map
= cb_init_open_map
;
753 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
756 va_start (args
, format
);
758 vfprintf (stdout
, format
, args
);
764 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
766 vprintf (format
, args
);
771 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
773 vfprintf (stderr
, format
, args
);
778 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
781 va_start (args
, format
);
783 vfprintf (stderr
, format
, args
);
784 fprintf (stderr
, "\n");
790 host_callback default_callback
=
825 os_printf_filtered
, /* deprecated */
828 os_evprintf_filtered
,
834 { -1, }, /* fd_buddy */
836 { { 0, 0 }, }, /* pipe_buffer */
844 /* Defaults expected to be overridden at initialization, where needed. */
845 BFD_ENDIAN_UNKNOWN
, /* target_endian */
846 4, /* target_sizeof_int */
851 /* Read in a file describing the target's system call values.
852 E.g. maybe someone will want to use something other than newlib.
853 This assumes that the basic system call recognition and value passing/
854 returning is supported. So maybe some coding/recompilation will be
855 necessary, but not as much.
857 If an error occurs, the existing mapping is not changed. */
860 cb_read_target_syscall_maps (cb
, file
)
864 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
865 const char *stat_map
;
868 if ((f
= fopen (file
, "r")) == NULL
)
871 /* ... read in and parse file ... */
874 return CB_RC_NO_MEM
; /* FIXME:wip */
876 /* Free storage allocated for any existing maps. */
878 free (cb
->syscall_map
);
880 free (cb
->errno_map
);
884 free (cb
->signal_map
);
886 free ((PTR
) cb
->stat_map
);
888 cb
->syscall_map
= syscall_map
;
889 cb
->errno_map
= errno_map
;
890 cb
->open_map
= open_map
;
891 cb
->signal_map
= signal_map
;
892 cb
->stat_map
= stat_map
;
897 /* Translate the target's version of a syscall number to the host's.
898 This isn't actually the host's version, rather a canonical form.
899 ??? Perhaps this should be renamed to ..._canon_syscall. */
902 cb_target_to_host_syscall (cb
, target_val
)
906 CB_TARGET_DEFS_MAP
*m
;
908 for (m
= &cb
->syscall_map
[0]; m
->target_val
!= -1; ++m
)
909 if (m
->target_val
== target_val
)
915 /* FIXME: sort tables if large.
916 Alternatively, an obvious improvement for errno conversion is
917 to machine generate a function with a large switch(). */
919 /* Translate the host's version of errno to the target's. */
922 cb_host_to_target_errno (cb
, host_val
)
926 CB_TARGET_DEFS_MAP
*m
;
928 for (m
= &cb
->errno_map
[0]; m
->host_val
; ++m
)
929 if (m
->host_val
== host_val
)
930 return m
->target_val
;
932 /* ??? Which error to return in this case is up for grabs.
933 Note that some missing values may have standard alternatives.
934 For now return 0 and require caller to deal with it. */
938 /* Given a set of target bitmasks for the open system call,
939 return the host equivalent.
940 Mapping open flag values is best done by looping so there's no need
941 to machine generate this function. */
944 cb_target_to_host_open (cb
, target_val
)
949 CB_TARGET_DEFS_MAP
*m
;
951 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
953 switch (m
->target_val
)
955 /* O_RDONLY can be (and usually is) 0 which needs to be treated
957 case TARGET_O_RDONLY
:
958 case TARGET_O_WRONLY
:
960 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
962 host_val
|= m
->host_val
;
963 /* Handle the host/target differentiating between binary and
964 text mode. Only one case is of importance */
965 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
966 host_val
|= O_BINARY
;
970 if ((m
->target_val
& target_val
) == m
->target_val
)
971 host_val
|= m
->host_val
;
979 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
983 cb_store_target_endian (cb
, p
, size
, val
)
987 long val
; /* ??? must be as big as target word size */
989 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
1008 /* Translate a host's stat struct into a target's.
1009 If HS is NULL, just compute the length of the buffer required,
1012 The result is the size of the target's stat struct,
1013 or zero if an error occurred during the translation. */
1016 cb_host_to_target_stat (cb
, hs
, ts
)
1018 const struct stat
*hs
;
1021 const char *m
= cb
->stat_map
;
1030 char *q
= strchr (m
, ',');
1033 /* FIXME: Use sscanf? */
1036 /* FIXME: print error message */
1039 size
= atoi (q
+ 1);
1042 /* FIXME: print error message */
1050 /* Defined here to avoid emacs indigestion on a lone "else". */
1053 else if (strncmp (m, #FLD, q - m) == 0) \
1054 cb_store_target_endian (cb, p, size, hs->FLD)
1056 #ifdef HAVE_STRUCT_STAT_ST_DEV
1059 #ifdef HAVE_STRUCT_STAT_ST_INO
1062 #ifdef HAVE_STRUCT_STAT_ST_MODE
1065 #ifdef HAVE_STRUCT_STAT_ST_NLINK
1068 #ifdef HAVE_STRUCT_STAT_ST_UID
1071 #ifdef HAVE_STRUCT_STAT_ST_GID
1074 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1077 #ifdef HAVE_STRUCT_STAT_ST_SIZE
1080 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1083 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1086 #ifdef HAVE_STRUCT_STAT_ST_ATIME
1089 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1092 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1098 /* Unsupported field, store 0. */
1099 cb_store_target_endian (cb
, p
, size
, 0);
1103 m
= strchr (q
, ':');
1108 return p
- (char *) ts
;
1111 /* Cover functions to the vfprintf callbacks.
1113 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
1114 a larger "remote target subsystem"] with a well defined interface, then
1115 one would think that the subsystem would provide these. However, until
1116 one is allowed to create such a subsystem (with its own source tree
1117 independent of any particular user), such a critter can't exist. Thus
1118 these functions are here for the time being. */
1121 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
1126 p
->vprintf_filtered (p
, fmt
, ap
);
1131 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
1136 p
->evprintf_filtered (p
, fmt
, ap
);
1141 cb_is_stdin (host_callback
*cb
, int fd
)
1143 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 0;
1147 cb_is_stdout (host_callback
*cb
, int fd
)
1149 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 1;
1153 cb_is_stderr (host_callback
*cb
, int fd
)
1155 return fdbad (cb
, fd
) ? 0 : fdmap (cb
, fd
) == 2;