1 /* Remote debugging interface for M32R/SDI.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Contributed by Renesas Technology Co.
7 Written by Kei Sakamoto <sakamoto.kei@renesas.com>.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "gdb_string.h"
31 #include "gdbthread.h"
37 #include <netinet/in.h>
39 #include <sys/types.h>
47 /* Descriptor for I/O to remote machine. */
49 static struct serial
*sdi_desc
= NULL
;
51 #define SDI_TIMEOUT 30
56 static char chip_name
[64];
59 static unsigned long last_pc_addr
= 0xffffffff;
60 static unsigned char last_pc_addr_data
[2];
62 static int mmu_on
= 0;
64 static int use_ib_breakpoints
= 1;
66 #define MAX_BREAKPOINTS 1024
67 static int max_ib_breakpoints
;
68 static unsigned long bp_address
[MAX_BREAKPOINTS
];
69 static unsigned char bp_data
[MAX_BREAKPOINTS
][4];
72 static const unsigned char dbt_bp_entry
[] = {
73 0x10, 0xe0, 0x70, 0x00
76 #define MAX_ACCESS_BREAKS 4
77 static int max_access_breaks
;
78 static unsigned long ab_address
[MAX_ACCESS_BREAKS
];
79 static unsigned int ab_type
[MAX_ACCESS_BREAKS
];
80 static unsigned int ab_size
[MAX_ACCESS_BREAKS
];
81 static CORE_ADDR hit_watchpoint_addr
= 0;
83 static int interrupted
= 0;
85 /* Forward data declarations */
86 extern struct target_ops m32r_ops
;
88 /* This is the ptid we use while we're connected to the remote. Its
89 value is arbitrary, as the target doesn't have a notion of
90 processes or threads, but we need something non-null to place in
92 static ptid_t remote_m32r_ptid
;
98 #define SDI_READ_CPU_REG 4
99 #define SDI_WRITE_CPU_REG 5
100 #define SDI_READ_MEMORY 6
101 #define SDI_WRITE_MEMORY 7
102 #define SDI_EXEC_CPU 8
103 #define SDI_STOP_CPU 9
104 #define SDI_WAIT_FOR_READY 10
105 #define SDI_GET_ATTR 11
106 #define SDI_SET_ATTR 12
107 #define SDI_STATUS 13
110 #define SDI_ATTR_NAME 1
111 #define SDI_ATTR_BRK 2
112 #define SDI_ATTR_ABRK 3
113 #define SDI_ATTR_CACHE 4
114 #define SDI_CACHE_TYPE_M32102 0
115 #define SDI_CACHE_TYPE_CHAOS 1
116 #define SDI_ATTR_MEM_ACCESS 5
117 #define SDI_MEM_ACCESS_DEBUG_DMA 0
118 #define SDI_MEM_ACCESS_MON_CODE 1
131 #define SDI_REG_R10 10
132 #define SDI_REG_R11 11
133 #define SDI_REG_R12 12
134 #define SDI_REG_FP 13
135 #define SDI_REG_LR 14
136 #define SDI_REG_SP 15
137 #define SDI_REG_PSW 16
138 #define SDI_REG_CBR 17
139 #define SDI_REG_SPI 18
140 #define SDI_REG_SPU 19
141 #define SDI_REG_CR4 20
142 #define SDI_REG_EVB 21
143 #define SDI_REG_BPC 22
144 #define SDI_REG_CR7 23
145 #define SDI_REG_BBPSW 24
146 #define SDI_REG_CR9 25
147 #define SDI_REG_CR10 26
148 #define SDI_REG_CR11 27
149 #define SDI_REG_CR12 28
150 #define SDI_REG_WR 29
151 #define SDI_REG_BBPC 30
152 #define SDI_REG_PBP 31
153 #define SDI_REG_ACCH 32
154 #define SDI_REG_ACCL 33
155 #define SDI_REG_ACC1H 34
156 #define SDI_REG_ACC1L 35
159 /* Low level communication functions */
161 /* Check an ack packet from the target */
170 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
175 if (c
!= '+') /* error */
181 /* Send data to the target and check an ack packet */
183 send_data (void *buf
, int len
)
190 if (serial_write (sdi_desc
, buf
, len
) != 0)
193 if (get_ack () == -1)
199 /* Receive data from the target */
201 recv_data (void *buf
, int len
)
211 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
216 ((unsigned char *) buf
)[total
++] = c
;
222 /* Store unsigned long parameter on packet */
224 store_long_parameter (void *buf
, long val
)
227 memcpy (buf
, &val
, 4);
231 send_cmd (unsigned char cmd
)
233 unsigned char buf
[1];
236 return send_data (buf
, 1);
240 send_one_arg_cmd (unsigned char cmd
, unsigned char arg1
)
242 unsigned char buf
[2];
246 return send_data (buf
, 2);
250 send_two_arg_cmd (unsigned char cmd
, unsigned char arg1
, unsigned long arg2
)
252 unsigned char buf
[6];
256 store_long_parameter (buf
+ 2, arg2
);
257 return send_data (buf
, 6);
261 send_three_arg_cmd (unsigned char cmd
, unsigned long arg1
, unsigned long arg2
,
264 unsigned char buf
[13];
267 store_long_parameter (buf
+ 1, arg1
);
268 store_long_parameter (buf
+ 5, arg2
);
269 store_long_parameter (buf
+ 9, arg3
);
270 return send_data (buf
, 13);
274 recv_char_data (void)
283 recv_long_data (void)
292 /* Check if MMU is on */
294 check_mmu_status (void)
298 /* Read PC address */
299 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
) == -1)
301 val
= recv_long_data ();
302 if ((val
& 0xc0000000) == 0x80000000)
308 /* Read EVB address */
309 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_EVB
) == -1)
311 val
= recv_long_data ();
312 if ((val
& 0xc0000000) == 0x80000000)
322 /* This is called not only when we first attach, but also when the
323 user types "run" after having attached. */
325 m32r_create_inferior (struct target_ops
*ops
, char *execfile
,
326 char *args
, char **env
, int from_tty
)
331 error (_("Cannot pass arguments to remote STDEBUG process"));
333 if (execfile
== 0 || exec_bfd
== 0)
334 error (_("No executable file specified"));
337 fprintf_unfiltered (gdb_stdlog
, "m32r_create_inferior(%s,%s)\n", execfile
,
340 entry_pt
= bfd_get_start_address (exec_bfd
);
342 /* The "process" (board) is already stopped awaiting our commands, and
343 the program is already downloaded. We just set its PC and go. */
345 clear_proceed_status ();
347 /* Tell wait_for_inferior that we've started a new process. */
348 init_wait_for_inferior ();
350 /* Set up the "saved terminal modes" of the inferior
351 based on what modes we are starting it with. */
352 target_terminal_init ();
354 /* Install inferior's terminal modes. */
355 target_terminal_inferior ();
357 regcache_write_pc (get_current_regcache (), entry_pt
);
360 /* Open a connection to a remote debugger.
361 NAME is the filename used for communication. */
364 m32r_open (char *args
, int from_tty
)
366 struct hostent
*host_ent
;
367 struct sockaddr_in server_addr
;
368 char *port_str
, hostname
[256];
374 fprintf_unfiltered (gdb_stdlog
, "m32r_open(%d)\n", from_tty
);
376 target_preopen (from_tty
);
378 push_target (&m32r_ops
);
381 sprintf (hostname
, "localhost:%d", SDIPORT
);
384 port_str
= strchr (args
, ':');
385 if (port_str
== NULL
)
386 sprintf (hostname
, "%s:%d", args
, SDIPORT
);
388 strcpy (hostname
, args
);
391 sdi_desc
= serial_open (hostname
);
393 error (_("Connection refused."));
395 if (get_ack () == -1)
396 error (_("Cannot connect to SDI target."));
398 if (send_cmd (SDI_OPEN
) == -1)
399 error (_("Cannot connect to SDI target."));
401 /* Get maximum number of ib breakpoints */
402 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_BRK
);
403 max_ib_breakpoints
= recv_char_data ();
405 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints
);
407 /* Initialize breakpoints. */
408 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
409 bp_address
[i
] = 0xffffffff;
411 /* Get maximum number of access breaks. */
412 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_ABRK
);
413 max_access_breaks
= recv_char_data ();
415 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks
);
417 /* Initialize access breask. */
418 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
419 ab_address
[i
] = 0x00000000;
423 /* Get the name of chip on target board. */
424 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_NAME
);
425 recv_data (chip_name
, 64);
428 printf_filtered ("Remote %s connected to %s\n", target_shortname
,
432 /* Close out all files and local state before this target loses control. */
435 m32r_close (int quitting
)
438 fprintf_unfiltered (gdb_stdlog
, "m32r_close(%d)\n", quitting
);
442 send_cmd (SDI_CLOSE
);
443 serial_close (sdi_desc
);
447 inferior_ptid
= null_ptid
;
448 delete_thread_silent (remote_m32r_ptid
);
452 /* Tell the remote machine to resume. */
455 m32r_resume (struct target_ops
*ops
,
456 ptid_t ptid
, int step
, enum target_signal sig
)
458 unsigned long pc_addr
, bp_addr
, ab_addr
;
460 unsigned char buf
[13];
466 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(step)\n");
468 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(cont)\n");
473 pc_addr
= regcache_read_pc (get_current_regcache ());
475 fprintf_unfiltered (gdb_stdlog
, "pc <= 0x%lx\n", pc_addr
);
477 /* At pc address there is a parallel instruction with +2 offset,
478 so we have to make it a serial instruction or avoid it. */
479 if (pc_addr
== last_pc_addr
)
481 /* Avoid a parallel nop. */
482 if (last_pc_addr_data
[0] == 0xf0 && last_pc_addr_data
[1] == 0x00)
485 /* Now we can forget this instruction. */
486 last_pc_addr
= 0xffffffff;
488 /* Clear a parallel bit. */
491 buf
[0] = SDI_WRITE_MEMORY
;
492 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
493 store_long_parameter (buf
+ 1, pc_addr
);
495 store_long_parameter (buf
+ 1, pc_addr
- 1);
496 store_long_parameter (buf
+ 5, 1);
497 buf
[9] = last_pc_addr_data
[0] & 0x7f;
503 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
510 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, pc_addr
| 1);
515 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, 0x00000000);
518 if (use_ib_breakpoints
)
519 ib_breakpoints
= max_ib_breakpoints
;
523 /* Set ib breakpoints. */
524 for (i
= 0; i
< ib_breakpoints
; i
++)
526 bp_addr
= bp_address
[i
];
528 if (bp_addr
== 0xffffffff)
532 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
533 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
536 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
539 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8080 + 4 * i
, 4, bp_addr
);
542 /* Set dbt breakpoints. */
543 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
545 bp_addr
= bp_address
[i
];
547 if (bp_addr
== 0xffffffff)
551 bp_addr
&= 0x7fffffff;
553 /* Write DBT instruction. */
554 buf
[0] = SDI_WRITE_MEMORY
;
555 store_long_parameter (buf
+ 1, (bp_addr
& 0xfffffffc));
556 store_long_parameter (buf
+ 5, 4);
557 if ((bp_addr
& 2) == 0 && bp_addr
!= (pc_addr
& 0xfffffffc))
559 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
561 buf
[9] = dbt_bp_entry
[0];
562 buf
[10] = dbt_bp_entry
[1];
563 buf
[11] = dbt_bp_entry
[2];
564 buf
[12] = dbt_bp_entry
[3];
568 buf
[9] = dbt_bp_entry
[3];
569 buf
[10] = dbt_bp_entry
[2];
570 buf
[11] = dbt_bp_entry
[1];
571 buf
[12] = dbt_bp_entry
[0];
576 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
578 if ((bp_addr
& 2) == 0)
580 buf
[9] = dbt_bp_entry
[0];
581 buf
[10] = dbt_bp_entry
[1];
582 buf
[11] = bp_data
[i
][2] & 0x7f;
583 buf
[12] = bp_data
[i
][3];
587 buf
[9] = bp_data
[i
][0];
588 buf
[10] = bp_data
[i
][1];
589 buf
[11] = dbt_bp_entry
[0];
590 buf
[12] = dbt_bp_entry
[1];
595 if ((bp_addr
& 2) == 0)
597 buf
[9] = bp_data
[i
][0];
598 buf
[10] = bp_data
[i
][1] & 0x7f;
599 buf
[11] = dbt_bp_entry
[1];
600 buf
[12] = dbt_bp_entry
[0];
604 buf
[9] = dbt_bp_entry
[1];
605 buf
[10] = dbt_bp_entry
[0];
606 buf
[11] = bp_data
[i
][2];
607 buf
[12] = bp_data
[i
][3];
614 /* Set access breaks. */
615 for (i
= 0; i
< max_access_breaks
; i
++)
617 ab_addr
= ab_address
[i
];
619 if (ab_addr
== 0x00000000)
623 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
627 case 0: /* write watch */
628 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
631 case 1: /* read watch */
632 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
635 case 2: /* access watch */
636 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
645 case 0: /* write watch */
646 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
649 case 1: /* read watch */
650 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
653 case 2: /* access watch */
654 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
661 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8180 + 4 * i
, 4, ab_addr
);
664 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8200 + 4 * i
, 4,
668 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8280 + 4 * i
, 4,
672 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8300 + 4 * i
, 4,
676 /* Resume program. */
677 send_cmd (SDI_EXEC_CPU
);
679 /* Without this, some commands which require an active target (such as kill)
680 won't work. This variable serves (at least) double duty as both the pid
681 of the target process (if it has such), and as a flag indicating that a
682 target is active. These functions should be split out into seperate
683 variables, especially since GDB will someday have a notion of debugging
684 several processes. */
685 inferior_ptid
= remote_m32r_ptid
;
686 add_thread_silent (remote_m32r_ptid
);
691 /* Wait until the remote machine stops, then return,
692 storing status in STATUS just as `wait' would. */
695 gdb_cntrl_c (int signo
)
698 fprintf_unfiltered (gdb_stdlog
, "interrupt\n");
703 m32r_wait (struct target_ops
*ops
,
704 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
706 static RETSIGTYPE (*prev_sigint
) ();
707 unsigned long bp_addr
, pc_addr
;
710 unsigned char buf
[13];
715 fprintf_unfiltered (gdb_stdlog
, "m32r_wait()\n");
717 status
->kind
= TARGET_WAITKIND_EXITED
;
718 status
->value
.sig
= TARGET_SIGNAL_0
;
721 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
724 buf
[0] = SDI_WAIT_FOR_READY
;
725 if (serial_write (sdi_desc
, buf
, 1) != 0)
726 error (_("Remote connection closed"));
730 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
732 error (_("Remote connection closed"));
734 if (c
== '-') /* error */
736 status
->kind
= TARGET_WAITKIND_STOPPED
;
737 status
->value
.sig
= TARGET_SIGNAL_HUP
;
738 return inferior_ptid
;
740 else if (c
== '+') /* stopped */
744 ret
= serial_write (sdi_desc
, "!", 1); /* packet to interrupt */
746 ret
= serial_write (sdi_desc
, ".", 1); /* packet to wait */
748 error (_("Remote connection closed"));
751 status
->kind
= TARGET_WAITKIND_STOPPED
;
753 status
->value
.sig
= TARGET_SIGNAL_INT
;
755 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
758 signal (SIGINT
, prev_sigint
);
762 /* Recover parallel bit. */
763 if (last_pc_addr
!= 0xffffffff)
765 buf
[0] = SDI_WRITE_MEMORY
;
766 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
767 store_long_parameter (buf
+ 1, last_pc_addr
);
769 store_long_parameter (buf
+ 1, last_pc_addr
- 1);
770 store_long_parameter (buf
+ 5, 1);
771 buf
[9] = last_pc_addr_data
[0];
773 last_pc_addr
= 0xffffffff;
776 if (use_ib_breakpoints
)
777 ib_breakpoints
= max_ib_breakpoints
;
781 /* Set back pc by 2 if m32r is stopped with dbt. */
782 last_pc_addr
= 0xffffffff;
783 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
);
784 pc_addr
= recv_long_data () - 2;
785 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
787 if (pc_addr
== bp_address
[i
])
789 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
791 /* If there is a parallel instruction with +2 offset at pc
792 address, we have to take care of it later. */
793 if ((pc_addr
& 0x2) != 0)
795 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
797 if ((bp_data
[i
][2] & 0x80) != 0)
799 last_pc_addr
= pc_addr
;
800 last_pc_addr_data
[0] = bp_data
[i
][2];
801 last_pc_addr_data
[1] = bp_data
[i
][3];
806 if ((bp_data
[i
][1] & 0x80) != 0)
808 last_pc_addr
= pc_addr
;
809 last_pc_addr_data
[0] = bp_data
[i
][1];
810 last_pc_addr_data
[1] = bp_data
[i
][0];
818 /* Remove ib breakpoints. */
819 for (i
= 0; i
< ib_breakpoints
; i
++)
821 if (bp_address
[i
] != 0xffffffff)
822 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
825 /* Remove dbt breakpoints. */
826 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
828 bp_addr
= bp_address
[i
];
829 if (bp_addr
!= 0xffffffff)
832 bp_addr
&= 0x7fffffff;
833 buf
[0] = SDI_WRITE_MEMORY
;
834 store_long_parameter (buf
+ 1, bp_addr
& 0xfffffffc);
835 store_long_parameter (buf
+ 5, 4);
836 buf
[9] = bp_data
[i
][0];
837 buf
[10] = bp_data
[i
][1];
838 buf
[11] = bp_data
[i
][2];
839 buf
[12] = bp_data
[i
][3];
844 /* Remove access breaks. */
845 hit_watchpoint_addr
= 0;
846 for (i
= 0; i
< max_access_breaks
; i
++)
848 if (ab_address
[i
] != 0x00000000)
850 buf
[0] = SDI_READ_MEMORY
;
851 store_long_parameter (buf
+ 1, 0xffff8100 + 4 * i
);
852 store_long_parameter (buf
+ 5, 4);
853 serial_write (sdi_desc
, buf
, 9);
854 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
855 if (c
!= '-' && recv_data (buf
, 4) != -1)
857 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
859 if ((buf
[3] & 0x1) == 0x1)
860 hit_watchpoint_addr
= ab_address
[i
];
864 if ((buf
[0] & 0x1) == 0x1)
865 hit_watchpoint_addr
= ab_address
[i
];
869 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
875 fprintf_unfiltered (gdb_stdlog
, "pc => 0x%lx\n", pc_addr
);
877 return inferior_ptid
;
880 /* Terminate the open connection to the remote debugger.
881 Use this when you want to detach and do something else
884 m32r_detach (struct target_ops
*ops
, char *args
, int from_tty
)
887 fprintf_unfiltered (gdb_stdlog
, "m32r_detach(%d)\n", from_tty
);
889 m32r_resume (ops
, inferior_ptid
, 0, TARGET_SIGNAL_0
);
891 /* calls m32r_close to do the real work */
894 fprintf_unfiltered (gdb_stdlog
, "Ending remote %s debugging\n",
898 /* Return the id of register number REGNO. */
901 get_reg_id (int regno
)
920 /* Fetch register REGNO, or all registers if REGNO is -1.
921 Returns errno value. */
923 m32r_fetch_register (struct target_ops
*ops
,
924 struct regcache
*regcache
, int regno
)
926 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
927 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
928 unsigned long val
, val2
, regid
;
933 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
935 m32r_fetch_register (ops
, regcache
, regno
);
939 char buffer
[MAX_REGISTER_SIZE
];
941 regid
= get_reg_id (regno
);
942 send_one_arg_cmd (SDI_READ_CPU_REG
, regid
);
943 val
= recv_long_data ();
945 if (regid
== SDI_REG_PSW
)
947 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
948 val2
= recv_long_data ();
949 val
= ((0x00cf & val2
) << 8) | ((0xcf00 & val
) >> 8);
953 fprintf_unfiltered (gdb_stdlog
, "m32r_fetch_register(%d,0x%08lx)\n",
956 /* We got the number the register holds, but gdb expects to see a
957 value in the target byte ordering. */
958 store_unsigned_integer (buffer
, 4, byte_order
, val
);
959 regcache_raw_supply (regcache
, regno
, buffer
);
964 /* Store register REGNO, or all if REGNO == 0.
965 Return errno value. */
967 m32r_store_register (struct target_ops
*ops
,
968 struct regcache
*regcache
, int regno
)
971 ULONGEST regval
, tmp
;
976 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
978 m32r_store_register (ops
, regcache
, regno
);
982 regcache_cooked_read_unsigned (regcache
, regno
, ®val
);
983 regid
= get_reg_id (regno
);
985 if (regid
== SDI_REG_PSW
)
987 unsigned long psw
, bbpsw
;
989 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_PSW
);
990 psw
= recv_long_data ();
992 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
993 bbpsw
= recv_long_data ();
995 tmp
= (0x00cf & psw
) | ((0x00cf & regval
) << 8);
996 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PSW
, tmp
);
998 tmp
= (0x0030 & bbpsw
) | ((0xcf00 & regval
) >> 8);
999 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BBPSW
, tmp
);
1003 send_two_arg_cmd (SDI_WRITE_CPU_REG
, regid
, regval
);
1007 fprintf_unfiltered (gdb_stdlog
, "m32r_store_register(%d,0x%08lu)\n",
1008 regno
, (unsigned long) regval
);
1012 /* Get ready to modify the registers array. On machines which store
1013 individual registers, this doesn't need to do anything. On machines
1014 which store all the registers in one fell swoop, this makes sure
1015 that registers contains all the registers from the program being
1019 m32r_prepare_to_store (struct regcache
*regcache
)
1021 /* Do nothing, since we can store individual regs */
1023 fprintf_unfiltered (gdb_stdlog
, "m32r_prepare_to_store()\n");
1027 m32r_files_info (struct target_ops
*target
)
1029 const char *file
= "nothing";
1033 file
= bfd_get_filename (exec_bfd
);
1034 printf_filtered ("\tAttached to %s running program %s\n",
1039 /* Read/Write memory. */
1041 m32r_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
,
1043 struct mem_attrib
*attrib
, struct target_ops
*target
)
1045 unsigned long taddr
;
1046 unsigned char buf
[0x2000];
1053 if ((taddr
& 0xa0000000) == 0x80000000)
1054 taddr
&= 0x7fffffff;
1060 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%s,%d,write)\n",
1061 paddress (target_gdbarch
, memaddr
), len
);
1063 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%s,%d,read)\n",
1064 paddress (target_gdbarch
, memaddr
), len
);
1069 buf
[0] = SDI_WRITE_MEMORY
;
1070 store_long_parameter (buf
+ 1, taddr
);
1071 store_long_parameter (buf
+ 5, len
);
1074 memcpy (buf
+ 9, myaddr
, len
);
1075 ret
= send_data (buf
, len
+ 9) - 9;
1079 if (serial_write (sdi_desc
, buf
, 9) != 0)
1082 fprintf_unfiltered (gdb_stdlog
,
1083 "m32r_xfer_memory() failed\n");
1086 ret
= send_data (myaddr
, len
);
1091 buf
[0] = SDI_READ_MEMORY
;
1092 store_long_parameter (buf
+ 1, taddr
);
1093 store_long_parameter (buf
+ 5, len
);
1094 if (serial_write (sdi_desc
, buf
, 9) != 0)
1097 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1101 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1102 if (c
< 0 || c
== '-')
1105 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1109 ret
= recv_data (myaddr
, len
);
1115 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() fails\n");
1123 m32r_kill (struct target_ops
*ops
)
1126 fprintf_unfiltered (gdb_stdlog
, "m32r_kill()\n");
1128 inferior_ptid
= null_ptid
;
1129 delete_thread_silent (remote_m32r_ptid
);
1134 /* Clean up when a program exits.
1136 The program actually lives on in the remote processor's RAM, and may be
1137 run again without a download. Don't leave it full of breakpoint
1141 m32r_mourn_inferior (struct target_ops
*ops
)
1144 fprintf_unfiltered (gdb_stdlog
, "m32r_mourn_inferior()\n");
1146 remove_breakpoints ();
1147 generic_mourn_inferior ();
1151 m32r_insert_breakpoint (struct gdbarch
*gdbarch
,
1152 struct bp_target_info
*bp_tgt
)
1154 CORE_ADDR addr
= bp_tgt
->placed_address
;
1156 unsigned char buf
[13];
1160 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_breakpoint(%s,...)\n",
1161 paddress (gdbarch
, addr
));
1163 if (use_ib_breakpoints
)
1164 ib_breakpoints
= max_ib_breakpoints
;
1168 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1170 if (bp_address
[i
] == 0xffffffff)
1172 bp_address
[i
] = addr
;
1173 if (i
>= ib_breakpoints
)
1175 buf
[0] = SDI_READ_MEMORY
;
1177 store_long_parameter (buf
+ 1, addr
& 0xfffffffc);
1179 store_long_parameter (buf
+ 1, addr
& 0x7ffffffc);
1180 store_long_parameter (buf
+ 5, 4);
1181 serial_write (sdi_desc
, buf
, 9);
1182 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1184 recv_data (bp_data
[i
], 4);
1190 error (_("Too many breakpoints"));
1195 m32r_remove_breakpoint (struct gdbarch
*gdbarch
,
1196 struct bp_target_info
*bp_tgt
)
1198 CORE_ADDR addr
= bp_tgt
->placed_address
;
1202 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_breakpoint(%s)\n",
1203 paddress (gdbarch
, addr
));
1205 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1207 if (bp_address
[i
] == addr
)
1209 bp_address
[i
] = 0xffffffff;
1218 m32r_load (char *args
, int from_tty
)
1220 struct cleanup
*old_chain
;
1227 struct timeval start_time
, end_time
;
1228 unsigned long data_count
; /* Number of bytes transferred to memory */
1230 static RETSIGTYPE (*prev_sigint
) ();
1232 /* for direct tcp connections, we can do a fast binary download */
1237 while (*args
!= '\000')
1241 while (isspace (*args
))
1246 while ((*args
!= '\000') && !isspace (*args
))
1249 if (*args
!= '\000')
1254 else if (strncmp (arg
, "-quiet", strlen (arg
)) == 0)
1256 else if (strncmp (arg
, "-nostart", strlen (arg
)) == 0)
1259 error (_("Unknown option `%s'"), arg
);
1263 filename
= get_exec_file (1);
1265 pbfd
= bfd_openr (filename
, gnutarget
);
1268 perror_with_name (filename
);
1271 old_chain
= make_cleanup_bfd_close (pbfd
);
1273 if (!bfd_check_format (pbfd
, bfd_object
))
1274 error (_("\"%s\" is not an object file: %s"), filename
,
1275 bfd_errmsg (bfd_get_error ()));
1277 gettimeofday (&start_time
, NULL
);
1281 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
1283 for (section
= pbfd
->sections
; section
; section
= section
->next
)
1285 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
1287 bfd_vma section_address
;
1288 bfd_size_type section_size
;
1292 section_address
= bfd_section_lma (pbfd
, section
);
1293 section_size
= bfd_get_section_size (section
);
1297 if ((section_address
& 0xa0000000) == 0x80000000)
1298 section_address
&= 0x7fffffff;
1302 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1303 bfd_get_section_name (pbfd
, section
),
1304 (unsigned long) section_address
,
1305 (int) section_size
);
1309 data_count
+= section_size
;
1312 while (section_size
> 0)
1314 char unsigned buf
[0x1000 + 9];
1317 count
= min (section_size
, 0x1000);
1319 buf
[0] = SDI_WRITE_MEMORY
;
1320 store_long_parameter (buf
+ 1, section_address
);
1321 store_long_parameter (buf
+ 5, count
);
1323 bfd_get_section_contents (pbfd
, section
, buf
+ 9, fptr
, count
);
1324 if (send_data (buf
, count
+ 9) <= 0)
1325 error (_("Error while downloading %s section."),
1326 bfd_get_section_name (pbfd
, section
));
1330 printf_unfiltered (".");
1333 printf_unfiltered ("\n");
1336 gdb_flush (gdb_stdout
);
1339 section_address
+= count
;
1341 section_size
-= count
;
1347 if (!quiet
&& !interrupted
)
1349 printf_unfiltered ("done.\n");
1350 gdb_flush (gdb_stdout
);
1356 printf_unfiltered ("Interrupted.\n");
1362 signal (SIGINT
, prev_sigint
);
1364 gettimeofday (&end_time
, NULL
);
1366 /* Make the PC point at the start address */
1368 regcache_write_pc (get_current_regcache (),
1369 bfd_get_start_address (exec_bfd
));
1371 inferior_ptid
= null_ptid
; /* No process now */
1372 delete_thread_silent (remote_m32r_ptid
);
1374 /* This is necessary because many things were based on the PC at the time
1375 that we attached to the monitor, which is no longer valid now that we
1376 have loaded new code (and just changed the PC). Another way to do this
1377 might be to call normal_stop, except that the stack may not be valid,
1378 and things would get horribly confused... */
1380 clear_symtab_users (0);
1384 entry
= bfd_get_start_address (pbfd
);
1387 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename
,
1388 (unsigned long) entry
);
1391 print_transfer_performance (gdb_stdout
, data_count
, 0, &start_time
,
1394 do_cleanups (old_chain
);
1398 m32r_stop (ptid_t ptid
)
1401 fprintf_unfiltered (gdb_stdlog
, "m32r_stop()\n");
1403 send_cmd (SDI_STOP_CPU
);
1409 /* Tell whether this target can support a hardware breakpoint. CNT
1410 is the number of hardware breakpoints already installed. This
1411 implements the target_can_use_hardware_watchpoint macro. */
1414 m32r_can_use_hw_watchpoint (int type
, int cnt
, int othertype
)
1416 return sdi_desc
!= NULL
&& cnt
< max_access_breaks
;
1419 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1420 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1424 m32r_insert_watchpoint (CORE_ADDR addr
, int len
, int type
,
1425 struct expression
*cond
)
1430 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_watchpoint(%s,%d,%d)\n",
1431 paddress (target_gdbarch
, addr
), len
, type
);
1433 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1435 if (ab_address
[i
] == 0x00000000)
1437 ab_address
[i
] = addr
;
1444 error (_("Too many watchpoints"));
1449 m32r_remove_watchpoint (CORE_ADDR addr
, int len
, int type
,
1450 struct expression
*cond
)
1455 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_watchpoint(%s,%d,%d)\n",
1456 paddress (target_gdbarch
, addr
), len
, type
);
1458 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1460 if (ab_address
[i
] == addr
)
1462 ab_address
[i
] = 0x00000000;
1471 m32r_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
1475 if (hit_watchpoint_addr
!= 0x00000000)
1477 *addr_p
= hit_watchpoint_addr
;
1484 m32r_stopped_by_watchpoint (void)
1488 return m32r_stopped_data_address (¤t_target
, &addr
);
1491 /* Check to see if a thread is still alive. */
1494 m32r_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1496 if (ptid_equal (ptid
, remote_m32r_ptid
))
1497 /* The main task is always alive. */
1503 /* Convert a thread ID to a string. Returns the string in a static
1507 m32r_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1509 static char buf
[64];
1511 if (ptid_equal (remote_m32r_ptid
, ptid
))
1513 xsnprintf (buf
, sizeof buf
, "Thread <main>");
1517 return normal_pid_to_str (ptid
);
1521 sdireset_command (char *args
, int from_tty
)
1524 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1526 send_cmd (SDI_OPEN
);
1528 inferior_ptid
= null_ptid
;
1529 delete_thread_silent (remote_m32r_ptid
);
1534 sdistatus_command (char *args
, int from_tty
)
1536 unsigned char buf
[4096];
1540 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1545 send_cmd (SDI_STATUS
);
1546 for (i
= 0; i
< 4096; i
++)
1548 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1556 printf_filtered ("%s", buf
);
1561 debug_chaos_command (char *args
, int from_tty
)
1563 unsigned char buf
[3];
1565 buf
[0] = SDI_SET_ATTR
;
1566 buf
[1] = SDI_ATTR_CACHE
;
1567 buf
[2] = SDI_CACHE_TYPE_CHAOS
;
1573 use_debug_dma_command (char *args
, int from_tty
)
1575 unsigned char buf
[3];
1577 buf
[0] = SDI_SET_ATTR
;
1578 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1579 buf
[2] = SDI_MEM_ACCESS_DEBUG_DMA
;
1584 use_mon_code_command (char *args
, int from_tty
)
1586 unsigned char buf
[3];
1588 buf
[0] = SDI_SET_ATTR
;
1589 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1590 buf
[2] = SDI_MEM_ACCESS_MON_CODE
;
1596 use_ib_breakpoints_command (char *args
, int from_tty
)
1598 use_ib_breakpoints
= 1;
1602 use_dbt_breakpoints_command (char *args
, int from_tty
)
1604 use_ib_breakpoints
= 0;
1608 m32r_return_one (struct target_ops
*target
)
1613 /* Define the target subroutine names */
1615 struct target_ops m32r_ops
;
1618 init_m32r_ops (void)
1620 m32r_ops
.to_shortname
= "m32rsdi";
1621 m32r_ops
.to_longname
= "Remote M32R debugging over SDI interface";
1622 m32r_ops
.to_doc
= "Use an M32R board using SDI debugging protocol.";
1623 m32r_ops
.to_open
= m32r_open
;
1624 m32r_ops
.to_close
= m32r_close
;
1625 m32r_ops
.to_detach
= m32r_detach
;
1626 m32r_ops
.to_resume
= m32r_resume
;
1627 m32r_ops
.to_wait
= m32r_wait
;
1628 m32r_ops
.to_fetch_registers
= m32r_fetch_register
;
1629 m32r_ops
.to_store_registers
= m32r_store_register
;
1630 m32r_ops
.to_prepare_to_store
= m32r_prepare_to_store
;
1631 m32r_ops
.deprecated_xfer_memory
= m32r_xfer_memory
;
1632 m32r_ops
.to_files_info
= m32r_files_info
;
1633 m32r_ops
.to_insert_breakpoint
= m32r_insert_breakpoint
;
1634 m32r_ops
.to_remove_breakpoint
= m32r_remove_breakpoint
;
1635 m32r_ops
.to_can_use_hw_breakpoint
= m32r_can_use_hw_watchpoint
;
1636 m32r_ops
.to_insert_watchpoint
= m32r_insert_watchpoint
;
1637 m32r_ops
.to_remove_watchpoint
= m32r_remove_watchpoint
;
1638 m32r_ops
.to_stopped_by_watchpoint
= m32r_stopped_by_watchpoint
;
1639 m32r_ops
.to_stopped_data_address
= m32r_stopped_data_address
;
1640 m32r_ops
.to_kill
= m32r_kill
;
1641 m32r_ops
.to_load
= m32r_load
;
1642 m32r_ops
.to_create_inferior
= m32r_create_inferior
;
1643 m32r_ops
.to_mourn_inferior
= m32r_mourn_inferior
;
1644 m32r_ops
.to_stop
= m32r_stop
;
1645 m32r_ops
.to_log_command
= serial_log_command
;
1646 m32r_ops
.to_thread_alive
= m32r_thread_alive
;
1647 m32r_ops
.to_pid_to_str
= m32r_pid_to_str
;
1648 m32r_ops
.to_stratum
= process_stratum
;
1649 m32r_ops
.to_has_all_memory
= m32r_return_one
;
1650 m32r_ops
.to_has_memory
= m32r_return_one
;
1651 m32r_ops
.to_has_stack
= m32r_return_one
;
1652 m32r_ops
.to_has_registers
= m32r_return_one
;
1653 m32r_ops
.to_has_execution
= m32r_return_one
;
1654 m32r_ops
.to_magic
= OPS_MAGIC
;
1658 extern initialize_file_ftype _initialize_remote_m32r
;
1661 _initialize_remote_m32r (void)
1667 /* Initialize breakpoints. */
1668 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1669 bp_address
[i
] = 0xffffffff;
1671 /* Initialize access breaks. */
1672 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1673 ab_address
[i
] = 0x00000000;
1675 add_target (&m32r_ops
);
1677 add_com ("sdireset", class_obscure
, sdireset_command
,
1678 _("Reset SDI connection."));
1680 add_com ("sdistatus", class_obscure
, sdistatus_command
,
1681 _("Show status of SDI connection."));
1683 add_com ("debug_chaos", class_obscure
, debug_chaos_command
,
1684 _("Debug M32R/Chaos."));
1686 add_com ("use_debug_dma", class_obscure
, use_debug_dma_command
,
1687 _("Use debug DMA mem access."));
1688 add_com ("use_mon_code", class_obscure
, use_mon_code_command
,
1689 _("Use mon code mem access."));
1691 add_com ("use_ib_break", class_obscure
, use_ib_breakpoints_command
,
1692 _("Set breakpoints by IB break."));
1693 add_com ("use_dbt_break", class_obscure
, use_dbt_breakpoints_command
,
1694 _("Set breakpoints by dbt."));
1696 /* Yes, 42000 is arbitrary. The only sense out of it, is that it
1698 remote_m32r_ptid
= ptid_build (42000, 0, 42000);