1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "src/allocation.h"
11 #include "src/base/platform/elapsed-timer.h"
12 #include "src/base/platform/platform.h"
13 #include "src/objects.h"
23 // Logger is used for collecting logging information from V8 during
24 // execution. The result is dumped to a file.
26 // Available command line flags:
29 // Minimal logging (no API, code, or GC sample events), default is off.
32 // Log all events to the file, default is off. This is the same as combining
33 // --log-api, --log-code, --log-gc, and --log-regexp.
36 // Log API events to the logfile, default is off. --log-api implies --log.
39 // Log code (create, move, and delete) events to the logfile, default is off.
40 // --log-code implies --log.
43 // Log GC heap samples after each GC that can be processed by hp2ps, default
44 // is off. --log-gc implies --log.
47 // Log creation and use of regular expressions, Default is off.
48 // --log-regexp implies --log.
50 // --logfile <filename>
51 // Specify the name of the logfile, default is "v8.log".
54 // Collect statistical profiling information (ticks), default is off. The
55 // tick profiler requires code events, so --prof implies --log-code.
57 // Forward declarations.
58 class CodeEventListener;
59 class CompilationInfo;
63 class PositionsRecorder;
69 #define LOG(isolate, Call) \
71 v8::internal::Logger* logger = \
72 (isolate)->logger(); \
73 if (logger->is_logging()) \
77 #define LOG_CODE_EVENT(isolate, Call) \
79 v8::internal::Logger* logger = \
80 (isolate)->logger(); \
81 if (logger->is_logging_code_events()) \
86 #define LOG_EVENTS_AND_TAGS_LIST(V) \
87 V(CODE_CREATION_EVENT, "code-creation") \
88 V(CODE_DISABLE_OPT_EVENT, "code-disable-optimization") \
89 V(CODE_MOVE_EVENT, "code-move") \
90 V(CODE_DELETE_EVENT, "code-delete") \
91 V(CODE_MOVING_GC, "code-moving-gc") \
92 V(SHARED_FUNC_MOVE_EVENT, "sfi-move") \
93 V(SNAPSHOT_POSITION_EVENT, "snapshot-pos") \
94 V(SNAPSHOT_CODE_NAME_EVENT, "snapshot-code-name") \
95 V(TICK_EVENT, "tick") \
96 V(REPEAT_META_EVENT, "repeat") \
97 V(BUILTIN_TAG, "Builtin") \
98 V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak") \
99 V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn") \
100 V(CALL_INITIALIZE_TAG, "CallInitialize") \
101 V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic") \
102 V(CALL_MISS_TAG, "CallMiss") \
103 V(CALL_NORMAL_TAG, "CallNormal") \
104 V(CALL_PRE_MONOMORPHIC_TAG, "CallPreMonomorphic") \
105 V(LOAD_INITIALIZE_TAG, "LoadInitialize") \
106 V(LOAD_PREMONOMORPHIC_TAG, "LoadPreMonomorphic") \
107 V(LOAD_MEGAMORPHIC_TAG, "LoadMegamorphic") \
108 V(STORE_INITIALIZE_TAG, "StoreInitialize") \
109 V(STORE_PREMONOMORPHIC_TAG, "StorePreMonomorphic") \
110 V(STORE_GENERIC_TAG, "StoreGeneric") \
111 V(STORE_MEGAMORPHIC_TAG, "StoreMegamorphic") \
112 V(KEYED_CALL_DEBUG_BREAK_TAG, "KeyedCallDebugBreak") \
113 V(KEYED_CALL_DEBUG_PREPARE_STEP_IN_TAG, \
114 "KeyedCallDebugPrepareStepIn") \
115 V(KEYED_CALL_INITIALIZE_TAG, "KeyedCallInitialize") \
116 V(KEYED_CALL_MEGAMORPHIC_TAG, "KeyedCallMegamorphic") \
117 V(KEYED_CALL_MISS_TAG, "KeyedCallMiss") \
118 V(KEYED_CALL_NORMAL_TAG, "KeyedCallNormal") \
119 V(KEYED_CALL_PRE_MONOMORPHIC_TAG, "KeyedCallPreMonomorphic") \
120 V(CALLBACK_TAG, "Callback") \
121 V(EVAL_TAG, "Eval") \
122 V(FUNCTION_TAG, "Function") \
123 V(HANDLER_TAG, "Handler") \
124 V(KEYED_LOAD_IC_TAG, "KeyedLoadIC") \
125 V(KEYED_LOAD_POLYMORPHIC_IC_TAG, "KeyedLoadPolymorphicIC") \
126 V(KEYED_EXTERNAL_ARRAY_LOAD_IC_TAG, "KeyedExternalArrayLoadIC") \
127 V(KEYED_STORE_IC_TAG, "KeyedStoreIC") \
128 V(KEYED_STORE_POLYMORPHIC_IC_TAG, "KeyedStorePolymorphicIC") \
129 V(KEYED_EXTERNAL_ARRAY_STORE_IC_TAG, "KeyedExternalArrayStoreIC") \
130 V(LAZY_COMPILE_TAG, "LazyCompile") \
131 V(CALL_IC_TAG, "CallIC") \
132 V(LOAD_IC_TAG, "LoadIC") \
133 V(LOAD_POLYMORPHIC_IC_TAG, "LoadPolymorphicIC") \
134 V(REG_EXP_TAG, "RegExp") \
135 V(SCRIPT_TAG, "Script") \
136 V(STORE_IC_TAG, "StoreIC") \
137 V(STORE_POLYMORPHIC_IC_TAG, "StorePolymorphicIC") \
138 V(STUB_TAG, "Stub") \
139 V(NATIVE_FUNCTION_TAG, "Function") \
140 V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile") \
141 V(NATIVE_SCRIPT_TAG, "Script")
142 // Note that 'NATIVE_' cases for functions and scripts are mapped onto
143 // original tags when writing to the log.
147 class PerfBasicLogger;
148 class LowLevelLogger;
154 enum StartEnd { START = 0, END = 1 };
156 #define DECLARE_ENUM(enum_item, ignore) enum_item,
157 enum LogEventsAndTags {
158 LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM)
163 // Acquires resources for logging if the right flags are set.
164 bool SetUp(Isolate* isolate);
166 // Sets the current code event handler.
167 void SetCodeEventHandler(uint32_t options,
168 JitCodeEventHandler event_handler);
172 // Frees resources acquired in SetUp.
173 // When a temporary file is used for the log, returns its stream descriptor,
174 // leaving the file open.
177 // Emits an event with a string value -> (name, value).
178 void StringEvent(const char* name, const char* value);
180 // Emits an event with an int value -> (name, value).
181 void IntEvent(const char* name, int value);
182 void IntPtrTEvent(const char* name, intptr_t value);
184 // Emits an event with an handle value -> (name, location).
185 void HandleEvent(const char* name, Object** location);
187 // Emits memory management events for C allocated structures.
188 void NewEvent(const char* name, void* object, size_t size);
189 void DeleteEvent(const char* name, void* object);
191 // Static versions of the above, operate on current isolate's logger.
192 // Used in TRACK_MEMORY(TypeName) defined in globals.h
193 static void NewEventStatic(const char* name, void* object, size_t size);
194 static void DeleteEventStatic(const char* name, void* object);
196 // Emits an event with a tag, and some resource usage information.
197 // -> (name, tag, <rusage information>).
198 // Currently, the resource usage information is a process time stamp
199 // and a real time timestamp.
200 void ResourceEvent(const char* name, const char* tag);
202 // Emits an event that an undefined property was read from an
204 void SuspectReadEvent(Name* name, Object* obj);
206 // Emits an event when a message is put on or read from a debugging queue.
207 // DebugTag lets us put a call-site specific label on the event.
208 void DebugTag(const char* call_site_tag);
209 void DebugEvent(const char* event_type, Vector<uint16_t> parameter);
212 // ==== Events logged by --log-api. ====
213 void ApiNamedSecurityCheck(Object* key);
214 void ApiIndexedSecurityCheck(uint32_t index);
215 void ApiNamedPropertyAccess(const char* tag, JSObject* holder, Object* name);
216 void ApiIndexedPropertyAccess(const char* tag,
219 void ApiObjectAccess(const char* tag, JSObject* obj);
220 void ApiEntryCall(const char* name);
223 // ==== Events logged by --log-code. ====
224 void addCodeEventListener(CodeEventListener* listener);
225 void removeCodeEventListener(CodeEventListener* listener);
226 bool hasCodeEventListener(CodeEventListener* listener);
229 // Emits a code event for a callback function.
230 void CallbackEvent(Name* name, Address entry_point);
231 void GetterCallbackEvent(Name* name, Address entry_point);
232 void SetterCallbackEvent(Name* name, Address entry_point);
233 // Emits a code create event.
234 void CodeCreateEvent(LogEventsAndTags tag,
235 Code* code, const char* source);
236 void CodeCreateEvent(LogEventsAndTags tag,
237 Code* code, Name* name);
238 void CodeCreateEvent(LogEventsAndTags tag,
240 SharedFunctionInfo* shared,
241 CompilationInfo* info,
243 void CodeCreateEvent(LogEventsAndTags tag,
245 SharedFunctionInfo* shared,
246 CompilationInfo* info,
247 Name* source, int line, int column);
248 void CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count);
249 // Emits a code deoptimization event.
250 void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared);
251 void CodeMovingGCEvent();
252 // Emits a code create event for a RegExp.
253 void RegExpCodeCreateEvent(Code* code, String* source);
254 // Emits a code move event.
255 void CodeMoveEvent(Address from, Address to);
256 // Emits a code delete event.
257 void CodeDeleteEvent(Address from);
258 // Emits a code line info add event with Postion type.
259 void CodeLinePosInfoAddPositionEvent(void* jit_handler_data,
262 // Emits a code line info add event with StatementPostion type.
263 void CodeLinePosInfoAddStatementPositionEvent(void* jit_handler_data,
266 // Emits a code line info start to record event
267 void CodeStartLinePosInfoRecordEvent(PositionsRecorder* pos_recorder);
268 // Emits a code line info finish record event.
269 // It's the callee's responsibility to dispose the parameter jit_handler_data.
270 void CodeEndLinePosInfoRecordEvent(Code* code, void* jit_handler_data);
272 void SharedFunctionInfoMoveEvent(Address from, Address to);
274 void CodeNameEvent(Address addr, int pos, const char* code_name);
275 void SnapshotPositionEvent(Address addr, int pos);
277 // ==== Events logged by --log-gc. ====
278 // Heap sampling events: start, end, and individual types.
279 void HeapSampleBeginEvent(const char* space, const char* kind);
280 void HeapSampleEndEvent(const char* space, const char* kind);
281 void HeapSampleItemEvent(const char* type, int number, int bytes);
282 void HeapSampleJSConstructorEvent(const char* constructor,
283 int number, int bytes);
284 void HeapSampleJSRetainersEvent(const char* constructor,
286 void HeapSampleJSProducerEvent(const char* constructor,
288 void HeapSampleStats(const char* space, const char* kind,
289 intptr_t capacity, intptr_t used);
291 void SharedLibraryEvent(const std::string& library_path,
295 void CodeDeoptEvent(Code* code, int bailout_id, Address from,
297 void CurrentTimeEvent();
299 void TimerEvent(StartEnd se, const char* name);
301 static void EnterExternal(Isolate* isolate);
302 static void LeaveExternal(Isolate* isolate);
304 static void DefaultEventLoggerSentinel(const char* name, int event) {}
306 INLINE(static void CallEventLogger(Isolate* isolate, const char* name,
307 StartEnd se, bool expose_to_api));
309 // ==== Events logged by --log-regexp ====
310 // Regexp compilation and execution events.
312 void RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache);
318 bool is_logging_code_events() {
319 return is_logging() || jit_logger_ != NULL;
322 // Stop collection of profiling data.
323 // When data collection is paused, CPU Tick events are discarded.
326 void LogExistingFunction(Handle<SharedFunctionInfo> shared,
328 // Logs all compiled functions found in the heap.
329 void LogCompiledFunctions();
330 // Logs all accessor callbacks found in the heap.
331 void LogAccessorCallbacks();
332 // Used for logging stubs found in the snapshot.
333 void LogCodeObjects();
335 // Converts tag to a corresponding NATIVE_... if the script is native.
336 INLINE(static LogEventsAndTags ToNativeByScript(LogEventsAndTags, Script*));
338 // Profiler's sampling interval (in milliseconds).
340 // Phones and tablets have processors that are much slower than desktop
341 // and laptop computers for which current heuristics are tuned.
342 static const int kSamplingIntervalMs = 5;
344 static const int kSamplingIntervalMs = 1;
347 // Callback from Log, stops profiling in case of insufficient resources.
351 explicit Logger(Isolate* isolate);
354 // Emits the profiler's first message.
355 void ProfilerBeginEvent();
357 // Emits callback event messages.
358 void CallbackEventInternal(const char* prefix,
360 Address entry_point);
362 // Internal configurable move event.
363 void MoveEventInternal(LogEventsAndTags event, Address from, Address to);
365 // Emits the source code of a regexp. Used by regexp events.
366 void LogRegExpSource(Handle<JSRegExp> regexp);
368 // Used for logging stubs found in the snapshot.
369 void LogCodeObject(Object* code_object);
371 // Helper method. It resets name_buffer_ and add tag name into it.
372 void InitNameBuffer(LogEventsAndTags tag);
374 // Emits a profiler tick event. Used by the profiler thread.
375 void TickEvent(TickSample* sample, bool overflow);
377 void ApiEvent(const char* name, ...);
379 // Logs a StringEvent regardless of whether FLAG_log is true.
380 void UncheckedStringEvent(const char* name, const char* value);
382 // Logs an IntEvent regardless of whether FLAG_log is true.
383 void UncheckedIntEvent(const char* name, int value);
384 void UncheckedIntPtrTEvent(const char* name, intptr_t value);
388 // The sampler used by the profiler and the sliding state window.
391 // When the statistical profile is active, profiler_
392 // points to a Profiler, that handles collection
396 // An array of log events names.
397 const char* const* log_events_;
399 // Internal implementation classes with access to
401 friend class EventLog;
402 friend class Isolate;
403 friend class TimeLog;
404 friend class Profiler;
405 template <StateTag Tag> friend class VMState;
406 friend class LoggerTestHelper;
410 PerfBasicLogger* perf_basic_logger_;
411 PerfJitLogger* perf_jit_logger_;
412 LowLevelLogger* ll_logger_;
413 JitLogger* jit_logger_;
414 List<CodeEventListener*> listeners_;
416 // Guards against multiple calls to TearDown() that can happen in some tests.
417 // 'true' between SetUp() and TearDown().
418 bool is_initialized_;
420 base::ElapsedTimer timer_;
422 friend class CpuProfiler;
426 #define TIMER_EVENTS_LIST(V) \
427 V(RecompileSynchronous, true) \
428 V(RecompileConcurrent, true) \
429 V(CompileFullCode, true) \
434 #define V(TimerName, expose) \
435 class TimerEvent##TimerName : public AllStatic { \
437 static const char* name(void* unused = NULL) { return "V8." #TimerName; } \
438 static bool expose_to_api() { return expose; } \
444 template <class TimerEvent>
445 class TimerEventScope {
447 explicit TimerEventScope(Isolate* isolate) : isolate_(isolate) {
448 LogTimerEvent(Logger::START);
451 ~TimerEventScope() { LogTimerEvent(Logger::END); }
453 void LogTimerEvent(Logger::StartEnd se);
460 class CodeEventListener {
462 virtual ~CodeEventListener() {}
464 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
466 const char* comment) = 0;
467 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
470 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
472 SharedFunctionInfo* shared,
473 CompilationInfo* info,
475 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
477 SharedFunctionInfo* shared,
478 CompilationInfo* info,
480 int line, int column) = 0;
481 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
484 virtual void CallbackEvent(Name* name, Address entry_point) = 0;
485 virtual void GetterCallbackEvent(Name* name, Address entry_point) = 0;
486 virtual void SetterCallbackEvent(Name* name, Address entry_point) = 0;
487 virtual void RegExpCodeCreateEvent(Code* code, String* source) = 0;
488 virtual void CodeMoveEvent(Address from, Address to) = 0;
489 virtual void CodeDeleteEvent(Address from) = 0;
490 virtual void SharedFunctionInfoMoveEvent(Address from, Address to) = 0;
491 virtual void CodeMovingGCEvent() = 0;
492 virtual void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared) = 0;
496 class CodeEventLogger : public CodeEventListener {
499 virtual ~CodeEventLogger();
501 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
503 const char* comment);
504 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
507 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
510 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
512 SharedFunctionInfo* shared,
513 CompilationInfo* info,
515 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
517 SharedFunctionInfo* shared,
518 CompilationInfo* info,
520 int line, int column);
521 virtual void RegExpCodeCreateEvent(Code* code, String* source);
523 virtual void CallbackEvent(Name* name, Address entry_point) { }
524 virtual void GetterCallbackEvent(Name* name, Address entry_point) { }
525 virtual void SetterCallbackEvent(Name* name, Address entry_point) { }
526 virtual void SharedFunctionInfoMoveEvent(Address from, Address to) { }
527 virtual void CodeMovingGCEvent() { }
532 virtual void LogRecordedBuffer(Code* code,
533 SharedFunctionInfo* shared,
537 NameBuffer* name_buffer_;
541 } } // namespace v8::internal