1 /* Remote debugging interface for M32R/SDI.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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];
235 return send_data (buf
, 1);
239 send_one_arg_cmd (unsigned char cmd
, unsigned char arg1
)
241 unsigned char buf
[2];
244 return send_data (buf
, 2);
248 send_two_arg_cmd (unsigned char cmd
, unsigned char arg1
, unsigned long arg2
)
250 unsigned char buf
[6];
253 store_long_parameter (buf
+ 2, arg2
);
254 return send_data (buf
, 6);
258 send_three_arg_cmd (unsigned char cmd
, unsigned long arg1
, unsigned long arg2
,
261 unsigned char buf
[13];
263 store_long_parameter (buf
+ 1, arg1
);
264 store_long_parameter (buf
+ 5, arg2
);
265 store_long_parameter (buf
+ 9, arg3
);
266 return send_data (buf
, 13);
270 recv_char_data (void)
278 recv_long_data (void)
286 /* Check if MMU is on */
288 check_mmu_status (void)
292 /* Read PC address */
293 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
) == -1)
295 val
= recv_long_data ();
296 if ((val
& 0xc0000000) == 0x80000000)
302 /* Read EVB address */
303 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_EVB
) == -1)
305 val
= recv_long_data ();
306 if ((val
& 0xc0000000) == 0x80000000)
316 /* This is called not only when we first attach, but also when the
317 user types "run" after having attached. */
319 m32r_create_inferior (struct target_ops
*ops
, char *execfile
,
320 char *args
, char **env
, int from_tty
)
325 error (_("Cannot pass arguments to remote STDEBUG process"));
327 if (execfile
== 0 || exec_bfd
== 0)
328 error (_("No executable file specified"));
331 fprintf_unfiltered (gdb_stdlog
, "m32r_create_inferior(%s,%s)\n", execfile
,
334 entry_pt
= bfd_get_start_address (exec_bfd
);
336 /* The "process" (board) is already stopped awaiting our commands, and
337 the program is already downloaded. We just set its PC and go. */
339 clear_proceed_status ();
341 /* Tell wait_for_inferior that we've started a new process. */
342 init_wait_for_inferior ();
344 /* Set up the "saved terminal modes" of the inferior
345 based on what modes we are starting it with. */
346 target_terminal_init ();
348 /* Install inferior's terminal modes. */
349 target_terminal_inferior ();
351 regcache_write_pc (get_current_regcache (), entry_pt
);
354 /* Open a connection to a remote debugger.
355 NAME is the filename used for communication. */
358 m32r_open (char *args
, int from_tty
)
360 struct hostent
*host_ent
;
361 struct sockaddr_in server_addr
;
362 char *port_str
, hostname
[256];
368 fprintf_unfiltered (gdb_stdlog
, "m32r_open(%d)\n", from_tty
);
370 target_preopen (from_tty
);
372 push_target (&m32r_ops
);
375 sprintf (hostname
, "localhost:%d", SDIPORT
);
378 port_str
= strchr (args
, ':');
379 if (port_str
== NULL
)
380 sprintf (hostname
, "%s:%d", args
, SDIPORT
);
382 strcpy (hostname
, args
);
385 sdi_desc
= serial_open (hostname
);
387 error (_("Connection refused."));
389 if (get_ack () == -1)
390 error (_("Cannot connect to SDI target."));
392 if (send_cmd (SDI_OPEN
) == -1)
393 error (_("Cannot connect to SDI target."));
395 /* Get maximum number of ib breakpoints */
396 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_BRK
);
397 max_ib_breakpoints
= recv_char_data ();
399 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints
);
401 /* Initialize breakpoints. */
402 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
403 bp_address
[i
] = 0xffffffff;
405 /* Get maximum number of access breaks. */
406 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_ABRK
);
407 max_access_breaks
= recv_char_data ();
409 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks
);
411 /* Initialize access breask. */
412 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
413 ab_address
[i
] = 0x00000000;
417 /* Get the name of chip on target board. */
418 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_NAME
);
419 recv_data (chip_name
, 64);
422 printf_filtered ("Remote %s connected to %s\n", target_shortname
,
426 /* Close out all files and local state before this target loses control. */
429 m32r_close (int quitting
)
432 fprintf_unfiltered (gdb_stdlog
, "m32r_close(%d)\n", quitting
);
436 send_cmd (SDI_CLOSE
);
437 serial_close (sdi_desc
);
441 inferior_ptid
= null_ptid
;
442 delete_thread_silent (remote_m32r_ptid
);
446 /* Tell the remote machine to resume. */
449 m32r_resume (struct target_ops
*ops
,
450 ptid_t ptid
, int step
, enum target_signal sig
)
452 unsigned long pc_addr
, bp_addr
, ab_addr
;
454 unsigned char buf
[13];
460 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(step)\n");
462 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(cont)\n");
467 pc_addr
= regcache_read_pc (get_current_regcache ());
469 fprintf_unfiltered (gdb_stdlog
, "pc <= 0x%lx\n", pc_addr
);
471 /* At pc address there is a parallel instruction with +2 offset,
472 so we have to make it a serial instruction or avoid it. */
473 if (pc_addr
== last_pc_addr
)
475 /* Avoid a parallel nop. */
476 if (last_pc_addr_data
[0] == 0xf0 && last_pc_addr_data
[1] == 0x00)
479 /* Now we can forget this instruction. */
480 last_pc_addr
= 0xffffffff;
482 /* Clear a parallel bit. */
485 buf
[0] = SDI_WRITE_MEMORY
;
486 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
487 store_long_parameter (buf
+ 1, pc_addr
);
489 store_long_parameter (buf
+ 1, pc_addr
- 1);
490 store_long_parameter (buf
+ 5, 1);
491 buf
[9] = last_pc_addr_data
[0] & 0x7f;
497 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
504 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, pc_addr
| 1);
509 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, 0x00000000);
512 if (use_ib_breakpoints
)
513 ib_breakpoints
= max_ib_breakpoints
;
517 /* Set ib breakpoints. */
518 for (i
= 0; i
< ib_breakpoints
; i
++)
520 bp_addr
= bp_address
[i
];
522 if (bp_addr
== 0xffffffff)
526 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
527 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
530 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
533 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8080 + 4 * i
, 4, bp_addr
);
536 /* Set dbt breakpoints. */
537 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
539 bp_addr
= bp_address
[i
];
541 if (bp_addr
== 0xffffffff)
545 bp_addr
&= 0x7fffffff;
547 /* Write DBT instruction. */
548 buf
[0] = SDI_WRITE_MEMORY
;
549 store_long_parameter (buf
+ 1, (bp_addr
& 0xfffffffc));
550 store_long_parameter (buf
+ 5, 4);
551 if ((bp_addr
& 2) == 0 && bp_addr
!= (pc_addr
& 0xfffffffc))
553 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
555 buf
[9] = dbt_bp_entry
[0];
556 buf
[10] = dbt_bp_entry
[1];
557 buf
[11] = dbt_bp_entry
[2];
558 buf
[12] = dbt_bp_entry
[3];
562 buf
[9] = dbt_bp_entry
[3];
563 buf
[10] = dbt_bp_entry
[2];
564 buf
[11] = dbt_bp_entry
[1];
565 buf
[12] = dbt_bp_entry
[0];
570 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
572 if ((bp_addr
& 2) == 0)
574 buf
[9] = dbt_bp_entry
[0];
575 buf
[10] = dbt_bp_entry
[1];
576 buf
[11] = bp_data
[i
][2] & 0x7f;
577 buf
[12] = bp_data
[i
][3];
581 buf
[9] = bp_data
[i
][0];
582 buf
[10] = bp_data
[i
][1];
583 buf
[11] = dbt_bp_entry
[0];
584 buf
[12] = dbt_bp_entry
[1];
589 if ((bp_addr
& 2) == 0)
591 buf
[9] = bp_data
[i
][0];
592 buf
[10] = bp_data
[i
][1] & 0x7f;
593 buf
[11] = dbt_bp_entry
[1];
594 buf
[12] = dbt_bp_entry
[0];
598 buf
[9] = dbt_bp_entry
[1];
599 buf
[10] = dbt_bp_entry
[0];
600 buf
[11] = bp_data
[i
][2];
601 buf
[12] = bp_data
[i
][3];
608 /* Set access breaks. */
609 for (i
= 0; i
< max_access_breaks
; i
++)
611 ab_addr
= ab_address
[i
];
613 if (ab_addr
== 0x00000000)
617 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
621 case 0: /* write watch */
622 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
625 case 1: /* read watch */
626 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
629 case 2: /* access watch */
630 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
639 case 0: /* write watch */
640 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
643 case 1: /* read watch */
644 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
647 case 2: /* access watch */
648 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
655 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8180 + 4 * i
, 4, ab_addr
);
658 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8200 + 4 * i
, 4,
662 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8280 + 4 * i
, 4,
666 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8300 + 4 * i
, 4,
670 /* Resume program. */
671 send_cmd (SDI_EXEC_CPU
);
673 /* Without this, some commands which require an active target (such as kill)
674 won't work. This variable serves (at least) double duty as both the pid
675 of the target process (if it has such), and as a flag indicating that a
676 target is active. These functions should be split out into seperate
677 variables, especially since GDB will someday have a notion of debugging
678 several processes. */
679 inferior_ptid
= remote_m32r_ptid
;
680 add_thread_silent (remote_m32r_ptid
);
685 /* Wait until the remote machine stops, then return,
686 storing status in STATUS just as `wait' would. */
689 gdb_cntrl_c (int signo
)
692 fprintf_unfiltered (gdb_stdlog
, "interrupt\n");
697 m32r_wait (struct target_ops
*ops
,
698 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
700 static RETSIGTYPE (*prev_sigint
) ();
701 unsigned long bp_addr
, pc_addr
;
704 unsigned char buf
[13];
709 fprintf_unfiltered (gdb_stdlog
, "m32r_wait()\n");
711 status
->kind
= TARGET_WAITKIND_EXITED
;
712 status
->value
.sig
= 0;
715 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
718 buf
[0] = SDI_WAIT_FOR_READY
;
719 if (serial_write (sdi_desc
, buf
, 1) != 0)
720 error (_("Remote connection closed"));
724 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
726 error (_("Remote connection closed"));
728 if (c
== '-') /* error */
730 status
->kind
= TARGET_WAITKIND_STOPPED
;
731 status
->value
.sig
= TARGET_SIGNAL_HUP
;
732 return inferior_ptid
;
734 else if (c
== '+') /* stopped */
738 ret
= serial_write (sdi_desc
, "!", 1); /* packet to interrupt */
740 ret
= serial_write (sdi_desc
, ".", 1); /* packet to wait */
742 error (_("Remote connection closed"));
745 status
->kind
= TARGET_WAITKIND_STOPPED
;
747 status
->value
.sig
= TARGET_SIGNAL_INT
;
749 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
752 signal (SIGINT
, prev_sigint
);
756 /* Recover parallel bit. */
757 if (last_pc_addr
!= 0xffffffff)
759 buf
[0] = SDI_WRITE_MEMORY
;
760 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
761 store_long_parameter (buf
+ 1, last_pc_addr
);
763 store_long_parameter (buf
+ 1, last_pc_addr
- 1);
764 store_long_parameter (buf
+ 5, 1);
765 buf
[9] = last_pc_addr_data
[0];
767 last_pc_addr
= 0xffffffff;
770 if (use_ib_breakpoints
)
771 ib_breakpoints
= max_ib_breakpoints
;
775 /* Set back pc by 2 if m32r is stopped with dbt. */
776 last_pc_addr
= 0xffffffff;
777 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
);
778 pc_addr
= recv_long_data () - 2;
779 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
781 if (pc_addr
== bp_address
[i
])
783 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
785 /* If there is a parallel instruction with +2 offset at pc
786 address, we have to take care of it later. */
787 if ((pc_addr
& 0x2) != 0)
789 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
791 if ((bp_data
[i
][2] & 0x80) != 0)
793 last_pc_addr
= pc_addr
;
794 last_pc_addr_data
[0] = bp_data
[i
][2];
795 last_pc_addr_data
[1] = bp_data
[i
][3];
800 if ((bp_data
[i
][1] & 0x80) != 0)
802 last_pc_addr
= pc_addr
;
803 last_pc_addr_data
[0] = bp_data
[i
][1];
804 last_pc_addr_data
[1] = bp_data
[i
][0];
812 /* Remove ib breakpoints. */
813 for (i
= 0; i
< ib_breakpoints
; i
++)
815 if (bp_address
[i
] != 0xffffffff)
816 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
819 /* Remove dbt breakpoints. */
820 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
822 bp_addr
= bp_address
[i
];
823 if (bp_addr
!= 0xffffffff)
826 bp_addr
&= 0x7fffffff;
827 buf
[0] = SDI_WRITE_MEMORY
;
828 store_long_parameter (buf
+ 1, bp_addr
& 0xfffffffc);
829 store_long_parameter (buf
+ 5, 4);
830 buf
[9] = bp_data
[i
][0];
831 buf
[10] = bp_data
[i
][1];
832 buf
[11] = bp_data
[i
][2];
833 buf
[12] = bp_data
[i
][3];
838 /* Remove access breaks. */
839 hit_watchpoint_addr
= 0;
840 for (i
= 0; i
< max_access_breaks
; i
++)
842 if (ab_address
[i
] != 0x00000000)
844 buf
[0] = SDI_READ_MEMORY
;
845 store_long_parameter (buf
+ 1, 0xffff8100 + 4 * i
);
846 store_long_parameter (buf
+ 5, 4);
847 serial_write (sdi_desc
, buf
, 9);
848 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
849 if (c
!= '-' && recv_data (buf
, 4) != -1)
851 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
853 if ((buf
[3] & 0x1) == 0x1)
854 hit_watchpoint_addr
= ab_address
[i
];
858 if ((buf
[0] & 0x1) == 0x1)
859 hit_watchpoint_addr
= ab_address
[i
];
863 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
869 fprintf_unfiltered (gdb_stdlog
, "pc => 0x%lx\n", pc_addr
);
871 return inferior_ptid
;
874 /* Terminate the open connection to the remote debugger.
875 Use this when you want to detach and do something else
878 m32r_detach (struct target_ops
*ops
, char *args
, int from_tty
)
881 fprintf_unfiltered (gdb_stdlog
, "m32r_detach(%d)\n", from_tty
);
883 m32r_resume (ops
, inferior_ptid
, 0, 0);
885 /* calls m32r_close to do the real work */
888 fprintf_unfiltered (gdb_stdlog
, "Ending remote %s debugging\n",
892 /* Return the id of register number REGNO. */
895 get_reg_id (int regno
)
914 /* Fetch register REGNO, or all registers if REGNO is -1.
915 Returns errno value. */
917 m32r_fetch_register (struct target_ops
*ops
,
918 struct regcache
*regcache
, int regno
)
920 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
921 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
922 unsigned long val
, val2
, regid
;
927 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
929 m32r_fetch_register (ops
, regcache
, regno
);
933 char buffer
[MAX_REGISTER_SIZE
];
935 regid
= get_reg_id (regno
);
936 send_one_arg_cmd (SDI_READ_CPU_REG
, regid
);
937 val
= recv_long_data ();
939 if (regid
== SDI_REG_PSW
)
941 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
942 val2
= recv_long_data ();
943 val
= ((0x00cf & val2
) << 8) | ((0xcf00 & val
) >> 8);
947 fprintf_unfiltered (gdb_stdlog
, "m32r_fetch_register(%d,0x%08lx)\n",
950 /* We got the number the register holds, but gdb expects to see a
951 value in the target byte ordering. */
952 store_unsigned_integer (buffer
, 4, byte_order
, val
);
953 regcache_raw_supply (regcache
, regno
, buffer
);
958 /* Store register REGNO, or all if REGNO == 0.
959 Return errno value. */
961 m32r_store_register (struct target_ops
*ops
,
962 struct regcache
*regcache
, int regno
)
965 ULONGEST regval
, tmp
;
970 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
972 m32r_store_register (ops
, regcache
, regno
);
976 regcache_cooked_read_unsigned (regcache
, regno
, ®val
);
977 regid
= get_reg_id (regno
);
979 if (regid
== SDI_REG_PSW
)
981 unsigned long psw
, bbpsw
;
983 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_PSW
);
984 psw
= recv_long_data ();
986 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
987 bbpsw
= recv_long_data ();
989 tmp
= (0x00cf & psw
) | ((0x00cf & regval
) << 8);
990 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PSW
, tmp
);
992 tmp
= (0x0030 & bbpsw
) | ((0xcf00 & regval
) >> 8);
993 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BBPSW
, tmp
);
997 send_two_arg_cmd (SDI_WRITE_CPU_REG
, regid
, regval
);
1001 fprintf_unfiltered (gdb_stdlog
, "m32r_store_register(%d,0x%08lu)\n",
1002 regno
, (unsigned long) regval
);
1006 /* Get ready to modify the registers array. On machines which store
1007 individual registers, this doesn't need to do anything. On machines
1008 which store all the registers in one fell swoop, this makes sure
1009 that registers contains all the registers from the program being
1013 m32r_prepare_to_store (struct regcache
*regcache
)
1015 /* Do nothing, since we can store individual regs */
1017 fprintf_unfiltered (gdb_stdlog
, "m32r_prepare_to_store()\n");
1021 m32r_files_info (struct target_ops
*target
)
1023 char *file
= "nothing";
1027 file
= bfd_get_filename (exec_bfd
);
1028 printf_filtered ("\tAttached to %s running program %s\n",
1033 /* Read/Write memory. */
1035 m32r_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
,
1037 struct mem_attrib
*attrib
, struct target_ops
*target
)
1039 unsigned long taddr
;
1040 unsigned char buf
[0x2000];
1047 if ((taddr
& 0xa0000000) == 0x80000000)
1048 taddr
&= 0x7fffffff;
1054 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%s,%d,write)\n",
1055 paddress (target_gdbarch
, memaddr
), len
);
1057 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%s,%d,read)\n",
1058 paddress (target_gdbarch
, memaddr
), len
);
1063 buf
[0] = SDI_WRITE_MEMORY
;
1064 store_long_parameter (buf
+ 1, taddr
);
1065 store_long_parameter (buf
+ 5, len
);
1068 memcpy (buf
+ 9, myaddr
, len
);
1069 ret
= send_data (buf
, len
+ 9) - 9;
1073 if (serial_write (sdi_desc
, buf
, 9) != 0)
1076 fprintf_unfiltered (gdb_stdlog
,
1077 "m32r_xfer_memory() failed\n");
1080 ret
= send_data (myaddr
, len
);
1085 buf
[0] = SDI_READ_MEMORY
;
1086 store_long_parameter (buf
+ 1, taddr
);
1087 store_long_parameter (buf
+ 5, len
);
1088 if (serial_write (sdi_desc
, buf
, 9) != 0)
1091 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1095 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1096 if (c
< 0 || c
== '-')
1099 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1103 ret
= recv_data (myaddr
, len
);
1109 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() fails\n");
1117 m32r_kill (struct target_ops
*ops
)
1120 fprintf_unfiltered (gdb_stdlog
, "m32r_kill()\n");
1122 inferior_ptid
= null_ptid
;
1123 delete_thread_silent (remote_m32r_ptid
);
1128 /* Clean up when a program exits.
1130 The program actually lives on in the remote processor's RAM, and may be
1131 run again without a download. Don't leave it full of breakpoint
1135 m32r_mourn_inferior (struct target_ops
*ops
)
1138 fprintf_unfiltered (gdb_stdlog
, "m32r_mourn_inferior()\n");
1140 remove_breakpoints ();
1141 generic_mourn_inferior ();
1145 m32r_insert_breakpoint (struct gdbarch
*gdbarch
,
1146 struct bp_target_info
*bp_tgt
)
1148 CORE_ADDR addr
= bp_tgt
->placed_address
;
1150 unsigned char buf
[13];
1154 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_breakpoint(%s,...)\n",
1155 paddress (gdbarch
, addr
));
1157 if (use_ib_breakpoints
)
1158 ib_breakpoints
= max_ib_breakpoints
;
1162 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1164 if (bp_address
[i
] == 0xffffffff)
1166 bp_address
[i
] = addr
;
1167 if (i
>= ib_breakpoints
)
1169 buf
[0] = SDI_READ_MEMORY
;
1171 store_long_parameter (buf
+ 1, addr
& 0xfffffffc);
1173 store_long_parameter (buf
+ 1, addr
& 0x7ffffffc);
1174 store_long_parameter (buf
+ 5, 4);
1175 serial_write (sdi_desc
, buf
, 9);
1176 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1178 recv_data (bp_data
[i
], 4);
1184 error (_("Too many breakpoints"));
1189 m32r_remove_breakpoint (struct gdbarch
*gdbarch
,
1190 struct bp_target_info
*bp_tgt
)
1192 CORE_ADDR addr
= bp_tgt
->placed_address
;
1196 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_breakpoint(%s)\n",
1197 paddress (gdbarch
, addr
));
1199 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1201 if (bp_address
[i
] == addr
)
1203 bp_address
[i
] = 0xffffffff;
1212 m32r_load (char *args
, int from_tty
)
1214 struct cleanup
*old_chain
;
1221 struct timeval start_time
, end_time
;
1222 unsigned long data_count
; /* Number of bytes transferred to memory */
1224 static RETSIGTYPE (*prev_sigint
) ();
1226 /* for direct tcp connections, we can do a fast binary download */
1231 while (*args
!= '\000')
1235 while (isspace (*args
))
1240 while ((*args
!= '\000') && !isspace (*args
))
1243 if (*args
!= '\000')
1248 else if (strncmp (arg
, "-quiet", strlen (arg
)) == 0)
1250 else if (strncmp (arg
, "-nostart", strlen (arg
)) == 0)
1253 error (_("Unknown option `%s'"), arg
);
1257 filename
= get_exec_file (1);
1259 pbfd
= bfd_openr (filename
, gnutarget
);
1262 perror_with_name (filename
);
1265 old_chain
= make_cleanup_bfd_close (pbfd
);
1267 if (!bfd_check_format (pbfd
, bfd_object
))
1268 error (_("\"%s\" is not an object file: %s"), filename
,
1269 bfd_errmsg (bfd_get_error ()));
1271 gettimeofday (&start_time
, NULL
);
1275 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
1277 for (section
= pbfd
->sections
; section
; section
= section
->next
)
1279 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
1281 bfd_vma section_address
;
1282 bfd_size_type section_size
;
1286 section_address
= bfd_section_lma (pbfd
, section
);
1287 section_size
= bfd_get_section_size (section
);
1291 if ((section_address
& 0xa0000000) == 0x80000000)
1292 section_address
&= 0x7fffffff;
1296 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1297 bfd_get_section_name (pbfd
, section
),
1298 (unsigned long) section_address
,
1299 (int) section_size
);
1303 data_count
+= section_size
;
1306 while (section_size
> 0)
1308 char unsigned buf
[0x1000 + 9];
1311 count
= min (section_size
, 0x1000);
1313 buf
[0] = SDI_WRITE_MEMORY
;
1314 store_long_parameter (buf
+ 1, section_address
);
1315 store_long_parameter (buf
+ 5, count
);
1317 bfd_get_section_contents (pbfd
, section
, buf
+ 9, fptr
, count
);
1318 if (send_data (buf
, count
+ 9) <= 0)
1319 error (_("Error while downloading %s section."),
1320 bfd_get_section_name (pbfd
, section
));
1324 printf_unfiltered (".");
1327 printf_unfiltered ("\n");
1330 gdb_flush (gdb_stdout
);
1333 section_address
+= count
;
1335 section_size
-= count
;
1341 if (!quiet
&& !interrupted
)
1343 printf_unfiltered ("done.\n");
1344 gdb_flush (gdb_stdout
);
1350 printf_unfiltered ("Interrupted.\n");
1356 signal (SIGINT
, prev_sigint
);
1358 gettimeofday (&end_time
, NULL
);
1360 /* Make the PC point at the start address */
1362 regcache_write_pc (get_current_regcache (),
1363 bfd_get_start_address (exec_bfd
));
1365 inferior_ptid
= null_ptid
; /* No process now */
1366 delete_thread_silent (remote_m32r_ptid
);
1368 /* This is necessary because many things were based on the PC at the time
1369 that we attached to the monitor, which is no longer valid now that we
1370 have loaded new code (and just changed the PC). Another way to do this
1371 might be to call normal_stop, except that the stack may not be valid,
1372 and things would get horribly confused... */
1374 clear_symtab_users ();
1378 entry
= bfd_get_start_address (pbfd
);
1381 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename
,
1382 (unsigned long) entry
);
1385 print_transfer_performance (gdb_stdout
, data_count
, 0, &start_time
,
1388 do_cleanups (old_chain
);
1392 m32r_stop (ptid_t ptid
)
1395 fprintf_unfiltered (gdb_stdlog
, "m32r_stop()\n");
1397 send_cmd (SDI_STOP_CPU
);
1403 /* Tell whether this target can support a hardware breakpoint. CNT
1404 is the number of hardware breakpoints already installed. This
1405 implements the target_can_use_hardware_watchpoint macro. */
1408 m32r_can_use_hw_watchpoint (int type
, int cnt
, int othertype
)
1410 return sdi_desc
!= NULL
&& cnt
< max_access_breaks
;
1413 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1414 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1418 m32r_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
1423 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_watchpoint(%s,%d,%d)\n",
1424 paddress (target_gdbarch
, addr
), len
, type
);
1426 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1428 if (ab_address
[i
] == 0x00000000)
1430 ab_address
[i
] = addr
;
1437 error (_("Too many watchpoints"));
1442 m32r_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
1447 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_watchpoint(%s,%d,%d)\n",
1448 paddress (target_gdbarch
, addr
), len
, type
);
1450 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1452 if (ab_address
[i
] == addr
)
1454 ab_address
[i
] = 0x00000000;
1463 m32r_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
1466 if (hit_watchpoint_addr
!= 0x00000000)
1468 *addr_p
= hit_watchpoint_addr
;
1475 m32r_stopped_by_watchpoint (void)
1478 return m32r_stopped_data_address (¤t_target
, &addr
);
1481 /* Check to see if a thread is still alive. */
1484 m32r_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1486 if (ptid_equal (ptid
, remote_m32r_ptid
))
1487 /* The main task is always alive. */
1493 /* Convert a thread ID to a string. Returns the string in a static
1497 m32r_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1499 static char buf
[64];
1501 if (ptid_equal (remote_m32r_ptid
, ptid
))
1503 xsnprintf (buf
, sizeof buf
, "Thread <main>");
1507 return normal_pid_to_str (ptid
);
1511 sdireset_command (char *args
, int from_tty
)
1514 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1516 send_cmd (SDI_OPEN
);
1518 inferior_ptid
= null_ptid
;
1519 delete_thread_silent (remote_m32r_ptid
);
1524 sdistatus_command (char *args
, int from_tty
)
1526 unsigned char buf
[4096];
1530 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1535 send_cmd (SDI_STATUS
);
1536 for (i
= 0; i
< 4096; i
++)
1538 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1546 printf_filtered ("%s", buf
);
1551 debug_chaos_command (char *args
, int from_tty
)
1553 unsigned char buf
[3];
1555 buf
[0] = SDI_SET_ATTR
;
1556 buf
[1] = SDI_ATTR_CACHE
;
1557 buf
[2] = SDI_CACHE_TYPE_CHAOS
;
1563 use_debug_dma_command (char *args
, int from_tty
)
1565 unsigned char buf
[3];
1567 buf
[0] = SDI_SET_ATTR
;
1568 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1569 buf
[2] = SDI_MEM_ACCESS_DEBUG_DMA
;
1574 use_mon_code_command (char *args
, int from_tty
)
1576 unsigned char buf
[3];
1578 buf
[0] = SDI_SET_ATTR
;
1579 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1580 buf
[2] = SDI_MEM_ACCESS_MON_CODE
;
1586 use_ib_breakpoints_command (char *args
, int from_tty
)
1588 use_ib_breakpoints
= 1;
1592 use_dbt_breakpoints_command (char *args
, int from_tty
)
1594 use_ib_breakpoints
= 0;
1598 m32r_return_one (struct target_ops
*target
)
1603 /* Define the target subroutine names */
1605 struct target_ops m32r_ops
;
1608 init_m32r_ops (void)
1610 m32r_ops
.to_shortname
= "m32rsdi";
1611 m32r_ops
.to_longname
= "Remote M32R debugging over SDI interface";
1612 m32r_ops
.to_doc
= "Use an M32R board using SDI debugging protocol.";
1613 m32r_ops
.to_open
= m32r_open
;
1614 m32r_ops
.to_close
= m32r_close
;
1615 m32r_ops
.to_detach
= m32r_detach
;
1616 m32r_ops
.to_resume
= m32r_resume
;
1617 m32r_ops
.to_wait
= m32r_wait
;
1618 m32r_ops
.to_fetch_registers
= m32r_fetch_register
;
1619 m32r_ops
.to_store_registers
= m32r_store_register
;
1620 m32r_ops
.to_prepare_to_store
= m32r_prepare_to_store
;
1621 m32r_ops
.deprecated_xfer_memory
= m32r_xfer_memory
;
1622 m32r_ops
.to_files_info
= m32r_files_info
;
1623 m32r_ops
.to_insert_breakpoint
= m32r_insert_breakpoint
;
1624 m32r_ops
.to_remove_breakpoint
= m32r_remove_breakpoint
;
1625 m32r_ops
.to_can_use_hw_breakpoint
= m32r_can_use_hw_watchpoint
;
1626 m32r_ops
.to_insert_watchpoint
= m32r_insert_watchpoint
;
1627 m32r_ops
.to_remove_watchpoint
= m32r_remove_watchpoint
;
1628 m32r_ops
.to_stopped_by_watchpoint
= m32r_stopped_by_watchpoint
;
1629 m32r_ops
.to_stopped_data_address
= m32r_stopped_data_address
;
1630 m32r_ops
.to_kill
= m32r_kill
;
1631 m32r_ops
.to_load
= m32r_load
;
1632 m32r_ops
.to_create_inferior
= m32r_create_inferior
;
1633 m32r_ops
.to_mourn_inferior
= m32r_mourn_inferior
;
1634 m32r_ops
.to_stop
= m32r_stop
;
1635 m32r_ops
.to_log_command
= serial_log_command
;
1636 m32r_ops
.to_thread_alive
= m32r_thread_alive
;
1637 m32r_ops
.to_pid_to_str
= m32r_pid_to_str
;
1638 m32r_ops
.to_stratum
= process_stratum
;
1639 m32r_ops
.to_has_all_memory
= m32r_return_one
;
1640 m32r_ops
.to_has_memory
= m32r_return_one
;
1641 m32r_ops
.to_has_stack
= m32r_return_one
;
1642 m32r_ops
.to_has_registers
= m32r_return_one
;
1643 m32r_ops
.to_has_execution
= m32r_return_one
;
1644 m32r_ops
.to_magic
= OPS_MAGIC
;
1648 extern initialize_file_ftype _initialize_remote_m32r
;
1651 _initialize_remote_m32r (void)
1657 /* Initialize breakpoints. */
1658 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1659 bp_address
[i
] = 0xffffffff;
1661 /* Initialize access breaks. */
1662 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1663 ab_address
[i
] = 0x00000000;
1665 add_target (&m32r_ops
);
1667 add_com ("sdireset", class_obscure
, sdireset_command
,
1668 _("Reset SDI connection."));
1670 add_com ("sdistatus", class_obscure
, sdistatus_command
,
1671 _("Show status of SDI connection."));
1673 add_com ("debug_chaos", class_obscure
, debug_chaos_command
,
1674 _("Debug M32R/Chaos."));
1676 add_com ("use_debug_dma", class_obscure
, use_debug_dma_command
,
1677 _("Use debug DMA mem access."));
1678 add_com ("use_mon_code", class_obscure
, use_mon_code_command
,
1679 _("Use mon code mem access."));
1681 add_com ("use_ib_break", class_obscure
, use_ib_breakpoints_command
,
1682 _("Set breakpoints by IB break."));
1683 add_com ("use_dbt_break", class_obscure
, use_dbt_breakpoints_command
,
1684 _("Set breakpoints by dbt."));
1686 /* Yes, 42000 is arbitrary. The only sense out of it, is that it
1688 remote_m32r_ptid
= ptid_build (42000, 0, 42000);