1 /* gdb-if.c -- sim interface to GDB.
3 Copyright (C) 2008, 2009, 2010 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/>. */
29 #include "gdb/callback.h"
30 #include "gdb/remote-sim.h"
31 #include "gdb/signals.h"
32 #include "gdb/sim-rx.h"
40 /* Ideally, we'd wrap up all the minisim's data structures in an
41 object and pass that around. However, neither GDB nor run needs
44 So we just have one instance, that lives in global variables, and
45 each time we open it, we re-initialize it. */
51 static struct sim_state the_minisim
= {
52 "This is the sole rx minisim instance. See libsim.a's global variables."
58 sim_open (SIM_OPEN_KIND kind
,
59 struct host_callback_struct
*callback
,
60 struct bfd
*abfd
, char **argv
)
63 fprintf (stderr
, "rx minisim: re-opened sim\n");
65 /* The 'run' interface doesn't use this function, so we don't care
66 about KIND; it's always SIM_OPEN_DEBUG. */
67 if (kind
!= SIM_OPEN_DEBUG
)
68 fprintf (stderr
, "rx minisim: sim_open KIND != SIM_OPEN_DEBUG: %d\n",
71 set_callbacks (callback
);
73 /* We don't expect any command-line arguments. */
77 execution_error_init_debugger ();
79 sim_disasm_init (abfd
);
85 check_desc (SIM_DESC sd
)
87 if (sd
!= &the_minisim
)
88 fprintf (stderr
, "rx minisim: desc != &the_minisim\n");
92 sim_close (SIM_DESC sd
, int quitting
)
96 /* Not much to do. At least free up our memory. */
103 open_objfile (const char *filename
)
105 bfd
*prog
= bfd_openr (filename
, 0);
109 fprintf (stderr
, "Can't read %s\n", filename
);
113 if (!bfd_check_format (prog
, bfd_object
))
115 fprintf (stderr
, "%s not a rx program\n", filename
);
122 static struct swap_list
125 struct swap_list
*next
;
129 free_swap_list (void)
133 struct swap_list
*next
= swap_list
->next
;
139 /* When running in big endian mode, we must do an additional
140 byte swap of memory areas used to hold instructions. See
141 the comment preceding rx_load in load.c to see why this is
144 Construct a list of memory areas that must be byte swapped.
145 This list will be consulted when either reading or writing
149 build_swap_list (struct bfd
*abfd
)
154 /* Nothing to do when in little endian mode. */
158 for (s
= abfd
->sections
; s
; s
= s
->next
)
160 if ((s
->flags
& SEC_LOAD
) && (s
->flags
& SEC_CODE
))
162 struct swap_list
*sl
;
165 size
= bfd_get_section_size (s
);
169 sl
= malloc (sizeof (struct swap_list
));
171 sl
->next
= swap_list
;
172 sl
->start
= bfd_section_lma (abfd
, s
);
173 sl
->end
= sl
->start
+ size
;
180 addr_in_swap_list (bfd_vma addr
)
184 for (s
= swap_list
; s
; s
= s
->next
)
186 if (s
->start
<= addr
&& addr
< s
->end
)
193 sim_load (SIM_DESC sd
, char *prog
, struct bfd
*abfd
, int from_tty
)
198 abfd
= open_objfile (prog
);
203 build_swap_list (abfd
);
209 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
, char **argv
, char **env
)
216 build_swap_list (abfd
);
223 sim_read (SIM_DESC sd
, SIM_ADDR mem
, unsigned char *buf
, int length
)
232 execution_error_clear_last_error ();
234 for (i
= 0; i
< length
; i
++)
236 bfd_vma addr
= mem
+ i
;
237 int do_swap
= addr_in_swap_list (addr
);
238 buf
[i
] = mem_get_qi (addr
^ (do_swap
? 3 : 0));
240 if (execution_error_get_last_error () != SIM_ERR_NONE
)
248 sim_write (SIM_DESC sd
, SIM_ADDR mem
, const unsigned char *buf
, int length
)
254 execution_error_clear_last_error ();
256 for (i
= 0; i
< length
; i
++)
258 bfd_vma addr
= mem
+ i
;
259 int do_swap
= addr_in_swap_list (addr
);
260 mem_put_qi (addr
^ (do_swap
? 3 : 0), buf
[i
]);
262 if (execution_error_get_last_error () != SIM_ERR_NONE
)
269 /* Read the LENGTH bytes at BUF as an little-endian value. */
271 get_le (unsigned char *buf
, int length
)
274 while (--length
>= 0)
275 acc
= (acc
<< 8) + buf
[length
];
280 /* Read the LENGTH bytes at BUF as a big-endian value. */
282 get_be (unsigned char *buf
, int length
)
286 acc
= (acc
<< 8) + *buf
++;
291 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
293 put_le (unsigned char *buf
, int length
, DI val
)
297 for (i
= 0; i
< length
; i
++)
304 /* Store VAL as a big-endian value in the LENGTH bytes at BUF. */
306 put_be (unsigned char *buf
, int length
, DI val
)
310 for (i
= length
-1; i
>= 0; i
--)
319 check_regno (enum sim_rx_regnum regno
)
321 return 0 <= regno
&& regno
< sim_rx_num_regs
;
325 reg_size (enum sim_rx_regnum regno
)
331 case sim_rx_r0_regnum
:
332 size
= sizeof (regs
.r
[0]);
334 case sim_rx_r1_regnum
:
335 size
= sizeof (regs
.r
[1]);
337 case sim_rx_r2_regnum
:
338 size
= sizeof (regs
.r
[2]);
340 case sim_rx_r3_regnum
:
341 size
= sizeof (regs
.r
[3]);
343 case sim_rx_r4_regnum
:
344 size
= sizeof (regs
.r
[4]);
346 case sim_rx_r5_regnum
:
347 size
= sizeof (regs
.r
[5]);
349 case sim_rx_r6_regnum
:
350 size
= sizeof (regs
.r
[6]);
352 case sim_rx_r7_regnum
:
353 size
= sizeof (regs
.r
[7]);
355 case sim_rx_r8_regnum
:
356 size
= sizeof (regs
.r
[8]);
358 case sim_rx_r9_regnum
:
359 size
= sizeof (regs
.r
[9]);
361 case sim_rx_r10_regnum
:
362 size
= sizeof (regs
.r
[10]);
364 case sim_rx_r11_regnum
:
365 size
= sizeof (regs
.r
[11]);
367 case sim_rx_r12_regnum
:
368 size
= sizeof (regs
.r
[12]);
370 case sim_rx_r13_regnum
:
371 size
= sizeof (regs
.r
[13]);
373 case sim_rx_r14_regnum
:
374 size
= sizeof (regs
.r
[14]);
376 case sim_rx_r15_regnum
:
377 size
= sizeof (regs
.r
[15]);
379 case sim_rx_isp_regnum
:
380 size
= sizeof (regs
.r_isp
);
382 case sim_rx_usp_regnum
:
383 size
= sizeof (regs
.r_usp
);
385 case sim_rx_intb_regnum
:
386 size
= sizeof (regs
.r_intb
);
388 case sim_rx_pc_regnum
:
389 size
= sizeof (regs
.r_pc
);
391 case sim_rx_ps_regnum
:
392 size
= sizeof (regs
.r_psw
);
394 case sim_rx_bpc_regnum
:
395 size
= sizeof (regs
.r_bpc
);
397 case sim_rx_bpsw_regnum
:
398 size
= sizeof (regs
.r_bpsw
);
400 case sim_rx_fintv_regnum
:
401 size
= sizeof (regs
.r_fintv
);
403 case sim_rx_fpsw_regnum
:
404 size
= sizeof (regs
.r_fpsw
);
414 sim_fetch_register (SIM_DESC sd
, int regno
, unsigned char *buf
, int length
)
421 if (!check_regno (regno
))
424 size
= reg_size (regno
);
431 case sim_rx_r0_regnum
:
434 case sim_rx_r1_regnum
:
437 case sim_rx_r2_regnum
:
440 case sim_rx_r3_regnum
:
443 case sim_rx_r4_regnum
:
446 case sim_rx_r5_regnum
:
449 case sim_rx_r6_regnum
:
452 case sim_rx_r7_regnum
:
455 case sim_rx_r8_regnum
:
458 case sim_rx_r9_regnum
:
461 case sim_rx_r10_regnum
:
464 case sim_rx_r11_regnum
:
467 case sim_rx_r12_regnum
:
470 case sim_rx_r13_regnum
:
473 case sim_rx_r14_regnum
:
476 case sim_rx_r15_regnum
:
479 case sim_rx_isp_regnum
:
482 case sim_rx_usp_regnum
:
485 case sim_rx_intb_regnum
:
486 val
= get_reg (intb
);
488 case sim_rx_pc_regnum
:
491 case sim_rx_ps_regnum
:
494 case sim_rx_bpc_regnum
:
497 case sim_rx_bpsw_regnum
:
498 val
= get_reg (bpsw
);
500 case sim_rx_fintv_regnum
:
501 val
= get_reg (fintv
);
503 case sim_rx_fpsw_regnum
:
504 val
= get_reg (fpsw
);
507 fprintf (stderr
, "rx minisim: unrecognized register number: %d\n",
513 put_be (buf
, length
, val
);
515 put_le (buf
, length
, val
);
521 sim_store_register (SIM_DESC sd
, int regno
, unsigned char *buf
, int length
)
528 if (!check_regno (regno
))
531 size
= reg_size (regno
);
537 val
= get_be (buf
, length
);
539 val
= get_le (buf
, length
);
543 case sim_rx_r0_regnum
:
546 case sim_rx_r1_regnum
:
549 case sim_rx_r2_regnum
:
552 case sim_rx_r3_regnum
:
555 case sim_rx_r4_regnum
:
558 case sim_rx_r5_regnum
:
561 case sim_rx_r6_regnum
:
564 case sim_rx_r7_regnum
:
567 case sim_rx_r8_regnum
:
570 case sim_rx_r9_regnum
:
573 case sim_rx_r10_regnum
:
576 case sim_rx_r11_regnum
:
579 case sim_rx_r12_regnum
:
582 case sim_rx_r13_regnum
:
585 case sim_rx_r14_regnum
:
588 case sim_rx_r15_regnum
:
591 case sim_rx_isp_regnum
:
594 case sim_rx_usp_regnum
:
597 case sim_rx_intb_regnum
:
600 case sim_rx_pc_regnum
:
603 case sim_rx_ps_regnum
:
606 case sim_rx_bpc_regnum
:
609 case sim_rx_bpsw_regnum
:
612 case sim_rx_fintv_regnum
:
613 put_reg (fintv
, val
);
615 case sim_rx_fpsw_regnum
:
619 fprintf (stderr
, "rx minisim: unrecognized register number: %d\n",
628 sim_info (SIM_DESC sd
, int verbose
)
632 printf ("The rx minisim doesn't collect any statistics.\n");
635 static volatile int stop
;
636 static enum sim_stop reason
;
640 /* Given a signal number used by the RX bsp (that is, newlib),
641 return a host signal number. (Oddly, the gdb/sim interface uses
642 host signal numbers...) */
644 rx_signal_to_host (int rx
)
693 /* Take a step return code RC and set up the variables consulted by
694 sim_stop_reason appropriately. */
698 if (execution_error_get_last_error () != SIM_ERR_NONE
)
700 reason
= sim_stopped
;
701 siggnal
= TARGET_SIGNAL_SEGV
;
703 if (RX_STEPPED (rc
) || RX_HIT_BREAK (rc
))
705 reason
= sim_stopped
;
706 siggnal
= TARGET_SIGNAL_TRAP
;
708 else if (RX_STOPPED (rc
))
710 reason
= sim_stopped
;
711 siggnal
= rx_signal_to_host (RX_STOP_SIG (rc
));
715 assert (RX_EXITED (rc
));
717 siggnal
= RX_EXIT_STATUS (rc
);
723 sim_resume (SIM_DESC sd
, int step
, int sig_to_deliver
)
727 if (sig_to_deliver
!= 0)
730 "Warning: the rx minisim does not implement "
731 "signal delivery yet.\n" "Resuming with no signal.\n");
734 execution_error_clear_last_error ();
737 handle_step (decode_opcode ());
740 /* We don't clear 'stop' here, because then we would miss
741 interrupts that arrived on the way here. Instead, we clear
742 the flag in sim_stop_reason, after GDB has disabled the
743 interrupt signal handler. */
749 reason
= sim_stopped
;
750 siggnal
= TARGET_SIGNAL_INT
;
754 int rc
= decode_opcode ();
756 if (execution_error_get_last_error () != SIM_ERR_NONE
)
758 reason
= sim_stopped
;
759 siggnal
= TARGET_SIGNAL_SEGV
;
763 if (!RX_STEPPED (rc
))
773 sim_stop (SIM_DESC sd
)
781 sim_stop_reason (SIM_DESC sd
, enum sim_stop
*reason_p
, int *sigrc_p
)
790 sim_do_command (SIM_DESC sd
, char *cmd
)
796 /* Skip leading whitespace. */
800 /* Find the extent of the command word. */
801 for (p
= cmd
; *p
; p
++)
805 /* Null-terminate the command word, and record the start of any
806 further arguments. */
812 while (isspace (*args
))
818 if (strcmp (cmd
, "trace") == 0)
820 if (strcmp (args
, "on") == 0)
822 else if (strcmp (args
, "off") == 0)
825 printf ("The 'sim trace' command expects 'on' or 'off' "
826 "as an argument.\n");
828 else if (strcmp (cmd
, "verbose") == 0)
830 if (strcmp (args
, "on") == 0)
832 else if (strcmp (args
, "off") == 0)
835 printf ("The 'sim verbose' command expects 'on' or 'off'"
836 " as an argument.\n");
839 printf ("The 'sim' command expects either 'trace' or 'verbose'"
840 " as a subcommand.\n");
This page took 0.04585 seconds and 4 git commands to generate.