]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/ppc/sim_calls.c
Add ABFD argument to sim_create_inferior. Document.
[thirdparty/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 SIM_DESC
57 sim_open (SIM_OPEN_KIND kind,
58 host_callback *callback,
59 struct _bfd *abfd,
60 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 /* get the start address */
113 if (abfd == NULL)
114 {
115 abfd = bfd_openr (argv[0], 0);
116 if (abfd == NULL)
117 error ("psim: can't open \"%s\": %s\n",
118 argv[0], 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 argv[0], errmsg);
125 }
126 bfd_close (abfd);
127 }
128
129 /* release the arguments */
130 freeargv(argv);
131
132 return SIM_RC_OK;
133 }
134
135
136 int
137 sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
138 {
139 int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
140 buf, mem, length);
141 TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
142 (long)mem, (long)buf, length, result));
143 return result;
144 }
145
146
147 int
148 sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
149 {
150 int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
151 buf, mem, length,
152 1/*violate_ro*/);
153 TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
154 (long)mem, (long)buf, length, result));
155 return result;
156 }
157
158
159 void
160 sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf)
161 {
162 if (simulator == NULL) {
163 return;
164 }
165 TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
166 regno, register_names[regno], (long)buf));
167 psim_read_register(simulator, MAX_NR_PROCESSORS,
168 buf, register_names[regno],
169 raw_transfer);
170 }
171
172
173 void
174 sim_store_register (SIM_DESC sd, int regno, unsigned char *buf)
175 {
176 if (simulator == NULL)
177 return;
178 TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
179 regno, register_names[regno], (long)buf));
180 psim_write_register(simulator, MAX_NR_PROCESSORS,
181 buf, register_names[regno],
182 raw_transfer);
183 }
184
185
186 void
187 sim_info (SIM_DESC sd, int verbose)
188 {
189 TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
190 psim_print_info (simulator, verbose);
191 }
192
193
194 SIM_RC
195 sim_create_inferior (SIM_DESC sd,
196 struct _bfd *abfd,
197 char **argv,
198 char **envp)
199 {
200 unsigned_word entry_point;
201 TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
202 entry_point));
203 if (abfd != NULL)
204 entry_point = bfd_get_start_address (abfd);
205 else
206 entry_point = 0xfff00000; /* ??? */
207
208 psim_init(simulator);
209 psim_stack(simulator, argv, envp);
210
211 psim_write_register(simulator, -1 /* all start at same PC */,
212 &entry_point, "pc", cooked_transfer);
213 return SIM_RC_OK;
214 }
215
216
217 void
218 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
219 {
220 psim_status status = psim_get_status(simulator);
221
222 switch (status.reason) {
223 case was_continuing:
224 *reason = sim_stopped;
225 if (status.signal == 0)
226 *sigrc = SIGTRAP;
227 else
228 *sigrc = status.signal;
229 break;
230 case was_trap:
231 *reason = sim_stopped;
232 *sigrc = SIGTRAP;
233 break;
234 case was_exited:
235 *reason = sim_exited;
236 *sigrc = status.signal;
237 break;
238 case was_signalled:
239 *reason = sim_signalled;
240 *sigrc = status.signal;
241 break;
242 }
243
244 TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
245 (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
246 }
247
248
249
250 /* Run (or resume) the program. */
251
252 int
253 sim_stop (SIM_DESC sd)
254 {
255 psim_stop (simulator);
256 return 1;
257 }
258
259 void
260 sim_resume (SIM_DESC sd, int step, int siggnal)
261 {
262 TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
263 step, siggnal));
264
265 if (step)
266 {
267 psim_step (simulator);
268 }
269 else
270 {
271 psim_run (simulator);
272 }
273 }
274
275 void
276 sim_do_command (SIM_DESC sd, char *cmd)
277 {
278 TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
279 cmd ? cmd : "(null)"));
280 if (cmd != NULL) {
281 char **argv = buildargv(cmd);
282 psim_command(root_device, argv);
283 freeargv(argv);
284 }
285 }
286
287
288 /* Map simulator IO operations onto the corresponding GDB I/O
289 functions.
290
291 NB: Only a limited subset of operations are mapped across. More
292 advanced operations (such as dup or write) must either be mapped to
293 one of the below calls or handled internally */
294
295 int
296 sim_io_read_stdin(char *buf,
297 int sizeof_buf)
298 {
299 switch (CURRENT_STDIO) {
300 case DO_USE_STDIO:
301 return callbacks->read_stdin(callbacks, buf, sizeof_buf);
302 break;
303 case DONT_USE_STDIO:
304 return callbacks->read(callbacks, 0, buf, sizeof_buf);
305 break;
306 default:
307 error("sim_io_read_stdin: unaccounted switch\n");
308 break;
309 }
310 return 0;
311 }
312
313 int
314 sim_io_write_stdout(const char *buf,
315 int sizeof_buf)
316 {
317 switch (CURRENT_STDIO) {
318 case DO_USE_STDIO:
319 return callbacks->write_stdout(callbacks, buf, sizeof_buf);
320 break;
321 case DONT_USE_STDIO:
322 return callbacks->write(callbacks, 1, buf, sizeof_buf);
323 break;
324 default:
325 error("sim_io_write_stdout: unaccounted switch\n");
326 break;
327 }
328 return 0;
329 }
330
331 int
332 sim_io_write_stderr(const char *buf,
333 int sizeof_buf)
334 {
335 switch (CURRENT_STDIO) {
336 case DO_USE_STDIO:
337 /* NB: I think there should be an explicit write_stderr callback */
338 return callbacks->write(callbacks, 3, buf, sizeof_buf);
339 break;
340 case DONT_USE_STDIO:
341 return callbacks->write(callbacks, 3, buf, sizeof_buf);
342 break;
343 default:
344 error("sim_io_write_stderr: unaccounted switch\n");
345 break;
346 }
347 return 0;
348 }
349
350
351 void
352 sim_io_printf_filtered(const char *fmt,
353 ...)
354 {
355 char message[1024];
356 va_list ap;
357 /* format the message */
358 va_start(ap, fmt);
359 vsprintf(message, fmt, ap);
360 va_end(ap);
361 /* sanity check */
362 if (strlen(message) >= sizeof(message))
363 error("sim_io_printf_filtered: buffer overflow\n");
364 callbacks->printf_filtered(callbacks, "%s", message);
365 }
366
367 void
368 sim_io_flush_stdoutput(void)
369 {
370 switch (CURRENT_STDIO) {
371 case DO_USE_STDIO:
372 gdb_flush (gdb_stdout);
373 break;
374 case DONT_USE_STDIO:
375 break;
376 default:
377 error("sim_io_read_stdin: unaccounted switch\n");
378 break;
379 }
380 }
381
382 /****/
383
384 void *
385 zalloc(long size)
386 {
387 void *memory = (void*)xmalloc(size);
388 if (memory == NULL)
389 error("xmalloc failed\n");
390 memset(memory, 0, size);
391 return memory;
392 }
393
394 void zfree(void *data)
395 {
396 mfree(NULL, data);
397 }