2012-01-19 16:52:16 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-12-18 10:06:49 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2011-09-29 08:55:36 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2008-12-18 10:06:49 +00:00
|
|
|
|
|
|
|
#include "d8.h"
|
|
|
|
#include "d8-debug.h"
|
2009-03-04 09:42:51 +00:00
|
|
|
#include "debug-agent.h"
|
2008-12-18 10:06:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
|
2011-01-03 07:56:30 +00:00
|
|
|
static bool was_running = true;
|
|
|
|
|
|
|
|
void PrintPrompt(bool is_running) {
|
|
|
|
const char* prompt = is_running? "> " : "dbg> ";
|
|
|
|
was_running = is_running;
|
|
|
|
printf("%s", prompt);
|
2010-04-16 12:19:47 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2008-12-18 10:06:49 +00:00
|
|
|
|
2011-01-03 07:56:30 +00:00
|
|
|
void PrintPrompt() {
|
|
|
|
PrintPrompt(was_running);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-19 09:38:18 +00:00
|
|
|
void HandleDebugEvent(const Debug::EventDetails& event_details) {
|
2013-03-15 12:06:53 +00:00
|
|
|
// TODO(svenpanne) There should be a way to retrieve this in the callback.
|
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
|
|
|
HandleScope scope(isolate);
|
2008-12-18 10:06:49 +00:00
|
|
|
|
2013-07-19 09:38:18 +00:00
|
|
|
DebugEvent event = event_details.GetEvent();
|
2009-02-05 10:10:45 +00:00
|
|
|
// Check for handled event.
|
|
|
|
if (event != Break && event != Exception && event != AfterCompile) {
|
|
|
|
return;
|
|
|
|
}
|
2008-12-18 10:06:49 +00:00
|
|
|
|
|
|
|
TryCatch try_catch;
|
|
|
|
|
2009-02-26 11:55:35 +00:00
|
|
|
// Get the toJSONProtocol function on the event and get the JSON format.
|
|
|
|
Local<String> to_json_fun_name = String::New("toJSONProtocol");
|
2013-07-19 09:38:18 +00:00
|
|
|
Handle<Object> event_data = event_details.GetEventData();
|
2009-02-26 11:55:35 +00:00
|
|
|
Local<Function> to_json_fun =
|
2013-06-13 09:27:09 +00:00
|
|
|
Local<Function>::Cast(event_data->Get(to_json_fun_name));
|
2009-02-26 11:55:35 +00:00
|
|
|
Local<Value> event_json = to_json_fun->Call(event_data, 0, NULL);
|
|
|
|
if (try_catch.HasCaught()) {
|
2013-03-15 12:06:53 +00:00
|
|
|
Shell::ReportException(isolate, &try_catch);
|
2009-02-26 11:55:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-18 10:06:49 +00:00
|
|
|
// Print the event details.
|
2009-03-02 19:02:27 +00:00
|
|
|
Handle<Object> details =
|
2013-05-02 20:18:42 +00:00
|
|
|
Shell::DebugMessageDetails(isolate, Handle<String>::Cast(event_json));
|
2009-03-02 19:02:27 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
2013-03-15 12:06:53 +00:00
|
|
|
Shell::ReportException(isolate, &try_catch);
|
2009-03-02 19:02:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
String::Utf8Value str(details->Get(String::New("text")));
|
|
|
|
if (str.length() == 0) {
|
2009-02-05 10:10:45 +00:00
|
|
|
// Empty string is used to signal not to process this event.
|
|
|
|
return;
|
|
|
|
}
|
2008-12-18 10:06:49 +00:00
|
|
|
printf("%s\n", *str);
|
|
|
|
|
|
|
|
// Get the debug command processor.
|
|
|
|
Local<String> fun_name = String::New("debugCommandProcessor");
|
2013-07-19 09:38:18 +00:00
|
|
|
Handle<Object> exec_state = event_details.GetExecutionState();
|
2013-06-13 09:27:09 +00:00
|
|
|
Local<Function> fun = Local<Function>::Cast(exec_state->Get(fun_name));
|
2008-12-18 10:06:49 +00:00
|
|
|
Local<Object> cmd_processor =
|
2013-06-13 09:27:09 +00:00
|
|
|
Local<Object>::Cast(fun->Call(exec_state, 0, NULL));
|
2008-12-18 10:06:49 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
2013-03-15 12:06:53 +00:00
|
|
|
Shell::ReportException(isolate, &try_catch);
|
2008-12-18 10:06:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int kBufferSize = 256;
|
|
|
|
bool running = false;
|
|
|
|
while (!running) {
|
|
|
|
char command[kBufferSize];
|
2011-01-03 07:56:30 +00:00
|
|
|
PrintPrompt(running);
|
2008-12-18 10:06:49 +00:00
|
|
|
char* str = fgets(command, kBufferSize, stdin);
|
|
|
|
if (str == NULL) break;
|
|
|
|
|
|
|
|
// Ignore empty commands.
|
|
|
|
if (strlen(command) == 0) continue;
|
|
|
|
|
|
|
|
TryCatch try_catch;
|
|
|
|
|
|
|
|
// Convert the debugger command to a JSON debugger request.
|
|
|
|
Handle<Value> request =
|
2013-05-02 20:18:42 +00:00
|
|
|
Shell::DebugCommandToJSONRequest(isolate, String::New(command));
|
2008-12-18 10:06:49 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
2013-03-15 12:06:53 +00:00
|
|
|
Shell::ReportException(isolate, &try_catch);
|
2008-12-18 10:06:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If undefined is returned the command was handled internally and there is
|
|
|
|
// no JSON to send.
|
|
|
|
if (request->IsUndefined()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> fun_name;
|
|
|
|
Handle<Function> fun;
|
|
|
|
// All the functions used below take one argument.
|
|
|
|
static const int kArgc = 1;
|
|
|
|
Handle<Value> args[kArgc];
|
|
|
|
|
|
|
|
// Invoke the JavaScript to convert the debug command line to a JSON
|
|
|
|
// request, invoke the JSON request and convert the JSON respose to a text
|
|
|
|
// representation.
|
|
|
|
fun_name = String::New("processDebugRequest");
|
|
|
|
fun = Handle<Function>::Cast(cmd_processor->Get(fun_name));
|
|
|
|
args[0] = request;
|
|
|
|
Handle<Value> response_val = fun->Call(cmd_processor, kArgc, args);
|
|
|
|
if (try_catch.HasCaught()) {
|
2013-03-15 12:06:53 +00:00
|
|
|
Shell::ReportException(isolate, &try_catch);
|
2008-12-18 10:06:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Handle<String> response = Handle<String>::Cast(response_val);
|
|
|
|
|
|
|
|
// Convert the debugger response into text details and the running state.
|
2013-05-02 20:18:42 +00:00
|
|
|
Handle<Object> response_details =
|
|
|
|
Shell::DebugMessageDetails(isolate, response);
|
2008-12-18 10:06:49 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
2013-03-15 12:06:53 +00:00
|
|
|
Shell::ReportException(isolate, &try_catch);
|
2008-12-18 10:06:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
String::Utf8Value text_str(response_details->Get(String::New("text")));
|
|
|
|
if (text_str.length() > 0) {
|
|
|
|
printf("%s\n", *text_str);
|
|
|
|
}
|
|
|
|
running =
|
|
|
|
response_details->Get(String::New("running"))->ToBoolean()->Value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-15 12:06:53 +00:00
|
|
|
void RunRemoteDebugger(Isolate* isolate, int port) {
|
|
|
|
RemoteDebugger debugger(isolate, port);
|
2009-03-04 09:42:51 +00:00
|
|
|
debugger.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteDebugger::Run() {
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
// Connect to the debugger agent.
|
2013-09-04 10:41:51 +00:00
|
|
|
conn_ = new i::Socket;
|
2009-03-04 09:42:51 +00:00
|
|
|
static const int kPortStrSize = 6;
|
|
|
|
char port_str[kPortStrSize];
|
|
|
|
i::OS::SNPrintF(i::Vector<char>(port_str, kPortStrSize), "%d", port_);
|
|
|
|
ok = conn_->Connect("localhost", port_str);
|
|
|
|
if (!ok) {
|
2013-09-04 10:41:51 +00:00
|
|
|
printf("Unable to connect to debug agent %d\n", i::Socket::GetLastError());
|
2009-03-04 09:42:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the receiver thread.
|
2011-06-10 12:41:35 +00:00
|
|
|
ReceiverThread receiver(this);
|
2009-03-04 09:42:51 +00:00
|
|
|
receiver.Start();
|
|
|
|
|
|
|
|
// Start the keyboard thread.
|
2011-06-10 12:41:35 +00:00
|
|
|
KeyboardThread keyboard(this);
|
2009-03-04 09:42:51 +00:00
|
|
|
keyboard.Start();
|
2010-04-16 12:19:47 +00:00
|
|
|
PrintPrompt();
|
2009-03-04 09:42:51 +00:00
|
|
|
|
|
|
|
// Process events received from debugged VM and from the keyboard.
|
|
|
|
bool terminate = false;
|
|
|
|
while (!terminate) {
|
2013-09-02 12:26:06 +00:00
|
|
|
event_available_.Wait();
|
2009-03-04 09:42:51 +00:00
|
|
|
RemoteDebuggerEvent* event = GetEvent();
|
|
|
|
switch (event->type()) {
|
|
|
|
case RemoteDebuggerEvent::kMessage:
|
|
|
|
HandleMessageReceived(event->data());
|
|
|
|
break;
|
|
|
|
case RemoteDebuggerEvent::kKeyboard:
|
|
|
|
HandleKeyboardCommand(event->data());
|
|
|
|
break;
|
|
|
|
case RemoteDebuggerEvent::kDisconnect:
|
|
|
|
terminate = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
delete event;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the receiver thread to end.
|
|
|
|
receiver.Join();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-09 22:39:47 +00:00
|
|
|
void RemoteDebugger::MessageReceived(i::SmartArrayPointer<char> message) {
|
2009-03-04 09:42:51 +00:00
|
|
|
RemoteDebuggerEvent* event =
|
|
|
|
new RemoteDebuggerEvent(RemoteDebuggerEvent::kMessage, message);
|
|
|
|
AddEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-09 22:39:47 +00:00
|
|
|
void RemoteDebugger::KeyboardCommand(i::SmartArrayPointer<char> command) {
|
2009-03-04 09:42:51 +00:00
|
|
|
RemoteDebuggerEvent* event =
|
|
|
|
new RemoteDebuggerEvent(RemoteDebuggerEvent::kKeyboard, command);
|
|
|
|
AddEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteDebugger::ConnectionClosed() {
|
|
|
|
RemoteDebuggerEvent* event =
|
|
|
|
new RemoteDebuggerEvent(RemoteDebuggerEvent::kDisconnect,
|
2011-09-09 22:39:47 +00:00
|
|
|
i::SmartArrayPointer<char>());
|
2009-03-04 09:42:51 +00:00
|
|
|
AddEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteDebugger::AddEvent(RemoteDebuggerEvent* event) {
|
2013-08-29 09:58:30 +00:00
|
|
|
i::LockGuard<i::Mutex> lock_guard(&event_access_);
|
2009-03-04 09:42:51 +00:00
|
|
|
if (head_ == NULL) {
|
|
|
|
ASSERT(tail_ == NULL);
|
|
|
|
head_ = event;
|
|
|
|
tail_ = event;
|
|
|
|
} else {
|
|
|
|
ASSERT(tail_ != NULL);
|
|
|
|
tail_->set_next(event);
|
|
|
|
tail_ = event;
|
|
|
|
}
|
2013-09-02 12:26:06 +00:00
|
|
|
event_available_.Signal();
|
2009-03-04 09:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RemoteDebuggerEvent* RemoteDebugger::GetEvent() {
|
2013-08-29 09:58:30 +00:00
|
|
|
i::LockGuard<i::Mutex> lock_guard(&event_access_);
|
2009-03-04 09:42:51 +00:00
|
|
|
ASSERT(head_ != NULL);
|
|
|
|
RemoteDebuggerEvent* result = head_;
|
|
|
|
head_ = head_->next();
|
|
|
|
if (head_ == NULL) {
|
|
|
|
ASSERT(tail_ == result);
|
|
|
|
tail_ = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteDebugger::HandleMessageReceived(char* message) {
|
2013-03-15 12:06:53 +00:00
|
|
|
Locker lock(isolate_);
|
|
|
|
HandleScope scope(isolate_);
|
2009-03-04 09:42:51 +00:00
|
|
|
|
|
|
|
// Print the event details.
|
|
|
|
TryCatch try_catch;
|
|
|
|
Handle<Object> details =
|
2013-05-02 20:18:42 +00:00
|
|
|
Shell::DebugMessageDetails(isolate_,
|
|
|
|
Handle<String>::Cast(String::New(message)));
|
2009-03-04 09:42:51 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
2013-03-15 12:06:53 +00:00
|
|
|
Shell::ReportException(isolate_, &try_catch);
|
2010-04-16 12:19:47 +00:00
|
|
|
PrintPrompt();
|
2009-03-04 09:42:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
String::Utf8Value str(details->Get(String::New("text")));
|
|
|
|
if (str.length() == 0) {
|
|
|
|
// Empty string is used to signal not to process this event.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (*str != NULL) {
|
|
|
|
printf("%s\n", *str);
|
|
|
|
} else {
|
|
|
|
printf("???\n");
|
|
|
|
}
|
2011-01-03 07:56:30 +00:00
|
|
|
|
|
|
|
bool is_running = details->Get(String::New("running"))->ToBoolean()->Value();
|
|
|
|
PrintPrompt(is_running);
|
2009-03-04 09:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteDebugger::HandleKeyboardCommand(char* command) {
|
2013-03-15 12:06:53 +00:00
|
|
|
Locker lock(isolate_);
|
|
|
|
HandleScope scope(isolate_);
|
2009-03-04 09:42:51 +00:00
|
|
|
|
|
|
|
// Convert the debugger command to a JSON debugger request.
|
|
|
|
TryCatch try_catch;
|
|
|
|
Handle<Value> request =
|
2013-05-02 20:18:42 +00:00
|
|
|
Shell::DebugCommandToJSONRequest(isolate_, String::New(command));
|
2009-03-04 09:42:51 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
2013-03-15 12:06:53 +00:00
|
|
|
Shell::ReportException(isolate_, &try_catch);
|
2010-04-16 12:19:47 +00:00
|
|
|
PrintPrompt();
|
2009-03-04 09:42:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If undefined is returned the command was handled internally and there is
|
|
|
|
// no JSON to send.
|
|
|
|
if (request->IsUndefined()) {
|
2010-04-16 12:19:47 +00:00
|
|
|
PrintPrompt();
|
2009-03-04 09:42:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the JSON debugger request.
|
|
|
|
i::DebuggerAgentUtil::SendMessage(conn_, Handle<String>::Cast(request));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ReceiverThread::Run() {
|
2009-03-23 22:23:39 +00:00
|
|
|
// Receive the connect message (with empty body).
|
2011-09-09 22:39:47 +00:00
|
|
|
i::SmartArrayPointer<char> message =
|
|
|
|
i::DebuggerAgentUtil::ReceiveMessage(remote_debugger_->conn());
|
2009-03-23 22:23:39 +00:00
|
|
|
ASSERT(*message == NULL);
|
|
|
|
|
2009-03-04 09:42:51 +00:00
|
|
|
while (true) {
|
|
|
|
// Receive a message.
|
2011-09-09 22:39:47 +00:00
|
|
|
i::SmartArrayPointer<char> message =
|
|
|
|
i::DebuggerAgentUtil::ReceiveMessage(remote_debugger_->conn());
|
2009-03-04 09:42:51 +00:00
|
|
|
if (*message == NULL) {
|
|
|
|
remote_debugger_->ConnectionClosed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the message to the main thread.
|
|
|
|
remote_debugger_->MessageReceived(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void KeyboardThread::Run() {
|
|
|
|
static const int kBufferSize = 256;
|
|
|
|
while (true) {
|
|
|
|
// read keyboard input.
|
|
|
|
char command[kBufferSize];
|
|
|
|
char* str = fgets(command, kBufferSize, stdin);
|
|
|
|
if (str == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the keyboard command to the main thread.
|
|
|
|
remote_debugger_->KeyboardCommand(
|
2011-09-09 22:39:47 +00:00
|
|
|
i::SmartArrayPointer<char>(i::StrDup(command)));
|
2009-03-04 09:42:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-18 10:06:49 +00:00
|
|
|
} // namespace v8
|
2011-09-29 08:55:36 +00:00
|
|
|
|
|
|
|
#endif // ENABLE_DEBUGGER_SUPPORT
|