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