2012-05-21 10:02:49 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2009-03-03 12:23:45 +00:00
|
|
|
// 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"
|
2011-01-06 13:14:32 +00:00
|
|
|
#include "debug.h"
|
2009-03-03 12:23:45 +00:00
|
|
|
#include "debug-agent.h"
|
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2011-01-06 13:14:32 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2009-03-03 12:23:45 +00:00
|
|
|
|
|
|
|
// Public V8 debugger API message handler function. This function just delegates
|
|
|
|
// to the debugger agent through it's data parameter.
|
2009-04-29 09:04:20 +00:00
|
|
|
void DebuggerAgentMessageHandler(const v8::Debug::Message& message) {
|
2011-03-18 20:35:07 +00:00
|
|
|
DebuggerAgent* agent = Isolate::Current()->debugger_agent_instance();
|
|
|
|
ASSERT(agent != NULL);
|
|
|
|
agent->DebuggerMessage(message);
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Debugger agent main thread.
|
|
|
|
void DebuggerAgent::Run() {
|
2009-03-19 21:07:07 +00:00
|
|
|
const int kOneSecondInMicros = 1000000;
|
|
|
|
|
2009-03-20 09:03:36 +00:00
|
|
|
// Allow this socket to reuse port even if still in TIME_WAIT.
|
|
|
|
server_->SetReuseAddress(true);
|
|
|
|
|
2009-03-19 21:07:07 +00:00
|
|
|
// First bind the socket to the requested port.
|
|
|
|
bool bound = false;
|
|
|
|
while (!bound && !terminate_) {
|
|
|
|
bound = server_->Bind(port_);
|
|
|
|
|
2010-01-15 18:11:12 +00:00
|
|
|
// If an error occurred wait a bit before retrying. The most common error
|
2009-03-19 21:07:07 +00:00
|
|
|
// would be that the port is already in use so this avoids a busy loop and
|
|
|
|
// make the agent take over the port when it becomes free.
|
|
|
|
if (!bound) {
|
2010-01-15 18:11:12 +00:00
|
|
|
PrintF("Failed to open socket on port %d, "
|
|
|
|
"waiting %d ms before retrying\n", port_, kOneSecondInMicros / 1000);
|
2009-03-19 21:07:07 +00:00
|
|
|
terminate_now_->Wait(kOneSecondInMicros);
|
|
|
|
}
|
|
|
|
}
|
2009-03-03 12:23:45 +00:00
|
|
|
|
2009-03-19 21:07:07 +00:00
|
|
|
// Accept connections on the bound port.
|
2009-03-03 12:23:45 +00:00
|
|
|
while (!terminate_) {
|
2009-03-19 21:07:07 +00:00
|
|
|
bool ok = server_->Listen(1);
|
2009-09-25 10:36:00 +00:00
|
|
|
listening_->Signal();
|
2009-03-19 21:07:07 +00:00
|
|
|
if (ok) {
|
|
|
|
// Accept the new connection.
|
|
|
|
Socket* client = server_->Accept();
|
|
|
|
ok = client != NULL;
|
|
|
|
if (ok) {
|
|
|
|
// Create and start a new session.
|
|
|
|
CreateSession(client);
|
|
|
|
}
|
|
|
|
}
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DebuggerAgent::Shutdown() {
|
2009-03-19 21:07:07 +00:00
|
|
|
// Set the termination flag.
|
|
|
|
terminate_ = true;
|
|
|
|
|
|
|
|
// Signal termination and make the server exit either its listen call or its
|
|
|
|
// binding loop. This makes sure that no new sessions can be established.
|
|
|
|
terminate_now_->Signal();
|
|
|
|
server_->Shutdown();
|
|
|
|
Join();
|
|
|
|
|
|
|
|
// Close existing session if any.
|
|
|
|
CloseSession();
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-25 10:36:00 +00:00
|
|
|
void DebuggerAgent::WaitUntilListening() {
|
|
|
|
listening_->Wait();
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
static const char* kCreateSessionMessage =
|
|
|
|
"Remote debugging session already active\r\n";
|
|
|
|
|
2009-03-03 12:23:45 +00:00
|
|
|
void DebuggerAgent::CreateSession(Socket* client) {
|
|
|
|
ScopedLock with(session_access_);
|
|
|
|
|
|
|
|
// If another session is already established terminate this one.
|
|
|
|
if (session_ != NULL) {
|
2011-03-18 20:35:07 +00:00
|
|
|
client->Send(kCreateSessionMessage, StrLength(kCreateSessionMessage));
|
2009-03-03 12:23:45 +00:00
|
|
|
delete client;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new session and hook up the debug message handler.
|
2011-06-10 09:54:04 +00:00
|
|
|
session_ = new DebuggerAgentSession(this, client);
|
|
|
|
isolate_->debugger()->SetMessageHandler(DebuggerAgentMessageHandler);
|
2009-03-03 12:23:45 +00:00
|
|
|
session_->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-19 21:07:07 +00:00
|
|
|
void DebuggerAgent::CloseSession() {
|
|
|
|
ScopedLock with(session_access_);
|
|
|
|
|
|
|
|
// Terminate the session.
|
|
|
|
if (session_ != NULL) {
|
|
|
|
session_->Shutdown();
|
|
|
|
session_->Join();
|
|
|
|
delete session_;
|
|
|
|
session_ = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-29 09:04:20 +00:00
|
|
|
void DebuggerAgent::DebuggerMessage(const v8::Debug::Message& message) {
|
2009-03-03 12:23:45 +00:00
|
|
|
ScopedLock with(session_access_);
|
|
|
|
|
|
|
|
// Forward the message handling to the session.
|
|
|
|
if (session_ != NULL) {
|
2009-04-29 09:04:20 +00:00
|
|
|
v8::String::Value val(message.GetJSON());
|
|
|
|
session_->DebuggerMessage(Vector<uint16_t>(const_cast<uint16_t*>(*val),
|
|
|
|
val.length()));
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-19 21:07:07 +00:00
|
|
|
void DebuggerAgent::OnSessionClosed(DebuggerAgentSession* session) {
|
|
|
|
// Don't do anything during termination.
|
|
|
|
if (terminate_) {
|
|
|
|
return;
|
|
|
|
}
|
2009-03-03 12:23:45 +00:00
|
|
|
|
|
|
|
// Terminate the session.
|
2009-03-19 21:07:07 +00:00
|
|
|
ScopedLock with(session_access_);
|
2009-03-03 12:23:45 +00:00
|
|
|
ASSERT(session == session_);
|
|
|
|
if (session == session_) {
|
2012-06-04 09:01:37 +00:00
|
|
|
session_->Shutdown();
|
|
|
|
delete session_;
|
|
|
|
session_ = NULL;
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DebuggerAgentSession::Run() {
|
2009-03-23 22:23:39 +00:00
|
|
|
// Send the hello message.
|
|
|
|
bool ok = DebuggerAgentUtil::SendConnectMessage(client_, *agent_->name_);
|
|
|
|
if (!ok) return;
|
|
|
|
|
2009-03-03 12:23:45 +00:00
|
|
|
while (true) {
|
|
|
|
// Read data from the debugger front end.
|
2011-09-09 22:39:47 +00:00
|
|
|
SmartArrayPointer<char> message =
|
|
|
|
DebuggerAgentUtil::ReceiveMessage(client_);
|
2011-01-06 13:14:32 +00:00
|
|
|
|
|
|
|
const char* msg = *message;
|
|
|
|
bool is_closing_session = (msg == NULL);
|
|
|
|
|
|
|
|
if (msg == NULL) {
|
|
|
|
// If we lost the connection, then simulate a disconnect msg:
|
|
|
|
msg = "{\"seq\":1,\"type\":\"request\",\"command\":\"disconnect\"}";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Check if we're getting a disconnect request:
|
|
|
|
const char* disconnectRequestStr =
|
|
|
|
"\"type\":\"request\",\"command\":\"disconnect\"}";
|
|
|
|
const char* result = strstr(msg, disconnectRequestStr);
|
|
|
|
if (result != NULL) {
|
|
|
|
is_closing_session = true;
|
|
|
|
}
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert UTF-8 to UTF-16.
|
2011-01-06 13:14:32 +00:00
|
|
|
unibrow::Utf8InputBuffer<> buf(msg, StrLength(msg));
|
2009-03-03 12:23:45 +00:00
|
|
|
int len = 0;
|
|
|
|
while (buf.has_more()) {
|
|
|
|
buf.GetNext();
|
|
|
|
len++;
|
|
|
|
}
|
2010-05-05 12:25:58 +00:00
|
|
|
ScopedVector<int16_t> temp(len + 1);
|
2011-01-06 13:14:32 +00:00
|
|
|
buf.Reset(msg, StrLength(msg));
|
2009-03-03 12:23:45 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
temp[i] = buf.GetNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the request received to the debugger.
|
2010-05-05 12:25:58 +00:00
|
|
|
v8::Debug::SendCommand(reinterpret_cast<const uint16_t *>(temp.start()),
|
2011-06-10 09:54:04 +00:00
|
|
|
len,
|
|
|
|
NULL,
|
|
|
|
reinterpret_cast<v8::Isolate*>(agent_->isolate()));
|
2011-01-06 13:14:32 +00:00
|
|
|
|
|
|
|
if (is_closing_session) {
|
|
|
|
// Session is closed.
|
|
|
|
agent_->OnSessionClosed(this);
|
|
|
|
return;
|
|
|
|
}
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DebuggerAgentSession::DebuggerMessage(Vector<uint16_t> message) {
|
|
|
|
DebuggerAgentUtil::SendMessage(client_, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-19 21:07:07 +00:00
|
|
|
void DebuggerAgentSession::Shutdown() {
|
|
|
|
// Shutdown the socket to end the blocking receive.
|
|
|
|
client_->Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
const char* const DebuggerAgentUtil::kContentLength = "Content-Length";
|
2009-03-03 12:23:45 +00:00
|
|
|
|
|
|
|
|
2011-09-09 22:39:47 +00:00
|
|
|
SmartArrayPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) {
|
2009-03-03 12:23:45 +00:00
|
|
|
int received;
|
|
|
|
|
|
|
|
// Read header.
|
|
|
|
int content_length = 0;
|
2009-03-24 10:15:02 +00:00
|
|
|
while (true) {
|
|
|
|
const int kHeaderBufferSize = 80;
|
|
|
|
char header_buffer[kHeaderBufferSize];
|
|
|
|
int header_buffer_position = 0;
|
|
|
|
char c = '\0'; // One character receive buffer.
|
|
|
|
char prev_c = '\0'; // Previous character.
|
|
|
|
|
|
|
|
// Read until CRLF.
|
|
|
|
while (!(c == '\n' && prev_c == '\r')) {
|
|
|
|
prev_c = c;
|
|
|
|
received = conn->Receive(&c, 1);
|
2012-05-21 10:02:49 +00:00
|
|
|
if (received == 0) {
|
2009-03-24 10:15:02 +00:00
|
|
|
PrintF("Error %d\n", Socket::LastError());
|
2011-09-09 22:39:47 +00:00
|
|
|
return SmartArrayPointer<char>();
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-24 10:15:02 +00:00
|
|
|
// Add character to header buffer.
|
2009-03-03 12:23:45 +00:00
|
|
|
if (header_buffer_position < kHeaderBufferSize) {
|
2009-03-24 10:15:02 +00:00
|
|
|
header_buffer[header_buffer_position++] = c;
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
2009-03-24 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for end of header (empty header line).
|
|
|
|
if (header_buffer_position == 2) { // Receive buffer contains CRLF.
|
|
|
|
break;
|
|
|
|
}
|
2009-03-03 12:23:45 +00:00
|
|
|
|
2009-03-24 10:15:02 +00:00
|
|
|
// Terminate header.
|
|
|
|
ASSERT(header_buffer_position > 1); // At least CRLF is received.
|
|
|
|
ASSERT(header_buffer_position <= kHeaderBufferSize);
|
|
|
|
header_buffer[header_buffer_position - 2] = '\0';
|
|
|
|
|
|
|
|
// Split header.
|
|
|
|
char* key = header_buffer;
|
|
|
|
char* value = NULL;
|
|
|
|
for (int i = 0; header_buffer[i] != '\0'; i++) {
|
|
|
|
if (header_buffer[i] == ':') {
|
|
|
|
header_buffer[i] = '\0';
|
|
|
|
value = header_buffer + i + 1;
|
|
|
|
while (*value == ' ') {
|
|
|
|
value++;
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
2009-03-24 10:15:02 +00:00
|
|
|
break;
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
2009-03-24 10:15:02 +00:00
|
|
|
}
|
2009-03-03 12:23:45 +00:00
|
|
|
|
2009-03-24 10:15:02 +00:00
|
|
|
// Check that key is Content-Length.
|
|
|
|
if (strcmp(key, kContentLength) == 0) {
|
2009-07-30 07:02:23 +00:00
|
|
|
// Get the content length value if present and within a sensible range.
|
|
|
|
if (value == NULL || strlen(value) > 7) {
|
2011-09-09 22:39:47 +00:00
|
|
|
return SmartArrayPointer<char>();
|
2009-03-24 10:15:02 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; value[i] != '\0'; i++) {
|
|
|
|
// Bail out if illegal data.
|
|
|
|
if (value[i] < '0' || value[i] > '9') {
|
2011-09-09 22:39:47 +00:00
|
|
|
return SmartArrayPointer<char>();
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
2009-03-24 10:15:02 +00:00
|
|
|
content_length = 10 * content_length + (value[i] - '0');
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-03-24 10:15:02 +00:00
|
|
|
// For now just print all other headers than Content-Length.
|
|
|
|
PrintF("%s: %s\n", key, value != NULL ? value : "(no value)");
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-23 22:23:39 +00:00
|
|
|
// Return now if no body.
|
|
|
|
if (content_length == 0) {
|
2011-09-09 22:39:47 +00:00
|
|
|
return SmartArrayPointer<char>();
|
2009-03-23 22:23:39 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 12:23:45 +00:00
|
|
|
// 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());
|
2011-09-09 22:39:47 +00:00
|
|
|
return SmartArrayPointer<char>();
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
buffer[content_length] = '\0';
|
|
|
|
|
2011-09-09 22:39:47 +00:00
|
|
|
return SmartArrayPointer<char>(buffer);
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-23 22:23:39 +00:00
|
|
|
bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn,
|
|
|
|
const char* embedding_host) {
|
|
|
|
static const int kBufferSize = 80;
|
|
|
|
char buffer[kBufferSize]; // Sending buffer.
|
|
|
|
bool ok;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
// Send the header.
|
|
|
|
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
|
2009-03-24 10:15:02 +00:00
|
|
|
"Type: connect\r\n");
|
2009-03-23 22:23:39 +00:00
|
|
|
ok = conn->Send(buffer, len);
|
|
|
|
if (!ok) return false;
|
|
|
|
|
|
|
|
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
|
2009-03-24 10:15:02 +00:00
|
|
|
"V8-Version: %s\r\n", v8::V8::GetVersion());
|
2009-03-23 22:23:39 +00:00
|
|
|
ok = conn->Send(buffer, len);
|
|
|
|
if (!ok) return false;
|
|
|
|
|
|
|
|
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
|
2009-03-24 10:15:02 +00:00
|
|
|
"Protocol-Version: 1\r\n");
|
2009-03-23 22:23:39 +00:00
|
|
|
ok = conn->Send(buffer, len);
|
|
|
|
if (!ok) return false;
|
|
|
|
|
|
|
|
if (embedding_host != NULL) {
|
|
|
|
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
|
2009-03-24 10:15:02 +00:00
|
|
|
"Embedding-Host: %s\r\n", embedding_host);
|
2009-03-23 22:23:39 +00:00
|
|
|
ok = conn->Send(buffer, len);
|
|
|
|
if (!ok) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
|
2009-03-24 10:15:02 +00:00
|
|
|
"%s: 0\r\n", kContentLength);
|
2009-03-23 22:23:39 +00:00
|
|
|
ok = conn->Send(buffer, len);
|
|
|
|
if (!ok) return false;
|
|
|
|
|
|
|
|
// Terminate header with empty line.
|
2009-03-24 10:15:02 +00:00
|
|
|
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n");
|
2009-03-23 22:23:39 +00:00
|
|
|
ok = conn->Send(buffer, len);
|
|
|
|
if (!ok) return false;
|
|
|
|
|
|
|
|
// No body for connect message.
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-03 12:23:45 +00:00
|
|
|
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;
|
2012-03-12 12:35:28 +00:00
|
|
|
int previous = unibrow::Utf16::kNoPreviousCharacter;
|
2009-03-03 12:23:45 +00:00
|
|
|
for (int i = 0; i < message.length(); i++) {
|
2012-03-12 12:35:28 +00:00
|
|
|
uint16_t character = message[i];
|
|
|
|
utf8_len += unibrow::Utf8::Length(character, previous);
|
|
|
|
previous = character;
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the header.
|
|
|
|
int len;
|
|
|
|
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
|
2009-03-24 10:15:02 +00:00
|
|
|
"%s: %d\r\n", kContentLength, utf8_len);
|
2009-03-03 12:23:45 +00:00
|
|
|
conn->Send(buffer, len);
|
|
|
|
|
|
|
|
// Terminate header with empty line.
|
2009-03-24 10:15:02 +00:00
|
|
|
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n");
|
2009-03-03 12:23:45 +00:00
|
|
|
conn->Send(buffer, len);
|
|
|
|
|
|
|
|
// Send message body as UTF-8.
|
|
|
|
int buffer_position = 0; // Current buffer position.
|
2012-03-12 12:35:28 +00:00
|
|
|
previous = unibrow::Utf16::kNoPreviousCharacter;
|
2009-03-03 12:23:45 +00:00
|
|
|
for (int i = 0; i < message.length(); i++) {
|
|
|
|
// Write next UTF-8 encoded character to buffer.
|
2012-03-12 12:35:28 +00:00
|
|
|
uint16_t character = message[i];
|
2009-03-03 12:23:45 +00:00
|
|
|
buffer_position +=
|
2012-03-12 12:35:28 +00:00
|
|
|
unibrow::Utf8::Encode(buffer + buffer_position, character, previous);
|
2009-03-03 12:23:45 +00:00
|
|
|
ASSERT(buffer_position < kBufferSize);
|
|
|
|
|
|
|
|
// Send buffer if full or last character is encoded.
|
2012-03-12 12:35:28 +00:00
|
|
|
if (kBufferSize - buffer_position <
|
|
|
|
unibrow::Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit ||
|
|
|
|
i == message.length() - 1) {
|
|
|
|
if (unibrow::Utf16::IsLeadSurrogate(character)) {
|
|
|
|
const int kEncodedSurrogateLength =
|
|
|
|
unibrow::Utf16::kUtf8BytesToCodeASurrogate;
|
|
|
|
ASSERT(buffer_position >= kEncodedSurrogateLength);
|
|
|
|
conn->Send(buffer, buffer_position - kEncodedSurrogateLength);
|
|
|
|
for (int i = 0; i < kEncodedSurrogateLength; i++) {
|
|
|
|
buffer[i] = buffer[buffer_position + i];
|
|
|
|
}
|
|
|
|
buffer_position = kEncodedSurrogateLength;
|
|
|
|
} else {
|
|
|
|
conn->Send(buffer, buffer_position);
|
|
|
|
buffer_position = 0;
|
|
|
|
}
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
2012-03-12 12:35:28 +00:00
|
|
|
previous = character;
|
2009-03-03 12:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2009-03-24 10:15:02 +00:00
|
|
|
"Content-Length: %d\r\n", utf8_request.length());
|
2009-03-03 12:23:45 +00:00
|
|
|
conn->Send(buffer, len);
|
|
|
|
|
|
|
|
// Terminate header with empty line.
|
2009-03-24 10:15:02 +00:00
|
|
|
len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n");
|
2009-03-03 12:23:45 +00:00
|
|
|
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);
|
2012-05-21 10:02:49 +00:00
|
|
|
if (received == 0) {
|
2009-03-03 12:23:45 +00:00
|
|
|
return total_received;
|
|
|
|
}
|
|
|
|
total_received += received;
|
|
|
|
}
|
|
|
|
return total_received;
|
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
2009-04-20 16:36:13 +00:00
|
|
|
|
|
|
|
#endif // ENABLE_DEBUGGER_SUPPORT
|