1 /* Remote debugging interface for M32R/SDI.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 Contributed by Renesas Technology Co.
6 Written by Kei Sakamoto <sakamoto.kei@renesas.com>.
8 This file is part of GDB.
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.
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.
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/>. */
29 #include "gdb_string.h"
35 #include <netinet/in.h>
37 #include <sys/types.h>
45 /* Descriptor for I/O to remote machine. */
47 static struct serial
*sdi_desc
= NULL
;
49 #define SDI_TIMEOUT 30
54 static char chip_name
[64];
57 static unsigned long last_pc_addr
= 0xffffffff;
58 static unsigned char last_pc_addr_data
[2];
60 static int mmu_on
= 0;
62 static int use_ib_breakpoints
= 1;
64 #define MAX_BREAKPOINTS 1024
65 static int max_ib_breakpoints
;
66 static unsigned long bp_address
[MAX_BREAKPOINTS
];
67 static unsigned char bp_data
[MAX_BREAKPOINTS
][4];
70 static const unsigned char dbt_bp_entry
[] = {
71 0x10, 0xe0, 0x70, 0x00
74 #define MAX_ACCESS_BREAKS 4
75 static int max_access_breaks
;
76 static unsigned long ab_address
[MAX_ACCESS_BREAKS
];
77 static unsigned int ab_type
[MAX_ACCESS_BREAKS
];
78 static unsigned int ab_size
[MAX_ACCESS_BREAKS
];
79 static CORE_ADDR hit_watchpoint_addr
= 0;
81 static int interrupted
= 0;
83 /* Forward data declarations */
84 extern struct target_ops m32r_ops
;
91 #define SDI_READ_CPU_REG 4
92 #define SDI_WRITE_CPU_REG 5
93 #define SDI_READ_MEMORY 6
94 #define SDI_WRITE_MEMORY 7
95 #define SDI_EXEC_CPU 8
96 #define SDI_STOP_CPU 9
97 #define SDI_WAIT_FOR_READY 10
98 #define SDI_GET_ATTR 11
99 #define SDI_SET_ATTR 12
100 #define SDI_STATUS 13
103 #define SDI_ATTR_NAME 1
104 #define SDI_ATTR_BRK 2
105 #define SDI_ATTR_ABRK 3
106 #define SDI_ATTR_CACHE 4
107 #define SDI_CACHE_TYPE_M32102 0
108 #define SDI_CACHE_TYPE_CHAOS 1
109 #define SDI_ATTR_MEM_ACCESS 5
110 #define SDI_MEM_ACCESS_DEBUG_DMA 0
111 #define SDI_MEM_ACCESS_MON_CODE 1
124 #define SDI_REG_R10 10
125 #define SDI_REG_R11 11
126 #define SDI_REG_R12 12
127 #define SDI_REG_FP 13
128 #define SDI_REG_LR 14
129 #define SDI_REG_SP 15
130 #define SDI_REG_PSW 16
131 #define SDI_REG_CBR 17
132 #define SDI_REG_SPI 18
133 #define SDI_REG_SPU 19
134 #define SDI_REG_CR4 20
135 #define SDI_REG_EVB 21
136 #define SDI_REG_BPC 22
137 #define SDI_REG_CR7 23
138 #define SDI_REG_BBPSW 24
139 #define SDI_REG_CR9 25
140 #define SDI_REG_CR10 26
141 #define SDI_REG_CR11 27
142 #define SDI_REG_CR12 28
143 #define SDI_REG_WR 29
144 #define SDI_REG_BBPC 30
145 #define SDI_REG_PBP 31
146 #define SDI_REG_ACCH 32
147 #define SDI_REG_ACCL 33
148 #define SDI_REG_ACC1H 34
149 #define SDI_REG_ACC1L 35
152 /* Low level communication functions */
154 /* Check an ack packet from the target */
163 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
168 if (c
!= '+') /* error */
174 /* Send data to the target and check an ack packet */
176 send_data (void *buf
, int len
)
183 if (serial_write (sdi_desc
, buf
, len
) != 0)
186 if (get_ack () == -1)
192 /* Receive data from the target */
194 recv_data (void *buf
, int len
)
204 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
209 ((unsigned char *) buf
)[total
++] = c
;
215 /* Store unsigned long parameter on packet */
217 store_long_parameter (void *buf
, long val
)
220 memcpy (buf
, &val
, 4);
224 send_cmd (unsigned char cmd
)
226 unsigned char buf
[1];
228 return send_data (buf
, 1);
232 send_one_arg_cmd (unsigned char cmd
, unsigned char arg1
)
234 unsigned char buf
[2];
237 return send_data (buf
, 2);
241 send_two_arg_cmd (unsigned char cmd
, unsigned char arg1
, unsigned long arg2
)
243 unsigned char buf
[6];
246 store_long_parameter (buf
+ 2, arg2
);
247 return send_data (buf
, 6);
251 send_three_arg_cmd (unsigned char cmd
, unsigned long arg1
, unsigned long arg2
,
254 unsigned char buf
[13];
256 store_long_parameter (buf
+ 1, arg1
);
257 store_long_parameter (buf
+ 5, arg2
);
258 store_long_parameter (buf
+ 9, arg3
);
259 return send_data (buf
, 13);
263 recv_char_data (void)
271 recv_long_data (void)
279 /* Check if MMU is on */
281 check_mmu_status (void)
285 /* Read PC address */
286 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
) == -1)
288 val
= recv_long_data ();
289 if ((val
& 0xc0000000) == 0x80000000)
295 /* Read EVB address */
296 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_EVB
) == -1)
298 val
= recv_long_data ();
299 if ((val
& 0xc0000000) == 0x80000000)
309 /* This is called not only when we first attach, but also when the
310 user types "run" after having attached. */
312 m32r_create_inferior (char *execfile
, char *args
, char **env
, int from_tty
)
317 error (_("Cannot pass arguments to remote STDEBUG process"));
319 if (execfile
== 0 || exec_bfd
== 0)
320 error (_("No executable file specified"));
323 fprintf_unfiltered (gdb_stdlog
, "m32r_create_inferior(%s,%s)\n", execfile
,
326 entry_pt
= bfd_get_start_address (exec_bfd
);
328 /* The "process" (board) is already stopped awaiting our commands, and
329 the program is already downloaded. We just set its PC and go. */
331 clear_proceed_status ();
333 /* Tell wait_for_inferior that we've started a new process. */
334 init_wait_for_inferior ();
336 /* Set up the "saved terminal modes" of the inferior
337 based on what modes we are starting it with. */
338 target_terminal_init ();
340 /* Install inferior's terminal modes. */
341 target_terminal_inferior ();
346 /* Open a connection to a remote debugger.
347 NAME is the filename used for communication. */
350 m32r_open (char *args
, int from_tty
)
352 struct hostent
*host_ent
;
353 struct sockaddr_in server_addr
;
354 char *port_str
, hostname
[256];
360 fprintf_unfiltered (gdb_stdlog
, "m32r_open(%d)\n", from_tty
);
362 target_preopen (from_tty
);
364 push_target (&m32r_ops
);
367 sprintf (hostname
, "localhost:%d", SDIPORT
);
370 port_str
= strchr (args
, ':');
371 if (port_str
== NULL
)
372 sprintf (hostname
, "%s:%d", args
, SDIPORT
);
374 strcpy (hostname
, args
);
377 sdi_desc
= serial_open (hostname
);
379 error (_("Connection refused."));
381 if (get_ack () == -1)
382 error (_("Cannot connect to SDI target."));
384 if (send_cmd (SDI_OPEN
) == -1)
385 error (_("Cannot connect to SDI target."));
387 /* Get maximum number of ib breakpoints */
388 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_BRK
);
389 max_ib_breakpoints
= recv_char_data ();
391 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints
);
393 /* Initialize breakpoints. */
394 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
395 bp_address
[i
] = 0xffffffff;
397 /* Get maximum number of access breaks. */
398 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_ABRK
);
399 max_access_breaks
= recv_char_data ();
401 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks
);
403 /* Initialize access breask. */
404 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
405 ab_address
[i
] = 0x00000000;
409 /* Get the name of chip on target board. */
410 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_NAME
);
411 recv_data (chip_name
, 64);
414 printf_filtered ("Remote %s connected to %s\n", target_shortname
,
418 /* Close out all files and local state before this target loses control. */
421 m32r_close (int quitting
)
424 fprintf_unfiltered (gdb_stdlog
, "m32r_close(%d)\n", quitting
);
428 send_cmd (SDI_CLOSE
);
429 serial_close (sdi_desc
);
433 inferior_ptid
= null_ptid
;
437 /* Tell the remote machine to resume. */
440 m32r_resume (ptid_t ptid
, int step
, enum target_signal sig
)
442 unsigned long pc_addr
, bp_addr
, ab_addr
;
444 unsigned char buf
[13];
450 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(step)\n");
452 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(cont)\n");
457 pc_addr
= read_pc ();
459 fprintf_unfiltered (gdb_stdlog
, "pc <= 0x%lx\n", pc_addr
);
461 /* At pc address there is a parallel instruction with +2 offset,
462 so we have to make it a serial instruction or avoid it. */
463 if (pc_addr
== last_pc_addr
)
465 /* Avoid a parallel nop. */
466 if (last_pc_addr_data
[0] == 0xf0 && last_pc_addr_data
[1] == 0x00)
469 /* Now we can forget this instruction. */
470 last_pc_addr
= 0xffffffff;
472 /* Clear a parallel bit. */
475 buf
[0] = SDI_WRITE_MEMORY
;
476 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
477 store_long_parameter (buf
+ 1, pc_addr
);
479 store_long_parameter (buf
+ 1, pc_addr
- 1);
480 store_long_parameter (buf
+ 5, 1);
481 buf
[9] = last_pc_addr_data
[0] & 0x7f;
487 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
494 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, pc_addr
| 1);
499 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, 0x00000000);
502 if (use_ib_breakpoints
)
503 ib_breakpoints
= max_ib_breakpoints
;
507 /* Set ib breakpoints. */
508 for (i
= 0; i
< ib_breakpoints
; i
++)
510 bp_addr
= bp_address
[i
];
512 if (bp_addr
== 0xffffffff)
516 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
517 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
520 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
523 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8080 + 4 * i
, 4, bp_addr
);
526 /* Set dbt breakpoints. */
527 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
529 bp_addr
= bp_address
[i
];
531 if (bp_addr
== 0xffffffff)
535 bp_addr
&= 0x7fffffff;
537 /* Write DBT instruction. */
538 buf
[0] = SDI_WRITE_MEMORY
;
539 store_long_parameter (buf
+ 1, (bp_addr
& 0xfffffffc));
540 store_long_parameter (buf
+ 5, 4);
541 if ((bp_addr
& 2) == 0 && bp_addr
!= (pc_addr
& 0xfffffffc))
543 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
545 buf
[9] = dbt_bp_entry
[0];
546 buf
[10] = dbt_bp_entry
[1];
547 buf
[11] = dbt_bp_entry
[2];
548 buf
[12] = dbt_bp_entry
[3];
552 buf
[9] = dbt_bp_entry
[3];
553 buf
[10] = dbt_bp_entry
[2];
554 buf
[11] = dbt_bp_entry
[1];
555 buf
[12] = dbt_bp_entry
[0];
560 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
562 if ((bp_addr
& 2) == 0)
564 buf
[9] = dbt_bp_entry
[0];
565 buf
[10] = dbt_bp_entry
[1];
566 buf
[11] = bp_data
[i
][2] & 0x7f;
567 buf
[12] = bp_data
[i
][3];
571 buf
[9] = bp_data
[i
][0];
572 buf
[10] = bp_data
[i
][1];
573 buf
[11] = dbt_bp_entry
[0];
574 buf
[12] = dbt_bp_entry
[1];
579 if ((bp_addr
& 2) == 0)
581 buf
[9] = bp_data
[i
][0];
582 buf
[10] = bp_data
[i
][1] & 0x7f;
583 buf
[11] = dbt_bp_entry
[1];
584 buf
[12] = dbt_bp_entry
[0];
588 buf
[9] = dbt_bp_entry
[1];
589 buf
[10] = dbt_bp_entry
[0];
590 buf
[11] = bp_data
[i
][2];
591 buf
[12] = bp_data
[i
][3];
598 /* Set access breaks. */
599 for (i
= 0; i
< max_access_breaks
; i
++)
601 ab_addr
= ab_address
[i
];
603 if (ab_addr
== 0x00000000)
607 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
611 case 0: /* write watch */
612 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
615 case 1: /* read watch */
616 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
619 case 2: /* access watch */
620 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
629 case 0: /* write watch */
630 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
633 case 1: /* read watch */
634 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
637 case 2: /* access watch */
638 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
645 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8180 + 4 * i
, 4, ab_addr
);
648 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8200 + 4 * i
, 4,
652 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8280 + 4 * i
, 4,
656 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8300 + 4 * i
, 4,
660 /* Resume program. */
661 send_cmd (SDI_EXEC_CPU
);
663 /* Without this, some commands which require an active target (such as kill)
664 won't work. This variable serves (at least) double duty as both the pid
665 of the target process (if it has such), and as a flag indicating that a
666 target is active. These functions should be split out into seperate
667 variables, especially since GDB will someday have a notion of debugging
668 several processes. */
669 inferior_ptid
= pid_to_ptid (32);
674 /* Wait until the remote machine stops, then return,
675 storing status in STATUS just as `wait' would. */
678 gdb_cntrl_c (int signo
)
681 fprintf_unfiltered (gdb_stdlog
, "interrupt\n");
686 m32r_wait (ptid_t ptid
, struct target_waitstatus
*status
)
688 static RETSIGTYPE (*prev_sigint
) ();
689 unsigned long bp_addr
, pc_addr
;
692 unsigned char buf
[13];
697 fprintf_unfiltered (gdb_stdlog
, "m32r_wait()\n");
699 status
->kind
= TARGET_WAITKIND_EXITED
;
700 status
->value
.sig
= 0;
703 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
706 buf
[0] = SDI_WAIT_FOR_READY
;
707 if (serial_write (sdi_desc
, buf
, 1) != 0)
708 error (_("Remote connection closed"));
712 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
714 error (_("Remote connection closed"));
716 if (c
== '-') /* error */
718 status
->kind
= TARGET_WAITKIND_STOPPED
;
719 status
->value
.sig
= TARGET_SIGNAL_HUP
;
720 return inferior_ptid
;
722 else if (c
== '+') /* stopped */
726 ret
= serial_write (sdi_desc
, "!", 1); /* packet to interrupt */
728 ret
= serial_write (sdi_desc
, ".", 1); /* packet to wait */
730 error (_("Remote connection closed"));
733 status
->kind
= TARGET_WAITKIND_STOPPED
;
735 status
->value
.sig
= TARGET_SIGNAL_INT
;
737 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
740 signal (SIGINT
, prev_sigint
);
744 /* Recover parallel bit. */
745 if (last_pc_addr
!= 0xffffffff)
747 buf
[0] = SDI_WRITE_MEMORY
;
748 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
749 store_long_parameter (buf
+ 1, last_pc_addr
);
751 store_long_parameter (buf
+ 1, last_pc_addr
- 1);
752 store_long_parameter (buf
+ 5, 1);
753 buf
[9] = last_pc_addr_data
[0];
755 last_pc_addr
= 0xffffffff;
758 if (use_ib_breakpoints
)
759 ib_breakpoints
= max_ib_breakpoints
;
763 /* Set back pc by 2 if m32r is stopped with dbt. */
764 last_pc_addr
= 0xffffffff;
765 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
);
766 pc_addr
= recv_long_data () - 2;
767 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
769 if (pc_addr
== bp_address
[i
])
771 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
773 /* If there is a parallel instruction with +2 offset at pc
774 address, we have to take care of it later. */
775 if ((pc_addr
& 0x2) != 0)
777 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
779 if ((bp_data
[i
][2] & 0x80) != 0)
781 last_pc_addr
= pc_addr
;
782 last_pc_addr_data
[0] = bp_data
[i
][2];
783 last_pc_addr_data
[1] = bp_data
[i
][3];
788 if ((bp_data
[i
][1] & 0x80) != 0)
790 last_pc_addr
= pc_addr
;
791 last_pc_addr_data
[0] = bp_data
[i
][1];
792 last_pc_addr_data
[1] = bp_data
[i
][0];
800 /* Remove ib breakpoints. */
801 for (i
= 0; i
< ib_breakpoints
; i
++)
803 if (bp_address
[i
] != 0xffffffff)
804 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
807 /* Remove dbt breakpoints. */
808 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
810 bp_addr
= bp_address
[i
];
811 if (bp_addr
!= 0xffffffff)
814 bp_addr
&= 0x7fffffff;
815 buf
[0] = SDI_WRITE_MEMORY
;
816 store_long_parameter (buf
+ 1, bp_addr
& 0xfffffffc);
817 store_long_parameter (buf
+ 5, 4);
818 buf
[9] = bp_data
[i
][0];
819 buf
[10] = bp_data
[i
][1];
820 buf
[11] = bp_data
[i
][2];
821 buf
[12] = bp_data
[i
][3];
826 /* Remove access breaks. */
827 hit_watchpoint_addr
= 0;
828 for (i
= 0; i
< max_access_breaks
; i
++)
830 if (ab_address
[i
] != 0x00000000)
832 buf
[0] = SDI_READ_MEMORY
;
833 store_long_parameter (buf
+ 1, 0xffff8100 + 4 * i
);
834 store_long_parameter (buf
+ 5, 4);
835 serial_write (sdi_desc
, buf
, 9);
836 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
837 if (c
!= '-' && recv_data (buf
, 4) != -1)
839 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
841 if ((buf
[3] & 0x1) == 0x1)
842 hit_watchpoint_addr
= ab_address
[i
];
846 if ((buf
[0] & 0x1) == 0x1)
847 hit_watchpoint_addr
= ab_address
[i
];
851 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
857 fprintf_unfiltered (gdb_stdlog
, "pc => 0x%lx\n", pc_addr
);
859 return inferior_ptid
;
862 /* Terminate the open connection to the remote debugger.
863 Use this when you want to detach and do something else
866 m32r_detach (char *args
, int from_tty
)
869 fprintf_unfiltered (gdb_stdlog
, "m32r_detach(%d)\n", from_tty
);
871 m32r_resume (inferior_ptid
, 0, 0);
873 /* calls m32r_close to do the real work */
876 fprintf_unfiltered (gdb_stdlog
, "Ending remote %s debugging\n",
880 /* Return the id of register number REGNO. */
883 get_reg_id (int regno
)
902 /* Read the remote registers into the block REGS. */
904 static void m32r_fetch_register (struct regcache
*, int);
907 m32r_fetch_registers (struct regcache
*regcache
)
911 for (regno
= 0; regno
< gdbarch_num_regs (current_gdbarch
); regno
++)
912 m32r_fetch_register (regcache
, regno
);
915 /* Fetch register REGNO, or all registers if REGNO is -1.
916 Returns errno value. */
918 m32r_fetch_register (struct regcache
*regcache
, int regno
)
920 unsigned long val
, val2
, regid
;
923 m32r_fetch_registers (regcache
);
926 char buffer
[MAX_REGISTER_SIZE
];
928 regid
= get_reg_id (regno
);
929 send_one_arg_cmd (SDI_READ_CPU_REG
, regid
);
930 val
= recv_long_data ();
932 if (regid
== SDI_REG_PSW
)
934 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
935 val2
= recv_long_data ();
936 val
= ((0x00cf & val2
) << 8) | ((0xcf00 & val
) >> 8);
940 fprintf_unfiltered (gdb_stdlog
, "m32r_fetch_register(%d,0x%08lx)\n",
943 /* We got the number the register holds, but gdb expects to see a
944 value in the target byte ordering. */
945 store_unsigned_integer (buffer
, 4, val
);
946 regcache_raw_supply (regcache
, regno
, buffer
);
951 /* Store the remote registers from the contents of the block REGS. */
953 static void m32r_store_register (struct regcache
*, int);
956 m32r_store_registers (struct regcache
*regcache
)
960 for (regno
= 0; regno
< gdbarch_num_regs (current_gdbarch
); regno
++)
961 m32r_store_register (regcache
, regno
);
963 registers_changed ();
966 /* Store register REGNO, or all if REGNO == 0.
967 Return errno value. */
969 m32r_store_register (struct regcache
*regcache
, int regno
)
972 ULONGEST regval
, tmp
;
975 m32r_store_registers (regcache
);
978 regcache_cooked_read_unsigned (regcache
, regno
, ®val
);
979 regid
= get_reg_id (regno
);
981 if (regid
== SDI_REG_PSW
)
983 unsigned long psw
, bbpsw
;
985 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_PSW
);
986 psw
= recv_long_data ();
988 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
989 bbpsw
= recv_long_data ();
991 tmp
= (0x00cf & psw
) | ((0x00cf & regval
) << 8);
992 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PSW
, tmp
);
994 tmp
= (0x0030 & bbpsw
) | ((0xcf00 & regval
) >> 8);
995 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BBPSW
, tmp
);
999 send_two_arg_cmd (SDI_WRITE_CPU_REG
, regid
, regval
);
1003 fprintf_unfiltered (gdb_stdlog
, "m32r_store_register(%d,0x%08lu)\n",
1004 regno
, (unsigned long) regval
);
1008 /* Get ready to modify the registers array. On machines which store
1009 individual registers, this doesn't need to do anything. On machines
1010 which store all the registers in one fell swoop, this makes sure
1011 that registers contains all the registers from the program being
1015 m32r_prepare_to_store (struct regcache
*regcache
)
1017 /* Do nothing, since we can store individual regs */
1019 fprintf_unfiltered (gdb_stdlog
, "m32r_prepare_to_store()\n");
1023 m32r_files_info (struct target_ops
*target
)
1025 char *file
= "nothing";
1029 file
= bfd_get_filename (exec_bfd
);
1030 printf_filtered ("\tAttached to %s running program %s\n",
1035 /* Read/Write memory. */
1037 m32r_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
,
1039 struct mem_attrib
*attrib
, struct target_ops
*target
)
1041 unsigned long taddr
;
1042 unsigned char buf
[0x2000];
1049 if ((taddr
& 0xa0000000) == 0x80000000)
1050 taddr
&= 0x7fffffff;
1056 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%08lx,%d,write)\n",
1059 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%08lx,%d,read)\n",
1065 buf
[0] = SDI_WRITE_MEMORY
;
1066 store_long_parameter (buf
+ 1, taddr
);
1067 store_long_parameter (buf
+ 5, len
);
1070 memcpy (buf
+ 9, myaddr
, len
);
1071 ret
= send_data (buf
, len
+ 9) - 9;
1075 if (serial_write (sdi_desc
, buf
, 9) != 0)
1078 fprintf_unfiltered (gdb_stdlog
,
1079 "m32r_xfer_memory() failed\n");
1082 ret
= send_data (myaddr
, len
);
1087 buf
[0] = SDI_READ_MEMORY
;
1088 store_long_parameter (buf
+ 1, taddr
);
1089 store_long_parameter (buf
+ 5, len
);
1090 if (serial_write (sdi_desc
, buf
, 9) != 0)
1093 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1097 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1098 if (c
< 0 || c
== '-')
1101 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1105 ret
= recv_data (myaddr
, len
);
1111 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() fails\n");
1122 fprintf_unfiltered (gdb_stdlog
, "m32r_kill()\n");
1124 inferior_ptid
= null_ptid
;
1129 /* Clean up when a program exits.
1131 The program actually lives on in the remote processor's RAM, and may be
1132 run again without a download. Don't leave it full of breakpoint
1136 m32r_mourn_inferior (void)
1139 fprintf_unfiltered (gdb_stdlog
, "m32r_mourn_inferior()\n");
1141 remove_breakpoints ();
1142 generic_mourn_inferior ();
1146 m32r_insert_breakpoint (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(%08lx,...)\n",
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 bp_target_info
*bp_tgt
)
1191 CORE_ADDR addr
= bp_tgt
->placed_address
;
1195 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_breakpoint(%08lx)\n",
1198 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1200 if (bp_address
[i
] == addr
)
1202 bp_address
[i
] = 0xffffffff;
1211 m32r_load (char *args
, int from_tty
)
1213 struct cleanup
*old_chain
;
1220 struct timeval start_time
, end_time
;
1221 unsigned long data_count
; /* Number of bytes transferred to memory */
1223 static RETSIGTYPE (*prev_sigint
) ();
1225 /* for direct tcp connections, we can do a fast binary download */
1230 while (*args
!= '\000')
1234 while (isspace (*args
))
1239 while ((*args
!= '\000') && !isspace (*args
))
1242 if (*args
!= '\000')
1247 else if (strncmp (arg
, "-quiet", strlen (arg
)) == 0)
1249 else if (strncmp (arg
, "-nostart", strlen (arg
)) == 0)
1252 error (_("Unknown option `%s'"), arg
);
1256 filename
= get_exec_file (1);
1258 pbfd
= bfd_openr (filename
, gnutarget
);
1261 perror_with_name (filename
);
1264 old_chain
= make_cleanup_bfd_close (pbfd
);
1266 if (!bfd_check_format (pbfd
, bfd_object
))
1267 error (_("\"%s\" is not an object file: %s"), filename
,
1268 bfd_errmsg (bfd_get_error ()));
1270 gettimeofday (&start_time
, NULL
);
1274 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
1276 for (section
= pbfd
->sections
; section
; section
= section
->next
)
1278 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
1280 bfd_vma section_address
;
1281 bfd_size_type section_size
;
1285 section_address
= bfd_section_lma (pbfd
, section
);
1286 section_size
= bfd_get_section_size (section
);
1290 if ((section_address
& 0xa0000000) == 0x80000000)
1291 section_address
&= 0x7fffffff;
1295 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1296 bfd_get_section_name (pbfd
, section
),
1297 section_address
, (int) section_size
);
1301 data_count
+= section_size
;
1304 while (section_size
> 0)
1306 char unsigned buf
[0x1000 + 9];
1309 count
= min (section_size
, 0x1000);
1311 buf
[0] = SDI_WRITE_MEMORY
;
1312 store_long_parameter (buf
+ 1, section_address
);
1313 store_long_parameter (buf
+ 5, count
);
1315 bfd_get_section_contents (pbfd
, section
, buf
+ 9, fptr
, count
);
1316 if (send_data (buf
, count
+ 9) <= 0)
1317 error (_("Error while downloading %s section."),
1318 bfd_get_section_name (pbfd
, section
));
1322 printf_unfiltered (".");
1325 printf_unfiltered ("\n");
1328 gdb_flush (gdb_stdout
);
1331 section_address
+= count
;
1333 section_size
-= count
;
1339 if (!quiet
&& !interrupted
)
1341 printf_unfiltered ("done.\n");
1342 gdb_flush (gdb_stdout
);
1348 printf_unfiltered ("Interrupted.\n");
1354 signal (SIGINT
, prev_sigint
);
1356 gettimeofday (&end_time
, NULL
);
1358 /* Make the PC point at the start address */
1360 write_pc (bfd_get_start_address (exec_bfd
));
1362 inferior_ptid
= null_ptid
; /* No process now */
1364 /* This is necessary because many things were based on the PC at the time
1365 that we attached to the monitor, which is no longer valid now that we
1366 have loaded new code (and just changed the PC). Another way to do this
1367 might be to call normal_stop, except that the stack may not be valid,
1368 and things would get horribly confused... */
1370 clear_symtab_users ();
1374 entry
= bfd_get_start_address (pbfd
);
1377 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename
, entry
);
1380 print_transfer_performance (gdb_stdout
, data_count
, 0, &start_time
,
1383 do_cleanups (old_chain
);
1390 fprintf_unfiltered (gdb_stdlog
, "m32r_stop()\n");
1392 send_cmd (SDI_STOP_CPU
);
1398 /* Tell whether this target can support a hardware breakpoint. CNT
1399 is the number of hardware breakpoints already installed. This
1400 implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro. */
1403 m32r_can_use_hw_watchpoint (int type
, int cnt
, int othertype
)
1405 return sdi_desc
!= NULL
&& cnt
< max_access_breaks
;
1408 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1409 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1413 m32r_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
1418 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_watchpoint(%08lx,%d,%d)\n",
1421 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1423 if (ab_address
[i
] == 0x00000000)
1425 ab_address
[i
] = addr
;
1432 error (_("Too many watchpoints"));
1437 m32r_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
1442 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_watchpoint(%08lx,%d,%d)\n",
1445 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1447 if (ab_address
[i
] == addr
)
1449 ab_address
[i
] = 0x00000000;
1458 m32r_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
1461 if (hit_watchpoint_addr
!= 0x00000000)
1463 *addr_p
= hit_watchpoint_addr
;
1470 m32r_stopped_by_watchpoint (void)
1473 return m32r_stopped_data_address (¤t_target
, &addr
);
1478 sdireset_command (char *args
, int from_tty
)
1481 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1483 send_cmd (SDI_OPEN
);
1485 inferior_ptid
= null_ptid
;
1490 sdistatus_command (char *args
, int from_tty
)
1492 unsigned char buf
[4096];
1496 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1501 send_cmd (SDI_STATUS
);
1502 for (i
= 0; i
< 4096; i
++)
1504 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1512 printf_filtered ("%s", buf
);
1517 debug_chaos_command (char *args
, int from_tty
)
1519 unsigned char buf
[3];
1521 buf
[0] = SDI_SET_ATTR
;
1522 buf
[1] = SDI_ATTR_CACHE
;
1523 buf
[2] = SDI_CACHE_TYPE_CHAOS
;
1529 use_debug_dma_command (char *args
, int from_tty
)
1531 unsigned char buf
[3];
1533 buf
[0] = SDI_SET_ATTR
;
1534 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1535 buf
[2] = SDI_MEM_ACCESS_DEBUG_DMA
;
1540 use_mon_code_command (char *args
, int from_tty
)
1542 unsigned char buf
[3];
1544 buf
[0] = SDI_SET_ATTR
;
1545 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1546 buf
[2] = SDI_MEM_ACCESS_MON_CODE
;
1552 use_ib_breakpoints_command (char *args
, int from_tty
)
1554 use_ib_breakpoints
= 1;
1558 use_dbt_breakpoints_command (char *args
, int from_tty
)
1560 use_ib_breakpoints
= 0;
1564 /* Define the target subroutine names */
1566 struct target_ops m32r_ops
;
1569 init_m32r_ops (void)
1571 m32r_ops
.to_shortname
= "m32rsdi";
1572 m32r_ops
.to_longname
= "Remote M32R debugging over SDI interface";
1573 m32r_ops
.to_doc
= "Use an M32R board using SDI debugging protocol.";
1574 m32r_ops
.to_open
= m32r_open
;
1575 m32r_ops
.to_close
= m32r_close
;
1576 m32r_ops
.to_detach
= m32r_detach
;
1577 m32r_ops
.to_resume
= m32r_resume
;
1578 m32r_ops
.to_wait
= m32r_wait
;
1579 m32r_ops
.to_fetch_registers
= m32r_fetch_register
;
1580 m32r_ops
.to_store_registers
= m32r_store_register
;
1581 m32r_ops
.to_prepare_to_store
= m32r_prepare_to_store
;
1582 m32r_ops
.deprecated_xfer_memory
= m32r_xfer_memory
;
1583 m32r_ops
.to_files_info
= m32r_files_info
;
1584 m32r_ops
.to_insert_breakpoint
= m32r_insert_breakpoint
;
1585 m32r_ops
.to_remove_breakpoint
= m32r_remove_breakpoint
;
1586 m32r_ops
.to_can_use_hw_breakpoint
= m32r_can_use_hw_watchpoint
;
1587 m32r_ops
.to_insert_watchpoint
= m32r_insert_watchpoint
;
1588 m32r_ops
.to_remove_watchpoint
= m32r_remove_watchpoint
;
1589 m32r_ops
.to_stopped_by_watchpoint
= m32r_stopped_by_watchpoint
;
1590 m32r_ops
.to_stopped_data_address
= m32r_stopped_data_address
;
1591 m32r_ops
.to_kill
= m32r_kill
;
1592 m32r_ops
.to_load
= m32r_load
;
1593 m32r_ops
.to_create_inferior
= m32r_create_inferior
;
1594 m32r_ops
.to_mourn_inferior
= m32r_mourn_inferior
;
1595 m32r_ops
.to_stop
= m32r_stop
;
1596 m32r_ops
.to_stratum
= process_stratum
;
1597 m32r_ops
.to_has_all_memory
= 1;
1598 m32r_ops
.to_has_memory
= 1;
1599 m32r_ops
.to_has_stack
= 1;
1600 m32r_ops
.to_has_registers
= 1;
1601 m32r_ops
.to_has_execution
= 1;
1602 m32r_ops
.to_magic
= OPS_MAGIC
;
1606 extern initialize_file_ftype _initialize_remote_m32r
;
1609 _initialize_remote_m32r (void)
1615 /* Initialize breakpoints. */
1616 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1617 bp_address
[i
] = 0xffffffff;
1619 /* Initialize access breaks. */
1620 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1621 ab_address
[i
] = 0x00000000;
1623 add_target (&m32r_ops
);
1625 add_com ("sdireset", class_obscure
, sdireset_command
,
1626 _("Reset SDI connection."));
1628 add_com ("sdistatus", class_obscure
, sdistatus_command
,
1629 _("Show status of SDI connection."));
1631 add_com ("debug_chaos", class_obscure
, debug_chaos_command
,
1632 _("Debug M32R/Chaos."));
1634 add_com ("use_debug_dma", class_obscure
, use_debug_dma_command
,
1635 _("Use debug DMA mem access."));
1636 add_com ("use_mon_code", class_obscure
, use_mon_code_command
,
1637 _("Use mon code mem access."));
1639 add_com ("use_ib_break", class_obscure
, use_ib_breakpoints_command
,
1640 _("Set breakpoints by IB break."));
1641 add_com ("use_dbt_break", class_obscure
, use_dbt_breakpoints_command
,
1642 _("Set breakpoints by dbt."));