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