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 27, 2024
1 parent 93d8595 commit 47dbfc8
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 71 deletions.
17 changes: 11 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 @@ -51,16 +56,16 @@ 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' };
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);
}
36 changes: 24 additions & 12 deletions src/plugin/memory-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as vscode from 'vscode';
import { sendRequest } from '../common/debug-requests';
import { stringToBytesMemory } from '../common/memory';
import { VariableRange } from '../common/memory-range';
import { ReadMemoryResult, WriteMemoryResult } from '../common/messaging';
import { Context, ReadMemoryResult, WriteMemoryResult } from '../common/messaging';
import { AdapterRegistry } from './adapter-registry/adapter-registry';
import { isSessionEvent, SessionTracker } from './session-tracker';

Expand Down Expand Up @@ -47,14 +47,14 @@ export class MemoryProvider {
context.subscriptions.push(vscode.debug.registerDebugAdapterTrackerFactory('*', { createDebugAdapterTracker }));
}

public async readMemory(args: DebugProtocol.ReadMemoryArguments): Promise<ReadMemoryResult> {
public async readMemory(args: DebugProtocol.ReadMemoryArguments, context?: Context): Promise<ReadMemoryResult> {
const session = this.sessionTracker.assertDebugCapability(this.sessionTracker.activeSession, 'supportsReadMemoryRequest', 'read memory');
const handler = this.adapterRegistry?.getHandlerForSession(session.type);
if (handler?.readMemory) { return handler.readMemory(session, args); }
if (handler?.readMemory) { return handler.readMemory(session, args, context); }
return sendRequest(session, 'readMemory', args);
}

public async writeMemory(args: DebugProtocol.WriteMemoryArguments): Promise<WriteMemoryResult> {
public async writeMemory(args: DebugProtocol.WriteMemoryArguments, context?: Context): Promise<WriteMemoryResult> {
const session = this.sessionTracker.assertDebugCapability(this.sessionTracker.activeSession, 'supportsWriteMemoryRequest', 'write memory');
// Schedule a emit in case we don't retrieve a memory event
this.scheduledOnDidMemoryWriteEvents[session.id + '_' + args.memoryReference] = response => {
Expand All @@ -67,7 +67,7 @@ export class MemoryProvider {
};
const handler = this.adapterRegistry?.getHandlerForSession(session.type);
if (handler?.writeMemory) {
return handler.writeMemory(session, args).then(response => {
return handler.writeMemory(session, args, context).then(response => {
// The memory event is handled before we got here, if the scheduled event still exists, we need to handle it
this.scheduledOnDidMemoryWriteEvents[args.memoryReference]?.(response);
return response;
Expand All @@ -81,22 +81,34 @@ export class MemoryProvider {
});
}

public async getVariables(variableArguments: DebugProtocol.ReadMemoryArguments): Promise<VariableRange[]> {
public async getVariables(variableArguments: DebugProtocol.ReadMemoryArguments, context?: Context): Promise<VariableRange[]> {
const session = this.sessionTracker.assertActiveSession('get variables');
const handler = this.adapterRegistry?.getHandlerForSession(session.type);
if (handler?.getResidents) { return handler.getResidents(session, variableArguments); }
return handler?.getVariables?.(session) ?? [];
if (handler?.getResidents) { return handler.getResidents(session, variableArguments, context); }
return handler?.getVariables?.(session, context) ?? [];
}

public async getAddressOfVariable(variableName: string): Promise<string | undefined> {
public async getAddressOfVariable(variableName: string, context?: Context): Promise<string | undefined> {
const session = this.sessionTracker.assertActiveSession('get address of variable');
const handler = this.adapterRegistry?.getHandlerForSession(session.type);
return handler?.getAddressOfVariable?.(session, variableName);
return handler?.getAddressOfVariable?.(session, variableName, context);
}

public async getSizeOfVariable(variableName: string): Promise<bigint | undefined> {
public async getSizeOfVariable(variableName: string, context?: Context): Promise<bigint | undefined> {
const session = this.sessionTracker.assertActiveSession('get address of variable');
const handler = this.adapterRegistry?.getHandlerForSession(session.type);
return handler?.getSizeOfVariable?.(session, variableName);
return handler?.getSizeOfVariable?.(session, variableName, context);
}

public async getContexts(): Promise<Context[]> {
const session = this.sessionTracker.assertActiveSession('get list of debug Contexts');
const handler = this.adapterRegistry?.getHandlerForSession(session.type);
return handler?.getContexts?.(session) ?? [];
}

public async getCurrentContext(): Promise<Context|undefined> {
const session = this.sessionTracker.assertActiveSession('get current debug Context');
const handler = this.adapterRegistry?.getHandlerForSession(session.type);
return handler?.getCurrentContext?.(session);
}
}
Loading

0 comments on commit 47dbfc8

Please sign in to comment.