1 /* Generic remote debugging interface for simulators.
3 Copyright (C) 1993-2021 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"
45 #include "memory-map.h"
49 static void init_callbacks (void);
51 static void end_callbacks (void);
53 static int gdb_os_write_stdout (host_callback
*, const char *, int);
55 static void gdb_os_flush_stdout (host_callback
*);
57 static int gdb_os_write_stderr (host_callback
*, const char *, int);
59 static void gdb_os_flush_stderr (host_callback
*);
61 static int gdb_os_poll_quit (host_callback
*);
63 /* printf_filtered is depreciated. */
64 static void gdb_os_printf_filtered (host_callback
*, const char *, ...);
66 static void gdb_os_vprintf_filtered (host_callback
*, const char *, va_list);
68 static void gdb_os_evprintf_filtered (host_callback
*, const char *, va_list);
70 static void gdb_os_error (host_callback
*, const char *, ...)
75 sim_* are the interface to the simulator (see remote-sim.h).
76 gdbsim_* are stuff which is internal to gdb. */
78 /* Value of the next pid to allocate for an inferior. As indicated
79 elsewhere, its initial value is somewhat arbitrary; it's critical
80 though that it's not zero or negative. */
82 #define INITIAL_PID 42000
84 /* Simulator-specific, per-inferior state. */
85 struct sim_inferior_data
{
86 explicit sim_inferior_data (SIM_DESC desc
)
88 remote_sim_ptid (next_pid
, 0, next_pid
)
90 gdb_assert (remote_sim_ptid
!= null_ptid
);
94 ~sim_inferior_data ();
96 /* Flag which indicates whether or not the program has been loaded. */
97 int program_loaded
= 0;
99 /* Simulator descriptor for this inferior. */
100 SIM_DESC gdbsim_desc
;
102 /* This is the ptid we use for this particular simulator instance. Its
103 value is somewhat arbitrary, as the simulator target don't have a
104 notion of tasks or threads, but we need something non-null to place
105 in inferior_ptid. For simulators which permit multiple instances,
106 we also need a unique identifier to use for each inferior. */
107 ptid_t remote_sim_ptid
;
109 /* Signal with which to resume. */
110 enum gdb_signal resume_siggnal
= GDB_SIGNAL_0
;
112 /* Flag which indicates whether resume should step or not. */
116 static const target_info gdbsim_target_info
= {
119 N_("Use the compiled-in simulator.")
122 struct gdbsim_target final
123 : public memory_breakpoint_target
<process_stratum_target
>
125 gdbsim_target () = default;
127 const target_info
&info () const override
128 { return gdbsim_target_info
; }
130 void close () override
;
132 void detach (inferior
*inf
, int) override
;
134 void resume (ptid_t
, int, enum gdb_signal
) override
;
135 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
137 void fetch_registers (struct regcache
*, int) override
;
138 void store_registers (struct regcache
*, int) override
;
139 void prepare_to_store (struct regcache
*) override
;
141 enum target_xfer_status
xfer_partial (enum target_object object
,
144 const gdb_byte
*writebuf
,
145 ULONGEST offset
, ULONGEST len
,
146 ULONGEST
*xfered_len
) override
;
148 void files_info () override
;
150 void kill () override
;
152 void load (const char *, int) override
;
154 bool can_create_inferior () override
{ return true; }
155 void create_inferior (const char *, const std::string
&,
156 char **, int) override
;
158 void mourn_inferior () override
;
160 void interrupt () override
;
162 bool thread_alive (ptid_t ptid
) override
;
164 std::string
pid_to_str (ptid_t
) override
;
166 bool has_all_memory () override
;
167 bool has_memory () override
;
168 std::vector
<mem_region
> memory_map () override
;
171 sim_inferior_data
*get_inferior_data_by_ptid (ptid_t ptid
,
172 int sim_instance_needed
);
173 void resume_one_inferior (inferior
*inf
, bool step
, gdb_signal siggnal
);
174 void close_one_inferior (inferior
*inf
);
177 static struct gdbsim_target gdbsim_ops
;
179 static inferior_key
<sim_inferior_data
> sim_inferior_data_key
;
181 /* Flag indicating the "open" status of this module. It's set to 1
182 in gdbsim_open() and 0 in gdbsim_close(). */
183 static int gdbsim_is_open
= 0;
185 /* Argument list to pass to sim_open(). It is allocated in gdbsim_open()
186 and deallocated in gdbsim_close(). The lifetime needs to extend beyond
187 the call to gdbsim_open() due to the fact that other sim instances other
188 than the first will be allocated after the gdbsim_open() call. */
189 static char **sim_argv
= NULL
;
191 /* OS-level callback functions for write, flush, etc. */
192 static host_callback gdb_callback
;
193 static int callbacks_initialized
= 0;
195 /* Flags indicating whether or not a sim instance is needed. One of these
196 flags should be passed to get_sim_inferior_data(). */
198 enum {SIM_INSTANCE_NOT_NEEDED
= 0, SIM_INSTANCE_NEEDED
= 1};
200 /* Obtain pointer to per-inferior simulator data, allocating it if necessary.
201 Attempt to open the sim if SIM_INSTANCE_NEEDED is true. */
203 static struct sim_inferior_data
*
204 get_sim_inferior_data (struct inferior
*inf
, int sim_instance_needed
)
206 SIM_DESC sim_desc
= NULL
;
207 struct sim_inferior_data
*sim_data
= sim_inferior_data_key
.get (inf
);
209 /* Try to allocate a new sim instance, if needed. We do this ahead of
210 a potential allocation of a sim_inferior_data struct in order to
211 avoid needlessly allocating that struct in the event that the sim
212 instance allocation fails. */
213 if (sim_instance_needed
== SIM_INSTANCE_NEEDED
214 && (sim_data
== NULL
|| sim_data
->gdbsim_desc
== NULL
))
216 sim_desc
= sim_open (SIM_OPEN_DEBUG
, &gdb_callback
,
217 current_program_space
->exec_bfd (), sim_argv
);
218 if (sim_desc
== NULL
)
219 error (_("Unable to create simulator instance for inferior %d."),
222 /* Check if the sim descriptor is the same as that of another
224 for (inferior
*other_inf
: all_inferiors ())
226 sim_inferior_data
*other_sim_data
227 = sim_inferior_data_key
.get (other_inf
);
229 if (other_sim_data
!= NULL
230 && other_sim_data
->gdbsim_desc
== sim_desc
)
232 /* We don't close the descriptor due to the fact that it's
233 shared with some other inferior. If we were to close it,
234 that might needlessly muck up the other inferior. Of
235 course, it's possible that the damage has already been
236 done... Note that it *will* ultimately be closed during
237 cleanup of the other inferior. */
240 _("Inferior %d and inferior %d would have identical simulator state.\n"
241 "(This simulator does not support the running of more than one inferior.)"),
242 inf
->num
, other_inf
->num
);
247 if (sim_data
== NULL
)
249 sim_data
= sim_inferior_data_key
.emplace (inf
, sim_desc
);
253 /* This handles the case where sim_data was allocated prior to
254 needing a sim instance. */
255 sim_data
->gdbsim_desc
= sim_desc
;
262 /* Return pointer to per-inferior simulator data using PTID to find the
263 inferior in question. Return NULL when no inferior is found or
264 when ptid has a zero or negative pid component. */
267 gdbsim_target::get_inferior_data_by_ptid (ptid_t ptid
,
268 int sim_instance_needed
)
270 struct inferior
*inf
;
271 int pid
= ptid
.pid ();
276 inf
= find_inferior_pid (this, pid
);
279 return get_sim_inferior_data (inf
, sim_instance_needed
);
284 /* Free the per-inferior simulator data. */
286 sim_inferior_data::~sim_inferior_data ()
289 sim_close (gdbsim_desc
, 0);
293 dump_mem (const gdb_byte
*buf
, int len
)
295 fputs_unfiltered ("\t", gdb_stdlog
);
297 if (len
== 8 || len
== 4)
301 memcpy (l
, buf
, len
);
302 fprintf_unfiltered (gdb_stdlog
, "0x%08x", l
[0]);
304 fprintf_unfiltered (gdb_stdlog
, " 0x%08x", l
[1]);
310 for (i
= 0; i
< len
; i
++)
311 fprintf_unfiltered (gdb_stdlog
, "0x%02x ", buf
[i
]);
314 fputs_unfiltered ("\n", gdb_stdlog
);
317 /* Initialize gdb_callback. */
320 init_callbacks (void)
322 if (!callbacks_initialized
)
324 gdb_callback
= default_callback
;
325 gdb_callback
.init (&gdb_callback
);
326 gdb_callback
.write_stdout
= gdb_os_write_stdout
;
327 gdb_callback
.flush_stdout
= gdb_os_flush_stdout
;
328 gdb_callback
.write_stderr
= gdb_os_write_stderr
;
329 gdb_callback
.flush_stderr
= gdb_os_flush_stderr
;
330 gdb_callback
.printf_filtered
= gdb_os_printf_filtered
;
331 gdb_callback
.vprintf_filtered
= gdb_os_vprintf_filtered
;
332 gdb_callback
.evprintf_filtered
= gdb_os_evprintf_filtered
;
333 gdb_callback
.error
= gdb_os_error
;
334 gdb_callback
.poll_quit
= gdb_os_poll_quit
;
335 gdb_callback
.magic
= HOST_CALLBACK_MAGIC
;
336 callbacks_initialized
= 1;
340 /* Release callbacks (free resources used by them). */
345 if (callbacks_initialized
)
347 gdb_callback
.shutdown (&gdb_callback
);
348 callbacks_initialized
= 0;
352 /* GDB version of os_write_stdout callback. */
355 gdb_os_write_stdout (host_callback
*p
, const char *buf
, int len
)
357 gdb_stdtarg
->write (buf
, len
);
361 /* GDB version of os_flush_stdout callback. */
364 gdb_os_flush_stdout (host_callback
*p
)
366 gdb_stdtarg
->flush ();
369 /* GDB version of os_write_stderr callback. */
372 gdb_os_write_stderr (host_callback
*p
, const char *buf
, int len
)
377 for (i
= 0; i
< len
; i
++)
381 gdb_stdtargerr
->puts (b
);
386 /* GDB version of os_flush_stderr callback. */
389 gdb_os_flush_stderr (host_callback
*p
)
391 gdb_stdtargerr
->flush ();
394 /* GDB version of printf_filtered callback. */
396 static void ATTRIBUTE_PRINTF (2, 3)
397 gdb_os_printf_filtered (host_callback
* p
, const char *format
, ...)
401 va_start (args
, format
);
402 vfprintf_filtered (gdb_stdout
, format
, args
);
406 /* GDB version of error vprintf_filtered. */
408 static void ATTRIBUTE_PRINTF (2, 0)
409 gdb_os_vprintf_filtered (host_callback
* p
, const char *format
, va_list ap
)
411 vfprintf_filtered (gdb_stdout
, format
, ap
);
414 /* GDB version of error evprintf_filtered. */
416 static void ATTRIBUTE_PRINTF (2, 0)
417 gdb_os_evprintf_filtered (host_callback
* p
, const char *format
, va_list ap
)
419 vfprintf_filtered (gdb_stderr
, format
, ap
);
422 /* GDB version of error callback. */
424 static void ATTRIBUTE_PRINTF (2, 3)
425 gdb_os_error (host_callback
* p
, const char *format
, ...)
429 va_start (args
, format
);
430 verror (format
, args
);
435 one2one_register_sim_regno (struct gdbarch
*gdbarch
, int regnum
)
437 /* Only makes sense to supply raw registers. */
438 gdb_assert (regnum
>= 0 && regnum
< gdbarch_num_regs (gdbarch
));
443 gdbsim_target::fetch_registers (struct regcache
*regcache
, int regno
)
445 struct gdbarch
*gdbarch
= regcache
->arch ();
446 struct inferior
*inf
= find_inferior_ptid (this, regcache
->ptid ());
447 struct sim_inferior_data
*sim_data
448 = get_sim_inferior_data (inf
, SIM_INSTANCE_NEEDED
);
452 for (regno
= 0; regno
< gdbarch_num_regs (gdbarch
); regno
++)
453 fetch_registers (regcache
, regno
);
457 switch (gdbarch_register_sim_regno (gdbarch
, regno
))
459 case LEGACY_SIM_REGNO_IGNORE
:
461 case SIM_REGNO_DOES_NOT_EXIST
:
463 /* For moment treat a `does not exist' register the same way
464 as an ``unavailable'' register. */
465 regcache
->raw_supply_zeroed (regno
);
471 static int warn_user
= 1;
472 int regsize
= register_size (gdbarch
, regno
);
473 gdb::byte_vector
buf (regsize
, 0);
476 gdb_assert (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
));
477 nr_bytes
= sim_fetch_register (sim_data
->gdbsim_desc
,
478 gdbarch_register_sim_regno
480 buf
.data (), regsize
);
481 if (nr_bytes
> 0 && nr_bytes
!= regsize
&& warn_user
)
483 fprintf_unfiltered (gdb_stderr
,
484 "Size of register %s (%d/%d) "
485 "incorrect (%d instead of %d))",
486 gdbarch_register_name (gdbarch
, regno
),
488 gdbarch_register_sim_regno (gdbarch
, regno
),
492 /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
493 indicating that GDB and the SIM have different ideas about
494 which registers are fetchable. */
495 /* Else if (nr_bytes < 0): an old simulator, that doesn't
496 think to return the register size. Just assume all is ok. */
497 regcache
->raw_supply (regno
, buf
.data ());
500 fprintf_unfiltered (gdb_stdlog
,
501 "gdbsim_fetch_register: %d", regno
);
502 /* FIXME: We could print something more intelligible. */
503 dump_mem (buf
.data (), regsize
);
512 gdbsim_target::store_registers (struct regcache
*regcache
, int regno
)
514 struct gdbarch
*gdbarch
= regcache
->arch ();
515 struct inferior
*inf
= find_inferior_ptid (this, regcache
->ptid ());
516 struct sim_inferior_data
*sim_data
517 = get_sim_inferior_data (inf
, SIM_INSTANCE_NEEDED
);
521 for (regno
= 0; regno
< gdbarch_num_regs (gdbarch
); regno
++)
522 store_registers (regcache
, regno
);
525 else if (gdbarch_register_sim_regno (gdbarch
, regno
) >= 0)
527 int regsize
= register_size (gdbarch
, regno
);
528 gdb::byte_vector
tmp (regsize
);
531 regcache
->cooked_read (regno
, tmp
.data ());
532 nr_bytes
= sim_store_register (sim_data
->gdbsim_desc
,
533 gdbarch_register_sim_regno
535 tmp
.data (), regsize
);
537 if (nr_bytes
> 0 && nr_bytes
!= regsize
)
538 internal_error (__FILE__
, __LINE__
,
539 _("Register size different to expected"));
541 internal_error (__FILE__
, __LINE__
,
542 _("Register %d not updated"), regno
);
544 warning (_("Register %s not updated"),
545 gdbarch_register_name (gdbarch
, regno
));
549 fprintf_unfiltered (gdb_stdlog
, "gdbsim_store_register: %d", regno
);
550 /* FIXME: We could print something more intelligible. */
551 dump_mem (tmp
.data (), regsize
);
556 /* Kill the running program. This may involve closing any open files
557 and releasing other resources acquired by the simulated program. */
560 gdbsim_target::kill ()
563 fprintf_unfiltered (gdb_stdlog
, "gdbsim_kill\n");
565 /* There is no need to `kill' running simulator - the simulator is
566 not running. Mourning it is enough. */
567 target_mourn_inferior (inferior_ptid
);
570 /* Load an executable file into the target process. This is expected to
571 not only bring new code into the target process, but also to update
572 GDB's symbol tables to match. */
575 gdbsim_target::load (const char *args
, int fromtty
)
578 struct sim_inferior_data
*sim_data
579 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED
);
582 error_no_arg (_("program to load"));
584 gdb_argv
argv (args
);
586 prog
= tilde_expand (argv
[0]);
589 error (_("GDB sim does not yet support a load offset."));
592 fprintf_unfiltered (gdb_stdlog
, "gdbsim_load: prog \"%s\"\n", prog
);
594 /* FIXME: We will print two messages on error.
595 Need error to either not print anything if passed NULL or need
596 another routine that doesn't take any arguments. */
597 if (sim_load (sim_data
->gdbsim_desc
, prog
, NULL
, fromtty
) == SIM_RC_FAIL
)
598 error (_("unable to load program"));
600 /* FIXME: If a load command should reset the targets registers then
601 a call to sim_create_inferior() should go here. */
603 sim_data
->program_loaded
= 1;
607 /* Start an inferior process and set inferior_ptid to its pid.
608 EXEC_FILE is the file to run.
609 ARGS is a string containing the arguments to the program.
610 ENV is the environment vector to pass. Errors reported with error().
611 On VxWorks and various standalone systems, we ignore exec_file. */
612 /* This is called not only when we first attach, but also when the
613 user types "run" after having attached. */
616 gdbsim_target::create_inferior (const char *exec_file
,
617 const std::string
&allargs
,
618 char **env
, int from_tty
)
620 struct sim_inferior_data
*sim_data
621 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED
);
624 const char *args
= allargs
.c_str ();
626 if (exec_file
== 0 || current_program_space
->exec_bfd () == 0)
627 warning (_("No executable file specified."));
628 if (!sim_data
->program_loaded
)
629 warning (_("No program loaded."));
632 fprintf_unfiltered (gdb_stdlog
,
633 "gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
634 (exec_file
? exec_file
: "(NULL)"),
637 if (inferior_ptid
== sim_data
->remote_sim_ptid
)
639 remove_breakpoints ();
640 init_wait_for_inferior ();
643 if (exec_file
!= NULL
)
645 len
= strlen (exec_file
) + 1 + allargs
.size () + 1 + /*slop */ 10;
646 arg_buf
= (char *) alloca (len
);
648 strcat (arg_buf
, exec_file
);
649 strcat (arg_buf
, " ");
650 strcat (arg_buf
, args
);
651 built_argv
.reset (arg_buf
);
654 if (sim_create_inferior (sim_data
->gdbsim_desc
,
655 current_program_space
->exec_bfd (),
656 built_argv
.get (), env
)
658 error (_("Unable to create sim inferior."));
660 inferior_appeared (current_inferior (),
661 sim_data
->remote_sim_ptid
.pid ());
662 thread_info
*thr
= add_thread_silent (this, sim_data
->remote_sim_ptid
);
663 switch_to_thread (thr
);
665 insert_breakpoints (); /* Needed to get correct instruction
668 clear_proceed_status (0);
671 /* The open routine takes the rest of the parameters from the command,
672 and (if successful) pushes a new target onto the stack.
673 Targets should supply this routine, if only to provide an error message. */
674 /* Called when selecting the simulator. E.g. (gdb) target sim name. */
677 gdbsim_target_open (const char *args
, int from_tty
)
681 struct sim_inferior_data
*sim_data
;
683 SIM_DESC gdbsim_desc
;
685 sysroot
= gdb_sysroot
;
686 if (is_target_filename (sysroot
))
687 sysroot
+= strlen (TARGET_SYSROOT_PREFIX
);
690 fprintf_unfiltered (gdb_stdlog
,
691 "gdbsim_open: args \"%s\"\n", args
? args
: "(null)");
693 /* Ensure that the sim target is not on the target stack. This is
694 necessary, because if it is on the target stack, the call to
695 push_target below will invoke sim_close(), thus freeing various
696 state (including a sim instance) that we allocate prior to
697 invoking push_target(). We want to delay the push_target()
698 operation until after we complete those operations which could
701 unpush_target (&gdbsim_ops
);
703 len
= (7 + 1 /* gdbsim */
704 + strlen (" -E little")
705 + strlen (" --architecture=xxxxxxxxxx")
706 + strlen (" --sysroot=") + strlen (sysroot
) +
707 + (args
? strlen (args
) : 0)
709 arg_buf
= (char *) alloca (len
);
710 strcpy (arg_buf
, "gdbsim"); /* 7 */
711 /* Specify the byte order for the target when it is explicitly
712 specified by the user (not auto detected). */
713 switch (selected_byte_order ())
716 strcat (arg_buf
, " -E big");
718 case BFD_ENDIAN_LITTLE
:
719 strcat (arg_buf
, " -E little");
721 case BFD_ENDIAN_UNKNOWN
:
724 /* Specify the architecture of the target when it has been
725 explicitly specified */
726 if (selected_architecture_name () != NULL
)
728 strcat (arg_buf
, " --architecture=");
729 strcat (arg_buf
, selected_architecture_name ());
731 /* Pass along gdb's concept of the sysroot. */
732 strcat (arg_buf
, " --sysroot=");
733 strcat (arg_buf
, sysroot
);
734 /* finally, any explicit args */
737 strcat (arg_buf
, " "); /* 1 */
738 strcat (arg_buf
, args
);
741 gdb_argv
argv (arg_buf
);
742 sim_argv
= argv
.release ();
745 gdbsim_desc
= sim_open (SIM_OPEN_DEBUG
, &gdb_callback
,
746 current_program_space
->exec_bfd (), sim_argv
);
748 if (gdbsim_desc
== 0)
752 error (_("unable to create simulator instance"));
755 /* Reset the pid numberings for this batch of sim instances. */
756 next_pid
= INITIAL_PID
;
758 /* Allocate the inferior data, but do not allocate a sim instance
759 since we've already just done that. */
760 sim_data
= get_sim_inferior_data (current_inferior (),
761 SIM_INSTANCE_NOT_NEEDED
);
763 sim_data
->gdbsim_desc
= gdbsim_desc
;
765 push_target (&gdbsim_ops
);
766 printf_filtered ("Connected to the simulator.\n");
768 /* There's nothing running after "target sim" or "load"; not until
770 switch_to_no_thread ();
775 /* Helper for gdbsim_target::close. */
778 gdbsim_target::close_one_inferior (inferior
*inf
)
780 struct sim_inferior_data
*sim_data
= sim_inferior_data_key
.get (inf
);
781 if (sim_data
!= NULL
)
783 ptid_t ptid
= sim_data
->remote_sim_ptid
;
785 sim_inferior_data_key
.clear (inf
);
787 /* Having a ptid allocated and stored in remote_sim_ptid does
788 not mean that a corresponding inferior was ever created.
789 Thus we need to verify the existence of an inferior using the
790 pid in question before setting inferior_ptid via
791 switch_to_thread() or mourning the inferior. */
792 if (find_inferior_ptid (this, ptid
) != NULL
)
794 switch_to_thread (this, ptid
);
795 generic_mourn_inferior ();
800 /* Close out all files and local state before this target loses control. */
803 gdbsim_target::close ()
806 fprintf_unfiltered (gdb_stdlog
, "gdbsim_close\n");
808 for (inferior
*inf
: all_inferiors (this))
809 close_one_inferior (inf
);
811 if (sim_argv
!= NULL
)
822 /* Takes a program previously attached to and detaches it.
823 The program may resume execution (some targets do, some don't) and will
824 no longer stop on signals, etc. We better not have left any breakpoints
825 in the program or it'll die when it hits one. FROM_TTY says whether to be
827 /* Terminate the open connection to the remote debugger.
828 Use this when you want to detach and do something else with your gdb. */
831 gdbsim_target::detach (inferior
*inf
, int from_tty
)
834 fprintf_unfiltered (gdb_stdlog
, "gdbsim_detach\n");
836 unpush_target (this); /* calls gdbsim_close to do the real work */
838 printf_filtered ("Ending simulator %s debugging\n", target_shortname
);
841 /* Resume execution of the target process. STEP says whether to single-step
842 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
843 to the target, or zero for no signal. */
846 gdbsim_target::resume_one_inferior (inferior
*inf
, bool step
,
849 struct sim_inferior_data
*sim_data
850 = get_sim_inferior_data (inf
, SIM_INSTANCE_NOT_NEEDED
);
854 sim_data
->resume_siggnal
= siggnal
;
855 sim_data
->resume_step
= step
;
858 fprintf_unfiltered (gdb_stdlog
,
859 _("gdbsim_resume: pid %d, step %d, signal %d\n"),
860 inf
->pid
, step
, siggnal
);
865 gdbsim_target::resume (ptid_t ptid
, int step
, enum gdb_signal siggnal
)
867 struct sim_inferior_data
*sim_data
868 = get_inferior_data_by_ptid (ptid
, SIM_INSTANCE_NOT_NEEDED
);
870 /* We don't access any sim_data members within this function.
871 What's of interest is whether or not the call to
872 get_sim_inferior_data_by_ptid(), above, is able to obtain a
873 non-NULL pointer. If it managed to obtain a non-NULL pointer, we
874 know we have a single inferior to consider. If it's NULL, we
875 either have multiple inferiors to resume or an error condition. */
878 resume_one_inferior (find_inferior_ptid (this, ptid
), step
, siggnal
);
879 else if (ptid
== minus_one_ptid
)
881 for (inferior
*inf
: all_inferiors (this))
882 resume_one_inferior (inf
, step
, siggnal
);
885 error (_("The program is not being run."));
888 /* Notify the simulator of an asynchronous request to interrupt.
890 The simulator shall ensure that the interrupt request is eventually
891 delivered to the simulator. If the call is made while the
892 simulator is not running then the interrupt request is processed when
893 the simulator is next resumed.
895 For simulators that do not support this operation, just abort. */
898 gdbsim_target::interrupt ()
900 for (inferior
*inf
: all_inferiors ())
902 sim_inferior_data
*sim_data
903 = get_sim_inferior_data (inf
, SIM_INSTANCE_NEEDED
);
905 if (sim_data
!= nullptr && !sim_stop (sim_data
->gdbsim_desc
))
910 /* GDB version of os_poll_quit callback.
911 Taken from gdb/util.c - should be in a library. */
914 gdb_os_poll_quit (host_callback
*p
)
916 if (deprecated_ui_loop_hook
!= NULL
)
917 deprecated_ui_loop_hook (0);
919 if (check_quit_flag ()) /* gdb's idea of quit */
924 /* Wait for inferior process to do something. Return pid of child,
925 or -1 in case of error; store status through argument pointer STATUS,
926 just as `wait' would. */
929 gdbsim_cntrl_c (int signo
)
931 gdbsim_ops
.interrupt ();
935 gdbsim_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
936 target_wait_flags options
)
938 struct sim_inferior_data
*sim_data
;
939 static sighandler_t prev_sigint
;
941 enum sim_stop reason
= sim_running
;
943 /* This target isn't able to (yet) resume more than one inferior at a time.
944 When ptid is minus_one_ptid, just use the current inferior. If we're
945 given an explicit pid, we'll try to find it and use that instead. */
946 if (ptid
== minus_one_ptid
)
947 sim_data
= get_sim_inferior_data (current_inferior (),
948 SIM_INSTANCE_NEEDED
);
951 sim_data
= get_inferior_data_by_ptid (ptid
, SIM_INSTANCE_NEEDED
);
952 if (sim_data
== NULL
)
953 error (_("Unable to wait for pid %d. Inferior not found."),
958 fprintf_unfiltered (gdb_stdlog
, "gdbsim_wait\n");
960 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
962 struct sigaction sa
, osa
;
963 sa
.sa_handler
= gdbsim_cntrl_c
;
964 sigemptyset (&sa
.sa_mask
);
966 sigaction (SIGINT
, &sa
, &osa
);
967 prev_sigint
= osa
.sa_handler
;
970 prev_sigint
= signal (SIGINT
, gdbsim_cntrl_c
);
972 sim_resume (sim_data
->gdbsim_desc
, sim_data
->resume_step
,
973 sim_data
->resume_siggnal
);
975 signal (SIGINT
, prev_sigint
);
976 sim_data
->resume_step
= 0;
978 sim_stop_reason (sim_data
->gdbsim_desc
, &reason
, &sigrc
);
983 status
->kind
= TARGET_WAITKIND_EXITED
;
984 status
->value
.integer
= sigrc
;
989 case GDB_SIGNAL_ABRT
:
993 case GDB_SIGNAL_TRAP
:
995 status
->kind
= TARGET_WAITKIND_STOPPED
;
996 status
->value
.sig
= (enum gdb_signal
) sigrc
;
1001 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
1002 status
->value
.sig
= (enum gdb_signal
) sigrc
;
1006 /* FIXME: Is this correct? */
1010 return sim_data
->remote_sim_ptid
;
1013 /* Get ready to modify the registers array. On machines which store
1014 individual registers, this doesn't need to do anything. On machines
1015 which store all the registers in one fell swoop, this makes sure
1016 that registers contains all the registers from the program being
1020 gdbsim_target::prepare_to_store (struct regcache
*regcache
)
1022 /* Do nothing, since we can store individual regs. */
1025 /* Helper for gdbsim_xfer_partial that handles memory transfers.
1026 Arguments are like target_xfer_partial. */
1028 static enum target_xfer_status
1029 gdbsim_xfer_memory (struct target_ops
*target
,
1030 gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1031 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
1033 struct sim_inferior_data
*sim_data
1034 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED
);
1037 /* If this target doesn't have memory yet, return 0 causing the
1038 request to be passed to a lower target, hopefully an exec
1040 if (!target
->has_memory ())
1041 return TARGET_XFER_EOF
;
1043 if (!sim_data
->program_loaded
)
1044 error (_("No program loaded."));
1046 /* Note that we obtained the sim_data pointer above using
1047 SIM_INSTANCE_NOT_NEEDED. We do this so that we don't needlessly
1048 allocate a sim instance prior to loading a program. If we
1049 get to this point in the code though, gdbsim_desc should be
1050 non-NULL. (Note that a sim instance is needed in order to load
1052 gdb_assert (sim_data
->gdbsim_desc
!= NULL
);
1055 fprintf_unfiltered (gdb_stdlog
,
1056 "gdbsim_xfer_memory: readbuf %s, writebuf %s, "
1057 "memaddr %s, len %s\n",
1058 host_address_to_string (readbuf
),
1059 host_address_to_string (writebuf
),
1060 paddress (target_gdbarch (), memaddr
),
1065 if (remote_debug
&& len
> 0)
1066 dump_mem (writebuf
, len
);
1067 l
= sim_write (sim_data
->gdbsim_desc
, memaddr
, writebuf
, len
);
1071 l
= sim_read (sim_data
->gdbsim_desc
, memaddr
, readbuf
, len
);
1072 if (remote_debug
&& len
> 0)
1073 dump_mem (readbuf
, len
);
1077 *xfered_len
= (ULONGEST
) l
;
1078 return TARGET_XFER_OK
;
1081 return TARGET_XFER_EOF
;
1083 return TARGET_XFER_E_IO
;
1086 /* Target to_xfer_partial implementation. */
1088 enum target_xfer_status
1089 gdbsim_target::xfer_partial (enum target_object object
,
1090 const char *annex
, gdb_byte
*readbuf
,
1091 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
1092 ULONGEST
*xfered_len
)
1096 case TARGET_OBJECT_MEMORY
:
1097 return gdbsim_xfer_memory (this, readbuf
, writebuf
, offset
, len
,
1101 return TARGET_XFER_E_IO
;
1106 gdbsim_target::files_info ()
1108 struct sim_inferior_data
*sim_data
1109 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED
);
1110 const char *file
= "nothing";
1112 if (current_program_space
->exec_bfd ())
1113 file
= bfd_get_filename (current_program_space
->exec_bfd ());
1116 fprintf_unfiltered (gdb_stdlog
, "gdbsim_files_info: file \"%s\"\n", file
);
1118 if (current_program_space
->exec_bfd ())
1120 fprintf_unfiltered (gdb_stdlog
, "\tAttached to %s running program %s\n",
1121 target_shortname
, file
);
1122 sim_info (sim_data
->gdbsim_desc
, 0);
1126 /* Clear the simulator's notion of what the break points are. */
1129 gdbsim_target::mourn_inferior ()
1132 fprintf_unfiltered (gdb_stdlog
, "gdbsim_mourn_inferior:\n");
1134 remove_breakpoints ();
1135 generic_mourn_inferior ();
1138 /* Pass the command argument through to the simulator verbatim. The
1139 simulator must do any command interpretation work. */
1142 simulator_command (const char *args
, int from_tty
)
1144 struct sim_inferior_data
*sim_data
;
1146 /* We use inferior_data() instead of get_sim_inferior_data() here in
1147 order to avoid attaching a sim_inferior_data struct to an
1148 inferior unnecessarily. The reason we take such care here is due
1149 to the fact that this function, simulator_command(), may be called
1150 even when the sim target is not active. If we were to use
1151 get_sim_inferior_data() here, it is possible that this call would
1152 be made either prior to gdbsim_open() or after gdbsim_close(),
1153 thus allocating memory that would not be garbage collected until
1154 the ultimate destruction of the associated inferior. */
1156 sim_data
= sim_inferior_data_key
.get (current_inferior ());
1157 if (sim_data
== NULL
|| sim_data
->gdbsim_desc
== NULL
)
1160 /* PREVIOUSLY: The user may give a command before the simulator
1161 is opened. [...] (??? assuming of course one wishes to
1162 continue to allow commands to be sent to unopened simulators,
1163 which isn't entirely unreasonable). */
1165 /* The simulator is a builtin abstraction of a remote target.
1166 Consistent with that model, access to the simulator, via sim
1167 commands, is restricted to the period when the channel to the
1168 simulator is open. */
1170 error (_("Not connected to the simulator target"));
1173 sim_do_command (sim_data
->gdbsim_desc
, args
);
1175 /* Invalidate the register cache, in case the simulator command does
1177 registers_changed ();
1181 sim_command_completer (struct cmd_list_element
*ignore
,
1182 completion_tracker
&tracker
,
1183 const char *text
, const char *word
)
1185 struct sim_inferior_data
*sim_data
;
1187 sim_data
= sim_inferior_data_key
.get (current_inferior ());
1188 if (sim_data
== NULL
|| sim_data
->gdbsim_desc
== NULL
)
1191 /* sim_complete_command returns a NULL-terminated malloc'ed array of
1192 malloc'ed strings. */
1193 struct sim_completions_deleter
1195 void operator() (char **ptr
) const
1197 for (size_t i
= 0; ptr
[i
] != NULL
; i
++)
1203 std::unique_ptr
<char *[], sim_completions_deleter
> sim_completions
1204 (sim_complete_command (sim_data
->gdbsim_desc
, text
, word
));
1205 if (sim_completions
== NULL
)
1208 /* Count the elements and add completions from tail to head because
1209 below we'll swap elements out of the array in case add_completion
1210 throws and the deleter deletes until it finds a NULL element. */
1212 while (sim_completions
[count
] != NULL
)
1215 for (size_t i
= count
; i
> 0; i
--)
1217 gdb::unique_xmalloc_ptr
<char> match (sim_completions
[i
- 1]);
1218 sim_completions
[i
- 1] = NULL
;
1219 tracker
.add_completion (std::move (match
));
1223 /* Check to see if a thread is still alive. */
1226 gdbsim_target::thread_alive (ptid_t ptid
)
1228 struct sim_inferior_data
*sim_data
1229 = get_inferior_data_by_ptid (ptid
, SIM_INSTANCE_NOT_NEEDED
);
1231 if (sim_data
== NULL
)
1234 if (ptid
== sim_data
->remote_sim_ptid
)
1235 /* The simulators' task is always alive. */
1241 /* Convert a thread ID to a string. */
1244 gdbsim_target::pid_to_str (ptid_t ptid
)
1246 return normal_pid_to_str (ptid
);
1249 /* Simulator memory may be accessed after the program has been loaded. */
1252 gdbsim_target::has_all_memory ()
1254 struct sim_inferior_data
*sim_data
1255 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED
);
1257 if (!sim_data
->program_loaded
)
1264 gdbsim_target::has_memory ()
1266 struct sim_inferior_data
*sim_data
1267 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED
);
1269 if (!sim_data
->program_loaded
)
1275 /* Get memory map from the simulator. */
1277 std::vector
<mem_region
>
1278 gdbsim_target::memory_map ()
1280 struct sim_inferior_data
*sim_data
1281 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED
);
1282 std::vector
<mem_region
> result
;
1283 gdb::unique_xmalloc_ptr
<char> text (sim_memory_map (sim_data
->gdbsim_desc
));
1285 if (text
!= nullptr)
1286 result
= parse_memory_map (text
.get ());
1291 void _initialize_remote_sim ();
1293 _initialize_remote_sim ()
1295 struct cmd_list_element
*c
;
1297 add_target (gdbsim_target_info
, gdbsim_target_open
);
1299 c
= add_com ("sim", class_obscure
, simulator_command
,
1300 _("Send a command to the simulator."));
1301 set_cmd_completer (c
, sim_command_completer
);