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