bfd/
[deliverable/binutils-gdb.git] / include / gdb / callback.h
CommitLineData
252b5132
RH
1/* Remote target system call callback support.
2 Copyright 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, 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
99b2a78e
HPN
59/* Needed for enum bfd_endian. */
60#include "bfd.h"
252b5132
RH
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
66typedef 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. */
74struct stat;
75
76typedef struct host_callback_struct host_callback;
77
78struct 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 *));
c4b36ce9 98 int (*lstat) PARAMS ((host_callback *, const char *, struct stat *));
cdc9c0bc
JR
99 int (*ftruncate) PARAMS ((host_callback *, int, long));
100 int (*truncate) PARAMS ((host_callback *, const char *, long));
6a7e5cfe
HPN
101 int (*pipe) PARAMS ((host_callback *, int *));
102
103 /* Called by the framework when a read call has emptied a pipe buffer. */
104 void (*pipe_empty) PARAMS ((host_callback *, int read_fd, int write_fd));
105
106 /* Called by the framework when a write call makes a pipe buffer
107 non-empty. */
108 void (*pipe_nonempty) PARAMS ((host_callback *, int read_fd, int write_fd));
252b5132
RH
109
110 /* When present, call to the client to give it the oportunity to
111 poll any io devices for a request to quit (indicated by a nonzero
112 return value). */
113 int (*poll_quit) PARAMS ((host_callback *));
114
115 /* Used when the target has gone away, so we can close open
116 handles and free memory etc etc. */
117 int (*shutdown) PARAMS ((host_callback *));
118 int (*init) PARAMS ((host_callback *));
119
120 /* depreciated, use vprintf_filtered - Talk to the user on a console. */
121 void (*printf_filtered) PARAMS ((host_callback *, const char *, ...));
122
123 /* Talk to the user on a console. */
124 void (*vprintf_filtered) PARAMS ((host_callback *, const char *, va_list));
125
126 /* Same as vprintf_filtered but to stderr. */
127 void (*evprintf_filtered) PARAMS ((host_callback *, const char *, va_list));
128
129 /* Print an error message and "exit".
130 In the case of gdb "exiting" means doing a longjmp back to the main
131 command loop. */
132 void (*error) PARAMS ((host_callback *, const char *, ...));
133
134 int last_errno; /* host format */
135
136 int fdmap[MAX_CALLBACK_FDS];
594ee3a7
JR
137 /* fd_buddy is used to contruct circular lists of target fds that point to
138 the same host fd. A uniquely mapped fd points to itself; for a closed
139 one, fd_buddy has the value -1. The host file descriptors for stdin /
140 stdout / stderr are never closed by the simulators, so they are put
141 in a special fd_buddy circular list which also has MAX_CALLBACK_FDS
142 as a member. */
143 /* ??? We don't have a callback entry for dup, although it is trival to
144 implement now. */
145 short fd_buddy[MAX_CALLBACK_FDS+1];
252b5132 146
6a7e5cfe
HPN
147 /* 0 = none, >0 = reader (index of writer),
148 <0 = writer (negative index of reader).
149 If abs (ispipe[N]) == N, then N is an end of a pipe whose other
150 end is closed. */
151 short ispipe[MAX_CALLBACK_FDS];
152
153 /* A writer stores the buffer at its index. Consecutive writes
154 realloc the buffer and add to the size. The reader indicates the
155 read part in its .size, until it has consumed it all, at which
156 point it deallocates the buffer and zeroes out both sizes. */
157 struct pipe_write_buffer
158 {
159 int size;
160 char *buffer;
161 } pipe_buffer[MAX_CALLBACK_FDS];
162
252b5132
RH
163 /* System call numbers. */
164 CB_TARGET_DEFS_MAP *syscall_map;
165 /* Errno values. */
166 CB_TARGET_DEFS_MAP *errno_map;
167 /* Flags to the open system call. */
168 CB_TARGET_DEFS_MAP *open_map;
169 /* Signal numbers. */
170 CB_TARGET_DEFS_MAP *signal_map;
171 /* Layout of `stat' struct.
172 The format is a series of "name,length" pairs separated by colons.
173 Empty space is indicated with a `name' of "space".
174 All padding must be explicitly mentioned.
175 Lengths are in bytes. If this needs to be extended to bits,
176 use "name.bits".
177 Example: "st_dev,4:st_ino,4:st_mode,4:..." */
178 const char *stat_map;
179
99b2a78e
HPN
180 enum bfd_endian target_endian;
181
6a7e5cfe
HPN
182 /* Size of an "int" on the target (for syscalls whose ABI uses "int").
183 This must include padding, and only padding-at-higher-address is
184 supported. For example, a 64-bit target with 32-bit int:s which
185 are padded to 64 bits when in an array, should supposedly set this
186 to 8. The default is 4 which matches ILP32 targets and 64-bit
187 targets with 32-bit ints and no padding. */
188 int target_sizeof_int;
189
252b5132
RH
190 /* Marker for those wanting to do sanity checks.
191 This should remain the last member of this struct to help catch
192 miscompilation errors. */
193#define HOST_CALLBACK_MAGIC 4705 /* teds constant */
194 int magic;
195};
196
197extern host_callback default_callback;
198\f
199/* Canonical versions of system call numbers.
200 It's not intended to willy-nilly throw every system call ever heard
201 of in here. Only include those that have an important use.
202 ??? One can certainly start a discussion over the ones that are currently
203 here, but that will always be true. */
204
205/* These are used by the ANSI C support of libc. */
206#define CB_SYS_exit 1
207#define CB_SYS_open 2
208#define CB_SYS_close 3
209#define CB_SYS_read 4
210#define CB_SYS_write 5
211#define CB_SYS_lseek 6
212#define CB_SYS_unlink 7
213#define CB_SYS_getpid 8
214#define CB_SYS_kill 9
215#define CB_SYS_fstat 10
216/*#define CB_SYS_sbrk 11 - not currently a system call, but reserved. */
217
218/* ARGV support. */
219#define CB_SYS_argvlen 12
220#define CB_SYS_argv 13
221
222/* These are extras added for one reason or another. */
223#define CB_SYS_chdir 14
224#define CB_SYS_stat 15
225#define CB_SYS_chmod 16
226#define CB_SYS_utime 17
227#define CB_SYS_time 18
c4b36ce9
HPN
228
229/* More standard syscalls. */
230#define CB_SYS_lstat 19
d3ee64d1 231#define CB_SYS_rename 20
2adf959d
HPN
232#define CB_SYS_truncate 21
233#define CB_SYS_ftruncate 22
6a7e5cfe 234#define CB_SYS_pipe 23
252b5132
RH
235\f
236/* Struct use to pass and return information necessary to perform a
237 system call. */
238/* FIXME: Need to consider target word size. */
239
240typedef struct cb_syscall {
241 /* The target's value of what system call to perform. */
242 int func;
243 /* The arguments to the syscall. */
244 long arg1, arg2, arg3, arg4;
245
246 /* The result. */
247 long result;
248 /* Some system calls have two results. */
249 long result2;
250 /* The target's errno value, or 0 if success.
251 This is converted to the target's value with host_to_target_errno. */
252 int errcode;
253
254 /* Working space to be used by memory read/write callbacks. */
255 PTR p1;
256 PTR p2;
257 long x1,x2;
258
259 /* Callbacks for reading/writing memory (e.g. for read/write syscalls).
260 ??? long or unsigned long might be better to use for the `count'
261 argument here. We mimic sim_{read,write} for now. Be careful to
262 test any changes with -Wall -Werror, mixed signed comparisons
263 will get you. */
264 int (*read_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/,
265 unsigned long /*taddr*/, char * /*buf*/,
266 int /*bytes*/));
267 int (*write_mem) PARAMS ((host_callback * /*cb*/, struct cb_syscall * /*sc*/,
268 unsigned long /*taddr*/, const char * /*buf*/,
269 int /*bytes*/));
270
271 /* For sanity checking, should be last entry. */
272 int magic;
273} CB_SYSCALL;
274
275/* Magic number sanity checker. */
276#define CB_SYSCALL_MAGIC 0x12344321
277
278/* Macro to initialize CB_SYSCALL. Called first, before filling in
279 any fields. */
280#define CB_SYSCALL_INIT(sc) \
281do { \
282 memset ((sc), 0, sizeof (*(sc))); \
283 (sc)->magic = CB_SYSCALL_MAGIC; \
284} while (0)
285\f
286/* Return codes for various interface routines. */
287
288typedef enum {
289 CB_RC_OK = 0,
290 /* generic error */
291 CB_RC_ERR,
292 /* either file not found or no read access */
293 CB_RC_ACCESS,
294 CB_RC_NO_MEM
295} CB_RC;
296
297/* Read in target values for system call numbers, errno values, signals. */
298CB_RC cb_read_target_syscall_maps PARAMS ((host_callback *, const char *));
299
300/* Translate target to host syscall function numbers. */
301int cb_target_to_host_syscall PARAMS ((host_callback *, int));
302
303/* Translate host to target errno value. */
304int cb_host_to_target_errno PARAMS ((host_callback *, int));
305
306/* Translate target to host open flags. */
307int cb_target_to_host_open PARAMS ((host_callback *, int));
308
309/* Translate target signal number to host. */
310int cb_target_to_host_signal PARAMS ((host_callback *, int));
311
312/* Translate host signal number to target. */
313int cb_host_to_target_signal PARAMS ((host_callback *, int));
314
315/* Translate host stat struct to target.
316 If stat struct ptr is NULL, just compute target stat struct size.
317 Result is size of target stat struct or 0 if error. */
318int cb_host_to_target_stat PARAMS ((host_callback *, const struct stat *, PTR));
319
99b2a78e
HPN
320/* Translate a value to target endian. */
321void cb_store_target_endian PARAMS ((host_callback *, char *, int, long));
322
252b5132
RH
323/* Perform a system call. */
324CB_RC cb_syscall PARAMS ((host_callback *, CB_SYSCALL *));
325
326#endif
This page took 0.238074 seconds and 4 git commands to generate.