bin/52190: [Patch] decode more syscalls in truss

Dan Nelson dnelson at allantgroup.com
Tue Jun 3 16:00:34 PDT 2003


The following reply was made to PR bin/52190; it has been noted by GNATS.

From: Dan Nelson <dnelson at allantgroup.com>
To: FreeBSD-gnats-submit at FreeBSD.org
Cc:  
Subject: Re: bin/52190: [Patch] decode more syscalls in truss
Date: Tue, 3 Jun 2003 17:54:06 -0500

 Updated patch that decodes struct pollfd and fd_set, fixes a memory
 leak in get_struct(), and avoids a unnecessary abort when a NULL
 sockaddr is passed to recvfrom().
 
 Index: syscall.h
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/truss/syscall.h,v
 retrieving revision 1.10
 diff -p -u -r1.10 syscall.h
 --- syscall.h	4 Aug 2002 02:24:21 -0000	1.10
 +++ syscall.h	2 Jun 2003 19:15:54 -0000
 @@ -9,9 +9,16 @@
   *	write() arguments as such, even though they may *not* be
   *	printable data.
   * Ptr -- pointer to some specific structure.  Just print as hex for now.
 - * Quad -- a double-word value.  e.g., lseek(int, offset_t, int)
   * Stat -- a pointer to a stat buffer.  Currently unused.
   * Ioctl -- an ioctl command.  Woefully limited.
 + * Quad -- a double-word value.  e.g., lseek(int, offset_t, int)
 + * Signal -- a signal number.  Prints the signal name (SIGxxx)
 + * Sockaddr -- a pointer to a struct sockaddr.  Prints symbolic AF, and IP:Port
 + * StringArray -- a pointer to an array of string pointers.
 + * Timespec -- a pointer to a struct timespec.  Prints both elements.
 + * Timeval -- a pointer to a struct timeval.  Prints both elements.
 + * Pollfd -- a pointer to an array of struct pollfd.  Prints .fd and .events.
 + * Fd_set -- a pointer to an array of fd_set.  Prints the fds that are set.
   *
   * In addition, the pointer types (String, Ptr) may have OUT masked in --
   * this means that the data is set on *return* from the system call -- or
 @@ -22,7 +29,7 @@
   */
  
  enum Argtype { None = 1, Hex, Octal, Int, String, Ptr, Stat, Ioctl, Quad,
 -	Signal, Sockaddr, StringArray };
 +	Signal, Sockaddr, StringArray, Timespec, Timeval, Pollfd, Fd_set };
  
  #define ARG_MASK	0xff
  #define OUT	0x100
 Index: syscalls.c
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/truss/syscalls.c,v
 retrieving revision 1.28
 diff -p -u -r1.28 syscalls.c
 --- syscalls.c	15 Apr 2003 06:12:12 -0000	1.28
 +++ syscalls.c	3 Jun 2003 22:45:57 -0000
 @@ -48,6 +48,7 @@ static const char rcsid[] =
  
  #include <ctype.h>
  #include <err.h>
 +#include <poll.h>
  #include <signal.h>
  #include <stdio.h>
  #include <stdlib.h>
 @@ -131,9 +132,21 @@ struct syscall syscalls[] = {
  	{ "kldnext", 0, 1, { { Int, 0 }}},
  	{ "kldstat", 0, 2, { { Int, 0 }, { Ptr, 1 }}},
  	{ "kldfirstmod", 0, 1, { { Int, 0 }}},
 +	{ "nanosleep", 0, 1, { { Timespec, 0 }}},
 +	{ "select", 1, 5, { { Int, 0 }, { Fd_set, 1 }, { Fd_set, 2 }, { Fd_set, 3 }, { Timeval, 4 }}},
 +	{ "poll", 1, 3, { { Pollfd, 0 }, { Int, 1 }, { Int, 2 }}},
 +	{ "gettimeofday", 1, 2, { { Timeval | OUT, 0 }, { Ptr, 1 }}},
 +	{ "clock_gettime", 1, 2, { { Int, 0 }, { Timeval | OUT, 1 }}},
 +	{ "recvfrom", 1, 6, { { Int, 0 }, { Ptr | OUT, 1 }, { Int, 2 }, { Int, 3 }, { Sockaddr | OUT, 4}, {Ptr | OUT, 5}}},
  	{ 0, 0, 0, { { 0, 0 }}},
  };
  
 +const char* const pollflags[] = {
 +    "IN", "PRI", "OUT", "ERR",              /* 0x000n */
 +    "HUP", "NVAL", "RDNORM", "RDBAND",      /* 0x00n0 */
 +    "WRBAND", "INIGNEOF", NULL              /* 0x0n00 */
 +};
 +
  /*
   * If/when the list gets big, it might be desirable to do it
   * as a hash table or binary search.
 @@ -159,21 +172,8 @@ get_syscall(const char *name) {
  
  static int
  get_struct(int procfd, void *offset, void *buf, int len) {
 -	char *pos;
 -	FILE *p;
 -	int c, fd;
 -
 -	if ((fd = dup(procfd)) == -1)
 -		err(1, "dup");
 -	if ((p = fdopen(fd, "r")) == NULL)
 -		err(1, "fdopen");
 -	fseeko(p, (uintptr_t)offset, SEEK_SET);
 -	for (pos = (char *)buf; len--; pos++) {
 -		if ((c = fgetc(p)) == EOF)
 +	if (pread(procfd, buf, len, (uintptr_t)offset) != len)
  			return -1;
 -		*pos = c;
 -	}
 -	fclose(p);
  	return 0;
  }
  
 @@ -333,6 +333,127 @@ print_arg(int fd, struct syscall_args *s
        }
      }
      break;
 +  case Timespec:
 +    {
 +      struct timespec ts;
 +      if (get_struct(fd, (void *)args[sc->offset], &ts, sizeof(struct timespec)) != -1)
 +        asprintf(&tmp, "{%d %d}", ts.tv_sec, ts.tv_nsec);
 +      else
 +        asprintf(&tmp, "0x%lx", args[sc->offset]);
 +    }
 +    break;
 +  case Timeval:
 +    {
 +      struct timeval tv;
 +      if (get_struct(fd, (void *)args[sc->offset], &tv, sizeof(struct timeval)) != -1)
 +        asprintf(&tmp, "{%d %d}", tv.tv_sec, tv.tv_usec);
 +      else
 +        asprintf(&tmp, "0x%lx", args[sc->offset]);
 +    }
 +    break;
 +  case Pollfd:
 +    {
 +      /* XXX: A Pollfd argument expects the /next/ syscall argument to be
 +         the number of fds in the array, which is what poll() does.
 +      */
 +      struct pollfd *pfd;
 +      int numfds = args[sc->offset+1];
 +      int bytes = sizeof(struct pollfd) * numfds;
 +      pfd = malloc(bytes);
 +      if (!pfd)
 +        err(1, "Cannot malloc %d bytes for pollfd array", bytes);
 +      if (get_struct(fd, (void *)args[sc->offset], pfd, bytes) != -1)
 +      {
 +        int i;
 +        int used = 0;
 +        int tmpsize = 1024;
 +        char *tmp2;
 +
 +        /*
 +           Pollfd's output can vary wildly in size, so just allocate a 1k
 +           buffer and double its size whenever it gets close to filling up.
 +        */
 +        tmp = malloc(tmpsize);
 +        if (!tmp)
 +          err(1, "Cannot realloc %d bytes for poll output", tmpsize);
 +
 +        used += sprintf(tmp + used, "{");
 +        for (i = 0; i < numfds; i++)
 +        {
 +          used += sprintf(tmp + used, "%s%d", i>0 ? " " : "", pfd[i].fd);
 +          if (pfd[i].events)
 +          {
 +            int j;
 +            for (j = 0; pollflags[j]; j++)
 +              if (pfd[i].events & 1<<j)
 +                used += sprintf(tmp + used, "|%s", pollflags[j]);
 +          }
 +          
 +          if (used + 100 > tmpsize)
 +          {
 +            tmpsize = tmpsize * 2;
 +            tmp = realloc(tmp, tmpsize);
 +            if (!tmp)
 +              err(1, "Cannot realloc %d bytes for poll output", tmpsize);
 +          }
 +        }
 +        used += sprintf(tmp + used, "}");
 +      }
 +      else
 +        asprintf(&tmp, "0x%lx", args[sc->offset]);
 +      free(pfd);
 +    }
 +    break;
 +  case Fd_set:
 +    {
 +      /* XXX: A Fd_set argument expects the /first/ syscall argument to be
 +         the number of fds in the array, which is what select() does.
 +      */
 +      fd_set *fds;
 +      int numfds = args[0];
 +      int bytes = _howmany(numfds, _NFDBITS) * _NFDBITS;
 +      fds = malloc(bytes);
 +      if (!fds)
 +        err(1, "Cannot malloc %d bytes for fd_set array", bytes);
 +      if (get_struct(fd, (void *)args[sc->offset], fds, bytes) != -1)
 +      {
 +        char *tmp2;
 +        int tmpsize = 1024;
 +        int used = 0;
 +        int i;
 +
 +        /*
 +           FD_set's output can vary wildly in size, so just allocate a 1k
 +           buffer and double its size whenever it gets close to filling up.
 +        */
 +
 +        tmp = malloc(tmpsize);
 +        if (!tmp)
 +          err(1, "Cannot realloc %d bytes for fd_set output", tmpsize);
 +
 +        used += sprintf(tmp + used, "{");
 +        for (i = 0; i < numfds; i++)
 +        {
 +          if (FD_ISSET(i, fds))
 +            used += sprintf(tmp + used, "%d ", i);
 +          
 +          if (used + 100 > tmpsize)
 +          {
 +            tmpsize = tmpsize * 2;
 +            tmp = realloc(tmp, tmpsize);
 +            if (!tmp)
 +              err(1, "Cannot realloc %d bytes for fd_set output", tmpsize);
 +          }
 +        }
 +        if (tmp[used-1] == ' ')
 +        	used--;
 +        used += sprintf(tmp + used, "}");
 +      }
 +      else
 +        asprintf(&tmp, "0x%lx", args[sc->offset]);
 +      free(fds);
 +    }
 +    break;
    case Signal:
      {
        long sig;
 @@ -360,6 +481,12 @@ print_arg(int fd, struct syscall_args *s
        char *p;
        u_char *q;
        int i;
 +
 +      if (args[sc->offset] == NULL)
 +      {
 +        asprintf(&tmp, "0x0");
 +        break;
 +      }
  
        /* yuck: get ss_len */
        if (get_struct(fd, (void *)args[sc->offset], (void *)&ss,


More information about the freebsd-bugs mailing list