Skip to content

Commit 26ab769

Browse files
sam-githubjasnell
authored andcommitted
inspector: refactor to rename and comment methods
Pure refactor, makes no functional changes but the renaming helped me see more clearly what the relationship was between methods and variables. * Renamed methods to reduce number of slightly different names for the same thing ("thread" vs "io thread", etc.). * Added comments where it was useful to me. PR-URL: #13321 Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ce5745b commit 26ab769

11 files changed

+278
-213
lines changed

src/inspector_agent.cc

+62-63
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ using v8_inspector::StringBuffer;
4040
using v8_inspector::StringView;
4141
using v8_inspector::V8Inspector;
4242

43-
static uv_sem_t inspector_io_thread_semaphore;
44-
static uv_async_t start_inspector_thread_async;
43+
static uv_sem_t start_io_thread_semaphore;
44+
static uv_async_t start_io_thread_async;
4545

4646
class StartIoTask : public v8::Task {
4747
public:
@@ -61,36 +61,36 @@ std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
6161
return StringBuffer::create(StringView(*buffer, buffer.length()));
6262
}
6363

64-
// Called from the main thread.
65-
void StartInspectorIoThreadAsyncCallback(uv_async_t* handle) {
64+
// Called on the main thread.
65+
void StartIoThreadAsyncCallback(uv_async_t* handle) {
6666
static_cast<Agent*>(handle->data)->StartIoThread(false);
6767
}
6868

69-
void StartIoCallback(Isolate* isolate, void* agent) {
69+
void StartIoInterrupt(Isolate* isolate, void* agent) {
7070
static_cast<Agent*>(agent)->StartIoThread(false);
7171
}
7272

7373

7474
#ifdef __POSIX__
75-
static void EnableInspectorIOThreadSignalHandler(int signo) {
76-
uv_sem_post(&inspector_io_thread_semaphore);
75+
static void StartIoThreadWakeup(int signo) {
76+
uv_sem_post(&start_io_thread_semaphore);
7777
}
7878

79-
inline void* InspectorIoThreadSignalThreadMain(void* unused) {
79+
inline void* StartIoThreadMain(void* unused) {
8080
for (;;) {
81-
uv_sem_wait(&inspector_io_thread_semaphore);
82-
Agent* agent = static_cast<Agent*>(start_inspector_thread_async.data);
81+
uv_sem_wait(&start_io_thread_semaphore);
82+
Agent* agent = static_cast<Agent*>(start_io_thread_async.data);
8383
if (agent != nullptr)
84-
agent->RequestIoStart();
84+
agent->RequestIoThreadStart();
8585
}
8686
return nullptr;
8787
}
8888

89-
static int RegisterDebugSignalHandler() {
89+
static int StartDebugSignalHandler() {
9090
// Start a watchdog thread for calling v8::Debug::DebugBreak() because
9191
// it's not safe to call directly from the signal handler, it can
9292
// deadlock with the thread it interrupts.
93-
CHECK_EQ(0, uv_sem_init(&inspector_io_thread_semaphore, 0));
93+
CHECK_EQ(0, uv_sem_init(&start_io_thread_semaphore, 0));
9494
pthread_attr_t attr;
9595
CHECK_EQ(0, pthread_attr_init(&attr));
9696
// Don't shrink the thread's stack on FreeBSD. Said platform decided to
@@ -101,11 +101,13 @@ static int RegisterDebugSignalHandler() {
101101
#endif // __FreeBSD__
102102
CHECK_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
103103
sigset_t sigmask;
104+
// Mask all signals.
104105
sigfillset(&sigmask);
105106
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, &sigmask));
106107
pthread_t thread;
107108
const int err = pthread_create(&thread, &attr,
108-
InspectorIoThreadSignalThreadMain, nullptr);
109+
StartIoThreadMain, nullptr);
110+
// Restore original mask
109111
CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr));
110112
CHECK_EQ(0, pthread_attr_destroy(&attr));
111113
if (err != 0) {
@@ -115,7 +117,7 @@ static int RegisterDebugSignalHandler() {
115117
// receiving the signal would terminate the process.
116118
return -err;
117119
}
118-
RegisterSignalHandler(SIGUSR1, EnableInspectorIOThreadSignalHandler);
120+
RegisterSignalHandler(SIGUSR1, StartIoThreadWakeup);
119121
// Unblock SIGUSR1. A pending SIGUSR1 signal will now be delivered.
120122
sigemptyset(&sigmask);
121123
sigaddset(&sigmask, SIGUSR1);
@@ -126,10 +128,10 @@ static int RegisterDebugSignalHandler() {
126128

127129

128130
#ifdef _WIN32
129-
DWORD WINAPI EnableDebugThreadProc(void* arg) {
130-
Agent* agent = static_cast<Agent*>(start_inspector_thread_async.data);
131+
DWORD WINAPI StartIoThreadProc(void* arg) {
132+
Agent* agent = static_cast<Agent*>(start_io_thread_async.data);
131133
if (agent != nullptr)
132-
agent->RequestIoStart();
134+
agent->RequestIoThreadStart();
133135
return 0;
134136
}
135137

@@ -138,7 +140,7 @@ static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
138140
return _snwprintf(buf, buf_len, L"node-debug-handler-%u", pid);
139141
}
140142

141-
static int RegisterDebugSignalHandler() {
143+
static int StartDebugSignalHandler() {
142144
wchar_t mapping_name[32];
143145
HANDLE mapping_handle;
144146
DWORD pid;
@@ -173,7 +175,7 @@ static int RegisterDebugSignalHandler() {
173175
return -1;
174176
}
175177

176-
*handler = EnableDebugThreadProc;
178+
*handler = StartIoThreadProc;
177179

178180
UnmapViewOfFile(static_cast<void*>(handler));
179181

@@ -205,7 +207,7 @@ class JsBindingsSessionDelegate : public InspectorSessionDelegate {
205207
return false;
206208
}
207209

208-
void OnMessage(const v8_inspector::StringView& message) override {
210+
void SendMessageToFrontend(const v8_inspector::StringView& message) override {
209211
Isolate* isolate = env_->isolate();
210212
v8::HandleScope handle_scope(isolate);
211213
Context::Scope context_scope(env_->context());
@@ -418,7 +420,7 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel {
418420
void flushProtocolNotifications() override { }
419421

420422
void sendMessageToFrontend(const StringView& message) {
421-
delegate_->OnMessage(message);
423+
delegate_->SendMessageToFrontend(message);
422424
}
423425

424426
InspectorSessionDelegate* const delegate_;
@@ -434,7 +436,7 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
434436
platform_(platform),
435437
terminated_(false),
436438
running_nested_loop_(false) {
437-
inspector_ = V8Inspector::create(env->isolate(), this);
439+
client_ = V8Inspector::create(env->isolate(), this);
438440
}
439441

440442
void runMessageLoopOnPause(int context_group_id) override {
@@ -459,11 +461,11 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
459461
std::unique_ptr<StringBuffer> name_buffer = Utf8ToStringView(name);
460462
v8_inspector::V8ContextInfo info(context, CONTEXT_GROUP_ID,
461463
name_buffer->string());
462-
inspector_->contextCreated(info);
464+
client_->contextCreated(info);
463465
}
464466

465467
void contextDestroyed(Local<Context> context) {
466-
inspector_->contextDestroyed(context);
468+
client_->contextDestroyed(context);
467469
}
468470

469471
void quitMessageLoopOnPause() override {
@@ -473,7 +475,7 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
473475
void connectFrontend(InspectorSessionDelegate* delegate) {
474476
CHECK_EQ(channel_, nullptr);
475477
channel_ = std::unique_ptr<ChannelImpl>(
476-
new ChannelImpl(inspector_.get(), delegate));
478+
new ChannelImpl(client_.get(), delegate));
477479
}
478480

479481
void disconnectFrontend() {
@@ -507,15 +509,15 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
507509

508510
Isolate* isolate = context->GetIsolate();
509511

510-
inspector_->exceptionThrown(
512+
client_->exceptionThrown(
511513
context,
512514
StringView(DETAILS, sizeof(DETAILS) - 1),
513515
error,
514516
ToProtocolString(isolate, message->Get())->string(),
515517
ToProtocolString(isolate, message->GetScriptResourceName())->string(),
516518
message->GetLineNumber(context).FromMaybe(0),
517519
message->GetStartColumn(context).FromMaybe(0),
518-
inspector_->createStackTrace(stack_trace),
520+
client_->createStackTrace(stack_trace),
519521
script_id);
520522
}
521523

@@ -528,12 +530,12 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
528530
v8::Platform* platform_;
529531
bool terminated_;
530532
bool running_nested_loop_;
531-
std::unique_ptr<V8Inspector> inspector_;
533+
std::unique_ptr<V8Inspector> client_;
532534
std::unique_ptr<ChannelImpl> channel_;
533535
};
534536

535537
Agent::Agent(Environment* env) : parent_env_(env),
536-
inspector_(nullptr),
538+
client_(nullptr),
537539
platform_(nullptr),
538540
enabled_(false) {}
539541

@@ -546,18 +548,19 @@ bool Agent::Start(v8::Platform* platform, const char* path,
546548
const DebugOptions& options) {
547549
path_ = path == nullptr ? "" : path;
548550
debug_options_ = options;
549-
inspector_ =
551+
client_ =
550552
std::unique_ptr<NodeInspectorClient>(
551553
new NodeInspectorClient(parent_env_, platform));
552-
inspector_->contextCreated(parent_env_->context(), "Node.js Main Context");
554+
client_->contextCreated(parent_env_->context(), "Node.js Main Context");
553555
platform_ = platform;
554556
CHECK_EQ(0, uv_async_init(uv_default_loop(),
555-
&start_inspector_thread_async,
556-
StartInspectorIoThreadAsyncCallback));
557-
start_inspector_thread_async.data = this;
558-
uv_unref(reinterpret_cast<uv_handle_t*>(&start_inspector_thread_async));
557+
&start_io_thread_async,
558+
StartIoThreadAsyncCallback));
559+
start_io_thread_async.data = this;
560+
uv_unref(reinterpret_cast<uv_handle_t*>(&start_io_thread_async));
559561

560-
RegisterDebugSignalHandler();
562+
// Ignore failure, SIGUSR1 won't work, but that should not block node start.
563+
StartDebugSignalHandler();
561564
if (options.inspector_enabled()) {
562565
return StartIoThread(options.wait_for_connect());
563566
}
@@ -568,14 +571,14 @@ bool Agent::StartIoThread(bool wait_for_connect) {
568571
if (io_ != nullptr)
569572
return true;
570573

571-
CHECK_NE(inspector_, nullptr);
574+
CHECK_NE(client_, nullptr);
572575

573576
enabled_ = true;
574577
io_ = std::unique_ptr<InspectorIo>(
575578
new InspectorIo(parent_env_, platform_, path_, debug_options_,
576579
wait_for_connect));
577580
if (!io_->Start()) {
578-
inspector_.reset();
581+
client_.reset();
579582
return false;
580583
}
581584

@@ -612,20 +615,16 @@ void Agent::Stop() {
612615

613616
void Agent::Connect(InspectorSessionDelegate* delegate) {
614617
enabled_ = true;
615-
inspector_->connectFrontend(delegate);
618+
client_->connectFrontend(delegate);
616619
}
617620

618621
bool Agent::IsConnected() {
619622
return io_ && io_->IsConnected();
620623
}
621624

622-
bool Agent::IsStarted() {
623-
return !!inspector_;
624-
}
625-
626625
void Agent::WaitForDisconnect() {
627-
CHECK_NE(inspector_, nullptr);
628-
inspector_->contextDestroyed(parent_env_->context());
626+
CHECK_NE(client_, nullptr);
627+
client_->contextDestroyed(parent_env_->context());
629628
if (io_ != nullptr) {
630629
io_->WaitForDisconnect();
631630
}
@@ -634,42 +633,42 @@ void Agent::WaitForDisconnect() {
634633
void Agent::FatalException(Local<Value> error, Local<v8::Message> message) {
635634
if (!IsStarted())
636635
return;
637-
inspector_->FatalException(error, message);
636+
client_->FatalException(error, message);
638637
WaitForDisconnect();
639638
}
640639

641640
void Agent::Dispatch(const StringView& message) {
642-
CHECK_NE(inspector_, nullptr);
643-
inspector_->dispatchMessageFromFrontend(message);
641+
CHECK_NE(client_, nullptr);
642+
client_->dispatchMessageFromFrontend(message);
644643
}
645644

646645
void Agent::Disconnect() {
647-
CHECK_NE(inspector_, nullptr);
648-
inspector_->disconnectFrontend();
646+
CHECK_NE(client_, nullptr);
647+
client_->disconnectFrontend();
649648
}
650649

651650
void Agent::RunMessageLoop() {
652-
CHECK_NE(inspector_, nullptr);
653-
inspector_->runMessageLoopOnPause(CONTEXT_GROUP_ID);
651+
CHECK_NE(client_, nullptr);
652+
client_->runMessageLoopOnPause(CONTEXT_GROUP_ID);
654653
}
655654

656655
InspectorSessionDelegate* Agent::delegate() {
657-
CHECK_NE(inspector_, nullptr);
658-
ChannelImpl* channel = inspector_->channel();
656+
CHECK_NE(client_, nullptr);
657+
ChannelImpl* channel = client_->channel();
659658
if (channel == nullptr)
660659
return nullptr;
661660
return channel->delegate();
662661
}
663662

664663
void Agent::PauseOnNextJavascriptStatement(const std::string& reason) {
665-
ChannelImpl* channel = inspector_->channel();
664+
ChannelImpl* channel = client_->channel();
666665
if (channel != nullptr)
667666
channel->schedulePauseOnNextStatement(reason);
668667
}
669668

670669
// static
671-
void Agent::InitJSBindings(Local<Object> target, Local<Value> unused,
672-
Local<Context> context, void* priv) {
670+
void Agent::InitInspector(Local<Object> target, Local<Value> unused,
671+
Local<Context> context, void* priv) {
673672
Environment* env = Environment::GetCurrent(context);
674673
Agent* agent = env->inspector_agent();
675674
env->SetMethod(target, "consoleCall", InspectorConsoleCall);
@@ -678,19 +677,19 @@ void Agent::InitJSBindings(Local<Object> target, Local<Value> unused,
678677
env->SetMethod(target, "connect", ConnectJSBindingsSession);
679678
}
680679

681-
void Agent::RequestIoStart() {
680+
void Agent::RequestIoThreadStart() {
682681
// We need to attempt to interrupt V8 flow (in case Node is running
683682
// continuous JS code) and to wake up libuv thread (in case Node is wating
684683
// for IO events)
685-
uv_async_send(&start_inspector_thread_async);
684+
uv_async_send(&start_io_thread_async);
686685
v8::Isolate* isolate = parent_env_->isolate();
687686
platform_->CallOnForegroundThread(isolate, new StartIoTask(this));
688-
isolate->RequestInterrupt(StartIoCallback, this);
689-
uv_async_send(&start_inspector_thread_async);
687+
isolate->RequestInterrupt(StartIoInterrupt, this);
688+
uv_async_send(&start_io_thread_async);
690689
}
691690

692691
} // namespace inspector
693692
} // namespace node
694693

695694
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector,
696-
node::inspector::Agent::InitJSBindings);
695+
node::inspector::Agent::InitInspector);

0 commit comments

Comments
 (0)