gdb/remote-sim.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Generic remote debugging interface for simulators.

  2.    Copyright (C) 1993-2015 Free Software Foundation, Inc.

  3.    Contributed by Cygnus Support.
  4.    Steve Chamberlain (sac@cygnus.com).

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (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.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. #include "defs.h"
  17. #include "inferior.h"
  18. #include "infrun.h"
  19. #include "value.h"
  20. #include <ctype.h>
  21. #include <fcntl.h>
  22. #include <signal.h>
  23. #include <setjmp.h>
  24. #include "terminal.h"
  25. #include "target.h"
  26. #include "gdbcore.h"
  27. #include "gdb/callback.h"
  28. #include "gdb/remote-sim.h"
  29. #include "command.h"
  30. #include "regcache.h"
  31. #include "sim-regno.h"
  32. #include "arch-utils.h"
  33. #include "readline/readline.h"
  34. #include "gdbthread.h"

  35. /* Prototypes */

  36. extern void _initialize_remote_sim (void);

  37. static void init_callbacks (void);

  38. static void end_callbacks (void);

  39. static int gdb_os_write_stdout (host_callback *, const char *, int);

  40. static void gdb_os_flush_stdout (host_callback *);

  41. static int gdb_os_write_stderr (host_callback *, const char *, int);

  42. static void gdb_os_flush_stderr (host_callback *);

  43. static int gdb_os_poll_quit (host_callback *);

  44. /* printf_filtered is depreciated.  */
  45. static void gdb_os_printf_filtered (host_callback *, const char *, ...);

  46. static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list);

  47. static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list);

  48. static void gdb_os_error (host_callback *, const char *, ...)
  49.      ATTRIBUTE_NORETURN;

  50. static void gdbsim_kill (struct target_ops *);

  51. static void gdbsim_load (struct target_ops *self, const char *prog,
  52.                          int fromtty);

  53. static void gdbsim_open (const char *args, int from_tty);

  54. static void gdbsim_close (struct target_ops *self);

  55. static void gdbsim_detach (struct target_ops *ops, const char *args,
  56.                            int from_tty);

  57. static void gdbsim_prepare_to_store (struct target_ops *self,
  58.                                      struct regcache *regcache);

  59. static void gdbsim_files_info (struct target_ops *target);

  60. static void gdbsim_mourn_inferior (struct target_ops *target);

  61. static void gdbsim_stop (struct target_ops *self, ptid_t ptid);

  62. void simulator_command (char *args, int from_tty);

  63. /* Naming convention:

  64.    sim_* are the interface to the simulator (see remote-sim.h).
  65.    gdbsim_* are stuff which is internal to gdb.  */

  66. /* Forward data declarations */
  67. extern struct target_ops gdbsim_ops;

  68. static const struct inferior_data *sim_inferior_data_key;

  69. /* Simulator-specific, per-inferior state.  */
  70. struct sim_inferior_data {
  71.   /* Flag which indicates whether or not the program has been loaded.  */
  72.   int program_loaded;

  73.   /* Simulator descriptor for this inferior.  */
  74.   SIM_DESC gdbsim_desc;

  75.   /* This is the ptid we use for this particular simulator instance.  Its
  76.      value is somewhat arbitrary, as the simulator target don't have a
  77.      notion of tasks or threads, but we need something non-null to place
  78.      in inferior_ptid.  For simulators which permit multiple instances,
  79.      we also need a unique identifier to use for each inferior.  */
  80.   ptid_t remote_sim_ptid;

  81.   /* Signal with which to resume.  */
  82.   enum gdb_signal resume_siggnal;

  83.   /* Flag which indicates whether resume should step or not.  */
  84.   int resume_step;
  85. };

  86. /* Flag indicating the "open" status of this module.  It's set to 1
  87.    in gdbsim_open() and 0 in gdbsim_close().  */
  88. static int gdbsim_is_open = 0;

  89. /* Value of the next pid to allocate for an inferior.  As indicated
  90.    elsewhere, its initial value is somewhat arbitrary; it's critical
  91.    though that it's not zero or negative.  */
  92. static int next_pid;
  93. #define INITIAL_PID 42000

  94. /* Argument list to pass to sim_open().  It is allocated in gdbsim_open()
  95.    and deallocated in gdbsim_close().  The lifetime needs to extend beyond
  96.    the call to gdbsim_open() due to the fact that other sim instances other
  97.    than the first will be allocated after the gdbsim_open() call.  */
  98. static char **sim_argv = NULL;

  99. /* OS-level callback functions for write, flush, etc.  */
  100. static host_callback gdb_callback;
  101. static int callbacks_initialized = 0;

  102. /* Callback for iterate_over_inferiors.  It checks to see if the sim
  103.    descriptor passed via ARG is the same as that for the inferior
  104.    designated by INF.  Return true if so; false otherwise.  */

  105. static int
  106. check_for_duplicate_sim_descriptor (struct inferior *inf, void *arg)
  107. {
  108.   struct sim_inferior_data *sim_data;
  109.   SIM_DESC new_sim_desc = arg;

  110.   sim_data = inferior_data (inf, sim_inferior_data_key);

  111.   return (sim_data != NULL && sim_data->gdbsim_desc == new_sim_desc);
  112. }

  113. /* Flags indicating whether or not a sim instance is needed.  One of these
  114.    flags should be passed to get_sim_inferior_data().  */

  115. enum {SIM_INSTANCE_NOT_NEEDED = 0, SIM_INSTANCE_NEEDED = 1};

  116. /* Obtain pointer to per-inferior simulator data, allocating it if necessary.
  117.    Attempt to open the sim if SIM_INSTANCE_NEEDED is true.  */

  118. static struct sim_inferior_data *
  119. get_sim_inferior_data (struct inferior *inf, int sim_instance_needed)
  120. {
  121.   SIM_DESC sim_desc = NULL;
  122.   struct sim_inferior_data *sim_data
  123.     = inferior_data (inf, sim_inferior_data_key);

  124.   /* Try to allocate a new sim instance, if needed.  We do this ahead of
  125.      a potential allocation of a sim_inferior_data struct in order to
  126.      avoid needlessly allocating that struct in the event that the sim
  127.      instance allocation fails.  */
  128.   if (sim_instance_needed == SIM_INSTANCE_NEEDED
  129.       && (sim_data == NULL || sim_data->gdbsim_desc == NULL))
  130.     {
  131.       struct inferior *idup;
  132.       sim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, sim_argv);
  133.       if (sim_desc == NULL)
  134.         error (_("Unable to create simulator instance for inferior %d."),
  135.                inf->num);

  136.       idup = iterate_over_inferiors (check_for_duplicate_sim_descriptor,
  137.                                      sim_desc);
  138.       if (idup != NULL)
  139.         {
  140.           /* We don't close the descriptor due to the fact that it's
  141.              shared with some other inferior.  If we were to close it,
  142.              that might needlessly muck up the other inferior.  Of
  143.              course, it's possible that the damage has already been
  144.              done...  Note that it *will* ultimately be closed during
  145.              cleanup of the other inferior.  */
  146.           sim_desc = NULL;
  147.           error (
  148. _("Inferior %d and inferior %d would have identical simulator state.\n"
  149.    "(This simulator does not support the running of more than one inferior.)"),
  150.                  inf->num, idup->num);
  151.         }
  152.     }

  153.   if (sim_data == NULL)
  154.     {
  155.       sim_data = XCNEW(struct sim_inferior_data);
  156.       set_inferior_data (inf, sim_inferior_data_key, sim_data);

  157.       /* Allocate a ptid for this inferior.  */
  158.       sim_data->remote_sim_ptid = ptid_build (next_pid, 0, next_pid);
  159.       next_pid++;

  160.       /* Initialize the other instance variables.  */
  161.       sim_data->program_loaded = 0;
  162.       sim_data->gdbsim_desc = sim_desc;
  163.       sim_data->resume_siggnal = GDB_SIGNAL_0;
  164.       sim_data->resume_step = 0;
  165.     }
  166.   else if (sim_desc)
  167.     {
  168.       /* This handles the case where sim_data was allocated prior to
  169.          needing a sim instance.  */
  170.       sim_data->gdbsim_desc = sim_desc;
  171.     }


  172.   return sim_data;
  173. }

  174. /* Return pointer to per-inferior simulator data using PTID to find the
  175.    inferior in question.  Return NULL when no inferior is found or
  176.    when ptid has a zero or negative pid component.  */

  177. static struct sim_inferior_data *
  178. get_sim_inferior_data_by_ptid (ptid_t ptid, int sim_instance_needed)
  179. {
  180.   struct inferior *inf;
  181.   int pid = ptid_get_pid (ptid);

  182.   if (pid <= 0)
  183.     return NULL;

  184.   inf = find_inferior_pid (pid);

  185.   if (inf)
  186.     return get_sim_inferior_data (inf, sim_instance_needed);
  187.   else
  188.     return NULL;
  189. }

  190. /* Free the per-inferior simulator data.  */

  191. static void
  192. sim_inferior_data_cleanup (struct inferior *inf, void *data)
  193. {
  194.   struct sim_inferior_data *sim_data = data;

  195.   if (sim_data != NULL)
  196.     {
  197.       if (sim_data->gdbsim_desc)
  198.         {
  199.           sim_close (sim_data->gdbsim_desc, 0);
  200.           sim_data->gdbsim_desc = NULL;
  201.         }
  202.       xfree (sim_data);
  203.     }
  204. }

  205. static void
  206. dump_mem (const gdb_byte *buf, int len)
  207. {
  208.   fputs_unfiltered ("\t", gdb_stdlog);

  209.   if (len == 8 || len == 4)
  210.     {
  211.       uint32_t l[2];

  212.       memcpy (l, buf, len);
  213.       fprintf_unfiltered (gdb_stdlog, "0x%08x", l[0]);
  214.       if (len == 8)
  215.         fprintf_unfiltered (gdb_stdlog, " 0x%08x", l[1]);
  216.     }
  217.   else
  218.     {
  219.       int i;

  220.       for (i = 0; i < len; i++)
  221.         fprintf_unfiltered (gdb_stdlog, "0x%02x ", buf[i]);
  222.     }

  223.   fputs_unfiltered ("\n", gdb_stdlog);
  224. }

  225. /* Initialize gdb_callback.  */

  226. static void
  227. init_callbacks (void)
  228. {
  229.   if (!callbacks_initialized)
  230.     {
  231.       gdb_callback = default_callback;
  232.       gdb_callback.init (&gdb_callback);
  233.       gdb_callback.write_stdout = gdb_os_write_stdout;
  234.       gdb_callback.flush_stdout = gdb_os_flush_stdout;
  235.       gdb_callback.write_stderr = gdb_os_write_stderr;
  236.       gdb_callback.flush_stderr = gdb_os_flush_stderr;
  237.       gdb_callback.printf_filtered = gdb_os_printf_filtered;
  238.       gdb_callback.vprintf_filtered = gdb_os_vprintf_filtered;
  239.       gdb_callback.evprintf_filtered = gdb_os_evprintf_filtered;
  240.       gdb_callback.error = gdb_os_error;
  241.       gdb_callback.poll_quit = gdb_os_poll_quit;
  242.       gdb_callback.magic = HOST_CALLBACK_MAGIC;
  243.       callbacks_initialized = 1;
  244.     }
  245. }

  246. /* Release callbacks (free resources used by them).  */

  247. static void
  248. end_callbacks (void)
  249. {
  250.   if (callbacks_initialized)
  251.     {
  252.       gdb_callback.shutdown (&gdb_callback);
  253.       callbacks_initialized = 0;
  254.     }
  255. }

  256. /* GDB version of os_write_stdout callback.  */

  257. static int
  258. gdb_os_write_stdout (host_callback *p, const char *buf, int len)
  259. {
  260.   int i;
  261.   char b[2];

  262.   ui_file_write (gdb_stdtarg, buf, len);
  263.   return len;
  264. }

  265. /* GDB version of os_flush_stdout callback.  */

  266. static void
  267. gdb_os_flush_stdout (host_callback *p)
  268. {
  269.   gdb_flush (gdb_stdtarg);
  270. }

  271. /* GDB version of os_write_stderr callback.  */

  272. static int
  273. gdb_os_write_stderr (host_callback *p, const char *buf, int len)
  274. {
  275.   int i;
  276.   char b[2];

  277.   for (i = 0; i < len; i++)
  278.     {
  279.       b[0] = buf[i];
  280.       b[1] = 0;
  281.       fputs_unfiltered (b, gdb_stdtargerr);
  282.     }
  283.   return len;
  284. }

  285. /* GDB version of os_flush_stderr callback.  */

  286. static void
  287. gdb_os_flush_stderr (host_callback *p)
  288. {
  289.   gdb_flush (gdb_stdtargerr);
  290. }

  291. /* GDB version of printf_filtered callback.  */

  292. static void
  293. gdb_os_printf_filtered (host_callback * p, const char *format,...)
  294. {
  295.   va_list args;

  296.   va_start (args, format);
  297.   vfprintf_filtered (gdb_stdout, format, args);
  298.   va_end (args);
  299. }

  300. /* GDB version of error vprintf_filtered.  */

  301. static void
  302. gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap)
  303. {
  304.   vfprintf_filtered (gdb_stdout, format, ap);
  305. }

  306. /* GDB version of error evprintf_filtered.  */

  307. static void
  308. gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap)
  309. {
  310.   vfprintf_filtered (gdb_stderr, format, ap);
  311. }

  312. /* GDB version of error callback.  */

  313. static void
  314. gdb_os_error (host_callback * p, const char *format, ...)
  315. {
  316.   va_list args;

  317.   va_start (args, format);
  318.   verror (format, args);
  319.   va_end (args);
  320. }

  321. int
  322. one2one_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  323. {
  324.   /* Only makes sense to supply raw registers.  */
  325.   gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
  326.   return regnum;
  327. }

  328. static void
  329. gdbsim_fetch_register (struct target_ops *ops,
  330.                        struct regcache *regcache, int regno)
  331. {
  332.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  333.   struct sim_inferior_data *sim_data
  334.     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);

  335.   if (regno == -1)
  336.     {
  337.       for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++)
  338.         gdbsim_fetch_register (ops, regcache, regno);
  339.       return;
  340.     }

  341.   switch (gdbarch_register_sim_regno (gdbarch, regno))
  342.     {
  343.     case LEGACY_SIM_REGNO_IGNORE:
  344.       break;
  345.     case SIM_REGNO_DOES_NOT_EXIST:
  346.       {
  347.         /* For moment treat a `does not exist' register the same way
  348.            as an ``unavailable'' register.  */
  349.         gdb_byte buf[MAX_REGISTER_SIZE];
  350.         int nr_bytes;

  351.         memset (buf, 0, MAX_REGISTER_SIZE);
  352.         regcache_raw_supply (regcache, regno, buf);
  353.         break;
  354.       }

  355.     default:
  356.       {
  357.         static int warn_user = 1;
  358.         gdb_byte buf[MAX_REGISTER_SIZE];
  359.         int nr_bytes;

  360.         gdb_assert (regno >= 0 && regno < gdbarch_num_regs (gdbarch));
  361.         memset (buf, 0, MAX_REGISTER_SIZE);
  362.         nr_bytes = sim_fetch_register (sim_data->gdbsim_desc,
  363.                                        gdbarch_register_sim_regno
  364.                                          (gdbarch, regno),
  365.                                        buf,
  366.                                        register_size (gdbarch, regno));
  367.         if (nr_bytes > 0
  368.             && nr_bytes != register_size (gdbarch, regno) && warn_user)
  369.           {
  370.             fprintf_unfiltered (gdb_stderr,
  371.                                 "Size of register %s (%d/%d) "
  372.                                 "incorrect (%d instead of %d))",
  373.                                 gdbarch_register_name (gdbarch, regno),
  374.                                 regno,
  375.                                 gdbarch_register_sim_regno
  376.                                   (gdbarch, regno),
  377.                                 nr_bytes, register_size (gdbarch, regno));
  378.             warn_user = 0;
  379.           }
  380.         /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
  381.            indicating that GDB and the SIM have different ideas about
  382.            which registers are fetchable.  */
  383.         /* Else if (nr_bytes < 0): an old simulator, that doesn't
  384.            think to return the register size.  Just assume all is ok.  */
  385.         regcache_raw_supply (regcache, regno, buf);
  386.         if (remote_debug)
  387.           {
  388.             fprintf_unfiltered (gdb_stdlog,
  389.                                 "gdbsim_fetch_register: %d", regno);
  390.             /* FIXME: We could print something more intelligible.  */
  391.             dump_mem (buf, register_size (gdbarch, regno));
  392.           }
  393.         break;
  394.       }
  395.     }
  396. }


  397. static void
  398. gdbsim_store_register (struct target_ops *ops,
  399.                        struct regcache *regcache, int regno)
  400. {
  401.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  402.   struct sim_inferior_data *sim_data
  403.     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);

  404.   if (regno == -1)
  405.     {
  406.       for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++)
  407.         gdbsim_store_register (ops, regcache, regno);
  408.       return;
  409.     }
  410.   else if (gdbarch_register_sim_regno (gdbarch, regno) >= 0)
  411.     {
  412.       gdb_byte tmp[MAX_REGISTER_SIZE];
  413.       int nr_bytes;

  414.       regcache_cooked_read (regcache, regno, tmp);
  415.       nr_bytes = sim_store_register (sim_data->gdbsim_desc,
  416.                                      gdbarch_register_sim_regno
  417.                                        (gdbarch, regno),
  418.                                      tmp, register_size (gdbarch, regno));
  419.       if (nr_bytes > 0 && nr_bytes != register_size (gdbarch, regno))
  420.         internal_error (__FILE__, __LINE__,
  421.                         _("Register size different to expected"));
  422.       if (nr_bytes < 0)
  423.         internal_error (__FILE__, __LINE__,
  424.                         _("Register %d not updated"), regno);
  425.       if (nr_bytes == 0)
  426.         warning (_("Register %s not updated"),
  427.                  gdbarch_register_name (gdbarch, regno));

  428.       if (remote_debug)
  429.         {
  430.           fprintf_unfiltered (gdb_stdlog, "gdbsim_store_register: %d", regno);
  431.           /* FIXME: We could print something more intelligible.  */
  432.           dump_mem (tmp, register_size (gdbarch, regno));
  433.         }
  434.     }
  435. }

  436. /* Kill the running program.  This may involve closing any open files
  437.    and releasing other resources acquired by the simulated program.  */

  438. static void
  439. gdbsim_kill (struct target_ops *ops)
  440. {
  441.   if (remote_debug)
  442.     fprintf_unfiltered (gdb_stdlog, "gdbsim_kill\n");

  443.   /* There is no need to `kill' running simulator - the simulator is
  444.      not running.  Mourning it is enough.  */
  445.   target_mourn_inferior ();
  446. }

  447. /* Load an executable file into the target process.  This is expected to
  448.    not only bring new code into the target process, but also to update
  449.    GDB's symbol tables to match.  */

  450. static void
  451. gdbsim_load (struct target_ops *self, const char *args, int fromtty)
  452. {
  453.   char **argv;
  454.   const char *prog;
  455.   struct sim_inferior_data *sim_data
  456.     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);

  457.   if (args == NULL)
  458.       error_no_arg (_("program to load"));

  459.   argv = gdb_buildargv (args);
  460.   make_cleanup_freeargv (argv);

  461.   prog = tilde_expand (argv[0]);

  462.   if (argv[1] != NULL)
  463.     error (_("GDB sim does not yet support a load offset."));

  464.   if (remote_debug)
  465.     fprintf_unfiltered (gdb_stdlog, "gdbsim_load: prog \"%s\"\n", prog);

  466.   /* FIXME: We will print two messages on error.
  467.      Need error to either not print anything if passed NULL or need
  468.      another routine that doesn't take any arguments.  */
  469.   if (sim_load (sim_data->gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL)
  470.     error (_("unable to load program"));

  471.   /* FIXME: If a load command should reset the targets registers then
  472.      a call to sim_create_inferior() should go here.  */

  473.   sim_data->program_loaded = 1;
  474. }


  475. /* Start an inferior process and set inferior_ptid to its pid.
  476.    EXEC_FILE is the file to run.
  477.    ARGS is a string containing the arguments to the program.
  478.    ENV is the environment vector to pass.  Errors reported with error().
  479.    On VxWorks and various standalone systems, we ignore exec_file.  */
  480. /* This is called not only when we first attach, but also when the
  481.    user types "run" after having attached.  */

  482. static void
  483. gdbsim_create_inferior (struct target_ops *target, char *exec_file, char *args,
  484.                         char **env, int from_tty)
  485. {
  486.   struct sim_inferior_data *sim_data
  487.     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
  488.   int len;
  489.   char *arg_buf, **argv;

  490.   if (exec_file == 0 || exec_bfd == 0)
  491.     warning (_("No executable file specified."));
  492.   if (!sim_data->program_loaded)
  493.     warning (_("No program loaded."));

  494.   if (remote_debug)
  495.     fprintf_unfiltered (gdb_stdlog,
  496.                         "gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
  497.                         (exec_file ? exec_file : "(NULL)"),
  498.                         args);

  499.   if (ptid_equal (inferior_ptid, sim_data->remote_sim_ptid))
  500.     gdbsim_kill (target);
  501.   remove_breakpoints ();
  502.   init_wait_for_inferior ();

  503.   if (exec_file != NULL)
  504.     {
  505.       len = strlen (exec_file) + 1 + strlen (args) + 1 + /*slop */ 10;
  506.       arg_buf = (char *) alloca (len);
  507.       arg_buf[0] = '\0';
  508.       strcat (arg_buf, exec_file);
  509.       strcat (arg_buf, " ");
  510.       strcat (arg_buf, args);
  511.       argv = gdb_buildargv (arg_buf);
  512.       make_cleanup_freeargv (argv);
  513.     }
  514.   else
  515.     argv = NULL;

  516.   if (!have_inferiors ())
  517.     init_thread_list ();

  518.   if (sim_create_inferior (sim_data->gdbsim_desc, exec_bfd, argv, env)
  519.       != SIM_RC_OK)
  520.     error (_("Unable to create sim inferior."));

  521.   inferior_ptid = sim_data->remote_sim_ptid;
  522.   inferior_appeared (current_inferior (), ptid_get_pid (inferior_ptid));
  523.   add_thread_silent (inferior_ptid);

  524.   insert_breakpoints ();        /* Needed to get correct instruction
  525.                                    in cache.  */

  526.   clear_proceed_status (0);
  527. }

  528. /* The open routine takes the rest of the parameters from the command,
  529.    and (if successful) pushes a new target onto the stack.
  530.    Targets should supply this routine, if only to provide an error message.  */
  531. /* Called when selecting the simulator.  E.g. (gdb) target sim name.  */

  532. static void
  533. gdbsim_open (const char *args, int from_tty)
  534. {
  535.   int len;
  536.   char *arg_buf;
  537.   struct sim_inferior_data *sim_data;
  538.   SIM_DESC gdbsim_desc;

  539.   if (remote_debug)
  540.     fprintf_unfiltered (gdb_stdlog,
  541.                         "gdbsim_open: args \"%s\"\n", args ? args : "(null)");

  542.   /* Ensure that the sim target is not on the target stack.  This is
  543.      necessary, because if it is on the target stack, the call to
  544.      push_target below will invoke sim_close(), thus freeing various
  545.      state (including a sim instance) that we allocate prior to
  546.      invoking push_target().  We want to delay the push_target()
  547.      operation until after we complete those operations which could
  548.      error out.  */
  549.   if (gdbsim_is_open)
  550.     unpush_target (&gdbsim_ops);

  551.   len = (7 + 1                        /* gdbsim */
  552.          + strlen (" -E little")
  553.          + strlen (" --architecture=xxxxxxxxxx")
  554.          + strlen (" --sysroot=") + strlen (gdb_sysroot) +
  555.          + (args ? strlen (args) : 0)
  556.          + 50) /* slack */ ;
  557.   arg_buf = (char *) alloca (len);
  558.   strcpy (arg_buf, "gdbsim");        /* 7 */
  559.   /* Specify the byte order for the target when it is explicitly
  560.      specified by the user (not auto detected).  */
  561.   switch (selected_byte_order ())
  562.     {
  563.     case BFD_ENDIAN_BIG:
  564.       strcat (arg_buf, " -E big");
  565.       break;
  566.     case BFD_ENDIAN_LITTLE:
  567.       strcat (arg_buf, " -E little");
  568.       break;
  569.     case BFD_ENDIAN_UNKNOWN:
  570.       break;
  571.     }
  572.   /* Specify the architecture of the target when it has been
  573.      explicitly specified */
  574.   if (selected_architecture_name () != NULL)
  575.     {
  576.       strcat (arg_buf, " --architecture=");
  577.       strcat (arg_buf, selected_architecture_name ());
  578.     }
  579.   /* Pass along gdb's concept of the sysroot.  */
  580.   strcat (arg_buf, " --sysroot=");
  581.   strcat (arg_buf, gdb_sysroot);
  582.   /* finally, any explicit args */
  583.   if (args)
  584.     {
  585.       strcat (arg_buf, " ");        /* 1 */
  586.       strcat (arg_buf, args);
  587.     }
  588.   sim_argv = gdb_buildargv (arg_buf);

  589.   init_callbacks ();
  590.   gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, sim_argv);

  591.   if (gdbsim_desc == 0)
  592.     {
  593.       freeargv (sim_argv);
  594.       sim_argv = NULL;
  595.       error (_("unable to create simulator instance"));
  596.     }

  597.   /* Reset the pid numberings for this batch of sim instances.  */
  598.   next_pid = INITIAL_PID;

  599.   /* Allocate the inferior data, but do not allocate a sim instance
  600.      since we've already just done that.  */
  601.   sim_data = get_sim_inferior_data (current_inferior (),
  602.                                     SIM_INSTANCE_NOT_NEEDED);

  603.   sim_data->gdbsim_desc = gdbsim_desc;

  604.   push_target (&gdbsim_ops);
  605.   printf_filtered ("Connected to the simulator.\n");

  606.   /* There's nothing running after "target sim" or "load"; not until
  607.      "run".  */
  608.   inferior_ptid = null_ptid;

  609.   gdbsim_is_open = 1;
  610. }

  611. /* Callback for iterate_over_inferiors.  Called (indirectly) by
  612.    gdbsim_close().  */

  613. static int
  614. gdbsim_close_inferior (struct inferior *inf, void *arg)
  615. {
  616.   struct sim_inferior_data *sim_data = inferior_data (inf,
  617.                                                       sim_inferior_data_key);
  618.   if (sim_data != NULL)
  619.     {
  620.       ptid_t ptid = sim_data->remote_sim_ptid;

  621.       sim_inferior_data_cleanup (inf, sim_data);
  622.       set_inferior_data (inf, sim_inferior_data_key, NULL);

  623.       /* Having a ptid allocated and stored in remote_sim_ptid does
  624.          not mean that a corresponding inferior was ever created.
  625.          Thus we need to verify the existence of an inferior using the
  626.          pid in question before setting inferior_ptid via
  627.          switch_to_thread() or mourning the inferior.  */
  628.       if (find_inferior_ptid (ptid) != NULL)
  629.         {
  630.           switch_to_thread (ptid);
  631.           generic_mourn_inferior ();
  632.         }
  633.     }

  634.   return 0;
  635. }

  636. /* Close out all files and local state before this target loses control.  */

  637. static void
  638. gdbsim_close (struct target_ops *self)
  639. {
  640.   struct sim_inferior_data *sim_data
  641.     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);

  642.   if (remote_debug)
  643.     fprintf_unfiltered (gdb_stdlog, "gdbsim_close\n");

  644.   iterate_over_inferiors (gdbsim_close_inferior, NULL);

  645.   if (sim_argv != NULL)
  646.     {
  647.       freeargv (sim_argv);
  648.       sim_argv = NULL;
  649.     }

  650.   end_callbacks ();

  651.   gdbsim_is_open = 0;
  652. }

  653. /* Takes a program previously attached to and detaches it.
  654.    The program may resume execution (some targets do, some don't) and will
  655.    no longer stop on signals, etc.  We better not have left any breakpoints
  656.    in the program or it'll die when it hits one.  ARGS is arguments
  657.    typed by the user (e.g. a signal to send the process).  FROM_TTY
  658.    says whether to be verbose or not.  */
  659. /* Terminate the open connection to the remote debugger.
  660.    Use this when you want to detach and do something else with your gdb.  */

  661. static void
  662. gdbsim_detach (struct target_ops *ops, const char *args, int from_tty)
  663. {
  664.   if (remote_debug)
  665.     fprintf_unfiltered (gdb_stdlog, "gdbsim_detach: args \"%s\"\n", args);

  666.   unpush_target (ops);                /* calls gdbsim_close to do the real work */
  667.   if (from_tty)
  668.     printf_filtered ("Ending simulator %s debugging\n", target_shortname);
  669. }

  670. /* Resume execution of the target process.  STEP says whether to single-step
  671.    or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
  672.    to the target, or zero for no signal.  */

  673. struct resume_data
  674. {
  675.   enum gdb_signal siggnal;
  676.   int step;
  677. };

  678. static int
  679. gdbsim_resume_inferior (struct inferior *inf, void *arg)
  680. {
  681.   struct sim_inferior_data *sim_data
  682.     = get_sim_inferior_data (inf, SIM_INSTANCE_NOT_NEEDED);
  683.   struct resume_data *rd = arg;

  684.   if (sim_data)
  685.     {
  686.       sim_data->resume_siggnal = rd->siggnal;
  687.       sim_data->resume_step = rd->step;

  688.       if (remote_debug)
  689.         fprintf_unfiltered (gdb_stdlog,
  690.                             _("gdbsim_resume: pid %d, step %d, signal %d\n"),
  691.                             inf->pid, rd->step, rd->siggnal);
  692.     }

  693.   /* When called from iterate_over_inferiors, a zero return causes the
  694.      iteration process to proceed until there are no more inferiors to
  695.      consider.  */
  696.   return 0;
  697. }

  698. static void
  699. gdbsim_resume (struct target_ops *ops,
  700.                ptid_t ptid, int step, enum gdb_signal siggnal)
  701. {
  702.   struct resume_data rd;
  703.   struct sim_inferior_data *sim_data
  704.     = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);

  705.   rd.siggnal = siggnal;
  706.   rd.step = step;

  707.   /* We don't access any sim_data members within this function.
  708.      What's of interest is whether or not the call to
  709.      get_sim_inferior_data_by_ptid(), above, is able to obtain a
  710.      non-NULL pointer.  If it managed to obtain a non-NULL pointer, we
  711.      know we have a single inferior to consider.  If it's NULL, we
  712.      either have multiple inferiors to resume or an error condition.  */

  713.   if (sim_data)
  714.     gdbsim_resume_inferior (find_inferior_ptid (ptid), &rd);
  715.   else if (ptid_equal (ptid, minus_one_ptid))
  716.     iterate_over_inferiors (gdbsim_resume_inferior, &rd);
  717.   else
  718.     error (_("The program is not being run."));
  719. }

  720. /* Notify the simulator of an asynchronous request to stop.

  721.    The simulator shall ensure that the stop request is eventually
  722.    delivered to the simulator.  If the call is made while the
  723.    simulator is not running then the stop request is processed when
  724.    the simulator is next resumed.

  725.    For simulators that do not support this operation, just abort.  */

  726. static int
  727. gdbsim_stop_inferior (struct inferior *inf, void *arg)
  728. {
  729.   struct sim_inferior_data *sim_data
  730.     = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);

  731.   if (sim_data)
  732.     {
  733.       if (!sim_stop (sim_data->gdbsim_desc))
  734.         {
  735.           quit ();
  736.         }
  737.     }

  738.   /* When called from iterate_over_inferiors, a zero return causes the
  739.      iteration process to proceed until there are no more inferiors to
  740.      consider.  */
  741.   return 0;
  742. }

  743. static void
  744. gdbsim_stop (struct target_ops *self, ptid_t ptid)
  745. {
  746.   struct sim_inferior_data *sim_data;

  747.   if (ptid_equal (ptid, minus_one_ptid))
  748.     {
  749.       iterate_over_inferiors (gdbsim_stop_inferior, NULL);
  750.     }
  751.   else
  752.     {
  753.       struct inferior *inf = find_inferior_ptid (ptid);

  754.       if (inf == NULL)
  755.         error (_("Can't stop pid %d.  No inferior found."),
  756.                ptid_get_pid (ptid));

  757.       gdbsim_stop_inferior (inf, NULL);
  758.     }
  759. }

  760. /* GDB version of os_poll_quit callback.
  761.    Taken from gdb/util.c - should be in a library.  */

  762. static int
  763. gdb_os_poll_quit (host_callback *p)
  764. {
  765.   if (deprecated_ui_loop_hook != NULL)
  766.     deprecated_ui_loop_hook (0);

  767.   if (check_quit_flag ())        /* gdb's idea of quit */
  768.     {
  769.       clear_quit_flag ();        /* we've stolen it */
  770.       return 1;
  771.     }
  772.   return 0;
  773. }

  774. /* Wait for inferior process to do something.  Return pid of child,
  775.    or -1 in case of error; store status through argument pointer STATUS,
  776.    just as `wait' would.  */

  777. static void
  778. gdbsim_cntrl_c (int signo)
  779. {
  780.   gdbsim_stop (NULL, minus_one_ptid);
  781. }

  782. static ptid_t
  783. gdbsim_wait (struct target_ops *ops,
  784.              ptid_t ptid, struct target_waitstatus *status, int options)
  785. {
  786.   struct sim_inferior_data *sim_data;
  787.   static RETSIGTYPE (*prev_sigint) ();
  788.   int sigrc = 0;
  789.   enum sim_stop reason = sim_running;

  790.   /* This target isn't able to (yet) resume more than one inferior at a time.
  791.      When ptid is minus_one_ptid, just use the current inferior.  If we're
  792.      given an explicit pid, we'll try to find it and use that instead.  */
  793.   if (ptid_equal (ptid, minus_one_ptid))
  794.     sim_data = get_sim_inferior_data (current_inferior (),
  795.                                       SIM_INSTANCE_NEEDED);
  796.   else
  797.     {
  798.       sim_data = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NEEDED);
  799.       if (sim_data == NULL)
  800.         error (_("Unable to wait for pid %d.  Inferior not found."),
  801.                ptid_get_pid (ptid));
  802.       inferior_ptid = ptid;
  803.     }

  804.   if (remote_debug)
  805.     fprintf_unfiltered (gdb_stdlog, "gdbsim_wait\n");

  806. #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
  807.   {
  808.     struct sigaction sa, osa;
  809.     sa.sa_handler = gdbsim_cntrl_c;
  810.     sigemptyset (&sa.sa_mask);
  811.     sa.sa_flags = 0;
  812.     sigaction (SIGINT, &sa, &osa);
  813.     prev_sigint = osa.sa_handler;
  814.   }
  815. #else
  816.   prev_sigint = signal (SIGINT, gdbsim_cntrl_c);
  817. #endif
  818.   sim_resume (sim_data->gdbsim_desc, sim_data->resume_step,
  819.               sim_data->resume_siggnal);

  820.   signal (SIGINT, prev_sigint);
  821.   sim_data->resume_step = 0;

  822.   sim_stop_reason (sim_data->gdbsim_desc, &reason, &sigrc);

  823.   switch (reason)
  824.     {
  825.     case sim_exited:
  826.       status->kind = TARGET_WAITKIND_EXITED;
  827.       status->value.integer = sigrc;
  828.       break;
  829.     case sim_stopped:
  830.       switch (sigrc)
  831.         {
  832.         case GDB_SIGNAL_ABRT:
  833.           quit ();
  834.           break;
  835.         case GDB_SIGNAL_INT:
  836.         case GDB_SIGNAL_TRAP:
  837.         default:
  838.           status->kind = TARGET_WAITKIND_STOPPED;
  839.           status->value.sig = sigrc;
  840.           break;
  841.         }
  842.       break;
  843.     case sim_signalled:
  844.       status->kind = TARGET_WAITKIND_SIGNALLED;
  845.       status->value.sig = sigrc;
  846.       break;
  847.     case sim_running:
  848.     case sim_polling:
  849.       /* FIXME: Is this correct?  */
  850.       break;
  851.     }

  852.   return inferior_ptid;
  853. }

  854. /* Get ready to modify the registers array.  On machines which store
  855.    individual registers, this doesn't need to do anything.  On machines
  856.    which store all the registers in one fell swoop, this makes sure
  857.    that registers contains all the registers from the program being
  858.    debugged.  */

  859. static void
  860. gdbsim_prepare_to_store (struct target_ops *self, struct regcache *regcache)
  861. {
  862.   /* Do nothing, since we can store individual regs.  */
  863. }

  864. /* Helper for gdbsim_xfer_partial that handles memory transfers.
  865.    Arguments are like target_xfer_partial.  */

  866. static enum target_xfer_status
  867. gdbsim_xfer_memory (struct target_ops *target,
  868.                     gdb_byte *readbuf, const gdb_byte *writebuf,
  869.                     ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
  870. {
  871.   struct sim_inferior_data *sim_data
  872.     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
  873.   int l;

  874.   /* If this target doesn't have memory yet, return 0 causing the
  875.      request to be passed to a lower target, hopefully an exec
  876.      file.  */
  877.   if (!target->to_has_memory (target))
  878.     return TARGET_XFER_EOF;

  879.   if (!sim_data->program_loaded)
  880.     error (_("No program loaded."));

  881.   /* Note that we obtained the sim_data pointer above using
  882.      SIM_INSTANCE_NOT_NEEDED.  We do this so that we don't needlessly
  883.      allocate a sim instance prior to loading a program.   If we
  884.      get to this point in the code though, gdbsim_desc should be
  885.      non-NULL.  (Note that a sim instance is needed in order to load
  886.      the program...)  */
  887.   gdb_assert (sim_data->gdbsim_desc != NULL);

  888.   if (remote_debug)
  889.     fprintf_unfiltered (gdb_stdlog,
  890.                         "gdbsim_xfer_memory: readbuf %s, writebuf %s, "
  891.                         "memaddr %s, len %s\n",
  892.                         host_address_to_string (readbuf),
  893.                         host_address_to_string (writebuf),
  894.                         paddress (target_gdbarch (), memaddr),
  895.                         pulongest (len));

  896.   if (writebuf)
  897.     {
  898.       if (remote_debug && len > 0)
  899.         dump_mem (writebuf, len);
  900.       l = sim_write (sim_data->gdbsim_desc, memaddr, writebuf, len);
  901.     }
  902.   else
  903.     {
  904.       l = sim_read (sim_data->gdbsim_desc, memaddr, readbuf, len);
  905.       if (remote_debug && len > 0)
  906.         dump_mem (readbuf, len);
  907.     }
  908.   if (l > 0)
  909.     {
  910.       *xfered_len = (ULONGEST) l;
  911.       return TARGET_XFER_OK;
  912.     }
  913.   else if (l == 0)
  914.     return TARGET_XFER_EOF;
  915.   else
  916.     return TARGET_XFER_E_IO;
  917. }

  918. /* Target to_xfer_partial implementation.  */

  919. static enum target_xfer_status
  920. gdbsim_xfer_partial (struct target_ops *ops, enum target_object object,
  921.                      const char *annex, gdb_byte *readbuf,
  922.                      const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
  923.                      ULONGEST *xfered_len)
  924. {
  925.   switch (object)
  926.     {
  927.     case TARGET_OBJECT_MEMORY:
  928.       return gdbsim_xfer_memory (ops, readbuf, writebuf, offset, len,
  929.                                  xfered_len);

  930.     default:
  931.       return TARGET_XFER_E_IO;
  932.     }
  933. }

  934. static void
  935. gdbsim_files_info (struct target_ops *target)
  936. {
  937.   struct sim_inferior_data *sim_data
  938.     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
  939.   const char *file = "nothing";

  940.   if (exec_bfd)
  941.     file = bfd_get_filename (exec_bfd);

  942.   if (remote_debug)
  943.     fprintf_unfiltered (gdb_stdlog, "gdbsim_files_info: file \"%s\"\n", file);

  944.   if (exec_bfd)
  945.     {
  946.       fprintf_unfiltered (gdb_stdlog, "\tAttached to %s running program %s\n",
  947.                           target_shortname, file);
  948.       sim_info (sim_data->gdbsim_desc, 0);
  949.     }
  950. }

  951. /* Clear the simulator's notion of what the break points are.  */

  952. static void
  953. gdbsim_mourn_inferior (struct target_ops *target)
  954. {
  955.   struct sim_inferior_data *sim_data
  956.     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);

  957.   if (remote_debug)
  958.     fprintf_unfiltered (gdb_stdlog, "gdbsim_mourn_inferior:\n");

  959.   remove_breakpoints ();
  960.   generic_mourn_inferior ();
  961.   delete_thread_silent (sim_data->remote_sim_ptid);
  962. }

  963. /* Pass the command argument through to the simulator verbatim.  The
  964.    simulator must do any command interpretation work.  */

  965. void
  966. simulator_command (char *args, int from_tty)
  967. {
  968.   struct sim_inferior_data *sim_data;

  969.   /* We use inferior_data() instead of get_sim_inferior_data() here in
  970.      order to avoid attaching a sim_inferior_data struct to an
  971.      inferior unnecessarily.  The reason we take such care here is due
  972.      to the fact that this function, simulator_command(), may be called
  973.      even when the sim target is not active.  If we were to use
  974.      get_sim_inferior_data() here, it is possible that this call would
  975.      be made either prior to gdbsim_open() or after gdbsim_close(),
  976.      thus allocating memory that would not be garbage collected until
  977.      the ultimate destruction of the associated inferior.  */

  978.   sim_data  = inferior_data (current_inferior (), sim_inferior_data_key);
  979.   if (sim_data == NULL || sim_data->gdbsim_desc == NULL)
  980.     {

  981.       /* PREVIOUSLY: The user may give a command before the simulator
  982.          is opened. [...] (??? assuming of course one wishes to
  983.          continue to allow commands to be sent to unopened simulators,
  984.          which isn't entirely unreasonable).  */

  985.       /* The simulator is a builtin abstraction of a remote target.
  986.          Consistent with that model, access to the simulator, via sim
  987.          commands, is restricted to the period when the channel to the
  988.          simulator is open.  */

  989.       error (_("Not connected to the simulator target"));
  990.     }

  991.   sim_do_command (sim_data->gdbsim_desc, args);

  992.   /* Invalidate the register cache, in case the simulator command does
  993.      something funny.  */
  994.   registers_changed ();
  995. }

  996. static VEC (char_ptr) *
  997. sim_command_completer (struct cmd_list_element *ignore, const char *text,
  998.                        const char *word)
  999. {
  1000.   struct sim_inferior_data *sim_data;
  1001.   char **tmp;
  1002.   int i;
  1003.   VEC (char_ptr) *result = NULL;

  1004.   sim_data = inferior_data (current_inferior (), sim_inferior_data_key);
  1005.   if (sim_data == NULL || sim_data->gdbsim_desc == NULL)
  1006.     return NULL;

  1007.   tmp = sim_complete_command (sim_data->gdbsim_desc, text, word);
  1008.   if (tmp == NULL)
  1009.     return NULL;

  1010.   /* Transform the array into a VEC, and then free the array.  */
  1011.   for (i = 0; tmp[i] != NULL; i++)
  1012.     VEC_safe_push (char_ptr, result, tmp[i]);
  1013.   xfree (tmp);

  1014.   return result;
  1015. }

  1016. /* Check to see if a thread is still alive.  */

  1017. static int
  1018. gdbsim_thread_alive (struct target_ops *ops, ptid_t ptid)
  1019. {
  1020.   struct sim_inferior_data *sim_data
  1021.     = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);

  1022.   if (sim_data == NULL)
  1023.     return 0;

  1024.   if (ptid_equal (ptid, sim_data->remote_sim_ptid))
  1025.     /* The simulators' task is always alive.  */
  1026.     return 1;

  1027.   return 0;
  1028. }

  1029. /* Convert a thread ID to a string.  Returns the string in a static
  1030.    buffer.  */

  1031. static char *
  1032. gdbsim_pid_to_str (struct target_ops *ops, ptid_t ptid)
  1033. {
  1034.   return normal_pid_to_str (ptid);
  1035. }

  1036. /* Simulator memory may be accessed after the program has been loaded.  */

  1037. static int
  1038. gdbsim_has_all_memory (struct target_ops *ops)
  1039. {
  1040.   struct sim_inferior_data *sim_data
  1041.     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);

  1042.   if (!sim_data->program_loaded)
  1043.     return 0;

  1044.   return 1;
  1045. }

  1046. static int
  1047. gdbsim_has_memory (struct target_ops *ops)
  1048. {
  1049.   struct sim_inferior_data *sim_data
  1050.     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);

  1051.   if (!sim_data->program_loaded)
  1052.     return 0;

  1053.   return 1;
  1054. }

  1055. /* Define the target subroutine names.  */

  1056. struct target_ops gdbsim_ops;

  1057. static void
  1058. init_gdbsim_ops (void)
  1059. {
  1060.   gdbsim_ops.to_shortname = "sim";
  1061.   gdbsim_ops.to_longname = "simulator";
  1062.   gdbsim_ops.to_doc = "Use the compiled-in simulator.";
  1063.   gdbsim_ops.to_open = gdbsim_open;
  1064.   gdbsim_ops.to_close = gdbsim_close;
  1065.   gdbsim_ops.to_detach = gdbsim_detach;
  1066.   gdbsim_ops.to_resume = gdbsim_resume;
  1067.   gdbsim_ops.to_wait = gdbsim_wait;
  1068.   gdbsim_ops.to_fetch_registers = gdbsim_fetch_register;
  1069.   gdbsim_ops.to_store_registers = gdbsim_store_register;
  1070.   gdbsim_ops.to_prepare_to_store = gdbsim_prepare_to_store;
  1071.   gdbsim_ops.to_xfer_partial = gdbsim_xfer_partial;
  1072.   gdbsim_ops.to_files_info = gdbsim_files_info;
  1073.   gdbsim_ops.to_insert_breakpoint = memory_insert_breakpoint;
  1074.   gdbsim_ops.to_remove_breakpoint = memory_remove_breakpoint;
  1075.   gdbsim_ops.to_kill = gdbsim_kill;
  1076.   gdbsim_ops.to_load = gdbsim_load;
  1077.   gdbsim_ops.to_create_inferior = gdbsim_create_inferior;
  1078.   gdbsim_ops.to_mourn_inferior = gdbsim_mourn_inferior;
  1079.   gdbsim_ops.to_stop = gdbsim_stop;
  1080.   gdbsim_ops.to_thread_alive = gdbsim_thread_alive;
  1081.   gdbsim_ops.to_pid_to_str = gdbsim_pid_to_str;
  1082.   gdbsim_ops.to_stratum = process_stratum;
  1083.   gdbsim_ops.to_has_all_memory = gdbsim_has_all_memory;
  1084.   gdbsim_ops.to_has_memory = gdbsim_has_memory;
  1085.   gdbsim_ops.to_has_stack = default_child_has_stack;
  1086.   gdbsim_ops.to_has_registers = default_child_has_registers;
  1087.   gdbsim_ops.to_has_execution = default_child_has_execution;
  1088.   gdbsim_ops.to_magic = OPS_MAGIC;
  1089. }

  1090. void
  1091. _initialize_remote_sim (void)
  1092. {
  1093.   struct cmd_list_element *c;

  1094.   init_gdbsim_ops ();
  1095.   add_target (&gdbsim_ops);

  1096.   c = add_com ("sim", class_obscure, simulator_command,
  1097.                _("Send a command to the simulator."));
  1098.   set_cmd_completer (c, sim_command_completer);

  1099.   sim_inferior_data_key
  1100.     = register_inferior_data_with_cleanup (NULL, sim_inferior_data_cleanup);
  1101. }