blob: 288efbaed39d4c52adc0649dacb125251b38c35a [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
ager@chromium.org9085a012009-05-11 19:22:57 +0000170
171// Compile and run the supplied source and return the requested function.
172static v8::Local<v8::Function> CompileFunction(const char* source,
173 const char* function_name) {
174 v8::Script::Compile(v8::String::New(source))->Run();
175 return v8::Local<v8::Function>::Cast(
176 v8::Context::GetCurrent()->Global()->Get(v8::String::New(function_name)));
177}
178
179
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000180// Helper function that compiles and runs the source.
181static v8::Local<v8::Value> CompileRun(const char* source) {
182 return v8::Script::Compile(v8::String::New(source))->Run();
183}
184
ager@chromium.org9085a012009-05-11 19:22:57 +0000185
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000186// Is there any debug info for the function?
187static bool HasDebugInfo(v8::Handle<v8::Function> fun) {
188 Handle<v8::internal::JSFunction> f = v8::Utils::OpenHandle(*fun);
189 Handle<v8::internal::SharedFunctionInfo> shared(f->shared());
190 return Debug::HasDebugInfo(shared);
191}
192
193
194// Set a break point in a function and return the associated break point
195// number.
196static int SetBreakPoint(Handle<v8::internal::JSFunction> fun, int position) {
197 static int break_point = 0;
198 Handle<v8::internal::SharedFunctionInfo> shared(fun->shared());
199 Debug::SetBreakPoint(
200 shared, position,
201 Handle<Object>(v8::internal::Smi::FromInt(++break_point)));
202 return break_point;
203}
204
205
206// Set a break point in a function and return the associated break point
207// number.
208static int SetBreakPoint(v8::Handle<v8::Function> fun, int position) {
209 return SetBreakPoint(v8::Utils::OpenHandle(*fun), position);
210}
211
212
213// Set a break point in a function using the Debug object and return the
214// associated break point number.
215static int SetBreakPointFromJS(const char* function_name,
216 int line, int position) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000217 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
218 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000219 "debug.Debug.setBreakPoint(%s,%d,%d)",
220 function_name, line, position);
221 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000222 v8::Handle<v8::String> str = v8::String::New(buffer.start());
223 return v8::Script::Compile(str)->Run()->Int32Value();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000224}
225
226
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000227// Set a break point in a script identified by id using the global Debug object.
228static int SetScriptBreakPointByIdFromJS(int script_id, int line, int column) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000229 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000230 if (column >= 0) {
231 // Column specified set script break point on precise location.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000232 OS::SNPrintF(buffer,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000233 "debug.Debug.setScriptBreakPointById(%d,%d,%d)",
234 script_id, line, column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000235 } else {
236 // Column not specified set script break point on line.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000237 OS::SNPrintF(buffer,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000238 "debug.Debug.setScriptBreakPointById(%d,%d)",
239 script_id, line);
240 }
241 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
242 {
243 v8::TryCatch try_catch;
244 v8::Handle<v8::String> str = v8::String::New(buffer.start());
245 v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000246 CHECK(!try_catch.HasCaught());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000247 return value->Int32Value();
248 }
249}
250
251
252// Set a break point in a script identified by name using the global Debug
253// object.
254static int SetScriptBreakPointByNameFromJS(const char* script_name,
255 int line, int column) {
256 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
257 if (column >= 0) {
258 // Column specified set script break point on precise location.
259 OS::SNPrintF(buffer,
260 "debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)",
261 script_name, line, column);
262 } else {
263 // Column not specified set script break point on line.
264 OS::SNPrintF(buffer,
265 "debug.Debug.setScriptBreakPointByName(\"%s\",%d)",
266 script_name, line);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000267 }
268 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000269 {
270 v8::TryCatch try_catch;
271 v8::Handle<v8::String> str = v8::String::New(buffer.start());
272 v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000273 CHECK(!try_catch.HasCaught());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000274 return value->Int32Value();
275 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000276}
277
278
279// Clear a break point.
280static void ClearBreakPoint(int break_point) {
281 Debug::ClearBreakPoint(
282 Handle<Object>(v8::internal::Smi::FromInt(break_point)));
283}
284
285
286// Clear a break point using the global Debug object.
287static void ClearBreakPointFromJS(int break_point_number) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000288 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
289 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000290 "debug.Debug.clearBreakPoint(%d)",
291 break_point_number);
292 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000293 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000294}
295
296
297static void EnableScriptBreakPointFromJS(int break_point_number) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000298 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
299 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000300 "debug.Debug.enableScriptBreakPoint(%d)",
301 break_point_number);
302 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000303 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000304}
305
306
307static void DisableScriptBreakPointFromJS(int break_point_number) {
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.disableScriptBreakPoint(%d)",
311 break_point_number);
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 ChangeScriptBreakPointConditionFromJS(int break_point_number,
318 const char* condition) {
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.changeScriptBreakPointCondition(%d, \"%s\")",
322 break_point_number, condition);
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
328static void ChangeScriptBreakPointIgnoreCountFromJS(int break_point_number,
329 int ignoreCount) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000330 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
331 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000332 "debug.Debug.changeScriptBreakPointIgnoreCount(%d, %d)",
333 break_point_number, ignoreCount);
334 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000335 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000336}
337
338
339// Change break on exception.
340static void ChangeBreakOnException(bool caught, bool uncaught) {
341 Debug::ChangeBreakOnException(v8::internal::BreakException, caught);
342 Debug::ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught);
343}
344
345
346// Change break on exception using the global Debug object.
347static void ChangeBreakOnExceptionFromJS(bool caught, bool uncaught) {
348 if (caught) {
349 v8::Script::Compile(
350 v8::String::New("debug.Debug.setBreakOnException()"))->Run();
351 } else {
352 v8::Script::Compile(
353 v8::String::New("debug.Debug.clearBreakOnException()"))->Run();
354 }
355 if (uncaught) {
356 v8::Script::Compile(
357 v8::String::New("debug.Debug.setBreakOnUncaughtException()"))->Run();
358 } else {
359 v8::Script::Compile(
360 v8::String::New("debug.Debug.clearBreakOnUncaughtException()"))->Run();
361 }
362}
363
364
365// Prepare to step to next break location.
366static void PrepareStep(StepAction step_action) {
367 Debug::PrepareStep(step_action, 1);
368}
369
370
371// This function is in namespace v8::internal to be friend with class
372// v8::internal::Debug.
373namespace v8 { namespace internal { // NOLINT
374
375// Collect the currently debugged functions.
376Handle<FixedArray> GetDebuggedFunctions() {
377 v8::internal::DebugInfoListNode* node = Debug::debug_info_list_;
378
379 // Find the number of debugged functions.
380 int count = 0;
381 while (node) {
382 count++;
383 node = node->next();
384 }
385
386 // Allocate array for the debugged functions
387 Handle<FixedArray> debugged_functions =
388 v8::internal::Factory::NewFixedArray(count);
389
390 // Run through the debug info objects and collect all functions.
391 count = 0;
392 while (node) {
393 debugged_functions->set(count++, *node->debug_info());
394 node = node->next();
395 }
396
397 return debugged_functions;
398}
399
400
401static Handle<Code> ComputeCallDebugBreak(int argc) {
402 CALL_HEAP_FUNCTION(v8::internal::StubCache::ComputeCallDebugBreak(argc),
403 Code);
404}
405
ager@chromium.org381abbb2009-02-25 13:23:22 +0000406
ager@chromium.org381abbb2009-02-25 13:23:22 +0000407// Check that the debugger has been fully unloaded.
408void CheckDebuggerUnloaded(bool check_functions) {
409 // Check that the debugger context is cleared and that there is no debug
410 // information stored for the debugger.
411 CHECK(Debug::debug_context().is_null());
412 CHECK_EQ(NULL, Debug::debug_info_list_);
413
414 // Collect garbage to ensure weak handles are cleared.
415 Heap::CollectAllGarbage();
416 Heap::CollectAllGarbage();
417
418 // Iterate the head and check that there are no debugger related objects left.
419 HeapIterator iterator;
420 while (iterator.has_next()) {
421 HeapObject* obj = iterator.next();
422 CHECK(obj != NULL);
423 CHECK(!obj->IsDebugInfo());
424 CHECK(!obj->IsBreakPointInfo());
425
426 // If deep check of functions is requested check that no debug break code
427 // is left in all functions.
428 if (check_functions) {
429 if (obj->IsJSFunction()) {
430 JSFunction* fun = JSFunction::cast(obj);
431 for (RelocIterator it(fun->shared()->code()); !it.done(); it.next()) {
432 RelocInfo::Mode rmode = it.rinfo()->rmode();
433 if (RelocInfo::IsCodeTarget(rmode)) {
434 CHECK(!Debug::IsDebugBreak(it.rinfo()->target_address()));
435 } else if (RelocInfo::IsJSReturn(rmode)) {
436 CHECK(!Debug::IsDebugBreakAtReturn(it.rinfo()));
437 }
438 }
439 }
440 }
441 }
442}
443
444
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000445} } // namespace v8::internal
446
ager@chromium.org381abbb2009-02-25 13:23:22 +0000447
ager@chromium.org381abbb2009-02-25 13:23:22 +0000448// Check that the debugger has been fully unloaded.
449static void CheckDebuggerUnloaded(bool check_functions = false) {
450 v8::internal::CheckDebuggerUnloaded(check_functions);
451}
452
453
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000454// Inherit from BreakLocationIterator to get access to protected parts for
455// testing.
456class TestBreakLocationIterator: public v8::internal::BreakLocationIterator {
457 public:
458 explicit TestBreakLocationIterator(Handle<v8::internal::DebugInfo> debug_info)
459 : BreakLocationIterator(debug_info, v8::internal::SOURCE_BREAK_LOCATIONS) {}
460 v8::internal::RelocIterator* it() { return reloc_iterator_; }
461 v8::internal::RelocIterator* it_original() {
462 return reloc_iterator_original_;
463 }
464};
465
466
467// Compile a function, set a break point and check that the call at the break
468// location in the code is the expected debug_break function.
469void CheckDebugBreakFunction(DebugLocalContext* env,
470 const char* source, const char* name,
ager@chromium.org236ad962008-09-25 09:45:57 +0000471 int position, v8::internal::RelocInfo::Mode mode,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000472 Code* debug_break) {
473 // Create function and set the break point.
474 Handle<v8::internal::JSFunction> fun = v8::Utils::OpenHandle(
475 *CompileFunction(env, source, name));
476 int bp = SetBreakPoint(fun, position);
477
478 // Check that the debug break function is as expected.
479 Handle<v8::internal::SharedFunctionInfo> shared(fun->shared());
480 CHECK(Debug::HasDebugInfo(shared));
481 TestBreakLocationIterator it1(Debug::GetDebugInfo(shared));
482 it1.FindBreakLocationFromPosition(position);
483 CHECK_EQ(mode, it1.it()->rinfo()->rmode());
ager@chromium.org236ad962008-09-25 09:45:57 +0000484 if (mode != v8::internal::RelocInfo::JS_RETURN) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000485 CHECK_EQ(debug_break,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000486 Code::GetCodeFromTargetAddress(it1.it()->rinfo()->target_address()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000487 } else {
488 // TODO(1240753): Make the test architecture independent or split
489 // parts of the debugger into architecture dependent files.
490 CHECK_EQ(0xE8, *(it1.rinfo()->pc()));
491 }
492
493 // Clear the break point and check that the debug break function is no longer
494 // there
495 ClearBreakPoint(bp);
496 CHECK(!Debug::HasDebugInfo(shared));
497 CHECK(Debug::EnsureDebugInfo(shared));
498 TestBreakLocationIterator it2(Debug::GetDebugInfo(shared));
499 it2.FindBreakLocationFromPosition(position);
500 CHECK_EQ(mode, it2.it()->rinfo()->rmode());
ager@chromium.org236ad962008-09-25 09:45:57 +0000501 if (mode == v8::internal::RelocInfo::JS_RETURN) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000502 // TODO(1240753): Make the test architecture independent or split
503 // parts of the debugger into architecture dependent files.
504 CHECK_NE(0xE8, *(it2.rinfo()->pc()));
505 }
506}
507
508
509// --- D e b u g E v e n t H a n d l e r s
510// ---
511// --- The different tests uses a number of debug event handlers.
512// ---
513
514
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000515// Source for The JavaScript function which picks out the function name of the
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000516// top frame.
517const char* frame_function_name_source =
518 "function frame_function_name(exec_state) {"
519 " return exec_state.frame(0).func().name();"
520 "}";
521v8::Local<v8::Function> frame_function_name;
522
ager@chromium.org8bb60582008-12-11 12:02:20 +0000523
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000524// Source for The JavaScript function which picks out the source line for the
525// top frame.
526const char* frame_source_line_source =
527 "function frame_source_line(exec_state) {"
528 " return exec_state.frame(0).sourceLine();"
529 "}";
530v8::Local<v8::Function> frame_source_line;
531
532
533// Source for The JavaScript function which picks out the source column for the
534// top frame.
535const char* frame_source_column_source =
536 "function frame_source_column(exec_state) {"
537 " return exec_state.frame(0).sourceColumn();"
538 "}";
539v8::Local<v8::Function> frame_source_column;
540
541
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000542// Source for The JavaScript function which picks out the script name for the
543// top frame.
544const char* frame_script_name_source =
545 "function frame_script_name(exec_state) {"
546 " return exec_state.frame(0).func().script().name();"
547 "}";
548v8::Local<v8::Function> frame_script_name;
549
550
551// Source for The JavaScript function which picks out the script data for the
552// top frame.
553const char* frame_script_data_source =
554 "function frame_script_data(exec_state) {"
555 " return exec_state.frame(0).func().script().data();"
556 "}";
557v8::Local<v8::Function> frame_script_data;
558
559
ager@chromium.org8bb60582008-12-11 12:02:20 +0000560// Source for The JavaScript function which returns the number of frames.
561static const char* frame_count_source =
562 "function frame_count(exec_state) {"
563 " return exec_state.frameCount();"
564 "}";
565v8::Handle<v8::Function> frame_count;
566
567
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000568// Global variable to store the last function hit - used by some tests.
569char last_function_hit[80];
570
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000571// Global variable to store the name and data for last script hit - used by some
572// tests.
573char last_script_name_hit[80];
574char last_script_data_hit[80];
575
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000576// Global variables to store the last source position - used by some tests.
577int last_source_line = -1;
578int last_source_column = -1;
579
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000580// Debug event handler which counts the break points which have been hit.
581int break_point_hit_count = 0;
582static void DebugEventBreakPointHitCount(v8::DebugEvent event,
583 v8::Handle<v8::Object> exec_state,
584 v8::Handle<v8::Object> event_data,
585 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000586 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000587 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000588
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000589 // Count the number of breaks.
590 if (event == v8::Break) {
591 break_point_hit_count++;
592 if (!frame_function_name.IsEmpty()) {
593 // Get the name of the function.
594 const int argc = 1;
595 v8::Handle<v8::Value> argv[argc] = { exec_state };
596 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
597 argc, argv);
598 if (result->IsUndefined()) {
599 last_function_hit[0] = '\0';
600 } else {
601 CHECK(result->IsString());
602 v8::Handle<v8::String> function_name(result->ToString());
603 function_name->WriteAscii(last_function_hit);
604 }
605 }
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000606
607 if (!frame_source_line.IsEmpty()) {
608 // Get the source line.
609 const int argc = 1;
610 v8::Handle<v8::Value> argv[argc] = { exec_state };
611 v8::Handle<v8::Value> result = frame_source_line->Call(exec_state,
612 argc, argv);
613 CHECK(result->IsNumber());
614 last_source_line = result->Int32Value();
615 }
616
617 if (!frame_source_column.IsEmpty()) {
618 // Get the source column.
619 const int argc = 1;
620 v8::Handle<v8::Value> argv[argc] = { exec_state };
621 v8::Handle<v8::Value> result = frame_source_column->Call(exec_state,
622 argc, argv);
623 CHECK(result->IsNumber());
624 last_source_column = result->Int32Value();
625 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000626
627 if (!frame_script_name.IsEmpty()) {
628 // Get the script name of the function script.
629 const int argc = 1;
630 v8::Handle<v8::Value> argv[argc] = { exec_state };
631 v8::Handle<v8::Value> result = frame_script_name->Call(exec_state,
632 argc, argv);
633 if (result->IsUndefined()) {
634 last_script_name_hit[0] = '\0';
635 } else {
636 CHECK(result->IsString());
637 v8::Handle<v8::String> script_name(result->ToString());
638 script_name->WriteAscii(last_script_name_hit);
639 }
640 }
641
642 if (!frame_script_data.IsEmpty()) {
643 // Get the script data of the function script.
644 const int argc = 1;
645 v8::Handle<v8::Value> argv[argc] = { exec_state };
646 v8::Handle<v8::Value> result = frame_script_data->Call(exec_state,
647 argc, argv);
648 if (result->IsUndefined()) {
649 last_script_data_hit[0] = '\0';
650 } else {
651 result = result->ToString();
652 CHECK(result->IsString());
653 v8::Handle<v8::String> script_data(result->ToString());
654 script_data->WriteAscii(last_script_data_hit);
655 }
656 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000657 }
658}
659
660
ager@chromium.org8bb60582008-12-11 12:02:20 +0000661// Debug event handler which counts a number of events and collects the stack
662// height if there is a function compiled for that.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000663int exception_hit_count = 0;
664int uncaught_exception_hit_count = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000665int last_js_stack_height = -1;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000666
667static void DebugEventCounterClear() {
668 break_point_hit_count = 0;
669 exception_hit_count = 0;
670 uncaught_exception_hit_count = 0;
671}
672
673static void DebugEventCounter(v8::DebugEvent event,
674 v8::Handle<v8::Object> exec_state,
675 v8::Handle<v8::Object> event_data,
676 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000677 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000678 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000679
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000680 // Count the number of breaks.
681 if (event == v8::Break) {
682 break_point_hit_count++;
683 } else if (event == v8::Exception) {
684 exception_hit_count++;
685
686 // Check whether the exception was uncaught.
687 v8::Local<v8::String> fun_name = v8::String::New("uncaught");
688 v8::Local<v8::Function> fun =
689 v8::Function::Cast(*event_data->Get(fun_name));
690 v8::Local<v8::Value> result = *fun->Call(event_data, 0, NULL);
691 if (result->IsTrue()) {
692 uncaught_exception_hit_count++;
693 }
694 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000695
696 // Collect the JavsScript stack height if the function frame_count is
697 // compiled.
698 if (!frame_count.IsEmpty()) {
699 static const int kArgc = 1;
700 v8::Handle<v8::Value> argv[kArgc] = { exec_state };
701 // Using exec_state as receiver is just to have a receiver.
702 v8::Handle<v8::Value> result = frame_count->Call(exec_state, kArgc, argv);
703 last_js_stack_height = result->Int32Value();
704 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000705}
706
707
708// Debug event handler which evaluates a number of expressions when a break
709// point is hit. Each evaluated expression is compared with an expected value.
710// For this debug event handler to work the following two global varaibles
711// must be initialized.
712// checks: An array of expressions and expected results
713// evaluate_check_function: A JavaScript function (see below)
714
715// Structure for holding checks to do.
716struct EvaluateCheck {
717 const char* expr; // An expression to evaluate when a break point is hit.
718 v8::Handle<v8::Value> expected; // The expected result.
719};
720// Array of checks to do.
721struct EvaluateCheck* checks = NULL;
722// Source for The JavaScript function which can do the evaluation when a break
723// point is hit.
724const char* evaluate_check_source =
725 "function evaluate_check(exec_state, expr, expected) {"
726 " return exec_state.frame(0).evaluate(expr).value() === expected;"
727 "}";
728v8::Local<v8::Function> evaluate_check_function;
729
730// The actual debug event described by the longer comment above.
731static void DebugEventEvaluate(v8::DebugEvent event,
732 v8::Handle<v8::Object> exec_state,
733 v8::Handle<v8::Object> event_data,
734 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000735 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000736 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000737
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000738 if (event == v8::Break) {
739 for (int i = 0; checks[i].expr != NULL; i++) {
740 const int argc = 3;
741 v8::Handle<v8::Value> argv[argc] = { exec_state,
742 v8::String::New(checks[i].expr),
743 checks[i].expected };
744 v8::Handle<v8::Value> result =
745 evaluate_check_function->Call(exec_state, argc, argv);
746 if (!result->IsTrue()) {
747 v8::String::AsciiValue ascii(checks[i].expected->ToString());
748 V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *ascii);
749 }
750 }
751 }
752}
753
754
755// This debug event listener removes a breakpoint in a function
756int debug_event_remove_break_point = 0;
757static void DebugEventRemoveBreakPoint(v8::DebugEvent event,
758 v8::Handle<v8::Object> exec_state,
759 v8::Handle<v8::Object> event_data,
760 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000761 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000762 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000763
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000764 if (event == v8::Break) {
765 break_point_hit_count++;
766 v8::Handle<v8::Function> fun = v8::Handle<v8::Function>::Cast(data);
767 ClearBreakPoint(debug_event_remove_break_point);
768 }
769}
770
771
772// Debug event handler which counts break points hit and performs a step
773// afterwards.
774StepAction step_action = StepIn; // Step action to perform when stepping.
775static void DebugEventStep(v8::DebugEvent event,
776 v8::Handle<v8::Object> exec_state,
777 v8::Handle<v8::Object> event_data,
778 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000779 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000780 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000781
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000782 if (event == v8::Break) {
783 break_point_hit_count++;
784 PrepareStep(step_action);
785 }
786}
787
788
789// Debug event handler which counts break points hit and performs a step
790// afterwards. For each call the expected function is checked.
791// For this debug event handler to work the following two global varaibles
792// must be initialized.
793// expected_step_sequence: An array of the expected function call sequence.
794// frame_function_name: A JavaScript function (see below).
795
796// String containing the expected function call sequence. Note: this only works
797// if functions have name length of one.
798const char* expected_step_sequence = NULL;
799
800// The actual debug event described by the longer comment above.
801static void DebugEventStepSequence(v8::DebugEvent event,
802 v8::Handle<v8::Object> exec_state,
803 v8::Handle<v8::Object> event_data,
804 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000805 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000806 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000807
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000808 if (event == v8::Break || event == v8::Exception) {
809 // Check that the current function is the expected.
810 CHECK(break_point_hit_count <
811 static_cast<int>(strlen(expected_step_sequence)));
812 const int argc = 1;
813 v8::Handle<v8::Value> argv[argc] = { exec_state };
814 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
815 argc, argv);
816 CHECK(result->IsString());
817 v8::String::AsciiValue function_name(result->ToString());
818 CHECK_EQ(1, strlen(*function_name));
819 CHECK_EQ((*function_name)[0],
820 expected_step_sequence[break_point_hit_count]);
821
822 // Perform step.
823 break_point_hit_count++;
824 PrepareStep(step_action);
825 }
826}
827
828
829// Debug event handler which performs a garbage collection.
830static void DebugEventBreakPointCollectGarbage(
831 v8::DebugEvent event,
832 v8::Handle<v8::Object> exec_state,
833 v8::Handle<v8::Object> event_data,
834 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000835 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000836 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000837
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000838 // Perform a garbage collection when break point is hit and continue. Based
839 // on the number of break points hit either scavenge or mark compact
840 // collector is used.
841 if (event == v8::Break) {
842 break_point_hit_count++;
843 if (break_point_hit_count % 2 == 0) {
844 // Scavenge.
845 Heap::CollectGarbage(0, v8::internal::NEW_SPACE);
846 } else {
847 // Mark sweep (and perhaps compact).
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000848 Heap::CollectAllGarbage();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000849 }
850 }
851}
852
853
854// Debug event handler which re-issues a debug break and calls the garbage
855// collector to have the heap verified.
856static void DebugEventBreak(v8::DebugEvent event,
857 v8::Handle<v8::Object> exec_state,
858 v8::Handle<v8::Object> event_data,
859 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000860 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000861 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000862
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000863 if (event == v8::Break) {
864 // Count the number of breaks.
865 break_point_hit_count++;
866
867 // Run the garbage collector to enforce heap verification if option
868 // --verify-heap is set.
869 Heap::CollectGarbage(0, v8::internal::NEW_SPACE);
870
871 // Set the break flag again to come back here as soon as possible.
872 v8::Debug::DebugBreak();
873 }
874}
875
876
877// --- M e s s a g e C a l l b a c k
878
879
880// Message callback which counts the number of messages.
881int message_callback_count = 0;
882
883static void MessageCallbackCountClear() {
884 message_callback_count = 0;
885}
886
887static void MessageCallbackCount(v8::Handle<v8::Message> message,
888 v8::Handle<v8::Value> data) {
889 message_callback_count++;
890}
891
892
893// --- T h e A c t u a l T e s t s
894
895
896// Test that the debug break function is the expected one for different kinds
897// of break locations.
898TEST(DebugStub) {
899 using ::v8::internal::Builtins;
900 v8::HandleScope scope;
901 DebugLocalContext env;
902
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000903 CheckDebugBreakFunction(&env,
904 "function f1(){}", "f1",
905 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000906 v8::internal::RelocInfo::JS_RETURN,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000907 NULL);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000908 CheckDebugBreakFunction(&env,
909 "function f2(){x=1;}", "f2",
910 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000911 v8::internal::RelocInfo::CODE_TARGET,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000912 Builtins::builtin(Builtins::StoreIC_DebugBreak));
913 CheckDebugBreakFunction(&env,
914 "function f3(){var a=x;}", "f3",
915 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000916 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000917 Builtins::builtin(Builtins::LoadIC_DebugBreak));
918
ager@chromium.org236ad962008-09-25 09:45:57 +0000919// TODO(1240753): Make the test architecture independent or split
920// parts of the debugger into architecture dependent files. This
921// part currently disabled as it is not portable between IA32/ARM.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000922// Currently on ICs for keyed store/load on ARM.
923#if !defined (__arm__) && !defined(__thumb__)
924 CheckDebugBreakFunction(
925 &env,
926 "function f4(){var index='propertyName'; var a={}; a[index] = 'x';}",
927 "f4",
928 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000929 v8::internal::RelocInfo::CODE_TARGET,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000930 Builtins::builtin(Builtins::KeyedStoreIC_DebugBreak));
931 CheckDebugBreakFunction(
932 &env,
933 "function f5(){var index='propertyName'; var a={}; return a[index];}",
934 "f5",
935 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000936 v8::internal::RelocInfo::CODE_TARGET,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000937 Builtins::builtin(Builtins::KeyedLoadIC_DebugBreak));
938#endif
939
940 // Check the debug break code stubs for call ICs with different number of
941 // parameters.
942 Handle<Code> debug_break_0 = v8::internal::ComputeCallDebugBreak(0);
943 Handle<Code> debug_break_1 = v8::internal::ComputeCallDebugBreak(1);
944 Handle<Code> debug_break_4 = v8::internal::ComputeCallDebugBreak(4);
945
946 CheckDebugBreakFunction(&env,
947 "function f4_0(){x();}", "f4_0",
948 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000949 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000950 *debug_break_0);
951
952 CheckDebugBreakFunction(&env,
953 "function f4_1(){x(1);}", "f4_1",
954 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000955 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000956 *debug_break_1);
957
958 CheckDebugBreakFunction(&env,
959 "function f4_4(){x(1,2,3,4);}", "f4_4",
960 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000961 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000962 *debug_break_4);
963}
964
965
966// Test that the debug info in the VM is in sync with the functions being
967// debugged.
968TEST(DebugInfo) {
969 v8::HandleScope scope;
970 DebugLocalContext env;
971 // Create a couple of functions for the test.
972 v8::Local<v8::Function> foo =
973 CompileFunction(&env, "function foo(){}", "foo");
974 v8::Local<v8::Function> bar =
975 CompileFunction(&env, "function bar(){}", "bar");
976 // Initially no functions are debugged.
977 CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
978 CHECK(!HasDebugInfo(foo));
979 CHECK(!HasDebugInfo(bar));
980 // One function (foo) is debugged.
981 int bp1 = SetBreakPoint(foo, 0);
982 CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
983 CHECK(HasDebugInfo(foo));
984 CHECK(!HasDebugInfo(bar));
985 // Two functions are debugged.
986 int bp2 = SetBreakPoint(bar, 0);
987 CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length());
988 CHECK(HasDebugInfo(foo));
989 CHECK(HasDebugInfo(bar));
990 // One function (bar) is debugged.
991 ClearBreakPoint(bp1);
992 CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
993 CHECK(!HasDebugInfo(foo));
994 CHECK(HasDebugInfo(bar));
995 // No functions are debugged.
996 ClearBreakPoint(bp2);
997 CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
998 CHECK(!HasDebugInfo(foo));
999 CHECK(!HasDebugInfo(bar));
1000}
1001
1002
1003// Test that a break point can be set at an IC store location.
1004TEST(BreakPointICStore) {
1005 break_point_hit_count = 0;
1006 v8::HandleScope scope;
1007 DebugLocalContext env;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001008
iposva@chromium.org245aa852009-02-10 00:49:54 +00001009 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001010 v8::Undefined());
1011 v8::Script::Compile(v8::String::New("function foo(){bar=0;}"))->Run();
1012 v8::Local<v8::Function> foo =
1013 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1014
1015 // Run without breakpoints.
1016 foo->Call(env->Global(), 0, NULL);
1017 CHECK_EQ(0, break_point_hit_count);
1018
1019 // Run with breakpoint
1020 int bp = SetBreakPoint(foo, 0);
1021 foo->Call(env->Global(), 0, NULL);
1022 CHECK_EQ(1, break_point_hit_count);
1023 foo->Call(env->Global(), 0, NULL);
1024 CHECK_EQ(2, break_point_hit_count);
1025
1026 // Run without breakpoints.
1027 ClearBreakPoint(bp);
1028 foo->Call(env->Global(), 0, NULL);
1029 CHECK_EQ(2, break_point_hit_count);
1030
iposva@chromium.org245aa852009-02-10 00:49:54 +00001031 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001032 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001033}
1034
1035
1036// Test that a break point can be set at an IC load location.
1037TEST(BreakPointICLoad) {
1038 break_point_hit_count = 0;
1039 v8::HandleScope scope;
1040 DebugLocalContext env;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001041 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001042 v8::Undefined());
1043 v8::Script::Compile(v8::String::New("bar=1"))->Run();
1044 v8::Script::Compile(v8::String::New("function foo(){var x=bar;}"))->Run();
1045 v8::Local<v8::Function> foo =
1046 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1047
1048 // Run without breakpoints.
1049 foo->Call(env->Global(), 0, NULL);
1050 CHECK_EQ(0, break_point_hit_count);
1051
1052 // Run with breakpoint
1053 int bp = SetBreakPoint(foo, 0);
1054 foo->Call(env->Global(), 0, NULL);
1055 CHECK_EQ(1, break_point_hit_count);
1056 foo->Call(env->Global(), 0, NULL);
1057 CHECK_EQ(2, break_point_hit_count);
1058
1059 // Run without breakpoints.
1060 ClearBreakPoint(bp);
1061 foo->Call(env->Global(), 0, NULL);
1062 CHECK_EQ(2, break_point_hit_count);
1063
iposva@chromium.org245aa852009-02-10 00:49:54 +00001064 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001065 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001066}
1067
1068
1069// Test that a break point can be set at an IC call location.
1070TEST(BreakPointICCall) {
1071 break_point_hit_count = 0;
1072 v8::HandleScope scope;
1073 DebugLocalContext env;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001074 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001075 v8::Undefined());
1076 v8::Script::Compile(v8::String::New("function bar(){}"))->Run();
1077 v8::Script::Compile(v8::String::New("function foo(){bar();}"))->Run();
1078 v8::Local<v8::Function> foo =
1079 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1080
1081 // Run without breakpoints.
1082 foo->Call(env->Global(), 0, NULL);
1083 CHECK_EQ(0, break_point_hit_count);
1084
1085 // Run with breakpoint
1086 int bp = SetBreakPoint(foo, 0);
1087 foo->Call(env->Global(), 0, NULL);
1088 CHECK_EQ(1, break_point_hit_count);
1089 foo->Call(env->Global(), 0, NULL);
1090 CHECK_EQ(2, break_point_hit_count);
1091
1092 // Run without breakpoints.
1093 ClearBreakPoint(bp);
1094 foo->Call(env->Global(), 0, NULL);
1095 CHECK_EQ(2, break_point_hit_count);
1096
iposva@chromium.org245aa852009-02-10 00:49:54 +00001097 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001098 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001099}
1100
1101
1102// Test that a break point can be set at a return store location.
1103TEST(BreakPointReturn) {
1104 break_point_hit_count = 0;
1105 v8::HandleScope scope;
1106 DebugLocalContext env;
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001107
1108 // Create a functions for checking the source line and column when hitting
1109 // a break point.
1110 frame_source_line = CompileFunction(&env,
1111 frame_source_line_source,
1112 "frame_source_line");
1113 frame_source_column = CompileFunction(&env,
1114 frame_source_column_source,
1115 "frame_source_column");
1116
1117
iposva@chromium.org245aa852009-02-10 00:49:54 +00001118 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001119 v8::Undefined());
1120 v8::Script::Compile(v8::String::New("function foo(){}"))->Run();
1121 v8::Local<v8::Function> foo =
1122 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1123
1124 // Run without breakpoints.
1125 foo->Call(env->Global(), 0, NULL);
1126 CHECK_EQ(0, break_point_hit_count);
1127
1128 // Run with breakpoint
1129 int bp = SetBreakPoint(foo, 0);
1130 foo->Call(env->Global(), 0, NULL);
1131 CHECK_EQ(1, break_point_hit_count);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001132 CHECK_EQ(0, last_source_line);
1133 CHECK_EQ(16, last_source_column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001134 foo->Call(env->Global(), 0, NULL);
1135 CHECK_EQ(2, break_point_hit_count);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001136 CHECK_EQ(0, last_source_line);
1137 CHECK_EQ(16, last_source_column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001138
1139 // Run without breakpoints.
1140 ClearBreakPoint(bp);
1141 foo->Call(env->Global(), 0, NULL);
1142 CHECK_EQ(2, break_point_hit_count);
1143
iposva@chromium.org245aa852009-02-10 00:49:54 +00001144 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001145 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001146}
1147
1148
1149static void CallWithBreakPoints(v8::Local<v8::Object> recv,
1150 v8::Local<v8::Function> f,
1151 int break_point_count,
1152 int call_count) {
1153 break_point_hit_count = 0;
1154 for (int i = 0; i < call_count; i++) {
1155 f->Call(recv, 0, NULL);
1156 CHECK_EQ((i + 1) * break_point_count, break_point_hit_count);
1157 }
1158}
1159
1160// Test GC during break point processing.
1161TEST(GCDuringBreakPointProcessing) {
1162 break_point_hit_count = 0;
1163 v8::HandleScope scope;
1164 DebugLocalContext env;
1165
iposva@chromium.org245aa852009-02-10 00:49:54 +00001166 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001167 v8::Undefined());
1168 v8::Local<v8::Function> foo;
1169
1170 // Test IC store break point with garbage collection.
1171 foo = CompileFunction(&env, "function foo(){bar=0;}", "foo");
1172 SetBreakPoint(foo, 0);
1173 CallWithBreakPoints(env->Global(), foo, 1, 10);
1174
1175 // Test IC load break point with garbage collection.
1176 foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo");
1177 SetBreakPoint(foo, 0);
1178 CallWithBreakPoints(env->Global(), foo, 1, 10);
1179
1180 // Test IC call break point with garbage collection.
1181 foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo");
1182 SetBreakPoint(foo, 0);
1183 CallWithBreakPoints(env->Global(), foo, 1, 10);
1184
1185 // Test return break point with garbage collection.
1186 foo = CompileFunction(&env, "function foo(){}", "foo");
1187 SetBreakPoint(foo, 0);
1188 CallWithBreakPoints(env->Global(), foo, 1, 25);
1189
iposva@chromium.org245aa852009-02-10 00:49:54 +00001190 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001191 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001192}
1193
1194
1195// Call the function three times with different garbage collections in between
1196// and make sure that the break point survives.
1197static void CallAndGC(v8::Local<v8::Object> recv, v8::Local<v8::Function> f) {
1198 break_point_hit_count = 0;
1199
1200 for (int i = 0; i < 3; i++) {
1201 // Call function.
1202 f->Call(recv, 0, NULL);
1203 CHECK_EQ(1 + i * 3, break_point_hit_count);
1204
1205 // Scavenge and call function.
1206 Heap::CollectGarbage(0, v8::internal::NEW_SPACE);
1207 f->Call(recv, 0, NULL);
1208 CHECK_EQ(2 + i * 3, break_point_hit_count);
1209
1210 // Mark sweep (and perhaps compact) and call function.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001211 Heap::CollectAllGarbage();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001212 f->Call(recv, 0, NULL);
1213 CHECK_EQ(3 + i * 3, break_point_hit_count);
1214 }
1215}
1216
1217
1218// Test that a break point can be set at a return store location.
1219TEST(BreakPointSurviveGC) {
1220 break_point_hit_count = 0;
1221 v8::HandleScope scope;
1222 DebugLocalContext env;
1223
iposva@chromium.org245aa852009-02-10 00:49:54 +00001224 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001225 v8::Undefined());
1226 v8::Local<v8::Function> foo;
1227
1228 // Test IC store break point with garbage collection.
1229 foo = CompileFunction(&env, "function foo(){bar=0;}", "foo");
1230 SetBreakPoint(foo, 0);
1231 CallAndGC(env->Global(), foo);
1232
1233 // Test IC load break point with garbage collection.
1234 foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo");
1235 SetBreakPoint(foo, 0);
1236 CallAndGC(env->Global(), foo);
1237
1238 // Test IC call break point with garbage collection.
1239 foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo");
1240 SetBreakPoint(foo, 0);
1241 CallAndGC(env->Global(), foo);
1242
1243 // Test return break point with garbage collection.
1244 foo = CompileFunction(&env, "function foo(){}", "foo");
1245 SetBreakPoint(foo, 0);
1246 CallAndGC(env->Global(), foo);
1247
iposva@chromium.org245aa852009-02-10 00:49:54 +00001248 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001249 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001250}
1251
1252
1253// Test that break points can be set using the global Debug object.
1254TEST(BreakPointThroughJavaScript) {
1255 break_point_hit_count = 0;
1256 v8::HandleScope scope;
1257 DebugLocalContext env;
1258 env.ExposeDebug();
1259
iposva@chromium.org245aa852009-02-10 00:49:54 +00001260 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001261 v8::Undefined());
1262 v8::Script::Compile(v8::String::New("function bar(){}"))->Run();
1263 v8::Script::Compile(v8::String::New("function foo(){bar();bar();}"))->Run();
1264 // 012345678901234567890
1265 // 1 2
1266 // Break points are set at position 3 and 9
1267 v8::Local<v8::Script> foo = v8::Script::Compile(v8::String::New("foo()"));
1268
1269 // Run without breakpoints.
1270 foo->Run();
1271 CHECK_EQ(0, break_point_hit_count);
1272
1273 // Run with one breakpoint
1274 int bp1 = SetBreakPointFromJS("foo", 0, 3);
1275 foo->Run();
1276 CHECK_EQ(1, break_point_hit_count);
1277 foo->Run();
1278 CHECK_EQ(2, break_point_hit_count);
1279
1280 // Run with two breakpoints
1281 int bp2 = SetBreakPointFromJS("foo", 0, 9);
1282 foo->Run();
1283 CHECK_EQ(4, break_point_hit_count);
1284 foo->Run();
1285 CHECK_EQ(6, break_point_hit_count);
1286
1287 // Run with one breakpoint
1288 ClearBreakPointFromJS(bp2);
1289 foo->Run();
1290 CHECK_EQ(7, break_point_hit_count);
1291 foo->Run();
1292 CHECK_EQ(8, break_point_hit_count);
1293
1294 // Run without breakpoints.
1295 ClearBreakPointFromJS(bp1);
1296 foo->Run();
1297 CHECK_EQ(8, break_point_hit_count);
1298
iposva@chromium.org245aa852009-02-10 00:49:54 +00001299 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001300 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001301
1302 // Make sure that the break point numbers are consecutive.
1303 CHECK_EQ(1, bp1);
1304 CHECK_EQ(2, bp2);
1305}
1306
1307
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001308// Test that break points on scripts identified by name can be set using the
1309// global Debug object.
1310TEST(ScriptBreakPointByNameThroughJavaScript) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001311 break_point_hit_count = 0;
1312 v8::HandleScope scope;
1313 DebugLocalContext env;
1314 env.ExposeDebug();
1315
iposva@chromium.org245aa852009-02-10 00:49:54 +00001316 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001317 v8::Undefined());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001318
1319 v8::Local<v8::String> script = v8::String::New(
1320 "function f() {\n"
1321 " function h() {\n"
1322 " a = 0; // line 2\n"
1323 " }\n"
1324 " b = 1; // line 4\n"
1325 " return h();\n"
1326 "}\n"
1327 "\n"
1328 "function g() {\n"
1329 " function h() {\n"
1330 " a = 0;\n"
1331 " }\n"
1332 " b = 2; // line 12\n"
1333 " h();\n"
1334 " b = 3; // line 14\n"
1335 " f(); // line 15\n"
1336 "}");
1337
1338 // Compile the script and get the two functions.
1339 v8::ScriptOrigin origin =
1340 v8::ScriptOrigin(v8::String::New("test"));
1341 v8::Script::Compile(script, &origin)->Run();
1342 v8::Local<v8::Function> f =
1343 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1344 v8::Local<v8::Function> g =
1345 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1346
1347 // Call f and g without break points.
1348 break_point_hit_count = 0;
1349 f->Call(env->Global(), 0, NULL);
1350 CHECK_EQ(0, break_point_hit_count);
1351 g->Call(env->Global(), 0, NULL);
1352 CHECK_EQ(0, break_point_hit_count);
1353
1354 // Call f and g with break point on line 12.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001355 int sbp1 = SetScriptBreakPointByNameFromJS("test", 12, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001356 break_point_hit_count = 0;
1357 f->Call(env->Global(), 0, NULL);
1358 CHECK_EQ(0, break_point_hit_count);
1359 g->Call(env->Global(), 0, NULL);
1360 CHECK_EQ(1, break_point_hit_count);
1361
1362 // Remove the break point again.
1363 break_point_hit_count = 0;
1364 ClearBreakPointFromJS(sbp1);
1365 f->Call(env->Global(), 0, NULL);
1366 CHECK_EQ(0, break_point_hit_count);
1367 g->Call(env->Global(), 0, NULL);
1368 CHECK_EQ(0, break_point_hit_count);
1369
1370 // Call f and g with break point on line 2.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001371 int sbp2 = SetScriptBreakPointByNameFromJS("test", 2, 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(1, break_point_hit_count);
1375 g->Call(env->Global(), 0, NULL);
1376 CHECK_EQ(2, break_point_hit_count);
1377
1378 // Call f and g with break point on line 2, 4, 12, 14 and 15.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001379 int sbp3 = SetScriptBreakPointByNameFromJS("test", 4, 0);
1380 int sbp4 = SetScriptBreakPointByNameFromJS("test", 12, 0);
1381 int sbp5 = SetScriptBreakPointByNameFromJS("test", 14, 0);
1382 int sbp6 = SetScriptBreakPointByNameFromJS("test", 15, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001383 break_point_hit_count = 0;
1384 f->Call(env->Global(), 0, NULL);
1385 CHECK_EQ(2, break_point_hit_count);
1386 g->Call(env->Global(), 0, NULL);
1387 CHECK_EQ(7, break_point_hit_count);
1388
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001389 // Remove all the break points again.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001390 break_point_hit_count = 0;
1391 ClearBreakPointFromJS(sbp2);
1392 ClearBreakPointFromJS(sbp3);
1393 ClearBreakPointFromJS(sbp4);
1394 ClearBreakPointFromJS(sbp5);
1395 ClearBreakPointFromJS(sbp6);
1396 f->Call(env->Global(), 0, NULL);
1397 CHECK_EQ(0, break_point_hit_count);
1398 g->Call(env->Global(), 0, NULL);
1399 CHECK_EQ(0, break_point_hit_count);
1400
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001401 v8::Debug::SetDebugEventListener(NULL);
1402 CheckDebuggerUnloaded();
1403
1404 // Make sure that the break point numbers are consecutive.
1405 CHECK_EQ(1, sbp1);
1406 CHECK_EQ(2, sbp2);
1407 CHECK_EQ(3, sbp3);
1408 CHECK_EQ(4, sbp4);
1409 CHECK_EQ(5, sbp5);
1410 CHECK_EQ(6, sbp6);
1411}
1412
1413
1414TEST(ScriptBreakPointByIdThroughJavaScript) {
1415 break_point_hit_count = 0;
1416 v8::HandleScope scope;
1417 DebugLocalContext env;
1418 env.ExposeDebug();
1419
1420 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
1421 v8::Undefined());
1422
1423 v8::Local<v8::String> source = v8::String::New(
1424 "function f() {\n"
1425 " function h() {\n"
1426 " a = 0; // line 2\n"
1427 " }\n"
1428 " b = 1; // line 4\n"
1429 " return h();\n"
1430 "}\n"
1431 "\n"
1432 "function g() {\n"
1433 " function h() {\n"
1434 " a = 0;\n"
1435 " }\n"
1436 " b = 2; // line 12\n"
1437 " h();\n"
1438 " b = 3; // line 14\n"
1439 " f(); // line 15\n"
1440 "}");
1441
1442 // Compile the script and get the two functions.
1443 v8::ScriptOrigin origin =
1444 v8::ScriptOrigin(v8::String::New("test"));
1445 v8::Local<v8::Script> script = v8::Script::Compile(source, &origin);
1446 script->Run();
1447 v8::Local<v8::Function> f =
1448 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1449 v8::Local<v8::Function> g =
1450 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1451
1452 // Get the script id knowing that internally it is a 32 integer.
1453 uint32_t script_id = script->Id()->Uint32Value();
1454
1455 // Call f and g without break points.
1456 break_point_hit_count = 0;
1457 f->Call(env->Global(), 0, NULL);
1458 CHECK_EQ(0, break_point_hit_count);
1459 g->Call(env->Global(), 0, NULL);
1460 CHECK_EQ(0, break_point_hit_count);
1461
1462 // Call f and g with break point on line 12.
1463 int sbp1 = SetScriptBreakPointByIdFromJS(script_id, 12, 0);
1464 break_point_hit_count = 0;
1465 f->Call(env->Global(), 0, NULL);
1466 CHECK_EQ(0, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001467 g->Call(env->Global(), 0, NULL);
1468 CHECK_EQ(1, break_point_hit_count);
1469
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001470 // Remove the break point again.
1471 break_point_hit_count = 0;
1472 ClearBreakPointFromJS(sbp1);
1473 f->Call(env->Global(), 0, NULL);
1474 CHECK_EQ(0, break_point_hit_count);
1475 g->Call(env->Global(), 0, NULL);
1476 CHECK_EQ(0, break_point_hit_count);
1477
1478 // Call f and g with break point on line 2.
1479 int sbp2 = SetScriptBreakPointByIdFromJS(script_id, 2, 0);
1480 break_point_hit_count = 0;
1481 f->Call(env->Global(), 0, NULL);
1482 CHECK_EQ(1, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001483 g->Call(env->Global(), 0, NULL);
1484 CHECK_EQ(2, break_point_hit_count);
1485
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001486 // Call f and g with break point on line 2, 4, 12, 14 and 15.
1487 int sbp3 = SetScriptBreakPointByIdFromJS(script_id, 4, 0);
1488 int sbp4 = SetScriptBreakPointByIdFromJS(script_id, 12, 0);
1489 int sbp5 = SetScriptBreakPointByIdFromJS(script_id, 14, 0);
1490 int sbp6 = SetScriptBreakPointByIdFromJS(script_id, 15, 0);
1491 break_point_hit_count = 0;
1492 f->Call(env->Global(), 0, NULL);
1493 CHECK_EQ(2, break_point_hit_count);
1494 g->Call(env->Global(), 0, NULL);
1495 CHECK_EQ(7, break_point_hit_count);
1496
1497 // Remove all the break points again.
1498 break_point_hit_count = 0;
1499 ClearBreakPointFromJS(sbp2);
1500 ClearBreakPointFromJS(sbp3);
1501 ClearBreakPointFromJS(sbp4);
1502 ClearBreakPointFromJS(sbp5);
1503 ClearBreakPointFromJS(sbp6);
1504 f->Call(env->Global(), 0, NULL);
1505 CHECK_EQ(0, break_point_hit_count);
1506 g->Call(env->Global(), 0, NULL);
1507 CHECK_EQ(0, break_point_hit_count);
1508
iposva@chromium.org245aa852009-02-10 00:49:54 +00001509 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001510 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001511
1512 // Make sure that the break point numbers are consecutive.
1513 CHECK_EQ(1, sbp1);
1514 CHECK_EQ(2, sbp2);
1515 CHECK_EQ(3, sbp3);
1516 CHECK_EQ(4, sbp4);
1517 CHECK_EQ(5, sbp5);
1518 CHECK_EQ(6, sbp6);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001519}
1520
1521
1522// Test conditional script break points.
1523TEST(EnableDisableScriptBreakPoint) {
1524 break_point_hit_count = 0;
1525 v8::HandleScope scope;
1526 DebugLocalContext env;
1527 env.ExposeDebug();
1528
iposva@chromium.org245aa852009-02-10 00:49:54 +00001529 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001530 v8::Undefined());
1531
1532 v8::Local<v8::String> script = v8::String::New(
1533 "function f() {\n"
1534 " a = 0; // line 1\n"
1535 "};");
1536
1537 // Compile the script and get function f.
1538 v8::ScriptOrigin origin =
1539 v8::ScriptOrigin(v8::String::New("test"));
1540 v8::Script::Compile(script, &origin)->Run();
1541 v8::Local<v8::Function> f =
1542 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1543
1544 // Set script break point on line 1 (in function f).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001545 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001546
1547 // Call f while enabeling and disabling the script break point.
1548 break_point_hit_count = 0;
1549 f->Call(env->Global(), 0, NULL);
1550 CHECK_EQ(1, break_point_hit_count);
1551
1552 DisableScriptBreakPointFromJS(sbp);
1553 f->Call(env->Global(), 0, NULL);
1554 CHECK_EQ(1, break_point_hit_count);
1555
1556 EnableScriptBreakPointFromJS(sbp);
1557 f->Call(env->Global(), 0, NULL);
1558 CHECK_EQ(2, break_point_hit_count);
1559
1560 DisableScriptBreakPointFromJS(sbp);
1561 f->Call(env->Global(), 0, NULL);
1562 CHECK_EQ(2, break_point_hit_count);
1563
1564 // Reload the script and get f again checking that the disabeling survives.
1565 v8::Script::Compile(script, &origin)->Run();
1566 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1567 f->Call(env->Global(), 0, NULL);
1568 CHECK_EQ(2, break_point_hit_count);
1569
1570 EnableScriptBreakPointFromJS(sbp);
1571 f->Call(env->Global(), 0, NULL);
1572 CHECK_EQ(3, break_point_hit_count);
1573
iposva@chromium.org245aa852009-02-10 00:49:54 +00001574 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001575 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001576}
1577
1578
1579// Test conditional script break points.
1580TEST(ConditionalScriptBreakPoint) {
1581 break_point_hit_count = 0;
1582 v8::HandleScope scope;
1583 DebugLocalContext env;
1584 env.ExposeDebug();
1585
iposva@chromium.org245aa852009-02-10 00:49:54 +00001586 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001587 v8::Undefined());
1588
1589 v8::Local<v8::String> script = v8::String::New(
1590 "count = 0;\n"
1591 "function f() {\n"
1592 " g(count++); // line 2\n"
1593 "};\n"
1594 "function g(x) {\n"
1595 " var a=x; // line 5\n"
1596 "};");
1597
1598 // Compile the script and get function f.
1599 v8::ScriptOrigin origin =
1600 v8::ScriptOrigin(v8::String::New("test"));
1601 v8::Script::Compile(script, &origin)->Run();
1602 v8::Local<v8::Function> f =
1603 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1604
1605 // Set script break point on line 5 (in function g).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001606 int sbp1 = SetScriptBreakPointByNameFromJS("test", 5, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001607
1608 // Call f with different conditions on the script break point.
1609 break_point_hit_count = 0;
1610 ChangeScriptBreakPointConditionFromJS(sbp1, "false");
1611 f->Call(env->Global(), 0, NULL);
1612 CHECK_EQ(0, break_point_hit_count);
1613
1614 ChangeScriptBreakPointConditionFromJS(sbp1, "true");
1615 break_point_hit_count = 0;
1616 f->Call(env->Global(), 0, NULL);
1617 CHECK_EQ(1, break_point_hit_count);
1618
1619 ChangeScriptBreakPointConditionFromJS(sbp1, "a % 2 == 0");
1620 break_point_hit_count = 0;
1621 for (int i = 0; i < 10; i++) {
1622 f->Call(env->Global(), 0, NULL);
1623 }
1624 CHECK_EQ(5, break_point_hit_count);
1625
1626 // Reload the script and get f again checking that the condition survives.
1627 v8::Script::Compile(script, &origin)->Run();
1628 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1629
1630 break_point_hit_count = 0;
1631 for (int i = 0; i < 10; i++) {
1632 f->Call(env->Global(), 0, NULL);
1633 }
1634 CHECK_EQ(5, break_point_hit_count);
1635
iposva@chromium.org245aa852009-02-10 00:49:54 +00001636 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001637 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001638}
1639
1640
1641// Test ignore count on script break points.
1642TEST(ScriptBreakPointIgnoreCount) {
1643 break_point_hit_count = 0;
1644 v8::HandleScope scope;
1645 DebugLocalContext env;
1646 env.ExposeDebug();
1647
iposva@chromium.org245aa852009-02-10 00:49:54 +00001648 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001649 v8::Undefined());
1650
1651 v8::Local<v8::String> script = v8::String::New(
1652 "function f() {\n"
1653 " a = 0; // line 1\n"
1654 "};");
1655
1656 // Compile the script and get function f.
1657 v8::ScriptOrigin origin =
1658 v8::ScriptOrigin(v8::String::New("test"));
1659 v8::Script::Compile(script, &origin)->Run();
1660 v8::Local<v8::Function> f =
1661 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1662
1663 // Set script break point on line 1 (in function f).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001664 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001665
1666 // Call f with different ignores on the script break point.
1667 break_point_hit_count = 0;
1668 ChangeScriptBreakPointIgnoreCountFromJS(sbp, 1);
1669 f->Call(env->Global(), 0, NULL);
1670 CHECK_EQ(0, break_point_hit_count);
1671 f->Call(env->Global(), 0, NULL);
1672 CHECK_EQ(1, break_point_hit_count);
1673
1674 ChangeScriptBreakPointIgnoreCountFromJS(sbp, 5);
1675 break_point_hit_count = 0;
1676 for (int i = 0; i < 10; i++) {
1677 f->Call(env->Global(), 0, NULL);
1678 }
1679 CHECK_EQ(5, break_point_hit_count);
1680
1681 // Reload the script and get f again checking that the ignore survives.
1682 v8::Script::Compile(script, &origin)->Run();
1683 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1684
1685 break_point_hit_count = 0;
1686 for (int i = 0; i < 10; i++) {
1687 f->Call(env->Global(), 0, NULL);
1688 }
1689 CHECK_EQ(5, break_point_hit_count);
1690
iposva@chromium.org245aa852009-02-10 00:49:54 +00001691 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001692 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001693}
1694
1695
1696// Test that script break points survive when a script is reloaded.
1697TEST(ScriptBreakPointReload) {
1698 break_point_hit_count = 0;
1699 v8::HandleScope scope;
1700 DebugLocalContext env;
1701 env.ExposeDebug();
1702
iposva@chromium.org245aa852009-02-10 00:49:54 +00001703 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001704 v8::Undefined());
1705
1706 v8::Local<v8::Function> f;
1707 v8::Local<v8::String> script = v8::String::New(
1708 "function f() {\n"
1709 " function h() {\n"
1710 " a = 0; // line 2\n"
1711 " }\n"
1712 " b = 1; // line 4\n"
1713 " return h();\n"
1714 "}");
1715
1716 v8::ScriptOrigin origin_1 = v8::ScriptOrigin(v8::String::New("1"));
1717 v8::ScriptOrigin origin_2 = v8::ScriptOrigin(v8::String::New("2"));
1718
1719 // Set a script break point before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001720 SetScriptBreakPointByNameFromJS("1", 2, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001721
1722 // Compile the script and get the function.
1723 v8::Script::Compile(script, &origin_1)->Run();
1724 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1725
1726 // Call f and check that the script break point is active.
1727 break_point_hit_count = 0;
1728 f->Call(env->Global(), 0, NULL);
1729 CHECK_EQ(1, break_point_hit_count);
1730
1731 // Compile the script again with a different script data and get the
1732 // function.
1733 v8::Script::Compile(script, &origin_2)->Run();
1734 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1735
1736 // Call f and check that no break points are set.
1737 break_point_hit_count = 0;
1738 f->Call(env->Global(), 0, NULL);
1739 CHECK_EQ(0, break_point_hit_count);
1740
1741 // Compile the script again and get the function.
1742 v8::Script::Compile(script, &origin_1)->Run();
1743 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1744
1745 // Call f and check that the script break point is active.
1746 break_point_hit_count = 0;
1747 f->Call(env->Global(), 0, NULL);
1748 CHECK_EQ(1, break_point_hit_count);
1749
iposva@chromium.org245aa852009-02-10 00:49:54 +00001750 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001751 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001752}
1753
1754
1755// Test when several scripts has the same script data
1756TEST(ScriptBreakPointMultiple) {
1757 break_point_hit_count = 0;
1758 v8::HandleScope scope;
1759 DebugLocalContext env;
1760 env.ExposeDebug();
1761
iposva@chromium.org245aa852009-02-10 00:49:54 +00001762 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001763 v8::Undefined());
1764
1765 v8::Local<v8::Function> f;
1766 v8::Local<v8::String> script_f = v8::String::New(
1767 "function f() {\n"
1768 " a = 0; // line 1\n"
1769 "}");
1770
1771 v8::Local<v8::Function> g;
1772 v8::Local<v8::String> script_g = v8::String::New(
1773 "function g() {\n"
1774 " b = 0; // line 1\n"
1775 "}");
1776
1777 v8::ScriptOrigin origin =
1778 v8::ScriptOrigin(v8::String::New("test"));
1779
1780 // Set a script break point before the scripts are loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001781 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001782
1783 // Compile the scripts with same script data and get the functions.
1784 v8::Script::Compile(script_f, &origin)->Run();
1785 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1786 v8::Script::Compile(script_g, &origin)->Run();
1787 g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1788
1789 // Call f and g and check that the script break point is active.
1790 break_point_hit_count = 0;
1791 f->Call(env->Global(), 0, NULL);
1792 CHECK_EQ(1, break_point_hit_count);
1793 g->Call(env->Global(), 0, NULL);
1794 CHECK_EQ(2, break_point_hit_count);
1795
1796 // Clear the script break point.
1797 ClearBreakPointFromJS(sbp);
1798
1799 // Call f and g and check that the script break point is no longer active.
1800 break_point_hit_count = 0;
1801 f->Call(env->Global(), 0, NULL);
1802 CHECK_EQ(0, break_point_hit_count);
1803 g->Call(env->Global(), 0, NULL);
1804 CHECK_EQ(0, break_point_hit_count);
1805
1806 // Set script break point with the scripts loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001807 sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001808
1809 // Call f and g and check that the script break point is active.
1810 break_point_hit_count = 0;
1811 f->Call(env->Global(), 0, NULL);
1812 CHECK_EQ(1, break_point_hit_count);
1813 g->Call(env->Global(), 0, NULL);
1814 CHECK_EQ(2, break_point_hit_count);
1815
iposva@chromium.org245aa852009-02-10 00:49:54 +00001816 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001817 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001818}
1819
1820
1821// Test the script origin which has both name and line offset.
1822TEST(ScriptBreakPointLineOffset) {
1823 break_point_hit_count = 0;
1824 v8::HandleScope scope;
1825 DebugLocalContext env;
1826 env.ExposeDebug();
1827
iposva@chromium.org245aa852009-02-10 00:49:54 +00001828 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001829 v8::Undefined());
1830
1831 v8::Local<v8::Function> f;
1832 v8::Local<v8::String> script = v8::String::New(
1833 "function f() {\n"
1834 " a = 0; // line 8 as this script has line offset 7\n"
1835 " b = 0; // line 9 as this script has line offset 7\n"
1836 "}");
1837
1838 // Create script origin both name and line offset.
1839 v8::ScriptOrigin origin(v8::String::New("test.html"),
1840 v8::Integer::New(7));
1841
1842 // Set two script break points before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001843 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 8, 0);
1844 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 9, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001845
1846 // Compile the script and get the function.
1847 v8::Script::Compile(script, &origin)->Run();
1848 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1849
1850 // Call f and check that the script break point is active.
1851 break_point_hit_count = 0;
1852 f->Call(env->Global(), 0, NULL);
1853 CHECK_EQ(2, break_point_hit_count);
1854
1855 // Clear the script break points.
1856 ClearBreakPointFromJS(sbp1);
1857 ClearBreakPointFromJS(sbp2);
1858
1859 // Call f and check that no script break points are active.
1860 break_point_hit_count = 0;
1861 f->Call(env->Global(), 0, NULL);
1862 CHECK_EQ(0, break_point_hit_count);
1863
1864 // Set a script break point with the script loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001865 sbp1 = SetScriptBreakPointByNameFromJS("test.html", 9, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001866
1867 // Call f and check that the script break point is active.
1868 break_point_hit_count = 0;
1869 f->Call(env->Global(), 0, NULL);
1870 CHECK_EQ(1, break_point_hit_count);
1871
iposva@chromium.org245aa852009-02-10 00:49:54 +00001872 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001873 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001874}
1875
1876
1877// Test script break points set on lines.
1878TEST(ScriptBreakPointLine) {
1879 v8::HandleScope scope;
1880 DebugLocalContext env;
1881 env.ExposeDebug();
1882
1883 // Create a function for checking the function when hitting a break point.
1884 frame_function_name = CompileFunction(&env,
1885 frame_function_name_source,
1886 "frame_function_name");
1887
iposva@chromium.org245aa852009-02-10 00:49:54 +00001888 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001889 v8::Undefined());
1890
1891 v8::Local<v8::Function> f;
1892 v8::Local<v8::Function> g;
1893 v8::Local<v8::String> script = v8::String::New(
1894 "a = 0 // line 0\n"
1895 "function f() {\n"
1896 " a = 1; // line 2\n"
1897 "}\n"
1898 " a = 2; // line 4\n"
1899 " /* xx */ function g() { // line 5\n"
1900 " function h() { // line 6\n"
1901 " a = 3; // line 7\n"
1902 " }\n"
1903 " h(); // line 9\n"
1904 " a = 4; // line 10\n"
1905 " }\n"
1906 " a=5; // line 12");
1907
1908 // Set a couple script break point before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001909 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 0, -1);
1910 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 1, -1);
1911 int sbp3 = SetScriptBreakPointByNameFromJS("test.html", 5, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001912
1913 // Compile the script and get the function.
1914 break_point_hit_count = 0;
1915 v8::ScriptOrigin origin(v8::String::New("test.html"), v8::Integer::New(0));
1916 v8::Script::Compile(script, &origin)->Run();
1917 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1918 g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1919
1920 // Chesk that a break point was hit when the script was run.
1921 CHECK_EQ(1, break_point_hit_count);
1922 CHECK_EQ(0, strlen(last_function_hit));
1923
1924 // Call f and check that the script break point.
1925 f->Call(env->Global(), 0, NULL);
1926 CHECK_EQ(2, break_point_hit_count);
1927 CHECK_EQ("f", last_function_hit);
1928
1929 // Call g and check that the script break point.
1930 g->Call(env->Global(), 0, NULL);
1931 CHECK_EQ(3, break_point_hit_count);
1932 CHECK_EQ("g", last_function_hit);
1933
1934 // Clear the script break point on g and set one on h.
1935 ClearBreakPointFromJS(sbp3);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001936 int sbp4 = SetScriptBreakPointByNameFromJS("test.html", 6, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001937
1938 // Call g and check that the script break point in h is hit.
1939 g->Call(env->Global(), 0, NULL);
1940 CHECK_EQ(4, break_point_hit_count);
1941 CHECK_EQ("h", last_function_hit);
1942
1943 // Clear break points in f and h. Set a new one in the script between
1944 // functions f and g and test that there is no break points in f and g any
1945 // more.
1946 ClearBreakPointFromJS(sbp2);
1947 ClearBreakPointFromJS(sbp4);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001948 int sbp5 = SetScriptBreakPointByNameFromJS("test.html", 4, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001949 break_point_hit_count = 0;
1950 f->Call(env->Global(), 0, NULL);
1951 g->Call(env->Global(), 0, NULL);
1952 CHECK_EQ(0, break_point_hit_count);
1953
1954 // Reload the script which should hit two break points.
1955 break_point_hit_count = 0;
1956 v8::Script::Compile(script, &origin)->Run();
1957 CHECK_EQ(2, break_point_hit_count);
1958 CHECK_EQ(0, strlen(last_function_hit));
1959
1960 // Set a break point in the code after the last function decleration.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001961 int sbp6 = SetScriptBreakPointByNameFromJS("test.html", 12, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001962
1963 // Reload the script which should hit three break points.
1964 break_point_hit_count = 0;
1965 v8::Script::Compile(script, &origin)->Run();
1966 CHECK_EQ(3, break_point_hit_count);
1967 CHECK_EQ(0, strlen(last_function_hit));
1968
1969 // Clear the last break points, and reload the script which should not hit any
1970 // break points.
1971 ClearBreakPointFromJS(sbp1);
1972 ClearBreakPointFromJS(sbp5);
1973 ClearBreakPointFromJS(sbp6);
1974 break_point_hit_count = 0;
1975 v8::Script::Compile(script, &origin)->Run();
1976 CHECK_EQ(0, break_point_hit_count);
1977
iposva@chromium.org245aa852009-02-10 00:49:54 +00001978 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001979 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001980}
1981
1982
1983// Test that it is possible to remove the last break point for a function
1984// inside the break handling of that break point.
1985TEST(RemoveBreakPointInBreak) {
1986 v8::HandleScope scope;
1987 DebugLocalContext env;
1988
1989 v8::Local<v8::Function> foo =
1990 CompileFunction(&env, "function foo(){a=1;}", "foo");
1991 debug_event_remove_break_point = SetBreakPoint(foo, 0);
1992
1993 // Register the debug event listener pasing the function
iposva@chromium.org245aa852009-02-10 00:49:54 +00001994 v8::Debug::SetDebugEventListener(DebugEventRemoveBreakPoint, foo);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001995
1996 break_point_hit_count = 0;
1997 foo->Call(env->Global(), 0, NULL);
1998 CHECK_EQ(1, break_point_hit_count);
1999
2000 break_point_hit_count = 0;
2001 foo->Call(env->Global(), 0, NULL);
2002 CHECK_EQ(0, break_point_hit_count);
2003
iposva@chromium.org245aa852009-02-10 00:49:54 +00002004 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002005 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002006}
2007
2008
2009// Test that the debugger statement causes a break.
2010TEST(DebuggerStatement) {
2011 break_point_hit_count = 0;
2012 v8::HandleScope scope;
2013 DebugLocalContext env;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002014 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002015 v8::Undefined());
2016 v8::Script::Compile(v8::String::New("function bar(){debugger}"))->Run();
2017 v8::Script::Compile(v8::String::New(
2018 "function foo(){debugger;debugger;}"))->Run();
2019 v8::Local<v8::Function> foo =
2020 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
2021 v8::Local<v8::Function> bar =
2022 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("bar")));
2023
2024 // Run function with debugger statement
2025 bar->Call(env->Global(), 0, NULL);
2026 CHECK_EQ(1, break_point_hit_count);
2027
2028 // Run function with two debugger statement
2029 foo->Call(env->Global(), 0, NULL);
2030 CHECK_EQ(3, break_point_hit_count);
2031
iposva@chromium.org245aa852009-02-10 00:49:54 +00002032 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002033 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002034}
2035
2036
2037// Thest that the evaluation of expressions when a break point is hit generates
2038// the correct results.
2039TEST(DebugEvaluate) {
2040 v8::HandleScope scope;
2041 DebugLocalContext env;
2042 env.ExposeDebug();
2043
2044 // Create a function for checking the evaluation when hitting a break point.
2045 evaluate_check_function = CompileFunction(&env,
2046 evaluate_check_source,
2047 "evaluate_check");
2048 // Register the debug event listener
iposva@chromium.org245aa852009-02-10 00:49:54 +00002049 v8::Debug::SetDebugEventListener(DebugEventEvaluate);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002050
2051 // Different expected vaules of x and a when in a break point (u = undefined,
2052 // d = Hello, world!).
2053 struct EvaluateCheck checks_uu[] = {
2054 {"x", v8::Undefined()},
2055 {"a", v8::Undefined()},
2056 {NULL, v8::Handle<v8::Value>()}
2057 };
2058 struct EvaluateCheck checks_hu[] = {
2059 {"x", v8::String::New("Hello, world!")},
2060 {"a", v8::Undefined()},
2061 {NULL, v8::Handle<v8::Value>()}
2062 };
2063 struct EvaluateCheck checks_hh[] = {
2064 {"x", v8::String::New("Hello, world!")},
2065 {"a", v8::String::New("Hello, world!")},
2066 {NULL, v8::Handle<v8::Value>()}
2067 };
2068
2069 // Simple test function. The "y=0" is in the function foo to provide a break
2070 // location. For "y=0" the "y" is at position 15 in the barbar function
2071 // therefore setting breakpoint at position 15 will break at "y=0" and
2072 // setting it higher will break after.
2073 v8::Local<v8::Function> foo = CompileFunction(&env,
2074 "function foo(x) {"
2075 " var a;"
2076 " y=0; /* To ensure break location.*/"
2077 " a=x;"
2078 "}",
2079 "foo");
2080 const int foo_break_position = 15;
2081
2082 // Arguments with one parameter "Hello, world!"
2083 v8::Handle<v8::Value> argv_foo[1] = { v8::String::New("Hello, world!") };
2084
2085 // Call foo with breakpoint set before a=x and undefined as parameter.
2086 int bp = SetBreakPoint(foo, foo_break_position);
2087 checks = checks_uu;
2088 foo->Call(env->Global(), 0, NULL);
2089
2090 // Call foo with breakpoint set before a=x and parameter "Hello, world!".
2091 checks = checks_hu;
2092 foo->Call(env->Global(), 1, argv_foo);
2093
2094 // Call foo with breakpoint set after a=x and parameter "Hello, world!".
2095 ClearBreakPoint(bp);
2096 SetBreakPoint(foo, foo_break_position + 1);
2097 checks = checks_hh;
2098 foo->Call(env->Global(), 1, argv_foo);
2099
2100 // Test function with an inner function. The "y=0" is in function barbar
2101 // to provide a break location. For "y=0" the "y" is at position 8 in the
2102 // barbar function therefore setting breakpoint at position 8 will break at
2103 // "y=0" and setting it higher will break after.
2104 v8::Local<v8::Function> bar = CompileFunction(&env,
2105 "y = 0;"
2106 "x = 'Goodbye, world!';"
2107 "function bar(x, b) {"
2108 " var a;"
2109 " function barbar() {"
2110 " y=0; /* To ensure break location.*/"
2111 " a=x;"
2112 " };"
2113 " debug.Debug.clearAllBreakPoints();"
2114 " barbar();"
2115 " y=0;a=x;"
2116 "}",
2117 "bar");
2118 const int barbar_break_position = 8;
2119
2120 // Call bar setting breakpoint before a=x in barbar and undefined as
2121 // parameter.
2122 checks = checks_uu;
2123 v8::Handle<v8::Value> argv_bar_1[2] = {
2124 v8::Undefined(),
2125 v8::Number::New(barbar_break_position)
2126 };
2127 bar->Call(env->Global(), 2, argv_bar_1);
2128
2129 // Call bar setting breakpoint before a=x in barbar and parameter
2130 // "Hello, world!".
2131 checks = checks_hu;
2132 v8::Handle<v8::Value> argv_bar_2[2] = {
2133 v8::String::New("Hello, world!"),
2134 v8::Number::New(barbar_break_position)
2135 };
2136 bar->Call(env->Global(), 2, argv_bar_2);
2137
2138 // Call bar setting breakpoint after a=x in barbar and parameter
2139 // "Hello, world!".
2140 checks = checks_hh;
2141 v8::Handle<v8::Value> argv_bar_3[2] = {
2142 v8::String::New("Hello, world!"),
2143 v8::Number::New(barbar_break_position + 1)
2144 };
2145 bar->Call(env->Global(), 2, argv_bar_3);
2146
iposva@chromium.org245aa852009-02-10 00:49:54 +00002147 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002148 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002149}
2150
2151
2152// Simple test of the stepping mechanism using only store ICs.
2153TEST(DebugStepLinear) {
2154 v8::HandleScope scope;
2155 DebugLocalContext env;
2156
2157 // Create a function for testing stepping.
2158 v8::Local<v8::Function> foo = CompileFunction(&env,
2159 "function foo(){a=1;b=1;c=1;}",
2160 "foo");
2161 SetBreakPoint(foo, 3);
2162
2163 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002164 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002165
2166 step_action = StepIn;
2167 break_point_hit_count = 0;
2168 foo->Call(env->Global(), 0, NULL);
2169
2170 // With stepping all break locations are hit.
2171 CHECK_EQ(4, break_point_hit_count);
2172
iposva@chromium.org245aa852009-02-10 00:49:54 +00002173 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002174 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002175
2176 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002177 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002178
ager@chromium.org381abbb2009-02-25 13:23:22 +00002179 SetBreakPoint(foo, 3);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002180 break_point_hit_count = 0;
2181 foo->Call(env->Global(), 0, NULL);
2182
2183 // Without stepping only active break points are hit.
2184 CHECK_EQ(1, break_point_hit_count);
2185
iposva@chromium.org245aa852009-02-10 00:49:54 +00002186 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002187 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002188}
2189
2190
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002191// Test of the stepping mechanism for keyed load in a loop.
2192TEST(DebugStepKeyedLoadLoop) {
2193 v8::HandleScope scope;
2194 DebugLocalContext env;
2195
2196 // Create a function for testing stepping of keyed load. The statement 'y=1'
2197 // is there to have more than one breakable statement in the loop, TODO(315).
2198 v8::Local<v8::Function> foo = CompileFunction(
2199 &env,
2200 "function foo(a) {\n"
2201 " var x;\n"
2202 " var len = a.length;\n"
2203 " for (var i = 0; i < len; i++) {\n"
2204 " y = 1;\n"
2205 " x = a[i];\n"
2206 " }\n"
2207 "}\n",
2208 "foo");
2209
2210 // Create array [0,1,2,3,4,5,6,7,8,9]
2211 v8::Local<v8::Array> a = v8::Array::New(10);
2212 for (int i = 0; i < 10; i++) {
2213 a->Set(v8::Number::New(i), v8::Number::New(i));
2214 }
2215
2216 // Call function without any break points to ensure inlining is in place.
2217 const int kArgc = 1;
2218 v8::Handle<v8::Value> args[kArgc] = { a };
2219 foo->Call(env->Global(), kArgc, args);
2220
2221 // Register a debug event listener which steps and counts.
2222 v8::Debug::SetDebugEventListener(DebugEventStep);
2223
2224 // Setup break point and step through the function.
2225 SetBreakPoint(foo, 3);
2226 step_action = StepNext;
2227 break_point_hit_count = 0;
2228 foo->Call(env->Global(), kArgc, args);
2229
2230 // With stepping all break locations are hit.
2231 CHECK_EQ(22, break_point_hit_count);
2232
2233 v8::Debug::SetDebugEventListener(NULL);
2234 CheckDebuggerUnloaded();
2235}
2236
2237
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002238// Test the stepping mechanism with different ICs.
2239TEST(DebugStepLinearMixedICs) {
2240 v8::HandleScope scope;
2241 DebugLocalContext env;
2242
2243 // Create a function for testing stepping.
2244 v8::Local<v8::Function> foo = CompileFunction(&env,
2245 "function bar() {};"
2246 "function foo() {"
2247 " var x;"
2248 " var index='name';"
2249 " var y = {};"
2250 " a=1;b=2;x=a;y[index]=3;x=y[index];bar();}", "foo");
2251 SetBreakPoint(foo, 0);
2252
2253 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002254 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002255
2256 step_action = StepIn;
2257 break_point_hit_count = 0;
2258 foo->Call(env->Global(), 0, NULL);
2259
2260 // With stepping all break locations are hit. For ARM the keyed load/store
2261 // is not hit as they are not implemented as ICs.
2262#if defined (__arm__) || defined(__thumb__)
2263 CHECK_EQ(6, break_point_hit_count);
2264#else
2265 CHECK_EQ(8, break_point_hit_count);
2266#endif
2267
iposva@chromium.org245aa852009-02-10 00:49:54 +00002268 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002269 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002270
2271 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002272 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002273
ager@chromium.org381abbb2009-02-25 13:23:22 +00002274 SetBreakPoint(foo, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002275 break_point_hit_count = 0;
2276 foo->Call(env->Global(), 0, NULL);
2277
2278 // Without stepping only active break points are hit.
2279 CHECK_EQ(1, break_point_hit_count);
2280
iposva@chromium.org245aa852009-02-10 00:49:54 +00002281 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002282 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002283}
2284
2285
2286TEST(DebugStepIf) {
2287 v8::HandleScope scope;
2288 DebugLocalContext env;
2289
2290 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002291 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002292
2293 // Create a function for testing stepping.
2294 const int argc = 1;
2295 const char* src = "function foo(x) { "
2296 " a = 1;"
2297 " if (x) {"
2298 " b = 1;"
2299 " } else {"
2300 " c = 1;"
2301 " d = 1;"
2302 " }"
2303 "}";
2304 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
2305 SetBreakPoint(foo, 0);
2306
2307 // Stepping through the true part.
2308 step_action = StepIn;
2309 break_point_hit_count = 0;
2310 v8::Handle<v8::Value> argv_true[argc] = { v8::True() };
2311 foo->Call(env->Global(), argc, argv_true);
2312 CHECK_EQ(3, break_point_hit_count);
2313
2314 // Stepping through the false part.
2315 step_action = StepIn;
2316 break_point_hit_count = 0;
2317 v8::Handle<v8::Value> argv_false[argc] = { v8::False() };
2318 foo->Call(env->Global(), argc, argv_false);
2319 CHECK_EQ(4, break_point_hit_count);
2320
2321 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002322 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002323 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002324}
2325
2326
2327TEST(DebugStepSwitch) {
2328 v8::HandleScope scope;
2329 DebugLocalContext env;
2330
2331 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002332 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002333
2334 // Create a function for testing stepping.
2335 const int argc = 1;
2336 const char* src = "function foo(x) { "
2337 " a = 1;"
2338 " switch (x) {"
2339 " case 1:"
2340 " b = 1;"
2341 " case 2:"
2342 " c = 1;"
2343 " break;"
2344 " case 3:"
2345 " d = 1;"
2346 " e = 1;"
2347 " break;"
2348 " }"
2349 "}";
2350 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
2351 SetBreakPoint(foo, 0);
2352
2353 // One case with fall-through.
2354 step_action = StepIn;
2355 break_point_hit_count = 0;
2356 v8::Handle<v8::Value> argv_1[argc] = { v8::Number::New(1) };
2357 foo->Call(env->Global(), argc, argv_1);
2358 CHECK_EQ(4, break_point_hit_count);
2359
2360 // Another case.
2361 step_action = StepIn;
2362 break_point_hit_count = 0;
2363 v8::Handle<v8::Value> argv_2[argc] = { v8::Number::New(2) };
2364 foo->Call(env->Global(), argc, argv_2);
2365 CHECK_EQ(3, break_point_hit_count);
2366
2367 // Last case.
2368 step_action = StepIn;
2369 break_point_hit_count = 0;
2370 v8::Handle<v8::Value> argv_3[argc] = { v8::Number::New(3) };
2371 foo->Call(env->Global(), argc, argv_3);
2372 CHECK_EQ(4, break_point_hit_count);
2373
2374 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002375 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002376 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002377}
2378
2379
2380TEST(DebugStepFor) {
2381 v8::HandleScope scope;
2382 DebugLocalContext env;
2383
2384 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002385 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002386
2387 // Create a function for testing stepping.
2388 const int argc = 1;
2389 const char* src = "function foo(x) { "
2390 " a = 1;"
2391 " for (i = 0; i < x; i++) {"
2392 " b = 1;"
2393 " }"
2394 "}";
2395 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
2396 SetBreakPoint(foo, 8); // "a = 1;"
2397
2398 // Looping 10 times.
2399 step_action = StepIn;
2400 break_point_hit_count = 0;
2401 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
2402 foo->Call(env->Global(), argc, argv_10);
2403 CHECK_EQ(23, break_point_hit_count);
2404
2405 // Looping 100 times.
2406 step_action = StepIn;
2407 break_point_hit_count = 0;
2408 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
2409 foo->Call(env->Global(), argc, argv_100);
2410 CHECK_EQ(203, break_point_hit_count);
2411
2412 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002413 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002414 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002415}
2416
2417
2418TEST(StepInOutSimple) {
2419 v8::HandleScope scope;
2420 DebugLocalContext env;
2421
2422 // Create a function for checking the function when hitting a break point.
2423 frame_function_name = CompileFunction(&env,
2424 frame_function_name_source,
2425 "frame_function_name");
2426
2427 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002428 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002429
2430 // Create functions for testing stepping.
2431 const char* src = "function a() {b();c();}; "
2432 "function b() {c();}; "
2433 "function c() {}; ";
2434 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
2435 SetBreakPoint(a, 0);
2436
2437 // Step through invocation of a with step in.
2438 step_action = StepIn;
2439 break_point_hit_count = 0;
2440 expected_step_sequence = "abcbaca";
2441 a->Call(env->Global(), 0, NULL);
2442 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2443
2444 // Step through invocation of a with step next.
2445 step_action = StepNext;
2446 break_point_hit_count = 0;
2447 expected_step_sequence = "aaa";
2448 a->Call(env->Global(), 0, NULL);
2449 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2450
2451 // Step through invocation of a with step out.
2452 step_action = StepOut;
2453 break_point_hit_count = 0;
2454 expected_step_sequence = "a";
2455 a->Call(env->Global(), 0, NULL);
2456 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2457
2458 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002459 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002460 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002461}
2462
2463
2464TEST(StepInOutTree) {
2465 v8::HandleScope scope;
2466 DebugLocalContext env;
2467
2468 // Create a function for checking the function when hitting a break point.
2469 frame_function_name = CompileFunction(&env,
2470 frame_function_name_source,
2471 "frame_function_name");
2472
2473 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002474 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002475
2476 // Create functions for testing stepping.
2477 const char* src = "function a() {b(c(d()),d());c(d());d()}; "
2478 "function b(x,y) {c();}; "
2479 "function c(x) {}; "
2480 "function d() {}; ";
2481 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
2482 SetBreakPoint(a, 0);
2483
2484 // Step through invocation of a with step in.
2485 step_action = StepIn;
2486 break_point_hit_count = 0;
2487 expected_step_sequence = "adacadabcbadacada";
2488 a->Call(env->Global(), 0, NULL);
2489 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2490
2491 // Step through invocation of a with step next.
2492 step_action = StepNext;
2493 break_point_hit_count = 0;
2494 expected_step_sequence = "aaaa";
2495 a->Call(env->Global(), 0, NULL);
2496 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2497
2498 // Step through invocation of a with step out.
2499 step_action = StepOut;
2500 break_point_hit_count = 0;
2501 expected_step_sequence = "a";
2502 a->Call(env->Global(), 0, NULL);
2503 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2504
2505 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002506 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002507 CheckDebuggerUnloaded(true);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002508}
2509
2510
2511TEST(StepInOutBranch) {
2512 v8::HandleScope scope;
2513 DebugLocalContext env;
2514
2515 // Create a function for checking the function when hitting a break point.
2516 frame_function_name = CompileFunction(&env,
2517 frame_function_name_source,
2518 "frame_function_name");
2519
2520 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002521 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002522
2523 // Create functions for testing stepping.
2524 const char* src = "function a() {b(false);c();}; "
2525 "function b(x) {if(x){c();};}; "
2526 "function c() {}; ";
2527 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
2528 SetBreakPoint(a, 0);
2529
2530 // Step through invocation of a.
2531 step_action = StepIn;
2532 break_point_hit_count = 0;
2533 expected_step_sequence = "abaca";
2534 a->Call(env->Global(), 0, NULL);
2535 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2536
2537 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002538 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002539 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002540}
2541
2542
2543// Test that step in does not step into native functions.
2544TEST(DebugStepNatives) {
2545 v8::HandleScope scope;
2546 DebugLocalContext env;
2547
2548 // Create a function for testing stepping.
2549 v8::Local<v8::Function> foo = CompileFunction(
2550 &env,
2551 "function foo(){debugger;Math.sin(1);}",
2552 "foo");
2553
2554 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002555 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002556
2557 step_action = StepIn;
2558 break_point_hit_count = 0;
2559 foo->Call(env->Global(), 0, NULL);
2560
2561 // With stepping all break locations are hit.
2562 CHECK_EQ(3, break_point_hit_count);
2563
iposva@chromium.org245aa852009-02-10 00:49:54 +00002564 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002565 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002566
2567 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002568 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002569
2570 break_point_hit_count = 0;
2571 foo->Call(env->Global(), 0, NULL);
2572
2573 // Without stepping only active break points are hit.
2574 CHECK_EQ(1, break_point_hit_count);
2575
iposva@chromium.org245aa852009-02-10 00:49:54 +00002576 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002577 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002578}
2579
2580
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00002581// Test that step in works with function.apply.
2582TEST(DebugStepFunctionApply) {
2583 v8::HandleScope scope;
2584 DebugLocalContext env;
2585
2586 // Create a function for testing stepping.
2587 v8::Local<v8::Function> foo = CompileFunction(
2588 &env,
2589 "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }"
2590 "function foo(){ debugger; bar.apply(this, [1,2,3]); }",
2591 "foo");
2592
2593 // Register a debug event listener which steps and counts.
2594 v8::Debug::SetDebugEventListener(DebugEventStep);
2595
2596 step_action = StepIn;
2597 break_point_hit_count = 0;
2598 foo->Call(env->Global(), 0, NULL);
2599
2600 // With stepping all break locations are hit.
2601 CHECK_EQ(6, break_point_hit_count);
2602
2603 v8::Debug::SetDebugEventListener(NULL);
2604 CheckDebuggerUnloaded();
2605
2606 // Register a debug event listener which just counts.
2607 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
2608
2609 break_point_hit_count = 0;
2610 foo->Call(env->Global(), 0, NULL);
2611
2612 // Without stepping only the debugger statement is hit.
2613 CHECK_EQ(1, break_point_hit_count);
2614
2615 v8::Debug::SetDebugEventListener(NULL);
2616 CheckDebuggerUnloaded();
2617}
2618
2619
2620// Test that step in works with function.call.
2621TEST(DebugStepFunctionCall) {
2622 v8::HandleScope scope;
2623 DebugLocalContext env;
2624
2625 // Create a function for testing stepping.
2626 v8::Local<v8::Function> foo = CompileFunction(
2627 &env,
2628 "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }"
2629 "function foo(a){ debugger;"
2630 " if (a) {"
2631 " bar.call(this, 1, 2, 3);"
2632 " } else {"
2633 " bar.call(this, 0);"
2634 " }"
2635 "}",
2636 "foo");
2637
2638 // Register a debug event listener which steps and counts.
2639 v8::Debug::SetDebugEventListener(DebugEventStep);
2640 step_action = StepIn;
2641
2642 // Check stepping where the if condition in bar is false.
2643 break_point_hit_count = 0;
2644 foo->Call(env->Global(), 0, NULL);
2645 CHECK_EQ(4, break_point_hit_count);
2646
2647 // Check stepping where the if condition in bar is true.
2648 break_point_hit_count = 0;
2649 const int argc = 1;
2650 v8::Handle<v8::Value> argv[argc] = { v8::True() };
2651 foo->Call(env->Global(), argc, argv);
2652 CHECK_EQ(6, break_point_hit_count);
2653
2654 v8::Debug::SetDebugEventListener(NULL);
2655 CheckDebuggerUnloaded();
2656
2657 // Register a debug event listener which just counts.
2658 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
2659
2660 break_point_hit_count = 0;
2661 foo->Call(env->Global(), 0, NULL);
2662
2663 // Without stepping only the debugger statement is hit.
2664 CHECK_EQ(1, break_point_hit_count);
2665
2666 v8::Debug::SetDebugEventListener(NULL);
2667 CheckDebuggerUnloaded();
2668}
2669
2670
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002671// Test break on exceptions. For each exception break combination the number
2672// of debug event exception callbacks and message callbacks are collected. The
ager@chromium.org8bb60582008-12-11 12:02:20 +00002673// number of debug event exception callbacks are used to check that the
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002674// debugger is called correctly and the number of message callbacks is used to
2675// check that uncaught exceptions are still returned even if there is a break
2676// for them.
2677TEST(BreakOnException) {
2678 v8::HandleScope scope;
2679 DebugLocalContext env;
2680 env.ExposeDebug();
2681
2682 v8::internal::Top::TraceException(false);
2683
2684 // Create functions for testing break on exception.
2685 v8::Local<v8::Function> throws =
2686 CompileFunction(&env, "function throws(){throw 1;}", "throws");
2687 v8::Local<v8::Function> caught =
2688 CompileFunction(&env,
2689 "function caught(){try {throws();} catch(e) {};}",
2690 "caught");
2691 v8::Local<v8::Function> notCaught =
2692 CompileFunction(&env, "function notCaught(){throws();}", "notCaught");
2693
2694 v8::V8::AddMessageListener(MessageCallbackCount);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002695 v8::Debug::SetDebugEventListener(DebugEventCounter);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002696
2697 // Initial state should be break on uncaught exception.
2698 DebugEventCounterClear();
2699 MessageCallbackCountClear();
2700 caught->Call(env->Global(), 0, NULL);
2701 CHECK_EQ(0, exception_hit_count);
2702 CHECK_EQ(0, uncaught_exception_hit_count);
2703 CHECK_EQ(0, message_callback_count);
2704 notCaught->Call(env->Global(), 0, NULL);
2705 CHECK_EQ(1, exception_hit_count);
2706 CHECK_EQ(1, uncaught_exception_hit_count);
2707 CHECK_EQ(1, message_callback_count);
2708
2709 // No break on exception
2710 DebugEventCounterClear();
2711 MessageCallbackCountClear();
2712 ChangeBreakOnException(false, false);
2713 caught->Call(env->Global(), 0, NULL);
2714 CHECK_EQ(0, exception_hit_count);
2715 CHECK_EQ(0, uncaught_exception_hit_count);
2716 CHECK_EQ(0, message_callback_count);
2717 notCaught->Call(env->Global(), 0, NULL);
2718 CHECK_EQ(0, exception_hit_count);
2719 CHECK_EQ(0, uncaught_exception_hit_count);
2720 CHECK_EQ(1, message_callback_count);
2721
2722 // Break on uncaught exception
2723 DebugEventCounterClear();
2724 MessageCallbackCountClear();
2725 ChangeBreakOnException(false, true);
2726 caught->Call(env->Global(), 0, NULL);
2727 CHECK_EQ(0, exception_hit_count);
2728 CHECK_EQ(0, uncaught_exception_hit_count);
2729 CHECK_EQ(0, message_callback_count);
2730 notCaught->Call(env->Global(), 0, NULL);
2731 CHECK_EQ(1, exception_hit_count);
2732 CHECK_EQ(1, uncaught_exception_hit_count);
2733 CHECK_EQ(1, message_callback_count);
2734
2735 // Break on exception and uncaught exception
2736 DebugEventCounterClear();
2737 MessageCallbackCountClear();
2738 ChangeBreakOnException(true, true);
2739 caught->Call(env->Global(), 0, NULL);
2740 CHECK_EQ(1, exception_hit_count);
2741 CHECK_EQ(0, uncaught_exception_hit_count);
2742 CHECK_EQ(0, message_callback_count);
2743 notCaught->Call(env->Global(), 0, NULL);
2744 CHECK_EQ(2, exception_hit_count);
2745 CHECK_EQ(1, uncaught_exception_hit_count);
2746 CHECK_EQ(1, message_callback_count);
2747
2748 // Break on exception
2749 DebugEventCounterClear();
2750 MessageCallbackCountClear();
2751 ChangeBreakOnException(true, false);
2752 caught->Call(env->Global(), 0, NULL);
2753 CHECK_EQ(1, exception_hit_count);
2754 CHECK_EQ(0, uncaught_exception_hit_count);
2755 CHECK_EQ(0, message_callback_count);
2756 notCaught->Call(env->Global(), 0, NULL);
2757 CHECK_EQ(2, exception_hit_count);
2758 CHECK_EQ(1, uncaught_exception_hit_count);
2759 CHECK_EQ(1, message_callback_count);
2760
2761 // No break on exception using JavaScript
2762 DebugEventCounterClear();
2763 MessageCallbackCountClear();
2764 ChangeBreakOnExceptionFromJS(false, false);
2765 caught->Call(env->Global(), 0, NULL);
2766 CHECK_EQ(0, exception_hit_count);
2767 CHECK_EQ(0, uncaught_exception_hit_count);
2768 CHECK_EQ(0, message_callback_count);
2769 notCaught->Call(env->Global(), 0, NULL);
2770 CHECK_EQ(0, exception_hit_count);
2771 CHECK_EQ(0, uncaught_exception_hit_count);
2772 CHECK_EQ(1, message_callback_count);
2773
2774 // Break on uncaught exception using JavaScript
2775 DebugEventCounterClear();
2776 MessageCallbackCountClear();
2777 ChangeBreakOnExceptionFromJS(false, true);
2778 caught->Call(env->Global(), 0, NULL);
2779 CHECK_EQ(0, exception_hit_count);
2780 CHECK_EQ(0, uncaught_exception_hit_count);
2781 CHECK_EQ(0, message_callback_count);
2782 notCaught->Call(env->Global(), 0, NULL);
2783 CHECK_EQ(1, exception_hit_count);
2784 CHECK_EQ(1, uncaught_exception_hit_count);
2785 CHECK_EQ(1, message_callback_count);
2786
2787 // Break on exception and uncaught exception using JavaScript
2788 DebugEventCounterClear();
2789 MessageCallbackCountClear();
2790 ChangeBreakOnExceptionFromJS(true, true);
2791 caught->Call(env->Global(), 0, NULL);
2792 CHECK_EQ(1, exception_hit_count);
2793 CHECK_EQ(0, message_callback_count);
2794 CHECK_EQ(0, uncaught_exception_hit_count);
2795 notCaught->Call(env->Global(), 0, NULL);
2796 CHECK_EQ(2, exception_hit_count);
2797 CHECK_EQ(1, uncaught_exception_hit_count);
2798 CHECK_EQ(1, message_callback_count);
2799
2800 // Break on exception using JavaScript
2801 DebugEventCounterClear();
2802 MessageCallbackCountClear();
2803 ChangeBreakOnExceptionFromJS(true, false);
2804 caught->Call(env->Global(), 0, NULL);
2805 CHECK_EQ(1, exception_hit_count);
2806 CHECK_EQ(0, uncaught_exception_hit_count);
2807 CHECK_EQ(0, message_callback_count);
2808 notCaught->Call(env->Global(), 0, NULL);
2809 CHECK_EQ(2, exception_hit_count);
2810 CHECK_EQ(1, uncaught_exception_hit_count);
2811 CHECK_EQ(1, message_callback_count);
2812
iposva@chromium.org245aa852009-02-10 00:49:54 +00002813 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002814 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002815 v8::V8::RemoveMessageListeners(MessageCallbackCount);
2816}
2817
2818
ager@chromium.org8bb60582008-12-11 12:02:20 +00002819// Test break on exception from compiler errors. When compiling using
2820// v8::Script::Compile there is no JavaScript stack whereas when compiling using
2821// eval there are JavaScript frames.
2822TEST(BreakOnCompileException) {
2823 v8::HandleScope scope;
2824 DebugLocalContext env;
2825
2826 v8::internal::Top::TraceException(false);
2827
2828 // Create a function for checking the function when hitting a break point.
2829 frame_count = CompileFunction(&env, frame_count_source, "frame_count");
2830
2831 v8::V8::AddMessageListener(MessageCallbackCount);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002832 v8::Debug::SetDebugEventListener(DebugEventCounter);
ager@chromium.org8bb60582008-12-11 12:02:20 +00002833
2834 DebugEventCounterClear();
2835 MessageCallbackCountClear();
2836
2837 // Check initial state.
2838 CHECK_EQ(0, exception_hit_count);
2839 CHECK_EQ(0, uncaught_exception_hit_count);
2840 CHECK_EQ(0, message_callback_count);
2841 CHECK_EQ(-1, last_js_stack_height);
2842
2843 // Throws SyntaxError: Unexpected end of input
2844 v8::Script::Compile(v8::String::New("+++"));
2845 CHECK_EQ(1, exception_hit_count);
2846 CHECK_EQ(1, uncaught_exception_hit_count);
2847 CHECK_EQ(1, message_callback_count);
2848 CHECK_EQ(0, last_js_stack_height); // No JavaScript stack.
2849
2850 // Throws SyntaxError: Unexpected identifier
2851 v8::Script::Compile(v8::String::New("x x"));
2852 CHECK_EQ(2, exception_hit_count);
2853 CHECK_EQ(2, uncaught_exception_hit_count);
2854 CHECK_EQ(2, message_callback_count);
2855 CHECK_EQ(0, last_js_stack_height); // No JavaScript stack.
2856
2857 // Throws SyntaxError: Unexpected end of input
2858 v8::Script::Compile(v8::String::New("eval('+++')"))->Run();
2859 CHECK_EQ(3, exception_hit_count);
2860 CHECK_EQ(3, uncaught_exception_hit_count);
2861 CHECK_EQ(3, message_callback_count);
2862 CHECK_EQ(1, last_js_stack_height);
2863
2864 // Throws SyntaxError: Unexpected identifier
2865 v8::Script::Compile(v8::String::New("eval('x x')"))->Run();
2866 CHECK_EQ(4, exception_hit_count);
2867 CHECK_EQ(4, uncaught_exception_hit_count);
2868 CHECK_EQ(4, message_callback_count);
2869 CHECK_EQ(1, last_js_stack_height);
2870}
2871
2872
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002873TEST(StepWithException) {
2874 v8::HandleScope scope;
2875 DebugLocalContext env;
2876
2877 // Create a function for checking the function when hitting a break point.
2878 frame_function_name = CompileFunction(&env,
2879 frame_function_name_source,
2880 "frame_function_name");
2881
2882 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002883 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002884
2885 // Create functions for testing stepping.
2886 const char* src = "function a() { n(); }; "
2887 "function b() { c(); }; "
2888 "function c() { n(); }; "
2889 "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; "
2890 "function e() { n(); }; "
2891 "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; "
2892 "function g() { h(); }; "
2893 "function h() { x = 1; throw 1; }; ";
2894
2895 // Step through invocation of a.
2896 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
2897 SetBreakPoint(a, 0);
2898 step_action = StepIn;
2899 break_point_hit_count = 0;
2900 expected_step_sequence = "aa";
2901 a->Call(env->Global(), 0, NULL);
2902 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2903
2904 // Step through invocation of b + c.
2905 v8::Local<v8::Function> b = CompileFunction(&env, src, "b");
2906 SetBreakPoint(b, 0);
2907 step_action = StepIn;
2908 break_point_hit_count = 0;
2909 expected_step_sequence = "bcc";
2910 b->Call(env->Global(), 0, NULL);
2911 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2912
2913 // Step through invocation of d + e.
2914 v8::Local<v8::Function> d = CompileFunction(&env, src, "d");
2915 SetBreakPoint(d, 0);
2916 ChangeBreakOnException(false, true);
2917 step_action = StepIn;
2918 break_point_hit_count = 0;
ager@chromium.org32912102009-01-16 10:38:43 +00002919 expected_step_sequence = "dded";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002920 d->Call(env->Global(), 0, NULL);
2921 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2922
2923 // Step through invocation of d + e now with break on caught exceptions.
2924 ChangeBreakOnException(true, true);
2925 step_action = StepIn;
2926 break_point_hit_count = 0;
ager@chromium.org32912102009-01-16 10:38:43 +00002927 expected_step_sequence = "ddeed";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002928 d->Call(env->Global(), 0, NULL);
2929 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2930
2931 // Step through invocation of f + g + h.
2932 v8::Local<v8::Function> f = CompileFunction(&env, src, "f");
2933 SetBreakPoint(f, 0);
2934 ChangeBreakOnException(false, true);
2935 step_action = StepIn;
2936 break_point_hit_count = 0;
ager@chromium.org32912102009-01-16 10:38:43 +00002937 expected_step_sequence = "ffghf";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002938 f->Call(env->Global(), 0, NULL);
2939 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2940
2941 // Step through invocation of f + g + h now with break on caught exceptions.
2942 ChangeBreakOnException(true, true);
2943 step_action = StepIn;
2944 break_point_hit_count = 0;
ager@chromium.org32912102009-01-16 10:38:43 +00002945 expected_step_sequence = "ffghhf";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002946 f->Call(env->Global(), 0, NULL);
2947 CHECK_EQ(strlen(expected_step_sequence), break_point_hit_count);
2948
2949 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002950 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002951 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002952}
2953
2954
2955TEST(DebugBreak) {
2956 v8::HandleScope scope;
2957 DebugLocalContext env;
2958
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002959 // This test should be run with option --verify-heap. As --verify-heap is
2960 // only available in debug mode only check for it in that case.
2961#ifdef DEBUG
2962 CHECK(v8::internal::FLAG_verify_heap);
2963#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002964
2965 // Register a debug event listener which sets the break flag and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002966 v8::Debug::SetDebugEventListener(DebugEventBreak);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002967
2968 // Create a function for testing stepping.
2969 const char* src = "function f0() {}"
2970 "function f1(x1) {}"
2971 "function f2(x1,x2) {}"
2972 "function f3(x1,x2,x3) {}";
2973 v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0");
2974 v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1");
2975 v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2");
2976 v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3");
2977
2978 // Call the function to make sure it is compiled.
2979 v8::Handle<v8::Value> argv[] = { v8::Number::New(1),
2980 v8::Number::New(1),
2981 v8::Number::New(1),
2982 v8::Number::New(1) };
2983
2984 // Call all functions to make sure that they are compiled.
2985 f0->Call(env->Global(), 0, NULL);
2986 f1->Call(env->Global(), 0, NULL);
2987 f2->Call(env->Global(), 0, NULL);
2988 f3->Call(env->Global(), 0, NULL);
2989
2990 // Set the debug break flag.
2991 v8::Debug::DebugBreak();
2992
2993 // Call all functions with different argument count.
2994 break_point_hit_count = 0;
2995 for (unsigned int i = 0; i < ARRAY_SIZE(argv); i++) {
2996 f0->Call(env->Global(), i, argv);
2997 f1->Call(env->Global(), i, argv);
2998 f2->Call(env->Global(), i, argv);
2999 f3->Call(env->Global(), i, argv);
3000 }
3001
3002 // One break for each function called.
3003 CHECK_EQ(4 * ARRAY_SIZE(argv), break_point_hit_count);
3004
3005 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003006 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003007 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003008}
3009
3010
3011// Test to ensure that JavaScript code keeps running while the debug break
3012// through the stack limit flag is set but breaks are disabled.
3013TEST(DisableBreak) {
3014 v8::HandleScope scope;
3015 DebugLocalContext env;
3016
3017 // Register a debug event listener which sets the break flag and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003018 v8::Debug::SetDebugEventListener(DebugEventCounter);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003019
3020 // Create a function for testing stepping.
3021 const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}";
3022 v8::Local<v8::Function> f = CompileFunction(&env, src, "f");
3023
3024 // Set the debug break flag.
3025 v8::Debug::DebugBreak();
3026
3027 // Call all functions with different argument count.
3028 break_point_hit_count = 0;
3029 f->Call(env->Global(), 0, NULL);
3030 CHECK_EQ(1, break_point_hit_count);
3031
3032 {
3033 v8::Debug::DebugBreak();
3034 v8::internal::DisableBreak disable_break(true);
3035 f->Call(env->Global(), 0, NULL);
3036 CHECK_EQ(1, break_point_hit_count);
3037 }
3038
3039 f->Call(env->Global(), 0, NULL);
3040 CHECK_EQ(2, break_point_hit_count);
3041
3042 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003043 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003044 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003045}
3046
3047
3048static v8::Handle<v8::Array> NamedEnum(const v8::AccessorInfo&) {
3049 v8::Handle<v8::Array> result = v8::Array::New(3);
3050 result->Set(v8::Integer::New(0), v8::String::New("a"));
3051 result->Set(v8::Integer::New(1), v8::String::New("b"));
3052 result->Set(v8::Integer::New(2), v8::String::New("c"));
3053 return result;
3054}
3055
3056
3057static v8::Handle<v8::Array> IndexedEnum(const v8::AccessorInfo&) {
3058 v8::Handle<v8::Array> result = v8::Array::New(2);
3059 result->Set(v8::Integer::New(0), v8::Number::New(1));
3060 result->Set(v8::Integer::New(1), v8::Number::New(10));
3061 return result;
3062}
3063
3064
3065static v8::Handle<v8::Value> NamedGetter(v8::Local<v8::String> name,
3066 const v8::AccessorInfo& info) {
3067 v8::String::AsciiValue n(name);
3068 if (strcmp(*n, "a") == 0) {
3069 return v8::String::New("AA");
3070 } else if (strcmp(*n, "b") == 0) {
3071 return v8::String::New("BB");
3072 } else if (strcmp(*n, "c") == 0) {
3073 return v8::String::New("CC");
3074 } else {
3075 return v8::Undefined();
3076 }
3077
3078 return name;
3079}
3080
3081
3082static v8::Handle<v8::Value> IndexedGetter(uint32_t index,
3083 const v8::AccessorInfo& info) {
3084 return v8::Number::New(index + 1);
3085}
3086
3087
3088TEST(InterceptorPropertyMirror) {
3089 // Create a V8 environment with debug access.
3090 v8::HandleScope scope;
3091 DebugLocalContext env;
3092 env.ExposeDebug();
3093
3094 // Create object with named interceptor.
3095 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New();
3096 named->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum);
3097 env->Global()->Set(v8::String::New("intercepted_named"),
3098 named->NewInstance());
3099
3100 // Create object with indexed interceptor.
3101 v8::Handle<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New();
3102 indexed->SetIndexedPropertyHandler(IndexedGetter,
3103 NULL,
3104 NULL,
3105 NULL,
3106 IndexedEnum);
3107 env->Global()->Set(v8::String::New("intercepted_indexed"),
3108 indexed->NewInstance());
3109
3110 // Create object with both named and indexed interceptor.
3111 v8::Handle<v8::ObjectTemplate> both = v8::ObjectTemplate::New();
3112 both->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum);
3113 both->SetIndexedPropertyHandler(IndexedGetter, NULL, NULL, NULL, IndexedEnum);
3114 env->Global()->Set(v8::String::New("intercepted_both"), both->NewInstance());
3115
3116 // Get mirrors for the three objects with interceptor.
3117 CompileRun(
3118 "named_mirror = debug.MakeMirror(intercepted_named);"
3119 "indexed_mirror = debug.MakeMirror(intercepted_indexed);"
3120 "both_mirror = debug.MakeMirror(intercepted_both)");
3121 CHECK(CompileRun(
3122 "named_mirror instanceof debug.ObjectMirror")->BooleanValue());
3123 CHECK(CompileRun(
3124 "indexed_mirror instanceof debug.ObjectMirror")->BooleanValue());
3125 CHECK(CompileRun(
3126 "both_mirror instanceof debug.ObjectMirror")->BooleanValue());
3127
3128 // Get the property names from the interceptors
3129 CompileRun(
ager@chromium.org32912102009-01-16 10:38:43 +00003130 "named_names = named_mirror.propertyNames();"
3131 "indexed_names = indexed_mirror.propertyNames();"
3132 "both_names = both_mirror.propertyNames()");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003133 CHECK_EQ(3, CompileRun("named_names.length")->Int32Value());
3134 CHECK_EQ(2, CompileRun("indexed_names.length")->Int32Value());
3135 CHECK_EQ(5, CompileRun("both_names.length")->Int32Value());
3136
3137 // Check the expected number of properties.
3138 const char* source;
ager@chromium.org32912102009-01-16 10:38:43 +00003139 source = "named_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003140 CHECK_EQ(3, CompileRun(source)->Int32Value());
3141
ager@chromium.org32912102009-01-16 10:38:43 +00003142 source = "indexed_mirror.properties().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 source = "both_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003146 CHECK_EQ(5, CompileRun(source)->Int32Value());
3147
ager@chromium.org32912102009-01-16 10:38:43 +00003148 // 1 is PropertyKind.Named;
3149 source = "both_mirror.properties(1).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003150 CHECK_EQ(3, CompileRun(source)->Int32Value());
3151
ager@chromium.org32912102009-01-16 10:38:43 +00003152 // 2 is PropertyKind.Indexed;
3153 source = "both_mirror.properties(2).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003154 CHECK_EQ(2, CompileRun(source)->Int32Value());
3155
ager@chromium.org32912102009-01-16 10:38:43 +00003156 // 3 is PropertyKind.Named | PropertyKind.Indexed;
3157 source = "both_mirror.properties(3).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003158 CHECK_EQ(5, CompileRun(source)->Int32Value());
3159
ager@chromium.org32912102009-01-16 10:38:43 +00003160 // Get the interceptor properties for the object with only named interceptor.
3161 CompileRun("named_values = named_mirror.properties()");
3162
3163 // Check that the properties are interceptor properties.
3164 for (int i = 0; i < 3; i++) {
3165 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
3166 OS::SNPrintF(buffer,
3167 "named_values[%d] instanceof debug.PropertyMirror", i);
3168 CHECK(CompileRun(buffer.start())->BooleanValue());
3169
3170 // 4 is PropertyType.Interceptor
3171 OS::SNPrintF(buffer, "named_values[%d].propertyType()", i);
3172 CHECK_EQ(4, CompileRun(buffer.start())->Int32Value());
3173
3174 OS::SNPrintF(buffer, "named_values[%d].isNative()", i);
3175 CHECK(CompileRun(buffer.start())->BooleanValue());
3176 }
3177
3178 // Get the interceptor properties for the object with only indexed
3179 // interceptor.
3180 CompileRun("indexed_values = indexed_mirror.properties()");
3181
3182 // Check that the properties are interceptor properties.
3183 for (int i = 0; i < 2; i++) {
3184 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
3185 OS::SNPrintF(buffer,
3186 "indexed_values[%d] instanceof debug.PropertyMirror", i);
3187 CHECK(CompileRun(buffer.start())->BooleanValue());
3188 }
3189
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003190 // Get the interceptor properties for the object with both types of
3191 // interceptors.
ager@chromium.org32912102009-01-16 10:38:43 +00003192 CompileRun("both_values = both_mirror.properties()");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003193
ager@chromium.org32912102009-01-16 10:38:43 +00003194 // Check that the properties are interceptor properties.
3195 for (int i = 0; i < 5; i++) {
3196 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
3197 OS::SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i);
3198 CHECK(CompileRun(buffer.start())->BooleanValue());
3199 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003200
3201 // Check the property names.
3202 source = "both_values[0].name() == 'a'";
3203 CHECK(CompileRun(source)->BooleanValue());
3204
3205 source = "both_values[1].name() == 'b'";
3206 CHECK(CompileRun(source)->BooleanValue());
3207
3208 source = "both_values[2].name() == 'c'";
3209 CHECK(CompileRun(source)->BooleanValue());
3210
3211 source = "both_values[3].name() == 1";
3212 CHECK(CompileRun(source)->BooleanValue());
3213
3214 source = "both_values[4].name() == 10";
3215 CHECK(CompileRun(source)->BooleanValue());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003216}
3217
3218
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003219TEST(HiddenPrototypePropertyMirror) {
3220 // Create a V8 environment with debug access.
3221 v8::HandleScope scope;
3222 DebugLocalContext env;
3223 env.ExposeDebug();
3224
3225 v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New();
3226 t0->InstanceTemplate()->Set(v8::String::New("x"), v8::Number::New(0));
3227 v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New();
3228 t1->SetHiddenPrototype(true);
3229 t1->InstanceTemplate()->Set(v8::String::New("y"), v8::Number::New(1));
3230 v8::Handle<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New();
3231 t2->SetHiddenPrototype(true);
3232 t2->InstanceTemplate()->Set(v8::String::New("z"), v8::Number::New(2));
3233 v8::Handle<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New();
3234 t3->InstanceTemplate()->Set(v8::String::New("u"), v8::Number::New(3));
3235
3236 // Create object and set them on the global object.
3237 v8::Handle<v8::Object> o0 = t0->GetFunction()->NewInstance();
3238 env->Global()->Set(v8::String::New("o0"), o0);
3239 v8::Handle<v8::Object> o1 = t1->GetFunction()->NewInstance();
3240 env->Global()->Set(v8::String::New("o1"), o1);
3241 v8::Handle<v8::Object> o2 = t2->GetFunction()->NewInstance();
3242 env->Global()->Set(v8::String::New("o2"), o2);
3243 v8::Handle<v8::Object> o3 = t3->GetFunction()->NewInstance();
3244 env->Global()->Set(v8::String::New("o3"), o3);
3245
3246 // Get mirrors for the four objects.
3247 CompileRun(
3248 "o0_mirror = debug.MakeMirror(o0);"
3249 "o1_mirror = debug.MakeMirror(o1);"
3250 "o2_mirror = debug.MakeMirror(o2);"
3251 "o3_mirror = debug.MakeMirror(o3)");
3252 CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror")->BooleanValue());
3253 CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror")->BooleanValue());
3254 CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror")->BooleanValue());
3255 CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror")->BooleanValue());
3256
3257 // Check that each object has one property.
3258 CHECK_EQ(1, CompileRun(
3259 "o0_mirror.propertyNames().length")->Int32Value());
3260 CHECK_EQ(1, CompileRun(
3261 "o1_mirror.propertyNames().length")->Int32Value());
3262 CHECK_EQ(1, CompileRun(
3263 "o2_mirror.propertyNames().length")->Int32Value());
3264 CHECK_EQ(1, CompileRun(
3265 "o3_mirror.propertyNames().length")->Int32Value());
3266
3267 // Set o1 as prototype for o0. o1 has the hidden prototype flag so all
3268 // properties on o1 should be seen on o0.
3269 o0->Set(v8::String::New("__proto__"), o1);
3270 CHECK_EQ(2, 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
3277 // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden
3278 // prototype flag. o2 also has the hidden prototype flag so all properties
3279 // on o2 should be seen on o0 as well as properties on o1.
3280 o0->Set(v8::String::New("__proto__"), o2);
3281 CHECK_EQ(3, CompileRun(
3282 "o0_mirror.propertyNames().length")->Int32Value());
3283 CHECK_EQ(0, CompileRun(
3284 "o0_mirror.property('x').value().value()")->Int32Value());
3285 CHECK_EQ(1, CompileRun(
3286 "o0_mirror.property('y').value().value()")->Int32Value());
3287 CHECK_EQ(2, CompileRun(
3288 "o0_mirror.property('z').value().value()")->Int32Value());
3289
3290 // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and
3291 // o2 has the hidden prototype flag. o3 does not have the hidden prototype
3292 // flag so properties on o3 should not be seen on o0 whereas the properties
3293 // from o1 and o2 should still be seen on o0.
3294 // Final prototype chain: o0 -> o1 -> o2 -> o3
3295 // Hidden prototypes: ^^ ^^
3296 o0->Set(v8::String::New("__proto__"), o3);
3297 CHECK_EQ(3, CompileRun(
3298 "o0_mirror.propertyNames().length")->Int32Value());
3299 CHECK_EQ(1, CompileRun(
3300 "o3_mirror.propertyNames().length")->Int32Value());
3301 CHECK_EQ(0, CompileRun(
3302 "o0_mirror.property('x').value().value()")->Int32Value());
3303 CHECK_EQ(1, CompileRun(
3304 "o0_mirror.property('y').value().value()")->Int32Value());
3305 CHECK_EQ(2, CompileRun(
3306 "o0_mirror.property('z').value().value()")->Int32Value());
3307 CHECK(CompileRun("o0_mirror.property('u').isUndefined()")->BooleanValue());
3308
3309 // The prototype (__proto__) for o0 should be o3 as o1 and o2 are hidden.
3310 CHECK(CompileRun("o0_mirror.protoObject() == o3_mirror")->BooleanValue());
3311}
3312
3313
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003314// Multithreaded tests of JSON debugger protocol
3315
3316// Support classes
3317
3318// Copies a C string to a 16-bit string. Does not check for buffer overflow.
3319// Does not use the V8 engine to convert strings, so it can be used
3320// in any thread. Returns the length of the string.
3321int AsciiToUtf16(const char* input_buffer, uint16_t* output_buffer) {
3322 int i;
3323 for (i = 0; input_buffer[i] != '\0'; ++i) {
3324 // ASCII does not use chars > 127, but be careful anyway.
3325 output_buffer[i] = static_cast<unsigned char>(input_buffer[i]);
3326 }
3327 output_buffer[i] = 0;
3328 return i;
3329}
3330
3331// Copies a 16-bit string to a C string by dropping the high byte of
3332// each character. Does not check for buffer overflow.
3333// Can be used in any thread. Requires string length as an input.
3334int Utf16ToAscii(const uint16_t* input_buffer, int length,
3335 char* output_buffer) {
3336 for (int i = 0; i < length; ++i) {
3337 output_buffer[i] = static_cast<char>(input_buffer[i]);
3338 }
3339 output_buffer[length] = '\0';
3340 return length;
3341}
3342
3343// Provides synchronization between k threads, where k is an input to the
3344// constructor. The Wait() call blocks a thread until it is called for the
3345// k'th time, then all calls return. Each ThreadBarrier object can only
3346// be used once.
3347class ThreadBarrier {
3348 public:
3349 explicit ThreadBarrier(int num_threads);
3350 ~ThreadBarrier();
3351 void Wait();
3352 private:
3353 int num_threads_;
3354 int num_blocked_;
3355 v8::internal::Mutex* lock_;
3356 v8::internal::Semaphore* sem_;
3357 bool invalid_;
3358};
3359
3360ThreadBarrier::ThreadBarrier(int num_threads)
3361 : num_threads_(num_threads), num_blocked_(0) {
3362 lock_ = OS::CreateMutex();
3363 sem_ = OS::CreateSemaphore(0);
3364 invalid_ = false; // A barrier may only be used once. Then it is invalid.
3365}
3366
3367// Do not call, due to race condition with Wait().
3368// Could be resolved with Pthread condition variables.
3369ThreadBarrier::~ThreadBarrier() {
3370 lock_->Lock();
3371 delete lock_;
3372 delete sem_;
3373}
3374
3375void ThreadBarrier::Wait() {
3376 lock_->Lock();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003377 CHECK(!invalid_);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003378 if (num_blocked_ == num_threads_ - 1) {
3379 // Signal and unblock all waiting threads.
3380 for (int i = 0; i < num_threads_ - 1; ++i) {
3381 sem_->Signal();
3382 }
3383 invalid_ = true;
3384 printf("BARRIER\n\n");
3385 fflush(stdout);
3386 lock_->Unlock();
3387 } else { // Wait for the semaphore.
3388 ++num_blocked_;
3389 lock_->Unlock(); // Potential race condition with destructor because
3390 sem_->Wait(); // these two lines are not atomic.
3391 }
3392}
3393
3394// A set containing enough barriers and semaphores for any of the tests.
3395class Barriers {
3396 public:
3397 Barriers();
3398 void Initialize();
3399 ThreadBarrier barrier_1;
3400 ThreadBarrier barrier_2;
3401 ThreadBarrier barrier_3;
3402 ThreadBarrier barrier_4;
3403 ThreadBarrier barrier_5;
3404 v8::internal::Semaphore* semaphore_1;
3405 v8::internal::Semaphore* semaphore_2;
3406};
3407
3408Barriers::Barriers() : barrier_1(2), barrier_2(2),
3409 barrier_3(2), barrier_4(2), barrier_5(2) {}
3410
3411void Barriers::Initialize() {
3412 semaphore_1 = OS::CreateSemaphore(0);
3413 semaphore_2 = OS::CreateSemaphore(0);
3414}
3415
3416
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003417// We match parts of the message to decide if it is a break message.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003418bool IsBreakEventMessage(char *message) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003419 const char* type_event = "\"type\":\"event\"";
3420 const char* event_break = "\"event\":\"break\"";
3421 // Does the message contain both type:event and event:break?
3422 return strstr(message, type_event) != NULL &&
3423 strstr(message, event_break) != NULL;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003424}
3425
3426
3427/* Test MessageQueues */
3428/* Tests the message queues that hold debugger commands and
3429 * response messages to the debugger. Fills queues and makes
3430 * them grow.
3431 */
3432Barriers message_queue_barriers;
3433
3434// This is the debugger thread, that executes no v8 calls except
3435// placing JSON debugger commands in the queue.
3436class MessageQueueDebuggerThread : public v8::internal::Thread {
3437 public:
3438 void Run();
3439};
3440
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003441static void MessageHandler(const uint16_t* message, int length,
3442 v8::Debug::ClientData* client_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003443 static char print_buffer[1000];
3444 Utf16ToAscii(message, length, print_buffer);
3445 if (IsBreakEventMessage(print_buffer)) {
3446 // Lets test script wait until break occurs to send commands.
3447 // Signals when a break is reported.
3448 message_queue_barriers.semaphore_2->Signal();
3449 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00003450
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003451 // Allow message handler to block on a semaphore, to test queueing of
3452 // messages while blocked.
3453 message_queue_barriers.semaphore_1->Wait();
3454 printf("%s\n", print_buffer);
3455 fflush(stdout);
3456}
3457
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003458void MessageQueueDebuggerThread::Run() {
3459 const int kBufferSize = 1000;
3460 uint16_t buffer_1[kBufferSize];
3461 uint16_t buffer_2[kBufferSize];
3462 const char* command_1 =
3463 "{\"seq\":117,"
3464 "\"type\":\"request\","
3465 "\"command\":\"evaluate\","
3466 "\"arguments\":{\"expression\":\"1+2\"}}";
3467 const char* command_2 =
3468 "{\"seq\":118,"
3469 "\"type\":\"request\","
3470 "\"command\":\"evaluate\","
3471 "\"arguments\":{\"expression\":\"1+a\"}}";
3472 const char* command_3 =
3473 "{\"seq\":119,"
3474 "\"type\":\"request\","
3475 "\"command\":\"evaluate\","
3476 "\"arguments\":{\"expression\":\"c.d * b\"}}";
3477 const char* command_continue =
3478 "{\"seq\":106,"
3479 "\"type\":\"request\","
3480 "\"command\":\"continue\"}";
3481 const char* command_single_step =
3482 "{\"seq\":107,"
3483 "\"type\":\"request\","
3484 "\"command\":\"continue\","
3485 "\"arguments\":{\"stepaction\":\"next\"}}";
3486
3487 /* Interleaved sequence of actions by the two threads:*/
3488 // Main thread compiles and runs source_1
ager@chromium.org5ec48922009-05-05 07:25:34 +00003489 message_queue_barriers.semaphore_1->Signal();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003490 message_queue_barriers.barrier_1.Wait();
3491 // Post 6 commands, filling the command queue and making it expand.
3492 // These calls return immediately, but the commands stay on the queue
3493 // until the execution of source_2.
3494 // Note: AsciiToUtf16 executes before SendCommand, so command is copied
3495 // to buffer before buffer is sent to SendCommand.
3496 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1));
3497 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2));
3498 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
3499 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
3500 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003501 message_queue_barriers.barrier_2.Wait();
3502 // Main thread compiles and runs source_2.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003503 // Queued commands are executed at the start of compilation of source_2(
3504 // beforeCompile event).
3505 // Free the message handler to process all the messages from the queue. 7
3506 // messages are expected: 2 afterCompile events and 5 responses.
3507 // All the commands added so far will fail to execute as long as call stack
3508 // is empty on beforeCompile event.
3509 for (int i = 0; i < 6 ; ++i) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003510 message_queue_barriers.semaphore_1->Signal();
3511 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00003512 message_queue_barriers.barrier_3.Wait();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003513 // Main thread compiles and runs source_3.
ager@chromium.org5ec48922009-05-05 07:25:34 +00003514 // Don't stop in the afterCompile handler.
3515 message_queue_barriers.semaphore_1->Signal();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003516 // source_3 includes a debugger statement, which causes a break event.
3517 // Wait on break event from hitting "debugger" statement
3518 message_queue_barriers.semaphore_2->Wait();
3519 // These should execute after the "debugger" statement in source_2
ager@chromium.org5ec48922009-05-05 07:25:34 +00003520 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1));
3521 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2));
3522 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003523 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_single_step, buffer_2));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003524 // Run after 2 break events, 4 responses.
3525 for (int i = 0; i < 6 ; ++i) {
3526 message_queue_barriers.semaphore_1->Signal();
3527 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003528 // Wait on break event after a single step executes.
3529 message_queue_barriers.semaphore_2->Wait();
3530 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_2, buffer_1));
3531 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_continue, buffer_2));
ager@chromium.org5ec48922009-05-05 07:25:34 +00003532 // Run after 2 responses.
3533 for (int i = 0; i < 2 ; ++i) {
3534 message_queue_barriers.semaphore_1->Signal();
3535 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003536 // Main thread continues running source_3 to end, waits for this thread.
3537}
3538
3539MessageQueueDebuggerThread message_queue_debugger_thread;
3540
3541// This thread runs the v8 engine.
3542TEST(MessageQueues) {
3543 // Create a V8 environment
3544 v8::HandleScope scope;
3545 DebugLocalContext env;
3546 message_queue_barriers.Initialize();
3547 v8::Debug::SetMessageHandler(MessageHandler);
3548 message_queue_debugger_thread.Start();
3549
3550 const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;";
3551 const char* source_2 = "e = 17;";
3552 const char* source_3 = "a = 4; debugger; a = 5; a = 6; a = 7;";
3553
3554 // See MessageQueueDebuggerThread::Run for interleaved sequence of
3555 // API calls and events in the two threads.
3556 CompileRun(source_1);
3557 message_queue_barriers.barrier_1.Wait();
3558 message_queue_barriers.barrier_2.Wait();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003559 CompileRun(source_2);
3560 message_queue_barriers.barrier_3.Wait();
3561 CompileRun(source_3);
3562 message_queue_debugger_thread.Join();
3563 fflush(stdout);
3564}
3565
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003566
3567class TestClientData : public v8::Debug::ClientData {
3568 public:
3569 TestClientData() {
3570 constructor_call_counter++;
3571 }
3572 virtual ~TestClientData() {
3573 destructor_call_counter++;
3574 }
3575
3576 static void ResetCounters() {
3577 constructor_call_counter = 0;
3578 destructor_call_counter = 0;
3579 }
3580
3581 static int constructor_call_counter;
3582 static int destructor_call_counter;
3583};
3584
3585int TestClientData::constructor_call_counter = 0;
3586int TestClientData::destructor_call_counter = 0;
3587
3588
3589// Tests that MessageQueue doesn't destroy client data when expands and
3590// does destroy when it dies.
3591TEST(MessageQueueExpandAndDestroy) {
3592 TestClientData::ResetCounters();
3593 { // Create a scope for the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003594 CommandMessageQueue queue(1);
3595 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003596 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003597 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003598 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003599 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003600 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003601 CHECK_EQ(0, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003602 queue.Get().Dispose();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003603 CHECK_EQ(1, TestClientData::destructor_call_counter);
3604 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003605 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003606 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003607 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003608 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003609 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003610 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
3611 new TestClientData()));
3612 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
3613 new TestClientData()));
3614 CHECK_EQ(1, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003615 queue.Get().Dispose();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003616 CHECK_EQ(2, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003617 }
3618 // All the client data should be destroyed when the queue is destroyed.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003619 CHECK_EQ(TestClientData::destructor_call_counter,
3620 TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003621}
3622
3623
3624static int handled_client_data_instances_count = 0;
3625static void MessageHandlerCountingClientData(
ager@chromium.org5ec48922009-05-05 07:25:34 +00003626 const v8::Debug::Message& message) {
3627 if (message.GetClientData() != NULL) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003628 handled_client_data_instances_count++;
3629 }
3630}
3631
3632
3633// Tests that all client data passed to the debugger are sent to the handler.
3634TEST(SendClientDataToHandler) {
3635 // Create a V8 environment
3636 v8::HandleScope scope;
3637 DebugLocalContext env;
3638 TestClientData::ResetCounters();
3639 handled_client_data_instances_count = 0;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003640 v8::Debug::SetMessageHandler2(MessageHandlerCountingClientData);
3641 const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;";
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003642 const int kBufferSize = 1000;
3643 uint16_t buffer[kBufferSize];
3644 const char* command_1 =
3645 "{\"seq\":117,"
3646 "\"type\":\"request\","
3647 "\"command\":\"evaluate\","
3648 "\"arguments\":{\"expression\":\"1+2\"}}";
3649 const char* command_2 =
3650 "{\"seq\":118,"
3651 "\"type\":\"request\","
3652 "\"command\":\"evaluate\","
3653 "\"arguments\":{\"expression\":\"1+a\"}}";
3654 const char* command_continue =
3655 "{\"seq\":106,"
3656 "\"type\":\"request\","
3657 "\"command\":\"continue\"}";
3658
3659 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer),
3660 new TestClientData());
3661 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), NULL);
3662 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer),
3663 new TestClientData());
3664 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer),
3665 new TestClientData());
ager@chromium.org5ec48922009-05-05 07:25:34 +00003666 // All the messages will be processed on beforeCompile event.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003667 CompileRun(source_1);
ager@chromium.org5ec48922009-05-05 07:25:34 +00003668 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003669 CHECK_EQ(3, TestClientData::constructor_call_counter);
3670 CHECK_EQ(TestClientData::constructor_call_counter,
3671 handled_client_data_instances_count);
3672 CHECK_EQ(TestClientData::constructor_call_counter,
3673 TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003674}
3675
3676
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003677/* Test ThreadedDebugging */
3678/* This test interrupts a running infinite loop that is
3679 * occupying the v8 thread by a break command from the
3680 * debugger thread. It then changes the value of a
3681 * global object, to make the loop terminate.
3682 */
3683
3684Barriers threaded_debugging_barriers;
3685
3686class V8Thread : public v8::internal::Thread {
3687 public:
3688 void Run();
3689};
3690
3691class DebuggerThread : public v8::internal::Thread {
3692 public:
3693 void Run();
3694};
3695
3696
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003697static v8::Handle<v8::Value> ThreadedAtBarrier1(const v8::Arguments& args) {
3698 threaded_debugging_barriers.barrier_1.Wait();
3699 return v8::Undefined();
3700}
3701
3702
ager@chromium.org5ec48922009-05-05 07:25:34 +00003703static void ThreadedMessageHandler(const v8::Debug::Message& message) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003704 static char print_buffer[1000];
ager@chromium.org5ec48922009-05-05 07:25:34 +00003705 v8::String::Value json(message.GetJSON());
3706 Utf16ToAscii(*json, json.length(), print_buffer);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003707 if (IsBreakEventMessage(print_buffer)) {
3708 threaded_debugging_barriers.barrier_2.Wait();
3709 }
3710 printf("%s\n", print_buffer);
3711 fflush(stdout);
3712}
3713
3714
3715void V8Thread::Run() {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003716 const char* source =
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003717 "flag = true;\n"
3718 "function bar( new_value ) {\n"
3719 " flag = new_value;\n"
3720 " return \"Return from bar(\" + new_value + \")\";\n"
3721 "}\n"
3722 "\n"
3723 "function foo() {\n"
3724 " var x = 1;\n"
3725 " while ( flag == true ) {\n"
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003726 " if ( x == 1 ) {\n"
3727 " ThreadedAtBarrier1();\n"
3728 " }\n"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003729 " x = x + 1;\n"
3730 " }\n"
3731 "}\n"
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003732 "\n"
3733 "foo();\n";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003734
3735 v8::HandleScope scope;
3736 DebugLocalContext env;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003737 v8::Debug::SetMessageHandler2(&ThreadedMessageHandler);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003738 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
3739 global_template->Set(v8::String::New("ThreadedAtBarrier1"),
3740 v8::FunctionTemplate::New(ThreadedAtBarrier1));
3741 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template);
3742 v8::Context::Scope context_scope(context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003743
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003744 CompileRun(source);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003745}
3746
3747void DebuggerThread::Run() {
3748 const int kBufSize = 1000;
3749 uint16_t buffer[kBufSize];
3750
3751 const char* command_1 = "{\"seq\":102,"
3752 "\"type\":\"request\","
3753 "\"command\":\"evaluate\","
3754 "\"arguments\":{\"expression\":\"bar(false)\"}}";
3755 const char* command_2 = "{\"seq\":103,"
3756 "\"type\":\"request\","
3757 "\"command\":\"continue\"}";
3758
3759 threaded_debugging_barriers.barrier_1.Wait();
3760 v8::Debug::DebugBreak();
3761 threaded_debugging_barriers.barrier_2.Wait();
3762 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
3763 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
3764}
3765
3766DebuggerThread debugger_thread;
3767V8Thread v8_thread;
3768
3769TEST(ThreadedDebugging) {
3770 // Create a V8 environment
3771 threaded_debugging_barriers.Initialize();
3772
3773 v8_thread.Start();
3774 debugger_thread.Start();
3775
3776 v8_thread.Join();
3777 debugger_thread.Join();
3778}
3779
3780/* Test RecursiveBreakpoints */
3781/* In this test, the debugger evaluates a function with a breakpoint, after
3782 * hitting a breakpoint in another function. We do this with both values
3783 * of the flag enabling recursive breakpoints, and verify that the second
3784 * breakpoint is hit when enabled, and missed when disabled.
3785 */
3786
3787class BreakpointsV8Thread : public v8::internal::Thread {
3788 public:
3789 void Run();
3790};
3791
3792class BreakpointsDebuggerThread : public v8::internal::Thread {
3793 public:
3794 void Run();
3795};
3796
3797
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003798Barriers* breakpoints_barriers;
3799
ager@chromium.org5ec48922009-05-05 07:25:34 +00003800static void BreakpointsMessageHandler(const v8::Debug::Message& message) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003801 static char print_buffer[1000];
ager@chromium.org5ec48922009-05-05 07:25:34 +00003802 v8::String::Value json(message.GetJSON());
3803 Utf16ToAscii(*json, json.length(), print_buffer);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003804 printf("%s\n", print_buffer);
3805 fflush(stdout);
3806
3807 // Is break_template a prefix of the message?
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003808 if (IsBreakEventMessage(print_buffer)) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003809 breakpoints_barriers->semaphore_1->Signal();
3810 }
3811}
3812
3813
3814void BreakpointsV8Thread::Run() {
3815 const char* source_1 = "var y_global = 3;\n"
3816 "function cat( new_value ) {\n"
3817 " var x = new_value;\n"
3818 " y_global = 4;\n"
3819 " x = 3 * x + 1;\n"
3820 " y_global = 5;\n"
3821 " return x;\n"
3822 "}\n"
3823 "\n"
3824 "function dog() {\n"
3825 " var x = 1;\n"
3826 " x = y_global;"
3827 " var z = 3;"
3828 " x += 100;\n"
3829 " return x;\n"
3830 "}\n"
3831 "\n";
3832 const char* source_2 = "cat(17);\n"
3833 "cat(19);\n";
3834
3835 v8::HandleScope scope;
3836 DebugLocalContext env;
ager@chromium.org5ec48922009-05-05 07:25:34 +00003837 v8::Debug::SetMessageHandler2(&BreakpointsMessageHandler);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003838
3839 CompileRun(source_1);
3840 breakpoints_barriers->barrier_1.Wait();
3841 breakpoints_barriers->barrier_2.Wait();
3842 CompileRun(source_2);
3843}
3844
3845
3846void BreakpointsDebuggerThread::Run() {
3847 const int kBufSize = 1000;
3848 uint16_t buffer[kBufSize];
3849
3850 const char* command_1 = "{\"seq\":101,"
3851 "\"type\":\"request\","
3852 "\"command\":\"setbreakpoint\","
3853 "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}";
3854 const char* command_2 = "{\"seq\":102,"
3855 "\"type\":\"request\","
3856 "\"command\":\"setbreakpoint\","
3857 "\"arguments\":{\"type\":\"function\",\"target\":\"dog\",\"line\":3}}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003858 const char* command_3 = "{\"seq\":104,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003859 "\"type\":\"request\","
3860 "\"command\":\"evaluate\","
3861 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false}}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003862 const char* command_4 = "{\"seq\":105,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003863 "\"type\":\"request\","
3864 "\"command\":\"evaluate\","
3865 "\"arguments\":{\"expression\":\"x\",\"disable_break\":true}}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003866 const char* command_5 = "{\"seq\":106,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003867 "\"type\":\"request\","
3868 "\"command\":\"continue\"}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003869 const char* command_6 = "{\"seq\":107,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003870 "\"type\":\"request\","
3871 "\"command\":\"continue\"}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003872 const char* command_7 = "{\"seq\":108,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003873 "\"type\":\"request\","
3874 "\"command\":\"evaluate\","
3875 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true}}";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003876 const char* command_8 = "{\"seq\":109,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003877 "\"type\":\"request\","
3878 "\"command\":\"continue\"}";
3879
3880
3881 // v8 thread initializes, runs source_1
3882 breakpoints_barriers->barrier_1.Wait();
3883 // 1:Set breakpoint in cat().
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003884 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
3885 // 2:Set breakpoint in dog()
3886 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003887 breakpoints_barriers->barrier_2.Wait();
3888 // v8 thread starts compiling source_2.
3889 // Automatic break happens, to run queued commands
3890 // breakpoints_barriers->semaphore_1->Wait();
3891 // Commands 1 through 3 run, thread continues.
3892 // v8 thread runs source_2 to breakpoint in cat().
3893 // message callback receives break event.
3894 breakpoints_barriers->semaphore_1->Wait();
3895 // 4:Evaluate dog() (which has a breakpoint).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003896 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_3, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003897 // v8 thread hits breakpoint in dog()
3898 breakpoints_barriers->semaphore_1->Wait(); // wait for break event
3899 // 5:Evaluate x
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003900 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_4, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003901 // 6:Continue evaluation of dog()
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003902 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_5, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003903 // dog() finishes.
3904 // 7:Continue evaluation of source_2, finish cat(17), hit breakpoint
3905 // in cat(19).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003906 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_6, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003907 // message callback gets break event
3908 breakpoints_barriers->semaphore_1->Wait(); // wait for break event
3909 // 8: Evaluate dog() with breaks disabled
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003910 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_7, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003911 // 9: Continue evaluation of source2, reach end.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003912 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_8, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003913}
3914
3915BreakpointsDebuggerThread breakpoints_debugger_thread;
3916BreakpointsV8Thread breakpoints_v8_thread;
3917
3918TEST(RecursiveBreakpoints) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003919 i::FLAG_debugger_auto_break = true;
3920
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003921 // Create a V8 environment
3922 Barriers stack_allocated_breakpoints_barriers;
3923 stack_allocated_breakpoints_barriers.Initialize();
3924 breakpoints_barriers = &stack_allocated_breakpoints_barriers;
3925
3926 breakpoints_v8_thread.Start();
3927 breakpoints_debugger_thread.Start();
3928
3929 breakpoints_v8_thread.Join();
3930 breakpoints_debugger_thread.Join();
3931}
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003932
3933
3934static void DummyDebugEventListener(v8::DebugEvent event,
3935 v8::Handle<v8::Object> exec_state,
3936 v8::Handle<v8::Object> event_data,
3937 v8::Handle<v8::Value> data) {
3938}
3939
3940
iposva@chromium.org245aa852009-02-10 00:49:54 +00003941TEST(SetDebugEventListenerOnUninitializedVM) {
3942 v8::Debug::SetDebugEventListener(DummyDebugEventListener);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003943}
3944
3945
ager@chromium.org5ec48922009-05-05 07:25:34 +00003946static void DummyMessageHandler(const v8::Debug::Message& message) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003947}
3948
3949
3950TEST(SetMessageHandlerOnUninitializedVM) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00003951 v8::Debug::SetMessageHandler2(DummyMessageHandler);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003952}
3953
3954
3955TEST(DebugBreakOnUninitializedVM) {
3956 v8::Debug::DebugBreak();
3957}
3958
3959
3960TEST(SendCommandToUninitializedVM) {
3961 const char* dummy_command = "{}";
3962 uint16_t dummy_buffer[80];
3963 int dummy_length = AsciiToUtf16(dummy_command, dummy_buffer);
3964 v8::Debug::SendCommand(dummy_buffer, dummy_length);
3965}
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003966
3967
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003968// Source for a JavaScript function which returns the data parameter of a
3969// function called in the context of the debugger. If no data parameter is
3970// passed it throws an exception.
3971static const char* debugger_call_with_data_source =
3972 "function debugger_call_with_data(exec_state, data) {"
3973 " if (data) return data;"
3974 " throw 'No data!'"
3975 "}";
3976v8::Handle<v8::Function> debugger_call_with_data;
3977
3978
3979// Source for a JavaScript function which returns the data parameter of a
3980// function called in the context of the debugger. If no data parameter is
3981// passed it throws an exception.
3982static const char* debugger_call_with_closure_source =
3983 "var x = 3;"
3984 "function (exec_state) {"
3985 " if (exec_state.y) return x - 1;"
3986 " exec_state.y = x;"
3987 " return exec_state.y"
3988 "}";
3989v8::Handle<v8::Function> debugger_call_with_closure;
3990
3991// Function to retrieve the number of JavaScript frames by calling a JavaScript
3992// in the debugger.
3993static v8::Handle<v8::Value> CheckFrameCount(const v8::Arguments& args) {
3994 CHECK(v8::Debug::Call(frame_count)->IsNumber());
3995 CHECK_EQ(args[0]->Int32Value(),
3996 v8::Debug::Call(frame_count)->Int32Value());
3997 return v8::Undefined();
3998}
3999
4000
4001// Function to retrieve the source line of the top JavaScript frame by calling a
4002// JavaScript function in the debugger.
4003static v8::Handle<v8::Value> CheckSourceLine(const v8::Arguments& args) {
4004 CHECK(v8::Debug::Call(frame_source_line)->IsNumber());
4005 CHECK_EQ(args[0]->Int32Value(),
4006 v8::Debug::Call(frame_source_line)->Int32Value());
4007 return v8::Undefined();
4008}
4009
4010
4011// Function to test passing an additional parameter to a JavaScript function
4012// called in the debugger. It also tests that functions called in the debugger
4013// can throw exceptions.
4014static v8::Handle<v8::Value> CheckDataParameter(const v8::Arguments& args) {
4015 v8::Handle<v8::String> data = v8::String::New("Test");
4016 CHECK(v8::Debug::Call(debugger_call_with_data, data)->IsString());
4017
4018 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty());
4019 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty());
4020
4021 v8::TryCatch catcher;
4022 v8::Debug::Call(debugger_call_with_data);
4023 CHECK(catcher.HasCaught());
4024 CHECK(catcher.Exception()->IsString());
4025
4026 return v8::Undefined();
4027}
4028
4029
4030// Function to test using a JavaScript with closure in the debugger.
4031static v8::Handle<v8::Value> CheckClosure(const v8::Arguments& args) {
4032 CHECK(v8::Debug::Call(debugger_call_with_closure)->IsNumber());
4033 CHECK_EQ(3, v8::Debug::Call(debugger_call_with_closure)->Int32Value());
4034 return v8::Undefined();
4035}
4036
4037
4038// Test functions called through the debugger.
4039TEST(CallFunctionInDebugger) {
4040 // Create and enter a context with the functions CheckFrameCount,
4041 // CheckSourceLine and CheckDataParameter installed.
4042 v8::HandleScope scope;
4043 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
4044 global_template->Set(v8::String::New("CheckFrameCount"),
4045 v8::FunctionTemplate::New(CheckFrameCount));
4046 global_template->Set(v8::String::New("CheckSourceLine"),
4047 v8::FunctionTemplate::New(CheckSourceLine));
4048 global_template->Set(v8::String::New("CheckDataParameter"),
4049 v8::FunctionTemplate::New(CheckDataParameter));
4050 global_template->Set(v8::String::New("CheckClosure"),
4051 v8::FunctionTemplate::New(CheckClosure));
4052 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template);
4053 v8::Context::Scope context_scope(context);
4054
4055 // Compile a function for checking the number of JavaScript frames.
4056 v8::Script::Compile(v8::String::New(frame_count_source))->Run();
4057 frame_count = v8::Local<v8::Function>::Cast(
4058 context->Global()->Get(v8::String::New("frame_count")));
4059
4060 // Compile a function for returning the source line for the top frame.
4061 v8::Script::Compile(v8::String::New(frame_source_line_source))->Run();
4062 frame_source_line = v8::Local<v8::Function>::Cast(
4063 context->Global()->Get(v8::String::New("frame_source_line")));
4064
4065 // Compile a function returning the data parameter.
4066 v8::Script::Compile(v8::String::New(debugger_call_with_data_source))->Run();
4067 debugger_call_with_data = v8::Local<v8::Function>::Cast(
4068 context->Global()->Get(v8::String::New("debugger_call_with_data")));
4069
4070 // Compile a function capturing closure.
4071 debugger_call_with_closure = v8::Local<v8::Function>::Cast(
4072 v8::Script::Compile(
4073 v8::String::New(debugger_call_with_closure_source))->Run());
4074
4075 // Calling a function through the debugger returns undefined if there are no
4076 // JavaScript frames.
4077 CHECK(v8::Debug::Call(frame_count)->IsUndefined());
4078 CHECK(v8::Debug::Call(frame_source_line)->IsUndefined());
4079 CHECK(v8::Debug::Call(debugger_call_with_data)->IsUndefined());
4080
4081 // Test that the number of frames can be retrieved.
4082 v8::Script::Compile(v8::String::New("CheckFrameCount(1)"))->Run();
4083 v8::Script::Compile(v8::String::New("function f() {"
4084 " CheckFrameCount(2);"
4085 "}; f()"))->Run();
4086
4087 // Test that the source line can be retrieved.
4088 v8::Script::Compile(v8::String::New("CheckSourceLine(0)"))->Run();
4089 v8::Script::Compile(v8::String::New("function f() {\n"
4090 " CheckSourceLine(1)\n"
4091 " CheckSourceLine(2)\n"
4092 " CheckSourceLine(3)\n"
4093 "}; f()"))->Run();
4094
4095 // Test that a parameter can be passed to a function called in the debugger.
4096 v8::Script::Compile(v8::String::New("CheckDataParameter()"))->Run();
4097
4098 // Test that a function with closure can be run in the debugger.
4099 v8::Script::Compile(v8::String::New("CheckClosure()"))->Run();
ager@chromium.org3a6061e2009-03-12 14:24:36 +00004100
4101
4102 // Test that the source line is correct when there is a line offset.
4103 v8::ScriptOrigin origin(v8::String::New("test"),
4104 v8::Integer::New(7));
4105 v8::Script::Compile(v8::String::New("CheckSourceLine(7)"), &origin)->Run();
4106 v8::Script::Compile(v8::String::New("function f() {\n"
4107 " CheckSourceLine(8)\n"
4108 " CheckSourceLine(9)\n"
4109 " CheckSourceLine(10)\n"
4110 "}; f()"), &origin)->Run();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00004111}
ager@chromium.org381abbb2009-02-25 13:23:22 +00004112
4113
4114// Test that clearing the debug event listener actually clears all break points
4115// and related information.
4116TEST(DebuggerUnload) {
4117 v8::HandleScope scope;
4118 DebugLocalContext env;
4119
4120 // Check debugger is unloaded before it is used.
4121 CheckDebuggerUnloaded();
4122
4123 // Add debug event listener.
4124 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
4125 v8::Undefined());
ager@chromium.org381abbb2009-02-25 13:23:22 +00004126 // Create a couple of functions for the test.
4127 v8::Local<v8::Function> foo =
4128 CompileFunction(&env, "function foo(){x=1}", "foo");
4129 v8::Local<v8::Function> bar =
4130 CompileFunction(&env, "function bar(){y=2}", "bar");
4131
4132 // Set some break points.
4133 SetBreakPoint(foo, 0);
4134 SetBreakPoint(foo, 4);
4135 SetBreakPoint(bar, 0);
4136 SetBreakPoint(bar, 4);
4137
4138 // Make sure that the break points are there.
4139 break_point_hit_count = 0;
4140 foo->Call(env->Global(), 0, NULL);
4141 CHECK_EQ(2, break_point_hit_count);
4142 bar->Call(env->Global(), 0, NULL);
4143 CHECK_EQ(4, break_point_hit_count);
4144
4145 // Remove the debug event listener without clearing breakpoints.
4146 v8::Debug::SetDebugEventListener(NULL);
4147 CheckDebuggerUnloaded(true);
4148
4149 // Set a new debug event listener.
4150 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
4151 v8::Undefined());
ager@chromium.org381abbb2009-02-25 13:23:22 +00004152 // Check that the break points was actually cleared.
4153 break_point_hit_count = 0;
4154 foo->Call(env->Global(), 0, NULL);
4155 CHECK_EQ(0, break_point_hit_count);
4156
4157 // Set break points and run again.
4158 SetBreakPoint(foo, 0);
4159 SetBreakPoint(foo, 4);
4160 foo->Call(env->Global(), 0, NULL);
4161 CHECK_EQ(2, break_point_hit_count);
4162
4163 // Remove the debug event listener without clearing breakpoints again.
4164 v8::Debug::SetDebugEventListener(NULL);
4165 CheckDebuggerUnloaded(true);
4166}
4167
4168
ager@chromium.org71daaf62009-04-01 07:22:49 +00004169// Debugger message handler which counts the number of times it is called.
4170static int message_handler_hit_count = 0;
ager@chromium.org5ec48922009-05-05 07:25:34 +00004171static void MessageHandlerHitCount(const v8::Debug::Message& message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00004172 message_handler_hit_count++;
4173
4174 const int kBufferSize = 1000;
4175 uint16_t buffer[kBufferSize];
4176 const char* command_continue =
4177 "{\"seq\":0,"
4178 "\"type\":\"request\","
4179 "\"command\":\"continue\"}";
4180
4181 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer));
4182}
4183
4184
4185// Test clearing the debug message handler.
4186TEST(DebuggerClearMessageHandler) {
4187 v8::HandleScope scope;
4188 DebugLocalContext env;
4189
4190 // Check debugger is unloaded before it is used.
4191 CheckDebuggerUnloaded();
4192
4193 // Set a debug message handler.
ager@chromium.org5ec48922009-05-05 07:25:34 +00004194 v8::Debug::SetMessageHandler2(MessageHandlerHitCount);
ager@chromium.org71daaf62009-04-01 07:22:49 +00004195
4196 // Run code to throw a unhandled exception. This should end up in the message
4197 // handler.
4198 CompileRun("throw 1");
4199
4200 // The message handler should be called.
4201 CHECK_GT(message_handler_hit_count, 0);
4202
4203 // Clear debug message handler.
4204 message_handler_hit_count = 0;
4205 v8::Debug::SetMessageHandler(NULL);
4206
4207 // Run code to throw a unhandled exception. This should end up in the message
4208 // handler.
4209 CompileRun("throw 1");
4210
4211 // The message handler should not be called more.
4212 CHECK_EQ(0, message_handler_hit_count);
4213
4214 CheckDebuggerUnloaded(true);
4215}
4216
4217
4218// Debugger message handler which clears the message handler while active.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004219static void MessageHandlerClearingMessageHandler(
ager@chromium.org5ec48922009-05-05 07:25:34 +00004220 const v8::Debug::Message& message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00004221 message_handler_hit_count++;
4222
4223 // Clear debug message handler.
4224 v8::Debug::SetMessageHandler(NULL);
4225}
4226
4227
4228// Test clearing the debug message handler while processing a debug event.
4229TEST(DebuggerClearMessageHandlerWhileActive) {
4230 v8::HandleScope scope;
4231 DebugLocalContext env;
4232
4233 // Check debugger is unloaded before it is used.
4234 CheckDebuggerUnloaded();
4235
4236 // Set a debug message handler.
ager@chromium.org5ec48922009-05-05 07:25:34 +00004237 v8::Debug::SetMessageHandler2(MessageHandlerClearingMessageHandler);
ager@chromium.org71daaf62009-04-01 07:22:49 +00004238
4239 // Run code to throw a unhandled exception. This should end up in the message
4240 // handler.
4241 CompileRun("throw 1");
4242
4243 // The message handler should be called.
4244 CHECK_EQ(1, message_handler_hit_count);
4245
4246 CheckDebuggerUnloaded(true);
4247}
4248
4249
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004250/* Test DebuggerHostDispatch */
4251/* In this test, the debugger waits for a command on a breakpoint
4252 * and is dispatching host commands while in the infinite loop.
4253 */
4254
4255class HostDispatchV8Thread : public v8::internal::Thread {
4256 public:
4257 void Run();
4258};
4259
4260class HostDispatchDebuggerThread : public v8::internal::Thread {
4261 public:
4262 void Run();
4263};
4264
4265Barriers* host_dispatch_barriers;
4266
ager@chromium.org5ec48922009-05-05 07:25:34 +00004267static void HostDispatchMessageHandler(const v8::Debug::Message& message) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004268 static char print_buffer[1000];
ager@chromium.org5ec48922009-05-05 07:25:34 +00004269 v8::String::Value json(message.GetJSON());
4270 Utf16ToAscii(*json, json.length(), print_buffer);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004271 printf("%s\n", print_buffer);
4272 fflush(stdout);
ager@chromium.org381abbb2009-02-25 13:23:22 +00004273}
4274
4275
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004276static void HostDispatchDispatchHandler() {
4277 host_dispatch_barriers->semaphore_1->Signal();
4278}
4279
4280
4281void HostDispatchV8Thread::Run() {
4282 const char* source_1 = "var y_global = 3;\n"
4283 "function cat( new_value ) {\n"
4284 " var x = new_value;\n"
4285 " y_global = 4;\n"
4286 " x = 3 * x + 1;\n"
4287 " y_global = 5;\n"
4288 " return x;\n"
4289 "}\n"
4290 "\n";
4291 const char* source_2 = "cat(17);\n";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004292
ager@chromium.org381abbb2009-02-25 13:23:22 +00004293 v8::HandleScope scope;
4294 DebugLocalContext env;
4295
ager@chromium.org381abbb2009-02-25 13:23:22 +00004296 // Setup message and host dispatch handlers.
ager@chromium.org5ec48922009-05-05 07:25:34 +00004297 v8::Debug::SetMessageHandler2(HostDispatchMessageHandler);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004298 v8::Debug::SetHostDispatchHandler(HostDispatchDispatchHandler, 10 /* ms */);
ager@chromium.org381abbb2009-02-25 13:23:22 +00004299
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004300 CompileRun(source_1);
4301 host_dispatch_barriers->barrier_1.Wait();
4302 host_dispatch_barriers->barrier_2.Wait();
4303 CompileRun(source_2);
4304}
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00004305
ager@chromium.org381abbb2009-02-25 13:23:22 +00004306
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004307void HostDispatchDebuggerThread::Run() {
4308 const int kBufSize = 1000;
4309 uint16_t buffer[kBufSize];
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00004310
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004311 const char* command_1 = "{\"seq\":101,"
4312 "\"type\":\"request\","
4313 "\"command\":\"setbreakpoint\","
4314 "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}";
4315 const char* command_2 = "{\"seq\":102,"
4316 "\"type\":\"request\","
4317 "\"command\":\"continue\"}";
4318
4319 // v8 thread initializes, runs source_1
4320 host_dispatch_barriers->barrier_1.Wait();
4321 // 1: Set breakpoint in cat().
4322 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
4323
4324 host_dispatch_barriers->barrier_2.Wait();
4325 // v8 thread starts compiling source_2.
4326 // Break happens, to run queued commands and host dispatches.
4327 // Wait for host dispatch to be processed.
4328 host_dispatch_barriers->semaphore_1->Wait();
4329 // 2: Continue evaluation
4330 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
4331}
4332
4333HostDispatchDebuggerThread host_dispatch_debugger_thread;
4334HostDispatchV8Thread host_dispatch_v8_thread;
4335
4336
4337TEST(DebuggerHostDispatch) {
4338 i::FLAG_debugger_auto_break = true;
4339
4340 // Create a V8 environment
4341 Barriers stack_allocated_host_dispatch_barriers;
4342 stack_allocated_host_dispatch_barriers.Initialize();
4343 host_dispatch_barriers = &stack_allocated_host_dispatch_barriers;
4344
4345 host_dispatch_v8_thread.Start();
4346 host_dispatch_debugger_thread.Start();
4347
4348 host_dispatch_v8_thread.Join();
4349 host_dispatch_debugger_thread.Join();
ager@chromium.org381abbb2009-02-25 13:23:22 +00004350}
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004351
4352
4353TEST(DebuggerAgent) {
4354 // Make sure this port is not used by other tests to allow tests to run in
4355 // parallel.
4356 const int kPort = 5858;
4357
4358 // Make a string with the port number.
4359 const int kPortBufferLen = 6;
4360 char port_str[kPortBufferLen];
4361 OS::SNPrintF(i::Vector<char>(port_str, kPortBufferLen), "%d", kPort);
4362
4363 bool ok;
4364
4365 // Initialize the socket library.
4366 i::Socket::Setup();
4367
4368 // Test starting and stopping the agent without any client connection.
4369 i::Debugger::StartAgent("test", kPort);
4370 i::Debugger::StopAgent();
4371
4372 // Test starting the agent, connecting a client and shutting down the agent
4373 // with the client connected.
4374 ok = i::Debugger::StartAgent("test", kPort);
4375 CHECK(ok);
4376 i::Socket* client = i::OS::CreateSocket();
4377 ok = client->Connect("localhost", port_str);
4378 CHECK(ok);
4379 i::Debugger::StopAgent();
4380 delete client;
4381
4382 // Test starting and stopping the agent with the required port already
4383 // occoupied.
4384 i::Socket* server = i::OS::CreateSocket();
4385 server->Bind(kPort);
4386
4387 i::Debugger::StartAgent("test", kPort);
4388 i::Debugger::StopAgent();
4389
4390 delete server;
4391}
4392
4393
4394class DebuggerAgentProtocolServerThread : public i::Thread {
4395 public:
4396 explicit DebuggerAgentProtocolServerThread(int port)
4397 : port_(port), server_(NULL), client_(NULL),
4398 listening_(OS::CreateSemaphore(0)) {
4399 }
4400 ~DebuggerAgentProtocolServerThread() {
4401 // Close both sockets.
4402 delete client_;
4403 delete server_;
4404 delete listening_;
4405 }
4406
4407 void Run();
4408 void WaitForListening() { listening_->Wait(); }
4409 char* body() { return *body_; }
4410
4411 private:
4412 int port_;
4413 i::SmartPointer<char> body_;
4414 i::Socket* server_; // Server socket used for bind/accept.
4415 i::Socket* client_; // Single client connection used by the test.
4416 i::Semaphore* listening_; // Signalled when the server is in listen mode.
4417};
4418
4419
4420void DebuggerAgentProtocolServerThread::Run() {
4421 bool ok;
4422
4423 // Create the server socket and bind it to the requested port.
4424 server_ = i::OS::CreateSocket();
4425 CHECK(server_ != NULL);
4426 ok = server_->Bind(port_);
4427 CHECK(ok);
4428
4429 // Listen for new connections.
4430 ok = server_->Listen(1);
4431 CHECK(ok);
4432 listening_->Signal();
4433
4434 // Accept a connection.
4435 client_ = server_->Accept();
4436 CHECK(client_ != NULL);
4437
4438 // Receive a debugger agent protocol message.
4439 i::DebuggerAgentUtil::ReceiveMessage(client_);
4440}
4441
4442
4443TEST(DebuggerAgentProtocolOverflowHeader) {
4444 // Make sure this port is not used by other tests to allow tests to run in
4445 // parallel.
4446 const int kPort = 5860;
4447 static const char* kLocalhost = "localhost";
4448
4449 // Make a string with the port number.
4450 const int kPortBufferLen = 6;
4451 char port_str[kPortBufferLen];
4452 OS::SNPrintF(i::Vector<char>(port_str, kPortBufferLen), "%d", kPort);
4453
4454 // Initialize the socket library.
4455 i::Socket::Setup();
4456
4457 // Create a socket server to receive a debugger agent message.
4458 DebuggerAgentProtocolServerThread* server =
4459 new DebuggerAgentProtocolServerThread(kPort);
4460 server->Start();
4461 server->WaitForListening();
4462
4463 // Connect.
4464 i::Socket* client = i::OS::CreateSocket();
4465 CHECK(client != NULL);
4466 bool ok = client->Connect(kLocalhost, port_str);
4467 CHECK(ok);
4468
4469 // Send headers which overflow the receive buffer.
4470 static const int kBufferSize = 1000;
4471 char buffer[kBufferSize];
4472
4473 // Long key and short value: XXXX....XXXX:0\r\n.
4474 for (int i = 0; i < kBufferSize - 4; i++) {
4475 buffer[i] = 'X';
4476 }
4477 buffer[kBufferSize - 4] = ':';
4478 buffer[kBufferSize - 3] = '0';
4479 buffer[kBufferSize - 2] = '\r';
4480 buffer[kBufferSize - 1] = '\n';
4481 client->Send(buffer, kBufferSize);
4482
4483 // Short key and long value: X:XXXX....XXXX\r\n.
4484 buffer[0] = 'X';
4485 buffer[1] = ':';
4486 for (int i = 2; i < kBufferSize - 2; i++) {
4487 buffer[i] = 'X';
4488 }
4489 buffer[kBufferSize - 2] = '\r';
4490 buffer[kBufferSize - 1] = '\n';
4491 client->Send(buffer, kBufferSize);
4492
4493 // Add empty body to request.
4494 const char* content_length_zero_header = "Content-Length:0\r\n";
4495 client->Send(content_length_zero_header, strlen(content_length_zero_header));
4496 client->Send("\r\n", 2);
4497
4498 // Wait until data is received.
4499 server->Join();
4500
4501 // Check for empty body.
4502 CHECK(server->body() == NULL);
4503
4504 // Close the client before the server to avoid TIME_WAIT issues.
4505 client->Shutdown();
4506 delete client;
4507 delete server;
4508}
ager@chromium.org41826e72009-03-30 13:30:57 +00004509
4510
4511// Test for issue http://code.google.com/p/v8/issues/detail?id=289.
4512// Make sure that DebugGetLoadedScripts doesn't return scripts
4513// with disposed external source.
4514class EmptyExternalStringResource : public v8::String::ExternalStringResource {
4515 public:
4516 EmptyExternalStringResource() { empty_[0] = 0; }
4517 virtual ~EmptyExternalStringResource() {}
4518 virtual size_t length() const { return empty_.length(); }
4519 virtual const uint16_t* data() const { return empty_.start(); }
4520 private:
4521 ::v8::internal::EmbeddedVector<uint16_t, 1> empty_;
4522};
4523
4524
4525TEST(DebugGetLoadedScripts) {
4526 v8::HandleScope scope;
4527 DebugLocalContext env;
4528 EmptyExternalStringResource source_ext_str;
4529 v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str);
4530 v8::Handle<v8::Script> evil_script = v8::Script::Compile(source);
4531 Handle<i::ExternalTwoByteString> i_source(
4532 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
4533 // This situation can happen if source was an external string disposed
4534 // by its owner.
4535 i_source->set_resource(0);
4536
4537 bool allow_natives_syntax = i::FLAG_allow_natives_syntax;
4538 i::FLAG_allow_natives_syntax = true;
4539 CompileRun(
4540 "var scripts = %DebugGetLoadedScripts();"
4541 "for (var i = 0; i < scripts.length; ++i) {"
4542 " scripts[i].line_ends;"
4543 "}");
4544 // Must not crash while accessing line_ends.
4545 i::FLAG_allow_natives_syntax = allow_natives_syntax;
4546}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004547
4548
4549// Test script break points set on lines.
4550TEST(ScriptNameAndData) {
4551 v8::HandleScope scope;
4552 DebugLocalContext env;
4553 env.ExposeDebug();
4554
4555 // Create functions for retrieving script name and data for the function on
4556 // the top frame when hitting a break point.
4557 frame_script_name = CompileFunction(&env,
4558 frame_script_name_source,
4559 "frame_script_name");
4560 frame_script_data = CompileFunction(&env,
4561 frame_script_data_source,
4562 "frame_script_data");
4563
4564 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
4565 v8::Undefined());
4566
4567 // Test function source.
4568 v8::Local<v8::String> script = v8::String::New(
4569 "function f() {\n"
4570 " debugger;\n"
4571 "}\n");
4572
4573 v8::ScriptOrigin origin1 = v8::ScriptOrigin(v8::String::New("name"));
4574 v8::Handle<v8::Script> script1 = v8::Script::Compile(script, &origin1);
4575 script1->SetData(v8::String::New("data"));
4576 script1->Run();
4577 v8::Script::Compile(script, &origin1)->Run();
4578 v8::Local<v8::Function> f;
4579 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
4580
4581 f->Call(env->Global(), 0, NULL);
4582 CHECK_EQ(1, break_point_hit_count);
4583 CHECK_EQ("name", last_script_name_hit);
4584 CHECK_EQ("data", last_script_data_hit);
4585
4586 v8::Local<v8::String> data_obj_source = v8::String::New(
4587 "({ a: 'abc',\n"
4588 " b: 123,\n"
4589 " toString: function() { return this.a + ' ' + this.b; }\n"
4590 "})\n");
4591 v8::Local<v8::Value> data_obj = v8::Script::Compile(data_obj_source)->Run();
4592 v8::ScriptOrigin origin2 = v8::ScriptOrigin(v8::String::New("new name"));
4593 v8::Handle<v8::Script> script2 = v8::Script::Compile(script, &origin2);
4594 script2->Run();
4595 script2->SetData(data_obj);
4596 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
4597 f->Call(env->Global(), 0, NULL);
4598 CHECK_EQ(2, break_point_hit_count);
4599 CHECK_EQ("new name", last_script_name_hit);
4600 CHECK_EQ("abc 123", last_script_data_hit);
4601}
ager@chromium.org9085a012009-05-11 19:22:57 +00004602
4603
4604static v8::Persistent<v8::Context> expected_context;
4605static v8::Handle<v8::Value> expected_context_data;
4606
4607
4608// Check that the expected context is the one generating the debug event.
4609static void ContextCheckMessageHandler(const v8::Debug::Message& message) {
4610 CHECK(message.GetEventContext() == expected_context);
4611 CHECK(message.GetEventContext()->GetData()->StrictEquals(
4612 expected_context_data));
4613 message_handler_hit_count++;
4614
4615 const int kBufferSize = 1000;
4616 uint16_t buffer[kBufferSize];
4617 const char* command_continue =
4618 "{\"seq\":0,"
4619 "\"type\":\"request\","
4620 "\"command\":\"continue\"}";
4621
4622 // Send a continue command for break events.
4623 if (message.GetEvent() == v8::Break) {
4624 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer));
4625 }
4626}
4627
4628
4629// Test which creates two contexts and sets different embedder data on each.
4630// Checks that this data is set correctly and that when the debug message
4631// handler is called the expected context is the one active.
4632TEST(ContextData) {
4633 v8::HandleScope scope;
4634
4635 v8::Debug::SetMessageHandler2(ContextCheckMessageHandler);
4636
4637 // Create two contexts.
4638 v8::Persistent<v8::Context> context_1;
4639 v8::Persistent<v8::Context> context_2;
4640 v8::Handle<v8::ObjectTemplate> global_template =
4641 v8::Handle<v8::ObjectTemplate>();
4642 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>();
4643 context_1 = v8::Context::New(NULL, global_template, global_object);
4644 context_2 = v8::Context::New(NULL, global_template, global_object);
4645
4646 // Default data value is undefined.
4647 CHECK(context_1->GetData()->IsUndefined());
4648 CHECK(context_2->GetData()->IsUndefined());
4649
4650 // Set and check different data values.
4651 v8::Handle<v8::Value> data_1 = v8::Number::New(1);
4652 v8::Handle<v8::Value> data_2 = v8::String::New("2");
4653 context_1->SetData(data_1);
4654 context_2->SetData(data_2);
4655 CHECK(context_1->GetData()->StrictEquals(data_1));
4656 CHECK(context_2->GetData()->StrictEquals(data_2));
4657
4658 // Simple test function which causes a break.
4659 const char* source = "function f() { debugger; }";
4660
4661 // Enter and run function in the first context.
4662 {
4663 v8::Context::Scope context_scope(context_1);
4664 expected_context = context_1;
4665 expected_context_data = data_1;
4666 v8::Local<v8::Function> f = CompileFunction(source, "f");
4667 f->Call(context_1->Global(), 0, NULL);
4668 }
4669
4670
4671 // Enter and run function in the second context.
4672 {
4673 v8::Context::Scope context_scope(context_2);
4674 expected_context = context_2;
4675 expected_context_data = data_2;
4676 v8::Local<v8::Function> f = CompileFunction(source, "f");
4677 f->Call(context_2->Global(), 0, NULL);
4678 }
4679
4680 // Two times compile event and two times break event.
4681 CHECK_GT(message_handler_hit_count, 4);
4682}