1 /* Remote debugging interface for M32R/SDI.
3 Copyright 2003, 2004 Free Software Foundation, Inc.
5 Contributed by Renesas Technology Co.
6 Written by Kei Sakamoto <sakamoto.kei@renesas.com>.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
31 #include "gdb_string.h"
34 #include <netinet/in.h>
35 #include <sys/types.h>
43 /* Descriptor for I/O to remote machine. */
45 static struct serial *sdi_desc = NULL;
47 #define SDI_TIMEOUT 30
52 static char chip_name[64];
55 static unsigned long last_pc_addr = 0xffffffff;
56 static unsigned char last_pc_addr_data[2];
58 static int mmu_on = 0;
60 static int use_ib_breakpoints = 1;
62 #define MAX_BREAKPOINTS 1024
63 static int max_ib_breakpoints;
64 static unsigned long bp_address[MAX_BREAKPOINTS];
65 static unsigned char bp_data[MAX_BREAKPOINTS][4];
68 static const unsigned char dbt_bp_entry[] = {
69 0x10, 0xe0, 0x70, 0x00
72 #define MAX_ACCESS_BREAKS 4
73 static int max_access_breaks;
74 static unsigned long ab_address[MAX_ACCESS_BREAKS];
75 static unsigned int ab_type[MAX_ACCESS_BREAKS];
76 static unsigned int ab_size[MAX_ACCESS_BREAKS];
77 static CORE_ADDR hit_watchpoint_addr = 0;
79 static int interrupted = 0;
81 /* Forward data declarations */
82 extern struct target_ops m32r_ops;
89 #define SDI_READ_CPU_REG 4
90 #define SDI_WRITE_CPU_REG 5
91 #define SDI_READ_MEMORY 6
92 #define SDI_WRITE_MEMORY 7
93 #define SDI_EXEC_CPU 8
94 #define SDI_STOP_CPU 9
95 #define SDI_WAIT_FOR_READY 10
96 #define SDI_GET_ATTR 11
97 #define SDI_SET_ATTR 12
101 #define SDI_ATTR_NAME 1
102 #define SDI_ATTR_BRK 2
103 #define SDI_ATTR_ABRK 3
104 #define SDI_ATTR_CACHE 4
105 #define SDI_CACHE_TYPE_M32102 0
106 #define SDI_CACHE_TYPE_CHAOS 1
107 #define SDI_ATTR_MEM_ACCESS 5
108 #define SDI_MEM_ACCESS_DEBUG_DMA 0
109 #define SDI_MEM_ACCESS_MON_CODE 1
122 #define SDI_REG_R10 10
123 #define SDI_REG_R11 11
124 #define SDI_REG_R12 12
125 #define SDI_REG_FP 13
126 #define SDI_REG_LR 14
127 #define SDI_REG_SP 15
128 #define SDI_REG_PSW 16
129 #define SDI_REG_CBR 17
130 #define SDI_REG_SPI 18
131 #define SDI_REG_SPU 19
132 #define SDI_REG_CR4 20
133 #define SDI_REG_EVB 21
134 #define SDI_REG_BPC 22
135 #define SDI_REG_CR7 23
136 #define SDI_REG_BBPSW 24
137 #define SDI_REG_CR9 25
138 #define SDI_REG_CR10 26
139 #define SDI_REG_CR11 27
140 #define SDI_REG_CR12 28
141 #define SDI_REG_WR 29
142 #define SDI_REG_BBPC 30
143 #define SDI_REG_PBP 31
144 #define SDI_REG_ACCH 32
145 #define SDI_REG_ACCL 33
146 #define SDI_REG_ACC1H 34
147 #define SDI_REG_ACC1L 35
150 /* Low level communication functions */
152 /* Check an ack packet from the target */
161 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
166 if (c != '+') /* error */
172 /* Send data to the target and check an ack packet */
174 send_data (void *buf, int len)
181 if (serial_write (sdi_desc, buf, len) != 0)
184 if (get_ack () == -1)
190 /* Receive data from the target */
192 recv_data (void *buf, int len)
202 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
207 ((unsigned char *) buf)[total++] = c;
213 /* Store unsigned long parameter on packet */
215 store_long_parameter (void *buf, long val)
218 memcpy (buf, &val, 4);
222 send_cmd (unsigned char cmd)
224 unsigned char buf[1];
226 return send_data (buf, 1);
230 send_one_arg_cmd (unsigned char cmd, unsigned char arg1)
232 unsigned char buf[2];
235 return send_data (buf, 2);
239 send_two_arg_cmd (unsigned char cmd, unsigned char arg1, unsigned long arg2)
241 unsigned char buf[6];
244 store_long_parameter (buf + 2, arg2);
245 return send_data (buf, 6);
249 send_three_arg_cmd (unsigned char cmd, unsigned long arg1, unsigned long arg2,
252 unsigned char buf[13];
254 store_long_parameter (buf + 1, arg1);
255 store_long_parameter (buf + 5, arg2);
256 store_long_parameter (buf + 9, arg3);
257 return send_data (buf, 13);
261 recv_char_data (void)
269 recv_long_data (void)
277 /* Check if MMU is on */
279 check_mmu_status (void)
283 /* Read PC address */
284 if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC) == -1)
286 val = recv_long_data ();
287 if ((val & 0xc0000000) == 0x80000000)
293 /* Read EVB address */
294 if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_EVB) == -1)
296 val = recv_long_data ();
297 if ((val & 0xc0000000) == 0x80000000)
307 /* This is called not only when we first attach, but also when the
308 user types "run" after having attached. */
310 m32r_create_inferior (char *execfile, char *args, char **env, int from_tty)
315 error (_("Cannot pass arguments to remote STDEBUG process"));
317 if (execfile == 0 || exec_bfd == 0)
318 error (_("No executable file specified"));
321 fprintf_unfiltered (gdb_stdlog, "m32r_create_inferior(%s,%s)\n", execfile,
324 entry_pt = bfd_get_start_address (exec_bfd);
326 /* The "process" (board) is already stopped awaiting our commands, and
327 the program is already downloaded. We just set its PC and go. */
329 clear_proceed_status ();
331 /* Tell wait_for_inferior that we've started a new process. */
332 init_wait_for_inferior ();
334 /* Set up the "saved terminal modes" of the inferior
335 based on what modes we are starting it with. */
336 target_terminal_init ();
338 /* Install inferior's terminal modes. */
339 target_terminal_inferior ();
341 proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
344 /* Open a connection to a remote debugger.
345 NAME is the filename used for communication. */
348 m32r_open (char *args, int from_tty)
350 struct hostent *host_ent;
351 struct sockaddr_in server_addr;
352 char *port_str, hostname[256];
358 fprintf_unfiltered (gdb_stdlog, "m32r_open(%d)\n", from_tty);
360 target_preopen (from_tty);
362 push_target (&m32r_ops);
365 sprintf (hostname, "localhost:%d", SDIPORT);
368 port_str = strchr (args, ':');
369 if (port_str == NULL)
370 sprintf (hostname, "%s:%d", args, SDIPORT);
372 strcpy (hostname, args);
375 sdi_desc = serial_open (hostname);
377 error (_("Connection refused."));
379 if (get_ack () == -1)
380 error (_("Cannot connect to SDI target."));
382 if (send_cmd (SDI_OPEN) == -1)
383 error (_("Cannot connect to SDI target."));
385 /* Get maximum number of ib breakpoints */
386 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_BRK);
387 max_ib_breakpoints = recv_char_data ();
389 printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints);
391 /* Initialize breakpoints. */
392 for (i = 0; i < MAX_BREAKPOINTS; i++)
393 bp_address[i] = 0xffffffff;
395 /* Get maximum number of access breaks. */
396 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_ABRK);
397 max_access_breaks = recv_char_data ();
399 printf_filtered ("Max Access Breaks = %d\n", max_access_breaks);
401 /* Initialize access breask. */
402 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
403 ab_address[i] = 0x00000000;
407 /* Get the name of chip on target board. */
408 send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_NAME);
409 recv_data (chip_name, 64);
412 printf_filtered ("Remote %s connected to %s\n", target_shortname,
416 /* Close out all files and local state before this target loses control. */
419 m32r_close (int quitting)
422 fprintf_unfiltered (gdb_stdlog, "m32r_close(%d)\n", quitting);
426 send_cmd (SDI_CLOSE);
427 serial_close (sdi_desc);
431 inferior_ptid = null_ptid;
435 /* Tell the remote machine to resume. */
438 m32r_resume (ptid_t ptid, int step, enum target_signal sig)
440 unsigned long pc_addr, bp_addr, ab_addr;
442 unsigned char buf[13];
448 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(step)\n");
450 fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(cont)\n");
455 pc_addr = read_pc ();
457 fprintf_unfiltered (gdb_stdlog, "pc <= 0x%lx\n", pc_addr);
459 /* At pc address there is a parallel instruction with +2 offset,
460 so we have to make it a serial instruction or avoid it. */
461 if (pc_addr == last_pc_addr)
463 /* Avoid a parallel nop. */
464 if (last_pc_addr_data[0] == 0xf0 && last_pc_addr_data[1] == 0x00)
467 /* Now we can forget this instruction. */
468 last_pc_addr = 0xffffffff;
470 /* Clear a parallel bit. */
473 buf[0] = SDI_WRITE_MEMORY;
474 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
475 store_long_parameter (buf + 1, pc_addr);
477 store_long_parameter (buf + 1, pc_addr - 1);
478 store_long_parameter (buf + 5, 1);
479 buf[9] = last_pc_addr_data[0] & 0x7f;
485 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
492 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, pc_addr | 1);
497 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, 0x00000000);
500 if (use_ib_breakpoints)
501 ib_breakpoints = max_ib_breakpoints;
505 /* Set ib breakpoints. */
506 for (i = 0; i < ib_breakpoints; i++)
508 bp_addr = bp_address[i];
510 if (bp_addr == 0xffffffff)
514 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
515 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
518 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
521 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8080 + 4 * i, 4, bp_addr);
524 /* Set dbt breakpoints. */
525 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
527 bp_addr = bp_address[i];
529 if (bp_addr == 0xffffffff)
533 bp_addr &= 0x7fffffff;
535 /* Write DBT instruction. */
536 buf[0] = SDI_WRITE_MEMORY;
537 store_long_parameter (buf + 1, bp_addr);
538 store_long_parameter (buf + 5, 4);
539 if ((bp_addr & 2) == 0 && bp_addr != (pc_addr & 0xfffffffc))
541 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
543 buf[9] = dbt_bp_entry[0];
544 buf[10] = dbt_bp_entry[1];
545 buf[11] = dbt_bp_entry[2];
546 buf[12] = dbt_bp_entry[3];
550 buf[9] = dbt_bp_entry[3];
551 buf[10] = dbt_bp_entry[2];
552 buf[11] = dbt_bp_entry[1];
553 buf[12] = dbt_bp_entry[0];
558 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
560 if ((bp_addr & 2) == 0)
562 buf[9] = dbt_bp_entry[0];
563 buf[10] = dbt_bp_entry[1];
564 buf[11] = bp_data[i][2] & 0x7f;
565 buf[12] = bp_data[i][3];
569 buf[9] = bp_data[i][0];
570 buf[10] = bp_data[i][1];
571 buf[11] = dbt_bp_entry[0];
572 buf[12] = dbt_bp_entry[1];
577 if ((bp_addr & 2) == 0)
579 buf[9] = bp_data[i][0];
580 buf[10] = bp_data[i][1] & 0x7f;
581 buf[11] = dbt_bp_entry[1];
582 buf[12] = dbt_bp_entry[0];
586 buf[9] = dbt_bp_entry[1];
587 buf[10] = dbt_bp_entry[0];
588 buf[11] = bp_data[i][2];
589 buf[12] = bp_data[i][3];
596 /* Set access breaks. */
597 for (i = 0; i < max_access_breaks; i++)
599 ab_addr = ab_address[i];
601 if (ab_addr == 0x00000000)
605 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
609 case 0: /* write watch */
610 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
613 case 1: /* read watch */
614 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
617 case 2: /* access watch */
618 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
627 case 0: /* write watch */
628 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
631 case 1: /* read watch */
632 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
635 case 2: /* access watch */
636 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
643 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8180 + 4 * i, 4, ab_addr);
646 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8200 + 4 * i, 4,
650 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8280 + 4 * i, 4,
654 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8300 + 4 * i, 4,
658 /* Resume program. */
659 send_cmd (SDI_EXEC_CPU);
661 /* Without this, some commands which require an active target (such as kill)
662 won't work. This variable serves (at least) double duty as both the pid
663 of the target process (if it has such), and as a flag indicating that a
664 target is active. These functions should be split out into seperate
665 variables, especially since GDB will someday have a notion of debugging
666 several processes. */
667 inferior_ptid = pid_to_ptid (32);
672 /* Wait until the remote machine stops, then return,
673 storing status in STATUS just as `wait' would. */
676 gdb_cntrl_c (int signo)
679 fprintf_unfiltered (gdb_stdlog, "interrupt\n");
684 m32r_wait (ptid_t ptid, struct target_waitstatus *status)
686 static RETSIGTYPE (*prev_sigint) ();
687 unsigned long bp_addr, pc_addr;
690 unsigned char buf[13];
695 fprintf_unfiltered (gdb_stdlog, "m32r_wait()\n");
697 status->kind = TARGET_WAITKIND_EXITED;
698 status->value.sig = 0;
701 prev_sigint = signal (SIGINT, gdb_cntrl_c);
704 buf[0] = SDI_WAIT_FOR_READY;
705 if (serial_write (sdi_desc, buf, 1) != 0)
706 error (_("Remote connection closed"));
710 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
712 error (_("Remote connection closed"));
714 if (c == '-') /* error */
716 status->kind = TARGET_WAITKIND_STOPPED;
717 status->value.sig = TARGET_SIGNAL_HUP;
718 return inferior_ptid;
720 else if (c == '+') /* stopped */
724 ret = serial_write (sdi_desc, "!", 1); /* packet to interrupt */
726 ret = serial_write (sdi_desc, ".", 1); /* packet to wait */
728 error (_("Remote connection closed"));
731 status->kind = TARGET_WAITKIND_STOPPED;
733 status->value.sig = TARGET_SIGNAL_INT;
735 status->value.sig = TARGET_SIGNAL_TRAP;
738 signal (SIGINT, prev_sigint);
742 /* Recover parallel bit. */
743 if (last_pc_addr != 0xffffffff)
745 buf[0] = SDI_WRITE_MEMORY;
746 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
747 store_long_parameter (buf + 1, last_pc_addr);
749 store_long_parameter (buf + 1, last_pc_addr - 1);
750 store_long_parameter (buf + 5, 1);
751 buf[9] = last_pc_addr_data[0];
753 last_pc_addr = 0xffffffff;
756 if (use_ib_breakpoints)
757 ib_breakpoints = max_ib_breakpoints;
761 /* Set back pc by 2 if m32r is stopped with dbt. */
762 last_pc_addr = 0xffffffff;
763 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC);
764 pc_addr = recv_long_data () - 2;
765 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
767 if (pc_addr == bp_address[i])
769 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
771 /* If there is a parallel instruction with +2 offset at pc
772 address, we have to take care of it later. */
773 if ((pc_addr & 0x2) != 0)
775 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
777 if ((bp_data[i][2] & 0x80) != 0)
779 last_pc_addr = pc_addr;
780 last_pc_addr_data[0] = bp_data[i][2];
781 last_pc_addr_data[1] = bp_data[i][3];
786 if ((bp_data[i][1] & 0x80) != 0)
788 last_pc_addr = pc_addr;
789 last_pc_addr_data[0] = bp_data[i][1];
790 last_pc_addr_data[1] = bp_data[i][0];
798 /* Remove ib breakpoints. */
799 for (i = 0; i < ib_breakpoints; i++)
801 if (bp_address[i] != 0xffffffff)
802 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
805 /* Remove dbt breakpoints. */
806 for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
808 bp_addr = bp_address[i];
809 if (bp_addr != 0xffffffff)
812 bp_addr &= 0x7fffffff;
813 buf[0] = SDI_READ_MEMORY;
814 store_long_parameter (buf + 1, bp_addr & 0xfffffffc);
815 store_long_parameter (buf + 5, 4);
816 buf[9] = bp_data[i][0];
817 buf[10] = bp_data[i][1];
818 buf[11] = bp_data[i][2];
819 buf[12] = bp_data[i][3];
824 /* Remove access breaks. */
825 hit_watchpoint_addr = 0;
826 for (i = 0; i < max_access_breaks; i++)
828 if (ab_address[i] != 0x00000000)
830 buf[0] = SDI_READ_MEMORY;
831 store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
832 store_long_parameter (buf + 5, 4);
833 serial_write (sdi_desc, buf, 9);
834 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
835 if (c != '-' && recv_data (buf, 4) != -1)
837 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
839 if ((buf[3] & 0x1) == 0x1)
840 hit_watchpoint_addr = ab_address[i];
844 if ((buf[0] & 0x1) == 0x1)
845 hit_watchpoint_addr = ab_address[i];
849 send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
855 fprintf_unfiltered (gdb_stdlog, "pc => 0x%lx\n", pc_addr);
857 return inferior_ptid;
860 /* Terminate the open connection to the remote debugger.
861 Use this when you want to detach and do something else
864 m32r_detach (char *args, int from_tty)
867 fprintf_unfiltered (gdb_stdlog, "m32r_detach(%d)\n", from_tty);
869 m32r_resume (inferior_ptid, 0, 0);
871 /* calls m32r_close to do the real work */
874 fprintf_unfiltered (gdb_stdlog, "Ending remote %s debugging\n",
878 /* Return the id of register number REGNO. */
881 get_reg_id (int regno)
900 /* Read the remote registers into the block REGS. */
902 static void m32r_fetch_register (int);
905 m32r_fetch_registers (void)
909 for (regno = 0; regno < NUM_REGS; regno++)
910 m32r_fetch_register (regno);
913 /* Fetch register REGNO, or all registers if REGNO is -1.
914 Returns errno value. */
916 m32r_fetch_register (int regno)
918 unsigned long val, val2, regid;
921 m32r_fetch_registers ();
924 char buffer[MAX_REGISTER_SIZE];
926 regid = get_reg_id (regno);
927 send_one_arg_cmd (SDI_READ_CPU_REG, regid);
928 val = recv_long_data ();
930 if (regid == SDI_REG_PSW)
932 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
933 val2 = recv_long_data ();
934 val = ((0x00c1 & val2) << 8) | ((0xc100 & val) >> 8);
938 fprintf_unfiltered (gdb_stdlog, "m32r_fetch_register(%d,0x%08lx)\n",
941 /* We got the number the register holds, but gdb expects to see a
942 value in the target byte ordering. */
943 store_unsigned_integer (buffer, 4, val);
944 regcache_raw_supply (current_regcache, regno, buffer);
949 /* Store the remote registers from the contents of the block REGS. */
951 static void m32r_store_register (int);
954 m32r_store_registers (void)
958 for (regno = 0; regno < NUM_REGS; regno++)
959 m32r_store_register (regno);
961 registers_changed ();
964 /* Store register REGNO, or all if REGNO == 0.
965 Return errno value. */
967 m32r_store_register (int regno)
970 ULONGEST regval, tmp;
973 m32r_store_registers ();
976 regcache_cooked_read_unsigned (current_regcache, regno, ®val);
977 regid = get_reg_id (regno);
979 if (regid == SDI_REG_PSW)
981 unsigned long psw, bbpsw;
983 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_PSW);
984 psw = recv_long_data ();
986 send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
987 bbpsw = recv_long_data ();
989 tmp = (0x00c1 & psw) | ((0x00c1 & regval) << 8);
990 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PSW, tmp);
992 tmp = (0x0030 & bbpsw) | ((0xc100 & regval) >> 8);
993 send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BBPSW, tmp);
997 send_two_arg_cmd (SDI_WRITE_CPU_REG, regid, regval);
1001 fprintf_unfiltered (gdb_stdlog, "m32r_store_register(%d,0x%08lu)\n",
1002 regno, (unsigned long) regval);
1006 /* Get ready to modify the registers array. On machines which store
1007 individual registers, this doesn't need to do anything. On machines
1008 which store all the registers in one fell swoop, this makes sure
1009 that registers contains all the registers from the program being
1013 m32r_prepare_to_store (void)
1015 /* Do nothing, since we can store individual regs */
1017 fprintf_unfiltered (gdb_stdlog, "m32r_prepare_to_store()\n");
1021 m32r_files_info (struct target_ops *target)
1023 char *file = "nothing";
1027 file = bfd_get_filename (exec_bfd);
1028 printf_filtered ("\tAttached to %s running program %s\n",
1033 /* Read/Write memory. */
1035 m32r_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
1037 struct mem_attrib *attrib, struct target_ops *target)
1039 unsigned long taddr;
1040 unsigned char buf[0x2000];
1047 if ((taddr & 0xa0000000) == 0x80000000)
1048 taddr &= 0x7fffffff;
1054 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%08lx,%d,write)\n",
1057 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%08lx,%d,read)\n",
1063 buf[0] = SDI_WRITE_MEMORY;
1064 store_long_parameter (buf + 1, taddr);
1065 store_long_parameter (buf + 5, len);
1068 memcpy (buf + 9, myaddr, len);
1069 ret = send_data (buf, len + 9) - 9;
1073 if (serial_write (sdi_desc, buf, 9) != 0)
1076 fprintf_unfiltered (gdb_stdlog,
1077 "m32r_xfer_memory() failed\n");
1080 ret = send_data (myaddr, len);
1085 buf[0] = SDI_READ_MEMORY;
1086 store_long_parameter (buf + 1, taddr);
1087 store_long_parameter (buf + 5, len);
1088 if (serial_write (sdi_desc, buf, 9) != 0)
1091 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1095 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1096 if (c < 0 || c == '-')
1099 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
1103 ret = recv_data (myaddr, len);
1109 fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() fails\n");
1120 fprintf_unfiltered (gdb_stdlog, "m32r_kill()\n");
1122 inferior_ptid = null_ptid;
1127 /* Clean up when a program exits.
1129 The program actually lives on in the remote processor's RAM, and may be
1130 run again without a download. Don't leave it full of breakpoint
1134 m32r_mourn_inferior (void)
1137 fprintf_unfiltered (gdb_stdlog, "m32r_mourn_inferior()\n");
1139 remove_breakpoints ();
1140 generic_mourn_inferior ();
1144 m32r_insert_breakpoint (CORE_ADDR addr, bfd_byte *shadow)
1147 unsigned char buf[13];
1151 fprintf_unfiltered (gdb_stdlog, "m32r_insert_breakpoint(%08lx,\"%s\")\n",
1154 if (use_ib_breakpoints)
1155 ib_breakpoints = max_ib_breakpoints;
1159 for (i = 0; i < MAX_BREAKPOINTS; i++)
1161 if (bp_address[i] == 0xffffffff)
1163 bp_address[i] = addr;
1164 if (i >= ib_breakpoints)
1166 buf[0] = SDI_READ_MEMORY;
1168 store_long_parameter (buf + 1, addr & 0xfffffffc);
1170 store_long_parameter (buf + 1, addr & 0x7ffffffc);
1171 store_long_parameter (buf + 5, 4);
1172 serial_write (sdi_desc, buf, 9);
1173 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1175 recv_data (bp_data[i], 4);
1181 error (_("Too many breakpoints"));
1186 m32r_remove_breakpoint (CORE_ADDR addr, bfd_byte *shadow)
1191 fprintf_unfiltered (gdb_stdlog, "m32r_remove_breakpoint(%08lx,\"%s\")\n",
1194 for (i = 0; i < MAX_BREAKPOINTS; i++)
1196 if (bp_address[i] == addr)
1198 bp_address[i] = 0xffffffff;
1207 m32r_load (char *args, int from_tty)
1209 struct cleanup *old_chain;
1216 time_t start_time, end_time; /* Start and end times of download */
1217 unsigned long data_count; /* Number of bytes transferred to memory */
1219 static RETSIGTYPE (*prev_sigint) ();
1221 /* for direct tcp connections, we can do a fast binary download */
1226 while (*args != '\000')
1230 while (isspace (*args))
1235 while ((*args != '\000') && !isspace (*args))
1238 if (*args != '\000')
1243 else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
1245 else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
1248 error (_("Unknown option `%s'"), arg);
1252 filename = get_exec_file (1);
1254 pbfd = bfd_openr (filename, gnutarget);
1257 perror_with_name (filename);
1260 old_chain = make_cleanup_bfd_close (pbfd);
1262 if (!bfd_check_format (pbfd, bfd_object))
1263 error (_("\"%s\" is not an object file: %s"), filename,
1264 bfd_errmsg (bfd_get_error ()));
1266 start_time = time (NULL);
1270 prev_sigint = signal (SIGINT, gdb_cntrl_c);
1272 for (section = pbfd->sections; section; section = section->next)
1274 if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
1276 bfd_vma section_address;
1277 bfd_size_type section_size;
1281 section_address = bfd_section_lma (pbfd, section);
1282 section_size = bfd_get_section_size (section);
1286 if ((section_address & 0xa0000000) == 0x80000000)
1287 section_address &= 0x7fffffff;
1291 printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
1292 bfd_get_section_name (pbfd, section),
1293 section_address, (int) section_size);
1297 data_count += section_size;
1300 while (section_size > 0)
1302 char unsigned buf[0x1000 + 9];
1305 count = min (section_size, 0x1000);
1307 buf[0] = SDI_WRITE_MEMORY;
1308 store_long_parameter (buf + 1, section_address);
1309 store_long_parameter (buf + 5, count);
1311 bfd_get_section_contents (pbfd, section, buf + 9, fptr, count);
1312 if (send_data (buf, count + 9) <= 0)
1313 error (_("Error while downloading %s section."),
1314 bfd_get_section_name (pbfd, section));
1318 printf_unfiltered (".");
1321 printf_unfiltered ("\n");
1324 gdb_flush (gdb_stdout);
1327 section_address += count;
1329 section_size -= count;
1335 if (!quiet && !interrupted)
1337 printf_unfiltered ("done.\n");
1338 gdb_flush (gdb_stdout);
1344 printf_unfiltered ("Interrupted.\n");
1350 signal (SIGINT, prev_sigint);
1352 end_time = time (NULL);
1354 /* Make the PC point at the start address */
1356 write_pc (bfd_get_start_address (exec_bfd));
1358 inferior_ptid = null_ptid; /* No process now */
1360 /* This is necessary because many things were based on the PC at the time
1361 that we attached to the monitor, which is no longer valid now that we
1362 have loaded new code (and just changed the PC). Another way to do this
1363 might be to call normal_stop, except that the stack may not be valid,
1364 and things would get horribly confused... */
1366 clear_symtab_users ();
1370 entry = bfd_get_start_address (pbfd);
1373 printf_unfiltered ("[Starting %s at 0x%lx]\n", filename, entry);
1376 print_transfer_performance (gdb_stdout, data_count, 0,
1377 end_time - start_time);
1379 do_cleanups (old_chain);
1386 fprintf_unfiltered (gdb_stdlog, "m32r_stop()\n");
1388 send_cmd (SDI_STOP_CPU);
1394 /* Tell whether this target can support a hardware breakpoint. CNT
1395 is the number of hardware breakpoints already installed. This
1396 implements the TARGET_CAN_USE_HARDWARE_WATCHPOINT macro. */
1399 m32r_can_use_hw_watchpoint (int type, int cnt, int othertype)
1401 return sdi_desc != NULL && cnt < max_access_breaks;
1404 /* Set a data watchpoint. ADDR and LEN should be obvious. TYPE is 0
1405 for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
1409 m32r_insert_watchpoint (CORE_ADDR addr, int len, int type)
1414 fprintf_unfiltered (gdb_stdlog, "m32r_insert_watchpoint(%08lx,%d,%d)\n",
1417 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1419 if (ab_address[i] == 0x00000000)
1421 ab_address[i] = addr;
1428 error (_("Too many watchpoints"));
1433 m32r_remove_watchpoint (CORE_ADDR addr, int len, int type)
1438 fprintf_unfiltered (gdb_stdlog, "m32r_remove_watchpoint(%08lx,%d,%d)\n",
1441 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1443 if (ab_address[i] == addr)
1445 ab_address[i] = 0x00000000;
1454 m32r_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
1457 if (hit_watchpoint_addr != 0x00000000)
1459 *addr_p = hit_watchpoint_addr;
1466 m32r_stopped_by_watchpoint (void)
1469 return m32r_stopped_data_address (¤t_target, &addr);
1474 sdireset_command (char *args, int from_tty)
1477 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1479 send_cmd (SDI_OPEN);
1481 inferior_ptid = null_ptid;
1486 sdistatus_command (char *args, int from_tty)
1488 unsigned char buf[4096];
1492 fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
1497 send_cmd (SDI_STATUS);
1498 for (i = 0; i < 4096; i++)
1500 c = serial_readchar (sdi_desc, SDI_TIMEOUT);
1508 printf_filtered ("%s", buf);
1513 debug_chaos_command (char *args, int from_tty)
1515 unsigned char buf[3];
1517 buf[0] = SDI_SET_ATTR;
1518 buf[1] = SDI_ATTR_CACHE;
1519 buf[2] = SDI_CACHE_TYPE_CHAOS;
1525 use_debug_dma_command (char *args, int from_tty)
1527 unsigned char buf[3];
1529 buf[0] = SDI_SET_ATTR;
1530 buf[1] = SDI_ATTR_MEM_ACCESS;
1531 buf[2] = SDI_MEM_ACCESS_DEBUG_DMA;
1536 use_mon_code_command (char *args, int from_tty)
1538 unsigned char buf[3];
1540 buf[0] = SDI_SET_ATTR;
1541 buf[1] = SDI_ATTR_MEM_ACCESS;
1542 buf[2] = SDI_MEM_ACCESS_MON_CODE;
1548 use_ib_breakpoints_command (char *args, int from_tty)
1550 use_ib_breakpoints = 1;
1554 use_dbt_breakpoints_command (char *args, int from_tty)
1556 use_ib_breakpoints = 0;
1560 /* Define the target subroutine names */
1562 struct target_ops m32r_ops;
1565 init_m32r_ops (void)
1567 m32r_ops.to_shortname = "m32rsdi";
1568 m32r_ops.to_longname = "Remote M32R debugging over SDI interface";
1569 m32r_ops.to_doc = "Use an M32R board using SDI debugging protocol.";
1570 m32r_ops.to_open = m32r_open;
1571 m32r_ops.to_close = m32r_close;
1572 m32r_ops.to_detach = m32r_detach;
1573 m32r_ops.to_resume = m32r_resume;
1574 m32r_ops.to_wait = m32r_wait;
1575 m32r_ops.to_fetch_registers = m32r_fetch_register;
1576 m32r_ops.to_store_registers = m32r_store_register;
1577 m32r_ops.to_prepare_to_store = m32r_prepare_to_store;
1578 m32r_ops.deprecated_xfer_memory = m32r_xfer_memory;
1579 m32r_ops.to_files_info = m32r_files_info;
1580 m32r_ops.to_insert_breakpoint = m32r_insert_breakpoint;
1581 m32r_ops.to_remove_breakpoint = m32r_remove_breakpoint;
1582 m32r_ops.to_can_use_hw_breakpoint = m32r_can_use_hw_watchpoint;
1583 m32r_ops.to_insert_watchpoint = m32r_insert_watchpoint;
1584 m32r_ops.to_remove_watchpoint = m32r_remove_watchpoint;
1585 m32r_ops.to_stopped_by_watchpoint = m32r_stopped_by_watchpoint;
1586 m32r_ops.to_stopped_data_address = m32r_stopped_data_address;
1587 m32r_ops.to_kill = m32r_kill;
1588 m32r_ops.to_load = m32r_load;
1589 m32r_ops.to_create_inferior = m32r_create_inferior;
1590 m32r_ops.to_mourn_inferior = m32r_mourn_inferior;
1591 m32r_ops.to_stop = m32r_stop;
1592 m32r_ops.to_stratum = process_stratum;
1593 m32r_ops.to_has_all_memory = 1;
1594 m32r_ops.to_has_memory = 1;
1595 m32r_ops.to_has_stack = 1;
1596 m32r_ops.to_has_registers = 1;
1597 m32r_ops.to_has_execution = 1;
1598 m32r_ops.to_magic = OPS_MAGIC;
1602 extern initialize_file_ftype _initialize_remote_m32r;
1605 _initialize_remote_m32r (void)
1611 /* Initialize breakpoints. */
1612 for (i = 0; i < MAX_BREAKPOINTS; i++)
1613 bp_address[i] = 0xffffffff;
1615 /* Initialize access breaks. */
1616 for (i = 0; i < MAX_ACCESS_BREAKS; i++)
1617 ab_address[i] = 0x00000000;
1619 add_target (&m32r_ops);
1621 add_com ("sdireset", class_obscure, sdireset_command,
1622 _("Reset SDI connection."));
1624 add_com ("sdistatus", class_obscure, sdistatus_command,
1625 _("Show status of SDI connection."));
1627 add_com ("debug_chaos", class_obscure, debug_chaos_command,
1628 _("Debug M32R/Chaos."));
1630 add_com ("use_debug_dma", class_obscure, use_debug_dma_command,
1631 _("Use debug DMA mem access."));
1632 add_com ("use_mon_code", class_obscure, use_mon_code_command,
1633 _("Use mon code mem access."));
1635 add_com ("use_ib_break", class_obscure, use_ib_breakpoints_command,
1636 _("Set breakpoints by IB break."));
1637 add_com ("use_dbt_break", class_obscure, use_dbt_breakpoints_command,
1638 _("Set breakpoints by dbt."));