Skip to content

Commit

Permalink
Adding Context for cdt-amalgamator and other DAPs
Browse files Browse the repository at this point in the history
Populate the Context Dropdown from the different debug Contexts
if available so different DAPs can be addressed.
Add an optional Context to queries so the query can be directed
to the appropriate handler.
Add support for cdt-amalgamator.

Signed-off-by: Thor Thayer <[email protected]>
  • Loading branch information
WyoTwT committed May 8, 2024
1 parent f3e1977 commit b24793c
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 73 deletions.
20 changes: 14 additions & 6 deletions src/common/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ 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;
Expand All @@ -50,20 +55,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: 25 additions & 20 deletions src/plugin/adapter-registry/adapter-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ import { DebugProtocol } from '@vscode/debugprotocol';
import * as vscode from 'vscode';
import { isDebugRequest, isDebugResponse } from '../../common/debug-requests';
import { VariableRange } from '../../common/memory-range';
import { ReadMemoryArguments, ReadMemoryResult, WriteMemoryArguments, WriteMemoryResult } from '../../common/messaging';
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): Promise<ReadMemoryResult>;
writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments): Promise<WriteMemoryResult>;
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>;
}

export type WithChildren<Original> = Original & { children?: Array<WithChildren<DebugProtocol.Variable>> };
Expand Down Expand Up @@ -104,14 +106,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 @@ -125,19 +127,22 @@ 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): Promise<ReadMemoryResult>;
readMemory?(session: vscode.DebugSession, params: ReadMemoryArguments, context?: Context): Promise<ReadMemoryResult>;

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

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

export class VariableTracker implements AdapterCapabilities {
Expand All @@ -159,15 +164,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);
}
}
110 changes: 110 additions & 0 deletions src/plugin/adapter-registry/amalgamator-gdb-tracker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/********************************************************************************
* 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);
}
}

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> {
return Promise.resolve(this.currentContext);
}

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

0 comments on commit b24793c

Please sign in to comment.