@@ -37,20 +37,30 @@ static const uint8_t PROTOCOL_JSON[] = {
37
37
#include " v8_inspector_protocol_json.h" // NOLINT(build/include_order)
38
38
};
39
39
40
- std::string GetWsUrl (int port , const std::string& id) {
40
+ std::string GetWsUrl (const sockaddr_in& address , const std::string& id) {
41
41
char buf[1024 ];
42
- snprintf (buf, sizeof (buf), " 127.0.0.1:%d/%s" , port, id.c_str ());
42
+ char name[64 ];
43
+
44
+ if (uv_ip4_name (&address, name, sizeof (name))) *name = ' \0 ' ;
45
+ const int port = ntohs (address.sin_port );
46
+ snprintf (buf, sizeof (buf), " %s:%d/%s" , name, port, id.c_str ());
47
+
43
48
return buf;
44
49
}
45
50
46
- void PrintDebuggerReadyMessage (int port, const std::string& id) {
47
- fprintf (stderr, " Debugger listening on port %d.\n "
51
+ void PrintDebuggerReadyMessage (const sockaddr_in& address,
52
+ const std::string& id) {
53
+ const std::string ws_url = GetWsUrl (address, id);
54
+ const size_t slash_pos = ws_url.find (' /' );
55
+ CHECK_NE (slash_pos, std::string::npos);
56
+
57
+ fprintf (stderr, " Debugger listening on %.*s.\n "
48
58
" Warning: This is an experimental feature and could change at any time.\n "
49
59
" To start debugging, open the following URL in Chrome:\n "
50
60
" chrome-devtools://devtools/remote/serve_file/"
51
61
" @" V8_INSPECTOR_REVISION " /inspector.html?"
52
62
" experiments=true&v8only=true&ws=%s\n " ,
53
- port, GetWsUrl (port, id) .c_str ());
63
+ static_cast < int >(slash_pos), ws_url. c_str (), ws_url .c_str ());
54
64
fflush (stderr);
55
65
}
56
66
@@ -187,7 +197,8 @@ class AgentImpl {
187
197
~AgentImpl ();
188
198
189
199
// Start the inspector agent thread
190
- bool Start (v8::Platform* platform, const char * path, int port, bool wait);
200
+ bool Start (v8::Platform* platform, const char * path,
201
+ const char * host, int port, bool wait);
191
202
// Stop the inspector agent
192
203
void Stop ();
193
204
@@ -234,7 +245,7 @@ class AgentImpl {
234
245
uv_thread_t thread_;
235
246
uv_loop_t child_loop_;
236
247
237
- int port_ ;
248
+ struct sockaddr_in saddr_ ;
238
249
bool wait_;
239
250
bool shutting_down_;
240
251
State state_;
@@ -380,7 +391,7 @@ class V8NodeInspector : public v8_inspector::V8InspectorClient {
380
391
std::unique_ptr<v8_inspector::V8InspectorSession> session_;
381
392
};
382
393
383
- AgentImpl::AgentImpl (Environment* env) : port_( 0 ),
394
+ AgentImpl::AgentImpl (Environment* env) : saddr_( ),
384
395
wait_(false ),
385
396
shutting_down_(false ),
386
397
state_(State::kNew ),
@@ -473,7 +484,10 @@ void InspectorWrapConsoleCall(const v8::FunctionCallbackInfo<v8::Value>& args) {
473
484
}
474
485
475
486
bool AgentImpl::Start (v8::Platform* platform, const char * path,
476
- int port, bool wait) {
487
+ const char * address, int port, bool wait) {
488
+ if (*address == ' \0 ' ) address = " 127.0.0.1" ;
489
+ if (uv_ip4_addr (address, port, &saddr_)) return false ;
490
+
477
491
auto env = parent_env_;
478
492
inspector_ = new V8NodeInspector (this , env, platform);
479
493
platform_ = platform;
@@ -485,7 +499,6 @@ bool AgentImpl::Start(v8::Platform* platform, const char* path,
485
499
int err = uv_loop_init (&child_loop_);
486
500
CHECK_EQ (err, 0 );
487
501
488
- port_ = port;
489
502
wait_ = wait ;
490
503
491
504
err = uv_thread_create (&thread_, AgentImpl::ThreadCbIO, this );
@@ -661,7 +674,7 @@ void AgentImpl::SendListResponse(InspectorSocket* socket) {
661
674
Escape (&response[" url" ]);
662
675
663
676
if (!client_socket_) {
664
- std::string address = GetWsUrl (port_ , id_);
677
+ std::string address = GetWsUrl (saddr_ , id_);
665
678
666
679
std::ostringstream frontend_url;
667
680
frontend_url << " https://chrome-devtools-frontend.appspot.com/serve_file/@" ;
@@ -715,7 +728,6 @@ void AgentImpl::WriteCbIO(uv_async_t* async) {
715
728
}
716
729
717
730
void AgentImpl::WorkerRunIO () {
718
- sockaddr_in addr;
719
731
uv_tcp_t server;
720
732
int err = uv_loop_init (&child_loop_);
721
733
CHECK_EQ (err, 0 );
@@ -729,14 +741,18 @@ void AgentImpl::WorkerRunIO() {
729
741
uv_fs_req_cleanup (&req);
730
742
}
731
743
uv_tcp_init (&child_loop_, &server);
732
- uv_ip4_addr (" 0.0.0.0" , port_, &addr);
733
744
server.data = this ;
734
745
err = uv_tcp_bind (&server,
735
- reinterpret_cast <const struct sockaddr *>(&addr ), 0 );
746
+ reinterpret_cast <const struct sockaddr *>(&saddr_ ), 0 );
736
747
if (err == 0 ) {
737
748
err = uv_listen (reinterpret_cast <uv_stream_t *>(&server), 1 ,
738
749
OnSocketConnectionIO);
739
750
}
751
+ if (err == 0 ) {
752
+ int namelen = sizeof (saddr_);
753
+ err = uv_tcp_getsockname (&server, reinterpret_cast <sockaddr*>(&saddr_),
754
+ &namelen);
755
+ }
740
756
if (err != 0 ) {
741
757
fprintf (stderr, " Unable to open devtools socket: %s\n " , uv_strerror (err));
742
758
state_ = State::kError ; // Safe, main thread is waiting on semaphore
@@ -746,7 +762,7 @@ void AgentImpl::WorkerRunIO() {
746
762
uv_sem_post (&start_sem_);
747
763
return ;
748
764
}
749
- PrintDebuggerReadyMessage (port_ , id_);
765
+ PrintDebuggerReadyMessage (saddr_ , id_);
750
766
if (!wait_) {
751
767
uv_sem_post (&start_sem_);
752
768
}
@@ -830,7 +846,7 @@ void AgentImpl::DispatchMessages() {
830
846
if (shutting_down_) {
831
847
state_ = State::kDone ;
832
848
} else {
833
- PrintDebuggerReadyMessage (port_ , id_);
849
+ PrintDebuggerReadyMessage (saddr_ , id_);
834
850
state_ = State::kAccepting ;
835
851
}
836
852
inspector_->quitMessageLoopOnPause ();
@@ -858,8 +874,8 @@ Agent::~Agent() {
858
874
}
859
875
860
876
bool Agent::Start (v8::Platform* platform, const char * path,
861
- int port, bool wait) {
862
- return impl->Start (platform, path, port, wait );
877
+ const char * host, int port, bool wait) {
878
+ return impl->Start (platform, path, host, port, wait );
863
879
}
864
880
865
881
void Agent::Stop () {
0 commit comments