@@ -28,9 +28,11 @@ import { toHexStringWithRadixMarker } from '../common/memory-range';
2828import { ApplyMemoryArguments , ApplyMemoryResult , MemoryOptions , StoreMemoryArguments } from '../common/messaging' ;
2929import { isWebviewContext } from '../common/webview-context' ;
3030import { MemoryProvider } from './memory-provider' ;
31+ import { MemoryProviderManager } from './memory-provider-manager' ;
32+ import { SessionTracker } from './session-tracker' ;
3133
32- export const StoreCommandType = `${ manifest . PACKAGE_NAME } .store-file` ;
33- export const ApplyCommandType = `${ manifest . PACKAGE_NAME } .apply-file` ;
34+ const StoreCommandType = `${ manifest . PACKAGE_NAME } .store-file` ;
35+ const ApplyCommandType = `${ manifest . PACKAGE_NAME } .apply-file` ;
3436
3537const VALID_FILE_NAME_CHARS = / [ ^ a - z A - Z 0 - 9 _ - ] / g;
3638
@@ -49,19 +51,26 @@ interface ApplyMemoryOptions {
4951 uri : vscode . Uri ;
5052}
5153
54+ const getActiveSession = ( ) => vscode . debug . activeDebugSession ;
55+
5256export class MemoryStorage {
53- constructor ( protected memoryProvider : MemoryProvider ) {
57+ constructor ( protected sessionTracker : SessionTracker , protected memoryProviderManager : MemoryProviderManager ) {
5458 }
5559
5660 public activate ( context : vscode . ExtensionContext ) : void {
5761 context . subscriptions . push (
58- vscode . commands . registerCommand ( StoreCommandType , args => this . storeMemory ( args ) ) ,
59- vscode . commands . registerCommand ( ApplyCommandType , args => this . applyMemory ( args ) )
62+ vscode . commands . registerCommand ( StoreCommandType , args => this . storeMemory ( getActiveSession ( ) ?. id , args ) ) ,
63+ vscode . commands . registerCommand ( ApplyCommandType , args => this . applyMemory ( getActiveSession ( ) ?. id , args ) )
6064 ) ;
6165 }
6266
63- public async storeMemory ( args ?: StoreMemoryArguments ) : Promise < void > {
64- const providedDefaultOptions = await this . storeArgsToOptions ( args ) ;
67+ public async storeMemory ( sessionId : string | undefined , args ?: StoreMemoryArguments ) : Promise < void > {
68+ // Even if we disable the command in VS Code through enablement or when condition, programmatic execution is still possible.
69+ // However, we want to fail early in case the user tries to execute a disabled command
70+ this . sessionTracker . assertDebugCapability ( this . sessionTracker . assertSession ( sessionId ) , 'supportsReadMemoryRequest' , 'store memory' ) ;
71+
72+ const memoryProvider = this . memoryProviderManager . getProvider ( sessionId ) ;
73+ const providedDefaultOptions = await this . storeArgsToOptions ( memoryProvider , args ) ;
6574 const options = await this . getStoreMemoryOptions ( providedDefaultOptions ) ;
6675 if ( ! options ) {
6776 // user aborted process
@@ -70,7 +79,7 @@ export class MemoryStorage {
7079
7180 const { outputFile, ...readArgs } = options ;
7281 try {
73- const memoryResponse = await this . memoryProvider . readMemory ( readArgs ) ;
82+ const memoryResponse = await memoryProvider . readMemory ( readArgs ) ;
7483 const memory = createMemoryFromRead ( memoryResponse ) ;
7584 const memoryMap = new MemoryMap ( { [ Number ( memory . address ) ] : memory . bytes } ) ;
7685 await vscode . workspace . fs . writeFile ( outputFile , new TextEncoder ( ) . encode ( memoryMap . asHexString ( ) ) ) ;
@@ -89,7 +98,7 @@ export class MemoryStorage {
8998 }
9099 }
91100
92- protected async storeArgsToOptions ( args ?: StoreMemoryArguments ) : Promise < Partial < StoreMemoryOptions > > {
101+ protected async storeArgsToOptions ( memoryProvider : MemoryProvider , args ?: StoreMemoryArguments ) : Promise < Partial < StoreMemoryOptions > > {
93102 if ( ! args ) {
94103 return { } ;
95104 }
@@ -99,8 +108,8 @@ export class MemoryStorage {
99108 if ( isVariablesContext ( args ) ) {
100109 try {
101110 const variableName = args . variable . evaluateName ?? args . variable . name ;
102- const count = await this . memoryProvider . getSizeOfVariable ( variableName ) ;
103- const memoryReference = args . variable . memoryReference ?? await this . memoryProvider . getAddressOfVariable ( variableName ) ;
111+ const count = await memoryProvider . getSizeOfVariable ( variableName ) ;
112+ const memoryReference = args . variable . memoryReference ?? await memoryProvider . getAddressOfVariable ( variableName ) ;
104113 return { count : Number ( count ) , memoryReference, offset : 0 , proposedOutputName : variableName } ;
105114 } catch ( error ) {
106115 // ignore, we are just using them as default values
@@ -153,7 +162,12 @@ export class MemoryStorage {
153162 return { memoryReference, offset : Number ( offset ) , count : Number ( count ) , outputFile } ;
154163 }
155164
156- public async applyMemory ( args ?: ApplyMemoryArguments ) : Promise < ApplyMemoryResult > {
165+ public async applyMemory ( sessionId : string | undefined , args ?: ApplyMemoryArguments ) : Promise < ApplyMemoryResult > {
166+ // Even if we disable the command in VS Code through enablement or when condition, programmatic execution is still possible.
167+ // However, we want to fail early in case the user tries to execute a disabled command
168+ this . sessionTracker . assertDebugCapability ( this . sessionTracker . assertSession ( sessionId ) , 'supportsWriteMemoryRequest' , 'apply memory' ) ;
169+
170+ const memoryProvider = this . memoryProviderManager . getProvider ( sessionId ) ;
157171 const providedDefaultOptions = await this . applyArgsToOptions ( args ) ;
158172 const options = await this . getApplyMemoryOptions ( providedDefaultOptions ) ;
159173 if ( ! options ) {
@@ -169,7 +183,7 @@ export class MemoryStorage {
169183 memoryReference = toHexStringWithRadixMarker ( address ) ;
170184 count = memory . length ;
171185 const data = bytesToStringMemory ( memory ) ;
172- await this . memoryProvider . writeMemory ( { memoryReference, data } ) ;
186+ await memoryProvider . writeMemory ( { memoryReference, data } ) ;
173187 }
174188 await vscode . window . showInformationMessage ( `Memory from '${ vscode . workspace . asRelativePath ( options . uri ) } ' applied.` ) ;
175189 return { memoryReference, count, offset : 0 } ;
0 commit comments