GDB (API)
/home/stan/gdb/src/gdb/remote-m32r-sdi.c
Go to the documentation of this file.
00001 /* Remote debugging interface for M32R/SDI.
00002 
00003    Copyright (C) 2003-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Renesas Technology Co.
00006    Written by Kei Sakamoto <sakamoto.kei@renesas.com>.
00007 
00008    This file is part of GDB.
00009 
00010    This program is free software; you can redistribute it and/or modify
00011    it under the terms of the GNU General Public License as published by
00012    the Free Software Foundation; either version 3 of the License, or
00013    (at your option) any later version.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License
00021    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00022 
00023 #include "defs.h"
00024 #include "gdbcmd.h"
00025 #include "gdbcore.h"
00026 #include "inferior.h"
00027 #include "target.h"
00028 #include "regcache.h"
00029 #include "gdb_string.h"
00030 #include "gdbthread.h"
00031 #include <ctype.h>
00032 #include <signal.h>
00033 #ifdef __MINGW32__
00034 #include <winsock2.h>
00035 #else
00036 #include <netinet/in.h>
00037 #endif
00038 #include <sys/types.h>
00039 #include <sys/time.h>
00040 #include <signal.h>
00041 #include <time.h>
00042 #include "gdb_bfd.h"
00043 #include "cli/cli-utils.h"
00044 
00045 #include "serial.h"
00046 
00047 /* Descriptor for I/O to remote machine.  */
00048 
00049 static struct serial *sdi_desc = NULL;
00050 
00051 #define SDI_TIMEOUT 30
00052 
00053 
00054 #define SDIPORT 3232
00055 
00056 static char chip_name[64];
00057 
00058 static int step_mode;
00059 static unsigned long last_pc_addr = 0xffffffff;
00060 static unsigned char last_pc_addr_data[2];
00061 
00062 static int mmu_on = 0;
00063 
00064 static int use_ib_breakpoints = 1;
00065 
00066 #define MAX_BREAKPOINTS 1024
00067 static int max_ib_breakpoints;
00068 static unsigned long bp_address[MAX_BREAKPOINTS];
00069 static unsigned char bp_data[MAX_BREAKPOINTS][4];
00070 
00071 /* dbt -> nop */
00072 static const unsigned char dbt_bp_entry[] = {
00073   0x10, 0xe0, 0x70, 0x00
00074 };
00075 
00076 #define MAX_ACCESS_BREAKS 4
00077 static int max_access_breaks;
00078 static unsigned long ab_address[MAX_ACCESS_BREAKS];
00079 static unsigned int ab_type[MAX_ACCESS_BREAKS];
00080 static unsigned int ab_size[MAX_ACCESS_BREAKS];
00081 static CORE_ADDR hit_watchpoint_addr = 0;
00082 
00083 static int interrupted = 0;
00084 
00085 /* Forward data declarations */
00086 extern struct target_ops m32r_ops;
00087 
00088 /* This is the ptid we use while we're connected to the remote.  Its
00089    value is arbitrary, as the target doesn't have a notion of
00090    processes or threads, but we need something non-null to place in
00091    inferior_ptid.  */
00092 static ptid_t remote_m32r_ptid;
00093 
00094 /* Commands */
00095 #define SDI_OPEN                 1
00096 #define SDI_CLOSE                2
00097 #define SDI_RELEASE              3
00098 #define SDI_READ_CPU_REG         4
00099 #define SDI_WRITE_CPU_REG        5
00100 #define SDI_READ_MEMORY          6
00101 #define SDI_WRITE_MEMORY         7
00102 #define SDI_EXEC_CPU             8
00103 #define SDI_STOP_CPU             9
00104 #define SDI_WAIT_FOR_READY      10
00105 #define SDI_GET_ATTR            11
00106 #define SDI_SET_ATTR            12
00107 #define SDI_STATUS              13
00108 
00109 /* Attributes */
00110 #define SDI_ATTR_NAME            1
00111 #define SDI_ATTR_BRK             2
00112 #define SDI_ATTR_ABRK            3
00113 #define SDI_ATTR_CACHE           4
00114 #define SDI_CACHE_TYPE_M32102    0
00115 #define SDI_CACHE_TYPE_CHAOS     1
00116 #define SDI_ATTR_MEM_ACCESS      5
00117 #define SDI_MEM_ACCESS_DEBUG_DMA 0
00118 #define SDI_MEM_ACCESS_MON_CODE  1
00119 
00120 /* Registers */
00121 #define SDI_REG_R0               0
00122 #define SDI_REG_R1               1
00123 #define SDI_REG_R2               2
00124 #define SDI_REG_R3               3
00125 #define SDI_REG_R4               4
00126 #define SDI_REG_R5               5
00127 #define SDI_REG_R6               6
00128 #define SDI_REG_R7               7
00129 #define SDI_REG_R8               8
00130 #define SDI_REG_R9               9
00131 #define SDI_REG_R10             10
00132 #define SDI_REG_R11             11
00133 #define SDI_REG_R12             12
00134 #define SDI_REG_FP              13
00135 #define SDI_REG_LR              14
00136 #define SDI_REG_SP              15
00137 #define SDI_REG_PSW             16
00138 #define SDI_REG_CBR             17
00139 #define SDI_REG_SPI             18
00140 #define SDI_REG_SPU             19
00141 #define SDI_REG_CR4             20
00142 #define SDI_REG_EVB             21
00143 #define SDI_REG_BPC             22
00144 #define SDI_REG_CR7             23
00145 #define SDI_REG_BBPSW           24
00146 #define SDI_REG_CR9             25
00147 #define SDI_REG_CR10            26
00148 #define SDI_REG_CR11            27
00149 #define SDI_REG_CR12            28
00150 #define SDI_REG_WR              29
00151 #define SDI_REG_BBPC            30
00152 #define SDI_REG_PBP             31
00153 #define SDI_REG_ACCH            32
00154 #define SDI_REG_ACCL            33
00155 #define SDI_REG_ACC1H           34
00156 #define SDI_REG_ACC1L           35
00157 
00158 
00159 /* Low level communication functions.  */
00160 
00161 /* Check an ack packet from the target.  */
00162 static int
00163 get_ack (void)
00164 {
00165   int c;
00166 
00167   if (!sdi_desc)
00168     return -1;
00169 
00170   c = serial_readchar (sdi_desc, SDI_TIMEOUT);
00171 
00172   if (c < 0)
00173     return -1;
00174 
00175   if (c != '+')                 /* error */
00176     return -1;
00177 
00178   return 0;
00179 }
00180 
00181 /* Send data to the target and check an ack packet.  */
00182 static int
00183 send_data (void *buf, int len)
00184 {
00185   if (!sdi_desc)
00186     return -1;
00187 
00188   if (serial_write (sdi_desc, buf, len) != 0)
00189     return -1;
00190 
00191   if (get_ack () == -1)
00192     return -1;
00193 
00194   return len;
00195 }
00196 
00197 /* Receive data from the target.  */
00198 static int
00199 recv_data (void *buf, int len)
00200 {
00201   int total = 0;
00202   int c;
00203 
00204   if (!sdi_desc)
00205     return -1;
00206 
00207   while (total < len)
00208     {
00209       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
00210 
00211       if (c < 0)
00212         return -1;
00213 
00214       ((unsigned char *) buf)[total++] = c;
00215     }
00216 
00217   return len;
00218 }
00219 
00220 /* Store unsigned long parameter on packet.  */
00221 static void
00222 store_long_parameter (void *buf, long val)
00223 {
00224   val = htonl (val);
00225   memcpy (buf, &val, 4);
00226 }
00227 
00228 static int
00229 send_cmd (unsigned char cmd)
00230 {
00231   unsigned char buf[1];
00232 
00233   buf[0] = cmd;
00234   return send_data (buf, 1);
00235 }
00236 
00237 static int
00238 send_one_arg_cmd (unsigned char cmd, unsigned char arg1)
00239 {
00240   unsigned char buf[2];
00241 
00242   buf[0] = cmd;
00243   buf[1] = arg1;
00244   return send_data (buf, 2);
00245 }
00246 
00247 static int
00248 send_two_arg_cmd (unsigned char cmd, unsigned char arg1, unsigned long arg2)
00249 {
00250   unsigned char buf[6];
00251 
00252   buf[0] = cmd;
00253   buf[1] = arg1;
00254   store_long_parameter (buf + 2, arg2);
00255   return send_data (buf, 6);
00256 }
00257 
00258 static int
00259 send_three_arg_cmd (unsigned char cmd, unsigned long arg1, unsigned long arg2,
00260                     unsigned long arg3)
00261 {
00262   unsigned char buf[13];
00263 
00264   buf[0] = cmd;
00265   store_long_parameter (buf + 1, arg1);
00266   store_long_parameter (buf + 5, arg2);
00267   store_long_parameter (buf + 9, arg3);
00268   return send_data (buf, 13);
00269 }
00270 
00271 static unsigned char
00272 recv_char_data (void)
00273 {
00274   unsigned char val;
00275 
00276   recv_data (&val, 1);
00277   return val;
00278 }
00279 
00280 static unsigned long
00281 recv_long_data (void)
00282 {
00283   unsigned long val;
00284 
00285   recv_data (&val, 4);
00286   return ntohl (val);
00287 }
00288 
00289 
00290 /* Check if MMU is on.  */
00291 static void
00292 check_mmu_status (void)
00293 {
00294   unsigned long val;
00295 
00296   /* Read PC address.  */
00297   if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC) == -1)
00298     return;
00299   val = recv_long_data ();
00300   if ((val & 0xc0000000) == 0x80000000)
00301     {
00302       mmu_on = 1;
00303       return;
00304     }
00305 
00306   /* Read EVB address.  */
00307   if (send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_EVB) == -1)
00308     return;
00309   val = recv_long_data ();
00310   if ((val & 0xc0000000) == 0x80000000)
00311     {
00312       mmu_on = 1;
00313       return;
00314     }
00315 
00316   mmu_on = 0;
00317 }
00318 
00319 
00320 /* This is called not only when we first attach, but also when the
00321    user types "run" after having attached.  */
00322 static void
00323 m32r_create_inferior (struct target_ops *ops, char *execfile,
00324                       char *args, char **env, int from_tty)
00325 {
00326   CORE_ADDR entry_pt;
00327 
00328   if (args && *args)
00329     error (_("Cannot pass arguments to remote STDEBUG process"));
00330 
00331   if (execfile == 0 || exec_bfd == 0)
00332     error (_("No executable file specified"));
00333 
00334   if (remote_debug)
00335     fprintf_unfiltered (gdb_stdlog, "m32r_create_inferior(%s,%s)\n", execfile,
00336                         args);
00337 
00338   entry_pt = bfd_get_start_address (exec_bfd);
00339 
00340   /* The "process" (board) is already stopped awaiting our commands, and
00341      the program is already downloaded.  We just set its PC and go.  */
00342 
00343   clear_proceed_status ();
00344 
00345   /* Tell wait_for_inferior that we've started a new process.  */
00346   init_wait_for_inferior ();
00347 
00348   /* Set up the "saved terminal modes" of the inferior
00349      based on what modes we are starting it with.  */
00350   target_terminal_init ();
00351 
00352   /* Install inferior's terminal modes.  */
00353   target_terminal_inferior ();
00354 
00355   regcache_write_pc (get_current_regcache (), entry_pt);
00356 }
00357 
00358 /* Open a connection to a remote debugger.
00359    NAME is the filename used for communication.  */
00360 
00361 static void
00362 m32r_open (char *args, int from_tty)
00363 {
00364   struct hostent *host_ent;
00365   struct sockaddr_in server_addr;
00366   char *port_str, hostname[256];
00367   int port;
00368   int i, n;
00369   int yes = 1;
00370 
00371   if (remote_debug)
00372     fprintf_unfiltered (gdb_stdlog, "m32r_open(%d)\n", from_tty);
00373 
00374   target_preopen (from_tty);
00375 
00376   push_target (&m32r_ops);
00377 
00378   if (args == NULL)
00379     xsnprintf (hostname, sizeof (hostname), "localhost:%d", SDIPORT);
00380   else
00381     {
00382       port_str = strchr (args, ':');
00383       if (port_str == NULL)
00384         xsnprintf (hostname, sizeof (hostname), "%s:%d", args, SDIPORT);
00385       else
00386         xsnprintf (hostname, sizeof (hostname), "%s", args);
00387     }
00388 
00389   sdi_desc = serial_open (hostname);
00390   if (!sdi_desc)
00391     error (_("Connection refused."));
00392 
00393   if (get_ack () == -1)
00394     error (_("Cannot connect to SDI target."));
00395 
00396   if (send_cmd (SDI_OPEN) == -1)
00397     error (_("Cannot connect to SDI target."));
00398 
00399   /* Get maximum number of ib breakpoints.  */
00400   send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_BRK);
00401   max_ib_breakpoints = recv_char_data ();
00402   if (remote_debug)
00403     printf_filtered ("Max IB Breakpoints = %d\n", max_ib_breakpoints);
00404 
00405   /* Initialize breakpoints.  */
00406   for (i = 0; i < MAX_BREAKPOINTS; i++)
00407     bp_address[i] = 0xffffffff;
00408 
00409   /* Get maximum number of access breaks.  */
00410   send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_ABRK);
00411   max_access_breaks = recv_char_data ();
00412   if (remote_debug)
00413     printf_filtered ("Max Access Breaks = %d\n", max_access_breaks);
00414 
00415   /* Initialize access breask.  */
00416   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
00417     ab_address[i] = 0x00000000;
00418 
00419   check_mmu_status ();
00420 
00421   /* Get the name of chip on target board.  */
00422   send_one_arg_cmd (SDI_GET_ATTR, SDI_ATTR_NAME);
00423   recv_data (chip_name, 64);
00424 
00425   if (from_tty)
00426     printf_filtered ("Remote %s connected to %s\n", target_shortname,
00427                      chip_name);
00428 }
00429 
00430 /* Close out all files and local state before this target loses control.  */
00431 
00432 static void
00433 m32r_close (void)
00434 {
00435   if (remote_debug)
00436     fprintf_unfiltered (gdb_stdlog, "m32r_close()\n");
00437 
00438   if (sdi_desc)
00439     {
00440       send_cmd (SDI_CLOSE);
00441       serial_close (sdi_desc);
00442       sdi_desc = NULL;
00443     }
00444 
00445   inferior_ptid = null_ptid;
00446   delete_thread_silent (remote_m32r_ptid);
00447   return;
00448 }
00449 
00450 /* Tell the remote machine to resume.  */
00451 
00452 static void
00453 m32r_resume (struct target_ops *ops,
00454              ptid_t ptid, int step, enum gdb_signal sig)
00455 {
00456   unsigned long pc_addr, bp_addr, ab_addr;
00457   int ib_breakpoints;
00458   unsigned char buf[13];
00459   int i;
00460 
00461   if (remote_debug)
00462     {
00463       if (step)
00464         fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(step)\n");
00465       else
00466         fprintf_unfiltered (gdb_stdlog, "\nm32r_resume(cont)\n");
00467     }
00468 
00469   check_mmu_status ();
00470 
00471   pc_addr = regcache_read_pc (get_current_regcache ());
00472   if (remote_debug)
00473     fprintf_unfiltered (gdb_stdlog, "pc <= 0x%lx\n", pc_addr);
00474 
00475   /* At pc address there is a parallel instruction with +2 offset,
00476      so we have to make it a serial instruction or avoid it.  */
00477   if (pc_addr == last_pc_addr)
00478     {
00479       /* Avoid a parallel nop.  */
00480       if (last_pc_addr_data[0] == 0xf0 && last_pc_addr_data[1] == 0x00)
00481         {
00482           pc_addr += 2;
00483           /* Now we can forget this instruction.  */
00484           last_pc_addr = 0xffffffff;
00485         }
00486       /* Clear a parallel bit.  */
00487       else
00488         {
00489           buf[0] = SDI_WRITE_MEMORY;
00490           if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
00491             store_long_parameter (buf + 1, pc_addr);
00492           else
00493             store_long_parameter (buf + 1, pc_addr - 1);
00494           store_long_parameter (buf + 5, 1);
00495           buf[9] = last_pc_addr_data[0] & 0x7f;
00496           send_data (buf, 10);
00497         }
00498     }
00499 
00500   /* Set PC.  */
00501   send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
00502 
00503   /* step mode.  */
00504   step_mode = step;
00505   if (step)
00506     {
00507       /* Set PBP.  */
00508       send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, pc_addr | 1);
00509     }
00510   else
00511     {
00512       /* Unset PBP.  */
00513       send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PBP, 0x00000000);
00514     }
00515 
00516   if (use_ib_breakpoints)
00517     ib_breakpoints = max_ib_breakpoints;
00518   else
00519     ib_breakpoints = 0;
00520 
00521   /* Set ib breakpoints.  */
00522   for (i = 0; i < ib_breakpoints; i++)
00523     {
00524       bp_addr = bp_address[i];
00525 
00526       if (bp_addr == 0xffffffff)
00527         continue;
00528 
00529       /* Set PBP.  */
00530       if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
00531         send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
00532                             0x00000006);
00533       else
00534         send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
00535                             0x06000000);
00536 
00537       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8080 + 4 * i, 4, bp_addr);
00538     }
00539 
00540   /* Set dbt breakpoints.  */
00541   for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
00542     {
00543       bp_addr = bp_address[i];
00544 
00545       if (bp_addr == 0xffffffff)
00546         continue;
00547 
00548       if (!mmu_on)
00549         bp_addr &= 0x7fffffff;
00550 
00551       /* Write DBT instruction.  */
00552       buf[0] = SDI_WRITE_MEMORY;
00553       store_long_parameter (buf + 1, (bp_addr & 0xfffffffc));
00554       store_long_parameter (buf + 5, 4);
00555       if ((bp_addr & 2) == 0 && bp_addr != (pc_addr & 0xfffffffc))
00556         {
00557           if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
00558             {
00559               buf[9] = dbt_bp_entry[0];
00560               buf[10] = dbt_bp_entry[1];
00561               buf[11] = dbt_bp_entry[2];
00562               buf[12] = dbt_bp_entry[3];
00563             }
00564           else
00565             {
00566               buf[9] = dbt_bp_entry[3];
00567               buf[10] = dbt_bp_entry[2];
00568               buf[11] = dbt_bp_entry[1];
00569               buf[12] = dbt_bp_entry[0];
00570             }
00571         }
00572       else
00573         {
00574           if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
00575             {
00576               if ((bp_addr & 2) == 0)
00577                 {
00578                   buf[9] = dbt_bp_entry[0];
00579                   buf[10] = dbt_bp_entry[1];
00580                   buf[11] = bp_data[i][2] & 0x7f;
00581                   buf[12] = bp_data[i][3];
00582                 }
00583               else
00584                 {
00585                   buf[9] = bp_data[i][0];
00586                   buf[10] = bp_data[i][1];
00587                   buf[11] = dbt_bp_entry[0];
00588                   buf[12] = dbt_bp_entry[1];
00589                 }
00590             }
00591           else
00592             {
00593               if ((bp_addr & 2) == 0)
00594                 {
00595                   buf[9] = bp_data[i][0];
00596                   buf[10] = bp_data[i][1] & 0x7f;
00597                   buf[11] = dbt_bp_entry[1];
00598                   buf[12] = dbt_bp_entry[0];
00599                 }
00600               else
00601                 {
00602                   buf[9] = dbt_bp_entry[1];
00603                   buf[10] = dbt_bp_entry[0];
00604                   buf[11] = bp_data[i][2];
00605                   buf[12] = bp_data[i][3];
00606                 }
00607             }
00608         }
00609       send_data (buf, 13);
00610     }
00611 
00612   /* Set access breaks.  */
00613   for (i = 0; i < max_access_breaks; i++)
00614     {
00615       ab_addr = ab_address[i];
00616 
00617       if (ab_addr == 0x00000000)
00618         continue;
00619 
00620       /* DBC register.  */
00621       if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
00622         {
00623           switch (ab_type[i])
00624             {
00625             case 0:             /* write watch */
00626               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
00627                                   0x00000086);
00628               break;
00629             case 1:             /* read watch */
00630               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
00631                                   0x00000046);
00632               break;
00633             case 2:             /* access watch */
00634               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
00635                                   0x00000006);
00636               break;
00637             }
00638         }
00639       else
00640         {
00641           switch (ab_type[i])
00642             {
00643             case 0:             /* write watch */
00644               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
00645                                   0x86000000);
00646               break;
00647             case 1:             /* read watch */
00648               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
00649                                   0x46000000);
00650               break;
00651             case 2:             /* access watch */
00652               send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
00653                                   0x06000000);
00654               break;
00655             }
00656         }
00657 
00658       /* DBAH register.  */
00659       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8180 + 4 * i, 4, ab_addr);
00660 
00661       /* DBAL register.  */
00662       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8200 + 4 * i, 4,
00663                           0xffffffff);
00664 
00665       /* DBD register.  */
00666       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8280 + 4 * i, 4,
00667                           0x00000000);
00668 
00669       /* DBDM register.  */
00670       send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8300 + 4 * i, 4,
00671                           0x00000000);
00672     }
00673 
00674   /* Resume program.  */
00675   send_cmd (SDI_EXEC_CPU);
00676 
00677   /* Without this, some commands which require an active target (such as kill)
00678      won't work.  This variable serves (at least) double duty as both the pid
00679      of the target process (if it has such), and as a flag indicating that a
00680      target is active.  These functions should be split out into seperate
00681      variables, especially since GDB will someday have a notion of debugging
00682      several processes.  */
00683   inferior_ptid = remote_m32r_ptid;
00684   add_thread_silent (remote_m32r_ptid);
00685 
00686   return;
00687 }
00688 
00689 /* Wait until the remote machine stops, then return,
00690    storing status in STATUS just as `wait' would.  */
00691 
00692 static void
00693 gdb_cntrl_c (int signo)
00694 {
00695   if (remote_debug)
00696     fprintf_unfiltered (gdb_stdlog, "interrupt\n");
00697   interrupted = 1;
00698 }
00699 
00700 static ptid_t
00701 m32r_wait (struct target_ops *ops,
00702            ptid_t ptid, struct target_waitstatus *status, int options)
00703 {
00704   static RETSIGTYPE (*prev_sigint) ();
00705   unsigned long bp_addr, pc_addr;
00706   int ib_breakpoints;
00707   long i;
00708   unsigned char buf[13];
00709   int ret, c;
00710 
00711   if (remote_debug)
00712     fprintf_unfiltered (gdb_stdlog, "m32r_wait()\n");
00713 
00714   status->kind = TARGET_WAITKIND_EXITED;
00715   status->value.sig = GDB_SIGNAL_0;
00716 
00717   interrupted = 0;
00718   prev_sigint = signal (SIGINT, gdb_cntrl_c);
00719 
00720   /* Wait for ready.  */
00721   buf[0] = SDI_WAIT_FOR_READY;
00722   if (serial_write (sdi_desc, buf, 1) != 0)
00723     error (_("Remote connection closed"));
00724 
00725   while (1)
00726     {
00727       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
00728       if (c < 0)
00729         error (_("Remote connection closed"));
00730 
00731       if (c == '-')             /* error */
00732         {
00733           status->kind = TARGET_WAITKIND_STOPPED;
00734           status->value.sig = GDB_SIGNAL_HUP;
00735           return inferior_ptid;
00736         }
00737       else if (c == '+')        /* stopped */
00738         break;
00739 
00740       if (interrupted)
00741         ret = serial_write (sdi_desc, "!", 1);  /* packet to interrupt */
00742       else
00743         ret = serial_write (sdi_desc, ".", 1);  /* packet to wait */
00744       if (ret != 0)
00745         error (_("Remote connection closed"));
00746     }
00747 
00748   status->kind = TARGET_WAITKIND_STOPPED;
00749   if (interrupted)
00750     status->value.sig = GDB_SIGNAL_INT;
00751   else
00752     status->value.sig = GDB_SIGNAL_TRAP;
00753 
00754   interrupted = 0;
00755   signal (SIGINT, prev_sigint);
00756 
00757   check_mmu_status ();
00758 
00759   /* Recover parallel bit.  */
00760   if (last_pc_addr != 0xffffffff)
00761     {
00762       buf[0] = SDI_WRITE_MEMORY;
00763       if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
00764         store_long_parameter (buf + 1, last_pc_addr);
00765       else
00766         store_long_parameter (buf + 1, last_pc_addr - 1);
00767       store_long_parameter (buf + 5, 1);
00768       buf[9] = last_pc_addr_data[0];
00769       send_data (buf, 10);
00770       last_pc_addr = 0xffffffff;
00771     }
00772 
00773   if (use_ib_breakpoints)
00774     ib_breakpoints = max_ib_breakpoints;
00775   else
00776     ib_breakpoints = 0;
00777 
00778   /* Set back pc by 2 if m32r is stopped with dbt.  */
00779   last_pc_addr = 0xffffffff;
00780   send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BPC);
00781   pc_addr = recv_long_data () - 2;
00782   for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
00783     {
00784       if (pc_addr == bp_address[i])
00785         {
00786           send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BPC, pc_addr);
00787 
00788           /* If there is a parallel instruction with +2 offset at pc
00789              address, we have to take care of it later.  */
00790           if ((pc_addr & 0x2) != 0)
00791             {
00792               if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
00793                 {
00794                   if ((bp_data[i][2] & 0x80) != 0)
00795                     {
00796                       last_pc_addr = pc_addr;
00797                       last_pc_addr_data[0] = bp_data[i][2];
00798                       last_pc_addr_data[1] = bp_data[i][3];
00799                     }
00800                 }
00801               else
00802                 {
00803                   if ((bp_data[i][1] & 0x80) != 0)
00804                     {
00805                       last_pc_addr = pc_addr;
00806                       last_pc_addr_data[0] = bp_data[i][1];
00807                       last_pc_addr_data[1] = bp_data[i][0];
00808                     }
00809                 }
00810             }
00811           break;
00812         }
00813     }
00814 
00815   /* Remove ib breakpoints.  */
00816   for (i = 0; i < ib_breakpoints; i++)
00817     {
00818       if (bp_address[i] != 0xffffffff)
00819         send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8000 + 4 * i, 4,
00820                             0x00000000);
00821     }
00822   /* Remove dbt breakpoints.  */
00823   for (i = ib_breakpoints; i < MAX_BREAKPOINTS; i++)
00824     {
00825       bp_addr = bp_address[i];
00826       if (bp_addr != 0xffffffff)
00827         {
00828           if (!mmu_on)
00829             bp_addr &= 0x7fffffff;
00830           buf[0] = SDI_WRITE_MEMORY;
00831           store_long_parameter (buf + 1, bp_addr & 0xfffffffc);
00832           store_long_parameter (buf + 5, 4);
00833           buf[9] = bp_data[i][0];
00834           buf[10] = bp_data[i][1];
00835           buf[11] = bp_data[i][2];
00836           buf[12] = bp_data[i][3];
00837           send_data (buf, 13);
00838         }
00839     }
00840 
00841   /* Remove access breaks.  */
00842   hit_watchpoint_addr = 0;
00843   for (i = 0; i < max_access_breaks; i++)
00844     {
00845       if (ab_address[i] != 0x00000000)
00846         {
00847           buf[0] = SDI_READ_MEMORY;
00848           store_long_parameter (buf + 1, 0xffff8100 + 4 * i);
00849           store_long_parameter (buf + 5, 4);
00850           serial_write (sdi_desc, buf, 9);
00851           c = serial_readchar (sdi_desc, SDI_TIMEOUT);
00852           if (c != '-' && recv_data (buf, 4) != -1)
00853             {
00854               if (gdbarch_byte_order (target_gdbarch ()) == BFD_ENDIAN_BIG)
00855                 {
00856                   if ((buf[3] & 0x1) == 0x1)
00857                     hit_watchpoint_addr = ab_address[i];
00858                 }
00859               else
00860                 {
00861                   if ((buf[0] & 0x1) == 0x1)
00862                     hit_watchpoint_addr = ab_address[i];
00863                 }
00864             }
00865 
00866           send_three_arg_cmd (SDI_WRITE_MEMORY, 0xffff8100 + 4 * i, 4,
00867                               0x00000000);
00868         }
00869     }
00870 
00871   if (remote_debug)
00872     fprintf_unfiltered (gdb_stdlog, "pc => 0x%lx\n", pc_addr);
00873 
00874   return inferior_ptid;
00875 }
00876 
00877 /* Terminate the open connection to the remote debugger.
00878    Use this when you want to detach and do something else
00879    with your gdb.  */
00880 static void
00881 m32r_detach (struct target_ops *ops, char *args, int from_tty)
00882 {
00883   if (remote_debug)
00884     fprintf_unfiltered (gdb_stdlog, "m32r_detach(%d)\n", from_tty);
00885 
00886   m32r_resume (ops, inferior_ptid, 0, GDB_SIGNAL_0);
00887 
00888   /* Calls m32r_close to do the real work.  */
00889   unpush_target (ops);
00890   if (from_tty)
00891     fprintf_unfiltered (gdb_stdlog, "Ending remote %s debugging\n",
00892                         target_shortname);
00893 }
00894 
00895 /* Return the id of register number REGNO.  */
00896 
00897 static int
00898 get_reg_id (int regno)
00899 {
00900   switch (regno)
00901     {
00902     case 20:
00903       return SDI_REG_BBPC;
00904     case 21:
00905       return SDI_REG_BPC;
00906     case 22:
00907       return SDI_REG_ACCL;
00908     case 23:
00909       return SDI_REG_ACCH;
00910     case 24:
00911       return SDI_REG_EVB;
00912     }
00913 
00914   return regno;
00915 }
00916 
00917 /* Fetch register REGNO, or all registers if REGNO is -1.
00918    Returns errno value.  */
00919 static void
00920 m32r_fetch_register (struct target_ops *ops,
00921                      struct regcache *regcache, int regno)
00922 {
00923   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00924   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00925   unsigned long val, val2, regid;
00926 
00927   if (regno == -1)
00928     {
00929       for (regno = 0;
00930            regno < gdbarch_num_regs (get_regcache_arch (regcache));
00931            regno++)
00932         m32r_fetch_register (ops, regcache, regno);
00933     }
00934   else
00935     {
00936       gdb_byte buffer[MAX_REGISTER_SIZE];
00937 
00938       regid = get_reg_id (regno);
00939       send_one_arg_cmd (SDI_READ_CPU_REG, regid);
00940       val = recv_long_data ();
00941 
00942       if (regid == SDI_REG_PSW)
00943         {
00944           send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
00945           val2 = recv_long_data ();
00946           val = ((0x00cf & val2) << 8) | ((0xcf00 & val) >> 8);
00947         }
00948 
00949       if (remote_debug)
00950         fprintf_unfiltered (gdb_stdlog, "m32r_fetch_register(%d,0x%08lx)\n",
00951                             regno, val);
00952 
00953       /* We got the number the register holds, but gdb expects to see a
00954          value in the target byte ordering.  */
00955       store_unsigned_integer (buffer, 4, byte_order, val);
00956       regcache_raw_supply (regcache, regno, buffer);
00957     }
00958   return;
00959 }
00960 
00961 /* Store register REGNO, or all if REGNO == 0.
00962    Return errno value.  */
00963 static void
00964 m32r_store_register (struct target_ops *ops,
00965                      struct regcache *regcache, int regno)
00966 {
00967   int regid;
00968   ULONGEST regval, tmp;
00969 
00970   if (regno == -1)
00971     {
00972       for (regno = 0;
00973            regno < gdbarch_num_regs (get_regcache_arch (regcache));
00974            regno++)
00975         m32r_store_register (ops, regcache, regno);
00976     }
00977   else
00978     {
00979       regcache_cooked_read_unsigned (regcache, regno, &regval);
00980       regid = get_reg_id (regno);
00981 
00982       if (regid == SDI_REG_PSW)
00983         {
00984           unsigned long psw, bbpsw;
00985 
00986           send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_PSW);
00987           psw = recv_long_data ();
00988 
00989           send_one_arg_cmd (SDI_READ_CPU_REG, SDI_REG_BBPSW);
00990           bbpsw = recv_long_data ();
00991 
00992           tmp = (0x00cf & psw) | ((0x00cf & regval) << 8);
00993           send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_PSW, tmp);
00994 
00995           tmp = (0x0030 & bbpsw) | ((0xcf00 & regval) >> 8);
00996           send_two_arg_cmd (SDI_WRITE_CPU_REG, SDI_REG_BBPSW, tmp);
00997         }
00998       else
00999         {
01000           send_two_arg_cmd (SDI_WRITE_CPU_REG, regid, regval);
01001         }
01002 
01003       if (remote_debug)
01004         fprintf_unfiltered (gdb_stdlog, "m32r_store_register(%d,0x%08lu)\n",
01005                             regno, (unsigned long) regval);
01006     }
01007 }
01008 
01009 /* Get ready to modify the registers array.  On machines which store
01010    individual registers, this doesn't need to do anything.  On machines
01011    which store all the registers in one fell swoop, this makes sure
01012    that registers contains all the registers from the program being
01013    debugged.  */
01014 
01015 static void
01016 m32r_prepare_to_store (struct regcache *regcache)
01017 {
01018   /* Do nothing, since we can store individual regs.  */
01019   if (remote_debug)
01020     fprintf_unfiltered (gdb_stdlog, "m32r_prepare_to_store()\n");
01021 }
01022 
01023 static void
01024 m32r_files_info (struct target_ops *target)
01025 {
01026   const char *file = "nothing";
01027 
01028   if (exec_bfd)
01029     {
01030       file = bfd_get_filename (exec_bfd);
01031       printf_filtered ("\tAttached to %s running program %s\n",
01032                        chip_name, file);
01033     }
01034 }
01035 
01036 /* Read/Write memory.  */
01037 static int
01038 m32r_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len,
01039                   int write,
01040                   struct mem_attrib *attrib, struct target_ops *target)
01041 {
01042   unsigned long taddr;
01043   unsigned char buf[0x2000];
01044   int ret, c;
01045 
01046   taddr = memaddr;
01047 
01048   if (!mmu_on)
01049     {
01050       if ((taddr & 0xa0000000) == 0x80000000)
01051         taddr &= 0x7fffffff;
01052     }
01053 
01054   if (remote_debug)
01055     {
01056       if (write)
01057         fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%d,write)\n",
01058                             paddress (target_gdbarch (), memaddr), len);
01059       else
01060         fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory(%s,%d,read)\n",
01061                             paddress (target_gdbarch (), memaddr), len);
01062     }
01063 
01064   if (write)
01065     {
01066       buf[0] = SDI_WRITE_MEMORY;
01067       store_long_parameter (buf + 1, taddr);
01068       store_long_parameter (buf + 5, len);
01069       if (len < 0x1000)
01070         {
01071           memcpy (buf + 9, myaddr, len);
01072           ret = send_data (buf, len + 9) - 9;
01073         }
01074       else
01075         {
01076           if (serial_write (sdi_desc, buf, 9) != 0)
01077             {
01078               if (remote_debug)
01079                 fprintf_unfiltered (gdb_stdlog,
01080                                     "m32r_xfer_memory() failed\n");
01081               return 0;
01082             }
01083           ret = send_data (myaddr, len);
01084         }
01085     }
01086   else
01087     {
01088       buf[0] = SDI_READ_MEMORY;
01089       store_long_parameter (buf + 1, taddr);
01090       store_long_parameter (buf + 5, len);
01091       if (serial_write (sdi_desc, buf, 9) != 0)
01092         {
01093           if (remote_debug)
01094             fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
01095           return 0;
01096         }
01097 
01098       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
01099       if (c < 0 || c == '-')
01100         {
01101           if (remote_debug)
01102             fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() failed\n");
01103           return 0;
01104         }
01105 
01106       ret = recv_data (myaddr, len);
01107     }
01108 
01109   if (ret <= 0)
01110     {
01111       if (remote_debug)
01112         fprintf_unfiltered (gdb_stdlog, "m32r_xfer_memory() fails\n");
01113       return 0;
01114     }
01115 
01116   return ret;
01117 }
01118 
01119 static void
01120 m32r_kill (struct target_ops *ops)
01121 {
01122   if (remote_debug)
01123     fprintf_unfiltered (gdb_stdlog, "m32r_kill()\n");
01124 
01125   inferior_ptid = null_ptid;
01126   delete_thread_silent (remote_m32r_ptid);
01127 
01128   return;
01129 }
01130 
01131 /* Clean up when a program exits.
01132 
01133    The program actually lives on in the remote processor's RAM, and may be
01134    run again without a download.  Don't leave it full of breakpoint
01135    instructions.  */
01136 
01137 static void
01138 m32r_mourn_inferior (struct target_ops *ops)
01139 {
01140   if (remote_debug)
01141     fprintf_unfiltered (gdb_stdlog, "m32r_mourn_inferior()\n");
01142 
01143   remove_breakpoints ();
01144   generic_mourn_inferior ();
01145 }
01146 
01147 static int
01148 m32r_insert_breakpoint (struct gdbarch *gdbarch,
01149                         struct bp_target_info *bp_tgt)
01150 {
01151   CORE_ADDR addr = bp_tgt->placed_address;
01152   int ib_breakpoints;
01153   unsigned char buf[13];
01154   int i, c;
01155 
01156   if (remote_debug)
01157     fprintf_unfiltered (gdb_stdlog, "m32r_insert_breakpoint(%s,...)\n",
01158                         paddress (gdbarch, addr));
01159 
01160   if (use_ib_breakpoints)
01161     ib_breakpoints = max_ib_breakpoints;
01162   else
01163     ib_breakpoints = 0;
01164 
01165   for (i = 0; i < MAX_BREAKPOINTS; i++)
01166     {
01167       if (bp_address[i] == 0xffffffff)
01168         {
01169           bp_address[i] = addr;
01170           if (i >= ib_breakpoints)
01171             {
01172               buf[0] = SDI_READ_MEMORY;
01173               if (mmu_on)
01174                 store_long_parameter (buf + 1, addr & 0xfffffffc);
01175               else
01176                 store_long_parameter (buf + 1, addr & 0x7ffffffc);
01177               store_long_parameter (buf + 5, 4);
01178               serial_write (sdi_desc, buf, 9);
01179               c = serial_readchar (sdi_desc, SDI_TIMEOUT);
01180               if (c != '-')
01181                 recv_data (bp_data[i], 4);
01182             }
01183           return 0;
01184         }
01185     }
01186 
01187   error (_("Too many breakpoints"));
01188   return 1;
01189 }
01190 
01191 static int
01192 m32r_remove_breakpoint (struct gdbarch *gdbarch,
01193                         struct bp_target_info *bp_tgt)
01194 {
01195   CORE_ADDR addr = bp_tgt->placed_address;
01196   int i;
01197 
01198   if (remote_debug)
01199     fprintf_unfiltered (gdb_stdlog, "m32r_remove_breakpoint(%s)\n",
01200                         paddress (gdbarch, addr));
01201 
01202   for (i = 0; i < MAX_BREAKPOINTS; i++)
01203     {
01204       if (bp_address[i] == addr)
01205         {
01206           bp_address[i] = 0xffffffff;
01207           break;
01208         }
01209     }
01210 
01211   return 0;
01212 }
01213 
01214 static void
01215 m32r_load (char *args, int from_tty)
01216 {
01217   struct cleanup *old_chain;
01218   asection *section;
01219   bfd *pbfd;
01220   bfd_vma entry;
01221   char *filename;
01222   int quiet;
01223   int nostart;
01224   struct timeval start_time, end_time;
01225   unsigned long data_count;     /* Number of bytes transferred to memory.  */
01226   static RETSIGTYPE (*prev_sigint) ();
01227 
01228   /* for direct tcp connections, we can do a fast binary download.  */
01229   quiet = 0;
01230   nostart = 0;
01231   filename = NULL;
01232 
01233   while (*args != '\000')
01234     {
01235       char *arg;
01236 
01237       args = skip_spaces (args);
01238 
01239       arg = args;
01240 
01241       while ((*args != '\000') && !isspace (*args))
01242         args++;
01243 
01244       if (*args != '\000')
01245         *args++ = '\000';
01246 
01247       if (*arg != '-')
01248         filename = arg;
01249       else if (strncmp (arg, "-quiet", strlen (arg)) == 0)
01250         quiet = 1;
01251       else if (strncmp (arg, "-nostart", strlen (arg)) == 0)
01252         nostart = 1;
01253       else
01254         error (_("Unknown option `%s'"), arg);
01255     }
01256 
01257   if (!filename)
01258     filename = get_exec_file (1);
01259 
01260   pbfd = gdb_bfd_open (filename, gnutarget, -1);
01261   if (pbfd == NULL)
01262     {
01263       perror_with_name (filename);
01264       return;
01265     }
01266   old_chain = make_cleanup_bfd_unref (pbfd);
01267 
01268   if (!bfd_check_format (pbfd, bfd_object))
01269     error (_("\"%s\" is not an object file: %s"), filename,
01270            bfd_errmsg (bfd_get_error ()));
01271 
01272   gettimeofday (&start_time, NULL);
01273   data_count = 0;
01274 
01275   interrupted = 0;
01276   prev_sigint = signal (SIGINT, gdb_cntrl_c);
01277 
01278   for (section = pbfd->sections; section; section = section->next)
01279     {
01280       if (bfd_get_section_flags (pbfd, section) & SEC_LOAD)
01281         {
01282           bfd_vma section_address;
01283           bfd_size_type section_size;
01284           file_ptr fptr;
01285           int n;
01286 
01287           section_address = bfd_section_lma (pbfd, section);
01288           section_size = bfd_get_section_size (section);
01289 
01290           if (!mmu_on)
01291             {
01292               if ((section_address & 0xa0000000) == 0x80000000)
01293                 section_address &= 0x7fffffff;
01294             }
01295 
01296           if (!quiet)
01297             printf_filtered ("[Loading section %s at 0x%lx (%d bytes)]\n",
01298                              bfd_get_section_name (pbfd, section),
01299                              (unsigned long) section_address,
01300                              (int) section_size);
01301 
01302           fptr = 0;
01303 
01304           data_count += section_size;
01305 
01306           n = 0;
01307           while (section_size > 0)
01308             {
01309               char unsigned buf[0x1000 + 9];
01310               int count;
01311 
01312               count = min (section_size, 0x1000);
01313 
01314               buf[0] = SDI_WRITE_MEMORY;
01315               store_long_parameter (buf + 1, section_address);
01316               store_long_parameter (buf + 5, count);
01317 
01318               bfd_get_section_contents (pbfd, section, buf + 9, fptr, count);
01319               if (send_data (buf, count + 9) <= 0)
01320                 error (_("Error while downloading %s section."),
01321                        bfd_get_section_name (pbfd, section));
01322 
01323               if (!quiet)
01324                 {
01325                   printf_unfiltered (".");
01326                   if (n++ > 60)
01327                     {
01328                       printf_unfiltered ("\n");
01329                       n = 0;
01330                     }
01331                   gdb_flush (gdb_stdout);
01332                 }
01333 
01334               section_address += count;
01335               fptr += count;
01336               section_size -= count;
01337 
01338               if (interrupted)
01339                 break;
01340             }
01341 
01342           if (!quiet && !interrupted)
01343             {
01344               printf_unfiltered ("done.\n");
01345               gdb_flush (gdb_stdout);
01346             }
01347         }
01348 
01349       if (interrupted)
01350         {
01351           printf_unfiltered ("Interrupted.\n");
01352           break;
01353         }
01354     }
01355 
01356   interrupted = 0;
01357   signal (SIGINT, prev_sigint);
01358 
01359   gettimeofday (&end_time, NULL);
01360 
01361   /* Make the PC point at the start address.  */
01362   if (exec_bfd)
01363     regcache_write_pc (get_current_regcache (),
01364                        bfd_get_start_address (exec_bfd));
01365 
01366   inferior_ptid = null_ptid;    /* No process now.  */
01367   delete_thread_silent (remote_m32r_ptid);
01368 
01369   /* This is necessary because many things were based on the PC at the time
01370      that we attached to the monitor, which is no longer valid now that we
01371      have loaded new code (and just changed the PC).  Another way to do this
01372      might be to call normal_stop, except that the stack may not be valid,
01373      and things would get horribly confused...  */
01374 
01375   clear_symtab_users (0);
01376 
01377   if (!nostart)
01378     {
01379       entry = bfd_get_start_address (pbfd);
01380 
01381       if (!quiet)
01382         printf_unfiltered ("[Starting %s at 0x%lx]\n", filename,
01383                            (unsigned long) entry);
01384     }
01385 
01386   print_transfer_performance (gdb_stdout, data_count, 0, &start_time,
01387                               &end_time);
01388 
01389   do_cleanups (old_chain);
01390 }
01391 
01392 static void
01393 m32r_stop (ptid_t ptid)
01394 {
01395   if (remote_debug)
01396     fprintf_unfiltered (gdb_stdlog, "m32r_stop()\n");
01397 
01398   send_cmd (SDI_STOP_CPU);
01399 
01400   return;
01401 }
01402 
01403 
01404 /* Tell whether this target can support a hardware breakpoint.  CNT
01405    is the number of hardware breakpoints already installed.  This
01406    implements the target_can_use_hardware_watchpoint macro.  */
01407 
01408 static int
01409 m32r_can_use_hw_watchpoint (int type, int cnt, int othertype)
01410 {
01411   return sdi_desc != NULL && cnt < max_access_breaks;
01412 }
01413 
01414 /* Set a data watchpoint.  ADDR and LEN should be obvious.  TYPE is 0
01415    for a write watchpoint, 1 for a read watchpoint, or 2 for a read/write
01416    watchpoint.  */
01417 
01418 static int
01419 m32r_insert_watchpoint (CORE_ADDR addr, int len, int type,
01420                         struct expression *cond)
01421 {
01422   int i;
01423 
01424   if (remote_debug)
01425     fprintf_unfiltered (gdb_stdlog, "m32r_insert_watchpoint(%s,%d,%d)\n",
01426                         paddress (target_gdbarch (), addr), len, type);
01427 
01428   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
01429     {
01430       if (ab_address[i] == 0x00000000)
01431         {
01432           ab_address[i] = addr;
01433           ab_size[i] = len;
01434           ab_type[i] = type;
01435           return 0;
01436         }
01437     }
01438 
01439   error (_("Too many watchpoints"));
01440   return 1;
01441 }
01442 
01443 static int
01444 m32r_remove_watchpoint (CORE_ADDR addr, int len, int type,
01445                         struct expression *cond)
01446 {
01447   int i;
01448 
01449   if (remote_debug)
01450     fprintf_unfiltered (gdb_stdlog, "m32r_remove_watchpoint(%s,%d,%d)\n",
01451                         paddress (target_gdbarch (), addr), len, type);
01452 
01453   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
01454     {
01455       if (ab_address[i] == addr)
01456         {
01457           ab_address[i] = 0x00000000;
01458           break;
01459         }
01460     }
01461 
01462   return 0;
01463 }
01464 
01465 static int
01466 m32r_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
01467 {
01468   int rc = 0;
01469 
01470   if (hit_watchpoint_addr != 0x00000000)
01471     {
01472       *addr_p = hit_watchpoint_addr;
01473       rc = 1;
01474     }
01475   return rc;
01476 }
01477 
01478 static int
01479 m32r_stopped_by_watchpoint (void)
01480 {
01481   CORE_ADDR addr;
01482 
01483   return m32r_stopped_data_address (&current_target, &addr);
01484 }
01485 
01486 /* Check to see if a thread is still alive.  */
01487 
01488 static int
01489 m32r_thread_alive (struct target_ops *ops, ptid_t ptid)
01490 {
01491   if (ptid_equal (ptid, remote_m32r_ptid))
01492     /* The main task is always alive.  */
01493     return 1;
01494 
01495   return 0;
01496 }
01497 
01498 /* Convert a thread ID to a string.  Returns the string in a static
01499    buffer.  */
01500 
01501 static char *
01502 m32r_pid_to_str (struct target_ops *ops, ptid_t ptid)
01503 {
01504   static char buf[64];
01505 
01506   if (ptid_equal (remote_m32r_ptid, ptid))
01507     {
01508       xsnprintf (buf, sizeof buf, "Thread <main>");
01509       return buf;
01510     }
01511 
01512   return normal_pid_to_str (ptid);
01513 }
01514 
01515 static void
01516 sdireset_command (char *args, int from_tty)
01517 {
01518   if (remote_debug)
01519     fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
01520 
01521   send_cmd (SDI_OPEN);
01522 
01523   inferior_ptid = null_ptid;
01524   delete_thread_silent (remote_m32r_ptid);
01525 }
01526 
01527 
01528 static void
01529 sdistatus_command (char *args, int from_tty)
01530 {
01531   unsigned char buf[4096];
01532   int i, c;
01533 
01534   if (remote_debug)
01535     fprintf_unfiltered (gdb_stdlog, "m32r_sdireset()\n");
01536 
01537   if (!sdi_desc)
01538     return;
01539 
01540   send_cmd (SDI_STATUS);
01541   for (i = 0; i < 4096; i++)
01542     {
01543       c = serial_readchar (sdi_desc, SDI_TIMEOUT);
01544       if (c < 0)
01545         return;
01546       buf[i] = c;
01547       if (c == 0)
01548         break;
01549     }
01550 
01551   printf_filtered ("%s", buf);
01552 }
01553 
01554 
01555 static void
01556 debug_chaos_command (char *args, int from_tty)
01557 {
01558   unsigned char buf[3];
01559 
01560   buf[0] = SDI_SET_ATTR;
01561   buf[1] = SDI_ATTR_CACHE;
01562   buf[2] = SDI_CACHE_TYPE_CHAOS;
01563   send_data (buf, 3);
01564 }
01565 
01566 
01567 static void
01568 use_debug_dma_command (char *args, int from_tty)
01569 {
01570   unsigned char buf[3];
01571 
01572   buf[0] = SDI_SET_ATTR;
01573   buf[1] = SDI_ATTR_MEM_ACCESS;
01574   buf[2] = SDI_MEM_ACCESS_DEBUG_DMA;
01575   send_data (buf, 3);
01576 }
01577 
01578 static void
01579 use_mon_code_command (char *args, int from_tty)
01580 {
01581   unsigned char buf[3];
01582 
01583   buf[0] = SDI_SET_ATTR;
01584   buf[1] = SDI_ATTR_MEM_ACCESS;
01585   buf[2] = SDI_MEM_ACCESS_MON_CODE;
01586   send_data (buf, 3);
01587 }
01588 
01589 
01590 static void
01591 use_ib_breakpoints_command (char *args, int from_tty)
01592 {
01593   use_ib_breakpoints = 1;
01594 }
01595 
01596 static void
01597 use_dbt_breakpoints_command (char *args, int from_tty)
01598 {
01599   use_ib_breakpoints = 0;
01600 }
01601 
01602 static int
01603 m32r_return_one (struct target_ops *target)
01604 {
01605   return 1;
01606 }
01607 
01608 /* Implementation of the to_has_execution method.  */
01609 
01610 static int
01611 m32r_has_execution (struct target_ops *target, ptid_t the_ptid)
01612 {
01613   return 1;
01614 }
01615 
01616 /* Define the target subroutine names.  */
01617 
01618 struct target_ops m32r_ops;
01619 
01620 static void
01621 init_m32r_ops (void)
01622 {
01623   m32r_ops.to_shortname = "m32rsdi";
01624   m32r_ops.to_longname = "Remote M32R debugging over SDI interface";
01625   m32r_ops.to_doc = "Use an M32R board using SDI debugging protocol.";
01626   m32r_ops.to_open = m32r_open;
01627   m32r_ops.to_close = m32r_close;
01628   m32r_ops.to_detach = m32r_detach;
01629   m32r_ops.to_resume = m32r_resume;
01630   m32r_ops.to_wait = m32r_wait;
01631   m32r_ops.to_fetch_registers = m32r_fetch_register;
01632   m32r_ops.to_store_registers = m32r_store_register;
01633   m32r_ops.to_prepare_to_store = m32r_prepare_to_store;
01634   m32r_ops.deprecated_xfer_memory = m32r_xfer_memory;
01635   m32r_ops.to_files_info = m32r_files_info;
01636   m32r_ops.to_insert_breakpoint = m32r_insert_breakpoint;
01637   m32r_ops.to_remove_breakpoint = m32r_remove_breakpoint;
01638   m32r_ops.to_can_use_hw_breakpoint = m32r_can_use_hw_watchpoint;
01639   m32r_ops.to_insert_watchpoint = m32r_insert_watchpoint;
01640   m32r_ops.to_remove_watchpoint = m32r_remove_watchpoint;
01641   m32r_ops.to_stopped_by_watchpoint = m32r_stopped_by_watchpoint;
01642   m32r_ops.to_stopped_data_address = m32r_stopped_data_address;
01643   m32r_ops.to_kill = m32r_kill;
01644   m32r_ops.to_load = m32r_load;
01645   m32r_ops.to_create_inferior = m32r_create_inferior;
01646   m32r_ops.to_mourn_inferior = m32r_mourn_inferior;
01647   m32r_ops.to_stop = m32r_stop;
01648   m32r_ops.to_log_command = serial_log_command;
01649   m32r_ops.to_thread_alive = m32r_thread_alive;
01650   m32r_ops.to_pid_to_str = m32r_pid_to_str;
01651   m32r_ops.to_stratum = process_stratum;
01652   m32r_ops.to_has_all_memory = m32r_return_one;
01653   m32r_ops.to_has_memory = m32r_return_one;
01654   m32r_ops.to_has_stack = m32r_return_one;
01655   m32r_ops.to_has_registers = m32r_return_one;
01656   m32r_ops.to_has_execution = m32r_has_execution;
01657   m32r_ops.to_magic = OPS_MAGIC;
01658 };
01659 
01660 
01661 extern initialize_file_ftype _initialize_remote_m32r;
01662 
01663 void
01664 _initialize_remote_m32r (void)
01665 {
01666   int i;
01667 
01668   init_m32r_ops ();
01669 
01670   /* Initialize breakpoints.  */
01671   for (i = 0; i < MAX_BREAKPOINTS; i++)
01672     bp_address[i] = 0xffffffff;
01673 
01674   /* Initialize access breaks.  */
01675   for (i = 0; i < MAX_ACCESS_BREAKS; i++)
01676     ab_address[i] = 0x00000000;
01677 
01678   add_target (&m32r_ops);
01679 
01680   add_com ("sdireset", class_obscure, sdireset_command,
01681            _("Reset SDI connection."));
01682 
01683   add_com ("sdistatus", class_obscure, sdistatus_command,
01684            _("Show status of SDI connection."));
01685 
01686   add_com ("debug_chaos", class_obscure, debug_chaos_command,
01687            _("Debug M32R/Chaos."));
01688 
01689   add_com ("use_debug_dma", class_obscure, use_debug_dma_command,
01690            _("Use debug DMA mem access."));
01691   add_com ("use_mon_code", class_obscure, use_mon_code_command,
01692            _("Use mon code mem access."));
01693 
01694   add_com ("use_ib_break", class_obscure, use_ib_breakpoints_command,
01695            _("Set breakpoints by IB break."));
01696   add_com ("use_dbt_break", class_obscure, use_dbt_breakpoints_command,
01697            _("Set breakpoints by dbt."));
01698 
01699   /* Yes, 42000 is arbitrary.  The only sense out of it, is that it
01700      isn't 0.  */
01701   remote_m32r_ptid = ptid_build (42000, 0, 42000);
01702 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines