Skip to content

Add property and event for debug attach #25788

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ private void WaitAndReceiveRunspaceOutput()
// Set up host script debugger to debug the runspace.
_debugger.DebugRunspace(_runspace, breakAll: BreakAll);

_runspace.IsRemoteDebuggerAttached = true;
_runspace.Events?.GenerateEvent(
PSEngineEvent.OnDebugAttach,
null,
Array.Empty<object>(),
null,
true,
false);

while (_debugging)
{
// Wait for running script.
Expand Down Expand Up @@ -307,6 +316,7 @@ private void WaitAndReceiveRunspaceOutput()
{
_runspace.AvailabilityChanged -= HandleRunspaceAvailabilityChanged;
_debugger.NestedDebuggingCancelledEvent -= HandleDebuggerNestedDebuggingCancelledEvent;
_runspace.IsRemoteDebuggerAttached = false;
_debugger.StopDebugRunspace(_runspace);
_newRunningScriptEvent.Dispose();
}
Expand Down
7 changes: 6 additions & 1 deletion src/System.Management.Automation/engine/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,11 @@ private PSEngineEvent() { }
/// </summary>
public const string OnIdle = "PowerShell.OnIdle";

/// <summary>
/// Called when Debug-Runspace has attached a debugger to the current runspace.
/// </summary>
public const string OnDebugAttach = "PowerShell.OnDebugAttach";

/// <summary>
/// Called during scriptblock invocation.
/// </summary>
Expand All @@ -1843,7 +1848,7 @@ private PSEngineEvent() { }
/// <summary>
/// A HashSet that contains all engine event names.
/// </summary>
internal static readonly HashSet<string> EngineEvents = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { Exiting, OnIdle, OnScriptBlockInvoke };
internal static readonly HashSet<string> EngineEvents = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { Exiting, OnIdle, OnDebugAttach, OnScriptBlockInvoke };
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,12 @@ public string Name
/// Gets the Runspace Id.
/// </summary>
public int Id { get; }

/// <summary>
/// Gets and sets a boolean indicating whether the runspace has a
/// debugger attached with <c>Debug-Runspace</c>.
/// </summary>
public bool IsRemoteDebuggerAttached { get; internal set; }

/// <summary>
/// Returns protocol version that the remote server uses for PS remoting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ Describe "Debug-Runspace" -Tag "CI" {
$rs1.Debugger.SetDebugMode("None")
{ Debug-Runspace -Runspace $rs1 -ErrorAction stop } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.DebugRunspaceCommand"
}

It "Should write attach event and mark runspace as having a remote debugger attached" {
$onAttachName = [System.Management.Automation.PSEngineEvent]::OnDebugAttach

$debugTarget = [PowerShell]::Create()
$null = $debugTarget.AddCommand('Wait-Event').AddParameter('SourceIdentifier', $onAttachName)
$waitTask = $debugTarget.BeginInvoke()

$debugTarget.Runspace.IsRemoteDebuggerAttached | Should -BeFalse

$debugger = [PowerShell]::Create()
$null = $debugger.AddCommand('Debug-Runspace').AddParameter('Id', $debugTarget.Runspace.Id)
$debugTask = $debugger.BeginInvoke()

$waitTask.AsyncWaitHandle.WaitOne(5000) | Should -BeTrue
$waitInfo = $debugTarget.EndInvoke($waitTask)
$waitInfo.SourceIdentifier | Should -Be $onAttachName

$debugTarget.Runspace.IsRemoteDebuggerAttached | Should -BeTrue

$debugger.Stop()
$exp = {
$debugger.EndInvoke($debugTask)
} | Should -Throw -PassThru
$exp.FullyQualifiedErrorId | Should -Be "PipelineStoppedException"

$debugTarget.Runspace.IsRemoteDebuggerAttached | Should -BeFalse
}
}

Loading