]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/remote-sim.c
configure: require libzstd >= 1.4.0
[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)
f34652de 540 internal_error (_("Register size different to expected"));
dae477fe 541 if (nr_bytes < 0)
f34652de 542 internal_error (_("Register %d not updated"), regno);
dae477fe 543 if (nr_bytes == 0)
74228e77
RM
544 warning (_("Register %s not updated"),
545 gdbarch_register_name (gdbarch, regno));
dae477fe 546
ea35711c 547 if (remote_debug)
c906108c 548 {
6cb06a8c 549 gdb_printf (gdb_stdlog, "gdbsim_store_register: %d", regno);
c906108c 550 /* FIXME: We could print something more intelligible. */
a4d1e79a 551 dump_mem (tmp.data (), regsize);
c906108c
SS
552 }
553 }
554}
555
556/* Kill the running program. This may involve closing any open files
557 and releasing other resources acquired by the simulated program. */
558
f6ac5f3d
PA
559void
560gdbsim_target::kill ()
c906108c 561{
ea35711c 562 if (remote_debug)
6cb06a8c 563 gdb_printf (gdb_stdlog, "gdbsim_kill\n");
c906108c
SS
564
565 /* There is no need to `kill' running simulator - the simulator is
52bb452f 566 not running. Mourning it is enough. */
bc1e6c81 567 target_mourn_inferior (inferior_ptid);
c906108c
SS
568}
569
570/* Load an executable file into the target process. This is expected to
571 not only bring new code into the target process, but also to update
572 GDB's symbol tables to match. */
573
f6ac5f3d
PA
574void
575gdbsim_target::load (const char *args, int fromtty)
c906108c 576{
b2b255bd 577 const char *prog;
db04efe6
KB
578 struct sim_inferior_data *sim_data
579 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
1986bccd 580
d1a41061
PP
581 if (args == NULL)
582 error_no_arg (_("program to load"));
1986bccd 583
773a1edc 584 gdb_argv argv (args);
1986bccd
AS
585
586 prog = tilde_expand (argv[0]);
587
588 if (argv[1] != NULL)
589 error (_("GDB sim does not yet support a load offset."));
590
ea35711c 591 if (remote_debug)
6cb06a8c 592 gdb_printf (gdb_stdlog, "gdbsim_load: prog \"%s\"\n", prog);
c906108c 593
c906108c
SS
594 /* FIXME: We will print two messages on error.
595 Need error to either not print anything if passed NULL or need
596 another routine that doesn't take any arguments. */
db04efe6 597 if (sim_load (sim_data->gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL)
8a3fe4f8 598 error (_("unable to load program"));
c906108c
SS
599
600 /* FIXME: If a load command should reset the targets registers then
0df8b418 601 a call to sim_create_inferior() should go here. */
c906108c 602
db04efe6 603 sim_data->program_loaded = 1;
c906108c
SS
604}
605
606
39f77062 607/* Start an inferior process and set inferior_ptid to its pid.
c906108c
SS
608 EXEC_FILE is the file to run.
609 ARGS is a string containing the arguments to the program.
610 ENV is the environment vector to pass. Errors reported with error().
611 On VxWorks and various standalone systems, we ignore exec_file. */
612/* This is called not only when we first attach, but also when the
613 user types "run" after having attached. */
614
f6ac5f3d
PA
615void
616gdbsim_target::create_inferior (const char *exec_file,
617 const std::string &allargs,
618 char **env, int from_tty)
c906108c 619{
db04efe6
KB
620 struct sim_inferior_data *sim_data
621 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
c906108c 622 int len;
773a1edc 623 char *arg_buf;
7c5ded6a 624 const char *args = allargs.c_str ();
c906108c 625
7e10abd1 626 if (exec_file == 0 || current_program_space->exec_bfd () == 0)
8a3fe4f8 627 warning (_("No executable file specified."));
db04efe6 628 if (!sim_data->program_loaded)
8a3fe4f8 629 warning (_("No program loaded."));
c906108c 630
ea35711c 631 if (remote_debug)
6cb06a8c
TT
632 gdb_printf (gdb_stdlog,
633 "gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
634 (exec_file ? exec_file : "(NULL)"),
635 args);
c906108c 636
d7e15655 637 if (inferior_ptid == sim_data->remote_sim_ptid)
f6ac5f3d 638 kill ();
c906108c
SS
639 remove_breakpoints ();
640 init_wait_for_inferior ();
641
773a1edc 642 gdb_argv built_argv;
c906108c
SS
643 if (exec_file != NULL)
644 {
7c5ded6a 645 len = strlen (exec_file) + 1 + allargs.size () + 1 + /*slop */ 10;
c906108c
SS
646 arg_buf = (char *) alloca (len);
647 arg_buf[0] = '\0';
648 strcat (arg_buf, exec_file);
649 strcat (arg_buf, " ");
650 strcat (arg_buf, args);
773a1edc 651 built_argv.reset (arg_buf);
c906108c 652 }
ddd60447 653
7e10abd1
TT
654 if (sim_create_inferior (sim_data->gdbsim_desc,
655 current_program_space->exec_bfd (),
773a1edc 656 built_argv.get (), env)
ddd60447
MF
657 != SIM_RC_OK)
658 error (_("Unable to create sim inferior."));
c906108c 659
191f02e5
PA
660 inferior_appeared (current_inferior (),
661 sim_data->remote_sim_ptid.pid ());
662 thread_info *thr = add_thread_silent (this, sim_data->remote_sim_ptid);
663 switch_to_thread (thr);
9de2bdd7 664
0df8b418
MS
665 insert_breakpoints (); /* Needed to get correct instruction
666 in cache. */
c906108c 667
88056fbb 668 clear_proceed_status (0);
c906108c
SS
669}
670
671/* The open routine takes the rest of the parameters from the command,
672 and (if successful) pushes a new target onto the stack.
673 Targets should supply this routine, if only to provide an error message. */
0df8b418 674/* Called when selecting the simulator. E.g. (gdb) target sim name. */
c906108c 675
d9f719f1
PA
676static void
677gdbsim_target_open (const char *args, int from_tty)
c906108c
SS
678{
679 int len;
680 char *arg_buf;
db04efe6
KB
681 struct sim_inferior_data *sim_data;
682 SIM_DESC gdbsim_desc;
c906108c 683
abe19f11 684 const char *sysroot = gdb_sysroot.c_str ();
87d1b309
MF
685 if (is_target_filename (sysroot))
686 sysroot += strlen (TARGET_SYSROOT_PREFIX);
687
ea35711c 688 if (remote_debug)
6cb06a8c
TT
689 gdb_printf (gdb_stdlog,
690 "gdbsim_open: args \"%s\"\n", args ? args : "(null)");
c906108c 691
db04efe6
KB
692 /* Ensure that the sim target is not on the target stack. This is
693 necessary, because if it is on the target stack, the call to
694 push_target below will invoke sim_close(), thus freeing various
695 state (including a sim instance) that we allocate prior to
696 invoking push_target(). We want to delay the push_target()
697 operation until after we complete those operations which could
698 error out. */
699 if (gdbsim_is_open)
fadf6add 700 current_inferior ()->unpush_target (&gdbsim_ops);
c906108c 701
c5aa993b 702 len = (7 + 1 /* gdbsim */
c906108c
SS
703 + strlen (" -E little")
704 + strlen (" --architecture=xxxxxxxxxx")
abe19f11 705 + strlen (" --sysroot=") + strlen (sysroot) +
c906108c 706 + (args ? strlen (args) : 0)
c5aa993b 707 + 50) /* slack */ ;
c906108c 708 arg_buf = (char *) alloca (len);
c5aa993b 709 strcpy (arg_buf, "gdbsim"); /* 7 */
b6d373df 710 /* Specify the byte order for the target when it is explicitly
0df8b418 711 specified by the user (not auto detected). */
b6d373df 712 switch (selected_byte_order ())
c906108c 713 {
a8cf2722
AC
714 case BFD_ENDIAN_BIG:
715 strcat (arg_buf, " -E big");
716 break;
717 case BFD_ENDIAN_LITTLE:
718 strcat (arg_buf, " -E little");
719 break;
720 case BFD_ENDIAN_UNKNOWN:
721 break;
c906108c
SS
722 }
723 /* Specify the architecture of the target when it has been
724 explicitly specified */
a8cf2722 725 if (selected_architecture_name () != NULL)
c906108c
SS
726 {
727 strcat (arg_buf, " --architecture=");
a8cf2722 728 strcat (arg_buf, selected_architecture_name ());
c906108c 729 }
f4b8c29b
MF
730 /* Pass along gdb's concept of the sysroot. */
731 strcat (arg_buf, " --sysroot=");
abe19f11 732 strcat (arg_buf, sysroot);
c906108c
SS
733 /* finally, any explicit args */
734 if (args)
735 {
c5aa993b 736 strcat (arg_buf, " "); /* 1 */
c906108c
SS
737 strcat (arg_buf, args);
738 }
773a1edc 739
74cbb09e 740 gdb_argv argv (arg_buf);
67a3048c 741 sim_argv = argv.release ();
c906108c
SS
742
743 init_callbacks ();
7e10abd1
TT
744 gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback,
745 current_program_space->exec_bfd (), sim_argv);
c906108c
SS
746
747 if (gdbsim_desc == 0)
db04efe6 748 {
67a3048c 749 freeargv (sim_argv);
db04efe6
KB
750 sim_argv = NULL;
751 error (_("unable to create simulator instance"));
752 }
753
754 /* Reset the pid numberings for this batch of sim instances. */
755 next_pid = INITIAL_PID;
756
757 /* Allocate the inferior data, but do not allocate a sim instance
758 since we've already just done that. */
0df8b418
MS
759 sim_data = get_sim_inferior_data (current_inferior (),
760 SIM_INSTANCE_NOT_NEEDED);
db04efe6
KB
761
762 sim_data->gdbsim_desc = gdbsim_desc;
c906108c 763
02980c56 764 current_inferior ()->push_target (&gdbsim_ops);
6cb06a8c 765 gdb_printf ("Connected to the simulator.\n");
52bb452f
DJ
766
767 /* There's nothing running after "target sim" or "load"; not until
768 "run". */
191f02e5 769 switch_to_no_thread ();
db04efe6
KB
770
771 gdbsim_is_open = 1;
772}
773
e0037b4c 774/* Helper for gdbsim_target::close. */
db04efe6 775
e0037b4c
SM
776void
777gdbsim_target::close_one_inferior (inferior *inf)
db04efe6 778{
18101a35 779 struct sim_inferior_data *sim_data = sim_inferior_data_key.get (inf);
db04efe6
KB
780 if (sim_data != NULL)
781 {
782 ptid_t ptid = sim_data->remote_sim_ptid;
783
18101a35 784 sim_inferior_data_key.clear (inf);
db04efe6
KB
785
786 /* Having a ptid allocated and stored in remote_sim_ptid does
0df8b418 787 not mean that a corresponding inferior was ever created.
db04efe6
KB
788 Thus we need to verify the existence of an inferior using the
789 pid in question before setting inferior_ptid via
790 switch_to_thread() or mourning the inferior. */
e0037b4c 791 if (find_inferior_ptid (this, ptid) != NULL)
db04efe6 792 {
e0037b4c 793 switch_to_thread (this, ptid);
db04efe6
KB
794 generic_mourn_inferior ();
795 }
796 }
c906108c
SS
797}
798
0df8b418 799/* Close out all files and local state before this target loses control. */
c906108c 800
f6ac5f3d
PA
801void
802gdbsim_target::close ()
c906108c 803{
ea35711c 804 if (remote_debug)
6cb06a8c 805 gdb_printf (gdb_stdlog, "gdbsim_close\n");
c906108c 806
e0037b4c
SM
807 for (inferior *inf : all_inferiors (this))
808 close_one_inferior (inf);
c906108c 809
db04efe6 810 if (sim_argv != NULL)
c906108c 811 {
db04efe6
KB
812 freeargv (sim_argv);
813 sim_argv = NULL;
c906108c
SS
814 }
815
816 end_callbacks ();
db04efe6
KB
817
818 gdbsim_is_open = 0;
c906108c
SS
819}
820
821/* Takes a program previously attached to and detaches it.
822 The program may resume execution (some targets do, some don't) and will
823 no longer stop on signals, etc. We better not have left any breakpoints
6bd6f3b6
SM
824 in the program or it'll die when it hits one. FROM_TTY says whether to be
825 verbose or not. */
c906108c
SS
826/* Terminate the open connection to the remote debugger.
827 Use this when you want to detach and do something else with your gdb. */
828
f6ac5f3d
PA
829void
830gdbsim_target::detach (inferior *inf, int from_tty)
c906108c 831{
ea35711c 832 if (remote_debug)
6cb06a8c 833 gdb_printf (gdb_stdlog, "gdbsim_detach\n");
c906108c 834
fadf6add 835 inf->unpush_target (this); /* calls gdbsim_close to do the real work */
c906108c 836 if (from_tty)
6cb06a8c 837 gdb_printf ("Ending simulator %s debugging\n", target_shortname ());
c906108c 838}
c5aa993b 839
c906108c
SS
840/* Resume execution of the target process. STEP says whether to single-step
841 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
842 to the target, or zero for no signal. */
843
e0037b4c
SM
844void
845gdbsim_target::resume_one_inferior (inferior *inf, bool step,
846 gdb_signal siggnal)
db04efe6
KB
847{
848 struct sim_inferior_data *sim_data
849 = get_sim_inferior_data (inf, SIM_INSTANCE_NOT_NEEDED);
db04efe6
KB
850
851 if (sim_data)
852 {
e0037b4c
SM
853 sim_data->resume_siggnal = siggnal;
854 sim_data->resume_step = step;
db04efe6
KB
855
856 if (remote_debug)
6cb06a8c
TT
857 gdb_printf (gdb_stdlog,
858 _("gdbsim_resume: pid %d, step %d, signal %d\n"),
859 inf->pid, step, siggnal);
db04efe6 860 }
db04efe6 861}
c906108c 862
f6ac5f3d
PA
863void
864gdbsim_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
c906108c 865{
db04efe6 866 struct sim_inferior_data *sim_data
e0037b4c 867 = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);
db04efe6 868
0df8b418 869 /* We don't access any sim_data members within this function.
db04efe6
KB
870 What's of interest is whether or not the call to
871 get_sim_inferior_data_by_ptid(), above, is able to obtain a
872 non-NULL pointer. If it managed to obtain a non-NULL pointer, we
873 know we have a single inferior to consider. If it's NULL, we
874 either have multiple inferiors to resume or an error condition. */
875
876 if (sim_data)
e0037b4c 877 resume_one_inferior (find_inferior_ptid (this, ptid), step, siggnal);
d7e15655 878 else if (ptid == minus_one_ptid)
e0037b4c
SM
879 {
880 for (inferior *inf : all_inferiors (this))
881 resume_one_inferior (inf, step, siggnal);
882 }
db04efe6 883 else
8a3fe4f8 884 error (_("The program is not being run."));
c906108c
SS
885}
886
bfedc46a 887/* Notify the simulator of an asynchronous request to interrupt.
c5aa993b 888
bfedc46a 889 The simulator shall ensure that the interrupt request is eventually
c906108c 890 delivered to the simulator. If the call is made while the
bfedc46a 891 simulator is not running then the interrupt request is processed when
c906108c
SS
892 the simulator is next resumed.
893
db04efe6
KB
894 For simulators that do not support this operation, just abort. */
895
b3ee6dd9
SM
896void
897gdbsim_target::interrupt ()
db04efe6 898{
b3ee6dd9 899 for (inferior *inf : all_inferiors ())
db04efe6 900 {
b3ee6dd9
SM
901 sim_inferior_data *sim_data
902 = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
903
904 if (sim_data != nullptr && !sim_stop (sim_data->gdbsim_desc))
db04efe6 905 quit ();
db04efe6 906 }
c906108c
SS
907}
908
909/* GDB version of os_poll_quit callback.
8a0ce09a 910 Taken from gdb/util.c - should be in a library. */
c906108c
SS
911
912static int
fba45db2 913gdb_os_poll_quit (host_callback *p)
c906108c 914{
98bbd631
AC
915 if (deprecated_ui_loop_hook != NULL)
916 deprecated_ui_loop_hook (0);
7a292a7a 917
522002f9 918 if (check_quit_flag ()) /* gdb's idea of quit */
abf009ef 919 return 1;
c906108c
SS
920 return 0;
921}
922
923/* Wait for inferior process to do something. Return pid of child,
924 or -1 in case of error; store status through argument pointer STATUS,
0df8b418 925 just as `wait' would. */
c906108c
SS
926
927static void
fba45db2 928gdbsim_cntrl_c (int signo)
c906108c 929{
f6ac5f3d 930 gdbsim_ops.interrupt ();
c906108c
SS
931}
932
f6ac5f3d 933ptid_t
b60cea74
TT
934gdbsim_target::wait (ptid_t ptid, struct target_waitstatus *status,
935 target_wait_flags options)
c906108c 936{
db04efe6 937 struct sim_inferior_data *sim_data;
a40805d4 938 static sighandler_t prev_sigint;
c906108c
SS
939 int sigrc = 0;
940 enum sim_stop reason = sim_running;
941
db04efe6
KB
942 /* This target isn't able to (yet) resume more than one inferior at a time.
943 When ptid is minus_one_ptid, just use the current inferior. If we're
944 given an explicit pid, we'll try to find it and use that instead. */
d7e15655 945 if (ptid == minus_one_ptid)
0df8b418
MS
946 sim_data = get_sim_inferior_data (current_inferior (),
947 SIM_INSTANCE_NEEDED);
db04efe6
KB
948 else
949 {
e0037b4c 950 sim_data = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NEEDED);
db04efe6
KB
951 if (sim_data == NULL)
952 error (_("Unable to wait for pid %d. Inferior not found."),
e99b03dc 953 ptid.pid ());
db04efe6
KB
954 }
955
ea35711c 956 if (remote_debug)
6cb06a8c 957 gdb_printf (gdb_stdlog, "gdbsim_wait\n");
c906108c
SS
958
959#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
960 {
961 struct sigaction sa, osa;
962 sa.sa_handler = gdbsim_cntrl_c;
963 sigemptyset (&sa.sa_mask);
964 sa.sa_flags = 0;
965 sigaction (SIGINT, &sa, &osa);
966 prev_sigint = osa.sa_handler;
967 }
968#else
969 prev_sigint = signal (SIGINT, gdbsim_cntrl_c);
970#endif
db04efe6 971 sim_resume (sim_data->gdbsim_desc, sim_data->resume_step,
74228e77 972 sim_data->resume_siggnal);
db04efe6 973
c906108c 974 signal (SIGINT, prev_sigint);
db04efe6 975 sim_data->resume_step = 0;
c906108c 976
db04efe6 977 sim_stop_reason (sim_data->gdbsim_desc, &reason, &sigrc);
c906108c
SS
978
979 switch (reason)
980 {
981 case sim_exited:
66cc0442 982 status->set_exited (sigrc);
c906108c
SS
983 break;
984 case sim_stopped:
985 switch (sigrc)
986 {
a493e3e2 987 case GDB_SIGNAL_ABRT:
c906108c
SS
988 quit ();
989 break;
a493e3e2
PA
990 case GDB_SIGNAL_INT:
991 case GDB_SIGNAL_TRAP:
c906108c 992 default:
66cc0442 993 status->set_stopped ((gdb_signal) sigrc);
c906108c
SS
994 break;
995 }
996 break;
997 case sim_signalled:
66cc0442 998 status->set_signalled ((gdb_signal) sigrc);
c906108c
SS
999 break;
1000 case sim_running:
1001 case sim_polling:
0df8b418 1002 /* FIXME: Is this correct? */
c906108c
SS
1003 break;
1004 }
1005
cf1d9e09 1006 return sim_data->remote_sim_ptid;
c906108c
SS
1007}
1008
1009/* Get ready to modify the registers array. On machines which store
1010 individual registers, this doesn't need to do anything. On machines
1011 which store all the registers in one fell swoop, this makes sure
1012 that registers contains all the registers from the program being
1013 debugged. */
1014
f6ac5f3d
PA
1015void
1016gdbsim_target::prepare_to_store (struct regcache *regcache)
c906108c 1017{
0df8b418 1018 /* Do nothing, since we can store individual regs. */
c906108c
SS
1019}
1020
146ec4db
PA
1021/* Helper for gdbsim_xfer_partial that handles memory transfers.
1022 Arguments are like target_xfer_partial. */
d93bce06 1023
9b409511 1024static enum target_xfer_status
146ec4db
PA
1025gdbsim_xfer_memory (struct target_ops *target,
1026 gdb_byte *readbuf, const gdb_byte *writebuf,
9b409511 1027 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
c906108c 1028{
db04efe6
KB
1029 struct sim_inferior_data *sim_data
1030 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
b55e14c7 1031 int l;
db04efe6 1032
0c012db1
KB
1033 /* If this target doesn't have memory yet, return 0 causing the
1034 request to be passed to a lower target, hopefully an exec
1035 file. */
f6ac5f3d 1036 if (!target->has_memory ())
9b409511 1037 return TARGET_XFER_EOF;
52bb452f 1038
db04efe6 1039 if (!sim_data->program_loaded)
8a3fe4f8 1040 error (_("No program loaded."));
c906108c 1041
db04efe6
KB
1042 /* Note that we obtained the sim_data pointer above using
1043 SIM_INSTANCE_NOT_NEEDED. We do this so that we don't needlessly
1044 allocate a sim instance prior to loading a program. If we
1045 get to this point in the code though, gdbsim_desc should be
1046 non-NULL. (Note that a sim instance is needed in order to load
1047 the program...) */
1048 gdb_assert (sim_data->gdbsim_desc != NULL);
1049
ea35711c 1050 if (remote_debug)
6cb06a8c
TT
1051 gdb_printf (gdb_stdlog,
1052 "gdbsim_xfer_memory: readbuf %s, writebuf %s, "
1053 "memaddr %s, len %s\n",
1054 host_address_to_string (readbuf),
1055 host_address_to_string (writebuf),
1056 paddress (target_gdbarch (), memaddr),
1057 pulongest (len));
c906108c 1058
146ec4db 1059 if (writebuf)
c906108c 1060 {
fcde0081 1061 if (remote_debug && len > 0)
146ec4db 1062 dump_mem (writebuf, len);
b55e14c7 1063 l = sim_write (sim_data->gdbsim_desc, memaddr, writebuf, len);
c906108c 1064 }
c5aa993b 1065 else
c906108c 1066 {
b55e14c7 1067 l = sim_read (sim_data->gdbsim_desc, memaddr, readbuf, len);
ea35711c 1068 if (remote_debug && len > 0)
146ec4db 1069 dump_mem (readbuf, len);
c5aa993b 1070 }
9b409511
YQ
1071 if (l > 0)
1072 {
1073 *xfered_len = (ULONGEST) l;
1074 return TARGET_XFER_OK;
1075 }
1076 else if (l == 0)
1077 return TARGET_XFER_EOF;
1078 else
1079 return TARGET_XFER_E_IO;
c906108c
SS
1080}
1081
146ec4db
PA
1082/* Target to_xfer_partial implementation. */
1083
f6ac5f3d
PA
1084enum target_xfer_status
1085gdbsim_target::xfer_partial (enum target_object object,
1086 const char *annex, gdb_byte *readbuf,
1087 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
1088 ULONGEST *xfered_len)
146ec4db
PA
1089{
1090 switch (object)
1091 {
1092 case TARGET_OBJECT_MEMORY:
f6ac5f3d 1093 return gdbsim_xfer_memory (this, readbuf, writebuf, offset, len,
9b409511 1094 xfered_len);
146ec4db
PA
1095
1096 default:
2ed4b548 1097 return TARGET_XFER_E_IO;
146ec4db
PA
1098 }
1099}
1100
f6ac5f3d
PA
1101void
1102gdbsim_target::files_info ()
c906108c 1103{
db04efe6
KB
1104 struct sim_inferior_data *sim_data
1105 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
0d18d720 1106 const char *file = "nothing";
c906108c 1107
7e10abd1
TT
1108 if (current_program_space->exec_bfd ())
1109 file = bfd_get_filename (current_program_space->exec_bfd ());
c906108c 1110
ea35711c 1111 if (remote_debug)
6cb06a8c 1112 gdb_printf (gdb_stdlog, "gdbsim_files_info: file \"%s\"\n", file);
c906108c 1113
7e10abd1 1114 if (current_program_space->exec_bfd ())
c906108c 1115 {
6cb06a8c
TT
1116 gdb_printf ("\tAttached to %s running program %s\n",
1117 target_shortname (), file);
db04efe6 1118 sim_info (sim_data->gdbsim_desc, 0);
c906108c
SS
1119 }
1120}
1121
1122/* Clear the simulator's notion of what the break points are. */
1123
f6ac5f3d
PA
1124void
1125gdbsim_target::mourn_inferior ()
c5aa993b 1126{
ea35711c 1127 if (remote_debug)
6cb06a8c 1128 gdb_printf (gdb_stdlog, "gdbsim_mourn_inferior:\n");
c906108c
SS
1129
1130 remove_breakpoints ();
1131 generic_mourn_inferior ();
1132}
1133
c906108c
SS
1134/* Pass the command argument through to the simulator verbatim. The
1135 simulator must do any command interpretation work. */
1136
d04afd58 1137static void
0b39b52e 1138simulator_command (const char *args, int from_tty)
c906108c 1139{
db04efe6
KB
1140 struct sim_inferior_data *sim_data;
1141
1142 /* We use inferior_data() instead of get_sim_inferior_data() here in
1143 order to avoid attaching a sim_inferior_data struct to an
1144 inferior unnecessarily. The reason we take such care here is due
1145 to the fact that this function, simulator_command(), may be called
1146 even when the sim target is not active. If we were to use
1147 get_sim_inferior_data() here, it is possible that this call would
1148 be made either prior to gdbsim_open() or after gdbsim_close(),
1149 thus allocating memory that would not be garbage collected until
1150 the ultimate destruction of the associated inferior. */
1151
18101a35 1152 sim_data = sim_inferior_data_key.get (current_inferior ());
db04efe6 1153 if (sim_data == NULL || sim_data->gdbsim_desc == NULL)
c906108c
SS
1154 {
1155
1156 /* PREVIOUSLY: The user may give a command before the simulator
74228e77
RM
1157 is opened. [...] (??? assuming of course one wishes to
1158 continue to allow commands to be sent to unopened simulators,
1159 which isn't entirely unreasonable). */
c906108c
SS
1160
1161 /* The simulator is a builtin abstraction of a remote target.
74228e77
RM
1162 Consistent with that model, access to the simulator, via sim
1163 commands, is restricted to the period when the channel to the
1164 simulator is open. */
c906108c 1165
8a3fe4f8 1166 error (_("Not connected to the simulator target"));
c906108c
SS
1167 }
1168
db04efe6 1169 sim_do_command (sim_data->gdbsim_desc, args);
c906108c
SS
1170
1171 /* Invalidate the register cache, in case the simulator command does
0df8b418 1172 something funny. */
c5aa993b 1173 registers_changed ();
c906108c
SS
1174}
1175
386535dd
PA
1176static void
1177sim_command_completer (struct cmd_list_element *ignore,
1178 completion_tracker &tracker,
1179 const char *text, const char *word)
56a9aa1d
MF
1180{
1181 struct sim_inferior_data *sim_data;
1182
18101a35 1183 sim_data = sim_inferior_data_key.get (current_inferior ());
56a9aa1d 1184 if (sim_data == NULL || sim_data->gdbsim_desc == NULL)
386535dd 1185 return;
56a9aa1d 1186
386535dd
PA
1187 /* sim_complete_command returns a NULL-terminated malloc'ed array of
1188 malloc'ed strings. */
1189 struct sim_completions_deleter
1190 {
1191 void operator() (char **ptr) const
1192 {
1193 for (size_t i = 0; ptr[i] != NULL; i++)
1194 xfree (ptr[i]);
1195 xfree (ptr);
1196 }
1197 };
1198
1199 std::unique_ptr<char *[], sim_completions_deleter> sim_completions
1200 (sim_complete_command (sim_data->gdbsim_desc, text, word));
1201 if (sim_completions == NULL)
1202 return;
34370865 1203
386535dd
PA
1204 /* Count the elements and add completions from tail to head because
1205 below we'll swap elements out of the array in case add_completion
1206 throws and the deleter deletes until it finds a NULL element. */
1207 size_t count = 0;
1208 while (sim_completions[count] != NULL)
1209 count++;
34370865 1210
386535dd
PA
1211 for (size_t i = count; i > 0; i--)
1212 {
1213 gdb::unique_xmalloc_ptr<char> match (sim_completions[i - 1]);
1214 sim_completions[i - 1] = NULL;
1215 tracker.add_completion (std::move (match));
1216 }
56a9aa1d
MF
1217}
1218
9de2bdd7
PA
1219/* Check to see if a thread is still alive. */
1220
57810aa7 1221bool
f6ac5f3d 1222gdbsim_target::thread_alive (ptid_t ptid)
9de2bdd7 1223{
db04efe6 1224 struct sim_inferior_data *sim_data
e0037b4c 1225 = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);
db04efe6
KB
1226
1227 if (sim_data == NULL)
57810aa7 1228 return false;
db04efe6 1229
d7e15655 1230 if (ptid == sim_data->remote_sim_ptid)
9de2bdd7 1231 /* The simulators' task is always alive. */
57810aa7 1232 return true;
9de2bdd7 1233
57810aa7 1234 return false;
9de2bdd7
PA
1235}
1236
a068643d 1237/* Convert a thread ID to a string. */
9de2bdd7 1238
a068643d 1239std::string
f6ac5f3d 1240gdbsim_target::pid_to_str (ptid_t ptid)
9de2bdd7 1241{
9de2bdd7
PA
1242 return normal_pid_to_str (ptid);
1243}
1244
0c012db1
KB
1245/* Simulator memory may be accessed after the program has been loaded. */
1246
57810aa7 1247bool
f6ac5f3d 1248gdbsim_target::has_all_memory ()
0c012db1
KB
1249{
1250 struct sim_inferior_data *sim_data
1251 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
1252
1253 if (!sim_data->program_loaded)
57810aa7 1254 return false;
0c012db1 1255
57810aa7 1256 return true;
0c012db1
KB
1257}
1258
57810aa7 1259bool
f6ac5f3d 1260gdbsim_target::has_memory ()
0c012db1
KB
1261{
1262 struct sim_inferior_data *sim_data
1263 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
1264
1265 if (!sim_data->program_loaded)
57810aa7 1266 return false;
0c012db1 1267
57810aa7 1268 return true;
0c012db1
KB
1269}
1270
e904f56d
MF
1271/* Get memory map from the simulator. */
1272
1273std::vector<mem_region>
1274gdbsim_target::memory_map ()
1275{
1276 struct sim_inferior_data *sim_data
1277 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
1278 std::vector<mem_region> result;
1279 gdb::unique_xmalloc_ptr<char> text (sim_memory_map (sim_data->gdbsim_desc));
1280
1281 if (text != nullptr)
1282 result = parse_memory_map (text.get ());
1283
1284 return result;
1285}
1286
6c265988 1287void _initialize_remote_sim ();
c906108c 1288void
6c265988 1289_initialize_remote_sim ()
c906108c 1290{
56a9aa1d
MF
1291 struct cmd_list_element *c;
1292
d9f719f1 1293 add_target (gdbsim_target_info, gdbsim_target_open);
c906108c 1294
56a9aa1d
MF
1295 c = add_com ("sim", class_obscure, simulator_command,
1296 _("Send a command to the simulator."));
1297 set_cmd_completer (c, sim_command_completer);
c906108c 1298}