Skip to content

Commit af06e99

Browse files
authored
On iOS 27+ devices, manually process lldb stops (flutter#191434)
On iOS 27+ devices, `lldb` sometimes doesn't stop on breakpoints when auto-continue is enabled. To workaround this, we disable auto-continue and manually process stops. If a stop is caused by a breakpoint, we tell the process to continue. Otherwise, we print the backtrace and detach. Fixes flutter#190307. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent fbb2656 commit af06e99

5 files changed

Lines changed: 453 additions & 278 deletions

File tree

‎packages/flutter_tools/lib/src/ios/core_devices.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import '../base/logger.dart';
1414
import '../base/process.dart';
1515
import '../base/template.dart';
1616
import '../base/utils.dart';
17+
import '../base/version.dart';
1718
import '../build_info.dart';
1819
import '../convert.dart';
1920
import '../device.dart';
@@ -42,6 +43,7 @@ class IOSCoreDeviceLauncher {
4243
required FileSystem fileSystem,
4344
required ProcessUtils processUtils,
4445
required XcodeProjectInterpreter xcodeProjectInterpreter,
46+
required Version? deviceVersion,
4547
@visibleForTesting LLDB? lldb,
4648
}) : _coreDeviceControl = coreDeviceControl,
4749
_logger = logger,
@@ -53,6 +55,7 @@ class IOSCoreDeviceLauncher {
5355
logger: logger,
5456
processUtils: processUtils,
5557
xcodeProjectInterpreter: xcodeProjectInterpreter,
58+
deviceVersion: deviceVersion,
5659
);
5760

5861
final IOSCoreDeviceControl _coreDeviceControl;
@@ -865,7 +868,7 @@ class IOSCoreDeviceControl {
865868
unawaited(
866869
launchProcess.exitCode
867870
.then((int status) async {
868-
_logger.printTrace('lldb exited with code $status');
871+
_logger.printTrace('devicectl exited with code $status');
869872
await stdoutSubscription.cancel();
870873
await stderrSubscription.cancel();
871874
})

‎packages/flutter_tools/lib/src/ios/lldb.dart‎

Lines changed: 102 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import '../base/io.dart';
1212
import '../base/logger.dart';
1313
import '../base/process.dart';
1414
import '../base/utils.dart';
15+
import '../base/version.dart';
1516
import '../build_info.dart';
1617
import 'device_support.dart';
1718
import 'xcodeproj.dart';
@@ -25,13 +26,16 @@ class LLDB {
2526
required Logger logger,
2627
required ProcessUtils processUtils,
2728
required XcodeProjectInterpreter xcodeProjectInterpreter,
29+
required Version? deviceVersion,
2830
}) : _logger = logger,
2931
_processUtils = processUtils,
30-
_xcodeProjectInterpreter = xcodeProjectInterpreter;
32+
_xcodeProjectInterpreter = xcodeProjectInterpreter,
33+
_deviceVersion = deviceVersion;
3134

3235
final Logger _logger;
3336
final ProcessUtils _processUtils;
3437
final XcodeProjectInterpreter _xcodeProjectInterpreter;
38+
final Version? _deviceVersion;
3539

3640
_LLDBProcess? _lldbProcess;
3741

@@ -78,6 +82,20 @@ class LLDB {
7882
/// Example: "- Hook 1 (thread backtrace all)"
7983
static final _stopHookProcessedPattern = RegExp(r'- Hook \d+');
8084

85+
/// Pattern of lldb log when the application has started.
86+
///
87+
/// Example: Target 0: (Flutter Gallery) stopped.
88+
static final _targetStoppedPattern = RegExp(r'Target .* stopped.');
89+
90+
/// LLDB command to continue execution of all threads in the current process.
91+
static const _processContinueCommand = 'process continue';
92+
93+
/// LLDB command to show backtraces of all thread call stacks.
94+
static const _threadBacktraceAllCommand = 'thread backtrace all';
95+
96+
/// LLDB command to detach from the current target process.
97+
static const _detachCommand = 'detach';
98+
8199
/// A list of log patterns to ignore.
82100
static final _ignorePatterns = <Pattern>[
83101
RegExp(r'\d+ location added to breakpoint \d+'),
@@ -110,8 +128,6 @@ if not error.Success():
110128
print(f'Failed to write into {base}[+{page_len}]', error)
111129
return
112130
113-
# If the returned value is False, that tells LLDB not to stop at the breakpoint
114-
return False
115131
''';
116132

117133
/// Starts an LLDB process and inputs commands to start debugging the [appProcessId].
@@ -144,9 +160,20 @@ return False
144160
}
145161
});
146162

163+
// iOS 27+ devices sometimes fail to stop at the JIT breakpoint (only used in debug mode)
164+
// when `auto-continue` is enabled. This causes the app to crash. To workaround this, we
165+
// manually listen for stops and continue the process if the stop is due to the breakpoint.
166+
var manualContinue = false;
167+
if (mode == BuildMode.debug &&
168+
_deviceVersion != null &&
169+
_deviceVersion >= Version(27, 0, 0)) {
170+
manualContinue = true;
171+
}
172+
147173
final bool start = await _startLLDB(
148174
appProcessId: appProcessId,
149175
lldbLogForwarder: lldbLogForwarder,
176+
manualContinue: manualContinue,
150177
);
151178
if (!start) {
152179
return false;
@@ -155,10 +182,15 @@ return False
155182
await _addSymbolSearchPaths(deviceSupport);
156183

157184
if (mode == BuildMode.debug) {
158-
await _setBreakpoint();
185+
await _setBreakpoint(manualContinue);
159186
}
160187
await _attachToAppProcess(appProcessId);
161-
await _setupStopHooks();
188+
189+
// Stop hooks should only be used when manualContinue is false. Otherwise, it would run on
190+
// every breakpoint stop instead of just crashes.
191+
if (!manualContinue) {
192+
await _setupStopHooks();
193+
}
162194
await _printDeviceSupportStatus();
163195
await _resumeProcess(mode);
164196
_isAttached = true;
@@ -180,6 +212,7 @@ return False
180212
Future<bool> _startLLDB({
181213
required int appProcessId,
182214
required LLDBLogForwarder lldbLogForwarder,
215+
required bool manualContinue,
183216
}) async {
184217
if (_lldbProcess != null) {
185218
_logger.printTrace(
@@ -196,18 +229,63 @@ return False
196229
appProcessId: appProcessId,
197230
logger: _logger,
198231
);
232+
233+
void printLine(String line) {
234+
if (_isAttached && !_ignoreLog(line)) {
235+
// Only forwards logs after LLDB is attached. All logs before then are part of the
236+
// attach process.
237+
238+
lldbLogForwarder.addLog(line);
239+
} else {
240+
_logger.printTrace('[lldb]: $line');
241+
_logCompleter?.checkForMatch(line);
242+
}
243+
}
244+
245+
var processIsStoppedAfterAttaching = false;
246+
final List<String> stopLogs = [];
199247
final StreamSubscription<String> stdoutSubscription = _lldbProcess!.stdout
200248
.transform(utf8LineDecoder)
201-
.listen((String line) {
202-
if (_isAttached && !_ignoreLog(line)) {
203-
// Only forwards logs after LLDB is attached. All logs before then are part of the
204-
// attach process.
249+
.listen((String line) async {
250+
// If not manually handling stops or if not attached yet, print the line directly.
251+
if (!manualContinue || !_isAttached) {
252+
printLine(line);
253+
return;
254+
}
205255

206-
lldbLogForwarder.addLog(line);
207-
} else {
208-
_logger.printTrace('[lldb]: $line');
209-
_logCompleter?.checkForMatch(line);
256+
// When manually continuing, listen for process stops and then handle the stop by
257+
// either continuing the process or detaching.
258+
//
259+
// When the process stops, collect logs until the target has stopped. Once the target
260+
// has stopped, check if it stopped due to a breakpoint. If it has, continue and
261+
// process and discard the logs after the process resumes. If the stop is not caused by
262+
// a breakpoint, print the collected logs, get the stack trace, and detach.
263+
if (line.contains(_lldbProcessStopped)) {
264+
processIsStoppedAfterAttaching = true;
265+
}
266+
if (processIsStoppedAfterAttaching) {
267+
stopLogs.add(line);
268+
}
269+
if (line.contains(_targetStoppedPattern)) {
270+
if (stopLogs.any((log) => log.contains('stop reason = breakpoint'))) {
271+
await _lldbProcess?.stdinWriteln(_processContinueCommand);
272+
} else {
273+
stopLogs.forEach(printLine);
274+
await _lldbProcess?.stdinWriteln(_threadBacktraceAllCommand);
275+
await _lldbProcess?.stdinWriteln(_detachCommand);
276+
processIsStoppedAfterAttaching = false;
277+
}
278+
return;
210279
}
280+
if (processIsStoppedAfterAttaching) {
281+
if (line.contains(_lldbProcessResuming)) {
282+
processIsStoppedAfterAttaching = false;
283+
stopLogs.clear();
284+
}
285+
return;
286+
}
287+
288+
printLine(line);
211289
});
212290

213291
final StreamSubscription<String> stderrSubscription = _lldbProcess!.stderr
@@ -294,14 +372,17 @@ return False
294372

295373
/// Sets a breakpoint, waits for it print the breakpoint id, and adds a python
296374
/// script command to be executed whenever the breakpoint is hit.
297-
Future<void> _setBreakpoint() async {
375+
Future<void> _setBreakpoint(bool manualContinue) async {
298376
final Future<String> futureLog = _startWaitingForLog(
299377
_breakpointPattern,
300378
).then((value) => value, onError: _handleAsyncError);
301379

302-
await _lldbProcess?.stdinWriteln(
303-
r"breakpoint set --func-regex '^NOTIFY_DEBUGGER_ABOUT_RX_PAGES$'",
304-
);
380+
final breakpointSetCommand = manualContinue
381+
? r"breakpoint set --func-regex '^NOTIFY_DEBUGGER_ABOUT_RX_PAGES$'"
382+
: r"breakpoint set --auto-continue true --func-regex '^NOTIFY_DEBUGGER_ABOUT_RX_PAGES$'";
383+
384+
await _lldbProcess?.stdinWriteln(breakpointSetCommand);
385+
305386
final String log = await futureLog;
306387
final Match? match = _breakpointPattern.firstMatch(log);
307388
final String? breakpointId = match?.group(1);
@@ -330,7 +411,7 @@ return False
330411
mode == BuildMode.debug ? _lldbBreakpointAdded : _lldbProcessResuming,
331412
).then((value) => value, onError: _handleAsyncError);
332413

333-
await _lldbProcess?.stdinWriteln('process continue');
414+
await _lldbProcess?.stdinWriteln(_processContinueCommand);
334415
await futureLog;
335416
}
336417

@@ -342,7 +423,9 @@ return False
342423
final Future<String> futureLog = _startWaitingForLog(
343424
_stopHookAddedPattern,
344425
).then((value) => value, onError: _handleAsyncError);
345-
await _lldbProcess?.stdinWriteln('target stop-hook add -o "thread backtrace all" -o "detach"');
426+
await _lldbProcess?.stdinWriteln(
427+
'target stop-hook add -o "$_threadBacktraceAllCommand" -o "$_detachCommand"',
428+
);
346429
await futureLog;
347430
}
348431

‎packages/flutter_tools/lib/src/macos/xcdevice.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ class XCDevice {
657657
fileSystem: globals.fs,
658658
processUtils: _processUtils,
659659
xcodeProjectInterpreter: globals.xcodeProjectInterpreter!,
660+
deviceVersion: Version.parse(sdkVersionString),
660661
),
661662
xcodeDebug: _xcodeDebug,
662663
xcode: _xcode,

0 commit comments

Comments
 (0)