| 1 | /* Generic remote debugging interface for simulators. |
| 2 | |
| 3 | Copyright (C) 1993-2021 Free Software Foundation, Inc. |
| 4 | |
| 5 | Contributed by Cygnus Support. |
| 6 | Steve Chamberlain (sac@cygnus.com). |
| 7 | |
| 8 | This file is part of GDB. |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 3 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include "gdb_bfd.h" |
| 25 | #include "inferior.h" |
| 26 | #include "infrun.h" |
| 27 | #include "value.h" |
| 28 | #include <ctype.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <signal.h> |
| 31 | #include <setjmp.h> |
| 32 | #include "terminal.h" |
| 33 | #include "target.h" |
| 34 | #include "process-stratum-target.h" |
| 35 | #include "gdbcore.h" |
| 36 | #include "gdb/callback.h" |
| 37 | #include "gdb/remote-sim.h" |
| 38 | #include "command.h" |
| 39 | #include "regcache.h" |
| 40 | #include "sim-regno.h" |
| 41 | #include "arch-utils.h" |
| 42 | #include "readline/readline.h" |
| 43 | #include "gdbthread.h" |
| 44 | #include "gdbsupport/byte-vector.h" |
| 45 | #include "memory-map.h" |
| 46 | #include "remote.h" |
| 47 | |
| 48 | /* Prototypes */ |
| 49 | |
| 50 | static void init_callbacks (void); |
| 51 | |
| 52 | static void end_callbacks (void); |
| 53 | |
| 54 | static int gdb_os_write_stdout (host_callback *, const char *, int); |
| 55 | |
| 56 | static void gdb_os_flush_stdout (host_callback *); |
| 57 | |
| 58 | static int gdb_os_write_stderr (host_callback *, const char *, int); |
| 59 | |
| 60 | static void gdb_os_flush_stderr (host_callback *); |
| 61 | |
| 62 | static int gdb_os_poll_quit (host_callback *); |
| 63 | |
| 64 | /* printf_filtered is depreciated. */ |
| 65 | static void gdb_os_printf_filtered (host_callback *, const char *, ...); |
| 66 | |
| 67 | static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list); |
| 68 | |
| 69 | static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list); |
| 70 | |
| 71 | static void gdb_os_error (host_callback *, const char *, ...) |
| 72 | ATTRIBUTE_NORETURN; |
| 73 | |
| 74 | /* Naming convention: |
| 75 | |
| 76 | sim_* are the interface to the simulator (see remote-sim.h). |
| 77 | gdbsim_* are stuff which is internal to gdb. */ |
| 78 | |
| 79 | /* Value of the next pid to allocate for an inferior. As indicated |
| 80 | elsewhere, its initial value is somewhat arbitrary; it's critical |
| 81 | though that it's not zero or negative. */ |
| 82 | static int next_pid; |
| 83 | #define INITIAL_PID 42000 |
| 84 | |
| 85 | /* Simulator-specific, per-inferior state. */ |
| 86 | struct sim_inferior_data { |
| 87 | explicit sim_inferior_data (SIM_DESC desc) |
| 88 | : gdbsim_desc (desc), |
| 89 | remote_sim_ptid (next_pid, 0, next_pid) |
| 90 | { |
| 91 | gdb_assert (remote_sim_ptid != null_ptid); |
| 92 | ++next_pid; |
| 93 | } |
| 94 | |
| 95 | ~sim_inferior_data (); |
| 96 | |
| 97 | /* Flag which indicates whether or not the program has been loaded. */ |
| 98 | int program_loaded = 0; |
| 99 | |
| 100 | /* Simulator descriptor for this inferior. */ |
| 101 | SIM_DESC gdbsim_desc; |
| 102 | |
| 103 | /* This is the ptid we use for this particular simulator instance. Its |
| 104 | value is somewhat arbitrary, as the simulator target don't have a |
| 105 | notion of tasks or threads, but we need something non-null to place |
| 106 | in inferior_ptid. For simulators which permit multiple instances, |
| 107 | we also need a unique identifier to use for each inferior. */ |
| 108 | ptid_t remote_sim_ptid; |
| 109 | |
| 110 | /* Signal with which to resume. */ |
| 111 | enum gdb_signal resume_siggnal = GDB_SIGNAL_0; |
| 112 | |
| 113 | /* Flag which indicates whether resume should step or not. */ |
| 114 | int resume_step = 0; |
| 115 | }; |
| 116 | |
| 117 | static const target_info gdbsim_target_info = { |
| 118 | "sim", |
| 119 | N_("simulator"), |
| 120 | N_("Use the compiled-in simulator.") |
| 121 | }; |
| 122 | |
| 123 | struct gdbsim_target final |
| 124 | : public memory_breakpoint_target<process_stratum_target> |
| 125 | { |
| 126 | gdbsim_target () = default; |
| 127 | |
| 128 | const target_info &info () const override |
| 129 | { return gdbsim_target_info; } |
| 130 | |
| 131 | void close () override; |
| 132 | |
| 133 | void detach (inferior *inf, int) override; |
| 134 | |
| 135 | void resume (ptid_t, int, enum gdb_signal) override; |
| 136 | ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override; |
| 137 | |
| 138 | void fetch_registers (struct regcache *, int) override; |
| 139 | void store_registers (struct regcache *, int) override; |
| 140 | void prepare_to_store (struct regcache *) override; |
| 141 | |
| 142 | enum target_xfer_status xfer_partial (enum target_object object, |
| 143 | const char *annex, |
| 144 | gdb_byte *readbuf, |
| 145 | const gdb_byte *writebuf, |
| 146 | ULONGEST offset, ULONGEST len, |
| 147 | ULONGEST *xfered_len) override; |
| 148 | |
| 149 | void files_info () override; |
| 150 | |
| 151 | void kill () override; |
| 152 | |
| 153 | void load (const char *, int) override; |
| 154 | |
| 155 | bool can_create_inferior () override { return true; } |
| 156 | void create_inferior (const char *, const std::string &, |
| 157 | char **, int) override; |
| 158 | |
| 159 | void mourn_inferior () override; |
| 160 | |
| 161 | void interrupt () override; |
| 162 | |
| 163 | bool thread_alive (ptid_t ptid) override; |
| 164 | |
| 165 | std::string pid_to_str (ptid_t) override; |
| 166 | |
| 167 | bool has_all_memory () override; |
| 168 | bool has_memory () override; |
| 169 | std::vector<mem_region> memory_map () override; |
| 170 | |
| 171 | private: |
| 172 | sim_inferior_data *get_inferior_data_by_ptid (ptid_t ptid, |
| 173 | int sim_instance_needed); |
| 174 | void resume_one_inferior (inferior *inf, bool step, gdb_signal siggnal); |
| 175 | void close_one_inferior (inferior *inf); |
| 176 | }; |
| 177 | |
| 178 | static struct gdbsim_target gdbsim_ops; |
| 179 | |
| 180 | static inferior_key<sim_inferior_data> sim_inferior_data_key; |
| 181 | |
| 182 | /* Flag indicating the "open" status of this module. It's set to 1 |
| 183 | in gdbsim_open() and 0 in gdbsim_close(). */ |
| 184 | static int gdbsim_is_open = 0; |
| 185 | |
| 186 | /* Argument list to pass to sim_open(). It is allocated in gdbsim_open() |
| 187 | and deallocated in gdbsim_close(). The lifetime needs to extend beyond |
| 188 | the call to gdbsim_open() due to the fact that other sim instances other |
| 189 | than the first will be allocated after the gdbsim_open() call. */ |
| 190 | static char **sim_argv = NULL; |
| 191 | |
| 192 | /* OS-level callback functions for write, flush, etc. */ |
| 193 | static host_callback gdb_callback; |
| 194 | static int callbacks_initialized = 0; |
| 195 | |
| 196 | /* Flags indicating whether or not a sim instance is needed. One of these |
| 197 | flags should be passed to get_sim_inferior_data(). */ |
| 198 | |
| 199 | enum {SIM_INSTANCE_NOT_NEEDED = 0, SIM_INSTANCE_NEEDED = 1}; |
| 200 | |
| 201 | /* Obtain pointer to per-inferior simulator data, allocating it if necessary. |
| 202 | Attempt to open the sim if SIM_INSTANCE_NEEDED is true. */ |
| 203 | |
| 204 | static struct sim_inferior_data * |
| 205 | get_sim_inferior_data (struct inferior *inf, int sim_instance_needed) |
| 206 | { |
| 207 | SIM_DESC sim_desc = NULL; |
| 208 | struct sim_inferior_data *sim_data = sim_inferior_data_key.get (inf); |
| 209 | |
| 210 | /* Try to allocate a new sim instance, if needed. We do this ahead of |
| 211 | a potential allocation of a sim_inferior_data struct in order to |
| 212 | avoid needlessly allocating that struct in the event that the sim |
| 213 | instance allocation fails. */ |
| 214 | if (sim_instance_needed == SIM_INSTANCE_NEEDED |
| 215 | && (sim_data == NULL || sim_data->gdbsim_desc == NULL)) |
| 216 | { |
| 217 | sim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, |
| 218 | current_program_space->exec_bfd (), sim_argv); |
| 219 | if (sim_desc == NULL) |
| 220 | error (_("Unable to create simulator instance for inferior %d."), |
| 221 | inf->num); |
| 222 | |
| 223 | /* Check if the sim descriptor is the same as that of another |
| 224 | inferior. */ |
| 225 | for (inferior *other_inf : all_inferiors ()) |
| 226 | { |
| 227 | sim_inferior_data *other_sim_data |
| 228 | = sim_inferior_data_key.get (other_inf); |
| 229 | |
| 230 | if (other_sim_data != NULL |
| 231 | && other_sim_data->gdbsim_desc == sim_desc) |
| 232 | { |
| 233 | /* We don't close the descriptor due to the fact that it's |
| 234 | shared with some other inferior. If we were to close it, |
| 235 | that might needlessly muck up the other inferior. Of |
| 236 | course, it's possible that the damage has already been |
| 237 | done... Note that it *will* ultimately be closed during |
| 238 | cleanup of the other inferior. */ |
| 239 | sim_desc = NULL; |
| 240 | error ( |
| 241 | _("Inferior %d and inferior %d would have identical simulator state.\n" |
| 242 | "(This simulator does not support the running of more than one inferior.)"), |
| 243 | inf->num, other_inf->num); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (sim_data == NULL) |
| 249 | { |
| 250 | sim_data = sim_inferior_data_key.emplace (inf, sim_desc); |
| 251 | } |
| 252 | else if (sim_desc) |
| 253 | { |
| 254 | /* This handles the case where sim_data was allocated prior to |
| 255 | needing a sim instance. */ |
| 256 | sim_data->gdbsim_desc = sim_desc; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | return sim_data; |
| 261 | } |
| 262 | |
| 263 | /* Return pointer to per-inferior simulator data using PTID to find the |
| 264 | inferior in question. Return NULL when no inferior is found or |
| 265 | when ptid has a zero or negative pid component. */ |
| 266 | |
| 267 | sim_inferior_data * |
| 268 | gdbsim_target::get_inferior_data_by_ptid (ptid_t ptid, |
| 269 | int sim_instance_needed) |
| 270 | { |
| 271 | struct inferior *inf; |
| 272 | int pid = ptid.pid (); |
| 273 | |
| 274 | if (pid <= 0) |
| 275 | return NULL; |
| 276 | |
| 277 | inf = find_inferior_pid (this, pid); |
| 278 | |
| 279 | if (inf) |
| 280 | return get_sim_inferior_data (inf, sim_instance_needed); |
| 281 | else |
| 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | /* Free the per-inferior simulator data. */ |
| 286 | |
| 287 | sim_inferior_data::~sim_inferior_data () |
| 288 | { |
| 289 | if (gdbsim_desc) |
| 290 | sim_close (gdbsim_desc, 0); |
| 291 | } |
| 292 | |
| 293 | static void |
| 294 | dump_mem (const gdb_byte *buf, int len) |
| 295 | { |
| 296 | fputs_unfiltered ("\t", gdb_stdlog); |
| 297 | |
| 298 | if (len == 8 || len == 4) |
| 299 | { |
| 300 | uint32_t l[2]; |
| 301 | |
| 302 | memcpy (l, buf, len); |
| 303 | fprintf_unfiltered (gdb_stdlog, "0x%08x", l[0]); |
| 304 | if (len == 8) |
| 305 | fprintf_unfiltered (gdb_stdlog, " 0x%08x", l[1]); |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | int i; |
| 310 | |
| 311 | for (i = 0; i < len; i++) |
| 312 | fprintf_unfiltered (gdb_stdlog, "0x%02x ", buf[i]); |
| 313 | } |
| 314 | |
| 315 | fputs_unfiltered ("\n", gdb_stdlog); |
| 316 | } |
| 317 | |
| 318 | /* Initialize gdb_callback. */ |
| 319 | |
| 320 | static void |
| 321 | init_callbacks (void) |
| 322 | { |
| 323 | if (!callbacks_initialized) |
| 324 | { |
| 325 | gdb_callback = default_callback; |
| 326 | gdb_callback.init (&gdb_callback); |
| 327 | gdb_callback.write_stdout = gdb_os_write_stdout; |
| 328 | gdb_callback.flush_stdout = gdb_os_flush_stdout; |
| 329 | gdb_callback.write_stderr = gdb_os_write_stderr; |
| 330 | gdb_callback.flush_stderr = gdb_os_flush_stderr; |
| 331 | gdb_callback.printf_filtered = gdb_os_printf_filtered; |
| 332 | gdb_callback.vprintf_filtered = gdb_os_vprintf_filtered; |
| 333 | gdb_callback.evprintf_filtered = gdb_os_evprintf_filtered; |
| 334 | gdb_callback.error = gdb_os_error; |
| 335 | gdb_callback.poll_quit = gdb_os_poll_quit; |
| 336 | gdb_callback.magic = HOST_CALLBACK_MAGIC; |
| 337 | callbacks_initialized = 1; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /* Release callbacks (free resources used by them). */ |
| 342 | |
| 343 | static void |
| 344 | end_callbacks (void) |
| 345 | { |
| 346 | if (callbacks_initialized) |
| 347 | { |
| 348 | gdb_callback.shutdown (&gdb_callback); |
| 349 | callbacks_initialized = 0; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /* GDB version of os_write_stdout callback. */ |
| 354 | |
| 355 | static int |
| 356 | gdb_os_write_stdout (host_callback *p, const char *buf, int len) |
| 357 | { |
| 358 | gdb_stdtarg->write (buf, len); |
| 359 | return len; |
| 360 | } |
| 361 | |
| 362 | /* GDB version of os_flush_stdout callback. */ |
| 363 | |
| 364 | static void |
| 365 | gdb_os_flush_stdout (host_callback *p) |
| 366 | { |
| 367 | gdb_stdtarg->flush (); |
| 368 | } |
| 369 | |
| 370 | /* GDB version of os_write_stderr callback. */ |
| 371 | |
| 372 | static int |
| 373 | gdb_os_write_stderr (host_callback *p, const char *buf, int len) |
| 374 | { |
| 375 | int i; |
| 376 | char b[2]; |
| 377 | |
| 378 | for (i = 0; i < len; i++) |
| 379 | { |
| 380 | b[0] = buf[i]; |
| 381 | b[1] = 0; |
| 382 | gdb_stdtargerr->puts (b); |
| 383 | } |
| 384 | return len; |
| 385 | } |
| 386 | |
| 387 | /* GDB version of os_flush_stderr callback. */ |
| 388 | |
| 389 | static void |
| 390 | gdb_os_flush_stderr (host_callback *p) |
| 391 | { |
| 392 | gdb_stdtargerr->flush (); |
| 393 | } |
| 394 | |
| 395 | /* GDB version of printf_filtered callback. */ |
| 396 | |
| 397 | static void ATTRIBUTE_PRINTF (2, 3) |
| 398 | gdb_os_printf_filtered (host_callback * p, const char *format, ...) |
| 399 | { |
| 400 | va_list args; |
| 401 | |
| 402 | va_start (args, format); |
| 403 | vfprintf_filtered (gdb_stdout, format, args); |
| 404 | va_end (args); |
| 405 | } |
| 406 | |
| 407 | /* GDB version of error vprintf_filtered. */ |
| 408 | |
| 409 | static void ATTRIBUTE_PRINTF (2, 0) |
| 410 | gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap) |
| 411 | { |
| 412 | vfprintf_filtered (gdb_stdout, format, ap); |
| 413 | } |
| 414 | |
| 415 | /* GDB version of error evprintf_filtered. */ |
| 416 | |
| 417 | static void ATTRIBUTE_PRINTF (2, 0) |
| 418 | gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap) |
| 419 | { |
| 420 | vfprintf_filtered (gdb_stderr, format, ap); |
| 421 | } |
| 422 | |
| 423 | /* GDB version of error callback. */ |
| 424 | |
| 425 | static void ATTRIBUTE_PRINTF (2, 3) |
| 426 | gdb_os_error (host_callback * p, const char *format, ...) |
| 427 | { |
| 428 | va_list args; |
| 429 | |
| 430 | va_start (args, format); |
| 431 | verror (format, args); |
| 432 | va_end (args); |
| 433 | } |
| 434 | |
| 435 | int |
| 436 | one2one_register_sim_regno (struct gdbarch *gdbarch, int regnum) |
| 437 | { |
| 438 | /* Only makes sense to supply raw registers. */ |
| 439 | gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)); |
| 440 | return regnum; |
| 441 | } |
| 442 | |
| 443 | void |
| 444 | gdbsim_target::fetch_registers (struct regcache *regcache, int regno) |
| 445 | { |
| 446 | struct gdbarch *gdbarch = regcache->arch (); |
| 447 | struct inferior *inf = find_inferior_ptid (this, regcache->ptid ()); |
| 448 | struct sim_inferior_data *sim_data |
| 449 | = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED); |
| 450 | |
| 451 | if (regno == -1) |
| 452 | { |
| 453 | for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++) |
| 454 | fetch_registers (regcache, regno); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | switch (gdbarch_register_sim_regno (gdbarch, regno)) |
| 459 | { |
| 460 | case LEGACY_SIM_REGNO_IGNORE: |
| 461 | break; |
| 462 | case SIM_REGNO_DOES_NOT_EXIST: |
| 463 | { |
| 464 | /* For moment treat a `does not exist' register the same way |
| 465 | as an ``unavailable'' register. */ |
| 466 | regcache->raw_supply_zeroed (regno); |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | default: |
| 471 | { |
| 472 | static int warn_user = 1; |
| 473 | int regsize = register_size (gdbarch, regno); |
| 474 | gdb::byte_vector buf (regsize, 0); |
| 475 | int nr_bytes; |
| 476 | |
| 477 | gdb_assert (regno >= 0 && regno < gdbarch_num_regs (gdbarch)); |
| 478 | nr_bytes = sim_fetch_register (sim_data->gdbsim_desc, |
| 479 | gdbarch_register_sim_regno |
| 480 | (gdbarch, regno), |
| 481 | buf.data (), regsize); |
| 482 | if (nr_bytes > 0 && nr_bytes != regsize && warn_user) |
| 483 | { |
| 484 | fprintf_unfiltered (gdb_stderr, |
| 485 | "Size of register %s (%d/%d) " |
| 486 | "incorrect (%d instead of %d))", |
| 487 | gdbarch_register_name (gdbarch, regno), |
| 488 | regno, |
| 489 | gdbarch_register_sim_regno (gdbarch, regno), |
| 490 | nr_bytes, regsize); |
| 491 | warn_user = 0; |
| 492 | } |
| 493 | /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0' |
| 494 | indicating that GDB and the SIM have different ideas about |
| 495 | which registers are fetchable. */ |
| 496 | /* Else if (nr_bytes < 0): an old simulator, that doesn't |
| 497 | think to return the register size. Just assume all is ok. */ |
| 498 | regcache->raw_supply (regno, buf.data ()); |
| 499 | if (remote_debug) |
| 500 | { |
| 501 | fprintf_unfiltered (gdb_stdlog, |
| 502 | "gdbsim_fetch_register: %d", regno); |
| 503 | /* FIXME: We could print something more intelligible. */ |
| 504 | dump_mem (buf.data (), regsize); |
| 505 | } |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | |
| 512 | void |
| 513 | gdbsim_target::store_registers (struct regcache *regcache, int regno) |
| 514 | { |
| 515 | struct gdbarch *gdbarch = regcache->arch (); |
| 516 | struct inferior *inf = find_inferior_ptid (this, regcache->ptid ()); |
| 517 | struct sim_inferior_data *sim_data |
| 518 | = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED); |
| 519 | |
| 520 | if (regno == -1) |
| 521 | { |
| 522 | for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++) |
| 523 | store_registers (regcache, regno); |
| 524 | return; |
| 525 | } |
| 526 | else if (gdbarch_register_sim_regno (gdbarch, regno) >= 0) |
| 527 | { |
| 528 | int regsize = register_size (gdbarch, regno); |
| 529 | gdb::byte_vector tmp (regsize); |
| 530 | int nr_bytes; |
| 531 | |
| 532 | regcache->cooked_read (regno, tmp.data ()); |
| 533 | nr_bytes = sim_store_register (sim_data->gdbsim_desc, |
| 534 | gdbarch_register_sim_regno |
| 535 | (gdbarch, regno), |
| 536 | tmp.data (), regsize); |
| 537 | |
| 538 | if (nr_bytes > 0 && nr_bytes != regsize) |
| 539 | internal_error (__FILE__, __LINE__, |
| 540 | _("Register size different to expected")); |
| 541 | if (nr_bytes < 0) |
| 542 | internal_error (__FILE__, __LINE__, |
| 543 | _("Register %d not updated"), regno); |
| 544 | if (nr_bytes == 0) |
| 545 | warning (_("Register %s not updated"), |
| 546 | gdbarch_register_name (gdbarch, regno)); |
| 547 | |
| 548 | if (remote_debug) |
| 549 | { |
| 550 | fprintf_unfiltered (gdb_stdlog, "gdbsim_store_register: %d", regno); |
| 551 | /* FIXME: We could print something more intelligible. */ |
| 552 | dump_mem (tmp.data (), regsize); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* Kill the running program. This may involve closing any open files |
| 558 | and releasing other resources acquired by the simulated program. */ |
| 559 | |
| 560 | void |
| 561 | gdbsim_target::kill () |
| 562 | { |
| 563 | if (remote_debug) |
| 564 | fprintf_unfiltered (gdb_stdlog, "gdbsim_kill\n"); |
| 565 | |
| 566 | /* There is no need to `kill' running simulator - the simulator is |
| 567 | not running. Mourning it is enough. */ |
| 568 | target_mourn_inferior (inferior_ptid); |
| 569 | } |
| 570 | |
| 571 | /* Load an executable file into the target process. This is expected to |
| 572 | not only bring new code into the target process, but also to update |
| 573 | GDB's symbol tables to match. */ |
| 574 | |
| 575 | void |
| 576 | gdbsim_target::load (const char *args, int fromtty) |
| 577 | { |
| 578 | const char *prog; |
| 579 | struct sim_inferior_data *sim_data |
| 580 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED); |
| 581 | |
| 582 | if (args == NULL) |
| 583 | error_no_arg (_("program to load")); |
| 584 | |
| 585 | gdb_argv argv (args); |
| 586 | |
| 587 | prog = tilde_expand (argv[0]); |
| 588 | |
| 589 | if (argv[1] != NULL) |
| 590 | error (_("GDB sim does not yet support a load offset.")); |
| 591 | |
| 592 | if (remote_debug) |
| 593 | fprintf_unfiltered (gdb_stdlog, "gdbsim_load: prog \"%s\"\n", prog); |
| 594 | |
| 595 | /* FIXME: We will print two messages on error. |
| 596 | Need error to either not print anything if passed NULL or need |
| 597 | another routine that doesn't take any arguments. */ |
| 598 | if (sim_load (sim_data->gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL) |
| 599 | error (_("unable to load program")); |
| 600 | |
| 601 | /* FIXME: If a load command should reset the targets registers then |
| 602 | a call to sim_create_inferior() should go here. */ |
| 603 | |
| 604 | sim_data->program_loaded = 1; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | /* Start an inferior process and set inferior_ptid to its pid. |
| 609 | EXEC_FILE is the file to run. |
| 610 | ARGS is a string containing the arguments to the program. |
| 611 | ENV is the environment vector to pass. Errors reported with error(). |
| 612 | On VxWorks and various standalone systems, we ignore exec_file. */ |
| 613 | /* This is called not only when we first attach, but also when the |
| 614 | user types "run" after having attached. */ |
| 615 | |
| 616 | void |
| 617 | gdbsim_target::create_inferior (const char *exec_file, |
| 618 | const std::string &allargs, |
| 619 | char **env, int from_tty) |
| 620 | { |
| 621 | struct sim_inferior_data *sim_data |
| 622 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED); |
| 623 | int len; |
| 624 | char *arg_buf; |
| 625 | const char *args = allargs.c_str (); |
| 626 | |
| 627 | if (exec_file == 0 || current_program_space->exec_bfd () == 0) |
| 628 | warning (_("No executable file specified.")); |
| 629 | if (!sim_data->program_loaded) |
| 630 | warning (_("No program loaded.")); |
| 631 | |
| 632 | if (remote_debug) |
| 633 | fprintf_unfiltered (gdb_stdlog, |
| 634 | "gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n", |
| 635 | (exec_file ? exec_file : "(NULL)"), |
| 636 | args); |
| 637 | |
| 638 | if (inferior_ptid == sim_data->remote_sim_ptid) |
| 639 | kill (); |
| 640 | remove_breakpoints (); |
| 641 | init_wait_for_inferior (); |
| 642 | |
| 643 | gdb_argv built_argv; |
| 644 | if (exec_file != NULL) |
| 645 | { |
| 646 | len = strlen (exec_file) + 1 + allargs.size () + 1 + /*slop */ 10; |
| 647 | arg_buf = (char *) alloca (len); |
| 648 | arg_buf[0] = '\0'; |
| 649 | strcat (arg_buf, exec_file); |
| 650 | strcat (arg_buf, " "); |
| 651 | strcat (arg_buf, args); |
| 652 | built_argv.reset (arg_buf); |
| 653 | } |
| 654 | |
| 655 | if (sim_create_inferior (sim_data->gdbsim_desc, |
| 656 | current_program_space->exec_bfd (), |
| 657 | built_argv.get (), env) |
| 658 | != SIM_RC_OK) |
| 659 | error (_("Unable to create sim inferior.")); |
| 660 | |
| 661 | inferior_appeared (current_inferior (), |
| 662 | sim_data->remote_sim_ptid.pid ()); |
| 663 | thread_info *thr = add_thread_silent (this, sim_data->remote_sim_ptid); |
| 664 | switch_to_thread (thr); |
| 665 | |
| 666 | insert_breakpoints (); /* Needed to get correct instruction |
| 667 | in cache. */ |
| 668 | |
| 669 | clear_proceed_status (0); |
| 670 | } |
| 671 | |
| 672 | /* The open routine takes the rest of the parameters from the command, |
| 673 | and (if successful) pushes a new target onto the stack. |
| 674 | Targets should supply this routine, if only to provide an error message. */ |
| 675 | /* Called when selecting the simulator. E.g. (gdb) target sim name. */ |
| 676 | |
| 677 | static void |
| 678 | gdbsim_target_open (const char *args, int from_tty) |
| 679 | { |
| 680 | int len; |
| 681 | char *arg_buf; |
| 682 | struct sim_inferior_data *sim_data; |
| 683 | const char *sysroot; |
| 684 | SIM_DESC gdbsim_desc; |
| 685 | |
| 686 | sysroot = gdb_sysroot; |
| 687 | if (is_target_filename (sysroot)) |
| 688 | sysroot += strlen (TARGET_SYSROOT_PREFIX); |
| 689 | |
| 690 | if (remote_debug) |
| 691 | fprintf_unfiltered (gdb_stdlog, |
| 692 | "gdbsim_open: args \"%s\"\n", args ? args : "(null)"); |
| 693 | |
| 694 | /* Ensure that the sim target is not on the target stack. This is |
| 695 | necessary, because if it is on the target stack, the call to |
| 696 | push_target below will invoke sim_close(), thus freeing various |
| 697 | state (including a sim instance) that we allocate prior to |
| 698 | invoking push_target(). We want to delay the push_target() |
| 699 | operation until after we complete those operations which could |
| 700 | error out. */ |
| 701 | if (gdbsim_is_open) |
| 702 | current_inferior ()->unpush_target (&gdbsim_ops); |
| 703 | |
| 704 | len = (7 + 1 /* gdbsim */ |
| 705 | + strlen (" -E little") |
| 706 | + strlen (" --architecture=xxxxxxxxxx") |
| 707 | + strlen (" --sysroot=") + strlen (sysroot) + |
| 708 | + (args ? strlen (args) : 0) |
| 709 | + 50) /* slack */ ; |
| 710 | arg_buf = (char *) alloca (len); |
| 711 | strcpy (arg_buf, "gdbsim"); /* 7 */ |
| 712 | /* Specify the byte order for the target when it is explicitly |
| 713 | specified by the user (not auto detected). */ |
| 714 | switch (selected_byte_order ()) |
| 715 | { |
| 716 | case BFD_ENDIAN_BIG: |
| 717 | strcat (arg_buf, " -E big"); |
| 718 | break; |
| 719 | case BFD_ENDIAN_LITTLE: |
| 720 | strcat (arg_buf, " -E little"); |
| 721 | break; |
| 722 | case BFD_ENDIAN_UNKNOWN: |
| 723 | break; |
| 724 | } |
| 725 | /* Specify the architecture of the target when it has been |
| 726 | explicitly specified */ |
| 727 | if (selected_architecture_name () != NULL) |
| 728 | { |
| 729 | strcat (arg_buf, " --architecture="); |
| 730 | strcat (arg_buf, selected_architecture_name ()); |
| 731 | } |
| 732 | /* Pass along gdb's concept of the sysroot. */ |
| 733 | strcat (arg_buf, " --sysroot="); |
| 734 | strcat (arg_buf, sysroot); |
| 735 | /* finally, any explicit args */ |
| 736 | if (args) |
| 737 | { |
| 738 | strcat (arg_buf, " "); /* 1 */ |
| 739 | strcat (arg_buf, args); |
| 740 | } |
| 741 | |
| 742 | gdb_argv argv (arg_buf); |
| 743 | sim_argv = argv.release (); |
| 744 | |
| 745 | init_callbacks (); |
| 746 | gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, |
| 747 | current_program_space->exec_bfd (), sim_argv); |
| 748 | |
| 749 | if (gdbsim_desc == 0) |
| 750 | { |
| 751 | freeargv (sim_argv); |
| 752 | sim_argv = NULL; |
| 753 | error (_("unable to create simulator instance")); |
| 754 | } |
| 755 | |
| 756 | /* Reset the pid numberings for this batch of sim instances. */ |
| 757 | next_pid = INITIAL_PID; |
| 758 | |
| 759 | /* Allocate the inferior data, but do not allocate a sim instance |
| 760 | since we've already just done that. */ |
| 761 | sim_data = get_sim_inferior_data (current_inferior (), |
| 762 | SIM_INSTANCE_NOT_NEEDED); |
| 763 | |
| 764 | sim_data->gdbsim_desc = gdbsim_desc; |
| 765 | |
| 766 | current_inferior ()->push_target (&gdbsim_ops); |
| 767 | printf_filtered ("Connected to the simulator.\n"); |
| 768 | |
| 769 | /* There's nothing running after "target sim" or "load"; not until |
| 770 | "run". */ |
| 771 | switch_to_no_thread (); |
| 772 | |
| 773 | gdbsim_is_open = 1; |
| 774 | } |
| 775 | |
| 776 | /* Helper for gdbsim_target::close. */ |
| 777 | |
| 778 | void |
| 779 | gdbsim_target::close_one_inferior (inferior *inf) |
| 780 | { |
| 781 | struct sim_inferior_data *sim_data = sim_inferior_data_key.get (inf); |
| 782 | if (sim_data != NULL) |
| 783 | { |
| 784 | ptid_t ptid = sim_data->remote_sim_ptid; |
| 785 | |
| 786 | sim_inferior_data_key.clear (inf); |
| 787 | |
| 788 | /* Having a ptid allocated and stored in remote_sim_ptid does |
| 789 | not mean that a corresponding inferior was ever created. |
| 790 | Thus we need to verify the existence of an inferior using the |
| 791 | pid in question before setting inferior_ptid via |
| 792 | switch_to_thread() or mourning the inferior. */ |
| 793 | if (find_inferior_ptid (this, ptid) != NULL) |
| 794 | { |
| 795 | switch_to_thread (this, ptid); |
| 796 | generic_mourn_inferior (); |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /* Close out all files and local state before this target loses control. */ |
| 802 | |
| 803 | void |
| 804 | gdbsim_target::close () |
| 805 | { |
| 806 | if (remote_debug) |
| 807 | fprintf_unfiltered (gdb_stdlog, "gdbsim_close\n"); |
| 808 | |
| 809 | for (inferior *inf : all_inferiors (this)) |
| 810 | close_one_inferior (inf); |
| 811 | |
| 812 | if (sim_argv != NULL) |
| 813 | { |
| 814 | freeargv (sim_argv); |
| 815 | sim_argv = NULL; |
| 816 | } |
| 817 | |
| 818 | end_callbacks (); |
| 819 | |
| 820 | gdbsim_is_open = 0; |
| 821 | } |
| 822 | |
| 823 | /* Takes a program previously attached to and detaches it. |
| 824 | The program may resume execution (some targets do, some don't) and will |
| 825 | no longer stop on signals, etc. We better not have left any breakpoints |
| 826 | in the program or it'll die when it hits one. FROM_TTY says whether to be |
| 827 | verbose or not. */ |
| 828 | /* Terminate the open connection to the remote debugger. |
| 829 | Use this when you want to detach and do something else with your gdb. */ |
| 830 | |
| 831 | void |
| 832 | gdbsim_target::detach (inferior *inf, int from_tty) |
| 833 | { |
| 834 | if (remote_debug) |
| 835 | fprintf_unfiltered (gdb_stdlog, "gdbsim_detach\n"); |
| 836 | |
| 837 | inf->unpush_target (this); /* calls gdbsim_close to do the real work */ |
| 838 | if (from_tty) |
| 839 | printf_filtered ("Ending simulator %s debugging\n", target_shortname ()); |
| 840 | } |
| 841 | |
| 842 | /* Resume execution of the target process. STEP says whether to single-step |
| 843 | or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given |
| 844 | to the target, or zero for no signal. */ |
| 845 | |
| 846 | void |
| 847 | gdbsim_target::resume_one_inferior (inferior *inf, bool step, |
| 848 | gdb_signal siggnal) |
| 849 | { |
| 850 | struct sim_inferior_data *sim_data |
| 851 | = get_sim_inferior_data (inf, SIM_INSTANCE_NOT_NEEDED); |
| 852 | |
| 853 | if (sim_data) |
| 854 | { |
| 855 | sim_data->resume_siggnal = siggnal; |
| 856 | sim_data->resume_step = step; |
| 857 | |
| 858 | if (remote_debug) |
| 859 | fprintf_unfiltered (gdb_stdlog, |
| 860 | _("gdbsim_resume: pid %d, step %d, signal %d\n"), |
| 861 | inf->pid, step, siggnal); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | void |
| 866 | gdbsim_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal) |
| 867 | { |
| 868 | struct sim_inferior_data *sim_data |
| 869 | = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED); |
| 870 | |
| 871 | /* We don't access any sim_data members within this function. |
| 872 | What's of interest is whether or not the call to |
| 873 | get_sim_inferior_data_by_ptid(), above, is able to obtain a |
| 874 | non-NULL pointer. If it managed to obtain a non-NULL pointer, we |
| 875 | know we have a single inferior to consider. If it's NULL, we |
| 876 | either have multiple inferiors to resume or an error condition. */ |
| 877 | |
| 878 | if (sim_data) |
| 879 | resume_one_inferior (find_inferior_ptid (this, ptid), step, siggnal); |
| 880 | else if (ptid == minus_one_ptid) |
| 881 | { |
| 882 | for (inferior *inf : all_inferiors (this)) |
| 883 | resume_one_inferior (inf, step, siggnal); |
| 884 | } |
| 885 | else |
| 886 | error (_("The program is not being run.")); |
| 887 | } |
| 888 | |
| 889 | /* Notify the simulator of an asynchronous request to interrupt. |
| 890 | |
| 891 | The simulator shall ensure that the interrupt request is eventually |
| 892 | delivered to the simulator. If the call is made while the |
| 893 | simulator is not running then the interrupt request is processed when |
| 894 | the simulator is next resumed. |
| 895 | |
| 896 | For simulators that do not support this operation, just abort. */ |
| 897 | |
| 898 | void |
| 899 | gdbsim_target::interrupt () |
| 900 | { |
| 901 | for (inferior *inf : all_inferiors ()) |
| 902 | { |
| 903 | sim_inferior_data *sim_data |
| 904 | = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED); |
| 905 | |
| 906 | if (sim_data != nullptr && !sim_stop (sim_data->gdbsim_desc)) |
| 907 | quit (); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | /* GDB version of os_poll_quit callback. |
| 912 | Taken from gdb/util.c - should be in a library. */ |
| 913 | |
| 914 | static int |
| 915 | gdb_os_poll_quit (host_callback *p) |
| 916 | { |
| 917 | if (deprecated_ui_loop_hook != NULL) |
| 918 | deprecated_ui_loop_hook (0); |
| 919 | |
| 920 | if (check_quit_flag ()) /* gdb's idea of quit */ |
| 921 | return 1; |
| 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | /* Wait for inferior process to do something. Return pid of child, |
| 926 | or -1 in case of error; store status through argument pointer STATUS, |
| 927 | just as `wait' would. */ |
| 928 | |
| 929 | static void |
| 930 | gdbsim_cntrl_c (int signo) |
| 931 | { |
| 932 | gdbsim_ops.interrupt (); |
| 933 | } |
| 934 | |
| 935 | ptid_t |
| 936 | gdbsim_target::wait (ptid_t ptid, struct target_waitstatus *status, |
| 937 | target_wait_flags options) |
| 938 | { |
| 939 | struct sim_inferior_data *sim_data; |
| 940 | static sighandler_t prev_sigint; |
| 941 | int sigrc = 0; |
| 942 | enum sim_stop reason = sim_running; |
| 943 | |
| 944 | /* This target isn't able to (yet) resume more than one inferior at a time. |
| 945 | When ptid is minus_one_ptid, just use the current inferior. If we're |
| 946 | given an explicit pid, we'll try to find it and use that instead. */ |
| 947 | if (ptid == minus_one_ptid) |
| 948 | sim_data = get_sim_inferior_data (current_inferior (), |
| 949 | SIM_INSTANCE_NEEDED); |
| 950 | else |
| 951 | { |
| 952 | sim_data = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NEEDED); |
| 953 | if (sim_data == NULL) |
| 954 | error (_("Unable to wait for pid %d. Inferior not found."), |
| 955 | ptid.pid ()); |
| 956 | } |
| 957 | |
| 958 | if (remote_debug) |
| 959 | fprintf_unfiltered (gdb_stdlog, "gdbsim_wait\n"); |
| 960 | |
| 961 | #if defined (HAVE_SIGACTION) && defined (SA_RESTART) |
| 962 | { |
| 963 | struct sigaction sa, osa; |
| 964 | sa.sa_handler = gdbsim_cntrl_c; |
| 965 | sigemptyset (&sa.sa_mask); |
| 966 | sa.sa_flags = 0; |
| 967 | sigaction (SIGINT, &sa, &osa); |
| 968 | prev_sigint = osa.sa_handler; |
| 969 | } |
| 970 | #else |
| 971 | prev_sigint = signal (SIGINT, gdbsim_cntrl_c); |
| 972 | #endif |
| 973 | sim_resume (sim_data->gdbsim_desc, sim_data->resume_step, |
| 974 | sim_data->resume_siggnal); |
| 975 | |
| 976 | signal (SIGINT, prev_sigint); |
| 977 | sim_data->resume_step = 0; |
| 978 | |
| 979 | sim_stop_reason (sim_data->gdbsim_desc, &reason, &sigrc); |
| 980 | |
| 981 | switch (reason) |
| 982 | { |
| 983 | case sim_exited: |
| 984 | status->kind = TARGET_WAITKIND_EXITED; |
| 985 | status->value.integer = sigrc; |
| 986 | break; |
| 987 | case sim_stopped: |
| 988 | switch (sigrc) |
| 989 | { |
| 990 | case GDB_SIGNAL_ABRT: |
| 991 | quit (); |
| 992 | break; |
| 993 | case GDB_SIGNAL_INT: |
| 994 | case GDB_SIGNAL_TRAP: |
| 995 | default: |
| 996 | status->kind = TARGET_WAITKIND_STOPPED; |
| 997 | status->value.sig = (enum gdb_signal) sigrc; |
| 998 | break; |
| 999 | } |
| 1000 | break; |
| 1001 | case sim_signalled: |
| 1002 | status->kind = TARGET_WAITKIND_SIGNALLED; |
| 1003 | status->value.sig = (enum gdb_signal) sigrc; |
| 1004 | break; |
| 1005 | case sim_running: |
| 1006 | case sim_polling: |
| 1007 | /* FIXME: Is this correct? */ |
| 1008 | break; |
| 1009 | } |
| 1010 | |
| 1011 | return sim_data->remote_sim_ptid; |
| 1012 | } |
| 1013 | |
| 1014 | /* Get ready to modify the registers array. On machines which store |
| 1015 | individual registers, this doesn't need to do anything. On machines |
| 1016 | which store all the registers in one fell swoop, this makes sure |
| 1017 | that registers contains all the registers from the program being |
| 1018 | debugged. */ |
| 1019 | |
| 1020 | void |
| 1021 | gdbsim_target::prepare_to_store (struct regcache *regcache) |
| 1022 | { |
| 1023 | /* Do nothing, since we can store individual regs. */ |
| 1024 | } |
| 1025 | |
| 1026 | /* Helper for gdbsim_xfer_partial that handles memory transfers. |
| 1027 | Arguments are like target_xfer_partial. */ |
| 1028 | |
| 1029 | static enum target_xfer_status |
| 1030 | gdbsim_xfer_memory (struct target_ops *target, |
| 1031 | gdb_byte *readbuf, const gdb_byte *writebuf, |
| 1032 | ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len) |
| 1033 | { |
| 1034 | struct sim_inferior_data *sim_data |
| 1035 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED); |
| 1036 | int l; |
| 1037 | |
| 1038 | /* If this target doesn't have memory yet, return 0 causing the |
| 1039 | request to be passed to a lower target, hopefully an exec |
| 1040 | file. */ |
| 1041 | if (!target->has_memory ()) |
| 1042 | return TARGET_XFER_EOF; |
| 1043 | |
| 1044 | if (!sim_data->program_loaded) |
| 1045 | error (_("No program loaded.")); |
| 1046 | |
| 1047 | /* Note that we obtained the sim_data pointer above using |
| 1048 | SIM_INSTANCE_NOT_NEEDED. We do this so that we don't needlessly |
| 1049 | allocate a sim instance prior to loading a program. If we |
| 1050 | get to this point in the code though, gdbsim_desc should be |
| 1051 | non-NULL. (Note that a sim instance is needed in order to load |
| 1052 | the program...) */ |
| 1053 | gdb_assert (sim_data->gdbsim_desc != NULL); |
| 1054 | |
| 1055 | if (remote_debug) |
| 1056 | fprintf_unfiltered (gdb_stdlog, |
| 1057 | "gdbsim_xfer_memory: readbuf %s, writebuf %s, " |
| 1058 | "memaddr %s, len %s\n", |
| 1059 | host_address_to_string (readbuf), |
| 1060 | host_address_to_string (writebuf), |
| 1061 | paddress (target_gdbarch (), memaddr), |
| 1062 | pulongest (len)); |
| 1063 | |
| 1064 | if (writebuf) |
| 1065 | { |
| 1066 | if (remote_debug && len > 0) |
| 1067 | dump_mem (writebuf, len); |
| 1068 | l = sim_write (sim_data->gdbsim_desc, memaddr, writebuf, len); |
| 1069 | } |
| 1070 | else |
| 1071 | { |
| 1072 | l = sim_read (sim_data->gdbsim_desc, memaddr, readbuf, len); |
| 1073 | if (remote_debug && len > 0) |
| 1074 | dump_mem (readbuf, len); |
| 1075 | } |
| 1076 | if (l > 0) |
| 1077 | { |
| 1078 | *xfered_len = (ULONGEST) l; |
| 1079 | return TARGET_XFER_OK; |
| 1080 | } |
| 1081 | else if (l == 0) |
| 1082 | return TARGET_XFER_EOF; |
| 1083 | else |
| 1084 | return TARGET_XFER_E_IO; |
| 1085 | } |
| 1086 | |
| 1087 | /* Target to_xfer_partial implementation. */ |
| 1088 | |
| 1089 | enum target_xfer_status |
| 1090 | gdbsim_target::xfer_partial (enum target_object object, |
| 1091 | const char *annex, gdb_byte *readbuf, |
| 1092 | const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, |
| 1093 | ULONGEST *xfered_len) |
| 1094 | { |
| 1095 | switch (object) |
| 1096 | { |
| 1097 | case TARGET_OBJECT_MEMORY: |
| 1098 | return gdbsim_xfer_memory (this, readbuf, writebuf, offset, len, |
| 1099 | xfered_len); |
| 1100 | |
| 1101 | default: |
| 1102 | return TARGET_XFER_E_IO; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | void |
| 1107 | gdbsim_target::files_info () |
| 1108 | { |
| 1109 | struct sim_inferior_data *sim_data |
| 1110 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED); |
| 1111 | const char *file = "nothing"; |
| 1112 | |
| 1113 | if (current_program_space->exec_bfd ()) |
| 1114 | file = bfd_get_filename (current_program_space->exec_bfd ()); |
| 1115 | |
| 1116 | if (remote_debug) |
| 1117 | fprintf_unfiltered (gdb_stdlog, "gdbsim_files_info: file \"%s\"\n", file); |
| 1118 | |
| 1119 | if (current_program_space->exec_bfd ()) |
| 1120 | { |
| 1121 | fprintf_unfiltered (gdb_stdlog, "\tAttached to %s running program %s\n", |
| 1122 | target_shortname (), file); |
| 1123 | sim_info (sim_data->gdbsim_desc, 0); |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | /* Clear the simulator's notion of what the break points are. */ |
| 1128 | |
| 1129 | void |
| 1130 | gdbsim_target::mourn_inferior () |
| 1131 | { |
| 1132 | if (remote_debug) |
| 1133 | fprintf_unfiltered (gdb_stdlog, "gdbsim_mourn_inferior:\n"); |
| 1134 | |
| 1135 | remove_breakpoints (); |
| 1136 | generic_mourn_inferior (); |
| 1137 | } |
| 1138 | |
| 1139 | /* Pass the command argument through to the simulator verbatim. The |
| 1140 | simulator must do any command interpretation work. */ |
| 1141 | |
| 1142 | static void |
| 1143 | simulator_command (const char *args, int from_tty) |
| 1144 | { |
| 1145 | struct sim_inferior_data *sim_data; |
| 1146 | |
| 1147 | /* We use inferior_data() instead of get_sim_inferior_data() here in |
| 1148 | order to avoid attaching a sim_inferior_data struct to an |
| 1149 | inferior unnecessarily. The reason we take such care here is due |
| 1150 | to the fact that this function, simulator_command(), may be called |
| 1151 | even when the sim target is not active. If we were to use |
| 1152 | get_sim_inferior_data() here, it is possible that this call would |
| 1153 | be made either prior to gdbsim_open() or after gdbsim_close(), |
| 1154 | thus allocating memory that would not be garbage collected until |
| 1155 | the ultimate destruction of the associated inferior. */ |
| 1156 | |
| 1157 | sim_data = sim_inferior_data_key.get (current_inferior ()); |
| 1158 | if (sim_data == NULL || sim_data->gdbsim_desc == NULL) |
| 1159 | { |
| 1160 | |
| 1161 | /* PREVIOUSLY: The user may give a command before the simulator |
| 1162 | is opened. [...] (??? assuming of course one wishes to |
| 1163 | continue to allow commands to be sent to unopened simulators, |
| 1164 | which isn't entirely unreasonable). */ |
| 1165 | |
| 1166 | /* The simulator is a builtin abstraction of a remote target. |
| 1167 | Consistent with that model, access to the simulator, via sim |
| 1168 | commands, is restricted to the period when the channel to the |
| 1169 | simulator is open. */ |
| 1170 | |
| 1171 | error (_("Not connected to the simulator target")); |
| 1172 | } |
| 1173 | |
| 1174 | sim_do_command (sim_data->gdbsim_desc, args); |
| 1175 | |
| 1176 | /* Invalidate the register cache, in case the simulator command does |
| 1177 | something funny. */ |
| 1178 | registers_changed (); |
| 1179 | } |
| 1180 | |
| 1181 | static void |
| 1182 | sim_command_completer (struct cmd_list_element *ignore, |
| 1183 | completion_tracker &tracker, |
| 1184 | const char *text, const char *word) |
| 1185 | { |
| 1186 | struct sim_inferior_data *sim_data; |
| 1187 | |
| 1188 | sim_data = sim_inferior_data_key.get (current_inferior ()); |
| 1189 | if (sim_data == NULL || sim_data->gdbsim_desc == NULL) |
| 1190 | return; |
| 1191 | |
| 1192 | /* sim_complete_command returns a NULL-terminated malloc'ed array of |
| 1193 | malloc'ed strings. */ |
| 1194 | struct sim_completions_deleter |
| 1195 | { |
| 1196 | void operator() (char **ptr) const |
| 1197 | { |
| 1198 | for (size_t i = 0; ptr[i] != NULL; i++) |
| 1199 | xfree (ptr[i]); |
| 1200 | xfree (ptr); |
| 1201 | } |
| 1202 | }; |
| 1203 | |
| 1204 | std::unique_ptr<char *[], sim_completions_deleter> sim_completions |
| 1205 | (sim_complete_command (sim_data->gdbsim_desc, text, word)); |
| 1206 | if (sim_completions == NULL) |
| 1207 | return; |
| 1208 | |
| 1209 | /* Count the elements and add completions from tail to head because |
| 1210 | below we'll swap elements out of the array in case add_completion |
| 1211 | throws and the deleter deletes until it finds a NULL element. */ |
| 1212 | size_t count = 0; |
| 1213 | while (sim_completions[count] != NULL) |
| 1214 | count++; |
| 1215 | |
| 1216 | for (size_t i = count; i > 0; i--) |
| 1217 | { |
| 1218 | gdb::unique_xmalloc_ptr<char> match (sim_completions[i - 1]); |
| 1219 | sim_completions[i - 1] = NULL; |
| 1220 | tracker.add_completion (std::move (match)); |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | /* Check to see if a thread is still alive. */ |
| 1225 | |
| 1226 | bool |
| 1227 | gdbsim_target::thread_alive (ptid_t ptid) |
| 1228 | { |
| 1229 | struct sim_inferior_data *sim_data |
| 1230 | = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED); |
| 1231 | |
| 1232 | if (sim_data == NULL) |
| 1233 | return false; |
| 1234 | |
| 1235 | if (ptid == sim_data->remote_sim_ptid) |
| 1236 | /* The simulators' task is always alive. */ |
| 1237 | return true; |
| 1238 | |
| 1239 | return false; |
| 1240 | } |
| 1241 | |
| 1242 | /* Convert a thread ID to a string. */ |
| 1243 | |
| 1244 | std::string |
| 1245 | gdbsim_target::pid_to_str (ptid_t ptid) |
| 1246 | { |
| 1247 | return normal_pid_to_str (ptid); |
| 1248 | } |
| 1249 | |
| 1250 | /* Simulator memory may be accessed after the program has been loaded. */ |
| 1251 | |
| 1252 | bool |
| 1253 | gdbsim_target::has_all_memory () |
| 1254 | { |
| 1255 | struct sim_inferior_data *sim_data |
| 1256 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED); |
| 1257 | |
| 1258 | if (!sim_data->program_loaded) |
| 1259 | return false; |
| 1260 | |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
| 1264 | bool |
| 1265 | gdbsim_target::has_memory () |
| 1266 | { |
| 1267 | struct sim_inferior_data *sim_data |
| 1268 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED); |
| 1269 | |
| 1270 | if (!sim_data->program_loaded) |
| 1271 | return false; |
| 1272 | |
| 1273 | return true; |
| 1274 | } |
| 1275 | |
| 1276 | /* Get memory map from the simulator. */ |
| 1277 | |
| 1278 | std::vector<mem_region> |
| 1279 | gdbsim_target::memory_map () |
| 1280 | { |
| 1281 | struct sim_inferior_data *sim_data |
| 1282 | = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED); |
| 1283 | std::vector<mem_region> result; |
| 1284 | gdb::unique_xmalloc_ptr<char> text (sim_memory_map (sim_data->gdbsim_desc)); |
| 1285 | |
| 1286 | if (text != nullptr) |
| 1287 | result = parse_memory_map (text.get ()); |
| 1288 | |
| 1289 | return result; |
| 1290 | } |
| 1291 | |
| 1292 | void _initialize_remote_sim (); |
| 1293 | void |
| 1294 | _initialize_remote_sim () |
| 1295 | { |
| 1296 | struct cmd_list_element *c; |
| 1297 | |
| 1298 | add_target (gdbsim_target_info, gdbsim_target_open); |
| 1299 | |
| 1300 | c = add_com ("sim", class_obscure, simulator_command, |
| 1301 | _("Send a command to the simulator.")); |
| 1302 | set_cmd_completer (c, sim_command_completer); |
| 1303 | } |