1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
31 #include "allocation.h"
34 #include "platform/elapsed-timer.h"
39 // Logger is used for collecting logging information from V8 during
40 // execution. The result is dumped to a file.
42 // Available command line flags:
45 // Minimal logging (no API, code, or GC sample events), default is off.
48 // Log all events to the file, default is off. This is the same as combining
49 // --log-api, --log-code, --log-gc, and --log-regexp.
52 // Log API events to the logfile, default is off. --log-api implies --log.
55 // Log code (create, move, and delete) events to the logfile, default is off.
56 // --log-code implies --log.
59 // Log GC heap samples after each GC that can be processed by hp2ps, default
60 // is off. --log-gc implies --log.
63 // Log creation and use of regular expressions, Default is off.
64 // --log-regexp implies --log.
66 // --logfile <filename>
67 // Specify the name of the logfile, default is "v8.log".
70 // Collect statistical profiling information (ticks), default is off. The
71 // tick profiler requires code events, so --prof implies --log-code.
73 // Forward declarations.
74 class CodeEventListener;
75 class CompilationInfo;
79 class PositionsRecorder;
86 #define LOG(isolate, Call) \
88 v8::internal::Logger* logger = \
89 (isolate)->logger(); \
90 if (logger->is_logging()) \
94 #define LOG_CODE_EVENT(isolate, Call) \
96 v8::internal::Logger* logger = \
97 (isolate)->logger(); \
98 if (logger->is_logging_code_events()) \
103 #define LOG_EVENTS_AND_TAGS_LIST(V) \
104 V(CODE_CREATION_EVENT, "code-creation") \
105 V(CODE_MOVE_EVENT, "code-move") \
106 V(CODE_DELETE_EVENT, "code-delete") \
107 V(CODE_MOVING_GC, "code-moving-gc") \
108 V(SHARED_FUNC_MOVE_EVENT, "sfi-move") \
109 V(SNAPSHOT_POSITION_EVENT, "snapshot-pos") \
110 V(SNAPSHOT_CODE_NAME_EVENT, "snapshot-code-name") \
111 V(TICK_EVENT, "tick") \
112 V(REPEAT_META_EVENT, "repeat") \
113 V(BUILTIN_TAG, "Builtin") \
114 V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak") \
115 V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn") \
116 V(CALL_IC_TAG, "CallIC") \
117 V(CALL_INITIALIZE_TAG, "CallInitialize") \
118 V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic") \
119 V(CALL_MISS_TAG, "CallMiss") \
120 V(CALL_NORMAL_TAG, "CallNormal") \
121 V(CALL_PRE_MONOMORPHIC_TAG, "CallPreMonomorphic") \
122 V(LOAD_INITIALIZE_TAG, "LoadInitialize") \
123 V(LOAD_PREMONOMORPHIC_TAG, "LoadPreMonomorphic") \
124 V(LOAD_MEGAMORPHIC_TAG, "LoadMegamorphic") \
125 V(STORE_INITIALIZE_TAG, "StoreInitialize") \
126 V(STORE_PREMONOMORPHIC_TAG, "StorePreMonomorphic") \
127 V(STORE_GENERIC_TAG, "StoreGeneric") \
128 V(STORE_MEGAMORPHIC_TAG, "StoreMegamorphic") \
129 V(KEYED_CALL_DEBUG_BREAK_TAG, "KeyedCallDebugBreak") \
130 V(KEYED_CALL_DEBUG_PREPARE_STEP_IN_TAG, \
131 "KeyedCallDebugPrepareStepIn") \
132 V(KEYED_CALL_IC_TAG, "KeyedCallIC") \
133 V(KEYED_CALL_INITIALIZE_TAG, "KeyedCallInitialize") \
134 V(KEYED_CALL_MEGAMORPHIC_TAG, "KeyedCallMegamorphic") \
135 V(KEYED_CALL_MISS_TAG, "KeyedCallMiss") \
136 V(KEYED_CALL_NORMAL_TAG, "KeyedCallNormal") \
137 V(KEYED_CALL_PRE_MONOMORPHIC_TAG, "KeyedCallPreMonomorphic") \
138 V(CALLBACK_TAG, "Callback") \
139 V(EVAL_TAG, "Eval") \
140 V(FUNCTION_TAG, "Function") \
141 V(HANDLER_TAG, "Handler") \
142 V(KEYED_LOAD_IC_TAG, "KeyedLoadIC") \
143 V(KEYED_LOAD_POLYMORPHIC_IC_TAG, "KeyedLoadPolymorphicIC") \
144 V(KEYED_EXTERNAL_ARRAY_LOAD_IC_TAG, "KeyedExternalArrayLoadIC") \
145 V(KEYED_STORE_IC_TAG, "KeyedStoreIC") \
146 V(KEYED_STORE_POLYMORPHIC_IC_TAG, "KeyedStorePolymorphicIC") \
147 V(KEYED_EXTERNAL_ARRAY_STORE_IC_TAG, "KeyedExternalArrayStoreIC") \
148 V(LAZY_COMPILE_TAG, "LazyCompile") \
149 V(LOAD_IC_TAG, "LoadIC") \
150 V(LOAD_POLYMORPHIC_IC_TAG, "LoadPolymorphicIC") \
151 V(REG_EXP_TAG, "RegExp") \
152 V(SCRIPT_TAG, "Script") \
153 V(STORE_IC_TAG, "StoreIC") \
154 V(STORE_POLYMORPHIC_IC_TAG, "StorePolymorphicIC") \
155 V(STUB_TAG, "Stub") \
156 V(NATIVE_FUNCTION_TAG, "Function") \
157 V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile") \
158 V(NATIVE_SCRIPT_TAG, "Script")
159 // Note that 'NATIVE_' cases for functions and scripts are mapped onto
160 // original tags when writing to the log.
164 class PerfBasicLogger;
165 class LowLevelLogger;
171 #define DECLARE_ENUM(enum_item, ignore) enum_item,
172 enum LogEventsAndTags {
173 LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM)
178 // Acquires resources for logging if the right flags are set.
179 bool SetUp(Isolate* isolate);
181 // Sets the current code event handler.
182 void SetCodeEventHandler(uint32_t options,
183 JitCodeEventHandler event_handler);
187 // Frees resources acquired in SetUp.
188 // When a temporary file is used for the log, returns its stream descriptor,
189 // leaving the file open.
192 // Emits an event with a string value -> (name, value).
193 void StringEvent(const char* name, const char* value);
195 // Emits an event with an int value -> (name, value).
196 void IntEvent(const char* name, int value);
197 void IntPtrTEvent(const char* name, intptr_t value);
199 // Emits an event with an handle value -> (name, location).
200 void HandleEvent(const char* name, Object** location);
202 // Emits memory management events for C allocated structures.
203 void NewEvent(const char* name, void* object, size_t size);
204 void DeleteEvent(const char* name, void* object);
206 // Static versions of the above, operate on current isolate's logger.
207 // Used in TRACK_MEMORY(TypeName) defined in globals.h
208 static void NewEventStatic(const char* name, void* object, size_t size);
209 static void DeleteEventStatic(const char* name, void* object);
211 // Emits an event with a tag, and some resource usage information.
212 // -> (name, tag, <rusage information>).
213 // Currently, the resource usage information is a process time stamp
214 // and a real time timestamp.
215 void ResourceEvent(const char* name, const char* tag);
217 // Emits an event that an undefined property was read from an
219 void SuspectReadEvent(Name* name, Object* obj);
221 // Emits an event when a message is put on or read from a debugging queue.
222 // DebugTag lets us put a call-site specific label on the event.
223 void DebugTag(const char* call_site_tag);
224 void DebugEvent(const char* event_type, Vector<uint16_t> parameter);
227 // ==== Events logged by --log-api. ====
228 void ApiNamedSecurityCheck(Object* key);
229 void ApiIndexedSecurityCheck(uint32_t index);
230 void ApiNamedPropertyAccess(const char* tag, JSObject* holder, Object* name);
231 void ApiIndexedPropertyAccess(const char* tag,
234 void ApiObjectAccess(const char* tag, JSObject* obj);
235 void ApiEntryCall(const char* name);
238 // ==== Events logged by --log-code. ====
239 void addCodeEventListener(CodeEventListener* listener);
240 void removeCodeEventListener(CodeEventListener* listener);
241 bool hasCodeEventListener(CodeEventListener* listener);
244 // Emits a code event for a callback function.
245 void CallbackEvent(Name* name, Address entry_point);
246 void GetterCallbackEvent(Name* name, Address entry_point);
247 void SetterCallbackEvent(Name* name, Address entry_point);
248 // Emits a code create event.
249 void CodeCreateEvent(LogEventsAndTags tag,
250 Code* code, const char* source);
251 void CodeCreateEvent(LogEventsAndTags tag,
252 Code* code, Name* name);
253 void CodeCreateEvent(LogEventsAndTags tag,
255 SharedFunctionInfo* shared,
256 CompilationInfo* info,
258 void CodeCreateEvent(LogEventsAndTags tag,
260 SharedFunctionInfo* shared,
261 CompilationInfo* info,
262 Name* source, int line, int column);
263 void CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count);
264 void CodeMovingGCEvent();
265 // Emits a code create event for a RegExp.
266 void RegExpCodeCreateEvent(Code* code, String* source);
267 // Emits a code move event.
268 void CodeMoveEvent(Address from, Address to);
269 // Emits a code delete event.
270 void CodeDeleteEvent(Address from);
271 // Emits a code line info add event with Postion type.
272 void CodeLinePosInfoAddPositionEvent(void* jit_handler_data,
275 // Emits a code line info add event with StatementPostion type.
276 void CodeLinePosInfoAddStatementPositionEvent(void* jit_handler_data,
279 // Emits a code line info start to record event
280 void CodeStartLinePosInfoRecordEvent(PositionsRecorder* pos_recorder);
281 // Emits a code line info finish record event.
282 // It's the callee's responsibility to dispose the parameter jit_handler_data.
283 void CodeEndLinePosInfoRecordEvent(Code* code, void* jit_handler_data);
285 void SharedFunctionInfoMoveEvent(Address from, Address to);
287 void CodeNameEvent(Address addr, int pos, const char* code_name);
288 void SnapshotPositionEvent(Address addr, int pos);
290 // ==== Events logged by --log-gc. ====
291 // Heap sampling events: start, end, and individual types.
292 void HeapSampleBeginEvent(const char* space, const char* kind);
293 void HeapSampleEndEvent(const char* space, const char* kind);
294 void HeapSampleItemEvent(const char* type, int number, int bytes);
295 void HeapSampleJSConstructorEvent(const char* constructor,
296 int number, int bytes);
297 void HeapSampleJSRetainersEvent(const char* constructor,
299 void HeapSampleJSProducerEvent(const char* constructor,
301 void HeapSampleStats(const char* space, const char* kind,
302 intptr_t capacity, intptr_t used);
304 void SharedLibraryEvent(const char* library_path,
307 void SharedLibraryEvent(const wchar_t* library_path,
311 // ==== Events logged by --log-timer-events. ====
312 enum StartEnd { START, END };
314 void CodeDeoptEvent(Code* code);
316 void TimerEvent(StartEnd se, const char* name);
318 static void EnterExternal(Isolate* isolate);
319 static void LeaveExternal(Isolate* isolate);
321 class TimerEventScope {
323 TimerEventScope(Isolate* isolate, const char* name)
324 : isolate_(isolate), name_(name) {
325 if (FLAG_log_internal_timer_events) LogTimerEvent(START);
329 if (FLAG_log_internal_timer_events) LogTimerEvent(END);
332 void LogTimerEvent(StartEnd se);
334 static const char* v8_recompile_synchronous;
335 static const char* v8_recompile_concurrent;
336 static const char* v8_compile_full_code;
337 static const char* v8_execute;
338 static const char* v8_external;
345 // ==== Events logged by --log-regexp ====
346 // Regexp compilation and execution events.
348 void RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache);
350 // Log an event reported from generated code
351 void LogRuntime(Vector<const char> format, JSArray* args);
357 bool is_logging_code_events() {
358 return is_logging() || jit_logger_ != NULL;
361 // Stop collection of profiling data.
362 // When data collection is paused, CPU Tick events are discarded.
365 void LogExistingFunction(Handle<SharedFunctionInfo> shared,
367 // Logs all compiled functions found in the heap.
368 void LogCompiledFunctions();
369 // Logs all accessor callbacks found in the heap.
370 void LogAccessorCallbacks();
371 // Used for logging stubs found in the snapshot.
372 void LogCodeObjects();
374 // Converts tag to a corresponding NATIVE_... if the script is native.
375 INLINE(static LogEventsAndTags ToNativeByScript(LogEventsAndTags, Script*));
377 // Profiler's sampling interval (in milliseconds).
379 // Phones and tablets have processors that are much slower than desktop
380 // and laptop computers for which current heuristics are tuned.
381 static const int kSamplingIntervalMs = 5;
383 static const int kSamplingIntervalMs = 1;
386 // Callback from Log, stops profiling in case of insufficient resources.
390 explicit Logger(Isolate* isolate);
393 // Emits the profiler's first message.
394 void ProfilerBeginEvent();
396 // Emits callback event messages.
397 void CallbackEventInternal(const char* prefix,
399 Address entry_point);
401 // Internal configurable move event.
402 void MoveEventInternal(LogEventsAndTags event, Address from, Address to);
404 // Emits the source code of a regexp. Used by regexp events.
405 void LogRegExpSource(Handle<JSRegExp> regexp);
407 // Used for logging stubs found in the snapshot.
408 void LogCodeObject(Object* code_object);
410 // Helper method. It resets name_buffer_ and add tag name into it.
411 void InitNameBuffer(LogEventsAndTags tag);
413 // Emits a profiler tick event. Used by the profiler thread.
414 void TickEvent(TickSample* sample, bool overflow);
416 void ApiEvent(const char* name, ...);
418 // Logs a StringEvent regardless of whether FLAG_log is true.
419 void UncheckedStringEvent(const char* name, const char* value);
421 // Logs an IntEvent regardless of whether FLAG_log is true.
422 void UncheckedIntEvent(const char* name, int value);
423 void UncheckedIntPtrTEvent(const char* name, intptr_t value);
427 // The sampler used by the profiler and the sliding state window.
430 // When the statistical profile is active, profiler_
431 // points to a Profiler, that handles collection
435 // An array of log events names.
436 const char* const* log_events_;
438 // Internal implementation classes with access to
440 friend class EventLog;
441 friend class Isolate;
442 friend class TimeLog;
443 friend class Profiler;
444 template <StateTag Tag> friend class VMState;
445 friend class LoggerTestHelper;
449 PerfBasicLogger* perf_basic_logger_;
450 PerfJitLogger* perf_jit_logger_;
451 LowLevelLogger* ll_logger_;
452 JitLogger* jit_logger_;
453 List<CodeEventListener*> listeners_;
455 // Guards against multiple calls to TearDown() that can happen in some tests.
456 // 'true' between SetUp() and TearDown().
457 bool is_initialized_;
461 friend class CpuProfiler;
465 class CodeEventListener {
467 virtual ~CodeEventListener() {}
469 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
471 const char* comment) = 0;
472 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
475 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
477 SharedFunctionInfo* shared,
478 CompilationInfo* info,
480 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
482 SharedFunctionInfo* shared,
483 CompilationInfo* info,
485 int line, int column) = 0;
486 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
489 virtual void CallbackEvent(Name* name, Address entry_point) = 0;
490 virtual void GetterCallbackEvent(Name* name, Address entry_point) = 0;
491 virtual void SetterCallbackEvent(Name* name, Address entry_point) = 0;
492 virtual void RegExpCodeCreateEvent(Code* code, String* source) = 0;
493 virtual void CodeMoveEvent(Address from, Address to) = 0;
494 virtual void CodeDeleteEvent(Address from) = 0;
495 virtual void SharedFunctionInfoMoveEvent(Address from, Address to) = 0;
496 virtual void CodeMovingGCEvent() = 0;
500 class CodeEventLogger : public CodeEventListener {
503 virtual ~CodeEventLogger();
505 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
507 const char* comment);
508 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
511 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
514 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
516 SharedFunctionInfo* shared,
517 CompilationInfo* info,
519 virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
521 SharedFunctionInfo* shared,
522 CompilationInfo* info,
524 int line, int column);
525 virtual void RegExpCodeCreateEvent(Code* code, String* source);
527 virtual void CallbackEvent(Name* name, Address entry_point) { }
528 virtual void GetterCallbackEvent(Name* name, Address entry_point) { }
529 virtual void SetterCallbackEvent(Name* name, Address entry_point) { }
530 virtual void SharedFunctionInfoMoveEvent(Address from, Address to) { }
531 virtual void CodeMovingGCEvent() { }
536 virtual void LogRecordedBuffer(Code* code,
537 SharedFunctionInfo* shared,
541 NameBuffer* name_buffer_;
545 } } // namespace v8::internal