| 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 int DebuggerConnectionHandler::listener_fd_ = -1; | 24 int DebuggerConnectionHandler::listener_fd_ = -1; |
| 25 dart::Monitor DebuggerConnectionHandler::handler_lock_; | 25 dart::Monitor* DebuggerConnectionHandler::handler_lock_ = new dart::Monitor(); |
| 26 | 26 |
| 27 // TODO(asiva): Remove this once we have support for multiple debugger | 27 // TODO(asiva): Remove this once we have support for multiple debugger |
| 28 // connections. For now we just store the single debugger connection | 28 // connections. For now we just store the single debugger connection |
| 29 // handler in a static variable. | 29 // handler in a static variable. |
| 30 static DebuggerConnectionHandler* singleton_handler = NULL; | 30 static DebuggerConnectionHandler* singleton_handler = NULL; |
| 31 | 31 |
| 32 | 32 |
| 33 class MessageBuffer { | 33 class MessageBuffer { |
| 34 public: | 34 public: |
| 35 explicit MessageBuffer(int fd); | 35 explicit MessageBuffer(int fd); |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 delete msgbuf_; | 273 delete msgbuf_; |
| 274 msgbuf_ = NULL; | 274 msgbuf_ = NULL; |
| 275 } | 275 } |
| 276 // TODO(hausner): Need to tell the VM debugger object to remove all | 276 // TODO(hausner): Need to tell the VM debugger object to remove all |
| 277 // breakpoints. | 277 // breakpoints. |
| 278 } | 278 } |
| 279 | 279 |
| 280 | 280 |
| 281 void DebuggerConnectionHandler::StartHandler(const char* address, | 281 void DebuggerConnectionHandler::StartHandler(const char* address, |
| 282 int port_number) { | 282 int port_number) { |
| 283 MonitorLocker ml(&handler_lock_); | 283 ASSERT(handler_lock_ != NULL); |
| 284 MonitorLocker ml(handler_lock_); |
| 284 if (listener_fd_ != -1) { | 285 if (listener_fd_ != -1) { |
| 285 return; // The debugger connection handler was already started. | 286 return; // The debugger connection handler was already started. |
| 286 } | 287 } |
| 287 | 288 |
| 288 // First setup breakpoint, exception and delayed breakpoint handlers. | 289 // First setup breakpoint, exception and delayed breakpoint handlers. |
| 289 DbgMsgQueueList::Initialize(); | 290 DbgMsgQueueList::Initialize(); |
| 290 | 291 |
| 291 // Initialize the socket implementation. | 292 // Initialize the socket implementation. |
| 292 if (!Socket::Initialize()) { | 293 if (!Socket::Initialize()) { |
| 293 FATAL("Failed initializing socket implementation."); | 294 FATAL("Failed initializing socket implementation."); |
| 294 } | 295 } |
| 295 | 296 |
| 296 // Now setup a listener socket and start a thread which will | 297 // Now setup a listener socket and start a thread which will |
| 297 // listen, accept connections from debuggers, read and handle/dispatch | 298 // listen, accept connections from debuggers, read and handle/dispatch |
| 298 // debugger commands received on these connections. | 299 // debugger commands received on these connections. |
| 299 ASSERT(listener_fd_ == -1); | 300 ASSERT(listener_fd_ == -1); |
| 300 | 301 |
| 301 OSError *os_error; | 302 OSError *os_error; |
| 302 SocketAddresses* addresses = Socket::LookupAddress(address, -1, &os_error); | 303 SocketAddresses* addresses = Socket::LookupAddress(address, -1, &os_error); |
| 303 listener_fd_ = ServerSocket::CreateBindListen( | 304 listener_fd_ = ServerSocket::CreateBindListen( |
| 304 addresses->GetAt(0)->addr(), port_number, 1); | 305 addresses->GetAt(0)->addr(), port_number, 1); |
| 305 DebuggerConnectionImpl::StartHandler(port_number); | 306 DebuggerConnectionImpl::StartHandler(port_number); |
| 306 } | 307 } |
| 307 | 308 |
| 308 | 309 |
| 309 void DebuggerConnectionHandler::WaitForConnection() { | 310 void DebuggerConnectionHandler::WaitForConnection() { |
| 310 MonitorLocker ml(&handler_lock_); | 311 ASSERT(handler_lock_ != NULL); |
| 312 MonitorLocker ml(handler_lock_); |
| 311 while (!IsConnected()) { | 313 while (!IsConnected()) { |
| 312 dart::Monitor::WaitResult res = ml.Wait(); | 314 dart::Monitor::WaitResult res = ml.Wait(); |
| 313 ASSERT(res == dart::Monitor::kNotified); | 315 ASSERT(res == dart::Monitor::kNotified); |
| 314 } | 316 } |
| 315 } | 317 } |
| 316 | 318 |
| 317 | 319 |
| 318 void DebuggerConnectionHandler::SendMsg(int debug_fd, dart::TextBuffer* msg) { | 320 void DebuggerConnectionHandler::SendMsg(int debug_fd, dart::TextBuffer* msg) { |
| 319 MonitorLocker ml(&handler_lock_); | 321 ASSERT(handler_lock_ != NULL); |
| 322 MonitorLocker ml(handler_lock_); |
| 320 SendMsgHelper(debug_fd, msg); | 323 SendMsgHelper(debug_fd, msg); |
| 321 } | 324 } |
| 322 | 325 |
| 323 | 326 |
| 324 void DebuggerConnectionHandler::BroadcastMsg(dart::TextBuffer* msg) { | 327 void DebuggerConnectionHandler::BroadcastMsg(dart::TextBuffer* msg) { |
| 325 MonitorLocker ml(&handler_lock_); | 328 ASSERT(handler_lock_ != NULL); |
| 329 MonitorLocker ml(handler_lock_); |
| 326 // TODO(asiva): Once we support connection to multiple debuggers | 330 // TODO(asiva): Once we support connection to multiple debuggers |
| 327 // we need to send the message to all of them. | 331 // we need to send the message to all of them. |
| 328 ASSERT(singleton_handler != NULL); | 332 ASSERT(singleton_handler != NULL); |
| 329 SendMsgHelper(singleton_handler->debug_fd(), msg); | 333 SendMsgHelper(singleton_handler->debug_fd(), msg); |
| 330 } | 334 } |
| 331 | 335 |
| 332 | 336 |
| 333 void DebuggerConnectionHandler::SendMsgHelper(int debug_fd, | 337 void DebuggerConnectionHandler::SendMsgHelper(int debug_fd, |
| 334 dart::TextBuffer* msg) { | 338 dart::TextBuffer* msg) { |
| 335 ASSERT(debug_fd >= 0); | 339 ASSERT(debug_fd >= 0); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 364 DebuggerConnectionImpl::Send(debug_fd, msg->buf(), msg->length()); | 368 DebuggerConnectionImpl::Send(debug_fd, msg->buf(), msg->length()); |
| 365 ASSERT(msg->length() == bytes_written); | 369 ASSERT(msg->length() == bytes_written); |
| 366 // TODO(hausner): Error checking. Probably just shut down the debugger | 370 // TODO(hausner): Error checking. Probably just shut down the debugger |
| 367 // session if we there is an error while writing. | 371 // session if we there is an error while writing. |
| 368 } | 372 } |
| 369 | 373 |
| 370 | 374 |
| 371 void DebuggerConnectionHandler::AcceptDbgConnection(int debug_fd) { | 375 void DebuggerConnectionHandler::AcceptDbgConnection(int debug_fd) { |
| 372 AddNewDebuggerConnection(debug_fd); | 376 AddNewDebuggerConnection(debug_fd); |
| 373 { | 377 { |
| 374 MonitorLocker ml(&handler_lock_); | 378 ASSERT(handler_lock_ != NULL); |
| 379 MonitorLocker ml(handler_lock_); |
| 375 ml.NotifyAll(); | 380 ml.NotifyAll(); |
| 376 } | 381 } |
| 377 // TODO(asiva): Once we implement support for multiple connections | 382 // TODO(asiva): Once we implement support for multiple connections |
| 378 // we should have a different callback for wakeups on fds which | 383 // we should have a different callback for wakeups on fds which |
| 379 // are not the listener_fd_. | 384 // are not the listener_fd_. |
| 380 // In that callback we would lookup the handler object | 385 // In that callback we would lookup the handler object |
| 381 // corresponding to that fd and invoke HandleMessages on it. | 386 // corresponding to that fd and invoke HandleMessages on it. |
| 382 // For now we run that code here. | 387 // For now we run that code here. |
| 383 DebuggerConnectionHandler* handler = GetDebuggerConnectionHandler(debug_fd); | 388 DebuggerConnectionHandler* handler = GetDebuggerConnectionHandler(debug_fd); |
| 384 if (handler != NULL) { | 389 if (handler != NULL) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 | 450 |
| 446 | 451 |
| 447 bool DebuggerConnectionHandler::IsConnected() { | 452 bool DebuggerConnectionHandler::IsConnected() { |
| 448 // TODO(asiva): Support multiple debugger connections. | 453 // TODO(asiva): Support multiple debugger connections. |
| 449 // Return true if a connection has been established. | 454 // Return true if a connection has been established. |
| 450 return singleton_handler != NULL; | 455 return singleton_handler != NULL; |
| 451 } | 456 } |
| 452 | 457 |
| 453 } // namespace bin | 458 } // namespace bin |
| 454 } // namespace dart | 459 } // namespace dart |
| OLD | NEW |