ulan@chromium.org | 0e3f88b | 2012-05-22 09:16:05 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | |
| 29 | #include "v8.h" |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 30 | #include "debug.h" |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 31 | #include "debug-agent.h" |
| 32 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 33 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 34 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 37 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 38 | // Public V8 debugger API message handler function. This function just delegates |
| 39 | // to the debugger agent through it's data parameter. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 40 | void DebuggerAgentMessageHandler(const v8::Debug::Message& message) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 41 | DebuggerAgent* agent = Isolate::Current()->debugger_agent_instance(); |
| 42 | ASSERT(agent != NULL); |
| 43 | agent->DebuggerMessage(message); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
| 47 | // Debugger agent main thread. |
| 48 | void DebuggerAgent::Run() { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 49 | const int kOneSecondInMicros = 1000000; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 50 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 51 | // Allow this socket to reuse port even if still in TIME_WAIT. |
| 52 | server_->SetReuseAddress(true); |
| 53 | |
| 54 | // First bind the socket to the requested port. |
| 55 | bool bound = false; |
| 56 | while (!bound && !terminate_) { |
| 57 | bound = server_->Bind(port_); |
| 58 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 59 | // If an error occurred wait a bit before retrying. The most common error |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 60 | // would be that the port is already in use so this avoids a busy loop and |
| 61 | // make the agent take over the port when it becomes free. |
| 62 | if (!bound) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 63 | PrintF("Failed to open socket on port %d, " |
| 64 | "waiting %d ms before retrying\n", port_, kOneSecondInMicros / 1000); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 65 | terminate_now_->Wait(kOneSecondInMicros); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Accept connections on the bound port. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 70 | while (!terminate_) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 71 | bool ok = server_->Listen(1); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 72 | listening_->Signal(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 73 | if (ok) { |
| 74 | // Accept the new connection. |
| 75 | Socket* client = server_->Accept(); |
| 76 | ok = client != NULL; |
| 77 | if (ok) { |
| 78 | // Create and start a new session. |
| 79 | CreateSession(client); |
| 80 | } |
| 81 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void DebuggerAgent::Shutdown() { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 87 | // Set the termination flag. |
| 88 | terminate_ = true; |
| 89 | |
| 90 | // Signal termination and make the server exit either its listen call or its |
| 91 | // binding loop. This makes sure that no new sessions can be established. |
| 92 | terminate_now_->Signal(); |
| 93 | server_->Shutdown(); |
| 94 | Join(); |
| 95 | |
| 96 | // Close existing session if any. |
| 97 | CloseSession(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 101 | void DebuggerAgent::WaitUntilListening() { |
| 102 | listening_->Wait(); |
| 103 | } |
| 104 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 105 | static const char* kCreateSessionMessage = |
| 106 | "Remote debugging session already active\r\n"; |
| 107 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 108 | void DebuggerAgent::CreateSession(Socket* client) { |
| 109 | ScopedLock with(session_access_); |
| 110 | |
| 111 | // If another session is already established terminate this one. |
| 112 | if (session_ != NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 113 | client->Send(kCreateSessionMessage, StrLength(kCreateSessionMessage)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 114 | delete client; |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | // Create a new session and hook up the debug message handler. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 119 | session_ = new DebuggerAgentSession(this, client); |
| 120 | isolate_->debugger()->SetMessageHandler(DebuggerAgentMessageHandler); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 121 | session_->Start(); |
| 122 | } |
| 123 | |
| 124 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 125 | void DebuggerAgent::CloseSession() { |
| 126 | ScopedLock with(session_access_); |
| 127 | |
| 128 | // Terminate the session. |
| 129 | if (session_ != NULL) { |
| 130 | session_->Shutdown(); |
| 131 | session_->Join(); |
| 132 | delete session_; |
| 133 | session_ = NULL; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 138 | void DebuggerAgent::DebuggerMessage(const v8::Debug::Message& message) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 139 | ScopedLock with(session_access_); |
| 140 | |
| 141 | // Forward the message handling to the session. |
| 142 | if (session_ != NULL) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 143 | v8::String::Value val(message.GetJSON()); |
| 144 | session_->DebuggerMessage(Vector<uint16_t>(const_cast<uint16_t*>(*val), |
| 145 | val.length())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 150 | void DebuggerAgent::OnSessionClosed(DebuggerAgentSession* session) { |
| 151 | // Don't do anything during termination. |
| 152 | if (terminate_) { |
| 153 | return; |
| 154 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 155 | |
| 156 | // Terminate the session. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 157 | ScopedLock with(session_access_); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 158 | ASSERT(session == session_); |
| 159 | if (session == session_) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 160 | session_->Shutdown(); |
| 161 | delete session_; |
| 162 | session_ = NULL; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | |
| 167 | void DebuggerAgentSession::Run() { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 168 | // Send the hello message. |
| 169 | bool ok = DebuggerAgentUtil::SendConnectMessage(client_, *agent_->name_); |
| 170 | if (!ok) return; |
| 171 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 172 | while (true) { |
| 173 | // Read data from the debugger front end. |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 174 | SmartArrayPointer<char> message = |
| 175 | DebuggerAgentUtil::ReceiveMessage(client_); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 176 | |
| 177 | const char* msg = *message; |
| 178 | bool is_closing_session = (msg == NULL); |
| 179 | |
| 180 | if (msg == NULL) { |
| 181 | // If we lost the connection, then simulate a disconnect msg: |
| 182 | msg = "{\"seq\":1,\"type\":\"request\",\"command\":\"disconnect\"}"; |
| 183 | |
| 184 | } else { |
| 185 | // Check if we're getting a disconnect request: |
| 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; |
| 191 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Convert UTF-8 to UTF-16. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 195 | unibrow::Utf8Decoder<128> decoder(msg, StrLength(msg)); |
| 196 | int utf16_length = decoder.Utf16Length(); |
| 197 | ScopedVector<uint16_t> temp(utf16_length + 1); |
| 198 | decoder.WriteUtf16(temp.start(), utf16_length); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 199 | |
| 200 | // Send the request received to the debugger. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 201 | v8::Debug::SendCommand(temp.start(), |
| 202 | utf16_length, |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 203 | NULL, |
| 204 | reinterpret_cast<v8::Isolate*>(agent_->isolate())); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 205 | |
| 206 | if (is_closing_session) { |
| 207 | // Session is closed. |
| 208 | agent_->OnSessionClosed(this); |
| 209 | return; |
| 210 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | void DebuggerAgentSession::DebuggerMessage(Vector<uint16_t> message) { |
| 216 | DebuggerAgentUtil::SendMessage(client_, message); |
| 217 | } |
| 218 | |
| 219 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 220 | void DebuggerAgentSession::Shutdown() { |
| 221 | // Shutdown the socket to end the blocking receive. |
| 222 | client_->Shutdown(); |
| 223 | } |
| 224 | |
| 225 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 226 | const char* const DebuggerAgentUtil::kContentLength = "Content-Length"; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 227 | |
| 228 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 229 | SmartArrayPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 230 | int received; |
| 231 | |
| 232 | // Read header. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 233 | int content_length = 0; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 234 | while (true) { |
| 235 | const int kHeaderBufferSize = 80; |
| 236 | char header_buffer[kHeaderBufferSize]; |
| 237 | int header_buffer_position = 0; |
| 238 | char c = '\0'; // One character receive buffer. |
| 239 | char prev_c = '\0'; // Previous character. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 240 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 241 | // Read until CRLF. |
| 242 | while (!(c == '\n' && prev_c == '\r')) { |
| 243 | prev_c = c; |
| 244 | received = conn->Receive(&c, 1); |
ulan@chromium.org | 0e3f88b | 2012-05-22 09:16:05 +0000 | [diff] [blame] | 245 | if (received == 0) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 246 | PrintF("Error %d\n", Socket::LastError()); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 247 | return SmartArrayPointer<char>(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 248 | } |
| 249 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 250 | // Add character to header buffer. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 251 | if (header_buffer_position < kHeaderBufferSize) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 252 | header_buffer[header_buffer_position++] = c; |
| 253 | } |
| 254 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 255 | |
| 256 | // Check for end of header (empty header line). |
| 257 | if (header_buffer_position == 2) { // Receive buffer contains CRLF. |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | // Terminate header. |
| 262 | ASSERT(header_buffer_position > 1); // At least CRLF is received. |
| 263 | ASSERT(header_buffer_position <= kHeaderBufferSize); |
| 264 | header_buffer[header_buffer_position - 2] = '\0'; |
| 265 | |
| 266 | // Split header. |
| 267 | char* key = header_buffer; |
| 268 | char* value = NULL; |
| 269 | for (int i = 0; header_buffer[i] != '\0'; i++) { |
| 270 | if (header_buffer[i] == ':') { |
| 271 | header_buffer[i] = '\0'; |
| 272 | value = header_buffer + i + 1; |
| 273 | while (*value == ' ') { |
| 274 | value++; |
| 275 | } |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // Check that key is Content-Length. |
| 281 | if (strcmp(key, kContentLength) == 0) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 282 | // Get the content length value if present and within a sensible range. |
| 283 | if (value == NULL || strlen(value) > 7) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 284 | return SmartArrayPointer<char>(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 285 | } |
| 286 | for (int i = 0; value[i] != '\0'; i++) { |
| 287 | // Bail out if illegal data. |
| 288 | if (value[i] < '0' || value[i] > '9') { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 289 | return SmartArrayPointer<char>(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 290 | } |
| 291 | content_length = 10 * content_length + (value[i] - '0'); |
| 292 | } |
| 293 | } else { |
| 294 | // For now just print all other headers than Content-Length. |
| 295 | PrintF("%s: %s\n", key, value != NULL ? value : "(no value)"); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Return now if no body. |
| 300 | if (content_length == 0) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 301 | return SmartArrayPointer<char>(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // Read body. |
| 305 | char* buffer = NewArray<char>(content_length + 1); |
| 306 | received = ReceiveAll(conn, buffer, content_length); |
| 307 | if (received < content_length) { |
| 308 | PrintF("Error %d\n", Socket::LastError()); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 309 | return SmartArrayPointer<char>(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 310 | } |
| 311 | buffer[content_length] = '\0'; |
| 312 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 313 | return SmartArrayPointer<char>(buffer); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 317 | bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn, |
| 318 | const char* embedding_host) { |
| 319 | static const int kBufferSize = 80; |
| 320 | char buffer[kBufferSize]; // Sending buffer. |
| 321 | bool ok; |
| 322 | int len; |
| 323 | |
| 324 | // Send the header. |
| 325 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 326 | "Type: connect\r\n"); |
| 327 | ok = conn->Send(buffer, len); |
| 328 | if (!ok) return false; |
| 329 | |
| 330 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 331 | "V8-Version: %s\r\n", v8::V8::GetVersion()); |
| 332 | ok = conn->Send(buffer, len); |
| 333 | if (!ok) return false; |
| 334 | |
| 335 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 336 | "Protocol-Version: 1\r\n"); |
| 337 | ok = conn->Send(buffer, len); |
| 338 | if (!ok) return false; |
| 339 | |
| 340 | if (embedding_host != NULL) { |
| 341 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 342 | "Embedding-Host: %s\r\n", embedding_host); |
| 343 | ok = conn->Send(buffer, len); |
| 344 | if (!ok) return false; |
| 345 | } |
| 346 | |
| 347 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 348 | "%s: 0\r\n", kContentLength); |
| 349 | ok = conn->Send(buffer, len); |
| 350 | if (!ok) return false; |
| 351 | |
| 352 | // Terminate header with empty line. |
| 353 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); |
| 354 | ok = conn->Send(buffer, len); |
| 355 | if (!ok) return false; |
| 356 | |
| 357 | // No body for connect message. |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 363 | bool DebuggerAgentUtil::SendMessage(const Socket* conn, |
| 364 | const Vector<uint16_t> message) { |
| 365 | static const int kBufferSize = 80; |
| 366 | char buffer[kBufferSize]; // Sending buffer both for header and body. |
| 367 | |
| 368 | // Calculate the message size in UTF-8 encoding. |
| 369 | int utf8_len = 0; |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 370 | int previous = unibrow::Utf16::kNoPreviousCharacter; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 371 | for (int i = 0; i < message.length(); i++) { |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 372 | uint16_t character = message[i]; |
| 373 | utf8_len += unibrow::Utf8::Length(character, previous); |
| 374 | previous = character; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // Send the header. |
| 378 | int len; |
| 379 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 380 | "%s: %d\r\n", kContentLength, utf8_len); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 381 | conn->Send(buffer, len); |
| 382 | |
| 383 | // Terminate header with empty line. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 384 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 385 | conn->Send(buffer, len); |
| 386 | |
| 387 | // Send message body as UTF-8. |
| 388 | int buffer_position = 0; // Current buffer position. |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 389 | previous = unibrow::Utf16::kNoPreviousCharacter; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 390 | for (int i = 0; i < message.length(); i++) { |
| 391 | // Write next UTF-8 encoded character to buffer. |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 392 | uint16_t character = message[i]; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 393 | buffer_position += |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 394 | unibrow::Utf8::Encode(buffer + buffer_position, character, previous); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 395 | ASSERT(buffer_position <= kBufferSize); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 396 | |
| 397 | // Send buffer if full or last character is encoded. |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 398 | if (kBufferSize - buffer_position < |
| 399 | unibrow::Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit || |
| 400 | i == message.length() - 1) { |
| 401 | if (unibrow::Utf16::IsLeadSurrogate(character)) { |
| 402 | const int kEncodedSurrogateLength = |
| 403 | unibrow::Utf16::kUtf8BytesToCodeASurrogate; |
| 404 | ASSERT(buffer_position >= kEncodedSurrogateLength); |
| 405 | conn->Send(buffer, buffer_position - kEncodedSurrogateLength); |
| 406 | for (int i = 0; i < kEncodedSurrogateLength; i++) { |
| 407 | buffer[i] = buffer[buffer_position + i]; |
| 408 | } |
| 409 | buffer_position = kEncodedSurrogateLength; |
| 410 | } else { |
| 411 | conn->Send(buffer, buffer_position); |
| 412 | buffer_position = 0; |
| 413 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 414 | } |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 415 | previous = character; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | return true; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | bool DebuggerAgentUtil::SendMessage(const Socket* conn, |
| 423 | const v8::Handle<v8::String> request) { |
| 424 | static const int kBufferSize = 80; |
| 425 | char buffer[kBufferSize]; // Sending buffer both for header and body. |
| 426 | |
| 427 | // Convert the request to UTF-8 encoding. |
| 428 | v8::String::Utf8Value utf8_request(request); |
| 429 | |
| 430 | // Send the header. |
| 431 | int len; |
| 432 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 433 | "Content-Length: %d\r\n", utf8_request.length()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 434 | conn->Send(buffer, len); |
| 435 | |
| 436 | // Terminate header with empty line. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 437 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 438 | conn->Send(buffer, len); |
| 439 | |
| 440 | // Send message body as UTF-8. |
| 441 | conn->Send(*utf8_request, utf8_request.length()); |
| 442 | |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | // Receive the full buffer before returning unless an error occours. |
| 448 | int DebuggerAgentUtil::ReceiveAll(const Socket* conn, char* data, int len) { |
| 449 | int total_received = 0; |
| 450 | while (total_received < len) { |
| 451 | int received = conn->Receive(data + total_received, len - total_received); |
ulan@chromium.org | 0e3f88b | 2012-05-22 09:16:05 +0000 | [diff] [blame] | 452 | if (received == 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 453 | return total_received; |
| 454 | } |
| 455 | total_received += received; |
| 456 | } |
| 457 | return total_received; |
| 458 | } |
| 459 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 460 | } } // namespace v8::internal |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 461 | |
| 462 | #endif // ENABLE_DEBUGGER_SUPPORT |