Commit | Line | Data |
---|---|---|
8eab189b MM |
1 | /* This file is part of the program psim. |
2 | ||
30c87b55 | 3 | Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au> |
8eab189b MM |
4 | |
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. | |
9 | ||
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. | |
14 | ||
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. | |
18 | ||
19 | */ | |
20 | ||
21 | ||
22 | #include <signal.h> /* FIXME - should be machine dependant version */ | |
23 | #include <stdarg.h> | |
8eab189b MM |
24 | #include <ctype.h> |
25 | ||
8eab189b | 26 | #include "psim.h" |
28816f45 | 27 | #include "options.h" |
8eab189b | 28 | |
939b233a DE |
29 | #undef printf_filtered /* blow away the mapping */ |
30 | ||
c494cadd MM |
31 | #ifdef HAVE_STDLIB_H |
32 | #include <stdlib.h> | |
33 | #endif | |
34 | ||
35 | #ifdef HAVE_STRING_H | |
36 | #include <string.h> | |
37 | #else | |
38 | #ifdef HAVE_STRINGS_H | |
39 | #include <strings.h> | |
40 | #endif | |
41 | #endif | |
42 | ||
5c04f4f7 | 43 | #include "defs.h" |
ed119303 | 44 | #include "bfd.h" |
5c04f4f7 MM |
45 | #include "callback.h" |
46 | #include "remote-sim.h" | |
8eab189b MM |
47 | |
48 | ||
49 | /* Structures used by the simulator, for gdb just have static structures */ | |
50 | ||
51 | static psim *simulator; | |
30c87b55 MM |
52 | static device *root_device; |
53 | static const char *register_names[] = REGISTER_NAMES; | |
247fccde | 54 | static host_callback *callbacks; |
8eab189b | 55 | |
ed119303 DE |
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; | |
59 | ||
60 | SIM_DESC | |
247fccde AC |
61 | sim_open (SIM_OPEN_KIND kind, |
62 | host_callback *callback, | |
63 | struct _bfd *abfd, | |
64 | char **argv) | |
8eab189b | 65 | { |
ff82f214 AC |
66 | callbacks = callback; |
67 | ||
30c87b55 MM |
68 | /* Note: The simulation is not created by sim_open() because |
69 | complete information is not yet available */ | |
8eab189b | 70 | /* trace the call */ |
ed119303 | 71 | TRACE(trace_gdb, ("sim_open called\n")); |
8eab189b | 72 | |
30c87b55 | 73 | if (root_device != NULL) |
939b233a | 74 | sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n"); |
30c87b55 MM |
75 | root_device = psim_tree(); |
76 | simulator = NULL; | |
77 | ||
ed119303 | 78 | psim_options(root_device, argv + 1); |
8eab189b | 79 | |
28816f45 MM |
80 | if (ppc_trace[trace_opts]) |
81 | print_options (); | |
ed119303 DE |
82 | |
83 | /* fudge our descriptor for now */ | |
84 | return (SIM_DESC) 1; | |
8eab189b MM |
85 | } |
86 | ||
87 | ||
88 | void | |
ed119303 | 89 | sim_close (SIM_DESC sd, int quitting) |
8eab189b MM |
90 | { |
91 | TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting)); | |
30c87b55 MM |
92 | if (ppc_trace[trace_print_info] && simulator != NULL) |
93 | psim_print_info (simulator, ppc_trace[trace_print_info]); | |
8eab189b MM |
94 | } |
95 | ||
96 | ||
ed119303 DE |
97 | SIM_RC |
98 | sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty) | |
8eab189b | 99 | { |
a983c8f0 | 100 | char **argv; |
8eab189b MM |
101 | TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n", |
102 | prog, from_tty)); | |
a983c8f0 | 103 | ASSERT(prog != NULL); |
8eab189b | 104 | |
a983c8f0 MM |
105 | /* parse the arguments, assume that the file is argument 0 */ |
106 | argv = buildargv(prog); | |
107 | ASSERT(argv != NULL && argv[0] != NULL); | |
8eab189b MM |
108 | |
109 | /* create the simulator */ | |
110 | TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n")); | |
30c87b55 | 111 | simulator = psim_create(argv[0], root_device); |
8eab189b MM |
112 | |
113 | /* bring in all the data section */ | |
a983c8f0 | 114 | psim_init(simulator); |
8eab189b | 115 | |
a983c8f0 MM |
116 | /* release the arguments */ |
117 | freeargv(argv); | |
118 | ||
ed119303 DE |
119 | /* get the start address */ |
120 | if (abfd != NULL) | |
121 | entry_point = bfd_get_start_address (abfd); | |
122 | else | |
123 | { | |
124 | abfd = bfd_openr (argv[0], 0); | |
125 | if (abfd == NULL) | |
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)) | |
129 | { | |
130 | const char *errmsg = bfd_errmsg (bfd_get_error ()); | |
131 | bfd_close (abfd); | |
132 | error ("psim: \"%s\" is not an object file: %s\n", | |
133 | argv[0], errmsg); | |
134 | } | |
135 | entry_point = bfd_get_start_address (abfd); | |
136 | bfd_close (abfd); | |
137 | } | |
138 | ||
139 | return SIM_RC_OK; | |
8eab189b MM |
140 | } |
141 | ||
142 | ||
143 | void | |
ed119303 | 144 | sim_kill (SIM_DESC sd) |
8eab189b MM |
145 | { |
146 | TRACE(trace_gdb, ("sim_kill(void) called\n")); | |
147 | /* do nothing, nothing to do */ | |
148 | } | |
149 | ||
150 | ||
151 | int | |
ed119303 | 152 | sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length) |
8eab189b | 153 | { |
a983c8f0 MM |
154 | int result = psim_read_memory(simulator, MAX_NR_PROCESSORS, |
155 | buf, mem, length); | |
28816f45 MM |
156 | TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n", |
157 | (long)mem, (long)buf, length, result)); | |
a983c8f0 | 158 | return result; |
8eab189b MM |
159 | } |
160 | ||
161 | ||
162 | int | |
ed119303 | 163 | sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length) |
8eab189b | 164 | { |
a983c8f0 MM |
165 | int result = psim_write_memory(simulator, MAX_NR_PROCESSORS, |
166 | buf, mem, length, | |
167 | 1/*violate_ro*/); | |
28816f45 MM |
168 | TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n", |
169 | (long)mem, (long)buf, length, result)); | |
a983c8f0 | 170 | return result; |
8eab189b MM |
171 | } |
172 | ||
173 | ||
174 | void | |
ed119303 | 175 | sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf) |
8eab189b MM |
176 | { |
177 | if (simulator == NULL) { | |
178 | return; | |
179 | } | |
28816f45 MM |
180 | TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n", |
181 | regno, register_names[regno], (long)buf)); | |
a983c8f0 MM |
182 | psim_read_register(simulator, MAX_NR_PROCESSORS, |
183 | buf, register_names[regno], | |
8eab189b MM |
184 | raw_transfer); |
185 | } | |
186 | ||
187 | ||
188 | void | |
ed119303 | 189 | sim_store_register (SIM_DESC sd, int regno, unsigned char *buf) |
8eab189b MM |
190 | { |
191 | if (simulator == NULL) | |
192 | return; | |
28816f45 MM |
193 | TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n", |
194 | regno, register_names[regno], (long)buf)); | |
a983c8f0 MM |
195 | psim_write_register(simulator, MAX_NR_PROCESSORS, |
196 | buf, register_names[regno], | |
8eab189b MM |
197 | raw_transfer); |
198 | } | |
199 | ||
200 | ||
201 | void | |
ed119303 | 202 | sim_info (SIM_DESC sd, int verbose) |
8eab189b MM |
203 | { |
204 | TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose)); | |
83d96c6e | 205 | psim_print_info (simulator, verbose); |
8eab189b MM |
206 | } |
207 | ||
208 | ||
ed119303 DE |
209 | SIM_RC |
210 | sim_create_inferior (SIM_DESC sd, char **argv, char **envp) | |
8eab189b | 211 | { |
8eab189b | 212 | TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n", |
ed119303 | 213 | entry_point)); |
8eab189b | 214 | |
a983c8f0 | 215 | psim_init(simulator); |
8eab189b MM |
216 | psim_stack(simulator, argv, envp); |
217 | ||
218 | psim_write_register(simulator, -1 /* all start at same PC */, | |
219 | &entry_point, "pc", cooked_transfer); | |
ed119303 | 220 | return SIM_RC_OK; |
8eab189b MM |
221 | } |
222 | ||
223 | ||
8eab189b | 224 | void |
ed119303 | 225 | sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc) |
8eab189b MM |
226 | { |
227 | psim_status status = psim_get_status(simulator); | |
228 | ||
ff82f214 AC |
229 | switch (status.reason) { |
230 | case was_continuing: | |
231 | *reason = sim_stopped; | |
232 | if (status.signal == 0) | |
8eab189b | 233 | *sigrc = SIGTRAP; |
ff82f214 | 234 | else |
8eab189b | 235 | *sigrc = status.signal; |
8eab189b | 236 | break; |
ff82f214 | 237 | case was_trap: |
8eab189b MM |
238 | *reason = sim_stopped; |
239 | *sigrc = SIGTRAP; | |
240 | break; | |
ff82f214 AC |
241 | case was_exited: |
242 | *reason = sim_exited; | |
243 | *sigrc = status.signal; | |
244 | break; | |
245 | case was_signalled: | |
246 | *reason = sim_signalled; | |
247 | *sigrc = status.signal; | |
248 | break; | |
8eab189b | 249 | } |
a983c8f0 | 250 | |
28816f45 MM |
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)); | |
8eab189b MM |
253 | } |
254 | ||
255 | ||
256 | ||
257 | /* Run (or resume) the program. */ | |
ff82f214 AC |
258 | |
259 | int | |
260 | sim_stop (SIM_DESC sd) | |
8eab189b | 261 | { |
ff82f214 AC |
262 | psim_stop (simulator); |
263 | return 1; | |
8eab189b MM |
264 | } |
265 | ||
266 | void | |
ed119303 | 267 | sim_resume (SIM_DESC sd, int step, int siggnal) |
8eab189b | 268 | { |
a983c8f0 MM |
269 | TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n", |
270 | step, siggnal)); | |
271 | ||
8eab189b | 272 | if (step) |
939b233a | 273 | { |
ff82f214 | 274 | psim_step (simulator); |
939b233a | 275 | } |
8eab189b | 276 | else |
939b233a | 277 | { |
ff82f214 | 278 | psim_run (simulator); |
939b233a | 279 | } |
8eab189b MM |
280 | } |
281 | ||
282 | void | |
ed119303 | 283 | sim_do_command (SIM_DESC sd, char *cmd) |
8eab189b | 284 | { |
30c87b55 MM |
285 | TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n", |
286 | cmd ? cmd : "(null)")); | |
5c04f4f7 | 287 | if (cmd != NULL) { |
30c87b55 | 288 | char **argv = buildargv(cmd); |
5c04f4f7 | 289 | psim_command(root_device, argv); |
30c87b55 MM |
290 | freeargv(argv); |
291 | } | |
8eab189b MM |
292 | } |
293 | ||
939b233a DE |
294 | |
295 | /* Map simulator IO operations onto the corresponding GDB I/O | |
296 | functions. | |
297 | ||
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 */ | |
301 | ||
939b233a DE |
302 | int |
303 | sim_io_read_stdin(char *buf, | |
304 | int sizeof_buf) | |
305 | { | |
306 | switch (CURRENT_STDIO) { | |
307 | case DO_USE_STDIO: | |
308 | return callbacks->read_stdin(callbacks, buf, sizeof_buf); | |
309 | break; | |
310 | case DONT_USE_STDIO: | |
311 | return callbacks->read(callbacks, 0, buf, sizeof_buf); | |
312 | break; | |
313 | default: | |
314 | error("sim_io_read_stdin: unaccounted switch\n"); | |
315 | break; | |
316 | } | |
5c04f4f7 | 317 | return 0; |
939b233a DE |
318 | } |
319 | ||
320 | int | |
321 | sim_io_write_stdout(const char *buf, | |
322 | int sizeof_buf) | |
323 | { | |
324 | switch (CURRENT_STDIO) { | |
325 | case DO_USE_STDIO: | |
326 | return callbacks->write_stdout(callbacks, buf, sizeof_buf); | |
327 | break; | |
328 | case DONT_USE_STDIO: | |
329 | return callbacks->write(callbacks, 1, buf, sizeof_buf); | |
330 | break; | |
331 | default: | |
332 | error("sim_io_write_stdout: unaccounted switch\n"); | |
333 | break; | |
334 | } | |
5c04f4f7 | 335 | return 0; |
939b233a DE |
336 | } |
337 | ||
338 | int | |
339 | sim_io_write_stderr(const char *buf, | |
340 | int sizeof_buf) | |
341 | { | |
342 | switch (CURRENT_STDIO) { | |
343 | case DO_USE_STDIO: | |
344 | /* NB: I think there should be an explicit write_stderr callback */ | |
345 | return callbacks->write(callbacks, 3, buf, sizeof_buf); | |
346 | break; | |
347 | case DONT_USE_STDIO: | |
348 | return callbacks->write(callbacks, 3, buf, sizeof_buf); | |
349 | break; | |
350 | default: | |
351 | error("sim_io_write_stderr: unaccounted switch\n"); | |
352 | break; | |
353 | } | |
5c04f4f7 | 354 | return 0; |
939b233a DE |
355 | } |
356 | ||
357 | ||
358 | void | |
359 | sim_io_printf_filtered(const char *fmt, | |
360 | ...) | |
361 | { | |
362 | char message[1024]; | |
363 | va_list ap; | |
364 | /* format the message */ | |
365 | va_start(ap, fmt); | |
366 | vsprintf(message, fmt, ap); | |
367 | va_end(ap); | |
368 | /* sanity check */ | |
369 | if (strlen(message) >= sizeof(message)) | |
370 | error("sim_io_printf_filtered: buffer overflow\n"); | |
371 | callbacks->printf_filtered(callbacks, "%s", message); | |
372 | } | |
373 | ||
374 | void | |
375 | sim_io_flush_stdoutput(void) | |
376 | { | |
377 | switch (CURRENT_STDIO) { | |
378 | case DO_USE_STDIO: | |
379 | gdb_flush (gdb_stdout); | |
380 | break; | |
381 | case DONT_USE_STDIO: | |
382 | break; | |
383 | default: | |
384 | error("sim_io_read_stdin: unaccounted switch\n"); | |
385 | break; | |
386 | } | |
387 | } | |
388 | ||
8eab189b MM |
389 | /****/ |
390 | ||
391 | void * | |
392 | zalloc(long size) | |
393 | { | |
394 | void *memory = (void*)xmalloc(size); | |
395 | if (memory == NULL) | |
396 | error("xmalloc failed\n"); | |
290ad14a | 397 | memset(memory, 0, size); |
8eab189b MM |
398 | return memory; |
399 | } | |
400 | ||
401 | void zfree(void *data) | |
402 | { | |
403 | mfree(NULL, data); | |
404 | } |