1 /* Remote debugging interface for M32R/SDI.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 Contributed by Renesas Technology Co.
7 Written by Kei Sakamoto <sakamoto.kei@renesas.com>.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "gdb_string.h"
31 #include "gdbthread.h"
37 #include <netinet/in.h>
39 #include <sys/types.h>
47 /* Descriptor for I/O to remote machine. */
49 static struct serial
*sdi_desc
= NULL
;
51 #define SDI_TIMEOUT 30
56 static char chip_name
[64];
59 static unsigned long last_pc_addr
= 0xffffffff;
60 static unsigned char last_pc_addr_data
[2];
62 static int mmu_on
= 0;
64 static int use_ib_breakpoints
= 1;
66 #define MAX_BREAKPOINTS 1024
67 static int max_ib_breakpoints
;
68 static unsigned long bp_address
[MAX_BREAKPOINTS
];
69 static unsigned char bp_data
[MAX_BREAKPOINTS
][4];
72 static const unsigned char dbt_bp_entry
[] = {
73 0x10, 0xe0, 0x70, 0x00
76 #define MAX_ACCESS_BREAKS 4
77 static int max_access_breaks
;
78 static unsigned long ab_address
[MAX_ACCESS_BREAKS
];
79 static unsigned int ab_type
[MAX_ACCESS_BREAKS
];
80 static unsigned int ab_size
[MAX_ACCESS_BREAKS
];
81 static CORE_ADDR hit_watchpoint_addr
= 0;
83 static int interrupted
= 0;
85 /* Forward data declarations */
86 extern struct target_ops m32r_ops
;
88 /* This is the ptid we use while we're connected to the remote. Its
89 value is arbitrary, as the target doesn't have a notion of
90 processes or threads, but we need something non-null to place in
92 static ptid_t remote_m32r_ptid
;
98 #define SDI_READ_CPU_REG 4
99 #define SDI_WRITE_CPU_REG 5
100 #define SDI_READ_MEMORY 6
101 #define SDI_WRITE_MEMORY 7
102 #define SDI_EXEC_CPU 8
103 #define SDI_STOP_CPU 9
104 #define SDI_WAIT_FOR_READY 10
105 #define SDI_GET_ATTR 11
106 #define SDI_SET_ATTR 12
107 #define SDI_STATUS 13
110 #define SDI_ATTR_NAME 1
111 #define SDI_ATTR_BRK 2
112 #define SDI_ATTR_ABRK 3
113 #define SDI_ATTR_CACHE 4
114 #define SDI_CACHE_TYPE_M32102 0
115 #define SDI_CACHE_TYPE_CHAOS 1
116 #define SDI_ATTR_MEM_ACCESS 5
117 #define SDI_MEM_ACCESS_DEBUG_DMA 0
118 #define SDI_MEM_ACCESS_MON_CODE 1
131 #define SDI_REG_R10 10
132 #define SDI_REG_R11 11
133 #define SDI_REG_R12 12
134 #define SDI_REG_FP 13
135 #define SDI_REG_LR 14
136 #define SDI_REG_SP 15
137 #define SDI_REG_PSW 16
138 #define SDI_REG_CBR 17
139 #define SDI_REG_SPI 18
140 #define SDI_REG_SPU 19
141 #define SDI_REG_CR4 20
142 #define SDI_REG_EVB 21
143 #define SDI_REG_BPC 22
144 #define SDI_REG_CR7 23
145 #define SDI_REG_BBPSW 24
146 #define SDI_REG_CR9 25
147 #define SDI_REG_CR10 26
148 #define SDI_REG_CR11 27
149 #define SDI_REG_CR12 28
150 #define SDI_REG_WR 29
151 #define SDI_REG_BBPC 30
152 #define SDI_REG_PBP 31
153 #define SDI_REG_ACCH 32
154 #define SDI_REG_ACCL 33
155 #define SDI_REG_ACC1H 34
156 #define SDI_REG_ACC1L 35
159 /* Low level communication functions */
161 /* Check an ack packet from the target */
170 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
175 if (c
!= '+') /* error */
181 /* Send data to the target and check an ack packet */
183 send_data (void *buf
, int len
)
190 if (serial_write (sdi_desc
, buf
, len
) != 0)
193 if (get_ack () == -1)
199 /* Receive data from the target */
201 recv_data (void *buf
, int len
)
211 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
216 ((unsigned char *) buf
)[total
++] = c
;
222 /* Store unsigned long parameter on packet */
224 store_long_parameter (void *buf
, long val
)
227 memcpy (buf
, &val
, 4);
231 send_cmd (unsigned char cmd
)
233 unsigned char buf
[1];
235 return send_data (buf
, 1);
239 send_one_arg_cmd (unsigned char cmd
, unsigned char arg1
)
241 unsigned char buf
[2];
244 return send_data (buf
, 2);
248 send_two_arg_cmd (unsigned char cmd
, unsigned char arg1
, unsigned long arg2
)
250 unsigned char buf
[6];
253 store_long_parameter (buf
+ 2, arg2
);
254 return send_data (buf
, 6);
258 send_three_arg_cmd (unsigned char cmd
, unsigned long arg1
, unsigned long arg2
,
261 unsigned char buf
[13];
263 store_long_parameter (buf
+ 1, arg1
);
264 store_long_parameter (buf
+ 5, arg2
);
265 store_long_parameter (buf
+ 9, arg3
);
266 return send_data (buf
, 13);
270 recv_char_data (void)
278 recv_long_data (void)
286 /* Check if MMU is on */
288 check_mmu_status (void)
292 /* Read PC address */
293 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
) == -1)
295 val
= recv_long_data ();
296 if ((val
& 0xc0000000) == 0x80000000)
302 /* Read EVB address */
303 if (send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_EVB
) == -1)
305 val
= recv_long_data ();
306 if ((val
& 0xc0000000) == 0x80000000)
316 /* This is called not only when we first attach, but also when the
317 user types "run" after having attached. */
319 m32r_create_inferior (struct target_ops
*ops
, char *execfile
,
320 char *args
, char **env
, int from_tty
)
325 error (_("Cannot pass arguments to remote STDEBUG process"));
327 if (execfile
== 0 || exec_bfd
== 0)
328 error (_("No executable file specified"));
331 fprintf_unfiltered (gdb_stdlog
, "m32r_create_inferior(%s,%s)\n", execfile
,
334 entry_pt
= bfd_get_start_address (exec_bfd
);
336 /* The "process" (board) is already stopped awaiting our commands, and
337 the program is already downloaded. We just set its PC and go. */
339 clear_proceed_status ();
341 /* Tell wait_for_inferior that we've started a new process. */
342 init_wait_for_inferior ();
344 /* Set up the "saved terminal modes" of the inferior
345 based on what modes we are starting it with. */
346 target_terminal_init ();
348 /* Install inferior's terminal modes. */
349 target_terminal_inferior ();
354 /* Open a connection to a remote debugger.
355 NAME is the filename used for communication. */
358 m32r_open (char *args
, int from_tty
)
360 struct hostent
*host_ent
;
361 struct sockaddr_in server_addr
;
362 char *port_str
, hostname
[256];
368 fprintf_unfiltered (gdb_stdlog
, "m32r_open(%d)\n", from_tty
);
370 target_preopen (from_tty
);
372 push_target (&m32r_ops
);
375 sprintf (hostname
, "localhost:%d", SDIPORT
);
378 port_str
= strchr (args
, ':');
379 if (port_str
== NULL
)
380 sprintf (hostname
, "%s:%d", args
, SDIPORT
);
382 strcpy (hostname
, args
);
385 sdi_desc
= serial_open (hostname
);
387 error (_("Connection refused."));
389 if (get_ack () == -1)
390 error (_("Cannot connect to SDI target."));
392 if (send_cmd (SDI_OPEN
) == -1)
393 error (_("Cannot connect to SDI target."));
395 /* Get maximum number of ib breakpoints */
396 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_BRK
);
397 max_ib_breakpoints
= recv_char_data ();
399 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints
);
401 /* Initialize breakpoints. */
402 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
403 bp_address
[i
] = 0xffffffff;
405 /* Get maximum number of access breaks. */
406 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_ABRK
);
407 max_access_breaks
= recv_char_data ();
409 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks
);
411 /* Initialize access breask. */
412 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
413 ab_address
[i
] = 0x00000000;
417 /* Get the name of chip on target board. */
418 send_one_arg_cmd (SDI_GET_ATTR
, SDI_ATTR_NAME
);
419 recv_data (chip_name
, 64);
422 printf_filtered ("Remote %s connected to %s\n", target_shortname
,
426 /* Close out all files and local state before this target loses control. */
429 m32r_close (int quitting
)
432 fprintf_unfiltered (gdb_stdlog
, "m32r_close(%d)\n", quitting
);
436 send_cmd (SDI_CLOSE
);
437 serial_close (sdi_desc
);
441 inferior_ptid
= null_ptid
;
442 delete_thread_silent (remote_m32r_ptid
);
446 /* Tell the remote machine to resume. */
449 m32r_resume (ptid_t ptid
, int step
, enum target_signal sig
)
451 unsigned long pc_addr
, bp_addr
, ab_addr
;
453 unsigned char buf
[13];
459 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(step)\n");
461 fprintf_unfiltered (gdb_stdlog
, "\nm32r_resume(cont)\n");
466 pc_addr
= read_pc ();
468 fprintf_unfiltered (gdb_stdlog
, "pc <= 0x%lx\n", pc_addr
);
470 /* At pc address there is a parallel instruction with +2 offset,
471 so we have to make it a serial instruction or avoid it. */
472 if (pc_addr
== last_pc_addr
)
474 /* Avoid a parallel nop. */
475 if (last_pc_addr_data
[0] == 0xf0 && last_pc_addr_data
[1] == 0x00)
478 /* Now we can forget this instruction. */
479 last_pc_addr
= 0xffffffff;
481 /* Clear a parallel bit. */
484 buf
[0] = SDI_WRITE_MEMORY
;
485 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
486 store_long_parameter (buf
+ 1, pc_addr
);
488 store_long_parameter (buf
+ 1, pc_addr
- 1);
489 store_long_parameter (buf
+ 5, 1);
490 buf
[9] = last_pc_addr_data
[0] & 0x7f;
496 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
503 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, pc_addr
| 1);
508 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PBP
, 0x00000000);
511 if (use_ib_breakpoints
)
512 ib_breakpoints
= max_ib_breakpoints
;
516 /* Set ib breakpoints. */
517 for (i
= 0; i
< ib_breakpoints
; i
++)
519 bp_addr
= bp_address
[i
];
521 if (bp_addr
== 0xffffffff)
525 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
526 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
529 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
532 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8080 + 4 * i
, 4, bp_addr
);
535 /* Set dbt breakpoints. */
536 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
538 bp_addr
= bp_address
[i
];
540 if (bp_addr
== 0xffffffff)
544 bp_addr
&= 0x7fffffff;
546 /* Write DBT instruction. */
547 buf
[0] = SDI_WRITE_MEMORY
;
548 store_long_parameter (buf
+ 1, (bp_addr
& 0xfffffffc));
549 store_long_parameter (buf
+ 5, 4);
550 if ((bp_addr
& 2) == 0 && bp_addr
!= (pc_addr
& 0xfffffffc))
552 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
554 buf
[9] = dbt_bp_entry
[0];
555 buf
[10] = dbt_bp_entry
[1];
556 buf
[11] = dbt_bp_entry
[2];
557 buf
[12] = dbt_bp_entry
[3];
561 buf
[9] = dbt_bp_entry
[3];
562 buf
[10] = dbt_bp_entry
[2];
563 buf
[11] = dbt_bp_entry
[1];
564 buf
[12] = dbt_bp_entry
[0];
569 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
571 if ((bp_addr
& 2) == 0)
573 buf
[9] = dbt_bp_entry
[0];
574 buf
[10] = dbt_bp_entry
[1];
575 buf
[11] = bp_data
[i
][2] & 0x7f;
576 buf
[12] = bp_data
[i
][3];
580 buf
[9] = bp_data
[i
][0];
581 buf
[10] = bp_data
[i
][1];
582 buf
[11] = dbt_bp_entry
[0];
583 buf
[12] = dbt_bp_entry
[1];
588 if ((bp_addr
& 2) == 0)
590 buf
[9] = bp_data
[i
][0];
591 buf
[10] = bp_data
[i
][1] & 0x7f;
592 buf
[11] = dbt_bp_entry
[1];
593 buf
[12] = dbt_bp_entry
[0];
597 buf
[9] = dbt_bp_entry
[1];
598 buf
[10] = dbt_bp_entry
[0];
599 buf
[11] = bp_data
[i
][2];
600 buf
[12] = bp_data
[i
][3];
607 /* Set access breaks. */
608 for (i
= 0; i
< max_access_breaks
; i
++)
610 ab_addr
= ab_address
[i
];
612 if (ab_addr
== 0x00000000)
616 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
620 case 0: /* write watch */
621 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
624 case 1: /* read watch */
625 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
628 case 2: /* access watch */
629 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
638 case 0: /* write watch */
639 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
642 case 1: /* read watch */
643 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
646 case 2: /* access watch */
647 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
654 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8180 + 4 * i
, 4, ab_addr
);
657 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8200 + 4 * i
, 4,
661 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8280 + 4 * i
, 4,
665 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8300 + 4 * i
, 4,
669 /* Resume program. */
670 send_cmd (SDI_EXEC_CPU
);
672 /* Without this, some commands which require an active target (such as kill)
673 won't work. This variable serves (at least) double duty as both the pid
674 of the target process (if it has such), and as a flag indicating that a
675 target is active. These functions should be split out into seperate
676 variables, especially since GDB will someday have a notion of debugging
677 several processes. */
678 inferior_ptid
= remote_m32r_ptid
;
679 add_thread_silent (remote_m32r_ptid
);
684 /* Wait until the remote machine stops, then return,
685 storing status in STATUS just as `wait' would. */
688 gdb_cntrl_c (int signo
)
691 fprintf_unfiltered (gdb_stdlog
, "interrupt\n");
696 m32r_wait (ptid_t ptid
, struct target_waitstatus
*status
)
698 static RETSIGTYPE (*prev_sigint
) ();
699 unsigned long bp_addr
, pc_addr
;
702 unsigned char buf
[13];
707 fprintf_unfiltered (gdb_stdlog
, "m32r_wait()\n");
709 status
->kind
= TARGET_WAITKIND_EXITED
;
710 status
->value
.sig
= 0;
713 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
716 buf
[0] = SDI_WAIT_FOR_READY
;
717 if (serial_write (sdi_desc
, buf
, 1) != 0)
718 error (_("Remote connection closed"));
722 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
724 error (_("Remote connection closed"));
726 if (c
== '-') /* error */
728 status
->kind
= TARGET_WAITKIND_STOPPED
;
729 status
->value
.sig
= TARGET_SIGNAL_HUP
;
730 return inferior_ptid
;
732 else if (c
== '+') /* stopped */
736 ret
= serial_write (sdi_desc
, "!", 1); /* packet to interrupt */
738 ret
= serial_write (sdi_desc
, ".", 1); /* packet to wait */
740 error (_("Remote connection closed"));
743 status
->kind
= TARGET_WAITKIND_STOPPED
;
745 status
->value
.sig
= TARGET_SIGNAL_INT
;
747 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
750 signal (SIGINT
, prev_sigint
);
754 /* Recover parallel bit. */
755 if (last_pc_addr
!= 0xffffffff)
757 buf
[0] = SDI_WRITE_MEMORY
;
758 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
759 store_long_parameter (buf
+ 1, last_pc_addr
);
761 store_long_parameter (buf
+ 1, last_pc_addr
- 1);
762 store_long_parameter (buf
+ 5, 1);
763 buf
[9] = last_pc_addr_data
[0];
765 last_pc_addr
= 0xffffffff;
768 if (use_ib_breakpoints
)
769 ib_breakpoints
= max_ib_breakpoints
;
773 /* Set back pc by 2 if m32r is stopped with dbt. */
774 last_pc_addr
= 0xffffffff;
775 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BPC
);
776 pc_addr
= recv_long_data () - 2;
777 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
779 if (pc_addr
== bp_address
[i
])
781 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BPC
, pc_addr
);
783 /* If there is a parallel instruction with +2 offset at pc
784 address, we have to take care of it later. */
785 if ((pc_addr
& 0x2) != 0)
787 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
789 if ((bp_data
[i
][2] & 0x80) != 0)
791 last_pc_addr
= pc_addr
;
792 last_pc_addr_data
[0] = bp_data
[i
][2];
793 last_pc_addr_data
[1] = bp_data
[i
][3];
798 if ((bp_data
[i
][1] & 0x80) != 0)
800 last_pc_addr
= pc_addr
;
801 last_pc_addr_data
[0] = bp_data
[i
][1];
802 last_pc_addr_data
[1] = bp_data
[i
][0];
810 /* Remove ib breakpoints. */
811 for (i
= 0; i
< ib_breakpoints
; i
++)
813 if (bp_address
[i
] != 0xffffffff)
814 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8000 + 4 * i
, 4,
817 /* Remove dbt breakpoints. */
818 for (i
= ib_breakpoints
; i
< MAX_BREAKPOINTS
; i
++)
820 bp_addr
= bp_address
[i
];
821 if (bp_addr
!= 0xffffffff)
824 bp_addr
&= 0x7fffffff;
825 buf
[0] = SDI_WRITE_MEMORY
;
826 store_long_parameter (buf
+ 1, bp_addr
& 0xfffffffc);
827 store_long_parameter (buf
+ 5, 4);
828 buf
[9] = bp_data
[i
][0];
829 buf
[10] = bp_data
[i
][1];
830 buf
[11] = bp_data
[i
][2];
831 buf
[12] = bp_data
[i
][3];
836 /* Remove access breaks. */
837 hit_watchpoint_addr
= 0;
838 for (i
= 0; i
< max_access_breaks
; i
++)
840 if (ab_address
[i
] != 0x00000000)
842 buf
[0] = SDI_READ_MEMORY
;
843 store_long_parameter (buf
+ 1, 0xffff8100 + 4 * i
);
844 store_long_parameter (buf
+ 5, 4);
845 serial_write (sdi_desc
, buf
, 9);
846 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
847 if (c
!= '-' && recv_data (buf
, 4) != -1)
849 if (gdbarch_byte_order (target_gdbarch
) == BFD_ENDIAN_BIG
)
851 if ((buf
[3] & 0x1) == 0x1)
852 hit_watchpoint_addr
= ab_address
[i
];
856 if ((buf
[0] & 0x1) == 0x1)
857 hit_watchpoint_addr
= ab_address
[i
];
861 send_three_arg_cmd (SDI_WRITE_MEMORY
, 0xffff8100 + 4 * i
, 4,
867 fprintf_unfiltered (gdb_stdlog
, "pc => 0x%lx\n", pc_addr
);
869 return inferior_ptid
;
872 /* Terminate the open connection to the remote debugger.
873 Use this when you want to detach and do something else
876 m32r_detach (struct target_ops
*ops
, char *args
, int from_tty
)
879 fprintf_unfiltered (gdb_stdlog
, "m32r_detach(%d)\n", from_tty
);
881 m32r_resume (inferior_ptid
, 0, 0);
883 /* calls m32r_close to do the real work */
886 fprintf_unfiltered (gdb_stdlog
, "Ending remote %s debugging\n",
890 /* Return the id of register number REGNO. */
893 get_reg_id (int regno
)
912 /* Read the remote registers into the block REGS. */
914 static void m32r_fetch_register (struct regcache
*, int);
917 m32r_fetch_registers (struct regcache
*regcache
)
922 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
924 m32r_fetch_register (regcache
, regno
);
927 /* Fetch register REGNO, or all registers if REGNO is -1.
928 Returns errno value. */
930 m32r_fetch_register (struct regcache
*regcache
, int regno
)
932 unsigned long val
, val2
, regid
;
935 m32r_fetch_registers (regcache
);
938 char buffer
[MAX_REGISTER_SIZE
];
940 regid
= get_reg_id (regno
);
941 send_one_arg_cmd (SDI_READ_CPU_REG
, regid
);
942 val
= recv_long_data ();
944 if (regid
== SDI_REG_PSW
)
946 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
947 val2
= recv_long_data ();
948 val
= ((0x00cf & val2
) << 8) | ((0xcf00 & val
) >> 8);
952 fprintf_unfiltered (gdb_stdlog
, "m32r_fetch_register(%d,0x%08lx)\n",
955 /* We got the number the register holds, but gdb expects to see a
956 value in the target byte ordering. */
957 store_unsigned_integer (buffer
, 4, val
);
958 regcache_raw_supply (regcache
, regno
, buffer
);
963 /* Store the remote registers from the contents of the block REGS. */
965 static void m32r_store_register (struct regcache
*, int);
968 m32r_store_registers (struct regcache
*regcache
)
973 regno
< gdbarch_num_regs (get_regcache_arch (regcache
));
975 m32r_store_register (regcache
, regno
);
977 registers_changed ();
980 /* Store register REGNO, or all if REGNO == 0.
981 Return errno value. */
983 m32r_store_register (struct regcache
*regcache
, int regno
)
986 ULONGEST regval
, tmp
;
989 m32r_store_registers (regcache
);
992 regcache_cooked_read_unsigned (regcache
, regno
, ®val
);
993 regid
= get_reg_id (regno
);
995 if (regid
== SDI_REG_PSW
)
997 unsigned long psw
, bbpsw
;
999 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_PSW
);
1000 psw
= recv_long_data ();
1002 send_one_arg_cmd (SDI_READ_CPU_REG
, SDI_REG_BBPSW
);
1003 bbpsw
= recv_long_data ();
1005 tmp
= (0x00cf & psw
) | ((0x00cf & regval
) << 8);
1006 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_PSW
, tmp
);
1008 tmp
= (0x0030 & bbpsw
) | ((0xcf00 & regval
) >> 8);
1009 send_two_arg_cmd (SDI_WRITE_CPU_REG
, SDI_REG_BBPSW
, tmp
);
1013 send_two_arg_cmd (SDI_WRITE_CPU_REG
, regid
, regval
);
1017 fprintf_unfiltered (gdb_stdlog
, "m32r_store_register(%d,0x%08lu)\n",
1018 regno
, (unsigned long) regval
);
1022 /* Get ready to modify the registers array. On machines which store
1023 individual registers, this doesn't need to do anything. On machines
1024 which store all the registers in one fell swoop, this makes sure
1025 that registers contains all the registers from the program being
1029 m32r_prepare_to_store (struct regcache
*regcache
)
1031 /* Do nothing, since we can store individual regs */
1033 fprintf_unfiltered (gdb_stdlog
, "m32r_prepare_to_store()\n");
1037 m32r_files_info (struct target_ops
*target
)
1039 char *file
= "nothing";
1043 file
= bfd_get_filename (exec_bfd
);
1044 printf_filtered ("\tAttached to %s running program %s\n",
1049 /* Read/Write memory. */
1051 m32r_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
,
1053 struct mem_attrib
*attrib
, struct target_ops
*target
)
1055 unsigned long taddr
;
1056 unsigned char buf
[0x2000];
1063 if ((taddr
& 0xa0000000) == 0x80000000)
1064 taddr
&= 0x7fffffff;
1070 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%s,%d,write)\n",
1071 paddr (memaddr
), len
);
1073 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory(%s,%d,read)\n",
1074 paddr (memaddr
), len
);
1079 buf
[0] = SDI_WRITE_MEMORY
;
1080 store_long_parameter (buf
+ 1, taddr
);
1081 store_long_parameter (buf
+ 5, len
);
1084 memcpy (buf
+ 9, myaddr
, len
);
1085 ret
= send_data (buf
, len
+ 9) - 9;
1089 if (serial_write (sdi_desc
, buf
, 9) != 0)
1092 fprintf_unfiltered (gdb_stdlog
,
1093 "m32r_xfer_memory() failed\n");
1096 ret
= send_data (myaddr
, len
);
1101 buf
[0] = SDI_READ_MEMORY
;
1102 store_long_parameter (buf
+ 1, taddr
);
1103 store_long_parameter (buf
+ 5, len
);
1104 if (serial_write (sdi_desc
, buf
, 9) != 0)
1107 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1111 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1112 if (c
< 0 || c
== '-')
1115 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() failed\n");
1119 ret
= recv_data (myaddr
, len
);
1125 fprintf_unfiltered (gdb_stdlog
, "m32r_xfer_memory() fails\n");
1136 fprintf_unfiltered (gdb_stdlog
, "m32r_kill()\n");
1138 inferior_ptid
= null_ptid
;
1139 delete_thread_silent (remote_m32r_ptid
);
1144 /* Clean up when a program exits.
1146 The program actually lives on in the remote processor's RAM, and may be
1147 run again without a download. Don't leave it full of breakpoint
1151 m32r_mourn_inferior (struct target_ops
*ops
)
1154 fprintf_unfiltered (gdb_stdlog
, "m32r_mourn_inferior()\n");
1156 remove_breakpoints ();
1157 generic_mourn_inferior ();
1161 m32r_insert_breakpoint (struct bp_target_info
*bp_tgt
)
1163 CORE_ADDR addr
= bp_tgt
->placed_address
;
1165 unsigned char buf
[13];
1169 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_breakpoint(%s,...)\n",
1172 if (use_ib_breakpoints
)
1173 ib_breakpoints
= max_ib_breakpoints
;
1177 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1179 if (bp_address
[i
] == 0xffffffff)
1181 bp_address
[i
] = addr
;
1182 if (i
>= ib_breakpoints
)
1184 buf
[0] = SDI_READ_MEMORY
;
1186 store_long_parameter (buf
+ 1, addr
& 0xfffffffc);
1188 store_long_parameter (buf
+ 1, addr
& 0x7ffffffc);
1189 store_long_parameter (buf
+ 5, 4);
1190 serial_write (sdi_desc
, buf
, 9);
1191 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1193 recv_data (bp_data
[i
], 4);
1199 error (_("Too many breakpoints"));
1204 m32r_remove_breakpoint (struct bp_target_info
*bp_tgt
)
1206 CORE_ADDR addr
= bp_tgt
->placed_address
;
1210 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_breakpoint(%s)\n",
1213 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1215 if (bp_address
[i
] == addr
)
1217 bp_address
[i
] = 0xffffffff;
1226 m32r_load (char *args
, int from_tty
)
1228 struct cleanup
*old_chain
;
1235 struct timeval start_time
, end_time
;
1236 unsigned long data_count
; /* Number of bytes transferred to memory */
1238 static RETSIGTYPE (*prev_sigint
) ();
1240 /* for direct tcp connections, we can do a fast binary download */
1245 while (*args
!= '\000')
1249 while (isspace (*args
))
1254 while ((*args
!= '\000') && !isspace (*args
))
1257 if (*args
!= '\000')
1262 else if (strncmp (arg
, "-quiet", strlen (arg
)) == 0)
1264 else if (strncmp (arg
, "-nostart", strlen (arg
)) == 0)
1267 error (_("Unknown option `%s'"), arg
);
1271 filename
= get_exec_file (1);
1273 pbfd
= bfd_openr (filename
, gnutarget
);
1276 perror_with_name (filename
);
1279 old_chain
= make_cleanup_bfd_close (pbfd
);
1281 if (!bfd_check_format (pbfd
, bfd_object
))
1282 error (_("\"%s\" is not an object file: %s"), filename
,
1283 bfd_errmsg (bfd_get_error ()));
1285 gettimeofday (&start_time
, NULL
);
1289 prev_sigint
= signal (SIGINT
, gdb_cntrl_c
);
1291 for (section
= pbfd
->sections
; section
; section
= section
->next
)
1293 if (bfd_get_section_flags (pbfd
, section
) & SEC_LOAD
)
1295 bfd_vma section_address
;
1296 bfd_size_type section_size
;
1300 section_address
= bfd_section_lma (pbfd
, section
);
1301 section_size
= bfd_get_section_size (section
);
1305 if ((section_address
& 0xa0000000) == 0x80000000)
1306 section_address
&= 0x7fffffff;
1310 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1311 bfd_get_section_name (pbfd
, section
),
1312 (unsigned long) section_address
,
1313 (int) section_size
);
1317 data_count
+= section_size
;
1320 while (section_size
> 0)
1322 char unsigned buf
[0x1000 + 9];
1325 count
= min (section_size
, 0x1000);
1327 buf
[0] = SDI_WRITE_MEMORY
;
1328 store_long_parameter (buf
+ 1, section_address
);
1329 store_long_parameter (buf
+ 5, count
);
1331 bfd_get_section_contents (pbfd
, section
, buf
+ 9, fptr
, count
);
1332 if (send_data (buf
, count
+ 9) <= 0)
1333 error (_("Error while downloading %s section."),
1334 bfd_get_section_name (pbfd
, section
));
1338 printf_unfiltered (".");
1341 printf_unfiltered ("\n");
1344 gdb_flush (gdb_stdout
);
1347 section_address
+= count
;
1349 section_size
-= count
;
1355 if (!quiet
&& !interrupted
)
1357 printf_unfiltered ("done.\n");
1358 gdb_flush (gdb_stdout
);
1364 printf_unfiltered ("Interrupted.\n");
1370 signal (SIGINT
, prev_sigint
);
1372 gettimeofday (&end_time
, NULL
);
1374 /* Make the PC point at the start address */
1376 write_pc (bfd_get_start_address (exec_bfd
));
1378 inferior_ptid
= null_ptid
; /* No process now */
1379 delete_thread_silent (remote_m32r_ptid
);
1381 /* This is necessary because many things were based on the PC at the time
1382 that we attached to the monitor, which is no longer valid now that we
1383 have loaded new code (and just changed the PC). Another way to do this
1384 might be to call normal_stop, except that the stack may not be valid,
1385 and things would get horribly confused... */
1387 clear_symtab_users ();
1391 entry
= bfd_get_start_address (pbfd
);
1394 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename
,
1395 (unsigned long) entry
);
1398 print_transfer_performance (gdb_stdout
, data_count
, 0, &start_time
,
1401 do_cleanups (old_chain
);
1405 m32r_stop (ptid_t ptid
)
1408 fprintf_unfiltered (gdb_stdlog
, "m32r_stop()\n");
1410 send_cmd (SDI_STOP_CPU
);
1416 /* Tell whether this target can support a hardware breakpoint. CNT
1417 is the number of hardware breakpoints already installed. This
1418 implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro. */
1421 m32r_can_use_hw_watchpoint (int type
, int cnt
, int othertype
)
1423 return sdi_desc
!= NULL
&& cnt
< max_access_breaks
;
1426 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1427 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1431 m32r_insert_watchpoint (CORE_ADDR addr
, int len
, int type
)
1436 fprintf_unfiltered (gdb_stdlog
, "m32r_insert_watchpoint(%s,%d,%d)\n",
1437 paddr (addr
), len
, type
);
1439 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1441 if (ab_address
[i
] == 0x00000000)
1443 ab_address
[i
] = addr
;
1450 error (_("Too many watchpoints"));
1455 m32r_remove_watchpoint (CORE_ADDR addr
, int len
, int type
)
1460 fprintf_unfiltered (gdb_stdlog
, "m32r_remove_watchpoint(%s,%d,%d)\n",
1461 paddr (addr
), len
, type
);
1463 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1465 if (ab_address
[i
] == addr
)
1467 ab_address
[i
] = 0x00000000;
1476 m32r_stopped_data_address (struct target_ops
*target
, CORE_ADDR
*addr_p
)
1479 if (hit_watchpoint_addr
!= 0x00000000)
1481 *addr_p
= hit_watchpoint_addr
;
1488 m32r_stopped_by_watchpoint (void)
1491 return m32r_stopped_data_address (¤t_target
, &addr
);
1494 /* Check to see if a thread is still alive. */
1497 m32r_thread_alive (ptid_t ptid
)
1499 if (ptid_equal (ptid
, remote_m32r_ptid
))
1500 /* The main task is always alive. */
1506 /* Convert a thread ID to a string. Returns the string in a static
1510 m32r_pid_to_str (ptid_t ptid
)
1512 static char buf
[64];
1514 if (ptid_equal (remote_m32r_ptid
, ptid
))
1516 xsnprintf (buf
, sizeof buf
, "Thread <main>");
1520 return normal_pid_to_str (ptid
);
1524 sdireset_command (char *args
, int from_tty
)
1527 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1529 send_cmd (SDI_OPEN
);
1531 inferior_ptid
= null_ptid
;
1532 delete_thread_silent (remote_m32r_ptid
);
1537 sdistatus_command (char *args
, int from_tty
)
1539 unsigned char buf
[4096];
1543 fprintf_unfiltered (gdb_stdlog
, "m32r_sdireset()\n");
1548 send_cmd (SDI_STATUS
);
1549 for (i
= 0; i
< 4096; i
++)
1551 c
= serial_readchar (sdi_desc
, SDI_TIMEOUT
);
1559 printf_filtered ("%s", buf
);
1564 debug_chaos_command (char *args
, int from_tty
)
1566 unsigned char buf
[3];
1568 buf
[0] = SDI_SET_ATTR
;
1569 buf
[1] = SDI_ATTR_CACHE
;
1570 buf
[2] = SDI_CACHE_TYPE_CHAOS
;
1576 use_debug_dma_command (char *args
, int from_tty
)
1578 unsigned char buf
[3];
1580 buf
[0] = SDI_SET_ATTR
;
1581 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1582 buf
[2] = SDI_MEM_ACCESS_DEBUG_DMA
;
1587 use_mon_code_command (char *args
, int from_tty
)
1589 unsigned char buf
[3];
1591 buf
[0] = SDI_SET_ATTR
;
1592 buf
[1] = SDI_ATTR_MEM_ACCESS
;
1593 buf
[2] = SDI_MEM_ACCESS_MON_CODE
;
1599 use_ib_breakpoints_command (char *args
, int from_tty
)
1601 use_ib_breakpoints
= 1;
1605 use_dbt_breakpoints_command (char *args
, int from_tty
)
1607 use_ib_breakpoints
= 0;
1611 /* Define the target subroutine names */
1613 struct target_ops m32r_ops
;
1616 init_m32r_ops (void)
1618 m32r_ops
.to_shortname
= "m32rsdi";
1619 m32r_ops
.to_longname
= "Remote M32R debugging over SDI interface";
1620 m32r_ops
.to_doc
= "Use an M32R board using SDI debugging protocol.";
1621 m32r_ops
.to_open
= m32r_open
;
1622 m32r_ops
.to_close
= m32r_close
;
1623 m32r_ops
.to_detach
= m32r_detach
;
1624 m32r_ops
.to_resume
= m32r_resume
;
1625 m32r_ops
.to_wait
= m32r_wait
;
1626 m32r_ops
.to_fetch_registers
= m32r_fetch_register
;
1627 m32r_ops
.to_store_registers
= m32r_store_register
;
1628 m32r_ops
.to_prepare_to_store
= m32r_prepare_to_store
;
1629 m32r_ops
.deprecated_xfer_memory
= m32r_xfer_memory
;
1630 m32r_ops
.to_files_info
= m32r_files_info
;
1631 m32r_ops
.to_insert_breakpoint
= m32r_insert_breakpoint
;
1632 m32r_ops
.to_remove_breakpoint
= m32r_remove_breakpoint
;
1633 m32r_ops
.to_can_use_hw_breakpoint
= m32r_can_use_hw_watchpoint
;
1634 m32r_ops
.to_insert_watchpoint
= m32r_insert_watchpoint
;
1635 m32r_ops
.to_remove_watchpoint
= m32r_remove_watchpoint
;
1636 m32r_ops
.to_stopped_by_watchpoint
= m32r_stopped_by_watchpoint
;
1637 m32r_ops
.to_stopped_data_address
= m32r_stopped_data_address
;
1638 m32r_ops
.to_kill
= m32r_kill
;
1639 m32r_ops
.to_load
= m32r_load
;
1640 m32r_ops
.to_create_inferior
= m32r_create_inferior
;
1641 m32r_ops
.to_mourn_inferior
= m32r_mourn_inferior
;
1642 m32r_ops
.to_stop
= m32r_stop
;
1643 m32r_ops
.to_log_command
= serial_log_command
;
1644 m32r_ops
.to_thread_alive
= m32r_thread_alive
;
1645 m32r_ops
.to_pid_to_str
= m32r_pid_to_str
;
1646 m32r_ops
.to_stratum
= process_stratum
;
1647 m32r_ops
.to_has_all_memory
= 1;
1648 m32r_ops
.to_has_memory
= 1;
1649 m32r_ops
.to_has_stack
= 1;
1650 m32r_ops
.to_has_registers
= 1;
1651 m32r_ops
.to_has_execution
= 1;
1652 m32r_ops
.to_magic
= OPS_MAGIC
;
1656 extern initialize_file_ftype _initialize_remote_m32r
;
1659 _initialize_remote_m32r (void)
1665 /* Initialize breakpoints. */
1666 for (i
= 0; i
< MAX_BREAKPOINTS
; i
++)
1667 bp_address
[i
] = 0xffffffff;
1669 /* Initialize access breaks. */
1670 for (i
= 0; i
< MAX_ACCESS_BREAKS
; i
++)
1671 ab_address
[i
] = 0x00000000;
1673 add_target (&m32r_ops
);
1675 add_com ("sdireset", class_obscure
, sdireset_command
,
1676 _("Reset SDI connection."));
1678 add_com ("sdistatus", class_obscure
, sdistatus_command
,
1679 _("Show status of SDI connection."));
1681 add_com ("debug_chaos", class_obscure
, debug_chaos_command
,
1682 _("Debug M32R/Chaos."));
1684 add_com ("use_debug_dma", class_obscure
, use_debug_dma_command
,
1685 _("Use debug DMA mem access."));
1686 add_com ("use_mon_code", class_obscure
, use_mon_code_command
,
1687 _("Use mon code mem access."));
1689 add_com ("use_ib_break", class_obscure
, use_ib_breakpoints_command
,
1690 _("Set breakpoints by IB break."));
1691 add_com ("use_dbt_break", class_obscure
, use_dbt_breakpoints_command
,
1692 _("Set breakpoints by dbt."));
1694 /* Yes, 42000 is arbitrary. The only sense out of it, is that it
1696 remote_m32r_ptid
= ptid_build (42000, 0, 42000);