2004-05-25 Andrew Cagney <cagney@gnu.com>
[deliverable/binutils-gdb.git] / gdb / remote-sim.c
CommitLineData
c906108c 1/* Generic remote debugging interface for simulators.
0a65a603
AC
2
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
59773b4e 4 2002, 2004 Free Software Foundation, Inc.
0a65a603 5
c906108c
SS
6 Contributed by Cygnus Support.
7 Steve Chamberlain (sac@cygnus.com).
8
c5aa993b 9 This file is part of GDB.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b
JM
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.
c906108c 20
c5aa993b
JM
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. */
c906108c
SS
25
26#include "defs.h"
27#include "inferior.h"
c906108c
SS
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"
3c25f8c7
AC
38#include "gdb/callback.h"
39#include "gdb/remote-sim.h"
c906108c
SS
40#include "remote-utils.h"
41#include "command.h"
4e052eda 42#include "regcache.h"
8238d0bf
AC
43#include "gdb_assert.h"
44#include "sim-regno.h"
a8cf2722 45#include "arch-utils.h"
c906108c
SS
46
47/* Prototypes */
48
a14ed312 49extern void _initialize_remote_sim (void);
392a587b 50
507f3c78 51extern int (*ui_loop_hook) (int signo);
7a292a7a 52
a14ed312 53static void dump_mem (char *buf, int len);
c906108c 54
a14ed312 55static void init_callbacks (void);
c906108c 56
a14ed312 57static void end_callbacks (void);
c906108c 58
a14ed312 59static int gdb_os_write_stdout (host_callback *, const char *, int);
c906108c 60
a14ed312 61static void gdb_os_flush_stdout (host_callback *);
c906108c 62
a14ed312 63static int gdb_os_write_stderr (host_callback *, const char *, int);
c906108c 64
a14ed312 65static void gdb_os_flush_stderr (host_callback *);
c906108c 66
a14ed312 67static int gdb_os_poll_quit (host_callback *);
c906108c
SS
68
69/* printf_filtered is depreciated */
a14ed312 70static void gdb_os_printf_filtered (host_callback *, const char *, ...);
c906108c 71
a14ed312 72static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list);
c906108c 73
a14ed312 74static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list);
c906108c 75
a14ed312 76static void gdb_os_error (host_callback *, const char *, ...);
c906108c 77
a14ed312 78static void gdbsim_fetch_register (int regno);
c906108c 79
a14ed312 80static void gdbsim_store_register (int regno);
c906108c 81
a14ed312 82static void gdbsim_kill (void);
c906108c 83
a14ed312 84static void gdbsim_load (char *prog, int fromtty);
c906108c 85
a14ed312 86static void gdbsim_create_inferior (char *exec_file, char *args, char **env);
c906108c 87
a14ed312 88static void gdbsim_open (char *args, int from_tty);
c906108c 89
a14ed312 90static void gdbsim_close (int quitting);
c906108c 91
a14ed312 92static void gdbsim_detach (char *args, int from_tty);
c906108c 93
39f77062 94static void gdbsim_resume (ptid_t ptid, int step, enum target_signal siggnal);
c906108c 95
39f77062 96static ptid_t gdbsim_wait (ptid_t ptid, struct target_waitstatus *status);
c906108c 97
a14ed312 98static void gdbsim_prepare_to_store (void);
c906108c 99
29e57380
C
100static 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);
c906108c 104
a14ed312 105static void gdbsim_files_info (struct target_ops *target);
c906108c 106
a14ed312 107static void gdbsim_mourn_inferior (void);
c906108c 108
a14ed312 109static void gdbsim_stop (void);
c906108c 110
a14ed312 111void simulator_command (char *args, int from_tty);
c906108c
SS
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 */
119extern struct target_ops gdbsim_ops;
120
121static 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. */
127static SIM_DESC gdbsim_desc = 0;
128
129static void
fba45db2 130dump_mem (char *buf, int len)
c906108c
SS
131{
132 if (len <= 8)
133 {
134 if (len == 8 || len == 4)
135 {
136 long l[2];
137 memcpy (l, buf, len);
d4f3574e 138 printf_filtered ("\t0x%lx", l[0]);
a6da1910
AC
139 if (len == 8)
140 printf_filtered (" 0x%lx", l[1]);
141 printf_filtered ("\n");
c906108c
SS
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
154static host_callback gdb_callback;
155static int callbacks_initialized = 0;
156
157/* Initialize gdb_callback. */
158
159static void
fba45db2 160init_callbacks (void)
c906108c 161{
c5aa993b 162 if (!callbacks_initialized)
c906108c
SS
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
182static void
fba45db2 183end_callbacks (void)
c906108c
SS
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
c5aa993b 194static int
fba45db2 195gdb_os_write_stdout (host_callback *p, const char *buf, int len)
c906108c
SS
196{
197 int i;
198 char b[2];
199
d9fcf2fb 200 ui_file_write (gdb_stdtarg, buf, len);
c906108c
SS
201 return len;
202}
203
204/* GDB version of os_flush_stdout callback. */
205
206static void
fba45db2 207gdb_os_flush_stdout (host_callback *p)
c906108c 208{
4ce44c66 209 gdb_flush (gdb_stdtarg);
c906108c
SS
210}
211
212/* GDB version of os_write_stderr callback. */
213
c5aa993b 214static int
fba45db2 215gdb_os_write_stderr (host_callback *p, const char *buf, int len)
c906108c
SS
216{
217 int i;
218 char b[2];
219
c5aa993b 220 for (i = 0; i < len; i++)
c906108c
SS
221 {
222 b[0] = buf[i];
223 b[1] = 0;
22e8e3c7 224 fputs_unfiltered (b, gdb_stdtargerr);
c906108c
SS
225 }
226 return len;
227}
228
229/* GDB version of os_flush_stderr callback. */
230
231static void
fba45db2 232gdb_os_flush_stderr (host_callback *p)
c906108c 233{
22e8e3c7 234 gdb_flush (gdb_stdtargerr);
c906108c
SS
235}
236
237/* GDB version of printf_filtered callback. */
238
c906108c 239static void
c5aa993b 240gdb_os_printf_filtered (host_callback * p, const char *format,...)
c906108c
SS
241{
242 va_list args;
c906108c 243 va_start (args, format);
c906108c
SS
244
245 vfprintf_filtered (gdb_stdout, format, args);
246
247 va_end (args);
248}
249
250/* GDB version of error vprintf_filtered. */
251
c906108c 252static void
c5aa993b 253gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap)
c906108c
SS
254{
255 vfprintf_filtered (gdb_stdout, format, ap);
256}
257
258/* GDB version of error evprintf_filtered. */
259
c906108c 260static void
c5aa993b 261gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap)
c906108c
SS
262{
263 vfprintf_filtered (gdb_stderr, format, ap);
264}
265
266/* GDB version of error callback. */
267
c906108c 268static void
c5aa993b 269gdb_os_error (host_callback * p, const char *format,...)
c906108c 270{
9a4105ab
AC
271 if (deprecated_error_hook)
272 (*deprecated_error_hook) ();
c5aa993b 273 else
c906108c
SS
274 {
275 va_list args;
c906108c 276 va_start (args, format);
4ce44c66 277 verror (format, args);
c906108c 278 va_end (args);
c906108c
SS
279 }
280}
281
8238d0bf
AC
282int
283one2one_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
c906108c 290static void
fba45db2 291gdbsim_fetch_register (int regno)
c906108c 292{
c5aa993b 293 if (regno == -1)
c906108c
SS
294 {
295 for (regno = 0; regno < NUM_REGS; regno++)
296 gdbsim_fetch_register (regno);
8238d0bf 297 return;
c906108c 298 }
8238d0bf
AC
299
300 switch (REGISTER_SIM_REGNO (regno))
c906108c 301 {
8238d0bf
AC
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. */
d9d9c31f 308 char buf[MAX_REGISTER_SIZE];
8238d0bf 309 int nr_bytes;
d9d9c31f 310 memset (buf, 0, MAX_REGISTER_SIZE);
8238d0bf
AC
311 supply_register (regno, buf);
312 set_register_cached (regno, -1);
313 break;
314 }
315 default:
316 {
317 static int warn_user = 1;
d9d9c31f 318 char buf[MAX_REGISTER_SIZE];
8238d0bf
AC
319 int nr_bytes;
320 gdb_assert (regno >= 0 && regno < NUM_REGS);
d9d9c31f 321 memset (buf, 0, MAX_REGISTER_SIZE);
d4f3574e
SS
322 nr_bytes = sim_fetch_register (gdbsim_desc,
323 REGISTER_SIM_REGNO (regno),
12c266ea
AC
324 buf, DEPRECATED_REGISTER_RAW_SIZE (regno));
325 if (nr_bytes > 0 && nr_bytes != DEPRECATED_REGISTER_RAW_SIZE (regno) && warn_user)
8238d0bf
AC
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),
12c266ea 331 nr_bytes, DEPRECATED_REGISTER_RAW_SIZE (regno));
8238d0bf
AC
332 warn_user = 0;
333 }
334 /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
278a7cf7 335 indicating that GDB and the SIM have different ideas about
8238d0bf
AC
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. */
12c266ea 344 dump_mem (buf, DEPRECATED_REGISTER_RAW_SIZE (regno));
8238d0bf
AC
345 }
346 break;
347 }
c906108c
SS
348 }
349}
350
351
352static void
fba45db2 353gdbsim_store_register (int regno)
c906108c 354{
c5aa993b 355 if (regno == -1)
c906108c
SS
356 {
357 for (regno = 0; regno < NUM_REGS; regno++)
358 gdbsim_store_register (regno);
8238d0bf 359 return;
c906108c 360 }
8238d0bf 361 else if (REGISTER_SIM_REGNO (regno) >= 0)
c906108c 362 {
123a958e 363 char tmp[MAX_REGISTER_SIZE];
c906108c 364 int nr_bytes;
4caf0990 365 deprecated_read_register_gen (regno, tmp);
d4f3574e
SS
366 nr_bytes = sim_store_register (gdbsim_desc,
367 REGISTER_SIM_REGNO (regno),
12c266ea
AC
368 tmp, DEPRECATED_REGISTER_RAW_SIZE (regno));
369 if (nr_bytes > 0 && nr_bytes != DEPRECATED_REGISTER_RAW_SIZE (regno))
8e65ff28
AC
370 internal_error (__FILE__, __LINE__,
371 "Register size different to expected");
8238d0bf 372 /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
278a7cf7 373 indicating that GDB and the SIM have different ideas about
8238d0bf 374 which registers are fetchable. */
c906108c
SS
375 if (sr_get_debug ())
376 {
377 printf_filtered ("gdbsim_store_register: %d", regno);
378 /* FIXME: We could print something more intelligible. */
12c266ea 379 dump_mem (tmp, DEPRECATED_REGISTER_RAW_SIZE (regno));
c906108c
SS
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
387static void
fba45db2 388gdbsim_kill (void)
c906108c
SS
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 */
39f77062 395 inferior_ptid = null_ptid;
c906108c
SS
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
402static void
fba45db2 403gdbsim_load (char *prog, int fromtty)
c906108c
SS
404{
405 if (sr_get_debug ())
406 printf_filtered ("gdbsim_load: prog \"%s\"\n", prog);
407
39f77062 408 inferior_ptid = null_ptid;
c906108c
SS
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
39f77062 423/* Start an inferior process and set inferior_ptid to its pid.
c906108c
SS
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
431static void
fba45db2 432gdbsim_create_inferior (char *exec_file, char *args, char **env)
c906108c
SS
433{
434 int len;
c5aa993b 435 char *arg_buf, **argv;
c906108c
SS
436
437 if (exec_file == 0 || exec_bfd == 0)
438 warning ("No executable file specified.");
c5aa993b 439 if (!program_loaded)
c906108c
SS
440 warning ("No program loaded.");
441
442 if (sr_get_debug ())
443 printf_filtered ("gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
c5aa993b 444 (exec_file ? exec_file : "(NULL)"),
c906108c
SS
445 args);
446
c5aa993b 447 gdbsim_kill ();
c906108c
SS
448 remove_breakpoints ();
449 init_wait_for_inferior ();
450
451 if (exec_file != NULL)
452 {
c5aa993b 453 len = strlen (exec_file) + 1 + strlen (args) + 1 + /*slop */ 10;
c906108c
SS
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);
7a292a7a 460 make_cleanup_freeargv (argv);
c906108c
SS
461 }
462 else
463 argv = NULL;
464 sim_create_inferior (gdbsim_desc, exec_bfd, argv, env);
465
39f77062 466 inferior_ptid = pid_to_ptid (42);
c5aa993b 467 insert_breakpoints (); /* Needed to get correct instruction in cache */
c906108c
SS
468
469 clear_proceed_status ();
470
471 /* NB: Entry point already set by sim_create_inferior. */
2acceee2 472 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
c906108c
SS
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
480static void
fba45db2 481gdbsim_open (char *args, int from_tty)
c906108c
SS
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
c5aa993b 499 len = (7 + 1 /* gdbsim */
c906108c
SS
500 + strlen (" -E little")
501 + strlen (" --architecture=xxxxxxxxxx")
502 + (args ? strlen (args) : 0)
c5aa993b 503 + 50) /* slack */ ;
c906108c 504 arg_buf = (char *) alloca (len);
c5aa993b 505 strcpy (arg_buf, "gdbsim"); /* 7 */
c906108c
SS
506 /* Specify the byte order for the target when it is both selectable
507 and explicitly specified by the user (not auto detected). */
a8cf2722 508 switch (selected_byte_order ())
c906108c 509 {
a8cf2722
AC
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;
c906108c
SS
518 }
519 /* Specify the architecture of the target when it has been
520 explicitly specified */
a8cf2722 521 if (selected_architecture_name () != NULL)
c906108c
SS
522 {
523 strcat (arg_buf, " --architecture=");
a8cf2722 524 strcat (arg_buf, selected_architecture_name ());
c906108c
SS
525 }
526 /* finally, any explicit args */
527 if (args)
528 {
c5aa993b 529 strcat (arg_buf, " "); /* 1 */
c906108c
SS
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.");
7a292a7a 535 make_cleanup_freeargv (argv);
c906108c
SS
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
557static void
fba45db2 558gdbsim_close (int quitting)
c906108c
SS
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 ();
75660bc0 572 generic_mourn_inferior ();
c906108c
SS
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
584static void
fba45db2 585gdbsim_detach (char *args, int from_tty)
c906108c
SS
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}
c5aa993b 594
c906108c
SS
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
599static enum target_signal resume_siggnal;
600static int resume_step;
601
602static void
39f77062 603gdbsim_resume (ptid_t ptid, int step, enum target_signal siggnal)
c906108c 604{
39f77062 605 if (PIDGET (inferior_ptid) != 42)
c906108c
SS
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.
c5aa993b 616
c906108c
SS
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
624static void
fba45db2 625gdbsim_stop (void)
c906108c 626{
c5aa993b 627 if (!sim_stop (gdbsim_desc))
c906108c
SS
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
636static int
fba45db2 637gdb_os_poll_quit (host_callback *p)
c906108c 638{
7a292a7a
SS
639 if (ui_loop_hook != NULL)
640 ui_loop_hook (0);
641
c5aa993b 642 if (quit_flag) /* gdb's idea of quit */
c906108c 643 {
c5aa993b 644 quit_flag = 0; /* we've stolen it */
c906108c
SS
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
658static void
fba45db2 659gdbsim_cntrl_c (int signo)
c906108c
SS
660{
661 gdbsim_stop ();
662}
663
39f77062
KB
664static ptid_t
665gdbsim_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c
SS
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
c5aa993b 718 should be fixed. */
c906108c
SS
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
39f77062 727 return inferior_ptid;
c906108c
SS
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
736static void
fba45db2 737gdbsim_prepare_to_store (void)
c906108c
SS
738{
739 /* Do nothing, since we can store individual regs */
740}
741
d93bce06
KB
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
c906108c 748static int
d93bce06 749gdbsim_xfer_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len,
0a65a603
AC
750 int write, struct mem_attrib *attrib,
751 struct target_ops *target)
c906108c 752{
c5aa993b 753 if (!program_loaded)
c906108c
SS
754 error ("No program loaded.");
755
756 if (sr_get_debug ())
757 {
d4f3574e
SS
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);
c906108c 763 if (sr_get_debug () && write)
c5aa993b 764 dump_mem (myaddr, len);
c906108c
SS
765 }
766
767 if (write)
768 {
769 len = sim_write (gdbsim_desc, memaddr, myaddr, len);
770 }
c5aa993b 771 else
c906108c
SS
772 {
773 len = sim_read (gdbsim_desc, memaddr, myaddr, len);
774 if (sr_get_debug () && len > 0)
c5aa993b
JM
775 dump_mem (myaddr, len);
776 }
c906108c
SS
777 return len;
778}
779
780static void
fba45db2 781gdbsim_files_info (struct target_ops *target)
c906108c
SS
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
801static void
fba45db2 802gdbsim_mourn_inferior (void)
c5aa993b 803{
c906108c
SS
804 if (sr_get_debug ())
805 printf_filtered ("gdbsim_mourn_inferior:\n");
806
807 remove_breakpoints ();
808 generic_mourn_inferior ();
809}
810
811static int
fba45db2 812gdbsim_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
c906108c 813{
c906108c 814 return memory_insert_breakpoint (addr, contents_cache);
c906108c
SS
815}
816
817static int
fba45db2 818gdbsim_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
c906108c 819{
c906108c 820 return memory_remove_breakpoint (addr, contents_cache);
c906108c
SS
821}
822
823/* Pass the command argument through to the simulator verbatim. The
824 simulator must do any command interpretation work. */
825
826void
fba45db2 827simulator_command (char *args, int from_tty)
c906108c
SS
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. */
c5aa993b 849 registers_changed ();
c906108c
SS
850}
851
852/* Define the target subroutine names */
853
c5aa993b
JM
854struct target_ops gdbsim_ops;
855
856static void
857init_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;
c5aa993b 864 gdbsim_ops.to_detach = gdbsim_detach;
c5aa993b
JM
865 gdbsim_ops.to_resume = gdbsim_resume;
866 gdbsim_ops.to_wait = gdbsim_wait;
c5aa993b
JM
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;
c5aa993b
JM
874 gdbsim_ops.to_kill = gdbsim_kill;
875 gdbsim_ops.to_load = gdbsim_load;
c5aa993b 876 gdbsim_ops.to_create_inferior = gdbsim_create_inferior;
c5aa993b 877 gdbsim_ops.to_mourn_inferior = gdbsim_mourn_inferior;
c5aa993b 878 gdbsim_ops.to_stop = gdbsim_stop;
c5aa993b 879 gdbsim_ops.to_stratum = process_stratum;
c5aa993b
JM
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;
c5aa993b 885 gdbsim_ops.to_magic = OPS_MAGIC;
c906108c
SS
886
887#ifdef TARGET_REDEFINE_DEFAULT_OPS
888 TARGET_REDEFINE_DEFAULT_OPS (&gdbsim_ops);
889#endif
890}
891
892void
fba45db2 893_initialize_remote_sim (void)
c906108c 894{
c5aa993b 895 init_gdbsim_ops ();
c906108c
SS
896 add_target (&gdbsim_ops);
897
898 add_com ("sim <command>", class_obscure, simulator_command,
c5aa993b 899 "Send a command to the simulator.");
c906108c 900}
This page took 0.458283 seconds and 4 git commands to generate.