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