Skip to content

Improve debugger's variable population mechanism #1620

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

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 15 additions & 25 deletions src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal class DebugService
private List<VariableDetailsBase> variables;
private VariableContainerDetails globalScopeVariables;
private VariableContainerDetails scriptScopeVariables;
private VariableContainerDetails localScopeVariables;
private StackFrameDetails[] stackFrameDetails;
private readonly PropertyInfo invocationTypeScriptPositionProperty;

Expand Down Expand Up @@ -446,11 +447,6 @@ public async Task<string> SetVariableAsync(int variableContainerReferenceId, str
for (int i = 0; i < stackFrames.Length; i++)
{
var stackFrame = stackFrames[i];
if (stackFrame.LocalVariables.ContainsVariable(variable.Id))
{
scope = i.ToString();
break;
}
}
}

Expand Down Expand Up @@ -627,13 +623,12 @@ internal async Task<StackFrameDetails[]> GetStackFramesAsync(CancellationToken c
public VariableScope[] GetVariableScopes(int stackFrameId)
{
var stackFrames = this.GetStackFrames();
int localStackFrameVariableId = stackFrames[stackFrameId].LocalVariables.Id;
int autoVariablesId = stackFrames[stackFrameId].AutoVariables.Id;

return new VariableScope[]
{
new VariableScope(autoVariablesId, VariableContainerDetails.AutoVariablesName),
new VariableScope(localStackFrameVariableId, VariableContainerDetails.LocalScopeName),
new VariableScope(this.localScopeVariables.Id, VariableContainerDetails.LocalScopeName),
new VariableScope(this.scriptScopeVariables.Id, VariableContainerDetails.ScriptScopeName),
new VariableScope(this.globalScopeVariables.Id, VariableContainerDetails.GlobalScopeName),
};
Expand All @@ -656,27 +651,26 @@ private async Task FetchStackFramesAndVariablesAsync(string scriptNameOverride)
new VariableDetails("Dummy", null)
};

// Must retrieve global/script variales before stack frame variables
// as we check stack frame variables against globals.
await FetchGlobalAndScriptVariablesAsync().ConfigureAwait(false);

// Must retrieve in order of broadest to narrowest scope for efficient deduplication: global, script, local
this.globalScopeVariables =
await FetchVariableContainerAsync(VariableContainerDetails.GlobalScopeName, null).ConfigureAwait(false);

this.scriptScopeVariables =
await FetchVariableContainerAsync(VariableContainerDetails.ScriptScopeName, null).ConfigureAwait(false);

this.localScopeVariables =
await FetchVariableContainerAsync(VariableContainerDetails.ScriptScopeName, null).ConfigureAwait(false);

await FetchStackFramesAsync(scriptNameOverride).ConfigureAwait(false);

}
finally
{
this.debugInfoHandle.Release();
}
}

private async Task FetchGlobalAndScriptVariablesAsync()
{
// Retrieve globals first as script variable retrieval needs to search globals.
this.globalScopeVariables =
await FetchVariableContainerAsync(VariableContainerDetails.GlobalScopeName, null).ConfigureAwait(false);

this.scriptScopeVariables =
await FetchVariableContainerAsync(VariableContainerDetails.ScriptScopeName, null).ConfigureAwait(false);
}

private async Task<VariableContainerDetails> FetchVariableContainerAsync(
string scope,
VariableContainerDetails autoVariables)
Expand Down Expand Up @@ -849,9 +843,6 @@ private async Task FetchStackFramesAsync(string scriptNameOverride)

variables.Add(autoVariables);

var localVariables = new VariableContainerDetails(nextVariableId++, callStackFrame.ToString());
variables.Add(localVariables);

foreach (DictionaryEntry entry in callStackVariables)
{
// TODO: This should be deduplicated into a new function for the other variable handling as well
Expand All @@ -860,15 +851,14 @@ private async Task FetchStackFramesAsync(string scriptNameOverride)
: (entry.Value as PSVariable).Value;
var variableDetails = new VariableDetails(entry.Key.ToString(), psVarValue) { Id = nextVariableId++ };
variables.Add(variableDetails);
localVariables.Children.Add(variableDetails.Name, variableDetails);

if (AddToAutoVariables(new PSObject(entry.Value), scope: null))
{
autoVariables.Children.Add(variableDetails.Name, variableDetails);
}
}

var stackFrameDetailsEntry = StackFrameDetails.Create(callStackFrame, autoVariables, localVariables, workspaceRootPath: _psesHost.InitialWorkingDirectory);
var stackFrameDetailsEntry = StackFrameDetails.Create(callStackFrame, autoVariables);

string stackFrameScriptPath = stackFrameDetailsEntry.ScriptPath;
if (scriptNameOverride is not null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ internal class StackFrameDetails
/// </summary>
public VariableContainerDetails AutoVariables { get; private set; }

/// <summary>
/// Gets or sets the VariableContainerDetails that contains the local variables.
/// </summary>
public VariableContainerDetails LocalVariables { get; private set; }

#endregion

#region Constructors
Expand All @@ -93,14 +88,9 @@ internal class StackFrameDetails
/// <returns>A new instance of the StackFrameDetails class.</returns>
static internal StackFrameDetails Create(
PSObject callStackFrameObject,
VariableContainerDetails autoVariables,
VariableContainerDetails localVariables,
string workspaceRootPath = null)
VariableContainerDetails autoVariables)
{
string moduleId = string.Empty;
var isExternal = false;

var invocationInfo = callStackFrameObject.Properties["InvocationInfo"]?.Value as InvocationInfo;
string scriptPath = (callStackFrameObject.Properties["ScriptName"].Value as string) ?? NoFileScriptPath;
int startLineNumber = (int)(callStackFrameObject.Properties["ScriptLineNumber"].Value ?? 0);

Expand All @@ -122,7 +112,6 @@ static internal StackFrameDetails Create(
StartColumnNumber = 0, // Column number isn't given in PowerShell stack frames
EndColumnNumber = 0,
AutoVariables = autoVariables,
LocalVariables = localVariables,
IsExternalCode = isExternal
};
}
Expand Down