Revert "[MemProf] Defer profile file setup until dump time"

This reverts commit ea1826ee57984d4f44fdb4b35a47169d393618ed.

This change is breaking the ability of tests to override the profile
output file. Need to add a mechanism to do that before resubmitting.

GitOrigin-RevId: 311801a0179be9dc86895e08e3bba66cea2c0724
diff --git a/lib/memprof/memprof_allocator.cpp b/lib/memprof/memprof_allocator.cpp
index 4bd3746..c21e4e8 100644
--- a/lib/memprof/memprof_allocator.cpp
+++ b/lib/memprof/memprof_allocator.cpp
@@ -26,7 +26,6 @@
 #include "sanitizer_common/sanitizer_errno.h"
 #include "sanitizer_common/sanitizer_file.h"
 #include "sanitizer_common/sanitizer_flags.h"
-#include "sanitizer_common/sanitizer_interface_internal.h"
 #include "sanitizer_common/sanitizer_internal_defs.h"
 #include "sanitizer_common/sanitizer_procmaps.h"
 #include "sanitizer_common/sanitizer_stackdepot.h"
@@ -34,9 +33,6 @@
 #include <sched.h>
 #include <time.h>
 
-// Allow the user to specify a profile output file via the binary.
-SANITIZER_WEAK_ATTRIBUTE char __memprof_profile_filename[1];
-
 namespace __memprof {
 namespace {
 using ::llvm::memprof::MemInfoBlock;
@@ -282,12 +278,6 @@
   }
 
   void FinishAndWrite() {
-    // Use profile name specified via the binary itself if it exists, and hasn't
-    // been overrriden by a flag at runtime.
-    if (__memprof_profile_filename[0] != 0 && !common_flags()->log_path)
-      __sanitizer_set_report_path(__memprof_profile_filename);
-    else
-      __sanitizer_set_report_path(common_flags()->log_path);
     if (print_text && common_flags()->print_module_map)
       DumpProcessMap();
 
@@ -312,11 +302,6 @@
     }
 
     allocator.ForceUnlock();
-
-    // Set the report back to the default stderr now that we have dumped the
-    // profile, in case there are later errors or stats dumping on exit has been
-    // enabled.
-    __sanitizer_set_report_path("stderr");
   }
 
   // Inserts any blocks which have been allocated but not yet deallocated.
diff --git a/lib/memprof/memprof_rtl.cpp b/lib/memprof/memprof_rtl.cpp
index c2e0b4e..d30b803 100644
--- a/lib/memprof/memprof_rtl.cpp
+++ b/lib/memprof/memprof_rtl.cpp
@@ -29,6 +29,9 @@
 
 uptr __memprof_shadow_memory_dynamic_address; // Global interface symbol.
 
+// Allow the user to specify a profile output file via the binary.
+SANITIZER_WEAK_ATTRIBUTE char __memprof_profile_filename[1];
+
 namespace __memprof {
 
 static void MemprofDie() {
@@ -166,6 +169,13 @@
   AddDieCallback(MemprofDie);
   SetCheckUnwindCallback(CheckUnwind);
 
+  // Use profile name specified via the binary itself if it exists, and hasn't
+  // been overrriden by a flag at runtime.
+  if (__memprof_profile_filename[0] != 0 && !common_flags()->log_path)
+    __sanitizer_set_report_path(__memprof_profile_filename);
+  else
+    __sanitizer_set_report_path(common_flags()->log_path);
+
   __sanitizer::InitializePlatformEarly();
 
   // Setup internal allocator callback.
diff --git a/test/memprof/TestCases/atexit_stats.cpp b/test/memprof/TestCases/atexit_stats.cpp
index 29d9a0a..4a5fac9 100644
--- a/test/memprof/TestCases/atexit_stats.cpp
+++ b/test/memprof/TestCases/atexit_stats.cpp
@@ -2,9 +2,6 @@
 
 // RUN: %clangxx_memprof -O0 %s -o %t
 // RUN: %env_memprof_opts=print_text=true:log_path=stderr:atexit=1 %run %t 2>&1 | FileCheck %s
-// Stats should be dumped to stderr even if the profile log path set to a file.
-// RUN: rm -f %t.log.*
-// RUN: %env_memprof_opts=print_text=true:log_path=%t.log:atexit=1 %run %t 2>&1 | FileCheck %s
 // RUN: %env_memprof_opts=print_text=true:log_path=stderr:atexit=0 %run %t 2>&1 | FileCheck %s --check-prefix=NOATEXIT
 
 // CHECK: MemProfiler exit stats:
diff --git a/test/memprof/TestCases/malloc-size-too-big.cpp b/test/memprof/TestCases/malloc-size-too-big.cpp
index 78825d0..4eb9e8e 100644
--- a/test/memprof/TestCases/malloc-size-too-big.cpp
+++ b/test/memprof/TestCases/malloc-size-too-big.cpp
@@ -1,8 +1,5 @@
 // RUN: %clangxx_memprof -O0 %s -o %t
 // RUN: %env_memprof_opts=print_text=true:log_path=stderr:allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-SUMMARY
-// Errors should be printed to stderr even if the log_path set to a file.
-// RUN: rm -f %t.log.*
-// RUN: %env_memprof_opts=print_text=true:log_path=%t.log:allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-SUMMARY
 // RUN: %env_memprof_opts=print_text=true:log_path=stderr:allocator_may_return_null=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
 // Test print_summary
 // RUN: %env_memprof_opts=print_text=true:log_path=stderr:allocator_may_return_null=0:print_summary=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-NOSUMMARY