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