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