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/>. */
30 #include "gdbthread.h"
36 #include <netinet/in.h>
38 #include <sys/types.h>
42 #include "cli/cli-utils.h"
46 /* Descriptor for I/O to remote machine. */
48 static struct serial
*sdi_desc
= NULL
;
50 #define SDI_TIMEOUT 30
55 static char chip_name
[64];
58 static unsigned long last_pc_addr
= 0xffffffff;
59 static unsigned char last_pc_addr_data
[2];
61 static int mmu_on
= 0;
63 static int use_ib_breakpoints
= 1;
65 #define MAX_BREAKPOINTS 1024
66 static int max_ib_breakpoints
;
67 static unsigned long bp_address
[MAX_BREAKPOINTS
];
68 static unsigned char bp_data
[MAX_BREAKPOINTS
][4];
71 static const unsigned char dbt_bp_entry
[] = {
72 0x10, 0xe0, 0x70, 0x00
75 #define MAX_ACCESS_BREAKS 4
76 static int max_access_breaks
;
77 static unsigned long ab_address
[MAX_ACCESS_BREAKS
];
78 static unsigned int ab_type
[MAX_ACCESS_BREAKS
];
79 static unsigned int ab_size
[MAX_ACCESS_BREAKS
];
80 static CORE_ADDR hit_watchpoint_addr
= 0;
82 static int interrupted
= 0;
84 /* Forward data declarations */
85 extern struct target_ops m32r_ops
;
87 /* This is the ptid we use while we're connected to the remote. Its
88 value is arbitrary, as the target doesn't have a notion of
89 processes or threads, but we need something non-null to place in
91 static ptid_t remote_m32r_ptid
;
97 #define SDI_READ_CPU_REG 4
98 #define SDI_WRITE_CPU_REG 5
99 #define SDI_READ_MEMORY 6
100 #define SDI_WRITE_MEMORY 7
101 #define SDI_EXEC_CPU 8
102 #define SDI_STOP_CPU 9
103 #define SDI_WAIT_FOR_READY 10
104 #define SDI_GET_ATTR 11
105 #define SDI_SET_ATTR 12
106 #define SDI_STATUS 13
109 #define SDI_ATTR_NAME 1
110 #define SDI_ATTR_BRK 2
111 #define SDI_ATTR_ABRK 3
112 #define SDI_ATTR_CACHE 4
113 #define SDI_CACHE_TYPE_M32102 0
114 #define SDI_CACHE_TYPE_CHAOS 1
115 #define SDI_ATTR_MEM_ACCESS 5
116 #define SDI_MEM_ACCESS_DEBUG_DMA 0
117 #define SDI_MEM_ACCESS_MON_CODE 1
130 #define SDI_REG_R10 10
131 #define SDI_REG_R11 11
132 #define SDI_REG_R12 12
133 #define SDI_REG_FP 13
134 #define SDI_REG_LR 14
135 #define SDI_REG_SP 15
136 #define SDI_REG_PSW 16
137 #define SDI_REG_CBR 17
138 #define SDI_REG_SPI 18
139 #define SDI_REG_SPU 19
140 #define SDI_REG_CR4 20
141 #define SDI_REG_EVB 21
142 #define SDI_REG_BPC 22
143 #define SDI_REG_CR7 23
144 #define SDI_REG_BBPSW 24
145 #define SDI_REG_CR9 25
146 #define SDI_REG_CR10 26
147 #define SDI_REG_CR11 27
148 #define SDI_REG_CR12 28
149 #define SDI_REG_WR 29
150 #define SDI_REG_BBPC 30
151 #define SDI_REG_PBP 31
152 #define SDI_REG_ACCH 32
153 #define SDI_REG_ACCL 33
154 #define SDI_REG_ACC1H 34
155 #define SDI_REG_ACC1L 35
158 /* Low level communication functions. */
160 /* Check an ack packet from the target. */
169 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
174 if (c
!= '+') /* error */
180 /* Send data to the target and check an ack packet. */
182 send_data (void *buf
, int len
)
187 if (serial_write (sdi_desc
, buf
, len
) != 0)
190 if (get_ack () == -1)
196 /* Receive data from the target. */
198 recv_data (void *buf
, int len
)
208 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
213 ((unsigned char *) buf
)[total
++] = c
;
219 /* Store unsigned long parameter on packet. */
221 store_long_parameter (void *buf
, long val
)
224 memcpy (buf
, &val
, 4);
228 send_cmd (unsigned char cmd
)
230 unsigned char buf
[1];
233 return send_data (buf
, 1);
237 send_one_arg_cmd (unsigned char cmd
, unsigned char arg1
)
239 unsigned char buf
[2];
243 return send_data (buf
, 2);
247 send_two_arg_cmd (unsigned char cmd
, unsigned char arg1
, unsigned long arg2
)
249 unsigned char buf
[6];
253 store_long_parameter (buf
+ 2, arg2
);
254 return send_data (buf
, 6);
258 send_three_arg_cmd (unsigned char cmd
, unsigned long arg1
, unsigned long arg2
,
261 unsigned char buf
[13];
264 store_long_parameter (buf
+ 1, arg1
);
265 store_long_parameter (buf
+ 5, arg2
);
266 store_long_parameter (buf
+ 9, arg3
);
267 return send_data (buf
, 13);
271 recv_char_data (void)
280 recv_long_data (void)
289 /* Check if MMU is on. */
291 check_mmu_status (void)
295 /* Read PC address. */
296 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
) == -1)
298 val
= recv_long_data ();
299 if ((val
& 0xc0000000) == 0x80000000)
305 /* Read EVB address. */
306 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_EVB
) == -1)
308 val
= recv_long_data ();
309 if ((val
& 0xc0000000) == 0x80000000)
319 /* This is called not only when we first attach, but also when the
320 user types "run" after having attached. */
322 m32r_create_inferior (struct target_ops
*ops
, char *execfile
,
323 char *args
, char **env
, int from_tty
)
328 error (_("Cannot pass arguments to remote STDEBUG process"));
330 if (execfile
== 0 || exec_bfd
== 0)
331 error (_("No executable file specified"));
334 fprintf_unfiltered (gdb_stdlog
, "m32r_create_inferior(%s,%s)\n", execfile
,
337 entry_pt
= bfd_get_start_address (exec_bfd
);
339 /* The "process" (board) is already stopped awaiting our commands, and
340 the program is already downloaded. We just set its PC and go. */
342 clear_proceed_status ();
344 /* Tell wait_for_inferior that we've started a new process. */
345 init_wait_for_inferior ();
347 /* Set up the "saved terminal modes" of the inferior
348 based on what modes we are starting it with. */
349 target_terminal_init ();
351 /* Install inferior's terminal modes. */
352 target_terminal_inferior ();
354 regcache_write_pc (get_current_regcache (), entry_pt
);
357 /* Open a connection to a remote debugger.
358 NAME is the filename used for communication. */
361 m32r_open (char *args
, int from_tty
)
363 struct hostent
*host_ent
;
364 struct sockaddr_in server_addr
;
365 char *port_str
, hostname
[256];
371 fprintf_unfiltered (gdb_stdlog
, "m32r_open(%d)\n", from_tty
);
373 target_preopen (from_tty
);
375 push_target (&m32r_ops
);
378 xsnprintf (hostname
, sizeof (hostname
), "localhost:%d", SDIPORT
);
381 port_str
= strchr (args
, ':');
382 if (port_str
== NULL
)
383 xsnprintf (hostname
, sizeof (hostname
), "%s:%d", args
, SDIPORT
);
385 xsnprintf (hostname
, sizeof (hostname
), "%s", args
);
388 sdi_desc
= serial_open (hostname
);
390 error (_("Connection refused."));
392 if (get_ack () == -1)
393 error (_("Cannot connect to SDI target."));
395 if (send_cmd (SDI_OPEN
) == -1)
396 error (_("Cannot connect to SDI target."));
398 /* Get maximum number of ib breakpoints. */
399 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_BRK
);
400 max_ib_breakpoints
= recv_char_data ();
402 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints
);
404 /* Initialize breakpoints. */
405 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
406 bp_address
[i
] = 0xffffffff;
408 /* Get maximum number of access breaks. */
409 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_ABRK
);
410 max_access_breaks
= recv_char_data ();
412 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks
);
414 /* Initialize access breask. */
415 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
416 ab_address
[i
] = 0x00000000;
420 /* Get the name of chip on target board. */
421 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_NAME
);
422 recv_data (chip_name
, 64);
425 printf_filtered ("Remote %s connected to %s\n", target_shortname
,
429 /* Close out all files and local state before this target loses control. */
435 fprintf_unfiltered (gdb_stdlog
, "m32r_close()\n");
439 send_cmd (SDI_CLOSE
);
440 serial_close (sdi_desc
);
444 inferior_ptid
= null_ptid
;
445 delete_thread_silent (remote_m32r_ptid
);
449 /* Tell the remote machine to resume. */
452 m32r_resume (struct target_ops
*ops
,
453 ptid_t ptid
, int step
, enum gdb_signal sig
)
455 unsigned long pc_addr
, bp_addr
, ab_addr
;
457 unsigned char buf
[13];
463 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(step)\n");
465 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(cont)\n");
470 pc_addr
= regcache_read_pc (get_current_regcache ());
472 fprintf_unfiltered (gdb_stdlog
, "pc <= 0x%lx\n", pc_addr
);
474 /* At pc address there is a parallel instruction with +2 offset,
475 so we have to make it a serial instruction or avoid it. */
476 if (pc_addr
== last_pc_addr
)
478 /* Avoid a parallel nop. */
479 if (last_pc_addr_data
[0] == 0xf0 && last_pc_addr_data
[1] == 0x00)
482 /* Now we can forget this instruction. */
483 last_pc_addr
= 0xffffffff;
485 /* Clear a parallel bit. */
488 buf
[0] = SDI_WRITE_MEMORY
;
489 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
490 store_long_parameter (buf
+ 1, pc_addr
);
492 store_long_parameter (buf
+ 1, pc_addr
- 1);
493 store_long_parameter (buf
+ 5, 1);
494 buf
[9] = last_pc_addr_data
[0] & 0x7f;
500 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
507 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, pc_addr
| 1);
512 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, 0x00000000);
515 if (use_ib_breakpoints
)
516 ib_breakpoints
= max_ib_breakpoints
;
520 /* Set ib breakpoints. */
521 for (i
= 0; i
< ib_breakpoints
; i
++)
523 bp_addr
= bp_address
[i
];
525 if (bp_addr
== 0xffffffff)
529 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
530 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
533 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
536 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8080 + 4 * i
, 4, bp_addr
);
539 /* Set dbt breakpoints. */
540 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
542 bp_addr
= bp_address
[i
];
544 if (bp_addr
== 0xffffffff)
548 bp_addr
&= 0x7fffffff;
550 /* Write DBT instruction. */
551 buf
[0] = SDI_WRITE_MEMORY
;
552 store_long_parameter (buf
+ 1, (bp_addr
& 0xfffffffc));
553 store_long_parameter (buf
+ 5, 4);
554 if ((bp_addr
& 2) == 0 && bp_addr
!= (pc_addr
& 0xfffffffc))
556 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
558 buf
[9] = dbt_bp_entry
[0];
559 buf
[10] = dbt_bp_entry
[1];
560 buf
[11] = dbt_bp_entry
[2];
561 buf
[12] = dbt_bp_entry
[3];
565 buf
[9] = dbt_bp_entry
[3];
566 buf
[10] = dbt_bp_entry
[2];
567 buf
[11] = dbt_bp_entry
[1];
568 buf
[12] = dbt_bp_entry
[0];
573 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
575 if ((bp_addr
& 2) == 0)
577 buf
[9] = dbt_bp_entry
[0];
578 buf
[10] = dbt_bp_entry
[1];
579 buf
[11] = bp_data
[i
][2] & 0x7f;
580 buf
[12] = bp_data
[i
][3];
584 buf
[9] = bp_data
[i
][0];
585 buf
[10] = bp_data
[i
][1];
586 buf
[11] = dbt_bp_entry
[0];
587 buf
[12] = dbt_bp_entry
[1];
592 if ((bp_addr
& 2) == 0)
594 buf
[9] = bp_data
[i
][0];
595 buf
[10] = bp_data
[i
][1] & 0x7f;
596 buf
[11] = dbt_bp_entry
[1];
597 buf
[12] = dbt_bp_entry
[0];
601 buf
[9] = dbt_bp_entry
[1];
602 buf
[10] = dbt_bp_entry
[0];
603 buf
[11] = bp_data
[i
][2];
604 buf
[12] = bp_data
[i
][3];
611 /* Set access breaks. */
612 for (i
= 0; i
< max_access_breaks
; i
++)
614 ab_addr
= ab_address
[i
];
616 if (ab_addr
== 0x00000000)
620 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
624 case 0: /* write watch */
625 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
628 case 1: /* read watch */
629 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
632 case 2: /* access watch */
633 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
642 case 0: /* write watch */
643 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
646 case 1: /* read watch */
647 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
650 case 2: /* access watch */
651 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
658 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8180 + 4 * i
, 4, ab_addr
);
661 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8200 + 4 * i
, 4,
665 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8280 + 4 * i
, 4,
669 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8300 + 4 * i
, 4,
673 /* Resume program. */
674 send_cmd (SDI_EXEC_CPU
);
676 /* Without this, some commands which require an active target (such as kill)
677 won't work. This variable serves (at least) double duty as both the pid
678 of the target process (if it has such), and as a flag indicating that a
679 target is active. These functions should be split out into seperate
680 variables, especially since GDB will someday have a notion of debugging
681 several processes. */
682 inferior_ptid
= remote_m32r_ptid
;
683 add_thread_silent (remote_m32r_ptid
);
688 /* Wait until the remote machine stops, then return,
689 storing status in STATUS just as `wait' would. */
692 gdb_cntrl_c (int signo
)
695 fprintf_unfiltered (gdb_stdlog
, "interrupt\n");
700 m32r_wait (struct target_ops
*ops
,
701 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
703 static RETSIGTYPE (*prev_sigint
) ();
704 unsigned long bp_addr
, pc_addr
;
707 unsigned char buf
[13];
711 fprintf_unfiltered (gdb_stdlog
, "m32r_wait()\n");
713 status
->kind
= TARGET_WAITKIND_EXITED
;
714 status
->value
.sig
= GDB_SIGNAL_0
;
717 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
719 /* Wait for ready. */
720 buf
[0] = SDI_WAIT_FOR_READY
;
721 if (serial_write (sdi_desc
, buf
, 1) != 0)
722 error (_("Remote connection closed"));
726 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
728 error (_("Remote connection closed"));
730 if (c
== '-') /* error */
732 status
->kind
= TARGET_WAITKIND_STOPPED
;
733 status
->value
.sig
= GDB_SIGNAL_HUP
;
734 return inferior_ptid
;
736 else if (c
== '+') /* stopped */
740 ret
= serial_write (sdi_desc
, "!", 1); /* packet to interrupt */
742 ret
= serial_write (sdi_desc
, ".", 1); /* packet to wait */
744 error (_("Remote connection closed"));
747 status
->kind
= TARGET_WAITKIND_STOPPED
;
749 status
->value
.sig
= GDB_SIGNAL_INT
;
751 status
->value
.sig
= GDB_SIGNAL_TRAP
;
754 signal (SIGINT
, prev_sigint
);
758 /* Recover parallel bit. */
759 if (last_pc_addr
!= 0xffffffff)
761 buf
[0] = SDI_WRITE_MEMORY
;
762 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
763 store_long_parameter (buf
+ 1, last_pc_addr
);
765 store_long_parameter (buf
+ 1, last_pc_addr
- 1);
766 store_long_parameter (buf
+ 5, 1);
767 buf
[9] = last_pc_addr_data
[0];
769 last_pc_addr
= 0xffffffff;
772 if (use_ib_breakpoints
)
773 ib_breakpoints
= max_ib_breakpoints
;
777 /* Set back pc by 2 if m32r is stopped with dbt. */
778 last_pc_addr
= 0xffffffff;
779 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
);
780 pc_addr
= recv_long_data () - 2;
781 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
783 if (pc_addr
== bp_address
[i
])
785 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
787 /* If there is a parallel instruction with +2 offset at pc
788 address, we have to take care of it later. */
789 if ((pc_addr
& 0x2) != 0)
791 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
793 if ((bp_data
[i
][2] & 0x80) != 0)
795 last_pc_addr
= pc_addr
;
796 last_pc_addr_data
[0] = bp_data
[i
][2];
797 last_pc_addr_data
[1] = bp_data
[i
][3];
802 if ((bp_data
[i
][1] & 0x80) != 0)
804 last_pc_addr
= pc_addr
;
805 last_pc_addr_data
[0] = bp_data
[i
][1];
806 last_pc_addr_data
[1] = bp_data
[i
][0];
814 /* Remove ib breakpoints. */
815 for (i
= 0; i
< ib_breakpoints
; i
++)
817 if (bp_address
[i
] != 0xffffffff)
818 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
821 /* Remove dbt breakpoints. */
822 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
824 bp_addr
= bp_address
[i
];
825 if (bp_addr
!= 0xffffffff)
828 bp_addr
&= 0x7fffffff;
829 buf
[0] = SDI_WRITE_MEMORY
;
830 store_long_parameter (buf
+ 1, bp_addr
& 0xfffffffc);
831 store_long_parameter (buf
+ 5, 4);
832 buf
[9] = bp_data
[i
][0];
833 buf
[10] = bp_data
[i
][1];
834 buf
[11] = bp_data
[i
][2];
835 buf
[12] = bp_data
[i
][3];
840 /* Remove access breaks. */
841 hit_watchpoint_addr
= 0;
842 for (i
= 0; i
< max_access_breaks
; i
++)
844 if (ab_address
[i
] != 0x00000000)
846 buf
[0] = SDI_READ_MEMORY
;
847 store_long_parameter (buf
+ 1, 0xffff8100 + 4 * i
);
848 store_long_parameter (buf
+ 5, 4);
849 serial_write (sdi_desc
, buf
, 9);
850 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
851 if (c
!= '-' && recv_data (buf
, 4) != -1)
853 if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG
)
855 if ((buf
[3] & 0x1) == 0x1)
856 hit_watchpoint_addr
= ab_address
[i
];
860 if ((buf
[0] & 0x1) == 0x1)
861 hit_watchpoint_addr
= ab_address
[i
];
865 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
871 fprintf_unfiltered (gdb_stdlog
, "pc => 0x%lx\n", pc_addr
);
873 return inferior_ptid
;
876 /* Terminate the open connection to the remote debugger.
877 Use this when you want to detach and do something else
880 m32r_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
883 fprintf_unfiltered (gdb_stdlog
, "m32r_detach(%d)\n", from_tty
);
885 m32r_resume (ops
, inferior_ptid
, 0, GDB_SIGNAL_0
);
887 /* Calls m32r_close to do the real work. */
890 fprintf_unfiltered (gdb_stdlog
, "Ending remote %s debugging\n",
894 /* Return the id of register number REGNO. */
897 get_reg_id (int regno
)
916 /* Fetch register REGNO, or all registers if REGNO is -1.
917 Returns errno value. */
919 m32r_fetch_register (struct target_ops
*ops
,
920 struct regcache
*regcache
, int regno
)
922 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
923 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
924 unsigned long val
, val2
, regid
;
929 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
931 m32r_fetch_register (ops
, regcache
, regno
);
935 gdb_byte buffer
[MAX_REGISTER_SIZE
];
937 regid
= get_reg_id (regno
);
938 send_one_arg_cmd (SDI_READ_CPU_REG
, regid
);
939 val
= recv_long_data ();
941 if (regid
== SDI_REG_PSW
)
943 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
944 val2
= recv_long_data ();
945 val
= ((0x00cf & val2
) << 8) | ((0xcf00 & val
) >> 8);
949 fprintf_unfiltered (gdb_stdlog
, "m32r_fetch_register(%d,0x%08lx)\n",
952 /* We got the number the register holds, but gdb expects to see a
953 value in the target byte ordering. */
954 store_unsigned_integer (buffer
, 4, byte_order
, val
);
955 regcache_raw_supply (regcache
, regno
, buffer
);
960 /* Store register REGNO, or all if REGNO == 0.
961 Return errno value. */
963 m32r_store_register (struct target_ops
*ops
,
964 struct regcache
*regcache
, int regno
)
967 ULONGEST regval
, tmp
;
972 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
974 m32r_store_register (ops
, regcache
, regno
);
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 const 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(%s,%d,write)\n",
1057 paddress (target_gdbarch (), memaddr
), len
);
1059 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%s,%d,read)\n",
1060 paddress (target_gdbarch (), memaddr
), len
);
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");
1119 m32r_kill (struct target_ops
*ops
)
1122 fprintf_unfiltered (gdb_stdlog
, "m32r_kill()\n");
1124 inferior_ptid
= null_ptid
;
1125 delete_thread_silent (remote_m32r_ptid
);
1130 /* Clean up when a program exits.
1132 The program actually lives on in the remote processor's RAM, and may be
1133 run again without a download. Don't leave it full of breakpoint
1137 m32r_mourn_inferior (struct target_ops
*ops
)
1140 fprintf_unfiltered (gdb_stdlog
, "m32r_mourn_inferior()\n");
1142 remove_breakpoints ();
1143 generic_mourn_inferior ();
1147 m32r_insert_breakpoint (struct gdbarch
*gdbarch
,
1148 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(%s,...)\n",
1157 paddress (gdbarch
, addr
));
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 gdbarch
*gdbarch
,
1192 struct bp_target_info
*bp_tgt
)
1194 CORE_ADDR addr
= bp_tgt
->placed_address
;
1198 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_breakpoint(%s)\n",
1199 paddress (gdbarch
, addr
));
1201 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1203 if (bp_address
[i
] == addr
)
1205 bp_address
[i
] = 0xffffffff;
1214 m32r_load (char *args
, int from_tty
)
1216 struct cleanup
*old_chain
;
1223 struct timeval start_time
, end_time
;
1224 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 args
= skip_spaces (args
);
1240 while ((*args
!= '\000') && !isspace (*args
))
1243 if (*args
!= '\000')
1248 else if (strncmp (arg
, "-quiet", strlen (arg
)) == 0)
1250 else if (strncmp (arg
, "-nostart", strlen (arg
)) == 0)
1253 error (_("Unknown option `%s'"), arg
);
1257 filename
= get_exec_file (1);
1259 pbfd
= gdb_bfd_open (filename
, gnutarget
, -1);
1262 perror_with_name (filename
);
1265 old_chain
= make_cleanup_bfd_unref (pbfd
);
1267 if (!bfd_check_format (pbfd
, bfd_object
))
1268 error (_("\"%s\" is not an object file: %s"), filename
,
1269 bfd_errmsg (bfd_get_error ()));
1271 gettimeofday (&start_time
, NULL
);
1275 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
1277 for (section
= pbfd
->sections
; section
; section
= section
->next
)
1279 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
1281 bfd_vma section_address
;
1282 bfd_size_type section_size
;
1286 section_address
= bfd_section_lma (pbfd
, section
);
1287 section_size
= bfd_get_section_size (section
);
1291 if ((section_address
& 0xa0000000) == 0x80000000)
1292 section_address
&= 0x7fffffff;
1296 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1297 bfd_get_section_name (pbfd
, section
),
1298 (unsigned long) section_address
,
1299 (int) section_size
);
1303 data_count
+= section_size
;
1306 while (section_size
> 0)
1308 char unsigned buf
[0x1000 + 9];
1311 count
= min (section_size
, 0x1000);
1313 buf
[0] = SDI_WRITE_MEMORY
;
1314 store_long_parameter (buf
+ 1, section_address
);
1315 store_long_parameter (buf
+ 5, count
);
1317 bfd_get_section_contents (pbfd
, section
, buf
+ 9, fptr
, count
);
1318 if (send_data (buf
, count
+ 9) <= 0)
1319 error (_("Error while downloading %s section."),
1320 bfd_get_section_name (pbfd
, section
));
1324 printf_unfiltered (".");
1327 printf_unfiltered ("\n");
1330 gdb_flush (gdb_stdout
);
1333 section_address
+= count
;
1335 section_size
-= count
;
1341 if (!quiet
&& !interrupted
)
1343 printf_unfiltered ("done.\n");
1344 gdb_flush (gdb_stdout
);
1350 printf_unfiltered ("Interrupted.\n");
1356 signal (SIGINT
, prev_sigint
);
1358 gettimeofday (&end_time
, NULL
);
1360 /* Make the PC point at the start address. */
1362 regcache_write_pc (get_current_regcache (),
1363 bfd_get_start_address (exec_bfd
));
1365 inferior_ptid
= null_ptid
; /* No process now. */
1366 delete_thread_silent (remote_m32r_ptid
);
1368 /* This is necessary because many things were based on the PC at the time
1369 that we attached to the monitor, which is no longer valid now that we
1370 have loaded new code (and just changed the PC). Another way to do this
1371 might be to call normal_stop, except that the stack may not be valid,
1372 and things would get horribly confused... */
1374 clear_symtab_users (0);
1378 entry
= bfd_get_start_address (pbfd
);
1381 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename
,
1382 (unsigned long) entry
);
1385 print_transfer_performance (gdb_stdout
, data_count
, 0, &start_time
,
1388 do_cleanups (old_chain
);
1392 m32r_stop (ptid_t ptid
)
1395 fprintf_unfiltered (gdb_stdlog
, "m32r_stop()\n");
1397 send_cmd (SDI_STOP_CPU
);
1403 /* Tell whether this target can support a hardware breakpoint. CNT
1404 is the number of hardware breakpoints already installed. This
1405 implements the target_can_use_hardware_watchpoint macro. */
1408 m32r_can_use_hw_watchpoint (int type
, int cnt
, int othertype
)
1410 return sdi_desc
!= NULL
&& cnt
< max_access_breaks
;
1413 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1414 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1418 m32r_insert_watchpoint (CORE_ADDR addr
, int len
, int type
,
1419 struct expression
*cond
)
1424 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_watchpoint(%s,%d,%d)\n",
1425 paddress (target_gdbarch (), addr
), len
, type
);
1427 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1429 if (ab_address
[i
] == 0x00000000)
1431 ab_address
[i
] = addr
;
1438 error (_("Too many watchpoints"));
1443 m32r_remove_watchpoint (CORE_ADDR addr
, int len
, int type
,
1444 struct expression
*cond
)
1449 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_watchpoint(%s,%d,%d)\n",
1450 paddress (target_gdbarch (), addr
), len
, type
);
1452 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1454 if (ab_address
[i
] == addr
)
1456 ab_address
[i
] = 0x00000000;
1465 m32r_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
1469 if (hit_watchpoint_addr
!= 0x00000000)
1471 *addr_p
= hit_watchpoint_addr
;
1478 m32r_stopped_by_watchpoint (void)
1482 return m32r_stopped_data_address (¤t_target
, &addr
);
1485 /* Check to see if a thread is still alive. */
1488 m32r_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
1490 if (ptid_equal (ptid
, remote_m32r_ptid
))
1491 /* The main task is always alive. */
1497 /* Convert a thread ID to a string. Returns the string in a static
1501 m32r_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1503 static char buf
[64];
1505 if (ptid_equal (remote_m32r_ptid
, ptid
))
1507 xsnprintf (buf
, sizeof buf
, "Thread <main>");
1511 return normal_pid_to_str (ptid
);
1515 sdireset_command (char *args
, int from_tty
)
1518 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1520 send_cmd (SDI_OPEN
);
1522 inferior_ptid
= null_ptid
;
1523 delete_thread_silent (remote_m32r_ptid
);
1528 sdistatus_command (char *args
, int from_tty
)
1530 unsigned char buf
[4096];
1534 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1539 send_cmd (SDI_STATUS
);
1540 for (i
= 0; i
< 4096; i
++)
1542 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1550 printf_filtered ("%s", buf
);
1555 debug_chaos_command (char *args
, int from_tty
)
1557 unsigned char buf
[3];
1559 buf
[0] = SDI_SET_ATTR
;
1560 buf
[1] = SDI_ATTR_CACHE
;
1561 buf
[2] = SDI_CACHE_TYPE_CHAOS
;
1567 use_debug_dma_command (char *args
, int from_tty
)
1569 unsigned char buf
[3];
1571 buf
[0] = SDI_SET_ATTR
;
1572 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1573 buf
[2] = SDI_MEM_ACCESS_DEBUG_DMA
;
1578 use_mon_code_command (char *args
, int from_tty
)
1580 unsigned char buf
[3];
1582 buf
[0] = SDI_SET_ATTR
;
1583 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1584 buf
[2] = SDI_MEM_ACCESS_MON_CODE
;
1590 use_ib_breakpoints_command (char *args
, int from_tty
)
1592 use_ib_breakpoints
= 1;
1596 use_dbt_breakpoints_command (char *args
, int from_tty
)
1598 use_ib_breakpoints
= 0;
1602 m32r_return_one (struct target_ops
*target
)
1607 /* Implementation of the to_has_execution method. */
1610 m32r_has_execution (struct target_ops
*target
, ptid_t the_ptid
)
1615 /* Define the target subroutine names. */
1617 struct target_ops m32r_ops
;
1620 init_m32r_ops (void)
1622 m32r_ops
.to_shortname
= "m32rsdi";
1623 m32r_ops
.to_longname
= "Remote M32R debugging over SDI interface";
1624 m32r_ops
.to_doc
= "Use an M32R board using SDI debugging protocol.";
1625 m32r_ops
.to_open
= m32r_open
;
1626 m32r_ops
.to_close
= m32r_close
;
1627 m32r_ops
.to_detach
= m32r_detach
;
1628 m32r_ops
.to_resume
= m32r_resume
;
1629 m32r_ops
.to_wait
= m32r_wait
;
1630 m32r_ops
.to_fetch_registers
= m32r_fetch_register
;
1631 m32r_ops
.to_store_registers
= m32r_store_register
;
1632 m32r_ops
.to_prepare_to_store
= m32r_prepare_to_store
;
1633 m32r_ops
.deprecated_xfer_memory
= m32r_xfer_memory
;
1634 m32r_ops
.to_files_info
= m32r_files_info
;
1635 m32r_ops
.to_insert_breakpoint
= m32r_insert_breakpoint
;
1636 m32r_ops
.to_remove_breakpoint
= m32r_remove_breakpoint
;
1637 m32r_ops
.to_can_use_hw_breakpoint
= m32r_can_use_hw_watchpoint
;
1638 m32r_ops
.to_insert_watchpoint
= m32r_insert_watchpoint
;
1639 m32r_ops
.to_remove_watchpoint
= m32r_remove_watchpoint
;
1640 m32r_ops
.to_stopped_by_watchpoint
= m32r_stopped_by_watchpoint
;
1641 m32r_ops
.to_stopped_data_address
= m32r_stopped_data_address
;
1642 m32r_ops
.to_kill
= m32r_kill
;
1643 m32r_ops
.to_load
= m32r_load
;
1644 m32r_ops
.to_create_inferior
= m32r_create_inferior
;
1645 m32r_ops
.to_mourn_inferior
= m32r_mourn_inferior
;
1646 m32r_ops
.to_stop
= m32r_stop
;
1647 m32r_ops
.to_log_command
= serial_log_command
;
1648 m32r_ops
.to_thread_alive
= m32r_thread_alive
;
1649 m32r_ops
.to_pid_to_str
= m32r_pid_to_str
;
1650 m32r_ops
.to_stratum
= process_stratum
;
1651 m32r_ops
.to_has_all_memory
= m32r_return_one
;
1652 m32r_ops
.to_has_memory
= m32r_return_one
;
1653 m32r_ops
.to_has_stack
= m32r_return_one
;
1654 m32r_ops
.to_has_registers
= m32r_return_one
;
1655 m32r_ops
.to_has_execution
= m32r_has_execution
;
1656 m32r_ops
.to_magic
= OPS_MAGIC
;
1660 extern initialize_file_ftype _initialize_remote_m32r
;
1663 _initialize_remote_m32r (void)
1669 /* Initialize breakpoints. */
1670 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1671 bp_address
[i
] = 0xffffffff;
1673 /* Initialize access breaks. */
1674 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1675 ab_address
[i
] = 0x00000000;
1677 add_target (&m32r_ops
);
1679 add_com ("sdireset", class_obscure
, sdireset_command
,
1680 _("Reset SDI connection."));
1682 add_com ("sdistatus", class_obscure
, sdistatus_command
,
1683 _("Show status of SDI connection."));
1685 add_com ("debug_chaos", class_obscure
, debug_chaos_command
,
1686 _("Debug M32R/Chaos."));
1688 add_com ("use_debug_dma", class_obscure
, use_debug_dma_command
,
1689 _("Use debug DMA mem access."));
1690 add_com ("use_mon_code", class_obscure
, use_mon_code_command
,
1691 _("Use mon code mem access."));
1693 add_com ("use_ib_break", class_obscure
, use_ib_breakpoints_command
,
1694 _("Set breakpoints by IB break."));
1695 add_com ("use_dbt_break", class_obscure
, use_dbt_breakpoints_command
,
1696 _("Set breakpoints by dbt."));
1698 /* Yes, 42000 is arbitrary. The only sense out of it, is that it
1700 remote_m32r_ptid
= ptid_build (42000, 0, 42000);