blob: 3a4d79b8da09e08b15a0dd9e6406d635878ea7e3 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 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
4// met:
5//
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.
15//
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.
27
28#ifndef V8_LOG_H_
29#define V8_LOG_H_
30
31#include "platform.h"
32#include "log-utils.h"
33
34namespace v8 {
35namespace internal {
36
37// Logger is used for collecting logging information from V8 during
38// execution. The result is dumped to a file.
39//
40// Available command line flags:
41//
42// --log
43// Minimal logging (no API, code, or GC sample events), default is off.
44//
45// --log-all
46// Log all events to the file, default is off. This is the same as combining
47// --log-api, --log-code, --log-gc, and --log-regexp.
48//
49// --log-api
50// Log API events to the logfile, default is off. --log-api implies --log.
51//
52// --log-code
53// Log code (create, move, and delete) events to the logfile, default is off.
54// --log-code implies --log.
55//
56// --log-gc
57// Log GC heap samples after each GC that can be processed by hp2ps, default
58// is off. --log-gc implies --log.
59//
60// --log-regexp
61// Log creation and use of regular expressions, Default is off.
62// --log-regexp implies --log.
63//
64// --logfile <filename>
65// Specify the name of the logfile, default is "v8.log".
66//
67// --prof
68// Collect statistical profiling information (ticks), default is off. The
69// tick profiler requires code events, so --prof implies --log-code.
70
71// Forward declarations.
72class Ticker;
73class Profiler;
74class Semaphore;
75class SlidingStateWindow;
76class LogMessageBuilder;
77class CompressionHelper;
78
79#undef LOG
80#ifdef ENABLE_LOGGING_AND_PROFILING
81#define LOG(Call) \
82 do { \
83 if (v8::internal::Logger::is_logging()) \
84 v8::internal::Logger::Call; \
85 } while (false)
86#else
87#define LOG(Call) ((void) 0)
88#endif
89
Steve Blocka7e24c12009-10-30 11:49:00 +000090#define LOG_EVENTS_AND_TAGS_LIST(V) \
91 V(CODE_CREATION_EVENT, "code-creation", "cc") \
92 V(CODE_MOVE_EVENT, "code-move", "cm") \
93 V(CODE_DELETE_EVENT, "code-delete", "cd") \
Ben Murdochf87a2032010-10-22 12:50:53 +010094 V(CODE_MOVING_GC, "code-moving-gc", "cg") \
Leon Clarked91b9f72010-01-27 17:25:45 +000095 V(FUNCTION_CREATION_EVENT, "function-creation", "fc") \
96 V(FUNCTION_MOVE_EVENT, "function-move", "fm") \
97 V(FUNCTION_DELETE_EVENT, "function-delete", "fd") \
Leon Clarkee46be812010-01-19 14:06:41 +000098 V(SNAPSHOT_POSITION_EVENT, "snapshot-pos", "sp") \
Steve Blocka7e24c12009-10-30 11:49:00 +000099 V(TICK_EVENT, "tick", "t") \
100 V(REPEAT_META_EVENT, "repeat", "r") \
101 V(BUILTIN_TAG, "Builtin", "bi") \
102 V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak", "cdb") \
103 V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn", "cdbsi") \
104 V(CALL_IC_TAG, "CallIC", "cic") \
105 V(CALL_INITIALIZE_TAG, "CallInitialize", "ci") \
106 V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic", "cmm") \
107 V(CALL_MISS_TAG, "CallMiss", "cm") \
108 V(CALL_NORMAL_TAG, "CallNormal", "cn") \
109 V(CALL_PRE_MONOMORPHIC_TAG, "CallPreMonomorphic", "cpm") \
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100110 V(KEYED_CALL_DEBUG_BREAK_TAG, "KeyedCallDebugBreak", "kcdb") \
111 V(KEYED_CALL_DEBUG_PREPARE_STEP_IN_TAG, \
112 "KeyedCallDebugPrepareStepIn", \
113 "kcdbsi") \
114 V(KEYED_CALL_IC_TAG, "KeyedCallIC", "kcic") \
115 V(KEYED_CALL_INITIALIZE_TAG, "KeyedCallInitialize", "kci") \
116 V(KEYED_CALL_MEGAMORPHIC_TAG, "KeyedCallMegamorphic", "kcmm") \
117 V(KEYED_CALL_MISS_TAG, "KeyedCallMiss", "kcm") \
118 V(KEYED_CALL_NORMAL_TAG, "KeyedCallNormal", "kcn") \
119 V(KEYED_CALL_PRE_MONOMORPHIC_TAG, \
120 "KeyedCallPreMonomorphic", \
121 "kcpm") \
Steve Blockd0582a62009-12-15 09:54:21 +0000122 V(CALLBACK_TAG, "Callback", "cb") \
Steve Blocka7e24c12009-10-30 11:49:00 +0000123 V(EVAL_TAG, "Eval", "e") \
124 V(FUNCTION_TAG, "Function", "f") \
125 V(KEYED_LOAD_IC_TAG, "KeyedLoadIC", "klic") \
126 V(KEYED_STORE_IC_TAG, "KeyedStoreIC", "ksic") \
127 V(LAZY_COMPILE_TAG, "LazyCompile", "lc") \
128 V(LOAD_IC_TAG, "LoadIC", "lic") \
129 V(REG_EXP_TAG, "RegExp", "re") \
130 V(SCRIPT_TAG, "Script", "sc") \
131 V(STORE_IC_TAG, "StoreIC", "sic") \
Steve Block6ded16b2010-05-10 14:33:55 +0100132 V(STUB_TAG, "Stub", "s") \
133 V(NATIVE_FUNCTION_TAG, "Function", "f") \
134 V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile", "lc") \
135 V(NATIVE_SCRIPT_TAG, "Script", "sc")
136// Note that 'NATIVE_' cases for functions and scripts are mapped onto
137// original tags when writing to the log.
138
Steve Blocka7e24c12009-10-30 11:49:00 +0000139
140class Logger {
141 public:
142#define DECLARE_ENUM(enum_item, ignore1, ignore2) enum_item,
143 enum LogEventsAndTags {
144 LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM)
145 NUMBER_OF_LOG_EVENTS
146 };
147#undef DECLARE_ENUM
148
149 // Acquires resources for logging if the right flags are set.
150 static bool Setup();
151
152 // Frees resources acquired in Setup.
153 static void TearDown();
154
155 // Enable the computation of a sliding window of states.
156 static void EnableSlidingStateWindow();
157
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 // Emits an event with a string value -> (name, value).
159 static void StringEvent(const char* name, const char* value);
160
161 // Emits an event with an int value -> (name, value).
162 static void IntEvent(const char* name, int value);
Ben Murdochf87a2032010-10-22 12:50:53 +0100163 static void IntPtrTEvent(const char* name, intptr_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000164
165 // Emits an event with an handle value -> (name, location).
166 static void HandleEvent(const char* name, Object** location);
167
168 // Emits memory management events for C allocated structures.
169 static void NewEvent(const char* name, void* object, size_t size);
170 static void DeleteEvent(const char* name, void* object);
171
172 // Emits an event with a tag, and some resource usage information.
173 // -> (name, tag, <rusage information>).
174 // Currently, the resource usage information is a process time stamp
175 // and a real time timestamp.
176 static void ResourceEvent(const char* name, const char* tag);
177
178 // Emits an event that an undefined property was read from an
179 // object.
180 static void SuspectReadEvent(String* name, Object* obj);
181
182 // Emits an event when a message is put on or read from a debugging queue.
183 // DebugTag lets us put a call-site specific label on the event.
184 static void DebugTag(const char* call_site_tag);
185 static void DebugEvent(const char* event_type, Vector<uint16_t> parameter);
186
187
188 // ==== Events logged by --log-api. ====
189 static void ApiNamedSecurityCheck(Object* key);
190 static void ApiIndexedSecurityCheck(uint32_t index);
191 static void ApiNamedPropertyAccess(const char* tag,
192 JSObject* holder,
193 Object* name);
194 static void ApiIndexedPropertyAccess(const char* tag,
195 JSObject* holder,
196 uint32_t index);
197 static void ApiObjectAccess(const char* tag, JSObject* obj);
198 static void ApiEntryCall(const char* name);
199
200
201 // ==== Events logged by --log-code. ====
Steve Blockd0582a62009-12-15 09:54:21 +0000202 // Emits a code event for a callback function.
203 static void CallbackEvent(String* name, Address entry_point);
204 static void GetterCallbackEvent(String* name, Address entry_point);
205 static void SetterCallbackEvent(String* name, Address entry_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 // Emits a code create event.
207 static void CodeCreateEvent(LogEventsAndTags tag,
208 Code* code, const char* source);
209 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, String* name);
210 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, String* name,
211 String* source, int line);
212 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count);
Ben Murdochf87a2032010-10-22 12:50:53 +0100213 static void CodeMovingGCEvent();
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 // Emits a code create event for a RegExp.
215 static void RegExpCodeCreateEvent(Code* code, String* source);
216 // Emits a code move event.
217 static void CodeMoveEvent(Address from, Address to);
218 // Emits a code delete event.
219 static void CodeDeleteEvent(Address from);
Leon Clarked91b9f72010-01-27 17:25:45 +0000220 // Emits a function object create event.
221 static void FunctionCreateEvent(JSFunction* function);
Ben Murdochf87a2032010-10-22 12:50:53 +0100222 static void FunctionCreateEventFromMove(JSFunction* function);
Leon Clarked91b9f72010-01-27 17:25:45 +0000223 // Emits a function move event.
224 static void FunctionMoveEvent(Address from, Address to);
225 // Emits a function delete event.
226 static void FunctionDeleteEvent(Address from);
Steve Blocka7e24c12009-10-30 11:49:00 +0000227
Leon Clarkee46be812010-01-19 14:06:41 +0000228 static void SnapshotPositionEvent(Address addr, int pos);
229
Steve Blocka7e24c12009-10-30 11:49:00 +0000230 // ==== Events logged by --log-gc. ====
231 // Heap sampling events: start, end, and individual types.
232 static void HeapSampleBeginEvent(const char* space, const char* kind);
233 static void HeapSampleEndEvent(const char* space, const char* kind);
234 static void HeapSampleItemEvent(const char* type, int number, int bytes);
235 static void HeapSampleJSConstructorEvent(const char* constructor,
236 int number, int bytes);
237 static void HeapSampleJSRetainersEvent(const char* constructor,
238 const char* event);
Steve Block3ce2e202009-11-05 08:53:23 +0000239 static void HeapSampleJSProducerEvent(const char* constructor,
240 Address* stack);
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 static void HeapSampleStats(const char* space, const char* kind,
Ben Murdochf87a2032010-10-22 12:50:53 +0100242 intptr_t capacity, intptr_t used);
Steve Blocka7e24c12009-10-30 11:49:00 +0000243
244 static void SharedLibraryEvent(const char* library_path,
245 uintptr_t start,
246 uintptr_t end);
247 static void SharedLibraryEvent(const wchar_t* library_path,
248 uintptr_t start,
249 uintptr_t end);
250
251 // ==== Events logged by --log-regexp ====
252 // Regexp compilation and execution events.
253
254 static void RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache);
255
256 // Log an event reported from generated code
257 static void LogRuntime(Vector<const char> format, JSArray* args);
258
259#ifdef ENABLE_LOGGING_AND_PROFILING
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 static bool is_logging() {
Steve Block6ded16b2010-05-10 14:33:55 +0100261 return logging_nesting_ > 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 }
263
264 // Pause/Resume collection of profiling data.
265 // When data collection is paused, CPU Tick events are discarded until
266 // data collection is Resumed.
Andrei Popescu402d9372010-02-26 13:31:12 +0000267 static void PauseProfiler(int flags, int tag);
268 static void ResumeProfiler(int flags, int tag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000269 static int GetActiveProfilerModules();
270
271 // If logging is performed into a memory buffer, allows to
272 // retrieve previously written messages. See v8.h.
273 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
274
275 // Logs all compiled functions found in the heap.
276 static void LogCompiledFunctions();
Leon Clarked91b9f72010-01-27 17:25:45 +0000277 // Logs all compiled JSFunction objects found in the heap.
278 static void LogFunctionObjects();
Steve Blockd0582a62009-12-15 09:54:21 +0000279 // Logs all accessor callbacks found in the heap.
280 static void LogAccessorCallbacks();
281 // Used for logging stubs found in the snapshot.
Andrei Popescu31002712010-02-23 13:46:05 +0000282 static void LogCodeObjects();
Steve Blocka7e24c12009-10-30 11:49:00 +0000283
Steve Block6ded16b2010-05-10 14:33:55 +0100284 // Converts tag to a corresponding NATIVE_... if the script is native.
285 INLINE(static LogEventsAndTags ToNativeByScript(LogEventsAndTags, Script*));
Steve Blocka7e24c12009-10-30 11:49:00 +0000286
287 // Profiler's sampling interval (in milliseconds).
288 static const int kSamplingIntervalMs = 1;
289
Steve Block6ded16b2010-05-10 14:33:55 +0100290 private:
291
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 // Size of window used for log records compression.
293 static const int kCompressionWindowSize = 4;
294
295 // Emits the profiler's first message.
296 static void ProfilerBeginEvent();
297
Steve Blockd0582a62009-12-15 09:54:21 +0000298 // Emits callback event messages.
299 static void CallbackEventInternal(const char* prefix,
300 const char* name,
301 Address entry_point);
302
Leon Clarked91b9f72010-01-27 17:25:45 +0000303 // Internal configurable move event.
304 static void MoveEventInternal(LogEventsAndTags event,
305 Address from,
306 Address to);
307
308 // Internal configurable move event.
309 static void DeleteEventInternal(LogEventsAndTags event,
310 Address from);
311
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 // Emits aliases for compressed messages.
313 static void LogAliases();
314
315 // Emits the source code of a regexp. Used by regexp events.
316 static void LogRegExpSource(Handle<JSRegExp> regexp);
317
Andrei Popescu31002712010-02-23 13:46:05 +0000318 // Used for logging stubs found in the snapshot.
319 static void LogCodeObject(Object* code_object);
320
Ben Murdochf87a2032010-10-22 12:50:53 +0100321 // Emits general information about generated code.
322 static void LogCodeInfo();
323
324 // Handles code creation when low-level profiling is active.
325 static void LowLevelCodeCreateEvent(Code* code, LogMessageBuilder* msg);
326
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 // Emits a profiler tick event. Used by the profiler thread.
328 static void TickEvent(TickSample* sample, bool overflow);
329
330 static void ApiEvent(const char* name, ...);
331
332 // Logs a StringEvent regardless of whether FLAG_log is true.
333 static void UncheckedStringEvent(const char* name, const char* value);
334
Steve Block6ded16b2010-05-10 14:33:55 +0100335 // Logs an IntEvent regardless of whether FLAG_log is true.
336 static void UncheckedIntEvent(const char* name, int value);
Ben Murdochf87a2032010-10-22 12:50:53 +0100337 static void UncheckedIntPtrTEvent(const char* name, intptr_t value);
Steve Block6ded16b2010-05-10 14:33:55 +0100338
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 // Stops logging and profiling in case of insufficient resources.
340 static void StopLoggingAndProfiling();
341
342 // Returns whether profiler's sampler is active.
343 static bool IsProfilerSamplerActive();
344
345 // The sampler used by the profiler and the sliding state window.
346 static Ticker* ticker_;
347
348 // When the statistical profile is active, profiler_
349 // points to a Profiler, that handles collection
350 // of samples.
351 static Profiler* profiler_;
352
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 // SlidingStateWindow instance keeping a sliding window of the most
354 // recent VM states.
355 static SlidingStateWindow* sliding_state_window_;
356
357 // An array of log events names.
358 static const char** log_events_;
359
360 // An instance of helper created if log compression is enabled.
361 static CompressionHelper* compression_helper_;
362
363 // Internal implementation classes with access to
364 // private members.
365 friend class CompressionHelper;
366 friend class EventLog;
367 friend class TimeLog;
368 friend class Profiler;
369 friend class SlidingStateWindow;
Steve Blockd0582a62009-12-15 09:54:21 +0000370 friend class StackTracer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 friend class VMState;
372
373 friend class LoggerTestHelper;
374
Steve Block6ded16b2010-05-10 14:33:55 +0100375 static int logging_nesting_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000376 static int cpu_profiler_nesting_;
377 static int heap_profiler_nesting_;
Steve Block6ded16b2010-05-10 14:33:55 +0100378
379 friend class CpuProfiler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000380#else
381 static bool is_logging() { return false; }
382#endif
383};
384
385
386// Class that extracts stack trace, used for profiling.
387class StackTracer : public AllStatic {
388 public:
389 static void Trace(TickSample* sample);
390};
391
Steve Blocka7e24c12009-10-30 11:49:00 +0000392} } // namespace v8::internal
393
Steve Block6ded16b2010-05-10 14:33:55 +0100394
Steve Blocka7e24c12009-10-30 11:49:00 +0000395#endif // V8_LOG_H_