[gnu.gdb.bug] hp9k320 support and remote.c fixes

glenne%hplsla@HPLABS.HP.COM (Glenn Engel) (02/16/89)

Please, please, please include these patches.  I've submitted them a number of
times over the last 9 months only to have them ignored.  Is anyone home ?

Included are patches to hp9k320-dep.c, m-hp9k320.h and remote.c to support
the attach command on the hp9000/300.  Patches apply to gdb3.1.

The patches to remote.c are generic bug fixes for any machine.  I have 
included the changes submitted by Brad Parker for timeouts and have added
fixes for machines which support TERMIO but not siginterrupt.

--
 |  Glenn R. Engel
 |  Hewlett-Packard 
 |  (206) 335-2066
 |  glenne%hplsla@hplabs.hp.com

*** hp9k320-dep.c.orig	Wed Feb 15 11:09:23 1989
--- hp9k320-dep.c	Wed Feb 15 10:49:06 1989
***************
*** 41,46
  #include <sys/stat.h>
  
  extern int errno;
  
  /* This function simply calls ptrace with the given arguments.  
     It exists so that all calls to ptrace are isolated in this 

--- 41,47 -----
  #include <sys/stat.h>
  
  extern int errno;
+ extern int attach_flag;
  
  /* This function simply calls ptrace with the given arguments.  
     It exists so that all calls to ptrace are isolated in this 
***************
*** 94,99
  	perror_with_name ("ptrace");
      }
  }
  
  #define INFERIOR_AR0(u)							\
    ((ptrace								\

--- 95,131 -----
  	perror_with_name ("ptrace");
      }
  }
+ 
+ #ifdef ATTACH_DETACH
+ 
+ /* Start debugging the process whose number is PID.  */
+ 
+ attach (pid)
+      int pid;
+ {
+   errno = 0;
+   /* ptrace (PTRACE_ATTACH, pid, 0, 0); */  /* how do we do this ? */
+   if (errno)
+     perror_with_name ("ptrace");
+   attach_flag = 1;
+   return pid;
+ }
+ 
+ /* Stop debugging the process whose number is PID
+    and continue it with signal number SIGNAL.
+    SIGNAL = 0 means just continue it.  */
+ 
+ void
+ detach (signal)
+      int signal;
+ {
+   errno = 0;
+   /* ptrace (PTRACE_DETACH, inferior_pid, 1, signal); */ /* how do we do this ? */
+   if (errno)
+     perror_with_name ("ptrace");
+   attach_flag = 0;
+ }
+ #endif /* ATTACH_DETACH */
  
  #define INFERIOR_AR0(u)							\
    ((ptrace								\
***************
*** 191,202
    struct user u;
    register int regno;
    register unsigned int ar0_offset;
! 
!   ar0_offset = (INFERIOR_AR0 (u));
!   for (regno = 0; (regno < FP0_REGNUM); regno++)
!     fetch_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
!   for (; (regno < NUM_REGS); regno++)
!     fetch_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
  }
  
  /* Store our register values back into the inferior.

--- 223,239 -----
    struct user u;
    register int regno;
    register unsigned int ar0_offset;
!   extern char registers[];
!   if (remote_debugging)
!     remote_fetch_registers (registers);
!   else
!     {
!       ar0_offset = (INFERIOR_AR0 (u));
!       for (regno = 0; (regno < FP0_REGNUM); regno++)
!         fetch_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
!       for (; (regno < NUM_REGS); regno++)
!         fetch_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
!     }
  }
  
  /* Store our register values back into the inferior.
***************
*** 208,215
  {
    struct user u;
    register unsigned int ar0_offset;
! 
!   if (regno >= FP0_REGNUM)
      {
        store_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
        return;

--- 245,254 -----
  {
    struct user u;
    register unsigned int ar0_offset;
!   extern char registers[];
!   if (remote_debugging)
!     remote_store_registers (registers);
!   else
      {
        if (regno >= FP0_REGNUM)
          {
***************
*** 211,219
  
    if (regno >= FP0_REGNUM)
      {
!       store_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
!       return;
!     }
  
    ar0_offset = (INFERIOR_AR0 (u));
    if (regno >= 0)

--- 250,260 -----
      remote_store_registers (registers);
    else
      {
!       if (regno >= FP0_REGNUM)
!         {
!           store_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
!           return;
!         }
  
        ar0_offset = (INFERIOR_AR0 (u));
        if (regno >= 0)
***************
*** 215,225
        return;
      }
  
!   ar0_offset = (INFERIOR_AR0 (u));
!   if (regno >= 0)
!     {
!       store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
!       return;
      }
  
    for (regno = 0; (regno < FP0_REGNUM); regno++)

--- 256,272 -----
            return;
          }
  
!       ar0_offset = (INFERIOR_AR0 (u));
!       if (regno >= 0)
!         {
!           store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
!           return;
!         }
!     
!       for (regno = 0; (regno < FP0_REGNUM); regno++)
!         store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
!       for (; (regno < NUM_REGS); regno++)
!         store_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
      }
    return;
  }
***************
*** 221,231
        store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
        return;
      }
- 
-   for (regno = 0; (regno < FP0_REGNUM); regno++)
-     store_inferior_register (regno, (REGISTER_ADDR (ar0_offset, regno)));
-   for (; (regno < NUM_REGS); regno++)
-     store_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
    return;
  }
  

--- 268,273 -----
        for (; (regno < NUM_REGS); regno++)
          store_inferior_register (regno, (FP_REGISTER_ADDR (u, regno)));
      }
    return;
  }
  
*** m-hp9k320.h.orig	Wed Feb 15 11:09:30 1989
--- m-hp9k320.h	Wed Feb 15 10:49:18 1989
***************
*** 248,253
       ? (&u.u_pcb.pcb_mc68881[FMC68881_R0 + (((regno) - FP0_REGNUM) * 3)]) \
       : (&u.u_pcb.pcb_mc68881[FMC68881_C + ((regno) - FPC_REGNUM)])))	\
     - ((char *) (& u)))
  
  /* It is safe to look for symsegs on a Sun, because Sun's ld
     does not screw up with random garbage at end of file.  */

--- 252,261 -----
       ? (&u.u_pcb.pcb_mc68881[FMC68881_R0 + (((regno) - FP0_REGNUM) * 3)]) \
       : (&u.u_pcb.pcb_mc68881[FMC68881_C + ((regno) - FPC_REGNUM)])))	\
     - ((char *) (& u)))
+ 
+ /* Do implement the attach and detach commands.  */
+ 
+ #define ATTACH_DETACH
  
  /* It is safe to look for symsegs on a Sun, because Sun's ld
     does not screw up with random garbage at end of file.  */
*** remote.c.orig	Wed Feb 15 11:09:41 1989
--- remote.c	Wed Feb 15 12:38:26 1989
***************
*** 93,99
  #define TERMINAL struct sgttyb
  #endif
  
! int kiodebug;
  
  int icache;
  

--- 93,100 -----
  #define TERMINAL struct sgttyb
  #endif
  
! static int kiodebug = 0;
! static int timeout = 5;
  
  int icache;
  
***************
*** 108,113
  static void dcache_flush ();
  
  
  /* Open a connection to a remote debugger.
     NAME is the filename used for communication.  */
  

--- 109,126 -----
  static void dcache_flush ();
  
  
+ /* called when SIGALRM signal sent due to alarm() timeout */
+ #ifndef HAVE_TERMIO
+ void
+ remote_timer()
+ {
+ 	if (kiodebug)
+ 		printf("[remote_timer]\n");
+ 
+ 	alarm(timeout);
+ }
+ #endif
+ 
  /* Open a connection to a remote debugger.
     NAME is the filename used for communication.  */
  
***************
*** 127,133
  
    ioctl (remote_desc, TIOCGETP, &sg);
  #ifdef HAVE_TERMIO
!   sg.c_lflag &= ~ICANON;
  #else
    sg.sg_flags = RAW;
  #endif

--- 140,149 -----
  
    ioctl (remote_desc, TIOCGETP, &sg);
  #ifdef HAVE_TERMIO
!   sg.c_cc[VMIN] = 0; /* read with timeout */
!   sg.c_cc[VTIME] = timeout*10;
!   sg.c_lflag &= ~(ICANON | ECHO);
!   
  #else
    sg.sg_flags = RAW;
  #endif
***************
*** 136,141
    if (from_tty)
      printf ("Remote debugging using %s\n", name);
    remote_debugging = 1;
  }
  
  /* Convert hex digit A to a number.  */

--- 152,167 -----
    if (from_tty)
      printf ("Remote debugging using %s\n", name);
    remote_debugging = 1;
+ 
+ #ifndef HAVE_TERMIO
+ /* cause SIGALRM's to make reads fail */
+   if (siginterrupt(SIGALRM, 1) != 0)
+ 	perror("[remote_open] siginterrupt");
+ 
+   /* set up read timeout timer */
+   if (signal(SIGALRM, remote_timer) == (void (*))-1)
+ 	perror("[remote_open] signal");
+ #endif
  }
  
  /* Convert hex digit A to a number.  */
***************
*** 186,192
  remote_wait (status)
       WAITTYPE *status;
  {
!   char buf[PBUFSIZ];
  
    WSETEXIT ((*status), 0);
    getpkt (buf);

--- 212,218 -----
  remote_wait (status)
       WAITTYPE *status;
  {
!   unsigned char buf[PBUFSIZ];
  
    WSETEXIT ((*status), 0);
    getpkt (buf);
***************
*** 244,249
        *p++ = tohex ((regs[i] >> 4) & 0xf);
        *p++ = tohex (regs[i] & 0xf);
      }
  
    remote_send (buf);
  }

--- 270,276 -----
        *p++ = tohex ((regs[i] >> 4) & 0xf);
        *p++ = tohex (regs[i] & 0xf);
      }
+   *p = '\0';
  
    remote_send (buf);
  }
***************
*** 310,315
        *p++ = tohex ((myaddr[i] >> 4) & 0xf);
        *p++ = tohex (myaddr[i] & 0xf);
      }
  
    remote_send (buf);
  }

--- 337,343 -----
        *p++ = tohex ((myaddr[i] >> 4) & 0xf);
        *p++ = tohex (myaddr[i] & 0xf);
      }
+   *p = '\0';
  
    remote_send (buf);
  }
***************
*** 370,375
  
  */
  
  /* Send the command in BUF to the remote machine,
     and read the reply into BUF.
     Report an error if we get an error reply.  */

--- 398,425 -----
  
  */
  
+ static int
+ readchar ()
+ {
+   char buf[1];
+ 
+   buf[0] = '\0';
+ #ifdef HAVE_TERMIO
+   /* termio does the timeout for us. */
+   read (remote_desc, buf, 1);
+ #else
+   alarm(timeout);
+   read (remote_desc, buf, 1);
+   alarm(0);
+ #endif
+ /*
+   if (kiodebug > 1)
+ 	printf("[readchar] got '%c' (0x%x)\n", buf[0] & 0x7f, buf[0] & 0x7f);
+ */
+ 
+   return buf[0] & 0x7f;
+ }
+ 
  /* Send the command in BUF to the remote machine,
     and read the reply into BUF.
     Report an error if we get an error reply.  */
***************
*** 394,400
       char *buf;
  {
    int i;
!   char csum = 0;
    char buf2[500];
    char buf3[1];
    int cnt = strlen (buf);

--- 444,450 -----
       char *buf;
  {
    int i;
!   unsigned char csum = 0;
    char buf2[500];
    char buf3[1];
    int cnt = strlen (buf);
***************
*** 400,408
    int cnt = strlen (buf);
    char *p;
  
-   if (kiodebug)
-     fprintf (stderr, "Sending packet: %s\n", buf);
- 
    /* Copy the packet into buffer BUF2, encapsulating it
       and giving it a checksum.  */
  

--- 450,455 -----
    int cnt = strlen (buf);
    char *p;
  
    /* Copy the packet into buffer BUF2, encapsulating it
       and giving it a checksum.  */
  
***************
*** 421,426
    /* Send it over and over until we get a positive ack.  */
  
    do {
      write (remote_desc, buf2, p - buf2);
      read (remote_desc, buf3, 1);
    } while (buf3[0] != '+');

--- 468,477 -----
    /* Send it over and over until we get a positive ack.  */
  
    do {
+     if (kiodebug) {
+ 	  *p = '\0';
+       fprintf (stderr, "Sending packet: %s (%s)\n", buf2, buf);
+     }
      write (remote_desc, buf2, p - buf2);
    } while (readchar() != '+');
  }
***************
*** 422,429
  
    do {
      write (remote_desc, buf2, p - buf2);
!     read (remote_desc, buf3, 1);
!   } while (buf3[0] != '+');
  }
  
  static int

--- 473,479 -----
        fprintf (stderr, "Sending packet: %s (%s)\n", buf2, buf);
      }
      write (remote_desc, buf2, p - buf2);
!   } while (readchar() != '+');
  }
  
  /* Read a packet from the remote machine, with error checking,
***************
*** 426,439
    } while (buf3[0] != '+');
  }
  
- static int
- readchar ()
- {
-   char buf[1];
-   while (read (remote_desc, buf, 1) != 1) ;
-   return buf[0] & 0x7f;
- }
- 
  /* Read a packet from the remote machine, with error checking,
     and store it in BUF.  */
  

--- 476,481 -----
    } while (readchar() != '+');
  }
  
  /* Read a packet from the remote machine, with error checking,
     and store it in BUF.  */
  
***************
*** 443,449
  {
    char *bp;
    unsigned char csum;
!   unsigned int c, c1, c2;
    extern kiodebug;
  
    while (1)

--- 485,492 -----
  {
    char *bp;
    unsigned char csum;
!   int c;
!   unsigned char c1, c2;
    extern kiodebug;
  
    while (1)
***************
*** 466,472
  
        c1 = fromhex (readchar ());
        c2 = fromhex (readchar ());
!       if (csum == (c1 << 4) + c2)
  	break;
        printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
  	      (c1 << 4) + c2, csum, buf);

--- 509,515 -----
  
        c1 = fromhex (readchar ());
        c2 = fromhex (readchar ());
!       if ((csum & 0xff) == (c1 << 4) + c2)
  	break;
        printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
  	      (c1 << 4) + c2, csum & 0xff, buf);
***************
*** 469,475
        if (csum == (c1 << 4) + c2)
  	break;
        printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
! 	      (c1 << 4) + c2, csum, buf);
        write (remote_desc, "-", 1);
      }
  

--- 512,519 -----
        if ((csum & 0xff) == (c1 << 4) + c2)
  	break;
        printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
! 	      (c1 << 4) + c2, csum & 0xff, buf);
! 
        write (remote_desc, "-", 1);
      }