-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdebug-adapter.ts
More file actions
499 lines (427 loc) · 12.8 KB
/
Copy pathdebug-adapter.ts
File metadata and controls
499 lines (427 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/**
* Core Debug Adapter Interface for multi-language debugging support
*
* This interface defines the contract that all language-specific debug adapters
* must implement. It abstracts the Debug Adapter Protocol (DAP) operations
* while allowing language-specific implementations.
*
* Design Principles:
* - Language agnostic
* - Async-first for all operations
* - Event-driven for state changes
* - Minimal overhead (< 5ms per operation)
*
* @since 2.0.0
*/
import { EventEmitter } from 'events';
import { DebugProtocol } from '@vscode/debugprotocol';
import { DebugLanguage, GenericAttachConfig, LanguageSpecificAttachConfig } from '../models/index.js';
import type { AdapterLaunchBarrier } from './adapter-launch-barrier.js';
/**
* Core debug adapter interface that all language adapters must implement
*/
export interface IDebugAdapter extends EventEmitter {
readonly language: DebugLanguage;
readonly name: string; // e.g., "Python Debug Adapter", "Node.js Debug Adapter"
// ===== Lifecycle Management =====
/**
* Initialize the adapter and validate the environment
*/
initialize(): Promise<void>;
/**
* Clean up resources and connections
*/
dispose(): Promise<void>;
// ===== State Management =====
/**
* Get the current adapter state
*/
getState(): AdapterState;
/**
* Check if the adapter is ready for debugging
*/
isReady(): boolean;
/**
* Get the current thread ID (if debugging)
*/
getCurrentThreadId(): number | null;
// ===== Environment Validation =====
/**
* Validate that the environment is properly configured for debugging
*/
validateEnvironment(): Promise<ValidationResult>;
/**
* Get list of required dependencies for this adapter
*/
getRequiredDependencies(): DependencyInfo[];
// ===== Executable Management =====
/**
* Resolve the path to the language executable
* @param preferredPath Optional user-specified path
*/
resolveExecutablePath(preferredPath?: string): Promise<string>;
/**
* Get the default executable name for this language
* @example 'python', 'node', 'go'
*/
getDefaultExecutableName(): string;
/**
* Get platform-specific paths to search for the executable
*/
getExecutableSearchPaths(): string[];
// ===== Adapter Configuration =====
/**
* Build the command to launch the debug adapter
*/
buildAdapterCommand(config: AdapterConfig): AdapterCommand;
/**
* Get the debug adapter module name
* @example 'debugpy.adapter', 'node-debug2'
*/
getAdapterModuleName(): string;
/**
* Get the command to install the debug adapter
* @example 'pip install debugpy', 'npm install -g node-debug2'
*/
getAdapterInstallCommand(): string;
/**
* Optionally provide a launch barrier that customizes how ProxyManager should
* coordinate a specific DAP request (e.g., fire-and-forget launches).
*/
createLaunchBarrier?(command: string, args?: unknown): AdapterLaunchBarrier | undefined;
// ===== Debug Configuration =====
/**
* Transform generic launch config to language-specific format
*
* @returns Promise resolving to language-specific launch configuration
* @since 2.1.0 - Made async to support build operations (e.g., Rust compilation)
*/
transformLaunchConfig(config: GenericLaunchConfig): Promise<LanguageSpecificLaunchConfig>;
/**
* Get default launch configuration for this language
*/
getDefaultLaunchConfig(): Partial<GenericLaunchConfig>;
/**
* Check if this adapter supports attaching to running processes
* @returns true if attach is supported, false otherwise
*/
supportsAttach?(): boolean;
/**
* Check if this adapter supports detaching without terminating the debuggee
* @returns true if detach is supported, false otherwise
*/
supportsDetach?(): boolean;
/**
* Transform generic attach config to language-specific format
* Only called if supportsAttach() returns true
* @param config Generic attach configuration
* @returns Language-specific attach configuration
*/
transformAttachConfig?(config: GenericAttachConfig): LanguageSpecificAttachConfig;
/**
* Get default attach configuration for this language
* Only called if supportsAttach() returns true
* @returns Default attach configuration with language-specific defaults
*/
getDefaultAttachConfig?(): Partial<GenericAttachConfig>;
// ===== DAP Protocol Operations =====
/**
* Send a DAP request through the adapter
*/
sendDapRequest<T extends DebugProtocol.Response>(
command: string,
args?: unknown
): Promise<T>;
/**
* Handle incoming DAP event
*/
handleDapEvent(event: DebugProtocol.Event): void;
/**
* Handle incoming DAP response
*/
handleDapResponse(response: DebugProtocol.Response): void;
// ===== Connection Management =====
/**
* Connect to the debug adapter
*/
connect(host: string, port: number): Promise<void>;
/**
* Disconnect from the debug adapter
*/
disconnect(): Promise<void>;
/**
* Check if connected to the debug adapter
*/
isConnected(): boolean;
// ===== Error Handling =====
/**
* Get installation instructions for this language's debugger
*/
getInstallationInstructions(): string;
/**
* Get error message when executable is missing
*/
getMissingExecutableError(): string;
/**
* Translate generic errors to language-specific messages
*/
translateErrorMessage(error: Error): string;
// ===== Feature Support =====
/**
* Check if a specific debug feature is supported
*/
supportsFeature(feature: DebugFeature): boolean;
/**
* Get requirements for a specific feature
*/
getFeatureRequirements(feature: DebugFeature): FeatureRequirement[];
/**
* Get full capability declaration
*/
getCapabilities(): AdapterCapabilities;
}
// ===== Supporting Types =====
/**
* Adapter state enumeration
*/
export enum AdapterState {
UNINITIALIZED = 'uninitialized',
INITIALIZING = 'initializing',
READY = 'ready',
CONNECTED = 'connected',
DEBUGGING = 'debugging',
DISCONNECTED = 'disconnected',
ERROR = 'error'
}
/**
* Environment validation result
*/
export interface ValidationResult {
valid: boolean;
errors: ValidationError[];
warnings: ValidationWarning[];
}
/**
* Validation error details
*/
export interface ValidationError {
code: string;
message: string;
recoverable: boolean;
}
/**
* Validation warning details
*/
export interface ValidationWarning {
code: string;
message: string;
}
/**
* Dependency information
*/
export interface DependencyInfo {
name: string;
version?: string;
required: boolean;
installCommand?: string;
}
/**
* Command to launch debug adapter
*/
export interface AdapterCommand {
command: string;
args: string[];
env?: Record<string, string>;
}
/**
* Adapter configuration
*/
export interface AdapterConfig {
sessionId: string;
executablePath: string;
adapterHost: string;
adapterPort: number;
logDir: string;
scriptPath: string;
scriptArgs?: string[];
launchConfig: GenericLaunchConfig;
}
/**
* Generic launch configuration (common across languages)
*/
export interface GenericLaunchConfig {
stopOnEntry?: boolean;
justMyCode?: boolean;
env?: Record<string, string>;
cwd?: string;
args?: string[];
// Common debug configuration options
}
/**
* Language-specific launch configuration
*/
export interface LanguageSpecificLaunchConfig extends GenericLaunchConfig {
// Language-specific additions
[key: string]: unknown;
}
/**
* Debug features enumeration (from DAP spec)
*/
export enum DebugFeature {
CONDITIONAL_BREAKPOINTS = 'conditionalBreakpoints',
FUNCTION_BREAKPOINTS = 'functionBreakpoints',
EXCEPTION_BREAKPOINTS = 'exceptionBreakpoints',
VARIABLE_PAGING = 'variablePaging',
EVALUATE_FOR_HOVERS = 'evaluateForHovers',
SET_VARIABLE = 'setVariable',
SET_EXPRESSION = 'setExpression',
DATA_BREAKPOINTS = 'dataBreakpoints',
DISASSEMBLE_REQUEST = 'disassembleRequest',
TERMINATE_THREADS_REQUEST = 'terminateThreadsRequest',
DELAYED_STACK_TRACE_LOADING = 'delayedStackTraceLoading',
LOADED_SOURCES_REQUEST = 'loadedSourcesRequest',
LOG_POINTS = 'logPoints',
TERMINATE_REQUEST = 'terminateRequest',
RESTART_REQUEST = 'restartRequest',
EXCEPTION_OPTIONS = 'exceptionOptions',
EXCEPTION_INFO_REQUEST = 'exceptionInfoRequest',
STEP_BACK = 'stepBack',
REVERSE_DEBUGGING = 'reverseDebugging',
STEP_IN_TARGETS_REQUEST = 'stepInTargetsRequest'
}
/**
* Feature requirement details
*/
export interface FeatureRequirement {
type: 'dependency' | 'version' | 'configuration';
description: string;
required: boolean;
}
/**
* Full adapter capabilities (mirrors DAP capabilities)
*/
export interface AdapterCapabilities {
supportsConfigurationDoneRequest?: boolean;
supportsFunctionBreakpoints?: boolean;
supportsConditionalBreakpoints?: boolean;
supportsHitConditionalBreakpoints?: boolean;
supportsEvaluateForHovers?: boolean;
exceptionBreakpointFilters?: ExceptionBreakpointFilter[];
supportsStepBack?: boolean;
supportsSetVariable?: boolean;
supportsRestartFrame?: boolean;
supportsGotoTargetsRequest?: boolean;
supportsStepInTargetsRequest?: boolean;
supportsCompletionsRequest?: boolean;
completionTriggerCharacters?: string[];
supportsModulesRequest?: boolean;
additionalModuleColumns?: DebugProtocol.ColumnDescriptor[];
supportedChecksumAlgorithms?: DebugProtocol.ChecksumAlgorithm[];
supportsRestartRequest?: boolean;
supportsExceptionOptions?: boolean;
supportsValueFormattingOptions?: boolean;
supportsExceptionInfoRequest?: boolean;
supportTerminateDebuggee?: boolean;
supportSuspendDebuggee?: boolean;
supportsDelayedStackTraceLoading?: boolean;
supportsLoadedSourcesRequest?: boolean;
supportsLogPoints?: boolean;
supportsTerminateThreadsRequest?: boolean;
supportsSetExpression?: boolean;
supportsTerminateRequest?: boolean;
supportsDataBreakpoints?: boolean;
supportsReadMemoryRequest?: boolean;
supportsWriteMemoryRequest?: boolean;
supportsDisassembleRequest?: boolean;
supportsCancelRequest?: boolean;
supportsBreakpointLocationsRequest?: boolean;
supportsClipboardContext?: boolean;
supportsSteppingGranularity?: boolean;
supportsInstructionBreakpoints?: boolean;
supportsExceptionFilterOptions?: boolean;
supportsSingleThreadExecutionRequests?: boolean;
}
/**
* Exception breakpoint filter
*/
export interface ExceptionBreakpointFilter {
filter: string;
label: string;
description?: string;
default?: boolean;
supportsCondition?: boolean;
conditionDescription?: string;
}
// ===== Error Handling =====
/**
* Base adapter error class
*/
export class AdapterError extends Error {
constructor(
message: string,
public code: AdapterErrorCode,
public recoverable: boolean = false
) {
super(message);
this.name = 'AdapterError';
}
}
/**
* Adapter error codes
*/
export enum AdapterErrorCode {
// Environment errors
ENVIRONMENT_INVALID = 'ENVIRONMENT_INVALID',
EXECUTABLE_NOT_FOUND = 'EXECUTABLE_NOT_FOUND',
ADAPTER_NOT_INSTALLED = 'ADAPTER_NOT_INSTALLED',
INCOMPATIBLE_VERSION = 'INCOMPATIBLE_VERSION',
// Connection errors
CONNECTION_FAILED = 'CONNECTION_FAILED',
CONNECTION_TIMEOUT = 'CONNECTION_TIMEOUT',
CONNECTION_LOST = 'CONNECTION_LOST',
// Protocol errors
INVALID_RESPONSE = 'INVALID_RESPONSE',
UNSUPPORTED_OPERATION = 'UNSUPPORTED_OPERATION',
// Runtime errors
DEBUGGER_ERROR = 'DEBUGGER_ERROR',
SCRIPT_NOT_FOUND = 'SCRIPT_NOT_FOUND',
PERMISSION_DENIED = 'PERMISSION_DENIED',
// Generic errors
UNKNOWN_ERROR = 'UNKNOWN_ERROR'
}
// ===== Adapter Events =====
/**
* Events emitted by debug adapters
*/
export interface AdapterEvents {
// DAP events
'stopped': (event: DebugProtocol.StoppedEvent) => void;
'continued': (event: DebugProtocol.ContinuedEvent) => void;
'terminated': (event: DebugProtocol.TerminatedEvent) => void;
'exited': (event: DebugProtocol.ExitedEvent) => void;
'thread': (event: DebugProtocol.ThreadEvent) => void;
'output': (event: DebugProtocol.OutputEvent) => void;
'breakpoint': (event: DebugProtocol.BreakpointEvent) => void;
'module': (event: DebugProtocol.ModuleEvent) => void;
// Adapter lifecycle events
'initialized': () => void;
'connected': () => void;
'disconnected': () => void;
'error': (error: AdapterError) => void;
// State change events
'stateChanged': (oldState: AdapterState, newState: AdapterState) => void;
}
// ===== Migration Helpers =====
/**
* Configuration migration utilities
*/
export interface ConfigMigration {
/**
* Transform old Python-specific config to generic config
*/
migratePythonConfig(oldConfig: Record<string, unknown>): GenericLaunchConfig;
/**
* Check if a config needs migration
*/
needsMigration(config: Record<string, unknown>): boolean;
}