blob: ca2d49e717a41fc6086e21829a2747810fd9a559 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2007-2008 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
29
30#include "v8.h"
31
32#include "api.h"
33#include "debug.h"
34#include "platform.h"
35#include "stub-cache.h"
36#include "cctest.h"
37
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000038
39using ::v8::internal::EmbeddedVector;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000040using ::v8::internal::Object;
41using ::v8::internal::OS;
42using ::v8::internal::Handle;
43using ::v8::internal::Heap;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000044using ::v8::internal::JSGlobalProxy;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000045using ::v8::internal::Code;
46using ::v8::internal::Debug;
47using ::v8::internal::Debugger;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000048using ::v8::internal::CommandMessage;
49using ::v8::internal::CommandMessageQueue;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050using ::v8::internal::StepAction;
51using ::v8::internal::StepIn; // From StepAction enum
52using ::v8::internal::StepNext; // From StepAction enum
53using ::v8::internal::StepOut; // From StepAction enum
ager@chromium.org65dad4b2009-04-23 08:48:43 +000054using ::v8::internal::Vector;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055
56
57// Size of temp buffer for formatting small strings.
58#define SMALL_STRING_BUFFER_SIZE 80
59
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000060// --- A d d i t i o n a l C h e c k H e l p e r s
61
62
63// Helper function used by the CHECK_EQ function when given Address
64// arguments. Should not be called directly.
65static inline void CheckEqualsHelper(const char* file, int line,
66 const char* expected_source,
67 ::v8::internal::Address expected,
68 const char* value_source,
69 ::v8::internal::Address value) {
70 if (expected != value) {
71 V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# "
72 "Expected: %i\n# Found: %i",
73 expected_source, value_source, expected, value);
74 }
75}
76
77
78// Helper function used by the CHECK_NE function when given Address
79// arguments. Should not be called directly.
80static inline void CheckNonEqualsHelper(const char* file, int line,
81 const char* unexpected_source,
82 ::v8::internal::Address unexpected,
83 const char* value_source,
84 ::v8::internal::Address value) {
85 if (unexpected == value) {
86 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i",
87 unexpected_source, value_source, value);
88 }
89}
90
91
92// Helper function used by the CHECK function when given code
93// arguments. Should not be called directly.
94static inline void CheckEqualsHelper(const char* file, int line,
95 const char* expected_source,
96 const Code* expected,
97 const char* value_source,
98 const Code* value) {
99 if (expected != value) {
100 V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# "
101 "Expected: %p\n# Found: %p",
102 expected_source, value_source, expected, value);
103 }
104}
105
106
107static inline void CheckNonEqualsHelper(const char* file, int line,
108 const char* expected_source,
109 const Code* expected,
110 const char* value_source,
111 const Code* value) {
112 if (expected == value) {
113 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p",
114 expected_source, value_source, value);
115 }
116}
117
118
119// --- H e l p e r C l a s s e s
120
121
122// Helper class for creating a V8 enviromnent for running tests
123class DebugLocalContext {
124 public:
125 inline DebugLocalContext(
126 v8::ExtensionConfiguration* extensions = 0,
127 v8::Handle<v8::ObjectTemplate> global_template =
128 v8::Handle<v8::ObjectTemplate>(),
129 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>())
130 : context_(v8::Context::New(extensions, global_template, global_object)) {
131 context_->Enter();
132 }
133 inline ~DebugLocalContext() {
134 context_->Exit();
135 context_.Dispose();
136 }
137 inline v8::Context* operator->() { return *context_; }
138 inline v8::Context* operator*() { return *context_; }
139 inline bool IsReady() { return !context_.IsEmpty(); }
140 void ExposeDebug() {
141 // Expose the debug context global object in the global object for testing.
142 Debug::Load();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000143 Debug::debug_context()->set_security_token(
144 v8::Utils::OpenHandle(*context_)->security_token());
145
146 Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast(
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000147 v8::Utils::OpenHandle(*context_->Global())));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000148 Handle<v8::internal::String> debug_string =
149 v8::internal::Factory::LookupAsciiSymbol("debug");
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000150 SetProperty(global, debug_string,
151 Handle<Object>(Debug::debug_context()->global_proxy()), DONT_ENUM);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000152 }
153 private:
154 v8::Persistent<v8::Context> context_;
155};
156
157
158// --- H e l p e r F u n c t i o n s
159
160
161// Compile and run the supplied source and return the fequested function.
162static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env,
163 const char* source,
164 const char* function_name) {
165 v8::Script::Compile(v8::String::New(source))->Run();
166 return v8::Local<v8::Function>::Cast(
167 (*env)->Global()->Get(v8::String::New(function_name)));
168}
169
170// Helper function that compiles and runs the source.
171static v8::Local<v8::Value> CompileRun(const char* source) {
172 return v8::Script::Compile(v8::String::New(source))->Run();
173}
174
175// Is there any debug info for the function?
176static bool HasDebugInfo(v8::Handle<v8::Function> fun) {
177 Handle<v8::internal::JSFunction> f = v8::Utils::OpenHandle(*fun);
178 Handle<v8::internal::SharedFunctionInfo> shared(f->shared());
179 return Debug::HasDebugInfo(shared);
180}
181
182
183// Set a break point in a function and return the associated break point
184// number.
185static int SetBreakPoint(Handle<v8::internal::JSFunction> fun, int position) {
186 static int break_point = 0;
187 Handle<v8::internal::SharedFunctionInfo> shared(fun->shared());
188 Debug::SetBreakPoint(
189 shared, position,
190 Handle<Object>(v8::internal::Smi::FromInt(++break_point)));
191 return break_point;
192}
193
194
195// Set a break point in a function and return the associated break point
196// number.
197static int SetBreakPoint(v8::Handle<v8::Function> fun, int position) {
198 return SetBreakPoint(v8::Utils::OpenHandle(*fun), position);
199}
200
201
202// Set a break point in a function using the Debug object and return the
203// associated break point number.
204static int SetBreakPointFromJS(const char* function_name,
205 int line, int position) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000206 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
207 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000208 "debug.Debug.setBreakPoint(%s,%d,%d)",
209 function_name, line, position);
210 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000211 v8::Handle<v8::String> str = v8::String::New(buffer.start());
212 return v8::Script::Compile(str)->Run()->Int32Value();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000213}
214
215
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000216// Set a break point in a script identified by id using the global Debug object.
217static int SetScriptBreakPointByIdFromJS(int script_id, int line, int column) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000218 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000219 if (column >= 0) {
220 // Column specified set script break point on precise location.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000221 OS::SNPrintF(buffer,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000222 "debug.Debug.setScriptBreakPointById(%d,%d,%d)",
223 script_id, line, column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000224 } else {
225 // Column not specified set script break point on line.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000226 OS::SNPrintF(buffer,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000227 "debug.Debug.setScriptBreakPointById(%d,%d)",
228 script_id, line);
229 }
230 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
231 {
232 v8::TryCatch try_catch;
233 v8::Handle<v8::String> str = v8::String::New(buffer.start());
234 v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000235 CHECK(!try_catch.HasCaught());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000236 return value->Int32Value();
237 }
238}
239
240
241// Set a break point in a script identified by name using the global Debug
242// object.
243static int SetScriptBreakPointByNameFromJS(const char* script_name,
244 int line, int column) {
245 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
246 if (column >= 0) {
247 // Column specified set script break point on precise location.
248 OS::SNPrintF(buffer,
249 "debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)",
250 script_name, line, column);
251 } else {
252 // Column not specified set script break point on line.
253 OS::SNPrintF(buffer,
254 "debug.Debug.setScriptBreakPointByName(\"%s\",%d)",
255 script_name, line);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000256 }
257 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000258 {
259 v8::TryCatch try_catch;
260 v8::Handle<v8::String> str = v8::String::New(buffer.start());
261 v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000262 CHECK(!try_catch.HasCaught());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000263 return value->Int32Value();
264 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000265}
266
267
268// Clear a break point.
269static void ClearBreakPoint(int break_point) {
270 Debug::ClearBreakPoint(
271 Handle<Object>(v8::internal::Smi::FromInt(break_point)));
272}
273
274
275// Clear a break point using the global Debug object.
276static void ClearBreakPointFromJS(int break_point_number) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000277 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
278 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000279 "debug.Debug.clearBreakPoint(%d)",
280 break_point_number);
281 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000282 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000283}
284
285
286static void EnableScriptBreakPointFromJS(int break_point_number) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000287 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
288 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000289 "debug.Debug.enableScriptBreakPoint(%d)",
290 break_point_number);
291 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000292 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000293}
294
295
296static void DisableScriptBreakPointFromJS(int break_point_number) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000297 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
298 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000299 "debug.Debug.disableScriptBreakPoint(%d)",
300 break_point_number);
301 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000302 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000303}
304
305
306static void ChangeScriptBreakPointConditionFromJS(int break_point_number,
307 const char* condition) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000308 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
309 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000310 "debug.Debug.changeScriptBreakPointCondition(%d, \"%s\")",
311 break_point_number, condition);
312 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000313 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000314}
315
316
317static void ChangeScriptBreakPointIgnoreCountFromJS(int break_point_number,
318 int ignoreCount) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000319 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
320 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000321 "debug.Debug.changeScriptBreakPointIgnoreCount(%d, %d)",
322 break_point_number, ignoreCount);
323 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000324 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000325}
326
327
328// Change break on exception.
329static void ChangeBreakOnException(bool caught, bool uncaught) {
330 Debug::ChangeBreakOnException(v8::internal::BreakException, caught);
331 Debug::ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught);
332}
333
334
335// Change break on exception using the global Debug object.
336static void ChangeBreakOnExceptionFromJS(bool caught, bool uncaught) {
337 if (caught) {
338 v8::Script::Compile(
339 v8::String::New("debug.Debug.setBreakOnException()"))->Run();
340 } else {
341 v8::Script::Compile(
342 v8::String::New("debug.Debug.clearBreakOnException()"))->Run();
343 }
344 if (uncaught) {
345 v8::Script::Compile(
346 v8::String::New("debug.Debug.setBreakOnUncaughtException()"))->Run();
347 } else {
348 v8::Script::Compile(
349 v8::String::New("debug.Debug.clearBreakOnUncaughtException()"))->Run();
350 }
351}
352
353
354// Prepare to step to next break location.
355static void PrepareStep(StepAction step_action) {
356 Debug::PrepareStep(step_action, 1);
357}
358
359
360// This function is in namespace v8::internal to be friend with class
361// v8::internal::Debug.
362namespace v8 { namespace internal { // NOLINT
363
364// Collect the currently debugged functions.
365Handle<FixedArray> GetDebuggedFunctions() {
366 v8::internal::DebugInfoListNode* node = Debug::debug_info_list_;
367
368 // Find the number of debugged functions.
369 int count = 0;
370 while (node) {
371 count++;
372 node = node->next();
373 }
374
375 // Allocate array for the debugged functions
376 Handle<FixedArray> debugged_functions =
377 v8::internal::Factory::NewFixedArray(count);
378
379 // Run through the debug info objects and collect all functions.
380 count = 0;
381 while (node) {
382 debugged_functions->set(count++, *node->debug_info());
383 node = node->next();
384 }
385
386 return debugged_functions;
387}
388
389
390static Handle<Code> ComputeCallDebugBreak(int argc) {
391 CALL_HEAP_FUNCTION(v8::internal::StubCache::ComputeCallDebugBreak(argc),
392 Code);
393}
394
ager@chromium.org381abbb2009-02-25 13:23:22 +0000395
ager@chromium.org381abbb2009-02-25 13:23:22 +0000396// Check that the debugger has been fully unloaded.
397void CheckDebuggerUnloaded(bool check_functions) {
398 // Check that the debugger context is cleared and that there is no debug
399 // information stored for the debugger.
400 CHECK(Debug::debug_context().is_null());
401 CHECK_EQ(NULL, Debug::debug_info_list_);
402
403 // Collect garbage to ensure weak handles are cleared.
404 Heap::CollectAllGarbage();
405 Heap::CollectAllGarbage();
406
407 // Iterate the head and check that there are no debugger related objects left.
408 HeapIterator iterator;
409 while (iterator.has_next()) {
410 HeapObject* obj = iterator.next();
411 CHECK(obj != NULL);
412 CHECK(!obj->IsDebugInfo());
413 CHECK(!obj->IsBreakPointInfo());
414
415 // If deep check of functions is requested check that no debug break code
416 // is left in all functions.
417 if (check_functions) {
418 if (obj->IsJSFunction()) {
419 JSFunction* fun = JSFunction::cast(obj);
420 for (RelocIterator it(fun->shared()->code()); !it.done(); it.next()) {
421 RelocInfo::Mode rmode = it.rinfo()->rmode();
422 if (RelocInfo::IsCodeTarget(rmode)) {
423 CHECK(!Debug::IsDebugBreak(it.rinfo()->target_address()));
424 } else if (RelocInfo::IsJSReturn(rmode)) {
425 CHECK(!Debug::IsDebugBreakAtReturn(it.rinfo()));
426 }
427 }
428 }
429 }
430 }
431}
432
433
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000434} } // namespace v8::internal
435
ager@chromium.org381abbb2009-02-25 13:23:22 +0000436
ager@chromium.org381abbb2009-02-25 13:23:22 +0000437// Check that the debugger has been fully unloaded.
438static void CheckDebuggerUnloaded(bool check_functions = false) {
439 v8::internal::CheckDebuggerUnloaded(check_functions);
440}
441
442
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000443// Inherit from BreakLocationIterator to get access to protected parts for
444// testing.
445class TestBreakLocationIterator: public v8::internal::BreakLocationIterator {
446 public:
447 explicit TestBreakLocationIterator(Handle<v8::internal::DebugInfo> debug_info)
448 : BreakLocationIterator(debug_info, v8::internal::SOURCE_BREAK_LOCATIONS) {}
449 v8::internal::RelocIterator* it() { return reloc_iterator_; }
450 v8::internal::RelocIterator* it_original() {
451 return reloc_iterator_original_;
452 }
453};
454
455
456// Compile a function, set a break point and check that the call at the break
457// location in the code is the expected debug_break function.
458void CheckDebugBreakFunction(DebugLocalContext* env,
459 const char* source, const char* name,
ager@chromium.org236ad962008-09-25 09:45:57 +0000460 int position, v8::internal::RelocInfo::Mode mode,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000461 Code* debug_break) {
462 // Create function and set the break point.
463 Handle<v8::internal::JSFunction> fun = v8::Utils::OpenHandle(
464 *CompileFunction(env, source, name));
465 int bp = SetBreakPoint(fun, position);
466
467 // Check that the debug break function is as expected.
468 Handle<v8::internal::SharedFunctionInfo> shared(fun->shared());
469 CHECK(Debug::HasDebugInfo(shared));
470 TestBreakLocationIterator it1(Debug::GetDebugInfo(shared));
471 it1.FindBreakLocationFromPosition(position);
472 CHECK_EQ(mode, it1.it()->rinfo()->rmode());
ager@chromium.org236ad962008-09-25 09:45:57 +0000473 if (mode != v8::internal::RelocInfo::JS_RETURN) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000474 CHECK_EQ(debug_break,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000475 Code::GetCodeFromTargetAddress(it1.it()->rinfo()->target_address()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000476 } else {
477 // TODO(1240753): Make the test architecture independent or split
478 // parts of the debugger into architecture dependent files.
479 CHECK_EQ(0xE8, *(it1.rinfo()->pc()));
480 }
481
482 // Clear the break point and check that the debug break function is no longer
483 // there
484 ClearBreakPoint(bp);
485 CHECK(!Debug::HasDebugInfo(shared));
486 CHECK(Debug::EnsureDebugInfo(shared));
487 TestBreakLocationIterator it2(Debug::GetDebugInfo(shared));
488 it2.FindBreakLocationFromPosition(position);
489 CHECK_EQ(mode, it2.it()->rinfo()->rmode());
ager@chromium.org236ad962008-09-25 09:45:57 +0000490 if (mode == v8::internal::RelocInfo::JS_RETURN) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000491 // TODO(1240753): Make the test architecture independent or split
492 // parts of the debugger into architecture dependent files.
493 CHECK_NE(0xE8, *(it2.rinfo()->pc()));
494 }
495}
496
497
498// --- D e b u g E v e n t H a n d l e r s
499// ---
500// --- The different tests uses a number of debug event handlers.
501// ---
502
503
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000504// Source for The JavaScript function which picks out the function name of the
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000505// top frame.
506const char* frame_function_name_source =
507 "function frame_function_name(exec_state) {"
508 " return exec_state.frame(0).func().name();"
509 "}";
510v8::Local<v8::Function> frame_function_name;
511
ager@chromium.org8bb60582008-12-11 12:02:20 +0000512
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000513// Source for The JavaScript function which picks out the source line for the
514// top frame.
515const char* frame_source_line_source =
516 "function frame_source_line(exec_state) {"
517 " return exec_state.frame(0).sourceLine();"
518 "}";
519v8::Local<v8::Function> frame_source_line;
520
521
522// Source for The JavaScript function which picks out the source column for the
523// top frame.
524const char* frame_source_column_source =
525 "function frame_source_column(exec_state) {"
526 " return exec_state.frame(0).sourceColumn();"
527 "}";
528v8::Local<v8::Function> frame_source_column;
529
530
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000531// Source for The JavaScript function which picks out the script name for the
532// top frame.
533const char* frame_script_name_source =
534 "function frame_script_name(exec_state) {"
535 " return exec_state.frame(0).func().script().name();"
536 "}";
537v8::Local<v8::Function> frame_script_name;
538
539
540// Source for The JavaScript function which picks out the script data for the
541// top frame.
542const char* frame_script_data_source =
543 "function frame_script_data(exec_state) {"
544 " return exec_state.frame(0).func().script().data();"
545 "}";
546v8::Local<v8::Function> frame_script_data;
547
548
ager@chromium.org8bb60582008-12-11 12:02:20 +0000549// Source for The JavaScript function which returns the number of frames.
550static const char* frame_count_source =
551 "function frame_count(exec_state) {"
552 " return exec_state.frameCount();"
553 "}";
554v8::Handle<v8::Function> frame_count;
555
556
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000557// Global variable to store the last function hit - used by some tests.
558char last_function_hit[80];
559
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000560// Global variable to store the name and data for last script hit - used by some
561// tests.
562char last_script_name_hit[80];
563char last_script_data_hit[80];
564
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000565// Global variables to store the last source position - used by some tests.
566int last_source_line = -1;
567int last_source_column = -1;
568
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000569// Debug event handler which counts the break points which have been hit.
570int break_point_hit_count = 0;
571static void DebugEventBreakPointHitCount(v8::DebugEvent event,
572 v8::Handle<v8::Object> exec_state,
573 v8::Handle<v8::Object> event_data,
574 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000575 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000576 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000577
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000578 // Count the number of breaks.
579 if (event == v8::Break) {
580 break_point_hit_count++;
581 if (!frame_function_name.IsEmpty()) {
582 // Get the name of the function.
583 const int argc = 1;
584 v8::Handle<v8::Value> argv[argc] = { exec_state };
585 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
586 argc, argv);
587 if (result->IsUndefined()) {
588 last_function_hit[0] = '\0';
589 } else {
590 CHECK(result->IsString());
591 v8::Handle<v8::String> function_name(result->ToString());
592 function_name->WriteAscii(last_function_hit);
593 }
594 }
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000595
596 if (!frame_source_line.IsEmpty()) {
597 // Get the source line.
598 const int argc = 1;
599 v8::Handle<v8::Value> argv[argc] = { exec_state };
600 v8::Handle<v8::Value> result = frame_source_line->Call(exec_state,
601 argc, argv);
602 CHECK(result->IsNumber());
603 last_source_line = result->Int32Value();
604 }
605
606 if (!frame_source_column.IsEmpty()) {
607 // Get the source column.
608 const int argc = 1;
609 v8::Handle<v8::Value> argv[argc] = { exec_state };
610 v8::Handle<v8::Value> result = frame_source_column->Call(exec_state,
611 argc, argv);
612 CHECK(result->IsNumber());
613 last_source_column = result->Int32Value();
614 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000615
616 if (!frame_script_name.IsEmpty()) {
617 // Get the script name of the function script.
618 const int argc = 1;
619 v8::Handle<v8::Value> argv[argc] = { exec_state };
620 v8::Handle<v8::Value> result = frame_script_name->Call(exec_state,
621 argc, argv);
622 if (result->IsUndefined()) {
623 last_script_name_hit[0] = '\0';
624 } else {
625 CHECK(result->IsString());
626 v8::Handle<v8::String> script_name(result->ToString());
627 script_name->WriteAscii(last_script_name_hit);
628 }
629 }
630
631 if (!frame_script_data.IsEmpty()) {
632 // Get the script data of the function script.
633 const int argc = 1;
634 v8::Handle<v8::Value> argv[argc] = { exec_state };
635 v8::Handle<v8::Value> result = frame_script_data->Call(exec_state,
636 argc, argv);
637 if (result->IsUndefined()) {
638 last_script_data_hit[0] = '\0';
639 } else {
640 result = result->ToString();
641 CHECK(result->IsString());
642 v8::Handle<v8::String> script_data(result->ToString());
643 script_data->WriteAscii(last_script_data_hit);
644 }
645 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000646 }
647}
648
649
ager@chromium.org8bb60582008-12-11 12:02:20 +0000650// Debug event handler which counts a number of events and collects the stack
651// height if there is a function compiled for that.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000652int exception_hit_count = 0;
653int uncaught_exception_hit_count = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000654int last_js_stack_height = -1;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000655
656static void DebugEventCounterClear() {
657 break_point_hit_count = 0;
658 exception_hit_count = 0;
659 uncaught_exception_hit_count = 0;
660}
661
662static void DebugEventCounter(v8::DebugEvent event,
663 v8::Handle<v8::Object> exec_state,
664 v8::Handle<v8::Object> event_data,
665 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000666 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000667 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000668
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000669 // Count the number of breaks.
670 if (event == v8::Break) {
671 break_point_hit_count++;
672 } else if (event == v8::Exception) {
673 exception_hit_count++;
674
675 // Check whether the exception was uncaught.
676 v8::Local<v8::String> fun_name = v8::String::New("uncaught");
677 v8::Local<v8::Function> fun =
678 v8::Function::Cast(*event_data->Get(fun_name));
679 v8::Local<v8::Value> result = *fun->Call(event_data, 0, NULL);
680 if (result->IsTrue()) {
681 uncaught_exception_hit_count++;
682 }
683 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000684
685 // Collect the JavsScript stack height if the function frame_count is
686 // compiled.
687 if (!frame_count.IsEmpty()) {
688 static const int kArgc = 1;
689 v8::Handle<v8::Value> argv[kArgc] = { exec_state };
690 // Using exec_state as receiver is just to have a receiver.
691 v8::Handle<v8::Value> result = frame_count->Call(exec_state, kArgc, argv);
692 last_js_stack_height = result->Int32Value();
693 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000694}
695
696
697// Debug event handler which evaluates a number of expressions when a break
698// point is hit. Each evaluated expression is compared with an expected value.
699// For this debug event handler to work the following two global varaibles
700// must be initialized.
701// checks: An array of expressions and expected results
702// evaluate_check_function: A JavaScript function (see below)
703
704// Structure for holding checks to do.
705struct EvaluateCheck {
706 const char* expr; // An expression to evaluate when a break point is hit.
707 v8::Handle<v8::Value> expected; // The expected result.
708};
709// Array of checks to do.
710struct EvaluateCheck* checks = NULL;
711// Source for The JavaScript function which can do the evaluation when a break
712// point is hit.
713const char* evaluate_check_source =
714 "function evaluate_check(exec_state, expr, expected) {"
715 " return exec_state.frame(0).evaluate(expr).value() === expected;"
716 "}";
717v8::Local<v8::Function> evaluate_check_function;
718
719// The actual debug event described by the longer comment above.
720static void DebugEventEvaluate(v8::DebugEvent event,
721 v8::Handle<v8::Object> exec_state,
722 v8::Handle<v8::Object> event_data,
723 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000724 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000725 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000726
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000727 if (event == v8::Break) {
728 for (int i = 0; checks[i].expr != NULL; i++) {
729 const int argc = 3;
730 v8::Handle<v8::Value> argv[argc] = { exec_state,
731 v8::String::New(checks[i].expr),
732 checks[i].expected };
733 v8::Handle<v8::Value> result =
734 evaluate_check_function->Call(exec_state, argc, argv);
735 if (!result->IsTrue()) {
736 v8::String::AsciiValue ascii(checks[i].expected->ToString());
737 V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *ascii);
738 }
739 }
740 }
741}
742
743
744// This debug event listener removes a breakpoint in a function
745int debug_event_remove_break_point = 0;
746static void DebugEventRemoveBreakPoint(v8::DebugEvent event,
747 v8::Handle<v8::Object> exec_state,
748 v8::Handle<v8::Object> event_data,
749 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000750 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000751 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000752
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000753 if (event == v8::Break) {
754 break_point_hit_count++;
755 v8::Handle<v8::Function> fun = v8::Handle<v8::Function>::Cast(data);
756 ClearBreakPoint(debug_event_remove_break_point);
757 }
758}
759
760
761// Debug event handler which counts break points hit and performs a step
762// afterwards.
763StepAction step_action = StepIn; // Step action to perform when stepping.
764static void DebugEventStep(v8::DebugEvent event,
765 v8::Handle<v8::Object> exec_state,
766 v8::Handle<v8::Object> event_data,
767 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000768 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000769 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000770
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000771 if (event == v8::Break) {
772 break_point_hit_count++;
773 PrepareStep(step_action);
774 }
775}
776
777
778// Debug event handler which counts break points hit and performs a step
779// afterwards. For each call the expected function is checked.
780// For this debug event handler to work the following two global varaibles
781// must be initialized.
782// expected_step_sequence: An array of the expected function call sequence.
783// frame_function_name: A JavaScript function (see below).
784
785// String containing the expected function call sequence. Note: this only works
786// if functions have name length of one.
787const char* expected_step_sequence = NULL;
788
789// The actual debug event described by the longer comment above.
790static void DebugEventStepSequence(v8::DebugEvent event,
791 v8::Handle<v8::Object> exec_state,
792 v8::Handle<v8::Object> event_data,
793 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000794 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000795 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000796
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000797 if (event == v8::Break || event == v8::Exception) {
798 // Check that the current function is the expected.
799 CHECK(break_point_hit_count <
800 static_cast<int>(strlen(expected_step_sequence)));
801 const int argc = 1;
802 v8::Handle<v8::Value> argv[argc] = { exec_state };
803 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
804 argc, argv);
805 CHECK(result->IsString());
806 v8::String::AsciiValue function_name(result->ToString());
807 CHECK_EQ(1, strlen(*function_name));
808 CHECK_EQ((*function_name)[0],
809 expected_step_sequence[break_point_hit_count]);
810
811 // Perform step.
812 break_point_hit_count++;
813 PrepareStep(step_action);
814 }
815}
816
817
818// Debug event handler which performs a garbage collection.
819static void DebugEventBreakPointCollectGarbage(
820 v8::DebugEvent event,
821 v8::Handle<v8::Object> exec_state,
822 v8::Handle<v8::Object> event_data,
823 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000824 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000825 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000826
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000827 // Perform a garbage collection when break point is hit and continue. Based
828 // on the number of break points hit either scavenge or mark compact
829 // collector is used.
830 if (event == v8::Break) {
831 break_point_hit_count++;
832 if (break_point_hit_count % 2 == 0) {
833 // Scavenge.
834 Heap::CollectGarbage(0, v8::internal::NEW_SPACE);
835 } else {
836 // Mark sweep (and perhaps compact).
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000837 Heap::CollectAllGarbage();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000838 }
839 }
840}
841
842
843// Debug event handler which re-issues a debug break and calls the garbage
844// collector to have the heap verified.
845static void DebugEventBreak(v8::DebugEvent event,
846 v8::Handle<v8::Object> exec_state,
847 v8::Handle<v8::Object> event_data,
848 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000849 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000850 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000851
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000852 if (event == v8::Break) {
853 // Count the number of breaks.
854 break_point_hit_count++;
855
856 // Run the garbage collector to enforce heap verification if option
857 // --verify-heap is set.
858 Heap::CollectGarbage(0, v8::internal::NEW_SPACE);
859
860 // Set the break flag again to come back here as soon as possible.
861 v8::Debug::DebugBreak();
862 }
863}
864
865
866// --- M e s s a g e C a l l b a c k
867
868
869// Message callback which counts the number of messages.
870int message_callback_count = 0;
871
872static void MessageCallbackCountClear() {
873 message_callback_count = 0;
874}
875
876static void MessageCallbackCount(v8::Handle<v8::Message> message,
877 v8::Handle<v8::Value> data) {
878 message_callback_count++;
879}
880
881
882// --- T h e A c t u a l T e s t s
883
884
885// Test that the debug break function is the expected one for different kinds
886// of break locations.
887TEST(DebugStub) {
888 using ::v8::internal::Builtins;
889 v8::HandleScope scope;
890 DebugLocalContext env;
891
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000892 CheckDebugBreakFunction(&env,
893 "function f1(){}", "f1",
894 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000895 v8::internal::RelocInfo::JS_RETURN,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000896 NULL);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000897 CheckDebugBreakFunction(&env,
898 "function f2(){x=1;}", "f2",
899 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000900 v8::internal::RelocInfo::CODE_TARGET,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000901 Builtins::builtin(Builtins::StoreIC_DebugBreak));
902 CheckDebugBreakFunction(&env,
903 "function f3(){var a=x;}", "f3",
904 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000905 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000906 Builtins::builtin(Builtins::LoadIC_DebugBreak));
907
ager@chromium.org236ad962008-09-25 09:45:57 +0000908// TODO(1240753): Make the test architecture independent or split
909// parts of the debugger into architecture dependent files. This
910// part currently disabled as it is not portable between IA32/ARM.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000911// Currently on ICs for keyed store/load on ARM.
912#if !defined (__arm__) && !defined(__thumb__)
913 CheckDebugBreakFunction(
914 &env,
915 "function f4(){var index='propertyName'; var a={}; a[index] = 'x';}",
916 "f4",
917 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000918 v8::internal::RelocInfo::CODE_TARGET,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000919 Builtins::builtin(Builtins::KeyedStoreIC_DebugBreak));
920 CheckDebugBreakFunction(
921 &env,
922 "function f5(){var index='propertyName'; var a={}; return a[index];}",
923 "f5",
924 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000925 v8::internal::RelocInfo::CODE_TARGET,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000926 Builtins::builtin(Builtins::KeyedLoadIC_DebugBreak));
927#endif
928
929 // Check the debug break code stubs for call ICs with different number of
930 // parameters.
931 Handle<Code> debug_break_0 = v8::internal::ComputeCallDebugBreak(0);
932 Handle<Code> debug_break_1 = v8::internal::ComputeCallDebugBreak(1);
933 Handle<Code> debug_break_4 = v8::internal::ComputeCallDebugBreak(4);
934
935 CheckDebugBreakFunction(&env,
936 "function f4_0(){x();}", "f4_0",
937 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000938 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000939 *debug_break_0);
940
941 CheckDebugBreakFunction(&env,
942 "function f4_1(){x(1);}", "f4_1",
943 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000944 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000945 *debug_break_1);
946
947 CheckDebugBreakFunction(&env,
948 "function f4_4(){x(1,2,3,4);}", "f4_4",
949 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000950 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000951 *debug_break_4);
952}
953
954
955// Test that the debug info in the VM is in sync with the functions being
956// debugged.
957TEST(DebugInfo) {
958 v8::HandleScope scope;
959 DebugLocalContext env;
960 // Create a couple of functions for the test.
961 v8::Local<v8::Function> foo =
962 CompileFunction(&env, "function foo(){}", "foo");
963 v8::Local<v8::Function> bar =
964 CompileFunction(&env, "function bar(){}", "bar");
965 // Initially no functions are debugged.
966 CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
967 CHECK(!HasDebugInfo(foo));
968 CHECK(!HasDebugInfo(bar));
969 // One function (foo) is debugged.
970 int bp1 = SetBreakPoint(foo, 0);
971 CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
972 CHECK(HasDebugInfo(foo));
973 CHECK(!HasDebugInfo(bar));
974 // Two functions are debugged.
975 int bp2 = SetBreakPoint(bar, 0);
976 CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length());
977 CHECK(HasDebugInfo(foo));
978 CHECK(HasDebugInfo(bar));
979 // One function (bar) is debugged.
980 ClearBreakPoint(bp1);
981 CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
982 CHECK(!HasDebugInfo(foo));
983 CHECK(HasDebugInfo(bar));
984 // No functions are debugged.
985 ClearBreakPoint(bp2);
986 CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
987 CHECK(!HasDebugInfo(foo));
988 CHECK(!HasDebugInfo(bar));
989}
990
991
992// Test that a break point can be set at an IC store location.
993TEST(BreakPointICStore) {
994 break_point_hit_count = 0;
995 v8::HandleScope scope;
996 DebugLocalContext env;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000997
iposva@chromium.org245aa852009-02-10 00:49:54 +0000998 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000999 v8::Undefined());
1000 v8::Script::Compile(v8::String::New("function foo(){bar=0;}"))->Run();
1001 v8::Local<v8::Function> foo =
1002 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1003
1004 // Run without breakpoints.
1005 foo->Call(env->Global(), 0, NULL);
1006 CHECK_EQ(0, break_point_hit_count);
1007
1008 // Run with breakpoint
1009 int bp = SetBreakPoint(foo, 0);
1010 foo->Call(env->Global(), 0, NULL);
1011 CHECK_EQ(1, break_point_hit_count);
1012 foo->Call(env->Global(), 0, NULL);
1013 CHECK_EQ(2, break_point_hit_count);
1014
1015 // Run without breakpoints.
1016 ClearBreakPoint(bp);
1017 foo->Call(env->Global(), 0, NULL);
1018 CHECK_EQ(2, break_point_hit_count);
1019
iposva@chromium.org245aa852009-02-10 00:49:54 +00001020 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001021 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001022}
1023
1024
1025// Test that a break point can be set at an IC load location.
1026TEST(BreakPointICLoad) {
1027 break_point_hit_count = 0;
1028 v8::HandleScope scope;
1029 DebugLocalContext env;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001030 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001031 v8::Undefined());
1032 v8::Script::Compile(v8::String::New("bar=1"))->Run();
1033 v8::Script::Compile(v8::String::New("function foo(){var x=bar;}"))->Run();
1034 v8::Local<v8::Function> foo =
1035 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1036
1037 // Run without breakpoints.
1038 foo->Call(env->Global(), 0, NULL);
1039 CHECK_EQ(0, break_point_hit_count);
1040
1041 // Run with breakpoint
1042 int bp = SetBreakPoint(foo, 0);
1043 foo->Call(env->Global(), 0, NULL);
1044 CHECK_EQ(1, break_point_hit_count);
1045 foo->Call(env->Global(), 0, NULL);
1046 CHECK_EQ(2, break_point_hit_count);
1047
1048 // Run without breakpoints.
1049 ClearBreakPoint(bp);
1050 foo->Call(env->Global(), 0, NULL);
1051 CHECK_EQ(2, break_point_hit_count);
1052
iposva@chromium.org245aa852009-02-10 00:49:54 +00001053 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001054 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001055}
1056
1057
1058// Test that a break point can be set at an IC call location.
1059TEST(BreakPointICCall) {
1060 break_point_hit_count = 0;
1061 v8::HandleScope scope;
1062 DebugLocalContext env;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001063 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001064 v8::Undefined());
1065 v8::Script::Compile(v8::String::New("function bar(){}"))->Run();
1066 v8::Script::Compile(v8::String::New("function foo(){bar();}"))->Run();
1067 v8::Local<v8::Function> foo =
1068 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1069
1070 // Run without breakpoints.
1071 foo->Call(env->Global(), 0, NULL);
1072 CHECK_EQ(0, break_point_hit_count);
1073
1074 // Run with breakpoint
1075 int bp = SetBreakPoint(foo, 0);
1076 foo->Call(env->Global(), 0, NULL);
1077 CHECK_EQ(1, break_point_hit_count);
1078 foo->Call(env->Global(), 0, NULL);
1079 CHECK_EQ(2, break_point_hit_count);
1080
1081 // Run without breakpoints.
1082 ClearBreakPoint(bp);
1083 foo->Call(env->Global(), 0, NULL);
1084 CHECK_EQ(2, break_point_hit_count);
1085
iposva@chromium.org245aa852009-02-10 00:49:54 +00001086 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001087 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001088}
1089
1090
1091// Test that a break point can be set at a return store location.
1092TEST(BreakPointReturn) {
1093 break_point_hit_count = 0;
1094 v8::HandleScope scope;
1095 DebugLocalContext env;
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001096
1097 // Create a functions for checking the source line and column when hitting
1098 // a break point.
1099 frame_source_line = CompileFunction(&env,
1100 frame_source_line_source,
1101 "frame_source_line");
1102 frame_source_column = CompileFunction(&env,
1103 frame_source_column_source,
1104 "frame_source_column");
1105
1106
iposva@chromium.org245aa852009-02-10 00:49:54 +00001107 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001108 v8::Undefined());
1109 v8::Script::Compile(v8::String::New("function foo(){}"))->Run();
1110 v8::Local<v8::Function> foo =
1111 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1112
1113 // Run without breakpoints.
1114 foo->Call(env->Global(), 0, NULL);
1115 CHECK_EQ(0, break_point_hit_count);
1116
1117 // Run with breakpoint
1118 int bp = SetBreakPoint(foo, 0);
1119 foo->Call(env->Global(), 0, NULL);
1120 CHECK_EQ(1, break_point_hit_count);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001121 CHECK_EQ(0, last_source_line);
1122 CHECK_EQ(16, last_source_column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001123 foo->Call(env->Global(), 0, NULL);
1124 CHECK_EQ(2, break_point_hit_count);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001125 CHECK_EQ(0, last_source_line);
1126 CHECK_EQ(16, last_source_column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001127
1128 // Run without breakpoints.
1129 ClearBreakPoint(bp);
1130 foo->Call(env->Global(), 0, NULL);
1131 CHECK_EQ(2, break_point_hit_count);
1132
iposva@chromium.org245aa852009-02-10 00:49:54 +00001133 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001134 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001135}
1136
1137
1138static void CallWithBreakPoints(v8::Local<v8::Object> recv,
1139 v8::Local<v8::Function> f,
1140 int break_point_count,
1141 int call_count) {
1142 break_point_hit_count = 0;
1143 for (int i = 0; i < call_count; i++) {
1144 f->Call(recv, 0, NULL);
1145 CHECK_EQ((i + 1) * break_point_count, break_point_hit_count);
1146 }
1147}
1148
1149// Test GC during break point processing.
1150TEST(GCDuringBreakPointProcessing) {
1151 break_point_hit_count = 0;
1152 v8::HandleScope scope;
1153 DebugLocalContext env;
1154
iposva@chromium.org245aa852009-02-10 00:49:54 +00001155 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001156 v8::Undefined());
1157 v8::Local<v8::Function> foo;
1158
1159 // Test IC store break point with garbage collection.
1160 foo = CompileFunction(&env, "function foo(){bar=0;}", "foo");
1161 SetBreakPoint(foo, 0);
1162 CallWithBreakPoints(env->Global(), foo, 1, 10);
1163
1164 // Test IC load break point with garbage collection.
1165 foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo");
1166 SetBreakPoint(foo, 0);
1167 CallWithBreakPoints(env->Global(), foo, 1, 10);
1168
1169 // Test IC call break point with garbage collection.
1170 foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo");
1171 SetBreakPoint(foo, 0);
1172 CallWithBreakPoints(env->Global(), foo, 1, 10);
1173
1174 // Test return break point with garbage collection.
1175 foo = CompileFunction(&env, "function foo(){}", "foo");
1176 SetBreakPoint(foo, 0);
1177 CallWithBreakPoints(env->Global(), foo, 1, 25);
1178
iposva@chromium.org245aa852009-02-10 00:49:54 +00001179 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001180 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001181}
1182
1183
1184// Call the function three times with different garbage collections in between
1185// and make sure that the break point survives.
1186static void CallAndGC(v8::Local<v8::Object> recv, v8::Local<v8::Function> f) {
1187 break_point_hit_count = 0;
1188
1189 for (int i = 0; i < 3; i++) {
1190 // Call function.
1191 f->Call(recv, 0, NULL);
1192 CHECK_EQ(1 + i * 3, break_point_hit_count);
1193
1194 // Scavenge and call function.
1195 Heap::CollectGarbage(0, v8::internal::NEW_SPACE);
1196 f->Call(recv, 0, NULL);
1197 CHECK_EQ(2 + i * 3, break_point_hit_count);
1198
1199 // Mark sweep (and perhaps compact) and call function.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001200 Heap::CollectAllGarbage();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001201 f->Call(recv, 0, NULL);
1202 CHECK_EQ(3 + i * 3, break_point_hit_count);
1203 }
1204}
1205
1206
1207// Test that a break point can be set at a return store location.
1208TEST(BreakPointSurviveGC) {
1209 break_point_hit_count = 0;
1210 v8::HandleScope scope;
1211 DebugLocalContext env;
1212
iposva@chromium.org245aa852009-02-10 00:49:54 +00001213 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001214 v8::Undefined());
1215 v8::Local<v8::Function> foo;
1216
1217 // Test IC store break point with garbage collection.
1218 foo = CompileFunction(&env, "function foo(){bar=0;}", "foo");
1219 SetBreakPoint(foo, 0);
1220 CallAndGC(env->Global(), foo);
1221
1222 // Test IC load break point with garbage collection.
1223 foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo");
1224 SetBreakPoint(foo, 0);
1225 CallAndGC(env->Global(), foo);
1226
1227 // Test IC call break point with garbage collection.
1228 foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo");
1229 SetBreakPoint(foo, 0);
1230 CallAndGC(env->Global(), foo);
1231
1232 // Test return break point with garbage collection.
1233 foo = CompileFunction(&env, "function foo(){}", "foo");
1234 SetBreakPoint(foo, 0);
1235 CallAndGC(env->Global(), foo);
1236
iposva@chromium.org245aa852009-02-10 00:49:54 +00001237 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001238 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001239}
1240
1241
1242// Test that break points can be set using the global Debug object.
1243TEST(BreakPointThroughJavaScript) {
1244 break_point_hit_count = 0;
1245 v8::HandleScope scope;
1246 DebugLocalContext env;
1247 env.ExposeDebug();
1248
iposva@chromium.org245aa852009-02-10 00:49:54 +00001249 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001250 v8::Undefined());
1251 v8::Script::Compile(v8::String::New("function bar(){}"))->Run();
1252 v8::Script::Compile(v8::String::New("function foo(){bar();bar();}"))->Run();
1253 // 012345678901234567890
1254 // 1 2
1255 // Break points are set at position 3 and 9
1256 v8::Local<v8::Script> foo = v8::Script::Compile(v8::String::New("foo()"));
1257
1258 // Run without breakpoints.
1259 foo->Run();
1260 CHECK_EQ(0, break_point_hit_count);
1261
1262 // Run with one breakpoint
1263 int bp1 = SetBreakPointFromJS("foo", 0, 3);
1264 foo->Run();
1265 CHECK_EQ(1, break_point_hit_count);
1266 foo->Run();
1267 CHECK_EQ(2, break_point_hit_count);
1268
1269 // Run with two breakpoints
1270 int bp2 = SetBreakPointFromJS("foo", 0, 9);
1271 foo->Run();
1272 CHECK_EQ(4, break_point_hit_count);
1273 foo->Run();
1274 CHECK_EQ(6, break_point_hit_count);
1275
1276 // Run with one breakpoint
1277 ClearBreakPointFromJS(bp2);
1278 foo->Run();
1279 CHECK_EQ(7, break_point_hit_count);
1280 foo->Run();
1281 CHECK_EQ(8, break_point_hit_count);
1282
1283 // Run without breakpoints.
1284 ClearBreakPointFromJS(bp1);
1285 foo->Run();
1286 CHECK_EQ(8, break_point_hit_count);
1287
iposva@chromium.org245aa852009-02-10 00:49:54 +00001288 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001289 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001290
1291 // Make sure that the break point numbers are consecutive.
1292 CHECK_EQ(1, bp1);
1293 CHECK_EQ(2, bp2);
1294}
1295
1296
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001297// Test that break points on scripts identified by name can be set using the
1298// global Debug object.
1299TEST(ScriptBreakPointByNameThroughJavaScript) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001300 break_point_hit_count = 0;
1301 v8::HandleScope scope;
1302 DebugLocalContext env;
1303 env.ExposeDebug();
1304
iposva@chromium.org245aa852009-02-10 00:49:54 +00001305 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001306 v8::Undefined());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001307
1308 v8::Local<v8::String> script = v8::String::New(
1309 "function f() {\n"
1310 " function h() {\n"
1311 " a = 0; // line 2\n"
1312 " }\n"
1313 " b = 1; // line 4\n"
1314 " return h();\n"
1315 "}\n"
1316 "\n"
1317 "function g() {\n"
1318 " function h() {\n"
1319 " a = 0;\n"
1320 " }\n"
1321 " b = 2; // line 12\n"
1322 " h();\n"
1323 " b = 3; // line 14\n"
1324 " f(); // line 15\n"
1325 "}");
1326
1327 // Compile the script and get the two functions.
1328 v8::ScriptOrigin origin =
1329 v8::ScriptOrigin(v8::String::New("test"));
1330 v8::Script::Compile(script, &origin)->Run();
1331 v8::Local<v8::Function> f =
1332 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1333 v8::Local<v8::Function> g =
1334 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1335
1336 // Call f and g without break points.
1337 break_point_hit_count = 0;
1338 f->Call(env->Global(), 0, NULL);
1339 CHECK_EQ(0, break_point_hit_count);
1340 g->Call(env->Global(), 0, NULL);
1341 CHECK_EQ(0, break_point_hit_count);
1342
1343 // Call f and g with break point on line 12.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001344 int sbp1 = SetScriptBreakPointByNameFromJS("test", 12, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001345 break_point_hit_count = 0;
1346 f->Call(env->Global(), 0, NULL);
1347 CHECK_EQ(0, break_point_hit_count);
1348 g->Call(env->Global(), 0, NULL);
1349 CHECK_EQ(1, break_point_hit_count);
1350
1351 // Remove the break point again.
1352 break_point_hit_count = 0;
1353 ClearBreakPointFromJS(sbp1);
1354 f->Call(env->Global(), 0, NULL);
1355 CHECK_EQ(0, break_point_hit_count);
1356 g->Call(env->Global(), 0, NULL);
1357 CHECK_EQ(0, break_point_hit_count);
1358
1359 // Call f and g with break point on line 2.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001360 int sbp2 = SetScriptBreakPointByNameFromJS("test", 2, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001361 break_point_hit_count = 0;
1362 f->Call(env->Global(), 0, NULL);
1363 CHECK_EQ(1, break_point_hit_count);
1364 g->Call(env->Global(), 0, NULL);
1365 CHECK_EQ(2, break_point_hit_count);
1366
1367 // Call f and g with break point on line 2, 4, 12, 14 and 15.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001368 int sbp3 = SetScriptBreakPointByNameFromJS("test", 4, 0);
1369 int sbp4 = SetScriptBreakPointByNameFromJS("test", 12, 0);
1370 int sbp5 = SetScriptBreakPointByNameFromJS("test", 14, 0);
1371 int sbp6 = SetScriptBreakPointByNameFromJS("test", 15, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001372 break_point_hit_count = 0;
1373 f->Call(env->Global(), 0, NULL);
1374 CHECK_EQ(2, break_point_hit_count);
1375 g->Call(env->Global(), 0, NULL);
1376 CHECK_EQ(7, break_point_hit_count);
1377
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001378 // Remove all the break points again.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001379 break_point_hit_count = 0;
1380 ClearBreakPointFromJS(sbp2);
1381 ClearBreakPointFromJS(sbp3);
1382 ClearBreakPointFromJS(sbp4);
1383 ClearBreakPointFromJS(sbp5);
1384 ClearBreakPointFromJS(sbp6);
1385 f->Call(env->Global(), 0, NULL);
1386 CHECK_EQ(0, break_point_hit_count);
1387 g->Call(env->Global(), 0, NULL);
1388 CHECK_EQ(0, break_point_hit_count);
1389
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001390 v8::Debug::SetDebugEventListener(NULL);
1391 CheckDebuggerUnloaded();
1392
1393 // Make sure that the break point numbers are consecutive.
1394 CHECK_EQ(1, sbp1);
1395 CHECK_EQ(2, sbp2);
1396 CHECK_EQ(3, sbp3);
1397 CHECK_EQ(4, sbp4);
1398 CHECK_EQ(5, sbp5);
1399 CHECK_EQ(6, sbp6);
1400}
1401
1402
1403TEST(ScriptBreakPointByIdThroughJavaScript) {
1404 break_point_hit_count = 0;
1405 v8::HandleScope scope;
1406 DebugLocalContext env;
1407 env.ExposeDebug();
1408
1409 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
1410 v8::Undefined());
1411
1412 v8::Local<v8::String> source = v8::String::New(
1413 "function f() {\n"
1414 " function h() {\n"
1415 " a = 0; // line 2\n"
1416 " }\n"
1417 " b = 1; // line 4\n"
1418 " return h();\n"
1419 "}\n"
1420 "\n"
1421 "function g() {\n"
1422 " function h() {\n"
1423 " a = 0;\n"
1424 " }\n"
1425 " b = 2; // line 12\n"
1426 " h();\n"
1427 " b = 3; // line 14\n"
1428 " f(); // line 15\n"
1429 "}");
1430
1431 // Compile the script and get the two functions.
1432 v8::ScriptOrigin origin =
1433 v8::ScriptOrigin(v8::String::New("test"));
1434 v8::Local<v8::Script> script = v8::Script::Compile(source, &origin);
1435 script->Run();
1436 v8::Local<v8::Function> f =
1437 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1438 v8::Local<v8::Function> g =
1439 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1440
1441 // Get the script id knowing that internally it is a 32 integer.
1442 uint32_t script_id = script->Id()->Uint32Value();
1443
1444 // Call f and g without break points.
1445 break_point_hit_count = 0;
1446 f->Call(env->Global(), 0, NULL);
1447 CHECK_EQ(0, break_point_hit_count);
1448 g->Call(env->Global(), 0, NULL);
1449 CHECK_EQ(0, break_point_hit_count);
1450
1451 // Call f and g with break point on line 12.
1452 int sbp1 = SetScriptBreakPointByIdFromJS(script_id, 12, 0);
1453 break_point_hit_count = 0;
1454 f->Call(env->Global(), 0, NULL);
1455 CHECK_EQ(0, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001456 g->Call(env->Global(), 0, NULL);
1457 CHECK_EQ(1, break_point_hit_count);
1458
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001459 // Remove the break point again.
1460 break_point_hit_count = 0;
1461 ClearBreakPointFromJS(sbp1);
1462 f->Call(env->Global(), 0, NULL);
1463 CHECK_EQ(0, break_point_hit_count);
1464 g->Call(env->Global(), 0, NULL);
1465 CHECK_EQ(0, break_point_hit_count);
1466
1467 // Call f and g with break point on line 2.
1468 int sbp2 = SetScriptBreakPointByIdFromJS(script_id, 2, 0);
1469 break_point_hit_count = 0;
1470 f->Call(env->Global(), 0, NULL);
1471 CHECK_EQ(1, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001472 g->Call(env->Global(), 0, NULL);
1473 CHECK_EQ(2, break_point_hit_count);
1474
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001475 // Call f and g with break point on line 2, 4, 12, 14 and 15.
1476 int sbp3 = SetScriptBreakPointByIdFromJS(script_id, 4, 0);
1477 int sbp4 = SetScriptBreakPointByIdFromJS(script_id, 12, 0);
1478 int sbp5 = SetScriptBreakPointByIdFromJS(script_id, 14, 0);
1479 int sbp6 = SetScriptBreakPointByIdFromJS(script_id, 15, 0);
1480 break_point_hit_count = 0;
1481 f->Call(env->Global(), 0, NULL);
1482 CHECK_EQ(2, break_point_hit_count);
1483 g->Call(env->Global(), 0, NULL);
1484 CHECK_EQ(7, break_point_hit_count);
1485
1486 // Remove all the break points again.
1487 break_point_hit_count = 0;
1488 ClearBreakPointFromJS(sbp2);
1489 ClearBreakPointFromJS(sbp3);
1490 ClearBreakPointFromJS(sbp4);
1491 ClearBreakPointFromJS(sbp5);
1492 ClearBreakPointFromJS(sbp6);
1493 f->Call(env->Global(), 0, NULL);
1494 CHECK_EQ(0, break_point_hit_count);
1495 g->Call(env->Global(), 0, NULL);
1496 CHECK_EQ(0, break_point_hit_count);
1497
iposva@chromium.org245aa852009-02-10 00:49:54 +00001498 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001499 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001500
1501 // Make sure that the break point numbers are consecutive.
1502 CHECK_EQ(1, sbp1);
1503 CHECK_EQ(2, sbp2);
1504 CHECK_EQ(3, sbp3);
1505 CHECK_EQ(4, sbp4);
1506 CHECK_EQ(5, sbp5);
1507 CHECK_EQ(6, sbp6);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001508}
1509
1510
1511// Test conditional script break points.
1512TEST(EnableDisableScriptBreakPoint) {
1513 break_point_hit_count = 0;
1514 v8::HandleScope scope;
1515 DebugLocalContext env;
1516 env.ExposeDebug();
1517
iposva@chromium.org245aa852009-02-10 00:49:54 +00001518 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001519 v8::Undefined());
1520
1521 v8::Local<v8::String> script = v8::String::New(
1522 "function f() {\n"
1523 " a = 0; // line 1\n"
1524 "};");
1525
1526 // Compile the script and get function f.
1527 v8::ScriptOrigin origin =
1528 v8::ScriptOrigin(v8::String::New("test"));
1529 v8::Script::Compile(script, &origin)->Run();
1530 v8::Local<v8::Function> f =
1531 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1532
1533 // Set script break point on line 1 (in function f).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001534 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001535
1536 // Call f while enabeling and disabling the script break point.
1537 break_point_hit_count = 0;
1538 f->Call(env->Global(), 0, NULL);
1539 CHECK_EQ(1, break_point_hit_count);
1540
1541 DisableScriptBreakPointFromJS(sbp);
1542 f->Call(env->Global(), 0, NULL);
1543 CHECK_EQ(1, break_point_hit_count);
1544
1545 EnableScriptBreakPointFromJS(sbp);
1546 f->Call(env->Global(), 0, NULL);
1547 CHECK_EQ(2, break_point_hit_count);
1548
1549 DisableScriptBreakPointFromJS(sbp);
1550 f->Call(env->Global(), 0, NULL);
1551 CHECK_EQ(2, break_point_hit_count);
1552
1553 // Reload the script and get f again checking that the disabeling survives.
1554 v8::Script::Compile(script, &origin)->Run();
1555 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1556 f->Call(env->Global(), 0, NULL);
1557 CHECK_EQ(2, break_point_hit_count);
1558
1559 EnableScriptBreakPointFromJS(sbp);
1560 f->Call(env->Global(), 0, NULL);
1561 CHECK_EQ(3, break_point_hit_count);
1562
iposva@chromium.org245aa852009-02-10 00:49:54 +00001563 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001564 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001565}
1566
1567
1568// Test conditional script break points.
1569TEST(ConditionalScriptBreakPoint) {
1570 break_point_hit_count = 0;
1571 v8::HandleScope scope;
1572 DebugLocalContext env;
1573 env.ExposeDebug();
1574
iposva@chromium.org245aa852009-02-10 00:49:54 +00001575 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001576 v8::Undefined());
1577
1578 v8::Local<v8::String> script = v8::String::New(
1579 "count = 0;\n"
1580 "function f() {\n"
1581 " g(count++); // line 2\n"
1582 "};\n"
1583 "function g(x) {\n"
1584 " var a=x; // line 5\n"
1585 "};");
1586
1587 // Compile the script and get function f.
1588 v8::ScriptOrigin origin =
1589 v8::ScriptOrigin(v8::String::New("test"));
1590 v8::Script::Compile(script, &origin)->Run();
1591 v8::Local<v8::Function> f =
1592 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1593
1594 // Set script break point on line 5 (in function g).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001595 int sbp1 = SetScriptBreakPointByNameFromJS("test", 5, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001596
1597 // Call f with different conditions on the script break point.
1598 break_point_hit_count = 0;
1599 ChangeScriptBreakPointConditionFromJS(sbp1, "false");
1600 f->Call(env->Global(), 0, NULL);
1601 CHECK_EQ(0, break_point_hit_count);
1602
1603 ChangeScriptBreakPointConditionFromJS(sbp1, "true");
1604 break_point_hit_count = 0;
1605 f->Call(env->Global(), 0, NULL);
1606 CHECK_EQ(1, break_point_hit_count);
1607
1608 ChangeScriptBreakPointConditionFromJS(sbp1, "a % 2 == 0");
1609 break_point_hit_count = 0;
1610 for (int i = 0; i < 10; i++) {
1611 f->Call(env->Global(), 0, NULL);
1612 }
1613 CHECK_EQ(5, break_point_hit_count);
1614
1615 // Reload the script and get f again checking that the condition survives.
1616 v8::Script::Compile(script, &origin)->Run();
1617 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1618
1619 break_point_hit_count = 0;
1620 for (int i = 0; i < 10; i++) {
1621 f->Call(env->Global(), 0, NULL);
1622 }
1623 CHECK_EQ(5, break_point_hit_count);
1624
iposva@chromium.org245aa852009-02-10 00:49:54 +00001625 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001626 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001627}
1628
1629
1630// Test ignore count on script break points.
1631TEST(ScriptBreakPointIgnoreCount) {
1632 break_point_hit_count = 0;
1633 v8::HandleScope scope;
1634 DebugLocalContext env;
1635 env.ExposeDebug();
1636
iposva@chromium.org245aa852009-02-10 00:49:54 +00001637 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001638 v8::Undefined());
1639
1640 v8::Local<v8::String> script = v8::String::New(
1641 "function f() {\n"
1642 " a = 0; // line 1\n"
1643 "};");
1644
1645 // Compile the script and get function f.
1646 v8::ScriptOrigin origin =
1647 v8::ScriptOrigin(v8::String::New("test"));
1648 v8::Script::Compile(script, &origin)->Run();
1649 v8::Local<v8::Function> f =
1650 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1651
1652 // Set script break point on line 1 (in function f).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001653 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001654
1655 // Call f with different ignores on the script break point.
1656 break_point_hit_count = 0;
1657 ChangeScriptBreakPointIgnoreCountFromJS(sbp, 1);
1658 f->Call(env->Global(), 0, NULL);
1659 CHECK_EQ(0, break_point_hit_count);
1660 f->Call(env->Global(), 0, NULL);
1661 CHECK_EQ(1, break_point_hit_count);
1662
1663 ChangeScriptBreakPointIgnoreCountFromJS(sbp, 5);
1664 break_point_hit_count = 0;
1665 for (int i = 0; i < 10; i++) {
1666 f->Call(env->Global(), 0, NULL);
1667 }
1668 CHECK_EQ(5, break_point_hit_count);
1669
1670 // Reload the script and get f again checking that the ignore survives.
1671 v8::Script::Compile(script, &origin)->Run();
1672 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1673
1674 break_point_hit_count = 0;
1675 for (int i = 0; i < 10; i++) {
1676 f->Call(env->Global(), 0, NULL);
1677 }
1678 CHECK_EQ(5, break_point_hit_count);
1679
iposva@chromium.org245aa852009-02-10 00:49:54 +00001680 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001681 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001682}
1683
1684
1685// Test that script break points survive when a script is reloaded.
1686TEST(ScriptBreakPointReload) {
1687 break_point_hit_count = 0;
1688 v8::HandleScope scope;
1689 DebugLocalContext env;
1690 env.ExposeDebug();
1691
iposva@chromium.org245aa852009-02-10 00:49:54 +00001692 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001693 v8::Undefined());
1694
1695 v8::Local<v8::Function> f;
1696 v8::Local<v8::String> script = v8::String::New(
1697 "function f() {\n"
1698 " function h() {\n"
1699 " a = 0; // line 2\n"
1700 " }\n"
1701 " b = 1; // line 4\n"
1702 " return h();\n"
1703 "}");
1704
1705 v8::ScriptOrigin origin_1 = v8::ScriptOrigin(v8::String::New("1"));
1706 v8::ScriptOrigin origin_2 = v8::ScriptOrigin(v8::String::New("2"));
1707
1708 // Set a script break point before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001709 SetScriptBreakPointByNameFromJS("1", 2, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001710
1711 // Compile the script and get the function.
1712 v8::Script::Compile(script, &origin_1)->Run();
1713 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1714
1715 // Call f and check that the script break point is active.
1716 break_point_hit_count = 0;
1717 f->Call(env->Global(), 0, NULL);
1718 CHECK_EQ(1, break_point_hit_count);
1719
1720 // Compile the script again with a different script data and get the
1721 // function.
1722 v8::Script::Compile(script, &origin_2)->Run();
1723 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1724
1725 // Call f and check that no break points are set.
1726 break_point_hit_count = 0;
1727 f->Call(env->Global(), 0, NULL);
1728 CHECK_EQ(0, break_point_hit_count);
1729
1730 // Compile the script again and get the function.
1731 v8::Script::Compile(script, &origin_1)->Run();
1732 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1733
1734 // Call f and check that the script break point is active.
1735 break_point_hit_count = 0;
1736 f->Call(env->Global(), 0, NULL);
1737 CHECK_EQ(1, break_point_hit_count);
1738
iposva@chromium.org245aa852009-02-10 00:49:54 +00001739 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001740 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001741}
1742
1743
1744// Test when several scripts has the same script data
1745TEST(ScriptBreakPointMultiple) {
1746 break_point_hit_count = 0;
1747 v8::HandleScope scope;
1748 DebugLocalContext env;
1749 env.ExposeDebug();
1750
iposva@chromium.org245aa852009-02-10 00:49:54 +00001751 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001752 v8::Undefined());
1753
1754 v8::Local<v8::Function> f;
1755 v8::Local<v8::String> script_f = v8::String::New(
1756 "function f() {\n"
1757 " a = 0; // line 1\n"
1758 "}");
1759
1760 v8::Local<v8::Function> g;
1761 v8::Local<v8::String> script_g = v8::String::New(
1762 "function g() {\n"
1763 " b = 0; // line 1\n"
1764 "}");
1765
1766 v8::ScriptOrigin origin =
1767 v8::ScriptOrigin(v8::String::New("test"));
1768
1769 // Set a script break point before the scripts are loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001770 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001771
1772 // Compile the scripts with same script data and get the functions.
1773 v8::Script::Compile(script_f, &origin)->Run();
1774 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1775 v8::Script::Compile(script_g, &origin)->Run();
1776 g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1777
1778 // Call f and g and check that the script break point is active.
1779 break_point_hit_count = 0;
1780 f->Call(env->Global(), 0, NULL);
1781 CHECK_EQ(1, break_point_hit_count);
1782 g->Call(env->Global(), 0, NULL);
1783 CHECK_EQ(2, break_point_hit_count);
1784
1785 // Clear the script break point.
1786 ClearBreakPointFromJS(sbp);
1787
1788 // Call f and g and check that the script break point is no longer active.
1789 break_point_hit_count = 0;
1790 f->Call(env->Global(), 0, NULL);
1791 CHECK_EQ(0, break_point_hit_count);
1792 g->Call(env->Global(), 0, NULL);
1793 CHECK_EQ(0, break_point_hit_count);
1794
1795 // Set script break point with the scripts loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001796 sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001797
1798 // Call f and g and check that the script break point is active.
1799 break_point_hit_count = 0;
1800 f->Call(env->Global(), 0, NULL);
1801 CHECK_EQ(1, break_point_hit_count);
1802 g->Call(env->Global(), 0, NULL);
1803 CHECK_EQ(2, break_point_hit_count);
1804
iposva@chromium.org245aa852009-02-10 00:49:54 +00001805 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001806 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001807}
1808
1809
1810// Test the script origin which has both name and line offset.
1811TEST(ScriptBreakPointLineOffset) {
1812 break_point_hit_count = 0;
1813 v8::HandleScope scope;
1814 DebugLocalContext env;
1815 env.ExposeDebug();
1816
iposva@chromium.org245aa852009-02-10 00:49:54 +00001817 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001818 v8::Undefined());
1819
1820 v8::Local<v8::Function> f;
1821 v8::Local<v8::String> script = v8::String::New(
1822 "function f() {\n"
1823 " a = 0; // line 8 as this script has line offset 7\n"
1824 " b = 0; // line 9 as this script has line offset 7\n"
1825 "}");
1826
1827 // Create script origin both name and line offset.
1828 v8::ScriptOrigin origin(v8::String::New("test.html"),
1829 v8::Integer::New(7));
1830
1831 // Set two script break points before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001832 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 8, 0);
1833 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 9, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001834
1835 // Compile the script and get the function.
1836 v8::Script::Compile(script, &origin)->Run();
1837 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1838
1839 // Call f and check that the script break point is active.
1840 break_point_hit_count = 0;
1841 f->Call(env->Global(), 0, NULL);
1842 CHECK_EQ(2, break_point_hit_count);
1843
1844 // Clear the script break points.
1845 ClearBreakPointFromJS(sbp1);
1846 ClearBreakPointFromJS(sbp2);
1847
1848 // Call f and check that no script break points are active.
1849 break_point_hit_count = 0;
1850 f->Call(env->Global(), 0, NULL);
1851 CHECK_EQ(0, break_point_hit_count);
1852
1853 // Set a script break point with the script loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001854 sbp1 = SetScriptBreakPointByNameFromJS("test.html", 9, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001855
1856 // Call f and check that the script break point is active.
1857 break_point_hit_count = 0;
1858 f->Call(env->Global(), 0, NULL);
1859 CHECK_EQ(1, break_point_hit_count);
1860
iposva@chromium.org245aa852009-02-10 00:49:54 +00001861 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001862 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001863}
1864
1865
1866// Test script break points set on lines.
1867TEST(ScriptBreakPointLine) {
1868 v8::HandleScope scope;
1869 DebugLocalContext env;
1870 env.ExposeDebug();
1871
1872 // Create a function for checking the function when hitting a break point.
1873 frame_function_name = CompileFunction(&env,
1874 frame_function_name_source,
1875 "frame_function_name");
1876
iposva@chromium.org245aa852009-02-10 00:49:54 +00001877 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001878 v8::Undefined());
1879
1880 v8::Local<v8::Function> f;
1881 v8::Local<v8::Function> g;
1882 v8::Local<v8::String> script = v8::String::New(
1883 "a = 0 // line 0\n"
1884 "function f() {\n"
1885 " a = 1; // line 2\n"
1886 "}\n"
1887 " a = 2; // line 4\n"
1888 " /* xx */ function g() { // line 5\n"
1889 " function h() { // line 6\n"
1890 " a = 3; // line 7\n"
1891 " }\n"
1892 " h(); // line 9\n"
1893 " a = 4; // line 10\n"
1894 " }\n"
1895 " a=5; // line 12");
1896
1897 // Set a couple script break point before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001898 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 0, -1);
1899 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 1, -1);
1900 int sbp3 = SetScriptBreakPointByNameFromJS("test.html", 5, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001901
1902 // Compile the script and get the function.
1903 break_point_hit_count = 0;
1904 v8::ScriptOrigin origin(v8::String::New("test.html"), v8::Integer::New(0));
1905 v8::Script::Compile(script, &origin)->Run();
1906 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1907 g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1908
1909 // Chesk that a break point was hit when the script was run.
1910 CHECK_EQ(1, break_point_hit_count);
1911 CHECK_EQ(0, strlen(last_function_hit));
1912
1913 // Call f and check that the script break point.
1914 f->Call(env->Global(), 0, NULL);
1915 CHECK_EQ(2, break_point_hit_count);
1916 CHECK_EQ("f", last_function_hit);
1917
1918 // Call g and check that the script break point.
1919 g->Call(env->Global(), 0, NULL);
1920 CHECK_EQ(3, break_point_hit_count);
1921 CHECK_EQ("g", last_function_hit);
1922
1923 // Clear the script break point on g and set one on h.
1924 ClearBreakPointFromJS(sbp3);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001925 int sbp4 = SetScriptBreakPointByNameFromJS("test.html", 6, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001926
1927 // Call g and check that the script break point in h is hit.
1928 g->Call(env->Global(), 0, NULL);
1929 CHECK_EQ(4, break_point_hit_count);
1930 CHECK_EQ("h", last_function_hit);
1931
1932 // Clear break points in f and h. Set a new one in the script between
1933 // functions f and g and test that there is no break points in f and g any
1934 // more.
1935 ClearBreakPointFromJS(sbp2);
1936 ClearBreakPointFromJS(sbp4);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001937 int sbp5 = SetScriptBreakPointByNameFromJS("test.html", 4, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001938 break_point_hit_count = 0;
1939 f->Call(env->Global(), 0, NULL);
1940 g->Call(env->Global(), 0, NULL);
1941 CHECK_EQ(0, break_point_hit_count);
1942
1943 // Reload the script which should hit two break points.
1944 break_point_hit_count = 0;
1945 v8::Script::Compile(script, &origin)->Run();
1946 CHECK_EQ(2, break_point_hit_count);
1947 CHECK_EQ(0, strlen(last_function_hit));
1948
1949 // Set a break point in the code after the last function decleration.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001950 int sbp6 = SetScriptBreakPointByNameFromJS("test.html", 12, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001951
1952 // Reload the script which should hit three break points.
1953 break_point_hit_count = 0;
1954 v8::Script::Compile(script, &origin)->Run();
1955 CHECK_EQ(3, break_point_hit_count);
1956 CHECK_EQ(0, strlen(last_function_hit));
1957
1958 // Clear the last break points, and reload the script which should not hit any
1959 // break points.
1960 ClearBreakPointFromJS(sbp1);
1961 ClearBreakPointFromJS(sbp5);
1962 ClearBreakPointFromJS(sbp6);
1963 break_point_hit_count = 0;
1964 v8::Script::Compile(script, &origin)->Run();
1965 CHECK_EQ(0, break_point_hit_count);
1966
iposva@chromium.org245aa852009-02-10 00:49:54 +00001967 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001968 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001969}
1970
1971
1972// Test that it is possible to remove the last break point for a function
1973// inside the break handling of that break point.
1974TEST(RemoveBreakPointInBreak) {
1975 v8::HandleScope scope;
1976 DebugLocalContext env;
1977
1978 v8::Local<v8::Function> foo =
1979 CompileFunction(&env, "function foo(){a=1;}", "foo");
1980 debug_event_remove_break_point = SetBreakPoint(foo, 0);
1981
1982 // Register the debug event listener pasing the function
iposva@chromium.org245aa852009-02-10 00:49:54 +00001983 v8::Debug::SetDebugEventListener(DebugEventRemoveBreakPoint, foo);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001984
1985 break_point_hit_count = 0;
1986 foo->Call(env->Global(), 0, NULL);
1987 CHECK_EQ(1, break_point_hit_count);
1988
1989 break_point_hit_count = 0;
1990 foo->Call(env->Global(), 0, NULL);
1991 CHECK_EQ(0, break_point_hit_count);
1992
iposva@chromium.org245aa852009-02-10 00:49:54 +00001993 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001994 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001995}
1996
1997
1998// Test that the debugger statement causes a break.
1999TEST(DebuggerStatement) {
2000 break_point_hit_count = 0;
2001 v8::HandleScope scope;
2002 DebugLocalContext env;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002003 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002004 v8::Undefined());
2005 v8::Script::Compile(v8::String::New("function bar(){debugger}"))->Run();
2006 v8::Script::Compile(v8::String::New(
2007 "function foo(){debugger;debugger;}"))->Run();
2008 v8::Local<v8::Function> foo =
2009 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
2010 v8::Local<v8::Function> bar =
2011 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("bar")));
2012
2013 // Run function with debugger statement
2014 bar->Call(env->Global(), 0, NULL);
2015 CHECK_EQ(1, break_point_hit_count);
2016
2017 // Run function with two debugger statement
2018 foo->Call(env->Global(), 0, NULL);
2019 CHECK_EQ(3, break_point_hit_count);
2020
iposva@chromium.org245aa852009-02-10 00:49:54 +00002021 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002022 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002023}
2024
2025
2026// Thest that the evaluation of expressions when a break point is hit generates
2027// the correct results.
2028TEST(DebugEvaluate) {
2029 v8::HandleScope scope;
2030 DebugLocalContext env;
2031 env.ExposeDebug();
2032
2033 // Create a function for checking the evaluation when hitting a break point.
2034 evaluate_check_function = CompileFunction(&env,
2035 evaluate_check_source,
2036 "evaluate_check");
2037 // Register the debug event listener
iposva@chromium.org245aa852009-02-10 00:49:54 +00002038 v8::Debug::SetDebugEventListener(DebugEventEvaluate);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002039
2040 // Different expected vaules of x and a when in a break point (u = undefined,
2041 // d = Hello, world!).
2042 struct EvaluateCheck checks_uu[] = {
2043 {"x", v8::Undefined()},
2044 {"a", v8::Undefined()},
2045 {NULL, v8::Handle<v8::Value>()}
2046 };
2047 struct EvaluateCheck checks_hu[] = {
2048 {"x", v8::String::New("Hello, world!")},
2049 {"a", v8::Undefined()},
2050 {NULL, v8::Handle<v8::Value>()}
2051 };
2052 struct EvaluateCheck checks_hh[] = {
2053 {"x", v8::String::New("Hello, world!")},
2054 {"a", v8::String::New("Hello, world!")},
2055 {NULL, v8::Handle<v8::Value>()}
2056 };
2057
2058 // Simple test function. The "y=0" is in the function foo to provide a break
2059 // location. For "y=0" the "y" is at position 15 in the barbar function
2060 // therefore setting breakpoint at position 15 will break at "y=0" and
2061 // setting it higher will break after.
2062 v8::Local<v8::Function> foo = CompileFunction(&env,
2063 "function foo(x) {"
2064 " var a;"
2065 " y=0; /* To ensure break location.*/"
2066 " a=x;"
2067 "}",
2068 "foo");
2069 const int foo_break_position = 15;
2070
2071 // Arguments with one parameter "Hello, world!"
2072 v8::Handle<v8::Value> argv_foo[1] = { v8::String::New("Hello, world!") };
2073
2074 // Call foo with breakpoint set before a=x and undefined as parameter.
2075 int bp = SetBreakPoint(foo, foo_break_position);
2076 checks = checks_uu;
2077 foo->Call(env->Global(), 0, NULL);
2078
2079 // Call foo with breakpoint set before a=x and parameter "Hello, world!".
2080 checks = checks_hu;
2081 foo->Call(env->Global(), 1, argv_foo);
2082
2083 // Call foo with breakpoint set after a=x and parameter "Hello, world!".
2084 ClearBreakPoint(bp);
2085 SetBreakPoint(foo, foo_break_position + 1);
2086 checks = checks_hh;
2087 foo->Call(env->Global(), 1, argv_foo);
2088
2089 // Test function with an inner function. The "y=0" is in function barbar
2090 // to provide a break location. For "y=0" the "y" is at position 8 in the
2091 // barbar function therefore setting breakpoint at position 8 will break at
2092 // "y=0" and setting it higher will break after.
2093 v8::Local<v8::Function> bar = CompileFunction(&env,
2094 "y = 0;"
2095 "x = 'Goodbye, world!';"
2096 "function bar(x, b) {"
2097 " var a;"
2098 " function barbar() {"
2099 " y=0; /* To ensure break location.*/"
2100 " a=x;"
2101 " };"
2102 " debug.Debug.clearAllBreakPoints();"
2103 " barbar();"
2104 " y=0;a=x;"
2105 "}",
2106 "bar");
2107 const int barbar_break_position = 8;
2108
2109 // Call bar setting breakpoint before a=x in barbar and undefined as
2110 // parameter.
2111 checks = checks_uu;
2112 v8::Handle<v8::Value> argv_bar_1[2] = {
2113 v8::Undefined(),
2114 v8::Number::New(barbar_break_position)
2115 };
2116 bar->Call(env->Global(), 2, argv_bar_1);
2117
2118 // Call bar setting breakpoint before a=x in barbar and parameter
2119 // "Hello, world!".
2120 checks = checks_hu;
2121 v8::Handle<v8::Value> argv_bar_2[2] = {
2122 v8::String::New("Hello, world!"),
2123 v8::Number::New(barbar_break_position)
2124 };
2125 bar->Call(env->Global(), 2, argv_bar_2);
2126
2127 // Call bar setting breakpoint after a=x in barbar and parameter
2128 // "Hello, world!".
2129 checks = checks_hh;
2130 v8::Handle<v8::Value> argv_bar_3[2] = {
2131 v8::String::New("Hello, world!"),
2132 v8::Number::New(barbar_break_position + 1)
2133 };
2134 bar->Call(env->Global(), 2, argv_bar_3);
2135
iposva@chromium.org245aa852009-02-10 00:49:54 +00002136 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002137 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002138}
2139
2140
2141// Simple test of the stepping mechanism using only store ICs.
2142TEST(DebugStepLinear) {
2143 v8::HandleScope scope;
2144 DebugLocalContext env;
2145
2146 // Create a function for testing stepping.
2147 v8::Local<v8::Function> foo = CompileFunction(&env,
2148 "function foo(){a=1;b=1;c=1;}",
2149 "foo");
2150 SetBreakPoint(foo, 3);
2151
2152 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002153 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002154
2155 step_action = StepIn;
2156 break_point_hit_count = 0;
2157 foo->Call(env->Global(), 0, NULL);
2158
2159 // With stepping all break locations are hit.
2160 CHECK_EQ(4, break_point_hit_count);
2161
iposva@chromium.org245aa852009-02-10 00:49:54 +00002162 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002163 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002164
2165 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002166 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002167
ager@chromium.org381abbb2009-02-25 13:23:22 +00002168 SetBreakPoint(foo, 3);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002169 break_point_hit_count = 0;
2170 foo->Call(env->Global(), 0, NULL);
2171
2172 // Without stepping only active break points are hit.
2173 CHECK_EQ(1, break_point_hit_count);
2174
iposva@chromium.org245aa852009-02-10 00:49:54 +00002175 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002176 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002177}
2178
2179
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002180// Test of the stepping mechanism for keyed load in a loop.
2181TEST(DebugStepKeyedLoadLoop) {
2182 v8::HandleScope scope;
2183 DebugLocalContext env;
2184
2185 // Create a function for testing stepping of keyed load. The statement 'y=1'
2186 // is there to have more than one breakable statement in the loop, TODO(315).
2187 v8::Local<v8::Function> foo = CompileFunction(
2188 &env,
2189 "function foo(a) {\n"
2190 " var x;\n"
2191 " var len = a.length;\n"
2192 " for (var i = 0; i < len; i++) {\n"
2193 " y = 1;\n"
2194 " x = a[i];\n"
2195 " }\n"
2196 "}\n",
2197 "foo");
2198
2199 // Create array [0,1,2,3,4,5,6,7,8,9]
2200 v8::Local<v8::Array> a = v8::Array::New(10);
2201 for (int i = 0; i < 10; i++) {
2202 a->Set(v8::Number::New(i), v8::Number::New(i));
2203 }
2204
2205 // Call function without any break points to ensure inlining is in place.
2206 const int kArgc = 1;
2207 v8::Handle<v8::Value> args[kArgc] = { a };
2208 foo->Call(env->Global(), kArgc, args);
2209
2210 // Register a debug event listener which steps and counts.
2211 v8::Debug::SetDebugEventListener(DebugEventStep);
2212
2213 // Setup break point and step through the function.
2214 SetBreakPoint(foo, 3);
2215 step_action = StepNext;
2216 break_point_hit_count = 0;
2217 foo->Call(env->Global(), kArgc, args);
2218
2219 // With stepping all break locations are hit.
2220 CHECK_EQ(22, break_point_hit_count);
2221
2222 v8::Debug::SetDebugEventListener(NULL);
2223 CheckDebuggerUnloaded();
2224}
2225
2226
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002227// Test the stepping mechanism with different ICs.
2228TEST(DebugStepLinearMixedICs) {
2229 v8::HandleScope scope;
2230 DebugLocalContext env;
2231
2232 // Create a function for testing stepping.
2233 v8::Local<v8::Function> foo = CompileFunction(&env,
2234 "function bar() {};"
2235 "function foo() {"
2236 " var x;"
2237 " var index='name';"
2238 " var y = {};"
2239 " a=1;b=2;x=a;y[index]=3;x=y[index];bar();}", "foo");
2240 SetBreakPoint(foo, 0);
2241
2242 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002243 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002244
2245 step_action = StepIn;
2246 break_point_hit_count = 0;
2247 foo->Call(env->Global(), 0, NULL);
2248
2249 // With stepping all break locations are hit. For ARM the keyed load/store
2250 // is not hit as they are not implemented as ICs.
2251#if defined (__arm__) || defined(__thumb__)
2252 CHECK_EQ(6, break_point_hit_count);
2253#else
2254 CHECK_EQ(8, break_point_hit_count);
2255#endif
2256
iposva@chromium.org245aa852009-02-10 00:49:54 +00002257 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002258 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002259
2260 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002261 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002262
ager@chromium.org381abbb2009-02-25 13:23:22 +00002263 SetBreakPoint(foo, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002264 break_point_hit_count = 0;
2265 foo->Call(env->Global(), 0, NULL);
2266
2267 // Without stepping only active break points are hit.
2268 CHECK_EQ(1, break_point_hit_count);
2269
iposva@chromium.org245aa852009-02-10 00:49:54 +00002270 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002271 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002272}
2273
2274
2275TEST(DebugStepIf) {
2276 v8::HandleScope scope;
2277 DebugLocalContext env;
2278
2279 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002280 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002281
2282 // Create a function for testing stepping.
2283 const int argc = 1;
2284 const char* src = "function foo(x) { "
2285 " a = 1;"
2286 " if (x) {"
2287 " b = 1;"
2288 " } else {"
2289 " c = 1;"
2290 " d = 1;"
2291 " }"
2292 "}";
2293 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
2294 SetBreakPoint(foo, 0);
2295
2296 // Stepping through the true part.
2297 step_action = StepIn;
2298 break_point_hit_count = 0;
2299 v8::Handle<v8::Value> argv_true[argc] = { v8::True() };
2300 foo->Call(env->Global(), argc, argv_true);
2301 CHECK_EQ(3, break_point_hit_count);
2302
2303 // Stepping through the false part.
2304 step_action = StepIn;
2305 break_point_hit_count = 0;
2306 v8::Handle<v8::Value> argv_false[argc] = { v8::False() };
2307 foo->Call(env->Global(), argc, argv_false);
2308 CHECK_EQ(4, break_point_hit_count);
2309
2310 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002311 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002312 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002313}
2314
2315
2316TEST(DebugStepSwitch) {
2317 v8::HandleScope scope;
2318 DebugLocalContext env;
2319
2320 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002321 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002322
2323 // Create a function for testing stepping.
2324 const int argc = 1;
2325 const char* src = "function foo(x) { "
2326 " a = 1;"
2327 " switch (x) {"
2328 " case 1:"
2329 " b = 1;"
2330 " case 2:"
2331 " c = 1;"
2332 " break;"
2333 " case 3:"
2334 " d = 1;"
2335 " e = 1;"
2336 " break;"
2337 " }"
2338 "}";
2339 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
2340 SetBreakPoint(foo, 0);
2341
2342 // One case with fall-through.
2343 step_action = StepIn;
2344 break_point_hit_count = 0;
2345 v8::Handle<v8::Value> argv_1[argc] = { v8::Number::New(1) };
2346 foo->Call(env->Global(), argc, argv_1);
2347 CHECK_EQ(4, break_point_hit_count);
2348
2349 // Another case.
2350 step_action = StepIn;
2351 break_point_hit_count = 0;
2352 v8::Handle<v8::Value> argv_2[argc] = { v8::Number::New(2) };
2353 foo->Call(env->Global(), argc, argv_2);
2354 CHECK_EQ(3, break_point_hit_count);
2355
2356 // Last case.
2357 step_action = StepIn;
2358 break_point_hit_count = 0;
2359 v8::Handle<v8::Value> argv_3[argc] = { v8::Number::New(3) };
2360 foo->Call(env->Global(), argc, argv_3);
2361 CHECK_EQ(4, break_point_hit_count);
2362
2363 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002364 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002365 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002366}
2367
2368
2369TEST(DebugStepFor) {
2370 v8::HandleScope scope;
2371 DebugLocalContext env;
2372
2373 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002374 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002375
2376 // Create a function for testing stepping.
2377 const int argc = 1;
2378 const char* src = "function foo(x) { "
2379 " a = 1;"
2380 " for (i = 0; i < x; i++) {"
2381 " b = 1;"
2382 " }"
2383 "}";
2384 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
2385 SetBreakPoint(foo, 8); // "a = 1;"
2386
2387 // Looping 10 times.
2388 step_action = StepIn;
2389 break_point_hit_count = 0;
2390 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
2391 foo->Call(env->Global(), argc, argv_10);
2392 CHECK_EQ(23, break_point_hit_count);
2393
2394 // Looping 100 times.
2395 step_action = StepIn;
2396 break_point_hit_count = 0;
2397 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
2398 foo->Call(env->Global(), argc, argv_100);
2399 CHECK_EQ(203, break_point_hit_count);
2400
2401 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002402 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002403 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002404}
2405
2406
2407TEST(StepInOutSimple) {
2408 v8::HandleScope scope;
2409 DebugLocalContext env;
2410
2411 // Create a function for checking the function when hitting a break point.
2412 frame_function_name = CompileFunction(&env,
2413 frame_function_name_source,
2414 "frame_function_name");
2415
2416 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002417 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002418
2419 // Create functions for testing stepping.
2420 const char* src = "function a() {b();c();}; "
2421 "function b() {c();}; "
2422 "function c() {}; ";
2423 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
2424 SetBreakPoint(a, 0);
2425
2426 // Step through invocation of a with step in.
2427 step_action = StepIn;
2428 break_point_hit_count = 0;
2429 expected_step_sequence = "abcbaca";
2430 a->Call(env->Global(), 0, NULL);
2431 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2432
2433 // Step through invocation of a with step next.
2434 step_action = StepNext;
2435 break_point_hit_count = 0;
2436 expected_step_sequence = "aaa";
2437 a->Call(env->Global(), 0, NULL);
2438 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2439
2440 // Step through invocation of a with step out.
2441 step_action = StepOut;
2442 break_point_hit_count = 0;
2443 expected_step_sequence = "a";
2444 a->Call(env->Global(), 0, NULL);
2445 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2446
2447 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002448 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002449 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002450}
2451
2452
2453TEST(StepInOutTree) {
2454 v8::HandleScope scope;
2455 DebugLocalContext env;
2456
2457 // Create a function for checking the function when hitting a break point.
2458 frame_function_name = CompileFunction(&env,
2459 frame_function_name_source,
2460 "frame_function_name");
2461
2462 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002463 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002464
2465 // Create functions for testing stepping.
2466 const char* src = "function a() {b(c(d()),d());c(d());d()}; "
2467 "function b(x,y) {c();}; "
2468 "function c(x) {}; "
2469 "function d() {}; ";
2470 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
2471 SetBreakPoint(a, 0);
2472
2473 // Step through invocation of a with step in.
2474 step_action = StepIn;
2475 break_point_hit_count = 0;
2476 expected_step_sequence = "adacadabcbadacada";
2477 a->Call(env->Global(), 0, NULL);
2478 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2479
2480 // Step through invocation of a with step next.
2481 step_action = StepNext;
2482 break_point_hit_count = 0;
2483 expected_step_sequence = "aaaa";
2484 a->Call(env->Global(), 0, NULL);
2485 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2486
2487 // Step through invocation of a with step out.
2488 step_action = StepOut;
2489 break_point_hit_count = 0;
2490 expected_step_sequence = "a";
2491 a->Call(env->Global(), 0, NULL);
2492 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2493
2494 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002495 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002496 CheckDebuggerUnloaded(true);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002497}
2498
2499
2500TEST(StepInOutBranch) {
2501 v8::HandleScope scope;
2502 DebugLocalContext env;
2503
2504 // Create a function for checking the function when hitting a break point.
2505 frame_function_name = CompileFunction(&env,
2506 frame_function_name_source,
2507 "frame_function_name");
2508
2509 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002510 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002511
2512 // Create functions for testing stepping.
2513 const char* src = "function a() {b(false);c();}; "
2514 "function b(x) {if(x){c();};}; "
2515 "function c() {}; ";
2516 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
2517 SetBreakPoint(a, 0);
2518
2519 // Step through invocation of a.
2520 step_action = StepIn;
2521 break_point_hit_count = 0;
2522 expected_step_sequence = "abaca";
2523 a->Call(env->Global(), 0, NULL);
2524 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2525
2526 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002527 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002528 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002529}
2530
2531
2532// Test that step in does not step into native functions.
2533TEST(DebugStepNatives) {
2534 v8::HandleScope scope;
2535 DebugLocalContext env;
2536
2537 // Create a function for testing stepping.
2538 v8::Local<v8::Function> foo = CompileFunction(
2539 &env,
2540 "function foo(){debugger;Math.sin(1);}",
2541 "foo");
2542
2543 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002544 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002545
2546 step_action = StepIn;
2547 break_point_hit_count = 0;
2548 foo->Call(env->Global(), 0, NULL);
2549
2550 // With stepping all break locations are hit.
2551 CHECK_EQ(3, break_point_hit_count);
2552
iposva@chromium.org245aa852009-02-10 00:49:54 +00002553 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002554 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002555
2556 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002557 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002558
2559 break_point_hit_count = 0;
2560 foo->Call(env->Global(), 0, NULL);
2561
2562 // Without stepping only active break points are hit.
2563 CHECK_EQ(1, break_point_hit_count);
2564
iposva@chromium.org245aa852009-02-10 00:49:54 +00002565 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002566 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002567}
2568
2569
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00002570// Test that step in works with function.apply.
2571TEST(DebugStepFunctionApply) {
2572 v8::HandleScope scope;
2573 DebugLocalContext env;
2574
2575 // Create a function for testing stepping.
2576 v8::Local<v8::Function> foo = CompileFunction(
2577 &env,
2578 "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }"
2579 "function foo(){ debugger; bar.apply(this, [1,2,3]); }",
2580 "foo");
2581
2582 // Register a debug event listener which steps and counts.
2583 v8::Debug::SetDebugEventListener(DebugEventStep);
2584
2585 step_action = StepIn;
2586 break_point_hit_count = 0;
2587 foo->Call(env->Global(), 0, NULL);
2588
2589 // With stepping all break locations are hit.
2590 CHECK_EQ(6, break_point_hit_count);
2591
2592 v8::Debug::SetDebugEventListener(NULL);
2593 CheckDebuggerUnloaded();
2594
2595 // Register a debug event listener which just counts.
2596 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
2597
2598 break_point_hit_count = 0;
2599 foo->Call(env->Global(), 0, NULL);
2600
2601 // Without stepping only the debugger statement is hit.
2602 CHECK_EQ(1, break_point_hit_count);
2603
2604 v8::Debug::SetDebugEventListener(NULL);
2605 CheckDebuggerUnloaded();
2606}
2607
2608
2609// Test that step in works with function.call.
2610TEST(DebugStepFunctionCall) {
2611 v8::HandleScope scope;
2612 DebugLocalContext env;
2613
2614 // Create a function for testing stepping.
2615 v8::Local<v8::Function> foo = CompileFunction(
2616 &env,
2617 "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }"
2618 "function foo(a){ debugger;"
2619 " if (a) {"
2620 " bar.call(this, 1, 2, 3);"
2621 " } else {"
2622 " bar.call(this, 0);"
2623 " }"
2624 "}",
2625 "foo");
2626
2627 // Register a debug event listener which steps and counts.
2628 v8::Debug::SetDebugEventListener(DebugEventStep);
2629 step_action = StepIn;
2630
2631 // Check stepping where the if condition in bar is false.
2632 break_point_hit_count = 0;
2633 foo->Call(env->Global(), 0, NULL);
2634 CHECK_EQ(4, break_point_hit_count);
2635
2636 // Check stepping where the if condition in bar is true.
2637 break_point_hit_count = 0;
2638 const int argc = 1;
2639 v8::Handle<v8::Value> argv[argc] = { v8::True() };
2640 foo->Call(env->Global(), argc, argv);
2641 CHECK_EQ(6, break_point_hit_count);
2642
2643 v8::Debug::SetDebugEventListener(NULL);
2644 CheckDebuggerUnloaded();
2645
2646 // Register a debug event listener which just counts.
2647 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
2648
2649 break_point_hit_count = 0;
2650 foo->Call(env->Global(), 0, NULL);
2651
2652 // Without stepping only the debugger statement is hit.
2653 CHECK_EQ(1, break_point_hit_count);
2654
2655 v8::Debug::SetDebugEventListener(NULL);
2656 CheckDebuggerUnloaded();
2657}
2658
2659
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002660// Test break on exceptions. For each exception break combination the number
2661// of debug event exception callbacks and message callbacks are collected. The
ager@chromium.org8bb60582008-12-11 12:02:20 +00002662// number of debug event exception callbacks are used to check that the
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002663// debugger is called correctly and the number of message callbacks is used to
2664// check that uncaught exceptions are still returned even if there is a break
2665// for them.
2666TEST(BreakOnException) {
2667 v8::HandleScope scope;
2668 DebugLocalContext env;
2669 env.ExposeDebug();
2670
2671 v8::internal::Top::TraceException(false);
2672
2673 // Create functions for testing break on exception.
2674 v8::Local<v8::Function> throws =
2675 CompileFunction(&env, "function throws(){throw 1;}", "throws");
2676 v8::Local<v8::Function> caught =
2677 CompileFunction(&env,
2678 "function caught(){try {throws();} catch(e) {};}",
2679 "caught");
2680 v8::Local<v8::Function> notCaught =
2681 CompileFunction(&env, "function notCaught(){throws();}", "notCaught");
2682
2683 v8::V8::AddMessageListener(MessageCallbackCount);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002684 v8::Debug::SetDebugEventListener(DebugEventCounter);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002685
2686 // Initial state should be break on uncaught exception.
2687 DebugEventCounterClear();
2688 MessageCallbackCountClear();
2689 caught->Call(env->Global(), 0, NULL);
2690 CHECK_EQ(0, exception_hit_count);
2691 CHECK_EQ(0, uncaught_exception_hit_count);
2692 CHECK_EQ(0, message_callback_count);
2693 notCaught->Call(env->Global(), 0, NULL);
2694 CHECK_EQ(1, exception_hit_count);
2695 CHECK_EQ(1, uncaught_exception_hit_count);
2696 CHECK_EQ(1, message_callback_count);
2697
2698 // No break on exception
2699 DebugEventCounterClear();
2700 MessageCallbackCountClear();
2701 ChangeBreakOnException(false, false);
2702 caught->Call(env->Global(), 0, NULL);
2703 CHECK_EQ(0, exception_hit_count);
2704 CHECK_EQ(0, uncaught_exception_hit_count);
2705 CHECK_EQ(0, message_callback_count);
2706 notCaught->Call(env->Global(), 0, NULL);
2707 CHECK_EQ(0, exception_hit_count);
2708 CHECK_EQ(0, uncaught_exception_hit_count);
2709 CHECK_EQ(1, message_callback_count);
2710
2711 // Break on uncaught exception
2712 DebugEventCounterClear();
2713 MessageCallbackCountClear();
2714 ChangeBreakOnException(false, true);
2715 caught->Call(env->Global(), 0, NULL);
2716 CHECK_EQ(0, exception_hit_count);
2717 CHECK_EQ(0, uncaught_exception_hit_count);
2718 CHECK_EQ(0, message_callback_count);
2719 notCaught->Call(env->Global(), 0, NULL);
2720 CHECK_EQ(1, exception_hit_count);
2721 CHECK_EQ(1, uncaught_exception_hit_count);
2722 CHECK_EQ(1, message_callback_count);
2723
2724 // Break on exception and uncaught exception
2725 DebugEventCounterClear();
2726 MessageCallbackCountClear();
2727 ChangeBreakOnException(true, true);
2728 caught->Call(env->Global(), 0, NULL);
2729 CHECK_EQ(1, exception_hit_count);
2730 CHECK_EQ(0, uncaught_exception_hit_count);
2731 CHECK_EQ(0, message_callback_count);
2732 notCaught->Call(env->Global(), 0, NULL);
2733 CHECK_EQ(2, exception_hit_count);
2734 CHECK_EQ(1, uncaught_exception_hit_count);
2735 CHECK_EQ(1, message_callback_count);
2736
2737 // Break on exception
2738 DebugEventCounterClear();
2739 MessageCallbackCountClear();
2740 ChangeBreakOnException(true, false);
2741 caught->Call(env->Global(), 0, NULL);
2742 CHECK_EQ(1, exception_hit_count);
2743 CHECK_EQ(0, uncaught_exception_hit_count);
2744 CHECK_EQ(0, message_callback_count);
2745 notCaught->Call(env->Global(), 0, NULL);
2746 CHECK_EQ(2, exception_hit_count);
2747 CHECK_EQ(1, uncaught_exception_hit_count);
2748 CHECK_EQ(1, message_callback_count);
2749
2750 // No break on exception using JavaScript
2751 DebugEventCounterClear();
2752 MessageCallbackCountClear();
2753 ChangeBreakOnExceptionFromJS(false, false);
2754 caught->Call(env->Global(), 0, NULL);
2755 CHECK_EQ(0, exception_hit_count);
2756 CHECK_EQ(0, uncaught_exception_hit_count);
2757 CHECK_EQ(0, message_callback_count);
2758 notCaught->Call(env->Global(), 0, NULL);
2759 CHECK_EQ(0, exception_hit_count);
2760 CHECK_EQ(0, uncaught_exception_hit_count);
2761 CHECK_EQ(1, message_callback_count);
2762
2763 // Break on uncaught exception using JavaScript
2764 DebugEventCounterClear();
2765 MessageCallbackCountClear();
2766 ChangeBreakOnExceptionFromJS(false, true);
2767 caught->Call(env->Global(), 0, NULL);
2768 CHECK_EQ(0, exception_hit_count);
2769 CHECK_EQ(0, uncaught_exception_hit_count);
2770 CHECK_EQ(0, message_callback_count);
2771 notCaught->Call(env->Global(), 0, NULL);
2772 CHECK_EQ(1, exception_hit_count);
2773 CHECK_EQ(1, uncaught_exception_hit_count);
2774 CHECK_EQ(1, message_callback_count);
2775
2776 // Break on exception and uncaught exception using JavaScript
2777 DebugEventCounterClear();
2778 MessageCallbackCountClear();
2779 ChangeBreakOnExceptionFromJS(true, true);
2780 caught->Call(env->Global(), 0, NULL);
2781 CHECK_EQ(1, exception_hit_count);
2782 CHECK_EQ(0, message_callback_count);
2783 CHECK_EQ(0, uncaught_exception_hit_count);
2784 notCaught->Call(env->Global(), 0, NULL);
2785 CHECK_EQ(2, exception_hit_count);
2786 CHECK_EQ(1, uncaught_exception_hit_count);
2787 CHECK_EQ(1, message_callback_count);
2788
2789 // Break on exception using JavaScript
2790 DebugEventCounterClear();
2791 MessageCallbackCountClear();
2792 ChangeBreakOnExceptionFromJS(true, false);
2793 caught->Call(env->Global(), 0, NULL);
2794 CHECK_EQ(1, exception_hit_count);
2795 CHECK_EQ(0, uncaught_exception_hit_count);
2796 CHECK_EQ(0, message_callback_count);
2797 notCaught->Call(env->Global(), 0, NULL);
2798 CHECK_EQ(2, exception_hit_count);
2799 CHECK_EQ(1, uncaught_exception_hit_count);
2800 CHECK_EQ(1, message_callback_count);
2801
iposva@chromium.org245aa852009-02-10 00:49:54 +00002802 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002803 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002804 v8::V8::RemoveMessageListeners(MessageCallbackCount);
2805}
2806
2807
ager@chromium.org8bb60582008-12-11 12:02:20 +00002808// Test break on exception from compiler errors. When compiling using
2809// v8::Script::Compile there is no JavaScript stack whereas when compiling using
2810// eval there are JavaScript frames.
2811TEST(BreakOnCompileException) {
2812 v8::HandleScope scope;
2813 DebugLocalContext env;
2814
2815 v8::internal::Top::TraceException(false);
2816
2817 // Create a function for checking the function when hitting a break point.
2818 frame_count = CompileFunction(&env, frame_count_source, "frame_count");
2819
2820 v8::V8::AddMessageListener(MessageCallbackCount);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002821 v8::Debug::SetDebugEventListener(DebugEventCounter);
ager@chromium.org8bb60582008-12-11 12:02:20 +00002822
2823 DebugEventCounterClear();
2824 MessageCallbackCountClear();
2825
2826 // Check initial state.
2827 CHECK_EQ(0, exception_hit_count);
2828 CHECK_EQ(0, uncaught_exception_hit_count);
2829 CHECK_EQ(0, message_callback_count);
2830 CHECK_EQ(-1, last_js_stack_height);
2831
2832 // Throws SyntaxError: Unexpected end of input
2833 v8::Script::Compile(v8::String::New("+++"));
2834 CHECK_EQ(1, exception_hit_count);
2835 CHECK_EQ(1, uncaught_exception_hit_count);
2836 CHECK_EQ(1, message_callback_count);
2837 CHECK_EQ(0, last_js_stack_height); // No JavaScript stack.
2838
2839 // Throws SyntaxError: Unexpected identifier
2840 v8::Script::Compile(v8::String::New("x x"));
2841 CHECK_EQ(2, exception_hit_count);
2842 CHECK_EQ(2, uncaught_exception_hit_count);
2843 CHECK_EQ(2, message_callback_count);
2844 CHECK_EQ(0, last_js_stack_height); // No JavaScript stack.
2845
2846 // Throws SyntaxError: Unexpected end of input
2847 v8::Script::Compile(v8::String::New("eval('+++')"))->Run();
2848 CHECK_EQ(3, exception_hit_count);
2849 CHECK_EQ(3, uncaught_exception_hit_count);
2850 CHECK_EQ(3, message_callback_count);
2851 CHECK_EQ(1, last_js_stack_height);
2852
2853 // Throws SyntaxError: Unexpected identifier
2854 v8::Script::Compile(v8::String::New("eval('x x')"))->Run();
2855 CHECK_EQ(4, exception_hit_count);
2856 CHECK_EQ(4, uncaught_exception_hit_count);
2857 CHECK_EQ(4, message_callback_count);
2858 CHECK_EQ(1, last_js_stack_height);
2859}
2860
2861
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002862TEST(StepWithException) {
2863 v8::HandleScope scope;
2864 DebugLocalContext env;
2865
2866 // Create a function for checking the function when hitting a break point.
2867 frame_function_name = CompileFunction(&env,
2868 frame_function_name_source,
2869 "frame_function_name");
2870
2871 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002872 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002873
2874 // Create functions for testing stepping.
2875 const char* src = "function a() { n(); }; "
2876 "function b() { c(); }; "
2877 "function c() { n(); }; "
2878 "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; "
2879 "function e() { n(); }; "
2880 "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; "
2881 "function g() { h(); }; "
2882 "function h() { x = 1; throw 1; }; ";
2883
2884 // Step through invocation of a.
2885 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
2886 SetBreakPoint(a, 0);
2887 step_action = StepIn;
2888 break_point_hit_count = 0;
2889 expected_step_sequence = "aa";
2890 a->Call(env->Global(), 0, NULL);
2891 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2892
2893 // Step through invocation of b + c.
2894 v8::Local<v8::Function> b = CompileFunction(&env, src, "b");
2895 SetBreakPoint(b, 0);
2896 step_action = StepIn;
2897 break_point_hit_count = 0;
2898 expected_step_sequence = "bcc";
2899 b->Call(env->Global(), 0, NULL);
2900 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2901
2902 // Step through invocation of d + e.
2903 v8::Local<v8::Function> d = CompileFunction(&env, src, "d");
2904 SetBreakPoint(d, 0);
2905 ChangeBreakOnException(false, true);
2906 step_action = StepIn;
2907 break_point_hit_count = 0;
ager@chromium.org32912102009-01-16 10:38:43 +00002908 expected_step_sequence = "dded";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002909 d->Call(env->Global(), 0, NULL);
2910 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2911
2912 // Step through invocation of d + e now with break on caught exceptions.
2913 ChangeBreakOnException(true, true);
2914 step_action = StepIn;
2915 break_point_hit_count = 0;
ager@chromium.org32912102009-01-16 10:38:43 +00002916 expected_step_sequence = "ddeed";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002917 d->Call(env->Global(), 0, NULL);
2918 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2919
2920 // Step through invocation of f + g + h.
2921 v8::Local<v8::Function> f = CompileFunction(&env, src, "f");
2922 SetBreakPoint(f, 0);
2923 ChangeBreakOnException(false, true);
2924 step_action = StepIn;
2925 break_point_hit_count = 0;
ager@chromium.org32912102009-01-16 10:38:43 +00002926 expected_step_sequence = "ffghf";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002927 f->Call(env->Global(), 0, NULL);
2928 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2929
2930 // Step through invocation of f + g + h now with break on caught exceptions.
2931 ChangeBreakOnException(true, true);
2932 step_action = StepIn;
2933 break_point_hit_count = 0;
ager@chromium.org32912102009-01-16 10:38:43 +00002934 expected_step_sequence = "ffghhf";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002935 f->Call(env->Global(), 0, NULL);
2936 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2937
2938 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002939 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002940 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002941}
2942
2943
2944TEST(DebugBreak) {
2945 v8::HandleScope scope;
2946 DebugLocalContext env;
2947
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002948 // This test should be run with option --verify-heap. As --verify-heap is
2949 // only available in debug mode only check for it in that case.
2950#ifdef DEBUG
2951 CHECK(v8::internal::FLAG_verify_heap);
2952#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002953
2954 // Register a debug event listener which sets the break flag and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002955 v8::Debug::SetDebugEventListener(DebugEventBreak);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002956
2957 // Create a function for testing stepping.
2958 const char* src = "function f0() {}"
2959 "function f1(x1) {}"
2960 "function f2(x1,x2) {}"
2961 "function f3(x1,x2,x3) {}";
2962 v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0");
2963 v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1");
2964 v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2");
2965 v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3");
2966
2967 // Call the function to make sure it is compiled.
2968 v8::Handle<v8::Value> argv[] = { v8::Number::New(1),
2969 v8::Number::New(1),
2970 v8::Number::New(1),
2971 v8::Number::New(1) };
2972
2973 // Call all functions to make sure that they are compiled.
2974 f0->Call(env->Global(), 0, NULL);
2975 f1->Call(env->Global(), 0, NULL);
2976 f2->Call(env->Global(), 0, NULL);
2977 f3->Call(env->Global(), 0, NULL);
2978
2979 // Set the debug break flag.
2980 v8::Debug::DebugBreak();
2981
2982 // Call all functions with different argument count.
2983 break_point_hit_count = 0;
2984 for (unsigned int i = 0; i < ARRAY_SIZE(argv); i++) {
2985 f0->Call(env->Global(), i, argv);
2986 f1->Call(env->Global(), i, argv);
2987 f2->Call(env->Global(), i, argv);
2988 f3->Call(env->Global(), i, argv);
2989 }
2990
2991 // One break for each function called.
2992 CHECK_EQ(4 * ARRAY_SIZE(argv), break_point_hit_count);
2993
2994 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002995 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002996 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002997}
2998
2999
3000// Test to ensure that JavaScript code keeps running while the debug break
3001// through the stack limit flag is set but breaks are disabled.
3002TEST(DisableBreak) {
3003 v8::HandleScope scope;
3004 DebugLocalContext env;
3005
3006 // Register a debug event listener which sets the break flag and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003007 v8::Debug::SetDebugEventListener(DebugEventCounter);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003008
3009 // Create a function for testing stepping.
3010 const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}";
3011 v8::Local<v8::Function> f = CompileFunction(&env, src, "f");
3012
3013 // Set the debug break flag.
3014 v8::Debug::DebugBreak();
3015
3016 // Call all functions with different argument count.
3017 break_point_hit_count = 0;
3018 f->Call(env->Global(), 0, NULL);
3019 CHECK_EQ(1, break_point_hit_count);
3020
3021 {
3022 v8::Debug::DebugBreak();
3023 v8::internal::DisableBreak disable_break(true);
3024 f->Call(env->Global(), 0, NULL);
3025 CHECK_EQ(1, break_point_hit_count);
3026 }
3027
3028 f->Call(env->Global(), 0, NULL);
3029 CHECK_EQ(2, break_point_hit_count);
3030
3031 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003032 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003033 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003034}
3035
3036
3037static v8::Handle<v8::Array> NamedEnum(const v8::AccessorInfo&) {
3038 v8::Handle<v8::Array> result = v8::Array::New(3);
3039 result->Set(v8::Integer::New(0), v8::String::New("a"));
3040 result->Set(v8::Integer::New(1), v8::String::New("b"));
3041 result->Set(v8::Integer::New(2), v8::String::New("c"));
3042 return result;
3043}
3044
3045
3046static v8::Handle<v8::Array> IndexedEnum(const v8::AccessorInfo&) {
3047 v8::Handle<v8::Array> result = v8::Array::New(2);
3048 result->Set(v8::Integer::New(0), v8::Number::New(1));
3049 result->Set(v8::Integer::New(1), v8::Number::New(10));
3050 return result;
3051}
3052
3053
3054static v8::Handle<v8::Value> NamedGetter(v8::Local<v8::String> name,
3055 const v8::AccessorInfo& info) {
3056 v8::String::AsciiValue n(name);
3057 if (strcmp(*n, "a") == 0) {
3058 return v8::String::New("AA");
3059 } else if (strcmp(*n, "b") == 0) {
3060 return v8::String::New("BB");
3061 } else if (strcmp(*n, "c") == 0) {
3062 return v8::String::New("CC");
3063 } else {
3064 return v8::Undefined();
3065 }
3066
3067 return name;
3068}
3069
3070
3071static v8::Handle<v8::Value> IndexedGetter(uint32_t index,
3072 const v8::AccessorInfo& info) {
3073 return v8::Number::New(index + 1);
3074}
3075
3076
3077TEST(InterceptorPropertyMirror) {
3078 // Create a V8 environment with debug access.
3079 v8::HandleScope scope;
3080 DebugLocalContext env;
3081 env.ExposeDebug();
3082
3083 // Create object with named interceptor.
3084 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New();
3085 named->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum);
3086 env->Global()->Set(v8::String::New("intercepted_named"),
3087 named->NewInstance());
3088
3089 // Create object with indexed interceptor.
3090 v8::Handle<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New();
3091 indexed->SetIndexedPropertyHandler(IndexedGetter,
3092 NULL,
3093 NULL,
3094 NULL,
3095 IndexedEnum);
3096 env->Global()->Set(v8::String::New("intercepted_indexed"),
3097 indexed->NewInstance());
3098
3099 // Create object with both named and indexed interceptor.
3100 v8::Handle<v8::ObjectTemplate> both = v8::ObjectTemplate::New();
3101 both->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum);
3102 both->SetIndexedPropertyHandler(IndexedGetter, NULL, NULL, NULL, IndexedEnum);
3103 env->Global()->Set(v8::String::New("intercepted_both"), both->NewInstance());
3104
3105 // Get mirrors for the three objects with interceptor.
3106 CompileRun(
3107 "named_mirror = debug.MakeMirror(intercepted_named);"
3108 "indexed_mirror = debug.MakeMirror(intercepted_indexed);"
3109 "both_mirror = debug.MakeMirror(intercepted_both)");
3110 CHECK(CompileRun(
3111 "named_mirror instanceof debug.ObjectMirror")->BooleanValue());
3112 CHECK(CompileRun(
3113 "indexed_mirror instanceof debug.ObjectMirror")->BooleanValue());
3114 CHECK(CompileRun(
3115 "both_mirror instanceof debug.ObjectMirror")->BooleanValue());
3116
3117 // Get the property names from the interceptors
3118 CompileRun(
ager@chromium.org32912102009-01-16 10:38:43 +00003119 "named_names = named_mirror.propertyNames();"
3120 "indexed_names = indexed_mirror.propertyNames();"
3121 "both_names = both_mirror.propertyNames()");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003122 CHECK_EQ(3, CompileRun("named_names.length")->Int32Value());
3123 CHECK_EQ(2, CompileRun("indexed_names.length")->Int32Value());
3124 CHECK_EQ(5, CompileRun("both_names.length")->Int32Value());
3125
3126 // Check the expected number of properties.
3127 const char* source;
ager@chromium.org32912102009-01-16 10:38:43 +00003128 source = "named_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003129 CHECK_EQ(3, CompileRun(source)->Int32Value());
3130
ager@chromium.org32912102009-01-16 10:38:43 +00003131 source = "indexed_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003132 CHECK_EQ(2, CompileRun(source)->Int32Value());
3133
ager@chromium.org32912102009-01-16 10:38:43 +00003134 source = "both_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003135 CHECK_EQ(5, CompileRun(source)->Int32Value());
3136
ager@chromium.org32912102009-01-16 10:38:43 +00003137 // 1 is PropertyKind.Named;
3138 source = "both_mirror.properties(1).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003139 CHECK_EQ(3, CompileRun(source)->Int32Value());
3140
ager@chromium.org32912102009-01-16 10:38:43 +00003141 // 2 is PropertyKind.Indexed;
3142 source = "both_mirror.properties(2).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003143 CHECK_EQ(2, CompileRun(source)->Int32Value());
3144
ager@chromium.org32912102009-01-16 10:38:43 +00003145 // 3 is PropertyKind.Named | PropertyKind.Indexed;
3146 source = "both_mirror.properties(3).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003147 CHECK_EQ(5, CompileRun(source)->Int32Value());
3148
ager@chromium.org32912102009-01-16 10:38:43 +00003149 // Get the interceptor properties for the object with only named interceptor.
3150 CompileRun("named_values = named_mirror.properties()");
3151
3152 // Check that the properties are interceptor properties.
3153 for (int i = 0; i < 3; i++) {
3154 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
3155 OS::SNPrintF(buffer,
3156 "named_values[%d] instanceof debug.PropertyMirror", i);
3157 CHECK(CompileRun(buffer.start())->BooleanValue());
3158
3159 // 4 is PropertyType.Interceptor
3160 OS::SNPrintF(buffer, "named_values[%d].propertyType()", i);
3161 CHECK_EQ(4, CompileRun(buffer.start())->Int32Value());
3162
3163 OS::SNPrintF(buffer, "named_values[%d].isNative()", i);
3164 CHECK(CompileRun(buffer.start())->BooleanValue());
3165 }
3166
3167 // Get the interceptor properties for the object with only indexed
3168 // interceptor.
3169 CompileRun("indexed_values = indexed_mirror.properties()");
3170
3171 // Check that the properties are interceptor properties.
3172 for (int i = 0; i < 2; i++) {
3173 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
3174 OS::SNPrintF(buffer,
3175 "indexed_values[%d] instanceof debug.PropertyMirror", i);
3176 CHECK(CompileRun(buffer.start())->BooleanValue());
3177 }
3178
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003179 // Get the interceptor properties for the object with both types of
3180 // interceptors.
ager@chromium.org32912102009-01-16 10:38:43 +00003181 CompileRun("both_values = both_mirror.properties()");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003182
ager@chromium.org32912102009-01-16 10:38:43 +00003183 // Check that the properties are interceptor properties.
3184 for (int i = 0; i < 5; i++) {
3185 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
3186 OS::SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i);
3187 CHECK(CompileRun(buffer.start())->BooleanValue());
3188 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003189
3190 // Check the property names.
3191 source = "both_values[0].name() == 'a'";
3192 CHECK(CompileRun(source)->BooleanValue());
3193
3194 source = "both_values[1].name() == 'b'";
3195 CHECK(CompileRun(source)->BooleanValue());
3196
3197 source = "both_values[2].name() == 'c'";
3198 CHECK(CompileRun(source)->BooleanValue());
3199
3200 source = "both_values[3].name() == 1";
3201 CHECK(CompileRun(source)->BooleanValue());
3202
3203 source = "both_values[4].name() == 10";
3204 CHECK(CompileRun(source)->BooleanValue());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003205}
3206
3207
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003208TEST(HiddenPrototypePropertyMirror) {
3209 // Create a V8 environment with debug access.
3210 v8::HandleScope scope;
3211 DebugLocalContext env;
3212 env.ExposeDebug();
3213
3214 v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New();
3215 t0->InstanceTemplate()->Set(v8::String::New("x"), v8::Number::New(0));
3216 v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New();
3217 t1->SetHiddenPrototype(true);
3218 t1->InstanceTemplate()->Set(v8::String::New("y"), v8::Number::New(1));
3219 v8::Handle<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New();
3220 t2->SetHiddenPrototype(true);
3221 t2->InstanceTemplate()->Set(v8::String::New("z"), v8::Number::New(2));
3222 v8::Handle<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New();
3223 t3->InstanceTemplate()->Set(v8::String::New("u"), v8::Number::New(3));
3224
3225 // Create object and set them on the global object.
3226 v8::Handle<v8::Object> o0 = t0->GetFunction()->NewInstance();
3227 env->Global()->Set(v8::String::New("o0"), o0);
3228 v8::Handle<v8::Object> o1 = t1->GetFunction()->NewInstance();
3229 env->Global()->Set(v8::String::New("o1"), o1);
3230 v8::Handle<v8::Object> o2 = t2->GetFunction()->NewInstance();
3231 env->Global()->Set(v8::String::New("o2"), o2);
3232 v8::Handle<v8::Object> o3 = t3->GetFunction()->NewInstance();
3233 env->Global()->Set(v8::String::New("o3"), o3);
3234
3235 // Get mirrors for the four objects.
3236 CompileRun(
3237 "o0_mirror = debug.MakeMirror(o0);"
3238 "o1_mirror = debug.MakeMirror(o1);"
3239 "o2_mirror = debug.MakeMirror(o2);"
3240 "o3_mirror = debug.MakeMirror(o3)");
3241 CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror")->BooleanValue());
3242 CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror")->BooleanValue());
3243 CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror")->BooleanValue());
3244 CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror")->BooleanValue());
3245
3246 // Check that each object has one property.
3247 CHECK_EQ(1, CompileRun(
3248 "o0_mirror.propertyNames().length")->Int32Value());
3249 CHECK_EQ(1, CompileRun(
3250 "o1_mirror.propertyNames().length")->Int32Value());
3251 CHECK_EQ(1, CompileRun(
3252 "o2_mirror.propertyNames().length")->Int32Value());
3253 CHECK_EQ(1, CompileRun(
3254 "o3_mirror.propertyNames().length")->Int32Value());
3255
3256 // Set o1 as prototype for o0. o1 has the hidden prototype flag so all
3257 // properties on o1 should be seen on o0.
3258 o0->Set(v8::String::New("__proto__"), o1);
3259 CHECK_EQ(2, CompileRun(
3260 "o0_mirror.propertyNames().length")->Int32Value());
3261 CHECK_EQ(0, CompileRun(
3262 "o0_mirror.property('x').value().value()")->Int32Value());
3263 CHECK_EQ(1, CompileRun(
3264 "o0_mirror.property('y').value().value()")->Int32Value());
3265
3266 // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden
3267 // prototype flag. o2 also has the hidden prototype flag so all properties
3268 // on o2 should be seen on o0 as well as properties on o1.
3269 o0->Set(v8::String::New("__proto__"), o2);
3270 CHECK_EQ(3, CompileRun(
3271 "o0_mirror.propertyNames().length")->Int32Value());
3272 CHECK_EQ(0, CompileRun(
3273 "o0_mirror.property('x').value().value()")->Int32Value());
3274 CHECK_EQ(1, CompileRun(
3275 "o0_mirror.property('y').value().value()")->Int32Value());
3276 CHECK_EQ(2, CompileRun(
3277 "o0_mirror.property('z').value().value()")->Int32Value());
3278
3279 // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and
3280 // o2 has the hidden prototype flag. o3 does not have the hidden prototype
3281 // flag so properties on o3 should not be seen on o0 whereas the properties
3282 // from o1 and o2 should still be seen on o0.
3283 // Final prototype chain: o0 -> o1 -> o2 -> o3
3284 // Hidden prototypes: ^^ ^^
3285 o0->Set(v8::String::New("__proto__"), o3);
3286 CHECK_EQ(3, CompileRun(
3287 "o0_mirror.propertyNames().length")->Int32Value());
3288 CHECK_EQ(1, CompileRun(
3289 "o3_mirror.propertyNames().length")->Int32Value());
3290 CHECK_EQ(0, CompileRun(
3291 "o0_mirror.property('x').value().value()")->Int32Value());
3292 CHECK_EQ(1, CompileRun(
3293 "o0_mirror.property('y').value().value()")->Int32Value());
3294 CHECK_EQ(2, CompileRun(
3295 "o0_mirror.property('z').value().value()")->Int32Value());
3296 CHECK(CompileRun("o0_mirror.property('u').isUndefined()")->BooleanValue());
3297
3298 // The prototype (__proto__) for o0 should be o3 as o1 and o2 are hidden.
3299 CHECK(CompileRun("o0_mirror.protoObject() == o3_mirror")->BooleanValue());
3300}
3301
3302
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003303// Multithreaded tests of JSON debugger protocol
3304
3305// Support classes
3306
3307// Copies a C string to a 16-bit string. Does not check for buffer overflow.
3308// Does not use the V8 engine to convert strings, so it can be used
3309// in any thread. Returns the length of the string.
3310int AsciiToUtf16(const char* input_buffer, uint16_t* output_buffer) {
3311 int i;
3312 for (i = 0; input_buffer[i] != '\0'; ++i) {
3313 // ASCII does not use chars > 127, but be careful anyway.
3314 output_buffer[i] = static_cast<unsigned char>(input_buffer[i]);
3315 }
3316 output_buffer[i] = 0;
3317 return i;
3318}
3319
3320// Copies a 16-bit string to a C string by dropping the high byte of
3321// each character. Does not check for buffer overflow.
3322// Can be used in any thread. Requires string length as an input.
3323int Utf16ToAscii(const uint16_t* input_buffer, int length,
3324 char* output_buffer) {
3325 for (int i = 0; i < length; ++i) {
3326 output_buffer[i] = static_cast<char>(input_buffer[i]);
3327 }
3328 output_buffer[length] = '\0';
3329 return length;
3330}
3331
3332// Provides synchronization between k threads, where k is an input to the
3333// constructor. The Wait() call blocks a thread until it is called for the
3334// k'th time, then all calls return. Each ThreadBarrier object can only
3335// be used once.
3336class ThreadBarrier {
3337 public:
3338 explicit ThreadBarrier(int num_threads);
3339 ~ThreadBarrier();
3340 void Wait();
3341 private:
3342 int num_threads_;
3343 int num_blocked_;
3344 v8::internal::Mutex* lock_;
3345 v8::internal::Semaphore* sem_;
3346 bool invalid_;
3347};
3348
3349ThreadBarrier::ThreadBarrier(int num_threads)
3350 : num_threads_(num_threads), num_blocked_(0) {
3351 lock_ = OS::CreateMutex();
3352 sem_ = OS::CreateSemaphore(0);
3353 invalid_ = false; // A barrier may only be used once. Then it is invalid.
3354}
3355
3356// Do not call, due to race condition with Wait().
3357// Could be resolved with Pthread condition variables.
3358ThreadBarrier::~ThreadBarrier() {
3359 lock_->Lock();
3360 delete lock_;
3361 delete sem_;
3362}
3363
3364void ThreadBarrier::Wait() {
3365 lock_->Lock();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003366 CHECK(!invalid_);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003367 if (num_blocked_ == num_threads_ - 1) {
3368 // Signal and unblock all waiting threads.
3369 for (int i = 0; i < num_threads_ - 1; ++i) {
3370 sem_->Signal();
3371 }
3372 invalid_ = true;
3373 printf("BARRIER\n\n");
3374 fflush(stdout);
3375 lock_->Unlock();
3376 } else { // Wait for the semaphore.
3377 ++num_blocked_;
3378 lock_->Unlock(); // Potential race condition with destructor because
3379 sem_->Wait(); // these two lines are not atomic.
3380 }
3381}
3382
3383// A set containing enough barriers and semaphores for any of the tests.
3384class Barriers {
3385 public:
3386 Barriers();
3387 void Initialize();
3388 ThreadBarrier barrier_1;
3389 ThreadBarrier barrier_2;
3390 ThreadBarrier barrier_3;
3391 ThreadBarrier barrier_4;
3392 ThreadBarrier barrier_5;
3393 v8::internal::Semaphore* semaphore_1;
3394 v8::internal::Semaphore* semaphore_2;
3395};
3396
3397Barriers::Barriers() : barrier_1(2), barrier_2(2),
3398 barrier_3(2), barrier_4(2), barrier_5(2) {}
3399
3400void Barriers::Initialize() {
3401 semaphore_1 = OS::CreateSemaphore(0);
3402 semaphore_2 = OS::CreateSemaphore(0);
3403}
3404
3405
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003406// We match parts of the message to decide if it is a break message.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003407bool IsBreakEventMessage(char *message) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003408 const char* type_event = "\"type\":\"event\"";
3409 const char* event_break = "\"event\":\"break\"";
3410 // Does the message contain both type:event and event:break?
3411 return strstr(message, type_event) != NULL &&
3412 strstr(message, event_break) != NULL;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003413}
3414
3415
3416/* Test MessageQueues */
3417/* Tests the message queues that hold debugger commands and
3418 * response messages to the debugger. Fills queues and makes
3419 * them grow.
3420 */
3421Barriers message_queue_barriers;
3422
3423// This is the debugger thread, that executes no v8 calls except
3424// placing JSON debugger commands in the queue.
3425class MessageQueueDebuggerThread : public v8::internal::Thread {
3426 public:
3427 void Run();
3428};
3429
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003430static void MessageHandler(const uint16_t* message, int length,
3431 v8::Debug::ClientData* client_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003432 static char print_buffer[1000];
3433 Utf16ToAscii(message, length, print_buffer);
3434 if (IsBreakEventMessage(print_buffer)) {
3435 // Lets test script wait until break occurs to send commands.
3436 // Signals when a break is reported.
3437 message_queue_barriers.semaphore_2->Signal();
3438 }
3439 // Allow message handler to block on a semaphore, to test queueing of
3440 // messages while blocked.
3441 message_queue_barriers.semaphore_1->Wait();
3442 printf("%s\n", print_buffer);
3443 fflush(stdout);
3444}
3445
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003446void MessageQueueDebuggerThread::Run() {
3447 const int kBufferSize = 1000;
3448 uint16_t buffer_1[kBufferSize];
3449 uint16_t buffer_2[kBufferSize];
3450 const char* command_1 =
3451 "{\"seq\":117,"
3452 "\"type\":\"request\","
3453 "\"command\":\"evaluate\","
3454 "\"arguments\":{\"expression\":\"1+2\"}}";
3455 const char* command_2 =
3456 "{\"seq\":118,"
3457 "\"type\":\"request\","
3458 "\"command\":\"evaluate\","
3459 "\"arguments\":{\"expression\":\"1+a\"}}";
3460 const char* command_3 =
3461 "{\"seq\":119,"
3462 "\"type\":\"request\","
3463 "\"command\":\"evaluate\","
3464 "\"arguments\":{\"expression\":\"c.d * b\"}}";
3465 const char* command_continue =
3466 "{\"seq\":106,"
3467 "\"type\":\"request\","
3468 "\"command\":\"continue\"}";
3469 const char* command_single_step =
3470 "{\"seq\":107,"
3471 "\"type\":\"request\","
3472 "\"command\":\"continue\","
3473 "\"arguments\":{\"stepaction\":\"next\"}}";
3474
3475 /* Interleaved sequence of actions by the two threads:*/
3476 // Main thread compiles and runs source_1
3477 message_queue_barriers.barrier_1.Wait();
3478 // Post 6 commands, filling the command queue and making it expand.
3479 // These calls return immediately, but the commands stay on the queue
3480 // until the execution of source_2.
3481 // Note: AsciiToUtf16 executes before SendCommand, so command is copied
3482 // to buffer before buffer is sent to SendCommand.
3483 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1));
3484 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2));
3485 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
3486 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
3487 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003488 message_queue_barriers.barrier_2.Wait();
3489 // Main thread compiles and runs source_2.
3490 // Queued commands are executed at the start of compilation of source_2.
3491 message_queue_barriers.barrier_3.Wait();
3492 // Free the message handler to process all the messages from the queue.
3493 for (int i = 0; i < 20 ; ++i) {
3494 message_queue_barriers.semaphore_1->Signal();
3495 }
3496 // Main thread compiles and runs source_3.
3497 // source_3 includes a debugger statement, which causes a break event.
3498 // Wait on break event from hitting "debugger" statement
3499 message_queue_barriers.semaphore_2->Wait();
3500 // These should execute after the "debugger" statement in source_2
3501 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_single_step, buffer_2));
3502 // Wait on break event after a single step executes.
3503 message_queue_barriers.semaphore_2->Wait();
3504 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_2, buffer_1));
3505 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_continue, buffer_2));
3506 // Main thread continues running source_3 to end, waits for this thread.
3507}
3508
3509MessageQueueDebuggerThread message_queue_debugger_thread;
3510
3511// This thread runs the v8 engine.
3512TEST(MessageQueues) {
3513 // Create a V8 environment
3514 v8::HandleScope scope;
3515 DebugLocalContext env;
3516 message_queue_barriers.Initialize();
3517 v8::Debug::SetMessageHandler(MessageHandler);
3518 message_queue_debugger_thread.Start();
3519
3520 const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;";
3521 const char* source_2 = "e = 17;";
3522 const char* source_3 = "a = 4; debugger; a = 5; a = 6; a = 7;";
3523
3524 // See MessageQueueDebuggerThread::Run for interleaved sequence of
3525 // API calls and events in the two threads.
3526 CompileRun(source_1);
3527 message_queue_barriers.barrier_1.Wait();
3528 message_queue_barriers.barrier_2.Wait();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003529 CompileRun(source_2);
3530 message_queue_barriers.barrier_3.Wait();
3531 CompileRun(source_3);
3532 message_queue_debugger_thread.Join();
3533 fflush(stdout);
3534}
3535
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003536
3537class TestClientData : public v8::Debug::ClientData {
3538 public:
3539 TestClientData() {
3540 constructor_call_counter++;
3541 }
3542 virtual ~TestClientData() {
3543 destructor_call_counter++;
3544 }
3545
3546 static void ResetCounters() {
3547 constructor_call_counter = 0;
3548 destructor_call_counter = 0;
3549 }
3550
3551 static int constructor_call_counter;
3552 static int destructor_call_counter;
3553};
3554
3555int TestClientData::constructor_call_counter = 0;
3556int TestClientData::destructor_call_counter = 0;
3557
3558
3559// Tests that MessageQueue doesn't destroy client data when expands and
3560// does destroy when it dies.
3561TEST(MessageQueueExpandAndDestroy) {
3562 TestClientData::ResetCounters();
3563 { // Create a scope for the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003564 CommandMessageQueue queue(1);
3565 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003566 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003567 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003568 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003569 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003570 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003571 CHECK_EQ(0, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003572 queue.Get().Dispose();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003573 CHECK_EQ(1, TestClientData::destructor_call_counter);
3574 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003575 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003576 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003577 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003578 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003579 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003580 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
3581 new TestClientData()));
3582 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
3583 new TestClientData()));
3584 CHECK_EQ(1, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003585 queue.Get().Dispose();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003586 CHECK_EQ(2, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003587 }
3588 // All the client data should be destroyed when the queue is destroyed.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003589 CHECK_EQ(TestClientData::destructor_call_counter,
3590 TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003591}
3592
3593
3594static int handled_client_data_instances_count = 0;
3595static void MessageHandlerCountingClientData(
3596 const uint16_t* message,
3597 int length,
3598 v8::Debug::ClientData* client_data) {
3599 if (client_data) {
3600 handled_client_data_instances_count++;
3601 }
3602}
3603
3604
3605// Tests that all client data passed to the debugger are sent to the handler.
3606TEST(SendClientDataToHandler) {
3607 // Create a V8 environment
3608 v8::HandleScope scope;
3609 DebugLocalContext env;
3610 TestClientData::ResetCounters();
3611 handled_client_data_instances_count = 0;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003612 v8::Debug::SetMessageHandler(MessageHandlerCountingClientData);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003613 const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5; debugger;";
3614 const int kBufferSize = 1000;
3615 uint16_t buffer[kBufferSize];
3616 const char* command_1 =
3617 "{\"seq\":117,"
3618 "\"type\":\"request\","
3619 "\"command\":\"evaluate\","
3620 "\"arguments\":{\"expression\":\"1+2\"}}";
3621 const char* command_2 =
3622 "{\"seq\":118,"
3623 "\"type\":\"request\","
3624 "\"command\":\"evaluate\","
3625 "\"arguments\":{\"expression\":\"1+a\"}}";
3626 const char* command_continue =
3627 "{\"seq\":106,"
3628 "\"type\":\"request\","
3629 "\"command\":\"continue\"}";
3630
3631 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer),
3632 new TestClientData());
3633 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), NULL);
3634 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer),
3635 new TestClientData());
3636 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer),
3637 new TestClientData());
3638 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer));
3639 CompileRun(source_1);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003640 CHECK_EQ(3, TestClientData::constructor_call_counter);
3641 CHECK_EQ(TestClientData::constructor_call_counter,
3642 handled_client_data_instances_count);
3643 CHECK_EQ(TestClientData::constructor_call_counter,
3644 TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003645}
3646
3647
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003648/* Test ThreadedDebugging */
3649/* This test interrupts a running infinite loop that is
3650 * occupying the v8 thread by a break command from the
3651 * debugger thread. It then changes the value of a
3652 * global object, to make the loop terminate.
3653 */
3654
3655Barriers threaded_debugging_barriers;
3656
3657class V8Thread : public v8::internal::Thread {
3658 public:
3659 void Run();
3660};
3661
3662class DebuggerThread : public v8::internal::Thread {
3663 public:
3664 void Run();
3665};
3666
3667
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003668static v8::Handle<v8::Value> ThreadedAtBarrier1(const v8::Arguments& args) {
3669 threaded_debugging_barriers.barrier_1.Wait();
3670 return v8::Undefined();
3671}
3672
3673
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003674static void ThreadedMessageHandler(const uint16_t* message, int length,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003675 v8::Debug::ClientData* client_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003676 static char print_buffer[1000];
3677 Utf16ToAscii(message, length, print_buffer);
3678 if (IsBreakEventMessage(print_buffer)) {
3679 threaded_debugging_barriers.barrier_2.Wait();
3680 }
3681 printf("%s\n", print_buffer);
3682 fflush(stdout);
3683}
3684
3685
3686void V8Thread::Run() {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003687 const char* source =
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003688 "flag = true;\n"
3689 "function bar( new_value ) {\n"
3690 " flag = new_value;\n"
3691 " return \"Return from bar(\" + new_value + \")\";\n"
3692 "}\n"
3693 "\n"
3694 "function foo() {\n"
3695 " var x = 1;\n"
3696 " while ( flag == true ) {\n"
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003697 " if ( x == 1 ) {\n"
3698 " ThreadedAtBarrier1();\n"
3699 " }\n"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003700 " x = x + 1;\n"
3701 " }\n"
3702 "}\n"
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003703 "\n"
3704 "foo();\n";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003705
3706 v8::HandleScope scope;
3707 DebugLocalContext env;
3708 v8::Debug::SetMessageHandler(&ThreadedMessageHandler);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003709 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
3710 global_template->Set(v8::String::New("ThreadedAtBarrier1"),
3711 v8::FunctionTemplate::New(ThreadedAtBarrier1));
3712 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template);
3713 v8::Context::Scope context_scope(context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003714
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003715 CompileRun(source);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003716}
3717
3718void DebuggerThread::Run() {
3719 const int kBufSize = 1000;
3720 uint16_t buffer[kBufSize];
3721
3722 const char* command_1 = "{\"seq\":102,"
3723 "\"type\":\"request\","
3724 "\"command\":\"evaluate\","
3725 "\"arguments\":{\"expression\":\"bar(false)\"}}";
3726 const char* command_2 = "{\"seq\":103,"
3727 "\"type\":\"request\","
3728 "\"command\":\"continue\"}";
3729
3730 threaded_debugging_barriers.barrier_1.Wait();
3731 v8::Debug::DebugBreak();
3732 threaded_debugging_barriers.barrier_2.Wait();
3733 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
3734 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
3735}
3736
3737DebuggerThread debugger_thread;
3738V8Thread v8_thread;
3739
3740TEST(ThreadedDebugging) {
3741 // Create a V8 environment
3742 threaded_debugging_barriers.Initialize();
3743
3744 v8_thread.Start();
3745 debugger_thread.Start();
3746
3747 v8_thread.Join();
3748 debugger_thread.Join();
3749}
3750
3751/* Test RecursiveBreakpoints */
3752/* In this test, the debugger evaluates a function with a breakpoint, after
3753 * hitting a breakpoint in another function. We do this with both values
3754 * of the flag enabling recursive breakpoints, and verify that the second
3755 * breakpoint is hit when enabled, and missed when disabled.
3756 */
3757
3758class BreakpointsV8Thread : public v8::internal::Thread {
3759 public:
3760 void Run();
3761};
3762
3763class BreakpointsDebuggerThread : public v8::internal::Thread {
3764 public:
3765 void Run();
3766};
3767
3768
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003769Barriers* breakpoints_barriers;
3770
3771static void BreakpointsMessageHandler(const uint16_t* message,
3772 int length,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003773 v8::Debug::ClientData* client_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003774 static char print_buffer[1000];
3775 Utf16ToAscii(message, length, print_buffer);
3776 printf("%s\n", print_buffer);
3777 fflush(stdout);
3778
3779 // Is break_template a prefix of the message?
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003780 if (IsBreakEventMessage(print_buffer)) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003781 breakpoints_barriers->semaphore_1->Signal();
3782 }
3783}
3784
3785
3786void BreakpointsV8Thread::Run() {
3787 const char* source_1 = "var y_global = 3;\n"
3788 "function cat( new_value ) {\n"
3789 " var x = new_value;\n"
3790 " y_global = 4;\n"
3791 " x = 3 * x + 1;\n"
3792 " y_global = 5;\n"
3793 " return x;\n"
3794 "}\n"
3795 "\n"
3796 "function dog() {\n"
3797 " var x = 1;\n"
3798 " x = y_global;"
3799 " var z = 3;"
3800 " x += 100;\n"
3801 " return x;\n"
3802 "}\n"
3803 "\n";
3804 const char* source_2 = "cat(17);\n"
3805 "cat(19);\n";
3806
3807 v8::HandleScope scope;
3808 DebugLocalContext env;
3809 v8::Debug::SetMessageHandler(&BreakpointsMessageHandler);
3810
3811 CompileRun(source_1);
3812 breakpoints_barriers->barrier_1.Wait();
3813 breakpoints_barriers->barrier_2.Wait();
3814 CompileRun(source_2);
3815}
3816
3817
3818void BreakpointsDebuggerThread::Run() {
3819 const int kBufSize = 1000;
3820 uint16_t buffer[kBufSize];
3821
3822 const char* command_1 = "{\"seq\":101,"
3823 "\"type\":\"request\","
3824 "\"command\":\"setbreakpoint\","
3825 "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}";
3826 const char* command_2 = "{\"seq\":102,"
3827 "\"type\":\"request\","
3828 "\"command\":\"setbreakpoint\","
3829 "\"arguments\":{\"type\":\"function\",\"target\":\"dog\",\"line\":3}}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003830 const char* command_3 = "{\"seq\":104,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003831 "\"type\":\"request\","
3832 "\"command\":\"evaluate\","
3833 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false}}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003834 const char* command_4 = "{\"seq\":105,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003835 "\"type\":\"request\","
3836 "\"command\":\"evaluate\","
3837 "\"arguments\":{\"expression\":\"x\",\"disable_break\":true}}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003838 const char* command_5 = "{\"seq\":106,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003839 "\"type\":\"request\","
3840 "\"command\":\"continue\"}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003841 const char* command_6 = "{\"seq\":107,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003842 "\"type\":\"request\","
3843 "\"command\":\"continue\"}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003844 const char* command_7 = "{\"seq\":108,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003845 "\"type\":\"request\","
3846 "\"command\":\"evaluate\","
3847 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true}}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003848 const char* command_8 = "{\"seq\":109,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003849 "\"type\":\"request\","
3850 "\"command\":\"continue\"}";
3851
3852
3853 // v8 thread initializes, runs source_1
3854 breakpoints_barriers->barrier_1.Wait();
3855 // 1:Set breakpoint in cat().
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003856 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
3857 // 2:Set breakpoint in dog()
3858 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003859 breakpoints_barriers->barrier_2.Wait();
3860 // v8 thread starts compiling source_2.
3861 // Automatic break happens, to run queued commands
3862 // breakpoints_barriers->semaphore_1->Wait();
3863 // Commands 1 through 3 run, thread continues.
3864 // v8 thread runs source_2 to breakpoint in cat().
3865 // message callback receives break event.
3866 breakpoints_barriers->semaphore_1->Wait();
3867 // 4:Evaluate dog() (which has a breakpoint).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003868 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_3, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003869 // v8 thread hits breakpoint in dog()
3870 breakpoints_barriers->semaphore_1->Wait(); // wait for break event
3871 // 5:Evaluate x
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003872 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_4, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003873 // 6:Continue evaluation of dog()
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003874 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_5, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003875 // dog() finishes.
3876 // 7:Continue evaluation of source_2, finish cat(17), hit breakpoint
3877 // in cat(19).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003878 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_6, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003879 // message callback gets break event
3880 breakpoints_barriers->semaphore_1->Wait(); // wait for break event
3881 // 8: Evaluate dog() with breaks disabled
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003882 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_7, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003883 // 9: Continue evaluation of source2, reach end.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003884 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_8, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003885}
3886
3887BreakpointsDebuggerThread breakpoints_debugger_thread;
3888BreakpointsV8Thread breakpoints_v8_thread;
3889
3890TEST(RecursiveBreakpoints) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003891 i::FLAG_debugger_auto_break = true;
3892
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003893 // Create a V8 environment
3894 Barriers stack_allocated_breakpoints_barriers;
3895 stack_allocated_breakpoints_barriers.Initialize();
3896 breakpoints_barriers = &stack_allocated_breakpoints_barriers;
3897
3898 breakpoints_v8_thread.Start();
3899 breakpoints_debugger_thread.Start();
3900
3901 breakpoints_v8_thread.Join();
3902 breakpoints_debugger_thread.Join();
3903}
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003904
3905
3906static void DummyDebugEventListener(v8::DebugEvent event,
3907 v8::Handle<v8::Object> exec_state,
3908 v8::Handle<v8::Object> event_data,
3909 v8::Handle<v8::Value> data) {
3910}
3911
3912
iposva@chromium.org245aa852009-02-10 00:49:54 +00003913TEST(SetDebugEventListenerOnUninitializedVM) {
3914 v8::Debug::SetDebugEventListener(DummyDebugEventListener);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003915}
3916
3917
3918static void DummyMessageHandler(const uint16_t* message,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003919 int length,
3920 v8::Debug::ClientData* client_data) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003921}
3922
3923
3924TEST(SetMessageHandlerOnUninitializedVM) {
3925 v8::Debug::SetMessageHandler(DummyMessageHandler);
3926}
3927
3928
3929TEST(DebugBreakOnUninitializedVM) {
3930 v8::Debug::DebugBreak();
3931}
3932
3933
3934TEST(SendCommandToUninitializedVM) {
3935 const char* dummy_command = "{}";
3936 uint16_t dummy_buffer[80];
3937 int dummy_length = AsciiToUtf16(dummy_command, dummy_buffer);
3938 v8::Debug::SendCommand(dummy_buffer, dummy_length);
3939}
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003940
3941
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003942// Source for a JavaScript function which returns the data parameter of a
3943// function called in the context of the debugger. If no data parameter is
3944// passed it throws an exception.
3945static const char* debugger_call_with_data_source =
3946 "function debugger_call_with_data(exec_state, data) {"
3947 " if (data) return data;"
3948 " throw 'No data!'"
3949 "}";
3950v8::Handle<v8::Function> debugger_call_with_data;
3951
3952
3953// Source for a JavaScript function which returns the data parameter of a
3954// function called in the context of the debugger. If no data parameter is
3955// passed it throws an exception.
3956static const char* debugger_call_with_closure_source =
3957 "var x = 3;"
3958 "function (exec_state) {"
3959 " if (exec_state.y) return x - 1;"
3960 " exec_state.y = x;"
3961 " return exec_state.y"
3962 "}";
3963v8::Handle<v8::Function> debugger_call_with_closure;
3964
3965// Function to retrieve the number of JavaScript frames by calling a JavaScript
3966// in the debugger.
3967static v8::Handle<v8::Value> CheckFrameCount(const v8::Arguments& args) {
3968 CHECK(v8::Debug::Call(frame_count)->IsNumber());
3969 CHECK_EQ(args[0]->Int32Value(),
3970 v8::Debug::Call(frame_count)->Int32Value());
3971 return v8::Undefined();
3972}
3973
3974
3975// Function to retrieve the source line of the top JavaScript frame by calling a
3976// JavaScript function in the debugger.
3977static v8::Handle<v8::Value> CheckSourceLine(const v8::Arguments& args) {
3978 CHECK(v8::Debug::Call(frame_source_line)->IsNumber());
3979 CHECK_EQ(args[0]->Int32Value(),
3980 v8::Debug::Call(frame_source_line)->Int32Value());
3981 return v8::Undefined();
3982}
3983
3984
3985// Function to test passing an additional parameter to a JavaScript function
3986// called in the debugger. It also tests that functions called in the debugger
3987// can throw exceptions.
3988static v8::Handle<v8::Value> CheckDataParameter(const v8::Arguments& args) {
3989 v8::Handle<v8::String> data = v8::String::New("Test");
3990 CHECK(v8::Debug::Call(debugger_call_with_data, data)->IsString());
3991
3992 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty());
3993 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty());
3994
3995 v8::TryCatch catcher;
3996 v8::Debug::Call(debugger_call_with_data);
3997 CHECK(catcher.HasCaught());
3998 CHECK(catcher.Exception()->IsString());
3999
4000 return v8::Undefined();
4001}
4002
4003
4004// Function to test using a JavaScript with closure in the debugger.
4005static v8::Handle<v8::Value> CheckClosure(const v8::Arguments& args) {
4006 CHECK(v8::Debug::Call(debugger_call_with_closure)->IsNumber());
4007 CHECK_EQ(3, v8::Debug::Call(debugger_call_with_closure)->Int32Value());
4008 return v8::Undefined();
4009}
4010
4011
4012// Test functions called through the debugger.
4013TEST(CallFunctionInDebugger) {
4014 // Create and enter a context with the functions CheckFrameCount,
4015 // CheckSourceLine and CheckDataParameter installed.
4016 v8::HandleScope scope;
4017 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
4018 global_template->Set(v8::String::New("CheckFrameCount"),
4019 v8::FunctionTemplate::New(CheckFrameCount));
4020 global_template->Set(v8::String::New("CheckSourceLine"),
4021 v8::FunctionTemplate::New(CheckSourceLine));
4022 global_template->Set(v8::String::New("CheckDataParameter"),
4023 v8::FunctionTemplate::New(CheckDataParameter));
4024 global_template->Set(v8::String::New("CheckClosure"),
4025 v8::FunctionTemplate::New(CheckClosure));
4026 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template);
4027 v8::Context::Scope context_scope(context);
4028
4029 // Compile a function for checking the number of JavaScript frames.
4030 v8::Script::Compile(v8::String::New(frame_count_source))->Run();
4031 frame_count = v8::Local<v8::Function>::Cast(
4032 context->Global()->Get(v8::String::New("frame_count")));
4033
4034 // Compile a function for returning the source line for the top frame.
4035 v8::Script::Compile(v8::String::New(frame_source_line_source))->Run();
4036 frame_source_line = v8::Local<v8::Function>::Cast(
4037 context->Global()->Get(v8::String::New("frame_source_line")));
4038
4039 // Compile a function returning the data parameter.
4040 v8::Script::Compile(v8::String::New(debugger_call_with_data_source))->Run();
4041 debugger_call_with_data = v8::Local<v8::Function>::Cast(
4042 context->Global()->Get(v8::String::New("debugger_call_with_data")));
4043
4044 // Compile a function capturing closure.
4045 debugger_call_with_closure = v8::Local<v8::Function>::Cast(
4046 v8::Script::Compile(
4047 v8::String::New(debugger_call_with_closure_source))->Run());
4048
4049 // Calling a function through the debugger returns undefined if there are no
4050 // JavaScript frames.
4051 CHECK(v8::Debug::Call(frame_count)->IsUndefined());
4052 CHECK(v8::Debug::Call(frame_source_line)->IsUndefined());
4053 CHECK(v8::Debug::Call(debugger_call_with_data)->IsUndefined());
4054
4055 // Test that the number of frames can be retrieved.
4056 v8::Script::Compile(v8::String::New("CheckFrameCount(1)"))->Run();
4057 v8::Script::Compile(v8::String::New("function f() {"
4058 " CheckFrameCount(2);"
4059 "}; f()"))->Run();
4060
4061 // Test that the source line can be retrieved.
4062 v8::Script::Compile(v8::String::New("CheckSourceLine(0)"))->Run();
4063 v8::Script::Compile(v8::String::New("function f() {\n"
4064 " CheckSourceLine(1)\n"
4065 " CheckSourceLine(2)\n"
4066 " CheckSourceLine(3)\n"
4067 "}; f()"))->Run();
4068
4069 // Test that a parameter can be passed to a function called in the debugger.
4070 v8::Script::Compile(v8::String::New("CheckDataParameter()"))->Run();
4071
4072 // Test that a function with closure can be run in the debugger.
4073 v8::Script::Compile(v8::String::New("CheckClosure()"))->Run();
ager@chromium.org3a6061e2009-03-12 14:24:36 +00004074
4075
4076 // Test that the source line is correct when there is a line offset.
4077 v8::ScriptOrigin origin(v8::String::New("test"),
4078 v8::Integer::New(7));
4079 v8::Script::Compile(v8::String::New("CheckSourceLine(7)"), &origin)->Run();
4080 v8::Script::Compile(v8::String::New("function f() {\n"
4081 " CheckSourceLine(8)\n"
4082 " CheckSourceLine(9)\n"
4083 " CheckSourceLine(10)\n"
4084 "}; f()"), &origin)->Run();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00004085}
ager@chromium.org381abbb2009-02-25 13:23:22 +00004086
4087
4088// Test that clearing the debug event listener actually clears all break points
4089// and related information.
4090TEST(DebuggerUnload) {
4091 v8::HandleScope scope;
4092 DebugLocalContext env;
4093
4094 // Check debugger is unloaded before it is used.
4095 CheckDebuggerUnloaded();
4096
4097 // Add debug event listener.
4098 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
4099 v8::Undefined());
ager@chromium.org381abbb2009-02-25 13:23:22 +00004100 // Create a couple of functions for the test.
4101 v8::Local<v8::Function> foo =
4102 CompileFunction(&env, "function foo(){x=1}", "foo");
4103 v8::Local<v8::Function> bar =
4104 CompileFunction(&env, "function bar(){y=2}", "bar");
4105
4106 // Set some break points.
4107 SetBreakPoint(foo, 0);
4108 SetBreakPoint(foo, 4);
4109 SetBreakPoint(bar, 0);
4110 SetBreakPoint(bar, 4);
4111
4112 // Make sure that the break points are there.
4113 break_point_hit_count = 0;
4114 foo->Call(env->Global(), 0, NULL);
4115 CHECK_EQ(2, break_point_hit_count);
4116 bar->Call(env->Global(), 0, NULL);
4117 CHECK_EQ(4, break_point_hit_count);
4118
4119 // Remove the debug event listener without clearing breakpoints.
4120 v8::Debug::SetDebugEventListener(NULL);
4121 CheckDebuggerUnloaded(true);
4122
4123 // Set a new debug event listener.
4124 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
4125 v8::Undefined());
ager@chromium.org381abbb2009-02-25 13:23:22 +00004126 // Check that the break points was actually cleared.
4127 break_point_hit_count = 0;
4128 foo->Call(env->Global(), 0, NULL);
4129 CHECK_EQ(0, break_point_hit_count);
4130
4131 // Set break points and run again.
4132 SetBreakPoint(foo, 0);
4133 SetBreakPoint(foo, 4);
4134 foo->Call(env->Global(), 0, NULL);
4135 CHECK_EQ(2, break_point_hit_count);
4136
4137 // Remove the debug event listener without clearing breakpoints again.
4138 v8::Debug::SetDebugEventListener(NULL);
4139 CheckDebuggerUnloaded(true);
4140}
4141
4142
ager@chromium.org71daaf62009-04-01 07:22:49 +00004143// Debugger message handler which counts the number of times it is called.
4144static int message_handler_hit_count = 0;
4145static void MessageHandlerHitCount(const uint16_t* message,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004146 int length,
4147 v8::Debug::ClientData* client_data) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00004148 message_handler_hit_count++;
4149
4150 const int kBufferSize = 1000;
4151 uint16_t buffer[kBufferSize];
4152 const char* command_continue =
4153 "{\"seq\":0,"
4154 "\"type\":\"request\","
4155 "\"command\":\"continue\"}";
4156
4157 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer));
4158}
4159
4160
4161// Test clearing the debug message handler.
4162TEST(DebuggerClearMessageHandler) {
4163 v8::HandleScope scope;
4164 DebugLocalContext env;
4165
4166 // Check debugger is unloaded before it is used.
4167 CheckDebuggerUnloaded();
4168
4169 // Set a debug message handler.
4170 v8::Debug::SetMessageHandler(MessageHandlerHitCount);
4171
4172 // Run code to throw a unhandled exception. This should end up in the message
4173 // handler.
4174 CompileRun("throw 1");
4175
4176 // The message handler should be called.
4177 CHECK_GT(message_handler_hit_count, 0);
4178
4179 // Clear debug message handler.
4180 message_handler_hit_count = 0;
4181 v8::Debug::SetMessageHandler(NULL);
4182
4183 // Run code to throw a unhandled exception. This should end up in the message
4184 // handler.
4185 CompileRun("throw 1");
4186
4187 // The message handler should not be called more.
4188 CHECK_EQ(0, message_handler_hit_count);
4189
4190 CheckDebuggerUnloaded(true);
4191}
4192
4193
4194// Debugger message handler which clears the message handler while active.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004195static void MessageHandlerClearingMessageHandler(
4196 const uint16_t* message,
4197 int length,
4198 v8::Debug::ClientData* client_data) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00004199 message_handler_hit_count++;
4200
4201 // Clear debug message handler.
4202 v8::Debug::SetMessageHandler(NULL);
4203}
4204
4205
4206// Test clearing the debug message handler while processing a debug event.
4207TEST(DebuggerClearMessageHandlerWhileActive) {
4208 v8::HandleScope scope;
4209 DebugLocalContext env;
4210
4211 // Check debugger is unloaded before it is used.
4212 CheckDebuggerUnloaded();
4213
4214 // Set a debug message handler.
4215 v8::Debug::SetMessageHandler(MessageHandlerClearingMessageHandler);
4216
4217 // Run code to throw a unhandled exception. This should end up in the message
4218 // handler.
4219 CompileRun("throw 1");
4220
4221 // The message handler should be called.
4222 CHECK_EQ(1, message_handler_hit_count);
4223
4224 CheckDebuggerUnloaded(true);
4225}
4226
4227
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004228/* Test DebuggerHostDispatch */
4229/* In this test, the debugger waits for a command on a breakpoint
4230 * and is dispatching host commands while in the infinite loop.
4231 */
4232
4233class HostDispatchV8Thread : public v8::internal::Thread {
4234 public:
4235 void Run();
4236};
4237
4238class HostDispatchDebuggerThread : public v8::internal::Thread {
4239 public:
4240 void Run();
4241};
4242
4243Barriers* host_dispatch_barriers;
4244
4245static void HostDispatchMessageHandler(const uint16_t* message,
4246 int length,
4247 v8::Debug::ClientData* client_data) {
4248 static char print_buffer[1000];
4249 Utf16ToAscii(message, length, print_buffer);
4250 printf("%s\n", print_buffer);
4251 fflush(stdout);
ager@chromium.org381abbb2009-02-25 13:23:22 +00004252}
4253
4254
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004255static void HostDispatchDispatchHandler() {
4256 host_dispatch_barriers->semaphore_1->Signal();
4257}
4258
4259
4260void HostDispatchV8Thread::Run() {
4261 const char* source_1 = "var y_global = 3;\n"
4262 "function cat( new_value ) {\n"
4263 " var x = new_value;\n"
4264 " y_global = 4;\n"
4265 " x = 3 * x + 1;\n"
4266 " y_global = 5;\n"
4267 " return x;\n"
4268 "}\n"
4269 "\n";
4270 const char* source_2 = "cat(17);\n";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004271
ager@chromium.org381abbb2009-02-25 13:23:22 +00004272 v8::HandleScope scope;
4273 DebugLocalContext env;
4274
ager@chromium.org381abbb2009-02-25 13:23:22 +00004275 // Setup message and host dispatch handlers.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004276 v8::Debug::SetMessageHandler(HostDispatchMessageHandler);
4277 v8::Debug::SetHostDispatchHandler(HostDispatchDispatchHandler, 10 /* ms */);
ager@chromium.org381abbb2009-02-25 13:23:22 +00004278
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004279 CompileRun(source_1);
4280 host_dispatch_barriers->barrier_1.Wait();
4281 host_dispatch_barriers->barrier_2.Wait();
4282 CompileRun(source_2);
4283}
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00004284
ager@chromium.org381abbb2009-02-25 13:23:22 +00004285
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004286void HostDispatchDebuggerThread::Run() {
4287 const int kBufSize = 1000;
4288 uint16_t buffer[kBufSize];
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00004289
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004290 const char* command_1 = "{\"seq\":101,"
4291 "\"type\":\"request\","
4292 "\"command\":\"setbreakpoint\","
4293 "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}";
4294 const char* command_2 = "{\"seq\":102,"
4295 "\"type\":\"request\","
4296 "\"command\":\"continue\"}";
4297
4298 // v8 thread initializes, runs source_1
4299 host_dispatch_barriers->barrier_1.Wait();
4300 // 1: Set breakpoint in cat().
4301 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
4302
4303 host_dispatch_barriers->barrier_2.Wait();
4304 // v8 thread starts compiling source_2.
4305 // Break happens, to run queued commands and host dispatches.
4306 // Wait for host dispatch to be processed.
4307 host_dispatch_barriers->semaphore_1->Wait();
4308 // 2: Continue evaluation
4309 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
4310}
4311
4312HostDispatchDebuggerThread host_dispatch_debugger_thread;
4313HostDispatchV8Thread host_dispatch_v8_thread;
4314
4315
4316TEST(DebuggerHostDispatch) {
4317 i::FLAG_debugger_auto_break = true;
4318
4319 // Create a V8 environment
4320 Barriers stack_allocated_host_dispatch_barriers;
4321 stack_allocated_host_dispatch_barriers.Initialize();
4322 host_dispatch_barriers = &stack_allocated_host_dispatch_barriers;
4323
4324 host_dispatch_v8_thread.Start();
4325 host_dispatch_debugger_thread.Start();
4326
4327 host_dispatch_v8_thread.Join();
4328 host_dispatch_debugger_thread.Join();
ager@chromium.org381abbb2009-02-25 13:23:22 +00004329}
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004330
4331
4332TEST(DebuggerAgent) {
4333 // Make sure this port is not used by other tests to allow tests to run in
4334 // parallel.
4335 const int kPort = 5858;
4336
4337 // Make a string with the port number.
4338 const int kPortBufferLen = 6;
4339 char port_str[kPortBufferLen];
4340 OS::SNPrintF(i::Vector<char>(port_str, kPortBufferLen), "%d", kPort);
4341
4342 bool ok;
4343
4344 // Initialize the socket library.
4345 i::Socket::Setup();
4346
4347 // Test starting and stopping the agent without any client connection.
4348 i::Debugger::StartAgent("test", kPort);
4349 i::Debugger::StopAgent();
4350
4351 // Test starting the agent, connecting a client and shutting down the agent
4352 // with the client connected.
4353 ok = i::Debugger::StartAgent("test", kPort);
4354 CHECK(ok);
4355 i::Socket* client = i::OS::CreateSocket();
4356 ok = client->Connect("localhost", port_str);
4357 CHECK(ok);
4358 i::Debugger::StopAgent();
4359 delete client;
4360
4361 // Test starting and stopping the agent with the required port already
4362 // occoupied.
4363 i::Socket* server = i::OS::CreateSocket();
4364 server->Bind(kPort);
4365
4366 i::Debugger::StartAgent("test", kPort);
4367 i::Debugger::StopAgent();
4368
4369 delete server;
4370}
4371
4372
4373class DebuggerAgentProtocolServerThread : public i::Thread {
4374 public:
4375 explicit DebuggerAgentProtocolServerThread(int port)
4376 : port_(port), server_(NULL), client_(NULL),
4377 listening_(OS::CreateSemaphore(0)) {
4378 }
4379 ~DebuggerAgentProtocolServerThread() {
4380 // Close both sockets.
4381 delete client_;
4382 delete server_;
4383 delete listening_;
4384 }
4385
4386 void Run();
4387 void WaitForListening() { listening_->Wait(); }
4388 char* body() { return *body_; }
4389
4390 private:
4391 int port_;
4392 i::SmartPointer<char> body_;
4393 i::Socket* server_; // Server socket used for bind/accept.
4394 i::Socket* client_; // Single client connection used by the test.
4395 i::Semaphore* listening_; // Signalled when the server is in listen mode.
4396};
4397
4398
4399void DebuggerAgentProtocolServerThread::Run() {
4400 bool ok;
4401
4402 // Create the server socket and bind it to the requested port.
4403 server_ = i::OS::CreateSocket();
4404 CHECK(server_ != NULL);
4405 ok = server_->Bind(port_);
4406 CHECK(ok);
4407
4408 // Listen for new connections.
4409 ok = server_->Listen(1);
4410 CHECK(ok);
4411 listening_->Signal();
4412
4413 // Accept a connection.
4414 client_ = server_->Accept();
4415 CHECK(client_ != NULL);
4416
4417 // Receive a debugger agent protocol message.
4418 i::DebuggerAgentUtil::ReceiveMessage(client_);
4419}
4420
4421
4422TEST(DebuggerAgentProtocolOverflowHeader) {
4423 // Make sure this port is not used by other tests to allow tests to run in
4424 // parallel.
4425 const int kPort = 5860;
4426 static const char* kLocalhost = "localhost";
4427
4428 // Make a string with the port number.
4429 const int kPortBufferLen = 6;
4430 char port_str[kPortBufferLen];
4431 OS::SNPrintF(i::Vector<char>(port_str, kPortBufferLen), "%d", kPort);
4432
4433 // Initialize the socket library.
4434 i::Socket::Setup();
4435
4436 // Create a socket server to receive a debugger agent message.
4437 DebuggerAgentProtocolServerThread* server =
4438 new DebuggerAgentProtocolServerThread(kPort);
4439 server->Start();
4440 server->WaitForListening();
4441
4442 // Connect.
4443 i::Socket* client = i::OS::CreateSocket();
4444 CHECK(client != NULL);
4445 bool ok = client->Connect(kLocalhost, port_str);
4446 CHECK(ok);
4447
4448 // Send headers which overflow the receive buffer.
4449 static const int kBufferSize = 1000;
4450 char buffer[kBufferSize];
4451
4452 // Long key and short value: XXXX....XXXX:0\r\n.
4453 for (int i = 0; i < kBufferSize - 4; i++) {
4454 buffer[i] = 'X';
4455 }
4456 buffer[kBufferSize - 4] = ':';
4457 buffer[kBufferSize - 3] = '0';
4458 buffer[kBufferSize - 2] = '\r';
4459 buffer[kBufferSize - 1] = '\n';
4460 client->Send(buffer, kBufferSize);
4461
4462 // Short key and long value: X:XXXX....XXXX\r\n.
4463 buffer[0] = 'X';
4464 buffer[1] = ':';
4465 for (int i = 2; i < kBufferSize - 2; i++) {
4466 buffer[i] = 'X';
4467 }
4468 buffer[kBufferSize - 2] = '\r';
4469 buffer[kBufferSize - 1] = '\n';
4470 client->Send(buffer, kBufferSize);
4471
4472 // Add empty body to request.
4473 const char* content_length_zero_header = "Content-Length:0\r\n";
4474 client->Send(content_length_zero_header, strlen(content_length_zero_header));
4475 client->Send("\r\n", 2);
4476
4477 // Wait until data is received.
4478 server->Join();
4479
4480 // Check for empty body.
4481 CHECK(server->body() == NULL);
4482
4483 // Close the client before the server to avoid TIME_WAIT issues.
4484 client->Shutdown();
4485 delete client;
4486 delete server;
4487}
ager@chromium.org41826e72009-03-30 13:30:57 +00004488
4489
4490// Test for issue http://code.google.com/p/v8/issues/detail?id=289.
4491// Make sure that DebugGetLoadedScripts doesn't return scripts
4492// with disposed external source.
4493class EmptyExternalStringResource : public v8::String::ExternalStringResource {
4494 public:
4495 EmptyExternalStringResource() { empty_[0] = 0; }
4496 virtual ~EmptyExternalStringResource() {}
4497 virtual size_t length() const { return empty_.length(); }
4498 virtual const uint16_t* data() const { return empty_.start(); }
4499 private:
4500 ::v8::internal::EmbeddedVector<uint16_t, 1> empty_;
4501};
4502
4503
4504TEST(DebugGetLoadedScripts) {
4505 v8::HandleScope scope;
4506 DebugLocalContext env;
4507 EmptyExternalStringResource source_ext_str;
4508 v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str);
4509 v8::Handle<v8::Script> evil_script = v8::Script::Compile(source);
4510 Handle<i::ExternalTwoByteString> i_source(
4511 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
4512 // This situation can happen if source was an external string disposed
4513 // by its owner.
4514 i_source->set_resource(0);
4515
4516 bool allow_natives_syntax = i::FLAG_allow_natives_syntax;
4517 i::FLAG_allow_natives_syntax = true;
4518 CompileRun(
4519 "var scripts = %DebugGetLoadedScripts();"
4520 "for (var i = 0; i < scripts.length; ++i) {"
4521 " scripts[i].line_ends;"
4522 "}");
4523 // Must not crash while accessing line_ends.
4524 i::FLAG_allow_natives_syntax = allow_natives_syntax;
4525}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004526
4527
4528// Test script break points set on lines.
4529TEST(ScriptNameAndData) {
4530 v8::HandleScope scope;
4531 DebugLocalContext env;
4532 env.ExposeDebug();
4533
4534 // Create functions for retrieving script name and data for the function on
4535 // the top frame when hitting a break point.
4536 frame_script_name = CompileFunction(&env,
4537 frame_script_name_source,
4538 "frame_script_name");
4539 frame_script_data = CompileFunction(&env,
4540 frame_script_data_source,
4541 "frame_script_data");
4542
4543 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
4544 v8::Undefined());
4545
4546 // Test function source.
4547 v8::Local<v8::String> script = v8::String::New(
4548 "function f() {\n"
4549 " debugger;\n"
4550 "}\n");
4551
4552 v8::ScriptOrigin origin1 = v8::ScriptOrigin(v8::String::New("name"));
4553 v8::Handle<v8::Script> script1 = v8::Script::Compile(script, &origin1);
4554 script1->SetData(v8::String::New("data"));
4555 script1->Run();
4556 v8::Script::Compile(script, &origin1)->Run();
4557 v8::Local<v8::Function> f;
4558 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
4559
4560 f->Call(env->Global(), 0, NULL);
4561 CHECK_EQ(1, break_point_hit_count);
4562 CHECK_EQ("name", last_script_name_hit);
4563 CHECK_EQ("data", last_script_data_hit);
4564
4565 v8::Local<v8::String> data_obj_source = v8::String::New(
4566 "({ a: 'abc',\n"
4567 " b: 123,\n"
4568 " toString: function() { return this.a + ' ' + this.b; }\n"
4569 "})\n");
4570 v8::Local<v8::Value> data_obj = v8::Script::Compile(data_obj_source)->Run();
4571 v8::ScriptOrigin origin2 = v8::ScriptOrigin(v8::String::New("new name"));
4572 v8::Handle<v8::Script> script2 = v8::Script::Compile(script, &origin2);
4573 script2->Run();
4574 script2->SetData(data_obj);
4575 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
4576 f->Call(env->Global(), 0, NULL);
4577 CHECK_EQ(2, break_point_hit_count);
4578 CHECK_EQ("new name", last_script_name_hit);
4579 CHECK_EQ("abc 123", last_script_data_hit);
4580}