]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/common/sim-io.c
b5e874bef794f479cf6aa919c162c2072ad91947
1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2021 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. */
27 #include "targ-vals.h"
42 /* Define the rate at which the simulator should poll the host
44 #ifndef POLL_QUIT_INTERVAL
45 #define POLL_QUIT_INTERVAL 0x10
48 static int poll_quit_count
= POLL_QUIT_INTERVAL
;
50 /* See the file include/callbacks.h for a description */
54 sim_io_init (SIM_DESC sd
)
56 return STATE_CALLBACK (sd
)->init (STATE_CALLBACK (sd
));
61 sim_io_shutdown (SIM_DESC sd
)
63 return STATE_CALLBACK (sd
)->shutdown (STATE_CALLBACK (sd
));
68 sim_io_unlink (SIM_DESC sd
,
71 return STATE_CALLBACK (sd
)->unlink (STATE_CALLBACK (sd
), f1
);
76 sim_io_time (SIM_DESC sd
)
78 return STATE_CALLBACK (sd
)->time (STATE_CALLBACK (sd
));
83 sim_io_system (SIM_DESC sd
, const char *s
)
85 return STATE_CALLBACK (sd
)->system (STATE_CALLBACK (sd
), s
);
90 sim_io_rename (SIM_DESC sd
,
94 return STATE_CALLBACK (sd
)->rename (STATE_CALLBACK (sd
), f1
, f2
);
99 sim_io_write_stdout (SIM_DESC sd
,
103 switch (CURRENT_STDIO
) {
105 return STATE_CALLBACK (sd
)->write_stdout (STATE_CALLBACK (sd
), buf
, len
);
108 return STATE_CALLBACK (sd
)->write (STATE_CALLBACK (sd
), 1, buf
, len
);
111 sim_io_error (sd
, "sim_io_write_stdout: unaccounted switch\n");
119 sim_io_flush_stdout (SIM_DESC sd
)
121 switch (CURRENT_STDIO
) {
123 STATE_CALLBACK (sd
)->flush_stdout (STATE_CALLBACK (sd
));
128 sim_io_error (sd
, "sim_io_flush_stdout: unaccounted switch\n");
135 sim_io_write_stderr (SIM_DESC sd
,
139 switch (CURRENT_STDIO
) {
141 return STATE_CALLBACK (sd
)->write_stderr (STATE_CALLBACK (sd
), buf
, len
);
144 return STATE_CALLBACK (sd
)->write (STATE_CALLBACK (sd
), 2, buf
, len
);
147 sim_io_error (sd
, "sim_io_write_stderr: unaccounted switch\n");
155 sim_io_flush_stderr (SIM_DESC sd
)
157 switch (CURRENT_STDIO
) {
159 STATE_CALLBACK (sd
)->flush_stderr (STATE_CALLBACK (sd
));
164 sim_io_error (sd
, "sim_io_flush_stderr: unaccounted switch\n");
171 sim_io_write (SIM_DESC sd
,
176 return STATE_CALLBACK (sd
)->write (STATE_CALLBACK (sd
), fd
, buf
, len
);
181 sim_io_read_stdin (SIM_DESC sd
,
185 switch (CURRENT_STDIO
) {
187 return STATE_CALLBACK (sd
)->read_stdin (STATE_CALLBACK (sd
), buf
, len
);
190 return STATE_CALLBACK (sd
)->read (STATE_CALLBACK (sd
), 0, buf
, len
);
193 sim_io_error (sd
, "sim_io_read_stdin: unaccounted switch\n");
201 sim_io_read (SIM_DESC sd
, int fd
,
205 return STATE_CALLBACK (sd
)->read (STATE_CALLBACK (sd
), fd
, buf
, len
);
210 sim_io_open (SIM_DESC sd
,
214 return STATE_CALLBACK (sd
)->open (STATE_CALLBACK (sd
), name
, flags
);
219 sim_io_lseek (SIM_DESC sd
,
224 return STATE_CALLBACK (sd
)->lseek (STATE_CALLBACK (sd
), fd
, off
, way
);
229 sim_io_isatty (SIM_DESC sd
,
232 return STATE_CALLBACK (sd
)->isatty (STATE_CALLBACK (sd
), fd
);
237 sim_io_get_errno (SIM_DESC sd
)
239 return STATE_CALLBACK (sd
)->get_errno (STATE_CALLBACK (sd
));
244 sim_io_close (SIM_DESC sd
,
247 return STATE_CALLBACK (sd
)->close (STATE_CALLBACK (sd
), fd
);
252 sim_io_printf (SIM_DESC sd
,
258 STATE_CALLBACK (sd
)->vprintf_filtered (STATE_CALLBACK (sd
), fmt
, ap
);
264 sim_io_vprintf (SIM_DESC sd
,
268 STATE_CALLBACK (sd
)->vprintf_filtered (STATE_CALLBACK (sd
), fmt
, ap
);
273 sim_io_eprintf (SIM_DESC sd
,
279 STATE_CALLBACK (sd
)->evprintf_filtered (STATE_CALLBACK (sd
), fmt
, ap
);
285 sim_io_evprintf (SIM_DESC sd
,
289 STATE_CALLBACK (sd
)->evprintf_filtered (STATE_CALLBACK (sd
), fmt
, ap
);
294 sim_io_error (SIM_DESC sd
,
298 if (sd
== NULL
|| STATE_CALLBACK (sd
) == NULL
) {
301 vfprintf (stderr
, fmt
, ap
);
303 fprintf (stderr
, "\n");
309 STATE_CALLBACK (sd
)->evprintf_filtered (STATE_CALLBACK (sd
), fmt
, ap
);
311 STATE_CALLBACK (sd
)->error (STATE_CALLBACK (sd
), "");
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
)))
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
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
,
350 #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
351 int fd
= STATE_CALLBACK (sd
)->fdmap
[sim_io_fd
];
356 STATE_CALLBACK (sd
)->last_errno
= 0;
357 /* get the old status */
358 flags
= fcntl (fd
, F_GETFL
, 0);
361 perror ("sim_io_poll_read");
364 /* temp, disable blocking IO */
365 status
= fcntl (fd
, F_SETFL
, flags
| O_NDELAY
);
368 perror ("sim_io_read_stdin");
372 nr_read
= read (fd
, buf
, sizeof_buf
);
375 /* printf ("<nr-read=%d>\n", nr_read); */
381 STATE_CALLBACK (sd
)->last_errno
= errno
;
383 /* return to regular vewing */
384 status
= fcntl (fd
, F_SETFL
, flags
);
387 perror ("sim_io_read_stdin");
392 return sim_io_read (sd
, sim_io_fd
, buf
, sizeof_buf
);
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
);