11/*********************************************************************
2- * Copyright (c) 2018 Ericsson and others
2+ * Copyright (c) 2018, 2023 Ericsson and others
33 *
44 * This program and the accompanying materials are made
55 * available under the terms of the Eclipse Public License 2.0
1010import * as cp from 'child_process' ;
1111import { DebugClient } from '@vscode/debugadapter-testsupport' ;
1212import { DebugProtocol } from '@vscode/debugprotocol' ;
13+ import * as path from 'path' ;
14+ import { defaultAdapter } from './utils' ;
15+ import * as os from 'os' ;
1316
1417export type ReverseRequestHandler <
1518 A = any ,
@@ -23,11 +26,83 @@ export interface ReverseRequestHandlers {
2326 > ;
2427}
2528
29+ function getAdapterAndArgs ( adapter ?: string ) : string [ ] {
30+ const chosenAdapter = adapter !== undefined ? adapter : defaultAdapter ;
31+ const adapterPath : string = path . join (
32+ __dirname ,
33+ '../../dist' ,
34+ chosenAdapter
35+ ) ;
36+ if ( process . env . INSPECT_DEBUG_ADAPTER ) {
37+ return [ '--inspect-brk' , adapterPath ] ;
38+ }
39+ return [ adapterPath ] ;
40+ }
41+
2642/**
27- * Extend the DebugClient to support Reverse Requests:
28- * https://microsoft.github.io/debug-adapter-protocol/specification#Reverse_Requests_RunInTerminal
43+ * Extend the standard DebugClient to support additional client features
2944 */
3045export class CdtDebugClient extends DebugClient {
46+ private _cdt_args : string [ ] ;
47+ private _cdt_adapterProcess ?: cp . ChildProcess ;
48+ constructor ( adapter ?: string , extraArgs ?: string [ ] ) {
49+ // The unused are as such because we do override process launching
50+ super ( 'unused' , 'unused' , 'gdb' ) ;
51+ this . _cdt_args = getAdapterAndArgs ( adapter ) ;
52+ if ( extraArgs ) {
53+ this . _cdt_args . push ( ...extraArgs ) ;
54+ }
55+ // These timeouts should match what is in .mocharc.json and .mocharc-windows-ci.json
56+ this . defaultTimeout = os . platform ( ) === 'win32' ? 25000 : 5000 ;
57+ }
58+
59+ /**
60+ * Start a debug session allowing command line arguments to be supplied
61+ */
62+ public start ( port ?: number ) : Promise < void > {
63+ if ( typeof port === 'number' ) {
64+ return super . start ( port ) ;
65+ }
66+
67+ return new Promise < void > ( ( resolve , reject ) => {
68+ this . _cdt_adapterProcess = cp . spawn ( 'node' , this . _cdt_args ) ;
69+ this . _cdt_adapterProcess . on ( 'error' , ( err ) => {
70+ console . log ( err ) ;
71+ reject ( err ) ;
72+ } ) ;
73+
74+ if (
75+ this . _cdt_adapterProcess . stdout === null ||
76+ this . _cdt_adapterProcess . stdin === null
77+ ) {
78+ reject ( 'Missing stdout/stdin' ) ;
79+ return ;
80+ }
81+ this . connect (
82+ this . _cdt_adapterProcess . stdout ,
83+ this . _cdt_adapterProcess . stdin
84+ ) ;
85+ resolve ( ) ;
86+ } ) ;
87+ }
88+
89+ public stop ( ) : Promise < void > {
90+ return super
91+ . stop ( )
92+ . then ( ( ) => {
93+ this . killAdapter ( ) ;
94+ } )
95+ . catch ( ( ) => {
96+ this . killAdapter ( ) ;
97+ } ) ;
98+ }
99+
100+ private killAdapter ( ) {
101+ if ( this . _cdt_adapterProcess ) {
102+ this . _cdt_adapterProcess . kill ( ) ;
103+ this . _cdt_adapterProcess = undefined ;
104+ }
105+ }
31106 /**
32107 * Reverse Request Handlers:
33108 */
0 commit comments