1 /* Remote target callback routines.
2 Copyright 1995, 1996, 1997 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 2 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 GAS; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This file provides a standard way for targets to talk to the host OS
28 #ifdef ANSI_PROTOTYPES
47 #include <sys/types.h>
50 #include "targ-vals.h"
56 /* ??? sim_cb_printf should be cb_printf, but until the callback support is
57 broken out of the simulator directory, these are here to not require
59 void sim_cb_printf
PARAMS ((host_callback
*, const char *, ...));
60 void sim_cb_eprintf
PARAMS ((host_callback
*, const char *, ...));
62 extern CB_TARGET_DEFS_MAP cb_init_syscall_map
[];
63 extern CB_TARGET_DEFS_MAP cb_init_errno_map
[];
64 extern CB_TARGET_DEFS_MAP cb_init_open_map
[];
66 extern int system
PARAMS ((const char *));
68 static int os_init
PARAMS ((host_callback
*));
69 static int os_shutdown
PARAMS ((host_callback
*));
70 static int os_unlink
PARAMS ((host_callback
*, const char *));
71 static long os_time
PARAMS ((host_callback
*, long *));
72 static int os_system
PARAMS ((host_callback
*, const char *));
73 static int os_rename
PARAMS ((host_callback
*, const char *, const char *));
74 static int os_write_stdout
PARAMS ((host_callback
*, const char *, int));
75 static void os_flush_stdout
PARAMS ((host_callback
*));
76 static int os_write_stderr
PARAMS ((host_callback
*, const char *, int));
77 static void os_flush_stderr
PARAMS ((host_callback
*));
78 static int os_write
PARAMS ((host_callback
*, int, const char *, int));
79 static int os_read_stdin
PARAMS ((host_callback
*, char *, int));
80 static int os_read
PARAMS ((host_callback
*, int, char *, int));
81 static int os_open
PARAMS ((host_callback
*, const char *, int));
82 static int os_lseek
PARAMS ((host_callback
*, int, long, int));
83 static int os_isatty
PARAMS ((host_callback
*, int));
84 static int os_get_errno
PARAMS ((host_callback
*));
85 static int os_close
PARAMS ((host_callback
*, int));
86 static void os_vprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
87 static void os_evprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
88 static void os_error
PARAMS ((host_callback
*, const char *, ...));
89 static int fdmap
PARAMS ((host_callback
*, int));
90 static int fdbad
PARAMS ((host_callback
*, int));
91 static int wrap
PARAMS ((host_callback
*, int));
92 static int enosys
PARAMS ((host_callback
*, int));
94 /* Set the callback copy of errno from what we see now. */
101 p
->last_errno
= errno
;
105 /* Return a value indicating the system call isn't present. */
113 p
->last_errno
= ENOSYS
;
115 p
->last_errno
= EINVAL
;
120 /* Make sure the FD provided is ok. If not, return non-zero
128 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| !p
->fdopen
[fd
])
130 p
->last_errno
= EINVAL
;
151 result
= fdbad (p
, fd
);
154 result
= wrap (p
, close (fdmap (p
, fd
)));
155 if(result
== 0 && !p
->alwaysopen
[fd
])
162 /* taken from gdb/util.c:notice_quit() - should be in a library */
165 #if defined(__GO32__) || defined (_MSC_VER)
170 #if defined(__GO32__)
186 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
190 #if defined (_MSC_VER)
191 /* NB - this will not compile! */
192 int k
= win32pollquit();
201 #define os_poll_quit 0
202 #endif /* defined(__GO32__) || defined(_MSC_VER) */
208 return cb_host_to_target_errno (p
, p
->last_errno
);
219 result
= fdbad (p
, fd
);
222 result
= wrap (p
, isatty (fdmap (p
, fd
)));
228 os_lseek (p
, fd
, off
, way
)
236 result
= fdbad (p
, fd
);
239 result
= lseek (fdmap (p
, fd
), off
, way
);
244 os_open (p
, name
, flags
)
250 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
254 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
257 p
->last_errno
= errno
;
265 p
->last_errno
= EMFILE
;
270 os_read (p
, fd
, buf
, len
)
278 result
= fdbad (p
, fd
);
281 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
286 os_read_stdin (p
, buf
, len
)
291 return wrap (p
, read (0, buf
, len
));
295 os_write (p
, fd
, buf
, len
)
304 result
= fdbad (p
, fd
);
307 real_fd
= fdmap (p
, fd
);
311 result
= wrap (p
, write (real_fd
, buf
, len
));
314 result
= p
->write_stdout (p
, buf
, len
);
317 result
= p
->write_stderr (p
, buf
, len
);
324 os_write_stdout (p
, buf
, len
)
329 return fwrite(buf
, 1, len
, stdout
);
340 os_write_stderr (p
, buf
, len
)
345 return fwrite(buf
, 1, len
, stderr
);
356 os_rename (p
, f1
, f2
)
361 return wrap (p
, rename (f1
, f2
));
370 return wrap (p
, system (s
));
378 return wrap (p
, time (t
));
387 return wrap (p
, unlink (f1
));
391 os_stat (p
, file
, buf
)
396 return wrap (p
, stat (file
, buf
));
400 os_fstat (p
, fd
, buf
)
405 return wrap (p
, fstat (fd
, buf
));
413 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
415 if (p
->fdopen
[i
] && !p
->alwaysopen
[i
]) {
430 for (i
= 0; i
< 3; i
++)
434 p
->alwaysopen
[i
] = 1;
437 p
->syscall_map
= cb_init_syscall_map
;
438 p
->errno_map
= cb_init_errno_map
;
439 p
->open_map
= cb_init_open_map
;
448 #ifdef ANSI_PROTOTYPES
449 os_printf_filtered (host_callback
*p
, const char *format
, ...)
451 os_printf_filtered (p
, va_alist
)
457 #ifdef ANSI_PROTOTYPES
458 va_start (args
, format
);
463 format
= va_arg (args
, char *);
466 vfprintf (stdout
, format
, args
);
472 #ifdef ANSI_PROTOTYPES
473 os_vprintf_filtered (host_callback
*p
, const char *format
, va_list args
)
475 os_vprintf_filtered (p
, format
, args
)
481 vprintf (format
, args
);
486 #ifdef ANSI_PROTOTYPES
487 os_evprintf_filtered (host_callback
*p
, const char *format
, va_list args
)
489 os_evprintf_filtered (p
, format
, args
)
495 vfprintf (stderr
, format
, args
);
500 #ifdef ANSI_PROTOTYPES
501 os_error (host_callback
*p
, const char *format
, ...)
503 os_error (p
, va_alist
)
509 #ifdef ANSI_PROTOTYPES
510 va_start (args
, format
);
515 format
= va_arg (args
, char *);
518 vfprintf (stderr
, format
, args
);
519 fprintf (stderr
, "\n");
525 host_callback default_callback
=
552 os_printf_filtered
, /* deprecated */
555 os_evprintf_filtered
,
562 { 0, }, /* alwaysopen */
573 /* Read in a file describing the target's system call values.
574 This allows simulators, etc. to support arbitrary libraries
575 without having to be recompiled.
576 E.g. maybe someone will want to use something other than newlib.
578 If an error occurs, the existing mapping is not changed. */
581 cb_read_target_syscall_maps (cb
, file
)
585 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
586 const char *stat_map
;
589 if ((f
= fopen (file
, "r")) == NULL
)
592 /* ... read in and parse file ... */
595 return CB_RC_NO_MEM
; /* FIXME:wip */
597 /* Free storage allocated for any existing maps. */
599 free (cb
->syscall_map
);
601 free (cb
->errno_map
);
605 free (cb
->signal_map
);
607 free ((PTR
) cb
->stat_map
);
609 cb
->syscall_map
= syscall_map
;
610 cb
->errno_map
= errno_map
;
611 cb
->open_map
= open_map
;
612 cb
->signal_map
= signal_map
;
613 cb
->stat_map
= stat_map
;
618 /* Translate the target's version of a syscall number to the host's.
619 This isn't actually the host's version, rather a canonical form.
620 ??? Perhaps this should be renamed to ..._canon_syscall. */
623 cb_target_to_host_syscall (cb
, target_val
)
627 CB_TARGET_DEFS_MAP
*m
;
629 for (m
= &cb
->syscall_map
[0]; m
->target_val
!= -1; ++m
)
630 if (m
->target_val
== target_val
)
636 /* FIXME: sort tables if large.
637 Alternatively, an obvious improvement for errno conversion is
638 to machine generate a function with a large switch(). */
640 /* Translate the host's version of errno to the target's. */
643 cb_host_to_target_errno (cb
, host_val
)
647 CB_TARGET_DEFS_MAP
*m
;
649 for (m
= &cb
->errno_map
[0]; m
->host_val
; ++m
)
650 if (m
->host_val
== host_val
)
651 return m
->target_val
;
653 /* ??? Which error to return in this case is up for grabs.
654 Note that some missing values may have standard alternatives.
655 For now return 0 and require caller to deal with it. */
659 /* Given a set of target bitmasks for the open system call,
660 return the host equivalent.
661 Mapping open flag values is best done by looping so there's no need
662 to machine generate this function. */
665 cb_target_to_host_open (cb
, target_val
)
670 CB_TARGET_DEFS_MAP
*m
;
672 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
674 switch (m
->target_val
)
676 /* O_RDONLY can be (and usually is) 0 which needs to be treated
678 case TARGET_O_RDONLY
:
679 case TARGET_O_WRONLY
:
681 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
683 host_val
|= m
->host_val
;
684 /* Handle the host/target differentiating between binary and
685 text mode. Only one case is of importance */
686 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
687 host_val
|= O_BINARY
;
691 if ((m
->target_val
& target_val
) == m
->target_val
)
692 host_val
|= m
->host_val
;
700 /* Utility for cb_host_to_target_stat to store values in the target's
704 store (p
, size
, val
, big_p
)
707 long val
; /* ??? must be as big as target word size */
729 /* Translate a host's stat struct into a target's.
731 BIG_P is non-zero if the target is big-endian.
732 The result is the size of the target's stat struct. */
735 cb_host_to_target_stat (cb
, hs
, ts
, big_p
)
737 const struct stat
*hs
;
741 const char *m
= cb
->stat_map
;
746 char *q
= strchr (m
, ',');
749 /* FIXME: Use sscanf? */
752 /* FIXME: print error message */
758 /* FIXME: print error message */
762 if (strncmp (m
, "st_dev", q
- m
) == 0)
763 store (p
, size
, hs
->st_dev
, big_p
);
764 else if (strncmp (m
, "st_ino", q
- m
) == 0)
765 store (p
, size
, hs
->st_ino
, big_p
);
768 store (p
, size
, 0, big_p
); /* unsupported field, store 0 */
777 /* Cover functions to the vfprintf callbacks.
779 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
780 a larger "remote target subsystem"] with a well defined interface, then
781 one would think that the subsystem would provide these. However, until
782 one is allowed to create such a subsystem (with its own source tree
783 independent of any particular user), such a critter can't exist. Thus
784 these functions are here for the time being. */
787 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
792 p
->vprintf_filtered (p
, fmt
, ap
);
797 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
802 p
->evprintf_filtered (p
, fmt
, ap
);