-
Notifications
You must be signed in to change notification settings - Fork 220
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
Add Breakpoint Label frame to optimize debug stepping performance #2190
Open
JustinGrote
wants to merge
3
commits into
main
Choose a base branch
from
feature/breakpointLabelFrame
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+235
−135
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 108 additions & 39 deletions
147
src/PowerShellEditorServices/Services/DebugAdapter/Handlers/StackTraceHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,133 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
#nullable enable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Management.Automation; | ||
using Microsoft.PowerShell.EditorServices.Services; | ||
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter; | ||
using Microsoft.PowerShell.EditorServices.Utility; | ||
using OmniSharp.Extensions.DebugAdapter.Protocol.Models; | ||
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests; | ||
using Microsoft.PowerShell.EditorServices.Services.DebugAdapter; | ||
using System.Linq; | ||
using OmniSharp.Extensions.JsonRpc; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Handlers; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Handlers | ||
internal class StackTraceHandler(DebugService debugService) : IStackTraceHandler | ||
{ | ||
internal class StackTraceHandler : IStackTraceHandler | ||
/// <summary> | ||
/// Because we don't know the size of the stacktrace beforehand, we will tell the client that there are more frames available, this is effectively a paging size, as the client should request this many frames after the first one. | ||
/// </summary> | ||
private const int INITIAL_PAGE_SIZE = 20; | ||
|
||
public async Task<StackTraceResponse> Handle(StackTraceArguments request, CancellationToken cancellationToken) | ||
{ | ||
private readonly DebugService _debugService; | ||
if (!debugService.IsDebuggerStopped) | ||
{ | ||
throw new RpcErrorException(0, null!, "Stacktrace was requested while we are not stopped at a breakpoint. This is a violation of the DAP protocol, and is probably a bug."); | ||
} | ||
|
||
public StackTraceHandler(DebugService debugService) => _debugService = debugService; | ||
// Adapting to int to let us use LINQ, realistically if you have a stacktrace larger than this that the client is requesting, you have bigger problems... | ||
int skip = Convert.ToInt32(request.StartFrame ?? 0); | ||
int take = Convert.ToInt32(request.Levels ?? 0); | ||
|
||
public async Task<StackTraceResponse> Handle(StackTraceArguments request, CancellationToken cancellationToken) | ||
{ | ||
StackFrameDetails[] stackFrameDetails = await _debugService.GetStackFramesAsync(cancellationToken).ConfigureAwait(false); | ||
// We generate a label for the breakpoint and can return that immediately if the client is supporting DelayedStackTraceLoading. | ||
InvocationInfo invocationInfo = debugService.CurrentDebuggerStoppedEventArgs?.OriginalEvent?.InvocationInfo | ||
?? throw new RpcErrorException(0, null!, "InvocationInfo was not available on CurrentDebuggerStoppedEvent args. This is a bug and you should report it."); | ||
|
||
// Handle a rare race condition where the adapter requests stack frames before they've | ||
// begun building. | ||
if (stackFrameDetails is null) | ||
{ | ||
return new StackTraceResponse | ||
{ | ||
StackFrames = Array.Empty<StackFrame>(), | ||
TotalFrames = 0 | ||
}; | ||
} | ||
|
||
List<StackFrame> newStackFrames = new(); | ||
|
||
long startFrameIndex = request.StartFrame ?? 0; | ||
long maxFrameCount = stackFrameDetails.Length; | ||
|
||
// If the number of requested levels == 0 (or null), that means get all stack frames | ||
// after the specified startFrame index. Otherwise get all the stack frames. | ||
long requestedFrameCount = request.Levels ?? 0; | ||
if (requestedFrameCount > 0) | ||
{ | ||
maxFrameCount = Math.Min(maxFrameCount, startFrameIndex + requestedFrameCount); | ||
} | ||
StackFrame breakpointLabel = CreateBreakpointLabel(invocationInfo); | ||
JustinGrote marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (long i = startFrameIndex; i < maxFrameCount; i++) | ||
if (skip == 0 && take == 1) // This indicates the client is doing an initial fetch, so we want to return quickly to unblock the UI and wait on the remaining stack frames for the subsequent requests. | ||
{ | ||
return new StackTraceResponse() | ||
{ | ||
// Create the new StackFrame object with an ID that can | ||
// be referenced back to the current list of stack frames | ||
newStackFrames.Add(LspDebugUtils.CreateStackFrame(stackFrameDetails[i], id: i)); | ||
} | ||
StackFrames = new StackFrame[] { breakpointLabel }, | ||
TotalFrames = INITIAL_PAGE_SIZE //Indicate to the client that there are more frames available | ||
JustinGrote marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} | ||
|
||
// Wait until the stack frames and variables have been fetched. | ||
await debugService.StackFramesAndVariablesFetched.ConfigureAwait(false); | ||
JustinGrote marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
StackFrameDetails[] stackFrameDetails = await debugService.GetStackFramesAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
// Handle a rare race condition where the adapter requests stack frames before they've | ||
// begun building. | ||
if (stackFrameDetails is null) | ||
{ | ||
return new StackTraceResponse | ||
{ | ||
StackFrames = newStackFrames, | ||
TotalFrames = newStackFrames.Count | ||
StackFrames = Array.Empty<StackFrame>(), | ||
TotalFrames = 0 | ||
}; | ||
} | ||
|
||
List<StackFrame> newStackFrames = new(); | ||
if (skip == 0) | ||
{ | ||
newStackFrames.Add(breakpointLabel); | ||
} | ||
|
||
newStackFrames.AddRange( | ||
stackFrameDetails | ||
.Skip(skip != 0 ? skip - 1 : skip) | ||
.Take(take != 0 ? take - 1 : take) | ||
.Select((frame, index) => CreateStackFrame(frame, index + 1)) | ||
); | ||
|
||
return new StackTraceResponse | ||
{ | ||
StackFrames = newStackFrames, | ||
TotalFrames = newStackFrames.Count | ||
}; | ||
} | ||
|
||
public static StackFrame CreateStackFrame(StackFrameDetails stackFrame, long id) | ||
{ | ||
SourcePresentationHint sourcePresentationHint = | ||
stackFrame.IsExternalCode ? SourcePresentationHint.Deemphasize : SourcePresentationHint.Normal; | ||
JustinGrote marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// When debugging an interactive session, the ScriptPath is <No File> which is not a valid source file. | ||
// We need to make sure the user can't open the file associated with this stack frame. | ||
// It will generate a VSCode error in this case. | ||
Source? source = null; | ||
if (!stackFrame.ScriptPath.Contains("<No File>")) | ||
{ | ||
source = new Source | ||
{ | ||
Path = stackFrame.ScriptPath, | ||
PresentationHint = sourcePresentationHint | ||
}; | ||
} | ||
|
||
return new StackFrame | ||
{ | ||
Id = id, | ||
Name = (source is not null) ? stackFrame.FunctionName : "Interactive Session", | ||
Line = (source is not null) ? stackFrame.StartLineNumber : 0, | ||
EndLine = stackFrame.EndLineNumber, | ||
Column = (source is not null) ? stackFrame.StartColumnNumber : 0, | ||
EndColumn = stackFrame.EndColumnNumber, | ||
Source = source | ||
}; | ||
} | ||
|
||
public static StackFrame CreateBreakpointLabel(InvocationInfo invocationInfo, int id = 0) => new() | ||
{ | ||
Name = "<Breakpoint>", | ||
Id = id, | ||
Source = new() | ||
{ | ||
Path = invocationInfo.ScriptName | ||
}, | ||
Line = invocationInfo.ScriptLineNumber, | ||
Column = invocationInfo.OffsetInLine, | ||
PresentationHint = StackFramePresentationHint.Label | ||
}; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could ou explain why this is either 0, or (and this is what's changed)
FrameId - 1
? That means there are two instances where we get scope 0:FrameId == 0
andFrameId == 1
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh, there was a reason, but I honestly don't remember at this point. Let me check