Skip to content
Merged
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,
sender: null,
args: Array.Empty<object>(),
extraData: null,
processInCurrentThread: true,
waitForCompletionInCurrentThread: 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 <c>Debug-Runspace</c> 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