Debugger implementation (#13433)

###  DISCLAIMER

> As of 6th March 2025, debugger is still in development. We plan to
merge it behind a staff-only feature flag for staff use only, followed
by non-public release and then finally a public one (akin to how Git
panel release was handled). This is done to ensure the best experience
when it gets released.

### END OF DISCLAIMER 

**The current state of the debugger implementation:**


https://github.com/user-attachments/assets/c4deff07-80dd-4dc6-ad2e-0c252a478fe9


https://github.com/user-attachments/assets/e1ed2345-b750-4bb6-9c97-50961b76904f

----

All the todo's are in the following channel, so it's easier to work on
this together:
https://zed.dev/channel/zed-debugger-11370

If you are on Linux, you can use the following command to join the
channel:
```cli
zed https://zed.dev/channel/zed-debugger-11370 
```

## Current Features

- Collab
  - Breakpoints
    - Sync when you (re)join a project
    - Sync when you add/remove a breakpoint
  - Sync active debug line
  - Stack frames
    - Click on stack frame
      - View variables that belong to the stack frame
      - Visit the source file
    - Restart stack frame (if adapter supports this)
  - Variables
  - Loaded sources
  - Modules
  - Controls
    - Continue
    - Step back
      - Stepping granularity (configurable)
    - Step into
      - Stepping granularity (configurable)
    - Step over
      - Stepping granularity (configurable)
    - Step out
      - Stepping granularity (configurable)
  - Debug console
- Breakpoints
  - Log breakpoints
  - line breakpoints
  - Persistent between zed sessions (configurable)
  - Multi buffer support
  - Toggle disable/enable all breakpoints
- Stack frames
  - Click on stack frame
    - View variables that belong to the stack frame
    - Visit the source file
    - Show collapsed stack frames
  - Restart stack frame (if adapter supports this)
- Loaded sources
  - View all used loaded sources if supported by adapter.
- Modules
  - View all used modules (if adapter supports this)
- Variables
  - Copy value
  - Copy name
  - Copy memory reference
  - Set value (if adapter supports this)
  - keyboard navigation
- Debug Console
  - See logs
  - View output that was sent from debug adapter
    - Output grouping
  - Evaluate code
    - Updates the variable list
    - Auto completion
- If not supported by adapter, we will show auto-completion for existing
variables
- Debug Terminal
- Run custom commands and change env values right inside your Zed
terminal
- Attach to process (if adapter supports this)
  - Process picker
- Controls
  - Continue
  - Step back
    - Stepping granularity (configurable)
  - Step into
    - Stepping granularity (configurable)
  - Step over
    - Stepping granularity (configurable)
  - Step out
    - Stepping granularity (configurable)
  - Disconnect
  - Restart
  - Stop
- Warning when a debug session exited without hitting any breakpoint
- Debug view to see Adapter/RPC log messages
- Testing
  - Fake debug adapter
    - Fake requests & events

---

Release Notes:

- N/A

---------

Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
Co-authored-by: Anthony Eid <hello@anthonyeid.me>
Co-authored-by: Anthony <anthony@zed.dev>
Co-authored-by: Piotr Osiewicz <peterosiewicz@gmail.com>
Co-authored-by: Piotr <piotr@zed.dev>
This commit is contained in:
Remco Smits 2025-03-18 17:55:25 +01:00 committed by GitHub
parent ed4e654fdf
commit 41a60ffecf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
156 changed files with 25840 additions and 451 deletions

View file

@ -28,8 +28,11 @@ use language_settings::{Formatter, FormatterList, IndentGuideSettings};
use multi_buffer::{IndentGuide, PathKey};
use parking_lot::Mutex;
use pretty_assertions::{assert_eq, assert_ne};
use project::project_settings::{LspSettings, ProjectSettings};
use project::FakeFs;
use project::{
debugger::breakpoint_store::{BreakpointKind, SerializedBreakpoint},
project_settings::{LspSettings, ProjectSettings},
FakeFs,
};
use serde_json::{self, json};
use std::{cell::RefCell, future::Future, rc::Rc, sync::atomic::AtomicBool, time::Instant};
use std::{
@ -11924,6 +11927,7 @@ async fn test_move_to_enclosing_bracket(cx: &mut TestAppContext) {
cx.update_editor(|editor, window, cx| {
editor.move_to_enclosing_bracket(&MoveToEnclosingBracket, window, cx)
});
cx.run_until_parked();
cx.assert_editor_state(after);
};
@ -17111,6 +17115,337 @@ async fn assert_highlighted_edits(
});
}
#[track_caller]
fn assert_breakpoint(
breakpoints: &BTreeMap<Arc<Path>, Vec<SerializedBreakpoint>>,
path: &Arc<Path>,
expected: Vec<(u32, BreakpointKind)>,
) {
if expected.len() == 0usize {
assert!(!breakpoints.contains_key(path));
} else {
let mut breakpoint = breakpoints
.get(path)
.unwrap()
.into_iter()
.map(|breakpoint| (breakpoint.position, breakpoint.kind.clone()))
.collect::<Vec<_>>();
breakpoint.sort_by_key(|(cached_position, _)| *cached_position);
assert_eq!(expected, breakpoint);
}
}
fn add_log_breakpoint_at_cursor(
editor: &mut Editor,
log_message: &str,
window: &mut Window,
cx: &mut Context<Editor>,
) {
let (anchor, bp) = editor
.breakpoint_at_cursor_head(window, cx)
.unwrap_or_else(|| {
let cursor_position: Point = editor.selections.newest(cx).head();
let breakpoint_position = editor
.snapshot(window, cx)
.display_snapshot
.buffer_snapshot
.anchor_before(Point::new(cursor_position.row, 0));
let kind = BreakpointKind::Log(Arc::from(log_message));
(breakpoint_position, Breakpoint { kind })
});
editor.edit_breakpoint_at_anchor(
anchor,
bp.kind,
BreakpointEditAction::EditLogMessage(log_message.into()),
cx,
);
}
#[gpui::test]
async fn test_breakpoint_toggling(cx: &mut TestAppContext) {
init_test(cx, |_| {});
let sample_text = "First line\nSecond line\nThird line\nFourth line".to_string();
let fs = FakeFs::new(cx.executor());
fs.insert_tree(
path!("/a"),
json!({
"main.rs": sample_text,
}),
)
.await;
let project = Project::test(fs, [path!("/a").as_ref()], cx).await;
let workspace = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
let cx = &mut VisualTestContext::from_window(*workspace.deref(), cx);
let fs = FakeFs::new(cx.executor());
fs.insert_tree(
path!("/a"),
json!({
"main.rs": sample_text,
}),
)
.await;
let project = Project::test(fs, [path!("/a").as_ref()], cx).await;
let workspace = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
let cx = &mut VisualTestContext::from_window(*workspace.deref(), cx);
let worktree_id = workspace
.update(cx, |workspace, _window, cx| {
workspace.project().update(cx, |project, cx| {
project.worktrees(cx).next().unwrap().read(cx).id()
})
})
.unwrap();
let buffer = project
.update(cx, |project, cx| {
project.open_buffer((worktree_id, "main.rs"), cx)
})
.await
.unwrap();
let (editor, cx) = cx.add_window_view(|window, cx| {
Editor::new(
EditorMode::Full,
MultiBuffer::build_from_buffer(buffer, cx),
Some(project.clone()),
window,
cx,
)
});
let project_path = editor.update(cx, |editor, cx| editor.project_path(cx).unwrap());
let abs_path = project.read_with(cx, |project, cx| {
project
.absolute_path(&project_path, cx)
.map(|path_buf| Arc::from(path_buf.to_owned()))
.unwrap()
});
// assert we can add breakpoint on the first line
editor.update_in(cx, |editor, window, cx| {
editor.toggle_breakpoint(&actions::ToggleBreakpoint, window, cx);
editor.move_to_end(&MoveToEnd, window, cx);
editor.toggle_breakpoint(&actions::ToggleBreakpoint, window, cx);
});
let breakpoints = editor.update(cx, |editor, cx| {
editor
.breakpoint_store()
.as_ref()
.unwrap()
.read(cx)
.all_breakpoints(cx)
.clone()
});
assert_eq!(1, breakpoints.len());
assert_breakpoint(
&breakpoints,
&abs_path,
vec![(0, BreakpointKind::Standard), (3, BreakpointKind::Standard)],
);
editor.update_in(cx, |editor, window, cx| {
editor.move_to_beginning(&MoveToBeginning, window, cx);
editor.toggle_breakpoint(&actions::ToggleBreakpoint, window, cx);
});
let breakpoints = editor.update(cx, |editor, cx| {
editor
.breakpoint_store()
.as_ref()
.unwrap()
.read(cx)
.all_breakpoints(cx)
.clone()
});
assert_eq!(1, breakpoints.len());
assert_breakpoint(&breakpoints, &abs_path, vec![(3, BreakpointKind::Standard)]);
editor.update_in(cx, |editor, window, cx| {
editor.move_to_end(&MoveToEnd, window, cx);
editor.toggle_breakpoint(&actions::ToggleBreakpoint, window, cx);
});
let breakpoints = editor.update(cx, |editor, cx| {
editor
.breakpoint_store()
.as_ref()
.unwrap()
.read(cx)
.all_breakpoints(cx)
.clone()
});
assert_eq!(0, breakpoints.len());
assert_breakpoint(&breakpoints, &abs_path, vec![]);
}
#[gpui::test]
async fn test_log_breakpoint_editing(cx: &mut TestAppContext) {
init_test(cx, |_| {});
let sample_text = "First line\nSecond line\nThird line\nFourth line".to_string();
let fs = FakeFs::new(cx.executor());
fs.insert_tree(
path!("/a"),
json!({
"main.rs": sample_text,
}),
)
.await;
let project = Project::test(fs, [path!("/a").as_ref()], cx).await;
let (workspace, cx) =
cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
let worktree_id = workspace.update(cx, |workspace, cx| {
workspace.project().update(cx, |project, cx| {
project.worktrees(cx).next().unwrap().read(cx).id()
})
});
let buffer = project
.update(cx, |project, cx| {
project.open_buffer((worktree_id, "main.rs"), cx)
})
.await
.unwrap();
let (editor, cx) = cx.add_window_view(|window, cx| {
Editor::new(
EditorMode::Full,
MultiBuffer::build_from_buffer(buffer, cx),
Some(project.clone()),
window,
cx,
)
});
let project_path = editor.update(cx, |editor, cx| editor.project_path(cx).unwrap());
let abs_path = project.read_with(cx, |project, cx| {
project
.absolute_path(&project_path, cx)
.map(|path_buf| Arc::from(path_buf.to_owned()))
.unwrap()
});
editor.update_in(cx, |editor, window, cx| {
add_log_breakpoint_at_cursor(editor, "hello world", window, cx);
});
let breakpoints = editor.update(cx, |editor, cx| {
editor
.breakpoint_store()
.as_ref()
.unwrap()
.read(cx)
.all_breakpoints(cx)
.clone()
});
assert_breakpoint(
&breakpoints,
&abs_path,
vec![(0, BreakpointKind::Log("hello world".into()))],
);
// Removing a log message from a log breakpoint should remove it
editor.update_in(cx, |editor, window, cx| {
add_log_breakpoint_at_cursor(editor, "", window, cx);
});
let breakpoints = editor.update(cx, |editor, cx| {
editor
.breakpoint_store()
.as_ref()
.unwrap()
.read(cx)
.all_breakpoints(cx)
.clone()
});
assert_breakpoint(&breakpoints, &abs_path, vec![]);
editor.update_in(cx, |editor, window, cx| {
editor.toggle_breakpoint(&actions::ToggleBreakpoint, window, cx);
editor.move_to_end(&MoveToEnd, window, cx);
editor.toggle_breakpoint(&actions::ToggleBreakpoint, window, cx);
// Not adding a log message to a standard breakpoint shouldn't remove it
add_log_breakpoint_at_cursor(editor, "", window, cx);
});
let breakpoints = editor.update(cx, |editor, cx| {
editor
.breakpoint_store()
.as_ref()
.unwrap()
.read(cx)
.all_breakpoints(cx)
.clone()
});
assert_breakpoint(
&breakpoints,
&abs_path,
vec![(0, BreakpointKind::Standard), (3, BreakpointKind::Standard)],
);
editor.update_in(cx, |editor, window, cx| {
add_log_breakpoint_at_cursor(editor, "hello world", window, cx);
});
let breakpoints = editor.update(cx, |editor, cx| {
editor
.breakpoint_store()
.as_ref()
.unwrap()
.read(cx)
.all_breakpoints(cx)
.clone()
});
assert_breakpoint(
&breakpoints,
&abs_path,
vec![
(0, BreakpointKind::Standard),
(3, BreakpointKind::Log("hello world".into())),
],
);
editor.update_in(cx, |editor, window, cx| {
add_log_breakpoint_at_cursor(editor, "hello Earth!!", window, cx);
});
let breakpoints = editor.update(cx, |editor, cx| {
editor
.breakpoint_store()
.as_ref()
.unwrap()
.read(cx)
.all_breakpoints(cx)
.clone()
});
assert_breakpoint(
&breakpoints,
&abs_path,
vec![
(0, BreakpointKind::Standard),
(3, BreakpointKind::Log("hello Earth !!".into())),
],
);
}
#[gpui::test]
async fn test_rename_with_duplicate_edits(cx: &mut TestAppContext) {
init_test(cx, |_| {});