home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / gdbserver / remote-utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-09  |  9.3 KB  |  476 lines

  1. /* Remote utility routines for the remote server for GDB.
  2.    Copyright (C) 1986, 1989, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "server.h"
  21. #include <stdio.h>
  22. #include <sgtty.h>
  23. #include <sys/file.h>
  24. #include <netinet/in.h>
  25. #include <sys/socket.h>
  26. #include <netdb.h>
  27. #include <netinet/tcp.h>
  28. #include <sys/ioctl.h>
  29. #include <signal.h>
  30.  
  31. static int kiodebug = 0;
  32. static int remote_desc;
  33.  
  34. /* Open a connection to a remote debugger.
  35.    NAME is the filename used for communication.  */
  36.  
  37. void
  38. remote_open (name)
  39.      char *name;
  40. {
  41.   struct sgttyb sg;
  42.  
  43.   if (!strchr (name, ':'))
  44.     {
  45.       remote_desc = open (name, O_RDWR);
  46.       if (remote_desc < 0)
  47.     perror_with_name ("Could not open remote device");
  48.  
  49.       ioctl (remote_desc, TIOCGETP, &sg);
  50.       sg.sg_flags = RAW;
  51.       ioctl (remote_desc, TIOCSETP, &sg);
  52.     }
  53.   else
  54.     {
  55.       char *port_str;
  56.       int port;
  57.       struct sockaddr_in sockaddr;
  58.       int tmp;
  59.       struct protoent *protoent;
  60.       int tmp_desc;
  61.  
  62.       port_str = strchr (name, ':');
  63.  
  64.       port = atoi (port_str + 1);
  65.  
  66.       tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
  67.       if (tmp_desc < 0)
  68.     perror_with_name ("Can't open socket");
  69.  
  70.       /* Allow rapid reuse of this port. */
  71.       tmp = 1;
  72.       setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp,
  73.           sizeof(tmp));
  74.  
  75.       sockaddr.sin_family = PF_INET;
  76.       sockaddr.sin_port = htons(port);
  77.       sockaddr.sin_addr.s_addr = INADDR_ANY;
  78.  
  79.       if (bind (tmp_desc, (struct sockaddr *)&sockaddr, sizeof (sockaddr))
  80.       || listen (tmp_desc, 1))
  81.     perror_with_name ("Can't bind address");
  82.  
  83.       tmp = sizeof (sockaddr);
  84.       remote_desc = accept (tmp_desc, (struct sockaddr *)&sockaddr, &tmp);
  85.       if (remote_desc == -1)
  86.     perror_with_name ("Accept failed");
  87.  
  88.       protoent = getprotobyname ("tcp");
  89.       if (!protoent)
  90.     perror_with_name ("getprotobyname");
  91.  
  92.       /* Enable TCP keep alive process. */
  93.       tmp = 1;
  94.       setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp));
  95.  
  96.       /* Tell TCP not to delay small packets.  This greatly speeds up
  97.      interactive response. */
  98.       tmp = 1;
  99.       setsockopt (remote_desc, protoent->p_proto, TCP_NODELAY,
  100.           (char *)&tmp, sizeof(tmp));
  101.  
  102.       close (tmp_desc);        /* No longer need this */
  103.  
  104.       signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
  105.                     exits when the remote side dies.  */
  106.     }
  107.  
  108.   fcntl (remote_desc, F_SETFL, FASYNC);
  109.  
  110.   fprintf (stderr, "Remote debugging using %s\n", name);
  111. }
  112.  
  113. void
  114. remote_close()
  115. {
  116.   close (remote_desc);
  117. }
  118.  
  119. /* Convert hex digit A to a number.  */
  120.  
  121. static int
  122. fromhex (a)
  123.      int a;
  124. {
  125.   if (a >= '0' && a <= '9')
  126.     return a - '0';
  127.   else if (a >= 'a' && a <= 'f')
  128.     return a - 'a' + 10;
  129.   else
  130.     error ("Reply contains invalid hex digit");
  131. }
  132.  
  133. /* Convert number NIB to a hex digit.  */
  134.  
  135. static int
  136. tohex (nib)
  137.      int nib;
  138. {
  139.   if (nib < 10)
  140.     return '0' + nib;
  141.   else
  142.     return 'a' + nib - 10;
  143. }
  144.  
  145. /* Send a packet to the remote machine, with error checking.
  146.    The data of the packet is in BUF.  Returns >= 0 on success, -1 otherwise. */
  147.  
  148. int
  149. putpkt (buf)
  150.      char *buf;
  151. {
  152.   int i;
  153.   unsigned char csum = 0;
  154.   char buf2[2000];
  155.   char buf3[1];
  156.   int cnt = strlen (buf);
  157.   char *p;
  158.  
  159.   /* Copy the packet into buffer BUF2, encapsulating it
  160.      and giving it a checksum.  */
  161.  
  162.   p = buf2;
  163.   *p++ = '$';
  164.  
  165.   for (i = 0; i < cnt; i++)
  166.     {
  167.       csum += buf[i];
  168.       *p++ = buf[i];
  169.     }
  170.   *p++ = '#';
  171.   *p++ = tohex ((csum >> 4) & 0xf);
  172.   *p++ = tohex (csum & 0xf);
  173.  
  174.   /* Send it over and over until we get a positive ack.  */
  175.  
  176.   do
  177.     {
  178.       int cc;
  179.  
  180.       if (write (remote_desc, buf2, p - buf2) != p - buf2)
  181.     {
  182.       perror ("putpkt(write)");
  183.       return -1;
  184.     }
  185.  
  186.       cc = read (remote_desc, buf3, 1);
  187.       if (cc <= 0)
  188.     {
  189.       if (cc == 0)
  190.         fprintf (stderr, "putpkt(read): Got EOF\n");
  191.       else
  192.         perror ("putpkt(read)");
  193.  
  194.       return -1;
  195.     }
  196.     }
  197.   while (buf3[0] != '+');
  198.  
  199.   return 1;            /* Success! */
  200. }
  201.  
  202. /* Come here when we get an input interrupt from the remote side.  This
  203.    interrupt should only be active while we are waiting for the child to do
  204.    something.  About the only thing that should come through is a ^C, which
  205.    will cause us to send a SIGINT to the child.  */
  206.  
  207. static void
  208. input_interrupt()
  209. {
  210.   int cc;
  211.   char c;
  212.  
  213.   cc = read (remote_desc, &c, 1);
  214.  
  215.   if (cc != 1 || c != '\003')
  216.     {
  217.       fprintf(stderr, "input_interrupt, cc = %d c = %d\n", cc, c);
  218.       return;
  219.     }
  220.  
  221.   kill (inferior_pid, SIGINT);
  222. }
  223.  
  224. void
  225. enable_async_io()
  226. {
  227.   signal (SIGIO, input_interrupt);
  228. }
  229.  
  230. void
  231. disable_async_io()
  232. {
  233.   signal (SIGIO, SIG_IGN);
  234. }
  235.  
  236. /* Returns next char from remote GDB.  -1 if error.  */
  237.  
  238. static int
  239. readchar ()
  240. {
  241.   static char buf[BUFSIZ];
  242.   static int bufcnt = 0;
  243.   static char *bufp;
  244.  
  245.   if (bufcnt-- > 0)
  246.     return *bufp++ & 0x7f;
  247.  
  248.   bufcnt = read (remote_desc, buf, sizeof (buf));
  249.  
  250.   if (bufcnt <= 0)
  251.     {
  252.       if (bufcnt == 0)
  253.     fprintf (stderr, "readchar: Got EOF\n");
  254.       else
  255.     perror ("readchar");
  256.  
  257.       return -1;
  258.     }
  259.  
  260.   bufp = buf;
  261.   bufcnt--;
  262.   return *bufp++ & 0x7f;
  263. }
  264.  
  265. /* Read a packet from the remote machine, with error checking,
  266.    and store it in BUF.  Returns length of packet, or negative if error. */
  267.  
  268. int
  269. getpkt (buf)
  270.      char *buf;
  271. {
  272.   char *bp;
  273.   unsigned char csum, c1, c2;
  274.   int c;
  275.  
  276.   while (1)
  277.     {
  278.       csum = 0;
  279.  
  280.       while (1)
  281.     {
  282.       c = readchar ();
  283.       if (c == '$')
  284.         break;
  285.       if (c < 0)
  286.         return -1;
  287.     }
  288.  
  289.       bp = buf;
  290.       while (1)
  291.     {
  292.       c = readchar ();
  293.       if (c < 0)
  294.         return -1;
  295.       if (c == '#')
  296.         break;
  297.       *bp++ = c;
  298.       csum += c;
  299.     }
  300.       *bp = 0;
  301.  
  302.       c1 = fromhex (readchar ());
  303.       c2 = fromhex (readchar ());
  304.       if (csum == (c1 << 4) + c2)
  305.     break;
  306.  
  307.       fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
  308.            (c1 << 4) + c2, csum, buf);
  309.       write (remote_desc, "-", 1);
  310.     }
  311.  
  312.   write (remote_desc, "+", 1);
  313.   return bp - buf;
  314. }
  315.  
  316. void
  317. write_ok (buf)
  318.      char *buf;
  319. {
  320.   buf[0] = 'O';
  321.   buf[1] = 'K';
  322.   buf[2] = '\0';
  323. }
  324.  
  325. void
  326. write_enn (buf)
  327.      char *buf;
  328. {
  329.   buf[0] = 'E';
  330.   buf[1] = 'N';
  331.   buf[2] = 'N';
  332.   buf[3] = '\0';
  333. }
  334.  
  335. void
  336. convert_int_to_ascii (from, to, n)
  337.      char *from, *to;
  338.      int n;
  339. {
  340.   int nib;
  341.   char ch;
  342.   while (n--)
  343.     {
  344.       ch = *from++;
  345.       nib = ((ch & 0xf0) >> 4) & 0x0f;
  346.       *to++ = tohex (nib);
  347.       nib = ch & 0x0f;
  348.       *to++ = tohex (nib);
  349.     }
  350.   *to++ = 0;
  351. }
  352.  
  353.  
  354. void
  355. convert_ascii_to_int (from, to, n)
  356.      char *from, *to;
  357.      int n;
  358. {
  359.   int nib1, nib2;
  360.   while (n--)
  361.     {
  362.       nib1 = fromhex (*from++);
  363.       nib2 = fromhex (*from++);
  364.       *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
  365.     }
  366. }
  367.  
  368. static char *
  369. outreg(regno, buf)
  370.      int regno;
  371.      char *buf;
  372. {
  373.   extern char registers[];
  374.  
  375.   *buf++ = tohex (regno >> 4);
  376.   *buf++ = tohex (regno & 0xf);
  377.   *buf++ = ':';
  378.   convert_int_to_ascii (®isters[REGISTER_BYTE (regno)], buf, 4);
  379.   buf += 8;
  380.   *buf++ = ';';
  381.  
  382.   return buf;
  383. }
  384.  
  385. void
  386. prepare_resume_reply (buf, status, signal)
  387.      char *buf;
  388.      char status;
  389.      unsigned char signal;
  390. {
  391.   int nib;
  392.   char ch;
  393.  
  394.   *buf++ = status;
  395.  
  396.   nib = ((signal & 0xf0) >> 4);
  397.   *buf++ = tohex (nib);
  398.   nib = signal & 0x0f;
  399.   *buf++ = tohex (nib);
  400.  
  401.   if (status == 'T')
  402.     {
  403.       buf = outreg (PC_REGNUM, buf);
  404.       buf = outreg (FP_REGNUM, buf);
  405.       buf = outreg (SP_REGNUM, buf);
  406. #ifdef NPC_REGNUM
  407.       buf = outreg (NPC_REGNUM, buf);
  408. #endif
  409. #ifdef O7_REGNUM
  410.       buf = outreg (O7_REGNUM, buf);
  411. #endif
  412.  
  413.       /* If the debugger hasn't used any thread features, don't burden it with
  414.      threads.  If we didn't check this, GDB 4.13 and older would choke.  */
  415.       if (cont_thread != 0)
  416.     {
  417.       if (old_thread_from_wait != thread_from_wait)
  418.         {
  419.           sprintf (buf, "thread:%x;", thread_from_wait);
  420.           buf += strlen (buf);
  421.           old_thread_from_wait = thread_from_wait;
  422.         }
  423.     }
  424.     }
  425.   /* For W and X, we're done.  */
  426.   *buf++ = 0;
  427. }
  428.  
  429. void
  430. decode_m_packet (from, mem_addr_ptr, len_ptr)
  431.      char *from;
  432.      unsigned int *mem_addr_ptr, *len_ptr;
  433. {
  434.   int i = 0, j = 0;
  435.   char ch;
  436.   *mem_addr_ptr = *len_ptr = 0;
  437.  
  438.   while ((ch = from[i++]) != ',')
  439.     {
  440.       *mem_addr_ptr = *mem_addr_ptr << 4;
  441.       *mem_addr_ptr |= fromhex (ch) & 0x0f;
  442.     }
  443.  
  444.   for (j = 0; j < 4; j++)
  445.     {
  446.       if ((ch = from[i++]) == 0)
  447.     break;
  448.       *len_ptr = *len_ptr << 4;
  449.       *len_ptr |= fromhex (ch) & 0x0f;
  450.     }
  451. }
  452.  
  453. void
  454. decode_M_packet (from, mem_addr_ptr, len_ptr, to)
  455.      char *from, *to;
  456.      unsigned int *mem_addr_ptr, *len_ptr;
  457. {
  458.   int i = 0, j = 0;
  459.   char ch;
  460.   *mem_addr_ptr = *len_ptr = 0;
  461.  
  462.   while ((ch = from[i++]) != ',')
  463.     {
  464.       *mem_addr_ptr = *mem_addr_ptr << 4;
  465.       *mem_addr_ptr |= fromhex (ch) & 0x0f;
  466.     }
  467.  
  468.   while ((ch = from[i++]) != ':')
  469.     {
  470.       *len_ptr = *len_ptr << 4;
  471.       *len_ptr |= fromhex (ch) & 0x0f;
  472.     }
  473.  
  474.   convert_ascii_to_int (&from[i++], to, *len_ptr);
  475. }
  476.