blob: e8863c7f2a5f248620c69a0eba52f037b3dc14fb [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::{
Jaewan Kim61f86142023-03-28 15:12:52 +090018 VirtualMachineAppConfig::DebugLevel::DebugLevel,
Jaewan Kimc03f6612023-02-20 00:06:26 +090019};
20use std::fs::File;
21use std::io::Read;
Jaewan Kim61f86142023-03-28 15:12:52 +090022use log::info;
23use rustutils::system_properties;
24
25const DEBUG_POLICY_LOG_PATH: &str = "/proc/device-tree/avf/guest/common/log";
26const DEBUG_POLICY_RAMDUMP_PATH: &str = "/proc/device-tree/avf/guest/common/ramdump";
27const DEBUG_POLICY_ADB_PATH: &str = "/proc/device-tree/avf/guest/microdroid/adb";
28
29const 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)]
33pub struct DebugConfig {
34 pub debug_level: DebugLevel,
35 debug_policy_log: bool,
36 debug_policy_ramdump: bool,
37 debug_policy_adb: bool,
38}
Jaewan Kimc03f6612023-02-20 00:06:26 +090039
40/// Get debug policy value in bool. It's true iff the value is explicitly set to <1>.
41fn get_debug_policy_bool(path: &'static str) -> Option<bool> {
Jaewan Kim66f062e2023-02-25 01:07:43 +090042 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 Kimc03f6612023-02-20 00:06:26 +090047}
48
Jaewan Kim61f86142023-03-28 15:12:52 +090049impl 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 Kimb24d1dc2023-02-13 15:34:56 +090073
Jaewan Kim61f86142023-03-28 15:12:52 +090074 debug_config
75 }
76 }
77 }
Jiyong Parked180932023-02-24 19:55:41 +090078
Jaewan Kim61f86142023-03-28 15:12:52 +090079 /// 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 Parked180932023-02-24 19:55:41 +090084
Jaewan Kim61f86142023-03-28 15:12:52 +090085 /// 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 Parked180932023-02-24 19:55:41 +090094}