Windows-powershell / PowerShell-master /src /Microsoft.PowerShell.Commands.Utility /commands /utility /Get-PSBreakpoint.cs
| // Copyright (c) Microsoft Corporation. | |
| // Licensed under the MIT License. | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Diagnostics.CodeAnalysis; | |
| using System.Management.Automation; | |
| namespace Microsoft.PowerShell.Commands | |
| { | |
| /// <summary> | |
| /// Types of breakpoints. | |
| /// </summary> | |
| public enum BreakpointType | |
| { | |
| /// <summary>Breakpoint on a line within a script</summary> | |
| Line, | |
| /// <summary>Breakpoint on a variable</summary> | |
| Variable, | |
| /// <summary>Breakpoint on a command</summary> | |
| Command | |
| } | |
| /// <summary> | |
| /// This class implements Get-PSBreakpoint. | |
| /// </summary> | |
| [] | |
| [] { CommandParameterSetName })] | |
| [] { LineParameterSetName })] | |
| [] { VariableParameterSetName })] | |
| [] { TypeParameterSetName, IdParameterSetName })] | |
| public class GetPSBreakpointCommand : PSBreakpointAccessorCommandBase | |
| { | |
| internal const string TypeParameterSetName = "Type"; | |
| internal const string IdParameterSetName = "Id"; | |
| /// <summary> | |
| /// Scripts of the breakpoints to output. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| public string[] Script { get; set; } | |
| /// <summary> | |
| /// IDs of the breakpoints to output. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| public int[] Id { get; set; } | |
| /// <summary> | |
| /// Variables of the breakpoints to output. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| public string[] Variable { get; set; } | |
| /// <summary> | |
| /// Commands of the breakpoints to output. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| public string[] Command { get; set; } | |
| /// <summary> | |
| /// Commands of the breakpoints to output. | |
| /// </summary> | |
| [] | |
| [] | |
| [] | |
| [] | |
| public BreakpointType[] Type { get; set; } | |
| /// <summary> | |
| /// Remove breakpoints. | |
| /// </summary> | |
| protected override void ProcessRecord() | |
| { | |
| List<Breakpoint> breakpoints = Runspace.Debugger.GetBreakpoints(); | |
| // Filter by parameter set | |
| if (ParameterSetName.Equals(LineParameterSetName, StringComparison.OrdinalIgnoreCase)) | |
| { | |
| // no filter | |
| } | |
| else if (ParameterSetName.Equals(IdParameterSetName, StringComparison.OrdinalIgnoreCase)) | |
| { | |
| breakpoints = Filter( | |
| breakpoints, | |
| Id, | |
| static (Breakpoint breakpoint, int id) => breakpoint.Id == id); | |
| } | |
| else if (ParameterSetName.Equals(CommandParameterSetName, StringComparison.OrdinalIgnoreCase)) | |
| { | |
| breakpoints = Filter( | |
| breakpoints, | |
| Command, | |
| (Breakpoint breakpoint, string command) => | |
| { | |
| if (breakpoint is not CommandBreakpoint commandBreakpoint) | |
| { | |
| return false; | |
| } | |
| return commandBreakpoint.Command.Equals(command, StringComparison.OrdinalIgnoreCase); | |
| }); | |
| } | |
| else if (ParameterSetName.Equals(VariableParameterSetName, StringComparison.OrdinalIgnoreCase)) | |
| { | |
| breakpoints = Filter( | |
| breakpoints, | |
| Variable, | |
| (Breakpoint breakpoint, string variable) => | |
| { | |
| if (breakpoint is not VariableBreakpoint variableBreakpoint) | |
| { | |
| return false; | |
| } | |
| return variableBreakpoint.Variable.Equals(variable, StringComparison.OrdinalIgnoreCase); | |
| }); | |
| } | |
| else if (ParameterSetName.Equals(TypeParameterSetName, StringComparison.OrdinalIgnoreCase)) | |
| { | |
| breakpoints = Filter( | |
| breakpoints, | |
| Type, | |
| (Breakpoint breakpoint, BreakpointType type) => | |
| { | |
| switch (type) | |
| { | |
| case BreakpointType.Line: | |
| if (breakpoint is LineBreakpoint) | |
| { | |
| return true; | |
| } | |
| break; | |
| case BreakpointType.Command: | |
| if (breakpoint is CommandBreakpoint) | |
| { | |
| return true; | |
| } | |
| break; | |
| case BreakpointType.Variable: | |
| if (breakpoint is VariableBreakpoint) | |
| { | |
| return true; | |
| } | |
| break; | |
| } | |
| return false; | |
| }); | |
| } | |
| else | |
| { | |
| Diagnostics.Assert(false, "Invalid parameter set: {0}", this.ParameterSetName); | |
| } | |
| // Filter by script | |
| if (Script != null) | |
| { | |
| breakpoints = Filter( | |
| breakpoints, | |
| Script, | |
| (Breakpoint breakpoint, string script) => | |
| { | |
| if (breakpoint.Script == null) | |
| { | |
| return false; | |
| } | |
| return string.Equals( | |
| SessionState.Path.GetUnresolvedProviderPathFromPSPath(breakpoint.Script), | |
| SessionState.Path.GetUnresolvedProviderPathFromPSPath(script), | |
| StringComparison.OrdinalIgnoreCase | |
| ); | |
| }); | |
| } | |
| // Output results | |
| foreach (Breakpoint b in breakpoints) | |
| { | |
| ProcessBreakpoint(b); | |
| } | |
| } | |
| /// <summary> | |
| /// Gives the criteria to filter breakpoints. | |
| /// </summary> | |
| private delegate bool FilterSelector<T>(Breakpoint breakpoint, T target); | |
| /// <summary> | |
| /// Returns the items in the input list that match an item in the filter array according to | |
| /// the given selection criterion. | |
| /// </summary> | |
| private static List<Breakpoint> Filter<T>(List<Breakpoint> input, T[] filter, FilterSelector<T> selector) | |
| { | |
| List<Breakpoint> output = new(); | |
| for (int i = 0; i < input.Count; i++) | |
| { | |
| for (int j = 0; j < filter.Length; j++) | |
| { | |
| if (selector(input[i], filter[j])) | |
| { | |
| output.Add(input[i]); | |
| break; | |
| } | |
| } | |
| } | |
| return output; | |
| } | |
| } | |
| } | |