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