forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-debug.cc
6727 lines (5593 loc) · 237 KB
/
test-debug.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdlib.h>
#include <set>
#include "src/v8.h"
#include "src/api.h"
#include "src/compilation-cache.h"
#include "src/debug/debug-interface.h"
#include "src/debug/debug.h"
#include "src/deoptimizer.h"
#include "src/frames.h"
#include "src/objects-inl.h"
#include "src/utils.h"
#include "test/cctest/cctest.h"
using ::v8::internal::EmbeddedVector;
using ::v8::internal::Object;
using ::v8::internal::Handle;
using ::v8::internal::Heap;
using ::v8::internal::JSGlobalProxy;
using ::v8::internal::Code;
using ::v8::internal::Debug;
using ::v8::internal::StackFrame;
using ::v8::internal::StepAction;
using ::v8::internal::StepIn; // From StepAction enum
using ::v8::internal::StepNext; // From StepAction enum
using ::v8::internal::StepOut; // From StepAction enum
using ::v8::internal::Vector;
using ::v8::internal::StrLength;
// Size of temp buffer for formatting small strings.
#define SMALL_STRING_BUFFER_SIZE 80
// --- H e l p e r C l a s s e s
// Helper class for creating a V8 enviromnent for running tests
class DebugLocalContext {
public:
inline DebugLocalContext(
v8::Isolate* isolate, v8::ExtensionConfiguration* extensions = 0,
v8::Local<v8::ObjectTemplate> global_template =
v8::Local<v8::ObjectTemplate>(),
v8::Local<v8::Value> global_object = v8::Local<v8::Value>())
: scope_(isolate),
context_(v8::Context::New(isolate, extensions, global_template,
global_object)) {
context_->Enter();
}
inline DebugLocalContext(
v8::ExtensionConfiguration* extensions = 0,
v8::Local<v8::ObjectTemplate> global_template =
v8::Local<v8::ObjectTemplate>(),
v8::Local<v8::Value> global_object = v8::Local<v8::Value>())
: scope_(CcTest::isolate()),
context_(v8::Context::New(CcTest::isolate(), extensions,
global_template, global_object)) {
context_->Enter();
}
inline ~DebugLocalContext() {
context_->Exit();
}
inline v8::Local<v8::Context> context() { return context_; }
inline v8::Context* operator->() { return *context_; }
inline v8::Context* operator*() { return *context_; }
inline v8::Isolate* GetIsolate() { return context_->GetIsolate(); }
inline bool IsReady() { return !context_.IsEmpty(); }
void ExposeDebug() {
v8::internal::Isolate* isolate =
reinterpret_cast<v8::internal::Isolate*>(context_->GetIsolate());
v8::internal::Factory* factory = isolate->factory();
// Expose the debug context global object in the global object for testing.
CHECK(isolate->debug()->Load());
Handle<v8::internal::Context> debug_context =
isolate->debug()->debug_context();
debug_context->set_security_token(
v8::Utils::OpenHandle(*context_)->security_token());
Handle<JSGlobalProxy> global(Handle<JSGlobalProxy>::cast(
v8::Utils::OpenHandle(*context_->Global())));
Handle<v8::internal::String> debug_string =
factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("debug"));
v8::internal::JSObject::SetOwnPropertyIgnoreAttributes(
global, debug_string, handle(debug_context->global_proxy()),
v8::internal::DONT_ENUM)
.Check();
}
private:
v8::HandleScope scope_;
v8::Local<v8::Context> context_;
};
// --- H e l p e r F u n c t i o n s
// Compile and run the supplied source and return the requested function.
static v8::Local<v8::Function> CompileFunction(v8::Isolate* isolate,
const char* source,
const char* function_name) {
CompileRunChecked(isolate, source);
v8::Local<v8::String> name = v8_str(isolate, function_name);
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::MaybeLocal<v8::Value> maybe_function =
context->Global()->Get(context, name);
return v8::Local<v8::Function>::Cast(maybe_function.ToLocalChecked());
}
// Compile and run the supplied source and return the requested function.
static v8::Local<v8::Function> CompileFunction(DebugLocalContext* env,
const char* source,
const char* function_name) {
return CompileFunction(env->GetIsolate(), source, function_name);
}
static void SetDebugEventListener(
v8::Isolate* isolate, v8::Debug::EventCallback that,
v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
i::HandleScope scope(i_isolate);
if (that == nullptr) {
i_isolate->debug()->SetDebugDelegate(nullptr, false);
} else {
i::Handle<i::Object> i_data = i_isolate->factory()->undefined_value();
if (!data.IsEmpty()) i_data = v8::Utils::OpenHandle(*data);
i::NativeDebugDelegate* delegate =
new i::NativeDebugDelegate(i_isolate, that, i_data);
i_isolate->debug()->SetDebugDelegate(delegate, true);
}
}
// Is there any debug info for the function?
static bool HasBreakInfo(v8::Local<v8::Function> fun) {
Handle<v8::internal::JSFunction> f =
Handle<v8::internal::JSFunction>::cast(v8::Utils::OpenHandle(*fun));
Handle<v8::internal::SharedFunctionInfo> shared(f->shared());
return shared->HasBreakInfo();
}
// Set a break point in a function with a position relative to function start,
// and return the associated break point number.
static int SetBreakPoint(v8::Local<v8::Function> fun, int position) {
i::Handle<i::JSFunction> function =
i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*fun));
position += function->shared()->start_position();
static int break_point = 0;
v8::internal::Isolate* isolate = function->GetIsolate();
v8::internal::Debug* debug = isolate->debug();
debug->SetBreakPoint(
function,
Handle<Object>(v8::internal::Smi::FromInt(++break_point), isolate),
&position);
return break_point;
}
// Set a break point in a function using the Debug object and return the
// associated break point number.
static int SetBreakPointFromJS(v8::Isolate* isolate,
const char* function_name,
int line, int position) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
SNPrintF(buffer,
"debug.Debug.setBreakPoint(%s,%d,%d)",
function_name, line, position);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
v8::Local<v8::Value> value = CompileRunChecked(isolate, buffer.start());
return value->Int32Value(isolate->GetCurrentContext()).FromJust();
}
// Set a break point in a script identified by id using the global Debug object.
static int SetScriptBreakPointByIdFromJS(v8::Isolate* isolate, int script_id,
int line, int column) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
if (column >= 0) {
// Column specified set script break point on precise location.
SNPrintF(buffer,
"debug.Debug.setScriptBreakPointById(%d,%d,%d)",
script_id, line, column);
} else {
// Column not specified set script break point on line.
SNPrintF(buffer,
"debug.Debug.setScriptBreakPointById(%d,%d)",
script_id, line);
}
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
{
v8::TryCatch try_catch(isolate);
v8::Local<v8::Value> value = CompileRunChecked(isolate, buffer.start());
CHECK(!try_catch.HasCaught());
return value->Int32Value(isolate->GetCurrentContext()).FromJust();
}
}
// Set a break point in a script identified by name using the global Debug
// object.
static int SetScriptBreakPointByNameFromJS(v8::Isolate* isolate,
const char* script_name, int line,
int column) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
if (column >= 0) {
// Column specified set script break point on precise location.
SNPrintF(buffer,
"debug.Debug.setScriptBreakPointByName(\"%s\",%d,%d)",
script_name, line, column);
} else {
// Column not specified set script break point on line.
SNPrintF(buffer,
"debug.Debug.setScriptBreakPointByName(\"%s\",%d)",
script_name, line);
}
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
{
v8::TryCatch try_catch(isolate);
v8::Local<v8::Value> value = CompileRunChecked(isolate, buffer.start());
CHECK(!try_catch.HasCaught());
return value->Int32Value(isolate->GetCurrentContext()).FromJust();
}
}
// Clear a break point.
static void ClearBreakPoint(int break_point) {
v8::internal::Isolate* isolate = CcTest::i_isolate();
v8::internal::Debug* debug = isolate->debug();
debug->ClearBreakPoint(
Handle<Object>(v8::internal::Smi::FromInt(break_point), isolate));
}
// Clear a break point using the global Debug object.
static void ClearBreakPointFromJS(v8::Isolate* isolate,
int break_point_number) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
SNPrintF(buffer,
"debug.Debug.clearBreakPoint(%d)",
break_point_number);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
CompileRunChecked(isolate, buffer.start());
}
static void EnableScriptBreakPointFromJS(v8::Isolate* isolate,
int break_point_number) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
SNPrintF(buffer,
"debug.Debug.enableScriptBreakPoint(%d)",
break_point_number);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
CompileRunChecked(isolate, buffer.start());
}
static void DisableScriptBreakPointFromJS(v8::Isolate* isolate,
int break_point_number) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
SNPrintF(buffer,
"debug.Debug.disableScriptBreakPoint(%d)",
break_point_number);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
CompileRunChecked(isolate, buffer.start());
}
static void ChangeScriptBreakPointConditionFromJS(v8::Isolate* isolate,
int break_point_number,
const char* condition) {
EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> buffer;
SNPrintF(buffer,
"debug.Debug.changeScriptBreakPointCondition(%d, \"%s\")",
break_point_number, condition);
buffer[SMALL_STRING_BUFFER_SIZE - 1] = '\0';
CompileRunChecked(isolate, buffer.start());
}
// Change break on exception.
static void ChangeBreakOnException(bool caught, bool uncaught) {
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
debug->ChangeBreakOnException(v8::internal::BreakException, caught);
debug->ChangeBreakOnException(v8::internal::BreakUncaughtException, uncaught);
}
// Change break on exception using the global Debug object.
static void ChangeBreakOnExceptionFromJS(v8::Isolate* isolate, bool caught,
bool uncaught) {
if (caught) {
CompileRunChecked(isolate, "debug.Debug.setBreakOnException()");
} else {
CompileRunChecked(isolate, "debug.Debug.clearBreakOnException()");
}
if (uncaught) {
CompileRunChecked(isolate, "debug.Debug.setBreakOnUncaughtException()");
} else {
CompileRunChecked(isolate, "debug.Debug.clearBreakOnUncaughtException()");
}
}
// Change break on exception using the native API call.
static void ChangeBreakOnExceptionFromAPI(
v8::Isolate* isolate, v8::debug::ExceptionBreakState state) {
v8::debug::ChangeBreakOnException(isolate, state);
}
// Prepare to step to next break location.
static void PrepareStep(StepAction step_action) {
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
debug->PrepareStep(step_action);
}
static void ClearStepping() { CcTest::i_isolate()->debug()->ClearStepping(); }
// This function is in namespace v8::internal to be friend with class
// v8::internal::Debug.
namespace v8 {
namespace internal {
// Collect the currently debugged functions.
Handle<FixedArray> GetDebuggedFunctions() {
Debug* debug = CcTest::i_isolate()->debug();
v8::internal::DebugInfoListNode* node = debug->debug_info_list_;
// Find the number of debugged functions.
int count = 0;
while (node) {
count++;
node = node->next();
}
// Allocate array for the debugged functions
Handle<FixedArray> debugged_functions =
CcTest::i_isolate()->factory()->NewFixedArray(count);
// Run through the debug info objects and collect all functions.
count = 0;
while (node) {
debugged_functions->set(count++, *node->debug_info());
node = node->next();
}
return debugged_functions;
}
// Check that the debugger has been fully unloaded.
void CheckDebuggerUnloaded() {
// Check that the debugger context is cleared and that there is no debug
// information stored for the debugger.
CHECK(CcTest::i_isolate()->debug()->debug_context().is_null());
CHECK(!CcTest::i_isolate()->debug()->debug_info_list_);
// Collect garbage to ensure weak handles are cleared.
CcTest::CollectAllGarbage();
CcTest::CollectAllGarbage(Heap::kMakeHeapIterableMask);
// Iterate the heap and check that there are no debugger related objects left.
HeapIterator iterator(CcTest::heap());
for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
CHECK(!obj->IsDebugInfo());
}
}
} // namespace internal
} // namespace v8
// Check that the debugger has been fully unloaded.
static void CheckDebuggerUnloaded() { v8::internal::CheckDebuggerUnloaded(); }
// --- D e b u g E v e n t H a n d l e r s
// ---
// --- The different tests uses a number of debug event handlers.
// ---
// Source for the JavaScript function which picks out the function
// name of a frame.
const char* frame_function_name_source =
"function frame_function_name(exec_state, frame_number) {"
" return exec_state.frame(frame_number).func().name();"
"}";
v8::Local<v8::Function> frame_function_name;
// Source for the JavaScript function which pick out the name of the
// first argument of a frame.
const char* frame_argument_name_source =
"function frame_argument_name(exec_state, frame_number) {"
" return exec_state.frame(frame_number).argumentName(0);"
"}";
v8::Local<v8::Function> frame_argument_name;
// Source for the JavaScript function which pick out the value of the
// first argument of a frame.
const char* frame_argument_value_source =
"function frame_argument_value(exec_state, frame_number) {"
" return exec_state.frame(frame_number).argumentValue(0).value_;"
"}";
v8::Local<v8::Function> frame_argument_value;
// Source for the JavaScript function which pick out the name of the
// first argument of a frame.
const char* frame_local_name_source =
"function frame_local_name(exec_state, frame_number) {"
" return exec_state.frame(frame_number).localName(0);"
"}";
v8::Local<v8::Function> frame_local_name;
// Source for the JavaScript function which pick out the value of the
// first argument of a frame.
const char* frame_local_value_source =
"function frame_local_value(exec_state, frame_number) {"
" return exec_state.frame(frame_number).localValue(0).value_;"
"}";
v8::Local<v8::Function> frame_local_value;
// Source for the JavaScript function which picks out the source line for the
// top frame.
const char* frame_source_line_source =
"function frame_source_line(exec_state) {"
" return exec_state.frame(0).sourceLine();"
"}";
v8::Local<v8::Function> frame_source_line;
// Source for the JavaScript function which picks out the source column for the
// top frame.
const char* frame_source_column_source =
"function frame_source_column(exec_state) {"
" return exec_state.frame(0).sourceColumn();"
"}";
v8::Local<v8::Function> frame_source_column;
// Source for the JavaScript function which picks out the script name for the
// top frame.
const char* frame_script_name_source =
"function frame_script_name(exec_state) {"
" return exec_state.frame(0).func().script().name();"
"}";
v8::Local<v8::Function> frame_script_name;
// Source for the JavaScript function which returns the number of frames.
static const char* frame_count_source =
"function frame_count(exec_state) {"
" return exec_state.frameCount();"
"}";
v8::Local<v8::Function> frame_count;
// Global variable to store the last function hit - used by some tests.
char last_function_hit[80];
// Global variable to store the name for last script hit - used by some tests.
char last_script_name_hit[80];
// Global variables to store the last source position - used by some tests.
int last_source_line = -1;
int last_source_column = -1;
// Debug event handler which counts the break points which have been hit.
int break_point_hit_count = 0;
int break_point_hit_count_deoptimize = 0;
static void DebugEventBreakPointHitCount(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Local<v8::Object> exec_state = event_details.GetExecutionState();
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
v8::internal::Isolate* isolate = CcTest::i_isolate();
Debug* debug = isolate->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
// Count the number of breaks.
if (event == v8::Break) {
break_point_hit_count++;
if (!frame_function_name.IsEmpty()) {
// Get the name of the function.
const int argc = 2;
v8::Local<v8::Value> argv[argc] = {
exec_state, v8::Integer::New(CcTest::isolate(), 0)};
v8::Local<v8::Value> result =
frame_function_name->Call(context, exec_state, argc, argv)
.ToLocalChecked();
if (result->IsUndefined()) {
last_function_hit[0] = '\0';
} else {
CHECK(result->IsString());
v8::Local<v8::String> function_name(result.As<v8::String>());
function_name->WriteUtf8(last_function_hit);
}
}
if (!frame_source_line.IsEmpty()) {
// Get the source line.
const int argc = 1;
v8::Local<v8::Value> argv[argc] = {exec_state};
v8::Local<v8::Value> result =
frame_source_line->Call(context, exec_state, argc, argv)
.ToLocalChecked();
CHECK(result->IsNumber());
last_source_line = result->Int32Value(context).FromJust();
}
if (!frame_source_column.IsEmpty()) {
// Get the source column.
const int argc = 1;
v8::Local<v8::Value> argv[argc] = {exec_state};
v8::Local<v8::Value> result =
frame_source_column->Call(context, exec_state, argc, argv)
.ToLocalChecked();
CHECK(result->IsNumber());
last_source_column = result->Int32Value(context).FromJust();
}
if (!frame_script_name.IsEmpty()) {
// Get the script name of the function script.
const int argc = 1;
v8::Local<v8::Value> argv[argc] = {exec_state};
v8::Local<v8::Value> result =
frame_script_name->Call(context, exec_state, argc, argv)
.ToLocalChecked();
if (result->IsUndefined()) {
last_script_name_hit[0] = '\0';
} else {
CHECK(result->IsString());
v8::Local<v8::String> script_name(result.As<v8::String>());
script_name->WriteUtf8(last_script_name_hit);
}
}
// Perform a full deoptimization when the specified number of
// breaks have been hit.
if (break_point_hit_count == break_point_hit_count_deoptimize) {
i::Deoptimizer::DeoptimizeAll(isolate);
}
}
}
// Debug event handler which counts a number of events and collects the stack
// height if there is a function compiled for that.
int exception_hit_count = 0;
int uncaught_exception_hit_count = 0;
int last_js_stack_height = -1;
v8::Local<v8::Function> debug_event_listener_callback;
int debug_event_listener_callback_result;
static void DebugEventCounterClear() {
break_point_hit_count = 0;
exception_hit_count = 0;
uncaught_exception_hit_count = 0;
}
static void DebugEventCounter(
const v8::Debug::EventDetails& event_details) {
v8::Isolate::AllowJavascriptExecutionScope allow_script(CcTest::isolate());
v8::DebugEvent event = event_details.GetEvent();
v8::Local<v8::Object> exec_state = event_details.GetExecutionState();
v8::Local<v8::Object> event_data = event_details.GetEventData();
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
// Count the number of breaks.
if (event == v8::Break) {
break_point_hit_count++;
} else if (event == v8::Exception) {
exception_hit_count++;
// Check whether the exception was uncaught.
v8::Local<v8::String> fun_name = v8_str(CcTest::isolate(), "uncaught");
v8::Local<v8::Function> fun = v8::Local<v8::Function>::Cast(
event_data->Get(context, fun_name).ToLocalChecked());
v8::Local<v8::Value> result =
fun->Call(context, event_data, 0, NULL).ToLocalChecked();
if (result->IsTrue()) {
uncaught_exception_hit_count++;
}
}
// Collect the JavsScript stack height if the function frame_count is
// compiled.
if (!frame_count.IsEmpty()) {
static const int kArgc = 1;
v8::Local<v8::Value> argv[kArgc] = {exec_state};
// Using exec_state as receiver is just to have a receiver.
v8::Local<v8::Value> result =
frame_count->Call(context, exec_state, kArgc, argv).ToLocalChecked();
last_js_stack_height = result->Int32Value(context).FromJust();
}
// Run callback from DebugEventListener and check the result.
if (!debug_event_listener_callback.IsEmpty()) {
v8::Local<v8::Value> result =
debug_event_listener_callback->Call(context, event_data, 0, NULL)
.ToLocalChecked();
CHECK(!result.IsEmpty());
CHECK_EQ(debug_event_listener_callback_result,
result->Int32Value(context).FromJust());
}
}
// Debug event handler which evaluates a number of expressions when a break
// point is hit. Each evaluated expression is compared with an expected value.
// For this debug event handler to work the following two global varaibles
// must be initialized.
// checks: An array of expressions and expected results
// evaluate_check_function: A JavaScript function (see below)
// Structure for holding checks to do.
struct EvaluateCheck {
const char* expr; // An expression to evaluate when a break point is hit.
v8::Local<v8::Value> expected; // The expected result.
};
// Array of checks to do.
struct EvaluateCheck* checks = NULL;
// Source for The JavaScript function which can do the evaluation when a break
// point is hit.
const char* evaluate_check_source =
"function evaluate_check(exec_state, expr, expected) {"
" return exec_state.frame(0).evaluate(expr).value() === expected;"
"}";
v8::Local<v8::Function> evaluate_check_function;
// The actual debug event described by the longer comment above.
static void DebugEventEvaluate(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Local<v8::Object> exec_state = event_details.GetExecutionState();
v8::Isolate* isolate = CcTest::isolate();
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break) {
break_point_hit_count++;
for (int i = 0; checks[i].expr != NULL; i++) {
const int argc = 3;
v8::Local<v8::String> string = v8_str(isolate, checks[i].expr);
v8::Local<v8::Value> argv[argc] = {exec_state, string,
checks[i].expected};
v8::Local<v8::Value> result =
evaluate_check_function->Call(context, exec_state, argc, argv)
.ToLocalChecked();
if (!result->IsTrue()) {
v8::String::Utf8Value utf8(isolate, checks[i].expected);
V8_Fatal(__FILE__, __LINE__, "%s != %s", checks[i].expr, *utf8);
}
}
}
}
// This debug event listener removes a breakpoint in a function
int debug_event_remove_break_point = 0;
static void DebugEventRemoveBreakPoint(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Local<v8::Value> data = event_details.GetCallbackData();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break) {
break_point_hit_count++;
CHECK(data->IsFunction());
ClearBreakPoint(debug_event_remove_break_point);
}
}
// Debug event handler which counts break points hit and performs a step
// afterwards.
StepAction step_action = StepIn; // Step action to perform when stepping.
static void DebugEventStep(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break) {
break_point_hit_count++;
PrepareStep(step_action);
}
}
// Debug event handler which counts break points hit and performs a step
// afterwards. For each call the expected function is checked.
// For this debug event handler to work the following two global varaibles
// must be initialized.
// expected_step_sequence: An array of the expected function call sequence.
// frame_function_name: A JavaScript function (see below).
// String containing the expected function call sequence. Note: this only works
// if functions have name length of one.
const char* expected_step_sequence = NULL;
// The actual debug event described by the longer comment above.
static void DebugEventStepSequence(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Local<v8::Object> exec_state = event_details.GetExecutionState();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break || event == v8::Exception) {
// Check that the current function is the expected.
CHECK(break_point_hit_count <
StrLength(expected_step_sequence));
const int argc = 2;
v8::Local<v8::Value> argv[argc] = {exec_state,
v8::Integer::New(CcTest::isolate(), 0)};
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
v8::Local<v8::Value> result =
frame_function_name->Call(context, exec_state, argc, argv)
.ToLocalChecked();
CHECK(result->IsString());
v8::String::Utf8Value function_name(
CcTest::isolate(), result->ToString(context).ToLocalChecked());
CHECK_EQ(1, StrLength(*function_name));
CHECK_EQ((*function_name)[0],
expected_step_sequence[break_point_hit_count]);
// Perform step.
break_point_hit_count++;
PrepareStep(step_action);
}
}
// Debug event handler which performs a garbage collection.
static void DebugEventBreakPointCollectGarbage(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
// Perform a garbage collection when break point is hit and continue. Based
// on the number of break points hit either scavenge or mark compact
// collector is used.
if (event == v8::Break) {
break_point_hit_count++;
if (break_point_hit_count % 2 == 0) {
// Scavenge.
CcTest::CollectGarbage(v8::internal::NEW_SPACE);
} else {
// Mark sweep compact.
CcTest::CollectAllGarbage();
}
}
}
// Debug event handler which re-issues a debug break and calls the garbage
// collector to have the heap verified.
static void DebugEventBreak(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::internal::Debug* debug = CcTest::i_isolate()->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break) {
// Count the number of breaks.
break_point_hit_count++;
// Run the garbage collector to enforce heap verification if option
// --verify-heap is set.
CcTest::CollectGarbage(v8::internal::NEW_SPACE);
// Set the break flag again to come back here as soon as possible.
v8::debug::DebugBreak(CcTest::isolate());
}
}
// Debug event handler which re-issues a debug break until a limit has been
// reached.
int max_break_point_hit_count = 0;
bool terminate_after_max_break_point_hit = false;
static void DebugEventBreakMax(
const v8::Debug::EventDetails& event_details) {
v8::DebugEvent event = event_details.GetEvent();
v8::Isolate* v8_isolate = CcTest::isolate();
v8::internal::Isolate* isolate = CcTest::i_isolate();
v8::internal::Debug* debug = isolate->debug();
// When hitting a debug event listener there must be a break set.
CHECK_NE(debug->break_id(), 0);
if (event == v8::Break) {
if (break_point_hit_count < max_break_point_hit_count) {
// Count the number of breaks.
break_point_hit_count++;
// Set the break flag again to come back here as soon as possible.
v8::debug::DebugBreak(v8_isolate);
} else if (terminate_after_max_break_point_hit) {
// Terminate execution after the last break if requested.
v8_isolate->TerminateExecution();
}
// Perform a full deoptimization when the specified number of
// breaks have been hit.
if (break_point_hit_count == break_point_hit_count_deoptimize) {
i::Deoptimizer::DeoptimizeAll(isolate);
}
}
}
// --- M e s s a g e C a l l b a c k
// Message callback which counts the number of messages.
int message_callback_count = 0;
static void MessageCallbackCountClear() {
message_callback_count = 0;
}
static void MessageCallbackCount(v8::Local<v8::Message> message,
v8::Local<v8::Value> data) {
message_callback_count++;
}
// --- T h e A c t u a l T e s t s
// Test that the debug info in the VM is in sync with the functions being
// debugged.
TEST(DebugInfo) {
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
// Create a couple of functions for the test.
v8::Local<v8::Function> foo =
CompileFunction(&env, "function foo(){}", "foo");
v8::Local<v8::Function> bar =
CompileFunction(&env, "function bar(){}", "bar");
// Initially no functions are debugged.
CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
CHECK(!HasBreakInfo(foo));
CHECK(!HasBreakInfo(bar));
EnableDebugger(env->GetIsolate());
// One function (foo) is debugged.
int bp1 = SetBreakPoint(foo, 0);
CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
CHECK(HasBreakInfo(foo));
CHECK(!HasBreakInfo(bar));
// Two functions are debugged.
int bp2 = SetBreakPoint(bar, 0);
CHECK_EQ(2, v8::internal::GetDebuggedFunctions()->length());
CHECK(HasBreakInfo(foo));
CHECK(HasBreakInfo(bar));
// One function (bar) is debugged.
ClearBreakPoint(bp1);
CHECK_EQ(1, v8::internal::GetDebuggedFunctions()->length());
CHECK(!HasBreakInfo(foo));
CHECK(HasBreakInfo(bar));
// No functions are debugged.
ClearBreakPoint(bp2);
DisableDebugger(env->GetIsolate());
CHECK_EQ(0, v8::internal::GetDebuggedFunctions()->length());
CHECK(!HasBreakInfo(foo));
CHECK(!HasBreakInfo(bar));
}
// Test that a break point can be set at an IC store location.
TEST(BreakPointICStore) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount);
v8::Local<v8::Function> foo =
CompileFunction(&env, "function foo(){bar=0;}", "foo");
// Run without breakpoints.
foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked();
CHECK_EQ(0, break_point_hit_count);
// Run with breakpoint
int bp = SetBreakPoint(foo, 0);
foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked();
CHECK_EQ(1, break_point_hit_count);
foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked();
CHECK_EQ(2, break_point_hit_count);
// Run without breakpoints.
ClearBreakPoint(bp);
foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked();
CHECK_EQ(2, break_point_hit_count);
SetDebugEventListener(env->GetIsolate(), nullptr);
CheckDebuggerUnloaded();
}
// Test that a break point can be set at an IC load location.
TEST(BreakPointICLoad) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount);
CompileRunChecked(env->GetIsolate(), "bar=1");
v8::Local<v8::Function> foo =
CompileFunction(&env, "function foo(){var x=bar;}", "foo");
// Run without breakpoints.
foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked();
CHECK_EQ(0, break_point_hit_count);
// Run with breakpoint.
int bp = SetBreakPoint(foo, 0);
foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked();
CHECK_EQ(1, break_point_hit_count);
foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked();
CHECK_EQ(2, break_point_hit_count);
// Run without breakpoints.
ClearBreakPoint(bp);
foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked();
CHECK_EQ(2, break_point_hit_count);
SetDebugEventListener(env->GetIsolate(), nullptr);
CheckDebuggerUnloaded();
}
// Test that a break point can be set at an IC call location.
TEST(BreakPointICCall) {
break_point_hit_count = 0;
DebugLocalContext env;
v8::HandleScope scope(env->GetIsolate());
SetDebugEventListener(env->GetIsolate(), DebugEventBreakPointHitCount);
CompileRunChecked(env->GetIsolate(), "function bar(){}");
v8::Local<v8::Function> foo =
CompileFunction(&env, "function foo(){bar();}", "foo");
// Run without breakpoints.
foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked();
CHECK_EQ(0, break_point_hit_count);
// Run with breakpoint
int bp = SetBreakPoint(foo, 0);
foo->Call(env.context(), env->Global(), 0, NULL).ToLocalChecked();
CHECK_EQ(1, break_point_hit_count);