sim: drop use of bfd/configure.host
[deliverable/binutils-gdb.git] / sim / ppc / main.c
CommitLineData
c906108c
SS
1/* This file is part of the program psim.
2
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
3fd725ef 7 the Free Software Foundation; either version 3 of the License, or
c906108c
SS
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
51b318de 16 along with this program; if not, see <http://www.gnu.org/licenses/>.
c906108c
SS
17
18 */
19
20
21#include <stdarg.h>
22#include <stdio.h>
23#include <fcntl.h>
24
25#include <signal.h>
26
27#include "psim.h"
28#include "options.h"
29#include "device.h" /* FIXME: psim should provide the interface */
30#include "events.h" /* FIXME: psim should provide the interface */
31
de46f45f 32#include "bfd.h"
3c25f8c7
AC
33#include "gdb/callback.h"
34#include "gdb/remote-sim.h"
de46f45f 35
c906108c 36#include <stdlib.h>
c906108c
SS
37#ifdef HAVE_UNISTD_H
38#include <unistd.h>
39#endif
c906108c 40#include <string.h>
c906108c
SS
41#include <errno.h>
42
43#if !defined(O_NDELAY) || !defined(F_GETFL) || !defined(F_SETFL)
44#undef WITH_STDIO
45#define WITH_STDIO DO_USE_STDIO
46#endif
47
48
49extern char **environ;
50
51static psim *simulation = NULL;
52
53
54void
55sim_io_poll_quit (void)
56{
57 /* nothing to do */
58}
59
60void
61sim_io_printf_filtered(const char *msg, ...)
62{
63 va_list ap;
64 va_start(ap, msg);
65 vprintf(msg, ap);
66 va_end(ap);
67}
68
69void
70error (const char *msg, ...)
71{
72 va_list ap;
73 va_start(ap, msg);
74 vprintf(msg, ap);
75 printf("\n");
76 va_end(ap);
77
78 /* any final clean up */
79 if (ppc_trace[trace_print_info] && simulation != NULL)
80 psim_print_info (simulation, ppc_trace[trace_print_info]);
81
82 exit (1);
83}
84
85int
86sim_io_write_stdout(const char *buf,
87 int sizeof_buf)
88{
89 switch (CURRENT_STDIO) {
90 case DO_USE_STDIO:
91 {
92 int i;
93 for (i = 0; i < sizeof_buf; i++) {
94 putchar(buf[i]);
95 }
96 return i;
97 }
98 break;
99 case DONT_USE_STDIO:
100 return write(1, buf, sizeof_buf);
101 break;
102 default:
103 error("sim_io_write_stdout: invalid switch\n");
104 }
105 return 0;
106}
107
108int
109sim_io_write_stderr(const char *buf,
110 int sizeof_buf)
111{
112 switch (CURRENT_STDIO) {
113 case DO_USE_STDIO:
114 {
115 int i;
116 for (i = 0; i < sizeof_buf; i++) {
117 fputc(buf[i], stderr);
118 }
119 return i;
120 }
121 break;
122 case DONT_USE_STDIO:
123 return write(2, buf, sizeof_buf);
124 break;
125 default:
126 error("sim_io_write_stdout: invalid switch\n");
127 }
128 return 0;
129}
130
131int
132sim_io_read_stdin(char *buf,
133 int sizeof_buf)
134{
135 switch (CURRENT_STDIO) {
136 case DO_USE_STDIO:
137 if (sizeof_buf > 1) {
138 if (fgets(buf, sizeof_buf, stdin) != NULL)
139 return strlen(buf);
140 }
141 else if (sizeof_buf == 1) {
142 char b[2];
143 if (fgets(b, sizeof(b), stdin) != NULL) {
144 memcpy(buf, b, strlen(b));
145 return strlen(b);
146 }
147 }
148 else if (sizeof_buf == 0)
149 return 0;
150 return sim_io_eof;
151 break;
152 case DONT_USE_STDIO:
153#if defined(O_NDELAY) && defined(F_GETFL) && defined(F_SETFL)
154 {
155 /* check for input */
156 int flags;
157 int status;
158 int nr_read;
159 int result;
160 /* get the old status */
161 flags = fcntl(0, F_GETFL, 0);
162 if (flags == -1) {
163 perror("sim_io_read_stdin");
164 return sim_io_eof;
165 }
166 /* temp, disable blocking IO */
167 status = fcntl(0, F_SETFL, flags | O_NDELAY);
168 if (status == -1) {
169 perror("sim_io_read_stdin");
170 return sim_io_eof;
171 }
172 /* try for input */
173 nr_read = read(0, buf, sizeof_buf);
174 if (nr_read > 0
175 || (nr_read == 0 && sizeof_buf == 0))
176 result = nr_read;
177 else if (nr_read == 0)
178 result = sim_io_eof;
179 else { /* nr_read < 0 */
180 if (errno == EAGAIN)
181 result = sim_io_not_ready;
182 else
183 result = sim_io_eof;
184 }
185 /* return to regular vewing */
186 status = fcntl(0, F_SETFL, flags);
187 if (status == -1) {
188 perror("sim_io_read_stdin");
189 return sim_io_eof;
190 }
191 return result;
192 }
193 break;
194#endif
195 default:
196 error("sim_io_read_stdin: invalid switch\n");
197 break;
198 }
199 return 0;
200}
201
202void
203sim_io_flush_stdoutput(void)
204{
205 switch (CURRENT_STDIO) {
206 case DO_USE_STDIO:
207 fflush (stdout);
208 break;
209 case DONT_USE_STDIO:
210 break;
211 default:
212 error("sim_io_flush_stdoutput: invalid switch\n");
213 break;
214 }
215}
216
de46f45f
MG
217void
218sim_io_error (SIM_DESC sd, const char *msg, ...)
219{
220 va_list ap;
221 va_start(ap, msg);
222 vprintf(msg, ap);
223 printf("\n");
224 va_end(ap);
225
226 /* any final clean up */
227 if (ppc_trace[trace_print_info] && simulation != NULL)
228 psim_print_info (simulation, ppc_trace[trace_print_info]);
229
230 exit (1);
231}
232
c906108c
SS
233
234void *
235zalloc(long size)
236{
237 void *memory = malloc(size);
238 if (memory == NULL)
5c884464 239 error("zalloc failed\n");
c906108c
SS
240 memset(memory, 0, size);
241 return memory;
242}
243
c906108c
SS
244/* When a CNTRL-C occures, queue an event to shut down the simulation */
245
246static RETSIGTYPE
247cntrl_c(int sig)
248{
249 psim_stop (simulation);
250}
251
252
253int
254main(int argc, char **argv)
255{
256 const char *name_of_file;
257 char *arg_;
258 psim_status status;
259 device *root = psim_tree();
260
261 /* parse the arguments */
dea827fc 262 argv = psim_options (root, argv + 1, SIM_OPEN_STANDALONE);
c906108c
SS
263 if (argv[0] == NULL) {
264 if (ppc_trace[trace_opts]) {
265 print_options ();
266 return 0;
267 } else {
dea827fc 268 psim_usage (0, 0, SIM_OPEN_STANDALONE);
c906108c
SS
269 }
270 }
271 name_of_file = argv[0];
272
273 if (ppc_trace[trace_opts])
274 print_options ();
275
276 /* create the simulator */
277 simulation = psim_create(name_of_file, root);
278
279 /* fudge the environment so that _=prog-name */
280 arg_ = (char*)zalloc(strlen(argv[0]) + strlen("_=") + 1);
281 strcpy(arg_, "_=");
282 strcat(arg_, argv[0]);
283 putenv(arg_);
284
285 /* initialize it */
286 psim_init(simulation);
287 psim_stack(simulation, argv, environ);
288
289 {
290 RETSIGTYPE (*prev) ();
291 prev = signal(SIGINT, cntrl_c);
292 psim_run(simulation);
293 signal(SIGINT, prev);
294 }
295
296 /* any final clean up */
297 if (ppc_trace[trace_print_info])
298 psim_print_info (simulation, ppc_trace[trace_print_info]);
299
300 /* why did we stop */
301 status = psim_get_status(simulation);
302 switch (status.reason) {
303 case was_continuing:
93cfa9cf 304 error("psim: continuing while stopped!\n");
c906108c
SS
305 return 0;
306 case was_trap:
307 error("psim: no trap insn\n");
308 return 0;
309 case was_exited:
310 return status.signal;
311 case was_signalled:
312 printf ("%s: Caught signal %d at address 0x%lx\n",
313 name_of_file, (int)status.signal,
314 (long)status.program_counter);
315 return status.signal;
316 default:
317 error("unknown halt condition\n");
318 return 0;
319 }
320}
This page took 0.93614 seconds and 4 git commands to generate.