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