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