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