session.rs

   1use crate::debugger::breakpoint_store::BreakpointSessionState;
   2
   3use super::breakpoint_store::{
   4    BreakpointStore, BreakpointStoreEvent, BreakpointUpdatedReason, SourceBreakpoint,
   5};
   6use super::dap_command::{
   7    self, Attach, ConfigurationDone, ContinueCommand, DapCommand, DisconnectCommand,
   8    EvaluateCommand, Initialize, Launch, LoadedSourcesCommand, LocalDapCommand, LocationsCommand,
   9    ModulesCommand, NextCommand, PauseCommand, RestartCommand, RestartStackFrameCommand,
  10    ScopesCommand, SetExceptionBreakpoints, SetVariableValueCommand, StackTraceCommand,
  11    StepBackCommand, StepCommand, StepInCommand, StepOutCommand, TerminateCommand,
  12    TerminateThreadsCommand, ThreadsCommand, VariablesCommand,
  13};
  14use super::dap_store::DapStore;
  15use anyhow::{Context as _, Result, anyhow};
  16use collections::{HashMap, HashSet, IndexMap};
  17use dap::adapters::{DebugAdapterBinary, DebugAdapterName};
  18use dap::messages::Response;
  19use dap::requests::{Request, RunInTerminal, StartDebugging};
  20use dap::{
  21    Capabilities, ContinueArguments, EvaluateArgumentsContext, Module, Source, StackFrameId,
  22    SteppingGranularity, StoppedEvent, VariableReference,
  23    client::{DebugAdapterClient, SessionId},
  24    messages::{Events, Message},
  25};
  26use dap::{
  27    ExceptionBreakpointsFilter, ExceptionFilterOptions, OutputEvent, OutputEventCategory,
  28    RunInTerminalRequestArguments, StackFramePresentationHint, StartDebuggingRequestArguments,
  29    StartDebuggingRequestArgumentsRequest, VariablePresentationHint,
  30};
  31use futures::SinkExt;
  32use futures::channel::mpsc::UnboundedSender;
  33use futures::channel::{mpsc, oneshot};
  34use futures::{FutureExt, future::Shared};
  35use gpui::{
  36    App, AppContext, AsyncApp, BackgroundExecutor, Context, Entity, EventEmitter, SharedString,
  37    Task, WeakEntity,
  38};
  39
  40use rpc::ErrorExt;
  41use serde_json::Value;
  42use smol::stream::StreamExt;
  43use std::any::TypeId;
  44use std::collections::BTreeMap;
  45use std::u64;
  46use std::{
  47    any::Any,
  48    collections::hash_map::Entry,
  49    hash::{Hash, Hasher},
  50    path::Path,
  51    sync::Arc,
  52};
  53use task::TaskContext;
  54use text::{PointUtf16, ToPointUtf16};
  55use util::ResultExt;
  56use worktree::Worktree;
  57
  58#[derive(Debug, Copy, Clone, Hash, PartialEq, PartialOrd, Ord, Eq)]
  59#[repr(transparent)]
  60pub struct ThreadId(pub u64);
  61
  62impl ThreadId {
  63    pub const MIN: ThreadId = ThreadId(u64::MIN);
  64    pub const MAX: ThreadId = ThreadId(u64::MAX);
  65}
  66
  67impl From<u64> for ThreadId {
  68    fn from(id: u64) -> Self {
  69        Self(id)
  70    }
  71}
  72
  73#[derive(Clone, Debug)]
  74pub struct StackFrame {
  75    pub dap: dap::StackFrame,
  76    pub scopes: Vec<dap::Scope>,
  77}
  78
  79impl From<dap::StackFrame> for StackFrame {
  80    fn from(stack_frame: dap::StackFrame) -> Self {
  81        Self {
  82            scopes: vec![],
  83            dap: stack_frame,
  84        }
  85    }
  86}
  87
  88#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
  89pub enum ThreadStatus {
  90    #[default]
  91    Running,
  92    Stopped,
  93    Stepping,
  94    Exited,
  95    Ended,
  96}
  97
  98impl ThreadStatus {
  99    pub fn label(&self) -> &'static str {
 100        match self {
 101            ThreadStatus::Running => "Running",
 102            ThreadStatus::Stopped => "Stopped",
 103            ThreadStatus::Stepping => "Stepping",
 104            ThreadStatus::Exited => "Exited",
 105            ThreadStatus::Ended => "Ended",
 106        }
 107    }
 108}
 109
 110#[derive(Debug)]
 111pub struct Thread {
 112    dap: dap::Thread,
 113    stack_frames: Vec<StackFrame>,
 114    stack_frames_error: Option<anyhow::Error>,
 115    _has_stopped: bool,
 116}
 117
 118impl From<dap::Thread> for Thread {
 119    fn from(dap: dap::Thread) -> Self {
 120        Self {
 121            dap,
 122            stack_frames: Default::default(),
 123            stack_frames_error: None,
 124            _has_stopped: false,
 125        }
 126    }
 127}
 128
 129#[derive(Debug, Clone, PartialEq)]
 130pub struct Watcher {
 131    pub expression: SharedString,
 132    pub value: SharedString,
 133    pub variables_reference: u64,
 134    pub presentation_hint: Option<VariablePresentationHint>,
 135}
 136
 137pub enum Mode {
 138    Building,
 139    Running(RunningMode),
 140}
 141
 142#[derive(Clone)]
 143pub struct RunningMode {
 144    client: Arc<DebugAdapterClient>,
 145    binary: DebugAdapterBinary,
 146    tmp_breakpoint: Option<SourceBreakpoint>,
 147    worktree: WeakEntity<Worktree>,
 148    executor: BackgroundExecutor,
 149    is_started: bool,
 150    has_ever_stopped: bool,
 151    messages_tx: UnboundedSender<Message>,
 152}
 153
 154fn client_source(abs_path: &Path) -> dap::Source {
 155    dap::Source {
 156        name: abs_path
 157            .file_name()
 158            .map(|filename| filename.to_string_lossy().to_string()),
 159        path: Some(abs_path.to_string_lossy().to_string()),
 160        source_reference: None,
 161        presentation_hint: None,
 162        origin: None,
 163        sources: None,
 164        adapter_data: None,
 165        checksums: None,
 166    }
 167}
 168
 169impl RunningMode {
 170    async fn new(
 171        session_id: SessionId,
 172        parent_session: Option<Entity<Session>>,
 173        worktree: WeakEntity<Worktree>,
 174        binary: DebugAdapterBinary,
 175        messages_tx: futures::channel::mpsc::UnboundedSender<Message>,
 176        cx: &mut AsyncApp,
 177    ) -> Result<Self> {
 178        let message_handler = Box::new({
 179            let messages_tx = messages_tx.clone();
 180            move |message| {
 181                messages_tx.unbounded_send(message).ok();
 182            }
 183        });
 184
 185        let client = if let Some(client) = parent_session
 186            .and_then(|session| cx.update(|cx| session.read(cx).adapter_client()).ok())
 187            .flatten()
 188        {
 189            client
 190                .create_child_connection(session_id, binary.clone(), message_handler, cx)
 191                .await?
 192        } else {
 193            DebugAdapterClient::start(session_id, binary.clone(), message_handler, cx).await?
 194        };
 195
 196        Ok(Self {
 197            client: Arc::new(client),
 198            worktree,
 199            tmp_breakpoint: None,
 200            binary,
 201            executor: cx.background_executor().clone(),
 202            is_started: false,
 203            has_ever_stopped: false,
 204            messages_tx,
 205        })
 206    }
 207
 208    pub(crate) fn worktree(&self) -> &WeakEntity<Worktree> {
 209        &self.worktree
 210    }
 211
 212    fn unset_breakpoints_from_paths(&self, paths: &Vec<Arc<Path>>, cx: &mut App) -> Task<()> {
 213        let tasks: Vec<_> = paths
 214            .into_iter()
 215            .map(|path| {
 216                self.request(dap_command::SetBreakpoints {
 217                    source: client_source(path),
 218                    source_modified: None,
 219                    breakpoints: vec![],
 220                })
 221            })
 222            .collect();
 223
 224        cx.background_spawn(async move {
 225            futures::future::join_all(tasks)
 226                .await
 227                .iter()
 228                .for_each(|res| match res {
 229                    Ok(_) => {}
 230                    Err(err) => {
 231                        log::warn!("Set breakpoints request failed: {}", err);
 232                    }
 233                });
 234        })
 235    }
 236
 237    fn send_breakpoints_from_path(
 238        &self,
 239        abs_path: Arc<Path>,
 240        reason: BreakpointUpdatedReason,
 241        breakpoint_store: &Entity<BreakpointStore>,
 242        cx: &mut App,
 243    ) -> Task<()> {
 244        let breakpoints =
 245            breakpoint_store
 246                .read(cx)
 247                .source_breakpoints_from_path(&abs_path, cx)
 248                .into_iter()
 249                .filter(|bp| bp.state.is_enabled())
 250                .chain(self.tmp_breakpoint.iter().filter_map(|breakpoint| {
 251                    breakpoint.path.eq(&abs_path).then(|| breakpoint.clone())
 252                }))
 253                .map(Into::into)
 254                .collect();
 255
 256        let raw_breakpoints = breakpoint_store
 257            .read(cx)
 258            .breakpoints_from_path(&abs_path)
 259            .into_iter()
 260            .filter(|bp| bp.bp.state.is_enabled())
 261            .collect::<Vec<_>>();
 262
 263        let task = self.request(dap_command::SetBreakpoints {
 264            source: client_source(&abs_path),
 265            source_modified: Some(matches!(reason, BreakpointUpdatedReason::FileSaved)),
 266            breakpoints,
 267        });
 268        let session_id = self.client.id();
 269        let breakpoint_store = breakpoint_store.downgrade();
 270        cx.spawn(async move |cx| match cx.background_spawn(task).await {
 271            Ok(breakpoints) => {
 272                let breakpoints =
 273                    breakpoints
 274                        .into_iter()
 275                        .zip(raw_breakpoints)
 276                        .filter_map(|(dap_bp, zed_bp)| {
 277                            Some((
 278                                zed_bp,
 279                                BreakpointSessionState {
 280                                    id: dap_bp.id?,
 281                                    verified: dap_bp.verified,
 282                                },
 283                            ))
 284                        });
 285                breakpoint_store
 286                    .update(cx, |this, _| {
 287                        this.mark_breakpoints_verified(session_id, &abs_path, breakpoints);
 288                    })
 289                    .ok();
 290            }
 291            Err(err) => log::warn!("Set breakpoints request failed for path: {}", err),
 292        })
 293    }
 294
 295    fn send_exception_breakpoints(
 296        &self,
 297        filters: Vec<ExceptionBreakpointsFilter>,
 298        supports_filter_options: bool,
 299    ) -> Task<Result<Vec<dap::Breakpoint>>> {
 300        let arg = if supports_filter_options {
 301            SetExceptionBreakpoints::WithOptions {
 302                filters: filters
 303                    .into_iter()
 304                    .map(|filter| ExceptionFilterOptions {
 305                        filter_id: filter.filter,
 306                        condition: None,
 307                        mode: None,
 308                    })
 309                    .collect(),
 310            }
 311        } else {
 312            SetExceptionBreakpoints::Plain {
 313                filters: filters.into_iter().map(|filter| filter.filter).collect(),
 314            }
 315        };
 316        self.request(arg)
 317    }
 318
 319    fn send_source_breakpoints(
 320        &self,
 321        ignore_breakpoints: bool,
 322        breakpoint_store: &Entity<BreakpointStore>,
 323        cx: &App,
 324    ) -> Task<HashMap<Arc<Path>, anyhow::Error>> {
 325        let mut breakpoint_tasks = Vec::new();
 326        let breakpoints = breakpoint_store.read(cx).all_source_breakpoints(cx);
 327        let mut raw_breakpoints = breakpoint_store.read_with(cx, |this, _| this.all_breakpoints());
 328        debug_assert_eq!(raw_breakpoints.len(), breakpoints.len());
 329        let session_id = self.client.id();
 330        for (path, breakpoints) in breakpoints {
 331            let breakpoints = if ignore_breakpoints {
 332                vec![]
 333            } else {
 334                breakpoints
 335                    .into_iter()
 336                    .filter(|bp| bp.state.is_enabled())
 337                    .map(Into::into)
 338                    .collect()
 339            };
 340
 341            let raw_breakpoints = raw_breakpoints
 342                .remove(&path)
 343                .unwrap_or_default()
 344                .into_iter()
 345                .filter(|bp| bp.bp.state.is_enabled());
 346            let error_path = path.clone();
 347            let send_request = self
 348                .request(dap_command::SetBreakpoints {
 349                    source: client_source(&path),
 350                    source_modified: Some(false),
 351                    breakpoints,
 352                })
 353                .map(|result| result.map_err(move |e| (error_path, e)));
 354
 355            let task = cx.spawn({
 356                let breakpoint_store = breakpoint_store.downgrade();
 357                async move |cx| {
 358                    let breakpoints = cx.background_spawn(send_request).await?;
 359
 360                    let breakpoints = breakpoints.into_iter().zip(raw_breakpoints).filter_map(
 361                        |(dap_bp, zed_bp)| {
 362                            Some((
 363                                zed_bp,
 364                                BreakpointSessionState {
 365                                    id: dap_bp.id?,
 366                                    verified: dap_bp.verified,
 367                                },
 368                            ))
 369                        },
 370                    );
 371                    breakpoint_store
 372                        .update(cx, |this, _| {
 373                            this.mark_breakpoints_verified(session_id, &path, breakpoints);
 374                        })
 375                        .ok();
 376
 377                    Ok(())
 378                }
 379            });
 380            breakpoint_tasks.push(task);
 381        }
 382
 383        cx.background_spawn(async move {
 384            futures::future::join_all(breakpoint_tasks)
 385                .await
 386                .into_iter()
 387                .filter_map(Result::err)
 388                .collect::<HashMap<_, _>>()
 389        })
 390    }
 391
 392    fn initialize_sequence(
 393        &self,
 394        capabilities: &Capabilities,
 395        initialized_rx: oneshot::Receiver<()>,
 396        dap_store: WeakEntity<DapStore>,
 397        cx: &mut Context<Session>,
 398    ) -> Task<Result<()>> {
 399        let raw = self.binary.request_args.clone();
 400
 401        // Of relevance: https://github.com/microsoft/vscode/issues/4902#issuecomment-368583522
 402        let launch = match raw.request {
 403            dap::StartDebuggingRequestArgumentsRequest::Launch => self.request(Launch {
 404                raw: raw.configuration,
 405            }),
 406            dap::StartDebuggingRequestArgumentsRequest::Attach => self.request(Attach {
 407                raw: raw.configuration,
 408            }),
 409        };
 410
 411        let configuration_done_supported = ConfigurationDone::is_supported(capabilities);
 412        let exception_filters = capabilities
 413            .exception_breakpoint_filters
 414            .as_ref()
 415            .map(|exception_filters| {
 416                exception_filters
 417                    .iter()
 418                    .filter(|filter| filter.default == Some(true))
 419                    .cloned()
 420                    .collect::<Vec<_>>()
 421            })
 422            .unwrap_or_default();
 423        // From spec (on initialization sequence):
 424        // client sends a setExceptionBreakpoints request if one or more exceptionBreakpointFilters have been defined (or if supportsConfigurationDoneRequest is not true)
 425        //
 426        // Thus we should send setExceptionBreakpoints even if `exceptionFilters` variable is empty (as long as there were some options in the first place).
 427        let should_send_exception_breakpoints = capabilities
 428            .exception_breakpoint_filters
 429            .as_ref()
 430            .map_or(false, |filters| !filters.is_empty())
 431            || !configuration_done_supported;
 432        let supports_exception_filters = capabilities
 433            .supports_exception_filter_options
 434            .unwrap_or_default();
 435        let this = self.clone();
 436        let worktree = self.worktree().clone();
 437        let configuration_sequence = cx.spawn({
 438            async move |_, cx| {
 439                let breakpoint_store =
 440                    dap_store.read_with(cx, |dap_store, _| dap_store.breakpoint_store().clone())?;
 441                initialized_rx.await?;
 442                let errors_by_path = cx
 443                    .update(|cx| this.send_source_breakpoints(false, &breakpoint_store, cx))?
 444                    .await;
 445
 446                dap_store.update(cx, |_, cx| {
 447                    let Some(worktree) = worktree.upgrade() else {
 448                        return;
 449                    };
 450
 451                    for (path, error) in &errors_by_path {
 452                        log::error!("failed to set breakpoints for {path:?}: {error}");
 453                    }
 454
 455                    if let Some(failed_path) = errors_by_path.keys().next() {
 456                        let failed_path = failed_path
 457                            .strip_prefix(worktree.read(cx).abs_path())
 458                            .unwrap_or(failed_path)
 459                            .display();
 460                        let message = format!(
 461                            "Failed to set breakpoints for {failed_path}{}",
 462                            match errors_by_path.len() {
 463                                0 => unreachable!(),
 464                                1 => "".into(),
 465                                2 => " and 1 other path".into(),
 466                                n => format!(" and {} other paths", n - 1),
 467                            }
 468                        );
 469                        cx.emit(super::dap_store::DapStoreEvent::Notification(message));
 470                    }
 471                })?;
 472
 473                if should_send_exception_breakpoints {
 474                    this.send_exception_breakpoints(exception_filters, supports_exception_filters)
 475                        .await
 476                        .ok();
 477                }
 478
 479                let ret = if configuration_done_supported {
 480                    this.request(ConfigurationDone {})
 481                } else {
 482                    Task::ready(Ok(()))
 483                }
 484                .await;
 485                ret
 486            }
 487        });
 488
 489        let task = cx.background_spawn(futures::future::try_join(launch, configuration_sequence));
 490
 491        cx.spawn(async move |this, cx| {
 492            let result = task.await;
 493
 494            this.update(cx, |this, cx| {
 495                if let Some(this) = this.as_running_mut() {
 496                    this.is_started = true;
 497                    cx.notify();
 498                }
 499            })
 500            .ok();
 501
 502            result?;
 503            anyhow::Ok(())
 504        })
 505    }
 506
 507    fn reconnect_for_ssh(&self, cx: &mut AsyncApp) -> Option<Task<Result<()>>> {
 508        let client = self.client.clone();
 509        let messages_tx = self.messages_tx.clone();
 510        let message_handler = Box::new(move |message| {
 511            messages_tx.unbounded_send(message).ok();
 512        });
 513        if client.should_reconnect_for_ssh() {
 514            Some(cx.spawn(async move |cx| {
 515                client.connect(message_handler, cx).await?;
 516                anyhow::Ok(())
 517            }))
 518        } else {
 519            None
 520        }
 521    }
 522
 523    fn request<R: LocalDapCommand>(&self, request: R) -> Task<Result<R::Response>>
 524    where
 525        <R::DapRequest as dap::requests::Request>::Response: 'static,
 526        <R::DapRequest as dap::requests::Request>::Arguments: 'static + Send,
 527    {
 528        let request = Arc::new(request);
 529
 530        let request_clone = request.clone();
 531        let connection = self.client.clone();
 532        self.executor.spawn(async move {
 533            let args = request_clone.to_dap();
 534            let response = connection.request::<R::DapRequest>(args).await?;
 535            request.response_from_dap(response)
 536        })
 537    }
 538}
 539
 540impl Mode {
 541    pub(super) fn request_dap<R: DapCommand>(&self, request: R) -> Task<Result<R::Response>>
 542    where
 543        <R::DapRequest as dap::requests::Request>::Response: 'static,
 544        <R::DapRequest as dap::requests::Request>::Arguments: 'static + Send,
 545    {
 546        match self {
 547            Mode::Running(debug_adapter_client) => debug_adapter_client.request(request),
 548            Mode::Building => Task::ready(Err(anyhow!(
 549                "no adapter running to send request: {request:?}"
 550            ))),
 551        }
 552    }
 553
 554    /// Did this debug session stop at least once?
 555    pub(crate) fn has_ever_stopped(&self) -> bool {
 556        match self {
 557            Mode::Building => false,
 558            Mode::Running(running_mode) => running_mode.has_ever_stopped,
 559        }
 560    }
 561
 562    fn stopped(&mut self) {
 563        if let Mode::Running(running) = self {
 564            running.has_ever_stopped = true;
 565        }
 566    }
 567}
 568
 569#[derive(Default)]
 570struct ThreadStates {
 571    global_state: Option<ThreadStatus>,
 572    known_thread_states: IndexMap<ThreadId, ThreadStatus>,
 573}
 574
 575impl ThreadStates {
 576    fn stop_all_threads(&mut self) {
 577        self.global_state = Some(ThreadStatus::Stopped);
 578        self.known_thread_states.clear();
 579    }
 580
 581    fn exit_all_threads(&mut self) {
 582        self.global_state = Some(ThreadStatus::Exited);
 583        self.known_thread_states.clear();
 584    }
 585
 586    fn continue_all_threads(&mut self) {
 587        self.global_state = Some(ThreadStatus::Running);
 588        self.known_thread_states.clear();
 589    }
 590
 591    fn stop_thread(&mut self, thread_id: ThreadId) {
 592        self.known_thread_states
 593            .insert(thread_id, ThreadStatus::Stopped);
 594    }
 595
 596    fn continue_thread(&mut self, thread_id: ThreadId) {
 597        self.known_thread_states
 598            .insert(thread_id, ThreadStatus::Running);
 599    }
 600
 601    fn process_step(&mut self, thread_id: ThreadId) {
 602        self.known_thread_states
 603            .insert(thread_id, ThreadStatus::Stepping);
 604    }
 605
 606    fn thread_status(&self, thread_id: ThreadId) -> ThreadStatus {
 607        self.thread_state(thread_id)
 608            .unwrap_or(ThreadStatus::Running)
 609    }
 610
 611    fn thread_state(&self, thread_id: ThreadId) -> Option<ThreadStatus> {
 612        self.known_thread_states
 613            .get(&thread_id)
 614            .copied()
 615            .or(self.global_state)
 616    }
 617
 618    fn exit_thread(&mut self, thread_id: ThreadId) {
 619        self.known_thread_states
 620            .insert(thread_id, ThreadStatus::Exited);
 621    }
 622
 623    fn any_stopped_thread(&self) -> bool {
 624        self.global_state
 625            .is_some_and(|state| state == ThreadStatus::Stopped)
 626            || self
 627                .known_thread_states
 628                .values()
 629                .any(|status| *status == ThreadStatus::Stopped)
 630    }
 631}
 632const MAX_TRACKED_OUTPUT_EVENTS: usize = 5000;
 633
 634type IsEnabled = bool;
 635
 636#[derive(Copy, Clone, Default, Debug, PartialEq, PartialOrd, Eq, Ord)]
 637pub struct OutputToken(pub usize);
 638/// Represents a current state of a single debug adapter and provides ways to mutate it.
 639pub struct Session {
 640    pub mode: Mode,
 641    id: SessionId,
 642    label: SharedString,
 643    adapter: DebugAdapterName,
 644    pub(super) capabilities: Capabilities,
 645    child_session_ids: HashSet<SessionId>,
 646    parent_session: Option<Entity<Session>>,
 647    modules: Vec<dap::Module>,
 648    loaded_sources: Vec<dap::Source>,
 649    output_token: OutputToken,
 650    output: Box<circular_buffer::CircularBuffer<MAX_TRACKED_OUTPUT_EVENTS, dap::OutputEvent>>,
 651    threads: IndexMap<ThreadId, Thread>,
 652    thread_states: ThreadStates,
 653    watchers: HashMap<SharedString, Watcher>,
 654    variables: HashMap<VariableReference, Vec<dap::Variable>>,
 655    stack_frames: IndexMap<StackFrameId, StackFrame>,
 656    locations: HashMap<u64, dap::LocationsResponse>,
 657    is_session_terminated: bool,
 658    requests: HashMap<TypeId, HashMap<RequestSlot, Shared<Task<Option<()>>>>>,
 659    pub(crate) breakpoint_store: Entity<BreakpointStore>,
 660    ignore_breakpoints: bool,
 661    exception_breakpoints: BTreeMap<String, (ExceptionBreakpointsFilter, IsEnabled)>,
 662    background_tasks: Vec<Task<()>>,
 663    task_context: TaskContext,
 664}
 665
 666trait CacheableCommand: Any + Send + Sync {
 667    fn dyn_eq(&self, rhs: &dyn CacheableCommand) -> bool;
 668    fn dyn_hash(&self, hasher: &mut dyn Hasher);
 669    fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
 670}
 671
 672impl<T> CacheableCommand for T
 673where
 674    T: DapCommand + PartialEq + Eq + Hash,
 675{
 676    fn dyn_eq(&self, rhs: &dyn CacheableCommand) -> bool {
 677        (rhs as &dyn Any)
 678            .downcast_ref::<Self>()
 679            .map_or(false, |rhs| self == rhs)
 680    }
 681
 682    fn dyn_hash(&self, mut hasher: &mut dyn Hasher) {
 683        T::hash(self, &mut hasher);
 684    }
 685
 686    fn as_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
 687        self
 688    }
 689}
 690
 691pub(crate) struct RequestSlot(Arc<dyn CacheableCommand>);
 692
 693impl<T: DapCommand + PartialEq + Eq + Hash> From<T> for RequestSlot {
 694    fn from(request: T) -> Self {
 695        Self(Arc::new(request))
 696    }
 697}
 698
 699impl PartialEq for RequestSlot {
 700    fn eq(&self, other: &Self) -> bool {
 701        self.0.dyn_eq(other.0.as_ref())
 702    }
 703}
 704
 705impl Eq for RequestSlot {}
 706
 707impl Hash for RequestSlot {
 708    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
 709        self.0.dyn_hash(state);
 710        (&*self.0 as &dyn Any).type_id().hash(state)
 711    }
 712}
 713
 714#[derive(Debug, Clone, Hash, PartialEq, Eq)]
 715pub struct CompletionsQuery {
 716    pub query: String,
 717    pub column: u64,
 718    pub line: Option<u64>,
 719    pub frame_id: Option<u64>,
 720}
 721
 722impl CompletionsQuery {
 723    pub fn new(
 724        buffer: &language::Buffer,
 725        cursor_position: language::Anchor,
 726        frame_id: Option<u64>,
 727    ) -> Self {
 728        let PointUtf16 { row, column } = cursor_position.to_point_utf16(&buffer.snapshot());
 729        Self {
 730            query: buffer.text(),
 731            column: column as u64,
 732            frame_id,
 733            line: Some(row as u64),
 734        }
 735    }
 736}
 737
 738#[derive(Debug)]
 739pub enum SessionEvent {
 740    Modules,
 741    LoadedSources,
 742    Stopped(Option<ThreadId>),
 743    StackTrace,
 744    Variables,
 745    Watchers,
 746    Threads,
 747    InvalidateInlineValue,
 748    CapabilitiesLoaded,
 749    RunInTerminal {
 750        request: RunInTerminalRequestArguments,
 751        sender: mpsc::Sender<Result<u32>>,
 752    },
 753    ConsoleOutput,
 754}
 755
 756#[derive(Clone, Debug, PartialEq, Eq)]
 757pub enum SessionStateEvent {
 758    Running,
 759    Shutdown,
 760    Restart,
 761    SpawnChildSession {
 762        request: StartDebuggingRequestArguments,
 763    },
 764}
 765
 766impl EventEmitter<SessionEvent> for Session {}
 767impl EventEmitter<SessionStateEvent> for Session {}
 768
 769// local session will send breakpoint updates to DAP for all new breakpoints
 770// remote side will only send breakpoint updates when it is a breakpoint created by that peer
 771// BreakpointStore notifies session on breakpoint changes
 772impl Session {
 773    pub(crate) fn new(
 774        breakpoint_store: Entity<BreakpointStore>,
 775        session_id: SessionId,
 776        parent_session: Option<Entity<Session>>,
 777        label: SharedString,
 778        adapter: DebugAdapterName,
 779        task_context: TaskContext,
 780        cx: &mut App,
 781    ) -> Entity<Self> {
 782        cx.new::<Self>(|cx| {
 783            cx.subscribe(&breakpoint_store, |this, store, event, cx| match event {
 784                BreakpointStoreEvent::BreakpointsUpdated(path, reason) => {
 785                    if let Some(local) = (!this.ignore_breakpoints)
 786                        .then(|| this.as_running_mut())
 787                        .flatten()
 788                    {
 789                        local
 790                            .send_breakpoints_from_path(path.clone(), *reason, &store, cx)
 791                            .detach();
 792                    };
 793                }
 794                BreakpointStoreEvent::BreakpointsCleared(paths) => {
 795                    if let Some(local) = (!this.ignore_breakpoints)
 796                        .then(|| this.as_running_mut())
 797                        .flatten()
 798                    {
 799                        local.unset_breakpoints_from_paths(paths, cx).detach();
 800                    }
 801                }
 802                BreakpointStoreEvent::SetDebugLine | BreakpointStoreEvent::ClearDebugLines => {}
 803            })
 804            .detach();
 805            // cx.on_app_quit(Self::on_app_quit).detach();
 806
 807            let this = Self {
 808                mode: Mode::Building,
 809                id: session_id,
 810                child_session_ids: HashSet::default(),
 811                parent_session,
 812                capabilities: Capabilities::default(),
 813                watchers: HashMap::default(),
 814                variables: Default::default(),
 815                stack_frames: Default::default(),
 816                thread_states: ThreadStates::default(),
 817                output_token: OutputToken(0),
 818                output: circular_buffer::CircularBuffer::boxed(),
 819                requests: HashMap::default(),
 820                modules: Vec::default(),
 821                loaded_sources: Vec::default(),
 822                threads: IndexMap::default(),
 823                background_tasks: Vec::default(),
 824                locations: Default::default(),
 825                is_session_terminated: false,
 826                ignore_breakpoints: false,
 827                breakpoint_store,
 828                exception_breakpoints: Default::default(),
 829                label,
 830                adapter,
 831                task_context,
 832            };
 833
 834            this
 835        })
 836    }
 837
 838    pub fn task_context(&self) -> &TaskContext {
 839        &self.task_context
 840    }
 841
 842    pub fn worktree(&self) -> Option<Entity<Worktree>> {
 843        match &self.mode {
 844            Mode::Building => None,
 845            Mode::Running(local_mode) => local_mode.worktree.upgrade(),
 846        }
 847    }
 848
 849    pub fn boot(
 850        &mut self,
 851        binary: DebugAdapterBinary,
 852        worktree: Entity<Worktree>,
 853        dap_store: WeakEntity<DapStore>,
 854        cx: &mut Context<Self>,
 855    ) -> Task<Result<()>> {
 856        let (message_tx, mut message_rx) = futures::channel::mpsc::unbounded();
 857        let (initialized_tx, initialized_rx) = futures::channel::oneshot::channel();
 858
 859        let background_tasks = vec![cx.spawn(async move |this: WeakEntity<Session>, cx| {
 860            let mut initialized_tx = Some(initialized_tx);
 861            while let Some(message) = message_rx.next().await {
 862                if let Message::Event(event) = message {
 863                    if let Events::Initialized(_) = *event {
 864                        if let Some(tx) = initialized_tx.take() {
 865                            tx.send(()).ok();
 866                        }
 867                    } else {
 868                        let Ok(_) = this.update(cx, |session, cx| {
 869                            session.handle_dap_event(event, cx);
 870                        }) else {
 871                            break;
 872                        };
 873                    }
 874                } else if let Message::Request(request) = message {
 875                    let Ok(_) = this.update(cx, |this, cx| {
 876                        if request.command == StartDebugging::COMMAND {
 877                            this.handle_start_debugging_request(request, cx)
 878                                .detach_and_log_err(cx);
 879                        } else if request.command == RunInTerminal::COMMAND {
 880                            this.handle_run_in_terminal_request(request, cx)
 881                                .detach_and_log_err(cx);
 882                        }
 883                    }) else {
 884                        break;
 885                    };
 886                }
 887            }
 888        })];
 889        self.background_tasks = background_tasks;
 890        let id = self.id;
 891        let parent_session = self.parent_session.clone();
 892
 893        cx.spawn(async move |this, cx| {
 894            let mode = RunningMode::new(
 895                id,
 896                parent_session,
 897                worktree.downgrade(),
 898                binary.clone(),
 899                message_tx,
 900                cx,
 901            )
 902            .await?;
 903            this.update(cx, |this, cx| {
 904                this.mode = Mode::Running(mode);
 905                cx.emit(SessionStateEvent::Running);
 906            })?;
 907
 908            this.update(cx, |session, cx| session.request_initialize(cx))?
 909                .await?;
 910
 911            let result = this
 912                .update(cx, |session, cx| {
 913                    session.initialize_sequence(initialized_rx, dap_store.clone(), cx)
 914                })?
 915                .await;
 916
 917            if result.is_err() {
 918                let mut console = this.update(cx, |session, cx| session.console_output(cx))?;
 919
 920                console
 921                    .send(format!(
 922                        "Tried to launch debugger with: {}",
 923                        serde_json::to_string_pretty(&binary.request_args.configuration)
 924                            .unwrap_or_default(),
 925                    ))
 926                    .await
 927                    .ok();
 928            }
 929
 930            result
 931        })
 932    }
 933
 934    pub fn session_id(&self) -> SessionId {
 935        self.id
 936    }
 937
 938    pub fn child_session_ids(&self) -> HashSet<SessionId> {
 939        self.child_session_ids.clone()
 940    }
 941
 942    pub fn add_child_session_id(&mut self, session_id: SessionId) {
 943        self.child_session_ids.insert(session_id);
 944    }
 945
 946    pub fn remove_child_session_id(&mut self, session_id: SessionId) {
 947        self.child_session_ids.remove(&session_id);
 948    }
 949
 950    pub fn parent_id(&self, cx: &App) -> Option<SessionId> {
 951        self.parent_session
 952            .as_ref()
 953            .map(|session| session.read(cx).id)
 954    }
 955
 956    pub fn parent_session(&self) -> Option<&Entity<Self>> {
 957        self.parent_session.as_ref()
 958    }
 959
 960    pub fn on_app_quit(&mut self, cx: &mut Context<Self>) -> Task<()> {
 961        let Some(client) = self.adapter_client() else {
 962            return Task::ready(());
 963        };
 964
 965        let supports_terminate = self
 966            .capabilities
 967            .support_terminate_debuggee
 968            .unwrap_or(false);
 969
 970        cx.background_spawn(async move {
 971            if supports_terminate {
 972                client
 973                    .request::<dap::requests::Terminate>(dap::TerminateArguments {
 974                        restart: Some(false),
 975                    })
 976                    .await
 977                    .ok();
 978            } else {
 979                client
 980                    .request::<dap::requests::Disconnect>(dap::DisconnectArguments {
 981                        restart: Some(false),
 982                        terminate_debuggee: Some(true),
 983                        suspend_debuggee: Some(false),
 984                    })
 985                    .await
 986                    .ok();
 987            }
 988        })
 989    }
 990
 991    pub fn capabilities(&self) -> &Capabilities {
 992        &self.capabilities
 993    }
 994
 995    pub fn binary(&self) -> Option<&DebugAdapterBinary> {
 996        match &self.mode {
 997            Mode::Building => None,
 998            Mode::Running(running_mode) => Some(&running_mode.binary),
 999        }
1000    }
1001
1002    pub fn adapter(&self) -> DebugAdapterName {
1003        self.adapter.clone()
1004    }
1005
1006    pub fn label(&self) -> SharedString {
1007        self.label.clone()
1008    }
1009
1010    pub fn is_terminated(&self) -> bool {
1011        self.is_session_terminated
1012    }
1013
1014    pub fn console_output(&mut self, cx: &mut Context<Self>) -> mpsc::UnboundedSender<String> {
1015        let (tx, mut rx) = mpsc::unbounded();
1016
1017        cx.spawn(async move |this, cx| {
1018            while let Some(output) = rx.next().await {
1019                this.update(cx, |this, _| {
1020                    let event = dap::OutputEvent {
1021                        category: None,
1022                        output,
1023                        group: None,
1024                        variables_reference: None,
1025                        source: None,
1026                        line: None,
1027                        column: None,
1028                        data: None,
1029                        location_reference: None,
1030                    };
1031                    this.push_output(event);
1032                })?;
1033            }
1034            anyhow::Ok(())
1035        })
1036        .detach();
1037
1038        return tx;
1039    }
1040
1041    pub fn is_started(&self) -> bool {
1042        match &self.mode {
1043            Mode::Building => false,
1044            Mode::Running(running) => running.is_started,
1045        }
1046    }
1047
1048    pub fn is_building(&self) -> bool {
1049        matches!(self.mode, Mode::Building)
1050    }
1051
1052    pub fn as_running_mut(&mut self) -> Option<&mut RunningMode> {
1053        match &mut self.mode {
1054            Mode::Running(local_mode) => Some(local_mode),
1055            Mode::Building => None,
1056        }
1057    }
1058
1059    pub fn as_running(&self) -> Option<&RunningMode> {
1060        match &self.mode {
1061            Mode::Running(local_mode) => Some(local_mode),
1062            Mode::Building => None,
1063        }
1064    }
1065
1066    fn handle_start_debugging_request(
1067        &mut self,
1068        request: dap::messages::Request,
1069        cx: &mut Context<Self>,
1070    ) -> Task<Result<()>> {
1071        let request_seq = request.seq;
1072
1073        let launch_request: Option<Result<StartDebuggingRequestArguments, _>> = request
1074            .arguments
1075            .as_ref()
1076            .map(|value| serde_json::from_value(value.clone()));
1077
1078        let mut success = true;
1079        if let Some(Ok(request)) = launch_request {
1080            cx.emit(SessionStateEvent::SpawnChildSession { request });
1081        } else {
1082            log::error!(
1083                "Failed to parse launch request arguments: {:?}",
1084                request.arguments
1085            );
1086            success = false;
1087        }
1088
1089        cx.spawn(async move |this, cx| {
1090            this.update(cx, |this, cx| {
1091                this.respond_to_client(
1092                    request_seq,
1093                    success,
1094                    StartDebugging::COMMAND.to_string(),
1095                    None,
1096                    cx,
1097                )
1098            })?
1099            .await
1100        })
1101    }
1102
1103    fn handle_run_in_terminal_request(
1104        &mut self,
1105        request: dap::messages::Request,
1106        cx: &mut Context<Self>,
1107    ) -> Task<Result<()>> {
1108        let request_args = match serde_json::from_value::<RunInTerminalRequestArguments>(
1109            request.arguments.unwrap_or_default(),
1110        ) {
1111            Ok(args) => args,
1112            Err(error) => {
1113                return cx.spawn(async move |session, cx| {
1114                    let error = serde_json::to_value(dap::ErrorResponse {
1115                        error: Some(dap::Message {
1116                            id: request.seq,
1117                            format: error.to_string(),
1118                            variables: None,
1119                            send_telemetry: None,
1120                            show_user: None,
1121                            url: None,
1122                            url_label: None,
1123                        }),
1124                    })
1125                    .ok();
1126
1127                    session
1128                        .update(cx, |this, cx| {
1129                            this.respond_to_client(
1130                                request.seq,
1131                                false,
1132                                StartDebugging::COMMAND.to_string(),
1133                                error,
1134                                cx,
1135                            )
1136                        })?
1137                        .await?;
1138
1139                    Err(anyhow!("Failed to parse RunInTerminalRequestArguments"))
1140                });
1141            }
1142        };
1143
1144        let seq = request.seq;
1145
1146        let (tx, mut rx) = mpsc::channel::<Result<u32>>(1);
1147        cx.emit(SessionEvent::RunInTerminal {
1148            request: request_args,
1149            sender: tx,
1150        });
1151        cx.notify();
1152
1153        cx.spawn(async move |session, cx| {
1154            let result = util::maybe!(async move {
1155                rx.next().await.ok_or_else(|| {
1156                    anyhow!("failed to receive response from spawn terminal".to_string())
1157                })?
1158            })
1159            .await;
1160            let (success, body) = match result {
1161                Ok(pid) => (
1162                    true,
1163                    serde_json::to_value(dap::RunInTerminalResponse {
1164                        process_id: None,
1165                        shell_process_id: Some(pid as u64),
1166                    })
1167                    .ok(),
1168                ),
1169                Err(error) => (
1170                    false,
1171                    serde_json::to_value(dap::ErrorResponse {
1172                        error: Some(dap::Message {
1173                            id: seq,
1174                            format: error.to_string(),
1175                            variables: None,
1176                            send_telemetry: None,
1177                            show_user: None,
1178                            url: None,
1179                            url_label: None,
1180                        }),
1181                    })
1182                    .ok(),
1183                ),
1184            };
1185
1186            session
1187                .update(cx, |session, cx| {
1188                    session.respond_to_client(
1189                        seq,
1190                        success,
1191                        RunInTerminal::COMMAND.to_string(),
1192                        body,
1193                        cx,
1194                    )
1195                })?
1196                .await
1197        })
1198    }
1199
1200    pub(super) fn request_initialize(&mut self, cx: &mut Context<Self>) -> Task<Result<()>> {
1201        let adapter_id = self.adapter().to_string();
1202        let request = Initialize { adapter_id };
1203
1204        let Mode::Running(running) = &self.mode else {
1205            return Task::ready(Err(anyhow!(
1206                "Cannot send initialize request, task still building"
1207            )));
1208        };
1209        let mut response = running.request(request.clone());
1210
1211        cx.spawn(async move |this, cx| {
1212            loop {
1213                let capabilities = response.await;
1214                match capabilities {
1215                    Err(e) => {
1216                        let Ok(Some(reconnect)) = this.update(cx, |this, cx| {
1217                            this.as_running()
1218                                .and_then(|running| running.reconnect_for_ssh(&mut cx.to_async()))
1219                        }) else {
1220                            return Err(e);
1221                        };
1222                        log::info!("Failed to connect to debug adapter: {}, retrying...", e);
1223                        reconnect.await?;
1224
1225                        let Ok(Some(r)) = this.update(cx, |this, _| {
1226                            this.as_running()
1227                                .map(|running| running.request(request.clone()))
1228                        }) else {
1229                            return Err(e);
1230                        };
1231                        response = r
1232                    }
1233                    Ok(capabilities) => {
1234                        this.update(cx, |session, cx| {
1235                            session.capabilities = capabilities;
1236                            let filters = session
1237                                .capabilities
1238                                .exception_breakpoint_filters
1239                                .clone()
1240                                .unwrap_or_default();
1241                            for filter in filters {
1242                                let default = filter.default.unwrap_or_default();
1243                                session
1244                                    .exception_breakpoints
1245                                    .entry(filter.filter.clone())
1246                                    .or_insert_with(|| (filter, default));
1247                            }
1248                            cx.emit(SessionEvent::CapabilitiesLoaded);
1249                        })?;
1250                        return Ok(());
1251                    }
1252                }
1253            }
1254        })
1255    }
1256
1257    pub(super) fn initialize_sequence(
1258        &mut self,
1259        initialize_rx: oneshot::Receiver<()>,
1260        dap_store: WeakEntity<DapStore>,
1261        cx: &mut Context<Self>,
1262    ) -> Task<Result<()>> {
1263        match &self.mode {
1264            Mode::Running(local_mode) => {
1265                local_mode.initialize_sequence(&self.capabilities, initialize_rx, dap_store, cx)
1266            }
1267            Mode::Building => Task::ready(Err(anyhow!("cannot initialize, still building"))),
1268        }
1269    }
1270
1271    pub fn run_to_position(
1272        &mut self,
1273        breakpoint: SourceBreakpoint,
1274        active_thread_id: ThreadId,
1275        cx: &mut Context<Self>,
1276    ) {
1277        match &mut self.mode {
1278            Mode::Running(local_mode) => {
1279                if !matches!(
1280                    self.thread_states.thread_state(active_thread_id),
1281                    Some(ThreadStatus::Stopped)
1282                ) {
1283                    return;
1284                };
1285                let path = breakpoint.path.clone();
1286                local_mode.tmp_breakpoint = Some(breakpoint);
1287                let task = local_mode.send_breakpoints_from_path(
1288                    path,
1289                    BreakpointUpdatedReason::Toggled,
1290                    &self.breakpoint_store,
1291                    cx,
1292                );
1293
1294                cx.spawn(async move |this, cx| {
1295                    task.await;
1296                    this.update(cx, |this, cx| {
1297                        this.continue_thread(active_thread_id, cx);
1298                    })
1299                })
1300                .detach();
1301            }
1302            Mode::Building => {}
1303        }
1304    }
1305
1306    pub fn has_new_output(&self, last_update: OutputToken) -> bool {
1307        self.output_token.0.checked_sub(last_update.0).unwrap_or(0) != 0
1308    }
1309
1310    pub fn output(
1311        &self,
1312        since: OutputToken,
1313    ) -> (impl Iterator<Item = &dap::OutputEvent>, OutputToken) {
1314        if self.output_token.0 == 0 {
1315            return (self.output.range(0..0), OutputToken(0));
1316        };
1317
1318        let events_since = self.output_token.0.checked_sub(since.0).unwrap_or(0);
1319
1320        let clamped_events_since = events_since.clamp(0, self.output.len());
1321        (
1322            self.output
1323                .range(self.output.len() - clamped_events_since..),
1324            self.output_token,
1325        )
1326    }
1327
1328    pub fn respond_to_client(
1329        &self,
1330        request_seq: u64,
1331        success: bool,
1332        command: String,
1333        body: Option<serde_json::Value>,
1334        cx: &mut Context<Self>,
1335    ) -> Task<Result<()>> {
1336        let Some(local_session) = self.as_running() else {
1337            unreachable!("Cannot respond to remote client");
1338        };
1339        let client = local_session.client.clone();
1340
1341        cx.background_spawn(async move {
1342            client
1343                .send_message(Message::Response(Response {
1344                    body,
1345                    success,
1346                    command,
1347                    seq: request_seq + 1,
1348                    request_seq,
1349                    message: None,
1350                }))
1351                .await
1352        })
1353    }
1354
1355    fn handle_stopped_event(&mut self, event: StoppedEvent, cx: &mut Context<Self>) {
1356        self.mode.stopped();
1357        // todo(debugger): Find a clean way to get around the clone
1358        let breakpoint_store = self.breakpoint_store.clone();
1359        if let Some((local, path)) = self.as_running_mut().and_then(|local| {
1360            let breakpoint = local.tmp_breakpoint.take()?;
1361            let path = breakpoint.path.clone();
1362            Some((local, path))
1363        }) {
1364            local
1365                .send_breakpoints_from_path(
1366                    path,
1367                    BreakpointUpdatedReason::Toggled,
1368                    &breakpoint_store,
1369                    cx,
1370                )
1371                .detach();
1372        };
1373
1374        if event.all_threads_stopped.unwrap_or_default() || event.thread_id.is_none() {
1375            self.thread_states.stop_all_threads();
1376            self.invalidate_command_type::<StackTraceCommand>();
1377        }
1378
1379        // Event if we stopped all threads we still need to insert the thread_id
1380        // to our own data
1381        if let Some(thread_id) = event.thread_id {
1382            self.thread_states.stop_thread(ThreadId(thread_id));
1383
1384            self.invalidate_state(
1385                &StackTraceCommand {
1386                    thread_id,
1387                    start_frame: None,
1388                    levels: None,
1389                }
1390                .into(),
1391            );
1392        }
1393
1394        self.invalidate_generic();
1395        self.threads.clear();
1396        self.variables.clear();
1397        cx.emit(SessionEvent::Stopped(
1398            event
1399                .thread_id
1400                .map(Into::into)
1401                .filter(|_| !event.preserve_focus_hint.unwrap_or(false)),
1402        ));
1403        cx.emit(SessionEvent::InvalidateInlineValue);
1404        cx.notify();
1405    }
1406
1407    pub(crate) fn handle_dap_event(&mut self, event: Box<Events>, cx: &mut Context<Self>) {
1408        match *event {
1409            Events::Initialized(_) => {
1410                debug_assert!(
1411                    false,
1412                    "Initialized event should have been handled in LocalMode"
1413                );
1414            }
1415            Events::Stopped(event) => self.handle_stopped_event(event, cx),
1416            Events::Continued(event) => {
1417                if event.all_threads_continued.unwrap_or_default() {
1418                    self.thread_states.continue_all_threads();
1419                    self.breakpoint_store.update(cx, |store, cx| {
1420                        store.remove_active_position(Some(self.session_id()), cx)
1421                    });
1422                } else {
1423                    self.thread_states
1424                        .continue_thread(ThreadId(event.thread_id));
1425                }
1426                // todo(debugger): We should be able to get away with only invalidating generic if all threads were continued
1427                self.invalidate_generic();
1428            }
1429            Events::Exited(_event) => {
1430                self.clear_active_debug_line(cx);
1431            }
1432            Events::Terminated(_) => {
1433                self.shutdown(cx).detach();
1434            }
1435            Events::Thread(event) => {
1436                let thread_id = ThreadId(event.thread_id);
1437
1438                match event.reason {
1439                    dap::ThreadEventReason::Started => {
1440                        self.thread_states.continue_thread(thread_id);
1441                    }
1442                    dap::ThreadEventReason::Exited => {
1443                        self.thread_states.exit_thread(thread_id);
1444                    }
1445                    reason => {
1446                        log::error!("Unhandled thread event reason {:?}", reason);
1447                    }
1448                }
1449                self.invalidate_state(&ThreadsCommand.into());
1450                cx.notify();
1451            }
1452            Events::Output(event) => {
1453                if event
1454                    .category
1455                    .as_ref()
1456                    .is_some_and(|category| *category == OutputEventCategory::Telemetry)
1457                {
1458                    return;
1459                }
1460
1461                self.push_output(event);
1462                cx.notify();
1463            }
1464            Events::Breakpoint(event) => self.breakpoint_store.update(cx, |store, _| {
1465                store.update_session_breakpoint(self.session_id(), event.reason, event.breakpoint);
1466            }),
1467            Events::Module(event) => {
1468                match event.reason {
1469                    dap::ModuleEventReason::New => {
1470                        self.modules.push(event.module);
1471                    }
1472                    dap::ModuleEventReason::Changed => {
1473                        if let Some(module) = self
1474                            .modules
1475                            .iter_mut()
1476                            .find(|other| event.module.id == other.id)
1477                        {
1478                            *module = event.module;
1479                        }
1480                    }
1481                    dap::ModuleEventReason::Removed => {
1482                        self.modules.retain(|other| event.module.id != other.id);
1483                    }
1484                }
1485
1486                // todo(debugger): We should only send the invalidate command to downstream clients.
1487                // self.invalidate_state(&ModulesCommand.into());
1488            }
1489            Events::LoadedSource(_) => {
1490                self.invalidate_state(&LoadedSourcesCommand.into());
1491            }
1492            Events::Capabilities(event) => {
1493                self.capabilities = self.capabilities.merge(event.capabilities);
1494
1495                // The adapter might've enabled new exception breakpoints (or disabled existing ones).
1496                let recent_filters = self
1497                    .capabilities
1498                    .exception_breakpoint_filters
1499                    .iter()
1500                    .flatten()
1501                    .map(|filter| (filter.filter.clone(), filter.clone()))
1502                    .collect::<BTreeMap<_, _>>();
1503                for filter in recent_filters.values() {
1504                    let default = filter.default.unwrap_or_default();
1505                    self.exception_breakpoints
1506                        .entry(filter.filter.clone())
1507                        .or_insert_with(|| (filter.clone(), default));
1508                }
1509                self.exception_breakpoints
1510                    .retain(|k, _| recent_filters.contains_key(k));
1511                if self.is_started() {
1512                    self.send_exception_breakpoints(cx);
1513                }
1514
1515                // Remove the ones that no longer exist.
1516                cx.notify();
1517            }
1518            Events::Memory(_) => {}
1519            Events::Process(_) => {}
1520            Events::ProgressEnd(_) => {}
1521            Events::ProgressStart(_) => {}
1522            Events::ProgressUpdate(_) => {}
1523            Events::Invalidated(_) => {}
1524            Events::Other(_) => {}
1525        }
1526    }
1527
1528    /// Ensure that there's a request in flight for the given command, and if not, send it. Use this to run requests that are idempotent.
1529    fn fetch<T: DapCommand + PartialEq + Eq + Hash>(
1530        &mut self,
1531        request: T,
1532        process_result: impl FnOnce(&mut Self, Result<T::Response>, &mut Context<Self>) + 'static,
1533        cx: &mut Context<Self>,
1534    ) {
1535        const {
1536            assert!(
1537                T::CACHEABLE,
1538                "Only requests marked as cacheable should invoke `fetch`"
1539            );
1540        }
1541
1542        if !self.thread_states.any_stopped_thread()
1543            && request.type_id() != TypeId::of::<ThreadsCommand>()
1544            || self.is_session_terminated
1545        {
1546            return;
1547        }
1548
1549        let request_map = self
1550            .requests
1551            .entry(std::any::TypeId::of::<T>())
1552            .or_default();
1553
1554        if let Entry::Vacant(vacant) = request_map.entry(request.into()) {
1555            let command = vacant.key().0.clone().as_any_arc().downcast::<T>().unwrap();
1556
1557            let task = Self::request_inner::<Arc<T>>(
1558                &self.capabilities,
1559                &self.mode,
1560                command,
1561                |this, result, cx| {
1562                    process_result(this, result, cx);
1563                    None
1564                },
1565                cx,
1566            );
1567            let task = cx
1568                .background_executor()
1569                .spawn(async move {
1570                    let _ = task.await?;
1571                    Some(())
1572                })
1573                .shared();
1574
1575            vacant.insert(task);
1576            cx.notify();
1577        }
1578    }
1579
1580    fn request_inner<T: DapCommand + PartialEq + Eq + Hash>(
1581        capabilities: &Capabilities,
1582        mode: &Mode,
1583        request: T,
1584        process_result: impl FnOnce(
1585            &mut Self,
1586            Result<T::Response>,
1587            &mut Context<Self>,
1588        ) -> Option<T::Response>
1589        + 'static,
1590        cx: &mut Context<Self>,
1591    ) -> Task<Option<T::Response>> {
1592        if !T::is_supported(&capabilities) {
1593            log::warn!(
1594                "Attempted to send a DAP request that isn't supported: {:?}",
1595                request
1596            );
1597            let error = Err(anyhow::Error::msg(
1598                "Couldn't complete request because it's not supported",
1599            ));
1600            return cx.spawn(async move |this, cx| {
1601                this.update(cx, |this, cx| process_result(this, error, cx))
1602                    .ok()
1603                    .flatten()
1604            });
1605        }
1606
1607        let request = mode.request_dap(request);
1608        cx.spawn(async move |this, cx| {
1609            let result = request.await;
1610            this.update(cx, |this, cx| process_result(this, result, cx))
1611                .ok()
1612                .flatten()
1613        })
1614    }
1615
1616    fn request<T: DapCommand + PartialEq + Eq + Hash>(
1617        &self,
1618        request: T,
1619        process_result: impl FnOnce(
1620            &mut Self,
1621            Result<T::Response>,
1622            &mut Context<Self>,
1623        ) -> Option<T::Response>
1624        + 'static,
1625        cx: &mut Context<Self>,
1626    ) -> Task<Option<T::Response>> {
1627        Self::request_inner(&self.capabilities, &self.mode, request, process_result, cx)
1628    }
1629
1630    fn invalidate_command_type<Command: DapCommand>(&mut self) {
1631        self.requests.remove(&std::any::TypeId::of::<Command>());
1632    }
1633
1634    fn invalidate_generic(&mut self) {
1635        self.invalidate_command_type::<ModulesCommand>();
1636        self.invalidate_command_type::<LoadedSourcesCommand>();
1637        self.invalidate_command_type::<ThreadsCommand>();
1638    }
1639
1640    fn invalidate_state(&mut self, key: &RequestSlot) {
1641        self.requests
1642            .entry((&*key.0 as &dyn Any).type_id())
1643            .and_modify(|request_map| {
1644                request_map.remove(&key);
1645            });
1646    }
1647
1648    fn push_output(&mut self, event: OutputEvent) {
1649        self.output.push_back(event);
1650        self.output_token.0 += 1;
1651    }
1652
1653    pub fn any_stopped_thread(&self) -> bool {
1654        self.thread_states.any_stopped_thread()
1655    }
1656
1657    pub fn thread_status(&self, thread_id: ThreadId) -> ThreadStatus {
1658        self.thread_states.thread_status(thread_id)
1659    }
1660
1661    pub fn threads(&mut self, cx: &mut Context<Self>) -> Vec<(dap::Thread, ThreadStatus)> {
1662        self.fetch(
1663            dap_command::ThreadsCommand,
1664            |this, result, cx| {
1665                let Some(result) = result.log_err() else {
1666                    return;
1667                };
1668
1669                this.threads = result
1670                    .into_iter()
1671                    .map(|thread| (ThreadId(thread.id), Thread::from(thread.clone())))
1672                    .collect();
1673
1674                this.invalidate_command_type::<StackTraceCommand>();
1675                cx.emit(SessionEvent::Threads);
1676                cx.notify();
1677            },
1678            cx,
1679        );
1680
1681        self.threads
1682            .values()
1683            .map(|thread| {
1684                (
1685                    thread.dap.clone(),
1686                    self.thread_states.thread_status(ThreadId(thread.dap.id)),
1687                )
1688            })
1689            .collect()
1690    }
1691
1692    pub fn modules(&mut self, cx: &mut Context<Self>) -> &[Module] {
1693        self.fetch(
1694            dap_command::ModulesCommand,
1695            |this, result, cx| {
1696                let Some(result) = result.log_err() else {
1697                    return;
1698                };
1699
1700                this.modules = result;
1701                cx.emit(SessionEvent::Modules);
1702                cx.notify();
1703            },
1704            cx,
1705        );
1706
1707        &self.modules
1708    }
1709
1710    pub fn ignore_breakpoints(&self) -> bool {
1711        self.ignore_breakpoints
1712    }
1713
1714    pub fn toggle_ignore_breakpoints(
1715        &mut self,
1716        cx: &mut App,
1717    ) -> Task<HashMap<Arc<Path>, anyhow::Error>> {
1718        self.set_ignore_breakpoints(!self.ignore_breakpoints, cx)
1719    }
1720
1721    pub(crate) fn set_ignore_breakpoints(
1722        &mut self,
1723        ignore: bool,
1724        cx: &mut App,
1725    ) -> Task<HashMap<Arc<Path>, anyhow::Error>> {
1726        if self.ignore_breakpoints == ignore {
1727            return Task::ready(HashMap::default());
1728        }
1729
1730        self.ignore_breakpoints = ignore;
1731
1732        if let Some(local) = self.as_running() {
1733            local.send_source_breakpoints(ignore, &self.breakpoint_store, cx)
1734        } else {
1735            // todo(debugger): We need to propagate this change to downstream sessions and send a message to upstream sessions
1736            unimplemented!()
1737        }
1738    }
1739
1740    pub fn exception_breakpoints(
1741        &self,
1742    ) -> impl Iterator<Item = &(ExceptionBreakpointsFilter, IsEnabled)> {
1743        self.exception_breakpoints.values()
1744    }
1745
1746    pub fn toggle_exception_breakpoint(&mut self, id: &str, cx: &App) {
1747        if let Some((_, is_enabled)) = self.exception_breakpoints.get_mut(id) {
1748            *is_enabled = !*is_enabled;
1749            self.send_exception_breakpoints(cx);
1750        }
1751    }
1752
1753    fn send_exception_breakpoints(&mut self, cx: &App) {
1754        if let Some(local) = self.as_running() {
1755            let exception_filters = self
1756                .exception_breakpoints
1757                .values()
1758                .filter_map(|(filter, is_enabled)| is_enabled.then(|| filter.clone()))
1759                .collect();
1760
1761            let supports_exception_filters = self
1762                .capabilities
1763                .supports_exception_filter_options
1764                .unwrap_or_default();
1765            local
1766                .send_exception_breakpoints(exception_filters, supports_exception_filters)
1767                .detach_and_log_err(cx);
1768        } else {
1769            debug_assert!(false, "Not implemented");
1770        }
1771    }
1772
1773    pub fn breakpoints_enabled(&self) -> bool {
1774        self.ignore_breakpoints
1775    }
1776
1777    pub fn loaded_sources(&mut self, cx: &mut Context<Self>) -> &[Source] {
1778        self.fetch(
1779            dap_command::LoadedSourcesCommand,
1780            |this, result, cx| {
1781                let Some(result) = result.log_err() else {
1782                    return;
1783                };
1784                this.loaded_sources = result;
1785                cx.emit(SessionEvent::LoadedSources);
1786                cx.notify();
1787            },
1788            cx,
1789        );
1790
1791        &self.loaded_sources
1792    }
1793
1794    fn fallback_to_manual_restart(
1795        &mut self,
1796        res: Result<()>,
1797        cx: &mut Context<Self>,
1798    ) -> Option<()> {
1799        if res.log_err().is_none() {
1800            cx.emit(SessionStateEvent::Restart);
1801            return None;
1802        }
1803        Some(())
1804    }
1805
1806    fn empty_response(&mut self, res: Result<()>, _cx: &mut Context<Self>) -> Option<()> {
1807        res.log_err()?;
1808        Some(())
1809    }
1810
1811    fn on_step_response<T: DapCommand + PartialEq + Eq + Hash>(
1812        thread_id: ThreadId,
1813    ) -> impl FnOnce(&mut Self, Result<T::Response>, &mut Context<Self>) -> Option<T::Response> + 'static
1814    {
1815        move |this, response, cx| match response.log_err() {
1816            Some(response) => {
1817                this.breakpoint_store.update(cx, |store, cx| {
1818                    store.remove_active_position(Some(this.session_id()), cx)
1819                });
1820                Some(response)
1821            }
1822            None => {
1823                this.thread_states.stop_thread(thread_id);
1824                cx.notify();
1825                None
1826            }
1827        }
1828    }
1829
1830    fn clear_active_debug_line_response(
1831        &mut self,
1832        response: Result<()>,
1833        cx: &mut Context<Session>,
1834    ) -> Option<()> {
1835        response.log_err()?;
1836        self.clear_active_debug_line(cx);
1837        Some(())
1838    }
1839
1840    fn clear_active_debug_line(&mut self, cx: &mut Context<Session>) {
1841        self.breakpoint_store.update(cx, |store, cx| {
1842            store.remove_active_position(Some(self.id), cx)
1843        });
1844    }
1845
1846    pub fn pause_thread(&mut self, thread_id: ThreadId, cx: &mut Context<Self>) {
1847        self.request(
1848            PauseCommand {
1849                thread_id: thread_id.0,
1850            },
1851            Self::empty_response,
1852            cx,
1853        )
1854        .detach();
1855    }
1856
1857    pub fn restart_stack_frame(&mut self, stack_frame_id: u64, cx: &mut Context<Self>) {
1858        self.request(
1859            RestartStackFrameCommand { stack_frame_id },
1860            Self::empty_response,
1861            cx,
1862        )
1863        .detach();
1864    }
1865
1866    pub fn restart(&mut self, args: Option<Value>, cx: &mut Context<Self>) {
1867        if self.capabilities.supports_restart_request.unwrap_or(false) && !self.is_terminated() {
1868            self.request(
1869                RestartCommand {
1870                    raw: args.unwrap_or(Value::Null),
1871                },
1872                Self::fallback_to_manual_restart,
1873                cx,
1874            )
1875            .detach();
1876        } else {
1877            cx.emit(SessionStateEvent::Restart);
1878        }
1879    }
1880
1881    pub fn shutdown(&mut self, cx: &mut Context<Self>) -> Task<()> {
1882        if self.is_session_terminated {
1883            return Task::ready(());
1884        }
1885
1886        self.is_session_terminated = true;
1887        self.thread_states.exit_all_threads();
1888        cx.notify();
1889
1890        let task = if self
1891            .capabilities
1892            .supports_terminate_request
1893            .unwrap_or_default()
1894        {
1895            self.request(
1896                TerminateCommand {
1897                    restart: Some(false),
1898                },
1899                Self::clear_active_debug_line_response,
1900                cx,
1901            )
1902        } else {
1903            self.request(
1904                DisconnectCommand {
1905                    restart: Some(false),
1906                    terminate_debuggee: Some(true),
1907                    suspend_debuggee: Some(false),
1908                },
1909                Self::clear_active_debug_line_response,
1910                cx,
1911            )
1912        };
1913
1914        cx.emit(SessionStateEvent::Shutdown);
1915
1916        cx.spawn(async move |_, _| {
1917            task.await;
1918        })
1919    }
1920
1921    pub fn completions(
1922        &mut self,
1923        query: CompletionsQuery,
1924        cx: &mut Context<Self>,
1925    ) -> Task<Result<Vec<dap::CompletionItem>>> {
1926        let task = self.request(query, |_, result, _| result.log_err(), cx);
1927
1928        cx.background_executor().spawn(async move {
1929            anyhow::Ok(
1930                task.await
1931                    .map(|response| response.targets)
1932                    .context("failed to fetch completions")?,
1933            )
1934        })
1935    }
1936
1937    pub fn continue_thread(&mut self, thread_id: ThreadId, cx: &mut Context<Self>) {
1938        let supports_single_thread_execution_requests =
1939            self.capabilities.supports_single_thread_execution_requests;
1940        self.thread_states.continue_thread(thread_id);
1941        self.request(
1942            ContinueCommand {
1943                args: ContinueArguments {
1944                    thread_id: thread_id.0,
1945                    single_thread: supports_single_thread_execution_requests,
1946                },
1947            },
1948            Self::on_step_response::<ContinueCommand>(thread_id),
1949            cx,
1950        )
1951        .detach();
1952    }
1953
1954    pub fn adapter_client(&self) -> Option<Arc<DebugAdapterClient>> {
1955        match self.mode {
1956            Mode::Running(ref local) => Some(local.client.clone()),
1957            Mode::Building => None,
1958        }
1959    }
1960
1961    pub fn has_ever_stopped(&self) -> bool {
1962        self.mode.has_ever_stopped()
1963    }
1964    pub fn step_over(
1965        &mut self,
1966        thread_id: ThreadId,
1967        granularity: SteppingGranularity,
1968        cx: &mut Context<Self>,
1969    ) {
1970        let supports_single_thread_execution_requests =
1971            self.capabilities.supports_single_thread_execution_requests;
1972        let supports_stepping_granularity = self
1973            .capabilities
1974            .supports_stepping_granularity
1975            .unwrap_or_default();
1976
1977        let command = NextCommand {
1978            inner: StepCommand {
1979                thread_id: thread_id.0,
1980                granularity: supports_stepping_granularity.then(|| granularity),
1981                single_thread: supports_single_thread_execution_requests,
1982            },
1983        };
1984
1985        self.thread_states.process_step(thread_id);
1986        self.request(
1987            command,
1988            Self::on_step_response::<NextCommand>(thread_id),
1989            cx,
1990        )
1991        .detach();
1992    }
1993
1994    pub fn step_in(
1995        &mut self,
1996        thread_id: ThreadId,
1997        granularity: SteppingGranularity,
1998        cx: &mut Context<Self>,
1999    ) {
2000        let supports_single_thread_execution_requests =
2001            self.capabilities.supports_single_thread_execution_requests;
2002        let supports_stepping_granularity = self
2003            .capabilities
2004            .supports_stepping_granularity
2005            .unwrap_or_default();
2006
2007        let command = StepInCommand {
2008            inner: StepCommand {
2009                thread_id: thread_id.0,
2010                granularity: supports_stepping_granularity.then(|| granularity),
2011                single_thread: supports_single_thread_execution_requests,
2012            },
2013        };
2014
2015        self.thread_states.process_step(thread_id);
2016        self.request(
2017            command,
2018            Self::on_step_response::<StepInCommand>(thread_id),
2019            cx,
2020        )
2021        .detach();
2022    }
2023
2024    pub fn step_out(
2025        &mut self,
2026        thread_id: ThreadId,
2027        granularity: SteppingGranularity,
2028        cx: &mut Context<Self>,
2029    ) {
2030        let supports_single_thread_execution_requests =
2031            self.capabilities.supports_single_thread_execution_requests;
2032        let supports_stepping_granularity = self
2033            .capabilities
2034            .supports_stepping_granularity
2035            .unwrap_or_default();
2036
2037        let command = StepOutCommand {
2038            inner: StepCommand {
2039                thread_id: thread_id.0,
2040                granularity: supports_stepping_granularity.then(|| granularity),
2041                single_thread: supports_single_thread_execution_requests,
2042            },
2043        };
2044
2045        self.thread_states.process_step(thread_id);
2046        self.request(
2047            command,
2048            Self::on_step_response::<StepOutCommand>(thread_id),
2049            cx,
2050        )
2051        .detach();
2052    }
2053
2054    pub fn step_back(
2055        &mut self,
2056        thread_id: ThreadId,
2057        granularity: SteppingGranularity,
2058        cx: &mut Context<Self>,
2059    ) {
2060        let supports_single_thread_execution_requests =
2061            self.capabilities.supports_single_thread_execution_requests;
2062        let supports_stepping_granularity = self
2063            .capabilities
2064            .supports_stepping_granularity
2065            .unwrap_or_default();
2066
2067        let command = StepBackCommand {
2068            inner: StepCommand {
2069                thread_id: thread_id.0,
2070                granularity: supports_stepping_granularity.then(|| granularity),
2071                single_thread: supports_single_thread_execution_requests,
2072            },
2073        };
2074
2075        self.thread_states.process_step(thread_id);
2076
2077        self.request(
2078            command,
2079            Self::on_step_response::<StepBackCommand>(thread_id),
2080            cx,
2081        )
2082        .detach();
2083    }
2084
2085    pub fn stack_frames(
2086        &mut self,
2087        thread_id: ThreadId,
2088        cx: &mut Context<Self>,
2089    ) -> Result<Vec<StackFrame>> {
2090        if self.thread_states.thread_status(thread_id) == ThreadStatus::Stopped
2091            && self.requests.contains_key(&ThreadsCommand.type_id())
2092            && self.threads.contains_key(&thread_id)
2093        // ^ todo(debugger): We need a better way to check that we're not querying stale data
2094        // We could still be using an old thread id and have sent a new thread's request
2095        // This isn't the biggest concern right now because it hasn't caused any issues outside of tests
2096        // But it very well could cause a minor bug in the future that is hard to track down
2097        {
2098            self.fetch(
2099                super::dap_command::StackTraceCommand {
2100                    thread_id: thread_id.0,
2101                    start_frame: None,
2102                    levels: None,
2103                },
2104                move |this, stack_frames, cx| {
2105                    let entry =
2106                        this.threads
2107                            .entry(thread_id)
2108                            .and_modify(|thread| match &stack_frames {
2109                                Ok(stack_frames) => {
2110                                    thread.stack_frames = stack_frames
2111                                        .iter()
2112                                        .cloned()
2113                                        .map(StackFrame::from)
2114                                        .collect();
2115                                    thread.stack_frames_error = None;
2116                                }
2117                                Err(error) => {
2118                                    thread.stack_frames.clear();
2119                                    thread.stack_frames_error = Some(error.cloned());
2120                                }
2121                            });
2122                    debug_assert!(
2123                        matches!(entry, indexmap::map::Entry::Occupied(_)),
2124                        "Sent request for thread_id that doesn't exist"
2125                    );
2126                    if let Ok(stack_frames) = stack_frames {
2127                        this.stack_frames.extend(
2128                            stack_frames
2129                                .into_iter()
2130                                .filter(|frame| {
2131                                    // Workaround for JavaScript debug adapter sending out "fake" stack frames for delineating await points. This is fine,
2132                                    // except that they always use an id of 0 for it, which collides with other (valid) stack frames.
2133                                    !(frame.id == 0
2134                                        && frame.line == 0
2135                                        && frame.column == 0
2136                                        && frame.presentation_hint
2137                                            == Some(StackFramePresentationHint::Label))
2138                                })
2139                                .map(|frame| (frame.id, StackFrame::from(frame))),
2140                        );
2141                    }
2142
2143                    this.invalidate_command_type::<ScopesCommand>();
2144                    this.invalidate_command_type::<VariablesCommand>();
2145
2146                    cx.emit(SessionEvent::StackTrace);
2147                },
2148                cx,
2149            );
2150        }
2151
2152        match self.threads.get(&thread_id) {
2153            Some(thread) => {
2154                if let Some(error) = &thread.stack_frames_error {
2155                    Err(error.cloned())
2156                } else {
2157                    Ok(thread.stack_frames.clone())
2158                }
2159            }
2160            None => Ok(Vec::new()),
2161        }
2162    }
2163
2164    pub fn scopes(&mut self, stack_frame_id: u64, cx: &mut Context<Self>) -> &[dap::Scope] {
2165        if self.requests.contains_key(&TypeId::of::<ThreadsCommand>())
2166            && self
2167                .requests
2168                .contains_key(&TypeId::of::<StackTraceCommand>())
2169        {
2170            self.fetch(
2171                ScopesCommand { stack_frame_id },
2172                move |this, scopes, cx| {
2173                    let Some(scopes) = scopes.log_err() else {
2174                        return
2175                    };
2176
2177                    for scope in scopes.iter() {
2178                        this.variables(scope.variables_reference, cx);
2179                    }
2180
2181                    let entry = this
2182                        .stack_frames
2183                        .entry(stack_frame_id)
2184                        .and_modify(|stack_frame| {
2185                            stack_frame.scopes = scopes;
2186                        });
2187
2188                    cx.emit(SessionEvent::Variables);
2189
2190                    debug_assert!(
2191                        matches!(entry, indexmap::map::Entry::Occupied(_)),
2192                        "Sent scopes request for stack_frame_id that doesn't exist or hasn't been fetched"
2193                    );
2194                },
2195                cx,
2196            );
2197        }
2198
2199        self.stack_frames
2200            .get(&stack_frame_id)
2201            .map(|frame| frame.scopes.as_slice())
2202            .unwrap_or_default()
2203    }
2204
2205    pub fn variables_by_stack_frame_id(
2206        &self,
2207        stack_frame_id: StackFrameId,
2208        globals: bool,
2209        locals: bool,
2210    ) -> Vec<dap::Variable> {
2211        let Some(stack_frame) = self.stack_frames.get(&stack_frame_id) else {
2212            return Vec::new();
2213        };
2214
2215        stack_frame
2216            .scopes
2217            .iter()
2218            .filter(|scope| {
2219                (scope.name.to_lowercase().contains("local") && locals)
2220                    || (scope.name.to_lowercase().contains("global") && globals)
2221            })
2222            .filter_map(|scope| self.variables.get(&scope.variables_reference))
2223            .flatten()
2224            .cloned()
2225            .collect()
2226    }
2227
2228    pub fn watchers(&self) -> &HashMap<SharedString, Watcher> {
2229        &self.watchers
2230    }
2231
2232    pub fn add_watcher(
2233        &mut self,
2234        expression: SharedString,
2235        frame_id: u64,
2236        cx: &mut Context<Self>,
2237    ) -> Task<Result<()>> {
2238        let request = self.mode.request_dap(EvaluateCommand {
2239            expression: expression.to_string(),
2240            context: Some(EvaluateArgumentsContext::Watch),
2241            frame_id: Some(frame_id),
2242            source: None,
2243        });
2244
2245        cx.spawn(async move |this, cx| {
2246            let response = request.await?;
2247
2248            this.update(cx, |session, cx| {
2249                session.watchers.insert(
2250                    expression.clone(),
2251                    Watcher {
2252                        expression,
2253                        value: response.result.into(),
2254                        variables_reference: response.variables_reference,
2255                        presentation_hint: response.presentation_hint,
2256                    },
2257                );
2258                cx.emit(SessionEvent::Watchers);
2259            })
2260        })
2261    }
2262
2263    pub fn refresh_watchers(&mut self, frame_id: u64, cx: &mut Context<Self>) {
2264        let watches = self.watchers.clone();
2265        for (_, watch) in watches.into_iter() {
2266            self.add_watcher(watch.expression.clone(), frame_id, cx)
2267                .detach();
2268        }
2269    }
2270
2271    pub fn remove_watcher(&mut self, expression: SharedString) {
2272        self.watchers.remove(&expression);
2273    }
2274
2275    pub fn variables(
2276        &mut self,
2277        variables_reference: VariableReference,
2278        cx: &mut Context<Self>,
2279    ) -> Vec<dap::Variable> {
2280        let command = VariablesCommand {
2281            variables_reference,
2282            filter: None,
2283            start: None,
2284            count: None,
2285            format: None,
2286        };
2287
2288        self.fetch(
2289            command,
2290            move |this, variables, cx| {
2291                let Some(variables) = variables.log_err() else {
2292                    return;
2293                };
2294
2295                this.variables.insert(variables_reference, variables);
2296
2297                cx.emit(SessionEvent::Variables);
2298                cx.emit(SessionEvent::InvalidateInlineValue);
2299            },
2300            cx,
2301        );
2302
2303        self.variables
2304            .get(&variables_reference)
2305            .cloned()
2306            .unwrap_or_default()
2307    }
2308
2309    pub fn set_variable_value(
2310        &mut self,
2311        stack_frame_id: u64,
2312        variables_reference: u64,
2313        name: String,
2314        value: String,
2315        cx: &mut Context<Self>,
2316    ) {
2317        if self.capabilities.supports_set_variable.unwrap_or_default() {
2318            self.request(
2319                SetVariableValueCommand {
2320                    name,
2321                    value,
2322                    variables_reference,
2323                },
2324                move |this, response, cx| {
2325                    let response = response.log_err()?;
2326                    this.invalidate_command_type::<VariablesCommand>();
2327                    this.refresh_watchers(stack_frame_id, cx);
2328                    cx.emit(SessionEvent::Variables);
2329                    Some(response)
2330                },
2331                cx,
2332            )
2333            .detach();
2334        }
2335    }
2336
2337    pub fn evaluate(
2338        &mut self,
2339        expression: String,
2340        context: Option<EvaluateArgumentsContext>,
2341        frame_id: Option<u64>,
2342        source: Option<Source>,
2343        cx: &mut Context<Self>,
2344    ) -> Task<()> {
2345        let event = dap::OutputEvent {
2346            category: None,
2347            output: format!("> {expression}"),
2348            group: None,
2349            variables_reference: None,
2350            source: None,
2351            line: None,
2352            column: None,
2353            data: None,
2354            location_reference: None,
2355        };
2356        self.push_output(event);
2357        let request = self.mode.request_dap(EvaluateCommand {
2358            expression,
2359            context,
2360            frame_id,
2361            source,
2362        });
2363        cx.spawn(async move |this, cx| {
2364            let response = request.await;
2365            this.update(cx, |this, cx| {
2366                match response {
2367                    Ok(response) => {
2368                        let event = dap::OutputEvent {
2369                            category: None,
2370                            output: format!("< {}", &response.result),
2371                            group: None,
2372                            variables_reference: Some(response.variables_reference),
2373                            source: None,
2374                            line: None,
2375                            column: None,
2376                            data: None,
2377                            location_reference: None,
2378                        };
2379                        this.push_output(event);
2380                    }
2381                    Err(e) => {
2382                        let event = dap::OutputEvent {
2383                            category: None,
2384                            output: format!("{}", e),
2385                            group: None,
2386                            variables_reference: None,
2387                            source: None,
2388                            line: None,
2389                            column: None,
2390                            data: None,
2391                            location_reference: None,
2392                        };
2393                        this.push_output(event);
2394                    }
2395                };
2396                cx.notify();
2397            })
2398            .ok();
2399        })
2400    }
2401
2402    pub fn location(
2403        &mut self,
2404        reference: u64,
2405        cx: &mut Context<Self>,
2406    ) -> Option<dap::LocationsResponse> {
2407        self.fetch(
2408            LocationsCommand { reference },
2409            move |this, response, _| {
2410                let Some(response) = response.log_err() else {
2411                    return;
2412                };
2413                this.locations.insert(reference, response);
2414            },
2415            cx,
2416        );
2417        self.locations.get(&reference).cloned()
2418    }
2419
2420    pub fn is_attached(&self) -> bool {
2421        let Mode::Running(local_mode) = &self.mode else {
2422            return false;
2423        };
2424        local_mode.binary.request_args.request == StartDebuggingRequestArgumentsRequest::Attach
2425    }
2426
2427    pub fn disconnect_client(&mut self, cx: &mut Context<Self>) {
2428        let command = DisconnectCommand {
2429            restart: Some(false),
2430            terminate_debuggee: Some(false),
2431            suspend_debuggee: Some(false),
2432        };
2433
2434        self.request(command, Self::empty_response, cx).detach()
2435    }
2436
2437    pub fn terminate_threads(&mut self, thread_ids: Option<Vec<ThreadId>>, cx: &mut Context<Self>) {
2438        if self
2439            .capabilities
2440            .supports_terminate_threads_request
2441            .unwrap_or_default()
2442        {
2443            self.request(
2444                TerminateThreadsCommand {
2445                    thread_ids: thread_ids.map(|ids| ids.into_iter().map(|id| id.0).collect()),
2446                },
2447                Self::clear_active_debug_line_response,
2448                cx,
2449            )
2450            .detach();
2451        } else {
2452            self.shutdown(cx).detach();
2453        }
2454    }
2455
2456    pub fn thread_state(&self, thread_id: ThreadId) -> Option<ThreadStatus> {
2457        self.thread_states.thread_state(thread_id)
2458    }
2459}