| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/dbg_connection.h" | 5 #include "bin/dbg_connection.h" |
| 6 #include "bin/dbg_message.h" | 6 #include "bin/dbg_message.h" |
| 7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
| 8 #include "bin/log.h" | 8 #include "bin/log.h" |
| 9 #include "bin/socket.h" | 9 #include "bin/socket.h" |
| 10 #include "bin/thread.h" | 10 #include "bin/thread.h" |
| 11 #include "bin/utils.h" | 11 #include "bin/utils.h" |
| 12 | 12 |
| 13 #include "platform/globals.h" | 13 #include "platform/globals.h" |
| 14 #include "platform/json.h" | 14 #include "platform/json.h" |
| 15 #include "platform/thread.h" | 15 #include "platform/thread.h" |
| 16 #include "platform/utils.h" | 16 #include "platform/utils.h" |
| 17 | 17 |
| 18 #include "include/dart_api.h" | 18 #include "include/dart_api.h" |
| 19 | 19 |
| 20 | 20 |
| 21 namespace dart { | 21 namespace dart { |
| 22 namespace bin { | 22 namespace bin { |
| 23 | 23 |
| 24 extern bool trace_debug_protocol; |
| 25 |
| 24 int DebuggerConnectionHandler::listener_fd_ = -1; | 26 int DebuggerConnectionHandler::listener_fd_ = -1; |
| 25 dart::Monitor* DebuggerConnectionHandler::handler_lock_ = new dart::Monitor(); | 27 dart::Monitor* DebuggerConnectionHandler::handler_lock_ = new dart::Monitor(); |
| 26 | 28 |
| 27 // TODO(asiva): Remove this once we have support for multiple debugger | 29 // TODO(asiva): Remove this once we have support for multiple debugger |
| 28 // connections. For now we just store the single debugger connection | 30 // connections. For now we just store the single debugger connection |
| 29 // handler in a static variable. | 31 // handler in a static variable. |
| 30 static DebuggerConnectionHandler* singleton_handler = NULL; | 32 static DebuggerConnectionHandler* singleton_handler = NULL; |
| 31 | 33 |
| 34 // The maximum message length to print when --trace_debug_protocol is |
| 35 // specified. |
| 36 static const int kMaxPrintMessageLen = 1024; |
| 32 | 37 |
| 33 class MessageBuffer { | 38 class MessageBuffer { |
| 34 public: | 39 public: |
| 35 explicit MessageBuffer(int fd); | 40 explicit MessageBuffer(int fd); |
| 36 ~MessageBuffer(); | 41 ~MessageBuffer(); |
| 37 void ReadData(); | 42 void ReadData(); |
| 38 bool IsValidMessage() const; | 43 bool IsValidMessage() const; |
| 39 void PopMessage(); | 44 void PopMessage(); |
| 40 int MessageId() const; | 45 int MessageId() const; |
| 41 char* buf() const { return buf_; } | 46 char* buf() const { return buf_; } |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 | 194 |
| 190 for (;;) { | 195 for (;;) { |
| 191 // Read a message. | 196 // Read a message. |
| 192 while (!msgbuf_->IsValidMessage() && msgbuf_->Alive()) { | 197 while (!msgbuf_->IsValidMessage() && msgbuf_->Alive()) { |
| 193 msgbuf_->ReadData(); | 198 msgbuf_->ReadData(); |
| 194 } | 199 } |
| 195 if (!msgbuf_->Alive()) { | 200 if (!msgbuf_->Alive()) { |
| 196 return; | 201 return; |
| 197 } | 202 } |
| 198 | 203 |
| 204 if (trace_debug_protocol) { |
| 205 dart::JSONReader r(msgbuf_->buf()); |
| 206 const char* msg_end = r.EndOfObject(); |
| 207 if (msg_end != NULL) { |
| 208 intptr_t msg_len = msg_end - msgbuf_->buf(); |
| 209 int print_len = ((msg_len > kMaxPrintMessageLen) |
| 210 ? kMaxPrintMessageLen : msg_len); |
| 211 Log::Print("[<<<] Receiving message from debug fd %d:\n%.*s%s\n", |
| 212 debug_fd_, print_len, msgbuf_->buf(), |
| 213 ((msg_len > print_len) ? "..." : "")); |
| 214 } |
| 215 } |
| 216 |
| 199 // Parse out the command portion from the message. | 217 // Parse out the command portion from the message. |
| 200 dart::JSONReader r(msgbuf_->buf()); | 218 dart::JSONReader r(msgbuf_->buf()); |
| 201 bool found = r.Seek("command"); | 219 bool found = r.Seek("command"); |
| 202 if (r.Error()) { | 220 if (r.Error()) { |
| 203 FATAL("Illegal JSON message received"); | 221 FATAL("Illegal JSON message received"); |
| 204 } | 222 } |
| 205 if (!found) { | 223 if (!found) { |
| 206 Log::Print("'command' not found in JSON message: '%s'\n", | 224 Log::Print("'command' not found in JSON message: '%s'\n", |
| 207 msgbuf_->buf()); | 225 msgbuf_->buf()); |
| 208 msgbuf_->PopMessage(); | 226 msgbuf_->PopMessage(); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 // we need to send the message to all of them. | 355 // we need to send the message to all of them. |
| 338 ASSERT(singleton_handler != NULL); | 356 ASSERT(singleton_handler != NULL); |
| 339 SendMsgHelper(singleton_handler->debug_fd(), msg); | 357 SendMsgHelper(singleton_handler->debug_fd(), msg); |
| 340 } | 358 } |
| 341 | 359 |
| 342 | 360 |
| 343 void DebuggerConnectionHandler::SendMsgHelper(int debug_fd, | 361 void DebuggerConnectionHandler::SendMsgHelper(int debug_fd, |
| 344 dart::TextBuffer* msg) { | 362 dart::TextBuffer* msg) { |
| 345 ASSERT(debug_fd >= 0); | 363 ASSERT(debug_fd >= 0); |
| 346 ASSERT(IsValidJSON(msg->buf())); | 364 ASSERT(IsValidJSON(msg->buf())); |
| 365 |
| 366 if (trace_debug_protocol) { |
| 367 int print_len = ((msg->length() > kMaxPrintMessageLen) |
| 368 ? kMaxPrintMessageLen : msg->length()); |
| 369 Log::Print("[>>>] Sending message to debug fd %d:\n%.*s%s\n", |
| 370 debug_fd, print_len, msg->buf(), |
| 371 ((msg->length() > print_len) ? "..." : "")); |
| 372 } |
| 373 |
| 347 // Sending messages in short pieces can be used to stress test the | 374 // Sending messages in short pieces can be used to stress test the |
| 348 // debugger front-end's message handling code. | 375 // debugger front-end's message handling code. |
| 349 const bool send_in_pieces = false; | 376 const bool send_in_pieces = false; |
| 350 if (send_in_pieces) { | 377 if (send_in_pieces) { |
| 351 intptr_t remaining = msg->length(); | 378 intptr_t remaining = msg->length(); |
| 352 intptr_t sent = 0; | 379 intptr_t sent = 0; |
| 353 const intptr_t max_piece_len = 122; // Pretty arbitrary, not a power of 2. | 380 const intptr_t max_piece_len = 122; // Pretty arbitrary, not a power of 2. |
| 354 dart::Monitor sleep; | 381 dart::Monitor sleep; |
| 355 while (remaining > 0) { | 382 while (remaining > 0) { |
| 356 intptr_t piece_len = remaining; | 383 intptr_t piece_len = remaining; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 | 484 |
| 458 | 485 |
| 459 bool DebuggerConnectionHandler::IsConnected() { | 486 bool DebuggerConnectionHandler::IsConnected() { |
| 460 // TODO(asiva): Support multiple debugger connections. | 487 // TODO(asiva): Support multiple debugger connections. |
| 461 // Return true if a connection has been established. | 488 // Return true if a connection has been established. |
| 462 return singleton_handler != NULL; | 489 return singleton_handler != NULL; |
| 463 } | 490 } |
| 464 | 491 |
| 465 } // namespace bin | 492 } // namespace bin |
| 466 } // namespace dart | 493 } // namespace dart |
| OLD | NEW |