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 2 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, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
31 #include "gdb_string.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
;
93 #define SDI_READ_CPU_REG 4
94 #define SDI_WRITE_CPU_REG 5
95 #define SDI_READ_MEMORY 6
96 #define SDI_WRITE_MEMORY 7
97 #define SDI_EXEC_CPU 8
98 #define SDI_STOP_CPU 9
99 #define SDI_WAIT_FOR_READY 10
100 #define SDI_GET_ATTR 11
101 #define SDI_SET_ATTR 12
102 #define SDI_STATUS 13
105 #define SDI_ATTR_NAME 1
106 #define SDI_ATTR_BRK 2
107 #define SDI_ATTR_ABRK 3
108 #define SDI_ATTR_CACHE 4
109 #define SDI_CACHE_TYPE_M32102 0
110 #define SDI_CACHE_TYPE_CHAOS 1
111 #define SDI_ATTR_MEM_ACCESS 5
112 #define SDI_MEM_ACCESS_DEBUG_DMA 0
113 #define SDI_MEM_ACCESS_MON_CODE 1
126 #define SDI_REG_R10 10
127 #define SDI_REG_R11 11
128 #define SDI_REG_R12 12
129 #define SDI_REG_FP 13
130 #define SDI_REG_LR 14
131 #define SDI_REG_SP 15
132 #define SDI_REG_PSW 16
133 #define SDI_REG_CBR 17
134 #define SDI_REG_SPI 18
135 #define SDI_REG_SPU 19
136 #define SDI_REG_CR4 20
137 #define SDI_REG_EVB 21
138 #define SDI_REG_BPC 22
139 #define SDI_REG_CR7 23
140 #define SDI_REG_BBPSW 24
141 #define SDI_REG_CR9 25
142 #define SDI_REG_CR10 26
143 #define SDI_REG_CR11 27
144 #define SDI_REG_CR12 28
145 #define SDI_REG_WR 29
146 #define SDI_REG_BBPC 30
147 #define SDI_REG_PBP 31
148 #define SDI_REG_ACCH 32
149 #define SDI_REG_ACCL 33
150 #define SDI_REG_ACC1H 34
151 #define SDI_REG_ACC1L 35
154 /* Low level communication functions */
156 /* Check an ack packet from the target */
165 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
170 if (c
!= '+') /* error */
176 /* Send data to the target and check an ack packet */
178 send_data (void *buf
, int len
)
185 if (serial_write (sdi_desc
, buf
, len
) != 0)
188 if (get_ack () == -1)
194 /* Receive data from the target */
196 recv_data (void *buf
, int len
)
206 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
211 ((unsigned char *) buf
)[total
++] = c
;
217 /* Store unsigned long parameter on packet */
219 store_long_parameter (void *buf
, long val
)
222 memcpy (buf
, &val
, 4);
226 send_cmd (unsigned char cmd
)
228 unsigned char buf
[1];
230 return send_data (buf
, 1);
234 send_one_arg_cmd (unsigned char cmd
, unsigned char arg1
)
236 unsigned char buf
[2];
239 return send_data (buf
, 2);
243 send_two_arg_cmd (unsigned char cmd
, unsigned char arg1
, unsigned long arg2
)
245 unsigned char buf
[6];
248 store_long_parameter (buf
+ 2, arg2
);
249 return send_data (buf
, 6);
253 send_three_arg_cmd (unsigned char cmd
, unsigned long arg1
, unsigned long arg2
,
256 unsigned char buf
[13];
258 store_long_parameter (buf
+ 1, arg1
);
259 store_long_parameter (buf
+ 5, arg2
);
260 store_long_parameter (buf
+ 9, arg3
);
261 return send_data (buf
, 13);
265 recv_char_data (void)
273 recv_long_data (void)
281 /* Check if MMU is on */
283 check_mmu_status (void)
287 /* Read PC address */
288 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
) == -1)
290 val
= recv_long_data ();
291 if ((val
& 0xc0000000) == 0x80000000)
297 /* Read EVB address */
298 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_EVB
) == -1)
300 val
= recv_long_data ();
301 if ((val
& 0xc0000000) == 0x80000000)
311 /* This is called not only when we first attach, but also when the
312 user types "run" after having attached. */
314 m32r_create_inferior (char *execfile
, char *args
, char **env
, int from_tty
)
319 error (_("Cannot pass arguments to remote STDEBUG process"));
321 if (execfile
== 0 || exec_bfd
== 0)
322 error (_("No executable file specified"));
325 fprintf_unfiltered (gdb_stdlog
, "m32r_create_inferior(%s,%s)\n", execfile
,
328 entry_pt
= bfd_get_start_address (exec_bfd
);
330 /* The "process" (board) is already stopped awaiting our commands, and
331 the program is already downloaded. We just set its PC and go. */
333 clear_proceed_status ();
335 /* Tell wait_for_inferior that we've started a new process. */
336 init_wait_for_inferior ();
338 /* Set up the "saved terminal modes" of the inferior
339 based on what modes we are starting it with. */
340 target_terminal_init ();
342 /* Install inferior's terminal modes. */
343 target_terminal_inferior ();
348 /* Open a connection to a remote debugger.
349 NAME is the filename used for communication. */
352 m32r_open (char *args
, int from_tty
)
354 struct hostent
*host_ent
;
355 struct sockaddr_in server_addr
;
356 char *port_str
, hostname
[256];
362 fprintf_unfiltered (gdb_stdlog
, "m32r_open(%d)\n", from_tty
);
364 target_preopen (from_tty
);
366 push_target (&m32r_ops
);
369 sprintf (hostname
, "localhost:%d", SDIPORT
);
372 port_str
= strchr (args
, ':');
373 if (port_str
== NULL
)
374 sprintf (hostname
, "%s:%d", args
, SDIPORT
);
376 strcpy (hostname
, args
);
379 sdi_desc
= serial_open (hostname
);
381 error (_("Connection refused."));
383 if (get_ack () == -1)
384 error (_("Cannot connect to SDI target."));
386 if (send_cmd (SDI_OPEN
) == -1)
387 error (_("Cannot connect to SDI target."));
389 /* Get maximum number of ib breakpoints */
390 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_BRK
);
391 max_ib_breakpoints
= recv_char_data ();
393 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints
);
395 /* Initialize breakpoints. */
396 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
397 bp_address
[i
] = 0xffffffff;
399 /* Get maximum number of access breaks. */
400 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_ABRK
);
401 max_access_breaks
= recv_char_data ();
403 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks
);
405 /* Initialize access breask. */
406 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
407 ab_address
[i
] = 0x00000000;
411 /* Get the name of chip on target board. */
412 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_NAME
);
413 recv_data (chip_name
, 64);
416 printf_filtered ("Remote %s connected to %s\n", target_shortname
,
420 /* Close out all files and local state before this target loses control. */
423 m32r_close (int quitting
)
426 fprintf_unfiltered (gdb_stdlog
, "m32r_close(%d)\n", quitting
);
430 send_cmd (SDI_CLOSE
);
431 serial_close (sdi_desc
);
435 inferior_ptid
= null_ptid
;
439 /* Tell the remote machine to resume. */
442 m32r_resume (ptid_t ptid
, int step
, enum target_signal sig
)
444 unsigned long pc_addr
, bp_addr
, ab_addr
;
446 unsigned char buf
[13];
452 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(step)\n");
454 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(cont)\n");
459 pc_addr
= read_pc ();
461 fprintf_unfiltered (gdb_stdlog
, "pc <= 0x%lx\n", pc_addr
);
463 /* At pc address there is a parallel instruction with +2 offset,
464 so we have to make it a serial instruction or avoid it. */
465 if (pc_addr
== last_pc_addr
)
467 /* Avoid a parallel nop. */
468 if (last_pc_addr_data
[0] == 0xf0 && last_pc_addr_data
[1] == 0x00)
471 /* Now we can forget this instruction. */
472 last_pc_addr
= 0xffffffff;
474 /* Clear a parallel bit. */
477 buf
[0] = SDI_WRITE_MEMORY
;
478 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
479 store_long_parameter (buf
+ 1, pc_addr
);
481 store_long_parameter (buf
+ 1, pc_addr
- 1);
482 store_long_parameter (buf
+ 5, 1);
483 buf
[9] = last_pc_addr_data
[0] & 0x7f;
489 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
496 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, pc_addr
| 1);
501 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, 0x00000000);
504 if (use_ib_breakpoints
)
505 ib_breakpoints
= max_ib_breakpoints
;
509 /* Set ib breakpoints. */
510 for (i
= 0; i
< ib_breakpoints
; i
++)
512 bp_addr
= bp_address
[i
];
514 if (bp_addr
== 0xffffffff)
518 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
519 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
522 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
525 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8080 + 4 * i
, 4, bp_addr
);
528 /* Set dbt breakpoints. */
529 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
531 bp_addr
= bp_address
[i
];
533 if (bp_addr
== 0xffffffff)
537 bp_addr
&= 0x7fffffff;
539 /* Write DBT instruction. */
540 buf
[0] = SDI_WRITE_MEMORY
;
541 store_long_parameter (buf
+ 1, (bp_addr
& 0xfffffffc));
542 store_long_parameter (buf
+ 5, 4);
543 if ((bp_addr
& 2) == 0 && bp_addr
!= (pc_addr
& 0xfffffffc))
545 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
547 buf
[9] = dbt_bp_entry
[0];
548 buf
[10] = dbt_bp_entry
[1];
549 buf
[11] = dbt_bp_entry
[2];
550 buf
[12] = dbt_bp_entry
[3];
554 buf
[9] = dbt_bp_entry
[3];
555 buf
[10] = dbt_bp_entry
[2];
556 buf
[11] = dbt_bp_entry
[1];
557 buf
[12] = dbt_bp_entry
[0];
562 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
564 if ((bp_addr
& 2) == 0)
566 buf
[9] = dbt_bp_entry
[0];
567 buf
[10] = dbt_bp_entry
[1];
568 buf
[11] = bp_data
[i
][2] & 0x7f;
569 buf
[12] = bp_data
[i
][3];
573 buf
[9] = bp_data
[i
][0];
574 buf
[10] = bp_data
[i
][1];
575 buf
[11] = dbt_bp_entry
[0];
576 buf
[12] = dbt_bp_entry
[1];
581 if ((bp_addr
& 2) == 0)
583 buf
[9] = bp_data
[i
][0];
584 buf
[10] = bp_data
[i
][1] & 0x7f;
585 buf
[11] = dbt_bp_entry
[1];
586 buf
[12] = dbt_bp_entry
[0];
590 buf
[9] = dbt_bp_entry
[1];
591 buf
[10] = dbt_bp_entry
[0];
592 buf
[11] = bp_data
[i
][2];
593 buf
[12] = bp_data
[i
][3];
600 /* Set access breaks. */
601 for (i
= 0; i
< max_access_breaks
; i
++)
603 ab_addr
= ab_address
[i
];
605 if (ab_addr
== 0x00000000)
609 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
613 case 0: /* write watch */
614 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
617 case 1: /* read watch */
618 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
621 case 2: /* access watch */
622 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
631 case 0: /* write watch */
632 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
635 case 1: /* read watch */
636 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
639 case 2: /* access watch */
640 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
647 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8180 + 4 * i
, 4, ab_addr
);
650 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8200 + 4 * i
, 4,
654 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8280 + 4 * i
, 4,
658 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8300 + 4 * i
, 4,
662 /* Resume program. */
663 send_cmd (SDI_EXEC_CPU
);
665 /* Without this, some commands which require an active target (such as kill)
666 won't work. This variable serves (at least) double duty as both the pid
667 of the target process (if it has such), and as a flag indicating that a
668 target is active. These functions should be split out into seperate
669 variables, especially since GDB will someday have a notion of debugging
670 several processes. */
671 inferior_ptid
= pid_to_ptid (32);
676 /* Wait until the remote machine stops, then return,
677 storing status in STATUS just as `wait' would. */
680 gdb_cntrl_c (int signo
)
683 fprintf_unfiltered (gdb_stdlog
, "interrupt\n");
688 m32r_wait (ptid_t ptid
, struct target_waitstatus
*status
)
690 static RETSIGTYPE (*prev_sigint
) ();
691 unsigned long bp_addr
, pc_addr
;
694 unsigned char buf
[13];
699 fprintf_unfiltered (gdb_stdlog
, "m32r_wait()\n");
701 status
->kind
= TARGET_WAITKIND_EXITED
;
702 status
->value
.sig
= 0;
705 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
708 buf
[0] = SDI_WAIT_FOR_READY
;
709 if (serial_write (sdi_desc
, buf
, 1) != 0)
710 error (_("Remote connection closed"));
714 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
716 error (_("Remote connection closed"));
718 if (c
== '-') /* error */
720 status
->kind
= TARGET_WAITKIND_STOPPED
;
721 status
->value
.sig
= TARGET_SIGNAL_HUP
;
722 return inferior_ptid
;
724 else if (c
== '+') /* stopped */
728 ret
= serial_write (sdi_desc
, "!", 1); /* packet to interrupt */
730 ret
= serial_write (sdi_desc
, ".", 1); /* packet to wait */
732 error (_("Remote connection closed"));
735 status
->kind
= TARGET_WAITKIND_STOPPED
;
737 status
->value
.sig
= TARGET_SIGNAL_INT
;
739 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
742 signal (SIGINT
, prev_sigint
);
746 /* Recover parallel bit. */
747 if (last_pc_addr
!= 0xffffffff)
749 buf
[0] = SDI_WRITE_MEMORY
;
750 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
751 store_long_parameter (buf
+ 1, last_pc_addr
);
753 store_long_parameter (buf
+ 1, last_pc_addr
- 1);
754 store_long_parameter (buf
+ 5, 1);
755 buf
[9] = last_pc_addr_data
[0];
757 last_pc_addr
= 0xffffffff;
760 if (use_ib_breakpoints
)
761 ib_breakpoints
= max_ib_breakpoints
;
765 /* Set back pc by 2 if m32r is stopped with dbt. */
766 last_pc_addr
= 0xffffffff;
767 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
);
768 pc_addr
= recv_long_data () - 2;
769 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
771 if (pc_addr
== bp_address
[i
])
773 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
775 /* If there is a parallel instruction with +2 offset at pc
776 address, we have to take care of it later. */
777 if ((pc_addr
& 0x2) != 0)
779 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
781 if ((bp_data
[i
][2] & 0x80) != 0)
783 last_pc_addr
= pc_addr
;
784 last_pc_addr_data
[0] = bp_data
[i
][2];
785 last_pc_addr_data
[1] = bp_data
[i
][3];
790 if ((bp_data
[i
][1] & 0x80) != 0)
792 last_pc_addr
= pc_addr
;
793 last_pc_addr_data
[0] = bp_data
[i
][1];
794 last_pc_addr_data
[1] = bp_data
[i
][0];
802 /* Remove ib breakpoints. */
803 for (i
= 0; i
< ib_breakpoints
; i
++)
805 if (bp_address
[i
] != 0xffffffff)
806 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
809 /* Remove dbt breakpoints. */
810 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
812 bp_addr
= bp_address
[i
];
813 if (bp_addr
!= 0xffffffff)
816 bp_addr
&= 0x7fffffff;
817 buf
[0] = SDI_WRITE_MEMORY
;
818 store_long_parameter (buf
+ 1, bp_addr
& 0xfffffffc);
819 store_long_parameter (buf
+ 5, 4);
820 buf
[9] = bp_data
[i
][0];
821 buf
[10] = bp_data
[i
][1];
822 buf
[11] = bp_data
[i
][2];
823 buf
[12] = bp_data
[i
][3];
828 /* Remove access breaks. */
829 hit_watchpoint_addr
= 0;
830 for (i
= 0; i
< max_access_breaks
; i
++)
832 if (ab_address
[i
] != 0x00000000)
834 buf
[0] = SDI_READ_MEMORY
;
835 store_long_parameter (buf
+ 1, 0xffff8100 + 4 * i
);
836 store_long_parameter (buf
+ 5, 4);
837 serial_write (sdi_desc
, buf
, 9);
838 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
839 if (c
!= '-' && recv_data (buf
, 4) != -1)
841 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_BIG
)
843 if ((buf
[3] & 0x1) == 0x1)
844 hit_watchpoint_addr
= ab_address
[i
];
848 if ((buf
[0] & 0x1) == 0x1)
849 hit_watchpoint_addr
= ab_address
[i
];
853 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
859 fprintf_unfiltered (gdb_stdlog
, "pc => 0x%lx\n", pc_addr
);
861 return inferior_ptid
;
864 /* Terminate the open connection to the remote debugger.
865 Use this when you want to detach and do something else
868 m32r_detach (char *args
, int from_tty
)
871 fprintf_unfiltered (gdb_stdlog
, "m32r_detach(%d)\n", from_tty
);
873 m32r_resume (inferior_ptid
, 0, 0);
875 /* calls m32r_close to do the real work */
878 fprintf_unfiltered (gdb_stdlog
, "Ending remote %s debugging\n",
882 /* Return the id of register number REGNO. */
885 get_reg_id (int regno
)
904 /* Read the remote registers into the block REGS. */
906 static void m32r_fetch_register (struct regcache
*, int);
909 m32r_fetch_registers (struct regcache
*regcache
)
913 for (regno
= 0; regno
< gdbarch_num_regs (current_gdbarch
); regno
++)
914 m32r_fetch_register (regcache
, regno
);
917 /* Fetch register REGNO, or all registers if REGNO is -1.
918 Returns errno value. */
920 m32r_fetch_register (struct regcache
*regcache
, int regno
)
922 unsigned long val
, val2
, regid
;
925 m32r_fetch_registers (regcache
);
928 char buffer
[MAX_REGISTER_SIZE
];
930 regid
= get_reg_id (regno
);
931 send_one_arg_cmd (SDI_READ_CPU_REG
, regid
);
932 val
= recv_long_data ();
934 if (regid
== SDI_REG_PSW
)
936 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
937 val2
= recv_long_data ();
938 val
= ((0x00cf & val2
) << 8) | ((0xcf00 & val
) >> 8);
942 fprintf_unfiltered (gdb_stdlog
, "m32r_fetch_register(%d,0x%08lx)\n",
945 /* We got the number the register holds, but gdb expects to see a
946 value in the target byte ordering. */
947 store_unsigned_integer (buffer
, 4, val
);
948 regcache_raw_supply (regcache
, regno
, buffer
);
953 /* Store the remote registers from the contents of the block REGS. */
955 static void m32r_store_register (struct regcache
*, int);
958 m32r_store_registers (struct regcache
*regcache
)
962 for (regno
= 0; regno
< gdbarch_num_regs (current_gdbarch
); regno
++)
963 m32r_store_register (regcache
, regno
);
965 registers_changed ();
968 /* Store register REGNO, or all if REGNO == 0.
969 Return errno value. */
971 m32r_store_register (struct regcache
*regcache
, int regno
)
974 ULONGEST regval
, tmp
;
977 m32r_store_registers (regcache
);
980 regcache_cooked_read_unsigned (regcache
, regno
, ®val
);
981 regid
= get_reg_id (regno
);
983 if (regid
== SDI_REG_PSW
)
985 unsigned long psw
, bbpsw
;
987 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_PSW
);
988 psw
= recv_long_data ();
990 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
991 bbpsw
= recv_long_data ();
993 tmp
= (0x00cf & psw
) | ((0x00cf & regval
) << 8);
994 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PSW
, tmp
);
996 tmp
= (0x0030 & bbpsw
) | ((0xcf00 & regval
) >> 8);
997 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BBPSW
, tmp
);
1001 send_two_arg_cmd (SDI_WRITE_CPU_REG
, regid
, regval
);
1005 fprintf_unfiltered (gdb_stdlog
, "m32r_store_register(%d,0x%08lu)\n",
1006 regno
, (unsigned long) regval
);
1010 /* Get ready to modify the registers array. On machines which store
1011 individual registers, this doesn't need to do anything. On machines
1012 which store all the registers in one fell swoop, this makes sure
1013 that registers contains all the registers from the program being
1017 m32r_prepare_to_store (struct regcache
*regcache
)
1019 /* Do nothing, since we can store individual regs */
1021 fprintf_unfiltered (gdb_stdlog
, "m32r_prepare_to_store()\n");
1025 m32r_files_info (struct target_ops
*target
)
1027 char *file
= "nothing";
1031 file
= bfd_get_filename (exec_bfd
);
1032 printf_filtered ("\tAttached to %s running program %s\n",
1037 /* Read/Write memory. */
1039 m32r_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
,
1041 struct mem_attrib
*attrib
, struct target_ops
*target
)
1043 unsigned long taddr
;
1044 unsigned char buf
[0x2000];
1051 if ((taddr
& 0xa0000000) == 0x80000000)
1052 taddr
&= 0x7fffffff;
1058 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%08lx,%d,write)\n",
1061 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%08lx,%d,read)\n",
1067 buf
[0] = SDI_WRITE_MEMORY
;
1068 store_long_parameter (buf
+ 1, taddr
);
1069 store_long_parameter (buf
+ 5, len
);
1072 memcpy (buf
+ 9, myaddr
, len
);
1073 ret
= send_data (buf
, len
+ 9) - 9;
1077 if (serial_write (sdi_desc
, buf
, 9) != 0)
1080 fprintf_unfiltered (gdb_stdlog
,
1081 "m32r_xfer_memory() failed\n");
1084 ret
= send_data (myaddr
, len
);
1089 buf
[0] = SDI_READ_MEMORY
;
1090 store_long_parameter (buf
+ 1, taddr
);
1091 store_long_parameter (buf
+ 5, len
);
1092 if (serial_write (sdi_desc
, buf
, 9) != 0)
1095 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1099 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1100 if (c
< 0 || c
== '-')
1103 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1107 ret
= recv_data (myaddr
, len
);
1113 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() fails\n");
1124 fprintf_unfiltered (gdb_stdlog
, "m32r_kill()\n");
1126 inferior_ptid
= null_ptid
;
1131 /* Clean up when a program exits.
1133 The program actually lives on in the remote processor's RAM, and may be
1134 run again without a download. Don't leave it full of breakpoint
1138 m32r_mourn_inferior (void)
1141 fprintf_unfiltered (gdb_stdlog
, "m32r_mourn_inferior()\n");
1143 remove_breakpoints ();
1144 generic_mourn_inferior ();
1148 m32r_insert_breakpoint (struct bp_target_info
*bp_tgt
)
1150 CORE_ADDR addr
= bp_tgt
->placed_address
;
1152 unsigned char buf
[13];
1156 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_breakpoint(%08lx,...)\n",
1159 if (use_ib_breakpoints
)
1160 ib_breakpoints
= max_ib_breakpoints
;
1164 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1166 if (bp_address
[i
] == 0xffffffff)
1168 bp_address
[i
] = addr
;
1169 if (i
>= ib_breakpoints
)
1171 buf
[0] = SDI_READ_MEMORY
;
1173 store_long_parameter (buf
+ 1, addr
& 0xfffffffc);
1175 store_long_parameter (buf
+ 1, addr
& 0x7ffffffc);
1176 store_long_parameter (buf
+ 5, 4);
1177 serial_write (sdi_desc
, buf
, 9);
1178 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1180 recv_data (bp_data
[i
], 4);
1186 error (_("Too many breakpoints"));
1191 m32r_remove_breakpoint (struct bp_target_info
*bp_tgt
)
1193 CORE_ADDR addr
= bp_tgt
->placed_address
;
1197 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_breakpoint(%08lx)\n",
1200 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1202 if (bp_address
[i
] == addr
)
1204 bp_address
[i
] = 0xffffffff;
1213 m32r_load (char *args
, int from_tty
)
1215 struct cleanup
*old_chain
;
1222 struct timeval start_time
, end_time
;
1223 unsigned long data_count
; /* Number of bytes transferred to memory */
1225 static RETSIGTYPE (*prev_sigint
) ();
1227 /* for direct tcp connections, we can do a fast binary download */
1232 while (*args
!= '\000')
1236 while (isspace (*args
))
1241 while ((*args
!= '\000') && !isspace (*args
))
1244 if (*args
!= '\000')
1249 else if (strncmp (arg
, "-quiet", strlen (arg
)) == 0)
1251 else if (strncmp (arg
, "-nostart", strlen (arg
)) == 0)
1254 error (_("Unknown option `%s'"), arg
);
1258 filename
= get_exec_file (1);
1260 pbfd
= bfd_openr (filename
, gnutarget
);
1263 perror_with_name (filename
);
1266 old_chain
= make_cleanup_bfd_close (pbfd
);
1268 if (!bfd_check_format (pbfd
, bfd_object
))
1269 error (_("\"%s\" is not an object file: %s"), filename
,
1270 bfd_errmsg (bfd_get_error ()));
1272 gettimeofday (&start_time
, NULL
);
1276 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
1278 for (section
= pbfd
->sections
; section
; section
= section
->next
)
1280 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
1282 bfd_vma section_address
;
1283 bfd_size_type section_size
;
1287 section_address
= bfd_section_lma (pbfd
, section
);
1288 section_size
= bfd_get_section_size (section
);
1292 if ((section_address
& 0xa0000000) == 0x80000000)
1293 section_address
&= 0x7fffffff;
1297 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1298 bfd_get_section_name (pbfd
, section
),
1299 section_address
, (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 write_pc (bfd_get_start_address (exec_bfd
));
1364 inferior_ptid
= null_ptid
; /* No process now */
1366 /* This is necessary because many things were based on the PC at the time
1367 that we attached to the monitor, which is no longer valid now that we
1368 have loaded new code (and just changed the PC). Another way to do this
1369 might be to call normal_stop, except that the stack may not be valid,
1370 and things would get horribly confused... */
1372 clear_symtab_users ();
1376 entry
= bfd_get_start_address (pbfd
);
1379 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename
, entry
);
1382 print_transfer_performance (gdb_stdout
, data_count
, 0, &start_time
,
1385 do_cleanups (old_chain
);
1392 fprintf_unfiltered (gdb_stdlog
, "m32r_stop()\n");
1394 send_cmd (SDI_STOP_CPU
);
1400 /* Tell whether this target can support a hardware breakpoint. CNT
1401 is the number of hardware breakpoints already installed. This
1402 implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro. */
1405 m32r_can_use_hw_watchpoint (int type
, int cnt
, int othertype
)
1407 return sdi_desc
!= NULL
&& cnt
< max_access_breaks
;
1410 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1411 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1415 m32r_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
1420 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_watchpoint(%08lx,%d,%d)\n",
1423 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1425 if (ab_address
[i
] == 0x00000000)
1427 ab_address
[i
] = addr
;
1434 error (_("Too many watchpoints"));
1439 m32r_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
1444 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_watchpoint(%08lx,%d,%d)\n",
1447 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1449 if (ab_address
[i
] == addr
)
1451 ab_address
[i
] = 0x00000000;
1460 m32r_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
1463 if (hit_watchpoint_addr
!= 0x00000000)
1465 *addr_p
= hit_watchpoint_addr
;
1472 m32r_stopped_by_watchpoint (void)
1475 return m32r_stopped_data_address (¤t_target
, &addr
);
1480 sdireset_command (char *args
, int from_tty
)
1483 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1485 send_cmd (SDI_OPEN
);
1487 inferior_ptid
= null_ptid
;
1492 sdistatus_command (char *args
, int from_tty
)
1494 unsigned char buf
[4096];
1498 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1503 send_cmd (SDI_STATUS
);
1504 for (i
= 0; i
< 4096; i
++)
1506 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1514 printf_filtered ("%s", buf
);
1519 debug_chaos_command (char *args
, int from_tty
)
1521 unsigned char buf
[3];
1523 buf
[0] = SDI_SET_ATTR
;
1524 buf
[1] = SDI_ATTR_CACHE
;
1525 buf
[2] = SDI_CACHE_TYPE_CHAOS
;
1531 use_debug_dma_command (char *args
, int from_tty
)
1533 unsigned char buf
[3];
1535 buf
[0] = SDI_SET_ATTR
;
1536 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1537 buf
[2] = SDI_MEM_ACCESS_DEBUG_DMA
;
1542 use_mon_code_command (char *args
, int from_tty
)
1544 unsigned char buf
[3];
1546 buf
[0] = SDI_SET_ATTR
;
1547 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1548 buf
[2] = SDI_MEM_ACCESS_MON_CODE
;
1554 use_ib_breakpoints_command (char *args
, int from_tty
)
1556 use_ib_breakpoints
= 1;
1560 use_dbt_breakpoints_command (char *args
, int from_tty
)
1562 use_ib_breakpoints
= 0;
1566 /* Define the target subroutine names */
1568 struct target_ops m32r_ops
;
1571 init_m32r_ops (void)
1573 m32r_ops
.to_shortname
= "m32rsdi";
1574 m32r_ops
.to_longname
= "Remote M32R debugging over SDI interface";
1575 m32r_ops
.to_doc
= "Use an M32R board using SDI debugging protocol.";
1576 m32r_ops
.to_open
= m32r_open
;
1577 m32r_ops
.to_close
= m32r_close
;
1578 m32r_ops
.to_detach
= m32r_detach
;
1579 m32r_ops
.to_resume
= m32r_resume
;
1580 m32r_ops
.to_wait
= m32r_wait
;
1581 m32r_ops
.to_fetch_registers
= m32r_fetch_register
;
1582 m32r_ops
.to_store_registers
= m32r_store_register
;
1583 m32r_ops
.to_prepare_to_store
= m32r_prepare_to_store
;
1584 m32r_ops
.deprecated_xfer_memory
= m32r_xfer_memory
;
1585 m32r_ops
.to_files_info
= m32r_files_info
;
1586 m32r_ops
.to_insert_breakpoint
= m32r_insert_breakpoint
;
1587 m32r_ops
.to_remove_breakpoint
= m32r_remove_breakpoint
;
1588 m32r_ops
.to_can_use_hw_breakpoint
= m32r_can_use_hw_watchpoint
;
1589 m32r_ops
.to_insert_watchpoint
= m32r_insert_watchpoint
;
1590 m32r_ops
.to_remove_watchpoint
= m32r_remove_watchpoint
;
1591 m32r_ops
.to_stopped_by_watchpoint
= m32r_stopped_by_watchpoint
;
1592 m32r_ops
.to_stopped_data_address
= m32r_stopped_data_address
;
1593 m32r_ops
.to_kill
= m32r_kill
;
1594 m32r_ops
.to_load
= m32r_load
;
1595 m32r_ops
.to_create_inferior
= m32r_create_inferior
;
1596 m32r_ops
.to_mourn_inferior
= m32r_mourn_inferior
;
1597 m32r_ops
.to_stop
= m32r_stop
;
1598 m32r_ops
.to_stratum
= process_stratum
;
1599 m32r_ops
.to_has_all_memory
= 1;
1600 m32r_ops
.to_has_memory
= 1;
1601 m32r_ops
.to_has_stack
= 1;
1602 m32r_ops
.to_has_registers
= 1;
1603 m32r_ops
.to_has_execution
= 1;
1604 m32r_ops
.to_magic
= OPS_MAGIC
;
1608 extern initialize_file_ftype _initialize_remote_m32r
;
1611 _initialize_remote_m32r (void)
1617 /* Initialize breakpoints. */
1618 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1619 bp_address
[i
] = 0xffffffff;
1621 /* Initialize access breaks. */
1622 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1623 ab_address
[i
] = 0x00000000;
1625 add_target (&m32r_ops
);
1627 add_com ("sdireset", class_obscure
, sdireset_command
,
1628 _("Reset SDI connection."));
1630 add_com ("sdistatus", class_obscure
, sdistatus_command
,
1631 _("Show status of SDI connection."));
1633 add_com ("debug_chaos", class_obscure
, debug_chaos_command
,
1634 _("Debug M32R/Chaos."));
1636 add_com ("use_debug_dma", class_obscure
, use_debug_dma_command
,
1637 _("Use debug DMA mem access."));
1638 add_com ("use_mon_code", class_obscure
, use_mon_code_command
,
1639 _("Use mon code mem access."));
1641 add_com ("use_ib_break", class_obscure
, use_ib_breakpoints_command
,
1642 _("Set breakpoints by IB break."));
1643 add_com ("use_dbt_break", class_obscure
, use_dbt_breakpoints_command
,
1644 _("Set breakpoints by dbt."));