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