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