Skip to content
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 Debug Context Selector #1

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"debug/variables/context": [
{
"command": "memory-inspector.show-variable",
"when": "canViewMemory && memory-inspector.canRead"
"when": "canViewMemory && memory-inspector.canReadVariable"
},
{
"command": "memory-inspector.store-file",
Expand All @@ -152,7 +152,7 @@
"view/item/context": [
{
"command": "memory-inspector.show-variable",
"when": "canViewMemory && memory-inspector.canRead"
"when": "canViewMemory && memory-inspector.canReadVariable"
}
],
"explorer/context": [
Expand Down
21 changes: 15 additions & 6 deletions src/common/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ export type StoreMemoryResult = void;
export type ApplyMemoryArguments = URI | undefined;
export type ApplyMemoryResult = MemoryOptions;

export interface Context {
name: string;
id: number;
}

export interface SessionContext {
sessionId?: string;
canRead: boolean;
canWrite: boolean;
canReadVariable: boolean;
}

// Notifications
Expand All @@ -50,20 +56,23 @@ export const setMemoryViewSettingsType: NotificationType<Partial<MemoryViewSetti
export const resetMemoryViewSettingsType: NotificationType<void> = { method: 'resetMemoryViewSettings' };
export const setTitleType: NotificationType<string> = { method: 'setTitle' };
export const memoryWrittenType: NotificationType<WrittenMemory> = { method: 'memoryWritten' };
export const sessionContextChangedType: NotificationType<SessionContext> = { method: 'sessionContextChanged' };
export const sessionContextChangedType: NotificationType<[SessionContext, Context?, Context[]?]> = { method: 'sessionContextChanged' };

// Requests
export const setOptionsType: RequestType<MemoryOptions, void> = { method: 'setOptions' };
export const logMessageType: RequestType<string, void> = { method: 'logMessage' };
export const readMemoryType: RequestType<ReadMemoryArguments, ReadMemoryResult> = { method: 'readMemory' };
export const writeMemoryType: RequestType<WriteMemoryArguments, WriteMemoryResult> = { method: 'writeMemory' };
export const getVariablesType: RequestType<ReadMemoryArguments, VariableRange[]> = { method: 'getVariables' };
export const storeMemoryType: RequestType<StoreMemoryArguments, void> = { method: 'storeMemory' };
export const applyMemoryType: RequestType<ApplyMemoryArguments, ApplyMemoryResult> = { method: 'applyMemory' };
export const readMemoryType: RequestType<[ReadMemoryArguments, Context?], ReadMemoryResult> = { method: 'readMemory' };
export const writeMemoryType: RequestType<[WriteMemoryArguments, Context?], WriteMemoryResult> = { method: 'writeMemory' };
export const getVariablesType: RequestType<[ReadMemoryArguments, Context?], VariableRange[]> = { method: 'getVariables' };
export const storeMemoryType: RequestType<[StoreMemoryArguments, Context?], void> = { method: 'storeMemory' };
export const applyMemoryType: RequestType<[ApplyMemoryArguments, Context?], ApplyMemoryResult> = { method: 'applyMemory' };

export const showAdvancedOptionsType: NotificationType<void> = { method: 'showAdvancedOptions' };
export const getWebviewSelectionType: RequestType<void, WebviewSelection> = { method: 'getWebviewSelection' };

export const getContextsType: RequestType<null, Context[]> = { method: 'getContexts' };
export const getCurrentContextType: RequestType<null, Context> = { method: 'getCurrentContext' };

export interface WebviewSelection {
selectedCell?: {
column: string
Expand Down
4 changes: 4 additions & 0 deletions src/entry-points/desktop/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import * as vscode from 'vscode';
import { AdapterRegistry } from '../../plugin/adapter-registry/adapter-registry';
import { AmalgamatorGdbVariableTransformer, AmalgamatorSessionManager } from '../../plugin/adapter-registry/amalgamator-gdb-tracker';
import { CAdapter } from '../../plugin/adapter-registry/c-adapter';
import { outputChannelLogger } from '../../plugin/logger';
import { MemoryProvider } from '../../plugin/memory-provider';
import { MemoryStorage } from '../../plugin/memory-storage';
import { MemoryWebview } from '../../plugin/memory-webview-main';
Expand All @@ -27,6 +29,8 @@ export const activate = async (context: vscode.ExtensionContext): Promise<Adapte
const memoryView = new MemoryWebview(context.extensionUri, memoryProvider);
const memoryStorage = new MemoryStorage(memoryProvider);
const cAdapter = new CAdapter(registry);
const debugTypes = ['amalgamator'];
registry.registerAdapter(new AmalgamatorSessionManager(AmalgamatorGdbVariableTransformer, outputChannelLogger, ...debugTypes), ...debugTypes);

memoryProvider.activate(context);
registry.activate(context);
Expand Down
45 changes: 30 additions & 15 deletions src/plugin/adapter-registry/adapter-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@ import { DebugProtocol } from '@vscode/debugprotocol';
import * as vscode from 'vscode';
import { isDebugRequest, isDebugResponse } from '../../common/debug-requests';
import { VariableRange } from '../../common/memory-range';
import { Context, ReadMemoryArguments, ReadMemoryResult, WriteMemoryArguments, WriteMemoryResult } from '../../common/messaging';
import { Logger } from '../logger';

/** Represents capabilities that may be achieved with particular debug adapters but are not part of the DAP */
export interface AdapterCapabilities {
/** Resolve variables known to the adapter to their locations. Fallback if {@link getResidents} is not present */
getVariables?(session: vscode.DebugSession): Promise<VariableRange[]>;
getVariables?(session: vscode.DebugSession, context?: Context): Promise<VariableRange[]>;
/** Resolve symbols resident in the memory at the specified range. Will be preferred to {@link getVariables} if present. */
getResidents?(session: vscode.DebugSession, params: DebugProtocol.ReadMemoryArguments): Promise<VariableRange[]>;
getResidents?(session: vscode.DebugSession, params: DebugProtocol.ReadMemoryArguments, context?: Context): Promise<VariableRange[]>;
/** Resolves the address of a given variable in bytes with the current context. */
getAddressOfVariable?(session: vscode.DebugSession, variableName: string): Promise<string | undefined>;
getAddressOfVariable?(session: vscode.DebugSession, variableName: string, context?: Context): Promise<string | undefined>;
/** Resolves the size of a given variable in bytes within the current context. */
getSizeOfVariable?(session: vscode.DebugSession, variableName: string): Promise<bigint | undefined>;
getSizeOfVariable?(session: vscode.DebugSession, variableName: string, context?: Context): Promise<bigint | undefined>;
initializeAdapterTracker?(session: vscode.DebugSession): vscode.DebugAdapterTracker | undefined;
readMemory?(session: vscode.DebugSession, params: ReadMemoryArguments, context?: Context): Promise<ReadMemoryResult>;
writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments, context?: Context): Promise<WriteMemoryResult>;
getContexts?(session: vscode.DebugSession): Promise<Context[]>;
getCurrentContext?(session: vscode.DebugSession): Promise<Context | undefined>;
supportShowVariables?(session: vscode.DebugSession): boolean;
}

export type WithChildren<Original> = Original & { children?: Array<WithChildren<DebugProtocol.Variable>> };
Expand Down Expand Up @@ -101,14 +107,14 @@ export class AdapterVariableTracker implements vscode.DebugAdapterTracker {
this.pendingMessages.clear();
}

async getLocals(session: vscode.DebugSession): Promise<VariableRange[]> {
async getLocals(session: vscode.DebugSession, context?: Context): Promise<VariableRange[]> {
this.logger.debug('Retrieving local variables in', session.name + ' Current variables:\n', this.variablesTree);
if (this.currentFrame === undefined) { return []; }
const maybeRanges = await Promise.all(Object.values(this.variablesTree).reduce<Array<Promise<VariableRange | undefined>>>((previous, parent) => {
if (this.isDesiredVariable(parent) && parent.children?.length) {
this.logger.debug('Resolving children of', parent.name);
parent.children.forEach(child => {
previous.push(this.variableToVariableRange(child, session));
previous.push(this.variableToVariableRange(child, session, context));
});
} else {
this.logger.debug('Ignoring', parent.name);
Expand All @@ -122,15 +128,24 @@ export class AdapterVariableTracker implements vscode.DebugAdapterTracker {
return candidate.presentationHint !== 'registers' && candidate.name !== 'Registers';
}

protected variableToVariableRange(_variable: DebugProtocol.Variable, _session: vscode.DebugSession): Promise<VariableRange | undefined> {
protected variableToVariableRange(_variable: DebugProtocol.Variable, _session: vscode.DebugSession, _context?: Context): Promise<VariableRange | undefined> {
throw new Error('To be implemented by derived classes!');
}

/** Resolves the address of a given variable in bytes within the current context. */
getAddressOfVariable?(variableName: string, session: vscode.DebugSession): Promise<string | undefined>;
getAddressOfVariable?(variableName: string, session: vscode.DebugSession, context?: Context): Promise<string | undefined>;

/** Resolves the size of a given variable in bytes within the current context. */
getSizeOfVariable?(variableName: string, session: vscode.DebugSession): Promise<bigint | undefined>;
getSizeOfVariable?(variableName: string, session: vscode.DebugSession, context?: Context): Promise<bigint | undefined>;

readMemory?(session: vscode.DebugSession, params: ReadMemoryArguments, context?: Context): Promise<ReadMemoryResult>;

writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments, context?: Context): Promise<WriteMemoryResult>;

getContexts?(session: vscode.DebugSession): Promise<Context[]>;
getCurrentContext?(session: vscode.DebugSession): Promise<Context | undefined>;

supportShowVariables?(session: vscode.DebugSession): boolean;
}

export class VariableTracker implements AdapterCapabilities {
Expand All @@ -152,15 +167,15 @@ export class VariableTracker implements AdapterCapabilities {
}
}

async getVariables(session: vscode.DebugSession): Promise<VariableRange[]> {
return this.sessions.get(session.id)?.getLocals(session) ?? [];
async getVariables(session: vscode.DebugSession, context?: Context): Promise<VariableRange[]> {
return this.sessions.get(session.id)?.getLocals(session, context) ?? [];
}

async getAddressOfVariable(session: vscode.DebugSession, variableName: string): Promise<string | undefined> {
return this.sessions.get(session.id)?.getAddressOfVariable?.(variableName, session);
async getAddressOfVariable(session: vscode.DebugSession, variableName: string, context?: Context): Promise<string | undefined> {
return this.sessions.get(session.id)?.getAddressOfVariable?.(variableName, session, context);
}

async getSizeOfVariable(session: vscode.DebugSession, variableName: string): Promise<bigint | undefined> {
return this.sessions.get(session.id)?.getSizeOfVariable?.(variableName, session);
async getSizeOfVariable(session: vscode.DebugSession, variableName: string, context?: Context): Promise<bigint | undefined> {
return this.sessions.get(session.id)?.getSizeOfVariable?.(variableName, session, context);
}
}
118 changes: 118 additions & 0 deletions src/plugin/adapter-registry/amalgamator-gdb-tracker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/********************************************************************************
* Copyright (C) 2024 Ericsson, Arm and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { DebugProtocol } from '@vscode/debugprotocol';
import * as vscode from 'vscode';
import { Context, ReadMemoryArguments, ReadMemoryResult, WriteMemoryArguments, WriteMemoryResult } from '../../common/messaging';
import { AdapterCapabilities, AdapterVariableTracker, VariableTracker } from './adapter-capabilities';

// Copied from cdt-amalgamator [AmalgamatorSession.d.ts] file
/**
* Response for our custom 'cdt-amalgamator/getChildDaps' request.
*/
export interface Contexts {
children?: Context[];
}
export interface GetContextsResponse extends DebugProtocol.Response {
body: Contexts;
}
export type GetContextsResult = GetContextsResponse['body'];

export interface AmalgamatorReadArgs extends ReadMemoryArguments {
child: Context;
}

export class AmalgamatorSessionManager extends VariableTracker implements AdapterCapabilities {
async getContexts(session: vscode.DebugSession): Promise<Context[]> {
return this.sessions.get(session.id)?.getContexts?.(session) || [];
}

async readMemory(session: vscode.DebugSession, args: ReadMemoryArguments, context: Context): Promise<ReadMemoryResult> {
if (!context) {
vscode.window.showErrorMessage('Invalid context for Amalgamator. Select Context in Dropdown');
return {
address: args.memoryReference
};
}
return this.sessions.get(session.id)?.readMemory?.(session, args, context);
}

async writeMemory(session: vscode.DebugSession, args: WriteMemoryArguments, context: Context): Promise<WriteMemoryResult> {
return this.sessions.get(session.id)?.writeMemory?.(session, args, context);
}

async getCurrentContext(session: vscode.DebugSession): Promise<Context | undefined> {
return this.sessions.get(session.id)?.getCurrentContext?.(session);
}

supportShowVariables(_session: vscode.DebugSession): boolean {
return false;
}
}

export class AmalgamatorGdbVariableTransformer extends AdapterVariableTracker {
protected contexts?: Context[];
protected currentContext?: Context;

onWillReceiveMessage(message: unknown): void {
if (isStacktraceRequest(message)) {
if (typeof(message.arguments.threadId) !== 'undefined') {
this.currentContext = {
id: message.arguments.threadId,
name: message.arguments.threadId.toString()
};
} else {
this.logger.warn('Invalid ThreadID in stackTrace');
this.currentContext = undefined;
}
} else {
super.onWillReceiveMessage(message);
}
}

get frame(): number | undefined { return this.currentFrame; }

async getContexts(session: vscode.DebugSession): Promise<Context[]> {
if (!this.contexts) {
const contexts: GetContextsResult = (await session.customRequest('cdt-amalgamator/getChildDaps'));
this.contexts = contexts.children?.map(({ name, id }) => ({ name, id })) ?? [];
}
return Promise.resolve(this.contexts);
}

async getCurrentContext(_session: vscode.DebugSession): Promise<Context | undefined> {
const curContext = this.contexts?.length ?
(this.contexts?.filter(context => context.id === this.currentContext?.id).shift() ??
this.currentContext) :
this.currentContext;
return Promise.resolve(curContext);
}

readMemory(session: vscode.DebugSession, args: ReadMemoryArguments, context: Context): Promise<ReadMemoryResult> {
const amalReadArgs: AmalgamatorReadArgs = { ...args, child: context };
return Promise.resolve(session.customRequest('cdt-amalgamator/readMemory', amalReadArgs));
}
}

export function isStacktraceRequest(message: unknown): message is DebugProtocol.StackTraceRequest {
const candidate = message as DebugProtocol.StackTraceRequest;
return !!candidate && candidate.command === 'stackTrace';
}

export function isStacktraceResponse(message: unknown): message is DebugProtocol.StackTraceResponse {
const candidate = message as DebugProtocol.StackTraceResponse;
return !!candidate && candidate.command === 'stackTrace' && Array.isArray(candidate.body.stackFrames);
}
Loading