Commit | Line | Data |
---|---|---|
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 | 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 | ||
49 | static int poll_quit_count = POLL_QUIT_INTERVAL; | |
c906108c SS |
50 | |
51 | /* See the file include/callbacks.h for a description */ | |
52 | ||
53 | ||
54 | int | |
34b47c38 | 55 | sim_io_init (SIM_DESC sd) |
c906108c SS |
56 | { |
57 | return STATE_CALLBACK (sd)->init (STATE_CALLBACK (sd)); | |
58 | } | |
59 | ||
60 | ||
61 | int | |
34b47c38 | 62 | sim_io_shutdown (SIM_DESC sd) |
c906108c SS |
63 | { |
64 | return STATE_CALLBACK (sd)->shutdown (STATE_CALLBACK (sd)); | |
65 | } | |
66 | ||
67 | ||
68 | int | |
34b47c38 MF |
69 | sim_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 |
76 | int64_t |
77 | sim_io_time (SIM_DESC sd) | |
c906108c | 78 | { |
00330cd1 | 79 | return STATE_CALLBACK (sd)->time (STATE_CALLBACK (sd)); |
c906108c SS |
80 | } |
81 | ||
82 | ||
83 | int | |
34b47c38 | 84 | sim_io_system (SIM_DESC sd, const char *s) |
c906108c SS |
85 | { |
86 | return STATE_CALLBACK (sd)->system (STATE_CALLBACK (sd), s); | |
87 | } | |
88 | ||
89 | ||
90 | int | |
34b47c38 MF |
91 | sim_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 | ||
99 | int | |
34b47c38 MF |
100 | sim_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 | ||
119 | void | |
34b47c38 | 120 | sim_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 | ||
135 | int | |
34b47c38 MF |
136 | sim_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 | ||
155 | void | |
34b47c38 | 156 | sim_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 | ||
171 | int | |
34b47c38 MF |
172 | sim_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 | ||
181 | int | |
34b47c38 MF |
182 | sim_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 | ||
201 | int | |
34b47c38 MF |
202 | sim_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 | ||
210 | int | |
34b47c38 MF |
211 | sim_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 | 219 | int64_t |
34b47c38 MF |
220 | sim_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 | ||
229 | int | |
34b47c38 MF |
230 | sim_io_isatty (SIM_DESC sd, |
231 | int fd) | |
c906108c SS |
232 | { |
233 | return STATE_CALLBACK (sd)->isatty (STATE_CALLBACK (sd), fd); | |
234 | } | |
235 | ||
236 | ||
237 | int | |
34b47c38 | 238 | sim_io_get_errno (SIM_DESC sd) |
c906108c SS |
239 | { |
240 | return STATE_CALLBACK (sd)->get_errno (STATE_CALLBACK (sd)); | |
241 | } | |
242 | ||
243 | ||
244 | int | |
34b47c38 MF |
245 | sim_io_close (SIM_DESC sd, |
246 | int fd) | |
c906108c SS |
247 | { |
248 | return STATE_CALLBACK (sd)->close (STATE_CALLBACK (sd), fd); | |
249 | } | |
250 | ||
251 | ||
252 | void | |
34b47c38 MF |
253 | sim_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 | ||
264 | void | |
34b47c38 MF |
265 | sim_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 | ||
273 | void | |
34b47c38 MF |
274 | sim_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 | ||
285 | void | |
34b47c38 MF |
286 | sim_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 | ||
294 | void | |
34b47c38 MF |
295 | sim_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); |
c906108c SS |
312 | STATE_CALLBACK (sd)->error (STATE_CALLBACK (sd), ""); |
313 | } | |
314 | } | |
315 | ||
316 | ||
317 | void | |
34b47c38 | 318 | sim_io_poll_quit (SIM_DESC sd) |
c906108c | 319 | { |
7a292a7a SS |
320 | if (STATE_CALLBACK (sd)->poll_quit != NULL && poll_quit_count-- < 0) |
321 | { | |
322 | poll_quit_count = POLL_QUIT_INTERVAL; | |
323 | if (STATE_CALLBACK (sd)->poll_quit (STATE_CALLBACK (sd))) | |
324 | sim_stop (sd); | |
325 | } | |
c906108c SS |
326 | } |
327 | ||
328 | ||
329 | /* Based on gdb-4.17/sim/ppc/main.c:sim_io_read_stdin(). | |
330 | ||
331 | FIXME: Should not be calling fcntl() or grubbing around inside of | |
332 | ->fdmap and ->errno. | |
333 | ||
334 | FIXME: Some completly new mechanism for handling the general | |
335 | problem of asynchronous IO is needed. | |
336 | ||
337 | FIXME: This function does not supress the echoing (ECHO) of input. | |
338 | Consequently polled input is always displayed. | |
339 | ||
340 | FIXME: This function does not perform uncooked reads. | |
341 | Consequently, data will not be read until an EOLN character has | |
342 | been entered. A cntrl-d may force the early termination of a line */ | |
343 | ||
344 | ||
345 | int | |
346 | sim_io_poll_read (SIM_DESC sd, | |
347 | int sim_io_fd, | |
348 | char *buf, | |
349 | int sizeof_buf) | |
350 | { | |
351 | #if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL) | |
352 | int fd = STATE_CALLBACK (sd)->fdmap[sim_io_fd]; | |
353 | int flags; | |
354 | int status; | |
355 | int nr_read; | |
356 | int result; | |
357 | STATE_CALLBACK (sd)->last_errno = 0; | |
358 | /* get the old status */ | |
359 | flags = fcntl (fd, F_GETFL, 0); | |
360 | if (flags == -1) | |
361 | { | |
362 | perror ("sim_io_poll_read"); | |
363 | return 0; | |
364 | } | |
365 | /* temp, disable blocking IO */ | |
366 | status = fcntl (fd, F_SETFL, flags | O_NDELAY); | |
367 | if (status == -1) | |
368 | { | |
369 | perror ("sim_io_read_stdin"); | |
370 | return 0; | |
371 | } | |
372 | /* try for input */ | |
373 | nr_read = read (fd, buf, sizeof_buf); | |
374 | if (nr_read >= 0) | |
375 | { | |
376 | /* printf ("<nr-read=%d>\n", nr_read); */ | |
377 | result = nr_read; | |
378 | } | |
379 | else | |
380 | { /* nr_read < 0 */ | |
381 | result = -1; | |
382 | STATE_CALLBACK (sd)->last_errno = errno; | |
383 | } | |
384 | /* return to regular vewing */ | |
385 | status = fcntl (fd, F_SETFL, flags); | |
386 | if (status == -1) | |
387 | { | |
388 | perror ("sim_io_read_stdin"); | |
389 | /* return 0; */ | |
390 | } | |
391 | return result; | |
392 | #else | |
393 | return sim_io_read (sd, sim_io_fd, buf, sizeof_buf); | |
394 | #endif | |
395 | } | |
165b70ea KB |
396 | |
397 | int | |
398 | sim_io_stat (SIM_DESC sd, const char *path, struct stat *buf) | |
399 | { | |
2d7bb758 | 400 | return STATE_CALLBACK (sd)->to_stat (STATE_CALLBACK (sd), path, buf); |
165b70ea KB |
401 | } |
402 | ||
403 | int | |
404 | sim_io_fstat (SIM_DESC sd, int fd, struct stat *buf) | |
405 | { | |
2d7bb758 | 406 | return STATE_CALLBACK (sd)->to_fstat (STATE_CALLBACK (sd), fd, buf); |
165b70ea | 407 | } |