1 /* Generic remote debugging interface for simulators.
3 Copyright (C) 1993-2018 Free Software Foundation, Inc.
5 Contributed by Cygnus Support.
6 Steve Chamberlain (sac@cygnus.com).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
35 #include "gdb/callback.h"
36 #include "gdb/remote-sim.h"
39 #include "sim-regno.h"
40 #include "arch-utils.h"
41 #include "readline/readline.h"
42 #include "gdbthread.h"
43 #include "common/byte-vector.h"
47 static void init_callbacks (void);
49 static void end_callbacks (void);
51 static int gdb_os_write_stdout (host_callback *, const char *, int);
53 static void gdb_os_flush_stdout (host_callback *);
55 static int gdb_os_write_stderr (host_callback *, const char *, int);
57 static void gdb_os_flush_stderr (host_callback *);
59 static int gdb_os_poll_quit (host_callback *);
61 /* printf_filtered is depreciated. */
62 static void gdb_os_printf_filtered (host_callback *, const char *, ...);
64 static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list);
66 static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list);
68 static void gdb_os_error (host_callback *, const char *, ...)
71 void simulator_command (char *args, int from_tty);
75 sim_* are the interface to the simulator (see remote-sim.h).
76 gdbsim_* are stuff which is internal to gdb. */
78 static const target_info gdbsim_target_info = {
81 N_("Use the compiled-in simulator.")
84 struct gdbsim_target final
85 : public memory_breakpoint_target<target_ops>
88 { to_stratum = process_stratum; }
90 const target_info &info () const override
91 { return gdbsim_target_info; }
93 void close () override;
95 void detach (inferior *inf, int) override;
97 void resume (ptid_t, int, enum gdb_signal) override;
98 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
100 void fetch_registers (struct regcache *, int) override;
101 void store_registers (struct regcache *, int) override;
102 void prepare_to_store (struct regcache *) override;
104 enum target_xfer_status xfer_partial (enum target_object object,
107 const gdb_byte *writebuf,
108 ULONGEST offset, ULONGEST len,
109 ULONGEST *xfered_len) override;
111 void files_info () override;
113 void kill () override;
115 void load (const char *, int) override;
117 bool can_create_inferior () override { return true; }
118 void create_inferior (const char *, const std::string &,
119 char **, int) override;
121 void mourn_inferior () override;
123 void interrupt () override;
125 bool thread_alive (ptid_t ptid) override;
127 const char *pid_to_str (ptid_t) override;
129 bool has_all_memory () override;
130 bool has_memory () override;
132 bool has_stack () override
133 { return default_child_has_stack (); }
135 bool has_registers () override
136 { return default_child_has_registers (); }
138 bool has_execution (ptid_t ptid) override
139 { return default_child_has_execution (ptid); }
142 static struct gdbsim_target gdbsim_ops;
144 static const struct inferior_data *sim_inferior_data_key;
146 /* Simulator-specific, per-inferior state. */
147 struct sim_inferior_data {
148 /* Flag which indicates whether or not the program has been loaded. */
151 /* Simulator descriptor for this inferior. */
152 SIM_DESC gdbsim_desc;
154 /* This is the ptid we use for this particular simulator instance. Its
155 value is somewhat arbitrary, as the simulator target don't have a
156 notion of tasks or threads, but we need something non-null to place
157 in inferior_ptid. For simulators which permit multiple instances,
158 we also need a unique identifier to use for each inferior. */
159 ptid_t remote_sim_ptid;
161 /* Signal with which to resume. */
162 enum gdb_signal resume_siggnal;
164 /* Flag which indicates whether resume should step or not. */
168 /* Flag indicating the "open" status of this module. It's set to 1
169 in gdbsim_open() and 0 in gdbsim_close(). */
170 static int gdbsim_is_open = 0;
172 /* Value of the next pid to allocate for an inferior. As indicated
173 elsewhere, its initial value is somewhat arbitrary; it's critical
174 though that it's not zero or negative. */
176 #define INITIAL_PID 42000
178 /* Argument list to pass to sim_open(). It is allocated in gdbsim_open()
179 and deallocated in gdbsim_close(). The lifetime needs to extend beyond
180 the call to gdbsim_open() due to the fact that other sim instances other
181 than the first will be allocated after the gdbsim_open() call. */
182 static char **sim_argv = NULL;
184 /* OS-level callback functions for write, flush, etc. */
185 static host_callback gdb_callback;
186 static int callbacks_initialized = 0;
188 /* Callback for iterate_over_inferiors. It checks to see if the sim
189 descriptor passed via ARG is the same as that for the inferior
190 designated by INF. Return true if so; false otherwise. */
193 check_for_duplicate_sim_descriptor (struct inferior *inf, void *arg)
195 struct sim_inferior_data *sim_data;
196 SIM_DESC new_sim_desc = (SIM_DESC) arg;
198 sim_data = ((struct sim_inferior_data *)
199 inferior_data (inf, sim_inferior_data_key));
201 return (sim_data != NULL && sim_data->gdbsim_desc == new_sim_desc);
204 /* Flags indicating whether or not a sim instance is needed. One of these
205 flags should be passed to get_sim_inferior_data(). */
207 enum {SIM_INSTANCE_NOT_NEEDED = 0, SIM_INSTANCE_NEEDED = 1};
209 /* Obtain pointer to per-inferior simulator data, allocating it if necessary.
210 Attempt to open the sim if SIM_INSTANCE_NEEDED is true. */
212 static struct sim_inferior_data *
213 get_sim_inferior_data (struct inferior *inf, int sim_instance_needed)
215 SIM_DESC sim_desc = NULL;
216 struct sim_inferior_data *sim_data
217 = (struct sim_inferior_data *) inferior_data (inf, sim_inferior_data_key);
219 /* Try to allocate a new sim instance, if needed. We do this ahead of
220 a potential allocation of a sim_inferior_data struct in order to
221 avoid needlessly allocating that struct in the event that the sim
222 instance allocation fails. */
223 if (sim_instance_needed == SIM_INSTANCE_NEEDED
224 && (sim_data == NULL || sim_data->gdbsim_desc == NULL))
226 struct inferior *idup;
227 sim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, sim_argv);
228 if (sim_desc == NULL)
229 error (_("Unable to create simulator instance for inferior %d."),
232 idup = iterate_over_inferiors (check_for_duplicate_sim_descriptor,
236 /* We don't close the descriptor due to the fact that it's
237 shared with some other inferior. If we were to close it,
238 that might needlessly muck up the other inferior. Of
239 course, it's possible that the damage has already been
240 done... Note that it *will* ultimately be closed during
241 cleanup of the other inferior. */
244 _("Inferior %d and inferior %d would have identical simulator state.\n"
245 "(This simulator does not support the running of more than one inferior.)"),
246 inf->num, idup->num);
250 if (sim_data == NULL)
252 sim_data = XCNEW(struct sim_inferior_data);
253 set_inferior_data (inf, sim_inferior_data_key, sim_data);
255 /* Allocate a ptid for this inferior. */
256 sim_data->remote_sim_ptid = ptid_t (next_pid, 0, next_pid);
259 /* Initialize the other instance variables. */
260 sim_data->program_loaded = 0;
261 sim_data->gdbsim_desc = sim_desc;
262 sim_data->resume_siggnal = GDB_SIGNAL_0;
263 sim_data->resume_step = 0;
267 /* This handles the case where sim_data was allocated prior to
268 needing a sim instance. */
269 sim_data->gdbsim_desc = sim_desc;
276 /* Return pointer to per-inferior simulator data using PTID to find the
277 inferior in question. Return NULL when no inferior is found or
278 when ptid has a zero or negative pid component. */
280 static struct sim_inferior_data *
281 get_sim_inferior_data_by_ptid (ptid_t ptid, int sim_instance_needed)
283 struct inferior *inf;
284 int pid = ptid.pid ();
289 inf = find_inferior_pid (pid);
292 return get_sim_inferior_data (inf, sim_instance_needed);
297 /* Free the per-inferior simulator data. */
300 sim_inferior_data_cleanup (struct inferior *inf, void *data)
302 struct sim_inferior_data *sim_data = (struct sim_inferior_data *) data;
304 if (sim_data != NULL)
306 if (sim_data->gdbsim_desc)
308 sim_close (sim_data->gdbsim_desc, 0);
309 sim_data->gdbsim_desc = NULL;
316 dump_mem (const gdb_byte *buf, int len)
318 fputs_unfiltered ("\t", gdb_stdlog);
320 if (len == 8 || len == 4)
324 memcpy (l, buf, len);
325 fprintf_unfiltered (gdb_stdlog, "0x%08x", l[0]);
327 fprintf_unfiltered (gdb_stdlog, " 0x%08x", l[1]);
333 for (i = 0; i < len; i++)
334 fprintf_unfiltered (gdb_stdlog, "0x%02x ", buf[i]);
337 fputs_unfiltered ("\n", gdb_stdlog);
340 /* Initialize gdb_callback. */
343 init_callbacks (void)
345 if (!callbacks_initialized)
347 gdb_callback = default_callback;
348 gdb_callback.init (&gdb_callback);
349 gdb_callback.write_stdout = gdb_os_write_stdout;
350 gdb_callback.flush_stdout = gdb_os_flush_stdout;
351 gdb_callback.write_stderr = gdb_os_write_stderr;
352 gdb_callback.flush_stderr = gdb_os_flush_stderr;
353 gdb_callback.printf_filtered = gdb_os_printf_filtered;
354 gdb_callback.vprintf_filtered = gdb_os_vprintf_filtered;
355 gdb_callback.evprintf_filtered = gdb_os_evprintf_filtered;
356 gdb_callback.error = gdb_os_error;
357 gdb_callback.poll_quit = gdb_os_poll_quit;
358 gdb_callback.magic = HOST_CALLBACK_MAGIC;
359 callbacks_initialized = 1;
363 /* Release callbacks (free resources used by them). */
368 if (callbacks_initialized)
370 gdb_callback.shutdown (&gdb_callback);
371 callbacks_initialized = 0;
375 /* GDB version of os_write_stdout callback. */
378 gdb_os_write_stdout (host_callback *p, const char *buf, int len)
383 ui_file_write (gdb_stdtarg, buf, len);
387 /* GDB version of os_flush_stdout callback. */
390 gdb_os_flush_stdout (host_callback *p)
392 gdb_flush (gdb_stdtarg);
395 /* GDB version of os_write_stderr callback. */
398 gdb_os_write_stderr (host_callback *p, const char *buf, int len)
403 for (i = 0; i < len; i++)
407 fputs_unfiltered (b, gdb_stdtargerr);
412 /* GDB version of os_flush_stderr callback. */
415 gdb_os_flush_stderr (host_callback *p)
417 gdb_flush (gdb_stdtargerr);
420 /* GDB version of printf_filtered callback. */
422 static void ATTRIBUTE_PRINTF (2, 3)
423 gdb_os_printf_filtered (host_callback * p, const char *format, ...)
427 va_start (args, format);
428 vfprintf_filtered (gdb_stdout, format, args);
432 /* GDB version of error vprintf_filtered. */
434 static void ATTRIBUTE_PRINTF (2, 0)
435 gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap)
437 vfprintf_filtered (gdb_stdout, format, ap);
440 /* GDB version of error evprintf_filtered. */
442 static void ATTRIBUTE_PRINTF (2, 0)
443 gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap)
445 vfprintf_filtered (gdb_stderr, format, ap);
448 /* GDB version of error callback. */
450 static void ATTRIBUTE_PRINTF (2, 3)
451 gdb_os_error (host_callback * p, const char *format, ...)
455 va_start (args, format);
456 verror (format, args);
461 one2one_register_sim_regno (struct gdbarch *gdbarch, int regnum)
463 /* Only makes sense to supply raw registers. */
464 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
469 gdbsim_target::fetch_registers (struct regcache *regcache, int regno)
471 struct gdbarch *gdbarch = regcache->arch ();
472 struct inferior *inf = find_inferior_ptid (regcache->ptid ());
473 struct sim_inferior_data *sim_data
474 = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
478 for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++)
479 fetch_registers (regcache, regno);
483 switch (gdbarch_register_sim_regno (gdbarch, regno))
485 case LEGACY_SIM_REGNO_IGNORE:
487 case SIM_REGNO_DOES_NOT_EXIST:
489 /* For moment treat a `does not exist' register the same way
490 as an ``unavailable'' register. */
491 regcache->raw_supply_zeroed (regno);
497 static int warn_user = 1;
498 int regsize = register_size (gdbarch, regno);
499 gdb::byte_vector buf (regsize, 0);
502 gdb_assert (regno >= 0 && regno < gdbarch_num_regs (gdbarch));
503 nr_bytes = sim_fetch_register (sim_data->gdbsim_desc,
504 gdbarch_register_sim_regno
506 buf.data (), regsize);
507 if (nr_bytes > 0 && nr_bytes != regsize && warn_user)
509 fprintf_unfiltered (gdb_stderr,
510 "Size of register %s (%d/%d) "
511 "incorrect (%d instead of %d))",
512 gdbarch_register_name (gdbarch, regno),
514 gdbarch_register_sim_regno (gdbarch, regno),
518 /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
519 indicating that GDB and the SIM have different ideas about
520 which registers are fetchable. */
521 /* Else if (nr_bytes < 0): an old simulator, that doesn't
522 think to return the register size. Just assume all is ok. */
523 regcache->raw_supply (regno, buf.data ());
526 fprintf_unfiltered (gdb_stdlog,
527 "gdbsim_fetch_register: %d", regno);
528 /* FIXME: We could print something more intelligible. */
529 dump_mem (buf.data (), regsize);
538 gdbsim_target::store_registers (struct regcache *regcache, int regno)
540 struct gdbarch *gdbarch = regcache->arch ();
541 struct inferior *inf = find_inferior_ptid (regcache->ptid ());
542 struct sim_inferior_data *sim_data
543 = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
547 for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++)
548 store_registers (regcache, regno);
551 else if (gdbarch_register_sim_regno (gdbarch, regno) >= 0)
553 int regsize = register_size (gdbarch, regno);
554 gdb::byte_vector tmp (regsize);
557 regcache->cooked_read (regno, tmp.data ());
558 nr_bytes = sim_store_register (sim_data->gdbsim_desc,
559 gdbarch_register_sim_regno
561 tmp.data (), regsize);
563 if (nr_bytes > 0 && nr_bytes != regsize)
564 internal_error (__FILE__, __LINE__,
565 _("Register size different to expected"));
567 internal_error (__FILE__, __LINE__,
568 _("Register %d not updated"), regno);
570 warning (_("Register %s not updated"),
571 gdbarch_register_name (gdbarch, regno));
575 fprintf_unfiltered (gdb_stdlog, "gdbsim_store_register: %d", regno);
576 /* FIXME: We could print something more intelligible. */
577 dump_mem (tmp.data (), regsize);
582 /* Kill the running program. This may involve closing any open files
583 and releasing other resources acquired by the simulated program. */
586 gdbsim_target::kill ()
589 fprintf_unfiltered (gdb_stdlog, "gdbsim_kill\n");
591 /* There is no need to `kill' running simulator - the simulator is
592 not running. Mourning it is enough. */
593 target_mourn_inferior (inferior_ptid);
596 /* Load an executable file into the target process. This is expected to
597 not only bring new code into the target process, but also to update
598 GDB's symbol tables to match. */
601 gdbsim_target::load (const char *args, int fromtty)
604 struct sim_inferior_data *sim_data
605 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
608 error_no_arg (_("program to load"));
610 gdb_argv argv (args);
612 prog = tilde_expand (argv[0]);
615 error (_("GDB sim does not yet support a load offset."));
618 fprintf_unfiltered (gdb_stdlog, "gdbsim_load: prog \"%s\"\n", prog);
620 /* FIXME: We will print two messages on error.
621 Need error to either not print anything if passed NULL or need
622 another routine that doesn't take any arguments. */
623 if (sim_load (sim_data->gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL)
624 error (_("unable to load program"));
626 /* FIXME: If a load command should reset the targets registers then
627 a call to sim_create_inferior() should go here. */
629 sim_data->program_loaded = 1;
633 /* Start an inferior process and set inferior_ptid to its pid.
634 EXEC_FILE is the file to run.
635 ARGS is a string containing the arguments to the program.
636 ENV is the environment vector to pass. Errors reported with error().
637 On VxWorks and various standalone systems, we ignore exec_file. */
638 /* This is called not only when we first attach, but also when the
639 user types "run" after having attached. */
642 gdbsim_target::create_inferior (const char *exec_file,
643 const std::string &allargs,
644 char **env, int from_tty)
646 struct sim_inferior_data *sim_data
647 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
650 const char *args = allargs.c_str ();
652 if (exec_file == 0 || exec_bfd == 0)
653 warning (_("No executable file specified."));
654 if (!sim_data->program_loaded)
655 warning (_("No program loaded."));
658 fprintf_unfiltered (gdb_stdlog,
659 "gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
660 (exec_file ? exec_file : "(NULL)"),
663 if (inferior_ptid == sim_data->remote_sim_ptid)
665 remove_breakpoints ();
666 init_wait_for_inferior ();
669 if (exec_file != NULL)
671 len = strlen (exec_file) + 1 + allargs.size () + 1 + /*slop */ 10;
672 arg_buf = (char *) alloca (len);
674 strcat (arg_buf, exec_file);
675 strcat (arg_buf, " ");
676 strcat (arg_buf, args);
677 built_argv.reset (arg_buf);
680 if (!have_inferiors ())
683 if (sim_create_inferior (sim_data->gdbsim_desc, exec_bfd,
684 built_argv.get (), env)
686 error (_("Unable to create sim inferior."));
688 inferior_ptid = sim_data->remote_sim_ptid;
689 inferior_appeared (current_inferior (), inferior_ptid.pid ());
690 add_thread_silent (inferior_ptid);
692 insert_breakpoints (); /* Needed to get correct instruction
695 clear_proceed_status (0);
698 /* The open routine takes the rest of the parameters from the command,
699 and (if successful) pushes a new target onto the stack.
700 Targets should supply this routine, if only to provide an error message. */
701 /* Called when selecting the simulator. E.g. (gdb) target sim name. */
704 gdbsim_target_open (const char *args, int from_tty)
708 struct sim_inferior_data *sim_data;
710 SIM_DESC gdbsim_desc;
712 sysroot = gdb_sysroot;
713 if (is_target_filename (sysroot))
714 sysroot += strlen (TARGET_SYSROOT_PREFIX);
717 fprintf_unfiltered (gdb_stdlog,
718 "gdbsim_open: args \"%s\"\n", args ? args : "(null)");
720 /* Ensure that the sim target is not on the target stack. This is
721 necessary, because if it is on the target stack, the call to
722 push_target below will invoke sim_close(), thus freeing various
723 state (including a sim instance) that we allocate prior to
724 invoking push_target(). We want to delay the push_target()
725 operation until after we complete those operations which could
728 unpush_target (&gdbsim_ops);
730 len = (7 + 1 /* gdbsim */
731 + strlen (" -E little")
732 + strlen (" --architecture=xxxxxxxxxx")
733 + strlen (" --sysroot=") + strlen (sysroot) +
734 + (args ? strlen (args) : 0)
736 arg_buf = (char *) alloca (len);
737 strcpy (arg_buf, "gdbsim"); /* 7 */
738 /* Specify the byte order for the target when it is explicitly
739 specified by the user (not auto detected). */
740 switch (selected_byte_order ())
743 strcat (arg_buf, " -E big");
745 case BFD_ENDIAN_LITTLE:
746 strcat (arg_buf, " -E little");
748 case BFD_ENDIAN_UNKNOWN:
751 /* Specify the architecture of the target when it has been
752 explicitly specified */
753 if (selected_architecture_name () != NULL)
755 strcat (arg_buf, " --architecture=");
756 strcat (arg_buf, selected_architecture_name ());
758 /* Pass along gdb's concept of the sysroot. */
759 strcat (arg_buf, " --sysroot=");
760 strcat (arg_buf, sysroot);
761 /* finally, any explicit args */
764 strcat (arg_buf, " "); /* 1 */
765 strcat (arg_buf, args);
768 gdb_argv argv (arg_buf);
769 sim_argv = argv.get ();
772 gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, sim_argv);
774 if (gdbsim_desc == 0)
777 error (_("unable to create simulator instance"));
782 /* Reset the pid numberings for this batch of sim instances. */
783 next_pid = INITIAL_PID;
785 /* Allocate the inferior data, but do not allocate a sim instance
786 since we've already just done that. */
787 sim_data = get_sim_inferior_data (current_inferior (),
788 SIM_INSTANCE_NOT_NEEDED);
790 sim_data->gdbsim_desc = gdbsim_desc;
792 push_target (&gdbsim_ops);
793 printf_filtered ("Connected to the simulator.\n");
795 /* There's nothing running after "target sim" or "load"; not until
797 inferior_ptid = null_ptid;
802 /* Callback for iterate_over_inferiors. Called (indirectly) by
806 gdbsim_close_inferior (struct inferior *inf, void *arg)
808 struct sim_inferior_data *sim_data
809 = (struct sim_inferior_data *) inferior_data (inf, sim_inferior_data_key);
810 if (sim_data != NULL)
812 ptid_t ptid = sim_data->remote_sim_ptid;
814 sim_inferior_data_cleanup (inf, sim_data);
815 set_inferior_data (inf, sim_inferior_data_key, NULL);
817 /* Having a ptid allocated and stored in remote_sim_ptid does
818 not mean that a corresponding inferior was ever created.
819 Thus we need to verify the existence of an inferior using the
820 pid in question before setting inferior_ptid via
821 switch_to_thread() or mourning the inferior. */
822 if (find_inferior_ptid (ptid) != NULL)
824 switch_to_thread (ptid);
825 generic_mourn_inferior ();
832 /* Close out all files and local state before this target loses control. */
835 gdbsim_target::close ()
837 struct sim_inferior_data *sim_data
838 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
841 fprintf_unfiltered (gdb_stdlog, "gdbsim_close\n");
843 iterate_over_inferiors (gdbsim_close_inferior, NULL);
845 if (sim_argv != NULL)
856 /* Takes a program previously attached to and detaches it.
857 The program may resume execution (some targets do, some don't) and will
858 no longer stop on signals, etc. We better not have left any breakpoints
859 in the program or it'll die when it hits one. FROM_TTY says whether to be
861 /* Terminate the open connection to the remote debugger.
862 Use this when you want to detach and do something else with your gdb. */
865 gdbsim_target::detach (inferior *inf, int from_tty)
868 fprintf_unfiltered (gdb_stdlog, "gdbsim_detach\n");
870 unpush_target (this); /* calls gdbsim_close to do the real work */
872 printf_filtered ("Ending simulator %s debugging\n", target_shortname);
875 /* Resume execution of the target process. STEP says whether to single-step
876 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
877 to the target, or zero for no signal. */
881 enum gdb_signal siggnal;
886 gdbsim_resume_inferior (struct inferior *inf, void *arg)
888 struct sim_inferior_data *sim_data
889 = get_sim_inferior_data (inf, SIM_INSTANCE_NOT_NEEDED);
890 struct resume_data *rd = (struct resume_data *) arg;
894 sim_data->resume_siggnal = rd->siggnal;
895 sim_data->resume_step = rd->step;
898 fprintf_unfiltered (gdb_stdlog,
899 _("gdbsim_resume: pid %d, step %d, signal %d\n"),
900 inf->pid, rd->step, rd->siggnal);
903 /* When called from iterate_over_inferiors, a zero return causes the
904 iteration process to proceed until there are no more inferiors to
910 gdbsim_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
912 struct resume_data rd;
913 struct sim_inferior_data *sim_data
914 = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);
916 rd.siggnal = siggnal;
919 /* We don't access any sim_data members within this function.
920 What's of interest is whether or not the call to
921 get_sim_inferior_data_by_ptid(), above, is able to obtain a
922 non-NULL pointer. If it managed to obtain a non-NULL pointer, we
923 know we have a single inferior to consider. If it's NULL, we
924 either have multiple inferiors to resume or an error condition. */
927 gdbsim_resume_inferior (find_inferior_ptid (ptid), &rd);
928 else if (ptid == minus_one_ptid)
929 iterate_over_inferiors (gdbsim_resume_inferior, &rd);
931 error (_("The program is not being run."));
934 /* Notify the simulator of an asynchronous request to interrupt.
936 The simulator shall ensure that the interrupt request is eventually
937 delivered to the simulator. If the call is made while the
938 simulator is not running then the interrupt request is processed when
939 the simulator is next resumed.
941 For simulators that do not support this operation, just abort. */
944 gdbsim_interrupt_inferior (struct inferior *inf, void *arg)
946 struct sim_inferior_data *sim_data
947 = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
951 if (!sim_stop (sim_data->gdbsim_desc))
957 /* When called from iterate_over_inferiors, a zero return causes the
958 iteration process to proceed until there are no more inferiors to
964 gdbsim_target::interrupt ()
966 iterate_over_inferiors (gdbsim_interrupt_inferior, NULL);
969 /* GDB version of os_poll_quit callback.
970 Taken from gdb/util.c - should be in a library. */
973 gdb_os_poll_quit (host_callback *p)
975 if (deprecated_ui_loop_hook != NULL)
976 deprecated_ui_loop_hook (0);
978 if (check_quit_flag ()) /* gdb's idea of quit */
983 /* Wait for inferior process to do something. Return pid of child,
984 or -1 in case of error; store status through argument pointer STATUS,
985 just as `wait' would. */
988 gdbsim_cntrl_c (int signo)
990 gdbsim_ops.interrupt ();
994 gdbsim_target::wait (ptid_t ptid, struct target_waitstatus *status, int options)
996 struct sim_inferior_data *sim_data;
997 static sighandler_t prev_sigint;
999 enum sim_stop reason = sim_running;
1001 /* This target isn't able to (yet) resume more than one inferior at a time.
1002 When ptid is minus_one_ptid, just use the current inferior. If we're
1003 given an explicit pid, we'll try to find it and use that instead. */
1004 if (ptid == minus_one_ptid)
1005 sim_data = get_sim_inferior_data (current_inferior (),
1006 SIM_INSTANCE_NEEDED);
1009 sim_data = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NEEDED);
1010 if (sim_data == NULL)
1011 error (_("Unable to wait for pid %d. Inferior not found."),
1013 inferior_ptid = ptid;
1017 fprintf_unfiltered (gdb_stdlog, "gdbsim_wait\n");
1019 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1021 struct sigaction sa, osa;
1022 sa.sa_handler = gdbsim_cntrl_c;
1023 sigemptyset (&sa.sa_mask);
1025 sigaction (SIGINT, &sa, &osa);
1026 prev_sigint = osa.sa_handler;
1029 prev_sigint = signal (SIGINT, gdbsim_cntrl_c);
1031 sim_resume (sim_data->gdbsim_desc, sim_data->resume_step,
1032 sim_data->resume_siggnal);
1034 signal (SIGINT, prev_sigint);
1035 sim_data->resume_step = 0;
1037 sim_stop_reason (sim_data->gdbsim_desc, &reason, &sigrc);
1042 status->kind = TARGET_WAITKIND_EXITED;
1043 status->value.integer = sigrc;
1048 case GDB_SIGNAL_ABRT:
1051 case GDB_SIGNAL_INT:
1052 case GDB_SIGNAL_TRAP:
1054 status->kind = TARGET_WAITKIND_STOPPED;
1055 status->value.sig = (enum gdb_signal) sigrc;
1060 status->kind = TARGET_WAITKIND_SIGNALLED;
1061 status->value.sig = (enum gdb_signal) sigrc;
1065 /* FIXME: Is this correct? */
1069 return inferior_ptid;
1072 /* Get ready to modify the registers array. On machines which store
1073 individual registers, this doesn't need to do anything. On machines
1074 which store all the registers in one fell swoop, this makes sure
1075 that registers contains all the registers from the program being
1079 gdbsim_target::prepare_to_store (struct regcache *regcache)
1081 /* Do nothing, since we can store individual regs. */
1084 /* Helper for gdbsim_xfer_partial that handles memory transfers.
1085 Arguments are like target_xfer_partial. */
1087 static enum target_xfer_status
1088 gdbsim_xfer_memory (struct target_ops *target,
1089 gdb_byte *readbuf, const gdb_byte *writebuf,
1090 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
1092 struct sim_inferior_data *sim_data
1093 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
1096 /* If this target doesn't have memory yet, return 0 causing the
1097 request to be passed to a lower target, hopefully an exec
1099 if (!target->has_memory ())
1100 return TARGET_XFER_EOF;
1102 if (!sim_data->program_loaded)
1103 error (_("No program loaded."));
1105 /* Note that we obtained the sim_data pointer above using
1106 SIM_INSTANCE_NOT_NEEDED. We do this so that we don't needlessly
1107 allocate a sim instance prior to loading a program. If we
1108 get to this point in the code though, gdbsim_desc should be
1109 non-NULL. (Note that a sim instance is needed in order to load
1111 gdb_assert (sim_data->gdbsim_desc != NULL);
1114 fprintf_unfiltered (gdb_stdlog,
1115 "gdbsim_xfer_memory: readbuf %s, writebuf %s, "
1116 "memaddr %s, len %s\n",
1117 host_address_to_string (readbuf),
1118 host_address_to_string (writebuf),
1119 paddress (target_gdbarch (), memaddr),
1124 if (remote_debug && len > 0)
1125 dump_mem (writebuf, len);
1126 l = sim_write (sim_data->gdbsim_desc, memaddr, writebuf, len);
1130 l = sim_read (sim_data->gdbsim_desc, memaddr, readbuf, len);
1131 if (remote_debug && len > 0)
1132 dump_mem (readbuf, len);
1136 *xfered_len = (ULONGEST) l;
1137 return TARGET_XFER_OK;
1140 return TARGET_XFER_EOF;
1142 return TARGET_XFER_E_IO;
1145 /* Target to_xfer_partial implementation. */
1147 enum target_xfer_status
1148 gdbsim_target::xfer_partial (enum target_object object,
1149 const char *annex, gdb_byte *readbuf,
1150 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
1151 ULONGEST *xfered_len)
1155 case TARGET_OBJECT_MEMORY:
1156 return gdbsim_xfer_memory (this, readbuf, writebuf, offset, len,
1160 return TARGET_XFER_E_IO;
1165 gdbsim_target::files_info ()
1167 struct sim_inferior_data *sim_data
1168 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
1169 const char *file = "nothing";
1172 file = bfd_get_filename (exec_bfd);
1175 fprintf_unfiltered (gdb_stdlog, "gdbsim_files_info: file \"%s\"\n", file);
1179 fprintf_unfiltered (gdb_stdlog, "\tAttached to %s running program %s\n",
1180 target_shortname, file);
1181 sim_info (sim_data->gdbsim_desc, 0);
1185 /* Clear the simulator's notion of what the break points are. */
1188 gdbsim_target::mourn_inferior ()
1190 struct sim_inferior_data *sim_data
1191 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
1194 fprintf_unfiltered (gdb_stdlog, "gdbsim_mourn_inferior:\n");
1196 remove_breakpoints ();
1197 generic_mourn_inferior ();
1200 /* Pass the command argument through to the simulator verbatim. The
1201 simulator must do any command interpretation work. */
1204 simulator_command (const char *args, int from_tty)
1206 struct sim_inferior_data *sim_data;
1208 /* We use inferior_data() instead of get_sim_inferior_data() here in
1209 order to avoid attaching a sim_inferior_data struct to an
1210 inferior unnecessarily. The reason we take such care here is due
1211 to the fact that this function, simulator_command(), may be called
1212 even when the sim target is not active. If we were to use
1213 get_sim_inferior_data() here, it is possible that this call would
1214 be made either prior to gdbsim_open() or after gdbsim_close(),
1215 thus allocating memory that would not be garbage collected until
1216 the ultimate destruction of the associated inferior. */
1218 sim_data = ((struct sim_inferior_data *)
1219 inferior_data (current_inferior (), sim_inferior_data_key));
1220 if (sim_data == NULL || sim_data->gdbsim_desc == NULL)
1223 /* PREVIOUSLY: The user may give a command before the simulator
1224 is opened. [...] (??? assuming of course one wishes to
1225 continue to allow commands to be sent to unopened simulators,
1226 which isn't entirely unreasonable). */
1228 /* The simulator is a builtin abstraction of a remote target.
1229 Consistent with that model, access to the simulator, via sim
1230 commands, is restricted to the period when the channel to the
1231 simulator is open. */
1233 error (_("Not connected to the simulator target"));
1236 sim_do_command (sim_data->gdbsim_desc, args);
1238 /* Invalidate the register cache, in case the simulator command does
1240 registers_changed ();
1244 sim_command_completer (struct cmd_list_element *ignore,
1245 completion_tracker &tracker,
1246 const char *text, const char *word)
1248 struct sim_inferior_data *sim_data;
1250 sim_data = ((struct sim_inferior_data *)
1251 inferior_data (current_inferior (), sim_inferior_data_key));
1252 if (sim_data == NULL || sim_data->gdbsim_desc == NULL)
1255 /* sim_complete_command returns a NULL-terminated malloc'ed array of
1256 malloc'ed strings. */
1257 struct sim_completions_deleter
1259 void operator() (char **ptr) const
1261 for (size_t i = 0; ptr[i] != NULL; i++)
1267 std::unique_ptr<char *[], sim_completions_deleter> sim_completions
1268 (sim_complete_command (sim_data->gdbsim_desc, text, word));
1269 if (sim_completions == NULL)
1272 /* Count the elements and add completions from tail to head because
1273 below we'll swap elements out of the array in case add_completion
1274 throws and the deleter deletes until it finds a NULL element. */
1276 while (sim_completions[count] != NULL)
1279 for (size_t i = count; i > 0; i--)
1281 gdb::unique_xmalloc_ptr<char> match (sim_completions[i - 1]);
1282 sim_completions[i - 1] = NULL;
1283 tracker.add_completion (std::move (match));
1287 /* Check to see if a thread is still alive. */
1290 gdbsim_target::thread_alive (ptid_t ptid)
1292 struct sim_inferior_data *sim_data
1293 = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);
1295 if (sim_data == NULL)
1298 if (ptid == sim_data->remote_sim_ptid)
1299 /* The simulators' task is always alive. */
1305 /* Convert a thread ID to a string. Returns the string in a static
1309 gdbsim_target::pid_to_str (ptid_t ptid)
1311 return normal_pid_to_str (ptid);
1314 /* Simulator memory may be accessed after the program has been loaded. */
1317 gdbsim_target::has_all_memory ()
1319 struct sim_inferior_data *sim_data
1320 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
1322 if (!sim_data->program_loaded)
1329 gdbsim_target::has_memory ()
1331 struct sim_inferior_data *sim_data
1332 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
1334 if (!sim_data->program_loaded)
1341 _initialize_remote_sim (void)
1343 struct cmd_list_element *c;
1345 add_target (gdbsim_target_info, gdbsim_target_open);
1347 c = add_com ("sim", class_obscure, simulator_command,
1348 _("Send a command to the simulator."));
1349 set_cmd_completer (c, sim_command_completer);
1351 sim_inferior_data_key
1352 = register_inferior_data_with_cleanup (NULL, sim_inferior_data_cleanup);