Jaewan Kim | c03f661 | 2023-02-20 00:06:26 +0900 | [diff] [blame] | 1 | // Copyright 2023, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Functions for AVF debug policy and debug level |
| 16 | |
| 17 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
Jaewan Kim | 61f8614 | 2023-03-28 15:12:52 +0900 | [diff] [blame^] | 18 | VirtualMachineAppConfig::DebugLevel::DebugLevel, |
Jaewan Kim | c03f661 | 2023-02-20 00:06:26 +0900 | [diff] [blame] | 19 | }; |
| 20 | use std::fs::File; |
| 21 | use std::io::Read; |
Jaewan Kim | 61f8614 | 2023-03-28 15:12:52 +0900 | [diff] [blame^] | 22 | use log::info; |
| 23 | use rustutils::system_properties; |
| 24 | |
| 25 | const DEBUG_POLICY_LOG_PATH: &str = "/proc/device-tree/avf/guest/common/log"; |
| 26 | const DEBUG_POLICY_RAMDUMP_PATH: &str = "/proc/device-tree/avf/guest/common/ramdump"; |
| 27 | const DEBUG_POLICY_ADB_PATH: &str = "/proc/device-tree/avf/guest/microdroid/adb"; |
| 28 | |
| 29 | const SYSPROP_CUSTOM_DEBUG_POLICY_PATH: &str = "hypervisor.virtualizationmanager.debug_policy.path"; |
| 30 | |
| 31 | /// Debug configurations for both debug level and debug policy |
| 32 | #[derive(Debug)] |
| 33 | pub struct DebugConfig { |
| 34 | pub debug_level: DebugLevel, |
| 35 | debug_policy_log: bool, |
| 36 | debug_policy_ramdump: bool, |
| 37 | debug_policy_adb: bool, |
| 38 | } |
Jaewan Kim | c03f661 | 2023-02-20 00:06:26 +0900 | [diff] [blame] | 39 | |
| 40 | /// Get debug policy value in bool. It's true iff the value is explicitly set to <1>. |
| 41 | fn get_debug_policy_bool(path: &'static str) -> Option<bool> { |
Jaewan Kim | 66f062e | 2023-02-25 01:07:43 +0900 | [diff] [blame] | 42 | let mut file = File::open(path).ok()?; |
| 43 | let mut log: [u8; 4] = Default::default(); |
| 44 | file.read_exact(&mut log).ok()?; |
| 45 | // DT spec uses big endian although Android is always little endian. |
| 46 | Some(u32::from_be_bytes(log) == 1) |
Jaewan Kim | c03f661 | 2023-02-20 00:06:26 +0900 | [diff] [blame] | 47 | } |
| 48 | |
Jaewan Kim | 61f8614 | 2023-03-28 15:12:52 +0900 | [diff] [blame^] | 49 | impl DebugConfig { |
| 50 | pub fn new(debug_level: DebugLevel) -> Self { |
| 51 | match system_properties::read(SYSPROP_CUSTOM_DEBUG_POLICY_PATH).unwrap_or_default() { |
| 52 | Some(debug_policy_path) if !debug_policy_path.is_empty() => { |
| 53 | // TODO: Read debug policy file and override log, adb, ramdump for testing. |
| 54 | info!("Debug policy is disabled by sysprop"); |
| 55 | Self { |
| 56 | debug_level, |
| 57 | debug_policy_log: false, |
| 58 | debug_policy_ramdump: false, |
| 59 | debug_policy_adb: false, |
| 60 | } |
| 61 | } |
| 62 | _ => { |
| 63 | let debug_config = Self { |
| 64 | debug_level, |
| 65 | debug_policy_log: get_debug_policy_bool(DEBUG_POLICY_LOG_PATH) |
| 66 | .unwrap_or_default(), |
| 67 | debug_policy_ramdump: get_debug_policy_bool(DEBUG_POLICY_RAMDUMP_PATH) |
| 68 | .unwrap_or_default(), |
| 69 | debug_policy_adb: get_debug_policy_bool(DEBUG_POLICY_ADB_PATH) |
| 70 | .unwrap_or_default(), |
| 71 | }; |
| 72 | info!("Loaded debug policy from host OS: {:?}", debug_config); |
Jaewan Kim | b24d1dc | 2023-02-13 15:34:56 +0900 | [diff] [blame] | 73 | |
Jaewan Kim | 61f8614 | 2023-03-28 15:12:52 +0900 | [diff] [blame^] | 74 | debug_config |
| 75 | } |
| 76 | } |
| 77 | } |
Jiyong Park | ed18093 | 2023-02-24 19:55:41 +0900 | [diff] [blame] | 78 | |
Jaewan Kim | 61f8614 | 2023-03-28 15:12:52 +0900 | [diff] [blame^] | 79 | /// Get whether console output should be configred for VM to leave console and adb log. |
| 80 | /// Caller should create pipe and prepare for receiving VM log with it. |
| 81 | pub fn should_prepare_console_output(&self) -> bool { |
| 82 | self.debug_level != DebugLevel::NONE || self.debug_policy_log || self.debug_policy_adb |
| 83 | } |
Jiyong Park | ed18093 | 2023-02-24 19:55:41 +0900 | [diff] [blame] | 84 | |
Jaewan Kim | 61f8614 | 2023-03-28 15:12:52 +0900 | [diff] [blame^] | 85 | /// Get whether debug apexes (MICRODROID_REQUIRED_APEXES_DEBUG) are required. |
| 86 | pub fn should_include_debug_apexes(&self) -> bool { |
| 87 | self.debug_level != DebugLevel::NONE || self.debug_policy_adb |
| 88 | } |
| 89 | |
| 90 | /// Decision to support ramdump |
| 91 | pub fn is_ramdump_needed(&self) -> bool { |
| 92 | self.debug_level != DebugLevel::NONE || self.debug_policy_ramdump |
| 93 | } |
Jiyong Park | ed18093 | 2023-02-24 19:55:41 +0900 | [diff] [blame] | 94 | } |