home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / gdb-4.12-src.lha / src / amiga / gdb-4.12 / gdb / remote-utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  14.7 KB  |  693 lines

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