OSDN Git Service

5ae1d00c30aaccb838a47849a2df25eee3196769
[pf3gnuchains/pf3gnuchains3x.git] / include / gdb / callback.h
1 /* Remote target system call callback support.
2    Copyright 1997, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>.  */
19
20 /* This interface isn't intended to be specific to any particular kind
21    of remote (hardware, simulator, whatever).  As such, support for it
22    (e.g. sim/common/callback.c) should *not* live in the simulator source
23    tree, nor should it live in the gdb source tree.  */
24
25 /* There are various ways to handle system calls:
26
27    1) Have a simulator intercept the appropriate trap instruction and
28    directly perform the system call on behalf of the target program.
29    This is the typical way of handling system calls for embedded targets.
30    [Handling system calls for embedded targets isn't that much of an
31    oxymoron as running compiler testsuites make use of the capability.]
32
33    This method of system call handling is done when STATE_ENVIRONMENT
34    is ENVIRONMENT_USER.
35
36    2) Have a simulator emulate the hardware as much as possible.
37    If the program running on the real hardware communicates with some sort
38    of target manager, one would want to be able to run this program on the
39    simulator as well.
40
41    This method of system call handling is done when STATE_ENVIRONMENT
42    is ENVIRONMENT_OPERATING.
43 */
44
45 #ifndef CALLBACK_H
46 #define CALLBACK_H
47
48 /* ??? The reason why we check for va_start here should be documented.  */
49
50 #ifndef va_start
51 #include <ansidecl.h>
52 #include <stdarg.h>
53 #endif
54 /* Needed for enum bfd_endian.  */
55 #include "bfd.h"
56 \f
57 /* Mapping of host/target values.  */
58 /* ??? For debugging purposes, one might want to add a string of the
59    name of the symbol.  */
60
61 typedef struct {
62   int host_val;
63   int target_val;
64 } CB_TARGET_DEFS_MAP;
65
66 #define MAX_CALLBACK_FDS 10
67
68 /* Forward decl for stat/fstat.  */
69 struct stat;
70
71 typedef struct host_callback_struct host_callback;
72
73 struct host_callback_struct 
74 {
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   int (*stat) PARAMS ((host_callback *, const char *, struct stat *));
92   int (*fstat) PARAMS ((host_callback *, int, struct stat *));
93   int (*lstat) PARAMS ((host_callback *, const char *, struct stat *));
94   int (*ftruncate) PARAMS ((host_callback *, int, long));
95   int (*truncate) PARAMS ((host_callback *, const char *, long));
96   int (*pipe) PARAMS ((host_callback *, int *));
97
98   /* Called by the framework when a read call has emptied a pipe buffer.  */
99   void (*pipe_empty) PARAMS ((host_callback *, int read_fd, int write_fd));
100
101   /* Called by the framework when a write call makes a pipe buffer
102      non-empty.  */
103   void (*pipe_nonempty) PARAMS ((host_callback *, int read_fd, int write_fd));
104
105   /* When present, call to the client to give it the oportunity to
106      poll any io devices for a request to quit (indicated by a nonzero
107      return value). */
108   int (*poll_quit) PARAMS ((host_callback *));
109
110   /* Used when the target has gone away, so we can close open
111      handles and free memory etc etc.  */
112   int (*shutdown) PARAMS ((host_callback *));
113   int (*init)     PARAMS ((host_callback *));
114
115   /* depreciated, use vprintf_filtered - Talk to the user on a console.  */
116   void (*printf_filtered) PARAMS ((host_callback *, const char *, ...));
117
118   /* Talk to the user on a console.  */
119   void (*vprintf_filtered) PARAMS ((host_callback *, const char *, va_list));
120
121   /* Same as vprintf_filtered but to stderr.  */
122   void (*evprintf_filtered) PARAMS ((host_callback *, const char *, va_list));
123
124   /* Print an error message and "exit".
125      In the case of gdb "exiting" means doing a longjmp back to the main
126      command loop.  */
127   void (*error) PARAMS ((host_callback *, const char *, ...));
128
129   int last_errno;               /* host format */
130
131   int fdmap[MAX_CALLBACK_FDS];
132   /* fd_buddy is used to contruct circular lists of target fds that point to
133      the same host fd.  A uniquely mapped fd points to itself; for a closed
134      one, fd_buddy has the value -1.  The host file descriptors for stdin /
135      stdout / stderr are never closed by the simulators, so they are put
136      in a special fd_buddy circular list which also has MAX_CALLBACK_FDS
137      as a member.  */
138   /* ??? We don't have a callback entry for dup, although it is trival to
139      implement now.  */
140   short fd_buddy[MAX_CALLBACK_FDS+1];
141
142   /* 0 = none, >0 = reader (index of writer),
143      <0 = writer (negative index of reader).
144      If abs (ispipe[N]) == N, then N is an end of a pipe whose other
145      end is closed.  */
146   short ispipe[MAX_CALLBACK_FDS];
147
148   /* A writer stores the buffer at its index.  Consecutive writes
149      realloc the buffer and add to the size.  The reader indicates the
150      read part in its .size, until it has consumed it all, at which
151      point it deallocates the buffer and zeroes out both sizes.  */
152   struct pipe_write_buffer
153   {
154     int size;
155     char *buffer;
156   } pipe_buffer[MAX_CALLBACK_FDS];
157
158   /* System call numbers.  */
159   CB_TARGET_DEFS_MAP *syscall_map;
160   /* Errno values.  */
161   CB_TARGET_DEFS_MAP *errno_map;
162   /* Flags to the open system call.  */
163   CB_TARGET_DEFS_MAP *open_map;
164   /* Signal numbers.  */
165   CB_TARGET_DEFS_MAP *signal_map;
166   /* Layout of `stat' struct.
167      The format is a series of "name,length" pairs separated by colons.
168      Empty space is indicated with a `name' of "space".
169      All padding must be explicitly mentioned.
170      Lengths are in bytes.  If this needs to be extended to bits,
171      use "name.bits".
172      Example: "st_dev,4:st_ino,4:st_mode,4:..."  */
173   const char *stat_map;
174
175   enum bfd_endian target_endian;
176
177   /* Size of an "int" on the target (for syscalls whose ABI uses "int").
178      This must include padding, and only padding-at-higher-address is
179      supported.  For example, a 64-bit target with 32-bit int:s which
180      are padded to 64 bits when in an array, should supposedly set this
181      to 8.  The default is 4 which matches ILP32 targets and 64-bit
182      targets with 32-bit ints and no padding.  */
183   int target_sizeof_int;
184
185   /* Marker for those wanting to do sanity checks.
186      This should remain the last member of this struct to help catch
187      miscompilation errors. */
188 #define HOST_CALLBACK_MAGIC 4705 /* teds constant */
189   int magic;
190 };
191
192 extern host_callback default_callback;
193 \f
194 /* Canonical versions of system call numbers.
195    It's not intended to willy-nilly throw every system call ever heard
196    of in here.  Only include those that have an important use.
197    ??? One can certainly start a discussion over the ones that are currently
198    here, but that will always be true.  */
199
200 /* These are used by the ANSI C support of libc.  */
201 #define CB_SYS_exit     1
202 #define CB_SYS_open     2
203 #define CB_SYS_close    3
204 #define CB_SYS_read     4
205 #define CB_SYS_write    5
206 #define CB_SYS_lseek    6
207 #define CB_SYS_unlink   7
208 #define CB_SYS_getpid   8
209 #define CB_SYS_kill     9
210 #define CB_SYS_fstat    10
211 /*#define CB_SYS_sbrk   11 - not currently a system call, but reserved.  */
212
213 /* ARGV support.  */
214 #define CB_SYS_argvlen  12
215 #define CB_SYS_argv     13
216
217 /* These are extras added for one reason or another.  */
218 #define CB_SYS_chdir    14
219 #define CB_SYS_stat     15
220 #define CB_SYS_chmod    16
221 #define CB_SYS_utime    17
222 #define CB_SYS_time     18
223
224 /* More standard syscalls.  */
225 #define CB_SYS_lstat    19
226 #define CB_SYS_rename   20
227 #define CB_SYS_truncate 21
228 #define CB_SYS_ftruncate 22
229 #define CB_SYS_pipe     23
230 \f
231 /* Struct use to pass and return information necessary to perform a
232    system call.  */
233 /* FIXME: Need to consider target word size.  */
234
235 typedef struct cb_syscall {
236   /* The target's value of what system call to perform.  */
237   int func;
238   /* The arguments to the syscall.  */
239   long arg1, arg2, arg3, arg4;
240
241   /* The result.  */
242   long result;
243   /* Some system calls have two results.  */
244   long result2;
245   /* The target's errno value, or 0 if success.
246      This is converted to the target's value with host_to_target_errno.  */
247   int errcode;
248
249   /* Working space to be used by memory read/write callbacks.  */
250   PTR p1;
251   PTR p2;
252   long x1,x2;
253
254   /* Callbacks for reading/writing memory (e.g. for read/write syscalls).
255      ??? long or unsigned long might be better to use for the `count'
256      argument here.  We mimic sim_{read,write} for now.  Be careful to
257      test any changes with -Wall -Werror, mixed signed comparisons
258      will get you.  */
259   int (*read_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/,
260                            unsigned long /*taddr*/, char * /*buf*/,
261                            int /*bytes*/));
262   int (*write_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/,
263                             unsigned long /*taddr*/, const char * /*buf*/,
264                             int /*bytes*/));
265
266   /* For sanity checking, should be last entry.  */
267   int magic;
268 } CB_SYSCALL;
269
270 /* Magic number sanity checker.  */
271 #define CB_SYSCALL_MAGIC 0x12344321
272
273 /* Macro to initialize CB_SYSCALL.  Called first, before filling in
274    any fields.  */
275 #define CB_SYSCALL_INIT(sc) \
276 do { \
277   memset ((sc), 0, sizeof (*(sc))); \
278   (sc)->magic = CB_SYSCALL_MAGIC; \
279 } while (0)
280 \f
281 /* Return codes for various interface routines.  */
282
283 typedef enum {
284   CB_RC_OK = 0,
285   /* generic error */
286   CB_RC_ERR,
287   /* either file not found or no read access */
288   CB_RC_ACCESS,
289   CB_RC_NO_MEM
290 } CB_RC;
291
292 /* Read in target values for system call numbers, errno values, signals.  */
293 CB_RC cb_read_target_syscall_maps PARAMS ((host_callback *, const char *));
294
295 /* Translate target to host syscall function numbers.  */
296 int cb_target_to_host_syscall PARAMS ((host_callback *, int));
297
298 /* Translate host to target errno value.  */
299 int cb_host_to_target_errno PARAMS ((host_callback *, int));
300
301 /* Translate target to host open flags.  */
302 int cb_target_to_host_open PARAMS ((host_callback *, int));
303
304 /* Translate target signal number to host.  */
305 int cb_target_to_host_signal PARAMS ((host_callback *, int));
306
307 /* Translate host signal number to target.  */
308 int cb_host_to_target_signal PARAMS ((host_callback *, int));
309
310 /* Translate host stat struct to target.
311    If stat struct ptr is NULL, just compute target stat struct size.
312    Result is size of target stat struct or 0 if error.  */
313 int cb_host_to_target_stat PARAMS ((host_callback *, const struct stat *, PTR));
314
315 /* Translate a value to target endian.  */
316 void cb_store_target_endian PARAMS ((host_callback *, char *, int, long));
317
318 /* Tests for special fds.  */
319 int cb_is_stdin PARAMS ((host_callback *, int));
320 int cb_is_stdout PARAMS ((host_callback *, int));
321 int cb_is_stderr PARAMS ((host_callback *, int));
322
323 /* Perform a system call.  */
324 CB_RC cb_syscall PARAMS ((host_callback *, CB_SYSCALL *));
325
326 #endif