profcollectd: Use --decode-etm when recording

Also use --binary to filter binaries, use --exclude-perf to exclude
ETM data for profcollectd.

Bug: 279094308
Test: run profcollectd manually
Change-Id: I6d74ebab932ae576599f7435bab9824b7d3c1d57
diff --git a/profcollectd/libprofcollectd/logging_trace_provider.rs b/profcollectd/libprofcollectd/logging_trace_provider.rs
index 0b9e36f..d9fd35e 100644
--- a/profcollectd/libprofcollectd/logging_trace_provider.rs
+++ b/profcollectd/libprofcollectd/logging_trace_provider.rs
@@ -36,7 +36,7 @@
         true
     }
 
-    fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration) {
+    fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration, binary_filter: &str) {
         let trace_file = trace_provider::get_path(trace_dir, tag, LOGGING_TRACEFILE_EXTENSION);
 
         log::info!(
diff --git a/profcollectd/libprofcollectd/scheduler.rs b/profcollectd/libprofcollectd/scheduler.rs
index c4d6880..5558f58 100644
--- a/profcollectd/libprofcollectd/scheduler.rs
+++ b/profcollectd/libprofcollectd/scheduler.rs
@@ -74,6 +74,7 @@
                                 &TRACE_OUTPUT_DIR,
                                 "periodic",
                                 &config.sampling_period,
+                                &config.binary_filter,
                             );
                         }
                     }
@@ -96,7 +97,12 @@
     pub fn one_shot(&self, config: &Config, tag: &str) -> Result<()> {
         let trace_provider = self.trace_provider.clone();
         if check_space_limit(&TRACE_OUTPUT_DIR, config)? {
-            trace_provider.lock().unwrap().trace(&TRACE_OUTPUT_DIR, tag, &config.sampling_period);
+            trace_provider.lock().unwrap().trace(
+                &TRACE_OUTPUT_DIR,
+                tag,
+                &config.sampling_period,
+                &config.binary_filter,
+            );
         }
         Ok(())
     }
diff --git a/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs b/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs
index 6b326b4..cb61802 100644
--- a/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs
+++ b/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs
@@ -38,12 +38,13 @@
         simpleperf_profcollect::has_device_support()
     }
 
-    fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration) {
+    fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration, binary_filter: &str) {
         let trace_file = trace_provider::get_path(trace_dir, tag, ETM_TRACEFILE_EXTENSION);
 
         simpleperf_profcollect::record(
             &trace_file,
             sampling_period,
+            binary_filter,
             simpleperf_profcollect::RecordScope::BOTH,
         );
     }
diff --git a/profcollectd/libprofcollectd/trace_provider.rs b/profcollectd/libprofcollectd/trace_provider.rs
index 704eb9c..133fa5c 100644
--- a/profcollectd/libprofcollectd/trace_provider.rs
+++ b/profcollectd/libprofcollectd/trace_provider.rs
@@ -30,7 +30,7 @@
 pub trait TraceProvider {
     fn get_name(&self) -> &'static str;
     fn is_ready(&self) -> bool;
-    fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration);
+    fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration, binary_filter: &str);
     fn process(&self, trace_dir: &Path, profile_dir: &Path, binary_filter: &str) -> Result<()>;
     fn set_log_file(&self, filename: &Path);
     fn reset_log_file(&self);
diff --git a/simpleperf/include/simpleperf_profcollect.hpp b/simpleperf/include/simpleperf_profcollect.hpp
index d06ac6b..d1e6661 100644
--- a/simpleperf/include/simpleperf_profcollect.hpp
+++ b/simpleperf/include/simpleperf_profcollect.hpp
@@ -18,7 +18,7 @@
 
 bool HasDriverSupport();
 bool HasDeviceSupport();
-bool Record(const char* event_name, const char* output, float duration);
+bool Record(const char* event_name, const char* output, float duration, const char* binary_filter);
 bool Inject(const char* traceInput, const char* profileOutput, const char* binary_filter);
 void SetLogFile(const char* filename);
 void ResetLogFile();
diff --git a/simpleperf/profcollect.cpp b/simpleperf/profcollect.cpp
index ee3a21e..9cffbdd 100644
--- a/simpleperf/profcollect.cpp
+++ b/simpleperf/profcollect.cpp
@@ -52,8 +52,9 @@
   return ret;
 }
 
-bool Record(const char* event_name, const char* output, float duration) {
-  LOG(INFO) << "Record " << event_name << ", duration " << duration << ", output " << output;
+bool Record(const char* event_name, const char* output, float duration, const char* binary_filter) {
+  LOG(INFO) << "Record " << event_name << ", duration " << duration << ", output " << output
+            << ", binary_filter " << binary_filter;
   // The kernel may panic when trying to hibernate or hotplug CPUs while collecting
   // ETM data. So get wakelock to keep the CPUs on.
   auto wakelock = android::wakelock::WakeLock::tryGet("profcollectd");
@@ -62,11 +63,17 @@
     return false;
   }
   auto recordCmd = CreateCommandInstance("record");
-  std::vector<std::string> args;
-  args.push_back("-a");
-  args.insert(args.end(), {"-e", event_name});
-  args.insert(args.end(), {"--duration", std::to_string(duration)});
-  args.insert(args.end(), {"-o", output});
+  std::vector<std::string> args = {"-a",
+                                   "-e",
+                                   event_name,
+                                   "--duration",
+                                   std::to_string(duration),
+                                   "--decode-etm",
+                                   "--exclude-perf",
+                                   "--binary",
+                                   binary_filter,
+                                   "-o",
+                                   output};
   bool result = recordCmd->Run(args);
   LOG(INFO) << "Record result " << result;
   return result;
@@ -76,14 +83,8 @@
   LOG(INFO) << "Inject traceInput " << traceInput << ", profileOutput " << profileOutput
             << ", binary_filter " << binary_filter;
   auto injectCmd = CreateCommandInstance("inject");
-  std::vector<std::string> args;
-  args.insert(args.end(), {"-i", traceInput});
-  args.insert(args.end(), {"-o", profileOutput});
-  if (binary_filter) {
-    args.insert(args.end(), {"--binary", binary_filter});
-  }
-  args.insert(args.end(), {"--output", "branch-list"});
-  args.emplace_back("--exclude-perf");
+  std::vector<std::string> args = {"-i",       traceInput,    "-o",       profileOutput,
+                                   "--output", "branch-list", "--binary", binary_filter};
   bool result = injectCmd->Run(args);
   LOG(INFO) << "Inject result " << result;
   return result;
diff --git a/simpleperf/rust/lib.rs b/simpleperf/rust/lib.rs
index c1f35e0..94ad879 100644
--- a/simpleperf/rust/lib.rs
+++ b/simpleperf/rust/lib.rs
@@ -48,7 +48,7 @@
 }
 
 /// Trigger an ETM trace event.
-pub fn record(trace_file: &Path, duration: &Duration, scope: RecordScope) {
+pub fn record(trace_file: &Path, duration: &Duration, binary_filter: &str, scope: RecordScope) {
     let event_name: CString = match scope {
         RecordScope::USERSPACE => CString::new("cs-etm:u").unwrap(),
         RecordScope::KERNEL => CString::new("cs-etm:k").unwrap(),
@@ -56,9 +56,15 @@
     };
     let trace_file = path_to_cstr(trace_file);
     let duration = duration.as_secs_f32();
+    let binary_filter = CString::new(binary_filter).unwrap();
 
     unsafe {
-        simpleperf_profcollect_bindgen::Record(event_name.as_ptr(), trace_file.as_ptr(), duration);
+        simpleperf_profcollect_bindgen::Record(
+            event_name.as_ptr(),
+            trace_file.as_ptr(),
+            duration,
+            binary_filter.as_ptr(),
+        );
     }
 }