AArch64: View the pseudo V registers as vectors
[deliverable/binutils-gdb.git] / gdb / remote-sim.c
1 /* Generic remote debugging interface for simulators.
2
3 Copyright (C) 1993-2019 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Support.
6 Steve Chamberlain (sac@cygnus.com).
7
8 This file is part of GDB.
9
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
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
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.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 #include "defs.h"
24 #include "gdb_bfd.h"
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "value.h"
28 #include <ctype.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <setjmp.h>
32 #include "terminal.h"
33 #include "target.h"
34 #include "process-stratum-target.h"
35 #include "gdbcore.h"
36 #include "gdb/callback.h"
37 #include "gdb/remote-sim.h"
38 #include "command.h"
39 #include "regcache.h"
40 #include "sim-regno.h"
41 #include "arch-utils.h"
42 #include "readline/readline.h"
43 #include "gdbthread.h"
44 #include "common/byte-vector.h"
45
46 /* Prototypes */
47
48 static void init_callbacks (void);
49
50 static void end_callbacks (void);
51
52 static int gdb_os_write_stdout (host_callback *, const char *, int);
53
54 static void gdb_os_flush_stdout (host_callback *);
55
56 static int gdb_os_write_stderr (host_callback *, const char *, int);
57
58 static void gdb_os_flush_stderr (host_callback *);
59
60 static int gdb_os_poll_quit (host_callback *);
61
62 /* printf_filtered is depreciated. */
63 static void gdb_os_printf_filtered (host_callback *, const char *, ...);
64
65 static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list);
66
67 static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list);
68
69 static void gdb_os_error (host_callback *, const char *, ...)
70 ATTRIBUTE_NORETURN;
71
72 void simulator_command (char *args, int from_tty);
73
74 /* Naming convention:
75
76 sim_* are the interface to the simulator (see remote-sim.h).
77 gdbsim_* are stuff which is internal to gdb. */
78
79 static const target_info gdbsim_target_info = {
80 "sim",
81 N_("simulator"),
82 N_("Use the compiled-in simulator.")
83 };
84
85 struct gdbsim_target final
86 : public memory_breakpoint_target<process_stratum_target>
87 {
88 gdbsim_target () = default;
89
90 const target_info &info () const override
91 { return gdbsim_target_info; }
92
93 void close () override;
94
95 void detach (inferior *inf, int) override;
96
97 void resume (ptid_t, int, enum gdb_signal) override;
98 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
99
100 void fetch_registers (struct regcache *, int) override;
101 void store_registers (struct regcache *, int) override;
102 void prepare_to_store (struct regcache *) override;
103
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
125 bool thread_alive (ptid_t ptid) override;
126
127 std::string pid_to_str (ptid_t) override;
128
129 bool has_all_memory () override;
130 bool has_memory () override;
131 };
132
133 static struct gdbsim_target gdbsim_ops;
134
135 static const struct inferior_data *sim_inferior_data_key;
136
137 /* Simulator-specific, per-inferior state. */
138 struct 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. */
153 enum gdb_signal resume_siggnal;
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(). */
161 static 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. */
166 static 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. */
173 static char **sim_argv = NULL;
174
175 /* OS-level callback functions for write, flush, etc. */
176 static host_callback gdb_callback;
177 static 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
183 static int
184 check_for_duplicate_sim_descriptor (struct inferior *inf, void *arg)
185 {
186 struct sim_inferior_data *sim_data;
187 SIM_DESC new_sim_desc = (SIM_DESC) arg;
188
189 sim_data = ((struct sim_inferior_data *)
190 inferior_data (inf, sim_inferior_data_key));
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
198 enum {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
203 static struct sim_inferior_data *
204 get_sim_inferior_data (struct inferior *inf, int sim_instance_needed)
205 {
206 SIM_DESC sim_desc = NULL;
207 struct sim_inferior_data *sim_data
208 = (struct sim_inferior_data *) inferior_data (inf, sim_inferior_data_key);
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. */
214 if (sim_instance_needed == SIM_INSTANCE_NEEDED
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,
224 sim_desc);
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.)"),
237 inf->num, idup->num);
238 }
239 }
240
241 if (sim_data == NULL)
242 {
243 sim_data = XCNEW(struct sim_inferior_data);
244 set_inferior_data (inf, sim_inferior_data_key, sim_data);
245
246 /* Allocate a ptid for this inferior. */
247 sim_data->remote_sim_ptid = ptid_t (next_pid, 0, next_pid);
248 next_pid++;
249
250 /* Initialize the other instance variables. */
251 sim_data->program_loaded = 0;
252 sim_data->gdbsim_desc = sim_desc;
253 sim_data->resume_siggnal = GDB_SIGNAL_0;
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
259 needing a sim instance. */
260 sim_data->gdbsim_desc = sim_desc;
261 }
262
263
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
271 static struct sim_inferior_data *
272 get_sim_inferior_data_by_ptid (ptid_t ptid, int sim_instance_needed)
273 {
274 struct inferior *inf;
275 int pid = ptid.pid ();
276
277 if (pid <= 0)
278 return NULL;
279
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
290 static void
291 sim_inferior_data_cleanup (struct inferior *inf, void *data)
292 {
293 struct sim_inferior_data *sim_data = (struct sim_inferior_data *) data;
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 }
305
306 static void
307 dump_mem (const gdb_byte *buf, int len)
308 {
309 fputs_unfiltered ("\t", gdb_stdlog);
310
311 if (len == 8 || len == 4)
312 {
313 uint32_t l[2];
314
315 memcpy (l, buf, len);
316 fprintf_unfiltered (gdb_stdlog, "0x%08x", l[0]);
317 if (len == 8)
318 fprintf_unfiltered (gdb_stdlog, " 0x%08x", l[1]);
319 }
320 else
321 {
322 int i;
323
324 for (i = 0; i < len; i++)
325 fprintf_unfiltered (gdb_stdlog, "0x%02x ", buf[i]);
326 }
327
328 fputs_unfiltered ("\n", gdb_stdlog);
329 }
330
331 /* Initialize gdb_callback. */
332
333 static void
334 init_callbacks (void)
335 {
336 if (!callbacks_initialized)
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
356 static void
357 end_callbacks (void)
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
368 static int
369 gdb_os_write_stdout (host_callback *p, const char *buf, int len)
370 {
371 ui_file_write (gdb_stdtarg, buf, len);
372 return len;
373 }
374
375 /* GDB version of os_flush_stdout callback. */
376
377 static void
378 gdb_os_flush_stdout (host_callback *p)
379 {
380 gdb_flush (gdb_stdtarg);
381 }
382
383 /* GDB version of os_write_stderr callback. */
384
385 static int
386 gdb_os_write_stderr (host_callback *p, const char *buf, int len)
387 {
388 int i;
389 char b[2];
390
391 for (i = 0; i < len; i++)
392 {
393 b[0] = buf[i];
394 b[1] = 0;
395 fputs_unfiltered (b, gdb_stdtargerr);
396 }
397 return len;
398 }
399
400 /* GDB version of os_flush_stderr callback. */
401
402 static void
403 gdb_os_flush_stderr (host_callback *p)
404 {
405 gdb_flush (gdb_stdtargerr);
406 }
407
408 /* GDB version of printf_filtered callback. */
409
410 static void ATTRIBUTE_PRINTF (2, 3)
411 gdb_os_printf_filtered (host_callback * p, const char *format, ...)
412 {
413 va_list args;
414
415 va_start (args, format);
416 vfprintf_filtered (gdb_stdout, format, args);
417 va_end (args);
418 }
419
420 /* GDB version of error vprintf_filtered. */
421
422 static void ATTRIBUTE_PRINTF (2, 0)
423 gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap)
424 {
425 vfprintf_filtered (gdb_stdout, format, ap);
426 }
427
428 /* GDB version of error evprintf_filtered. */
429
430 static void ATTRIBUTE_PRINTF (2, 0)
431 gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap)
432 {
433 vfprintf_filtered (gdb_stderr, format, ap);
434 }
435
436 /* GDB version of error callback. */
437
438 static void ATTRIBUTE_PRINTF (2, 3)
439 gdb_os_error (host_callback * p, const char *format, ...)
440 {
441 va_list args;
442
443 va_start (args, format);
444 verror (format, args);
445 va_end (args);
446 }
447
448 int
449 one2one_register_sim_regno (struct gdbarch *gdbarch, int regnum)
450 {
451 /* Only makes sense to supply raw registers. */
452 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
453 return regnum;
454 }
455
456 void
457 gdbsim_target::fetch_registers (struct regcache *regcache, int regno)
458 {
459 struct gdbarch *gdbarch = regcache->arch ();
460 struct inferior *inf = find_inferior_ptid (regcache->ptid ());
461 struct sim_inferior_data *sim_data
462 = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
463
464 if (regno == -1)
465 {
466 for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++)
467 fetch_registers (regcache, regno);
468 return;
469 }
470
471 switch (gdbarch_register_sim_regno (gdbarch, regno))
472 {
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
478 as an ``unavailable'' register. */
479 regcache->raw_supply_zeroed (regno);
480 break;
481 }
482
483 default:
484 {
485 static int warn_user = 1;
486 int regsize = register_size (gdbarch, regno);
487 gdb::byte_vector buf (regsize, 0);
488 int nr_bytes;
489
490 gdb_assert (regno >= 0 && regno < gdbarch_num_regs (gdbarch));
491 nr_bytes = sim_fetch_register (sim_data->gdbsim_desc,
492 gdbarch_register_sim_regno
493 (gdbarch, regno),
494 buf.data (), regsize);
495 if (nr_bytes > 0 && nr_bytes != regsize && warn_user)
496 {
497 fprintf_unfiltered (gdb_stderr,
498 "Size of register %s (%d/%d) "
499 "incorrect (%d instead of %d))",
500 gdbarch_register_name (gdbarch, regno),
501 regno,
502 gdbarch_register_sim_regno (gdbarch, regno),
503 nr_bytes, regsize);
504 warn_user = 0;
505 }
506 /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
507 indicating that GDB and the SIM have different ideas about
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. */
511 regcache->raw_supply (regno, buf.data ());
512 if (remote_debug)
513 {
514 fprintf_unfiltered (gdb_stdlog,
515 "gdbsim_fetch_register: %d", regno);
516 /* FIXME: We could print something more intelligible. */
517 dump_mem (buf.data (), regsize);
518 }
519 break;
520 }
521 }
522 }
523
524
525 void
526 gdbsim_target::store_registers (struct regcache *regcache, int regno)
527 {
528 struct gdbarch *gdbarch = regcache->arch ();
529 struct inferior *inf = find_inferior_ptid (regcache->ptid ());
530 struct sim_inferior_data *sim_data
531 = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
532
533 if (regno == -1)
534 {
535 for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++)
536 store_registers (regcache, regno);
537 return;
538 }
539 else if (gdbarch_register_sim_regno (gdbarch, regno) >= 0)
540 {
541 int regsize = register_size (gdbarch, regno);
542 gdb::byte_vector tmp (regsize);
543 int nr_bytes;
544
545 regcache->cooked_read (regno, tmp.data ());
546 nr_bytes = sim_store_register (sim_data->gdbsim_desc,
547 gdbarch_register_sim_regno
548 (gdbarch, regno),
549 tmp.data (), regsize);
550
551 if (nr_bytes > 0 && nr_bytes != regsize)
552 internal_error (__FILE__, __LINE__,
553 _("Register size different to expected"));
554 if (nr_bytes < 0)
555 internal_error (__FILE__, __LINE__,
556 _("Register %d not updated"), regno);
557 if (nr_bytes == 0)
558 warning (_("Register %s not updated"),
559 gdbarch_register_name (gdbarch, regno));
560
561 if (remote_debug)
562 {
563 fprintf_unfiltered (gdb_stdlog, "gdbsim_store_register: %d", regno);
564 /* FIXME: We could print something more intelligible. */
565 dump_mem (tmp.data (), regsize);
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
573 void
574 gdbsim_target::kill ()
575 {
576 if (remote_debug)
577 fprintf_unfiltered (gdb_stdlog, "gdbsim_kill\n");
578
579 /* There is no need to `kill' running simulator - the simulator is
580 not running. Mourning it is enough. */
581 target_mourn_inferior (inferior_ptid);
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
588 void
589 gdbsim_target::load (const char *args, int fromtty)
590 {
591 const char *prog;
592 struct sim_inferior_data *sim_data
593 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
594
595 if (args == NULL)
596 error_no_arg (_("program to load"));
597
598 gdb_argv argv (args);
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
605 if (remote_debug)
606 fprintf_unfiltered (gdb_stdlog, "gdbsim_load: prog \"%s\"\n", prog);
607
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. */
611 if (sim_load (sim_data->gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL)
612 error (_("unable to load program"));
613
614 /* FIXME: If a load command should reset the targets registers then
615 a call to sim_create_inferior() should go here. */
616
617 sim_data->program_loaded = 1;
618 }
619
620
621 /* Start an inferior process and set inferior_ptid to its pid.
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
629 void
630 gdbsim_target::create_inferior (const char *exec_file,
631 const std::string &allargs,
632 char **env, int from_tty)
633 {
634 struct sim_inferior_data *sim_data
635 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
636 int len;
637 char *arg_buf;
638 const char *args = allargs.c_str ();
639
640 if (exec_file == 0 || exec_bfd == 0)
641 warning (_("No executable file specified."));
642 if (!sim_data->program_loaded)
643 warning (_("No program loaded."));
644
645 if (remote_debug)
646 fprintf_unfiltered (gdb_stdlog,
647 "gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
648 (exec_file ? exec_file : "(NULL)"),
649 args);
650
651 if (inferior_ptid == sim_data->remote_sim_ptid)
652 kill ();
653 remove_breakpoints ();
654 init_wait_for_inferior ();
655
656 gdb_argv built_argv;
657 if (exec_file != NULL)
658 {
659 len = strlen (exec_file) + 1 + allargs.size () + 1 + /*slop */ 10;
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);
665 built_argv.reset (arg_buf);
666 }
667
668 if (sim_create_inferior (sim_data->gdbsim_desc, exec_bfd,
669 built_argv.get (), env)
670 != SIM_RC_OK)
671 error (_("Unable to create sim inferior."));
672
673 inferior_ptid = sim_data->remote_sim_ptid;
674 inferior_appeared (current_inferior (), inferior_ptid.pid ());
675 add_thread_silent (inferior_ptid);
676
677 insert_breakpoints (); /* Needed to get correct instruction
678 in cache. */
679
680 clear_proceed_status (0);
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. */
686 /* Called when selecting the simulator. E.g. (gdb) target sim name. */
687
688 static void
689 gdbsim_target_open (const char *args, int from_tty)
690 {
691 int len;
692 char *arg_buf;
693 struct sim_inferior_data *sim_data;
694 const char *sysroot;
695 SIM_DESC gdbsim_desc;
696
697 sysroot = gdb_sysroot;
698 if (is_target_filename (sysroot))
699 sysroot += strlen (TARGET_SYSROOT_PREFIX);
700
701 if (remote_debug)
702 fprintf_unfiltered (gdb_stdlog,
703 "gdbsim_open: args \"%s\"\n", args ? args : "(null)");
704
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)
713 unpush_target (&gdbsim_ops);
714
715 len = (7 + 1 /* gdbsim */
716 + strlen (" -E little")
717 + strlen (" --architecture=xxxxxxxxxx")
718 + strlen (" --sysroot=") + strlen (sysroot) +
719 + (args ? strlen (args) : 0)
720 + 50) /* slack */ ;
721 arg_buf = (char *) alloca (len);
722 strcpy (arg_buf, "gdbsim"); /* 7 */
723 /* Specify the byte order for the target when it is explicitly
724 specified by the user (not auto detected). */
725 switch (selected_byte_order ())
726 {
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;
735 }
736 /* Specify the architecture of the target when it has been
737 explicitly specified */
738 if (selected_architecture_name () != NULL)
739 {
740 strcat (arg_buf, " --architecture=");
741 strcat (arg_buf, selected_architecture_name ());
742 }
743 /* Pass along gdb's concept of the sysroot. */
744 strcat (arg_buf, " --sysroot=");
745 strcat (arg_buf, sysroot);
746 /* finally, any explicit args */
747 if (args)
748 {
749 strcat (arg_buf, " "); /* 1 */
750 strcat (arg_buf, args);
751 }
752
753 gdb_argv argv (arg_buf);
754 sim_argv = argv.release ();
755
756 init_callbacks ();
757 gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, sim_argv);
758
759 if (gdbsim_desc == 0)
760 {
761 freeargv (sim_argv);
762 sim_argv = NULL;
763 error (_("unable to create simulator instance"));
764 }
765
766 /* Reset the pid numberings for this batch of sim instances. */
767 next_pid = INITIAL_PID;
768
769 /* Allocate the inferior data, but do not allocate a sim instance
770 since we've already just done that. */
771 sim_data = get_sim_inferior_data (current_inferior (),
772 SIM_INSTANCE_NOT_NEEDED);
773
774 sim_data->gdbsim_desc = gdbsim_desc;
775
776 push_target (&gdbsim_ops);
777 printf_filtered ("Connected to the simulator.\n");
778
779 /* There's nothing running after "target sim" or "load"; not until
780 "run". */
781 inferior_ptid = null_ptid;
782
783 gdbsim_is_open = 1;
784 }
785
786 /* Callback for iterate_over_inferiors. Called (indirectly) by
787 gdbsim_close(). */
788
789 static int
790 gdbsim_close_inferior (struct inferior *inf, void *arg)
791 {
792 struct sim_inferior_data *sim_data
793 = (struct sim_inferior_data *) inferior_data (inf, sim_inferior_data_key);
794 if (sim_data != NULL)
795 {
796 ptid_t ptid = sim_data->remote_sim_ptid;
797
798 sim_inferior_data_cleanup (inf, sim_data);
799 set_inferior_data (inf, sim_inferior_data_key, NULL);
800
801 /* Having a ptid allocated and stored in remote_sim_ptid does
802 not mean that a corresponding inferior was ever created.
803 Thus we need to verify the existence of an inferior using the
804 pid in question before setting inferior_ptid via
805 switch_to_thread() or mourning the inferior. */
806 if (find_inferior_ptid (ptid) != NULL)
807 {
808 switch_to_thread (ptid);
809 generic_mourn_inferior ();
810 }
811 }
812
813 return 0;
814 }
815
816 /* Close out all files and local state before this target loses control. */
817
818 void
819 gdbsim_target::close ()
820 {
821 if (remote_debug)
822 fprintf_unfiltered (gdb_stdlog, "gdbsim_close\n");
823
824 iterate_over_inferiors (gdbsim_close_inferior, NULL);
825
826 if (sim_argv != NULL)
827 {
828 freeargv (sim_argv);
829 sim_argv = NULL;
830 }
831
832 end_callbacks ();
833
834 gdbsim_is_open = 0;
835 }
836
837 /* Takes a program previously attached to and detaches it.
838 The program may resume execution (some targets do, some don't) and will
839 no longer stop on signals, etc. We better not have left any breakpoints
840 in the program or it'll die when it hits one. FROM_TTY says whether to be
841 verbose or not. */
842 /* Terminate the open connection to the remote debugger.
843 Use this when you want to detach and do something else with your gdb. */
844
845 void
846 gdbsim_target::detach (inferior *inf, int from_tty)
847 {
848 if (remote_debug)
849 fprintf_unfiltered (gdb_stdlog, "gdbsim_detach\n");
850
851 unpush_target (this); /* calls gdbsim_close to do the real work */
852 if (from_tty)
853 printf_filtered ("Ending simulator %s debugging\n", target_shortname);
854 }
855
856 /* Resume execution of the target process. STEP says whether to single-step
857 or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
858 to the target, or zero for no signal. */
859
860 struct resume_data
861 {
862 enum gdb_signal siggnal;
863 int step;
864 };
865
866 static int
867 gdbsim_resume_inferior (struct inferior *inf, void *arg)
868 {
869 struct sim_inferior_data *sim_data
870 = get_sim_inferior_data (inf, SIM_INSTANCE_NOT_NEEDED);
871 struct resume_data *rd = (struct resume_data *) arg;
872
873 if (sim_data)
874 {
875 sim_data->resume_siggnal = rd->siggnal;
876 sim_data->resume_step = rd->step;
877
878 if (remote_debug)
879 fprintf_unfiltered (gdb_stdlog,
880 _("gdbsim_resume: pid %d, step %d, signal %d\n"),
881 inf->pid, rd->step, rd->siggnal);
882 }
883
884 /* When called from iterate_over_inferiors, a zero return causes the
885 iteration process to proceed until there are no more inferiors to
886 consider. */
887 return 0;
888 }
889
890 void
891 gdbsim_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
892 {
893 struct resume_data rd;
894 struct sim_inferior_data *sim_data
895 = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);
896
897 rd.siggnal = siggnal;
898 rd.step = step;
899
900 /* We don't access any sim_data members within this function.
901 What's of interest is whether or not the call to
902 get_sim_inferior_data_by_ptid(), above, is able to obtain a
903 non-NULL pointer. If it managed to obtain a non-NULL pointer, we
904 know we have a single inferior to consider. If it's NULL, we
905 either have multiple inferiors to resume or an error condition. */
906
907 if (sim_data)
908 gdbsim_resume_inferior (find_inferior_ptid (ptid), &rd);
909 else if (ptid == minus_one_ptid)
910 iterate_over_inferiors (gdbsim_resume_inferior, &rd);
911 else
912 error (_("The program is not being run."));
913 }
914
915 /* Notify the simulator of an asynchronous request to interrupt.
916
917 The simulator shall ensure that the interrupt request is eventually
918 delivered to the simulator. If the call is made while the
919 simulator is not running then the interrupt request is processed when
920 the simulator is next resumed.
921
922 For simulators that do not support this operation, just abort. */
923
924 static int
925 gdbsim_interrupt_inferior (struct inferior *inf, void *arg)
926 {
927 struct sim_inferior_data *sim_data
928 = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
929
930 if (sim_data)
931 {
932 if (!sim_stop (sim_data->gdbsim_desc))
933 {
934 quit ();
935 }
936 }
937
938 /* When called from iterate_over_inferiors, a zero return causes the
939 iteration process to proceed until there are no more inferiors to
940 consider. */
941 return 0;
942 }
943
944 void
945 gdbsim_target::interrupt ()
946 {
947 iterate_over_inferiors (gdbsim_interrupt_inferior, NULL);
948 }
949
950 /* GDB version of os_poll_quit callback.
951 Taken from gdb/util.c - should be in a library. */
952
953 static int
954 gdb_os_poll_quit (host_callback *p)
955 {
956 if (deprecated_ui_loop_hook != NULL)
957 deprecated_ui_loop_hook (0);
958
959 if (check_quit_flag ()) /* gdb's idea of quit */
960 return 1;
961 return 0;
962 }
963
964 /* Wait for inferior process to do something. Return pid of child,
965 or -1 in case of error; store status through argument pointer STATUS,
966 just as `wait' would. */
967
968 static void
969 gdbsim_cntrl_c (int signo)
970 {
971 gdbsim_ops.interrupt ();
972 }
973
974 ptid_t
975 gdbsim_target::wait (ptid_t ptid, struct target_waitstatus *status, int options)
976 {
977 struct sim_inferior_data *sim_data;
978 static sighandler_t prev_sigint;
979 int sigrc = 0;
980 enum sim_stop reason = sim_running;
981
982 /* This target isn't able to (yet) resume more than one inferior at a time.
983 When ptid is minus_one_ptid, just use the current inferior. If we're
984 given an explicit pid, we'll try to find it and use that instead. */
985 if (ptid == minus_one_ptid)
986 sim_data = get_sim_inferior_data (current_inferior (),
987 SIM_INSTANCE_NEEDED);
988 else
989 {
990 sim_data = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NEEDED);
991 if (sim_data == NULL)
992 error (_("Unable to wait for pid %d. Inferior not found."),
993 ptid.pid ());
994 inferior_ptid = ptid;
995 }
996
997 if (remote_debug)
998 fprintf_unfiltered (gdb_stdlog, "gdbsim_wait\n");
999
1000 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1001 {
1002 struct sigaction sa, osa;
1003 sa.sa_handler = gdbsim_cntrl_c;
1004 sigemptyset (&sa.sa_mask);
1005 sa.sa_flags = 0;
1006 sigaction (SIGINT, &sa, &osa);
1007 prev_sigint = osa.sa_handler;
1008 }
1009 #else
1010 prev_sigint = signal (SIGINT, gdbsim_cntrl_c);
1011 #endif
1012 sim_resume (sim_data->gdbsim_desc, sim_data->resume_step,
1013 sim_data->resume_siggnal);
1014
1015 signal (SIGINT, prev_sigint);
1016 sim_data->resume_step = 0;
1017
1018 sim_stop_reason (sim_data->gdbsim_desc, &reason, &sigrc);
1019
1020 switch (reason)
1021 {
1022 case sim_exited:
1023 status->kind = TARGET_WAITKIND_EXITED;
1024 status->value.integer = sigrc;
1025 break;
1026 case sim_stopped:
1027 switch (sigrc)
1028 {
1029 case GDB_SIGNAL_ABRT:
1030 quit ();
1031 break;
1032 case GDB_SIGNAL_INT:
1033 case GDB_SIGNAL_TRAP:
1034 default:
1035 status->kind = TARGET_WAITKIND_STOPPED;
1036 status->value.sig = (enum gdb_signal) sigrc;
1037 break;
1038 }
1039 break;
1040 case sim_signalled:
1041 status->kind = TARGET_WAITKIND_SIGNALLED;
1042 status->value.sig = (enum gdb_signal) sigrc;
1043 break;
1044 case sim_running:
1045 case sim_polling:
1046 /* FIXME: Is this correct? */
1047 break;
1048 }
1049
1050 return inferior_ptid;
1051 }
1052
1053 /* Get ready to modify the registers array. On machines which store
1054 individual registers, this doesn't need to do anything. On machines
1055 which store all the registers in one fell swoop, this makes sure
1056 that registers contains all the registers from the program being
1057 debugged. */
1058
1059 void
1060 gdbsim_target::prepare_to_store (struct regcache *regcache)
1061 {
1062 /* Do nothing, since we can store individual regs. */
1063 }
1064
1065 /* Helper for gdbsim_xfer_partial that handles memory transfers.
1066 Arguments are like target_xfer_partial. */
1067
1068 static enum target_xfer_status
1069 gdbsim_xfer_memory (struct target_ops *target,
1070 gdb_byte *readbuf, const gdb_byte *writebuf,
1071 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
1072 {
1073 struct sim_inferior_data *sim_data
1074 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
1075 int l;
1076
1077 /* If this target doesn't have memory yet, return 0 causing the
1078 request to be passed to a lower target, hopefully an exec
1079 file. */
1080 if (!target->has_memory ())
1081 return TARGET_XFER_EOF;
1082
1083 if (!sim_data->program_loaded)
1084 error (_("No program loaded."));
1085
1086 /* Note that we obtained the sim_data pointer above using
1087 SIM_INSTANCE_NOT_NEEDED. We do this so that we don't needlessly
1088 allocate a sim instance prior to loading a program. If we
1089 get to this point in the code though, gdbsim_desc should be
1090 non-NULL. (Note that a sim instance is needed in order to load
1091 the program...) */
1092 gdb_assert (sim_data->gdbsim_desc != NULL);
1093
1094 if (remote_debug)
1095 fprintf_unfiltered (gdb_stdlog,
1096 "gdbsim_xfer_memory: readbuf %s, writebuf %s, "
1097 "memaddr %s, len %s\n",
1098 host_address_to_string (readbuf),
1099 host_address_to_string (writebuf),
1100 paddress (target_gdbarch (), memaddr),
1101 pulongest (len));
1102
1103 if (writebuf)
1104 {
1105 if (remote_debug && len > 0)
1106 dump_mem (writebuf, len);
1107 l = sim_write (sim_data->gdbsim_desc, memaddr, writebuf, len);
1108 }
1109 else
1110 {
1111 l = sim_read (sim_data->gdbsim_desc, memaddr, readbuf, len);
1112 if (remote_debug && len > 0)
1113 dump_mem (readbuf, len);
1114 }
1115 if (l > 0)
1116 {
1117 *xfered_len = (ULONGEST) l;
1118 return TARGET_XFER_OK;
1119 }
1120 else if (l == 0)
1121 return TARGET_XFER_EOF;
1122 else
1123 return TARGET_XFER_E_IO;
1124 }
1125
1126 /* Target to_xfer_partial implementation. */
1127
1128 enum target_xfer_status
1129 gdbsim_target::xfer_partial (enum target_object object,
1130 const char *annex, gdb_byte *readbuf,
1131 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
1132 ULONGEST *xfered_len)
1133 {
1134 switch (object)
1135 {
1136 case TARGET_OBJECT_MEMORY:
1137 return gdbsim_xfer_memory (this, readbuf, writebuf, offset, len,
1138 xfered_len);
1139
1140 default:
1141 return TARGET_XFER_E_IO;
1142 }
1143 }
1144
1145 void
1146 gdbsim_target::files_info ()
1147 {
1148 struct sim_inferior_data *sim_data
1149 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
1150 const char *file = "nothing";
1151
1152 if (exec_bfd)
1153 file = bfd_get_filename (exec_bfd);
1154
1155 if (remote_debug)
1156 fprintf_unfiltered (gdb_stdlog, "gdbsim_files_info: file \"%s\"\n", file);
1157
1158 if (exec_bfd)
1159 {
1160 fprintf_unfiltered (gdb_stdlog, "\tAttached to %s running program %s\n",
1161 target_shortname, file);
1162 sim_info (sim_data->gdbsim_desc, 0);
1163 }
1164 }
1165
1166 /* Clear the simulator's notion of what the break points are. */
1167
1168 void
1169 gdbsim_target::mourn_inferior ()
1170 {
1171 if (remote_debug)
1172 fprintf_unfiltered (gdb_stdlog, "gdbsim_mourn_inferior:\n");
1173
1174 remove_breakpoints ();
1175 generic_mourn_inferior ();
1176 }
1177
1178 /* Pass the command argument through to the simulator verbatim. The
1179 simulator must do any command interpretation work. */
1180
1181 void
1182 simulator_command (const char *args, int from_tty)
1183 {
1184 struct sim_inferior_data *sim_data;
1185
1186 /* We use inferior_data() instead of get_sim_inferior_data() here in
1187 order to avoid attaching a sim_inferior_data struct to an
1188 inferior unnecessarily. The reason we take such care here is due
1189 to the fact that this function, simulator_command(), may be called
1190 even when the sim target is not active. If we were to use
1191 get_sim_inferior_data() here, it is possible that this call would
1192 be made either prior to gdbsim_open() or after gdbsim_close(),
1193 thus allocating memory that would not be garbage collected until
1194 the ultimate destruction of the associated inferior. */
1195
1196 sim_data = ((struct sim_inferior_data *)
1197 inferior_data (current_inferior (), sim_inferior_data_key));
1198 if (sim_data == NULL || sim_data->gdbsim_desc == NULL)
1199 {
1200
1201 /* PREVIOUSLY: The user may give a command before the simulator
1202 is opened. [...] (??? assuming of course one wishes to
1203 continue to allow commands to be sent to unopened simulators,
1204 which isn't entirely unreasonable). */
1205
1206 /* The simulator is a builtin abstraction of a remote target.
1207 Consistent with that model, access to the simulator, via sim
1208 commands, is restricted to the period when the channel to the
1209 simulator is open. */
1210
1211 error (_("Not connected to the simulator target"));
1212 }
1213
1214 sim_do_command (sim_data->gdbsim_desc, args);
1215
1216 /* Invalidate the register cache, in case the simulator command does
1217 something funny. */
1218 registers_changed ();
1219 }
1220
1221 static void
1222 sim_command_completer (struct cmd_list_element *ignore,
1223 completion_tracker &tracker,
1224 const char *text, const char *word)
1225 {
1226 struct sim_inferior_data *sim_data;
1227
1228 sim_data = ((struct sim_inferior_data *)
1229 inferior_data (current_inferior (), sim_inferior_data_key));
1230 if (sim_data == NULL || sim_data->gdbsim_desc == NULL)
1231 return;
1232
1233 /* sim_complete_command returns a NULL-terminated malloc'ed array of
1234 malloc'ed strings. */
1235 struct sim_completions_deleter
1236 {
1237 void operator() (char **ptr) const
1238 {
1239 for (size_t i = 0; ptr[i] != NULL; i++)
1240 xfree (ptr[i]);
1241 xfree (ptr);
1242 }
1243 };
1244
1245 std::unique_ptr<char *[], sim_completions_deleter> sim_completions
1246 (sim_complete_command (sim_data->gdbsim_desc, text, word));
1247 if (sim_completions == NULL)
1248 return;
1249
1250 /* Count the elements and add completions from tail to head because
1251 below we'll swap elements out of the array in case add_completion
1252 throws and the deleter deletes until it finds a NULL element. */
1253 size_t count = 0;
1254 while (sim_completions[count] != NULL)
1255 count++;
1256
1257 for (size_t i = count; i > 0; i--)
1258 {
1259 gdb::unique_xmalloc_ptr<char> match (sim_completions[i - 1]);
1260 sim_completions[i - 1] = NULL;
1261 tracker.add_completion (std::move (match));
1262 }
1263 }
1264
1265 /* Check to see if a thread is still alive. */
1266
1267 bool
1268 gdbsim_target::thread_alive (ptid_t ptid)
1269 {
1270 struct sim_inferior_data *sim_data
1271 = get_sim_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);
1272
1273 if (sim_data == NULL)
1274 return false;
1275
1276 if (ptid == sim_data->remote_sim_ptid)
1277 /* The simulators' task is always alive. */
1278 return true;
1279
1280 return false;
1281 }
1282
1283 /* Convert a thread ID to a string. */
1284
1285 std::string
1286 gdbsim_target::pid_to_str (ptid_t ptid)
1287 {
1288 return normal_pid_to_str (ptid);
1289 }
1290
1291 /* Simulator memory may be accessed after the program has been loaded. */
1292
1293 bool
1294 gdbsim_target::has_all_memory ()
1295 {
1296 struct sim_inferior_data *sim_data
1297 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
1298
1299 if (!sim_data->program_loaded)
1300 return false;
1301
1302 return true;
1303 }
1304
1305 bool
1306 gdbsim_target::has_memory ()
1307 {
1308 struct sim_inferior_data *sim_data
1309 = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
1310
1311 if (!sim_data->program_loaded)
1312 return false;
1313
1314 return true;
1315 }
1316
1317 void
1318 _initialize_remote_sim (void)
1319 {
1320 struct cmd_list_element *c;
1321
1322 add_target (gdbsim_target_info, gdbsim_target_open);
1323
1324 c = add_com ("sim", class_obscure, simulator_command,
1325 _("Send a command to the simulator."));
1326 set_cmd_completer (c, sim_command_completer);
1327
1328 sim_inferior_data_key
1329 = register_inferior_data_with_cleanup (NULL, sim_inferior_data_cleanup);
1330 }
This page took 0.068995 seconds and 4 git commands to generate.