Skip to content

Conversation

@arthurp
Copy link
Contributor

@arthurp arthurp commented Feb 9, 2026

This enables stepping debugging on ktests. It is not polished, but is useful.

This includes a large block of code moved from run.rs to gdb.rs. Here is a diff of the actual changes to that code:

Diff of the fragment of `run.rs` to `gdb.rs` (ignoring white space, i.e., `-w`)
--- osdk/src/commands/gdb.orig.rs	2026-02-09 11:03:09.657038950 -0600
+++ osdk/src/commands/gdb.rs	2026-02-09 12:14:35.429719926 -0600
@@ -1,35 +1,40 @@
-fn adapt_for_gdb_server(config: &mut Config, gdb_server_str: &str) -> Option<VscLaunchConfig> {
+/// Adapt a `Config` to enable GDB server based on the provided args. Returns an optional
+/// `VscLaunchConfig` which ensures VSCode launch file cleanup when it is dropped.
+pub fn adapt_for_gdb_server(
+    config: &mut Action,
+    gdb_server_str: &str,
+) -> Option<vsc::VscLaunchConfig> {
     let gdb_server_args = GdbServerArgs::from_str(gdb_server_str);
 
     // Add GDB server arguments to QEMU.
     let qemu_gdb_args = {
         let gdb_stub_addr = gdb_server_args.host_addr.as_str();
-        match gdb::stub_type_of(gdb_stub_addr) {
-            gdb::StubAddrType::Unix => {
+        match stub_type_of(gdb_stub_addr) {
+            StubAddrType::Unix => {
                 format!(
                     " -chardev socket,path={},server=on,wait=off,id=gdb0 -gdb chardev:gdb0",
                     gdb_stub_addr
                 )
             }
-            gdb::StubAddrType::Tcp => {
+            StubAddrType::Tcp => {
                 format!(
                     " -gdb tcp:{}",
-                    gdb::tcp_addr_util::format_tcp_addr(gdb_stub_addr)
+                    tcp_addr_util::format_tcp_addr(gdb_stub_addr)
                 )
             }
         }
     };
-    config.run.qemu.args += &qemu_gdb_args;
+    config.qemu.args += &qemu_gdb_args;
 
     if gdb_server_args.wait_client {
-        config.run.qemu.args += " -S";
+        config.qemu.args += " -S";
     }
 
     if is_tdx_enabled() {
         let target = "-object tdx-guest,";
-        if let Some(pos) = config.run.qemu.args.find(target) {
+        if let Some(pos) = config.qemu.args.find(target) {
             let insert_pos = pos + target.len();
-            config.run.qemu.args.insert_str(insert_pos, "debug=on,");
+            config.qemu.args.insert_str(insert_pos, "debug=on,");
         } else {
             warn_msg!(
                 "TDX is enabled, but the TDX guest object is not found in the QEMU arguments"
@@ -38,29 +43,29 @@
     }
 
     // Ensure debug info added when debugging in the release profile.
-    if config.run.build.profile.contains("release") {
+    if config.build.profile.contains("release") {
         config
-            .run
             .build
             .override_configs
-            .push(format!("profile.{}.debug=true", config.run.build.profile));
+            .push(format!("profile.{}.debug=true", config.build.profile));
     }
 
     gdb_server_args.vsc_launch_file.then(|| {
         vsc::check_gdb_config(&gdb_server_args);
-        let profile = super::util::profile_name_adapter(&config.run.build.profile);
+        let profile = super::util::profile_name_adapter(&config.build.profile);
         vsc::VscLaunchConfig::new(profile, &gdb_server_args.host_addr)
     })
 }
 
-struct GdbServerArgs {
-    host_addr: String,
-    wait_client: bool,
-    vsc_launch_file: bool,
+/// Parsed gdb server args
+pub struct GdbServerArgs {
+    pub host_addr: String,
+    pub wait_client: bool,
+    pub vsc_launch_file: bool,
 }
 
 impl GdbServerArgs {
-    fn from_str(args: &str) -> Self {
+    pub fn from_str(args: &str) -> Self {
         let mut host_addr = ".osdk-gdb-socket".to_string();
         let mut wait_client = false;
         let mut vsc_launch_file = false;
@@ -86,20 +91,21 @@
     }
 }
 
-mod gdb {
+/// Utilities for determining stub address type and tcp formatting
     #[derive(Debug, Clone, Copy, PartialEq, Eq)]
-    pub enum StubAddrType {
+enum StubAddrType {
         Unix, // Unix Domain Socket
         Tcp,  // IP_ADDR:PORT
     }
-    pub fn stub_type_of(stub: &str) -> StubAddrType {
+
+fn stub_type_of(stub: &str) -> StubAddrType {
         if stub.split(':').next_back().unwrap().parse::<u16>().is_ok() {
             return StubAddrType::Tcp;
         }
         StubAddrType::Unix
     }
 
-    pub mod tcp_addr_util {
+mod tcp_addr_util {
         use crate::{error::Errno, error_msg};
         use std::process::exit;
 
@@ -124,9 +130,9 @@
             format!("{}:{}", host, port)
         }
     }
-}
 
-mod vsc {
+/// VS Code launch file generator for GDB
+pub mod vsc {
     use crate::{
         commands::util::bin_file_name,
         util::{get_cargo_metadata, get_kernel_crate},
@@ -137,7 +143,7 @@
         path::Path,
     };
 
-    use super::{GdbServerArgs, gdb};
+    use super::{GdbServerArgs, StubAddrType, stub_type_of, tcp_addr_util};
 
     const VSC_DIR: &str = ".vscode";
 
@@ -159,6 +165,7 @@
         existence: Existence,
         backup_launch_path: Option<String>,
     }
+
     impl VscLaunchConfig {
         pub fn new(profile: &str, addr: &str) -> Self {
             let workspace = workspace_root();
@@ -228,7 +235,7 @@
             error_msg!("GDB server address is required to generate a VSCode launch file");
             exit(Errno::ParseMetadata as _);
         }
-        if gdb::stub_type_of(gdb_stub_addr) != gdb::StubAddrType::Tcp {
+        if stub_type_of(gdb_stub_addr) != StubAddrType::Tcp {
             error_msg!(
                 "Non-TCP GDB server address is not supported under '--gdb-server vscode' currently"
             );
@@ -247,7 +254,7 @@
             .replace("#BIN_NAME#", &bin_file_name())
             .replace(
                 "#ADDR_PORT#",
-                gdb::tcp_addr_util::format_tcp_addr(addr).trim_start_matches(':'),
+                tcp_addr_util::format_tcp_addr(addr).trim_start_matches(':'),
             );
 
         let original_items: Option<Value> = {

It's very likely that I could add automatic tests for this. However, I think learning the framework and writing them would be a significant overhead. If you think that's important, I'll do it.

@arthurp arthurp force-pushed the arthurp/test-debug branch from cb2be27 to aa3cbb5 Compare February 9, 2026 18:13
This enables stepping debugging on ktests. It is not polished,
but is useful.
@arthurp arthurp force-pushed the arthurp/test-debug branch from aa3cbb5 to 3088285 Compare February 9, 2026 18:19
@arthurp arthurp requested review from aanyas72 and ioeddk February 9, 2026 18:21
@arthurp arthurp marked this pull request as ready for review February 9, 2026 18:21
@arthurp arthurp requested a review from a team as a code owner February 9, 2026 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant