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