• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
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 
17 //! This module implements safe wrappers for simpleperf operations required
18 //! by profcollect.
19 
20 use std::ffi::{c_char, CString};
21 use std::path::Path;
22 
path_to_cstr(path: &Path) -> CString23 fn path_to_cstr(path: &Path) -> CString {
24     CString::new(path.to_str().unwrap()).unwrap()
25 }
26 
27 /// Returns whether the system has etm driver. ETM driver should be available immediately
28 /// after boot.
is_etm_driver_available() -> bool29 pub fn is_etm_driver_available() -> bool {
30     // SAFETY: This is always safe to call.
31     unsafe { simpleperf_profcollect_bindgen::IsETMDriverAvailable() }
32 }
33 
34 /// Returns whether the system has etm device. ETM device may not be available immediately
35 /// after boot.
is_etm_device_available() -> bool36 pub fn is_etm_device_available() -> bool {
37     // SAFETY: This is always safe to call.
38     unsafe { simpleperf_profcollect_bindgen::IsETMDeviceAvailable() }
39 }
40 
41 /// Returns whether the system support LBR recording.
is_lbr_available() -> bool42 pub fn is_lbr_available() -> bool {
43     // SAFETY: This is always safe to call.
44     unsafe { simpleperf_profcollect_bindgen::IsLBRAvailable() }
45 }
46 
47 /// Run the record command to record ETM/LBR data.
run_record_cmd(args: &[&str]) -> bool48 pub 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();
52     // SAFETY: pointer_args is an array of valid C strings. Its length is defined by arg_count.
53     unsafe { simpleperf_profcollect_bindgen::RunRecordCmd(pointer_args.as_mut_ptr(), arg_count) }
54 }
55 
56 /// Run the inject command to process ETM/LBR data.
run_inject_cmd(args: &[&str]) -> bool57 pub 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();
61     // SAFETY: pointer_args is an array of valid C strings. Its length is defined by arg_count.
62     unsafe { simpleperf_profcollect_bindgen::RunInjectCmd(pointer_args.as_mut_ptr(), arg_count) }
63 }
64 
65 /// Save logs in file.
set_log_file(filename: &Path)66 pub fn set_log_file(filename: &Path) {
67     let log_file = path_to_cstr(filename);
68     // SAFETY: The pointer is a valid C string, and isn't retained after the function call returns.
69     unsafe {
70         simpleperf_profcollect_bindgen::SetLogFile(log_file.as_ptr());
71     }
72 }
73 
74 /// Stop using log file.
reset_log_file()75 pub fn reset_log_file() {
76     // SAFETY: This is always safe to call.
77     unsafe {
78         simpleperf_profcollect_bindgen::ResetLogFile();
79     }
80 }
81