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 */
45 #include "remote-sim.h"
48 /* Structures used by the simulator, for gdb just have static structures */
50 static psim
*simulator
;
51 static device
*root_device
;
52 static const char *register_names
[] = REGISTER_NAMES
;
57 /* Note: The simulation is not created by sim_open() because
58 complete information is not yet available */
60 TRACE(trace_gdb
, ("sim_open(args=%s) called\n", args
? args
: "(null)"));
62 if (root_device
!= NULL
)
63 sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
64 root_device
= psim_tree();
68 char **argv
= buildargv(args
);
69 psim_options(root_device
, argv
);
73 if (ppc_trace
[trace_opts
])
79 sim_close (int quitting
)
81 TRACE(trace_gdb
, ("sim_close(quitting=%d) called\n", quitting
));
82 if (ppc_trace
[trace_print_info
] && simulator
!= NULL
)
83 psim_print_info (simulator
, ppc_trace
[trace_print_info
]);
88 sim_load (char *prog
, int from_tty
)
91 TRACE(trace_gdb
, ("sim_load(prog=%s, from_tty=%d) called\n",
95 /* parse the arguments, assume that the file is argument 0 */
96 argv
= buildargv(prog
);
97 ASSERT(argv
!= NULL
&& argv
[0] != NULL
);
99 /* create the simulator */
100 TRACE(trace_gdb
, ("sim_load() - first time, create the simulator\n"));
101 simulator
= psim_create(argv
[0], root_device
);
103 /* bring in all the data section */
104 psim_init(simulator
);
106 /* release the arguments */
109 /* `I did it my way' */
117 TRACE(trace_gdb
, ("sim_kill(void) called\n"));
118 /* do nothing, nothing to do */
123 sim_read (SIM_ADDR mem
, unsigned char *buf
, int length
)
125 int result
= psim_read_memory(simulator
, MAX_NR_PROCESSORS
,
127 TRACE(trace_gdb
, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
128 (long)mem
, (long)buf
, length
, result
));
134 sim_write (SIM_ADDR mem
, unsigned char *buf
, int length
)
136 int result
= psim_write_memory(simulator
, MAX_NR_PROCESSORS
,
139 TRACE(trace_gdb
, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
140 (long)mem
, (long)buf
, length
, result
));
146 sim_fetch_register (int regno
, unsigned char *buf
)
148 if (simulator
== NULL
) {
151 TRACE(trace_gdb
, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
152 regno
, register_names
[regno
], (long)buf
));
153 psim_read_register(simulator
, MAX_NR_PROCESSORS
,
154 buf
, register_names
[regno
],
160 sim_store_register (int regno
, unsigned char *buf
)
162 if (simulator
== NULL
)
164 TRACE(trace_gdb
, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
165 regno
, register_names
[regno
], (long)buf
));
166 psim_write_register(simulator
, MAX_NR_PROCESSORS
,
167 buf
, register_names
[regno
],
173 sim_info (int verbose
)
175 TRACE(trace_gdb
, ("sim_info(verbose=%d) called\n", verbose
));
176 psim_print_info (simulator
, verbose
);
181 sim_create_inferior (SIM_ADDR start_address
, char **argv
, char **envp
)
183 unsigned_word entry_point
= start_address
;
185 TRACE(trace_gdb
, ("sim_create_inferior(start_address=0x%x, ...)\n",
188 psim_init(simulator
);
189 psim_stack(simulator
, argv
, envp
);
191 psim_write_register(simulator
, -1 /* all start at same PC */,
192 &entry_point
, "pc", cooked_transfer
);
196 static volatile int sim_should_run
;
199 sim_stop_reason (enum sim_stop
*reason
, int *sigrc
)
201 psim_status status
= psim_get_status(simulator
);
203 switch (CURRENT_ENVIRONMENT
) {
205 case USER_ENVIRONMENT
:
206 case VIRTUAL_ENVIRONMENT
:
207 switch (status
.reason
) {
209 *reason
= sim_stopped
;
211 if (sim_should_run
) {
212 error("sim_stop_reason() unknown reason for halt\n");
216 *reason
= sim_stopped
;
220 *reason
= sim_exited
;
224 *reason
= sim_signalled
;
225 *sigrc
= status
.signal
;
230 case OPERATING_ENVIRONMENT
:
231 *reason
= sim_stopped
;
236 error("sim_stop_reason() - unknown environment\n");
240 TRACE(trace_gdb
, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
241 (long)reason
, (long)*reason
, (long)sigrc
, (long)*sigrc
));
246 /* Run (or resume) the program. */
254 sim_resume (int step
, int siggnal
)
256 TRACE(trace_gdb
, ("sim_resume(step=%d, siggnal=%d)\n",
261 psim_step(simulator
);
262 /* sim_stop_reason has a sanity check for stopping while
263 was_continuing. We don't want that here so reset sim_should_run. */
270 prev
= signal(SIGINT
, sim_ctrl_c
);
272 psim_run_until_stop(simulator
, &sim_should_run
);
273 signal(SIGINT
, prev
);
278 sim_do_command (char *cmd
)
280 TRACE(trace_gdb
, ("sim_do_commands(cmd=%s) called\n",
281 cmd
? cmd
: "(null)"));
283 char **argv
= buildargv(cmd
);
284 psim_command(root_device
, argv
);
290 /* Map simulator IO operations onto the corresponding GDB I/O
293 NB: Only a limited subset of operations are mapped across. More
294 advanced operations (such as dup or write) must either be mapped to
295 one of the below calls or handled internally */
297 static host_callback
*callbacks
;
300 sim_io_read_stdin(char *buf
,
303 switch (CURRENT_STDIO
) {
305 return callbacks
->read_stdin(callbacks
, buf
, sizeof_buf
);
308 return callbacks
->read(callbacks
, 0, buf
, sizeof_buf
);
311 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");
336 sim_io_write_stderr(const char *buf
,
339 switch (CURRENT_STDIO
) {
341 /* NB: I think there should be an explicit write_stderr callback */
342 return callbacks
->write(callbacks
, 3, buf
, sizeof_buf
);
345 return callbacks
->write(callbacks
, 3, buf
, sizeof_buf
);
348 error("sim_io_write_stderr: unaccounted switch\n");
356 sim_io_printf_filtered(const char *fmt
,
361 /* format the message */
363 vsprintf(message
, fmt
, ap
);
366 if (strlen(message
) >= sizeof(message
))
367 error("sim_io_printf_filtered: buffer overflow\n");
368 callbacks
->printf_filtered(callbacks
, "%s", message
);
372 sim_io_flush_stdoutput(void)
374 switch (CURRENT_STDIO
) {
376 gdb_flush (gdb_stdout
);
381 error("sim_io_read_stdin: unaccounted switch\n");
387 sim_set_callbacks (host_callback
*callback
)
389 callbacks
= callback
;
390 TRACE(trace_gdb
, ("sim_set_callbacks called\n"));
398 void *memory
= (void*)xmalloc(size
);
400 error("xmalloc failed\n");
401 memset(memory
, 0, size
);
405 void zfree(void *data
)