home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / remote.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  24.6 KB  |  1,085 lines

  1. /* Remote target communications for serial-line targets in custom GDB protocol
  2.    Copyright 1988, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Remote communication protocol.
  21.    All values are encoded in ascii hex digits.
  22.  
  23.     Request        Packet
  24.  
  25.     read registers  g
  26.     reply        XX....X        Each byte of register data
  27.                     is described by two hex digits.
  28.                     Registers are in the internal order
  29.                     for GDB, and the bytes in a register
  30.                     are in the same order the machine uses.
  31.             or ENN        for an error.
  32.  
  33.     write regs    GXX..XX        Each byte of register data
  34.                     is described by two hex digits.
  35.     reply        OK        for success
  36.             ENN        for an error
  37.  
  38.     read mem    mAA..AA,LLLL    AA..AA is address, LLLL is length.
  39.     reply        XX..XX        XX..XX is mem contents
  40.             or ENN        NN is errno
  41.  
  42.     write mem    MAA..AA,LLLL:XX..XX
  43.                     AA..AA is address,
  44.                     LLLL is number of bytes,
  45.                     XX..XX is data
  46.     reply        OK        for success
  47.             ENN        for an error
  48.  
  49.     cont        cAA..AA        AA..AA is address to resume
  50.                     If AA..AA is omitted,
  51.                     resume at same address.
  52.  
  53.     step        sAA..AA        AA..AA is address to resume
  54.                     If AA..AA is omitted,
  55.                     resume at same address.
  56.  
  57.     last signal     ?               Reply the current reason for stopping.
  58.                                         This is the same reply as is generated
  59.                     for step or cont : SAA where AA is the
  60.                     signal number.
  61.  
  62.     There is no immediate reply to step or cont.
  63.     The reply comes when the machine stops.
  64.     It is        SAA        AA is the "signal number"
  65.  
  66.     or...        TAAPPPPPPPPFFFFFFFF
  67.                     where AA is the signal number,
  68.                     PPPPPPPP is the PC (PC_REGNUM), and
  69.                     FFFFFFFF is the frame ptr (FP_REGNUM).
  70.  
  71.     kill req    k
  72. */
  73.  
  74. #include "defs.h"
  75. #include <string.h>
  76. #include <fcntl.h>
  77. #include "frame.h"
  78. #include "inferior.h"
  79. #include "target.h"
  80. #include "wait.h"
  81. #include "terminal.h"
  82. #include "gdbcmd.h"
  83.  
  84. #if !defined(DONT_USE_REMOTE)
  85. #ifdef USG
  86. #include <sys/types.h>
  87. #endif
  88.  
  89. #include <signal.h>
  90. #include "serial.h"
  91.  
  92. /* Prototypes for local functions */
  93.  
  94. static void
  95. remote_write_bytes PARAMS ((CORE_ADDR, char *, int));
  96.  
  97. static void
  98. remote_read_bytes PARAMS ((CORE_ADDR, char *, int));
  99.  
  100. static void
  101. remote_files_info PARAMS ((struct target_ops *));
  102.  
  103. static int
  104. remote_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
  105.  
  106. static void 
  107. remote_prepare_to_store PARAMS ((void));
  108.  
  109. static void
  110. remote_fetch_registers PARAMS ((int));
  111.  
  112. static void
  113. remote_resume PARAMS ((int, int));
  114.  
  115. static int
  116. remote_start_remote PARAMS ((char *));
  117.  
  118. static void
  119. remote_open PARAMS ((char *, int));
  120.  
  121. static void
  122. remote_close PARAMS ((int));
  123.  
  124. static void
  125. remote_store_registers PARAMS ((int));
  126.  
  127. static void
  128. getpkt PARAMS ((char *, int));
  129.  
  130. static void
  131. putpkt PARAMS ((char *));
  132.  
  133. static void
  134. remote_send PARAMS ((char *));
  135.  
  136. static int
  137. readchar PARAMS ((void));
  138.  
  139. static int
  140. remote_wait PARAMS ((WAITTYPE *));
  141.  
  142. static int
  143. tohex PARAMS ((int));
  144.  
  145. static int
  146. fromhex PARAMS ((int));
  147.  
  148. static void
  149. remote_detach PARAMS ((char *, int));
  150.  
  151. extern struct target_ops remote_ops;    /* Forward decl */
  152.  
  153. static int kiodebug = 0;
  154. /* This was 5 seconds, which is a long time to sit and wait.
  155.    Unless this is going though some terminal server or multiplexer or
  156.    other form of hairy serial connection, I would think 2 seconds would
  157.    be plenty.  */
  158. static int timeout = 2;
  159.  
  160. #if 0
  161. int icache;
  162. #endif
  163.  
  164. /* Descriptor for I/O to remote machine.  Initialize it to -1 so that
  165.    remote_open knows that we don't have a file open when the program
  166.    starts.  */
  167. serial_t remote_desc = NULL;
  168.  
  169. #define    PBUFSIZ    1024
  170.  
  171. /* Maximum number of bytes to read/write at once.  The value here
  172.    is chosen to fill up a packet (the headers account for the 32).  */
  173. #define MAXBUFBYTES ((PBUFSIZ-32)/2)
  174.  
  175. /* Round up PBUFSIZ to hold all the registers, at least.  */
  176. #if REGISTER_BYTES > MAXBUFBYTES
  177. #undef    PBUFSIZ
  178. #define    PBUFSIZ    (REGISTER_BYTES * 2 + 32)
  179. #endif
  180.  
  181. /* Clean up connection to a remote debugger.  */
  182.  
  183. /* ARGSUSED */
  184. static void
  185. remote_close (quitting)
  186.      int quitting;
  187. {
  188.   if (remote_desc)
  189.     SERIAL_CLOSE (remote_desc);
  190.   remote_desc = NULL;
  191. }
  192.  
  193. /* Stub for catch_errors.  */
  194.  
  195. static int
  196. remote_start_remote (dummy)
  197.      char *dummy;
  198. {
  199.   /* Ack any packet which the remote side has already sent.  */
  200.   SERIAL_WRITE (remote_desc, "+\r", 2);
  201.   putpkt ("?");            /* initiate a query from remote machine */
  202.  
  203.   start_remote ();        /* Initialize gdb process mechanisms */
  204.   return 1;
  205. }
  206.  
  207. /* Open a connection to a remote debugger.
  208.    NAME is the filename used for communication.  */
  209.  
  210. static void
  211. remote_open (name, from_tty)
  212.      char *name;
  213.      int from_tty;
  214. {
  215.   if (name == 0)
  216.     error (
  217. "To open a remote debug connection, you need to specify what serial\n\
  218. device is attached to the remote system (e.g. /dev/ttya).");
  219.  
  220.   target_preopen (from_tty);
  221.  
  222.   unpush_target (&remote_ops);
  223.  
  224. #if 0
  225.   dcache_init ();
  226. #endif
  227.  
  228.   remote_desc = SERIAL_OPEN (name);
  229.   if (!remote_desc)
  230.     perror_with_name (name);
  231.  
  232.   if (baud_rate)
  233.     {
  234.       int rate;
  235.  
  236.       if (sscanf (baud_rate, "%d", &rate) == 1)
  237.     if (SERIAL_SETBAUDRATE (remote_desc, rate))
  238.       {
  239.         SERIAL_CLOSE (remote_desc);
  240.         perror_with_name (name);
  241.       }
  242.     }
  243.  
  244.   SERIAL_RAW (remote_desc);
  245.  
  246.   if (from_tty)
  247.     {
  248.       puts_filtered ("Remote debugging using ");
  249.       puts_filtered (name);
  250.       puts_filtered ("\n");
  251.     }
  252.   push_target (&remote_ops);    /* Switch to using remote target now */
  253.  
  254.   /* Start the remote connection; if error (0), discard this target. */
  255.   immediate_quit++;        /* Allow user to interrupt it */
  256.   if (!catch_errors (remote_start_remote, (char *)0, 
  257.     "Couldn't establish connection to remote target\n"))
  258.     pop_target();
  259. }
  260.  
  261. /* remote_detach()
  262.    takes a program previously attached to and detaches it.
  263.    We better not have left any breakpoints
  264.    in the program or it'll die when it hits one.
  265.    Close the open connection to the remote debugger.
  266.    Use this when you want to detach and do something else
  267.    with your gdb.  */
  268.  
  269. static void
  270. remote_detach (args, from_tty)
  271.      char *args;
  272.      int from_tty;
  273. {
  274.   if (args)
  275.     error ("Argument given to \"detach\" when remotely debugging.");
  276.   
  277.   pop_target ();
  278.   if (from_tty)
  279.     puts_filtered ("Ending remote debugging.\n");
  280. }
  281.  
  282. /* Convert hex digit A to a number.  */
  283.  
  284. static int
  285. fromhex (a)
  286.      int a;
  287. {
  288.   if (a >= '0' && a <= '9')
  289.     return a - '0';
  290.   else if (a >= 'a' && a <= 'f')
  291.     return a - 'a' + 10;
  292.   else
  293.     error ("Reply contains invalid hex digit");
  294.   return -1;
  295. }
  296.  
  297. /* Convert number NIB to a hex digit.  */
  298.  
  299. static int
  300. tohex (nib)
  301.      int nib;
  302. {
  303.   if (nib < 10)
  304.     return '0'+nib;
  305.   else
  306.     return 'a'+nib-10;
  307. }
  308.  
  309. /* Tell the remote machine to resume.  */
  310.  
  311. static void
  312. remote_resume (step, siggnal)
  313.      int step, siggnal;
  314. {
  315.   char buf[PBUFSIZ];
  316.  
  317.   if (siggnal)
  318.     {
  319.       char *name;
  320.       target_terminal_ours_for_output ();
  321.       printf_filtered ("Can't send signals to a remote system.  ");
  322.       name = strsigno (siggnal);
  323.       if (name)
  324.     printf_filtered (name);
  325.       else
  326.     printf_filtered ("Signal %d", siggnal);
  327.       printf_filtered (" not sent.\n");
  328.       target_terminal_inferior ();
  329.     }
  330.  
  331. #if 0
  332.   dcache_flush ();
  333. #endif
  334.  
  335.   strcpy (buf, step ? "s": "c");
  336.  
  337.   putpkt (buf);
  338. }
  339.  
  340. static void remote_interrupt_twice PARAMS ((int));
  341. static void (*ofunc)();
  342.  
  343. /* Send ^C to target to halt it.  Target will respond, and send us a
  344.    packet.  */
  345.  
  346. void remote_interrupt(signo)
  347.      int signo;
  348. {
  349.   /* If this doesn't work, try more severe steps.  */
  350.   signal (signo, remote_interrupt_twice);
  351.   
  352.   if (kiodebug)
  353.     printf ("remote_interrupt called\n");
  354.  
  355.   SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
  356. }
  357.  
  358. /* The user typed ^C twice.  */
  359. static void
  360. remote_interrupt_twice (signo)
  361.      int signo;
  362. {
  363.   signal (signo, ofunc);
  364.   
  365.   target_terminal_ours ();
  366.   if (query ("Interrupted while waiting for the program.\n\
  367. Give up (and stop debugging it)? "))
  368.     {
  369.       target_mourn_inferior ();
  370.       return_to_top_level ();
  371.     }
  372.   else
  373.     {
  374.       signal (signo, remote_interrupt);
  375.       target_terminal_inferior ();
  376.     }
  377. }
  378.  
  379. /* Wait until the remote machine stops, then return,
  380.    storing status in STATUS just as `wait' would.
  381.    Returns "pid" (though it's not clear what, if anything, that
  382.    means in the case of this target).  */
  383.  
  384. static int
  385. remote_wait (status)
  386.      WAITTYPE *status;
  387. {
  388.   unsigned char buf[PBUFSIZ];
  389.   unsigned char *p;
  390.   int i;
  391.   long regno;
  392.   char regs[MAX_REGISTER_RAW_SIZE];
  393.  
  394.   WSETEXIT ((*status), 0);
  395.  
  396.   ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
  397.   getpkt ((char *) buf, 1);
  398.   signal (SIGINT, ofunc);
  399.  
  400.   if (buf[0] == 'E')
  401.     error ("Remote failure reply: %s", buf);
  402.   if (buf[0] == 'T')
  403.     {
  404.       /* Expedited reply, containing Signal, {regno, reg} repeat */
  405.       /*  format is:  'Tssn...:r...;n...:r...;n...:r...;#cc', where
  406.       ss = signal number
  407.       n... = register number
  408.       r... = register contents
  409.       */
  410.  
  411.       p = &buf[3];        /* after Txx */
  412.  
  413.       while (*p)
  414.     {
  415.       regno = strtol (p, &p, 16); /* Read the register number */
  416.  
  417.       if (*p++ != ':'
  418.           || regno >= NUM_REGS)
  419.         error ("Remote sent bad register number %s", buf);
  420.  
  421.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
  422.         {
  423.           if (p[0] == 0 || p[1] == 0)
  424.         error ("Remote reply is too short: %s", buf);
  425.           regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  426.           p += 2;
  427.         }
  428.  
  429.       if (*p++ != ';')
  430.         error("Remote register badly formatted: %s", buf);
  431.  
  432.       supply_register (regno, regs);
  433.     }
  434.     }
  435.   else if (buf[0] != 'S')
  436.     error ("Invalid remote reply: %s", buf);
  437.  
  438.   WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
  439.  
  440.   return 0;
  441. }
  442.  
  443. /* Read the remote registers into the block REGS.  */
  444. /* Currently we just read all the registers, so we don't use regno.  */
  445. /* ARGSUSED */
  446. static void
  447. remote_fetch_registers (regno)
  448.      int regno;
  449. {
  450.   char buf[PBUFSIZ];
  451.   int i;
  452.   char *p;
  453.   char regs[REGISTER_BYTES];
  454.  
  455.   sprintf (buf, "g");
  456.   remote_send (buf);
  457.  
  458.   /* Reply describes registers byte by byte, each byte encoded as two
  459.      hex characters.  Suck them all up, then supply them to the
  460.      register cacheing/storage mechanism.  */
  461.  
  462.   p = buf;
  463.   for (i = 0; i < REGISTER_BYTES; i++)
  464.     {
  465.       if (p[0] == 0 || p[1] == 0)
  466.     error ("Remote reply is too short: %s", buf);
  467.       regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  468.       p += 2;
  469.     }
  470.   for (i = 0; i < NUM_REGS; i++)
  471.     supply_register (i, ®s[REGISTER_BYTE(i)]);
  472. }
  473.  
  474. /* Prepare to store registers.  Since we send them all, we have to
  475.    read out the ones we don't want to change first.  */
  476.  
  477. static void 
  478. remote_prepare_to_store ()
  479. {
  480.   /* Make sure the entire registers array is valid.  */
  481.   read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
  482. }
  483.  
  484. /* Store the remote registers from the contents of the block REGISTERS. 
  485.    FIXME, eventually just store one register if that's all that is needed.  */
  486.  
  487. /* ARGSUSED */
  488. static void
  489. remote_store_registers (regno)
  490.      int regno;
  491. {
  492.   char buf[PBUFSIZ];
  493.   int i;
  494.   char *p;
  495.  
  496.   buf[0] = 'G';
  497.   
  498.   /* Command describes registers byte by byte,
  499.      each byte encoded as two hex characters.  */
  500.  
  501.   p = buf + 1;
  502.   for (i = 0; i < REGISTER_BYTES; i++)
  503.     {
  504.       *p++ = tohex ((registers[i] >> 4) & 0xf);
  505.       *p++ = tohex (registers[i] & 0xf);
  506.     }
  507.   *p = '\0';
  508.  
  509.   remote_send (buf);
  510. }
  511.  
  512. #if 0
  513. /* Read a word from remote address ADDR and return it.
  514.    This goes through the data cache.  */
  515.  
  516. int
  517. remote_fetch_word (addr)
  518.      CORE_ADDR addr;
  519. {
  520.   if (icache)
  521.     {
  522.       extern CORE_ADDR text_start, text_end;
  523.  
  524.       if (addr >= text_start && addr < text_end)
  525.     {
  526.       int buffer;
  527.       xfer_core_file (addr, &buffer, sizeof (int));
  528.       return buffer;
  529.     }
  530.     }
  531.   return dcache_fetch (addr);
  532. }
  533.  
  534. /* Write a word WORD into remote address ADDR.
  535.    This goes through the data cache.  */
  536.  
  537. void
  538. remote_store_word (addr, word)
  539.      CORE_ADDR addr;
  540.      int word;
  541. {
  542.   dcache_poke (addr, word);
  543. }
  544. #endif /* 0 */
  545.  
  546. /* Write memory data directly to the remote machine.
  547.    This does not inform the data cache; the data cache uses this.
  548.    MEMADDR is the address in the remote memory space.
  549.    MYADDR is the address of the buffer in our space.
  550.    LEN is the number of bytes.  */
  551.  
  552. static void
  553. remote_write_bytes (memaddr, myaddr, len)
  554.      CORE_ADDR memaddr;
  555.      char *myaddr;
  556.      int len;
  557. {
  558.   char buf[PBUFSIZ];
  559.   int i;
  560.   char *p;
  561.  
  562.   if (len > PBUFSIZ / 2 - 20)
  563.     abort ();
  564.  
  565.   sprintf (buf, "M%x,%x:", memaddr, len);
  566.  
  567.   /* We send target system values byte by byte, in increasing byte addresses,
  568.      each byte encoded as two hex characters.  */
  569.  
  570.   p = buf + strlen (buf);
  571.   for (i = 0; i < len; i++)
  572.     {
  573.       *p++ = tohex ((myaddr[i] >> 4) & 0xf);
  574.       *p++ = tohex (myaddr[i] & 0xf);
  575.     }
  576.   *p = '\0';
  577.  
  578.   remote_send (buf);
  579. }
  580.  
  581. /* Read memory data directly from the remote machine.
  582.    This does not use the data cache; the data cache uses this.
  583.    MEMADDR is the address in the remote memory space.
  584.    MYADDR is the address of the buffer in our space.
  585.    LEN is the number of bytes.  */
  586.  
  587. static void
  588. remote_read_bytes (memaddr, myaddr, len)
  589.      CORE_ADDR memaddr;
  590.      char *myaddr;
  591.      int len;
  592. {
  593.   char buf[PBUFSIZ];
  594.   int i;
  595.   char *p;
  596.  
  597.   if (len > PBUFSIZ / 2 - 1)
  598.     abort ();
  599.  
  600.   sprintf (buf, "m%x,%x", memaddr, len);
  601.   remote_send (buf);
  602.  
  603.   /* Reply describes memory byte by byte,
  604.      each byte encoded as two hex characters.  */
  605.  
  606.   p = buf;
  607.   for (i = 0; i < len; i++)
  608.     {
  609.       if (p[0] == 0 || p[1] == 0)
  610.     error ("Remote reply is too short: %s", buf);
  611.       myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  612.       p += 2;
  613.     }
  614. }
  615.  
  616. /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
  617.    to or from debugger address MYADDR.  Write to inferior if SHOULD_WRITE is
  618.    nonzero.  Returns length of data written or read; 0 for error.  */
  619.  
  620. /* ARGSUSED */
  621. static int
  622. remote_xfer_memory(memaddr, myaddr, len, should_write, target)
  623.      CORE_ADDR memaddr;
  624.      char *myaddr;
  625.      int len;
  626.      int should_write;
  627.      struct target_ops *target;            /* ignored */
  628. {
  629.   int origlen = len;
  630.   int xfersize;
  631.   while (len > 0)
  632.     {
  633.       if (len > MAXBUFBYTES)
  634.     xfersize = MAXBUFBYTES;
  635.       else
  636.     xfersize = len;
  637.  
  638.       if (should_write)
  639.         remote_write_bytes(memaddr, myaddr, xfersize);
  640.       else
  641.     remote_read_bytes (memaddr, myaddr, xfersize);
  642.       memaddr += xfersize;
  643.       myaddr  += xfersize;
  644.       len     -= xfersize;
  645.     }
  646.   return origlen; /* no error possible */
  647. }
  648.  
  649. static void
  650. remote_files_info (ignore)
  651. struct target_ops *ignore;
  652. {
  653.   puts_filtered ("Debugging a target over a serial line.\n");
  654. }
  655.  
  656. /*
  657.  
  658. A debug packet whose contents are <data>
  659. is encapsulated for transmission in the form:
  660.  
  661.     $ <data> # CSUM1 CSUM2
  662.  
  663.     <data> must be ASCII alphanumeric and cannot include characters
  664.     '$' or '#'
  665.  
  666.     CSUM1 and CSUM2 are ascii hex representation of an 8-bit 
  667.     checksum of <data>, the most significant nibble is sent first.
  668.     the hex digits 0-9,a-f are used.
  669.  
  670. Receiver responds with:
  671.  
  672.     +    - if CSUM is correct and ready for next packet
  673.     -    - if CSUM is incorrect
  674.  
  675. */
  676.  
  677. /* Read a single character from the remote end, masking it down to 7 bits. */
  678.  
  679. static int
  680. readchar ()
  681. {
  682.   int ch;
  683.  
  684.   ch = SERIAL_READCHAR (remote_desc, timeout);
  685.  
  686.   if (ch < 0)
  687.     return ch;
  688.  
  689.   return ch & 0x7f;
  690. }
  691.  
  692. /* Send the command in BUF to the remote machine,
  693.    and read the reply into BUF.
  694.    Report an error if we get an error reply.  */
  695.  
  696. static void
  697. remote_send (buf)
  698.      char *buf;
  699. {
  700.  
  701.   putpkt (buf);
  702.   getpkt (buf, 0);
  703.  
  704.   if (buf[0] == 'E')
  705.     error ("Remote failure reply: %s", buf);
  706. }
  707.  
  708. /* Send a packet to the remote machine, with error checking.
  709.    The data of the packet is in BUF.  */
  710.  
  711. static void
  712. putpkt (buf)
  713.      char *buf;
  714. {
  715.   int i;
  716.   unsigned char csum = 0;
  717.   char buf2[PBUFSIZ];
  718.   int cnt = strlen (buf);
  719.   int ch;
  720.   char *p;
  721.  
  722.   /* Copy the packet into buffer BUF2, encapsulating it
  723.      and giving it a checksum.  */
  724.  
  725.   if (cnt > sizeof(buf2) - 5)        /* Prosanity check */
  726.     abort();
  727.  
  728.   p = buf2;
  729.   *p++ = '$';
  730.  
  731.   for (i = 0; i < cnt; i++)
  732.     {
  733.       csum += buf[i];
  734.       *p++ = buf[i];
  735.     }
  736.   *p++ = '#';
  737.   *p++ = tohex ((csum >> 4) & 0xf);
  738.   *p++ = tohex (csum & 0xf);
  739.  
  740.   /* Send it over and over until we get a positive ack.  */
  741.  
  742.   do {
  743.     if (kiodebug)
  744.       {
  745.     *p = '\0';
  746.     printf ("Sending packet: %s...", buf2);  fflush(stdout);
  747.       }
  748.     SERIAL_WRITE (remote_desc, buf2, p - buf2);
  749.  
  750.     /* read until either a timeout occurs (-2) or '+' is read */
  751.     do {
  752.       ch = readchar ();
  753.       if (kiodebug) {
  754.     if (ch == '+')
  755.       printf("Ack\n");
  756.     else
  757.       printf ("%02X%c ", ch&0xFF, ch);
  758.       }
  759.     } while ((ch != '+') && (ch != SERIAL_TIMEOUT));
  760.   } while (ch != '+');
  761. }
  762.  
  763. /* Read a packet from the remote machine, with error checking,
  764.    and store it in BUF.  BUF is expected to be of size PBUFSIZ.
  765.    If FOREVER, wait forever rather than timing out; this is used
  766.    while the target is executing user code.  */
  767.  
  768. static void
  769. getpkt (buf, forever)
  770.      char *buf;
  771.      int forever;
  772. {
  773.   char *bp;
  774.   unsigned char csum;
  775.   int c = 0;
  776.   unsigned char c1, c2;
  777.   int retries = 0;
  778. #define MAX_RETRIES    10
  779.  
  780.   while (1)
  781.     {
  782.       /* This can loop forever if the remote side sends us characters
  783.      continuously, but if it pauses, we'll get a zero from readchar
  784.      because of timeout.  Then we'll count that as a retry.  */
  785.       while (c != '$')
  786.         if ((c = readchar()) == SERIAL_TIMEOUT)
  787.       if (!forever) 
  788.         {
  789.           if (++retries >= MAX_RETRIES)
  790.         if (kiodebug) puts_filtered ("Timed out.\n");
  791.         goto out;
  792.         }
  793.  
  794.       /* Force csum to be zero here because of possible error retry.  */
  795.       csum = 0;
  796.       bp = buf;
  797.  
  798.       while (1)
  799.     {
  800.       c = readchar ();
  801.       if (c == SERIAL_TIMEOUT)
  802.         {
  803.           if (kiodebug)
  804.         puts_filtered ("Timeout in mid-packet, retrying\n");
  805.           goto whole;        /* Start a new packet, count retries */
  806.         } 
  807.       if (c == '$')
  808.         {
  809.           if (kiodebug)
  810.         puts_filtered ("Saw new packet start in middle of old one\n");
  811.           goto whole;        /* Start a new packet, count retries */
  812.         }
  813.       if (c == '#')
  814.         break;
  815.       if (bp >= buf+PBUFSIZ-1)
  816.       {
  817.         *bp = '\0';
  818.         puts_filtered ("Remote packet too long: ");
  819.         puts_filtered (buf);
  820.         puts_filtered ("\n");
  821.         goto whole;
  822.       }
  823.       *bp++ = c;
  824.       csum += c;
  825.     }
  826.       *bp = 0;
  827.  
  828.       c1 = fromhex (readchar ());
  829.       c2 = fromhex (readchar ());
  830.       if ((csum & 0xff) == (c1 << 4) + c2)
  831.     break;
  832.       printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
  833.           (c1 << 4) + c2, csum & 0xff);
  834.       puts_filtered (buf);
  835.       puts_filtered ("\n");
  836.  
  837.       /* Try the whole thing again.  */
  838. whole:
  839.       if (++retries < MAX_RETRIES)
  840.     {
  841.       SERIAL_WRITE (remote_desc, "-", 1);
  842.     }
  843.       else
  844.     {
  845.       printf ("Ignoring packet error, continuing...\n");
  846.       break;
  847.     }
  848.     }
  849.  
  850. out:
  851.  
  852.   SERIAL_WRITE (remote_desc, "+", 1);
  853.  
  854.   if (kiodebug)
  855.     fprintf (stderr,"Packet received: %s\n", buf);
  856. }
  857.  
  858. /* The data cache leads to incorrect results because it doesn't know about
  859.    volatile variables, thus making it impossible to debug functions which
  860.    use hardware registers.  Therefore it is #if 0'd out.  Effect on
  861.    performance is some, for backtraces of functions with a few
  862.    arguments each.  For functions with many arguments, the stack
  863.    frames don't fit in the cache blocks, which makes the cache less
  864.    helpful.  Disabling the cache is a big performance win for fetching
  865.    large structures, because the cache code fetched data in 16-byte
  866.    chunks.  */
  867. #if 0
  868. /* The data cache records all the data read from the remote machine
  869.    since the last time it stopped.
  870.  
  871.    Each cache block holds 16 bytes of data
  872.    starting at a multiple-of-16 address.  */
  873.  
  874. #define DCACHE_SIZE 64        /* Number of cache blocks */
  875.  
  876. struct dcache_block {
  877.     struct dcache_block *next, *last;
  878.     unsigned int addr;    /* Address for which data is recorded.  */
  879.     int data[4];
  880. };
  881.  
  882. struct dcache_block dcache_free, dcache_valid;
  883.  
  884. /* Free all the data cache blocks, thus discarding all cached data.  */ 
  885.  
  886. static void
  887. dcache_flush ()
  888. {
  889.   register struct dcache_block *db;
  890.  
  891.   while ((db = dcache_valid.next) != &dcache_valid)
  892.     {
  893.       remque (db);
  894.       insque (db, &dcache_free);
  895.     }
  896. }
  897.  
  898. /*
  899.  * If addr is present in the dcache, return the address of the block 
  900.  * containing it.
  901.  */
  902.  
  903. struct dcache_block *
  904. dcache_hit (addr)
  905. {
  906.   register struct dcache_block *db;
  907.  
  908.   if (addr & 3)
  909.     abort ();
  910.  
  911.   /* Search all cache blocks for one that is at this address.  */
  912.   db = dcache_valid.next;
  913.   while (db != &dcache_valid)
  914.     {
  915.       if ((addr & 0xfffffff0) == db->addr)
  916.     return db;
  917.       db = db->next;
  918.     }
  919.   return NULL;
  920. }
  921.  
  922. /*  Return the int data at address ADDR in dcache block DC.  */
  923.  
  924. int
  925. dcache_value (db, addr)
  926.      struct dcache_block *db;
  927.      unsigned int addr;
  928. {
  929.   if (addr & 3)
  930.     abort ();
  931.   return (db->data[(addr>>2)&3]);
  932. }
  933.  
  934. /* Get a free cache block, put it on the valid list,
  935.    and return its address.  The caller should store into the block
  936.    the address and data that it describes.  */
  937.  
  938. struct dcache_block *
  939. dcache_alloc ()
  940. {
  941.   register struct dcache_block *db;
  942.  
  943.   if ((db = dcache_free.next) == &dcache_free)
  944.     /* If we can't get one from the free list, take last valid */
  945.     db = dcache_valid.last;
  946.  
  947.   remque (db);
  948.   insque (db, &dcache_valid);
  949.   return (db);
  950. }
  951.  
  952. /* Return the contents of the word at address ADDR in the remote machine,
  953.    using the data cache.  */
  954.  
  955. int
  956. dcache_fetch (addr)
  957.      CORE_ADDR addr;
  958. {
  959.   register struct dcache_block *db;
  960.  
  961.   db = dcache_hit (addr);
  962.   if (db == 0)
  963.     {
  964.       db = dcache_alloc ();
  965.       remote_read_bytes (addr & ~0xf, db->data, 16);
  966.       db->addr = addr & ~0xf;
  967.     }
  968.   return (dcache_value (db, addr));
  969. }
  970.  
  971. /* Write the word at ADDR both in the data cache and in the remote machine.  */
  972.  
  973. dcache_poke (addr, data)
  974.      CORE_ADDR addr;
  975.      int data;
  976. {
  977.   register struct dcache_block *db;
  978.  
  979.   /* First make sure the word is IN the cache.  DB is its cache block.  */
  980.   db = dcache_hit (addr);
  981.   if (db == 0)
  982.     {
  983.       db = dcache_alloc ();
  984.       remote_read_bytes (addr & ~0xf, db->data, 16);
  985.       db->addr = addr & ~0xf;
  986.     }
  987.  
  988.   /* Modify the word in the cache.  */
  989.   db->data[(addr>>2)&3] = data;
  990.  
  991.   /* Send the changed word.  */
  992.   remote_write_bytes (addr, &data, 4);
  993. }
  994.  
  995. /* Initialize the data cache.  */
  996.  
  997. dcache_init ()
  998. {
  999.   register i;
  1000.   register struct dcache_block *db;
  1001.  
  1002.   db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) * 
  1003.                     DCACHE_SIZE);
  1004.   dcache_free.next = dcache_free.last = &dcache_free;
  1005.   dcache_valid.next = dcache_valid.last = &dcache_valid;
  1006.   for (i=0;i<DCACHE_SIZE;i++,db++)
  1007.     insque (db, &dcache_free);
  1008. }
  1009. #endif /* 0 */
  1010.  
  1011. static void
  1012. remote_kill ()
  1013. {
  1014.   putpkt ("k");
  1015.   /* Don't wait for it to die.  I'm not really sure it matters whether
  1016.      we do or not.  For the existing stubs, kill is a noop.  */
  1017.   target_mourn_inferior ();
  1018. }
  1019.  
  1020. static void
  1021. remote_mourn ()
  1022. {
  1023.   unpush_target (&remote_ops);
  1024.   generic_mourn_inferior ();
  1025. }
  1026.  
  1027. /* Define the target subroutine names */
  1028.  
  1029. struct target_ops remote_ops = {
  1030.   "remote",            /* to_shortname */
  1031.   "Remote serial target in gdb-specific protocol",    /* to_longname */
  1032.   "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
  1033. Specify the serial device it is connected to (e.g. /dev/ttya).",  /* to_doc */
  1034.   remote_open,            /* to_open */
  1035.   remote_close,            /* to_close */
  1036.   NULL,                /* to_attach */
  1037.   remote_detach,        /* to_detach */
  1038.   remote_resume,        /* to_resume */
  1039.   remote_wait,            /* to_wait */
  1040.   remote_fetch_registers,    /* to_fetch_registers */
  1041.   remote_store_registers,    /* to_store_registers */
  1042.   remote_prepare_to_store,    /* to_prepare_to_store */
  1043.   remote_xfer_memory,        /* to_xfer_memory */
  1044.   remote_files_info,        /* to_files_info */
  1045.   NULL,                /* to_insert_breakpoint */
  1046.   NULL,                /* to_remove_breakpoint */
  1047.   NULL,                /* to_terminal_init */
  1048.   NULL,                /* to_terminal_inferior */
  1049.   NULL,                /* to_terminal_ours_for_output */
  1050.   NULL,                /* to_terminal_ours */
  1051.   NULL,                /* to_terminal_info */
  1052.   remote_kill,            /* to_kill */
  1053.   NULL,                /* to_load */
  1054.   NULL,                /* to_lookup_symbol */
  1055.   NULL,                /* to_create_inferior */
  1056.   remote_mourn,            /* to_mourn_inferior */
  1057.   0,                /* to_can_run */
  1058.   0,                /* to_notice_signals */
  1059.   process_stratum,        /* to_stratum */
  1060.   NULL,                /* to_next */
  1061.   1,                /* to_has_all_memory */
  1062.   1,                /* to_has_memory */
  1063.   1,                /* to_has_stack */
  1064.   1,                /* to_has_registers */
  1065.   1,                /* to_has_execution */
  1066.   NULL,                /* sections */
  1067.   NULL,                /* sections_end */
  1068.   OPS_MAGIC            /* to_magic */
  1069. };
  1070.  
  1071. void
  1072. _initialize_remote ()
  1073. {
  1074.   add_target (&remote_ops);
  1075.  
  1076.   add_show_from_set (
  1077.     add_set_cmd ("remotedebug", no_class, var_boolean, (char *)&kiodebug,
  1078.            "Set debugging of remote serial I/O.\n\
  1079. When enabled, each packet sent or received with the remote target\n\
  1080. is displayed.", &setlist),
  1081.     &showlist);
  1082. }
  1083.  
  1084. #endif
  1085.