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

  1. /* Remote target communications for serial-line targets in custom GDB protocol
  2.    Copyright 1988, 1991, 1992, 1993, 1994 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.  
  22.    A debug packet whose contents are <data>
  23.    is encapsulated for transmission in the form:
  24.  
  25.     $ <data> # CSUM1 CSUM2
  26.  
  27.     <data> must be ASCII alphanumeric and cannot include characters
  28.     '$' or '#'.  If <data> starts with two characters followed by
  29.     ':', then the existing stubs interpret this as a sequence number.
  30.  
  31.     CSUM1 and CSUM2 are ascii hex representation of an 8-bit 
  32.     checksum of <data>, the most significant nibble is sent first.
  33.     the hex digits 0-9,a-f are used.
  34.  
  35.    Receiver responds with:
  36.  
  37.     +    - if CSUM is correct and ready for next packet
  38.     -    - if CSUM is incorrect
  39.  
  40.    <data> is as follows:
  41.    Most values are encoded in ascii hex digits.  Signal numbers are according
  42.    to the numbering in target.h.
  43.  
  44.     Request        Packet
  45.  
  46.     set thread    Hct...        Set thread for subsequent operations.
  47.                     c = 'c' for thread used in step and 
  48.                     continue; t... can be -1 for all
  49.                     threads.
  50.                     c = 'g' for thread used in other
  51.                     operations.  If zero, pick a thread,
  52.                     any thread.
  53.     reply        OK        for success
  54.             ENN        for an error.
  55.  
  56.     read registers  g
  57.     reply        XX....X        Each byte of register data
  58.                     is described by two hex digits.
  59.                     Registers are in the internal order
  60.                     for GDB, and the bytes in a register
  61.                     are in the same order the machine uses.
  62.             or ENN        for an error.
  63.  
  64.     write regs    GXX..XX        Each byte of register data
  65.                     is described by two hex digits.
  66.     reply        OK        for success
  67.             ENN        for an error
  68.  
  69.         write reg    Pn...=r...    Write register n... with value r...,
  70.                     which contains two hex digits for each
  71.                     byte in the register (target byte
  72.                     order).
  73.     reply        OK        for success
  74.             ENN        for an error
  75.     (not supported by all stubs).
  76.  
  77.     read mem    mAA..AA,LLLL    AA..AA is address, LLLL is length.
  78.     reply        XX..XX        XX..XX is mem contents
  79.                     Can be fewer bytes than requested
  80.                     if able to read only part of the data.
  81.             or ENN        NN is errno
  82.  
  83.     write mem    MAA..AA,LLLL:XX..XX
  84.                     AA..AA is address,
  85.                     LLLL is number of bytes,
  86.                     XX..XX is data
  87.     reply        OK        for success
  88.             ENN        for an error (this includes the case
  89.                     where only part of the data was
  90.                     written).
  91.  
  92.     continue    cAA..AA        AA..AA is address to resume
  93.                     If AA..AA is omitted,
  94.                     resume at same address.
  95.  
  96.     step        sAA..AA        AA..AA is address to resume
  97.                     If AA..AA is omitted,
  98.                     resume at same address.
  99.  
  100.     continue with    Csig;AA        Continue with signal sig (hex signal
  101.     signal                number).
  102.  
  103.     step with    Ssig;AA        Like 'C' but step not continue.
  104.     signal
  105.  
  106.     last signal     ?               Reply the current reason for stopping.
  107.                                         This is the same reply as is generated
  108.                     for step or cont : SAA where AA is the
  109.                     signal number.
  110.  
  111.     There is no immediate reply to step or cont.
  112.     The reply comes when the machine stops.
  113.     It is        SAA        AA is the signal number.
  114.  
  115.     or...        TAAn...:r...;n...:r...;n...:r...;
  116.                     AA = signal number
  117.                     n... = register number (hex)
  118.                       r... = register contents
  119.                     n... = `thread'
  120.                       r... = thread process ID.  This is
  121.                          a hex integer.
  122.                     n... = other string not starting 
  123.                         with valid hex digit.
  124.                       gdb should ignore this n,r pair
  125.                       and go on to the next.  This way
  126.                       we can extend the protocol.
  127.     or...        WAA        The process exited, and AA is
  128.                     the exit status.  This is only
  129.                     applicable for certains sorts of
  130.                     targets.
  131.     or...        XAA        The process terminated with signal
  132.                     AA.
  133.     or...             Otext        Send text to stdout.  This can happen
  134.                     at any time while the program is
  135.                     running and the debugger should
  136.                     continue to wait for 'W', 'T', etc.
  137.  
  138.     kill request    k
  139.  
  140.     toggle debug    d        toggle debug flag (see 386 & 68k stubs)
  141.     reset        r        reset -- see sparc stub.
  142.     reserved    <other>        On other requests, the stub should
  143.                     ignore the request and send an empty
  144.                     response ($#<checksum>).  This way
  145.                     we can extend the protocol and GDB
  146.                     can tell whether the stub it is
  147.                     talking to uses the old or the new.
  148.     search        tAA:PP,MM    Search backwards starting at address
  149.                     AA for a match with pattern PP and
  150.                     mask MM.  PP and MM are 4 bytes.
  151.                     Not supported by all stubs.
  152.  
  153.     general query    qXXXX        Request info about XXXX.
  154.     general set    QXXXX=yyyy    Set value of XXXX to yyyy.
  155.     query sect offs    qOffsets    Get section offsets.  Reply is
  156.                     Text=xxx;Data=yyy;Bss=zzz
  157.  
  158.     Responses can be run-length encoded to save space.  A '*' means that
  159.     the next character is an ASCII encoding giving a repeat count which
  160.     stands for that many repititions of the character preceding the '*'.
  161.     The encoding is n+29, yielding a printable character where n >=3 
  162.     (which is where rle starts to win).  Don't use an n > 126.
  163.  
  164.     So 
  165.     "0* " means the same as "0000".  */
  166.  
  167. #include "defs.h"
  168. #include <string.h>
  169. #include <fcntl.h>
  170. #include "frame.h"
  171. #include "inferior.h"
  172. #include "bfd.h"
  173. #include "symfile.h"
  174. #include "target.h"
  175. #include "wait.h"
  176. #include "terminal.h"
  177. #include "gdbcmd.h"
  178. #include "objfiles.h"
  179. #include "gdb-stabs.h"
  180. #include "thread.h"
  181.  
  182. #include "dcache.h"
  183.  
  184. #ifdef USG
  185. #include <sys/types.h>
  186. #endif
  187.  
  188. #include <signal.h>
  189. #include "serial.h"
  190.  
  191. /* Prototypes for local functions */
  192.  
  193. static int remote_write_bytes PARAMS ((CORE_ADDR memaddr,
  194.                        unsigned char *myaddr, int len));
  195.  
  196. static int remote_read_bytes PARAMS ((CORE_ADDR memaddr,
  197.                       unsigned char *myaddr, int len));
  198.  
  199. static void remote_files_info PARAMS ((struct target_ops *ignore));
  200.  
  201. static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
  202.                        int len, int should_write,
  203.                        struct target_ops *target));
  204.  
  205. static void remote_prepare_to_store PARAMS ((void));
  206.  
  207. static void remote_fetch_registers PARAMS ((int regno));
  208.  
  209. static void remote_resume PARAMS ((int pid, int step,
  210.                    enum target_signal siggnal));
  211.  
  212. static int remote_start_remote PARAMS ((char *dummy));
  213.  
  214. static void remote_open PARAMS ((char *name, int from_tty));
  215.  
  216. static void remote_close PARAMS ((int quitting));
  217.  
  218. static void remote_store_registers PARAMS ((int regno));
  219.  
  220. static void getpkt PARAMS ((char *buf, int forever));
  221.  
  222. static int putpkt PARAMS ((char *buf));
  223.  
  224. static void remote_send PARAMS ((char *buf));
  225.  
  226. static int readchar PARAMS ((int timeout));
  227.  
  228. static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
  229.  
  230. static int tohex PARAMS ((int nib));
  231.  
  232. static int fromhex PARAMS ((int a));
  233.  
  234. static void remote_detach PARAMS ((char *args, int from_tty));
  235.  
  236. static void remote_interrupt PARAMS ((int signo));
  237.  
  238. static void remote_interrupt_twice PARAMS ((int signo));
  239.  
  240. static void interrupt_query PARAMS ((void));
  241.  
  242. extern struct target_ops remote_ops;    /* Forward decl */
  243.  
  244. /* This was 5 seconds, which is a long time to sit and wait.
  245.    Unless this is going though some terminal server or multiplexer or
  246.    other form of hairy serial connection, I would think 2 seconds would
  247.    be plenty.  */
  248.  
  249. static int remote_timeout = 2;
  250.  
  251. /* Descriptor for I/O to remote machine.  Initialize it to NULL so that
  252.    remote_open knows that we don't have a file open when the program
  253.    starts.  */
  254. serial_t remote_desc = NULL;
  255.  
  256. /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
  257.    and i386-stub.c.  Normally, no one would notice because it only matters
  258.    for writing large chunks of memory (e.g. in downloads).  Also, this needs
  259.    to be more than 400 if required to hold the registers (see below, where
  260.    we round it up based on REGISTER_BYTES).  */
  261. #define    PBUFSIZ    400
  262.  
  263. /* Maximum number of bytes to read/write at once.  The value here
  264.    is chosen to fill up a packet (the headers account for the 32).  */
  265. #define MAXBUFBYTES ((PBUFSIZ-32)/2)
  266.  
  267. /* Round up PBUFSIZ to hold all the registers, at least.  */
  268. /* The blank line after the #if seems to be required to work around a
  269.    bug in HP's PA compiler.  */
  270. #if REGISTER_BYTES > MAXBUFBYTES
  271.  
  272. #undef PBUFSIZ
  273. #define    PBUFSIZ    (REGISTER_BYTES * 2 + 32)
  274. #endif
  275.  
  276. /* Should we try the 'P' request?  If this is set to one when the stub
  277.    doesn't support 'P', the only consequence is some unnecessary traffic.  */
  278. static int stub_supports_P = 1;
  279.  
  280.  
  281. /* These are the threads which we last sent to the remote system.  -1 for all
  282.    or -2 for not sent yet.  */
  283. int general_thread;
  284. int cont_thread;
  285.  
  286. static void
  287. set_thread (th, gen)
  288.      int th;
  289.      int gen;
  290. {
  291.   char buf[PBUFSIZ];
  292.   int state = gen ? general_thread : cont_thread;
  293.   if (state == th)
  294.     return;
  295.   buf[0] = 'H';
  296.   buf[1] = gen ? 'g' : 'c';
  297.   if (th == 42000)
  298.     {
  299.       buf[2] = '0';
  300.       buf[3] = '\0';
  301.     }
  302.   else if (th < 0)
  303.     sprintf (&buf[2], "-%x", -th);
  304.   else
  305.     sprintf (&buf[2], "%x", th);
  306.   putpkt (buf);
  307.   getpkt (buf, 0);
  308.   if (gen)
  309.     general_thread = th;
  310.   else
  311.     cont_thread = th;
  312. }
  313.  
  314. /* Clean up connection to a remote debugger.  */
  315.  
  316. /* ARGSUSED */
  317. static void
  318. remote_close (quitting)
  319.      int quitting;
  320. {
  321.   if (remote_desc)
  322.     SERIAL_CLOSE (remote_desc);
  323.   remote_desc = NULL;
  324. }
  325.  
  326. /* Query the remote side for the text, data and bss offsets. */
  327.  
  328. static void
  329. get_offsets ()
  330. {
  331.   char buf[PBUFSIZ];
  332.   int nvals;
  333.   CORE_ADDR text_addr, data_addr, bss_addr;
  334.   struct section_offsets *offs;
  335.  
  336.   putpkt ("qOffsets");
  337.  
  338.   getpkt (buf, 0);
  339.  
  340.   if (buf[0] == '\000')
  341.     return;            /* Return silently.  Stub doesn't support this
  342.                    command. */
  343.   if (buf[0] == 'E')
  344.     {
  345.       warning ("Remote failure reply: %s", buf);
  346.       return;
  347.     }
  348.  
  349.   nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr,
  350.           &bss_addr);
  351.   if (nvals != 3)
  352.     error ("Malformed response to offset query, %s", buf);
  353.  
  354.   if (symfile_objfile == NULL)
  355.     return;
  356.  
  357.   offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
  358.                         + symfile_objfile->num_sections
  359.                         * sizeof (offs->offsets));
  360.   memcpy (offs, symfile_objfile->section_offsets,
  361.       sizeof (struct section_offsets)
  362.       + symfile_objfile->num_sections
  363.       * sizeof (offs->offsets));
  364.  
  365.   /* FIXME: This code assumes gdb-stabs.h is being used; it's broken
  366.      for xcoff, dwarf, sdb-coff, etc.  But there is no simple
  367.      canonical representation for this stuff.  (Just what does "text"
  368.      as seen by the stub mean, anyway?  I think it means all sections
  369.      with SEC_CODE set, but we currently have no way to deal with that).  */
  370.  
  371.   ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
  372.  
  373.   /* This is a temporary kludge to force data and bss to use the same offsets
  374.      because that's what nlmconv does now.  The real solution requires changes
  375.      to the stub and remote.c that I don't have time to do right now.  */
  376.  
  377.   ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
  378.   ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
  379.  
  380.   objfile_relocate (symfile_objfile, offs);
  381. }
  382.  
  383. /* Stub for catch_errors.  */
  384.  
  385. static int
  386. remote_start_remote (dummy)
  387.      char *dummy;
  388. {
  389.   immediate_quit = 1;        /* Allow user to interrupt it */
  390.  
  391.   /* Ack any packet which the remote side has already sent.  */
  392.  
  393.   SERIAL_WRITE (remote_desc, "+", 1);
  394.  
  395.   /* Let the stub know that we want it to return the thread.  */
  396.   set_thread (-1, 0);
  397.  
  398.   get_offsets ();        /* Get text, data & bss offsets */
  399.  
  400.   putpkt ("?");            /* initiate a query from remote machine */
  401.   immediate_quit = 0;
  402.  
  403.   start_remote ();        /* Initialize gdb process mechanisms */
  404.  
  405.   return 1;
  406. }
  407.  
  408. /* Open a connection to a remote debugger.
  409.    NAME is the filename used for communication.  */
  410.  
  411. static DCACHE *remote_dcache;
  412.  
  413. static void
  414. remote_open (name, from_tty)
  415.      char *name;
  416.      int from_tty;
  417. {
  418.   if (name == 0)
  419.     error (
  420. "To open a remote debug connection, you need to specify what serial\n\
  421. device is attached to the remote system (e.g. /dev/ttya).");
  422.  
  423.   target_preopen (from_tty);
  424.  
  425.   unpush_target (&remote_ops);
  426.  
  427.   remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
  428.  
  429.   remote_desc = SERIAL_OPEN (name);
  430.   if (!remote_desc)
  431.     perror_with_name (name);
  432.  
  433.   if (baud_rate != -1)
  434.     {
  435.       if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
  436.     {
  437.       SERIAL_CLOSE (remote_desc);
  438.       perror_with_name (name);
  439.     }
  440.     }
  441.  
  442.   SERIAL_RAW (remote_desc);
  443.  
  444.   /* If there is something sitting in the buffer we might take it as a
  445.      response to a command, which would be bad.  */
  446.   SERIAL_FLUSH_INPUT (remote_desc);
  447.  
  448.   if (from_tty)
  449.     {
  450.       puts_filtered ("Remote debugging using ");
  451.       puts_filtered (name);
  452.       puts_filtered ("\n");
  453.     }
  454.   push_target (&remote_ops);    /* Switch to using remote target now */
  455.  
  456.   /* Start out by trying the 'P' request to set registers.  We set this each
  457.      time that we open a new target so that if the user switches from one
  458.      stub to another, we can (if the target is closed and reopened) cope.  */
  459.   stub_supports_P = 1;
  460.  
  461.   general_thread = -2;
  462.   cont_thread = -2;
  463.  
  464.   /* Without this, some commands which require an active target (such as kill)
  465.      won't work.  This variable serves (at least) double duty as both the pid
  466.      of the target process (if it has such), and as a flag indicating that a
  467.      target is active.  These functions should be split out into seperate
  468.      variables, especially since GDB will someday have a notion of debugging
  469.      several processes.  */
  470.  
  471.   inferior_pid = 42000;
  472.  
  473.   /* Start the remote connection; if error (0), discard this target.
  474.      In particular, if the user quits, be sure to discard it
  475.      (we'd be in an inconsistent state otherwise).  */
  476.   if (!catch_errors (remote_start_remote, (char *)0, 
  477.     "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
  478.     pop_target();
  479. }
  480.  
  481. /* remote_detach()
  482.    takes a program previously attached to and detaches it.
  483.    We better not have left any breakpoints
  484.    in the program or it'll die when it hits one.
  485.    Close the open connection to the remote debugger.
  486.    Use this when you want to detach and do something else
  487.    with your gdb.  */
  488.  
  489. static void
  490. remote_detach (args, from_tty)
  491.      char *args;
  492.      int from_tty;
  493. {
  494.   if (args)
  495.     error ("Argument given to \"detach\" when remotely debugging.");
  496.   
  497.   pop_target ();
  498.   if (from_tty)
  499.     puts_filtered ("Ending remote debugging.\n");
  500. }
  501.  
  502. /* Convert hex digit A to a number.  */
  503.  
  504. static int
  505. fromhex (a)
  506.      int a;
  507. {
  508.   if (a >= '0' && a <= '9')
  509.     return a - '0';
  510.   else if (a >= 'a' && a <= 'f')
  511.     return a - 'a' + 10;
  512.   else
  513.     error ("Reply contains invalid hex digit %d", a);
  514. }
  515.  
  516. /* Convert number NIB to a hex digit.  */
  517.  
  518. static int
  519. tohex (nib)
  520.      int nib;
  521. {
  522.   if (nib < 10)
  523.     return '0'+nib;
  524.   else
  525.     return 'a'+nib-10;
  526. }
  527.  
  528. /* Tell the remote machine to resume.  */
  529.  
  530. static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
  531. int last_sent_step;
  532.  
  533. static void
  534. remote_resume (pid, step, siggnal)
  535.      int pid, step;
  536.      enum target_signal siggnal;
  537. {
  538.   char buf[PBUFSIZ];
  539.  
  540.   if (pid == -1)
  541.     set_thread (inferior_pid, 0);
  542.   else
  543.     set_thread (pid, 0);
  544.  
  545.   dcache_flush (remote_dcache);
  546.  
  547.   last_sent_signal = siggnal;
  548.   last_sent_step = step;
  549.  
  550.   if (siggnal != TARGET_SIGNAL_0)
  551.     {
  552.       buf[0] = step ? 'S' : 'C';
  553.       buf[1] = tohex (((int)siggnal >> 4) & 0xf);
  554.       buf[2] = tohex ((int)siggnal & 0xf);
  555.       buf[3] = '\0';
  556.     }
  557.   else
  558.     strcpy (buf, step ? "s": "c");
  559.  
  560.   putpkt (buf);
  561. }
  562.  
  563. /* Send ^C to target to halt it.  Target will respond, and send us a
  564.    packet.  */
  565.  
  566. static void
  567. remote_interrupt (signo)
  568.      int signo;
  569. {
  570.   /* If this doesn't work, try more severe steps.  */
  571.   signal (signo, remote_interrupt_twice);
  572.   
  573.   if (remote_debug)
  574.     printf_unfiltered ("remote_interrupt called\n");
  575.  
  576.   SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
  577. }
  578.  
  579. static void (*ofunc)();
  580.  
  581. /* The user typed ^C twice.  */
  582. static void
  583. remote_interrupt_twice (signo)
  584.      int signo;
  585. {
  586.   signal (signo, ofunc);
  587.   
  588.   interrupt_query ();
  589.  
  590.   signal (signo, remote_interrupt);
  591. }
  592.  
  593. /* Ask the user what to do when an interrupt is received.  */
  594.  
  595. static void
  596. interrupt_query ()
  597. {
  598.   target_terminal_ours ();
  599.  
  600.   if (query ("Interrupted while waiting for the program.\n\
  601. Give up (and stop debugging it)? "))
  602.     {
  603.       target_mourn_inferior ();
  604.       return_to_top_level (RETURN_QUIT);
  605.     }
  606.  
  607.   target_terminal_inferior ();
  608. }
  609.  
  610. /* If nonzero, ignore the next kill.  */
  611. int kill_kludge;
  612.  
  613. /* Wait until the remote machine stops, then return,
  614.    storing status in STATUS just as `wait' would.
  615.    Returns "pid" (though it's not clear what, if anything, that
  616.    means in the case of this target).  */
  617.  
  618. static int
  619. remote_wait (pid, status)
  620.      int pid;
  621.      struct target_waitstatus *status;
  622. {
  623.   unsigned char buf[PBUFSIZ];
  624.   int thread_num = -1;
  625.  
  626.   status->kind = TARGET_WAITKIND_EXITED;
  627.   status->value.integer = 0;
  628.  
  629.   while (1)
  630.     {
  631.       unsigned char *p;
  632.  
  633.       ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
  634.       getpkt ((char *) buf, 1);
  635.       signal (SIGINT, ofunc);
  636.  
  637.       switch (buf[0])
  638.     {
  639.     case 'E':        /* Error of some sort */
  640.       warning ("Remote failure reply: %s", buf);
  641.       continue;
  642.     case 'T':        /* Status with PC, SP, FP, ... */
  643.       {
  644.         int i;
  645.         long regno;
  646.         char regs[MAX_REGISTER_RAW_SIZE];
  647.  
  648.         /* Expedited reply, containing Signal, {regno, reg} repeat */
  649.         /*  format is:  'Tssn...:r...;n...:r...;n...:r...;#cc', where
  650.         ss = signal number
  651.         n... = register number
  652.         r... = register contents
  653.         */
  654.  
  655.         p = &buf[3];    /* after Txx */
  656.  
  657.         while (*p)
  658.           {
  659.         unsigned char *p1;
  660.  
  661.         regno = strtol (p, &p1, 16); /* Read the register number */
  662.  
  663.         if (p1 == p)
  664.           {
  665.             p1 = (unsigned char *) strchr (p, ':');
  666.             if (p1 == NULL)
  667.               warning ("Malformed packet (missing colon): %s\n\
  668. Packet: '%s'\n",
  669.                    p, buf);
  670.             if (strncmp (p, "thread", p1 - p) == 0)
  671.               {
  672.             thread_num = strtol (++p1, &p, 16);
  673.               }
  674.           }
  675.         else
  676.           {
  677.             p = p1;
  678.  
  679.             if (*p++ != ':')
  680.               warning ("Malformed packet (missing colon): %s\n\
  681. Packet: '%s'\n",
  682.                    p, buf);
  683.  
  684.             if (regno >= NUM_REGS)
  685.               warning ("Remote sent bad register number %d: %s\n\
  686. Packet: '%s'\n",
  687.                    regno, p, buf);
  688.  
  689.             for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
  690.               {
  691.             if (p[0] == 0 || p[1] == 0)
  692.               warning ("Remote reply is too short: %s", buf);
  693.             regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  694.             p += 2;
  695.               }
  696.             supply_register (regno, regs);
  697.           }
  698.  
  699.         if (*p++ != ';')
  700.           warning ("Remote register badly formatted: %s", buf);
  701.           }
  702.       }
  703.       /* fall through */
  704.     case 'S':        /* Old style status, just signal only */
  705.       status->kind = TARGET_WAITKIND_STOPPED;
  706.       status->value.sig = (enum target_signal)
  707.         (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
  708.  
  709.       goto got_status;
  710.     case 'W':        /* Target exited */
  711.       {
  712.         /* The remote process exited.  */
  713.         status->kind = TARGET_WAITKIND_EXITED;
  714.         status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
  715.         goto got_status;
  716.       }
  717.     case 'X':
  718.       status->kind = TARGET_WAITKIND_SIGNALLED;
  719.       status->value.sig = (enum target_signal)
  720.         (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
  721.       kill_kludge = 1;
  722.  
  723.       goto got_status;
  724.     case 'O':        /* Console output */
  725.       fputs_filtered ((char *)(buf + 1), gdb_stdout);
  726.       continue;
  727.     case '\0':
  728.       if (last_sent_signal != TARGET_SIGNAL_0)
  729.         {
  730.           /* Zero length reply means that we tried 'S' or 'C' and
  731.          the remote system doesn't support it.  */
  732.           target_terminal_ours_for_output ();
  733.           printf_filtered
  734.         ("Can't send signals to this remote system.  %s not sent.\n",
  735.          target_signal_to_name (last_sent_signal));
  736.           last_sent_signal = TARGET_SIGNAL_0;
  737.           target_terminal_inferior ();
  738.  
  739.           strcpy (buf, last_sent_step ? "s" : "c");
  740.           putpkt (buf);
  741.           continue;
  742.         }
  743.       /* else fallthrough */
  744.     default:
  745.       warning ("Invalid remote reply: %s", buf);
  746.       continue;
  747.     }
  748.     }
  749.  got_status:
  750.   if (thread_num != -1)
  751.     {
  752.       /* Initial thread value can only be acquired via wait, so deal with
  753.      this marker which is used before the first thread value is
  754.      acquired.  */
  755.       if (inferior_pid == 42000)
  756.     {
  757.       inferior_pid = thread_num;
  758.       add_thread (inferior_pid);
  759.     }
  760.       return thread_num;
  761.     }
  762.   return inferior_pid;
  763. }
  764.  
  765. /* Number of bytes of registers this stub implements.  */
  766. static int register_bytes_found;
  767.  
  768. /* Read the remote registers into the block REGS.  */
  769. /* Currently we just read all the registers, so we don't use regno.  */
  770. /* ARGSUSED */
  771. static void
  772. remote_fetch_registers (regno)
  773.      int regno;
  774. {
  775.   char buf[PBUFSIZ];
  776.   int i;
  777.   char *p;
  778.   char regs[REGISTER_BYTES];
  779.  
  780.   set_thread (inferior_pid, 1);
  781.  
  782.   sprintf (buf, "g");
  783.   remote_send (buf);
  784.  
  785.   /* Unimplemented registers read as all bits zero.  */
  786.   memset (regs, 0, REGISTER_BYTES);
  787.  
  788.   /* We can get out of synch in various cases.  If the first character
  789.      in the buffer is not a hex character, assume that has happened
  790.      and try to fetch another packet to read.  */
  791.   while ((buf[0] < '0' || buf[0] > '9')
  792.      && (buf[0] < 'a' || buf[0] > 'f'))
  793.     {
  794.       if (remote_debug)
  795.     printf_unfiltered ("Bad register packet; fetching a new packet\n");
  796.       getpkt (buf, 0);
  797.     }
  798.  
  799.   /* Reply describes registers byte by byte, each byte encoded as two
  800.      hex characters.  Suck them all up, then supply them to the
  801.      register cacheing/storage mechanism.  */
  802.  
  803.   p = buf;
  804.   for (i = 0; i < REGISTER_BYTES; i++)
  805.     {
  806.       if (p[0] == 0)
  807.     break;
  808.       if (p[1] == 0)
  809.     {
  810.       warning ("Remote reply is of odd length: %s", buf);
  811.       /* Don't change register_bytes_found in this case, and don't
  812.          print a second warning.  */
  813.       goto supply_them;
  814.     }
  815.       regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  816.       p += 2;
  817.     }
  818.  
  819.   if (i != register_bytes_found)
  820.     {
  821.       register_bytes_found = i;
  822. #ifdef REGISTER_BYTES_OK
  823.       if (!REGISTER_BYTES_OK (i))
  824.     warning ("Remote reply is too short: %s", buf);
  825. #endif
  826.     }
  827.  
  828.  supply_them:
  829.   for (i = 0; i < NUM_REGS; i++)
  830.     supply_register (i, ®s[REGISTER_BYTE(i)]);
  831. }
  832.  
  833. /* Prepare to store registers.  Since we may send them all (using a
  834.    'G' request), we have to read out the ones we don't want to change
  835.    first.  */
  836.  
  837. static void 
  838. remote_prepare_to_store ()
  839. {
  840.   /* Make sure the entire registers array is valid.  */
  841.   read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
  842. }
  843.  
  844. /* Store register REGNO, or all registers if REGNO == -1, from the contents
  845.    of REGISTERS.  FIXME: ignores errors.  */
  846.  
  847. static void
  848. remote_store_registers (regno)
  849.      int regno;
  850. {
  851.   char buf[PBUFSIZ];
  852.   int i;
  853.   char *p;
  854.  
  855.   set_thread (inferior_pid, 1);
  856.  
  857.   if (regno >= 0 && stub_supports_P)
  858.     {
  859.       /* Try storing a single register.  */
  860.       char *regp;
  861.  
  862.       sprintf (buf, "P%x=", regno);
  863.       p = buf + strlen (buf);
  864.       regp = ®isters[REGISTER_BYTE (regno)];
  865.       for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
  866.     {
  867.       *p++ = tohex ((regp[i] >> 4) & 0xf);
  868.       *p++ = tohex (regp[i] & 0xf);
  869.     }
  870.       *p = '\0';
  871.       remote_send (buf);
  872.       if (buf[0] != '\0')
  873.     {
  874.       /* The stub understands the 'P' request.  We are done.  */
  875.       return;
  876.     }
  877.  
  878.       /* The stub does not support the 'P' request.  Use 'G' instead,
  879.      and don't try using 'P' in the future (it will just waste our
  880.      time).  */
  881.       stub_supports_P = 0;
  882.     }
  883.  
  884.   buf[0] = 'G';
  885.  
  886.   /* Command describes registers byte by byte,
  887.      each byte encoded as two hex characters.  */
  888.  
  889.   p = buf + 1;
  890.   /* remote_prepare_to_store insures that register_bytes_found gets set.  */
  891.   for (i = 0; i < register_bytes_found; i++)
  892.     {
  893.       *p++ = tohex ((registers[i] >> 4) & 0xf);
  894.       *p++ = tohex (registers[i] & 0xf);
  895.     }
  896.   *p = '\0';
  897.  
  898.   remote_send (buf);
  899. }
  900.  
  901. #if 0
  902.  
  903. /* Use of the data cache is disabled because it loses for looking at
  904.    and changing hardware I/O ports and the like.  Accepting `volatile'
  905.    would perhaps be one way to fix it.  Another idea would be to use the
  906.    executable file for the text segment (for all SEC_CODE sections?
  907.    For all SEC_READONLY sections?).  This has problems if you want to
  908.    actually see what the memory contains (e.g. self-modifying code,
  909.    clobbered memory, user downloaded the wrong thing).  */
  910.  
  911. /* Read a word from remote address ADDR and return it.
  912.    This goes through the data cache.  */
  913.  
  914. static int
  915. remote_fetch_word (addr)
  916.      CORE_ADDR addr;
  917. {
  918.   return dcache_fetch (remote_dcache, addr);
  919. }
  920.  
  921. /* Write a word WORD into remote address ADDR.
  922.    This goes through the data cache.  */
  923.  
  924. static void
  925. remote_store_word (addr, word)
  926.      CORE_ADDR addr;
  927.      int word;
  928. {
  929.   dcache_poke (remote_dcache, addr, word);
  930. }
  931. #endif /* 0 */
  932.  
  933. /* Write memory data directly to the remote machine.
  934.    This does not inform the data cache; the data cache uses this.
  935.    MEMADDR is the address in the remote memory space.
  936.    MYADDR is the address of the buffer in our space.
  937.    LEN is the number of bytes.
  938.  
  939.    Returns number of bytes transferred, or 0 for error.  */
  940.  
  941. static int
  942. remote_write_bytes (memaddr, myaddr, len)
  943.      CORE_ADDR memaddr;
  944.      unsigned char *myaddr;
  945.      int len;
  946. {
  947.   char buf[PBUFSIZ];
  948.   int i;
  949.   char *p;
  950.  
  951.   /* FIXME-32x64: Need a version of print_address_numeric which puts the
  952.      result in a buffer like sprintf.  */
  953.   sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, len);
  954.  
  955.   /* We send target system values byte by byte, in increasing byte addresses,
  956.      each byte encoded as two hex characters.  */
  957.  
  958.   p = buf + strlen (buf);
  959.   for (i = 0; i < len; i++)
  960.     {
  961.       *p++ = tohex ((myaddr[i] >> 4) & 0xf);
  962.       *p++ = tohex (myaddr[i] & 0xf);
  963.     }
  964.   *p = '\0';
  965.  
  966.   putpkt (buf);
  967.   getpkt (buf, 0);
  968.  
  969.   if (buf[0] == 'E')
  970.     {
  971.       /* There is no correspondance between what the remote protocol uses
  972.      for errors and errno codes.  We would like a cleaner way of
  973.      representing errors (big enough to include errno codes, bfd_error
  974.      codes, and others).  But for now just return EIO.  */
  975.       errno = EIO;
  976.       return 0;
  977.     }
  978.   return len;
  979. }
  980.  
  981. /* Read memory data directly from the remote machine.
  982.    This does not use the data cache; the data cache uses this.
  983.    MEMADDR is the address in the remote memory space.
  984.    MYADDR is the address of the buffer in our space.
  985.    LEN is the number of bytes.
  986.  
  987.    Returns number of bytes transferred, or 0 for error.  */
  988.  
  989. static int
  990. remote_read_bytes (memaddr, myaddr, len)
  991.      CORE_ADDR memaddr;
  992.      unsigned char *myaddr;
  993.      int len;
  994. {
  995.   char buf[PBUFSIZ];
  996.   int i;
  997.   char *p;
  998.  
  999.   if (len > PBUFSIZ / 2 - 1)
  1000.     abort ();
  1001.  
  1002.   /* FIXME-32x64: Need a version of print_address_numeric which puts the
  1003.      result in a buffer like sprintf.  */
  1004.   sprintf (buf, "m%lx,%x", (unsigned long) memaddr, len);
  1005.   putpkt (buf);
  1006.   getpkt (buf, 0);
  1007.  
  1008.   if (buf[0] == 'E')
  1009.     {
  1010.       /* There is no correspondance between what the remote protocol uses
  1011.      for errors and errno codes.  We would like a cleaner way of
  1012.      representing errors (big enough to include errno codes, bfd_error
  1013.      codes, and others).  But for now just return EIO.  */
  1014.       errno = EIO;
  1015.       return 0;
  1016.     }
  1017.  
  1018.   /* Reply describes memory byte by byte,
  1019.      each byte encoded as two hex characters.  */
  1020.  
  1021.   p = buf;
  1022.   for (i = 0; i < len; i++)
  1023.     {
  1024.       if (p[0] == 0 || p[1] == 0)
  1025.     /* Reply is short.  This means that we were able to read only part
  1026.        of what we wanted to.  */
  1027.     break;
  1028.       myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  1029.       p += 2;
  1030.     }
  1031.   return i;
  1032. }
  1033.  
  1034. /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
  1035.    to or from debugger address MYADDR.  Write to inferior if SHOULD_WRITE is
  1036.    nonzero.  Returns length of data written or read; 0 for error.  */
  1037.  
  1038. /* ARGSUSED */
  1039. static int
  1040. remote_xfer_memory(memaddr, myaddr, len, should_write, target)
  1041.      CORE_ADDR memaddr;
  1042.      char *myaddr;
  1043.      int len;
  1044.      int should_write;
  1045.      struct target_ops *target;            /* ignored */
  1046. {
  1047.   int xfersize;
  1048.   int bytes_xferred;
  1049.   int total_xferred = 0;
  1050.  
  1051.   set_thread (inferior_pid, 1);
  1052.  
  1053.   while (len > 0)
  1054.     {
  1055.       if (len > MAXBUFBYTES)
  1056.     xfersize = MAXBUFBYTES;
  1057.       else
  1058.     xfersize = len;
  1059.  
  1060.       if (should_write)
  1061.         bytes_xferred = remote_write_bytes (memaddr,
  1062.                         (unsigned char *)myaddr, xfersize);
  1063.       else
  1064.     bytes_xferred = remote_read_bytes (memaddr,
  1065.                        (unsigned char *)myaddr, xfersize);
  1066.  
  1067.       /* If we get an error, we are done xferring.  */
  1068.       if (bytes_xferred == 0)
  1069.     break;
  1070.  
  1071.       memaddr += bytes_xferred;
  1072.       myaddr  += bytes_xferred;
  1073.       len     -= bytes_xferred;
  1074.       total_xferred += bytes_xferred;
  1075.     }
  1076.   return total_xferred;
  1077. }
  1078.  
  1079. #if 0
  1080. /* Enable after 4.12.  */
  1081.  
  1082. void
  1083. remote_search (len, data, mask, startaddr, increment, lorange, hirange
  1084.            addr_found, data_found)
  1085.      int len;
  1086.      char *data;
  1087.      char *mask;
  1088.      CORE_ADDR startaddr;
  1089.      int increment;
  1090.      CORE_ADDR lorange;
  1091.      CORE_ADDR hirange;
  1092.      CORE_ADDR *addr_found;
  1093.      char *data_found;
  1094. {
  1095.   if (increment == -4 && len == 4)
  1096.     {
  1097.       long mask_long, data_long;
  1098.       long data_found_long;
  1099.       CORE_ADDR addr_we_found;
  1100.       char buf[PBUFSIZ];
  1101.       long returned_long[2];
  1102.       char *p;
  1103.  
  1104.       mask_long = extract_unsigned_integer (mask, len);
  1105.       data_long = extract_unsigned_integer (data, len);
  1106.       sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
  1107.       putpkt (buf);
  1108.       getpkt (buf, 0);
  1109.       if (buf[0] == '\0')
  1110.     {
  1111.       /* The stub doesn't support the 't' request.  We might want to
  1112.          remember this fact, but on the other hand the stub could be
  1113.          switched on us.  Maybe we should remember it only until
  1114.          the next "target remote".  */
  1115.       generic_search (len, data, mask, startaddr, increment, lorange,
  1116.               hirange, addr_found, data_found);
  1117.       return;
  1118.     }
  1119.  
  1120.       if (buf[0] == 'E')
  1121.     /* There is no correspondance between what the remote protocol uses
  1122.        for errors and errno codes.  We would like a cleaner way of
  1123.        representing errors (big enough to include errno codes, bfd_error
  1124.        codes, and others).  But for now just use EIO.  */
  1125.     memory_error (EIO, startaddr);
  1126.       p = buf;
  1127.       addr_we_found = 0;
  1128.       while (*p != '\0' && *p != ',')
  1129.     addr_we_found = (addr_we_found << 4) + fromhex (*p++);
  1130.       if (*p == '\0')
  1131.     error ("Protocol error: short return for search");
  1132.  
  1133.       data_found_long = 0;
  1134.       while (*p != '\0' && *p != ',')
  1135.     data_found_long = (data_found_long << 4) + fromhex (*p++);
  1136.       /* Ignore anything after this comma, for future extensions.  */
  1137.  
  1138.       if (addr_we_found < lorange || addr_we_found >= hirange)
  1139.     {
  1140.       *addr_found = 0;
  1141.       return;
  1142.     }
  1143.  
  1144.       *addr_found = addr_we_found;
  1145.       *data_found = store_unsigned_integer (data_we_found, len);
  1146.       return;
  1147.     }
  1148.   generic_search (len, data, mask, startaddr, increment, lorange,
  1149.           hirange, addr_found, data_found);
  1150. }
  1151. #endif /* 0 */
  1152.  
  1153. static void
  1154. remote_files_info (ignore)
  1155.      struct target_ops *ignore;
  1156. {
  1157.   puts_filtered ("Debugging a target over a serial line.\n");
  1158. }
  1159.  
  1160. /* Stuff for dealing with the packets which are part of this protocol.
  1161.    See comment at top of file for details.  */
  1162.  
  1163. /* Read a single character from the remote end, masking it down to 7 bits. */
  1164.  
  1165. static int
  1166. readchar (timeout)
  1167.      int timeout;
  1168. {
  1169.   int ch;
  1170.  
  1171.   ch = SERIAL_READCHAR (remote_desc, timeout);
  1172.  
  1173.   switch (ch)
  1174.     {
  1175.     case SERIAL_EOF:
  1176.       error ("Remote connection closed");
  1177.     case SERIAL_ERROR:
  1178.       perror_with_name ("Remote communication error");
  1179.     case SERIAL_TIMEOUT:
  1180.       return ch;
  1181.     default:
  1182.       return ch & 0x7f;
  1183.     }
  1184. }
  1185.  
  1186. /* Send the command in BUF to the remote machine,
  1187.    and read the reply into BUF.
  1188.    Report an error if we get an error reply.  */
  1189.  
  1190. static void
  1191. remote_send (buf)
  1192.      char *buf;
  1193. {
  1194.  
  1195.   putpkt (buf);
  1196.   getpkt (buf, 0);
  1197.  
  1198.   if (buf[0] == 'E')
  1199.     error ("Remote failure reply: %s", buf);
  1200. }
  1201.  
  1202. /* Send a packet to the remote machine, with error checking.
  1203.    The data of the packet is in BUF.  */
  1204.  
  1205. static int
  1206. putpkt (buf)
  1207.      char *buf;
  1208. {
  1209.   int i;
  1210.   unsigned char csum = 0;
  1211.   char buf2[PBUFSIZ];
  1212.   int cnt = strlen (buf);
  1213.   int ch;
  1214.   char *p;
  1215.  
  1216.   /* Copy the packet into buffer BUF2, encapsulating it
  1217.      and giving it a checksum.  */
  1218.  
  1219.   if (cnt > sizeof(buf2) - 5)        /* Prosanity check */
  1220.     abort();
  1221.  
  1222.   p = buf2;
  1223.   *p++ = '$';
  1224.  
  1225.   for (i = 0; i < cnt; i++)
  1226.     {
  1227.       csum += buf[i];
  1228.       *p++ = buf[i];
  1229.     }
  1230.   *p++ = '#';
  1231.   *p++ = tohex ((csum >> 4) & 0xf);
  1232.   *p++ = tohex (csum & 0xf);
  1233.  
  1234.   /* Send it over and over until we get a positive ack.  */
  1235.  
  1236.   while (1)
  1237.     {
  1238.       int started_error_output = 0;
  1239.  
  1240.       if (remote_debug)
  1241.     {
  1242.       *p = '\0';
  1243.       printf_unfiltered ("Sending packet: %s...", buf2);
  1244.       gdb_flush(gdb_stdout);
  1245.     }
  1246.       if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
  1247.     perror_with_name ("putpkt: write failed");
  1248.  
  1249.       /* read until either a timeout occurs (-2) or '+' is read */
  1250.       while (1)
  1251.     {
  1252.       ch = readchar (remote_timeout);
  1253.  
  1254.       if (remote_debug)
  1255.         {
  1256.           switch (ch)
  1257.         {
  1258.         case '+':
  1259.         case SERIAL_TIMEOUT:
  1260.         case '$':
  1261.           if (started_error_output)
  1262.             {
  1263.               putc_unfiltered ('\n');
  1264.               started_error_output = 0;
  1265.             }
  1266.         }
  1267.         }
  1268.  
  1269.       switch (ch)
  1270.         {
  1271.         case '+':
  1272.           if (remote_debug)
  1273.         printf_unfiltered("Ack\n");
  1274.           return 1;
  1275.         case SERIAL_TIMEOUT:
  1276.           break;        /* Retransmit buffer */
  1277.         case '$':
  1278.           {
  1279.         char junkbuf[PBUFSIZ];
  1280.  
  1281.           /* It's probably an old response, and we're out of sync.  Just
  1282.          gobble up the packet and ignore it.  */
  1283.         getpkt (junkbuf, 0);
  1284.         continue;        /* Now, go look for + */
  1285.           }
  1286.         default:
  1287.           if (remote_debug)
  1288.         {
  1289.           if (!started_error_output)
  1290.             {
  1291.               started_error_output = 1;
  1292.               printf_unfiltered ("putpkt: Junk: ");
  1293.             }
  1294.           putc_unfiltered (ch & 0177);
  1295.         }
  1296.           continue;
  1297.         }
  1298.       break;        /* Here to retransmit */
  1299.     }
  1300.  
  1301. #if 0
  1302.       /* This is wrong.  If doing a long backtrace, the user should be
  1303.      able to get out next time we call QUIT, without anything as violent
  1304.      as interrupt_query.  If we want to provide a way out of here
  1305.      without getting to the next QUIT, it should be based on hitting
  1306.      ^C twice as in remote_wait.  */
  1307.       if (quit_flag)
  1308.     {
  1309.       quit_flag = 0;
  1310.       interrupt_query ();
  1311.     }
  1312. #endif
  1313.     }
  1314. }
  1315.  
  1316. /* Come here after finding the start of the frame.  Collect the rest into BUF,
  1317.    verifying the checksum, length, and handling run-length compression.
  1318.    Returns 0 on any error, 1 on success.  */
  1319.  
  1320. static int
  1321. read_frame (buf)
  1322.      char *buf;
  1323. {
  1324.   unsigned char csum;
  1325.   char *bp;
  1326.   int c;
  1327.  
  1328.   csum = 0;
  1329.   bp = buf;
  1330.  
  1331.   while (1)
  1332.     {
  1333.       c = readchar (remote_timeout);
  1334.  
  1335.       switch (c)
  1336.     {
  1337.     case SERIAL_TIMEOUT:
  1338.       if (remote_debug)
  1339.         puts_filtered ("Timeout in mid-packet, retrying\n");
  1340.       return 0;
  1341.     case '$':
  1342.       if (remote_debug)
  1343.         puts_filtered ("Saw new packet start in middle of old one\n");
  1344.       return 0;        /* Start a new packet, count retries */
  1345.     case '#':
  1346.       {
  1347.         unsigned char pktcsum;
  1348.  
  1349.         *bp = '\000';
  1350.  
  1351.         pktcsum = fromhex (readchar (remote_timeout)) << 4;
  1352.         pktcsum |= fromhex (readchar (remote_timeout));
  1353.  
  1354.         if (csum == pktcsum)
  1355.           return 1;
  1356.  
  1357.         if (remote_debug) 
  1358.           {
  1359.         printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
  1360.                  pktcsum, csum);
  1361.         puts_filtered (buf);
  1362.         puts_filtered ("\n");
  1363.           }
  1364.         return 0;
  1365.       }
  1366.     case '*':        /* Run length encoding */
  1367.       csum += c;
  1368.       c = readchar (remote_timeout);
  1369.       csum += c;
  1370.       c = c - ' ' + 3;    /* Compute repeat count */
  1371.  
  1372.  
  1373.       if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
  1374.         {
  1375.           memset (bp, *(bp - 1), c);
  1376.           bp += c;
  1377.           continue;
  1378.         }
  1379.  
  1380.       *bp = '\0';
  1381.       printf_filtered ("Repeat count %d too large for buffer: ", c);
  1382.       puts_filtered (buf);
  1383.       puts_filtered ("\n");
  1384.       return 0;
  1385.  
  1386.     default:
  1387.       if (bp < buf + PBUFSIZ - 1)
  1388.         {
  1389.           *bp++ = c;
  1390.           csum += c;
  1391.           continue;
  1392.         }
  1393.  
  1394.       *bp = '\0';
  1395.       puts_filtered ("Remote packet too long: ");
  1396.       puts_filtered (buf);
  1397.       puts_filtered ("\n");
  1398.  
  1399.       return 0;
  1400.     }
  1401.     }
  1402. }
  1403.  
  1404. /* Read a packet from the remote machine, with error checking,
  1405.    and store it in BUF.  BUF is expected to be of size PBUFSIZ.
  1406.    If FOREVER, wait forever rather than timing out; this is used
  1407.    while the target is executing user code.  */
  1408.  
  1409. static void
  1410. getpkt (buf, forever)
  1411.      char *buf;
  1412.      int forever;
  1413. {
  1414.   char *bp;
  1415.   int c;
  1416.   int tries;
  1417.   int timeout;
  1418.   int val;
  1419.  
  1420.   if (forever)
  1421.     timeout = -1;
  1422.   else
  1423.     timeout = remote_timeout;
  1424.  
  1425. #define MAX_TRIES 10
  1426.  
  1427.   for (tries = 1; tries <= MAX_TRIES; tries++)
  1428.     {
  1429.       /* This can loop forever if the remote side sends us characters
  1430.      continuously, but if it pauses, we'll get a zero from readchar
  1431.      because of timeout.  Then we'll count that as a retry.  */
  1432.  
  1433.       /* Note that we will only wait forever prior to the start of a packet.
  1434.      After that, we expect characters to arrive at a brisk pace.  They
  1435.      should show up within remote_timeout intervals.  */
  1436.  
  1437.       do
  1438.     {
  1439.       c = readchar (timeout);
  1440.  
  1441.       if (c == SERIAL_TIMEOUT)
  1442.         {
  1443.           if (remote_debug)
  1444.         puts_filtered ("Timed out.\n");
  1445.           goto retry;
  1446.         }
  1447.     }
  1448.       while (c != '$');
  1449.  
  1450.       /* We've found the start of a packet, now collect the data.  */
  1451.  
  1452.       val = read_frame (buf);
  1453.  
  1454.       if (val == 1)
  1455.     {
  1456.       if (remote_debug)
  1457.         fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf);
  1458.       SERIAL_WRITE (remote_desc, "+", 1);
  1459.       return;
  1460.     }
  1461.  
  1462.       /* Try the whole thing again.  */
  1463. retry:
  1464.       SERIAL_WRITE (remote_desc, "-", 1);
  1465.     }
  1466.  
  1467.   /* We have tried hard enough, and just can't receive the packet.  Give up. */
  1468.  
  1469.   printf_unfiltered ("Ignoring packet error, continuing...\n");
  1470.   SERIAL_WRITE (remote_desc, "+", 1);
  1471. }
  1472.  
  1473. static void
  1474. remote_kill ()
  1475. {
  1476.   /* For some mysterious reason, wait_for_inferior calls kill instead of
  1477.      mourn after it gets TARGET_WAITKIND_SIGNALLED.  Work around it.  */
  1478.   if (kill_kludge)
  1479.     {
  1480.       kill_kludge = 0;
  1481.       target_mourn_inferior ();
  1482.       return;
  1483.     }
  1484.  
  1485.   /* Use catch_errors so the user can quit from gdb even when we aren't on
  1486.      speaking terms with the remote system.  */
  1487.   catch_errors (putpkt, "k", "", RETURN_MASK_ERROR);
  1488.  
  1489.   /* Don't wait for it to die.  I'm not really sure it matters whether
  1490.      we do or not.  For the existing stubs, kill is a noop.  */
  1491.   target_mourn_inferior ();
  1492. }
  1493.  
  1494. static void
  1495. remote_mourn ()
  1496. {
  1497.   unpush_target (&remote_ops);
  1498.   generic_mourn_inferior ();
  1499. }
  1500.  
  1501. #ifdef REMOTE_BREAKPOINT
  1502.  
  1503. /* On some machines, e.g. 68k, we may use a different breakpoint instruction
  1504.    than other targets.  */
  1505. static unsigned char break_insn[] = REMOTE_BREAKPOINT;
  1506.  
  1507. /* Check that it fits in BREAKPOINT_MAX bytes.  */
  1508. static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
  1509.  
  1510. #else /* No REMOTE_BREAKPOINT.  */
  1511.  
  1512. /* Same old breakpoint instruction.  This code does nothing different
  1513.    than mem-break.c.  */
  1514. static unsigned char break_insn[] = BREAKPOINT;
  1515.  
  1516. #endif /* No REMOTE_BREAKPOINT.  */
  1517.  
  1518. /* Insert a breakpoint on targets that don't have any better breakpoint
  1519.    support.  We read the contents of the target location and stash it,
  1520.    then overwrite it with a breakpoint instruction.  ADDR is the target
  1521.    location in the target machine.  CONTENTS_CACHE is a pointer to 
  1522.    memory allocated for saving the target contents.  It is guaranteed
  1523.    by the caller to be long enough to save sizeof BREAKPOINT bytes (this
  1524.    is accomplished via BREAKPOINT_MAX).  */
  1525.  
  1526. static int
  1527. remote_insert_breakpoint (addr, contents_cache)
  1528.      CORE_ADDR addr;
  1529.      char *contents_cache;
  1530. {
  1531.   int val;
  1532.  
  1533.   val = target_read_memory (addr, contents_cache, sizeof break_insn);
  1534.  
  1535.   if (val == 0)
  1536.     val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
  1537.  
  1538.   return val;
  1539. }
  1540.  
  1541. static int
  1542. remote_remove_breakpoint (addr, contents_cache)
  1543.      CORE_ADDR addr;
  1544.      char *contents_cache;
  1545. {
  1546.   return target_write_memory (addr, contents_cache, sizeof break_insn);
  1547. }
  1548.  
  1549. /* Define the target subroutine names */
  1550.  
  1551. struct target_ops remote_ops = {
  1552.   "remote",            /* to_shortname */
  1553.   "Remote serial target in gdb-specific protocol",    /* to_longname */
  1554.   "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
  1555. Specify the serial device it is connected to (e.g. /dev/ttya).",  /* to_doc */
  1556.   remote_open,            /* to_open */
  1557.   remote_close,            /* to_close */
  1558.   NULL,                /* to_attach */
  1559.   remote_detach,        /* to_detach */
  1560.   remote_resume,        /* to_resume */
  1561.   remote_wait,            /* to_wait */
  1562.   remote_fetch_registers,    /* to_fetch_registers */
  1563.   remote_store_registers,    /* to_store_registers */
  1564.   remote_prepare_to_store,    /* to_prepare_to_store */
  1565.   remote_xfer_memory,        /* to_xfer_memory */
  1566.   remote_files_info,        /* to_files_info */
  1567.  
  1568.   remote_insert_breakpoint,    /* to_insert_breakpoint */
  1569.   remote_remove_breakpoint,    /* to_remove_breakpoint */
  1570.  
  1571.   NULL,                /* to_terminal_init */
  1572.   NULL,                /* to_terminal_inferior */
  1573.   NULL,                /* to_terminal_ours_for_output */
  1574.   NULL,                /* to_terminal_ours */
  1575.   NULL,                /* to_terminal_info */
  1576.   remote_kill,            /* to_kill */
  1577.   generic_load,            /* to_load */
  1578.   NULL,                /* to_lookup_symbol */
  1579.   NULL,                /* to_create_inferior */
  1580.   remote_mourn,            /* to_mourn_inferior */
  1581.   0,                /* to_can_run */
  1582.   0,                /* to_notice_signals */
  1583.   0,                /* to_stop */
  1584.   process_stratum,        /* to_stratum */
  1585.   NULL,                /* to_next */
  1586.   1,                /* to_has_all_memory */
  1587.   1,                /* to_has_memory */
  1588.   1,                /* to_has_stack */
  1589.   1,                /* to_has_registers */
  1590.   1,                /* to_has_execution */
  1591.   NULL,                /* sections */
  1592.   NULL,                /* sections_end */
  1593.   OPS_MAGIC            /* to_magic */
  1594. };
  1595.  
  1596. void
  1597. _initialize_remote ()
  1598. {
  1599.   add_target (&remote_ops);
  1600. }
  1601.