|
1 | 1 | // Copyright (c) Microsoft Corporation.
|
2 | 2 | // Licensed under the MIT License.
|
| 3 | +#nullable enable |
3 | 4 |
|
4 | 5 | using System;
|
5 | 6 | using System.Collections.Generic;
|
6 | 7 | using System.Threading;
|
7 | 8 | using System.Threading.Tasks;
|
| 9 | +using System.Management.Automation; |
8 | 10 | using Microsoft.PowerShell.EditorServices.Services;
|
9 |
| -using Microsoft.PowerShell.EditorServices.Services.DebugAdapter; |
10 |
| -using Microsoft.PowerShell.EditorServices.Utility; |
11 | 11 | using OmniSharp.Extensions.DebugAdapter.Protocol.Models;
|
12 | 12 | using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
|
| 13 | +using Microsoft.PowerShell.EditorServices.Services.DebugAdapter; |
| 14 | +using System.Linq; |
| 15 | + |
| 16 | +namespace Microsoft.PowerShell.EditorServices.Handlers; |
13 | 17 |
|
14 |
| -namespace Microsoft.PowerShell.EditorServices.Handlers |
| 18 | +internal class StackTraceHandler(DebugService debugService) : IStackTraceHandler |
15 | 19 | {
|
16 |
| - internal class StackTraceHandler : IStackTraceHandler |
| 20 | + /// <summary> |
| 21 | + /// 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. |
| 22 | + /// </summary> |
| 23 | + private const int INITIAL_PAGE_SIZE = 20; |
| 24 | + |
| 25 | + public async Task<StackTraceResponse> Handle(StackTraceArguments request, CancellationToken cancellationToken) |
17 | 26 | {
|
18 |
| - private readonly DebugService _debugService; |
| 27 | + if (!debugService.IsDebuggerStopped) |
| 28 | + { |
| 29 | + throw new NotSupportedException("Stacktrace was requested while we are not stopped at a breakpoint."); |
| 30 | + } |
19 | 31 |
|
20 |
| - public StackTraceHandler(DebugService debugService) => _debugService = debugService; |
| 32 | + // 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... |
| 33 | + int skip = Convert.ToInt32(request.StartFrame ?? 0); |
| 34 | + int take = Convert.ToInt32(request.Levels ?? 0); |
21 | 35 |
|
22 |
| - public async Task<StackTraceResponse> Handle(StackTraceArguments request, CancellationToken cancellationToken) |
23 |
| - { |
24 |
| - StackFrameDetails[] stackFrameDetails = await _debugService.GetStackFramesAsync(cancellationToken).ConfigureAwait(false); |
| 36 | + // We generate a label for the breakpoint and can return that immediately if the client is supporting DelayedStackTraceLoading. |
| 37 | + InvocationInfo invocationInfo = debugService.CurrentDebuggerStoppedEventArgs?.OriginalEvent?.InvocationInfo |
| 38 | + ?? throw new NotSupportedException("InvocationInfo was not available on CurrentDebuggerStoppedEvent args. This is a bug."); |
25 | 39 |
|
26 |
| - // Handle a rare race condition where the adapter requests stack frames before they've |
27 |
| - // begun building. |
28 |
| - if (stackFrameDetails is null) |
29 |
| - { |
30 |
| - return new StackTraceResponse |
31 |
| - { |
32 |
| - StackFrames = Array.Empty<StackFrame>(), |
33 |
| - TotalFrames = 0 |
34 |
| - }; |
35 |
| - } |
36 |
| - |
37 |
| - List<StackFrame> newStackFrames = new(); |
38 |
| - |
39 |
| - long startFrameIndex = request.StartFrame ?? 0; |
40 |
| - long maxFrameCount = stackFrameDetails.Length; |
41 |
| - |
42 |
| - // If the number of requested levels == 0 (or null), that means get all stack frames |
43 |
| - // after the specified startFrame index. Otherwise get all the stack frames. |
44 |
| - long requestedFrameCount = request.Levels ?? 0; |
45 |
| - if (requestedFrameCount > 0) |
46 |
| - { |
47 |
| - maxFrameCount = Math.Min(maxFrameCount, startFrameIndex + requestedFrameCount); |
48 |
| - } |
| 40 | + StackFrame breakpointLabel = CreateBreakpointLabel(invocationInfo); |
49 | 41 |
|
50 |
| - for (long i = startFrameIndex; i < maxFrameCount; i++) |
| 42 | + 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. |
| 43 | + { |
| 44 | + return new StackTraceResponse() |
51 | 45 | {
|
52 |
| - // Create the new StackFrame object with an ID that can |
53 |
| - // be referenced back to the current list of stack frames |
54 |
| - newStackFrames.Add(LspDebugUtils.CreateStackFrame(stackFrameDetails[i], id: i)); |
55 |
| - } |
| 46 | + StackFrames = new StackFrame[] { breakpointLabel }, |
| 47 | + TotalFrames = INITIAL_PAGE_SIZE //Indicate to the client that there are more frames available |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + // Wait until the stack frames and variables have been fetched. |
| 52 | + await debugService.StackFramesAndVariablesFetched.ConfigureAwait(false); |
| 53 | + |
| 54 | + StackFrameDetails[] stackFrameDetails = await debugService.GetStackFramesAsync(cancellationToken) |
| 55 | + .ConfigureAwait(false); |
56 | 56 |
|
| 57 | + // Handle a rare race condition where the adapter requests stack frames before they've |
| 58 | + // begun building. |
| 59 | + if (stackFrameDetails is null) |
| 60 | + { |
57 | 61 | return new StackTraceResponse
|
58 | 62 | {
|
59 |
| - StackFrames = newStackFrames, |
60 |
| - TotalFrames = newStackFrames.Count |
| 63 | + StackFrames = Array.Empty<StackFrame>(), |
| 64 | + TotalFrames = 0 |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + List<StackFrame> newStackFrames = new(); |
| 69 | + if (skip == 0) |
| 70 | + { |
| 71 | + newStackFrames.Add(breakpointLabel); |
| 72 | + } |
| 73 | + |
| 74 | + newStackFrames.AddRange( |
| 75 | + stackFrameDetails |
| 76 | + .Skip(skip != 0 ? skip - 1 : skip) |
| 77 | + .Take(take != 0 ? take - 1 : take) |
| 78 | + .Select((frame, index) => CreateStackFrame(frame, index + 1)) |
| 79 | + ); |
| 80 | + |
| 81 | + return new StackTraceResponse |
| 82 | + { |
| 83 | + StackFrames = newStackFrames, |
| 84 | + TotalFrames = newStackFrames.Count |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + public static StackFrame CreateStackFrame(StackFrameDetails stackFrame, long id) |
| 89 | + { |
| 90 | + SourcePresentationHint sourcePresentationHint = |
| 91 | + stackFrame.IsExternalCode ? SourcePresentationHint.Deemphasize : SourcePresentationHint.Normal; |
| 92 | + |
| 93 | + // When debugging an interactive session, the ScriptPath is <No File> which is not a valid source file. |
| 94 | + // We need to make sure the user can't open the file associated with this stack frame. |
| 95 | + // It will generate a VSCode error in this case. |
| 96 | + Source? source = null; |
| 97 | + if (!stackFrame.ScriptPath.Contains("<")) |
| 98 | + { |
| 99 | + source = new Source |
| 100 | + { |
| 101 | + Path = stackFrame.ScriptPath, |
| 102 | + PresentationHint = sourcePresentationHint |
61 | 103 | };
|
62 | 104 | }
|
| 105 | + |
| 106 | + return new StackFrame |
| 107 | + { |
| 108 | + Id = id, |
| 109 | + Name = (source is not null) ? stackFrame.FunctionName : "Interactive Session", |
| 110 | + Line = (source is not null) ? stackFrame.StartLineNumber : 0, |
| 111 | + EndLine = stackFrame.EndLineNumber, |
| 112 | + Column = (source is not null) ? stackFrame.StartColumnNumber : 0, |
| 113 | + EndColumn = stackFrame.EndColumnNumber, |
| 114 | + Source = source |
| 115 | + }; |
63 | 116 | }
|
| 117 | + |
| 118 | + public static StackFrame CreateBreakpointLabel(InvocationInfo invocationInfo, int id = 0) => new() |
| 119 | + { |
| 120 | + Name = "<Breakpoint>", |
| 121 | + Id = id, |
| 122 | + Source = new() |
| 123 | + { |
| 124 | + Path = invocationInfo.ScriptName |
| 125 | + }, |
| 126 | + Line = invocationInfo.ScriptLineNumber, |
| 127 | + Column = invocationInfo.OffsetInLine, |
| 128 | + PresentationHint = StackFramePresentationHint.Label |
| 129 | + }; |
| 130 | + |
64 | 131 | }
|
| 132 | + |
0 commit comments