[PATCH] D62541: Adding a function for setting coverage output file.

Sajjad Mirza via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 28 11:44:35 PDT 2019


sajjadm created this revision.
Herald added projects: Sanitizers, LLVM.
Herald added subscribers: llvm-commits, Sanitizers.

User code can open a file on its own and pass it to the runtime, rather than
specifying a name and having the runtime open the file. This supports the use
case where a process cannot open a file on its own but can receive a file
descriptor from another process.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D62541

Files:
  compiler-rt/lib/profile/InstrProfiling.h
  compiler-rt/lib/profile/InstrProfilingFile.c


Index: compiler-rt/lib/profile/InstrProfilingFile.c
===================================================================
--- compiler-rt/lib/profile/InstrProfilingFile.c
+++ compiler-rt/lib/profile/InstrProfilingFile.c
@@ -37,7 +37,7 @@
 /* From where is profile name specified.
  * The order the enumerators define their
  * precedence. Re-order them may lead to
- * runtime behavior change. */ 
+ * runtime behavior change. */
 typedef enum ProfileNameSpecifier {
   PNS_unknown = 0,
   PNS_default,
@@ -89,6 +89,17 @@
 COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0,   0, 0, 0, {0},
                                                    {0}, 0, 0, 0, PNS_unknown};
 
+static FILE *ProfileFile = NULL;
+COMPILER_RT_VISIBILITY FILE *lprofGetProfileFile() { return ProfileFile; }
+
+COMPILER_RT_VISIBILITY void lprofSetProfileFile(FILE *File) {
+  ProfileFile = File;
+}
+
+COMPILER_RT_VISIBILITY void __llvm_profile_set_file_object(FILE *File) {
+  lprofSetProfileFile(File);
+}
+
 static int getCurFilenameLength();
 static const char *getCurFilename(char *FilenameBuf, int ForceUseBuf);
 static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
@@ -248,10 +259,14 @@
 static int writeFile(const char *OutputName) {
   int RetVal;
   FILE *OutputFile;
+  FILE *ProfileFilePtr;
 
   int MergeDone = 0;
   VPMergeHook = &lprofMergeValueProfData;
-  if (!doMerging())
+  ProfileFilePtr = lprofGetProfileFile();
+  if (ProfileFilePtr != NULL)
+    OutputFile = ProfileFilePtr;
+  else if (!doMerging())
     OutputFile = fopen(OutputName, "ab");
   else
     OutputFile = openFileForMerging(OutputName, &MergeDone);
@@ -591,7 +606,7 @@
 
   EnvFilenamePat = getFilenamePatFromEnv();
   if (EnvFilenamePat) {
-    /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid 
+    /* Pass CopyFilenamePat = 1, to ensure that the filename would be valid
        at the  moment when __llvm_profile_write_file() gets executed. */
     parseAndSetFilename(EnvFilenamePat, PNS_environment, 1);
     return;
@@ -627,8 +642,7 @@
   int PDeathSig = 0;
 
   if (lprofProfileDumped()) {
-    PROF_NOTE("Profile data not written to file: %s.\n", 
-              "already written");
+    PROF_NOTE("Profile data not written to file: %s.\n", "already written");
     return 0;
   }
 
Index: compiler-rt/lib/profile/InstrProfiling.h
===================================================================
--- compiler-rt/lib/profile/InstrProfiling.h
+++ compiler-rt/lib/profile/InstrProfiling.h
@@ -10,6 +10,7 @@
 #define PROFILE_INSTRPROFILING_H_
 
 #include "InstrProfilingPort.h"
+#include <stdio.h>
 
 #define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY
 #include "InstrProfData.inc"
@@ -125,7 +126,7 @@
 /*!
  * \brief this is a wrapper interface to \c __llvm_profile_write_file.
  * After this interface is invoked, a arleady dumped flag will be set
- * so that profile won't be dumped again during program exit. 
+ * so that profile won't be dumped again during program exit.
  * Invocation of interface __llvm_profile_reset_counters will clear
  * the flag. This interface is designed to be used to collect profile
  * data from user selected hot regions. The use model is
@@ -157,6 +158,8 @@
  */
 void __llvm_profile_set_filename(const char *Name);
 
+void __llvm_profile_set_file_object(FILE *File);
+
 /*! \brief Register to write instrumentation data to file at exit. */
 int __llvm_profile_register_write_file_atexit(void);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D62541.201733.patch
Type: text/x-patch
Size: 3448 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190528/78d8b21e/attachment.bin>


More information about the llvm-commits mailing list