1 /* Remote debugging interface for M32R/SDI.
3 Copyright (C) 2003-2014 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/>. */
31 #include "gdbthread.h"
37 #include <netinet/in.h>
39 #include <sys/types.h>
43 #include "cli/cli-utils.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 (const void *buf
, int len
)
188 if (serial_write (sdi_desc
, buf
, len
) != 0)
191 if (get_ack () == -1)
197 /* Receive data from the target. */
199 recv_data (void *buf
, int len
)
209 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
214 ((unsigned char *) buf
)[total
++] = c
;
220 /* Store unsigned long parameter on packet. */
222 store_long_parameter (void *buf
, long val
)
225 memcpy (buf
, &val
, 4);
229 send_cmd (unsigned char cmd
)
231 unsigned char buf
[1];
234 return send_data (buf
, 1);
238 send_one_arg_cmd (unsigned char cmd
, unsigned char arg1
)
240 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];
254 store_long_parameter (buf
+ 2, arg2
);
255 return send_data (buf
, 6);
259 send_three_arg_cmd (unsigned char cmd
, unsigned long arg1
, unsigned long arg2
,
262 unsigned char buf
[13];
265 store_long_parameter (buf
+ 1, arg1
);
266 store_long_parameter (buf
+ 5, arg2
);
267 store_long_parameter (buf
+ 9, arg3
);
268 return send_data (buf
, 13);
272 recv_char_data (void)
281 recv_long_data (void)
290 /* Check if MMU is on. */
292 check_mmu_status (void)
296 /* Read PC address. */
297 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
) == -1)
299 val
= recv_long_data ();
300 if ((val
& 0xc0000000) == 0x80000000)
306 /* Read EVB address. */
307 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_EVB
) == -1)
309 val
= recv_long_data ();
310 if ((val
& 0xc0000000) == 0x80000000)
320 /* This is called not only when we first attach, but also when the
321 user types "run" after having attached. */
323 m32r_create_inferior (struct target_ops
*ops
, char *execfile
,
324 char *args
, char **env
, int from_tty
)
329 error (_("Cannot pass arguments to remote STDEBUG process"));
331 if (execfile
== 0 || exec_bfd
== 0)
332 error (_("No executable file specified"));
335 fprintf_unfiltered (gdb_stdlog
, "m32r_create_inferior(%s,%s)\n", execfile
,
338 entry_pt
= bfd_get_start_address (exec_bfd
);
340 /* The "process" (board) is already stopped awaiting our commands, and
341 the program is already downloaded. We just set its PC and go. */
343 clear_proceed_status ();
345 /* Tell wait_for_inferior that we've started a new process. */
346 init_wait_for_inferior ();
348 /* Set up the "saved terminal modes" of the inferior
349 based on what modes we are starting it with. */
350 target_terminal_init ();
352 /* Install inferior's terminal modes. */
353 target_terminal_inferior ();
355 regcache_write_pc (get_current_regcache (), entry_pt
);
358 /* Open a connection to a remote debugger.
359 NAME is the filename used for communication. */
362 m32r_open (char *args
, int from_tty
)
364 struct hostent
*host_ent
;
365 struct sockaddr_in server_addr
;
366 char *port_str
, hostname
[256];
372 fprintf_unfiltered (gdb_stdlog
, "m32r_open(%d)\n", from_tty
);
374 target_preopen (from_tty
);
376 push_target (&m32r_ops
);
379 xsnprintf (hostname
, sizeof (hostname
), "localhost:%d", SDIPORT
);
382 port_str
= strchr (args
, ':');
383 if (port_str
== NULL
)
384 xsnprintf (hostname
, sizeof (hostname
), "%s:%d", args
, SDIPORT
);
386 xsnprintf (hostname
, sizeof (hostname
), "%s", args
);
389 sdi_desc
= serial_open (hostname
);
391 error (_("Connection refused."));
393 if (get_ack () == -1)
394 error (_("Cannot connect to SDI target."));
396 if (send_cmd (SDI_OPEN
) == -1)
397 error (_("Cannot connect to SDI target."));
399 /* Get maximum number of ib breakpoints. */
400 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_BRK
);
401 max_ib_breakpoints
= recv_char_data ();
403 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints
);
405 /* Initialize breakpoints. */
406 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
407 bp_address
[i
] = 0xffffffff;
409 /* Get maximum number of access breaks. */
410 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_ABRK
);
411 max_access_breaks
= recv_char_data ();
413 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks
);
415 /* Initialize access breask. */
416 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
417 ab_address
[i
] = 0x00000000;
421 /* Get the name of chip on target board. */
422 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_NAME
);
423 recv_data (chip_name
, 64);
426 printf_filtered ("Remote %s connected to %s\n", target_shortname
,
430 /* Close out all files and local state before this target loses control. */
433 m32r_close (struct target_ops
*self
)
436 fprintf_unfiltered (gdb_stdlog
, "m32r_close()\n");
440 send_cmd (SDI_CLOSE
);
441 serial_close (sdi_desc
);
445 inferior_ptid
= null_ptid
;
446 delete_thread_silent (remote_m32r_ptid
);
450 /* Tell the remote machine to resume. */
453 m32r_resume (struct target_ops
*ops
,
454 ptid_t ptid
, int step
, enum gdb_signal sig
)
456 unsigned long pc_addr
, bp_addr
, ab_addr
;
458 unsigned char buf
[13];
464 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(step)\n");
466 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(cont)\n");
471 pc_addr
= regcache_read_pc (get_current_regcache ());
473 fprintf_unfiltered (gdb_stdlog
, "pc <= 0x%lx\n", pc_addr
);
475 /* At pc address there is a parallel instruction with +2 offset,
476 so we have to make it a serial instruction or avoid it. */
477 if (pc_addr
== last_pc_addr
)
479 /* Avoid a parallel nop. */
480 if (last_pc_addr_data
[0] == 0xf0 && last_pc_addr_data
[1] == 0x00)
483 /* Now we can forget this instruction. */
484 last_pc_addr
= 0xffffffff;
486 /* Clear a parallel bit. */
489 buf
[0] = SDI_WRITE_MEMORY
;
490 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
491 store_long_parameter (buf
+ 1, pc_addr
);
493 store_long_parameter (buf
+ 1, pc_addr
- 1);
494 store_long_parameter (buf
+ 5, 1);
495 buf
[9] = last_pc_addr_data
[0] & 0x7f;
501 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
508 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, pc_addr
| 1);
513 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, 0x00000000);
516 if (use_ib_breakpoints
)
517 ib_breakpoints
= max_ib_breakpoints
;
521 /* Set ib breakpoints. */
522 for (i
= 0; i
< ib_breakpoints
; i
++)
524 bp_addr
= bp_address
[i
];
526 if (bp_addr
== 0xffffffff)
530 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
531 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
534 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
537 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8080 + 4 * i
, 4, bp_addr
);
540 /* Set dbt breakpoints. */
541 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
543 bp_addr
= bp_address
[i
];
545 if (bp_addr
== 0xffffffff)
549 bp_addr
&= 0x7fffffff;
551 /* Write DBT instruction. */
552 buf
[0] = SDI_WRITE_MEMORY
;
553 store_long_parameter (buf
+ 1, (bp_addr
& 0xfffffffc));
554 store_long_parameter (buf
+ 5, 4);
555 if ((bp_addr
& 2) == 0 && bp_addr
!= (pc_addr
& 0xfffffffc))
557 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
559 buf
[9] = dbt_bp_entry
[0];
560 buf
[10] = dbt_bp_entry
[1];
561 buf
[11] = dbt_bp_entry
[2];
562 buf
[12] = dbt_bp_entry
[3];
566 buf
[9] = dbt_bp_entry
[3];
567 buf
[10] = dbt_bp_entry
[2];
568 buf
[11] = dbt_bp_entry
[1];
569 buf
[12] = dbt_bp_entry
[0];
574 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
576 if ((bp_addr
& 2) == 0)
578 buf
[9] = dbt_bp_entry
[0];
579 buf
[10] = dbt_bp_entry
[1];
580 buf
[11] = bp_data
[i
][2] & 0x7f;
581 buf
[12] = bp_data
[i
][3];
585 buf
[9] = bp_data
[i
][0];
586 buf
[10] = bp_data
[i
][1];
587 buf
[11] = dbt_bp_entry
[0];
588 buf
[12] = dbt_bp_entry
[1];
593 if ((bp_addr
& 2) == 0)
595 buf
[9] = bp_data
[i
][0];
596 buf
[10] = bp_data
[i
][1] & 0x7f;
597 buf
[11] = dbt_bp_entry
[1];
598 buf
[12] = dbt_bp_entry
[0];
602 buf
[9] = dbt_bp_entry
[1];
603 buf
[10] = dbt_bp_entry
[0];
604 buf
[11] = bp_data
[i
][2];
605 buf
[12] = bp_data
[i
][3];
612 /* Set access breaks. */
613 for (i
= 0; i
< max_access_breaks
; i
++)
615 ab_addr
= ab_address
[i
];
617 if (ab_addr
== 0x00000000)
621 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
625 case 0: /* write watch */
626 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
629 case 1: /* read watch */
630 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
633 case 2: /* access watch */
634 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
643 case 0: /* write watch */
644 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
647 case 1: /* read watch */
648 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
651 case 2: /* access watch */
652 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
659 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8180 + 4 * i
, 4, ab_addr
);
662 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8200 + 4 * i
, 4,
666 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8280 + 4 * i
, 4,
670 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8300 + 4 * i
, 4,
674 /* Resume program. */
675 send_cmd (SDI_EXEC_CPU
);
677 /* Without this, some commands which require an active target (such as kill)
678 won't work. This variable serves (at least) double duty as both the pid
679 of the target process (if it has such), and as a flag indicating that a
680 target is active. These functions should be split out into seperate
681 variables, especially since GDB will someday have a notion of debugging
682 several processes. */
683 inferior_ptid
= remote_m32r_ptid
;
684 add_thread_silent (remote_m32r_ptid
);
689 /* Wait until the remote machine stops, then return,
690 storing status in STATUS just as `wait' would. */
693 gdb_cntrl_c (int signo
)
696 fprintf_unfiltered (gdb_stdlog
, "interrupt\n");
701 m32r_wait (struct target_ops
*ops
,
702 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
704 static RETSIGTYPE (*prev_sigint
) ();
705 unsigned long bp_addr
, pc_addr
;
708 unsigned char buf
[13];
712 fprintf_unfiltered (gdb_stdlog
, "m32r_wait()\n");
714 status
->kind
= TARGET_WAITKIND_EXITED
;
715 status
->value
.sig
= GDB_SIGNAL_0
;
718 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
720 /* Wait for ready. */
721 buf
[0] = SDI_WAIT_FOR_READY
;
722 if (serial_write (sdi_desc
, buf
, 1) != 0)
723 error (_("Remote connection closed"));
727 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
729 error (_("Remote connection closed"));
731 if (c
== '-') /* error */
733 status
->kind
= TARGET_WAITKIND_STOPPED
;
734 status
->value
.sig
= GDB_SIGNAL_HUP
;
735 return inferior_ptid
;
737 else if (c
== '+') /* stopped */
741 ret
= serial_write (sdi_desc
, "!", 1); /* packet to interrupt */
743 ret
= serial_write (sdi_desc
, ".", 1); /* packet to wait */
745 error (_("Remote connection closed"));
748 status
->kind
= TARGET_WAITKIND_STOPPED
;
750 status
->value
.sig
= GDB_SIGNAL_INT
;
752 status
->value
.sig
= GDB_SIGNAL_TRAP
;
755 signal (SIGINT
, prev_sigint
);
759 /* Recover parallel bit. */
760 if (last_pc_addr
!= 0xffffffff)
762 buf
[0] = SDI_WRITE_MEMORY
;
763 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
764 store_long_parameter (buf
+ 1, last_pc_addr
);
766 store_long_parameter (buf
+ 1, last_pc_addr
- 1);
767 store_long_parameter (buf
+ 5, 1);
768 buf
[9] = last_pc_addr_data
[0];
770 last_pc_addr
= 0xffffffff;
773 if (use_ib_breakpoints
)
774 ib_breakpoints
= max_ib_breakpoints
;
778 /* Set back pc by 2 if m32r is stopped with dbt. */
779 last_pc_addr
= 0xffffffff;
780 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
);
781 pc_addr
= recv_long_data () - 2;
782 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
784 if (pc_addr
== bp_address
[i
])
786 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
788 /* If there is a parallel instruction with +2 offset at pc
789 address, we have to take care of it later. */
790 if ((pc_addr
& 0x2) != 0)
792 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
794 if ((bp_data
[i
][2] & 0x80) != 0)
796 last_pc_addr
= pc_addr
;
797 last_pc_addr_data
[0] = bp_data
[i
][2];
798 last_pc_addr_data
[1] = bp_data
[i
][3];
803 if ((bp_data
[i
][1] & 0x80) != 0)
805 last_pc_addr
= pc_addr
;
806 last_pc_addr_data
[0] = bp_data
[i
][1];
807 last_pc_addr_data
[1] = bp_data
[i
][0];
815 /* Remove ib breakpoints. */
816 for (i
= 0; i
< ib_breakpoints
; i
++)
818 if (bp_address
[i
] != 0xffffffff)
819 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
822 /* Remove dbt breakpoints. */
823 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
825 bp_addr
= bp_address
[i
];
826 if (bp_addr
!= 0xffffffff)
829 bp_addr
&= 0x7fffffff;
830 buf
[0] = SDI_WRITE_MEMORY
;
831 store_long_parameter (buf
+ 1, bp_addr
& 0xfffffffc);
832 store_long_parameter (buf
+ 5, 4);
833 buf
[9] = bp_data
[i
][0];
834 buf
[10] = bp_data
[i
][1];
835 buf
[11] = bp_data
[i
][2];
836 buf
[12] = bp_data
[i
][3];
841 /* Remove access breaks. */
842 hit_watchpoint_addr
= 0;
843 for (i
= 0; i
< max_access_breaks
; i
++)
845 if (ab_address
[i
] != 0x00000000)
847 buf
[0] = SDI_READ_MEMORY
;
848 store_long_parameter (buf
+ 1, 0xffff8100 + 4 * i
);
849 store_long_parameter (buf
+ 5, 4);
850 serial_write (sdi_desc
, buf
, 9);
851 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
852 if (c
!= '-' && recv_data (buf
, 4) != -1)
854 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
856 if ((buf
[3] & 0x1) == 0x1)
857 hit_watchpoint_addr
= ab_address
[i
];
861 if ((buf
[0] & 0x1) == 0x1)
862 hit_watchpoint_addr
= ab_address
[i
];
866 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
872 fprintf_unfiltered (gdb_stdlog
, "pc => 0x%lx\n", pc_addr
);
874 return inferior_ptid
;
877 /* Terminate the open connection to the remote debugger.
878 Use this when you want to detach and do something else
881 m32r_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
884 fprintf_unfiltered (gdb_stdlog
, "m32r_detach(%d)\n", from_tty
);
886 m32r_resume (ops
, inferior_ptid
, 0, GDB_SIGNAL_0
);
888 /* Calls m32r_close to do the real work. */
891 fprintf_unfiltered (gdb_stdlog
, "Ending remote %s debugging\n",
895 /* Return the id of register number REGNO. */
898 get_reg_id (int regno
)
917 /* Fetch register REGNO, or all registers if REGNO is -1.
918 Returns errno value. */
920 m32r_fetch_register (struct target_ops
*ops
,
921 struct regcache
*regcache
, int regno
)
923 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
924 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
925 unsigned long val
, val2
, regid
;
930 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
932 m32r_fetch_register (ops
, regcache
, regno
);
936 gdb_byte buffer
[MAX_REGISTER_SIZE
];
938 regid
= get_reg_id (regno
);
939 send_one_arg_cmd (SDI_READ_CPU_REG
, regid
);
940 val
= recv_long_data ();
942 if (regid
== SDI_REG_PSW
)
944 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
945 val2
= recv_long_data ();
946 val
= ((0x00cf & val2
) << 8) | ((0xcf00 & val
) >> 8);
950 fprintf_unfiltered (gdb_stdlog
, "m32r_fetch_register(%d,0x%08lx)\n",
953 /* We got the number the register holds, but gdb expects to see a
954 value in the target byte ordering. */
955 store_unsigned_integer (buffer
, 4, byte_order
, val
);
956 regcache_raw_supply (regcache
, regno
, buffer
);
961 /* Store register REGNO, or all if REGNO == 0.
962 Return errno value. */
964 m32r_store_register (struct target_ops
*ops
,
965 struct regcache
*regcache
, int regno
)
968 ULONGEST regval
, tmp
;
973 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
975 m32r_store_register (ops
, regcache
, regno
);
979 regcache_cooked_read_unsigned (regcache
, regno
, ®val
);
980 regid
= get_reg_id (regno
);
982 if (regid
== SDI_REG_PSW
)
984 unsigned long psw
, bbpsw
;
986 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_PSW
);
987 psw
= recv_long_data ();
989 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
990 bbpsw
= recv_long_data ();
992 tmp
= (0x00cf & psw
) | ((0x00cf & regval
) << 8);
993 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PSW
, tmp
);
995 tmp
= (0x0030 & bbpsw
) | ((0xcf00 & regval
) >> 8);
996 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BBPSW
, tmp
);
1000 send_two_arg_cmd (SDI_WRITE_CPU_REG
, regid
, regval
);
1004 fprintf_unfiltered (gdb_stdlog
, "m32r_store_register(%d,0x%08lu)\n",
1005 regno
, (unsigned long) regval
);
1009 /* Get ready to modify the registers array. On machines which store
1010 individual registers, this doesn't need to do anything. On machines
1011 which store all the registers in one fell swoop, this makes sure
1012 that registers contains all the registers from the program being
1016 m32r_prepare_to_store (struct target_ops
*self
, struct regcache
*regcache
)
1018 /* Do nothing, since we can store individual regs. */
1020 fprintf_unfiltered (gdb_stdlog
, "m32r_prepare_to_store()\n");
1024 m32r_files_info (struct target_ops
*target
)
1026 const char *file
= "nothing";
1030 file
= bfd_get_filename (exec_bfd
);
1031 printf_filtered ("\tAttached to %s running program %s\n",
1036 /* Helper for m32r_xfer_partial that handles memory transfers.
1037 Arguments are like target_xfer_partial. */
1039 static enum target_xfer_status
1040 m32r_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
1041 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
1043 unsigned long taddr
;
1044 unsigned char buf
[0x2000];
1051 if ((taddr
& 0xa0000000) == 0x80000000)
1052 taddr
&= 0x7fffffff;
1057 if (writebuf
!= NULL
)
1058 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%s,%s,write)\n",
1059 paddress (target_gdbarch (), memaddr
),
1062 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%s,%s,read)\n",
1063 paddress (target_gdbarch (), memaddr
),
1067 if (writebuf
!= NULL
)
1069 buf
[0] = SDI_WRITE_MEMORY
;
1070 store_long_parameter (buf
+ 1, taddr
);
1071 store_long_parameter (buf
+ 5, len
);
1074 memcpy (buf
+ 9, writebuf
, len
);
1075 ret
= send_data (buf
, len
+ 9) - 9;
1079 if (serial_write (sdi_desc
, buf
, 9) != 0)
1082 fprintf_unfiltered (gdb_stdlog
,
1083 "m32r_xfer_memory() failed\n");
1086 ret
= send_data (writebuf
, len
);
1091 buf
[0] = SDI_READ_MEMORY
;
1092 store_long_parameter (buf
+ 1, taddr
);
1093 store_long_parameter (buf
+ 5, len
);
1094 if (serial_write (sdi_desc
, buf
, 9) != 0)
1097 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1101 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1102 if (c
< 0 || c
== '-')
1105 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1109 ret
= recv_data (readbuf
, len
);
1115 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() fails\n");
1116 return TARGET_XFER_E_IO
;
1120 return TARGET_XFER_OK
;
1123 /* Target to_xfer_partial implementation. */
1125 static enum target_xfer_status
1126 m32r_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1127 const char *annex
, gdb_byte
*readbuf
,
1128 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
1129 ULONGEST
*xfered_len
)
1133 case TARGET_OBJECT_MEMORY
:
1134 return m32r_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
1137 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
,
1138 readbuf
, writebuf
, offset
, len
,
1144 m32r_kill (struct target_ops
*ops
)
1147 fprintf_unfiltered (gdb_stdlog
, "m32r_kill()\n");
1149 inferior_ptid
= null_ptid
;
1150 delete_thread_silent (remote_m32r_ptid
);
1155 /* Clean up when a program exits.
1157 The program actually lives on in the remote processor's RAM, and may be
1158 run again without a download. Don't leave it full of breakpoint
1162 m32r_mourn_inferior (struct target_ops
*ops
)
1165 fprintf_unfiltered (gdb_stdlog
, "m32r_mourn_inferior()\n");
1167 remove_breakpoints ();
1168 generic_mourn_inferior ();
1172 m32r_insert_breakpoint (struct target_ops
*ops
,
1173 struct gdbarch
*gdbarch
,
1174 struct bp_target_info
*bp_tgt
)
1176 CORE_ADDR addr
= bp_tgt
->placed_address
;
1178 unsigned char buf
[13];
1182 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_breakpoint(%s,...)\n",
1183 paddress (gdbarch
, addr
));
1185 if (use_ib_breakpoints
)
1186 ib_breakpoints
= max_ib_breakpoints
;
1190 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1192 if (bp_address
[i
] == 0xffffffff)
1194 bp_address
[i
] = addr
;
1195 if (i
>= ib_breakpoints
)
1197 buf
[0] = SDI_READ_MEMORY
;
1199 store_long_parameter (buf
+ 1, addr
& 0xfffffffc);
1201 store_long_parameter (buf
+ 1, addr
& 0x7ffffffc);
1202 store_long_parameter (buf
+ 5, 4);
1203 serial_write (sdi_desc
, buf
, 9);
1204 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1206 recv_data (bp_data
[i
], 4);
1212 error (_("Too many breakpoints"));
1217 m32r_remove_breakpoint (struct target_ops
*ops
,
1218 struct gdbarch
*gdbarch
,
1219 struct bp_target_info
*bp_tgt
)
1221 CORE_ADDR addr
= bp_tgt
->placed_address
;
1225 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_breakpoint(%s)\n",
1226 paddress (gdbarch
, addr
));
1228 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1230 if (bp_address
[i
] == addr
)
1232 bp_address
[i
] = 0xffffffff;
1241 m32r_load (struct target_ops
*self
, const char *args
, int from_tty
)
1243 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
1250 struct timeval start_time
, end_time
;
1251 unsigned long data_count
; /* Number of bytes transferred to memory. */
1252 static RETSIGTYPE (*prev_sigint
) ();
1254 /* for direct tcp connections, we can do a fast binary download. */
1259 while (*args
!= '\000')
1261 char *arg
= extract_arg_const (&args
);
1265 make_cleanup (xfree
, arg
);
1269 else if (strncmp (arg
, "-quiet", strlen (arg
)) == 0)
1271 else if (strncmp (arg
, "-nostart", strlen (arg
)) == 0)
1274 error (_("Unknown option `%s'"), arg
);
1278 filename
= get_exec_file (1);
1280 pbfd
= gdb_bfd_open (filename
, gnutarget
, -1);
1282 perror_with_name (filename
);
1283 make_cleanup_bfd_unref (pbfd
);
1285 if (!bfd_check_format (pbfd
, bfd_object
))
1286 error (_("\"%s\" is not an object file: %s"), filename
,
1287 bfd_errmsg (bfd_get_error ()));
1289 gettimeofday (&start_time
, NULL
);
1293 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
1295 for (section
= pbfd
->sections
; section
; section
= section
->next
)
1297 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
1299 bfd_vma section_address
;
1300 bfd_size_type section_size
;
1304 section_address
= bfd_section_lma (pbfd
, section
);
1305 section_size
= bfd_get_section_size (section
);
1309 if ((section_address
& 0xa0000000) == 0x80000000)
1310 section_address
&= 0x7fffffff;
1314 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1315 bfd_get_section_name (pbfd
, section
),
1316 (unsigned long) section_address
,
1317 (int) section_size
);
1321 data_count
+= section_size
;
1324 while (section_size
> 0)
1326 char unsigned buf
[0x1000 + 9];
1329 count
= min (section_size
, 0x1000);
1331 buf
[0] = SDI_WRITE_MEMORY
;
1332 store_long_parameter (buf
+ 1, section_address
);
1333 store_long_parameter (buf
+ 5, count
);
1335 bfd_get_section_contents (pbfd
, section
, buf
+ 9, fptr
, count
);
1336 if (send_data (buf
, count
+ 9) <= 0)
1337 error (_("Error while downloading %s section."),
1338 bfd_get_section_name (pbfd
, section
));
1342 printf_unfiltered (".");
1345 printf_unfiltered ("\n");
1348 gdb_flush (gdb_stdout
);
1351 section_address
+= count
;
1353 section_size
-= count
;
1359 if (!quiet
&& !interrupted
)
1361 printf_unfiltered ("done.\n");
1362 gdb_flush (gdb_stdout
);
1368 printf_unfiltered ("Interrupted.\n");
1374 signal (SIGINT
, prev_sigint
);
1376 gettimeofday (&end_time
, NULL
);
1378 /* Make the PC point at the start address. */
1380 regcache_write_pc (get_current_regcache (),
1381 bfd_get_start_address (exec_bfd
));
1383 inferior_ptid
= null_ptid
; /* No process now. */
1384 delete_thread_silent (remote_m32r_ptid
);
1386 /* This is necessary because many things were based on the PC at the time
1387 that we attached to the monitor, which is no longer valid now that we
1388 have loaded new code (and just changed the PC). Another way to do this
1389 might be to call normal_stop, except that the stack may not be valid,
1390 and things would get horribly confused... */
1392 clear_symtab_users (0);
1396 entry
= bfd_get_start_address (pbfd
);
1399 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename
,
1400 (unsigned long) entry
);
1403 print_transfer_performance (gdb_stdout
, data_count
, 0, &start_time
,
1406 do_cleanups (old_chain
);
1410 m32r_stop (struct target_ops
*self
, ptid_t ptid
)
1413 fprintf_unfiltered (gdb_stdlog
, "m32r_stop()\n");
1415 send_cmd (SDI_STOP_CPU
);
1421 /* Tell whether this target can support a hardware breakpoint. CNT
1422 is the number of hardware breakpoints already installed. This
1423 implements the target_can_use_hardware_watchpoint macro. */
1426 m32r_can_use_hw_watchpoint (struct target_ops
*self
,
1427 int type
, int cnt
, int othertype
)
1429 return sdi_desc
!= NULL
&& cnt
< max_access_breaks
;
1432 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1433 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1437 m32r_insert_watchpoint (struct target_ops
*self
,
1438 CORE_ADDR addr
, int len
, int type
,
1439 struct expression
*cond
)
1444 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_watchpoint(%s,%d,%d)\n",
1445 paddress (target_gdbarch (), addr
), len
, type
);
1447 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1449 if (ab_address
[i
] == 0x00000000)
1451 ab_address
[i
] = addr
;
1458 error (_("Too many watchpoints"));
1463 m32r_remove_watchpoint (struct target_ops
*self
,
1464 CORE_ADDR addr
, int len
, int type
,
1465 struct expression
*cond
)
1470 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_watchpoint(%s,%d,%d)\n",
1471 paddress (target_gdbarch (), addr
), len
, type
);
1473 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1475 if (ab_address
[i
] == addr
)
1477 ab_address
[i
] = 0x00000000;
1486 m32r_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
1490 if (hit_watchpoint_addr
!= 0x00000000)
1492 *addr_p
= hit_watchpoint_addr
;
1499 m32r_stopped_by_watchpoint (struct target_ops
*ops
)
1503 return m32r_stopped_data_address (¤t_target
, &addr
);
1506 /* Check to see if a thread is still alive. */
1509 m32r_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1511 if (ptid_equal (ptid
, remote_m32r_ptid
))
1512 /* The main task is always alive. */
1518 /* Convert a thread ID to a string. Returns the string in a static
1522 m32r_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1524 static char buf
[64];
1526 if (ptid_equal (remote_m32r_ptid
, ptid
))
1528 xsnprintf (buf
, sizeof buf
, "Thread <main>");
1532 return normal_pid_to_str (ptid
);
1536 sdireset_command (char *args
, int from_tty
)
1539 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1541 send_cmd (SDI_OPEN
);
1543 inferior_ptid
= null_ptid
;
1544 delete_thread_silent (remote_m32r_ptid
);
1549 sdistatus_command (char *args
, int from_tty
)
1551 unsigned char buf
[4096];
1555 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1560 send_cmd (SDI_STATUS
);
1561 for (i
= 0; i
< 4096; i
++)
1563 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1571 printf_filtered ("%s", buf
);
1576 debug_chaos_command (char *args
, int from_tty
)
1578 unsigned char buf
[3];
1580 buf
[0] = SDI_SET_ATTR
;
1581 buf
[1] = SDI_ATTR_CACHE
;
1582 buf
[2] = SDI_CACHE_TYPE_CHAOS
;
1588 use_debug_dma_command (char *args
, int from_tty
)
1590 unsigned char buf
[3];
1592 buf
[0] = SDI_SET_ATTR
;
1593 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1594 buf
[2] = SDI_MEM_ACCESS_DEBUG_DMA
;
1599 use_mon_code_command (char *args
, int from_tty
)
1601 unsigned char buf
[3];
1603 buf
[0] = SDI_SET_ATTR
;
1604 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1605 buf
[2] = SDI_MEM_ACCESS_MON_CODE
;
1611 use_ib_breakpoints_command (char *args
, int from_tty
)
1613 use_ib_breakpoints
= 1;
1617 use_dbt_breakpoints_command (char *args
, int from_tty
)
1619 use_ib_breakpoints
= 0;
1623 m32r_return_one (struct target_ops
*target
)
1628 /* Implementation of the to_has_execution method. */
1631 m32r_has_execution (struct target_ops
*target
, ptid_t the_ptid
)
1636 /* Define the target subroutine names. */
1638 struct target_ops m32r_ops
;
1641 init_m32r_ops (void)
1643 m32r_ops
.to_shortname
= "m32rsdi";
1644 m32r_ops
.to_longname
= "Remote M32R debugging over SDI interface";
1645 m32r_ops
.to_doc
= "Use an M32R board using SDI debugging protocol.";
1646 m32r_ops
.to_open
= m32r_open
;
1647 m32r_ops
.to_close
= m32r_close
;
1648 m32r_ops
.to_detach
= m32r_detach
;
1649 m32r_ops
.to_resume
= m32r_resume
;
1650 m32r_ops
.to_wait
= m32r_wait
;
1651 m32r_ops
.to_fetch_registers
= m32r_fetch_register
;
1652 m32r_ops
.to_store_registers
= m32r_store_register
;
1653 m32r_ops
.to_prepare_to_store
= m32r_prepare_to_store
;
1654 m32r_ops
.to_xfer_partial
= m32r_xfer_partial
;
1655 m32r_ops
.to_files_info
= m32r_files_info
;
1656 m32r_ops
.to_insert_breakpoint
= m32r_insert_breakpoint
;
1657 m32r_ops
.to_remove_breakpoint
= m32r_remove_breakpoint
;
1658 m32r_ops
.to_can_use_hw_breakpoint
= m32r_can_use_hw_watchpoint
;
1659 m32r_ops
.to_insert_watchpoint
= m32r_insert_watchpoint
;
1660 m32r_ops
.to_remove_watchpoint
= m32r_remove_watchpoint
;
1661 m32r_ops
.to_stopped_by_watchpoint
= m32r_stopped_by_watchpoint
;
1662 m32r_ops
.to_stopped_data_address
= m32r_stopped_data_address
;
1663 m32r_ops
.to_kill
= m32r_kill
;
1664 m32r_ops
.to_load
= m32r_load
;
1665 m32r_ops
.to_create_inferior
= m32r_create_inferior
;
1666 m32r_ops
.to_mourn_inferior
= m32r_mourn_inferior
;
1667 m32r_ops
.to_stop
= m32r_stop
;
1668 m32r_ops
.to_log_command
= serial_log_command
;
1669 m32r_ops
.to_thread_alive
= m32r_thread_alive
;
1670 m32r_ops
.to_pid_to_str
= m32r_pid_to_str
;
1671 m32r_ops
.to_stratum
= process_stratum
;
1672 m32r_ops
.to_has_all_memory
= m32r_return_one
;
1673 m32r_ops
.to_has_memory
= m32r_return_one
;
1674 m32r_ops
.to_has_stack
= m32r_return_one
;
1675 m32r_ops
.to_has_registers
= m32r_return_one
;
1676 m32r_ops
.to_has_execution
= m32r_has_execution
;
1677 m32r_ops
.to_magic
= OPS_MAGIC
;
1681 extern initialize_file_ftype _initialize_remote_m32r
;
1684 _initialize_remote_m32r (void)
1690 /* Initialize breakpoints. */
1691 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1692 bp_address
[i
] = 0xffffffff;
1694 /* Initialize access breaks. */
1695 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1696 ab_address
[i
] = 0x00000000;
1698 add_target (&m32r_ops
);
1700 add_com ("sdireset", class_obscure
, sdireset_command
,
1701 _("Reset SDI connection."));
1703 add_com ("sdistatus", class_obscure
, sdistatus_command
,
1704 _("Show status of SDI connection."));
1706 add_com ("debug_chaos", class_obscure
, debug_chaos_command
,
1707 _("Debug M32R/Chaos."));
1709 add_com ("use_debug_dma", class_obscure
, use_debug_dma_command
,
1710 _("Use debug DMA mem access."));
1711 add_com ("use_mon_code", class_obscure
, use_mon_code_command
,
1712 _("Use mon code mem access."));
1714 add_com ("use_ib_break", class_obscure
, use_ib_breakpoints_command
,
1715 _("Set breakpoints by IB break."));
1716 add_com ("use_dbt_break", class_obscure
, use_dbt_breakpoints_command
,
1717 _("Set breakpoints by dbt."));
1719 /* Yes, 42000 is arbitrary. The only sense out of it, is that it
1721 remote_m32r_ptid
= ptid_build (42000, 0, 42000);