Allow build-and-collect-pgo-profiles to write files
https://bugs.webkit.org/show_bug.cgi?id=241692
<rdar://95311491>

Patch by Brianna Fan <briannaf@berkeley.edu> on 2022-06-21
Reviewed by Dewei Zhu.

Ran into a missing profiles issue as required permissions to write were missing.
Fixed this issue by moving a code block in GenerateProfiles.h.

* Source/WTF/wtf/GenerateProfiles.h:
(WTF::registerProfileGenerationCallback):

Canonical link: https://commits.webkit.org/251703@main

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@295698 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WTF/wtf/GenerateProfiles.h b/Source/WTF/wtf/GenerateProfiles.h
index 505414c..d1b38e3 100644
--- a/Source/WTF/wtf/GenerateProfiles.h
+++ b/Source/WTF/wtf/GenerateProfiles.h
@@ -59,18 +59,19 @@
             "`.");
         WTFLogAlways("<WEBKIT_LLVM_PROFILE> To sanity-check the output: `for f in ./profiles/*; do echo $f; xcrun -sdk macosx.internal llvm-profdata show $f; done;`.");
 
-        {
-            // Maybe we could use %t instead here, but this folder is permitted through the sandbox because of ANGLE.
-            FileSystem::PlatformFileHandle fileHandle;
-            auto filePath = FileSystem::openTemporaryFile(makeString(name, "-", getpid()), fileHandle, ".profraw"_s);
-            profileFileBase.get() = String::fromUTF8(filePath.utf8().data());
-            FileSystem::closeFile(fileHandle);
-        }
-
         WTFLogAlways("<WEBKIT_LLVM_PROFILE><%s><%d>: We will dump the resulting profile to %s.", name, pid, profileFileBase->utf8().data());
 
         int token;
         notify_register_dispatch("com.apple.WebKit.profiledata", &token, dispatch_get_main_queue(), ^(int) {
+            
+            {
+                // Maybe we could use %t instead here, but this folder is permitted through the sandbox because of ANGLE.
+                FileSystem::PlatformFileHandle fileHandle;
+                auto filePath = FileSystem::openTemporaryFile(makeString(name, "-", getpid()), fileHandle, ".profraw"_s);
+                profileFileBase.get() = String::fromUTF8(filePath.utf8().data());
+                FileSystem::closeFile(fileHandle);
+            }
+            
             profileFileName.get() = makeString(profileFileBase.get(), ".", profileCount++, ".profraw");
             __llvm_profile_set_filename(profileFileName->utf8().data()); // Must stay alive while it is used by llvm.