* callback.h: Include "bfd.h".
[platform/upstream/binutils.git] / include / gdb / callback.h
1 /* Remote target system call callback support.
2    Copyright 1997 Free Software Foundation, Inc.
3    Contributed by Cygnus Solutions.
4
5 This file is part of GDB.
6
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.
11
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.
16
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.  */
20
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.  */
25
26 /* There are various ways to handle system calls:
27
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.]
33
34    This method of system call handling is done when STATE_ENVIRONMENT
35    is ENVIRONMENT_USER.
36
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
40    simulator as well.
41
42    This method of system call handling is done when STATE_ENVIRONMENT
43    is ENVIRONMENT_OPERATING.
44 */
45
46 #ifndef CALLBACK_H
47 #define CALLBACK_H
48
49 /* ??? The reason why we check for va_start here should be documented.  */
50
51 #ifndef va_start
52 #include <ansidecl.h>
53 #ifdef ANSI_PROTOTYPES
54 #include <stdarg.h>
55 #else
56 #include <varargs.h>
57 #endif
58 #endif
59 /* Needed for enum bfd_endian.  */
60 #include "bfd.h"
61 \f
62 /* Mapping of host/target values.  */
63 /* ??? For debugging purposes, one might want to add a string of the
64    name of the symbol.  */
65
66 typedef struct {
67   int host_val;
68   int target_val;
69 } CB_TARGET_DEFS_MAP;
70
71 #define MAX_CALLBACK_FDS 10
72
73 /* Forward decl for stat/fstat.  */
74 struct stat;
75
76 typedef struct host_callback_struct host_callback;
77
78 struct host_callback_struct 
79 {
80   int (*close) PARAMS ((host_callback *,int));
81   int (*get_errno) PARAMS ((host_callback *));
82   int (*isatty) PARAMS ((host_callback *, int));
83   int (*lseek) PARAMS ((host_callback *, int, long , int));
84   int (*open) PARAMS ((host_callback *, const char*, int mode));
85   int (*read) PARAMS ((host_callback *,int,  char *, int));
86   int (*read_stdin) PARAMS (( host_callback *, char *, int));
87   int (*rename) PARAMS ((host_callback *, const char *, const char *));
88   int (*system) PARAMS ((host_callback *, const char *));
89   long (*time) PARAMS ((host_callback *, long *));
90   int (*unlink) PARAMS ((host_callback *, const char *));
91   int (*write) PARAMS ((host_callback *,int, const char *, int));
92   int (*write_stdout) PARAMS ((host_callback *, const char *, int));
93   void (*flush_stdout) PARAMS ((host_callback *));
94   int (*write_stderr) PARAMS ((host_callback *, const char *, int));
95   void (*flush_stderr) PARAMS ((host_callback *));
96   int (*stat) PARAMS ((host_callback *, const char *, struct stat *));
97   int (*fstat) PARAMS ((host_callback *, int, struct stat *));
98   int (*lstat) PARAMS ((host_callback *, const char *, struct stat *));
99   int (*ftruncate) PARAMS ((host_callback *, int, long));
100   int (*truncate) PARAMS ((host_callback *, const char *, long));
101
102   /* When present, call to the client to give it the oportunity to
103      poll any io devices for a request to quit (indicated by a nonzero
104      return value). */
105   int (*poll_quit) PARAMS ((host_callback *));
106
107   /* Used when the target has gone away, so we can close open
108      handles and free memory etc etc.  */
109   int (*shutdown) PARAMS ((host_callback *));
110   int (*init)     PARAMS ((host_callback *));
111
112   /* depreciated, use vprintf_filtered - Talk to the user on a console.  */
113   void (*printf_filtered) PARAMS ((host_callback *, const char *, ...));
114
115   /* Talk to the user on a console.  */
116   void (*vprintf_filtered) PARAMS ((host_callback *, const char *, va_list));
117
118   /* Same as vprintf_filtered but to stderr.  */
119   void (*evprintf_filtered) PARAMS ((host_callback *, const char *, va_list));
120
121   /* Print an error message and "exit".
122      In the case of gdb "exiting" means doing a longjmp back to the main
123      command loop.  */
124   void (*error) PARAMS ((host_callback *, const char *, ...));
125
126   int last_errno;               /* host format */
127
128   int fdmap[MAX_CALLBACK_FDS];
129   /* fd_buddy is used to contruct circular lists of target fds that point to
130      the same host fd.  A uniquely mapped fd points to itself; for a closed
131      one, fd_buddy has the value -1.  The host file descriptors for stdin /
132      stdout / stderr are never closed by the simulators, so they are put
133      in a special fd_buddy circular list which also has MAX_CALLBACK_FDS
134      as a member.  */
135   /* ??? We don't have a callback entry for dup, although it is trival to
136      implement now.  */
137   short fd_buddy[MAX_CALLBACK_FDS+1];
138
139   /* System call numbers.  */
140   CB_TARGET_DEFS_MAP *syscall_map;
141   /* Errno values.  */
142   CB_TARGET_DEFS_MAP *errno_map;
143   /* Flags to the open system call.  */
144   CB_TARGET_DEFS_MAP *open_map;
145   /* Signal numbers.  */
146   CB_TARGET_DEFS_MAP *signal_map;
147   /* Layout of `stat' struct.
148      The format is a series of "name,length" pairs separated by colons.
149      Empty space is indicated with a `name' of "space".
150      All padding must be explicitly mentioned.
151      Lengths are in bytes.  If this needs to be extended to bits,
152      use "name.bits".
153      Example: "st_dev,4:st_ino,4:st_mode,4:..."  */
154   const char *stat_map;
155
156   enum bfd_endian target_endian;
157
158   /* Marker for those wanting to do sanity checks.
159      This should remain the last member of this struct to help catch
160      miscompilation errors. */
161 #define HOST_CALLBACK_MAGIC 4705 /* teds constant */
162   int magic;
163 };
164
165 extern host_callback default_callback;
166 \f
167 /* Canonical versions of system call numbers.
168    It's not intended to willy-nilly throw every system call ever heard
169    of in here.  Only include those that have an important use.
170    ??? One can certainly start a discussion over the ones that are currently
171    here, but that will always be true.  */
172
173 /* These are used by the ANSI C support of libc.  */
174 #define CB_SYS_exit     1
175 #define CB_SYS_open     2
176 #define CB_SYS_close    3
177 #define CB_SYS_read     4
178 #define CB_SYS_write    5
179 #define CB_SYS_lseek    6
180 #define CB_SYS_unlink   7
181 #define CB_SYS_getpid   8
182 #define CB_SYS_kill     9
183 #define CB_SYS_fstat    10
184 /*#define CB_SYS_sbrk   11 - not currently a system call, but reserved.  */
185
186 /* ARGV support.  */
187 #define CB_SYS_argvlen  12
188 #define CB_SYS_argv     13
189
190 /* These are extras added for one reason or another.  */
191 #define CB_SYS_chdir    14
192 #define CB_SYS_stat     15
193 #define CB_SYS_chmod    16
194 #define CB_SYS_utime    17
195 #define CB_SYS_time     18
196
197 /* More standard syscalls.  */
198 #define CB_SYS_lstat    19
199 #define CB_SYS_rename   20
200 #define CB_SYS_truncate 21
201 #define CB_SYS_ftruncate 22
202 \f
203 /* Struct use to pass and return information necessary to perform a
204    system call.  */
205 /* FIXME: Need to consider target word size.  */
206
207 typedef struct cb_syscall {
208   /* The target's value of what system call to perform.  */
209   int func;
210   /* The arguments to the syscall.  */
211   long arg1, arg2, arg3, arg4;
212
213   /* The result.  */
214   long result;
215   /* Some system calls have two results.  */
216   long result2;
217   /* The target's errno value, or 0 if success.
218      This is converted to the target's value with host_to_target_errno.  */
219   int errcode;
220
221   /* Working space to be used by memory read/write callbacks.  */
222   PTR p1;
223   PTR p2;
224   long x1,x2;
225
226   /* Callbacks for reading/writing memory (e.g. for read/write syscalls).
227      ??? long or unsigned long might be better to use for the `count'
228      argument here.  We mimic sim_{read,write} for now.  Be careful to
229      test any changes with -Wall -Werror, mixed signed comparisons
230      will get you.  */
231   int (*read_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/,
232                            unsigned long /*taddr*/, char * /*buf*/,
233                            int /*bytes*/));
234   int (*write_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/,
235                             unsigned long /*taddr*/, const char * /*buf*/,
236                             int /*bytes*/));
237
238   /* For sanity checking, should be last entry.  */
239   int magic;
240 } CB_SYSCALL;
241
242 /* Magic number sanity checker.  */
243 #define CB_SYSCALL_MAGIC 0x12344321
244
245 /* Macro to initialize CB_SYSCALL.  Called first, before filling in
246    any fields.  */
247 #define CB_SYSCALL_INIT(sc) \
248 do { \
249   memset ((sc), 0, sizeof (*(sc))); \
250   (sc)->magic = CB_SYSCALL_MAGIC; \
251 } while (0)
252 \f
253 /* Return codes for various interface routines.  */
254
255 typedef enum {
256   CB_RC_OK = 0,
257   /* generic error */
258   CB_RC_ERR,
259   /* either file not found or no read access */
260   CB_RC_ACCESS,
261   CB_RC_NO_MEM
262 } CB_RC;
263
264 /* Read in target values for system call numbers, errno values, signals.  */
265 CB_RC cb_read_target_syscall_maps PARAMS ((host_callback *, const char *));
266
267 /* Translate target to host syscall function numbers.  */
268 int cb_target_to_host_syscall PARAMS ((host_callback *, int));
269
270 /* Translate host to target errno value.  */
271 int cb_host_to_target_errno PARAMS ((host_callback *, int));
272
273 /* Translate target to host open flags.  */
274 int cb_target_to_host_open PARAMS ((host_callback *, int));
275
276 /* Translate target signal number to host.  */
277 int cb_target_to_host_signal PARAMS ((host_callback *, int));
278
279 /* Translate host signal number to target.  */
280 int cb_host_to_target_signal PARAMS ((host_callback *, int));
281
282 /* Translate host stat struct to target.
283    If stat struct ptr is NULL, just compute target stat struct size.
284    Result is size of target stat struct or 0 if error.  */
285 int cb_host_to_target_stat PARAMS ((host_callback *, const struct stat *, PTR));
286
287 /* Translate a value to target endian.  */
288 void cb_store_target_endian PARAMS ((host_callback *, char *, int, long));
289
290 /* Perform a system call.  */
291 CB_RC cb_syscall PARAMS ((host_callback *, CB_SYSCALL *));
292
293 #endif