forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.cc
More file actions
2601 lines (2314 loc) · 89.8 KB
/
log.cc
File metadata and controls
2601 lines (2314 loc) · 89.8 KB
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 2011 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/logging/log.h"
#include <atomic>
#include <cstdarg>
#include <memory>
#include <sstream>
#include "include/v8-locker.h"
#include "src/api/api-inl.h"
#include "src/base/functional.h"
#include "src/base/platform/mutex.h"
#include "src/base/platform/platform.h"
#include "src/base/platform/wrappers.h"
#include "src/builtins/profile-data-reader.h"
#include "src/codegen/bailout-reason.h"
#include "src/codegen/compiler.h"
#include "src/codegen/macro-assembler.h"
#include "src/codegen/source-position-table.h"
#include "src/common/assert-scope.h"
#include "src/deoptimizer/deoptimizer.h"
#include "src/diagnostics/perf-jit.h"
#include "src/execution/isolate.h"
#include "src/execution/v8threads.h"
#include "src/execution/vm-state-inl.h"
#include "src/handles/global-handles.h"
#include "src/heap/combined-heap.h"
#include "src/heap/heap-inl.h"
#include "src/init/bootstrapper.h"
#include "src/interpreter/bytecodes.h"
#include "src/interpreter/interpreter.h"
#include "src/libsampler/sampler.h"
#include "src/logging/code-events.h"
#include "src/logging/counters.h"
#include "src/logging/log-file.h"
#include "src/logging/log-inl.h"
#include "src/objects/api-callbacks.h"
#include "src/objects/code-kind.h"
#include "src/objects/code.h"
#include "src/profiler/tick-sample.h"
#include "src/snapshot/embedded/embedded-data.h"
#include "src/strings/string-stream.h"
#include "src/strings/unicode-inl.h"
#include "src/tracing/tracing-category-observer.h"
#include "src/utils/memcopy.h"
#include "src/utils/version.h"
#ifdef ENABLE_GDB_JIT_INTERFACE
#include "src/diagnostics/gdb-jit.h"
#endif // ENABLE_GDB_JIT_INTERFACE
#if V8_ENABLE_WEBASSEMBLY
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-engine.h"
#include "src/wasm/wasm-objects-inl.h"
#endif // V8_ENABLE_WEBASSEMBLY
#if V8_OS_WIN
#if defined(V8_ENABLE_ETW_STACK_WALKING)
#include "src/diagnostics/etw-jit-win.h"
#endif
#endif // V8_OS_WIN
namespace v8 {
namespace internal {
static const char* kLogEventsNames[] = {
#define DECLARE_EVENT(ignore1, name) name,
LOG_EVENT_LIST(DECLARE_EVENT)
#undef DECLARE_EVENT
};
static const char* kCodeTagNames[] = {
#define DECLARE_EVENT(ignore1, name) #name,
CODE_TYPE_LIST(DECLARE_EVENT)
#undef DECLARE_EVENT
};
std::ostream& operator<<(std::ostream& os, LogEventListener::CodeTag tag) {
os << kCodeTagNames[static_cast<int>(tag)];
return os;
}
std::ostream& operator<<(std::ostream& os, LogEventListener::Event event) {
os << kLogEventsNames[static_cast<int>(event)];
return os;
}
namespace {
v8::CodeEventType GetCodeEventTypeForTag(LogEventListener::CodeTag tag) {
switch (tag) {
case LogEventListener::CodeTag::kLength:
// Manually create this switch, since v8::CodeEventType is API expose and
// cannot be easily modified.
case LogEventListener::CodeTag::kBuiltin:
return v8::CodeEventType::kBuiltinType;
case LogEventListener::CodeTag::kCallback:
return v8::CodeEventType::kCallbackType;
case LogEventListener::CodeTag::kEval:
return v8::CodeEventType::kEvalType;
case LogEventListener::CodeTag::kNativeFunction:
case LogEventListener::CodeTag::kFunction:
return v8::CodeEventType::kFunctionType;
case LogEventListener::CodeTag::kHandler:
return v8::CodeEventType::kHandlerType;
case LogEventListener::CodeTag::kBytecodeHandler:
return v8::CodeEventType::kBytecodeHandlerType;
case LogEventListener::CodeTag::kRegExp:
return v8::CodeEventType::kRegExpType;
case LogEventListener::CodeTag::kNativeScript:
case LogEventListener::CodeTag::kScript:
return v8::CodeEventType::kScriptType;
case LogEventListener::CodeTag::kStub:
return v8::CodeEventType::kStubType;
}
UNREACHABLE();
}
#define CALL_CODE_EVENT_HANDLER(Call) \
if (listener_) { \
listener_->Call; \
} else { \
PROFILE(isolate_, Call); \
}
const char* ComputeMarker(Tagged<SharedFunctionInfo> shared,
Tagged<AbstractCode> code) {
PtrComprCageBase cage_base = GetPtrComprCageBase(shared);
CodeKind kind = code->kind(cage_base);
// We record interpreter trampoline builtin copies as having the
// "interpreted" marker.
if (v8_flags.interpreted_frames_native_stack && kind == CodeKind::BUILTIN &&
code->has_instruction_stream(cage_base)) {
DCHECK_EQ(code->builtin_id(cage_base),
Builtin::kInterpreterEntryTrampoline);
kind = CodeKind::INTERPRETED_FUNCTION;
}
if (shared->optimization_disabled() &&
kind == CodeKind::INTERPRETED_FUNCTION) {
return "";
}
return CodeKindToMarker(kind);
}
#if V8_ENABLE_WEBASSEMBLY
const char* ComputeMarker(const wasm::WasmCode* code) {
switch (code->kind()) {
case wasm::WasmCode::kWasmFunction:
return code->is_liftoff() ? "" : "*";
default:
return "";
}
}
#endif // V8_ENABLE_WEBASSEMBLY
} // namespace
class CodeEventLogger::NameBuffer {
public:
NameBuffer() { Reset(); }
void Reset() { utf8_pos_ = 0; }
void Init(CodeTag tag) {
Reset();
AppendBytes(kCodeTagNames[static_cast<int>(tag)]);
AppendByte(':');
}
void AppendName(Tagged<Name> name) {
if (IsString(name)) {
AppendString(String::cast(name));
} else {
Tagged<Symbol> symbol = Symbol::cast(name);
AppendBytes("symbol(");
if (!IsUndefined(symbol->description())) {
AppendBytes("\"");
AppendString(String::cast(symbol->description()));
AppendBytes("\" ");
}
AppendBytes("hash ");
AppendHex(symbol->hash());
AppendByte(')');
}
}
void AppendString(Tagged<String> str) {
if (str.is_null()) return;
int length = 0;
std::unique_ptr<char[]> c_str =
str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, &length);
AppendBytes(c_str.get(), length);
}
void AppendBytes(const char* bytes, int size) {
size = std::min(size, kUtf8BufferSize - utf8_pos_);
MemCopy(utf8_buffer_ + utf8_pos_, bytes, size);
utf8_pos_ += size;
}
void AppendBytes(const char* bytes) {
size_t len = strlen(bytes);
DCHECK_GE(kMaxInt, len);
AppendBytes(bytes, static_cast<int>(len));
}
void AppendByte(char c) {
if (utf8_pos_ >= kUtf8BufferSize) return;
utf8_buffer_[utf8_pos_++] = c;
}
void AppendInt(int n) {
int space = kUtf8BufferSize - utf8_pos_;
if (space <= 0) return;
base::Vector<char> buffer(utf8_buffer_ + utf8_pos_, space);
int size = SNPrintF(buffer, "%d", n);
if (size > 0 && utf8_pos_ + size <= kUtf8BufferSize) {
utf8_pos_ += size;
}
}
void AppendHex(uint32_t n) {
int space = kUtf8BufferSize - utf8_pos_;
if (space <= 0) return;
base::Vector<char> buffer(utf8_buffer_ + utf8_pos_, space);
int size = SNPrintF(buffer, "%x", n);
if (size > 0 && utf8_pos_ + size <= kUtf8BufferSize) {
utf8_pos_ += size;
}
}
const char* get() { return utf8_buffer_; }
int size() const { return utf8_pos_; }
private:
static const int kUtf8BufferSize = 4096;
static const int kUtf16BufferSize = kUtf8BufferSize;
int utf8_pos_;
char utf8_buffer_[kUtf8BufferSize];
};
CodeEventLogger::CodeEventLogger(Isolate* isolate)
: isolate_(isolate), name_buffer_(std::make_unique<NameBuffer>()) {}
CodeEventLogger::~CodeEventLogger() = default;
void CodeEventLogger::CodeCreateEvent(CodeTag tag, Handle<AbstractCode> code,
const char* comment) {
DCHECK(is_listening_to_code_events());
name_buffer_->Init(tag);
name_buffer_->AppendBytes(comment);
DisallowGarbageCollection no_gc;
LogRecordedBuffer(*code, MaybeHandle<SharedFunctionInfo>(),
name_buffer_->get(), name_buffer_->size());
}
void CodeEventLogger::CodeCreateEvent(CodeTag tag, Handle<AbstractCode> code,
Handle<Name> name) {
DCHECK(is_listening_to_code_events());
name_buffer_->Init(tag);
name_buffer_->AppendName(*name);
DisallowGarbageCollection no_gc;
LogRecordedBuffer(*code, MaybeHandle<SharedFunctionInfo>(),
name_buffer_->get(), name_buffer_->size());
}
void CodeEventLogger::CodeCreateEvent(CodeTag tag, Handle<AbstractCode> code,
Handle<SharedFunctionInfo> shared,
Handle<Name> script_name) {
DCHECK(is_listening_to_code_events());
name_buffer_->Init(tag);
name_buffer_->AppendBytes(ComputeMarker(*shared, *code));
name_buffer_->AppendByte(' ');
name_buffer_->AppendName(*script_name);
DisallowGarbageCollection no_gc;
LogRecordedBuffer(*code, shared, name_buffer_->get(), name_buffer_->size());
}
void CodeEventLogger::CodeCreateEvent(CodeTag tag, Handle<AbstractCode> code,
Handle<SharedFunctionInfo> shared,
Handle<Name> script_name, int line,
int column) {
DCHECK(is_listening_to_code_events());
name_buffer_->Init(tag);
name_buffer_->AppendBytes(ComputeMarker(*shared, *code));
name_buffer_->AppendBytes(shared->DebugNameCStr().get());
name_buffer_->AppendByte(' ');
if (IsString(*script_name)) {
name_buffer_->AppendString(String::cast(*script_name));
} else {
name_buffer_->AppendBytes("symbol(hash ");
name_buffer_->AppendHex(Name::cast(*script_name)->hash());
name_buffer_->AppendByte(')');
}
name_buffer_->AppendByte(':');
name_buffer_->AppendInt(line);
name_buffer_->AppendByte(':');
name_buffer_->AppendInt(column);
DisallowGarbageCollection no_gc;
LogRecordedBuffer(*code, shared, name_buffer_->get(), name_buffer_->size());
}
#if V8_ENABLE_WEBASSEMBLY
void CodeEventLogger::CodeCreateEvent(CodeTag tag, const wasm::WasmCode* code,
wasm::WasmName name,
const char* source_url,
int /*code_offset*/, int /*script_id*/) {
DCHECK(is_listening_to_code_events());
name_buffer_->Init(tag);
DCHECK(!name.empty());
name_buffer_->AppendBytes(name.begin(), name.length());
name_buffer_->AppendByte('-');
if (code->IsAnonymous()) {
name_buffer_->AppendBytes("<anonymous>");
} else {
name_buffer_->AppendInt(code->index());
}
name_buffer_->AppendByte('-');
name_buffer_->AppendBytes(ExecutionTierToString(code->tier()));
DisallowGarbageCollection no_gc;
LogRecordedBuffer(code, name_buffer_->get(), name_buffer_->size());
}
#endif // V8_ENABLE_WEBASSEMBLY
void CodeEventLogger::RegExpCodeCreateEvent(Handle<AbstractCode> code,
Handle<String> source) {
DCHECK(is_listening_to_code_events());
name_buffer_->Init(LogEventListener::CodeTag::kRegExp);
name_buffer_->AppendString(*source);
DisallowGarbageCollection no_gc;
LogRecordedBuffer(*code, MaybeHandle<SharedFunctionInfo>(),
name_buffer_->get(), name_buffer_->size());
}
// Linux perf tool logging support.
#if V8_OS_LINUX
class LinuxPerfBasicLogger : public CodeEventLogger {
public:
explicit LinuxPerfBasicLogger(Isolate* isolate);
~LinuxPerfBasicLogger() override;
void CodeMoveEvent(Tagged<InstructionStream> from,
Tagged<InstructionStream> to) override {}
void BytecodeMoveEvent(Tagged<BytecodeArray> from,
Tagged<BytecodeArray> to) override {}
void CodeDisableOptEvent(Handle<AbstractCode> code,
Handle<SharedFunctionInfo> shared) override {}
private:
void LogRecordedBuffer(Tagged<AbstractCode> code,
MaybeHandle<SharedFunctionInfo> maybe_shared,
const char* name, int length) override;
#if V8_ENABLE_WEBASSEMBLY
void LogRecordedBuffer(const wasm::WasmCode* code, const char* name,
int length) override;
#endif // V8_ENABLE_WEBASSEMBLY
void WriteLogRecordedBuffer(uintptr_t address, int size, const char* name,
int name_length);
static base::LazyRecursiveMutex& GetFileMutex();
// Extension added to V8 log file name to get the low-level log name.
static const char kFilenameFormatString[];
static const int kFilenameBufferPadding;
// Per-process singleton file. We assume that there is one main isolate
// to determine when it goes away, we keep the reference count.
static FILE* perf_output_handle_;
static uint64_t reference_count_;
};
// Extra space for the "perf-%d.map" filename, including the PID.
const int LinuxPerfBasicLogger::kFilenameBufferPadding = 32;
// static
base::LazyRecursiveMutex& LinuxPerfBasicLogger::GetFileMutex() {
static base::LazyRecursiveMutex file_mutex = LAZY_RECURSIVE_MUTEX_INITIALIZER;
return file_mutex;
}
// The following static variables are protected by
// LinuxPerfBasicLogger::GetFileMutex().
uint64_t LinuxPerfBasicLogger::reference_count_ = 0;
FILE* LinuxPerfBasicLogger::perf_output_handle_ = nullptr;
LinuxPerfBasicLogger::LinuxPerfBasicLogger(Isolate* isolate)
: CodeEventLogger(isolate) {
base::LockGuard<base::RecursiveMutex> guard_file(GetFileMutex().Pointer());
int process_id_ = base::OS::GetCurrentProcessId();
reference_count_++;
// If this is the first logger, open the file.
if (reference_count_ == 1) {
CHECK_NULL(perf_output_handle_);
CHECK_NOT_NULL(v8_flags.perf_basic_prof_path);
const char* base_dir = v8_flags.perf_basic_prof_path;
// Open the perf JIT dump file.
base::ScopedVector<char> perf_dump_name(strlen(base_dir) +
kFilenameBufferPadding);
int size =
SNPrintF(perf_dump_name, "%s/perf-%d.map", base_dir, process_id_);
CHECK_NE(size, -1);
perf_output_handle_ =
base::OS::FOpen(perf_dump_name.begin(), base::OS::LogFileOpenMode);
CHECK_NOT_NULL(perf_output_handle_);
setvbuf(perf_output_handle_, nullptr, _IOLBF, 0);
}
}
LinuxPerfBasicLogger::~LinuxPerfBasicLogger() {
base::LockGuard<base::RecursiveMutex> guard_file(GetFileMutex().Pointer());
reference_count_--;
// If this was the last logger, close the file.
if (reference_count_ == 0) {
CHECK_NOT_NULL(perf_output_handle_);
base::Fclose(perf_output_handle_);
perf_output_handle_ = nullptr;
}
}
void LinuxPerfBasicLogger::WriteLogRecordedBuffer(uintptr_t address, int size,
const char* name,
int name_length) {
// Linux perf expects hex literals without a leading 0x, while some
// implementations of printf might prepend one when using the %p format
// for pointers, leading to wrongly formatted JIT symbols maps. On the other
// hand, Android's simpleperf does expect a leading 0x.
//
// Instead, we use V8PRIxPTR format string and cast pointer to uintpr_t,
// so that we have control over the exact output format.
#ifdef V8_OS_ANDROID
base::OS::FPrint(perf_output_handle_, "0x%" V8PRIxPTR " 0x%x %.*s\n", address,
size, name_length, name);
#else
base::OS::FPrint(perf_output_handle_, "%" V8PRIxPTR " %x %.*s\n", address,
size, name_length, name);
#endif
}
void LinuxPerfBasicLogger::LogRecordedBuffer(Tagged<AbstractCode> code,
MaybeHandle<SharedFunctionInfo>,
const char* name, int length) {
DisallowGarbageCollection no_gc;
PtrComprCageBase cage_base(isolate_);
if (v8_flags.perf_basic_prof_only_functions &&
!CodeKindIsBuiltinOrJSFunction(code->kind(cage_base))) {
return;
}
WriteLogRecordedBuffer(
static_cast<uintptr_t>(code->InstructionStart(cage_base)),
code->InstructionSize(cage_base), name, length);
}
#if V8_ENABLE_WEBASSEMBLY
void LinuxPerfBasicLogger::LogRecordedBuffer(const wasm::WasmCode* code,
const char* name, int length) {
WriteLogRecordedBuffer(static_cast<uintptr_t>(code->instruction_start()),
code->instructions().length(), name, length);
}
#endif // V8_ENABLE_WEBASSEMBLY
#endif // V8_OS_LINUX
// External LogEventListener
ExternalLogEventListener::ExternalLogEventListener(Isolate* isolate)
: is_listening_(false), isolate_(isolate), code_event_handler_(nullptr) {}
ExternalLogEventListener::~ExternalLogEventListener() {
if (is_listening_) {
StopListening();
}
}
void ExternalLogEventListener::LogExistingCode() {
HandleScope scope(isolate_);
ExistingCodeLogger logger(isolate_, this);
logger.LogBuiltins();
logger.LogCodeObjects();
logger.LogCompiledFunctions();
}
void ExternalLogEventListener::StartListening(
CodeEventHandler* code_event_handler) {
if (is_listening_ || code_event_handler == nullptr) {
return;
}
code_event_handler_ = code_event_handler;
is_listening_ = isolate_->logger()->AddListener(this);
if (is_listening_) {
LogExistingCode();
}
}
void ExternalLogEventListener::StopListening() {
if (!is_listening_) {
return;
}
isolate_->logger()->RemoveListener(this);
is_listening_ = false;
}
void ExternalLogEventListener::CodeCreateEvent(CodeTag tag,
Handle<AbstractCode> code,
const char* comment) {
PtrComprCageBase cage_base(isolate_);
CodeEvent code_event;
code_event.code_start_address =
static_cast<uintptr_t>(code->InstructionStart(cage_base));
code_event.code_size = static_cast<size_t>(code->InstructionSize(cage_base));
code_event.function_name = isolate_->factory()->empty_string();
code_event.script_name = isolate_->factory()->empty_string();
code_event.script_line = 0;
code_event.script_column = 0;
code_event.code_type = GetCodeEventTypeForTag(tag);
code_event.comment = comment;
code_event_handler_->Handle(reinterpret_cast<v8::CodeEvent*>(&code_event));
}
void ExternalLogEventListener::CodeCreateEvent(CodeTag tag,
Handle<AbstractCode> code,
Handle<Name> name) {
Handle<String> name_string =
Name::ToFunctionName(isolate_, name).ToHandleChecked();
PtrComprCageBase cage_base(isolate_);
CodeEvent code_event;
code_event.code_start_address =
static_cast<uintptr_t>(code->InstructionStart(cage_base));
code_event.code_size = static_cast<size_t>(code->InstructionSize(cage_base));
code_event.function_name = name_string;
code_event.script_name = isolate_->factory()->empty_string();
code_event.script_line = 0;
code_event.script_column = 0;
code_event.code_type = GetCodeEventTypeForTag(tag);
code_event.comment = "";
code_event_handler_->Handle(reinterpret_cast<v8::CodeEvent*>(&code_event));
}
void ExternalLogEventListener::CodeCreateEvent(
CodeTag tag, Handle<AbstractCode> code, Handle<SharedFunctionInfo> shared,
Handle<Name> name) {
Handle<String> name_string =
Name::ToFunctionName(isolate_, name).ToHandleChecked();
PtrComprCageBase cage_base(isolate_);
CodeEvent code_event;
code_event.code_start_address =
static_cast<uintptr_t>(code->InstructionStart(cage_base));
code_event.code_size = static_cast<size_t>(code->InstructionSize(cage_base));
code_event.function_name = name_string;
code_event.script_name = isolate_->factory()->empty_string();
code_event.script_line = 0;
code_event.script_column = 0;
code_event.code_type = GetCodeEventTypeForTag(tag);
code_event.comment = "";
code_event_handler_->Handle(reinterpret_cast<v8::CodeEvent*>(&code_event));
}
void ExternalLogEventListener::CodeCreateEvent(
CodeTag tag, Handle<AbstractCode> code, Handle<SharedFunctionInfo> shared,
Handle<Name> source, int line, int column) {
Handle<String> name_string =
Name::ToFunctionName(isolate_, handle(shared->Name(), isolate_))
.ToHandleChecked();
Handle<String> source_string =
Name::ToFunctionName(isolate_, source).ToHandleChecked();
PtrComprCageBase cage_base(isolate_);
CodeEvent code_event;
code_event.code_start_address =
static_cast<uintptr_t>(code->InstructionStart(cage_base));
code_event.code_size = static_cast<size_t>(code->InstructionSize(cage_base));
code_event.function_name = name_string;
code_event.script_name = source_string;
code_event.script_line = line;
code_event.script_column = column;
code_event.code_type = GetCodeEventTypeForTag(tag);
code_event.comment = "";
code_event_handler_->Handle(reinterpret_cast<v8::CodeEvent*>(&code_event));
}
#if V8_ENABLE_WEBASSEMBLY
void ExternalLogEventListener::CodeCreateEvent(CodeTag tag,
const wasm::WasmCode* code,
wasm::WasmName name,
const char* source_url,
int code_offset, int script_id) {
// TODO(mmarchini): handle later
}
#endif // V8_ENABLE_WEBASSEMBLY
void ExternalLogEventListener::RegExpCodeCreateEvent(Handle<AbstractCode> code,
Handle<String> source) {
PtrComprCageBase cage_base(isolate_);
CodeEvent code_event;
code_event.code_start_address =
static_cast<uintptr_t>(code->InstructionStart(cage_base));
code_event.code_size = static_cast<size_t>(code->InstructionSize(cage_base));
code_event.function_name = source;
code_event.script_name = isolate_->factory()->empty_string();
code_event.script_line = 0;
code_event.script_column = 0;
code_event.code_type =
GetCodeEventTypeForTag(LogEventListener::CodeTag::kRegExp);
code_event.comment = "";
code_event_handler_->Handle(reinterpret_cast<v8::CodeEvent*>(&code_event));
}
namespace {
void InitializeCodeEvent(Isolate* isolate, CodeEvent* event,
Address previous_code_start_address,
Address code_start_address, int code_size) {
event->previous_code_start_address =
static_cast<uintptr_t>(previous_code_start_address);
event->code_start_address = static_cast<uintptr_t>(code_start_address);
event->code_size = static_cast<size_t>(code_size);
event->function_name = isolate->factory()->empty_string();
event->script_name = isolate->factory()->empty_string();
event->script_line = 0;
event->script_column = 0;
event->code_type = v8::CodeEventType::kRelocationType;
event->comment = "";
}
} // namespace
void ExternalLogEventListener::CodeMoveEvent(Tagged<InstructionStream> from,
Tagged<InstructionStream> to) {
CodeEvent code_event;
InitializeCodeEvent(isolate_, &code_event, from->instruction_start(),
to->instruction_start(),
to->code(kAcquireLoad)->instruction_size());
code_event_handler_->Handle(reinterpret_cast<v8::CodeEvent*>(&code_event));
}
void ExternalLogEventListener::BytecodeMoveEvent(Tagged<BytecodeArray> from,
Tagged<BytecodeArray> to) {
CodeEvent code_event;
InitializeCodeEvent(isolate_, &code_event, from->GetFirstBytecodeAddress(),
to->GetFirstBytecodeAddress(), to->length());
code_event_handler_->Handle(reinterpret_cast<v8::CodeEvent*>(&code_event));
}
// Low-level logging support.
class LowLevelLogger : public CodeEventLogger {
public:
LowLevelLogger(Isolate* isolate, const char* file_name);
~LowLevelLogger() override;
void CodeMoveEvent(Tagged<InstructionStream> from,
Tagged<InstructionStream> to) override;
void BytecodeMoveEvent(Tagged<BytecodeArray> from,
Tagged<BytecodeArray> to) override;
void CodeDisableOptEvent(Handle<AbstractCode> code,
Handle<SharedFunctionInfo> shared) override {}
void SnapshotPositionEvent(Tagged<HeapObject> obj, int pos);
void CodeMovingGCEvent() override;
private:
void LogRecordedBuffer(Tagged<AbstractCode> code,
MaybeHandle<SharedFunctionInfo> maybe_shared,
const char* name, int length) override;
#if V8_ENABLE_WEBASSEMBLY
void LogRecordedBuffer(const wasm::WasmCode* code, const char* name,
int length) override;
#endif // V8_ENABLE_WEBASSEMBLY
// Low-level profiling event structures.
struct CodeCreateStruct {
static const char kTag = 'C';
int32_t name_size;
Address code_address;
int32_t code_size;
};
struct CodeMoveStruct {
static const char kTag = 'M';
Address from_address;
Address to_address;
};
static const char kCodeMovingGCTag = 'G';
// Extension added to V8 log file name to get the low-level log name.
static const char kLogExt[];
void LogCodeInfo();
void LogWriteBytes(const char* bytes, int size);
template <typename T>
void LogWriteStruct(const T& s) {
char tag = T::kTag;
LogWriteBytes(reinterpret_cast<const char*>(&tag), sizeof(tag));
LogWriteBytes(reinterpret_cast<const char*>(&s), sizeof(s));
}
FILE* ll_output_handle_;
};
const char LowLevelLogger::kLogExt[] = ".ll";
LowLevelLogger::LowLevelLogger(Isolate* isolate, const char* name)
: CodeEventLogger(isolate), ll_output_handle_(nullptr) {
// Open the low-level log file.
size_t len = strlen(name);
base::ScopedVector<char> ll_name(static_cast<int>(len + sizeof(kLogExt)));
MemCopy(ll_name.begin(), name, len);
MemCopy(ll_name.begin() + len, kLogExt, sizeof(kLogExt));
ll_output_handle_ =
base::OS::FOpen(ll_name.begin(), base::OS::LogFileOpenMode);
setvbuf(ll_output_handle_, nullptr, _IOLBF, 0);
LogCodeInfo();
}
LowLevelLogger::~LowLevelLogger() {
base::Fclose(ll_output_handle_);
ll_output_handle_ = nullptr;
}
void LowLevelLogger::LogCodeInfo() {
#if V8_TARGET_ARCH_IA32
const char arch[] = "ia32";
#elif V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_64_BIT
const char arch[] = "x64";
#elif V8_TARGET_ARCH_ARM
const char arch[] = "arm";
#elif V8_TARGET_ARCH_PPC
const char arch[] = "ppc";
#elif V8_TARGET_ARCH_PPC64
const char arch[] = "ppc64";
#elif V8_TARGET_ARCH_LOONG64
const char arch[] = "loong64";
#elif V8_TARGET_ARCH_ARM64
const char arch[] = "arm64";
#elif V8_TARGET_ARCH_S390
const char arch[] = "s390";
#elif V8_TARGET_ARCH_RISCV64
const char arch[] = "riscv64";
#elif V8_TARGET_ARCH_RISCV32
const char arch[] = "riscv32";
#else
const char arch[] = "unknown";
#endif
LogWriteBytes(arch, sizeof(arch));
}
void LowLevelLogger::LogRecordedBuffer(Tagged<AbstractCode> code,
MaybeHandle<SharedFunctionInfo>,
const char* name, int length) {
DisallowGarbageCollection no_gc;
PtrComprCageBase cage_base(isolate_);
CodeCreateStruct event;
event.name_size = length;
event.code_address = code->InstructionStart(cage_base);
event.code_size = code->InstructionSize(cage_base);
LogWriteStruct(event);
LogWriteBytes(name, length);
LogWriteBytes(
reinterpret_cast<const char*>(code->InstructionStart(cage_base)),
code->InstructionSize(cage_base));
}
#if V8_ENABLE_WEBASSEMBLY
void LowLevelLogger::LogRecordedBuffer(const wasm::WasmCode* code,
const char* name, int length) {
CodeCreateStruct event;
event.name_size = length;
event.code_address = code->instruction_start();
event.code_size = code->instructions().length();
LogWriteStruct(event);
LogWriteBytes(name, length);
LogWriteBytes(reinterpret_cast<const char*>(code->instruction_start()),
code->instructions().length());
}
#endif // V8_ENABLE_WEBASSEMBLY
void LowLevelLogger::CodeMoveEvent(Tagged<InstructionStream> from,
Tagged<InstructionStream> to) {
CodeMoveStruct event;
event.from_address = from->instruction_start();
event.to_address = to->instruction_start();
LogWriteStruct(event);
}
void LowLevelLogger::BytecodeMoveEvent(Tagged<BytecodeArray> from,
Tagged<BytecodeArray> to) {
CodeMoveStruct event;
event.from_address = from->GetFirstBytecodeAddress();
event.to_address = to->GetFirstBytecodeAddress();
LogWriteStruct(event);
}
void LowLevelLogger::LogWriteBytes(const char* bytes, int size) {
size_t rv = fwrite(bytes, 1, size, ll_output_handle_);
DCHECK(static_cast<size_t>(size) == rv);
USE(rv);
}
void LowLevelLogger::CodeMovingGCEvent() {
const char tag = kCodeMovingGCTag;
LogWriteBytes(&tag, sizeof(tag));
}
class JitLogger : public CodeEventLogger {
public:
JitLogger(Isolate* isolate, JitCodeEventHandler code_event_handler);
void CodeMoveEvent(Tagged<InstructionStream> from,
Tagged<InstructionStream> to) override;
void BytecodeMoveEvent(Tagged<BytecodeArray> from,
Tagged<BytecodeArray> to) override;
void CodeDisableOptEvent(Handle<AbstractCode> code,
Handle<SharedFunctionInfo> shared) override {}
void AddCodeLinePosInfoEvent(void* jit_handler_data, int pc_offset,
int position,
JitCodeEvent::PositionType position_type,
JitCodeEvent::CodeType code_type);
void* StartCodePosInfoEvent(JitCodeEvent::CodeType code_type);
void EndCodePosInfoEvent(Address start_address, void* jit_handler_data,
JitCodeEvent::CodeType code_type);
private:
void LogRecordedBuffer(Tagged<AbstractCode> code,
MaybeHandle<SharedFunctionInfo> maybe_shared,
const char* name, int length) override;
#if V8_ENABLE_WEBASSEMBLY
void LogRecordedBuffer(const wasm::WasmCode* code, const char* name,
int length) override;
#endif // V8_ENABLE_WEBASSEMBLY
JitCodeEventHandler code_event_handler_;
base::Mutex logger_mutex_;
};
JitLogger::JitLogger(Isolate* isolate, JitCodeEventHandler code_event_handler)
: CodeEventLogger(isolate), code_event_handler_(code_event_handler) {
DCHECK_NOT_NULL(code_event_handler);
}
void JitLogger::LogRecordedBuffer(Tagged<AbstractCode> code,
MaybeHandle<SharedFunctionInfo> maybe_shared,
const char* name, int length) {
DisallowGarbageCollection no_gc;
PtrComprCageBase cage_base(isolate_);
JitCodeEvent event;
event.type = JitCodeEvent::CODE_ADDED;
event.code_start = reinterpret_cast<void*>(code->InstructionStart(cage_base));
event.code_type = IsCode(code, cage_base) ? JitCodeEvent::JIT_CODE
: JitCodeEvent::BYTE_CODE;
event.code_len = code->InstructionSize(cage_base);
Handle<SharedFunctionInfo> shared;
if (maybe_shared.ToHandle(&shared) &&
IsScript(shared->script(cage_base), cage_base)) {
event.script = ToApiHandle<v8::UnboundScript>(shared);
} else {
event.script = Local<v8::UnboundScript>();
}
event.name.str = name;
event.name.len = length;
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);
code_event_handler_(&event);
}
#if V8_ENABLE_WEBASSEMBLY
void JitLogger::LogRecordedBuffer(const wasm::WasmCode* code, const char* name,
int length) {
JitCodeEvent event;
event.type = JitCodeEvent::CODE_ADDED;
event.code_type = JitCodeEvent::WASM_CODE;
event.code_start = code->instructions().begin();
event.code_len = code->instructions().length();
event.name.str = name;
event.name.len = length;
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);
wasm::WasmModuleSourceMap* source_map =
code->native_module()->GetWasmSourceMap();
wasm::WireBytesRef code_ref =
code->native_module()->module()->functions[code->index()].code;
uint32_t code_offset = code_ref.offset();
uint32_t code_end_offset = code_ref.end_offset();
std::vector<v8::JitCodeEvent::line_info_t> mapping_info;
std::string filename;
std::unique_ptr<JitCodeEvent::wasm_source_info_t> wasm_source_info;
if (source_map && source_map->IsValid() &&
source_map->HasSource(code_offset, code_end_offset)) {
size_t last_line_number = 0;
for (SourcePositionTableIterator iterator(code->source_positions());
!iterator.done(); iterator.Advance()) {
uint32_t offset = iterator.source_position().ScriptOffset() + code_offset;
if (!source_map->HasValidEntry(code_offset, offset)) continue;
if (filename.empty()) {
filename = source_map->GetFilename(offset);
}
mapping_info.push_back({static_cast<size_t>(iterator.code_offset()),
last_line_number, JitCodeEvent::POSITION});
last_line_number = source_map->GetSourceLine(offset) + 1;
}
wasm_source_info = std::make_unique<JitCodeEvent::wasm_source_info_t>();
wasm_source_info->filename = filename.c_str();
wasm_source_info->filename_size = filename.size();
wasm_source_info->line_number_table_size = mapping_info.size();
wasm_source_info->line_number_table = mapping_info.data();
event.wasm_source_info = wasm_source_info.get();
}
code_event_handler_(&event);
}
#endif // V8_ENABLE_WEBASSEMBLY
void JitLogger::CodeMoveEvent(Tagged<InstructionStream> from,
Tagged<InstructionStream> to) {
base::MutexGuard guard(&logger_mutex_);
Tagged<Code> code;
if (!from->TryGetCodeUnchecked(&code, kAcquireLoad)) {
// Not yet fully initialized and no CodeCreateEvent has been emitted yet.
return;
}
JitCodeEvent event;
event.type = JitCodeEvent::CODE_MOVED;
event.code_type = JitCodeEvent::JIT_CODE;
event.code_start = reinterpret_cast<void*>(from->instruction_start());
event.code_len = code->instruction_size();
event.new_code_start = reinterpret_cast<void*>(to->instruction_start());
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);
code_event_handler_(&event);
}
void JitLogger::BytecodeMoveEvent(Tagged<BytecodeArray> from,
Tagged<BytecodeArray> to) {
base::MutexGuard guard(&logger_mutex_);
JitCodeEvent event;
event.type = JitCodeEvent::CODE_MOVED;
event.code_type = JitCodeEvent::BYTE_CODE;
event.code_start = reinterpret_cast<void*>(from->GetFirstBytecodeAddress());
event.code_len = from->length();
event.new_code_start = reinterpret_cast<void*>(to->GetFirstBytecodeAddress());
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);
code_event_handler_(&event);
}
void JitLogger::AddCodeLinePosInfoEvent(
void* jit_handler_data, int pc_offset, int position,
JitCodeEvent::PositionType position_type,
JitCodeEvent::CodeType code_type) {
JitCodeEvent event;
event.type = JitCodeEvent::CODE_ADD_LINE_POS_INFO;
event.code_type = code_type;
event.user_data = jit_handler_data;
event.line_info.offset = pc_offset;
event.line_info.pos = position;
event.line_info.position_type = position_type;
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);
code_event_handler_(&event);
}
void* JitLogger::StartCodePosInfoEvent(JitCodeEvent::CodeType code_type) {
JitCodeEvent event;
event.type = JitCodeEvent::CODE_START_LINE_INFO_RECORDING;
event.code_type = code_type;
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);
code_event_handler_(&event);
return event.user_data;
}
void JitLogger::EndCodePosInfoEvent(Address start_address,
void* jit_handler_data,
JitCodeEvent::CodeType code_type) {
JitCodeEvent event;
event.type = JitCodeEvent::CODE_END_LINE_INFO_RECORDING;
event.code_type = code_type;
event.code_start = reinterpret_cast<void*>(start_address);
event.user_data = jit_handler_data;
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);