blob: f044328ae1f2157bb7e12637723c1ddbc11db321 [file] [log] [blame]
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00001// Copyright 2012 the V8 project authors. All rights reserved.
ager@chromium.org32912102009-01-16 10:38:43 +00002// 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
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000028#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org32912102009-01-16 10:38:43 +000029
30#include "d8.h"
31#include "d8-debug.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000032#include "platform.h"
33#include "debug-agent.h"
ager@chromium.org32912102009-01-16 10:38:43 +000034
35
36namespace v8 {
37
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +000038static bool was_running = true;
39
40void PrintPrompt(bool is_running) {
41 const char* prompt = is_running? "> " : "dbg> ";
42 was_running = is_running;
43 printf("%s", prompt);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000044 fflush(stdout);
45}
46
ager@chromium.org32912102009-01-16 10:38:43 +000047
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +000048void PrintPrompt() {
49 PrintPrompt(was_running);
50}
51
52
ager@chromium.org32912102009-01-16 10:38:43 +000053void HandleDebugEvent(DebugEvent event,
54 Handle<Object> exec_state,
55 Handle<Object> event_data,
56 Handle<Value> data) {
57 HandleScope scope;
58
iposva@chromium.org245aa852009-02-10 00:49:54 +000059 // Check for handled event.
60 if (event != Break && event != Exception && event != AfterCompile) {
61 return;
62 }
ager@chromium.org32912102009-01-16 10:38:43 +000063
64 TryCatch try_catch;
65
kasperl@chromium.org061ef742009-02-27 12:16:20 +000066 // Get the toJSONProtocol function on the event and get the JSON format.
67 Local<String> to_json_fun_name = String::New("toJSONProtocol");
68 Local<Function> to_json_fun =
69 Function::Cast(*event_data->Get(to_json_fun_name));
70 Local<Value> event_json = to_json_fun->Call(event_data, 0, NULL);
71 if (try_catch.HasCaught()) {
72 Shell::ReportException(&try_catch);
73 return;
74 }
75
ager@chromium.org32912102009-01-16 10:38:43 +000076 // Print the event details.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000077 Handle<Object> details =
78 Shell::DebugMessageDetails(Handle<String>::Cast(event_json));
79 if (try_catch.HasCaught()) {
80 Shell::ReportException(&try_catch);
81 return;
82 }
83 String::Utf8Value str(details->Get(String::New("text")));
84 if (str.length() == 0) {
iposva@chromium.org245aa852009-02-10 00:49:54 +000085 // Empty string is used to signal not to process this event.
86 return;
87 }
ager@chromium.org32912102009-01-16 10:38:43 +000088 printf("%s\n", *str);
89
90 // Get the debug command processor.
91 Local<String> fun_name = String::New("debugCommandProcessor");
92 Local<Function> fun = Function::Cast(*exec_state->Get(fun_name));
93 Local<Object> cmd_processor =
94 Object::Cast(*fun->Call(exec_state, 0, NULL));
95 if (try_catch.HasCaught()) {
96 Shell::ReportException(&try_catch);
97 return;
98 }
99
100 static const int kBufferSize = 256;
101 bool running = false;
102 while (!running) {
103 char command[kBufferSize];
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000104 PrintPrompt(running);
ager@chromium.org32912102009-01-16 10:38:43 +0000105 char* str = fgets(command, kBufferSize, stdin);
106 if (str == NULL) break;
107
108 // Ignore empty commands.
109 if (strlen(command) == 0) continue;
110
111 TryCatch try_catch;
112
113 // Convert the debugger command to a JSON debugger request.
114 Handle<Value> request =
115 Shell::DebugCommandToJSONRequest(String::New(command));
116 if (try_catch.HasCaught()) {
117 Shell::ReportException(&try_catch);
118 continue;
119 }
120
121 // If undefined is returned the command was handled internally and there is
122 // no JSON to send.
123 if (request->IsUndefined()) {
124 continue;
125 }
126
127 Handle<String> fun_name;
128 Handle<Function> fun;
129 // All the functions used below take one argument.
130 static const int kArgc = 1;
131 Handle<Value> args[kArgc];
132
133 // Invoke the JavaScript to convert the debug command line to a JSON
134 // request, invoke the JSON request and convert the JSON respose to a text
135 // representation.
136 fun_name = String::New("processDebugRequest");
137 fun = Handle<Function>::Cast(cmd_processor->Get(fun_name));
138 args[0] = request;
139 Handle<Value> response_val = fun->Call(cmd_processor, kArgc, args);
140 if (try_catch.HasCaught()) {
141 Shell::ReportException(&try_catch);
142 continue;
143 }
144 Handle<String> response = Handle<String>::Cast(response_val);
145
146 // Convert the debugger response into text details and the running state.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000147 Handle<Object> response_details = Shell::DebugMessageDetails(response);
ager@chromium.org32912102009-01-16 10:38:43 +0000148 if (try_catch.HasCaught()) {
149 Shell::ReportException(&try_catch);
150 continue;
151 }
152 String::Utf8Value text_str(response_details->Get(String::New("text")));
153 if (text_str.length() > 0) {
154 printf("%s\n", *text_str);
155 }
156 running =
157 response_details->Get(String::New("running"))->ToBoolean()->Value();
158 }
159}
160
161
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000162void RunRemoteDebugger(int port) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000163 RemoteDebugger debugger(port);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000164 debugger.Run();
165}
166
167
168void RemoteDebugger::Run() {
169 bool ok;
170
171 // Make sure that socket support is initialized.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000172 ok = i::Socket::SetUp();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000173 if (!ok) {
174 printf("Unable to initialize socket support %d\n", i::Socket::LastError());
175 return;
176 }
177
178 // Connect to the debugger agent.
179 conn_ = i::OS::CreateSocket();
180 static const int kPortStrSize = 6;
181 char port_str[kPortStrSize];
182 i::OS::SNPrintF(i::Vector<char>(port_str, kPortStrSize), "%d", port_);
183 ok = conn_->Connect("localhost", port_str);
184 if (!ok) {
185 printf("Unable to connect to debug agent %d\n", i::Socket::LastError());
186 return;
187 }
188
189 // Start the receiver thread.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000190 ReceiverThread receiver(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000191 receiver.Start();
192
193 // Start the keyboard thread.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000194 KeyboardThread keyboard(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000195 keyboard.Start();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000196 PrintPrompt();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000197
198 // Process events received from debugged VM and from the keyboard.
199 bool terminate = false;
200 while (!terminate) {
201 event_available_->Wait();
202 RemoteDebuggerEvent* event = GetEvent();
203 switch (event->type()) {
204 case RemoteDebuggerEvent::kMessage:
205 HandleMessageReceived(event->data());
206 break;
207 case RemoteDebuggerEvent::kKeyboard:
208 HandleKeyboardCommand(event->data());
209 break;
210 case RemoteDebuggerEvent::kDisconnect:
211 terminate = true;
212 break;
213
214 default:
215 UNREACHABLE();
216 }
217 delete event;
218 }
219
220 // Wait for the receiver thread to end.
221 receiver.Join();
222}
223
224
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000225void RemoteDebugger::MessageReceived(i::SmartArrayPointer<char> message) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000226 RemoteDebuggerEvent* event =
227 new RemoteDebuggerEvent(RemoteDebuggerEvent::kMessage, message);
228 AddEvent(event);
229}
230
231
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000232void RemoteDebugger::KeyboardCommand(i::SmartArrayPointer<char> command) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000233 RemoteDebuggerEvent* event =
234 new RemoteDebuggerEvent(RemoteDebuggerEvent::kKeyboard, command);
235 AddEvent(event);
236}
237
238
239void RemoteDebugger::ConnectionClosed() {
240 RemoteDebuggerEvent* event =
241 new RemoteDebuggerEvent(RemoteDebuggerEvent::kDisconnect,
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000242 i::SmartArrayPointer<char>());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000243 AddEvent(event);
244}
245
246
247void RemoteDebugger::AddEvent(RemoteDebuggerEvent* event) {
248 i::ScopedLock lock(event_access_);
249 if (head_ == NULL) {
250 ASSERT(tail_ == NULL);
251 head_ = event;
252 tail_ = event;
253 } else {
254 ASSERT(tail_ != NULL);
255 tail_->set_next(event);
256 tail_ = event;
257 }
258 event_available_->Signal();
259}
260
261
262RemoteDebuggerEvent* RemoteDebugger::GetEvent() {
263 i::ScopedLock lock(event_access_);
264 ASSERT(head_ != NULL);
265 RemoteDebuggerEvent* result = head_;
266 head_ = head_->next();
267 if (head_ == NULL) {
268 ASSERT(tail_ == result);
269 tail_ = NULL;
270 }
271 return result;
272}
273
274
275void RemoteDebugger::HandleMessageReceived(char* message) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000276 Locker lock(v8::Isolate::GetCurrent());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000277 HandleScope scope;
278
279 // Print the event details.
280 TryCatch try_catch;
281 Handle<Object> details =
282 Shell::DebugMessageDetails(Handle<String>::Cast(String::New(message)));
283 if (try_catch.HasCaught()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000284 Shell::ReportException(&try_catch);
285 PrintPrompt();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000286 return;
287 }
288 String::Utf8Value str(details->Get(String::New("text")));
289 if (str.length() == 0) {
290 // Empty string is used to signal not to process this event.
291 return;
292 }
293 if (*str != NULL) {
294 printf("%s\n", *str);
295 } else {
296 printf("???\n");
297 }
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000298
299 bool is_running = details->Get(String::New("running"))->ToBoolean()->Value();
300 PrintPrompt(is_running);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000301}
302
303
304void RemoteDebugger::HandleKeyboardCommand(char* command) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000305 Locker lock(v8::Isolate::GetCurrent());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000306 HandleScope scope;
307
308 // Convert the debugger command to a JSON debugger request.
309 TryCatch try_catch;
310 Handle<Value> request =
311 Shell::DebugCommandToJSONRequest(String::New(command));
312 if (try_catch.HasCaught()) {
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +0000313 Shell::ReportException(&try_catch);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000314 PrintPrompt();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000315 return;
316 }
317
318 // If undefined is returned the command was handled internally and there is
319 // no JSON to send.
320 if (request->IsUndefined()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000321 PrintPrompt();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000322 return;
323 }
324
325 // Send the JSON debugger request.
326 i::DebuggerAgentUtil::SendMessage(conn_, Handle<String>::Cast(request));
327}
328
329
330void ReceiverThread::Run() {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000331 // Receive the connect message (with empty body).
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000332 i::SmartArrayPointer<char> message =
333 i::DebuggerAgentUtil::ReceiveMessage(remote_debugger_->conn());
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000334 ASSERT(*message == NULL);
335
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000336 while (true) {
337 // Receive a message.
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000338 i::SmartArrayPointer<char> message =
339 i::DebuggerAgentUtil::ReceiveMessage(remote_debugger_->conn());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000340 if (*message == NULL) {
341 remote_debugger_->ConnectionClosed();
342 return;
343 }
344
345 // Pass the message to the main thread.
346 remote_debugger_->MessageReceived(message);
347 }
348}
349
350
351void KeyboardThread::Run() {
352 static const int kBufferSize = 256;
353 while (true) {
354 // read keyboard input.
355 char command[kBufferSize];
356 char* str = fgets(command, kBufferSize, stdin);
357 if (str == NULL) {
358 break;
359 }
360
361 // Pass the keyboard command to the main thread.
362 remote_debugger_->KeyboardCommand(
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000363 i::SmartArrayPointer<char>(i::StrDup(command)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000364 }
365}
366
367
ager@chromium.org32912102009-01-16 10:38:43 +0000368} // namespace v8
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000369
370#endif // ENABLE_DEBUGGER_SUPPORT