1 /* gdb-if.c -- sim interface to GDB.
3 Copyright (C) 2008-2019 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "gdb/callback.h"
31 #include "gdb/remote-sim.h"
32 #include "gdb/signals.h"
33 #include "gdb/sim-rx.h"
42 /* Ideally, we'd wrap up all the minisim's data structures in an
43 object and pass that around. However, neither GDB nor run needs
46 So we just have one instance, that lives in global variables, and
47 each time we open it, we re-initialize it. */
53 static struct sim_state the_minisim
= {
54 "This is the sole rx minisim instance. See libsim.a's global variables."
57 static int rx_sim_is_open
;
60 sim_open (SIM_OPEN_KIND kind
,
61 struct host_callback_struct
*callback
,
62 struct bfd
*abfd
, char * const *argv
)
65 fprintf (stderr
, "rx minisim: re-opened sim\n");
67 /* The 'run' interface doesn't use this function, so we don't care
68 about KIND; it's always SIM_OPEN_DEBUG. */
69 if (kind
!= SIM_OPEN_DEBUG
)
70 fprintf (stderr
, "rx minisim: sim_open KIND != SIM_OPEN_DEBUG: %d\n",
73 set_callbacks (callback
);
75 /* We don't expect any command-line arguments. */
79 execution_error_init_debugger ();
81 sim_disasm_init (abfd
);
87 check_desc (SIM_DESC sd
)
89 if (sd
!= &the_minisim
)
90 fprintf (stderr
, "rx minisim: desc != &the_minisim\n");
94 sim_close (SIM_DESC sd
, int quitting
)
98 /* Not much to do. At least free up our memory. */
105 open_objfile (const char *filename
)
107 bfd
*prog
= bfd_openr (filename
, 0);
111 fprintf (stderr
, "Can't read %s\n", filename
);
115 if (!bfd_check_format (prog
, bfd_object
))
117 fprintf (stderr
, "%s not a rx program\n", filename
);
124 static struct swap_list
127 struct swap_list
*next
;
131 free_swap_list (void)
135 struct swap_list
*next
= swap_list
->next
;
141 /* When running in big endian mode, we must do an additional
142 byte swap of memory areas used to hold instructions. See
143 the comment preceding rx_load in load.c to see why this is
146 Construct a list of memory areas that must be byte swapped.
147 This list will be consulted when either reading or writing
151 build_swap_list (struct bfd
*abfd
)
156 /* Nothing to do when in little endian mode. */
160 for (s
= abfd
->sections
; s
; s
= s
->next
)
162 if ((s
->flags
& SEC_LOAD
) && (s
->flags
& SEC_CODE
))
164 struct swap_list
*sl
;
167 size
= bfd_get_section_size (s
);
171 sl
= malloc (sizeof (struct swap_list
));
173 sl
->next
= swap_list
;
174 sl
->start
= bfd_section_lma (abfd
, s
);
175 sl
->end
= sl
->start
+ size
;
182 addr_in_swap_list (bfd_vma addr
)
186 for (s
= swap_list
; s
; s
= s
->next
)
188 if (s
->start
<= addr
&& addr
< s
->end
)
195 sim_load (SIM_DESC sd
, const char *prog
, struct bfd
*abfd
, int from_tty
)
200 abfd
= open_objfile (prog
);
204 rx_load (abfd
, get_callbacks ());
205 build_swap_list (abfd
);
211 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
212 char * const *argv
, char * const *env
)
218 rx_load (abfd
, NULL
);
219 build_swap_list (abfd
);
226 sim_read (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
235 execution_error_clear_last_error ();
237 for (i
= 0; i
< length
; i
++)
239 bfd_vma addr
= mem
+ i
;
240 int do_swap
= addr_in_swap_list (addr
);
241 buf
[i
] = mem_get_qi (addr
^ (do_swap
? 3 : 0));
243 if (execution_error_get_last_error () != SIM_ERR_NONE
)
251 sim_write (SIM_DESC sd
, SIM_ADDR mem
, const unsigned char *buf
, int length
)
257 execution_error_clear_last_error ();
259 for (i
= 0; i
< length
; i
++)
261 bfd_vma addr
= mem
+ i
;
262 int do_swap
= addr_in_swap_list (addr
);
263 mem_put_qi (addr
^ (do_swap
? 3 : 0), buf
[i
]);
265 if (execution_error_get_last_error () != SIM_ERR_NONE
)
272 /* Read the LENGTH bytes at BUF as an little-endian value. */
274 get_le (unsigned char *buf
, int length
)
277 while (--length
>= 0)
278 acc
= (acc
<< 8) + buf
[length
];
283 /* Read the LENGTH bytes at BUF as a big-endian value. */
285 get_be (unsigned char *buf
, int length
)
289 acc
= (acc
<< 8) + *buf
++;
294 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
296 put_le (unsigned char *buf
, int length
, DI val
)
300 for (i
= 0; i
< length
; i
++)
307 /* Store VAL as a big-endian value in the LENGTH bytes at BUF. */
309 put_be (unsigned char *buf
, int length
, DI val
)
313 for (i
= length
-1; i
>= 0; i
--)
322 check_regno (enum sim_rx_regnum regno
)
324 return 0 <= regno
&& regno
< sim_rx_num_regs
;
328 reg_size (enum sim_rx_regnum regno
)
334 case sim_rx_r0_regnum
:
335 size
= sizeof (regs
.r
[0]);
337 case sim_rx_r1_regnum
:
338 size
= sizeof (regs
.r
[1]);
340 case sim_rx_r2_regnum
:
341 size
= sizeof (regs
.r
[2]);
343 case sim_rx_r3_regnum
:
344 size
= sizeof (regs
.r
[3]);
346 case sim_rx_r4_regnum
:
347 size
= sizeof (regs
.r
[4]);
349 case sim_rx_r5_regnum
:
350 size
= sizeof (regs
.r
[5]);
352 case sim_rx_r6_regnum
:
353 size
= sizeof (regs
.r
[6]);
355 case sim_rx_r7_regnum
:
356 size
= sizeof (regs
.r
[7]);
358 case sim_rx_r8_regnum
:
359 size
= sizeof (regs
.r
[8]);
361 case sim_rx_r9_regnum
:
362 size
= sizeof (regs
.r
[9]);
364 case sim_rx_r10_regnum
:
365 size
= sizeof (regs
.r
[10]);
367 case sim_rx_r11_regnum
:
368 size
= sizeof (regs
.r
[11]);
370 case sim_rx_r12_regnum
:
371 size
= sizeof (regs
.r
[12]);
373 case sim_rx_r13_regnum
:
374 size
= sizeof (regs
.r
[13]);
376 case sim_rx_r14_regnum
:
377 size
= sizeof (regs
.r
[14]);
379 case sim_rx_r15_regnum
:
380 size
= sizeof (regs
.r
[15]);
382 case sim_rx_isp_regnum
:
383 size
= sizeof (regs
.r_isp
);
385 case sim_rx_usp_regnum
:
386 size
= sizeof (regs
.r_usp
);
388 case sim_rx_intb_regnum
:
389 size
= sizeof (regs
.r_intb
);
391 case sim_rx_pc_regnum
:
392 size
= sizeof (regs
.r_pc
);
394 case sim_rx_ps_regnum
:
395 size
= sizeof (regs
.r_psw
);
397 case sim_rx_bpc_regnum
:
398 size
= sizeof (regs
.r_bpc
);
400 case sim_rx_bpsw_regnum
:
401 size
= sizeof (regs
.r_bpsw
);
403 case sim_rx_fintv_regnum
:
404 size
= sizeof (regs
.r_fintv
);
406 case sim_rx_fpsw_regnum
:
407 size
= sizeof (regs
.r_fpsw
);
409 case sim_rx_acc_regnum
:
410 size
= sizeof (regs
.r_acc
);
420 sim_fetch_register (SIM_DESC sd
, int regno
, unsigned char *buf
, int length
)
427 if (!check_regno (regno
))
430 size
= reg_size (regno
);
437 case sim_rx_r0_regnum
:
440 case sim_rx_r1_regnum
:
443 case sim_rx_r2_regnum
:
446 case sim_rx_r3_regnum
:
449 case sim_rx_r4_regnum
:
452 case sim_rx_r5_regnum
:
455 case sim_rx_r6_regnum
:
458 case sim_rx_r7_regnum
:
461 case sim_rx_r8_regnum
:
464 case sim_rx_r9_regnum
:
467 case sim_rx_r10_regnum
:
470 case sim_rx_r11_regnum
:
473 case sim_rx_r12_regnum
:
476 case sim_rx_r13_regnum
:
479 case sim_rx_r14_regnum
:
482 case sim_rx_r15_regnum
:
485 case sim_rx_isp_regnum
:
488 case sim_rx_usp_regnum
:
491 case sim_rx_intb_regnum
:
492 val
= get_reg (intb
);
494 case sim_rx_pc_regnum
:
497 case sim_rx_ps_regnum
:
500 case sim_rx_bpc_regnum
:
503 case sim_rx_bpsw_regnum
:
504 val
= get_reg (bpsw
);
506 case sim_rx_fintv_regnum
:
507 val
= get_reg (fintv
);
509 case sim_rx_fpsw_regnum
:
510 val
= get_reg (fpsw
);
512 case sim_rx_acc_regnum
:
513 val
= ((DI
) get_reg (acchi
) << 32) | get_reg (acclo
);
516 fprintf (stderr
, "rx minisim: unrecognized register number: %d\n",
522 put_be (buf
, length
, val
);
524 put_le (buf
, length
, val
);
530 sim_store_register (SIM_DESC sd
, int regno
, unsigned char *buf
, int length
)
537 if (!check_regno (regno
))
540 size
= reg_size (regno
);
546 val
= get_be (buf
, length
);
548 val
= get_le (buf
, length
);
552 case sim_rx_r0_regnum
:
555 case sim_rx_r1_regnum
:
558 case sim_rx_r2_regnum
:
561 case sim_rx_r3_regnum
:
564 case sim_rx_r4_regnum
:
567 case sim_rx_r5_regnum
:
570 case sim_rx_r6_regnum
:
573 case sim_rx_r7_regnum
:
576 case sim_rx_r8_regnum
:
579 case sim_rx_r9_regnum
:
582 case sim_rx_r10_regnum
:
585 case sim_rx_r11_regnum
:
588 case sim_rx_r12_regnum
:
591 case sim_rx_r13_regnum
:
594 case sim_rx_r14_regnum
:
597 case sim_rx_r15_regnum
:
600 case sim_rx_isp_regnum
:
603 case sim_rx_usp_regnum
:
606 case sim_rx_intb_regnum
:
609 case sim_rx_pc_regnum
:
612 case sim_rx_ps_regnum
:
615 case sim_rx_bpc_regnum
:
618 case sim_rx_bpsw_regnum
:
621 case sim_rx_fintv_regnum
:
622 put_reg (fintv
, val
);
624 case sim_rx_fpsw_regnum
:
627 case sim_rx_acc_regnum
:
628 put_reg (acclo
, val
& 0xffffffff);
629 put_reg (acchi
, (val
>> 32) & 0xffffffff);
632 fprintf (stderr
, "rx minisim: unrecognized register number: %d\n",
641 sim_info (SIM_DESC sd
, int verbose
)
645 printf ("The rx minisim doesn't collect any statistics.\n");
648 static volatile int stop
;
649 static enum sim_stop reason
;
653 /* Given a signal number used by the RX bsp (that is, newlib),
654 return a target signal number used by GDB. */
656 rx_signal_to_gdb_signal (int rx
)
661 return GDB_SIGNAL_ILL
;
664 return GDB_SIGNAL_TRAP
;
667 return GDB_SIGNAL_BUS
;
670 return GDB_SIGNAL_SEGV
;
673 return GDB_SIGNAL_XCPU
;
676 return GDB_SIGNAL_INT
;
679 return GDB_SIGNAL_FPE
;
682 return GDB_SIGNAL_ABRT
;
689 /* Take a step return code RC and set up the variables consulted by
690 sim_stop_reason appropriately. */
694 if (execution_error_get_last_error () != SIM_ERR_NONE
)
696 reason
= sim_stopped
;
697 siggnal
= GDB_SIGNAL_SEGV
;
699 if (RX_STEPPED (rc
) || RX_HIT_BREAK (rc
))
701 reason
= sim_stopped
;
702 siggnal
= GDB_SIGNAL_TRAP
;
704 else if (RX_STOPPED (rc
))
706 reason
= sim_stopped
;
707 siggnal
= rx_signal_to_gdb_signal (RX_STOP_SIG (rc
));
711 assert (RX_EXITED (rc
));
713 siggnal
= RX_EXIT_STATUS (rc
);
719 sim_resume (SIM_DESC sd
, int step
, int sig_to_deliver
)
725 if (sig_to_deliver
!= 0)
728 "Warning: the rx minisim does not implement "
729 "signal delivery yet.\n" "Resuming with no signal.\n");
732 execution_error_clear_last_error ();
736 rc
= setjmp (decode_jmp_buf
);
738 rc
= decode_opcode ();
743 /* We don't clear 'stop' here, because then we would miss
744 interrupts that arrived on the way here. Instead, we clear
745 the flag in sim_stop_reason, after GDB has disabled the
746 interrupt signal handler. */
752 reason
= sim_stopped
;
753 siggnal
= GDB_SIGNAL_INT
;
757 rc
= setjmp (decode_jmp_buf
);
759 rc
= decode_opcode ();
761 if (execution_error_get_last_error () != SIM_ERR_NONE
)
763 reason
= sim_stopped
;
764 siggnal
= GDB_SIGNAL_SEGV
;
768 if (!RX_STEPPED (rc
))
778 sim_stop (SIM_DESC sd
)
786 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason_p
, int *sigrc_p
)
795 sim_do_command (SIM_DESC sd
, const char *cmd
)
798 char *p
= strdup (cmd
);
802 /* Skip leading whitespace. */
806 /* Find the extent of the command word. */
807 for (p
= cmd
; *p
; p
++)
811 /* Null-terminate the command word, and record the start of any
812 further arguments. */
817 while (isspace (*args
))
823 if (strcmp (cmd
, "trace") == 0)
825 if (strcmp (args
, "on") == 0)
827 else if (strcmp (args
, "off") == 0)
830 printf ("The 'sim trace' command expects 'on' or 'off' "
831 "as an argument.\n");
833 else if (strcmp (cmd
, "verbose") == 0)
835 if (strcmp (args
, "on") == 0)
837 else if (strcmp (args
, "noisy") == 0)
839 else if (strcmp (args
, "off") == 0)
842 printf ("The 'sim verbose' command expects 'on', 'noisy', or 'off'"
843 " as an argument.\n");
846 printf ("The 'sim' command expects either 'trace' or 'verbose'"
847 " as a subcommand.\n");
853 sim_complete_command (SIM_DESC sd
, const char *text
, const char *word
)