sim: callback: convert time interface to 64-bit
[deliverable/binutils-gdb.git] / sim / common / sim-io.c
CommitLineData
b85e4829
AC
1/* The common simulator framework for GDB, the GNU Debugger.
2
3666a048 3 Copyright 2002-2021 Free Software Foundation, Inc.
b85e4829
AC
4
5 Contributed by Andrew Cagney and Red Hat.
6
7 This file is part of GDB.
8
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
4744ac1b 11 the Free Software Foundation; either version 3 of the License, or
b85e4829
AC
12 (at your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
4744ac1b 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22
23#include "sim-main.h"
24#include "sim-io.h"
25#include "targ-vals.h"
26
27#include <errno.h>
28#if HAVE_FCNTL_H
29#include <fcntl.h>
30#endif
31
32#if HAVE_UNISTD_H
33#include <unistd.h>
34#endif
35
b760fb3a
SM
36#include <stdlib.h>
37
7a292a7a
SS
38/* Define the rate at which the simulator should poll the host
39 for a quit. */
40#ifndef POLL_QUIT_INTERVAL
41#define POLL_QUIT_INTERVAL 0x10
42#endif
43
44static int poll_quit_count = POLL_QUIT_INTERVAL;
c906108c
SS
45
46/* See the file include/callbacks.h for a description */
47
48
49int
34b47c38 50sim_io_init (SIM_DESC sd)
c906108c
SS
51{
52 return STATE_CALLBACK (sd)->init (STATE_CALLBACK (sd));
53}
54
55
56int
34b47c38 57sim_io_shutdown (SIM_DESC sd)
c906108c
SS
58{
59 return STATE_CALLBACK (sd)->shutdown (STATE_CALLBACK (sd));
60}
61
62
63int
34b47c38
MF
64sim_io_unlink (SIM_DESC sd,
65 const char *f1)
c906108c
SS
66{
67 return STATE_CALLBACK (sd)->unlink (STATE_CALLBACK (sd), f1);
68}
69
70
00330cd1
MF
71int64_t
72sim_io_time (SIM_DESC sd)
c906108c 73{
00330cd1 74 return STATE_CALLBACK (sd)->time (STATE_CALLBACK (sd));
c906108c
SS
75}
76
77
78int
34b47c38 79sim_io_system (SIM_DESC sd, const char *s)
c906108c
SS
80{
81 return STATE_CALLBACK (sd)->system (STATE_CALLBACK (sd), s);
82}
83
84
85int
34b47c38
MF
86sim_io_rename (SIM_DESC sd,
87 const char *f1,
88 const char *f2)
c906108c
SS
89{
90 return STATE_CALLBACK (sd)->rename (STATE_CALLBACK (sd), f1, f2);
91}
92
93
94int
34b47c38
MF
95sim_io_write_stdout (SIM_DESC sd,
96 const char *buf,
97 int len)
c906108c
SS
98{
99 switch (CURRENT_STDIO) {
100 case DO_USE_STDIO:
101 return STATE_CALLBACK (sd)->write_stdout (STATE_CALLBACK (sd), buf, len);
102 break;
103 case DONT_USE_STDIO:
104 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 1, buf, len);
105 break;
106 default:
107 sim_io_error (sd, "sim_io_write_stdout: unaccounted switch\n");
108 break;
109 }
110 return 0;
111}
112
113
114void
34b47c38 115sim_io_flush_stdout (SIM_DESC sd)
c906108c
SS
116{
117 switch (CURRENT_STDIO) {
118 case DO_USE_STDIO:
119 STATE_CALLBACK (sd)->flush_stdout (STATE_CALLBACK (sd));
120 break;
121 case DONT_USE_STDIO:
122 break;
123 default:
124 sim_io_error (sd, "sim_io_flush_stdout: unaccounted switch\n");
125 break;
126 }
127}
128
129
130int
34b47c38
MF
131sim_io_write_stderr (SIM_DESC sd,
132 const char *buf,
133 int len)
c906108c
SS
134{
135 switch (CURRENT_STDIO) {
136 case DO_USE_STDIO:
137 return STATE_CALLBACK (sd)->write_stderr (STATE_CALLBACK (sd), buf, len);
138 break;
139 case DONT_USE_STDIO:
140 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), 2, buf, len);
141 break;
142 default:
143 sim_io_error (sd, "sim_io_write_stderr: unaccounted switch\n");
144 break;
145 }
146 return 0;
147}
148
149
150void
34b47c38 151sim_io_flush_stderr (SIM_DESC sd)
c906108c
SS
152{
153 switch (CURRENT_STDIO) {
154 case DO_USE_STDIO:
155 STATE_CALLBACK (sd)->flush_stderr (STATE_CALLBACK (sd));
156 break;
157 case DONT_USE_STDIO:
158 break;
159 default:
160 sim_io_error (sd, "sim_io_flush_stderr: unaccounted switch\n");
161 break;
162 }
163}
164
165
166int
34b47c38
MF
167sim_io_write (SIM_DESC sd,
168 int fd,
169 const char *buf,
170 int len)
c906108c
SS
171{
172 return STATE_CALLBACK (sd)->write (STATE_CALLBACK (sd), fd, buf, len);
173}
174
175
176int
34b47c38
MF
177sim_io_read_stdin (SIM_DESC sd,
178 char *buf,
179 int len)
c906108c
SS
180{
181 switch (CURRENT_STDIO) {
182 case DO_USE_STDIO:
183 return STATE_CALLBACK (sd)->read_stdin (STATE_CALLBACK (sd), buf, len);
184 break;
185 case DONT_USE_STDIO:
186 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), 0, buf, len);
187 break;
188 default:
189 sim_io_error (sd, "sim_io_read_stdin: unaccounted switch\n");
190 break;
191 }
192 return 0;
193}
194
195
196int
34b47c38
MF
197sim_io_read (SIM_DESC sd, int fd,
198 char *buf,
199 int len)
c906108c
SS
200{
201 return STATE_CALLBACK (sd)->read (STATE_CALLBACK (sd), fd, buf, len);
202}
203
204
205int
34b47c38
MF
206sim_io_open (SIM_DESC sd,
207 const char *name,
208 int flags)
c906108c
SS
209{
210 return STATE_CALLBACK (sd)->open (STATE_CALLBACK (sd), name, flags);
211}
212
213
214int
34b47c38
MF
215sim_io_lseek (SIM_DESC sd,
216 int fd,
217 long off,
218 int way)
c906108c
SS
219{
220 return STATE_CALLBACK (sd)->lseek (STATE_CALLBACK (sd), fd, off, way);
221}
222
223
224int
34b47c38
MF
225sim_io_isatty (SIM_DESC sd,
226 int fd)
c906108c
SS
227{
228 return STATE_CALLBACK (sd)->isatty (STATE_CALLBACK (sd), fd);
229}
230
231
232int
34b47c38 233sim_io_get_errno (SIM_DESC sd)
c906108c
SS
234{
235 return STATE_CALLBACK (sd)->get_errno (STATE_CALLBACK (sd));
236}
237
238
239int
34b47c38
MF
240sim_io_close (SIM_DESC sd,
241 int fd)
c906108c
SS
242{
243 return STATE_CALLBACK (sd)->close (STATE_CALLBACK (sd), fd);
244}
245
246
247void
34b47c38
MF
248sim_io_printf (SIM_DESC sd,
249 const char *fmt,
250 ...)
c906108c
SS
251{
252 va_list ap;
34b47c38 253 va_start (ap, fmt);
c906108c 254 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
34b47c38 255 va_end (ap);
c906108c
SS
256}
257
258
259void
34b47c38
MF
260sim_io_vprintf (SIM_DESC sd,
261 const char *fmt,
262 va_list ap)
c906108c
SS
263{
264 STATE_CALLBACK (sd)->vprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
265}
266
267
268void
34b47c38
MF
269sim_io_eprintf (SIM_DESC sd,
270 const char *fmt,
271 ...)
c906108c
SS
272{
273 va_list ap;
34b47c38 274 va_start (ap, fmt);
c906108c 275 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
34b47c38 276 va_end (ap);
c906108c
SS
277}
278
279
280void
34b47c38
MF
281sim_io_evprintf (SIM_DESC sd,
282 const char *fmt,
283 va_list ap)
c906108c
SS
284{
285 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
286}
287
288
289void
34b47c38
MF
290sim_io_error (SIM_DESC sd,
291 const char *fmt,
292 ...)
c906108c
SS
293{
294 if (sd == NULL || STATE_CALLBACK (sd) == NULL) {
295 va_list ap;
34b47c38 296 va_start (ap, fmt);
c906108c 297 vfprintf (stderr, fmt, ap);
34b47c38 298 va_end (ap);
c906108c
SS
299 fprintf (stderr, "\n");
300 abort ();
301 }
302 else {
303 va_list ap;
34b47c38 304 va_start (ap, fmt);
c906108c 305 STATE_CALLBACK (sd)->evprintf_filtered (STATE_CALLBACK (sd), fmt, ap);
34b47c38 306 va_end (ap);
c906108c
SS
307 STATE_CALLBACK (sd)->error (STATE_CALLBACK (sd), "");
308 }
309}
310
311
312void
34b47c38 313sim_io_poll_quit (SIM_DESC sd)
c906108c 314{
7a292a7a
SS
315 if (STATE_CALLBACK (sd)->poll_quit != NULL && poll_quit_count-- < 0)
316 {
317 poll_quit_count = POLL_QUIT_INTERVAL;
318 if (STATE_CALLBACK (sd)->poll_quit (STATE_CALLBACK (sd)))
319 sim_stop (sd);
320 }
c906108c
SS
321}
322
323
324/* Based on gdb-4.17/sim/ppc/main.c:sim_io_read_stdin().
325
326 FIXME: Should not be calling fcntl() or grubbing around inside of
327 ->fdmap and ->errno.
328
329 FIXME: Some completly new mechanism for handling the general
330 problem of asynchronous IO is needed.
331
332 FIXME: This function does not supress the echoing (ECHO) of input.
333 Consequently polled input is always displayed.
334
335 FIXME: This function does not perform uncooked reads.
336 Consequently, data will not be read until an EOLN character has
337 been entered. A cntrl-d may force the early termination of a line */
338
339
340int
341sim_io_poll_read (SIM_DESC sd,
342 int sim_io_fd,
343 char *buf,
344 int sizeof_buf)
345{
346#if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
347 int fd = STATE_CALLBACK (sd)->fdmap[sim_io_fd];
348 int flags;
349 int status;
350 int nr_read;
351 int result;
352 STATE_CALLBACK (sd)->last_errno = 0;
353 /* get the old status */
354 flags = fcntl (fd, F_GETFL, 0);
355 if (flags == -1)
356 {
357 perror ("sim_io_poll_read");
358 return 0;
359 }
360 /* temp, disable blocking IO */
361 status = fcntl (fd, F_SETFL, flags | O_NDELAY);
362 if (status == -1)
363 {
364 perror ("sim_io_read_stdin");
365 return 0;
366 }
367 /* try for input */
368 nr_read = read (fd, buf, sizeof_buf);
369 if (nr_read >= 0)
370 {
371 /* printf ("<nr-read=%d>\n", nr_read); */
372 result = nr_read;
373 }
374 else
375 { /* nr_read < 0 */
376 result = -1;
377 STATE_CALLBACK (sd)->last_errno = errno;
378 }
379 /* return to regular vewing */
380 status = fcntl (fd, F_SETFL, flags);
381 if (status == -1)
382 {
383 perror ("sim_io_read_stdin");
384 /* return 0; */
385 }
386 return result;
387#else
388 return sim_io_read (sd, sim_io_fd, buf, sizeof_buf);
389#endif
390}
165b70ea
KB
391
392int
393sim_io_stat (SIM_DESC sd, const char *path, struct stat *buf)
394{
2d7bb758 395 return STATE_CALLBACK (sd)->to_stat (STATE_CALLBACK (sd), path, buf);
165b70ea
KB
396}
397
398int
399sim_io_fstat (SIM_DESC sd, int fd, struct stat *buf)
400{
2d7bb758 401 return STATE_CALLBACK (sd)->to_fstat (STATE_CALLBACK (sd), fd, buf);
165b70ea 402}
This page took 0.957186 seconds and 4 git commands to generate.