forked from googleapis/cloud-debug-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebuglet.ts
1269 lines (1152 loc) · 38.7 KB
/
debuglet.ts
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 2014 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as assert from 'assert';
import * as consoleLogLevel from 'console-log-level';
import * as crypto from 'crypto';
import {EventEmitter} from 'events';
import * as extend from 'extend';
import * as fs from 'fs';
import * as metadata from 'gcp-metadata';
import * as path from 'path';
import * as util from 'util';
import {Debug, PackageInfo} from '../client/stackdriver/debug';
import {StatusMessage} from '../client/stackdriver/status-message';
import {CanaryMode, Debuggee, DebuggeeProperties} from '../debuggee';
import * as stackdriver from '../types/stackdriver';
import {defaultConfig} from './config';
import {
DebugAgentConfig,
Logger,
LogLevel,
ResolvedDebugAgentConfig,
} from './config';
import {Controller} from './controller';
import * as scanner from './io/scanner';
import * as SourceMapper from './io/sourcemapper';
import * as utils from './util/utils';
import * as debugapi from './v8/debugapi';
import {DebugApi} from './v8/debugapi';
const readFilep = util.promisify(fs.readFile);
const ALLOW_EXPRESSIONS_MESSAGE =
'Expressions and conditions are not allowed' +
' by default. Please set the allowExpressions configuration option to true.' +
' See the debug agent documentation at https://goo.gl/ShSm6r.';
const NODE_VERSION_MESSAGE =
'Node.js version not supported. Node.js 5.2.0 and ' +
'versions older than 0.12 are not supported.';
const NODE_10_CIRC_REF_MESSAGE =
'capture.maxDataSize=0 is not recommended on older versions of Node 10/11' +
' and Node 12.' +
' See https://github.com/googleapis/cloud-debug-nodejs/issues/516 for more' +
' information.';
const BREAKPOINT_ACTION_MESSAGE =
'The only currently supported breakpoint actions' + ' are CAPTURE and LOG.';
// PROMISE_RESOLVE_CUT_OFF_IN_MILLISECONDS is a heuristic duration that we set
// to force the debug agent to return a new promise for isReady. The value is
// the average of Stackdriver debugger hanging get duration (40s) and TCP
// time-out on GCF (540s).
const PROMISE_RESOLVE_CUT_OFF_IN_MILLISECONDS = ((40 + 540) / 2) * 1000;
interface SourceContext {
[key: string]: string;
}
/**
* Environments that this system might be running in.
* Helps provide platform-specific information and integration.
*/
export enum Platforms {
/** Google Cloud Functions */
CLOUD_FUNCTION = 'cloud_function',
/** Any other platform. */
DEFAULT = 'default',
}
/**
* Formats a breakpoint object prefixed with a provided message as a string
* intended for logging.
* @param {string} msg The message that prefixes the formatted breakpoint.
* @param {Breakpoint} breakpoint The breakpoint to format.
* @return {string} A formatted string.
*/
const formatBreakpoint = (
msg: string,
breakpoint: stackdriver.Breakpoint
): string => {
let text =
msg +
util.format(
'breakpoint id: %s,\n\tlocation: %s',
breakpoint.id,
util.inspect(breakpoint.location)
);
if (breakpoint.createdTime) {
const unixTime = Number(breakpoint.createdTime.seconds);
const date = new Date(unixTime * 1000); // to milliseconds.
text += '\n\tcreatedTime: ' + date.toString();
}
if (breakpoint.condition) {
text += '\n\tcondition: ' + util.inspect(breakpoint.condition);
}
if (breakpoint.expressions) {
text += '\n\texpressions: ' + util.inspect(breakpoint.expressions);
}
return text;
};
/**
* Formats a map of breakpoint objects prefixed with a provided message as a
* string intended for logging.
* @param {string} msg The message that prefixes the formatted breakpoint.
* @param {Object.<string, Breakpoint>} breakpoints A map of breakpoints.
* @return {string} A formatted string.
*/
const formatBreakpoints = (
msg: string,
breakpoints: {[key: string]: stackdriver.Breakpoint}
): string => {
return (
msg +
Object.keys(breakpoints)
.map(b => {
return formatBreakpoint('', breakpoints[b]);
})
.join('\n')
);
};
/**
* CachedPromise stores a promise. This promise can be resolved by calling
* function resolve() and can only be resolved once.
*/
export class CachedPromise {
private promiseResolve: (() => void) | null = null;
private promise: Promise<void> = new Promise<void>(resolve => {
this.promiseResolve = resolve;
});
get(): Promise<void> {
return this.promise;
}
resolve(): void {
// Each promise can be resolved only once.
if (this.promiseResolve) {
this.promiseResolve();
this.promiseResolve = null;
}
}
}
/**
* IsReady will return a promise to user after user starting the debug agent.
* This promise will be resolved when one of the following is true:
* 1. Time since last listBreakpoint was within a heuristic time.
* 2. listBreakpoint completed successfully.
* 3. Debuggee registration expired or failed, listBreakpoint cannot be
* completed.
*/
export interface IsReady {
isReady(): Promise<void>;
}
/**
* IsReadyManager is a wrapper class to use debuglet.isReady().
*/
class IsReadyImpl implements IsReady {
constructor(private debuglet: Debuglet) {}
isReady(): Promise<void> {
return this.debuglet.isReady();
}
}
export interface FindFilesResult {
jsStats: scanner.ScanStats;
mapFiles: string[];
errors: Map<string, Error>;
hash: string;
}
export class Debuglet extends EventEmitter {
private debug: Debug;
private v8debug: DebugApi | null;
private running: boolean;
private project: string | null;
private controller: Controller;
private completedBreakpointMap: {[key: string]: boolean};
// breakpointFetchedTimestamp represents the last timestamp when
// breakpointFetched was resolved, which means breakpoint update was
// successful.
private breakpointFetchedTimestamp: number;
// breakpointFetched is a CachedPromise only to be resolved after breakpoint
// fetch was successful. Its stored promise will be returned by isReady().
private breakpointFetched: CachedPromise | null;
// debuggeeRegistered is a CachedPromise only to be resolved after debuggee
// registration was successful.
private debuggeeRegistered: CachedPromise;
isReadyManager: IsReady = new IsReadyImpl(this);
// Exposed for testing
config: ResolvedDebugAgentConfig;
fetcherActive: boolean;
logger: Logger;
debuggee: Debuggee | null;
activeBreakpointMap: {[key: string]: stackdriver.Breakpoint};
/**
* @param {Debug} debug - A Debug instance.
* @param {object=} config - The option parameters for the Debuglet.
* @event 'started' once the startup tasks are completed. Only called once.
* @event 'stopped' if the agent stops due to a fatal error after starting.
* Only called once.
* @event 'registered' once successfully registered to the debug api. May be
* emitted multiple times.
* @event 'remotelyDisabled' if the debuggee is disabled by the server. May be
* called multiple times.
* @constructor
*/
constructor(debug: Debug, config: DebugAgentConfig) {
super();
/** @private {object} */
this.config = Debuglet.normalizeConfig_(config);
/** @private {Debug} */
this.debug = debug;
/**
* @private {object} V8 Debug API. This can be null if the Node.js version
* is out of date.
*/
this.v8debug = null;
/** @private {boolean} */
this.running = false;
/** @private {string} */
this.project = null;
/** @private {boolean} */
this.fetcherActive = false;
/** @private */
this.logger = consoleLogLevel({
stderr: true,
prefix: this.debug.packageInfo.name,
level: Debuglet.logLevelToName(this.config.logLevel),
});
/** @private {DebugletApi} */
this.controller = new Controller(this.debug, {apiUrl: config.apiUrl});
/** @private {Debuggee} */
this.debuggee = null;
/** @private {Object.<string, Breakpoint>} */
this.activeBreakpointMap = {};
/** @private {Object.<string, Boolean>} */
this.completedBreakpointMap = {};
this.breakpointFetched = null;
this.breakpointFetchedTimestamp = -Infinity;
this.debuggeeRegistered = new CachedPromise();
}
static LEVELNAMES: LogLevel[] = [
'fatal',
'error',
'warn',
'info',
'debug',
'trace',
];
// The return type `LogLevel` is used instead of
// `consoleLogLevel.LogLevelNames` because, otherwise,
// the `consoleLogLevel.LogLevelNames` type is exposed to
// users of the debug agent, requiring them to have
// @types/console-log-level installed to compile their code.
static logLevelToName(level: number): LogLevel {
if (typeof level === 'string') {
level = Number(level);
}
if (typeof level !== 'number') {
level = defaultConfig.logLevel;
}
if (level < 0) level = 0;
if (level > 4) level = 4;
return Debuglet.LEVELNAMES[level];
}
static normalizeConfig_(config: DebugAgentConfig): ResolvedDebugAgentConfig {
const envConfig = {
logLevel: process.env.GCLOUD_DEBUG_LOGLEVEL,
serviceContext: {
service:
process.env.GAE_SERVICE ||
process.env.GAE_MODULE_NAME ||
process.env.K_SERVICE,
version:
process.env.GAE_VERSION ||
process.env.GAE_MODULE_VERSION ||
process.env.K_REVISION,
minorVersion_:
process.env.GAE_DEPLOYMENT_ID || process.env.GAE_MINOR_VERSION,
},
};
if (process.env.FUNCTION_NAME) {
envConfig.serviceContext.service = process.env.FUNCTION_NAME;
envConfig.serviceContext.version = 'unversioned';
}
return extend(true, {}, defaultConfig, config, envConfig);
}
static buildRegExp(fileExtensions: string[]): RegExp {
return new RegExp(fileExtensions.map(f => f + '$').join('|'));
}
static async findFiles(
config: ResolvedDebugAgentConfig,
precomputedHash?: string
): Promise<FindFilesResult> {
const baseDir = config.workingDirectory;
const fileStats = await scanner.scan(
baseDir,
Debuglet.buildRegExp(config.javascriptFileExtensions.concat('js.map')),
precomputedHash
);
const jsStats = fileStats.selectStats(
Debuglet.buildRegExp(config.javascriptFileExtensions)
);
const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd());
const errors = fileStats.errors();
return {jsStats, mapFiles, errors, hash: fileStats.hash};
}
/**
* Starts the Debuglet. It is important that this is as quick as possible
* as it is on the critical path of application startup.
* @private
*/
async start(): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
const stat = util.promisify(fs.stat);
try {
await stat(path.join(that.config.workingDirectory, 'package.json'));
} catch (err) {
that.logger.error('No package.json located in working directory.');
that.emit('initError', new Error('No package.json found.'));
return;
}
const workingDir = that.config.workingDirectory;
// Don't continue if the working directory is a root directory
// unless the user wants to force using the root directory
if (
!that.config.allowRootAsWorkingDirectory &&
path.join(workingDir, '..') === workingDir
) {
const message =
'The working directory is a root directory. Disabling ' +
'to avoid a scan of the entire filesystem for JavaScript files. ' +
'Use config `allowRootAsWorkingDirectory` if you really want to ' +
'do this.';
that.logger.error(message);
that.emit('initError', new Error(message));
return;
}
let gaeId: string | undefined;
if (process.env.GAE_MINOR_VERSION) {
gaeId = 'GAE-' + process.env.GAE_MINOR_VERSION;
}
let findResults: FindFilesResult;
try {
findResults = await Debuglet.findFiles(that.config, gaeId);
findResults.errors.forEach(that.logger.warn);
} catch (err) {
that.logger.error('Error scanning the filesystem.', err);
that.emit('initError', err);
return;
}
let mapper;
try {
mapper = await SourceMapper.create(findResults.mapFiles, that.logger);
} catch (err3) {
that.logger.error('Error processing the sourcemaps.', err3);
that.emit('initError', err3);
return;
}
that.v8debug = debugapi.create(
that.logger,
that.config,
findResults.jsStats,
mapper
);
const id: string = gaeId || findResults.hash;
that.logger.info('Unique ID for this Application: ' + id);
let onGCP: boolean;
try {
onGCP = await Debuglet.runningOnGCP();
} catch (err) {
that.logger.warn(
'Unexpected error detecting GCE metadata service: ' + err.message
);
// Continue, assuming not on GCP.
onGCP = false;
}
let project: string;
try {
project = await that.debug.authClient.getProjectId();
} catch (err) {
that.logger.error(
'The project ID could not be determined: ' + err.message
);
that.emit('initError', err);
return;
}
if (
onGCP &&
(!that.config.serviceContext || !that.config.serviceContext.service)
) {
// If on GCP, check if the clusterName instance attribute is availble.
// Use this as the service context for better service identification on
// GKE.
try {
const clusterName = await Debuglet.getClusterNameFromMetadata();
that.config.serviceContext = {
service: clusterName,
version: 'unversioned',
minorVersion_: undefined,
};
} catch (err) {
/* we are not running on GKE - Ignore error. */
}
}
let sourceContext;
try {
sourceContext =
(that.config.sourceContext as {} as SourceContext) ||
(await Debuglet.getSourceContextFromFile());
} catch (err5) {
that.logger.warn('Unable to discover source context', err5);
// This is ignorable.
}
if (
this.config.capture &&
this.config.capture.maxDataSize === 0 &&
utils.satisfies(process.version, '>=10 <10.15.3 || >=11 <11.7 || >=12')
) {
that.logger.warn(NODE_10_CIRC_REF_MESSAGE);
}
const platform = Debuglet.getPlatform();
let region: string | undefined;
if (platform === Platforms.CLOUD_FUNCTION) {
region = await Debuglet.getRegion();
}
// We can register as a debuggee now.
that.logger.debug('Starting debuggee, project', project);
that.running = true;
that.project = project;
that.debuggee = Debuglet.createDebuggee(
project,
id,
that.config.serviceContext,
sourceContext,
onGCP,
that.debug.packageInfo,
platform,
that.config.description,
/*errorMessage=*/ undefined,
region
);
that.scheduleRegistration_(0 /* immediately */);
that.emit('started');
}
/**
* isReady returns a promise that only resolved if the last breakpoint update
* happend within a duration (PROMISE_RESOLVE_CUT_OFF_IN_MILLISECONDS). This
* feature is mainly used in Google Cloud Function (GCF), as it is a
* serverless environment and we wanted to make sure debug agent always
* captures the snapshots.
*/
isReady(): Promise<void> {
if (
Date.now() <
this.breakpointFetchedTimestamp + PROMISE_RESOLVE_CUT_OFF_IN_MILLISECONDS
) {
return Promise.resolve();
} else {
if (this.breakpointFetched) return this.breakpointFetched.get();
this.breakpointFetched = new CachedPromise();
this.debuggeeRegistered.get().then(() => {
this.scheduleBreakpointFetch_(
0 /*immediately*/,
true /*only fetch once*/
);
});
return this.breakpointFetched.get();
}
}
/**
* @private
*/
// TODO: Determine the type of sourceContext
static createDebuggee(
projectId: string,
uid: string,
serviceContext: {
service?: string;
version?: string;
minorVersion_?: string;
enableCanary?: boolean;
allowCanaryOverride?: boolean;
},
sourceContext: SourceContext | undefined,
onGCP: boolean,
packageInfo: PackageInfo,
platform: string,
description?: string,
errorMessage?: string,
region?: string
): Debuggee {
const cwd = process.cwd();
const mainScript = path.relative(cwd, process.argv[1]);
const version =
'google.com/node-' +
(onGCP ? 'gcp' : 'standalone') +
'/v' +
packageInfo.version;
let desc = process.title + ' ' + mainScript;
const labels: {[key: string]: string} = {
'main script': mainScript,
'process.title': process.title,
'node version': process.versions.node,
'V8 version': process.versions.v8,
'agent.name': packageInfo.name,
'agent.version': packageInfo.version,
projectid: projectId,
platform,
};
if (region) {
labels.region = region;
}
if (serviceContext) {
if (
typeof serviceContext.service === 'string' &&
serviceContext.service !== 'default'
) {
// As per app-engine-ids, the module label is not reported
// when it happens to be 'default'.
labels.module = serviceContext.service;
desc += ' module:' + serviceContext.service;
}
if (typeof serviceContext.version === 'string') {
labels.version = serviceContext.version;
desc += ' version:' + serviceContext.version;
}
if (typeof serviceContext.minorVersion_ === 'string') {
// v--- intentional lowercase
labels.minorversion = serviceContext.minorVersion_;
}
}
if (region) {
desc += ' region:' + region;
}
if (!description && process.env.FUNCTION_NAME) {
description = 'Function: ' + process.env.FUNCTION_NAME;
}
if (description) {
desc += ' description:' + description;
}
const uniquifier = Debuglet._createUniquifier(
desc,
version,
uid,
sourceContext,
labels
);
const statusMessage = errorMessage
? new StatusMessage(StatusMessage.UNSPECIFIED, errorMessage, true)
: undefined;
const properties: DebuggeeProperties = {
project: projectId,
uniquifier,
description: desc,
agentVersion: version,
labels,
statusMessage,
packageInfo,
canaryMode: Debuglet._getCanaryMode(serviceContext),
};
if (sourceContext) {
properties.sourceContexts = [sourceContext];
}
return new Debuggee(properties);
}
/**
* Use environment vars to infer the current platform.
* For now this is only Cloud Functions and other.
*/
static getPlatform(): Platforms {
const {FUNCTION_NAME, FUNCTION_TARGET} = process.env;
// (In theory) only the Google Cloud Functions environment will have these env vars.
if (FUNCTION_NAME || FUNCTION_TARGET) {
return Platforms.CLOUD_FUNCTION;
}
return Platforms.DEFAULT;
}
static runningOnGCP(): Promise<boolean> {
return metadata.isAvailable();
}
static async getClusterNameFromMetadata(): Promise<string> {
return (await metadata.instance('attributes/cluster-name')).data as string;
}
/**
* Returns the region from environment varaible if available.
* Otherwise, returns the region from the metadata service.
* If metadata is not available, returns undefined.
*/
static async getRegion(): Promise<string | undefined> {
if (process.env.FUNCTION_REGION) {
return process.env.FUNCTION_REGION;
}
try {
// Example returned region format: /process/1234567/us-central
const segments = ((await metadata.instance('region')) as string).split(
'/'
);
return segments[segments.length - 1];
} catch (err) {
return undefined;
}
}
static async getSourceContextFromFile(): Promise<SourceContext> {
// If read errors, the error gets thrown to the caller.
const contents = await readFilep('source-context.json', 'utf8');
try {
return JSON.parse(contents);
} catch (e) {
throw new Error('Malformed source-context.json file: ' + e);
}
}
/**
* @param {number} seconds
* @private
*/
scheduleRegistration_(seconds: number): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
function onError(err: Error) {
that.logger.error(
'Failed to re-register debuggee ' + that.project + ': ' + err
);
that.scheduleRegistration_(
Math.min(
(seconds + 1) * 2,
that.config.internal.maxRegistrationRetryDelay
)
);
}
setTimeout(() => {
if (!that.running) {
onError(new Error('Debuglet not running'));
return;
}
// TODO: Handle the case when `that.debuggee` is null.
that.controller.register(
that.debuggee as Debuggee,
(err: Error | null, result?: {debuggee: Debuggee}) => {
if (err) {
onError(err);
return;
}
// TODO: It appears that the Debuggee class never has an
// `isDisabled`
// field set. Determine if this is a bug or if the following
// code is not needed.
// TODO: Handle the case when `result` is undefined.
if ((result as {debuggee: Debuggee}).debuggee.isDisabled) {
// Server has disabled this debuggee / debug agent.
onError(new Error('Disabled by the server'));
that.emit('remotelyDisabled');
return;
}
// TODO: Handle the case when `result` is undefined.
that.logger.info(
'Registered as debuggee:',
(result as {debuggee: Debuggee}).debuggee.id
);
// TODO: Handle the case when `that.debuggee` is null.
// TODO: Handle the case when `result` is undefined.
(that.debuggee as Debuggee).id = (
result as {
debuggee: Debuggee;
}
).debuggee.id;
// TODO: Handle the case when `result` is undefined.
that.emit('registered', (result as {debuggee: Debuggee}).debuggee.id);
that.debuggeeRegistered.resolve();
if (!that.fetcherActive) {
that.scheduleBreakpointFetch_(0, false);
}
}
);
}, seconds * 1000).unref();
}
/**
* @param {number} seconds
* @param {boolean} once
* @private
*/
scheduleBreakpointFetch_(seconds: number, once: boolean): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
if (!once) {
that.fetcherActive = true;
}
setTimeout(() => {
if (!that.running) {
return;
}
if (!once) {
assert(that.fetcherActive);
}
that.logger.info('Fetching breakpoints');
// TODO: Address the case when `that.debuggee` is `null`.
that.controller.listBreakpoints(
that.debuggee as Debuggee,
(err, response, body) => {
if (err) {
that.logger.error(
'Error fetching breakpoints – scheduling retry',
err
);
that.fetcherActive = false;
// We back-off from fetching breakpoints, and try to register
// again after a while. Successful registration will restart the
// breakpoint fetcher.
that.updatePromise();
that.scheduleRegistration_(
that.config.internal.registerDelayOnFetcherErrorSec
);
return;
}
// TODO: Address the case where `response` is `undefined`.
switch (response!.statusCode) {
case 404:
// Registration expired. Deactivate the fetcher and queue
// re-registration, which will re-active breakpoint fetching.
that.logger.info('\t404 Registration expired.');
that.fetcherActive = false;
that.updatePromise();
that.scheduleRegistration_(0 /*immediately*/);
return;
default:
// TODO: Address the case where `response` is `undefined`.
that.logger.info('\t' + response!.statusCode + ' completed.');
if (!body) {
that.logger.error('\tinvalid list response: empty body');
that.scheduleBreakpointFetch_(
that.config.breakpointUpdateIntervalSec,
once
);
return;
}
if (body.waitExpired) {
that.logger.info('\tLong poll completed.');
that.scheduleBreakpointFetch_(0 /*immediately*/, once);
return;
}
// eslint-disable-next-line no-case-declarations
const bps = (body.breakpoints || []).filter(
(bp: stackdriver.Breakpoint) => {
const action = bp.action || 'CAPTURE';
if (action !== 'CAPTURE' && action !== 'LOG') {
that.logger.warn(
'Found breakpoint with invalid action:',
action
);
bp.status = new StatusMessage(
StatusMessage.UNSPECIFIED,
BREAKPOINT_ACTION_MESSAGE,
true
);
that.rejectBreakpoint_(bp);
return false;
}
return true;
}
);
that.updateActiveBreakpoints_(bps);
if (Object.keys(that.activeBreakpointMap).length) {
that.logger.info(
formatBreakpoints(
'Active Breakpoints: ',
that.activeBreakpointMap
)
);
}
that.breakpointFetchedTimestamp = Date.now();
if (once) {
if (that.breakpointFetched) {
that.breakpointFetched.resolve();
that.breakpointFetched = null;
}
} else {
that.scheduleBreakpointFetch_(
that.config.breakpointUpdateIntervalSec,
once
);
}
return;
}
}
);
}, seconds * 1000).unref();
}
/**
* updatePromise_ is called when debuggee is expired. debuggeeRegistered
* CachedPromise will be refreshed. Also, breakpointFetched CachedPromise will
* be resolved so that uses (such as GCF users) will not hang forever to wait
* non-fetchable breakpoints.
*/
private updatePromise() {
this.debuggeeRegistered = new CachedPromise();
if (this.breakpointFetched) {
this.breakpointFetched.resolve();
this.breakpointFetched = null;
}
}
/**
* Given a list of server breakpoints, update our internal list of breakpoints
* @param {Array.<Breakpoint>} breakpoints
* @private
*/
updateActiveBreakpoints_(breakpoints: stackdriver.Breakpoint[]): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
const updatedBreakpointMap = this.convertBreakpointListToMap_(breakpoints);
if (breakpoints.length) {
that.logger.info(
formatBreakpoints('Server breakpoints: ', updatedBreakpointMap)
);
}
breakpoints.forEach((breakpoint: stackdriver.Breakpoint) => {
// TODO: Address the case when `breakpoint.id` is `undefined`.
if (
!that.completedBreakpointMap[breakpoint.id as string] &&
!that.activeBreakpointMap[breakpoint.id as string]
) {
// New breakpoint
that.addBreakpoint_(breakpoint, err => {
if (err) {
that.completeBreakpoint_(breakpoint, false);
}
});
// Schedule the expiry of server breakpoints.
that.scheduleBreakpointExpiry_(breakpoint);
}
});
// Remove completed breakpoints that the server no longer cares about.
Debuglet.mapSubtract(
this.completedBreakpointMap,
updatedBreakpointMap
).forEach(breakpoint => {
// TODO: FIXME: breakpoint is a boolean here that doesn't have an id
// field. It is possible that breakpoint.id is always
// undefined!
// TODO: Make sure the use of `that` here is correct.
delete that.completedBreakpointMap[(breakpoint as {} as {id: number}).id];
});
// Remove active breakpoints that the server no longer care about.
Debuglet.mapSubtract(
this.activeBreakpointMap,
updatedBreakpointMap
).forEach(bp => {
this.removeBreakpoint_(bp, true);
});
}
/**
* Array of breakpints get converted to Map of breakpoints, indexed by id
* @param {Array.<Breakpoint>} breakpointList
* @return {Object.<string, Breakpoint>} A map of breakpoint IDs to breakpoints.
* @private
*/
convertBreakpointListToMap_(breakpointList: stackdriver.Breakpoint[]): {
[key: string]: stackdriver.Breakpoint;
} {
const map: {[id: string]: stackdriver.Breakpoint} = {};
breakpointList.forEach(breakpoint => {
// TODO: Address the case when `breakpoint.id` is `undefined`.
map[breakpoint.id as string] = breakpoint;
});
return map;
}
/**
* @param {Breakpoint} breakpoint
* @private
*/
removeBreakpoint_(
breakpoint: stackdriver.Breakpoint,
deleteFromV8: boolean
): void {
this.logger.info('\tdeleted breakpoint', breakpoint.id);
// TODO: Address the case when `breakpoint.id` is `undefined`.
delete this.activeBreakpointMap[breakpoint.id as string];
if (deleteFromV8 && this.v8debug) {
this.v8debug.clear(breakpoint, err => {
if (err) this.logger.error(err);
});
}
}
/**
* @param {Breakpoint} breakpoint
* @return {boolean} false on error
* @private
*/
addBreakpoint_(
breakpoint: stackdriver.Breakpoint,
cb: (ob: Error | string) => void
): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
if (
!that.config.allowExpressions &&
(breakpoint.condition || breakpoint.expressions)
) {
that.logger.error(ALLOW_EXPRESSIONS_MESSAGE);
breakpoint.status = new StatusMessage(
StatusMessage.UNSPECIFIED,
ALLOW_EXPRESSIONS_MESSAGE,
true
);