-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathbreakpoint_manager.dart
More file actions
423 lines (363 loc) · 13.3 KB
/
Copy pathbreakpoint_manager.dart
File metadata and controls
423 lines (363 loc) · 13.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
// Copyright 2022 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.
import 'dart:async';
import 'package:collection/collection.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/foundation.dart';
import 'package:vm_service/vm_service.dart';
import '../../service/vm_service_wrapper.dart';
import '../../shared/diagnostics/primitives/source_location.dart';
import '../../shared/globals.dart';
import 'debugger_model.dart';
class BreakpointManager with DisposerMixin {
BreakpointManager({this.initialSwitchToIsolate = true});
final bool initialSwitchToIsolate;
VmServiceWrapper get _service => serviceConnection.serviceManager.service!;
final _breakPositionsMap = <String, List<SourcePosition>>{};
final _breakpoints = ValueNotifier<List<Breakpoint>>([]);
ValueListenable<List<BreakpointAndSourcePosition>>
get breakpointsWithLocation => _breakpointsWithLocation;
final _breakpointsWithLocation =
ValueNotifier<List<BreakpointAndSourcePosition>>([]);
IsolateRef? _isolateRef;
String get _isolateRefId => _isolateRef?.id ?? '';
final _previousIsolateBreakpoints = <BreakpointAndSourcePosition>[];
Future<void> initialize() async {
final isolate =
serviceConnection.serviceManager.isolateManager.selectedIsolate.value;
if (initialSwitchToIsolate && isolate != null) {
await switchToIsolate(
serviceConnection.serviceManager.isolateManager.selectedIsolate.value,
);
}
addAutoDisposeListener(
serviceConnection.serviceManager.isolateManager.selectedIsolate,
() async {
await switchToIsolate(
serviceConnection.serviceManager.isolateManager.selectedIsolate.value,
);
},
);
autoDisposeStreamSubscription(
_service.onDebugEvent.listen(_handleDebugEvent),
);
autoDisposeStreamSubscription(
_service.onIsolateEvent.listen(_handleIsolateEvent),
);
}
Future<void> switchToIsolate(IsolateRef? isolateRef) async {
_isolateRef = isolateRef;
if (isolateRef == null) {
_saveAndClearCurrentBreakpoints();
return;
}
final breakpointsForIsolate = await _getBreakpointsForIsolate(
_isolateRefId,
);
if (breakpointsForIsolate.isNotEmpty) {
// If the isolate already has breakpoints, then update them:
await _updateBreakpoints(
breakpoints: breakpointsForIsolate,
isolateId: _isolateRefId,
);
} else {
// Otherwise, re-establish the breakpoints from the previous isolate:
await _setUpBreakpoints(
breakpoints: _previousIsolateBreakpoints,
isolateRef: isolateRef,
);
}
// Maybe resume the isolate now that the breakpoints have been set:
final isolate = await _service.getIsolate(_isolateRefId);
final pauseEventKind = isolate.pauseEvent?.kind;
if ([
EventKind.kPauseStart,
EventKind.kPausePostRequest,
// We check for a resume event because package:dwds sends a resume event
// after a hot-restart. See:
// https://github.com/dart-lang/webdev/issues/2610
EventKind.kResume,
].contains(pauseEventKind)) {
await serviceConnection.serviceManager.isolateManager.resumeIsolate(
isolateRef,
);
}
}
void clearCache({required bool isServiceShutdown}) {
_breakPositionsMap.clear();
_breakpoints.value = [];
_breakpointsWithLocation.value = [];
if (isServiceShutdown) {
_previousIsolateBreakpoints.clear();
}
}
Future<void> clearBreakpoints() async {
final breakpoints = _breakpoints.value.toList();
await Future.forEach(breakpoints, (Breakpoint breakpoint) {
return removeBreakpoint(breakpoint);
});
}
Future<Breakpoint> addBreakpoint(String scriptId, int line) =>
_service.addBreakpoint(_isolateRefId, scriptId, line);
Future<void> removeBreakpoint(Breakpoint breakpoint) =>
_service.removeBreakpoint(_isolateRefId, breakpoint.id!);
Future<void> toggleBreakpoint(ScriptRef script, int line) async {
final selectedIsolate =
serviceConnection.serviceManager.isolateManager.selectedIsolate.value;
if (selectedIsolate == null) {
// Can't toggle breakpoints if we don't have an isolate.
return;
}
// The VM doesn't support debugging for system isolates and will crash on
// a failed assert in debug mode. Disable the toggle breakpoint
// functionality for system isolates.
if (selectedIsolate.isSystemIsolate!) {
return;
}
final bp = breakpointsWithLocation.value.firstWhereOrNull((bp) {
return bp.scriptRef == script && bp.line == line;
});
if (bp != null) {
await removeBreakpoint(bp.breakpoint);
} else {
try {
await addBreakpoint(script.id!, line);
} catch (_) {
// ignore errors setting breakpoints
}
}
}
void _saveAndClearCurrentBreakpoints() {
if (breakpointsWithLocation.value.isNotEmpty) {
_previousIsolateBreakpoints
..clear()
..addAll(_breakpointsWithLocation.value);
}
_breakpoints.value = [];
_breakpointsWithLocation.value = [];
}
void _updateAfterIsolateReload(Event _) async {
// TODO(devoncarew): We need to coordinate this with other debugger clients
// as well as pause before re-setting the breakpoints.
// Refresh the list of scripts.
final previousScriptRefs = scriptManager.sortedScripts.value;
final currentScriptRefs = await scriptManager.retrieveAndSortScripts(
_isolateRef!,
);
final removedScripts = Set<ScriptRef>.of(
previousScriptRefs,
).difference(Set<ScriptRef>.of(currentScriptRefs));
final addedScripts = Set<ScriptRef>.of(
currentScriptRefs,
).difference(Set<ScriptRef>.of(previousScriptRefs));
final breakpointsToRemove = <BreakpointAndSourcePosition>[];
// Find all breakpoints set in files where we have newer versions of those
// files.
for (final scriptRef in removedScripts) {
for (final bp in breakpointsWithLocation.value) {
if (bp.scriptRef == scriptRef) {
breakpointsToRemove.add(bp);
}
}
}
await [
// Remove the breakpoints.
for (final bp in breakpointsToRemove) removeBreakpoint(bp.breakpoint),
// Add them back to the newer versions of those scripts.
for (final scriptRef in addedScripts) ...[
for (final bp in breakpointsToRemove)
if (scriptRef.uri == bp.scriptUri)
addBreakpoint(scriptRef.id!, bp.line!),
],
].wait;
}
Future<List<Breakpoint>> _getBreakpointsForIsolate(String isolateId) async {
final isolate = await _service.getIsolate(isolateId);
if (isolate.id != _isolateRefId) {
// Current request is obsolete.
return [];
}
// Ignore attempts from DWDS to re-establish breakpoints because DevTools is
// now in charge of re-establishing breakpoints:
final connectedToDwds =
serviceConnection.serviceManager.connectedApp?.isDartWebAppNow ?? false;
if (connectedToDwds) return [];
return isolate.breakpoints ?? [];
}
Future<void> _updateBreakpoints({
required List<Breakpoint> breakpoints,
required String isolateId,
}) async {
_breakpoints.value = breakpoints;
// Build _breakpointsWithLocation from _breakpoints.
final breakpointsWithLocation = await _breakpoints.value
.map(breakpointManager.createBreakpointWithLocation)
.wait;
if (isolateId != _isolateRefId) {
// Current request is obsolete.
return;
}
_breakpointsWithLocation.value = breakpointsWithLocation.sorted();
}
Future<void> _setUpBreakpoints({
required List<BreakpointAndSourcePosition> breakpoints,
required IsolateRef isolateRef,
}) async {
final scriptUriToRef = await _scriptRefsForBreakpoints(
breakpoints: breakpoints,
isolateRef: isolateRef,
);
for (final breakpoint in breakpoints) {
final newScriptRef = scriptUriToRef[breakpoint.scriptUri];
final breakpointLine = breakpoint.line;
final scriptId = newScriptRef?.id;
if (scriptId != null && breakpointLine != null) {
await addBreakpoint(scriptId, breakpointLine);
}
}
}
Future<Map<String, ScriptRef>> _scriptRefsForBreakpoints({
required List<BreakpointAndSourcePosition> breakpoints,
required IsolateRef isolateRef,
}) async {
final bpScriptUris = breakpoints.fold(<String>{}, (scriptSet, breakpoint) {
final scriptUri = breakpoint.scriptUri;
if (scriptUri != null) {
scriptSet.add(scriptUri);
}
return scriptSet;
});
final newScripts = await scriptManager.retrieveAndSortScripts(isolateRef);
final scriptUriToRef = newScripts.fold(<String, ScriptRef>{}, (
scriptMap,
script,
) {
final scriptUri = script.uri;
if (scriptUri != null && bpScriptUris.contains(scriptUri)) {
scriptMap[scriptUri] = script;
}
return scriptMap;
});
return scriptUriToRef;
}
/// Return the list of valid positions for breakpoints for a given script.
Future<List<SourcePosition>> getBreakablePositions(
IsolateRef? isolateRef,
Script script,
) async {
final key = script.id;
if (key == null) return [];
if (!_breakPositionsMap.containsKey(key)) {
_breakPositionsMap[key] = await _getBreakablePositions(
isolateRef,
script,
);
}
return _breakPositionsMap[key] ?? [];
}
Future<List<SourcePosition>> _getBreakablePositions(
IsolateRef? isolateRef,
Script script,
) async {
final report = await _service.getSourceReport(
isolateRef?.id ?? '',
[SourceReportKind.kPossibleBreakpoints],
scriptId: script.id,
forceCompile: true,
);
final positions = <SourcePosition>[];
for (final range in report.ranges!) {
final possibleBreakpoints = range.possibleBreakpoints;
if (possibleBreakpoints != null) {
for (final tokenPos in possibleBreakpoints) {
positions.add(SourcePosition.calculatePosition(script, tokenPos));
}
}
}
return positions;
}
Future<BreakpointAndSourcePosition> createBreakpointWithLocation(
Breakpoint breakpoint,
) async {
if (breakpoint.resolved!) {
final bp = BreakpointAndSourcePosition.create(breakpoint);
return scriptManager.getScript(bp.scriptRef!).then((Script? script) {
final pos = SourcePosition.calculatePosition(script!, bp.tokenPos!);
return BreakpointAndSourcePosition.create(breakpoint, pos);
});
} else {
return BreakpointAndSourcePosition.create(breakpoint);
}
}
void _handleIsolateEvent(Event event) {
final eventId = event.isolate?.id;
if (eventId != _isolateRefId) return;
switch (event.kind) {
case EventKind.kIsolateReload:
_updateAfterIsolateReload(event);
break;
}
}
Future<void> _handleDebugEvent(Event event) async {
if (event.isolate!.id != _isolateRefId) return;
switch (event.kind) {
// TODO(djshuckerow): switch the _breakpoints notifier to a 'ListNotifier'
// that knows how to notify when performing a list edit operation.
case EventKind.kBreakpointAdded:
final breakpoint = event.breakpoint!;
final isDuplicate = _breakpoints.value.any(
(bp) => bp.id == breakpoint.id,
);
if (isDuplicate) break;
_breakpoints.value = [..._breakpoints.value, breakpoint];
await breakpointManager.createBreakpointWithLocation(breakpoint).then((
bp,
) {
final list = [..._breakpointsWithLocation.value, bp]..sort();
_breakpointsWithLocation.value = list;
});
break;
case EventKind.kBreakpointResolved:
final breakpoint = event.breakpoint!;
_breakpoints.value = [
for (final b in _breakpoints.value)
if (b != event.breakpoint) b,
breakpoint,
];
await breakpointManager.createBreakpointWithLocation(breakpoint).then((
bp,
) {
final list = _breakpointsWithLocation.value;
// Remove the bp with the older, unresolved information from the list.
list.removeWhere((breakpoint) => breakpoint.id == bp.id);
// Add the bp with the newer, resolved information.
list.add(bp);
list.sort();
_breakpointsWithLocation.value = list;
});
break;
case EventKind.kBreakpointRemoved:
// Ignore any breakpoints removed during a hot restart, because the VM
// service removes them before resuming the isolate and then performing
// the restart. Note we only track hot restarts triggered by DevTools,
// if a hot-restart was triggered by another client we won't know.
// See https://github.com/flutter/flutter/issues/134470
final hotRestartInProgress = serviceConnection
.serviceManager
.isolateManager
.hotRestartInProgress;
if (hotRestartInProgress) break;
final breakpoint = event.breakpoint;
_breakpoints.value = [
for (final b in _breakpoints.value)
if (b != breakpoint) b,
];
_breakpointsWithLocation.value = [
for (final b in _breakpointsWithLocation.value)
if (b.breakpoint != breakpoint) b,
];
break;
}
}
}