1
1
import * as vscode from 'vscode' ;
2
+ import { TextDecoder } from 'util' ;
2
3
3
4
// Track sessions since vscode doesn't provide a list of them.
4
5
const sessions = new Map < string , vscode . DebugSession > ( ) ;
@@ -25,21 +26,45 @@ export function registerGoDebugCommands(ctx: vscode.ExtensionContext) {
25
26
const session = sessions . get ( sessionId ) ;
26
27
if ( ! session ) return 'Debug session has been terminated' ;
27
28
28
- const r : { variables : Variable [ ] } = await session . customRequest ( 'variables' , {
29
+ const { variables } = await session . customRequest ( 'variables' , {
29
30
variablesReference : parseInt ( container , 10 )
30
- } ) ;
31
+ } ) as { variables : Variable [ ] } ;
31
32
32
- const v = r . variables . find ( ( v ) => v . name === name ) ;
33
+ const v = variables . find ( v => v . name === name ) ;
33
34
if ( ! v ) return `Cannot resolve variable ${ name } ` ;
34
35
35
- const { result } = await session . customRequest ( 'evaluate' , {
36
- expression : v . evaluateName ,
37
- context : 'clipboard'
38
- } ) ;
36
+ if ( ! v . memoryReference ) {
37
+ const { result } = await session . customRequest ( 'evaluate' , {
38
+ expression : v . evaluateName ,
39
+ context : 'clipboard'
40
+ } ) as { result : string } ;
41
+
42
+ v . value = result ?? v . value ;
43
+
44
+ return parseVariable ( v ) ;
45
+ }
46
+
47
+ const chunk = 1 << 14 ;
48
+ let offset = 0 ;
49
+ let full : Uint8Array [ ] = [ ] ;
39
50
40
- v . value = result ?? v . value ;
51
+ while ( true ) {
52
+ const resp = await session . customRequest ( 'readMemory' , {
53
+ memoryReference : v . memoryReference ,
54
+ offset,
55
+ count : chunk
56
+ } ) as { address : string ; data : string ; unreadableBytes : number } ;
41
57
42
- return parseVariable ( v ) ;
58
+ if ( ! resp . data ) break ;
59
+ full . push ( Buffer . from ( resp . data , 'base64' ) ) ;
60
+
61
+ if ( resp . unreadableBytes === 0 ) break ;
62
+ offset += chunk ;
63
+ }
64
+
65
+ const allBytes = Buffer . concat ( full ) ;
66
+
67
+ return new TextDecoder ( 'utf-8' ) . decode ( allBytes ) ;
43
68
}
44
69
}
45
70
@@ -72,6 +97,7 @@ export function registerGoDebugCommands(ctx: vscode.ExtensionContext) {
72
97
value : string ;
73
98
evaluateName : string ;
74
99
variablesReference : number ;
100
+ memoryReference ?: string ;
75
101
}
76
102
77
103
const escapeCodes : Record < string , string > = {
@@ -83,14 +109,9 @@ export function registerGoDebugCommands(ctx: vscode.ExtensionContext) {
83
109
function parseVariable ( variable : Variable ) {
84
110
let raw = variable . value . trim ( ) ;
85
111
try {
86
- // Attempt to parse as JSON
87
112
return JSON . parse ( raw ) ;
88
113
} catch ( _ ) {
89
- // Fall back to manual unescaping
90
- raw = raw . slice ( 1 , - 1 ) ;
114
+ return raw . replace ( / \\ [ n r t \\ " ' ` ] / , ( _ , s ) => ( s in escapeCodes ? escapeCodes [ s ] : s ) ) ;
91
115
}
92
-
93
- // Manually unescape
94
- return raw . replace ( / \\ [ n r t \\ " ' ` ] / , ( _ , s ) => ( s in escapeCodes ? escapeCodes [ s ] : s ) ) ;
95
116
}
96
117
}
0 commit comments