blob: b9f6038302d078fd1426fd521b523aec4008a4de [file] [log] [blame]
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001// Copyright 2010 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
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000028#ifdef ENABLE_DEBUGGER_SUPPORT
29
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000030#include <stdlib.h>
31
32#include "v8.h"
33
34#include "api.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000035#include "cctest.h"
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +000036#include "compilation-cache.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000037#include "debug.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000038#include "deoptimizer.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000039#include "platform.h"
40#include "stub-cache.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000041#include "utils.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000042
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000043
44using ::v8::internal::EmbeddedVector;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000045using ::v8::internal::Object;
46using ::v8::internal::OS;
47using ::v8::internal::Handle;
48using ::v8::internal::Heap;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000049using ::v8::internal::JSGlobalProxy;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050using ::v8::internal::Code;
51using ::v8::internal::Debug;
52using ::v8::internal::Debugger;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000053using ::v8::internal::CommandMessage;
54using ::v8::internal::CommandMessageQueue;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055using ::v8::internal::StepAction;
56using ::v8::internal::StepIn; // From StepAction enum
57using ::v8::internal::StepNext; // From StepAction enum
58using ::v8::internal::StepOut; // From StepAction enum
ager@chromium.org65dad4b2009-04-23 08:48:43 +000059using ::v8::internal::Vector;
ager@chromium.orgc4c92722009-11-18 14:12:51 +000060using ::v8::internal::StrLength;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061
62// Size of temp buffer for formatting small strings.
63#define SMALL_STRING_BUFFER_SIZE 80
64
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000065// --- A d d i t i o n a l C h e c k H e l p e r s
66
67
68// Helper function used by the CHECK_EQ function when given Address
69// arguments. Should not be called directly.
70static inline void CheckEqualsHelper(const char* file, int line,
71 const char* expected_source,
72 ::v8::internal::Address expected,
73 const char* value_source,
74 ::v8::internal::Address value) {
75 if (expected != value) {
76 V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# "
77 "Expected: %i\n# Found: %i",
78 expected_source, value_source, expected, value);
79 }
80}
81
82
83// Helper function used by the CHECK_NE function when given Address
84// arguments. Should not be called directly.
85static inline void CheckNonEqualsHelper(const char* file, int line,
86 const char* unexpected_source,
87 ::v8::internal::Address unexpected,
88 const char* value_source,
89 ::v8::internal::Address value) {
90 if (unexpected == value) {
91 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i",
92 unexpected_source, value_source, value);
93 }
94}
95
96
97// Helper function used by the CHECK function when given code
98// arguments. Should not be called directly.
99static inline void CheckEqualsHelper(const char* file, int line,
100 const char* expected_source,
101 const Code* expected,
102 const char* value_source,
103 const Code* value) {
104 if (expected != value) {
105 V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n# "
106 "Expected: %p\n# Found: %p",
107 expected_source, value_source, expected, value);
108 }
109}
110
111
112static inline void CheckNonEqualsHelper(const char* file, int line,
113 const char* expected_source,
114 const Code* expected,
115 const char* value_source,
116 const Code* value) {
117 if (expected == value) {
118 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p",
119 expected_source, value_source, value);
120 }
121}
122
123
124// --- H e l p e r C l a s s e s
125
126
127// Helper class for creating a V8 enviromnent for running tests
128class DebugLocalContext {
129 public:
130 inline DebugLocalContext(
131 v8::ExtensionConfiguration* extensions = 0,
132 v8::Handle<v8::ObjectTemplate> global_template =
133 v8::Handle<v8::ObjectTemplate>(),
134 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>())
135 : context_(v8::Context::New(extensions, global_template, global_object)) {
136 context_->Enter();
137 }
138 inline ~DebugLocalContext() {
139 context_->Exit();
140 context_.Dispose();
141 }
142 inline v8::Context* operator->() { return *context_; }
143 inline v8::Context* operator*() { return *context_; }
144 inline bool IsReady() { return !context_.IsEmpty(); }
145 void ExposeDebug() {
146 // Expose the debug context global object in the global object for testing.
147 Debug::Load();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000148 Debug::debug_context()->set_security_token(
149 v8::Utils::OpenHandle(*context_)->security_token());
150
151 Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast(
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000152 v8::Utils::OpenHandle(*context_->Global())));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000153 Handle<v8::internal::String> debug_string =
154 v8::internal::Factory::LookupAsciiSymbol("debug");
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000155 SetProperty(global, debug_string,
156 Handle<Object>(Debug::debug_context()->global_proxy()), DONT_ENUM);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000157 }
158 private:
159 v8::Persistent<v8::Context> context_;
160};
161
162
163// --- H e l p e r F u n c t i o n s
164
165
166// Compile and run the supplied source and return the fequested function.
167static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env,
168 const char* source,
169 const char* function_name) {
170 v8::Script::Compile(v8::String::New(source))->Run();
171 return v8::Local<v8::Function>::Cast(
172 (*env)->Global()->Get(v8::String::New(function_name)));
173}
174
ager@chromium.org9085a012009-05-11 19:22:57 +0000175
176// Compile and run the supplied source and return the requested function.
177static v8::Local<v8::Function> CompileFunction(const char* source,
178 const char* function_name) {
179 v8::Script::Compile(v8::String::New(source))->Run();
180 return v8::Local<v8::Function>::Cast(
181 v8::Context::GetCurrent()->Global()->Get(v8::String::New(function_name)));
182}
183
184
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000185// Is there any debug info for the function?
186static bool HasDebugInfo(v8::Handle<v8::Function> fun) {
187 Handle<v8::internal::JSFunction> f = v8::Utils::OpenHandle(*fun);
188 Handle<v8::internal::SharedFunctionInfo> shared(f->shared());
189 return Debug::HasDebugInfo(shared);
190}
191
192
193// Set a break point in a function and return the associated break point
194// number.
195static int SetBreakPoint(Handle<v8::internal::JSFunction> fun, int position) {
196 static int break_point = 0;
197 Handle<v8::internal::SharedFunctionInfo> shared(fun->shared());
198 Debug::SetBreakPoint(
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000199 shared,
200 Handle<Object>(v8::internal::Smi::FromInt(++break_point)),
201 &position);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000202 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.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000373namespace v8 {
374namespace internal {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000375
376// Collect the currently debugged functions.
377Handle<FixedArray> GetDebuggedFunctions() {
378 v8::internal::DebugInfoListNode* node = Debug::debug_info_list_;
379
380 // Find the number of debugged functions.
381 int count = 0;
382 while (node) {
383 count++;
384 node = node->next();
385 }
386
387 // Allocate array for the debugged functions
388 Handle<FixedArray> debugged_functions =
389 v8::internal::Factory::NewFixedArray(count);
390
391 // Run through the debug info objects and collect all functions.
392 count = 0;
393 while (node) {
394 debugged_functions->set(count++, *node->debug_info());
395 node = node->next();
396 }
397
398 return debugged_functions;
399}
400
401
402static Handle<Code> ComputeCallDebugBreak(int argc) {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000403 CALL_HEAP_FUNCTION(
404 v8::internal::StubCache::ComputeCallDebugBreak(argc, Code::CALL_IC),
405 Code);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000406}
407
ager@chromium.org381abbb2009-02-25 13:23:22 +0000408
ager@chromium.org381abbb2009-02-25 13:23:22 +0000409// Check that the debugger has been fully unloaded.
410void CheckDebuggerUnloaded(bool check_functions) {
411 // Check that the debugger context is cleared and that there is no debug
412 // information stored for the debugger.
413 CHECK(Debug::debug_context().is_null());
414 CHECK_EQ(NULL, Debug::debug_info_list_);
415
416 // Collect garbage to ensure weak handles are cleared.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000417 Heap::CollectAllGarbage(false);
418 Heap::CollectAllGarbage(false);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000419
420 // Iterate the head and check that there are no debugger related objects left.
421 HeapIterator iterator;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000422 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000423 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
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000445void ForceUnloadDebugger() {
446 Debugger::never_unload_debugger_ = false;
447 Debugger::UnloadDebugger();
448}
449
450
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000451} } // namespace v8::internal
452
ager@chromium.org381abbb2009-02-25 13:23:22 +0000453
ager@chromium.org381abbb2009-02-25 13:23:22 +0000454// Check that the debugger has been fully unloaded.
455static void CheckDebuggerUnloaded(bool check_functions = false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000456 // Let debugger to unload itself synchronously
457 v8::Debug::ProcessDebugMessages();
458
ager@chromium.org381abbb2009-02-25 13:23:22 +0000459 v8::internal::CheckDebuggerUnloaded(check_functions);
460}
461
462
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000463// Inherit from BreakLocationIterator to get access to protected parts for
464// testing.
465class TestBreakLocationIterator: public v8::internal::BreakLocationIterator {
466 public:
467 explicit TestBreakLocationIterator(Handle<v8::internal::DebugInfo> debug_info)
468 : BreakLocationIterator(debug_info, v8::internal::SOURCE_BREAK_LOCATIONS) {}
469 v8::internal::RelocIterator* it() { return reloc_iterator_; }
470 v8::internal::RelocIterator* it_original() {
471 return reloc_iterator_original_;
472 }
473};
474
475
476// Compile a function, set a break point and check that the call at the break
477// location in the code is the expected debug_break function.
478void CheckDebugBreakFunction(DebugLocalContext* env,
479 const char* source, const char* name,
ager@chromium.org236ad962008-09-25 09:45:57 +0000480 int position, v8::internal::RelocInfo::Mode mode,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000481 Code* debug_break) {
482 // Create function and set the break point.
483 Handle<v8::internal::JSFunction> fun = v8::Utils::OpenHandle(
484 *CompileFunction(env, source, name));
485 int bp = SetBreakPoint(fun, position);
486
487 // Check that the debug break function is as expected.
488 Handle<v8::internal::SharedFunctionInfo> shared(fun->shared());
489 CHECK(Debug::HasDebugInfo(shared));
490 TestBreakLocationIterator it1(Debug::GetDebugInfo(shared));
491 it1.FindBreakLocationFromPosition(position);
492 CHECK_EQ(mode, it1.it()->rinfo()->rmode());
ager@chromium.org236ad962008-09-25 09:45:57 +0000493 if (mode != v8::internal::RelocInfo::JS_RETURN) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000494 CHECK_EQ(debug_break,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000495 Code::GetCodeFromTargetAddress(it1.it()->rinfo()->target_address()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000496 } else {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000497 CHECK(Debug::IsDebugBreakAtReturn(it1.it()->rinfo()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000498 }
499
500 // Clear the break point and check that the debug break function is no longer
501 // there
502 ClearBreakPoint(bp);
503 CHECK(!Debug::HasDebugInfo(shared));
504 CHECK(Debug::EnsureDebugInfo(shared));
505 TestBreakLocationIterator it2(Debug::GetDebugInfo(shared));
506 it2.FindBreakLocationFromPosition(position);
507 CHECK_EQ(mode, it2.it()->rinfo()->rmode());
ager@chromium.org236ad962008-09-25 09:45:57 +0000508 if (mode == v8::internal::RelocInfo::JS_RETURN) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000509 CHECK(!Debug::IsDebugBreakAtReturn(it2.it()->rinfo()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000510 }
511}
512
513
514// --- D e b u g E v e n t H a n d l e r s
515// ---
516// --- The different tests uses a number of debug event handlers.
517// ---
518
519
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000520// Source for the JavaScript function which picks out the function
521// name of a frame.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000522const char* frame_function_name_source =
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000523 "function frame_function_name(exec_state, frame_number) {"
524 " return exec_state.frame(frame_number).func().name();"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000525 "}";
526v8::Local<v8::Function> frame_function_name;
527
ager@chromium.org8bb60582008-12-11 12:02:20 +0000528
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000529// Source for the JavaScript function which pick out the name of the
530// first argument of a frame.
531const char* frame_argument_name_source =
532 "function frame_argument_name(exec_state, frame_number) {"
533 " return exec_state.frame(frame_number).argumentName(0);"
534 "}";
535v8::Local<v8::Function> frame_argument_name;
536
537
538// Source for the JavaScript function which pick out the value of the
539// first argument of a frame.
540const char* frame_argument_value_source =
541 "function frame_argument_value(exec_state, frame_number) {"
542 " return exec_state.frame(frame_number).argumentValue(0).value_;"
543 "}";
544v8::Local<v8::Function> frame_argument_value;
545
546
547// Source for the JavaScript function which pick out the name of the
548// first argument of a frame.
549const char* frame_local_name_source =
550 "function frame_local_name(exec_state, frame_number) {"
551 " return exec_state.frame(frame_number).localName(0);"
552 "}";
553v8::Local<v8::Function> frame_local_name;
554
555
556// Source for the JavaScript function which pick out the value of the
557// first argument of a frame.
558const char* frame_local_value_source =
559 "function frame_local_value(exec_state, frame_number) {"
560 " return exec_state.frame(frame_number).localValue(0).value_;"
561 "}";
562v8::Local<v8::Function> frame_local_value;
563
564
565// Source for the JavaScript function which picks out the source line for the
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000566// top frame.
567const char* frame_source_line_source =
568 "function frame_source_line(exec_state) {"
569 " return exec_state.frame(0).sourceLine();"
570 "}";
571v8::Local<v8::Function> frame_source_line;
572
573
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000574// Source for the JavaScript function which picks out the source column for the
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000575// top frame.
576const char* frame_source_column_source =
577 "function frame_source_column(exec_state) {"
578 " return exec_state.frame(0).sourceColumn();"
579 "}";
580v8::Local<v8::Function> frame_source_column;
581
582
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000583// Source for the JavaScript function which picks out the script name for the
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000584// top frame.
585const char* frame_script_name_source =
586 "function frame_script_name(exec_state) {"
587 " return exec_state.frame(0).func().script().name();"
588 "}";
589v8::Local<v8::Function> frame_script_name;
590
591
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000592// Source for the JavaScript function which picks out the script data for the
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000593// top frame.
594const char* frame_script_data_source =
595 "function frame_script_data(exec_state) {"
596 " return exec_state.frame(0).func().script().data();"
597 "}";
598v8::Local<v8::Function> frame_script_data;
599
600
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000601// Source for the JavaScript function which picks out the script data from
ager@chromium.org5c838252010-02-19 08:53:10 +0000602// AfterCompile event
603const char* compiled_script_data_source =
604 "function compiled_script_data(event_data) {"
605 " return event_data.script().data();"
606 "}";
607v8::Local<v8::Function> compiled_script_data;
608
609
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000610// Source for the JavaScript function which returns the number of frames.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000611static const char* frame_count_source =
612 "function frame_count(exec_state) {"
613 " return exec_state.frameCount();"
614 "}";
615v8::Handle<v8::Function> frame_count;
616
617
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000618// Global variable to store the last function hit - used by some tests.
619char last_function_hit[80];
620
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000621// Global variable to store the name and data for last script hit - used by some
622// tests.
623char last_script_name_hit[80];
624char last_script_data_hit[80];
625
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000626// Global variables to store the last source position - used by some tests.
627int last_source_line = -1;
628int last_source_column = -1;
629
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000630// Debug event handler which counts the break points which have been hit.
631int break_point_hit_count = 0;
632static void DebugEventBreakPointHitCount(v8::DebugEvent event,
633 v8::Handle<v8::Object> exec_state,
634 v8::Handle<v8::Object> event_data,
635 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000636 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000637 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000638
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000639 // Count the number of breaks.
640 if (event == v8::Break) {
641 break_point_hit_count++;
642 if (!frame_function_name.IsEmpty()) {
643 // Get the name of the function.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000644 const int argc = 2;
645 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) };
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000646 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
647 argc, argv);
648 if (result->IsUndefined()) {
649 last_function_hit[0] = '\0';
650 } else {
651 CHECK(result->IsString());
652 v8::Handle<v8::String> function_name(result->ToString());
653 function_name->WriteAscii(last_function_hit);
654 }
655 }
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000656
657 if (!frame_source_line.IsEmpty()) {
658 // Get the source line.
659 const int argc = 1;
660 v8::Handle<v8::Value> argv[argc] = { exec_state };
661 v8::Handle<v8::Value> result = frame_source_line->Call(exec_state,
662 argc, argv);
663 CHECK(result->IsNumber());
664 last_source_line = result->Int32Value();
665 }
666
667 if (!frame_source_column.IsEmpty()) {
668 // Get the source column.
669 const int argc = 1;
670 v8::Handle<v8::Value> argv[argc] = { exec_state };
671 v8::Handle<v8::Value> result = frame_source_column->Call(exec_state,
672 argc, argv);
673 CHECK(result->IsNumber());
674 last_source_column = result->Int32Value();
675 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000676
677 if (!frame_script_name.IsEmpty()) {
678 // Get the script name of the function script.
679 const int argc = 1;
680 v8::Handle<v8::Value> argv[argc] = { exec_state };
681 v8::Handle<v8::Value> result = frame_script_name->Call(exec_state,
682 argc, argv);
683 if (result->IsUndefined()) {
684 last_script_name_hit[0] = '\0';
685 } else {
686 CHECK(result->IsString());
687 v8::Handle<v8::String> script_name(result->ToString());
688 script_name->WriteAscii(last_script_name_hit);
689 }
690 }
691
692 if (!frame_script_data.IsEmpty()) {
693 // Get the script data of the function script.
694 const int argc = 1;
695 v8::Handle<v8::Value> argv[argc] = { exec_state };
696 v8::Handle<v8::Value> result = frame_script_data->Call(exec_state,
697 argc, argv);
698 if (result->IsUndefined()) {
699 last_script_data_hit[0] = '\0';
700 } else {
701 result = result->ToString();
702 CHECK(result->IsString());
703 v8::Handle<v8::String> script_data(result->ToString());
704 script_data->WriteAscii(last_script_data_hit);
705 }
706 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000707 } else if (event == v8::AfterCompile && !compiled_script_data.IsEmpty()) {
708 const int argc = 1;
709 v8::Handle<v8::Value> argv[argc] = { event_data };
710 v8::Handle<v8::Value> result = compiled_script_data->Call(exec_state,
711 argc, argv);
712 if (result->IsUndefined()) {
713 last_script_data_hit[0] = '\0';
714 } else {
715 result = result->ToString();
716 CHECK(result->IsString());
717 v8::Handle<v8::String> script_data(result->ToString());
718 script_data->WriteAscii(last_script_data_hit);
719 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000720 }
721}
722
723
ager@chromium.org8bb60582008-12-11 12:02:20 +0000724// Debug event handler which counts a number of events and collects the stack
725// height if there is a function compiled for that.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000726int exception_hit_count = 0;
727int uncaught_exception_hit_count = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000728int last_js_stack_height = -1;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000729
730static void DebugEventCounterClear() {
731 break_point_hit_count = 0;
732 exception_hit_count = 0;
733 uncaught_exception_hit_count = 0;
734}
735
736static void DebugEventCounter(v8::DebugEvent event,
737 v8::Handle<v8::Object> exec_state,
738 v8::Handle<v8::Object> event_data,
739 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000740 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000741 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000742
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000743 // Count the number of breaks.
744 if (event == v8::Break) {
745 break_point_hit_count++;
746 } else if (event == v8::Exception) {
747 exception_hit_count++;
748
749 // Check whether the exception was uncaught.
750 v8::Local<v8::String> fun_name = v8::String::New("uncaught");
751 v8::Local<v8::Function> fun =
752 v8::Function::Cast(*event_data->Get(fun_name));
753 v8::Local<v8::Value> result = *fun->Call(event_data, 0, NULL);
754 if (result->IsTrue()) {
755 uncaught_exception_hit_count++;
756 }
757 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000758
759 // Collect the JavsScript stack height if the function frame_count is
760 // compiled.
761 if (!frame_count.IsEmpty()) {
762 static const int kArgc = 1;
763 v8::Handle<v8::Value> argv[kArgc] = { exec_state };
764 // Using exec_state as receiver is just to have a receiver.
765 v8::Handle<v8::Value> result = frame_count->Call(exec_state, kArgc, argv);
766 last_js_stack_height = result->Int32Value();
767 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000768}
769
770
771// Debug event handler which evaluates a number of expressions when a break
772// point is hit. Each evaluated expression is compared with an expected value.
773// For this debug event handler to work the following two global varaibles
774// must be initialized.
775// checks: An array of expressions and expected results
776// evaluate_check_function: A JavaScript function (see below)
777
778// Structure for holding checks to do.
779struct EvaluateCheck {
780 const char* expr; // An expression to evaluate when a break point is hit.
781 v8::Handle<v8::Value> expected; // The expected result.
782};
783// Array of checks to do.
784struct EvaluateCheck* checks = NULL;
785// Source for The JavaScript function which can do the evaluation when a break
786// point is hit.
787const char* evaluate_check_source =
788 "function evaluate_check(exec_state, expr, expected) {"
789 " return exec_state.frame(0).evaluate(expr).value() === expected;"
790 "}";
791v8::Local<v8::Function> evaluate_check_function;
792
793// The actual debug event described by the longer comment above.
794static void DebugEventEvaluate(v8::DebugEvent event,
795 v8::Handle<v8::Object> exec_state,
796 v8::Handle<v8::Object> event_data,
797 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000798 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000799 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000800
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000801 if (event == v8::Break) {
802 for (int i = 0; checks[i].expr != NULL; i++) {
803 const int argc = 3;
804 v8::Handle<v8::Value> argv[argc] = { exec_state,
805 v8::String::New(checks[i].expr),
806 checks[i].expected };
807 v8::Handle<v8::Value> result =
808 evaluate_check_function->Call(exec_state, argc, argv);
809 if (!result->IsTrue()) {
810 v8::String::AsciiValue ascii(checks[i].expected->ToString());
811 V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *ascii);
812 }
813 }
814 }
815}
816
817
818// This debug event listener removes a breakpoint in a function
819int debug_event_remove_break_point = 0;
820static void DebugEventRemoveBreakPoint(v8::DebugEvent event,
821 v8::Handle<v8::Object> exec_state,
822 v8::Handle<v8::Object> event_data,
823 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000824 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000825 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000826
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000827 if (event == v8::Break) {
828 break_point_hit_count++;
829 v8::Handle<v8::Function> fun = v8::Handle<v8::Function>::Cast(data);
830 ClearBreakPoint(debug_event_remove_break_point);
831 }
832}
833
834
835// Debug event handler which counts break points hit and performs a step
836// afterwards.
837StepAction step_action = StepIn; // Step action to perform when stepping.
838static void DebugEventStep(v8::DebugEvent event,
839 v8::Handle<v8::Object> exec_state,
840 v8::Handle<v8::Object> event_data,
841 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000842 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000843 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000844
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000845 if (event == v8::Break) {
846 break_point_hit_count++;
847 PrepareStep(step_action);
848 }
849}
850
851
852// Debug event handler which counts break points hit and performs a step
853// afterwards. For each call the expected function is checked.
854// For this debug event handler to work the following two global varaibles
855// must be initialized.
856// expected_step_sequence: An array of the expected function call sequence.
857// frame_function_name: A JavaScript function (see below).
858
859// String containing the expected function call sequence. Note: this only works
860// if functions have name length of one.
861const char* expected_step_sequence = NULL;
862
863// The actual debug event described by the longer comment above.
864static void DebugEventStepSequence(v8::DebugEvent event,
865 v8::Handle<v8::Object> exec_state,
866 v8::Handle<v8::Object> event_data,
867 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000868 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000869 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000870
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000871 if (event == v8::Break || event == v8::Exception) {
872 // Check that the current function is the expected.
873 CHECK(break_point_hit_count <
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000874 StrLength(expected_step_sequence));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000875 const int argc = 2;
876 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) };
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000877 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
878 argc, argv);
879 CHECK(result->IsString());
880 v8::String::AsciiValue function_name(result->ToString());
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000881 CHECK_EQ(1, StrLength(*function_name));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000882 CHECK_EQ((*function_name)[0],
883 expected_step_sequence[break_point_hit_count]);
884
885 // Perform step.
886 break_point_hit_count++;
887 PrepareStep(step_action);
888 }
889}
890
891
892// Debug event handler which performs a garbage collection.
893static void DebugEventBreakPointCollectGarbage(
894 v8::DebugEvent event,
895 v8::Handle<v8::Object> exec_state,
896 v8::Handle<v8::Object> event_data,
897 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000898 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000899 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000900
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000901 // Perform a garbage collection when break point is hit and continue. Based
902 // on the number of break points hit either scavenge or mark compact
903 // collector is used.
904 if (event == v8::Break) {
905 break_point_hit_count++;
906 if (break_point_hit_count % 2 == 0) {
907 // Scavenge.
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000908 Heap::CollectGarbage(v8::internal::NEW_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000909 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000910 // Mark sweep compact.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000911 Heap::CollectAllGarbage(true);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000912 }
913 }
914}
915
916
917// Debug event handler which re-issues a debug break and calls the garbage
918// collector to have the heap verified.
919static void DebugEventBreak(v8::DebugEvent event,
920 v8::Handle<v8::Object> exec_state,
921 v8::Handle<v8::Object> event_data,
922 v8::Handle<v8::Value> data) {
ager@chromium.org8bb60582008-12-11 12:02:20 +0000923 // When hitting a debug event listener there must be a break set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000924 CHECK_NE(v8::internal::Debug::break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000925
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000926 if (event == v8::Break) {
927 // Count the number of breaks.
928 break_point_hit_count++;
929
930 // Run the garbage collector to enforce heap verification if option
931 // --verify-heap is set.
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000932 Heap::CollectGarbage(v8::internal::NEW_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000933
934 // Set the break flag again to come back here as soon as possible.
935 v8::Debug::DebugBreak();
936 }
937}
938
939
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000940// Debug event handler which re-issues a debug break until a limit has been
941// reached.
942int max_break_point_hit_count = 0;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000943bool terminate_after_max_break_point_hit = false;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000944static void DebugEventBreakMax(v8::DebugEvent event,
945 v8::Handle<v8::Object> exec_state,
946 v8::Handle<v8::Object> event_data,
947 v8::Handle<v8::Value> data) {
948 // When hitting a debug event listener there must be a break set.
949 CHECK_NE(v8::internal::Debug::break_id(), 0);
950
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000951 if (event == v8::Break) {
952 if (break_point_hit_count < max_break_point_hit_count) {
953 // Count the number of breaks.
954 break_point_hit_count++;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000955
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000956 // Set the break flag again to come back here as soon as possible.
957 v8::Debug::DebugBreak();
958 } else if (terminate_after_max_break_point_hit) {
959 // Terminate execution after the last break if requested.
960 v8::V8::TerminateExecution();
961 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000962 }
963}
964
965
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000966// --- M e s s a g e C a l l b a c k
967
968
969// Message callback which counts the number of messages.
970int message_callback_count = 0;
971
972static void MessageCallbackCountClear() {
973 message_callback_count = 0;
974}
975
976static void MessageCallbackCount(v8::Handle<v8::Message> message,
977 v8::Handle<v8::Value> data) {
978 message_callback_count++;
979}
980
981
982// --- T h e A c t u a l T e s t s
983
984
985// Test that the debug break function is the expected one for different kinds
986// of break locations.
987TEST(DebugStub) {
988 using ::v8::internal::Builtins;
989 v8::HandleScope scope;
990 DebugLocalContext env;
991
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000992 CheckDebugBreakFunction(&env,
993 "function f1(){}", "f1",
994 0,
ager@chromium.org236ad962008-09-25 09:45:57 +0000995 v8::internal::RelocInfo::JS_RETURN,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000996 NULL);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000997 CheckDebugBreakFunction(&env,
998 "function f2(){x=1;}", "f2",
999 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001000 v8::internal::RelocInfo::CODE_TARGET,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001001 Builtins::builtin(Builtins::StoreIC_DebugBreak));
1002 CheckDebugBreakFunction(&env,
1003 "function f3(){var a=x;}", "f3",
1004 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001005 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001006 Builtins::builtin(Builtins::LoadIC_DebugBreak));
1007
ager@chromium.org236ad962008-09-25 09:45:57 +00001008// TODO(1240753): Make the test architecture independent or split
1009// parts of the debugger into architecture dependent files. This
1010// part currently disabled as it is not portable between IA32/ARM.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001011// Currently on ICs for keyed store/load on ARM.
1012#if !defined (__arm__) && !defined(__thumb__)
1013 CheckDebugBreakFunction(
1014 &env,
1015 "function f4(){var index='propertyName'; var a={}; a[index] = 'x';}",
1016 "f4",
1017 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001018 v8::internal::RelocInfo::CODE_TARGET,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001019 Builtins::builtin(Builtins::KeyedStoreIC_DebugBreak));
1020 CheckDebugBreakFunction(
1021 &env,
1022 "function f5(){var index='propertyName'; var a={}; return a[index];}",
1023 "f5",
1024 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001025 v8::internal::RelocInfo::CODE_TARGET,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001026 Builtins::builtin(Builtins::KeyedLoadIC_DebugBreak));
1027#endif
1028
1029 // Check the debug break code stubs for call ICs with different number of
1030 // parameters.
1031 Handle<Code> debug_break_0 = v8::internal::ComputeCallDebugBreak(0);
1032 Handle<Code> debug_break_1 = v8::internal::ComputeCallDebugBreak(1);
1033 Handle<Code> debug_break_4 = v8::internal::ComputeCallDebugBreak(4);
1034
1035 CheckDebugBreakFunction(&env,
1036 "function f4_0(){x();}", "f4_0",
1037 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001038 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001039 *debug_break_0);
1040
1041 CheckDebugBreakFunction(&env,
1042 "function f4_1(){x(1);}", "f4_1",
1043 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001044 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001045 *debug_break_1);
1046
1047 CheckDebugBreakFunction(&env,
1048 "function f4_4(){x(1,2,3,4);}", "f4_4",
1049 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001050 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001051 *debug_break_4);
1052}
1053
1054
1055// Test that the debug info in the VM is in sync with the functions being
1056// debugged.
1057TEST(DebugInfo) {
1058 v8::HandleScope scope;
1059 DebugLocalContext env;
1060 // Create a couple of functions for the test.
1061 v8::Local<v8::Function> foo =
1062 CompileFunction(&env, "function foo(){}", "foo");
1063 v8::Local<v8::Function> bar =
1064 CompileFunction(&env, "function bar(){}", "bar");
1065 // Initially no functions are debugged.
1066 CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
1067 CHECK(!HasDebugInfo(foo));
1068 CHECK(!HasDebugInfo(bar));
1069 // One function (foo) is debugged.
1070 int bp1 = SetBreakPoint(foo, 0);
1071 CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
1072 CHECK(HasDebugInfo(foo));
1073 CHECK(!HasDebugInfo(bar));
1074 // Two functions are debugged.
1075 int bp2 = SetBreakPoint(bar, 0);
1076 CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length());
1077 CHECK(HasDebugInfo(foo));
1078 CHECK(HasDebugInfo(bar));
1079 // One function (bar) is debugged.
1080 ClearBreakPoint(bp1);
1081 CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
1082 CHECK(!HasDebugInfo(foo));
1083 CHECK(HasDebugInfo(bar));
1084 // No functions are debugged.
1085 ClearBreakPoint(bp2);
1086 CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
1087 CHECK(!HasDebugInfo(foo));
1088 CHECK(!HasDebugInfo(bar));
1089}
1090
1091
1092// Test that a break point can be set at an IC store location.
1093TEST(BreakPointICStore) {
1094 break_point_hit_count = 0;
1095 v8::HandleScope scope;
1096 DebugLocalContext env;
ager@chromium.org381abbb2009-02-25 13:23:22 +00001097
iposva@chromium.org245aa852009-02-10 00:49:54 +00001098 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001099 v8::Undefined());
1100 v8::Script::Compile(v8::String::New("function foo(){bar=0;}"))->Run();
1101 v8::Local<v8::Function> foo =
1102 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1103
1104 // Run without breakpoints.
1105 foo->Call(env->Global(), 0, NULL);
1106 CHECK_EQ(0, break_point_hit_count);
1107
1108 // Run with breakpoint
1109 int bp = SetBreakPoint(foo, 0);
1110 foo->Call(env->Global(), 0, NULL);
1111 CHECK_EQ(1, break_point_hit_count);
1112 foo->Call(env->Global(), 0, NULL);
1113 CHECK_EQ(2, break_point_hit_count);
1114
1115 // Run without breakpoints.
1116 ClearBreakPoint(bp);
1117 foo->Call(env->Global(), 0, NULL);
1118 CHECK_EQ(2, break_point_hit_count);
1119
iposva@chromium.org245aa852009-02-10 00:49:54 +00001120 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001121 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001122}
1123
1124
1125// Test that a break point can be set at an IC load location.
1126TEST(BreakPointICLoad) {
1127 break_point_hit_count = 0;
1128 v8::HandleScope scope;
1129 DebugLocalContext env;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001130 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001131 v8::Undefined());
1132 v8::Script::Compile(v8::String::New("bar=1"))->Run();
1133 v8::Script::Compile(v8::String::New("function foo(){var x=bar;}"))->Run();
1134 v8::Local<v8::Function> foo =
1135 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1136
1137 // Run without breakpoints.
1138 foo->Call(env->Global(), 0, NULL);
1139 CHECK_EQ(0, break_point_hit_count);
1140
1141 // Run with breakpoint
1142 int bp = SetBreakPoint(foo, 0);
1143 foo->Call(env->Global(), 0, NULL);
1144 CHECK_EQ(1, break_point_hit_count);
1145 foo->Call(env->Global(), 0, NULL);
1146 CHECK_EQ(2, break_point_hit_count);
1147
1148 // Run without breakpoints.
1149 ClearBreakPoint(bp);
1150 foo->Call(env->Global(), 0, NULL);
1151 CHECK_EQ(2, break_point_hit_count);
1152
iposva@chromium.org245aa852009-02-10 00:49:54 +00001153 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001154 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001155}
1156
1157
1158// Test that a break point can be set at an IC call location.
1159TEST(BreakPointICCall) {
1160 break_point_hit_count = 0;
1161 v8::HandleScope scope;
1162 DebugLocalContext env;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001163 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001164 v8::Undefined());
1165 v8::Script::Compile(v8::String::New("function bar(){}"))->Run();
1166 v8::Script::Compile(v8::String::New("function foo(){bar();}"))->Run();
1167 v8::Local<v8::Function> foo =
1168 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1169
1170 // Run without breakpoints.
1171 foo->Call(env->Global(), 0, NULL);
1172 CHECK_EQ(0, break_point_hit_count);
1173
ricow@chromium.org65fae842010-08-25 15:26:24 +00001174 // Run with breakpoint.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001175 int bp = SetBreakPoint(foo, 0);
1176 foo->Call(env->Global(), 0, NULL);
1177 CHECK_EQ(1, break_point_hit_count);
1178 foo->Call(env->Global(), 0, NULL);
1179 CHECK_EQ(2, break_point_hit_count);
1180
1181 // Run without breakpoints.
1182 ClearBreakPoint(bp);
1183 foo->Call(env->Global(), 0, NULL);
1184 CHECK_EQ(2, break_point_hit_count);
1185
iposva@chromium.org245aa852009-02-10 00:49:54 +00001186 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001187 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001188}
1189
1190
ricow@chromium.org65fae842010-08-25 15:26:24 +00001191// Test that a break point can be set at an IC call location and survive a GC.
1192TEST(BreakPointICCallWithGC) {
1193 break_point_hit_count = 0;
1194 v8::HandleScope scope;
1195 DebugLocalContext env;
1196 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage,
1197 v8::Undefined());
1198 v8::Script::Compile(v8::String::New("function bar(){return 1;}"))->Run();
1199 v8::Script::Compile(v8::String::New("function foo(){return bar();}"))->Run();
1200 v8::Local<v8::Function> foo =
1201 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1202
1203 // Run without breakpoints.
1204 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1205 CHECK_EQ(0, break_point_hit_count);
1206
1207 // Run with breakpoint.
1208 int bp = SetBreakPoint(foo, 0);
1209 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1210 CHECK_EQ(1, break_point_hit_count);
1211 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1212 CHECK_EQ(2, break_point_hit_count);
1213
1214 // Run without breakpoints.
1215 ClearBreakPoint(bp);
1216 foo->Call(env->Global(), 0, NULL);
1217 CHECK_EQ(2, break_point_hit_count);
1218
1219 v8::Debug::SetDebugEventListener(NULL);
1220 CheckDebuggerUnloaded();
1221}
1222
1223
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001224// Test that a break point can be set at an IC call location and survive a GC.
1225TEST(BreakPointConstructCallWithGC) {
1226 break_point_hit_count = 0;
1227 v8::HandleScope scope;
1228 DebugLocalContext env;
1229 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage,
1230 v8::Undefined());
1231 v8::Script::Compile(v8::String::New("function bar(){ this.x = 1;}"))->Run();
1232 v8::Script::Compile(v8::String::New(
1233 "function foo(){return new bar(1).x;}"))->Run();
1234 v8::Local<v8::Function> foo =
1235 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1236
1237 // Run without breakpoints.
1238 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1239 CHECK_EQ(0, break_point_hit_count);
1240
1241 // Run with breakpoint.
1242 int bp = SetBreakPoint(foo, 0);
1243 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1244 CHECK_EQ(1, break_point_hit_count);
1245 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1246 CHECK_EQ(2, break_point_hit_count);
1247
1248 // Run without breakpoints.
1249 ClearBreakPoint(bp);
1250 foo->Call(env->Global(), 0, NULL);
1251 CHECK_EQ(2, break_point_hit_count);
1252
1253 v8::Debug::SetDebugEventListener(NULL);
1254 CheckDebuggerUnloaded();
1255}
1256
1257
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001258// Test that a break point can be set at a return store location.
1259TEST(BreakPointReturn) {
1260 break_point_hit_count = 0;
1261 v8::HandleScope scope;
1262 DebugLocalContext env;
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001263
1264 // Create a functions for checking the source line and column when hitting
1265 // a break point.
1266 frame_source_line = CompileFunction(&env,
1267 frame_source_line_source,
1268 "frame_source_line");
1269 frame_source_column = CompileFunction(&env,
1270 frame_source_column_source,
1271 "frame_source_column");
1272
1273
iposva@chromium.org245aa852009-02-10 00:49:54 +00001274 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001275 v8::Undefined());
1276 v8::Script::Compile(v8::String::New("function foo(){}"))->Run();
1277 v8::Local<v8::Function> foo =
1278 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1279
1280 // Run without breakpoints.
1281 foo->Call(env->Global(), 0, NULL);
1282 CHECK_EQ(0, break_point_hit_count);
1283
1284 // Run with breakpoint
1285 int bp = SetBreakPoint(foo, 0);
1286 foo->Call(env->Global(), 0, NULL);
1287 CHECK_EQ(1, break_point_hit_count);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001288 CHECK_EQ(0, last_source_line);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001289 CHECK_EQ(15, last_source_column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001290 foo->Call(env->Global(), 0, NULL);
1291 CHECK_EQ(2, break_point_hit_count);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001292 CHECK_EQ(0, last_source_line);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001293 CHECK_EQ(15, last_source_column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001294
1295 // Run without breakpoints.
1296 ClearBreakPoint(bp);
1297 foo->Call(env->Global(), 0, NULL);
1298 CHECK_EQ(2, break_point_hit_count);
1299
iposva@chromium.org245aa852009-02-10 00:49:54 +00001300 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001301 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001302}
1303
1304
1305static void CallWithBreakPoints(v8::Local<v8::Object> recv,
1306 v8::Local<v8::Function> f,
1307 int break_point_count,
1308 int call_count) {
1309 break_point_hit_count = 0;
1310 for (int i = 0; i < call_count; i++) {
1311 f->Call(recv, 0, NULL);
1312 CHECK_EQ((i + 1) * break_point_count, break_point_hit_count);
1313 }
1314}
1315
1316// Test GC during break point processing.
1317TEST(GCDuringBreakPointProcessing) {
1318 break_point_hit_count = 0;
1319 v8::HandleScope scope;
1320 DebugLocalContext env;
1321
iposva@chromium.org245aa852009-02-10 00:49:54 +00001322 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001323 v8::Undefined());
1324 v8::Local<v8::Function> foo;
1325
1326 // Test IC store break point with garbage collection.
1327 foo = CompileFunction(&env, "function foo(){bar=0;}", "foo");
1328 SetBreakPoint(foo, 0);
1329 CallWithBreakPoints(env->Global(), foo, 1, 10);
1330
1331 // Test IC load break point with garbage collection.
1332 foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo");
1333 SetBreakPoint(foo, 0);
1334 CallWithBreakPoints(env->Global(), foo, 1, 10);
1335
1336 // Test IC call break point with garbage collection.
1337 foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo");
1338 SetBreakPoint(foo, 0);
1339 CallWithBreakPoints(env->Global(), foo, 1, 10);
1340
1341 // Test return break point with garbage collection.
1342 foo = CompileFunction(&env, "function foo(){}", "foo");
1343 SetBreakPoint(foo, 0);
1344 CallWithBreakPoints(env->Global(), foo, 1, 25);
1345
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001346 // Test debug break slot break point with garbage collection.
1347 foo = CompileFunction(&env, "function foo(){var a;}", "foo");
1348 SetBreakPoint(foo, 0);
1349 CallWithBreakPoints(env->Global(), foo, 1, 25);
1350
iposva@chromium.org245aa852009-02-10 00:49:54 +00001351 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001352 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001353}
1354
1355
1356// Call the function three times with different garbage collections in between
1357// and make sure that the break point survives.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001358static void CallAndGC(v8::Local<v8::Object> recv,
1359 v8::Local<v8::Function> f,
1360 bool force_compaction) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001361 break_point_hit_count = 0;
1362
1363 for (int i = 0; i < 3; i++) {
1364 // Call function.
1365 f->Call(recv, 0, NULL);
1366 CHECK_EQ(1 + i * 3, break_point_hit_count);
1367
1368 // Scavenge and call function.
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001369 Heap::CollectGarbage(v8::internal::NEW_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001370 f->Call(recv, 0, NULL);
1371 CHECK_EQ(2 + i * 3, break_point_hit_count);
1372
1373 // Mark sweep (and perhaps compact) and call function.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001374 Heap::CollectAllGarbage(force_compaction);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001375 f->Call(recv, 0, NULL);
1376 CHECK_EQ(3 + i * 3, break_point_hit_count);
1377 }
1378}
1379
1380
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001381static void TestBreakPointSurviveGC(bool force_compaction) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001382 break_point_hit_count = 0;
1383 v8::HandleScope scope;
1384 DebugLocalContext env;
1385
iposva@chromium.org245aa852009-02-10 00:49:54 +00001386 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001387 v8::Undefined());
1388 v8::Local<v8::Function> foo;
1389
1390 // Test IC store break point with garbage collection.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001391 {
1392 v8::Local<v8::Function> bar =
1393 CompileFunction(&env, "function foo(){}", "foo");
1394 foo = CompileFunction(&env, "function foo(){bar=0;}", "foo");
1395 SetBreakPoint(foo, 0);
1396 }
1397 CallAndGC(env->Global(), foo, force_compaction);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001398
1399 // Test IC load break point with garbage collection.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001400 {
1401 v8::Local<v8::Function> bar =
1402 CompileFunction(&env, "function foo(){}", "foo");
1403 foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo");
1404 SetBreakPoint(foo, 0);
1405 }
1406 CallAndGC(env->Global(), foo, force_compaction);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001407
1408 // Test IC call break point with garbage collection.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001409 {
1410 v8::Local<v8::Function> bar =
1411 CompileFunction(&env, "function foo(){}", "foo");
1412 foo = CompileFunction(&env,
1413 "function bar(){};function foo(){bar();}",
1414 "foo");
1415 SetBreakPoint(foo, 0);
1416 }
1417 CallAndGC(env->Global(), foo, force_compaction);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001418
1419 // Test return break point with garbage collection.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001420 {
1421 v8::Local<v8::Function> bar =
1422 CompileFunction(&env, "function foo(){}", "foo");
1423 foo = CompileFunction(&env, "function foo(){}", "foo");
1424 SetBreakPoint(foo, 0);
1425 }
1426 CallAndGC(env->Global(), foo, force_compaction);
1427
1428 // Test non IC break point with garbage collection.
1429 {
1430 v8::Local<v8::Function> bar =
1431 CompileFunction(&env, "function foo(){}", "foo");
1432 foo = CompileFunction(&env, "function foo(){var bar=0;}", "foo");
1433 SetBreakPoint(foo, 0);
1434 }
1435 CallAndGC(env->Global(), foo, force_compaction);
1436
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001437
iposva@chromium.org245aa852009-02-10 00:49:54 +00001438 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001439 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001440}
1441
1442
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001443// Test that a break point can be set at a return store location.
1444TEST(BreakPointSurviveGC) {
1445 TestBreakPointSurviveGC(false);
1446 TestBreakPointSurviveGC(true);
1447}
1448
1449
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001450// Test that break points can be set using the global Debug object.
1451TEST(BreakPointThroughJavaScript) {
1452 break_point_hit_count = 0;
1453 v8::HandleScope scope;
1454 DebugLocalContext env;
1455 env.ExposeDebug();
1456
iposva@chromium.org245aa852009-02-10 00:49:54 +00001457 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001458 v8::Undefined());
1459 v8::Script::Compile(v8::String::New("function bar(){}"))->Run();
1460 v8::Script::Compile(v8::String::New("function foo(){bar();bar();}"))->Run();
1461 // 012345678901234567890
1462 // 1 2
1463 // Break points are set at position 3 and 9
1464 v8::Local<v8::Script> foo = v8::Script::Compile(v8::String::New("foo()"));
1465
1466 // Run without breakpoints.
1467 foo->Run();
1468 CHECK_EQ(0, break_point_hit_count);
1469
1470 // Run with one breakpoint
1471 int bp1 = SetBreakPointFromJS("foo", 0, 3);
1472 foo->Run();
1473 CHECK_EQ(1, break_point_hit_count);
1474 foo->Run();
1475 CHECK_EQ(2, break_point_hit_count);
1476
1477 // Run with two breakpoints
1478 int bp2 = SetBreakPointFromJS("foo", 0, 9);
1479 foo->Run();
1480 CHECK_EQ(4, break_point_hit_count);
1481 foo->Run();
1482 CHECK_EQ(6, break_point_hit_count);
1483
1484 // Run with one breakpoint
1485 ClearBreakPointFromJS(bp2);
1486 foo->Run();
1487 CHECK_EQ(7, break_point_hit_count);
1488 foo->Run();
1489 CHECK_EQ(8, break_point_hit_count);
1490
1491 // Run without breakpoints.
1492 ClearBreakPointFromJS(bp1);
1493 foo->Run();
1494 CHECK_EQ(8, break_point_hit_count);
1495
iposva@chromium.org245aa852009-02-10 00:49:54 +00001496 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001497 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001498
1499 // Make sure that the break point numbers are consecutive.
1500 CHECK_EQ(1, bp1);
1501 CHECK_EQ(2, bp2);
1502}
1503
1504
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001505// Test that break points on scripts identified by name can be set using the
1506// global Debug object.
1507TEST(ScriptBreakPointByNameThroughJavaScript) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001508 break_point_hit_count = 0;
1509 v8::HandleScope scope;
1510 DebugLocalContext env;
1511 env.ExposeDebug();
1512
iposva@chromium.org245aa852009-02-10 00:49:54 +00001513 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001514 v8::Undefined());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001515
1516 v8::Local<v8::String> script = v8::String::New(
1517 "function f() {\n"
1518 " function h() {\n"
1519 " a = 0; // line 2\n"
1520 " }\n"
1521 " b = 1; // line 4\n"
1522 " return h();\n"
1523 "}\n"
1524 "\n"
1525 "function g() {\n"
1526 " function h() {\n"
1527 " a = 0;\n"
1528 " }\n"
1529 " b = 2; // line 12\n"
1530 " h();\n"
1531 " b = 3; // line 14\n"
1532 " f(); // line 15\n"
1533 "}");
1534
1535 // Compile the script and get the two functions.
1536 v8::ScriptOrigin origin =
1537 v8::ScriptOrigin(v8::String::New("test"));
1538 v8::Script::Compile(script, &origin)->Run();
1539 v8::Local<v8::Function> f =
1540 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1541 v8::Local<v8::Function> g =
1542 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1543
1544 // Call f and g without break points.
1545 break_point_hit_count = 0;
1546 f->Call(env->Global(), 0, NULL);
1547 CHECK_EQ(0, break_point_hit_count);
1548 g->Call(env->Global(), 0, NULL);
1549 CHECK_EQ(0, break_point_hit_count);
1550
1551 // Call f and g with break point on line 12.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001552 int sbp1 = SetScriptBreakPointByNameFromJS("test", 12, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001553 break_point_hit_count = 0;
1554 f->Call(env->Global(), 0, NULL);
1555 CHECK_EQ(0, break_point_hit_count);
1556 g->Call(env->Global(), 0, NULL);
1557 CHECK_EQ(1, break_point_hit_count);
1558
1559 // Remove the break point again.
1560 break_point_hit_count = 0;
1561 ClearBreakPointFromJS(sbp1);
1562 f->Call(env->Global(), 0, NULL);
1563 CHECK_EQ(0, break_point_hit_count);
1564 g->Call(env->Global(), 0, NULL);
1565 CHECK_EQ(0, break_point_hit_count);
1566
1567 // Call f and g with break point on line 2.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001568 int sbp2 = SetScriptBreakPointByNameFromJS("test", 2, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001569 break_point_hit_count = 0;
1570 f->Call(env->Global(), 0, NULL);
1571 CHECK_EQ(1, break_point_hit_count);
1572 g->Call(env->Global(), 0, NULL);
1573 CHECK_EQ(2, break_point_hit_count);
1574
1575 // Call f and g with break point on line 2, 4, 12, 14 and 15.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001576 int sbp3 = SetScriptBreakPointByNameFromJS("test", 4, 0);
1577 int sbp4 = SetScriptBreakPointByNameFromJS("test", 12, 0);
1578 int sbp5 = SetScriptBreakPointByNameFromJS("test", 14, 0);
1579 int sbp6 = SetScriptBreakPointByNameFromJS("test", 15, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001580 break_point_hit_count = 0;
1581 f->Call(env->Global(), 0, NULL);
1582 CHECK_EQ(2, break_point_hit_count);
1583 g->Call(env->Global(), 0, NULL);
1584 CHECK_EQ(7, break_point_hit_count);
1585
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001586 // Remove all the break points again.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001587 break_point_hit_count = 0;
1588 ClearBreakPointFromJS(sbp2);
1589 ClearBreakPointFromJS(sbp3);
1590 ClearBreakPointFromJS(sbp4);
1591 ClearBreakPointFromJS(sbp5);
1592 ClearBreakPointFromJS(sbp6);
1593 f->Call(env->Global(), 0, NULL);
1594 CHECK_EQ(0, break_point_hit_count);
1595 g->Call(env->Global(), 0, NULL);
1596 CHECK_EQ(0, break_point_hit_count);
1597
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001598 v8::Debug::SetDebugEventListener(NULL);
1599 CheckDebuggerUnloaded();
1600
1601 // Make sure that the break point numbers are consecutive.
1602 CHECK_EQ(1, sbp1);
1603 CHECK_EQ(2, sbp2);
1604 CHECK_EQ(3, sbp3);
1605 CHECK_EQ(4, sbp4);
1606 CHECK_EQ(5, sbp5);
1607 CHECK_EQ(6, sbp6);
1608}
1609
1610
1611TEST(ScriptBreakPointByIdThroughJavaScript) {
1612 break_point_hit_count = 0;
1613 v8::HandleScope scope;
1614 DebugLocalContext env;
1615 env.ExposeDebug();
1616
1617 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
1618 v8::Undefined());
1619
1620 v8::Local<v8::String> source = v8::String::New(
1621 "function f() {\n"
1622 " function h() {\n"
1623 " a = 0; // line 2\n"
1624 " }\n"
1625 " b = 1; // line 4\n"
1626 " return h();\n"
1627 "}\n"
1628 "\n"
1629 "function g() {\n"
1630 " function h() {\n"
1631 " a = 0;\n"
1632 " }\n"
1633 " b = 2; // line 12\n"
1634 " h();\n"
1635 " b = 3; // line 14\n"
1636 " f(); // line 15\n"
1637 "}");
1638
1639 // Compile the script and get the two functions.
1640 v8::ScriptOrigin origin =
1641 v8::ScriptOrigin(v8::String::New("test"));
1642 v8::Local<v8::Script> script = v8::Script::Compile(source, &origin);
1643 script->Run();
1644 v8::Local<v8::Function> f =
1645 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1646 v8::Local<v8::Function> g =
1647 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1648
1649 // Get the script id knowing that internally it is a 32 integer.
1650 uint32_t script_id = script->Id()->Uint32Value();
1651
1652 // Call f and g without break points.
1653 break_point_hit_count = 0;
1654 f->Call(env->Global(), 0, NULL);
1655 CHECK_EQ(0, break_point_hit_count);
1656 g->Call(env->Global(), 0, NULL);
1657 CHECK_EQ(0, break_point_hit_count);
1658
1659 // Call f and g with break point on line 12.
1660 int sbp1 = SetScriptBreakPointByIdFromJS(script_id, 12, 0);
1661 break_point_hit_count = 0;
1662 f->Call(env->Global(), 0, NULL);
1663 CHECK_EQ(0, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001664 g->Call(env->Global(), 0, NULL);
1665 CHECK_EQ(1, break_point_hit_count);
1666
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001667 // Remove the break point again.
1668 break_point_hit_count = 0;
1669 ClearBreakPointFromJS(sbp1);
1670 f->Call(env->Global(), 0, NULL);
1671 CHECK_EQ(0, break_point_hit_count);
1672 g->Call(env->Global(), 0, NULL);
1673 CHECK_EQ(0, break_point_hit_count);
1674
1675 // Call f and g with break point on line 2.
1676 int sbp2 = SetScriptBreakPointByIdFromJS(script_id, 2, 0);
1677 break_point_hit_count = 0;
1678 f->Call(env->Global(), 0, NULL);
1679 CHECK_EQ(1, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001680 g->Call(env->Global(), 0, NULL);
1681 CHECK_EQ(2, break_point_hit_count);
1682
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001683 // Call f and g with break point on line 2, 4, 12, 14 and 15.
1684 int sbp3 = SetScriptBreakPointByIdFromJS(script_id, 4, 0);
1685 int sbp4 = SetScriptBreakPointByIdFromJS(script_id, 12, 0);
1686 int sbp5 = SetScriptBreakPointByIdFromJS(script_id, 14, 0);
1687 int sbp6 = SetScriptBreakPointByIdFromJS(script_id, 15, 0);
1688 break_point_hit_count = 0;
1689 f->Call(env->Global(), 0, NULL);
1690 CHECK_EQ(2, break_point_hit_count);
1691 g->Call(env->Global(), 0, NULL);
1692 CHECK_EQ(7, break_point_hit_count);
1693
1694 // Remove all the break points again.
1695 break_point_hit_count = 0;
1696 ClearBreakPointFromJS(sbp2);
1697 ClearBreakPointFromJS(sbp3);
1698 ClearBreakPointFromJS(sbp4);
1699 ClearBreakPointFromJS(sbp5);
1700 ClearBreakPointFromJS(sbp6);
1701 f->Call(env->Global(), 0, NULL);
1702 CHECK_EQ(0, break_point_hit_count);
1703 g->Call(env->Global(), 0, NULL);
1704 CHECK_EQ(0, break_point_hit_count);
1705
iposva@chromium.org245aa852009-02-10 00:49:54 +00001706 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001707 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001708
1709 // Make sure that the break point numbers are consecutive.
1710 CHECK_EQ(1, sbp1);
1711 CHECK_EQ(2, sbp2);
1712 CHECK_EQ(3, sbp3);
1713 CHECK_EQ(4, sbp4);
1714 CHECK_EQ(5, sbp5);
1715 CHECK_EQ(6, sbp6);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001716}
1717
1718
1719// Test conditional script break points.
1720TEST(EnableDisableScriptBreakPoint) {
1721 break_point_hit_count = 0;
1722 v8::HandleScope scope;
1723 DebugLocalContext env;
1724 env.ExposeDebug();
1725
iposva@chromium.org245aa852009-02-10 00:49:54 +00001726 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001727 v8::Undefined());
1728
1729 v8::Local<v8::String> script = v8::String::New(
1730 "function f() {\n"
1731 " a = 0; // line 1\n"
1732 "};");
1733
1734 // Compile the script and get function f.
1735 v8::ScriptOrigin origin =
1736 v8::ScriptOrigin(v8::String::New("test"));
1737 v8::Script::Compile(script, &origin)->Run();
1738 v8::Local<v8::Function> f =
1739 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1740
1741 // Set script break point on line 1 (in function f).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001742 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001743
1744 // Call f while enabeling and disabling the script break point.
1745 break_point_hit_count = 0;
1746 f->Call(env->Global(), 0, NULL);
1747 CHECK_EQ(1, break_point_hit_count);
1748
1749 DisableScriptBreakPointFromJS(sbp);
1750 f->Call(env->Global(), 0, NULL);
1751 CHECK_EQ(1, break_point_hit_count);
1752
1753 EnableScriptBreakPointFromJS(sbp);
1754 f->Call(env->Global(), 0, NULL);
1755 CHECK_EQ(2, break_point_hit_count);
1756
1757 DisableScriptBreakPointFromJS(sbp);
1758 f->Call(env->Global(), 0, NULL);
1759 CHECK_EQ(2, break_point_hit_count);
1760
1761 // Reload the script and get f again checking that the disabeling survives.
1762 v8::Script::Compile(script, &origin)->Run();
1763 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1764 f->Call(env->Global(), 0, NULL);
1765 CHECK_EQ(2, break_point_hit_count);
1766
1767 EnableScriptBreakPointFromJS(sbp);
1768 f->Call(env->Global(), 0, NULL);
1769 CHECK_EQ(3, break_point_hit_count);
1770
iposva@chromium.org245aa852009-02-10 00:49:54 +00001771 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001772 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001773}
1774
1775
1776// Test conditional script break points.
1777TEST(ConditionalScriptBreakPoint) {
1778 break_point_hit_count = 0;
1779 v8::HandleScope scope;
1780 DebugLocalContext env;
1781 env.ExposeDebug();
1782
iposva@chromium.org245aa852009-02-10 00:49:54 +00001783 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001784 v8::Undefined());
1785
1786 v8::Local<v8::String> script = v8::String::New(
1787 "count = 0;\n"
1788 "function f() {\n"
1789 " g(count++); // line 2\n"
1790 "};\n"
1791 "function g(x) {\n"
1792 " var a=x; // line 5\n"
1793 "};");
1794
1795 // Compile the script and get function f.
1796 v8::ScriptOrigin origin =
1797 v8::ScriptOrigin(v8::String::New("test"));
1798 v8::Script::Compile(script, &origin)->Run();
1799 v8::Local<v8::Function> f =
1800 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1801
1802 // Set script break point on line 5 (in function g).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001803 int sbp1 = SetScriptBreakPointByNameFromJS("test", 5, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001804
1805 // Call f with different conditions on the script break point.
1806 break_point_hit_count = 0;
1807 ChangeScriptBreakPointConditionFromJS(sbp1, "false");
1808 f->Call(env->Global(), 0, NULL);
1809 CHECK_EQ(0, break_point_hit_count);
1810
1811 ChangeScriptBreakPointConditionFromJS(sbp1, "true");
1812 break_point_hit_count = 0;
1813 f->Call(env->Global(), 0, NULL);
1814 CHECK_EQ(1, break_point_hit_count);
1815
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001816 ChangeScriptBreakPointConditionFromJS(sbp1, "x % 2 == 0");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001817 break_point_hit_count = 0;
1818 for (int i = 0; i < 10; i++) {
1819 f->Call(env->Global(), 0, NULL);
1820 }
1821 CHECK_EQ(5, break_point_hit_count);
1822
1823 // Reload the script and get f again checking that the condition survives.
1824 v8::Script::Compile(script, &origin)->Run();
1825 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1826
1827 break_point_hit_count = 0;
1828 for (int i = 0; i < 10; i++) {
1829 f->Call(env->Global(), 0, NULL);
1830 }
1831 CHECK_EQ(5, break_point_hit_count);
1832
iposva@chromium.org245aa852009-02-10 00:49:54 +00001833 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001834 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001835}
1836
1837
1838// Test ignore count on script break points.
1839TEST(ScriptBreakPointIgnoreCount) {
1840 break_point_hit_count = 0;
1841 v8::HandleScope scope;
1842 DebugLocalContext env;
1843 env.ExposeDebug();
1844
iposva@chromium.org245aa852009-02-10 00:49:54 +00001845 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001846 v8::Undefined());
1847
1848 v8::Local<v8::String> script = v8::String::New(
1849 "function f() {\n"
1850 " a = 0; // line 1\n"
1851 "};");
1852
1853 // Compile the script and get function f.
1854 v8::ScriptOrigin origin =
1855 v8::ScriptOrigin(v8::String::New("test"));
1856 v8::Script::Compile(script, &origin)->Run();
1857 v8::Local<v8::Function> f =
1858 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1859
1860 // Set script break point on line 1 (in function f).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001861 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001862
1863 // Call f with different ignores on the script break point.
1864 break_point_hit_count = 0;
1865 ChangeScriptBreakPointIgnoreCountFromJS(sbp, 1);
1866 f->Call(env->Global(), 0, NULL);
1867 CHECK_EQ(0, break_point_hit_count);
1868 f->Call(env->Global(), 0, NULL);
1869 CHECK_EQ(1, break_point_hit_count);
1870
1871 ChangeScriptBreakPointIgnoreCountFromJS(sbp, 5);
1872 break_point_hit_count = 0;
1873 for (int i = 0; i < 10; i++) {
1874 f->Call(env->Global(), 0, NULL);
1875 }
1876 CHECK_EQ(5, break_point_hit_count);
1877
1878 // Reload the script and get f again checking that the ignore survives.
1879 v8::Script::Compile(script, &origin)->Run();
1880 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1881
1882 break_point_hit_count = 0;
1883 for (int i = 0; i < 10; i++) {
1884 f->Call(env->Global(), 0, NULL);
1885 }
1886 CHECK_EQ(5, break_point_hit_count);
1887
iposva@chromium.org245aa852009-02-10 00:49:54 +00001888 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001889 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001890}
1891
1892
1893// Test that script break points survive when a script is reloaded.
1894TEST(ScriptBreakPointReload) {
1895 break_point_hit_count = 0;
1896 v8::HandleScope scope;
1897 DebugLocalContext env;
1898 env.ExposeDebug();
1899
iposva@chromium.org245aa852009-02-10 00:49:54 +00001900 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001901 v8::Undefined());
1902
1903 v8::Local<v8::Function> f;
1904 v8::Local<v8::String> script = v8::String::New(
1905 "function f() {\n"
1906 " function h() {\n"
1907 " a = 0; // line 2\n"
1908 " }\n"
1909 " b = 1; // line 4\n"
1910 " return h();\n"
1911 "}");
1912
1913 v8::ScriptOrigin origin_1 = v8::ScriptOrigin(v8::String::New("1"));
1914 v8::ScriptOrigin origin_2 = v8::ScriptOrigin(v8::String::New("2"));
1915
1916 // Set a script break point before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001917 SetScriptBreakPointByNameFromJS("1", 2, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001918
1919 // Compile the script and get the function.
1920 v8::Script::Compile(script, &origin_1)->Run();
1921 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1922
1923 // Call f and check that the script break point is active.
1924 break_point_hit_count = 0;
1925 f->Call(env->Global(), 0, NULL);
1926 CHECK_EQ(1, break_point_hit_count);
1927
1928 // Compile the script again with a different script data and get the
1929 // function.
1930 v8::Script::Compile(script, &origin_2)->Run();
1931 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1932
1933 // Call f and check that no break points are set.
1934 break_point_hit_count = 0;
1935 f->Call(env->Global(), 0, NULL);
1936 CHECK_EQ(0, break_point_hit_count);
1937
1938 // Compile the script again and get the function.
1939 v8::Script::Compile(script, &origin_1)->Run();
1940 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1941
1942 // Call f and check that the script break point is active.
1943 break_point_hit_count = 0;
1944 f->Call(env->Global(), 0, NULL);
1945 CHECK_EQ(1, break_point_hit_count);
1946
iposva@chromium.org245aa852009-02-10 00:49:54 +00001947 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001948 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001949}
1950
1951
1952// Test when several scripts has the same script data
1953TEST(ScriptBreakPointMultiple) {
1954 break_point_hit_count = 0;
1955 v8::HandleScope scope;
1956 DebugLocalContext env;
1957 env.ExposeDebug();
1958
iposva@chromium.org245aa852009-02-10 00:49:54 +00001959 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001960 v8::Undefined());
1961
1962 v8::Local<v8::Function> f;
1963 v8::Local<v8::String> script_f = v8::String::New(
1964 "function f() {\n"
1965 " a = 0; // line 1\n"
1966 "}");
1967
1968 v8::Local<v8::Function> g;
1969 v8::Local<v8::String> script_g = v8::String::New(
1970 "function g() {\n"
1971 " b = 0; // line 1\n"
1972 "}");
1973
1974 v8::ScriptOrigin origin =
1975 v8::ScriptOrigin(v8::String::New("test"));
1976
1977 // Set a script break point before the scripts are loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001978 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001979
1980 // Compile the scripts with same script data and get the functions.
1981 v8::Script::Compile(script_f, &origin)->Run();
1982 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1983 v8::Script::Compile(script_g, &origin)->Run();
1984 g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1985
1986 // Call f and g and check that the script break point is active.
1987 break_point_hit_count = 0;
1988 f->Call(env->Global(), 0, NULL);
1989 CHECK_EQ(1, break_point_hit_count);
1990 g->Call(env->Global(), 0, NULL);
1991 CHECK_EQ(2, break_point_hit_count);
1992
1993 // Clear the script break point.
1994 ClearBreakPointFromJS(sbp);
1995
1996 // Call f and g and check that the script break point is no longer active.
1997 break_point_hit_count = 0;
1998 f->Call(env->Global(), 0, NULL);
1999 CHECK_EQ(0, break_point_hit_count);
2000 g->Call(env->Global(), 0, NULL);
2001 CHECK_EQ(0, break_point_hit_count);
2002
2003 // Set script break point with the scripts loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002004 sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002005
2006 // Call f and g and check that the script break point is active.
2007 break_point_hit_count = 0;
2008 f->Call(env->Global(), 0, NULL);
2009 CHECK_EQ(1, break_point_hit_count);
2010 g->Call(env->Global(), 0, NULL);
2011 CHECK_EQ(2, break_point_hit_count);
2012
iposva@chromium.org245aa852009-02-10 00:49:54 +00002013 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002014 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002015}
2016
2017
2018// Test the script origin which has both name and line offset.
2019TEST(ScriptBreakPointLineOffset) {
2020 break_point_hit_count = 0;
2021 v8::HandleScope scope;
2022 DebugLocalContext env;
2023 env.ExposeDebug();
2024
iposva@chromium.org245aa852009-02-10 00:49:54 +00002025 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002026 v8::Undefined());
2027
2028 v8::Local<v8::Function> f;
2029 v8::Local<v8::String> script = v8::String::New(
2030 "function f() {\n"
2031 " a = 0; // line 8 as this script has line offset 7\n"
2032 " b = 0; // line 9 as this script has line offset 7\n"
2033 "}");
2034
2035 // Create script origin both name and line offset.
2036 v8::ScriptOrigin origin(v8::String::New("test.html"),
2037 v8::Integer::New(7));
2038
2039 // Set two script break points before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002040 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 8, 0);
2041 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 9, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002042
2043 // Compile the script and get the function.
2044 v8::Script::Compile(script, &origin)->Run();
2045 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
2046
2047 // Call f and check that the script break point is active.
2048 break_point_hit_count = 0;
2049 f->Call(env->Global(), 0, NULL);
2050 CHECK_EQ(2, break_point_hit_count);
2051
2052 // Clear the script break points.
2053 ClearBreakPointFromJS(sbp1);
2054 ClearBreakPointFromJS(sbp2);
2055
2056 // Call f and check that no script break points are active.
2057 break_point_hit_count = 0;
2058 f->Call(env->Global(), 0, NULL);
2059 CHECK_EQ(0, break_point_hit_count);
2060
2061 // Set a script break point with the script loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002062 sbp1 = SetScriptBreakPointByNameFromJS("test.html", 9, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002063
2064 // Call f and check that the script break point is active.
2065 break_point_hit_count = 0;
2066 f->Call(env->Global(), 0, NULL);
2067 CHECK_EQ(1, break_point_hit_count);
2068
iposva@chromium.org245aa852009-02-10 00:49:54 +00002069 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002070 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002071}
2072
2073
2074// Test script break points set on lines.
2075TEST(ScriptBreakPointLine) {
2076 v8::HandleScope scope;
2077 DebugLocalContext env;
2078 env.ExposeDebug();
2079
2080 // Create a function for checking the function when hitting a break point.
2081 frame_function_name = CompileFunction(&env,
2082 frame_function_name_source,
2083 "frame_function_name");
2084
iposva@chromium.org245aa852009-02-10 00:49:54 +00002085 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002086 v8::Undefined());
2087
2088 v8::Local<v8::Function> f;
2089 v8::Local<v8::Function> g;
2090 v8::Local<v8::String> script = v8::String::New(
2091 "a = 0 // line 0\n"
2092 "function f() {\n"
2093 " a = 1; // line 2\n"
2094 "}\n"
2095 " a = 2; // line 4\n"
2096 " /* xx */ function g() { // line 5\n"
2097 " function h() { // line 6\n"
2098 " a = 3; // line 7\n"
2099 " }\n"
2100 " h(); // line 9\n"
2101 " a = 4; // line 10\n"
2102 " }\n"
2103 " a=5; // line 12");
2104
2105 // Set a couple script break point before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002106 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 0, -1);
2107 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 1, -1);
2108 int sbp3 = SetScriptBreakPointByNameFromJS("test.html", 5, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002109
2110 // Compile the script and get the function.
2111 break_point_hit_count = 0;
2112 v8::ScriptOrigin origin(v8::String::New("test.html"), v8::Integer::New(0));
2113 v8::Script::Compile(script, &origin)->Run();
2114 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
2115 g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
2116
2117 // Chesk that a break point was hit when the script was run.
2118 CHECK_EQ(1, break_point_hit_count);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002119 CHECK_EQ(0, StrLength(last_function_hit));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002120
2121 // Call f and check that the script break point.
2122 f->Call(env->Global(), 0, NULL);
2123 CHECK_EQ(2, break_point_hit_count);
2124 CHECK_EQ("f", last_function_hit);
2125
2126 // Call g and check that the script break point.
2127 g->Call(env->Global(), 0, NULL);
2128 CHECK_EQ(3, break_point_hit_count);
2129 CHECK_EQ("g", last_function_hit);
2130
2131 // Clear the script break point on g and set one on h.
2132 ClearBreakPointFromJS(sbp3);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002133 int sbp4 = SetScriptBreakPointByNameFromJS("test.html", 6, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002134
2135 // Call g and check that the script break point in h is hit.
2136 g->Call(env->Global(), 0, NULL);
2137 CHECK_EQ(4, break_point_hit_count);
2138 CHECK_EQ("h", last_function_hit);
2139
2140 // Clear break points in f and h. Set a new one in the script between
2141 // functions f and g and test that there is no break points in f and g any
2142 // more.
2143 ClearBreakPointFromJS(sbp2);
2144 ClearBreakPointFromJS(sbp4);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002145 int sbp5 = SetScriptBreakPointByNameFromJS("test.html", 4, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002146 break_point_hit_count = 0;
2147 f->Call(env->Global(), 0, NULL);
2148 g->Call(env->Global(), 0, NULL);
2149 CHECK_EQ(0, break_point_hit_count);
2150
2151 // Reload the script which should hit two break points.
2152 break_point_hit_count = 0;
2153 v8::Script::Compile(script, &origin)->Run();
2154 CHECK_EQ(2, break_point_hit_count);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002155 CHECK_EQ(0, StrLength(last_function_hit));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002156
2157 // Set a break point in the code after the last function decleration.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002158 int sbp6 = SetScriptBreakPointByNameFromJS("test.html", 12, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002159
2160 // Reload the script which should hit three break points.
2161 break_point_hit_count = 0;
2162 v8::Script::Compile(script, &origin)->Run();
2163 CHECK_EQ(3, break_point_hit_count);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002164 CHECK_EQ(0, StrLength(last_function_hit));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002165
2166 // Clear the last break points, and reload the script which should not hit any
2167 // break points.
2168 ClearBreakPointFromJS(sbp1);
2169 ClearBreakPointFromJS(sbp5);
2170 ClearBreakPointFromJS(sbp6);
2171 break_point_hit_count = 0;
2172 v8::Script::Compile(script, &origin)->Run();
2173 CHECK_EQ(0, break_point_hit_count);
2174
iposva@chromium.org245aa852009-02-10 00:49:54 +00002175 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002176 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002177}
2178
2179
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002180// Test top level script break points set on lines.
2181TEST(ScriptBreakPointLineTopLevel) {
2182 v8::HandleScope scope;
2183 DebugLocalContext env;
2184 env.ExposeDebug();
2185
2186 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
2187 v8::Undefined());
2188
2189 v8::Local<v8::String> script = v8::String::New(
2190 "function f() {\n"
2191 " a = 1; // line 1\n"
2192 "}\n"
2193 "a = 2; // line 3\n");
2194 v8::Local<v8::Function> f;
2195 {
2196 v8::HandleScope scope;
2197 v8::Script::Compile(script, v8::String::New("test.html"))->Run();
2198 }
2199 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
2200
2201 Heap::CollectAllGarbage(false);
2202
2203 SetScriptBreakPointByNameFromJS("test.html", 3, -1);
2204
2205 // Call f and check that there was no break points.
2206 break_point_hit_count = 0;
2207 f->Call(env->Global(), 0, NULL);
2208 CHECK_EQ(0, break_point_hit_count);
2209
2210 // Recompile and run script and check that break point was hit.
2211 break_point_hit_count = 0;
2212 v8::Script::Compile(script, v8::String::New("test.html"))->Run();
2213 CHECK_EQ(1, break_point_hit_count);
2214
2215 // Call f and check that there are still no break points.
2216 break_point_hit_count = 0;
2217 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
2218 CHECK_EQ(0, break_point_hit_count);
2219
2220 v8::Debug::SetDebugEventListener(NULL);
2221 CheckDebuggerUnloaded();
2222}
2223
2224
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002225// Test that it is possible to add and remove break points in a top level
2226// function which has no references but has not been collected yet.
2227TEST(ScriptBreakPointTopLevelCrash) {
2228 v8::HandleScope scope;
2229 DebugLocalContext env;
2230 env.ExposeDebug();
2231
2232 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
2233 v8::Undefined());
2234
2235 v8::Local<v8::String> script_source = v8::String::New(
2236 "function f() {\n"
2237 " return 0;\n"
2238 "}\n"
2239 "f()");
2240
2241 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 3, -1);
2242 {
2243 v8::HandleScope scope;
2244 break_point_hit_count = 0;
2245 v8::Script::Compile(script_source, v8::String::New("test.html"))->Run();
2246 CHECK_EQ(1, break_point_hit_count);
2247 }
2248
2249 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 3, -1);
2250 ClearBreakPointFromJS(sbp1);
2251 ClearBreakPointFromJS(sbp2);
2252
2253 v8::Debug::SetDebugEventListener(NULL);
2254 CheckDebuggerUnloaded();
2255}
2256
2257
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002258// Test that it is possible to remove the last break point for a function
2259// inside the break handling of that break point.
2260TEST(RemoveBreakPointInBreak) {
2261 v8::HandleScope scope;
2262 DebugLocalContext env;
2263
2264 v8::Local<v8::Function> foo =
2265 CompileFunction(&env, "function foo(){a=1;}", "foo");
2266 debug_event_remove_break_point = SetBreakPoint(foo, 0);
2267
2268 // Register the debug event listener pasing the function
iposva@chromium.org245aa852009-02-10 00:49:54 +00002269 v8::Debug::SetDebugEventListener(DebugEventRemoveBreakPoint, foo);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002270
2271 break_point_hit_count = 0;
2272 foo->Call(env->Global(), 0, NULL);
2273 CHECK_EQ(1, break_point_hit_count);
2274
2275 break_point_hit_count = 0;
2276 foo->Call(env->Global(), 0, NULL);
2277 CHECK_EQ(0, break_point_hit_count);
2278
iposva@chromium.org245aa852009-02-10 00:49:54 +00002279 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002280 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002281}
2282
2283
2284// Test that the debugger statement causes a break.
2285TEST(DebuggerStatement) {
2286 break_point_hit_count = 0;
2287 v8::HandleScope scope;
2288 DebugLocalContext env;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002289 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002290 v8::Undefined());
2291 v8::Script::Compile(v8::String::New("function bar(){debugger}"))->Run();
2292 v8::Script::Compile(v8::String::New(
2293 "function foo(){debugger;debugger;}"))->Run();
2294 v8::Local<v8::Function> foo =
2295 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
2296 v8::Local<v8::Function> bar =
2297 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("bar")));
2298
2299 // Run function with debugger statement
2300 bar->Call(env->Global(), 0, NULL);
2301 CHECK_EQ(1, break_point_hit_count);
2302
2303 // Run function with two debugger statement
2304 foo->Call(env->Global(), 0, NULL);
2305 CHECK_EQ(3, break_point_hit_count);
2306
iposva@chromium.org245aa852009-02-10 00:49:54 +00002307 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002308 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002309}
2310
2311
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002312// Test setting a breakpoint on the debugger statement.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002313TEST(DebuggerStatementBreakpoint) {
2314 break_point_hit_count = 0;
2315 v8::HandleScope scope;
2316 DebugLocalContext env;
2317 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
2318 v8::Undefined());
2319 v8::Script::Compile(v8::String::New("function foo(){debugger;}"))->Run();
2320 v8::Local<v8::Function> foo =
2321 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
2322
2323 // The debugger statement triggers breakpint hit
2324 foo->Call(env->Global(), 0, NULL);
2325 CHECK_EQ(1, break_point_hit_count);
2326
2327 int bp = SetBreakPoint(foo, 0);
2328
2329 // Set breakpoint does not duplicate hits
2330 foo->Call(env->Global(), 0, NULL);
2331 CHECK_EQ(2, break_point_hit_count);
2332
2333 ClearBreakPoint(bp);
2334 v8::Debug::SetDebugEventListener(NULL);
2335 CheckDebuggerUnloaded();
2336}
2337
2338
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002339// Thest that the evaluation of expressions when a break point is hit generates
2340// the correct results.
2341TEST(DebugEvaluate) {
2342 v8::HandleScope scope;
2343 DebugLocalContext env;
2344 env.ExposeDebug();
2345
2346 // Create a function for checking the evaluation when hitting a break point.
2347 evaluate_check_function = CompileFunction(&env,
2348 evaluate_check_source,
2349 "evaluate_check");
2350 // Register the debug event listener
iposva@chromium.org245aa852009-02-10 00:49:54 +00002351 v8::Debug::SetDebugEventListener(DebugEventEvaluate);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002352
2353 // Different expected vaules of x and a when in a break point (u = undefined,
2354 // d = Hello, world!).
2355 struct EvaluateCheck checks_uu[] = {
2356 {"x", v8::Undefined()},
2357 {"a", v8::Undefined()},
2358 {NULL, v8::Handle<v8::Value>()}
2359 };
2360 struct EvaluateCheck checks_hu[] = {
2361 {"x", v8::String::New("Hello, world!")},
2362 {"a", v8::Undefined()},
2363 {NULL, v8::Handle<v8::Value>()}
2364 };
2365 struct EvaluateCheck checks_hh[] = {
2366 {"x", v8::String::New("Hello, world!")},
2367 {"a", v8::String::New("Hello, world!")},
2368 {NULL, v8::Handle<v8::Value>()}
2369 };
2370
2371 // Simple test function. The "y=0" is in the function foo to provide a break
2372 // location. For "y=0" the "y" is at position 15 in the barbar function
2373 // therefore setting breakpoint at position 15 will break at "y=0" and
2374 // setting it higher will break after.
2375 v8::Local<v8::Function> foo = CompileFunction(&env,
2376 "function foo(x) {"
2377 " var a;"
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002378 " y=0;" // To ensure break location 1.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002379 " a=x;"
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002380 " y=0;" // To ensure break location 2.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002381 "}",
2382 "foo");
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002383 const int foo_break_position_1 = 15;
2384 const int foo_break_position_2 = 29;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002385
2386 // Arguments with one parameter "Hello, world!"
2387 v8::Handle<v8::Value> argv_foo[1] = { v8::String::New("Hello, world!") };
2388
2389 // Call foo with breakpoint set before a=x and undefined as parameter.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002390 int bp = SetBreakPoint(foo, foo_break_position_1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002391 checks = checks_uu;
2392 foo->Call(env->Global(), 0, NULL);
2393
2394 // Call foo with breakpoint set before a=x and parameter "Hello, world!".
2395 checks = checks_hu;
2396 foo->Call(env->Global(), 1, argv_foo);
2397
2398 // Call foo with breakpoint set after a=x and parameter "Hello, world!".
2399 ClearBreakPoint(bp);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002400 SetBreakPoint(foo, foo_break_position_2);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002401 checks = checks_hh;
2402 foo->Call(env->Global(), 1, argv_foo);
2403
2404 // Test function with an inner function. The "y=0" is in function barbar
2405 // to provide a break location. For "y=0" the "y" is at position 8 in the
2406 // barbar function therefore setting breakpoint at position 8 will break at
2407 // "y=0" and setting it higher will break after.
2408 v8::Local<v8::Function> bar = CompileFunction(&env,
2409 "y = 0;"
2410 "x = 'Goodbye, world!';"
2411 "function bar(x, b) {"
2412 " var a;"
2413 " function barbar() {"
2414 " y=0; /* To ensure break location.*/"
2415 " a=x;"
2416 " };"
2417 " debug.Debug.clearAllBreakPoints();"
2418 " barbar();"
2419 " y=0;a=x;"
2420 "}",
2421 "bar");
2422 const int barbar_break_position = 8;
2423
2424 // Call bar setting breakpoint before a=x in barbar and undefined as
2425 // parameter.
2426 checks = checks_uu;
2427 v8::Handle<v8::Value> argv_bar_1[2] = {
2428 v8::Undefined(),
2429 v8::Number::New(barbar_break_position)
2430 };
2431 bar->Call(env->Global(), 2, argv_bar_1);
2432
2433 // Call bar setting breakpoint before a=x in barbar and parameter
2434 // "Hello, world!".
2435 checks = checks_hu;
2436 v8::Handle<v8::Value> argv_bar_2[2] = {
2437 v8::String::New("Hello, world!"),
2438 v8::Number::New(barbar_break_position)
2439 };
2440 bar->Call(env->Global(), 2, argv_bar_2);
2441
2442 // Call bar setting breakpoint after a=x in barbar and parameter
2443 // "Hello, world!".
2444 checks = checks_hh;
2445 v8::Handle<v8::Value> argv_bar_3[2] = {
2446 v8::String::New("Hello, world!"),
2447 v8::Number::New(barbar_break_position + 1)
2448 };
2449 bar->Call(env->Global(), 2, argv_bar_3);
2450
iposva@chromium.org245aa852009-02-10 00:49:54 +00002451 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002452 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002453}
2454
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002455// Copies a C string to a 16-bit string. Does not check for buffer overflow.
2456// Does not use the V8 engine to convert strings, so it can be used
2457// in any thread. Returns the length of the string.
2458int AsciiToUtf16(const char* input_buffer, uint16_t* output_buffer) {
2459 int i;
2460 for (i = 0; input_buffer[i] != '\0'; ++i) {
2461 // ASCII does not use chars > 127, but be careful anyway.
2462 output_buffer[i] = static_cast<unsigned char>(input_buffer[i]);
2463 }
2464 output_buffer[i] = 0;
2465 return i;
2466}
2467
2468// Copies a 16-bit string to a C string by dropping the high byte of
2469// each character. Does not check for buffer overflow.
2470// Can be used in any thread. Requires string length as an input.
2471int Utf16ToAscii(const uint16_t* input_buffer, int length,
2472 char* output_buffer, int output_len = -1) {
2473 if (output_len >= 0) {
2474 if (length > output_len - 1) {
2475 length = output_len - 1;
2476 }
2477 }
2478
2479 for (int i = 0; i < length; ++i) {
2480 output_buffer[i] = static_cast<char>(input_buffer[i]);
2481 }
2482 output_buffer[length] = '\0';
2483 return length;
2484}
2485
2486
2487// We match parts of the message to get evaluate result int value.
2488bool GetEvaluateStringResult(char *message, char* buffer, int buffer_size) {
2489 if (strstr(message, "\"command\":\"evaluate\"") == NULL) {
2490 return false;
2491 }
2492 const char* prefix = "\"text\":\"";
2493 char* pos1 = strstr(message, prefix);
2494 if (pos1 == NULL) {
2495 return false;
2496 }
2497 pos1 += strlen(prefix);
2498 char* pos2 = strchr(pos1, '"');
2499 if (pos2 == NULL) {
2500 return false;
2501 }
2502 Vector<char> buf(buffer, buffer_size);
2503 int len = static_cast<int>(pos2 - pos1);
2504 if (len > buffer_size - 1) {
2505 len = buffer_size - 1;
2506 }
2507 OS::StrNCpy(buf, pos1, len);
2508 buffer[buffer_size - 1] = '\0';
2509 return true;
2510}
2511
2512
2513struct EvaluateResult {
2514 static const int kBufferSize = 20;
2515 char buffer[kBufferSize];
2516};
2517
2518struct DebugProcessDebugMessagesData {
2519 static const int kArraySize = 5;
2520 int counter;
2521 EvaluateResult results[kArraySize];
2522
2523 void reset() {
2524 counter = 0;
2525 }
2526 EvaluateResult* current() {
2527 return &results[counter % kArraySize];
2528 }
2529 void next() {
2530 counter++;
2531 }
2532};
2533
2534DebugProcessDebugMessagesData process_debug_messages_data;
2535
2536static void DebugProcessDebugMessagesHandler(
2537 const uint16_t* message,
2538 int length,
2539 v8::Debug::ClientData* client_data) {
2540
2541 const int kBufferSize = 100000;
2542 char print_buffer[kBufferSize];
2543 Utf16ToAscii(message, length, print_buffer, kBufferSize);
2544
2545 EvaluateResult* array_item = process_debug_messages_data.current();
2546
2547 bool res = GetEvaluateStringResult(print_buffer,
2548 array_item->buffer,
2549 EvaluateResult::kBufferSize);
2550 if (res) {
2551 process_debug_messages_data.next();
2552 }
2553}
2554
2555// Test that the evaluation of expressions works even from ProcessDebugMessages
2556// i.e. with empty stack.
2557TEST(DebugEvaluateWithoutStack) {
2558 v8::Debug::SetMessageHandler(DebugProcessDebugMessagesHandler);
2559
2560 v8::HandleScope scope;
2561 DebugLocalContext env;
2562
2563 const char* source =
2564 "var v1 = 'Pinguin';\n function getAnimal() { return 'Capy' + 'bara'; }";
2565
2566 v8::Script::Compile(v8::String::New(source))->Run();
2567
2568 v8::Debug::ProcessDebugMessages();
2569
2570 const int kBufferSize = 1000;
2571 uint16_t buffer[kBufferSize];
2572
2573 const char* command_111 = "{\"seq\":111,"
2574 "\"type\":\"request\","
2575 "\"command\":\"evaluate\","
2576 "\"arguments\":{"
2577 " \"global\":true,"
2578 " \"expression\":\"v1\",\"disable_break\":true"
2579 "}}";
2580
2581 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_111, buffer));
2582
2583 const char* command_112 = "{\"seq\":112,"
2584 "\"type\":\"request\","
2585 "\"command\":\"evaluate\","
2586 "\"arguments\":{"
2587 " \"global\":true,"
2588 " \"expression\":\"getAnimal()\",\"disable_break\":true"
2589 "}}";
2590
2591 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_112, buffer));
2592
2593 const char* command_113 = "{\"seq\":113,"
2594 "\"type\":\"request\","
2595 "\"command\":\"evaluate\","
2596 "\"arguments\":{"
2597 " \"global\":true,"
2598 " \"expression\":\"239 + 566\",\"disable_break\":true"
2599 "}}";
2600
2601 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_113, buffer));
2602
2603 v8::Debug::ProcessDebugMessages();
2604
2605 CHECK_EQ(3, process_debug_messages_data.counter);
2606
2607 CHECK_EQ(strcmp("Pinguin", process_debug_messages_data.results[0].buffer), 0);
2608 CHECK_EQ(strcmp("Capybara", process_debug_messages_data.results[1].buffer),
2609 0);
2610 CHECK_EQ(strcmp("805", process_debug_messages_data.results[2].buffer), 0);
2611
2612 v8::Debug::SetMessageHandler(NULL);
2613 v8::Debug::SetDebugEventListener(NULL);
2614 CheckDebuggerUnloaded();
2615}
2616
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002617
2618// Simple test of the stepping mechanism using only store ICs.
2619TEST(DebugStepLinear) {
2620 v8::HandleScope scope;
2621 DebugLocalContext env;
2622
2623 // Create a function for testing stepping.
2624 v8::Local<v8::Function> foo = CompileFunction(&env,
2625 "function foo(){a=1;b=1;c=1;}",
2626 "foo");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002627
2628 // Run foo to allow it to get optimized.
2629 CompileRun("a=0; b=0; c=0; foo();");
2630
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002631 SetBreakPoint(foo, 3);
2632
2633 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002634 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002635
2636 step_action = StepIn;
2637 break_point_hit_count = 0;
2638 foo->Call(env->Global(), 0, NULL);
2639
2640 // With stepping all break locations are hit.
2641 CHECK_EQ(4, break_point_hit_count);
2642
iposva@chromium.org245aa852009-02-10 00:49:54 +00002643 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002644 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002645
2646 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002647 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002648
ager@chromium.org381abbb2009-02-25 13:23:22 +00002649 SetBreakPoint(foo, 3);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002650 break_point_hit_count = 0;
2651 foo->Call(env->Global(), 0, NULL);
2652
2653 // Without stepping only active break points are hit.
2654 CHECK_EQ(1, break_point_hit_count);
2655
iposva@chromium.org245aa852009-02-10 00:49:54 +00002656 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002657 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002658}
2659
2660
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002661// Test of the stepping mechanism for keyed load in a loop.
2662TEST(DebugStepKeyedLoadLoop) {
2663 v8::HandleScope scope;
2664 DebugLocalContext env;
2665
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002666 // Register a debug event listener which steps and counts.
2667 v8::Debug::SetDebugEventListener(DebugEventStep);
2668
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002669 // Create a function for testing stepping of keyed load. The statement 'y=1'
2670 // is there to have more than one breakable statement in the loop, TODO(315).
2671 v8::Local<v8::Function> foo = CompileFunction(
2672 &env,
2673 "function foo(a) {\n"
2674 " var x;\n"
2675 " var len = a.length;\n"
2676 " for (var i = 0; i < len; i++) {\n"
2677 " y = 1;\n"
2678 " x = a[i];\n"
2679 " }\n"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002680 "}\n"
2681 "y=0\n",
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002682 "foo");
2683
2684 // Create array [0,1,2,3,4,5,6,7,8,9]
2685 v8::Local<v8::Array> a = v8::Array::New(10);
2686 for (int i = 0; i < 10; i++) {
2687 a->Set(v8::Number::New(i), v8::Number::New(i));
2688 }
2689
2690 // Call function without any break points to ensure inlining is in place.
2691 const int kArgc = 1;
2692 v8::Handle<v8::Value> args[kArgc] = { a };
2693 foo->Call(env->Global(), kArgc, args);
2694
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002695 // Setup break point and step through the function.
2696 SetBreakPoint(foo, 3);
2697 step_action = StepNext;
2698 break_point_hit_count = 0;
2699 foo->Call(env->Global(), kArgc, args);
2700
2701 // With stepping all break locations are hit.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002702 CHECK_EQ(33, break_point_hit_count);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002703
2704 v8::Debug::SetDebugEventListener(NULL);
2705 CheckDebuggerUnloaded();
2706}
2707
2708
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002709// Test of the stepping mechanism for keyed store in a loop.
2710TEST(DebugStepKeyedStoreLoop) {
2711 v8::HandleScope scope;
2712 DebugLocalContext env;
2713
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002714 // Register a debug event listener which steps and counts.
2715 v8::Debug::SetDebugEventListener(DebugEventStep);
2716
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002717 // Create a function for testing stepping of keyed store. The statement 'y=1'
2718 // is there to have more than one breakable statement in the loop, TODO(315).
2719 v8::Local<v8::Function> foo = CompileFunction(
2720 &env,
2721 "function foo(a) {\n"
2722 " var len = a.length;\n"
2723 " for (var i = 0; i < len; i++) {\n"
2724 " y = 1;\n"
2725 " a[i] = 42;\n"
2726 " }\n"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002727 "}\n"
2728 "y=0\n",
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002729 "foo");
2730
2731 // Create array [0,1,2,3,4,5,6,7,8,9]
2732 v8::Local<v8::Array> a = v8::Array::New(10);
2733 for (int i = 0; i < 10; i++) {
2734 a->Set(v8::Number::New(i), v8::Number::New(i));
2735 }
2736
2737 // Call function without any break points to ensure inlining is in place.
2738 const int kArgc = 1;
2739 v8::Handle<v8::Value> args[kArgc] = { a };
2740 foo->Call(env->Global(), kArgc, args);
2741
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002742 // Setup break point and step through the function.
2743 SetBreakPoint(foo, 3);
2744 step_action = StepNext;
2745 break_point_hit_count = 0;
2746 foo->Call(env->Global(), kArgc, args);
2747
2748 // With stepping all break locations are hit.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002749 CHECK_EQ(32, break_point_hit_count);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002750
2751 v8::Debug::SetDebugEventListener(NULL);
2752 CheckDebuggerUnloaded();
2753}
2754
2755
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002756// Test of the stepping mechanism for named load in a loop.
2757TEST(DebugStepNamedLoadLoop) {
2758 v8::HandleScope scope;
2759 DebugLocalContext env;
2760
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002761 // Register a debug event listener which steps and counts.
2762 v8::Debug::SetDebugEventListener(DebugEventStep);
2763
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002764 // Create a function for testing stepping of named load.
2765 v8::Local<v8::Function> foo = CompileFunction(
2766 &env,
2767 "function foo() {\n"
2768 " var a = [];\n"
2769 " var s = \"\";\n"
2770 " for (var i = 0; i < 10; i++) {\n"
2771 " var v = new V(i, i + 1);\n"
2772 " v.y;\n"
2773 " a.length;\n" // Special case: array length.
2774 " s.length;\n" // Special case: string length.
2775 " }\n"
2776 "}\n"
2777 "function V(x, y) {\n"
2778 " this.x = x;\n"
2779 " this.y = y;\n"
2780 "}\n",
2781 "foo");
2782
2783 // Call function without any break points to ensure inlining is in place.
2784 foo->Call(env->Global(), 0, NULL);
2785
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002786 // Setup break point and step through the function.
2787 SetBreakPoint(foo, 4);
2788 step_action = StepNext;
2789 break_point_hit_count = 0;
2790 foo->Call(env->Global(), 0, NULL);
2791
2792 // With stepping all break locations are hit.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002793 CHECK_EQ(53, break_point_hit_count);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002794
2795 v8::Debug::SetDebugEventListener(NULL);
2796 CheckDebuggerUnloaded();
2797}
2798
2799
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002800static void DoDebugStepNamedStoreLoop(int expected) {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002801 v8::HandleScope scope;
2802 DebugLocalContext env;
2803
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002804 // Register a debug event listener which steps and counts.
2805 v8::Debug::SetDebugEventListener(DebugEventStep);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002806
2807 // Create a function for testing stepping of named store.
2808 v8::Local<v8::Function> foo = CompileFunction(
2809 &env,
2810 "function foo() {\n"
2811 " var a = {a:1};\n"
2812 " for (var i = 0; i < 10; i++) {\n"
2813 " a.a = 2\n"
2814 " }\n"
2815 "}\n",
2816 "foo");
2817
2818 // Call function without any break points to ensure inlining is in place.
2819 foo->Call(env->Global(), 0, NULL);
2820
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002821 // Setup break point and step through the function.
2822 SetBreakPoint(foo, 3);
2823 step_action = StepNext;
2824 break_point_hit_count = 0;
2825 foo->Call(env->Global(), 0, NULL);
2826
2827 // With stepping all expected break locations are hit.
2828 CHECK_EQ(expected, break_point_hit_count);
2829
2830 v8::Debug::SetDebugEventListener(NULL);
2831 CheckDebuggerUnloaded();
2832}
2833
2834
2835// Test of the stepping mechanism for named load in a loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002836TEST(DebugStepNamedStoreLoop) {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002837 DoDebugStepNamedStoreLoop(22);
2838}
2839
2840
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002841// Test the stepping mechanism with different ICs.
2842TEST(DebugStepLinearMixedICs) {
2843 v8::HandleScope scope;
2844 DebugLocalContext env;
2845
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002846 // Register a debug event listener which steps and counts.
2847 v8::Debug::SetDebugEventListener(DebugEventStep);
2848
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002849 // Create a function for testing stepping.
2850 v8::Local<v8::Function> foo = CompileFunction(&env,
2851 "function bar() {};"
2852 "function foo() {"
2853 " var x;"
2854 " var index='name';"
2855 " var y = {};"
2856 " a=1;b=2;x=a;y[index]=3;x=y[index];bar();}", "foo");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002857
2858 // Run functions to allow them to get optimized.
2859 CompileRun("a=0; b=0; bar(); foo();");
2860
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002861 SetBreakPoint(foo, 0);
2862
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002863 step_action = StepIn;
2864 break_point_hit_count = 0;
2865 foo->Call(env->Global(), 0, NULL);
2866
ager@chromium.org4af710e2009-09-15 12:20:11 +00002867 // With stepping all break locations are hit.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002868 CHECK_EQ(11, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002869
iposva@chromium.org245aa852009-02-10 00:49:54 +00002870 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002871 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002872
2873 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002874 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002875
ager@chromium.org381abbb2009-02-25 13:23:22 +00002876 SetBreakPoint(foo, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002877 break_point_hit_count = 0;
2878 foo->Call(env->Global(), 0, NULL);
2879
2880 // Without stepping only active break points are hit.
2881 CHECK_EQ(1, break_point_hit_count);
2882
iposva@chromium.org245aa852009-02-10 00:49:54 +00002883 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002884 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002885}
2886
2887
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002888TEST(DebugStepDeclarations) {
2889 v8::HandleScope scope;
2890 DebugLocalContext env;
2891
2892 // Register a debug event listener which steps and counts.
2893 v8::Debug::SetDebugEventListener(DebugEventStep);
2894
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002895 // Create a function for testing stepping. Run it to allow it to get
2896 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002897 const char* src = "function foo() { "
2898 " var a;"
2899 " var b = 1;"
2900 " var c = foo;"
2901 " var d = Math.floor;"
2902 " var e = b + d(1.2);"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002903 "}"
2904 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002905 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002906
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002907 SetBreakPoint(foo, 0);
2908
2909 // Stepping through the declarations.
2910 step_action = StepIn;
2911 break_point_hit_count = 0;
2912 foo->Call(env->Global(), 0, NULL);
2913 CHECK_EQ(6, break_point_hit_count);
2914
2915 // Get rid of the debug event listener.
2916 v8::Debug::SetDebugEventListener(NULL);
2917 CheckDebuggerUnloaded();
2918}
2919
2920
2921TEST(DebugStepLocals) {
2922 v8::HandleScope scope;
2923 DebugLocalContext env;
2924
2925 // Register a debug event listener which steps and counts.
2926 v8::Debug::SetDebugEventListener(DebugEventStep);
2927
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002928 // Create a function for testing stepping. Run it to allow it to get
2929 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002930 const char* src = "function foo() { "
2931 " var a,b;"
2932 " a = 1;"
2933 " b = a + 2;"
2934 " b = 1 + 2 + 3;"
2935 " a = Math.floor(b);"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002936 "}"
2937 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002938 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002939
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002940 SetBreakPoint(foo, 0);
2941
2942 // Stepping through the declarations.
2943 step_action = StepIn;
2944 break_point_hit_count = 0;
2945 foo->Call(env->Global(), 0, NULL);
2946 CHECK_EQ(6, break_point_hit_count);
2947
2948 // Get rid of the debug event listener.
2949 v8::Debug::SetDebugEventListener(NULL);
2950 CheckDebuggerUnloaded();
2951}
2952
2953
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002954TEST(DebugStepIf) {
2955 v8::HandleScope scope;
2956 DebugLocalContext env;
2957
2958 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002959 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002960
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002961 // Create a function for testing stepping. Run it to allow it to get
2962 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002963 const int argc = 1;
2964 const char* src = "function foo(x) { "
2965 " a = 1;"
2966 " if (x) {"
2967 " b = 1;"
2968 " } else {"
2969 " c = 1;"
2970 " d = 1;"
2971 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002972 "}"
2973 "a=0; b=0; c=0; d=0; foo()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002974 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
2975 SetBreakPoint(foo, 0);
2976
2977 // Stepping through the true part.
2978 step_action = StepIn;
2979 break_point_hit_count = 0;
2980 v8::Handle<v8::Value> argv_true[argc] = { v8::True() };
2981 foo->Call(env->Global(), argc, argv_true);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002982 CHECK_EQ(4, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002983
2984 // Stepping through the false part.
2985 step_action = StepIn;
2986 break_point_hit_count = 0;
2987 v8::Handle<v8::Value> argv_false[argc] = { v8::False() };
2988 foo->Call(env->Global(), argc, argv_false);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002989 CHECK_EQ(5, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002990
2991 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002992 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002993 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002994}
2995
2996
2997TEST(DebugStepSwitch) {
2998 v8::HandleScope scope;
2999 DebugLocalContext env;
3000
3001 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003002 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003003
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003004 // Create a function for testing stepping. Run it to allow it to get
3005 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003006 const int argc = 1;
3007 const char* src = "function foo(x) { "
3008 " a = 1;"
3009 " switch (x) {"
3010 " case 1:"
3011 " b = 1;"
3012 " case 2:"
3013 " c = 1;"
3014 " break;"
3015 " case 3:"
3016 " d = 1;"
3017 " e = 1;"
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003018 " f = 1;"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003019 " break;"
3020 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003021 "}"
3022 "a=0; b=0; c=0; d=0; e=0; f=0; foo()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003023 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3024 SetBreakPoint(foo, 0);
3025
3026 // One case with fall-through.
3027 step_action = StepIn;
3028 break_point_hit_count = 0;
3029 v8::Handle<v8::Value> argv_1[argc] = { v8::Number::New(1) };
3030 foo->Call(env->Global(), argc, argv_1);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003031 CHECK_EQ(6, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003032
3033 // Another case.
3034 step_action = StepIn;
3035 break_point_hit_count = 0;
3036 v8::Handle<v8::Value> argv_2[argc] = { v8::Number::New(2) };
3037 foo->Call(env->Global(), argc, argv_2);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003038 CHECK_EQ(5, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003039
3040 // Last case.
3041 step_action = StepIn;
3042 break_point_hit_count = 0;
3043 v8::Handle<v8::Value> argv_3[argc] = { v8::Number::New(3) };
3044 foo->Call(env->Global(), argc, argv_3);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003045 CHECK_EQ(7, break_point_hit_count);
3046
3047 // Get rid of the debug event listener.
3048 v8::Debug::SetDebugEventListener(NULL);
3049 CheckDebuggerUnloaded();
3050}
3051
3052
3053TEST(DebugStepWhile) {
3054 v8::HandleScope scope;
3055 DebugLocalContext env;
3056
3057 // Register a debug event listener which steps and counts.
3058 v8::Debug::SetDebugEventListener(DebugEventStep);
3059
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003060 // Create a function for testing stepping. Run it to allow it to get
3061 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003062 const int argc = 1;
3063 const char* src = "function foo(x) { "
3064 " var a = 0;"
3065 " while (a < x) {"
3066 " a++;"
3067 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003068 "}"
3069 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003070 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3071 SetBreakPoint(foo, 8); // "var a = 0;"
3072
3073 // Looping 10 times.
3074 step_action = StepIn;
3075 break_point_hit_count = 0;
3076 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
3077 foo->Call(env->Global(), argc, argv_10);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003078 CHECK_EQ(22, break_point_hit_count);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003079
3080 // Looping 100 times.
3081 step_action = StepIn;
3082 break_point_hit_count = 0;
3083 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
3084 foo->Call(env->Global(), argc, argv_100);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003085 CHECK_EQ(202, break_point_hit_count);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003086
3087 // Get rid of the debug event listener.
3088 v8::Debug::SetDebugEventListener(NULL);
3089 CheckDebuggerUnloaded();
3090}
3091
3092
3093TEST(DebugStepDoWhile) {
3094 v8::HandleScope scope;
3095 DebugLocalContext env;
3096
3097 // Register a debug event listener which steps and counts.
3098 v8::Debug::SetDebugEventListener(DebugEventStep);
3099
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003100 // Create a function for testing stepping. Run it to allow it to get
3101 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003102 const int argc = 1;
3103 const char* src = "function foo(x) { "
3104 " var a = 0;"
3105 " do {"
3106 " a++;"
3107 " } while (a < x)"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003108 "}"
3109 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003110 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3111 SetBreakPoint(foo, 8); // "var a = 0;"
3112
3113 // Looping 10 times.
3114 step_action = StepIn;
3115 break_point_hit_count = 0;
3116 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
3117 foo->Call(env->Global(), argc, argv_10);
3118 CHECK_EQ(22, break_point_hit_count);
3119
3120 // Looping 100 times.
3121 step_action = StepIn;
3122 break_point_hit_count = 0;
3123 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
3124 foo->Call(env->Global(), argc, argv_100);
3125 CHECK_EQ(202, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003126
3127 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003128 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003129 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003130}
3131
3132
3133TEST(DebugStepFor) {
3134 v8::HandleScope scope;
3135 DebugLocalContext env;
3136
3137 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003138 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003139
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003140 // Create a function for testing stepping. Run it to allow it to get
3141 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003142 const int argc = 1;
3143 const char* src = "function foo(x) { "
3144 " a = 1;"
3145 " for (i = 0; i < x; i++) {"
3146 " b = 1;"
3147 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003148 "}"
3149 "a=0; b=0; i=0; foo()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003150 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003151
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003152 SetBreakPoint(foo, 8); // "a = 1;"
3153
3154 // Looping 10 times.
3155 step_action = StepIn;
3156 break_point_hit_count = 0;
3157 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
3158 foo->Call(env->Global(), argc, argv_10);
3159 CHECK_EQ(23, break_point_hit_count);
3160
3161 // Looping 100 times.
3162 step_action = StepIn;
3163 break_point_hit_count = 0;
3164 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
3165 foo->Call(env->Global(), argc, argv_100);
3166 CHECK_EQ(203, break_point_hit_count);
3167
3168 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003169 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003170 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003171}
3172
3173
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003174TEST(DebugStepForContinue) {
3175 v8::HandleScope scope;
3176 DebugLocalContext env;
3177
3178 // Register a debug event listener which steps and counts.
3179 v8::Debug::SetDebugEventListener(DebugEventStep);
3180
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003181 // Create a function for testing stepping. Run it to allow it to get
3182 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003183 const int argc = 1;
3184 const char* src = "function foo(x) { "
3185 " var a = 0;"
3186 " var b = 0;"
3187 " var c = 0;"
3188 " for (var i = 0; i < x; i++) {"
3189 " a++;"
3190 " if (a % 2 == 0) continue;"
3191 " b++;"
3192 " c++;"
3193 " }"
3194 " return b;"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003195 "}"
3196 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003197 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3198 v8::Handle<v8::Value> result;
3199 SetBreakPoint(foo, 8); // "var a = 0;"
3200
3201 // Each loop generates 4 or 5 steps depending on whether a is equal.
3202
3203 // Looping 10 times.
3204 step_action = StepIn;
3205 break_point_hit_count = 0;
3206 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
3207 result = foo->Call(env->Global(), argc, argv_10);
3208 CHECK_EQ(5, result->Int32Value());
3209 CHECK_EQ(50, break_point_hit_count);
3210
3211 // Looping 100 times.
3212 step_action = StepIn;
3213 break_point_hit_count = 0;
3214 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
3215 result = foo->Call(env->Global(), argc, argv_100);
3216 CHECK_EQ(50, result->Int32Value());
3217 CHECK_EQ(455, break_point_hit_count);
3218
3219 // Get rid of the debug event listener.
3220 v8::Debug::SetDebugEventListener(NULL);
3221 CheckDebuggerUnloaded();
3222}
3223
3224
3225TEST(DebugStepForBreak) {
3226 v8::HandleScope scope;
3227 DebugLocalContext env;
3228
3229 // Register a debug event listener which steps and counts.
3230 v8::Debug::SetDebugEventListener(DebugEventStep);
3231
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003232 // Create a function for testing stepping. Run it to allow it to get
3233 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003234 const int argc = 1;
3235 const char* src = "function foo(x) { "
3236 " var a = 0;"
3237 " var b = 0;"
3238 " var c = 0;"
3239 " for (var i = 0; i < 1000; i++) {"
3240 " a++;"
3241 " if (a == x) break;"
3242 " b++;"
3243 " c++;"
3244 " }"
3245 " return b;"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003246 "}"
3247 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003248 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3249 v8::Handle<v8::Value> result;
3250 SetBreakPoint(foo, 8); // "var a = 0;"
3251
3252 // Each loop generates 5 steps except for the last (when break is executed)
3253 // which only generates 4.
3254
3255 // Looping 10 times.
3256 step_action = StepIn;
3257 break_point_hit_count = 0;
3258 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
3259 result = foo->Call(env->Global(), argc, argv_10);
3260 CHECK_EQ(9, result->Int32Value());
3261 CHECK_EQ(53, break_point_hit_count);
3262
3263 // Looping 100 times.
3264 step_action = StepIn;
3265 break_point_hit_count = 0;
3266 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
3267 result = foo->Call(env->Global(), argc, argv_100);
3268 CHECK_EQ(99, result->Int32Value());
3269 CHECK_EQ(503, break_point_hit_count);
3270
3271 // Get rid of the debug event listener.
3272 v8::Debug::SetDebugEventListener(NULL);
3273 CheckDebuggerUnloaded();
3274}
3275
3276
3277TEST(DebugStepForIn) {
3278 v8::HandleScope scope;
3279 DebugLocalContext env;
3280
3281 // Register a debug event listener which steps and counts.
3282 v8::Debug::SetDebugEventListener(DebugEventStep);
3283
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003284 // Create a function for testing stepping. Run it to allow it to get
3285 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003286 v8::Local<v8::Function> foo;
3287 const char* src_1 = "function foo() { "
3288 " var a = [1, 2];"
3289 " for (x in a) {"
3290 " b = 0;"
3291 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003292 "}"
3293 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003294 foo = CompileFunction(&env, src_1, "foo");
3295 SetBreakPoint(foo, 0); // "var a = ..."
3296
3297 step_action = StepIn;
3298 break_point_hit_count = 0;
3299 foo->Call(env->Global(), 0, NULL);
3300 CHECK_EQ(6, break_point_hit_count);
3301
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003302 // Create a function for testing stepping. Run it to allow it to get
3303 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003304 const char* src_2 = "function foo() { "
3305 " var a = {a:[1, 2, 3]};"
3306 " for (x in a.a) {"
3307 " b = 0;"
3308 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003309 "}"
3310 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003311 foo = CompileFunction(&env, src_2, "foo");
3312 SetBreakPoint(foo, 0); // "var a = ..."
3313
3314 step_action = StepIn;
3315 break_point_hit_count = 0;
3316 foo->Call(env->Global(), 0, NULL);
3317 CHECK_EQ(8, break_point_hit_count);
3318
3319 // Get rid of the debug event listener.
3320 v8::Debug::SetDebugEventListener(NULL);
3321 CheckDebuggerUnloaded();
3322}
3323
3324
3325TEST(DebugStepWith) {
3326 v8::HandleScope scope;
3327 DebugLocalContext env;
3328
3329 // Register a debug event listener which steps and counts.
3330 v8::Debug::SetDebugEventListener(DebugEventStep);
3331
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003332 // Create a function for testing stepping. Run it to allow it to get
3333 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003334 const char* src = "function foo(x) { "
3335 " var a = {};"
3336 " with (a) {}"
3337 " with (b) {}"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003338 "}"
3339 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003340 env->Global()->Set(v8::String::New("b"), v8::Object::New());
3341 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3342 v8::Handle<v8::Value> result;
3343 SetBreakPoint(foo, 8); // "var a = {};"
3344
3345 step_action = StepIn;
3346 break_point_hit_count = 0;
3347 foo->Call(env->Global(), 0, NULL);
3348 CHECK_EQ(4, break_point_hit_count);
3349
3350 // Get rid of the debug event listener.
3351 v8::Debug::SetDebugEventListener(NULL);
3352 CheckDebuggerUnloaded();
3353}
3354
3355
3356TEST(DebugConditional) {
3357 v8::HandleScope scope;
3358 DebugLocalContext env;
3359
3360 // Register a debug event listener which steps and counts.
3361 v8::Debug::SetDebugEventListener(DebugEventStep);
3362
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003363 // Create a function for testing stepping. Run it to allow it to get
3364 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003365 const char* src = "function foo(x) { "
3366 " var a;"
3367 " a = x ? 1 : 2;"
3368 " return a;"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003369 "}"
3370 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003371 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3372 SetBreakPoint(foo, 0); // "var a;"
3373
3374 step_action = StepIn;
3375 break_point_hit_count = 0;
3376 foo->Call(env->Global(), 0, NULL);
3377 CHECK_EQ(5, break_point_hit_count);
3378
3379 step_action = StepIn;
3380 break_point_hit_count = 0;
3381 const int argc = 1;
3382 v8::Handle<v8::Value> argv_true[argc] = { v8::True() };
3383 foo->Call(env->Global(), argc, argv_true);
3384 CHECK_EQ(5, break_point_hit_count);
3385
3386 // Get rid of the debug event listener.
3387 v8::Debug::SetDebugEventListener(NULL);
3388 CheckDebuggerUnloaded();
3389}
3390
3391
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003392TEST(StepInOutSimple) {
3393 v8::HandleScope scope;
3394 DebugLocalContext env;
3395
3396 // Create a function for checking the function when hitting a break point.
3397 frame_function_name = CompileFunction(&env,
3398 frame_function_name_source,
3399 "frame_function_name");
3400
3401 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003402 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003403
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003404 // Create a function for testing stepping. Run it to allow it to get
3405 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003406 const char* src = "function a() {b();c();}; "
3407 "function b() {c();}; "
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003408 "function c() {}; "
3409 "a(); b(); c()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003410 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
3411 SetBreakPoint(a, 0);
3412
3413 // Step through invocation of a with step in.
3414 step_action = StepIn;
3415 break_point_hit_count = 0;
3416 expected_step_sequence = "abcbaca";
3417 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003418 CHECK_EQ(StrLength(expected_step_sequence),
3419 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003420
3421 // Step through invocation of a with step next.
3422 step_action = StepNext;
3423 break_point_hit_count = 0;
3424 expected_step_sequence = "aaa";
3425 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003426 CHECK_EQ(StrLength(expected_step_sequence),
3427 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003428
3429 // Step through invocation of a with step out.
3430 step_action = StepOut;
3431 break_point_hit_count = 0;
3432 expected_step_sequence = "a";
3433 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003434 CHECK_EQ(StrLength(expected_step_sequence),
3435 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003436
3437 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003438 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003439 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003440}
3441
3442
3443TEST(StepInOutTree) {
3444 v8::HandleScope scope;
3445 DebugLocalContext env;
3446
3447 // Create a function for checking the function when hitting a break point.
3448 frame_function_name = CompileFunction(&env,
3449 frame_function_name_source,
3450 "frame_function_name");
3451
3452 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003453 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003454
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003455 // Create a function for testing stepping. Run it to allow it to get
3456 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003457 const char* src = "function a() {b(c(d()),d());c(d());d()}; "
3458 "function b(x,y) {c();}; "
3459 "function c(x) {}; "
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003460 "function d() {}; "
3461 "a(); b(); c(); d()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003462 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
3463 SetBreakPoint(a, 0);
3464
3465 // Step through invocation of a with step in.
3466 step_action = StepIn;
3467 break_point_hit_count = 0;
3468 expected_step_sequence = "adacadabcbadacada";
3469 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003470 CHECK_EQ(StrLength(expected_step_sequence),
3471 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003472
3473 // Step through invocation of a with step next.
3474 step_action = StepNext;
3475 break_point_hit_count = 0;
3476 expected_step_sequence = "aaaa";
3477 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003478 CHECK_EQ(StrLength(expected_step_sequence),
3479 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003480
3481 // Step through invocation of a with step out.
3482 step_action = StepOut;
3483 break_point_hit_count = 0;
3484 expected_step_sequence = "a";
3485 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003486 CHECK_EQ(StrLength(expected_step_sequence),
3487 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003488
3489 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003490 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003491 CheckDebuggerUnloaded(true);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003492}
3493
3494
3495TEST(StepInOutBranch) {
3496 v8::HandleScope scope;
3497 DebugLocalContext env;
3498
3499 // Create a function for checking the function when hitting a break point.
3500 frame_function_name = CompileFunction(&env,
3501 frame_function_name_source,
3502 "frame_function_name");
3503
3504 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003505 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003506
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003507 // Create a function for testing stepping. Run it to allow it to get
3508 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003509 const char* src = "function a() {b(false);c();}; "
3510 "function b(x) {if(x){c();};}; "
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003511 "function c() {}; "
3512 "a(); b(); c()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003513 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
3514 SetBreakPoint(a, 0);
3515
3516 // Step through invocation of a.
3517 step_action = StepIn;
3518 break_point_hit_count = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003519 expected_step_sequence = "abbaca";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003520 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003521 CHECK_EQ(StrLength(expected_step_sequence),
3522 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003523
3524 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003525 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003526 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003527}
3528
3529
3530// Test that step in does not step into native functions.
3531TEST(DebugStepNatives) {
3532 v8::HandleScope scope;
3533 DebugLocalContext env;
3534
3535 // Create a function for testing stepping.
3536 v8::Local<v8::Function> foo = CompileFunction(
3537 &env,
3538 "function foo(){debugger;Math.sin(1);}",
3539 "foo");
3540
3541 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003542 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003543
3544 step_action = StepIn;
3545 break_point_hit_count = 0;
3546 foo->Call(env->Global(), 0, NULL);
3547
3548 // With stepping all break locations are hit.
3549 CHECK_EQ(3, break_point_hit_count);
3550
iposva@chromium.org245aa852009-02-10 00:49:54 +00003551 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003552 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003553
3554 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003555 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003556
3557 break_point_hit_count = 0;
3558 foo->Call(env->Global(), 0, NULL);
3559
3560 // Without stepping only active break points are hit.
3561 CHECK_EQ(1, break_point_hit_count);
3562
iposva@chromium.org245aa852009-02-10 00:49:54 +00003563 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003564 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003565}
3566
3567
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003568// Test that step in works with function.apply.
3569TEST(DebugStepFunctionApply) {
3570 v8::HandleScope scope;
3571 DebugLocalContext env;
3572
3573 // Create a function for testing stepping.
3574 v8::Local<v8::Function> foo = CompileFunction(
3575 &env,
3576 "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }"
3577 "function foo(){ debugger; bar.apply(this, [1,2,3]); }",
3578 "foo");
3579
3580 // Register a debug event listener which steps and counts.
3581 v8::Debug::SetDebugEventListener(DebugEventStep);
3582
3583 step_action = StepIn;
3584 break_point_hit_count = 0;
3585 foo->Call(env->Global(), 0, NULL);
3586
3587 // With stepping all break locations are hit.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003588 CHECK_EQ(7, break_point_hit_count);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003589
3590 v8::Debug::SetDebugEventListener(NULL);
3591 CheckDebuggerUnloaded();
3592
3593 // Register a debug event listener which just counts.
3594 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
3595
3596 break_point_hit_count = 0;
3597 foo->Call(env->Global(), 0, NULL);
3598
3599 // Without stepping only the debugger statement is hit.
3600 CHECK_EQ(1, break_point_hit_count);
3601
3602 v8::Debug::SetDebugEventListener(NULL);
3603 CheckDebuggerUnloaded();
3604}
3605
3606
3607// Test that step in works with function.call.
3608TEST(DebugStepFunctionCall) {
3609 v8::HandleScope scope;
3610 DebugLocalContext env;
3611
3612 // Create a function for testing stepping.
3613 v8::Local<v8::Function> foo = CompileFunction(
3614 &env,
3615 "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }"
3616 "function foo(a){ debugger;"
3617 " if (a) {"
3618 " bar.call(this, 1, 2, 3);"
3619 " } else {"
3620 " bar.call(this, 0);"
3621 " }"
3622 "}",
3623 "foo");
3624
3625 // Register a debug event listener which steps and counts.
3626 v8::Debug::SetDebugEventListener(DebugEventStep);
3627 step_action = StepIn;
3628
3629 // Check stepping where the if condition in bar is false.
3630 break_point_hit_count = 0;
3631 foo->Call(env->Global(), 0, NULL);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003632 CHECK_EQ(6, break_point_hit_count);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003633
3634 // Check stepping where the if condition in bar is true.
3635 break_point_hit_count = 0;
3636 const int argc = 1;
3637 v8::Handle<v8::Value> argv[argc] = { v8::True() };
3638 foo->Call(env->Global(), argc, argv);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003639 CHECK_EQ(8, break_point_hit_count);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003640
3641 v8::Debug::SetDebugEventListener(NULL);
3642 CheckDebuggerUnloaded();
3643
3644 // Register a debug event listener which just counts.
3645 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
3646
3647 break_point_hit_count = 0;
3648 foo->Call(env->Global(), 0, NULL);
3649
3650 // Without stepping only the debugger statement is hit.
3651 CHECK_EQ(1, break_point_hit_count);
3652
3653 v8::Debug::SetDebugEventListener(NULL);
3654 CheckDebuggerUnloaded();
3655}
3656
3657
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003658// Tests that breakpoint will be hit if it's set in script.
3659TEST(PauseInScript) {
3660 v8::HandleScope scope;
3661 DebugLocalContext env;
3662 env.ExposeDebug();
3663
3664 // Register a debug event listener which counts.
3665 v8::Debug::SetDebugEventListener(DebugEventCounter);
3666
3667 // Create a script that returns a function.
3668 const char* src = "(function (evt) {})";
3669 const char* script_name = "StepInHandlerTest";
3670
3671 // Set breakpoint in the script.
3672 SetScriptBreakPointByNameFromJS(script_name, 0, -1);
3673 break_point_hit_count = 0;
3674
3675 v8::ScriptOrigin origin(v8::String::New(script_name), v8::Integer::New(0));
3676 v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(src),
3677 &origin);
3678 v8::Local<v8::Value> r = script->Run();
3679
3680 CHECK(r->IsFunction());
3681 CHECK_EQ(1, break_point_hit_count);
3682
3683 // Get rid of the debug event listener.
3684 v8::Debug::SetDebugEventListener(NULL);
3685 CheckDebuggerUnloaded();
3686}
3687
3688
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003689// Test break on exceptions. For each exception break combination the number
3690// of debug event exception callbacks and message callbacks are collected. The
ager@chromium.org8bb60582008-12-11 12:02:20 +00003691// number of debug event exception callbacks are used to check that the
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003692// debugger is called correctly and the number of message callbacks is used to
3693// check that uncaught exceptions are still returned even if there is a break
3694// for them.
3695TEST(BreakOnException) {
3696 v8::HandleScope scope;
3697 DebugLocalContext env;
3698 env.ExposeDebug();
3699
3700 v8::internal::Top::TraceException(false);
3701
3702 // Create functions for testing break on exception.
3703 v8::Local<v8::Function> throws =
3704 CompileFunction(&env, "function throws(){throw 1;}", "throws");
3705 v8::Local<v8::Function> caught =
3706 CompileFunction(&env,
3707 "function caught(){try {throws();} catch(e) {};}",
3708 "caught");
3709 v8::Local<v8::Function> notCaught =
3710 CompileFunction(&env, "function notCaught(){throws();}", "notCaught");
3711
3712 v8::V8::AddMessageListener(MessageCallbackCount);
iposva@chromium.org245aa852009-02-10 00:49:54 +00003713 v8::Debug::SetDebugEventListener(DebugEventCounter);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003714
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003715 // Initial state should be no break on exceptions.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003716 DebugEventCounterClear();
3717 MessageCallbackCountClear();
3718 caught->Call(env->Global(), 0, NULL);
3719 CHECK_EQ(0, exception_hit_count);
3720 CHECK_EQ(0, uncaught_exception_hit_count);
3721 CHECK_EQ(0, message_callback_count);
3722 notCaught->Call(env->Global(), 0, NULL);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003723 CHECK_EQ(0, exception_hit_count);
3724 CHECK_EQ(0, uncaught_exception_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003725 CHECK_EQ(1, message_callback_count);
3726
3727 // No break on exception
3728 DebugEventCounterClear();
3729 MessageCallbackCountClear();
3730 ChangeBreakOnException(false, false);
3731 caught->Call(env->Global(), 0, NULL);
3732 CHECK_EQ(0, exception_hit_count);
3733 CHECK_EQ(0, uncaught_exception_hit_count);
3734 CHECK_EQ(0, message_callback_count);
3735 notCaught->Call(env->Global(), 0, NULL);
3736 CHECK_EQ(0, exception_hit_count);
3737 CHECK_EQ(0, uncaught_exception_hit_count);
3738 CHECK_EQ(1, message_callback_count);
3739
3740 // Break on uncaught exception
3741 DebugEventCounterClear();
3742 MessageCallbackCountClear();
3743 ChangeBreakOnException(false, true);
3744 caught->Call(env->Global(), 0, NULL);
3745 CHECK_EQ(0, exception_hit_count);
3746 CHECK_EQ(0, uncaught_exception_hit_count);
3747 CHECK_EQ(0, message_callback_count);
3748 notCaught->Call(env->Global(), 0, NULL);
3749 CHECK_EQ(1, exception_hit_count);
3750 CHECK_EQ(1, uncaught_exception_hit_count);
3751 CHECK_EQ(1, message_callback_count);
3752
3753 // Break on exception and uncaught exception
3754 DebugEventCounterClear();
3755 MessageCallbackCountClear();
3756 ChangeBreakOnException(true, true);
3757 caught->Call(env->Global(), 0, NULL);
3758 CHECK_EQ(1, exception_hit_count);
3759 CHECK_EQ(0, uncaught_exception_hit_count);
3760 CHECK_EQ(0, message_callback_count);
3761 notCaught->Call(env->Global(), 0, NULL);
3762 CHECK_EQ(2, exception_hit_count);
3763 CHECK_EQ(1, uncaught_exception_hit_count);
3764 CHECK_EQ(1, message_callback_count);
3765
3766 // Break on exception
3767 DebugEventCounterClear();
3768 MessageCallbackCountClear();
3769 ChangeBreakOnException(true, false);
3770 caught->Call(env->Global(), 0, NULL);
3771 CHECK_EQ(1, exception_hit_count);
3772 CHECK_EQ(0, uncaught_exception_hit_count);
3773 CHECK_EQ(0, message_callback_count);
3774 notCaught->Call(env->Global(), 0, NULL);
3775 CHECK_EQ(2, exception_hit_count);
3776 CHECK_EQ(1, uncaught_exception_hit_count);
3777 CHECK_EQ(1, message_callback_count);
3778
3779 // No break on exception using JavaScript
3780 DebugEventCounterClear();
3781 MessageCallbackCountClear();
3782 ChangeBreakOnExceptionFromJS(false, false);
3783 caught->Call(env->Global(), 0, NULL);
3784 CHECK_EQ(0, exception_hit_count);
3785 CHECK_EQ(0, uncaught_exception_hit_count);
3786 CHECK_EQ(0, message_callback_count);
3787 notCaught->Call(env->Global(), 0, NULL);
3788 CHECK_EQ(0, exception_hit_count);
3789 CHECK_EQ(0, uncaught_exception_hit_count);
3790 CHECK_EQ(1, message_callback_count);
3791
3792 // Break on uncaught exception using JavaScript
3793 DebugEventCounterClear();
3794 MessageCallbackCountClear();
3795 ChangeBreakOnExceptionFromJS(false, true);
3796 caught->Call(env->Global(), 0, NULL);
3797 CHECK_EQ(0, exception_hit_count);
3798 CHECK_EQ(0, uncaught_exception_hit_count);
3799 CHECK_EQ(0, message_callback_count);
3800 notCaught->Call(env->Global(), 0, NULL);
3801 CHECK_EQ(1, exception_hit_count);
3802 CHECK_EQ(1, uncaught_exception_hit_count);
3803 CHECK_EQ(1, message_callback_count);
3804
3805 // Break on exception and uncaught exception using JavaScript
3806 DebugEventCounterClear();
3807 MessageCallbackCountClear();
3808 ChangeBreakOnExceptionFromJS(true, true);
3809 caught->Call(env->Global(), 0, NULL);
3810 CHECK_EQ(1, exception_hit_count);
3811 CHECK_EQ(0, message_callback_count);
3812 CHECK_EQ(0, uncaught_exception_hit_count);
3813 notCaught->Call(env->Global(), 0, NULL);
3814 CHECK_EQ(2, exception_hit_count);
3815 CHECK_EQ(1, uncaught_exception_hit_count);
3816 CHECK_EQ(1, message_callback_count);
3817
3818 // Break on exception using JavaScript
3819 DebugEventCounterClear();
3820 MessageCallbackCountClear();
3821 ChangeBreakOnExceptionFromJS(true, false);
3822 caught->Call(env->Global(), 0, NULL);
3823 CHECK_EQ(1, exception_hit_count);
3824 CHECK_EQ(0, uncaught_exception_hit_count);
3825 CHECK_EQ(0, message_callback_count);
3826 notCaught->Call(env->Global(), 0, NULL);
3827 CHECK_EQ(2, exception_hit_count);
3828 CHECK_EQ(1, uncaught_exception_hit_count);
3829 CHECK_EQ(1, message_callback_count);
3830
iposva@chromium.org245aa852009-02-10 00:49:54 +00003831 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003832 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003833 v8::V8::RemoveMessageListeners(MessageCallbackCount);
3834}
3835
3836
ager@chromium.org8bb60582008-12-11 12:02:20 +00003837// Test break on exception from compiler errors. When compiling using
3838// v8::Script::Compile there is no JavaScript stack whereas when compiling using
3839// eval there are JavaScript frames.
3840TEST(BreakOnCompileException) {
3841 v8::HandleScope scope;
3842 DebugLocalContext env;
3843
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003844 // For this test, we want to break on uncaught exceptions:
3845 ChangeBreakOnException(false, true);
3846
ager@chromium.org8bb60582008-12-11 12:02:20 +00003847 v8::internal::Top::TraceException(false);
3848
3849 // Create a function for checking the function when hitting a break point.
3850 frame_count = CompileFunction(&env, frame_count_source, "frame_count");
3851
3852 v8::V8::AddMessageListener(MessageCallbackCount);
iposva@chromium.org245aa852009-02-10 00:49:54 +00003853 v8::Debug::SetDebugEventListener(DebugEventCounter);
ager@chromium.org8bb60582008-12-11 12:02:20 +00003854
3855 DebugEventCounterClear();
3856 MessageCallbackCountClear();
3857
3858 // Check initial state.
3859 CHECK_EQ(0, exception_hit_count);
3860 CHECK_EQ(0, uncaught_exception_hit_count);
3861 CHECK_EQ(0, message_callback_count);
3862 CHECK_EQ(-1, last_js_stack_height);
3863
3864 // Throws SyntaxError: Unexpected end of input
3865 v8::Script::Compile(v8::String::New("+++"));
3866 CHECK_EQ(1, exception_hit_count);
3867 CHECK_EQ(1, uncaught_exception_hit_count);
3868 CHECK_EQ(1, message_callback_count);
3869 CHECK_EQ(0, last_js_stack_height); // No JavaScript stack.
3870
3871 // Throws SyntaxError: Unexpected identifier
3872 v8::Script::Compile(v8::String::New("x x"));
3873 CHECK_EQ(2, exception_hit_count);
3874 CHECK_EQ(2, uncaught_exception_hit_count);
3875 CHECK_EQ(2, message_callback_count);
3876 CHECK_EQ(0, last_js_stack_height); // No JavaScript stack.
3877
3878 // Throws SyntaxError: Unexpected end of input
3879 v8::Script::Compile(v8::String::New("eval('+++')"))->Run();
3880 CHECK_EQ(3, exception_hit_count);
3881 CHECK_EQ(3, uncaught_exception_hit_count);
3882 CHECK_EQ(3, message_callback_count);
3883 CHECK_EQ(1, last_js_stack_height);
3884
3885 // Throws SyntaxError: Unexpected identifier
3886 v8::Script::Compile(v8::String::New("eval('x x')"))->Run();
3887 CHECK_EQ(4, exception_hit_count);
3888 CHECK_EQ(4, uncaught_exception_hit_count);
3889 CHECK_EQ(4, message_callback_count);
3890 CHECK_EQ(1, last_js_stack_height);
3891}
3892
3893
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003894TEST(StepWithException) {
3895 v8::HandleScope scope;
3896 DebugLocalContext env;
3897
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003898 // For this test, we want to break on uncaught exceptions:
3899 ChangeBreakOnException(false, true);
3900
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003901 // Create a function for checking the function when hitting a break point.
3902 frame_function_name = CompileFunction(&env,
3903 frame_function_name_source,
3904 "frame_function_name");
3905
3906 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003907 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003908
3909 // Create functions for testing stepping.
3910 const char* src = "function a() { n(); }; "
3911 "function b() { c(); }; "
3912 "function c() { n(); }; "
3913 "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; "
3914 "function e() { n(); }; "
3915 "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; "
3916 "function g() { h(); }; "
3917 "function h() { x = 1; throw 1; }; ";
3918
3919 // Step through invocation of a.
3920 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
3921 SetBreakPoint(a, 0);
3922 step_action = StepIn;
3923 break_point_hit_count = 0;
3924 expected_step_sequence = "aa";
3925 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003926 CHECK_EQ(StrLength(expected_step_sequence),
3927 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003928
3929 // Step through invocation of b + c.
3930 v8::Local<v8::Function> b = CompileFunction(&env, src, "b");
3931 SetBreakPoint(b, 0);
3932 step_action = StepIn;
3933 break_point_hit_count = 0;
3934 expected_step_sequence = "bcc";
3935 b->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003936 CHECK_EQ(StrLength(expected_step_sequence),
3937 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003938 // Step through invocation of d + e.
3939 v8::Local<v8::Function> d = CompileFunction(&env, src, "d");
3940 SetBreakPoint(d, 0);
3941 ChangeBreakOnException(false, true);
3942 step_action = StepIn;
3943 break_point_hit_count = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003944 expected_step_sequence = "ddedd";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003945 d->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003946 CHECK_EQ(StrLength(expected_step_sequence),
3947 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003948
3949 // Step through invocation of d + e now with break on caught exceptions.
3950 ChangeBreakOnException(true, true);
3951 step_action = StepIn;
3952 break_point_hit_count = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003953 expected_step_sequence = "ddeedd";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003954 d->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003955 CHECK_EQ(StrLength(expected_step_sequence),
3956 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003957
3958 // Step through invocation of f + g + h.
3959 v8::Local<v8::Function> f = CompileFunction(&env, src, "f");
3960 SetBreakPoint(f, 0);
3961 ChangeBreakOnException(false, true);
3962 step_action = StepIn;
3963 break_point_hit_count = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003964 expected_step_sequence = "ffghhff";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003965 f->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003966 CHECK_EQ(StrLength(expected_step_sequence),
3967 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003968
3969 // Step through invocation of f + g + h now with break on caught exceptions.
3970 ChangeBreakOnException(true, true);
3971 step_action = StepIn;
3972 break_point_hit_count = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003973 expected_step_sequence = "ffghhhff";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003974 f->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003975 CHECK_EQ(StrLength(expected_step_sequence),
3976 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003977
3978 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003979 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003980 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003981}
3982
3983
3984TEST(DebugBreak) {
3985 v8::HandleScope scope;
3986 DebugLocalContext env;
3987
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003988 // This test should be run with option --verify-heap. As --verify-heap is
3989 // only available in debug mode only check for it in that case.
3990#ifdef DEBUG
3991 CHECK(v8::internal::FLAG_verify_heap);
3992#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003993
3994 // Register a debug event listener which sets the break flag and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003995 v8::Debug::SetDebugEventListener(DebugEventBreak);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003996
3997 // Create a function for testing stepping.
3998 const char* src = "function f0() {}"
3999 "function f1(x1) {}"
4000 "function f2(x1,x2) {}"
4001 "function f3(x1,x2,x3) {}";
4002 v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0");
4003 v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1");
4004 v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2");
4005 v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3");
4006
4007 // Call the function to make sure it is compiled.
4008 v8::Handle<v8::Value> argv[] = { v8::Number::New(1),
4009 v8::Number::New(1),
4010 v8::Number::New(1),
4011 v8::Number::New(1) };
4012
4013 // Call all functions to make sure that they are compiled.
4014 f0->Call(env->Global(), 0, NULL);
4015 f1->Call(env->Global(), 0, NULL);
4016 f2->Call(env->Global(), 0, NULL);
4017 f3->Call(env->Global(), 0, NULL);
4018
4019 // Set the debug break flag.
4020 v8::Debug::DebugBreak();
4021
4022 // Call all functions with different argument count.
4023 break_point_hit_count = 0;
4024 for (unsigned int i = 0; i < ARRAY_SIZE(argv); i++) {
4025 f0->Call(env->Global(), i, argv);
4026 f1->Call(env->Global(), i, argv);
4027 f2->Call(env->Global(), i, argv);
4028 f3->Call(env->Global(), i, argv);
4029 }
4030
4031 // One break for each function called.
4032 CHECK_EQ(4 * ARRAY_SIZE(argv), break_point_hit_count);
4033
4034 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00004035 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00004036 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004037}
4038
4039
4040// Test to ensure that JavaScript code keeps running while the debug break
4041// through the stack limit flag is set but breaks are disabled.
4042TEST(DisableBreak) {
4043 v8::HandleScope scope;
4044 DebugLocalContext env;
4045
4046 // Register a debug event listener which sets the break flag and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00004047 v8::Debug::SetDebugEventListener(DebugEventCounter);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004048
4049 // Create a function for testing stepping.
4050 const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}";
4051 v8::Local<v8::Function> f = CompileFunction(&env, src, "f");
4052
4053 // Set the debug break flag.
4054 v8::Debug::DebugBreak();
4055
4056 // Call all functions with different argument count.
4057 break_point_hit_count = 0;
4058 f->Call(env->Global(), 0, NULL);
4059 CHECK_EQ(1, break_point_hit_count);
4060
4061 {
4062 v8::Debug::DebugBreak();
4063 v8::internal::DisableBreak disable_break(true);
4064 f->Call(env->Global(), 0, NULL);
4065 CHECK_EQ(1, break_point_hit_count);
4066 }
4067
4068 f->Call(env->Global(), 0, NULL);
4069 CHECK_EQ(2, break_point_hit_count);
4070
4071 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00004072 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00004073 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004074}
4075
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004076static const char* kSimpleExtensionSource =
4077 "(function Foo() {"
4078 " return 4;"
4079 "})() ";
4080
4081// http://crbug.com/28933
4082// Test that debug break is disabled when bootstrapper is active.
4083TEST(NoBreakWhenBootstrapping) {
4084 v8::HandleScope scope;
4085
4086 // Register a debug event listener which sets the break flag and counts.
4087 v8::Debug::SetDebugEventListener(DebugEventCounter);
4088
4089 // Set the debug break flag.
4090 v8::Debug::DebugBreak();
4091 break_point_hit_count = 0;
4092 {
4093 // Create a context with an extension to make sure that some JavaScript
4094 // code is executed during bootstrapping.
4095 v8::RegisterExtension(new v8::Extension("simpletest",
4096 kSimpleExtensionSource));
4097 const char* extension_names[] = { "simpletest" };
4098 v8::ExtensionConfiguration extensions(1, extension_names);
4099 v8::Persistent<v8::Context> context = v8::Context::New(&extensions);
4100 context.Dispose();
4101 }
4102 // Check that no DebugBreak events occured during the context creation.
4103 CHECK_EQ(0, break_point_hit_count);
4104
4105 // Get rid of the debug event listener.
4106 v8::Debug::SetDebugEventListener(NULL);
4107 CheckDebuggerUnloaded();
4108}
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004109
4110static v8::Handle<v8::Array> NamedEnum(const v8::AccessorInfo&) {
4111 v8::Handle<v8::Array> result = v8::Array::New(3);
4112 result->Set(v8::Integer::New(0), v8::String::New("a"));
4113 result->Set(v8::Integer::New(1), v8::String::New("b"));
4114 result->Set(v8::Integer::New(2), v8::String::New("c"));
4115 return result;
4116}
4117
4118
4119static v8::Handle<v8::Array> IndexedEnum(const v8::AccessorInfo&) {
4120 v8::Handle<v8::Array> result = v8::Array::New(2);
4121 result->Set(v8::Integer::New(0), v8::Number::New(1));
4122 result->Set(v8::Integer::New(1), v8::Number::New(10));
4123 return result;
4124}
4125
4126
4127static v8::Handle<v8::Value> NamedGetter(v8::Local<v8::String> name,
4128 const v8::AccessorInfo& info) {
4129 v8::String::AsciiValue n(name);
4130 if (strcmp(*n, "a") == 0) {
4131 return v8::String::New("AA");
4132 } else if (strcmp(*n, "b") == 0) {
4133 return v8::String::New("BB");
4134 } else if (strcmp(*n, "c") == 0) {
4135 return v8::String::New("CC");
4136 } else {
4137 return v8::Undefined();
4138 }
4139
4140 return name;
4141}
4142
4143
4144static v8::Handle<v8::Value> IndexedGetter(uint32_t index,
4145 const v8::AccessorInfo& info) {
4146 return v8::Number::New(index + 1);
4147}
4148
4149
4150TEST(InterceptorPropertyMirror) {
4151 // Create a V8 environment with debug access.
4152 v8::HandleScope scope;
4153 DebugLocalContext env;
4154 env.ExposeDebug();
4155
4156 // Create object with named interceptor.
4157 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New();
4158 named->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum);
4159 env->Global()->Set(v8::String::New("intercepted_named"),
4160 named->NewInstance());
4161
4162 // Create object with indexed interceptor.
4163 v8::Handle<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New();
4164 indexed->SetIndexedPropertyHandler(IndexedGetter,
4165 NULL,
4166 NULL,
4167 NULL,
4168 IndexedEnum);
4169 env->Global()->Set(v8::String::New("intercepted_indexed"),
4170 indexed->NewInstance());
4171
4172 // Create object with both named and indexed interceptor.
4173 v8::Handle<v8::ObjectTemplate> both = v8::ObjectTemplate::New();
4174 both->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum);
4175 both->SetIndexedPropertyHandler(IndexedGetter, NULL, NULL, NULL, IndexedEnum);
4176 env->Global()->Set(v8::String::New("intercepted_both"), both->NewInstance());
4177
4178 // Get mirrors for the three objects with interceptor.
4179 CompileRun(
4180 "named_mirror = debug.MakeMirror(intercepted_named);"
4181 "indexed_mirror = debug.MakeMirror(intercepted_indexed);"
4182 "both_mirror = debug.MakeMirror(intercepted_both)");
4183 CHECK(CompileRun(
4184 "named_mirror instanceof debug.ObjectMirror")->BooleanValue());
4185 CHECK(CompileRun(
4186 "indexed_mirror instanceof debug.ObjectMirror")->BooleanValue());
4187 CHECK(CompileRun(
4188 "both_mirror instanceof debug.ObjectMirror")->BooleanValue());
4189
4190 // Get the property names from the interceptors
4191 CompileRun(
ager@chromium.org32912102009-01-16 10:38:43 +00004192 "named_names = named_mirror.propertyNames();"
4193 "indexed_names = indexed_mirror.propertyNames();"
4194 "both_names = both_mirror.propertyNames()");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004195 CHECK_EQ(3, CompileRun("named_names.length")->Int32Value());
4196 CHECK_EQ(2, CompileRun("indexed_names.length")->Int32Value());
4197 CHECK_EQ(5, CompileRun("both_names.length")->Int32Value());
4198
4199 // Check the expected number of properties.
4200 const char* source;
ager@chromium.org32912102009-01-16 10:38:43 +00004201 source = "named_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004202 CHECK_EQ(3, CompileRun(source)->Int32Value());
4203
ager@chromium.org32912102009-01-16 10:38:43 +00004204 source = "indexed_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004205 CHECK_EQ(2, CompileRun(source)->Int32Value());
4206
ager@chromium.org32912102009-01-16 10:38:43 +00004207 source = "both_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004208 CHECK_EQ(5, CompileRun(source)->Int32Value());
4209
ager@chromium.org32912102009-01-16 10:38:43 +00004210 // 1 is PropertyKind.Named;
4211 source = "both_mirror.properties(1).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004212 CHECK_EQ(3, CompileRun(source)->Int32Value());
4213
ager@chromium.org32912102009-01-16 10:38:43 +00004214 // 2 is PropertyKind.Indexed;
4215 source = "both_mirror.properties(2).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004216 CHECK_EQ(2, CompileRun(source)->Int32Value());
4217
ager@chromium.org32912102009-01-16 10:38:43 +00004218 // 3 is PropertyKind.Named | PropertyKind.Indexed;
4219 source = "both_mirror.properties(3).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004220 CHECK_EQ(5, CompileRun(source)->Int32Value());
4221
ager@chromium.org32912102009-01-16 10:38:43 +00004222 // Get the interceptor properties for the object with only named interceptor.
4223 CompileRun("named_values = named_mirror.properties()");
4224
4225 // Check that the properties are interceptor properties.
4226 for (int i = 0; i < 3; i++) {
4227 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
4228 OS::SNPrintF(buffer,
4229 "named_values[%d] instanceof debug.PropertyMirror", i);
4230 CHECK(CompileRun(buffer.start())->BooleanValue());
4231
4232 // 4 is PropertyType.Interceptor
4233 OS::SNPrintF(buffer, "named_values[%d].propertyType()", i);
4234 CHECK_EQ(4, CompileRun(buffer.start())->Int32Value());
4235
4236 OS::SNPrintF(buffer, "named_values[%d].isNative()", i);
4237 CHECK(CompileRun(buffer.start())->BooleanValue());
4238 }
4239
4240 // Get the interceptor properties for the object with only indexed
4241 // interceptor.
4242 CompileRun("indexed_values = indexed_mirror.properties()");
4243
4244 // Check that the properties are interceptor properties.
4245 for (int i = 0; i < 2; i++) {
4246 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
4247 OS::SNPrintF(buffer,
4248 "indexed_values[%d] instanceof debug.PropertyMirror", i);
4249 CHECK(CompileRun(buffer.start())->BooleanValue());
4250 }
4251
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004252 // Get the interceptor properties for the object with both types of
4253 // interceptors.
ager@chromium.org32912102009-01-16 10:38:43 +00004254 CompileRun("both_values = both_mirror.properties()");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004255
ager@chromium.org32912102009-01-16 10:38:43 +00004256 // Check that the properties are interceptor properties.
4257 for (int i = 0; i < 5; i++) {
4258 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
4259 OS::SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i);
4260 CHECK(CompileRun(buffer.start())->BooleanValue());
4261 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004262
4263 // Check the property names.
4264 source = "both_values[0].name() == 'a'";
4265 CHECK(CompileRun(source)->BooleanValue());
4266
4267 source = "both_values[1].name() == 'b'";
4268 CHECK(CompileRun(source)->BooleanValue());
4269
4270 source = "both_values[2].name() == 'c'";
4271 CHECK(CompileRun(source)->BooleanValue());
4272
4273 source = "both_values[3].name() == 1";
4274 CHECK(CompileRun(source)->BooleanValue());
4275
4276 source = "both_values[4].name() == 10";
4277 CHECK(CompileRun(source)->BooleanValue());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004278}
4279
4280
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004281TEST(HiddenPrototypePropertyMirror) {
4282 // Create a V8 environment with debug access.
4283 v8::HandleScope scope;
4284 DebugLocalContext env;
4285 env.ExposeDebug();
4286
4287 v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New();
4288 t0->InstanceTemplate()->Set(v8::String::New("x"), v8::Number::New(0));
4289 v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New();
4290 t1->SetHiddenPrototype(true);
4291 t1->InstanceTemplate()->Set(v8::String::New("y"), v8::Number::New(1));
4292 v8::Handle<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New();
4293 t2->SetHiddenPrototype(true);
4294 t2->InstanceTemplate()->Set(v8::String::New("z"), v8::Number::New(2));
4295 v8::Handle<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New();
4296 t3->InstanceTemplate()->Set(v8::String::New("u"), v8::Number::New(3));
4297
4298 // Create object and set them on the global object.
4299 v8::Handle<v8::Object> o0 = t0->GetFunction()->NewInstance();
4300 env->Global()->Set(v8::String::New("o0"), o0);
4301 v8::Handle<v8::Object> o1 = t1->GetFunction()->NewInstance();
4302 env->Global()->Set(v8::String::New("o1"), o1);
4303 v8::Handle<v8::Object> o2 = t2->GetFunction()->NewInstance();
4304 env->Global()->Set(v8::String::New("o2"), o2);
4305 v8::Handle<v8::Object> o3 = t3->GetFunction()->NewInstance();
4306 env->Global()->Set(v8::String::New("o3"), o3);
4307
4308 // Get mirrors for the four objects.
4309 CompileRun(
4310 "o0_mirror = debug.MakeMirror(o0);"
4311 "o1_mirror = debug.MakeMirror(o1);"
4312 "o2_mirror = debug.MakeMirror(o2);"
4313 "o3_mirror = debug.MakeMirror(o3)");
4314 CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror")->BooleanValue());
4315 CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror")->BooleanValue());
4316 CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror")->BooleanValue());
4317 CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror")->BooleanValue());
4318
4319 // Check that each object has one property.
4320 CHECK_EQ(1, CompileRun(
4321 "o0_mirror.propertyNames().length")->Int32Value());
4322 CHECK_EQ(1, CompileRun(
4323 "o1_mirror.propertyNames().length")->Int32Value());
4324 CHECK_EQ(1, CompileRun(
4325 "o2_mirror.propertyNames().length")->Int32Value());
4326 CHECK_EQ(1, CompileRun(
4327 "o3_mirror.propertyNames().length")->Int32Value());
4328
4329 // Set o1 as prototype for o0. o1 has the hidden prototype flag so all
4330 // properties on o1 should be seen on o0.
4331 o0->Set(v8::String::New("__proto__"), o1);
4332 CHECK_EQ(2, CompileRun(
4333 "o0_mirror.propertyNames().length")->Int32Value());
4334 CHECK_EQ(0, CompileRun(
4335 "o0_mirror.property('x').value().value()")->Int32Value());
4336 CHECK_EQ(1, CompileRun(
4337 "o0_mirror.property('y').value().value()")->Int32Value());
4338
4339 // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden
4340 // prototype flag. o2 also has the hidden prototype flag so all properties
4341 // on o2 should be seen on o0 as well as properties on o1.
4342 o0->Set(v8::String::New("__proto__"), o2);
4343 CHECK_EQ(3, CompileRun(
4344 "o0_mirror.propertyNames().length")->Int32Value());
4345 CHECK_EQ(0, CompileRun(
4346 "o0_mirror.property('x').value().value()")->Int32Value());
4347 CHECK_EQ(1, CompileRun(
4348 "o0_mirror.property('y').value().value()")->Int32Value());
4349 CHECK_EQ(2, CompileRun(
4350 "o0_mirror.property('z').value().value()")->Int32Value());
4351
4352 // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and
4353 // o2 has the hidden prototype flag. o3 does not have the hidden prototype
4354 // flag so properties on o3 should not be seen on o0 whereas the properties
4355 // from o1 and o2 should still be seen on o0.
4356 // Final prototype chain: o0 -> o1 -> o2 -> o3
4357 // Hidden prototypes: ^^ ^^
4358 o0->Set(v8::String::New("__proto__"), o3);
4359 CHECK_EQ(3, CompileRun(
4360 "o0_mirror.propertyNames().length")->Int32Value());
4361 CHECK_EQ(1, CompileRun(
4362 "o3_mirror.propertyNames().length")->Int32Value());
4363 CHECK_EQ(0, CompileRun(
4364 "o0_mirror.property('x').value().value()")->Int32Value());
4365 CHECK_EQ(1, CompileRun(
4366 "o0_mirror.property('y').value().value()")->Int32Value());
4367 CHECK_EQ(2, CompileRun(
4368 "o0_mirror.property('z').value().value()")->Int32Value());
4369 CHECK(CompileRun("o0_mirror.property('u').isUndefined()")->BooleanValue());
4370
4371 // The prototype (__proto__) for o0 should be o3 as o1 and o2 are hidden.
4372 CHECK(CompileRun("o0_mirror.protoObject() == o3_mirror")->BooleanValue());
4373}
4374
4375
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004376static v8::Handle<v8::Value> ProtperyXNativeGetter(
4377 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
4378 return v8::Integer::New(10);
4379}
4380
4381
4382TEST(NativeGetterPropertyMirror) {
4383 // Create a V8 environment with debug access.
4384 v8::HandleScope scope;
4385 DebugLocalContext env;
4386 env.ExposeDebug();
4387
4388 v8::Handle<v8::String> name = v8::String::New("x");
4389 // Create object with named accessor.
4390 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New();
4391 named->SetAccessor(name, &ProtperyXNativeGetter, NULL,
4392 v8::Handle<v8::Value>(), v8::DEFAULT, v8::None);
4393
4394 // Create object with named property getter.
4395 env->Global()->Set(v8::String::New("instance"), named->NewInstance());
4396 CHECK_EQ(10, CompileRun("instance.x")->Int32Value());
4397
4398 // Get mirror for the object with property getter.
4399 CompileRun("instance_mirror = debug.MakeMirror(instance);");
4400 CHECK(CompileRun(
4401 "instance_mirror instanceof debug.ObjectMirror")->BooleanValue());
4402
4403 CompileRun("named_names = instance_mirror.propertyNames();");
4404 CHECK_EQ(1, CompileRun("named_names.length")->Int32Value());
4405 CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue());
4406 CHECK(CompileRun(
4407 "instance_mirror.property('x').value().isNumber()")->BooleanValue());
4408 CHECK(CompileRun(
4409 "instance_mirror.property('x').value().value() == 10")->BooleanValue());
4410}
4411
4412
4413static v8::Handle<v8::Value> ProtperyXNativeGetterThrowingError(
4414 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
4415 return CompileRun("throw new Error('Error message');");
4416}
4417
4418
4419TEST(NativeGetterThrowingErrorPropertyMirror) {
4420 // Create a V8 environment with debug access.
4421 v8::HandleScope scope;
4422 DebugLocalContext env;
4423 env.ExposeDebug();
4424
4425 v8::Handle<v8::String> name = v8::String::New("x");
4426 // Create object with named accessor.
4427 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New();
4428 named->SetAccessor(name, &ProtperyXNativeGetterThrowingError, NULL,
4429 v8::Handle<v8::Value>(), v8::DEFAULT, v8::None);
4430
4431 // Create object with named property getter.
4432 env->Global()->Set(v8::String::New("instance"), named->NewInstance());
4433
4434 // Get mirror for the object with property getter.
4435 CompileRun("instance_mirror = debug.MakeMirror(instance);");
4436 CHECK(CompileRun(
4437 "instance_mirror instanceof debug.ObjectMirror")->BooleanValue());
4438 CompileRun("named_names = instance_mirror.propertyNames();");
4439 CHECK_EQ(1, CompileRun("named_names.length")->Int32Value());
4440 CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue());
4441 CHECK(CompileRun(
4442 "instance_mirror.property('x').value().isError()")->BooleanValue());
4443
4444 // Check that the message is that passed to the Error constructor.
4445 CHECK(CompileRun(
4446 "instance_mirror.property('x').value().message() == 'Error message'")->
4447 BooleanValue());
4448}
4449
4450
ager@chromium.orgc730f772009-11-11 10:11:16 +00004451// Test that hidden properties object is not returned as an unnamed property
4452// among regular properties.
4453// See http://crbug.com/26491
4454TEST(NoHiddenProperties) {
4455 // Create a V8 environment with debug access.
4456 v8::HandleScope scope;
4457 DebugLocalContext env;
4458 env.ExposeDebug();
4459
4460 // Create an object in the global scope.
4461 const char* source = "var obj = {a: 1};";
4462 v8::Script::Compile(v8::String::New(source))->Run();
4463 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(
4464 env->Global()->Get(v8::String::New("obj")));
4465 // Set a hidden property on the object.
4466 obj->SetHiddenValue(v8::String::New("v8::test-debug::a"),
4467 v8::Int32::New(11));
4468
4469 // Get mirror for the object with property getter.
4470 CompileRun("var obj_mirror = debug.MakeMirror(obj);");
4471 CHECK(CompileRun(
4472 "obj_mirror instanceof debug.ObjectMirror")->BooleanValue());
4473 CompileRun("var named_names = obj_mirror.propertyNames();");
4474 // There should be exactly one property. But there is also an unnamed
4475 // property whose value is hidden properties dictionary. The latter
4476 // property should not be in the list of reguar properties.
4477 CHECK_EQ(1, CompileRun("named_names.length")->Int32Value());
4478 CHECK(CompileRun("named_names[0] == 'a'")->BooleanValue());
4479 CHECK(CompileRun(
4480 "obj_mirror.property('a').value().value() == 1")->BooleanValue());
4481
4482 // Object created by t0 will become hidden prototype of object 'obj'.
4483 v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New();
4484 t0->InstanceTemplate()->Set(v8::String::New("b"), v8::Number::New(2));
4485 t0->SetHiddenPrototype(true);
4486 v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New();
4487 t1->InstanceTemplate()->Set(v8::String::New("c"), v8::Number::New(3));
4488
4489 // Create proto objects, add hidden properties to them and set them on
4490 // the global object.
4491 v8::Handle<v8::Object> protoObj = t0->GetFunction()->NewInstance();
4492 protoObj->SetHiddenValue(v8::String::New("v8::test-debug::b"),
4493 v8::Int32::New(12));
4494 env->Global()->Set(v8::String::New("protoObj"), protoObj);
4495 v8::Handle<v8::Object> grandProtoObj = t1->GetFunction()->NewInstance();
4496 grandProtoObj->SetHiddenValue(v8::String::New("v8::test-debug::c"),
4497 v8::Int32::New(13));
4498 env->Global()->Set(v8::String::New("grandProtoObj"), grandProtoObj);
4499
4500 // Setting prototypes: obj->protoObj->grandProtoObj
4501 protoObj->Set(v8::String::New("__proto__"), grandProtoObj);
4502 obj->Set(v8::String::New("__proto__"), protoObj);
4503
4504 // Get mirror for the object with property getter.
4505 CompileRun("var obj_mirror = debug.MakeMirror(obj);");
4506 CHECK(CompileRun(
4507 "obj_mirror instanceof debug.ObjectMirror")->BooleanValue());
4508 CompileRun("var named_names = obj_mirror.propertyNames();");
4509 // There should be exactly two properties - one from the object itself and
4510 // another from its hidden prototype.
4511 CHECK_EQ(2, CompileRun("named_names.length")->Int32Value());
4512 CHECK(CompileRun("named_names.sort(); named_names[0] == 'a' &&"
4513 "named_names[1] == 'b'")->BooleanValue());
4514 CHECK(CompileRun(
4515 "obj_mirror.property('a').value().value() == 1")->BooleanValue());
4516 CHECK(CompileRun(
4517 "obj_mirror.property('b').value().value() == 2")->BooleanValue());
4518}
4519
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004520
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004521// Multithreaded tests of JSON debugger protocol
4522
4523// Support classes
4524
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004525// Provides synchronization between k threads, where k is an input to the
4526// constructor. The Wait() call blocks a thread until it is called for the
4527// k'th time, then all calls return. Each ThreadBarrier object can only
4528// be used once.
4529class ThreadBarrier {
4530 public:
4531 explicit ThreadBarrier(int num_threads);
4532 ~ThreadBarrier();
4533 void Wait();
4534 private:
4535 int num_threads_;
4536 int num_blocked_;
4537 v8::internal::Mutex* lock_;
4538 v8::internal::Semaphore* sem_;
4539 bool invalid_;
4540};
4541
4542ThreadBarrier::ThreadBarrier(int num_threads)
4543 : num_threads_(num_threads), num_blocked_(0) {
4544 lock_ = OS::CreateMutex();
4545 sem_ = OS::CreateSemaphore(0);
4546 invalid_ = false; // A barrier may only be used once. Then it is invalid.
4547}
4548
4549// Do not call, due to race condition with Wait().
4550// Could be resolved with Pthread condition variables.
4551ThreadBarrier::~ThreadBarrier() {
4552 lock_->Lock();
4553 delete lock_;
4554 delete sem_;
4555}
4556
4557void ThreadBarrier::Wait() {
4558 lock_->Lock();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004559 CHECK(!invalid_);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004560 if (num_blocked_ == num_threads_ - 1) {
4561 // Signal and unblock all waiting threads.
4562 for (int i = 0; i < num_threads_ - 1; ++i) {
4563 sem_->Signal();
4564 }
4565 invalid_ = true;
4566 printf("BARRIER\n\n");
4567 fflush(stdout);
4568 lock_->Unlock();
4569 } else { // Wait for the semaphore.
4570 ++num_blocked_;
4571 lock_->Unlock(); // Potential race condition with destructor because
4572 sem_->Wait(); // these two lines are not atomic.
4573 }
4574}
4575
4576// A set containing enough barriers and semaphores for any of the tests.
4577class Barriers {
4578 public:
4579 Barriers();
4580 void Initialize();
4581 ThreadBarrier barrier_1;
4582 ThreadBarrier barrier_2;
4583 ThreadBarrier barrier_3;
4584 ThreadBarrier barrier_4;
4585 ThreadBarrier barrier_5;
4586 v8::internal::Semaphore* semaphore_1;
4587 v8::internal::Semaphore* semaphore_2;
4588};
4589
4590Barriers::Barriers() : barrier_1(2), barrier_2(2),
4591 barrier_3(2), barrier_4(2), barrier_5(2) {}
4592
4593void Barriers::Initialize() {
4594 semaphore_1 = OS::CreateSemaphore(0);
4595 semaphore_2 = OS::CreateSemaphore(0);
4596}
4597
4598
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004599// We match parts of the message to decide if it is a break message.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004600bool IsBreakEventMessage(char *message) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004601 const char* type_event = "\"type\":\"event\"";
4602 const char* event_break = "\"event\":\"break\"";
4603 // Does the message contain both type:event and event:break?
4604 return strstr(message, type_event) != NULL &&
4605 strstr(message, event_break) != NULL;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004606}
4607
4608
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004609// We match parts of the message to decide if it is a exception message.
4610bool IsExceptionEventMessage(char *message) {
4611 const char* type_event = "\"type\":\"event\"";
4612 const char* event_exception = "\"event\":\"exception\"";
4613 // Does the message contain both type:event and event:exception?
4614 return strstr(message, type_event) != NULL &&
4615 strstr(message, event_exception) != NULL;
4616}
4617
4618
4619// We match the message wether it is an evaluate response message.
4620bool IsEvaluateResponseMessage(char* message) {
4621 const char* type_response = "\"type\":\"response\"";
4622 const char* command_evaluate = "\"command\":\"evaluate\"";
4623 // Does the message contain both type:response and command:evaluate?
4624 return strstr(message, type_response) != NULL &&
4625 strstr(message, command_evaluate) != NULL;
4626}
4627
4628
ager@chromium.org5c838252010-02-19 08:53:10 +00004629static int StringToInt(const char* s) {
4630 return atoi(s); // NOLINT
4631}
4632
4633
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004634// We match parts of the message to get evaluate result int value.
4635int GetEvaluateIntResult(char *message) {
4636 const char* value = "\"value\":";
4637 char* pos = strstr(message, value);
4638 if (pos == NULL) {
4639 return -1;
4640 }
4641 int res = -1;
ager@chromium.org5c838252010-02-19 08:53:10 +00004642 res = StringToInt(pos + strlen(value));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004643 return res;
4644}
4645
4646
4647// We match parts of the message to get hit breakpoint id.
4648int GetBreakpointIdFromBreakEventMessage(char *message) {
4649 const char* breakpoints = "\"breakpoints\":[";
4650 char* pos = strstr(message, breakpoints);
4651 if (pos == NULL) {
4652 return -1;
4653 }
4654 int res = -1;
ager@chromium.org5c838252010-02-19 08:53:10 +00004655 res = StringToInt(pos + strlen(breakpoints));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004656 return res;
4657}
4658
4659
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004660// We match parts of the message to get total frames number.
4661int GetTotalFramesInt(char *message) {
4662 const char* prefix = "\"totalFrames\":";
4663 char* pos = strstr(message, prefix);
4664 if (pos == NULL) {
4665 return -1;
4666 }
4667 pos += strlen(prefix);
ager@chromium.org5c838252010-02-19 08:53:10 +00004668 int res = StringToInt(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004669 return res;
4670}
4671
4672
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004673// We match parts of the message to get source line.
4674int GetSourceLineFromBreakEventMessage(char *message) {
4675 const char* source_line = "\"sourceLine\":";
4676 char* pos = strstr(message, source_line);
4677 if (pos == NULL) {
4678 return -1;
4679 }
4680 int res = -1;
4681 res = StringToInt(pos + strlen(source_line));
4682 return res;
4683}
4684
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004685/* Test MessageQueues */
4686/* Tests the message queues that hold debugger commands and
4687 * response messages to the debugger. Fills queues and makes
4688 * them grow.
4689 */
4690Barriers message_queue_barriers;
4691
4692// This is the debugger thread, that executes no v8 calls except
4693// placing JSON debugger commands in the queue.
4694class MessageQueueDebuggerThread : public v8::internal::Thread {
4695 public:
4696 void Run();
4697};
4698
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004699static void MessageHandler(const uint16_t* message, int length,
4700 v8::Debug::ClientData* client_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004701 static char print_buffer[1000];
4702 Utf16ToAscii(message, length, print_buffer);
4703 if (IsBreakEventMessage(print_buffer)) {
4704 // Lets test script wait until break occurs to send commands.
4705 // Signals when a break is reported.
4706 message_queue_barriers.semaphore_2->Signal();
4707 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00004708
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004709 // Allow message handler to block on a semaphore, to test queueing of
4710 // messages while blocked.
4711 message_queue_barriers.semaphore_1->Wait();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004712}
4713
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004714void MessageQueueDebuggerThread::Run() {
4715 const int kBufferSize = 1000;
4716 uint16_t buffer_1[kBufferSize];
4717 uint16_t buffer_2[kBufferSize];
4718 const char* command_1 =
4719 "{\"seq\":117,"
4720 "\"type\":\"request\","
4721 "\"command\":\"evaluate\","
4722 "\"arguments\":{\"expression\":\"1+2\"}}";
4723 const char* command_2 =
4724 "{\"seq\":118,"
4725 "\"type\":\"request\","
4726 "\"command\":\"evaluate\","
4727 "\"arguments\":{\"expression\":\"1+a\"}}";
4728 const char* command_3 =
4729 "{\"seq\":119,"
4730 "\"type\":\"request\","
4731 "\"command\":\"evaluate\","
4732 "\"arguments\":{\"expression\":\"c.d * b\"}}";
4733 const char* command_continue =
4734 "{\"seq\":106,"
4735 "\"type\":\"request\","
4736 "\"command\":\"continue\"}";
4737 const char* command_single_step =
4738 "{\"seq\":107,"
4739 "\"type\":\"request\","
4740 "\"command\":\"continue\","
4741 "\"arguments\":{\"stepaction\":\"next\"}}";
4742
4743 /* Interleaved sequence of actions by the two threads:*/
4744 // Main thread compiles and runs source_1
ager@chromium.org5ec48922009-05-05 07:25:34 +00004745 message_queue_barriers.semaphore_1->Signal();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004746 message_queue_barriers.barrier_1.Wait();
4747 // Post 6 commands, filling the command queue and making it expand.
4748 // These calls return immediately, but the commands stay on the queue
4749 // until the execution of source_2.
4750 // Note: AsciiToUtf16 executes before SendCommand, so command is copied
4751 // to buffer before buffer is sent to SendCommand.
4752 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1));
4753 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2));
4754 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
4755 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
4756 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004757 message_queue_barriers.barrier_2.Wait();
4758 // Main thread compiles and runs source_2.
ager@chromium.org5ec48922009-05-05 07:25:34 +00004759 // Queued commands are executed at the start of compilation of source_2(
4760 // beforeCompile event).
4761 // Free the message handler to process all the messages from the queue. 7
4762 // messages are expected: 2 afterCompile events and 5 responses.
4763 // All the commands added so far will fail to execute as long as call stack
4764 // is empty on beforeCompile event.
4765 for (int i = 0; i < 6 ; ++i) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004766 message_queue_barriers.semaphore_1->Signal();
4767 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00004768 message_queue_barriers.barrier_3.Wait();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004769 // Main thread compiles and runs source_3.
ager@chromium.org5ec48922009-05-05 07:25:34 +00004770 // Don't stop in the afterCompile handler.
4771 message_queue_barriers.semaphore_1->Signal();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004772 // source_3 includes a debugger statement, which causes a break event.
4773 // Wait on break event from hitting "debugger" statement
4774 message_queue_barriers.semaphore_2->Wait();
4775 // These should execute after the "debugger" statement in source_2
ager@chromium.org5ec48922009-05-05 07:25:34 +00004776 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1));
4777 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2));
4778 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004779 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_single_step, buffer_2));
ager@chromium.org5ec48922009-05-05 07:25:34 +00004780 // Run after 2 break events, 4 responses.
4781 for (int i = 0; i < 6 ; ++i) {
4782 message_queue_barriers.semaphore_1->Signal();
4783 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004784 // Wait on break event after a single step executes.
4785 message_queue_barriers.semaphore_2->Wait();
4786 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_2, buffer_1));
4787 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_continue, buffer_2));
ager@chromium.org5ec48922009-05-05 07:25:34 +00004788 // Run after 2 responses.
4789 for (int i = 0; i < 2 ; ++i) {
4790 message_queue_barriers.semaphore_1->Signal();
4791 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004792 // Main thread continues running source_3 to end, waits for this thread.
4793}
4794
4795MessageQueueDebuggerThread message_queue_debugger_thread;
4796
4797// This thread runs the v8 engine.
4798TEST(MessageQueues) {
4799 // Create a V8 environment
4800 v8::HandleScope scope;
4801 DebugLocalContext env;
4802 message_queue_barriers.Initialize();
4803 v8::Debug::SetMessageHandler(MessageHandler);
4804 message_queue_debugger_thread.Start();
4805
4806 const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;";
4807 const char* source_2 = "e = 17;";
4808 const char* source_3 = "a = 4; debugger; a = 5; a = 6; a = 7;";
4809
4810 // See MessageQueueDebuggerThread::Run for interleaved sequence of
4811 // API calls and events in the two threads.
4812 CompileRun(source_1);
4813 message_queue_barriers.barrier_1.Wait();
4814 message_queue_barriers.barrier_2.Wait();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004815 CompileRun(source_2);
4816 message_queue_barriers.barrier_3.Wait();
4817 CompileRun(source_3);
4818 message_queue_debugger_thread.Join();
4819 fflush(stdout);
4820}
4821
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004822
4823class TestClientData : public v8::Debug::ClientData {
4824 public:
4825 TestClientData() {
4826 constructor_call_counter++;
4827 }
4828 virtual ~TestClientData() {
4829 destructor_call_counter++;
4830 }
4831
4832 static void ResetCounters() {
4833 constructor_call_counter = 0;
4834 destructor_call_counter = 0;
4835 }
4836
4837 static int constructor_call_counter;
4838 static int destructor_call_counter;
4839};
4840
4841int TestClientData::constructor_call_counter = 0;
4842int TestClientData::destructor_call_counter = 0;
4843
4844
4845// Tests that MessageQueue doesn't destroy client data when expands and
4846// does destroy when it dies.
4847TEST(MessageQueueExpandAndDestroy) {
4848 TestClientData::ResetCounters();
4849 { // Create a scope for the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004850 CommandMessageQueue queue(1);
4851 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004852 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004853 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004854 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004855 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004856 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004857 CHECK_EQ(0, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004858 queue.Get().Dispose();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004859 CHECK_EQ(1, TestClientData::destructor_call_counter);
4860 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004861 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004862 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004863 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004864 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004865 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004866 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
4867 new TestClientData()));
4868 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
4869 new TestClientData()));
4870 CHECK_EQ(1, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004871 queue.Get().Dispose();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004872 CHECK_EQ(2, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004873 }
4874 // All the client data should be destroyed when the queue is destroyed.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004875 CHECK_EQ(TestClientData::destructor_call_counter,
4876 TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004877}
4878
4879
4880static int handled_client_data_instances_count = 0;
4881static void MessageHandlerCountingClientData(
ager@chromium.org5ec48922009-05-05 07:25:34 +00004882 const v8::Debug::Message& message) {
4883 if (message.GetClientData() != NULL) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004884 handled_client_data_instances_count++;
4885 }
4886}
4887
4888
4889// Tests that all client data passed to the debugger are sent to the handler.
4890TEST(SendClientDataToHandler) {
4891 // Create a V8 environment
4892 v8::HandleScope scope;
4893 DebugLocalContext env;
4894 TestClientData::ResetCounters();
4895 handled_client_data_instances_count = 0;
ager@chromium.org5ec48922009-05-05 07:25:34 +00004896 v8::Debug::SetMessageHandler2(MessageHandlerCountingClientData);
4897 const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;";
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004898 const int kBufferSize = 1000;
4899 uint16_t buffer[kBufferSize];
4900 const char* command_1 =
4901 "{\"seq\":117,"
4902 "\"type\":\"request\","
4903 "\"command\":\"evaluate\","
4904 "\"arguments\":{\"expression\":\"1+2\"}}";
4905 const char* command_2 =
4906 "{\"seq\":118,"
4907 "\"type\":\"request\","
4908 "\"command\":\"evaluate\","
4909 "\"arguments\":{\"expression\":\"1+a\"}}";
4910 const char* command_continue =
4911 "{\"seq\":106,"
4912 "\"type\":\"request\","
4913 "\"command\":\"continue\"}";
4914
4915 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer),
4916 new TestClientData());
4917 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), NULL);
4918 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer),
4919 new TestClientData());
4920 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer),
4921 new TestClientData());
ager@chromium.org5ec48922009-05-05 07:25:34 +00004922 // All the messages will be processed on beforeCompile event.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004923 CompileRun(source_1);
ager@chromium.org5ec48922009-05-05 07:25:34 +00004924 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004925 CHECK_EQ(3, TestClientData::constructor_call_counter);
4926 CHECK_EQ(TestClientData::constructor_call_counter,
4927 handled_client_data_instances_count);
4928 CHECK_EQ(TestClientData::constructor_call_counter,
4929 TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004930}
4931
4932
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004933/* Test ThreadedDebugging */
4934/* This test interrupts a running infinite loop that is
4935 * occupying the v8 thread by a break command from the
4936 * debugger thread. It then changes the value of a
4937 * global object, to make the loop terminate.
4938 */
4939
4940Barriers threaded_debugging_barriers;
4941
4942class V8Thread : public v8::internal::Thread {
4943 public:
4944 void Run();
4945};
4946
4947class DebuggerThread : public v8::internal::Thread {
4948 public:
4949 void Run();
4950};
4951
4952
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00004953static v8::Handle<v8::Value> ThreadedAtBarrier1(const v8::Arguments& args) {
4954 threaded_debugging_barriers.barrier_1.Wait();
4955 return v8::Undefined();
4956}
4957
4958
ager@chromium.org5ec48922009-05-05 07:25:34 +00004959static void ThreadedMessageHandler(const v8::Debug::Message& message) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004960 static char print_buffer[1000];
ager@chromium.org5ec48922009-05-05 07:25:34 +00004961 v8::String::Value json(message.GetJSON());
4962 Utf16ToAscii(*json, json.length(), print_buffer);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004963 if (IsBreakEventMessage(print_buffer)) {
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004964 // Check that we are inside the while loop.
4965 int source_line = GetSourceLineFromBreakEventMessage(print_buffer);
4966 CHECK(8 <= source_line && source_line <= 13);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004967 threaded_debugging_barriers.barrier_2.Wait();
4968 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004969}
4970
4971
4972void V8Thread::Run() {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00004973 const char* source =
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004974 "flag = true;\n"
4975 "function bar( new_value ) {\n"
4976 " flag = new_value;\n"
4977 " return \"Return from bar(\" + new_value + \")\";\n"
4978 "}\n"
4979 "\n"
4980 "function foo() {\n"
4981 " var x = 1;\n"
4982 " while ( flag == true ) {\n"
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00004983 " if ( x == 1 ) {\n"
4984 " ThreadedAtBarrier1();\n"
4985 " }\n"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004986 " x = x + 1;\n"
4987 " }\n"
4988 "}\n"
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00004989 "\n"
4990 "foo();\n";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004991
4992 v8::HandleScope scope;
4993 DebugLocalContext env;
ager@chromium.org5ec48922009-05-05 07:25:34 +00004994 v8::Debug::SetMessageHandler2(&ThreadedMessageHandler);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00004995 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
4996 global_template->Set(v8::String::New("ThreadedAtBarrier1"),
4997 v8::FunctionTemplate::New(ThreadedAtBarrier1));
4998 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template);
4999 v8::Context::Scope context_scope(context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005000
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00005001 CompileRun(source);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005002}
5003
5004void DebuggerThread::Run() {
5005 const int kBufSize = 1000;
5006 uint16_t buffer[kBufSize];
5007
5008 const char* command_1 = "{\"seq\":102,"
5009 "\"type\":\"request\","
5010 "\"command\":\"evaluate\","
5011 "\"arguments\":{\"expression\":\"bar(false)\"}}";
5012 const char* command_2 = "{\"seq\":103,"
5013 "\"type\":\"request\","
5014 "\"command\":\"continue\"}";
5015
5016 threaded_debugging_barriers.barrier_1.Wait();
5017 v8::Debug::DebugBreak();
5018 threaded_debugging_barriers.barrier_2.Wait();
5019 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
5020 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
5021}
5022
5023DebuggerThread debugger_thread;
5024V8Thread v8_thread;
5025
5026TEST(ThreadedDebugging) {
5027 // Create a V8 environment
5028 threaded_debugging_barriers.Initialize();
5029
5030 v8_thread.Start();
5031 debugger_thread.Start();
5032
5033 v8_thread.Join();
5034 debugger_thread.Join();
5035}
5036
5037/* Test RecursiveBreakpoints */
5038/* In this test, the debugger evaluates a function with a breakpoint, after
5039 * hitting a breakpoint in another function. We do this with both values
5040 * of the flag enabling recursive breakpoints, and verify that the second
5041 * breakpoint is hit when enabled, and missed when disabled.
5042 */
5043
5044class BreakpointsV8Thread : public v8::internal::Thread {
5045 public:
5046 void Run();
5047};
5048
5049class BreakpointsDebuggerThread : public v8::internal::Thread {
5050 public:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005051 explicit BreakpointsDebuggerThread(bool global_evaluate)
5052 : global_evaluate_(global_evaluate) {}
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005053 void Run();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005054
5055 private:
5056 bool global_evaluate_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005057};
5058
5059
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005060Barriers* breakpoints_barriers;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005061int break_event_breakpoint_id;
5062int evaluate_int_result;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005063
ager@chromium.org5ec48922009-05-05 07:25:34 +00005064static void BreakpointsMessageHandler(const v8::Debug::Message& message) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005065 static char print_buffer[1000];
ager@chromium.org5ec48922009-05-05 07:25:34 +00005066 v8::String::Value json(message.GetJSON());
5067 Utf16ToAscii(*json, json.length(), print_buffer);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005068
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005069 if (IsBreakEventMessage(print_buffer)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005070 break_event_breakpoint_id =
5071 GetBreakpointIdFromBreakEventMessage(print_buffer);
5072 breakpoints_barriers->semaphore_1->Signal();
5073 } else if (IsEvaluateResponseMessage(print_buffer)) {
5074 evaluate_int_result = GetEvaluateIntResult(print_buffer);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005075 breakpoints_barriers->semaphore_1->Signal();
5076 }
5077}
5078
5079
5080void BreakpointsV8Thread::Run() {
5081 const char* source_1 = "var y_global = 3;\n"
5082 "function cat( new_value ) {\n"
5083 " var x = new_value;\n"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005084 " y_global = y_global + 4;\n"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005085 " x = 3 * x + 1;\n"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005086 " y_global = y_global + 5;\n"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005087 " return x;\n"
5088 "}\n"
5089 "\n"
5090 "function dog() {\n"
5091 " var x = 1;\n"
5092 " x = y_global;"
5093 " var z = 3;"
5094 " x += 100;\n"
5095 " return x;\n"
5096 "}\n"
5097 "\n";
5098 const char* source_2 = "cat(17);\n"
5099 "cat(19);\n";
5100
5101 v8::HandleScope scope;
5102 DebugLocalContext env;
ager@chromium.org5ec48922009-05-05 07:25:34 +00005103 v8::Debug::SetMessageHandler2(&BreakpointsMessageHandler);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005104
5105 CompileRun(source_1);
5106 breakpoints_barriers->barrier_1.Wait();
5107 breakpoints_barriers->barrier_2.Wait();
5108 CompileRun(source_2);
5109}
5110
5111
5112void BreakpointsDebuggerThread::Run() {
5113 const int kBufSize = 1000;
5114 uint16_t buffer[kBufSize];
5115
5116 const char* command_1 = "{\"seq\":101,"
5117 "\"type\":\"request\","
5118 "\"command\":\"setbreakpoint\","
5119 "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}";
5120 const char* command_2 = "{\"seq\":102,"
5121 "\"type\":\"request\","
5122 "\"command\":\"setbreakpoint\","
5123 "\"arguments\":{\"type\":\"function\",\"target\":\"dog\",\"line\":3}}";
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005124 const char* command_3;
5125 if (this->global_evaluate_) {
5126 command_3 = "{\"seq\":103,"
5127 "\"type\":\"request\","
5128 "\"command\":\"evaluate\","
5129 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false,"
5130 "\"global\":true}}";
5131 } else {
5132 command_3 = "{\"seq\":103,"
5133 "\"type\":\"request\","
5134 "\"command\":\"evaluate\","
5135 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false}}";
5136 }
5137 const char* command_4;
5138 if (this->global_evaluate_) {
5139 command_4 = "{\"seq\":104,"
5140 "\"type\":\"request\","
5141 "\"command\":\"evaluate\","
5142 "\"arguments\":{\"expression\":\"100 + 8\",\"disable_break\":true,"
5143 "\"global\":true}}";
5144 } else {
5145 command_4 = "{\"seq\":104,"
5146 "\"type\":\"request\","
5147 "\"command\":\"evaluate\","
5148 "\"arguments\":{\"expression\":\"x + 1\",\"disable_break\":true}}";
5149 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005150 const char* command_5 = "{\"seq\":105,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005151 "\"type\":\"request\","
5152 "\"command\":\"continue\"}";
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005153 const char* command_6 = "{\"seq\":106,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005154 "\"type\":\"request\","
5155 "\"command\":\"continue\"}";
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005156 const char* command_7;
5157 if (this->global_evaluate_) {
5158 command_7 = "{\"seq\":107,"
5159 "\"type\":\"request\","
5160 "\"command\":\"evaluate\","
5161 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true,"
5162 "\"global\":true}}";
5163 } else {
5164 command_7 = "{\"seq\":107,"
5165 "\"type\":\"request\","
5166 "\"command\":\"evaluate\","
5167 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true}}";
5168 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005169 const char* command_8 = "{\"seq\":108,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005170 "\"type\":\"request\","
5171 "\"command\":\"continue\"}";
5172
5173
5174 // v8 thread initializes, runs source_1
5175 breakpoints_barriers->barrier_1.Wait();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005176 // 1:Set breakpoint in cat() (will get id 1).
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005177 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005178 // 2:Set breakpoint in dog() (will get id 2).
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005179 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005180 breakpoints_barriers->barrier_2.Wait();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005181 // V8 thread starts compiling source_2.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005182 // Automatic break happens, to run queued commands
5183 // breakpoints_barriers->semaphore_1->Wait();
5184 // Commands 1 through 3 run, thread continues.
5185 // v8 thread runs source_2 to breakpoint in cat().
5186 // message callback receives break event.
5187 breakpoints_barriers->semaphore_1->Wait();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005188 // Must have hit breakpoint #1.
5189 CHECK_EQ(1, break_event_breakpoint_id);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005190 // 4:Evaluate dog() (which has a breakpoint).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005191 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_3, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005192 // V8 thread hits breakpoint in dog().
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005193 breakpoints_barriers->semaphore_1->Wait(); // wait for break event
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005194 // Must have hit breakpoint #2.
5195 CHECK_EQ(2, break_event_breakpoint_id);
5196 // 5:Evaluate (x + 1).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005197 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_4, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005198 // Evaluate (x + 1) finishes.
5199 breakpoints_barriers->semaphore_1->Wait();
5200 // Must have result 108.
5201 CHECK_EQ(108, evaluate_int_result);
5202 // 6:Continue evaluation of dog().
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005203 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_5, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005204 // Evaluate dog() finishes.
5205 breakpoints_barriers->semaphore_1->Wait();
5206 // Must have result 107.
5207 CHECK_EQ(107, evaluate_int_result);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005208 // 7:Continue evaluation of source_2, finish cat(17), hit breakpoint
5209 // in cat(19).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005210 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_6, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005211 // Message callback gets break event.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005212 breakpoints_barriers->semaphore_1->Wait(); // wait for break event
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005213 // Must have hit breakpoint #1.
5214 CHECK_EQ(1, break_event_breakpoint_id);
5215 // 8: Evaluate dog() with breaks disabled.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005216 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_7, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005217 // Evaluate dog() finishes.
5218 breakpoints_barriers->semaphore_1->Wait();
5219 // Must have result 116.
5220 CHECK_EQ(116, evaluate_int_result);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005221 // 9: Continue evaluation of source2, reach end.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005222 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_8, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005223}
5224
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005225void TestRecursiveBreakpointsGeneric(bool global_evaluate) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005226 i::FLAG_debugger_auto_break = true;
5227
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005228 BreakpointsDebuggerThread breakpoints_debugger_thread(global_evaluate);
5229 BreakpointsV8Thread breakpoints_v8_thread;
5230
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005231 // Create a V8 environment
5232 Barriers stack_allocated_breakpoints_barriers;
5233 stack_allocated_breakpoints_barriers.Initialize();
5234 breakpoints_barriers = &stack_allocated_breakpoints_barriers;
5235
5236 breakpoints_v8_thread.Start();
5237 breakpoints_debugger_thread.Start();
5238
5239 breakpoints_v8_thread.Join();
5240 breakpoints_debugger_thread.Join();
5241}
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005242
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005243TEST(RecursiveBreakpoints) {
5244 TestRecursiveBreakpointsGeneric(false);
5245}
5246
5247TEST(RecursiveBreakpointsGlobal) {
5248 TestRecursiveBreakpointsGeneric(true);
5249}
5250
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005251
5252static void DummyDebugEventListener(v8::DebugEvent event,
5253 v8::Handle<v8::Object> exec_state,
5254 v8::Handle<v8::Object> event_data,
5255 v8::Handle<v8::Value> data) {
5256}
5257
5258
iposva@chromium.org245aa852009-02-10 00:49:54 +00005259TEST(SetDebugEventListenerOnUninitializedVM) {
5260 v8::Debug::SetDebugEventListener(DummyDebugEventListener);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005261}
5262
5263
ager@chromium.org5ec48922009-05-05 07:25:34 +00005264static void DummyMessageHandler(const v8::Debug::Message& message) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005265}
5266
5267
5268TEST(SetMessageHandlerOnUninitializedVM) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00005269 v8::Debug::SetMessageHandler2(DummyMessageHandler);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005270}
5271
5272
5273TEST(DebugBreakOnUninitializedVM) {
5274 v8::Debug::DebugBreak();
5275}
5276
5277
5278TEST(SendCommandToUninitializedVM) {
5279 const char* dummy_command = "{}";
5280 uint16_t dummy_buffer[80];
5281 int dummy_length = AsciiToUtf16(dummy_command, dummy_buffer);
5282 v8::Debug::SendCommand(dummy_buffer, dummy_length);
5283}
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005284
5285
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005286// Source for a JavaScript function which returns the data parameter of a
5287// function called in the context of the debugger. If no data parameter is
5288// passed it throws an exception.
5289static const char* debugger_call_with_data_source =
5290 "function debugger_call_with_data(exec_state, data) {"
5291 " if (data) return data;"
5292 " throw 'No data!'"
5293 "}";
5294v8::Handle<v8::Function> debugger_call_with_data;
5295
5296
5297// Source for a JavaScript function which returns the data parameter of a
5298// function called in the context of the debugger. If no data parameter is
5299// passed it throws an exception.
5300static const char* debugger_call_with_closure_source =
5301 "var x = 3;"
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005302 "(function (exec_state) {"
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005303 " if (exec_state.y) return x - 1;"
5304 " exec_state.y = x;"
5305 " return exec_state.y"
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005306 "})";
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005307v8::Handle<v8::Function> debugger_call_with_closure;
5308
5309// Function to retrieve the number of JavaScript frames by calling a JavaScript
5310// in the debugger.
5311static v8::Handle<v8::Value> CheckFrameCount(const v8::Arguments& args) {
5312 CHECK(v8::Debug::Call(frame_count)->IsNumber());
5313 CHECK_EQ(args[0]->Int32Value(),
5314 v8::Debug::Call(frame_count)->Int32Value());
5315 return v8::Undefined();
5316}
5317
5318
5319// Function to retrieve the source line of the top JavaScript frame by calling a
5320// JavaScript function in the debugger.
5321static v8::Handle<v8::Value> CheckSourceLine(const v8::Arguments& args) {
5322 CHECK(v8::Debug::Call(frame_source_line)->IsNumber());
5323 CHECK_EQ(args[0]->Int32Value(),
5324 v8::Debug::Call(frame_source_line)->Int32Value());
5325 return v8::Undefined();
5326}
5327
5328
5329// Function to test passing an additional parameter to a JavaScript function
5330// called in the debugger. It also tests that functions called in the debugger
5331// can throw exceptions.
5332static v8::Handle<v8::Value> CheckDataParameter(const v8::Arguments& args) {
5333 v8::Handle<v8::String> data = v8::String::New("Test");
5334 CHECK(v8::Debug::Call(debugger_call_with_data, data)->IsString());
5335
5336 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty());
5337 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty());
5338
5339 v8::TryCatch catcher;
5340 v8::Debug::Call(debugger_call_with_data);
5341 CHECK(catcher.HasCaught());
5342 CHECK(catcher.Exception()->IsString());
5343
5344 return v8::Undefined();
5345}
5346
5347
5348// Function to test using a JavaScript with closure in the debugger.
5349static v8::Handle<v8::Value> CheckClosure(const v8::Arguments& args) {
5350 CHECK(v8::Debug::Call(debugger_call_with_closure)->IsNumber());
5351 CHECK_EQ(3, v8::Debug::Call(debugger_call_with_closure)->Int32Value());
5352 return v8::Undefined();
5353}
5354
5355
5356// Test functions called through the debugger.
5357TEST(CallFunctionInDebugger) {
5358 // Create and enter a context with the functions CheckFrameCount,
5359 // CheckSourceLine and CheckDataParameter installed.
5360 v8::HandleScope scope;
5361 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
5362 global_template->Set(v8::String::New("CheckFrameCount"),
5363 v8::FunctionTemplate::New(CheckFrameCount));
5364 global_template->Set(v8::String::New("CheckSourceLine"),
5365 v8::FunctionTemplate::New(CheckSourceLine));
5366 global_template->Set(v8::String::New("CheckDataParameter"),
5367 v8::FunctionTemplate::New(CheckDataParameter));
5368 global_template->Set(v8::String::New("CheckClosure"),
5369 v8::FunctionTemplate::New(CheckClosure));
5370 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template);
5371 v8::Context::Scope context_scope(context);
5372
5373 // Compile a function for checking the number of JavaScript frames.
5374 v8::Script::Compile(v8::String::New(frame_count_source))->Run();
5375 frame_count = v8::Local<v8::Function>::Cast(
5376 context->Global()->Get(v8::String::New("frame_count")));
5377
5378 // Compile a function for returning the source line for the top frame.
5379 v8::Script::Compile(v8::String::New(frame_source_line_source))->Run();
5380 frame_source_line = v8::Local<v8::Function>::Cast(
5381 context->Global()->Get(v8::String::New("frame_source_line")));
5382
5383 // Compile a function returning the data parameter.
5384 v8::Script::Compile(v8::String::New(debugger_call_with_data_source))->Run();
5385 debugger_call_with_data = v8::Local<v8::Function>::Cast(
5386 context->Global()->Get(v8::String::New("debugger_call_with_data")));
5387
5388 // Compile a function capturing closure.
5389 debugger_call_with_closure = v8::Local<v8::Function>::Cast(
5390 v8::Script::Compile(
5391 v8::String::New(debugger_call_with_closure_source))->Run());
5392
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00005393 // Calling a function through the debugger returns 0 frames if there are
5394 // no JavaScript frames.
5395 CHECK_EQ(v8::Integer::New(0), v8::Debug::Call(frame_count));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005396
5397 // Test that the number of frames can be retrieved.
5398 v8::Script::Compile(v8::String::New("CheckFrameCount(1)"))->Run();
5399 v8::Script::Compile(v8::String::New("function f() {"
5400 " CheckFrameCount(2);"
5401 "}; f()"))->Run();
5402
5403 // Test that the source line can be retrieved.
5404 v8::Script::Compile(v8::String::New("CheckSourceLine(0)"))->Run();
5405 v8::Script::Compile(v8::String::New("function f() {\n"
5406 " CheckSourceLine(1)\n"
5407 " CheckSourceLine(2)\n"
5408 " CheckSourceLine(3)\n"
5409 "}; f()"))->Run();
5410
5411 // Test that a parameter can be passed to a function called in the debugger.
5412 v8::Script::Compile(v8::String::New("CheckDataParameter()"))->Run();
5413
5414 // Test that a function with closure can be run in the debugger.
5415 v8::Script::Compile(v8::String::New("CheckClosure()"))->Run();
ager@chromium.org3a6061e2009-03-12 14:24:36 +00005416
5417
5418 // Test that the source line is correct when there is a line offset.
5419 v8::ScriptOrigin origin(v8::String::New("test"),
5420 v8::Integer::New(7));
5421 v8::Script::Compile(v8::String::New("CheckSourceLine(7)"), &origin)->Run();
5422 v8::Script::Compile(v8::String::New("function f() {\n"
5423 " CheckSourceLine(8)\n"
5424 " CheckSourceLine(9)\n"
5425 " CheckSourceLine(10)\n"
5426 "}; f()"), &origin)->Run();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005427}
ager@chromium.org381abbb2009-02-25 13:23:22 +00005428
5429
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005430// Debugger message handler which counts the number of breaks.
5431static void SendContinueCommand();
5432static void MessageHandlerBreakPointHitCount(
5433 const v8::Debug::Message& message) {
5434 if (message.IsEvent() && message.GetEvent() == v8::Break) {
5435 // Count the number of breaks.
5436 break_point_hit_count++;
5437
5438 SendContinueCommand();
5439 }
5440}
5441
5442
ager@chromium.org381abbb2009-02-25 13:23:22 +00005443// Test that clearing the debug event listener actually clears all break points
5444// and related information.
5445TEST(DebuggerUnload) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00005446 DebugLocalContext env;
5447
5448 // Check debugger is unloaded before it is used.
5449 CheckDebuggerUnloaded();
5450
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005451 // Set a debug event listener.
5452 break_point_hit_count = 0;
ager@chromium.org381abbb2009-02-25 13:23:22 +00005453 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
5454 v8::Undefined());
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005455 {
5456 v8::HandleScope scope;
5457 // Create a couple of functions for the test.
5458 v8::Local<v8::Function> foo =
5459 CompileFunction(&env, "function foo(){x=1}", "foo");
5460 v8::Local<v8::Function> bar =
5461 CompileFunction(&env, "function bar(){y=2}", "bar");
ager@chromium.org381abbb2009-02-25 13:23:22 +00005462
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005463 // Set some break points.
5464 SetBreakPoint(foo, 0);
5465 SetBreakPoint(foo, 4);
5466 SetBreakPoint(bar, 0);
5467 SetBreakPoint(bar, 4);
ager@chromium.org381abbb2009-02-25 13:23:22 +00005468
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005469 // Make sure that the break points are there.
5470 break_point_hit_count = 0;
5471 foo->Call(env->Global(), 0, NULL);
5472 CHECK_EQ(2, break_point_hit_count);
5473 bar->Call(env->Global(), 0, NULL);
5474 CHECK_EQ(4, break_point_hit_count);
5475 }
sgjesse@chromium.orgf457d1f2009-06-17 14:18:36 +00005476
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005477 // Remove the debug event listener without clearing breakpoints. Do this
5478 // outside a handle scope.
ager@chromium.org381abbb2009-02-25 13:23:22 +00005479 v8::Debug::SetDebugEventListener(NULL);
5480 CheckDebuggerUnloaded(true);
5481
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005482 // Now set a debug message handler.
ager@chromium.org381abbb2009-02-25 13:23:22 +00005483 break_point_hit_count = 0;
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005484 v8::Debug::SetMessageHandler2(MessageHandlerBreakPointHitCount);
5485 {
5486 v8::HandleScope scope;
ager@chromium.org381abbb2009-02-25 13:23:22 +00005487
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005488 // Get the test functions again.
5489 v8::Local<v8::Function> foo =
5490 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
5491 v8::Local<v8::Function> bar =
5492 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
ager@chromium.org381abbb2009-02-25 13:23:22 +00005493
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005494 foo->Call(env->Global(), 0, NULL);
5495 CHECK_EQ(0, break_point_hit_count);
5496
5497 // Set break points and run again.
5498 SetBreakPoint(foo, 0);
5499 SetBreakPoint(foo, 4);
5500 foo->Call(env->Global(), 0, NULL);
5501 CHECK_EQ(2, break_point_hit_count);
5502 }
5503
5504 // Remove the debug message handler without clearing breakpoints. Do this
5505 // outside a handle scope.
5506 v8::Debug::SetMessageHandler2(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00005507 CheckDebuggerUnloaded(true);
5508}
5509
5510
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005511// Sends continue command to the debugger.
5512static void SendContinueCommand() {
ager@chromium.org71daaf62009-04-01 07:22:49 +00005513 const int kBufferSize = 1000;
5514 uint16_t buffer[kBufferSize];
5515 const char* command_continue =
5516 "{\"seq\":0,"
5517 "\"type\":\"request\","
5518 "\"command\":\"continue\"}";
5519
5520 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer));
5521}
5522
5523
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005524// Debugger message handler which counts the number of times it is called.
5525static int message_handler_hit_count = 0;
5526static void MessageHandlerHitCount(const v8::Debug::Message& message) {
5527 message_handler_hit_count++;
5528
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005529 static char print_buffer[1000];
5530 v8::String::Value json(message.GetJSON());
5531 Utf16ToAscii(*json, json.length(), print_buffer);
5532 if (IsExceptionEventMessage(print_buffer)) {
5533 // Send a continue command for exception events.
5534 SendContinueCommand();
5535 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005536}
5537
5538
ager@chromium.org71daaf62009-04-01 07:22:49 +00005539// Test clearing the debug message handler.
5540TEST(DebuggerClearMessageHandler) {
5541 v8::HandleScope scope;
5542 DebugLocalContext env;
5543
5544 // Check debugger is unloaded before it is used.
5545 CheckDebuggerUnloaded();
5546
5547 // Set a debug message handler.
ager@chromium.org5ec48922009-05-05 07:25:34 +00005548 v8::Debug::SetMessageHandler2(MessageHandlerHitCount);
ager@chromium.org71daaf62009-04-01 07:22:49 +00005549
5550 // Run code to throw a unhandled exception. This should end up in the message
5551 // handler.
5552 CompileRun("throw 1");
5553
5554 // The message handler should be called.
5555 CHECK_GT(message_handler_hit_count, 0);
5556
5557 // Clear debug message handler.
5558 message_handler_hit_count = 0;
5559 v8::Debug::SetMessageHandler(NULL);
5560
5561 // Run code to throw a unhandled exception. This should end up in the message
5562 // handler.
5563 CompileRun("throw 1");
5564
5565 // The message handler should not be called more.
5566 CHECK_EQ(0, message_handler_hit_count);
5567
5568 CheckDebuggerUnloaded(true);
5569}
5570
5571
5572// Debugger message handler which clears the message handler while active.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005573static void MessageHandlerClearingMessageHandler(
ager@chromium.org5ec48922009-05-05 07:25:34 +00005574 const v8::Debug::Message& message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00005575 message_handler_hit_count++;
5576
5577 // Clear debug message handler.
5578 v8::Debug::SetMessageHandler(NULL);
5579}
5580
5581
5582// Test clearing the debug message handler while processing a debug event.
5583TEST(DebuggerClearMessageHandlerWhileActive) {
5584 v8::HandleScope scope;
5585 DebugLocalContext env;
5586
5587 // Check debugger is unloaded before it is used.
5588 CheckDebuggerUnloaded();
5589
5590 // Set a debug message handler.
ager@chromium.org5ec48922009-05-05 07:25:34 +00005591 v8::Debug::SetMessageHandler2(MessageHandlerClearingMessageHandler);
ager@chromium.org71daaf62009-04-01 07:22:49 +00005592
5593 // Run code to throw a unhandled exception. This should end up in the message
5594 // handler.
5595 CompileRun("throw 1");
5596
5597 // The message handler should be called.
5598 CHECK_EQ(1, message_handler_hit_count);
5599
5600 CheckDebuggerUnloaded(true);
5601}
5602
5603
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005604/* Test DebuggerHostDispatch */
5605/* In this test, the debugger waits for a command on a breakpoint
5606 * and is dispatching host commands while in the infinite loop.
5607 */
5608
5609class HostDispatchV8Thread : public v8::internal::Thread {
5610 public:
5611 void Run();
5612};
5613
5614class HostDispatchDebuggerThread : public v8::internal::Thread {
5615 public:
5616 void Run();
5617};
5618
5619Barriers* host_dispatch_barriers;
5620
ager@chromium.org5ec48922009-05-05 07:25:34 +00005621static void HostDispatchMessageHandler(const v8::Debug::Message& message) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005622 static char print_buffer[1000];
ager@chromium.org5ec48922009-05-05 07:25:34 +00005623 v8::String::Value json(message.GetJSON());
5624 Utf16ToAscii(*json, json.length(), print_buffer);
ager@chromium.org381abbb2009-02-25 13:23:22 +00005625}
5626
5627
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005628static void HostDispatchDispatchHandler() {
5629 host_dispatch_barriers->semaphore_1->Signal();
5630}
5631
5632
5633void HostDispatchV8Thread::Run() {
5634 const char* source_1 = "var y_global = 3;\n"
5635 "function cat( new_value ) {\n"
5636 " var x = new_value;\n"
5637 " y_global = 4;\n"
5638 " x = 3 * x + 1;\n"
5639 " y_global = 5;\n"
5640 " return x;\n"
5641 "}\n"
5642 "\n";
5643 const char* source_2 = "cat(17);\n";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005644
ager@chromium.org381abbb2009-02-25 13:23:22 +00005645 v8::HandleScope scope;
5646 DebugLocalContext env;
5647
ager@chromium.org381abbb2009-02-25 13:23:22 +00005648 // Setup message and host dispatch handlers.
ager@chromium.org5ec48922009-05-05 07:25:34 +00005649 v8::Debug::SetMessageHandler2(HostDispatchMessageHandler);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005650 v8::Debug::SetHostDispatchHandler(HostDispatchDispatchHandler, 10 /* ms */);
ager@chromium.org381abbb2009-02-25 13:23:22 +00005651
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005652 CompileRun(source_1);
5653 host_dispatch_barriers->barrier_1.Wait();
5654 host_dispatch_barriers->barrier_2.Wait();
5655 CompileRun(source_2);
5656}
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00005657
ager@chromium.org381abbb2009-02-25 13:23:22 +00005658
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005659void HostDispatchDebuggerThread::Run() {
5660 const int kBufSize = 1000;
5661 uint16_t buffer[kBufSize];
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00005662
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005663 const char* command_1 = "{\"seq\":101,"
5664 "\"type\":\"request\","
5665 "\"command\":\"setbreakpoint\","
5666 "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}";
5667 const char* command_2 = "{\"seq\":102,"
5668 "\"type\":\"request\","
5669 "\"command\":\"continue\"}";
5670
5671 // v8 thread initializes, runs source_1
5672 host_dispatch_barriers->barrier_1.Wait();
5673 // 1: Set breakpoint in cat().
5674 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
5675
5676 host_dispatch_barriers->barrier_2.Wait();
5677 // v8 thread starts compiling source_2.
5678 // Break happens, to run queued commands and host dispatches.
5679 // Wait for host dispatch to be processed.
5680 host_dispatch_barriers->semaphore_1->Wait();
5681 // 2: Continue evaluation
5682 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
5683}
5684
5685HostDispatchDebuggerThread host_dispatch_debugger_thread;
5686HostDispatchV8Thread host_dispatch_v8_thread;
5687
5688
5689TEST(DebuggerHostDispatch) {
5690 i::FLAG_debugger_auto_break = true;
5691
5692 // Create a V8 environment
5693 Barriers stack_allocated_host_dispatch_barriers;
5694 stack_allocated_host_dispatch_barriers.Initialize();
5695 host_dispatch_barriers = &stack_allocated_host_dispatch_barriers;
5696
5697 host_dispatch_v8_thread.Start();
5698 host_dispatch_debugger_thread.Start();
5699
5700 host_dispatch_v8_thread.Join();
5701 host_dispatch_debugger_thread.Join();
ager@chromium.org381abbb2009-02-25 13:23:22 +00005702}
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005703
5704
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005705/* Test DebugMessageDispatch */
5706/* In this test, the V8 thread waits for a message from the debug thread.
5707 * The DebugMessageDispatchHandler is executed from the debugger thread
5708 * which signals the V8 thread to wake up.
5709 */
5710
5711class DebugMessageDispatchV8Thread : public v8::internal::Thread {
5712 public:
5713 void Run();
5714};
5715
5716class DebugMessageDispatchDebuggerThread : public v8::internal::Thread {
5717 public:
5718 void Run();
5719};
5720
5721Barriers* debug_message_dispatch_barriers;
5722
5723
5724static void DebugMessageHandler() {
5725 debug_message_dispatch_barriers->semaphore_1->Signal();
5726}
5727
5728
5729void DebugMessageDispatchV8Thread::Run() {
5730 v8::HandleScope scope;
5731 DebugLocalContext env;
5732
5733 // Setup debug message dispatch handler.
5734 v8::Debug::SetDebugMessageDispatchHandler(DebugMessageHandler);
5735
5736 CompileRun("var y = 1 + 2;\n");
5737 debug_message_dispatch_barriers->barrier_1.Wait();
5738 debug_message_dispatch_barriers->semaphore_1->Wait();
5739 debug_message_dispatch_barriers->barrier_2.Wait();
5740}
5741
5742
5743void DebugMessageDispatchDebuggerThread::Run() {
5744 debug_message_dispatch_barriers->barrier_1.Wait();
5745 SendContinueCommand();
5746 debug_message_dispatch_barriers->barrier_2.Wait();
5747}
5748
5749DebugMessageDispatchDebuggerThread debug_message_dispatch_debugger_thread;
5750DebugMessageDispatchV8Thread debug_message_dispatch_v8_thread;
5751
5752
5753TEST(DebuggerDebugMessageDispatch) {
5754 i::FLAG_debugger_auto_break = true;
5755
5756 // Create a V8 environment
5757 Barriers stack_allocated_debug_message_dispatch_barriers;
5758 stack_allocated_debug_message_dispatch_barriers.Initialize();
5759 debug_message_dispatch_barriers =
5760 &stack_allocated_debug_message_dispatch_barriers;
5761
5762 debug_message_dispatch_v8_thread.Start();
5763 debug_message_dispatch_debugger_thread.Start();
5764
5765 debug_message_dispatch_v8_thread.Join();
5766 debug_message_dispatch_debugger_thread.Join();
5767}
5768
5769
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005770TEST(DebuggerAgent) {
ager@chromium.orga1645e22009-09-09 19:27:10 +00005771 // Make sure these ports is not used by other tests to allow tests to run in
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005772 // parallel.
ager@chromium.orga1645e22009-09-09 19:27:10 +00005773 const int kPort1 = 5858;
5774 const int kPort2 = 5857;
5775 const int kPort3 = 5856;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005776
ager@chromium.orga1645e22009-09-09 19:27:10 +00005777 // Make a string with the port2 number.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005778 const int kPortBufferLen = 6;
ager@chromium.orga1645e22009-09-09 19:27:10 +00005779 char port2_str[kPortBufferLen];
5780 OS::SNPrintF(i::Vector<char>(port2_str, kPortBufferLen), "%d", kPort2);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005781
5782 bool ok;
5783
5784 // Initialize the socket library.
5785 i::Socket::Setup();
5786
5787 // Test starting and stopping the agent without any client connection.
ager@chromium.orga1645e22009-09-09 19:27:10 +00005788 i::Debugger::StartAgent("test", kPort1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005789 i::Debugger::StopAgent();
5790
5791 // Test starting the agent, connecting a client and shutting down the agent
5792 // with the client connected.
ager@chromium.orga1645e22009-09-09 19:27:10 +00005793 ok = i::Debugger::StartAgent("test", kPort2);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005794 CHECK(ok);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005795 i::Debugger::WaitForAgent();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005796 i::Socket* client = i::OS::CreateSocket();
ager@chromium.orga1645e22009-09-09 19:27:10 +00005797 ok = client->Connect("localhost", port2_str);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005798 CHECK(ok);
5799 i::Debugger::StopAgent();
5800 delete client;
5801
5802 // Test starting and stopping the agent with the required port already
5803 // occoupied.
5804 i::Socket* server = i::OS::CreateSocket();
ager@chromium.orga1645e22009-09-09 19:27:10 +00005805 server->Bind(kPort3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005806
ager@chromium.orga1645e22009-09-09 19:27:10 +00005807 i::Debugger::StartAgent("test", kPort3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005808 i::Debugger::StopAgent();
5809
5810 delete server;
5811}
5812
5813
5814class DebuggerAgentProtocolServerThread : public i::Thread {
5815 public:
5816 explicit DebuggerAgentProtocolServerThread(int port)
5817 : port_(port), server_(NULL), client_(NULL),
5818 listening_(OS::CreateSemaphore(0)) {
5819 }
5820 ~DebuggerAgentProtocolServerThread() {
5821 // Close both sockets.
5822 delete client_;
5823 delete server_;
5824 delete listening_;
5825 }
5826
5827 void Run();
5828 void WaitForListening() { listening_->Wait(); }
5829 char* body() { return *body_; }
5830
5831 private:
5832 int port_;
5833 i::SmartPointer<char> body_;
5834 i::Socket* server_; // Server socket used for bind/accept.
5835 i::Socket* client_; // Single client connection used by the test.
5836 i::Semaphore* listening_; // Signalled when the server is in listen mode.
5837};
5838
5839
5840void DebuggerAgentProtocolServerThread::Run() {
5841 bool ok;
5842
5843 // Create the server socket and bind it to the requested port.
5844 server_ = i::OS::CreateSocket();
5845 CHECK(server_ != NULL);
5846 ok = server_->Bind(port_);
5847 CHECK(ok);
5848
5849 // Listen for new connections.
5850 ok = server_->Listen(1);
5851 CHECK(ok);
5852 listening_->Signal();
5853
5854 // Accept a connection.
5855 client_ = server_->Accept();
5856 CHECK(client_ != NULL);
5857
5858 // Receive a debugger agent protocol message.
5859 i::DebuggerAgentUtil::ReceiveMessage(client_);
5860}
5861
5862
5863TEST(DebuggerAgentProtocolOverflowHeader) {
5864 // Make sure this port is not used by other tests to allow tests to run in
5865 // parallel.
5866 const int kPort = 5860;
5867 static const char* kLocalhost = "localhost";
5868
5869 // Make a string with the port number.
5870 const int kPortBufferLen = 6;
5871 char port_str[kPortBufferLen];
5872 OS::SNPrintF(i::Vector<char>(port_str, kPortBufferLen), "%d", kPort);
5873
5874 // Initialize the socket library.
5875 i::Socket::Setup();
5876
5877 // Create a socket server to receive a debugger agent message.
5878 DebuggerAgentProtocolServerThread* server =
5879 new DebuggerAgentProtocolServerThread(kPort);
5880 server->Start();
5881 server->WaitForListening();
5882
5883 // Connect.
5884 i::Socket* client = i::OS::CreateSocket();
5885 CHECK(client != NULL);
5886 bool ok = client->Connect(kLocalhost, port_str);
5887 CHECK(ok);
5888
5889 // Send headers which overflow the receive buffer.
5890 static const int kBufferSize = 1000;
5891 char buffer[kBufferSize];
5892
5893 // Long key and short value: XXXX....XXXX:0\r\n.
5894 for (int i = 0; i < kBufferSize - 4; i++) {
5895 buffer[i] = 'X';
5896 }
5897 buffer[kBufferSize - 4] = ':';
5898 buffer[kBufferSize - 3] = '0';
5899 buffer[kBufferSize - 2] = '\r';
5900 buffer[kBufferSize - 1] = '\n';
5901 client->Send(buffer, kBufferSize);
5902
5903 // Short key and long value: X:XXXX....XXXX\r\n.
5904 buffer[0] = 'X';
5905 buffer[1] = ':';
5906 for (int i = 2; i < kBufferSize - 2; i++) {
5907 buffer[i] = 'X';
5908 }
5909 buffer[kBufferSize - 2] = '\r';
5910 buffer[kBufferSize - 1] = '\n';
5911 client->Send(buffer, kBufferSize);
5912
5913 // Add empty body to request.
5914 const char* content_length_zero_header = "Content-Length:0\r\n";
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005915 client->Send(content_length_zero_header,
5916 StrLength(content_length_zero_header));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005917 client->Send("\r\n", 2);
5918
5919 // Wait until data is received.
5920 server->Join();
5921
5922 // Check for empty body.
5923 CHECK(server->body() == NULL);
5924
5925 // Close the client before the server to avoid TIME_WAIT issues.
5926 client->Shutdown();
5927 delete client;
5928 delete server;
5929}
ager@chromium.org41826e72009-03-30 13:30:57 +00005930
5931
5932// Test for issue http://code.google.com/p/v8/issues/detail?id=289.
5933// Make sure that DebugGetLoadedScripts doesn't return scripts
5934// with disposed external source.
5935class EmptyExternalStringResource : public v8::String::ExternalStringResource {
5936 public:
5937 EmptyExternalStringResource() { empty_[0] = 0; }
5938 virtual ~EmptyExternalStringResource() {}
5939 virtual size_t length() const { return empty_.length(); }
5940 virtual const uint16_t* data() const { return empty_.start(); }
5941 private:
5942 ::v8::internal::EmbeddedVector<uint16_t, 1> empty_;
5943};
5944
5945
5946TEST(DebugGetLoadedScripts) {
5947 v8::HandleScope scope;
5948 DebugLocalContext env;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005949 env.ExposeDebug();
5950
ager@chromium.org41826e72009-03-30 13:30:57 +00005951 EmptyExternalStringResource source_ext_str;
5952 v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str);
5953 v8::Handle<v8::Script> evil_script = v8::Script::Compile(source);
5954 Handle<i::ExternalTwoByteString> i_source(
5955 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
5956 // This situation can happen if source was an external string disposed
5957 // by its owner.
5958 i_source->set_resource(0);
5959
5960 bool allow_natives_syntax = i::FLAG_allow_natives_syntax;
5961 i::FLAG_allow_natives_syntax = true;
5962 CompileRun(
5963 "var scripts = %DebugGetLoadedScripts();"
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005964 "var count = scripts.length;"
5965 "for (var i = 0; i < count; ++i) {"
5966 " scripts[i].line_ends;"
ager@chromium.org41826e72009-03-30 13:30:57 +00005967 "}");
5968 // Must not crash while accessing line_ends.
5969 i::FLAG_allow_natives_syntax = allow_natives_syntax;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005970
5971 // Some scripts are retrieved - at least the number of native scripts.
5972 CHECK_GT((*env)->Global()->Get(v8::String::New("count"))->Int32Value(), 8);
ager@chromium.org41826e72009-03-30 13:30:57 +00005973}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005974
5975
5976// Test script break points set on lines.
5977TEST(ScriptNameAndData) {
5978 v8::HandleScope scope;
5979 DebugLocalContext env;
5980 env.ExposeDebug();
5981
5982 // Create functions for retrieving script name and data for the function on
5983 // the top frame when hitting a break point.
5984 frame_script_name = CompileFunction(&env,
5985 frame_script_name_source,
5986 "frame_script_name");
5987 frame_script_data = CompileFunction(&env,
5988 frame_script_data_source,
5989 "frame_script_data");
ager@chromium.org5c838252010-02-19 08:53:10 +00005990 compiled_script_data = CompileFunction(&env,
5991 compiled_script_data_source,
5992 "compiled_script_data");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005993
5994 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
5995 v8::Undefined());
5996
5997 // Test function source.
5998 v8::Local<v8::String> script = v8::String::New(
5999 "function f() {\n"
6000 " debugger;\n"
6001 "}\n");
6002
6003 v8::ScriptOrigin origin1 = v8::ScriptOrigin(v8::String::New("name"));
6004 v8::Handle<v8::Script> script1 = v8::Script::Compile(script, &origin1);
6005 script1->SetData(v8::String::New("data"));
6006 script1->Run();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006007 v8::Local<v8::Function> f;
6008 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6009
6010 f->Call(env->Global(), 0, NULL);
6011 CHECK_EQ(1, break_point_hit_count);
6012 CHECK_EQ("name", last_script_name_hit);
6013 CHECK_EQ("data", last_script_data_hit);
6014
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006015 // Compile the same script again without setting data. As the compilation
6016 // cache is disabled when debugging expect the data to be missing.
6017 v8::Script::Compile(script, &origin1)->Run();
6018 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6019 f->Call(env->Global(), 0, NULL);
6020 CHECK_EQ(2, break_point_hit_count);
6021 CHECK_EQ("name", last_script_name_hit);
6022 CHECK_EQ("", last_script_data_hit); // Undefined results in empty string.
6023
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006024 v8::Local<v8::String> data_obj_source = v8::String::New(
6025 "({ a: 'abc',\n"
6026 " b: 123,\n"
6027 " toString: function() { return this.a + ' ' + this.b; }\n"
6028 "})\n");
6029 v8::Local<v8::Value> data_obj = v8::Script::Compile(data_obj_source)->Run();
6030 v8::ScriptOrigin origin2 = v8::ScriptOrigin(v8::String::New("new name"));
6031 v8::Handle<v8::Script> script2 = v8::Script::Compile(script, &origin2);
6032 script2->Run();
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00006033 script2->SetData(data_obj->ToString());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006034 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6035 f->Call(env->Global(), 0, NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006036 CHECK_EQ(3, break_point_hit_count);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006037 CHECK_EQ("new name", last_script_name_hit);
6038 CHECK_EQ("abc 123", last_script_data_hit);
ager@chromium.org5c838252010-02-19 08:53:10 +00006039
6040 v8::Handle<v8::Script> script3 =
6041 v8::Script::Compile(script, &origin2, NULL,
6042 v8::String::New("in compile"));
6043 CHECK_EQ("in compile", last_script_data_hit);
6044 script3->Run();
6045 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6046 f->Call(env->Global(), 0, NULL);
6047 CHECK_EQ(4, break_point_hit_count);
6048 CHECK_EQ("in compile", last_script_data_hit);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006049}
ager@chromium.org9085a012009-05-11 19:22:57 +00006050
6051
6052static v8::Persistent<v8::Context> expected_context;
6053static v8::Handle<v8::Value> expected_context_data;
6054
6055
6056// Check that the expected context is the one generating the debug event.
6057static void ContextCheckMessageHandler(const v8::Debug::Message& message) {
6058 CHECK(message.GetEventContext() == expected_context);
6059 CHECK(message.GetEventContext()->GetData()->StrictEquals(
6060 expected_context_data));
6061 message_handler_hit_count++;
6062
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006063 static char print_buffer[1000];
6064 v8::String::Value json(message.GetJSON());
6065 Utf16ToAscii(*json, json.length(), print_buffer);
6066
ager@chromium.org9085a012009-05-11 19:22:57 +00006067 // Send a continue command for break events.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006068 if (IsBreakEventMessage(print_buffer)) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006069 SendContinueCommand();
ager@chromium.org9085a012009-05-11 19:22:57 +00006070 }
6071}
6072
6073
6074// Test which creates two contexts and sets different embedder data on each.
6075// Checks that this data is set correctly and that when the debug message
6076// handler is called the expected context is the one active.
6077TEST(ContextData) {
6078 v8::HandleScope scope;
6079
6080 v8::Debug::SetMessageHandler2(ContextCheckMessageHandler);
6081
6082 // Create two contexts.
6083 v8::Persistent<v8::Context> context_1;
6084 v8::Persistent<v8::Context> context_2;
6085 v8::Handle<v8::ObjectTemplate> global_template =
6086 v8::Handle<v8::ObjectTemplate>();
6087 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>();
6088 context_1 = v8::Context::New(NULL, global_template, global_object);
6089 context_2 = v8::Context::New(NULL, global_template, global_object);
6090
6091 // Default data value is undefined.
6092 CHECK(context_1->GetData()->IsUndefined());
6093 CHECK(context_2->GetData()->IsUndefined());
6094
6095 // Set and check different data values.
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00006096 v8::Handle<v8::String> data_1 = v8::String::New("1");
6097 v8::Handle<v8::String> data_2 = v8::String::New("2");
ager@chromium.org9085a012009-05-11 19:22:57 +00006098 context_1->SetData(data_1);
6099 context_2->SetData(data_2);
6100 CHECK(context_1->GetData()->StrictEquals(data_1));
6101 CHECK(context_2->GetData()->StrictEquals(data_2));
6102
6103 // Simple test function which causes a break.
6104 const char* source = "function f() { debugger; }";
6105
6106 // Enter and run function in the first context.
6107 {
6108 v8::Context::Scope context_scope(context_1);
6109 expected_context = context_1;
6110 expected_context_data = data_1;
6111 v8::Local<v8::Function> f = CompileFunction(source, "f");
6112 f->Call(context_1->Global(), 0, NULL);
6113 }
6114
6115
6116 // Enter and run function in the second context.
6117 {
6118 v8::Context::Scope context_scope(context_2);
6119 expected_context = context_2;
6120 expected_context_data = data_2;
6121 v8::Local<v8::Function> f = CompileFunction(source, "f");
6122 f->Call(context_2->Global(), 0, NULL);
6123 }
6124
6125 // Two times compile event and two times break event.
6126 CHECK_GT(message_handler_hit_count, 4);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006127
6128 v8::Debug::SetMessageHandler2(NULL);
6129 CheckDebuggerUnloaded();
6130}
6131
6132
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006133// Debug message handler which issues a debug break when it hits a break event.
6134static int message_handler_break_hit_count = 0;
6135static void DebugBreakMessageHandler(const v8::Debug::Message& message) {
6136 // Schedule a debug break for break events.
6137 if (message.IsEvent() && message.GetEvent() == v8::Break) {
6138 message_handler_break_hit_count++;
6139 if (message_handler_break_hit_count == 1) {
6140 v8::Debug::DebugBreak();
6141 }
6142 }
6143
6144 // Issue a continue command if this event will not cause the VM to start
6145 // running.
6146 if (!message.WillStartRunning()) {
6147 SendContinueCommand();
6148 }
6149}
6150
6151
6152// Test that a debug break can be scheduled while in a message handler.
6153TEST(DebugBreakInMessageHandler) {
6154 v8::HandleScope scope;
6155 DebugLocalContext env;
6156
6157 v8::Debug::SetMessageHandler2(DebugBreakMessageHandler);
6158
6159 // Test functions.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006160 const char* script = "function f() { debugger; g(); } function g() { }";
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006161 CompileRun(script);
6162 v8::Local<v8::Function> f =
6163 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6164 v8::Local<v8::Function> g =
6165 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
6166
6167 // Call f then g. The debugger statement in f will casue a break which will
6168 // cause another break.
6169 f->Call(env->Global(), 0, NULL);
6170 CHECK_EQ(2, message_handler_break_hit_count);
6171 // Calling g will not cause any additional breaks.
6172 g->Call(env->Global(), 0, NULL);
6173 CHECK_EQ(2, message_handler_break_hit_count);
6174}
6175
6176
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006177#ifndef V8_INTERPRETED_REGEXP
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006178// Debug event handler which gets the function on the top frame and schedules a
6179// break a number of times.
6180static void DebugEventDebugBreak(
6181 v8::DebugEvent event,
6182 v8::Handle<v8::Object> exec_state,
6183 v8::Handle<v8::Object> event_data,
6184 v8::Handle<v8::Value> data) {
6185
6186 if (event == v8::Break) {
6187 break_point_hit_count++;
6188
6189 // Get the name of the top frame function.
6190 if (!frame_function_name.IsEmpty()) {
6191 // Get the name of the function.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006192 const int argc = 2;
6193 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) };
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006194 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
6195 argc, argv);
6196 if (result->IsUndefined()) {
6197 last_function_hit[0] = '\0';
6198 } else {
6199 CHECK(result->IsString());
6200 v8::Handle<v8::String> function_name(result->ToString());
6201 function_name->WriteAscii(last_function_hit);
6202 }
6203 }
6204
6205 // Keep forcing breaks.
6206 if (break_point_hit_count < 20) {
6207 v8::Debug::DebugBreak();
6208 }
6209 }
6210}
6211
6212
6213TEST(RegExpDebugBreak) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00006214 // This test only applies to native regexps.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006215 v8::HandleScope scope;
6216 DebugLocalContext env;
6217
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006218 // Create a function for checking the function when hitting a break point.
6219 frame_function_name = CompileFunction(&env,
6220 frame_function_name_source,
6221 "frame_function_name");
6222
6223 // Test RegExp which matches white spaces and comments at the begining of a
6224 // source line.
6225 const char* script =
6226 "var sourceLineBeginningSkip = /^(?:[ \\v\\h]*(?:\\/\\*.*?\\*\\/)*)*/;\n"
6227 "function f(s) { return s.match(sourceLineBeginningSkip)[0].length; }";
6228
6229 v8::Local<v8::Function> f = CompileFunction(script, "f");
6230 const int argc = 1;
6231 v8::Handle<v8::Value> argv[argc] = { v8::String::New(" /* xxx */ a=0;") };
6232 v8::Local<v8::Value> result = f->Call(env->Global(), argc, argv);
6233 CHECK_EQ(12, result->Int32Value());
6234
6235 v8::Debug::SetDebugEventListener(DebugEventDebugBreak);
6236 v8::Debug::DebugBreak();
6237 result = f->Call(env->Global(), argc, argv);
6238
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006239 // Check that there was only one break event. Matching RegExp should not
6240 // cause Break events.
6241 CHECK_EQ(1, break_point_hit_count);
6242 CHECK_EQ("f", last_function_hit);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006243}
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006244#endif // V8_INTERPRETED_REGEXP
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006245
6246
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006247// Common part of EvalContextData and NestedBreakEventContextData tests.
6248static void ExecuteScriptForContextCheck() {
6249 // Create a context.
6250 v8::Persistent<v8::Context> context_1;
6251 v8::Handle<v8::ObjectTemplate> global_template =
6252 v8::Handle<v8::ObjectTemplate>();
6253 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>();
6254 context_1 = v8::Context::New(NULL, global_template, global_object);
6255
6256 // Default data value is undefined.
6257 CHECK(context_1->GetData()->IsUndefined());
6258
6259 // Set and check a data value.
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00006260 v8::Handle<v8::String> data_1 = v8::String::New("1");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006261 context_1->SetData(data_1);
6262 CHECK(context_1->GetData()->StrictEquals(data_1));
6263
6264 // Simple test function with eval that causes a break.
6265 const char* source = "function f() { eval('debugger;'); }";
6266
6267 // Enter and run function in the context.
6268 {
6269 v8::Context::Scope context_scope(context_1);
6270 expected_context = context_1;
6271 expected_context_data = data_1;
6272 v8::Local<v8::Function> f = CompileFunction(source, "f");
6273 f->Call(context_1->Global(), 0, NULL);
6274 }
6275}
6276
6277
6278// Test which creates a context and sets embedder data on it. Checks that this
6279// data is set correctly and that when the debug message handler is called for
6280// break event in an eval statement the expected context is the one returned by
6281// Message.GetEventContext.
6282TEST(EvalContextData) {
6283 v8::HandleScope scope;
6284 v8::Debug::SetMessageHandler2(ContextCheckMessageHandler);
6285
6286 ExecuteScriptForContextCheck();
6287
6288 // One time compile event and one time break event.
6289 CHECK_GT(message_handler_hit_count, 2);
6290 v8::Debug::SetMessageHandler2(NULL);
6291 CheckDebuggerUnloaded();
6292}
6293
6294
6295static bool sent_eval = false;
6296static int break_count = 0;
6297static int continue_command_send_count = 0;
6298// Check that the expected context is the one generating the debug event
6299// including the case of nested break event.
6300static void DebugEvalContextCheckMessageHandler(
6301 const v8::Debug::Message& message) {
6302 CHECK(message.GetEventContext() == expected_context);
6303 CHECK(message.GetEventContext()->GetData()->StrictEquals(
6304 expected_context_data));
6305 message_handler_hit_count++;
6306
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006307 static char print_buffer[1000];
6308 v8::String::Value json(message.GetJSON());
6309 Utf16ToAscii(*json, json.length(), print_buffer);
6310
6311 if (IsBreakEventMessage(print_buffer)) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006312 break_count++;
6313 if (!sent_eval) {
6314 sent_eval = true;
6315
6316 const int kBufferSize = 1000;
6317 uint16_t buffer[kBufferSize];
6318 const char* eval_command =
6319 "{\"seq\":0,"
6320 "\"type\":\"request\","
6321 "\"command\":\"evaluate\","
6322 "arguments:{\"expression\":\"debugger;\","
6323 "\"global\":true,\"disable_break\":false}}";
6324
6325 // Send evaluate command.
6326 v8::Debug::SendCommand(buffer, AsciiToUtf16(eval_command, buffer));
6327 return;
6328 } else {
6329 // It's a break event caused by the evaluation request above.
6330 SendContinueCommand();
6331 continue_command_send_count++;
6332 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006333 } else if (IsEvaluateResponseMessage(print_buffer) &&
6334 continue_command_send_count < 2) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006335 // Response to the evaluation request. We're still on the breakpoint so
6336 // send continue.
6337 SendContinueCommand();
6338 continue_command_send_count++;
6339 }
6340}
6341
6342
6343// Tests that context returned for break event is correct when the event occurs
6344// in 'evaluate' debugger request.
6345TEST(NestedBreakEventContextData) {
6346 v8::HandleScope scope;
6347 break_count = 0;
6348 message_handler_hit_count = 0;
6349 v8::Debug::SetMessageHandler2(DebugEvalContextCheckMessageHandler);
6350
6351 ExecuteScriptForContextCheck();
6352
6353 // One time compile event and two times break event.
6354 CHECK_GT(message_handler_hit_count, 3);
6355
6356 // One break from the source and another from the evaluate request.
6357 CHECK_EQ(break_count, 2);
6358 v8::Debug::SetMessageHandler2(NULL);
6359 CheckDebuggerUnloaded();
6360}
6361
6362
6363// Debug event listener which counts the script collected events.
6364int script_collected_count = 0;
6365static void DebugEventScriptCollectedEvent(v8::DebugEvent event,
6366 v8::Handle<v8::Object> exec_state,
6367 v8::Handle<v8::Object> event_data,
6368 v8::Handle<v8::Value> data) {
6369 // Count the number of breaks.
6370 if (event == v8::ScriptCollected) {
6371 script_collected_count++;
6372 }
6373}
6374
6375
6376// Test that scripts collected are reported through the debug event listener.
6377TEST(ScriptCollectedEvent) {
6378 break_point_hit_count = 0;
6379 script_collected_count = 0;
6380 v8::HandleScope scope;
6381 DebugLocalContext env;
6382
6383 // Request the loaded scripts to initialize the debugger script cache.
6384 Debug::GetLoadedScripts();
6385
6386 // Do garbage collection to ensure that only the script in this test will be
6387 // collected afterwards.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006388 Heap::CollectAllGarbage(false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006389
6390 script_collected_count = 0;
6391 v8::Debug::SetDebugEventListener(DebugEventScriptCollectedEvent,
6392 v8::Undefined());
6393 {
6394 v8::Script::Compile(v8::String::New("eval('a=1')"))->Run();
6395 v8::Script::Compile(v8::String::New("eval('a=2')"))->Run();
6396 }
6397
6398 // Do garbage collection to collect the script above which is no longer
6399 // referenced.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006400 Heap::CollectAllGarbage(false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006401
6402 CHECK_EQ(2, script_collected_count);
6403
6404 v8::Debug::SetDebugEventListener(NULL);
6405 CheckDebuggerUnloaded();
6406}
6407
6408
6409// Debug event listener which counts the script collected events.
6410int script_collected_message_count = 0;
6411static void ScriptCollectedMessageHandler(const v8::Debug::Message& message) {
6412 // Count the number of scripts collected.
6413 if (message.IsEvent() && message.GetEvent() == v8::ScriptCollected) {
6414 script_collected_message_count++;
6415 v8::Handle<v8::Context> context = message.GetEventContext();
6416 CHECK(context.IsEmpty());
6417 }
6418}
6419
6420
6421// Test that GetEventContext doesn't fail and return empty handle for
6422// ScriptCollected events.
6423TEST(ScriptCollectedEventContext) {
6424 script_collected_message_count = 0;
6425 v8::HandleScope scope;
6426
6427 { // Scope for the DebugLocalContext.
6428 DebugLocalContext env;
6429
6430 // Request the loaded scripts to initialize the debugger script cache.
6431 Debug::GetLoadedScripts();
6432
6433 // Do garbage collection to ensure that only the script in this test will be
6434 // collected afterwards.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006435 Heap::CollectAllGarbage(false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006436
6437 v8::Debug::SetMessageHandler2(ScriptCollectedMessageHandler);
6438 {
6439 v8::Script::Compile(v8::String::New("eval('a=1')"))->Run();
6440 v8::Script::Compile(v8::String::New("eval('a=2')"))->Run();
6441 }
6442 }
6443
6444 // Do garbage collection to collect the script above which is no longer
6445 // referenced.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006446 Heap::CollectAllGarbage(false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006447
6448 CHECK_EQ(2, script_collected_message_count);
6449
6450 v8::Debug::SetMessageHandler2(NULL);
6451}
6452
6453
6454// Debug event listener which counts the after compile events.
6455int after_compile_message_count = 0;
6456static void AfterCompileMessageHandler(const v8::Debug::Message& message) {
6457 // Count the number of scripts collected.
6458 if (message.IsEvent()) {
6459 if (message.GetEvent() == v8::AfterCompile) {
6460 after_compile_message_count++;
6461 } else if (message.GetEvent() == v8::Break) {
6462 SendContinueCommand();
6463 }
6464 }
6465}
6466
6467
6468// Tests that after compile event is sent as many times as there are scripts
6469// compiled.
6470TEST(AfterCompileMessageWhenMessageHandlerIsReset) {
6471 v8::HandleScope scope;
6472 DebugLocalContext env;
6473 after_compile_message_count = 0;
6474 const char* script = "var a=1";
6475
6476 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6477 v8::Script::Compile(v8::String::New(script))->Run();
6478 v8::Debug::SetMessageHandler2(NULL);
6479
6480 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6481 v8::Debug::DebugBreak();
6482 v8::Script::Compile(v8::String::New(script))->Run();
6483
6484 // Setting listener to NULL should cause debugger unload.
6485 v8::Debug::SetMessageHandler2(NULL);
6486 CheckDebuggerUnloaded();
6487
6488 // Compilation cache should be disabled when debugger is active.
6489 CHECK_EQ(2, after_compile_message_count);
6490}
6491
6492
6493// Tests that break event is sent when message handler is reset.
6494TEST(BreakMessageWhenMessageHandlerIsReset) {
6495 v8::HandleScope scope;
6496 DebugLocalContext env;
6497 after_compile_message_count = 0;
6498 const char* script = "function f() {};";
6499
6500 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6501 v8::Script::Compile(v8::String::New(script))->Run();
6502 v8::Debug::SetMessageHandler2(NULL);
6503
6504 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6505 v8::Debug::DebugBreak();
6506 v8::Local<v8::Function> f =
6507 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6508 f->Call(env->Global(), 0, NULL);
6509
6510 // Setting message handler to NULL should cause debugger unload.
6511 v8::Debug::SetMessageHandler2(NULL);
6512 CheckDebuggerUnloaded();
6513
6514 // Compilation cache should be disabled when debugger is active.
6515 CHECK_EQ(1, after_compile_message_count);
6516}
6517
6518
6519static int exception_event_count = 0;
6520static void ExceptionMessageHandler(const v8::Debug::Message& message) {
6521 if (message.IsEvent() && message.GetEvent() == v8::Exception) {
6522 exception_event_count++;
6523 SendContinueCommand();
6524 }
6525}
6526
6527
6528// Tests that exception event is sent when message handler is reset.
6529TEST(ExceptionMessageWhenMessageHandlerIsReset) {
6530 v8::HandleScope scope;
6531 DebugLocalContext env;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00006532
6533 // For this test, we want to break on uncaught exceptions:
6534 ChangeBreakOnException(false, true);
6535
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006536 exception_event_count = 0;
6537 const char* script = "function f() {throw new Error()};";
6538
6539 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6540 v8::Script::Compile(v8::String::New(script))->Run();
6541 v8::Debug::SetMessageHandler2(NULL);
6542
6543 v8::Debug::SetMessageHandler2(ExceptionMessageHandler);
6544 v8::Local<v8::Function> f =
6545 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6546 f->Call(env->Global(), 0, NULL);
6547
6548 // Setting message handler to NULL should cause debugger unload.
6549 v8::Debug::SetMessageHandler2(NULL);
6550 CheckDebuggerUnloaded();
6551
6552 CHECK_EQ(1, exception_event_count);
ager@chromium.org9085a012009-05-11 19:22:57 +00006553}
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006554
6555
6556// Tests after compile event is sent when there are some provisional
6557// breakpoints out of the scripts lines range.
6558TEST(ProvisionalBreakpointOnLineOutOfRange) {
6559 v8::HandleScope scope;
6560 DebugLocalContext env;
6561 env.ExposeDebug();
6562 const char* script = "function f() {};";
6563 const char* resource_name = "test_resource";
6564
6565 // Set a couple of provisional breakpoint on lines out of the script lines
6566 // range.
6567 int sbp1 = SetScriptBreakPointByNameFromJS(resource_name, 3,
6568 -1 /* no column */);
6569 int sbp2 = SetScriptBreakPointByNameFromJS(resource_name, 5, 5);
6570
6571 after_compile_message_count = 0;
6572 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6573
6574 v8::ScriptOrigin origin(
6575 v8::String::New(resource_name),
6576 v8::Integer::New(10),
6577 v8::Integer::New(1));
6578 // Compile a script whose first line number is greater than the breakpoints'
6579 // lines.
6580 v8::Script::Compile(v8::String::New(script), &origin)->Run();
6581
6582 // If the script is compiled successfully there is exactly one after compile
6583 // event. In case of an exception in debugger code after compile event is not
6584 // sent.
6585 CHECK_EQ(1, after_compile_message_count);
6586
6587 ClearBreakPointFromJS(sbp1);
6588 ClearBreakPointFromJS(sbp2);
6589 v8::Debug::SetMessageHandler2(NULL);
6590}
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006591
6592
6593static void BreakMessageHandler(const v8::Debug::Message& message) {
6594 if (message.IsEvent() && message.GetEvent() == v8::Break) {
6595 // Count the number of breaks.
6596 break_point_hit_count++;
6597
6598 v8::HandleScope scope;
6599 v8::Handle<v8::String> json = message.GetJSON();
6600
6601 SendContinueCommand();
6602 } else if (message.IsEvent() && message.GetEvent() == v8::AfterCompile) {
6603 v8::HandleScope scope;
6604
6605 bool is_debug_break = i::StackGuard::IsDebugBreak();
6606 // Force DebugBreak flag while serializer is working.
6607 i::StackGuard::DebugBreak();
6608
6609 // Force serialization to trigger some internal JS execution.
6610 v8::Handle<v8::String> json = message.GetJSON();
6611
6612 // Restore previous state.
6613 if (is_debug_break) {
6614 i::StackGuard::DebugBreak();
6615 } else {
6616 i::StackGuard::Continue(i::DEBUGBREAK);
6617 }
6618 }
6619}
6620
6621
6622// Test that if DebugBreak is forced it is ignored when code from
6623// debug-delay.js is executed.
6624TEST(NoDebugBreakInAfterCompileMessageHandler) {
6625 v8::HandleScope scope;
6626 DebugLocalContext env;
6627
6628 // Register a debug event listener which sets the break flag and counts.
6629 v8::Debug::SetMessageHandler2(BreakMessageHandler);
6630
6631 // Set the debug break flag.
6632 v8::Debug::DebugBreak();
6633
6634 // Create a function for testing stepping.
6635 const char* src = "function f() { eval('var x = 10;'); } ";
6636 v8::Local<v8::Function> f = CompileFunction(&env, src, "f");
6637
6638 // There should be only one break event.
6639 CHECK_EQ(1, break_point_hit_count);
6640
6641 // Set the debug break flag again.
6642 v8::Debug::DebugBreak();
6643 f->Call(env->Global(), 0, NULL);
6644 // There should be one more break event when the script is evaluated in 'f'.
6645 CHECK_EQ(2, break_point_hit_count);
6646
6647 // Get rid of the debug message handler.
6648 v8::Debug::SetMessageHandler2(NULL);
6649 CheckDebuggerUnloaded();
6650}
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006651
6652
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006653static int counting_message_handler_counter;
6654
6655static void CountingMessageHandler(const v8::Debug::Message& message) {
6656 counting_message_handler_counter++;
6657}
6658
6659// Test that debug messages get processed when ProcessDebugMessages is called.
6660TEST(ProcessDebugMessages) {
6661 v8::HandleScope scope;
6662 DebugLocalContext env;
6663
6664 counting_message_handler_counter = 0;
6665
6666 v8::Debug::SetMessageHandler2(CountingMessageHandler);
6667
6668 const int kBufferSize = 1000;
6669 uint16_t buffer[kBufferSize];
6670 const char* scripts_command =
6671 "{\"seq\":0,"
6672 "\"type\":\"request\","
6673 "\"command\":\"scripts\"}";
6674
6675 // Send scripts command.
6676 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer));
6677
6678 CHECK_EQ(0, counting_message_handler_counter);
6679 v8::Debug::ProcessDebugMessages();
6680 // At least one message should come
6681 CHECK_GE(counting_message_handler_counter, 1);
6682
6683 counting_message_handler_counter = 0;
6684
6685 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer));
6686 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer));
6687 CHECK_EQ(0, counting_message_handler_counter);
6688 v8::Debug::ProcessDebugMessages();
6689 // At least two messages should come
6690 CHECK_GE(counting_message_handler_counter, 2);
6691
6692 // Get rid of the debug message handler.
6693 v8::Debug::SetMessageHandler2(NULL);
6694 CheckDebuggerUnloaded();
6695}
6696
6697
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006698struct BacktraceData {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006699 static int frame_counter;
6700 static void MessageHandler(const v8::Debug::Message& message) {
6701 char print_buffer[1000];
6702 v8::String::Value json(message.GetJSON());
6703 Utf16ToAscii(*json, json.length(), print_buffer, 1000);
6704
6705 if (strstr(print_buffer, "backtrace") == NULL) {
6706 return;
6707 }
6708 frame_counter = GetTotalFramesInt(print_buffer);
6709 }
6710};
6711
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006712int BacktraceData::frame_counter;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006713
6714
6715// Test that debug messages get processed when ProcessDebugMessages is called.
6716TEST(Backtrace) {
6717 v8::HandleScope scope;
6718 DebugLocalContext env;
6719
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006720 v8::Debug::SetMessageHandler2(BacktraceData::MessageHandler);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006721
6722 const int kBufferSize = 1000;
6723 uint16_t buffer[kBufferSize];
6724 const char* scripts_command =
6725 "{\"seq\":0,"
6726 "\"type\":\"request\","
6727 "\"command\":\"backtrace\"}";
6728
6729 // Check backtrace from ProcessDebugMessages.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006730 BacktraceData::frame_counter = -10;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006731 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer));
6732 v8::Debug::ProcessDebugMessages();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006733 CHECK_EQ(BacktraceData::frame_counter, 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006734
6735 v8::Handle<v8::String> void0 = v8::String::New("void(0)");
6736 v8::Handle<v8::Script> script = v8::Script::Compile(void0, void0);
6737
6738 // Check backtrace from "void(0)" script.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006739 BacktraceData::frame_counter = -10;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006740 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer));
6741 script->Run();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006742 CHECK_EQ(BacktraceData::frame_counter, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006743
6744 // Get rid of the debug message handler.
6745 v8::Debug::SetMessageHandler2(NULL);
6746 CheckDebuggerUnloaded();
6747}
6748
6749
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006750TEST(GetMirror) {
6751 v8::HandleScope scope;
6752 DebugLocalContext env;
6753 v8::Handle<v8::Value> obj = v8::Debug::GetMirror(v8::String::New("hodja"));
6754 v8::Handle<v8::Function> run_test = v8::Handle<v8::Function>::Cast(
6755 v8::Script::New(
6756 v8::String::New(
6757 "function runTest(mirror) {"
6758 " return mirror.isString() && (mirror.length() == 5);"
6759 "}"
6760 ""
6761 "runTest;"))->Run());
6762 v8::Handle<v8::Value> result = run_test->Call(env->Global(), 1, &obj);
6763 CHECK(result->IsTrue());
6764}
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006765
6766
6767// Test that the debug break flag works with function.apply.
6768TEST(DebugBreakFunctionApply) {
6769 v8::HandleScope scope;
6770 DebugLocalContext env;
6771
6772 // Create a function for testing breaking in apply.
6773 v8::Local<v8::Function> foo = CompileFunction(
6774 &env,
6775 "function baz(x) { }"
6776 "function bar(x) { baz(); }"
6777 "function foo(){ bar.apply(this, [1]); }",
6778 "foo");
6779
6780 // Register a debug event listener which steps and counts.
6781 v8::Debug::SetDebugEventListener(DebugEventBreakMax);
6782
6783 // Set the debug break flag before calling the code using function.apply.
6784 v8::Debug::DebugBreak();
6785
6786 // Limit the number of debug breaks. This is a regression test for issue 493
6787 // where this test would enter an infinite loop.
6788 break_point_hit_count = 0;
6789 max_break_point_hit_count = 10000; // 10000 => infinite loop.
6790 foo->Call(env->Global(), 0, NULL);
6791
6792 // When keeping the debug break several break will happen.
6793 CHECK_EQ(3, break_point_hit_count);
6794
6795 v8::Debug::SetDebugEventListener(NULL);
6796 CheckDebuggerUnloaded();
6797}
6798
6799
6800v8::Handle<v8::Context> debugee_context;
6801v8::Handle<v8::Context> debugger_context;
6802
6803
6804// Property getter that checks that current and calling contexts
6805// are both the debugee contexts.
6806static v8::Handle<v8::Value> NamedGetterWithCallingContextCheck(
6807 v8::Local<v8::String> name,
6808 const v8::AccessorInfo& info) {
6809 CHECK_EQ(0, strcmp(*v8::String::AsciiValue(name), "a"));
6810 v8::Handle<v8::Context> current = v8::Context::GetCurrent();
6811 CHECK(current == debugee_context);
6812 CHECK(current != debugger_context);
6813 v8::Handle<v8::Context> calling = v8::Context::GetCalling();
6814 CHECK(calling == debugee_context);
6815 CHECK(calling != debugger_context);
6816 return v8::Int32::New(1);
6817}
6818
6819
6820// Debug event listener that checks if the first argument of a function is
6821// an object with property 'a' == 1. If the property has custom accessor
6822// this handler will eventually invoke it.
6823static void DebugEventGetAtgumentPropertyValue(
6824 v8::DebugEvent event,
6825 v8::Handle<v8::Object> exec_state,
6826 v8::Handle<v8::Object> event_data,
6827 v8::Handle<v8::Value> data) {
6828 if (event == v8::Break) {
6829 break_point_hit_count++;
6830 CHECK(debugger_context == v8::Context::GetCurrent());
6831 v8::Handle<v8::Function> func(v8::Function::Cast(*CompileRun(
6832 "(function(exec_state) {\n"
6833 " return (exec_state.frame(0).argumentValue(0).property('a').\n"
6834 " value().value() == 1);\n"
6835 "})")));
6836 const int argc = 1;
6837 v8::Handle<v8::Value> argv[argc] = { exec_state };
6838 v8::Handle<v8::Value> result = func->Call(exec_state, argc, argv);
6839 CHECK(result->IsTrue());
6840 }
6841}
6842
6843
6844TEST(CallingContextIsNotDebugContext) {
6845 // Create and enter a debugee context.
6846 v8::HandleScope scope;
6847 DebugLocalContext env;
6848 env.ExposeDebug();
6849
6850 // Save handles to the debugger and debugee contexts to be used in
6851 // NamedGetterWithCallingContextCheck.
6852 debugee_context = v8::Local<v8::Context>(*env);
6853 debugger_context = v8::Utils::ToLocal(Debug::debug_context());
6854
6855 // Create object with 'a' property accessor.
6856 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New();
6857 named->SetAccessor(v8::String::New("a"),
6858 NamedGetterWithCallingContextCheck);
6859 env->Global()->Set(v8::String::New("obj"),
6860 named->NewInstance());
6861
6862 // Register the debug event listener
6863 v8::Debug::SetDebugEventListener(DebugEventGetAtgumentPropertyValue);
6864
6865 // Create a function that invokes debugger.
6866 v8::Local<v8::Function> foo = CompileFunction(
6867 &env,
6868 "function bar(x) { debugger; }"
6869 "function foo(){ bar(obj); }",
6870 "foo");
6871
6872 break_point_hit_count = 0;
6873 foo->Call(env->Global(), 0, NULL);
6874 CHECK_EQ(1, break_point_hit_count);
6875
6876 v8::Debug::SetDebugEventListener(NULL);
6877 debugee_context = v8::Handle<v8::Context>();
6878 debugger_context = v8::Handle<v8::Context>();
6879 CheckDebuggerUnloaded();
6880}
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006881
6882
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006883TEST(DebugContextIsPreservedBetweenAccesses) {
6884 v8::HandleScope scope;
6885 v8::Local<v8::Context> context1 = v8::Debug::GetDebugContext();
6886 v8::Local<v8::Context> context2 = v8::Debug::GetDebugContext();
6887 CHECK_EQ(*context1, *context2);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006888}
6889
6890
6891static v8::Handle<v8::Value> expected_callback_data;
6892static void DebugEventContextChecker(const v8::Debug::EventDetails& details) {
6893 CHECK(details.GetEventContext() == expected_context);
6894 CHECK_EQ(expected_callback_data, details.GetCallbackData());
6895}
6896
6897// Check that event details contain context where debug event occured.
6898TEST(DebugEventContext) {
6899 v8::HandleScope scope;
6900 expected_callback_data = v8::Int32::New(2010);
6901 v8::Debug::SetDebugEventListener2(DebugEventContextChecker,
6902 expected_callback_data);
6903 expected_context = v8::Context::New();
6904 v8::Context::Scope context_scope(expected_context);
6905 v8::Script::Compile(v8::String::New("(function(){debugger;})();"))->Run();
6906 expected_context.Dispose();
6907 expected_context.Clear();
6908 v8::Debug::SetDebugEventListener(NULL);
6909 expected_context_data = v8::Handle<v8::Value>();
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006910 CheckDebuggerUnloaded();
6911}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006912
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00006913
6914static void* expected_break_data;
6915static bool was_debug_break_called;
6916static bool was_debug_event_called;
6917static void DebugEventBreakDataChecker(const v8::Debug::EventDetails& details) {
6918 if (details.GetEvent() == v8::BreakForCommand) {
6919 CHECK_EQ(expected_break_data, details.GetClientData());
6920 was_debug_event_called = true;
6921 } else if (details.GetEvent() == v8::Break) {
6922 was_debug_break_called = true;
6923 }
6924}
6925
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006926
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00006927// Check that event details contain context where debug event occured.
6928TEST(DebugEventBreakData) {
6929 v8::HandleScope scope;
6930 DebugLocalContext env;
6931 v8::Debug::SetDebugEventListener2(DebugEventBreakDataChecker);
6932
6933 TestClientData::constructor_call_counter = 0;
6934 TestClientData::destructor_call_counter = 0;
6935
6936 expected_break_data = NULL;
6937 was_debug_event_called = false;
6938 was_debug_break_called = false;
6939 v8::Debug::DebugBreakForCommand();
6940 v8::Script::Compile(v8::String::New("(function(x){return x;})(1);"))->Run();
6941 CHECK(was_debug_event_called);
6942 CHECK(!was_debug_break_called);
6943
6944 TestClientData* data1 = new TestClientData();
6945 expected_break_data = data1;
6946 was_debug_event_called = false;
6947 was_debug_break_called = false;
6948 v8::Debug::DebugBreakForCommand(data1);
6949 v8::Script::Compile(v8::String::New("(function(x){return x+1;})(1);"))->Run();
6950 CHECK(was_debug_event_called);
6951 CHECK(!was_debug_break_called);
6952
6953 expected_break_data = NULL;
6954 was_debug_event_called = false;
6955 was_debug_break_called = false;
6956 v8::Debug::DebugBreak();
6957 v8::Script::Compile(v8::String::New("(function(x){return x+2;})(1);"))->Run();
6958 CHECK(!was_debug_event_called);
6959 CHECK(was_debug_break_called);
6960
6961 TestClientData* data2 = new TestClientData();
6962 expected_break_data = data2;
6963 was_debug_event_called = false;
6964 was_debug_break_called = false;
6965 v8::Debug::DebugBreak();
6966 v8::Debug::DebugBreakForCommand(data2);
6967 v8::Script::Compile(v8::String::New("(function(x){return x+3;})(1);"))->Run();
6968 CHECK(was_debug_event_called);
6969 CHECK(was_debug_break_called);
6970
6971 CHECK_EQ(2, TestClientData::constructor_call_counter);
6972 CHECK_EQ(TestClientData::constructor_call_counter,
6973 TestClientData::destructor_call_counter);
6974
6975 v8::Debug::SetDebugEventListener(NULL);
6976 CheckDebuggerUnloaded();
6977}
6978
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006979static bool debug_event_break_deoptimize_done = false;
6980
6981static void DebugEventBreakDeoptimize(v8::DebugEvent event,
6982 v8::Handle<v8::Object> exec_state,
6983 v8::Handle<v8::Object> event_data,
6984 v8::Handle<v8::Value> data) {
6985 if (event == v8::Break) {
6986 if (!frame_function_name.IsEmpty()) {
6987 // Get the name of the function.
6988 const int argc = 2;
6989 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) };
6990 v8::Handle<v8::Value> result =
6991 frame_function_name->Call(exec_state, argc, argv);
6992 if (!result->IsUndefined()) {
6993 char fn[80];
6994 CHECK(result->IsString());
6995 v8::Handle<v8::String> function_name(result->ToString());
6996 function_name->WriteAscii(fn);
6997 if (strcmp(fn, "bar") == 0) {
6998 i::Deoptimizer::DeoptimizeAll();
6999 debug_event_break_deoptimize_done = true;
7000 }
7001 }
7002 }
7003
7004 v8::Debug::DebugBreak();
7005 }
7006}
7007
7008
7009// Test deoptimization when execution is broken using the debug break stack
7010// check interrupt.
7011TEST(DeoptimizeDuringDebugBreak) {
7012 v8::HandleScope scope;
7013 DebugLocalContext env;
7014 env.ExposeDebug();
7015
7016 // Create a function for checking the function when hitting a break point.
7017 frame_function_name = CompileFunction(&env,
7018 frame_function_name_source,
7019 "frame_function_name");
7020
7021
7022 // Set a debug event listener which will keep interrupting execution until
7023 // debug break. When inside function bar it will deoptimize all functions.
7024 // This tests lazy deoptimization bailout for the stack check, as the first
7025 // time in function bar when using debug break and no break points will be at
7026 // the initial stack check.
7027 v8::Debug::SetDebugEventListener(DebugEventBreakDeoptimize,
7028 v8::Undefined());
7029
7030 // Compile and run function bar which will optimize it for some flag settings.
7031 v8::Script::Compile(v8::String::New("function bar(){}; bar()"))->Run();
7032
7033 // Set debug break and call bar again.
7034 v8::Debug::DebugBreak();
7035 v8::Script::Compile(v8::String::New("bar()"))->Run();
7036
7037 CHECK(debug_event_break_deoptimize_done);
7038
7039 v8::Debug::SetDebugEventListener(NULL);
7040}
7041
7042
7043static void DebugEventBreakWithOptimizedStack(v8::DebugEvent event,
7044 v8::Handle<v8::Object> exec_state,
7045 v8::Handle<v8::Object> event_data,
7046 v8::Handle<v8::Value> data) {
7047 if (event == v8::Break) {
7048 if (!frame_function_name.IsEmpty()) {
7049 for (int i = 0; i < 2; i++) {
7050 const int argc = 2;
7051 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(i) };
7052 // Get the name of the function in frame i.
7053 v8::Handle<v8::Value> result =
7054 frame_function_name->Call(exec_state, argc, argv);
7055 CHECK(result->IsString());
7056 v8::Handle<v8::String> function_name(result->ToString());
7057 CHECK(function_name->Equals(v8::String::New("loop")));
7058 // Get the name of the first argument in frame i.
7059 result = frame_argument_name->Call(exec_state, argc, argv);
7060 CHECK(result->IsString());
7061 v8::Handle<v8::String> argument_name(result->ToString());
7062 CHECK(argument_name->Equals(v8::String::New("count")));
7063 // Get the value of the first argument in frame i. If the
7064 // funtion is optimized the value will be undefined, otherwise
7065 // the value will be '1 - i'.
7066 //
7067 // TODO(3141533): We should be able to get the real value for
7068 // optimized frames.
7069 result = frame_argument_value->Call(exec_state, argc, argv);
7070 CHECK(result->IsUndefined() || (result->Int32Value() == 1 - i));
7071 // Get the name of the first local variable.
7072 result = frame_local_name->Call(exec_state, argc, argv);
7073 CHECK(result->IsString());
7074 v8::Handle<v8::String> local_name(result->ToString());
7075 CHECK(local_name->Equals(v8::String::New("local")));
7076 // Get the value of the first local variable. If the function
7077 // is optimized the value will be undefined, otherwise it will
7078 // be 42.
7079 //
7080 // TODO(3141533): We should be able to get the real value for
7081 // optimized frames.
7082 result = frame_local_value->Call(exec_state, argc, argv);
7083 CHECK(result->IsUndefined() || (result->Int32Value() == 42));
7084 }
7085 }
7086 }
7087}
7088
7089
7090static v8::Handle<v8::Value> ScheduleBreak(const v8::Arguments& args) {
7091 v8::Debug::SetDebugEventListener(DebugEventBreakWithOptimizedStack,
7092 v8::Undefined());
7093 v8::Debug::DebugBreak();
7094 return v8::Undefined();
7095}
7096
7097
7098TEST(DebugBreakStackInspection) {
7099 v8::HandleScope scope;
7100 DebugLocalContext env;
7101
7102 frame_function_name =
7103 CompileFunction(&env, frame_function_name_source, "frame_function_name");
7104 frame_argument_name =
7105 CompileFunction(&env, frame_argument_name_source, "frame_argument_name");
7106 frame_argument_value = CompileFunction(&env,
7107 frame_argument_value_source,
7108 "frame_argument_value");
7109 frame_local_name =
7110 CompileFunction(&env, frame_local_name_source, "frame_local_name");
7111 frame_local_value =
7112 CompileFunction(&env, frame_local_value_source, "frame_local_value");
7113
7114 v8::Handle<v8::FunctionTemplate> schedule_break_template =
7115 v8::FunctionTemplate::New(ScheduleBreak);
7116 v8::Handle<v8::Function> schedule_break =
7117 schedule_break_template->GetFunction();
7118 env->Global()->Set(v8_str("scheduleBreak"), schedule_break);
7119
7120 const char* src =
7121 "function loop(count) {"
7122 " var local = 42;"
7123 " if (count < 1) { scheduleBreak(); loop(count + 1); }"
7124 "}"
7125 "loop(0);";
7126 v8::Script::Compile(v8::String::New(src))->Run();
7127}
7128
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007129
7130// Test that setting the terminate execution flag during debug break processing.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007131static void TestDebugBreakInLoop(const char* loop_head,
7132 const char** loop_bodies,
7133 const char* loop_tail) {
7134 // Receive 100 breaks for each test and then terminate JavaScript execution.
7135 static int count = 0;
7136
7137 for (int i = 0; loop_bodies[i] != NULL; i++) {
7138 count++;
7139 max_break_point_hit_count = count * 100;
7140 terminate_after_max_break_point_hit = true;
7141
7142 EmbeddedVector<char, 1024> buffer;
7143 OS::SNPrintF(buffer,
7144 "function f() {%s%s%s}",
7145 loop_head, loop_bodies[i], loop_tail);
7146
7147 // Function with infinite loop.
7148 CompileRun(buffer.start());
7149
7150 // Set the debug break to enter the debugger as soon as possible.
7151 v8::Debug::DebugBreak();
7152
7153 // Call function with infinite loop.
7154 CompileRun("f();");
7155 CHECK_EQ(count * 100, break_point_hit_count);
7156
7157 CHECK(!v8::V8::IsExecutionTerminating());
7158 }
7159}
7160
7161
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007162TEST(DebugBreakLoop) {
7163 v8::HandleScope scope;
7164 DebugLocalContext env;
7165
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007166 // Register a debug event listener which sets the break flag and counts.
7167 v8::Debug::SetDebugEventListener(DebugEventBreakMax);
7168
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007169 CompileRun("var a = 1;");
7170 CompileRun("function g() { }");
7171 CompileRun("function h() { }");
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007172
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007173 const char* loop_bodies[] = {
7174 "",
7175 "g()",
7176 "if (a == 0) { g() }",
7177 "if (a == 1) { g() }",
7178 "if (a == 0) { g() } else { h() }",
7179 "if (a == 0) { continue }",
7180 "if (a == 1) { continue }",
7181 "switch (a) { case 1: g(); }",
7182 "switch (a) { case 1: continue; }",
7183 "switch (a) { case 1: g(); break; default: h() }",
7184 "switch (a) { case 1: continue; break; default: h() }",
7185 NULL
7186 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007187
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007188 TestDebugBreakInLoop("while (true) {", loop_bodies, "}");
7189 TestDebugBreakInLoop("while (a == 1) {", loop_bodies, "}");
7190
7191 TestDebugBreakInLoop("do {", loop_bodies, "} while (true)");
7192 TestDebugBreakInLoop("do {", loop_bodies, "} while (a == 1)");
7193
7194 TestDebugBreakInLoop("for (;;) {", loop_bodies, "}");
7195 TestDebugBreakInLoop("for (;a == 1;) {", loop_bodies, "}");
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007196
7197 // Get rid of the debug event listener.
7198 v8::Debug::SetDebugEventListener(NULL);
7199 CheckDebuggerUnloaded();
7200}
7201
7202
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007203#endif // ENABLE_DEBUGGER_SUPPORT