-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
1245 lines (1126 loc) · 36.3 KB
/
client.js
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** COPYRIGHT TEMPLATE */
/**
* The DA Client driver. This file contains no tests but is included by the test to be able to spawn
* a new mdb session. It emulates that of an Inline DA in VSCode (look at the extension `Midas` for examples)
*/
Error.stackTraceLimit = 5
const path = require('path')
const fs = require('fs')
const { spawn, ChildProcess } = require('child_process')
const EventEmitter = require('events')
const { assertLog, prettyJson, allUniqueVariableReferences, TestArgs, parseTestConfiguration } = require('./utils')
const net = require('net')
const { randomUUID } = require('crypto')
// Environment setup
const DRIVER_DIR = path.dirname(__filename)
const TEST_DIR = path.dirname(DRIVER_DIR)
const REPO_DIR = path.dirname(TEST_DIR)
let MDB_PATH = undefined
class RemoteService {
#host
#port
#server
constructor(service, host, port, binary, args = []) {
this.#server = service
this.#port = port
this.#host = host
process.on('exit', () => {
this.#server.kill('SIGTERM')
})
}
/** @returns {number} */
get port() {
return this.#port
}
/** @returns {ChildProcess} */
get serverProcess() {
return this.#server
}
get attachArgs() {
return {
type: 'gdbremote',
host: this.#host,
port: this.#port,
allstop: false,
}
}
}
/**
* @param {string} gdbserver
* @param {string} host
* @param {number} port
* @param {string} binary
* @param {string[]} args
* @returns { Promise<RemoteService> }
*/
async function createRemoteService(gdbserver, host, port, binary, args) {
port = Number.parseInt(port)
return new Promise((resolve, reject) => {
let serviceReadyEmitter = new EventEmitter()
if (args == undefined || args == null) {
args = []
}
console.log(`spawning gdbserver...`)
let service = spawn(gdbserver, ['--multi', `${host}:${port}`, binary, ...args], {
shell: true,
stdio: ['pipe', 'pipe', 'pipe'],
})
console.log('setup listeners')
process.stdout.on('data', (data) => {
console.log(`PROCESS STDOUT: ${data.toString()}`)
})
const waitUntilServiceReady = (data) => {
const str = data.toString()
console.log(`DATA: ${str}`)
if (str.includes('Listening on port')) {
console.log(`Ready to connect to remote service`)
serviceReadyEmitter.emit('init', true)
}
}
service.stdout.addListener('data', waitUntilServiceReady)
service.stderr.addListener('data', waitUntilServiceReady)
if (service == null) {
reject(`Failed to spawn service`)
}
serviceReadyEmitter.once('init', (done) => {
if (done) {
service.stderr.removeListener('data', waitUntilServiceReady)
service.stdout.removeListener('data', waitUntilServiceReady)
resolve(new RemoteService(service, host, port, binary, args))
} else {
reject(`Failed to initialize gdb server and wait until it starts listening on the port`)
}
})
})
}
function* randomSeqGenerator() {
const min = 10000
const max = 60000
const set = new Set()
while (true) {
let value = Math.floor(Math.random() * (max - min + 1)) + min
while (set.has(value)) {
value = Math.floor(Math.random() * (max - min + 1)) + min
}
set.add(value)
yield value
}
}
function stringToBool(str) {
if (typeof str !== 'string') {
throw new Error(`str must be of string type, but was ${typeof str}`)
}
const lowered = str.toLowerCase()
if (lowered === 'true') return true
else return false
}
function envVarBool(varName) {
const v = process.env[varName]
if (v != undefined) {
return stringToBool(v)
}
return false
}
function checkPortAvailability(maxtries = 20, host = 'localhost') {
const portListener = (p) => {
return new Promise((resolve, reject) => {
const server = net.createServer()
server.once('error', (err) => {
if (err.code === 'EADDRINUSE') {
resolve(false) // Port is in use
} else {
reject(err) // Some other error
}
})
server.once('listening', () => {
server.close()
resolve(true) // Port is available
})
server.listen(p, host)
})
}
return new Promise(async (resolve, reject) => {
let tries = 0
for (const port of randomSeqGenerator()) {
if (tries >= maxtries) {
reject('Max retry count hit')
}
const free = await portListener(port)
if (free) {
resolve(port)
} else {
tries += 1
}
}
})
}
/**
* @returns { { mdb: string, args: TestArgs } }
*/
function getExecutorArgs() {
if (process.argv.length < 4) {
throw new Error(
`Test executor not received the required parameters. It requires 3 parameters.\nWorking dir: the directory from which the test is executed in\nTest suite name: The test suite\nTest name: An optional test name, if only a single test in the suite should be executed. Usage:\nnode source/to/client.js current/working/dir testSuiteName testSuite`
)
}
const testArgs = new TestArgs(parseTestConfiguration(process.argv.slice(2)))
const config = {
mdb: testArgs.getBinary('mdb'),
args: testArgs,
}
return config
}
/**
* Splits `fileData` into lines and looks for what line `string_identifier` can be found on - returns the first line where it can be found.
* @param {string} fileData
* @param {string} string_identifier
*/
function getLineOf(fileData, string_identifier) {
let lineIdx = 1
for (const line of fileData.split('\n')) {
if (line.includes(string_identifier)) return lineIdx
lineIdx++
}
return null
}
function readFileContents(path) {
return fs.readFileSync(path).toString()
}
function unpackRecordArgs(param) {
const p = param.split(';')
const [recorder] = p.splice(0, 1)
return { path: recorder, args: p }
}
function unpackDebuggerArgs() {
if (process.env.hasOwnProperty('MDB')) {
const env = process.env['MDB']
console.log(`MDB=${env}`)
const params = env.split(';')
return params.flatMap((p) => p.split(' '))
} else {
return []
}
}
class Thread {
#client
id
name
constructor(client, threadId, name) {
this.#client = client
this.id = threadId
this.name = name
}
/**
* @param {number} timeout
* @returns {Promise<StackFrame[]>}
*/
async stacktrace(timeout) {
const response = await this.#client.sendReqGetResponse('stackTrace', { threadId: this.id }, timeout)
checkResponse(response, 'stackTrace', true, this.stackTrace)
return response.body.stackFrames.map((frame) => {
return new StackFrame(
this.#client,
frame.id,
frame.name,
frame.line,
frame.column,
frame.instructionPointerReference,
frame.source
)
})
}
}
/**
* @typedef { { variablesReference: number, name: string, value: string, type: string , evaluateName:string, namedVariables: number, indexedVariables: number, memoryReference: string } } DAPVariable
*/
class Variable {
/** @type {DAClient} */
#client
/** @type { DAPVariable } */
dap
/** @type { Variable[] } */
cache = null
/**
* @param {DAClient} client
* @param {DAPVariable} dap
*/
constructor(client, dap) {
this.#client = client
this.dap = dap
this.cache = null
}
/**
* @returns { Promise<Variable[]> }
*/
async variables(timeout = 1000) {
if (this.dap.variablesReference == 0) {
throw new Error(`This variable has id == 0 it can't make any requests! DAP Object: ${JSON.stringify(this.dap)}`)
}
if (this.cache != null) {
return this.cache
}
const {
success,
body: { variables },
} = await this.#client.sendReqGetResponse('variables', { variablesReference: this.id }, timeout)
assertLog(success, 'expected success from variables request')
this.cache = variables.map((el) => {
return new Variable(this.#client, el)
})
return this.cache
}
get memoryReference() {
return this.dap.memoryReference
}
get value() {
return this.dap.value
}
get name() {
return this.dap.name
}
get type() {
return this.dap.type
}
get id() {
return this.dap.variablesReference
}
get variablesReference() {
return this.dap.variablesReference
}
}
const ScopeNames = ['Arguments', 'Locals', 'Registers']
class StackFrame {
/** @type { DAClient } */
#client
/** @type { number } */
id
/** @type { string } */
name
/** @type { number } */
line
/** @type {string } */
instructionPointerReference
source
/**
* order of scopes is: [args, locals, registers]
* @type { number[] }
*/
scopes = null
constructor(client, id, name, line, column, instructionPointerReference, source) {
this.#client = client
this.id = id
this.name = name
this.line = line
this.column = column
this.instructionPointerReference = instructionPointerReference
this.source = source
this.scopes = null
}
/**
* @param {number} timeout
* @returns {Promise<Variable[]>}
*/
async locals(timeout = 1000) {
await this.get_scopes()
const {
success,
body: { variables },
} = await this.#client.sendReqGetResponse('variables', { variablesReference: this.scopes[1] }, timeout)
assertLog(success, 'expected success from variables request')
return variables.map((v) => {
return new Variable(this.#client, v)
})
}
/**
* @param {number} timeout
* @returns { Promise<Variable[]> }
*/
async args(timeout) {
await this.get_scopes()
const {
success,
body: { variables },
} = await this.#client.sendReqGetResponse('variables', { variablesReference: this.scopes[0] }, timeout)
assertLog(success, 'expected success from variables request')
return variables.map((v) => {
return new Variable(this.#client, v)
})
}
async get_scopes() {
if (this.scopes !== null) {
return
}
const {
success,
body: { scopes },
} = await this.#client.sendReqGetResponse('scopes', { frameId: this.id }, 1000)
assertLog(success, `Scopes retrieved for ${this.id}`)
assertLog(scopes.length == ScopeNames.length, `Got ${ScopeNames.length} scopes`, `Failed, got: ${scopes.length}`)
this.scopes = []
for (let i = 0; i < ScopeNames.length; i++) {
this.scopes.push(scopes[i].variablesReference)
assertLog(scopes[i].name == ScopeNames[i], `Expected scope name ${ScopeNames[i]}`, ` but was ${scopes[i].name}`)
}
}
}
const regex = /Content-Length: (\d+)\s{4}/gm
class DAClient {
/** @type {EventEmitter} */
send_wait_res
/** @type {EventEmitter} */
events
/** @type {number} - Current request number */
seq
/** The MDB process */
mdb
/** @type { {next_packet_length: number, receive_buffer: string } } Parsed stdout contents */
buf
/** @type { RemoteService } */
remoteService = null
/** @type { { mdb: string, args: TestArgs } } config */
config
isRemoteSession() {
return this.remoteService !== null
}
static async CreateRemoteServer(testConfig, program, args) {
const remoteServerBinaryPath = testConfig.args.getServerBinary()
const host = 'localhost'
let remoteService = await checkPortAvailability(20, host).then((port) => {
return createRemoteService(remoteServerBinaryPath, host, port, program, args)
})
if (remoteService == null) {
throw new Error(`Failed to spawn GDB Server on ${host}:${port}`)
}
return remoteService
}
constructor(mdb, mdb_args, config) {
// for future work when we get recording in tests suites working.
this.config = config
// For now we don't support multi-process testing. That is only testable manually.
this.processId = 0
try {
this.recording = process.env.hasOwnProperty('REC')
if (this.recording) {
const parsed = unpackRecordArgs(process.env['REC'])
const { path, args } = parsed
console.log(`${JSON.stringify(parsed)}`)
console.log(`Recording using ${process.env['REC']}`)
let mdb_recorded_arg = ['-r']
const cfg = unpackDebuggerArgs()
if (!cfg.some((v) => v == '-t')) {
mdb_recorded_arg.push('-t', 2)
}
const test_spawn_args = ['record', ...args, mdb, ...mdb_recorded_arg, ...cfg]
console.log(`Spawning test with: ${path} ${test_spawn_args.join(' ')}`)
this.mdb = spawn(path, test_spawn_args)
} else {
let config_args = unpackDebuggerArgs()
// if no thread pool size is configured, set it to 12. MDB will attempt to automatically set it to half available threads otherwise
if (!config_args.some((v) => v == '-t')) {
config_args.push('-t', 12)
}
console.log(`Spawning test with: ${mdb} ${config_args.join(' ')}`)
this.mdb = spawn(mdb, [...config_args], {
shell: true,
stdio: 'pipe',
})
}
} catch (ex) {
console.log(`failed to spawn mdb: ${ex}`)
}
this.mdb.on('error', (err) => {
console.error(`[TEST FAILED] MDB error: ${err}`)
process.exit(-1)
})
this.mdb.on('exit', (exitCode) => {
if (exitCode != 0) {
console.error(`[TEST FAILED] MDB panicked or terminated with exit code ${exitCode}`)
process.exit(-1)
}
})
process.on('exit', (code) => {
this.mdb.kill('SIGTERM')
if (code != 0) {
dump_log(this.config.args.test)
}
})
this.seq = 1
this.send_wait_res = new EventEmitter()
this.events = new EventEmitter()
this.buf = {
next_packet_length: null,
receive_buffer: '',
}
// Emit processed DAP Events to this event handler
this.events.on('event', async (evt) => {
const { event, body } = evt
this.events.emit(event, body)
})
// Emit processed DAP Responses to this event handler
this.events.on('response', (response) => {
this.send_wait_res.emit(response.command, response)
})
this.mdb.stdout.on('data', (data) => {
const str_data = data.toString()
this.appendBuffer(str_data)
let msgs = this.parseContents(this.buf.receive_buffer)
let last_ends = 0
for (const { content_start, length } of msgs.filter((i) => i.all_received)) {
const end = content_start + length
const data = this.buf.receive_buffer.slice(content_start, end)
try {
const json = JSON.parse(data)
if (!this.events.emit(json.type, json)) {
this.events.emit('err', json)
}
last_ends = content_start + length
} catch (ex) {
console.log(`Buffer contents: '''${this.buf.receive_buffer}'''`)
console.log(`Exception: ${ex}`)
process.exit(-1)
}
}
this.buf.receive_buffer = this.buf.receive_buffer.slice(last_ends)
})
}
requestedUseOfRemote() {
return this.config.remote
}
buildDirFile(fileName) {
return this.config.args.getBinary(fileName)
}
async setBreakpoints(filePath, lines) {
const bp_lines = lines.map((l) => ({ line: l }))
const args = {
source: {
name: repoDirFile(filePath),
path: repoDirFile(filePath),
},
breakpoints: bp_lines,
}
const res = await this.sendReqGetResponse('setBreakpoints', args)
const {
success,
body: { breakpoints },
} = res
assertLog(success, `expected bp request of ${JSON.stringify(bp_lines)} to succeed`, `. It failed`)
assertLog(
breakpoints.length == lines.length,
`Expected ${lines.length} breakpoints`,
`Failed to set ${lines.length} breakpoints. Response: \n${prettyJson(res)}`
)
return breakpoints
}
serializeRequest(processId, req, args = {}) {
const json = {
seq: this.seq,
type: 'request',
processId: processId,
command: req,
arguments: args,
}
this.seq += 1
const data = JSON.stringify(json)
const length = data.length
const res = `Content-Length: ${length}\r\n\r\n${data}`
return res
}
/**
* @returns {Promise<Thread[]>} timeout
*/
async getThreads(timeout) {
const threads = await this.sendReqGetResponse('threads', {}, timeout).then((res) => {
if (!res.success) {
throw new Error('Failed to get threads')
}
return res.body.threads
})
return threads.map((thread) => {
return new Thread(this, thread.id, thread.name)
})
}
/**
* @returns {Promise<{id: number, name: string}[]>}
*/
async threads(timeout = seconds(1)) {
return this.sendReqGetResponse('threads', {}, timeout)
.then((res) => {
return res.body.threads
})
.catch(testException)
}
/* Called _before_ an action that is expected to create an event.
* Calling this after, may or may not work, as the event handler might not be set up in time,
* before the actual event comes across the wire.*/
prepareWaitForEvent(evt) {
return new Promise((res, rej) => {
this.events.once(evt, (body) => {
res(body)
})
})
}
/* Called _before_ an action that is expected to create an event.
* Calling this after, may or may not work, as the event handler might not be set up in time,
* before the actual event comes across the wire.*/
prepareWaitForEventN(evt, n, timeout, fn = this.prepareWaitForEventN) {
const ctrl = new AbortController()
const signal = ctrl.signal
const timeOut = setTimeout(() => {
ctrl.abort()
}, timeout)
let eventCount = 0
// we create the exception object here, to report decent stack traces for when it actually does fail.
const err = new Error('Timed out')
Error.captureStackTrace(err, fn)
let p = new Promise((res, rej) => {
let evts = []
const listener = (body) => {
eventCount++
evts.push(body)
if (evts.length == n) {
this.events.removeListener(evt, listener)
res(evts)
}
}
this.events.on(evt, listener)
})
return Promise.race([
p.then((res) => {
clearTimeout(timeOut)
return res
}),
new Promise((_, rej) => {
signal.addEventListener('abort', () => {
err.message = `Timed out (${timeout}ms threshold crossed): Waiting for ${n} events of type ${evt} to have happened (but saw ${eventCount})`
rej(err)
})
}),
])
}
_sendReqGetResponseImpl(processId, req, args) {
return new Promise((res) => {
const serialized = this.serializeRequest(processId, req, args)
this.send_wait_res.once(req, (response) => {
res(response)
})
this.mdb.stdin.write(serialized)
})
}
/**
* @typedef {{response_seq: number, type: string, success: boolean, command: string, body: object}} Response
*
* @param { string } req - the request "command"
* @param { object } args - request's arguments, as per the DAP spec: https://microsoft.github.io/debug-adapter-protocol/specification
* @param { number } failureTimeout - The maximum time (in milliseconds) that we should wait for response. If the request takes longer, the test will fail.
* @returns { Promise<Response> } - Returns a promise that resolves to the response to the `req` command.
*/
async sendReqGetResponse(req, args, failureTimeout = seconds(1), fn = this.sendReqGetResponse) {
const ctrl = new AbortController()
const signal = ctrl.signal
const req_promise = this._sendReqGetResponseImpl(this.processId, req, args)
// we create the exception object here, to report decent stack traces for when it actually does fail.
const err = new Error('Timed out')
Error.captureStackTrace(err, fn)
const timeOut = setTimeout(() => {
ctrl.abort()
}, failureTimeout)
return Promise.race([
req_promise.then((res) => {
clearTimeout(timeOut)
return res
}),
new Promise((_, rej) => {
signal.addEventListener('abort', () => {
err.message = `Timed out (${failureTimeout} milliseconds threshold crossed) waiting for response from request ${req}`
rej(err)
})
}),
])
}
/**
* @typedef {{ id: number, name: string, source: {name: string, path: string}, line: number, column: number, instructionPointerReference: string}} StackFrame
* @param { number } threadId
* @returns {Promise<{response_seq: number, type: string, success: boolean, command: string, body: { stackFrames: StackFrame[] }}>}
*/
async stackTrace(threadId, timeout = 1000) {
threadId = threadId != null ? threadId : await this.getAnyThreadId()
const response = await this.sendReqGetResponse('stackTrace', { threadId: threadId }, timeout)
checkResponse(response, 'stackTrace', true, this.stackTrace)
return response
}
async getAnyThreadId() {
const thrs = await this.threads()
return thrs[0].id
}
flushConnection() {
this.mdb.stdin.write('----\n')
}
/**
* @param {string} contents
* @returns {{ content_start: number, length: number, all_received: boolean }[]}
*/
parseContents(contents) {
let m
const result = []
while ((m = regex.exec(contents)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++
}
// The result can be accessed through the `m`-variable.
let contents_start = 0
m.forEach((match, groupIndex) => {
if (groupIndex == 0) {
contents_start = m.index + match.length
}
if (groupIndex == 1) {
const len = Number.parseInt(match)
const all_received = contents_start + len <= contents.length
result.push({
content_start: contents_start,
length: len,
all_received,
})
}
})
}
return result
}
appendBuffer(data) {
this.buf.receive_buffer = this.buf.receive_buffer.concat(data)
}
/**
* Starts a debug session in a similar fashion that what happens in a VSCode setting.
* `configurationRoutine` can be whatever we want during testing, in the VSCode environment
* this is where
*
* @param {string} startType
* @param {object} startArguments
* @param {number} timeout
* @param {function} configurationRoutine
*/
async startSession(startType, startArguments, timeout, configurationRoutine) {
if (this.sessionId == null) {
this.sessionId = randomUUID()
}
if (startArguments.sessionId == null) {
startArguments.sessionId = this.sessionId
}
let initEventWaitable = this.prepareWaitForEvent('initialized').then(async (initEvent) => {
const { processId, sessionId } = initEvent
if (!processId || !sessionId) {
throw new Error(`Expected init event to contain process id(${processId}) and sessionId(${sessionId})`)
}
this.processId = processId
if (configurationRoutine) {
await configurationRoutine()
}
await this.sendReqGetResponse('configurationDone', {}, timeout)
})
// the init event is fired (it doesn't fire before) after attach/launch has
// started, and configurationDone is sent _before_ launch/attach responds
console.log(`${startType}ing using arguments: ${JSON.stringify(startArguments)}`)
let sessionRes = this.sendReqGetResponse(startType, startArguments, timeout)
await initEventWaitable
console.log(`Init completed`)
const sessionStartResponse = await sessionRes
console.log(`Attach completed`)
checkResponse(sessionStartResponse, startType, true)
}
async #startRunToMainNative(launchArgs, timeout) {
let stopped_promise = this.prepareWaitForEventN('stopped', 1, timeout, this.#startRunToMainNative)
await this.startSession(
'launch',
{
program: launchArgs['program'],
stopOnEntry: launchArgs['stopOnEntry'],
sessionId: this.sessionId,
},
1000,
() => console.log(`configuration completed`)
)
return stopped_promise
}
async #startRunToMainRemote(program, args, timeout) {
this.remoteService = await DAClient.CreateRemoteServer(this.config, program, args)
if (this.sessionId == null) {
this.sessionId = randomUUID()
}
let hit_main_stopped_promise = this.prepareWaitForEventN('stopped', 1, timeout, this.#startRunToMainRemote)
let attachArguments = this.remoteService.attachArgs
attachArguments['RRSession'] = false
attachArguments['sessionId'] = this.sessionId
console.log(`attach arguments: ${prettyJson(attachArguments)}`)
await this.startSession('attach', attachArguments, timeout, async () => {
const functions = ['main'].map((n) => ({ name: n }))
const fnBreakpointResponse = await this.sendReqGetResponse('setFunctionBreakpoints', {
breakpoints: functions,
})
assertLog(
fnBreakpointResponse.success,
'Function breakpoints request',
`Response failed with contents: ${JSON.stringify(fnBreakpointResponse)}`
)
assertLog(
fnBreakpointResponse.body.breakpoints.length == 1,
'Expected 1 breakpoint returned',
`But received ${JSON.stringify(fnBreakpointResponse.body.breakpoints)}`
)
console.log(`${prettyJson(fnBreakpointResponse)}`)
console.log(`configuration sequence done, waiting for attach response...`)
})
return hit_main_stopped_promise
}
async startRunToMain(program, args = [], timeout = seconds(1)) {
if (this.sessionId == null) {
this.sessionId = randomUUID()
}
let init_res = await this.sendReqGetResponse('initialize', { sessionId: this.sessionId }, timeout)
checkResponse(init_res, 'initialize', true)
switch (this.config.args.getArg('session')) {
case 'remote': {
await this.#startRunToMainRemote(program, args, timeout)
const threads = await this.threads()
console.log(`threads: ${JSON.stringify(threads)}`)
await this.contNextStop(threads[0].id)
}
case 'native': {
return await this.#startRunToMainNative(
{
program: program,
stopOnEntry: true,
},
timeout
)
}
default:
throw new Error(`Unknown session kind`)
}
}
// utility function to initialize, launch `program` and run to `main`
async launchToMain(program, timeout = seconds(1)) {
let stopped_promise = this.prepareWaitForEventN('stopped', 1, timeout, this.launchToMain)
await this.startSession(
'launch',
{
program: program,
stopOnEntry: true,
},
timeout,
() => console.log('configuration done')
)
await stopped_promise
}
async remoteAttach(attachArgs, init, timeout = seconds(1)) {
let stopped_promise = this.prepareWaitForEventN('stopped', 1, timeout, this.remoteAttach)
if (init) {
let init_res = await this.sendReqGetResponse('initialize', {}, timeout)
checkResponse(init_res, 'initialize', true)
}
console.log(`attach args: ${JSON.stringify(attachArgs)}`)
let attach_res = await this.sendReqGetResponse(
'attach',
{
type: attachArgs['type'] ?? 'gdbremote',
host: attachArgs['host'],
port: attachArgs['port'],
},
timeout
)
checkResponse(attach_res, 'attach', true)
await this.sendReqGetResponse('configurationDone', {}, timeout)
await stopped_promise
}
async setInsBreakpoint(addr) {
return this.sendReqGetResponse(
'setInstructionBreakpoints',
{
breakpoints: [{ instructionReference: addr }],
},
1000,
this.setInsBreakpoint
)
}
async contNextStop(threadId, timeout = 1000) {
if (threadId == null) {
const thrs = await this.threads()
threadId = thrs[0].id
}
let stopped_promise = this.prepareWaitForEventN('stopped', 1, timeout, this.contNextStop)
await this.sendReqGetResponse(
'continue',
{
threadId: threadId,
singleThread: false,
},
timeout,
this.contNextStop
)
return await stopped_promise
}
/**
* @param { "terminate" | "suspend" } kind
* @param { number } timeout
* @returns
*/
async disconnect(kind = 'terminateDebuggee', timeout = 1000) {
switch (kind) {
case 'terminate':
return this.sendReqGetResponse('disconnect', {
terminateDebuggee: true,
})
case 'suspend':
return this.sendReqGetResponse('disconnect', {
suspendDebuggee: true,
})
}
}
/**
*
* @param {string} req
* @param {object} args
* @param {string} event
* @param {number} failureTimeout
* @returns {Promise<{ event_body: object, response: object }>}
*/
async sendReqWaitEvent(req, args, event, failureTimeout, fn = this.sendReqWaitEvent) {
const ctrl = new AbortController()
const signal = ctrl.signal
const err = new Error('Timed out')
Error.captureStackTrace(err, fn)
const event_promise = this.prepareWaitForEvent(event)
const response = await this.sendReqGetResponse(req, args, failureTimeout, fn)
const timeOut = setTimeout(() => {
ctrl.abort()
}, failureTimeout)
return Promise.race([
event_promise.then((event_body) => {
clearTimeout(timeOut)
return { event_body, response }
}),