1 /* Remote target callback routines.
2 Copyright 1995, 1996, 1997, 2000, 2002, 2003, 2004
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 2 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 GAS; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file provides a standard way for targets to talk to the host OS
29 #ifdef ANSI_PROTOTYPES
48 #include <sys/types.h>
50 #include "gdb/callback.h"
51 #include "targ-vals.h"
57 /* ??? sim_cb_printf should be cb_printf, but until the callback support is
58 broken out of the simulator directory, these are here to not require
60 void sim_cb_printf
PARAMS ((host_callback
*, const char *, ...));
61 void sim_cb_eprintf
PARAMS ((host_callback
*, const char *, ...));
63 extern CB_TARGET_DEFS_MAP cb_init_syscall_map
[];
64 extern CB_TARGET_DEFS_MAP cb_init_errno_map
[];
65 extern CB_TARGET_DEFS_MAP cb_init_open_map
[];
67 extern int system
PARAMS ((const char *));
69 static int os_init
PARAMS ((host_callback
*));
70 static int os_shutdown
PARAMS ((host_callback
*));
71 static int os_unlink
PARAMS ((host_callback
*, const char *));
72 static long os_time
PARAMS ((host_callback
*, long *));
73 static int os_system
PARAMS ((host_callback
*, const char *));
74 static int os_rename
PARAMS ((host_callback
*, const char *, const char *));
75 static int os_write_stdout
PARAMS ((host_callback
*, const char *, int));
76 static void os_flush_stdout
PARAMS ((host_callback
*));
77 static int os_write_stderr
PARAMS ((host_callback
*, const char *, int));
78 static void os_flush_stderr
PARAMS ((host_callback
*));
79 static int os_write
PARAMS ((host_callback
*, int, const char *, int));
80 static int os_read_stdin
PARAMS ((host_callback
*, char *, int));
81 static int os_read
PARAMS ((host_callback
*, int, char *, int));
82 static int os_open
PARAMS ((host_callback
*, const char *, int));
83 static int os_lseek
PARAMS ((host_callback
*, int, long, int));
84 static int os_isatty
PARAMS ((host_callback
*, int));
85 static int os_get_errno
PARAMS ((host_callback
*));
86 static int os_close
PARAMS ((host_callback
*, int));
87 static void os_vprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
88 static void os_evprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
89 static void os_error
PARAMS ((host_callback
*, const char *, ...));
90 static int fdmap
PARAMS ((host_callback
*, int));
91 static int fdbad
PARAMS ((host_callback
*, int));
92 static int wrap
PARAMS ((host_callback
*, int));
94 /* Set the callback copy of errno from what we see now. */
101 p
->last_errno
= errno
;
105 /* Make sure the FD provided is ok. If not, return non-zero
113 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| !p
->fdopen
[fd
])
115 p
->last_errno
= EINVAL
;
136 result
= fdbad (p
, fd
);
139 result
= wrap (p
, close (fdmap (p
, fd
)));
140 if (result
== 0 && !p
->alwaysopen
[fd
])
147 /* taken from gdb/util.c:notice_quit() - should be in a library */
150 #if defined(__GO32__) || defined (_MSC_VER)
155 #if defined(__GO32__)
171 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
175 #if defined (_MSC_VER)
176 /* NB - this will not compile! */
177 int k
= win32pollquit();
186 #define os_poll_quit 0
187 #endif /* defined(__GO32__) || defined(_MSC_VER) */
193 return cb_host_to_target_errno (p
, p
->last_errno
);
204 result
= fdbad (p
, fd
);
207 result
= wrap (p
, isatty (fdmap (p
, fd
)));
213 os_lseek (p
, fd
, off
, way
)
221 result
= fdbad (p
, fd
);
224 result
= lseek (fdmap (p
, fd
), off
, way
);
229 os_open (p
, name
, flags
)
235 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
239 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
242 p
->last_errno
= errno
;
250 p
->last_errno
= EMFILE
;
255 os_read (p
, fd
, buf
, len
)
263 result
= fdbad (p
, fd
);
266 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
271 os_read_stdin (p
, buf
, len
)
276 return wrap (p
, read (0, buf
, len
));
280 os_write (p
, fd
, buf
, len
)
289 result
= fdbad (p
, fd
);
292 real_fd
= fdmap (p
, fd
);
296 result
= wrap (p
, write (real_fd
, buf
, len
));
299 result
= p
->write_stdout (p
, buf
, len
);
302 result
= p
->write_stderr (p
, buf
, len
);
309 os_write_stdout (p
, buf
, len
)
310 host_callback
*p ATTRIBUTE_UNUSED
;
314 return fwrite (buf
, 1, len
, stdout
);
319 host_callback
*p ATTRIBUTE_UNUSED
;
325 os_write_stderr (p
, buf
, len
)
326 host_callback
*p ATTRIBUTE_UNUSED
;
330 return fwrite (buf
, 1, len
, stderr
);
335 host_callback
*p ATTRIBUTE_UNUSED
;
341 os_rename (p
, f1
, f2
)
346 return wrap (p
, rename (f1
, f2
));
355 return wrap (p
, system (s
));
363 return wrap (p
, time (t
));
372 return wrap (p
, unlink (f1
));
376 os_stat (p
, file
, buf
)
381 /* ??? There is an issue of when to translate to the target layout.
382 One could do that inside this function, or one could have the
383 caller do it. It's more flexible to let the caller do it, though
384 I'm not sure the flexibility will ever be useful. */
385 return wrap (p
, stat (file
, buf
));
389 os_fstat (p
, fd
, buf
)
396 /* ??? There is an issue of when to translate to the target layout.
397 One could do that inside this function, or one could have the
398 caller do it. It's more flexible to let the caller do it, though
399 I'm not sure the flexibility will ever be useful. */
400 return wrap (p
, fstat (fdmap (p
, fd
), buf
));
404 os_ftruncate (p
, fd
, len
)
411 result
= fdbad (p
, fd
);
414 result
= wrap (p
, ftruncate (fdmap (p
, fd
), len
));
419 os_truncate (p
, file
, len
)
424 return wrap (p
, truncate (file
, len
));
432 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
434 if (p
->fdopen
[i
] && !p
->alwaysopen
[i
]) {
449 for (i
= 0; i
< 3; i
++)
453 p
->alwaysopen
[i
] = 1;
456 p
->syscall_map
= cb_init_syscall_map
;
457 p
->errno_map
= cb_init_errno_map
;
458 p
->open_map
= cb_init_open_map
;
467 #ifdef ANSI_PROTOTYPES
468 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
470 os_printf_filtered (p
, va_alist
)
476 #ifdef ANSI_PROTOTYPES
477 va_start (args
, format
);
482 format
= va_arg (args
, char *);
485 vfprintf (stdout
, format
, args
);
491 #ifdef ANSI_PROTOTYPES
492 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
494 os_vprintf_filtered (p
, format
, args
)
500 vprintf (format
, args
);
505 #ifdef ANSI_PROTOTYPES
506 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
508 os_evprintf_filtered (p
, format
, args
)
514 vfprintf (stderr
, format
, args
);
519 #ifdef ANSI_PROTOTYPES
520 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
522 os_error (p
, va_alist
)
528 #ifdef ANSI_PROTOTYPES
529 va_start (args
, format
);
534 format
= va_arg (args
, char *);
537 vfprintf (stderr
, format
, args
);
538 fprintf (stderr
, "\n");
544 host_callback default_callback
=
574 os_printf_filtered
, /* deprecated */
577 os_evprintf_filtered
,
584 { 0, }, /* alwaysopen */
595 /* Read in a file describing the target's system call values.
596 E.g. maybe someone will want to use something other than newlib.
597 This assumes that the basic system call recognition and value passing/
598 returning is supported. So maybe some coding/recompilation will be
599 necessary, but not as much.
601 If an error occurs, the existing mapping is not changed. */
604 cb_read_target_syscall_maps (cb
, file
)
608 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
609 const char *stat_map
;
612 if ((f
= fopen (file
, "r")) == NULL
)
615 /* ... read in and parse file ... */
618 return CB_RC_NO_MEM
; /* FIXME:wip */
620 /* Free storage allocated for any existing maps. */
622 free (cb
->syscall_map
);
624 free (cb
->errno_map
);
628 free (cb
->signal_map
);
630 free ((PTR
) cb
->stat_map
);
632 cb
->syscall_map
= syscall_map
;
633 cb
->errno_map
= errno_map
;
634 cb
->open_map
= open_map
;
635 cb
->signal_map
= signal_map
;
636 cb
->stat_map
= stat_map
;
641 /* Translate the target's version of a syscall number to the host's.
642 This isn't actually the host's version, rather a canonical form.
643 ??? Perhaps this should be renamed to ..._canon_syscall. */
646 cb_target_to_host_syscall (cb
, target_val
)
650 CB_TARGET_DEFS_MAP
*m
;
652 for (m
= &cb
->syscall_map
[0]; m
->target_val
!= -1; ++m
)
653 if (m
->target_val
== target_val
)
659 /* FIXME: sort tables if large.
660 Alternatively, an obvious improvement for errno conversion is
661 to machine generate a function with a large switch(). */
663 /* Translate the host's version of errno to the target's. */
666 cb_host_to_target_errno (cb
, host_val
)
670 CB_TARGET_DEFS_MAP
*m
;
672 for (m
= &cb
->errno_map
[0]; m
->host_val
; ++m
)
673 if (m
->host_val
== host_val
)
674 return m
->target_val
;
676 /* ??? Which error to return in this case is up for grabs.
677 Note that some missing values may have standard alternatives.
678 For now return 0 and require caller to deal with it. */
682 /* Given a set of target bitmasks for the open system call,
683 return the host equivalent.
684 Mapping open flag values is best done by looping so there's no need
685 to machine generate this function. */
688 cb_target_to_host_open (cb
, target_val
)
693 CB_TARGET_DEFS_MAP
*m
;
695 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
697 switch (m
->target_val
)
699 /* O_RDONLY can be (and usually is) 0 which needs to be treated
701 case TARGET_O_RDONLY
:
702 case TARGET_O_WRONLY
:
704 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
706 host_val
|= m
->host_val
;
707 /* Handle the host/target differentiating between binary and
708 text mode. Only one case is of importance */
709 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
710 host_val
|= O_BINARY
;
714 if ((m
->target_val
& target_val
) == m
->target_val
)
715 host_val
|= m
->host_val
;
723 /* Utility for cb_host_to_target_stat to store values in the target's
727 store (p
, size
, val
, big_p
)
730 long val
; /* ??? must be as big as target word size */
752 /* Translate a host's stat struct into a target's.
753 If HS is NULL, just compute the length of the buffer required,
756 The result is the size of the target's stat struct,
757 or zero if an error occurred during the translation. */
760 cb_host_to_target_stat (cb
, hs
, ts
)
762 const struct stat
*hs
;
765 const char *m
= cb
->stat_map
;
775 char *q
= strchr (m
, ',');
778 /* FIXME: Use sscanf? */
781 /* FIXME: print error message */
787 /* FIXME: print error message */
793 if (strncmp (m
, "st_dev", q
- m
) == 0)
794 store (p
, size
, hs
->st_dev
, big_p
);
795 else if (strncmp (m
, "st_ino", q
- m
) == 0)
796 store (p
, size
, hs
->st_ino
, big_p
);
799 store (p
, size
, 0, big_p
); /* unsupported field, store 0 */
808 return p
- (char *) ts
;
811 /* Cover functions to the vfprintf callbacks.
813 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
814 a larger "remote target subsystem"] with a well defined interface, then
815 one would think that the subsystem would provide these. However, until
816 one is allowed to create such a subsystem (with its own source tree
817 independent of any particular user), such a critter can't exist. Thus
818 these functions are here for the time being. */
821 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
826 p
->vprintf_filtered (p
, fmt
, ap
);
831 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
836 p
->evprintf_filtered (p
, fmt
, ap
);