Commit e0c7b0f3 authored by sgjesse@chromium.org's avatar sgjesse@chromium.org

Added command line debugger to D8 shell.

  break location [condition]
  clear <breakpoint #>
  backtrace [from frame #] [to frame #]]
  frame <frame #>
  step [in | next | out| min [step count]]
  print <expression>
  source [from line [num lines]]
  scripts
  continue
  help

It is enabled through the option --debugger which is on by default.
Review URL: http://codereview.chromium.org/14509

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@996 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent e33f70db
......@@ -73,7 +73,7 @@ SOURCES = {
D8_FILES = {
'all': [
'd8.cc'
'd8.cc', 'd8-debug.cc'
],
'console:readline': [
'd8-readline.cc'
......
// Copyright 2008 the V8 project authors. All rights reserved.
// 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.
#include "d8.h"
#include "d8-debug.h"
namespace v8 {
void HandleDebugEvent(DebugEvent event,
Handle<Object> exec_state,
Handle<Object> event_data,
Handle<Value> data) {
HandleScope scope;
// Currently only handles break and exception events.
if (event != Break && event != Exception) return;
TryCatch try_catch;
// Print the event details.
Handle<String> details = Shell::DebugEventToText(event_data);
String::Utf8Value str(details);
printf("%s\n", *str);
// Get the debug command processor.
Local<String> fun_name = String::New("debugCommandProcessor");
Local<Function> fun = Function::Cast(*exec_state->Get(fun_name));
Local<Object> cmd_processor =
Object::Cast(*fun->Call(exec_state, 0, NULL));
if (try_catch.HasCaught()) {
Shell::ReportException(&try_catch);
return;
}
static const int kBufferSize = 256;
bool running = false;
while (!running) {
char command[kBufferSize];
printf("dbg> ");
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 =
Shell::DebugCommandToJSONRequest(String::New(command));
if (try_catch.HasCaught()) {
Shell::ReportException(&try_catch);
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()) {
Shell::ReportException(&try_catch);
continue;
}
Handle<String> response = Handle<String>::Cast(response_val);
// Convert the debugger response into text details and the running state.
Handle<Object> response_details = Shell::DebugResponseDetails(response);
if (try_catch.HasCaught()) {
Shell::ReportException(&try_catch);
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();
}
}
} // namespace v8
// Copyright 2008 the V8 project authors. All rights reserved.
// 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.
#ifndef V8_D8_DEBUG_H_
#define V8_D8_DEBUG_H_
#include "d8.h"
#include "debug.h"
namespace v8 {
void HandleDebugEvent(DebugEvent event,
Handle<Object> exec_state,
Handle<Object> event_data,
Handle<Value> data);
} // namespace v8
#endif // V8_D8_DEBUG_H_
......@@ -29,6 +29,7 @@
#include <stdlib.h>
#include "d8.h"
#include "d8-debug.h"
#include "debug.h"
#include "api.h"
#include "natives.h"
......@@ -98,6 +99,10 @@ bool Shell::ExecuteString(Handle<String> source,
bool report_exceptions) {
HandleScope handle_scope;
TryCatch try_catch;
if (i::FLAG_debugger) {
// When debugging make exceptions appear to be uncaught.
try_catch.SetVerbose(true);
}
Handle<Script> script = Script::Compile(source, name);
if (script.IsEmpty()) {
// Print errors that happened during compilation.
......@@ -212,6 +217,46 @@ Handle<Array> Shell::GetCompletions(Handle<String> text, Handle<String> full) {
}
Handle<String> Shell::DebugEventToText(Handle<Object> event) {
HandleScope handle_scope;
Context::Scope context_scope(utility_context_);
Handle<Object> global = utility_context_->Global();
Handle<Value> fun = global->Get(String::New("DebugEventToText"));
TryCatch try_catch;
try_catch.SetVerbose(true);
static const int kArgc = 1;
Handle<Value> argv[kArgc] = { event };
Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
if (try_catch.HasCaught()) {
return handle_scope.Close(try_catch.Exception()->ToString());
} else {
return handle_scope.Close(Handle<String>::Cast(val));
}
}
Handle<Value> Shell::DebugCommandToJSONRequest(Handle<String> command) {
Context::Scope context_scope(utility_context_);
Handle<Object> global = utility_context_->Global();
Handle<Value> fun = global->Get(String::New("DebugCommandToJSONRequest"));
static const int kArgc = 1;
Handle<Value> argv[kArgc] = { command };
Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
return val;
}
Handle<Object> Shell::DebugResponseDetails(Handle<String> response) {
Context::Scope context_scope(utility_context_);
Handle<Object> global = utility_context_->Global();
Handle<Value> fun = global->Get(String::New("DebugResponseDetails"));
static const int kArgc = 1;
Handle<Value> argv[kArgc] = { response };
Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
return Handle<Object>::Cast(val);
}
int32_t* Counter::Bind(const char* name) {
int i;
for (i = 0; i < kMaxNameSize - 1 && name[i]; i++)
......@@ -415,6 +460,8 @@ int Shell::Main(int argc, char* argv[]) {
return 1;
}
}
if (i::FLAG_debugger)
v8::Debug::AddDebugEventListener(HandleDebugEvent);
if (run_shell)
RunShell();
OnExit();
......
......@@ -88,12 +88,17 @@ class Shell: public i::AllStatic {
static int Main(int argc, char* argv[]);
static Handle<Array> GetCompletions(Handle<String> text,
Handle<String> full);
static Handle<String> DebugEventToText(Handle<Object> event);
static Handle<Value> DebugCommandToJSONRequest(Handle<String> command);
static Handle<Object> DebugResponseDetails(Handle<String> response);
static Handle<Value> Print(const Arguments& args);
static Handle<Value> Quit(const Arguments& args);
static Handle<Value> Version(const Arguments& args);
static Handle<Value> Load(const Arguments& args);
static Handle<Context> utility_context() { return utility_context_; }
static const char* kHistoryFileName;
static const char* kPrompt;
private:
......
This diff is collapsed.
......@@ -1329,6 +1329,9 @@ DebugCommandProcessor.prototype.clearBreakPointRequest_ = function(request, resp
// Clear break point.
Debug.clearBreakPoint(break_point);
// Add the cleared break point number to the response.
response.body = { breakpoint: break_point }
}
......@@ -1388,6 +1391,11 @@ DebugCommandProcessor.prototype.backtracec = function(cmd, args) {
DebugCommandProcessor.prototype.frameRequest_ = function(request, response) {
// No frames no source.
if (this.exec_state_.frameCount() == 0) {
return response.failed('No frames');
}
// With no arguments just keep the selected frame.
if (request.arguments && request.arguments.number >= 0) {
this.exec_state_.setSelectedFrame(request.arguments.number);
......@@ -1453,6 +1461,11 @@ DebugCommandProcessor.prototype.evaluateRequest_ = function(request, response) {
DebugCommandProcessor.prototype.sourceRequest_ = function(request, response) {
// No frames no source.
if (this.exec_state_.frameCount() == 0) {
return response.failed('No source');
}
var from_line;
var to_line;
var frame = this.exec_state_.frame();
......
......@@ -226,6 +226,7 @@ DEFINE_string(testing_serialization_file, "/tmp/serdes",
DEFINE_bool(help, false, "Print usage message, including flags, on console")
DEFINE_bool(dump_counters, false, "Dump counters on exit")
DEFINE_bool(debugger, true, "Enable JavaScript debugger")
DEFINE_string(map_counters, false, "Map counters to a file")
DEFINE_args(js_arguments, JSArguments(),
"Pass all remaining arguments to the script. Alias for \"--\".")
......
......@@ -147,6 +147,14 @@
RelativePath="..\..\src\d8.h"
>
</File>
<File
RelativePath="..\..\src\d8-debug.cc"
>
</File>
<File
RelativePath="..\..\src\d8-debug.h"
>
</File>
<File
RelativePath="..\..\src\d8.js"
>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment