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
)
89 gdb_assert (remote_sim_ptid
!= null_ptid
);
93 ~sim_inferior_data ();
95 /* Flag which indicates whether or not the program has been loaded. */
96 int program_loaded
= 0;
98 /* Simulator descriptor for this inferior. */
101 /* This is the ptid we use for this particular simulator instance. Its
102 value is somewhat arbitrary, as the simulator target don't have a
103 notion of tasks or threads, but we need something non-null to place
104 in inferior_ptid. For simulators which permit multiple instances,
105 we also need a unique identifier to use for each inferior. */
106 ptid_t remote_sim_ptid
;
108 /* Signal with which to resume. */
109 enum gdb_signal resume_siggnal
= GDB_SIGNAL_0
;
111 /* Flag which indicates whether resume should step or not. */
115 static const target_info gdbsim_target_info
= {
118 N_("Use the compiled-in simulator.")
121 struct gdbsim_target final
122 : public memory_breakpoint_target
<process_stratum_target
>
124 gdbsim_target () = default;
126 const target_info
&info () const override
127 { return gdbsim_target_info
; }
129 void close () override
;
131 void detach (inferior
*inf
, int) override
;
133 void resume (ptid_t
, int, enum gdb_signal
) override
;
134 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
136 void fetch_registers (struct regcache
*, int) override
;
137 void store_registers (struct regcache
*, int) override
;
138 void prepare_to_store (struct regcache
*) override
;
140 enum target_xfer_status
xfer_partial (enum target_object object
,
143 const gdb_byte
*writebuf
,
144 ULONGEST offset
, ULONGEST len
,
145 ULONGEST
*xfered_len
) override
;
147 void files_info () override
;
149 void kill () override
;
151 void load (const char *, int) override
;
153 bool can_create_inferior () override
{ return true; }
154 void create_inferior (const char *, const std::string
&,
155 char **, int) override
;
157 void mourn_inferior () override
;
159 void interrupt () override
;
161 bool thread_alive (ptid_t ptid
) override
;
163 std::string
pid_to_str (ptid_t
) override
;
165 bool has_all_memory () override
;
166 bool has_memory () override
;
169 sim_inferior_data
*get_inferior_data_by_ptid (ptid_t ptid
,
170 int sim_instance_needed
);
171 void resume_one_inferior (inferior
*inf
, bool step
, gdb_signal siggnal
);
172 void close_one_inferior (inferior
*inf
);
175 static struct gdbsim_target gdbsim_ops
;
177 static inferior_key
<sim_inferior_data
> sim_inferior_data_key
;
179 /* Flag indicating the "open" status of this module. It's set to 1
180 in gdbsim_open() and 0 in gdbsim_close(). */
181 static int gdbsim_is_open
= 0;
183 /* Argument list to pass to sim_open(). It is allocated in gdbsim_open()
184 and deallocated in gdbsim_close(). The lifetime needs to extend beyond
185 the call to gdbsim_open() due to the fact that other sim instances other
186 than the first will be allocated after the gdbsim_open() call. */
187 static char **sim_argv
= NULL
;
189 /* OS-level callback functions for write, flush, etc. */
190 static host_callback gdb_callback
;
191 static int callbacks_initialized
= 0;
193 /* Flags indicating whether or not a sim instance is needed. One of these
194 flags should be passed to get_sim_inferior_data(). */
196 enum {SIM_INSTANCE_NOT_NEEDED
= 0, SIM_INSTANCE_NEEDED
= 1};
198 /* Obtain pointer to per-inferior simulator data, allocating it if necessary.
199 Attempt to open the sim if SIM_INSTANCE_NEEDED is true. */
201 static struct sim_inferior_data
*
202 get_sim_inferior_data (struct inferior
*inf
, int sim_instance_needed
)
204 SIM_DESC sim_desc
= NULL
;
205 struct sim_inferior_data
*sim_data
= sim_inferior_data_key
.get (inf
);
207 /* Try to allocate a new sim instance, if needed. We do this ahead of
208 a potential allocation of a sim_inferior_data struct in order to
209 avoid needlessly allocating that struct in the event that the sim
210 instance allocation fails. */
211 if (sim_instance_needed
== SIM_INSTANCE_NEEDED
212 && (sim_data
== NULL
|| sim_data
->gdbsim_desc
== NULL
))
214 sim_desc
= sim_open (SIM_OPEN_DEBUG
, &gdb_callback
, exec_bfd
, sim_argv
);
215 if (sim_desc
== NULL
)
216 error (_("Unable to create simulator instance for inferior %d."),
219 /* Check if the sim descriptor is the same as that of another
221 for (inferior
*other_inf
: all_inferiors ())
223 sim_inferior_data
*other_sim_data
224 = sim_inferior_data_key
.get (other_inf
);
226 if (other_sim_data
!= NULL
227 && other_sim_data
->gdbsim_desc
== sim_desc
)
229 /* We don't close the descriptor due to the fact that it's
230 shared with some other inferior. If we were to close it,
231 that might needlessly muck up the other inferior. Of
232 course, it's possible that the damage has already been
233 done... Note that it *will* ultimately be closed during
234 cleanup of the other inferior. */
237 _("Inferior %d and inferior %d would have identical simulator state.\n"
238 "(This simulator does not support the running of more than one inferior.)"),
239 inf
->num
, other_inf
->num
);
244 if (sim_data
== NULL
)
246 sim_data
= sim_inferior_data_key
.emplace (inf
, sim_desc
);
250 /* This handles the case where sim_data was allocated prior to
251 needing a sim instance. */
252 sim_data
->gdbsim_desc
= sim_desc
;
259 /* Return pointer to per-inferior simulator data using PTID to find the
260 inferior in question. Return NULL when no inferior is found or
261 when ptid has a zero or negative pid component. */
264 gdbsim_target::get_inferior_data_by_ptid (ptid_t ptid
,
265 int sim_instance_needed
)
267 struct inferior
*inf
;
268 int pid
= ptid
.pid ();
273 inf
= find_inferior_pid (this, pid
);
276 return get_sim_inferior_data (inf
, sim_instance_needed
);
281 /* Free the per-inferior simulator data. */
283 sim_inferior_data::~sim_inferior_data ()
286 sim_close (gdbsim_desc
, 0);
290 dump_mem (const gdb_byte
*buf
, int len
)
292 fputs_unfiltered ("\t", gdb_stdlog
);
294 if (len
== 8 || len
== 4)
298 memcpy (l
, buf
, len
);
299 fprintf_unfiltered (gdb_stdlog
, "0x%08x", l
[0]);
301 fprintf_unfiltered (gdb_stdlog
, " 0x%08x", l
[1]);
307 for (i
= 0; i
< len
; i
++)
308 fprintf_unfiltered (gdb_stdlog
, "0x%02x ", buf
[i
]);
311 fputs_unfiltered ("\n", gdb_stdlog
);
314 /* Initialize gdb_callback. */
317 init_callbacks (void)
319 if (!callbacks_initialized
)
321 gdb_callback
= default_callback
;
322 gdb_callback
.init (&gdb_callback
);
323 gdb_callback
.write_stdout
= gdb_os_write_stdout
;
324 gdb_callback
.flush_stdout
= gdb_os_flush_stdout
;
325 gdb_callback
.write_stderr
= gdb_os_write_stderr
;
326 gdb_callback
.flush_stderr
= gdb_os_flush_stderr
;
327 gdb_callback
.printf_filtered
= gdb_os_printf_filtered
;
328 gdb_callback
.vprintf_filtered
= gdb_os_vprintf_filtered
;
329 gdb_callback
.evprintf_filtered
= gdb_os_evprintf_filtered
;
330 gdb_callback
.error
= gdb_os_error
;
331 gdb_callback
.poll_quit
= gdb_os_poll_quit
;
332 gdb_callback
.magic
= HOST_CALLBACK_MAGIC
;
333 callbacks_initialized
= 1;
337 /* Release callbacks (free resources used by them). */
342 if (callbacks_initialized
)
344 gdb_callback
.shutdown (&gdb_callback
);
345 callbacks_initialized
= 0;
349 /* GDB version of os_write_stdout callback. */
352 gdb_os_write_stdout (host_callback
*p
, const char *buf
, int len
)
354 gdb_stdtarg
->write (buf
, len
);
358 /* GDB version of os_flush_stdout callback. */
361 gdb_os_flush_stdout (host_callback
*p
)
363 gdb_stdtarg
->flush ();
366 /* GDB version of os_write_stderr callback. */
369 gdb_os_write_stderr (host_callback
*p
, const char *buf
, int len
)
374 for (i
= 0; i
< len
; i
++)
378 gdb_stdtargerr
->puts (b
);
383 /* GDB version of os_flush_stderr callback. */
386 gdb_os_flush_stderr (host_callback
*p
)
388 gdb_stdtargerr
->flush ();
391 /* GDB version of printf_filtered callback. */
393 static void ATTRIBUTE_PRINTF (2, 3)
394 gdb_os_printf_filtered (host_callback
* p
, const char *format
, ...)
398 va_start (args
, format
);
399 vfprintf_filtered (gdb_stdout
, format
, args
);
403 /* GDB version of error vprintf_filtered. */
405 static void ATTRIBUTE_PRINTF (2, 0)
406 gdb_os_vprintf_filtered (host_callback
* p
, const char *format
, va_list ap
)
408 vfprintf_filtered (gdb_stdout
, format
, ap
);
411 /* GDB version of error evprintf_filtered. */
413 static void ATTRIBUTE_PRINTF (2, 0)
414 gdb_os_evprintf_filtered (host_callback
* p
, const char *format
, va_list ap
)
416 vfprintf_filtered (gdb_stderr
, format
, ap
);
419 /* GDB version of error callback. */
421 static void ATTRIBUTE_PRINTF (2, 3)
422 gdb_os_error (host_callback
* p
, const char *format
, ...)
426 va_start (args
, format
);
427 verror (format
, args
);
432 one2one_register_sim_regno (struct gdbarch
*gdbarch
, int regnum
)
434 /* Only makes sense to supply raw registers. */
435 gdb_assert (regnum
>= 0 && regnum
< gdbarch_num_regs (gdbarch
));
440 gdbsim_target::fetch_registers (struct regcache
*regcache
, int regno
)
442 struct gdbarch
*gdbarch
= regcache
->arch ();
443 struct inferior
*inf
= find_inferior_ptid (this, regcache
->ptid ());
444 struct sim_inferior_data
*sim_data
445 = get_sim_inferior_data (inf
, SIM_INSTANCE_NEEDED
);
449 for (regno
= 0; regno
< gdbarch_num_regs (gdbarch
); regno
++)
450 fetch_registers (regcache
, regno
);
454 switch (gdbarch_register_sim_regno (gdbarch
, regno
))
456 case LEGACY_SIM_REGNO_IGNORE
:
458 case SIM_REGNO_DOES_NOT_EXIST
:
460 /* For moment treat a `does not exist' register the same way
461 as an ``unavailable'' register. */
462 regcache
->raw_supply_zeroed (regno
);
468 static int warn_user
= 1;
469 int regsize
= register_size (gdbarch
, regno
);
470 gdb::byte_vector
buf (regsize
, 0);
473 gdb_assert (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
));
474 nr_bytes
= sim_fetch_register (sim_data
->gdbsim_desc
,
475 gdbarch_register_sim_regno
477 buf
.data (), regsize
);
478 if (nr_bytes
> 0 && nr_bytes
!= regsize
&& warn_user
)
480 fprintf_unfiltered (gdb_stderr
,
481 "Size of register %s (%d/%d) "
482 "incorrect (%d instead of %d))",
483 gdbarch_register_name (gdbarch
, regno
),
485 gdbarch_register_sim_regno (gdbarch
, regno
),
489 /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
490 indicating that GDB and the SIM have different ideas about
491 which registers are fetchable. */
492 /* Else if (nr_bytes < 0): an old simulator, that doesn't
493 think to return the register size. Just assume all is ok. */
494 regcache
->raw_supply (regno
, buf
.data ());
497 fprintf_unfiltered (gdb_stdlog
,
498 "gdbsim_fetch_register: %d", regno
);
499 /* FIXME: We could print something more intelligible. */
500 dump_mem (buf
.data (), regsize
);
509 gdbsim_target::store_registers (struct regcache
*regcache
, int regno
)
511 struct gdbarch
*gdbarch
= regcache
->arch ();
512 struct inferior
*inf
= find_inferior_ptid (this, regcache
->ptid ());
513 struct sim_inferior_data
*sim_data
514 = get_sim_inferior_data (inf
, SIM_INSTANCE_NEEDED
);
518 for (regno
= 0; regno
< gdbarch_num_regs (gdbarch
); regno
++)
519 store_registers (regcache
, regno
);
522 else if (gdbarch_register_sim_regno (gdbarch
, regno
) >= 0)
524 int regsize
= register_size (gdbarch
, regno
);
525 gdb::byte_vector
tmp (regsize
);
528 regcache
->cooked_read (regno
, tmp
.data ());
529 nr_bytes
= sim_store_register (sim_data
->gdbsim_desc
,
530 gdbarch_register_sim_regno
532 tmp
.data (), regsize
);
534 if (nr_bytes
> 0 && nr_bytes
!= regsize
)
535 internal_error (__FILE__
, __LINE__
,
536 _("Register size different to expected"));
538 internal_error (__FILE__
, __LINE__
,
539 _("Register %d not updated"), regno
);
541 warning (_("Register %s not updated"),
542 gdbarch_register_name (gdbarch
, regno
));
546 fprintf_unfiltered (gdb_stdlog
, "gdbsim_store_register: %d", regno
);
547 /* FIXME: We could print something more intelligible. */
548 dump_mem (tmp
.data (), regsize
);
553 /* Kill the running program. This may involve closing any open files
554 and releasing other resources acquired by the simulated program. */
557 gdbsim_target::kill ()
560 fprintf_unfiltered (gdb_stdlog
, "gdbsim_kill\n");
562 /* There is no need to `kill' running simulator - the simulator is
563 not running. Mourning it is enough. */
564 target_mourn_inferior (inferior_ptid
);
567 /* Load an executable file into the target process. This is expected to
568 not only bring new code into the target process, but also to update
569 GDB's symbol tables to match. */
572 gdbsim_target::load (const char *args
, int fromtty
)
575 struct sim_inferior_data
*sim_data
576 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED
);
579 error_no_arg (_("program to load"));
581 gdb_argv
argv (args
);
583 prog
= tilde_expand (argv
[0]);
586 error (_("GDB sim does not yet support a load offset."));
589 fprintf_unfiltered (gdb_stdlog
, "gdbsim_load: prog \"%s\"\n", prog
);
591 /* FIXME: We will print two messages on error.
592 Need error to either not print anything if passed NULL or need
593 another routine that doesn't take any arguments. */
594 if (sim_load (sim_data
->gdbsim_desc
, prog
, NULL
, fromtty
) == SIM_RC_FAIL
)
595 error (_("unable to load program"));
597 /* FIXME: If a load command should reset the targets registers then
598 a call to sim_create_inferior() should go here. */
600 sim_data
->program_loaded
= 1;
604 /* Start an inferior process and set inferior_ptid to its pid.
605 EXEC_FILE is the file to run.
606 ARGS is a string containing the arguments to the program.
607 ENV is the environment vector to pass. Errors reported with error().
608 On VxWorks and various standalone systems, we ignore exec_file. */
609 /* This is called not only when we first attach, but also when the
610 user types "run" after having attached. */
613 gdbsim_target::create_inferior (const char *exec_file
,
614 const std::string
&allargs
,
615 char **env
, int from_tty
)
617 struct sim_inferior_data
*sim_data
618 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED
);
621 const char *args
= allargs
.c_str ();
623 if (exec_file
== 0 || exec_bfd
== 0)
624 warning (_("No executable file specified."));
625 if (!sim_data
->program_loaded
)
626 warning (_("No program loaded."));
629 fprintf_unfiltered (gdb_stdlog
,
630 "gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
631 (exec_file
? exec_file
: "(NULL)"),
634 if (inferior_ptid
== sim_data
->remote_sim_ptid
)
636 remove_breakpoints ();
637 init_wait_for_inferior ();
640 if (exec_file
!= NULL
)
642 len
= strlen (exec_file
) + 1 + allargs
.size () + 1 + /*slop */ 10;
643 arg_buf
= (char *) alloca (len
);
645 strcat (arg_buf
, exec_file
);
646 strcat (arg_buf
, " ");
647 strcat (arg_buf
, args
);
648 built_argv
.reset (arg_buf
);
651 if (sim_create_inferior (sim_data
->gdbsim_desc
, exec_bfd
,
652 built_argv
.get (), env
)
654 error (_("Unable to create sim inferior."));
656 inferior_appeared (current_inferior (),
657 sim_data
->remote_sim_ptid
.pid ());
658 thread_info
*thr
= add_thread_silent (this, sim_data
->remote_sim_ptid
);
659 switch_to_thread (thr
);
661 insert_breakpoints (); /* Needed to get correct instruction
664 clear_proceed_status (0);
667 /* The open routine takes the rest of the parameters from the command,
668 and (if successful) pushes a new target onto the stack.
669 Targets should supply this routine, if only to provide an error message. */
670 /* Called when selecting the simulator. E.g. (gdb) target sim name. */
673 gdbsim_target_open (const char *args
, int from_tty
)
677 struct sim_inferior_data
*sim_data
;
679 SIM_DESC gdbsim_desc
;
681 sysroot
= gdb_sysroot
;
682 if (is_target_filename (sysroot
))
683 sysroot
+= strlen (TARGET_SYSROOT_PREFIX
);
686 fprintf_unfiltered (gdb_stdlog
,
687 "gdbsim_open: args \"%s\"\n", args
? args
: "(null)");
689 /* Ensure that the sim target is not on the target stack. This is
690 necessary, because if it is on the target stack, the call to
691 push_target below will invoke sim_close(), thus freeing various
692 state (including a sim instance) that we allocate prior to
693 invoking push_target(). We want to delay the push_target()
694 operation until after we complete those operations which could
697 unpush_target (&gdbsim_ops
);
699 len
= (7 + 1 /* gdbsim */
700 + strlen (" -E little")
701 + strlen (" --architecture=xxxxxxxxxx")
702 + strlen (" --sysroot=") + strlen (sysroot
) +
703 + (args
? strlen (args
) : 0)
705 arg_buf
= (char *) alloca (len
);
706 strcpy (arg_buf
, "gdbsim"); /* 7 */
707 /* Specify the byte order for the target when it is explicitly
708 specified by the user (not auto detected). */
709 switch (selected_byte_order ())
712 strcat (arg_buf
, " -E big");
714 case BFD_ENDIAN_LITTLE
:
715 strcat (arg_buf
, " -E little");
717 case BFD_ENDIAN_UNKNOWN
:
720 /* Specify the architecture of the target when it has been
721 explicitly specified */
722 if (selected_architecture_name () != NULL
)
724 strcat (arg_buf
, " --architecture=");
725 strcat (arg_buf
, selected_architecture_name ());
727 /* Pass along gdb's concept of the sysroot. */
728 strcat (arg_buf
, " --sysroot=");
729 strcat (arg_buf
, sysroot
);
730 /* finally, any explicit args */
733 strcat (arg_buf
, " "); /* 1 */
734 strcat (arg_buf
, args
);
737 gdb_argv
argv (arg_buf
);
738 sim_argv
= argv
.release ();
741 gdbsim_desc
= sim_open (SIM_OPEN_DEBUG
, &gdb_callback
, exec_bfd
, sim_argv
);
743 if (gdbsim_desc
== 0)
747 error (_("unable to create simulator instance"));
750 /* Reset the pid numberings for this batch of sim instances. */
751 next_pid
= INITIAL_PID
;
753 /* Allocate the inferior data, but do not allocate a sim instance
754 since we've already just done that. */
755 sim_data
= get_sim_inferior_data (current_inferior (),
756 SIM_INSTANCE_NOT_NEEDED
);
758 sim_data
->gdbsim_desc
= gdbsim_desc
;
760 push_target (&gdbsim_ops
);
761 printf_filtered ("Connected to the simulator.\n");
763 /* There's nothing running after "target sim" or "load"; not until
765 switch_to_no_thread ();
770 /* Helper for gdbsim_target::close. */
773 gdbsim_target::close_one_inferior (inferior
*inf
)
775 struct sim_inferior_data
*sim_data
= sim_inferior_data_key
.get (inf
);
776 if (sim_data
!= NULL
)
778 ptid_t ptid
= sim_data
->remote_sim_ptid
;
780 sim_inferior_data_key
.clear (inf
);
782 /* Having a ptid allocated and stored in remote_sim_ptid does
783 not mean that a corresponding inferior was ever created.
784 Thus we need to verify the existence of an inferior using the
785 pid in question before setting inferior_ptid via
786 switch_to_thread() or mourning the inferior. */
787 if (find_inferior_ptid (this, ptid
) != NULL
)
789 switch_to_thread (this, ptid
);
790 generic_mourn_inferior ();
795 /* Close out all files and local state before this target loses control. */
798 gdbsim_target::close ()
801 fprintf_unfiltered (gdb_stdlog
, "gdbsim_close\n");
803 for (inferior
*inf
: all_inferiors (this))
804 close_one_inferior (inf
);
806 if (sim_argv
!= NULL
)
817 /* Takes a program previously attached to and detaches it.
818 The program may resume execution (some targets do, some don't) and will
819 no longer stop on signals, etc. We better not have left any breakpoints
820 in the program or it'll die when it hits one. FROM_TTY says whether to be
822 /* Terminate the open connection to the remote debugger.
823 Use this when you want to detach and do something else with your gdb. */
826 gdbsim_target::detach (inferior
*inf
, int from_tty
)
829 fprintf_unfiltered (gdb_stdlog
, "gdbsim_detach\n");
831 unpush_target (this); /* calls gdbsim_close to do the real work */
833 printf_filtered ("Ending simulator %s debugging\n", target_shortname
);
836 /* Resume execution of the target process. STEP says whether to single-step
837 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
838 to the target, or zero for no signal. */
841 gdbsim_target::resume_one_inferior (inferior
*inf
, bool step
,
844 struct sim_inferior_data
*sim_data
845 = get_sim_inferior_data (inf
, SIM_INSTANCE_NOT_NEEDED
);
849 sim_data
->resume_siggnal
= siggnal
;
850 sim_data
->resume_step
= step
;
853 fprintf_unfiltered (gdb_stdlog
,
854 _("gdbsim_resume: pid %d, step %d, signal %d\n"),
855 inf
->pid
, step
, siggnal
);
860 gdbsim_target::resume (ptid_t ptid
, int step
, enum gdb_signal siggnal
)
862 struct sim_inferior_data
*sim_data
863 = get_inferior_data_by_ptid (ptid
, SIM_INSTANCE_NOT_NEEDED
);
865 /* We don't access any sim_data members within this function.
866 What's of interest is whether or not the call to
867 get_sim_inferior_data_by_ptid(), above, is able to obtain a
868 non-NULL pointer. If it managed to obtain a non-NULL pointer, we
869 know we have a single inferior to consider. If it's NULL, we
870 either have multiple inferiors to resume or an error condition. */
873 resume_one_inferior (find_inferior_ptid (this, ptid
), step
, siggnal
);
874 else if (ptid
== minus_one_ptid
)
876 for (inferior
*inf
: all_inferiors (this))
877 resume_one_inferior (inf
, step
, siggnal
);
880 error (_("The program is not being run."));
883 /* Notify the simulator of an asynchronous request to interrupt.
885 The simulator shall ensure that the interrupt request is eventually
886 delivered to the simulator. If the call is made while the
887 simulator is not running then the interrupt request is processed when
888 the simulator is next resumed.
890 For simulators that do not support this operation, just abort. */
893 gdbsim_target::interrupt ()
895 for (inferior
*inf
: all_inferiors ())
897 sim_inferior_data
*sim_data
898 = get_sim_inferior_data (inf
, SIM_INSTANCE_NEEDED
);
900 if (sim_data
!= nullptr && !sim_stop (sim_data
->gdbsim_desc
))
905 /* GDB version of os_poll_quit callback.
906 Taken from gdb/util.c - should be in a library. */
909 gdb_os_poll_quit (host_callback
*p
)
911 if (deprecated_ui_loop_hook
!= NULL
)
912 deprecated_ui_loop_hook (0);
914 if (check_quit_flag ()) /* gdb's idea of quit */
919 /* Wait for inferior process to do something. Return pid of child,
920 or -1 in case of error; store status through argument pointer STATUS,
921 just as `wait' would. */
924 gdbsim_cntrl_c (int signo
)
926 gdbsim_ops
.interrupt ();
930 gdbsim_target::wait (ptid_t ptid
, struct target_waitstatus
*status
, int options
)
932 struct sim_inferior_data
*sim_data
;
933 static sighandler_t prev_sigint
;
935 enum sim_stop reason
= sim_running
;
937 /* This target isn't able to (yet) resume more than one inferior at a time.
938 When ptid is minus_one_ptid, just use the current inferior. If we're
939 given an explicit pid, we'll try to find it and use that instead. */
940 if (ptid
== minus_one_ptid
)
941 sim_data
= get_sim_inferior_data (current_inferior (),
942 SIM_INSTANCE_NEEDED
);
945 sim_data
= get_inferior_data_by_ptid (ptid
, SIM_INSTANCE_NEEDED
);
946 if (sim_data
== NULL
)
947 error (_("Unable to wait for pid %d. Inferior not found."),
952 fprintf_unfiltered (gdb_stdlog
, "gdbsim_wait\n");
954 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
956 struct sigaction sa
, osa
;
957 sa
.sa_handler
= gdbsim_cntrl_c
;
958 sigemptyset (&sa
.sa_mask
);
960 sigaction (SIGINT
, &sa
, &osa
);
961 prev_sigint
= osa
.sa_handler
;
964 prev_sigint
= signal (SIGINT
, gdbsim_cntrl_c
);
966 sim_resume (sim_data
->gdbsim_desc
, sim_data
->resume_step
,
967 sim_data
->resume_siggnal
);
969 signal (SIGINT
, prev_sigint
);
970 sim_data
->resume_step
= 0;
972 sim_stop_reason (sim_data
->gdbsim_desc
, &reason
, &sigrc
);
977 status
->kind
= TARGET_WAITKIND_EXITED
;
978 status
->value
.integer
= sigrc
;
983 case GDB_SIGNAL_ABRT
:
987 case GDB_SIGNAL_TRAP
:
989 status
->kind
= TARGET_WAITKIND_STOPPED
;
990 status
->value
.sig
= (enum gdb_signal
) sigrc
;
995 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
996 status
->value
.sig
= (enum gdb_signal
) sigrc
;
1000 /* FIXME: Is this correct? */
1004 return sim_data
->remote_sim_ptid
;
1007 /* Get ready to modify the registers array. On machines which store
1008 individual registers, this doesn't need to do anything. On machines
1009 which store all the registers in one fell swoop, this makes sure
1010 that registers contains all the registers from the program being
1014 gdbsim_target::prepare_to_store (struct regcache
*regcache
)
1016 /* Do nothing, since we can store individual regs. */
1019 /* Helper for gdbsim_xfer_partial that handles memory transfers.
1020 Arguments are like target_xfer_partial. */
1022 static enum target_xfer_status
1023 gdbsim_xfer_memory (struct target_ops
*target
,
1024 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1025 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
1027 struct sim_inferior_data
*sim_data
1028 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED
);
1031 /* If this target doesn't have memory yet, return 0 causing the
1032 request to be passed to a lower target, hopefully an exec
1034 if (!target
->has_memory ())
1035 return TARGET_XFER_EOF
;
1037 if (!sim_data
->program_loaded
)
1038 error (_("No program loaded."));
1040 /* Note that we obtained the sim_data pointer above using
1041 SIM_INSTANCE_NOT_NEEDED. We do this so that we don't needlessly
1042 allocate a sim instance prior to loading a program. If we
1043 get to this point in the code though, gdbsim_desc should be
1044 non-NULL. (Note that a sim instance is needed in order to load
1046 gdb_assert (sim_data
->gdbsim_desc
!= NULL
);
1049 fprintf_unfiltered (gdb_stdlog
,
1050 "gdbsim_xfer_memory: readbuf %s, writebuf %s, "
1051 "memaddr %s, len %s\n",
1052 host_address_to_string (readbuf
),
1053 host_address_to_string (writebuf
),
1054 paddress (target_gdbarch (), memaddr
),
1059 if (remote_debug
&& len
> 0)
1060 dump_mem (writebuf
, len
);
1061 l
= sim_write (sim_data
->gdbsim_desc
, memaddr
, writebuf
, len
);
1065 l
= sim_read (sim_data
->gdbsim_desc
, memaddr
, readbuf
, len
);
1066 if (remote_debug
&& len
> 0)
1067 dump_mem (readbuf
, len
);
1071 *xfered_len
= (ULONGEST
) l
;
1072 return TARGET_XFER_OK
;
1075 return TARGET_XFER_EOF
;
1077 return TARGET_XFER_E_IO
;
1080 /* Target to_xfer_partial implementation. */
1082 enum target_xfer_status
1083 gdbsim_target::xfer_partial (enum target_object object
,
1084 const char *annex
, gdb_byte
*readbuf
,
1085 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
1086 ULONGEST
*xfered_len
)
1090 case TARGET_OBJECT_MEMORY
:
1091 return gdbsim_xfer_memory (this, readbuf
, writebuf
, offset
, len
,
1095 return TARGET_XFER_E_IO
;
1100 gdbsim_target::files_info ()
1102 struct sim_inferior_data
*sim_data
1103 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED
);
1104 const char *file
= "nothing";
1107 file
= bfd_get_filename (exec_bfd
);
1110 fprintf_unfiltered (gdb_stdlog
, "gdbsim_files_info: file \"%s\"\n", file
);
1114 fprintf_unfiltered (gdb_stdlog
, "\tAttached to %s running program %s\n",
1115 target_shortname
, file
);
1116 sim_info (sim_data
->gdbsim_desc
, 0);
1120 /* Clear the simulator's notion of what the break points are. */
1123 gdbsim_target::mourn_inferior ()
1126 fprintf_unfiltered (gdb_stdlog
, "gdbsim_mourn_inferior:\n");
1128 remove_breakpoints ();
1129 generic_mourn_inferior ();
1132 /* Pass the command argument through to the simulator verbatim. The
1133 simulator must do any command interpretation work. */
1136 simulator_command (const char *args
, int from_tty
)
1138 struct sim_inferior_data
*sim_data
;
1140 /* We use inferior_data() instead of get_sim_inferior_data() here in
1141 order to avoid attaching a sim_inferior_data struct to an
1142 inferior unnecessarily. The reason we take such care here is due
1143 to the fact that this function, simulator_command(), may be called
1144 even when the sim target is not active. If we were to use
1145 get_sim_inferior_data() here, it is possible that this call would
1146 be made either prior to gdbsim_open() or after gdbsim_close(),
1147 thus allocating memory that would not be garbage collected until
1148 the ultimate destruction of the associated inferior. */
1150 sim_data
= sim_inferior_data_key
.get (current_inferior ());
1151 if (sim_data
== NULL
|| sim_data
->gdbsim_desc
== NULL
)
1154 /* PREVIOUSLY: The user may give a command before the simulator
1155 is opened. [...] (??? assuming of course one wishes to
1156 continue to allow commands to be sent to unopened simulators,
1157 which isn't entirely unreasonable). */
1159 /* The simulator is a builtin abstraction of a remote target.
1160 Consistent with that model, access to the simulator, via sim
1161 commands, is restricted to the period when the channel to the
1162 simulator is open. */
1164 error (_("Not connected to the simulator target"));
1167 sim_do_command (sim_data
->gdbsim_desc
, args
);
1169 /* Invalidate the register cache, in case the simulator command does
1171 registers_changed ();
1175 sim_command_completer (struct cmd_list_element
*ignore
,
1176 completion_tracker
&tracker
,
1177 const char *text
, const char *word
)
1179 struct sim_inferior_data
*sim_data
;
1181 sim_data
= sim_inferior_data_key
.get (current_inferior ());
1182 if (sim_data
== NULL
|| sim_data
->gdbsim_desc
== NULL
)
1185 /* sim_complete_command returns a NULL-terminated malloc'ed array of
1186 malloc'ed strings. */
1187 struct sim_completions_deleter
1189 void operator() (char **ptr
) const
1191 for (size_t i
= 0; ptr
[i
] != NULL
; i
++)
1197 std::unique_ptr
<char *[], sim_completions_deleter
> sim_completions
1198 (sim_complete_command (sim_data
->gdbsim_desc
, text
, word
));
1199 if (sim_completions
== NULL
)
1202 /* Count the elements and add completions from tail to head because
1203 below we'll swap elements out of the array in case add_completion
1204 throws and the deleter deletes until it finds a NULL element. */
1206 while (sim_completions
[count
] != NULL
)
1209 for (size_t i
= count
; i
> 0; i
--)
1211 gdb::unique_xmalloc_ptr
<char> match (sim_completions
[i
- 1]);
1212 sim_completions
[i
- 1] = NULL
;
1213 tracker
.add_completion (std::move (match
));
1217 /* Check to see if a thread is still alive. */
1220 gdbsim_target::thread_alive (ptid_t ptid
)
1222 struct sim_inferior_data
*sim_data
1223 = get_inferior_data_by_ptid (ptid
, SIM_INSTANCE_NOT_NEEDED
);
1225 if (sim_data
== NULL
)
1228 if (ptid
== sim_data
->remote_sim_ptid
)
1229 /* The simulators' task is always alive. */
1235 /* Convert a thread ID to a string. */
1238 gdbsim_target::pid_to_str (ptid_t ptid
)
1240 return normal_pid_to_str (ptid
);
1243 /* Simulator memory may be accessed after the program has been loaded. */
1246 gdbsim_target::has_all_memory ()
1248 struct sim_inferior_data
*sim_data
1249 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED
);
1251 if (!sim_data
->program_loaded
)
1258 gdbsim_target::has_memory ()
1260 struct sim_inferior_data
*sim_data
1261 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED
);
1263 if (!sim_data
->program_loaded
)
1269 void _initialize_remote_sim ();
1271 _initialize_remote_sim ()
1273 struct cmd_list_element
*c
;
1275 add_target (gdbsim_target_info
, gdbsim_target_open
);
1277 c
= add_com ("sim", class_obscure
, simulator_command
,
1278 _("Send a command to the simulator."));
1279 set_cmd_completer (c
, sim_command_completer
);