From 5ec1fa07b526791fe19ee22b1fdda3e974224b93 Mon Sep 17 00:00:00 2001 From: commandblock2 Date: Sun, 3 Mar 2024 01:59:21 +0800 Subject: [PATCH] added thread filtering and handle a edge case --- core/context/providers/LocalsProvider.ts | 7 +- extensions/vscode/src/activation/activate.ts | 13 +-- extensions/vscode/src/continueIdeClient.ts | 116 ++++++++++++------- extensions/vscode/src/debug/debug.ts | 53 +++++++++ 4 files changed, 132 insertions(+), 57 deletions(-) create mode 100644 extensions/vscode/src/debug/debug.ts diff --git a/core/context/providers/LocalsProvider.ts b/core/context/providers/LocalsProvider.ts index c01ebbd33..c385ecc93 100644 --- a/core/context/providers/LocalsProvider.ts +++ b/core/context/providers/LocalsProvider.ts @@ -21,8 +21,10 @@ class LocalsProvider extends BaseContextProvider { ): Promise { // Assuming that the query is a number const localVariables = await extras.ide.getDebugLocals(Number(query)); + const threadIndex = Number(query); + const thread = (await extras.ide.getAvailableThreads())[threadIndex]; const callStacksSources = await extras.ide.getTopLevelCallStackSources( - Number(query), + threadIndex, this.options?.stackDepth || 3 ); const callStackContents = callStacksSources.reduce( @@ -34,6 +36,7 @@ class LocalsProvider extends BaseContextProvider { { description: "The value, name and possibly type of the local variables", content: + `This is a paused thread: ${thread.split(",")[1].trimStart()}\n` + `Current local variable contents:\n\n${localVariables}.\n` + `Current top level call stacks: \n\n${callStackContents}`, name: "Locals", @@ -49,7 +52,7 @@ class LocalsProvider extends BaseContextProvider { return threads.map((thread, threadIndex) => { const [threadId, threadName] = thread .split(",") - .map((str) => str.trimEnd()); + .map((str) => str.trimStart()); return { id: `${threadIndex}`, title: threadName, diff --git a/extensions/vscode/src/activation/activate.ts b/extensions/vscode/src/activation/activate.ts index d7b1efbc3..eab213c90 100644 --- a/extensions/vscode/src/activation/activate.ts +++ b/extensions/vscode/src/activation/activate.ts @@ -18,7 +18,7 @@ import { setupRemoteConfigSync } from "../util/remoteConfig"; import { getExtensionVersion } from "../util/util"; import { getExtensionUri } from "../util/vscode"; import { setupInlineTips } from "./inlineTips"; -import { debugPanelWebview } from "../debugPanel"; +import { registerDebugTracker } from "../debug/debug"; export let extensionContext: vscode.ExtensionContext | undefined = undefined; export let ideProtocolClient: IdeProtocolClient; @@ -132,16 +132,7 @@ export async function activateExtension(context: vscode.ExtensionContext) { ); }); - vscode.debug.registerDebugAdapterTrackerFactory("*", { - createDebugAdapterTracker(session: vscode.DebugSession) { - return { - onDidSendMessage(message: any) { - if (message.type == "event" && message.event == "breakpoint") - debugPanelWebview?.postMessage({ type: "refreshSubmenuItems" }); - }, - }; - }, - }); + registerDebugTracker(); // (async () => { // const defaultDocsPages = [ diff --git a/extensions/vscode/src/continueIdeClient.ts b/extensions/vscode/src/continueIdeClient.ts index 3685fbf81..4f75260c7 100644 --- a/extensions/vscode/src/continueIdeClient.ts +++ b/extensions/vscode/src/continueIdeClient.ts @@ -22,21 +22,14 @@ import { uriFromFilePath, } from "./util/vscode"; import { trace } from "console"; +import { threadStopped } from "./debug/debug"; + const util = require("util"); const exec = util.promisify(require("child_process").exec); const continueVirtualDocumentScheme = "continue"; class IdeProtocolClient { - async getAvailableThreads(): Promise { - const session = vscode.debug.activeDebugSession; - if (!session) return []; - - const threadsResponse = await session.customRequest("threads"); - return threadsResponse.threads.map( - (thread: { id: number; name: string }) => `${thread.id}, ${thread.name}` - ); - } private static PREVIOUS_BRANCH_FOR_WORKSPACE_DIR: { [dir: string]: string } = {}; @@ -452,6 +445,27 @@ class IdeProtocolClient { return terminalContents; } + private async _getThreads(session: vscode.DebugSession) { + const threadsResponse = await session.customRequest("threads"); + const threads = threadsResponse.threads.filter((thread: any) => + threadStopped.get(thread.id) + ); + threads.sort((a: any, b: any) => a.id - b.id); + threadsResponse.threads = threads; + + return threadsResponse; + } + + async getAvailableThreads(): Promise { + const session = vscode.debug.activeDebugSession; + if (!session) return []; + + const threadsResponse = await this._getThreads(session); + return threadsResponse.threads.map( + (thread: { id: number; name: string }) => `${thread.id}, ${thread.name}` + ); + } + async getDebugLocals(threadIndex: number = 0): Promise { const session = vscode.debug.activeDebugSession; @@ -462,11 +476,10 @@ class IdeProtocolClient { return ""; } - const variablesResponse = await session - .customRequest("threads") + const variablesResponse = await this._getThreads(session) .then((threadsResponse) => session.customRequest("stackTrace", { - threadId: threadsResponse.threads[threadIndex].threadId, + threadId: threadsResponse.threads[threadIndex].id, startFrame: 0, }) ) @@ -481,15 +494,16 @@ class IdeProtocolClient { }) ); - const filteredVariables = variablesResponse.variables.map( - (variable: any) => ({ - name: variable.name, - type: variable.type, - value: variable.value, - }) - ); + const variableContext = variablesResponse.variables + .filter((variable: any) => variable.type !== "global") + .reduce( + (acc: any, variable: any) => + `${acc}\nname: ${variable.name}, type: ${variable.type}, ` + + `value: ${variable.value}`, + "" + ); - return JSON.stringify(filteredVariables); + return variableContext; } async getTopLevelCallStackSources( @@ -497,53 +511,67 @@ class IdeProtocolClient { stackDepth: number = 3 ): Promise { const session = vscode.debug.activeDebugSession; + if (!session) return []; - const sources = await session - ?.customRequest("threads") + const sourcesPromises = await this._getThreads(session) .then((threadsResponse) => session.customRequest("stackTrace", { - threadId: threadsResponse.threads[threadIndex].threadId, + threadId: threadsResponse.threads[threadIndex].id, startFrame: 0, }) ) .then((traceResponse) => traceResponse.stackFrames .slice(0, stackDepth) - .map(async (stackFrame: any) => - this.retrieveSource( - await session.customRequest("scopes", { - frameId: stackFrame.id, - }) - ) - ) + .map(async (stackFrame: any) => { + const scopeResponse = await session.customRequest("scopes", { + frameId: stackFrame.id, + }); + + const scope = scopeResponse.scopes[0]; + + return await this.retrieveSource(scope.source ? scope : stackFrame); + }) ); - return Promise.all(sources); + return Promise.all(sourcesPromises); } - private async retrieveSource(scopesResponse: any): Promise { - const scope = scopesResponse.scopes[0]; - if (!scope.source) return ""; + private async retrieveSource(sourceContainer: any): Promise { + if (!sourceContainer.source) return ""; - const sourceRef = scope.source.sourceReference; + const sourceRef = sourceContainer.source.sourceReference; if (sourceRef && sourceRef > 0) { + // according to the spec, source might be ony available in a debug session + // not yet able to test this branch const sourceResponse = await vscode.debug.activeDebugSession?.customRequest("source", { - source: scope.source, + source: sourceContainer.source, sourceReference: sourceRef, }); - return sourceResponse.content; // TODO: test this - } else if (scope.line) { + return sourceResponse.content; + } else if (sourceContainer.line && sourceContainer.endLine) { return await this.readRangeInFile( - scope.source.path, + sourceContainer.source.path, new vscode.Range( - scope.line - 1, // The line number starts from 1 - scope.column, - scope.endLine - 1, - scope.endColumn + sourceContainer.line - 1, // The line number from scope response starts from 1 + sourceContainer.column, + sourceContainer.endLine - 1, + sourceContainer.endColumn ) ); - } else return ""; + } else if (sourceContainer.line) + // fall back to 5 line of context + return await this.readRangeInFile( + sourceContainer.source.path, + new vscode.Range( + sourceContainer.line - 3, + 0, + sourceContainer.line + 2, + 0 + ) + ); + else return "unavailable"; } private async _getRepo(forDirectory: vscode.Uri): Promise { diff --git a/extensions/vscode/src/debug/debug.ts b/extensions/vscode/src/debug/debug.ts new file mode 100644 index 000000000..252a368e1 --- /dev/null +++ b/extensions/vscode/src/debug/debug.ts @@ -0,0 +1,53 @@ +import * as vscode from "vscode"; +import { debugPanelWebview } from "../debugPanel"; + +export const threadStopped: Map = new Map(); +// Arrays has better perf but you probably won't have thousands of threads in a single debug session + +export function registerDebugTracker() { + vscode.debug.registerDebugAdapterTrackerFactory("*", { + createDebugAdapterTracker(session: vscode.DebugSession) { + return { + onDidSendMessage(message: any) { + console.log(message) + if (message.type == "event") { + switch (message.event) { + case "continued": + case "stopped": + if (typeof message.body.threadId !== "undefined") + threadStopped.set( + Number(message.body.threadId), + message.event == "stopped" + ); + + if (message.body.allThreadsStopped) + threadStopped.forEach((_, key) => + threadStopped.set(key, true) + ); + + if (message.body.allThreadsContinued) + threadStopped.forEach((_, key) => + threadStopped.set(key, false) + ); + + debugPanelWebview?.postMessage({ type: "refreshSubmenuItems" }); + break; + + case "thread": + if (message.body.reason == "exited") + threadStopped.delete(Number(message.body.threadId)); + else if (message.body.reason == "started") + threadStopped.set(Number(message.body.threadId), false); + // somehow the threadId does not respect the specification in my vscodium (debugging C++) + // expecting a number but got a string instead + break; + + default: + break; + } + } + } + }; + }, + }); +}