blob: ff802e3ddcdb52a0e2257d895d1bd3fa84e67aa9 [file] [log] [blame]
// Copyright 2012 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.
#ifdef ENABLE_DEBUGGER_SUPPORT
#include <stdlib.h>
#include "v8.h"
#include "api.h"
#include "cctest.h"
#include "compilation-cache.h"
#include "debug.h"
#include "deoptimizer.h"
#include "frames.h"
#include "platform.h"
#include "platform/condition-variable.h"
#include "platform/socket.h"
#include "stub-cache.h"
#include "utils.h"
using ::v8::internal::Mutex;
using ::v8::internal::LockGuard;
using ::v8::internal::ConditionVariable;
using ::v8::internal::Semaphore;
using ::v8::internal::EmbeddedVector;
using ::v8::internal::Object;
using ::v8::internal::OS;
using ::v8::internal::Handle;
using ::v8::internal::Heap;
using ::v8::internal::JSGlobalProxy;
using ::v8::internal::Code;
using ::v8::internal::Debug;
using ::v8::internal::Debugger;
using ::v8::internal::CommandMessage;
using ::v8::internal::CommandMessageQueue;
using ::v8::internal::StackFrame;
using ::v8::internal::StepAction;
using ::v8::internal::StepIn; // From StepAction enum
using ::v8::internal::StepNext; // From StepAction enum
using ::v8::internal::StepOut; // From StepAction enum
using ::v8::internal::Vector;
using ::v8::internal::StrLength;
// Size of temp buffer for formatting small strings.
#define SMALL_STRING_BUFFER_SIZE 80
// --- A d d i t i o n a l C h e c k H e l p e r s
// Helper function used by the CHECK_EQ function when given Address
// arguments. Should not be called directly.
static inline void CheckEqualsHelper(const char* file, int line,
const char* expected_source,
::v8::internal::Address expected,
const char* value_source,
::v8::internal::Address value) {
if (expected != value) {
V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# "
"Expected: %i\n# Found: %i",
expected_source, value_source, expected, value);
}
}
// Helper function used by the CHECK_NE function when given Address
// arguments. Should not be called directly.
static inline void CheckNonEqualsHelper(const char* file, int line,
const char* unexpected_source,
::v8::internal::Address unexpected,
const char* value_source,
::v8::internal::Address value) {
if (unexpected == value) {
V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i",
unexpected_source, value_source, value);
}
}
// Helper function used by the CHECK function when given code
// arguments. Should not be called directly.
static inline void CheckEqualsHelper(const char* file, int line,
const char* expected_source,
const Code* expected,
const char* value_source,
const Code* value) {
if (expected != value) {
V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# "
"Expected: %p\n# Found: %p",
expected_source, value_source, expected, value);
}
}
static inline void CheckNonEqualsHelper(const char* file, int line,
const char* expected_source,
const Code* expected,
const char* value_source,
const Code* value) {
if (expected == value) {
V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p",
expected_source, value_source, value);
}
}
// --- H e l p e r C l a s s e s
// Helper class for creating a V8 enviromnent for running tests
class DebugLocalContext {
public:
inline DebugLocalContext(
v8::ExtensionConfiguration* extensions = 0,
v8::Handle<v8::ObjectTemplate> global_template =
v8::Handle<v8::ObjectTemplate>(),
v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>())
: scope_(CcTest::isolate()),
context_(
v8::Context::New(CcTest::isolate(),
extensions,
global_template,
global_object)) {
context_->Enter();
}
inline ~DebugLocalContext() {
context_->Exit();
}
inline v8::Local<v8::Context> context() { return context_; }
inline v8::Context* operator->() { return *context_; }
inline v8::Context* operator*() { return *context_; }
inline v8::Isolate* GetIsolate() { return context_->GetIsolate(); }
inline bool IsReady() { return !context_.IsEmpty(); }
void ExposeDebug() {
v8::internal::Isolate* isolate =
reinterpret_cast<v8::internal::Isolate*>(context_->GetIsolate());
v8::internal::Factory* factory = isolate->factory();
v8::internal::Debug* debug = isolate->debug();
// Expose the debug context global object in the global object for testing.
debug->Load();
debug->debug_context()->set_security_token(
v8::Utils::OpenHandle(*context_)->security_token());
Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast(
v8::Utils::OpenHandle(*context_->Global())));
Handle<v8::internal::String> debug_string =
factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("debug"));
v8::internal::Runtime::SetObjectProperty(isolate, global, debug_string,
Handle<Object>(debug->debug_context()->global_proxy(), isolate),
DONT_ENUM,
::v8::internal::kNonStrictMode);
}
private:
v8::HandleScope scope_;
v8::Local<v8::Context> context_;
};
// --- H e l p e r F u n c t i o n s
// Compile and run the supplied source and return the fequested function.
static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env,
const char* source,
const char* function_name) {
v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), source))
->Run();
return v8::Local<v8::Function>::Cast((*env)->Global()->Get(
v8::String::NewFromUtf8(env->GetIsolate(), function_name)));
}
// Compile and run the supplied source and return the requested function.
static v8::Local<v8::Function> CompileFunction(v8::Isolate* isolate,
const char* source,
const char* function_name) {
v8::Script::Compile(v8::String::NewFromUtf8(isolate, source))->Run();
v8::Local<v8::Object> global =
CcTest::isolate()->GetCurrentContext()->Global();
return v8::Local<v8::Function>::Cast(
global->Get(v8::String::NewFromUtf8(isolate, function_name)));
}
// Is there any debug info for the function?
static bool HasDebugInfo(v8::Handle<v8::Function> fun) {
Handle<v8::internal::JSFunction> f = v8::Utils::OpenHandle(*fun);
Handle<v8::internal::SharedFunctionInfo> shared(f->shared());
return Debug::HasDebugInfo(shared);
}
// Set a break point in a function and return the associated break point
// number.
static int SetBreakPoint(Handle<v8::internal::JSFunction> fun, int position) {
static int break_point = 0;
v8::internal::Isolate* isolate = fun->GetIsolate();
v8::internal::Debug* debug = isolate->debug();
debug->SetBreakPoint(
fun,
Handle<Object>(v8::internal::Smi::FromInt(++break_point), isolate),
&position);
return break_point;
}
// Set a break point in a function and return the associated break point
// number.
static int SetBreakPoint(v8::Handle<v8::Function> fun, int position) {
return SetBreakPoint(v8::Utils::OpenHandle(*fun), position);
}
// Set a break point in a function using the Debug object and return the
// associated break point number.
static int SetBreakPointFromJS(v8::Isolate* isolate,
const char* function_name,
int line, int position) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
OS::SNPrintF(buffer,
"debug.Debug.setBreakPoint(%s,%d,%d)",
function_name, line, position);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
v8::Handle<v8::String> str = v8::String::NewFromUtf8(isolate, buffer.start());
return v8::Script::Compile(str)->Run()->Int32Value();
}
// Set a break point in a script identified by id using the global Debug object.
static int SetScriptBreakPointByIdFromJS(v8::Isolate* isolate, int script_id,
int line, int column) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
if (column >= 0) {
// Column specified set script break point on precise location.
OS::SNPrintF(buffer,
"debug.Debug.setScriptBreakPointById(%d,%d,%d)",
script_id, line, column);
} else {
// Column not specified set script break point on line.
OS::SNPrintF(buffer,
"debug.Debug.setScriptBreakPointById(%d,%d)",
script_id, line);
}
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
{
v8::TryCatch try_catch;
v8::Handle<v8::String> str =
v8::String::NewFromUtf8(isolate, buffer.start());
v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run();
CHECK(!try_catch.HasCaught());
return value->Int32Value();
}
}
// Set a break point in a script identified by name using the global Debug
// object.
static int SetScriptBreakPointByNameFromJS(v8::Isolate* isolate,
const char* script_name, int line,
int column) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
if (column >= 0) {
// Column specified set script break point on precise location.
OS::SNPrintF(buffer,
"debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)",
script_name, line, column);
} else {
// Column not specified set script break point on line.
OS::SNPrintF(buffer,
"debug.Debug.setScriptBreakPointByName(\"%s\",%d)",
script_name, line);
}
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
{
v8::TryCatch try_catch;
v8::Handle<v8::String> str =
v8::String::NewFromUtf8(isolate, buffer.start());
v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run();
CHECK(!try_catch.HasCaught());
return value->Int32Value();
}
}
// Clear a break point.
static void ClearBreakPoint(int break_point) {
v8::internal::Isolate* isolate = CcTest::i_isolate();
v8::internal::Debug* debug = isolate->debug();
debug->ClearBreakPoint(
Handle<Object>(v8::internal::Smi::FromInt(break_point), isolate));
}
// Clear a break point using the global Debug object.
static void ClearBreakPointFromJS(v8::Isolate* isolate,
int break_point_number) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
OS::SNPrintF(buffer,
"debug.Debug.clearBreakPoint(%d)",
break_point_number);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
v8::Script::Compile(v8::String::NewFromUtf8(isolate, buffer.start()))->Run();
}
static void EnableScriptBreakPointFromJS(v8::Isolate* isolate,
int break_point_number) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
OS::SNPrintF(buffer,
"debug.Debug.enableScriptBreakPoint(%d)",
break_point_number);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
v8::Script::Compile(v8::String::NewFromUtf8(isolate, buffer.start()))->Run();
}
static void DisableScriptBreakPointFromJS(v8::Isolate* isolate,
int break_point_number) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
OS::SNPrintF(buffer,
"debug.Debug.disableScriptBreakPoint(%d)",
break_point_number);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
v8::Script::Compile(v8::String::NewFromUtf8(isolate, buffer.start()))->Run();
}
static void ChangeScriptBreakPointConditionFromJS(v8::Isolate* isolate,
int break_point_number,
const char* condition) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
OS::SNPrintF(buffer,
"debug.Debug.changeScriptBreakPointCondition(%d, \"%s\")",
break_point_number, condition);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
v8::Script::Compile(v8::String::NewFromUtf8(isolate, buffer.start()))->Run();
}
static void ChangeScriptBreakPointIgnoreCountFromJS(v8::Isolate* isolate,
int break_point_number,
int ignoreCount) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
OS::SNPrintF(buffer,
"debug.Debug.changeScriptBreakPointIgnoreCount(%d, %d)",
break_point_number, ignoreCount);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
v8::Script::Compile(v8::String::NewFromUtf8(isolate, buffer.start()))->Run();
}
// Change break on exception.
static void ChangeBreakOnException(bool caught, bool uncaught) {
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
debug->ChangeBreakOnException(v8::internal::BreakException, caught);
debug->ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught);
}
// Change break on exception using the global Debug object.
static void ChangeBreakOnExceptionFromJS(v8::Isolate* isolate, bool caught,
bool uncaught) {
if (caught) {
v8::Script::Compile(
v8::String::NewFromUtf8(isolate, "debug.Debug.setBreakOnException()"))
->Run();
} else {
v8::Script::Compile(
v8::String::NewFromUtf8(isolate, "debug.Debug.clearBreakOnException()"))
->Run();
}
if (uncaught) {
v8::Script::Compile(
v8::String::NewFromUtf8(
isolate, "debug.Debug.setBreakOnUncaughtException()"))->Run();
} else {
v8::Script::Compile(
v8::String::NewFromUtf8(
isolate, "debug.Debug.clearBreakOnUncaughtException()"))->Run();
}
}
// Prepare to step to next break location.
static void PrepareStep(StepAction step_action) {
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
debug->PrepareStep(step_action, 1, StackFrame::NO_ID);
}
// This function is in namespace v8::internal to be friend with class
// v8::internal::Debug.
namespace v8 {
namespace internal {
// Collect the currently debugged functions.
Handle<FixedArray> GetDebuggedFunctions() {
Debug* debug = CcTest::i_isolate()->debug();
v8::internal::DebugInfoListNode* node = debug->debug_info_list_;
// Find the number of debugged functions.
int count = 0;
while (node) {
count++;
node = node->next();
}
// Allocate array for the debugged functions
Handle<FixedArray> debugged_functions =
CcTest::i_isolate()->factory()->NewFixedArray(count);
// Run through the debug info objects and collect all functions.
count = 0;
while (node) {
debugged_functions->set(count++, *node->debug_info());
node = node->next();
}
return debugged_functions;
}
static Handle<Code> ComputeCallDebugBreak(int argc) {
return CcTest::i_isolate()->stub_cache()->ComputeCallDebugBreak(argc,
Code::CALL_IC);
}
// Check that the debugger has been fully unloaded.
void CheckDebuggerUnloaded(bool check_functions) {
// Check that the debugger context is cleared and that there is no debug
// information stored for the debugger.
CHECK(CcTest::i_isolate()->debug()->debug_context().is_null());
CHECK_EQ(NULL, CcTest::i_isolate()->debug()->debug_info_list_);
// Collect garbage to ensure weak handles are cleared.
CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags);
CcTest::heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask);
// Iterate the head and check that there are no debugger related objects left.
HeapIterator iterator(CcTest::heap());
for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
CHECK(!obj->IsDebugInfo());
CHECK(!obj->IsBreakPointInfo());
// If deep check of functions is requested check that no debug break code
// is left in all functions.
if (check_functions) {
if (obj->IsJSFunction()) {
JSFunction* fun = JSFunction::cast(obj);
for (RelocIterator it(fun->shared()->code()); !it.done(); it.next()) {
RelocInfo::Mode rmode = it.rinfo()->rmode();
if (RelocInfo::IsCodeTarget(rmode)) {
CHECK(!Debug::IsDebugBreak(it.rinfo()->target_address()));
} else if (RelocInfo::IsJSReturn(rmode)) {
CHECK(!Debug::IsDebugBreakAtReturn(it.rinfo()));
}
}
}
}
}
}
void ForceUnloadDebugger() {
CcTest::i_isolate()->debugger()->never_unload_debugger_ = false;
CcTest::i_isolate()->debugger()->UnloadDebugger();
}
} } // namespace v8::internal
// Check that the debugger has been fully unloaded.
static void CheckDebuggerUnloaded(bool check_functions = false) {
// Let debugger to unload itself synchronously
v8::Debug::ProcessDebugMessages();
v8::internal::CheckDebuggerUnloaded(check_functions);
}
// Inherit from BreakLocationIterator to get access to protected parts for
// testing.
class TestBreakLocationIterator: public v8::internal::BreakLocationIterator {
public:
explicit TestBreakLocationIterator(Handle<v8::internal::DebugInfo> debug_info)
: BreakLocationIterator(debug_info, v8::internal::SOURCE_BREAK_LOCATIONS) {}
v8::internal::RelocIterator* it() { return reloc_iterator_; }
v8::internal::RelocIterator* it_original() {
return reloc_iterator_original_;
}
};
// Compile a function, set a break point and check that the call at the break
// location in the code is the expected debug_break function.
void CheckDebugBreakFunction(DebugLocalContext* env,
const char* source, const char* name,
int position, v8::internal::RelocInfo::Mode mode,
Code* debug_break) {
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// Create function and set the break point.
Handle<v8::internal::JSFunction> fun = v8::Utils::OpenHandle(
*CompileFunction(env, source, name));
int bp = SetBreakPoint(fun, position);
// Check that the debug break function is as expected.
Handle<v8::internal::SharedFunctionInfo> shared(fun->shared());
CHECK(Debug::HasDebugInfo(shared));
TestBreakLocationIterator it1(Debug::GetDebugInfo(shared));
it1.FindBreakLocationFromPosition(position, v8::internal::STATEMENT_ALIGNED);
v8::internal::RelocInfo::Mode actual_mode = it1.it()->rinfo()->rmode();
if (actual_mode == v8::internal::RelocInfo::CODE_TARGET_WITH_ID) {
actual_mode = v8::internal::RelocInfo::CODE_TARGET;
}
CHECK_EQ(mode, actual_mode);
if (mode != v8::internal::RelocInfo::JS_RETURN) {
CHECK_EQ(debug_break,
Code::GetCodeFromTargetAddress(it1.it()->rinfo()->target_address()));
} else {
CHECK(Debug::IsDebugBreakAtReturn(it1.it()->rinfo()));
}
// Clear the break point and check that the debug break function is no longer
// there
ClearBreakPoint(bp);
CHECK(!debug->HasDebugInfo(shared));
CHECK(debug->EnsureDebugInfo(shared, fun));
TestBreakLocationIterator it2(Debug::GetDebugInfo(shared));
it2.FindBreakLocationFromPosition(position, v8::internal::STATEMENT_ALIGNED);
actual_mode = it2.it()->rinfo()->rmode();
if (actual_mode == v8::internal::RelocInfo::CODE_TARGET_WITH_ID) {
actual_mode = v8::internal::RelocInfo::CODE_TARGET;
}
CHECK_EQ(mode, actual_mode);
if (mode == v8::internal::RelocInfo::JS_RETURN) {
CHECK(!Debug::IsDebugBreakAtReturn(it2.it()->rinfo()));
}
}
// --- D e b u g E v e n t H a n d l e r s
// ---
// --- The different tests uses a number of debug event handlers.
// ---
// Source for the JavaScript function which picks out the function
// name of a frame.
const char* frame_function_name_source =
"function frame_function_name(exec_state, frame_number) {"
" return exec_state.frame(frame_number).func().name();"
"}";
v8::Local<v8::Function> frame_function_name;
// Source for the JavaScript function which pick out the name of the
// first argument of a frame.
const char* frame_argument_name_source =
"function frame_argument_name(exec_state, frame_number) {"
" return exec_state.frame(frame_number).argumentName(0);"
"}";
v8::Local<v8::Function> frame_argument_name;
// Source for the JavaScript function which pick out the value of the
// first argument of a frame.
const char* frame_argument_value_source =
"function frame_argument_value(exec_state, frame_number) {"
" return exec_state.frame(frame_number).argumentValue(0).value_;"
"}";
v8::Local<v8::Function> frame_argument_value;
// Source for the JavaScript function which pick out the name of the
// first argument of a frame.
const char* frame_local_name_source =
"function frame_local_name(exec_state, frame_number) {"
" return exec_state.frame(frame_number).localName(0);"
"}";
v8::Local<v8::Function> frame_local_name;
// Source for the JavaScript function which pick out the value of the
// first argument of a frame.
const char* frame_local_value_source =
"function frame_local_value(exec_state, frame_number) {"
" return exec_state.frame(frame_number).localValue(0).value_;"
"}";
v8::Local<v8::Function> frame_local_value;
// Source for the JavaScript function which picks out the source line for the
// top frame.
const char* frame_source_line_source =
"function frame_source_line(exec_state) {"
" return exec_state.frame(0).sourceLine();"
"}";
v8::Local<v8::Function> frame_source_line;
// Source for the JavaScript function which picks out the source column for the
// top frame.
const char* frame_source_column_source =
"function frame_source_column(exec_state) {"
" return exec_state.frame(0).sourceColumn();"
"}";
v8::Local<v8::Function> frame_source_column;
// Source for the JavaScript function which picks out the script name for the
// top frame.
const char* frame_script_name_source =
"function frame_script_name(exec_state) {"
" return exec_state.frame(0).func().script().name();"
"}";
v8::Local<v8::Function> frame_script_name;
// Source for the JavaScript function which picks out the script data for the
// top frame.
const char* frame_script_data_source =
"function frame_script_data(exec_state) {"
" return exec_state.frame(0).func().script().data();"
"}";
v8::Local<v8::Function> frame_script_data;
// Source for the JavaScript function which picks out the script data from
// AfterCompile event
const char* compiled_script_data_source =
"function compiled_script_data(event_data) {"
" return event_data.script().data();"
"}";
v8::Local<v8::Function> compiled_script_data;
// Source for the JavaScript function which returns the number of frames.
static const char* frame_count_source =
"function frame_count(exec_state) {"
" return exec_state.frameCount();"
"}";
v8::Handle<v8::Function> frame_count;
// Global variable to store the last function hit - used by some tests.
char last_function_hit[80];
// Global variable to store the name and data for last script hit - used by some
// tests.
char last_script_name_hit[80];
char last_script_data_hit[80];
// Global variables to store the last source position - used by some tests.
int last_source_line = -1;
int last_source_column = -1;
// Debug event handler which counts the break points which have been hit.
int break_point_hit_count = 0;
int break_point_hit_count_deoptimize = 0;
static void DebugEventBreakPointHitCount(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Handle<v8::Object> exec_state = event_details.GetExecutionState();
v8::Handle<v8::Object> event_data = event_details.GetEventData();
v8::internal::Isolate* isolate = CcTest::i_isolate();
Debug* debug = isolate->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
// Count the number of breaks.
if (event == v8::Break) {
break_point_hit_count++;
if (!frame_function_name.IsEmpty()) {
// Get the name of the function.
const int argc = 2;
v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) };
v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
argc, argv);
if (result->IsUndefined()) {
last_function_hit[0] = '\0';
} else {
CHECK(result->IsString());
v8::Handle<v8::String> function_name(result->ToString());
function_name->WriteUtf8(last_function_hit);
}
}
if (!frame_source_line.IsEmpty()) {
// Get the source line.
const int argc = 1;
v8::Handle<v8::Value> argv[argc] = { exec_state };
v8::Handle<v8::Value> result = frame_source_line->Call(exec_state,
argc, argv);
CHECK(result->IsNumber());
last_source_line = result->Int32Value();
}
if (!frame_source_column.IsEmpty()) {
// Get the source column.
const int argc = 1;
v8::Handle<v8::Value> argv[argc] = { exec_state };
v8::Handle<v8::Value> result = frame_source_column->Call(exec_state,
argc, argv);
CHECK(result->IsNumber());
last_source_column = result->Int32Value();
}
if (!frame_script_name.IsEmpty()) {
// Get the script name of the function script.
const int argc = 1;
v8::Handle<v8::Value> argv[argc] = { exec_state };
v8::Handle<v8::Value> result = frame_script_name->Call(exec_state,
argc, argv);
if (result->IsUndefined()) {
last_script_name_hit[0] = '\0';
} else {
CHECK(result->IsString());
v8::Handle<v8::String> script_name(result->ToString());
script_name->WriteUtf8(last_script_name_hit);
}
}
if (!frame_script_data.IsEmpty()) {
// Get the script data of the function script.
const int argc = 1;
v8::Handle<v8::Value> argv[argc] = { exec_state };
v8::Handle<v8::Value> result = frame_script_data->Call(exec_state,
argc, argv);
if (result->IsUndefined()) {
last_script_data_hit[0] = '\0';
} else {
result = result->ToString();
CHECK(result->IsString());
v8::Handle<v8::String> script_data(result->ToString());
script_data->WriteUtf8(last_script_data_hit);
}
}
// Perform a full deoptimization when the specified number of
// breaks have been hit.
if (break_point_hit_count == break_point_hit_count_deoptimize) {
i::Deoptimizer::DeoptimizeAll(isolate);
}
} else if (event == v8::AfterCompile && !compiled_script_data.IsEmpty()) {
const int argc = 1;
v8::Handle<v8::Value> argv[argc] = { event_data };
v8::Handle<v8::Value> result = compiled_script_data->Call(exec_state,
argc, argv);
if (result->IsUndefined()) {
last_script_data_hit[0] = '\0';
} else {
result = result->ToString();
CHECK(result->IsString());
v8::Handle<v8::String> script_data(result->ToString());
script_data->WriteUtf8(last_script_data_hit);
}
}
}
// Debug event handler which counts a number of events and collects the stack
// height if there is a function compiled for that.
int exception_hit_count = 0;
int uncaught_exception_hit_count = 0;
int last_js_stack_height = -1;
static void DebugEventCounterClear() {
break_point_hit_count = 0;
exception_hit_count = 0;
uncaught_exception_hit_count = 0;
}
static void DebugEventCounter(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Handle<v8::Object> exec_state = event_details.GetExecutionState();
v8::Handle<v8::Object> event_data = event_details.GetEventData();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
// Count the number of breaks.
if (event == v8::Break) {
break_point_hit_count++;
} else if (event == v8::Exception) {
exception_hit_count++;
// Check whether the exception was uncaught.
v8::Local<v8::String> fun_name =
v8::String::NewFromUtf8(CcTest::isolate(), "uncaught");
v8::Local<v8::Function> fun =
v8::Local<v8::Function>::Cast(event_data->Get(fun_name));
v8::Local<v8::Value> result = fun->Call(event_data, 0, NULL);
if (result->IsTrue()) {
uncaught_exception_hit_count++;
}
}
// Collect the JavsScript stack height if the function frame_count is
// compiled.
if (!frame_count.IsEmpty()) {
static const int kArgc = 1;
v8::Handle<v8::Value> argv[kArgc] = { exec_state };
// Using exec_state as receiver is just to have a receiver.
v8::Handle<v8::Value> result = frame_count->Call(exec_state, kArgc, argv);
last_js_stack_height = result->Int32Value();
}
}
// Debug event handler which evaluates a number of expressions when a break
// point is hit. Each evaluated expression is compared with an expected value.
// For this debug event handler to work the following two global varaibles
// must be initialized.
// checks: An array of expressions and expected results
// evaluate_check_function: A JavaScript function (see below)
// Structure for holding checks to do.
struct EvaluateCheck {
const char* expr; // An expression to evaluate when a break point is hit.
v8::Handle<v8::Value> expected; // The expected result.
};
// Array of checks to do.
struct EvaluateCheck* checks = NULL;
// Source for The JavaScript function which can do the evaluation when a break
// point is hit.
const char* evaluate_check_source =
"function evaluate_check(exec_state, expr, expected) {"
" return exec_state.frame(0).evaluate(expr).value() === expected;"
"}";
v8::Local<v8::Function> evaluate_check_function;
// The actual debug event described by the longer comment above.
static void DebugEventEvaluate(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Handle<v8::Object> exec_state = event_details.GetExecutionState();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break) {
for (int i = 0; checks[i].expr != NULL; i++) {
const int argc = 3;
v8::Handle<v8::Value> argv[argc] = {
exec_state,
v8::String::NewFromUtf8(CcTest::isolate(), checks[i].expr),
checks[i].expected};
v8::Handle<v8::Value> result =
evaluate_check_function->Call(exec_state, argc, argv);
if (!result->IsTrue()) {
v8::String::Utf8Value utf8(checks[i].expected->ToString());
V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *utf8);
}
}
}
}
// This debug event listener removes a breakpoint in a function
int debug_event_remove_break_point = 0;
static void DebugEventRemoveBreakPoint(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Handle<v8::Value> data = event_details.GetCallbackData();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break) {
break_point_hit_count++;
CHECK(data->IsFunction());
ClearBreakPoint(debug_event_remove_break_point);
}
}
// Debug event handler which counts break points hit and performs a step
// afterwards.
StepAction step_action = StepIn; // Step action to perform when stepping.
static void DebugEventStep(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break) {
break_point_hit_count++;
PrepareStep(step_action);
}
}
// Debug event handler which counts break points hit and performs a step
// afterwards. For each call the expected function is checked.
// For this debug event handler to work the following two global varaibles
// must be initialized.
// expected_step_sequence: An array of the expected function call sequence.
// frame_function_name: A JavaScript function (see below).
// String containing the expected function call sequence. Note: this only works
// if functions have name length of one.
const char* expected_step_sequence = NULL;
// The actual debug event described by the longer comment above.
static void DebugEventStepSequence(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Handle<v8::Object> exec_state = event_details.GetExecutionState();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break || event == v8::Exception) {
// Check that the current function is the expected.
CHECK(break_point_hit_count <
StrLength(expected_step_sequence));
const int argc = 2;
v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) };
v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
argc, argv);
CHECK(result->IsString());
v8::String::Utf8Value function_name(result->ToString());
CHECK_EQ(1, StrLength(*function_name));
CHECK_EQ((*function_name)[0],
expected_step_sequence[break_point_hit_count]);
// Perform step.
break_point_hit_count++;
PrepareStep(step_action);
}
}
// Debug event handler which performs a garbage collection.
static void DebugEventBreakPointCollectGarbage(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
// Perform a garbage collection when break point is hit and continue. Based
// on the number of break points hit either scavenge or mark compact
// collector is used.
if (event == v8::Break) {
break_point_hit_count++;
if (break_point_hit_count % 2 == 0) {
// Scavenge.
CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE);
} else {
// Mark sweep compact.
CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags);
}
}
}
// Debug event handler which re-issues a debug break and calls the garbage
// collector to have the heap verified.
static void DebugEventBreak(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break) {
// Count the number of breaks.
break_point_hit_count++;
// Run the garbage collector to enforce heap verification if option
// --verify-heap is set.
CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE);
// Set the break flag again to come back here as soon as possible.
v8::Debug::DebugBreak(CcTest::isolate());
}
}
// Debug event handler which re-issues a debug break until a limit has been
// reached.
int max_break_point_hit_count = 0;
bool terminate_after_max_break_point_hit = false;
static void DebugEventBreakMax(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Handle<v8::Object> exec_state = event_details.GetExecutionState();
v8::Isolate* v8_isolate = CcTest::isolate();
v8::internal::Isolate* isolate = CcTest::i_isolate();
v8::internal::Debug* debug = isolate->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break) {
if (break_point_hit_count < max_break_point_hit_count) {
// Count the number of breaks.
break_point_hit_count++;
// Collect the JavsScript stack height if the function frame_count is
// compiled.
if (!frame_count.IsEmpty()) {
static const int kArgc = 1;
v8::Handle<v8::Value> argv[kArgc] = { exec_state };
// Using exec_state as receiver is just to have a receiver.
v8::Handle<v8::Value> result =
frame_count->Call(exec_state, kArgc, argv);
last_js_stack_height = result->Int32Value();
}
// Set the break flag again to come back here as soon as possible.
v8::Debug::DebugBreak(v8_isolate);
} else if (terminate_after_max_break_point_hit) {
// Terminate execution after the last break if requested.
v8::V8::TerminateExecution(v8_isolate);
}
// Perform a full deoptimization when the specified number of
// breaks have been hit.
if (break_point_hit_count == break_point_hit_count_deoptimize) {
i::Deoptimizer::DeoptimizeAll(isolate);
}
}
}
// --- M e s s a g e C a l l b a c k
// Message callback which counts the number of messages.
int message_callback_count = 0;
static void MessageCallbackCountClear() {
message_callback_count = 0;
}
static void MessageCallbackCount(v8::Handle<v8::Message> message,
v8::Handle<v8::Value> data) {
message_callback_count++;
}
// --- T h e A c t u a l T e s t s
// Test that the debug break function is the expected one for different kinds
// of break locations.
TEST(DebugStub) {
using ::v8::internal::Builtins;
using ::v8::internal::Isolate;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
CheckDebugBreakFunction(&env,
"function f1(){}", "f1",
0,
v8::internal::RelocInfo::JS_RETURN,
NULL);
CheckDebugBreakFunction(&env,
"function f2(){x=1;}", "f2",
0,
v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
CcTest::i_isolate()->builtins()->builtin(
Builtins::kStoreIC_DebugBreak));
CheckDebugBreakFunction(&env,
"function f3(){var a=x;}", "f3",
0,
v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
CcTest::i_isolate()->builtins()->builtin(
Builtins::kLoadIC_DebugBreak));
// TODO(1240753): Make the test architecture independent or split
// parts of the debugger into architecture dependent files. This
// part currently disabled as it is not portable between IA32/ARM.
// Currently on ICs for keyed store/load on ARM.
#if !defined (__arm__) && !defined(__thumb__)
CheckDebugBreakFunction(
&env,
"function f4(){var index='propertyName'; var a={}; a[index] = 'x';}",
"f4",
0,
v8::internal::RelocInfo::CODE_TARGET,
CcTest::i_isolate()->builtins()->builtin(
Builtins::kKeyedStoreIC_DebugBreak));
CheckDebugBreakFunction(
&env,
"function f5(){var index='propertyName'; var a={}; return a[index];}",
"f5",
0,
v8::internal::RelocInfo::CODE_TARGET,
CcTest::i_isolate()->builtins()->builtin(
Builtins::kKeyedLoadIC_DebugBreak));
#endif
CheckDebugBreakFunction(
&env,
"function f6(a){return a==null;}",
"f6",
0,
v8::internal::RelocInfo::CODE_TARGET,
CcTest::i_isolate()->builtins()->builtin(
Builtins::kCompareNilIC_DebugBreak));
// Check the debug break code stubs for call ICs with different number of
// parameters.
Handle<Code> debug_break_0 = v8::internal::ComputeCallDebugBreak(0);
Handle<Code> debug_break_1 = v8::internal::ComputeCallDebugBreak(1);
Handle<Code> debug_break_4 = v8::internal::ComputeCallDebugBreak(4);
CheckDebugBreakFunction(&env,
"function f4_0(){x();}", "f4_0",
0,
v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
*debug_break_0);
CheckDebugBreakFunction(&env,
"function f4_1(){x(1);}", "f4_1",
0,
v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
*debug_break_1);
CheckDebugBreakFunction(&env,
"function f4_4(){x(1,2,3,4);}", "f4_4",
0,
v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
*debug_break_4);
}
// Test that the debug info in the VM is in sync with the functions being
// debugged.
TEST(DebugInfo) {
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
// Create a couple of functions for the test.
v8::Local<v8::Function> foo =
CompileFunction(&env, "function foo(){}", "foo");
v8::Local<v8::Function> bar =
CompileFunction(&env, "function bar(){}", "bar");
// Initially no functions are debugged.
CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
CHECK(!HasDebugInfo(foo));
CHECK(!HasDebugInfo(bar));
// One function (foo) is debugged.
int bp1 = SetBreakPoint(foo, 0);
CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
CHECK(HasDebugInfo(foo));
CHECK(!HasDebugInfo(bar));
// Two functions are debugged.
int bp2 = SetBreakPoint(bar, 0);
CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length());
CHECK(HasDebugInfo(foo));
CHECK(HasDebugInfo(bar));
// One function (bar) is debugged.
ClearBreakPoint(bp1);
CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
CHECK(!HasDebugInfo(foo));
CHECK(HasDebugInfo(bar));
// No functions are debugged.
ClearBreakPoint(bp2);
CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
CHECK(!HasDebugInfo(foo));
CHECK(!HasDebugInfo(bar));
}
// Test that a break point can be set at an IC store location.
TEST(BreakPointICStore) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(),
"function foo(){bar=0;}"))->Run();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo")));
// Run without breakpoints.
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Run with breakpoint
int bp = SetBreakPoint(foo, 0);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
// Run without breakpoints.
ClearBreakPoint(bp);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that a break point can be set at an IC load location.
TEST(BreakPointICLoad) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "bar=1"))
->Run();
v8::Script::Compile(
v8::String::NewFromUtf8(env->GetIsolate(), "function foo(){var x=bar;}"))
->Run();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo")));
// Run without breakpoints.
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Run with breakpoint.
int bp = SetBreakPoint(foo, 0);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
// Run without breakpoints.
ClearBreakPoint(bp);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that a break point can be set at an IC call location.
TEST(BreakPointICCall) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Script::Compile(
v8::String::NewFromUtf8(env->GetIsolate(), "function bar(){}"))->Run();
v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(),
"function foo(){bar();}"))->Run();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo")));
// Run without breakpoints.
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Run with breakpoint
int bp = SetBreakPoint(foo, 0);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
// Run without breakpoints.
ClearBreakPoint(bp);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that a break point can be set at an IC call location and survive a GC.
TEST(BreakPointICCallWithGC) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
v8::Debug::SetDebugEventListener2(DebugEventBreakPointCollectGarbage);
v8::Script::Compile(
v8::String::NewFromUtf8(env->GetIsolate(), "function bar(){return 1;}"))
->Run();
v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(),
"function foo(){return bar();}"))
->Run();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo")));
// Run without breakpoints.
CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
CHECK_EQ(0, break_point_hit_count);
// Run with breakpoint.
int bp = SetBreakPoint(foo, 0);
CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
CHECK_EQ(1, break_point_hit_count);
CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
CHECK_EQ(2, break_point_hit_count);
// Run without breakpoints.
ClearBreakPoint(bp);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that a break point can be set at an IC call location and survive a GC.
TEST(BreakPointConstructCallWithGC) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
v8::Debug::SetDebugEventListener2(DebugEventBreakPointCollectGarbage);
v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(),
"function bar(){ this.x = 1;}"))
->Run();
v8::Script::Compile(
v8::String::NewFromUtf8(env->GetIsolate(),
"function foo(){return new bar(1).x;}"))->Run();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo")));
// Run without breakpoints.
CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
CHECK_EQ(0, break_point_hit_count);
// Run with breakpoint.
int bp = SetBreakPoint(foo, 0);
CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
CHECK_EQ(1, break_point_hit_count);
CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
CHECK_EQ(2, break_point_hit_count);
// Run without breakpoints.
ClearBreakPoint(bp);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that a break point can be set at a return store location.
TEST(BreakPointReturn) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
// Create a functions for checking the source line and column when hitting
// a break point.
frame_source_line = CompileFunction(&env,
frame_source_line_source,
"frame_source_line");
frame_source_column = CompileFunction(&env,
frame_source_column_source,
"frame_source_column");
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Script::Compile(
v8::String::NewFromUtf8(env->GetIsolate(), "function foo(){}"))->Run();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo")));
// Run without breakpoints.
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Run with breakpoint
int bp = SetBreakPoint(foo, 0);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
CHECK_EQ(0, last_source_line);
CHECK_EQ(15, last_source_column);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
CHECK_EQ(0, last_source_line);
CHECK_EQ(15, last_source_column);
// Run without breakpoints.
ClearBreakPoint(bp);
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
static void CallWithBreakPoints(v8::Local<v8::Object> recv,
v8::Local<v8::Function> f,
int break_point_count,
int call_count) {
break_point_hit_count = 0;
for (int i = 0; i < call_count; i++) {
f->Call(recv, 0, NULL);
CHECK_EQ((i + 1) * break_point_count, break_point_hit_count);
}
}
// Test GC during break point processing.
TEST(GCDuringBreakPointProcessing) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
v8::Debug::SetDebugEventListener2(DebugEventBreakPointCollectGarbage);
v8::Local<v8::Function> foo;
// Test IC store break point with garbage collection.
foo = CompileFunction(&env, "function foo(){bar=0;}", "foo");
SetBreakPoint(foo, 0);
CallWithBreakPoints(env->Global(), foo, 1, 10);
// Test IC load break point with garbage collection.
foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo");
SetBreakPoint(foo, 0);
CallWithBreakPoints(env->Global(), foo, 1, 10);
// Test IC call break point with garbage collection.
foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo");
SetBreakPoint(foo, 0);
CallWithBreakPoints(env->Global(), foo, 1, 10);
// Test return break point with garbage collection.
foo = CompileFunction(&env, "function foo(){}", "foo");
SetBreakPoint(foo, 0);
CallWithBreakPoints(env->Global(), foo, 1, 25);
// Test debug break slot break point with garbage collection.
foo = CompileFunction(&env, "function foo(){var a;}", "foo");
SetBreakPoint(foo, 0);
CallWithBreakPoints(env->Global(), foo, 1, 25);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Call the function three times with different garbage collections in between
// and make sure that the break point survives.
static void CallAndGC(v8::Local<v8::Object> recv,
v8::Local<v8::Function> f) {
break_point_hit_count = 0;
for (int i = 0; i < 3; i++) {
// Call function.
f->Call(recv, 0, NULL);
CHECK_EQ(1 + i * 3, break_point_hit_count);
// Scavenge and call function.
CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE);
f->Call(recv, 0, NULL);
CHECK_EQ(2 + i * 3, break_point_hit_count);
// Mark sweep (and perhaps compact) and call function.
CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags);
f->Call(recv, 0, NULL);
CHECK_EQ(3 + i * 3, break_point_hit_count);
}
}
// Test that a break point can be set at a return store location.
TEST(BreakPointSurviveGC) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::Function> foo;
// Test IC store break point with garbage collection.
{
CompileFunction(&env, "function foo(){}", "foo");
foo = CompileFunction(&env, "function foo(){bar=0;}", "foo");
SetBreakPoint(foo, 0);
}
CallAndGC(env->Global(), foo);
// Test IC load break point with garbage collection.
{
CompileFunction(&env, "function foo(){}", "foo");
foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo");
SetBreakPoint(foo, 0);
}
CallAndGC(env->Global(), foo);
// Test IC call break point with garbage collection.
{
CompileFunction(&env, "function foo(){}", "foo");
foo = CompileFunction(&env,
"function bar(){};function foo(){bar();}",
"foo");
SetBreakPoint(foo, 0);
}
CallAndGC(env->Global(), foo);
// Test return break point with garbage collection.
{
CompileFunction(&env, "function foo(){}", "foo");
foo = CompileFunction(&env, "function foo(){}", "foo");
SetBreakPoint(foo, 0);
}
CallAndGC(env->Global(), foo);
// Test non IC break point with garbage collection.
{
CompileFunction(&env, "function foo(){}", "foo");
foo = CompileFunction(&env, "function foo(){var bar=0;}", "foo");
SetBreakPoint(foo, 0);
}
CallAndGC(env->Global(), foo);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that break points can be set using the global Debug object.
TEST(BreakPointThroughJavaScript) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Script::Compile(
v8::String::NewFromUtf8(env->GetIsolate(), "function bar(){}"))->Run();
v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(),
"function foo(){bar();bar();}"))
->Run();
// 012345678901234567890
// 1 2
// Break points are set at position 3 and 9
v8::Local<v8::Script> foo =
v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), "foo()"));
// Run without breakpoints.
foo->Run();
CHECK_EQ(0, break_point_hit_count);
// Run with one breakpoint
int bp1 = SetBreakPointFromJS(env->GetIsolate(), "foo", 0, 3);
foo->Run();
CHECK_EQ(1, break_point_hit_count);
foo->Run();
CHECK_EQ(2, break_point_hit_count);
// Run with two breakpoints
int bp2 = SetBreakPointFromJS(env->GetIsolate(), "foo", 0, 9);
foo->Run();
CHECK_EQ(4, break_point_hit_count);
foo->Run();
CHECK_EQ(6, break_point_hit_count);
// Run with one breakpoint
ClearBreakPointFromJS(env->GetIsolate(), bp2);
foo->Run();
CHECK_EQ(7, break_point_hit_count);
foo->Run();
CHECK_EQ(8, break_point_hit_count);
// Run without breakpoints.
ClearBreakPointFromJS(env->GetIsolate(), bp1);
foo->Run();
CHECK_EQ(8, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
// Make sure that the break point numbers are consecutive.
CHECK_EQ(1, bp1);
CHECK_EQ(2, bp2);
}
// Test that break points on scripts identified by name can be set using the
// global Debug object.
TEST(ScriptBreakPointByNameThroughJavaScript) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::String> script = v8::String::NewFromUtf8(
env->GetIsolate(),
"function f() {\n"
" function h() {\n"
" a = 0; // line 2\n"
" }\n"
" b = 1; // line 4\n"
" return h();\n"
"}\n"
"\n"
"function g() {\n"
" function h() {\n"
" a = 0;\n"
" }\n"
" b = 2; // line 12\n"
" h();\n"
" b = 3; // line 14\n"
" f(); // line 15\n"
"}");
// Compile the script and get the two functions.
v8::ScriptOrigin origin =
v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test"));
v8::Script::Compile(script, &origin)->Run();
v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "g")));
// Call f and g without break points.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Call f and g with break point on line 12.
int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 12, 0);
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
// Remove the break point again.
break_point_hit_count = 0;
ClearBreakPointFromJS(env->GetIsolate(), sbp1);
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Call f and g with break point on line 2.
int sbp2 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 2, 0);
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
// Call f and g with break point on line 2, 4, 12, 14 and 15.
int sbp3 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 4, 0);
int sbp4 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 12, 0);
int sbp5 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 14, 0);
int sbp6 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 15, 0);
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(7, break_point_hit_count);
// Remove all the break points again.
break_point_hit_count = 0;
ClearBreakPointFromJS(env->GetIsolate(), sbp2);
ClearBreakPointFromJS(env->GetIsolate(), sbp3);
ClearBreakPointFromJS(env->GetIsolate(), sbp4);
ClearBreakPointFromJS(env->GetIsolate(), sbp5);
ClearBreakPointFromJS(env->GetIsolate(), sbp6);
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
// Make sure that the break point numbers are consecutive.
CHECK_EQ(1, sbp1);
CHECK_EQ(2, sbp2);
CHECK_EQ(3, sbp3);
CHECK_EQ(4, sbp4);
CHECK_EQ(5, sbp5);
CHECK_EQ(6, sbp6);
}
TEST(ScriptBreakPointByIdThroughJavaScript) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::String> source = v8::String::NewFromUtf8(
env->GetIsolate(),
"function f() {\n"
" function h() {\n"
" a = 0; // line 2\n"
" }\n"
" b = 1; // line 4\n"
" return h();\n"
"}\n"
"\n"
"function g() {\n"
" function h() {\n"
" a = 0;\n"
" }\n"
" b = 2; // line 12\n"
" h();\n"
" b = 3; // line 14\n"
" f(); // line 15\n"
"}");
// Compile the script and get the two functions.
v8::ScriptOrigin origin =
v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test"));
v8::Local<v8::Script> script = v8::Script::Compile(source, &origin);
script->Run();
v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "g")));
// Get the script id knowing that internally it is a 32 integer.
int script_id = script->GetId();
// Call f and g without break points.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Call f and g with break point on line 12.
int sbp1 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 12, 0);
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
// Remove the break point again.
break_point_hit_count = 0;
ClearBreakPointFromJS(env->GetIsolate(), sbp1);
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Call f and g with break point on line 2.
int sbp2 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 2, 0);
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
// Call f and g with break point on line 2, 4, 12, 14 and 15.
int sbp3 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 4, 0);
int sbp4 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 12, 0);
int sbp5 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 14, 0);
int sbp6 = SetScriptBreakPointByIdFromJS(env->GetIsolate(), script_id, 15, 0);
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(7, break_point_hit_count);
// Remove all the break points again.
break_point_hit_count = 0;
ClearBreakPointFromJS(env->GetIsolate(), sbp2);
ClearBreakPointFromJS(env->GetIsolate(), sbp3);
ClearBreakPointFromJS(env->GetIsolate(), sbp4);
ClearBreakPointFromJS(env->GetIsolate(), sbp5);
ClearBreakPointFromJS(env->GetIsolate(), sbp6);
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
// Make sure that the break point numbers are consecutive.
CHECK_EQ(1, sbp1);
CHECK_EQ(2, sbp2);
CHECK_EQ(3, sbp3);
CHECK_EQ(4, sbp4);
CHECK_EQ(5, sbp5);
CHECK_EQ(6, sbp6);
}
// Test conditional script break points.
TEST(EnableDisableScriptBreakPoint) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::String> script = v8::String::NewFromUtf8(
env->GetIsolate(),
"function f() {\n"
" a = 0; // line 1\n"
"};");
// Compile the script and get function f.
v8::ScriptOrigin origin =
v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test"));
v8::Script::Compile(script, &origin)->Run();
v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
// Set script break point on line 1 (in function f).
int sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0);
// Call f while enabeling and disabling the script break point.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
DisableScriptBreakPointFromJS(env->GetIsolate(), sbp);
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
EnableScriptBreakPointFromJS(env->GetIsolate(), sbp);
f->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
DisableScriptBreakPointFromJS(env->GetIsolate(), sbp);
f->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
// Reload the script and get f again checking that the disabeling survives.
v8::Script::Compile(script, &origin)->Run();
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
f->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
EnableScriptBreakPointFromJS(env->GetIsolate(), sbp);
f->Call(env->Global(), 0, NULL);
CHECK_EQ(3, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test conditional script break points.
TEST(ConditionalScriptBreakPoint) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::String> script = v8::String::NewFromUtf8(
env->GetIsolate(),
"count = 0;\n"
"function f() {\n"
" g(count++); // line 2\n"
"};\n"
"function g(x) {\n"
" var a=x; // line 5\n"
"};");
// Compile the script and get function f.
v8::ScriptOrigin origin =
v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test"));
v8::Script::Compile(script, &origin)->Run();
v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
// Set script break point on line 5 (in function g).
int sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 5, 0);
// Call f with different conditions on the script break point.
break_point_hit_count = 0;
ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "false");
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "true");
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
ChangeScriptBreakPointConditionFromJS(env->GetIsolate(), sbp1, "x % 2 == 0");
break_point_hit_count = 0;
for (int i = 0; i < 10; i++) {
f->Call(env->Global(), 0, NULL);
}
CHECK_EQ(5, break_point_hit_count);
// Reload the script and get f again checking that the condition survives.
v8::Script::Compile(script, &origin)->Run();
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
break_point_hit_count = 0;
for (int i = 0; i < 10; i++) {
f->Call(env->Global(), 0, NULL);
}
CHECK_EQ(5, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test ignore count on script break points.
TEST(ScriptBreakPointIgnoreCount) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::String> script = v8::String::NewFromUtf8(
env->GetIsolate(),
"function f() {\n"
" a = 0; // line 1\n"
"};");
// Compile the script and get function f.
v8::ScriptOrigin origin =
v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test"));
v8::Script::Compile(script, &origin)->Run();
v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
// Set script break point on line 1 (in function f).
int sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0);
// Call f with different ignores on the script break point.
break_point_hit_count = 0;
ChangeScriptBreakPointIgnoreCountFromJS(env->GetIsolate(), sbp, 1);
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
ChangeScriptBreakPointIgnoreCountFromJS(env->GetIsolate(), sbp, 5);
break_point_hit_count = 0;
for (int i = 0; i < 10; i++) {
f->Call(env->Global(), 0, NULL);
}
CHECK_EQ(5, break_point_hit_count);
// Reload the script and get f again checking that the ignore survives.
v8::Script::Compile(script, &origin)->Run();
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
break_point_hit_count = 0;
for (int i = 0; i < 10; i++) {
f->Call(env->Global(), 0, NULL);
}
CHECK_EQ(5, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that script break points survive when a script is reloaded.
TEST(ScriptBreakPointReload) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::Function> f;
v8::Local<v8::String> script = v8::String::NewFromUtf8(
env->GetIsolate(),
"function f() {\n"
" function h() {\n"
" a = 0; // line 2\n"
" }\n"
" b = 1; // line 4\n"
" return h();\n"
"}");
v8::ScriptOrigin origin_1 =
v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "1"));
v8::ScriptOrigin origin_2 =
v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "2"));
// Set a script break point before the script is loaded.
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "1", 2, 0);
// Compile the script and get the function.
v8::Script::Compile(script, &origin_1)->Run();
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
// Call f and check that the script break point is active.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
// Compile the script again with a different script data and get the
// function.
v8::Script::Compile(script, &origin_2)->Run();
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
// Call f and check that no break points are set.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Compile the script again and get the function.
v8::Script::Compile(script, &origin_1)->Run();
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
// Call f and check that the script break point is active.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test when several scripts has the same script data
TEST(ScriptBreakPointMultiple) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::Function> f;
v8::Local<v8::String> script_f =
v8::String::NewFromUtf8(env->GetIsolate(),
"function f() {\n"
" a = 0; // line 1\n"
"}");
v8::Local<v8::Function> g;
v8::Local<v8::String> script_g =
v8::String::NewFromUtf8(env->GetIsolate(),
"function g() {\n"
" b = 0; // line 1\n"
"}");
v8::ScriptOrigin origin =
v8::ScriptOrigin(v8::String::NewFromUtf8(env->GetIsolate(), "test"));
// Set a script break point before the scripts are loaded.
int sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0);
// Compile the scripts with same script data and get the functions.
v8::Script::Compile(script_f, &origin)->Run();
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
v8::Script::Compile(script_g, &origin)->Run();
g = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "g")));
// Call f and g and check that the script break point is active.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
// Clear the script break point.
ClearBreakPointFromJS(env->GetIsolate(), sbp);
// Call f and g and check that the script break point is no longer active.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Set script break point with the scripts loaded.
sbp = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test", 1, 0);
// Call f and g and check that the script break point is active.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test the script origin which has both name and line offset.
TEST(ScriptBreakPointLineOffset) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::Function> f;
v8::Local<v8::String> script = v8::String::NewFromUtf8(
env->GetIsolate(),
"function f() {\n"
" a = 0; // line 8 as this script has line offset 7\n"
" b = 0; // line 9 as this script has line offset 7\n"
"}");
// Create script origin both name and line offset.
v8::ScriptOrigin origin(
v8::String::NewFromUtf8(env->GetIsolate(), "test.html"),
v8::Integer::New(7));
// Set two script break points before the script is loaded.
int sbp1 =
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 8, 0);
int sbp2 =
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 9, 0);
// Compile the script and get the function.
v8::Script::Compile(script, &origin)->Run();
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
// Call f and check that the script break point is active.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
// Clear the script break points.
ClearBreakPointFromJS(env->GetIsolate(), sbp1);
ClearBreakPointFromJS(env->GetIsolate(), sbp2);
// Call f and check that no script break points are active.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Set a script break point with the script loaded.
sbp1 = SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 9, 0);
// Call f and check that the script break point is active.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test script break points set on lines.
TEST(ScriptBreakPointLine) {
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
// Create a function for checking the function when hitting a break point.
frame_function_name = CompileFunction(&env,
frame_function_name_source,
"frame_function_name");
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::Function> f;
v8::Local<v8::Function> g;
v8::Local<v8::String> script =
v8::String::NewFromUtf8(env->GetIsolate(),
"a = 0 // line 0\n"
"function f() {\n"
" a = 1; // line 2\n"
"}\n"
" a = 2; // line 4\n"
" /* xx */ function g() { // line 5\n"
" function h() { // line 6\n"
" a = 3; // line 7\n"
" }\n"
" h(); // line 9\n"
" a = 4; // line 10\n"
" }\n"
" a=5; // line 12");
// Set a couple script break point before the script is loaded.
int sbp1 =
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 0, -1);
int sbp2 =
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 1, -1);
int sbp3 =
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 5, -1);
// Compile the script and get the function.
break_point_hit_count = 0;
v8::ScriptOrigin origin(
v8::String::NewFromUtf8(env->GetIsolate(), "test.html"),
v8::Integer::New(0));
v8::Script::Compile(script, &origin)->Run();
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
g = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "g")));
// Check that a break point was hit when the script was run.
CHECK_EQ(1, break_point_hit_count);
CHECK_EQ(0, StrLength(last_function_hit));
// Call f and check that the script break point.
f->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
CHECK_EQ("f", last_function_hit);
// Call g and check that the script break point.
g->Call(env->Global(), 0, NULL);
CHECK_EQ(3, break_point_hit_count);
CHECK_EQ("g", last_function_hit);
// Clear the script break point on g and set one on h.
ClearBreakPointFromJS(env->GetIsolate(), sbp3);
int sbp4 =
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 6, -1);
// Call g and check that the script break point in h is hit.
g->Call(env->Global(), 0, NULL);
CHECK_EQ(4, break_point_hit_count);
CHECK_EQ("h", last_function_hit);
// Clear break points in f and h. Set a new one in the script between
// functions f and g and test that there is no break points in f and g any
// more.
ClearBreakPointFromJS(env->GetIsolate(), sbp2);
ClearBreakPointFromJS(env->GetIsolate(), sbp4);
int sbp5 =
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 4, -1);
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
g->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Reload the script which should hit two break points.
break_point_hit_count = 0;
v8::Script::Compile(script, &origin)->Run();
CHECK_EQ(2, break_point_hit_count);
CHECK_EQ(0, StrLength(last_function_hit));
// Set a break point in the code after the last function decleration.
int sbp6 =
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 12, -1);
// Reload the script which should hit three break points.
break_point_hit_count = 0;
v8::Script::Compile(script, &origin)->Run();
CHECK_EQ(3, break_point_hit_count);
CHECK_EQ(0, StrLength(last_function_hit));
// Clear the last break points, and reload the script which should not hit any
// break points.
ClearBreakPointFromJS(env->GetIsolate(), sbp1);
ClearBreakPointFromJS(env->GetIsolate(), sbp5);
ClearBreakPointFromJS(env->GetIsolate(), sbp6);
break_point_hit_count = 0;
v8::Script::Compile(script, &origin)->Run();
CHECK_EQ(0, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test top level script break points set on lines.
TEST(ScriptBreakPointLineTopLevel) {
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::String> script =
v8::String::NewFromUtf8(env->GetIsolate(),
"function f() {\n"
" a = 1; // line 1\n"
"}\n"
"a = 2; // line 3\n");
v8::Local<v8::Function> f;
{
v8::HandleScope scope(env->GetIsolate());
v8::Script::Compile(
script, v8::String::NewFromUtf8(env->GetIsolate(), "test.html"))->Run();
}
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags);
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1);
// Call f and check that there was no break points.
break_point_hit_count = 0;
f->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
// Recompile and run script and check that break point was hit.
break_point_hit_count = 0;
v8::Script::Compile(
script, v8::String::NewFromUtf8(env->GetIsolate(), "test.html"))->Run();
CHECK_EQ(1, break_point_hit_count);
// Call f and check that there are still no break points.
break_point_hit_count = 0;
f = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "f")));
CHECK_EQ(0, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that it is possible to add and remove break points in a top level
// function which has no references but has not been collected yet.
TEST(ScriptBreakPointTopLevelCrash) {
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
env.ExposeDebug();
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Local<v8::String> script_source =
v8::String::NewFromUtf8(env->GetIsolate(),
"function f() {\n"
" return 0;\n"
"}\n"
"f()");
int sbp1 =
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1);
{
v8::HandleScope scope(env->GetIsolate());
break_point_hit_count = 0;
v8::Script::Compile(script_source,
v8::String::NewFromUtf8(env->GetIsolate(), "test.html"))
->Run();
CHECK_EQ(1, break_point_hit_count);
}
int sbp2 =
SetScriptBreakPointByNameFromJS(env->GetIsolate(), "test.html", 3, -1);
ClearBreakPointFromJS(env->GetIsolate(), sbp1);
ClearBreakPointFromJS(env->GetIsolate(), sbp2);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that it is possible to remove the last break point for a function
// inside the break handling of that break point.
TEST(RemoveBreakPointInBreak) {
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
v8::Local<v8::Function> foo =
CompileFunction(&env, "function foo(){a=1;}", "foo");
debug_event_remove_break_point = SetBreakPoint(foo, 0);
// Register the debug event listener pasing the function
v8::Debug::SetDebugEventListener2(DebugEventRemoveBreakPoint, foo);
break_point_hit_count = 0;
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
break_point_hit_count = 0;
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(0, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that the debugger statement causes a break.
TEST(DebuggerStatement) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Script::Compile(
v8::String::NewFromUtf8(env->GetIsolate(), "function bar(){debugger}"))
->Run();
v8::Script::Compile(
v8::String::NewFromUtf8(env->GetIsolate(),
"function foo(){debugger;debugger;}"))->Run();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo")));
v8::Local<v8::Function> bar = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "bar")));
// Run function with debugger statement
bar->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
// Run function with two debugger statement
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(3, break_point_hit_count);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test setting a breakpoint on the debugger statement.
TEST(DebuggerStatementBreakpoint) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
v8::Debug::SetDebugEventListener2(DebugEventBreakPointHitCount);
v8::Script::Compile(
v8::String::NewFromUtf8(env->GetIsolate(), "function foo(){debugger;}"))
->Run();
v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(
env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "foo")));
// The debugger statement triggers breakpint hit
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(1, break_point_hit_count);
int bp = SetBreakPoint(foo, 0);
// Set breakpoint does not duplicate hits
foo->Call(env->Global(), 0, NULL);
CHECK_EQ(2, break_point_hit_count);
ClearBreakPoint(bp);
v8::Debug::SetDebugEventListener2(NULL);
CheckDebuggerUnloaded();
}
// Test that the evaluation of expressions when a break point is hit generates
// the correct results.
TEST(DebugEvaluate) {
DebugLocalContext env;
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope scope(isolate);
env.ExposeDebug();
// Create a function for checking the evaluation when hitting a break point.
evaluate_check_function = CompileFunction(&env,
evaluate_check_source,
"evaluate_check");
// Register the debug event listener
v8::Debug::SetDebugEventListener2(DebugEventEvaluate);
// Different expected vaules of x and a when in a break point (u = undefined,
// d = Hello, world!).
struct EvaluateCheck checks_uu[] = {
{"x", v8::Undefined(isolate)},
{"a", v8::Undefined(isolate)},
{NULL, v8::Handle<v8::Value>()}
};
struct EvaluateCheck checks_hu[] = {
{"x", v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!")},
{"a", v8::Undefined(isolate)},
{NULL, v8::Handle<v8::Value>()}
};
struct EvaluateCheck checks_hh[] = {
{"x", v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!")},
{"a", v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!")},
{NULL, v8::Handle<v8::Value>()}
};
// Simple test function. The "y=0" is in the function foo to provide a break
// location. For "y=0" the "y" is at position 15 in the barbar function
// therefore setting breakpoint at position 15 will break at "y=0" and
// setting it higher will break after.
v8::Local<v8::Function> foo = CompileFunction(&env,
"function foo(x) {"
" var a;"
" y=0;" // To ensure break location 1.
" a=x;"
" y=0;" // To ensure break location 2.
"}",
"foo");
const int foo_break_position_1 = 15;
const int foo_break_position_2 = 29;
// Arguments with one parameter "Hello, world!"
v8::Handle<v8::Value> argv_foo[1] = {
v8::String::NewFromUtf8(env->GetIsolate(), "Hello, world!")};
// Call foo with breakpoint set before a=x and undefined as parameter.
int bp = SetBreakPoint(foo, foo_break_position_1);
checks = checks_uu;
foo->Call(env->Global(), 0, NULL);
// Call foo with breakpoint set before a=x and parameter "Hello, world!".
checks = checks_hu;
foo->Call(env->Global(), 1, argv_foo);
// Call foo with breakpoint set after a=x and parameter "Hello, world!".
ClearBreakPoint(bp);
SetBreakPoint(foo, foo_break_position_2);
checks = checks_hh;
foo->Call(env->Global(), 1, argv_foo);
// Test function with an inner function. The "y=0" is in function barbar
// to provide a break location. For "y=0" the "y" is at position 8 in the
// barbar function therefore setting breakpoint at position 8 will break at
// "y=0" and setting it higher will break after.
v8::Local<v8::Function> bar =