blob: bca8f7540de0ec359078f3608e2f8208d87e5e99 [file] [log] [blame]
Jaewan Kimc03f6612023-02-20 00:06:26 +09001// 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
17use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
Jiyong Parked180932023-02-24 19:55:41 +090018 VirtualMachineAppConfig::DebugLevel::DebugLevel, VirtualMachineConfig::VirtualMachineConfig,
Jaewan Kimc03f6612023-02-20 00:06:26 +090019};
20use std::fs::File;
21use std::io::Read;
22
23/// Get debug policy value in bool. It's true iff the value is explicitly set to <1>.
24fn get_debug_policy_bool(path: &'static str) -> Option<bool> {
Jaewan Kim66f062e2023-02-25 01:07:43 +090025 let mut file = File::open(path).ok()?;
26 let mut log: [u8; 4] = Default::default();
27 file.read_exact(&mut log).ok()?;
28 // DT spec uses big endian although Android is always little endian.
29 Some(u32::from_be_bytes(log) == 1)
Jaewan Kimc03f6612023-02-20 00:06:26 +090030}
31
32/// Get whether console output should be configred for VM to leave console and adb log.
33/// Caller should create pipe and prepare for receiving VM log with it.
34pub fn should_prepare_console_output(debug_level: DebugLevel) -> bool {
35 debug_level != DebugLevel::NONE
36 || get_debug_policy_bool("/proc/device-tree/avf/guest/common/log").unwrap_or_default()
Jaewan Kimb24d1dc2023-02-13 15:34:56 +090037 || get_debug_policy_bool("/proc/device-tree/avf/guest/microdroid/adb").unwrap_or_default()
38}
39
40/// Get whether debug apexes (MICRODROID_REQUIRED_APEXES_DEBUG) are required.
41pub fn should_include_debug_apexes(debug_level: DebugLevel) -> bool {
42 debug_level != DebugLevel::NONE
43 || get_debug_policy_bool("/proc/device-tree/avf/guest/microdroid/adb").unwrap_or_default()
Jaewan Kimc03f6612023-02-20 00:06:26 +090044}
Jiyong Parked180932023-02-24 19:55:41 +090045
46/// Decision to support ramdump
47pub fn is_ramdump_needed(config: &VirtualMachineConfig) -> bool {
48 let enabled_in_dp =
49 get_debug_policy_bool("/proc/device-tree/avf/guest/common/ramdump").unwrap_or_default();
50 let (protected, debuggable) = match config {
51 VirtualMachineConfig::RawConfig(config) => {
52 // custom VMs are considered debuggable for flexibility
53 (config.protectedVm, true)
54 }
55 VirtualMachineConfig::AppConfig(config) => {
56 (config.protectedVm, config.debugLevel == DebugLevel::FULL)
57 }
58 };
59
60 if protected {
61 enabled_in_dp
62 } else {
63 enabled_in_dp || debuggable
64 }
65}