Skip to content

Commit

Permalink
Add optional context parameter to readMemory and writeMemory
Browse files Browse the repository at this point in the history
Add context parameter to readMemory and writeMemory for those Debug Adapters
that require more information to switch or choose different contexts.
  • Loading branch information
WyoTwT committed Apr 5, 2024
1 parent 481e578 commit fd1d918
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/common/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export const sessionContextChangedType: NotificationType<SessionContext> = { met
// 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 readMemoryType: RequestType<[ReadMemoryArguments, Context?], ReadMemoryResult> = { method: 'readMemory' };
export const writeMemoryType: RequestType<[WriteMemoryArguments, Context?], 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' };
Expand Down
19 changes: 10 additions & 9 deletions src/plugin/memory-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,25 @@ export class MemoryProvider {
return vscode.debug.activeDebugSession;
}

public async readMemory(args: DebugProtocol.ReadMemoryArguments): Promise<ReadMemoryResult> {
return sendRequest(this.assertCapability('supportsReadMemoryRequest', 'read memory'), 'readMemory', args);
public async readMemory(args: [DebugProtocol.ReadMemoryArguments, Context?]): Promise<ReadMemoryResult> {
return sendRequest(this.assertCapability('supportsReadMemoryRequest', 'read memory'), 'readMemory', args[0]);
}

public async writeMemory(args: DebugProtocol.WriteMemoryArguments): Promise<WriteMemoryResult> {
public async writeMemory(args: [DebugProtocol.WriteMemoryArguments, Context?]): Promise<WriteMemoryResult> {
const readArgs = args[0];
const session = this.assertCapability('supportsWriteMemoryRequest', 'write memory');
// Schedule a emit in case we don't retrieve a memory event
this.scheduledOnDidMemoryWriteEvents[args.memoryReference] = response => {
this.scheduledOnDidMemoryWriteEvents[readArgs.memoryReference] = response => {
// We only send out a custom event if we don't expect the client to handle the memory event
// since our client is VS Code we can assume that they will always support this but better to be safe
const offset = response?.offset ? (args.offset ?? 0) + response.offset : args.offset;
const count = response?.bytesWritten ?? stringToBytesMemory(args.data).length;
this._onDidWriteMemory.fire({ memoryReference: args.memoryReference, offset, count });
const offset = response?.offset ? (readArgs.offset ?? 0) + response.offset : readArgs.offset;
const count = response?.bytesWritten ?? stringToBytesMemory(readArgs.data).length;
this._onDidWriteMemory.fire({ memoryReference: readArgs.memoryReference, offset, count });
};

return sendRequest(session, 'writeMemory', args).then(response => {
return sendRequest(session, 'writeMemory', readArgs).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);
this.scheduledOnDidMemoryWriteEvents[readArgs.memoryReference]?.(response);
return response;
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/memory-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class MemoryStorage {

const { outputFile, ...readArgs } = options;
try {
const memoryResponse = await this.memoryProvider.readMemory(readArgs);
const memoryResponse = await this.memoryProvider.readMemory([readArgs, undefined]);
const memory = createMemoryFromRead(memoryResponse);
const memoryMap = new MemoryMap({ [Number(memory.address)]: memory.bytes });
await vscode.workspace.fs.writeFile(outputFile, new TextEncoder().encode(memoryMap.asHexString()));
Expand Down Expand Up @@ -169,7 +169,7 @@ export class MemoryStorage {
memoryReference = toHexStringWithRadixMarker(address);
count = memory.length;
const data = bytesToStringMemory(memory);
await this.memoryProvider.writeMemory({ memoryReference, data });
await this.memoryProvider.writeMemory([{ memoryReference, data }, undefined]);
}
await vscode.window.showInformationMessage(`Memory from '${vscode.workspace.asRelativePath(options.uri)}' applied.`);
return { memoryReference, count, offset: 0 };
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/memory-webview-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,15 @@ export class MemoryWebview implements vscode.CustomReadonlyEditorProvider {
};
}

protected async readMemory(request: ReadMemoryArguments): Promise<ReadMemoryResult> {
protected async readMemory(request: [ReadMemoryArguments, Context?]): Promise<ReadMemoryResult> {
try {
return await this.memoryProvider.readMemory(request);
} catch (err) {
this.logError('Error fetching memory', err);
}
}

protected async writeMemory(request: WriteMemoryArguments): Promise<WriteMemoryResult> {
protected async writeMemory(request: [WriteMemoryArguments, Context?]): Promise<WriteMemoryResult> {
try {
return await this.memoryProvider.writeMemory(request);
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/webview/columns/data-column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ export class EditableDataColumnRow extends React.Component<EditableDataColumnRow
if (originalData !== this.inputText.current.value) {
const newMemoryValue = this.processData(this.inputText.current.value, this.state.editedRange);
const converted = Buffer.from(newMemoryValue, 'hex').toString('base64');
await messenger.sendRequest(writeMemoryType, HOST_EXTENSION, {
await messenger.sendRequest(writeMemoryType, HOST_EXTENSION, [{
memoryReference: toHexStringWithRadixMarker(this.state.editedRange.startAddress),
data: converted
}).catch(() => { });
}, undefined]).catch(() => { });
}

this.disableEdit();
Expand Down
2 changes: 1 addition & 1 deletion src/webview/memory-webview-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class App extends React.Component<{}, MemoryAppState> {
};

try {
const response = await messenger.sendRequest(readMemoryType, HOST_EXTENSION, completeOptions);
const response = await messenger.sendRequest(readMemoryType, HOST_EXTENSION, [completeOptions, this.state.child]);
await Promise.all(Array.from(
new Set(columnContributionService.getUpdateExecutors().concat(decorationService.getUpdateExecutors())),
executor => executor.fetchData(completeOptions)
Expand Down

0 comments on commit fd1d918

Please sign in to comment.