Skip to content

Commit

Permalink
Add threads reqeust to debug session
Browse files Browse the repository at this point in the history
Co-authored-by: Piotr  <piotr@zed.dev>
  • Loading branch information
Anthony-Eid and osiewicz committed Feb 13, 2025
1 parent fe200a0 commit 807c4f7
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions crates/dap/src/proto_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,22 @@ impl ProtoConversion for dap_types::CompletionItemType {
}
}
}

impl ProtoConversion for dap_types::Thread {
type ProtoType = proto::DapThread;
type Output = Self;

fn to_proto(&self) -> Self::ProtoType {
proto::DapThread {
id: self.id,
name: self.name.clone(),
}
}

fn from_proto(payload: Self::ProtoType) -> Self {
Self {
id: payload.id,
name: payload.name,
}
}
}
1 change: 0 additions & 1 deletion crates/debugger_ui/src/debugger_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ impl DebugPanel {
let thread_panel = item.downcast::<DebugPanelItem>().unwrap();

let thread_id = thread_panel.read(cx).thread_id();
let session_id = thread_panel.read(cx).session().read(cx).id();
let client_id = thread_panel.read(cx).client_id();

self.thread_states.remove(&(client_id, thread_id));
Expand Down
4 changes: 2 additions & 2 deletions crates/debugger_ui/src/stack_frame_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl StackFrameList {
session: Entity<DebugSession>,
client_id: DebugAdapterClientId,
thread_id: ThreadId,
window: &Window,
_window: &Window,
cx: &mut Context<Self>,
) -> Self {
let weak_entity = cx.weak_entity();
Expand All @@ -67,7 +67,7 @@ impl StackFrameList {
let client_state = session.read(cx).client_state(client_id).unwrap();

let _subscription = cx.observe(&client_state, |stack_frame_list, state, cx| {
let frame_len = state.update(cx, |state, cx| {
let _frame_len = state.update(cx, |state, cx| {
state.stack_frames(stack_frame_list.thread_id, cx).len()
});

Expand Down
2 changes: 1 addition & 1 deletion crates/debugger_ui/src/tests/variable_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ async fn test_it_only_fetches_scopes_and_variables_for_the_first_stack_frame(

assert_eq!(1, stack_frame_id);
assert_eq!(stack_frames, stack_frame_list);
let stack_frame_list = debug_panel_item.stack_frame_list().read(cx);

let variable_list = debug_panel_item.variable_list().read(cx);

assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions crates/project/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ globset.workspace = true
gpui.workspace = true
http_client.workspace = true
itertools.workspace = true
indexmap.workspace = true
language.workspace = true
log.workspace = true
lsp.workspace = true
Expand Down
59 changes: 59 additions & 0 deletions crates/project/src/debugger/dap_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,3 +1527,62 @@ impl DapCommand for EvaluateCommand {
}
}
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub(crate) struct ThreadsCommand;

impl DapCommand for ThreadsCommand {
type Response = Vec<dap::Thread>;
type DapRequest = dap::requests::Threads;
type ProtoRequest = proto::DapThreadsRequest;

fn to_dap(&self) -> <Self::DapRequest as dap::requests::Request>::Arguments {
()
}

fn response_from_dap(
&self,
message: <Self::DapRequest as dap::requests::Request>::Response,
) -> Result<Self::Response> {
Ok(message.threads)
}

fn is_supported(&self, _capabilities: &Capabilities) -> bool {
true
}

fn to_proto(
&self,
debug_client_id: DebugAdapterClientId,
upstream_project_id: u64,
) -> Self::ProtoRequest {
proto::DapThreadsRequest {
project_id: upstream_project_id,
client_id: debug_client_id.to_proto(),
}
}

fn from_proto(_request: &Self::ProtoRequest) -> Self {
Self {}
}

fn client_id_from_proto(request: &Self::ProtoRequest) -> DebugAdapterClientId {
DebugAdapterClientId::from_proto(request.client_id)
}

fn response_from_proto(
&self,
message: <Self::ProtoRequest as proto::RequestMessage>::Response,
) -> Result<Self::Response> {
Ok(Vec::from_proto(message.threads))
}

fn response_to_proto(
_debug_client_id: DebugAdapterClientId,
message: Self::Response,
) -> <Self::ProtoRequest as proto::RequestMessage>::Response {
proto::DapThreadsResponse {
threads: message.to_proto(),
}
}
}
41 changes: 35 additions & 6 deletions crates/project/src/debugger/dap_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::dap_command::{
TerminateThreadsCommand, VariablesCommand,
};
use anyhow::{anyhow, Result};
use collections::{BTreeMap, HashMap};
use collections::{BTreeMap, HashMap, IndexMap};
use dap::client::{DebugAdapterClient, DebugAdapterClientId};
use dap::requests::Request;
use dap::{
Expand Down Expand Up @@ -42,7 +42,7 @@ impl DebugSessionId {
}
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, PartialOrd, Ord, Eq)]
#[repr(transparent)]
pub struct ThreadId(pub u64);

Expand Down Expand Up @@ -106,12 +106,23 @@ pub enum ThreadStatus {
}

pub struct Thread {
_thread: dap::Thread,
dap: dap::Thread,
stack_frames: Vec<StackFrame>,
_status: ThreadStatus,
_has_stopped: bool,
}

impl From<dap::Thread> for Thread {
fn from(dap: dap::Thread) -> Self {
Self {
dap,
stack_frames: vec![],
_status: ThreadStatus::default(),
_has_stopped: false,
}
}
}

type UpstreamProjectId = u64;

pub struct RemoteConnection {
Expand Down Expand Up @@ -227,7 +238,7 @@ pub struct Client {
client_id: DebugAdapterClientId,
modules: Vec<dap::Module>,
loaded_sources: Vec<dap::Source>,
threads: BTreeMap<ThreadId, Thread>,
threads: IndexMap<ThreadId, Thread>,
requests: HashMap<RequestSlot, Shared<Task<Option<()>>>>,
}

Expand Down Expand Up @@ -391,6 +402,24 @@ impl Client {
cx.notify();
}

pub fn threads(&mut self, cx: &mut Context<Self>) -> Vec<dap::Thread> {
self.fetch(
dap_command::ThreadsCommand,
|this, result, cx| {
this.threads.extend(
result
.iter()
.map(|thread| (ThreadId(thread.id), Thread::from(thread.clone()))),
);
},
cx,
);
self.threads
.values()
.map(|thread| thread.dap.clone())
.collect()
}

pub fn modules(&mut self, cx: &mut Context<Self>) -> &[Module] {
self.fetch(
dap_command::ModulesCommand,
Expand Down Expand Up @@ -681,7 +710,7 @@ impl Client {
thread.stack_frames = stack_frames.iter().cloned().map(From::from).collect();
});
debug_assert!(
matches!(entry, BTreeMapEntry::Occupied(_)),
matches!(entry, indexmap::map::Entry::Occupied(_)),
"Sent request for thread_id that doesn't exist"
);

Expand Down Expand Up @@ -978,7 +1007,7 @@ impl DebugSession {
client_id,
modules: Vec::default(),
loaded_sources: Vec::default(),
threads: BTreeMap::default(),
threads: IndexMap::default(),
requests: HashMap::default(),
capabilities: Default::default(),
mode,
Expand Down
18 changes: 17 additions & 1 deletion crates/proto/proto/zed.proto
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ message Envelope {
DapEvaluateRequest dap_evaluate_request = 341;
DapEvaluateResponse dap_evaluate_response = 342;
DapCompletionRequest dap_completion_request = 343;
DapCompletionResponse dap_completion_response = 344; // current max
DapCompletionResponse dap_completion_response = 344;
DapThreadsRequest dap_threads_request = 345;
DapThreadsResponse dap_threads_response = 346; // current max
}

reserved 87 to 88;
Expand Down Expand Up @@ -2880,6 +2882,15 @@ message DapTerminateThreadsRequest {
repeated uint64 thread_ids = 3;
}

message DapThreadsRequest {
uint64 project_id = 1;
uint64 client_id = 2;
}

message DapThreadsResponse {
repeated DapThread threads = 1;
}

message DapTerminateRequest {
uint64 project_id = 1;
uint64 client_id = 2;
Expand Down Expand Up @@ -3069,6 +3080,11 @@ message DapVariable {
optional string memory_reference = 9;
}

message DapThread {
uint64 id = 1;
string name = 2;
}

message DapScope {
string name = 1;
optional DapScopePresentationHint presentation_hint = 2;
Expand Down
4 changes: 4 additions & 0 deletions crates/proto/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ messages!(
(DapEvaluateResponse, Background),
(DapCompletionRequest, Background),
(DapCompletionResponse, Background),
(DapThreadsRequest, Background),
(DapThreadsResponse, Background),
);

request_messages!(
Expand Down Expand Up @@ -643,6 +645,7 @@ request_messages!(
(DapSetVariableValueRequest, DapSetVariableValueResponse),
(DapEvaluateRequest, DapEvaluateResponse),
(DapCompletionRequest, DapCompletionResponse),
(DapThreadsRequest, DapThreadsResponse),
);

entity_messages!(
Expand Down Expand Up @@ -768,6 +771,7 @@ entity_messages!(
DapSetVariableValueRequest,
DapEvaluateRequest,
DapCompletionRequest,
DapThreadsRequest,
);

entity_messages!(
Expand Down

0 comments on commit 807c4f7

Please sign in to comment.