libgcc2.c-patch 2.29 KB
Newer Older
1 2 3
This patch, relative to gcc-2.95.3, improves gcov support by properly
locking the output files that are written at process exit.

4
/Niels Möller <nisse@lysator.liu.se>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

--- libgcc2.c.orig	Fri Jun 11 05:11:43 1999
+++ libgcc2.c	Thu Mar 22 20:48:39 2001
@@ -1459,6 +1459,10 @@
 #include "gcov-io.h"
 #include <string.h>
 
+/* For O_RDWR and O_CREAT */
+#include <fcntl.h>
+#include <errno.h>
+
 static struct bb *bb_head;
 
 /* Return the number of digits needed to print a value */
@@ -1478,6 +1482,23 @@
   return ret;
 }
 
+/* Opens a file, locking it for exclusive access. Used for .da files
+ * below. 
+ */
+static FILE *
+lopen(const char *name)
+{
+  int fd = open(name, O_RDWR | O_CREAT, 0666);
+  if (fd < 0)
+    return NULL;
+
+  while ( (lockf(fd, F_LOCK, 0) < 0))
+    if (errno != EINTR)
+      return NULL;
+
+  return fdopen(fd, "r+b");
+}
+
 void
 __bb_exit_func (void)
 {
@@ -1502,18 +1523,21 @@
 	  /* If the file exists, and the number of counts in it is the same,
 	     then merge them in.  */
 	     
-	  if ((da_file = fopen (ptr->filename, "r")) != 0)
+	  long n_counts = 0;
+
+	  /* If the file exists, and the number of counts in it is the same,
+	     then merge them in.  */
+
+
+	  if ((da_file = lopen (ptr->filename)) == 0)
 	    {
-	      long n_counts = 0;
-	      
-	      if (__read_long (&n_counts, da_file, 8) != 0)
-		{
-		  fprintf (stderr, "arc profiling: Can't read output file %s.\n",
-			   ptr->filename);
-		  continue;
-		}
+	      fprintf (stderr, "arc profiling: Can't open output file %s.\n",
+		       ptr->filename);
+	      continue;
+	    }
 
-	      if (n_counts == ptr->ncounts)
+	  if ( (__read_long (&n_counts, da_file, 8) == 0)
+	       && (n_counts == ptr->ncounts) )
 		{
 		  int i;
 
@@ -1531,16 +1555,9 @@
 		    }
 		}
 
-	      if (fclose (da_file) == EOF)
-		fprintf (stderr, "arc profiling: Error closing output file %s.\n",
-			 ptr->filename);
-	    }
-	  if ((da_file = fopen (ptr->filename, "w")) == 0)
-	    {
-	      fprintf (stderr, "arc profiling: Can't open output file %s.\n",
-		       ptr->filename);
-	      continue;
-	    }
+          /* Perhaps we should also truncate the file before
+	   * writing? */
+	  rewind(da_file);
 
 	  /* ??? Should first write a header to the file.  Preferably, a 4 byte
 	     magic number, 4 bytes containing the time the program was