Skip to main content

strat9_kernel/shell/commands/sys/
silo_attach.rs

1use super::*;
2
3/// Attach to a silo's debug output stream.
4///
5/// Usage: `silo attach <id|label|name>`
6///
7/// Displays output from `sys_debug_log` calls made by tasks in the silo.
8/// Press Ctrl+C or 'q' to detach.
9pub(super) fn cmd_silo_attach(args: &[String]) -> Result<(), ShellError> {
10    if args.len() < 2 {
11        shell_println!("Usage: silo attach <id|label|name>");
12        return Err(ShellError::InvalidArguments);
13    }
14    let selector = normalize_current_silo_selector(args[1].as_str());
15
16    let sid = match silo::silo_detail_snapshot(selector.as_str()) {
17        Ok(detail) => {
18            shell_println!(
19                "Attached to silo {} ({}). Press Ctrl+C or 'q' to detach.",
20                detail.base.id,
21                detail.base.name
22            );
23            detail.base.id
24        }
25        Err(e) => {
26            shell_println!("silo attach: {:?}", e);
27            return Err(ShellError::ExecutionFailed);
28        }
29    };
30
31    loop {
32        if let Some(ch) = crate::arch::x86_64::keyboard::read_char() {
33            if ch == b'q' || ch == 0x03 || ch == 0x1B {
34                break;
35            }
36        }
37
38        match silo::silo_output_drain(&alloc::format!("{}", sid)) {
39            Ok(data) if !data.is_empty() => {
40                if let Ok(s) = core::str::from_utf8(&data) {
41                    crate::shell_print!("{}", s);
42                }
43            }
44            _ => {}
45        }
46
47        crate::process::yield_task();
48    }
49
50    shell_println!("\nDetached from silo {}", sid);
51    Ok(())
52}