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 */
43 #include "libiberty.h"
45 #include "gdb/callback.h"
46 #include "gdb/remote-sim.h"
47 #include "gdb/signals.h"
49 /* Define the rate at which the simulator should poll the host
51 #ifndef POLL_QUIT_INTERVAL
52 #define POLL_QUIT_INTERVAL 0x20
55 static int poll_quit_count = POLL_QUIT_INTERVAL;
57 /* Structures used by the simulator, for gdb just have static structures */
60 static device *root_device;
61 static host_callback *callbacks;
64 sim_open (SIM_OPEN_KIND kind,
65 host_callback *callback,
71 /* Note: The simulation is not created by sim_open() because
72 complete information is not yet available */
74 TRACE(trace_gdb, ("sim_open called\n"));
76 if (root_device != NULL)
77 sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
78 root_device = psim_tree();
81 psim_options(root_device, argv + 1);
83 if (ppc_trace[trace_opts])
86 /* fudge our descriptor for now */
92 sim_close (SIM_DESC sd, int quitting)
94 TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
95 if (ppc_trace[trace_print_info] && simulator != NULL)
96 psim_print_info (simulator, ppc_trace[trace_print_info]);
101 sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
103 TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
105 ASSERT(prog != NULL);
107 /* create the simulator */
108 TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
109 simulator = psim_create(prog, root_device);
111 /* bring in all the data section */
112 psim_init(simulator);
114 /* get the start address */
117 abfd = bfd_openr (prog, 0);
119 error ("psim: can't open \"%s\": %s\n",
120 prog, bfd_errmsg (bfd_get_error ()));
121 if (!bfd_check_format (abfd, bfd_object))
123 const char *errmsg = bfd_errmsg (bfd_get_error ());
125 error ("psim: \"%s\" is not an object file: %s\n",
136 sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
138 int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
140 TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
141 (long)mem, (long)buf, length, result));
147 sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
149 int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
152 TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
153 (long)mem, (long)buf, length, result));
158 sim_info (SIM_DESC sd, int verbose)
160 TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
161 psim_print_info (simulator, verbose);
166 sim_create_inferior (SIM_DESC sd,
171 unsigned_word entry_point;
172 TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
175 if (simulator == NULL)
176 error ("No program loaded");
179 entry_point = bfd_get_start_address (abfd);
181 entry_point = 0xfff00000; /* ??? */
183 psim_init(simulator);
184 psim_stack(simulator, argv, envp);
186 ASSERT (psim_write_register(simulator, -1 /* all start at same PC */,
187 &entry_point, "pc", cooked_transfer) > 0);
193 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
195 psim_status status = psim_get_status(simulator);
197 switch (status.reason) {
199 *reason = sim_stopped;
200 if (status.signal == 0)
201 *sigrc = TARGET_SIGNAL_TRAP;
203 *sigrc = status.signal;
206 *reason = sim_stopped;
207 *sigrc = TARGET_SIGNAL_TRAP;
210 *reason = sim_exited;
211 *sigrc = status.signal;
214 *reason = sim_signalled;
215 *sigrc = status.signal;
219 TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
220 (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
225 /* Run (or resume) the program. */
228 sim_stop (SIM_DESC sd)
230 psim_stop (simulator);
235 sim_resume (SIM_DESC sd, int step, int siggnal)
237 TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
242 psim_step (simulator);
246 psim_run (simulator);
251 sim_do_command (SIM_DESC sd, char *cmd)
253 TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
254 cmd ? cmd : "(null)"));
256 char **argv = buildargv(cmd);
257 psim_command(root_device, argv);
263 /* Polling, if required */
266 sim_io_poll_quit (void)
268 if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
270 poll_quit_count = POLL_QUIT_INTERVAL;
271 if (callbacks->poll_quit (callbacks))
272 psim_stop (simulator);
278 /* Map simulator IO operations onto the corresponding GDB I/O
281 NB: Only a limited subset of operations are mapped across. More
282 advanced operations (such as dup or write) must either be mapped to
283 one of the below calls or handled internally */
286 sim_io_read_stdin(char *buf,
289 switch (CURRENT_STDIO) {
291 return callbacks->read_stdin(callbacks, buf, sizeof_buf);
294 return callbacks->read(callbacks, 0, buf, sizeof_buf);
297 error("sim_io_read_stdin: unaccounted switch\n");
304 sim_io_write_stdout(const char *buf,
307 switch (CURRENT_STDIO) {
309 return callbacks->write_stdout(callbacks, buf, sizeof_buf);
312 return callbacks->write(callbacks, 1, buf, sizeof_buf);
315 error("sim_io_write_stdout: unaccounted switch\n");
322 sim_io_write_stderr(const char *buf,
325 switch (CURRENT_STDIO) {
327 /* NB: I think there should be an explicit write_stderr callback */
328 return callbacks->write(callbacks, 3, buf, sizeof_buf);
331 return callbacks->write(callbacks, 3, buf, sizeof_buf);
334 error("sim_io_write_stderr: unaccounted switch\n");
342 sim_io_printf_filtered(const char *fmt,
347 /* format the message */
349 vsprintf(message, fmt, ap);
352 if (strlen(message) >= sizeof(message))
353 error("sim_io_printf_filtered: buffer overflow\n");
354 callbacks->printf_filtered(callbacks, "%s", message);
358 sim_io_flush_stdoutput(void)
360 switch (CURRENT_STDIO) {
362 callbacks->flush_stdout (callbacks);
367 error("sim_io_read_stdin: unaccounted switch\n");
373 sim_io_error (SIM_DESC sd, const char *fmt, ...)
377 callbacks->evprintf_filtered (callbacks, fmt, ap);
379 callbacks->error (callbacks, "");
387 void *memory = (void*)xmalloc(size);
389 error("xmalloc failed\n");
390 memset(memory, 0, size);
394 void zfree(void *data)