blob: a808cd1d4c54e9511a14c12b0157c006f76098e3 [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;
Steve Blocka7e24c12009-10-30 11:49:00 +000077
78#undef LOG
79#ifdef ENABLE_LOGGING_AND_PROFILING
80#define LOG(Call) \
81 do { \
82 if (v8::internal::Logger::is_logging()) \
83 v8::internal::Logger::Call; \
84 } while (false)
85#else
86#define LOG(Call) ((void) 0)
87#endif
88
Steve Blocka7e24c12009-10-30 11:49:00 +000089#define LOG_EVENTS_AND_TAGS_LIST(V) \
Ben Murdochb0fe1622011-05-05 13:52:32 +010090 V(CODE_CREATION_EVENT, "code-creation") \
91 V(CODE_MOVE_EVENT, "code-move") \
92 V(CODE_DELETE_EVENT, "code-delete") \
93 V(CODE_MOVING_GC, "code-moving-gc") \
Ben Murdoche0cee9b2011-05-25 10:26:03 +010094 V(SFI_MOVE_EVENT, "sfi-move") \
Ben Murdochb0fe1622011-05-05 13:52:32 +010095 V(SNAPSHOT_POSITION_EVENT, "snapshot-pos") \
96 V(TICK_EVENT, "tick") \
97 V(REPEAT_META_EVENT, "repeat") \
98 V(BUILTIN_TAG, "Builtin") \
99 V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak") \
100 V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn") \
101 V(CALL_IC_TAG, "CallIC") \
102 V(CALL_INITIALIZE_TAG, "CallInitialize") \
103 V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic") \
104 V(CALL_MISS_TAG, "CallMiss") \
105 V(CALL_NORMAL_TAG, "CallNormal") \
106 V(CALL_PRE_MONOMORPHIC_TAG, "CallPreMonomorphic") \
107 V(KEYED_CALL_DEBUG_BREAK_TAG, "KeyedCallDebugBreak") \
108 V(KEYED_CALL_DEBUG_PREPARE_STEP_IN_TAG, \
109 "KeyedCallDebugPrepareStepIn") \
110 V(KEYED_CALL_IC_TAG, "KeyedCallIC") \
111 V(KEYED_CALL_INITIALIZE_TAG, "KeyedCallInitialize") \
112 V(KEYED_CALL_MEGAMORPHIC_TAG, "KeyedCallMegamorphic") \
113 V(KEYED_CALL_MISS_TAG, "KeyedCallMiss") \
114 V(KEYED_CALL_NORMAL_TAG, "KeyedCallNormal") \
115 V(KEYED_CALL_PRE_MONOMORPHIC_TAG, "KeyedCallPreMonomorphic") \
116 V(CALLBACK_TAG, "Callback") \
117 V(EVAL_TAG, "Eval") \
118 V(FUNCTION_TAG, "Function") \
119 V(KEYED_LOAD_IC_TAG, "KeyedLoadIC") \
120 V(KEYED_STORE_IC_TAG, "KeyedStoreIC") \
121 V(LAZY_COMPILE_TAG, "LazyCompile") \
122 V(LOAD_IC_TAG, "LoadIC") \
123 V(REG_EXP_TAG, "RegExp") \
124 V(SCRIPT_TAG, "Script") \
125 V(STORE_IC_TAG, "StoreIC") \
126 V(STUB_TAG, "Stub") \
127 V(NATIVE_FUNCTION_TAG, "Function") \
128 V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile") \
129 V(NATIVE_SCRIPT_TAG, "Script")
Steve Block6ded16b2010-05-10 14:33:55 +0100130// Note that 'NATIVE_' cases for functions and scripts are mapped onto
131// original tags when writing to the log.
132
Steve Blocka7e24c12009-10-30 11:49:00 +0000133
134class Logger {
135 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100136#define DECLARE_ENUM(enum_item, ignore) enum_item,
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 enum LogEventsAndTags {
138 LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM)
139 NUMBER_OF_LOG_EVENTS
140 };
141#undef DECLARE_ENUM
142
143 // Acquires resources for logging if the right flags are set.
144 static bool Setup();
145
Ben Murdochb0fe1622011-05-05 13:52:32 +0100146 static void EnsureTickerStarted();
147 static void EnsureTickerStopped();
148
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 // Frees resources acquired in Setup.
150 static void TearDown();
151
152 // Enable the computation of a sliding window of states.
153 static void EnableSlidingStateWindow();
154
Steve Blocka7e24c12009-10-30 11:49:00 +0000155 // Emits an event with a string value -> (name, value).
156 static void StringEvent(const char* name, const char* value);
157
158 // Emits an event with an int value -> (name, value).
159 static void IntEvent(const char* name, int value);
Ben Murdochf87a2032010-10-22 12:50:53 +0100160 static void IntPtrTEvent(const char* name, intptr_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000161
162 // Emits an event with an handle value -> (name, location).
163 static void HandleEvent(const char* name, Object** location);
164
165 // Emits memory management events for C allocated structures.
166 static void NewEvent(const char* name, void* object, size_t size);
167 static void DeleteEvent(const char* name, void* object);
168
169 // Emits an event with a tag, and some resource usage information.
170 // -> (name, tag, <rusage information>).
171 // Currently, the resource usage information is a process time stamp
172 // and a real time timestamp.
173 static void ResourceEvent(const char* name, const char* tag);
174
175 // Emits an event that an undefined property was read from an
176 // object.
177 static void SuspectReadEvent(String* name, Object* obj);
178
179 // Emits an event when a message is put on or read from a debugging queue.
180 // DebugTag lets us put a call-site specific label on the event.
181 static void DebugTag(const char* call_site_tag);
182 static void DebugEvent(const char* event_type, Vector<uint16_t> parameter);
183
184
185 // ==== Events logged by --log-api. ====
186 static void ApiNamedSecurityCheck(Object* key);
187 static void ApiIndexedSecurityCheck(uint32_t index);
188 static void ApiNamedPropertyAccess(const char* tag,
189 JSObject* holder,
190 Object* name);
191 static void ApiIndexedPropertyAccess(const char* tag,
192 JSObject* holder,
193 uint32_t index);
194 static void ApiObjectAccess(const char* tag, JSObject* obj);
195 static void ApiEntryCall(const char* name);
196
197
198 // ==== Events logged by --log-code. ====
Steve Blockd0582a62009-12-15 09:54:21 +0000199 // Emits a code event for a callback function.
200 static void CallbackEvent(String* name, Address entry_point);
201 static void GetterCallbackEvent(String* name, Address entry_point);
202 static void SetterCallbackEvent(String* name, Address entry_point);
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 // Emits a code create event.
204 static void CodeCreateEvent(LogEventsAndTags tag,
205 Code* code, const char* source);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100206 static void CodeCreateEvent(LogEventsAndTags tag,
207 Code* code, String* name);
208 static void CodeCreateEvent(LogEventsAndTags tag,
209 Code* code,
210 SharedFunctionInfo* shared,
211 String* name);
212 static void CodeCreateEvent(LogEventsAndTags tag,
213 Code* code,
214 SharedFunctionInfo* shared,
Steve Blocka7e24c12009-10-30 11:49:00 +0000215 String* source, int line);
216 static void CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count);
Ben Murdochf87a2032010-10-22 12:50:53 +0100217 static void CodeMovingGCEvent();
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 // Emits a code create event for a RegExp.
219 static void RegExpCodeCreateEvent(Code* code, String* source);
220 // Emits a code move event.
221 static void CodeMoveEvent(Address from, Address to);
222 // Emits a code delete event.
223 static void CodeDeleteEvent(Address from);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100224
225 static void SFIMoveEvent(Address from, Address to);
Steve Blocka7e24c12009-10-30 11:49:00 +0000226
Leon Clarkee46be812010-01-19 14:06:41 +0000227 static void SnapshotPositionEvent(Address addr, int pos);
228
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 // ==== Events logged by --log-gc. ====
230 // Heap sampling events: start, end, and individual types.
231 static void HeapSampleBeginEvent(const char* space, const char* kind);
232 static void HeapSampleEndEvent(const char* space, const char* kind);
233 static void HeapSampleItemEvent(const char* type, int number, int bytes);
234 static void HeapSampleJSConstructorEvent(const char* constructor,
235 int number, int bytes);
236 static void HeapSampleJSRetainersEvent(const char* constructor,
237 const char* event);
Steve Block3ce2e202009-11-05 08:53:23 +0000238 static void HeapSampleJSProducerEvent(const char* constructor,
239 Address* stack);
Steve Blocka7e24c12009-10-30 11:49:00 +0000240 static void HeapSampleStats(const char* space, const char* kind,
Ben Murdochf87a2032010-10-22 12:50:53 +0100241 intptr_t capacity, intptr_t used);
Steve Blocka7e24c12009-10-30 11:49:00 +0000242
243 static void SharedLibraryEvent(const char* library_path,
244 uintptr_t start,
245 uintptr_t end);
246 static void SharedLibraryEvent(const wchar_t* library_path,
247 uintptr_t start,
248 uintptr_t end);
249
250 // ==== Events logged by --log-regexp ====
251 // Regexp compilation and execution events.
252
253 static void RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache);
254
255 // Log an event reported from generated code
256 static void LogRuntime(Vector<const char> format, JSArray* args);
257
258#ifdef ENABLE_LOGGING_AND_PROFILING
Steve Blocka7e24c12009-10-30 11:49:00 +0000259 static bool is_logging() {
Steve Block6ded16b2010-05-10 14:33:55 +0100260 return logging_nesting_ > 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 }
262
263 // Pause/Resume collection of profiling data.
264 // When data collection is paused, CPU Tick events are discarded until
265 // data collection is Resumed.
Andrei Popescu402d9372010-02-26 13:31:12 +0000266 static void PauseProfiler(int flags, int tag);
267 static void ResumeProfiler(int flags, int tag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000268 static int GetActiveProfilerModules();
269
270 // If logging is performed into a memory buffer, allows to
271 // retrieve previously written messages. See v8.h.
272 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
273
274 // Logs all compiled functions found in the heap.
275 static void LogCompiledFunctions();
Steve Blockd0582a62009-12-15 09:54:21 +0000276 // Logs all accessor callbacks found in the heap.
277 static void LogAccessorCallbacks();
278 // Used for logging stubs found in the snapshot.
Andrei Popescu31002712010-02-23 13:46:05 +0000279 static void LogCodeObjects();
Steve Blocka7e24c12009-10-30 11:49:00 +0000280
Steve Block6ded16b2010-05-10 14:33:55 +0100281 // Converts tag to a corresponding NATIVE_... if the script is native.
282 INLINE(static LogEventsAndTags ToNativeByScript(LogEventsAndTags, Script*));
Steve Blocka7e24c12009-10-30 11:49:00 +0000283
284 // Profiler's sampling interval (in milliseconds).
285 static const int kSamplingIntervalMs = 1;
286
Steve Block6ded16b2010-05-10 14:33:55 +0100287 private:
288
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 // Emits the profiler's first message.
290 static void ProfilerBeginEvent();
291
Steve Blockd0582a62009-12-15 09:54:21 +0000292 // Emits callback event messages.
293 static void CallbackEventInternal(const char* prefix,
294 const char* name,
295 Address entry_point);
296
Leon Clarked91b9f72010-01-27 17:25:45 +0000297 // Internal configurable move event.
298 static void MoveEventInternal(LogEventsAndTags event,
299 Address from,
300 Address to);
301
302 // Internal configurable move event.
303 static void DeleteEventInternal(LogEventsAndTags event,
304 Address from);
305
Steve Blocka7e24c12009-10-30 11:49:00 +0000306 // Emits the source code of a regexp. Used by regexp events.
307 static void LogRegExpSource(Handle<JSRegExp> regexp);
308
Andrei Popescu31002712010-02-23 13:46:05 +0000309 // Used for logging stubs found in the snapshot.
310 static void LogCodeObject(Object* code_object);
311
Ben Murdochf87a2032010-10-22 12:50:53 +0100312 // Emits general information about generated code.
313 static void LogCodeInfo();
314
315 // Handles code creation when low-level profiling is active.
316 static void LowLevelCodeCreateEvent(Code* code, LogMessageBuilder* msg);
317
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 // Emits a profiler tick event. Used by the profiler thread.
319 static void TickEvent(TickSample* sample, bool overflow);
320
321 static void ApiEvent(const char* name, ...);
322
323 // Logs a StringEvent regardless of whether FLAG_log is true.
324 static void UncheckedStringEvent(const char* name, const char* value);
325
Steve Block6ded16b2010-05-10 14:33:55 +0100326 // Logs an IntEvent regardless of whether FLAG_log is true.
327 static void UncheckedIntEvent(const char* name, int value);
Ben Murdochf87a2032010-10-22 12:50:53 +0100328 static void UncheckedIntPtrTEvent(const char* name, intptr_t value);
Steve Block6ded16b2010-05-10 14:33:55 +0100329
Steve Blocka7e24c12009-10-30 11:49:00 +0000330 // Stops logging and profiling in case of insufficient resources.
331 static void StopLoggingAndProfiling();
332
333 // Returns whether profiler's sampler is active.
334 static bool IsProfilerSamplerActive();
335
336 // The sampler used by the profiler and the sliding state window.
337 static Ticker* ticker_;
338
339 // When the statistical profile is active, profiler_
340 // points to a Profiler, that handles collection
341 // of samples.
342 static Profiler* profiler_;
343
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 // SlidingStateWindow instance keeping a sliding window of the most
345 // recent VM states.
346 static SlidingStateWindow* sliding_state_window_;
347
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 // Internal implementation classes with access to
349 // private members.
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 friend class EventLog;
351 friend class TimeLog;
352 friend class Profiler;
353 friend class SlidingStateWindow;
Steve Blockd0582a62009-12-15 09:54:21 +0000354 friend class StackTracer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 friend class VMState;
356
357 friend class LoggerTestHelper;
358
Steve Block6ded16b2010-05-10 14:33:55 +0100359 static int logging_nesting_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000360 static int cpu_profiler_nesting_;
361 static int heap_profiler_nesting_;
Steve Block6ded16b2010-05-10 14:33:55 +0100362
363 friend class CpuProfiler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000364#else
365 static bool is_logging() { return false; }
366#endif
367};
368
369
370// Class that extracts stack trace, used for profiling.
371class StackTracer : public AllStatic {
372 public:
373 static void Trace(TickSample* sample);
374};
375
Steve Blocka7e24c12009-10-30 11:49:00 +0000376} } // namespace v8::internal
377
Steve Block6ded16b2010-05-10 14:33:55 +0100378
Steve Blocka7e24c12009-10-30 11:49:00 +0000379#endif // V8_LOG_H_