Skip to content

Commit 725eace

Browse files
rigor789edusperoni
authored andcommitted
fix(inspector): pause debug-brk apps before they run
The app was released about a second after a frontend connected, whatever it had enabled by then, and the pause relied on a require call, which an ES module app doesn't make, while schedulePauseOnNextStatement didn't stop on the main module. The app now waits for Runtime.runIfWaitingForDebugger, or 3 seconds for frontends that don't send it, then stops on a debugger statement before any app code runs.
1 parent 9aa0902 commit 725eace

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

‎NativeScript/inspector/JsV8InspectorClient.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
6262
void runMessageLoopOnPause(int contextGroupId) override;
6363
void quitMessageLoopOnPause() override;
6464

65-
void scheduleBreak();
65+
void pauseBeforeApp();
6666
void registerModules();
6767

6868
void consoleLog(v8::Isolate* isolate, ConsoleAPIType method,
@@ -107,7 +107,7 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
107107
uint64_t sessionConnectionId_ = 0;
108108
uint64_t frontendGeneration_ = 0; // guarded by senderMutex_
109109
bool isWaitingForDebugger_ = false;
110-
bool hasScheduledDebugBreak_ = false;
110+
bool debuggerAttachedWhileWaiting_ = false;
111111

112112
std::unique_ptr<tns::inspector::TracingAgentImpl> tracing_agent_;
113113

‎NativeScript/inspector/JsV8InspectorClient.mm‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ bool GetInt32(const json& value, long long& out) {
254254
do {
255255
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
256256
} while (this->isWaitingForDebugger_);
257+
if (this->debuggerAttachedWhileWaiting_) {
258+
this->pauseBeforeApp();
259+
}
257260
});
258261
CFRunLoopWakeUp(CFRunLoopGetMain());
259262
});
@@ -309,14 +312,13 @@ bool GetInt32(const json& value, long long& out) {
309312
std::function<void(const std::string&)> sender) {
310313
std::lock_guard<std::mutex> lifecycle(this->frontendLifecycleMutex_);
311314

312-
if (this->isWaitingForDebugger_) {
313-
this->isWaitingForDebugger_ = NO;
314-
CFRunLoopRef runloop = CFRunLoopGetMain();
315-
CFRunLoopPerformBlock(runloop, (__bridge CFTypeRef)(NSRunLoopCommonModes), ^{
316-
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1, false);
317-
this->scheduleBreak();
315+
if (this->isWaitingForDebugger_ && !this->debuggerAttachedWhileWaiting_) {
316+
this->debuggerAttachedWhileWaiting_ = true;
317+
// Frontends that don't send Runtime.runIfWaitingForDebugger still get the app
318+
// going, once they've had time to enable the debugger.
319+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 3), dispatch_get_main_queue(), ^{
320+
this->isWaitingForDebugger_ = NO;
318321
});
319-
CFRunLoopWakeUp(runloop);
320322
}
321323

322324
bool replacesFrontend;
@@ -854,6 +856,13 @@ bool GetInt32(const json& value, long long& out) {
854856
auto json_message = json::parse(message);
855857
std::string method = json_message["method"];
856858

859+
// The frontend's last message before it expects the app to run, so it arrives
860+
// after the Debugger.enable that the pause depends on. The Runtime domain still
861+
// answers it below.
862+
if (method == "Runtime.runIfWaitingForDebugger") {
863+
this->isWaitingForDebugger_ = NO;
864+
}
865+
857866
// Note: the Tracing domain is handled in onFrontendMessageReceived, so it
858867
// never waits on the message queue or takes the Locker below.
859868

@@ -1242,26 +1251,21 @@ bool GetInt32(const json& value, long long& out) {
12421251
return result;
12431252
}
12441253

1245-
void JsV8InspectorClient::scheduleBreak() {
1254+
void JsV8InspectorClient::pauseBeforeApp() {
12461255
Isolate* isolate = isolate_;
12471256
v8::Locker locker(isolate);
12481257
Isolate::Scope isolate_scope(isolate);
12491258
HandleScope handle_scope(isolate);
1250-
auto context = isolate->GetCurrentContext();
1259+
Local<Context> context = tns::Caches::Get(isolate)->GetContext();
12511260
Context::Scope context_scope(context);
12521261

1253-
if (!this->hasScheduledDebugBreak_) {
1254-
this->hasScheduledDebugBreak_ = true;
1255-
// hack: force a debugger; statement in ModuleInternal to actually break before loading the next
1256-
// (main) script...
1257-
// FIXME: find a proper fix to not need to resort to this hack.
1258-
context->Global()
1259-
->Set(context, tns::ToV8String(isolate, "__pauseOnNextRequire"),
1260-
v8::Boolean::New(isolate, true))
1261-
.ToChecked();
1262+
// schedulePauseOnNextStatement doesn't break on the evaluation of the main
1263+
// module, and an ES module has no require call to stop at either. This stops
1264+
// before any app code has run, so its breakpoints can still be set.
1265+
Local<Script> script;
1266+
if (Script::Compile(context, tns::ToV8String(isolate, "debugger;")).ToLocal(&script)) {
1267+
script->Run(context).IsEmpty();
12621268
}
1263-
1264-
this->session_->schedulePauseOnNextStatement({}, {});
12651269
}
12661270

12671271
void JsV8InspectorClient::registerModules() {

0 commit comments

Comments
 (0)