Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Generic remote debugging interface for simulators. |
0a65a603 AC |
2 | |
3 | Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, | |
4 | 2002 Free Software Foundation, Inc. | |
5 | ||
c906108c SS |
6 | Contributed by Cygnus Support. |
7 | Steve Chamberlain (sac@cygnus.com). | |
8 | ||
c5aa993b | 9 | This file is part of GDB. |
c906108c | 10 | |
c5aa993b JM |
11 | This program is free software; you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License as published by | |
13 | the Free Software Foundation; either version 2 of the License, or | |
14 | (at your option) any later version. | |
c906108c | 15 | |
c5aa993b JM |
16 | This program is distributed in the hope that it will be useful, |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU General Public License for more details. | |
c906108c | 20 | |
c5aa993b JM |
21 | You should have received a copy of the GNU General Public License |
22 | along with this program; if not, write to the Free Software | |
23 | Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
25 | |
26 | #include "defs.h" | |
27 | #include "inferior.h" | |
c906108c SS |
28 | #include "value.h" |
29 | #include "gdb_string.h" | |
30 | #include <ctype.h> | |
31 | #include <fcntl.h> | |
32 | #include <signal.h> | |
33 | #include <setjmp.h> | |
34 | #include <errno.h> | |
35 | #include "terminal.h" | |
36 | #include "target.h" | |
37 | #include "gdbcore.h" | |
38 | #include "callback.h" | |
39 | #include "remote-sim.h" | |
40 | #include "remote-utils.h" | |
41 | #include "command.h" | |
4e052eda | 42 | #include "regcache.h" |
8238d0bf AC |
43 | #include "gdb_assert.h" |
44 | #include "sim-regno.h" | |
c906108c SS |
45 | |
46 | /* Prototypes */ | |
47 | ||
a14ed312 | 48 | extern void _initialize_remote_sim (void); |
392a587b | 49 | |
507f3c78 | 50 | extern int (*ui_loop_hook) (int signo); |
7a292a7a | 51 | |
a14ed312 | 52 | static void dump_mem (char *buf, int len); |
c906108c | 53 | |
a14ed312 | 54 | static void init_callbacks (void); |
c906108c | 55 | |
a14ed312 | 56 | static void end_callbacks (void); |
c906108c | 57 | |
a14ed312 | 58 | static int gdb_os_write_stdout (host_callback *, const char *, int); |
c906108c | 59 | |
a14ed312 | 60 | static void gdb_os_flush_stdout (host_callback *); |
c906108c | 61 | |
a14ed312 | 62 | static int gdb_os_write_stderr (host_callback *, const char *, int); |
c906108c | 63 | |
a14ed312 | 64 | static void gdb_os_flush_stderr (host_callback *); |
c906108c | 65 | |
a14ed312 | 66 | static int gdb_os_poll_quit (host_callback *); |
c906108c SS |
67 | |
68 | /* printf_filtered is depreciated */ | |
a14ed312 | 69 | static void gdb_os_printf_filtered (host_callback *, const char *, ...); |
c906108c | 70 | |
a14ed312 | 71 | static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list); |
c906108c | 72 | |
a14ed312 | 73 | static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list); |
c906108c | 74 | |
a14ed312 | 75 | static void gdb_os_error (host_callback *, const char *, ...); |
c906108c | 76 | |
a14ed312 | 77 | static void gdbsim_fetch_register (int regno); |
c906108c | 78 | |
a14ed312 | 79 | static void gdbsim_store_register (int regno); |
c906108c | 80 | |
a14ed312 | 81 | static void gdbsim_kill (void); |
c906108c | 82 | |
a14ed312 | 83 | static void gdbsim_load (char *prog, int fromtty); |
c906108c | 84 | |
a14ed312 | 85 | static void gdbsim_create_inferior (char *exec_file, char *args, char **env); |
c906108c | 86 | |
a14ed312 | 87 | static void gdbsim_open (char *args, int from_tty); |
c906108c | 88 | |
a14ed312 | 89 | static void gdbsim_close (int quitting); |
c906108c | 90 | |
a14ed312 | 91 | static void gdbsim_detach (char *args, int from_tty); |
c906108c | 92 | |
39f77062 | 93 | static void gdbsim_resume (ptid_t ptid, int step, enum target_signal siggnal); |
c906108c | 94 | |
39f77062 | 95 | static ptid_t gdbsim_wait (ptid_t ptid, struct target_waitstatus *status); |
c906108c | 96 | |
a14ed312 | 97 | static void gdbsim_prepare_to_store (void); |
c906108c | 98 | |
29e57380 C |
99 | static int gdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr, |
100 | int len, int write, | |
101 | struct mem_attrib *attrib, | |
102 | struct target_ops *target); | |
c906108c | 103 | |
a14ed312 | 104 | static void gdbsim_files_info (struct target_ops *target); |
c906108c | 105 | |
a14ed312 | 106 | static void gdbsim_mourn_inferior (void); |
c906108c | 107 | |
a14ed312 | 108 | static void gdbsim_stop (void); |
c906108c | 109 | |
a14ed312 | 110 | void simulator_command (char *args, int from_tty); |
c906108c SS |
111 | |
112 | /* Naming convention: | |
113 | ||
114 | sim_* are the interface to the simulator (see remote-sim.h). | |
115 | gdbsim_* are stuff which is internal to gdb. */ | |
116 | ||
117 | /* Forward data declarations */ | |
118 | extern struct target_ops gdbsim_ops; | |
119 | ||
120 | static int program_loaded = 0; | |
121 | ||
122 | /* We must keep track of whether the simulator has been opened or not because | |
123 | GDB can call a target's close routine twice, but sim_close doesn't allow | |
124 | this. We also need to record the result of sim_open so we can pass it | |
125 | back to the other sim_foo routines. */ | |
126 | static SIM_DESC gdbsim_desc = 0; | |
127 | ||
128 | static void | |
fba45db2 | 129 | dump_mem (char *buf, int len) |
c906108c SS |
130 | { |
131 | if (len <= 8) | |
132 | { | |
133 | if (len == 8 || len == 4) | |
134 | { | |
135 | long l[2]; | |
136 | memcpy (l, buf, len); | |
d4f3574e | 137 | printf_filtered ("\t0x%lx", l[0]); |
a6da1910 AC |
138 | if (len == 8) |
139 | printf_filtered (" 0x%lx", l[1]); | |
140 | printf_filtered ("\n"); | |
c906108c SS |
141 | } |
142 | else | |
143 | { | |
144 | int i; | |
145 | printf_filtered ("\t"); | |
146 | for (i = 0; i < len; i++) | |
147 | printf_filtered ("0x%x ", buf[i]); | |
148 | printf_filtered ("\n"); | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
153 | static host_callback gdb_callback; | |
154 | static int callbacks_initialized = 0; | |
155 | ||
156 | /* Initialize gdb_callback. */ | |
157 | ||
158 | static void | |
fba45db2 | 159 | init_callbacks (void) |
c906108c | 160 | { |
c5aa993b | 161 | if (!callbacks_initialized) |
c906108c SS |
162 | { |
163 | gdb_callback = default_callback; | |
164 | gdb_callback.init (&gdb_callback); | |
165 | gdb_callback.write_stdout = gdb_os_write_stdout; | |
166 | gdb_callback.flush_stdout = gdb_os_flush_stdout; | |
167 | gdb_callback.write_stderr = gdb_os_write_stderr; | |
168 | gdb_callback.flush_stderr = gdb_os_flush_stderr; | |
169 | gdb_callback.printf_filtered = gdb_os_printf_filtered; | |
170 | gdb_callback.vprintf_filtered = gdb_os_vprintf_filtered; | |
171 | gdb_callback.evprintf_filtered = gdb_os_evprintf_filtered; | |
172 | gdb_callback.error = gdb_os_error; | |
173 | gdb_callback.poll_quit = gdb_os_poll_quit; | |
174 | gdb_callback.magic = HOST_CALLBACK_MAGIC; | |
175 | callbacks_initialized = 1; | |
176 | } | |
177 | } | |
178 | ||
179 | /* Release callbacks (free resources used by them). */ | |
180 | ||
181 | static void | |
fba45db2 | 182 | end_callbacks (void) |
c906108c SS |
183 | { |
184 | if (callbacks_initialized) | |
185 | { | |
186 | gdb_callback.shutdown (&gdb_callback); | |
187 | callbacks_initialized = 0; | |
188 | } | |
189 | } | |
190 | ||
191 | /* GDB version of os_write_stdout callback. */ | |
192 | ||
c5aa993b | 193 | static int |
fba45db2 | 194 | gdb_os_write_stdout (host_callback *p, const char *buf, int len) |
c906108c SS |
195 | { |
196 | int i; | |
197 | char b[2]; | |
198 | ||
d9fcf2fb | 199 | ui_file_write (gdb_stdtarg, buf, len); |
c906108c SS |
200 | return len; |
201 | } | |
202 | ||
203 | /* GDB version of os_flush_stdout callback. */ | |
204 | ||
205 | static void | |
fba45db2 | 206 | gdb_os_flush_stdout (host_callback *p) |
c906108c | 207 | { |
4ce44c66 | 208 | gdb_flush (gdb_stdtarg); |
c906108c SS |
209 | } |
210 | ||
211 | /* GDB version of os_write_stderr callback. */ | |
212 | ||
c5aa993b | 213 | static int |
fba45db2 | 214 | gdb_os_write_stderr (host_callback *p, const char *buf, int len) |
c906108c SS |
215 | { |
216 | int i; | |
217 | char b[2]; | |
218 | ||
c5aa993b | 219 | for (i = 0; i < len; i++) |
c906108c SS |
220 | { |
221 | b[0] = buf[i]; | |
222 | b[1] = 0; | |
43ff13b4 | 223 | fputs_unfiltered (b, gdb_stdtarg); |
c906108c SS |
224 | } |
225 | return len; | |
226 | } | |
227 | ||
228 | /* GDB version of os_flush_stderr callback. */ | |
229 | ||
230 | static void | |
fba45db2 | 231 | gdb_os_flush_stderr (host_callback *p) |
c906108c SS |
232 | { |
233 | gdb_flush (gdb_stderr); | |
234 | } | |
235 | ||
236 | /* GDB version of printf_filtered callback. */ | |
237 | ||
c906108c | 238 | static void |
c5aa993b | 239 | gdb_os_printf_filtered (host_callback * p, const char *format,...) |
c906108c SS |
240 | { |
241 | va_list args; | |
c906108c | 242 | va_start (args, format); |
c906108c SS |
243 | |
244 | vfprintf_filtered (gdb_stdout, format, args); | |
245 | ||
246 | va_end (args); | |
247 | } | |
248 | ||
249 | /* GDB version of error vprintf_filtered. */ | |
250 | ||
c906108c | 251 | static void |
c5aa993b | 252 | gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap) |
c906108c SS |
253 | { |
254 | vfprintf_filtered (gdb_stdout, format, ap); | |
255 | } | |
256 | ||
257 | /* GDB version of error evprintf_filtered. */ | |
258 | ||
c906108c | 259 | static void |
c5aa993b | 260 | gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap) |
c906108c SS |
261 | { |
262 | vfprintf_filtered (gdb_stderr, format, ap); | |
263 | } | |
264 | ||
265 | /* GDB version of error callback. */ | |
266 | ||
c906108c | 267 | static void |
c5aa993b | 268 | gdb_os_error (host_callback * p, const char *format,...) |
c906108c SS |
269 | { |
270 | if (error_hook) | |
271 | (*error_hook) (); | |
c5aa993b | 272 | else |
c906108c SS |
273 | { |
274 | va_list args; | |
c906108c | 275 | va_start (args, format); |
4ce44c66 | 276 | verror (format, args); |
c906108c | 277 | va_end (args); |
c906108c SS |
278 | } |
279 | } | |
280 | ||
8238d0bf AC |
281 | int |
282 | legacy_register_sim_regno (int regnum) | |
283 | { | |
284 | /* Only makes sense to supply raw registers. */ | |
285 | gdb_assert (regnum >= 0 && regnum < NUM_REGS); | |
286 | /* NOTE: cagney/2002-05-13: The old code did it this way and it is | |
287 | suspected that some GDB/SIM combinations may rely on this | |
288 | behavour. The default should be one2one_register_sim_regno | |
289 | (below). */ | |
290 | if (REGISTER_NAME (regnum) != NULL | |
291 | && REGISTER_NAME (regnum)[0] != '\0') | |
292 | return regnum; | |
293 | else | |
294 | return LEGACY_SIM_REGNO_IGNORE; | |
295 | } | |
296 | ||
297 | int | |
298 | one2one_register_sim_regno (int regnum) | |
299 | { | |
300 | /* Only makes sense to supply raw registers. */ | |
301 | gdb_assert (regnum >= 0 && regnum < NUM_REGS); | |
302 | return regnum; | |
303 | } | |
304 | ||
c906108c | 305 | static void |
fba45db2 | 306 | gdbsim_fetch_register (int regno) |
c906108c | 307 | { |
c5aa993b | 308 | if (regno == -1) |
c906108c SS |
309 | { |
310 | for (regno = 0; regno < NUM_REGS; regno++) | |
311 | gdbsim_fetch_register (regno); | |
8238d0bf | 312 | return; |
c906108c | 313 | } |
8238d0bf AC |
314 | |
315 | switch (REGISTER_SIM_REGNO (regno)) | |
c906108c | 316 | { |
8238d0bf AC |
317 | case LEGACY_SIM_REGNO_IGNORE: |
318 | break; | |
319 | case SIM_REGNO_DOES_NOT_EXIST: | |
320 | { | |
321 | /* For moment treat a `does not exist' register the same way | |
322 | as an ``unavailable'' register. */ | |
323 | char *buf = alloca (MAX_REGISTER_RAW_SIZE); | |
324 | int nr_bytes; | |
325 | memset (buf, 0, MAX_REGISTER_RAW_SIZE); | |
326 | supply_register (regno, buf); | |
327 | set_register_cached (regno, -1); | |
328 | break; | |
329 | } | |
330 | default: | |
331 | { | |
332 | static int warn_user = 1; | |
333 | char *buf = alloca (MAX_REGISTER_RAW_SIZE); | |
334 | int nr_bytes; | |
335 | gdb_assert (regno >= 0 && regno < NUM_REGS); | |
336 | memset (buf, 0, MAX_REGISTER_RAW_SIZE); | |
d4f3574e SS |
337 | nr_bytes = sim_fetch_register (gdbsim_desc, |
338 | REGISTER_SIM_REGNO (regno), | |
339 | buf, REGISTER_RAW_SIZE (regno)); | |
8238d0bf AC |
340 | if (nr_bytes > 0 && nr_bytes != REGISTER_RAW_SIZE (regno) && warn_user) |
341 | { | |
342 | fprintf_unfiltered (gdb_stderr, | |
343 | "Size of register %s (%d/%d) incorrect (%d instead of %d))", | |
344 | REGISTER_NAME (regno), | |
345 | regno, REGISTER_SIM_REGNO (regno), | |
346 | nr_bytes, REGISTER_RAW_SIZE (regno)); | |
347 | warn_user = 0; | |
348 | } | |
349 | /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0' | |
350 | indicatingthat GDB and the SIM have different ideas about | |
351 | which registers are fetchable. */ | |
352 | /* Else if (nr_bytes < 0): an old simulator, that doesn't | |
353 | think to return the register size. Just assume all is ok. */ | |
354 | supply_register (regno, buf); | |
355 | if (sr_get_debug ()) | |
356 | { | |
357 | printf_filtered ("gdbsim_fetch_register: %d", regno); | |
358 | /* FIXME: We could print something more intelligible. */ | |
359 | dump_mem (buf, REGISTER_RAW_SIZE (regno)); | |
360 | } | |
361 | break; | |
362 | } | |
c906108c SS |
363 | } |
364 | } | |
365 | ||
366 | ||
367 | static void | |
fba45db2 | 368 | gdbsim_store_register (int regno) |
c906108c | 369 | { |
c5aa993b | 370 | if (regno == -1) |
c906108c SS |
371 | { |
372 | for (regno = 0; regno < NUM_REGS; regno++) | |
373 | gdbsim_store_register (regno); | |
8238d0bf | 374 | return; |
c906108c | 375 | } |
8238d0bf | 376 | else if (REGISTER_SIM_REGNO (regno) >= 0) |
c906108c SS |
377 | { |
378 | char tmp[MAX_REGISTER_RAW_SIZE]; | |
379 | int nr_bytes; | |
380 | read_register_gen (regno, tmp); | |
d4f3574e SS |
381 | nr_bytes = sim_store_register (gdbsim_desc, |
382 | REGISTER_SIM_REGNO (regno), | |
383 | tmp, REGISTER_RAW_SIZE (regno)); | |
c906108c | 384 | if (nr_bytes > 0 && nr_bytes != REGISTER_RAW_SIZE (regno)) |
8e65ff28 AC |
385 | internal_error (__FILE__, __LINE__, |
386 | "Register size different to expected"); | |
8238d0bf AC |
387 | /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0' |
388 | indicatingthat GDB and the SIM have different ideas about | |
389 | which registers are fetchable. */ | |
c906108c SS |
390 | if (sr_get_debug ()) |
391 | { | |
392 | printf_filtered ("gdbsim_store_register: %d", regno); | |
393 | /* FIXME: We could print something more intelligible. */ | |
394 | dump_mem (tmp, REGISTER_RAW_SIZE (regno)); | |
395 | } | |
396 | } | |
397 | } | |
398 | ||
399 | /* Kill the running program. This may involve closing any open files | |
400 | and releasing other resources acquired by the simulated program. */ | |
401 | ||
402 | static void | |
fba45db2 | 403 | gdbsim_kill (void) |
c906108c SS |
404 | { |
405 | if (sr_get_debug ()) | |
406 | printf_filtered ("gdbsim_kill\n"); | |
407 | ||
408 | /* There is no need to `kill' running simulator - the simulator is | |
409 | not running */ | |
39f77062 | 410 | inferior_ptid = null_ptid; |
c906108c SS |
411 | } |
412 | ||
413 | /* Load an executable file into the target process. This is expected to | |
414 | not only bring new code into the target process, but also to update | |
415 | GDB's symbol tables to match. */ | |
416 | ||
417 | static void | |
fba45db2 | 418 | gdbsim_load (char *prog, int fromtty) |
c906108c SS |
419 | { |
420 | if (sr_get_debug ()) | |
421 | printf_filtered ("gdbsim_load: prog \"%s\"\n", prog); | |
422 | ||
39f77062 | 423 | inferior_ptid = null_ptid; |
c906108c SS |
424 | |
425 | /* FIXME: We will print two messages on error. | |
426 | Need error to either not print anything if passed NULL or need | |
427 | another routine that doesn't take any arguments. */ | |
428 | if (sim_load (gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL) | |
429 | error ("unable to load program"); | |
430 | ||
431 | /* FIXME: If a load command should reset the targets registers then | |
432 | a call to sim_create_inferior() should go here. */ | |
433 | ||
434 | program_loaded = 1; | |
435 | } | |
436 | ||
437 | ||
39f77062 | 438 | /* Start an inferior process and set inferior_ptid to its pid. |
c906108c SS |
439 | EXEC_FILE is the file to run. |
440 | ARGS is a string containing the arguments to the program. | |
441 | ENV is the environment vector to pass. Errors reported with error(). | |
442 | On VxWorks and various standalone systems, we ignore exec_file. */ | |
443 | /* This is called not only when we first attach, but also when the | |
444 | user types "run" after having attached. */ | |
445 | ||
446 | static void | |
fba45db2 | 447 | gdbsim_create_inferior (char *exec_file, char *args, char **env) |
c906108c SS |
448 | { |
449 | int len; | |
c5aa993b | 450 | char *arg_buf, **argv; |
c906108c SS |
451 | |
452 | if (exec_file == 0 || exec_bfd == 0) | |
453 | warning ("No executable file specified."); | |
c5aa993b | 454 | if (!program_loaded) |
c906108c SS |
455 | warning ("No program loaded."); |
456 | ||
457 | if (sr_get_debug ()) | |
458 | printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n", | |
c5aa993b | 459 | (exec_file ? exec_file : "(NULL)"), |
c906108c SS |
460 | args); |
461 | ||
c5aa993b | 462 | gdbsim_kill (); |
c906108c SS |
463 | remove_breakpoints (); |
464 | init_wait_for_inferior (); | |
465 | ||
466 | if (exec_file != NULL) | |
467 | { | |
c5aa993b | 468 | len = strlen (exec_file) + 1 + strlen (args) + 1 + /*slop */ 10; |
c906108c SS |
469 | arg_buf = (char *) alloca (len); |
470 | arg_buf[0] = '\0'; | |
471 | strcat (arg_buf, exec_file); | |
472 | strcat (arg_buf, " "); | |
473 | strcat (arg_buf, args); | |
474 | argv = buildargv (arg_buf); | |
7a292a7a | 475 | make_cleanup_freeargv (argv); |
c906108c SS |
476 | } |
477 | else | |
478 | argv = NULL; | |
479 | sim_create_inferior (gdbsim_desc, exec_bfd, argv, env); | |
480 | ||
39f77062 | 481 | inferior_ptid = pid_to_ptid (42); |
c5aa993b | 482 | insert_breakpoints (); /* Needed to get correct instruction in cache */ |
c906108c SS |
483 | |
484 | clear_proceed_status (); | |
485 | ||
486 | /* NB: Entry point already set by sim_create_inferior. */ | |
2acceee2 | 487 | proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0); |
c906108c SS |
488 | } |
489 | ||
490 | /* The open routine takes the rest of the parameters from the command, | |
491 | and (if successful) pushes a new target onto the stack. | |
492 | Targets should supply this routine, if only to provide an error message. */ | |
493 | /* Called when selecting the simulator. EG: (gdb) target sim name. */ | |
494 | ||
495 | static void | |
fba45db2 | 496 | gdbsim_open (char *args, int from_tty) |
c906108c SS |
497 | { |
498 | int len; | |
499 | char *arg_buf; | |
500 | char **argv; | |
501 | ||
502 | if (sr_get_debug ()) | |
503 | printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)"); | |
504 | ||
505 | /* Remove current simulator if one exists. Only do this if the simulator | |
506 | has been opened because sim_close requires it. | |
507 | This is important because the call to push_target below will cause | |
508 | sim_close to be called if the simulator is already open, but push_target | |
509 | is called after sim_open! We can't move the call to push_target before | |
510 | the call to sim_open because sim_open may invoke `error'. */ | |
511 | if (gdbsim_desc != NULL) | |
512 | unpush_target (&gdbsim_ops); | |
513 | ||
c5aa993b | 514 | len = (7 + 1 /* gdbsim */ |
c906108c SS |
515 | + strlen (" -E little") |
516 | + strlen (" --architecture=xxxxxxxxxx") | |
517 | + (args ? strlen (args) : 0) | |
c5aa993b | 518 | + 50) /* slack */ ; |
c906108c | 519 | arg_buf = (char *) alloca (len); |
c5aa993b | 520 | strcpy (arg_buf, "gdbsim"); /* 7 */ |
c906108c SS |
521 | /* Specify the byte order for the target when it is both selectable |
522 | and explicitly specified by the user (not auto detected). */ | |
3fd3d7d2 | 523 | if (!TARGET_BYTE_ORDER_AUTO) |
c906108c SS |
524 | { |
525 | switch (TARGET_BYTE_ORDER) | |
526 | { | |
d7449b42 | 527 | case BFD_ENDIAN_BIG: |
c906108c SS |
528 | strcat (arg_buf, " -E big"); |
529 | break; | |
778eb05e | 530 | case BFD_ENDIAN_LITTLE: |
c906108c SS |
531 | strcat (arg_buf, " -E little"); |
532 | break; | |
533 | default: | |
8e65ff28 AC |
534 | internal_error (__FILE__, __LINE__, |
535 | "Value of TARGET_BYTE_ORDER unknown"); | |
c906108c SS |
536 | } |
537 | } | |
538 | /* Specify the architecture of the target when it has been | |
539 | explicitly specified */ | |
540 | if (!TARGET_ARCHITECTURE_AUTO) | |
541 | { | |
542 | strcat (arg_buf, " --architecture="); | |
543 | strcat (arg_buf, TARGET_ARCHITECTURE->printable_name); | |
544 | } | |
545 | /* finally, any explicit args */ | |
546 | if (args) | |
547 | { | |
c5aa993b | 548 | strcat (arg_buf, " "); /* 1 */ |
c906108c SS |
549 | strcat (arg_buf, args); |
550 | } | |
551 | argv = buildargv (arg_buf); | |
552 | if (argv == NULL) | |
553 | error ("Insufficient memory available to allocate simulator arg list."); | |
7a292a7a | 554 | make_cleanup_freeargv (argv); |
c906108c SS |
555 | |
556 | init_callbacks (); | |
557 | gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, argv); | |
558 | ||
559 | if (gdbsim_desc == 0) | |
560 | error ("unable to create simulator instance"); | |
561 | ||
562 | push_target (&gdbsim_ops); | |
563 | target_fetch_registers (-1); | |
564 | printf_filtered ("Connected to the simulator.\n"); | |
565 | } | |
566 | ||
567 | /* Does whatever cleanup is required for a target that we are no longer | |
568 | going to be calling. Argument says whether we are quitting gdb and | |
569 | should not get hung in case of errors, or whether we want a clean | |
570 | termination even if it takes a while. This routine is automatically | |
571 | always called just before a routine is popped off the target stack. | |
572 | Closing file descriptors and freeing memory are typical things it should | |
573 | do. */ | |
574 | /* Close out all files and local state before this target loses control. */ | |
575 | ||
576 | static void | |
fba45db2 | 577 | gdbsim_close (int quitting) |
c906108c SS |
578 | { |
579 | if (sr_get_debug ()) | |
580 | printf_filtered ("gdbsim_close: quitting %d\n", quitting); | |
581 | ||
582 | program_loaded = 0; | |
583 | ||
584 | if (gdbsim_desc != NULL) | |
585 | { | |
586 | sim_close (gdbsim_desc, quitting); | |
587 | gdbsim_desc = NULL; | |
588 | } | |
589 | ||
590 | end_callbacks (); | |
75660bc0 | 591 | generic_mourn_inferior (); |
c906108c SS |
592 | } |
593 | ||
594 | /* Takes a program previously attached to and detaches it. | |
595 | The program may resume execution (some targets do, some don't) and will | |
596 | no longer stop on signals, etc. We better not have left any breakpoints | |
597 | in the program or it'll die when it hits one. ARGS is arguments | |
598 | typed by the user (e.g. a signal to send the process). FROM_TTY | |
599 | says whether to be verbose or not. */ | |
600 | /* Terminate the open connection to the remote debugger. | |
601 | Use this when you want to detach and do something else with your gdb. */ | |
602 | ||
603 | static void | |
fba45db2 | 604 | gdbsim_detach (char *args, int from_tty) |
c906108c SS |
605 | { |
606 | if (sr_get_debug ()) | |
607 | printf_filtered ("gdbsim_detach: args \"%s\"\n", args); | |
608 | ||
609 | pop_target (); /* calls gdbsim_close to do the real work */ | |
610 | if (from_tty) | |
611 | printf_filtered ("Ending simulator %s debugging\n", target_shortname); | |
612 | } | |
c5aa993b | 613 | |
c906108c SS |
614 | /* Resume execution of the target process. STEP says whether to single-step |
615 | or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given | |
616 | to the target, or zero for no signal. */ | |
617 | ||
618 | static enum target_signal resume_siggnal; | |
619 | static int resume_step; | |
620 | ||
621 | static void | |
39f77062 | 622 | gdbsim_resume (ptid_t ptid, int step, enum target_signal siggnal) |
c906108c | 623 | { |
39f77062 | 624 | if (PIDGET (inferior_ptid) != 42) |
c906108c SS |
625 | error ("The program is not being run."); |
626 | ||
627 | if (sr_get_debug ()) | |
628 | printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal); | |
629 | ||
630 | resume_siggnal = siggnal; | |
631 | resume_step = step; | |
632 | } | |
633 | ||
634 | /* Notify the simulator of an asynchronous request to stop. | |
c5aa993b | 635 | |
c906108c SS |
636 | The simulator shall ensure that the stop request is eventually |
637 | delivered to the simulator. If the call is made while the | |
638 | simulator is not running then the stop request is processed when | |
639 | the simulator is next resumed. | |
640 | ||
641 | For simulators that do not support this operation, just abort */ | |
642 | ||
643 | static void | |
fba45db2 | 644 | gdbsim_stop (void) |
c906108c | 645 | { |
c5aa993b | 646 | if (!sim_stop (gdbsim_desc)) |
c906108c SS |
647 | { |
648 | quit (); | |
649 | } | |
650 | } | |
651 | ||
652 | /* GDB version of os_poll_quit callback. | |
653 | Taken from gdb/util.c - should be in a library */ | |
654 | ||
655 | static int | |
fba45db2 | 656 | gdb_os_poll_quit (host_callback *p) |
c906108c | 657 | { |
7a292a7a SS |
658 | if (ui_loop_hook != NULL) |
659 | ui_loop_hook (0); | |
660 | ||
c5aa993b | 661 | if (quit_flag) /* gdb's idea of quit */ |
c906108c | 662 | { |
c5aa993b | 663 | quit_flag = 0; /* we've stolen it */ |
c906108c SS |
664 | return 1; |
665 | } | |
666 | else if (immediate_quit) | |
667 | { | |
668 | return 1; | |
669 | } | |
670 | return 0; | |
671 | } | |
672 | ||
673 | /* Wait for inferior process to do something. Return pid of child, | |
674 | or -1 in case of error; store status through argument pointer STATUS, | |
675 | just as `wait' would. */ | |
676 | ||
677 | static void | |
fba45db2 | 678 | gdbsim_cntrl_c (int signo) |
c906108c SS |
679 | { |
680 | gdbsim_stop (); | |
681 | } | |
682 | ||
39f77062 KB |
683 | static ptid_t |
684 | gdbsim_wait (ptid_t ptid, struct target_waitstatus *status) | |
c906108c SS |
685 | { |
686 | static RETSIGTYPE (*prev_sigint) (); | |
687 | int sigrc = 0; | |
688 | enum sim_stop reason = sim_running; | |
689 | ||
690 | if (sr_get_debug ()) | |
691 | printf_filtered ("gdbsim_wait\n"); | |
692 | ||
693 | #if defined (HAVE_SIGACTION) && defined (SA_RESTART) | |
694 | { | |
695 | struct sigaction sa, osa; | |
696 | sa.sa_handler = gdbsim_cntrl_c; | |
697 | sigemptyset (&sa.sa_mask); | |
698 | sa.sa_flags = 0; | |
699 | sigaction (SIGINT, &sa, &osa); | |
700 | prev_sigint = osa.sa_handler; | |
701 | } | |
702 | #else | |
703 | prev_sigint = signal (SIGINT, gdbsim_cntrl_c); | |
704 | #endif | |
705 | sim_resume (gdbsim_desc, resume_step, | |
706 | target_signal_to_host (resume_siggnal)); | |
707 | signal (SIGINT, prev_sigint); | |
708 | resume_step = 0; | |
709 | ||
710 | sim_stop_reason (gdbsim_desc, &reason, &sigrc); | |
711 | ||
712 | switch (reason) | |
713 | { | |
714 | case sim_exited: | |
715 | status->kind = TARGET_WAITKIND_EXITED; | |
716 | status->value.integer = sigrc; | |
717 | break; | |
718 | case sim_stopped: | |
719 | switch (sigrc) | |
720 | { | |
721 | case SIGABRT: | |
722 | quit (); | |
723 | break; | |
724 | case SIGINT: | |
725 | case SIGTRAP: | |
726 | default: | |
727 | status->kind = TARGET_WAITKIND_STOPPED; | |
728 | /* The signal in sigrc is a host signal. That probably | |
729 | should be fixed. */ | |
730 | status->value.sig = target_signal_from_host (sigrc); | |
731 | break; | |
732 | } | |
733 | break; | |
734 | case sim_signalled: | |
735 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
736 | /* The signal in sigrc is a host signal. That probably | |
c5aa993b | 737 | should be fixed. */ |
c906108c SS |
738 | status->value.sig = target_signal_from_host (sigrc); |
739 | break; | |
740 | case sim_running: | |
741 | case sim_polling: | |
742 | /* FIXME: Is this correct? */ | |
743 | break; | |
744 | } | |
745 | ||
39f77062 | 746 | return inferior_ptid; |
c906108c SS |
747 | } |
748 | ||
749 | /* Get ready to modify the registers array. On machines which store | |
750 | individual registers, this doesn't need to do anything. On machines | |
751 | which store all the registers in one fell swoop, this makes sure | |
752 | that registers contains all the registers from the program being | |
753 | debugged. */ | |
754 | ||
755 | static void | |
fba45db2 | 756 | gdbsim_prepare_to_store (void) |
c906108c SS |
757 | { |
758 | /* Do nothing, since we can store individual regs */ | |
759 | } | |
760 | ||
d93bce06 KB |
761 | /* Transfer LEN bytes between GDB address MYADDR and target address |
762 | MEMADDR. If WRITE is non-zero, transfer them to the target, | |
763 | otherwise transfer them from the target. TARGET is unused. | |
764 | ||
765 | Returns the number of bytes transferred. */ | |
766 | ||
c906108c | 767 | static int |
d93bce06 | 768 | gdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len, |
0a65a603 AC |
769 | int write, struct mem_attrib *attrib, |
770 | struct target_ops *target) | |
c906108c | 771 | { |
c5aa993b | 772 | if (!program_loaded) |
c906108c SS |
773 | error ("No program loaded."); |
774 | ||
775 | if (sr_get_debug ()) | |
776 | { | |
d4f3574e SS |
777 | /* FIXME: Send to something other than STDOUT? */ |
778 | printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x"); | |
779 | gdb_print_host_address (myaddr, gdb_stdout); | |
780 | printf_filtered (", memaddr 0x%s, len %d, write %d\n", | |
781 | paddr_nz (memaddr), len, write); | |
c906108c | 782 | if (sr_get_debug () && write) |
c5aa993b | 783 | dump_mem (myaddr, len); |
c906108c SS |
784 | } |
785 | ||
786 | if (write) | |
787 | { | |
788 | len = sim_write (gdbsim_desc, memaddr, myaddr, len); | |
789 | } | |
c5aa993b | 790 | else |
c906108c SS |
791 | { |
792 | len = sim_read (gdbsim_desc, memaddr, myaddr, len); | |
793 | if (sr_get_debug () && len > 0) | |
c5aa993b JM |
794 | dump_mem (myaddr, len); |
795 | } | |
c906108c SS |
796 | return len; |
797 | } | |
798 | ||
799 | static void | |
fba45db2 | 800 | gdbsim_files_info (struct target_ops *target) |
c906108c SS |
801 | { |
802 | char *file = "nothing"; | |
803 | ||
804 | if (exec_bfd) | |
805 | file = bfd_get_filename (exec_bfd); | |
806 | ||
807 | if (sr_get_debug ()) | |
808 | printf_filtered ("gdbsim_files_info: file \"%s\"\n", file); | |
809 | ||
810 | if (exec_bfd) | |
811 | { | |
812 | printf_filtered ("\tAttached to %s running program %s\n", | |
813 | target_shortname, file); | |
814 | sim_info (gdbsim_desc, 0); | |
815 | } | |
816 | } | |
817 | ||
818 | /* Clear the simulator's notion of what the break points are. */ | |
819 | ||
820 | static void | |
fba45db2 | 821 | gdbsim_mourn_inferior (void) |
c5aa993b | 822 | { |
c906108c SS |
823 | if (sr_get_debug ()) |
824 | printf_filtered ("gdbsim_mourn_inferior:\n"); | |
825 | ||
826 | remove_breakpoints (); | |
827 | generic_mourn_inferior (); | |
828 | } | |
829 | ||
830 | static int | |
fba45db2 | 831 | gdbsim_insert_breakpoint (CORE_ADDR addr, char *contents_cache) |
c906108c SS |
832 | { |
833 | #ifdef SIM_HAS_BREAKPOINTS | |
834 | SIM_RC retcode; | |
835 | ||
836 | retcode = sim_set_breakpoint (gdbsim_desc, addr); | |
837 | ||
838 | switch (retcode) | |
839 | { | |
840 | case SIM_RC_OK: | |
841 | return 0; | |
842 | case SIM_RC_INSUFFICIENT_RESOURCES: | |
843 | return ENOMEM; | |
844 | default: | |
845 | return EIO; | |
846 | } | |
847 | #else | |
848 | return memory_insert_breakpoint (addr, contents_cache); | |
849 | #endif | |
850 | } | |
851 | ||
852 | static int | |
fba45db2 | 853 | gdbsim_remove_breakpoint (CORE_ADDR addr, char *contents_cache) |
c906108c SS |
854 | { |
855 | #ifdef SIM_HAS_BREAKPOINTS | |
856 | SIM_RC retcode; | |
857 | ||
858 | retcode = sim_clear_breakpoint (gdbsim_desc, addr); | |
859 | ||
860 | switch (retcode) | |
861 | { | |
862 | case SIM_RC_OK: | |
863 | case SIM_RC_UNKNOWN_BREAKPOINT: | |
864 | return 0; | |
865 | case SIM_RC_INSUFFICIENT_RESOURCES: | |
866 | return ENOMEM; | |
867 | default: | |
868 | return EIO; | |
869 | } | |
870 | #else | |
871 | return memory_remove_breakpoint (addr, contents_cache); | |
872 | #endif | |
873 | } | |
874 | ||
875 | /* Pass the command argument through to the simulator verbatim. The | |
876 | simulator must do any command interpretation work. */ | |
877 | ||
878 | void | |
fba45db2 | 879 | simulator_command (char *args, int from_tty) |
c906108c SS |
880 | { |
881 | if (gdbsim_desc == NULL) | |
882 | { | |
883 | ||
884 | /* PREVIOUSLY: The user may give a command before the simulator | |
885 | is opened. [...] (??? assuming of course one wishes to | |
886 | continue to allow commands to be sent to unopened simulators, | |
887 | which isn't entirely unreasonable). */ | |
888 | ||
889 | /* The simulator is a builtin abstraction of a remote target. | |
890 | Consistent with that model, access to the simulator, via sim | |
891 | commands, is restricted to the period when the channel to the | |
892 | simulator is open. */ | |
893 | ||
894 | error ("Not connected to the simulator target"); | |
895 | } | |
896 | ||
897 | sim_do_command (gdbsim_desc, args); | |
898 | ||
899 | /* Invalidate the register cache, in case the simulator command does | |
900 | something funny. */ | |
c5aa993b | 901 | registers_changed (); |
c906108c SS |
902 | } |
903 | ||
904 | /* Define the target subroutine names */ | |
905 | ||
c5aa993b JM |
906 | struct target_ops gdbsim_ops; |
907 | ||
908 | static void | |
909 | init_gdbsim_ops (void) | |
910 | { | |
911 | gdbsim_ops.to_shortname = "sim"; | |
912 | gdbsim_ops.to_longname = "simulator"; | |
913 | gdbsim_ops.to_doc = "Use the compiled-in simulator."; | |
914 | gdbsim_ops.to_open = gdbsim_open; | |
915 | gdbsim_ops.to_close = gdbsim_close; | |
916 | gdbsim_ops.to_attach = NULL; | |
917 | gdbsim_ops.to_post_attach = NULL; | |
918 | gdbsim_ops.to_require_attach = NULL; | |
919 | gdbsim_ops.to_detach = gdbsim_detach; | |
920 | gdbsim_ops.to_require_detach = NULL; | |
921 | gdbsim_ops.to_resume = gdbsim_resume; | |
922 | gdbsim_ops.to_wait = gdbsim_wait; | |
923 | gdbsim_ops.to_post_wait = NULL; | |
924 | gdbsim_ops.to_fetch_registers = gdbsim_fetch_register; | |
925 | gdbsim_ops.to_store_registers = gdbsim_store_register; | |
926 | gdbsim_ops.to_prepare_to_store = gdbsim_prepare_to_store; | |
927 | gdbsim_ops.to_xfer_memory = gdbsim_xfer_inferior_memory; | |
928 | gdbsim_ops.to_files_info = gdbsim_files_info; | |
929 | gdbsim_ops.to_insert_breakpoint = gdbsim_insert_breakpoint; | |
930 | gdbsim_ops.to_remove_breakpoint = gdbsim_remove_breakpoint; | |
931 | gdbsim_ops.to_terminal_init = NULL; | |
932 | gdbsim_ops.to_terminal_inferior = NULL; | |
933 | gdbsim_ops.to_terminal_ours_for_output = NULL; | |
934 | gdbsim_ops.to_terminal_ours = NULL; | |
935 | gdbsim_ops.to_terminal_info = NULL; | |
936 | gdbsim_ops.to_kill = gdbsim_kill; | |
937 | gdbsim_ops.to_load = gdbsim_load; | |
938 | gdbsim_ops.to_lookup_symbol = NULL; | |
939 | gdbsim_ops.to_create_inferior = gdbsim_create_inferior; | |
c906108c SS |
940 | gdbsim_ops.to_post_startup_inferior = NULL; |
941 | gdbsim_ops.to_acknowledge_created_inferior = NULL; | |
942 | gdbsim_ops.to_clone_and_follow_inferior = NULL; | |
943 | gdbsim_ops.to_post_follow_inferior_by_clone = NULL; | |
944 | gdbsim_ops.to_insert_fork_catchpoint = NULL; | |
945 | gdbsim_ops.to_remove_fork_catchpoint = NULL; | |
946 | gdbsim_ops.to_insert_vfork_catchpoint = NULL; | |
947 | gdbsim_ops.to_remove_vfork_catchpoint = NULL; | |
948 | gdbsim_ops.to_has_forked = NULL; | |
949 | gdbsim_ops.to_has_vforked = NULL; | |
950 | gdbsim_ops.to_can_follow_vfork_prior_to_exec = NULL; | |
951 | gdbsim_ops.to_post_follow_vfork = NULL; | |
952 | gdbsim_ops.to_insert_exec_catchpoint = NULL; | |
953 | gdbsim_ops.to_remove_exec_catchpoint = NULL; | |
954 | gdbsim_ops.to_has_execd = NULL; | |
955 | gdbsim_ops.to_reported_exec_events_per_exec_call = NULL; | |
956 | gdbsim_ops.to_has_exited = NULL; | |
c5aa993b JM |
957 | gdbsim_ops.to_mourn_inferior = gdbsim_mourn_inferior; |
958 | gdbsim_ops.to_can_run = 0; | |
959 | gdbsim_ops.to_notice_signals = 0; | |
960 | gdbsim_ops.to_thread_alive = 0; | |
961 | gdbsim_ops.to_stop = gdbsim_stop; | |
962 | gdbsim_ops.to_pid_to_exec_file = NULL; | |
c5aa993b JM |
963 | gdbsim_ops.to_stratum = process_stratum; |
964 | gdbsim_ops.DONT_USE = NULL; | |
965 | gdbsim_ops.to_has_all_memory = 1; | |
966 | gdbsim_ops.to_has_memory = 1; | |
967 | gdbsim_ops.to_has_stack = 1; | |
968 | gdbsim_ops.to_has_registers = 1; | |
969 | gdbsim_ops.to_has_execution = 1; | |
970 | gdbsim_ops.to_sections = NULL; | |
971 | gdbsim_ops.to_sections_end = NULL; | |
972 | gdbsim_ops.to_magic = OPS_MAGIC; | |
c906108c SS |
973 | |
974 | #ifdef TARGET_REDEFINE_DEFAULT_OPS | |
975 | TARGET_REDEFINE_DEFAULT_OPS (&gdbsim_ops); | |
976 | #endif | |
977 | } | |
978 | ||
979 | void | |
fba45db2 | 980 | _initialize_remote_sim (void) |
c906108c | 981 | { |
c5aa993b | 982 | init_gdbsim_ops (); |
c906108c SS |
983 | add_target (&gdbsim_ops); |
984 | ||
985 | add_com ("sim <command>", class_obscure, simulator_command, | |
c5aa993b | 986 | "Send a command to the simulator."); |
c906108c | 987 | } |