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