2015-08-27 04:25:28 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/node_debugger.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/command_line.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"
|
2015-08-27 04:59:54 +00:00
|
|
|
#include "net/test/embedded_test_server/tcp_listen_socket.h"
|
2015-08-27 04:25:28 +00:00
|
|
|
|
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
const char* kContentLength = "Content-Length";
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NodeDebugger::NodeDebugger(v8::Isolate* isolate)
|
|
|
|
: isolate_(isolate),
|
|
|
|
thread_("NodeDebugger"),
|
|
|
|
content_length_(-1),
|
|
|
|
weak_factory_(this) {
|
|
|
|
bool use_debug_agent = false;
|
|
|
|
int port = 5858;
|
|
|
|
|
|
|
|
std::string port_str;
|
|
|
|
base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
|
|
|
|
if (cmd->HasSwitch("debug")) {
|
|
|
|
use_debug_agent = true;
|
|
|
|
port_str = cmd->GetSwitchValueASCII("debug");
|
2015-10-22 10:50:48 +00:00
|
|
|
} else if (cmd->HasSwitch("debug-brk")) {
|
2015-08-27 04:25:28 +00:00
|
|
|
use_debug_agent = true;
|
|
|
|
port_str = cmd->GetSwitchValueASCII("debug-brk");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_debug_agent) {
|
|
|
|
if (!port_str.empty())
|
|
|
|
base::StringToInt(port_str, &port);
|
|
|
|
|
|
|
|
isolate_->SetData(kIsolateSlot, this);
|
2016-11-30 07:30:03 +00:00
|
|
|
v8::Debug::SetMessageHandler(isolate_, DebugMessageHandler);
|
2015-08-27 04:25:28 +00:00
|
|
|
|
2016-11-30 07:30:03 +00:00
|
|
|
weak_up_ui_handle_.data = this;
|
2015-08-27 05:30:04 +00:00
|
|
|
uv_async_init(uv_default_loop(), &weak_up_ui_handle_, ProcessMessageInUI);
|
|
|
|
|
2015-08-27 04:25:28 +00:00
|
|
|
// Start a new IO thread.
|
|
|
|
base::Thread::Options options;
|
2017-04-05 09:02:06 +00:00
|
|
|
options.message_loop_type = base::MessageLoop::TYPE_IO;
|
2015-08-27 04:25:28 +00:00
|
|
|
if (!thread_.StartWithOptions(options)) {
|
|
|
|
LOG(ERROR) << "Unable to start debugger thread";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the server in new IO thread.
|
2016-11-30 07:30:03 +00:00
|
|
|
thread_.task_runner()->PostTask(
|
2015-08-27 04:25:28 +00:00
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&NodeDebugger::StartServer, weak_factory_.GetWeakPtr(),
|
|
|
|
port));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeDebugger::~NodeDebugger() {
|
|
|
|
thread_.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NodeDebugger::IsRunning() const {
|
|
|
|
return thread_.IsRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeDebugger::StartServer(int port) {
|
2015-08-27 04:59:54 +00:00
|
|
|
server_ = net::test_server::TCPListenSocket::CreateAndListen(
|
|
|
|
"127.0.0.1", port, this);
|
2015-08-27 04:25:28 +00:00
|
|
|
if (!server_) {
|
|
|
|
LOG(ERROR) << "Cannot start debugger server";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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_,
|
|
|
|
reinterpret_cast<const uint16_t*>(message16.data()), message16.size());
|
|
|
|
|
2015-08-27 05:30:04 +00:00
|
|
|
uv_async_send(&weak_up_ui_handle_);
|
2015-08-27 04:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NodeDebugger::SendMessage(const std::string& message) {
|
|
|
|
if (accepted_socket_) {
|
|
|
|
std::string header = base::StringPrintf(
|
|
|
|
"%s: %d\r\n\r\n", kContentLength, static_cast<int>(message.size()));
|
|
|
|
accepted_socket_->Send(header);
|
|
|
|
accepted_socket_->Send(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeDebugger::SendConnectMessage() {
|
|
|
|
accepted_socket_->Send(base::StringPrintf(
|
|
|
|
"Type: connect\r\n"
|
|
|
|
"V8-Version: %s\r\n"
|
|
|
|
"Protocol-Version: 1\r\n"
|
|
|
|
"Embedding-Host: %s\r\n"
|
|
|
|
"%s: 0\r\n",
|
|
|
|
v8::V8::GetVersion(), ATOM_PRODUCT_NAME, kContentLength), true);
|
|
|
|
}
|
|
|
|
|
2015-08-27 05:30:04 +00:00
|
|
|
// static
|
|
|
|
void NodeDebugger::ProcessMessageInUI(uv_async_t* handle) {
|
2016-11-30 07:30:03 +00:00
|
|
|
NodeDebugger* self = static_cast<NodeDebugger*>(handle->data);
|
|
|
|
v8::Debug::ProcessDebugMessages(self->isolate_);
|
2015-08-27 05:30:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 04:25:28 +00:00
|
|
|
// 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()));
|
2016-11-30 07:30:03 +00:00
|
|
|
self->thread_.task_runner()->PostTask(
|
2015-08-27 04:25:28 +00:00
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&NodeDebugger::SendMessage, self->weak_factory_.GetWeakPtr(),
|
|
|
|
message8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 04:59:54 +00:00
|
|
|
void NodeDebugger::DidAccept(
|
|
|
|
net::test_server::StreamListenSocket* server,
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<net::test_server::StreamListenSocket> socket) {
|
2015-08-27 04:25:28 +00:00
|
|
|
// Only accept one session.
|
|
|
|
if (accepted_socket_) {
|
|
|
|
socket->Send(std::string("Remote debugging session already active"), true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-08 14:28:53 +00:00
|
|
|
accepted_socket_ = std::move(socket);
|
2015-08-27 04:25:28 +00:00
|
|
|
SendConnectMessage();
|
|
|
|
}
|
|
|
|
|
2015-08-27 04:59:54 +00:00
|
|
|
void NodeDebugger::DidRead(net::test_server::StreamListenSocket* socket,
|
2015-08-27 04:25:28 +00:00
|
|
|
const char* data,
|
|
|
|
int len) {
|
|
|
|
buffer_.append(data, len);
|
|
|
|
|
|
|
|
do {
|
2017-03-30 19:59:18 +00:00
|
|
|
if (buffer_.empty())
|
2015-08-27 04:25:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Read the "Content-Length" header.
|
|
|
|
if (content_length_ < 0) {
|
|
|
|
size_t pos = buffer_.find("\r\n\r\n");
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We can be sure that the header is "Content-Length: xxx\r\n".
|
|
|
|
std::string content_length = buffer_.substr(16, pos - 16);
|
|
|
|
if (!base::StringToInt(content_length, &content_length_)) {
|
|
|
|
DidClose(accepted_socket_.get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip header from buffer.
|
|
|
|
buffer_ = buffer_.substr(pos + 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the message.
|
|
|
|
if (buffer_.size() >= static_cast<size_t>(content_length_)) {
|
|
|
|
std::string message = buffer_.substr(0, content_length_);
|
|
|
|
buffer_ = buffer_.substr(content_length_);
|
|
|
|
|
|
|
|
OnMessage(message);
|
|
|
|
|
|
|
|
// Get ready for next message.
|
|
|
|
content_length_ = -1;
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
2015-08-27 04:59:54 +00:00
|
|
|
void NodeDebugger::DidClose(net::test_server::StreamListenSocket* socket) {
|
2015-08-27 04:25:28 +00:00
|
|
|
// If we lost the connection, then simulate a disconnect msg:
|
|
|
|
OnMessage("{\"seq\":1,\"type\":\"request\",\"command\":\"disconnect\"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|