home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / remote-hms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-27  |  28.0 KB  |  1,403 lines

  1. /* Remote debugging interface for Hitachi HMS Monitor Version 1.0
  2.    Copyright 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.  Written by Steve Chamberlain
  4.    (sac@cygnus.com).
  5.  
  6.    This file is part of GDB.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2 of the License, or
  11.    (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "defs.h"
  23. #include "inferior.h"
  24. #include "wait.h"
  25. #include "value.h"
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <fcntl.h>
  29. #include <signal.h>
  30. #include <setjmp.h>
  31. #include <errno.h>
  32. #include "terminal.h"
  33. #include "target.h"
  34. #include "gdbcore.h"
  35. #include "serial.h"
  36. #include "remote-utils.h"
  37. /* External data declarations */
  38. extern int stop_soon_quietly;    /* for wait_for_inferior */
  39.  
  40. /* Forward data declarations */
  41. extern struct target_ops hms_ops;    /* Forward declaration */
  42.  
  43. /* Forward function declarations */
  44. static void hms_fetch_registers ();
  45. static int hms_store_registers ();
  46. static void hms_close ();
  47. static int hms_clear_breakpoints ();
  48.  
  49. extern struct target_ops hms_ops;
  50. static void hms_drain ();
  51. static void add_commands ();
  52. static void remove_commands ();
  53.  
  54. static int quiet = 1;        /* FIXME - can be removed after Dec '94 */
  55.  
  56. static DCACHE *remote_dcache;
  57. serial_t desc;
  58.  
  59.  
  60. /***********************************************************************
  61.  * I/O stuff stolen from remote-eb.c
  62.  ***********************************************************************/
  63.  
  64. static int timeout = 2;
  65.  
  66. static const char *dev_name;
  67.  
  68. /* Descriptor for I/O to remote machine.  Initialize it to -1 so that
  69.    hms_open knows that we don't have a file open when the program
  70.    starts.  */
  71.  
  72. static int before = 0xdead;
  73. static int is_open = 0;
  74. static int after = 0xdead;
  75. int
  76. check_open ()
  77. {
  78.   if (before != 0xdead
  79.       || after != 0xdead)
  80.     printf ("OUTCH! \n");
  81.   if (!is_open)
  82.     {
  83.       error ("remote device not open");
  84.     }
  85. }
  86.  
  87. #define ON    1
  88. #define OFF    0
  89.  
  90. /* Read a character from the remote system, doing all the fancy
  91.    timeout stuff.  */
  92. static int
  93. readchar ()
  94. {
  95.   int buf;
  96.  
  97.   buf = SERIAL_READCHAR (desc, timeout);
  98.  
  99.   if (buf == SERIAL_TIMEOUT)
  100.     {
  101.       hms_write (".\r\n", 3);
  102.       error ("Timeout reading from remote system.");
  103.     }
  104.   if (buf == SERIAL_ERROR)
  105.     {
  106.       error ("Serial port error!");
  107.     }
  108.  
  109.   if (!quiet || remote_debug)
  110.     printf_unfiltered ("%c", buf);
  111.  
  112.   return buf & 0x7f;
  113. }
  114.  
  115. static void 
  116. flush ()
  117. {
  118.   while (1)
  119.     {
  120.       int b = SERIAL_READCHAR (desc, 0);
  121.       if (b == SERIAL_TIMEOUT)
  122.     return;
  123.     }
  124. }
  125.  
  126. static int
  127. readchar_nofail ()
  128. {
  129.   int buf;
  130.  
  131.   buf = SERIAL_READCHAR (desc, timeout);
  132.   if (buf == SERIAL_TIMEOUT)
  133.     buf = 0;
  134.   if (!quiet || remote_debug)
  135.     printf_unfiltered ("%c", buf);
  136.  
  137.   return buf & 0x7f;
  138.  
  139. }
  140.  
  141. /* Keep discarding input from the remote system, until STRING is found.
  142.    Let the user break out immediately.  */
  143. static void
  144. expect (string)
  145.      char *string;
  146. {
  147.   char *p = string;
  148.   char c;
  149.   immediate_quit = 1;
  150.   while (1)
  151.     {
  152.       c = readchar ();
  153.       if (c == *p)
  154.     {
  155.       p++;
  156.       if (*p == '\0')
  157.         {
  158.           immediate_quit = 0;
  159.           return;
  160.         }
  161.     }
  162.       else
  163.     {
  164.       p = string;
  165.       if (c == *p)
  166.         p++;
  167.     }
  168.     }
  169. }
  170.  
  171. /* Keep discarding input until we see the hms prompt.
  172.  
  173.    The convention for dealing with the prompt is that you
  174.    o give your command
  175.    o *then* wait for the prompt.
  176.  
  177.    Thus the last thing that a procedure does with the serial line
  178.    will be an expect_prompt().  Exception:  hms_resume does not
  179.    wait for the prompt, because the terminal is being handed over
  180.    to the inferior.  However, the next thing which happens after that
  181.    is a hms_wait which does wait for the prompt.
  182.    Note that this includes abnormal exit, e.g. error().  This is
  183.    necessary to prevent getting into states from which we can't
  184.    recover.  */
  185. static void
  186. expect_prompt ()
  187. {
  188.   expect ("HMS>");
  189. }
  190.  
  191. /* Get a hex digit from the remote system & return its value.
  192.    If ignore_space is nonzero, ignore spaces (not newline, tab, etc).  */
  193. static int
  194. get_hex_digit (ignore_space)
  195.      int ignore_space;
  196. {
  197.   int ch;
  198.  
  199.   while (1)
  200.     {
  201.       ch = readchar ();
  202.       if (ch >= '0' && ch <= '9')
  203.     return ch - '0';
  204.       else if (ch >= 'A' && ch <= 'F')
  205.     return ch - 'A' + 10;
  206.       else if (ch >= 'a' && ch <= 'f')
  207.     return ch - 'a' + 10;
  208.       else if (ch == ' ' && ignore_space)
  209.     ;
  210.       else
  211.     {
  212.       expect_prompt ();
  213.       error ("Invalid hex digit from remote system.");
  214.     }
  215.     }
  216. }
  217.  
  218. /* Get a byte from hms_desc and put it in *BYT.  Accept any number
  219.    leading spaces.  */
  220. static void
  221. get_hex_byte (byt)
  222.      char *byt;
  223. {
  224.   int val;
  225.  
  226.   val = get_hex_digit (1) << 4;
  227.   val |= get_hex_digit (0);
  228.   *byt = val;
  229. }
  230.  
  231. /* Read a 32-bit hex word from the hms, preceded by a space  */
  232. static long
  233. get_hex_word ()
  234. {
  235.   long val;
  236.   int j;
  237.  
  238.   val = 0;
  239.   for (j = 0; j < 8; j++)
  240.     val = (val << 4) + get_hex_digit (j == 0);
  241.   return val;
  242. }
  243.  
  244. /* Called when SIGALRM signal sent due to alarm() timeout.  */
  245.  
  246. /* Number of SIGTRAPs we need to simulate.  That is, the next
  247.    NEED_ARTIFICIAL_TRAP calls to hms_wait should just return
  248.    SIGTRAP without actually waiting for anything.  */
  249.  
  250. static int need_artificial_trap = 0;
  251.  
  252. void
  253. hms_kill (arg, from_tty)
  254.      char *arg;
  255.      int from_tty;
  256. {
  257.  
  258. }
  259.  
  260. /* This is called not only when we first attach, but also when the
  261.    user types "run" after having attached.  */
  262. void
  263. hms_create_inferior (execfile, args, env)
  264.      char *execfile;
  265.      char *args;
  266.      char **env;
  267. {
  268.   int entry_pt;
  269.   char buffer[100];
  270.  
  271.   if (args && *args)
  272.     error ("Can't pass arguments to remote hms process.");
  273.  
  274.   if (execfile == 0 || exec_bfd == 0)
  275.     error ("No exec file specified");
  276.  
  277.   entry_pt = (int) bfd_get_start_address (exec_bfd);
  278.   check_open ();
  279.  
  280.   hms_kill (NULL, NULL);
  281.   hms_clear_breakpoints ();
  282.   init_wait_for_inferior ();
  283.   hms_write_cr ("");
  284.   expect_prompt ();
  285.  
  286.   insert_breakpoints ();    /* Needed to get correct instruction in cache */
  287.   proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
  288. }
  289.  
  290. /* Open a connection to a remote debugger.
  291.    NAME is the filename used for communication, then a space,
  292.    then the baud rate.
  293.  */
  294.  
  295. static char *
  296. find_end_of_word (s)
  297.      char *s;
  298. {
  299.   while (*s && !isspace (*s))
  300.     s++;
  301.   return s;
  302. }
  303.  
  304. static char *
  305. get_word (p)
  306.      char **p;
  307. {
  308.   char *s = *p;
  309.   char *word;
  310.   char *copy;
  311.   size_t len;
  312.  
  313.   while (isspace (*s))
  314.     s++;
  315.  
  316.   word = s;
  317.  
  318.   len = 0;
  319.  
  320.   while (*s && !isspace (*s))
  321.     {
  322.       s++;
  323.       len++;
  324.  
  325.     }
  326.   copy = xmalloc (len + 1);
  327.   memcpy (copy, word, len);
  328.   copy[len] = 0;
  329.   *p = s;
  330.   return copy;
  331. }
  332.  
  333. static int baudrate = 9600;
  334.  
  335. static int
  336. is_baudrate_right ()
  337. {
  338.   int ok;
  339.  
  340.   /* Put this port into NORMAL mode, send the 'normal' character */
  341.  
  342.   hms_write ("\001", 1);    /* Control A */
  343.   hms_write ("\r\n", 2);    /* Cr */
  344.  
  345.   while (1)
  346.     {
  347.       ok = SERIAL_READCHAR (desc, timeout);
  348.       if (ok < 0)
  349.     break;
  350.     }
  351.  
  352.   hms_write ("r", 1);
  353.  
  354.   if (readchar_nofail () == 'r')
  355.     return 1;
  356.  
  357.   /* Not the right baudrate, or the board's not on */
  358.   return 0;
  359. }
  360. static void
  361. set_rate ()
  362. {
  363.   if (!SERIAL_SETBAUDRATE (desc, baudrate))
  364.     error ("Can't set baudrate");
  365. }
  366.  
  367.  
  368.  
  369. /* Close out all files and local state before this target loses control. */
  370.  
  371. static void
  372. hms_close (quitting)
  373.      int quitting;
  374. {
  375.   /* Clear any break points */
  376.   remove_commands ();
  377.   hms_clear_breakpoints ();
  378.   sleep (1);            /* Let any output make it all the way back */
  379.   if (is_open)
  380.     {
  381.       SERIAL_WRITE (desc, "R\r\n", 3);
  382.       SERIAL_CLOSE (desc);
  383.     }
  384.   is_open = 0;
  385. }
  386.  
  387. /* Terminate the open connection to the remote debugger.  Use this
  388.    when you want to detach and do something else with your gdb.  */ void
  389. hms_detach (args, from_tty)
  390.      char *args;
  391.      int from_tty;
  392. {
  393.   if (is_open)
  394.     {
  395.       hms_clear_breakpoints ();
  396.     }
  397.  
  398.   pop_target ();        /* calls hms_close to do the real work
  399.                  */
  400.   if (from_tty)
  401.     printf_filtered ("Ending remote %s debugging\n",
  402.              target_shortname);
  403. }
  404.  
  405. /* Tell the remote machine to resume.  */
  406.  
  407. void
  408. hms_resume (pid, step, sig)
  409.      int pid, step;
  410.      enum target_signal
  411.        sig;
  412. {
  413.   dcache_flush (remote_dcache);
  414.  
  415.   if (step)
  416.     {
  417.       hms_write_cr ("s");
  418.       expect ("Step>");
  419.  
  420.       /* Force the next hms_wait to return a trap.  Not doing anything
  421.          about I/O from the target means that the user has to type "continue"
  422.          to see any.  FIXME, this should be fixed.  */
  423.       need_artificial_trap = 1;
  424.     }
  425.   else
  426.     {
  427.       hms_write_cr ("g");
  428.       expect ("g");
  429.     }
  430. }
  431.  
  432. /* Wait until the remote machine stops, then return, storing status in
  433.    STATUS just as `wait' would.  */
  434.  
  435. int
  436. hms_wait (pid, status)
  437.      int pid;
  438.      struct target_waitstatus *status;
  439. {
  440.   /* Strings to look for.  '?' means match any single character.  Note
  441.      that with the algorithm we use, the initial character of the string
  442.      cannot recur in the string, or we will not find some cases of the
  443.      string in the input.  */
  444.  
  445.   static char bpt[] = "At breakpoint:";
  446.  
  447.   /* It would be tempting to look for "\n[__exit + 0x8]\n" but that
  448.      requires loading symbols with "yc i" and even if we did do that we
  449.      don't know that the file has symbols.  */
  450.   static char exitmsg[] = "HMS>";
  451.   char *bp = bpt;
  452.   char *ep = exitmsg;
  453.  
  454.   /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars.
  455.    */
  456.   char swallowed[50];
  457.  
  458.   /* Current position in swallowed.  */
  459.   char *swallowed_p = swallowed;
  460.  
  461.   int ch;
  462.   int ch_handled;
  463.   int old_timeout = timeout;
  464.   int
  465.     old_immediate_quit = immediate_quit;
  466.   int swallowed_cr = 0;
  467.  
  468.   status->kind = TARGET_WAITKIND_EXITED;
  469.   status->value.integer = 0;
  470.  
  471.   if (need_artificial_trap != 0)
  472.     {
  473.       status->kind =
  474.     TARGET_WAITKIND_STOPPED;
  475.       status->value.sig = TARGET_SIGNAL_TRAP;
  476.       need_artificial_trap--;
  477.       return 0;
  478.     }
  479.  
  480.   timeout = 5;            /* Don't time out for a while - user program is running.
  481.                  */
  482.   immediate_quit = 1;        /* Helps ability to QUIT */
  483.   while (1)
  484.     {
  485.       QUIT;            /* Let user quit and leave process running */
  486.       ch_handled = 0;
  487.       ch = readchar ();
  488.       if (ch == *bp)
  489.     {
  490.       bp++;
  491.       if (*bp == '\0')
  492.         break;
  493.       ch_handled = 1;
  494.  
  495.       *swallowed_p++ = ch;
  496.     }
  497.       else
  498.     {
  499.       bp = bpt;
  500.     }
  501.       if
  502.     (ch == *ep || *ep == '?')
  503.     {
  504.       ep++;
  505.       if (*ep == '\0')
  506.         break;
  507.  
  508.       if (!ch_handled)
  509.         *swallowed_p++ = ch;
  510.       ch_handled =
  511.         1;
  512.     }
  513.       else
  514.     {
  515.       ep = exitmsg;
  516.     }
  517.  
  518.       if (!ch_handled)
  519.     {
  520.       char *p;
  521.  
  522.       /* Print out any characters which have been swallowed.  */
  523.       for (p = swallowed; p < swallowed_p; ++p)
  524.         putc_unfiltered (*p);
  525.       swallowed_p = swallowed;
  526.  
  527.       if ((ch != '\r' && ch != '\n') || swallowed_cr > 10)
  528.         {
  529.           putc_unfiltered (ch);
  530.           swallowed_cr = 10;
  531.         }
  532.       swallowed_cr++;
  533.  
  534.     }
  535.     }
  536.   if (*bp == '\0')
  537.     {
  538.       status->kind = TARGET_WAITKIND_STOPPED;
  539.       status->value.sig = TARGET_SIGNAL_TRAP;
  540.       expect_prompt ();
  541.     }
  542.   else
  543.     {
  544.       status->kind = TARGET_WAITKIND_EXITED;
  545.       status->value.integer =
  546.     TARGET_SIGNAL_STOP;
  547.     }
  548.  
  549.   timeout = old_timeout;
  550.   immediate_quit = old_immediate_quit;
  551.   return
  552.     0;
  553. }
  554.  
  555. /* Return the name of register number REGNO in the form input and
  556.    output by hms.
  557.  
  558.    Returns a pointer to a static buffer containing the answer.  */
  559. static char *
  560. get_reg_name (regno)
  561.      int regno;
  562. {
  563.   static char *rn[] =
  564.   REGISTER_NAMES;
  565.  
  566.   return rn[regno];
  567. }
  568.  
  569. /* Read the remote registers.  */
  570.  
  571. static int
  572. gethex (length, start, ok)
  573.      unsigned int length;
  574.      char *start;
  575.      int *ok;
  576. {
  577.   int result = 0;
  578.  
  579.   while (length--)
  580.     {
  581.       result <<= 4;
  582.       if (*start >= 'a' && *start <= 'f')
  583.     {
  584.       result += *start - 'a' + 10;
  585.     }
  586.       else if (*start >= 'A' &&
  587.            *start <= 'F')
  588.     {
  589.       result += *start - 'A' + 10;
  590.     }
  591.       else if
  592.     (*start >= '0' && *start <= '9')
  593.     {
  594.       result += *start - '0';
  595.     }
  596.       else
  597.     *ok = 0;
  598.       start++;
  599.  
  600.     }
  601.   return result;
  602. }
  603. static int
  604. timed_read (buf, n, timeout)
  605.      char
  606.       *buf;
  607.  
  608. {
  609.   int i;
  610.   char c;
  611.  
  612.   i = 0;
  613.   while (i < n)
  614.     {
  615.       c = readchar ();
  616.  
  617.       if (c == 0)
  618.     return i;
  619.       buf[i] = c;
  620.       i++;
  621.  
  622.     }
  623.   return i;
  624. }
  625.  
  626. hms_write (a, l)
  627.      char *a;
  628. {
  629.   int i;
  630.  
  631.   SERIAL_WRITE (desc, a, l);
  632.  
  633.   if (!quiet || remote_debug)
  634.     {
  635.       printf_unfiltered ("<");
  636.       for (i = 0; i < l; i++)
  637.     {
  638.       printf_unfiltered ("%c", a[i]);
  639.     }
  640.       printf_unfiltered (">");
  641.     }
  642. }
  643.  
  644. hms_write_cr (s)
  645.      char *s;
  646. {
  647.   hms_write (s, strlen (s));
  648.   hms_write ("\r\n", 2);
  649. }
  650.  
  651. #ifdef GDB_TARGET_IS_H8500
  652.  
  653. /* H8/500 monitor reg dump looks like:
  654.  
  655.    HMS>r
  656.    PC:8000 SR:070C .7NZ.. CP:00 DP:00 EP:00 TP:00 BR:00
  657.    R0-R7: FF5A 0001 F4FE F500 0000 F528 F528 F4EE
  658.    HMS>
  659.  
  660.  
  661.  */
  662.  
  663. supply_val (n, size, ptr, segptr)
  664.      int n;
  665.      int size;
  666.      char *ptr;
  667.      char *segptr;
  668. {
  669.   int ok;
  670.   char raw[4];
  671.   switch (size)
  672.     {
  673.     case 2:
  674.       raw[0] = gethex (2, ptr, &ok);
  675.       raw[1] = gethex (2, ptr + 2, &ok);
  676.       supply_register (n, raw);
  677.       break;
  678.     case 1:
  679.       raw[0] = gethex (2, ptr, &ok);
  680.       supply_register (n, raw);
  681.       break;
  682.     case 4:
  683.       {
  684.     int v = gethex (4, ptr, &ok);
  685.     v |= gethex (2, segptr, &ok) << 16;
  686.     raw[0] = 0;
  687.     raw[1] = (v >> 16) & 0xff;
  688.     raw[2] = (v >> 8) & 0xff;
  689.     raw[3] = (v >> 0) & 0xff;
  690.     supply_register (n, raw);
  691.       }
  692.     }
  693.  
  694. }
  695. static void
  696. hms_fetch_register (dummy)
  697.      int dummy;
  698. {
  699. #define REGREPLY_SIZE 108
  700.   char linebuf[REGREPLY_SIZE + 1];
  701.   int i;
  702.   int s;
  703.   int gottok;
  704.  
  705.   LONGEST reg[NUM_REGS];
  706.   check_open ();
  707.  
  708.   do
  709.     {
  710.  
  711.       hms_write_cr ("r");
  712.       expect ("r");
  713.       s = timed_read (linebuf + 1, REGREPLY_SIZE, 1);
  714.  
  715.       linebuf[REGREPLY_SIZE] = 0;
  716.       gottok = 0;
  717.       if (linebuf[3] == 'P' &&
  718.       linebuf[4] == 'C' &&
  719.       linebuf[5] == ':' &&
  720.       linebuf[105] == 'H' &&
  721.       linebuf[106] == 'M' &&
  722.       linebuf[107] == 'S')
  723.     {
  724.  
  725.       /*
  726.          012
  727.          r**
  728.          -------1---------2---------3---------4---------5-----
  729.          345678901234567890123456789012345678901234567890123456
  730.          PC:8000 SR:070C .7NZ.. CP:00 DP:00 EP:00 TP:00 BR:00**
  731.          ---6---------7---------8---------9--------10----
  732.          789012345678901234567890123456789012345678901234
  733.          R0-R7: FF5A 0001 F4FE F500 0000 F528 F528 F4EE**
  734.  
  735.          56789
  736.          HMS>
  737.        */
  738.       gottok = 1;
  739.  
  740.  
  741.       supply_val (PC_REGNUM, 4, linebuf + 6, linebuf + 29);
  742.  
  743.       supply_val (CCR_REGNUM, 2, linebuf + 14);
  744.       supply_val (SEG_C_REGNUM, 1, linebuf + 29);
  745.       supply_val (SEG_D_REGNUM, 1, linebuf + 35);
  746.       supply_val (SEG_E_REGNUM, 1, linebuf + 41);
  747.       supply_val (SEG_T_REGNUM, 1, linebuf + 47);
  748.       for (i = 0; i < 8; i++)
  749.         {
  750.           static int sr[8] =
  751.           {35, 35, 35, 35,
  752.            41, 41, 47, 47};
  753.  
  754.           char raw[4];
  755.           char *src = linebuf + 64 + 5 * i;
  756.           char *segsrc = linebuf + sr[i];
  757.           supply_val (R0_REGNUM + i, 2, src);
  758.           supply_val (PR0_REGNUM + i, 4, src, segsrc);
  759.         }
  760.     }
  761.       if (!gottok)
  762.     {
  763.       hms_write_cr ("");
  764.       expect ("HMS>");
  765.     }
  766.     }
  767.   while (!gottok);
  768. }
  769. #endif
  770.  
  771. #ifdef GDB_TARGET_IS_H8300
  772. static void
  773. hms_fetch_register (dummy)
  774.      int dummy;
  775. {
  776. #define REGREPLY_SIZE 79
  777.   char linebuf[REGREPLY_SIZE + 1];
  778.   int i;
  779.   int s;
  780.   int gottok;
  781.  
  782.   unsigned LONGEST reg[NUM_REGS];
  783.  
  784.   check_open ();
  785.  
  786.   do
  787.     {
  788.       hms_write_cr ("r");
  789.  
  790.       s = timed_read (linebuf, 1, 1);
  791.  
  792.       while (linebuf[0] != 'r')
  793.     s = timed_read (linebuf, 1, 1);
  794.  
  795.       s = timed_read (linebuf + 1, REGREPLY_SIZE - 1, 1);
  796.  
  797.       linebuf[REGREPLY_SIZE] = 0;
  798.       gottok = 0;
  799.       if (linebuf[0] == 'r' &&
  800.       linebuf[3] == 'P' &&
  801.       linebuf[4] == 'C' &&
  802.       linebuf[5] == '=' &&
  803.       linebuf[75] == 'H' &&
  804.       linebuf[76] == 'M' &&
  805.       linebuf[77] == 'S')
  806.     {
  807.       /*
  808.          PC=XXXX CCR=XX:XXXXXXXX R0-R7= XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX
  809.          5436789012345678901234567890123456789012345678901234567890123456789012
  810.          0      1         2         3         4         5         6
  811.        */
  812.       gottok = 1;
  813.  
  814.       reg[PC_REGNUM] = gethex (4, linebuf + 6, &gottok);
  815.       reg[CCR_REGNUM] = gethex (2, linebuf + 15, &gottok);
  816.       for (i = 0; i < 8; i++)
  817.         {
  818.           reg[i] = gethex (4, linebuf + 34 + 5 * i, &gottok);
  819.         }
  820.     }
  821.     }
  822.   while (!gottok);
  823.   for (i = 0; i < NUM_REGS; i++)
  824.     {
  825.       char swapped[2];
  826.  
  827.       swapped[1] = reg[i];
  828.       swapped[0] = (reg[i]) >> 8;
  829.  
  830.       supply_register (i, swapped);
  831.     }
  832. }
  833. #endif
  834. /* Store register REGNO, or all if REGNO == -1.
  835.    Return errno value.  */
  836. static void
  837. hms_store_register (regno)
  838.      int regno;
  839. {
  840.   if (regno == -1)
  841.     {
  842.       for (regno = 0; regno < NUM_REGS; regno++)
  843.     {
  844.       hms_store_register (regno);
  845.     }
  846.     }
  847.   else
  848.     {
  849.       char *name = get_reg_name (regno);
  850.       char buffer[100];
  851.       /* Some regs dont really exist */
  852.       if (!(name[0] == 'p' && name[1] == 'r')
  853.       && !(name[0] == 'c' && name[1] == 'y')
  854.       && !(name[0] == 't' && name[1] == 'i')
  855.       && !(name[0] == 'i' && name[1] == 'n'))
  856.     {
  857.       sprintf (buffer, "r %s=%x", name, read_register (regno));
  858.       hms_write_cr (buffer);
  859.       expect_prompt ();
  860.     }
  861.     }
  862. }
  863.  
  864.  
  865. /* Get ready to modify the registers array.  On machines which store
  866.    individual registers, this doesn't need to do anything.  On machines
  867.    which store all the registers in one fell swoop, this makes sure
  868.    that registers contains all the registers from the program being
  869.    debugged.  */
  870.  
  871. void
  872. hms_prepare_to_store ()
  873. {
  874.   /* Do nothing, since we can store individual regs */
  875. }
  876.  
  877. static CORE_ADDR
  878. translate_addr (addr)
  879.      CORE_ADDR addr;
  880. {
  881.  
  882.   return (addr);
  883.  
  884. }
  885.  
  886. /* Read a word from remote address ADDR and return it.
  887.  * This goes through the data cache.
  888.  */
  889. int
  890. hms_fetch_word (addr)
  891.      CORE_ADDR addr;
  892. {
  893.   return dcache_fetch (remote_dcache, addr);
  894. }
  895.  
  896. /* Write a word WORD into remote address ADDR.
  897.    This goes through the data cache.  */
  898.  
  899. void
  900. hms_store_word (addr, word)
  901.      CORE_ADDR addr;
  902.      int word;
  903. {
  904.   dcache_poke (remote_dcache, addr, word);
  905. }
  906.  
  907. int
  908. hms_xfer_inferior_memory (memaddr, myaddr, len, write, target)
  909.      CORE_ADDR memaddr;
  910.      char *myaddr;
  911.      int len;
  912.      int write;
  913.      struct target_ops *target;    /* ignored */
  914. {
  915.   register int i;
  916.  
  917.   /* Round starting address down to longword boundary.  */
  918.   register CORE_ADDR addr;
  919.  
  920.   /* Round ending address up; get number of longwords that makes.  */
  921.   register int count;
  922.  
  923.   /* Allocate buffer of that many longwords.  */
  924.   register int *buffer;
  925.  
  926.   memaddr &= 0xffff;
  927.   addr = memaddr & -sizeof (int);
  928.   count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  929.  
  930.   buffer = (int *) alloca (count * sizeof (int));
  931.  
  932.   if (write)
  933.     {
  934.       /* Fill start and end extra bytes of buffer with existing memory data.  */
  935.  
  936.       if (addr != memaddr || len < (int) sizeof (int))
  937.     {
  938.       /* Need part of initial word -- fetch it.  */
  939.       buffer[0] = hms_fetch_word (addr);
  940.     }
  941.  
  942.       if (count > 1)        /* FIXME, avoid if even boundary */
  943.     {
  944.       buffer[count - 1]
  945.         = hms_fetch_word (addr + (count - 1) * sizeof (int));
  946.     }
  947.  
  948.       /* Copy data to be written over corresponding part of buffer */
  949.  
  950.       memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  951.  
  952.       /* Write the entire buffer.  */
  953.  
  954.       for (i = 0; i < count; i++, addr += sizeof (int))
  955.     {
  956.       errno = 0;
  957.       hms_store_word (addr, buffer[i]);
  958.       if (errno)
  959.         {
  960.           return 0;
  961.         }
  962.  
  963.     }
  964.     }
  965.   else
  966.     {
  967.       /* Read all the longwords */
  968.       for (i = 0; i < count; i++, addr += sizeof (int))
  969.     {
  970.       errno = 0;
  971.       buffer[i] = hms_fetch_word (addr);
  972.       if (errno)
  973.         {
  974.           return 0;
  975.         }
  976.       QUIT;
  977.     }
  978.  
  979.       /* Copy appropriate bytes out of the buffer.  */
  980.       memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  981.     }
  982.  
  983.   return len;
  984. }
  985.  
  986. int
  987. hms_write_inferior_memory (memaddr, myaddr, len)
  988.      CORE_ADDR memaddr;
  989.      unsigned char *myaddr;
  990.      int len;
  991. {
  992.   bfd_vma addr;
  993.   int done;
  994.   int todo;
  995.   char buffer[100];
  996.   done = 0;
  997.   hms_write_cr (".");
  998.   expect_prompt ();
  999.   while (done < len)
  1000.     {
  1001.       char *ptr = buffer;
  1002.       int thisgo;
  1003.       int idx;
  1004.  
  1005.       thisgo = len - done;
  1006.       if (thisgo > 20)
  1007.     thisgo = 20;
  1008.  
  1009.       sprintf (ptr, "M.B %4x =", memaddr + done);
  1010.       ptr += 10;
  1011.       for (idx = 0; idx < thisgo; idx++)
  1012.     {
  1013.       sprintf (ptr, "%2x ", myaddr[idx + done]);
  1014.       ptr += 3;
  1015.     }
  1016.       hms_write_cr (buffer);
  1017.       expect_prompt ();
  1018.       done += thisgo;
  1019.     }
  1020. }
  1021.  
  1022. void
  1023. hms_files_info ()
  1024. {
  1025.   char *file = "nothing";
  1026.  
  1027.   if (exec_bfd)
  1028.     file = bfd_get_filename (exec_bfd);
  1029.  
  1030.   if (exec_bfd)
  1031. #ifdef __GO32__
  1032.     printf_filtered ("\tAttached to DOS asynctsr and running program %s\n", file);
  1033. #else
  1034.     printf_filtered ("\tAttached to %s at %d baud and running program %s\n", dev_name, baudrate, file);
  1035. #endif
  1036.   printf_filtered ("\ton an H8/300 processor.\n");
  1037. }
  1038.  
  1039. /* Copy LEN bytes of data from debugger memory at MYADDR
  1040.    to inferior's memory at MEMADDR.  Returns errno value.
  1041.    * sb/sh instructions don't work on unaligned addresses, when TU=1.
  1042.  */
  1043.  
  1044. /* Read LEN bytes from inferior memory at MEMADDR.  Put the result
  1045.    at debugger address MYADDR.  Returns errno value.  */
  1046. int
  1047. hms_read_inferior_memory (memaddr, myaddr, len)
  1048.      CORE_ADDR memaddr;
  1049.      char *myaddr;
  1050.      int len;
  1051. {
  1052.   /* Align to nearest low 16 bits */
  1053.   int i;
  1054.  
  1055.   CORE_ADDR start = memaddr;
  1056.   CORE_ADDR end = memaddr + len - 1;
  1057.  
  1058.   int ok = 1;
  1059.  
  1060.   /*
  1061.      AAAA: XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX '................'
  1062.      012345678901234567890123456789012345678901234567890123456789012345
  1063.      0         1         2         3         4         5         6
  1064.    */
  1065.   char buffer[66];
  1066.  
  1067.   if (memaddr & 0xf)
  1068.     abort ();
  1069.   if (len != 16)
  1070.     abort ();
  1071.  
  1072.   sprintf (buffer, "m %4x %4x", start & 0xffff, end & 0xffff);
  1073.  
  1074.   flush ();
  1075.   hms_write_cr (buffer);
  1076.   /* drop the echo and newline */
  1077.   for (i = 0; i < 13; i++)
  1078.     readchar ();
  1079.  
  1080.   /* Grab the lines as they come out and fill the area */
  1081.   /* Skip over cr */
  1082.   while (1)
  1083.     {
  1084.       int p;
  1085.       int i;
  1086.       int addr;
  1087.       size_t idx;
  1088.  
  1089.       char byte[16];
  1090.  
  1091.       buffer[0] = readchar ();
  1092.       while (buffer[0] == '\r'
  1093.          || buffer[0] == '\n')
  1094.     buffer[0] = readchar ();
  1095.  
  1096.       if (buffer[0] == 'M')
  1097.     break;
  1098.  
  1099.       for (i = 1; i < 50; i++)
  1100.     {
  1101.       buffer[i] = readchar ();
  1102.     }
  1103.       /* sometimes we loose characters in the ascii representation of the 
  1104.          data.  I don't know where.  So just scan for the end of line */
  1105.       i = readchar ();
  1106.       while (i != '\n' && i != '\r')
  1107.     i = readchar ();
  1108.  
  1109.       /* Now parse the line */
  1110.  
  1111.       addr = gethex (4, buffer, &ok);
  1112.       idx = 6;
  1113.       for (p = 0; p < 16; p += 2)
  1114.     {
  1115.       byte[p] = gethex (2, buffer + idx, &ok);
  1116.       byte[p + 1] = gethex (2, buffer + idx + 2, &ok);
  1117.       idx += 5;
  1118.     }
  1119.  
  1120.       for (p = 0; p < 16; p++)
  1121.     {
  1122.       if (addr + p >= memaddr &&
  1123.           addr + p < memaddr + len)
  1124.         {
  1125.           myaddr[(addr + p) - memaddr] = byte[p];
  1126.  
  1127.         }
  1128.  
  1129.     }
  1130.     }
  1131. #ifdef GDB_TARGET_IS_H8500
  1132.   expect ("ore>");
  1133. #endif
  1134. #ifdef GDB_TARGET_IS_H8300
  1135.   expect ("emory>");
  1136. #endif
  1137.   hms_write_cr (".");
  1138.  
  1139.   expect_prompt ();
  1140.   return len;
  1141. }
  1142.  
  1143.  
  1144.  
  1145. #define MAX_BREAKS    16
  1146. static int num_brkpts = 0;
  1147. static int
  1148. hms_insert_breakpoint (addr, save)
  1149.      CORE_ADDR addr;
  1150.      char *save;        /* Throw away, let hms save instructions */
  1151. {
  1152.   check_open ();
  1153.  
  1154.   if (num_brkpts < MAX_BREAKS)
  1155.     {
  1156.       char buffer[100];
  1157.  
  1158.       num_brkpts++;
  1159.       sprintf (buffer, "b %x", addr & 0xffff);
  1160.       hms_write_cr (buffer);
  1161.       expect_prompt ();
  1162.       return (0);
  1163.     }
  1164.   else
  1165.     {
  1166.       fprintf_filtered (gdb_stderr,
  1167.               "Too many break points, break point not installed\n");
  1168.       return (1);
  1169.     }
  1170.  
  1171. }
  1172. static int
  1173. hms_remove_breakpoint (addr, save)
  1174.      CORE_ADDR addr;
  1175.      char *save;        /* Throw away, let hms save instructions */
  1176. {
  1177.   if (num_brkpts > 0)
  1178.     {
  1179.       char buffer[100];
  1180.  
  1181.       num_brkpts--;
  1182.       sprintf (buffer, "b - %x", addr & 0xffff);
  1183.       hms_write_cr (buffer);
  1184.       expect_prompt ();
  1185.  
  1186.     }
  1187.   return (0);
  1188. }
  1189.  
  1190. /* Clear the hmss notion of what the break points are */
  1191. static int
  1192. hms_clear_breakpoints ()
  1193. {
  1194.  
  1195.   if (is_open)
  1196.     {
  1197.       hms_write_cr ("b -");
  1198.       expect_prompt ();
  1199.     }
  1200.   num_brkpts = 0;
  1201. }
  1202. static void
  1203. hms_mourn ()
  1204. {
  1205.   hms_clear_breakpoints ();
  1206.   unpush_target (&hms_ops);
  1207.   generic_mourn_inferior ();
  1208. }
  1209.  
  1210. /* Put a command string, in args, out to the hms.  The hms is assumed to
  1211.    be in raw mode, all writing/reading done through desc.
  1212.    Ouput from the hms is placed on the users terminal until the
  1213.    prompt from the hms is seen.
  1214.    FIXME: Can't handle commands that take input.  */
  1215.  
  1216. void
  1217. hms_com (args, fromtty)
  1218.      char *args;
  1219.      int fromtty;
  1220. {
  1221.   check_open ();
  1222.  
  1223.   if (!args)
  1224.     return;
  1225.  
  1226.   /* Clear all input so only command relative output is displayed */
  1227.  
  1228.   hms_write_cr (args);
  1229. /*  hms_write ("\030", 1); */
  1230.   expect_prompt ();
  1231. }
  1232.  
  1233. static void
  1234. hms_open (name, from_tty)
  1235.      char *name;
  1236.      int from_tty;
  1237. {
  1238.   unsigned int prl;
  1239.   char *p;
  1240.  
  1241.   if (name == 0)
  1242.     {
  1243.       name = "";
  1244.     }
  1245.   if (is_open)
  1246.     hms_close (0);
  1247.   dev_name = strdup (name);
  1248.  
  1249.   if (!(desc = SERIAL_OPEN (dev_name)))
  1250.     perror_with_name ((char *) dev_name);
  1251.  
  1252.   SERIAL_RAW (desc);
  1253.   is_open = 1;
  1254.   push_target (&hms_ops);
  1255.   dcache_init (hms_read_inferior_memory,
  1256.            hms_write_inferior_memory);
  1257.  
  1258.   /* Hello?  Are you there?  */
  1259.   SERIAL_WRITE (desc, "\r\n", 2);
  1260.   expect_prompt ();
  1261.  
  1262.   /* Clear any break points */
  1263.   hms_clear_breakpoints ();
  1264.  
  1265.   printf_filtered ("Connected to remote board running HMS monitor.\n");
  1266.   add_commands ();
  1267. /*  hms_drain (); */
  1268. }
  1269.  
  1270. /* Define the target subroutine names */
  1271.  
  1272. struct target_ops hms_ops =
  1273. {
  1274.   "hms", "Remote HMS monitor",
  1275.   "Use the H8 evaluation board running the HMS monitor connected\n\
  1276. by a serial line.",
  1277.  
  1278.   hms_open, hms_close,
  1279.   0, hms_detach, hms_resume, hms_wait,    /* attach */
  1280.   hms_fetch_register, hms_store_register,
  1281.   hms_prepare_to_store,
  1282.   hms_xfer_inferior_memory,
  1283.   hms_files_info,
  1284.   hms_insert_breakpoint, hms_remove_breakpoint,        /* Breakpoints */
  1285.   0, 0, 0, 0, 0,        /* Terminal handling */
  1286.   hms_kill,            /* FIXME, kill */
  1287.   gr_load_image,
  1288.   0,                /* lookup_symbol */
  1289.   hms_create_inferior,        /* create_inferior */
  1290.   hms_mourn,            /* mourn_inferior FIXME */
  1291.   0,                /* can_run */
  1292.   0,                /* notice_signals */
  1293.   0,                /* to_stop */
  1294.   process_stratum, 0,        /* next */
  1295.   1, 1, 1, 1, 1,        /* all mem, mem, stack, regs, exec */
  1296.   0, 0,                /* Section pointers */
  1297.   OPS_MAGIC,            /* Always the last thing */
  1298. };
  1299.  
  1300. hms_quiet ()            /* FIXME - this routine can be removed after Dec '94 */
  1301. {
  1302.   quiet = !quiet;
  1303.   if (quiet)
  1304.     printf_filtered ("Snoop disabled\n");
  1305.   else
  1306.     printf_filtered ("Snoop enabled\n");
  1307.  
  1308.   printf_filtered ("`snoop' is obsolete, please use `set remotedebug'.\n");
  1309. }
  1310.  
  1311. hms_device (s)
  1312.      char *s;
  1313. {
  1314.   if (s)
  1315.     {
  1316.       dev_name = get_word (&s);
  1317.     }
  1318. }
  1319.  
  1320. static
  1321. hms_speed (s)
  1322.      char *s;
  1323. {
  1324.   check_open ();
  1325.  
  1326.   if (s)
  1327.     {
  1328.       char buffer[100];
  1329.       int newrate = atoi (s);
  1330.       int which = 0;
  1331.  
  1332.       if (SERIAL_SETBAUDRATE (desc, newrate))
  1333.     error ("Can't use %d baud\n", newrate);
  1334.  
  1335.       printf_filtered ("Checking target is in sync\n");
  1336.  
  1337.       printf_filtered ("Sending commands to set target to %d\n",
  1338.                baudrate);
  1339.  
  1340.       sprintf (buffer, "tm %d. N 8 1", baudrate);
  1341.       hms_write_cr (buffer);
  1342.     }
  1343. }
  1344.  
  1345. /***********************************************************************/
  1346.  
  1347. static void
  1348. hms_drain (args, fromtty)
  1349.      char *args;
  1350.      int fromtty;
  1351. {
  1352.   int c;
  1353.   while (1)
  1354.     {
  1355.       c = SERIAL_READCHAR (desc, 1);
  1356.       if (c == SERIAL_TIMEOUT)
  1357.     break;
  1358.       if (c == SERIAL_ERROR)
  1359.     break;
  1360.       if (c > ' ' && c < 127)
  1361.     printf ("%c", c & 0xff);
  1362.       else
  1363.     printf ("<%x>", c & 0xff);
  1364.     }
  1365. }
  1366.  
  1367. static void
  1368. add_commands ()
  1369. {
  1370.  
  1371.   add_com ("hms_drain", class_obscure, hms_drain,
  1372.        "Drain pending hms text buffers.");
  1373. }
  1374.  
  1375. static void
  1376. remove_commands ()
  1377. {
  1378.   extern struct cmd_list_element *cmdlist;
  1379.   delete_cmd ("hms-drain", &cmdlist);
  1380. }
  1381.  
  1382.  
  1383. void
  1384. _initialize_remote_hms ()
  1385. {
  1386.   add_target (&hms_ops);
  1387.  
  1388.   add_com ("hms <command>", class_obscure, hms_com,
  1389.        "Send a command to the HMS monitor.");
  1390.  
  1391.   /* FIXME - hms_quiet and `snoop' can be removed after Dec '94 */
  1392.   add_com ("snoop", class_obscure, hms_quiet,
  1393.        "Show what commands are going to the monitor (OBSOLETE - see 'set remotedebug')");
  1394.  
  1395.   add_com ("device", class_obscure, hms_device,
  1396.        "Set the terminal line for HMS communications");
  1397.  
  1398.   add_com ("speed", class_obscure, hms_speed,
  1399.        "Set the terminal line speed for HMS communications");
  1400.  
  1401.   dev_name = NULL;
  1402. }
  1403.