* configure.in: Remove target-libstdc++-v3 from noconfigdirs for
[deliverable/binutils-gdb.git] / sim / ppc / sim_calls.c
1 /* This file is part of the program psim.
2
3 Copyright 1994, 1995, 1996, 1998, 2003 Andrew Cagney
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
7 the Free Software Foundation; either version 2 of the License, or
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
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21
22 #include <signal.h> /* FIXME - should be machine dependant version */
23 #include <stdarg.h>
24 #include <ctype.h>
25
26 #include "psim.h"
27 #include "options.h"
28
29 #undef printf_filtered /* blow away the mapping */
30
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #else
38 #ifdef HAVE_STRINGS_H
39 #include <strings.h>
40 #endif
41 #endif
42
43 #include "bfd.h"
44 #include "gdb/callback.h"
45 #include "gdb/remote-sim.h"
46
47 /* Define the rate at which the simulator should poll the host
48 for a quit. */
49 #ifndef POLL_QUIT_INTERVAL
50 #define POLL_QUIT_INTERVAL 0x20
51 #endif
52
53 static int poll_quit_count = POLL_QUIT_INTERVAL;
54
55 /* Structures used by the simulator, for gdb just have static structures */
56
57 psim *simulator;
58 static device *root_device;
59 static host_callback *callbacks;
60
61 SIM_DESC
62 sim_open (SIM_OPEN_KIND kind,
63 host_callback *callback,
64 struct bfd *abfd,
65 char **argv)
66 {
67 callbacks = callback;
68
69 /* Note: The simulation is not created by sim_open() because
70 complete information is not yet available */
71 /* trace the call */
72 TRACE(trace_gdb, ("sim_open called\n"));
73
74 if (root_device != NULL)
75 sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
76 root_device = psim_tree();
77 simulator = NULL;
78
79 psim_options(root_device, argv + 1);
80
81 if (ppc_trace[trace_opts])
82 print_options ();
83
84 /* fudge our descriptor for now */
85 return (SIM_DESC) 1;
86 }
87
88
89 void
90 sim_close (SIM_DESC sd, int quitting)
91 {
92 TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
93 if (ppc_trace[trace_print_info] && simulator != NULL)
94 psim_print_info (simulator, ppc_trace[trace_print_info]);
95 }
96
97
98 SIM_RC
99 sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
100 {
101 TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
102 prog, from_tty));
103 ASSERT(prog != NULL);
104
105 /* create the simulator */
106 TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
107 simulator = psim_create(prog, root_device);
108
109 /* bring in all the data section */
110 psim_init(simulator);
111
112 /* get the start address */
113 if (abfd == NULL)
114 {
115 abfd = bfd_openr (prog, 0);
116 if (abfd == NULL)
117 error ("psim: can't open \"%s\": %s\n",
118 prog, bfd_errmsg (bfd_get_error ()));
119 if (!bfd_check_format (abfd, bfd_object))
120 {
121 const char *errmsg = bfd_errmsg (bfd_get_error ());
122 bfd_close (abfd);
123 error ("psim: \"%s\" is not an object file: %s\n",
124 prog, errmsg);
125 }
126 bfd_close (abfd);
127 }
128
129 return SIM_RC_OK;
130 }
131
132
133 int
134 sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
135 {
136 int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
137 buf, mem, length);
138 TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
139 (long)mem, (long)buf, length, result));
140 return result;
141 }
142
143
144 int
145 sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
146 {
147 int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
148 buf, mem, length,
149 1/*violate_ro*/);
150 TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
151 (long)mem, (long)buf, length, result));
152 return result;
153 }
154
155 void
156 sim_info (SIM_DESC sd, int verbose)
157 {
158 TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
159 psim_print_info (simulator, verbose);
160 }
161
162
163 SIM_RC
164 sim_create_inferior (SIM_DESC sd,
165 struct bfd *abfd,
166 char **argv,
167 char **envp)
168 {
169 unsigned_word entry_point;
170 TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
171 entry_point));
172
173 if (simulator == NULL)
174 error ("No program loaded");
175
176 if (abfd != NULL)
177 entry_point = bfd_get_start_address (abfd);
178 else
179 entry_point = 0xfff00000; /* ??? */
180
181 psim_init(simulator);
182 psim_stack(simulator, argv, envp);
183
184 ASSERT (psim_write_register(simulator, -1 /* all start at same PC */,
185 &entry_point, "pc", cooked_transfer) > 0);
186 return SIM_RC_OK;
187 }
188
189
190 void
191 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
192 {
193 psim_status status = psim_get_status(simulator);
194
195 switch (status.reason) {
196 case was_continuing:
197 *reason = sim_stopped;
198 if (status.signal == 0)
199 *sigrc = SIGTRAP;
200 else
201 *sigrc = status.signal;
202 break;
203 case was_trap:
204 *reason = sim_stopped;
205 *sigrc = SIGTRAP;
206 break;
207 case was_exited:
208 *reason = sim_exited;
209 *sigrc = status.signal;
210 break;
211 case was_signalled:
212 *reason = sim_signalled;
213 *sigrc = status.signal;
214 break;
215 }
216
217 TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
218 (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
219 }
220
221
222
223 /* Run (or resume) the program. */
224
225 int
226 sim_stop (SIM_DESC sd)
227 {
228 psim_stop (simulator);
229 return 1;
230 }
231
232 void
233 sim_resume (SIM_DESC sd, int step, int siggnal)
234 {
235 TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
236 step, siggnal));
237
238 if (step)
239 {
240 psim_step (simulator);
241 }
242 else
243 {
244 psim_run (simulator);
245 }
246 }
247
248 void
249 sim_do_command (SIM_DESC sd, char *cmd)
250 {
251 TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
252 cmd ? cmd : "(null)"));
253 if (cmd != NULL) {
254 char **argv = buildargv(cmd);
255 psim_command(root_device, argv);
256 freeargv(argv);
257 }
258 }
259
260
261 /* Polling, if required */
262
263 void
264 sim_io_poll_quit (void)
265 {
266 if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
267 {
268 poll_quit_count = POLL_QUIT_INTERVAL;
269 if (callbacks->poll_quit (callbacks))
270 psim_stop (simulator);
271 }
272 }
273
274
275
276 /* Map simulator IO operations onto the corresponding GDB I/O
277 functions.
278
279 NB: Only a limited subset of operations are mapped across. More
280 advanced operations (such as dup or write) must either be mapped to
281 one of the below calls or handled internally */
282
283 int
284 sim_io_read_stdin(char *buf,
285 int sizeof_buf)
286 {
287 switch (CURRENT_STDIO) {
288 case DO_USE_STDIO:
289 return callbacks->read_stdin(callbacks, buf, sizeof_buf);
290 break;
291 case DONT_USE_STDIO:
292 return callbacks->read(callbacks, 0, buf, sizeof_buf);
293 break;
294 default:
295 error("sim_io_read_stdin: unaccounted switch\n");
296 break;
297 }
298 return 0;
299 }
300
301 int
302 sim_io_write_stdout(const char *buf,
303 int sizeof_buf)
304 {
305 switch (CURRENT_STDIO) {
306 case DO_USE_STDIO:
307 return callbacks->write_stdout(callbacks, buf, sizeof_buf);
308 break;
309 case DONT_USE_STDIO:
310 return callbacks->write(callbacks, 1, buf, sizeof_buf);
311 break;
312 default:
313 error("sim_io_write_stdout: unaccounted switch\n");
314 break;
315 }
316 return 0;
317 }
318
319 int
320 sim_io_write_stderr(const char *buf,
321 int sizeof_buf)
322 {
323 switch (CURRENT_STDIO) {
324 case DO_USE_STDIO:
325 /* NB: I think there should be an explicit write_stderr callback */
326 return callbacks->write(callbacks, 3, buf, sizeof_buf);
327 break;
328 case DONT_USE_STDIO:
329 return callbacks->write(callbacks, 3, buf, sizeof_buf);
330 break;
331 default:
332 error("sim_io_write_stderr: unaccounted switch\n");
333 break;
334 }
335 return 0;
336 }
337
338
339 void
340 sim_io_printf_filtered(const char *fmt,
341 ...)
342 {
343 char message[1024];
344 va_list ap;
345 /* format the message */
346 va_start(ap, fmt);
347 vsprintf(message, fmt, ap);
348 va_end(ap);
349 /* sanity check */
350 if (strlen(message) >= sizeof(message))
351 error("sim_io_printf_filtered: buffer overflow\n");
352 callbacks->printf_filtered(callbacks, "%s", message);
353 }
354
355 void
356 sim_io_flush_stdoutput(void)
357 {
358 switch (CURRENT_STDIO) {
359 case DO_USE_STDIO:
360 callbacks->flush_stdout (callbacks);
361 break;
362 case DONT_USE_STDIO:
363 break;
364 default:
365 error("sim_io_read_stdin: unaccounted switch\n");
366 break;
367 }
368 }
369
370 void
371 sim_io_error (SIM_DESC sd, const char *fmt, ...)
372 {
373 va_list ap;
374 va_start(ap, fmt);
375 callbacks->evprintf_filtered (callbacks, fmt, ap);
376 va_end(ap);
377 callbacks->error (callbacks, "");
378 }
379
380 /****/
381
382 void *
383 zalloc(long size)
384 {
385 void *memory = (void*)xmalloc(size);
386 if (memory == NULL)
387 error("xmalloc failed\n");
388 memset(memory, 0, size);
389 return memory;
390 }
391
392 void zfree(void *data)
393 {
394 free(data);
395 }
This page took 0.039607 seconds and 4 git commands to generate.