-/* Host callback routines for GDB.
- Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
- Contributed by Cygnus Support.
+/* Remote target callback routines.
+ Copyright 1995, 1996, 1997, 2000, 2002, 2003, 2004
+ Free Software Foundation, Inc.
+ Contributed by Cygnus Solutions.
This file is part of GDB.
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+ along with GAS; see the file COPYING. If not, write to the Free Software
+ Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* This file provides a standard way for targets to talk to the host OS
level. */
#ifdef HAVE_CONFIG_H
-#include "config.h"
+#include "cconfig.h"
#endif
#include "ansidecl.h"
#ifdef ANSI_PROTOTYPES
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#else
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+#endif
#include <errno.h>
#include <fcntl.h>
#include <time.h>
-#include "callback.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "gdb/callback.h"
#include "targ-vals.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
+/* ??? sim_cb_printf should be cb_printf, but until the callback support is
+ broken out of the simulator directory, these are here to not require
+ sim-utils.h. */
+void sim_cb_printf PARAMS ((host_callback *, const char *, ...));
+void sim_cb_eprintf PARAMS ((host_callback *, const char *, ...));
+
+extern CB_TARGET_DEFS_MAP cb_init_syscall_map[];
+extern CB_TARGET_DEFS_MAP cb_init_errno_map[];
+extern CB_TARGET_DEFS_MAP cb_init_open_map[];
+
extern int system PARAMS ((const char *));
static int os_init PARAMS ((host_callback *));
host_callback *p;
int fd;
{
- if (fd < 0 || fd > MAX_CALLBACK_FDS || !p->fdopen[fd])
+ if (fd < 0 || fd > MAX_CALLBACK_FDS || p->fd_buddy[fd] < 0)
{
p->last_errno = EINVAL;
return -1;
int fd;
{
int result;
+ int i, next;
result = fdbad (p, fd);
if (result)
return result;
- result = wrap (p, close (fdmap (p, fd)));
- if(result == 0 && !p->alwaysopen[fd])
- p->fdopen[fd] = 0;
+ /* If this file descripter has one or more buddies (originals /
+ duplicates from a dup), just remove it from the circular list. */
+ for (i = fd; (next = p->fd_buddy[i]) != fd; )
+ i = next;
+ if (fd != i)
+ p->fd_buddy[i] = p->fd_buddy[fd];
+ else
+ result = wrap (p, close (fdmap (p, fd)));
+ p->fd_buddy[fd] = -1;
return result;
}
-/* taken from gdb/util.c - should be in a library */
+/* taken from gdb/util.c:notice_quit() - should be in a library */
-#if defined(__GO32__) || defined (_WIN32)
+#if defined(__GO32__) || defined (_MSC_VER)
static int
os_poll_quit (p)
host_callback *p;
{
-#ifndef _MSC_VER
+#if defined(__GO32__)
+ int kbhit ();
+ int getkey ();
if (kbhit ())
{
int k = getkey ();
sim_cb_eprintf (p, "CTRL-A to quit, CTRL-B to quit harder\n");
}
}
-#else /* !_MSC_VER */
+#endif
+#if defined (_MSC_VER)
/* NB - this will not compile! */
int k = win32pollquit();
if (k == 1)
return 1;
else if (k == 2)
return 1;
-#endif /* !_MSC_VER */
+#endif
return 0;
}
#else
#define os_poll_quit 0
-#endif /* defined(__GO32__) || defined(_WIN32) */
+#endif /* defined(__GO32__) || defined(_MSC_VER) */
static int
os_get_errno (p)
host_callback *p;
{
- return host_to_target_errno (p->last_errno);
+ return cb_host_to_target_errno (p, p->last_errno);
}
int i;
for (i = 0; i < MAX_CALLBACK_FDS; i++)
{
- if (!p->fdopen[i])
+ if (p->fd_buddy[i] < 0)
{
- int f = open (name, target_to_host_open (flags), 0644);
+ int f = open (name, cb_target_to_host_open (p, flags), 0644);
if (f < 0)
{
p->last_errno = errno;
return f;
}
- p->fdopen[i] = 1;
+ p->fd_buddy[i] = i;
p->fdmap[i] = f;
return i;
}
int len;
{
int result;
+ int real_fd;
result = fdbad (p, fd);
if (result)
return result;
- result = wrap (p, write (fdmap (p, fd), buf, len));
+ real_fd = fdmap (p, fd);
+ switch (real_fd)
+ {
+ default:
+ result = wrap (p, write (real_fd, buf, len));
+ break;
+ case 1:
+ result = p->write_stdout (p, buf, len);
+ break;
+ case 2:
+ result = p->write_stderr (p, buf, len);
+ break;
+ }
return result;
}
static int
os_write_stdout (p, buf, len)
- host_callback *p;
+ host_callback *p ATTRIBUTE_UNUSED;
const char *buf;
int len;
{
- return fwrite(buf, 1, len, stdout);
+ return fwrite (buf, 1, len, stdout);
}
static void
os_flush_stdout (p)
- host_callback *p;
+ host_callback *p ATTRIBUTE_UNUSED;
{
fflush (stdout);
}
static int
os_write_stderr (p, buf, len)
- host_callback *p;
+ host_callback *p ATTRIBUTE_UNUSED;
const char *buf;
int len;
{
- return fwrite(buf, 1, len, stderr);
+ return fwrite (buf, 1, len, stderr);
}
static void
os_flush_stderr (p)
- host_callback *p;
+ host_callback *p ATTRIBUTE_UNUSED;
{
fflush (stderr);
}
return wrap (p, unlink (f1));
}
+static int
+os_stat (p, file, buf)
+ host_callback *p;
+ const char *file;
+ struct stat *buf;
+{
+ /* ??? There is an issue of when to translate to the target layout.
+ One could do that inside this function, or one could have the
+ caller do it. It's more flexible to let the caller do it, though
+ I'm not sure the flexibility will ever be useful. */
+ return wrap (p, stat (file, buf));
+}
+
+static int
+os_fstat (p, fd, buf)
+ host_callback *p;
+ int fd;
+ struct stat *buf;
+{
+ if (fdbad (p, fd))
+ return -1;
+ /* ??? There is an issue of when to translate to the target layout.
+ One could do that inside this function, or one could have the
+ caller do it. It's more flexible to let the caller do it, though
+ I'm not sure the flexibility will ever be useful. */
+ return wrap (p, fstat (fdmap (p, fd), buf));
+}
+
+static int
+os_lstat (p, file, buf)
+ host_callback *p;
+ const char *file;
+ struct stat *buf;
+{
+ /* NOTE: hpn/2004-12-12: Same issue here as with os_fstat. */
+ return wrap (p, lstat (file, buf));
+}
+
+static int
+os_ftruncate (p, fd, len)
+ host_callback *p;
+ int fd;
+ long len;
+{
+ int result;
+
+ result = fdbad (p, fd);
+ if (result)
+ return result;
+ result = wrap (p, ftruncate (fdmap (p, fd), len));
+ return result;
+}
+
+static int
+os_truncate (p, file, len)
+ host_callback *p;
+ const char *file;
+ long len;
+{
+ return wrap (p, truncate (file, len));
+}
static int
os_shutdown (p)
host_callback *p;
{
- int i;
+ int i, next, j;
for (i = 0; i < MAX_CALLBACK_FDS; i++)
{
- if (p->fdopen[i] && !p->alwaysopen[i]) {
+ int do_close = 1;
+
+ next = p->fd_buddy[i];
+ if (next < 0)
+ continue;
+ do
+ {
+ j = next;
+ if (j == MAX_CALLBACK_FDS)
+ do_close = 0;
+ next = p->fd_buddy[j];
+ p->fd_buddy[j] = -1;
+ /* At the initial call of os_init, we got -1, 0, 0, 0, ... */
+ if (next < 0)
+ {
+ p->fd_buddy[i] = -1;
+ do_close = 0;
+ break;
+ }
+ }
+ while (j != i);
+ if (do_close)
close (p->fdmap[i]);
- p->fdopen[i] = 0;
- }
}
return 1;
}
static int
-os_init(p)
+os_init (p)
host_callback *p;
{
int i;
+
os_shutdown (p);
- for (i= 0; i < 3; i++)
+ for (i = 0; i < 3; i++)
{
p->fdmap[i] = i;
- p->fdopen[i] = 1;
- p->alwaysopen[i] = 1;
+ p->fd_buddy[i] = i - 1;
}
+ p->fd_buddy[0] = MAX_CALLBACK_FDS;
+ p->fd_buddy[MAX_CALLBACK_FDS] = 2;
+
+ p->syscall_map = cb_init_syscall_map;
+ p->errno_map = cb_init_errno_map;
+ p->open_map = cb_init_open_map;
+
return 1;
}
-/* DEPRECIATED */
+/* DEPRECATED */
/* VARARGS */
static void
#ifdef ANSI_PROTOTYPES
-os_printf_filtered (host_callback *p, const char *format, ...)
+os_printf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, ...)
#else
os_printf_filtered (p, va_alist)
host_callback *p;
/* VARARGS */
static void
#ifdef ANSI_PROTOTYPES
-os_vprintf_filtered (host_callback *p, const char *format, va_list args)
+os_vprintf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, va_list args)
#else
os_vprintf_filtered (p, format, args)
host_callback *p;
/* VARARGS */
static void
#ifdef ANSI_PROTOTYPES
-os_evprintf_filtered (host_callback *p, const char *format, va_list args)
+os_evprintf_filtered (host_callback *p ATTRIBUTE_UNUSED, const char *format, va_list args)
#else
os_evprintf_filtered (p, format, args)
host_callback *p;
/* VARARGS */
static void
#ifdef ANSI_PROTOTYPES
-os_error (host_callback *p, const char *format, ...)
+os_error (host_callback *p ATTRIBUTE_UNUSED, const char *format, ...)
#else
os_error (p, va_alist)
host_callback *p;
os_write_stderr,
os_flush_stderr,
+ os_stat,
+ os_fstat,
+ os_lstat,
+
+ os_ftruncate,
+ os_truncate,
+
os_poll_quit,
os_shutdown,
os_init,
- os_printf_filtered, /* depreciated */
+ os_printf_filtered, /* deprecated */
os_vprintf_filtered,
os_evprintf_filtered,
0, /* last errno */
{ 0, }, /* fdmap */
- { 0, }, /* fdopen */
- { 0, }, /* alwaysopen */
-
+ { -1, }, /* fd_buddy */
+
+ 0, /* syscall_map */
+ 0, /* errno_map */
+ 0, /* open_map */
+ 0, /* signal_map */
+ 0, /* stat_map */
+
HOST_CALLBACK_MAGIC,
};
\f
-/* FIXME: Need to add hooks so target can tweak as necessary. */
+/* Read in a file describing the target's system call values.
+ E.g. maybe someone will want to use something other than newlib.
+ This assumes that the basic system call recognition and value passing/
+ returning is supported. So maybe some coding/recompilation will be
+ necessary, but not as much.
+
+ If an error occurs, the existing mapping is not changed. */
+
+CB_RC
+cb_read_target_syscall_maps (cb, file)
+ host_callback *cb;
+ const char *file;
+{
+ CB_TARGET_DEFS_MAP *syscall_map, *errno_map, *open_map, *signal_map;
+ const char *stat_map;
+ FILE *f;
+
+ if ((f = fopen (file, "r")) == NULL)
+ return CB_RC_ACCESS;
+
+ /* ... read in and parse file ... */
+
+ fclose (f);
+ return CB_RC_NO_MEM; /* FIXME:wip */
+
+ /* Free storage allocated for any existing maps. */
+ if (cb->syscall_map)
+ free (cb->syscall_map);
+ if (cb->errno_map)
+ free (cb->errno_map);
+ if (cb->open_map)
+ free (cb->open_map);
+ if (cb->signal_map)
+ free (cb->signal_map);
+ if (cb->stat_map)
+ free ((PTR) cb->stat_map);
+
+ cb->syscall_map = syscall_map;
+ cb->errno_map = errno_map;
+ cb->open_map = open_map;
+ cb->signal_map = signal_map;
+ cb->stat_map = stat_map;
+
+ return CB_RC_OK;
+}
+
+/* Translate the target's version of a syscall number to the host's.
+ This isn't actually the host's version, rather a canonical form.
+ ??? Perhaps this should be renamed to ..._canon_syscall. */
+
+int
+cb_target_to_host_syscall (cb, target_val)
+ host_callback *cb;
+ int target_val;
+{
+ CB_TARGET_DEFS_MAP *m;
-/* FIXME: struct stat conversion is missing. */
+ for (m = &cb->syscall_map[0]; m->target_val != -1; ++m)
+ if (m->target_val == target_val)
+ return m->host_val;
+
+ return -1;
+}
/* FIXME: sort tables if large.
Alternatively, an obvious improvement for errno conversion is
to machine generate a function with a large switch(). */
+/* Translate the host's version of errno to the target's. */
+
int
-host_to_target_errno (host_val)
+cb_host_to_target_errno (cb, host_val)
+ host_callback *cb;
int host_val;
{
- target_defs_map *m;
+ CB_TARGET_DEFS_MAP *m;
- for (m = &errno_map[0]; m->host_val; ++m)
+ for (m = &cb->errno_map[0]; m->host_val; ++m)
if (m->host_val == host_val)
return m->target_val;
to machine generate this function. */
int
-target_to_host_open (target_val)
+cb_target_to_host_open (cb, target_val)
+ host_callback *cb;
int target_val;
{
int host_val = 0;
- target_defs_map *m;
+ CB_TARGET_DEFS_MAP *m;
- for (m = &open_map[0]; m->host_val != -1; ++m)
+ for (m = &cb->open_map[0]; m->host_val != -1; ++m)
{
switch (m->target_val)
{
return host_val;
}
+
+/* Utility for cb_host_to_target_stat to store values in the target's
+ stat struct. */
+
+static void
+store (p, size, val, big_p)
+ char *p;
+ int size;
+ long val; /* ??? must be as big as target word size */
+ int big_p;
+{
+ if (big_p)
+ {
+ p += size;
+ while (size-- > 0)
+ {
+ *--p = val;
+ val >>= 8;
+ }
+ }
+ else
+ {
+ while (size-- > 0)
+ {
+ *p++ = val;
+ val >>= 8;
+ }
+ }
+}
+
+/* Translate a host's stat struct into a target's.
+ If HS is NULL, just compute the length of the buffer required,
+ TS is ignored.
+
+ The result is the size of the target's stat struct,
+ or zero if an error occurred during the translation. */
+
+int
+cb_host_to_target_stat (cb, hs, ts)
+ host_callback *cb;
+ const struct stat *hs;
+ PTR ts;
+{
+ const char *m = cb->stat_map;
+ char *p;
+ int big_p = 0;
+
+ if (hs == NULL)
+ ts = NULL;
+ p = ts;
+
+ while (m)
+ {
+ char *q = strchr (m, ',');
+ int size;
+
+ /* FIXME: Use sscanf? */
+ if (q == NULL)
+ {
+ /* FIXME: print error message */
+ return 0;
+ }
+ size = atoi (q + 1);
+ if (size == 0)
+ {
+ /* FIXME: print error message */
+ return 0;
+ }
+
+ if (hs != NULL)
+ {
+ if (0)
+ ;
+ /* Defined here to avoid emacs indigestion on a lone "else". */
+#undef ST_x
+#define ST_x(FLD) \
+ else if (strncmp (m, #FLD, q - m) == 0) \
+ store (p, size, hs->FLD, big_p)
+
+#ifdef HAVE_STRUCT_STAT_ST_DEV
+ ST_x (st_dev);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_INO
+ ST_x (st_ino);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_MODE
+ ST_x (st_mode);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_NLINK
+ ST_x (st_nlink);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_UID
+ ST_x (st_uid);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_GID
+ ST_x (st_gid);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_RDEV
+ ST_x (st_rdev);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_SIZE
+ ST_x (st_size);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
+ ST_x (st_blksize);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
+ ST_x (st_blocks);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_ATIME
+ ST_x (st_atime);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_MTIME
+ ST_x (st_mtime);
+#endif
+#ifdef HAVE_STRUCT_STAT_ST_CTIME
+ ST_x (st_ctime);
+#endif
+#undef ST_x
+ /* FIXME:wip */
+ else
+ store (p, size, 0, big_p); /* unsupported field, store 0 */
+ }
+
+ p += size;
+ m = strchr (q, ':');
+ if (m)
+ ++m;
+ }
+
+ return p - (char *) ts;
+}
\f
-/* Cover functions to the vfprintf callbacks. */
+/* Cover functions to the vfprintf callbacks.
+
+ ??? If one thinks of the callbacks as a subsystem onto itself [or part of
+ a larger "remote target subsystem"] with a well defined interface, then
+ one would think that the subsystem would provide these. However, until
+ one is allowed to create such a subsystem (with its own source tree
+ independent of any particular user), such a critter can't exist. Thus
+ these functions are here for the time being. */
void
sim_cb_printf (host_callback *p, const char *fmt, ...)