From f47f14717cf1d0e71dd981d126dd808f3dddcd75 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Wed, 11 May 2016 10:09:32 -0700 Subject: [PATCH 1/2] V8 inspector: provide filenames in the form of URLs to the debugger --- lib/internal/bootstrap_node.js | 2 +- lib/module.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 56cdca5796d3b3..2187bbe49e020f 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -134,7 +134,7 @@ // wrap it source = Module.wrap(source); // compile the script, this will throw if it fails - new vm.Script(source, {filename: filename, displayErrors: true}); + new vm.Script(source, {filename: 'file://' + filename, displayErrors: true}); process.exit(0); } diff --git a/lib/module.js b/lib/module.js index 344ea97af91953..ac51f7ac2ee31f 100644 --- a/lib/module.js +++ b/lib/module.js @@ -509,7 +509,7 @@ Module.prototype._compile = function(content, filename) { var wrapper = Module.wrap(content); var compiledWrapper = vm.runInThisContext(wrapper, { - filename: filename, + filename: 'file://' + filename, lineOffset: 0, displayErrors: true }); From ffc30e3a721eb2660fb6eaa8bdc81667564e8c13 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Wed, 11 May 2016 16:56:15 -0700 Subject: [PATCH 2/2] Wait for debugger to disconnect after app is done. --- src/inspector_agent.cc | 24 +++++++++++++++++++++++- src/inspector_agent.h | 6 +++++- src/inspector_socket.cc | 3 +++ src/node.cc | 9 +++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index 4577f4876663e7..78288d5cb3f0f2 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -84,7 +84,7 @@ class AsyncWriteRequest { AsyncWriteRequest(Agent* agent, const String16& message) : agent_(agent), message_(message) {} void perform() { - inspector_socket_t* socket = agent_->client_socket(); + inspector_socket_t* socket = agent_->client_socket_; if (socket) { inspector_write(socket, message_.utf8().c_str(), message_.length()); } @@ -94,6 +94,21 @@ class AsyncWriteRequest { const String16 message_; }; +class SetConnectedTask : public v8::Task { + public: + SetConnectedTask(Agent* agent, bool connected) + : agent_(agent), + connected_(connected) {} + + void Run() override { + agent_->connected_ = connected_; + } + + private: + Agent* agent_; + bool connected_; +}; + static void DisposeAsyncCb(uv_handle_t* handle) { free(handle); } @@ -171,6 +186,7 @@ static void SendTargentsListResponse(inspector_socket_t* socket) { Agent::Agent(Environment* env) : port_(5858), wait_(false), + connected_(false), parent_env_(env), client_socket_(nullptr), platform_(nullptr) { @@ -248,6 +264,10 @@ void Agent::OnRemoteData(uv_stream_t* stream, ssize_t read, const uv_buf_t* b) { agent->client_socket_ = nullptr; } DisconnectAndDispose(socket); + } else { + // EOF + agent->platform_->CallOnForegroundThread(agent->parent_env()->isolate(), + new SetConnectedTask(agent, false)); } } @@ -273,6 +293,8 @@ void Agent::OnInspectorConnection(inspector_socket_t* socket) { client_socket_ = socket; inspector_read_start(socket, OnBufferAlloc, OnRemoteData); uv_sem_post(&start_sem_); + platform_->CallOnForegroundThread(parent_env()->isolate(), + new SetConnectedTask(this, true)); } bool Agent::RespondToGet(inspector_socket_t* socket, const char* path) { diff --git a/src/inspector_agent.h b/src/inspector_agent.h index e00df80c677143..1199b9d6cc7542 100644 --- a/src/inspector_agent.h +++ b/src/inspector_agent.h @@ -37,7 +37,8 @@ class Agent { // Stop the inspector agent void Stop(); - inspector_socket_t* client_socket() { return client_socket_; } + bool connected() { return connected_; } + protected: inline node::Environment* parent_env() { return parent_env_; } void InitAdaptor(Environment* env); @@ -52,6 +53,7 @@ class Agent { int port_; bool wait_; + bool connected_; uv_thread_t thread_; node::Environment* parent_env_; @@ -82,6 +84,8 @@ class Agent { friend class ChannelImpl; friend class DispatchOnInspectorBackendTask; + friend class SetConnectedTask; + friend class AsyncWriteRequest; }; } // namespace inspector diff --git a/src/inspector_socket.cc b/src/inspector_socket.cc index ed1f9402d983b8..f780050b76ee21 100644 --- a/src/inspector_socket.cc +++ b/src/inspector_socket.cc @@ -288,6 +288,7 @@ static ws_decode_result decode_frame_hybi17(const char* buffer_begin, size_t pos = p + actual_masking_key_length + payload_length - buffer_begin; *bytes_consumed = pos; + return closed ? FRAME_CLOSE : FRAME_OK; } @@ -319,6 +320,7 @@ static void on_close_frame_written(uv_write_t* write, int status) { static void close_frame_received(inspector_socket_t* inspector) { inspector->ws_state->received_close = true; if (!inspector->ws_state->close_sent) { + invoke_read_callback(inspector, 0, 0); write_to_client(inspector, CLOSE_FRAME, sizeof(CLOSE_FRAME), on_close_frame_written); } else { @@ -423,6 +425,7 @@ int inspector_read_start(inspector_socket_t* inspector, inspector->ws_state->close_sent = false; inspector->ws_state->alloc_cb = alloc_cb; inspector->ws_state->read_cb = read_cb; + inspector->ws_state->read_cb = read_cb; int err = uv_read_start(reinterpret_cast(&inspector->client), prepare_buffer, diff --git a/src/node.cc b/src/node.cc index 4e0cdd2c73b122..a9da5b8c3af960 100644 --- a/src/node.cc +++ b/src/node.cc @@ -4430,9 +4430,18 @@ static void StartNodeInstance(void* arg) { if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0) more = true; } + } while (more == true); } +#if HAVE_INSPECTOR + if (env->inspector_agent()->connected()) + fprintf(stderr, "Waiting for the debugger to disconnect...\n"); + while (env->inspector_agent()->connected()) { + v8::platform::PumpMessageLoop(default_platform, isolate); + } +#endif + env->set_trace_sync_io(false); int exit_code = EmitExit(env);