1 /* Remote target callback routines.
2 Copyright 1995, 1996, 1997, 2000, 2002, 2003, 2004, 2007
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
48 #include <sys/types.h>
50 #include "gdb/callback.h"
51 #include "targ-vals.h"
53 #include "libiberty.h"
63 /* ??? sim_cb_printf should be cb_printf, but until the callback support is
64 broken out of the simulator directory, these are here to not require
66 void sim_cb_printf
PARAMS ((host_callback
*, const char *, ...));
67 void sim_cb_eprintf
PARAMS ((host_callback
*, const char *, ...));
69 extern CB_TARGET_DEFS_MAP cb_init_syscall_map
[];
70 extern CB_TARGET_DEFS_MAP cb_init_errno_map
[];
71 extern CB_TARGET_DEFS_MAP cb_init_open_map
[];
73 extern int system
PARAMS ((const char *));
75 static int os_init
PARAMS ((host_callback
*));
76 static int os_shutdown
PARAMS ((host_callback
*));
77 static int os_unlink
PARAMS ((host_callback
*, const char *));
78 static long os_time
PARAMS ((host_callback
*, long *));
79 static int os_system
PARAMS ((host_callback
*, const char *));
80 static int os_rename
PARAMS ((host_callback
*, const char *, const char *));
81 static int os_write_stdout
PARAMS ((host_callback
*, const char *, int));
82 static void os_flush_stdout
PARAMS ((host_callback
*));
83 static int os_write_stderr
PARAMS ((host_callback
*, const char *, int));
84 static void os_flush_stderr
PARAMS ((host_callback
*));
85 static int os_write
PARAMS ((host_callback
*, int, const char *, int));
86 static int os_read_stdin
PARAMS ((host_callback
*, char *, int));
87 static int os_read
PARAMS ((host_callback
*, int, char *, int));
88 static int os_open
PARAMS ((host_callback
*, const char *, int));
89 static int os_lseek
PARAMS ((host_callback
*, int, long, int));
90 static int os_isatty
PARAMS ((host_callback
*, int));
91 static int os_get_errno
PARAMS ((host_callback
*));
92 static int os_close
PARAMS ((host_callback
*, int));
93 static void os_vprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
94 static void os_evprintf_filtered
PARAMS ((host_callback
*, const char *, va_list));
95 static void os_error
PARAMS ((host_callback
*, const char *, ...));
96 static int fdmap
PARAMS ((host_callback
*, int));
97 static int fdbad
PARAMS ((host_callback
*, int));
98 static int wrap
PARAMS ((host_callback
*, int));
100 /* Set the callback copy of errno from what we see now. */
107 p
->last_errno
= errno
;
111 /* Make sure the FD provided is ok. If not, return non-zero
119 if (fd
< 0 || fd
> MAX_CALLBACK_FDS
|| p
->fd_buddy
[fd
] < 0)
121 p
->last_errno
= EINVAL
;
143 result
= fdbad (p
, fd
);
146 /* If this file descripter has one or more buddies (originals /
147 duplicates from a dup), just remove it from the circular list. */
148 for (i
= fd
; (next
= p
->fd_buddy
[i
]) != fd
; )
151 p
->fd_buddy
[i
] = p
->fd_buddy
[fd
];
156 int other
= p
->ispipe
[fd
];
161 /* Closing the read side. */
167 /* Closing the write side. */
172 /* If there was data in the buffer, make a last "now empty"
173 call, then deallocate data. */
174 if (p
->pipe_buffer
[writer
].buffer
!= NULL
)
176 (*p
->pipe_empty
) (p
, reader
, writer
);
177 free (p
->pipe_buffer
[writer
].buffer
);
178 p
->pipe_buffer
[writer
].buffer
= NULL
;
181 /* Clear pipe data for this side. */
182 p
->pipe_buffer
[fd
].size
= 0;
185 /* If this was the first close, mark the other side as the
186 only remaining side. */
187 if (fd
!= abs (other
))
188 p
->ispipe
[abs (other
)] = -other
;
189 p
->fd_buddy
[fd
] = -1;
193 result
= wrap (p
, close (fdmap (p
, fd
)));
195 p
->fd_buddy
[fd
] = -1;
201 /* taken from gdb/util.c:notice_quit() - should be in a library */
204 #if defined(__GO32__) || defined (_MSC_VER)
209 #if defined(__GO32__)
225 sim_cb_eprintf (p
, "CTRL-A to quit, CTRL-B to quit harder\n");
229 #if defined (_MSC_VER)
230 /* NB - this will not compile! */
231 int k
= win32pollquit();
240 #define os_poll_quit 0
241 #endif /* defined(__GO32__) || defined(_MSC_VER) */
247 return cb_host_to_target_errno (p
, p
->last_errno
);
258 result
= fdbad (p
, fd
);
261 result
= wrap (p
, isatty (fdmap (p
, fd
)));
267 os_lseek (p
, fd
, off
, way
)
275 result
= fdbad (p
, fd
);
278 result
= lseek (fdmap (p
, fd
), off
, way
);
283 os_open (p
, name
, flags
)
289 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
291 if (p
->fd_buddy
[i
] < 0)
293 int f
= open (name
, cb_target_to_host_open (p
, flags
), 0644);
296 p
->last_errno
= errno
;
304 p
->last_errno
= EMFILE
;
309 os_read (p
, fd
, buf
, len
)
317 result
= fdbad (p
, fd
);
322 int writer
= p
->ispipe
[fd
];
324 /* Can't read from the write-end. */
327 p
->last_errno
= EBADF
;
331 /* Nothing to read if nothing is written. */
332 if (p
->pipe_buffer
[writer
].size
== 0)
335 /* Truncate read request size to buffer size minus what's already
337 if (len
> p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
)
338 len
= p
->pipe_buffer
[writer
].size
- p
->pipe_buffer
[fd
].size
;
340 memcpy (buf
, p
->pipe_buffer
[writer
].buffer
+ p
->pipe_buffer
[fd
].size
,
343 /* Account for what we just read. */
344 p
->pipe_buffer
[fd
].size
+= len
;
346 /* If we've read everything, empty and deallocate the buffer and
347 signal buffer-empty to client. (This isn't expected to be a
348 hot path in the simulator, so we don't hold on to the buffer.) */
349 if (p
->pipe_buffer
[fd
].size
== p
->pipe_buffer
[writer
].size
)
351 free (p
->pipe_buffer
[writer
].buffer
);
352 p
->pipe_buffer
[writer
].buffer
= NULL
;
353 p
->pipe_buffer
[fd
].size
= 0;
354 p
->pipe_buffer
[writer
].size
= 0;
355 (*p
->pipe_empty
) (p
, fd
, writer
);
361 result
= wrap (p
, read (fdmap (p
, fd
), buf
, len
));
366 os_read_stdin (p
, buf
, len
)
371 return wrap (p
, read (0, buf
, len
));
375 os_write (p
, fd
, buf
, len
)
384 result
= fdbad (p
, fd
);
390 int reader
= -p
->ispipe
[fd
];
392 /* Can't write to the read-end. */
395 p
->last_errno
= EBADF
;
399 /* Can't write to pipe with closed read end.
400 FIXME: We should send a SIGPIPE. */
403 p
->last_errno
= EPIPE
;
407 /* As a sanity-check, we bail out it the buffered contents is much
408 larger than the size of the buffer on the host. We don't want
409 to run out of memory in the simulator due to a target program
410 bug if we can help it. Unfortunately, regarding the value that
411 reaches the simulated program, it's no use returning *less*
412 than the requested amount, because cb_syscall loops calling
413 this function until the whole amount is done. */
414 if (p
->pipe_buffer
[fd
].size
+ len
> 10 * PIPE_BUF
)
416 p
->last_errno
= EFBIG
;
420 p
->pipe_buffer
[fd
].buffer
421 = xrealloc (p
->pipe_buffer
[fd
].buffer
, p
->pipe_buffer
[fd
].size
+ len
);
422 memcpy (p
->pipe_buffer
[fd
].buffer
+ p
->pipe_buffer
[fd
].size
,
424 p
->pipe_buffer
[fd
].size
+= len
;
426 (*p
->pipe_nonempty
) (p
, reader
, fd
);
430 real_fd
= fdmap (p
, fd
);
434 result
= wrap (p
, write (real_fd
, buf
, len
));
437 result
= p
->write_stdout (p
, buf
, len
);
440 result
= p
->write_stderr (p
, buf
, len
);
447 os_write_stdout (p
, buf
, len
)
448 host_callback
*p ATTRIBUTE_UNUSED
;
452 return fwrite (buf
, 1, len
, stdout
);
457 host_callback
*p ATTRIBUTE_UNUSED
;
463 os_write_stderr (p
, buf
, len
)
464 host_callback
*p ATTRIBUTE_UNUSED
;
468 return fwrite (buf
, 1, len
, stderr
);
473 host_callback
*p ATTRIBUTE_UNUSED
;
479 os_rename (p
, f1
, f2
)
484 return wrap (p
, rename (f1
, f2
));
493 return wrap (p
, system (s
));
501 return wrap (p
, time (t
));
510 return wrap (p
, unlink (f1
));
514 os_stat (p
, file
, buf
)
519 /* ??? There is an issue of when to translate to the target layout.
520 One could do that inside this function, or one could have the
521 caller do it. It's more flexible to let the caller do it, though
522 I'm not sure the flexibility will ever be useful. */
523 return wrap (p
, stat (file
, buf
));
527 os_fstat (p
, fd
, buf
)
537 #if defined (HAVE_STRUCT_STAT_ST_ATIME) || defined (HAVE_STRUCT_STAT_ST_CTIME) || defined (HAVE_STRUCT_STAT_ST_MTIME)
538 time_t t
= (*p
->time
) (p
, NULL
);
541 /* We have to fake the struct stat contents, since the pipe is
542 made up in the simulator. */
543 memset (buf
, 0, sizeof (*buf
));
545 #ifdef HAVE_STRUCT_STAT_ST_MODE
546 buf
->st_mode
= S_IFIFO
;
549 /* If more accurate tracking than current-time is needed (for
550 example, on GNU/Linux we get accurate numbers), the p->time
551 callback (which may be something other than os_time) should
552 happen for each read and write, and we'd need to keep track of
553 atime, ctime and mtime. */
554 #ifdef HAVE_STRUCT_STAT_ST_ATIME
557 #ifdef HAVE_STRUCT_STAT_ST_CTIME
560 #ifdef HAVE_STRUCT_STAT_ST_MTIME
566 /* ??? There is an issue of when to translate to the target layout.
567 One could do that inside this function, or one could have the
568 caller do it. It's more flexible to let the caller do it, though
569 I'm not sure the flexibility will ever be useful. */
570 return wrap (p
, fstat (fdmap (p
, fd
), buf
));
574 os_lstat (p
, file
, buf
)
579 /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
581 return wrap (p
, lstat (file
, buf
));
583 return wrap (p
, stat (file
, buf
));
588 os_ftruncate (p
, fd
, len
)
595 result
= fdbad (p
, fd
);
598 p
->last_errno
= EINVAL
;
603 #ifdef HAVE_FTRUNCATE
604 result
= wrap (p
, ftruncate (fdmap (p
, fd
), len
));
606 p
->last_errno
= EINVAL
;
613 os_truncate (p
, file
, len
)
619 return wrap (p
, truncate (file
, len
));
621 p
->last_errno
= EINVAL
;
633 /* We deliberately don't use fd 0. It's probably stdin anyway. */
634 for (i
= 1; i
< MAX_CALLBACK_FDS
; i
++)
638 if (p
->fd_buddy
[i
] < 0)
639 for (j
= i
+ 1; j
< MAX_CALLBACK_FDS
; j
++)
640 if (p
->fd_buddy
[j
] < 0)
642 /* Found two free fd:s. Set stat to allocated and mark
651 /* Poison the FD map to make bugs apparent. */
658 p
->last_errno
= EMFILE
;
662 /* Stub functions for pipe support. They should always be overridden in
663 targets using the pipe support, but that's up to the target. */
665 /* Called when the simulator says that the pipe at (reader, writer) is
666 now empty (so the writer should leave its waiting state). */
669 os_pipe_empty (p
, reader
, writer
)
676 /* Called when the simulator says the pipe at (reader, writer) is now
677 non-empty (so the writer should wait). */
680 os_pipe_nonempty (p
, reader
, writer
)
692 for (i
= 0; i
< MAX_CALLBACK_FDS
; i
++)
696 /* Zero out all pipe state. Don't call callbacks for non-empty
697 pipes; the target program has likely terminated at this point
698 or we're called at initialization time. */
700 p
->pipe_buffer
[i
].size
= 0;
701 p
->pipe_buffer
[i
].buffer
= NULL
;
703 next
= p
->fd_buddy
[i
];
709 if (j
== MAX_CALLBACK_FDS
)
711 next
= p
->fd_buddy
[j
];
713 /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
735 for (i
= 0; i
< 3; i
++)
738 p
->fd_buddy
[i
] = i
- 1;
740 p
->fd_buddy
[0] = MAX_CALLBACK_FDS
;
741 p
->fd_buddy
[MAX_CALLBACK_FDS
] = 2;
743 p
->syscall_map
= cb_init_syscall_map
;
744 p
->errno_map
= cb_init_errno_map
;
745 p
->open_map
= cb_init_open_map
;
754 os_printf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
757 va_start (args
, format
);
759 vfprintf (stdout
, format
, args
);
765 os_vprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
767 vprintf (format
, args
);
772 os_evprintf_filtered (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, va_list args
)
774 vfprintf (stderr
, format
, args
);
779 os_error (host_callback
*p ATTRIBUTE_UNUSED
, const char *format
, ...)
782 va_start (args
, format
);
784 vfprintf (stderr
, format
, args
);
785 fprintf (stderr
, "\n");
791 host_callback default_callback
=
826 os_printf_filtered
, /* deprecated */
829 os_evprintf_filtered
,
835 { -1, }, /* fd_buddy */
837 { { 0, 0 }, }, /* pipe_buffer */
845 /* Defaults expected to be overridden at initialization, where needed. */
846 BFD_ENDIAN_UNKNOWN
, /* target_endian */
847 4, /* target_sizeof_int */
852 /* Read in a file describing the target's system call values.
853 E.g. maybe someone will want to use something other than newlib.
854 This assumes that the basic system call recognition and value passing/
855 returning is supported. So maybe some coding/recompilation will be
856 necessary, but not as much.
858 If an error occurs, the existing mapping is not changed. */
861 cb_read_target_syscall_maps (cb
, file
)
865 CB_TARGET_DEFS_MAP
*syscall_map
, *errno_map
, *open_map
, *signal_map
;
866 const char *stat_map
;
869 if ((f
= fopen (file
, "r")) == NULL
)
872 /* ... read in and parse file ... */
875 return CB_RC_NO_MEM
; /* FIXME:wip */
877 /* Free storage allocated for any existing maps. */
879 free (cb
->syscall_map
);
881 free (cb
->errno_map
);
885 free (cb
->signal_map
);
887 free ((PTR
) cb
->stat_map
);
889 cb
->syscall_map
= syscall_map
;
890 cb
->errno_map
= errno_map
;
891 cb
->open_map
= open_map
;
892 cb
->signal_map
= signal_map
;
893 cb
->stat_map
= stat_map
;
898 /* Translate the target's version of a syscall number to the host's.
899 This isn't actually the host's version, rather a canonical form.
900 ??? Perhaps this should be renamed to ..._canon_syscall. */
903 cb_target_to_host_syscall (cb
, target_val
)
907 CB_TARGET_DEFS_MAP
*m
;
909 for (m
= &cb
->syscall_map
[0]; m
->target_val
!= -1; ++m
)
910 if (m
->target_val
== target_val
)
916 /* FIXME: sort tables if large.
917 Alternatively, an obvious improvement for errno conversion is
918 to machine generate a function with a large switch(). */
920 /* Translate the host's version of errno to the target's. */
923 cb_host_to_target_errno (cb
, host_val
)
927 CB_TARGET_DEFS_MAP
*m
;
929 for (m
= &cb
->errno_map
[0]; m
->host_val
; ++m
)
930 if (m
->host_val
== host_val
)
931 return m
->target_val
;
933 /* ??? Which error to return in this case is up for grabs.
934 Note that some missing values may have standard alternatives.
935 For now return 0 and require caller to deal with it. */
939 /* Given a set of target bitmasks for the open system call,
940 return the host equivalent.
941 Mapping open flag values is best done by looping so there's no need
942 to machine generate this function. */
945 cb_target_to_host_open (cb
, target_val
)
950 CB_TARGET_DEFS_MAP
*m
;
952 for (m
= &cb
->open_map
[0]; m
->host_val
!= -1; ++m
)
954 switch (m
->target_val
)
956 /* O_RDONLY can be (and usually is) 0 which needs to be treated
958 case TARGET_O_RDONLY
:
959 case TARGET_O_WRONLY
:
961 if ((target_val
& (TARGET_O_RDONLY
| TARGET_O_WRONLY
| TARGET_O_RDWR
))
963 host_val
|= m
->host_val
;
964 /* Handle the host/target differentiating between binary and
965 text mode. Only one case is of importance */
966 #if ! defined (TARGET_O_BINARY) && defined (O_BINARY)
967 host_val
|= O_BINARY
;
971 if ((m
->target_val
& target_val
) == m
->target_val
)
972 host_val
|= m
->host_val
;
980 /* Utility for e.g. cb_host_to_target_stat to store values in the target's
984 cb_store_target_endian (cb
, p
, size
, val
)
988 long val
; /* ??? must be as big as target word size */
990 if (cb
->target_endian
== BFD_ENDIAN_BIG
)
1009 /* Translate a host's stat struct into a target's.
1010 If HS is NULL, just compute the length of the buffer required,
1013 The result is the size of the target's stat struct,
1014 or zero if an error occurred during the translation. */
1017 cb_host_to_target_stat (cb
, hs
, ts
)
1019 const struct stat
*hs
;
1022 const char *m
= cb
->stat_map
;
1031 char *q
= strchr (m
, ',');
1034 /* FIXME: Use sscanf? */
1037 /* FIXME: print error message */
1040 size
= atoi (q
+ 1);
1043 /* FIXME: print error message */
1051 /* Defined here to avoid emacs indigestion on a lone "else". */
1054 else if (strncmp (m, #FLD, q - m) == 0) \
1055 cb_store_target_endian (cb, p, size, hs->FLD)
1057 #ifdef HAVE_STRUCT_STAT_ST_DEV
1060 #ifdef HAVE_STRUCT_STAT_ST_INO
1063 #ifdef HAVE_STRUCT_STAT_ST_MODE
1066 #ifdef HAVE_STRUCT_STAT_ST_NLINK
1069 #ifdef HAVE_STRUCT_STAT_ST_UID
1072 #ifdef HAVE_STRUCT_STAT_ST_GID
1075 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1078 #ifdef HAVE_STRUCT_STAT_ST_SIZE
1081 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1084 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1087 #ifdef HAVE_STRUCT_STAT_ST_ATIME
1090 #ifdef HAVE_STRUCT_STAT_ST_MTIME
1093 #ifdef HAVE_STRUCT_STAT_ST_CTIME
1099 /* Unsupported field, store 0. */
1100 cb_store_target_endian (cb
, p
, size
, 0);
1104 m
= strchr (q
, ':');
1109 return p
- (char *) ts
;
1112 /* Cover functions to the vfprintf callbacks.
1114 ??? If one thinks of the callbacks as a subsystem onto itself [or part of
1115 a larger "remote target subsystem"] with a well defined interface, then
1116 one would think that the subsystem would provide these. However, until
1117 one is allowed to create such a subsystem (with its own source tree
1118 independent of any particular user), such a critter can't exist. Thus
1119 these functions are here for the time being. */
1122 sim_cb_printf (host_callback
*p
, const char *fmt
, ...)
1127 p
->vprintf_filtered (p
, fmt
, ap
);
1132 sim_cb_eprintf (host_callback
*p
, const char *fmt
, ...)
1137 p
->evprintf_filtered (p
, fmt
, ap
);