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

  1. /* Remote debugging interface for boot monitors, for GDB.
  2.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* This file was derived from various remote-* modules. It is a collection
  22.    of generic support functions so GDB can talk directly to a ROM based
  23.    monitor. This saves use from having to hack an exception based handler
  24.    into existance, and makes for quick porting.
  25.  
  26.    This module talks to a debug monitor called 'MONITOR', which
  27.    We communicate with MONITOR via either a direct serial line, or a TCP
  28.    (or possibly TELNET) stream to a terminal multiplexor,
  29.    which in turn talks to the target board.
  30. */
  31.  
  32. #include "defs.h"
  33. #include "gdbcore.h"
  34. #include "target.h"
  35. #include "wait.h"
  36. #include <varargs.h>
  37. #include <signal.h>
  38. #include <string.h>
  39. #include <sys/types.h>
  40. #include "command.h"
  41. #include "serial.h"
  42. #include "monitor.h"
  43. #include "remote-utils.h"
  44.  
  45. #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
  46. #define HAVE_SGTTY
  47. #endif
  48.  
  49. #ifdef HAVE_TERMIOS
  50. #include <termio.h>
  51. #include <termios.h>
  52. #  define TERMINAL struct termios
  53. #else
  54. #include <fcntl.h>
  55. #  define TERMINAL struct sgttyb
  56. #endif
  57. #include "terminal.h"
  58. #ifndef CSTOPB
  59. #define  CSTOPB  0x00000040
  60. #endif
  61.  
  62. static const char hexchars[]="0123456789abcdef";
  63. static char *hex2mem();
  64.  
  65. #define SWAP_TARGET_AND_HOST(buffer,len)                 \
  66.   do                                    \
  67.     {                                    \
  68.       if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER)                \
  69.     {                                \
  70.       char tmp;                            \
  71.       char *p = (char *)(buffer);                    \
  72.       char *q = ((char *)(buffer)) + len - 1;               \
  73.       for (; p < q; p++, q--)                     \
  74.         {                                \
  75.           tmp = *q;                            \
  76.           *q = *p;                            \
  77.           *p = tmp;                            \
  78.         }                                \
  79.     }                                \
  80.     }                                    \
  81.   while (0)
  82.  
  83. static void make_xmodem_packet();
  84. static void print_xmodem_packet();
  85. static void make_gdb_packet();
  86. static unsigned long ascii2hexword();
  87. static void hexword2ascii();
  88. static int tohex();
  89. static int from_hex();
  90.  
  91. struct monitor_ops *current_monitor;
  92. extern struct cmd_list_element *setlist;
  93. extern struct cmd_list_element *unsetlist;
  94. struct cmd_list_element *showlist;
  95. extern char *version;
  96. extern char *host_name;
  97. extern char *target_name;
  98.  
  99. static int hashmark;                /* flag set by "set hash" */
  100.  
  101. #define LOG_FILE "monitor.log"
  102. #if defined (LOG_FILE)
  103. FILE *log_file;
  104. #endif
  105.  
  106. static int timeout = 30;
  107. /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
  108.    and i386-stub.c.  Normally, no one would notice because it only matters
  109.    for writing large chunks of memory (e.g. in downloads).  Also, this needs
  110.    to be more than 400 if required to hold the registers (see below, where
  111.    we round it up based on REGISTER_BYTES).  */
  112. #define PBUFSIZ 400
  113.  
  114. /* 
  115.  * Descriptor for I/O to remote machine.  Initialize it to NULL so that
  116.  * monitor_open knows that we don't have a file open when the program starts.
  117.  */
  118. serial_t monitor_desc = NULL;
  119.  
  120. /* sets the download protocol, choices are srec, generic, boot */
  121. char *loadtype;
  122. static char *loadtype_str;
  123. static char *loadproto_str;
  124. static void set_loadtype_command();
  125. static void set_loadproto_command();
  126. static void monitor_load_srec();
  127. static int monitor_write_srec();
  128.  
  129. /*
  130.  * these definitions are for xmodem protocol
  131.  */
  132. #define SOH    0x01
  133. #define ACK    0x06
  134. #define NAK    0x15
  135. #define EOT    0x04
  136. #define CANCEL    0x18
  137. #define GETACK        getacknak(ACK)
  138. #define GETNAK        getacknak(NAK)
  139. #define XMODEM_DATASIZE    128        /* the data   size is ALWAYS 128 */
  140. #define XMODEM_PACKETSIZE    131    /* the packet size is ALWAYS 132 (zero based) */
  141. #define XMODEM        1
  142.  
  143. /*
  144.  * set_loadtype_command -- set the type for downloading. Check to make
  145.  *    sure you have a support protocol for this target.
  146.  */
  147. static void
  148. set_loadtype_command (ignore, from_tty, c)
  149.      char *ignore;
  150.      int from_tty;
  151.      struct cmd_list_element *c;
  152. {
  153.   char *tmp;
  154.   char *type;
  155.  
  156.   if (current_monitor == 0x0)
  157.     return;
  158.  
  159.     if (STREQ (LOADTYPES, "")) {
  160.     error ("No loadtype set");
  161.     return;
  162.   }
  163.   
  164.   tmp = savestring (LOADTYPES, strlen(LOADTYPES));
  165.   type = strtok(tmp, ",");
  166.   if (STREQ (type, (*(char **) c->var))) {
  167.       loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
  168.       return;
  169.     }
  170.   
  171.   while ((type = strtok (NULL, ",")) != (char *)NULL) {
  172.     if (STREQ (type, (*(char **) c->var)))
  173.       loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
  174.     return;
  175.   }
  176.   free (tmp);
  177.   error ("Loadtype \"%s\" does not exist.", (*(char **) c->var));
  178. }
  179. /*
  180.  * set_loadproto_command -- set the protocol for downloading. Check to make
  181.  *    sure you have a supported protocol for this target.
  182.  */
  183. static void
  184. set_loadproto_command (ignore, from_tty, c)
  185.      char *ignore;
  186.      int from_tty;
  187.      struct cmd_list_element *c;
  188. {
  189.   char *tmp;
  190.   char *type;
  191.  
  192.   if (current_monitor == 0x0)
  193.     return;
  194.  
  195.   if (STREQ (LOADPROTOS, "")) {
  196.     error ("No load protocols set");
  197.     return;
  198.   }
  199.   
  200.   tmp = savestring (LOADPROTOS, strlen(LOADPROTOS));
  201.   type = strtok(tmp, ",");
  202.   if (STREQ (type, (*(char **) c->var))) {
  203.       loadproto_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
  204.       return;
  205.     }
  206.   
  207.   while ((type = strtok (NULL, ",")) != (char *)NULL) {
  208.     if (STREQ (type, (*(char **) c->var)))
  209.       loadproto_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
  210.     return;
  211.   }
  212.   free (tmp);
  213.   error ("Load protocol \"%s\" does not exist.", (*(char **) c->var));
  214. }
  215.  
  216. /*
  217.  * printf_monitor -- send data to monitor.  Works just like printf.
  218.  */
  219. static void
  220. printf_monitor(va_alist)
  221.      va_dcl
  222. {
  223.   va_list args;
  224.   char *pattern;
  225.   char buf[200];
  226.   int i;
  227.  
  228.   va_start(args);
  229.  
  230.   pattern = va_arg(args, char *);
  231.  
  232.   vsprintf(buf, pattern, args);
  233.  
  234.   debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf);
  235.  
  236.   if (SERIAL_WRITE(monitor_desc, buf, strlen(buf)))
  237.     fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
  238. }
  239. /*
  240.  * write_monitor -- send raw data to monitor.
  241.  */
  242. static void
  243. write_monitor(data, len)
  244.      char data[];
  245.      int len;
  246. {
  247.   if (SERIAL_WRITE(monitor_desc, data, len))
  248.     fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno));
  249.  
  250.   *(data + len+1) = '\0';
  251.   debuglogs (1, "write_monitor(), Sending: \"%s\".", data);
  252.  
  253. }
  254.  
  255. /*
  256.  * debuglogs -- deal with debugging info to multiple sources. This takes
  257.  *    two real args, the first one is the level to be compared against 
  258.  *    the sr_get_debug() value, the second arg is a printf buffer and args
  259.  *    to be formatted and printed. A CR is added after each string is printed.
  260.  */
  261. void
  262. debuglogs(va_alist)
  263.      va_dcl
  264. {
  265.   va_list args;
  266.   char *pattern, *p;
  267.   char buf[200];
  268.   char newbuf[300];
  269.   int level, i;
  270.  
  271.   va_start(args);
  272.  
  273.   level = va_arg(args, int);            /* get the debug level */
  274.   if ((level <0) || (level > 100)) {
  275.     error ("Bad argument passed to debuglogs(), needs debug level");
  276.     return;
  277.   }
  278.       
  279.   pattern = va_arg(args, char *);        /* get the printf style pattern */
  280.  
  281.   vsprintf(buf, pattern, args);            /* format the string */
  282.   
  283.   /* convert some characters so it'll look right in the log */
  284.   p = newbuf;
  285.   for (i=0 ; buf[i] != '\0'; i++) {
  286.     switch (buf[i]) {
  287.     case '\n':                    /* newlines */
  288.       *p++ = '\\';
  289.       *p++ = 'n';
  290.       continue;
  291.     case '\r':                    /* carriage returns */
  292.       *p++ = '\\';
  293.       *p++ = 'r';
  294.       continue;
  295.     case '\033':                /* escape */
  296.       *p++ = '\\';
  297.       *p++ = 'e';
  298.       continue;
  299.     case '\t':                    /* tab */
  300.       *p++ = '\\';
  301.       *p++ = 't';
  302.       continue;
  303.     case '\b':                    /* backspace */
  304.       *p++ = '\\';
  305.       *p++ = 'b';
  306.       continue;
  307.     default:                    /* no change */
  308.       *p++ = buf[i];
  309.     }
  310.  
  311.     if (buf[i] < 26) {                /* modify control characters */
  312.       *p++ = '^';
  313.       *p++ = buf[i] + 'A';
  314.       continue;
  315.     }
  316.   }
  317.   *p = '\0';                    /* terminate the string */
  318.  
  319.   if (sr_get_debug() > level)
  320.     puts (newbuf);
  321.  
  322. #ifdef LOG_FILE                    /* write to the monitor log */
  323.   if (log_file != 0x0) {
  324.     fputs (newbuf, log_file);
  325.     fputc ('\n', log_file);
  326.     fflush (log_file);
  327.   }
  328. #endif
  329. }
  330.  
  331. /* readchar -- read a character from the remote system, doing all the fancy
  332.  *    timeout stuff.
  333.  */
  334. static int
  335. readchar(timeout)
  336.      int timeout;
  337. {
  338.   int c;
  339.  
  340.   c = SERIAL_READCHAR(monitor_desc, timeout);
  341.  
  342.   if (sr_get_debug() > 5) {
  343.     putchar(c & 0x7f);
  344.     debuglogs (5, "readchar: timeout = %d\n", timeout);
  345.   }
  346.  
  347. #ifdef LOG_FILE
  348.   if (isascii (c))
  349.     putc(c & 0x7f, log_file);
  350. #endif
  351.  
  352.   if (c >= 0)
  353.     return c & 0x7f;
  354.  
  355.   if (c == SERIAL_TIMEOUT) {
  356.     if (timeout == 0)
  357.       return c;        /* Polls shouldn't generate timeout errors */
  358.     error("Timeout reading from remote system.");
  359. #ifdef LOG_FILE
  360.       fputs ("ERROR: Timeout reading from remote system", log_file);
  361. #endif
  362.   }
  363.   perror_with_name("remote-monitor");
  364. }
  365.  
  366. /* 
  367.  * expect --  scan input from the remote system, until STRING is found.
  368.  *    If DISCARD is non-zero, then discard non-matching input, else print
  369.  *    it out. Let the user break out immediately.
  370.  */
  371. static void
  372. expect (string, discard)
  373.      char *string;
  374.      int discard;
  375. {
  376.   char *p = string;
  377.   int c;
  378.  
  379.  
  380.   debuglogs (1, "Expecting \"%s\".", string);
  381.  
  382.   immediate_quit = 1;
  383.   while (1) {
  384.     c = readchar(timeout);
  385.     if (!isascii (c))
  386.       continue;
  387.     if (c == *p++) {
  388.       if (*p == '\0') {
  389.     immediate_quit = 0;
  390.     debuglogs (4, "Matched");
  391.     return;
  392.       }
  393.     } else {
  394.       if (!discard) {
  395.     fwrite(string, 1, (p - 1) - string, stdout);
  396.     putchar((char)c);
  397.     fflush(stdout);
  398.       }
  399.       p = string;
  400.     }
  401.   }
  402. }
  403.  
  404. /* Keep discarding input until we see the MONITOR prompt.
  405.  
  406.    The convention for dealing with the prompt is that you
  407.    o give your command
  408.    o *then* wait for the prompt.
  409.  
  410.    Thus the last thing that a procedure does with the serial line
  411.    will be an expect_prompt().  Exception:  monitor_resume does not
  412.    wait for the prompt, because the terminal is being handed over
  413.    to the inferior.  However, the next thing which happens after that
  414.    is a monitor_wait which does wait for the prompt.
  415.    Note that this includes abnormal exit, e.g. error().  This is
  416.    necessary to prevent getting into states from which we can't
  417.    recover.  */
  418. static void
  419. expect_prompt(discard)
  420.      int discard;
  421. {
  422.   expect (PROMPT, discard);
  423. }
  424.  
  425. /*
  426.  * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise
  427.  */
  428. static int
  429. junk(ch)
  430.      char ch;
  431. {
  432.   switch (ch) {
  433.   case '\0':
  434.   case ' ':
  435.   case '-':
  436.   case '\t':
  437.   case '\r':
  438.   case '\n':
  439.     if (sr_get_debug() > 5)
  440.       debuglogs (5, "Ignoring \'%c\'.", ch);
  441.     return 1;
  442.   default:
  443.     if (sr_get_debug() > 5)
  444.       debuglogs (5, "Accepting \'%c\'.", ch);
  445.     return 0;
  446.   }
  447. }
  448.  
  449. /* 
  450.  *  get_hex_digit -- Get a hex digit from the remote system & return its value.
  451.  *        If ignore is nonzero, ignore spaces, newline & tabs.
  452.  */
  453. static int
  454. get_hex_digit(ignore)
  455.      int ignore;
  456. {
  457.   static int ch;
  458.   while (1) {
  459.     ch = readchar(timeout);
  460.     if (junk(ch))
  461.       continue;
  462.     if (sr_get_debug() > 4) {
  463.       debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch);
  464.     } else {
  465. #ifdef LOG_FILE                    /* write to the monitor log */
  466.       if (log_file != 0x0) {
  467.     fputs ("get_hex_digit() got a 0x", log_file);
  468.     fputc (ch, log_file);
  469.     fputc ('\n', log_file);
  470.     fflush (log_file);
  471.       }
  472. #endif
  473.     }
  474.  
  475.     if (ch >= '0' && ch <= '9')
  476.       return ch - '0';
  477.     else if (ch >= 'A' && ch <= 'F')
  478.       return ch - 'A' + 10;
  479.     else if (ch >= 'a' && ch <= 'f')
  480.       return ch - 'a' + 10;
  481.     else if (ch == ' ' && ignore)
  482.       ;
  483.     else {
  484.       expect_prompt(1);
  485.       debuglogs (4, "Invalid hex digit from remote system. (0x%x)", ch);
  486.       error("Invalid hex digit from remote system. (0x%x)", ch);
  487.     }
  488.   }
  489. }
  490.  
  491. /* get_hex_byte -- Get a byte from monitor and put it in *BYT. 
  492.  *    Accept any number leading spaces.
  493.  */
  494. static void
  495. get_hex_byte (byt)
  496.      char *byt;
  497. {
  498.   int val;
  499.  
  500.   val = get_hex_digit (1) << 4;
  501.   debuglogs (4, "get_hex_byte() -- Read first nibble 0x%x", val);
  502.  
  503.   val |= get_hex_digit (0);
  504.   debuglogs (4, "get_hex_byte() -- Read second nibble 0x%x", val);
  505.   *byt = val;
  506.   
  507.   debuglogs (4, "get_hex_byte() -- Read a 0x%x", val);
  508. }
  509.  
  510. /* 
  511.  * get_hex_word --  Get N 32-bit words from remote, each preceded by a space,
  512.  *    and put them in registers starting at REGNO.
  513.  */
  514. static int
  515. get_hex_word ()
  516. {
  517.   long val, newval;
  518.   int i;
  519.  
  520.   val = 0;
  521.  
  522. #if 0
  523.   if (HOST_BYTE_ORDER == BIG_ENDIAN) {
  524. #endif
  525.     for (i = 0; i < 8; i++)
  526.       val = (val << 4) + get_hex_digit (i == 0);
  527. #if 0
  528.   } else {
  529.     for (i = 7; i >= 0; i--)
  530.       val = (val << 4) + get_hex_digit (i == 0);
  531.   }
  532. #endif
  533.  
  534.   debuglogs (4, "get_hex_word() got a 0x%x for a %s host.", val, (HOST_BYTE_ORDER == BIG_ENDIAN) ? "big endian" : "little endian");
  535.  
  536.   return val;
  537. }
  538.  
  539. /* This is called not only when we first attach, but also when the
  540.    user types "run" after having attached.  */
  541. void
  542. monitor_create_inferior (execfile, args, env)
  543.      char *execfile;
  544.      char *args;
  545.      char **env;
  546. {
  547.   int entry_pt;
  548.  
  549.   if (args && *args)
  550.     error("Can't pass arguments to remote MONITOR process");
  551.  
  552.   if (execfile == 0 || exec_bfd == 0)
  553.     error("No exec file specified");
  554.  
  555.   entry_pt = (int) bfd_get_start_address (exec_bfd);
  556.  
  557.   debuglogs (3, "create_inferior(exexfile=%s, args=%s, env=%s)", execfile, args, env);
  558.  
  559. /* The "process" (board) is already stopped awaiting our commands, and
  560.    the program is already downloaded.  We just set its PC and go.  */
  561.  
  562.   clear_proceed_status ();
  563.  
  564.   /* Tell wait_for_inferior that we've started a new process.  */
  565.   init_wait_for_inferior ();
  566.  
  567.   /* Set up the "saved terminal modes" of the inferior
  568.      based on what modes we are starting it with.  */
  569.   target_terminal_init ();
  570.  
  571.   /* Install inferior's terminal modes.  */
  572.   target_terminal_inferior ();
  573.  
  574.   /* insert_step_breakpoint ();  FIXME, do we need this?  */
  575.  
  576.   /* Let 'er rip... */
  577.   proceed ((CORE_ADDR)entry_pt, TARGET_SIGNAL_DEFAULT, 0);
  578. }
  579.  
  580. /*
  581.  * monitor_open -- open a connection to a remote debugger.
  582.  *    NAME is the filename used for communication.
  583.  */
  584. static int baudrate = 9600;
  585. static char dev_name[100];
  586.  
  587. void
  588. monitor_open(args, name, from_tty)
  589.      char *args;
  590.      char *name;
  591.      int from_tty;
  592. {
  593.   TERMINAL *temptempio;
  594.  
  595.   if (args == NULL)
  596.     error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\
  597. `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name);
  598.  
  599. /*  if (is_open) */
  600.     monitor_close(0);
  601.  
  602.   strcpy(dev_name, args);
  603.   monitor_desc = SERIAL_OPEN(dev_name);
  604.  
  605.   if (monitor_desc == NULL)
  606.     perror_with_name(dev_name);
  607.  
  608.   if (baud_rate != -1) {
  609.     if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate)) {
  610.       SERIAL_CLOSE (monitor_desc);
  611.       perror_with_name (name);
  612.     }
  613.   }
  614.   
  615.   SERIAL_RAW(monitor_desc);
  616.  
  617. #ifndef __GO32__
  618.   /* some systems only work with 2 stop bits */
  619.   if (STOPBITS == 2) {
  620.     temptempio = (TERMINAL *)SERIAL_GET_TTY_STATE(monitor_desc);
  621. #ifdef HAVE_SGTTY
  622.     temptempio->sg_cflag |= baud_rate | CSTOPB;
  623. #else
  624.     temptempio->c_cflag |= baud_rate | CSTOPB;
  625. /***    temptempio->c_lflag |= ~0x00000008; turn off echo ***/
  626. #endif
  627.     SERIAL_SET_TTY_STATE(monitor_desc, temptempio);
  628.     debuglogs (4, "Set serial port to 2 stop bits");
  629.   }
  630. #endif    /* __GO32__ */
  631.  
  632. #if defined (LOG_FILE)
  633.   log_file = fopen (LOG_FILE, "w");
  634.   if (log_file == NULL)
  635.     perror_with_name (LOG_FILE);
  636.   fprintf_filtered (log_file, "GDB %s (%s", version, host_name);
  637.   fprintf_filtered (log_file, " --target %s)\n", target_name);
  638.   fprintf_filtered (log_file, "Remote target %s connected to %s\n\n", TARGET_NAME, dev_name);
  639. #endif
  640.  
  641.   /* see if the target is alive. For a ROM monitor, we can just try to force the
  642.      prompt to print a few times. FOr the GDB remote protocol, the appilcation
  643.      being debugged is sitting at a breakpoint and waiting for GDB to initialize
  644.      the connection. We force it to give us an empty packet to see if it's alive.
  645.      */
  646.   if (GDBPROTO) {
  647.     debuglogs (3, "Trying to ACK the target's debug stub");
  648.     printf_monitor (INIT_CMD);    /* ask for the last signal */
  649.     expect ("$S05#b8",0);        /* look for a response */
  650.     printf_monitor ("+\n");    /* ask for the last signal */
  651.     expect_prompt(1);        /* See if we get a prompt */
  652.   } else {
  653.     /* wake up the monitor and see if it's alive */
  654.     printf_monitor(INIT_CMD);
  655.     expect_prompt(1);        /* See if we get a prompt */
  656.  
  657.     /* try again to be sure */
  658.     printf_monitor(INIT_CMD);
  659.     expect_prompt(1);        /* See if we get a prompt */
  660.   }
  661.  
  662.   if (from_tty)
  663.     printf("Remote target %s connected to %s\n", TARGET_NAME, dev_name);
  664. }
  665.  
  666. /*
  667.  * monitor_close -- Close out all files and local state before this
  668.  *    target loses control.
  669.  */
  670.  
  671. void
  672. monitor_close (quitting)
  673.      int quitting;
  674. {
  675.   SERIAL_CLOSE(monitor_desc);
  676.   monitor_desc = NULL;
  677.  
  678.   debuglogs (1, "monitor_close (quitting=%d)", quitting);
  679.  
  680. #if defined (LOG_FILE)
  681.   if (log_file) {
  682.     if (ferror(log_file))
  683.       fprintf(stderr, "Error writing log file.\n");
  684.     if (fclose(log_file) != 0)
  685.       fprintf(stderr, "Error closing log file.\n");
  686.   }
  687. #endif
  688. }
  689.  
  690. /* 
  691.  * monitor_detach -- terminate the open connection to the remote
  692.  *    debugger. Use this when you want to detach and do something
  693.  *    else with your gdb.
  694.  */
  695. void
  696. monitor_detach (from_tty)
  697.      int from_tty;
  698. {
  699.  
  700.   debuglogs (1, "monitor_detach ()");
  701.  
  702.   pop_target();        /* calls monitor_close to do the real work */
  703.   if (from_tty)
  704.     printf ("Ending remote %s debugging\n", target_shortname);
  705. }
  706.  
  707. /*
  708.  * monitor_attach -- attach GDB to the target.
  709.  */
  710. void
  711. monitor_attach (args, from_tty)
  712.      char *args;
  713.      int from_tty;
  714. {
  715.   if (from_tty)
  716.     printf ("Starting remote %s debugging\n", target_shortname);
  717.  
  718.   debuglogs (1, "monitor_attach (args=%s)", args);
  719.   
  720.   printf_monitor (GO_CMD);
  721.   /* swallow the echo.  */
  722.   expect (GO_CMD, 1);
  723. }
  724.   
  725. /*
  726.  * monitor_resume -- Tell the remote machine to resume.
  727.  */
  728. void
  729. monitor_resume (pid, step, sig)
  730.      int pid, step;
  731.      enum target_signal sig;
  732. {
  733.   debuglogs (1, "monitor_resume (step=%d, sig=%d)", step, sig);
  734.  
  735.   if (step) {
  736.     printf_monitor (STEP_CMD);
  737.   } else {
  738.     printf_monitor (CONT_CMD);
  739.   }
  740. }
  741.  
  742. /*
  743.  * monitor_wait -- Wait until the remote machine stops, then return,
  744.  *          storing status in status just as `wait' would.
  745.  */
  746. int
  747. monitor_wait (pid, status)
  748.      int pid;
  749.      struct target_waitstatus *status;
  750. {
  751.   int old_timeout = timeout;
  752.  
  753.   debuglogs(1, "monitor_wait (), printing extraneous text.");
  754.   
  755.   status->kind = TARGET_WAITKIND_EXITED;
  756.   status->value.integer = 0;
  757.  
  758.   timeout = 0;        /* Don't time out -- user program is running. */
  759.  
  760.   expect_prompt(0);    /* Wait for prompt, outputting extraneous text */
  761.   debuglogs (4, "monitor_wait(), got the prompt.");
  762.  
  763.   status->kind = TARGET_WAITKIND_STOPPED;
  764.   status->value.sig = TARGET_SIGNAL_TRAP;
  765.  
  766.  
  767.  
  768.   timeout = old_timeout;
  769.  
  770.   return 0;
  771. }
  772.  
  773. /* Return the name of register number regno in the form input and output by
  774.    monitor.  Currently, register_names just happens to contain exactly what
  775.    monitor wants.  Lets take advantage of that just as long as possible! */
  776.  
  777. static char *
  778. get_reg_name (regno)
  779.      int regno;
  780. {
  781.   static char buf[50];
  782.   const char *p;
  783.   char *b;
  784.  
  785.   b = buf;
  786.  
  787.   if (regno < 0)
  788.     return ("");
  789.  
  790.   for (p = REGNAMES(regno); *p; p++)
  791.     *b++ = tolower(*p);
  792.  
  793.   *b = '\000';
  794.  
  795.   debuglogs (5, "Got name \"%s\" from regno #%d.", buf, regno);
  796.  
  797.   return buf;
  798. }
  799.  
  800. /*
  801.  * monitor_fetch_registers -- read the remote registers into the
  802.  *    block regs.
  803.  */
  804. void
  805. monitor_fetch_registers (ignored)
  806.      int ignored;
  807. {
  808.   int regno, i;
  809.   char *p;
  810.   unsigned char packet[PBUFSIZ];
  811.   char regs[REGISTER_BYTES];
  812.  
  813.   debuglogs (1, "monitor_fetch_registers (ignored=%d)\n", ignored);
  814.  
  815.   if (GDBPROTO) {
  816.     /* Unimplemented registers read as all bits zero.  */
  817.     memset (regs, 0, REGISTER_BYTES);
  818.     make_gdb_packet (packet, "g");
  819.     if (monitor_send_packet (packet) == 0)
  820.       error ("Couldn't transmit packet\n");
  821.     if (monitor_get_packet (packet) == 0)
  822.           error ("Couldn't receive packet\n");  
  823.     /* FIXME: read bytes from packet */
  824.     debuglogs (4, "monitor_fetch_registers: Got a \"%s\" back\n", packet);
  825.     for (regno = 0; regno <= PC_REGNUM; regno++) {
  826.       /* supply register stores in target byte order, so swap here */
  827.       /* FIXME: convert from ASCII hex to raw bytes */
  828.       i = ascii2hexword (packet + (regno * 8));
  829.       debuglogs (5, "Adding register %d = %x\n", regno, i);
  830.       SWAP_TARGET_AND_HOST (&i, 4);
  831.       supply_register (regno, (char *)&i);
  832.     }
  833.   } else {
  834.     for (regno = 0; regno <= PC_REGNUM; regno++)
  835.       monitor_fetch_register(regno);
  836.   }
  837. }
  838.  
  839. /* 
  840.  * monitor_fetch_register -- fetch register REGNO, or all registers if REGNO
  841.  *    is -1. Returns errno value.
  842.  */
  843. void
  844. monitor_fetch_register (regno)
  845.      int regno;
  846. {
  847.   int newval, val, j;
  848.  
  849.   debuglogs (1, "monitor_fetch_register (reg=%s)", get_reg_name (regno));
  850.  
  851.   if (regno < 0) {
  852.     monitor_fetch_registers ();
  853.   } else {
  854.     char *name = get_reg_name (regno);
  855.     if (STREQ(name, ""))
  856.       return;
  857.     printf_monitor (ROMCMD(GET_REG), name);    /* send the command */
  858.     expect (name, 1);                /* then strip the leading garbage */
  859.     if (*ROMDELIM(GET_REG) != 0) {        /* if there's a delimiter */
  860.       expect (ROMDELIM(GET_REG), 1);
  861.     }
  862.     
  863.     val =  get_hex_word();            /* get the value, ignore junk */
  864.  
  865.  
  866.     /* supply register stores in target byte order, so swap here */
  867.     SWAP_TARGET_AND_HOST (&val, 4);
  868.     supply_register (regno, (char *) &val);
  869.     
  870.     if (*ROMDELIM(GET_REG) != 0) {
  871. /***  expect (ROMRES(GET_REG)); ***/
  872.       printf_monitor (CMD_END);
  873.     }
  874.     expect_prompt (1);
  875.   }
  876.   return;
  877. }
  878.  
  879. /* Store the remote registers from the contents of the block REGS.  */
  880.  
  881. void
  882. monitor_store_registers ()
  883. {
  884.   int regno;
  885.  
  886.   debuglogs (1, "monitor_store_registers()");
  887.  
  888.   for (regno = 0; regno <= PC_REGNUM; regno++)
  889.     monitor_store_register(regno);
  890.  
  891.   registers_changed ();
  892. }
  893.  
  894. /* 
  895.  * monitor_store_register -- store register REGNO, or all if REGNO == 0.
  896.  *    return errno value.
  897.  */
  898. void
  899. monitor_store_register (regno)
  900.      int regno;
  901. {
  902.   char *name;
  903.   int i;
  904.  
  905.   i = read_register(regno);
  906.  
  907.   debuglogs (1, "monitor_store_register (regno=%d)", regno);
  908.  
  909.   if (regno < 0)
  910.     monitor_store_registers ();
  911.   else {
  912.       debuglogs (3, "Setting register %s to 0x%x", get_reg_name (regno), read_register (regno));
  913.     
  914.     name = get_reg_name (regno);
  915.     if (STREQ(name, ""))
  916.       return;
  917.     printf_monitor (ROMCMD(SET_REG), name, read_register(regno));
  918.     expect (name, 1);                /* strip the leading garbage */
  919.     if (*ROMDELIM(SET_REG) != 0) {        /* if there's a delimiter */
  920.       expect (ROMDELIM(SET_REG), 1);
  921.       get_hex_word(1);
  922.       printf_monitor ("%d%s\n", i, CMD_END);
  923.     }
  924.     expect_prompt (1);
  925.   }
  926.   return;
  927.   
  928. #if 0
  929.       printf_monitor (SET_REG, get_reg_name (regno),
  930.               read_register (regno));
  931.       expect_prompt (1);
  932.     }
  933. #endif
  934. }
  935.  
  936. /* Get ready to modify the registers array.  On machines which store
  937.    individual registers, this doesn't need to do anything.  On machines
  938.    which store all the registers in one fell swoop, this makes sure
  939.    that registers contains all the registers from the program being
  940.    debugged.  */
  941.  
  942. void
  943. monitor_prepare_to_store ()
  944. {
  945.   /* Do nothing, since we can store individual regs */
  946. }
  947.  
  948. void
  949. monitor_files_info ()
  950. {
  951.   printf ("\tAttached to %s at %d baud.\n",
  952.       dev_name, baudrate);
  953. }
  954.  
  955. /*
  956.  * monitor_write_inferior_memory -- Copy LEN bytes of data from debugger
  957.  *    memory at MYADDR to inferior's memory at MEMADDR.  Returns length moved.
  958.  */
  959. int
  960. monitor_write_inferior_memory (memaddr, myaddr, len)
  961.      CORE_ADDR memaddr;
  962.      unsigned char *myaddr;
  963.      int len;
  964. {
  965.   int i;
  966.   char buf[10];
  967.  
  968.   debuglogs (1, "monitor_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
  969.  
  970.   for (i = 0; i < len; i++) {
  971.     printf_monitor (ROMCMD(SET_MEM), memaddr + i, myaddr[i] );
  972.     if (*ROMDELIM(SET_MEM) != 0) {        /* if there's a delimiter */
  973.       expect (ROMDELIM(SET_MEM), 1);
  974.       expect (CMD_DELIM);
  975.       printf_monitor ("%x", myaddr[i]);
  976.     }
  977. /***    printf_monitor ("%x", myaddr[i]); ***/
  978.     if (sr_get_debug() > 1)
  979.       printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]);
  980.     if (*ROMDELIM(SET_MEM) != 0) {
  981.       expect (CMD_DELIM);
  982.       printf_monitor (CMD_END);
  983.     }
  984.     expect_prompt (1);
  985.   }
  986.   return len;
  987. }
  988.  
  989. /*
  990.  * monitor_read_inferior_memory -- read LEN bytes from inferior memory
  991.  *    at MEMADDR.  Put the result at debugger address MYADDR.  Returns
  992.  *    length moved.
  993.  */
  994. int
  995. monitor_read_inferior_memory(memaddr, myaddr, len)
  996.      CORE_ADDR memaddr;
  997.      char *myaddr;
  998.      int len;
  999. {
  1000.   int i, j;
  1001.   char buf[20];
  1002.   char packet[PBUFSIZ];
  1003.  
  1004.   /* Number of bytes read so far.  */
  1005.   int count;
  1006.  
  1007.   /* Starting address of this pass.  */
  1008.   unsigned long startaddr;
  1009.  
  1010.   /* Starting address of this pass.  */
  1011.   unsigned long endaddr;
  1012.  
  1013.   /* Number of bytes to read in this pass.  */
  1014.   int len_this_pass;
  1015.  
  1016.   debuglogs (1, "monitor_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len);
  1017.  
  1018.   /* Note that this code works correctly if startaddr is just less
  1019.      than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
  1020.      thing).  That is, something like
  1021.      monitor_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
  1022.      works--it never adds len To memaddr and gets 0.  */
  1023.   /* However, something like
  1024.      monitor_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
  1025.      doesn't need to work.  Detect it and give up if there's an attempt
  1026.      to do that.  */
  1027.   if (((memaddr - 1) + len) < memaddr) {
  1028.     errno = EIO;
  1029.     return 0;
  1030.   }
  1031.   
  1032.   startaddr = memaddr;
  1033.   count = 0;
  1034.   while (count < len) {
  1035.     len_this_pass = 16;
  1036.     if ((startaddr % 16) != 0)
  1037.       len_this_pass -= startaddr % 16;
  1038.     if (len_this_pass > (len - count))
  1039.       len_this_pass = (len - count);
  1040.     
  1041.     debuglogs (3, "Display %d bytes at %x for Big Endian host", len_this_pass, startaddr);
  1042.     
  1043.     if (GDBPROTO) {
  1044.       for (i = 0; i < len_this_pass; i++) {
  1045.     sprintf (buf, "m%08x,%04x", startaddr, len_this_pass);
  1046.     make_gdb_packet (packet, buf);
  1047.     if (monitor_send_packet (packet) == 0)
  1048.       error ("Couldn't transmit packet\n");
  1049.     if (monitor_get_packet (packet) == 0)
  1050.       error ("Couldn't receive packet\n");  
  1051.     debuglogs (4, "monitor_read_inferior: Got a \"%s\" back\n", packet);
  1052.     for (j = 0; j < len_this_pass ; j++) {        /* extract the byte values */
  1053.       myaddr[count++] = from_hex (*(packet+(j*2))) * 16 + from_hex (*(packet+(j*2)+1));
  1054.       debuglogs (5, "myaddr set to %x\n", myaddr[count-1]);
  1055.     }
  1056.     startaddr += 1;
  1057.       }
  1058.     } else {
  1059.       for (i = 0; i < len_this_pass; i++) {
  1060.     printf_monitor (ROMCMD(GET_MEM), startaddr, startaddr);
  1061.     sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
  1062.     if (*ROMDELIM(GET_MEM) != 0) {        /* if there's a delimiter */
  1063.       expect (ROMDELIM(GET_MEM), 1);
  1064.     } else {
  1065.       sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr);
  1066.       expect (buf,1);                /* get the command echo */
  1067.       get_hex_word(1);            /* strip away the address */
  1068.     }
  1069.     get_hex_byte (&myaddr[count++]);        /* get the value at this address */
  1070.     
  1071.     if (*ROMDELIM(GET_MEM) != 0) {
  1072.       printf_monitor (CMD_END);
  1073.     }
  1074.     expect_prompt (1);
  1075.     startaddr += 1;
  1076.       }
  1077.     }
  1078.   } 
  1079.   return len;
  1080. }
  1081.  
  1082. /* FIXME-someday!  merge these two.  */
  1083. int
  1084. monitor_xfer_inferior_memory (memaddr, myaddr, len, write, target)
  1085.      CORE_ADDR memaddr;
  1086.      char *myaddr;
  1087.      int len;
  1088.      int write;
  1089.      struct target_ops *target;        /* ignored */
  1090. {
  1091.   if (write)
  1092.     return monitor_write_inferior_memory (memaddr, myaddr, len);
  1093.   else
  1094.     return monitor_read_inferior_memory (memaddr, myaddr, len);
  1095. }
  1096.  
  1097. void
  1098. monitor_kill (args, from_tty)
  1099.      char *args;
  1100.      int from_tty;
  1101. {
  1102.   return;        /* ignore attempts to kill target system */
  1103. }
  1104.  
  1105. /* Clean up when a program exits.
  1106.    The program actually lives on in the remote processor's RAM, and may be
  1107.    run again without a download.  Don't leave it full of breakpoint
  1108.    instructions.  */
  1109.  
  1110. void
  1111. monitor_mourn_inferior ()
  1112. {
  1113.   remove_breakpoints ();
  1114.   generic_mourn_inferior ();    /* Do all the proper things now */
  1115. }
  1116.  
  1117. #define MAX_MONITOR_BREAKPOINTS 16
  1118.  
  1119. extern int memory_breakpoint_size;
  1120. static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0};
  1121.  
  1122. /*
  1123.  * monitor_insert_breakpoint -- add a breakpoint
  1124.  */
  1125. int
  1126. monitor_insert_breakpoint (addr, shadow)
  1127.      CORE_ADDR addr;
  1128.      char *shadow;
  1129. {
  1130.   int i;
  1131.  
  1132.   debuglogs (1, "monitor_insert_breakpoint() addr = 0x%x", addr);
  1133.  
  1134.   for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++) {
  1135.     if (breakaddr[i] == 0) {
  1136.       breakaddr[i] = addr;
  1137.       if (sr_get_debug() > 4)
  1138.     printf ("Breakpoint at %x\n", addr);
  1139.       monitor_read_inferior_memory(addr, shadow, memory_breakpoint_size);
  1140.       printf_monitor(SET_BREAK_CMD, addr);
  1141.       expect_prompt(1);
  1142.       return 0;
  1143.     }
  1144.   }
  1145.  
  1146.   fprintf(stderr, "Too many breakpoints (> 16) for monitor\n");
  1147.   return 1;
  1148. }
  1149.  
  1150. /*
  1151.  * _remove_breakpoint -- Tell the monitor to remove a breakpoint
  1152.  */
  1153. int
  1154. monitor_remove_breakpoint (addr, shadow)
  1155.      CORE_ADDR addr;
  1156.      char *shadow;
  1157. {
  1158.   int i;
  1159.  
  1160.   debuglogs (1, "monitor_remove_breakpoint() addr = 0x%x", addr);
  1161.  
  1162.   for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++) {
  1163.     if (breakaddr[i] == addr) {
  1164.       breakaddr[i] = 0;
  1165.       /* some monitors remove breakpoints based on the address */
  1166.       if (CLR_BREAK_ADDR)   
  1167.     printf_monitor(CLR_BREAK_CMD, addr);
  1168.       else
  1169.     printf_monitor(CLR_BREAK_CMD, i);
  1170.       expect_prompt(1);
  1171.       return 0;
  1172.     }
  1173.   }
  1174.   fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr);
  1175.   return 1;
  1176. }
  1177.  
  1178. /* monitor_load -- load a file. This file determines which of the
  1179.  *    supported formats to use. The current types are:
  1180.  *    FIXME: not all types supported yet.
  1181.  *    default - reads any file using bfd and writes it to memory. This
  1182.  *        is really slow.
  1183.  *    srec    - reads binary file using bfd and writes it as an
  1184.  *        ascii srecord.
  1185.  *    xmodem-bin - reads a binary file using bfd, and  downloads it
  1186.  *         using xmodem protocol.
  1187.  *    xmodem-srec - reads a binary file using bfd, and after converting
  1188.  *         it downloads it as an srecord using xmodem protocol.
  1189.  *    ascii-srec - reads a ascii srecord file and downloads it
  1190.  *        without a change.
  1191.  *    ascii-xmodem - reads a ascii file and downloads using xmodem
  1192.  *        protocol.
  1193.  */
  1194. void
  1195. monitor_load (file, fromtty)
  1196.     char *file;
  1197.     int  fromtty;
  1198. {
  1199.   FILE *download;
  1200.   int i, bytes_read;
  1201.  
  1202.   debuglogs (1, "Loading %s to monitor", file);
  1203.  
  1204.   if (STREQ (loadtype_str, "default")) {    /* default, load a binary */
  1205.     gr_load_image (file, fromtty);        /* by writing it into memory */
  1206.     return;
  1207.   }
  1208.  
  1209.   /* load an srecord by converting */
  1210.   if ((STREQ (loadtype_str, "srec")) && STREQ (loadproto_str, "xmodem")) {
  1211.     monitor_load_srec(file, XMODEM);
  1212.     return;
  1213.   }
  1214.  
  1215.   if (STREQ (loadtype_str, "srec")) {        /* load an srecord by converting */
  1216.     monitor_load_srec(file, 0);            /* if from a binary */
  1217.     return;
  1218.   }
  1219.  
  1220.   if (STREQ (loadtype_str, "none")) {        /* load an srecord by converting */
  1221.     error ("Unimplemented");
  1222.     return;
  1223.   }
  1224.  
  1225.   if (STREQ (loadproto_str, "none")) {    /* load an srecord file */
  1226.     monitor_load_ascii_srec(file, fromtty);        /* if from a binary */
  1227.     return;
  1228.   }
  1229.  
  1230.   if (STREQ (loadproto_str, "xmodem")) {        /* load an srecord using the */
  1231.     monitor_load_srec(file, XMODEM);
  1232.     return;
  1233.   }
  1234. }
  1235.  
  1236. /*
  1237.  * monitor_load_ascii_srec -- download an ASCII srecord file.
  1238.  */
  1239. #define DOWNLOAD_LINE_SIZE 100
  1240. int
  1241. monitor_load_ascii_srec (file, fromtty)
  1242.     char *file;
  1243.     int fromtty;
  1244. {
  1245.   FILE *download;
  1246.   char buf[DOWNLOAD_LINE_SIZE];
  1247.   int i, bytes_read;
  1248.  
  1249.   debuglogs (1, "Loading an ASCII srecord file, %s.", file);
  1250.  
  1251.   download = fopen (file, "r");
  1252.   if (download == NULL) {
  1253.     error ("%s Does not exist", file);
  1254.     return;
  1255.   }
  1256.  
  1257.   printf_monitor (LOAD_CMD);
  1258.   sleep(1);
  1259.   while (!feof (download)) {
  1260.     bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
  1261.     if (hashmark) {
  1262.       putchar ('.');
  1263.       fflush (stdout);
  1264.     }
  1265.     if (SERIAL_WRITE(monitor_desc, buf, bytes_read)) {
  1266.       fprintf(stderr, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno));
  1267.       break;
  1268.     }
  1269.     i = 0;
  1270.     while (i++ <=200) {} ;                 /* Ugly HACK, probably needs flow control */
  1271.     if (bytes_read < DOWNLOAD_LINE_SIZE) {
  1272.       if (!feof (download))
  1273.     error ("Only read %d bytes\n", bytes_read);
  1274.       break;
  1275.     }
  1276.   }
  1277.   
  1278.   if (hashmark) {
  1279.     putchar ('\n');
  1280.   }
  1281.   if (!feof (download))
  1282.     error ("Never got EOF while downloading");
  1283.   expect_prompt(1);
  1284.   fclose (download);
  1285. }
  1286.  
  1287. /* 
  1288.  * monitor_command -- put a command string, in args, out to MONITOR.
  1289.  *    Output from MONITOR is placed on the users terminal until the
  1290.  *    prompt is seen. FIXME: We read the charcters ourseleves here
  1291.  *    cause of a nasty echo.
  1292.  */
  1293. void
  1294. monitor_command (args, fromtty)
  1295.      char    *args;
  1296.      int    fromtty;
  1297. {
  1298.  
  1299.   char *p;
  1300.   char c, cp;
  1301.   p = PROMPT;
  1302.  
  1303.   debuglogs (1, "monitor_command (args=%s)", args);
  1304.  
  1305.   if (monitor_desc == NULL)
  1306.     error("monitor target not open.");
  1307.  
  1308.   if (!args)
  1309.     error("Missing command.");
  1310.     
  1311.   printf_monitor ("%s\n", args);
  1312.  
  1313.   expect_prompt(0);
  1314. }
  1315.  
  1316. /*
  1317.  * monitor_load_srec -- download a binary file by converting it to srecords. This
  1318.  *    will also use xmodem to download the resulting file.
  1319.  *
  1320.  *    A download goes like this when using xmodem:
  1321.  *    Receiver:        Sender
  1322.  *    NAK ---------->
  1323.  *        <-------- (packet)    [SOH|1|1|data|SUM]
  1324.  *    ACK ---------->
  1325.  *        <-------- (packet)    [SOH|2|2|data|SUM]
  1326.  *    ACK ---------->
  1327.  *        <-------- EOT
  1328.  *    ACK ---------->
  1329.  *
  1330.  *    ACK = 0x06
  1331.  *    NAK = 0x15
  1332.  *    EOT = 0x04
  1333.  *
  1334.  */
  1335. static void
  1336. monitor_load_srec (args, protocol)
  1337.      char *args;
  1338.      int protocol;
  1339. {
  1340.   bfd *abfd;
  1341.   asection *s;
  1342.   char buffer[1024];
  1343.   char srec[1024];
  1344.   char packet[XMODEM_PACKETSIZE];
  1345.   int i;
  1346.   int retries;
  1347.   int type = 0;                    /* default to a type 0, header record */
  1348.   int srec_frame = 57;                /* FIXME: this must be 57 There is 12 bytes
  1349.                          of header, and 2 bytes of checksum at the end.
  1350.                          The problem is an xmodem packet holds exactly
  1351.                          128 bytes. */
  1352.  
  1353.   abfd = bfd_openr (args, 0);
  1354.   if (!abfd) {
  1355.     printf_filtered ("Unable to open file %s\n", args);
  1356.     return;
  1357.   }
  1358.  
  1359.   if (bfd_check_format (abfd, bfd_object) == 0) {
  1360.     printf_filtered ("File is not an object file\n");
  1361.     return;
  1362.   }
  1363.   
  1364.   printf_monitor (LOAD_CMD);            /* tell the monitor to load */
  1365.   if (protocol == XMODEM) {            /* get the NAK from the target */
  1366.     if (GETNAK) {
  1367.       debuglogs (3, "Got the NAK to start loading");
  1368.     } else {
  1369.       printf_monitor ("%c", EOT);
  1370.       debuglogs (3, "Never got the NAK to start loading");
  1371.       error ("Never got the NAK to start loading");
  1372.     }
  1373.   }
  1374.   
  1375.   s = abfd->sections;
  1376.   while (s != (asection *) NULL) {
  1377.     if (s->flags & SEC_LOAD) {
  1378.       char *buffer = xmalloc (srec_frame);
  1379.       printf_filtered ("%s\t: 0x%4x .. 0x%4x  ", s->name, s->vma, s->vma + s->_raw_size);
  1380.       fflush (stdout);
  1381.       for (i = 0; i < s->_raw_size; i += srec_frame) {
  1382.     if (srec_frame > s->_raw_size - i)
  1383.       srec_frame = s->_raw_size - i;
  1384.     
  1385.     bfd_get_section_contents (abfd, s, buffer, i, srec_frame);
  1386.     monitor_make_srec (srec, type, s->vma + i, buffer, srec_frame);
  1387.     if (protocol == XMODEM) {        /* send a packet using xmodem */
  1388.       make_xmodem_packet (packet, srec, XMODEM_DATASIZE);
  1389.       write_monitor (packet, XMODEM_PACKETSIZE+1);
  1390.       retries = 0;
  1391.       while (retries++ <= 3) {
  1392.         if (GETNAK) {            /* Resend packet */
  1393.           debuglogs (3, "Got a NAK, resending packet");
  1394.           sleep(1);
  1395.           write_monitor (packet, XMODEM_PACKETSIZE+1); /* send it again */
  1396.           if (GETACK)            /* ACKnowledged, get next data chunk */
  1397.         break;
  1398.         } else {                /* assume we got an ACK */
  1399.           if (hashmark)
  1400.         printf_filtered ("#");
  1401.           debuglogs (3, "Got an ACK, sending next packet");
  1402.           break;
  1403.         }
  1404.       }
  1405.       if (retries >= 4) {            /* too many tries, must be hosed */
  1406.         printf_monitor ("%c", EOT);
  1407.         error ("Never got a ACK after sending an xmodem packet");
  1408.       }
  1409.     } else {                /* no protocols at all */
  1410.       printf_monitor ("%s\n", srec);
  1411.     }
  1412.     if (hashmark)
  1413.       printf_filtered ("#");
  1414.     type = 3;                /* switch to a 4 byte address record */
  1415.     fflush (stdout);
  1416.       }
  1417.       free (buffer);
  1418.     } else {
  1419.       debuglogs (3, "%s doesn't need to be loaded", s->name);
  1420.     }
  1421.     s = s->next;
  1422.   }
  1423.   printf_filtered ("\n");
  1424.   
  1425.   /*
  1426.      write a type 7 terminator record. no data for a type 7,
  1427.      and there is no data, so len is 0. 
  1428.    */
  1429.   if (protocol == XMODEM) {        /* send a packet using xmodem */
  1430.     monitor_make_srec (srec, 7, abfd->start_address, "", 0);
  1431.     make_xmodem_packet (packet, srec, XMODEM_DATASIZE);  
  1432.     write_monitor (packet, XMODEM_PACKETSIZE+1);
  1433.   } else {
  1434.     monitor_make_srec (srec, 7, abfd->start_address, "", 0);
  1435.     printf_monitor ("%s\n", srec);
  1436.   }
  1437.   if (protocol == XMODEM) {
  1438.     printf_monitor ("%c", EOT);
  1439.     if (!GETACK)
  1440.       error ("Never got ACK after sending EOT");
  1441.   }
  1442.  
  1443.   if (hashmark) 
  1444.     putchar ('\n');
  1445.   
  1446.   expect_prompt ();
  1447. }
  1448.  
  1449. /*
  1450.  * getacknak -- get an ACK or a NAK from the target.
  1451.  *        returns 1 (true) or 0 (false) This is
  1452.  *        for xmodem. ANy string starting with "***"
  1453.  *        is an error message from the target.
  1454.  *    Here's a few from the WinBond w89k "Cougar" PA board.
  1455.  *        *** Too many errors found.
  1456.  *        *** Bad command
  1457.  *        *** Command syntax error
  1458.  */
  1459. int
  1460. getacknak (byte)
  1461.      int byte;
  1462. {
  1463.   char character;
  1464.   int i;
  1465.   
  1466.   i = 0;
  1467.   while (i++ < 60) {
  1468.     character = (char)readchar (0);
  1469.     if ((character == 0xfffffffe) || (character == 0x7f)) {        /* empty uart */
  1470.       if (sr_get_debug() > 3)
  1471.     putchar ('.');
  1472.       fflush (stdout);
  1473.       sleep (1);
  1474.       continue;
  1475.     }
  1476.     if (character == CANCEL) {            /* target aborted load */
  1477.       expect_prompt (0);
  1478.       error ("Got a CANCEL from the target.");
  1479.     }
  1480.     if (character == '*') {            /* look for missed error message */
  1481.       expect_prompt (0);
  1482.       error ("Got an error message from the target");
  1483.     }
  1484.     debuglogs (3, "Got a %s (0x%x or \'%c\'), expecting a %s.\n",
  1485.            (character == ACK) ? "ACK" : (character == NAK) ? "NAK" : "BOGUS",
  1486.            character,  character, (byte == ACK) ? "ACK" : "NAK");
  1487.     if (character == byte)            /* got what we wanted */
  1488.       return 1;
  1489.     if (character == ((byte == ACK) ? NAK : ACK)) {    /* got the opposite */
  1490.       debuglogs (3, "Got the opposite, wanted 0x%x, got a 0x%x", byte, character);
  1491.       return 0;
  1492.     }
  1493.     sleep (1); 
  1494.   }
  1495.   return 0;
  1496. }
  1497.  
  1498. /*
  1499.  * monitor_make_srec -- make an srecord. This writes each line, one at a
  1500.  *    time, each with it's own header and trailer line.
  1501.  *    An srecord looks like this:
  1502.  *
  1503.  * byte count-+     address
  1504.  * start ---+ |        |       data        +- checksum
  1505.  *        | |        |                   |
  1506.  *      S01000006F6B692D746573742E73726563E4
  1507.  *      S315000448600000000000000000FC00005900000000E9
  1508.  *      S31A0004000023C1400037DE00F023604000377B009020825000348D
  1509.  *      S30B0004485A0000000000004E
  1510.  *      S70500040000F6
  1511.  *
  1512.  *    S<type><length><address><data><checksum>
  1513.  *
  1514.  *      Where
  1515.  *      - length
  1516.  *        is the number of bytes following upto the checksum. Note that
  1517.  *        this is not the number of chars following, since it takes two
  1518.  *        chars to represent a byte.
  1519.  *      - type
  1520.  *        is one of:
  1521.  *        0) header record
  1522.  *        1) two byte address data record
  1523.  *        2) three byte address data record
  1524.  *        3) four byte address data record
  1525.  *        7) four byte address termination record
  1526.  *        8) three byte address termination record
  1527.  *        9) two byte address termination record
  1528.  *       
  1529.  *      - address
  1530.  *        is the start address of the data following, or in the case of
  1531.  *        a termination record, the start address of the image
  1532.  *      - data
  1533.  *        is the data.
  1534.  *      - checksum
  1535.  *      is the sum of all the raw byte data in the record, from the length
  1536.  *        upwards, modulo 256 and subtracted from 255.
  1537.  */
  1538. int
  1539. monitor_make_srec (buffer, type, memaddr, myaddr, len)
  1540.      char *buffer;
  1541.      int type;
  1542.      CORE_ADDR memaddr;
  1543.      unsigned char *myaddr;
  1544.      int len;
  1545. {
  1546.   int checksum;
  1547.   int i;
  1548.   char *buf;
  1549.  
  1550.   buf = buffer;
  1551.   debuglogs (4, "monitor_make_srec (buffer=0x%x, type=%d, memaddr=0x%x, len=%d",
  1552.                                 buffer, type, memaddr, len); 
  1553.   checksum = 0;
  1554.   
  1555.   /*
  1556.      create the header for the srec. 4 is the number of bytes in the address,
  1557.      and 1 is the number of bytes in the count.
  1558.    */
  1559.   if (type == 0)                /* FIXME: type 0 is optional */
  1560.     type = 3;                    /* so use data as it works */
  1561.   sprintf (buf, "S%d%02X%08X", type, len + 4 + 1, memaddr);
  1562.   buf += 12;
  1563.   
  1564.   checksum += (len + 4 + 1            /* calculate the checksum */
  1565.            + (memaddr & 0xff)
  1566.            + ((memaddr >>  8) & 0xff)
  1567.            + ((memaddr >> 16) & 0xff)
  1568.            + ((memaddr >> 24) & 0xff));
  1569.   
  1570.   for (i = 0; i < len; i++) {        /* build the srecord */
  1571.     sprintf (buf, "%02X", myaddr[i]);
  1572.     checksum += myaddr[i];
  1573.     buf += 2;
  1574.   }
  1575.  
  1576.   sprintf(buf, "%02X", ~checksum & 0xff);    /* add the checksum */
  1577.   debuglogs (3, "srec is \"%s\"", buffer);
  1578.   
  1579.   return(0);
  1580. }
  1581.  
  1582. /*
  1583.  * make_xmodem_packet -- this takes a 128 bytes of data and makes a packet
  1584.  *    out of it.
  1585.  *
  1586.  *    Each packet looks like this:
  1587.  *    +-----+-------+-------+------+-----+
  1588.  *    | SOH | Seq1. | Seq2. | data | SUM |
  1589.  *    +-----+-------+-------+------+-----+
  1590.  *    SOH  = 0x01
  1591.  *    Seq1 = The sequence number.
  1592.  *    Seq2 = The complement of the sequence number.
  1593.  *    Data = A 128 bytes of data.
  1594.  *    SUM  = Add the contents of the 128 bytes and use the low-order
  1595.  *           8 bits of the result.
  1596.  */
  1597. static void
  1598. make_xmodem_packet (packet, data, len)
  1599.      unsigned char packet[];
  1600.      unsigned char *data;
  1601.      int len;
  1602. {
  1603.   static int sequence = 1;
  1604.   int i, sum;
  1605.   unsigned char *buf;
  1606.   
  1607.   buf = data;
  1608.   /* build the packet header */
  1609.   packet[0] = SOH;
  1610.   packet[1] = sequence;
  1611.   packet[2] = 255 - sequence;
  1612.   sequence++;
  1613. #if 0
  1614.   packet[2] = ~sequence++;            /* the complement is the sequence checksum */
  1615. #endif
  1616.   
  1617.   sum = 0;                    /* calculate the data checksum */
  1618.   for (i = 3; i <= len + 2; i++) {
  1619.     packet[i] = *buf;
  1620.     sum += *buf;
  1621.     buf++;
  1622.   }
  1623.  
  1624.   for (i = len+1 ; i <= XMODEM_DATASIZE ; i++) {    /* add padding for the rest of the packet */
  1625.     packet[i] = '0';
  1626.   }
  1627.  
  1628.   packet[XMODEM_PACKETSIZE] = sum & 0xff;    /* add the checksum */
  1629.  
  1630.   if (sr_get_debug() > 4) {
  1631.     debuglogs (4, "The xmodem checksum is %d (0x%x)\n", sum & 0xff, sum & 0xff);
  1632.     print_xmodem_packet (packet);
  1633.     }
  1634. }
  1635.  
  1636. /*
  1637.  * print_xmodem_packet -- print the packet as a debug check
  1638.  */
  1639. static void
  1640. print_xmodem_packet(packet)
  1641.      char packet[];
  1642. {
  1643.   int i;
  1644.   static int lastseq;
  1645.   int sum;
  1646.  
  1647.   /* take apart the packet header the packet header */
  1648.   if (packet[0] == SOH) {
  1649.      ("SOH");
  1650.   } else {
  1651.     error ("xmodem: SOH is wrong");
  1652.   }
  1653.   
  1654.   /* check the sequence */
  1655.   if (packet[1] != 0) {
  1656.     lastseq = packet[1];
  1657.     if (packet[2] != ~lastseq)
  1658.       error ("xmodem: Sequence checksum is wrong");
  1659.     else
  1660.       printf_filtered (" %d %d", lastseq, ~lastseq);
  1661.   }
  1662.   
  1663.   /* check the data checksum */
  1664.   sum = 0;
  1665.   for (i = 3; i <= XMODEM_DATASIZE; i++) {
  1666.     sum += packet[i];
  1667.   }
  1668.  
  1669.   /* ignore the data */
  1670. #if 0
  1671.   printf (" [128 bytes of data] %d\n", sum & 0xff);
  1672. #endif
  1673.   printf_filtered (" [%s] %d\n", packet, sum & 0xff);
  1674.  
  1675.   if ((packet[XMODEM_PACKETSIZE] & 0xff) != (sum & 0xff)) {
  1676.     debuglogs (4, "xmodem: data checksum wrong, got a %d", packet[XMODEM_PACKETSIZE] & 0xff);
  1677.   }
  1678.   putchar ('\n');
  1679. }
  1680.  
  1681. /*
  1682.  * make_gdb_packet -- make a GDB packet. The data is always ASCII.
  1683.  *     A debug packet whose contents are <data>
  1684.  *     is encapsulated for transmission in the form:
  1685.  *
  1686.  *        $ <data> # CSUM1 CSUM2
  1687.  *
  1688.  *       <data> must be ASCII alphanumeric and cannot include characters
  1689.  *       '$' or '#'.  If <data> starts with two characters followed by
  1690.  *       ':', then the existing stubs interpret this as a sequence number.
  1691.  *
  1692.  *       CSUM1 and CSUM2 are ascii hex representation of an 8-bit 
  1693.  *       checksum of <data>, the most significant nibble is sent first.
  1694.  *       the hex digits 0-9,a-f are used.
  1695.  *
  1696.  */
  1697. static void
  1698. make_gdb_packet (buf, data)
  1699.      char *buf, *data;
  1700. {
  1701.   int i;
  1702.   unsigned char csum = 0;
  1703.   int cnt = strlen (data);
  1704.   char *p;
  1705.  
  1706.   debuglogs (3, "make_gdb_packet(%s)\n", data);
  1707.   if (cnt > sizeof(data) - 5)           /* Prosanity check */
  1708.     abort();
  1709.  
  1710.   /* start with the packet header */
  1711.   p = buf;
  1712.   *p++ = '$';
  1713.  
  1714.   /* calculate the checksum */
  1715.   for (i = 0; i < cnt; i++)
  1716.     {
  1717.       csum += data[i];
  1718.       *p++ = data[i];
  1719.     }
  1720.  
  1721.   /* terminate the data with a '#' */
  1722.   *p++ = '#';
  1723.  
  1724.   /* add the checksum as two ascii digits */
  1725.   *p++ = tohex ((csum >> 4) & 0xf);
  1726.   *p++ = tohex (csum & 0xf);
  1727.   *p  = 0x0;            /* Null terminator on string */
  1728. }
  1729.  
  1730. /*
  1731.  * monitor_send_packet -- send a GDB packet to the target with error handling. We get
  1732.  *        a '+' (ACK) back if the packet is received and the checksum matches.
  1733.  *        Otherwise a '-' (NAK) is returned. It returns a 1 for a successful
  1734.  *        transmition, or a 0 for a failure.
  1735.  */
  1736. int
  1737. monitor_send_packet (packet)
  1738.      char *packet;
  1739. {
  1740.   int c, retries;
  1741.   char junk[PBUFSIZ];
  1742.  
  1743.   retries = 0;
  1744.   while (retries <= 10) {
  1745.     debuglogs (3, "monitor_send_packet(): Sending packet: %s...", packet);
  1746.     /* first we fire off a '+' to finish off any hung packets from a previous
  1747.      * command. then we fire a '+' to force the ROM monitor prompt. Then once
  1748.      * we're syncronized, we can send our packet.
  1749.      */
  1750.     printf_monitor ("+\r");
  1751.     expect_prompt (1);
  1752.     printf_monitor ("%s", packet);
  1753.     
  1754.     /* read until either a timeout occurs (-2) or '+' is read */
  1755.     while (retries <= 10) {
  1756.       c = readchar (timeout);
  1757.       debuglogs (3, "Reading a GDB protocol packet... Got a '%c'\n", c);
  1758.       switch (c) {
  1759.       case '+':
  1760.     debuglogs (3, "Got Ack\n");
  1761.     return 1;
  1762.       case SERIAL_TIMEOUT:
  1763.     debuglogs (3, "Timed out reading serial port\n");
  1764.     break;            /* Retransmit buffer */
  1765.       case '-':
  1766.     debuglogs (3, "Got NAK\n");
  1767.     continue;
  1768.       case '$':
  1769.     /* it's probably an old response, or the echo of our command.
  1770.      * just gobble up the packet and ignore it.
  1771.      */
  1772.     debuglogs (3, "Got a junk packet\n");
  1773.     do {
  1774.       c = readchar (timeout);
  1775.       debuglogs (3, "Reading a junk packet, got a '%c'.\n", c);
  1776.     } while (c != '#');
  1777.     c = readchar (timeout);
  1778.     c = readchar (timeout);
  1779.     continue;               /* Now, go look for next packet */
  1780.       default:
  1781.     continue;
  1782.       }
  1783.       retries++;
  1784.       debuglogs (3, "Retransmitting packet \"%s\"\n", packet);
  1785.       break;                /* Here to retransmit */
  1786.     }
  1787.   } /* outer while */
  1788.   return 0;
  1789. }
  1790.  
  1791. /*
  1792.  * monitor_get_packet -- get a GDB packet from the target. Basically we read till we
  1793.  *        see a '#', then check the checksum. It returns a 1 if it's gotten a
  1794.  *        packet, or a 0 it the packet wasn't transmitted correctly.
  1795.  */
  1796. int
  1797. monitor_get_packet (packet)
  1798.      char *packet;
  1799. {
  1800.   int c;
  1801.   int retries;
  1802.   unsigned char csum;
  1803.   unsigned char pktcsum;
  1804.   char *bp;
  1805.  
  1806.   csum = 0;
  1807.   bp = packet;
  1808.  
  1809.   retries = 0;
  1810.   while (retries <= 10) {
  1811.     do {
  1812.       c = readchar (timeout);
  1813.       if (c == SERIAL_TIMEOUT) {
  1814.     debuglogs (3, "monitor_get_packet: got time out from serial port.\n");
  1815.       }
  1816.       debuglogs (3, "Waiting for a '$', got a %c\n", c);
  1817.     } while (c != '$');
  1818.     
  1819.     retries = 0;
  1820.     while (retries <= 10) {
  1821.       c = readchar (timeout);
  1822.       debuglogs (3, "monitor_get_packet: got a '%c'\n", c);
  1823.       switch (c) {
  1824.       case SERIAL_TIMEOUT:
  1825.     debuglogs (3, "Timeout in mid-packet, retrying\n");
  1826.     return 0;
  1827.       case '$':
  1828.     debuglogs (3, "Saw new packet start in middle of old one\n");
  1829.     return 0;             /* Start a new packet, count retries */
  1830.       case '#':    
  1831.     *bp = '\000';
  1832.     
  1833.     pktcsum = from_hex (readchar (timeout)) << 4;
  1834.     pktcsum |= from_hex (readchar (timeout));
  1835.     if (csum == pktcsum) {
  1836.       debuglogs (3, "\npacket data is \"$s\", GDB packet checksum correct.\n", packet);
  1837.       printf_monitor ("+");
  1838.       expect_prompt (1);
  1839.       return 1;
  1840.     }
  1841.     debuglogs (3, "Bad checksum, sentsum=0x%x, csum=0x%x\n", pktcsum, csum);
  1842.     return 0;
  1843.       case '*':               /* Run length encoding */
  1844.     debuglogs (5, "Run length encoding in packet\n");
  1845.     csum += c;
  1846.     c = readchar (timeout);
  1847.     csum += c;
  1848.     c = c - ' ' + 3;      /* Compute repeat count */
  1849.     
  1850.     if (c > 0 && c < 255 && bp + c - 1 < packet + PBUFSIZ - 1) {
  1851.       memset (bp, *(bp - 1), c);
  1852.       bp += c;
  1853.       continue;
  1854.     }
  1855.         *bp = '\0';
  1856.     printf_filtered ("Repeat count %d too large for buffer.\n", c);
  1857.     return 0;
  1858.     
  1859.       default:
  1860.     if (bp < packet + PBUFSIZ - 1) {
  1861.       *bp++ = c;
  1862.       csum += c;
  1863.       continue;
  1864.     }
  1865.     
  1866.     *bp = '\0';
  1867.     puts_filtered ("Remote packet too long.\n");
  1868.     return 0;
  1869.       }
  1870.     }
  1871.   }
  1872. }
  1873.  
  1874. /*
  1875.  * ascii2hexword -- convert an ascii number represented by 8 digits to a hex value.
  1876.  */
  1877. static unsigned long
  1878. ascii2hexword (mem)
  1879.      unsigned char *mem;
  1880. {
  1881.   unsigned long val;
  1882.   int i;
  1883.   char buf[9];
  1884.  
  1885.   val = 0;
  1886.   for (i = 0; i < 8; i++) {
  1887.     val <<= 4;
  1888.     if (mem[i] >= 'A' && mem[i] <= 'F')
  1889.       val = val + mem[i] - 'A' + 10;      
  1890.     if (mem[i] >= 'a' && mem[i] <= 'f')
  1891.       val = val + mem[i] - 'a' + 10;
  1892.     if (mem[i] >= '0' && mem[i] <= '9')
  1893.       val = val + mem[i] - '0';
  1894.     buf[i] = mem[i];
  1895.   }
  1896.   buf[8] = '\0';
  1897.   debuglogs (4, "ascii2hexword() got a 0x%x from %s(%x).\n", val, buf, mem);
  1898.   return val;
  1899. }
  1900.  
  1901. /*
  1902.  * ascii2hexword -- convert an ascii number represented by 8 digits to a hex value.
  1903.  */
  1904. static void
  1905. hexword2ascii (mem, num)
  1906.      unsigned char *mem;
  1907.      int num;
  1908. {
  1909.   int i;
  1910.   long val;
  1911.   unsigned long mask;
  1912.   
  1913.   for (i = 0; i < 8; i++) {
  1914.     mask = 0xf * (0xf^i);
  1915.     val = (val << 4) + num & mask;
  1916.   }
  1917.   *mem = '\0';
  1918. }
  1919.  
  1920. /* Convert hex digit A to a number.  */
  1921. static int
  1922. from_hex (a)
  1923.      int a;
  1924. {  
  1925.   if (a == 0)
  1926.     return 0;
  1927.  
  1928.   debuglogs (4, "from_hex got a 0x%x(%c)\n",a,a);
  1929.   if (a >= '0' && a <= '9')
  1930.     return a - '0';
  1931.   if (a >= 'a' && a <= 'f')
  1932.     return a - 'a' + 10;
  1933.   if (a >= 'A' && a <= 'F')
  1934.     return a - 'A' + 10;
  1935.   else {
  1936.     error ("Reply contains invalid hex digit 0x%x", a);
  1937.   }
  1938. }
  1939.  
  1940. /* Convert number NIB to a hex digit.  */
  1941. static int
  1942. tohex (nib)
  1943.      int nib;
  1944. {
  1945.   if (nib < 10)
  1946.     return '0'+nib;
  1947.   else
  1948.     return 'a'+nib-10;
  1949. }
  1950.  
  1951. /*
  1952.  * _initialize_remote_monitors -- setup a few addtitional commands that
  1953.  *        are usually only used by monitors.
  1954.  */
  1955. void
  1956. _initialize_remote_monitors ()
  1957. {
  1958.   struct cmd_list_element *c;
  1959.  
  1960.   /* this sets the type of download protocol */
  1961.   c = add_set_cmd ("remoteloadprotocol", no_class, var_string, (char *)&loadproto_str,
  1962.        "Set the type of the remote load protocol.\n", &setlist);
  1963.   c->function.sfunc =  set_loadproto_command;
  1964.   add_show_from_set (c, &showlist);
  1965.   loadproto_str = savestring ("none", 5);
  1966.  
  1967.   /* this sets the conversion type when loading */
  1968.   c = add_set_cmd ("remoteloadtype", no_class, var_string, (char *)&loadtype_str,
  1969.        "Set the type of the remote load protocol.\n", &setlist);
  1970.   c->function.sfunc =  set_loadtype_command;
  1971.   add_show_from_set (c, &showlist);
  1972.   loadtype_str = savestring ("srec", 5);
  1973.  
  1974.   add_show_from_set (add_set_cmd ("hash", no_class, var_boolean,
  1975.                                   (char *)&hashmark,
  1976.                   "Set display of activity while downloading a file.\n\
  1977. When enabled, a period \'.\' is displayed.",
  1978.                                   &setlist),
  1979.              &showlist);
  1980.  
  1981.   /* generic monitor command */
  1982.   add_com ("monitor", class_obscure, monitor_command,
  1983.        "Send a command to the debug monitor."); 
  1984. }
  1985.