[gcc r16-8288] gcov-profile/121074 - hold onto gcov file for less time

Richard Biener rguenth@gcc.gnu.org
Wed Mar 25 16:10:15 GMT 2026


https://gcc.gnu.org/g:476d61a8cb28b627f99500a30ccd11e36ade0a72

commit r16-8288-g476d61a8cb28b627f99500a30ccd11e36ade0a72
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Mar 25 14:40:59 2026 +0100

    gcov-profile/121074 - hold onto gcov file for less time
    
    The following fixes the issue of two open gcov files at the
    same time, one from -ftest-coverage, opened/closed by
    coverage_init/finish and one from -fauto-profile, attempted
    to be opened by pass_ipa_auto_profile.  The solution is
    to open the coverage files only during pass_ipa_tree_profile.
    
            PR gcov-profile/121074
            * coverage.h (coverage_init_file): Declare.
            (coverage_finish_file): Likewise.
            * coverage.cc (coverage_init_file): New function, split
            out actual file opening and writing from ...
            (coverage_init): ... here.
            (coverage_finish_file): Likewise for file closing, from ...
            (coverage_finish): ... here.
            * tree-profile.cc (tree_profiling): Call coverage_init_file
            and coverage_finish_file here.

Diff:
---
 gcc/coverage.cc     | 57 +++++++++++++++++++++++++++++++++--------------------
 gcc/coverage.h      |  2 ++
 gcc/tree-profile.cc |  3 +++
 3 files changed, 41 insertions(+), 21 deletions(-)

diff --git a/gcc/coverage.cc b/gcc/coverage.cc
index f2a095a12791..3d960b8608c4 100644
--- a/gcc/coverage.cc
+++ b/gcc/coverage.cc
@@ -1248,6 +1248,33 @@ coverage_obj_finish (vec<constructor_elt, va_gc> *ctor,
   varpool_node::finalize_decl (gcov_info_var);
 }
 
+/* Open the coverage files.  */
+
+void
+coverage_init_file (void)
+{
+  if (flag_test_coverage && !flag_compare_debug)
+    {
+      if (!gcov_open (bbg_file_name, -1))
+	{
+	  error ("cannot open %s", bbg_file_name);
+	  bbg_file_name = NULL;
+	}
+      else
+	{
+	  gcov_write_unsigned (GCOV_NOTE_MAGIC);
+	  gcov_write_unsigned (GCOV_VERSION);
+	  gcov_write_unsigned (bbg_file_stamp);
+	  /* Use an arbitrary checksum */
+	  gcov_write_unsigned (0);
+	  gcov_write_string (remap_profile_filename (getpwd ()));
+
+	  /* Do not support has_unexecuted_blocks for Ada.  */
+	  gcov_write_unsigned (strcmp (lang_hooks.name, "GNU Ada") != 0);
+	}
+    }
+}
+
 /* Perform file-level initialization. Read in data file, generate name
    of notes file.  */
 
@@ -1330,34 +1357,15 @@ coverage_init (const char *filename)
 	  memcpy (bbg_file_name, original_filename, original_len);
 	  strcpy (bbg_file_name + original_len, GCOV_NOTE_SUFFIX);
 	}
-
-      if (!gcov_open (bbg_file_name, -1))
-	{
-	  error ("cannot open %s", bbg_file_name);
-	  bbg_file_name = NULL;
-	}
-      else
-	{
-	  gcov_write_unsigned (GCOV_NOTE_MAGIC);
-	  gcov_write_unsigned (GCOV_VERSION);
-	  gcov_write_unsigned (bbg_file_stamp);
-	  /* Use an arbitrary checksum */
-	  gcov_write_unsigned (0);
-	  gcov_write_string (remap_profile_filename (getpwd ()));
-
-	  /* Do not support has_unexecuted_blocks for Ada.  */
-	  gcov_write_unsigned (strcmp (lang_hooks.name, "GNU Ada") != 0);
-	}
     }
 
   g->get_dumps ()->dump_finish (profile_pass_num);
 }
 
-/* Performs file-level cleanup.  Close notes file, generate coverage
-   variables and constructor.  */
+/* Close the coverage files.  */
 
 void
-coverage_finish (void)
+coverage_finish_file (void)
 {
   if (bbg_file_name && gcov_close ())
     unlink (bbg_file_name);
@@ -1367,7 +1375,14 @@ coverage_finish (void)
     /* Only remove the da file, if we're emitting coverage code and
        cannot uniquely stamp it.  If we can stamp it, libgcov will DTRT.  */
     unlink (da_file_name);
+}
 
+/* Performs file-level cleanup.  Close notes file, generate coverage
+   variables and constructor.  */
+
+void
+coverage_finish (void)
+{
   /* Global GCDA checksum that aggregates all functions.  */
   unsigned object_checksum = 0;
 
diff --git a/gcc/coverage.h b/gcc/coverage.h
index abd07e40bad7..4450f427e986 100644
--- a/gcc/coverage.h
+++ b/gcc/coverage.h
@@ -24,6 +24,8 @@ along with GCC; see the file COPYING3.  If not see
 
 extern void coverage_init (const char *);
 extern void coverage_finish (void);
+extern void coverage_init_file (void);
+extern void coverage_finish_file (void);
 extern void coverage_remove_note_file (void);
 
 /* Start outputting coverage information for the current
diff --git a/gcc/tree-profile.cc b/gcc/tree-profile.cc
index 273050ba55a9..7569dbb2f91a 100644
--- a/gcc/tree-profile.cc
+++ b/gcc/tree-profile.cc
@@ -1856,6 +1856,8 @@ tree_profiling (void)
 {
   struct cgraph_node *node;
 
+  coverage_init_file ();
+
   /* Verify whether we can utilize atomic update operations.  */
   bool can_support_atomic = targetm.have_libatomic;
   unsigned HOST_WIDE_INT gcov_type_size
@@ -2071,6 +2073,7 @@ tree_profiling (void)
 
   del_node_map ();
   end_branch_prob ();
+  coverage_finish_file ();
   return 0;
 }


More information about the Gcc-cvs mailing list