deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / log.h
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.
4
5 #ifndef V8_LOG_H_
6 #define V8_LOG_H_
7
8 #include <string>
9
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"
14
15 namespace v8 {
16
17 namespace base {
18 class Semaphore;
19 }
20
21 namespace internal {
22
23 // Logger is used for collecting logging information from V8 during
24 // execution. The result is dumped to a file.
25 //
26 // Available command line flags:
27 //
28 //  --log
29 // Minimal logging (no API, code, or GC sample events), default is off.
30 //
31 // --log-all
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.
34 //
35 // --log-api
36 // Log API events to the logfile, default is off.  --log-api implies --log.
37 //
38 // --log-code
39 // Log code (create, move, and delete) events to the logfile, default is off.
40 // --log-code implies --log.
41 //
42 // --log-gc
43 // Log GC heap samples after each GC that can be processed by hp2ps, default
44 // is off.  --log-gc implies --log.
45 //
46 // --log-regexp
47 // Log creation and use of regular expressions, Default is off.
48 // --log-regexp implies --log.
49 //
50 // --logfile <filename>
51 // Specify the name of the logfile, default is "v8.log".
52 //
53 // --prof
54 // Collect statistical profiling information (ticks), default is off.  The
55 // tick profiler requires code events, so --prof implies --log-code.
56
57 // Forward declarations.
58 class CodeEventListener;
59 class CompilationInfo;
60 class CpuProfiler;
61 class Isolate;
62 class Log;
63 class PositionsRecorder;
64 class Profiler;
65 class Ticker;
66 struct TickSample;
67
68 #undef LOG
69 #define LOG(isolate, Call)                          \
70   do {                                              \
71     v8::internal::Logger* logger =                  \
72         (isolate)->logger();                        \
73     if (logger->is_logging())                       \
74       logger->Call;                                 \
75   } while (false)
76
77 #define LOG_CODE_EVENT(isolate, Call)               \
78   do {                                              \
79     v8::internal::Logger* logger =                  \
80         (isolate)->logger();                        \
81     if (logger->is_logging_code_events())           \
82       logger->Call;                                 \
83   } while (false)
84
85
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.
144
145
146 class JitLogger;
147 class PerfBasicLogger;
148 class LowLevelLogger;
149 class PerfJitLogger;
150 class Sampler;
151
152 class Logger {
153  public:
154   enum StartEnd { START = 0, END = 1 };
155
156 #define DECLARE_ENUM(enum_item, ignore) enum_item,
157   enum LogEventsAndTags {
158     LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM)
159     NUMBER_OF_LOG_EVENTS
160   };
161 #undef DECLARE_ENUM
162
163   // Acquires resources for logging if the right flags are set.
164   bool SetUp(Isolate* isolate);
165
166   // Sets the current code event handler.
167   void SetCodeEventHandler(uint32_t options,
168                            JitCodeEventHandler event_handler);
169
170   Sampler* sampler();
171
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.
175   FILE* TearDown();
176
177   // Emits an event with a string value -> (name, value).
178   void StringEvent(const char* name, const char* value);
179
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);
183
184   // Emits an event with an handle value -> (name, location).
185   void HandleEvent(const char* name, Object** location);
186
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);
190
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);
195
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);
201
202   // Emits an event that an undefined property was read from an
203   // object.
204   void SuspectReadEvent(Name* name, Object* obj);
205
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);
210
211
212   // ==== Events logged by --log-api. ====
213   void ApiSecurityCheck();
214   void ApiNamedPropertyAccess(const char* tag, JSObject* holder, Object* name);
215   void ApiIndexedPropertyAccess(const char* tag,
216                                 JSObject* holder,
217                                 uint32_t index);
218   void ApiObjectAccess(const char* tag, JSObject* obj);
219   void ApiEntryCall(const char* name);
220
221
222   // ==== Events logged by --log-code. ====
223   void addCodeEventListener(CodeEventListener* listener);
224   void removeCodeEventListener(CodeEventListener* listener);
225   bool hasCodeEventListener(CodeEventListener* listener);
226
227
228   // Emits a code event for a callback function.
229   void CallbackEvent(Name* name, Address entry_point);
230   void GetterCallbackEvent(Name* name, Address entry_point);
231   void SetterCallbackEvent(Name* name, Address entry_point);
232   // Emits a code create event.
233   void CodeCreateEvent(LogEventsAndTags tag,
234                        Code* code, const char* source);
235   void CodeCreateEvent(LogEventsAndTags tag,
236                        Code* code, Name* name);
237   void CodeCreateEvent(LogEventsAndTags tag,
238                        Code* code,
239                        SharedFunctionInfo* shared,
240                        CompilationInfo* info,
241                        Name* name);
242   void CodeCreateEvent(LogEventsAndTags tag,
243                        Code* code,
244                        SharedFunctionInfo* shared,
245                        CompilationInfo* info,
246                        Name* source, int line, int column);
247   void CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count);
248   // Emits a code deoptimization event.
249   void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared);
250   void CodeMovingGCEvent();
251   // Emits a code create event for a RegExp.
252   void RegExpCodeCreateEvent(Code* code, String* source);
253   // Emits a code move event.
254   void CodeMoveEvent(Address from, Address to);
255   // Emits a code delete event.
256   void CodeDeleteEvent(Address from);
257   // Emits a code line info add event with Postion type.
258   void CodeLinePosInfoAddPositionEvent(void* jit_handler_data,
259                                        int pc_offset,
260                                        int position);
261   // Emits a code line info add event with StatementPostion type.
262   void CodeLinePosInfoAddStatementPositionEvent(void* jit_handler_data,
263                                                 int pc_offset,
264                                                 int position);
265   // Emits a code line info start to record event
266   void CodeStartLinePosInfoRecordEvent(PositionsRecorder* pos_recorder);
267   // Emits a code line info finish record event.
268   // It's the callee's responsibility to dispose the parameter jit_handler_data.
269   void CodeEndLinePosInfoRecordEvent(Code* code, void* jit_handler_data);
270
271   void SharedFunctionInfoMoveEvent(Address from, Address to);
272
273   void CodeNameEvent(Address addr, int pos, const char* code_name);
274   void SnapshotPositionEvent(Address addr, int pos);
275
276   // ==== Events logged by --log-gc. ====
277   // Heap sampling events: start, end, and individual types.
278   void HeapSampleBeginEvent(const char* space, const char* kind);
279   void HeapSampleEndEvent(const char* space, const char* kind);
280   void HeapSampleItemEvent(const char* type, int number, int bytes);
281   void HeapSampleJSConstructorEvent(const char* constructor,
282                                     int number, int bytes);
283   void HeapSampleJSRetainersEvent(const char* constructor,
284                                          const char* event);
285   void HeapSampleJSProducerEvent(const char* constructor,
286                                  Address* stack);
287   void HeapSampleStats(const char* space, const char* kind,
288                        intptr_t capacity, intptr_t used);
289
290   void SharedLibraryEvent(const std::string& library_path,
291                           uintptr_t start,
292                           uintptr_t end);
293
294   void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta);
295   void CurrentTimeEvent();
296
297   void TimerEvent(StartEnd se, const char* name);
298
299   static void EnterExternal(Isolate* isolate);
300   static void LeaveExternal(Isolate* isolate);
301
302   static void DefaultEventLoggerSentinel(const char* name, int event) {}
303
304   INLINE(static void CallEventLogger(Isolate* isolate, const char* name,
305                                      StartEnd se, bool expose_to_api));
306
307   // ==== Events logged by --log-regexp ====
308   // Regexp compilation and execution events.
309
310   void RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache);
311
312   bool is_logging() {
313     return is_logging_;
314   }
315
316   bool is_logging_code_events() {
317     return is_logging() || jit_logger_ != NULL;
318   }
319
320   // Stop collection of profiling data.
321   // When data collection is paused, CPU Tick events are discarded.
322   void StopProfiler();
323
324   void LogExistingFunction(Handle<SharedFunctionInfo> shared,
325                            Handle<Code> code);
326   // Logs all compiled functions found in the heap.
327   void LogCompiledFunctions();
328   // Logs all accessor callbacks found in the heap.
329   void LogAccessorCallbacks();
330   // Used for logging stubs found in the snapshot.
331   void LogCodeObjects();
332
333   // Converts tag to a corresponding NATIVE_... if the script is native.
334   INLINE(static LogEventsAndTags ToNativeByScript(LogEventsAndTags, Script*));
335
336   // Profiler's sampling interval (in milliseconds).
337 #if defined(ANDROID)
338   // Phones and tablets have processors that are much slower than desktop
339   // and laptop computers for which current heuristics are tuned.
340   static const int kSamplingIntervalMs = 5;
341 #else
342   static const int kSamplingIntervalMs = 1;
343 #endif
344
345   // Callback from Log, stops profiling in case of insufficient resources.
346   void LogFailure();
347
348  private:
349   explicit Logger(Isolate* isolate);
350   ~Logger();
351
352   // Emits the profiler's first message.
353   void ProfilerBeginEvent();
354
355   // Emits callback event messages.
356   void CallbackEventInternal(const char* prefix,
357                              Name* name,
358                              Address entry_point);
359
360   // Internal configurable move event.
361   void MoveEventInternal(LogEventsAndTags event, Address from, Address to);
362
363   // Emits the source code of a regexp. Used by regexp events.
364   void LogRegExpSource(Handle<JSRegExp> regexp);
365
366   // Used for logging stubs found in the snapshot.
367   void LogCodeObject(Object* code_object);
368
369   // Helper method. It resets name_buffer_ and add tag name into it.
370   void InitNameBuffer(LogEventsAndTags tag);
371
372   // Emits a profiler tick event. Used by the profiler thread.
373   void TickEvent(TickSample* sample, bool overflow);
374
375   void ApiEvent(const char* name, ...);
376
377   // Logs a StringEvent regardless of whether FLAG_log is true.
378   void UncheckedStringEvent(const char* name, const char* value);
379
380   // Logs an IntEvent regardless of whether FLAG_log is true.
381   void UncheckedIntEvent(const char* name, int value);
382   void UncheckedIntPtrTEvent(const char* name, intptr_t value);
383
384   Isolate* isolate_;
385
386   // The sampler used by the profiler and the sliding state window.
387   Ticker* ticker_;
388
389   // When the statistical profile is active, profiler_
390   // points to a Profiler, that handles collection
391   // of samples.
392   Profiler* profiler_;
393
394   // An array of log events names.
395   const char* const* log_events_;
396
397   // Internal implementation classes with access to
398   // private members.
399   friend class EventLog;
400   friend class Isolate;
401   friend class TimeLog;
402   friend class Profiler;
403   template <StateTag Tag> friend class VMState;
404   friend class LoggerTestHelper;
405
406   bool is_logging_;
407   Log* log_;
408   PerfBasicLogger* perf_basic_logger_;
409   PerfJitLogger* perf_jit_logger_;
410   LowLevelLogger* ll_logger_;
411   JitLogger* jit_logger_;
412   List<CodeEventListener*> listeners_;
413
414   // Guards against multiple calls to TearDown() that can happen in some tests.
415   // 'true' between SetUp() and TearDown().
416   bool is_initialized_;
417
418   base::ElapsedTimer timer_;
419
420   friend class CpuProfiler;
421 };
422
423
424 #define TIMER_EVENTS_LIST(V)    \
425   V(RecompileSynchronous, true) \
426   V(RecompileConcurrent, true)  \
427   V(CompileFullCode, true)      \
428   V(Execute, true)              \
429   V(External, true)             \
430   V(IcMiss, false)
431
432 #define V(TimerName, expose)                                                  \
433   class TimerEvent##TimerName : public AllStatic {                            \
434    public:                                                                    \
435     static const char* name(void* unused = NULL) { return "V8." #TimerName; } \
436     static bool expose_to_api() { return expose; }                            \
437   };
438 TIMER_EVENTS_LIST(V)
439 #undef V
440
441
442 template <class TimerEvent>
443 class TimerEventScope {
444  public:
445   explicit TimerEventScope(Isolate* isolate) : isolate_(isolate) {
446     LogTimerEvent(Logger::START);
447   }
448
449   ~TimerEventScope() { LogTimerEvent(Logger::END); }
450
451   void LogTimerEvent(Logger::StartEnd se);
452
453  private:
454   Isolate* isolate_;
455 };
456
457
458 class CodeEventListener {
459  public:
460   virtual ~CodeEventListener() {}
461
462   virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
463                                Code* code,
464                                const char* comment) = 0;
465   virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
466                                Code* code,
467                                Name* name) = 0;
468   virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
469                                Code* code,
470                                SharedFunctionInfo* shared,
471                                CompilationInfo* info,
472                                Name* name) = 0;
473   virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
474                                Code* code,
475                                SharedFunctionInfo* shared,
476                                CompilationInfo* info,
477                                Name* source,
478                                int line, int column) = 0;
479   virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
480                                Code* code,
481                                int args_count) = 0;
482   virtual void CallbackEvent(Name* name, Address entry_point) = 0;
483   virtual void GetterCallbackEvent(Name* name, Address entry_point) = 0;
484   virtual void SetterCallbackEvent(Name* name, Address entry_point) = 0;
485   virtual void RegExpCodeCreateEvent(Code* code, String* source) = 0;
486   virtual void CodeMoveEvent(Address from, Address to) = 0;
487   virtual void CodeDeleteEvent(Address from) = 0;
488   virtual void SharedFunctionInfoMoveEvent(Address from, Address to) = 0;
489   virtual void CodeMovingGCEvent() = 0;
490   virtual void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared) = 0;
491 };
492
493
494 class CodeEventLogger : public CodeEventListener {
495  public:
496   CodeEventLogger();
497   virtual ~CodeEventLogger();
498
499   virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
500                                Code* code,
501                                const char* comment);
502   virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
503                                Code* code,
504                                Name* name);
505   virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
506                                Code* code,
507                                int args_count);
508   virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
509                                Code* code,
510                                SharedFunctionInfo* shared,
511                                CompilationInfo* info,
512                                Name* name);
513   virtual void CodeCreateEvent(Logger::LogEventsAndTags tag,
514                                Code* code,
515                                SharedFunctionInfo* shared,
516                                CompilationInfo* info,
517                                Name* source,
518                                int line, int column);
519   virtual void RegExpCodeCreateEvent(Code* code, String* source);
520
521   virtual void CallbackEvent(Name* name, Address entry_point) { }
522   virtual void GetterCallbackEvent(Name* name, Address entry_point) { }
523   virtual void SetterCallbackEvent(Name* name, Address entry_point) { }
524   virtual void SharedFunctionInfoMoveEvent(Address from, Address to) { }
525   virtual void CodeMovingGCEvent() { }
526
527  private:
528   class NameBuffer;
529
530   virtual void LogRecordedBuffer(Code* code,
531                                  SharedFunctionInfo* shared,
532                                  const char* name,
533                                  int length) = 0;
534
535   NameBuffer* name_buffer_;
536 };
537
538
539 } }  // namespace v8::internal
540
541
542 #endif  // V8_LOG_H_