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>
49 #include "gdb/callback.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));
93 /* Set the callback copy of errno from what we see now. */
100 p
->last_errno
= errno
;
104 /* Make sure the FD provided is ok. If not, return non-zero
112 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| !p
->fdopen
[fd
])
114 p
->last_errno
= EINVAL
;
135 result
= fdbad (p
, fd
);
138 result
= wrap (p
, close (fdmap (p
, fd
)));
139 if (result
== 0 && !p
->alwaysopen
[fd
])
146 /* taken from gdb/util.c:notice_quit() - should be in a library */
149 #if defined(__GO32__) || defined (_MSC_VER)
154 #if defined(__GO32__)
170 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
174 #if defined (_MSC_VER)
175 /* NB - this will not compile! */
176 int k
= win32pollquit();
185 #define os_poll_quit 0
186 #endif /* defined(__GO32__) || defined(_MSC_VER) */
192 return cb_host_to_target_errno (p
, p
->last_errno
);
203 result
= fdbad (p
, fd
);
206 result
= wrap (p
, isatty (fdmap (p
, fd
)));
212 os_lseek (p
, fd
, off
, way
)
220 result
= fdbad (p
, fd
);
223 result
= lseek (fdmap (p
, fd
), off
, way
);
228 os_open (p
, name
, flags
)
234 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
238 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
241 p
->last_errno
= errno
;
249 p
->last_errno
= EMFILE
;
254 os_read (p
, fd
, buf
, len
)
262 result
= fdbad (p
, fd
);
265 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
270 os_read_stdin (p
, buf
, len
)
275 return wrap (p
, read (0, buf
, len
));
279 os_write (p
, fd
, buf
, len
)
288 result
= fdbad (p
, fd
);
291 real_fd
= fdmap (p
, fd
);
295 result
= wrap (p
, write (real_fd
, buf
, len
));
298 result
= p
->write_stdout (p
, buf
, len
);
301 result
= p
->write_stderr (p
, buf
, len
);
308 os_write_stdout (p
, buf
, len
)
309 host_callback
*p ATTRIBUTE_UNUSED
;
313 return fwrite (buf
, 1, len
, stdout
);
318 host_callback
*p ATTRIBUTE_UNUSED
;
324 os_write_stderr (p
, buf
, len
)
325 host_callback
*p ATTRIBUTE_UNUSED
;
329 return fwrite (buf
, 1, len
, stderr
);
334 host_callback
*p ATTRIBUTE_UNUSED
;
340 os_rename (p
, f1
, f2
)
345 return wrap (p
, rename (f1
, f2
));
354 return wrap (p
, system (s
));
362 return wrap (p
, time (t
));
371 return wrap (p
, unlink (f1
));
375 os_stat (p
, file
, buf
)
380 /* ??? There is an issue of when to translate to the target layout.
381 One could do that inside this function, or one could have the
382 caller do it. It's more flexible to let the caller do it, though
383 I'm not sure the flexibility will ever be useful. */
384 return wrap (p
, stat (file
, buf
));
388 os_fstat (p
, fd
, buf
)
395 /* ??? There is an issue of when to translate to the target layout.
396 One could do that inside this function, or one could have the
397 caller do it. It's more flexible to let the caller do it, though
398 I'm not sure the flexibility will ever be useful. */
399 return wrap (p
, fstat (fdmap (p
, fd
), buf
));
403 os_ftruncate (p
, fd
, len
)
410 result
= fdbad (p
, fd
);
413 result
= wrap (p
, ftruncate (fdmap (p
, fd
), len
));
418 os_truncate (p
, file
, len
)
423 return wrap (p
, truncate (file
, len
));
431 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
433 if (p
->fdopen
[i
] && !p
->alwaysopen
[i
]) {
448 for (i
= 0; i
< 3; i
++)
452 p
->alwaysopen
[i
] = 1;
455 p
->syscall_map
= cb_init_syscall_map
;
456 p
->errno_map
= cb_init_errno_map
;
457 p
->open_map
= cb_init_open_map
;
466 #ifdef ANSI_PROTOTYPES
467 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
469 os_printf_filtered (p
, va_alist
)
475 #ifdef ANSI_PROTOTYPES
476 va_start (args
, format
);
481 format
= va_arg (args
, char *);
484 vfprintf (stdout
, format
, args
);
490 #ifdef ANSI_PROTOTYPES
491 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
493 os_vprintf_filtered (p
, format
, args
)
499 vprintf (format
, args
);
504 #ifdef ANSI_PROTOTYPES
505 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
507 os_evprintf_filtered (p
, format
, args
)
513 vfprintf (stderr
, format
, args
);
518 #ifdef ANSI_PROTOTYPES
519 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
521 os_error (p
, va_alist
)
527 #ifdef ANSI_PROTOTYPES
528 va_start (args
, format
);
533 format
= va_arg (args
, char *);
536 vfprintf (stderr
, format
, args
);
537 fprintf (stderr
, "\n");
543 host_callback default_callback
=
573 os_printf_filtered
, /* deprecated */
576 os_evprintf_filtered
,
583 { 0, }, /* alwaysopen */
594 /* Read in a file describing the target's system call values.
595 E.g. maybe someone will want to use something other than newlib.
596 This assumes that the basic system call recognition and value passing/
597 returning is supported. So maybe some coding/recompilation will be
598 necessary, but not as much.
600 If an error occurs, the existing mapping is not changed. */
603 cb_read_target_syscall_maps (cb
, file
)
607 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
608 const char *stat_map
;
611 if ((f
= fopen (file
, "r")) == NULL
)
614 /* ... read in and parse file ... */
617 return CB_RC_NO_MEM
; /* FIXME:wip */
619 /* Free storage allocated for any existing maps. */
621 free (cb
->syscall_map
);
623 free (cb
->errno_map
);
627 free (cb
->signal_map
);
629 free ((PTR
) cb
->stat_map
);
631 cb
->syscall_map
= syscall_map
;
632 cb
->errno_map
= errno_map
;
633 cb
->open_map
= open_map
;
634 cb
->signal_map
= signal_map
;
635 cb
->stat_map
= stat_map
;
640 /* Translate the target's version of a syscall number to the host's.
641 This isn't actually the host's version, rather a canonical form.
642 ??? Perhaps this should be renamed to ..._canon_syscall. */
645 cb_target_to_host_syscall (cb
, target_val
)
649 CB_TARGET_DEFS_MAP
*m
;
651 for (m
= &cb
->syscall_map
[0]; m
->target_val
!= -1; ++m
)
652 if (m
->target_val
== target_val
)
658 /* FIXME: sort tables if large.
659 Alternatively, an obvious improvement for errno conversion is
660 to machine generate a function with a large switch(). */
662 /* Translate the host's version of errno to the target's. */
665 cb_host_to_target_errno (cb
, host_val
)
669 CB_TARGET_DEFS_MAP
*m
;
671 for (m
= &cb
->errno_map
[0]; m
->host_val
; ++m
)
672 if (m
->host_val
== host_val
)
673 return m
->target_val
;
675 /* ??? Which error to return in this case is up for grabs.
676 Note that some missing values may have standard alternatives.
677 For now return 0 and require caller to deal with it. */
681 /* Given a set of target bitmasks for the open system call,
682 return the host equivalent.
683 Mapping open flag values is best done by looping so there's no need
684 to machine generate this function. */
687 cb_target_to_host_open (cb
, target_val
)
692 CB_TARGET_DEFS_MAP
*m
;
694 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
696 switch (m
->target_val
)
698 /* O_RDONLY can be (and usually is) 0 which needs to be treated
700 case TARGET_O_RDONLY
:
701 case TARGET_O_WRONLY
:
703 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
705 host_val
|= m
->host_val
;
706 /* Handle the host/target differentiating between binary and
707 text mode. Only one case is of importance */
708 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
709 host_val
|= O_BINARY
;
713 if ((m
->target_val
& target_val
) == m
->target_val
)
714 host_val
|= m
->host_val
;
722 /* Utility for cb_host_to_target_stat to store values in the target's
726 store (p
, size
, val
, big_p
)
729 long val
; /* ??? must be as big as target word size */
751 /* Translate a host's stat struct into a target's.
752 If HS is NULL, just compute the length of the buffer required,
755 The result is the size of the target's stat struct,
756 or zero if an error occurred during the translation. */
759 cb_host_to_target_stat (cb
, hs
, ts
)
761 const struct stat
*hs
;
764 const char *m
= cb
->stat_map
;
774 char *q
= strchr (m
, ',');
777 /* FIXME: Use sscanf? */
780 /* FIXME: print error message */
786 /* FIXME: print error message */
792 if (strncmp (m
, "st_dev", q
- m
) == 0)
793 store (p
, size
, hs
->st_dev
, big_p
);
794 else if (strncmp (m
, "st_ino", q
- m
) == 0)
795 store (p
, size
, hs
->st_ino
, big_p
);
798 store (p
, size
, 0, big_p
); /* unsupported field, store 0 */
807 return p
- (char *) ts
;
810 /* Cover functions to the vfprintf callbacks.
812 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
813 a larger "remote target subsystem"] with a well defined interface, then
814 one would think that the subsystem would provide these. However, until
815 one is allowed to create such a subsystem (with its own source tree
816 independent of any particular user), such a critter can't exist. Thus
817 these functions are here for the time being. */
820 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
825 p
->vprintf_filtered (p
, fmt
, ap
);
830 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
835 p
->evprintf_filtered (p
, fmt
, ap
);