lldb.rs

 1use std::{collections::HashMap, ffi::OsStr, path::PathBuf};
 2
 3use anyhow::Result;
 4use async_trait::async_trait;
 5use gpui::AsyncApp;
 6use sysinfo::{Pid, Process};
 7use task::{DebugAdapterConfig, DebugRequestType};
 8
 9use crate::*;
10
11pub(crate) struct LldbDebugAdapter {}
12
13impl LldbDebugAdapter {
14    const ADAPTER_NAME: &'static str = "lldb";
15
16    pub(crate) fn new() -> Self {
17        LldbDebugAdapter {}
18    }
19
20    pub fn attach_processes(processes: &HashMap<Pid, Process>) -> Vec<(&Pid, &Process)> {
21        processes.iter().collect::<Vec<_>>()
22    }
23}
24
25#[async_trait(?Send)]
26impl DebugAdapter for LldbDebugAdapter {
27    fn name(&self) -> DebugAdapterName {
28        DebugAdapterName(Self::ADAPTER_NAME.into())
29    }
30
31    async fn get_binary(
32        &self,
33        delegate: &dyn DapDelegate,
34        config: &DebugAdapterConfig,
35        user_installed_path: Option<PathBuf>,
36        _: &mut AsyncApp,
37    ) -> Result<DebugAdapterBinary> {
38        let lldb_dap_path = if let Some(user_installed_path) = user_installed_path {
39            user_installed_path.to_string_lossy().into()
40        } else {
41            delegate
42                .which(OsStr::new("lldb-dap"))
43                .and_then(|p| p.to_str().map(|s| s.to_string()))
44                .ok_or(anyhow!("Could not find lldb-dap in path"))?
45        };
46
47        Ok(DebugAdapterBinary {
48            command: lldb_dap_path,
49            arguments: None,
50            envs: None,
51            cwd: config.cwd.clone(),
52            connection: None,
53        })
54    }
55
56    async fn install_binary(
57        &self,
58        _version: AdapterVersion,
59        _delegate: &dyn DapDelegate,
60    ) -> Result<()> {
61        unimplemented!("LLDB debug adapter cannot be installed by Zed (yet)")
62    }
63
64    async fn fetch_latest_adapter_version(&self, _: &dyn DapDelegate) -> Result<AdapterVersion> {
65        unimplemented!("Fetch latest adapter version not implemented for lldb (yet)")
66    }
67
68    async fn get_installed_binary(
69        &self,
70        _: &dyn DapDelegate,
71        _: &DebugAdapterConfig,
72        _: Option<PathBuf>,
73        _: &mut AsyncApp,
74    ) -> Result<DebugAdapterBinary> {
75        unimplemented!("LLDB debug adapter cannot be installed by Zed (yet)")
76    }
77
78    fn request_args(&self, config: &DebugAdapterConfig) -> Value {
79        let pid = if let DebugRequestType::Attach(attach_config) = &config.request {
80            attach_config.process_id
81        } else {
82            None
83        };
84
85        json!({
86            "program": config.program,
87            "request": match config.request {
88                DebugRequestType::Launch => "launch",
89                DebugRequestType::Attach(_) => "attach",
90            },
91            "pid": pid,
92            "cwd": config.cwd,
93        })
94    }
95}