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
52 #include <sys/types.h>
54 #include "gdb/callback.h"
55 #include "targ-vals.h"
57 #include "libiberty.h"
67 /* ??? sim_cb_printf should be cb_printf, but until the callback support is
68 broken out of the simulator directory, these are here to not require
70 void sim_cb_printf
PARAMS ((host_callback
*, const char *, ...));
71 void sim_cb_eprintf
PARAMS ((host_callback
*, const char *, ...));
73 extern CB_TARGET_DEFS_MAP cb_init_syscall_map
[];
74 extern CB_TARGET_DEFS_MAP cb_init_errno_map
[];
75 extern CB_TARGET_DEFS_MAP cb_init_open_map
[];
77 extern int system
PARAMS ((const char *));
79 static int os_init
PARAMS ((host_callback
*));
80 static int os_shutdown
PARAMS ((host_callback
*));
81 static int os_unlink
PARAMS ((host_callback
*, const char *));
82 static long os_time
PARAMS ((host_callback
*, long *));
83 static int os_system
PARAMS ((host_callback
*, const char *));
84 static int os_rename
PARAMS ((host_callback
*, const char *, const char *));
85 static int os_write_stdout
PARAMS ((host_callback
*, const char *, int));
86 static void os_flush_stdout
PARAMS ((host_callback
*));
87 static int os_write_stderr
PARAMS ((host_callback
*, const char *, int));
88 static void os_flush_stderr
PARAMS ((host_callback
*));
89 static int os_write
PARAMS ((host_callback
*, int, const char *, int));
90 static int os_read_stdin
PARAMS ((host_callback
*, char *, int));
91 static int os_read
PARAMS ((host_callback
*, int, char *, int));
92 static int os_open
PARAMS ((host_callback
*, const char *, int));
93 static int os_lseek
PARAMS ((host_callback
*, int, long, int));
94 static int os_isatty
PARAMS ((host_callback
*, int));
95 static int os_get_errno
PARAMS ((host_callback
*));
96 static int os_close
PARAMS ((host_callback
*, int));
97 static void os_vprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
98 static void os_evprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
99 static void os_error
PARAMS ((host_callback
*, const char *, ...));
100 static int fdmap
PARAMS ((host_callback
*, int));
101 static int fdbad
PARAMS ((host_callback
*, int));
102 static int wrap
PARAMS ((host_callback
*, int));
104 /* Set the callback copy of errno from what we see now. */
111 p
->last_errno
= errno
;
115 /* Make sure the FD provided is ok. If not, return non-zero
123 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| p
->fd_buddy
[fd
] < 0)
125 p
->last_errno
= EINVAL
;
147 result
= fdbad (p
, fd
);
150 /* If this file descripter has one or more buddies (originals /
151 duplicates from a dup), just remove it from the circular list. */
152 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
155 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
160 int other
= p
->ispipe
[fd
];
165 /* Closing the read side. */
171 /* Closing the write side. */
176 /* If there was data in the buffer, make a last "now empty"
177 call, then deallocate data. */
178 if (p
->pipe_buffer
[writer
].buffer
!= NULL
)
180 (*p
->pipe_empty
) (p
, reader
, writer
);
181 free (p
->pipe_buffer
[writer
].buffer
);
182 p
->pipe_buffer
[writer
].buffer
= NULL
;
185 /* Clear pipe data for this side. */
186 p
->pipe_buffer
[fd
].size
= 0;
189 /* If this was the first close, mark the other side as the
190 only remaining side. */
191 if (fd
!= abs (other
))
192 p
->ispipe
[abs (other
)] = -other
;
193 p
->fd_buddy
[fd
] = -1;
197 result
= wrap (p
, close (fdmap (p
, fd
)));
199 p
->fd_buddy
[fd
] = -1;
205 /* taken from gdb/util.c:notice_quit() - should be in a library */
208 #if defined(__GO32__) || defined (_MSC_VER)
213 #if defined(__GO32__)
229 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
233 #if defined (_MSC_VER)
234 /* NB - this will not compile! */
235 int k
= win32pollquit();
244 #define os_poll_quit 0
245 #endif /* defined(__GO32__) || defined(_MSC_VER) */
251 return cb_host_to_target_errno (p
, p
->last_errno
);
262 result
= fdbad (p
, fd
);
265 result
= wrap (p
, isatty (fdmap (p
, fd
)));
271 os_lseek (p
, fd
, off
, way
)
279 result
= fdbad (p
, fd
);
282 result
= lseek (fdmap (p
, fd
), off
, way
);
287 os_open (p
, name
, flags
)
293 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
295 if (p
->fd_buddy
[i
] < 0)
297 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
300 p
->last_errno
= errno
;
308 p
->last_errno
= EMFILE
;
313 os_read (p
, fd
, buf
, len
)
321 result
= fdbad (p
, fd
);
326 int writer
= p
->ispipe
[fd
];
328 /* Can't read from the write-end. */
331 p
->last_errno
= EBADF
;
335 /* Nothing to read if nothing is written. */
336 if (p
->pipe_buffer
[writer
].size
== 0)
339 /* Truncate read request size to buffer size minus what's already
341 if (len
> p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
)
342 len
= p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
;
344 memcpy (buf
, p
->pipe_buffer
[writer
].buffer
+ p
->pipe_buffer
[fd
].size
,
347 /* Account for what we just read. */
348 p
->pipe_buffer
[fd
].size
+= len
;
350 /* If we've read everything, empty and deallocate the buffer and
351 signal buffer-empty to client. (This isn't expected to be a
352 hot path in the simulator, so we don't hold on to the buffer.) */
353 if (p
->pipe_buffer
[fd
].size
== p
->pipe_buffer
[writer
].size
)
355 free (p
->pipe_buffer
[writer
].buffer
);
356 p
->pipe_buffer
[writer
].buffer
= NULL
;
357 p
->pipe_buffer
[fd
].size
= 0;
358 p
->pipe_buffer
[writer
].size
= 0;
359 (*p
->pipe_empty
) (p
, fd
, writer
);
365 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
370 os_read_stdin (p
, buf
, len
)
375 return wrap (p
, read (0, buf
, len
));
379 os_write (p
, fd
, buf
, len
)
388 result
= fdbad (p
, fd
);
394 int reader
= -p
->ispipe
[fd
];
396 /* Can't write to the read-end. */
399 p
->last_errno
= EBADF
;
403 /* Can't write to pipe with closed read end.
404 FIXME: We should send a SIGPIPE. */
407 p
->last_errno
= EPIPE
;
411 /* As a sanity-check, we bail out it the buffered contents is much
412 larger than the size of the buffer on the host. We don't want
413 to run out of memory in the simulator due to a target program
414 bug if we can help it. Unfortunately, regarding the value that
415 reaches the simulated program, it's no use returning *less*
416 than the requested amount, because cb_syscall loops calling
417 this function until the whole amount is done. */
418 if (p
->pipe_buffer
[fd
].size
+ len
> 10 * PIPE_BUF
)
420 p
->last_errno
= EFBIG
;
424 p
->pipe_buffer
[fd
].buffer
425 = xrealloc (p
->pipe_buffer
[fd
].buffer
, p
->pipe_buffer
[fd
].size
+ len
);
426 memcpy (p
->pipe_buffer
[fd
].buffer
+ p
->pipe_buffer
[fd
].size
,
428 p
->pipe_buffer
[fd
].size
+= len
;
430 (*p
->pipe_nonempty
) (p
, reader
, fd
);
434 real_fd
= fdmap (p
, fd
);
438 result
= wrap (p
, write (real_fd
, buf
, len
));
441 result
= p
->write_stdout (p
, buf
, len
);
444 result
= p
->write_stderr (p
, buf
, len
);
451 os_write_stdout (p
, buf
, len
)
452 host_callback
*p ATTRIBUTE_UNUSED
;
456 return fwrite (buf
, 1, len
, stdout
);
461 host_callback
*p ATTRIBUTE_UNUSED
;
467 os_write_stderr (p
, buf
, len
)
468 host_callback
*p ATTRIBUTE_UNUSED
;
472 return fwrite (buf
, 1, len
, stderr
);
477 host_callback
*p ATTRIBUTE_UNUSED
;
483 os_rename (p
, f1
, f2
)
488 return wrap (p
, rename (f1
, f2
));
497 return wrap (p
, system (s
));
505 return wrap (p
, time (t
));
514 return wrap (p
, unlink (f1
));
518 os_stat (p
, file
, buf
)
523 /* ??? There is an issue of when to translate to the target layout.
524 One could do that inside this function, or one could have the
525 caller do it. It's more flexible to let the caller do it, though
526 I'm not sure the flexibility will ever be useful. */
527 return wrap (p
, stat (file
, buf
));
531 os_fstat (p
, fd
, buf
)
541 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
542 time_t t
= (*p
->time
) (p
, NULL
);
545 /* We have to fake the struct stat contents, since the pipe is
546 made up in the simulator. */
547 memset (buf
, 0, sizeof (*buf
));
549 #ifdef HAVE_STRUCT_STAT_ST_MODE
550 buf
->st_mode
= S_IFIFO
;
553 /* If more accurate tracking than current-time is needed (for
554 example, on GNU/Linux we get accurate numbers), the p->time
555 callback (which may be something other than os_time) should
556 happen for each read and write, and we'd need to keep track of
557 atime, ctime and mtime. */
558 #ifdef HAVE_STRUCT_STAT_ST_ATIME
561 #ifdef HAVE_STRUCT_STAT_ST_CTIME
564 #ifdef HAVE_STRUCT_STAT_ST_MTIME
570 /* ??? There is an issue of when to translate to the target layout.
571 One could do that inside this function, or one could have the
572 caller do it. It's more flexible to let the caller do it, though
573 I'm not sure the flexibility will ever be useful. */
574 return wrap (p
, fstat (fdmap (p
, fd
), buf
));
578 os_lstat (p
, file
, buf
)
583 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
585 return wrap (p
, lstat (file
, buf
));
587 return wrap (p
, stat (file
, buf
));
592 os_ftruncate (p
, fd
, len
)
599 result
= fdbad (p
, fd
);
602 p
->last_errno
= EINVAL
;
607 #ifdef HAVE_FTRUNCATE
608 result
= wrap (p
, ftruncate (fdmap (p
, fd
), len
));
610 p
->last_errno
= EINVAL
;
617 os_truncate (p
, file
, len
)
623 return wrap (p
, truncate (file
, len
));
625 p
->last_errno
= EINVAL
;
637 /* We deliberately don't use fd 0. It's probably stdin anyway. */
638 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
642 if (p
->fd_buddy
[i
] < 0)
643 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
644 if (p
->fd_buddy
[j
] < 0)
646 /* Found two free fd:s. Set stat to allocated and mark
655 /* Poison the FD map to make bugs apparent. */
662 p
->last_errno
= EMFILE
;
666 /* Stub functions for pipe support. They should always be overridden in
667 targets using the pipe support, but that's up to the target. */
669 /* Called when the simulator says that the pipe at (reader, writer) is
670 now empty (so the writer should leave its waiting state). */
673 os_pipe_empty (p
, reader
, writer
)
680 /* Called when the simulator says the pipe at (reader, writer) is now
681 non-empty (so the writer should wait). */
684 os_pipe_nonempty (p
, reader
, writer
)
696 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
700 /* Zero out all pipe state. Don't call callbacks for non-empty
701 pipes; the target program has likely terminated at this point
702 or we're called at initialization time. */
704 p
->pipe_buffer
[i
].size
= 0;
705 p
->pipe_buffer
[i
].buffer
= NULL
;
707 next
= p
->fd_buddy
[i
];
713 if (j
== MAX_CALLBACK_FDS
)
715 next
= p
->fd_buddy
[j
];
717 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
739 for (i
= 0; i
< 3; i
++)
742 p
->fd_buddy
[i
] = i
- 1;
744 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
745 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
747 p
->syscall_map
= cb_init_syscall_map
;
748 p
->errno_map
= cb_init_errno_map
;
749 p
->open_map
= cb_init_open_map
;
758 #ifdef ANSI_PROTOTYPES
759 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
761 os_printf_filtered (p
, va_alist
)
767 #ifdef ANSI_PROTOTYPES
768 va_start (args
, format
);
773 format
= va_arg (args
, char *);
776 vfprintf (stdout
, format
, args
);
782 #ifdef ANSI_PROTOTYPES
783 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
785 os_vprintf_filtered (p
, format
, args
)
791 vprintf (format
, args
);
796 #ifdef ANSI_PROTOTYPES
797 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
799 os_evprintf_filtered (p
, format
, args
)
805 vfprintf (stderr
, format
, args
);
810 #ifdef ANSI_PROTOTYPES
811 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
813 os_error (p
, va_alist
)
819 #ifdef ANSI_PROTOTYPES
820 va_start (args
, format
);
825 format
= va_arg (args
, char *);
828 vfprintf (stderr
, format
, args
);
829 fprintf (stderr
, "\n");
835 host_callback default_callback
=
870 os_printf_filtered
, /* deprecated */
873 os_evprintf_filtered
,
879 { -1, }, /* fd_buddy */
881 { { 0, 0 }, }, /* pipe_buffer */
889 /* Defaults expected to be overridden at initialization, where needed. */
890 BFD_ENDIAN_UNKNOWN
, /* target_endian */
891 4, /* target_sizeof_int */
896 /* Read in a file describing the target's system call values.
897 E.g. maybe someone will want to use something other than newlib.
898 This assumes that the basic system call recognition and value passing/
899 returning is supported. So maybe some coding/recompilation will be
900 necessary, but not as much.
902 If an error occurs, the existing mapping is not changed. */
905 cb_read_target_syscall_maps (cb
, file
)
909 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
910 const char *stat_map
;
913 if ((f
= fopen (file
, "r")) == NULL
)
916 /* ... read in and parse file ... */
919 return CB_RC_NO_MEM
; /* FIXME:wip */
921 /* Free storage allocated for any existing maps. */
923 free (cb
->syscall_map
);
925 free (cb
->errno_map
);
929 free (cb
->signal_map
);
931 free ((PTR
) cb
->stat_map
);
933 cb
->syscall_map
= syscall_map
;
934 cb
->errno_map
= errno_map
;
935 cb
->open_map
= open_map
;
936 cb
->signal_map
= signal_map
;
937 cb
->stat_map
= stat_map
;
942 /* Translate the target's version of a syscall number to the host's.
943 This isn't actually the host's version, rather a canonical form.
944 ??? Perhaps this should be renamed to ..._canon_syscall. */
947 cb_target_to_host_syscall (cb
, target_val
)
951 CB_TARGET_DEFS_MAP
*m
;
953 for (m
= &cb
->syscall_map
[0]; m
->target_val
!= -1; ++m
)
954 if (m
->target_val
== target_val
)
960 /* FIXME: sort tables if large.
961 Alternatively, an obvious improvement for errno conversion is
962 to machine generate a function with a large switch(). */
964 /* Translate the host's version of errno to the target's. */
967 cb_host_to_target_errno (cb
, host_val
)
971 CB_TARGET_DEFS_MAP
*m
;
973 for (m
= &cb
->errno_map
[0]; m
->host_val
; ++m
)
974 if (m
->host_val
== host_val
)
975 return m
->target_val
;
977 /* ??? Which error to return in this case is up for grabs.
978 Note that some missing values may have standard alternatives.
979 For now return 0 and require caller to deal with it. */
983 /* Given a set of target bitmasks for the open system call,
984 return the host equivalent.
985 Mapping open flag values is best done by looping so there's no need
986 to machine generate this function. */
989 cb_target_to_host_open (cb
, target_val
)
994 CB_TARGET_DEFS_MAP
*m
;
996 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
998 switch (m
->target_val
)
1000 /* O_RDONLY can be (and usually is) 0 which needs to be treated
1002 case TARGET_O_RDONLY
:
1003 case TARGET_O_WRONLY
:
1004 case TARGET_O_RDWR
:
1005 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
1007 host_val
|= m
->host_val
;
1008 /* Handle the host/target differentiating between binary and
1009 text mode. Only one case is of importance */
1010 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
1011 host_val
|= O_BINARY
;
1015 if ((m
->target_val
& target_val
) == m
->target_val
)
1016 host_val
|= m
->host_val
;
1024 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
1028 cb_store_target_endian (cb
, p
, size
, val
)
1032 long val
; /* ??? must be as big as target word size */
1034 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
1053 /* Translate a host's stat struct into a target's.
1054 If HS is NULL, just compute the length of the buffer required,
1057 The result is the size of the target's stat struct,
1058 or zero if an error occurred during the translation. */
1061 cb_host_to_target_stat (cb
, hs
, ts
)
1063 const struct stat
*hs
;
1066 const char *m
= cb
->stat_map
;
1075 char *q
= strchr (m
, ',');
1078 /* FIXME: Use sscanf? */
1081 /* FIXME: print error message */
1084 size
= atoi (q
+ 1);
1087 /* FIXME: print error message */
1095 /* Defined here to avoid emacs indigestion on a lone "else". */
1098 else if (strncmp (m, #FLD, q - m) == 0) \
1099 cb_store_target_endian (cb, p, size, hs->FLD)
1101 #ifdef HAVE_STRUCT_STAT_ST_DEV
1104 #ifdef HAVE_STRUCT_STAT_ST_INO
1107 #ifdef HAVE_STRUCT_STAT_ST_MODE
1110 #ifdef HAVE_STRUCT_STAT_ST_NLINK
1113 #ifdef HAVE_STRUCT_STAT_ST_UID
1116 #ifdef HAVE_STRUCT_STAT_ST_GID
1119 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1122 #ifdef HAVE_STRUCT_STAT_ST_SIZE
1125 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1128 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1131 #ifdef HAVE_STRUCT_STAT_ST_ATIME
1134 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1137 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1143 /* Unsupported field, store 0. */
1144 cb_store_target_endian (cb
, p
, size
, 0);
1148 m
= strchr (q
, ':');
1153 return p
- (char *) ts
;
1156 /* Cover functions to the vfprintf callbacks.
1158 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
1159 a larger "remote target subsystem"] with a well defined interface, then
1160 one would think that the subsystem would provide these. However, until
1161 one is allowed to create such a subsystem (with its own source tree
1162 independent of any particular user), such a critter can't exist. Thus
1163 these functions are here for the time being. */
1166 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
1171 p
->vprintf_filtered (p
, fmt
, ap
);
1176 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
1181 p
->evprintf_filtered (p
, fmt
, ap
);