blob: c5fe5daca02aeeb0cac4cc1c57c12d815c81b87d [file] [log] [blame]
Yi Kongf8abfdb2021-02-18 16:23:37 +08001//
2// Copyright (C) 2021 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
Yabin Cuiffc25f82023-12-13 11:04:40 -080017//! This module implements safe wrappers for simpleperf operations required
Yi Kongf8abfdb2021-02-18 16:23:37 +080018//! by profcollect.
19
Yabin Cuiffc25f82023-12-13 11:04:40 -080020use std::ffi::{c_char, CString};
Yi Kongf8abfdb2021-02-18 16:23:37 +080021use std::path::Path;
Yi Kongf8abfdb2021-02-18 16:23:37 +080022
23fn path_to_cstr(path: &Path) -> CString {
24 CString::new(path.to_str().unwrap()).unwrap()
25}
26
Yabin Cuif158a752022-01-10 15:35:59 -080027/// Returns whether the system has etm driver. ETM driver should be available immediately
28/// after boot.
Yabin Cuiffc25f82023-12-13 11:04:40 -080029pub fn is_etm_driver_available() -> bool {
Andrew Walbranb044aad2023-07-14 13:58:29 +010030 // SAFETY: This is always safe to call.
Yabin Cuiffc25f82023-12-13 11:04:40 -080031 unsafe { simpleperf_profcollect_bindgen::IsETMDriverAvailable() }
Yabin Cuif158a752022-01-10 15:35:59 -080032}
33
34/// Returns whether the system has etm device. ETM device may not be available immediately
35/// after boot.
Yabin Cuiffc25f82023-12-13 11:04:40 -080036pub fn is_etm_device_available() -> bool {
Andrew Walbranb044aad2023-07-14 13:58:29 +010037 // SAFETY: This is always safe to call.
Yabin Cuiffc25f82023-12-13 11:04:40 -080038 unsafe { simpleperf_profcollect_bindgen::IsETMDeviceAvailable() }
Yi Kongf8abfdb2021-02-18 16:23:37 +080039}
40
Yabin Cuiffc25f82023-12-13 11:04:40 -080041/// Returns whether the system support LBR recording.
42pub fn is_lbr_available() -> bool {
43 // SAFETY: This is always safe to call.
44 unsafe { simpleperf_profcollect_bindgen::IsLBRAvailable() }
Yabin Cui4abcd872021-03-23 15:51:08 -070045}
46
Yabin Cuiffc25f82023-12-13 11:04:40 -080047/// Run the record command to record ETM/LBR data.
48pub fn run_record_cmd(args: &[&str]) -> bool {
49 let c_args: Vec<CString> = args.iter().map(|s| CString::new(s.as_bytes()).unwrap()).collect();
50 let mut pointer_args: Vec<*const c_char> = c_args.iter().map(|s| s.as_ptr()).collect();
51 let arg_count: i32 = pointer_args.len().try_into().unwrap();
Yabin Cui101d2aa2023-12-18 14:42:47 -080052 // SAFETY: pointer_args is an array of valid C strings. Its length is defined by arg_count.
Yabin Cuiffc25f82023-12-13 11:04:40 -080053 unsafe { simpleperf_profcollect_bindgen::RunRecordCmd(pointer_args.as_mut_ptr(), arg_count) }
Yi Kongf8abfdb2021-02-18 16:23:37 +080054}
55
Yabin Cuiffc25f82023-12-13 11:04:40 -080056/// Run the inject command to process ETM/LBR data.
57pub fn run_inject_cmd(args: &[&str]) -> bool {
58 let c_args: Vec<CString> = args.iter().map(|s| CString::new(s.as_bytes()).unwrap()).collect();
59 let mut pointer_args: Vec<*const c_char> = c_args.iter().map(|s| s.as_ptr()).collect();
60 let arg_count: i32 = pointer_args.len().try_into().unwrap();
Yabin Cui101d2aa2023-12-18 14:42:47 -080061 // SAFETY: pointer_args is an array of valid C strings. Its length is defined by arg_count.
Yabin Cuiffc25f82023-12-13 11:04:40 -080062 unsafe { simpleperf_profcollect_bindgen::RunInjectCmd(pointer_args.as_mut_ptr(), arg_count) }
Yi Kongf8abfdb2021-02-18 16:23:37 +080063}
Yabin Cuif1d91d22023-04-27 12:50:59 -070064
65/// Save logs in file.
66pub fn set_log_file(filename: &Path) {
67 let log_file = path_to_cstr(filename);
Andrew Walbranb044aad2023-07-14 13:58:29 +010068 // SAFETY: The pointer is a valid C string, and isn't retained after the function call returns.
Yabin Cuif1d91d22023-04-27 12:50:59 -070069 unsafe {
70 simpleperf_profcollect_bindgen::SetLogFile(log_file.as_ptr());
71 }
72}
73
74/// Stop using log file.
75pub fn reset_log_file() {
Andrew Walbranb044aad2023-07-14 13:58:29 +010076 // SAFETY: This is always safe to call.
Yabin Cuif1d91d22023-04-27 12:50:59 -070077 unsafe {
78 simpleperf_profcollect_bindgen::ResetLogFile();
79 }
80}