@@ -37,20 +37,30 @@ static const uint8_t PROTOCOL_JSON[] = {
3737#include " v8_inspector_protocol_json.h" // NOLINT(build/include_order)
3838};
3939
40- std::string GetWsUrl (int port , const std::string& id) {
40+ std::string GetWsUrl (const sockaddr_in& address , const std::string& id) {
4141 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+
4348 return buf;
4449}
4550
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 "
4858 " Warning: This is an experimental feature and could change at any time.\n "
4959 " To start debugging, open the following URL in Chrome:\n "
5060 " chrome-devtools://devtools/remote/serve_file/"
5161 " @" V8_INSPECTOR_REVISION " /inspector.html?"
5262 " 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 ());
5464 fflush (stderr);
5565}
5666
@@ -187,7 +197,8 @@ class AgentImpl {
187197 ~AgentImpl ();
188198
189199 // 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);
191202 // Stop the inspector agent
192203 void Stop ();
193204
@@ -234,7 +245,7 @@ class AgentImpl {
234245 uv_thread_t thread_;
235246 uv_loop_t child_loop_;
236247
237- int port_ ;
248+ struct sockaddr_in saddr_ ;
238249 bool wait_;
239250 bool shutting_down_;
240251 State state_;
@@ -380,7 +391,7 @@ class V8NodeInspector : public v8_inspector::V8InspectorClient {
380391 std::unique_ptr<v8_inspector::V8InspectorSession> session_;
381392};
382393
383- AgentImpl::AgentImpl (Environment* env) : port_( 0 ),
394+ AgentImpl::AgentImpl (Environment* env) : saddr_( ),
384395 wait_(false ),
385396 shutting_down_(false ),
386397 state_(State::kNew ),
@@ -473,7 +484,10 @@ void InspectorWrapConsoleCall(const v8::FunctionCallbackInfo<v8::Value>& args) {
473484}
474485
475486bool 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+
477491 auto env = parent_env_;
478492 inspector_ = new V8NodeInspector (this , env, platform);
479493 platform_ = platform;
@@ -485,7 +499,6 @@ bool AgentImpl::Start(v8::Platform* platform, const char* path,
485499 int err = uv_loop_init (&child_loop_);
486500 CHECK_EQ (err, 0 );
487501
488- port_ = port;
489502 wait_ = wait;
490503
491504 err = uv_thread_create (&thread_, AgentImpl::ThreadCbIO, this );
@@ -661,7 +674,7 @@ void AgentImpl::SendListResponse(InspectorSocket* socket) {
661674 Escape (&response[" url" ]);
662675
663676 if (!client_socket_) {
664- std::string address = GetWsUrl (port_ , id_);
677+ std::string address = GetWsUrl (saddr_ , id_);
665678
666679 std::ostringstream frontend_url;
667680 frontend_url << " https://chrome-devtools-frontend.appspot.com/serve_file/@" ;
@@ -715,7 +728,6 @@ void AgentImpl::WriteCbIO(uv_async_t* async) {
715728}
716729
717730void AgentImpl::WorkerRunIO () {
718- sockaddr_in addr;
719731 uv_tcp_t server;
720732 int err = uv_loop_init (&child_loop_);
721733 CHECK_EQ (err, 0 );
@@ -729,14 +741,18 @@ void AgentImpl::WorkerRunIO() {
729741 uv_fs_req_cleanup (&req);
730742 }
731743 uv_tcp_init (&child_loop_, &server);
732- uv_ip4_addr (" 0.0.0.0" , port_, &addr);
733744 server.data = this ;
734745 err = uv_tcp_bind (&server,
735- reinterpret_cast <const struct sockaddr *>(&addr ), 0 );
746+ reinterpret_cast <const struct sockaddr *>(&saddr_ ), 0 );
736747 if (err == 0 ) {
737748 err = uv_listen (reinterpret_cast <uv_stream_t *>(&server), 1 ,
738749 OnSocketConnectionIO);
739750 }
751+ if (err == 0 ) {
752+ int namelen = sizeof (saddr_);
753+ err = uv_tcp_getsockname (&server, reinterpret_cast <sockaddr*>(&saddr_),
754+ &namelen);
755+ }
740756 if (err != 0 ) {
741757 fprintf (stderr, " Unable to open devtools socket: %s\n " , uv_strerror (err));
742758 state_ = State::kError ; // Safe, main thread is waiting on semaphore
@@ -746,7 +762,7 @@ void AgentImpl::WorkerRunIO() {
746762 uv_sem_post (&start_sem_);
747763 return ;
748764 }
749- PrintDebuggerReadyMessage (port_ , id_);
765+ PrintDebuggerReadyMessage (saddr_ , id_);
750766 if (!wait_) {
751767 uv_sem_post (&start_sem_);
752768 }
@@ -830,7 +846,7 @@ void AgentImpl::DispatchMessages() {
830846 if (shutting_down_) {
831847 state_ = State::kDone ;
832848 } else {
833- PrintDebuggerReadyMessage (port_ , id_);
849+ PrintDebuggerReadyMessage (saddr_ , id_);
834850 state_ = State::kAccepting ;
835851 }
836852 inspector_->quitMessageLoopOnPause ();
@@ -858,8 +874,8 @@ Agent::~Agent() {
858874}
859875
860876bool 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);
863879}
864880
865881void Agent::Stop () {
0 commit comments