📄 test-debug.cc.svn-base
字号:
// 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::AddDebugEventListener(DebugEventBreakPointHitCount, v8::Undefined()); v8::Local<v8::Function> f; v8::Local<v8::Function> g; v8::Local<v8::String> script = v8::String::New( "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 = SetScriptBreakPointFromJS("test.html", 0, -1); int sbp2 = SetScriptBreakPointFromJS("test.html", 1, -1); int sbp3 = SetScriptBreakPointFromJS("test.html", 5, -1); // Compile the script and get the function. break_point_hit_count = 0; v8::ScriptOrigin origin(v8::String::New("test.html"), v8::Integer::New(0)); v8::Script::Compile(script, &origin)->Run(); f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g"))); // Chesk that a break point was hit when the script was run. CHECK_EQ(1, break_point_hit_count); CHECK_EQ(0, strlen(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(sbp3); int sbp4 = SetScriptBreakPointFromJS("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(sbp2); ClearBreakPointFromJS(sbp4); int sbp5 = SetScriptBreakPointFromJS("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, strlen(last_function_hit)); // Set a break point in the code after the last function decleration. int sbp6 = SetScriptBreakPointFromJS("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, strlen(last_function_hit)); // Clear the last break points, and reload the script which should not hit any // break points. ClearBreakPointFromJS(sbp1); ClearBreakPointFromJS(sbp5); ClearBreakPointFromJS(sbp6); break_point_hit_count = 0; v8::Script::Compile(script, &origin)->Run(); CHECK_EQ(0, break_point_hit_count); v8::Debug::RemoveDebugEventListener(DebugEventBreakPointHitCount);}// Test that it is possible to remove the last break point for a function// inside the break handling of that break point.TEST(RemoveBreakPointInBreak) { v8::HandleScope scope; DebugLocalContext env; 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::AddDebugEventListener(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::RemoveDebugEventListener(DebugEventRemoveBreakPoint);}// Test that the debugger statement causes a break.TEST(DebuggerStatement) { break_point_hit_count = 0; v8::HandleScope scope; DebugLocalContext env; v8::Debug::AddDebugEventListener(DebugEventBreakPointHitCount, v8::Undefined()); v8::Script::Compile(v8::String::New("function bar(){debugger}"))->Run(); v8::Script::Compile(v8::String::New( "function foo(){debugger;debugger;}"))->Run(); v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo"))); v8::Local<v8::Function> bar = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("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::RemoveDebugEventListener(DebugEventBreakPointHitCount);}// Thest that the evaluation of expressions when a break point is hit generates// the correct results.TEST(DebugEvaluate) { v8::HandleScope scope; DebugLocalContext env; 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::AddDebugEventListener(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()}, {"a", v8::Undefined()}, {NULL, v8::Handle<v8::Value>()} }; struct EvaluateCheck checks_hu[] = { {"x", v8::String::New("Hello, world!")}, {"a", v8::Undefined()}, {NULL, v8::Handle<v8::Value>()} }; struct EvaluateCheck checks_hh[] = { {"x", v8::String::New("Hello, world!")}, {"a", v8::String::New("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.*/" " a=x;" "}", "foo"); const int foo_break_position = 15; // Arguments with one parameter "Hello, world!" v8::Handle<v8::Value> argv_foo[1] = { v8::String::New("Hello, world!") }; // Call foo with breakpoint set before a=x and undefined as parameter. int bp = SetBreakPoint(foo, foo_break_position); 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 + 1); 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 = CompileFunction(&env, "y = 0;" "x = 'Goodbye, world!';" "function bar(x, b) {" " var a;" " function barbar() {" " y=0; /* To ensure break location.*/" " a=x;" " };" " debug.Debug.clearAllBreakPoints();" " barbar();" " y=0;a=x;" "}", "bar"); const int barbar_break_position = 8; // Call bar setting breakpoint before a=x in barbar and undefined as // parameter. checks = checks_uu; v8::Handle<v8::Value> argv_bar_1[2] = { v8::Undefined(), v8::Number::New(barbar_break_position) }; bar->Call(env->Global(), 2, argv_bar_1); // Call bar setting breakpoint before a=x in barbar and parameter // "Hello, world!". checks = checks_hu; v8::Handle<v8::Value> argv_bar_2[2] = { v8::String::New("Hello, world!"), v8::Number::New(barbar_break_position) }; bar->Call(env->Global(), 2, argv_bar_2); // Call bar setting breakpoint after a=x in barbar and parameter // "Hello, world!". checks = checks_hh; v8::Handle<v8::Value> argv_bar_3[2] = { v8::String::New("Hello, world!"), v8::Number::New(barbar_break_position + 1) }; bar->Call(env->Global(), 2, argv_bar_3); v8::Debug::RemoveDebugEventListener(DebugEventEvaluate);}// Simple test of the stepping mechanism using only store ICs.TEST(DebugStepLinear) { v8::HandleScope scope; DebugLocalContext env; // Create a function for testing stepping. v8::Local<v8::Function> foo = CompileFunction(&env, "function foo(){a=1;b=1;c=1;}", "foo"); SetBreakPoint(foo, 3); // Register a debug event listener which steps and counts. v8::Debug::AddDebugEventListener(DebugEventStep); step_action = StepIn; break_point_hit_count = 0; foo->Call(env->Global(), 0, NULL); // With stepping all break locations are hit. CHECK_EQ(4, break_point_hit_count); v8::Debug::RemoveDebugEventListener(DebugEventStep); // Register a debug event listener which just counts. v8::Debug::AddDebugEventListener(DebugEventBreakPointHitCount); break_point_hit_count = 0; foo->Call(env->Global(), 0, NULL); // Without stepping only active break points are hit. CHECK_EQ(1, break_point_hit_count); v8::Debug::RemoveDebugEventListener(DebugEventBreakPointHitCount);}// Test the stepping mechanism with different ICs.TEST(DebugStepLinearMixedICs) { v8::HandleScope scope; DebugLocalContext env; // Create a function for testing stepping. v8::Local<v8::Function> foo = CompileFunction(&env, "function bar() {};" "function foo() {" " var x;" " var index='name';" " var y = {};" " a=1;b=2;x=a;y[index]=3;x=y[index];bar();}", "foo"); SetBreakPoint(foo, 0); // Register a debug event listener which steps and counts. v8::Debug::AddDebugEventListener(DebugEventStep); step_action = StepIn; break_point_hit_count = 0; foo->Call(env->Global(), 0, NULL); // With stepping all break locations are hit. For ARM the keyed load/store // is not hit as they are not implemented as ICs.#if defined (__arm__) || defined(__thumb__) CHECK_EQ(6, break_point_hit_count);#else CHECK_EQ(8, break_point_hit_count);#endif v8::Debug::RemoveDebugEventListener(DebugEventStep); // Register a debug event listener which just counts. v8::Debug::AddDebugEventListener(DebugEventBreakPointHitCount); break_point_hit_count = 0; foo->Call(env->Global(), 0, NULL); // Without stepping only active break points are hit. CHECK_EQ(1, break_point_hit_count); v8::Debug::RemoveDebugEventListener(DebugEventBreakPointHitCount);}TEST(DebugStepIf) { v8::HandleScope scope; DebugLocalContext env; // Register a debug event listener which steps and counts. v8::Debug::AddDebugEventListener(DebugEventStep); // Create a function for testing stepping. const int argc = 1; const char* src = "function foo(x) { " " a = 1;" " if (x) {" " b = 1;" " } else {" " c = 1;" " d = 1;" " }" "}"; v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo"); SetBreakPoint(foo, 0); // Stepping through the true part. step_action = StepIn; break_point_hit_count = 0; v8::Handle<v8::Value> argv_true[argc] = { v8::True() }; foo->Call(env->Global(), argc, argv_true); CHECK_EQ(3, break_point_hit_count); // Stepping through the false part. step_a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -