kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 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_) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 160 | CloseSession(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void DebuggerAgentSession::Run() { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 166 | // Send the hello message. |
| 167 | bool ok = DebuggerAgentUtil::SendConnectMessage(client_, *agent_->name_); |
| 168 | if (!ok) return; |
| 169 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 170 | while (true) { |
| 171 | // Read data from the debugger front end. |
| 172 | SmartPointer<char> message = DebuggerAgentUtil::ReceiveMessage(client_); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 173 | |
| 174 | const char* msg = *message; |
| 175 | bool is_closing_session = (msg == NULL); |
| 176 | |
| 177 | if (msg == NULL) { |
| 178 | // If we lost the connection, then simulate a disconnect msg: |
| 179 | msg = "{\"seq\":1,\"type\":\"request\",\"command\":\"disconnect\"}"; |
| 180 | |
| 181 | } else { |
| 182 | // Check if we're getting a disconnect request: |
| 183 | const char* disconnectRequestStr = |
| 184 | "\"type\":\"request\",\"command\":\"disconnect\"}"; |
| 185 | const char* result = strstr(msg, disconnectRequestStr); |
| 186 | if (result != NULL) { |
| 187 | is_closing_session = true; |
| 188 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // Convert UTF-8 to UTF-16. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 192 | unibrow::Utf8InputBuffer<> buf(msg, StrLength(msg)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 193 | int len = 0; |
| 194 | while (buf.has_more()) { |
| 195 | buf.GetNext(); |
| 196 | len++; |
| 197 | } |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 198 | ScopedVector<int16_t> temp(len + 1); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 199 | buf.Reset(msg, StrLength(msg)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 200 | for (int i = 0; i < len; i++) { |
| 201 | temp[i] = buf.GetNext(); |
| 202 | } |
| 203 | |
| 204 | // Send the request received to the debugger. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 205 | v8::Debug::SendCommand(reinterpret_cast<const uint16_t *>(temp.start()), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 206 | len, |
| 207 | NULL, |
| 208 | reinterpret_cast<v8::Isolate*>(agent_->isolate())); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 209 | |
| 210 | if (is_closing_session) { |
| 211 | // Session is closed. |
| 212 | agent_->OnSessionClosed(this); |
| 213 | return; |
| 214 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | void DebuggerAgentSession::DebuggerMessage(Vector<uint16_t> message) { |
| 220 | DebuggerAgentUtil::SendMessage(client_, message); |
| 221 | } |
| 222 | |
| 223 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 224 | void DebuggerAgentSession::Shutdown() { |
| 225 | // Shutdown the socket to end the blocking receive. |
| 226 | client_->Shutdown(); |
| 227 | } |
| 228 | |
| 229 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 230 | const char* const DebuggerAgentUtil::kContentLength = "Content-Length"; |
| 231 | const int DebuggerAgentUtil::kContentLengthSize = |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 232 | StrLength(kContentLength); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 233 | |
| 234 | |
| 235 | SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) { |
| 236 | int received; |
| 237 | |
| 238 | // Read header. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 239 | int content_length = 0; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 240 | while (true) { |
| 241 | const int kHeaderBufferSize = 80; |
| 242 | char header_buffer[kHeaderBufferSize]; |
| 243 | int header_buffer_position = 0; |
| 244 | char c = '\0'; // One character receive buffer. |
| 245 | char prev_c = '\0'; // Previous character. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 246 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 247 | // Read until CRLF. |
| 248 | while (!(c == '\n' && prev_c == '\r')) { |
| 249 | prev_c = c; |
| 250 | received = conn->Receive(&c, 1); |
| 251 | if (received <= 0) { |
| 252 | PrintF("Error %d\n", Socket::LastError()); |
| 253 | return SmartPointer<char>(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 254 | } |
| 255 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 256 | // Add character to header buffer. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 257 | if (header_buffer_position < kHeaderBufferSize) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 258 | header_buffer[header_buffer_position++] = c; |
| 259 | } |
| 260 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 261 | |
| 262 | // Check for end of header (empty header line). |
| 263 | if (header_buffer_position == 2) { // Receive buffer contains CRLF. |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | // Terminate header. |
| 268 | ASSERT(header_buffer_position > 1); // At least CRLF is received. |
| 269 | ASSERT(header_buffer_position <= kHeaderBufferSize); |
| 270 | header_buffer[header_buffer_position - 2] = '\0'; |
| 271 | |
| 272 | // Split header. |
| 273 | char* key = header_buffer; |
| 274 | char* value = NULL; |
| 275 | for (int i = 0; header_buffer[i] != '\0'; i++) { |
| 276 | if (header_buffer[i] == ':') { |
| 277 | header_buffer[i] = '\0'; |
| 278 | value = header_buffer + i + 1; |
| 279 | while (*value == ' ') { |
| 280 | value++; |
| 281 | } |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // Check that key is Content-Length. |
| 287 | if (strcmp(key, kContentLength) == 0) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 288 | // Get the content length value if present and within a sensible range. |
| 289 | if (value == NULL || strlen(value) > 7) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 290 | return SmartPointer<char>(); |
| 291 | } |
| 292 | for (int i = 0; value[i] != '\0'; i++) { |
| 293 | // Bail out if illegal data. |
| 294 | if (value[i] < '0' || value[i] > '9') { |
| 295 | return SmartPointer<char>(); |
| 296 | } |
| 297 | content_length = 10 * content_length + (value[i] - '0'); |
| 298 | } |
| 299 | } else { |
| 300 | // For now just print all other headers than Content-Length. |
| 301 | PrintF("%s: %s\n", key, value != NULL ? value : "(no value)"); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Return now if no body. |
| 306 | if (content_length == 0) { |
| 307 | return SmartPointer<char>(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Read body. |
| 311 | char* buffer = NewArray<char>(content_length + 1); |
| 312 | received = ReceiveAll(conn, buffer, content_length); |
| 313 | if (received < content_length) { |
| 314 | PrintF("Error %d\n", Socket::LastError()); |
| 315 | return SmartPointer<char>(); |
| 316 | } |
| 317 | buffer[content_length] = '\0'; |
| 318 | |
| 319 | return SmartPointer<char>(buffer); |
| 320 | } |
| 321 | |
| 322 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 323 | bool DebuggerAgentUtil::SendConnectMessage(const Socket* conn, |
| 324 | const char* embedding_host) { |
| 325 | static const int kBufferSize = 80; |
| 326 | char buffer[kBufferSize]; // Sending buffer. |
| 327 | bool ok; |
| 328 | int len; |
| 329 | |
| 330 | // Send the header. |
| 331 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 332 | "Type: connect\r\n"); |
| 333 | ok = conn->Send(buffer, len); |
| 334 | if (!ok) return false; |
| 335 | |
| 336 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 337 | "V8-Version: %s\r\n", v8::V8::GetVersion()); |
| 338 | ok = conn->Send(buffer, len); |
| 339 | if (!ok) return false; |
| 340 | |
| 341 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 342 | "Protocol-Version: 1\r\n"); |
| 343 | ok = conn->Send(buffer, len); |
| 344 | if (!ok) return false; |
| 345 | |
| 346 | if (embedding_host != NULL) { |
| 347 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 348 | "Embedding-Host: %s\r\n", embedding_host); |
| 349 | ok = conn->Send(buffer, len); |
| 350 | if (!ok) return false; |
| 351 | } |
| 352 | |
| 353 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
| 354 | "%s: 0\r\n", kContentLength); |
| 355 | ok = conn->Send(buffer, len); |
| 356 | if (!ok) return false; |
| 357 | |
| 358 | // Terminate header with empty line. |
| 359 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); |
| 360 | ok = conn->Send(buffer, len); |
| 361 | if (!ok) return false; |
| 362 | |
| 363 | // No body for connect message. |
| 364 | |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 369 | bool DebuggerAgentUtil::SendMessage(const Socket* conn, |
| 370 | const Vector<uint16_t> message) { |
| 371 | static const int kBufferSize = 80; |
| 372 | char buffer[kBufferSize]; // Sending buffer both for header and body. |
| 373 | |
| 374 | // Calculate the message size in UTF-8 encoding. |
| 375 | int utf8_len = 0; |
| 376 | for (int i = 0; i < message.length(); i++) { |
| 377 | utf8_len += unibrow::Utf8::Length(message[i]); |
| 378 | } |
| 379 | |
| 380 | // Send the header. |
| 381 | int len; |
| 382 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 383 | "%s: %d\r\n", kContentLength, utf8_len); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 384 | conn->Send(buffer, len); |
| 385 | |
| 386 | // Terminate header with empty line. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 387 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 388 | conn->Send(buffer, len); |
| 389 | |
| 390 | // Send message body as UTF-8. |
| 391 | int buffer_position = 0; // Current buffer position. |
| 392 | for (int i = 0; i < message.length(); i++) { |
| 393 | // Write next UTF-8 encoded character to buffer. |
| 394 | buffer_position += |
| 395 | unibrow::Utf8::Encode(buffer + buffer_position, message[i]); |
| 396 | ASSERT(buffer_position < kBufferSize); |
| 397 | |
| 398 | // Send buffer if full or last character is encoded. |
| 399 | if (kBufferSize - buffer_position < 3 || i == message.length() - 1) { |
| 400 | conn->Send(buffer, buffer_position); |
| 401 | buffer_position = 0; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | bool DebuggerAgentUtil::SendMessage(const Socket* conn, |
| 410 | const v8::Handle<v8::String> request) { |
| 411 | static const int kBufferSize = 80; |
| 412 | char buffer[kBufferSize]; // Sending buffer both for header and body. |
| 413 | |
| 414 | // Convert the request to UTF-8 encoding. |
| 415 | v8::String::Utf8Value utf8_request(request); |
| 416 | |
| 417 | // Send the header. |
| 418 | int len; |
| 419 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 420 | "Content-Length: %d\r\n", utf8_request.length()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 421 | conn->Send(buffer, len); |
| 422 | |
| 423 | // Terminate header with empty line. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 424 | len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\r\n"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 425 | conn->Send(buffer, len); |
| 426 | |
| 427 | // Send message body as UTF-8. |
| 428 | conn->Send(*utf8_request, utf8_request.length()); |
| 429 | |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | // Receive the full buffer before returning unless an error occours. |
| 435 | int DebuggerAgentUtil::ReceiveAll(const Socket* conn, char* data, int len) { |
| 436 | int total_received = 0; |
| 437 | while (total_received < len) { |
| 438 | int received = conn->Receive(data + total_received, len - total_received); |
| 439 | if (received <= 0) { |
| 440 | return total_received; |
| 441 | } |
| 442 | total_received += received; |
| 443 | } |
| 444 | return total_received; |
| 445 | } |
| 446 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 447 | } } // namespace v8::internal |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 448 | |
| 449 | #endif // ENABLE_DEBUGGER_SUPPORT |