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 @@ -100,6 +100,21 @@ public Guid InstanceId
set;
}

/// <summary>
/// The optional breakpoint objects to use for debugging.
/// </summary>
[Experimental("Microsoft.PowerShell.Utility.PSDebugRunspaceWithBreakpoints", ExperimentAction.Show)]
[Parameter(Position = 1,
ParameterSetName = DebugRunspaceCommand.InstanceIdParameterSet)]
[Parameter(ParameterSetName = DebugRunspaceCommand.RunspaceParameterSet)]
[Parameter(ParameterSetName = DebugRunspaceCommand.IdParameterSet)]
[Parameter(ParameterSetName = DebugRunspaceCommand.NameParameterSet)]
public Breakpoint[] Breakpoint
{
get;
set;
}

#endregion

#region Overrides
Expand Down Expand Up @@ -260,7 +275,7 @@ private void WaitAndReceiveRunspaceOutput()
_debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);

// Set up host script debugger to debug the runspace.
_debugger.DebugRunspace(_runspace);
_debugger.DebugRunspace(_runspace, disableBreakAll: Breakpoint?.Length > 0);

while (_debugging)
{
Expand Down Expand Up @@ -517,6 +532,10 @@ private void PrepareRunspace(Runspace runspace)
{
SetLocalMode(runspace.Debugger, true);
EnableHostDebugger(runspace, false);
if (Breakpoint?.Length > 0)
{
runspace.Debugger?.SetBreakpoints(Breakpoint);
}
}

private void RestoreRunspace(Runspace runspace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,22 @@ public SwitchParameter BreakAll
set;
}

/// <summary>
/// The optional breakpoint objects to use for debugging.
/// </summary>
[Experimental("Microsoft.PowerShell.Utility.PSDebugRunspaceWithBreakpoints", ExperimentAction.Show)]
[Parameter(Position = 1,
ParameterSetName = CommonRunspaceCommandBase.RunspaceParameterSet)]
[Parameter(Position = 1,
ParameterSetName = CommonRunspaceCommandBase.RunspaceNameParameterSet)]
[Parameter(Position = 1,
ParameterSetName = CommonRunspaceCommandBase.RunspaceIdParameterSet)]
public Breakpoint[] Breakpoint
{
get;
set;
}

#endregion

#region Overrides
Expand All @@ -362,58 +378,61 @@ protected override void ProcessRecord()
if (this.ParameterSetName.Equals(CommonRunspaceCommandBase.ProcessNameParameterSet))
{
SetDebugPreferenceHelper(ProcessName, AppDomainName, true, "EnableRunspaceDebugCommandPersistDebugPreferenceFailure");
return;
}
else
{
IReadOnlyList<Runspace> results = GetRunspaces();

foreach (var runspace in results)
IReadOnlyList<Runspace> results = GetRunspaces();

foreach (var runspace in results)
{
if (runspace.RunspaceStateInfo.State != RunspaceState.Opened)
{
if (runspace.RunspaceStateInfo.State != RunspaceState.Opened)
{
WriteError(
new ErrorRecord(new PSInvalidOperationException(string.Format(CultureInfo.InvariantCulture, Debugger.RunspaceOptionInvalidRunspaceState, runspace.Name)),
"SetRunspaceDebugOptionCommandInvalidRunspaceState",
ErrorCategory.InvalidOperation,
this)
);
WriteError(
new ErrorRecord(new PSInvalidOperationException(string.Format(CultureInfo.InvariantCulture, Debugger.RunspaceOptionInvalidRunspaceState, runspace.Name)),
"SetRunspaceDebugOptionCommandInvalidRunspaceState",
ErrorCategory.InvalidOperation,
this));

continue;
}
continue;
}

System.Management.Automation.Debugger debugger = GetDebuggerFromRunspace(runspace);
if (debugger == null)
{
continue;
}
System.Management.Automation.Debugger debugger = GetDebuggerFromRunspace(runspace);
if (debugger == null)
{
continue;
}

// Enable debugging by preserving debug stop events.
debugger.UnhandledBreakpointMode = UnhandledBreakpointProcessingMode.Wait;
// Enable debugging by preserving debug stop events.
debugger.UnhandledBreakpointMode = UnhandledBreakpointProcessingMode.Wait;

if (this.MyInvocation.BoundParameters.ContainsKey(nameof(BreakAll)))
if (this.MyInvocation.BoundParameters.ContainsKey(nameof(BreakAll)))
{
if (BreakAll)
{
if (BreakAll)
try
{
try
{
debugger.SetDebuggerStepMode(true);
}
catch (PSInvalidOperationException e)
{
WriteError(
new ErrorRecord(
e,
"SetRunspaceDebugOptionCommandCannotEnableDebuggerStepping",
ErrorCategory.InvalidOperation,
this)
);
}
debugger.SetDebuggerStepMode(true);
}
else
catch (PSInvalidOperationException e)
{
debugger.SetDebuggerStepMode(false);
WriteError(
new ErrorRecord(
e,
"SetRunspaceDebugOptionCommandCannotEnableDebuggerStepping",
ErrorCategory.InvalidOperation,
this));
}
}
else
{
debugger.SetDebuggerStepMode(false);
}
}

// If any breakpoints were provided, set those in the debugger.
if (Breakpoint?.Length > 0)
{
debugger.SetBreakpoints(Breakpoint);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Internal;

namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// This class implements New-PSBreakpoint command.
/// </summary>
[Experimental("Microsoft.PowerShell.Utility.PSDebugRunspaceWithBreakpoints", ExperimentAction.Show)]
[Cmdlet(VerbsCommon.New, "PSBreakpoint", DefaultParameterSetName = LineParameterSetName, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113449")]
[OutputType(typeof(VariableBreakpoint), typeof(CommandBreakpoint), typeof(LineBreakpoint))]
public class NewPSBreakpointCommand : PSBreakpointCreationBase
{
/// <summary>
/// Create a new breakpoint.
/// </summary>
protected override void ProcessRecord()
{
// If there is a script, resolve its path
Collection<string> scripts = ResolveScriptPaths();

// If it is a command breakpoint...
if (ParameterSetName.Equals(CommandParameterSetName, StringComparison.OrdinalIgnoreCase))
{
for (int i = 0; i < Command.Length; i++)
{
if (scripts.Count > 0)
{
foreach (string path in scripts)
{
WildcardPattern pattern = WildcardPattern.Get(Command[i], WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
WriteObject(new CommandBreakpoint(path, pattern, Command[i], Action));
}
}
else
{
WildcardPattern pattern = WildcardPattern.Get(Command[i], WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
WriteObject(new CommandBreakpoint(null, pattern, Command[i], Action));
}
}
}
else if (ParameterSetName.Equals(VariableParameterSetName, StringComparison.OrdinalIgnoreCase))
{
// If it is a variable breakpoint...
for (int i = 0; i < Variable.Length; i++)
{
if (scripts.Count > 0)
{
foreach (string path in scripts)
{
WriteObject(new VariableBreakpoint(path, Variable[i], Mode, Action));
}
}
else
{
WriteObject(new VariableBreakpoint(null, Variable[i], Mode, Action));
}
}
}
else
{
// Else it is the default parameter set (Line breakpoint)...
Debug.Assert(ParameterSetName.Equals(LineParameterSetName, StringComparison.OrdinalIgnoreCase));

for (int i = 0; i < Line.Length; i++)
{
if (Line[i] < 1)
{
WriteError(
new ErrorRecord(
new ArgumentException(Debugger.LineLessThanOne),
"NewPSBreakpoint:LineLessThanOne",
ErrorCategory.InvalidArgument,
null));

continue;
}

foreach (string path in scripts)
{
if (Column != 0)
{
WriteObject(new LineBreakpoint(path, Line[i], Column, Action));
}
else
{
WriteObject(new LineBreakpoint(path, Line[i], Action));
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Internal;

namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Base class for Set/New-PSBreakpoint.
/// </summary>
public class PSBreakpointCreationBase : PSCmdlet
{
internal const string CommandParameterSetName = "Command";
internal const string LineParameterSetName = "Line";
internal const string VariableParameterSetName = "Variable";

#region parameters

/// <summary>
/// The action to take when hitting this breakpoint.
/// </summary>
[Parameter(ParameterSetName = CommandParameterSetName)]
[Parameter(ParameterSetName = LineParameterSetName)]
[Parameter(ParameterSetName = VariableParameterSetName)]
public ScriptBlock Action { get; set; }

/// <summary>
/// The column to set the breakpoint on.
/// </summary>
[Parameter(Position = 2, ParameterSetName = LineParameterSetName)]
[ValidateRange(1, int.MaxValue)]
public int Column { get; set; }

/// <summary>
/// The command(s) to set the breakpoint on.
/// </summary>
[Alias("C")]
[Parameter(ParameterSetName = CommandParameterSetName, Mandatory = true)]
public string[] Command { get; set; }

/// <summary>
/// The line to set the breakpoint on.
/// </summary>
[Parameter(Position = 1, ParameterSetName = LineParameterSetName, Mandatory = true)]
public int[] Line { get; set; }

/// <summary>
/// The script to set the breakpoint on.
/// </summary>
[Parameter(ParameterSetName = CommandParameterSetName, Position = 0)]
[Parameter(ParameterSetName = LineParameterSetName, Mandatory = true, Position = 0)]
[Parameter(ParameterSetName = VariableParameterSetName, Position = 0)]
[ValidateNotNull]
public string[] Script { get; set; }

/// <summary>
/// The variables to set the breakpoint(s) on.
/// </summary>
[Alias("V")]
[Parameter(ParameterSetName = VariableParameterSetName, Mandatory = true)]
public string[] Variable { get; set; }

/// <summary>
/// The access type for variable breakpoints to break on.
/// </summary>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add useful doc comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

[Parameter(ParameterSetName = VariableParameterSetName)]
public VariableAccessMode Mode { get; set; } = VariableAccessMode.Write;

#endregion parameters

internal Collection<string> ResolveScriptPaths()
{
Collection<string> scripts = new Collection<string>();

if (Script != null)
{
foreach (string script in Script)
{
Collection<PathInfo> scriptPaths = SessionState.Path.GetResolvedPSPathFromPSPath(script);

for (int i = 0; i < scriptPaths.Count; i++)
{
string providerPath = scriptPaths[i].ProviderPath;

if (!File.Exists(providerPath))
{
WriteError(
new ErrorRecord(
new ArgumentException(StringUtil.Format(Debugger.FileDoesNotExist, providerPath)),
"NewPSBreakpoint:FileDoesNotExist",
ErrorCategory.InvalidArgument,
null));

continue;
}

string extension = Path.GetExtension(providerPath);

if (!extension.Equals(".ps1", StringComparison.OrdinalIgnoreCase) && !extension.Equals(".psm1", StringComparison.OrdinalIgnoreCase))
{
WriteError(
new ErrorRecord(
new ArgumentException(StringUtil.Format(Debugger.WrongExtension, providerPath)),
"NewPSBreakpoint:WrongExtension",
ErrorCategory.InvalidArgument,
null));
continue;
}

scripts.Add(Path.GetFullPath(providerPath));
}
}
}

return scripts;
}
}
}
Loading