[iOS][code coverage] Change ios raw coverage data file path and name.

- Changed the path to the simulator shared resources directory, where
the app also write rights, and won't be deleted after app is killed.
- Added "%m" to the file name, so that on-line profile merging is
enabled.
- Added a generated UUID to the file name, so that there won't be a
conflict when multiple apps are launched in one test suite.

Please see ios code coverage design doc for rationales of those changes:
go/bling-code-coverage-doc.

Bug: 1043273
Change-Id: I21b2c8f4018036fdd13eecbe7e1362c926886eca
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2031673
Commit-Queue: Zhaoyang Li <[email protected]>
Reviewed-by: John Budorick <[email protected]>
Reviewed-by: Eugene But <[email protected]>
Reviewed-by: Yuke Liao <[email protected]>
Cr-Commit-Position: refs/heads/master@{#738365}
diff --git a/testing/coverage_util_ios.mm b/testing/coverage_util_ios.mm
index a8c8670..c148b2f 100644
--- a/testing/coverage_util_ios.mm
+++ b/testing/coverage_util_ios.mm
@@ -6,33 +6,43 @@
 
 #import "testing/gtest/ios_enable_coverage.h"
 
-#if !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE)
+#if !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) && \
+    TARGET_IPHONE_SIMULATOR
 extern "C" void __llvm_profile_set_filename(const char* name);
 #endif
 
 namespace coverage_util {
 
 void ConfigureCoverageReportPath() {
-#if !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE)
+// Targets won't build on real devices with BUILDFLAG(IOS_ENABLE_COVERAGE)
+// because of llvm library linking issue for arm64 architecture.
+#if !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) && \
+    TARGET_IPHONE_SIMULATOR
   static dispatch_once_t once_token;
   dispatch_once(&once_token, ^{
-    // Writes the profraw file to the Documents directory, where the app has
-    // write rights.
-    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
-                                                         NSUserDomainMask, YES);
-    NSString* documents_directory = [paths firstObject];
-    NSString* file_name = [documents_directory
-        stringByAppendingPathComponent:@"coverage.profraw"];
+    // Writes the profraw file to the simulator shared resources directory,
+    // where the app has write rights, and will be preserved after app is
+    // killed.
+    NSString* shared_resources_path =
+        NSProcessInfo.processInfo
+            .environment[@"SIMULATOR_SHARED_RESOURCES_DIRECTORY"];
+    // UUID ensures that there won't be a conflict when multiple apps are
+    // launched in one test suite in EG2. %m enables on-line profile merging.
+    NSString* file_name =
+        [NSString stringWithFormat:@"%@-%%m.profraw", NSUUID.UUID.UUIDString];
+    NSString* file_path =
+        [shared_resources_path stringByAppendingPathComponent:file_name];
 
     // For documentation, see:
     // http://clang.llvm.org/docs/SourceBasedCodeCoverage.html
     __llvm_profile_set_filename(
-        [file_name cStringUsingEncoding:NSUTF8StringEncoding]);
+        [file_path cStringUsingEncoding:NSUTF8StringEncoding]);
 
     // Print the path for easier retrieval.
-    NSLog(@"Coverage data at %@.", file_name);
+    NSLog(@"Coverage data at %@.", file_path);
   });
-#endif  // !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE)
+#endif  // !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) &&
+        // TARGET_IPHONE_SIMULATOR
 }
 
 }  // namespace coverage_util