Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(645)

Side by Side Diff: src/debug-agent.cc

Issue 27355: Add V8 debugger agent.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/debug-agent.h ('k') | src/flag-definitions.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ native
OLDNEW
(Empty)
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"
30 #include "debug-agent.h"
31
32 namespace v8 { namespace internal {
33
34
35 // Public V8 debugger API message handler function. This function just delegates
36 // to the debugger agent through it's data parameter.
37 void DebuggerAgentMessageHandler(const uint16_t* message, int length,
38 void *data) {
39 reinterpret_cast<DebuggerAgent*>(data)->DebuggerMessage(message, length);
40 }
41
42
43 // Debugger agent main thread.
44 void DebuggerAgent::Run() {
45 // Create a server socket and bind it to the requested port.
46 server_ = OS::CreateSocket();
47 server_->Bind(port_);
48
49 while (!terminate_) {
50 // Listen for new connections.
51 server_->Listen(1);
52
53 // Accept the new connection.
54 Socket* client = server_->Accept();
55
56 // Create and start a new session.
57 CreateSession(client);
58 }
59 }
60
61
62 void DebuggerAgent::Shutdown() {
63 delete server_;
64 }
65
66
67 void DebuggerAgent::CreateSession(Socket* client) {
68 ScopedLock with(session_access_);
69
70 // If another session is already established terminate this one.
71 if (session_ != NULL) {
72 static const char* message = "Remote debugging session already active\n";
73
74 client->Send(message, strlen(message));
75 delete client;
76 return;
77 }
78
79 // Create a new session and hook up the debug message handler.
80 session_ = new DebuggerAgentSession(this, client);
81 v8::Debug::SetMessageHandler(DebuggerAgentMessageHandler, this);
82 session_->Start();
83 }
84
85
86 void DebuggerAgent::DebuggerMessage(const uint16_t* message, int length) {
87 ScopedLock with(session_access_);
88
89 // Forward the message handling to the session.
90 if (session_ != NULL) {
91 session_->DebuggerMessage(Vector<uint16_t>(const_cast<uint16_t*>(message),
92 length));
93 }
94 }
95
96
97 void DebuggerAgent::SessionClosed(DebuggerAgentSession* session) {
98 ScopedLock with(session_access_);
99
100 // Terminate the session.
101 ASSERT(session == session_);
102 if (session == session_) {
103 session->Join();
104 delete session;
105 session_ = NULL;
106 }
107 }
108
109
110 void DebuggerAgentSession::Run() {
111 while (true) {
112 // Read data from the debugger front end.
113 SmartPointer<char> message = DebuggerAgentUtil::ReceiveMessage(client_);
114 if (*message == NULL) {
115 // Session is closed.
116 agent_->SessionClosed(this);
117 return;
118 }
119
120 // Convert UTF-8 to UTF-16.
121 unibrow::Utf8InputBuffer<> buf(*message, strlen(*message));
122 int len = 0;
123 while (buf.has_more()) {
124 buf.GetNext();
125 len++;
126 }
127 int16_t* temp = NewArray<int16_t>(len + 1);
128 buf.Reset(*message, strlen(*message));
129 for (int i = 0; i < len; i++) {
130 temp[i] = buf.GetNext();
131 }
132
133 // Send the request received to the debugger.
134 v8::Debug::SendCommand(reinterpret_cast<const uint16_t *>(temp), len);
135 DeleteArray(temp);
136 }
137 }
138
139
140 void DebuggerAgentSession::DebuggerMessage(Vector<uint16_t> message) {
141 DebuggerAgentUtil::SendMessage(client_, message);
142 }
143
144
145 const char* DebuggerAgentUtil::kContentLength = "Content-Length";
146 int DebuggerAgentUtil::kContentLengthSize = strlen(kContentLength);
147
148
149 SmartPointer<char> DebuggerAgentUtil::ReceiveMessage(const Socket* conn) {
150 int received;
151
152 // Read header.
153 const int kHeaderBufferSize = 80;
154 char header_buffer[kHeaderBufferSize];
155 int header_buffer_position = 0;
156 char c = '\0'; // One character receive buffer.
157 char last_c = '\0'; // Previous character.
158 int content_length = 0;
159 while (!(c == '\n' && last_c == '\n')) {
160 last_c = c;
161 received = conn->Receive(&c, 1);
162 if (received <= 0) {
163 PrintF("Error %d\n", Socket::LastError());
164 return SmartPointer<char>();
165 }
166
167 // Check for end of header line.
168 if (c == '\n') {
169 // Empty header line.
170 if (header_buffer_position == 0) {
171 continue;
172 }
173
174 // Terminate header.
175 ASSERT(header_buffer_position < kHeaderBufferSize);
176 if (header_buffer_position < kHeaderBufferSize) {
177 header_buffer[header_buffer_position] = '\0';
178 }
179
180 // Split header.
181 char* key = header_buffer;
182 char* value = NULL;
183 for (int i = 0; i < header_buffer_position; i++) {
184 if (header_buffer[i] == ':') {
185 header_buffer[i] = '\0';
186 value = header_buffer + i + 1;
187 while (*value == ' ') {
188 value++;
189 }
190 break;
191 }
192 }
193
194 // Check that key is Content-Length.
195 if (strcmp(key, kContentLength) == 0) {
196 // Get the content length value if within a sensible range.
197 if (strlen(value) > 7) {
198 return SmartPointer<char>();
199 }
200 for (int i = 0; value[i] != '\0'; i++) {
201 // Bail out if illegal data.
202 if (value[i] < '0' || value[i] > '9') {
203 return SmartPointer<char>();
204 }
205 content_length = 10 * content_length + (value[i] - '0');
206 }
207 }
208
209 // Start collecting new header.
210 header_buffer_position = 0;
211 } else {
212 // Add character to header buffer (reserve room for terminating '\0').
213 if (header_buffer_position < kHeaderBufferSize - 1) {
214 header_buffer[header_buffer_position++] = c;
215 }
216 }
217 }
218
219 // Read body.
220 char* buffer = NewArray<char>(content_length + 1);
221 received = ReceiveAll(conn, buffer, content_length);
222 if (received < content_length) {
223 PrintF("Error %d\n", Socket::LastError());
224 return SmartPointer<char>();
225 }
226 buffer[content_length] = '\0';
227
228 return SmartPointer<char>(buffer);
229 }
230
231
232 bool DebuggerAgentUtil::SendMessage(const Socket* conn,
233 const Vector<uint16_t> message) {
234 static const int kBufferSize = 80;
235 char buffer[kBufferSize]; // Sending buffer both for header and body.
236
237 // Calculate the message size in UTF-8 encoding.
238 int utf8_len = 0;
239 for (int i = 0; i < message.length(); i++) {
240 utf8_len += unibrow::Utf8::Length(message[i]);
241 }
242
243 // Send the header.
244 int len;
245 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
246 "Content-Length: %d\n", utf8_len);
247 conn->Send(buffer, len);
248
249 // Terminate header with empty line.
250 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n");
251 conn->Send(buffer, len);
252
253 // Send message body as UTF-8.
254 int buffer_position = 0; // Current buffer position.
255 for (int i = 0; i < message.length(); i++) {
256 // Write next UTF-8 encoded character to buffer.
257 buffer_position +=
258 unibrow::Utf8::Encode(buffer + buffer_position, message[i]);
259 ASSERT(buffer_position < kBufferSize);
260
261 // Send buffer if full or last character is encoded.
262 if (kBufferSize - buffer_position < 3 || i == message.length() - 1) {
263 conn->Send(buffer, buffer_position);
264 buffer_position = 0;
265 }
266 }
267
268 return true;
269 }
270
271
272 bool DebuggerAgentUtil::SendMessage(const Socket* conn,
273 const v8::Handle<v8::String> request) {
274 static const int kBufferSize = 80;
275 char buffer[kBufferSize]; // Sending buffer both for header and body.
276
277 // Convert the request to UTF-8 encoding.
278 v8::String::Utf8Value utf8_request(request);
279
280 // Send the header.
281 int len;
282 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize),
283 "Content-Length: %d\n", utf8_request.length());
284 conn->Send(buffer, len);
285
286 // Terminate header with empty line.
287 len = OS::SNPrintF(Vector<char>(buffer, kBufferSize), "\n");
288 conn->Send(buffer, len);
289
290 // Send message body as UTF-8.
291 conn->Send(*utf8_request, utf8_request.length());
292
293 return true;
294 }
295
296
297 // Receive the full buffer before returning unless an error occours.
298 int DebuggerAgentUtil::ReceiveAll(const Socket* conn, char* data, int len) {
299 int total_received = 0;
300 while (total_received < len) {
301 int received = conn->Receive(data + total_received, len - total_received);
302 if (received <= 0) {
303 return total_received;
304 }
305 total_received += received;
306 }
307 return total_received;
308 }
309
310
311 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/debug-agent.h ('k') | src/flag-definitions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698