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::{ |
| 18 | VirtualMachineAppConfig::DebugLevel::DebugLevel |
| 19 | }; |
| 20 | use std::fs::File; |
| 21 | use std::io::Read; |
| 22 | |
| 23 | /// Get debug policy value in bool. It's true iff the value is explicitly set to <1>. |
| 24 | fn get_debug_policy_bool(path: &'static str) -> Option<bool> { |
| 25 | if let Ok(mut file) = File::open(path) { |
| 26 | let mut log: [u8; 4] = Default::default(); |
| 27 | file.read_exact(&mut log).map_err(|_| false).unwrap(); |
| 28 | // DT spec uses big endian although Android is always little endian. |
| 29 | return Some(u32::from_be_bytes(log) == 1); |
| 30 | } |
| 31 | None |
| 32 | } |
| 33 | |
| 34 | /// Get whether console output should be configred for VM to leave console and adb log. |
| 35 | /// Caller should create pipe and prepare for receiving VM log with it. |
| 36 | pub fn should_prepare_console_output(debug_level: DebugLevel) -> bool { |
| 37 | debug_level != DebugLevel::NONE |
| 38 | || get_debug_policy_bool("/proc/device-tree/avf/guest/common/log").unwrap_or_default() |
| 39 | } |