vmconfig: Introduce get_debug_level()

Add helper function to centralize deducing the debug level from a VM
config (either app or raw).

Test: m libvmconfig vm virtmgr
Change-Id: Ia04202fe3b1659b0428fd772e83e075d329f4cca
diff --git a/libs/vmconfig/src/lib.rs b/libs/vmconfig/src/lib.rs
index 907e0d3..7c917b0 100644
--- a/libs/vmconfig/src/lib.rs
+++ b/libs/vmconfig/src/lib.rs
@@ -18,6 +18,8 @@
     aidl::android::system::virtualizationservice::CpuTopology::CpuTopology,
     aidl::android::system::virtualizationservice::DiskImage::DiskImage as AidlDiskImage,
     aidl::android::system::virtualizationservice::Partition::Partition as AidlPartition,
+    aidl::android::system::virtualizationservice::VirtualMachineAppConfig::DebugLevel::DebugLevel,
+    aidl::android::system::virtualizationservice::VirtualMachineConfig::VirtualMachineConfig,
     aidl::android::system::virtualizationservice::VirtualMachineRawConfig::VirtualMachineRawConfig,
     binder::ParcelFileDescriptor,
 };
@@ -127,6 +129,14 @@
     }
 }
 
+/// Returns the debug level of the VM from its configuration.
+pub fn get_debug_level(config: &VirtualMachineConfig) -> Option<DebugLevel> {
+    match config {
+        VirtualMachineConfig::AppConfig(config) => Some(config.debugLevel),
+        VirtualMachineConfig::RawConfig(_) => None,
+    }
+}
+
 /// A disk image to be made available to the VM.
 #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
 pub struct DiskImage {
diff --git a/virtualizationmanager/src/aidl.rs b/virtualizationmanager/src/aidl.rs
index c5f1ab7..769ac4c 100644
--- a/virtualizationmanager/src/aidl.rs
+++ b/virtualizationmanager/src/aidl.rs
@@ -89,7 +89,7 @@
 use std::path::{Path, PathBuf};
 use std::sync::{Arc, Mutex, Weak};
 use vbmeta::VbMetaImage;
-use vmconfig::VmConfig;
+use vmconfig::{VmConfig, get_debug_level};
 use vsock::VsockStream;
 use zip::ZipArchive;
 
@@ -1356,17 +1356,12 @@
             .or_binder_exception(ExceptionCode::SECURITY);
     }
 
-    match config {
-        VirtualMachineConfig::RawConfig(_) => Ok(()),
-        VirtualMachineConfig::AppConfig(config) => {
-            if config.debugLevel != DebugLevel::FULL {
-                Err(anyhow!("Can't use gdb with non-debuggable VMs"))
-                    .or_binder_exception(ExceptionCode::SECURITY)
-            } else {
-                Ok(())
-            }
-        }
+    if get_debug_level(config) == Some(DebugLevel::NONE) {
+        return Err(anyhow!("Can't use gdb with non-debuggable VMs"))
+            .or_binder_exception(ExceptionCode::SECURITY);
     }
+
+    Ok(())
 }
 
 fn extract_instance_id(config: &VirtualMachineConfig) -> [u8; 64] {
diff --git a/virtualizationmanager/src/debug_config.rs b/virtualizationmanager/src/debug_config.rs
index a2ea40d..6b74353 100644
--- a/virtualizationmanager/src/debug_config.rs
+++ b/virtualizationmanager/src/debug_config.rs
@@ -26,6 +26,7 @@
 use std::fs;
 use std::io::ErrorKind;
 use std::path::{Path, PathBuf};
+use vmconfig::get_debug_level;
 
 const CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP: &str =
     "hypervisor.virtualizationmanager.debug_policy.path";
@@ -157,10 +158,7 @@
 
 impl DebugConfig {
     pub fn new(config: &VirtualMachineConfig) -> Self {
-        let debug_level = match config {
-            VirtualMachineConfig::AppConfig(config) => config.debugLevel,
-            _ => DebugLevel::NONE,
-        };
+        let debug_level = get_debug_level(config).unwrap_or(DebugLevel::NONE);
 
         let dp_sysprop = system_properties::read(CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP);
         let custom_dp = dp_sysprop.unwrap_or_else(|e| {
diff --git a/vm/src/run.rs b/vm/src/run.rs
index 07e0276..ca3e857 100644
--- a/vm/src/run.rs
+++ b/vm/src/run.rs
@@ -39,7 +39,7 @@
 use std::os::unix::io::{AsRawFd, FromRawFd};
 use std::path::{Path, PathBuf};
 use vmclient::{ErrorCode, VmInstance};
-use vmconfig::{open_parcel_file, VmConfig};
+use vmconfig::{get_debug_level, open_parcel_file, VmConfig};
 use zip::ZipArchive;
 
 /// Run a VM from the given APK, idsig, and config.
@@ -315,10 +315,8 @@
         .context("Failed to create VM")?;
     vm.start().context("Failed to start VM")?;
 
-    let debug_level = match config {
-        VirtualMachineConfig::AppConfig(config) => config.debugLevel,
-        _ => DebugLevel::NONE,
-    };
+    let debug_level = get_debug_level(config).unwrap_or(DebugLevel::NONE);
+
     println!(
         "Created {} from {} with CID {}, state is {}.",
         if debug_level == DebugLevel::FULL { "debuggable VM" } else { "VM" },