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 */
46 #include "remote-sim.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
;
54 static host_callback
*callbacks
;
56 /* For communication between sim_load and sim_create_inferior.
57 This can be made to go away, please do. */
58 static unsigned_word entry_point
;
61 sim_open (SIM_OPEN_KIND kind
,
62 host_callback
*callback
,
68 /* Note: The simulation is not created by sim_open() because
69 complete information is not yet available */
71 TRACE(trace_gdb
, ("sim_open called\n"));
73 if (root_device
!= NULL
)
74 sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
75 root_device
= psim_tree();
78 psim_options(root_device
, argv
+ 1);
80 if (ppc_trace
[trace_opts
])
83 /* fudge our descriptor for now */
89 sim_close (SIM_DESC sd
, int quitting
)
91 TRACE(trace_gdb
, ("sim_close(quitting=%d) called\n", quitting
));
92 if (ppc_trace
[trace_print_info
] && simulator
!= NULL
)
93 psim_print_info (simulator
, ppc_trace
[trace_print_info
]);
98 sim_load (SIM_DESC sd
, char *prog
, bfd
*abfd
, int from_tty
)
101 TRACE(trace_gdb
, ("sim_load(prog=%s, from_tty=%d) called\n",
103 ASSERT(prog
!= NULL
);
105 /* parse the arguments, assume that the file is argument 0 */
106 argv
= buildargv(prog
);
107 ASSERT(argv
!= NULL
&& argv
[0] != NULL
);
109 /* create the simulator */
110 TRACE(trace_gdb
, ("sim_load() - first time, create the simulator\n"));
111 simulator
= psim_create(argv
[0], root_device
);
113 /* bring in all the data section */
114 psim_init(simulator
);
116 /* release the arguments */
119 /* get the start address */
121 entry_point
= bfd_get_start_address (abfd
);
124 abfd
= bfd_openr (argv
[0], 0);
126 error ("psim: can't open \"%s\": %s\n",
127 argv
[0], bfd_errmsg (bfd_get_error ()));
128 if (!bfd_check_format (abfd
, bfd_object
))
130 const char *errmsg
= bfd_errmsg (bfd_get_error ());
132 error ("psim: \"%s\" is not an object file: %s\n",
135 entry_point
= bfd_get_start_address (abfd
);
144 sim_kill (SIM_DESC sd
)
146 TRACE(trace_gdb
, ("sim_kill(void) called\n"));
147 /* do nothing, nothing to do */
152 sim_read (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
154 int result
= psim_read_memory(simulator
, MAX_NR_PROCESSORS
,
156 TRACE(trace_gdb
, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
157 (long)mem
, (long)buf
, length
, result
));
163 sim_write (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
165 int result
= psim_write_memory(simulator
, MAX_NR_PROCESSORS
,
168 TRACE(trace_gdb
, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
169 (long)mem
, (long)buf
, length
, result
));
175 sim_fetch_register (SIM_DESC sd
, int regno
, unsigned char *buf
)
177 if (simulator
== NULL
) {
180 TRACE(trace_gdb
, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
181 regno
, register_names
[regno
], (long)buf
));
182 psim_read_register(simulator
, MAX_NR_PROCESSORS
,
183 buf
, register_names
[regno
],
189 sim_store_register (SIM_DESC sd
, int regno
, unsigned char *buf
)
191 if (simulator
== NULL
)
193 TRACE(trace_gdb
, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
194 regno
, register_names
[regno
], (long)buf
));
195 psim_write_register(simulator
, MAX_NR_PROCESSORS
,
196 buf
, register_names
[regno
],
202 sim_info (SIM_DESC sd
, int verbose
)
204 TRACE(trace_gdb
, ("sim_info(verbose=%d) called\n", verbose
));
205 psim_print_info (simulator
, verbose
);
210 sim_create_inferior (SIM_DESC sd
, char **argv
, char **envp
)
212 TRACE(trace_gdb
, ("sim_create_inferior(start_address=0x%x, ...)\n",
215 psim_init(simulator
);
216 psim_stack(simulator
, argv
, envp
);
218 psim_write_register(simulator
, -1 /* all start at same PC */,
219 &entry_point
, "pc", cooked_transfer
);
225 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason
, int *sigrc
)
227 psim_status status
= psim_get_status(simulator
);
229 switch (status
.reason
) {
231 *reason
= sim_stopped
;
232 if (status
.signal
== 0)
235 *sigrc
= status
.signal
;
238 *reason
= sim_stopped
;
242 *reason
= sim_exited
;
243 *sigrc
= status
.signal
;
246 *reason
= sim_signalled
;
247 *sigrc
= status
.signal
;
251 TRACE(trace_gdb
, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
252 (long)reason
, (long)*reason
, (long)sigrc
, (long)*sigrc
));
257 /* Run (or resume) the program. */
260 sim_stop (SIM_DESC sd
)
262 psim_stop (simulator
);
267 sim_resume (SIM_DESC sd
, int step
, int siggnal
)
269 TRACE(trace_gdb
, ("sim_resume(step=%d, siggnal=%d)\n",
274 psim_step (simulator
);
278 psim_run (simulator
);
283 sim_do_command (SIM_DESC sd
, char *cmd
)
285 TRACE(trace_gdb
, ("sim_do_commands(cmd=%s) called\n",
286 cmd
? cmd
: "(null)"));
288 char **argv
= buildargv(cmd
);
289 psim_command(root_device
, argv
);
295 /* Map simulator IO operations onto the corresponding GDB I/O
298 NB: Only a limited subset of operations are mapped across. More
299 advanced operations (such as dup or write) must either be mapped to
300 one of the below calls or handled internally */
303 sim_io_read_stdin(char *buf
,
306 switch (CURRENT_STDIO
) {
308 return callbacks
->read_stdin(callbacks
, buf
, sizeof_buf
);
311 return callbacks
->read(callbacks
, 0, buf
, sizeof_buf
);
314 error("sim_io_read_stdin: unaccounted switch\n");
321 sim_io_write_stdout(const char *buf
,
324 switch (CURRENT_STDIO
) {
326 return callbacks
->write_stdout(callbacks
, buf
, sizeof_buf
);
329 return callbacks
->write(callbacks
, 1, buf
, sizeof_buf
);
332 error("sim_io_write_stdout: unaccounted switch\n");
339 sim_io_write_stderr(const char *buf
,
342 switch (CURRENT_STDIO
) {
344 /* NB: I think there should be an explicit write_stderr callback */
345 return callbacks
->write(callbacks
, 3, buf
, sizeof_buf
);
348 return callbacks
->write(callbacks
, 3, buf
, sizeof_buf
);
351 error("sim_io_write_stderr: unaccounted switch\n");
359 sim_io_printf_filtered(const char *fmt
,
364 /* format the message */
366 vsprintf(message
, fmt
, ap
);
369 if (strlen(message
) >= sizeof(message
))
370 error("sim_io_printf_filtered: buffer overflow\n");
371 callbacks
->printf_filtered(callbacks
, "%s", message
);
375 sim_io_flush_stdoutput(void)
377 switch (CURRENT_STDIO
) {
379 gdb_flush (gdb_stdout
);
384 error("sim_io_read_stdin: unaccounted switch\n");
394 void *memory
= (void*)xmalloc(size
);
396 error("xmalloc failed\n");
397 memset(memory
, 0, size
);
401 void zfree(void *data
)