1 /* This file is part of the program psim.
3 Copyright 1994, 1995, 1996, 1998, 2003 Andrew Cagney
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.
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.
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.
22 #include <signal.h> /* FIXME - should be machine dependant version */
29 #undef printf_filtered /* blow away the mapping */
45 #include "gdb/callback.h"
46 #include "gdb/remote-sim.h"
48 /* Define the rate at which the simulator should poll the host
50 #ifndef POLL_QUIT_INTERVAL
51 #define POLL_QUIT_INTERVAL 0x20
54 static int poll_quit_count
= POLL_QUIT_INTERVAL
;
56 /* Structures used by the simulator, for gdb just have static structures */
58 static psim
*simulator
;
59 static device
*root_device
;
60 static host_callback
*callbacks
;
62 /* We use GDB's gdbarch_register_name function to map GDB register
63 numbers onto names, which we can then look up in the register
64 table. Since the `set architecture' command can select a new
65 processor variant at run-time, the meanings of the register numbers
66 can change, so we need to make sure the sim uses the same
67 name/number mapping that GDB uses.
69 (We don't use the REGISTER_NAME macro, which is a wrapper for
70 gdbarch_register_name. We #include GDB's "defs.h", which tries to
71 #include GDB's "config.h", but gets ours instead, and REGISTER_NAME
72 ends up not getting defined. Simpler to just use
73 gdbarch_register_name directly.)
75 We used to just use the REGISTER_NAMES macro from GDB's
76 target-dependent header files, which expanded into an initializer
77 for an array of strings. That was kind of nice, because it meant
78 that libsim.a had only a compile-time dependency on GDB; using
79 gdbarch_register_name directly means that there are now link-time
80 and run-time dependencies too.
82 Perhaps the host_callback structure could provide a function for
83 retrieving register names; that would be cleaner. */
86 sim_open (SIM_OPEN_KIND kind
,
87 host_callback
*callback
,
93 /* Note: The simulation is not created by sim_open() because
94 complete information is not yet available */
96 TRACE(trace_gdb
, ("sim_open called\n"));
98 if (root_device
!= NULL
)
99 sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
100 root_device
= psim_tree();
103 psim_options(root_device
, argv
+ 1);
105 if (ppc_trace
[trace_opts
])
108 /* fudge our descriptor for now */
114 sim_close (SIM_DESC sd
, int quitting
)
116 TRACE(trace_gdb
, ("sim_close(quitting=%d) called\n", quitting
));
117 if (ppc_trace
[trace_print_info
] && simulator
!= NULL
)
118 psim_print_info (simulator
, ppc_trace
[trace_print_info
]);
123 sim_load (SIM_DESC sd
, char *prog
, bfd
*abfd
, int from_tty
)
125 TRACE(trace_gdb
, ("sim_load(prog=%s, from_tty=%d) called\n",
127 ASSERT(prog
!= NULL
);
129 /* create the simulator */
130 TRACE(trace_gdb
, ("sim_load() - first time, create the simulator\n"));
131 simulator
= psim_create(prog
, root_device
);
133 /* bring in all the data section */
134 psim_init(simulator
);
136 /* get the start address */
139 abfd
= bfd_openr (prog
, 0);
141 error ("psim: can't open \"%s\": %s\n",
142 prog
, bfd_errmsg (bfd_get_error ()));
143 if (!bfd_check_format (abfd
, bfd_object
))
145 const char *errmsg
= bfd_errmsg (bfd_get_error ());
147 error ("psim: \"%s\" is not an object file: %s\n",
158 sim_read (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
160 int result
= psim_read_memory(simulator
, MAX_NR_PROCESSORS
,
162 TRACE(trace_gdb
, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
163 (long)mem
, (long)buf
, length
, result
));
169 sim_write (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
171 int result
= psim_write_memory(simulator
, MAX_NR_PROCESSORS
,
174 TRACE(trace_gdb
, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
175 (long)mem
, (long)buf
, length
, result
));
181 sim_fetch_register (SIM_DESC sd
, int regno
, unsigned char *buf
, int length
)
185 if (simulator
== NULL
) {
189 /* GDB will sometimes ask for the contents of a register named "";
190 we ignore such requests, and leave garbage in *BUF. In GDB
191 terms, the empty string means "the register with this number is
192 not present in the currently selected architecture variant."
193 That's following the kludge we're using for the MIPS processors.
194 But there are loops that just walk through the entire list of
195 names and try to get everything. */
196 regname
= gdbarch_register_name (current_gdbarch
, regno
);
197 if (! regname
|| regname
[0] == '\0')
200 TRACE(trace_gdb
, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
201 regno
, regname
, (long)buf
));
202 return psim_read_register(simulator
, MAX_NR_PROCESSORS
,
203 buf
, regname
, raw_transfer
);
208 sim_store_register (SIM_DESC sd
, int regno
, unsigned char *buf
, int length
)
212 if (simulator
== NULL
)
215 /* See comments in sim_fetch_register, above. */
216 regname
= gdbarch_register_name (current_gdbarch
, regno
);
217 if (! regname
|| regname
[0] == '\0')
220 TRACE(trace_gdb
, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
221 regno
, regname
, (long)buf
));
222 return psim_write_register(simulator
, MAX_NR_PROCESSORS
,
223 buf
, regname
, raw_transfer
);
228 sim_info (SIM_DESC sd
, int verbose
)
230 TRACE(trace_gdb
, ("sim_info(verbose=%d) called\n", verbose
));
231 psim_print_info (simulator
, verbose
);
236 sim_create_inferior (SIM_DESC sd
,
241 unsigned_word entry_point
;
242 TRACE(trace_gdb
, ("sim_create_inferior(start_address=0x%x, ...)\n",
245 if (simulator
== NULL
)
246 error ("No program loaded");
249 entry_point
= bfd_get_start_address (abfd
);
251 entry_point
= 0xfff00000; /* ??? */
253 psim_init(simulator
);
254 psim_stack(simulator
, argv
, envp
);
256 ASSERT (psim_write_register(simulator
, -1 /* all start at same PC */,
257 &entry_point
, "pc", cooked_transfer
) > 0);
263 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason
, int *sigrc
)
265 psim_status status
= psim_get_status(simulator
);
267 switch (status
.reason
) {
269 *reason
= sim_stopped
;
270 if (status
.signal
== 0)
273 *sigrc
= status
.signal
;
276 *reason
= sim_stopped
;
280 *reason
= sim_exited
;
281 *sigrc
= status
.signal
;
284 *reason
= sim_signalled
;
285 *sigrc
= status
.signal
;
289 TRACE(trace_gdb
, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
290 (long)reason
, (long)*reason
, (long)sigrc
, (long)*sigrc
));
295 /* Run (or resume) the program. */
298 sim_stop (SIM_DESC sd
)
300 psim_stop (simulator
);
305 sim_resume (SIM_DESC sd
, int step
, int siggnal
)
307 TRACE(trace_gdb
, ("sim_resume(step=%d, siggnal=%d)\n",
312 psim_step (simulator
);
316 psim_run (simulator
);
321 sim_do_command (SIM_DESC sd
, char *cmd
)
323 TRACE(trace_gdb
, ("sim_do_commands(cmd=%s) called\n",
324 cmd
? cmd
: "(null)"));
326 char **argv
= buildargv(cmd
);
327 psim_command(root_device
, argv
);
333 /* Polling, if required */
336 sim_io_poll_quit (void)
338 if (callbacks
->poll_quit
!= NULL
&& poll_quit_count
-- < 0)
340 poll_quit_count
= POLL_QUIT_INTERVAL
;
341 if (callbacks
->poll_quit (callbacks
))
342 psim_stop (simulator
);
348 /* Map simulator IO operations onto the corresponding GDB I/O
351 NB: Only a limited subset of operations are mapped across. More
352 advanced operations (such as dup or write) must either be mapped to
353 one of the below calls or handled internally */
356 sim_io_read_stdin(char *buf
,
359 switch (CURRENT_STDIO
) {
361 return callbacks
->read_stdin(callbacks
, buf
, sizeof_buf
);
364 return callbacks
->read(callbacks
, 0, buf
, sizeof_buf
);
367 error("sim_io_read_stdin: unaccounted switch\n");
374 sim_io_write_stdout(const char *buf
,
377 switch (CURRENT_STDIO
) {
379 return callbacks
->write_stdout(callbacks
, buf
, sizeof_buf
);
382 return callbacks
->write(callbacks
, 1, buf
, sizeof_buf
);
385 error("sim_io_write_stdout: unaccounted switch\n");
392 sim_io_write_stderr(const char *buf
,
395 switch (CURRENT_STDIO
) {
397 /* NB: I think there should be an explicit write_stderr callback */
398 return callbacks
->write(callbacks
, 3, buf
, sizeof_buf
);
401 return callbacks
->write(callbacks
, 3, buf
, sizeof_buf
);
404 error("sim_io_write_stderr: unaccounted switch\n");
412 sim_io_printf_filtered(const char *fmt
,
417 /* format the message */
419 vsprintf(message
, fmt
, ap
);
422 if (strlen(message
) >= sizeof(message
))
423 error("sim_io_printf_filtered: buffer overflow\n");
424 callbacks
->printf_filtered(callbacks
, "%s", message
);
428 sim_io_flush_stdoutput(void)
430 switch (CURRENT_STDIO
) {
432 callbacks
->flush_stdout (callbacks
);
437 error("sim_io_read_stdin: unaccounted switch\n");
443 sim_io_error (SIM_DESC sd
, const char *fmt
, ...)
447 callbacks
->evprintf_filtered (callbacks
, fmt
, ap
);
449 callbacks
->error (callbacks
, "");
457 void *memory
= (void*)xmalloc(size
);
459 error("xmalloc failed\n");
460 memset(memory
, 0, size
);
464 void zfree(void *data
)