Pass debugger messages between V8 debugger.
This commit is contained in:
parent
ed7d430f2b
commit
3795cc58b0
3 changed files with 81 additions and 7 deletions
|
@ -12,6 +12,7 @@
|
||||||
#include "atom/common/api/atom_bindings.h"
|
#include "atom/common/api/atom_bindings.h"
|
||||||
#include "atom/common/node_bindings.h"
|
#include "atom/common/node_bindings.h"
|
||||||
#include "base/command_line.h"
|
#include "base/command_line.h"
|
||||||
|
#include "v8/include/v8-debug.h"
|
||||||
|
|
||||||
#if defined(USE_X11)
|
#if defined(USE_X11)
|
||||||
#include "chrome/browser/ui/libgtk2ui/gtk2_util.h"
|
#include "chrome/browser/ui/libgtk2ui/gtk2_util.h"
|
||||||
|
@ -55,11 +56,15 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
|
||||||
node_bindings_->Initialize();
|
node_bindings_->Initialize();
|
||||||
|
|
||||||
// Support the "--debug" switch.
|
// Support the "--debug" switch.
|
||||||
node_debugger_.reset(new NodeDebugger);
|
node_debugger_.reset(new NodeDebugger(js_env_->isolate()));
|
||||||
|
|
||||||
// Create the global environment.
|
// Create the global environment.
|
||||||
global_env = node_bindings_->CreateEnvironment(js_env_->context());
|
global_env = node_bindings_->CreateEnvironment(js_env_->context());
|
||||||
|
|
||||||
|
// Make sure node can get correct environment when debugging.
|
||||||
|
if (node_debugger_->IsRunning())
|
||||||
|
global_env->AssignToContext(v8::Debug::GetDebugContext());
|
||||||
|
|
||||||
// Add atom-shell extended APIs.
|
// Add atom-shell extended APIs.
|
||||||
atom_bindings_->BindTo(js_env_->isolate(), global_env->process_object());
|
atom_bindings_->BindTo(js_env_->isolate(), global_env->process_object());
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,17 +6,29 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "atom/common/atom_version.h"
|
|
||||||
#include "base/bind.h"
|
#include "base/bind.h"
|
||||||
#include "base/command_line.h"
|
#include "base/command_line.h"
|
||||||
#include "base/strings/string_number_conversions.h"
|
#include "base/strings/string_number_conversions.h"
|
||||||
|
#include "base/strings/stringprintf.h"
|
||||||
|
#include "base/strings/utf_string_conversions.h"
|
||||||
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "net/socket/tcp_listen_socket.h"
|
#include "net/socket/tcp_listen_socket.h"
|
||||||
#include "v8/include/v8-debug.h"
|
|
||||||
|
#include "atom/common/node_includes.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
NodeDebugger::NodeDebugger()
|
namespace {
|
||||||
: thread_("NodeDebugger"),
|
|
||||||
|
// NodeDebugger is stored in Isolate's data, slots 0, 1, 3 have already been
|
||||||
|
// taken by gin, blink and node, using 2 is a safe option for now.
|
||||||
|
const int kIsolateSlot = 2;
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
NodeDebugger::NodeDebugger(v8::Isolate* isolate)
|
||||||
|
: isolate_(isolate),
|
||||||
|
thread_("NodeDebugger"),
|
||||||
content_length_(-1),
|
content_length_(-1),
|
||||||
weak_factory_(this) {
|
weak_factory_(this) {
|
||||||
bool use_debug_agent = false;
|
bool use_debug_agent = false;
|
||||||
|
@ -39,6 +51,9 @@ NodeDebugger::NodeDebugger()
|
||||||
if (!port_str.empty())
|
if (!port_str.empty())
|
||||||
base::StringToInt(port_str, &port);
|
base::StringToInt(port_str, &port);
|
||||||
|
|
||||||
|
isolate_->SetData(kIsolateSlot, this);
|
||||||
|
v8::Debug::SetMessageHandler(DebugMessageHandler);
|
||||||
|
|
||||||
// Start a new IO thread.
|
// Start a new IO thread.
|
||||||
base::Thread::Options options;
|
base::Thread::Options options;
|
||||||
options.message_loop_type = base::MessageLoop::TYPE_IO;
|
options.message_loop_type = base::MessageLoop::TYPE_IO;
|
||||||
|
@ -59,6 +74,10 @@ NodeDebugger::~NodeDebugger() {
|
||||||
thread_.Stop();
|
thread_.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool NodeDebugger::IsRunning() const {
|
||||||
|
return thread_.IsRunning();
|
||||||
|
}
|
||||||
|
|
||||||
void NodeDebugger::StartServer(int port) {
|
void NodeDebugger::StartServer(int port) {
|
||||||
server_ = net::TCPListenSocket::CreateAndListen("127.0.0.1", port, this);
|
server_ = net::TCPListenSocket::CreateAndListen("127.0.0.1", port, this);
|
||||||
if (!server_) {
|
if (!server_) {
|
||||||
|
@ -67,6 +86,43 @@ void NodeDebugger::StartServer(int port) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NodeDebugger::CloseSession() {
|
||||||
|
accepted_socket_.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void NodeDebugger::OnMessage(const std::string& message) {
|
||||||
|
if (message.find("\"type\":\"request\",\"command\":\"disconnect\"}") !=
|
||||||
|
std::string::npos)
|
||||||
|
CloseSession();
|
||||||
|
|
||||||
|
base::string16 message16 = base::UTF8ToUTF16(message);
|
||||||
|
v8::Debug::SendCommand(isolate_, message16.data(), message16.size());
|
||||||
|
|
||||||
|
content::BrowserThread::PostTask(
|
||||||
|
content::BrowserThread::UI, FROM_HERE,
|
||||||
|
base::Bind(&v8::Debug::ProcessDebugMessages));
|
||||||
|
}
|
||||||
|
|
||||||
|
void NodeDebugger::SendMessage(const std::string& message) {
|
||||||
|
if (accepted_socket_) {
|
||||||
|
std::string header = base::StringPrintf("Content-Length: %d\r\n\r\n",
|
||||||
|
static_cast<int>(message.size()));
|
||||||
|
accepted_socket_->Send(header);
|
||||||
|
accepted_socket_->Send(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
void NodeDebugger::DebugMessageHandler(const v8::Debug::Message& message) {
|
||||||
|
NodeDebugger* self = static_cast<NodeDebugger*>(
|
||||||
|
message.GetIsolate()->GetData(kIsolateSlot));
|
||||||
|
|
||||||
|
if (self) {
|
||||||
|
std::string message8(*v8::String::Utf8Value(message.GetJSON()));
|
||||||
|
self->SendMessage(message8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void NodeDebugger::DidAccept(net::StreamListenSocket* server,
|
void NodeDebugger::DidAccept(net::StreamListenSocket* server,
|
||||||
scoped_ptr<net::StreamListenSocket> socket) {
|
scoped_ptr<net::StreamListenSocket> socket) {
|
||||||
// Only accept one session.
|
// Only accept one session.
|
||||||
|
@ -109,6 +165,8 @@ void NodeDebugger::DidRead(net::StreamListenSocket* socket,
|
||||||
std::string message = buffer_.substr(0, content_length_);
|
std::string message = buffer_.substr(0, content_length_);
|
||||||
buffer_ = buffer_.substr(content_length_);
|
buffer_ = buffer_.substr(content_length_);
|
||||||
|
|
||||||
|
OnMessage(message);
|
||||||
|
|
||||||
// Get ready for next message.
|
// Get ready for next message.
|
||||||
content_length_ = -1;
|
content_length_ = -1;
|
||||||
}
|
}
|
||||||
|
@ -116,7 +174,8 @@ void NodeDebugger::DidRead(net::StreamListenSocket* socket,
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeDebugger::DidClose(net::StreamListenSocket* socket) {
|
void NodeDebugger::DidClose(net::StreamListenSocket* socket) {
|
||||||
accepted_socket_.reset();
|
// If we lost the connection, then simulate a disconnect msg:
|
||||||
|
OnMessage("{\"seq\":1,\"type\":\"request\",\"command\":\"disconnect\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
|
@ -11,17 +11,25 @@
|
||||||
#include "base/memory/weak_ptr.h"
|
#include "base/memory/weak_ptr.h"
|
||||||
#include "base/threading/thread.h"
|
#include "base/threading/thread.h"
|
||||||
#include "net/socket/stream_listen_socket.h"
|
#include "net/socket/stream_listen_socket.h"
|
||||||
|
#include "v8/include/v8-debug.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
// Add support for node's "--debug" switch.
|
// Add support for node's "--debug" switch.
|
||||||
class NodeDebugger : public net::StreamListenSocket::Delegate {
|
class NodeDebugger : public net::StreamListenSocket::Delegate {
|
||||||
public:
|
public:
|
||||||
NodeDebugger();
|
explicit NodeDebugger(v8::Isolate* isolate);
|
||||||
virtual ~NodeDebugger();
|
virtual ~NodeDebugger();
|
||||||
|
|
||||||
|
bool IsRunning() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void StartServer(int port);
|
void StartServer(int port);
|
||||||
|
void CloseSession();
|
||||||
|
void OnMessage(const std::string& message);
|
||||||
|
void SendMessage(const std::string& message);
|
||||||
|
|
||||||
|
static void DebugMessageHandler(const v8::Debug::Message& message);
|
||||||
|
|
||||||
// net::StreamListenSocket::Delegate:
|
// net::StreamListenSocket::Delegate:
|
||||||
virtual void DidAccept(net::StreamListenSocket* server,
|
virtual void DidAccept(net::StreamListenSocket* server,
|
||||||
|
@ -31,6 +39,8 @@ class NodeDebugger : public net::StreamListenSocket::Delegate {
|
||||||
int len) OVERRIDE;
|
int len) OVERRIDE;
|
||||||
virtual void DidClose(net::StreamListenSocket* socket) OVERRIDE;
|
virtual void DidClose(net::StreamListenSocket* socket) OVERRIDE;
|
||||||
|
|
||||||
|
v8::Isolate* isolate_;
|
||||||
|
|
||||||
base::Thread thread_;
|
base::Thread thread_;
|
||||||
scoped_ptr<net::StreamListenSocket> server_;
|
scoped_ptr<net::StreamListenSocket> server_;
|
||||||
scoped_ptr<net::StreamListenSocket> accepted_socket_;
|
scoped_ptr<net::StreamListenSocket> accepted_socket_;
|
||||||
|
|
Loading…
Reference in a new issue