1 /* Remote target system call callback support.
2 Copyright 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This interface isn't intended to be specific to any particular kind
22 of remote (hardware, simulator, whatever). As such, support for it
23 (e.g. sim/common/callback.c) should *not* live in the simulator source
24 tree, nor should it live in the gdb source tree. */
26 /* There are various ways to handle system calls:
28 1) Have a simulator intercept the appropriate trap instruction and
29 directly perform the system call on behalf of the target program.
30 This is the typical way of handling system calls for embedded targets.
31 [Handling system calls for embedded targets isn't that much of an
32 oxymoron as running compiler testsuites make use of the capability.]
34 This method of system call handling is done when STATE_ENVIRONMENT
37 2) Have a simulator emulate the hardware as much as possible.
38 If the program running on the real hardware communicates with some sort
39 of target manager, one would want to be able to run this program on the
42 This method of system call handling is done when STATE_ENVIRONMENT
43 is ENVIRONMENT_OPERATING.
49 /* ??? The reason why we check for va_start here should be documented. */
53 #ifdef ANSI_PROTOTYPES
60 /* Mapping of host/target values. */
61 /* ??? For debugging purposes, one might want to add a string of the
62 name of the symbol. */
69 #define MAX_CALLBACK_FDS 10
71 typedef struct host_callback_struct host_callback
;
73 struct host_callback_struct
75 int (*close
) PARAMS ((host_callback
*,int));
76 int (*get_errno
) PARAMS ((host_callback
*));
77 int (*isatty
) PARAMS ((host_callback
*, int));
78 int (*lseek
) PARAMS ((host_callback
*, int, long , int));
79 int (*open
) PARAMS ((host_callback
*, const char*, int mode
));
80 int (*read
) PARAMS ((host_callback
*,int, char *, int));
81 int (*read_stdin
) PARAMS (( host_callback
*, char *, int));
82 int (*rename
) PARAMS ((host_callback
*, const char *, const char *));
83 int (*system
) PARAMS ((host_callback
*, const char *));
84 long (*time
) PARAMS ((host_callback
*, long *));
85 int (*unlink
) PARAMS ((host_callback
*, const char *));
86 int (*write
) PARAMS ((host_callback
*,int, const char *, int));
87 int (*write_stdout
) PARAMS ((host_callback
*, const char *, int));
88 void (*flush_stdout
) PARAMS ((host_callback
*));
89 int (*write_stderr
) PARAMS ((host_callback
*, const char *, int));
90 void (*flush_stderr
) PARAMS ((host_callback
*));
91 /* PTR is not `struct stat' because the target's value is stored. */
92 int (*stat
) PARAMS ((host_callback
*, const char *, PTR
));
93 int (*fstat
) PARAMS ((host_callback
*, int, PTR
));
95 /* When present, call to the client to give it the oportunity to
96 poll any io devices for a request to quit (indicated by a nonzero
98 int (*poll_quit
) PARAMS ((host_callback
*));
100 /* Used when the target has gone away, so we can close open
101 handles and free memory etc etc. */
102 int (*shutdown
) PARAMS ((host_callback
*));
103 int (*init
) PARAMS ((host_callback
*));
105 /* depreciated, use vprintf_filtered - Talk to the user on a console. */
106 void (*printf_filtered
) PARAMS ((host_callback
*, const char *, ...));
108 /* Talk to the user on a console. */
109 void (*vprintf_filtered
) PARAMS ((host_callback
*, const char *, va_list));
111 /* Same as vprintf_filtered but to stderr. */
112 void (*evprintf_filtered
) PARAMS ((host_callback
*, const char *, va_list));
114 /* Print an error message and "exit".
115 In the case of gdb "exiting" means doing a longjmp back to the main
117 void (*error
) PARAMS ((host_callback
*, const char *, ...));
119 int last_errno
; /* host format */
121 int fdmap
[MAX_CALLBACK_FDS
];
122 char fdopen
[MAX_CALLBACK_FDS
];
123 char alwaysopen
[MAX_CALLBACK_FDS
];
125 /* System call numbers. */
126 CB_TARGET_DEFS_MAP
*syscall_map
;
128 CB_TARGET_DEFS_MAP
*errno_map
;
129 /* Flags to the open system call. */
130 CB_TARGET_DEFS_MAP
*open_map
;
131 /* Signal numbers. */
132 CB_TARGET_DEFS_MAP
*signal_map
;
133 /* Layout of `stat' struct.
134 The format is a series of "name,length" pairs separated by colons.
135 Empty space is indicated with a `name' of "space".
136 All padding must be explicitly mentioned.
137 Lengths are in bytes. If this needs to be extended to bits,
139 Example: "st_dev,4:st_ino,4:st_mode,4:..." */
140 const char *stat_map
;
142 /* Marker for those wanting to do sanity checks.
143 This should remain the last member of this struct to help catch
144 miscompilation errors. */
145 #define HOST_CALLBACK_MAGIC 4705 /* teds constant */
149 extern host_callback default_callback
;
151 /* Canonical versions of system call numbers.
152 It's not intended to willy-nilly throw every system call ever heard
153 of in here. Only include those that have an important use. */
155 #define CB_SYS_exit 1
156 #define CB_SYS_open 2
157 #define CB_SYS_close 3
158 #define CB_SYS_read 4
159 #define CB_SYS_write 5
160 #define CB_SYS_lseek 6
161 #define CB_SYS_unlink 7
162 #define CB_SYS_getpid 8
163 #define CB_SYS_kill 9
164 #define CB_SYS_fstat 10
165 /*#define CB_SYS_sbrk 11 - not currently a system call, but reserved. */
168 #define CB_SYS_argvlen 12
169 #define CB_SYS_argv 13
171 /* These are extras added for one reason or another. */
172 #define CB_SYS_chdir 20
173 #define CB_SYS_stat 21
174 #define CB_SYS_chmod 22
175 #define CB_SYS_utime 23
176 #define CB_SYS_time 24
178 /* Struct use to pass and return information necessary to perform a
180 /* FIXME: Need to consider target word size. */
182 typedef struct cb_syscall
{
183 /* The target's value of what system call to perform. */
185 /* The arguments to the syscall. */
186 long arg1
, arg2
, arg3
, arg4
;
190 /* Some system calls have two results. */
192 /* The target's errno value, or 0 if success.
193 This is converted to the target's value with host_to_target_errno. */
196 /* Working space to be used by memory read/write callbacks. */
199 /* Callbacks for reading/writing memory (e.g. for read/write syscalls). */
200 unsigned long (*read_mem
) PARAMS ((host_callback
*, struct cb_syscall
*, unsigned long taddr
, char *buf
, unsigned long bytes
));
201 unsigned long (*write_mem
) PARAMS ((host_callback
*, struct cb_syscall
*, unsigned long taddr
, const char *buf
, unsigned long bytes
));
204 /* Return codes for various interface routines. */
210 /* either file not found or no read access */
215 /* Read in target values for system call numbers, errno values, signals. */
216 CB_RC cb_read_target_syscall_maps
PARAMS ((host_callback
*, const char *));
218 /* Translate target to host syscall function numbers. */
219 int cb_target_to_host_syscall
PARAMS ((host_callback
*, int));
221 /* Translate host to target errno value. */
222 int cb_host_to_target_errno
PARAMS ((host_callback
*, int));
224 /* Translate target to host open flags. */
225 int cb_target_to_host_open
PARAMS ((host_callback
*, int));
227 /* Translate target signal number to host. */
228 int cb_target_to_host_signal
PARAMS ((host_callback
*, int));
230 /* Translate host signal number to target. */
231 int cb_host_to_target_signal
PARAMS ((host_callback
*, int));
233 /* Translate host stat struct to target. */
234 struct stat
; /* forward decl */
235 int cb_host_to_target_stat
PARAMS ((host_callback
*, const struct stat
*,
238 /* Perform a system call. */
239 CB_RC cb_syscall
PARAMS ((host_callback
*, CB_SYSCALL
*));