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
->fd_buddy
[fd
] < 0)
115 p
->last_errno
= EINVAL
;
137 result
= fdbad (p
, fd
);
140 /* If this file descripter has one or more buddies (originals /
141 duplicates from a dup), just remove it from the circular list. */
142 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
145 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
147 result
= wrap (p
, close (fdmap (p
, fd
)));
148 p
->fd_buddy
[fd
] = -1;
154 /* taken from gdb/util.c:notice_quit() - should be in a library */
157 #if defined(__GO32__) || defined (_MSC_VER)
162 #if defined(__GO32__)
178 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
182 #if defined (_MSC_VER)
183 /* NB - this will not compile! */
184 int k
= win32pollquit();
193 #define os_poll_quit 0
194 #endif /* defined(__GO32__) || defined(_MSC_VER) */
200 return cb_host_to_target_errno (p
, p
->last_errno
);
211 result
= fdbad (p
, fd
);
214 result
= wrap (p
, isatty (fdmap (p
, fd
)));
220 os_lseek (p
, fd
, off
, way
)
228 result
= fdbad (p
, fd
);
231 result
= lseek (fdmap (p
, fd
), off
, way
);
236 os_open (p
, name
, flags
)
242 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
244 if (p
->fd_buddy
[i
] < 0)
246 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
249 p
->last_errno
= errno
;
257 p
->last_errno
= EMFILE
;
262 os_read (p
, fd
, buf
, len
)
270 result
= fdbad (p
, fd
);
273 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
278 os_read_stdin (p
, buf
, len
)
283 return wrap (p
, read (0, buf
, len
));
287 os_write (p
, fd
, buf
, len
)
296 result
= fdbad (p
, fd
);
299 real_fd
= fdmap (p
, fd
);
303 result
= wrap (p
, write (real_fd
, buf
, len
));
306 result
= p
->write_stdout (p
, buf
, len
);
309 result
= p
->write_stderr (p
, buf
, len
);
316 os_write_stdout (p
, buf
, len
)
317 host_callback
*p ATTRIBUTE_UNUSED
;
321 return fwrite (buf
, 1, len
, stdout
);
326 host_callback
*p ATTRIBUTE_UNUSED
;
332 os_write_stderr (p
, buf
, len
)
333 host_callback
*p ATTRIBUTE_UNUSED
;
337 return fwrite (buf
, 1, len
, stderr
);
342 host_callback
*p ATTRIBUTE_UNUSED
;
348 os_rename (p
, f1
, f2
)
353 return wrap (p
, rename (f1
, f2
));
362 return wrap (p
, system (s
));
370 return wrap (p
, time (t
));
379 return wrap (p
, unlink (f1
));
383 os_stat (p
, file
, buf
)
388 /* ??? There is an issue of when to translate to the target layout.
389 One could do that inside this function, or one could have the
390 caller do it. It's more flexible to let the caller do it, though
391 I'm not sure the flexibility will ever be useful. */
392 return wrap (p
, stat (file
, buf
));
396 os_fstat (p
, fd
, buf
)
403 /* ??? There is an issue of when to translate to the target layout.
404 One could do that inside this function, or one could have the
405 caller do it. It's more flexible to let the caller do it, though
406 I'm not sure the flexibility will ever be useful. */
407 return wrap (p
, fstat (fdmap (p
, fd
), buf
));
411 os_ftruncate (p
, fd
, len
)
418 result
= fdbad (p
, fd
);
421 result
= wrap (p
, ftruncate (fdmap (p
, fd
), len
));
426 os_truncate (p
, file
, len
)
431 return wrap (p
, truncate (file
, len
));
439 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
443 next
= p
->fd_buddy
[i
];
449 if (j
== MAX_CALLBACK_FDS
)
451 next
= p
->fd_buddy
[j
];
453 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
475 for (i
= 0; i
< 3; i
++)
478 p
->fd_buddy
[i
] = i
- 1;
480 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
481 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
483 p
->syscall_map
= cb_init_syscall_map
;
484 p
->errno_map
= cb_init_errno_map
;
485 p
->open_map
= cb_init_open_map
;
494 #ifdef ANSI_PROTOTYPES
495 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
497 os_printf_filtered (p
, va_alist
)
503 #ifdef ANSI_PROTOTYPES
504 va_start (args
, format
);
509 format
= va_arg (args
, char *);
512 vfprintf (stdout
, format
, args
);
518 #ifdef ANSI_PROTOTYPES
519 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
521 os_vprintf_filtered (p
, format
, args
)
527 vprintf (format
, args
);
532 #ifdef ANSI_PROTOTYPES
533 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
535 os_evprintf_filtered (p
, format
, args
)
541 vfprintf (stderr
, format
, args
);
546 #ifdef ANSI_PROTOTYPES
547 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
549 os_error (p
, va_alist
)
555 #ifdef ANSI_PROTOTYPES
556 va_start (args
, format
);
561 format
= va_arg (args
, char *);
564 vfprintf (stderr
, format
, args
);
565 fprintf (stderr
, "\n");
571 host_callback default_callback
=
601 os_printf_filtered
, /* deprecated */
604 os_evprintf_filtered
,
610 { -1, }, /* fd_buddy */
621 /* Read in a file describing the target's system call values.
622 E.g. maybe someone will want to use something other than newlib.
623 This assumes that the basic system call recognition and value passing/
624 returning is supported. So maybe some coding/recompilation will be
625 necessary, but not as much.
627 If an error occurs, the existing mapping is not changed. */
630 cb_read_target_syscall_maps (cb
, file
)
634 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
635 const char *stat_map
;
638 if ((f
= fopen (file
, "r")) == NULL
)
641 /* ... read in and parse file ... */
644 return CB_RC_NO_MEM
; /* FIXME:wip */
646 /* Free storage allocated for any existing maps. */
648 free (cb
->syscall_map
);
650 free (cb
->errno_map
);
654 free (cb
->signal_map
);
656 free ((PTR
) cb
->stat_map
);
658 cb
->syscall_map
= syscall_map
;
659 cb
->errno_map
= errno_map
;
660 cb
->open_map
= open_map
;
661 cb
->signal_map
= signal_map
;
662 cb
->stat_map
= stat_map
;
667 /* Translate the target's version of a syscall number to the host's.
668 This isn't actually the host's version, rather a canonical form.
669 ??? Perhaps this should be renamed to ..._canon_syscall. */
672 cb_target_to_host_syscall (cb
, target_val
)
676 CB_TARGET_DEFS_MAP
*m
;
678 for (m
= &cb
->syscall_map
[0]; m
->target_val
!= -1; ++m
)
679 if (m
->target_val
== target_val
)
685 /* FIXME: sort tables if large.
686 Alternatively, an obvious improvement for errno conversion is
687 to machine generate a function with a large switch(). */
689 /* Translate the host's version of errno to the target's. */
692 cb_host_to_target_errno (cb
, host_val
)
696 CB_TARGET_DEFS_MAP
*m
;
698 for (m
= &cb
->errno_map
[0]; m
->host_val
; ++m
)
699 if (m
->host_val
== host_val
)
700 return m
->target_val
;
702 /* ??? Which error to return in this case is up for grabs.
703 Note that some missing values may have standard alternatives.
704 For now return 0 and require caller to deal with it. */
708 /* Given a set of target bitmasks for the open system call,
709 return the host equivalent.
710 Mapping open flag values is best done by looping so there's no need
711 to machine generate this function. */
714 cb_target_to_host_open (cb
, target_val
)
719 CB_TARGET_DEFS_MAP
*m
;
721 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
723 switch (m
->target_val
)
725 /* O_RDONLY can be (and usually is) 0 which needs to be treated
727 case TARGET_O_RDONLY
:
728 case TARGET_O_WRONLY
:
730 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
732 host_val
|= m
->host_val
;
733 /* Handle the host/target differentiating between binary and
734 text mode. Only one case is of importance */
735 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
736 host_val
|= O_BINARY
;
740 if ((m
->target_val
& target_val
) == m
->target_val
)
741 host_val
|= m
->host_val
;
749 /* Utility for cb_host_to_target_stat to store values in the target's
753 store (p
, size
, val
, big_p
)
756 long val
; /* ??? must be as big as target word size */
778 /* Translate a host's stat struct into a target's.
779 If HS is NULL, just compute the length of the buffer required,
782 The result is the size of the target's stat struct,
783 or zero if an error occurred during the translation. */
786 cb_host_to_target_stat (cb
, hs
, ts
)
788 const struct stat
*hs
;
791 const char *m
= cb
->stat_map
;
801 char *q
= strchr (m
, ',');
804 /* FIXME: Use sscanf? */
807 /* FIXME: print error message */
813 /* FIXME: print error message */
821 /* Defined here to avoid emacs indigestion on a lone "else". */
824 else if (strncmp (m, #FLD, q - m) == 0) \
825 store (p, size, hs->FLD, big_p)
827 #ifdef HAVE_STRUCT_STAT_ST_DEV
830 #ifdef HAVE_STRUCT_STAT_ST_INO
833 #ifdef HAVE_STRUCT_STAT_ST_MODE
836 #ifdef HAVE_STRUCT_STAT_ST_NLINK
839 #ifdef HAVE_STRUCT_STAT_ST_UID
842 #ifdef HAVE_STRUCT_STAT_ST_GID
845 #ifdef HAVE_STRUCT_STAT_ST_RDEV
848 #ifdef HAVE_STRUCT_STAT_ST_SIZE
851 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
854 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
857 #ifdef HAVE_STRUCT_STAT_ST_ATIME
860 #ifdef HAVE_STRUCT_STAT_ST_MTIME
863 #ifdef HAVE_STRUCT_STAT_ST_CTIME
869 store (p
, size
, 0, big_p
); /* unsupported field, store 0 */
878 return p
- (char *) ts
;
881 /* Cover functions to the vfprintf callbacks.
883 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
884 a larger "remote target subsystem"] with a well defined interface, then
885 one would think that the subsystem would provide these. However, until
886 one is allowed to create such a subsystem (with its own source tree
887 independent of any particular user), such a critter can't exist. Thus
888 these functions are here for the time being. */
891 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
896 p
->vprintf_filtered (p
, fmt
, ap
);
901 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
906 p
->evprintf_filtered (p
, fmt
, ap
);