Skip to content

Commit 61c41c3

Browse files
committed
Adding Context for cdt-amalgamator and other DAPs
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.
1 parent f1384d6 commit 61c41c3

12 files changed

Lines changed: 297 additions & 68 deletions

File tree

src/common/messaging.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ export type StoreMemoryResult = void;
3838
export type ApplyMemoryArguments = URI | undefined;
3939
export type ApplyMemoryResult = MemoryOptions;
4040

41+
export interface Context {
42+
name: string;
43+
id: number;
44+
}
45+
4146
export interface SessionContext {
4247
sessionId?: string;
4348
canRead: boolean;
@@ -50,20 +55,23 @@ export const setMemoryViewSettingsType: NotificationType<Partial<MemoryViewSetti
5055
export const resetMemoryViewSettingsType: NotificationType<void> = { method: 'resetMemoryViewSettings' };
5156
export const setTitleType: NotificationType<string> = { method: 'setTitle' };
5257
export const memoryWrittenType: NotificationType<WrittenMemory> = { method: 'memoryWritten' };
53-
export const sessionContextChangedType: NotificationType<SessionContext> = { method: 'sessionContextChanged' };
58+
export const sessionContextChangedType: NotificationType<[SessionContext, Context?, Context[]?]> = { method: 'sessionContextChanged' };
5459

5560
// Requests
5661
export const setOptionsType: RequestType<MemoryOptions, void> = { method: 'setOptions' };
5762
export const logMessageType: RequestType<string, void> = { method: 'logMessage' };
58-
export const readMemoryType: RequestType<ReadMemoryArguments, ReadMemoryResult> = { method: 'readMemory' };
59-
export const writeMemoryType: RequestType<WriteMemoryArguments, WriteMemoryResult> = { method: 'writeMemory' };
60-
export const getVariablesType: RequestType<ReadMemoryArguments, VariableRange[]> = { method: 'getVariables' };
61-
export const storeMemoryType: RequestType<StoreMemoryArguments, void> = { method: 'storeMemory' };
62-
export const applyMemoryType: RequestType<ApplyMemoryArguments, ApplyMemoryResult> = { method: 'applyMemory' };
63+
export const readMemoryType: RequestType<[ReadMemoryArguments, Context?], ReadMemoryResult> = { method: 'readMemory' };
64+
export const writeMemoryType: RequestType<[WriteMemoryArguments, Context?], WriteMemoryResult> = { method: 'writeMemory' };
65+
export const getVariablesType: RequestType<[ReadMemoryArguments, Context?], VariableRange[]> = { method: 'getVariables' };
66+
export const storeMemoryType: RequestType<[StoreMemoryArguments, Context?], void> = { method: 'storeMemory' };
67+
export const applyMemoryType: RequestType<[ApplyMemoryArguments, Context?], ApplyMemoryResult> = { method: 'applyMemory' };
6368

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

72+
export const getContextsType: RequestType<null, Context[]> = { method: 'getContexts' };
73+
export const getCurrentContextType: RequestType<null, Context> = { method: 'getCurrentContext' };
74+
6775
export interface WebviewSelection {
6876
selectedCell?: {
6977
column: string

src/entry-points/desktop/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import * as vscode from 'vscode';
1818
import { AdapterRegistry } from '../../plugin/adapter-registry/adapter-registry';
19+
import { AmalgamatorGdbVariableTransformer, AmalgamatorSessionManager } from '../../plugin/adapter-registry/amalgamator-gdb-tracker';
1920
import { CAdapter } from '../../plugin/adapter-registry/c-adapter';
21+
import { outputChannelLogger } from '../../plugin/logger';
2022
import { MemoryProvider } from '../../plugin/memory-provider';
2123
import { MemoryStorage } from '../../plugin/memory-storage';
2224
import { MemoryWebview } from '../../plugin/memory-webview-main';
@@ -27,6 +29,8 @@ export const activate = async (context: vscode.ExtensionContext): Promise<Adapte
2729
const memoryView = new MemoryWebview(context.extensionUri, memoryProvider);
2830
const memoryStorage = new MemoryStorage(memoryProvider);
2931
const cAdapter = new CAdapter(registry);
32+
const debugTypes = ['amalgamator'];
33+
registry.registerAdapter(new AmalgamatorSessionManager(AmalgamatorGdbVariableTransformer, outputChannelLogger, ...debugTypes), ...debugTypes);
3034

3135
memoryProvider.activate(context);
3236
registry.activate(context);

src/plugin/adapter-registry/adapter-capabilities.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ import { DebugProtocol } from '@vscode/debugprotocol';
1818
import * as vscode from 'vscode';
1919
import { isDebugRequest, isDebugResponse } from '../../common/debug-requests';
2020
import { VariableRange } from '../../common/memory-range';
21-
import { ReadMemoryArguments, ReadMemoryResult, WriteMemoryArguments, WriteMemoryResult } from '../../common/messaging';
21+
import { Context, ReadMemoryArguments, ReadMemoryResult, WriteMemoryArguments, WriteMemoryResult } from '../../common/messaging';
2222
import { Logger } from '../logger';
2323

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

3941
export type WithChildren<Original> = Original & { children?: Array<WithChildren<DebugProtocol.Variable>> };
@@ -104,14 +106,14 @@ export class AdapterVariableTracker implements vscode.DebugAdapterTracker {
104106
this.pendingMessages.clear();
105107
}
106108

107-
async getLocals(session: vscode.DebugSession): Promise<VariableRange[]> {
109+
async getLocals(session: vscode.DebugSession, context?: Context): Promise<VariableRange[]> {
108110
this.logger.debug('Retrieving local variables in', session.name + ' Current variables:\n', this.variablesTree);
109111
if (this.currentFrame === undefined) { return []; }
110112
const maybeRanges = await Promise.all(Object.values(this.variablesTree).reduce<Array<Promise<VariableRange | undefined>>>((previous, parent) => {
111113
if (this.isDesiredVariable(parent) && parent.children?.length) {
112114
this.logger.debug('Resolving children of', parent.name);
113115
parent.children.forEach(child => {
114-
previous.push(this.variableToVariableRange(child, session));
116+
previous.push(this.variableToVariableRange(child, session, context));
115117
});
116118
} else {
117119
this.logger.debug('Ignoring', parent.name);
@@ -125,19 +127,22 @@ export class AdapterVariableTracker implements vscode.DebugAdapterTracker {
125127
return candidate.presentationHint !== 'registers' && candidate.name !== 'Registers';
126128
}
127129

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

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

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

138-
readMemory?(session: vscode.DebugSession, params: ReadMemoryArguments): Promise<ReadMemoryResult>;
140+
readMemory?(session: vscode.DebugSession, params: ReadMemoryArguments, context: Context): Promise<ReadMemoryResult>;
139141

140-
writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments): Promise<WriteMemoryResult>;
142+
writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments, context: Context): Promise<WriteMemoryResult>;
143+
144+
getContexts?(session: vscode.DebugSession): Promise<Context[]>;
145+
getCurrentContext?(session: vscode.DebugSession): Promise<Context | undefined>;
141146
}
142147

143148
export class VariableTracker implements AdapterCapabilities {
@@ -159,15 +164,15 @@ export class VariableTracker implements AdapterCapabilities {
159164
}
160165
}
161166

162-
async getVariables(session: vscode.DebugSession): Promise<VariableRange[]> {
163-
return this.sessions.get(session.id)?.getLocals(session) ?? [];
167+
async getVariables(session: vscode.DebugSession, context?: Context): Promise<VariableRange[]> {
168+
return this.sessions.get(session.id)?.getLocals(session, context) ?? [];
164169
}
165170

166-
async getAddressOfVariable(session: vscode.DebugSession, variableName: string): Promise<string | undefined> {
167-
return this.sessions.get(session.id)?.getAddressOfVariable?.(variableName, session);
171+
async getAddressOfVariable(session: vscode.DebugSession, variableName: string, context?: Context): Promise<string | undefined> {
172+
return this.sessions.get(session.id)?.getAddressOfVariable?.(variableName, session, context);
168173
}
169174

170-
async getSizeOfVariable(session: vscode.DebugSession, variableName: string): Promise<bigint | undefined> {
171-
return this.sessions.get(session.id)?.getSizeOfVariable?.(variableName, session);
175+
async getSizeOfVariable(session: vscode.DebugSession, variableName: string, context?: Context): Promise<bigint | undefined> {
176+
return this.sessions.get(session.id)?.getSizeOfVariable?.(variableName, session, context);
172177
}
173178
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/********************************************************************************
2+
* Copyright (C) 2024 Ericsson, Arm and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the Eclipse
10+
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
11+
* with the GNU Classpath Exception which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
********************************************************************************/
16+
17+
import { DebugProtocol } from '@vscode/debugprotocol';
18+
import * as vscode from 'vscode';
19+
import { Context, ReadMemoryArguments, ReadMemoryResult, WriteMemoryArguments, WriteMemoryResult } from '../../common/messaging';
20+
import { AdapterCapabilities, AdapterVariableTracker, VariableTracker } from './adapter-capabilities';
21+
22+
// Copied from cdt-amalgamator [AmalgamatorSession.d.ts] file
23+
/**
24+
* Response for our custom 'cdt-amalgamator/getChildDaps' request.
25+
*/
26+
export interface Contexts {
27+
children?: Context[];
28+
}
29+
export interface GetContextsResponse extends DebugProtocol.Response {
30+
body: Contexts;
31+
}
32+
export type GetContextsResult = GetContextsResponse['body'];
33+
34+
export interface AmalgamatorReadArgs extends ReadMemoryArguments {
35+
child: Context;
36+
}
37+
38+
export class AmalgamatorSessionManager extends VariableTracker implements AdapterCapabilities {
39+
async getContexts(session: vscode.DebugSession): Promise<Context[]> {
40+
return this.sessions.get(session.id)?.getContexts?.(session) || [];
41+
}
42+
43+
async readMemory(session: vscode.DebugSession, args: ReadMemoryArguments, context: Context): Promise<ReadMemoryResult> {
44+
if (!context) {
45+
vscode.window.showErrorMessage('Invalid context for Amalgamator. Select Context in Dropdown');
46+
return {
47+
address: args.memoryReference
48+
};
49+
}
50+
return this.sessions.get(session.id)?.readMemory?.(session, args, context);
51+
}
52+
53+
async writeMemory(session: vscode.DebugSession, args: WriteMemoryArguments, context: Context): Promise<WriteMemoryResult> {
54+
return this.sessions.get(session.id)?.writeMemory?.(session, args, context);
55+
}
56+
57+
async getCurrentContext(session: vscode.DebugSession): Promise<Context | undefined> {
58+
return this.sessions.get(session.id)?.getCurrentContext?.(session);
59+
}
60+
}
61+
62+
export class AmalgamatorGdbVariableTransformer extends AdapterVariableTracker {
63+
protected contexts?: Context[];
64+
protected currentContext?: Context;
65+
66+
onWillReceiveMessage(message: unknown): void {
67+
if (isStacktraceRequest(message)) {
68+
if (typeof(message.arguments.threadId) !== 'undefined') {
69+
this.currentContext = {
70+
id: message.arguments.threadId,
71+
name: message.arguments.threadId.toString()
72+
};
73+
} else {
74+
this.logger.warn('Invalid ThreadID in stackTrace');
75+
this.currentContext = undefined;
76+
}
77+
} else {
78+
super.onWillReceiveMessage(message);
79+
}
80+
}
81+
82+
get frame(): number | undefined { return this.currentFrame; }
83+
84+
async getContexts(session: vscode.DebugSession): Promise<Context[]> {
85+
if (!this.contexts) {
86+
const contexts: GetContextsResult = (await session.customRequest('cdt-amalgamator/getChildDaps'));
87+
this.contexts = contexts.children?.map(({ name, id }) => ({ name, id })) ?? [];
88+
}
89+
return Promise.resolve(this.contexts);
90+
}
91+
92+
async getCurrentContext(_session: vscode.DebugSession): Promise<Context | undefined> {
93+
return Promise.resolve(this.currentContext);
94+
}
95+
96+
readMemory(session: vscode.DebugSession, args: ReadMemoryArguments, context: Context): Promise<ReadMemoryResult> {
97+
const amalReadArgs: AmalgamatorReadArgs = { ...args, child: context };
98+
return Promise.resolve(session.customRequest('cdt-amalgamator/readMemory', amalReadArgs));
99+
}
100+
}
101+
102+
export function isStacktraceRequest(message: unknown): message is DebugProtocol.StackTraceRequest {
103+
const candidate = message as DebugProtocol.StackTraceRequest;
104+
return !!candidate && candidate.command === 'stackTrace';
105+
}
106+
107+
export function isStacktraceResponse(message: unknown): message is DebugProtocol.StackTraceResponse {
108+
const candidate = message as DebugProtocol.StackTraceResponse;
109+
return !!candidate && candidate.command === 'stackTrace' && Array.isArray(candidate.body.stackFrames);
110+
}

0 commit comments

Comments
 (0)