1 /* This file is part of the program psim.
3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
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 "../../gdb/defs.h"
45 #include "../../gdb/remote-sim.h"
46 #include "../../gdb/callback.h"
49 /* Structures used by the simulator, for gdb just have static structures */
51 static psim
*simulator
;
52 static device
*root_device
;
53 static const char *register_names
[] = REGISTER_NAMES
;
58 /* Note: The simulation is not created by sim_open() because
59 complete information is not yet available */
61 TRACE(trace_gdb
, ("sim_open(args=%s) called\n", args
? args
: "(null)"));
63 if (root_device
!= NULL
)
64 sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
65 root_device
= psim_tree();
69 char **argv
= buildargv(args
);
70 psim_options(root_device
, argv
);
74 if (ppc_trace
[trace_opts
])
80 sim_close (int quitting
)
82 TRACE(trace_gdb
, ("sim_close(quitting=%d) called\n", quitting
));
83 if (ppc_trace
[trace_print_info
] && simulator
!= NULL
)
84 psim_print_info (simulator
, ppc_trace
[trace_print_info
]);
89 sim_load (char *prog
, int from_tty
)
92 TRACE(trace_gdb
, ("sim_load(prog=%s, from_tty=%d) called\n",
96 /* parse the arguments, assume that the file is argument 0 */
97 argv
= buildargv(prog
);
98 ASSERT(argv
!= NULL
&& argv
[0] != NULL
);
100 /* create the simulator */
101 TRACE(trace_gdb
, ("sim_load() - first time, create the simulator\n"));
102 simulator
= psim_create(argv
[0], root_device
);
104 /* bring in all the data section */
105 psim_init(simulator
);
107 /* release the arguments */
110 /* `I did it my way' */
118 TRACE(trace_gdb
, ("sim_kill(void) called\n"));
119 /* do nothing, nothing to do */
124 sim_read (SIM_ADDR mem
, unsigned char *buf
, int length
)
126 int result
= psim_read_memory(simulator
, MAX_NR_PROCESSORS
,
128 TRACE(trace_gdb
, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
129 (long)mem
, (long)buf
, length
, result
));
135 sim_write (SIM_ADDR mem
, unsigned char *buf
, int length
)
137 int result
= psim_write_memory(simulator
, MAX_NR_PROCESSORS
,
140 TRACE(trace_gdb
, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
141 (long)mem
, (long)buf
, length
, result
));
147 sim_fetch_register (int regno
, unsigned char *buf
)
149 if (simulator
== NULL
) {
152 TRACE(trace_gdb
, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
153 regno
, register_names
[regno
], (long)buf
));
154 psim_read_register(simulator
, MAX_NR_PROCESSORS
,
155 buf
, register_names
[regno
],
161 sim_store_register (int regno
, unsigned char *buf
)
163 if (simulator
== NULL
)
165 TRACE(trace_gdb
, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
166 regno
, register_names
[regno
], (long)buf
));
167 psim_write_register(simulator
, MAX_NR_PROCESSORS
,
168 buf
, register_names
[regno
],
174 sim_info (int verbose
)
176 TRACE(trace_gdb
, ("sim_info(verbose=%d) called\n", verbose
));
177 psim_print_info (simulator
, verbose
);
182 sim_create_inferior (SIM_ADDR start_address
, char **argv
, char **envp
)
184 unsigned_word entry_point
= start_address
;
186 TRACE(trace_gdb
, ("sim_create_inferior(start_address=0x%x, ...)\n",
189 psim_init(simulator
);
190 psim_stack(simulator
, argv
, envp
);
192 psim_write_register(simulator
, -1 /* all start at same PC */,
193 &entry_point
, "pc", cooked_transfer
);
197 static volatile int sim_should_run
;
200 sim_stop_reason (enum sim_stop
*reason
, int *sigrc
)
202 psim_status status
= psim_get_status(simulator
);
204 switch (CURRENT_ENVIRONMENT
) {
206 case USER_ENVIRONMENT
:
207 case VIRTUAL_ENVIRONMENT
:
208 switch (status
.reason
) {
210 *reason
= sim_stopped
;
212 if (sim_should_run
) {
213 error("sim_stop_reason() unknown reason for halt\n");
217 *reason
= sim_stopped
;
221 *reason
= sim_exited
;
225 *reason
= sim_signalled
;
226 *sigrc
= status
.signal
;
231 case OPERATING_ENVIRONMENT
:
232 *reason
= sim_stopped
;
237 error("sim_stop_reason() - unknown environment\n");
241 TRACE(trace_gdb
, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
242 (long)reason
, (long)*reason
, (long)sigrc
, (long)*sigrc
));
247 /* Run (or resume) the program. */
255 sim_resume (int step
, int siggnal
)
257 TRACE(trace_gdb
, ("sim_resume(step=%d, siggnal=%d)\n",
262 psim_step(simulator
);
263 /* sim_stop_reason has a sanity check for stopping while
264 was_continuing. We don't want that here so reset sim_should_run. */
271 prev
= signal(SIGINT
, sim_ctrl_c
);
273 psim_run_until_stop(simulator
, &sim_should_run
);
274 signal(SIGINT
, prev
);
279 sim_do_command (char *cmd
)
281 TRACE(trace_gdb
, ("sim_do_commands(cmd=%s) called\n",
282 cmd
? cmd
: "(null)"));
284 char **argv
= buildargv(cmd
);
285 psim_options(root_device
, argv
);
291 /* Map simulator IO operations onto the corresponding GDB I/O
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 */
298 static host_callback
*callbacks
;
301 sim_io_read_stdin(char *buf
,
304 switch (CURRENT_STDIO
) {
306 return callbacks
->read_stdin(callbacks
, buf
, sizeof_buf
);
309 return callbacks
->read(callbacks
, 0, buf
, sizeof_buf
);
312 error("sim_io_read_stdin: unaccounted switch\n");
318 sim_io_write_stdout(const char *buf
,
321 switch (CURRENT_STDIO
) {
323 return callbacks
->write_stdout(callbacks
, buf
, sizeof_buf
);
326 return callbacks
->write(callbacks
, 1, buf
, sizeof_buf
);
329 error("sim_io_write_stdout: unaccounted switch\n");
335 sim_io_write_stderr(const char *buf
,
338 switch (CURRENT_STDIO
) {
340 /* NB: I think there should be an explicit write_stderr callback */
341 return callbacks
->write(callbacks
, 3, buf
, sizeof_buf
);
344 return callbacks
->write(callbacks
, 3, buf
, sizeof_buf
);
347 error("sim_io_write_stderr: unaccounted switch\n");
354 sim_io_printf_filtered(const char *fmt
,
359 /* format the message */
361 vsprintf(message
, fmt
, ap
);
364 if (strlen(message
) >= sizeof(message
))
365 error("sim_io_printf_filtered: buffer overflow\n");
366 callbacks
->printf_filtered(callbacks
, "%s", message
);
370 sim_io_flush_stdoutput(void)
372 switch (CURRENT_STDIO
) {
374 gdb_flush (gdb_stdout
);
379 error("sim_io_read_stdin: unaccounted switch\n");
385 sim_set_callbacks (host_callback
*callback
)
387 callbacks
= callback
;
388 TRACE(trace_gdb
, ("sim_set_callbacks called\n"));
396 void *memory
= (void*)xmalloc(size
);
398 error("xmalloc failed\n");
399 memset(memory
, 0, size
);
403 void zfree(void *data
)