forked from flutter/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm_service_wrapper.dart
More file actions
521 lines (446 loc) · 15.3 KB
/
vm_service_wrapper.dart
File metadata and controls
521 lines (446 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Copyright 2018 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
// Code needs to match API from VmService.
// ignore_for_file: avoid-dynamic
/// @docImport '../shared/preferences/preferences.dart';
library;
import 'dart:async';
import 'dart:convert';
import 'package:collection/collection.dart' show IterableExtension;
import 'package:dap/dap.dart' as dap;
import 'package:dds_service_extensions/dap.dart';
import 'package:dds_service_extensions/dds_service_extensions.dart';
import 'package:devtools_app_shared/service.dart';
import 'package:devtools_shared/devtools_shared.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:vm_service/vm_service.dart';
import '../screens/vm_developer/vm_service_private_extensions.dart';
import '../shared/feature_flags.dart';
import '../shared/globals.dart';
import '../shared/primitives/utils.dart';
import '../shared/utils/utils.dart';
import 'json_to_service_cache.dart';
final _log = Logger('vm_service_wrapper');
class VmServiceWrapper extends VmService {
VmServiceWrapper(
super.inStream,
super.writeMessage, {
super.log,
super.disposeHandler,
super.streamClosed,
super.wsUri,
bool trackFutures = false,
}) : _trackFutures = trackFutures {
unawaited(_initSupportedProtocols());
}
static VmServiceWrapper defaultFactory({
required Stream<dynamic> /*String|List<int>*/ inStream,
required void Function(String message) writeMessage,
Log? log,
DisposeHandler? disposeHandler,
Future? streamClosed,
String? wsUri,
bool trackFutures = false,
}) {
return VmServiceWrapper(
inStream,
writeMessage,
log: log,
disposeHandler: disposeHandler,
streamClosed: streamClosed,
wsUri: wsUri,
trackFutures: trackFutures,
);
}
// TODO(https://github.com/dart-lang/sdk/issues/49072): in the long term, do
// not support diverging DevTools functionality based on whether the DDS
// protocol is supported. Conditional logic around [_ddsSupported] was added
// in https://github.com/flutter/devtools/pull/4119 as a workaround for
// profiling the analysis server.
Future<void> _initSupportedProtocols() async {
Protocol? ddsProtocol;
try {
final supportedProtocols = await getSupportedProtocols();
ddsProtocol = supportedProtocols.protocols?.firstWhereOrNull(
(Protocol p) => p.protocolName?.caseInsensitiveEquals('DDS') ?? false,
);
} on RPCError catch (e) {
if (!e.isServiceDisposedError) {
// Swallow exceptions related to trying to interact with an
// already-disposed service connection. Otherwise, rethrow.
rethrow;
}
}
_ddsSupported = ddsProtocol != null;
_supportedProtocolsInitialized.complete();
}
final _supportedProtocolsInitialized = Completer<void>();
bool _ddsSupported = false;
final bool _trackFutures;
final _activeStreams = <String, Future<Success>>{};
final activeFutures = <TrackedFuture<Object>>{};
Future<void> get allFuturesCompleted => _allFuturesCompleter.future;
Completer<bool> _allFuturesCompleter = Completer<bool>()
// Mark the future as completed by default so if we don't track any
// futures but someone tries to wait on [allFuturesCompleted] they don't
// hang. The first tracked future will replace this with a new completer.
..complete(true);
// A local cache of "fake" service objects. Used to convert JSON objects to
// VM service response formats to be used with APIs that require them.
final fakeServiceCache = JsonToServiceCache();
/// A counter for unique ids to add to each of a future's messages.
static int _logIdCounter = 0;
/// A sequence number incremented and attached to each DAP request.
static int _dapSeq = 0;
/// Executes `callback` for each isolate, and waiting for all callbacks to
/// finish before completing.
Future<void> forEachIsolate(
Future<void> Function(IsolateRef) callback,
) async {
await forEachIsolateHelper(this, callback);
}
@override
Future<AllocationProfile> getAllocationProfile(
String isolateId, {
bool? reset,
bool? gc,
}) {
return callMethod(
// TODO(bkonyi): add _new and _old to public response.
'_getAllocationProfile',
isolateId: isolateId,
args: <String, dynamic>{
if (reset != null && reset) 'reset': reset,
if (gc != null && gc) 'gc': gc,
},
).then((r) => r as AllocationProfile);
}
@override
Future<CpuSamples> getCpuSamples(
String isolateId,
int timeOriginMicros,
int timeExtentMicros,
) {
return callMethod(
'getCpuSamples',
isolateId: isolateId,
args: {
'timeOriginMicros': timeOriginMicros,
'timeExtentMicros': timeExtentMicros,
// Requests the code profile in addition to the function profile when
// running with advanced developer mode enabled. This data isn't
// accessible in non-advanced developer mode, so not requesting the code
// profile will save on space and network usage.
'_code': preferences.advancedDeveloperModeEnabled.value,
},
).then((e) => e as CpuSamples);
}
@override
Future<Obj> getObject(
String isolateId,
String objectId, {
int? offset,
int? count,
String? idZoneId,
}) {
final cachedObj = fakeServiceCache.getObject(
objectId: objectId,
offset: offset,
count: count,
);
if (cachedObj != null) {
return Future.value(cachedObj);
}
return super.getObject(isolateId, objectId, offset: offset, count: count);
}
Future<HeapSnapshotGraph> getHeapSnapshotGraph(
IsolateRef isolateRef, {
bool calculateReferrers = true,
bool decodeObjectData = true,
bool decodeExternalProperties = true,
bool decodeIdentityHashCodes = true,
}) async {
return await HeapSnapshotGraph.getSnapshot(
this,
isolateRef,
calculateReferrers: calculateReferrers,
decodeObjectData: decodeObjectData,
decodeExternalProperties: decodeExternalProperties,
decodeIdentityHashCodes: decodeIdentityHashCodes,
);
}
@override
Future<Success> streamCancel(String streamId) async {
await _activeStreams.remove(streamId);
return super.streamCancel(streamId);
}
// We tweaked this method so that we do not try to listen to the same stream
// twice. This was causing an issue with the test environment and this change
// should not affect the run environment.
@override
Future<Success> streamListen(String streamId) {
if (!_activeStreams.containsKey(streamId)) {
return _activeStreams[streamId] = super.streamListen(streamId);
} else {
return _activeStreams[streamId]!.then((value) => value);
}
}
// Mark: Overrides for [DdsExtension]. We wrap these methods so that we can
// override them in tests.
Future<PerfettoTimeline> getPerfettoVMTimelineWithCpuSamplesWrapper({
int? timeOriginMicros,
int? timeExtentMicros,
}) {
return getPerfettoVMTimelineWithCpuSamples(
timeOriginMicros: timeOriginMicros,
timeExtentMicros: timeExtentMicros,
);
}
Stream<Event> get onExtensionEventWithHistorySafe {
return _maybeReturnStreamWithHistory(
onExtensionEventWithHistory,
fallbackStream: onExtensionEvent,
);
}
Stream<Event> get onLoggingEventWithHistorySafe {
return _maybeReturnStreamWithHistory(
onLoggingEventWithHistory,
fallbackStream: onLoggingEvent,
);
}
Stream<Event> get onStderrEventWithHistorySafe {
return _maybeReturnStreamWithHistory(
onStderrEventWithHistory,
fallbackStream: onStderrEvent,
);
}
Stream<Event> get onStdoutEventWithHistorySafe {
return _maybeReturnStreamWithHistory(
onStdoutEventWithHistory,
fallbackStream: onStdoutEvent,
);
}
Stream<Event> get onTimerEventWithHistorySafe {
return _maybeReturnStreamWithHistory(
onTimerEventWithHistory,
fallbackStream: onTimerEvent,
);
}
Stream<Event> _maybeReturnStreamWithHistory(
Stream<Event> ddsStream, {
required Stream<Event> fallbackStream,
}) {
assert(_supportedProtocolsInitialized.isCompleted);
if (_ddsSupported) {
return ddsStream;
}
return fallbackStream;
}
// Begin Dart IO extension method wrappers. We wrap these methods so that we
// can override them in tests.
Future<bool> isSocketProfilingAvailableWrapper(String isolateId) {
return isSocketProfilingAvailable(isolateId);
}
Future<SocketProfilingState> socketProfilingEnabledWrapper(
String isolateId, [
bool? enabled,
]) {
return socketProfilingEnabled(isolateId, enabled);
}
Future<Success> clearSocketProfileWrapper(String isolateId) {
return clearSocketProfile(isolateId);
}
Future<SocketProfile> getSocketProfileWrapper(String isolateId) {
return getSocketProfile(isolateId);
}
Future<HttpProfileRequest> getHttpProfileRequestWrapper(
String isolateId,
String id,
) {
return getHttpProfileRequest(isolateId, id);
}
Future<HttpProfile> getHttpProfileWrapper(
String isolateId, {
DateTime? updatedSince,
}) {
return getHttpProfile(isolateId, updatedSince: updatedSince);
}
Future<Success> clearHttpProfileWrapper(String isolateId) {
return clearHttpProfile(isolateId);
}
Future<bool> isHttpTimelineLoggingAvailableWrapper(String isolateId) {
return isHttpTimelineLoggingAvailable(isolateId);
}
Future<HttpTimelineLoggingState> httpEnableTimelineLoggingWrapper(
String isolateId, [
bool? enabled,
]) {
return httpEnableTimelineLogging(isolateId, enabled);
}
// End Dart IO extension method wrappers.
/// Testing only method to indicate that we don't really need to await all
/// currently pending futures.
///
/// If you use this method be sure to indicate why you believe all pending
/// futures are safe to ignore. Currently the theory is this method should be
/// used after a hot restart to avoid bugs where we have zombie futures lying
/// around causing tests to flake.
@visibleForTesting
void doNotWaitForPendingFuturesBeforeExit() {
_allFuturesCompleter = Completer<bool>();
_allFuturesCompleter.complete(true);
activeFutures.clear();
}
@visibleForTesting
int vmServiceCallCount = 0;
@visibleForTesting
final vmServiceCalls = <String>[];
@visibleForTesting
void clearVmServiceCalls() {
vmServiceCalls.clear();
vmServiceCallCount = 0;
}
/// If logging is enabled, wraps a future with logs at its start and finish.
///
/// All logs from this run will have matching unique ids, so that they can
/// be associated together in the logs.
Future<T> _maybeLogWrappedFuture<T>(String name, Future<T> future) async {
// If the logger is not accepting FINE logs, then we won't be logging any
// messages. So just return the [future] as-is.
if (!_log.isLoggable(Level.FINE)) return future;
final logId = ++_logIdCounter;
try {
_log.fine('[$logId]-wrapFuture($name,...): Started');
final result = await future;
_log.fine('[$logId]-wrapFuture($name,...): Succeeded');
return result;
} catch (error) {
_log.severe('[$logId]-wrapFuture($name,...): Failed', error);
rethrow;
}
}
@override
Future<T> wrapFuture<T>(String name, Future<T> future) {
final localFuture = _maybeLogWrappedFuture<T>(name, future);
if (!_trackFutures) {
return localFuture;
}
vmServiceCallCount++;
vmServiceCalls.add(name);
final trackedFuture = TrackedFuture(name, localFuture as Future<Object>);
if (_allFuturesCompleter.isCompleted) {
_allFuturesCompleter = Completer<bool>();
}
activeFutures.add(trackedFuture);
void futureComplete() {
activeFutures.remove(trackedFuture);
if (activeFutures.isEmpty) {
_allFuturesCompleter.safeComplete(true);
}
}
safeUnawaited(
localFuture.then(
(value) => futureComplete(),
onError: (error) => futureComplete(),
),
);
return localFuture;
}
/// Adds support for private VM RPCs that can only be used when VM developer
/// mode is enabled. Not for use outside of VM developer pages.
/// Allows callers to invoke extension methods for private RPCs. This should
/// only be set by [PreferencesController.toggleAdvancedDeveloperMode] or tests.
static bool enablePrivateRpcs = false;
Future<T?> _privateRpcInvoke<T>(
String method, {
required T? Function(Map<String, dynamic>?) parser,
String? isolateId,
Map<String, dynamic>? args,
}) async {
if (!enablePrivateRpcs) {
throw StateError('Attempted to invoke private RPC');
}
final result = await callMethod(
'_$method',
isolateId: isolateId,
args: args,
);
return parser(result.json);
}
/// Forces the VM to perform a full garbage collection.
Future<Success?> collectAllGarbage() =>
_privateRpcInvoke('collectAllGarbage', parser: Success.parse);
Future<InstanceRef?> getReachableSize(String isolateId, String targetId) =>
_privateRpcInvoke(
'getReachableSize',
isolateId: isolateId,
args: {'targetId': targetId},
parser: InstanceRef.parse,
);
Future<InstanceRef?> getRetainedSize(String isolateId, String targetId) =>
_privateRpcInvoke(
'getRetainedSize',
isolateId: isolateId,
args: {'targetId': targetId},
parser: InstanceRef.parse,
);
Future<ObjectStore?> getObjectStore(String isolateId) => _privateRpcInvoke(
'getObjectStore',
isolateId: isolateId,
parser: ObjectStore.parse,
);
Future<dap.VariablesResponseBody?> dapVariablesRequest(
dap.VariablesArguments args,
) async {
final response = await _sendDapRequest('variables', args: args);
if (response == null) return null;
return dap.VariablesResponseBody.fromJson(response as Map<String, Object?>);
}
Future<dap.ScopesResponseBody?> dapScopesRequest(
dap.ScopesArguments args,
) async {
final response = await _sendDapRequest('scopes', args: args);
if (response == null) return null;
return dap.ScopesResponseBody.fromJson(response as Map<String, Object?>);
}
Future<dap.StackTraceResponseBody?> dapStackTraceRequest(
dap.StackTraceArguments args,
) async {
final response = await _sendDapRequest('stackTrace', args: args);
if (response == null) return null;
return dap.StackTraceResponseBody.fromJson(
response as Map<String, Object?>,
);
}
Future<Object?> _sendDapRequest(
String command, {
required Object? args,
}) async {
if (!FeatureFlags.dapDebugging) return null;
// Warn the user if there is no DDS connection.
if (!_ddsSupported) {
_log.warning('A DDS connection is required to debug via DAP.');
return null;
}
final response = await sendDapRequest(
jsonEncode(
dap.Request(command: command, seq: _dapSeq++, arguments: args),
),
);
// Log any errors from DAP if the request failed:
if (!response.dapResponse.success) {
_log.warning(
'Error for dap.$command: ${response.dapResponse.message ?? 'Unknown.'}',
);
return null;
}
return response.dapResponse.body;
}
}
class TrackedFuture<T> {
TrackedFuture(this.name, this.future);
final String name;
final Future<T> future;
}