Skip to content

Commit efc0ab0

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. Signed-off-by: Thor Thayer <thor.thayer@ericsson.com>
1 parent 99302f5 commit efc0ab0

12 files changed

Lines changed: 319 additions & 70 deletions

src/common/messaging.ts

Lines changed: 12 additions & 5 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 ConnectionContext {
42+
name: string;
43+
id: number;
44+
}
45+
4146
export interface SessionContext {
4247
sessionId?: string;
4348
canRead: boolean;
@@ -51,15 +56,17 @@ export const setMemoryViewSettingsType: NotificationType<Partial<MemoryViewSetti
5156
export const setTitleType: NotificationType<string> = { method: 'setTitle' };
5257
export const memoryWrittenType: NotificationType<WrittenMemory> = { method: 'memoryWritten' };
5358
export const sessionContextChangedType: NotificationType<SessionContext> = { method: 'sessionContextChanged' };
59+
export const connectionContextChangedType: NotificationType<[ConnectionContext?,
60+
ConnectionContext[]?]> = { method: 'connectionContextChanged' };
5461

5562
// Requests
5663
export const setOptionsType: RequestType<MemoryOptions, void> = { method: 'setOptions' };
5764
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' };
65+
export const readMemoryType: RequestType<[ReadMemoryArguments, ConnectionContext?], ReadMemoryResult> = { method: 'readMemory' };
66+
export const writeMemoryType: RequestType<[WriteMemoryArguments, ConnectionContext?], WriteMemoryResult> = { method: 'writeMemory' };
67+
export const getVariablesType: RequestType<[ReadMemoryArguments, ConnectionContext?], VariableRange[]> = { method: 'getVariables' };
68+
export const storeMemoryType: RequestType<[StoreMemoryArguments, ConnectionContext?], void> = { method: 'storeMemory' };
69+
export const applyMemoryType: RequestType<[ApplyMemoryArguments, ConnectionContext?], ApplyMemoryResult> = { method: 'applyMemory' };
6370

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

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,29 @@ 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 { ConnectionContext, ReadMemoryArguments, ReadMemoryResult,
22+
WriteMemoryArguments, WriteMemoryResult } from '../../common/messaging';
2223
import { MemoryDisplaySettingsContribution } from '../../common/webview-configuration';
2324
import { Logger } from '../logger';
2425

2526
/** Represents capabilities that may be achieved with particular debug adapters but are not part of the DAP */
2627
export interface AdapterCapabilities {
2728
/** Resolve variables known to the adapter to their locations. Fallback if {@link getResidents} is not present */
28-
getVariables?(session: vscode.DebugSession): Promise<VariableRange[]>;
29+
getVariables?(session: vscode.DebugSession, context?: ConnectionContext): Promise<VariableRange[]>;
2930
/** Resolve symbols resident in the memory at the specified range. Will be preferred to {@link getVariables} if present. */
30-
getResidents?(session: vscode.DebugSession, params: DebugProtocol.ReadMemoryArguments): Promise<VariableRange[]>;
31+
getResidents?(session: vscode.DebugSession, params: DebugProtocol.ReadMemoryArguments, context?: ConnectionContext): Promise<VariableRange[]>;
3132
/** Resolves the address of a given variable in bytes with the current context. */
32-
getAddressOfVariable?(session: vscode.DebugSession, variableName: string): Promise<string | undefined>;
33+
getAddressOfVariable?(session: vscode.DebugSession, variableName: string, context?: ConnectionContext): Promise<string | undefined>;
3334
/** Resolves the size of a given variable in bytes within the current context. */
34-
getSizeOfVariable?(session: vscode.DebugSession, variableName: string): Promise<bigint | undefined>;
35+
getSizeOfVariable?(session: vscode.DebugSession, variableName: string, context?: ConnectionContext): Promise<bigint | undefined>;
3536
/** Retrieve the suggested default display settings for the memory view. */
3637
getMemoryDisplaySettings?(session: vscode.DebugSession): Promise<Partial<MemoryDisplaySettingsContribution>>;
3738
/** Initialize the trackers of this adapter's for the debug session. */
3839
initializeAdapterTracker?(session: vscode.DebugSession): vscode.DebugAdapterTracker | undefined;
39-
readMemory?(session: vscode.DebugSession, params: ReadMemoryArguments): Promise<ReadMemoryResult>;
40-
writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments): Promise<WriteMemoryResult>;
40+
readMemory?(session: vscode.DebugSession, params: ReadMemoryArguments, context?: ConnectionContext): Promise<ReadMemoryResult>;
41+
writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments, context?: ConnectionContext): Promise<WriteMemoryResult>;
42+
getConnectionContexts?(session: vscode.DebugSession): Promise<ConnectionContext[]>;
43+
getCurrentConnectionContext?(session: vscode.DebugSession): Promise<ConnectionContext | undefined>;
4144
}
4245

4346
export type WithChildren<Original> = Original & { children?: Array<WithChildren<DebugProtocol.Variable>> };
@@ -108,14 +111,14 @@ export class AdapterVariableTracker implements vscode.DebugAdapterTracker {
108111
this.pendingMessages.clear();
109112
}
110113

111-
async getLocals(session: vscode.DebugSession): Promise<VariableRange[]> {
114+
async getLocals(session: vscode.DebugSession, context?: ConnectionContext): Promise<VariableRange[]> {
112115
this.logger.debug('Retrieving local variables in', session.name + ' Current variables:\n', this.variablesTree);
113116
if (this.currentFrame === undefined) { return []; }
114117
const maybeRanges = await Promise.all(Object.values(this.variablesTree).reduce<Array<Promise<VariableRange | undefined>>>((previous, parent) => {
115118
if (this.isDesiredVariable(parent) && parent.children?.length) {
116119
this.logger.debug('Resolving children of', parent.name);
117120
parent.children.forEach(child => {
118-
previous.push(this.variableToVariableRange(child, session));
121+
previous.push(this.variableToVariableRange(child, session, context));
119122
});
120123
} else {
121124
this.logger.debug('Ignoring', parent.name);
@@ -129,19 +132,23 @@ export class AdapterVariableTracker implements vscode.DebugAdapterTracker {
129132
return candidate.presentationHint !== 'registers' && candidate.name !== 'Registers';
130133
}
131134

132-
protected variableToVariableRange(_variable: DebugProtocol.Variable, _session: vscode.DebugSession): Promise<VariableRange | undefined> {
135+
protected variableToVariableRange(_variable: DebugProtocol.Variable, _session: vscode.DebugSession,
136+
_context?: ConnectionContext): Promise<VariableRange | undefined> {
133137
throw new Error('To be implemented by derived classes!');
134138
}
135139

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

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

142-
readMemory?(session: vscode.DebugSession, params: ReadMemoryArguments): Promise<ReadMemoryResult>;
146+
readMemory?(session: vscode.DebugSession, params: ReadMemoryArguments, context?: ConnectionContext): Promise<ReadMemoryResult>;
143147

144-
writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments): Promise<WriteMemoryResult>;
148+
writeMemory?(session: vscode.DebugSession, params: WriteMemoryArguments, context?: ConnectionContext): Promise<WriteMemoryResult>;
149+
150+
getConnectionContexts?(session: vscode.DebugSession): Promise<ConnectionContext[]>;
151+
getCurrentConnectionContext?(session: vscode.DebugSession): Promise<ConnectionContext | undefined>;
145152
}
146153

147154
export class VariableTracker implements AdapterCapabilities {
@@ -163,15 +170,15 @@ export class VariableTracker implements AdapterCapabilities {
163170
}
164171
}
165172

166-
async getVariables(session: vscode.DebugSession): Promise<VariableRange[]> {
167-
return this.sessions.get(session.id)?.getLocals(session) ?? [];
173+
async getVariables(session: vscode.DebugSession, context?: ConnectionContext): Promise<VariableRange[]> {
174+
return this.sessions.get(session.id)?.getLocals(session, context) ?? [];
168175
}
169176

170-
async getAddressOfVariable(session: vscode.DebugSession, variableName: string): Promise<string | undefined> {
171-
return this.sessions.get(session.id)?.getAddressOfVariable?.(variableName, session);
177+
async getAddressOfVariable(session: vscode.DebugSession, variableName: string, context?: ConnectionContext): Promise<string | undefined> {
178+
return this.sessions.get(session.id)?.getAddressOfVariable?.(variableName, session, context);
172179
}
173180

174-
async getSizeOfVariable(session: vscode.DebugSession, variableName: string): Promise<bigint | undefined> {
175-
return this.sessions.get(session.id)?.getSizeOfVariable?.(variableName, session);
181+
async getSizeOfVariable(session: vscode.DebugSession, variableName: string, context?: ConnectionContext): Promise<bigint | undefined> {
182+
return this.sessions.get(session.id)?.getSizeOfVariable?.(variableName, session, context);
176183
}
177184
}
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 { ConnectionContext, 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 ConnectionContexts {
27+
children?: ConnectionContext[];
28+
}
29+
export interface GetContextsResponse extends DebugProtocol.Response {
30+
body: ConnectionContexts;
31+
}
32+
export type GetContextsResult = GetContextsResponse['body'];
33+
34+
export interface AmalgamatorReadArgs extends ReadMemoryArguments {
35+
child: ConnectionContext;
36+
}
37+
38+
export class AmalgamatorSessionManager extends VariableTracker implements AdapterCapabilities {
39+
async getConnectionContexts(session: vscode.DebugSession): Promise<ConnectionContext[]> {
40+
return this.sessions.get(session.id)?.getConnectionContexts?.(session) || [];
41+
}
42+
43+
async readMemory(session: vscode.DebugSession, args: ReadMemoryArguments, context: ConnectionContext): 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: ConnectionContext): Promise<WriteMemoryResult> {
54+
return this.sessions.get(session.id)?.writeMemory?.(session, args, context);
55+
}
56+
57+
async getCurrentConnectionContext(session: vscode.DebugSession): Promise<ConnectionContext | undefined> {
58+
return this.sessions.get(session.id)?.getCurrentConnectionContext?.(session);
59+
}
60+
}
61+
62+
export class AmalgamatorGdbVariableTransformer extends AdapterVariableTracker {
63+
protected connectionContexts?: ConnectionContext[];
64+
protected currentConnectionContext?: ConnectionContext;
65+
66+
onWillReceiveMessage(message: unknown): void {
67+
if (isStacktraceRequest(message)) {
68+
if (typeof (message.arguments.threadId) !== 'undefined') {
69+
this.currentConnectionContext = {
70+
id: message.arguments.threadId,
71+
name: message.arguments.threadId.toString()
72+
};
73+
} else {
74+
this.logger.warn('Invalid ThreadID in stackTrace');
75+
this.currentConnectionContext = undefined;
76+
}
77+
} else {
78+
super.onWillReceiveMessage(message);
79+
}
80+
}
81+
82+
get frame(): number | undefined { return this.currentFrame; }
83+
84+
async getConnectionContexts(session: vscode.DebugSession): Promise<ConnectionContext[]> {
85+
if (!this.connectionContexts) {
86+
const contexts: GetContextsResult = (await session.customRequest('cdt-amalgamator/getChildDaps'));
87+
this.connectionContexts = contexts.children?.map(({ name, id }) => ({ name, id })) ?? [];
88+
}
89+
return Promise.resolve(this.connectionContexts);
90+
}
91+
92+
async getCurrentConnectionContext(_session: vscode.DebugSession): Promise<ConnectionContext | undefined> {
93+
return Promise.resolve(this.currentConnectionContext);
94+
}
95+
96+
readMemory(session: vscode.DebugSession, args: ReadMemoryArguments, context: ConnectionContext): 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)