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