33 #ifdef ENABLE_DEBUGGER_SUPPORT
41 DebuggerAgent* agent = Isolate::Current()->debugger_agent_instance();
43 agent->DebuggerMessage(message);
48 void DebuggerAgent::Run() {
49 const int kOneSecondInMicros = 1000000;
52 server_->SetReuseAddress(
true);
56 while (!bound && !terminate_) {
57 bound = server_->Bind(port_);
63 PrintF(
"Failed to open socket on port %d, "
64 "waiting %d ms before retrying\n", port_, kOneSecondInMicros / 1000);
65 terminate_now_->Wait(kOneSecondInMicros);
71 bool ok = server_->Listen(1);
75 Socket* client = server_->Accept();
79 CreateSession(client);
86 void DebuggerAgent::Shutdown() {
92 terminate_now_->Signal();
101 void DebuggerAgent::WaitUntilListening() {
105 static const char* kCreateSessionMessage =
106 "Remote debugging session already active\r\n";
108 void DebuggerAgent::CreateSession(Socket* client) {
109 ScopedLock with(session_access_);
112 if (session_ !=
NULL) {
113 client->Send(kCreateSessionMessage,
StrLength(kCreateSessionMessage));
119 session_ =
new DebuggerAgentSession(
this, client);
120 isolate_->debugger()->SetMessageHandler(DebuggerAgentMessageHandler);
125 void DebuggerAgent::CloseSession() {
126 ScopedLock with(session_access_);
129 if (session_ !=
NULL) {
130 session_->Shutdown();
139 ScopedLock with(session_access_);
142 if (session_ !=
NULL) {
144 session_->DebuggerMessage(Vector<uint16_t>(const_cast<uint16_t*>(*val),
150 void DebuggerAgent::OnSessionClosed(DebuggerAgentSession* session) {
157 ScopedLock with(session_access_);
158 ASSERT(session == session_);
159 if (session == session_) {
160 session_->Shutdown();
167 void DebuggerAgentSession::Run() {
169 bool ok = DebuggerAgentUtil::SendConnectMessage(client_, *agent_->name_);
174 SmartArrayPointer<char> message =
175 DebuggerAgentUtil::ReceiveMessage(client_);
177 const char* msg = *message;
178 bool is_closing_session = (msg ==
NULL);
182 msg =
"{\"seq\":1,\"type\":\"request\",\"command\":\"disconnect\"}";
186 const char* disconnectRequestStr =
187 "\"type\":\"request\",\"command\":\"disconnect\"}";
188 const char* result = strstr(msg, disconnectRequestStr);
189 if (result !=
NULL) {
190 is_closing_session =
true;
197 while (buf.has_more()) {
201 ScopedVector<int16_t> temp(len + 1);
203 for (
int i = 0; i < len; i++) {
204 temp[i] = buf.GetNext();
211 reinterpret_cast<v8::Isolate*>(agent_->isolate()));
213 if (is_closing_session) {
215 agent_->OnSessionClosed(
this);
222 void DebuggerAgentSession::DebuggerMessage(Vector<uint16_t> message) {
223 DebuggerAgentUtil::SendMessage(client_, message);
227 void DebuggerAgentSession::Shutdown() {
233 const char*
const DebuggerAgentUtil::kContentLength =
"Content-Length";
236 SmartArrayPointer<char> DebuggerAgentUtil::ReceiveMessage(
const Socket* conn) {
240 int content_length = 0;
242 const int kHeaderBufferSize = 80;
243 char header_buffer[kHeaderBufferSize];
244 int header_buffer_position = 0;
249 while (!(c ==
'\n' && prev_c ==
'\r')) {
251 received = conn->Receive(&c, 1);
254 return SmartArrayPointer<char>();
258 if (header_buffer_position < kHeaderBufferSize) {
259 header_buffer[header_buffer_position++] = c;
264 if (header_buffer_position == 2) {
269 ASSERT(header_buffer_position > 1);
270 ASSERT(header_buffer_position <= kHeaderBufferSize);
271 header_buffer[header_buffer_position - 2] =
'\0';
274 char* key = header_buffer;
276 for (
int i = 0; header_buffer[i] !=
'\0'; i++) {
277 if (header_buffer[i] ==
':') {
278 header_buffer[i] =
'\0';
279 value = header_buffer + i + 1;
280 while (*value ==
' ') {
288 if (strcmp(key, kContentLength) == 0) {
290 if (value ==
NULL || strlen(value) > 7) {
291 return SmartArrayPointer<char>();
293 for (
int i = 0; value[i] !=
'\0'; i++) {
295 if (value[i] <
'0' || value[i] >
'9') {
296 return SmartArrayPointer<char>();
298 content_length = 10 * content_length + (value[i] -
'0');
302 PrintF(
"%s: %s\n", key, value !=
NULL ? value :
"(no value)");
307 if (content_length == 0) {
308 return SmartArrayPointer<char>();
312 char* buffer = NewArray<char>(content_length + 1);
313 received = ReceiveAll(conn, buffer, content_length);
314 if (received < content_length) {
316 return SmartArrayPointer<char>();
318 buffer[content_length] =
'\0';
320 return SmartArrayPointer<char>(buffer);
324 bool DebuggerAgentUtil::SendConnectMessage(
const Socket* conn,
325 const char* embedding_host) {
326 static const int kBufferSize = 80;
327 char buffer[kBufferSize];
333 "Type: connect\r\n");
334 ok = conn->Send(buffer, len);
335 if (!ok)
return false;
339 ok = conn->Send(buffer, len);
340 if (!ok)
return false;
343 "Protocol-Version: 1\r\n");
344 ok = conn->Send(buffer, len);
345 if (!ok)
return false;
347 if (embedding_host !=
NULL) {
349 "Embedding-Host: %s\r\n", embedding_host);
350 ok = conn->Send(buffer, len);
351 if (!ok)
return false;
355 "%s: 0\r\n", kContentLength);
356 ok = conn->Send(buffer, len);
357 if (!ok)
return false;
360 len =
OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"\r\n");
361 ok = conn->Send(buffer, len);
362 if (!ok)
return false;
370 bool DebuggerAgentUtil::SendMessage(
const Socket* conn,
371 const Vector<uint16_t> message) {
372 static const int kBufferSize = 80;
373 char buffer[kBufferSize];
378 for (
int i = 0; i < message.length(); i++) {
381 previous = character;
387 "%s: %d\r\n", kContentLength, utf8_len);
388 conn->Send(buffer, len);
391 len =
OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"\r\n");
392 conn->Send(buffer, len);
395 int buffer_position = 0;
397 for (
int i = 0; i < message.length(); i++) {
402 ASSERT(buffer_position <= kBufferSize);
405 if (kBufferSize - buffer_position <
407 i == message.length() - 1) {
409 const int kEncodedSurrogateLength =
411 ASSERT(buffer_position >= kEncodedSurrogateLength);
412 conn->Send(buffer, buffer_position - kEncodedSurrogateLength);
413 for (
int i = 0; i < kEncodedSurrogateLength; i++) {
414 buffer[i] = buffer[buffer_position + i];
416 buffer_position = kEncodedSurrogateLength;
418 conn->Send(buffer, buffer_position);
422 previous = character;
429 bool DebuggerAgentUtil::SendMessage(
const Socket* conn,
431 static const int kBufferSize = 80;
432 char buffer[kBufferSize];
440 "Content-Length: %d\r\n", utf8_request.length());
441 conn->Send(buffer, len);
444 len =
OS::SNPrintF(Vector<char>(buffer, kBufferSize),
"\r\n");
445 conn->Send(buffer, len);
448 conn->Send(*utf8_request, utf8_request.length());
455 int DebuggerAgentUtil::ReceiveAll(
const Socket* conn,
char* data,
int len) {
456 int total_received = 0;
457 while (total_received < len) {
458 int received = conn->Receive(data + total_received, len - total_received);
460 return total_received;
462 total_received += received;
464 return total_received;
469 #endif // ENABLE_DEBUGGER_SUPPORT
void PrintF(const char *format,...)
#define ASSERT(condition)
static const int kMaxExtraUtf8BytesForOneUtf16CodeUnit
static const char * GetVersion()
static uchar Length(uchar chr, int previous)
static const int kUtf8BytesToCodeASurrogate
virtual Handle< String > GetJSON() const =0
static unsigned Encode(char *out, uchar c, int previous)
int StrLength(const char *string)
static int SNPrintF(Vector< char > str, const char *format,...)
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination trace on stack replacement optimize closures functions with arguments object optimize functions containing for in loops profiler considers IC stability primitive functions trigger their own optimization re try self optimization if it failed insert an interrupt check at function exit execution budget before interrupt is triggered call count before self optimization self_optimization count_based_interrupts weighted_back_edges trace_opt emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 enable use of ARMv7 instructions if enable use of MIPS FPU instructions if NULL
static bool IsLeadSurrogate(int code)
static void SendCommand(const uint16_t *command, int length, ClientData *client_data=NULL, Isolate *isolate=NULL)
static const int kNoPreviousCharacter