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