This is the mail archive of the gdb-patches@sourceware.cygnus.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[PATCH]: Clean-up of procfs debug trace.



This cleans up a little private debugging API that I use for
working with /proc, and adds a little more diagnostics.
All of the ugliness is now hidden in proc-api.c and
proc-utils.h.

2000-05-05  Michael Snyder  <msnyder@seadog.cygnus.com>

        * procfs.c: Cleanup of procfs tracing.  Move defines and 
        prototypes to proc-utils.h
        * proc-utils.h: Define tracing macros.  Declare trace functions.
        * proc-api.c: Make procfs tracing a runtime option.
        (prepare_to_trace): New function, abstracted out of several
        places.  Open a trace file if one is required.
        (ioctl_with_trace, write_with_trace, open_with_trace,
        close_with_trace, wait_with_trace, lseek_with_trace):
        Report errno if an error occurs in a system call.
        (write_with_trace): Make 2nd arg void *, to agree with write.

Index: proc-utils.h
===================================================================
RCS file: /cvs/src/src/gdb/proc-utils.h,v
retrieving revision 1.1.1.1
diff -p -r1.1.1.1 proc-utils.h
*** proc-utils.h	2000/01/25 02:39:49	1.1.1.1
--- proc-utils.h	2000/05/05 20:52:44
*************** along with this program; if not, write t
*** 18,29 ****
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  
  
  extern void 
  proc_prettyprint_why (unsigned long why, unsigned long what, int verbose);
  
! extern void proc_prettyprint_syscalls (sysset_t *sysset, int verbose);
  
! extern void proc_prettyprint_syscall (int num, int verbose);
  
  extern void proc_prettyprint_flags (unsigned long flags, int verbose);
  
--- 18,35 ----
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  
  
+ /*
+  * Pretty-print functions for /proc data 
+  */
+ 
  extern void 
  proc_prettyprint_why (unsigned long why, unsigned long what, int verbose);
  
! extern void 
! proc_prettyprint_syscalls (sysset_t *sysset, int verbose);
  
! extern void 
! proc_prettyprint_syscall (int num, int verbose);
  
  extern void proc_prettyprint_flags (unsigned long flags, int verbose);
  
*************** extern void
*** 43,52 ****
  proc_prettyfprint_flags (FILE *file, unsigned long flags, int verbose);
  
  extern void
! proc_prettyfprint_why (FILE *file, unsigned long why, unsigned long what, int verbose);
  
  extern void
  proc_prettyfprint_fault (FILE *file, int faultno, int verbose);
  
  extern void
  proc_prettyfprint_syscalls (FILE *file, sysset_t *sysset, int verbose);
--- 49,94 ----
  proc_prettyfprint_flags (FILE *file, unsigned long flags, int verbose);
  
  extern void
! proc_prettyfprint_why (FILE *file, unsigned long why, 
! 		       unsigned long what, int verbose);
  
  extern void
  proc_prettyfprint_fault (FILE *file, int faultno, int verbose);
  
  extern void
  proc_prettyfprint_syscalls (FILE *file, sysset_t *sysset, int verbose);
+ 
+ extern void
+ proc_prettyfprint_status (long, int, int, int);
+ 
+ /*
+  * Trace functions for /proc api.
+  */
+ 
+ extern  int   write_with_trace (int, void *, size_t, char *, int);
+ extern  off_t lseek_with_trace (int, off_t,  int,    char *, int);
+ extern  int   ioctl_with_trace (int, long, void *, char *, int);
+ extern  pid_t wait_with_trace  (int *, char *, int);
+ extern  int   open_with_trace  (char *, int, char *, int);
+ extern  int   close_with_trace (int, char *, int);
+ extern  void  procfs_note      (char *, char *, int);
+ 
+ #ifdef PROCFS_TRACE
+ /*
+  * Debugging code:
+  *
+  * These macros allow me to trace the system calls that we make
+  * to control the child process.  This is quite handy for comparing
+  * with the older version of procfs.
+  */
+ 
+ #define write(X,Y,Z)   write_with_trace (X, Y, Z, __FILE__, __LINE__)
+ #define lseek(X,Y,Z)   lseek_with_trace (X, Y, Z, __FILE__, __LINE__)
+ #define ioctl(X,Y,Z)   ioctl_with_trace (X, Y, Z, __FILE__, __LINE__)
+ #define open(X,Y)      open_with_trace  (X, Y,    __FILE__, __LINE__)
+ #define close(X)       close_with_trace (X,       __FILE__, __LINE__)
+ #define wait(X)        wait_with_trace  (X,       __FILE__, __LINE__)
+ #define PROCFS_NOTE(X) procfs_note      (X,       __FILE__, __LINE__)
+ #define PROC_PRETTYFPRINT_STATUS(X,Y,Z,T) \
+      proc_prettyfprint_status (X, Y, Z, T)
+ #endif
Index: procfs.c
===================================================================
RCS file: /cvs/src/src/gdb/procfs.c,v
retrieving revision 1.8
diff -p -r1.8 procfs.c
*** procfs.c	2000/05/05 18:29:33	1.8
--- procfs.c	2000/05/05 20:52:44
*************** Inc., 59 Temple Place - Suite 330, Bosto
*** 38,45 ****
  #include <signal.h>
  #include <ctype.h>
  
- #include "proc-utils.h"
- 
  /* 
   * PROCFS.C
   *
--- 38,43 ----
*************** Inc., 59 Temple Place - Suite 330, Bosto
*** 85,90 ****
--- 83,95 ----
  #include <unistd.h>	/* for "X_OK" */
  #include "gdb_stat.h"	/* for struct stat */
  
+ /* Note: procfs-utils.h must be included after the above system header
+    files, because it redefines various system calls using macros.
+    This may be incompatible with the prototype declarations.  */
+ 
+ #define PROCFS_TRACE
+ #include "proc-utils.h"
+ 
  /* =================== TARGET_OPS "MODULE" =================== */
  
  /*
*************** init_procfs_ops ()
*** 154,161 ****
    procfs_ops.to_thread_alive       = procfs_thread_alive;
    procfs_ops.to_pid_to_str         = procfs_pid_to_str;
  
!   procfs_ops.to_has_all_memory    = 1;
!   procfs_ops.to_has_memory        = 1;
    procfs_ops.to_has_execution      = 1;
    procfs_ops.to_has_stack          = 1;
    procfs_ops.to_has_registers      = 1;
--- 159,166 ----
    procfs_ops.to_thread_alive       = procfs_thread_alive;
    procfs_ops.to_pid_to_str         = procfs_pid_to_str;
  
!   procfs_ops.to_has_all_memory     = 1;
!   procfs_ops.to_has_memory         = 1;
    procfs_ops.to_has_execution      = 1;
    procfs_ops.to_has_stack          = 1;
    procfs_ops.to_has_registers      = 1;
*************** init_procfs_ops ()
*** 167,202 ****
  /* =================== END, TARGET_OPS "MODULE" =================== */
  
  /*
-  * Temporary debugging code:
-  *
-  * These macros allow me to trace the system calls that we make
-  * to control the child process.  This is quite handy for comparing
-  * with the older version of procfs.
-  */
- 
- #ifdef TRACE_PROCFS
- #ifdef NEW_PROC_API
- extern  int   write_with_trace PARAMS ((int, void *, size_t, char *, int));
- extern  off_t lseek_with_trace PARAMS ((int, off_t,  int,    char *, int));
- #define write(X,Y,Z)   write_with_trace (X, Y, Z, __FILE__, __LINE__)
- #define lseek(X,Y,Z)   lseek_with_trace (X, Y, Z, __FILE__, __LINE__)
- #else
- extern  int ioctl_with_trace PARAMS ((int, long, void *, char *, int));
- #define ioctl(X,Y,Z)   ioctl_with_trace (X, Y, Z, __FILE__, __LINE__)
- #endif
- #define open(X,Y)      open_with_trace  (X, Y,    __FILE__, __LINE__)
- #define close(X)       close_with_trace (X,       __FILE__, __LINE__)
- #define wait(X)        wait_with_trace  (X,       __FILE__, __LINE__)
- #define PROCFS_NOTE(X) procfs_note      (X,       __FILE__, __LINE__)
- #define PROC_PRETTYFPRINT_STATUS(X,Y,Z,T) \
- proc_prettyfprint_status (X, Y, Z, T)
- #else
- #define PROCFS_NOTE(X)
- #define PROC_PRETTYFPRINT_STATUS(X,Y,Z,T)
- #endif
- 
- 
- /*
   * World Unification:
   *
   * Put any typedefs, defines etc. here that are required for
--- 172,177 ----
*************** unconditionally_kill_inferior (pi)
*** 4460,4466 ****
    }
  #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
    if (!proc_kill (pi, SIGKILL))
!     proc_warn (pi, "unconditionally_kill, proc_kill", __LINE__);
  #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
    destroy_procinfo (pi);
  
--- 4435,4441 ----
    }
  #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
    if (!proc_kill (pi, SIGKILL))
!     proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
  #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
    destroy_procinfo (pi);
  
Index: proc-api.c
===================================================================
RCS file: /cvs/src/src/gdb/proc-api.c,v
retrieving revision 1.3
diff -p -r1.3 proc-api.c
*** proc-api.c	2000/05/05 18:29:33	1.3
--- proc-api.c	2000/05/05 20:52:44
*************** struct trans {
*** 53,64 ****
    char *desc;                   /* Short description of value */
  };
  
! static int   procfs_trace    = 1;
! /*static int   info_verbose    = 1;*/	/* kludge */
  static FILE *procfs_file     = NULL;
  static char *procfs_filename = "procfs_trace";
  
  static void
  set_procfs_trace_cmd (args, from_tty, c)
       char *args;
       int from_tty;
--- 53,72 ----
    char *desc;                   /* Short description of value */
  };
  
! static int   procfs_trace    = 0;
  static FILE *procfs_file     = NULL;
  static char *procfs_filename = "procfs_trace";
  
  static void
+ prepare_to_trace (void)
+ {
+   if (procfs_trace)			/* if procfs tracing turned on */
+     if (procfs_file == NULL)		/* if output file not yet open */
+       if (procfs_filename != NULL)	/* if output filename known */
+ 	procfs_file = fopen (procfs_filename, "a");	/* open output file */
+ }
+ 
+ static void
  set_procfs_trace_cmd (args, from_tty, c)
       char *args;
       int from_tty;
*************** ioctl_with_trace (fd, opcode, ptr, file,
*** 218,228 ****
  {
    int i, ret, arg1;
  
    if (procfs_trace)
      {
-       if (procfs_file == NULL && procfs_filename != NULL)
- 	procfs_file = fopen (procfs_filename, "a");
- 
        for (i = 0; ioctl_table[i].name != NULL; i++)
  	if (ioctl_table[i].value == opcode)
  	  break;
--- 226,235 ----
  {
    int i, ret, arg1;
  
+   prepare_to_trace ();
+ 
    if (procfs_trace)
      {
        for (i = 0; ioctl_table[i].name != NULL; i++)
  	if (ioctl_table[i].value == opcode)
  	  break;
*************** ioctl_with_trace (fd, opcode, ptr, file,
*** 364,376 ****
        if (procfs_file)
  	fflush (procfs_file);
      }
    ret = ioctl (fd, opcode, ptr);
    if (procfs_trace && ret < 0)
      {
        fprintf (procfs_file ? procfs_file : stdout, 
! 	       "[ioctl (%s) FAILED!]\n",
  	       ioctl_table[i].name != NULL ? 
! 	       ioctl_table[i].name : "<unknown>");
        if (procfs_file)
  	fflush (procfs_file);
      }
--- 371,385 ----
        if (procfs_file)
  	fflush (procfs_file);
      }
+   errno = 0;
    ret = ioctl (fd, opcode, ptr);
    if (procfs_trace && ret < 0)
      {
        fprintf (procfs_file ? procfs_file : stdout, 
! 	       "[ioctl (%s) FAILED! (%s)]\n",
  	       ioctl_table[i].name != NULL ? 
! 	       ioctl_table[i].name : "<unknown>",
! 	       safe_strerror (errno));
        if (procfs_file)
  	fflush (procfs_file);
      }
*************** static struct trans rw_table[] = {
*** 438,459 ****
  static off_t lseek_offset;
  
  int
! write_with_trace (fd, arg, len, file, line)
       int  fd;
!      long *arg;
       size_t len;
       char *file;
       int  line;
  {
    int  i;
    int ret;
!   long opcode = arg[0];
  
    if (procfs_trace)
      {
!       if (procfs_file == NULL && procfs_filename != NULL)
! 	procfs_file = fopen (procfs_filename, "a");
! 
        for (i = 0; rw_table[i].name != NULL; i++)
  	if (rw_table[i].value == opcode)
  	  break;
--- 447,467 ----
  static off_t lseek_offset;
  
  int
! write_with_trace (fd, varg, len, file, line)
       int  fd;
!      void *varg;
       size_t len;
       char *file;
       int  line;
  {
    int  i;
    int ret;
!   long *arg = (long *) varg;
  
+   prepare_to_trace ();
    if (procfs_trace)
      {
!       long opcode = arg[0];
        for (i = 0; rw_table[i].name != NULL; i++)
  	if (rw_table[i].value == opcode)
  	  break;
*************** write_with_trace (fd, arg, len, file, li
*** 582,594 ****
        if (procfs_file)
  	fflush (procfs_file);
      }
    ret = write (fd, (void *) arg, len);
    if (procfs_trace && ret != len)
      {
        fprintf (procfs_file ? procfs_file : stdout, 
! 	       "[write (%s) FAILED!\n",
  	       rw_table[i].name != NULL ? 
! 	       rw_table[i].name : "<unknown>");
        if (procfs_file)
  	fflush (procfs_file);
      }
--- 590,604 ----
        if (procfs_file)
  	fflush (procfs_file);
      }
+   errno = 0;
    ret = write (fd, (void *) arg, len);
    if (procfs_trace && ret != len)
      {
        fprintf (procfs_file ? procfs_file : stdout, 
! 	       "[write (%s) FAILED! (%s)]\n",
  	       rw_table[i].name != NULL ? 
! 	       rw_table[i].name : "<unknown>", 
! 	       safe_strerror (errno));
        if (procfs_file)
  	fflush (procfs_file);
      }
*************** lseek_with_trace (fd, offset, whence, fi
*** 607,640 ****
  {
    off_t ret;
  
! #if 0	/* don't need output, just need address */
!   if (procfs_trace)
!     {
!       if (procfs_file == NULL && procfs_filename != NULL)
! 	procfs_file = fopen (procfs_filename, "a");
! 
!       if (info_verbose)
! 	fprintf (procfs_file ? procfs_file : stdout, 
! 		 "%s:%d -- ", file, line);
!       fprintf (procfs_file ? procfs_file : stdout, 
! 	       "lseek (0x%08x, %s) \n", offset, 
! 	       whence == SEEK_SET ? "SEEK_SET" :
! 	       whence == SEEK_CUR ? "SEEK_CUR" : 
! 	       whence == SEEK_END ? "SEEK_END" :
! 	       "<unknown whence>");
!       if (procfs_file)
! 	fflush (procfs_file);
!     }
! #endif
    ret = lseek (fd, offset, whence);
    lseek_offset = ret;
!   if (procfs_trace && ret == -1)
      {
-       if (procfs_file == NULL && procfs_filename != NULL)
- 	procfs_file = fopen (procfs_filename, "a");
- 
        fprintf (procfs_file ? procfs_file : stdout, 
! 	       "[lseek (0x%08lx) FAILED!\n", (unsigned long) offset);
        if (procfs_file)
  	fflush (procfs_file);
      }
--- 617,631 ----
  {
    off_t ret;
  
!   prepare_to_trace ();
!   errno = 0;
    ret = lseek (fd, offset, whence);
    lseek_offset = ret;
!   if (procfs_trace && (ret == -1 || errno != 0))
      {
        fprintf (procfs_file ? procfs_file : stdout, 
! 	       "[lseek (0x%08lx) FAILED! (%s)]\n", 
! 	       (unsigned long) offset, safe_strerror (errno));
        if (procfs_file)
  	fflush (procfs_file);
      }
*************** open_with_trace (filename, mode, file, l
*** 651,674 ****
       char *file;
       int   line;
  {
!   int ret = open (filename, mode);
  
    if (procfs_trace)
      {
-       if (procfs_file == NULL && procfs_filename != NULL)
- 	procfs_file = fopen (procfs_filename, "a");
- 
        if (info_verbose)
  	fprintf (procfs_file ? procfs_file : stdout, 
  		 "%s:%d -- ", file, line);
!       fprintf (procfs_file ? procfs_file : stdout, 
! 	       "%d = open (%s, ", ret, filename);
!       if (mode == O_RDONLY)
! 	fprintf (procfs_file ? procfs_file : stdout, "O_RDONLY) %d\n", line);
!       else if (mode == O_WRONLY)
! 	fprintf (procfs_file ? procfs_file : stdout, "O_WRONLY) %d\n", line);
!       else if (mode == O_RDWR)
! 	fprintf (procfs_file ? procfs_file : stdout, "O_RDWR)   %d\n", line);
        if (procfs_file)
  	fflush (procfs_file);
      }
--- 642,678 ----
       char *file;
       int   line;
  {
!   int ret;
  
+   prepare_to_trace ();
+   errno = 0;
+   ret = open (filename, mode);
    if (procfs_trace)
      {
        if (info_verbose)
  	fprintf (procfs_file ? procfs_file : stdout, 
  		 "%s:%d -- ", file, line);
! 
!       if (errno)
! 	{
! 	  fprintf (procfs_file ? procfs_file : stdout, 
! 		   "[open FAILED! (%s) line %d]\\n", 
! 		   safe_strerror (errno), line);
! 	}
!       else
! 	{
! 	  fprintf (procfs_file ? procfs_file : stdout, 
! 		   "%d = open (%s, ", ret, filename);
! 	  if (mode == O_RDONLY)
! 	    fprintf (procfs_file ? procfs_file : stdout, "O_RDONLY) %d\n",
! 		     line);
! 	  else if (mode == O_WRONLY)
! 	    fprintf (procfs_file ? procfs_file : stdout, "O_WRONLY) %d\n",
! 		     line);
! 	  else if (mode == O_RDWR)
! 	    fprintf (procfs_file ? procfs_file : stdout, "O_RDWR)   %d\n",
! 		     line);
! 	}
        if (procfs_file)
  	fflush (procfs_file);
      }
*************** close_with_trace (fd, file, line)
*** 682,699 ****
       char *file;
       int   line;
  {
!   int ret = close (fd);
  
    if (procfs_trace)
      {
-       if (procfs_file == NULL && procfs_filename != NULL)
- 	procfs_file = fopen (procfs_filename, "a");
- 
        if (info_verbose)
  	fprintf (procfs_file ? procfs_file : stdout, 
  		 "%s:%d -- ", file, line);
!       fprintf (procfs_file ? procfs_file : stdout, 
! 	       "%d = close (%d)\n", ret, fd);
        if (procfs_file)
  	fflush (procfs_file);
      }
--- 686,707 ----
       char *file;
       int   line;
  {
!   int ret;
  
+   prepare_to_trace ();
+   errno = 0;
+   ret = close (fd);
    if (procfs_trace)
      {
        if (info_verbose)
  	fprintf (procfs_file ? procfs_file : stdout, 
  		 "%s:%d -- ", file, line);
!       if (errno)
! 	fprintf (procfs_file ? procfs_file : stdout, 
! 		 "[close FAILED! (%s)]\n", safe_strerror (errno));
!       else
! 	fprintf (procfs_file ? procfs_file : stdout, 
! 		 "%d = close (%d)\n", ret, fd);
        if (procfs_file)
  	fflush (procfs_file);
      }
*************** close_with_trace (fd, file, line)
*** 701,707 ****
    return ret;
  }
  
! int
  wait_with_trace (wstat, file, line)
       int  *wstat;
       char *file;
--- 709,715 ----
    return ret;
  }
  
! pid_t
  wait_with_trace (wstat, file, line)
       int  *wstat;
       char *file;
*************** wait_with_trace (wstat, file, line)
*** 709,719 ****
  {
    int ret, lstat = 0;
  
    if (procfs_trace)
      {
-       if (procfs_file == NULL && procfs_filename != NULL)
- 	procfs_file = fopen (procfs_filename, "a");
- 
        if (info_verbose)
  	fprintf (procfs_file ? procfs_file : stdout, 
  		 "%s:%d -- ", file, line);
--- 717,725 ----
  {
    int ret, lstat = 0;
  
+   prepare_to_trace ();
    if (procfs_trace)
      {
        if (info_verbose)
  	fprintf (procfs_file ? procfs_file : stdout, 
  		 "%s:%d -- ", file, line);
*************** wait_with_trace (wstat, file, line)
*** 722,732 ****
        if (procfs_file)
  	fflush (procfs_file);
      }
    ret = wait (&lstat);
    if (procfs_trace)
      {
!       fprintf (procfs_file ? procfs_file : stdout, 
! 	       "returned pid %d, status 0x%x\n", ret, lstat);
        if (procfs_file)
  	fflush (procfs_file);
      }
--- 728,743 ----
        if (procfs_file)
  	fflush (procfs_file);
      }
+   errno = 0;
    ret = wait (&lstat);
    if (procfs_trace)
      {
!       if (errno)
! 	fprintf (procfs_file ? procfs_file : stdout, 
! 		 "[wait FAILED! (%s)]\n", safe_strerror (errno));
!       else
! 	fprintf (procfs_file ? procfs_file : stdout, 
! 		 "returned pid %d, status 0x%x\n", ret, lstat);
        if (procfs_file)
  	fflush (procfs_file);
      }
*************** procfs_note (msg, file, line)
*** 742,752 ****
       char *file;
       int   line;
  {
    if (procfs_trace)
      {
-       if (procfs_file == NULL && procfs_filename != NULL)
- 	procfs_file = fopen (procfs_filename, "a");
- 
        if (info_verbose)
  	fprintf (procfs_file ? procfs_file : stdout, 
  		 "%s:%d -- ", file, line);
--- 753,761 ----
       char *file;
       int   line;
  {
+   prepare_to_trace ();
    if (procfs_trace)
      {
        if (info_verbose)
  	fprintf (procfs_file ? procfs_file : stdout, 
  		 "%s:%d -- ", file, line);
*************** proc_prettyfprint_status (flags, why, wh
*** 763,773 ****
       int  what;
       int  thread;
  {
    if (procfs_trace)
      {
-       if (procfs_file == NULL && procfs_filename != NULL)
- 	procfs_file = fopen (procfs_filename, "a");
- 
        if (thread)
  	fprintf (procfs_file ? procfs_file : stdout,
  		 "Thread %d: ", thread);
--- 772,780 ----
       int  what;
       int  thread;
  {
+   prepare_to_trace ();
    if (procfs_trace)
      {
        if (thread)
  	fprintf (procfs_file ? procfs_file : stdout,
  		 "Thread %d: ", thread);
*************** _initialize_proc_api ()
*** 791,797 ****
  
    c = add_set_cmd ("procfs-trace", no_class,
  		   var_boolean, (char *) &procfs_trace, 
! 		   "Set tracing for /proc ioctl calls.\n", &setlist);
  
    add_show_from_set (c, &showlist);
    c->function.sfunc = set_procfs_trace_cmd;
--- 798,804 ----
  
    c = add_set_cmd ("procfs-trace", no_class,
  		   var_boolean, (char *) &procfs_trace, 
! 		   "Set tracing for /proc api calls.\n", &setlist);
  
    add_show_from_set (c, &showlist);
    c->function.sfunc = set_procfs_trace_cmd;
*************** _initialize_proc_api ()
*** 802,810 ****
  
    add_show_from_set (c, &showlist);
    c->function.sfunc = set_procfs_file_cmd;
- 
- #ifdef TRACE_PROCFS
-   if (procfs_file == NULL && procfs_filename != NULL)
-     procfs_file = fopen (procfs_filename, "a");
- #endif
  }
--- 809,812 ----

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]