Pass configuration to Perfetto using files.

This change avoids shell quoting issues if the profiling configuration
specified contains single or double quotes.

Bug: 267137202
Change-Id: If739418004d61496bfd46eaef08465559e87e661
diff --git a/python/tools/cpu_profile.py b/python/tools/cpu_profile.py
index 481248a..a743981 100644
--- a/python/tools/cpu_profile.py
+++ b/python/tools/cpu_profile.py
@@ -297,14 +297,23 @@
   }
   if not release_or_newer('R'):
     sys.exit("This tool requires Android R+ to run.")
+
+  # Push configuration to the device.
+  tf = tempfile.NamedTemporaryFile()
+  tf.file.write(config.encode('utf-8'))
+  tf.file.flush()
+  profile_config_path = '/data/misc/perfetto-configs/config-' + UUID
+  adb_check_output(['adb', 'push', tf.name, profile_config_path])
+  tf.close()
+
+
   profile_device_path = '/data/misc/perfetto-traces/profile-' + UUID
-  perfetto_command = ('CONFIG=\'{}\'; echo ${{CONFIG}} | '
-                      'perfetto --txt -c - -o {} -d')
+  perfetto_command = ('perfetto --txt -c {} -o {} -d')
   try:
     perfetto_pid = int(
         adb_check_output([
             'adb', 'exec-out',
-            perfetto_command.format(config, profile_device_path)
+            perfetto_command.format(profile_config_path, profile_device_path)
         ]).strip())
   except ValueError as error:
     sys.exit("Unable to start profiling: {}".format(error))
@@ -334,6 +343,7 @@
 
   profile_host_path = os.path.join(profile_target, 'raw-trace')
   adb_check_output(['adb', 'pull', profile_device_path, profile_host_path])
+  adb_check_output(['adb', 'shell', 'rm', profile_config_path])
   adb_check_output(['adb', 'shell', 'rm', profile_device_path])
 
 
diff --git a/tools/cpu_profile b/tools/cpu_profile
index c305ebc..8cd5eda 100755
--- a/tools/cpu_profile
+++ b/tools/cpu_profile
@@ -560,14 +560,23 @@
   }
   if not release_or_newer('R'):
     sys.exit("This tool requires Android R+ to run.")
+
+  # Push configuration to the device.
+  tf = tempfile.NamedTemporaryFile()
+  tf.file.write(config.encode('utf-8'))
+  tf.file.flush()
+  profile_config_path = '/data/misc/perfetto-configs/config-' + UUID
+  adb_check_output(['adb', 'push', tf.name, profile_config_path])
+  tf.close()
+
+
   profile_device_path = '/data/misc/perfetto-traces/profile-' + UUID
-  perfetto_command = ('CONFIG=\'{}\'; echo ${{CONFIG}} | '
-                      'perfetto --txt -c - -o {} -d')
+  perfetto_command = ('perfetto --txt -c {} -o {} -d')
   try:
     perfetto_pid = int(
         adb_check_output([
             'adb', 'exec-out',
-            perfetto_command.format(config, profile_device_path)
+            perfetto_command.format(profile_config_path, profile_device_path)
         ]).strip())
   except ValueError as error:
     sys.exit("Unable to start profiling: {}".format(error))
@@ -597,6 +606,7 @@
 
   profile_host_path = os.path.join(profile_target, 'raw-trace')
   adb_check_output(['adb', 'pull', profile_device_path, profile_host_path])
+  adb_check_output(['adb', 'shell', 'rm', profile_config_path])
   adb_check_output(['adb', 'shell', 'rm', profile_device_path])