@@ -40,8 +40,8 @@ using v8_inspector::StringBuffer;
40
40
using v8_inspector::StringView;
41
41
using v8_inspector::V8Inspector;
42
42
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 ;
45
45
46
46
class StartIoTask : public v8 ::Task {
47
47
public:
@@ -61,36 +61,36 @@ std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
61
61
return StringBuffer::create (StringView (*buffer, buffer.length ()));
62
62
}
63
63
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) {
66
66
static_cast <Agent*>(handle->data )->StartIoThread (false );
67
67
}
68
68
69
- void StartIoCallback (Isolate* isolate, void * agent) {
69
+ void StartIoInterrupt (Isolate* isolate, void * agent) {
70
70
static_cast <Agent*>(agent)->StartIoThread (false );
71
71
}
72
72
73
73
74
74
#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 );
77
77
}
78
78
79
- inline void * InspectorIoThreadSignalThreadMain (void * unused) {
79
+ inline void * StartIoThreadMain (void * unused) {
80
80
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 );
83
83
if (agent != nullptr )
84
- agent->RequestIoStart ();
84
+ agent->RequestIoThreadStart ();
85
85
}
86
86
return nullptr ;
87
87
}
88
88
89
- static int RegisterDebugSignalHandler () {
89
+ static int StartDebugSignalHandler () {
90
90
// Start a watchdog thread for calling v8::Debug::DebugBreak() because
91
91
// it's not safe to call directly from the signal handler, it can
92
92
// 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 ));
94
94
pthread_attr_t attr;
95
95
CHECK_EQ (0 , pthread_attr_init (&attr));
96
96
// Don't shrink the thread's stack on FreeBSD. Said platform decided to
@@ -101,11 +101,13 @@ static int RegisterDebugSignalHandler() {
101
101
#endif // __FreeBSD__
102
102
CHECK_EQ (0 , pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
103
103
sigset_t sigmask;
104
+ // Mask all signals.
104
105
sigfillset (&sigmask);
105
106
CHECK_EQ (0 , pthread_sigmask (SIG_SETMASK, &sigmask, &sigmask));
106
107
pthread_t thread;
107
108
const int err = pthread_create (&thread, &attr,
108
- InspectorIoThreadSignalThreadMain, nullptr );
109
+ StartIoThreadMain, nullptr );
110
+ // Restore original mask
109
111
CHECK_EQ (0 , pthread_sigmask (SIG_SETMASK, &sigmask, nullptr ));
110
112
CHECK_EQ (0 , pthread_attr_destroy (&attr));
111
113
if (err != 0 ) {
@@ -115,7 +117,7 @@ static int RegisterDebugSignalHandler() {
115
117
// receiving the signal would terminate the process.
116
118
return -err;
117
119
}
118
- RegisterSignalHandler (SIGUSR1, EnableInspectorIOThreadSignalHandler );
120
+ RegisterSignalHandler (SIGUSR1, StartIoThreadWakeup );
119
121
// Unblock SIGUSR1. A pending SIGUSR1 signal will now be delivered.
120
122
sigemptyset (&sigmask);
121
123
sigaddset (&sigmask, SIGUSR1);
@@ -126,10 +128,10 @@ static int RegisterDebugSignalHandler() {
126
128
127
129
128
130
#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 );
131
133
if (agent != nullptr )
132
- agent->RequestIoStart ();
134
+ agent->RequestIoThreadStart ();
133
135
return 0 ;
134
136
}
135
137
@@ -138,7 +140,7 @@ static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
138
140
return _snwprintf (buf, buf_len, L" node-debug-handler-%u" , pid);
139
141
}
140
142
141
- static int RegisterDebugSignalHandler () {
143
+ static int StartDebugSignalHandler () {
142
144
wchar_t mapping_name[32 ];
143
145
HANDLE mapping_handle;
144
146
DWORD pid;
@@ -173,7 +175,7 @@ static int RegisterDebugSignalHandler() {
173
175
return -1 ;
174
176
}
175
177
176
- *handler = EnableDebugThreadProc ;
178
+ *handler = StartIoThreadProc ;
177
179
178
180
UnmapViewOfFile (static_cast <void *>(handler));
179
181
@@ -205,7 +207,7 @@ class JsBindingsSessionDelegate : public InspectorSessionDelegate {
205
207
return false ;
206
208
}
207
209
208
- void OnMessage (const v8_inspector::StringView& message) override {
210
+ void SendMessageToFrontend (const v8_inspector::StringView& message) override {
209
211
Isolate* isolate = env_->isolate ();
210
212
v8::HandleScope handle_scope (isolate);
211
213
Context::Scope context_scope (env_->context ());
@@ -418,7 +420,7 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel {
418
420
void flushProtocolNotifications () override { }
419
421
420
422
void sendMessageToFrontend (const StringView& message) {
421
- delegate_->OnMessage (message);
423
+ delegate_->SendMessageToFrontend (message);
422
424
}
423
425
424
426
InspectorSessionDelegate* const delegate_;
@@ -434,7 +436,7 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
434
436
platform_ (platform),
435
437
terminated_(false ),
436
438
running_nested_loop_(false ) {
437
- inspector_ = V8Inspector::create (env->isolate (), this );
439
+ client_ = V8Inspector::create (env->isolate (), this );
438
440
}
439
441
440
442
void runMessageLoopOnPause (int context_group_id) override {
@@ -459,11 +461,11 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
459
461
std::unique_ptr<StringBuffer> name_buffer = Utf8ToStringView (name);
460
462
v8_inspector::V8ContextInfo info (context, CONTEXT_GROUP_ID,
461
463
name_buffer->string ());
462
- inspector_ ->contextCreated (info);
464
+ client_ ->contextCreated (info);
463
465
}
464
466
465
467
void contextDestroyed (Local<Context> context) {
466
- inspector_ ->contextDestroyed (context);
468
+ client_ ->contextDestroyed (context);
467
469
}
468
470
469
471
void quitMessageLoopOnPause () override {
@@ -473,7 +475,7 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
473
475
void connectFrontend (InspectorSessionDelegate* delegate) {
474
476
CHECK_EQ (channel_, nullptr );
475
477
channel_ = std::unique_ptr<ChannelImpl>(
476
- new ChannelImpl (inspector_ .get (), delegate));
478
+ new ChannelImpl (client_ .get (), delegate));
477
479
}
478
480
479
481
void disconnectFrontend () {
@@ -507,15 +509,15 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
507
509
508
510
Isolate* isolate = context->GetIsolate ();
509
511
510
- inspector_ ->exceptionThrown (
512
+ client_ ->exceptionThrown (
511
513
context,
512
514
StringView (DETAILS, sizeof (DETAILS) - 1 ),
513
515
error,
514
516
ToProtocolString (isolate, message->Get ())->string (),
515
517
ToProtocolString (isolate, message->GetScriptResourceName ())->string (),
516
518
message->GetLineNumber (context).FromMaybe (0 ),
517
519
message->GetStartColumn (context).FromMaybe (0 ),
518
- inspector_ ->createStackTrace (stack_trace),
520
+ client_ ->createStackTrace (stack_trace),
519
521
script_id);
520
522
}
521
523
@@ -528,12 +530,12 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
528
530
v8::Platform* platform_;
529
531
bool terminated_;
530
532
bool running_nested_loop_;
531
- std::unique_ptr<V8Inspector> inspector_ ;
533
+ std::unique_ptr<V8Inspector> client_ ;
532
534
std::unique_ptr<ChannelImpl> channel_;
533
535
};
534
536
535
537
Agent::Agent (Environment* env) : parent_env_(env),
536
- inspector_ (nullptr ),
538
+ client_ (nullptr ),
537
539
platform_(nullptr ),
538
540
enabled_(false ) {}
539
541
@@ -546,18 +548,19 @@ bool Agent::Start(v8::Platform* platform, const char* path,
546
548
const DebugOptions& options) {
547
549
path_ = path == nullptr ? " " : path;
548
550
debug_options_ = options;
549
- inspector_ =
551
+ client_ =
550
552
std::unique_ptr<NodeInspectorClient>(
551
553
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" );
553
555
platform_ = platform;
554
556
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 ));
559
561
560
- RegisterDebugSignalHandler ();
562
+ // Ignore failure, SIGUSR1 won't work, but that should not block node start.
563
+ StartDebugSignalHandler ();
561
564
if (options.inspector_enabled ()) {
562
565
return StartIoThread (options.wait_for_connect ());
563
566
}
@@ -568,14 +571,14 @@ bool Agent::StartIoThread(bool wait_for_connect) {
568
571
if (io_ != nullptr )
569
572
return true ;
570
573
571
- CHECK_NE (inspector_ , nullptr );
574
+ CHECK_NE (client_ , nullptr );
572
575
573
576
enabled_ = true ;
574
577
io_ = std::unique_ptr<InspectorIo>(
575
578
new InspectorIo (parent_env_, platform_, path_, debug_options_,
576
579
wait_for_connect));
577
580
if (!io_->Start ()) {
578
- inspector_ .reset ();
581
+ client_ .reset ();
579
582
return false ;
580
583
}
581
584
@@ -612,20 +615,16 @@ void Agent::Stop() {
612
615
613
616
void Agent::Connect (InspectorSessionDelegate* delegate) {
614
617
enabled_ = true ;
615
- inspector_ ->connectFrontend (delegate);
618
+ client_ ->connectFrontend (delegate);
616
619
}
617
620
618
621
bool Agent::IsConnected () {
619
622
return io_ && io_->IsConnected ();
620
623
}
621
624
622
- bool Agent::IsStarted () {
623
- return !!inspector_;
624
- }
625
-
626
625
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 ());
629
628
if (io_ != nullptr ) {
630
629
io_->WaitForDisconnect ();
631
630
}
@@ -634,42 +633,42 @@ void Agent::WaitForDisconnect() {
634
633
void Agent::FatalException (Local<Value> error, Local<v8::Message> message) {
635
634
if (!IsStarted ())
636
635
return ;
637
- inspector_ ->FatalException (error, message);
636
+ client_ ->FatalException (error, message);
638
637
WaitForDisconnect ();
639
638
}
640
639
641
640
void Agent::Dispatch (const StringView& message) {
642
- CHECK_NE (inspector_ , nullptr );
643
- inspector_ ->dispatchMessageFromFrontend (message);
641
+ CHECK_NE (client_ , nullptr );
642
+ client_ ->dispatchMessageFromFrontend (message);
644
643
}
645
644
646
645
void Agent::Disconnect () {
647
- CHECK_NE (inspector_ , nullptr );
648
- inspector_ ->disconnectFrontend ();
646
+ CHECK_NE (client_ , nullptr );
647
+ client_ ->disconnectFrontend ();
649
648
}
650
649
651
650
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);
654
653
}
655
654
656
655
InspectorSessionDelegate* Agent::delegate () {
657
- CHECK_NE (inspector_ , nullptr );
658
- ChannelImpl* channel = inspector_ ->channel ();
656
+ CHECK_NE (client_ , nullptr );
657
+ ChannelImpl* channel = client_ ->channel ();
659
658
if (channel == nullptr )
660
659
return nullptr ;
661
660
return channel->delegate ();
662
661
}
663
662
664
663
void Agent::PauseOnNextJavascriptStatement (const std::string& reason) {
665
- ChannelImpl* channel = inspector_ ->channel ();
664
+ ChannelImpl* channel = client_ ->channel ();
666
665
if (channel != nullptr )
667
666
channel->schedulePauseOnNextStatement (reason);
668
667
}
669
668
670
669
// 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) {
673
672
Environment* env = Environment::GetCurrent (context);
674
673
Agent* agent = env->inspector_agent ();
675
674
env->SetMethod (target, " consoleCall" , InspectorConsoleCall);
@@ -678,19 +677,19 @@ void Agent::InitJSBindings(Local<Object> target, Local<Value> unused,
678
677
env->SetMethod (target, " connect" , ConnectJSBindingsSession);
679
678
}
680
679
681
- void Agent::RequestIoStart () {
680
+ void Agent::RequestIoThreadStart () {
682
681
// We need to attempt to interrupt V8 flow (in case Node is running
683
682
// continuous JS code) and to wake up libuv thread (in case Node is wating
684
683
// for IO events)
685
- uv_async_send (&start_inspector_thread_async );
684
+ uv_async_send (&start_io_thread_async );
686
685
v8::Isolate* isolate = parent_env_->isolate ();
687
686
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 );
690
689
}
691
690
692
691
} // namespace inspector
693
692
} // namespace node
694
693
695
694
NODE_MODULE_CONTEXT_AWARE_BUILTIN (inspector,
696
- node::inspector::Agent::InitJSBindings );
695
+ node::inspector::Agent::InitInspector );
0 commit comments