This repository was archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathdebuglet.ts
1029 lines (919 loc) · 35.1 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 Inc. All Rights Reserved.
*
* 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 {AuthenticationConfig, Common} from '../types/common';
const common: Common = require('@google-cloud/common');
import * as crypto from 'crypto';
import {EventEmitter} from 'events';
import extend from 'extend';
import * as fs from 'fs';
import * as metadata from 'gcp-metadata';
import * as _ from 'lodash';
import * as path from 'path';
import * as util from 'util';
import * as utils from './util/utils';
import * as http from 'http';
import {Controller} from './controller';
import {Debuggee} from '../debuggee';
import {StatusMessage} from '../client/stackdriver/status-message';
import {defaultConfig} from './config';
import * as scanner from './io/scanner';
import * as SourceMapper from './io/sourcemapper';
import * as debugapi from './v8/debugapi';
import assert from 'assert';
import * as stackdriver from '../types/stackdriver';
import {DebugAgentConfig, ResolvedDebugAgentConfig} from './config';
import {Debug, PackageInfo} from '../client/stackdriver/debug';
import {Logger} from '../types/common';
import {DebugApi} from './v8/debugapi';
const promisify = require('util.promisify');
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 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;
/**
* 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 {common.logger} */
this.logger = new common.logger({
level: common.logger.LEVELS[this.config.logLevel],
tag: this.debug.packageInfo.name
});
/** @private {DebugletApi} */
this.controller = new Controller(this.debug);
/** @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 normalizeConfig_(config: DebugAgentConfig): ResolvedDebugAgentConfig {
const envConfig = {
logLevel: process.env.GCLOUD_DEBUG_LOGLEVEL,
serviceContext: {
service: process.env.GAE_SERVICE || process.env.GAE_MODULE_NAME,
version: process.env.GAE_VERSION || process.env.GAE_MODULE_VERSION,
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 async findFiles(shouldHash: boolean, baseDir: string):
Promise<FindFilesResult> {
const fileStats = await scanner.scan(shouldHash, baseDir, /.js$|.js.map$/);
const jsStats = fileStats.selectStats(/.js$/);
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> {
const that = this;
const stat = 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;
}
// TODO: Verify that it is fine for `id` to be undefined.
let id: string|undefined;
if (process.env.GAE_MINOR_VERSION) {
id = 'GAE-' + process.env.GAE_MINOR_VERSION;
}
let findResults: FindFilesResult;
try {
findResults = await Debuglet.findFiles(!id, that.config.workingDirectory);
findResults.errors.forEach(that.logger.warn);
} catch (err) {
that.logger.error('Error scanning the filesystem.', err);
that.emit('initError', err);
return;
}
SourceMapper.create(findResults.mapFiles, async (err3, sourcemapper) => {
if (err3) {
that.logger.error('Error processing the sourcemaps.', err3);
that.emit('initError', err3);
return;
}
// At this point err3 being falsy implies sourcemapper is defined
const mapper = sourcemapper as SourceMapper.SourceMapper;
that.v8debug = debugapi.create(
that.logger, that.config, findResults.jsStats, mapper);
id = id || findResults.hash;
that.logger.info('Unique ID for this Application: ' + id);
const onGCP = await Debuglet.runningOnGCP();
let project: string;
try {
project = await Debuglet.getProjectId(that.debug.options);
} 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. */
}
}
that.getSourceContext_((err5, sourceContext) => {
if (err5) {
that.logger.warn('Unable to discover source context', err5);
// This is ignorable.
}
if (utils.satisfies(process.version, '5.2 || <4')) {
// Using an unsupported version. We report an error
// message about the Node.js version, but we keep on
// running. The idea is that the user may miss the error
// message on the console. This way we can report the
// error when the user tries to set a breakpoint.
that.logger.error(NODE_VERSION_MESSAGE);
}
// We can register as a debuggee now.
that.logger.debug('Starting debuggee, project', project);
that.running = true;
// TODO: Address the case where `project` is `undefined`.
that.project = project;
that.debuggee = Debuglet.createDebuggee(
// TODO: Address the case when `id` is `undefined`.
project, id as string, that.config.serviceContext, sourceContext,
onGCP, that.debug.packageInfo, that.config.description, undefined);
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},
sourceContext: {[key: string]: string}, onGCP: boolean,
packageInfo: PackageInfo, description?: string,
errorMessage?: 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
};
if (serviceContext) {
if (_.isString(serviceContext.service) &&
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 (_.isString(serviceContext.version)) {
labels.version = serviceContext.version;
desc += ' version:' + serviceContext.version;
}
if (_.isString(serviceContext.minorVersion_)) {
// v--- intentional lowercase
labels.minorversion = serviceContext.minorVersion_;
}
}
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 = {
project: projectId,
uniquifier,
description: desc,
agentVersion: version,
labels,
statusMessage,
sourceContexts: [sourceContext],
packageInfo
};
return new Debuggee(properties);
}
static async getProjectId(options: AuthenticationConfig): Promise<string> {
const project = options.projectId || process.env.GCLOUD_PROJECT ||
await this.getProjectIdFromMetadata();
if (!project) {
const msg = 'Unable to discover projectId. Please provide the ' +
'projectId to be able to use the Debug agent';
throw new Error(msg);
}
return project;
}
static runningOnGCP(): Promise<boolean> {
return metadata.isAvailable();
}
static async getProjectIdFromMetadata(): Promise<string> {
return (await metadata.project('project-id')).data as string;
}
static async getClusterNameFromMetadata(): Promise<string> {
return (await metadata.instance('attributes/cluster-name')).data as string;
}
getSourceContext_(
callback:
(err: Error|string, sourceContext: {[key: string]: string}) => void):
void {
fs.readFile('source-context.json', 'utf8', (err: string|Error, data) => {
let sourceContext;
if (!err) {
try {
sourceContext = JSON.parse(data);
} catch (e) {
// TODO: Fix casting `err` from an ErrnoException to a string
err = 'Malformed source-context.json file: ' + e;
}
}
// We keep on going even if there are errors.
return callback(err, sourceContext);
});
}
/**
* @param {number} seconds
* @private
*/
scheduleRegistration_(seconds: number): void {
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 {
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(
'Unable to fetch breakpoints – stopping fetcher', 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 as http.ServerResponse).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 as http.ServerResponse).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;
}
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 {
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);
}
});
// 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(this.removeBreakpoint_, this);
}
/**
* 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): 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 (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 {
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);
setImmediate(() => {
cb(ALLOW_EXPRESSIONS_MESSAGE);
});
return;
}
if (utils.satisfies(process.version, '5.2 || <4')) {
const message = NODE_VERSION_MESSAGE;
that.logger.error(message);
breakpoint.status =
new StatusMessage(StatusMessage.UNSPECIFIED, message, true);
setImmediate(() => {
cb(message);
});
return;
}
// TODO: Address the case when `that.v8debug` is `null`.
(that.v8debug as DebugApi).set(breakpoint, (err1) => {
if (err1) {
cb(err1);
return;
}
that.logger.info('\tsuccessfully added breakpoint ' + breakpoint.id);
// TODO: Address the case when `breakpoint.id` is `undefined`.
that.activeBreakpointMap[breakpoint.id as string] = breakpoint;
if (breakpoint.action === 'LOG') {
// TODO: Address the case when `that.v8debug` is `null`.
(that.v8debug as DebugApi)
.log(
breakpoint,
(fmt: string, exprs: string[]) => {
console.log('LOGPOINT:', Debuglet.format(fmt, exprs));
},
() => {
// TODO: Address the case when `breakpoint.id` is `undefined`.
return that.completedBreakpointMap[breakpoint.id as string];
});
} else {
// TODO: Address the case when `that.v8debug` is `null`.
(that.v8debug as DebugApi).wait(breakpoint, (err2) => {
if (err2) {
that.logger.error(err2);
cb(err2);
return;
}
that.logger.info('Breakpoint hit!: ' + breakpoint.id);
that.completeBreakpoint_(breakpoint);
});
}
});
}
/**
* Update the server that the breakpoint has been completed (captured, or
* expired).
* @param {Breakpoint} breakpoint
* @private
*/
completeBreakpoint_(breakpoint: stackdriver.Breakpoint): void {
const that = this;
that.logger.info('\tupdating breakpoint data on server', breakpoint.id);
that.controller.updateBreakpoint(
// TODO: Address the case when `that.debuggee` is `null`.
(that.debuggee as Debuggee), breakpoint, (err /*, body*/) => {
if (err) {
that.logger.error('Unable to complete breakpoint on server', err);
} else {
// TODO: Address the case when `breakpoint.id` is `undefined`.
that.completedBreakpointMap[breakpoint.id as string] = true;
that.removeBreakpoint_(breakpoint);
}
});
}
/**
* Update the server that the breakpoint cannot be handled.
* @param {Breakpoint} breakpoint
* @private
*/
rejectBreakpoint_(breakpoint: stackdriver.Breakpoint): void {
const that = this;
// TODO: Address the case when `that.debuggee` is `null`.
that.controller.updateBreakpoint(
(that.debuggee as Debuggee), breakpoint, (err /*, body*/) => {
if (err) {
that.logger.error('Unable to complete breakpoint on server', err);
}
});
}
/**
* This schedules a delayed operation that will delete the breakpoint from the
* server after the expiry period.
* FIXME: we should cancel the timer when the breakpoint completes. Otherwise
* we hold onto the closure memory until the breapointExpirateion timeout.
* @param {Breakpoint} breakpoint Server breakpoint object
* @private
*/
scheduleBreakpointExpiry_(breakpoint: stackdriver.Breakpoint): void {
const that = this;
const now = Date.now() / 1000;
const createdTime =
breakpoint.createdTime ? Number(breakpoint.createdTime.seconds) : now;
const expiryTime = createdTime + that.config.breakpointExpirationSec;
setTimeout(() => {
that.logger.info('Expiring breakpoint ' + breakpoint.id);
breakpoint.status = {
description: {format: 'The snapshot has expired'},
isError: true,
refersTo: StatusMessage.BREAKPOINT_AGE
};
that.completeBreakpoint_(breakpoint);
}, (expiryTime - now) * 1000).unref();
}
/**
* Stops the Debuglet. This is for testing purposes only. Stop should only be
* called on a agent that has started (i.e. emitted the 'started' event).
* Calling this while the agent is initializing may not necessarily stop all
* pending operations.
*/
stop(): void {
assert.ok(this.running, 'stop can only be called on a running agent');
this.logger.debug('Stopping Debuglet');
this.running = false;
this.emit('stopped');
}
/**
* Performs a set subtract. Returns A - B given maps A, B.
* @return {Array.<Breakpoint>} A array containing elements from A that are not
* in B.
*/
// TODO: Determine if this can be generic
// TODO: The code that uses this actually assumes the supplied arguments
// are objects and used as an associative array. Determine what is
// correct (the code or the docs).
// TODO: Fix the docs because the code actually assumes that the values
// of the keys in the supplied arguments have boolean values or
// Breakpoint values.
static mapSubtract<T, U>(A: {[key: string]: T}, B: {[key: string]: U}): T[] {
const removed = [];
for (const key in A) {
if (!B[key]) {
removed.push(A[key]);
}
}
return removed;
}
/**
* Formats the message base with placeholders `$0`, `$1`, etc
* by substituting the provided expressions. If more expressions
* are given than placeholders extra expressions are dropped.
*/
static format(base: string, exprs: string[]): string {
const tokens = Debuglet._tokenize(base, exprs.length);
for (let i = 0; i < tokens.length; i++) {
// TODO: Determine how to remove this explicit cast
if (!(tokens[i] as {v: string}).v) {
continue;
}
// TODO: Determine how to not have an explicit cast here
if ((tokens[i] as {v: string}).v === '$$') {
tokens[i] = '$';
continue;
}
for (let j = 0; j < exprs.length; j++) {
// TODO: Determine how to not have an explicit cast here
if ((tokens[i] as {v: string}).v === '$' + j) {
tokens[i] = exprs[j];
break;
}
}
}
return tokens.join('');
}
static _tokenize(base: string, exprLength: number):
Array<{v: string}|string> {
let acc = Debuglet._delimit(base, '$$');
for (let i = exprLength - 1; i >= 0; i--) {
const newAcc = [];
for (let j = 0; j < acc.length; j++) {
// TODO: Determine how to remove this explicit cast
if ((acc[j] as {v: string}).v) {
newAcc.push(acc[j]);
} else {