xref: /freebsd-12.1/contrib/gdb/gdb/remote-sim.c (revision 47184b5e)
1 /* Generic remote debugging interface for simulators.
2 
3    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4    2002, 2004 Free Software Foundation, Inc.
5 
6    Contributed by Cygnus Support.
7    Steve Chamberlain ([email protected]).
8 
9    This file is part of GDB.
10 
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place - Suite 330,
24    Boston, MA 02111-1307, USA.  */
25 
26 #include "defs.h"
27 #include "inferior.h"
28 #include "value.h"
29 #include "gdb_string.h"
30 #include <ctype.h>
31 #include <fcntl.h>
32 #include <signal.h>
33 #include <setjmp.h>
34 #include <errno.h>
35 #include "terminal.h"
36 #include "target.h"
37 #include "gdbcore.h"
38 #include "gdb/callback.h"
39 #include "gdb/remote-sim.h"
40 #include "remote-utils.h"
41 #include "command.h"
42 #include "regcache.h"
43 #include "gdb_assert.h"
44 #include "sim-regno.h"
45 #include "arch-utils.h"
46 
47 /* Prototypes */
48 
49 extern void _initialize_remote_sim (void);
50 
51 extern int (*ui_loop_hook) (int signo);
52 
53 static void dump_mem (char *buf, int len);
54 
55 static void init_callbacks (void);
56 
57 static void end_callbacks (void);
58 
59 static int gdb_os_write_stdout (host_callback *, const char *, int);
60 
61 static void gdb_os_flush_stdout (host_callback *);
62 
63 static int gdb_os_write_stderr (host_callback *, const char *, int);
64 
65 static void gdb_os_flush_stderr (host_callback *);
66 
67 static int gdb_os_poll_quit (host_callback *);
68 
69 /* printf_filtered is depreciated */
70 static void gdb_os_printf_filtered (host_callback *, const char *, ...);
71 
72 static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list);
73 
74 static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list);
75 
76 static void gdb_os_error (host_callback *, const char *, ...);
77 
78 static void gdbsim_fetch_register (int regno);
79 
80 static void gdbsim_store_register (int regno);
81 
82 static void gdbsim_kill (void);
83 
84 static void gdbsim_load (char *prog, int fromtty);
85 
86 static void gdbsim_create_inferior (char *exec_file, char *args, char **env);
87 
88 static void gdbsim_open (char *args, int from_tty);
89 
90 static void gdbsim_close (int quitting);
91 
92 static void gdbsim_detach (char *args, int from_tty);
93 
94 static void gdbsim_resume (ptid_t ptid, int step, enum target_signal siggnal);
95 
96 static ptid_t gdbsim_wait (ptid_t ptid, struct target_waitstatus *status);
97 
98 static void gdbsim_prepare_to_store (void);
99 
100 static int gdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr,
101 					int len, int write,
102 					struct mem_attrib *attrib,
103 					struct target_ops *target);
104 
105 static void gdbsim_files_info (struct target_ops *target);
106 
107 static void gdbsim_mourn_inferior (void);
108 
109 static void gdbsim_stop (void);
110 
111 void simulator_command (char *args, int from_tty);
112 
113 /* Naming convention:
114 
115    sim_* are the interface to the simulator (see remote-sim.h).
116    gdbsim_* are stuff which is internal to gdb.  */
117 
118 /* Forward data declarations */
119 extern struct target_ops gdbsim_ops;
120 
121 static int program_loaded = 0;
122 
123 /* We must keep track of whether the simulator has been opened or not because
124    GDB can call a target's close routine twice, but sim_close doesn't allow
125    this.  We also need to record the result of sim_open so we can pass it
126    back to the other sim_foo routines.  */
127 static SIM_DESC gdbsim_desc = 0;
128 
129 static void
dump_mem(char * buf,int len)130 dump_mem (char *buf, int len)
131 {
132   if (len <= 8)
133     {
134       if (len == 8 || len == 4)
135 	{
136 	  long l[2];
137 	  memcpy (l, buf, len);
138 	  printf_filtered ("\t0x%lx", l[0]);
139 	  if (len == 8)
140 	    printf_filtered (" 0x%lx", l[1]);
141 	  printf_filtered ("\n");
142 	}
143       else
144 	{
145 	  int i;
146 	  printf_filtered ("\t");
147 	  for (i = 0; i < len; i++)
148 	    printf_filtered ("0x%x ", buf[i]);
149 	  printf_filtered ("\n");
150 	}
151     }
152 }
153 
154 static host_callback gdb_callback;
155 static int callbacks_initialized = 0;
156 
157 /* Initialize gdb_callback.  */
158 
159 static void
init_callbacks(void)160 init_callbacks (void)
161 {
162   if (!callbacks_initialized)
163     {
164       gdb_callback = default_callback;
165       gdb_callback.init (&gdb_callback);
166       gdb_callback.write_stdout = gdb_os_write_stdout;
167       gdb_callback.flush_stdout = gdb_os_flush_stdout;
168       gdb_callback.write_stderr = gdb_os_write_stderr;
169       gdb_callback.flush_stderr = gdb_os_flush_stderr;
170       gdb_callback.printf_filtered = gdb_os_printf_filtered;
171       gdb_callback.vprintf_filtered = gdb_os_vprintf_filtered;
172       gdb_callback.evprintf_filtered = gdb_os_evprintf_filtered;
173       gdb_callback.error = gdb_os_error;
174       gdb_callback.poll_quit = gdb_os_poll_quit;
175       gdb_callback.magic = HOST_CALLBACK_MAGIC;
176       callbacks_initialized = 1;
177     }
178 }
179 
180 /* Release callbacks (free resources used by them).  */
181 
182 static void
end_callbacks(void)183 end_callbacks (void)
184 {
185   if (callbacks_initialized)
186     {
187       gdb_callback.shutdown (&gdb_callback);
188       callbacks_initialized = 0;
189     }
190 }
191 
192 /* GDB version of os_write_stdout callback.  */
193 
194 static int
gdb_os_write_stdout(host_callback * p,const char * buf,int len)195 gdb_os_write_stdout (host_callback *p, const char *buf, int len)
196 {
197   int i;
198   char b[2];
199 
200   ui_file_write (gdb_stdtarg, buf, len);
201   return len;
202 }
203 
204 /* GDB version of os_flush_stdout callback.  */
205 
206 static void
gdb_os_flush_stdout(host_callback * p)207 gdb_os_flush_stdout (host_callback *p)
208 {
209   gdb_flush (gdb_stdtarg);
210 }
211 
212 /* GDB version of os_write_stderr callback.  */
213 
214 static int
gdb_os_write_stderr(host_callback * p,const char * buf,int len)215 gdb_os_write_stderr (host_callback *p, const char *buf, int len)
216 {
217   int i;
218   char b[2];
219 
220   for (i = 0; i < len; i++)
221     {
222       b[0] = buf[i];
223       b[1] = 0;
224       fputs_unfiltered (b, gdb_stdtargerr);
225     }
226   return len;
227 }
228 
229 /* GDB version of os_flush_stderr callback.  */
230 
231 static void
gdb_os_flush_stderr(host_callback * p)232 gdb_os_flush_stderr (host_callback *p)
233 {
234   gdb_flush (gdb_stdtargerr);
235 }
236 
237 /* GDB version of printf_filtered callback.  */
238 
239 static void
gdb_os_printf_filtered(host_callback * p,const char * format,...)240 gdb_os_printf_filtered (host_callback * p, const char *format,...)
241 {
242   va_list args;
243   va_start (args, format);
244 
245   vfprintf_filtered (gdb_stdout, format, args);
246 
247   va_end (args);
248 }
249 
250 /* GDB version of error vprintf_filtered.  */
251 
252 static void
gdb_os_vprintf_filtered(host_callback * p,const char * format,va_list ap)253 gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap)
254 {
255   vfprintf_filtered (gdb_stdout, format, ap);
256 }
257 
258 /* GDB version of error evprintf_filtered.  */
259 
260 static void
gdb_os_evprintf_filtered(host_callback * p,const char * format,va_list ap)261 gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap)
262 {
263   vfprintf_filtered (gdb_stderr, format, ap);
264 }
265 
266 /* GDB version of error callback.  */
267 
268 static void
gdb_os_error(host_callback * p,const char * format,...)269 gdb_os_error (host_callback * p, const char *format,...)
270 {
271   if (error_hook)
272     (*error_hook) ();
273   else
274     {
275       va_list args;
276       va_start (args, format);
277       verror (format, args);
278       va_end (args);
279     }
280 }
281 
282 int
one2one_register_sim_regno(int regnum)283 one2one_register_sim_regno (int regnum)
284 {
285   /* Only makes sense to supply raw registers.  */
286   gdb_assert (regnum >= 0 && regnum < NUM_REGS);
287   return regnum;
288 }
289 
290 static void
gdbsim_fetch_register(int regno)291 gdbsim_fetch_register (int regno)
292 {
293   if (regno == -1)
294     {
295       for (regno = 0; regno < NUM_REGS; regno++)
296 	gdbsim_fetch_register (regno);
297       return;
298     }
299 
300   switch (REGISTER_SIM_REGNO (regno))
301     {
302     case LEGACY_SIM_REGNO_IGNORE:
303       break;
304     case SIM_REGNO_DOES_NOT_EXIST:
305       {
306 	/* For moment treat a `does not exist' register the same way
307            as an ``unavailable'' register.  */
308 	char buf[MAX_REGISTER_SIZE];
309 	int nr_bytes;
310 	memset (buf, 0, MAX_REGISTER_SIZE);
311 	supply_register (regno, buf);
312 	set_register_cached (regno, -1);
313 	break;
314       }
315     default:
316       {
317 	static int warn_user = 1;
318 	char buf[MAX_REGISTER_SIZE];
319 	int nr_bytes;
320 	gdb_assert (regno >= 0 && regno < NUM_REGS);
321 	memset (buf, 0, MAX_REGISTER_SIZE);
322 	nr_bytes = sim_fetch_register (gdbsim_desc,
323 				       REGISTER_SIM_REGNO (regno),
324 				       buf, DEPRECATED_REGISTER_RAW_SIZE (regno));
325 	if (nr_bytes > 0 && nr_bytes != DEPRECATED_REGISTER_RAW_SIZE (regno) && warn_user)
326 	  {
327 	    fprintf_unfiltered (gdb_stderr,
328 				"Size of register %s (%d/%d) incorrect (%d instead of %d))",
329 				REGISTER_NAME (regno),
330 				regno, REGISTER_SIM_REGNO (regno),
331 				nr_bytes, DEPRECATED_REGISTER_RAW_SIZE (regno));
332 	    warn_user = 0;
333 	  }
334 	/* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
335 	   indicating that GDB and the SIM have different ideas about
336 	   which registers are fetchable.  */
337 	/* Else if (nr_bytes < 0): an old simulator, that doesn't
338 	   think to return the register size.  Just assume all is ok.  */
339 	supply_register (regno, buf);
340 	if (sr_get_debug ())
341 	  {
342 	    printf_filtered ("gdbsim_fetch_register: %d", regno);
343 	    /* FIXME: We could print something more intelligible.  */
344 	    dump_mem (buf, DEPRECATED_REGISTER_RAW_SIZE (regno));
345 	  }
346 	break;
347       }
348     }
349 }
350 
351 
352 static void
gdbsim_store_register(int regno)353 gdbsim_store_register (int regno)
354 {
355   if (regno == -1)
356     {
357       for (regno = 0; regno < NUM_REGS; regno++)
358 	gdbsim_store_register (regno);
359       return;
360     }
361   else if (REGISTER_SIM_REGNO (regno) >= 0)
362     {
363       char tmp[MAX_REGISTER_SIZE];
364       int nr_bytes;
365       deprecated_read_register_gen (regno, tmp);
366       nr_bytes = sim_store_register (gdbsim_desc,
367 				     REGISTER_SIM_REGNO (regno),
368 				     tmp, DEPRECATED_REGISTER_RAW_SIZE (regno));
369       if (nr_bytes > 0 && nr_bytes != DEPRECATED_REGISTER_RAW_SIZE (regno))
370 	internal_error (__FILE__, __LINE__,
371 			"Register size different to expected");
372       /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
373 	 indicating that GDB and the SIM have different ideas about
374 	 which registers are fetchable.  */
375       if (sr_get_debug ())
376 	{
377 	  printf_filtered ("gdbsim_store_register: %d", regno);
378 	  /* FIXME: We could print something more intelligible.  */
379 	  dump_mem (tmp, DEPRECATED_REGISTER_RAW_SIZE (regno));
380 	}
381     }
382 }
383 
384 /* Kill the running program.  This may involve closing any open files
385    and releasing other resources acquired by the simulated program.  */
386 
387 static void
gdbsim_kill(void)388 gdbsim_kill (void)
389 {
390   if (sr_get_debug ())
391     printf_filtered ("gdbsim_kill\n");
392 
393   /* There is no need to `kill' running simulator - the simulator is
394      not running */
395   inferior_ptid = null_ptid;
396 }
397 
398 /* Load an executable file into the target process.  This is expected to
399    not only bring new code into the target process, but also to update
400    GDB's symbol tables to match.  */
401 
402 static void
gdbsim_load(char * prog,int fromtty)403 gdbsim_load (char *prog, int fromtty)
404 {
405   if (sr_get_debug ())
406     printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
407 
408   inferior_ptid = null_ptid;
409 
410   /* FIXME: We will print two messages on error.
411      Need error to either not print anything if passed NULL or need
412      another routine that doesn't take any arguments.  */
413   if (sim_load (gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL)
414     error ("unable to load program");
415 
416   /* FIXME: If a load command should reset the targets registers then
417      a call to sim_create_inferior() should go here. */
418 
419   program_loaded = 1;
420 }
421 
422 
423 /* Start an inferior process and set inferior_ptid to its pid.
424    EXEC_FILE is the file to run.
425    ARGS is a string containing the arguments to the program.
426    ENV is the environment vector to pass.  Errors reported with error().
427    On VxWorks and various standalone systems, we ignore exec_file.  */
428 /* This is called not only when we first attach, but also when the
429    user types "run" after having attached.  */
430 
431 static void
gdbsim_create_inferior(char * exec_file,char * args,char ** env)432 gdbsim_create_inferior (char *exec_file, char *args, char **env)
433 {
434   int len;
435   char *arg_buf, **argv;
436 
437   if (exec_file == 0 || exec_bfd == 0)
438     warning ("No executable file specified.");
439   if (!program_loaded)
440     warning ("No program loaded.");
441 
442   if (sr_get_debug ())
443     printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
444 		     (exec_file ? exec_file : "(NULL)"),
445 		     args);
446 
447   gdbsim_kill ();
448   remove_breakpoints ();
449   init_wait_for_inferior ();
450 
451   if (exec_file != NULL)
452     {
453       len = strlen (exec_file) + 1 + strlen (args) + 1 + /*slop */ 10;
454       arg_buf = (char *) alloca (len);
455       arg_buf[0] = '\0';
456       strcat (arg_buf, exec_file);
457       strcat (arg_buf, " ");
458       strcat (arg_buf, args);
459       argv = buildargv (arg_buf);
460       make_cleanup_freeargv (argv);
461     }
462   else
463     argv = NULL;
464   sim_create_inferior (gdbsim_desc, exec_bfd, argv, env);
465 
466   inferior_ptid = pid_to_ptid (42);
467   insert_breakpoints ();	/* Needed to get correct instruction in cache */
468 
469   clear_proceed_status ();
470 
471   /* NB: Entry point already set by sim_create_inferior. */
472   proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
473 }
474 
475 /* The open routine takes the rest of the parameters from the command,
476    and (if successful) pushes a new target onto the stack.
477    Targets should supply this routine, if only to provide an error message.  */
478 /* Called when selecting the simulator. EG: (gdb) target sim name.  */
479 
480 static void
gdbsim_open(char * args,int from_tty)481 gdbsim_open (char *args, int from_tty)
482 {
483   int len;
484   char *arg_buf;
485   char **argv;
486 
487   if (sr_get_debug ())
488     printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)");
489 
490   /* Remove current simulator if one exists.  Only do this if the simulator
491      has been opened because sim_close requires it.
492      This is important because the call to push_target below will cause
493      sim_close to be called if the simulator is already open, but push_target
494      is called after sim_open!  We can't move the call to push_target before
495      the call to sim_open because sim_open may invoke `error'.  */
496   if (gdbsim_desc != NULL)
497     unpush_target (&gdbsim_ops);
498 
499   len = (7 + 1			/* gdbsim */
500 	 + strlen (" -E little")
501 	 + strlen (" --architecture=xxxxxxxxxx")
502 	 + (args ? strlen (args) : 0)
503 	 + 50) /* slack */ ;
504   arg_buf = (char *) alloca (len);
505   strcpy (arg_buf, "gdbsim");	/* 7 */
506   /* Specify the byte order for the target when it is both selectable
507      and explicitly specified by the user (not auto detected). */
508   switch (selected_byte_order ())
509     {
510     case BFD_ENDIAN_BIG:
511       strcat (arg_buf, " -E big");
512       break;
513     case BFD_ENDIAN_LITTLE:
514       strcat (arg_buf, " -E little");
515       break;
516     case BFD_ENDIAN_UNKNOWN:
517       break;
518     }
519   /* Specify the architecture of the target when it has been
520      explicitly specified */
521   if (selected_architecture_name () != NULL)
522     {
523       strcat (arg_buf, " --architecture=");
524       strcat (arg_buf, selected_architecture_name ());
525     }
526   /* finally, any explicit args */
527   if (args)
528     {
529       strcat (arg_buf, " ");	/* 1 */
530       strcat (arg_buf, args);
531     }
532   argv = buildargv (arg_buf);
533   if (argv == NULL)
534     error ("Insufficient memory available to allocate simulator arg list.");
535   make_cleanup_freeargv (argv);
536 
537   init_callbacks ();
538   gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, argv);
539 
540   if (gdbsim_desc == 0)
541     error ("unable to create simulator instance");
542 
543   push_target (&gdbsim_ops);
544   target_fetch_registers (-1);
545   printf_filtered ("Connected to the simulator.\n");
546 }
547 
548 /* Does whatever cleanup is required for a target that we are no longer
549    going to be calling.  Argument says whether we are quitting gdb and
550    should not get hung in case of errors, or whether we want a clean
551    termination even if it takes a while.  This routine is automatically
552    always called just before a routine is popped off the target stack.
553    Closing file descriptors and freeing memory are typical things it should
554    do.  */
555 /* Close out all files and local state before this target loses control. */
556 
557 static void
gdbsim_close(int quitting)558 gdbsim_close (int quitting)
559 {
560   if (sr_get_debug ())
561     printf_filtered ("gdbsim_close: quitting %d\n", quitting);
562 
563   program_loaded = 0;
564 
565   if (gdbsim_desc != NULL)
566     {
567       sim_close (gdbsim_desc, quitting);
568       gdbsim_desc = NULL;
569     }
570 
571   end_callbacks ();
572   generic_mourn_inferior ();
573 }
574 
575 /* Takes a program previously attached to and detaches it.
576    The program may resume execution (some targets do, some don't) and will
577    no longer stop on signals, etc.  We better not have left any breakpoints
578    in the program or it'll die when it hits one.  ARGS is arguments
579    typed by the user (e.g. a signal to send the process).  FROM_TTY
580    says whether to be verbose or not.  */
581 /* Terminate the open connection to the remote debugger.
582    Use this when you want to detach and do something else with your gdb.  */
583 
584 static void
gdbsim_detach(char * args,int from_tty)585 gdbsim_detach (char *args, int from_tty)
586 {
587   if (sr_get_debug ())
588     printf_filtered ("gdbsim_detach: args \"%s\"\n", args);
589 
590   pop_target ();		/* calls gdbsim_close to do the real work */
591   if (from_tty)
592     printf_filtered ("Ending simulator %s debugging\n", target_shortname);
593 }
594 
595 /* Resume execution of the target process.  STEP says whether to single-step
596    or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
597    to the target, or zero for no signal.  */
598 
599 static enum target_signal resume_siggnal;
600 static int resume_step;
601 
602 static void
gdbsim_resume(ptid_t ptid,int step,enum target_signal siggnal)603 gdbsim_resume (ptid_t ptid, int step, enum target_signal siggnal)
604 {
605   if (PIDGET (inferior_ptid) != 42)
606     error ("The program is not being run.");
607 
608   if (sr_get_debug ())
609     printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
610 
611   resume_siggnal = siggnal;
612   resume_step = step;
613 }
614 
615 /* Notify the simulator of an asynchronous request to stop.
616 
617    The simulator shall ensure that the stop request is eventually
618    delivered to the simulator.  If the call is made while the
619    simulator is not running then the stop request is processed when
620    the simulator is next resumed.
621 
622    For simulators that do not support this operation, just abort */
623 
624 static void
gdbsim_stop(void)625 gdbsim_stop (void)
626 {
627   if (!sim_stop (gdbsim_desc))
628     {
629       quit ();
630     }
631 }
632 
633 /* GDB version of os_poll_quit callback.
634    Taken from gdb/util.c - should be in a library */
635 
636 static int
gdb_os_poll_quit(host_callback * p)637 gdb_os_poll_quit (host_callback *p)
638 {
639   if (ui_loop_hook != NULL)
640     ui_loop_hook (0);
641 
642   if (quit_flag)		/* gdb's idea of quit */
643     {
644       quit_flag = 0;		/* we've stolen it */
645       return 1;
646     }
647   else if (immediate_quit)
648     {
649       return 1;
650     }
651   return 0;
652 }
653 
654 /* Wait for inferior process to do something.  Return pid of child,
655    or -1 in case of error; store status through argument pointer STATUS,
656    just as `wait' would. */
657 
658 static void
gdbsim_cntrl_c(int signo)659 gdbsim_cntrl_c (int signo)
660 {
661   gdbsim_stop ();
662 }
663 
664 static ptid_t
gdbsim_wait(ptid_t ptid,struct target_waitstatus * status)665 gdbsim_wait (ptid_t ptid, struct target_waitstatus *status)
666 {
667   static RETSIGTYPE (*prev_sigint) ();
668   int sigrc = 0;
669   enum sim_stop reason = sim_running;
670 
671   if (sr_get_debug ())
672     printf_filtered ("gdbsim_wait\n");
673 
674 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
675   {
676     struct sigaction sa, osa;
677     sa.sa_handler = gdbsim_cntrl_c;
678     sigemptyset (&sa.sa_mask);
679     sa.sa_flags = 0;
680     sigaction (SIGINT, &sa, &osa);
681     prev_sigint = osa.sa_handler;
682   }
683 #else
684   prev_sigint = signal (SIGINT, gdbsim_cntrl_c);
685 #endif
686   sim_resume (gdbsim_desc, resume_step,
687 	      target_signal_to_host (resume_siggnal));
688   signal (SIGINT, prev_sigint);
689   resume_step = 0;
690 
691   sim_stop_reason (gdbsim_desc, &reason, &sigrc);
692 
693   switch (reason)
694     {
695     case sim_exited:
696       status->kind = TARGET_WAITKIND_EXITED;
697       status->value.integer = sigrc;
698       break;
699     case sim_stopped:
700       switch (sigrc)
701 	{
702 	case SIGABRT:
703 	  quit ();
704 	  break;
705 	case SIGINT:
706 	case SIGTRAP:
707 	default:
708 	  status->kind = TARGET_WAITKIND_STOPPED;
709 	  /* The signal in sigrc is a host signal.  That probably
710 	     should be fixed.  */
711 	  status->value.sig = target_signal_from_host (sigrc);
712 	  break;
713 	}
714       break;
715     case sim_signalled:
716       status->kind = TARGET_WAITKIND_SIGNALLED;
717       /* The signal in sigrc is a host signal.  That probably
718          should be fixed.  */
719       status->value.sig = target_signal_from_host (sigrc);
720       break;
721     case sim_running:
722     case sim_polling:
723       /* FIXME: Is this correct? */
724       break;
725     }
726 
727   return inferior_ptid;
728 }
729 
730 /* Get ready to modify the registers array.  On machines which store
731    individual registers, this doesn't need to do anything.  On machines
732    which store all the registers in one fell swoop, this makes sure
733    that registers contains all the registers from the program being
734    debugged.  */
735 
736 static void
gdbsim_prepare_to_store(void)737 gdbsim_prepare_to_store (void)
738 {
739   /* Do nothing, since we can store individual regs */
740 }
741 
742 /* Transfer LEN bytes between GDB address MYADDR and target address
743    MEMADDR.  If WRITE is non-zero, transfer them to the target,
744    otherwise transfer them from the target.  TARGET is unused.
745 
746    Returns the number of bytes transferred. */
747 
748 static int
gdbsim_xfer_inferior_memory(CORE_ADDR memaddr,char * myaddr,int len,int write,struct mem_attrib * attrib,struct target_ops * target)749 gdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len,
750 			     int write, struct mem_attrib *attrib,
751 			     struct target_ops *target)
752 {
753   if (!program_loaded)
754     error ("No program loaded.");
755 
756   if (sr_get_debug ())
757     {
758       /* FIXME: Send to something other than STDOUT? */
759       printf_filtered ("gdbsim_xfer_inferior_memory: myaddr 0x");
760       gdb_print_host_address (myaddr, gdb_stdout);
761       printf_filtered (", memaddr 0x%s, len %d, write %d\n",
762 		       paddr_nz (memaddr), len, write);
763       if (sr_get_debug () && write)
764 	dump_mem (myaddr, len);
765     }
766 
767   if (write)
768     {
769       len = sim_write (gdbsim_desc, memaddr, myaddr, len);
770     }
771   else
772     {
773       len = sim_read (gdbsim_desc, memaddr, myaddr, len);
774       if (sr_get_debug () && len > 0)
775 	dump_mem (myaddr, len);
776     }
777   return len;
778 }
779 
780 static void
gdbsim_files_info(struct target_ops * target)781 gdbsim_files_info (struct target_ops *target)
782 {
783   char *file = "nothing";
784 
785   if (exec_bfd)
786     file = bfd_get_filename (exec_bfd);
787 
788   if (sr_get_debug ())
789     printf_filtered ("gdbsim_files_info: file \"%s\"\n", file);
790 
791   if (exec_bfd)
792     {
793       printf_filtered ("\tAttached to %s running program %s\n",
794 		       target_shortname, file);
795       sim_info (gdbsim_desc, 0);
796     }
797 }
798 
799 /* Clear the simulator's notion of what the break points are.  */
800 
801 static void
gdbsim_mourn_inferior(void)802 gdbsim_mourn_inferior (void)
803 {
804   if (sr_get_debug ())
805     printf_filtered ("gdbsim_mourn_inferior:\n");
806 
807   remove_breakpoints ();
808   generic_mourn_inferior ();
809 }
810 
811 static int
gdbsim_insert_breakpoint(CORE_ADDR addr,char * contents_cache)812 gdbsim_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
813 {
814   return memory_insert_breakpoint (addr, contents_cache);
815 }
816 
817 static int
gdbsim_remove_breakpoint(CORE_ADDR addr,char * contents_cache)818 gdbsim_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
819 {
820   return memory_remove_breakpoint (addr, contents_cache);
821 }
822 
823 /* Pass the command argument through to the simulator verbatim.  The
824    simulator must do any command interpretation work.  */
825 
826 void
simulator_command(char * args,int from_tty)827 simulator_command (char *args, int from_tty)
828 {
829   if (gdbsim_desc == NULL)
830     {
831 
832       /* PREVIOUSLY: The user may give a command before the simulator
833          is opened. [...] (??? assuming of course one wishes to
834          continue to allow commands to be sent to unopened simulators,
835          which isn't entirely unreasonable). */
836 
837       /* The simulator is a builtin abstraction of a remote target.
838          Consistent with that model, access to the simulator, via sim
839          commands, is restricted to the period when the channel to the
840          simulator is open. */
841 
842       error ("Not connected to the simulator target");
843     }
844 
845   sim_do_command (gdbsim_desc, args);
846 
847   /* Invalidate the register cache, in case the simulator command does
848      something funny. */
849   registers_changed ();
850 }
851 
852 /* Define the target subroutine names */
853 
854 struct target_ops gdbsim_ops;
855 
856 static void
init_gdbsim_ops(void)857 init_gdbsim_ops (void)
858 {
859   gdbsim_ops.to_shortname = "sim";
860   gdbsim_ops.to_longname = "simulator";
861   gdbsim_ops.to_doc = "Use the compiled-in simulator.";
862   gdbsim_ops.to_open = gdbsim_open;
863   gdbsim_ops.to_close = gdbsim_close;
864   gdbsim_ops.to_detach = gdbsim_detach;
865   gdbsim_ops.to_resume = gdbsim_resume;
866   gdbsim_ops.to_wait = gdbsim_wait;
867   gdbsim_ops.to_fetch_registers = gdbsim_fetch_register;
868   gdbsim_ops.to_store_registers = gdbsim_store_register;
869   gdbsim_ops.to_prepare_to_store = gdbsim_prepare_to_store;
870   gdbsim_ops.to_xfer_memory = gdbsim_xfer_inferior_memory;
871   gdbsim_ops.to_files_info = gdbsim_files_info;
872   gdbsim_ops.to_insert_breakpoint = gdbsim_insert_breakpoint;
873   gdbsim_ops.to_remove_breakpoint = gdbsim_remove_breakpoint;
874   gdbsim_ops.to_kill = gdbsim_kill;
875   gdbsim_ops.to_load = gdbsim_load;
876   gdbsim_ops.to_create_inferior = gdbsim_create_inferior;
877   gdbsim_ops.to_mourn_inferior = gdbsim_mourn_inferior;
878   gdbsim_ops.to_stop = gdbsim_stop;
879   gdbsim_ops.to_stratum = process_stratum;
880   gdbsim_ops.to_has_all_memory = 1;
881   gdbsim_ops.to_has_memory = 1;
882   gdbsim_ops.to_has_stack = 1;
883   gdbsim_ops.to_has_registers = 1;
884   gdbsim_ops.to_has_execution = 1;
885   gdbsim_ops.to_magic = OPS_MAGIC;
886 
887 #ifdef TARGET_REDEFINE_DEFAULT_OPS
888   TARGET_REDEFINE_DEFAULT_OPS (&gdbsim_ops);
889 #endif
890 }
891 
892 void
_initialize_remote_sim(void)893 _initialize_remote_sim (void)
894 {
895   init_gdbsim_ops ();
896   add_target (&gdbsim_ops);
897 
898   add_com ("sim <command>", class_obscure, simulator_command,
899 	   "Send a command to the simulator.");
900 }
901