1 /* Generic remote debugging interface for simulators.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2004 Free Software Foundation, Inc.
6 Contributed by Cygnus Support.
7 Steve Chamberlain (sac@cygnus.com).
9 This file is part of GDB.
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.
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.
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. */
29 #include "gdb_string.h"
38 #include "gdb/callback.h"
39 #include "gdb/remote-sim.h"
40 #include "remote-utils.h"
43 #include "gdb_assert.h"
44 #include "sim-regno.h"
45 #include "arch-utils.h"
49 extern void _initialize_remote_sim (void);
51 extern int (*ui_loop_hook) (int signo);
53 static void dump_mem (char *buf, int len);
55 static void init_callbacks (void);
57 static void end_callbacks (void);
59 static int gdb_os_write_stdout (host_callback *, const char *, int);
61 static void gdb_os_flush_stdout (host_callback *);
63 static int gdb_os_write_stderr (host_callback *, const char *, int);
65 static void gdb_os_flush_stderr (host_callback *);
67 static int gdb_os_poll_quit (host_callback *);
69 /* printf_filtered is depreciated */
70 static void gdb_os_printf_filtered (host_callback *, const char *, ...);
72 static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list);
74 static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list);
76 static void gdb_os_error (host_callback *, const char *, ...);
78 static void gdbsim_fetch_register (int regno);
80 static void gdbsim_store_register (int regno);
82 static void gdbsim_kill (void);
84 static void gdbsim_load (char *prog, int fromtty);
86 static void gdbsim_create_inferior (char *exec_file, char *args, char **env);
88 static void gdbsim_open (char *args, int from_tty);
90 static void gdbsim_close (int quitting);
92 static void gdbsim_detach (char *args, int from_tty);
94 static void gdbsim_resume (ptid_t ptid, int step, enum target_signal siggnal);
96 static ptid_t gdbsim_wait (ptid_t ptid, struct target_waitstatus *status);
98 static void gdbsim_prepare_to_store (void);
100 static int gdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr,
102 struct mem_attrib *attrib,
103 struct target_ops *target);
105 static void gdbsim_files_info (struct target_ops *target);
107 static void gdbsim_mourn_inferior (void);
109 static void gdbsim_stop (void);
111 void simulator_command (char *args, int from_tty);
113 /* Naming convention:
115 sim_* are the interface to the simulator (see remote-sim.h).
116 gdbsim_* are stuff which is internal to gdb. */
118 /* Forward data declarations */
119 extern struct target_ops gdbsim_ops;
121 static int program_loaded = 0;
123 /* We must keep track of whether the simulator has been opened or not because
124 GDB can call a target's close routine twice, but sim_close doesn't allow
125 this. We also need to record the result of sim_open so we can pass it
126 back to the other sim_foo routines. */
127 static SIM_DESC gdbsim_desc = 0;
130 dump_mem (char *buf, int len)
134 if (len == 8 || len == 4)
137 memcpy (l, buf, len);
138 printf_filtered ("\t0x%lx", l[0]);
140 printf_filtered (" 0x%lx", l[1]);
141 printf_filtered ("\n");
146 printf_filtered ("\t");
147 for (i = 0; i < len; i++)
148 printf_filtered ("0x%x ", buf[i]);
149 printf_filtered ("\n");
154 static host_callback gdb_callback;
155 static int callbacks_initialized = 0;
157 /* Initialize gdb_callback. */
160 init_callbacks (void)
162 if (!callbacks_initialized)
164 gdb_callback = default_callback;
165 gdb_callback.init (&gdb_callback);
166 gdb_callback.write_stdout = gdb_os_write_stdout;
167 gdb_callback.flush_stdout = gdb_os_flush_stdout;
168 gdb_callback.write_stderr = gdb_os_write_stderr;
169 gdb_callback.flush_stderr = gdb_os_flush_stderr;
170 gdb_callback.printf_filtered = gdb_os_printf_filtered;
171 gdb_callback.vprintf_filtered = gdb_os_vprintf_filtered;
172 gdb_callback.evprintf_filtered = gdb_os_evprintf_filtered;
173 gdb_callback.error = gdb_os_error;
174 gdb_callback.poll_quit = gdb_os_poll_quit;
175 gdb_callback.magic = HOST_CALLBACK_MAGIC;
176 callbacks_initialized = 1;
180 /* Release callbacks (free resources used by them). */
185 if (callbacks_initialized)
187 gdb_callback.shutdown (&gdb_callback);
188 callbacks_initialized = 0;
192 /* GDB version of os_write_stdout callback. */
195 gdb_os_write_stdout (host_callback *p, const char *buf, int len)
200 ui_file_write (gdb_stdtarg, buf, len);
204 /* GDB version of os_flush_stdout callback. */
207 gdb_os_flush_stdout (host_callback *p)
209 gdb_flush (gdb_stdtarg);
212 /* GDB version of os_write_stderr callback. */
215 gdb_os_write_stderr (host_callback *p, const char *buf, int len)
220 for (i = 0; i < len; i++)
224 fputs_unfiltered (b, gdb_stdtargerr);
229 /* GDB version of os_flush_stderr callback. */
232 gdb_os_flush_stderr (host_callback *p)
234 gdb_flush (gdb_stdtargerr);
237 /* GDB version of printf_filtered callback. */
240 gdb_os_printf_filtered (host_callback * p, const char *format,...)
243 va_start (args, format);
245 vfprintf_filtered (gdb_stdout, format, args);
250 /* GDB version of error vprintf_filtered. */
253 gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap)
255 vfprintf_filtered (gdb_stdout, format, ap);
258 /* GDB version of error evprintf_filtered. */
261 gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap)
263 vfprintf_filtered (gdb_stderr, format, ap);
266 /* GDB version of error callback. */
269 gdb_os_error (host_callback * p, const char *format,...)
276 va_start (args, format);
277 verror (format, args);
283 one2one_register_sim_regno (int regnum)
285 /* Only makes sense to supply raw registers. */
286 gdb_assert (regnum >= 0 && regnum < NUM_REGS);
291 gdbsim_fetch_register (int regno)
295 for (regno = 0; regno < NUM_REGS; regno++)
296 gdbsim_fetch_register (regno);
300 switch (REGISTER_SIM_REGNO (regno))
302 case LEGACY_SIM_REGNO_IGNORE:
304 case SIM_REGNO_DOES_NOT_EXIST:
306 /* For moment treat a `does not exist' register the same way
307 as an ``unavailable'' register. */
308 char buf[MAX_REGISTER_SIZE];
310 memset (buf, 0, MAX_REGISTER_SIZE);
311 supply_register (regno, buf);
312 set_register_cached (regno, -1);
317 static int warn_user = 1;
318 char buf[MAX_REGISTER_SIZE];
320 gdb_assert (regno >= 0 && regno < NUM_REGS);
321 memset (buf, 0, MAX_REGISTER_SIZE);
322 nr_bytes = sim_fetch_register (gdbsim_desc,
323 REGISTER_SIM_REGNO (regno),
324 buf, DEPRECATED_REGISTER_RAW_SIZE (regno));
325 if (nr_bytes > 0 && nr_bytes != DEPRECATED_REGISTER_RAW_SIZE (regno) && warn_user)
327 fprintf_unfiltered (gdb_stderr,
328 "Size of register %s (%d/%d) incorrect (%d instead of %d))",
329 REGISTER_NAME (regno),
330 regno, REGISTER_SIM_REGNO (regno),
331 nr_bytes, DEPRECATED_REGISTER_RAW_SIZE (regno));
334 /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
335 indicating that GDB and the SIM have different ideas about
336 which registers are fetchable. */
337 /* Else if (nr_bytes < 0): an old simulator, that doesn't
338 think to return the register size. Just assume all is ok. */
339 supply_register (regno, buf);
342 printf_filtered ("gdbsim_fetch_register: %d", regno);
343 /* FIXME: We could print something more intelligible. */
344 dump_mem (buf, DEPRECATED_REGISTER_RAW_SIZE (regno));
353 gdbsim_store_register (int regno)
357 for (regno = 0; regno < NUM_REGS; regno++)
358 gdbsim_store_register (regno);
361 else if (REGISTER_SIM_REGNO (regno) >= 0)
363 char tmp[MAX_REGISTER_SIZE];
365 deprecated_read_register_gen (regno, tmp);
366 nr_bytes = sim_store_register (gdbsim_desc,
367 REGISTER_SIM_REGNO (regno),
368 tmp, DEPRECATED_REGISTER_RAW_SIZE (regno));
369 if (nr_bytes > 0 && nr_bytes != DEPRECATED_REGISTER_RAW_SIZE (regno))
370 internal_error (__FILE__, __LINE__,
371 "Register size different to expected");
372 /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
373 indicating that GDB and the SIM have different ideas about
374 which registers are fetchable. */
377 printf_filtered ("gdbsim_store_register: %d", regno);
378 /* FIXME: We could print something more intelligible. */
379 dump_mem (tmp, DEPRECATED_REGISTER_RAW_SIZE (regno));
384 /* Kill the running program. This may involve closing any open files
385 and releasing other resources acquired by the simulated program. */
391 printf_filtered ("gdbsim_kill\n");
393 /* There is no need to `kill' running simulator - the simulator is
395 inferior_ptid = null_ptid;
398 /* Load an executable file into the target process. This is expected to
399 not only bring new code into the target process, but also to update
400 GDB's symbol tables to match. */
403 gdbsim_load (char *prog, int fromtty)
406 printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
408 inferior_ptid = null_ptid;
410 /* FIXME: We will print two messages on error.
411 Need error to either not print anything if passed NULL or need
412 another routine that doesn't take any arguments. */
413 if (sim_load (gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL)
414 error ("unable to load program");
416 /* FIXME: If a load command should reset the targets registers then
417 a call to sim_create_inferior() should go here. */
423 /* Start an inferior process and set inferior_ptid to its pid.
424 EXEC_FILE is the file to run.
425 ARGS is a string containing the arguments to the program.
426 ENV is the environment vector to pass. Errors reported with error().
427 On VxWorks and various standalone systems, we ignore exec_file. */
428 /* This is called not only when we first attach, but also when the
429 user types "run" after having attached. */
432 gdbsim_create_inferior (char *exec_file, char *args, char **env)
435 char *arg_buf, **argv;
437 if (exec_file == 0 || exec_bfd == 0)
438 warning ("No executable file specified.");
440 warning ("No program loaded.");
443 printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
444 (exec_file ? exec_file : "(NULL)"),
448 remove_breakpoints ();
449 init_wait_for_inferior ();
451 if (exec_file != NULL)
453 len = strlen (exec_file) + 1 + strlen (args) + 1 + /*slop */ 10;
454 arg_buf = (char *) alloca (len);
456 strcat (arg_buf, exec_file);
457 strcat (arg_buf, " ");
458 strcat (arg_buf, args);
459 argv = buildargv (arg_buf);
460 make_cleanup_freeargv (argv);
464 sim_create_inferior (gdbsim_desc, exec_bfd, argv, env);
466 inferior_ptid = pid_to_ptid (42);
467 insert_breakpoints (); /* Needed to get correct instruction in cache */
469 clear_proceed_status ();
471 /* NB: Entry point already set by sim_create_inferior. */
472 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
475 /* The open routine takes the rest of the parameters from the command,
476 and (if successful) pushes a new target onto the stack.
477 Targets should supply this routine, if only to provide an error message. */
478 /* Called when selecting the simulator. EG: (gdb) target sim name. */
481 gdbsim_open (char *args, int from_tty)
488 printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)");
490 /* Remove current simulator if one exists. Only do this if the simulator
491 has been opened because sim_close requires it.
492 This is important because the call to push_target below will cause
493 sim_close to be called if the simulator is already open, but push_target
494 is called after sim_open! We can't move the call to push_target before
495 the call to sim_open because sim_open may invoke `error'. */
496 if (gdbsim_desc != NULL)
497 unpush_target (&gdbsim_ops);
499 len = (7 + 1 /* gdbsim */
500 + strlen (" -E little")
501 + strlen (" --architecture=xxxxxxxxxx")
502 + (args ? strlen (args) : 0)
504 arg_buf = (char *) alloca (len);
505 strcpy (arg_buf, "gdbsim"); /* 7 */
506 /* Specify the byte order for the target when it is both selectable
507 and explicitly specified by the user (not auto detected). */
508 switch (selected_byte_order ())
511 strcat (arg_buf, " -E big");
513 case BFD_ENDIAN_LITTLE:
514 strcat (arg_buf, " -E little");
516 case BFD_ENDIAN_UNKNOWN:
519 /* Specify the architecture of the target when it has been
520 explicitly specified */
521 if (selected_architecture_name () != NULL)
523 strcat (arg_buf, " --architecture=");
524 strcat (arg_buf, selected_architecture_name ());
526 /* finally, any explicit args */
529 strcat (arg_buf, " "); /* 1 */
530 strcat (arg_buf, args);
532 argv = buildargv (arg_buf);
534 error ("Insufficient memory available to allocate simulator arg list.");
535 make_cleanup_freeargv (argv);
538 gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, argv);
540 if (gdbsim_desc == 0)
541 error ("unable to create simulator instance");
543 push_target (&gdbsim_ops);
544 target_fetch_registers (-1);
545 printf_filtered ("Connected to the simulator.\n");
548 /* Does whatever cleanup is required for a target that we are no longer
549 going to be calling. Argument says whether we are quitting gdb and
550 should not get hung in case of errors, or whether we want a clean
551 termination even if it takes a while. This routine is automatically
552 always called just before a routine is popped off the target stack.
553 Closing file descriptors and freeing memory are typical things it should
555 /* Close out all files and local state before this target loses control. */
558 gdbsim_close (int quitting)
561 printf_filtered ("gdbsim_close: quitting %d\n", quitting);
565 if (gdbsim_desc != NULL)
567 sim_close (gdbsim_desc, quitting);
572 generic_mourn_inferior ();
575 /* Takes a program previously attached to and detaches it.
576 The program may resume execution (some targets do, some don't) and will
577 no longer stop on signals, etc. We better not have left any breakpoints
578 in the program or it'll die when it hits one. ARGS is arguments
579 typed by the user (e.g. a signal to send the process). FROM_TTY
580 says whether to be verbose or not. */
581 /* Terminate the open connection to the remote debugger.
582 Use this when you want to detach and do something else with your gdb. */
585 gdbsim_detach (char *args, int from_tty)
588 printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
590 pop_target (); /* calls gdbsim_close to do the real work */
592 printf_filtered ("Ending simulator %s debugging\n", target_shortname);
595 /* Resume execution of the target process. STEP says whether to single-step
596 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
597 to the target, or zero for no signal. */
599 static enum target_signal resume_siggnal;
600 static int resume_step;
603 gdbsim_resume (ptid_t ptid, int step, enum target_signal siggnal)
605 if (PIDGET (inferior_ptid) != 42)
606 error ("The program is not being run.");
609 printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
611 resume_siggnal = siggnal;
615 /* Notify the simulator of an asynchronous request to stop.
617 The simulator shall ensure that the stop request is eventually
618 delivered to the simulator. If the call is made while the
619 simulator is not running then the stop request is processed when
620 the simulator is next resumed.
622 For simulators that do not support this operation, just abort */
627 if (!sim_stop (gdbsim_desc))
633 /* GDB version of os_poll_quit callback.
634 Taken from gdb/util.c - should be in a library */
637 gdb_os_poll_quit (host_callback *p)
639 if (ui_loop_hook != NULL)
642 if (quit_flag) /* gdb's idea of quit */
644 quit_flag = 0; /* we've stolen it */
647 else if (immediate_quit)
654 /* Wait for inferior process to do something. Return pid of child,
655 or -1 in case of error; store status through argument pointer STATUS,
656 just as `wait' would. */
659 gdbsim_cntrl_c (int signo)
665 gdbsim_wait (ptid_t ptid, struct target_waitstatus *status)
667 static RETSIGTYPE (*prev_sigint) ();
669 enum sim_stop reason = sim_running;
672 printf_filtered ("gdbsim_wait\n");
674 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
676 struct sigaction sa, osa;
677 sa.sa_handler = gdbsim_cntrl_c;
678 sigemptyset (&sa.sa_mask);
680 sigaction (SIGINT, &sa, &osa);
681 prev_sigint = osa.sa_handler;
684 prev_sigint = signal (SIGINT, gdbsim_cntrl_c);
686 sim_resume (gdbsim_desc, resume_step,
687 target_signal_to_host (resume_siggnal));
688 signal (SIGINT, prev_sigint);
691 sim_stop_reason (gdbsim_desc, &reason, &sigrc);
696 status->kind = TARGET_WAITKIND_EXITED;
697 status->value.integer = sigrc;
708 status->kind = TARGET_WAITKIND_STOPPED;
709 /* The signal in sigrc is a host signal. That probably
711 status->value.sig = target_signal_from_host (sigrc);
716 status->kind = TARGET_WAITKIND_SIGNALLED;
717 /* The signal in sigrc is a host signal. That probably
719 status->value.sig = target_signal_from_host (sigrc);
723 /* FIXME: Is this correct? */
727 return inferior_ptid;
730 /* Get ready to modify the registers array. On machines which store
731 individual registers, this doesn't need to do anything. On machines
732 which store all the registers in one fell swoop, this makes sure
733 that registers contains all the registers from the program being
737 gdbsim_prepare_to_store (void)
739 /* Do nothing, since we can store individual regs */
742 /* Transfer LEN bytes between GDB address MYADDR and target address
743 MEMADDR. If WRITE is non-zero, transfer them to the target,
744 otherwise transfer them from the target. TARGET is unused.
746 Returns the number of bytes transferred. */
749 gdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len,
750 int write, struct mem_attrib *attrib,
751 struct target_ops *target)
754 error ("No program loaded.");
758 /* FIXME: Send to something other than STDOUT? */
759 printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x");
760 gdb_print_host_address (myaddr, gdb_stdout);
761 printf_filtered (", memaddr 0x%s, len %d, write %d\n",
762 paddr_nz (memaddr), len, write);
763 if (sr_get_debug () && write)
764 dump_mem (myaddr, len);
769 len = sim_write (gdbsim_desc, memaddr, myaddr, len);
773 len = sim_read (gdbsim_desc, memaddr, myaddr, len);
774 if (sr_get_debug () && len > 0)
775 dump_mem (myaddr, len);
781 gdbsim_files_info (struct target_ops *target)
783 char *file = "nothing";
786 file = bfd_get_filename (exec_bfd);
789 printf_filtered ("gdbsim_files_info: file \"%s\"\n", file);
793 printf_filtered ("\tAttached to %s running program %s\n",
794 target_shortname, file);
795 sim_info (gdbsim_desc, 0);
799 /* Clear the simulator's notion of what the break points are. */
802 gdbsim_mourn_inferior (void)
805 printf_filtered ("gdbsim_mourn_inferior:\n");
807 remove_breakpoints ();
808 generic_mourn_inferior ();
812 gdbsim_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
814 return memory_insert_breakpoint (addr, contents_cache);
818 gdbsim_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
820 return memory_remove_breakpoint (addr, contents_cache);
823 /* Pass the command argument through to the simulator verbatim. The
824 simulator must do any command interpretation work. */
827 simulator_command (char *args, int from_tty)
829 if (gdbsim_desc == NULL)
832 /* PREVIOUSLY: The user may give a command before the simulator
833 is opened. [...] (??? assuming of course one wishes to
834 continue to allow commands to be sent to unopened simulators,
835 which isn't entirely unreasonable). */
837 /* The simulator is a builtin abstraction of a remote target.
838 Consistent with that model, access to the simulator, via sim
839 commands, is restricted to the period when the channel to the
840 simulator is open. */
842 error ("Not connected to the simulator target");
845 sim_do_command (gdbsim_desc, args);
847 /* Invalidate the register cache, in case the simulator command does
849 registers_changed ();
852 /* Define the target subroutine names */
854 struct target_ops gdbsim_ops;
857 init_gdbsim_ops (void)
859 gdbsim_ops.to_shortname = "sim";
860 gdbsim_ops.to_longname = "simulator";
861 gdbsim_ops.to_doc = "Use the compiled-in simulator.";
862 gdbsim_ops.to_open = gdbsim_open;
863 gdbsim_ops.to_close = gdbsim_close;
864 gdbsim_ops.to_detach = gdbsim_detach;
865 gdbsim_ops.to_resume = gdbsim_resume;
866 gdbsim_ops.to_wait = gdbsim_wait;
867 gdbsim_ops.to_fetch_registers = gdbsim_fetch_register;
868 gdbsim_ops.to_store_registers = gdbsim_store_register;
869 gdbsim_ops.to_prepare_to_store = gdbsim_prepare_to_store;
870 gdbsim_ops.to_xfer_memory = gdbsim_xfer_inferior_memory;
871 gdbsim_ops.to_files_info = gdbsim_files_info;
872 gdbsim_ops.to_insert_breakpoint = gdbsim_insert_breakpoint;
873 gdbsim_ops.to_remove_breakpoint = gdbsim_remove_breakpoint;
874 gdbsim_ops.to_kill = gdbsim_kill;
875 gdbsim_ops.to_load = gdbsim_load;
876 gdbsim_ops.to_create_inferior = gdbsim_create_inferior;
877 gdbsim_ops.to_mourn_inferior = gdbsim_mourn_inferior;
878 gdbsim_ops.to_stop = gdbsim_stop;
879 gdbsim_ops.to_stratum = process_stratum;
880 gdbsim_ops.to_has_all_memory = 1;
881 gdbsim_ops.to_has_memory = 1;
882 gdbsim_ops.to_has_stack = 1;
883 gdbsim_ops.to_has_registers = 1;
884 gdbsim_ops.to_has_execution = 1;
885 gdbsim_ops.to_magic = OPS_MAGIC;
887 #ifdef TARGET_REDEFINE_DEFAULT_OPS
888 TARGET_REDEFINE_DEFAULT_OPS (&gdbsim_ops);
893 _initialize_remote_sim (void)
896 add_target (&gdbsim_ops);
898 add_com ("sim <command>", class_obscure, simulator_command,
899 "Send a command to the simulator.");