gdb/remote-m32r-sdi.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Remote debugging interface for M32R/SDI.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    Contributed by Renesas Technology Co.
  4.    Written by Kei Sakamoto <sakamoto.kei@renesas.com>.

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. #include "defs.h"
  17. #include "gdbcmd.h"
  18. #include "gdbcore.h"
  19. #include "inferior.h"
  20. #include "infrun.h"
  21. #include "target.h"
  22. #include "regcache.h"
  23. #include "gdbthread.h"
  24. #include <ctype.h>
  25. #include <signal.h>
  26. #ifdef __MINGW32__
  27. #include <winsock2.h>
  28. #else
  29. #include <netinet/in.h>
  30. #endif
  31. #include <sys/types.h>
  32. #include <sys/time.h>
  33. #include <time.h>
  34. #include "gdb_bfd.h"
  35. #include "cli/cli-utils.h"

  36. #include "serial.h"

  37. /* Descriptor for I/O to remote machine.  */

  38. static struct serial *sdi_desc = NULL;

  39. #define SDI_TIMEOUT 30


  40. #define SDIPORT 3232

  41. static char chip_name[64];

  42. static int step_mode;
  43. static unsigned long last_pc_addr = 0xffffffff;
  44. static unsigned char last_pc_addr_data[2];

  45. static int mmu_on = 0;

  46. static int use_ib_breakpoints = 1;

  47. #define MAX_BREAKPOINTS 1024
  48. static int max_ib_breakpoints;
  49. static unsigned long bp_address[MAX_BREAKPOINTS];
  50. static unsigned char bp_data[MAX_BREAKPOINTS][4];

  51. /* dbt -> nop */
  52. static const unsigned char dbt_bp_entry[] = {
  53.   0x10, 0xe0, 0x70, 0x00
  54. };

  55. #define MAX_ACCESS_BREAKS 4
  56. static int max_access_breaks;
  57. static unsigned long ab_address[MAX_ACCESS_BREAKS];
  58. static unsigned int ab_type[MAX_ACCESS_BREAKS];
  59. static unsigned int ab_size[MAX_ACCESS_BREAKS];
  60. static CORE_ADDR hit_watchpoint_addr = 0;

  61. static int interrupted = 0;

  62. /* Forward data declarations */
  63. extern struct target_ops m32r_ops;

  64. /* This is the ptid we use while we're connected to the remote.  Its
  65.    value is arbitrary, as the target doesn't have a notion of
  66.    processes or threads, but we need something non-null to place in
  67.    inferior_ptid.  */
  68. static ptid_t remote_m32r_ptid;

  69. /* Commands */
  70. #define SDI_OPEN                 1
  71. #define SDI_CLOSE                2
  72. #define SDI_RELEASE              3
  73. #define SDI_READ_CPU_REG         4
  74. #define SDI_WRITE_CPU_REG        5
  75. #define SDI_READ_MEMORY          6
  76. #define SDI_WRITE_MEMORY         7
  77. #define SDI_EXEC_CPU             8
  78. #define SDI_STOP_CPU             9
  79. #define SDI_WAIT_FOR_READY      10
  80. #define SDI_GET_ATTR            11
  81. #define SDI_SET_ATTR            12
  82. #define SDI_STATUS              13

  83. /* Attributes */
  84. #define SDI_ATTR_NAME            1
  85. #define SDI_ATTR_BRK             2
  86. #define SDI_ATTR_ABRK            3
  87. #define SDI_ATTR_CACHE           4
  88. #define SDI_CACHE_TYPE_M32102    0
  89. #define SDI_CACHE_TYPE_CHAOS     1
  90. #define SDI_ATTR_MEM_ACCESS      5
  91. #define SDI_MEM_ACCESS_DEBUG_DMA 0
  92. #define SDI_MEM_ACCESS_MON_CODE  1

  93. /* Registers */
  94. #define SDI_REG_R0               0
  95. #define SDI_REG_R1               1
  96. #define SDI_REG_R2               2
  97. #define SDI_REG_R3               3
  98. #define SDI_REG_R4               4
  99. #define SDI_REG_R5               5
  100. #define SDI_REG_R6               6
  101. #define SDI_REG_R7               7
  102. #define SDI_REG_R8               8
  103. #define SDI_REG_R9               9
  104. #define SDI_REG_R10             10
  105. #define SDI_REG_R11             11
  106. #define SDI_REG_R12             12
  107. #define SDI_REG_FP              13
  108. #define SDI_REG_LR              14
  109. #define SDI_REG_SP              15
  110. #define SDI_REG_PSW             16
  111. #define SDI_REG_CBR             17
  112. #define SDI_REG_SPI             18
  113. #define SDI_REG_SPU             19
  114. #define SDI_REG_CR4             20
  115. #define SDI_REG_EVB             21
  116. #define SDI_REG_BPC             22
  117. #define SDI_REG_CR7             23
  118. #define SDI_REG_BBPSW           24
  119. #define SDI_REG_CR9             25
  120. #define SDI_REG_CR10            26
  121. #define SDI_REG_CR11            27
  122. #define SDI_REG_CR12            28
  123. #define SDI_REG_WR              29
  124. #define SDI_REG_BBPC            30
  125. #define SDI_REG_PBP             31
  126. #define SDI_REG_ACCH            32
  127. #define SDI_REG_ACCL            33
  128. #define SDI_REG_ACC1H           34
  129. #define SDI_REG_ACC1L           35


  130. /* Low level communication functions.  */

  131. /* Check an ack packet from the target.  */
  132. static int
  133. get_ack (void)
  134. {
  135.   int c;

  136.   if (!sdi_desc)
  137.     return -1;

  138.   c = serial_readchar (sdi_desc, SDI_TIMEOUT);

  139.   if (c < 0)
  140.     return -1;

  141.   if (c != '+')                        /* error */
  142.     return -1;

  143.   return 0;
  144. }

  145. /* Send data to the target and check an ack packet.  */
  146. static int
  147. send_data (const void *buf, int len)
  148. {
  149.   if (!sdi_desc)
  150.     return -1;

  151.   if (serial_write (sdi_desc, buf, len) != 0)
  152.     return -1;

  153.   if (get_ack () == -1)
  154.     return -1;

  155.   return len;
  156. }

  157. /* Receive data from the target.  */
  158. static int
  159. recv_data (void *buf, int len)
  160. {
  161.   int total = 0;
  162.   int c;

  163.   if (!sdi_desc)
  164.     return -1;

  165.   while (total < len)
  166.     {
  167.       c = serial_readchar (sdi_desc, SDI_TIMEOUT);

  168.       if (c < 0)
  169.         return -1;

  170.       ((unsigned char *) buf)[total++] = c;
  171.     }

  172.   return len;
  173. }

  174. /* Store unsigned long parameter on packet.  */
  175. static void
  176. store_long_parameter (void *buf, long val)
  177. {
  178.   val = htonl (val);
  179.   memcpy (buf, &val, 4);
  180. }

  181. static int
  182. send_cmd (unsigned char cmd)
  183. {
  184.   unsigned char buf[1];

  185.   buf[0] = cmd;
  186.   return send_data (buf, 1);
  187. }

  188. static int
  189. send_one_arg_cmd (unsigned char cmd, unsigned char arg1)
  190. {
  191.   unsigned char buf[2];

  192.   buf[0] = cmd;
  193.   buf[1] = arg1;
  194.   return send_data (buf, 2);
  195. }

  196. static int
  197. send_two_arg_cmd (unsigned char cmd, unsigned char arg1, unsigned long arg2)
  198. {
  199.   unsigned char buf[6];

  200.   buf[0] = cmd;
  201.   buf[1] = arg1;
  202.   store_long_parameter (buf + 2, arg2);
  203.   return send_data (buf, 6);
  204. }

  205. static int
  206. send_three_arg_cmd (unsigned char cmd, unsigned long arg1, unsigned long arg2,
  207.                     unsigned long arg3)
  208. {
  209.   unsigned char buf[13];

  210.   buf[0] = cmd;
  211.   store_long_parameter (buf + 1, arg1);
  212.   store_long_parameter (buf + 5, arg2);
  213.   store_long_parameter (buf + 9, arg3);
  214.   return send_data (buf, 13);
  215. }

  216. static unsigned char
  217. recv_char_data (void)
  218. {
  219.   unsigned char val;

  220.   recv_data (&val, 1);
  221.   return val;
  222. }

  223. static unsigned long
  224. recv_long_data (void)
  225. {
  226.   unsigned long val;

  227.   recv_data (&val, 4);
  228.   return ntohl (val);
  229. }


  230. /* Check if MMU is on.  */
  231. static void
  232. check_mmu_status (void)
  233. {
  234.   unsigned long val;

  235.   /* Read PC address.  */
  236.   if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC) == -1)
  237.     return;
  238.   val = recv_long_data ();
  239.   if ((val & 0xc0000000) == 0x80000000)
  240.     {
  241.       mmu_on = 1;
  242.       return;
  243.     }

  244.   /* Read EVB address.  */
  245.   if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_EVB) == -1)
  246.     return;
  247.   val = recv_long_data ();
  248.   if ((val & 0xc0000000) == 0x80000000)
  249.     {
  250.       mmu_on = 1;
  251.       return;
  252.     }

  253.   mmu_on = 0;
  254. }


  255. /* This is called not only when we first attach, but also when the
  256.    user types "run" after having attached.  */
  257. static void
  258. m32r_create_inferior (struct target_ops *ops, char *execfile,
  259.                       char *args, char **env, int from_tty)
  260. {
  261.   CORE_ADDR entry_pt;

  262.   if (args && *args)
  263.     error (_("Cannot pass arguments to remote STDEBUG process"));

  264.   if (execfile == 0 || exec_bfd == 0)
  265.     error (_("No executable file specified"));

  266.   if (remote_debug)
  267.     fprintf_unfiltered (gdb_stdlog, "m32r_create_inferior(%s,%s)\n", execfile,
  268.                         args);

  269.   entry_pt = bfd_get_start_address (exec_bfd);

  270.   /* The "process" (board) is already stopped awaiting our commands, and
  271.      the program is already downloaded.  We just set its PC and go.  */

  272.   clear_proceed_status (0);

  273.   /* Tell wait_for_inferior that we've started a new process.  */
  274.   init_wait_for_inferior ();

  275.   /* Set up the "saved terminal modes" of the inferior
  276.      based on what modes we are starting it with.  */
  277.   target_terminal_init ();

  278.   /* Install inferior's terminal modes.  */
  279.   target_terminal_inferior ();

  280.   regcache_write_pc (get_current_regcache (), entry_pt);
  281. }

  282. /* Open a connection to a remote debugger.
  283.    NAME is the filename used for communication.  */

  284. static void
  285. m32r_open (const char *args, int from_tty)
  286. {
  287.   struct hostent *host_ent;
  288.   struct sockaddr_in server_addr;
  289.   char *port_str, hostname[256];
  290.   int port;
  291.   int i, n;
  292.   int yes = 1;

  293.   if (remote_debug)
  294.     fprintf_unfiltered (gdb_stdlog, "m32r_open(%d)\n", from_tty);

  295.   target_preopen (from_tty);

  296.   push_target (&m32r_ops);

  297.   if (args == NULL)
  298.     xsnprintf (hostname, sizeof (hostname), "localhost:%d", SDIPORT);
  299.   else
  300.     {
  301.       port_str = strchr (args, ':');
  302.       if (port_str == NULL)
  303.         xsnprintf (hostname, sizeof (hostname), "%s:%d", args, SDIPORT);
  304.       else
  305.         xsnprintf (hostname, sizeof (hostname), "%s", args);
  306.     }

  307.   sdi_desc = serial_open (hostname);
  308.   if (!sdi_desc)
  309.     error (_("Connection refused."));

  310.   if (get_ack () == -1)
  311.     error (_("Cannot connect to SDI target."));

  312.   if (send_cmd (SDI_OPEN) == -1)
  313.     error (_("Cannot connect to SDI target."));

  314.   /* Get maximum number of ib breakpoints.  */
  315.   send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_BRK);
  316.   max_ib_breakpoints = recv_char_data ();
  317.   if (remote_debug)
  318.     printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints);

  319.   /* Initialize breakpoints.  */
  320.   for (i = 0; i < MAX_BREAKPOINTS; i++)
  321.     bp_address[i] = 0xffffffff;

  322.   /* Get maximum number of access breaks.  */
  323.   send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_ABRK);
  324.   max_access_breaks = recv_char_data ();
  325.   if (remote_debug)
  326.     printf_filtered ("Max Access Breaks = %d\n", max_access_breaks);

  327.   /* Initialize access breask.  */
  328.   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
  329.     ab_address[i] = 0x00000000;

  330.   check_mmu_status ();

  331.   /* Get the name of chip on target board.  */
  332.   send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_NAME);
  333.   recv_data (chip_name, 64);

  334.   if (from_tty)
  335.     printf_filtered ("Remote %s connected to %s\n", target_shortname,
  336.                      chip_name);
  337. }

  338. /* Close out all files and local state before this target loses control.  */

  339. static void
  340. m32r_close (struct target_ops *self)
  341. {
  342.   if (remote_debug)
  343.     fprintf_unfiltered (gdb_stdlog, "m32r_close()\n");

  344.   if (sdi_desc)
  345.     {
  346.       send_cmd (SDI_CLOSE);
  347.       serial_close (sdi_desc);
  348.       sdi_desc = NULL;
  349.     }

  350.   inferior_ptid = null_ptid;
  351.   delete_thread_silent (remote_m32r_ptid);
  352.   return;
  353. }

  354. /* Tell the remote machine to resume.  */

  355. static void
  356. m32r_resume (struct target_ops *ops,
  357.              ptid_t ptid, int step, enum gdb_signal sig)
  358. {
  359.   unsigned long pc_addr, bp_addr, ab_addr;
  360.   int ib_breakpoints;
  361.   unsigned char buf[13];
  362.   int i;

  363.   if (remote_debug)
  364.     {
  365.       if (step)
  366.         fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(step)\n");
  367.       else
  368.         fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(cont)\n");
  369.     }

  370.   check_mmu_status ();

  371.   pc_addr = regcache_read_pc (get_current_regcache ());
  372.   if (remote_debug)
  373.     fprintf_unfiltered (gdb_stdlog, "pc <= 0x%lx\n", pc_addr);

  374.   /* At pc address there is a parallel instruction with +2 offset,
  375.      so we have to make it a serial instruction or avoid it.  */
  376.   if (pc_addr == last_pc_addr)
  377.     {
  378.       /* Avoid a parallel nop.  */
  379.       if (last_pc_addr_data[0] == 0xf0 && last_pc_addr_data[1] == 0x00)
  380.         {
  381.           pc_addr += 2;
  382.           /* Now we can forget this instruction.  */
  383.           last_pc_addr = 0xffffffff;
  384.         }
  385.       /* Clear a parallel bit.  */
  386.       else
  387.         {
  388.           buf[0] = SDI_WRITE_MEMORY;
  389.           if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
  390.             store_long_parameter (buf + 1, pc_addr);
  391.           else
  392.             store_long_parameter (buf + 1, pc_addr - 1);
  393.           store_long_parameter (buf + 5, 1);
  394.           buf[9] = last_pc_addr_data[0] & 0x7f;
  395.           send_data (buf, 10);
  396.         }
  397.     }

  398.   /* Set PC.  */
  399.   send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);

  400.   /* step mode.  */
  401.   step_mode = step;
  402.   if (step)
  403.     {
  404.       /* Set PBP.  */
  405.       send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, pc_addr | 1);
  406.     }
  407.   else
  408.     {
  409.       /* Unset PBP.  */
  410.       send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, 0x00000000);
  411.     }

  412.   if (use_ib_breakpoints)
  413.     ib_breakpoints = max_ib_breakpoints;
  414.   else
  415.     ib_breakpoints = 0;

  416.   /* Set ib breakpoints.  */
  417.   for (i = 0; i < ib_breakpoints; i++)
  418.     {
  419.       bp_addr = bp_address[i];

  420.       if (bp_addr == 0xffffffff)
  421.         continue;

  422.       /* Set PBP.  */
  423.       if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
  424.         send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
  425.                             0x00000006);
  426.       else
  427.         send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
  428.                             0x06000000);

  429.       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8080 + 4 * i, 4, bp_addr);
  430.     }

  431.   /* Set dbt breakpoints.  */
  432.   for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
  433.     {
  434.       bp_addr = bp_address[i];

  435.       if (bp_addr == 0xffffffff)
  436.         continue;

  437.       if (!mmu_on)
  438.         bp_addr &= 0x7fffffff;

  439.       /* Write DBT instruction.  */
  440.       buf[0] = SDI_WRITE_MEMORY;
  441.       store_long_parameter (buf + 1, (bp_addr & 0xfffffffc));
  442.       store_long_parameter (buf + 5, 4);
  443.       if ((bp_addr & 2) == 0 && bp_addr != (pc_addr & 0xfffffffc))
  444.         {
  445.           if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
  446.             {
  447.               buf[9] = dbt_bp_entry[0];
  448.               buf[10] = dbt_bp_entry[1];
  449.               buf[11] = dbt_bp_entry[2];
  450.               buf[12] = dbt_bp_entry[3];
  451.             }
  452.           else
  453.             {
  454.               buf[9] = dbt_bp_entry[3];
  455.               buf[10] = dbt_bp_entry[2];
  456.               buf[11] = dbt_bp_entry[1];
  457.               buf[12] = dbt_bp_entry[0];
  458.             }
  459.         }
  460.       else
  461.         {
  462.           if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
  463.             {
  464.               if ((bp_addr & 2) == 0)
  465.                 {
  466.                   buf[9] = dbt_bp_entry[0];
  467.                   buf[10] = dbt_bp_entry[1];
  468.                   buf[11] = bp_data[i][2] & 0x7f;
  469.                   buf[12] = bp_data[i][3];
  470.                 }
  471.               else
  472.                 {
  473.                   buf[9] = bp_data[i][0];
  474.                   buf[10] = bp_data[i][1];
  475.                   buf[11] = dbt_bp_entry[0];
  476.                   buf[12] = dbt_bp_entry[1];
  477.                 }
  478.             }
  479.           else
  480.             {
  481.               if ((bp_addr & 2) == 0)
  482.                 {
  483.                   buf[9] = bp_data[i][0];
  484.                   buf[10] = bp_data[i][1] & 0x7f;
  485.                   buf[11] = dbt_bp_entry[1];
  486.                   buf[12] = dbt_bp_entry[0];
  487.                 }
  488.               else
  489.                 {
  490.                   buf[9] = dbt_bp_entry[1];
  491.                   buf[10] = dbt_bp_entry[0];
  492.                   buf[11] = bp_data[i][2];
  493.                   buf[12] = bp_data[i][3];
  494.                 }
  495.             }
  496.         }
  497.       send_data (buf, 13);
  498.     }

  499.   /* Set access breaks.  */
  500.   for (i = 0; i < max_access_breaks; i++)
  501.     {
  502.       ab_addr = ab_address[i];

  503.       if (ab_addr == 0x00000000)
  504.         continue;

  505.       /* DBC register.  */
  506.       if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
  507.         {
  508.           switch (ab_type[i])
  509.             {
  510.             case 0:                /* write watch */
  511.               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
  512.                                   0x00000086);
  513.               break;
  514.             case 1:                /* read watch */
  515.               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
  516.                                   0x00000046);
  517.               break;
  518.             case 2:                /* access watch */
  519.               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
  520.                                   0x00000006);
  521.               break;
  522.             }
  523.         }
  524.       else
  525.         {
  526.           switch (ab_type[i])
  527.             {
  528.             case 0:                /* write watch */
  529.               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
  530.                                   0x86000000);
  531.               break;
  532.             case 1:                /* read watch */
  533.               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
  534.                                   0x46000000);
  535.               break;
  536.             case 2:                /* access watch */
  537.               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
  538.                                   0x06000000);
  539.               break;
  540.             }
  541.         }

  542.       /* DBAH register.  */
  543.       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8180 + 4 * i, 4, ab_addr);

  544.       /* DBAL register.  */
  545.       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8200 + 4 * i, 4,
  546.                           0xffffffff);

  547.       /* DBD register.  */
  548.       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8280 + 4 * i, 4,
  549.                           0x00000000);

  550.       /* DBDM register.  */
  551.       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8300 + 4 * i, 4,
  552.                           0x00000000);
  553.     }

  554.   /* Resume program.  */
  555.   send_cmd (SDI_EXEC_CPU);

  556.   /* Without this, some commands which require an active target (such as kill)
  557.      won't work.  This variable serves (at least) double duty as both the pid
  558.      of the target process (if it has such), and as a flag indicating that a
  559.      target is active.  These functions should be split out into seperate
  560.      variables, especially since GDB will someday have a notion of debugging
  561.      several processes.  */
  562.   inferior_ptid = remote_m32r_ptid;
  563.   add_thread_silent (remote_m32r_ptid);

  564.   return;
  565. }

  566. /* Wait until the remote machine stops, then return,
  567.    storing status in STATUS just as `wait' would.  */

  568. static void
  569. gdb_cntrl_c (int signo)
  570. {
  571.   if (remote_debug)
  572.     fprintf_unfiltered (gdb_stdlog, "interrupt\n");
  573.   interrupted = 1;
  574. }

  575. static ptid_t
  576. m32r_wait (struct target_ops *ops,
  577.            ptid_t ptid, struct target_waitstatus *status, int options)
  578. {
  579.   static RETSIGTYPE (*prev_sigint) ();
  580.   unsigned long bp_addr, pc_addr;
  581.   int ib_breakpoints;
  582.   long i;
  583.   unsigned char buf[13];
  584.   int ret, c;

  585.   if (remote_debug)
  586.     fprintf_unfiltered (gdb_stdlog, "m32r_wait()\n");

  587.   status->kind = TARGET_WAITKIND_EXITED;
  588.   status->value.sig = GDB_SIGNAL_0;

  589.   interrupted = 0;
  590.   prev_sigint = signal (SIGINT, gdb_cntrl_c);

  591.   /* Wait for ready.  */
  592.   buf[0] = SDI_WAIT_FOR_READY;
  593.   if (serial_write (sdi_desc, buf, 1) != 0)
  594.     error (_("Remote connection closed"));

  595.   while (1)
  596.     {
  597.       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
  598.       if (c < 0)
  599.         error (_("Remote connection closed"));

  600.       if (c == '-')                /* error */
  601.         {
  602.           status->kind = TARGET_WAITKIND_STOPPED;
  603.           status->value.sig = GDB_SIGNAL_HUP;
  604.           return inferior_ptid;
  605.         }
  606.       else if (c == '+')        /* stopped */
  607.         break;

  608.       if (interrupted)
  609.         ret = serial_write (sdi_desc, "!", 1);        /* packet to interrupt */
  610.       else
  611.         ret = serial_write (sdi_desc, ".", 1);        /* packet to wait */
  612.       if (ret != 0)
  613.         error (_("Remote connection closed"));
  614.     }

  615.   status->kind = TARGET_WAITKIND_STOPPED;
  616.   if (interrupted)
  617.     status->value.sig = GDB_SIGNAL_INT;
  618.   else
  619.     status->value.sig = GDB_SIGNAL_TRAP;

  620.   interrupted = 0;
  621.   signal (SIGINT, prev_sigint);

  622.   check_mmu_status ();

  623.   /* Recover parallel bit.  */
  624.   if (last_pc_addr != 0xffffffff)
  625.     {
  626.       buf[0] = SDI_WRITE_MEMORY;
  627.       if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
  628.         store_long_parameter (buf + 1, last_pc_addr);
  629.       else
  630.         store_long_parameter (buf + 1, last_pc_addr - 1);
  631.       store_long_parameter (buf + 5, 1);
  632.       buf[9] = last_pc_addr_data[0];
  633.       send_data (buf, 10);
  634.       last_pc_addr = 0xffffffff;
  635.     }

  636.   if (use_ib_breakpoints)
  637.     ib_breakpoints = max_ib_breakpoints;
  638.   else
  639.     ib_breakpoints = 0;

  640.   /* Set back pc by 2 if m32r is stopped with dbt.  */
  641.   last_pc_addr = 0xffffffff;
  642.   send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC);
  643.   pc_addr = recv_long_data () - 2;
  644.   for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
  645.     {
  646.       if (pc_addr == bp_address[i])
  647.         {
  648.           send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);

  649.           /* If there is a parallel instruction with +2 offset at pc
  650.              address, we have to take care of it later.  */
  651.           if ((pc_addr & 0x2) != 0)
  652.             {
  653.               if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
  654.                 {
  655.                   if ((bp_data[i][2] & 0x80) != 0)
  656.                     {
  657.                       last_pc_addr = pc_addr;
  658.                       last_pc_addr_data[0] = bp_data[i][2];
  659.                       last_pc_addr_data[1] = bp_data[i][3];
  660.                     }
  661.                 }
  662.               else
  663.                 {
  664.                   if ((bp_data[i][1] & 0x80) != 0)
  665.                     {
  666.                       last_pc_addr = pc_addr;
  667.                       last_pc_addr_data[0] = bp_data[i][1];
  668.                       last_pc_addr_data[1] = bp_data[i][0];
  669.                     }
  670.                 }
  671.             }
  672.           break;
  673.         }
  674.     }

  675.   /* Remove ib breakpoints.  */
  676.   for (i = 0; i < ib_breakpoints; i++)
  677.     {
  678.       if (bp_address[i] != 0xffffffff)
  679.         send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
  680.                             0x00000000);
  681.     }
  682.   /* Remove dbt breakpoints.  */
  683.   for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
  684.     {
  685.       bp_addr = bp_address[i];
  686.       if (bp_addr != 0xffffffff)
  687.         {
  688.           if (!mmu_on)
  689.             bp_addr &= 0x7fffffff;
  690.           buf[0] = SDI_WRITE_MEMORY;
  691.           store_long_parameter (buf + 1, bp_addr & 0xfffffffc);
  692.           store_long_parameter (buf + 5, 4);
  693.           buf[9] = bp_data[i][0];
  694.           buf[10] = bp_data[i][1];
  695.           buf[11] = bp_data[i][2];
  696.           buf[12] = bp_data[i][3];
  697.           send_data (buf, 13);
  698.         }
  699.     }

  700.   /* Remove access breaks.  */
  701.   hit_watchpoint_addr = 0;
  702.   for (i = 0; i < max_access_breaks; i++)
  703.     {
  704.       if (ab_address[i] != 0x00000000)
  705.         {
  706.           buf[0] = SDI_READ_MEMORY;
  707.           store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
  708.           store_long_parameter (buf + 5, 4);
  709.           serial_write (sdi_desc, buf, 9);
  710.           c = serial_readchar (sdi_desc, SDI_TIMEOUT);
  711.           if (c != '-' && recv_data (buf, 4) != -1)
  712.             {
  713.               if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
  714.                 {
  715.                   if ((buf[3] & 0x1) == 0x1)
  716.                     hit_watchpoint_addr = ab_address[i];
  717.                 }
  718.               else
  719.                 {
  720.                   if ((buf[0] & 0x1) == 0x1)
  721.                     hit_watchpoint_addr = ab_address[i];
  722.                 }
  723.             }

  724.           send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
  725.                               0x00000000);
  726.         }
  727.     }

  728.   if (remote_debug)
  729.     fprintf_unfiltered (gdb_stdlog, "pc => 0x%lx\n", pc_addr);

  730.   return inferior_ptid;
  731. }

  732. /* Terminate the open connection to the remote debugger.
  733.    Use this when you want to detach and do something else
  734.    with your gdb.  */
  735. static void
  736. m32r_detach (struct target_ops *ops, const char *args, int from_tty)
  737. {
  738.   if (remote_debug)
  739.     fprintf_unfiltered (gdb_stdlog, "m32r_detach(%d)\n", from_tty);

  740.   m32r_resume (ops, inferior_ptid, 0, GDB_SIGNAL_0);

  741.   /* Calls m32r_close to do the real work.  */
  742.   unpush_target (ops);
  743.   if (from_tty)
  744.     fprintf_unfiltered (gdb_stdlog, "Ending remote %s debugging\n",
  745.                         target_shortname);
  746. }

  747. /* Return the id of register number REGNO.  */

  748. static int
  749. get_reg_id (int regno)
  750. {
  751.   switch (regno)
  752.     {
  753.     case 20:
  754.       return SDI_REG_BBPC;
  755.     case 21:
  756.       return SDI_REG_BPC;
  757.     case 22:
  758.       return SDI_REG_ACCL;
  759.     case 23:
  760.       return SDI_REG_ACCH;
  761.     case 24:
  762.       return SDI_REG_EVB;
  763.     }

  764.   return regno;
  765. }

  766. /* Fetch register REGNO, or all registers if REGNO is -1.
  767.    Returns errno value.  */
  768. static void
  769. m32r_fetch_register (struct target_ops *ops,
  770.                      struct regcache *regcache, int regno)
  771. {
  772.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  773.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  774.   unsigned long val, val2, regid;

  775.   if (regno == -1)
  776.     {
  777.       for (regno = 0;
  778.            regno < gdbarch_num_regs (get_regcache_arch (regcache));
  779.            regno++)
  780.         m32r_fetch_register (ops, regcache, regno);
  781.     }
  782.   else
  783.     {
  784.       gdb_byte buffer[MAX_REGISTER_SIZE];

  785.       regid = get_reg_id (regno);
  786.       send_one_arg_cmd (SDI_READ_CPU_REG, regid);
  787.       val = recv_long_data ();

  788.       if (regid == SDI_REG_PSW)
  789.         {
  790.           send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
  791.           val2 = recv_long_data ();
  792.           val = ((0x00cf & val2) << 8) | ((0xcf00 & val) >> 8);
  793.         }

  794.       if (remote_debug)
  795.         fprintf_unfiltered (gdb_stdlog, "m32r_fetch_register(%d,0x%08lx)\n",
  796.                             regno, val);

  797.       /* We got the number the register holds, but gdb expects to see a
  798.          value in the target byte ordering.  */
  799.       store_unsigned_integer (buffer, 4, byte_order, val);
  800.       regcache_raw_supply (regcache, regno, buffer);
  801.     }
  802.   return;
  803. }

  804. /* Store register REGNO, or all if REGNO == 0.
  805.    Return errno value.  */
  806. static void
  807. m32r_store_register (struct target_ops *ops,
  808.                      struct regcache *regcache, int regno)
  809. {
  810.   int regid;
  811.   ULONGEST regval, tmp;

  812.   if (regno == -1)
  813.     {
  814.       for (regno = 0;
  815.            regno < gdbarch_num_regs (get_regcache_arch (regcache));
  816.            regno++)
  817.         m32r_store_register (ops, regcache, regno);
  818.     }
  819.   else
  820.     {
  821.       regcache_cooked_read_unsigned (regcache, regno, &regval);
  822.       regid = get_reg_id (regno);

  823.       if (regid == SDI_REG_PSW)
  824.         {
  825.           unsigned long psw, bbpsw;

  826.           send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_PSW);
  827.           psw = recv_long_data ();

  828.           send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
  829.           bbpsw = recv_long_data ();

  830.           tmp = (0x00cf & psw) | ((0x00cf & regval) << 8);
  831.           send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PSW, tmp);

  832.           tmp = (0x0030 & bbpsw) | ((0xcf00 & regval) >> 8);
  833.           send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BBPSW, tmp);
  834.         }
  835.       else
  836.         {
  837.           send_two_arg_cmd (SDI_WRITE_CPU_REG, regid, regval);
  838.         }

  839.       if (remote_debug)
  840.         fprintf_unfiltered (gdb_stdlog, "m32r_store_register(%d,0x%08lu)\n",
  841.                             regno, (unsigned long) regval);
  842.     }
  843. }

  844. /* Get ready to modify the registers array.  On machines which store
  845.    individual registers, this doesn't need to do anything.  On machines
  846.    which store all the registers in one fell swoop, this makes sure
  847.    that registers contains all the registers from the program being
  848.    debugged.  */

  849. static void
  850. m32r_prepare_to_store (struct target_ops *self, struct regcache *regcache)
  851. {
  852.   /* Do nothing, since we can store individual regs.  */
  853.   if (remote_debug)
  854.     fprintf_unfiltered (gdb_stdlog, "m32r_prepare_to_store()\n");
  855. }

  856. static void
  857. m32r_files_info (struct target_ops *target)
  858. {
  859.   const char *file = "nothing";

  860.   if (exec_bfd)
  861.     {
  862.       file = bfd_get_filename (exec_bfd);
  863.       printf_filtered ("\tAttached to %s running program %s\n",
  864.                        chip_name, file);
  865.     }
  866. }

  867. /* Helper for m32r_xfer_partial that handles memory transfers.
  868.    Arguments are like target_xfer_partial.  */

  869. static enum target_xfer_status
  870. m32r_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
  871.                   ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
  872. {
  873.   unsigned long taddr;
  874.   unsigned char buf[0x2000];
  875.   int ret, c;

  876.   taddr = memaddr;

  877.   if (!mmu_on)
  878.     {
  879.       if ((taddr & 0xa0000000) == 0x80000000)
  880.         taddr &= 0x7fffffff;
  881.     }

  882.   if (remote_debug)
  883.     {
  884.       if (writebuf != NULL)
  885.         fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%s,write)\n",
  886.                             paddress (target_gdbarch (), memaddr),
  887.                             plongest (len));
  888.       else
  889.         fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%s,read)\n",
  890.                             paddress (target_gdbarch (), memaddr),
  891.                             plongest (len));
  892.     }

  893.   if (writebuf != NULL)
  894.     {
  895.       buf[0] = SDI_WRITE_MEMORY;
  896.       store_long_parameter (buf + 1, taddr);
  897.       store_long_parameter (buf + 5, len);
  898.       if (len < 0x1000)
  899.         {
  900.           memcpy (buf + 9, writebuf, len);
  901.           ret = send_data (buf, len + 9) - 9;
  902.         }
  903.       else
  904.         {
  905.           if (serial_write (sdi_desc, buf, 9) != 0)
  906.             {
  907.               if (remote_debug)
  908.                 fprintf_unfiltered (gdb_stdlog,
  909.                                     "m32r_xfer_memory() failed\n");
  910.               return TARGET_XFER_EOF;
  911.             }
  912.           ret = send_data (writebuf, len);
  913.         }
  914.     }
  915.   else
  916.     {
  917.       buf[0] = SDI_READ_MEMORY;
  918.       store_long_parameter (buf + 1, taddr);
  919.       store_long_parameter (buf + 5, len);
  920.       if (serial_write (sdi_desc, buf, 9) != 0)
  921.         {
  922.           if (remote_debug)
  923.             fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
  924.           return TARGET_XFER_EOF;
  925.         }

  926.       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
  927.       if (c < 0 || c == '-')
  928.         {
  929.           if (remote_debug)
  930.             fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
  931.           return TARGET_XFER_EOF;
  932.         }

  933.       ret = recv_data (readbuf, len);
  934.     }

  935.   if (ret <= 0)
  936.     {
  937.       if (remote_debug)
  938.         fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() fails\n");
  939.       return TARGET_XFER_E_IO;
  940.     }

  941.   *xfered_len = ret;
  942.   return TARGET_XFER_OK;
  943. }

  944. /* Target to_xfer_partial implementation.  */

  945. static enum target_xfer_status
  946. m32r_xfer_partial (struct target_ops *ops, enum target_object object,
  947.                    const char *annex, gdb_byte *readbuf,
  948.                    const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
  949.                    ULONGEST *xfered_len)
  950. {
  951.   switch (object)
  952.     {
  953.     case TARGET_OBJECT_MEMORY:
  954.       return m32r_xfer_memory (readbuf, writebuf, offset, len, xfered_len);

  955.     default:
  956.       return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
  957.                                             readbuf, writebuf, offset, len,
  958.                                             xfered_len);
  959.     }
  960. }

  961. static void
  962. m32r_kill (struct target_ops *ops)
  963. {
  964.   if (remote_debug)
  965.     fprintf_unfiltered (gdb_stdlog, "m32r_kill()\n");

  966.   inferior_ptid = null_ptid;
  967.   delete_thread_silent (remote_m32r_ptid);

  968.   return;
  969. }

  970. /* Clean up when a program exits.

  971.    The program actually lives on in the remote processor's RAM, and may be
  972.    run again without a download.  Don't leave it full of breakpoint
  973.    instructions.  */

  974. static void
  975. m32r_mourn_inferior (struct target_ops *ops)
  976. {
  977.   if (remote_debug)
  978.     fprintf_unfiltered (gdb_stdlog, "m32r_mourn_inferior()\n");

  979.   remove_breakpoints ();
  980.   generic_mourn_inferior ();
  981. }

  982. static int
  983. m32r_insert_breakpoint (struct target_ops *ops,
  984.                         struct gdbarch *gdbarch,
  985.                         struct bp_target_info *bp_tgt)
  986. {
  987.   CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
  988.   int ib_breakpoints;
  989.   unsigned char buf[13];
  990.   int i, c;

  991.   if (remote_debug)
  992.     fprintf_unfiltered (gdb_stdlog, "m32r_insert_breakpoint(%s,...)\n",
  993.                         paddress (gdbarch, addr));

  994.   if (use_ib_breakpoints)
  995.     ib_breakpoints = max_ib_breakpoints;
  996.   else
  997.     ib_breakpoints = 0;

  998.   for (i = 0; i < MAX_BREAKPOINTS; i++)
  999.     {
  1000.       if (bp_address[i] == 0xffffffff)
  1001.         {
  1002.           bp_address[i] = addr;
  1003.           if (i >= ib_breakpoints)
  1004.             {
  1005.               buf[0] = SDI_READ_MEMORY;
  1006.               if (mmu_on)
  1007.                 store_long_parameter (buf + 1, addr & 0xfffffffc);
  1008.               else
  1009.                 store_long_parameter (buf + 1, addr & 0x7ffffffc);
  1010.               store_long_parameter (buf + 5, 4);
  1011.               serial_write (sdi_desc, buf, 9);
  1012.               c = serial_readchar (sdi_desc, SDI_TIMEOUT);
  1013.               if (c != '-')
  1014.                 recv_data (bp_data[i], 4);
  1015.             }
  1016.           return 0;
  1017.         }
  1018.     }

  1019.   error (_("Too many breakpoints"));
  1020.   return 1;
  1021. }

  1022. static int
  1023. m32r_remove_breakpoint (struct target_ops *ops,
  1024.                         struct gdbarch *gdbarch,
  1025.                         struct bp_target_info *bp_tgt)
  1026. {
  1027.   CORE_ADDR addr = bp_tgt->placed_address;
  1028.   int i;

  1029.   if (remote_debug)
  1030.     fprintf_unfiltered (gdb_stdlog, "m32r_remove_breakpoint(%s)\n",
  1031.                         paddress (gdbarch, addr));

  1032.   for (i = 0; i < MAX_BREAKPOINTS; i++)
  1033.     {
  1034.       if (bp_address[i] == addr)
  1035.         {
  1036.           bp_address[i] = 0xffffffff;
  1037.           break;
  1038.         }
  1039.     }

  1040.   return 0;
  1041. }

  1042. static void
  1043. m32r_load (struct target_ops *self, const char *args, int from_tty)
  1044. {
  1045.   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
  1046.   asection *section;
  1047.   bfd *pbfd;
  1048.   bfd_vma entry;
  1049.   char *filename;
  1050.   int quiet;
  1051.   int nostart;
  1052.   struct timeval start_time, end_time;
  1053.   unsigned long data_count;        /* Number of bytes transferred to memory.  */
  1054.   static RETSIGTYPE (*prev_sigint) ();

  1055.   /* for direct tcp connections, we can do a fast binary download.  */
  1056.   quiet = 0;
  1057.   nostart = 0;
  1058.   filename = NULL;

  1059.   while (*args != '\000')
  1060.     {
  1061.       char *arg = extract_arg_const (&args);

  1062.       if (arg == NULL)
  1063.         break;
  1064.       make_cleanup (xfree, arg);

  1065.       if (*arg != '-')
  1066.         filename = arg;
  1067.       else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
  1068.         quiet = 1;
  1069.       else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
  1070.         nostart = 1;
  1071.       else
  1072.         error (_("Unknown option `%s'"), arg);
  1073.     }

  1074.   if (!filename)
  1075.     filename = get_exec_file (1);

  1076.   pbfd = gdb_bfd_open (filename, gnutarget, -1);
  1077.   if (pbfd == NULL)
  1078.     perror_with_name (filename);
  1079.   make_cleanup_bfd_unref (pbfd);

  1080.   if (!bfd_check_format (pbfd, bfd_object))
  1081.     error (_("\"%s\" is not an object file: %s"), filename,
  1082.            bfd_errmsg (bfd_get_error ()));

  1083.   gettimeofday (&start_time, NULL);
  1084.   data_count = 0;

  1085.   interrupted = 0;
  1086.   prev_sigint = signal (SIGINT, gdb_cntrl_c);

  1087.   for (section = pbfd->sections; section; section = section->next)
  1088.     {
  1089.       if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
  1090.         {
  1091.           bfd_vma section_address;
  1092.           bfd_size_type section_size;
  1093.           file_ptr fptr;
  1094.           int n;

  1095.           section_address = bfd_section_lma (pbfd, section);
  1096.           section_size = bfd_get_section_size (section);

  1097.           if (!mmu_on)
  1098.             {
  1099.               if ((section_address & 0xa0000000) == 0x80000000)
  1100.                 section_address &= 0x7fffffff;
  1101.             }

  1102.           if (!quiet)
  1103.             printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
  1104.                              bfd_get_section_name (pbfd, section),
  1105.                              (unsigned long) section_address,
  1106.                              (int) section_size);

  1107.           fptr = 0;

  1108.           data_count += section_size;

  1109.           n = 0;
  1110.           while (section_size > 0)
  1111.             {
  1112.               char unsigned buf[0x1000 + 9];
  1113.               int count;

  1114.               count = min (section_size, 0x1000);

  1115.               buf[0] = SDI_WRITE_MEMORY;
  1116.               store_long_parameter (buf + 1, section_address);
  1117.               store_long_parameter (buf + 5, count);

  1118.               bfd_get_section_contents (pbfd, section, buf + 9, fptr, count);
  1119.               if (send_data (buf, count + 9) <= 0)
  1120.                 error (_("Error while downloading %s section."),
  1121.                        bfd_get_section_name (pbfd, section));

  1122.               if (!quiet)
  1123.                 {
  1124.                   printf_unfiltered (".");
  1125.                   if (n++ > 60)
  1126.                     {
  1127.                       printf_unfiltered ("\n");
  1128.                       n = 0;
  1129.                     }
  1130.                   gdb_flush (gdb_stdout);
  1131.                 }

  1132.               section_address += count;
  1133.               fptr += count;
  1134.               section_size -= count;

  1135.               if (interrupted)
  1136.                 break;
  1137.             }

  1138.           if (!quiet && !interrupted)
  1139.             {
  1140.               printf_unfiltered ("done.\n");
  1141.               gdb_flush (gdb_stdout);
  1142.             }
  1143.         }

  1144.       if (interrupted)
  1145.         {
  1146.           printf_unfiltered ("Interrupted.\n");
  1147.           break;
  1148.         }
  1149.     }

  1150.   interrupted = 0;
  1151.   signal (SIGINT, prev_sigint);

  1152.   gettimeofday (&end_time, NULL);

  1153.   /* Make the PC point at the start address.  */
  1154.   if (exec_bfd)
  1155.     regcache_write_pc (get_current_regcache (),
  1156.                        bfd_get_start_address (exec_bfd));

  1157.   inferior_ptid = null_ptid;        /* No process now.  */
  1158.   delete_thread_silent (remote_m32r_ptid);

  1159.   /* This is necessary because many things were based on the PC at the time
  1160.      that we attached to the monitor, which is no longer valid now that we
  1161.      have loaded new code (and just changed the PC).  Another way to do this
  1162.      might be to call normal_stop, except that the stack may not be valid,
  1163.      and things would get horribly confused...  */

  1164.   clear_symtab_users (0);

  1165.   if (!nostart)
  1166.     {
  1167.       entry = bfd_get_start_address (pbfd);

  1168.       if (!quiet)
  1169.         printf_unfiltered ("[Starting %s at 0x%lx]\n", filename,
  1170.                            (unsigned long) entry);
  1171.     }

  1172.   print_transfer_performance (gdb_stdout, data_count, 0, &start_time,
  1173.                               &end_time);

  1174.   do_cleanups (old_chain);
  1175. }

  1176. static void
  1177. m32r_stop (struct target_ops *self, ptid_t ptid)
  1178. {
  1179.   if (remote_debug)
  1180.     fprintf_unfiltered (gdb_stdlog, "m32r_stop()\n");

  1181.   send_cmd (SDI_STOP_CPU);

  1182.   return;
  1183. }


  1184. /* Tell whether this target can support a hardware breakpoint.  CNT
  1185.    is the number of hardware breakpoints already installed.  This
  1186.    implements the target_can_use_hardware_watchpoint macro.  */

  1187. static int
  1188. m32r_can_use_hw_watchpoint (struct target_ops *self,
  1189.                             int type, int cnt, int othertype)
  1190. {
  1191.   return sdi_desc != NULL && cnt < max_access_breaks;
  1192. }

  1193. /* Set a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
  1194.    for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
  1195.    watchpoint.  */

  1196. static int
  1197. m32r_insert_watchpoint (struct target_ops *self,
  1198.                         CORE_ADDR addr, int len, int type,
  1199.                         struct expression *cond)
  1200. {
  1201.   int i;

  1202.   if (remote_debug)
  1203.     fprintf_unfiltered (gdb_stdlog, "m32r_insert_watchpoint(%s,%d,%d)\n",
  1204.                         paddress (target_gdbarch (), addr), len, type);

  1205.   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
  1206.     {
  1207.       if (ab_address[i] == 0x00000000)
  1208.         {
  1209.           ab_address[i] = addr;
  1210.           ab_size[i] = len;
  1211.           ab_type[i] = type;
  1212.           return 0;
  1213.         }
  1214.     }

  1215.   error (_("Too many watchpoints"));
  1216.   return 1;
  1217. }

  1218. static int
  1219. m32r_remove_watchpoint (struct target_ops *self,
  1220.                         CORE_ADDR addr, int len, int type,
  1221.                         struct expression *cond)
  1222. {
  1223.   int i;

  1224.   if (remote_debug)
  1225.     fprintf_unfiltered (gdb_stdlog, "m32r_remove_watchpoint(%s,%d,%d)\n",
  1226.                         paddress (target_gdbarch (), addr), len, type);

  1227.   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
  1228.     {
  1229.       if (ab_address[i] == addr)
  1230.         {
  1231.           ab_address[i] = 0x00000000;
  1232.           break;
  1233.         }
  1234.     }

  1235.   return 0;
  1236. }

  1237. static int
  1238. m32r_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
  1239. {
  1240.   int rc = 0;

  1241.   if (hit_watchpoint_addr != 0x00000000)
  1242.     {
  1243.       *addr_p = hit_watchpoint_addr;
  1244.       rc = 1;
  1245.     }
  1246.   return rc;
  1247. }

  1248. static int
  1249. m32r_stopped_by_watchpoint (struct target_ops *ops)
  1250. {
  1251.   CORE_ADDR addr;

  1252.   return m32r_stopped_data_address (&current_target, &addr);
  1253. }

  1254. /* Check to see if a thread is still alive.  */

  1255. static int
  1256. m32r_thread_alive (struct target_ops *ops, ptid_t ptid)
  1257. {
  1258.   if (ptid_equal (ptid, remote_m32r_ptid))
  1259.     /* The main task is always alive.  */
  1260.     return 1;

  1261.   return 0;
  1262. }

  1263. /* Convert a thread ID to a string.  Returns the string in a static
  1264.    buffer.  */

  1265. static char *
  1266. m32r_pid_to_str (struct target_ops *ops, ptid_t ptid)
  1267. {
  1268.   static char buf[64];

  1269.   if (ptid_equal (remote_m32r_ptid, ptid))
  1270.     {
  1271.       xsnprintf (buf, sizeof buf, "Thread <main>");
  1272.       return buf;
  1273.     }

  1274.   return normal_pid_to_str (ptid);
  1275. }

  1276. static void
  1277. sdireset_command (char *args, int from_tty)
  1278. {
  1279.   if (remote_debug)
  1280.     fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");

  1281.   send_cmd (SDI_OPEN);

  1282.   inferior_ptid = null_ptid;
  1283.   delete_thread_silent (remote_m32r_ptid);
  1284. }


  1285. static void
  1286. sdistatus_command (char *args, int from_tty)
  1287. {
  1288.   unsigned char buf[4096];
  1289.   int i, c;

  1290.   if (remote_debug)
  1291.     fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");

  1292.   if (!sdi_desc)
  1293.     return;

  1294.   send_cmd (SDI_STATUS);
  1295.   for (i = 0; i < 4096; i++)
  1296.     {
  1297.       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
  1298.       if (c < 0)
  1299.         return;
  1300.       buf[i] = c;
  1301.       if (c == 0)
  1302.         break;
  1303.     }

  1304.   printf_filtered ("%s", buf);
  1305. }


  1306. static void
  1307. debug_chaos_command (char *args, int from_tty)
  1308. {
  1309.   unsigned char buf[3];

  1310.   buf[0] = SDI_SET_ATTR;
  1311.   buf[1] = SDI_ATTR_CACHE;
  1312.   buf[2] = SDI_CACHE_TYPE_CHAOS;
  1313.   send_data (buf, 3);
  1314. }


  1315. static void
  1316. use_debug_dma_command (char *args, int from_tty)
  1317. {
  1318.   unsigned char buf[3];

  1319.   buf[0] = SDI_SET_ATTR;
  1320.   buf[1] = SDI_ATTR_MEM_ACCESS;
  1321.   buf[2] = SDI_MEM_ACCESS_DEBUG_DMA;
  1322.   send_data (buf, 3);
  1323. }

  1324. static void
  1325. use_mon_code_command (char *args, int from_tty)
  1326. {
  1327.   unsigned char buf[3];

  1328.   buf[0] = SDI_SET_ATTR;
  1329.   buf[1] = SDI_ATTR_MEM_ACCESS;
  1330.   buf[2] = SDI_MEM_ACCESS_MON_CODE;
  1331.   send_data (buf, 3);
  1332. }


  1333. static void
  1334. use_ib_breakpoints_command (char *args, int from_tty)
  1335. {
  1336.   use_ib_breakpoints = 1;
  1337. }

  1338. static void
  1339. use_dbt_breakpoints_command (char *args, int from_tty)
  1340. {
  1341.   use_ib_breakpoints = 0;
  1342. }

  1343. static int
  1344. m32r_return_one (struct target_ops *target)
  1345. {
  1346.   return 1;
  1347. }

  1348. /* Implementation of the to_has_execution method.  */

  1349. static int
  1350. m32r_has_execution (struct target_ops *target, ptid_t the_ptid)
  1351. {
  1352.   return 1;
  1353. }

  1354. /* Define the target subroutine names.  */

  1355. struct target_ops m32r_ops;

  1356. static void
  1357. init_m32r_ops (void)
  1358. {
  1359.   m32r_ops.to_shortname = "m32rsdi";
  1360.   m32r_ops.to_longname = "Remote M32R debugging over SDI interface";
  1361.   m32r_ops.to_doc = "Use an M32R board using SDI debugging protocol.";
  1362.   m32r_ops.to_open = m32r_open;
  1363.   m32r_ops.to_close = m32r_close;
  1364.   m32r_ops.to_detach = m32r_detach;
  1365.   m32r_ops.to_resume = m32r_resume;
  1366.   m32r_ops.to_wait = m32r_wait;
  1367.   m32r_ops.to_fetch_registers = m32r_fetch_register;
  1368.   m32r_ops.to_store_registers = m32r_store_register;
  1369.   m32r_ops.to_prepare_to_store = m32r_prepare_to_store;
  1370.   m32r_ops.to_xfer_partial = m32r_xfer_partial;
  1371.   m32r_ops.to_files_info = m32r_files_info;
  1372.   m32r_ops.to_insert_breakpoint = m32r_insert_breakpoint;
  1373.   m32r_ops.to_remove_breakpoint = m32r_remove_breakpoint;
  1374.   m32r_ops.to_can_use_hw_breakpoint = m32r_can_use_hw_watchpoint;
  1375.   m32r_ops.to_insert_watchpoint = m32r_insert_watchpoint;
  1376.   m32r_ops.to_remove_watchpoint = m32r_remove_watchpoint;
  1377.   m32r_ops.to_stopped_by_watchpoint = m32r_stopped_by_watchpoint;
  1378.   m32r_ops.to_stopped_data_address = m32r_stopped_data_address;
  1379.   m32r_ops.to_kill = m32r_kill;
  1380.   m32r_ops.to_load = m32r_load;
  1381.   m32r_ops.to_create_inferior = m32r_create_inferior;
  1382.   m32r_ops.to_mourn_inferior = m32r_mourn_inferior;
  1383.   m32r_ops.to_stop = m32r_stop;
  1384.   m32r_ops.to_log_command = serial_log_command;
  1385.   m32r_ops.to_thread_alive = m32r_thread_alive;
  1386.   m32r_ops.to_pid_to_str = m32r_pid_to_str;
  1387.   m32r_ops.to_stratum = process_stratum;
  1388.   m32r_ops.to_has_all_memory = m32r_return_one;
  1389.   m32r_ops.to_has_memory = m32r_return_one;
  1390.   m32r_ops.to_has_stack = m32r_return_one;
  1391.   m32r_ops.to_has_registers = m32r_return_one;
  1392.   m32r_ops.to_has_execution = m32r_has_execution;
  1393.   m32r_ops.to_magic = OPS_MAGIC;
  1394. };


  1395. extern initialize_file_ftype _initialize_remote_m32r;

  1396. void
  1397. _initialize_remote_m32r (void)
  1398. {
  1399.   int i;

  1400.   init_m32r_ops ();

  1401.   /* Initialize breakpoints.  */
  1402.   for (i = 0; i < MAX_BREAKPOINTS; i++)
  1403.     bp_address[i] = 0xffffffff;

  1404.   /* Initialize access breaks.  */
  1405.   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
  1406.     ab_address[i] = 0x00000000;

  1407.   add_target (&m32r_ops);

  1408.   add_com ("sdireset", class_obscure, sdireset_command,
  1409.            _("Reset SDI connection."));

  1410.   add_com ("sdistatus", class_obscure, sdistatus_command,
  1411.            _("Show status of SDI connection."));

  1412.   add_com ("debug_chaos", class_obscure, debug_chaos_command,
  1413.            _("Debug M32R/Chaos."));

  1414.   add_com ("use_debug_dma", class_obscure, use_debug_dma_command,
  1415.            _("Use debug DMA mem access."));
  1416.   add_com ("use_mon_code", class_obscure, use_mon_code_command,
  1417.            _("Use mon code mem access."));

  1418.   add_com ("use_ib_break", class_obscure, use_ib_breakpoints_command,
  1419.            _("Set breakpoints by IB break."));
  1420.   add_com ("use_dbt_break", class_obscure, use_dbt_breakpoints_command,
  1421.            _("Set breakpoints by dbt."));

  1422.   /* Yes, 42000 is arbitrary.  The only sense out of it, is that it
  1423.      isn't 0.  */
  1424.   remote_m32r_ptid = ptid_build (42000, 0, 42000);
  1425. }