1 /* Generic remote debugging interface for simulators.
3 Copyright (C) 1993-2020 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/>. */
34 #include "process-stratum-target.h"
36 #include "gdb/callback.h"
37 #include "gdb/remote-sim.h"
40 #include "sim-regno.h"
41 #include "arch-utils.h"
42 #include "readline/readline.h"
43 #include "gdbthread.h"
44 #include "gdbsupport/byte-vector.h"
48 static void init_callbacks (void);
50 static void end_callbacks (void);
52 static int gdb_os_write_stdout (host_callback
*, const char *, int);
54 static void gdb_os_flush_stdout (host_callback
*);
56 static int gdb_os_write_stderr (host_callback
*, const char *, int);
58 static void gdb_os_flush_stderr (host_callback
*);
60 static int gdb_os_poll_quit (host_callback
*);
62 /* printf_filtered is depreciated. */
63 static void gdb_os_printf_filtered (host_callback
*, const char *, ...);
65 static void gdb_os_vprintf_filtered (host_callback
*, const char *, va_list);
67 static void gdb_os_evprintf_filtered (host_callback
*, const char *, va_list);
69 static void gdb_os_error (host_callback
*, const char *, ...)
74 sim_* are the interface to the simulator (see remote-sim.h).
75 gdbsim_* are stuff which is internal to gdb. */
77 /* Value of the next pid to allocate for an inferior. As indicated
78 elsewhere, its initial value is somewhat arbitrary; it's critical
79 though that it's not zero or negative. */
81 #define INITIAL_PID 42000
83 /* Simulator-specific, per-inferior state. */
84 struct sim_inferior_data
{
85 explicit sim_inferior_data (SIM_DESC desc
)
87 remote_sim_ptid (next_pid
, 0, next_pid
)
92 ~sim_inferior_data ();
94 /* Flag which indicates whether or not the program has been loaded. */
95 int program_loaded
= 0;
97 /* Simulator descriptor for this inferior. */
100 /* This is the ptid we use for this particular simulator instance. Its
101 value is somewhat arbitrary, as the simulator target don't have a
102 notion of tasks or threads, but we need something non-null to place
103 in inferior_ptid. For simulators which permit multiple instances,
104 we also need a unique identifier to use for each inferior. */
105 ptid_t remote_sim_ptid
;
107 /* Signal with which to resume. */
108 enum gdb_signal resume_siggnal
= GDB_SIGNAL_0
;
110 /* Flag which indicates whether resume should step or not. */
114 static const target_info gdbsim_target_info
= {
117 N_("Use the compiled-in simulator.")
120 struct gdbsim_target final
121 : public memory_breakpoint_target
<process_stratum_target
>
123 gdbsim_target () = default;
125 const target_info
&info () const override
126 { return gdbsim_target_info
; }
128 void close () override
;
130 void detach (inferior
*inf
, int) override
;
132 void resume (ptid_t
, int, enum gdb_signal
) override
;
133 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
135 void fetch_registers (struct regcache
*, int) override
;
136 void store_registers (struct regcache
*, int) override
;
137 void prepare_to_store (struct regcache
*) override
;
139 enum target_xfer_status
xfer_partial (enum target_object object
,
142 const gdb_byte
*writebuf
,
143 ULONGEST offset
, ULONGEST len
,
144 ULONGEST
*xfered_len
) override
;
146 void files_info () override
;
148 void kill () override
;
150 void load (const char *, int) override
;
152 bool can_create_inferior () override
{ return true; }
153 void create_inferior (const char *, const std::string
&,
154 char **, int) override
;
156 void mourn_inferior () override
;
158 void interrupt () override
;
160 bool thread_alive (ptid_t ptid
) override
;
162 std::string
pid_to_str (ptid_t
) override
;
164 bool has_all_memory () override
;
165 bool has_memory () override
;
168 sim_inferior_data
*get_inferior_data_by_ptid (ptid_t ptid
,
169 int sim_instance_needed
);
170 void resume_one_inferior (inferior
*inf
, bool step
, gdb_signal siggnal
);
171 void close_one_inferior (inferior
*inf
);
174 static struct gdbsim_target gdbsim_ops
;
176 static inferior_key
<sim_inferior_data
> sim_inferior_data_key
;
178 /* Flag indicating the "open" status of this module. It's set to 1
179 in gdbsim_open() and 0 in gdbsim_close(). */
180 static int gdbsim_is_open
= 0;
182 /* Argument list to pass to sim_open(). It is allocated in gdbsim_open()
183 and deallocated in gdbsim_close(). The lifetime needs to extend beyond
184 the call to gdbsim_open() due to the fact that other sim instances other
185 than the first will be allocated after the gdbsim_open() call. */
186 static char **sim_argv
= NULL
;
188 /* OS-level callback functions for write, flush, etc. */
189 static host_callback gdb_callback
;
190 static int callbacks_initialized
= 0;
192 /* Callback for iterate_over_inferiors. It checks to see if the sim
193 descriptor passed via ARG is the same as that for the inferior
194 designated by INF. Return true if so; false otherwise. */
197 check_for_duplicate_sim_descriptor (struct inferior
*inf
, void *arg
)
199 struct sim_inferior_data
*sim_data
;
200 SIM_DESC new_sim_desc
= (SIM_DESC
) arg
;
202 sim_data
= sim_inferior_data_key
.get (inf
);
204 return (sim_data
!= NULL
&& sim_data
->gdbsim_desc
== new_sim_desc
);
207 /* Flags indicating whether or not a sim instance is needed. One of these
208 flags should be passed to get_sim_inferior_data(). */
210 enum {SIM_INSTANCE_NOT_NEEDED
= 0, SIM_INSTANCE_NEEDED
= 1};
212 /* Obtain pointer to per-inferior simulator data, allocating it if necessary.
213 Attempt to open the sim if SIM_INSTANCE_NEEDED is true. */
215 static struct sim_inferior_data
*
216 get_sim_inferior_data (struct inferior
*inf
, int sim_instance_needed
)
218 SIM_DESC sim_desc
= NULL
;
219 struct sim_inferior_data
*sim_data
= sim_inferior_data_key
.get (inf
);
221 /* Try to allocate a new sim instance, if needed. We do this ahead of
222 a potential allocation of a sim_inferior_data struct in order to
223 avoid needlessly allocating that struct in the event that the sim
224 instance allocation fails. */
225 if (sim_instance_needed
== SIM_INSTANCE_NEEDED
226 && (sim_data
== NULL
|| sim_data
->gdbsim_desc
== NULL
))
228 struct inferior
*idup
;
229 sim_desc
= sim_open (SIM_OPEN_DEBUG
, &gdb_callback
, exec_bfd
, sim_argv
);
230 if (sim_desc
== NULL
)
231 error (_("Unable to create simulator instance for inferior %d."),
234 idup
= iterate_over_inferiors (check_for_duplicate_sim_descriptor
,
238 /* We don't close the descriptor due to the fact that it's
239 shared with some other inferior. If we were to close it,
240 that might needlessly muck up the other inferior. Of
241 course, it's possible that the damage has already been
242 done... Note that it *will* ultimately be closed during
243 cleanup of the other inferior. */
246 _("Inferior %d and inferior %d would have identical simulator state.\n"
247 "(This simulator does not support the running of more than one inferior.)"),
248 inf
->num
, idup
->num
);
252 if (sim_data
== NULL
)
254 sim_data
= sim_inferior_data_key
.emplace (inf
, sim_desc
);
258 /* This handles the case where sim_data was allocated prior to
259 needing a sim instance. */
260 sim_data
->gdbsim_desc
= sim_desc
;
267 /* Return pointer to per-inferior simulator data using PTID to find the
268 inferior in question. Return NULL when no inferior is found or
269 when ptid has a zero or negative pid component. */
272 gdbsim_target::get_inferior_data_by_ptid (ptid_t ptid
,
273 int sim_instance_needed
)
275 struct inferior
*inf
;
276 int pid
= ptid
.pid ();
281 inf
= find_inferior_pid (this, pid
);
284 return get_sim_inferior_data (inf
, sim_instance_needed
);
289 /* Free the per-inferior simulator data. */
291 sim_inferior_data::~sim_inferior_data ()
294 sim_close (gdbsim_desc
, 0);
298 dump_mem (const gdb_byte
*buf
, int len
)
300 fputs_unfiltered ("\t", gdb_stdlog
);
302 if (len
== 8 || len
== 4)
306 memcpy (l
, buf
, len
);
307 fprintf_unfiltered (gdb_stdlog
, "0x%08x", l
[0]);
309 fprintf_unfiltered (gdb_stdlog
, " 0x%08x", l
[1]);
315 for (i
= 0; i
< len
; i
++)
316 fprintf_unfiltered (gdb_stdlog
, "0x%02x ", buf
[i
]);
319 fputs_unfiltered ("\n", gdb_stdlog
);
322 /* Initialize gdb_callback. */
325 init_callbacks (void)
327 if (!callbacks_initialized
)
329 gdb_callback
= default_callback
;
330 gdb_callback
.init (&gdb_callback
);
331 gdb_callback
.write_stdout
= gdb_os_write_stdout
;
332 gdb_callback
.flush_stdout
= gdb_os_flush_stdout
;
333 gdb_callback
.write_stderr
= gdb_os_write_stderr
;
334 gdb_callback
.flush_stderr
= gdb_os_flush_stderr
;
335 gdb_callback
.printf_filtered
= gdb_os_printf_filtered
;
336 gdb_callback
.vprintf_filtered
= gdb_os_vprintf_filtered
;
337 gdb_callback
.evprintf_filtered
= gdb_os_evprintf_filtered
;
338 gdb_callback
.error
= gdb_os_error
;
339 gdb_callback
.poll_quit
= gdb_os_poll_quit
;
340 gdb_callback
.magic
= HOST_CALLBACK_MAGIC
;
341 callbacks_initialized
= 1;
345 /* Release callbacks (free resources used by them). */
350 if (callbacks_initialized
)
352 gdb_callback
.shutdown (&gdb_callback
);
353 callbacks_initialized
= 0;
357 /* GDB version of os_write_stdout callback. */
360 gdb_os_write_stdout (host_callback
*p
, const char *buf
, int len
)
362 ui_file_write (gdb_stdtarg
, buf
, len
);
366 /* GDB version of os_flush_stdout callback. */
369 gdb_os_flush_stdout (host_callback
*p
)
371 gdb_flush (gdb_stdtarg
);
374 /* GDB version of os_write_stderr callback. */
377 gdb_os_write_stderr (host_callback
*p
, const char *buf
, int len
)
382 for (i
= 0; i
< len
; i
++)
386 fputs_unfiltered (b
, gdb_stdtargerr
);
391 /* GDB version of os_flush_stderr callback. */
394 gdb_os_flush_stderr (host_callback
*p
)
396 gdb_flush (gdb_stdtargerr
);
399 /* GDB version of printf_filtered callback. */
401 static void ATTRIBUTE_PRINTF (2, 3)
402 gdb_os_printf_filtered (host_callback
* p
, const char *format
, ...)
406 va_start (args
, format
);
407 vfprintf_filtered (gdb_stdout
, format
, args
);
411 /* GDB version of error vprintf_filtered. */
413 static void ATTRIBUTE_PRINTF (2, 0)
414 gdb_os_vprintf_filtered (host_callback
* p
, const char *format
, va_list ap
)
416 vfprintf_filtered (gdb_stdout
, format
, ap
);
419 /* GDB version of error evprintf_filtered. */
421 static void ATTRIBUTE_PRINTF (2, 0)
422 gdb_os_evprintf_filtered (host_callback
* p
, const char *format
, va_list ap
)
424 vfprintf_filtered (gdb_stderr
, format
, ap
);
427 /* GDB version of error callback. */
429 static void ATTRIBUTE_PRINTF (2, 3)
430 gdb_os_error (host_callback
* p
, const char *format
, ...)
434 va_start (args
, format
);
435 verror (format
, args
);
440 one2one_register_sim_regno (struct gdbarch
*gdbarch
, int regnum
)
442 /* Only makes sense to supply raw registers. */
443 gdb_assert (regnum
>= 0 && regnum
< gdbarch_num_regs (gdbarch
));
448 gdbsim_target::fetch_registers (struct regcache
*regcache
, int regno
)
450 struct gdbarch
*gdbarch
= regcache
->arch ();
451 struct inferior
*inf
= find_inferior_ptid (this, regcache
->ptid ());
452 struct sim_inferior_data
*sim_data
453 = get_sim_inferior_data (inf
, SIM_INSTANCE_NEEDED
);
457 for (regno
= 0; regno
< gdbarch_num_regs (gdbarch
); regno
++)
458 fetch_registers (regcache
, regno
);
462 switch (gdbarch_register_sim_regno (gdbarch
, regno
))
464 case LEGACY_SIM_REGNO_IGNORE
:
466 case SIM_REGNO_DOES_NOT_EXIST
:
468 /* For moment treat a `does not exist' register the same way
469 as an ``unavailable'' register. */
470 regcache
->raw_supply_zeroed (regno
);
476 static int warn_user
= 1;
477 int regsize
= register_size (gdbarch
, regno
);
478 gdb::byte_vector
buf (regsize
, 0);
481 gdb_assert (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
));
482 nr_bytes
= sim_fetch_register (sim_data
->gdbsim_desc
,
483 gdbarch_register_sim_regno
485 buf
.data (), regsize
);
486 if (nr_bytes
> 0 && nr_bytes
!= regsize
&& warn_user
)
488 fprintf_unfiltered (gdb_stderr
,
489 "Size of register %s (%d/%d) "
490 "incorrect (%d instead of %d))",
491 gdbarch_register_name (gdbarch
, regno
),
493 gdbarch_register_sim_regno (gdbarch
, regno
),
497 /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
498 indicating that GDB and the SIM have different ideas about
499 which registers are fetchable. */
500 /* Else if (nr_bytes < 0): an old simulator, that doesn't
501 think to return the register size. Just assume all is ok. */
502 regcache
->raw_supply (regno
, buf
.data ());
505 fprintf_unfiltered (gdb_stdlog
,
506 "gdbsim_fetch_register: %d", regno
);
507 /* FIXME: We could print something more intelligible. */
508 dump_mem (buf
.data (), regsize
);
517 gdbsim_target::store_registers (struct regcache
*regcache
, int regno
)
519 struct gdbarch
*gdbarch
= regcache
->arch ();
520 struct inferior
*inf
= find_inferior_ptid (this, regcache
->ptid ());
521 struct sim_inferior_data
*sim_data
522 = get_sim_inferior_data (inf
, SIM_INSTANCE_NEEDED
);
526 for (regno
= 0; regno
< gdbarch_num_regs (gdbarch
); regno
++)
527 store_registers (regcache
, regno
);
530 else if (gdbarch_register_sim_regno (gdbarch
, regno
) >= 0)
532 int regsize
= register_size (gdbarch
, regno
);
533 gdb::byte_vector
tmp (regsize
);
536 regcache
->cooked_read (regno
, tmp
.data ());
537 nr_bytes
= sim_store_register (sim_data
->gdbsim_desc
,
538 gdbarch_register_sim_regno
540 tmp
.data (), regsize
);
542 if (nr_bytes
> 0 && nr_bytes
!= regsize
)
543 internal_error (__FILE__
, __LINE__
,
544 _("Register size different to expected"));
546 internal_error (__FILE__
, __LINE__
,
547 _("Register %d not updated"), regno
);
549 warning (_("Register %s not updated"),
550 gdbarch_register_name (gdbarch
, regno
));
554 fprintf_unfiltered (gdb_stdlog
, "gdbsim_store_register: %d", regno
);
555 /* FIXME: We could print something more intelligible. */
556 dump_mem (tmp
.data (), regsize
);
561 /* Kill the running program. This may involve closing any open files
562 and releasing other resources acquired by the simulated program. */
565 gdbsim_target::kill ()
568 fprintf_unfiltered (gdb_stdlog
, "gdbsim_kill\n");
570 /* There is no need to `kill' running simulator - the simulator is
571 not running. Mourning it is enough. */
572 target_mourn_inferior (inferior_ptid
);
575 /* Load an executable file into the target process. This is expected to
576 not only bring new code into the target process, but also to update
577 GDB's symbol tables to match. */
580 gdbsim_target::load (const char *args
, int fromtty
)
583 struct sim_inferior_data
*sim_data
584 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED
);
587 error_no_arg (_("program to load"));
589 gdb_argv
argv (args
);
591 prog
= tilde_expand (argv
[0]);
594 error (_("GDB sim does not yet support a load offset."));
597 fprintf_unfiltered (gdb_stdlog
, "gdbsim_load: prog \"%s\"\n", prog
);
599 /* FIXME: We will print two messages on error.
600 Need error to either not print anything if passed NULL or need
601 another routine that doesn't take any arguments. */
602 if (sim_load (sim_data
->gdbsim_desc
, prog
, NULL
, fromtty
) == SIM_RC_FAIL
)
603 error (_("unable to load program"));
605 /* FIXME: If a load command should reset the targets registers then
606 a call to sim_create_inferior() should go here. */
608 sim_data
->program_loaded
= 1;
612 /* Start an inferior process and set inferior_ptid to its pid.
613 EXEC_FILE is the file to run.
614 ARGS is a string containing the arguments to the program.
615 ENV is the environment vector to pass. Errors reported with error().
616 On VxWorks and various standalone systems, we ignore exec_file. */
617 /* This is called not only when we first attach, but also when the
618 user types "run" after having attached. */
621 gdbsim_target::create_inferior (const char *exec_file
,
622 const std::string
&allargs
,
623 char **env
, int from_tty
)
625 struct sim_inferior_data
*sim_data
626 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED
);
629 const char *args
= allargs
.c_str ();
631 if (exec_file
== 0 || exec_bfd
== 0)
632 warning (_("No executable file specified."));
633 if (!sim_data
->program_loaded
)
634 warning (_("No program loaded."));
637 fprintf_unfiltered (gdb_stdlog
,
638 "gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
639 (exec_file
? exec_file
: "(NULL)"),
642 if (inferior_ptid
== sim_data
->remote_sim_ptid
)
644 remove_breakpoints ();
645 init_wait_for_inferior ();
648 if (exec_file
!= NULL
)
650 len
= strlen (exec_file
) + 1 + allargs
.size () + 1 + /*slop */ 10;
651 arg_buf
= (char *) alloca (len
);
653 strcat (arg_buf
, exec_file
);
654 strcat (arg_buf
, " ");
655 strcat (arg_buf
, args
);
656 built_argv
.reset (arg_buf
);
659 if (sim_create_inferior (sim_data
->gdbsim_desc
, exec_bfd
,
660 built_argv
.get (), env
)
662 error (_("Unable to create sim inferior."));
664 inferior_ptid
= sim_data
->remote_sim_ptid
;
665 inferior_appeared (current_inferior (), inferior_ptid
.pid ());
666 add_thread_silent (this, inferior_ptid
);
668 insert_breakpoints (); /* Needed to get correct instruction
671 clear_proceed_status (0);
674 /* The open routine takes the rest of the parameters from the command,
675 and (if successful) pushes a new target onto the stack.
676 Targets should supply this routine, if only to provide an error message. */
677 /* Called when selecting the simulator. E.g. (gdb) target sim name. */
680 gdbsim_target_open (const char *args
, int from_tty
)
684 struct sim_inferior_data
*sim_data
;
686 SIM_DESC gdbsim_desc
;
688 sysroot
= gdb_sysroot
;
689 if (is_target_filename (sysroot
))
690 sysroot
+= strlen (TARGET_SYSROOT_PREFIX
);
693 fprintf_unfiltered (gdb_stdlog
,
694 "gdbsim_open: args \"%s\"\n", args
? args
: "(null)");
696 /* Ensure that the sim target is not on the target stack. This is
697 necessary, because if it is on the target stack, the call to
698 push_target below will invoke sim_close(), thus freeing various
699 state (including a sim instance) that we allocate prior to
700 invoking push_target(). We want to delay the push_target()
701 operation until after we complete those operations which could
704 unpush_target (&gdbsim_ops
);
706 len
= (7 + 1 /* gdbsim */
707 + strlen (" -E little")
708 + strlen (" --architecture=xxxxxxxxxx")
709 + strlen (" --sysroot=") + strlen (sysroot
) +
710 + (args
? strlen (args
) : 0)
712 arg_buf
= (char *) alloca (len
);
713 strcpy (arg_buf
, "gdbsim"); /* 7 */
714 /* Specify the byte order for the target when it is explicitly
715 specified by the user (not auto detected). */
716 switch (selected_byte_order ())
719 strcat (arg_buf
, " -E big");
721 case BFD_ENDIAN_LITTLE
:
722 strcat (arg_buf
, " -E little");
724 case BFD_ENDIAN_UNKNOWN
:
727 /* Specify the architecture of the target when it has been
728 explicitly specified */
729 if (selected_architecture_name () != NULL
)
731 strcat (arg_buf
, " --architecture=");
732 strcat (arg_buf
, selected_architecture_name ());
734 /* Pass along gdb's concept of the sysroot. */
735 strcat (arg_buf
, " --sysroot=");
736 strcat (arg_buf
, sysroot
);
737 /* finally, any explicit args */
740 strcat (arg_buf
, " "); /* 1 */
741 strcat (arg_buf
, args
);
744 gdb_argv
argv (arg_buf
);
745 sim_argv
= argv
.release ();
748 gdbsim_desc
= sim_open (SIM_OPEN_DEBUG
, &gdb_callback
, exec_bfd
, sim_argv
);
750 if (gdbsim_desc
== 0)
754 error (_("unable to create simulator instance"));
757 /* Reset the pid numberings for this batch of sim instances. */
758 next_pid
= INITIAL_PID
;
760 /* Allocate the inferior data, but do not allocate a sim instance
761 since we've already just done that. */
762 sim_data
= get_sim_inferior_data (current_inferior (),
763 SIM_INSTANCE_NOT_NEEDED
);
765 sim_data
->gdbsim_desc
= gdbsim_desc
;
767 push_target (&gdbsim_ops
);
768 printf_filtered ("Connected to the simulator.\n");
770 /* There's nothing running after "target sim" or "load"; not until
772 inferior_ptid
= null_ptid
;
777 /* Helper for gdbsim_target::close. */
780 gdbsim_target::close_one_inferior (inferior
*inf
)
782 struct sim_inferior_data
*sim_data
= sim_inferior_data_key
.get (inf
);
783 if (sim_data
!= NULL
)
785 ptid_t ptid
= sim_data
->remote_sim_ptid
;
787 sim_inferior_data_key
.clear (inf
);
789 /* Having a ptid allocated and stored in remote_sim_ptid does
790 not mean that a corresponding inferior was ever created.
791 Thus we need to verify the existence of an inferior using the
792 pid in question before setting inferior_ptid via
793 switch_to_thread() or mourning the inferior. */
794 if (find_inferior_ptid (this, ptid
) != NULL
)
796 switch_to_thread (this, ptid
);
797 generic_mourn_inferior ();
802 /* Close out all files and local state before this target loses control. */
805 gdbsim_target::close ()
808 fprintf_unfiltered (gdb_stdlog
, "gdbsim_close\n");
810 for (inferior
*inf
: all_inferiors (this))
811 close_one_inferior (inf
);
813 if (sim_argv
!= NULL
)
824 /* Takes a program previously attached to and detaches it.
825 The program may resume execution (some targets do, some don't) and will
826 no longer stop on signals, etc. We better not have left any breakpoints
827 in the program or it'll die when it hits one. FROM_TTY says whether to be
829 /* Terminate the open connection to the remote debugger.
830 Use this when you want to detach and do something else with your gdb. */
833 gdbsim_target::detach (inferior
*inf
, int from_tty
)
836 fprintf_unfiltered (gdb_stdlog
, "gdbsim_detach\n");
838 unpush_target (this); /* calls gdbsim_close to do the real work */
840 printf_filtered ("Ending simulator %s debugging\n", target_shortname
);
843 /* Resume execution of the target process. STEP says whether to single-step
844 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
845 to the target, or zero for no signal. */
848 gdbsim_target::resume_one_inferior (inferior
*inf
, bool step
,
851 struct sim_inferior_data
*sim_data
852 = get_sim_inferior_data (inf
, SIM_INSTANCE_NOT_NEEDED
);
856 sim_data
->resume_siggnal
= siggnal
;
857 sim_data
->resume_step
= step
;
860 fprintf_unfiltered (gdb_stdlog
,
861 _("gdbsim_resume: pid %d, step %d, signal %d\n"),
862 inf
->pid
, step
, siggnal
);
867 gdbsim_target::resume (ptid_t ptid
, int step
, enum gdb_signal siggnal
)
869 struct sim_inferior_data
*sim_data
870 = get_inferior_data_by_ptid (ptid
, SIM_INSTANCE_NOT_NEEDED
);
872 /* We don't access any sim_data members within this function.
873 What's of interest is whether or not the call to
874 get_sim_inferior_data_by_ptid(), above, is able to obtain a
875 non-NULL pointer. If it managed to obtain a non-NULL pointer, we
876 know we have a single inferior to consider. If it's NULL, we
877 either have multiple inferiors to resume or an error condition. */
880 resume_one_inferior (find_inferior_ptid (this, ptid
), step
, siggnal
);
881 else if (ptid
== minus_one_ptid
)
883 for (inferior
*inf
: all_inferiors (this))
884 resume_one_inferior (inf
, step
, siggnal
);
887 error (_("The program is not being run."));
890 /* Notify the simulator of an asynchronous request to interrupt.
892 The simulator shall ensure that the interrupt request is eventually
893 delivered to the simulator. If the call is made while the
894 simulator is not running then the interrupt request is processed when
895 the simulator is next resumed.
897 For simulators that do not support this operation, just abort. */
900 gdbsim_interrupt_inferior (struct inferior
*inf
, void *arg
)
902 struct sim_inferior_data
*sim_data
903 = get_sim_inferior_data (inf
, SIM_INSTANCE_NEEDED
);
907 if (!sim_stop (sim_data
->gdbsim_desc
))
913 /* When called from iterate_over_inferiors, a zero return causes the
914 iteration process to proceed until there are no more inferiors to
920 gdbsim_target::interrupt ()
922 iterate_over_inferiors (gdbsim_interrupt_inferior
, NULL
);
925 /* GDB version of os_poll_quit callback.
926 Taken from gdb/util.c - should be in a library. */
929 gdb_os_poll_quit (host_callback
*p
)
931 if (deprecated_ui_loop_hook
!= NULL
)
932 deprecated_ui_loop_hook (0);
934 if (check_quit_flag ()) /* gdb's idea of quit */
939 /* Wait for inferior process to do something. Return pid of child,
940 or -1 in case of error; store status through argument pointer STATUS,
941 just as `wait' would. */
944 gdbsim_cntrl_c (int signo
)
946 gdbsim_ops
.interrupt ();
950 gdbsim_target::wait (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
952 struct sim_inferior_data
*sim_data
;
953 static sighandler_t prev_sigint
;
955 enum sim_stop reason
= sim_running
;
957 /* This target isn't able to (yet) resume more than one inferior at a time.
958 When ptid is minus_one_ptid, just use the current inferior. If we're
959 given an explicit pid, we'll try to find it and use that instead. */
960 if (ptid
== minus_one_ptid
)
961 sim_data
= get_sim_inferior_data (current_inferior (),
962 SIM_INSTANCE_NEEDED
);
965 sim_data
= get_inferior_data_by_ptid (ptid
, SIM_INSTANCE_NEEDED
);
966 if (sim_data
== NULL
)
967 error (_("Unable to wait for pid %d. Inferior not found."),
969 inferior_ptid
= ptid
;
973 fprintf_unfiltered (gdb_stdlog
, "gdbsim_wait\n");
975 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
977 struct sigaction sa
, osa
;
978 sa
.sa_handler
= gdbsim_cntrl_c
;
979 sigemptyset (&sa
.sa_mask
);
981 sigaction (SIGINT
, &sa
, &osa
);
982 prev_sigint
= osa
.sa_handler
;
985 prev_sigint
= signal (SIGINT
, gdbsim_cntrl_c
);
987 sim_resume (sim_data
->gdbsim_desc
, sim_data
->resume_step
,
988 sim_data
->resume_siggnal
);
990 signal (SIGINT
, prev_sigint
);
991 sim_data
->resume_step
= 0;
993 sim_stop_reason (sim_data
->gdbsim_desc
, &reason
, &sigrc
);
998 status
->kind
= TARGET_WAITKIND_EXITED
;
999 status
->value
.integer
= sigrc
;
1004 case GDB_SIGNAL_ABRT
:
1007 case GDB_SIGNAL_INT
:
1008 case GDB_SIGNAL_TRAP
:
1010 status
->kind
= TARGET_WAITKIND_STOPPED
;
1011 status
->value
.sig
= (enum gdb_signal
) sigrc
;
1016 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
1017 status
->value
.sig
= (enum gdb_signal
) sigrc
;
1021 /* FIXME: Is this correct? */
1025 return inferior_ptid
;
1028 /* Get ready to modify the registers array. On machines which store
1029 individual registers, this doesn't need to do anything. On machines
1030 which store all the registers in one fell swoop, this makes sure
1031 that registers contains all the registers from the program being
1035 gdbsim_target::prepare_to_store (struct regcache
*regcache
)
1037 /* Do nothing, since we can store individual regs. */
1040 /* Helper for gdbsim_xfer_partial that handles memory transfers.
1041 Arguments are like target_xfer_partial. */
1043 static enum target_xfer_status
1044 gdbsim_xfer_memory (struct target_ops
*target
,
1045 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1046 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
1048 struct sim_inferior_data
*sim_data
1049 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED
);
1052 /* If this target doesn't have memory yet, return 0 causing the
1053 request to be passed to a lower target, hopefully an exec
1055 if (!target
->has_memory ())
1056 return TARGET_XFER_EOF
;
1058 if (!sim_data
->program_loaded
)
1059 error (_("No program loaded."));
1061 /* Note that we obtained the sim_data pointer above using
1062 SIM_INSTANCE_NOT_NEEDED. We do this so that we don't needlessly
1063 allocate a sim instance prior to loading a program. If we
1064 get to this point in the code though, gdbsim_desc should be
1065 non-NULL. (Note that a sim instance is needed in order to load
1067 gdb_assert (sim_data
->gdbsim_desc
!= NULL
);
1070 fprintf_unfiltered (gdb_stdlog
,
1071 "gdbsim_xfer_memory: readbuf %s, writebuf %s, "
1072 "memaddr %s, len %s\n",
1073 host_address_to_string (readbuf
),
1074 host_address_to_string (writebuf
),
1075 paddress (target_gdbarch (), memaddr
),
1080 if (remote_debug
&& len
> 0)
1081 dump_mem (writebuf
, len
);
1082 l
= sim_write (sim_data
->gdbsim_desc
, memaddr
, writebuf
, len
);
1086 l
= sim_read (sim_data
->gdbsim_desc
, memaddr
, readbuf
, len
);
1087 if (remote_debug
&& len
> 0)
1088 dump_mem (readbuf
, len
);
1092 *xfered_len
= (ULONGEST
) l
;
1093 return TARGET_XFER_OK
;
1096 return TARGET_XFER_EOF
;
1098 return TARGET_XFER_E_IO
;
1101 /* Target to_xfer_partial implementation. */
1103 enum target_xfer_status
1104 gdbsim_target::xfer_partial (enum target_object object
,
1105 const char *annex
, gdb_byte
*readbuf
,
1106 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
1107 ULONGEST
*xfered_len
)
1111 case TARGET_OBJECT_MEMORY
:
1112 return gdbsim_xfer_memory (this, readbuf
, writebuf
, offset
, len
,
1116 return TARGET_XFER_E_IO
;
1121 gdbsim_target::files_info ()
1123 struct sim_inferior_data
*sim_data
1124 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED
);
1125 const char *file
= "nothing";
1128 file
= bfd_get_filename (exec_bfd
);
1131 fprintf_unfiltered (gdb_stdlog
, "gdbsim_files_info: file \"%s\"\n", file
);
1135 fprintf_unfiltered (gdb_stdlog
, "\tAttached to %s running program %s\n",
1136 target_shortname
, file
);
1137 sim_info (sim_data
->gdbsim_desc
, 0);
1141 /* Clear the simulator's notion of what the break points are. */
1144 gdbsim_target::mourn_inferior ()
1147 fprintf_unfiltered (gdb_stdlog
, "gdbsim_mourn_inferior:\n");
1149 remove_breakpoints ();
1150 generic_mourn_inferior ();
1153 /* Pass the command argument through to the simulator verbatim. The
1154 simulator must do any command interpretation work. */
1157 simulator_command (const char *args
, int from_tty
)
1159 struct sim_inferior_data
*sim_data
;
1161 /* We use inferior_data() instead of get_sim_inferior_data() here in
1162 order to avoid attaching a sim_inferior_data struct to an
1163 inferior unnecessarily. The reason we take such care here is due
1164 to the fact that this function, simulator_command(), may be called
1165 even when the sim target is not active. If we were to use
1166 get_sim_inferior_data() here, it is possible that this call would
1167 be made either prior to gdbsim_open() or after gdbsim_close(),
1168 thus allocating memory that would not be garbage collected until
1169 the ultimate destruction of the associated inferior. */
1171 sim_data
= sim_inferior_data_key
.get (current_inferior ());
1172 if (sim_data
== NULL
|| sim_data
->gdbsim_desc
== NULL
)
1175 /* PREVIOUSLY: The user may give a command before the simulator
1176 is opened. [...] (??? assuming of course one wishes to
1177 continue to allow commands to be sent to unopened simulators,
1178 which isn't entirely unreasonable). */
1180 /* The simulator is a builtin abstraction of a remote target.
1181 Consistent with that model, access to the simulator, via sim
1182 commands, is restricted to the period when the channel to the
1183 simulator is open. */
1185 error (_("Not connected to the simulator target"));
1188 sim_do_command (sim_data
->gdbsim_desc
, args
);
1190 /* Invalidate the register cache, in case the simulator command does
1192 registers_changed ();
1196 sim_command_completer (struct cmd_list_element
*ignore
,
1197 completion_tracker
&tracker
,
1198 const char *text
, const char *word
)
1200 struct sim_inferior_data
*sim_data
;
1202 sim_data
= sim_inferior_data_key
.get (current_inferior ());
1203 if (sim_data
== NULL
|| sim_data
->gdbsim_desc
== NULL
)
1206 /* sim_complete_command returns a NULL-terminated malloc'ed array of
1207 malloc'ed strings. */
1208 struct sim_completions_deleter
1210 void operator() (char **ptr
) const
1212 for (size_t i
= 0; ptr
[i
] != NULL
; i
++)
1218 std::unique_ptr
<char *[], sim_completions_deleter
> sim_completions
1219 (sim_complete_command (sim_data
->gdbsim_desc
, text
, word
));
1220 if (sim_completions
== NULL
)
1223 /* Count the elements and add completions from tail to head because
1224 below we'll swap elements out of the array in case add_completion
1225 throws and the deleter deletes until it finds a NULL element. */
1227 while (sim_completions
[count
] != NULL
)
1230 for (size_t i
= count
; i
> 0; i
--)
1232 gdb::unique_xmalloc_ptr
<char> match (sim_completions
[i
- 1]);
1233 sim_completions
[i
- 1] = NULL
;
1234 tracker
.add_completion (std::move (match
));
1238 /* Check to see if a thread is still alive. */
1241 gdbsim_target::thread_alive (ptid_t ptid
)
1243 struct sim_inferior_data
*sim_data
1244 = get_inferior_data_by_ptid (ptid
, SIM_INSTANCE_NOT_NEEDED
);
1246 if (sim_data
== NULL
)
1249 if (ptid
== sim_data
->remote_sim_ptid
)
1250 /* The simulators' task is always alive. */
1256 /* Convert a thread ID to a string. */
1259 gdbsim_target::pid_to_str (ptid_t ptid
)
1261 return normal_pid_to_str (ptid
);
1264 /* Simulator memory may be accessed after the program has been loaded. */
1267 gdbsim_target::has_all_memory ()
1269 struct sim_inferior_data
*sim_data
1270 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED
);
1272 if (!sim_data
->program_loaded
)
1279 gdbsim_target::has_memory ()
1281 struct sim_inferior_data
*sim_data
1282 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED
);
1284 if (!sim_data
->program_loaded
)
1290 void _initialize_remote_sim ();
1292 _initialize_remote_sim ()
1294 struct cmd_list_element
*c
;
1296 add_target (gdbsim_target_info
, gdbsim_target_open
);
1298 c
= add_com ("sim", class_obscure
, simulator_command
,
1299 _("Send a command to the simulator."));
1300 set_cmd_completer (c
, sim_command_completer
);