| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 // Public V8 debugger API message handler function. This function just delegates | 35 // Public V8 debugger API message handler function. This function just delegates |
| 36 // to the debugger agent through it's data parameter. | 36 // to the debugger agent through it's data parameter. |
| 37 void DebuggerAgentMessageHandler(const uint16_t* message, int length, | 37 void DebuggerAgentMessageHandler(const uint16_t* message, int length, |
| 38 void *data) { | 38 void *data) { |
| 39 reinterpret_cast<DebuggerAgent*>(data)->DebuggerMessage(message, length); | 39 reinterpret_cast<DebuggerAgent*>(data)->DebuggerMessage(message, length); |
| 40 } | 40 } |
| 41 | 41 |
| 42 | 42 |
| 43 // Debugger agent main thread. | 43 // Debugger agent main thread. |
| 44 void DebuggerAgent::Run() { | 44 void DebuggerAgent::Run() { |
| 45 // Create a server socket and bind it to the requested port. | 45 const int kOneSecondInMicros = 1000000; |
| 46 server_ = OS::CreateSocket(); | |
| 47 server_->Bind(port_); | |
| 48 | 46 |
| 47 // First bind the socket to the requested port. |
| 48 bool bound = false; |
| 49 while (!bound && !terminate_) { |
| 50 bound = server_->Bind(port_); |
| 51 |
| 52 // If an error occoured wait a bit before retrying. The most common error |
| 53 // would be that the port is already in use so this avoids a busy loop and |
| 54 // make the agent take over the port when it becomes free. |
| 55 if (!bound) { |
| 56 terminate_now_->Wait(kOneSecondInMicros); |
| 57 } |
| 58 } |
| 59 |
| 60 // Accept connections on the bound port. |
| 49 while (!terminate_) { | 61 while (!terminate_) { |
| 50 // Listen for new connections. | 62 bool ok = server_->Listen(1); |
| 51 server_->Listen(1); | 63 if (ok) { |
| 52 | 64 // Accept the new connection. |
| 53 // Accept the new connection. | 65 Socket* client = server_->Accept(); |
| 54 Socket* client = server_->Accept(); | 66 ok = client != NULL; |
| 55 | 67 if (ok) { |
| 56 // Create and start a new session. | 68 // Create and start a new session. |
| 57 CreateSession(client); | 69 CreateSession(client); |
| 70 } |
| 71 } |
| 58 } | 72 } |
| 59 } | 73 } |
| 60 | 74 |
| 61 | 75 |
| 62 void DebuggerAgent::Shutdown() { | 76 void DebuggerAgent::Shutdown() { |
| 63 delete server_; | 77 // Set the termination flag. |
| 78 terminate_ = true; |
| 79 |
| 80 // Signal termination and make the server exit either its listen call or its |
| 81 // binding loop. This makes sure that no new sessions can be established. |
| 82 terminate_now_->Signal(); |
| 83 server_->Shutdown(); |
| 84 Join(); |
| 85 |
| 86 // Close existing session if any. |
| 87 CloseSession(); |
| 64 } | 88 } |
| 65 | 89 |
| 66 | 90 |
| 67 void DebuggerAgent::CreateSession(Socket* client) { | 91 void DebuggerAgent::CreateSession(Socket* client) { |
| 68 ScopedLock with(session_access_); | 92 ScopedLock with(session_access_); |
| 69 | 93 |
| 70 // If another session is already established terminate this one. | 94 // If another session is already established terminate this one. |
| 71 if (session_ != NULL) { | 95 if (session_ != NULL) { |
| 72 static const char* message = "Remote debugging session already active\n"; | 96 static const char* message = "Remote debugging session already active\n"; |
| 73 | 97 |
| 74 client->Send(message, strlen(message)); | 98 client->Send(message, strlen(message)); |
| 75 delete client; | 99 delete client; |
| 76 return; | 100 return; |
| 77 } | 101 } |
| 78 | 102 |
| 79 // Create a new session and hook up the debug message handler. | 103 // Create a new session and hook up the debug message handler. |
| 80 session_ = new DebuggerAgentSession(this, client); | 104 session_ = new DebuggerAgentSession(this, client); |
| 81 v8::Debug::SetMessageHandler(DebuggerAgentMessageHandler, this); | 105 v8::Debug::SetMessageHandler(DebuggerAgentMessageHandler, this); |
| 82 session_->Start(); | 106 session_->Start(); |
| 83 } | 107 } |
| 84 | 108 |
| 85 | 109 |
| 110 void DebuggerAgent::CloseSession() { |
| 111 ScopedLock with(session_access_); |
| 112 |
| 113 // Terminate the session. |
| 114 if (session_ != NULL) { |
| 115 session_->Shutdown(); |
| 116 session_->Join(); |
| 117 delete session_; |
| 118 session_ = NULL; |
| 119 } |
| 120 } |
| 121 |
| 122 |
| 86 void DebuggerAgent::DebuggerMessage(const uint16_t* message, int length) { | 123 void DebuggerAgent::DebuggerMessage(const uint16_t* message, int length) { |
| 87 ScopedLock with(session_access_); | 124 ScopedLock with(session_access_); |
| 88 | 125 |
| 89 // Forward the message handling to the session. | 126 // Forward the message handling to the session. |
| 90 if (session_ != NULL) { | 127 if (session_ != NULL) { |
| 91 session_->DebuggerMessage(Vector<uint16_t>(const_cast<uint16_t*>(message), | 128 session_->DebuggerMessage(Vector<uint16_t>(const_cast<uint16_t*>(message), |
| 92 length)); | 129 length)); |
| 93 } | 130 } |
| 94 } | 131 } |
| 95 | 132 |
| 96 | 133 |
| 97 void DebuggerAgent::SessionClosed(DebuggerAgentSession* session) { | 134 void DebuggerAgent::OnSessionClosed(DebuggerAgentSession* session) { |
| 98 ScopedLock with(session_access_); | 135 // Don't do anything during termination. |
| 136 if (terminate_) { |
| 137 return; |
| 138 } |
| 99 | 139 |
| 100 // Terminate the session. | 140 // Terminate the session. |
| 141 ScopedLock with(session_access_); |
| 101 ASSERT(session == session_); | 142 ASSERT(session == session_); |
| 102 if (session == session_) { | 143 if (session == session_) { |
| 103 session->Join(); | 144 CloseSession(); |
| 104 delete session; | |
| 105 session_ = NULL; | |
| 106 } | 145 } |
| 107 } | 146 } |
| 108 | 147 |
| 109 | 148 |
| 110 void DebuggerAgentSession::Run() { | 149 void DebuggerAgentSession::Run() { |
| 111 while (true) { | 150 while (true) { |
| 112 // Read data from the debugger front end. | 151 // Read data from the debugger front end. |
| 113 SmartPointer<char> message = DebuggerAgentUtil::ReceiveMessage(client_); | 152 SmartPointer<char> message = DebuggerAgentUtil::ReceiveMessage(client_); |
| 114 if (*message == NULL) { | 153 if (*message == NULL) { |
| 115 // Session is closed. | 154 // Session is closed. |
| 116 agent_->SessionClosed(this); | 155 agent_->OnSessionClosed(this); |
| 117 return; | 156 return; |
| 118 } | 157 } |
| 119 | 158 |
| 120 // Convert UTF-8 to UTF-16. | 159 // Convert UTF-8 to UTF-16. |
| 121 unibrow::Utf8InputBuffer<> buf(*message, strlen(*message)); | 160 unibrow::Utf8InputBuffer<> buf(*message, strlen(*message)); |
| 122 int len = 0; | 161 int len = 0; |
| 123 while (buf.has_more()) { | 162 while (buf.has_more()) { |
| 124 buf.GetNext(); | 163 buf.GetNext(); |
| 125 len++; | 164 len++; |
| 126 } | 165 } |
| 127 int16_t* temp = NewArray<int16_t>(len + 1); | 166 int16_t* temp = NewArray<int16_t>(len + 1); |
| 128 buf.Reset(*message, strlen(*message)); | 167 buf.Reset(*message, strlen(*message)); |
| 129 for (int i = 0; i < len; i++) { | 168 for (int i = 0; i < len; i++) { |
| 130 temp[i] = buf.GetNext(); | 169 temp[i] = buf.GetNext(); |
| 131 } | 170 } |
| 132 | 171 |
| 133 // Send the request received to the debugger. | 172 // Send the request received to the debugger. |
| 134 v8::Debug::SendCommand(reinterpret_cast<const uint16_t *>(temp), len); | 173 v8::Debug::SendCommand(reinterpret_cast<const uint16_t *>(temp), len); |
| 135 DeleteArray(temp); | 174 DeleteArray(temp); |
| 136 } | 175 } |
| 137 } | 176 } |
| 138 | 177 |
| 139 | 178 |
| 140 void DebuggerAgentSession::DebuggerMessage(Vector<uint16_t> message) { | 179 void DebuggerAgentSession::DebuggerMessage(Vector<uint16_t> message) { |
| 141 DebuggerAgentUtil::SendMessage(client_, message); | 180 DebuggerAgentUtil::SendMessage(client_, message); |
| 142 } | 181 } |
| 143 | 182 |
| 144 | 183 |
| 184 void DebuggerAgentSession::Shutdown() { |
| 185 // Shutdown the socket to end the blocking receive. |
| 186 client_->Shutdown(); |
| 187 } |
| 188 |
| 189 |
| 145 const char* DebuggerAgentUtil::kContentLength = "Content-Length"; | 190 const char* DebuggerAgentUtil::kContentLength = "Content-Length"; |
| 146 int DebuggerAgentUtil::kContentLengthSize = strlen(kContentLength); | 191 int DebuggerAgentUtil::kContentLengthSize = strlen(kContentLength); |
| 147 | 192 |
| 148 | 193 |
| 149 SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { | 194 SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { |
| 150 int received; | 195 int received; |
| 151 | 196 |
| 152 // Read header. | 197 // Read header. |
| 153 const int kHeaderBufferSize = 80; | 198 const int kHeaderBufferSize = 80; |
| 154 char header_buffer[kHeaderBufferSize]; | 199 char header_buffer[kHeaderBufferSize]; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 if (received <= 0) { | 347 if (received <= 0) { |
| 303 return total_received; | 348 return total_received; |
| 304 } | 349 } |
| 305 total_received += received; | 350 total_received += received; |
| 306 } | 351 } |
| 307 return total_received; | 352 return total_received; |
| 308 } | 353 } |
| 309 | 354 |
| 310 | 355 |
| 311 } } // namespace v8::internal | 356 } } // namespace v8::internal |
| OLD | NEW |