Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dap_adapters: add gdb #70

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .zed/debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@
"program": "$ZED_WORKTREE_ROOT/target/debug/zed",
"request": "launch",
"cwd": "$ZED_WORKTREE_ROOT"
},
{
"label": "Debug Zed with GDB",
"adapter": "gdb",
"program": "$ZED_WORKTREE_ROOT/target/debug/zed",
"request": "launch",
"cwd": "$ZED_WORKTREE_ROOT",
"initialize_args": {
"stopAtBeginningOfMainSubprogram": true
}
}
]
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, "cwd": config.cwd})
}
}
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