Automatic date update in version.in
[binutils-gdb.git] / sim / common / sim-io.c
blob988c50ef0ec408d86880f5c9171083224a38f332
1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2024 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney and Red Hat.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* This must come before any other includes. */
23 #include "defs.h"
25 #include <errno.h>
26 #if HAVE_FCNTL_H
27 #include <fcntl.h>
28 #endif
29 #include <stdarg.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <unistd.h>
34 #undef open
36 #include "sim-main.h"
37 #include "sim-io.h"
38 #include "sim/callback.h"
40 /* Define the rate at which the simulator should poll the host
41 for a quit. */
42 #ifndef POLL_QUIT_INTERVAL
43 #define POLL_QUIT_INTERVAL 0x10
44 #endif
46 static int poll_quit_count = POLL_QUIT_INTERVAL;
48 /* See the file include/callbacks.h for a description */
51 int
52 sim_io_init (SIM_DESC sd)
54 return STATE_CALLBACK (sd)->init (STATE_CALLBACK (sd));
58 int
59 sim_io_shutdown (SIM_DESC sd)
61 return STATE_CALLBACK (sd)->shutdown (STATE_CALLBACK (sd));
65 int
66 sim_io_unlink (SIM_DESC sd,
67 const char *f1)
69 return STATE_CALLBACK (sd)->unlink (STATE_CALLBACK (sd), f1);
73 int64_t
74 sim_io_time (SIM_DESC sd)
76 return STATE_CALLBACK (sd)->time (STATE_CALLBACK (sd));
80 int
81 sim_io_system (SIM_DESC sd, const char *s)
83 return STATE_CALLBACK (sd)->system (STATE_CALLBACK (sd), s);
87 int
88 sim_io_rename (SIM_DESC sd,
89 const char *f1,
90 const char *f2)
92 return STATE_CALLBACK (sd)->rename (STATE_CALLBACK (sd), f1, f2);
96 int
97 sim_io_write_stdout (SIM_DESC sd,
98 const char *buf,
99 int len)
101 switch (CURRENT_STDIO) {
102 case DO_USE_STDIO:
103 return STATE_CALLBACK (sd)->write_stdout (STATE_CALLBACK (sd), buf, len);
104 break;
105 case DONT_USE_STDIO:
106 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 1, buf, len);
107 break;
108 default:
109 sim_io_error (sd, "sim_io_write_stdout: unaccounted switch\n");
110 break;
112 return 0;
116 void
117 sim_io_flush_stdout (SIM_DESC sd)
119 switch (CURRENT_STDIO) {
120 case DO_USE_STDIO:
121 STATE_CALLBACK (sd)->flush_stdout (STATE_CALLBACK (sd));
122 break;
123 case DONT_USE_STDIO:
124 break;
125 default:
126 sim_io_error (sd, "sim_io_flush_stdout: unaccounted switch\n");
127 break;
133 sim_io_write_stderr (SIM_DESC sd,
134 const char *buf,
135 int len)
137 switch (CURRENT_STDIO) {
138 case DO_USE_STDIO:
139 return STATE_CALLBACK (sd)->write_stderr (STATE_CALLBACK (sd), buf, len);
140 break;
141 case DONT_USE_STDIO:
142 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 2, buf, len);
143 break;
144 default:
145 sim_io_error (sd, "sim_io_write_stderr: unaccounted switch\n");
146 break;
148 return 0;
152 void
153 sim_io_flush_stderr (SIM_DESC sd)
155 switch (CURRENT_STDIO) {
156 case DO_USE_STDIO:
157 STATE_CALLBACK (sd)->flush_stderr (STATE_CALLBACK (sd));
158 break;
159 case DONT_USE_STDIO:
160 break;
161 default:
162 sim_io_error (sd, "sim_io_flush_stderr: unaccounted switch\n");
163 break;
169 sim_io_write (SIM_DESC sd,
170 int fd,
171 const char *buf,
172 int len)
174 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), fd, buf, len);
179 sim_io_read_stdin (SIM_DESC sd,
180 char *buf,
181 int len)
183 switch (CURRENT_STDIO) {
184 case DO_USE_STDIO:
185 return STATE_CALLBACK (sd)->read_stdin (STATE_CALLBACK (sd), buf, len);
186 break;
187 case DONT_USE_STDIO:
188 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), 0, buf, len);
189 break;
190 default:
191 sim_io_error (sd, "sim_io_read_stdin: unaccounted switch\n");
192 break;
194 return 0;
199 sim_io_read (SIM_DESC sd, int fd,
200 char *buf,
201 int len)
203 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), fd, buf, len);
208 sim_io_open (SIM_DESC sd,
209 const char *name,
210 int flags)
212 return STATE_CALLBACK (sd)->open (STATE_CALLBACK (sd), name, flags);
216 int64_t
217 sim_io_lseek (SIM_DESC sd,
218 int fd,
219 int64_t off,
220 int way)
222 return STATE_CALLBACK (sd)->lseek (STATE_CALLBACK (sd), fd, off, way);
227 sim_io_isatty (SIM_DESC sd,
228 int fd)
230 return STATE_CALLBACK (sd)->isatty (STATE_CALLBACK (sd), fd);
235 sim_io_get_errno (SIM_DESC sd)
237 return STATE_CALLBACK (sd)->get_errno (STATE_CALLBACK (sd));
242 sim_io_close (SIM_DESC sd,
243 int fd)
245 return STATE_CALLBACK (sd)->close (STATE_CALLBACK (sd), fd);
249 void
250 sim_io_printf (SIM_DESC sd,
251 const char *fmt,
252 ...)
254 va_list ap;
255 va_start (ap, fmt);
256 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
257 va_end (ap);
261 void
262 sim_io_vprintf (SIM_DESC sd,
263 const char *fmt,
264 va_list ap)
266 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
270 void
271 sim_io_eprintf (SIM_DESC sd,
272 const char *fmt,
273 ...)
275 va_list ap;
276 va_start (ap, fmt);
277 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
278 va_end (ap);
282 void
283 sim_io_evprintf (SIM_DESC sd,
284 const char *fmt,
285 va_list ap)
287 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
291 void
292 sim_io_error (SIM_DESC sd,
293 const char *fmt,
294 ...)
296 if (sd == NULL || STATE_CALLBACK (sd) == NULL) {
297 va_list ap;
298 va_start (ap, fmt);
299 vfprintf (stderr, fmt, ap);
300 va_end (ap);
301 fprintf (stderr, "\n");
302 abort ();
304 else {
305 va_list ap;
306 va_start (ap, fmt);
307 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
308 va_end (ap);
309 /* The %s avoids empty printf compiler warnings. Not ideal, but we want
310 error's side-effect where it halts processing. */
311 STATE_CALLBACK (sd)->error (STATE_CALLBACK (sd), "%s", "");
316 void
317 sim_io_poll_quit (SIM_DESC sd)
319 if (STATE_CALLBACK (sd)->poll_quit != NULL && poll_quit_count-- < 0)
321 poll_quit_count = POLL_QUIT_INTERVAL;
322 if (STATE_CALLBACK (sd)->poll_quit (STATE_CALLBACK (sd)))
323 sim_stop (sd);
328 /* Based on gdb-4.17/sim/ppc/main.c:sim_io_read_stdin().
330 FIXME: Should not be calling fcntl() or grubbing around inside of
331 ->fdmap and ->errno.
333 FIXME: Some completly new mechanism for handling the general
334 problem of asynchronous IO is needed.
336 FIXME: This function does not supress the echoing (ECHO) of input.
337 Consequently polled input is always displayed.
339 FIXME: This function does not perform uncooked reads.
340 Consequently, data will not be read until an EOLN character has
341 been entered. A cntrl-d may force the early termination of a line */
345 sim_io_poll_read (SIM_DESC sd,
346 int sim_io_fd,
347 char *buf,
348 int sizeof_buf)
350 #if defined(O_NONBLOCK) && defined(F_GETFL) && defined(F_SETFL)
351 int fd = STATE_CALLBACK (sd)->fdmap[sim_io_fd];
352 int flags;
353 int status;
354 int nr_read;
355 int result;
356 STATE_CALLBACK (sd)->last_errno = 0;
357 /* get the old status */
358 flags = fcntl (fd, F_GETFL, 0);
359 if (flags == -1)
361 perror ("sim_io_poll_read");
362 return 0;
364 /* temp, disable blocking IO */
365 status = fcntl (fd, F_SETFL, flags | O_NONBLOCK);
366 if (status == -1)
368 perror ("sim_io_read_stdin");
369 return 0;
371 /* try for input */
372 nr_read = read (fd, buf, sizeof_buf);
373 if (nr_read >= 0)
375 /* printf ("<nr-read=%d>\n", nr_read); */
376 result = nr_read;
378 else
379 { /* nr_read < 0 */
380 result = -1;
381 STATE_CALLBACK (sd)->last_errno = errno;
383 /* return to regular vewing */
384 status = fcntl (fd, F_SETFL, flags);
385 if (status == -1)
387 perror ("sim_io_read_stdin");
388 /* return 0; */
390 return result;
391 #else
392 return sim_io_read (sd, sim_io_fd, buf, sizeof_buf);
393 #endif
397 sim_io_stat (SIM_DESC sd, const char *path, struct stat *buf)
399 return STATE_CALLBACK (sd)->to_stat (STATE_CALLBACK (sd), path, buf);
403 sim_io_fstat (SIM_DESC sd, int fd, struct stat *buf)
405 return STATE_CALLBACK (sd)->to_fstat (STATE_CALLBACK (sd), fd, buf);