1*288bf522SAndroid Build Coastguard Worker // 2*288bf522SAndroid Build Coastguard Worker // Copyright (C) 2021 The Android Open Source Project 3*288bf522SAndroid Build Coastguard Worker // 4*288bf522SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); 5*288bf522SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License. 6*288bf522SAndroid Build Coastguard Worker // You may obtain a copy of the License at 7*288bf522SAndroid Build Coastguard Worker // 8*288bf522SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0 9*288bf522SAndroid Build Coastguard Worker // 10*288bf522SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 11*288bf522SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 12*288bf522SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*288bf522SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 14*288bf522SAndroid Build Coastguard Worker // limitations under the License. 15*288bf522SAndroid Build Coastguard Worker // 16*288bf522SAndroid Build Coastguard Worker 17*288bf522SAndroid Build Coastguard Worker //! This module implements safe wrappers for simpleperf operations required 18*288bf522SAndroid Build Coastguard Worker //! by profcollect. 19*288bf522SAndroid Build Coastguard Worker 20*288bf522SAndroid Build Coastguard Worker use std::ffi::{c_char, CString}; 21*288bf522SAndroid Build Coastguard Worker use std::path::Path; 22*288bf522SAndroid Build Coastguard Worker path_to_cstr(path: &Path) -> CString23*288bf522SAndroid Build Coastguard Workerfn path_to_cstr(path: &Path) -> CString { 24*288bf522SAndroid Build Coastguard Worker CString::new(path.to_str().unwrap()).unwrap() 25*288bf522SAndroid Build Coastguard Worker } 26*288bf522SAndroid Build Coastguard Worker 27*288bf522SAndroid Build Coastguard Worker /// Returns whether the system has etm driver. ETM driver should be available immediately 28*288bf522SAndroid Build Coastguard Worker /// after boot. is_etm_driver_available() -> bool29*288bf522SAndroid Build Coastguard Workerpub fn is_etm_driver_available() -> bool { 30*288bf522SAndroid Build Coastguard Worker // SAFETY: This is always safe to call. 31*288bf522SAndroid Build Coastguard Worker unsafe { simpleperf_profcollect_bindgen::IsETMDriverAvailable() } 32*288bf522SAndroid Build Coastguard Worker } 33*288bf522SAndroid Build Coastguard Worker 34*288bf522SAndroid Build Coastguard Worker /// Returns whether the system has etm device. ETM device may not be available immediately 35*288bf522SAndroid Build Coastguard Worker /// after boot. is_etm_device_available() -> bool36*288bf522SAndroid Build Coastguard Workerpub fn is_etm_device_available() -> bool { 37*288bf522SAndroid Build Coastguard Worker // SAFETY: This is always safe to call. 38*288bf522SAndroid Build Coastguard Worker unsafe { simpleperf_profcollect_bindgen::IsETMDeviceAvailable() } 39*288bf522SAndroid Build Coastguard Worker } 40*288bf522SAndroid Build Coastguard Worker 41*288bf522SAndroid Build Coastguard Worker /// Returns whether the system support LBR recording. is_lbr_available() -> bool42*288bf522SAndroid Build Coastguard Workerpub fn is_lbr_available() -> bool { 43*288bf522SAndroid Build Coastguard Worker // SAFETY: This is always safe to call. 44*288bf522SAndroid Build Coastguard Worker unsafe { simpleperf_profcollect_bindgen::IsLBRAvailable() } 45*288bf522SAndroid Build Coastguard Worker } 46*288bf522SAndroid Build Coastguard Worker 47*288bf522SAndroid Build Coastguard Worker /// Run the record command to record ETM/LBR data. run_record_cmd(args: &[&str]) -> bool48*288bf522SAndroid Build Coastguard Workerpub fn run_record_cmd(args: &[&str]) -> bool { 49*288bf522SAndroid Build Coastguard Worker let c_args: Vec<CString> = args.iter().map(|s| CString::new(s.as_bytes()).unwrap()).collect(); 50*288bf522SAndroid Build Coastguard Worker let mut pointer_args: Vec<*const c_char> = c_args.iter().map(|s| s.as_ptr()).collect(); 51*288bf522SAndroid Build Coastguard Worker let arg_count: i32 = pointer_args.len().try_into().unwrap(); 52*288bf522SAndroid Build Coastguard Worker // SAFETY: pointer_args is an array of valid C strings. Its length is defined by arg_count. 53*288bf522SAndroid Build Coastguard Worker unsafe { simpleperf_profcollect_bindgen::RunRecordCmd(pointer_args.as_mut_ptr(), arg_count) } 54*288bf522SAndroid Build Coastguard Worker } 55*288bf522SAndroid Build Coastguard Worker 56*288bf522SAndroid Build Coastguard Worker /// Run the inject command to process ETM/LBR data. run_inject_cmd(args: &[&str]) -> bool57*288bf522SAndroid Build Coastguard Workerpub fn run_inject_cmd(args: &[&str]) -> bool { 58*288bf522SAndroid Build Coastguard Worker let c_args: Vec<CString> = args.iter().map(|s| CString::new(s.as_bytes()).unwrap()).collect(); 59*288bf522SAndroid Build Coastguard Worker let mut pointer_args: Vec<*const c_char> = c_args.iter().map(|s| s.as_ptr()).collect(); 60*288bf522SAndroid Build Coastguard Worker let arg_count: i32 = pointer_args.len().try_into().unwrap(); 61*288bf522SAndroid Build Coastguard Worker // SAFETY: pointer_args is an array of valid C strings. Its length is defined by arg_count. 62*288bf522SAndroid Build Coastguard Worker unsafe { simpleperf_profcollect_bindgen::RunInjectCmd(pointer_args.as_mut_ptr(), arg_count) } 63*288bf522SAndroid Build Coastguard Worker } 64*288bf522SAndroid Build Coastguard Worker 65*288bf522SAndroid Build Coastguard Worker /// Save logs in file. set_log_file(filename: &Path)66*288bf522SAndroid Build Coastguard Workerpub fn set_log_file(filename: &Path) { 67*288bf522SAndroid Build Coastguard Worker let log_file = path_to_cstr(filename); 68*288bf522SAndroid Build Coastguard Worker // SAFETY: The pointer is a valid C string, and isn't retained after the function call returns. 69*288bf522SAndroid Build Coastguard Worker unsafe { 70*288bf522SAndroid Build Coastguard Worker simpleperf_profcollect_bindgen::SetLogFile(log_file.as_ptr()); 71*288bf522SAndroid Build Coastguard Worker } 72*288bf522SAndroid Build Coastguard Worker } 73*288bf522SAndroid Build Coastguard Worker 74*288bf522SAndroid Build Coastguard Worker /// Stop using log file. reset_log_file()75*288bf522SAndroid Build Coastguard Workerpub fn reset_log_file() { 76*288bf522SAndroid Build Coastguard Worker // SAFETY: This is always safe to call. 77*288bf522SAndroid Build Coastguard Worker unsafe { 78*288bf522SAndroid Build Coastguard Worker simpleperf_profcollect_bindgen::ResetLogFile(); 79*288bf522SAndroid Build Coastguard Worker } 80*288bf522SAndroid Build Coastguard Worker } 81