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, ... */
474 for (i
= 0; i
< 3; i
++)
477 p
->fd_buddy
[i
] = i
- 1;
479 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
480 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
482 p
->syscall_map
= cb_init_syscall_map
;
483 p
->errno_map
= cb_init_errno_map
;
484 p
->open_map
= cb_init_open_map
;
493 #ifdef ANSI_PROTOTYPES
494 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
496 os_printf_filtered (p
, va_alist
)
502 #ifdef ANSI_PROTOTYPES
503 va_start (args
, format
);
508 format
= va_arg (args
, char *);
511 vfprintf (stdout
, format
, args
);
517 #ifdef ANSI_PROTOTYPES
518 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
520 os_vprintf_filtered (p
, format
, args
)
526 vprintf (format
, args
);
531 #ifdef ANSI_PROTOTYPES
532 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
534 os_evprintf_filtered (p
, format
, args
)
540 vfprintf (stderr
, format
, args
);
545 #ifdef ANSI_PROTOTYPES
546 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
548 os_error (p
, va_alist
)
554 #ifdef ANSI_PROTOTYPES
555 va_start (args
, format
);
560 format
= va_arg (args
, char *);
563 vfprintf (stderr
, format
, args
);
564 fprintf (stderr
, "\n");
570 host_callback default_callback
=
600 os_printf_filtered
, /* deprecated */
603 os_evprintf_filtered
,
609 { -1, }, /* fd_buddy */
620 /* Read in a file describing the target's system call values.
621 E.g. maybe someone will want to use something other than newlib.
622 This assumes that the basic system call recognition and value passing/
623 returning is supported. So maybe some coding/recompilation will be
624 necessary, but not as much.
626 If an error occurs, the existing mapping is not changed. */
629 cb_read_target_syscall_maps (cb
, file
)
633 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
634 const char *stat_map
;
637 if ((f
= fopen (file
, "r")) == NULL
)
640 /* ... read in and parse file ... */
643 return CB_RC_NO_MEM
; /* FIXME:wip */
645 /* Free storage allocated for any existing maps. */
647 free (cb
->syscall_map
);
649 free (cb
->errno_map
);
653 free (cb
->signal_map
);
655 free ((PTR
) cb
->stat_map
);
657 cb
->syscall_map
= syscall_map
;
658 cb
->errno_map
= errno_map
;
659 cb
->open_map
= open_map
;
660 cb
->signal_map
= signal_map
;
661 cb
->stat_map
= stat_map
;
666 /* Translate the target's version of a syscall number to the host's.
667 This isn't actually the host's version, rather a canonical form.
668 ??? Perhaps this should be renamed to ..._canon_syscall. */
671 cb_target_to_host_syscall (cb
, target_val
)
675 CB_TARGET_DEFS_MAP
*m
;
677 for (m
= &cb
->syscall_map
[0]; m
->target_val
!= -1; ++m
)
678 if (m
->target_val
== target_val
)
684 /* FIXME: sort tables if large.
685 Alternatively, an obvious improvement for errno conversion is
686 to machine generate a function with a large switch(). */
688 /* Translate the host's version of errno to the target's. */
691 cb_host_to_target_errno (cb
, host_val
)
695 CB_TARGET_DEFS_MAP
*m
;
697 for (m
= &cb
->errno_map
[0]; m
->host_val
; ++m
)
698 if (m
->host_val
== host_val
)
699 return m
->target_val
;
701 /* ??? Which error to return in this case is up for grabs.
702 Note that some missing values may have standard alternatives.
703 For now return 0 and require caller to deal with it. */
707 /* Given a set of target bitmasks for the open system call,
708 return the host equivalent.
709 Mapping open flag values is best done by looping so there's no need
710 to machine generate this function. */
713 cb_target_to_host_open (cb
, target_val
)
718 CB_TARGET_DEFS_MAP
*m
;
720 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
722 switch (m
->target_val
)
724 /* O_RDONLY can be (and usually is) 0 which needs to be treated
726 case TARGET_O_RDONLY
:
727 case TARGET_O_WRONLY
:
729 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
731 host_val
|= m
->host_val
;
732 /* Handle the host/target differentiating between binary and
733 text mode. Only one case is of importance */
734 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
735 host_val
|= O_BINARY
;
739 if ((m
->target_val
& target_val
) == m
->target_val
)
740 host_val
|= m
->host_val
;
748 /* Utility for cb_host_to_target_stat to store values in the target's
752 store (p
, size
, val
, big_p
)
755 long val
; /* ??? must be as big as target word size */
777 /* Translate a host's stat struct into a target's.
778 If HS is NULL, just compute the length of the buffer required,
781 The result is the size of the target's stat struct,
782 or zero if an error occurred during the translation. */
785 cb_host_to_target_stat (cb
, hs
, ts
)
787 const struct stat
*hs
;
790 const char *m
= cb
->stat_map
;
800 char *q
= strchr (m
, ',');
803 /* FIXME: Use sscanf? */
806 /* FIXME: print error message */
812 /* FIXME: print error message */
818 if (strncmp (m
, "st_dev", q
- m
) == 0)
819 store (p
, size
, hs
->st_dev
, big_p
);
820 else if (strncmp (m
, "st_ino", q
- m
) == 0)
821 store (p
, size
, hs
->st_ino
, big_p
);
824 store (p
, size
, 0, big_p
); /* unsupported field, store 0 */
833 return p
- (char *) ts
;
836 /* Cover functions to the vfprintf callbacks.
838 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
839 a larger "remote target subsystem"] with a well defined interface, then
840 one would think that the subsystem would provide these. However, until
841 one is allowed to create such a subsystem (with its own source tree
842 independent of any particular user), such a critter can't exist. Thus
843 these functions are here for the time being. */
846 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
851 p
->vprintf_filtered (p
, fmt
, ap
);
856 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
861 p
->evprintf_filtered (p
, fmt
, ap
);