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-utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-02  |  15.4 KB  |  711 lines

  1. /* Generic support for remote debugging interfaces.
  2.  
  3.    Copyright 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*  This file actually contains two distinct logical "packages".  They
  22.     are packaged together in this one file because they are typically
  23.     used together.
  24.  
  25.     The first package is an addition to the serial package.  The
  26.     addition provides reading and writing with debugging output and
  27.     timeouts based on user settable variables.  These routines are
  28.     intended to support serial port based remote backends.  These
  29.     functions are prefixed with sr_.
  30.  
  31.     The second package is a collection of more or less generic
  32.     functions for use by remote backends.  They support user settable
  33.     variables for debugging, retries, and the like.  
  34.  
  35.    Todo:
  36.  
  37.    * a pass through mode a la kermit or telnet.
  38.    * autobaud.
  39.    * ask remote to change his baud rate.
  40.    */
  41.  
  42. #include <ctype.h>
  43.  
  44. #include "defs.h"
  45. #include <string.h>
  46. #include "gdbcmd.h"
  47. #include "target.h"
  48. #include "serial.h"
  49. #include "gdbcore.h" /* for exec_bfd */
  50. #include "inferior.h" /* for generic_mourn_inferior */
  51. #include "remote-utils.h"
  52.  
  53. struct _sr_settings sr_settings = {
  54.   4, /* timeout:
  55.     remote-hms.c had 2
  56.     remote-bug.c had "with a timeout of 2, we time out waiting for
  57.     the prompt after an s-record dump."
  58.  
  59.     remote.c had (2): This was 5 seconds, which is a long time to
  60.     sit and wait. Unless this is going though some terminal server
  61.     or multiplexer or other form of hairy serial connection, I
  62.     would think 2 seconds would be plenty.
  63. */
  64.  
  65.   10, /* retries */
  66.   NULL,    /* device */
  67.   NULL,    /* descriptor */
  68. };
  69.  
  70. struct gr_settings *gr_settings = NULL;
  71.  
  72. static void
  73. usage(proto, junk)
  74.      char *proto;
  75.      char *junk;
  76. {
  77.   if (junk != NULL)
  78.     fprintf_unfiltered(gdb_stderr, "Unrecognized arguments: `%s'.\n", junk);
  79.  
  80.   error ("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\
  81. where DEVICE is the name of a device or HOST:PORT", proto, proto);
  82.  
  83.   return;
  84. }
  85.  
  86. #define CHECKDONE(p, q) \
  87. { \
  88.   if (q == p) \
  89.     { \
  90.       if (*p == '\0') \
  91.     return; \
  92.       else \
  93.     usage(proto, p); \
  94.     } \
  95. }
  96.  
  97. void
  98. sr_scan_args(proto, args)
  99.      char *proto;
  100.      char *args;
  101. {
  102.   int n;
  103.   char *p, *q;
  104.  
  105.   /* if no args, then nothing to do. */
  106.   if (args == NULL || *args == '\0')
  107.     return;
  108.  
  109.   /* scan off white space.  */
  110.   for (p = args; isspace(*p); ++p) ;;
  111.  
  112.   /* find end of device name.  */
  113.   for (q = p; *q != '\0' && !isspace(*q); ++q) ;;
  114.  
  115.   /* check for missing or empty device name.  */
  116.   CHECKDONE(p, q);
  117.   sr_set_device(savestring(p, q - p));
  118.  
  119.   /* look for baud rate.  */
  120.   n = strtol(q, &p, 10);
  121.  
  122.   /* check for missing or empty baud rate.  */
  123.   CHECKDONE(p, q);
  124.   baud_rate = n;
  125.  
  126.   /* look for debug value.  */
  127.   n = strtol(p, &q, 10);
  128.  
  129.   /* check for missing or empty debug value.  */
  130.   CHECKDONE(p, q);
  131.   sr_set_debug(n);
  132.  
  133.   /* scan off remaining white space.  */
  134.   for (p = q; isspace(*p); ++p) ;;
  135.  
  136.   /* if not end of string, then there's unrecognized junk. */
  137.   if (*p != '\0')
  138.     usage(proto, p);
  139.  
  140.   return;
  141. }
  142.  
  143. void
  144. gr_generic_checkin()
  145. {
  146.   sr_write_cr("");
  147.   gr_expect_prompt();
  148. }
  149.  
  150. void
  151. gr_open(args, from_tty, gr)
  152.      char *args;
  153.      int from_tty;
  154.      struct gr_settings *gr;
  155. {
  156.   target_preopen(from_tty);
  157.   sr_scan_args(gr->ops->to_shortname, args);
  158.   unpush_target(gr->ops);
  159.  
  160.   gr_settings = gr;
  161.  
  162.   gr_set_dcache(dcache_init(gr->readfunc, gr->writefunc));
  163.  
  164.   if (sr_get_desc() != NULL)
  165.     gr_close (0);
  166.  
  167.   /* If no args are specified, then we use the device specified by a
  168.      previous command or "set remotedevice".  But if there is no
  169.      device, better stop now, not dump core.  */
  170.  
  171.   if (sr_get_device () == NULL)
  172.     usage (gr->ops->to_shortname, NULL);
  173.  
  174.   sr_set_desc(SERIAL_OPEN (sr_get_device()));
  175.   if (!sr_get_desc())
  176.     perror_with_name((char *) sr_get_device());
  177.  
  178.   if (baud_rate != -1)
  179.     {
  180.       if (SERIAL_SETBAUDRATE(sr_get_desc(), baud_rate) != 0)
  181.     {
  182.       SERIAL_CLOSE(sr_get_desc());
  183.       perror_with_name(sr_get_device());
  184.     }
  185.     }
  186.  
  187.   SERIAL_RAW (sr_get_desc());
  188.  
  189.   /* If there is something sitting in the buffer we might take it as a
  190.      response to a command, which would be bad.  */
  191.   SERIAL_FLUSH_INPUT (sr_get_desc ());
  192.  
  193.   /* default retries */
  194.   if (sr_get_retries() == 0)
  195.     sr_set_retries(1);
  196.  
  197.   /* default clear breakpoint function */
  198.   if (gr_settings->clear_all_breakpoints == NULL)
  199.     gr_settings->clear_all_breakpoints = remove_breakpoints;
  200.  
  201.   if (from_tty)
  202.     {
  203.       printf_filtered ("Remote debugging using `%s'", sr_get_device ());
  204.       if (baud_rate != -1)
  205.     printf_filtered (" at baud rate of %d",
  206.              baud_rate);
  207.       printf_filtered ("\n");
  208.     }
  209.  
  210.   push_target(gr->ops);
  211.   gr_checkin();
  212.   gr_clear_all_breakpoints ();
  213.   return;
  214. }
  215.  
  216. /* Read a character from the remote system masking it down to 7 bits
  217.    and doing all the fancy timeout stuff.  */
  218.  
  219. int
  220. sr_readchar ()
  221. {
  222.   int buf;
  223.  
  224.   buf = SERIAL_READCHAR (sr_get_desc(), sr_get_timeout());
  225.  
  226.   if (buf == SERIAL_TIMEOUT)
  227.     error ("Timeout reading from remote system.");
  228.  
  229.   if (sr_get_debug() > 0)
  230.     printf_unfiltered ("%c", buf);
  231.  
  232.   return buf & 0x7f;
  233. }
  234.  
  235. int
  236. sr_pollchar()
  237. {
  238.   int buf;
  239.  
  240.   buf = SERIAL_READCHAR (sr_get_desc(), 0);
  241.   if (buf == SERIAL_TIMEOUT)
  242.     buf = 0;
  243.   if (sr_get_debug() > 0)
  244.     if (buf)
  245.       printf_unfiltered ("%c", buf);
  246.     else
  247.       printf_unfiltered ("<empty character poll>");
  248.  
  249.   return buf & 0x7f;
  250. }
  251.  
  252. /* Keep discarding input from the remote system, until STRING is found.
  253.    Let the user break out immediately.  */
  254. void
  255. sr_expect (string)
  256.      char *string;
  257. {
  258.   char *p = string;
  259.  
  260.   immediate_quit = 1;
  261.   while (1)
  262.     {
  263.       if (sr_readchar () == *p)
  264.     {
  265.       p++;
  266.       if (*p == '\0')
  267.         {
  268.           immediate_quit = 0;
  269.           return;
  270.         }
  271.     }
  272.       else
  273.     p = string;
  274.     }
  275. }
  276.  
  277. void
  278. sr_write (a, l)
  279.      char *a;
  280.      int l;
  281. {
  282.   int i;
  283.  
  284.   if (SERIAL_WRITE (sr_get_desc(), a, l) != 0)
  285.     perror_with_name ("sr_write: Error writing to remote");
  286.  
  287.   if (sr_get_debug() > 0)
  288.     for (i = 0; i < l; i++)
  289.       printf_unfiltered ("%c", a[i]);
  290.  
  291.   return;
  292. }
  293.  
  294. void
  295. sr_write_cr (s)
  296.      char *s;
  297. {
  298.   sr_write (s, strlen (s));
  299.   sr_write ("\r", 1);
  300.   return;
  301. }
  302.  
  303. int
  304. sr_timed_read (buf, n)
  305.      char *buf;
  306.      int n;
  307. {
  308.   int i;
  309.   char c;
  310.  
  311.   i = 0;
  312.   while (i < n)
  313.     {
  314.       c = sr_readchar ();
  315.  
  316.       if (c == 0)
  317.     return i;
  318.       buf[i] = c;
  319.       i++;
  320.  
  321.     }
  322.   return i;
  323. }
  324.  
  325. /* Get a hex digit from the remote system & return its value. If
  326.    ignore_space is nonzero, ignore spaces (not newline, tab, etc).  */
  327.  
  328. int
  329. sr_get_hex_digit (ignore_space)
  330.      int ignore_space;
  331. {
  332.   int ch;
  333.  
  334.   while (1)
  335.     {
  336.       ch = sr_readchar ();
  337.       if (ch >= '0' && ch <= '9')
  338.     return ch - '0';
  339.       else if (ch >= 'A' && ch <= 'F')
  340.     return ch - 'A' + 10;
  341.       else if (ch >= 'a' && ch <= 'f')
  342.     return ch - 'a' + 10;
  343.       else if (ch != ' ' || !ignore_space)
  344.     {
  345.       gr_expect_prompt ();
  346.       error ("Invalid hex digit from remote system.");
  347.     }
  348.     }
  349. }
  350.  
  351. /* Get a byte from the remote and put it in *BYT.  Accept any number
  352.    leading spaces.  */
  353. void
  354. sr_get_hex_byte (byt)
  355.      char *byt;
  356. {
  357.   int val;
  358.  
  359.   val = sr_get_hex_digit (1) << 4;
  360.   val |= sr_get_hex_digit (0);
  361.   *byt = val;
  362. }
  363.  
  364. /* Read a 32-bit hex word from the remote, preceded by a space  */
  365. long
  366. sr_get_hex_word ()
  367. {
  368.   long val;
  369.   int j;
  370.  
  371.   val = 0;
  372.   for (j = 0; j < 8; j++)
  373.     val = (val << 4) + sr_get_hex_digit (j == 0);
  374.   return val;
  375. }
  376.  
  377. /* Put a command string, in args, out to the remote.  The remote is assumed to
  378.    be in raw mode, all writing/reading done through desc.
  379.    Ouput from the remote is placed on the users terminal until the
  380.    prompt from the remote is seen.
  381.    FIXME: Can't handle commands that take input.  */
  382.  
  383. void
  384. sr_com (args, fromtty)
  385.      char *args;
  386.      int fromtty;
  387. {
  388.   sr_check_open ();
  389.  
  390.   if (!args)
  391.     return;
  392.  
  393.   /* Clear all input so only command relative output is displayed */
  394.  
  395.   sr_write_cr (args);
  396.   sr_write ("\030", 1);
  397.   registers_changed ();
  398.   gr_expect_prompt ();
  399. }
  400.  
  401. void
  402. gr_close(quitting)
  403.      int quitting;
  404. {
  405.   gr_clear_all_breakpoints();
  406.  
  407.   if (sr_is_open())
  408.     {
  409.       SERIAL_CLOSE (sr_get_desc());
  410.       sr_set_desc(NULL);
  411.     }
  412.  
  413.   return;
  414. }
  415.  
  416. /* gr_detach()
  417.    takes a program previously attached to and detaches it.
  418.    We better not have left any breakpoints
  419.    in the program or it'll die when it hits one.
  420.    Close the open connection to the remote debugger.
  421.    Use this when you want to detach and do something else
  422.    with your gdb.  */
  423.  
  424. void
  425. gr_detach(args, from_tty)
  426.      char *args;
  427.      int from_tty;
  428. {
  429.   if (args)
  430.     error ("Argument given to \"detach\" when remotely debugging.");
  431.   
  432.   if (sr_is_open())
  433.     gr_clear_all_breakpoints ();
  434.  
  435.   pop_target ();
  436.   if (from_tty)
  437.     puts_filtered ("Ending remote debugging.\n");
  438.  
  439.   return;
  440. }  
  441.  
  442. void
  443. gr_files_info (ops)
  444.      struct target_ops *ops;
  445. {
  446. #ifdef __GO32__
  447.   printf_filtered ("\tAttached to DOS asynctsr\n");
  448. #else
  449.   printf_filtered ("\tAttached to %s", sr_get_device());
  450.   if (baud_rate != -1)
  451.     printf_filtered ("at %d baud", baud_rate);
  452.   printf_filtered ("\n");
  453. #endif
  454.  
  455.   if (exec_bfd)
  456.     {
  457.       printf_filtered ("\tand running program %s\n",
  458.                bfd_get_filename (exec_bfd));
  459.     }
  460.   printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname);
  461. }
  462.  
  463. void
  464. gr_mourn ()
  465. {
  466.   gr_clear_all_breakpoints ();
  467.   unpush_target (gr_get_ops());
  468.   generic_mourn_inferior ();
  469. }
  470.  
  471. void
  472. gr_kill ()
  473. {
  474.   return;
  475. }
  476.  
  477. /* This is called not only when we first attach, but also when the
  478.    user types "run" after having attached.  */
  479. void
  480. gr_create_inferior (execfile, args, env)
  481.      char *execfile;
  482.      char *args;
  483.      char **env;
  484. {
  485.   int entry_pt;
  486.  
  487.   if (args && *args)
  488.     error ("Can't pass arguments to remote process.");
  489.  
  490.   if (execfile == 0 || exec_bfd == 0)
  491.     error ("No exec file specified");
  492.  
  493.   entry_pt = (int) bfd_get_start_address (exec_bfd);
  494.   sr_check_open ();
  495.  
  496.   gr_kill ();
  497.   gr_clear_all_breakpoints ();
  498.  
  499.   init_wait_for_inferior ();
  500.   gr_checkin();
  501.  
  502.   insert_breakpoints ();    /* Needed to get correct instruction in cache */
  503.   proceed (entry_pt, -1, 0);
  504. }
  505.  
  506. /* Given a null terminated list of strings LIST, read the input until we find one of
  507.    them.  Return the index of the string found or -1 on error.  '?' means match
  508.    any single character. Note that with the algorithm we use, the initial
  509.    character of the string cannot recur in the string, or we will not find some
  510.    cases of the string in the input.  If PASSTHROUGH is non-zero, then
  511.    pass non-matching data on.  */
  512.  
  513. int
  514. gr_multi_scan (list, passthrough)
  515.      char *list[];
  516.      int passthrough;
  517. {
  518.   char *swallowed = NULL; /* holding area */
  519.   char *swallowed_p = swallowed; /* Current position in swallowed.  */
  520.   int ch;
  521.   int ch_handled;
  522.   int i;
  523.   int string_count;
  524.   int max_length;
  525.   char **plist;
  526.  
  527.   /* Look through the strings.  Count them.  Find the largest one so we can
  528.      allocate a holding area.  */
  529.  
  530.   for (max_length = string_count = i = 0;
  531.        list[i] != NULL;
  532.        ++i, ++string_count)
  533.     {
  534.       int length = strlen(list[i]);
  535.  
  536.       if (length > max_length)
  537.     max_length = length;
  538.     }
  539.  
  540.   /* if we have no strings, then something is wrong. */
  541.   if (string_count == 0)
  542.     return(-1);
  543.  
  544.   /* otherwise, we will need a holding area big enough to hold almost two
  545.      copies of our largest string.  */
  546.   swallowed_p = swallowed = alloca(max_length << 1);
  547.  
  548.   /* and a list of pointers to current scan points. */
  549.   plist = (char **) alloca (string_count * sizeof(*plist));
  550.  
  551.   /* and initialize */
  552.   for (i = 0; i < string_count; ++i)
  553.     plist[i] = list[i];
  554.  
  555.   for (ch = sr_readchar(); /* loop forever */ ; ch = sr_readchar())
  556.     {
  557.       QUIT; /* Let user quit and leave process running */
  558.       ch_handled = 0;
  559.  
  560.       for (i = 0; i < string_count; ++i)
  561.     {
  562.       if (ch == *plist[i] || *plist[i] == '?')
  563.         {
  564.           ++plist[i];
  565.           if (*plist[i] == '\0')
  566.         return(i);
  567.  
  568.           if (!ch_handled)
  569.         *swallowed_p++ = ch;
  570.  
  571.           ch_handled = 1;
  572.         }
  573.       else
  574.         plist[i] = list[i];
  575.     }
  576.  
  577.       if (!ch_handled)
  578.     {
  579.       char *p;
  580.  
  581.       /* Print out any characters which have been swallowed.  */
  582.       if (passthrough)
  583.         {
  584.           for (p = swallowed; p < swallowed_p; ++p)
  585.         fputc_unfiltered (*p, gdb_stdout);
  586.  
  587.           fputc_unfiltered (ch, gdb_stdout);
  588.         }
  589.  
  590.       swallowed_p = swallowed;
  591.     }
  592.     }
  593. #if 0
  594.   /* Never reached.  */
  595.   return(-1);
  596. #endif
  597. }
  598.  
  599. /* Get ready to modify the registers array.  On machines which store
  600.    individual registers, this doesn't need to do anything.  On machines
  601.    which store all the registers in one fell swoop, this makes sure
  602.    that registers contains all the registers from the program being
  603.    debugged.  */
  604.  
  605. void
  606. gr_prepare_to_store ()
  607. {
  608.   /* Do nothing, since we assume we can store individual regs */
  609. }
  610.  
  611. /* Read a word from remote address ADDR and return it.
  612.  * This goes through the data cache.
  613.  */
  614. int
  615. gr_fetch_word (addr)
  616.      CORE_ADDR addr;
  617. {
  618.   return dcache_fetch (gr_get_dcache(), addr);
  619. }
  620.  
  621. /* Write a word WORD into remote address ADDR.
  622.    This goes through the data cache.  */
  623.  
  624. void
  625. gr_store_word (addr, word)
  626.      CORE_ADDR addr;
  627.      int word;
  628. {
  629.   dcache_poke (gr_get_dcache(), addr, word);
  630. }
  631.  
  632. /* general purpose load a file specified on the command line
  633.    into target memory. */
  634.  
  635. void
  636. gr_load_image (args, fromtty)
  637.      char *args;
  638.      int fromtty;
  639. {
  640.   bfd *abfd;
  641.  
  642.   asection *s;
  643.   struct cleanup *old_cleanups;
  644.   int delta = 4096;
  645.   char *buffer = xmalloc (delta);
  646.  
  647.   abfd = bfd_openr (args, (char *) 0);
  648.  
  649.   if (!abfd)
  650.     /* FIXME: should be using bfd_errmsg, not assuming it was
  651.        bfd_error_system_call.  */
  652.     perror_with_name (args);
  653.  
  654.   /* FIXME: should be checking for errors from bfd_close (for one thing,
  655.      on error it does not free all the storage associated with the
  656.      bfd).  */
  657.   old_cleanups = make_cleanup (bfd_close, abfd);
  658.  
  659.   QUIT;
  660.  
  661.   if (!bfd_check_format (abfd, bfd_object))
  662.     error ("It doesn't seem to be an object file.\n");
  663.  
  664.   for (s = abfd->sections; s && !quit_flag; s = s->next)
  665.     {
  666.       if (bfd_get_section_flags (abfd, s) & SEC_LOAD)
  667.     {
  668.       int i;
  669.       printf_filtered ("%s\t: 0x%4x .. 0x%4x  ",
  670.                s->name, s->vma, s->vma + s->_raw_size);
  671.       fflush (stdout);
  672.       for (i = 0; i < s->_raw_size && !quit_flag; i += delta)
  673.         {
  674.           int sub_delta = delta;
  675.           if (sub_delta > s->_raw_size - i)
  676.         sub_delta = s->_raw_size - i;
  677.           QUIT;
  678.           bfd_get_section_contents (abfd, s, buffer, i, sub_delta);
  679.           target_write_memory (s->vma + i, buffer, sub_delta);
  680.           printf_filtered ("*");
  681.           fflush (stdout);
  682.         }
  683.       printf_filtered ("\n");
  684.     }
  685.     }
  686.  
  687.   free (buffer);
  688.   write_pc (bfd_get_start_address (abfd));
  689.   if (!bfd_close (abfd))
  690.     warning ("cannot close \"%s\": %s",
  691.          args, bfd_errmsg (bfd_get_error ()));
  692.   discard_cleanups (old_cleanups);
  693. }
  694.  
  695.  
  696. void
  697. _initialize_sr_support ()
  698. {
  699. /* FIXME-now: if target is open... */
  700.   add_show_from_set (add_set_cmd ("remotedevice", no_class,
  701.                   var_filename, (char *)&sr_settings.device,
  702.                   "Set device for remote serial I/O.\n\
  703. This device is used as the serial port when debugging using remote\n\
  704. targets.", &setlist),
  705.              &showlist);
  706.  
  707.   add_com ("remote <command>", class_obscure, sr_com,
  708.        "Send a command to the remote monitor.");
  709.  
  710. }
  711.