Skip to content

Commit

Permalink
dap_adapters: add gdb
Browse files Browse the repository at this point in the history
  • Loading branch information
jansol committed Nov 26, 2024
1 parent 5971d37 commit 9720c3a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/dap_adapters/src/dap_adapters.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod custom;
mod gdb;
mod go;
mod javascript;
mod lldb;
Expand All @@ -12,6 +13,7 @@ use dap::adapters::{
self, AdapterVersion, DapDelegate, DebugAdapter, DebugAdapterBinary, DebugAdapterName,
GithubRepo,
};
use gdb::GdbDebugAdapter;
use go::GoDebugAdapter;
use javascript::JsDebugAdapter;
use lldb::LldbDebugAdapter;
Expand All @@ -33,5 +35,6 @@ pub async fn build_adapter(kind: &DebugAdapterKind) -> Result<Box<dyn DebugAdapt
}
DebugAdapterKind::Lldb => Ok(Box::new(LldbDebugAdapter::new())),
DebugAdapterKind::Go(host) => Ok(Box::new(GoDebugAdapter::new(host).await?)),
DebugAdapterKind::Gdb => Ok(Box::new(GdbDebugAdapter::new())),
}
}
85 changes: 85 additions & 0 deletions crates/dap_adapters/src/gdb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::ffi::OsStr;

use anyhow::Result;
use async_trait::async_trait;
use dap::transport::{StdioTransport, Transport};
use task::DebugAdapterConfig;

use crate::*;

pub(crate) struct GdbDebugAdapter {}

impl GdbDebugAdapter {
const ADAPTER_NAME: &'static str = "gdb";

pub(crate) fn new() -> Self {
GdbDebugAdapter {}
}
}

#[async_trait(?Send)]
impl DebugAdapter for GdbDebugAdapter {
fn name(&self) -> DebugAdapterName {
DebugAdapterName(Self::ADAPTER_NAME.into())
}

fn transport(&self) -> Box<dyn Transport> {
Box::new(StdioTransport::new())
}

async fn get_binary(
&self,
delegate: &dyn DapDelegate,
config: &DebugAdapterConfig,
user_installed_path: Option<PathBuf>,
) -> Result<DebugAdapterBinary> {
let user_setting_path = user_installed_path
.filter(|p| p.exists())
.and_then(|p| p.to_str().map(|s| s.to_string()));

/* GDB implements DAP natively so just need to */
let gdb_path = delegate
.which(OsStr::new("gdb"))
.and_then(|p| p.to_str().map(|s| s.to_string()))
.ok_or(anyhow!("Could not find gdb in path"));

if gdb_path.is_err() && user_setting_path.is_none() {
bail!("Could not find gdb path or it's not installed");
}

let gdb_path = user_setting_path.unwrap_or(gdb_path?);

Ok(DebugAdapterBinary {
command: gdb_path,
arguments: Some(vec!["-i=dap".into()]),
envs: None,
cwd: config.cwd.clone(),
version: "1".into(),
})
}

async fn install_binary(
&self,
_version: AdapterVersion,
_delegate: &dyn DapDelegate,
) -> Result<()> {
unimplemented!("GDB debug adapter cannot be installed by Zed (yet)")
}

async fn fetch_latest_adapter_version(&self, _: &dyn DapDelegate) -> Result<AdapterVersion> {
unimplemented!("Fetch latest GDB version not implemented (yet)")
}

async fn get_installed_binary(
&self,
_: &dyn DapDelegate,
_: &DebugAdapterConfig,
_: Option<PathBuf>,
) -> Result<DebugAdapterBinary> {
unimplemented!("GDB cannot be installed by Zed (yet)")
}

fn request_args(&self, config: &DebugAdapterConfig) -> Value {
json!({"program": config.program})
}
}
3 changes: 3 additions & 0 deletions crates/task/src/debug_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub enum DebugAdapterKind {
Go(TCPHost),
/// Use lldb
Lldb,
/// Use GDB's built-in DAP support
Gdb,
}

impl DebugAdapterKind {
Expand All @@ -84,6 +86,7 @@ impl DebugAdapterKind {
Self::Php(_) => "PHP",
Self::Javascript(_) => "JavaScript",
Self::Lldb => "LLDB",
Self::Gdb => "GDB",
Self::Go(_) => "Go",
}
}
Expand Down

0 comments on commit 9720c3a

Please sign in to comment.