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