Commit 92aa4ab3 authored by sgjesse@chromium.org's avatar sgjesse@chromium.org

Add V8 debugger agent.

The debugger agent listens on a TCP/IP port for a remote debugger connection. When such a connection is established the debuger JSON protocol is communicated between the agent the the remote debugger. The messages containing the JSON protocol has a RFC-822 like header with a Content-Length field and with the body containing the JSON in UTF-8 encoding.

The D8 shell has option --debugger-agent to start the debugger agent.
Review URL: http://codereview.chromium.org/27355

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1405 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 3db607a3
......@@ -158,6 +158,13 @@ class EXPORT Debug {
*/
static Handle<Value> Call(v8::Handle<v8::Function> fun,
Handle<Value> data = Handle<Value>());
/**
* Enable the V8 builtin debug agent. The debugger agent will listen on the
* supplied TCP/IP port for remote debugger connection.
* \param port the TCP/IP port to listen on
*/
static bool EnableAgent(int port);
};
......
......@@ -39,11 +39,11 @@ SOURCES = {
'bootstrapper.cc', 'builtins.cc', 'checks.cc', 'code-stubs.cc',
'codegen.cc', 'compilation-cache.cc', 'compiler.cc', 'contexts.cc',
'conversions.cc', 'counters.cc', 'dateparser.cc', 'debug.cc',
'disassembler.cc', 'execution.cc', 'factory.cc', 'flags.cc',
'frames.cc', 'global-handles.cc', 'handles.cc', 'hashmap.cc', 'heap.cc',
'ic.cc', 'interpreter-irregexp.cc', 'jsregexp.cc', 'jump-target.cc',
'log.cc', 'mark-compact.cc', 'messages.cc', 'objects.cc', 'parser.cc',
'property.cc', 'regexp-macro-assembler.cc',
'debug-agent.cc', 'disassembler.cc', 'execution.cc', 'factory.cc',
'flags.cc', 'frames.cc', 'global-handles.cc', 'handles.cc', 'hashmap.cc',
'heap.cc', 'ic.cc', 'interpreter-irregexp.cc', 'jsregexp.cc',
'jump-target.cc', 'log.cc', 'mark-compact.cc', 'messages.cc', 'objects.cc',
'parser.cc', 'property.cc', 'regexp-macro-assembler.cc',
'regexp-macro-assembler-irregexp.cc', 'regexp-stack.cc',
'register-allocator.cc', 'rewriter.cc', 'runtime.cc', 'scanner.cc',
'scopeinfo.cc', 'scopes.cc', 'serialize.cc', 'snapshot-common.cc',
......
......@@ -2960,6 +2960,11 @@ Handle<Value> Debug::Call(v8::Handle<v8::Function> fun,
}
bool Debug::EnableAgent(int port) {
return i::Debugger::StartAgent(port);
}
namespace internal {
......
......@@ -573,9 +573,17 @@ int Shell::Main(int argc, char* argv[]) {
return 1;
}
}
if (i::FLAG_debugger)
// Start the debugger agent if requested.
if (i::FLAG_debugger_agent) {
v8::Debug::EnableAgent(i::FLAG_debugger_port);
}
// Start the in-process debugger if requested.
if (i::FLAG_debugger && !i::FLAG_debugger_agent)
v8::Debug::SetDebugEventListener(HandleDebugEvent);
}
if (run_shell)
RunShell();
for (int i = 0; i < threads.length(); i++) {
......
// Copyright 2009 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "v8.h"
#include "debug-agent.h"
namespace v8 { namespace internal {
// Public V8 debugger API message handler function. This function just delegates
// to the debugger agent through it's data parameter.
void DebuggerAgentMessageHandler(const uint16_t* message, int length,
void *data) {
reinterpret_cast<DebuggerAgent*>(data)->DebuggerMessage(message, length);
}
// Debugger agent main thread.
void DebuggerAgent::Run() {
// Create a server socket and bind it to the requested port.
server_ = OS::CreateSocket();
server_->Bind(port_);
while (!terminate_) {
// Listen for new connections.
server_->Listen(1);
// Accept the new connection.
Socket* client = server_->Accept();
// Create and start a new session.
CreateSession(client);
}
}
void DebuggerAgent::Shutdown() {
delete server_;
}
void DebuggerAgent::CreateSession(Socket* client) {
ScopedLock with(session_access_);
// If another session is already established terminate this one.
if (session_ != NULL) {
static const char* message = "Remote debugging session already active\n";
client->Send(message, strlen(message));
delete client;
return;
}
// Create a new session and hook up the debug message handler.
session_ = new DebuggerAgentSession(this, client);
v8::Debug::SetMessageHandler(DebuggerAgentMessageHandler, this);
session_->Start();
}
void DebuggerAgent::DebuggerMessage(const uint16_t* message, int length) {
ScopedLock with(session_access_);
// Forward the message handling to the session.
if (session_ != NULL) {
session_->DebuggerMessage(Vector<uint16_t>(const_cast<uint16_t*>(message),
length));
}
}
void DebuggerAgent::SessionClosed(DebuggerAgentSession* session) {
ScopedLock with(session_access_);
// Terminate the session.
ASSERT(session == session_);
if (session == session_) {
session->Join();
delete session;
session_ = NULL;
}
}
void DebuggerAgentSession::Run() {
while (true) {
// Read data from the debugger front end.
SmartPointer<char> message = DebuggerAgentUtil::ReceiveMessage(client_);
if (*message == NULL) {
// Session is closed.
agent_->SessionClosed(this);
return;
}
// Convert UTF-8 to UTF-16.
unibrow::Utf8InputBuffer<> buf(*message, strlen(*message));
int len = 0;
while (buf.has_more()) {
buf.GetNext();
len++;
}
int16_t* temp = NewArray<int16_t>(len + 1);
buf.Reset(*message, strlen(*message));
for (int i = 0; i < len; i++) {
temp[i] = buf.GetNext();
}
// Send the request received to the debugger.
v8::Debug::SendCommand(reinterpret_cast<const uint16_t *>(temp), len);
DeleteArray(temp);
}
}
void DebuggerAgentSession::DebuggerMessage(Vector<uint16_t> message) {
DebuggerAgentUtil::SendMessage(client_, message);
}
const char* DebuggerAgentUtil::kContentLength = "Content-Length";
int DebuggerAgentUtil::kContentLengthSize = strlen(kContentLength);
SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) {
int received;
// Read header.
const int kHeaderBufferSize = 80;
char header_buffer[kHeaderBufferSize];
int header_buffer_position = 0;
char c = '\0'; // One character receive buffer.
char last_c = '\0'; // Previous character.
int content_length = 0;
while (!(c == '\n' && last_c == '\n')) {
last_c = c;
received = conn->Receive(&c, 1);
if (received <= 0) {
PrintF("Error %d\n", Socket::LastError());
return SmartPointer<char>();
}
// Check for end of header line.
if (c == '\n') {
// Empty header line.
if (header_buffer_position == 0) {
continue;
}
// Terminate header.
ASSERT(header_buffer_position < kHeaderBufferSize);
if (header_buffer_position < kHeaderBufferSize) {
header_buffer[header_buffer_position] = '\0';
}
// Split header.
char* key = header_buffer;
char* value = NULL;
for (int i = 0; i < header_buffer_position; i++) {
if (header_buffer[i] == ':') {
header_buffer[i] = '\0';
value = header_buffer + i + 1;
while (*value == ' ') {
value++;
}
break;
}
}
// Check that key is Content-Length.
if (strcmp(key, kContentLength) == 0) {
// Get the content length value if within a sensible range.
if (strlen(value) > 7) {
return SmartPointer<char>();
}
for (int i = 0; value[i] != '\0'; i++) {
// Bail out if illegal data.
if (value[i] < '0' || value[i] > '9') {
return SmartPointer<char>();
}
content_length = 10 * content_length + (value[i] - '0');
}
}
// Start collecting new header.
header_buffer_position = 0;
} else {
// Add character to header buffer (reserve room for terminating '\0').
if (header_buffer_position < kHeaderBufferSize - 1) {
header_buffer[header_buffer_position++] = c;
}
}
}
// Read body.
char* buffer = NewArray<char>(content_length + 1);
received = ReceiveAll(conn, buffer, content_length);
if (received < content_length) {
PrintF("Error %d\n", Socket::LastError());
return SmartPointer<char>();
}
buffer[content_length] = '\0';
return SmartPointer<char>(buffer);
}
bool DebuggerAgentUtil::SendMessage(const Socket* conn,
const Vector<uint16_t> message) {
static const int kBufferSize = 80;
char buffer[kBufferSize]; // Sending buffer both for header and body.
// Calculate the message size in UTF-8 encoding.
int utf8_len = 0;
for (int i = 0; i < message.length(); i++) {
utf8_len += unibrow::Utf8::Length(message[i]);
}
// Send the header.
int len;
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"Content-Length: %d\n", utf8_len);
conn->Send(buffer, len);
// Terminate header with empty line.
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n");
conn->Send(buffer, len);
// Send message body as UTF-8.
int buffer_position = 0; // Current buffer position.
for (int i = 0; i < message.length(); i++) {
// Write next UTF-8 encoded character to buffer.
buffer_position +=
unibrow::Utf8::Encode(buffer + buffer_position, message[i]);
ASSERT(buffer_position < kBufferSize);
// Send buffer if full or last character is encoded.
if (kBufferSize - buffer_position < 3 || i == message.length() - 1) {
conn->Send(buffer, buffer_position);
buffer_position = 0;
}
}
return true;
}
bool DebuggerAgentUtil::SendMessage(const Socket* conn,
const v8::Handle<v8::String> request) {
static const int kBufferSize = 80;
char buffer[kBufferSize]; // Sending buffer both for header and body.
// Convert the request to UTF-8 encoding.
v8::String::Utf8Value utf8_request(request);
// Send the header.
int len;
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"Content-Length: %d\n", utf8_request.length());
conn->Send(buffer, len);
// Terminate header with empty line.
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n");
conn->Send(buffer, len);
// Send message body as UTF-8.
conn->Send(*utf8_request, utf8_request.length());
return true;
}
// Receive the full buffer before returning unless an error occours.
int DebuggerAgentUtil::ReceiveAll(const Socket* conn, char* data, int len) {
int total_received = 0;
while (total_received < len) {
int received = conn->Receive(data + total_received, len - total_received);
if (received <= 0) {
return total_received;
}
total_received += received;
}
return total_received;
}
} } // namespace v8::internal
// Copyright 2006-2008 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef V8_V8_DEBUG_AGENT_H_
#define V8_V8_DEBUG_AGENT_H_
#include "../include/v8-debug.h"
#include "platform.h"
namespace v8 { namespace internal {
// Forward decelrations.
class DebuggerAgentSession;
// Debugger agent which starts a socket listener on the debugger port and
// handles connection from a remote debugger.
class DebuggerAgent: public Thread {
public:
explicit DebuggerAgent(int port)
: port_(port), server_(OS::CreateSocket()), terminate_(false),
session_access_(OS::CreateMutex()), session_(NULL) {}
~DebuggerAgent() {}
void Shutdown();
private:
void Run();
void CreateSession(Socket* socket);
void DebuggerMessage(const uint16_t* message, int length);
void SessionClosed(DebuggerAgentSession* session);
int port_; // Port to use for the agent.
Socket* server_; // Server socket for listen/accept.
bool terminate_; // Termination flag.
Mutex* session_access_; // Mutex guarging access to session_.
DebuggerAgentSession* session_; // Current active session if any.
friend class DebuggerAgentSession;
friend void DebuggerAgentMessageHandler(const uint16_t* message, int length,
void *data);
DISALLOW_COPY_AND_ASSIGN(DebuggerAgent);
};
// Debugger agent session. The session receives requests from the remote
// debugger and sends debugger events/responses to the remote debugger.
class DebuggerAgentSession: public Thread {
public:
DebuggerAgentSession(DebuggerAgent* agent, Socket* client)
: agent_(agent), client_(client) {}
void DebuggerMessage(Vector<uint16_t> message);
private:
void Run();
void DebuggerMessage(Vector<char> message);
DebuggerAgent* agent_;
const Socket* client_;
DISALLOW_COPY_AND_ASSIGN(DebuggerAgentSession);
};
// Utility methods factored out to be used by the D8 shell as well.
class DebuggerAgentUtil {
public:
static const char* kContentLength;
static int kContentLengthSize;
static SmartPointer<char> ReceiveMessage(const Socket* conn);
static bool SendMessage(const Socket* conn, const Vector<uint16_t> message);
static bool SendMessage(const Socket* conn,
const v8::Handle<v8::String> message);
static int ReceiveAll(const Socket* conn, char* data, int len);
};
} } // namespace v8::internal
#endif // V8_V8_DEBUG_AGENT_H_
......@@ -1355,6 +1355,7 @@ v8::DebugMessageHandler Debugger::message_handler_ = NULL;
void* Debugger::message_handler_data_ = NULL;
v8::DebugHostDispatchHandler Debugger::host_dispatch_handler_ = NULL;
void* Debugger::host_dispatch_handler_data_ = NULL;
DebuggerAgent* Debugger::agent_ = NULL;
Handle<Object> Debugger::MakeJSObject(Vector<const char> constructor_name,
......@@ -1792,6 +1793,17 @@ Handle<Object> Debugger::Call(Handle<JSFunction> fun,
}
bool Debugger::StartAgent(int port) {
if (Socket::Setup()) {
agent_ = new DebuggerAgent(port);
agent_->Start();
return true;
}
return false;
}
DebugMessageThread::DebugMessageThread()
: host_running_(true),
command_queue_(kQueueInitialSize),
......
......@@ -31,6 +31,7 @@
#include "../include/v8-debug.h"
#include "assembler.h"
#include "code-stubs.h"
#include "debug-agent.h"
#include "execution.h"
#include "factory.h"
#include "platform.h"
......@@ -391,6 +392,9 @@ class Debugger {
Handle<Object> data,
bool* pending_exception);
// Start the debugger agent listening on the provided port.
static bool StartAgent(int port);
inline static bool EventActive(v8::DebugEvent event) {
// Currently argument event is not used.
return !Debugger::compiling_natives_ && Debugger::debugger_active_;
......@@ -419,6 +423,8 @@ class Debugger {
static v8::DebugHostDispatchHandler host_dispatch_handler_;
static void* host_dispatch_handler_data_;
static DebuggerAgent* agent_;
friend class DebugMessageThread;
};
......
......@@ -201,7 +201,8 @@ DEFINE_bool(preemption, false,
// Regexp
DEFINE_bool(trace_regexps, false, "trace regexp execution")
DEFINE_bool(regexp_native, true, "use native code regexp implementation (IA32 only)")
DEFINE_bool(regexp_native, true,
"use native code regexp implementation (IA32 only)")
DEFINE_bool(regexp_optimization, true, "generate optimized regexp code")
// Testing flags test/cctest/test-{flags,api,serialization}.cc
......@@ -225,6 +226,8 @@ DEFINE_string(testing_serialization_file, "/tmp/serdes",
DEFINE_bool(help, false, "Print usage message, including flags, on console")
DEFINE_bool(dump_counters, false, "Dump counters on exit")
DEFINE_bool(debugger, true, "Enable JavaScript debugger")
DEFINE_bool(debugger_agent, false, "Enable debugger agent")
DEFINE_int(debugger_port, 5858, "Port to use for remote debugging")
DEFINE_string(map_counters, false, "Map counters to a file")
DEFINE_args(js_arguments, JSArguments(),
"Pass all remaining arguments to the script. Alias for \"--\".")
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -344,6 +344,14 @@
RelativePath="..\..\src\dateparser.h"
>
</File>
<File
RelativePath="..\..\src\debug-agent.cc"
>
</File>
<File
RelativePath="..\..\src\debug-agent.h"
>
</File>
<File
RelativePath="..\..\src\debug-ia32.cc"
>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment