blob: d66d97f1337564287bcaf65572357b9523af0880 [file] [log] [blame]
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001// Copyright 2012 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();
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000140 context_.Dispose(context_->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000141 }
142 inline v8::Context* operator->() { return *context_; }
143 inline v8::Context* operator*() { return *context_; }
144 inline bool IsReady() { return !context_.IsEmpty(); }
145 void ExposeDebug() {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000146 v8::internal::Isolate* isolate =
147 reinterpret_cast<v8::internal::Isolate*>(context_->GetIsolate());
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000148 v8::internal::Debug* debug = isolate->debug();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000149 // Expose the debug context global object in the global object for testing.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000150 debug->Load();
151 debug->debug_context()->set_security_token(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000152 v8::Utils::OpenHandle(*context_)->security_token());
153
154 Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast(
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000155 v8::Utils::OpenHandle(*context_->Global())));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000156 Handle<v8::internal::String> debug_string =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000157 FACTORY->InternalizeOneByteString(STATIC_ASCII_VECTOR("debug"));
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000158 SetProperty(isolate, global, debug_string,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000159 Handle<Object>(debug->debug_context()->global_proxy(), isolate),
160 DONT_ENUM,
161 ::v8::internal::kNonStrictMode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000162 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000163
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000164 private:
165 v8::Persistent<v8::Context> context_;
166};
167
168
169// --- H e l p e r F u n c t i o n s
170
171
172// Compile and run the supplied source and return the fequested function.
173static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env,
174 const char* source,
175 const char* function_name) {
176 v8::Script::Compile(v8::String::New(source))->Run();
177 return v8::Local<v8::Function>::Cast(
178 (*env)->Global()->Get(v8::String::New(function_name)));
179}
180
ager@chromium.org9085a012009-05-11 19:22:57 +0000181
182// Compile and run the supplied source and return the requested function.
183static v8::Local<v8::Function> CompileFunction(const char* source,
184 const char* function_name) {
185 v8::Script::Compile(v8::String::New(source))->Run();
186 return v8::Local<v8::Function>::Cast(
187 v8::Context::GetCurrent()->Global()->Get(v8::String::New(function_name)));
188}
189
190
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000191// Is there any debug info for the function?
192static bool HasDebugInfo(v8::Handle<v8::Function> fun) {
193 Handle<v8::internal::JSFunction> f = v8::Utils::OpenHandle(*fun);
194 Handle<v8::internal::SharedFunctionInfo> shared(f->shared());
195 return Debug::HasDebugInfo(shared);
196}
197
198
199// Set a break point in a function and return the associated break point
200// number.
201static int SetBreakPoint(Handle<v8::internal::JSFunction> fun, int position) {
202 static int break_point = 0;
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000203 v8::internal::Isolate* isolate = fun->GetIsolate();
204 v8::internal::Debug* debug = isolate->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000205 debug->SetBreakPoint(
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000206 fun,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000207 Handle<Object>(v8::internal::Smi::FromInt(++break_point), isolate),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000208 &position);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000209 return break_point;
210}
211
212
213// Set a break point in a function and return the associated break point
214// number.
215static int SetBreakPoint(v8::Handle<v8::Function> fun, int position) {
216 return SetBreakPoint(v8::Utils::OpenHandle(*fun), position);
217}
218
219
220// Set a break point in a function using the Debug object and return the
221// associated break point number.
222static int SetBreakPointFromJS(const char* function_name,
223 int line, int position) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000224 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
225 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000226 "debug.Debug.setBreakPoint(%s,%d,%d)",
227 function_name, line, position);
228 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000229 v8::Handle<v8::String> str = v8::String::New(buffer.start());
230 return v8::Script::Compile(str)->Run()->Int32Value();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000231}
232
233
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000234// Set a break point in a script identified by id using the global Debug object.
235static int SetScriptBreakPointByIdFromJS(int script_id, int line, int column) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000236 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000237 if (column >= 0) {
238 // Column specified set script break point on precise location.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000239 OS::SNPrintF(buffer,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000240 "debug.Debug.setScriptBreakPointById(%d,%d,%d)",
241 script_id, line, column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000242 } else {
243 // Column not specified set script break point on line.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000244 OS::SNPrintF(buffer,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000245 "debug.Debug.setScriptBreakPointById(%d,%d)",
246 script_id, line);
247 }
248 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
249 {
250 v8::TryCatch try_catch;
251 v8::Handle<v8::String> str = v8::String::New(buffer.start());
252 v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000253 CHECK(!try_catch.HasCaught());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000254 return value->Int32Value();
255 }
256}
257
258
259// Set a break point in a script identified by name using the global Debug
260// object.
261static int SetScriptBreakPointByNameFromJS(const char* script_name,
262 int line, int column) {
263 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
264 if (column >= 0) {
265 // Column specified set script break point on precise location.
266 OS::SNPrintF(buffer,
267 "debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)",
268 script_name, line, column);
269 } else {
270 // Column not specified set script break point on line.
271 OS::SNPrintF(buffer,
272 "debug.Debug.setScriptBreakPointByName(\"%s\",%d)",
273 script_name, line);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000274 }
275 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000276 {
277 v8::TryCatch try_catch;
278 v8::Handle<v8::String> str = v8::String::New(buffer.start());
279 v8::Handle<v8::Value> value = v8::Script::Compile(str)->Run();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000280 CHECK(!try_catch.HasCaught());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000281 return value->Int32Value();
282 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000283}
284
285
286// Clear a break point.
287static void ClearBreakPoint(int break_point) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000288 v8::internal::Isolate* isolate = v8::internal::Isolate::Current();
289 v8::internal::Debug* debug = isolate->debug();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 debug->ClearBreakPoint(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000291 Handle<Object>(v8::internal::Smi::FromInt(break_point), isolate));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000292}
293
294
295// Clear a break point using the global Debug object.
296static void ClearBreakPointFromJS(int break_point_number) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000297 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
298 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000299 "debug.Debug.clearBreakPoint(%d)",
300 break_point_number);
301 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000302 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000303}
304
305
306static void EnableScriptBreakPointFromJS(int break_point_number) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000307 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
308 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000309 "debug.Debug.enableScriptBreakPoint(%d)",
310 break_point_number);
311 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000312 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000313}
314
315
316static void DisableScriptBreakPointFromJS(int break_point_number) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000317 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
318 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000319 "debug.Debug.disableScriptBreakPoint(%d)",
320 break_point_number);
321 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000322 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000323}
324
325
326static void ChangeScriptBreakPointConditionFromJS(int break_point_number,
327 const char* condition) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000328 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
329 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000330 "debug.Debug.changeScriptBreakPointCondition(%d, \"%s\")",
331 break_point_number, condition);
332 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000333 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000334}
335
336
337static void ChangeScriptBreakPointIgnoreCountFromJS(int break_point_number,
338 int ignoreCount) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000339 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
340 OS::SNPrintF(buffer,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000341 "debug.Debug.changeScriptBreakPointIgnoreCount(%d, %d)",
342 break_point_number, ignoreCount);
343 buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000344 v8::Script::Compile(v8::String::New(buffer.start()))->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000345}
346
347
348// Change break on exception.
349static void ChangeBreakOnException(bool caught, bool uncaught) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000350 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
351 debug->ChangeBreakOnException(v8::internal::BreakException, caught);
352 debug->ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000353}
354
355
356// Change break on exception using the global Debug object.
357static void ChangeBreakOnExceptionFromJS(bool caught, bool uncaught) {
358 if (caught) {
359 v8::Script::Compile(
360 v8::String::New("debug.Debug.setBreakOnException()"))->Run();
361 } else {
362 v8::Script::Compile(
363 v8::String::New("debug.Debug.clearBreakOnException()"))->Run();
364 }
365 if (uncaught) {
366 v8::Script::Compile(
367 v8::String::New("debug.Debug.setBreakOnUncaughtException()"))->Run();
368 } else {
369 v8::Script::Compile(
370 v8::String::New("debug.Debug.clearBreakOnUncaughtException()"))->Run();
371 }
372}
373
374
375// Prepare to step to next break location.
376static void PrepareStep(StepAction step_action) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000377 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
378 debug->PrepareStep(step_action, 1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000379}
380
381
382// This function is in namespace v8::internal to be friend with class
383// v8::internal::Debug.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000384namespace v8 {
385namespace internal {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000386
387// Collect the currently debugged functions.
388Handle<FixedArray> GetDebuggedFunctions() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000389 Debug* debug = Isolate::Current()->debug();
390
391 v8::internal::DebugInfoListNode* node = debug->debug_info_list_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000392
393 // Find the number of debugged functions.
394 int count = 0;
395 while (node) {
396 count++;
397 node = node->next();
398 }
399
400 // Allocate array for the debugged functions
401 Handle<FixedArray> debugged_functions =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000402 FACTORY->NewFixedArray(count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000403
404 // Run through the debug info objects and collect all functions.
405 count = 0;
406 while (node) {
407 debugged_functions->set(count++, *node->debug_info());
408 node = node->next();
409 }
410
411 return debugged_functions;
412}
413
414
415static Handle<Code> ComputeCallDebugBreak(int argc) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000416 return Isolate::Current()->stub_cache()->ComputeCallDebugBreak(argc,
417 Code::CALL_IC);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000418}
419
ager@chromium.org381abbb2009-02-25 13:23:22 +0000420
ager@chromium.org381abbb2009-02-25 13:23:22 +0000421// Check that the debugger has been fully unloaded.
422void CheckDebuggerUnloaded(bool check_functions) {
423 // Check that the debugger context is cleared and that there is no debug
424 // information stored for the debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000425 CHECK(Isolate::Current()->debug()->debug_context().is_null());
426 CHECK_EQ(NULL, Isolate::Current()->debug()->debug_info_list_);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000427
428 // Collect garbage to ensure weak handles are cleared.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000429 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
430 HEAP->CollectAllGarbage(Heap::kMakeHeapIterableMask);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000431
432 // Iterate the head and check that there are no debugger related objects left.
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000433 HeapIterator iterator(HEAP);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000434 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000435 CHECK(!obj->IsDebugInfo());
436 CHECK(!obj->IsBreakPointInfo());
437
438 // If deep check of functions is requested check that no debug break code
439 // is left in all functions.
440 if (check_functions) {
441 if (obj->IsJSFunction()) {
442 JSFunction* fun = JSFunction::cast(obj);
443 for (RelocIterator it(fun->shared()->code()); !it.done(); it.next()) {
444 RelocInfo::Mode rmode = it.rinfo()->rmode();
445 if (RelocInfo::IsCodeTarget(rmode)) {
446 CHECK(!Debug::IsDebugBreak(it.rinfo()->target_address()));
447 } else if (RelocInfo::IsJSReturn(rmode)) {
448 CHECK(!Debug::IsDebugBreakAtReturn(it.rinfo()));
449 }
450 }
451 }
452 }
453 }
454}
455
456
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000457void ForceUnloadDebugger() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000458 Isolate::Current()->debugger()->never_unload_debugger_ = false;
459 Isolate::Current()->debugger()->UnloadDebugger();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000460}
461
462
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000463} } // namespace v8::internal
464
ager@chromium.org381abbb2009-02-25 13:23:22 +0000465
ager@chromium.org381abbb2009-02-25 13:23:22 +0000466// Check that the debugger has been fully unloaded.
467static void CheckDebuggerUnloaded(bool check_functions = false) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000468 // Let debugger to unload itself synchronously
469 v8::Debug::ProcessDebugMessages();
470
ager@chromium.org381abbb2009-02-25 13:23:22 +0000471 v8::internal::CheckDebuggerUnloaded(check_functions);
472}
473
474
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000475// Inherit from BreakLocationIterator to get access to protected parts for
476// testing.
477class TestBreakLocationIterator: public v8::internal::BreakLocationIterator {
478 public:
479 explicit TestBreakLocationIterator(Handle<v8::internal::DebugInfo> debug_info)
480 : BreakLocationIterator(debug_info, v8::internal::SOURCE_BREAK_LOCATIONS) {}
481 v8::internal::RelocIterator* it() { return reloc_iterator_; }
482 v8::internal::RelocIterator* it_original() {
483 return reloc_iterator_original_;
484 }
485};
486
487
488// Compile a function, set a break point and check that the call at the break
489// location in the code is the expected debug_break function.
490void CheckDebugBreakFunction(DebugLocalContext* env,
491 const char* source, const char* name,
ager@chromium.org236ad962008-09-25 09:45:57 +0000492 int position, v8::internal::RelocInfo::Mode mode,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000493 Code* debug_break) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000494 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
495
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000496 // Create function and set the break point.
497 Handle<v8::internal::JSFunction> fun = v8::Utils::OpenHandle(
498 *CompileFunction(env, source, name));
499 int bp = SetBreakPoint(fun, position);
500
501 // Check that the debug break function is as expected.
502 Handle<v8::internal::SharedFunctionInfo> shared(fun->shared());
503 CHECK(Debug::HasDebugInfo(shared));
504 TestBreakLocationIterator it1(Debug::GetDebugInfo(shared));
505 it1.FindBreakLocationFromPosition(position);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000506 v8::internal::RelocInfo::Mode actual_mode = it1.it()->rinfo()->rmode();
507 if (actual_mode == v8::internal::RelocInfo::CODE_TARGET_WITH_ID) {
508 actual_mode = v8::internal::RelocInfo::CODE_TARGET;
509 }
510 CHECK_EQ(mode, actual_mode);
ager@chromium.org236ad962008-09-25 09:45:57 +0000511 if (mode != v8::internal::RelocInfo::JS_RETURN) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000512 CHECK_EQ(debug_break,
ager@chromium.org8bb60582008-12-11 12:02:20 +0000513 Code::GetCodeFromTargetAddress(it1.it()->rinfo()->target_address()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000514 } else {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000515 CHECK(Debug::IsDebugBreakAtReturn(it1.it()->rinfo()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000516 }
517
518 // Clear the break point and check that the debug break function is no longer
519 // there
520 ClearBreakPoint(bp);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000521 CHECK(!debug->HasDebugInfo(shared));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000522 CHECK(debug->EnsureDebugInfo(shared, fun));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000523 TestBreakLocationIterator it2(Debug::GetDebugInfo(shared));
524 it2.FindBreakLocationFromPosition(position);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000525 actual_mode = it2.it()->rinfo()->rmode();
526 if (actual_mode == v8::internal::RelocInfo::CODE_TARGET_WITH_ID) {
527 actual_mode = v8::internal::RelocInfo::CODE_TARGET;
528 }
529 CHECK_EQ(mode, actual_mode);
ager@chromium.org236ad962008-09-25 09:45:57 +0000530 if (mode == v8::internal::RelocInfo::JS_RETURN) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000531 CHECK(!Debug::IsDebugBreakAtReturn(it2.it()->rinfo()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000532 }
533}
534
535
536// --- D e b u g E v e n t H a n d l e r s
537// ---
538// --- The different tests uses a number of debug event handlers.
539// ---
540
541
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000542// Source for the JavaScript function which picks out the function
543// name of a frame.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000544const char* frame_function_name_source =
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000545 "function frame_function_name(exec_state, frame_number) {"
546 " return exec_state.frame(frame_number).func().name();"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000547 "}";
548v8::Local<v8::Function> frame_function_name;
549
ager@chromium.org8bb60582008-12-11 12:02:20 +0000550
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000551// Source for the JavaScript function which pick out the name of the
552// first argument of a frame.
553const char* frame_argument_name_source =
554 "function frame_argument_name(exec_state, frame_number) {"
555 " return exec_state.frame(frame_number).argumentName(0);"
556 "}";
557v8::Local<v8::Function> frame_argument_name;
558
559
560// Source for the JavaScript function which pick out the value of the
561// first argument of a frame.
562const char* frame_argument_value_source =
563 "function frame_argument_value(exec_state, frame_number) {"
564 " return exec_state.frame(frame_number).argumentValue(0).value_;"
565 "}";
566v8::Local<v8::Function> frame_argument_value;
567
568
569// Source for the JavaScript function which pick out the name of the
570// first argument of a frame.
571const char* frame_local_name_source =
572 "function frame_local_name(exec_state, frame_number) {"
573 " return exec_state.frame(frame_number).localName(0);"
574 "}";
575v8::Local<v8::Function> frame_local_name;
576
577
578// Source for the JavaScript function which pick out the value of the
579// first argument of a frame.
580const char* frame_local_value_source =
581 "function frame_local_value(exec_state, frame_number) {"
582 " return exec_state.frame(frame_number).localValue(0).value_;"
583 "}";
584v8::Local<v8::Function> frame_local_value;
585
586
587// Source for the JavaScript function which picks out the source line for the
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000588// top frame.
589const char* frame_source_line_source =
590 "function frame_source_line(exec_state) {"
591 " return exec_state.frame(0).sourceLine();"
592 "}";
593v8::Local<v8::Function> frame_source_line;
594
595
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000596// Source for the JavaScript function which picks out the source column for the
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000597// top frame.
598const char* frame_source_column_source =
599 "function frame_source_column(exec_state) {"
600 " return exec_state.frame(0).sourceColumn();"
601 "}";
602v8::Local<v8::Function> frame_source_column;
603
604
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000605// Source for the JavaScript function which picks out the script name for the
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000606// top frame.
607const char* frame_script_name_source =
608 "function frame_script_name(exec_state) {"
609 " return exec_state.frame(0).func().script().name();"
610 "}";
611v8::Local<v8::Function> frame_script_name;
612
613
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000614// Source for the JavaScript function which picks out the script data for the
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000615// top frame.
616const char* frame_script_data_source =
617 "function frame_script_data(exec_state) {"
618 " return exec_state.frame(0).func().script().data();"
619 "}";
620v8::Local<v8::Function> frame_script_data;
621
622
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000623// Source for the JavaScript function which picks out the script data from
ager@chromium.org5c838252010-02-19 08:53:10 +0000624// AfterCompile event
625const char* compiled_script_data_source =
626 "function compiled_script_data(event_data) {"
627 " return event_data.script().data();"
628 "}";
629v8::Local<v8::Function> compiled_script_data;
630
631
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000632// Source for the JavaScript function which returns the number of frames.
ager@chromium.org8bb60582008-12-11 12:02:20 +0000633static const char* frame_count_source =
634 "function frame_count(exec_state) {"
635 " return exec_state.frameCount();"
636 "}";
637v8::Handle<v8::Function> frame_count;
638
639
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000640// Global variable to store the last function hit - used by some tests.
641char last_function_hit[80];
642
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000643// Global variable to store the name and data for last script hit - used by some
644// tests.
645char last_script_name_hit[80];
646char last_script_data_hit[80];
647
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000648// Global variables to store the last source position - used by some tests.
649int last_source_line = -1;
650int last_source_column = -1;
651
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000652// Debug event handler which counts the break points which have been hit.
653int break_point_hit_count = 0;
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000654int break_point_hit_count_deoptimize = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000655static void DebugEventBreakPointHitCount(v8::DebugEvent event,
656 v8::Handle<v8::Object> exec_state,
657 v8::Handle<v8::Object> event_data,
658 v8::Handle<v8::Value> data) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000659 v8::internal::Isolate* isolate = v8::internal::Isolate::Current();
660 Debug* debug = isolate->debug();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000661 // When hitting a debug event listener there must be a break set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000662 CHECK_NE(debug->break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000663
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000664 // Count the number of breaks.
665 if (event == v8::Break) {
666 break_point_hit_count++;
667 if (!frame_function_name.IsEmpty()) {
668 // Get the name of the function.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000669 const int argc = 2;
670 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) };
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000671 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
672 argc, argv);
673 if (result->IsUndefined()) {
674 last_function_hit[0] = '\0';
675 } else {
676 CHECK(result->IsString());
677 v8::Handle<v8::String> function_name(result->ToString());
678 function_name->WriteAscii(last_function_hit);
679 }
680 }
kasperl@chromium.org2d18d102009-04-15 13:27:32 +0000681
682 if (!frame_source_line.IsEmpty()) {
683 // Get the source line.
684 const int argc = 1;
685 v8::Handle<v8::Value> argv[argc] = { exec_state };
686 v8::Handle<v8::Value> result = frame_source_line->Call(exec_state,
687 argc, argv);
688 CHECK(result->IsNumber());
689 last_source_line = result->Int32Value();
690 }
691
692 if (!frame_source_column.IsEmpty()) {
693 // Get the source column.
694 const int argc = 1;
695 v8::Handle<v8::Value> argv[argc] = { exec_state };
696 v8::Handle<v8::Value> result = frame_source_column->Call(exec_state,
697 argc, argv);
698 CHECK(result->IsNumber());
699 last_source_column = result->Int32Value();
700 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000701
702 if (!frame_script_name.IsEmpty()) {
703 // Get the script name of the function script.
704 const int argc = 1;
705 v8::Handle<v8::Value> argv[argc] = { exec_state };
706 v8::Handle<v8::Value> result = frame_script_name->Call(exec_state,
707 argc, argv);
708 if (result->IsUndefined()) {
709 last_script_name_hit[0] = '\0';
710 } else {
711 CHECK(result->IsString());
712 v8::Handle<v8::String> script_name(result->ToString());
713 script_name->WriteAscii(last_script_name_hit);
714 }
715 }
716
717 if (!frame_script_data.IsEmpty()) {
718 // Get the script data of the function script.
719 const int argc = 1;
720 v8::Handle<v8::Value> argv[argc] = { exec_state };
721 v8::Handle<v8::Value> result = frame_script_data->Call(exec_state,
722 argc, argv);
723 if (result->IsUndefined()) {
724 last_script_data_hit[0] = '\0';
725 } else {
726 result = result->ToString();
727 CHECK(result->IsString());
728 v8::Handle<v8::String> script_data(result->ToString());
729 script_data->WriteAscii(last_script_data_hit);
730 }
731 }
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000732
733 // Perform a full deoptimization when the specified number of
734 // breaks have been hit.
735 if (break_point_hit_count == break_point_hit_count_deoptimize) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000736 i::Deoptimizer::DeoptimizeAll(isolate);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000737 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000738 } else if (event == v8::AfterCompile && !compiled_script_data.IsEmpty()) {
739 const int argc = 1;
740 v8::Handle<v8::Value> argv[argc] = { event_data };
741 v8::Handle<v8::Value> result = compiled_script_data->Call(exec_state,
742 argc, argv);
743 if (result->IsUndefined()) {
744 last_script_data_hit[0] = '\0';
745 } else {
746 result = result->ToString();
747 CHECK(result->IsString());
748 v8::Handle<v8::String> script_data(result->ToString());
749 script_data->WriteAscii(last_script_data_hit);
750 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000751 }
752}
753
754
ager@chromium.org8bb60582008-12-11 12:02:20 +0000755// Debug event handler which counts a number of events and collects the stack
756// height if there is a function compiled for that.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000757int exception_hit_count = 0;
758int uncaught_exception_hit_count = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000759int last_js_stack_height = -1;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000760
761static void DebugEventCounterClear() {
762 break_point_hit_count = 0;
763 exception_hit_count = 0;
764 uncaught_exception_hit_count = 0;
765}
766
767static void DebugEventCounter(v8::DebugEvent event,
768 v8::Handle<v8::Object> exec_state,
769 v8::Handle<v8::Object> event_data,
770 v8::Handle<v8::Value> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000771 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
772
ager@chromium.org8bb60582008-12-11 12:02:20 +0000773 // When hitting a debug event listener there must be a break set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000774 CHECK_NE(debug->break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000775
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000776 // Count the number of breaks.
777 if (event == v8::Break) {
778 break_point_hit_count++;
779 } else if (event == v8::Exception) {
780 exception_hit_count++;
781
782 // Check whether the exception was uncaught.
783 v8::Local<v8::String> fun_name = v8::String::New("uncaught");
784 v8::Local<v8::Function> fun =
785 v8::Function::Cast(*event_data->Get(fun_name));
786 v8::Local<v8::Value> result = *fun->Call(event_data, 0, NULL);
787 if (result->IsTrue()) {
788 uncaught_exception_hit_count++;
789 }
790 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000791
792 // Collect the JavsScript stack height if the function frame_count is
793 // compiled.
794 if (!frame_count.IsEmpty()) {
795 static const int kArgc = 1;
796 v8::Handle<v8::Value> argv[kArgc] = { exec_state };
797 // Using exec_state as receiver is just to have a receiver.
798 v8::Handle<v8::Value> result = frame_count->Call(exec_state, kArgc, argv);
799 last_js_stack_height = result->Int32Value();
800 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000801}
802
803
804// Debug event handler which evaluates a number of expressions when a break
805// point is hit. Each evaluated expression is compared with an expected value.
806// For this debug event handler to work the following two global varaibles
807// must be initialized.
808// checks: An array of expressions and expected results
809// evaluate_check_function: A JavaScript function (see below)
810
811// Structure for holding checks to do.
812struct EvaluateCheck {
813 const char* expr; // An expression to evaluate when a break point is hit.
814 v8::Handle<v8::Value> expected; // The expected result.
815};
816// Array of checks to do.
817struct EvaluateCheck* checks = NULL;
818// Source for The JavaScript function which can do the evaluation when a break
819// point is hit.
820const char* evaluate_check_source =
821 "function evaluate_check(exec_state, expr, expected) {"
822 " return exec_state.frame(0).evaluate(expr).value() === expected;"
823 "}";
824v8::Local<v8::Function> evaluate_check_function;
825
826// The actual debug event described by the longer comment above.
827static void DebugEventEvaluate(v8::DebugEvent event,
828 v8::Handle<v8::Object> exec_state,
829 v8::Handle<v8::Object> event_data,
830 v8::Handle<v8::Value> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000831 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000832 // When hitting a debug event listener there must be a break set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000833 CHECK_NE(debug->break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000834
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000835 if (event == v8::Break) {
836 for (int i = 0; checks[i].expr != NULL; i++) {
837 const int argc = 3;
838 v8::Handle<v8::Value> argv[argc] = { exec_state,
839 v8::String::New(checks[i].expr),
840 checks[i].expected };
841 v8::Handle<v8::Value> result =
842 evaluate_check_function->Call(exec_state, argc, argv);
843 if (!result->IsTrue()) {
844 v8::String::AsciiValue ascii(checks[i].expected->ToString());
845 V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *ascii);
846 }
847 }
848 }
849}
850
851
852// This debug event listener removes a breakpoint in a function
853int debug_event_remove_break_point = 0;
854static void DebugEventRemoveBreakPoint(v8::DebugEvent event,
855 v8::Handle<v8::Object> exec_state,
856 v8::Handle<v8::Object> event_data,
857 v8::Handle<v8::Value> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000858 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000859 // When hitting a debug event listener there must be a break set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000860 CHECK_NE(debug->break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000861
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000862 if (event == v8::Break) {
863 break_point_hit_count++;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000864 CHECK(data->IsFunction());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000865 ClearBreakPoint(debug_event_remove_break_point);
866 }
867}
868
869
870// Debug event handler which counts break points hit and performs a step
871// afterwards.
872StepAction step_action = StepIn; // Step action to perform when stepping.
873static void DebugEventStep(v8::DebugEvent event,
874 v8::Handle<v8::Object> exec_state,
875 v8::Handle<v8::Object> event_data,
876 v8::Handle<v8::Value> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000877 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000878 // When hitting a debug event listener there must be a break set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000879 CHECK_NE(debug->break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000880
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000881 if (event == v8::Break) {
882 break_point_hit_count++;
883 PrepareStep(step_action);
884 }
885}
886
887
888// Debug event handler which counts break points hit and performs a step
889// afterwards. For each call the expected function is checked.
890// For this debug event handler to work the following two global varaibles
891// must be initialized.
892// expected_step_sequence: An array of the expected function call sequence.
893// frame_function_name: A JavaScript function (see below).
894
895// String containing the expected function call sequence. Note: this only works
896// if functions have name length of one.
897const char* expected_step_sequence = NULL;
898
899// The actual debug event described by the longer comment above.
900static void DebugEventStepSequence(v8::DebugEvent event,
901 v8::Handle<v8::Object> exec_state,
902 v8::Handle<v8::Object> event_data,
903 v8::Handle<v8::Value> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000904 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000905 // When hitting a debug event listener there must be a break set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000906 CHECK_NE(debug->break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000907
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000908 if (event == v8::Break || event == v8::Exception) {
909 // Check that the current function is the expected.
910 CHECK(break_point_hit_count <
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000911 StrLength(expected_step_sequence));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000912 const int argc = 2;
913 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) };
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000914 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
915 argc, argv);
916 CHECK(result->IsString());
917 v8::String::AsciiValue function_name(result->ToString());
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000918 CHECK_EQ(1, StrLength(*function_name));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000919 CHECK_EQ((*function_name)[0],
920 expected_step_sequence[break_point_hit_count]);
921
922 // Perform step.
923 break_point_hit_count++;
924 PrepareStep(step_action);
925 }
926}
927
928
929// Debug event handler which performs a garbage collection.
930static void DebugEventBreakPointCollectGarbage(
931 v8::DebugEvent event,
932 v8::Handle<v8::Object> exec_state,
933 v8::Handle<v8::Object> event_data,
934 v8::Handle<v8::Value> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000935 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000936 // When hitting a debug event listener there must be a break set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000937 CHECK_NE(debug->break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000938
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000939 // Perform a garbage collection when break point is hit and continue. Based
940 // on the number of break points hit either scavenge or mark compact
941 // collector is used.
942 if (event == v8::Break) {
943 break_point_hit_count++;
944 if (break_point_hit_count % 2 == 0) {
945 // Scavenge.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000946 HEAP->CollectGarbage(v8::internal::NEW_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000947 } else {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000948 // Mark sweep compact.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000949 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000950 }
951 }
952}
953
954
955// Debug event handler which re-issues a debug break and calls the garbage
956// collector to have the heap verified.
957static void DebugEventBreak(v8::DebugEvent event,
958 v8::Handle<v8::Object> exec_state,
959 v8::Handle<v8::Object> event_data,
960 v8::Handle<v8::Value> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000961 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000962 // When hitting a debug event listener there must be a break set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000963 CHECK_NE(debug->break_id(), 0);
ager@chromium.org8bb60582008-12-11 12:02:20 +0000964
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000965 if (event == v8::Break) {
966 // Count the number of breaks.
967 break_point_hit_count++;
968
969 // Run the garbage collector to enforce heap verification if option
970 // --verify-heap is set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000971 HEAP->CollectGarbage(v8::internal::NEW_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000972
973 // Set the break flag again to come back here as soon as possible.
974 v8::Debug::DebugBreak();
975 }
976}
977
978
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000979// Debug event handler which re-issues a debug break until a limit has been
980// reached.
981int max_break_point_hit_count = 0;
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000982bool terminate_after_max_break_point_hit = false;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000983static void DebugEventBreakMax(v8::DebugEvent event,
984 v8::Handle<v8::Object> exec_state,
985 v8::Handle<v8::Object> event_data,
986 v8::Handle<v8::Value> data) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000987 v8::internal::Isolate* isolate = v8::internal::Isolate::Current();
988 v8::internal::Debug* debug = isolate->debug();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000989 // When hitting a debug event listener there must be a break set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000990 CHECK_NE(debug->break_id(), 0);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000991
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000992 if (event == v8::Break) {
993 if (break_point_hit_count < max_break_point_hit_count) {
994 // Count the number of breaks.
995 break_point_hit_count++;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000996
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000997 // Collect the JavsScript stack height if the function frame_count is
998 // compiled.
999 if (!frame_count.IsEmpty()) {
1000 static const int kArgc = 1;
1001 v8::Handle<v8::Value> argv[kArgc] = { exec_state };
1002 // Using exec_state as receiver is just to have a receiver.
1003 v8::Handle<v8::Value> result =
1004 frame_count->Call(exec_state, kArgc, argv);
1005 last_js_stack_height = result->Int32Value();
1006 }
1007
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001008 // Set the break flag again to come back here as soon as possible.
1009 v8::Debug::DebugBreak();
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001010
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001011 } else if (terminate_after_max_break_point_hit) {
1012 // Terminate execution after the last break if requested.
1013 v8::V8::TerminateExecution();
1014 }
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001015
1016 // Perform a full deoptimization when the specified number of
1017 // breaks have been hit.
1018 if (break_point_hit_count == break_point_hit_count_deoptimize) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00001019 i::Deoptimizer::DeoptimizeAll(isolate);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001020 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001021 }
1022}
1023
1024
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001025// --- M e s s a g e C a l l b a c k
1026
1027
1028// Message callback which counts the number of messages.
1029int message_callback_count = 0;
1030
1031static void MessageCallbackCountClear() {
1032 message_callback_count = 0;
1033}
1034
1035static void MessageCallbackCount(v8::Handle<v8::Message> message,
1036 v8::Handle<v8::Value> data) {
1037 message_callback_count++;
1038}
1039
1040
1041// --- T h e A c t u a l T e s t s
1042
1043
1044// Test that the debug break function is the expected one for different kinds
1045// of break locations.
1046TEST(DebugStub) {
1047 using ::v8::internal::Builtins;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001048 using ::v8::internal::Isolate;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001049 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001050 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001051
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001052 CheckDebugBreakFunction(&env,
1053 "function f1(){}", "f1",
1054 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001055 v8::internal::RelocInfo::JS_RETURN,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001056 NULL);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001057 CheckDebugBreakFunction(&env,
1058 "function f2(){x=1;}", "f2",
1059 0,
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001060 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001061 Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001062 Builtins::kStoreIC_DebugBreak));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001063 CheckDebugBreakFunction(&env,
1064 "function f3(){var a=x;}", "f3",
1065 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001066 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001067 Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001068 Builtins::kLoadIC_DebugBreak));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001069
ager@chromium.org236ad962008-09-25 09:45:57 +00001070// TODO(1240753): Make the test architecture independent or split
1071// parts of the debugger into architecture dependent files. This
1072// part currently disabled as it is not portable between IA32/ARM.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001073// Currently on ICs for keyed store/load on ARM.
1074#if !defined (__arm__) && !defined(__thumb__)
1075 CheckDebugBreakFunction(
1076 &env,
1077 "function f4(){var index='propertyName'; var a={}; a[index] = 'x';}",
1078 "f4",
1079 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001080 v8::internal::RelocInfo::CODE_TARGET,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001081 Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001082 Builtins::kKeyedStoreIC_DebugBreak));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001083 CheckDebugBreakFunction(
1084 &env,
1085 "function f5(){var index='propertyName'; var a={}; return a[index];}",
1086 "f5",
1087 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001088 v8::internal::RelocInfo::CODE_TARGET,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001089 Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001090 Builtins::kKeyedLoadIC_DebugBreak));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001091#endif
1092
1093 // Check the debug break code stubs for call ICs with different number of
1094 // parameters.
1095 Handle<Code> debug_break_0 = v8::internal::ComputeCallDebugBreak(0);
1096 Handle<Code> debug_break_1 = v8::internal::ComputeCallDebugBreak(1);
1097 Handle<Code> debug_break_4 = v8::internal::ComputeCallDebugBreak(4);
1098
1099 CheckDebugBreakFunction(&env,
1100 "function f4_0(){x();}", "f4_0",
1101 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001102 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001103 *debug_break_0);
1104
1105 CheckDebugBreakFunction(&env,
1106 "function f4_1(){x(1);}", "f4_1",
1107 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001108 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001109 *debug_break_1);
1110
1111 CheckDebugBreakFunction(&env,
1112 "function f4_4(){x(1,2,3,4);}", "f4_4",
1113 0,
ager@chromium.org236ad962008-09-25 09:45:57 +00001114 v8::internal::RelocInfo::CODE_TARGET_CONTEXT,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001115 *debug_break_4);
1116}
1117
1118
1119// Test that the debug info in the VM is in sync with the functions being
1120// debugged.
1121TEST(DebugInfo) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001122 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001123 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001124 // Create a couple of functions for the test.
1125 v8::Local<v8::Function> foo =
1126 CompileFunction(&env, "function foo(){}", "foo");
1127 v8::Local<v8::Function> bar =
1128 CompileFunction(&env, "function bar(){}", "bar");
1129 // Initially no functions are debugged.
1130 CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
1131 CHECK(!HasDebugInfo(foo));
1132 CHECK(!HasDebugInfo(bar));
1133 // One function (foo) is debugged.
1134 int bp1 = SetBreakPoint(foo, 0);
1135 CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
1136 CHECK(HasDebugInfo(foo));
1137 CHECK(!HasDebugInfo(bar));
1138 // Two functions are debugged.
1139 int bp2 = SetBreakPoint(bar, 0);
1140 CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length());
1141 CHECK(HasDebugInfo(foo));
1142 CHECK(HasDebugInfo(bar));
1143 // One function (bar) is debugged.
1144 ClearBreakPoint(bp1);
1145 CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
1146 CHECK(!HasDebugInfo(foo));
1147 CHECK(HasDebugInfo(bar));
1148 // No functions are debugged.
1149 ClearBreakPoint(bp2);
1150 CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
1151 CHECK(!HasDebugInfo(foo));
1152 CHECK(!HasDebugInfo(bar));
1153}
1154
1155
1156// Test that a break point can be set at an IC store location.
1157TEST(BreakPointICStore) {
1158 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001159 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001160 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org381abbb2009-02-25 13:23:22 +00001161
iposva@chromium.org245aa852009-02-10 00:49:54 +00001162 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001163 v8::Undefined());
1164 v8::Script::Compile(v8::String::New("function foo(){bar=0;}"))->Run();
1165 v8::Local<v8::Function> foo =
1166 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1167
1168 // Run without breakpoints.
1169 foo->Call(env->Global(), 0, NULL);
1170 CHECK_EQ(0, break_point_hit_count);
1171
1172 // Run with breakpoint
1173 int bp = SetBreakPoint(foo, 0);
1174 foo->Call(env->Global(), 0, NULL);
1175 CHECK_EQ(1, break_point_hit_count);
1176 foo->Call(env->Global(), 0, NULL);
1177 CHECK_EQ(2, break_point_hit_count);
1178
1179 // Run without breakpoints.
1180 ClearBreakPoint(bp);
1181 foo->Call(env->Global(), 0, NULL);
1182 CHECK_EQ(2, break_point_hit_count);
1183
iposva@chromium.org245aa852009-02-10 00:49:54 +00001184 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001185 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001186}
1187
1188
1189// Test that a break point can be set at an IC load location.
1190TEST(BreakPointICLoad) {
1191 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001192 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001193 v8::HandleScope scope(env->GetIsolate());
iposva@chromium.org245aa852009-02-10 00:49:54 +00001194 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001195 v8::Undefined());
1196 v8::Script::Compile(v8::String::New("bar=1"))->Run();
1197 v8::Script::Compile(v8::String::New("function foo(){var x=bar;}"))->Run();
1198 v8::Local<v8::Function> foo =
1199 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1200
1201 // Run without breakpoints.
1202 foo->Call(env->Global(), 0, NULL);
1203 CHECK_EQ(0, break_point_hit_count);
1204
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001205 // Run with breakpoint.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001206 int bp = SetBreakPoint(foo, 0);
1207 foo->Call(env->Global(), 0, NULL);
1208 CHECK_EQ(1, break_point_hit_count);
1209 foo->Call(env->Global(), 0, NULL);
1210 CHECK_EQ(2, break_point_hit_count);
1211
1212 // Run without breakpoints.
1213 ClearBreakPoint(bp);
1214 foo->Call(env->Global(), 0, NULL);
1215 CHECK_EQ(2, break_point_hit_count);
1216
iposva@chromium.org245aa852009-02-10 00:49:54 +00001217 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001218 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001219}
1220
1221
1222// Test that a break point can be set at an IC call location.
1223TEST(BreakPointICCall) {
1224 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001225 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001226 v8::HandleScope scope(env->GetIsolate());
iposva@chromium.org245aa852009-02-10 00:49:54 +00001227 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001228 v8::Undefined());
1229 v8::Script::Compile(v8::String::New("function bar(){}"))->Run();
1230 v8::Script::Compile(v8::String::New("function foo(){bar();}"))->Run();
1231 v8::Local<v8::Function> foo =
1232 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1233
1234 // Run without breakpoints.
1235 foo->Call(env->Global(), 0, NULL);
1236 CHECK_EQ(0, break_point_hit_count);
1237
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001238 // Run with breakpoint
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001239 int bp = SetBreakPoint(foo, 0);
1240 foo->Call(env->Global(), 0, NULL);
1241 CHECK_EQ(1, break_point_hit_count);
1242 foo->Call(env->Global(), 0, NULL);
1243 CHECK_EQ(2, break_point_hit_count);
1244
1245 // Run without breakpoints.
1246 ClearBreakPoint(bp);
1247 foo->Call(env->Global(), 0, NULL);
1248 CHECK_EQ(2, break_point_hit_count);
1249
iposva@chromium.org245aa852009-02-10 00:49:54 +00001250 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001251 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001252}
1253
1254
ricow@chromium.org65fae842010-08-25 15:26:24 +00001255// Test that a break point can be set at an IC call location and survive a GC.
1256TEST(BreakPointICCallWithGC) {
1257 break_point_hit_count = 0;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001258 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001259 v8::HandleScope scope(env->GetIsolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001260 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage,
1261 v8::Undefined());
1262 v8::Script::Compile(v8::String::New("function bar(){return 1;}"))->Run();
1263 v8::Script::Compile(v8::String::New("function foo(){return bar();}"))->Run();
1264 v8::Local<v8::Function> foo =
1265 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1266
1267 // Run without breakpoints.
1268 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1269 CHECK_EQ(0, break_point_hit_count);
1270
1271 // Run with breakpoint.
1272 int bp = SetBreakPoint(foo, 0);
1273 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1274 CHECK_EQ(1, break_point_hit_count);
1275 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1276 CHECK_EQ(2, break_point_hit_count);
1277
1278 // Run without breakpoints.
1279 ClearBreakPoint(bp);
1280 foo->Call(env->Global(), 0, NULL);
1281 CHECK_EQ(2, break_point_hit_count);
1282
1283 v8::Debug::SetDebugEventListener(NULL);
1284 CheckDebuggerUnloaded();
1285}
1286
1287
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001288// Test that a break point can be set at an IC call location and survive a GC.
1289TEST(BreakPointConstructCallWithGC) {
1290 break_point_hit_count = 0;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001291 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001292 v8::HandleScope scope(env->GetIsolate());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001293 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage,
1294 v8::Undefined());
1295 v8::Script::Compile(v8::String::New("function bar(){ this.x = 1;}"))->Run();
1296 v8::Script::Compile(v8::String::New(
1297 "function foo(){return new bar(1).x;}"))->Run();
1298 v8::Local<v8::Function> foo =
1299 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1300
1301 // Run without breakpoints.
1302 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1303 CHECK_EQ(0, break_point_hit_count);
1304
1305 // Run with breakpoint.
1306 int bp = SetBreakPoint(foo, 0);
1307 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1308 CHECK_EQ(1, break_point_hit_count);
1309 CHECK_EQ(1, foo->Call(env->Global(), 0, NULL)->Int32Value());
1310 CHECK_EQ(2, break_point_hit_count);
1311
1312 // Run without breakpoints.
1313 ClearBreakPoint(bp);
1314 foo->Call(env->Global(), 0, NULL);
1315 CHECK_EQ(2, break_point_hit_count);
1316
1317 v8::Debug::SetDebugEventListener(NULL);
1318 CheckDebuggerUnloaded();
1319}
1320
1321
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001322// Test that a break point can be set at a return store location.
1323TEST(BreakPointReturn) {
1324 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001325 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001326 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001327
1328 // Create a functions for checking the source line and column when hitting
1329 // a break point.
1330 frame_source_line = CompileFunction(&env,
1331 frame_source_line_source,
1332 "frame_source_line");
1333 frame_source_column = CompileFunction(&env,
1334 frame_source_column_source,
1335 "frame_source_column");
1336
1337
iposva@chromium.org245aa852009-02-10 00:49:54 +00001338 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001339 v8::Undefined());
1340 v8::Script::Compile(v8::String::New("function foo(){}"))->Run();
1341 v8::Local<v8::Function> foo =
1342 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
1343
1344 // Run without breakpoints.
1345 foo->Call(env->Global(), 0, NULL);
1346 CHECK_EQ(0, break_point_hit_count);
1347
1348 // Run with breakpoint
1349 int bp = SetBreakPoint(foo, 0);
1350 foo->Call(env->Global(), 0, NULL);
1351 CHECK_EQ(1, break_point_hit_count);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001352 CHECK_EQ(0, last_source_line);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001353 CHECK_EQ(15, last_source_column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001354 foo->Call(env->Global(), 0, NULL);
1355 CHECK_EQ(2, break_point_hit_count);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00001356 CHECK_EQ(0, last_source_line);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001357 CHECK_EQ(15, last_source_column);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001358
1359 // Run without breakpoints.
1360 ClearBreakPoint(bp);
1361 foo->Call(env->Global(), 0, NULL);
1362 CHECK_EQ(2, break_point_hit_count);
1363
iposva@chromium.org245aa852009-02-10 00:49:54 +00001364 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001365 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001366}
1367
1368
1369static void CallWithBreakPoints(v8::Local<v8::Object> recv,
1370 v8::Local<v8::Function> f,
1371 int break_point_count,
1372 int call_count) {
1373 break_point_hit_count = 0;
1374 for (int i = 0; i < call_count; i++) {
1375 f->Call(recv, 0, NULL);
1376 CHECK_EQ((i + 1) * break_point_count, break_point_hit_count);
1377 }
1378}
1379
1380// Test GC during break point processing.
1381TEST(GCDuringBreakPointProcessing) {
1382 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001383 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001384 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001385
iposva@chromium.org245aa852009-02-10 00:49:54 +00001386 v8::Debug::SetDebugEventListener(DebugEventBreakPointCollectGarbage,
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.
1391 foo = CompileFunction(&env, "function foo(){bar=0;}", "foo");
1392 SetBreakPoint(foo, 0);
1393 CallWithBreakPoints(env->Global(), foo, 1, 10);
1394
1395 // Test IC load break point with garbage collection.
1396 foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo");
1397 SetBreakPoint(foo, 0);
1398 CallWithBreakPoints(env->Global(), foo, 1, 10);
1399
1400 // Test IC call break point with garbage collection.
1401 foo = CompileFunction(&env, "function bar(){};function foo(){bar();}", "foo");
1402 SetBreakPoint(foo, 0);
1403 CallWithBreakPoints(env->Global(), foo, 1, 10);
1404
1405 // Test return break point with garbage collection.
1406 foo = CompileFunction(&env, "function foo(){}", "foo");
1407 SetBreakPoint(foo, 0);
1408 CallWithBreakPoints(env->Global(), foo, 1, 25);
1409
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001410 // Test debug break slot break point with garbage collection.
1411 foo = CompileFunction(&env, "function foo(){var a;}", "foo");
1412 SetBreakPoint(foo, 0);
1413 CallWithBreakPoints(env->Global(), foo, 1, 25);
1414
iposva@chromium.org245aa852009-02-10 00:49:54 +00001415 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001416 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001417}
1418
1419
1420// Call the function three times with different garbage collections in between
1421// and make sure that the break point survives.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001422static void CallAndGC(v8::Local<v8::Object> recv,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001423 v8::Local<v8::Function> f) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001424 break_point_hit_count = 0;
1425
1426 for (int i = 0; i < 3; i++) {
1427 // Call function.
1428 f->Call(recv, 0, NULL);
1429 CHECK_EQ(1 + i * 3, break_point_hit_count);
1430
1431 // Scavenge and call function.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001432 HEAP->CollectGarbage(v8::internal::NEW_SPACE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001433 f->Call(recv, 0, NULL);
1434 CHECK_EQ(2 + i * 3, break_point_hit_count);
1435
1436 // Mark sweep (and perhaps compact) and call function.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001437 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001438 f->Call(recv, 0, NULL);
1439 CHECK_EQ(3 + i * 3, break_point_hit_count);
1440 }
1441}
1442
1443
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001444// Test that a break point can be set at a return store location.
1445TEST(BreakPointSurviveGC) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001446 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001447 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001448 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001449
iposva@chromium.org245aa852009-02-10 00:49:54 +00001450 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001451 v8::Undefined());
1452 v8::Local<v8::Function> foo;
1453
1454 // Test IC store break point with garbage collection.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001455 {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001456 CompileFunction(&env, "function foo(){}", "foo");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001457 foo = CompileFunction(&env, "function foo(){bar=0;}", "foo");
1458 SetBreakPoint(foo, 0);
1459 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001460 CallAndGC(env->Global(), foo);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001461
1462 // Test IC load break point with garbage collection.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001463 {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001464 CompileFunction(&env, "function foo(){}", "foo");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001465 foo = CompileFunction(&env, "bar=1;function foo(){var x=bar;}", "foo");
1466 SetBreakPoint(foo, 0);
1467 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001468 CallAndGC(env->Global(), foo);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001469
1470 // Test IC call break point with garbage collection.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001471 {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001472 CompileFunction(&env, "function foo(){}", "foo");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001473 foo = CompileFunction(&env,
1474 "function bar(){};function foo(){bar();}",
1475 "foo");
1476 SetBreakPoint(foo, 0);
1477 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001478 CallAndGC(env->Global(), foo);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001479
1480 // Test return break point with garbage collection.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001481 {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001482 CompileFunction(&env, "function foo(){}", "foo");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001483 foo = CompileFunction(&env, "function foo(){}", "foo");
1484 SetBreakPoint(foo, 0);
1485 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001486 CallAndGC(env->Global(), foo);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001487
1488 // Test non IC break point with garbage collection.
1489 {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001490 CompileFunction(&env, "function foo(){}", "foo");
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001491 foo = CompileFunction(&env, "function foo(){var bar=0;}", "foo");
1492 SetBreakPoint(foo, 0);
1493 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001494 CallAndGC(env->Global(), foo);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001495
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001496
iposva@chromium.org245aa852009-02-10 00:49:54 +00001497 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001498 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001499}
1500
1501
1502// Test that break points can be set using the global Debug object.
1503TEST(BreakPointThroughJavaScript) {
1504 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001505 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001506 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001507 env.ExposeDebug();
1508
iposva@chromium.org245aa852009-02-10 00:49:54 +00001509 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001510 v8::Undefined());
1511 v8::Script::Compile(v8::String::New("function bar(){}"))->Run();
1512 v8::Script::Compile(v8::String::New("function foo(){bar();bar();}"))->Run();
1513 // 012345678901234567890
1514 // 1 2
1515 // Break points are set at position 3 and 9
1516 v8::Local<v8::Script> foo = v8::Script::Compile(v8::String::New("foo()"));
1517
1518 // Run without breakpoints.
1519 foo->Run();
1520 CHECK_EQ(0, break_point_hit_count);
1521
1522 // Run with one breakpoint
1523 int bp1 = SetBreakPointFromJS("foo", 0, 3);
1524 foo->Run();
1525 CHECK_EQ(1, break_point_hit_count);
1526 foo->Run();
1527 CHECK_EQ(2, break_point_hit_count);
1528
1529 // Run with two breakpoints
1530 int bp2 = SetBreakPointFromJS("foo", 0, 9);
1531 foo->Run();
1532 CHECK_EQ(4, break_point_hit_count);
1533 foo->Run();
1534 CHECK_EQ(6, break_point_hit_count);
1535
1536 // Run with one breakpoint
1537 ClearBreakPointFromJS(bp2);
1538 foo->Run();
1539 CHECK_EQ(7, break_point_hit_count);
1540 foo->Run();
1541 CHECK_EQ(8, break_point_hit_count);
1542
1543 // Run without breakpoints.
1544 ClearBreakPointFromJS(bp1);
1545 foo->Run();
1546 CHECK_EQ(8, break_point_hit_count);
1547
iposva@chromium.org245aa852009-02-10 00:49:54 +00001548 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001549 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001550
1551 // Make sure that the break point numbers are consecutive.
1552 CHECK_EQ(1, bp1);
1553 CHECK_EQ(2, bp2);
1554}
1555
1556
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001557// Test that break points on scripts identified by name can be set using the
1558// global Debug object.
1559TEST(ScriptBreakPointByNameThroughJavaScript) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001560 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001561 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001562 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001563 env.ExposeDebug();
1564
iposva@chromium.org245aa852009-02-10 00:49:54 +00001565 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001566 v8::Undefined());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001567
1568 v8::Local<v8::String> script = v8::String::New(
1569 "function f() {\n"
1570 " function h() {\n"
1571 " a = 0; // line 2\n"
1572 " }\n"
1573 " b = 1; // line 4\n"
1574 " return h();\n"
1575 "}\n"
1576 "\n"
1577 "function g() {\n"
1578 " function h() {\n"
1579 " a = 0;\n"
1580 " }\n"
1581 " b = 2; // line 12\n"
1582 " h();\n"
1583 " b = 3; // line 14\n"
1584 " f(); // line 15\n"
1585 "}");
1586
1587 // Compile the script and get the two functions.
1588 v8::ScriptOrigin origin =
1589 v8::ScriptOrigin(v8::String::New("test"));
1590 v8::Script::Compile(script, &origin)->Run();
1591 v8::Local<v8::Function> f =
1592 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1593 v8::Local<v8::Function> g =
1594 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1595
1596 // Call f and g without break points.
1597 break_point_hit_count = 0;
1598 f->Call(env->Global(), 0, NULL);
1599 CHECK_EQ(0, break_point_hit_count);
1600 g->Call(env->Global(), 0, NULL);
1601 CHECK_EQ(0, break_point_hit_count);
1602
1603 // Call f and g with break point on line 12.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001604 int sbp1 = SetScriptBreakPointByNameFromJS("test", 12, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001605 break_point_hit_count = 0;
1606 f->Call(env->Global(), 0, NULL);
1607 CHECK_EQ(0, break_point_hit_count);
1608 g->Call(env->Global(), 0, NULL);
1609 CHECK_EQ(1, break_point_hit_count);
1610
1611 // Remove the break point again.
1612 break_point_hit_count = 0;
1613 ClearBreakPointFromJS(sbp1);
1614 f->Call(env->Global(), 0, NULL);
1615 CHECK_EQ(0, break_point_hit_count);
1616 g->Call(env->Global(), 0, NULL);
1617 CHECK_EQ(0, break_point_hit_count);
1618
1619 // Call f and g with break point on line 2.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001620 int sbp2 = SetScriptBreakPointByNameFromJS("test", 2, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001621 break_point_hit_count = 0;
1622 f->Call(env->Global(), 0, NULL);
1623 CHECK_EQ(1, break_point_hit_count);
1624 g->Call(env->Global(), 0, NULL);
1625 CHECK_EQ(2, break_point_hit_count);
1626
1627 // Call f and g with break point on line 2, 4, 12, 14 and 15.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001628 int sbp3 = SetScriptBreakPointByNameFromJS("test", 4, 0);
1629 int sbp4 = SetScriptBreakPointByNameFromJS("test", 12, 0);
1630 int sbp5 = SetScriptBreakPointByNameFromJS("test", 14, 0);
1631 int sbp6 = SetScriptBreakPointByNameFromJS("test", 15, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001632 break_point_hit_count = 0;
1633 f->Call(env->Global(), 0, NULL);
1634 CHECK_EQ(2, break_point_hit_count);
1635 g->Call(env->Global(), 0, NULL);
1636 CHECK_EQ(7, break_point_hit_count);
1637
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001638 // Remove all the break points again.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001639 break_point_hit_count = 0;
1640 ClearBreakPointFromJS(sbp2);
1641 ClearBreakPointFromJS(sbp3);
1642 ClearBreakPointFromJS(sbp4);
1643 ClearBreakPointFromJS(sbp5);
1644 ClearBreakPointFromJS(sbp6);
1645 f->Call(env->Global(), 0, NULL);
1646 CHECK_EQ(0, break_point_hit_count);
1647 g->Call(env->Global(), 0, NULL);
1648 CHECK_EQ(0, break_point_hit_count);
1649
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001650 v8::Debug::SetDebugEventListener(NULL);
1651 CheckDebuggerUnloaded();
1652
1653 // Make sure that the break point numbers are consecutive.
1654 CHECK_EQ(1, sbp1);
1655 CHECK_EQ(2, sbp2);
1656 CHECK_EQ(3, sbp3);
1657 CHECK_EQ(4, sbp4);
1658 CHECK_EQ(5, sbp5);
1659 CHECK_EQ(6, sbp6);
1660}
1661
1662
1663TEST(ScriptBreakPointByIdThroughJavaScript) {
1664 break_point_hit_count = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001665 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001666 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001667 env.ExposeDebug();
1668
1669 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
1670 v8::Undefined());
1671
1672 v8::Local<v8::String> source = v8::String::New(
1673 "function f() {\n"
1674 " function h() {\n"
1675 " a = 0; // line 2\n"
1676 " }\n"
1677 " b = 1; // line 4\n"
1678 " return h();\n"
1679 "}\n"
1680 "\n"
1681 "function g() {\n"
1682 " function h() {\n"
1683 " a = 0;\n"
1684 " }\n"
1685 " b = 2; // line 12\n"
1686 " h();\n"
1687 " b = 3; // line 14\n"
1688 " f(); // line 15\n"
1689 "}");
1690
1691 // Compile the script and get the two functions.
1692 v8::ScriptOrigin origin =
1693 v8::ScriptOrigin(v8::String::New("test"));
1694 v8::Local<v8::Script> script = v8::Script::Compile(source, &origin);
1695 script->Run();
1696 v8::Local<v8::Function> f =
1697 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1698 v8::Local<v8::Function> g =
1699 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
1700
1701 // Get the script id knowing that internally it is a 32 integer.
1702 uint32_t script_id = script->Id()->Uint32Value();
1703
1704 // Call f and g without break points.
1705 break_point_hit_count = 0;
1706 f->Call(env->Global(), 0, NULL);
1707 CHECK_EQ(0, break_point_hit_count);
1708 g->Call(env->Global(), 0, NULL);
1709 CHECK_EQ(0, break_point_hit_count);
1710
1711 // Call f and g with break point on line 12.
1712 int sbp1 = SetScriptBreakPointByIdFromJS(script_id, 12, 0);
1713 break_point_hit_count = 0;
1714 f->Call(env->Global(), 0, NULL);
1715 CHECK_EQ(0, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001716 g->Call(env->Global(), 0, NULL);
1717 CHECK_EQ(1, break_point_hit_count);
1718
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001719 // Remove the break point again.
1720 break_point_hit_count = 0;
1721 ClearBreakPointFromJS(sbp1);
1722 f->Call(env->Global(), 0, NULL);
1723 CHECK_EQ(0, break_point_hit_count);
1724 g->Call(env->Global(), 0, NULL);
1725 CHECK_EQ(0, break_point_hit_count);
1726
1727 // Call f and g with break point on line 2.
1728 int sbp2 = SetScriptBreakPointByIdFromJS(script_id, 2, 0);
1729 break_point_hit_count = 0;
1730 f->Call(env->Global(), 0, NULL);
1731 CHECK_EQ(1, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001732 g->Call(env->Global(), 0, NULL);
1733 CHECK_EQ(2, break_point_hit_count);
1734
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001735 // Call f and g with break point on line 2, 4, 12, 14 and 15.
1736 int sbp3 = SetScriptBreakPointByIdFromJS(script_id, 4, 0);
1737 int sbp4 = SetScriptBreakPointByIdFromJS(script_id, 12, 0);
1738 int sbp5 = SetScriptBreakPointByIdFromJS(script_id, 14, 0);
1739 int sbp6 = SetScriptBreakPointByIdFromJS(script_id, 15, 0);
1740 break_point_hit_count = 0;
1741 f->Call(env->Global(), 0, NULL);
1742 CHECK_EQ(2, break_point_hit_count);
1743 g->Call(env->Global(), 0, NULL);
1744 CHECK_EQ(7, break_point_hit_count);
1745
1746 // Remove all the break points again.
1747 break_point_hit_count = 0;
1748 ClearBreakPointFromJS(sbp2);
1749 ClearBreakPointFromJS(sbp3);
1750 ClearBreakPointFromJS(sbp4);
1751 ClearBreakPointFromJS(sbp5);
1752 ClearBreakPointFromJS(sbp6);
1753 f->Call(env->Global(), 0, NULL);
1754 CHECK_EQ(0, break_point_hit_count);
1755 g->Call(env->Global(), 0, NULL);
1756 CHECK_EQ(0, break_point_hit_count);
1757
iposva@chromium.org245aa852009-02-10 00:49:54 +00001758 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001759 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001760
1761 // Make sure that the break point numbers are consecutive.
1762 CHECK_EQ(1, sbp1);
1763 CHECK_EQ(2, sbp2);
1764 CHECK_EQ(3, sbp3);
1765 CHECK_EQ(4, sbp4);
1766 CHECK_EQ(5, sbp5);
1767 CHECK_EQ(6, sbp6);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001768}
1769
1770
1771// Test conditional script break points.
1772TEST(EnableDisableScriptBreakPoint) {
1773 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001774 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001775 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001776 env.ExposeDebug();
1777
iposva@chromium.org245aa852009-02-10 00:49:54 +00001778 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001779 v8::Undefined());
1780
1781 v8::Local<v8::String> script = v8::String::New(
1782 "function f() {\n"
1783 " a = 0; // line 1\n"
1784 "};");
1785
1786 // Compile the script and get function f.
1787 v8::ScriptOrigin origin =
1788 v8::ScriptOrigin(v8::String::New("test"));
1789 v8::Script::Compile(script, &origin)->Run();
1790 v8::Local<v8::Function> f =
1791 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1792
1793 // Set script break point on line 1 (in function f).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001794 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001795
1796 // Call f while enabeling and disabling the script break point.
1797 break_point_hit_count = 0;
1798 f->Call(env->Global(), 0, NULL);
1799 CHECK_EQ(1, break_point_hit_count);
1800
1801 DisableScriptBreakPointFromJS(sbp);
1802 f->Call(env->Global(), 0, NULL);
1803 CHECK_EQ(1, break_point_hit_count);
1804
1805 EnableScriptBreakPointFromJS(sbp);
1806 f->Call(env->Global(), 0, NULL);
1807 CHECK_EQ(2, break_point_hit_count);
1808
1809 DisableScriptBreakPointFromJS(sbp);
1810 f->Call(env->Global(), 0, NULL);
1811 CHECK_EQ(2, break_point_hit_count);
1812
1813 // Reload the script and get f again checking that the disabeling survives.
1814 v8::Script::Compile(script, &origin)->Run();
1815 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1816 f->Call(env->Global(), 0, NULL);
1817 CHECK_EQ(2, break_point_hit_count);
1818
1819 EnableScriptBreakPointFromJS(sbp);
1820 f->Call(env->Global(), 0, NULL);
1821 CHECK_EQ(3, break_point_hit_count);
1822
iposva@chromium.org245aa852009-02-10 00:49:54 +00001823 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001824 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001825}
1826
1827
1828// Test conditional script break points.
1829TEST(ConditionalScriptBreakPoint) {
1830 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001831 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001832 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001833 env.ExposeDebug();
1834
iposva@chromium.org245aa852009-02-10 00:49:54 +00001835 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001836 v8::Undefined());
1837
1838 v8::Local<v8::String> script = v8::String::New(
1839 "count = 0;\n"
1840 "function f() {\n"
1841 " g(count++); // line 2\n"
1842 "};\n"
1843 "function g(x) {\n"
1844 " var a=x; // line 5\n"
1845 "};");
1846
1847 // Compile the script and get function f.
1848 v8::ScriptOrigin origin =
1849 v8::ScriptOrigin(v8::String::New("test"));
1850 v8::Script::Compile(script, &origin)->Run();
1851 v8::Local<v8::Function> f =
1852 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1853
1854 // Set script break point on line 5 (in function g).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001855 int sbp1 = SetScriptBreakPointByNameFromJS("test", 5, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001856
1857 // Call f with different conditions on the script break point.
1858 break_point_hit_count = 0;
1859 ChangeScriptBreakPointConditionFromJS(sbp1, "false");
1860 f->Call(env->Global(), 0, NULL);
1861 CHECK_EQ(0, break_point_hit_count);
1862
1863 ChangeScriptBreakPointConditionFromJS(sbp1, "true");
1864 break_point_hit_count = 0;
1865 f->Call(env->Global(), 0, NULL);
1866 CHECK_EQ(1, break_point_hit_count);
1867
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001868 ChangeScriptBreakPointConditionFromJS(sbp1, "x % 2 == 0");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001869 break_point_hit_count = 0;
1870 for (int i = 0; i < 10; i++) {
1871 f->Call(env->Global(), 0, NULL);
1872 }
1873 CHECK_EQ(5, break_point_hit_count);
1874
1875 // Reload the script and get f again checking that the condition survives.
1876 v8::Script::Compile(script, &origin)->Run();
1877 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1878
1879 break_point_hit_count = 0;
1880 for (int i = 0; i < 10; i++) {
1881 f->Call(env->Global(), 0, NULL);
1882 }
1883 CHECK_EQ(5, break_point_hit_count);
1884
iposva@chromium.org245aa852009-02-10 00:49:54 +00001885 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001886 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001887}
1888
1889
1890// Test ignore count on script break points.
1891TEST(ScriptBreakPointIgnoreCount) {
1892 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001893 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001894 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001895 env.ExposeDebug();
1896
iposva@chromium.org245aa852009-02-10 00:49:54 +00001897 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001898 v8::Undefined());
1899
1900 v8::Local<v8::String> script = v8::String::New(
1901 "function f() {\n"
1902 " a = 0; // line 1\n"
1903 "};");
1904
1905 // Compile the script and get function f.
1906 v8::ScriptOrigin origin =
1907 v8::ScriptOrigin(v8::String::New("test"));
1908 v8::Script::Compile(script, &origin)->Run();
1909 v8::Local<v8::Function> f =
1910 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1911
1912 // Set script break point on line 1 (in function f).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001913 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001914
1915 // Call f with different ignores on the script break point.
1916 break_point_hit_count = 0;
1917 ChangeScriptBreakPointIgnoreCountFromJS(sbp, 1);
1918 f->Call(env->Global(), 0, NULL);
1919 CHECK_EQ(0, break_point_hit_count);
1920 f->Call(env->Global(), 0, NULL);
1921 CHECK_EQ(1, break_point_hit_count);
1922
1923 ChangeScriptBreakPointIgnoreCountFromJS(sbp, 5);
1924 break_point_hit_count = 0;
1925 for (int i = 0; i < 10; i++) {
1926 f->Call(env->Global(), 0, NULL);
1927 }
1928 CHECK_EQ(5, break_point_hit_count);
1929
1930 // Reload the script and get f again checking that the ignore survives.
1931 v8::Script::Compile(script, &origin)->Run();
1932 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1933
1934 break_point_hit_count = 0;
1935 for (int i = 0; i < 10; i++) {
1936 f->Call(env->Global(), 0, NULL);
1937 }
1938 CHECK_EQ(5, break_point_hit_count);
1939
iposva@chromium.org245aa852009-02-10 00:49:54 +00001940 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001941 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001942}
1943
1944
1945// Test that script break points survive when a script is reloaded.
1946TEST(ScriptBreakPointReload) {
1947 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001948 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001949 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001950 env.ExposeDebug();
1951
iposva@chromium.org245aa852009-02-10 00:49:54 +00001952 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001953 v8::Undefined());
1954
1955 v8::Local<v8::Function> f;
1956 v8::Local<v8::String> script = v8::String::New(
1957 "function f() {\n"
1958 " function h() {\n"
1959 " a = 0; // line 2\n"
1960 " }\n"
1961 " b = 1; // line 4\n"
1962 " return h();\n"
1963 "}");
1964
1965 v8::ScriptOrigin origin_1 = v8::ScriptOrigin(v8::String::New("1"));
1966 v8::ScriptOrigin origin_2 = v8::ScriptOrigin(v8::String::New("2"));
1967
1968 // Set a script break point before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001969 SetScriptBreakPointByNameFromJS("1", 2, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001970
1971 // Compile the script and get the function.
1972 v8::Script::Compile(script, &origin_1)->Run();
1973 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1974
1975 // Call f and check that the script break point is active.
1976 break_point_hit_count = 0;
1977 f->Call(env->Global(), 0, NULL);
1978 CHECK_EQ(1, break_point_hit_count);
1979
1980 // Compile the script again with a different script data and get the
1981 // function.
1982 v8::Script::Compile(script, &origin_2)->Run();
1983 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1984
1985 // Call f and check that no break points are set.
1986 break_point_hit_count = 0;
1987 f->Call(env->Global(), 0, NULL);
1988 CHECK_EQ(0, break_point_hit_count);
1989
1990 // Compile the script again and get the function.
1991 v8::Script::Compile(script, &origin_1)->Run();
1992 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
1993
1994 // Call f and check that the script break point is active.
1995 break_point_hit_count = 0;
1996 f->Call(env->Global(), 0, NULL);
1997 CHECK_EQ(1, break_point_hit_count);
1998
iposva@chromium.org245aa852009-02-10 00:49:54 +00001999 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002000 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002001}
2002
2003
2004// Test when several scripts has the same script data
2005TEST(ScriptBreakPointMultiple) {
2006 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002007 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002008 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002009 env.ExposeDebug();
2010
iposva@chromium.org245aa852009-02-10 00:49:54 +00002011 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002012 v8::Undefined());
2013
2014 v8::Local<v8::Function> f;
2015 v8::Local<v8::String> script_f = v8::String::New(
2016 "function f() {\n"
2017 " a = 0; // line 1\n"
2018 "}");
2019
2020 v8::Local<v8::Function> g;
2021 v8::Local<v8::String> script_g = v8::String::New(
2022 "function g() {\n"
2023 " b = 0; // line 1\n"
2024 "}");
2025
2026 v8::ScriptOrigin origin =
2027 v8::ScriptOrigin(v8::String::New("test"));
2028
2029 // Set a script break point before the scripts are loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002030 int sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002031
2032 // Compile the scripts with same script data and get the functions.
2033 v8::Script::Compile(script_f, &origin)->Run();
2034 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
2035 v8::Script::Compile(script_g, &origin)->Run();
2036 g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
2037
2038 // Call f and g and check that the script break point is active.
2039 break_point_hit_count = 0;
2040 f->Call(env->Global(), 0, NULL);
2041 CHECK_EQ(1, break_point_hit_count);
2042 g->Call(env->Global(), 0, NULL);
2043 CHECK_EQ(2, break_point_hit_count);
2044
2045 // Clear the script break point.
2046 ClearBreakPointFromJS(sbp);
2047
2048 // Call f and g and check that the script break point is no longer active.
2049 break_point_hit_count = 0;
2050 f->Call(env->Global(), 0, NULL);
2051 CHECK_EQ(0, break_point_hit_count);
2052 g->Call(env->Global(), 0, NULL);
2053 CHECK_EQ(0, break_point_hit_count);
2054
2055 // Set script break point with the scripts loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002056 sbp = SetScriptBreakPointByNameFromJS("test", 1, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002057
2058 // Call f and g and check that the script break point is active.
2059 break_point_hit_count = 0;
2060 f->Call(env->Global(), 0, NULL);
2061 CHECK_EQ(1, break_point_hit_count);
2062 g->Call(env->Global(), 0, NULL);
2063 CHECK_EQ(2, break_point_hit_count);
2064
iposva@chromium.org245aa852009-02-10 00:49:54 +00002065 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002066 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002067}
2068
2069
2070// Test the script origin which has both name and line offset.
2071TEST(ScriptBreakPointLineOffset) {
2072 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002073 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002074 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002075 env.ExposeDebug();
2076
iposva@chromium.org245aa852009-02-10 00:49:54 +00002077 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002078 v8::Undefined());
2079
2080 v8::Local<v8::Function> f;
2081 v8::Local<v8::String> script = v8::String::New(
2082 "function f() {\n"
2083 " a = 0; // line 8 as this script has line offset 7\n"
2084 " b = 0; // line 9 as this script has line offset 7\n"
2085 "}");
2086
2087 // Create script origin both name and line offset.
2088 v8::ScriptOrigin origin(v8::String::New("test.html"),
2089 v8::Integer::New(7));
2090
2091 // Set two script break points before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002092 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 8, 0);
2093 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 9, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002094
2095 // Compile the script and get the function.
2096 v8::Script::Compile(script, &origin)->Run();
2097 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
2098
2099 // Call f and check that the script break point is active.
2100 break_point_hit_count = 0;
2101 f->Call(env->Global(), 0, NULL);
2102 CHECK_EQ(2, break_point_hit_count);
2103
2104 // Clear the script break points.
2105 ClearBreakPointFromJS(sbp1);
2106 ClearBreakPointFromJS(sbp2);
2107
2108 // Call f and check that no script break points are active.
2109 break_point_hit_count = 0;
2110 f->Call(env->Global(), 0, NULL);
2111 CHECK_EQ(0, break_point_hit_count);
2112
2113 // Set a script break point with the script loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002114 sbp1 = SetScriptBreakPointByNameFromJS("test.html", 9, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002115
2116 // Call f and check that the script break point is active.
2117 break_point_hit_count = 0;
2118 f->Call(env->Global(), 0, NULL);
2119 CHECK_EQ(1, break_point_hit_count);
2120
iposva@chromium.org245aa852009-02-10 00:49:54 +00002121 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002122 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002123}
2124
2125
2126// Test script break points set on lines.
2127TEST(ScriptBreakPointLine) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002128 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002129 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002130 env.ExposeDebug();
2131
2132 // Create a function for checking the function when hitting a break point.
2133 frame_function_name = CompileFunction(&env,
2134 frame_function_name_source,
2135 "frame_function_name");
2136
iposva@chromium.org245aa852009-02-10 00:49:54 +00002137 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002138 v8::Undefined());
2139
2140 v8::Local<v8::Function> f;
2141 v8::Local<v8::Function> g;
2142 v8::Local<v8::String> script = v8::String::New(
2143 "a = 0 // line 0\n"
2144 "function f() {\n"
2145 " a = 1; // line 2\n"
2146 "}\n"
2147 " a = 2; // line 4\n"
2148 " /* xx */ function g() { // line 5\n"
2149 " function h() { // line 6\n"
2150 " a = 3; // line 7\n"
2151 " }\n"
2152 " h(); // line 9\n"
2153 " a = 4; // line 10\n"
2154 " }\n"
2155 " a=5; // line 12");
2156
2157 // Set a couple script break point before the script is loaded.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002158 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 0, -1);
2159 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 1, -1);
2160 int sbp3 = SetScriptBreakPointByNameFromJS("test.html", 5, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002161
2162 // Compile the script and get the function.
2163 break_point_hit_count = 0;
2164 v8::ScriptOrigin origin(v8::String::New("test.html"), v8::Integer::New(0));
2165 v8::Script::Compile(script, &origin)->Run();
2166 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
2167 g = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
2168
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002169 // Check that a break point was hit when the script was run.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002170 CHECK_EQ(1, break_point_hit_count);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002171 CHECK_EQ(0, StrLength(last_function_hit));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002172
2173 // Call f and check that the script break point.
2174 f->Call(env->Global(), 0, NULL);
2175 CHECK_EQ(2, break_point_hit_count);
2176 CHECK_EQ("f", last_function_hit);
2177
2178 // Call g and check that the script break point.
2179 g->Call(env->Global(), 0, NULL);
2180 CHECK_EQ(3, break_point_hit_count);
2181 CHECK_EQ("g", last_function_hit);
2182
2183 // Clear the script break point on g and set one on h.
2184 ClearBreakPointFromJS(sbp3);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002185 int sbp4 = SetScriptBreakPointByNameFromJS("test.html", 6, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002186
2187 // Call g and check that the script break point in h is hit.
2188 g->Call(env->Global(), 0, NULL);
2189 CHECK_EQ(4, break_point_hit_count);
2190 CHECK_EQ("h", last_function_hit);
2191
2192 // Clear break points in f and h. Set a new one in the script between
2193 // functions f and g and test that there is no break points in f and g any
2194 // more.
2195 ClearBreakPointFromJS(sbp2);
2196 ClearBreakPointFromJS(sbp4);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002197 int sbp5 = SetScriptBreakPointByNameFromJS("test.html", 4, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002198 break_point_hit_count = 0;
2199 f->Call(env->Global(), 0, NULL);
2200 g->Call(env->Global(), 0, NULL);
2201 CHECK_EQ(0, break_point_hit_count);
2202
2203 // Reload the script which should hit two break points.
2204 break_point_hit_count = 0;
2205 v8::Script::Compile(script, &origin)->Run();
2206 CHECK_EQ(2, break_point_hit_count);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002207 CHECK_EQ(0, StrLength(last_function_hit));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002208
2209 // Set a break point in the code after the last function decleration.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002210 int sbp6 = SetScriptBreakPointByNameFromJS("test.html", 12, -1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002211
2212 // Reload the script which should hit three break points.
2213 break_point_hit_count = 0;
2214 v8::Script::Compile(script, &origin)->Run();
2215 CHECK_EQ(3, break_point_hit_count);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002216 CHECK_EQ(0, StrLength(last_function_hit));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002217
2218 // Clear the last break points, and reload the script which should not hit any
2219 // break points.
2220 ClearBreakPointFromJS(sbp1);
2221 ClearBreakPointFromJS(sbp5);
2222 ClearBreakPointFromJS(sbp6);
2223 break_point_hit_count = 0;
2224 v8::Script::Compile(script, &origin)->Run();
2225 CHECK_EQ(0, break_point_hit_count);
2226
iposva@chromium.org245aa852009-02-10 00:49:54 +00002227 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002228 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002229}
2230
2231
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002232// Test top level script break points set on lines.
2233TEST(ScriptBreakPointLineTopLevel) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002234 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002235 v8::HandleScope scope(env->GetIsolate());
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002236 env.ExposeDebug();
2237
2238 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
2239 v8::Undefined());
2240
2241 v8::Local<v8::String> script = v8::String::New(
2242 "function f() {\n"
2243 " a = 1; // line 1\n"
2244 "}\n"
2245 "a = 2; // line 3\n");
2246 v8::Local<v8::Function> f;
2247 {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002248 v8::HandleScope scope(env->GetIsolate());
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002249 v8::Script::Compile(script, v8::String::New("test.html"))->Run();
2250 }
2251 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
2252
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002253 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00002254
2255 SetScriptBreakPointByNameFromJS("test.html", 3, -1);
2256
2257 // Call f and check that there was no break points.
2258 break_point_hit_count = 0;
2259 f->Call(env->Global(), 0, NULL);
2260 CHECK_EQ(0, break_point_hit_count);
2261
2262 // Recompile and run script and check that break point was hit.
2263 break_point_hit_count = 0;
2264 v8::Script::Compile(script, v8::String::New("test.html"))->Run();
2265 CHECK_EQ(1, break_point_hit_count);
2266
2267 // Call f and check that there are still no break points.
2268 break_point_hit_count = 0;
2269 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
2270 CHECK_EQ(0, break_point_hit_count);
2271
2272 v8::Debug::SetDebugEventListener(NULL);
2273 CheckDebuggerUnloaded();
2274}
2275
2276
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002277// Test that it is possible to add and remove break points in a top level
2278// function which has no references but has not been collected yet.
2279TEST(ScriptBreakPointTopLevelCrash) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002280 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002281 v8::HandleScope scope(env->GetIsolate());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002282 env.ExposeDebug();
2283
2284 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
2285 v8::Undefined());
2286
2287 v8::Local<v8::String> script_source = v8::String::New(
2288 "function f() {\n"
2289 " return 0;\n"
2290 "}\n"
2291 "f()");
2292
2293 int sbp1 = SetScriptBreakPointByNameFromJS("test.html", 3, -1);
2294 {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002295 v8::HandleScope scope(env->GetIsolate());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002296 break_point_hit_count = 0;
2297 v8::Script::Compile(script_source, v8::String::New("test.html"))->Run();
2298 CHECK_EQ(1, break_point_hit_count);
2299 }
2300
2301 int sbp2 = SetScriptBreakPointByNameFromJS("test.html", 3, -1);
2302 ClearBreakPointFromJS(sbp1);
2303 ClearBreakPointFromJS(sbp2);
2304
2305 v8::Debug::SetDebugEventListener(NULL);
2306 CheckDebuggerUnloaded();
2307}
2308
2309
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002310// Test that it is possible to remove the last break point for a function
2311// inside the break handling of that break point.
2312TEST(RemoveBreakPointInBreak) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002313 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002314 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002315
2316 v8::Local<v8::Function> foo =
2317 CompileFunction(&env, "function foo(){a=1;}", "foo");
2318 debug_event_remove_break_point = SetBreakPoint(foo, 0);
2319
2320 // Register the debug event listener pasing the function
iposva@chromium.org245aa852009-02-10 00:49:54 +00002321 v8::Debug::SetDebugEventListener(DebugEventRemoveBreakPoint, foo);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002322
2323 break_point_hit_count = 0;
2324 foo->Call(env->Global(), 0, NULL);
2325 CHECK_EQ(1, break_point_hit_count);
2326
2327 break_point_hit_count = 0;
2328 foo->Call(env->Global(), 0, NULL);
2329 CHECK_EQ(0, break_point_hit_count);
2330
iposva@chromium.org245aa852009-02-10 00:49:54 +00002331 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002332 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002333}
2334
2335
2336// Test that the debugger statement causes a break.
2337TEST(DebuggerStatement) {
2338 break_point_hit_count = 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002339 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002340 v8::HandleScope scope(env->GetIsolate());
iposva@chromium.org245aa852009-02-10 00:49:54 +00002341 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002342 v8::Undefined());
2343 v8::Script::Compile(v8::String::New("function bar(){debugger}"))->Run();
2344 v8::Script::Compile(v8::String::New(
2345 "function foo(){debugger;debugger;}"))->Run();
2346 v8::Local<v8::Function> foo =
2347 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
2348 v8::Local<v8::Function> bar =
2349 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("bar")));
2350
2351 // Run function with debugger statement
2352 bar->Call(env->Global(), 0, NULL);
2353 CHECK_EQ(1, break_point_hit_count);
2354
2355 // Run function with two debugger statement
2356 foo->Call(env->Global(), 0, NULL);
2357 CHECK_EQ(3, break_point_hit_count);
2358
iposva@chromium.org245aa852009-02-10 00:49:54 +00002359 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002360 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002361}
2362
2363
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002364// Test setting a breakpoint on the debugger statement.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002365TEST(DebuggerStatementBreakpoint) {
2366 break_point_hit_count = 0;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002367 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002368 v8::HandleScope scope(env->GetIsolate());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002369 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
2370 v8::Undefined());
2371 v8::Script::Compile(v8::String::New("function foo(){debugger;}"))->Run();
2372 v8::Local<v8::Function> foo =
2373 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("foo")));
2374
2375 // The debugger statement triggers breakpint hit
2376 foo->Call(env->Global(), 0, NULL);
2377 CHECK_EQ(1, break_point_hit_count);
2378
2379 int bp = SetBreakPoint(foo, 0);
2380
2381 // Set breakpoint does not duplicate hits
2382 foo->Call(env->Global(), 0, NULL);
2383 CHECK_EQ(2, break_point_hit_count);
2384
2385 ClearBreakPoint(bp);
2386 v8::Debug::SetDebugEventListener(NULL);
2387 CheckDebuggerUnloaded();
2388}
2389
2390
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002391// Test that the evaluation of expressions when a break point is hit generates
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002392// the correct results.
2393TEST(DebugEvaluate) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002394 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002395 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002396 env.ExposeDebug();
2397
2398 // Create a function for checking the evaluation when hitting a break point.
2399 evaluate_check_function = CompileFunction(&env,
2400 evaluate_check_source,
2401 "evaluate_check");
2402 // Register the debug event listener
iposva@chromium.org245aa852009-02-10 00:49:54 +00002403 v8::Debug::SetDebugEventListener(DebugEventEvaluate);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002404
2405 // Different expected vaules of x and a when in a break point (u = undefined,
2406 // d = Hello, world!).
2407 struct EvaluateCheck checks_uu[] = {
2408 {"x", v8::Undefined()},
2409 {"a", v8::Undefined()},
2410 {NULL, v8::Handle<v8::Value>()}
2411 };
2412 struct EvaluateCheck checks_hu[] = {
2413 {"x", v8::String::New("Hello, world!")},
2414 {"a", v8::Undefined()},
2415 {NULL, v8::Handle<v8::Value>()}
2416 };
2417 struct EvaluateCheck checks_hh[] = {
2418 {"x", v8::String::New("Hello, world!")},
2419 {"a", v8::String::New("Hello, world!")},
2420 {NULL, v8::Handle<v8::Value>()}
2421 };
2422
2423 // Simple test function. The "y=0" is in the function foo to provide a break
2424 // location. For "y=0" the "y" is at position 15 in the barbar function
2425 // therefore setting breakpoint at position 15 will break at "y=0" and
2426 // setting it higher will break after.
2427 v8::Local<v8::Function> foo = CompileFunction(&env,
2428 "function foo(x) {"
2429 " var a;"
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002430 " y=0;" // To ensure break location 1.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002431 " a=x;"
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002432 " y=0;" // To ensure break location 2.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002433 "}",
2434 "foo");
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002435 const int foo_break_position_1 = 15;
2436 const int foo_break_position_2 = 29;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002437
2438 // Arguments with one parameter "Hello, world!"
2439 v8::Handle<v8::Value> argv_foo[1] = { v8::String::New("Hello, world!") };
2440
2441 // Call foo with breakpoint set before a=x and undefined as parameter.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002442 int bp = SetBreakPoint(foo, foo_break_position_1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002443 checks = checks_uu;
2444 foo->Call(env->Global(), 0, NULL);
2445
2446 // Call foo with breakpoint set before a=x and parameter "Hello, world!".
2447 checks = checks_hu;
2448 foo->Call(env->Global(), 1, argv_foo);
2449
2450 // Call foo with breakpoint set after a=x and parameter "Hello, world!".
2451 ClearBreakPoint(bp);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002452 SetBreakPoint(foo, foo_break_position_2);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002453 checks = checks_hh;
2454 foo->Call(env->Global(), 1, argv_foo);
2455
2456 // Test function with an inner function. The "y=0" is in function barbar
2457 // to provide a break location. For "y=0" the "y" is at position 8 in the
2458 // barbar function therefore setting breakpoint at position 8 will break at
2459 // "y=0" and setting it higher will break after.
2460 v8::Local<v8::Function> bar = CompileFunction(&env,
2461 "y = 0;"
2462 "x = 'Goodbye, world!';"
2463 "function bar(x, b) {"
2464 " var a;"
2465 " function barbar() {"
2466 " y=0; /* To ensure break location.*/"
2467 " a=x;"
2468 " };"
2469 " debug.Debug.clearAllBreakPoints();"
2470 " barbar();"
2471 " y=0;a=x;"
2472 "}",
2473 "bar");
2474 const int barbar_break_position = 8;
2475
2476 // Call bar setting breakpoint before a=x in barbar and undefined as
2477 // parameter.
2478 checks = checks_uu;
2479 v8::Handle<v8::Value> argv_bar_1[2] = {
2480 v8::Undefined(),
2481 v8::Number::New(barbar_break_position)
2482 };
2483 bar->Call(env->Global(), 2, argv_bar_1);
2484
2485 // Call bar setting breakpoint before a=x in barbar and parameter
2486 // "Hello, world!".
2487 checks = checks_hu;
2488 v8::Handle<v8::Value> argv_bar_2[2] = {
2489 v8::String::New("Hello, world!"),
2490 v8::Number::New(barbar_break_position)
2491 };
2492 bar->Call(env->Global(), 2, argv_bar_2);
2493
2494 // Call bar setting breakpoint after a=x in barbar and parameter
2495 // "Hello, world!".
2496 checks = checks_hh;
2497 v8::Handle<v8::Value> argv_bar_3[2] = {
2498 v8::String::New("Hello, world!"),
2499 v8::Number::New(barbar_break_position + 1)
2500 };
2501 bar->Call(env->Global(), 2, argv_bar_3);
2502
iposva@chromium.org245aa852009-02-10 00:49:54 +00002503 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002504 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002505}
2506
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002507
2508int debugEventCount = 0;
2509static void CheckDebugEvent(const v8::Debug::EventDetails& eventDetails) {
2510 if (eventDetails.GetEvent() == v8::Break) ++debugEventCount;
2511}
2512
2513// Test that the conditional breakpoints work event if code generation from
2514// strings is prohibited in the debugee context.
2515TEST(ConditionalBreakpointWithCodeGenerationDisallowed) {
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002516 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002517 v8::HandleScope scope(env->GetIsolate());
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002518 env.ExposeDebug();
2519
2520 v8::Debug::SetDebugEventListener2(CheckDebugEvent);
2521
2522 v8::Local<v8::Function> foo = CompileFunction(&env,
2523 "function foo(x) {\n"
2524 " var s = 'String value2';\n"
2525 " return s + x;\n"
2526 "}",
2527 "foo");
2528
2529 // Set conditional breakpoint with condition 'true'.
2530 CompileRun("debug.Debug.setBreakPoint(foo, 2, 0, 'true')");
2531
2532 debugEventCount = 0;
2533 env->AllowCodeGenerationFromStrings(false);
2534 foo->Call(env->Global(), 0, NULL);
2535 CHECK_EQ(1, debugEventCount);
2536
2537 v8::Debug::SetDebugEventListener2(NULL);
2538 CheckDebuggerUnloaded();
2539}
2540
2541
2542bool checkedDebugEvals = true;
2543v8::Handle<v8::Function> checkGlobalEvalFunction;
2544v8::Handle<v8::Function> checkFrameEvalFunction;
2545static void CheckDebugEval(const v8::Debug::EventDetails& eventDetails) {
2546 if (eventDetails.GetEvent() == v8::Break) {
2547 ++debugEventCount;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002548 v8::HandleScope handleScope(v8::Isolate::GetCurrent());
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002549
2550 v8::Handle<v8::Value> args[] = { eventDetails.GetExecutionState() };
2551 CHECK(checkGlobalEvalFunction->Call(
2552 eventDetails.GetEventContext()->Global(), 1, args)->IsTrue());
2553 CHECK(checkFrameEvalFunction->Call(
2554 eventDetails.GetEventContext()->Global(), 1, args)->IsTrue());
2555 }
2556}
2557
2558// Test that the evaluation of expressions when a break point is hit generates
2559// the correct results in case code generation from strings is disallowed in the
2560// debugee context.
2561TEST(DebugEvaluateWithCodeGenerationDisallowed) {
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002562 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002563 v8::HandleScope scope(env->GetIsolate());
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002564 env.ExposeDebug();
2565
2566 v8::Debug::SetDebugEventListener2(CheckDebugEval);
2567
2568 v8::Local<v8::Function> foo = CompileFunction(&env,
2569 "var global = 'Global';\n"
2570 "function foo(x) {\n"
2571 " var local = 'Local';\n"
2572 " debugger;\n"
2573 " return local + x;\n"
2574 "}",
2575 "foo");
2576 checkGlobalEvalFunction = CompileFunction(&env,
2577 "function checkGlobalEval(exec_state) {\n"
2578 " return exec_state.evaluateGlobal('global').value() === 'Global';\n"
2579 "}",
2580 "checkGlobalEval");
2581
2582 checkFrameEvalFunction = CompileFunction(&env,
2583 "function checkFrameEval(exec_state) {\n"
2584 " return exec_state.frame(0).evaluate('local').value() === 'Local';\n"
2585 "}",
2586 "checkFrameEval");
2587 debugEventCount = 0;
2588 env->AllowCodeGenerationFromStrings(false);
2589 foo->Call(env->Global(), 0, NULL);
2590 CHECK_EQ(1, debugEventCount);
2591
2592 checkGlobalEvalFunction.Clear();
2593 checkFrameEvalFunction.Clear();
2594 v8::Debug::SetDebugEventListener2(NULL);
2595 CheckDebuggerUnloaded();
2596}
2597
2598
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002599// Copies a C string to a 16-bit string. Does not check for buffer overflow.
2600// Does not use the V8 engine to convert strings, so it can be used
2601// in any thread. Returns the length of the string.
2602int AsciiToUtf16(const char* input_buffer, uint16_t* output_buffer) {
2603 int i;
2604 for (i = 0; input_buffer[i] != '\0'; ++i) {
2605 // ASCII does not use chars > 127, but be careful anyway.
2606 output_buffer[i] = static_cast<unsigned char>(input_buffer[i]);
2607 }
2608 output_buffer[i] = 0;
2609 return i;
2610}
2611
2612// Copies a 16-bit string to a C string by dropping the high byte of
2613// each character. Does not check for buffer overflow.
2614// Can be used in any thread. Requires string length as an input.
2615int Utf16ToAscii(const uint16_t* input_buffer, int length,
2616 char* output_buffer, int output_len = -1) {
2617 if (output_len >= 0) {
2618 if (length > output_len - 1) {
2619 length = output_len - 1;
2620 }
2621 }
2622
2623 for (int i = 0; i < length; ++i) {
2624 output_buffer[i] = static_cast<char>(input_buffer[i]);
2625 }
2626 output_buffer[length] = '\0';
2627 return length;
2628}
2629
2630
2631// We match parts of the message to get evaluate result int value.
2632bool GetEvaluateStringResult(char *message, char* buffer, int buffer_size) {
2633 if (strstr(message, "\"command\":\"evaluate\"") == NULL) {
2634 return false;
2635 }
2636 const char* prefix = "\"text\":\"";
2637 char* pos1 = strstr(message, prefix);
2638 if (pos1 == NULL) {
2639 return false;
2640 }
2641 pos1 += strlen(prefix);
2642 char* pos2 = strchr(pos1, '"');
2643 if (pos2 == NULL) {
2644 return false;
2645 }
2646 Vector<char> buf(buffer, buffer_size);
2647 int len = static_cast<int>(pos2 - pos1);
2648 if (len > buffer_size - 1) {
2649 len = buffer_size - 1;
2650 }
2651 OS::StrNCpy(buf, pos1, len);
2652 buffer[buffer_size - 1] = '\0';
2653 return true;
2654}
2655
2656
2657struct EvaluateResult {
2658 static const int kBufferSize = 20;
2659 char buffer[kBufferSize];
2660};
2661
2662struct DebugProcessDebugMessagesData {
2663 static const int kArraySize = 5;
2664 int counter;
2665 EvaluateResult results[kArraySize];
2666
2667 void reset() {
2668 counter = 0;
2669 }
2670 EvaluateResult* current() {
2671 return &results[counter % kArraySize];
2672 }
2673 void next() {
2674 counter++;
2675 }
2676};
2677
2678DebugProcessDebugMessagesData process_debug_messages_data;
2679
2680static void DebugProcessDebugMessagesHandler(
2681 const uint16_t* message,
2682 int length,
2683 v8::Debug::ClientData* client_data) {
2684
2685 const int kBufferSize = 100000;
2686 char print_buffer[kBufferSize];
2687 Utf16ToAscii(message, length, print_buffer, kBufferSize);
2688
2689 EvaluateResult* array_item = process_debug_messages_data.current();
2690
2691 bool res = GetEvaluateStringResult(print_buffer,
2692 array_item->buffer,
2693 EvaluateResult::kBufferSize);
2694 if (res) {
2695 process_debug_messages_data.next();
2696 }
2697}
2698
2699// Test that the evaluation of expressions works even from ProcessDebugMessages
2700// i.e. with empty stack.
2701TEST(DebugEvaluateWithoutStack) {
2702 v8::Debug::SetMessageHandler(DebugProcessDebugMessagesHandler);
2703
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002704 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002705 v8::HandleScope scope(env->GetIsolate());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002706
2707 const char* source =
2708 "var v1 = 'Pinguin';\n function getAnimal() { return 'Capy' + 'bara'; }";
2709
2710 v8::Script::Compile(v8::String::New(source))->Run();
2711
2712 v8::Debug::ProcessDebugMessages();
2713
2714 const int kBufferSize = 1000;
2715 uint16_t buffer[kBufferSize];
2716
2717 const char* command_111 = "{\"seq\":111,"
2718 "\"type\":\"request\","
2719 "\"command\":\"evaluate\","
2720 "\"arguments\":{"
2721 " \"global\":true,"
2722 " \"expression\":\"v1\",\"disable_break\":true"
2723 "}}";
2724
2725 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_111, buffer));
2726
2727 const char* command_112 = "{\"seq\":112,"
2728 "\"type\":\"request\","
2729 "\"command\":\"evaluate\","
2730 "\"arguments\":{"
2731 " \"global\":true,"
2732 " \"expression\":\"getAnimal()\",\"disable_break\":true"
2733 "}}";
2734
2735 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_112, buffer));
2736
2737 const char* command_113 = "{\"seq\":113,"
2738 "\"type\":\"request\","
2739 "\"command\":\"evaluate\","
2740 "\"arguments\":{"
2741 " \"global\":true,"
2742 " \"expression\":\"239 + 566\",\"disable_break\":true"
2743 "}}";
2744
2745 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_113, buffer));
2746
2747 v8::Debug::ProcessDebugMessages();
2748
2749 CHECK_EQ(3, process_debug_messages_data.counter);
2750
2751 CHECK_EQ(strcmp("Pinguin", process_debug_messages_data.results[0].buffer), 0);
2752 CHECK_EQ(strcmp("Capybara", process_debug_messages_data.results[1].buffer),
2753 0);
2754 CHECK_EQ(strcmp("805", process_debug_messages_data.results[2].buffer), 0);
2755
2756 v8::Debug::SetMessageHandler(NULL);
2757 v8::Debug::SetDebugEventListener(NULL);
2758 CheckDebuggerUnloaded();
2759}
2760
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002761
2762// Simple test of the stepping mechanism using only store ICs.
2763TEST(DebugStepLinear) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002764 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002765 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002766
2767 // Create a function for testing stepping.
2768 v8::Local<v8::Function> foo = CompileFunction(&env,
2769 "function foo(){a=1;b=1;c=1;}",
2770 "foo");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002771
2772 // Run foo to allow it to get optimized.
2773 CompileRun("a=0; b=0; c=0; foo();");
2774
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002775 SetBreakPoint(foo, 3);
2776
2777 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002778 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002779
2780 step_action = StepIn;
2781 break_point_hit_count = 0;
2782 foo->Call(env->Global(), 0, NULL);
2783
2784 // With stepping all break locations are hit.
2785 CHECK_EQ(4, break_point_hit_count);
2786
iposva@chromium.org245aa852009-02-10 00:49:54 +00002787 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002788 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002789
2790 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00002791 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002792
ager@chromium.org381abbb2009-02-25 13:23:22 +00002793 SetBreakPoint(foo, 3);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002794 break_point_hit_count = 0;
2795 foo->Call(env->Global(), 0, NULL);
2796
2797 // Without stepping only active break points are hit.
2798 CHECK_EQ(1, break_point_hit_count);
2799
iposva@chromium.org245aa852009-02-10 00:49:54 +00002800 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002801 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002802}
2803
2804
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002805// Test of the stepping mechanism for keyed load in a loop.
2806TEST(DebugStepKeyedLoadLoop) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002807 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002808 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002809
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002810 // Register a debug event listener which steps and counts.
2811 v8::Debug::SetDebugEventListener(DebugEventStep);
2812
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002813 // Create a function for testing stepping of keyed load. The statement 'y=1'
2814 // is there to have more than one breakable statement in the loop, TODO(315).
2815 v8::Local<v8::Function> foo = CompileFunction(
2816 &env,
2817 "function foo(a) {\n"
2818 " var x;\n"
2819 " var len = a.length;\n"
2820 " for (var i = 0; i < len; i++) {\n"
2821 " y = 1;\n"
2822 " x = a[i];\n"
2823 " }\n"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002824 "}\n"
2825 "y=0\n",
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002826 "foo");
2827
2828 // Create array [0,1,2,3,4,5,6,7,8,9]
2829 v8::Local<v8::Array> a = v8::Array::New(10);
2830 for (int i = 0; i < 10; i++) {
2831 a->Set(v8::Number::New(i), v8::Number::New(i));
2832 }
2833
2834 // Call function without any break points to ensure inlining is in place.
2835 const int kArgc = 1;
2836 v8::Handle<v8::Value> args[kArgc] = { a };
2837 foo->Call(env->Global(), kArgc, args);
2838
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002839 // Set up break point and step through the function.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002840 SetBreakPoint(foo, 3);
2841 step_action = StepNext;
2842 break_point_hit_count = 0;
2843 foo->Call(env->Global(), kArgc, args);
2844
2845 // With stepping all break locations are hit.
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00002846 CHECK_EQ(34, break_point_hit_count);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002847
2848 v8::Debug::SetDebugEventListener(NULL);
2849 CheckDebuggerUnloaded();
2850}
2851
2852
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002853// Test of the stepping mechanism for keyed store in a loop.
2854TEST(DebugStepKeyedStoreLoop) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002855 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002856 v8::HandleScope scope(env->GetIsolate());
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002857
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002858 // Register a debug event listener which steps and counts.
2859 v8::Debug::SetDebugEventListener(DebugEventStep);
2860
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002861 // Create a function for testing stepping of keyed store. The statement 'y=1'
2862 // is there to have more than one breakable statement in the loop, TODO(315).
2863 v8::Local<v8::Function> foo = CompileFunction(
2864 &env,
2865 "function foo(a) {\n"
2866 " var len = a.length;\n"
2867 " for (var i = 0; i < len; i++) {\n"
2868 " y = 1;\n"
2869 " a[i] = 42;\n"
2870 " }\n"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002871 "}\n"
2872 "y=0\n",
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002873 "foo");
2874
2875 // Create array [0,1,2,3,4,5,6,7,8,9]
2876 v8::Local<v8::Array> a = v8::Array::New(10);
2877 for (int i = 0; i < 10; i++) {
2878 a->Set(v8::Number::New(i), v8::Number::New(i));
2879 }
2880
2881 // Call function without any break points to ensure inlining is in place.
2882 const int kArgc = 1;
2883 v8::Handle<v8::Value> args[kArgc] = { a };
2884 foo->Call(env->Global(), kArgc, args);
2885
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002886 // Set up break point and step through the function.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002887 SetBreakPoint(foo, 3);
2888 step_action = StepNext;
2889 break_point_hit_count = 0;
2890 foo->Call(env->Global(), kArgc, args);
2891
2892 // With stepping all break locations are hit.
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00002893 CHECK_EQ(33, break_point_hit_count);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002894
2895 v8::Debug::SetDebugEventListener(NULL);
2896 CheckDebuggerUnloaded();
2897}
2898
2899
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002900// Test of the stepping mechanism for named load in a loop.
2901TEST(DebugStepNamedLoadLoop) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002902 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002903 v8::HandleScope scope(env->GetIsolate());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002904
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002905 // Register a debug event listener which steps and counts.
2906 v8::Debug::SetDebugEventListener(DebugEventStep);
2907
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002908 // Create a function for testing stepping of named load.
2909 v8::Local<v8::Function> foo = CompileFunction(
2910 &env,
2911 "function foo() {\n"
2912 " var a = [];\n"
2913 " var s = \"\";\n"
2914 " for (var i = 0; i < 10; i++) {\n"
2915 " var v = new V(i, i + 1);\n"
2916 " v.y;\n"
2917 " a.length;\n" // Special case: array length.
2918 " s.length;\n" // Special case: string length.
2919 " }\n"
2920 "}\n"
2921 "function V(x, y) {\n"
2922 " this.x = x;\n"
2923 " this.y = y;\n"
2924 "}\n",
2925 "foo");
2926
2927 // Call function without any break points to ensure inlining is in place.
2928 foo->Call(env->Global(), 0, NULL);
2929
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002930 // Set up break point and step through the function.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002931 SetBreakPoint(foo, 4);
2932 step_action = StepNext;
2933 break_point_hit_count = 0;
2934 foo->Call(env->Global(), 0, NULL);
2935
2936 // With stepping all break locations are hit.
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00002937 CHECK_EQ(54, break_point_hit_count);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002938
2939 v8::Debug::SetDebugEventListener(NULL);
2940 CheckDebuggerUnloaded();
2941}
2942
2943
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002944static void DoDebugStepNamedStoreLoop(int expected) {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002945 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002946 v8::HandleScope scope(env->GetIsolate());
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002947
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002948 // Register a debug event listener which steps and counts.
2949 v8::Debug::SetDebugEventListener(DebugEventStep);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002950
2951 // Create a function for testing stepping of named store.
2952 v8::Local<v8::Function> foo = CompileFunction(
2953 &env,
2954 "function foo() {\n"
2955 " var a = {a:1};\n"
2956 " for (var i = 0; i < 10; i++) {\n"
2957 " a.a = 2\n"
2958 " }\n"
2959 "}\n",
2960 "foo");
2961
2962 // Call function without any break points to ensure inlining is in place.
2963 foo->Call(env->Global(), 0, NULL);
2964
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002965 // Set up break point and step through the function.
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002966 SetBreakPoint(foo, 3);
2967 step_action = StepNext;
2968 break_point_hit_count = 0;
2969 foo->Call(env->Global(), 0, NULL);
2970
2971 // With stepping all expected break locations are hit.
2972 CHECK_EQ(expected, break_point_hit_count);
2973
2974 v8::Debug::SetDebugEventListener(NULL);
2975 CheckDebuggerUnloaded();
2976}
2977
2978
2979// Test of the stepping mechanism for named load in a loop.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002980TEST(DebugStepNamedStoreLoop) {
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00002981 DoDebugStepNamedStoreLoop(23);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002982}
2983
2984
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002985// Test the stepping mechanism with different ICs.
2986TEST(DebugStepLinearMixedICs) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002987 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002988 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002989
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00002990 // Register a debug event listener which steps and counts.
2991 v8::Debug::SetDebugEventListener(DebugEventStep);
2992
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002993 // Create a function for testing stepping.
2994 v8::Local<v8::Function> foo = CompileFunction(&env,
2995 "function bar() {};"
2996 "function foo() {"
2997 " var x;"
2998 " var index='name';"
2999 " var y = {};"
3000 " a=1;b=2;x=a;y[index]=3;x=y[index];bar();}", "foo");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003001
3002 // Run functions to allow them to get optimized.
3003 CompileRun("a=0; b=0; bar(); foo();");
3004
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003005 SetBreakPoint(foo, 0);
3006
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003007 step_action = StepIn;
3008 break_point_hit_count = 0;
3009 foo->Call(env->Global(), 0, NULL);
3010
ager@chromium.org4af710e2009-09-15 12:20:11 +00003011 // With stepping all break locations are hit.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003012 CHECK_EQ(11, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003013
iposva@chromium.org245aa852009-02-10 00:49:54 +00003014 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003015 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003016
3017 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003018 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003019
ager@chromium.org381abbb2009-02-25 13:23:22 +00003020 SetBreakPoint(foo, 0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003021 break_point_hit_count = 0;
3022 foo->Call(env->Global(), 0, NULL);
3023
3024 // Without stepping only active break points are hit.
3025 CHECK_EQ(1, break_point_hit_count);
3026
iposva@chromium.org245aa852009-02-10 00:49:54 +00003027 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003028 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003029}
3030
3031
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003032TEST(DebugStepDeclarations) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003033 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003034 v8::HandleScope scope(env->GetIsolate());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003035
3036 // Register a debug event listener which steps and counts.
3037 v8::Debug::SetDebugEventListener(DebugEventStep);
3038
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003039 // Create a function for testing stepping. Run it to allow it to get
3040 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003041 const char* src = "function foo() { "
3042 " var a;"
3043 " var b = 1;"
3044 " var c = foo;"
3045 " var d = Math.floor;"
3046 " var e = b + d(1.2);"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003047 "}"
3048 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003049 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003050
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003051 SetBreakPoint(foo, 0);
3052
3053 // Stepping through the declarations.
3054 step_action = StepIn;
3055 break_point_hit_count = 0;
3056 foo->Call(env->Global(), 0, NULL);
3057 CHECK_EQ(6, break_point_hit_count);
3058
3059 // Get rid of the debug event listener.
3060 v8::Debug::SetDebugEventListener(NULL);
3061 CheckDebuggerUnloaded();
3062}
3063
3064
3065TEST(DebugStepLocals) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003066 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003067 v8::HandleScope scope(env->GetIsolate());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003068
3069 // Register a debug event listener which steps and counts.
3070 v8::Debug::SetDebugEventListener(DebugEventStep);
3071
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003072 // Create a function for testing stepping. Run it to allow it to get
3073 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003074 const char* src = "function foo() { "
3075 " var a,b;"
3076 " a = 1;"
3077 " b = a + 2;"
3078 " b = 1 + 2 + 3;"
3079 " a = Math.floor(b);"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003080 "}"
3081 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003082 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003083
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003084 SetBreakPoint(foo, 0);
3085
3086 // Stepping through the declarations.
3087 step_action = StepIn;
3088 break_point_hit_count = 0;
3089 foo->Call(env->Global(), 0, NULL);
3090 CHECK_EQ(6, break_point_hit_count);
3091
3092 // Get rid of the debug event listener.
3093 v8::Debug::SetDebugEventListener(NULL);
3094 CheckDebuggerUnloaded();
3095}
3096
3097
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003098TEST(DebugStepIf) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003099 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003100 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003101
3102 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003103 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003104
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003105 // Create a function for testing stepping. Run it to allow it to get
3106 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003107 const int argc = 1;
3108 const char* src = "function foo(x) { "
3109 " a = 1;"
3110 " if (x) {"
3111 " b = 1;"
3112 " } else {"
3113 " c = 1;"
3114 " d = 1;"
3115 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003116 "}"
3117 "a=0; b=0; c=0; d=0; foo()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003118 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3119 SetBreakPoint(foo, 0);
3120
3121 // Stepping through the true part.
3122 step_action = StepIn;
3123 break_point_hit_count = 0;
3124 v8::Handle<v8::Value> argv_true[argc] = { v8::True() };
3125 foo->Call(env->Global(), argc, argv_true);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003126 CHECK_EQ(4, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003127
3128 // Stepping through the false part.
3129 step_action = StepIn;
3130 break_point_hit_count = 0;
3131 v8::Handle<v8::Value> argv_false[argc] = { v8::False() };
3132 foo->Call(env->Global(), argc, argv_false);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003133 CHECK_EQ(5, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003134
3135 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003136 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003137 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003138}
3139
3140
3141TEST(DebugStepSwitch) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003142 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003143 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003144
3145 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003146 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003147
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003148 // Create a function for testing stepping. Run it to allow it to get
3149 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003150 const int argc = 1;
3151 const char* src = "function foo(x) { "
3152 " a = 1;"
3153 " switch (x) {"
3154 " case 1:"
3155 " b = 1;"
3156 " case 2:"
3157 " c = 1;"
3158 " break;"
3159 " case 3:"
3160 " d = 1;"
3161 " e = 1;"
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003162 " f = 1;"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003163 " break;"
3164 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003165 "}"
3166 "a=0; b=0; c=0; d=0; e=0; f=0; foo()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003167 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3168 SetBreakPoint(foo, 0);
3169
3170 // One case with fall-through.
3171 step_action = StepIn;
3172 break_point_hit_count = 0;
3173 v8::Handle<v8::Value> argv_1[argc] = { v8::Number::New(1) };
3174 foo->Call(env->Global(), argc, argv_1);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003175 CHECK_EQ(6, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003176
3177 // Another case.
3178 step_action = StepIn;
3179 break_point_hit_count = 0;
3180 v8::Handle<v8::Value> argv_2[argc] = { v8::Number::New(2) };
3181 foo->Call(env->Global(), argc, argv_2);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003182 CHECK_EQ(5, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003183
3184 // Last case.
3185 step_action = StepIn;
3186 break_point_hit_count = 0;
3187 v8::Handle<v8::Value> argv_3[argc] = { v8::Number::New(3) };
3188 foo->Call(env->Global(), argc, argv_3);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003189 CHECK_EQ(7, break_point_hit_count);
3190
3191 // Get rid of the debug event listener.
3192 v8::Debug::SetDebugEventListener(NULL);
3193 CheckDebuggerUnloaded();
3194}
3195
3196
3197TEST(DebugStepWhile) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003198 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003199 v8::HandleScope scope(env->GetIsolate());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003200
3201 // Register a debug event listener which steps and counts.
3202 v8::Debug::SetDebugEventListener(DebugEventStep);
3203
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003204 // Create a function for testing stepping. Run it to allow it to get
3205 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003206 const int argc = 1;
3207 const char* src = "function foo(x) { "
3208 " var a = 0;"
3209 " while (a < x) {"
3210 " a++;"
3211 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003212 "}"
3213 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003214 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3215 SetBreakPoint(foo, 8); // "var a = 0;"
3216
3217 // Looping 10 times.
3218 step_action = StepIn;
3219 break_point_hit_count = 0;
3220 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
3221 foo->Call(env->Global(), argc, argv_10);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003222 CHECK_EQ(22, break_point_hit_count);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003223
3224 // Looping 100 times.
3225 step_action = StepIn;
3226 break_point_hit_count = 0;
3227 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
3228 foo->Call(env->Global(), argc, argv_100);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003229 CHECK_EQ(202, break_point_hit_count);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003230
3231 // Get rid of the debug event listener.
3232 v8::Debug::SetDebugEventListener(NULL);
3233 CheckDebuggerUnloaded();
3234}
3235
3236
3237TEST(DebugStepDoWhile) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003238 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003239 v8::HandleScope scope(env->GetIsolate());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003240
3241 // Register a debug event listener which steps and counts.
3242 v8::Debug::SetDebugEventListener(DebugEventStep);
3243
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003244 // Create a function for testing stepping. Run it to allow it to get
3245 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003246 const int argc = 1;
3247 const char* src = "function foo(x) { "
3248 " var a = 0;"
3249 " do {"
3250 " a++;"
3251 " } while (a < x)"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003252 "}"
3253 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003254 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3255 SetBreakPoint(foo, 8); // "var a = 0;"
3256
3257 // Looping 10 times.
3258 step_action = StepIn;
3259 break_point_hit_count = 0;
3260 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
3261 foo->Call(env->Global(), argc, argv_10);
3262 CHECK_EQ(22, break_point_hit_count);
3263
3264 // Looping 100 times.
3265 step_action = StepIn;
3266 break_point_hit_count = 0;
3267 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
3268 foo->Call(env->Global(), argc, argv_100);
3269 CHECK_EQ(202, break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003270
3271 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003272 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003273 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003274}
3275
3276
3277TEST(DebugStepFor) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003278 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003279 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003280
3281 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003282 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003283
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003284 // Create a function for testing stepping. Run it to allow it to get
3285 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003286 const int argc = 1;
3287 const char* src = "function foo(x) { "
3288 " a = 1;"
3289 " for (i = 0; i < x; i++) {"
3290 " b = 1;"
3291 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003292 "}"
3293 "a=0; b=0; i=0; foo()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003294 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003295
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003296 SetBreakPoint(foo, 8); // "a = 1;"
3297
3298 // Looping 10 times.
3299 step_action = StepIn;
3300 break_point_hit_count = 0;
3301 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
3302 foo->Call(env->Global(), argc, argv_10);
3303 CHECK_EQ(23, break_point_hit_count);
3304
3305 // Looping 100 times.
3306 step_action = StepIn;
3307 break_point_hit_count = 0;
3308 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
3309 foo->Call(env->Global(), argc, argv_100);
3310 CHECK_EQ(203, break_point_hit_count);
3311
3312 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003313 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003314 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003315}
3316
3317
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003318TEST(DebugStepForContinue) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003319 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003320 v8::HandleScope scope(env->GetIsolate());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003321
3322 // Register a debug event listener which steps and counts.
3323 v8::Debug::SetDebugEventListener(DebugEventStep);
3324
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003325 // Create a function for testing stepping. Run it to allow it to get
3326 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003327 const int argc = 1;
3328 const char* src = "function foo(x) { "
3329 " var a = 0;"
3330 " var b = 0;"
3331 " var c = 0;"
3332 " for (var i = 0; i < x; i++) {"
3333 " a++;"
3334 " if (a % 2 == 0) continue;"
3335 " b++;"
3336 " c++;"
3337 " }"
3338 " return b;"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003339 "}"
3340 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003341 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3342 v8::Handle<v8::Value> result;
3343 SetBreakPoint(foo, 8); // "var a = 0;"
3344
3345 // Each loop generates 4 or 5 steps depending on whether a is equal.
3346
3347 // Looping 10 times.
3348 step_action = StepIn;
3349 break_point_hit_count = 0;
3350 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
3351 result = foo->Call(env->Global(), argc, argv_10);
3352 CHECK_EQ(5, result->Int32Value());
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00003353 CHECK_EQ(51, break_point_hit_count);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003354
3355 // Looping 100 times.
3356 step_action = StepIn;
3357 break_point_hit_count = 0;
3358 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
3359 result = foo->Call(env->Global(), argc, argv_100);
3360 CHECK_EQ(50, result->Int32Value());
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00003361 CHECK_EQ(456, break_point_hit_count);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003362
3363 // Get rid of the debug event listener.
3364 v8::Debug::SetDebugEventListener(NULL);
3365 CheckDebuggerUnloaded();
3366}
3367
3368
3369TEST(DebugStepForBreak) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003370 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003371 v8::HandleScope scope(env->GetIsolate());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003372
3373 // Register a debug event listener which steps and counts.
3374 v8::Debug::SetDebugEventListener(DebugEventStep);
3375
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003376 // Create a function for testing stepping. Run it to allow it to get
3377 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003378 const int argc = 1;
3379 const char* src = "function foo(x) { "
3380 " var a = 0;"
3381 " var b = 0;"
3382 " var c = 0;"
3383 " for (var i = 0; i < 1000; i++) {"
3384 " a++;"
3385 " if (a == x) break;"
3386 " b++;"
3387 " c++;"
3388 " }"
3389 " return b;"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003390 "}"
3391 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003392 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3393 v8::Handle<v8::Value> result;
3394 SetBreakPoint(foo, 8); // "var a = 0;"
3395
3396 // Each loop generates 5 steps except for the last (when break is executed)
3397 // which only generates 4.
3398
3399 // Looping 10 times.
3400 step_action = StepIn;
3401 break_point_hit_count = 0;
3402 v8::Handle<v8::Value> argv_10[argc] = { v8::Number::New(10) };
3403 result = foo->Call(env->Global(), argc, argv_10);
3404 CHECK_EQ(9, result->Int32Value());
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00003405 CHECK_EQ(54, break_point_hit_count);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003406
3407 // Looping 100 times.
3408 step_action = StepIn;
3409 break_point_hit_count = 0;
3410 v8::Handle<v8::Value> argv_100[argc] = { v8::Number::New(100) };
3411 result = foo->Call(env->Global(), argc, argv_100);
3412 CHECK_EQ(99, result->Int32Value());
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00003413 CHECK_EQ(504, break_point_hit_count);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003414
3415 // Get rid of the debug event listener.
3416 v8::Debug::SetDebugEventListener(NULL);
3417 CheckDebuggerUnloaded();
3418}
3419
3420
3421TEST(DebugStepForIn) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003422 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003423 v8::HandleScope scope(env->GetIsolate());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003424
3425 // Register a debug event listener which steps and counts.
3426 v8::Debug::SetDebugEventListener(DebugEventStep);
3427
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003428 // Create a function for testing stepping. Run it to allow it to get
3429 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003430 v8::Local<v8::Function> foo;
3431 const char* src_1 = "function foo() { "
3432 " var a = [1, 2];"
3433 " for (x in a) {"
3434 " b = 0;"
3435 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003436 "}"
3437 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003438 foo = CompileFunction(&env, src_1, "foo");
3439 SetBreakPoint(foo, 0); // "var a = ..."
3440
3441 step_action = StepIn;
3442 break_point_hit_count = 0;
3443 foo->Call(env->Global(), 0, NULL);
3444 CHECK_EQ(6, break_point_hit_count);
3445
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003446 // Create a function for testing stepping. Run it to allow it to get
3447 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003448 const char* src_2 = "function foo() { "
3449 " var a = {a:[1, 2, 3]};"
3450 " for (x in a.a) {"
3451 " b = 0;"
3452 " }"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003453 "}"
3454 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003455 foo = CompileFunction(&env, src_2, "foo");
3456 SetBreakPoint(foo, 0); // "var a = ..."
3457
3458 step_action = StepIn;
3459 break_point_hit_count = 0;
3460 foo->Call(env->Global(), 0, NULL);
3461 CHECK_EQ(8, break_point_hit_count);
3462
3463 // Get rid of the debug event listener.
3464 v8::Debug::SetDebugEventListener(NULL);
3465 CheckDebuggerUnloaded();
3466}
3467
3468
3469TEST(DebugStepWith) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003470 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003471 v8::HandleScope scope(env->GetIsolate());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003472
3473 // Register a debug event listener which steps and counts.
3474 v8::Debug::SetDebugEventListener(DebugEventStep);
3475
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003476 // Create a function for testing stepping. Run it to allow it to get
3477 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003478 const char* src = "function foo(x) { "
3479 " var a = {};"
3480 " with (a) {}"
3481 " with (b) {}"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003482 "}"
3483 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003484 env->Global()->Set(v8::String::New("b"), v8::Object::New());
3485 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3486 v8::Handle<v8::Value> result;
3487 SetBreakPoint(foo, 8); // "var a = {};"
3488
3489 step_action = StepIn;
3490 break_point_hit_count = 0;
3491 foo->Call(env->Global(), 0, NULL);
3492 CHECK_EQ(4, break_point_hit_count);
3493
3494 // Get rid of the debug event listener.
3495 v8::Debug::SetDebugEventListener(NULL);
3496 CheckDebuggerUnloaded();
3497}
3498
3499
3500TEST(DebugConditional) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003501 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003502 v8::HandleScope scope(env->GetIsolate());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003503
3504 // Register a debug event listener which steps and counts.
3505 v8::Debug::SetDebugEventListener(DebugEventStep);
3506
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003507 // Create a function for testing stepping. Run it to allow it to get
3508 // optimized.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003509 const char* src = "function foo(x) { "
3510 " var a;"
3511 " a = x ? 1 : 2;"
3512 " return a;"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003513 "}"
3514 "foo()";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003515 v8::Local<v8::Function> foo = CompileFunction(&env, src, "foo");
3516 SetBreakPoint(foo, 0); // "var a;"
3517
3518 step_action = StepIn;
3519 break_point_hit_count = 0;
3520 foo->Call(env->Global(), 0, NULL);
3521 CHECK_EQ(5, break_point_hit_count);
3522
3523 step_action = StepIn;
3524 break_point_hit_count = 0;
3525 const int argc = 1;
3526 v8::Handle<v8::Value> argv_true[argc] = { v8::True() };
3527 foo->Call(env->Global(), argc, argv_true);
3528 CHECK_EQ(5, break_point_hit_count);
3529
3530 // Get rid of the debug event listener.
3531 v8::Debug::SetDebugEventListener(NULL);
3532 CheckDebuggerUnloaded();
3533}
3534
3535
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003536TEST(StepInOutSimple) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003537 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003538 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003539
3540 // Create a function for checking the function when hitting a break point.
3541 frame_function_name = CompileFunction(&env,
3542 frame_function_name_source,
3543 "frame_function_name");
3544
3545 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003546 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003547
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003548 // Create a function for testing stepping. Run it to allow it to get
3549 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003550 const char* src = "function a() {b();c();}; "
3551 "function b() {c();}; "
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003552 "function c() {}; "
3553 "a(); b(); c()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003554 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
3555 SetBreakPoint(a, 0);
3556
3557 // Step through invocation of a with step in.
3558 step_action = StepIn;
3559 break_point_hit_count = 0;
3560 expected_step_sequence = "abcbaca";
3561 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003562 CHECK_EQ(StrLength(expected_step_sequence),
3563 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003564
3565 // Step through invocation of a with step next.
3566 step_action = StepNext;
3567 break_point_hit_count = 0;
3568 expected_step_sequence = "aaa";
3569 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003570 CHECK_EQ(StrLength(expected_step_sequence),
3571 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003572
3573 // Step through invocation of a with step out.
3574 step_action = StepOut;
3575 break_point_hit_count = 0;
3576 expected_step_sequence = "a";
3577 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003578 CHECK_EQ(StrLength(expected_step_sequence),
3579 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003580
3581 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003582 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003583 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003584}
3585
3586
3587TEST(StepInOutTree) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003588 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003589 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003590
3591 // Create a function for checking the function when hitting a break point.
3592 frame_function_name = CompileFunction(&env,
3593 frame_function_name_source,
3594 "frame_function_name");
3595
3596 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003597 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003598
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003599 // Create a function for testing stepping. Run it to allow it to get
3600 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003601 const char* src = "function a() {b(c(d()),d());c(d());d()}; "
3602 "function b(x,y) {c();}; "
3603 "function c(x) {}; "
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003604 "function d() {}; "
3605 "a(); b(); c(); d()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003606 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
3607 SetBreakPoint(a, 0);
3608
3609 // Step through invocation of a with step in.
3610 step_action = StepIn;
3611 break_point_hit_count = 0;
3612 expected_step_sequence = "adacadabcbadacada";
3613 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003614 CHECK_EQ(StrLength(expected_step_sequence),
3615 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003616
3617 // Step through invocation of a with step next.
3618 step_action = StepNext;
3619 break_point_hit_count = 0;
3620 expected_step_sequence = "aaaa";
3621 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003622 CHECK_EQ(StrLength(expected_step_sequence),
3623 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003624
3625 // Step through invocation of a with step out.
3626 step_action = StepOut;
3627 break_point_hit_count = 0;
3628 expected_step_sequence = "a";
3629 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003630 CHECK_EQ(StrLength(expected_step_sequence),
3631 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003632
3633 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003634 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003635 CheckDebuggerUnloaded(true);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003636}
3637
3638
3639TEST(StepInOutBranch) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003640 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003641 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003642
3643 // Create a function for checking the function when hitting a break point.
3644 frame_function_name = CompileFunction(&env,
3645 frame_function_name_source,
3646 "frame_function_name");
3647
3648 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003649 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003650
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003651 // Create a function for testing stepping. Run it to allow it to get
3652 // optimized.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003653 const char* src = "function a() {b(false);c();}; "
3654 "function b(x) {if(x){c();};}; "
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003655 "function c() {}; "
3656 "a(); b(); c()";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003657 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
3658 SetBreakPoint(a, 0);
3659
3660 // Step through invocation of a.
3661 step_action = StepIn;
3662 break_point_hit_count = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003663 expected_step_sequence = "abbaca";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003664 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003665 CHECK_EQ(StrLength(expected_step_sequence),
3666 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003667
3668 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003669 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003670 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003671}
3672
3673
3674// Test that step in does not step into native functions.
3675TEST(DebugStepNatives) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003676 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003677 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003678
3679 // Create a function for testing stepping.
3680 v8::Local<v8::Function> foo = CompileFunction(
3681 &env,
3682 "function foo(){debugger;Math.sin(1);}",
3683 "foo");
3684
3685 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003686 v8::Debug::SetDebugEventListener(DebugEventStep);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003687
3688 step_action = StepIn;
3689 break_point_hit_count = 0;
3690 foo->Call(env->Global(), 0, NULL);
3691
3692 // With stepping all break locations are hit.
3693 CHECK_EQ(3, break_point_hit_count);
3694
iposva@chromium.org245aa852009-02-10 00:49:54 +00003695 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003696 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003697
3698 // Register a debug event listener which just counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00003699 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003700
3701 break_point_hit_count = 0;
3702 foo->Call(env->Global(), 0, NULL);
3703
3704 // Without stepping only active break points are hit.
3705 CHECK_EQ(1, break_point_hit_count);
3706
iposva@chromium.org245aa852009-02-10 00:49:54 +00003707 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003708 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003709}
3710
3711
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003712// Test that step in works with function.apply.
3713TEST(DebugStepFunctionApply) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003714 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003715 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003716
3717 // Create a function for testing stepping.
3718 v8::Local<v8::Function> foo = CompileFunction(
3719 &env,
3720 "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }"
3721 "function foo(){ debugger; bar.apply(this, [1,2,3]); }",
3722 "foo");
3723
3724 // Register a debug event listener which steps and counts.
3725 v8::Debug::SetDebugEventListener(DebugEventStep);
3726
3727 step_action = StepIn;
3728 break_point_hit_count = 0;
3729 foo->Call(env->Global(), 0, NULL);
3730
3731 // With stepping all break locations are hit.
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003732 CHECK_EQ(7, break_point_hit_count);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003733
3734 v8::Debug::SetDebugEventListener(NULL);
3735 CheckDebuggerUnloaded();
3736
3737 // Register a debug event listener which just counts.
3738 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
3739
3740 break_point_hit_count = 0;
3741 foo->Call(env->Global(), 0, NULL);
3742
3743 // Without stepping only the debugger statement is hit.
3744 CHECK_EQ(1, break_point_hit_count);
3745
3746 v8::Debug::SetDebugEventListener(NULL);
3747 CheckDebuggerUnloaded();
3748}
3749
3750
3751// Test that step in works with function.call.
3752TEST(DebugStepFunctionCall) {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003753 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003754 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003755
3756 // Create a function for testing stepping.
3757 v8::Local<v8::Function> foo = CompileFunction(
3758 &env,
3759 "function bar(x, y, z) { if (x == 1) { a = y; b = z; } }"
3760 "function foo(a){ debugger;"
3761 " if (a) {"
3762 " bar.call(this, 1, 2, 3);"
3763 " } else {"
3764 " bar.call(this, 0);"
3765 " }"
3766 "}",
3767 "foo");
3768
3769 // Register a debug event listener which steps and counts.
3770 v8::Debug::SetDebugEventListener(DebugEventStep);
3771 step_action = StepIn;
3772
3773 // Check stepping where the if condition in bar is false.
3774 break_point_hit_count = 0;
3775 foo->Call(env->Global(), 0, NULL);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003776 CHECK_EQ(6, break_point_hit_count);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003777
3778 // Check stepping where the if condition in bar is true.
3779 break_point_hit_count = 0;
3780 const int argc = 1;
3781 v8::Handle<v8::Value> argv[argc] = { v8::True() };
3782 foo->Call(env->Global(), argc, argv);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00003783 CHECK_EQ(8, break_point_hit_count);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00003784
3785 v8::Debug::SetDebugEventListener(NULL);
3786 CheckDebuggerUnloaded();
3787
3788 // Register a debug event listener which just counts.
3789 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount);
3790
3791 break_point_hit_count = 0;
3792 foo->Call(env->Global(), 0, NULL);
3793
3794 // Without stepping only the debugger statement is hit.
3795 CHECK_EQ(1, break_point_hit_count);
3796
3797 v8::Debug::SetDebugEventListener(NULL);
3798 CheckDebuggerUnloaded();
3799}
3800
3801
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003802// Tests that breakpoint will be hit if it's set in script.
3803TEST(PauseInScript) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003804 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003805 v8::HandleScope scope(env->GetIsolate());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003806 env.ExposeDebug();
3807
3808 // Register a debug event listener which counts.
3809 v8::Debug::SetDebugEventListener(DebugEventCounter);
3810
3811 // Create a script that returns a function.
3812 const char* src = "(function (evt) {})";
3813 const char* script_name = "StepInHandlerTest";
3814
3815 // Set breakpoint in the script.
3816 SetScriptBreakPointByNameFromJS(script_name, 0, -1);
3817 break_point_hit_count = 0;
3818
3819 v8::ScriptOrigin origin(v8::String::New(script_name), v8::Integer::New(0));
3820 v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(src),
3821 &origin);
3822 v8::Local<v8::Value> r = script->Run();
3823
3824 CHECK(r->IsFunction());
3825 CHECK_EQ(1, break_point_hit_count);
3826
3827 // Get rid of the debug event listener.
3828 v8::Debug::SetDebugEventListener(NULL);
3829 CheckDebuggerUnloaded();
3830}
3831
3832
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003833// Test break on exceptions. For each exception break combination the number
3834// of debug event exception callbacks and message callbacks are collected. The
ager@chromium.org8bb60582008-12-11 12:02:20 +00003835// number of debug event exception callbacks are used to check that the
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003836// debugger is called correctly and the number of message callbacks is used to
3837// check that uncaught exceptions are still returned even if there is a break
3838// for them.
3839TEST(BreakOnException) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003840 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003841 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003842 env.ExposeDebug();
3843
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003844 v8::internal::Isolate::Current()->TraceException(false);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003845
3846 // Create functions for testing break on exception.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003847 CompileFunction(&env, "function throws(){throw 1;}", "throws");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003848 v8::Local<v8::Function> caught =
3849 CompileFunction(&env,
3850 "function caught(){try {throws();} catch(e) {};}",
3851 "caught");
3852 v8::Local<v8::Function> notCaught =
3853 CompileFunction(&env, "function notCaught(){throws();}", "notCaught");
3854
3855 v8::V8::AddMessageListener(MessageCallbackCount);
iposva@chromium.org245aa852009-02-10 00:49:54 +00003856 v8::Debug::SetDebugEventListener(DebugEventCounter);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003857
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003858 // Initial state should be no break on exceptions.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003859 DebugEventCounterClear();
3860 MessageCallbackCountClear();
3861 caught->Call(env->Global(), 0, NULL);
3862 CHECK_EQ(0, exception_hit_count);
3863 CHECK_EQ(0, uncaught_exception_hit_count);
3864 CHECK_EQ(0, message_callback_count);
3865 notCaught->Call(env->Global(), 0, NULL);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003866 CHECK_EQ(0, exception_hit_count);
3867 CHECK_EQ(0, uncaught_exception_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003868 CHECK_EQ(1, message_callback_count);
3869
3870 // No break on exception
3871 DebugEventCounterClear();
3872 MessageCallbackCountClear();
3873 ChangeBreakOnException(false, false);
3874 caught->Call(env->Global(), 0, NULL);
3875 CHECK_EQ(0, exception_hit_count);
3876 CHECK_EQ(0, uncaught_exception_hit_count);
3877 CHECK_EQ(0, message_callback_count);
3878 notCaught->Call(env->Global(), 0, NULL);
3879 CHECK_EQ(0, exception_hit_count);
3880 CHECK_EQ(0, uncaught_exception_hit_count);
3881 CHECK_EQ(1, message_callback_count);
3882
3883 // Break on uncaught exception
3884 DebugEventCounterClear();
3885 MessageCallbackCountClear();
3886 ChangeBreakOnException(false, true);
3887 caught->Call(env->Global(), 0, NULL);
3888 CHECK_EQ(0, exception_hit_count);
3889 CHECK_EQ(0, uncaught_exception_hit_count);
3890 CHECK_EQ(0, message_callback_count);
3891 notCaught->Call(env->Global(), 0, NULL);
3892 CHECK_EQ(1, exception_hit_count);
3893 CHECK_EQ(1, uncaught_exception_hit_count);
3894 CHECK_EQ(1, message_callback_count);
3895
3896 // Break on exception and uncaught exception
3897 DebugEventCounterClear();
3898 MessageCallbackCountClear();
3899 ChangeBreakOnException(true, true);
3900 caught->Call(env->Global(), 0, NULL);
3901 CHECK_EQ(1, exception_hit_count);
3902 CHECK_EQ(0, uncaught_exception_hit_count);
3903 CHECK_EQ(0, message_callback_count);
3904 notCaught->Call(env->Global(), 0, NULL);
3905 CHECK_EQ(2, exception_hit_count);
3906 CHECK_EQ(1, uncaught_exception_hit_count);
3907 CHECK_EQ(1, message_callback_count);
3908
3909 // Break on exception
3910 DebugEventCounterClear();
3911 MessageCallbackCountClear();
3912 ChangeBreakOnException(true, false);
3913 caught->Call(env->Global(), 0, NULL);
3914 CHECK_EQ(1, exception_hit_count);
3915 CHECK_EQ(0, uncaught_exception_hit_count);
3916 CHECK_EQ(0, message_callback_count);
3917 notCaught->Call(env->Global(), 0, NULL);
3918 CHECK_EQ(2, exception_hit_count);
3919 CHECK_EQ(1, uncaught_exception_hit_count);
3920 CHECK_EQ(1, message_callback_count);
3921
3922 // No break on exception using JavaScript
3923 DebugEventCounterClear();
3924 MessageCallbackCountClear();
3925 ChangeBreakOnExceptionFromJS(false, false);
3926 caught->Call(env->Global(), 0, NULL);
3927 CHECK_EQ(0, exception_hit_count);
3928 CHECK_EQ(0, uncaught_exception_hit_count);
3929 CHECK_EQ(0, message_callback_count);
3930 notCaught->Call(env->Global(), 0, NULL);
3931 CHECK_EQ(0, exception_hit_count);
3932 CHECK_EQ(0, uncaught_exception_hit_count);
3933 CHECK_EQ(1, message_callback_count);
3934
3935 // Break on uncaught exception using JavaScript
3936 DebugEventCounterClear();
3937 MessageCallbackCountClear();
3938 ChangeBreakOnExceptionFromJS(false, true);
3939 caught->Call(env->Global(), 0, NULL);
3940 CHECK_EQ(0, exception_hit_count);
3941 CHECK_EQ(0, uncaught_exception_hit_count);
3942 CHECK_EQ(0, message_callback_count);
3943 notCaught->Call(env->Global(), 0, NULL);
3944 CHECK_EQ(1, exception_hit_count);
3945 CHECK_EQ(1, uncaught_exception_hit_count);
3946 CHECK_EQ(1, message_callback_count);
3947
3948 // Break on exception and uncaught exception using JavaScript
3949 DebugEventCounterClear();
3950 MessageCallbackCountClear();
3951 ChangeBreakOnExceptionFromJS(true, true);
3952 caught->Call(env->Global(), 0, NULL);
3953 CHECK_EQ(1, exception_hit_count);
3954 CHECK_EQ(0, message_callback_count);
3955 CHECK_EQ(0, uncaught_exception_hit_count);
3956 notCaught->Call(env->Global(), 0, NULL);
3957 CHECK_EQ(2, exception_hit_count);
3958 CHECK_EQ(1, uncaught_exception_hit_count);
3959 CHECK_EQ(1, message_callback_count);
3960
3961 // Break on exception using JavaScript
3962 DebugEventCounterClear();
3963 MessageCallbackCountClear();
3964 ChangeBreakOnExceptionFromJS(true, false);
3965 caught->Call(env->Global(), 0, NULL);
3966 CHECK_EQ(1, exception_hit_count);
3967 CHECK_EQ(0, uncaught_exception_hit_count);
3968 CHECK_EQ(0, message_callback_count);
3969 notCaught->Call(env->Global(), 0, NULL);
3970 CHECK_EQ(2, exception_hit_count);
3971 CHECK_EQ(1, uncaught_exception_hit_count);
3972 CHECK_EQ(1, message_callback_count);
3973
iposva@chromium.org245aa852009-02-10 00:49:54 +00003974 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003975 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003976 v8::V8::RemoveMessageListeners(MessageCallbackCount);
3977}
3978
3979
ager@chromium.org8bb60582008-12-11 12:02:20 +00003980// Test break on exception from compiler errors. When compiling using
3981// v8::Script::Compile there is no JavaScript stack whereas when compiling using
3982// eval there are JavaScript frames.
3983TEST(BreakOnCompileException) {
ager@chromium.org8bb60582008-12-11 12:02:20 +00003984 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003985 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org8bb60582008-12-11 12:02:20 +00003986
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003987 // For this test, we want to break on uncaught exceptions:
3988 ChangeBreakOnException(false, true);
3989
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003990 v8::internal::Isolate::Current()->TraceException(false);
ager@chromium.org8bb60582008-12-11 12:02:20 +00003991
3992 // Create a function for checking the function when hitting a break point.
3993 frame_count = CompileFunction(&env, frame_count_source, "frame_count");
3994
3995 v8::V8::AddMessageListener(MessageCallbackCount);
iposva@chromium.org245aa852009-02-10 00:49:54 +00003996 v8::Debug::SetDebugEventListener(DebugEventCounter);
ager@chromium.org8bb60582008-12-11 12:02:20 +00003997
3998 DebugEventCounterClear();
3999 MessageCallbackCountClear();
4000
4001 // Check initial state.
4002 CHECK_EQ(0, exception_hit_count);
4003 CHECK_EQ(0, uncaught_exception_hit_count);
4004 CHECK_EQ(0, message_callback_count);
4005 CHECK_EQ(-1, last_js_stack_height);
4006
4007 // Throws SyntaxError: Unexpected end of input
4008 v8::Script::Compile(v8::String::New("+++"));
4009 CHECK_EQ(1, exception_hit_count);
4010 CHECK_EQ(1, uncaught_exception_hit_count);
4011 CHECK_EQ(1, message_callback_count);
4012 CHECK_EQ(0, last_js_stack_height); // No JavaScript stack.
4013
4014 // Throws SyntaxError: Unexpected identifier
4015 v8::Script::Compile(v8::String::New("x x"));
4016 CHECK_EQ(2, exception_hit_count);
4017 CHECK_EQ(2, uncaught_exception_hit_count);
4018 CHECK_EQ(2, message_callback_count);
4019 CHECK_EQ(0, last_js_stack_height); // No JavaScript stack.
4020
4021 // Throws SyntaxError: Unexpected end of input
4022 v8::Script::Compile(v8::String::New("eval('+++')"))->Run();
4023 CHECK_EQ(3, exception_hit_count);
4024 CHECK_EQ(3, uncaught_exception_hit_count);
4025 CHECK_EQ(3, message_callback_count);
4026 CHECK_EQ(1, last_js_stack_height);
4027
4028 // Throws SyntaxError: Unexpected identifier
4029 v8::Script::Compile(v8::String::New("eval('x x')"))->Run();
4030 CHECK_EQ(4, exception_hit_count);
4031 CHECK_EQ(4, uncaught_exception_hit_count);
4032 CHECK_EQ(4, message_callback_count);
4033 CHECK_EQ(1, last_js_stack_height);
4034}
4035
4036
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004037TEST(StepWithException) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004038 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004039 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004040
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004041 // For this test, we want to break on uncaught exceptions:
4042 ChangeBreakOnException(false, true);
4043
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004044 // Create a function for checking the function when hitting a break point.
4045 frame_function_name = CompileFunction(&env,
4046 frame_function_name_source,
4047 "frame_function_name");
4048
4049 // Register a debug event listener which steps and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00004050 v8::Debug::SetDebugEventListener(DebugEventStepSequence);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004051
4052 // Create functions for testing stepping.
4053 const char* src = "function a() { n(); }; "
4054 "function b() { c(); }; "
4055 "function c() { n(); }; "
4056 "function d() { x = 1; try { e(); } catch(x) { x = 2; } }; "
4057 "function e() { n(); }; "
4058 "function f() { x = 1; try { g(); } catch(x) { x = 2; } }; "
4059 "function g() { h(); }; "
4060 "function h() { x = 1; throw 1; }; ";
4061
4062 // Step through invocation of a.
4063 v8::Local<v8::Function> a = CompileFunction(&env, src, "a");
4064 SetBreakPoint(a, 0);
4065 step_action = StepIn;
4066 break_point_hit_count = 0;
4067 expected_step_sequence = "aa";
4068 a->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004069 CHECK_EQ(StrLength(expected_step_sequence),
4070 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004071
4072 // Step through invocation of b + c.
4073 v8::Local<v8::Function> b = CompileFunction(&env, src, "b");
4074 SetBreakPoint(b, 0);
4075 step_action = StepIn;
4076 break_point_hit_count = 0;
4077 expected_step_sequence = "bcc";
4078 b->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004079 CHECK_EQ(StrLength(expected_step_sequence),
4080 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004081 // Step through invocation of d + e.
4082 v8::Local<v8::Function> d = CompileFunction(&env, src, "d");
4083 SetBreakPoint(d, 0);
4084 ChangeBreakOnException(false, true);
4085 step_action = StepIn;
4086 break_point_hit_count = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00004087 expected_step_sequence = "ddedd";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004088 d->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004089 CHECK_EQ(StrLength(expected_step_sequence),
4090 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004091
4092 // Step through invocation of d + e now with break on caught exceptions.
4093 ChangeBreakOnException(true, true);
4094 step_action = StepIn;
4095 break_point_hit_count = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00004096 expected_step_sequence = "ddeedd";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004097 d->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004098 CHECK_EQ(StrLength(expected_step_sequence),
4099 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004100
4101 // Step through invocation of f + g + h.
4102 v8::Local<v8::Function> f = CompileFunction(&env, src, "f");
4103 SetBreakPoint(f, 0);
4104 ChangeBreakOnException(false, true);
4105 step_action = StepIn;
4106 break_point_hit_count = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00004107 expected_step_sequence = "ffghhff";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004108 f->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004109 CHECK_EQ(StrLength(expected_step_sequence),
4110 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004111
4112 // Step through invocation of f + g + h now with break on caught exceptions.
4113 ChangeBreakOnException(true, true);
4114 step_action = StepIn;
4115 break_point_hit_count = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00004116 expected_step_sequence = "ffghhhff";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004117 f->Call(env->Global(), 0, NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004118 CHECK_EQ(StrLength(expected_step_sequence),
4119 break_point_hit_count);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004120
4121 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00004122 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00004123 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004124}
4125
4126
4127TEST(DebugBreak) {
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00004128#ifdef VERIFY_HEAP
4129 i::FLAG_verify_heap = true;
4130#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004131 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004132 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004133
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004134 // Register a debug event listener which sets the break flag and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00004135 v8::Debug::SetDebugEventListener(DebugEventBreak);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004136
4137 // Create a function for testing stepping.
4138 const char* src = "function f0() {}"
4139 "function f1(x1) {}"
4140 "function f2(x1,x2) {}"
4141 "function f3(x1,x2,x3) {}";
4142 v8::Local<v8::Function> f0 = CompileFunction(&env, src, "f0");
4143 v8::Local<v8::Function> f1 = CompileFunction(&env, src, "f1");
4144 v8::Local<v8::Function> f2 = CompileFunction(&env, src, "f2");
4145 v8::Local<v8::Function> f3 = CompileFunction(&env, src, "f3");
4146
4147 // Call the function to make sure it is compiled.
4148 v8::Handle<v8::Value> argv[] = { v8::Number::New(1),
4149 v8::Number::New(1),
4150 v8::Number::New(1),
4151 v8::Number::New(1) };
4152
4153 // Call all functions to make sure that they are compiled.
4154 f0->Call(env->Global(), 0, NULL);
4155 f1->Call(env->Global(), 0, NULL);
4156 f2->Call(env->Global(), 0, NULL);
4157 f3->Call(env->Global(), 0, NULL);
4158
4159 // Set the debug break flag.
4160 v8::Debug::DebugBreak();
4161
4162 // Call all functions with different argument count.
4163 break_point_hit_count = 0;
4164 for (unsigned int i = 0; i < ARRAY_SIZE(argv); i++) {
4165 f0->Call(env->Global(), i, argv);
4166 f1->Call(env->Global(), i, argv);
4167 f2->Call(env->Global(), i, argv);
4168 f3->Call(env->Global(), i, argv);
4169 }
4170
4171 // One break for each function called.
4172 CHECK_EQ(4 * ARRAY_SIZE(argv), break_point_hit_count);
4173
4174 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00004175 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00004176 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004177}
4178
4179
4180// Test to ensure that JavaScript code keeps running while the debug break
4181// through the stack limit flag is set but breaks are disabled.
4182TEST(DisableBreak) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004183 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004184 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004185
4186 // Register a debug event listener which sets the break flag and counts.
iposva@chromium.org245aa852009-02-10 00:49:54 +00004187 v8::Debug::SetDebugEventListener(DebugEventCounter);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004188
4189 // Create a function for testing stepping.
4190 const char* src = "function f() {g()};function g(){i=0; while(i<10){i++}}";
4191 v8::Local<v8::Function> f = CompileFunction(&env, src, "f");
4192
4193 // Set the debug break flag.
4194 v8::Debug::DebugBreak();
4195
4196 // Call all functions with different argument count.
4197 break_point_hit_count = 0;
4198 f->Call(env->Global(), 0, NULL);
4199 CHECK_EQ(1, break_point_hit_count);
4200
4201 {
4202 v8::Debug::DebugBreak();
4203 v8::internal::DisableBreak disable_break(true);
4204 f->Call(env->Global(), 0, NULL);
4205 CHECK_EQ(1, break_point_hit_count);
4206 }
4207
4208 f->Call(env->Global(), 0, NULL);
4209 CHECK_EQ(2, break_point_hit_count);
4210
4211 // Get rid of the debug event listener.
iposva@chromium.org245aa852009-02-10 00:49:54 +00004212 v8::Debug::SetDebugEventListener(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00004213 CheckDebuggerUnloaded();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004214}
4215
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004216static const char* kSimpleExtensionSource =
4217 "(function Foo() {"
4218 " return 4;"
4219 "})() ";
4220
4221// http://crbug.com/28933
4222// Test that debug break is disabled when bootstrapper is active.
4223TEST(NoBreakWhenBootstrapping) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004224 v8::HandleScope scope(v8::Isolate::GetCurrent());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004225
4226 // Register a debug event listener which sets the break flag and counts.
4227 v8::Debug::SetDebugEventListener(DebugEventCounter);
4228
4229 // Set the debug break flag.
4230 v8::Debug::DebugBreak();
4231 break_point_hit_count = 0;
4232 {
4233 // Create a context with an extension to make sure that some JavaScript
4234 // code is executed during bootstrapping.
4235 v8::RegisterExtension(new v8::Extension("simpletest",
4236 kSimpleExtensionSource));
4237 const char* extension_names[] = { "simpletest" };
4238 v8::ExtensionConfiguration extensions(1, extension_names);
4239 v8::Persistent<v8::Context> context = v8::Context::New(&extensions);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00004240 context.Dispose(context->GetIsolate());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004241 }
4242 // Check that no DebugBreak events occured during the context creation.
4243 CHECK_EQ(0, break_point_hit_count);
4244
4245 // Get rid of the debug event listener.
4246 v8::Debug::SetDebugEventListener(NULL);
4247 CheckDebuggerUnloaded();
4248}
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004249
4250static v8::Handle<v8::Array> NamedEnum(const v8::AccessorInfo&) {
4251 v8::Handle<v8::Array> result = v8::Array::New(3);
4252 result->Set(v8::Integer::New(0), v8::String::New("a"));
4253 result->Set(v8::Integer::New(1), v8::String::New("b"));
4254 result->Set(v8::Integer::New(2), v8::String::New("c"));
4255 return result;
4256}
4257
4258
4259static v8::Handle<v8::Array> IndexedEnum(const v8::AccessorInfo&) {
4260 v8::Handle<v8::Array> result = v8::Array::New(2);
4261 result->Set(v8::Integer::New(0), v8::Number::New(1));
4262 result->Set(v8::Integer::New(1), v8::Number::New(10));
4263 return result;
4264}
4265
4266
4267static v8::Handle<v8::Value> NamedGetter(v8::Local<v8::String> name,
4268 const v8::AccessorInfo& info) {
4269 v8::String::AsciiValue n(name);
4270 if (strcmp(*n, "a") == 0) {
4271 return v8::String::New("AA");
4272 } else if (strcmp(*n, "b") == 0) {
4273 return v8::String::New("BB");
4274 } else if (strcmp(*n, "c") == 0) {
4275 return v8::String::New("CC");
4276 } else {
4277 return v8::Undefined();
4278 }
4279
4280 return name;
4281}
4282
4283
4284static v8::Handle<v8::Value> IndexedGetter(uint32_t index,
4285 const v8::AccessorInfo& info) {
4286 return v8::Number::New(index + 1);
4287}
4288
4289
4290TEST(InterceptorPropertyMirror) {
4291 // Create a V8 environment with debug access.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004292 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004293 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004294 env.ExposeDebug();
4295
4296 // Create object with named interceptor.
4297 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New();
4298 named->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum);
4299 env->Global()->Set(v8::String::New("intercepted_named"),
4300 named->NewInstance());
4301
4302 // Create object with indexed interceptor.
4303 v8::Handle<v8::ObjectTemplate> indexed = v8::ObjectTemplate::New();
4304 indexed->SetIndexedPropertyHandler(IndexedGetter,
4305 NULL,
4306 NULL,
4307 NULL,
4308 IndexedEnum);
4309 env->Global()->Set(v8::String::New("intercepted_indexed"),
4310 indexed->NewInstance());
4311
4312 // Create object with both named and indexed interceptor.
4313 v8::Handle<v8::ObjectTemplate> both = v8::ObjectTemplate::New();
4314 both->SetNamedPropertyHandler(NamedGetter, NULL, NULL, NULL, NamedEnum);
4315 both->SetIndexedPropertyHandler(IndexedGetter, NULL, NULL, NULL, IndexedEnum);
4316 env->Global()->Set(v8::String::New("intercepted_both"), both->NewInstance());
4317
4318 // Get mirrors for the three objects with interceptor.
4319 CompileRun(
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004320 "var named_mirror = debug.MakeMirror(intercepted_named);"
4321 "var indexed_mirror = debug.MakeMirror(intercepted_indexed);"
4322 "var both_mirror = debug.MakeMirror(intercepted_both)");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004323 CHECK(CompileRun(
4324 "named_mirror instanceof debug.ObjectMirror")->BooleanValue());
4325 CHECK(CompileRun(
4326 "indexed_mirror instanceof debug.ObjectMirror")->BooleanValue());
4327 CHECK(CompileRun(
4328 "both_mirror instanceof debug.ObjectMirror")->BooleanValue());
4329
4330 // Get the property names from the interceptors
4331 CompileRun(
ager@chromium.org32912102009-01-16 10:38:43 +00004332 "named_names = named_mirror.propertyNames();"
4333 "indexed_names = indexed_mirror.propertyNames();"
4334 "both_names = both_mirror.propertyNames()");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004335 CHECK_EQ(3, CompileRun("named_names.length")->Int32Value());
4336 CHECK_EQ(2, CompileRun("indexed_names.length")->Int32Value());
4337 CHECK_EQ(5, CompileRun("both_names.length")->Int32Value());
4338
4339 // Check the expected number of properties.
4340 const char* source;
ager@chromium.org32912102009-01-16 10:38:43 +00004341 source = "named_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004342 CHECK_EQ(3, CompileRun(source)->Int32Value());
4343
ager@chromium.org32912102009-01-16 10:38:43 +00004344 source = "indexed_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004345 CHECK_EQ(2, CompileRun(source)->Int32Value());
4346
ager@chromium.org32912102009-01-16 10:38:43 +00004347 source = "both_mirror.properties().length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004348 CHECK_EQ(5, CompileRun(source)->Int32Value());
4349
ager@chromium.org32912102009-01-16 10:38:43 +00004350 // 1 is PropertyKind.Named;
4351 source = "both_mirror.properties(1).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004352 CHECK_EQ(3, CompileRun(source)->Int32Value());
4353
ager@chromium.org32912102009-01-16 10:38:43 +00004354 // 2 is PropertyKind.Indexed;
4355 source = "both_mirror.properties(2).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004356 CHECK_EQ(2, CompileRun(source)->Int32Value());
4357
ager@chromium.org32912102009-01-16 10:38:43 +00004358 // 3 is PropertyKind.Named | PropertyKind.Indexed;
4359 source = "both_mirror.properties(3).length";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004360 CHECK_EQ(5, CompileRun(source)->Int32Value());
4361
ager@chromium.org32912102009-01-16 10:38:43 +00004362 // Get the interceptor properties for the object with only named interceptor.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004363 CompileRun("var named_values = named_mirror.properties()");
ager@chromium.org32912102009-01-16 10:38:43 +00004364
4365 // Check that the properties are interceptor properties.
4366 for (int i = 0; i < 3; i++) {
4367 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
4368 OS::SNPrintF(buffer,
4369 "named_values[%d] instanceof debug.PropertyMirror", i);
4370 CHECK(CompileRun(buffer.start())->BooleanValue());
4371
ager@chromium.org32912102009-01-16 10:38:43 +00004372 OS::SNPrintF(buffer, "named_values[%d].propertyType()", i);
yangguo@chromium.org99aa4902012-07-06 16:21:55 +00004373 CHECK_EQ(v8::internal::INTERCEPTOR,
4374 CompileRun(buffer.start())->Int32Value());
ager@chromium.org32912102009-01-16 10:38:43 +00004375
4376 OS::SNPrintF(buffer, "named_values[%d].isNative()", i);
4377 CHECK(CompileRun(buffer.start())->BooleanValue());
4378 }
4379
4380 // Get the interceptor properties for the object with only indexed
4381 // interceptor.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004382 CompileRun("var indexed_values = indexed_mirror.properties()");
ager@chromium.org32912102009-01-16 10:38:43 +00004383
4384 // Check that the properties are interceptor properties.
4385 for (int i = 0; i < 2; i++) {
4386 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
4387 OS::SNPrintF(buffer,
4388 "indexed_values[%d] instanceof debug.PropertyMirror", i);
4389 CHECK(CompileRun(buffer.start())->BooleanValue());
4390 }
4391
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004392 // Get the interceptor properties for the object with both types of
4393 // interceptors.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004394 CompileRun("var both_values = both_mirror.properties()");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004395
ager@chromium.org32912102009-01-16 10:38:43 +00004396 // Check that the properties are interceptor properties.
4397 for (int i = 0; i < 5; i++) {
4398 EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
4399 OS::SNPrintF(buffer, "both_values[%d] instanceof debug.PropertyMirror", i);
4400 CHECK(CompileRun(buffer.start())->BooleanValue());
4401 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004402
4403 // Check the property names.
4404 source = "both_values[0].name() == 'a'";
4405 CHECK(CompileRun(source)->BooleanValue());
4406
4407 source = "both_values[1].name() == 'b'";
4408 CHECK(CompileRun(source)->BooleanValue());
4409
4410 source = "both_values[2].name() == 'c'";
4411 CHECK(CompileRun(source)->BooleanValue());
4412
4413 source = "both_values[3].name() == 1";
4414 CHECK(CompileRun(source)->BooleanValue());
4415
4416 source = "both_values[4].name() == 10";
4417 CHECK(CompileRun(source)->BooleanValue());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004418}
4419
4420
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004421TEST(HiddenPrototypePropertyMirror) {
4422 // Create a V8 environment with debug access.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004423 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004424 v8::HandleScope scope(env->GetIsolate());
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004425 env.ExposeDebug();
4426
4427 v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New();
4428 t0->InstanceTemplate()->Set(v8::String::New("x"), v8::Number::New(0));
4429 v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New();
4430 t1->SetHiddenPrototype(true);
4431 t1->InstanceTemplate()->Set(v8::String::New("y"), v8::Number::New(1));
4432 v8::Handle<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New();
4433 t2->SetHiddenPrototype(true);
4434 t2->InstanceTemplate()->Set(v8::String::New("z"), v8::Number::New(2));
4435 v8::Handle<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New();
4436 t3->InstanceTemplate()->Set(v8::String::New("u"), v8::Number::New(3));
4437
4438 // Create object and set them on the global object.
4439 v8::Handle<v8::Object> o0 = t0->GetFunction()->NewInstance();
4440 env->Global()->Set(v8::String::New("o0"), o0);
4441 v8::Handle<v8::Object> o1 = t1->GetFunction()->NewInstance();
4442 env->Global()->Set(v8::String::New("o1"), o1);
4443 v8::Handle<v8::Object> o2 = t2->GetFunction()->NewInstance();
4444 env->Global()->Set(v8::String::New("o2"), o2);
4445 v8::Handle<v8::Object> o3 = t3->GetFunction()->NewInstance();
4446 env->Global()->Set(v8::String::New("o3"), o3);
4447
4448 // Get mirrors for the four objects.
4449 CompileRun(
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004450 "var o0_mirror = debug.MakeMirror(o0);"
4451 "var o1_mirror = debug.MakeMirror(o1);"
4452 "var o2_mirror = debug.MakeMirror(o2);"
4453 "var o3_mirror = debug.MakeMirror(o3)");
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004454 CHECK(CompileRun("o0_mirror instanceof debug.ObjectMirror")->BooleanValue());
4455 CHECK(CompileRun("o1_mirror instanceof debug.ObjectMirror")->BooleanValue());
4456 CHECK(CompileRun("o2_mirror instanceof debug.ObjectMirror")->BooleanValue());
4457 CHECK(CompileRun("o3_mirror instanceof debug.ObjectMirror")->BooleanValue());
4458
4459 // Check that each object has one property.
4460 CHECK_EQ(1, CompileRun(
4461 "o0_mirror.propertyNames().length")->Int32Value());
4462 CHECK_EQ(1, CompileRun(
4463 "o1_mirror.propertyNames().length")->Int32Value());
4464 CHECK_EQ(1, CompileRun(
4465 "o2_mirror.propertyNames().length")->Int32Value());
4466 CHECK_EQ(1, CompileRun(
4467 "o3_mirror.propertyNames().length")->Int32Value());
4468
4469 // Set o1 as prototype for o0. o1 has the hidden prototype flag so all
4470 // properties on o1 should be seen on o0.
4471 o0->Set(v8::String::New("__proto__"), o1);
4472 CHECK_EQ(2, CompileRun(
4473 "o0_mirror.propertyNames().length")->Int32Value());
4474 CHECK_EQ(0, CompileRun(
4475 "o0_mirror.property('x').value().value()")->Int32Value());
4476 CHECK_EQ(1, CompileRun(
4477 "o0_mirror.property('y').value().value()")->Int32Value());
4478
4479 // Set o2 as prototype for o0 (it will end up after o1 as o1 has the hidden
4480 // prototype flag. o2 also has the hidden prototype flag so all properties
4481 // on o2 should be seen on o0 as well as properties on o1.
4482 o0->Set(v8::String::New("__proto__"), o2);
4483 CHECK_EQ(3, CompileRun(
4484 "o0_mirror.propertyNames().length")->Int32Value());
4485 CHECK_EQ(0, CompileRun(
4486 "o0_mirror.property('x').value().value()")->Int32Value());
4487 CHECK_EQ(1, CompileRun(
4488 "o0_mirror.property('y').value().value()")->Int32Value());
4489 CHECK_EQ(2, CompileRun(
4490 "o0_mirror.property('z').value().value()")->Int32Value());
4491
4492 // Set o3 as prototype for o0 (it will end up after o1 and o2 as both o1 and
4493 // o2 has the hidden prototype flag. o3 does not have the hidden prototype
4494 // flag so properties on o3 should not be seen on o0 whereas the properties
4495 // from o1 and o2 should still be seen on o0.
4496 // Final prototype chain: o0 -> o1 -> o2 -> o3
4497 // Hidden prototypes: ^^ ^^
4498 o0->Set(v8::String::New("__proto__"), o3);
4499 CHECK_EQ(3, CompileRun(
4500 "o0_mirror.propertyNames().length")->Int32Value());
4501 CHECK_EQ(1, CompileRun(
4502 "o3_mirror.propertyNames().length")->Int32Value());
4503 CHECK_EQ(0, CompileRun(
4504 "o0_mirror.property('x').value().value()")->Int32Value());
4505 CHECK_EQ(1, CompileRun(
4506 "o0_mirror.property('y').value().value()")->Int32Value());
4507 CHECK_EQ(2, CompileRun(
4508 "o0_mirror.property('z').value().value()")->Int32Value());
4509 CHECK(CompileRun("o0_mirror.property('u').isUndefined()")->BooleanValue());
4510
4511 // The prototype (__proto__) for o0 should be o3 as o1 and o2 are hidden.
4512 CHECK(CompileRun("o0_mirror.protoObject() == o3_mirror")->BooleanValue());
4513}
4514
4515
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004516static v8::Handle<v8::Value> ProtperyXNativeGetter(
4517 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
4518 return v8::Integer::New(10);
4519}
4520
4521
4522TEST(NativeGetterPropertyMirror) {
4523 // Create a V8 environment with debug access.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004524 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004525 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004526 env.ExposeDebug();
4527
4528 v8::Handle<v8::String> name = v8::String::New("x");
4529 // Create object with named accessor.
4530 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New();
4531 named->SetAccessor(name, &ProtperyXNativeGetter, NULL,
4532 v8::Handle<v8::Value>(), v8::DEFAULT, v8::None);
4533
4534 // Create object with named property getter.
4535 env->Global()->Set(v8::String::New("instance"), named->NewInstance());
4536 CHECK_EQ(10, CompileRun("instance.x")->Int32Value());
4537
4538 // Get mirror for the object with property getter.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004539 CompileRun("var instance_mirror = debug.MakeMirror(instance);");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004540 CHECK(CompileRun(
4541 "instance_mirror instanceof debug.ObjectMirror")->BooleanValue());
4542
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004543 CompileRun("var named_names = instance_mirror.propertyNames();");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004544 CHECK_EQ(1, CompileRun("named_names.length")->Int32Value());
4545 CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue());
4546 CHECK(CompileRun(
4547 "instance_mirror.property('x').value().isNumber()")->BooleanValue());
4548 CHECK(CompileRun(
4549 "instance_mirror.property('x').value().value() == 10")->BooleanValue());
4550}
4551
4552
4553static v8::Handle<v8::Value> ProtperyXNativeGetterThrowingError(
4554 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
4555 return CompileRun("throw new Error('Error message');");
4556}
4557
4558
4559TEST(NativeGetterThrowingErrorPropertyMirror) {
4560 // Create a V8 environment with debug access.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004561 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004562 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004563 env.ExposeDebug();
4564
4565 v8::Handle<v8::String> name = v8::String::New("x");
4566 // Create object with named accessor.
4567 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New();
4568 named->SetAccessor(name, &ProtperyXNativeGetterThrowingError, NULL,
4569 v8::Handle<v8::Value>(), v8::DEFAULT, v8::None);
4570
4571 // Create object with named property getter.
4572 env->Global()->Set(v8::String::New("instance"), named->NewInstance());
4573
4574 // Get mirror for the object with property getter.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00004575 CompileRun("var instance_mirror = debug.MakeMirror(instance);");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004576 CHECK(CompileRun(
4577 "instance_mirror instanceof debug.ObjectMirror")->BooleanValue());
4578 CompileRun("named_names = instance_mirror.propertyNames();");
4579 CHECK_EQ(1, CompileRun("named_names.length")->Int32Value());
4580 CHECK(CompileRun("named_names[0] == 'x'")->BooleanValue());
4581 CHECK(CompileRun(
4582 "instance_mirror.property('x').value().isError()")->BooleanValue());
4583
4584 // Check that the message is that passed to the Error constructor.
4585 CHECK(CompileRun(
4586 "instance_mirror.property('x').value().message() == 'Error message'")->
4587 BooleanValue());
4588}
4589
4590
ager@chromium.orgc730f772009-11-11 10:11:16 +00004591// Test that hidden properties object is not returned as an unnamed property
4592// among regular properties.
4593// See http://crbug.com/26491
4594TEST(NoHiddenProperties) {
4595 // Create a V8 environment with debug access.
ager@chromium.orgc730f772009-11-11 10:11:16 +00004596 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004597 v8::HandleScope scope(env->GetIsolate());
ager@chromium.orgc730f772009-11-11 10:11:16 +00004598 env.ExposeDebug();
4599
4600 // Create an object in the global scope.
4601 const char* source = "var obj = {a: 1};";
4602 v8::Script::Compile(v8::String::New(source))->Run();
4603 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(
4604 env->Global()->Get(v8::String::New("obj")));
4605 // Set a hidden property on the object.
4606 obj->SetHiddenValue(v8::String::New("v8::test-debug::a"),
4607 v8::Int32::New(11));
4608
4609 // Get mirror for the object with property getter.
4610 CompileRun("var obj_mirror = debug.MakeMirror(obj);");
4611 CHECK(CompileRun(
4612 "obj_mirror instanceof debug.ObjectMirror")->BooleanValue());
4613 CompileRun("var named_names = obj_mirror.propertyNames();");
4614 // There should be exactly one property. But there is also an unnamed
4615 // property whose value is hidden properties dictionary. The latter
4616 // property should not be in the list of reguar properties.
4617 CHECK_EQ(1, CompileRun("named_names.length")->Int32Value());
4618 CHECK(CompileRun("named_names[0] == 'a'")->BooleanValue());
4619 CHECK(CompileRun(
4620 "obj_mirror.property('a').value().value() == 1")->BooleanValue());
4621
4622 // Object created by t0 will become hidden prototype of object 'obj'.
4623 v8::Handle<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New();
4624 t0->InstanceTemplate()->Set(v8::String::New("b"), v8::Number::New(2));
4625 t0->SetHiddenPrototype(true);
4626 v8::Handle<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New();
4627 t1->InstanceTemplate()->Set(v8::String::New("c"), v8::Number::New(3));
4628
4629 // Create proto objects, add hidden properties to them and set them on
4630 // the global object.
4631 v8::Handle<v8::Object> protoObj = t0->GetFunction()->NewInstance();
4632 protoObj->SetHiddenValue(v8::String::New("v8::test-debug::b"),
4633 v8::Int32::New(12));
4634 env->Global()->Set(v8::String::New("protoObj"), protoObj);
4635 v8::Handle<v8::Object> grandProtoObj = t1->GetFunction()->NewInstance();
4636 grandProtoObj->SetHiddenValue(v8::String::New("v8::test-debug::c"),
4637 v8::Int32::New(13));
4638 env->Global()->Set(v8::String::New("grandProtoObj"), grandProtoObj);
4639
4640 // Setting prototypes: obj->protoObj->grandProtoObj
4641 protoObj->Set(v8::String::New("__proto__"), grandProtoObj);
4642 obj->Set(v8::String::New("__proto__"), protoObj);
4643
4644 // Get mirror for the object with property getter.
4645 CompileRun("var obj_mirror = debug.MakeMirror(obj);");
4646 CHECK(CompileRun(
4647 "obj_mirror instanceof debug.ObjectMirror")->BooleanValue());
4648 CompileRun("var named_names = obj_mirror.propertyNames();");
4649 // There should be exactly two properties - one from the object itself and
4650 // another from its hidden prototype.
4651 CHECK_EQ(2, CompileRun("named_names.length")->Int32Value());
4652 CHECK(CompileRun("named_names.sort(); named_names[0] == 'a' &&"
4653 "named_names[1] == 'b'")->BooleanValue());
4654 CHECK(CompileRun(
4655 "obj_mirror.property('a').value().value() == 1")->BooleanValue());
4656 CHECK(CompileRun(
4657 "obj_mirror.property('b').value().value() == 2")->BooleanValue());
4658}
4659
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004660
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004661// Multithreaded tests of JSON debugger protocol
4662
4663// Support classes
4664
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004665// Provides synchronization between k threads, where k is an input to the
4666// constructor. The Wait() call blocks a thread until it is called for the
4667// k'th time, then all calls return. Each ThreadBarrier object can only
4668// be used once.
4669class ThreadBarrier {
4670 public:
4671 explicit ThreadBarrier(int num_threads);
4672 ~ThreadBarrier();
4673 void Wait();
4674 private:
4675 int num_threads_;
4676 int num_blocked_;
4677 v8::internal::Mutex* lock_;
4678 v8::internal::Semaphore* sem_;
4679 bool invalid_;
4680};
4681
4682ThreadBarrier::ThreadBarrier(int num_threads)
4683 : num_threads_(num_threads), num_blocked_(0) {
4684 lock_ = OS::CreateMutex();
4685 sem_ = OS::CreateSemaphore(0);
4686 invalid_ = false; // A barrier may only be used once. Then it is invalid.
4687}
4688
4689// Do not call, due to race condition with Wait().
4690// Could be resolved with Pthread condition variables.
4691ThreadBarrier::~ThreadBarrier() {
4692 lock_->Lock();
4693 delete lock_;
4694 delete sem_;
4695}
4696
4697void ThreadBarrier::Wait() {
4698 lock_->Lock();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004699 CHECK(!invalid_);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004700 if (num_blocked_ == num_threads_ - 1) {
4701 // Signal and unblock all waiting threads.
4702 for (int i = 0; i < num_threads_ - 1; ++i) {
4703 sem_->Signal();
4704 }
4705 invalid_ = true;
4706 printf("BARRIER\n\n");
4707 fflush(stdout);
4708 lock_->Unlock();
4709 } else { // Wait for the semaphore.
4710 ++num_blocked_;
4711 lock_->Unlock(); // Potential race condition with destructor because
4712 sem_->Wait(); // these two lines are not atomic.
4713 }
4714}
4715
4716// A set containing enough barriers and semaphores for any of the tests.
4717class Barriers {
4718 public:
4719 Barriers();
4720 void Initialize();
4721 ThreadBarrier barrier_1;
4722 ThreadBarrier barrier_2;
4723 ThreadBarrier barrier_3;
4724 ThreadBarrier barrier_4;
4725 ThreadBarrier barrier_5;
4726 v8::internal::Semaphore* semaphore_1;
4727 v8::internal::Semaphore* semaphore_2;
4728};
4729
4730Barriers::Barriers() : barrier_1(2), barrier_2(2),
4731 barrier_3(2), barrier_4(2), barrier_5(2) {}
4732
4733void Barriers::Initialize() {
4734 semaphore_1 = OS::CreateSemaphore(0);
4735 semaphore_2 = OS::CreateSemaphore(0);
4736}
4737
4738
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004739// We match parts of the message to decide if it is a break message.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004740bool IsBreakEventMessage(char *message) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004741 const char* type_event = "\"type\":\"event\"";
4742 const char* event_break = "\"event\":\"break\"";
4743 // Does the message contain both type:event and event:break?
4744 return strstr(message, type_event) != NULL &&
4745 strstr(message, event_break) != NULL;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004746}
4747
4748
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004749// We match parts of the message to decide if it is a exception message.
4750bool IsExceptionEventMessage(char *message) {
4751 const char* type_event = "\"type\":\"event\"";
4752 const char* event_exception = "\"event\":\"exception\"";
4753 // Does the message contain both type:event and event:exception?
4754 return strstr(message, type_event) != NULL &&
4755 strstr(message, event_exception) != NULL;
4756}
4757
4758
4759// We match the message wether it is an evaluate response message.
4760bool IsEvaluateResponseMessage(char* message) {
4761 const char* type_response = "\"type\":\"response\"";
4762 const char* command_evaluate = "\"command\":\"evaluate\"";
4763 // Does the message contain both type:response and command:evaluate?
4764 return strstr(message, type_response) != NULL &&
4765 strstr(message, command_evaluate) != NULL;
4766}
4767
4768
ager@chromium.org5c838252010-02-19 08:53:10 +00004769static int StringToInt(const char* s) {
4770 return atoi(s); // NOLINT
4771}
4772
4773
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004774// We match parts of the message to get evaluate result int value.
4775int GetEvaluateIntResult(char *message) {
4776 const char* value = "\"value\":";
4777 char* pos = strstr(message, value);
4778 if (pos == NULL) {
4779 return -1;
4780 }
4781 int res = -1;
ager@chromium.org5c838252010-02-19 08:53:10 +00004782 res = StringToInt(pos + strlen(value));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004783 return res;
4784}
4785
4786
4787// We match parts of the message to get hit breakpoint id.
4788int GetBreakpointIdFromBreakEventMessage(char *message) {
4789 const char* breakpoints = "\"breakpoints\":[";
4790 char* pos = strstr(message, breakpoints);
4791 if (pos == NULL) {
4792 return -1;
4793 }
4794 int res = -1;
ager@chromium.org5c838252010-02-19 08:53:10 +00004795 res = StringToInt(pos + strlen(breakpoints));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004796 return res;
4797}
4798
4799
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004800// We match parts of the message to get total frames number.
4801int GetTotalFramesInt(char *message) {
4802 const char* prefix = "\"totalFrames\":";
4803 char* pos = strstr(message, prefix);
4804 if (pos == NULL) {
4805 return -1;
4806 }
4807 pos += strlen(prefix);
ager@chromium.org5c838252010-02-19 08:53:10 +00004808 int res = StringToInt(pos);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004809 return res;
4810}
4811
4812
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004813// We match parts of the message to get source line.
4814int GetSourceLineFromBreakEventMessage(char *message) {
4815 const char* source_line = "\"sourceLine\":";
4816 char* pos = strstr(message, source_line);
4817 if (pos == NULL) {
4818 return -1;
4819 }
4820 int res = -1;
4821 res = StringToInt(pos + strlen(source_line));
4822 return res;
4823}
4824
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004825/* Test MessageQueues */
4826/* Tests the message queues that hold debugger commands and
4827 * response messages to the debugger. Fills queues and makes
4828 * them grow.
4829 */
4830Barriers message_queue_barriers;
4831
4832// This is the debugger thread, that executes no v8 calls except
4833// placing JSON debugger commands in the queue.
4834class MessageQueueDebuggerThread : public v8::internal::Thread {
4835 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004836 MessageQueueDebuggerThread()
4837 : Thread("MessageQueueDebuggerThread") { }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004838 void Run();
4839};
4840
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004841static void MessageHandler(const uint16_t* message, int length,
4842 v8::Debug::ClientData* client_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004843 static char print_buffer[1000];
4844 Utf16ToAscii(message, length, print_buffer);
4845 if (IsBreakEventMessage(print_buffer)) {
4846 // Lets test script wait until break occurs to send commands.
4847 // Signals when a break is reported.
4848 message_queue_barriers.semaphore_2->Signal();
4849 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00004850
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004851 // Allow message handler to block on a semaphore, to test queueing of
4852 // messages while blocked.
4853 message_queue_barriers.semaphore_1->Wait();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004854}
4855
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004856void MessageQueueDebuggerThread::Run() {
4857 const int kBufferSize = 1000;
4858 uint16_t buffer_1[kBufferSize];
4859 uint16_t buffer_2[kBufferSize];
4860 const char* command_1 =
4861 "{\"seq\":117,"
4862 "\"type\":\"request\","
4863 "\"command\":\"evaluate\","
4864 "\"arguments\":{\"expression\":\"1+2\"}}";
4865 const char* command_2 =
4866 "{\"seq\":118,"
4867 "\"type\":\"request\","
4868 "\"command\":\"evaluate\","
4869 "\"arguments\":{\"expression\":\"1+a\"}}";
4870 const char* command_3 =
4871 "{\"seq\":119,"
4872 "\"type\":\"request\","
4873 "\"command\":\"evaluate\","
4874 "\"arguments\":{\"expression\":\"c.d * b\"}}";
4875 const char* command_continue =
4876 "{\"seq\":106,"
4877 "\"type\":\"request\","
4878 "\"command\":\"continue\"}";
4879 const char* command_single_step =
4880 "{\"seq\":107,"
4881 "\"type\":\"request\","
4882 "\"command\":\"continue\","
4883 "\"arguments\":{\"stepaction\":\"next\"}}";
4884
4885 /* Interleaved sequence of actions by the two threads:*/
4886 // Main thread compiles and runs source_1
ager@chromium.org5ec48922009-05-05 07:25:34 +00004887 message_queue_barriers.semaphore_1->Signal();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004888 message_queue_barriers.barrier_1.Wait();
4889 // Post 6 commands, filling the command queue and making it expand.
4890 // These calls return immediately, but the commands stay on the queue
4891 // until the execution of source_2.
4892 // Note: AsciiToUtf16 executes before SendCommand, so command is copied
4893 // to buffer before buffer is sent to SendCommand.
4894 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1));
4895 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2));
4896 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
4897 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
4898 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004899 message_queue_barriers.barrier_2.Wait();
4900 // Main thread compiles and runs source_2.
ager@chromium.org5ec48922009-05-05 07:25:34 +00004901 // Queued commands are executed at the start of compilation of source_2(
4902 // beforeCompile event).
4903 // Free the message handler to process all the messages from the queue. 7
4904 // messages are expected: 2 afterCompile events and 5 responses.
4905 // All the commands added so far will fail to execute as long as call stack
4906 // is empty on beforeCompile event.
4907 for (int i = 0; i < 6 ; ++i) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004908 message_queue_barriers.semaphore_1->Signal();
4909 }
ager@chromium.org5ec48922009-05-05 07:25:34 +00004910 message_queue_barriers.barrier_3.Wait();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004911 // Main thread compiles and runs source_3.
ager@chromium.org5ec48922009-05-05 07:25:34 +00004912 // Don't stop in the afterCompile handler.
4913 message_queue_barriers.semaphore_1->Signal();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004914 // source_3 includes a debugger statement, which causes a break event.
4915 // Wait on break event from hitting "debugger" statement
4916 message_queue_barriers.semaphore_2->Wait();
4917 // These should execute after the "debugger" statement in source_2
ager@chromium.org5ec48922009-05-05 07:25:34 +00004918 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_1, buffer_1));
4919 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_2, buffer_2));
4920 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_3, buffer_2));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004921 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_single_step, buffer_2));
ager@chromium.org5ec48922009-05-05 07:25:34 +00004922 // Run after 2 break events, 4 responses.
4923 for (int i = 0; i < 6 ; ++i) {
4924 message_queue_barriers.semaphore_1->Signal();
4925 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004926 // Wait on break event after a single step executes.
4927 message_queue_barriers.semaphore_2->Wait();
4928 v8::Debug::SendCommand(buffer_1, AsciiToUtf16(command_2, buffer_1));
4929 v8::Debug::SendCommand(buffer_2, AsciiToUtf16(command_continue, buffer_2));
ager@chromium.org5ec48922009-05-05 07:25:34 +00004930 // Run after 2 responses.
4931 for (int i = 0; i < 2 ; ++i) {
4932 message_queue_barriers.semaphore_1->Signal();
4933 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004934 // Main thread continues running source_3 to end, waits for this thread.
4935}
4936
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004937
4938// This thread runs the v8 engine.
4939TEST(MessageQueues) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004940 MessageQueueDebuggerThread message_queue_debugger_thread;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004941
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004942 // Create a V8 environment
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004943 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00004944 v8::HandleScope scope(env->GetIsolate());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004945 message_queue_barriers.Initialize();
4946 v8::Debug::SetMessageHandler(MessageHandler);
4947 message_queue_debugger_thread.Start();
4948
4949 const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;";
4950 const char* source_2 = "e = 17;";
4951 const char* source_3 = "a = 4; debugger; a = 5; a = 6; a = 7;";
4952
4953 // See MessageQueueDebuggerThread::Run for interleaved sequence of
4954 // API calls and events in the two threads.
4955 CompileRun(source_1);
4956 message_queue_barriers.barrier_1.Wait();
4957 message_queue_barriers.barrier_2.Wait();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004958 CompileRun(source_2);
4959 message_queue_barriers.barrier_3.Wait();
4960 CompileRun(source_3);
4961 message_queue_debugger_thread.Join();
4962 fflush(stdout);
4963}
4964
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004965
4966class TestClientData : public v8::Debug::ClientData {
4967 public:
4968 TestClientData() {
4969 constructor_call_counter++;
4970 }
4971 virtual ~TestClientData() {
4972 destructor_call_counter++;
4973 }
4974
4975 static void ResetCounters() {
4976 constructor_call_counter = 0;
4977 destructor_call_counter = 0;
4978 }
4979
4980 static int constructor_call_counter;
4981 static int destructor_call_counter;
4982};
4983
4984int TestClientData::constructor_call_counter = 0;
4985int TestClientData::destructor_call_counter = 0;
4986
4987
4988// Tests that MessageQueue doesn't destroy client data when expands and
4989// does destroy when it dies.
4990TEST(MessageQueueExpandAndDestroy) {
4991 TestClientData::ResetCounters();
4992 { // Create a scope for the queue.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004993 CommandMessageQueue queue(1);
4994 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004995 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004996 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004997 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004998 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004999 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005000 CHECK_EQ(0, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005001 queue.Get().Dispose();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005002 CHECK_EQ(1, TestClientData::destructor_call_counter);
5003 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005004 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005005 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005006 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005007 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005008 new TestClientData()));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005009 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
5010 new TestClientData()));
5011 queue.Put(CommandMessage::New(Vector<uint16_t>::empty(),
5012 new TestClientData()));
5013 CHECK_EQ(1, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005014 queue.Get().Dispose();
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005015 CHECK_EQ(2, TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005016 }
5017 // All the client data should be destroyed when the queue is destroyed.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005018 CHECK_EQ(TestClientData::destructor_call_counter,
5019 TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005020}
5021
5022
5023static int handled_client_data_instances_count = 0;
5024static void MessageHandlerCountingClientData(
ager@chromium.org5ec48922009-05-05 07:25:34 +00005025 const v8::Debug::Message& message) {
5026 if (message.GetClientData() != NULL) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005027 handled_client_data_instances_count++;
5028 }
5029}
5030
5031
5032// Tests that all client data passed to the debugger are sent to the handler.
5033TEST(SendClientDataToHandler) {
5034 // Create a V8 environment
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005035 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00005036 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005037 TestClientData::ResetCounters();
5038 handled_client_data_instances_count = 0;
ager@chromium.org5ec48922009-05-05 07:25:34 +00005039 v8::Debug::SetMessageHandler2(MessageHandlerCountingClientData);
5040 const char* source_1 = "a = 3; b = 4; c = new Object(); c.d = 5;";
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005041 const int kBufferSize = 1000;
5042 uint16_t buffer[kBufferSize];
5043 const char* command_1 =
5044 "{\"seq\":117,"
5045 "\"type\":\"request\","
5046 "\"command\":\"evaluate\","
5047 "\"arguments\":{\"expression\":\"1+2\"}}";
5048 const char* command_2 =
5049 "{\"seq\":118,"
5050 "\"type\":\"request\","
5051 "\"command\":\"evaluate\","
5052 "\"arguments\":{\"expression\":\"1+a\"}}";
5053 const char* command_continue =
5054 "{\"seq\":106,"
5055 "\"type\":\"request\","
5056 "\"command\":\"continue\"}";
5057
5058 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer),
5059 new TestClientData());
5060 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer), NULL);
5061 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer),
5062 new TestClientData());
5063 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer),
5064 new TestClientData());
ager@chromium.org5ec48922009-05-05 07:25:34 +00005065 // All the messages will be processed on beforeCompile event.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005066 CompileRun(source_1);
ager@chromium.org5ec48922009-05-05 07:25:34 +00005067 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005068 CHECK_EQ(3, TestClientData::constructor_call_counter);
5069 CHECK_EQ(TestClientData::constructor_call_counter,
5070 handled_client_data_instances_count);
5071 CHECK_EQ(TestClientData::constructor_call_counter,
5072 TestClientData::destructor_call_counter);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005073}
5074
5075
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005076/* Test ThreadedDebugging */
5077/* This test interrupts a running infinite loop that is
5078 * occupying the v8 thread by a break command from the
5079 * debugger thread. It then changes the value of a
5080 * global object, to make the loop terminate.
5081 */
5082
5083Barriers threaded_debugging_barriers;
5084
5085class V8Thread : public v8::internal::Thread {
5086 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005087 V8Thread() : Thread("V8Thread") { }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005088 void Run();
5089};
5090
5091class DebuggerThread : public v8::internal::Thread {
5092 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005093 DebuggerThread() : Thread("DebuggerThread") { }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005094 void Run();
5095};
5096
5097
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00005098static v8::Handle<v8::Value> ThreadedAtBarrier1(const v8::Arguments& args) {
5099 threaded_debugging_barriers.barrier_1.Wait();
5100 return v8::Undefined();
5101}
5102
5103
ager@chromium.org5ec48922009-05-05 07:25:34 +00005104static void ThreadedMessageHandler(const v8::Debug::Message& message) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005105 static char print_buffer[1000];
ager@chromium.org5ec48922009-05-05 07:25:34 +00005106 v8::String::Value json(message.GetJSON());
5107 Utf16ToAscii(*json, json.length(), print_buffer);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005108 if (IsBreakEventMessage(print_buffer)) {
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00005109 // Check that we are inside the while loop.
5110 int source_line = GetSourceLineFromBreakEventMessage(print_buffer);
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00005111 // TODO(2047): This should really be 8 <= source_line <= 13; but we
5112 // currently have an off-by-one error when calculating the source
5113 // position corresponding to the program counter at the debug break.
5114 CHECK(7 <= source_line && source_line <= 13);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005115 threaded_debugging_barriers.barrier_2.Wait();
5116 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005117}
5118
5119
5120void V8Thread::Run() {
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00005121 const char* source =
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005122 "flag = true;\n"
5123 "function bar( new_value ) {\n"
5124 " flag = new_value;\n"
5125 " return \"Return from bar(\" + new_value + \")\";\n"
5126 "}\n"
5127 "\n"
5128 "function foo() {\n"
5129 " var x = 1;\n"
5130 " while ( flag == true ) {\n"
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00005131 " if ( x == 1 ) {\n"
5132 " ThreadedAtBarrier1();\n"
5133 " }\n"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005134 " x = x + 1;\n"
5135 " }\n"
5136 "}\n"
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00005137 "\n"
5138 "foo();\n";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005139
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005140 v8::V8::Initialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005141 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00005142 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org5ec48922009-05-05 07:25:34 +00005143 v8::Debug::SetMessageHandler2(&ThreadedMessageHandler);
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00005144 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
5145 global_template->Set(v8::String::New("ThreadedAtBarrier1"),
5146 v8::FunctionTemplate::New(ThreadedAtBarrier1));
5147 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template);
5148 v8::Context::Scope context_scope(context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005149
kasperl@chromium.orgacae3782009-04-11 09:17:08 +00005150 CompileRun(source);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005151}
5152
5153void DebuggerThread::Run() {
5154 const int kBufSize = 1000;
5155 uint16_t buffer[kBufSize];
5156
5157 const char* command_1 = "{\"seq\":102,"
5158 "\"type\":\"request\","
5159 "\"command\":\"evaluate\","
5160 "\"arguments\":{\"expression\":\"bar(false)\"}}";
5161 const char* command_2 = "{\"seq\":103,"
5162 "\"type\":\"request\","
5163 "\"command\":\"continue\"}";
5164
5165 threaded_debugging_barriers.barrier_1.Wait();
5166 v8::Debug::DebugBreak();
5167 threaded_debugging_barriers.barrier_2.Wait();
5168 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
5169 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
5170}
5171
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005172
5173TEST(ThreadedDebugging) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005174 DebuggerThread debugger_thread;
5175 V8Thread v8_thread;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005176
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005177 // Create a V8 environment
5178 threaded_debugging_barriers.Initialize();
5179
5180 v8_thread.Start();
5181 debugger_thread.Start();
5182
5183 v8_thread.Join();
5184 debugger_thread.Join();
5185}
5186
5187/* Test RecursiveBreakpoints */
5188/* In this test, the debugger evaluates a function with a breakpoint, after
5189 * hitting a breakpoint in another function. We do this with both values
5190 * of the flag enabling recursive breakpoints, and verify that the second
5191 * breakpoint is hit when enabled, and missed when disabled.
5192 */
5193
5194class BreakpointsV8Thread : public v8::internal::Thread {
5195 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005196 BreakpointsV8Thread() : Thread("BreakpointsV8Thread") { }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005197 void Run();
5198};
5199
5200class BreakpointsDebuggerThread : public v8::internal::Thread {
5201 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005202 explicit BreakpointsDebuggerThread(bool global_evaluate)
5203 : Thread("BreakpointsDebuggerThread"),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005204 global_evaluate_(global_evaluate) {}
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005205 void Run();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005206
5207 private:
5208 bool global_evaluate_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005209};
5210
5211
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005212Barriers* breakpoints_barriers;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005213int break_event_breakpoint_id;
5214int evaluate_int_result;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005215
ager@chromium.org5ec48922009-05-05 07:25:34 +00005216static void BreakpointsMessageHandler(const v8::Debug::Message& message) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005217 static char print_buffer[1000];
ager@chromium.org5ec48922009-05-05 07:25:34 +00005218 v8::String::Value json(message.GetJSON());
5219 Utf16ToAscii(*json, json.length(), print_buffer);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005220
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005221 if (IsBreakEventMessage(print_buffer)) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005222 break_event_breakpoint_id =
5223 GetBreakpointIdFromBreakEventMessage(print_buffer);
5224 breakpoints_barriers->semaphore_1->Signal();
5225 } else if (IsEvaluateResponseMessage(print_buffer)) {
5226 evaluate_int_result = GetEvaluateIntResult(print_buffer);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005227 breakpoints_barriers->semaphore_1->Signal();
5228 }
5229}
5230
5231
5232void BreakpointsV8Thread::Run() {
5233 const char* source_1 = "var y_global = 3;\n"
5234 "function cat( new_value ) {\n"
5235 " var x = new_value;\n"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005236 " y_global = y_global + 4;\n"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005237 " x = 3 * x + 1;\n"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005238 " y_global = y_global + 5;\n"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005239 " return x;\n"
5240 "}\n"
5241 "\n"
5242 "function dog() {\n"
5243 " var x = 1;\n"
5244 " x = y_global;"
5245 " var z = 3;"
5246 " x += 100;\n"
5247 " return x;\n"
5248 "}\n"
5249 "\n";
5250 const char* source_2 = "cat(17);\n"
5251 "cat(19);\n";
5252
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005253 v8::V8::Initialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005254 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00005255 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org5ec48922009-05-05 07:25:34 +00005256 v8::Debug::SetMessageHandler2(&BreakpointsMessageHandler);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005257
5258 CompileRun(source_1);
5259 breakpoints_barriers->barrier_1.Wait();
5260 breakpoints_barriers->barrier_2.Wait();
5261 CompileRun(source_2);
5262}
5263
5264
5265void BreakpointsDebuggerThread::Run() {
5266 const int kBufSize = 1000;
5267 uint16_t buffer[kBufSize];
5268
5269 const char* command_1 = "{\"seq\":101,"
5270 "\"type\":\"request\","
5271 "\"command\":\"setbreakpoint\","
5272 "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}";
5273 const char* command_2 = "{\"seq\":102,"
5274 "\"type\":\"request\","
5275 "\"command\":\"setbreakpoint\","
5276 "\"arguments\":{\"type\":\"function\",\"target\":\"dog\",\"line\":3}}";
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005277 const char* command_3;
5278 if (this->global_evaluate_) {
5279 command_3 = "{\"seq\":103,"
5280 "\"type\":\"request\","
5281 "\"command\":\"evaluate\","
5282 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false,"
5283 "\"global\":true}}";
5284 } else {
5285 command_3 = "{\"seq\":103,"
5286 "\"type\":\"request\","
5287 "\"command\":\"evaluate\","
5288 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":false}}";
5289 }
5290 const char* command_4;
5291 if (this->global_evaluate_) {
5292 command_4 = "{\"seq\":104,"
5293 "\"type\":\"request\","
5294 "\"command\":\"evaluate\","
5295 "\"arguments\":{\"expression\":\"100 + 8\",\"disable_break\":true,"
5296 "\"global\":true}}";
5297 } else {
5298 command_4 = "{\"seq\":104,"
5299 "\"type\":\"request\","
5300 "\"command\":\"evaluate\","
5301 "\"arguments\":{\"expression\":\"x + 1\",\"disable_break\":true}}";
5302 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005303 const char* command_5 = "{\"seq\":105,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005304 "\"type\":\"request\","
5305 "\"command\":\"continue\"}";
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005306 const char* command_6 = "{\"seq\":106,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005307 "\"type\":\"request\","
5308 "\"command\":\"continue\"}";
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005309 const char* command_7;
5310 if (this->global_evaluate_) {
5311 command_7 = "{\"seq\":107,"
5312 "\"type\":\"request\","
5313 "\"command\":\"evaluate\","
5314 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true,"
5315 "\"global\":true}}";
5316 } else {
5317 command_7 = "{\"seq\":107,"
5318 "\"type\":\"request\","
5319 "\"command\":\"evaluate\","
5320 "\"arguments\":{\"expression\":\"dog()\",\"disable_break\":true}}";
5321 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005322 const char* command_8 = "{\"seq\":108,"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005323 "\"type\":\"request\","
5324 "\"command\":\"continue\"}";
5325
5326
5327 // v8 thread initializes, runs source_1
5328 breakpoints_barriers->barrier_1.Wait();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005329 // 1:Set breakpoint in cat() (will get id 1).
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005330 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005331 // 2:Set breakpoint in dog() (will get id 2).
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005332 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005333 breakpoints_barriers->barrier_2.Wait();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005334 // V8 thread starts compiling source_2.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005335 // Automatic break happens, to run queued commands
5336 // breakpoints_barriers->semaphore_1->Wait();
5337 // Commands 1 through 3 run, thread continues.
5338 // v8 thread runs source_2 to breakpoint in cat().
5339 // message callback receives break event.
5340 breakpoints_barriers->semaphore_1->Wait();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005341 // Must have hit breakpoint #1.
5342 CHECK_EQ(1, break_event_breakpoint_id);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005343 // 4:Evaluate dog() (which has a breakpoint).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005344 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_3, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005345 // V8 thread hits breakpoint in dog().
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005346 breakpoints_barriers->semaphore_1->Wait(); // wait for break event
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005347 // Must have hit breakpoint #2.
5348 CHECK_EQ(2, break_event_breakpoint_id);
5349 // 5:Evaluate (x + 1).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005350 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_4, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005351 // Evaluate (x + 1) finishes.
5352 breakpoints_barriers->semaphore_1->Wait();
5353 // Must have result 108.
5354 CHECK_EQ(108, evaluate_int_result);
5355 // 6:Continue evaluation of dog().
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005356 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_5, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005357 // Evaluate dog() finishes.
5358 breakpoints_barriers->semaphore_1->Wait();
5359 // Must have result 107.
5360 CHECK_EQ(107, evaluate_int_result);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005361 // 7:Continue evaluation of source_2, finish cat(17), hit breakpoint
5362 // in cat(19).
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005363 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_6, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005364 // Message callback gets break event.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005365 breakpoints_barriers->semaphore_1->Wait(); // wait for break event
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005366 // Must have hit breakpoint #1.
5367 CHECK_EQ(1, break_event_breakpoint_id);
5368 // 8: Evaluate dog() with breaks disabled.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005369 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_7, buffer));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005370 // Evaluate dog() finishes.
5371 breakpoints_barriers->semaphore_1->Wait();
5372 // Must have result 116.
5373 CHECK_EQ(116, evaluate_int_result);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005374 // 9: Continue evaluation of source2, reach end.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005375 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_8, buffer));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005376}
5377
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005378void TestRecursiveBreakpointsGeneric(bool global_evaluate) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005379 i::FLAG_debugger_auto_break = true;
5380
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005381 BreakpointsDebuggerThread breakpoints_debugger_thread(global_evaluate);
5382 BreakpointsV8Thread breakpoints_v8_thread;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005383
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005384 // Create a V8 environment
5385 Barriers stack_allocated_breakpoints_barriers;
5386 stack_allocated_breakpoints_barriers.Initialize();
5387 breakpoints_barriers = &stack_allocated_breakpoints_barriers;
5388
5389 breakpoints_v8_thread.Start();
5390 breakpoints_debugger_thread.Start();
5391
5392 breakpoints_v8_thread.Join();
5393 breakpoints_debugger_thread.Join();
5394}
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005395
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005396TEST(RecursiveBreakpoints) {
5397 TestRecursiveBreakpointsGeneric(false);
5398}
5399
5400TEST(RecursiveBreakpointsGlobal) {
5401 TestRecursiveBreakpointsGeneric(true);
5402}
5403
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005404
5405static void DummyDebugEventListener(v8::DebugEvent event,
5406 v8::Handle<v8::Object> exec_state,
5407 v8::Handle<v8::Object> event_data,
5408 v8::Handle<v8::Value> data) {
5409}
5410
5411
iposva@chromium.org245aa852009-02-10 00:49:54 +00005412TEST(SetDebugEventListenerOnUninitializedVM) {
5413 v8::Debug::SetDebugEventListener(DummyDebugEventListener);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005414}
5415
5416
ager@chromium.org5ec48922009-05-05 07:25:34 +00005417static void DummyMessageHandler(const v8::Debug::Message& message) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005418}
5419
5420
5421TEST(SetMessageHandlerOnUninitializedVM) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00005422 v8::Debug::SetMessageHandler2(DummyMessageHandler);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005423}
5424
5425
5426TEST(DebugBreakOnUninitializedVM) {
5427 v8::Debug::DebugBreak();
5428}
5429
5430
5431TEST(SendCommandToUninitializedVM) {
5432 const char* dummy_command = "{}";
5433 uint16_t dummy_buffer[80];
5434 int dummy_length = AsciiToUtf16(dummy_command, dummy_buffer);
5435 v8::Debug::SendCommand(dummy_buffer, dummy_length);
5436}
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005437
5438
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005439// Source for a JavaScript function which returns the data parameter of a
5440// function called in the context of the debugger. If no data parameter is
5441// passed it throws an exception.
5442static const char* debugger_call_with_data_source =
5443 "function debugger_call_with_data(exec_state, data) {"
5444 " if (data) return data;"
5445 " throw 'No data!'"
5446 "}";
5447v8::Handle<v8::Function> debugger_call_with_data;
5448
5449
5450// Source for a JavaScript function which returns the data parameter of a
5451// function called in the context of the debugger. If no data parameter is
5452// passed it throws an exception.
5453static const char* debugger_call_with_closure_source =
5454 "var x = 3;"
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005455 "(function (exec_state) {"
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005456 " if (exec_state.y) return x - 1;"
5457 " exec_state.y = x;"
5458 " return exec_state.y"
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005459 "})";
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005460v8::Handle<v8::Function> debugger_call_with_closure;
5461
5462// Function to retrieve the number of JavaScript frames by calling a JavaScript
5463// in the debugger.
5464static v8::Handle<v8::Value> CheckFrameCount(const v8::Arguments& args) {
5465 CHECK(v8::Debug::Call(frame_count)->IsNumber());
5466 CHECK_EQ(args[0]->Int32Value(),
5467 v8::Debug::Call(frame_count)->Int32Value());
5468 return v8::Undefined();
5469}
5470
5471
5472// Function to retrieve the source line of the top JavaScript frame by calling a
5473// JavaScript function in the debugger.
5474static v8::Handle<v8::Value> CheckSourceLine(const v8::Arguments& args) {
5475 CHECK(v8::Debug::Call(frame_source_line)->IsNumber());
5476 CHECK_EQ(args[0]->Int32Value(),
5477 v8::Debug::Call(frame_source_line)->Int32Value());
5478 return v8::Undefined();
5479}
5480
5481
5482// Function to test passing an additional parameter to a JavaScript function
5483// called in the debugger. It also tests that functions called in the debugger
5484// can throw exceptions.
5485static v8::Handle<v8::Value> CheckDataParameter(const v8::Arguments& args) {
5486 v8::Handle<v8::String> data = v8::String::New("Test");
5487 CHECK(v8::Debug::Call(debugger_call_with_data, data)->IsString());
5488
5489 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty());
5490 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty());
5491
5492 v8::TryCatch catcher;
5493 v8::Debug::Call(debugger_call_with_data);
5494 CHECK(catcher.HasCaught());
5495 CHECK(catcher.Exception()->IsString());
5496
5497 return v8::Undefined();
5498}
5499
5500
5501// Function to test using a JavaScript with closure in the debugger.
5502static v8::Handle<v8::Value> CheckClosure(const v8::Arguments& args) {
5503 CHECK(v8::Debug::Call(debugger_call_with_closure)->IsNumber());
5504 CHECK_EQ(3, v8::Debug::Call(debugger_call_with_closure)->Int32Value());
5505 return v8::Undefined();
5506}
5507
5508
5509// Test functions called through the debugger.
5510TEST(CallFunctionInDebugger) {
5511 // Create and enter a context with the functions CheckFrameCount,
5512 // CheckSourceLine and CheckDataParameter installed.
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00005513 v8::HandleScope scope(v8::Isolate::GetCurrent());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005514 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
5515 global_template->Set(v8::String::New("CheckFrameCount"),
5516 v8::FunctionTemplate::New(CheckFrameCount));
5517 global_template->Set(v8::String::New("CheckSourceLine"),
5518 v8::FunctionTemplate::New(CheckSourceLine));
5519 global_template->Set(v8::String::New("CheckDataParameter"),
5520 v8::FunctionTemplate::New(CheckDataParameter));
5521 global_template->Set(v8::String::New("CheckClosure"),
5522 v8::FunctionTemplate::New(CheckClosure));
5523 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template);
5524 v8::Context::Scope context_scope(context);
5525
5526 // Compile a function for checking the number of JavaScript frames.
5527 v8::Script::Compile(v8::String::New(frame_count_source))->Run();
5528 frame_count = v8::Local<v8::Function>::Cast(
5529 context->Global()->Get(v8::String::New("frame_count")));
5530
5531 // Compile a function for returning the source line for the top frame.
5532 v8::Script::Compile(v8::String::New(frame_source_line_source))->Run();
5533 frame_source_line = v8::Local<v8::Function>::Cast(
5534 context->Global()->Get(v8::String::New("frame_source_line")));
5535
5536 // Compile a function returning the data parameter.
5537 v8::Script::Compile(v8::String::New(debugger_call_with_data_source))->Run();
5538 debugger_call_with_data = v8::Local<v8::Function>::Cast(
5539 context->Global()->Get(v8::String::New("debugger_call_with_data")));
5540
5541 // Compile a function capturing closure.
5542 debugger_call_with_closure = v8::Local<v8::Function>::Cast(
5543 v8::Script::Compile(
5544 v8::String::New(debugger_call_with_closure_source))->Run());
5545
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00005546 // Calling a function through the debugger returns 0 frames if there are
5547 // no JavaScript frames.
5548 CHECK_EQ(v8::Integer::New(0), v8::Debug::Call(frame_count));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005549
5550 // Test that the number of frames can be retrieved.
5551 v8::Script::Compile(v8::String::New("CheckFrameCount(1)"))->Run();
5552 v8::Script::Compile(v8::String::New("function f() {"
5553 " CheckFrameCount(2);"
5554 "}; f()"))->Run();
5555
5556 // Test that the source line can be retrieved.
5557 v8::Script::Compile(v8::String::New("CheckSourceLine(0)"))->Run();
5558 v8::Script::Compile(v8::String::New("function f() {\n"
5559 " CheckSourceLine(1)\n"
5560 " CheckSourceLine(2)\n"
5561 " CheckSourceLine(3)\n"
5562 "}; f()"))->Run();
5563
5564 // Test that a parameter can be passed to a function called in the debugger.
5565 v8::Script::Compile(v8::String::New("CheckDataParameter()"))->Run();
5566
5567 // Test that a function with closure can be run in the debugger.
5568 v8::Script::Compile(v8::String::New("CheckClosure()"))->Run();
ager@chromium.org3a6061e2009-03-12 14:24:36 +00005569
5570
5571 // Test that the source line is correct when there is a line offset.
5572 v8::ScriptOrigin origin(v8::String::New("test"),
5573 v8::Integer::New(7));
5574 v8::Script::Compile(v8::String::New("CheckSourceLine(7)"), &origin)->Run();
5575 v8::Script::Compile(v8::String::New("function f() {\n"
5576 " CheckSourceLine(8)\n"
5577 " CheckSourceLine(9)\n"
5578 " CheckSourceLine(10)\n"
5579 "}; f()"), &origin)->Run();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005580}
ager@chromium.org381abbb2009-02-25 13:23:22 +00005581
5582
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005583// Debugger message handler which counts the number of breaks.
5584static void SendContinueCommand();
5585static void MessageHandlerBreakPointHitCount(
5586 const v8::Debug::Message& message) {
5587 if (message.IsEvent() && message.GetEvent() == v8::Break) {
5588 // Count the number of breaks.
5589 break_point_hit_count++;
5590
5591 SendContinueCommand();
5592 }
5593}
5594
5595
ager@chromium.org381abbb2009-02-25 13:23:22 +00005596// Test that clearing the debug event listener actually clears all break points
5597// and related information.
5598TEST(DebuggerUnload) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00005599 DebugLocalContext env;
5600
5601 // Check debugger is unloaded before it is used.
5602 CheckDebuggerUnloaded();
5603
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005604 // Set a debug event listener.
5605 break_point_hit_count = 0;
ager@chromium.org381abbb2009-02-25 13:23:22 +00005606 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
5607 v8::Undefined());
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005608 {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00005609 v8::HandleScope scope(env->GetIsolate());
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005610 // Create a couple of functions for the test.
5611 v8::Local<v8::Function> foo =
5612 CompileFunction(&env, "function foo(){x=1}", "foo");
5613 v8::Local<v8::Function> bar =
5614 CompileFunction(&env, "function bar(){y=2}", "bar");
ager@chromium.org381abbb2009-02-25 13:23:22 +00005615
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005616 // Set some break points.
5617 SetBreakPoint(foo, 0);
5618 SetBreakPoint(foo, 4);
5619 SetBreakPoint(bar, 0);
5620 SetBreakPoint(bar, 4);
ager@chromium.org381abbb2009-02-25 13:23:22 +00005621
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005622 // Make sure that the break points are there.
5623 break_point_hit_count = 0;
5624 foo->Call(env->Global(), 0, NULL);
5625 CHECK_EQ(2, break_point_hit_count);
5626 bar->Call(env->Global(), 0, NULL);
5627 CHECK_EQ(4, break_point_hit_count);
5628 }
sgjesse@chromium.orgf457d1f2009-06-17 14:18:36 +00005629
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005630 // Remove the debug event listener without clearing breakpoints. Do this
5631 // outside a handle scope.
ager@chromium.org381abbb2009-02-25 13:23:22 +00005632 v8::Debug::SetDebugEventListener(NULL);
5633 CheckDebuggerUnloaded(true);
5634
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005635 // Now set a debug message handler.
ager@chromium.org381abbb2009-02-25 13:23:22 +00005636 break_point_hit_count = 0;
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005637 v8::Debug::SetMessageHandler2(MessageHandlerBreakPointHitCount);
5638 {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00005639 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org381abbb2009-02-25 13:23:22 +00005640
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005641 // Get the test functions again.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00005642 v8::Local<v8::Function> foo(v8::Local<v8::Function>::Cast(
5643 env->Global()->Get(v8::String::New("foo"))));
ager@chromium.org381abbb2009-02-25 13:23:22 +00005644
sgjesse@chromium.orga9eaf5c2009-06-17 14:04:55 +00005645 foo->Call(env->Global(), 0, NULL);
5646 CHECK_EQ(0, break_point_hit_count);
5647
5648 // Set break points and run again.
5649 SetBreakPoint(foo, 0);
5650 SetBreakPoint(foo, 4);
5651 foo->Call(env->Global(), 0, NULL);
5652 CHECK_EQ(2, break_point_hit_count);
5653 }
5654
5655 // Remove the debug message handler without clearing breakpoints. Do this
5656 // outside a handle scope.
5657 v8::Debug::SetMessageHandler2(NULL);
ager@chromium.org381abbb2009-02-25 13:23:22 +00005658 CheckDebuggerUnloaded(true);
5659}
5660
5661
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005662// Sends continue command to the debugger.
5663static void SendContinueCommand() {
ager@chromium.org71daaf62009-04-01 07:22:49 +00005664 const int kBufferSize = 1000;
5665 uint16_t buffer[kBufferSize];
5666 const char* command_continue =
5667 "{\"seq\":0,"
5668 "\"type\":\"request\","
5669 "\"command\":\"continue\"}";
5670
5671 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer));
5672}
5673
5674
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005675// Debugger message handler which counts the number of times it is called.
5676static int message_handler_hit_count = 0;
5677static void MessageHandlerHitCount(const v8::Debug::Message& message) {
5678 message_handler_hit_count++;
5679
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005680 static char print_buffer[1000];
5681 v8::String::Value json(message.GetJSON());
5682 Utf16ToAscii(*json, json.length(), print_buffer);
5683 if (IsExceptionEventMessage(print_buffer)) {
5684 // Send a continue command for exception events.
5685 SendContinueCommand();
5686 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005687}
5688
5689
ager@chromium.org71daaf62009-04-01 07:22:49 +00005690// Test clearing the debug message handler.
5691TEST(DebuggerClearMessageHandler) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00005692 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00005693 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org71daaf62009-04-01 07:22:49 +00005694
5695 // Check debugger is unloaded before it is used.
5696 CheckDebuggerUnloaded();
5697
5698 // Set a debug message handler.
ager@chromium.org5ec48922009-05-05 07:25:34 +00005699 v8::Debug::SetMessageHandler2(MessageHandlerHitCount);
ager@chromium.org71daaf62009-04-01 07:22:49 +00005700
5701 // Run code to throw a unhandled exception. This should end up in the message
5702 // handler.
5703 CompileRun("throw 1");
5704
5705 // The message handler should be called.
5706 CHECK_GT(message_handler_hit_count, 0);
5707
5708 // Clear debug message handler.
5709 message_handler_hit_count = 0;
5710 v8::Debug::SetMessageHandler(NULL);
5711
5712 // Run code to throw a unhandled exception. This should end up in the message
5713 // handler.
5714 CompileRun("throw 1");
5715
5716 // The message handler should not be called more.
5717 CHECK_EQ(0, message_handler_hit_count);
5718
5719 CheckDebuggerUnloaded(true);
5720}
5721
5722
5723// Debugger message handler which clears the message handler while active.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005724static void MessageHandlerClearingMessageHandler(
ager@chromium.org5ec48922009-05-05 07:25:34 +00005725 const v8::Debug::Message& message) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00005726 message_handler_hit_count++;
5727
5728 // Clear debug message handler.
5729 v8::Debug::SetMessageHandler(NULL);
5730}
5731
5732
5733// Test clearing the debug message handler while processing a debug event.
5734TEST(DebuggerClearMessageHandlerWhileActive) {
ager@chromium.org71daaf62009-04-01 07:22:49 +00005735 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00005736 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org71daaf62009-04-01 07:22:49 +00005737
5738 // Check debugger is unloaded before it is used.
5739 CheckDebuggerUnloaded();
5740
5741 // Set a debug message handler.
ager@chromium.org5ec48922009-05-05 07:25:34 +00005742 v8::Debug::SetMessageHandler2(MessageHandlerClearingMessageHandler);
ager@chromium.org71daaf62009-04-01 07:22:49 +00005743
5744 // Run code to throw a unhandled exception. This should end up in the message
5745 // handler.
5746 CompileRun("throw 1");
5747
5748 // The message handler should be called.
5749 CHECK_EQ(1, message_handler_hit_count);
5750
5751 CheckDebuggerUnloaded(true);
5752}
5753
5754
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005755/* Test DebuggerHostDispatch */
5756/* In this test, the debugger waits for a command on a breakpoint
5757 * and is dispatching host commands while in the infinite loop.
5758 */
5759
5760class HostDispatchV8Thread : public v8::internal::Thread {
5761 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005762 HostDispatchV8Thread() : Thread("HostDispatchV8Thread") { }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005763 void Run();
5764};
5765
5766class HostDispatchDebuggerThread : public v8::internal::Thread {
5767 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005768 HostDispatchDebuggerThread() : Thread("HostDispatchDebuggerThread") { }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005769 void Run();
5770};
5771
5772Barriers* host_dispatch_barriers;
5773
ager@chromium.org5ec48922009-05-05 07:25:34 +00005774static void HostDispatchMessageHandler(const v8::Debug::Message& message) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005775 static char print_buffer[1000];
ager@chromium.org5ec48922009-05-05 07:25:34 +00005776 v8::String::Value json(message.GetJSON());
5777 Utf16ToAscii(*json, json.length(), print_buffer);
ager@chromium.org381abbb2009-02-25 13:23:22 +00005778}
5779
5780
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005781static void HostDispatchDispatchHandler() {
5782 host_dispatch_barriers->semaphore_1->Signal();
5783}
5784
5785
5786void HostDispatchV8Thread::Run() {
5787 const char* source_1 = "var y_global = 3;\n"
5788 "function cat( new_value ) {\n"
5789 " var x = new_value;\n"
5790 " y_global = 4;\n"
5791 " x = 3 * x + 1;\n"
5792 " y_global = 5;\n"
5793 " return x;\n"
5794 "}\n"
5795 "\n";
5796 const char* source_2 = "cat(17);\n";
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005797
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005798 v8::V8::Initialize();
ager@chromium.org381abbb2009-02-25 13:23:22 +00005799 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00005800 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org381abbb2009-02-25 13:23:22 +00005801
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00005802 // Set up message and host dispatch handlers.
ager@chromium.org5ec48922009-05-05 07:25:34 +00005803 v8::Debug::SetMessageHandler2(HostDispatchMessageHandler);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005804 v8::Debug::SetHostDispatchHandler(HostDispatchDispatchHandler, 10 /* ms */);
ager@chromium.org381abbb2009-02-25 13:23:22 +00005805
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005806 CompileRun(source_1);
5807 host_dispatch_barriers->barrier_1.Wait();
5808 host_dispatch_barriers->barrier_2.Wait();
5809 CompileRun(source_2);
5810}
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00005811
ager@chromium.org381abbb2009-02-25 13:23:22 +00005812
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005813void HostDispatchDebuggerThread::Run() {
5814 const int kBufSize = 1000;
5815 uint16_t buffer[kBufSize];
sgjesse@chromium.org3afc1582009-04-16 22:31:44 +00005816
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005817 const char* command_1 = "{\"seq\":101,"
5818 "\"type\":\"request\","
5819 "\"command\":\"setbreakpoint\","
5820 "\"arguments\":{\"type\":\"function\",\"target\":\"cat\",\"line\":3}}";
5821 const char* command_2 = "{\"seq\":102,"
5822 "\"type\":\"request\","
5823 "\"command\":\"continue\"}";
5824
5825 // v8 thread initializes, runs source_1
5826 host_dispatch_barriers->barrier_1.Wait();
5827 // 1: Set breakpoint in cat().
5828 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_1, buffer));
5829
5830 host_dispatch_barriers->barrier_2.Wait();
5831 // v8 thread starts compiling source_2.
5832 // Break happens, to run queued commands and host dispatches.
5833 // Wait for host dispatch to be processed.
5834 host_dispatch_barriers->semaphore_1->Wait();
5835 // 2: Continue evaluation
5836 v8::Debug::SendCommand(buffer, AsciiToUtf16(command_2, buffer));
5837}
5838
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005839
5840TEST(DebuggerHostDispatch) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005841 HostDispatchDebuggerThread host_dispatch_debugger_thread;
5842 HostDispatchV8Thread host_dispatch_v8_thread;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005843 i::FLAG_debugger_auto_break = true;
5844
5845 // Create a V8 environment
5846 Barriers stack_allocated_host_dispatch_barriers;
5847 stack_allocated_host_dispatch_barriers.Initialize();
5848 host_dispatch_barriers = &stack_allocated_host_dispatch_barriers;
5849
5850 host_dispatch_v8_thread.Start();
5851 host_dispatch_debugger_thread.Start();
5852
5853 host_dispatch_v8_thread.Join();
5854 host_dispatch_debugger_thread.Join();
ager@chromium.org381abbb2009-02-25 13:23:22 +00005855}
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005856
5857
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005858/* Test DebugMessageDispatch */
5859/* In this test, the V8 thread waits for a message from the debug thread.
5860 * The DebugMessageDispatchHandler is executed from the debugger thread
5861 * which signals the V8 thread to wake up.
5862 */
5863
5864class DebugMessageDispatchV8Thread : public v8::internal::Thread {
5865 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005866 DebugMessageDispatchV8Thread() : Thread("DebugMessageDispatchV8Thread") { }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005867 void Run();
5868};
5869
5870class DebugMessageDispatchDebuggerThread : public v8::internal::Thread {
5871 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005872 DebugMessageDispatchDebuggerThread()
5873 : Thread("DebugMessageDispatchDebuggerThread") { }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005874 void Run();
5875};
5876
5877Barriers* debug_message_dispatch_barriers;
5878
5879
5880static void DebugMessageHandler() {
5881 debug_message_dispatch_barriers->semaphore_1->Signal();
5882}
5883
5884
5885void DebugMessageDispatchV8Thread::Run() {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005886 v8::V8::Initialize();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005887 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00005888 v8::HandleScope scope(env->GetIsolate());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005889
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00005890 // Set up debug message dispatch handler.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005891 v8::Debug::SetDebugMessageDispatchHandler(DebugMessageHandler);
5892
5893 CompileRun("var y = 1 + 2;\n");
5894 debug_message_dispatch_barriers->barrier_1.Wait();
5895 debug_message_dispatch_barriers->semaphore_1->Wait();
5896 debug_message_dispatch_barriers->barrier_2.Wait();
5897}
5898
5899
5900void DebugMessageDispatchDebuggerThread::Run() {
5901 debug_message_dispatch_barriers->barrier_1.Wait();
5902 SendContinueCommand();
5903 debug_message_dispatch_barriers->barrier_2.Wait();
5904}
5905
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005906
5907TEST(DebuggerDebugMessageDispatch) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005908 DebugMessageDispatchDebuggerThread debug_message_dispatch_debugger_thread;
5909 DebugMessageDispatchV8Thread debug_message_dispatch_v8_thread;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005910
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005911 i::FLAG_debugger_auto_break = true;
5912
5913 // Create a V8 environment
5914 Barriers stack_allocated_debug_message_dispatch_barriers;
5915 stack_allocated_debug_message_dispatch_barriers.Initialize();
5916 debug_message_dispatch_barriers =
5917 &stack_allocated_debug_message_dispatch_barriers;
5918
5919 debug_message_dispatch_v8_thread.Start();
5920 debug_message_dispatch_debugger_thread.Start();
5921
5922 debug_message_dispatch_v8_thread.Join();
5923 debug_message_dispatch_debugger_thread.Join();
5924}
5925
5926
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005927TEST(DebuggerAgent) {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00005928 v8::V8::Initialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005929 i::Debugger* debugger = i::Isolate::Current()->debugger();
ager@chromium.orga1645e22009-09-09 19:27:10 +00005930 // Make sure these ports is not used by other tests to allow tests to run in
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005931 // parallel.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00005932 const int kPort1 = 5858 + FlagDependentPortOffset();
5933 const int kPort2 = 5857 + FlagDependentPortOffset();
5934 const int kPort3 = 5856 + FlagDependentPortOffset();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005935
ager@chromium.orga1645e22009-09-09 19:27:10 +00005936 // Make a string with the port2 number.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005937 const int kPortBufferLen = 6;
ager@chromium.orga1645e22009-09-09 19:27:10 +00005938 char port2_str[kPortBufferLen];
5939 OS::SNPrintF(i::Vector<char>(port2_str, kPortBufferLen), "%d", kPort2);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005940
5941 bool ok;
5942
5943 // Initialize the socket library.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00005944 i::Socket::SetUp();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005945
5946 // Test starting and stopping the agent without any client connection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005947 debugger->StartAgent("test", kPort1);
5948 debugger->StopAgent();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005949 // Test starting the agent, connecting a client and shutting down the agent
5950 // with the client connected.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005951 ok = debugger->StartAgent("test", kPort2);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005952 CHECK(ok);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005953 debugger->WaitForAgent();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005954 i::Socket* client = i::OS::CreateSocket();
ager@chromium.orga1645e22009-09-09 19:27:10 +00005955 ok = client->Connect("localhost", port2_str);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005956 CHECK(ok);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005957 // It is important to wait for a message from the agent. Otherwise,
5958 // we can close the server socket during "accept" syscall, making it failing
5959 // (at least on Linux), and the test will work incorrectly.
5960 char buf;
5961 ok = client->Receive(&buf, 1) == 1;
5962 CHECK(ok);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005963 debugger->StopAgent();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005964 delete client;
5965
5966 // Test starting and stopping the agent with the required port already
5967 // occoupied.
5968 i::Socket* server = i::OS::CreateSocket();
ager@chromium.orga1645e22009-09-09 19:27:10 +00005969 server->Bind(kPort3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005970
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005971 debugger->StartAgent("test", kPort3);
5972 debugger->StopAgent();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005973
5974 delete server;
5975}
5976
5977
5978class DebuggerAgentProtocolServerThread : public i::Thread {
5979 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00005980 explicit DebuggerAgentProtocolServerThread(int port)
5981 : Thread("DebuggerAgentProtocolServerThread"),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005982 port_(port),
5983 server_(NULL),
5984 client_(NULL),
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005985 listening_(OS::CreateSemaphore(0)) {
5986 }
5987 ~DebuggerAgentProtocolServerThread() {
5988 // Close both sockets.
5989 delete client_;
5990 delete server_;
5991 delete listening_;
5992 }
5993
5994 void Run();
5995 void WaitForListening() { listening_->Wait(); }
5996 char* body() { return *body_; }
5997
5998 private:
5999 int port_;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00006000 i::SmartArrayPointer<char> body_;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006001 i::Socket* server_; // Server socket used for bind/accept.
6002 i::Socket* client_; // Single client connection used by the test.
6003 i::Semaphore* listening_; // Signalled when the server is in listen mode.
6004};
6005
6006
6007void DebuggerAgentProtocolServerThread::Run() {
6008 bool ok;
6009
6010 // Create the server socket and bind it to the requested port.
6011 server_ = i::OS::CreateSocket();
6012 CHECK(server_ != NULL);
6013 ok = server_->Bind(port_);
6014 CHECK(ok);
6015
6016 // Listen for new connections.
6017 ok = server_->Listen(1);
6018 CHECK(ok);
6019 listening_->Signal();
6020
6021 // Accept a connection.
6022 client_ = server_->Accept();
6023 CHECK(client_ != NULL);
6024
6025 // Receive a debugger agent protocol message.
6026 i::DebuggerAgentUtil::ReceiveMessage(client_);
6027}
6028
6029
6030TEST(DebuggerAgentProtocolOverflowHeader) {
6031 // Make sure this port is not used by other tests to allow tests to run in
6032 // parallel.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00006033 const int kPort = 5860 + FlagDependentPortOffset();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006034 static const char* kLocalhost = "localhost";
6035
6036 // Make a string with the port number.
6037 const int kPortBufferLen = 6;
6038 char port_str[kPortBufferLen];
6039 OS::SNPrintF(i::Vector<char>(port_str, kPortBufferLen), "%d", kPort);
6040
6041 // Initialize the socket library.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00006042 i::Socket::SetUp();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006043
6044 // Create a socket server to receive a debugger agent message.
6045 DebuggerAgentProtocolServerThread* server =
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00006046 new DebuggerAgentProtocolServerThread(kPort);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006047 server->Start();
6048 server->WaitForListening();
6049
6050 // Connect.
6051 i::Socket* client = i::OS::CreateSocket();
6052 CHECK(client != NULL);
6053 bool ok = client->Connect(kLocalhost, port_str);
6054 CHECK(ok);
6055
6056 // Send headers which overflow the receive buffer.
6057 static const int kBufferSize = 1000;
6058 char buffer[kBufferSize];
6059
6060 // Long key and short value: XXXX....XXXX:0\r\n.
6061 for (int i = 0; i < kBufferSize - 4; i++) {
6062 buffer[i] = 'X';
6063 }
6064 buffer[kBufferSize - 4] = ':';
6065 buffer[kBufferSize - 3] = '0';
6066 buffer[kBufferSize - 2] = '\r';
6067 buffer[kBufferSize - 1] = '\n';
6068 client->Send(buffer, kBufferSize);
6069
6070 // Short key and long value: X:XXXX....XXXX\r\n.
6071 buffer[0] = 'X';
6072 buffer[1] = ':';
6073 for (int i = 2; i < kBufferSize - 2; i++) {
6074 buffer[i] = 'X';
6075 }
6076 buffer[kBufferSize - 2] = '\r';
6077 buffer[kBufferSize - 1] = '\n';
6078 client->Send(buffer, kBufferSize);
6079
6080 // Add empty body to request.
6081 const char* content_length_zero_header = "Content-Length:0\r\n";
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006082 client->Send(content_length_zero_header,
6083 StrLength(content_length_zero_header));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006084 client->Send("\r\n", 2);
6085
6086 // Wait until data is received.
6087 server->Join();
6088
6089 // Check for empty body.
6090 CHECK(server->body() == NULL);
6091
6092 // Close the client before the server to avoid TIME_WAIT issues.
6093 client->Shutdown();
6094 delete client;
6095 delete server;
6096}
ager@chromium.org41826e72009-03-30 13:30:57 +00006097
6098
6099// Test for issue http://code.google.com/p/v8/issues/detail?id=289.
6100// Make sure that DebugGetLoadedScripts doesn't return scripts
6101// with disposed external source.
6102class EmptyExternalStringResource : public v8::String::ExternalStringResource {
6103 public:
6104 EmptyExternalStringResource() { empty_[0] = 0; }
6105 virtual ~EmptyExternalStringResource() {}
6106 virtual size_t length() const { return empty_.length(); }
6107 virtual const uint16_t* data() const { return empty_.start(); }
6108 private:
6109 ::v8::internal::EmbeddedVector<uint16_t, 1> empty_;
6110};
6111
6112
6113TEST(DebugGetLoadedScripts) {
ager@chromium.org41826e72009-03-30 13:30:57 +00006114 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006115 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006116 env.ExposeDebug();
6117
ager@chromium.org41826e72009-03-30 13:30:57 +00006118 EmptyExternalStringResource source_ext_str;
6119 v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00006120 v8::Handle<v8::Script> evil_script(v8::Script::Compile(source));
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00006121 // "use" evil_script to make the compiler happy.
6122 (void) evil_script;
ager@chromium.org41826e72009-03-30 13:30:57 +00006123 Handle<i::ExternalTwoByteString> i_source(
6124 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
6125 // This situation can happen if source was an external string disposed
6126 // by its owner.
6127 i_source->set_resource(0);
6128
6129 bool allow_natives_syntax = i::FLAG_allow_natives_syntax;
6130 i::FLAG_allow_natives_syntax = true;
6131 CompileRun(
6132 "var scripts = %DebugGetLoadedScripts();"
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006133 "var count = scripts.length;"
6134 "for (var i = 0; i < count; ++i) {"
6135 " scripts[i].line_ends;"
ager@chromium.org41826e72009-03-30 13:30:57 +00006136 "}");
6137 // Must not crash while accessing line_ends.
6138 i::FLAG_allow_natives_syntax = allow_natives_syntax;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006139
6140 // Some scripts are retrieved - at least the number of native scripts.
6141 CHECK_GT((*env)->Global()->Get(v8::String::New("count"))->Int32Value(), 8);
ager@chromium.org41826e72009-03-30 13:30:57 +00006142}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006143
6144
6145// Test script break points set on lines.
6146TEST(ScriptNameAndData) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006147 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006148 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006149 env.ExposeDebug();
6150
6151 // Create functions for retrieving script name and data for the function on
6152 // the top frame when hitting a break point.
6153 frame_script_name = CompileFunction(&env,
6154 frame_script_name_source,
6155 "frame_script_name");
6156 frame_script_data = CompileFunction(&env,
6157 frame_script_data_source,
6158 "frame_script_data");
ager@chromium.org5c838252010-02-19 08:53:10 +00006159 compiled_script_data = CompileFunction(&env,
6160 compiled_script_data_source,
6161 "compiled_script_data");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006162
6163 v8::Debug::SetDebugEventListener(DebugEventBreakPointHitCount,
6164 v8::Undefined());
6165
6166 // Test function source.
6167 v8::Local<v8::String> script = v8::String::New(
6168 "function f() {\n"
6169 " debugger;\n"
6170 "}\n");
6171
6172 v8::ScriptOrigin origin1 = v8::ScriptOrigin(v8::String::New("name"));
6173 v8::Handle<v8::Script> script1 = v8::Script::Compile(script, &origin1);
6174 script1->SetData(v8::String::New("data"));
6175 script1->Run();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006176 v8::Local<v8::Function> f;
6177 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6178
6179 f->Call(env->Global(), 0, NULL);
6180 CHECK_EQ(1, break_point_hit_count);
6181 CHECK_EQ("name", last_script_name_hit);
6182 CHECK_EQ("data", last_script_data_hit);
6183
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006184 // Compile the same script again without setting data. As the compilation
6185 // cache is disabled when debugging expect the data to be missing.
6186 v8::Script::Compile(script, &origin1)->Run();
6187 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6188 f->Call(env->Global(), 0, NULL);
6189 CHECK_EQ(2, break_point_hit_count);
6190 CHECK_EQ("name", last_script_name_hit);
6191 CHECK_EQ("", last_script_data_hit); // Undefined results in empty string.
6192
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006193 v8::Local<v8::String> data_obj_source = v8::String::New(
6194 "({ a: 'abc',\n"
6195 " b: 123,\n"
6196 " toString: function() { return this.a + ' ' + this.b; }\n"
6197 "})\n");
6198 v8::Local<v8::Value> data_obj = v8::Script::Compile(data_obj_source)->Run();
6199 v8::ScriptOrigin origin2 = v8::ScriptOrigin(v8::String::New("new name"));
6200 v8::Handle<v8::Script> script2 = v8::Script::Compile(script, &origin2);
6201 script2->Run();
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00006202 script2->SetData(data_obj->ToString());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006203 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6204 f->Call(env->Global(), 0, NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006205 CHECK_EQ(3, break_point_hit_count);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006206 CHECK_EQ("new name", last_script_name_hit);
6207 CHECK_EQ("abc 123", last_script_data_hit);
ager@chromium.org5c838252010-02-19 08:53:10 +00006208
6209 v8::Handle<v8::Script> script3 =
6210 v8::Script::Compile(script, &origin2, NULL,
6211 v8::String::New("in compile"));
6212 CHECK_EQ("in compile", last_script_data_hit);
6213 script3->Run();
6214 f = v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6215 f->Call(env->Global(), 0, NULL);
6216 CHECK_EQ(4, break_point_hit_count);
6217 CHECK_EQ("in compile", last_script_data_hit);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006218}
ager@chromium.org9085a012009-05-11 19:22:57 +00006219
6220
6221static v8::Persistent<v8::Context> expected_context;
6222static v8::Handle<v8::Value> expected_context_data;
6223
6224
6225// Check that the expected context is the one generating the debug event.
6226static void ContextCheckMessageHandler(const v8::Debug::Message& message) {
6227 CHECK(message.GetEventContext() == expected_context);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00006228 CHECK(message.GetEventContext()->GetEmbedderData(0)->StrictEquals(
ager@chromium.org9085a012009-05-11 19:22:57 +00006229 expected_context_data));
6230 message_handler_hit_count++;
6231
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006232 static char print_buffer[1000];
6233 v8::String::Value json(message.GetJSON());
6234 Utf16ToAscii(*json, json.length(), print_buffer);
6235
ager@chromium.org9085a012009-05-11 19:22:57 +00006236 // Send a continue command for break events.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006237 if (IsBreakEventMessage(print_buffer)) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006238 SendContinueCommand();
ager@chromium.org9085a012009-05-11 19:22:57 +00006239 }
6240}
6241
6242
6243// Test which creates two contexts and sets different embedder data on each.
6244// Checks that this data is set correctly and that when the debug message
6245// handler is called the expected context is the one active.
6246TEST(ContextData) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006247 v8::HandleScope scope(v8::Isolate::GetCurrent());
ager@chromium.org9085a012009-05-11 19:22:57 +00006248
6249 v8::Debug::SetMessageHandler2(ContextCheckMessageHandler);
6250
6251 // Create two contexts.
6252 v8::Persistent<v8::Context> context_1;
6253 v8::Persistent<v8::Context> context_2;
6254 v8::Handle<v8::ObjectTemplate> global_template =
6255 v8::Handle<v8::ObjectTemplate>();
6256 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>();
6257 context_1 = v8::Context::New(NULL, global_template, global_object);
6258 context_2 = v8::Context::New(NULL, global_template, global_object);
6259
6260 // Default data value is undefined.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00006261 CHECK(context_1->GetEmbedderData(0)->IsUndefined());
6262 CHECK(context_2->GetEmbedderData(0)->IsUndefined());
ager@chromium.org9085a012009-05-11 19:22:57 +00006263
6264 // Set and check different data values.
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00006265 v8::Handle<v8::String> data_1 = v8::String::New("1");
6266 v8::Handle<v8::String> data_2 = v8::String::New("2");
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00006267 context_1->SetEmbedderData(0, data_1);
6268 context_2->SetEmbedderData(0, data_2);
6269 CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1));
6270 CHECK(context_2->GetEmbedderData(0)->StrictEquals(data_2));
ager@chromium.org9085a012009-05-11 19:22:57 +00006271
6272 // Simple test function which causes a break.
6273 const char* source = "function f() { debugger; }";
6274
6275 // Enter and run function in the first context.
6276 {
6277 v8::Context::Scope context_scope(context_1);
6278 expected_context = context_1;
6279 expected_context_data = data_1;
6280 v8::Local<v8::Function> f = CompileFunction(source, "f");
6281 f->Call(context_1->Global(), 0, NULL);
6282 }
6283
6284
6285 // Enter and run function in the second context.
6286 {
6287 v8::Context::Scope context_scope(context_2);
6288 expected_context = context_2;
6289 expected_context_data = data_2;
6290 v8::Local<v8::Function> f = CompileFunction(source, "f");
6291 f->Call(context_2->Global(), 0, NULL);
6292 }
6293
6294 // Two times compile event and two times break event.
6295 CHECK_GT(message_handler_hit_count, 4);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006296
6297 v8::Debug::SetMessageHandler2(NULL);
6298 CheckDebuggerUnloaded();
6299}
6300
6301
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006302// Debug message handler which issues a debug break when it hits a break event.
6303static int message_handler_break_hit_count = 0;
6304static void DebugBreakMessageHandler(const v8::Debug::Message& message) {
6305 // Schedule a debug break for break events.
6306 if (message.IsEvent() && message.GetEvent() == v8::Break) {
6307 message_handler_break_hit_count++;
6308 if (message_handler_break_hit_count == 1) {
6309 v8::Debug::DebugBreak();
6310 }
6311 }
6312
6313 // Issue a continue command if this event will not cause the VM to start
6314 // running.
6315 if (!message.WillStartRunning()) {
6316 SendContinueCommand();
6317 }
6318}
6319
6320
6321// Test that a debug break can be scheduled while in a message handler.
6322TEST(DebugBreakInMessageHandler) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006323 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006324 v8::HandleScope scope(env->GetIsolate());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006325
6326 v8::Debug::SetMessageHandler2(DebugBreakMessageHandler);
6327
6328 // Test functions.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006329 const char* script = "function f() { debugger; g(); } function g() { }";
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006330 CompileRun(script);
6331 v8::Local<v8::Function> f =
6332 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6333 v8::Local<v8::Function> g =
6334 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("g")));
6335
6336 // Call f then g. The debugger statement in f will casue a break which will
6337 // cause another break.
6338 f->Call(env->Global(), 0, NULL);
6339 CHECK_EQ(2, message_handler_break_hit_count);
6340 // Calling g will not cause any additional breaks.
6341 g->Call(env->Global(), 0, NULL);
6342 CHECK_EQ(2, message_handler_break_hit_count);
6343}
6344
6345
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006346#ifndef V8_INTERPRETED_REGEXP
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006347// Debug event handler which gets the function on the top frame and schedules a
6348// break a number of times.
6349static void DebugEventDebugBreak(
6350 v8::DebugEvent event,
6351 v8::Handle<v8::Object> exec_state,
6352 v8::Handle<v8::Object> event_data,
6353 v8::Handle<v8::Value> data) {
6354
6355 if (event == v8::Break) {
6356 break_point_hit_count++;
6357
6358 // Get the name of the top frame function.
6359 if (!frame_function_name.IsEmpty()) {
6360 // Get the name of the function.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006361 const int argc = 2;
6362 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) };
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006363 v8::Handle<v8::Value> result = frame_function_name->Call(exec_state,
6364 argc, argv);
6365 if (result->IsUndefined()) {
6366 last_function_hit[0] = '\0';
6367 } else {
6368 CHECK(result->IsString());
6369 v8::Handle<v8::String> function_name(result->ToString());
6370 function_name->WriteAscii(last_function_hit);
6371 }
6372 }
6373
6374 // Keep forcing breaks.
6375 if (break_point_hit_count < 20) {
6376 v8::Debug::DebugBreak();
6377 }
6378 }
6379}
6380
6381
6382TEST(RegExpDebugBreak) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00006383 // This test only applies to native regexps.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006384 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006385 v8::HandleScope scope(env->GetIsolate());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006386
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006387 // Create a function for checking the function when hitting a break point.
6388 frame_function_name = CompileFunction(&env,
6389 frame_function_name_source,
6390 "frame_function_name");
6391
6392 // Test RegExp which matches white spaces and comments at the begining of a
6393 // source line.
6394 const char* script =
6395 "var sourceLineBeginningSkip = /^(?:[ \\v\\h]*(?:\\/\\*.*?\\*\\/)*)*/;\n"
6396 "function f(s) { return s.match(sourceLineBeginningSkip)[0].length; }";
6397
6398 v8::Local<v8::Function> f = CompileFunction(script, "f");
6399 const int argc = 1;
6400 v8::Handle<v8::Value> argv[argc] = { v8::String::New(" /* xxx */ a=0;") };
6401 v8::Local<v8::Value> result = f->Call(env->Global(), argc, argv);
6402 CHECK_EQ(12, result->Int32Value());
6403
6404 v8::Debug::SetDebugEventListener(DebugEventDebugBreak);
6405 v8::Debug::DebugBreak();
6406 result = f->Call(env->Global(), argc, argv);
6407
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006408 // Check that there was only one break event. Matching RegExp should not
6409 // cause Break events.
6410 CHECK_EQ(1, break_point_hit_count);
6411 CHECK_EQ("f", last_function_hit);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006412}
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006413#endif // V8_INTERPRETED_REGEXP
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00006414
6415
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006416// Common part of EvalContextData and NestedBreakEventContextData tests.
6417static void ExecuteScriptForContextCheck() {
6418 // Create a context.
6419 v8::Persistent<v8::Context> context_1;
6420 v8::Handle<v8::ObjectTemplate> global_template =
6421 v8::Handle<v8::ObjectTemplate>();
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00006422 context_1 = v8::Context::New(NULL, global_template);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006423
6424 // Default data value is undefined.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00006425 CHECK(context_1->GetEmbedderData(0)->IsUndefined());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006426
6427 // Set and check a data value.
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00006428 v8::Handle<v8::String> data_1 = v8::String::New("1");
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00006429 context_1->SetEmbedderData(0, data_1);
6430 CHECK(context_1->GetEmbedderData(0)->StrictEquals(data_1));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006431
6432 // Simple test function with eval that causes a break.
6433 const char* source = "function f() { eval('debugger;'); }";
6434
6435 // Enter and run function in the context.
6436 {
6437 v8::Context::Scope context_scope(context_1);
6438 expected_context = context_1;
6439 expected_context_data = data_1;
6440 v8::Local<v8::Function> f = CompileFunction(source, "f");
6441 f->Call(context_1->Global(), 0, NULL);
6442 }
6443}
6444
6445
6446// Test which creates a context and sets embedder data on it. Checks that this
6447// data is set correctly and that when the debug message handler is called for
6448// break event in an eval statement the expected context is the one returned by
6449// Message.GetEventContext.
6450TEST(EvalContextData) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006451 v8::HandleScope scope(v8::Isolate::GetCurrent());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006452 v8::Debug::SetMessageHandler2(ContextCheckMessageHandler);
6453
6454 ExecuteScriptForContextCheck();
6455
6456 // One time compile event and one time break event.
6457 CHECK_GT(message_handler_hit_count, 2);
6458 v8::Debug::SetMessageHandler2(NULL);
6459 CheckDebuggerUnloaded();
6460}
6461
6462
6463static bool sent_eval = false;
6464static int break_count = 0;
6465static int continue_command_send_count = 0;
6466// Check that the expected context is the one generating the debug event
6467// including the case of nested break event.
6468static void DebugEvalContextCheckMessageHandler(
6469 const v8::Debug::Message& message) {
6470 CHECK(message.GetEventContext() == expected_context);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00006471 CHECK(message.GetEventContext()->GetEmbedderData(0)->StrictEquals(
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006472 expected_context_data));
6473 message_handler_hit_count++;
6474
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006475 static char print_buffer[1000];
6476 v8::String::Value json(message.GetJSON());
6477 Utf16ToAscii(*json, json.length(), print_buffer);
6478
6479 if (IsBreakEventMessage(print_buffer)) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006480 break_count++;
6481 if (!sent_eval) {
6482 sent_eval = true;
6483
6484 const int kBufferSize = 1000;
6485 uint16_t buffer[kBufferSize];
6486 const char* eval_command =
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00006487 "{\"seq\":0,"
6488 "\"type\":\"request\","
6489 "\"command\":\"evaluate\","
6490 "\"arguments\":{\"expression\":\"debugger;\","
6491 "\"global\":true,\"disable_break\":false}}";
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006492
6493 // Send evaluate command.
6494 v8::Debug::SendCommand(buffer, AsciiToUtf16(eval_command, buffer));
6495 return;
6496 } else {
6497 // It's a break event caused by the evaluation request above.
6498 SendContinueCommand();
6499 continue_command_send_count++;
6500 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00006501 } else if (IsEvaluateResponseMessage(print_buffer) &&
6502 continue_command_send_count < 2) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006503 // Response to the evaluation request. We're still on the breakpoint so
6504 // send continue.
6505 SendContinueCommand();
6506 continue_command_send_count++;
6507 }
6508}
6509
6510
6511// Tests that context returned for break event is correct when the event occurs
6512// in 'evaluate' debugger request.
6513TEST(NestedBreakEventContextData) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006514 v8::HandleScope scope(v8::Isolate::GetCurrent());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006515 break_count = 0;
6516 message_handler_hit_count = 0;
6517 v8::Debug::SetMessageHandler2(DebugEvalContextCheckMessageHandler);
6518
6519 ExecuteScriptForContextCheck();
6520
6521 // One time compile event and two times break event.
6522 CHECK_GT(message_handler_hit_count, 3);
6523
6524 // One break from the source and another from the evaluate request.
6525 CHECK_EQ(break_count, 2);
6526 v8::Debug::SetMessageHandler2(NULL);
6527 CheckDebuggerUnloaded();
6528}
6529
6530
6531// Debug event listener which counts the script collected events.
6532int script_collected_count = 0;
6533static void DebugEventScriptCollectedEvent(v8::DebugEvent event,
6534 v8::Handle<v8::Object> exec_state,
6535 v8::Handle<v8::Object> event_data,
6536 v8::Handle<v8::Value> data) {
6537 // Count the number of breaks.
6538 if (event == v8::ScriptCollected) {
6539 script_collected_count++;
6540 }
6541}
6542
6543
6544// Test that scripts collected are reported through the debug event listener.
6545TEST(ScriptCollectedEvent) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006546 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006547 break_point_hit_count = 0;
6548 script_collected_count = 0;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006549 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006550 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006551
6552 // Request the loaded scripts to initialize the debugger script cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006553 debug->GetLoadedScripts();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006554
6555 // Do garbage collection to ensure that only the script in this test will be
6556 // collected afterwards.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00006557 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006558
6559 script_collected_count = 0;
6560 v8::Debug::SetDebugEventListener(DebugEventScriptCollectedEvent,
6561 v8::Undefined());
6562 {
6563 v8::Script::Compile(v8::String::New("eval('a=1')"))->Run();
6564 v8::Script::Compile(v8::String::New("eval('a=2')"))->Run();
6565 }
6566
6567 // Do garbage collection to collect the script above which is no longer
6568 // referenced.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00006569 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006570
6571 CHECK_EQ(2, script_collected_count);
6572
6573 v8::Debug::SetDebugEventListener(NULL);
6574 CheckDebuggerUnloaded();
6575}
6576
6577
6578// Debug event listener which counts the script collected events.
6579int script_collected_message_count = 0;
6580static void ScriptCollectedMessageHandler(const v8::Debug::Message& message) {
6581 // Count the number of scripts collected.
6582 if (message.IsEvent() && message.GetEvent() == v8::ScriptCollected) {
6583 script_collected_message_count++;
6584 v8::Handle<v8::Context> context = message.GetEventContext();
6585 CHECK(context.IsEmpty());
6586 }
6587}
6588
6589
6590// Test that GetEventContext doesn't fail and return empty handle for
6591// ScriptCollected events.
6592TEST(ScriptCollectedEventContext) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006593 v8::Isolate* isolate = v8::Isolate::GetCurrent();
6594 v8::internal::Debug* debug =
6595 reinterpret_cast<v8::internal::Isolate*>(isolate)->debug();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006596 script_collected_message_count = 0;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006597 v8::HandleScope scope(isolate);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006598
6599 { // Scope for the DebugLocalContext.
6600 DebugLocalContext env;
6601
6602 // Request the loaded scripts to initialize the debugger script cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006603 debug->GetLoadedScripts();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006604
6605 // Do garbage collection to ensure that only the script in this test will be
6606 // collected afterwards.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00006607 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006608
6609 v8::Debug::SetMessageHandler2(ScriptCollectedMessageHandler);
6610 {
6611 v8::Script::Compile(v8::String::New("eval('a=1')"))->Run();
6612 v8::Script::Compile(v8::String::New("eval('a=2')"))->Run();
6613 }
6614 }
6615
6616 // Do garbage collection to collect the script above which is no longer
6617 // referenced.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00006618 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006619
6620 CHECK_EQ(2, script_collected_message_count);
6621
6622 v8::Debug::SetMessageHandler2(NULL);
6623}
6624
6625
6626// Debug event listener which counts the after compile events.
6627int after_compile_message_count = 0;
6628static void AfterCompileMessageHandler(const v8::Debug::Message& message) {
6629 // Count the number of scripts collected.
6630 if (message.IsEvent()) {
6631 if (message.GetEvent() == v8::AfterCompile) {
6632 after_compile_message_count++;
6633 } else if (message.GetEvent() == v8::Break) {
6634 SendContinueCommand();
6635 }
6636 }
6637}
6638
6639
6640// Tests that after compile event is sent as many times as there are scripts
6641// compiled.
6642TEST(AfterCompileMessageWhenMessageHandlerIsReset) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006643 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006644 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006645 after_compile_message_count = 0;
6646 const char* script = "var a=1";
6647
6648 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6649 v8::Script::Compile(v8::String::New(script))->Run();
6650 v8::Debug::SetMessageHandler2(NULL);
6651
6652 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6653 v8::Debug::DebugBreak();
6654 v8::Script::Compile(v8::String::New(script))->Run();
6655
6656 // Setting listener to NULL should cause debugger unload.
6657 v8::Debug::SetMessageHandler2(NULL);
6658 CheckDebuggerUnloaded();
6659
6660 // Compilation cache should be disabled when debugger is active.
6661 CHECK_EQ(2, after_compile_message_count);
6662}
6663
6664
6665// Tests that break event is sent when message handler is reset.
6666TEST(BreakMessageWhenMessageHandlerIsReset) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006667 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006668 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006669 after_compile_message_count = 0;
6670 const char* script = "function f() {};";
6671
6672 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6673 v8::Script::Compile(v8::String::New(script))->Run();
6674 v8::Debug::SetMessageHandler2(NULL);
6675
6676 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6677 v8::Debug::DebugBreak();
6678 v8::Local<v8::Function> f =
6679 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6680 f->Call(env->Global(), 0, NULL);
6681
6682 // Setting message handler to NULL should cause debugger unload.
6683 v8::Debug::SetMessageHandler2(NULL);
6684 CheckDebuggerUnloaded();
6685
6686 // Compilation cache should be disabled when debugger is active.
6687 CHECK_EQ(1, after_compile_message_count);
6688}
6689
6690
6691static int exception_event_count = 0;
6692static void ExceptionMessageHandler(const v8::Debug::Message& message) {
6693 if (message.IsEvent() && message.GetEvent() == v8::Exception) {
6694 exception_event_count++;
6695 SendContinueCommand();
6696 }
6697}
6698
6699
6700// Tests that exception event is sent when message handler is reset.
6701TEST(ExceptionMessageWhenMessageHandlerIsReset) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006702 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006703 v8::HandleScope scope(env->GetIsolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00006704
6705 // For this test, we want to break on uncaught exceptions:
6706 ChangeBreakOnException(false, true);
6707
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006708 exception_event_count = 0;
6709 const char* script = "function f() {throw new Error()};";
6710
6711 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6712 v8::Script::Compile(v8::String::New(script))->Run();
6713 v8::Debug::SetMessageHandler2(NULL);
6714
6715 v8::Debug::SetMessageHandler2(ExceptionMessageHandler);
6716 v8::Local<v8::Function> f =
6717 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f")));
6718 f->Call(env->Global(), 0, NULL);
6719
6720 // Setting message handler to NULL should cause debugger unload.
6721 v8::Debug::SetMessageHandler2(NULL);
6722 CheckDebuggerUnloaded();
6723
6724 CHECK_EQ(1, exception_event_count);
ager@chromium.org9085a012009-05-11 19:22:57 +00006725}
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006726
6727
6728// Tests after compile event is sent when there are some provisional
6729// breakpoints out of the scripts lines range.
6730TEST(ProvisionalBreakpointOnLineOutOfRange) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006731 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006732 v8::HandleScope scope(env->GetIsolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006733 env.ExposeDebug();
6734 const char* script = "function f() {};";
6735 const char* resource_name = "test_resource";
6736
6737 // Set a couple of provisional breakpoint on lines out of the script lines
6738 // range.
6739 int sbp1 = SetScriptBreakPointByNameFromJS(resource_name, 3,
6740 -1 /* no column */);
6741 int sbp2 = SetScriptBreakPointByNameFromJS(resource_name, 5, 5);
6742
6743 after_compile_message_count = 0;
6744 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler);
6745
6746 v8::ScriptOrigin origin(
6747 v8::String::New(resource_name),
6748 v8::Integer::New(10),
6749 v8::Integer::New(1));
6750 // Compile a script whose first line number is greater than the breakpoints'
6751 // lines.
6752 v8::Script::Compile(v8::String::New(script), &origin)->Run();
6753
6754 // If the script is compiled successfully there is exactly one after compile
6755 // event. In case of an exception in debugger code after compile event is not
6756 // sent.
6757 CHECK_EQ(1, after_compile_message_count);
6758
6759 ClearBreakPointFromJS(sbp1);
6760 ClearBreakPointFromJS(sbp2);
6761 v8::Debug::SetMessageHandler2(NULL);
6762}
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006763
6764
6765static void BreakMessageHandler(const v8::Debug::Message& message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006766 i::Isolate* isolate = i::Isolate::Current();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006767 if (message.IsEvent() && message.GetEvent() == v8::Break) {
6768 // Count the number of breaks.
6769 break_point_hit_count++;
6770
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006771 i::HandleScope scope(isolate);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00006772 message.GetJSON();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006773
6774 SendContinueCommand();
6775 } else if (message.IsEvent() && message.GetEvent() == v8::AfterCompile) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006776 i::HandleScope scope(isolate);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006777
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006778 bool is_debug_break = isolate->stack_guard()->IsDebugBreak();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006779 // Force DebugBreak flag while serializer is working.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006780 isolate->stack_guard()->DebugBreak();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006781
6782 // Force serialization to trigger some internal JS execution.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00006783 message.GetJSON();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006784
6785 // Restore previous state.
6786 if (is_debug_break) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006787 isolate->stack_guard()->DebugBreak();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006788 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006789 isolate->stack_guard()->Continue(i::DEBUGBREAK);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006790 }
6791 }
6792}
6793
6794
6795// Test that if DebugBreak is forced it is ignored when code from
6796// debug-delay.js is executed.
6797TEST(NoDebugBreakInAfterCompileMessageHandler) {
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006798 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006799 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006800
6801 // Register a debug event listener which sets the break flag and counts.
6802 v8::Debug::SetMessageHandler2(BreakMessageHandler);
6803
6804 // Set the debug break flag.
6805 v8::Debug::DebugBreak();
6806
6807 // Create a function for testing stepping.
6808 const char* src = "function f() { eval('var x = 10;'); } ";
6809 v8::Local<v8::Function> f = CompileFunction(&env, src, "f");
6810
6811 // There should be only one break event.
6812 CHECK_EQ(1, break_point_hit_count);
6813
6814 // Set the debug break flag again.
6815 v8::Debug::DebugBreak();
6816 f->Call(env->Global(), 0, NULL);
6817 // There should be one more break event when the script is evaluated in 'f'.
6818 CHECK_EQ(2, break_point_hit_count);
6819
6820 // Get rid of the debug message handler.
6821 v8::Debug::SetMessageHandler2(NULL);
6822 CheckDebuggerUnloaded();
6823}
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006824
6825
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006826static int counting_message_handler_counter;
6827
6828static void CountingMessageHandler(const v8::Debug::Message& message) {
6829 counting_message_handler_counter++;
6830}
6831
6832// Test that debug messages get processed when ProcessDebugMessages is called.
6833TEST(ProcessDebugMessages) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006834 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006835 v8::HandleScope scope(env->GetIsolate());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006836
6837 counting_message_handler_counter = 0;
6838
6839 v8::Debug::SetMessageHandler2(CountingMessageHandler);
6840
6841 const int kBufferSize = 1000;
6842 uint16_t buffer[kBufferSize];
6843 const char* scripts_command =
6844 "{\"seq\":0,"
6845 "\"type\":\"request\","
6846 "\"command\":\"scripts\"}";
6847
6848 // Send scripts command.
6849 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer));
6850
6851 CHECK_EQ(0, counting_message_handler_counter);
6852 v8::Debug::ProcessDebugMessages();
6853 // At least one message should come
6854 CHECK_GE(counting_message_handler_counter, 1);
6855
6856 counting_message_handler_counter = 0;
6857
6858 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer));
6859 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer));
6860 CHECK_EQ(0, counting_message_handler_counter);
6861 v8::Debug::ProcessDebugMessages();
6862 // At least two messages should come
6863 CHECK_GE(counting_message_handler_counter, 2);
6864
6865 // Get rid of the debug message handler.
6866 v8::Debug::SetMessageHandler2(NULL);
6867 CheckDebuggerUnloaded();
6868}
6869
6870
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006871struct BacktraceData {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006872 static int frame_counter;
6873 static void MessageHandler(const v8::Debug::Message& message) {
6874 char print_buffer[1000];
6875 v8::String::Value json(message.GetJSON());
6876 Utf16ToAscii(*json, json.length(), print_buffer, 1000);
6877
6878 if (strstr(print_buffer, "backtrace") == NULL) {
6879 return;
6880 }
6881 frame_counter = GetTotalFramesInt(print_buffer);
6882 }
6883};
6884
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006885int BacktraceData::frame_counter;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006886
6887
6888// Test that debug messages get processed when ProcessDebugMessages is called.
6889TEST(Backtrace) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006890 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006891 v8::HandleScope scope(env->GetIsolate());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006892
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006893 v8::Debug::SetMessageHandler2(BacktraceData::MessageHandler);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006894
6895 const int kBufferSize = 1000;
6896 uint16_t buffer[kBufferSize];
6897 const char* scripts_command =
6898 "{\"seq\":0,"
6899 "\"type\":\"request\","
6900 "\"command\":\"backtrace\"}";
6901
6902 // Check backtrace from ProcessDebugMessages.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006903 BacktraceData::frame_counter = -10;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006904 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer));
6905 v8::Debug::ProcessDebugMessages();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006906 CHECK_EQ(BacktraceData::frame_counter, 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006907
6908 v8::Handle<v8::String> void0 = v8::String::New("void(0)");
6909 v8::Handle<v8::Script> script = v8::Script::Compile(void0, void0);
6910
6911 // Check backtrace from "void(0)" script.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006912 BacktraceData::frame_counter = -10;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006913 v8::Debug::SendCommand(buffer, AsciiToUtf16(scripts_command, buffer));
6914 script->Run();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006915 CHECK_EQ(BacktraceData::frame_counter, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006916
6917 // Get rid of the debug message handler.
6918 v8::Debug::SetMessageHandler2(NULL);
6919 CheckDebuggerUnloaded();
6920}
6921
6922
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006923TEST(GetMirror) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006924 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006925 v8::HandleScope scope(env->GetIsolate());
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006926 v8::Handle<v8::Value> obj = v8::Debug::GetMirror(v8::String::New("hodja"));
6927 v8::Handle<v8::Function> run_test = v8::Handle<v8::Function>::Cast(
6928 v8::Script::New(
6929 v8::String::New(
6930 "function runTest(mirror) {"
6931 " return mirror.isString() && (mirror.length() == 5);"
6932 "}"
6933 ""
6934 "runTest;"))->Run());
6935 v8::Handle<v8::Value> result = run_test->Call(env->Global(), 1, &obj);
6936 CHECK(result->IsTrue());
6937}
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006938
6939
6940// Test that the debug break flag works with function.apply.
6941TEST(DebugBreakFunctionApply) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006942 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00006943 v8::HandleScope scope(env->GetIsolate());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006944
6945 // Create a function for testing breaking in apply.
6946 v8::Local<v8::Function> foo = CompileFunction(
6947 &env,
6948 "function baz(x) { }"
6949 "function bar(x) { baz(); }"
6950 "function foo(){ bar.apply(this, [1]); }",
6951 "foo");
6952
6953 // Register a debug event listener which steps and counts.
6954 v8::Debug::SetDebugEventListener(DebugEventBreakMax);
6955
6956 // Set the debug break flag before calling the code using function.apply.
6957 v8::Debug::DebugBreak();
6958
6959 // Limit the number of debug breaks. This is a regression test for issue 493
6960 // where this test would enter an infinite loop.
6961 break_point_hit_count = 0;
6962 max_break_point_hit_count = 10000; // 10000 => infinite loop.
6963 foo->Call(env->Global(), 0, NULL);
6964
6965 // When keeping the debug break several break will happen.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00006966 CHECK_GT(break_point_hit_count, 1);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006967
6968 v8::Debug::SetDebugEventListener(NULL);
6969 CheckDebuggerUnloaded();
6970}
6971
6972
6973v8::Handle<v8::Context> debugee_context;
6974v8::Handle<v8::Context> debugger_context;
6975
6976
6977// Property getter that checks that current and calling contexts
6978// are both the debugee contexts.
6979static v8::Handle<v8::Value> NamedGetterWithCallingContextCheck(
6980 v8::Local<v8::String> name,
6981 const v8::AccessorInfo& info) {
6982 CHECK_EQ(0, strcmp(*v8::String::AsciiValue(name), "a"));
6983 v8::Handle<v8::Context> current = v8::Context::GetCurrent();
6984 CHECK(current == debugee_context);
6985 CHECK(current != debugger_context);
6986 v8::Handle<v8::Context> calling = v8::Context::GetCalling();
6987 CHECK(calling == debugee_context);
6988 CHECK(calling != debugger_context);
6989 return v8::Int32::New(1);
6990}
6991
6992
6993// Debug event listener that checks if the first argument of a function is
6994// an object with property 'a' == 1. If the property has custom accessor
6995// this handler will eventually invoke it.
6996static void DebugEventGetAtgumentPropertyValue(
6997 v8::DebugEvent event,
6998 v8::Handle<v8::Object> exec_state,
6999 v8::Handle<v8::Object> event_data,
7000 v8::Handle<v8::Value> data) {
7001 if (event == v8::Break) {
7002 break_point_hit_count++;
7003 CHECK(debugger_context == v8::Context::GetCurrent());
7004 v8::Handle<v8::Function> func(v8::Function::Cast(*CompileRun(
7005 "(function(exec_state) {\n"
7006 " return (exec_state.frame(0).argumentValue(0).property('a').\n"
7007 " value().value() == 1);\n"
7008 "})")));
7009 const int argc = 1;
7010 v8::Handle<v8::Value> argv[argc] = { exec_state };
7011 v8::Handle<v8::Value> result = func->Call(exec_state, argc, argv);
7012 CHECK(result->IsTrue());
7013 }
7014}
7015
7016
7017TEST(CallingContextIsNotDebugContext) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00007018 v8::internal::Debug* debug = v8::internal::Isolate::Current()->debug();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007019 // Create and enter a debugee context.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007020 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007021 v8::HandleScope scope(env->GetIsolate());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007022 env.ExposeDebug();
7023
7024 // Save handles to the debugger and debugee contexts to be used in
7025 // NamedGetterWithCallingContextCheck.
7026 debugee_context = v8::Local<v8::Context>(*env);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00007027 debugger_context = v8::Utils::ToLocal(debug->debug_context());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007028
7029 // Create object with 'a' property accessor.
7030 v8::Handle<v8::ObjectTemplate> named = v8::ObjectTemplate::New();
7031 named->SetAccessor(v8::String::New("a"),
7032 NamedGetterWithCallingContextCheck);
7033 env->Global()->Set(v8::String::New("obj"),
7034 named->NewInstance());
7035
7036 // Register the debug event listener
7037 v8::Debug::SetDebugEventListener(DebugEventGetAtgumentPropertyValue);
7038
7039 // Create a function that invokes debugger.
7040 v8::Local<v8::Function> foo = CompileFunction(
7041 &env,
7042 "function bar(x) { debugger; }"
7043 "function foo(){ bar(obj); }",
7044 "foo");
7045
7046 break_point_hit_count = 0;
7047 foo->Call(env->Global(), 0, NULL);
7048 CHECK_EQ(1, break_point_hit_count);
7049
7050 v8::Debug::SetDebugEventListener(NULL);
7051 debugee_context = v8::Handle<v8::Context>();
7052 debugger_context = v8::Handle<v8::Context>();
7053 CheckDebuggerUnloaded();
7054}
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007055
7056
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00007057TEST(DebugContextIsPreservedBetweenAccesses) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007058 v8::HandleScope scope(v8::Isolate::GetCurrent());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00007059 v8::Local<v8::Context> context1 = v8::Debug::GetDebugContext();
7060 v8::Local<v8::Context> context2 = v8::Debug::GetDebugContext();
7061 CHECK_EQ(*context1, *context2);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007062}
7063
7064
7065static v8::Handle<v8::Value> expected_callback_data;
7066static void DebugEventContextChecker(const v8::Debug::EventDetails& details) {
7067 CHECK(details.GetEventContext() == expected_context);
7068 CHECK_EQ(expected_callback_data, details.GetCallbackData());
7069}
7070
7071// Check that event details contain context where debug event occured.
7072TEST(DebugEventContext) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007073 v8::HandleScope scope(v8::Isolate::GetCurrent());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007074 expected_callback_data = v8::Int32::New(2010);
7075 v8::Debug::SetDebugEventListener2(DebugEventContextChecker,
7076 expected_callback_data);
7077 expected_context = v8::Context::New();
7078 v8::Context::Scope context_scope(expected_context);
7079 v8::Script::Compile(v8::String::New("(function(){debugger;})();"))->Run();
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00007080 expected_context.Dispose(expected_context->GetIsolate());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007081 expected_context.Clear();
7082 v8::Debug::SetDebugEventListener(NULL);
7083 expected_context_data = v8::Handle<v8::Value>();
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00007084 CheckDebuggerUnloaded();
7085}
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007086
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00007087
7088static void* expected_break_data;
7089static bool was_debug_break_called;
7090static bool was_debug_event_called;
7091static void DebugEventBreakDataChecker(const v8::Debug::EventDetails& details) {
7092 if (details.GetEvent() == v8::BreakForCommand) {
7093 CHECK_EQ(expected_break_data, details.GetClientData());
7094 was_debug_event_called = true;
7095 } else if (details.GetEvent() == v8::Break) {
7096 was_debug_break_called = true;
7097 }
7098}
7099
kasperl@chromium.orga5551262010-12-07 12:49:48 +00007100
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00007101// Check that event details contain context where debug event occured.
7102TEST(DebugEventBreakData) {
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00007103 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007104 v8::HandleScope scope(env->GetIsolate());
mikhail.naganov@gmail.com22762872010-07-14 09:29:05 +00007105 v8::Debug::SetDebugEventListener2(DebugEventBreakDataChecker);
7106
7107 TestClientData::constructor_call_counter = 0;
7108 TestClientData::destructor_call_counter = 0;
7109
7110 expected_break_data = NULL;
7111 was_debug_event_called = false;
7112 was_debug_break_called = false;
7113 v8::Debug::DebugBreakForCommand();
7114 v8::Script::Compile(v8::String::New("(function(x){return x;})(1);"))->Run();
7115 CHECK(was_debug_event_called);
7116 CHECK(!was_debug_break_called);
7117
7118 TestClientData* data1 = new TestClientData();
7119 expected_break_data = data1;
7120 was_debug_event_called = false;
7121 was_debug_break_called = false;
7122 v8::Debug::DebugBreakForCommand(data1);
7123 v8::Script::Compile(v8::String::New("(function(x){return x+1;})(1);"))->Run();
7124 CHECK(was_debug_event_called);
7125 CHECK(!was_debug_break_called);
7126
7127 expected_break_data = NULL;
7128 was_debug_event_called = false;
7129 was_debug_break_called = false;
7130 v8::Debug::DebugBreak();
7131 v8::Script::Compile(v8::String::New("(function(x){return x+2;})(1);"))->Run();
7132 CHECK(!was_debug_event_called);
7133 CHECK(was_debug_break_called);
7134
7135 TestClientData* data2 = new TestClientData();
7136 expected_break_data = data2;
7137 was_debug_event_called = false;
7138 was_debug_break_called = false;
7139 v8::Debug::DebugBreak();
7140 v8::Debug::DebugBreakForCommand(data2);
7141 v8::Script::Compile(v8::String::New("(function(x){return x+3;})(1);"))->Run();
7142 CHECK(was_debug_event_called);
7143 CHECK(was_debug_break_called);
7144
7145 CHECK_EQ(2, TestClientData::constructor_call_counter);
7146 CHECK_EQ(TestClientData::constructor_call_counter,
7147 TestClientData::destructor_call_counter);
7148
7149 v8::Debug::SetDebugEventListener(NULL);
7150 CheckDebuggerUnloaded();
7151}
7152
kasperl@chromium.orga5551262010-12-07 12:49:48 +00007153static bool debug_event_break_deoptimize_done = false;
7154
7155static void DebugEventBreakDeoptimize(v8::DebugEvent event,
7156 v8::Handle<v8::Object> exec_state,
7157 v8::Handle<v8::Object> event_data,
7158 v8::Handle<v8::Value> data) {
7159 if (event == v8::Break) {
7160 if (!frame_function_name.IsEmpty()) {
7161 // Get the name of the function.
7162 const int argc = 2;
7163 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(0) };
7164 v8::Handle<v8::Value> result =
7165 frame_function_name->Call(exec_state, argc, argv);
7166 if (!result->IsUndefined()) {
7167 char fn[80];
7168 CHECK(result->IsString());
7169 v8::Handle<v8::String> function_name(result->ToString());
7170 function_name->WriteAscii(fn);
7171 if (strcmp(fn, "bar") == 0) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00007172 i::Deoptimizer::DeoptimizeAll(v8::internal::Isolate::Current());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00007173 debug_event_break_deoptimize_done = true;
7174 }
7175 }
7176 }
7177
7178 v8::Debug::DebugBreak();
7179 }
7180}
7181
7182
7183// Test deoptimization when execution is broken using the debug break stack
7184// check interrupt.
7185TEST(DeoptimizeDuringDebugBreak) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00007186 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007187 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00007188 env.ExposeDebug();
7189
7190 // Create a function for checking the function when hitting a break point.
7191 frame_function_name = CompileFunction(&env,
7192 frame_function_name_source,
7193 "frame_function_name");
7194
7195
7196 // Set a debug event listener which will keep interrupting execution until
7197 // debug break. When inside function bar it will deoptimize all functions.
7198 // This tests lazy deoptimization bailout for the stack check, as the first
7199 // time in function bar when using debug break and no break points will be at
7200 // the initial stack check.
7201 v8::Debug::SetDebugEventListener(DebugEventBreakDeoptimize,
7202 v8::Undefined());
7203
7204 // Compile and run function bar which will optimize it for some flag settings.
7205 v8::Script::Compile(v8::String::New("function bar(){}; bar()"))->Run();
7206
7207 // Set debug break and call bar again.
7208 v8::Debug::DebugBreak();
7209 v8::Script::Compile(v8::String::New("bar()"))->Run();
7210
7211 CHECK(debug_event_break_deoptimize_done);
7212
7213 v8::Debug::SetDebugEventListener(NULL);
7214}
7215
7216
7217static void DebugEventBreakWithOptimizedStack(v8::DebugEvent event,
7218 v8::Handle<v8::Object> exec_state,
7219 v8::Handle<v8::Object> event_data,
7220 v8::Handle<v8::Value> data) {
7221 if (event == v8::Break) {
7222 if (!frame_function_name.IsEmpty()) {
7223 for (int i = 0; i < 2; i++) {
7224 const int argc = 2;
7225 v8::Handle<v8::Value> argv[argc] = { exec_state, v8::Integer::New(i) };
7226 // Get the name of the function in frame i.
7227 v8::Handle<v8::Value> result =
7228 frame_function_name->Call(exec_state, argc, argv);
7229 CHECK(result->IsString());
7230 v8::Handle<v8::String> function_name(result->ToString());
7231 CHECK(function_name->Equals(v8::String::New("loop")));
7232 // Get the name of the first argument in frame i.
7233 result = frame_argument_name->Call(exec_state, argc, argv);
7234 CHECK(result->IsString());
7235 v8::Handle<v8::String> argument_name(result->ToString());
7236 CHECK(argument_name->Equals(v8::String::New("count")));
7237 // Get the value of the first argument in frame i. If the
7238 // funtion is optimized the value will be undefined, otherwise
7239 // the value will be '1 - i'.
7240 //
7241 // TODO(3141533): We should be able to get the real value for
7242 // optimized frames.
7243 result = frame_argument_value->Call(exec_state, argc, argv);
7244 CHECK(result->IsUndefined() || (result->Int32Value() == 1 - i));
7245 // Get the name of the first local variable.
7246 result = frame_local_name->Call(exec_state, argc, argv);
7247 CHECK(result->IsString());
7248 v8::Handle<v8::String> local_name(result->ToString());
7249 CHECK(local_name->Equals(v8::String::New("local")));
7250 // Get the value of the first local variable. If the function
7251 // is optimized the value will be undefined, otherwise it will
7252 // be 42.
7253 //
7254 // TODO(3141533): We should be able to get the real value for
7255 // optimized frames.
7256 result = frame_local_value->Call(exec_state, argc, argv);
7257 CHECK(result->IsUndefined() || (result->Int32Value() == 42));
7258 }
7259 }
7260 }
7261}
7262
7263
7264static v8::Handle<v8::Value> ScheduleBreak(const v8::Arguments& args) {
7265 v8::Debug::SetDebugEventListener(DebugEventBreakWithOptimizedStack,
7266 v8::Undefined());
7267 v8::Debug::DebugBreak();
7268 return v8::Undefined();
7269}
7270
7271
7272TEST(DebugBreakStackInspection) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00007273 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007274 v8::HandleScope scope(env->GetIsolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00007275
7276 frame_function_name =
7277 CompileFunction(&env, frame_function_name_source, "frame_function_name");
7278 frame_argument_name =
7279 CompileFunction(&env, frame_argument_name_source, "frame_argument_name");
7280 frame_argument_value = CompileFunction(&env,
7281 frame_argument_value_source,
7282 "frame_argument_value");
7283 frame_local_name =
7284 CompileFunction(&env, frame_local_name_source, "frame_local_name");
7285 frame_local_value =
7286 CompileFunction(&env, frame_local_value_source, "frame_local_value");
7287
7288 v8::Handle<v8::FunctionTemplate> schedule_break_template =
7289 v8::FunctionTemplate::New(ScheduleBreak);
7290 v8::Handle<v8::Function> schedule_break =
7291 schedule_break_template->GetFunction();
7292 env->Global()->Set(v8_str("scheduleBreak"), schedule_break);
7293
7294 const char* src =
7295 "function loop(count) {"
7296 " var local = 42;"
7297 " if (count < 1) { scheduleBreak(); loop(count + 1); }"
7298 "}"
7299 "loop(0);";
7300 v8::Script::Compile(v8::String::New(src))->Run();
7301}
7302
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007303
7304// Test that setting the terminate execution flag during debug break processing.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007305static void TestDebugBreakInLoop(const char* loop_head,
7306 const char** loop_bodies,
7307 const char* loop_tail) {
7308 // Receive 100 breaks for each test and then terminate JavaScript execution.
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00007309 static const int kBreaksPerTest = 100;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007310
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00007311 for (int i = 0; loop_bodies[i] != NULL; i++) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00007312 // Perform a lazy deoptimization after various numbers of breaks
7313 // have been hit.
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00007314 for (int j = 0; j < 11; j++) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00007315 break_point_hit_count_deoptimize = j;
7316 if (j == 10) {
7317 break_point_hit_count_deoptimize = kBreaksPerTest;
7318 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007319
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00007320 break_point_hit_count = 0;
7321 max_break_point_hit_count = kBreaksPerTest;
7322 terminate_after_max_break_point_hit = true;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007323
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00007324 EmbeddedVector<char, 1024> buffer;
7325 OS::SNPrintF(buffer,
7326 "function f() {%s%s%s}",
7327 loop_head, loop_bodies[i], loop_tail);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007328
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00007329 // Function with infinite loop.
7330 CompileRun(buffer.start());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007331
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00007332 // Set the debug break to enter the debugger as soon as possible.
7333 v8::Debug::DebugBreak();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007334
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00007335 // Call function with infinite loop.
7336 CompileRun("f();");
7337 CHECK_EQ(kBreaksPerTest, break_point_hit_count);
7338
7339 CHECK(!v8::V8::IsExecutionTerminating());
7340 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007341 }
7342}
7343
7344
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007345TEST(DebugBreakLoop) {
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007346 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007347 v8::HandleScope scope(env->GetIsolate());
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007348
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007349 // Register a debug event listener which sets the break flag and counts.
7350 v8::Debug::SetDebugEventListener(DebugEventBreakMax);
7351
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00007352 // Create a function for getting the frame count when hitting the break.
7353 frame_count = CompileFunction(&env, frame_count_source, "frame_count");
7354
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007355 CompileRun("var a = 1;");
7356 CompileRun("function g() { }");
7357 CompileRun("function h() { }");
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007358
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007359 const char* loop_bodies[] = {
7360 "",
7361 "g()",
7362 "if (a == 0) { g() }",
7363 "if (a == 1) { g() }",
7364 "if (a == 0) { g() } else { h() }",
7365 "if (a == 0) { continue }",
7366 "if (a == 1) { continue }",
7367 "switch (a) { case 1: g(); }",
7368 "switch (a) { case 1: continue; }",
7369 "switch (a) { case 1: g(); break; default: h() }",
7370 "switch (a) { case 1: continue; break; default: h() }",
7371 NULL
7372 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007373
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00007374 TestDebugBreakInLoop("while (true) {", loop_bodies, "}");
7375 TestDebugBreakInLoop("while (a == 1) {", loop_bodies, "}");
7376
7377 TestDebugBreakInLoop("do {", loop_bodies, "} while (true)");
7378 TestDebugBreakInLoop("do {", loop_bodies, "} while (a == 1)");
7379
7380 TestDebugBreakInLoop("for (;;) {", loop_bodies, "}");
7381 TestDebugBreakInLoop("for (;a == 1;) {", loop_bodies, "}");
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00007382
7383 // Get rid of the debug event listener.
7384 v8::Debug::SetDebugEventListener(NULL);
7385 CheckDebuggerUnloaded();
7386}
7387
7388
danno@chromium.orgfa458e42012-02-01 10:48:36 +00007389v8::Local<v8::Script> inline_script;
7390
7391static void DebugBreakInlineListener(v8::DebugEvent event,
7392 v8::Handle<v8::Object> exec_state,
7393 v8::Handle<v8::Object> event_data,
7394 v8::Handle<v8::Value> data) {
7395 if (event != v8::Break) return;
7396
7397 int expected_frame_count = 4;
7398 int expected_line_number[] = {1, 4, 7, 12};
7399
7400 i::Handle<i::Object> compiled_script = v8::Utils::OpenHandle(*inline_script);
7401 i::Handle<i::Script> source_script = i::Handle<i::Script>(i::Script::cast(
7402 i::JSFunction::cast(*compiled_script)->shared()->script()));
7403
7404 int break_id = v8::internal::Isolate::Current()->debug()->break_id();
7405 char script[128];
7406 i::Vector<char> script_vector(script, sizeof(script));
7407 OS::SNPrintF(script_vector, "%%GetFrameCount(%d)", break_id);
7408 v8::Local<v8::Value> result = CompileRun(script);
7409
7410 int frame_count = result->Int32Value();
7411 CHECK_EQ(expected_frame_count, frame_count);
7412
7413 for (int i = 0; i < frame_count; i++) {
7414 // The 5. element in the returned array of GetFrameDetails contains the
7415 // source position of that frame.
7416 OS::SNPrintF(script_vector, "%%GetFrameDetails(%d, %d)[5]", break_id, i);
7417 v8::Local<v8::Value> result = CompileRun(script);
7418 CHECK_EQ(expected_line_number[i],
7419 i::GetScriptLineNumber(source_script, result->Int32Value()));
7420 }
7421 v8::Debug::SetDebugEventListener(NULL);
7422 v8::V8::TerminateExecution();
7423}
7424
7425
7426TEST(DebugBreakInline) {
7427 i::FLAG_allow_natives_syntax = true;
danno@chromium.orgfa458e42012-02-01 10:48:36 +00007428 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007429 v8::HandleScope scope(env->GetIsolate());
danno@chromium.orgfa458e42012-02-01 10:48:36 +00007430 const char* source =
7431 "function debug(b) { \n"
7432 " if (b) debugger; \n"
7433 "} \n"
7434 "function f(b) { \n"
7435 " debug(b) \n"
7436 "}; \n"
7437 "function g(b) { \n"
7438 " f(b); \n"
7439 "}; \n"
7440 "g(false); \n"
7441 "g(false); \n"
7442 "%OptimizeFunctionOnNextCall(g); \n"
7443 "g(true);";
7444 v8::Debug::SetDebugEventListener(DebugBreakInlineListener);
7445 inline_script = v8::Script::Compile(v8::String::New(source));
7446 inline_script->Run();
7447}
7448
7449
danno@chromium.org81cac2b2012-07-10 11:28:27 +00007450static void DebugEventStepNext(v8::DebugEvent event,
7451 v8::Handle<v8::Object> exec_state,
7452 v8::Handle<v8::Object> event_data,
7453 v8::Handle<v8::Value> data) {
7454 if (event == v8::Break) {
7455 PrepareStep(StepNext);
7456 }
7457}
7458
7459
7460static void RunScriptInANewCFrame(const char* source) {
7461 v8::TryCatch try_catch;
7462 CompileRun(source);
7463 CHECK(try_catch.HasCaught());
7464}
7465
7466
7467TEST(Regress131642) {
7468 // Bug description:
7469 // When doing StepNext through the first script, the debugger is not reset
7470 // after exiting through exception. A flawed implementation enabling the
7471 // debugger to step into Array.prototype.forEach breaks inside the callback
7472 // for forEach in the second script under the assumption that we are in a
7473 // recursive call. In an attempt to step out, we crawl the stack using the
7474 // recorded frame pointer from the first script and fail when not finding it
7475 // on the stack.
danno@chromium.org81cac2b2012-07-10 11:28:27 +00007476 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007477 v8::HandleScope scope(env->GetIsolate());
danno@chromium.org81cac2b2012-07-10 11:28:27 +00007478 v8::Debug::SetDebugEventListener(DebugEventStepNext);
7479
7480 // We step through the first script. It exits through an exception. We run
7481 // this inside a new frame to record a different FP than the second script
7482 // would expect.
7483 const char* script_1 = "debugger; throw new Error();";
7484 RunScriptInANewCFrame(script_1);
7485
7486 // The second script uses forEach.
7487 const char* script_2 = "[0].forEach(function() { });";
7488 CompileRun(script_2);
7489
7490 v8::Debug::SetDebugEventListener(NULL);
7491}
7492
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00007493
7494// Import from test-heap.cc
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00007495int CountNativeContexts();
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00007496
7497
7498static void NopListener(v8::DebugEvent event,
7499 v8::Handle<v8::Object> exec_state,
7500 v8::Handle<v8::Object> event_data,
7501 v8::Handle<v8::Value> data) {
7502}
7503
7504
7505TEST(DebuggerCreatesContextIffActive) {
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00007506 DebugLocalContext env;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007507 v8::HandleScope scope(env->GetIsolate());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00007508 CHECK_EQ(1, CountNativeContexts());
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00007509
7510 v8::Debug::SetDebugEventListener(NULL);
7511 CompileRun("debugger;");
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00007512 CHECK_EQ(1, CountNativeContexts());
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00007513
7514 v8::Debug::SetDebugEventListener(NopListener);
7515 CompileRun("debugger;");
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00007516 CHECK_EQ(2, CountNativeContexts());
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00007517
7518 v8::Debug::SetDebugEventListener(NULL);
7519}
7520
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00007521
7522TEST(LiveEditEnabled) {
7523 v8::internal::FLAG_allow_natives_syntax = true;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007524 LocalContext env;
7525 v8::HandleScope scope(env->GetIsolate());
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00007526 v8::Debug::SetLiveEditEnabled(true);
7527 CompileRun("%LiveEditCompareStrings('', '')");
7528}
7529
7530
7531TEST(LiveEditDisabled) {
7532 v8::internal::FLAG_allow_natives_syntax = true;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00007533 LocalContext env;
7534 v8::HandleScope scope(env->GetIsolate());
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00007535 v8::Debug::SetLiveEditEnabled(false);
7536 CompileRun("%LiveEditCompareStrings('', '')");
7537}
7538
7539
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007540#endif // ENABLE_DEBUGGER_SUPPORT