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